From dfd1e334f874472e334576261c1693fa8ba7720a Mon Sep 17 00:00:00 2001 From: "hanxiaomax@qq.com" Date: Thu, 22 Feb 2024 21:42:44 +0800 Subject: [PATCH] Deployed 04d3dfd0 with MkDocs version: 1.4.2 --- 0-8-A-few-common-C++-problems/index.html | 4 +- .../index.html | 4 +- 1-10-Introduction-to-expressions/index.html | 4 +- 1-11-Developing-your-first-program/index.html | 4 +- 1-2-comments/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 1-x-Chapter-1-summary-and-quiz/index.html | 4 +- .../index.html | 4 +- 10-10-class-templates/index.html | 4 +- .../index.html | 4 +- 10-2-unscoped-enumerations/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 10-7-default-member-initialization/index.html | 4 +- 10-8-struct-passing-and-miscellany/index.html | 4 +- .../index.html | 4 +- 10-x-Chapter-10-comprehensive-quiz/index.html | 4 +- 10-y-using-a-language-reference/index.html | 4 +- 11-1-Arrays-Part-I/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 11-13-For-each-loops/index.html | 4 +- 11-14-Void-pointers/index.html | 4 +- .../index.html | 4 +- 11-16-An-introduction-to-std-array/index.html | 4 +- .../index.html | 4 +- 11-18-Introduction-to-iterators/index.html | 4 +- .../index.html | 8 +- 11-2-Arrays-Part-II/index.html | 4 +- 11-3-Arrays-and-loops/index.html | 4 +- .../index.html | 4 +- 11-5-Multidimensional-Arrays/index.html | 4 +- 11-6-C-style-strings/index.html | 4 +- 11-7-std-string-view-part-2/index.html | 4 +- 11-8-Pointers-and-arrays/index.html | 4 +- .../index.html | 4 +- .../index.html | 65 +- 12-1-function-pointers/index.html | 8 +- 12-2-the-stack-and-the-heap/index.html | 4 +- .../index.html | 4 +- 12-4-recursion/index.html | 4 +- 12-5-command-line-arguments/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 12-8-lambda-captures/index.html | 4 +- 12-x-Chapter-12-comprehensive-quiz/index.html | 4 +- .../index.html | 4 +- 13-10-the-hidden-this-pointer/index.html | 4 +- 13-11-class-code-and-header-files/index.html | 4 +- .../index.html | 4 +- 13-13-static-member-variables/index.html | 4 +- 13-14-static-member-functions/index.html | 4 +- 13-15-friend-functions-and-classes/index.html | 4 +- 13-16-anonymous-objects/index.html | 4 +- 13-17-nested-types-in-classes/index.html | 4 +- 13-18-timing-your-code/index.html | 4 +- 13-2-classes-and-class-members/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 13-5-constructors/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 13-9-destructors/index.html | 4 +- 13-x-Chapter-13-comprehensive-quiz/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 14-11-Overloading-typecasts/index.html | 4 +- 14-12-the-copy-constructor/index.html | 4 +- 14-13-copy-initialization/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 14-16-shallow-vs-deep-copying/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 14-4-overloading-the-IO-operators/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 14-x-Chapter-14-comprehensive-quiz/index.html | 4 +- 16-1-object-relationships/index.html | 4 +- 16-2-composition/index.html | 4 +- 16-3-aggregation/index.html | 4 +- 16-4-association/index.html | 4 +- 16-5-dependencies/index.html | 4 +- 16-6-container-classes/index.html | 4 +- 16-7-std-initializer_list/index.html | 4 +- 16-x-Chapter-16-comprehensive-quiz/index.html | 4 +- 17-1-introduction-to-inheritance/index.html | 4 +- 17-2-basic-inheritance-in-c++/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 17-9-multiple-inheritance/index.html | 4 +- 17-x-Chapter-17-comprehensive-quiz/index.html | 4 +- .../index.html | 4 +- 18-10-dynamic-casting/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 18-6-the-virtual-table/index.html | 4 +- .../index.html | 4 +- 18-8-virtual-base-classes/index.html | 4 +- 18-9-object-slicing/index.html | 4 +- 18-x-Chapter-18-comprehensive-quiz/index.html | 4 +- 19-x-Chapter-19-comprehensive-quiz/index.html | 4 +- 2-1-Introduction-to-functions/index.html | 4 +- .../index.html | 4 +- 2-11-Header-files/index.html | 4 +- 2-12-Header-guards/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 2-5-Introduction-to-local-scope/index.html | 6 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 2-x-Chapter-2-summary-and-quiz/index.html | 4 +- 20-x-Chapter-20-comprehensive-quiz/index.html | 4 +- 20.4/index.html | 4 +- 21-1-the-standard-library/index.html | 4 +- 21-2-STL-containers-overview/index.html | 4 +- 21-3-STL-iterators-overview/index.html | 4 +- 21-4-STL-algorithms-overview/index.html | 4 +- 22-1-std-string-and-std-wstring/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 22-6-std-string-appending/index.html | 4 +- 22-7-std-string-inserting/index.html | 4 +- 23-1-Input-and-output-IO-streams/index.html | 4 +- 23-2-Input-with-istream/index.html | 4 +- 23-3-output-with-ostream-and-ios/index.html | 4 +- 23-4-stream-classes-for-strings/index.html | 4 +- .../index.html | 4 +- 23-6-basic-file-IO/index.html | 4 +- 23-7-random-file-IO/index.html | 4 +- 3-1-Syntax-and-semantic-errors/index.html | 4 +- .../index.html | 4 +- 3-2-The-debugging-process/index.html | 4 +- 3-3-A-strategy-for-debugging/index.html | 4 +- 3-4-Basic-debugging-tactics/index.html | 4 +- 3-5-More-debugging-tactics/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 3-x-Chapter-3-summary-and-quiz/index.html | 4 +- .../index.html | 4 +- 4-10-Introduction-to-if-statements/index.html | 4 +- 4-11-Chars/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 4-15-Literals/index.html | 4 +- .../index.html | 4 +- 4-17-an-introduction-to-std-string/index.html | 4 +- .../index.html | 4 +- 4-2-Void/index.html | 4 +- .../index.html | 4 +- 4-4-Signed-integers/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 4-8-Floating-point-numbers/index.html | 4 +- 4-9-Boolean-values/index.html | 4 +- 4-x-Chapter-4-summary-and-quiz/index.html | 4 +- 404.html | 4 +- .../index.html | 4 +- 5-2-Arithmetic-operators/index.html | 4 +- 5-3-Modulus-and-Exponentiation/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 5-7-Logical-operators/index.html | 4 +- 5-x-Chapter-5-summary-and-quiz/index.html | 4 +- 6-1-Compound-statements-blocks/index.html | 4 +- 6-10-Static-local-variables/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 6-13-Inline-functions/index.html | 4 +- .../index.html | 4 +- 6-15-Unnamed-and-inline-namespaces/index.html | 4 +- .../index.html | 42 +- 6-3-Local-variables/index.html | 4 +- .../index.html | 4 +- 6-5-Variable-shadowing-name-hiding/index.html | 4 +- 6-6-Internal-linkage/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 6-x-Chapter-6-summary-and-quiz/index.html | 4 +- 7-1-Control-flow-introduction/index.html | 4 +- 7-10-Break-and-continue/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 7-13-Code-coverage/index.html | 4 +- 7-14-Common-semantic-errors-in-c/index.html | 4 +- 7-15-Detecting-and-handling-errors/index.html | 4 +- .../index.html | 4 +- 7-17-assert-and-static-assert/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 7-2-If-statements-and-blocks/index.html | 4 +- 7-3-Common-if-statement-problems/index.html | 4 +- 7-4-Switch-statement-basics/index.html | 4 +- 7-5-Switch-fallthrough-and-scoping/index.html | 4 +- 7-6-Goto-statements/index.html | 4 +- .../index.html | 4 +- 7-8-Do-while-statements/index.html | 4 +- 7-9-For-statements/index.html | 4 +- 7-x-Chapter-7-summary-and-quiz/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 8-12-Default-arguments/index.html | 4 +- 8-13-Function-templates/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 8-3-Numeric-conversions/index.html | 4 +- 8-4-Arithmetic-conversions/index.html | 4 +- .../index.html | 4 +- 8-6-Typedefs-and-type-aliases/index.html | 4 +- .../index.html | 4 +- 8-8-Type-deduction-for-functions/index.html | 4 +- .../index.html | 4 +- 8-x-Chapter-8-summary-and-quiz/index.html | 4 +- .../index.html | 4 +- 9-10-Pass-by-address-part-2/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- 9-3-Lvalue-references/index.html | 4 +- 9-4-Lvalue-references-to-const/index.html | 4 +- 9-5-Pass-by-lvalue-reference/index.html | 4 +- 9-6-Introduction-to-pointers/index.html | 4 +- 9-7-Null-pointers/index.html | 4 +- 9-8-Pointers-and-const/index.html | 4 +- 9-9-Pass-by-address/index.html | 4 +- 9-x-Chapter-9-comprehensive-quiz/index.html | 4 +- A-1-static-and-dynamic-libraries/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- A-4-C++FAQ/index.html | 4 +- Associations/index.html | 4 +- Associative-containers/index.html | 4 +- B-1-introduction-to-C++11/index.html | 4 +- B-2-introduction-to-C++14/index.html | 4 +- B-3-introduction-to-C++17/index.html | 4 +- B-4-introduction-to-C++20/index.html | 4 +- Binding/index.html | 4 +- C-style-casts/index.html | 4 +- Composition/index.html | 4 +- Early-binding/index.html | 4 +- Encapsulation/index.html | 4 +- Expression-parameters/index.html | 4 +- Frankenobject/index.html | 4 +- Late-binding/index.html | 4 +- .../index.html | 4 +- M-2-R-value-references/index.html | 4 +- .../index.html | 164 ++- M-4-std-move/index.html | 4 +- M-5-std-move-if-noexcept/index.html | 4 +- M-6-std-unique-ptr/index.html | 8 +- M-7-std-shared-ptr/index.html | 4 +- .../index.html | 4 +- M-x-chapter-M-comprehensive-review/index.html | 4 +- MOC/index.html | 6 +- Multiple inheritance/index.html | 4 +- Name-mangling/index.html | 4 +- .../index.html | 4 +- O-2-bitwise-operators/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- Off-by-one/index.html | 4 +- Output-manipulators/index.html | 4 +- .../index.html | 4 +- Run-time-type-information-RTTI/index.html | 4 +- Sequence-containers/index.html | 4 +- Template-specialization/index.html | 4 +- abbreviated function templates/index.html | 4 +- abstract-class/index.html | 4 +- abstraction/index.html | 4 +- access-function/index.html | 4 +- access-specifiers/index.html | 4 +- address-of-operator/index.html | 4 +- aggregate-data-type/index.html | 4 +- aggregate-initialization/index.html | 4 +- aggregation/index.html | 4 +- ambiguous-match/index.html | 4 +- anonymous-object/index.html | 4 +- arguments/index.html | 4 +- arity/index.html | 4 +- automatic-memory-allocation/index.html | 4 +- automatic-storage-duration/index.html | 4 +- base-class/index.html | 4 +- bit/index.html | 4 +- bitwise-and/index.html | 4 +- bitwise-or/index.html | 4 +- bss segment/index.html | 4 +- byte/index.html | 4 +- call-stack/index.html | 4 +- callable-object/index.html | 4 +- callback functions/index.html | 4 +- child-class/index.html | 4 +- class-instance/index.html | 4 +- class-template-argument-deduction/index.html | 4 +- class-template/index.html | 4 +- class-type/index.html | 4 +- closure/index.html | 4 +- code segment/index.html | 4 +- compile-time/index.html | 4 +- compiler/index.html | 4 +- composite-types/index.html | 4 +- const-cast/index.html | 4 +- const-member-function/index.html | 4 +- const-pointer/index.html | 4 +- constant expression/index.html | 4 +- constructor/index.html | 4 +- container-adapter/index.html | 4 +- container-class/index.html | 4 +- converting-constructors/index.html | 4 +- copy-assignment-operator/index.html | 4 +- copy-constructor/index.html | 4 +- copy-constructors/index.html | 4 +- copy-initialization/index.html | 4 +- copy-semantics/index.html | 4 +- .../index.html" | 4 +- covariant/index.html | 4 +- dangling/index.html | 4 +- data segment/index.html | 4 +- declaration/index.html | 4 +- deep-copy/index.html | 4 +- default-constructor/index.html | 4 +- default-initialization/index.html | 4 +- default-member-initializer/index.html | 4 +- definition/index.html | 4 +- delegating-constructors/index.html | 4 +- dependency/index.html | 4 +- dereference-operator/index.html | 4 +- derived-class/index.html | 4 +- designated-initializers/index.html | 4 +- destructor/index.html | 4 +- direct-initialization/index.html | 4 +- downcasting/index.html | 4 +- dumb-pointer/index.html | 4 +- dynamic-array/index.html | 4 +- dynamic-casts/index.html | 4 +- dynamic-duration/index.html | 4 +- dynamic-initialization/index.html | 4 +- dynamic-memory-allocation/index.html | 4 +- enum-class/index.html | 4 +- evaluation/index.html | 4 +- exception-safety-issue/index.html | 4 +- explicit-type-conversion/index.html | 4 +- expression-type/index.html | 4 +- external-linkage/index.html | 4 +- external-variable/index.html | 4 +- extraction-operator/index.html | 4 +- fallthrough/index.html | 4 +- file-scope/index.html | 4 +- fixed-array/index.html | 4 +- format-group/index.html | 4 +- forward-declaration/index.html | 4 +- friend-class/index.html | 4 +- friend-function/index.html | 4 +- function-instance/index.html | 4 +- function-literal/index.html | 4 +- function-pointer/index.html | 4 +- function-prototype/index.html | 4 +- function-template-instantiation/index.html | 4 +- function-template/index.html | 4 +- generic-algorithm/index.html | 4 +- global-variable/index.html | 4 +- going-out-of-scope/index.html | 4 +- header-guard/index.html | 4 +- heap/index.html | 4 +- identifier/index.html | 4 +- identity/index.html | 4 +- .../index.html | 4 +- .../Add Link and Open Page/index.html | 4 +- .../Box Selected Elements/index.html | 4 +- .../Downloaded/Bullet Point/index.html | 4 +- .../Convert freedraw to line/index.html | 4 +- .../index.html | 4 +- .../index.html | 4 +- .../Expand rectangles horizontally/index.html | 4 +- .../index.html | 4 +- .../Expand rectangles vertically/index.html | 4 +- .../Downloaded/Fixed spacing/index.html | 4 +- .../Fixed vertical distance/index.html | 4 +- .../index.html | 4 +- .../Normalize Selected Arrows/index.html | 4 +- .../Downloaded/Organic Line/index.html | 4 +- .../Downloaded/Repeat Elements/index.html | 4 +- .../Downloaded/Set Dimensions/index.html | 4 +- .../Downloaded/Set Text Alignment/index.html | 4 +- .../Downloaded/Split text by lines/index.html | 4 +- .../Downloaded/TheBrain-navigation/index.html | 4 +- images/onepage/Template.excalidraw/index.html | 4 +- images/onepage/c++-basic-onepage/index.html | 4 +- images/onepage/moc-onepage/index.html | 4 +- immediate-functions/index.html | 4 +- implicit-constructor/index.html | 4 +- implicit-object/index.html | 4 +- implicit-type-conversion/index.html | 4 +- index.html | 4 +- inheritance/index.html | 4 +- initialization/index.html | 4 +- initializer-list/index.html | 4 +- initializer/index.html | 4 +- inline-function/index.html | 4 +- inline-namespace/index.html | 4 +- inline-variables/index.html | 4 +- input-manipulators/index.html | 4 +- insertion-operator/index.html | 4 +- instance/index.html | 4 +- instantiated/index.html | 4 +- instuction/index.html | 4 +- integral-type/index.html | 4 +- interface-class/index.html | 4 +- internal-linkage/index.html | 4 +- internal-variable/index.html | 4 +- iterating/index.html | 4 +- iterator/index.html | 4 +- lambda/index.html | 4 +- link/index.html | 4 +- linkage/index.html | 4 +- linker/index.html | 4 +- list assignment operator/index.html | 4 +- list-constructor/index.html | 4 +- list-initialization/index.html | 4 +- literals/index.html | 4 +- local-variable/index.html | 4 +- lvalue-reference/index.html | 4 +- lvalue/index.html | 4 +- macro/index.html | 4 +- member-access-operator/index.html | 4 +- member-function/index.html | 4 +- member-initializer-list/index.html | 4 +- member-selection-operator/index.html | 4 +- member-types/index.html | 4 +- member-variable/index.html | 4 +- memberwise initialization/index.html | 4 +- memberwise-assignment/index.html | 4 +- memberwise-copy/index.html | 4 +- memory-leak/index.html | 4 +- mixin/index.html | 4 +- move-assignment-operator/index.html | 4 +- move-constructor/index.html | 4 +- move-semantics/index.html | 4 +- named-cast/index.html | 4 +- namespace/index.html | 4 +- narrowing-convertions/index.html | 4 +- non-static-member-initialization/index.html | 4 +- numeric promotions/index.html | 4 +- numeric-conversions/index.html | 4 +- object-composition/index.html | 4 +- object-like-macros/index.html | 4 +- object-slicing/index.html | 4 +- object/index.html | 4 +- one-definition-rule/index.html | 4 +- operands/index.html | 4 +- operator/index.html | 4 +- optional-parameter/index.html | 4 +- out-of-scope/index.html | 4 +- overload-resolution/index.html | 4 +- overload/index.html | 4 +- override/index.html | 4 +- parameters/index.html | 4 +- parent-class/index.html | 4 +- pass-by-address/index.html | 4 +- pass-by-reference/index.html | 4 +- pass-by-value/index.html | 4 +- placeholder-types/index.html | 4 +- pointer-to-const/index.html | 4 +- polymorphism/index.html | 4 +- preprocessor-directive/index.html | 4 +- preprocessor/index.html | 4 +- private-inheritance/index.html | 4 +- private-member/index.html | 4 +- protected-inheritance/index.html | 4 +- protected-members/index.html | 4 +- public-inheritance/index.html | 4 +- public-member/index.html | 4 +- pure-declaration/index.html | 4 +- pure-virtual/index.html | 4 +- qualifier/index.html | 4 +- quiz/index.html | 4 +- random-access-memory/index.html | 4 +- range-based-for-loops/index.html | 4 +- raw-pointer/index.html | 4 +- refactoring/index.html | 4 +- reference-container/index.html | 4 +- reflexive association/index.html | 4 +- reinterpret-casts/index.html | 4 +- return-by-address/index.html | 4 +- return-by-reference/index.html | 4 +- return-by-value/index.html | 4 +- return-type-deduction/index.html | 4 +- rounding-error/index.html | 4 +- runtime/index.html | 4 +- rvalue-reference/index.html | 4 +- rvalue/index.html | 4 +- scaling/index.html | 4 +- scope-resolution-operator/index.html | 4 +- scope/index.html | 4 +- scoped-enumerations/index.html | 4 +- search/search_index.json | 2 +- segment/index.html | 4 +- sentinel/index.html | 4 +- shadow/index.html | 4 +- shallow-copy/index.html | 4 +- side-effects/index.html | 4 +- signature/index.html | 4 +- sitemap.xml | 1190 ++++++++--------- sitemap.xml.gz | Bin 468 -> 468 bytes smart pointer class/index.html | 4 +- smart-pointer/index.html | 4 +- snake-case/index.html | 4 +- stack-frame/index.html | 4 +- stack-overflow/index.html | 4 +- stack/index.html | 4 +- standard-stream/index.html | 4 +- statement/index.html | 4 +- static-casts/index.html | 4 +- static-initialization/index.html | 4 +- static-member-functions/index.html | 4 +- static-member-variables/index.html | 4 +- static-memory-allocation/index.html | 4 +- static-storage-duration/index.html | 4 +- static-variables/index.html | 4 +- storage-duration/index.html | 4 +- strong-except/index.html | 4 +- struct-padding/index.html | 4 +- stub/index.html | 4 +- subscripts/index.html | 4 +- super-class/index.html | 4 +- symbolic-constants/index.html | 4 +- syntax/index.html | 4 +- tabledata/index.html | 4 +- tags/index.html | 6 +- template-class-specialization/index.html | 4 +- template-type-parameters/index.html | 4 +- the-diamond-problem/index.html | 4 +- transitive includes/index.html | 4 +- translation/index.html | 4 +- trivial-conversion/index.html | 4 +- type deduction/index.html | 4 +- type-aliases/index.html | 4 +- type-conversion/index.html | 4 +- type-definition/index.html | 4 +- type-inference/index.html | 4 +- type-safe/index.html | 4 +- type-signature/index.html | 4 +- typeid/index.html | 4 +- undefined-behavior/index.html | 4 +- uniform-initialization/index.html | 4 +- unnamed-namespace/index.html | 4 +- unscoped-enumerations/index.html | 4 +- upcasting/index.html | 4 +- using-declaration/index.html | 4 +- using-directive/index.html | 4 +- value-category/index.html | 4 +- value-container/index.html | 4 +- value-initialization/index.html | 4 +- value-preserving/index.html | 4 +- virtual-base-class/index.html | 4 +- virtual-destructor/index.html | 4 +- virtual-function/index.html | 4 +- virtual-table/index.html | 4 +- void pointer/index.html | 4 +- wild-pointer/index.html | 4 +- word/index.html | 4 +- zero-initialization/index.html | 4 +- .../index.html" | 4 +- .../index.html" | 4 +- 598 files changed, 1999 insertions(+), 1850 deletions(-) rename {11-x-chapter-11-comprehensive-quiz => 11-x-Chapter-11-comprehensive-quiz}/index.html (98%) diff --git a/0-8-A-few-common-C++-problems/index.html b/0-8-A-few-common-C++-problems/index.html index a4bb4b0bf..c395f0e6a 100644 --- a/0-8-A-few-common-C++-problems/index.html +++ b/0-8-A-few-common-C++-problems/index.html @@ -3251,8 +3251,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/1-1-Statements-and-the-structure-of-a-program/index.html b/1-1-Statements-and-the-structure-of-a-program/index.html index d3334f090..13a713e6f 100644 --- a/1-1-Statements-and-the-structure-of-a-program/index.html +++ b/1-1-Statements-and-the-structure-of-a-program/index.html @@ -3327,8 +3327,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/1-10-Introduction-to-expressions/index.html b/1-10-Introduction-to-expressions/index.html index 02fc0ed31..c58ba3223 100644 --- a/1-10-Introduction-to-expressions/index.html +++ b/1-10-Introduction-to-expressions/index.html @@ -3306,8 +3306,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/1-11-Developing-your-first-program/index.html b/1-11-Developing-your-first-program/index.html index b251dee98..262569f7b 100644 --- a/1-11-Developing-your-first-program/index.html +++ b/1-11-Developing-your-first-program/index.html @@ -3320,8 +3320,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/1-2-comments/index.html b/1-2-comments/index.html index 1a407abd5..cd6a8c5aa 100644 --- a/1-2-comments/index.html +++ b/1-2-comments/index.html @@ -3347,8 +3347,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/1-3-Introduction-to-objects-and-variables/index.html b/1-3-Introduction-to-objects-and-variables/index.html index e19a2bc1e..ab3e9ce7c 100644 --- a/1-3-Introduction-to-objects-and-variables/index.html +++ b/1-3-Introduction-to-objects-and-variables/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/1-4-Variable-assignment-and-initialization/index.html b/1-4-Variable-assignment-and-initialization/index.html index 1a76ef140..eadaae45c 100644 --- a/1-4-Variable-assignment-and-initialization/index.html +++ b/1-4-Variable-assignment-and-initialization/index.html @@ -3355,8 +3355,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/1-5-Introduction-to-iostream-cout-cin-and-endl/index.html b/1-5-Introduction-to-iostream-cout-cin-and-endl/index.html index 7744a4641..6fb362e0c 100644 --- a/1-5-Introduction-to-iostream-cout-cin-and-endl/index.html +++ b/1-5-Introduction-to-iostream-cout-cin-and-endl/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/1-6-Uninitialized-variables-and-undefined-behavior/index.html b/1-6-Uninitialized-variables-and-undefined-behavior/index.html index 84048b535..908679410 100644 --- a/1-6-Uninitialized-variables-and-undefined-behavior/index.html +++ b/1-6-Uninitialized-variables-and-undefined-behavior/index.html @@ -3306,8 +3306,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/1-7-Keywords-and-naming-identifiers/index.html b/1-7-Keywords-and-naming-identifiers/index.html index 84ebe0262..d5e2c3414 100644 --- a/1-7-Keywords-and-naming-identifiers/index.html +++ b/1-7-Keywords-and-naming-identifiers/index.html @@ -3313,8 +3313,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/1-8-Whitespace-and-basic-formatting/index.html b/1-8-Whitespace-and-basic-formatting/index.html index 9d1e0d737..f467144bf 100644 --- a/1-8-Whitespace-and-basic-formatting/index.html +++ b/1-8-Whitespace-and-basic-formatting/index.html @@ -3306,8 +3306,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/1-9-Introduction-to-literals-and-operators/index.html b/1-9-Introduction-to-literals-and-operators/index.html index c88a6ff51..5143a6492 100644 --- a/1-9-Introduction-to-literals-and-operators/index.html +++ b/1-9-Introduction-to-literals-and-operators/index.html @@ -3313,8 +3313,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/1-x-Chapter-1-summary-and-quiz/index.html b/1-x-Chapter-1-summary-and-quiz/index.html index c18218789..86f4990e4 100644 --- a/1-x-Chapter-1-summary-and-quiz/index.html +++ b/1-x-Chapter-1-summary-and-quiz/index.html @@ -3299,8 +3299,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/10-1-Introduction-to-program-defined-user-defined-types/index.html b/10-1-Introduction-to-program-defined-user-defined-types/index.html index e4c581317..cab1ec7da 100644 --- a/10-1-Introduction-to-program-defined-user-defined-types/index.html +++ b/10-1-Introduction-to-program-defined-user-defined-types/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/10-10-class-templates/index.html b/10-10-class-templates/index.html index c62e8fd58..2ae85b674 100644 --- a/10-10-class-templates/index.html +++ b/10-10-class-templates/index.html @@ -3341,8 +3341,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/10-11-class-template-argument-deduction-and-deduction -guides/index.html b/10-11-class-template-argument-deduction-and-deduction -guides/index.html index 6d98aa5a1..98632f29f 100644 --- a/10-11-class-template-argument-deduction-and-deduction -guides/index.html +++ b/10-11-class-template-argument-deduction-and-deduction -guides/index.html @@ -3306,8 +3306,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/10-2-unscoped-enumerations/index.html b/10-2-unscoped-enumerations/index.html index 60282ae20..1c22ea64a 100644 --- a/10-2-unscoped-enumerations/index.html +++ b/10-2-unscoped-enumerations/index.html @@ -3348,8 +3348,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/10-3-unscoped-enumeration-input-and-output/index.html b/10-3-unscoped-enumeration-input-and-output/index.html index 357219b0c..f2a95ddab 100644 --- a/10-3-unscoped-enumeration-input-and-output/index.html +++ b/10-3-unscoped-enumeration-input-and-output/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/10-4-scoped-enumerations-enum-classes/index.html b/10-4-scoped-enumerations-enum-classes/index.html index b3f367623..6cb73ec16 100644 --- a/10-4-scoped-enumerations-enum-classes/index.html +++ b/10-4-scoped-enumerations-enum-classes/index.html @@ -3327,8 +3327,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/10-5-Introduction-to-structs-members-and-member-selection/index.html b/10-5-Introduction-to-structs-members-and-member-selection/index.html index 0c49716a8..aad0c5373 100644 --- a/10-5-Introduction-to-structs-members-and-member-selection/index.html +++ b/10-5-Introduction-to-structs-members-and-member-selection/index.html @@ -3313,8 +3313,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/10-6-struct-aggregate-initialization/index.html b/10-6-struct-aggregate-initialization/index.html index eee02baaa..cf39fcad0 100644 --- a/10-6-struct-aggregate-initialization/index.html +++ b/10-6-struct-aggregate-initialization/index.html @@ -3348,8 +3348,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/10-7-default-member-initialization/index.html b/10-7-default-member-initialization/index.html index b3c071bf8..a581e6fd6 100644 --- a/10-7-default-member-initialization/index.html +++ b/10-7-default-member-initialization/index.html @@ -3327,8 +3327,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/10-8-struct-passing-and-miscellany/index.html b/10-8-struct-passing-and-miscellany/index.html index d7cd60613..8cd8ddc26 100644 --- a/10-8-struct-passing-and-miscellany/index.html +++ b/10-8-struct-passing-and-miscellany/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/10-9-member-selection-with-pointers-and-references/index.html b/10-9-member-selection-with-pointers-and-references/index.html index 8575870d0..3ac59a0e5 100644 --- a/10-9-member-selection-with-pointers-and-references/index.html +++ b/10-9-member-selection-with-pointers-and-references/index.html @@ -3313,8 +3313,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/10-x-Chapter-10-comprehensive-quiz/index.html b/10-x-Chapter-10-comprehensive-quiz/index.html index d21591687..e94b2b602 100644 --- a/10-x-Chapter-10-comprehensive-quiz/index.html +++ b/10-x-Chapter-10-comprehensive-quiz/index.html @@ -3299,8 +3299,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/10-y-using-a-language-reference/index.html b/10-y-using-a-language-reference/index.html index 52c508831..ebfee3acc 100644 --- a/10-y-using-a-language-reference/index.html +++ b/10-y-using-a-language-reference/index.html @@ -3327,8 +3327,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/11-1-Arrays-Part-I/index.html b/11-1-Arrays-Part-I/index.html index 43bb3ce87..b8c830111 100644 --- a/11-1-Arrays-Part-I/index.html +++ b/11-1-Arrays-Part-I/index.html @@ -3341,8 +3341,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/11-10-C-style-string-symbolic-constants/index.html b/11-10-C-style-string-symbolic-constants/index.html index 1100104ce..6d14ef7df 100644 --- a/11-10-C-style-string-symbolic-constants/index.html +++ b/11-10-C-style-string-symbolic-constants/index.html @@ -3256,8 +3256,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/11-11-Dynamic-memory-allocation-with-new-and-delete/index.html b/11-11-Dynamic-memory-allocation-with-new-and-delete/index.html index ec25719aa..356865364 100644 --- a/11-11-Dynamic-memory-allocation-with-new-and-delete/index.html +++ b/11-11-Dynamic-memory-allocation-with-new-and-delete/index.html @@ -3256,8 +3256,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/11-12-Dynamically-allocating-arrays/index.html b/11-12-Dynamically-allocating-arrays/index.html index b37b81452..0af390e34 100644 --- a/11-12-Dynamically-allocating-arrays/index.html +++ b/11-12-Dynamically-allocating-arrays/index.html @@ -3256,8 +3256,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/11-13-For-each-loops/index.html b/11-13-For-each-loops/index.html index dcfba1897..f87a732fd 100644 --- a/11-13-For-each-loops/index.html +++ b/11-13-For-each-loops/index.html @@ -3348,8 +3348,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/11-14-Void-pointers/index.html b/11-14-Void-pointers/index.html index c27283859..23a259298 100644 --- a/11-14-Void-pointers/index.html +++ b/11-14-Void-pointers/index.html @@ -3306,8 +3306,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/11-15-Pointers-to-pointers-and-dynamic-multidimensional-arrays/index.html b/11-15-Pointers-to-pointers-and-dynamic-multidimensional-arrays/index.html index 7de82819d..d457962ce 100644 --- a/11-15-Pointers-to-pointers-and-dynamic-multidimensional-arrays/index.html +++ b/11-15-Pointers-to-pointers-and-dynamic-multidimensional-arrays/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/11-16-An-introduction-to-std-array/index.html b/11-16-An-introduction-to-std-array/index.html index cd8c2ff5d..4d3604416 100644 --- a/11-16-An-introduction-to-std-array/index.html +++ b/11-16-An-introduction-to-std-array/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/11-17-An-introduction-to-std-vector/index.html b/11-17-An-introduction-to-std-vector/index.html index bf04eb499..2f6801feb 100644 --- a/11-17-An-introduction-to-std-vector/index.html +++ b/11-17-An-introduction-to-std-vector/index.html @@ -3341,8 +3341,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/11-18-Introduction-to-iterators/index.html b/11-18-Introduction-to-iterators/index.html index 912b4ac6a..d6feb895f 100644 --- a/11-18-Introduction-to-iterators/index.html +++ b/11-18-Introduction-to-iterators/index.html @@ -3327,8 +3327,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/11-19-Introduction-to-standard-library-algorithms/index.html b/11-19-Introduction-to-standard-library-algorithms/index.html index c28f276a5..0507c7d2b 100644 --- a/11-19-Introduction-to-standard-library-algorithms/index.html +++ b/11-19-Introduction-to-standard-library-algorithms/index.html @@ -3348,8 +3348,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • @@ -6560,13 +6560,13 @@

    结论

    - -

    此时我们不经要问,”那么实例化的变量是在合适被销毁的呢?“。局部变量是在包含其定义的一组花括号的结尾时销毁的,其销毁的顺序和定义顺序正好相反(对于形参来说,则是在函数结束时销毁)。

    +

    此时我们不经要问,”那么实例化的变量是在何时被销毁的呢?“。局部变量是在包含其定义的一组花括号的结尾时销毁的,其销毁的顺序和定义顺序正好相反(对于形参来说,则是在函数结束时销毁)。

    1
     2
     3
    diff --git a/2-6-Why-functions-are-useful-and-how-to-use-them-effectively/index.html b/2-6-Why-functions-are-useful-and-how-to-use-them-effectively/index.html
    index 55285584d..24e93772e 100644
    --- a/2-6-Why-functions-are-useful-and-how-to-use-them-effectively/index.html
    +++ b/2-6-Why-functions-are-useful-and-how-to-use-them-effectively/index.html
    @@ -3306,8 +3306,8 @@
       
       
         
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/2-7-Forward-declarations-and-definitions/index.html b/2-7-Forward-declarations-and-definitions/index.html index 12ecd0b9c..0fa7330b4 100644 --- a/2-7-Forward-declarations-and-definitions/index.html +++ b/2-7-Forward-declarations-and-definitions/index.html @@ -3327,8 +3327,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/2-8-Programs-with-multiple-code-files/index.html b/2-8-Programs-with-multiple-code-files/index.html index 43972261e..bcfe995a0 100644 --- a/2-8-Programs-with-multiple-code-files/index.html +++ b/2-8-Programs-with-multiple-code-files/index.html @@ -3320,8 +3320,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/2-9-Naming-collisions-and-an-introduction-to-namespaces/index.html b/2-9-Naming-collisions-and-an-introduction-to-namespaces/index.html index 58cc62a32..a739782e1 100644 --- a/2-9-Naming-collisions-and-an-introduction-to-namespaces/index.html +++ b/2-9-Naming-collisions-and-an-introduction-to-namespaces/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/2-x-Chapter-2-summary-and-quiz/index.html b/2-x-Chapter-2-summary-and-quiz/index.html index 4a9b959b0..d4c822d68 100644 --- a/2-x-Chapter-2-summary-and-quiz/index.html +++ b/2-x-Chapter-2-summary-and-quiz/index.html @@ -3299,8 +3299,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/20-x-Chapter-20-comprehensive-quiz/index.html b/20-x-Chapter-20-comprehensive-quiz/index.html index e356d1d2c..bc9de97ee 100644 --- a/20-x-Chapter-20-comprehensive-quiz/index.html +++ b/20-x-Chapter-20-comprehensive-quiz/index.html @@ -3255,8 +3255,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/20.4/index.html b/20.4/index.html index dbada7cd9..530099906 100644 --- a/20.4/index.html +++ b/20.4/index.html @@ -3251,8 +3251,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/21-1-the-standard-library/index.html b/21-1-the-standard-library/index.html index 9e6675261..d98255b67 100644 --- a/21-1-the-standard-library/index.html +++ b/21-1-the-standard-library/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/21-2-STL-containers-overview/index.html b/21-2-STL-containers-overview/index.html index 095a8569f..531aa1dff 100644 --- a/21-2-STL-containers-overview/index.html +++ b/21-2-STL-containers-overview/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/21-3-STL-iterators-overview/index.html b/21-3-STL-iterators-overview/index.html index a3d4b9bda..11c51ae05 100644 --- a/21-3-STL-iterators-overview/index.html +++ b/21-3-STL-iterators-overview/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/21-4-STL-algorithms-overview/index.html b/21-4-STL-algorithms-overview/index.html index b281eaba8..cc8ab9419 100644 --- a/21-4-STL-algorithms-overview/index.html +++ b/21-4-STL-algorithms-overview/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/22-1-std-string-and-std-wstring/index.html b/22-1-std-string-and-std-wstring/index.html index dd94e801e..695b94f37 100644 --- a/22-1-std-string-and-std-wstring/index.html +++ b/22-1-std-string-and-std-wstring/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/22-2-std-string-construction-and-destruction/index.html b/22-2-std-string-construction-and-destruction/index.html index b776bd528..341057939 100644 --- a/22-2-std-string-construction-and-destruction/index.html +++ b/22-2-std-string-construction-and-destruction/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/22-3-std-string-length-and-capacity/index.html b/22-3-std-string-length-and-capacity/index.html index cff2984f2..25b3d49ba 100644 --- a/22-3-std-string-length-and-capacity/index.html +++ b/22-3-std-string-length-and-capacity/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/22-4-std-string-character-access-and-conversion-to-C-style-arrays/index.html b/22-4-std-string-character-access-and-conversion-to-C-style-arrays/index.html index 130acaed4..bf259dcb9 100644 --- a/22-4-std-string-character-access-and-conversion-to-C-style-arrays/index.html +++ b/22-4-std-string-character-access-and-conversion-to-C-style-arrays/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/22-5-std-string-assignment-and-swapping/index.html b/22-5-std-string-assignment-and-swapping/index.html index c25cfeab1..ef0b96270 100644 --- a/22-5-std-string-assignment-and-swapping/index.html +++ b/22-5-std-string-assignment-and-swapping/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/22-6-std-string-appending/index.html b/22-6-std-string-appending/index.html index 0a65bc5aa..0721c73e2 100644 --- a/22-6-std-string-appending/index.html +++ b/22-6-std-string-appending/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/22-7-std-string-inserting/index.html b/22-7-std-string-inserting/index.html index d92d8206f..feadb56e4 100644 --- a/22-7-std-string-inserting/index.html +++ b/22-7-std-string-inserting/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/23-1-Input-and-output-IO-streams/index.html b/23-1-Input-and-output-IO-streams/index.html index b99902e1f..f3d92468b 100644 --- a/23-1-Input-and-output-IO-streams/index.html +++ b/23-1-Input-and-output-IO-streams/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/23-2-Input-with-istream/index.html b/23-2-Input-with-istream/index.html index 407cecce8..36854964d 100644 --- a/23-2-Input-with-istream/index.html +++ b/23-2-Input-with-istream/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/23-3-output-with-ostream-and-ios/index.html b/23-3-output-with-ostream-and-ios/index.html index 8f6d1e999..e6e025f77 100644 --- a/23-3-output-with-ostream-and-ios/index.html +++ b/23-3-output-with-ostream-and-ios/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/23-4-stream-classes-for-strings/index.html b/23-4-stream-classes-for-strings/index.html index ab610efc4..84f6b4268 100644 --- a/23-4-stream-classes-for-strings/index.html +++ b/23-4-stream-classes-for-strings/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/23-5-stream-states-and-input-validation/index.html b/23-5-stream-states-and-input-validation/index.html index 84599f486..23b573c9a 100644 --- a/23-5-stream-states-and-input-validation/index.html +++ b/23-5-stream-states-and-input-validation/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/23-6-basic-file-IO/index.html b/23-6-basic-file-IO/index.html index d7b2bc540..7c908eee1 100644 --- a/23-6-basic-file-IO/index.html +++ b/23-6-basic-file-IO/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/23-7-random-file-IO/index.html b/23-7-random-file-IO/index.html index e94f86c4b..00938033a 100644 --- a/23-7-random-file-IO/index.html +++ b/23-7-random-file-IO/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/3-1-Syntax-and-semantic-errors/index.html b/3-1-Syntax-and-semantic-errors/index.html index 82bfc4840..bb95c981f 100644 --- a/3-1-Syntax-and-semantic-errors/index.html +++ b/3-1-Syntax-and-semantic-errors/index.html @@ -3299,8 +3299,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/3-10-Finding-issues-before-they-become-problems/index.html b/3-10-Finding-issues-before-they-become-problems/index.html index 539e32589..44d0f929a 100644 --- a/3-10-Finding-issues-before-they-become-problems/index.html +++ b/3-10-Finding-issues-before-they-become-problems/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/3-2-The-debugging-process/index.html b/3-2-The-debugging-process/index.html index 0c915c421..6ca1e4edf 100644 --- a/3-2-The-debugging-process/index.html +++ b/3-2-The-debugging-process/index.html @@ -3299,8 +3299,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/3-3-A-strategy-for-debugging/index.html b/3-3-A-strategy-for-debugging/index.html index 7454474a8..5b3ba9de7 100644 --- a/3-3-A-strategy-for-debugging/index.html +++ b/3-3-A-strategy-for-debugging/index.html @@ -3320,8 +3320,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/3-4-Basic-debugging-tactics/index.html b/3-4-Basic-debugging-tactics/index.html index 13b398fec..ab3f9aac0 100644 --- a/3-4-Basic-debugging-tactics/index.html +++ b/3-4-Basic-debugging-tactics/index.html @@ -3320,8 +3320,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/3-5-More-debugging-tactics/index.html b/3-5-More-debugging-tactics/index.html index 2ff07c984..9f7a94349 100644 --- a/3-5-More-debugging-tactics/index.html +++ b/3-5-More-debugging-tactics/index.html @@ -3306,8 +3306,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/3-6-Using-an-integrated-debugger-Stepping/index.html b/3-6-Using-an-integrated-debugger-Stepping/index.html index c48393d29..c0a0ea2fa 100644 --- a/3-6-Using-an-integrated-debugger-Stepping/index.html +++ b/3-6-Using-an-integrated-debugger-Stepping/index.html @@ -3341,8 +3341,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/3-7-Using-an-integrated-debugger-Running-and-breakpoints/index.html b/3-7-Using-an-integrated-debugger-Running-and-breakpoints/index.html index dfa1fe08a..2879a0c8b 100644 --- a/3-7-Using-an-integrated-debugger-Running-and-breakpoints/index.html +++ b/3-7-Using-an-integrated-debugger-Running-and-breakpoints/index.html @@ -3299,8 +3299,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/3-8-Using-an-integrated-debugger-Watching-variables/index.html b/3-8-Using-an-integrated-debugger-Watching-variables/index.html index bcfbbe5d1..057a6521e 100644 --- a/3-8-Using-an-integrated-debugger-Watching-variables/index.html +++ b/3-8-Using-an-integrated-debugger-Watching-variables/index.html @@ -3265,8 +3265,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/3-9-Using-an-integrated-debugger-The-call-stack/index.html b/3-9-Using-an-integrated-debugger-The-call-stack/index.html index 38c2eaceb..8e9175a00 100644 --- a/3-9-Using-an-integrated-debugger-The-call-stack/index.html +++ b/3-9-Using-an-integrated-debugger-The-call-stack/index.html @@ -3265,8 +3265,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/3-x-Chapter-3-summary-and-quiz/index.html b/3-x-Chapter-3-summary-and-quiz/index.html index 9aef5ee14..e5be112a1 100644 --- a/3-x-Chapter-3-summary-and-quiz/index.html +++ b/3-x-Chapter-3-summary-and-quiz/index.html @@ -3265,8 +3265,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/4-1-Introduction-to-fundamental-data-types/index.html b/4-1-Introduction-to-fundamental-data-types/index.html index 5f39e3960..de3b53b76 100644 --- a/4-1-Introduction-to-fundamental-data-types/index.html +++ b/4-1-Introduction-to-fundamental-data-types/index.html @@ -3313,8 +3313,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/4-10-Introduction-to-if-statements/index.html b/4-10-Introduction-to-if-statements/index.html index e04f7846d..c07c5f231 100644 --- a/4-10-Introduction-to-if-statements/index.html +++ b/4-10-Introduction-to-if-statements/index.html @@ -3320,8 +3320,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/4-11-Chars/index.html b/4-11-Chars/index.html index 13ed9359d..601b0b944 100644 --- a/4-11-Chars/index.html +++ b/4-11-Chars/index.html @@ -3348,8 +3348,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/4-12-Introduction-to-type-conversion-and-static_cast/index.html b/4-12-Introduction-to-type-conversion-and-static_cast/index.html index ddf2de957..f48fd0343 100644 --- a/4-12-Introduction-to-type-conversion-and-static_cast/index.html +++ b/4-12-Introduction-to-type-conversion-and-static_cast/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/4-13-Const-variables-and-symbolic-constants/index.html b/4-13-Const-variables-and-symbolic-constants/index.html index 5625cff96..cae8a24c3 100644 --- a/4-13-Const-variables-and-symbolic-constants/index.html +++ b/4-13-Const-variables-and-symbolic-constants/index.html @@ -3355,8 +3355,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/4-14-Compile-time-constants-constant-expressions-and-constexpr/index.html b/4-14-Compile-time-constants-constant-expressions-and-constexpr/index.html index 979279d47..ec3e71108 100644 --- a/4-14-Compile-time-constants-constant-expressions-and-constexpr/index.html +++ b/4-14-Compile-time-constants-constant-expressions-and-constexpr/index.html @@ -3327,8 +3327,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/4-15-Literals/index.html b/4-15-Literals/index.html index b26e2e609..8f1bf8abe 100644 --- a/4-15-Literals/index.html +++ b/4-15-Literals/index.html @@ -3341,8 +3341,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/4-16-Numeral-systems-decimal-binary-hexadecimal-and-octal/index.html b/4-16-Numeral-systems-decimal-binary-hexadecimal-and-octal/index.html index dceb79801..3ca055a84 100644 --- a/4-16-Numeral-systems-decimal-binary-hexadecimal-and-octal/index.html +++ b/4-16-Numeral-systems-decimal-binary-hexadecimal-and-octal/index.html @@ -3313,8 +3313,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/4-17-an-introduction-to-std-string/index.html b/4-17-an-introduction-to-std-string/index.html index a24d77609..94b7ea1c5 100644 --- a/4-17-an-introduction-to-std-string/index.html +++ b/4-17-an-introduction-to-std-string/index.html @@ -3355,8 +3355,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/4-18-Introduction-to-std-string_view/index.html b/4-18-Introduction-to-std-string_view/index.html index 48e23f27a..d2cd9ce65 100644 --- a/4-18-Introduction-to-std-string_view/index.html +++ b/4-18-Introduction-to-std-string_view/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/4-2-Void/index.html b/4-2-Void/index.html index 62771839c..dcf1a26a7 100644 --- a/4-2-Void/index.html +++ b/4-2-Void/index.html @@ -3313,8 +3313,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/4-3-Object-sizes-and-the-sizeof-operator/index.html b/4-3-Object-sizes-and-the-sizeof-operator/index.html index e8b5b0c60..69282f472 100644 --- a/4-3-Object-sizes-and-the-sizeof-operator/index.html +++ b/4-3-Object-sizes-and-the-sizeof-operator/index.html @@ -3320,8 +3320,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/4-4-Signed-integers/index.html b/4-4-Signed-integers/index.html index a3ac19595..2aadeeae8 100644 --- a/4-4-Signed-integers/index.html +++ b/4-4-Signed-integers/index.html @@ -3327,8 +3327,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/4-5-Unsigned-integers-and-why-to-avoid-them/index.html b/4-5-Unsigned-integers-and-why-to-avoid-them/index.html index d71a1e19b..2c1746785 100644 --- a/4-5-Unsigned-integers-and-why-to-avoid-them/index.html +++ b/4-5-Unsigned-integers-and-why-to-avoid-them/index.html @@ -3341,8 +3341,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/4-6-Fixed-width-integers-and-size_t/index.html b/4-6-Fixed-width-integers-and-size_t/index.html index 4cac7a110..d84d9611a 100644 --- a/4-6-Fixed-width-integers-and-size_t/index.html +++ b/4-6-Fixed-width-integers-and-size_t/index.html @@ -3341,8 +3341,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/4-7-Introduction-to-scientific-notation/index.html b/4-7-Introduction-to-scientific-notation/index.html index 8f404d11b..6b83c5d68 100644 --- a/4-7-Introduction-to-scientific-notation/index.html +++ b/4-7-Introduction-to-scientific-notation/index.html @@ -3306,8 +3306,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/4-8-Floating-point-numbers/index.html b/4-8-Floating-point-numbers/index.html index cedd5e585..30d057289 100644 --- a/4-8-Floating-point-numbers/index.html +++ b/4-8-Floating-point-numbers/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/4-9-Boolean-values/index.html b/4-9-Boolean-values/index.html index 6e40b1c18..6014efdf0 100644 --- a/4-9-Boolean-values/index.html +++ b/4-9-Boolean-values/index.html @@ -3327,8 +3327,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/4-x-Chapter-4-summary-and-quiz/index.html b/4-x-Chapter-4-summary-and-quiz/index.html index 250985c70..f680b91e2 100644 --- a/4-x-Chapter-4-summary-and-quiz/index.html +++ b/4-x-Chapter-4-summary-and-quiz/index.html @@ -3299,8 +3299,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/404.html b/404.html index 7b083fc4a..f39609911 100644 --- a/404.html +++ b/404.html @@ -3251,8 +3251,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/5-1-Operator-precedence-and-associativity/index.html b/5-1-Operator-precedence-and-associativity/index.html index 4e937bbcc..61cf93f0a 100644 --- a/5-1-Operator-precedence-and-associativity/index.html +++ b/5-1-Operator-precedence-and-associativity/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/5-2-Arithmetic-operators/index.html b/5-2-Arithmetic-operators/index.html index 9d1ed85ac..ca5d9d526 100644 --- a/5-2-Arithmetic-operators/index.html +++ b/5-2-Arithmetic-operators/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/5-3-Modulus-and-Exponentiation/index.html b/5-3-Modulus-and-Exponentiation/index.html index 7bc97aa41..8aa83143b 100644 --- a/5-3-Modulus-and-Exponentiation/index.html +++ b/5-3-Modulus-and-Exponentiation/index.html @@ -3313,8 +3313,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/5-4-Increment-decrement-operators-and-side-effects/index.html b/5-4-Increment-decrement-operators-and-side-effects/index.html index 81a580004..75268f36b 100644 --- a/5-4-Increment-decrement-operators-and-side-effects/index.html +++ b/5-4-Increment-decrement-operators-and-side-effects/index.html @@ -3306,8 +3306,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/5-5-Comma-and-conditional-operators/index.html b/5-5-Comma-and-conditional-operators/index.html index 92758771d..160efe849 100644 --- a/5-5-Comma-and-conditional-operators/index.html +++ b/5-5-Comma-and-conditional-operators/index.html @@ -3341,8 +3341,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/5-6-Relational-operators-and-floating-point-comparisons/index.html b/5-6-Relational-operators-and-floating-point-comparisons/index.html index a16624186..0454b7a4e 100644 --- a/5-6-Relational-operators-and-floating-point-comparisons/index.html +++ b/5-6-Relational-operators-and-floating-point-comparisons/index.html @@ -3320,8 +3320,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/5-7-Logical-operators/index.html b/5-7-Logical-operators/index.html index 382006593..64138b75d 100644 --- a/5-7-Logical-operators/index.html +++ b/5-7-Logical-operators/index.html @@ -3348,8 +3348,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/5-x-Chapter-5-summary-and-quiz/index.html b/5-x-Chapter-5-summary-and-quiz/index.html index b04190ef7..04f8e11c3 100644 --- a/5-x-Chapter-5-summary-and-quiz/index.html +++ b/5-x-Chapter-5-summary-and-quiz/index.html @@ -3299,8 +3299,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/6-1-Compound-statements-blocks/index.html b/6-1-Compound-statements-blocks/index.html index 6cab530e7..1f23252c8 100644 --- a/6-1-Compound-statements-blocks/index.html +++ b/6-1-Compound-statements-blocks/index.html @@ -3313,8 +3313,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/6-10-Static-local-variables/index.html b/6-10-Static-local-variables/index.html index 3fa00cad9..1e10ca1cb 100644 --- a/6-10-Static-local-variables/index.html +++ b/6-10-Static-local-variables/index.html @@ -3313,8 +3313,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/6-11-Scope-duration-and-linkage-summary/index.html b/6-11-Scope-duration-and-linkage-summary/index.html index fd5d7045d..103aa75bc 100644 --- a/6-11-Scope-duration-and-linkage-summary/index.html +++ b/6-11-Scope-duration-and-linkage-summary/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/6-12-Using-declarations-and-using directives/index.html b/6-12-Using-declarations-and-using directives/index.html index ebef13c5f..f8889dd29 100644 --- a/6-12-Using-declarations-and-using directives/index.html +++ b/6-12-Using-declarations-and-using directives/index.html @@ -3348,8 +3348,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/6-13-Inline-functions/index.html b/6-13-Inline-functions/index.html index 866944b0d..ad48b85c6 100644 --- a/6-13-Inline-functions/index.html +++ b/6-13-Inline-functions/index.html @@ -3327,8 +3327,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/6-14-Constexpr-and-consteval-functions/index.html b/6-14-Constexpr-and-consteval-functions/index.html index 72a8ab77f..9b5541938 100644 --- a/6-14-Constexpr-and-consteval-functions/index.html +++ b/6-14-Constexpr-and-consteval-functions/index.html @@ -3348,8 +3348,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/6-15-Unnamed-and-inline-namespaces/index.html b/6-15-Unnamed-and-inline-namespaces/index.html index cb3818dec..4719ef36d 100644 --- a/6-15-Unnamed-and-inline-namespaces/index.html +++ b/6-15-Unnamed-and-inline-namespaces/index.html @@ -3306,8 +3306,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/6-2-User-defined-namespaces-and-the-scope-resolution-operator/index.html b/6-2-User-defined-namespaces-and-the-scope-resolution-operator/index.html index f5d6f1e1c..156a6ebf4 100644 --- a/6-2-User-defined-namespaces-and-the-scope-resolution-operator/index.html +++ b/6-2-User-defined-namespaces-and-the-scope-resolution-operator/index.html @@ -1765,6 +1765,13 @@ 什么时候应该使用命名空间 + + +
  • + + 如何使用命名空间 + +
  • @@ -3355,8 +3362,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • @@ -5903,6 +5910,13 @@ 什么时候应该使用命名空间 + + +
  • + + 如何使用命名空间 + +
  • @@ -6658,6 +6672,30 @@

    命名空间别名

    什么时候应该使用命名空间

    在实际应用中,命名空间可以被用来分割与当前应用程序强相关的代码和那些可能在日后被重用的代码(例如数学函数)。例如,物理和数学函数可以定义在一个命名空间中(例如 math::)。语言和本地化的函数则可以定义在另外的命名空间中(例如lang::) 。

    如果你编写的是库函数或用于提供给他人使用的代码,请始终把你的函数定义在一个自定义的命名空间中。如果你不遵循这项最佳实践,即你的代码没有声明在一个特定的命名空间中,那么很有可能会造成命名冲突。将你的代码放在自定义命名空间中的另外一个好处是,你的用户可以借助编辑器的自动补全和提示功能看到你库中的的内容。

    +

    如何使用命名空间

    +

    值得注意的是,C++ 中的命名空间最初并不是作为一种实现信息层次结构的方法而设计的,它们主要是作为一种防止命名冲突的机制而设计的。整个标准库都位于单一的顶级命名空间 std 下,这就是最好的证明。引入大量名称的新标准库功能已开始使用嵌套名称空间(如 std::ranges),以避免在 std 名称空间内发生命名冲突。

    +
      +
    • 为自己开发的小型应用程序通常不需要放在命名空间中。但是,对于包含大量第三方库的大型个人项目,对代码进行命名可以避免与未正确命名的库发生命名冲突。
    • +
    +
    +

    作者注

    +
    +

    这些教程中的示例通常不会使用命名空间,除非我们要说明命名空间的一些特殊情况,以保持示例的简洁性。

    +
      +
    • +

      任何将发布给他人的代码都必须使用命名空间,以防止与集成到其中的代码发生冲突。通常一个顶级命名空间就足够了(例如 Foologger)。将库代码置于命名空间内还有一个好处,即用户可以使用编辑器的自动完成和建议功能查看库的内容(例如,如果输入 Foologger,自动完成功能会显示 Foologger 中的所有名称)。

      +
    • +
    • +

      在多团队组织中,为了防止不同团队生成的代码之间出现命名冲突,通常会使用两级甚至三级命名空间。这些命名空间通常采用以下形式之一:

      +
    • +
    • +

      项目或库 ::模块(例如 Foologger::Lang

      +
    • +
    • 公司或组织::项目或库(例如 Foosoft::Foologger
    • +
    • 公司或组织::项目或库::模块(例如 Foosoft::Foologger::Lang
    • +
    +

    使用模块级命名空间有助于将日后可能重用的代码与不可重用的特定应用程序代码区分开来。例如,物理和数学函数可以放在一个命名空间中(如 Math::)。语言和本地化功能可以放在另一个命名空间(如 Lang::)。不过,目录结构也可用于此目的(特定于应用程序的代码放在项目目录树中,可重复使用的代码放在单独的共享目录树中)。

    +

    一般来说,应避免深度嵌套命名空间(超过 3 层)。

    diff --git a/6-3-Local-variables/index.html b/6-3-Local-variables/index.html index 148544f0e..9b42d3867 100644 --- a/6-3-Local-variables/index.html +++ b/6-3-Local-variables/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/6-4-Introduction-to-global-variables/index.html b/6-4-Introduction-to-global-variables/index.html index 3afa9e00a..bd4e95f47 100644 --- a/6-4-Introduction-to-global-variables/index.html +++ b/6-4-Introduction-to-global-variables/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/6-5-Variable-shadowing-name-hiding/index.html b/6-5-Variable-shadowing-name-hiding/index.html index 873303542..e8a932e85 100644 --- a/6-5-Variable-shadowing-name-hiding/index.html +++ b/6-5-Variable-shadowing-name-hiding/index.html @@ -3313,8 +3313,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/6-6-Internal-linkage/index.html b/6-6-Internal-linkage/index.html index 0c490673d..19dca6e54 100644 --- a/6-6-Internal-linkage/index.html +++ b/6-6-Internal-linkage/index.html @@ -3320,8 +3320,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/6-7-External-linkage-and-variable-forward-declarations/index.html b/6-7-External-linkage-and-variable-forward-declarations/index.html index a592e5d99..3a671b7f1 100644 --- a/6-7-External-linkage-and-variable-forward-declarations/index.html +++ b/6-7-External-linkage-and-variable-forward-declarations/index.html @@ -3327,8 +3327,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/6-8-Why-non-const-global-variables-are-evil/index.html b/6-8-Why-non-const-global-variables-are-evil/index.html index d5b405325..425d027d8 100644 --- a/6-8-Why-non-const-global-variables-are-evil/index.html +++ b/6-8-Why-non-const-global-variables-are-evil/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/6-9-Sharing-global-constants-across-multiple-files-using-inline-variables/index.html b/6-9-Sharing-global-constants-across-multiple-files-using-inline-variables/index.html index 7780d62ca..4b0bf6c77 100644 --- a/6-9-Sharing-global-constants-across-multiple-files-using-inline-variables/index.html +++ b/6-9-Sharing-global-constants-across-multiple-files-using-inline-variables/index.html @@ -3313,8 +3313,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/6-x-Chapter-6-summary-and-quiz/index.html b/6-x-Chapter-6-summary-and-quiz/index.html index 0cf89d5bf..f7e0f91ba 100644 --- a/6-x-Chapter-6-summary-and-quiz/index.html +++ b/6-x-Chapter-6-summary-and-quiz/index.html @@ -3299,8 +3299,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/7-1-Control-flow-introduction/index.html b/7-1-Control-flow-introduction/index.html index 5f665ad06..3a98756e6 100644 --- a/7-1-Control-flow-introduction/index.html +++ b/7-1-Control-flow-introduction/index.html @@ -3265,8 +3265,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/7-10-Break-and-continue/index.html b/7-10-Break-and-continue/index.html index a70c575fb..c185f0919 100644 --- a/7-10-Break-and-continue/index.html +++ b/7-10-Break-and-continue/index.html @@ -3341,8 +3341,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/7-11-Halts-exiting-your-program-early/index.html b/7-11-Halts-exiting-your-program-early/index.html index 384b7cd6c..f0d92d0c5 100644 --- a/7-11-Halts-exiting-your-program-early/index.html +++ b/7-11-Halts-exiting-your-program-early/index.html @@ -3327,8 +3327,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/7-12-Introduction-to-testing-your-code/index.html b/7-12-Introduction-to-testing-your-code/index.html index 0cee47e3d..8eabe9efc 100644 --- a/7-12-Introduction-to-testing-your-code/index.html +++ b/7-12-Introduction-to-testing-your-code/index.html @@ -3341,8 +3341,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/7-13-Code-coverage/index.html b/7-13-Code-coverage/index.html index c2984d53b..c4f331d6b 100644 --- a/7-13-Code-coverage/index.html +++ b/7-13-Code-coverage/index.html @@ -3327,8 +3327,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/7-14-Common-semantic-errors-in-c/index.html b/7-14-Common-semantic-errors-in-c/index.html index 62fdd9e7a..d69586575 100644 --- a/7-14-Common-semantic-errors-in-c/index.html +++ b/7-14-Common-semantic-errors-in-c/index.html @@ -3348,8 +3348,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/7-15-Detecting-and-handling-errors/index.html b/7-15-Detecting-and-handling-errors/index.html index 778f03123..5c84f30c9 100644 --- a/7-15-Detecting-and-handling-errors/index.html +++ b/7-15-Detecting-and-handling-errors/index.html @@ -3327,8 +3327,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/7-16-std-cin-and-handling-invalid-input/index.html b/7-16-std-cin-and-handling-invalid-input/index.html index 5a24a3aa6..137a6b867 100644 --- a/7-16-std-cin-and-handling-invalid-input/index.html +++ b/7-16-std-cin-and-handling-invalid-input/index.html @@ -3362,8 +3362,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/7-17-assert-and-static-assert/index.html b/7-17-assert-and-static-assert/index.html index a6246958e..08c81a1e3 100644 --- a/7-17-assert-and-static-assert/index.html +++ b/7-17-assert-and-static-assert/index.html @@ -3390,8 +3390,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/7-18-introduction-to-random-number-generation/index.html b/7-18-introduction-to-random-number-generation/index.html index b9dadfe7d..1876a99ba 100644 --- a/7-18-introduction-to-random-number-generation/index.html +++ b/7-18-introduction-to-random-number-generation/index.html @@ -3256,8 +3256,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/7-19-generating-random-numbers-using-mersenne-twister/index.html b/7-19-generating-random-numbers-using-mersenne-twister/index.html index fcb191df1..6d781b016 100644 --- a/7-19-generating-random-numbers-using-mersenne-twister/index.html +++ b/7-19-generating-random-numbers-using-mersenne-twister/index.html @@ -3256,8 +3256,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/7-2-If-statements-and-blocks/index.html b/7-2-If-statements-and-blocks/index.html index 7c339ad72..48e0af401 100644 --- a/7-2-If-statements-and-blocks/index.html +++ b/7-2-If-statements-and-blocks/index.html @@ -3320,8 +3320,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/7-3-Common-if-statement-problems/index.html b/7-3-Common-if-statement-problems/index.html index b38445fa4..f874c5ee4 100644 --- a/7-3-Common-if-statement-problems/index.html +++ b/7-3-Common-if-statement-problems/index.html @@ -3320,8 +3320,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/7-4-Switch-statement-basics/index.html b/7-4-Switch-statement-basics/index.html index 756c4ad04..66b014874 100644 --- a/7-4-Switch-statement-basics/index.html +++ b/7-4-Switch-statement-basics/index.html @@ -3327,8 +3327,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/7-5-Switch-fallthrough-and-scoping/index.html b/7-5-Switch-fallthrough-and-scoping/index.html index 245c367c8..df8d1242c 100644 --- a/7-5-Switch-fallthrough-and-scoping/index.html +++ b/7-5-Switch-fallthrough-and-scoping/index.html @@ -3327,8 +3327,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/7-6-Goto-statements/index.html b/7-6-Goto-statements/index.html index c2d6aa7f0..993ee8b63 100644 --- a/7-6-Goto-statements/index.html +++ b/7-6-Goto-statements/index.html @@ -3306,8 +3306,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/7-7-Intro-to-loops-and-while-statements/index.html b/7-7-Intro-to-loops-and-while-statements/index.html index 41ec3deef..68ab0f759 100644 --- a/7-7-Intro-to-loops-and-while-statements/index.html +++ b/7-7-Intro-to-loops-and-while-statements/index.html @@ -3355,8 +3355,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/7-8-Do-while-statements/index.html b/7-8-Do-while-statements/index.html index 82a9e9228..6cfc40aa2 100644 --- a/7-8-Do-while-statements/index.html +++ b/7-8-Do-while-statements/index.html @@ -3299,8 +3299,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/7-9-For-statements/index.html b/7-9-For-statements/index.html index 217e2aeb6..e6ea8e481 100644 --- a/7-9-For-statements/index.html +++ b/7-9-For-statements/index.html @@ -3341,8 +3341,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/7-x-Chapter-7-summary-and-quiz/index.html b/7-x-Chapter-7-summary-and-quiz/index.html index 9f1451062..4ccf7933c 100644 --- a/7-x-Chapter-7-summary-and-quiz/index.html +++ b/7-x-Chapter-7-summary-and-quiz/index.html @@ -3265,8 +3265,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/8-1-Implicit-type-conversion-coercion/index.html b/8-1-Implicit-type-conversion-coercion/index.html index e48c7a8aa..aa2e828ef 100644 --- a/8-1-Implicit-type-conversion-coercion/index.html +++ b/8-1-Implicit-type-conversion-coercion/index.html @@ -3320,8 +3320,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/8-10-Function-overload-differentiation/index.html b/8-10-Function-overload-differentiation/index.html index 8e67d91ae..6cf44c442 100644 --- a/8-10-Function-overload-differentiation/index.html +++ b/8-10-Function-overload-differentiation/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/8-11-Function-overload-resolution-and-ambiguous-matches/index.html b/8-11-Function-overload-resolution-and-ambiguous-matches/index.html index 46550b247..8935e1999 100644 --- a/8-11-Function-overload-resolution-and-ambiguous-matches/index.html +++ b/8-11-Function-overload-resolution-and-ambiguous-matches/index.html @@ -3327,8 +3327,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/8-12-Default-arguments/index.html b/8-12-Default-arguments/index.html index d4d85c0df..0ba4f216a 100644 --- a/8-12-Default-arguments/index.html +++ b/8-12-Default-arguments/index.html @@ -3327,8 +3327,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/8-13-Function-templates/index.html b/8-13-Function-templates/index.html index 637a3261a..1a1be1a22 100644 --- a/8-13-Function-templates/index.html +++ b/8-13-Function-templates/index.html @@ -3313,8 +3313,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/8-14-Function-template-instantiation/index.html b/8-14-Function-template-instantiation/index.html index 91d4bcb01..1e2a46054 100644 --- a/8-14-Function-template-instantiation/index.html +++ b/8-14-Function-template-instantiation/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/8-15-Function-templates-with-multiple-template-types/index.html b/8-15-Function-templates-with-multiple-template-types/index.html index e7dd8c03d..ae6029237 100644 --- a/8-15-Function-templates-with-multiple-template-types/index.html +++ b/8-15-Function-templates-with-multiple-template-types/index.html @@ -3320,8 +3320,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/8-2-Floating-point-and-integral-promotion/index.html b/8-2-Floating-point-and-integral-promotion/index.html index e302793f5..1b2ff1e14 100644 --- a/8-2-Floating-point-and-integral-promotion/index.html +++ b/8-2-Floating-point-and-integral-promotion/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/8-3-Numeric-conversions/index.html b/8-3-Numeric-conversions/index.html index 553bd2263..00e37053d 100644 --- a/8-3-Numeric-conversions/index.html +++ b/8-3-Numeric-conversions/index.html @@ -3320,8 +3320,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/8-4-Arithmetic-conversions/index.html b/8-4-Arithmetic-conversions/index.html index 6fcb19ae3..77d9522d3 100644 --- a/8-4-Arithmetic-conversions/index.html +++ b/8-4-Arithmetic-conversions/index.html @@ -3320,8 +3320,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/8-5-Explicit-type-conversion-casting-and-static-cast/index.html b/8-5-Explicit-type-conversion-casting-and-static-cast/index.html index fa737195a..bf5f8951e 100644 --- a/8-5-Explicit-type-conversion-casting-and-static-cast/index.html +++ b/8-5-Explicit-type-conversion-casting-and-static-cast/index.html @@ -3320,8 +3320,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/8-6-Typedefs-and-type-aliases/index.html b/8-6-Typedefs-and-type-aliases/index.html index da00e9f9d..19364a13a 100644 --- a/8-6-Typedefs-and-type-aliases/index.html +++ b/8-6-Typedefs-and-type-aliases/index.html @@ -3362,8 +3362,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/8-7-Type-deduction-for-objects-using-the auto-keyword/index.html b/8-7-Type-deduction-for-objects-using-the auto-keyword/index.html index f93800feb..b3cdd46b4 100644 --- a/8-7-Type-deduction-for-objects-using-the auto-keyword/index.html +++ b/8-7-Type-deduction-for-objects-using-the auto-keyword/index.html @@ -3320,8 +3320,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/8-8-Type-deduction-for-functions/index.html b/8-8-Type-deduction-for-functions/index.html index 0833d3c47..edd3f10b3 100644 --- a/8-8-Type-deduction-for-functions/index.html +++ b/8-8-Type-deduction-for-functions/index.html @@ -3306,8 +3306,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/8-9-Introduction-to-function-overloading/index.html b/8-9-Introduction-to-function-overloading/index.html index 2129dc2ef..0f913d89a 100644 --- a/8-9-Introduction-to-function-overloading/index.html +++ b/8-9-Introduction-to-function-overloading/index.html @@ -3320,8 +3320,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/8-x-Chapter-8-summary-and-quiz/index.html b/8-x-Chapter-8-summary-and-quiz/index.html index 2fb161d95..0b79daed7 100644 --- a/8-x-Chapter-8-summary-and-quiz/index.html +++ b/8-x-Chapter-8-summary-and-quiz/index.html @@ -3299,8 +3299,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/9-1-Introduction-to-compound-data-types/index.html b/9-1-Introduction-to-compound-data-types/index.html index 722d7051a..164f1221b 100644 --- a/9-1-Introduction-to-compound-data-types/index.html +++ b/9-1-Introduction-to-compound-data-types/index.html @@ -3312,8 +3312,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/9-10-Pass-by-address-part-2/index.html b/9-10-Pass-by-address-part-2/index.html index ce8037b53..a79c9aaa3 100644 --- a/9-10-Pass-by-address-part-2/index.html +++ b/9-10-Pass-by-address-part-2/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/9-11-Return-by-reference-and-return-by-address/index.html b/9-11-Return-by-reference-and-return-by-address/index.html index f093b9f27..f5ad4288f 100644 --- a/9-11-Return-by-reference-and-return-by-address/index.html +++ b/9-11-Return-by-reference-and-return-by-address/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/9-12-Type-deduction-with-pointers-references-and-const/index.html b/9-12-Type-deduction-with-pointers-references-and-const/index.html index 483e47cf5..37c6ac1c5 100644 --- a/9-12-Type-deduction-with-pointers-references-and-const/index.html +++ b/9-12-Type-deduction-with-pointers-references-and-const/index.html @@ -3334,8 +3334,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/9-2-Value-categories-lvalues-and-rvalues/index.html b/9-2-Value-categories-lvalues-and-rvalues/index.html index 52dd38099..eb6b7c528 100644 --- a/9-2-Value-categories-lvalues-and-rvalues/index.html +++ b/9-2-Value-categories-lvalues-and-rvalues/index.html @@ -3327,8 +3327,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/9-3-Lvalue-references/index.html b/9-3-Lvalue-references/index.html index ffa49c27c..316e217f9 100644 --- a/9-3-Lvalue-references/index.html +++ b/9-3-Lvalue-references/index.html @@ -3355,8 +3355,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/9-4-Lvalue-references-to-const/index.html b/9-4-Lvalue-references-to-const/index.html index a1dea75c8..dedaa1eb2 100644 --- a/9-4-Lvalue-references-to-const/index.html +++ b/9-4-Lvalue-references-to-const/index.html @@ -3320,8 +3320,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/9-5-Pass-by-lvalue-reference/index.html b/9-5-Pass-by-lvalue-reference/index.html index a0fbe8169..9ba8b4fc6 100644 --- a/9-5-Pass-by-lvalue-reference/index.html +++ b/9-5-Pass-by-lvalue-reference/index.html @@ -3348,8 +3348,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/9-6-Introduction-to-pointers/index.html b/9-6-Introduction-to-pointers/index.html index 053f92542..00a3bc976 100644 --- a/9-6-Introduction-to-pointers/index.html +++ b/9-6-Introduction-to-pointers/index.html @@ -3362,8 +3362,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/9-7-Null-pointers/index.html b/9-7-Null-pointers/index.html index bee713aed..d48305f30 100644 --- a/9-7-Null-pointers/index.html +++ b/9-7-Null-pointers/index.html @@ -3341,8 +3341,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/9-8-Pointers-and-const/index.html b/9-8-Pointers-and-const/index.html index cc98b2324..04f945e60 100644 --- a/9-8-Pointers-and-const/index.html +++ b/9-8-Pointers-and-const/index.html @@ -3320,8 +3320,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/9-9-Pass-by-address/index.html b/9-9-Pass-by-address/index.html index cf7a02ac0..b10d2f223 100644 --- a/9-9-Pass-by-address/index.html +++ b/9-9-Pass-by-address/index.html @@ -3327,8 +3327,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/9-x-Chapter-9-comprehensive-quiz/index.html b/9-x-Chapter-9-comprehensive-quiz/index.html index 74e25c083..aebb332ed 100644 --- a/9-x-Chapter-9-comprehensive-quiz/index.html +++ b/9-x-Chapter-9-comprehensive-quiz/index.html @@ -3299,8 +3299,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/A-1-static-and-dynamic-libraries/index.html b/A-1-static-and-dynamic-libraries/index.html index f36c6098f..4252e87ce 100644 --- a/A-1-static-and-dynamic-libraries/index.html +++ b/A-1-static-and-dynamic-libraries/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/A-2-using-libraries-with-visual-studio/index.html b/A-2-using-libraries-with-visual-studio/index.html index d3505e4f7..7143ebbb4 100644 --- a/A-2-using-libraries-with-visual-studio/index.html +++ b/A-2-using-libraries-with-visual-studio/index.html @@ -3255,8 +3255,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/A-3-using-libraries-with-Code-Blocks/index.html b/A-3-using-libraries-with-Code-Blocks/index.html index b7bcbb1c4..77ed33056 100644 --- a/A-3-using-libraries-with-Code-Blocks/index.html +++ b/A-3-using-libraries-with-Code-Blocks/index.html @@ -3255,8 +3255,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/A-4-C++FAQ/index.html b/A-4-C++FAQ/index.html index 173fc6cca..6dea2a965 100644 --- a/A-4-C++FAQ/index.html +++ b/A-4-C++FAQ/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/Associations/index.html b/Associations/index.html index 0715a398d..63b40600b 100644 --- a/Associations/index.html +++ b/Associations/index.html @@ -3251,8 +3251,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/Associative-containers/index.html b/Associative-containers/index.html index 0ebf1a460..4130e5694 100644 --- a/Associative-containers/index.html +++ b/Associative-containers/index.html @@ -3251,8 +3251,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/B-1-introduction-to-C++11/index.html b/B-1-introduction-to-C++11/index.html index 44e3770c8..006461078 100644 --- a/B-1-introduction-to-C++11/index.html +++ b/B-1-introduction-to-C++11/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/B-2-introduction-to-C++14/index.html b/B-2-introduction-to-C++14/index.html index 75a0f8100..64e823e60 100644 --- a/B-2-introduction-to-C++14/index.html +++ b/B-2-introduction-to-C++14/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/B-3-introduction-to-C++17/index.html b/B-3-introduction-to-C++17/index.html index 752212cf6..5591a4ce9 100644 --- a/B-3-introduction-to-C++17/index.html +++ b/B-3-introduction-to-C++17/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/B-4-introduction-to-C++20/index.html b/B-4-introduction-to-C++20/index.html index c2fdf60dd..7e2fcb023 100644 --- a/B-4-introduction-to-C++20/index.html +++ b/B-4-introduction-to-C++20/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/Binding/index.html b/Binding/index.html index dd278d64b..423c0913e 100644 --- a/Binding/index.html +++ b/Binding/index.html @@ -3251,8 +3251,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/C-style-casts/index.html b/C-style-casts/index.html index 609c28d46..e61e2a7e8 100644 --- a/C-style-casts/index.html +++ b/C-style-casts/index.html @@ -3251,8 +3251,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/Composition/index.html b/Composition/index.html index 80697f916..6b67d3df5 100644 --- a/Composition/index.html +++ b/Composition/index.html @@ -3251,8 +3251,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/Early-binding/index.html b/Early-binding/index.html index 18f6e91e8..09166160c 100644 --- a/Early-binding/index.html +++ b/Early-binding/index.html @@ -3251,8 +3251,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/Encapsulation/index.html b/Encapsulation/index.html index 139e44b5a..6726a366d 100644 --- a/Encapsulation/index.html +++ b/Encapsulation/index.html @@ -3251,8 +3251,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/Expression-parameters/index.html b/Expression-parameters/index.html index dce9905b8..662a51ee9 100644 --- a/Expression-parameters/index.html +++ b/Expression-parameters/index.html @@ -3251,8 +3251,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/Frankenobject/index.html b/Frankenobject/index.html index 20fff4d82..5bce87626 100644 --- a/Frankenobject/index.html +++ b/Frankenobject/index.html @@ -3251,8 +3251,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/Late-binding/index.html b/Late-binding/index.html index 66adc8e9b..aaafac434 100644 --- a/Late-binding/index.html +++ b/Late-binding/index.html @@ -3251,8 +3251,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/M-1-introduction-to-smart-pointers-and-move-semantics/index.html b/M-1-introduction-to-smart-pointers-and-move-semantics/index.html index 8dd6e2f5f..9406fc108 100644 --- a/M-1-introduction-to-smart-pointers-and-move-semantics/index.html +++ b/M-1-introduction-to-smart-pointers-and-move-semantics/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/M-2-R-value-references/index.html b/M-2-R-value-references/index.html index e90e95bf3..94638bece 100644 --- a/M-2-R-value-references/index.html +++ b/M-2-R-value-references/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/M-3-move-constructors-and-move-assignment/index.html b/M-3-move-constructors-and-move-assignment/index.html index 9f3c04315..9c93f891e 100644 --- a/M-3-move-constructors-and-move-assignment/index.html +++ b/M-3-move-constructors-and-move-assignment/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • @@ -4975,6 +4975,13 @@ 不要使用 std::swap 实现移动语义 + + +
  • + + 删除移动构造函数和移动赋值 + +
  • @@ -5903,6 +5910,13 @@ 不要使用 std::swap 实现移动语义 + + +
  • + + 删除移动构造函数和移动赋值 + +
  • @@ -7092,6 +7106,152 @@

    不要使用 std::swap 实现移动语义

    1
     2
    Move assign
     Joe
    +
    +

    删除移动构造函数和移动赋值

    +

    使用 = delete 语法删除移动构造函数和移动赋值的方法与删除复制构造函数和复制赋值的方法完全相同。

    +
     1
    + 2
    + 3
    + 4
    + 5
    + 6
    + 7
    + 8
    + 9
    +10
    +11
    +12
    +13
    +14
    +15
    +16
    +17
    +18
    +19
    +20
    +21
    +22
    +23
    +24
    +25
    +26
    +27
    +28
    +29
    +30
    +31
    #include <iostream>
    +#include <string>
    +#include <string_view>
    +
    +class Name
    +{
    +private:
    +    std::string m_name {};
    +
    +public:
    +    Name(std::string_view name) : m_name{ name }
    +    {
    +    }
    +
    +    Name(const Name& name) = delete;
    +    Name& operator=(const Name& name) = delete;
    +    Name(Name&& name) = delete;
    +    Name& operator=(Name&& name) = delete;
    +
    +    const std::string& get() const { return m_name; }
    +};
    +
    +int main()
    +{
    +    Name n1{ "Alex" };
    +    n1 = Name{ "Joe" }; // error: move assignment deleted
    +
    +    std::cout << n1.get() << '\n';
    +
    +    return 0;
    +}
    +
    +

    如果删除复制构造函数,编译器将不会生成隐式移动构造函数(使对象既不可复制也不可移动)。因此,在删除复制构造函数时,最好明确说明你希望从移动构造函数中得到什么行为。要么显式删除构造函数(明确这是我们想要的行为),要么默认删除构造函数(使类只可移动)。

    +
      +
    • Key insight
      +

      五则运算法则指出,如果定义或删除了复制构造函数、复制赋值函数、移动构造函数、移动赋值函数或析构函数,那么就应该定义或删除这些函数中的> 每一个。

      +
      +
    • +
    +

    如果你想要一个可复制但不可移动的对象,那么只删除 move 构造函数和 move 赋值似乎是个好主意,但这样做的不幸后果是,在强制复制消除不适用的情况下,该类不能按值返回。出现这种情况的原因是,已删除的移动构造函数仍被声明,因此符合重载解析的条件。而按值返回将使已删除的移动构造函数优先于未删除的复制构造函数。下面的程序可以说明这一点:

    +
     1
    + 2
    + 3
    + 4
    + 5
    + 6
    + 7
    + 8
    + 9
    +10
    +11
    +12
    +13
    +14
    +15
    +16
    +17
    +18
    +19
    +20
    +21
    +22
    +23
    +24
    +25
    +26
    +27
    +28
    +29
    +30
    +31
    +32
    +33
    +34
    +35
    +36
    +37
    #include <iostream>
    +#include <string>
    +#include <string_view>
    +
    +class Name
    +{
    +private:
    +    std::string m_name {};
    +
    +public:
    +    Name(std::string_view name) : m_name{ name }
    +    {
    +    }
    +
    +    Name(const Name& name) = default;
    +    Name& operator=(const Name& name) = default;
    +
    +    Name(Name&& name) = delete;
    +    Name& operator=(Name&& name) = delete;
    +
    +    const std::string& get() const { return m_name; }
    +};
    +
    +Name getJoe()
    +{
    +    Name joe{ "Joe" };
    +    return joe; // error: Move constructor was deleted
    +}
    +
    +int main()
    +{
    +    Name n{ getJoe() };
    +
    +    std::cout << n.get() << '\n';
    +
    +    return 0;
    +}
     
    diff --git a/M-4-std-move/index.html b/M-4-std-move/index.html index f3701613f..ac08ab66a 100644 --- a/M-4-std-move/index.html +++ b/M-4-std-move/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/M-5-std-move-if-noexcept/index.html b/M-5-std-move-if-noexcept/index.html index ae27b9558..00bb69f92 100644 --- a/M-5-std-move-if-noexcept/index.html +++ b/M-5-std-move-if-noexcept/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/M-6-std-unique-ptr/index.html b/M-6-std-unique-ptr/index.html index a937e1535..3d4ae6cc9 100644 --- a/M-6-std-unique-ptr/index.html +++ b/M-6-std-unique-ptr/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • @@ -6187,7 +6187,7 @@

    访问被智能指针管理的对象

    在上面的例子中,我们使用了重载的解引用运算符来获取 std::unique_ptr res 管理的 Resource 对象,然后将其送去 std::cout 打印。

    std::unique_ptr 和数组

    -

    std::auto_ptr 不一样的是,std::unique_ptr 足够只能,它懂得使用恰当的delete去删除内存(普通 delete 或数组 delete),所以,std::unique_ptr 既可以用于一般对象,也可以用于数组。

    +

    std::auto_ptr 不一样的是,std::unique_ptr 足够智能,它懂得使用恰当的delete去删除内存(普通 delete 或数组 delete),所以,std::unique_ptr 既可以用于一般对象,也可以用于数组。

    但是,相对于使用 std::unique_ptr 管理一个固定数组或C风格字符串,使用std::array 或者 std::vector (或者 std::string) 总是更好的选择。

    最佳实践

    @@ -6283,7 +6283,7 @@

    异常安全问题

    1
    some_function(std::unique_ptr<T>(new T), function_that_can_throw_exception());
     

    对于如何执行上述函数调用,编译器有很大的灵活性。它可以先创建新的类型T,然后调用function_that_can_throw_exception()吗,然后创建std::unique_ptr 去管理动态分配的T。如果 function_that_can_throw_exception() 抛出了依次,那么T在被分配内存后,没有被释放,因为用于管理它的智能指针还没有被创建。显然这会导致内存泄漏。

    -

    std::make_unique() 客服了整个问题,因为对象T和 std::unique_ptr 的创建都是在 std::make_unique()中完成的,执行顺序没有歧义。

    +

    std::make_unique()克服了整个问题,因为对象T和 std::unique_ptr 的创建都是在 std::make_unique()中完成的,执行顺序没有歧义。

    函数返回 std::unique_ptr

    std::unique_ptr 可以安全地从函数中按值返回

     1
    diff --git a/M-7-std-shared-ptr/index.html b/M-7-std-shared-ptr/index.html
    index 0f5665036..b4ded9d49 100644
    --- a/M-7-std-shared-ptr/index.html
    +++ b/M-7-std-shared-ptr/index.html
    @@ -3260,8 +3260,8 @@
       
       
         
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/M-8-circular-dependency-issues-with-std-shared-ptr-and-std-weak-ptr/index.html b/M-8-circular-dependency-issues-with-std-shared-ptr-and-std-weak-ptr/index.html index 384774335..087bb3624 100644 --- a/M-8-circular-dependency-issues-with-std-shared-ptr-and-std-weak-ptr/index.html +++ b/M-8-circular-dependency-issues-with-std-shared-ptr-and-std-weak-ptr/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/M-x-chapter-M-comprehensive-review/index.html b/M-x-chapter-M-comprehensive-review/index.html index 9df321445..8e13d2320 100644 --- a/M-x-chapter-M-comprehensive-review/index.html +++ b/M-x-chapter-M-comprehensive-review/index.html @@ -3260,8 +3260,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • diff --git a/MOC/index.html b/MOC/index.html index 687bb8bce..9ad8f6039 100644 --- a/MOC/index.html +++ b/MOC/index.html @@ -3470,8 +3470,8 @@
  • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
  • @@ -6354,7 +6354,7 @@

    11. 数组,字符串和动态内存分配

  • 11.17 - 动态数组 std::vector 简介
  • 11.18 - 迭代器简介
  • 11.19 - 标准库算法简介
  • -
  • 11.x - 小结与测试 - 数组、字符串和动态分配
  • +
  • 11.x - 小结与测试 - 数组、字符串和动态分配
  • 12. 函数

      diff --git a/Multiple inheritance/index.html b/Multiple inheritance/index.html index c5e639338..1625696c7 100644 --- a/Multiple inheritance/index.html +++ b/Multiple inheritance/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/Name-mangling/index.html b/Name-mangling/index.html index a9335cf7f..fc7258c2d 100644 --- a/Name-mangling/index.html +++ b/Name-mangling/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/O-1-bit-flags-and-bit-manipulation-via-std-bitset/index.html b/O-1-bit-flags-and-bit-manipulation-via-std-bitset/index.html index 8c6d97d10..649dbb35d 100644 --- a/O-1-bit-flags-and-bit-manipulation-via-std-bitset/index.html +++ b/O-1-bit-flags-and-bit-manipulation-via-std-bitset/index.html @@ -3260,8 +3260,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/O-2-bitwise-operators/index.html b/O-2-bitwise-operators/index.html index 117ac3bfc..cc48d6e11 100644 --- a/O-2-bitwise-operators/index.html +++ b/O-2-bitwise-operators/index.html @@ -3260,8 +3260,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/index.html b/O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/index.html index e2b87d2e9..05fd6a251 100644 --- a/O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/index.html +++ b/O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/index.html @@ -3260,8 +3260,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/O-4-converting-between-binary-and-decimal/index.html b/O-4-converting-between-binary-and-decimal/index.html index 584d4b556..494114c21 100644 --- a/O-4-converting-between-binary-and-decimal/index.html +++ b/O-4-converting-between-binary-and-decimal/index.html @@ -3260,8 +3260,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/Off-by-one/index.html b/Off-by-one/index.html index 3cef535fe..8ed086616 100644 --- a/Off-by-one/index.html +++ b/Off-by-one/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/Output-manipulators/index.html b/Output-manipulators/index.html index 6e6f2a113..5fa2d7e31 100644 --- a/Output-manipulators/index.html +++ b/Output-manipulators/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/RAII (Resource Acquisition Is Initialization)/index.html b/RAII (Resource Acquisition Is Initialization)/index.html index 7265222ac..7f29ab03e 100644 --- a/RAII (Resource Acquisition Is Initialization)/index.html +++ b/RAII (Resource Acquisition Is Initialization)/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/Run-time-type-information-RTTI/index.html b/Run-time-type-information-RTTI/index.html index 60ebebbc9..56467ee9e 100644 --- a/Run-time-type-information-RTTI/index.html +++ b/Run-time-type-information-RTTI/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/Sequence-containers/index.html b/Sequence-containers/index.html index 4272c4b5b..a11a4706e 100644 --- a/Sequence-containers/index.html +++ b/Sequence-containers/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/Template-specialization/index.html b/Template-specialization/index.html index 259d0150d..dad045f8d 100644 --- a/Template-specialization/index.html +++ b/Template-specialization/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/abbreviated function templates/index.html b/abbreviated function templates/index.html index b7d29626f..c8f5a8518 100644 --- a/abbreviated function templates/index.html +++ b/abbreviated function templates/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/abstract-class/index.html b/abstract-class/index.html index 24c45cb65..95df3245b 100644 --- a/abstract-class/index.html +++ b/abstract-class/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/abstraction/index.html b/abstraction/index.html index 52a44385f..9ecbdd7ca 100644 --- a/abstraction/index.html +++ b/abstraction/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/access-function/index.html b/access-function/index.html index ad6edc81b..ca0b119aa 100644 --- a/access-function/index.html +++ b/access-function/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/access-specifiers/index.html b/access-specifiers/index.html index 0a119f528..860e4588e 100644 --- a/access-specifiers/index.html +++ b/access-specifiers/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/address-of-operator/index.html b/address-of-operator/index.html index bb91f7330..7cea70332 100644 --- a/address-of-operator/index.html +++ b/address-of-operator/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/aggregate-data-type/index.html b/aggregate-data-type/index.html index 9bccc9b4f..24185f710 100644 --- a/aggregate-data-type/index.html +++ b/aggregate-data-type/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/aggregate-initialization/index.html b/aggregate-initialization/index.html index 1dd539f62..8ba284ec9 100644 --- a/aggregate-initialization/index.html +++ b/aggregate-initialization/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/aggregation/index.html b/aggregation/index.html index 305b0d225..1b1c42c80 100644 --- a/aggregation/index.html +++ b/aggregation/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/ambiguous-match/index.html b/ambiguous-match/index.html index bae040b04..6aa43cf06 100644 --- a/ambiguous-match/index.html +++ b/ambiguous-match/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/anonymous-object/index.html b/anonymous-object/index.html index a82830256..2926630e9 100644 --- a/anonymous-object/index.html +++ b/anonymous-object/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/arguments/index.html b/arguments/index.html index 1e3caba0d..3f59f40cc 100644 --- a/arguments/index.html +++ b/arguments/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/arity/index.html b/arity/index.html index 221ac3589..1677a4215 100644 --- a/arity/index.html +++ b/arity/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/automatic-memory-allocation/index.html b/automatic-memory-allocation/index.html index 756f125ad..533cad801 100644 --- a/automatic-memory-allocation/index.html +++ b/automatic-memory-allocation/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/automatic-storage-duration/index.html b/automatic-storage-duration/index.html index cbcab5bdb..ef43f4c3b 100644 --- a/automatic-storage-duration/index.html +++ b/automatic-storage-duration/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/base-class/index.html b/base-class/index.html index 5351d5013..c00c030a1 100644 --- a/base-class/index.html +++ b/base-class/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/bit/index.html b/bit/index.html index d607aaf08..28183e42c 100644 --- a/bit/index.html +++ b/bit/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/bitwise-and/index.html b/bitwise-and/index.html index b5060d16c..b2ac9508e 100644 --- a/bitwise-and/index.html +++ b/bitwise-and/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/bitwise-or/index.html b/bitwise-or/index.html index 46bd82c7f..b5e876f5b 100644 --- a/bitwise-or/index.html +++ b/bitwise-or/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/bss segment/index.html b/bss segment/index.html index 62dbc3958..ce8fec360 100644 --- a/bss segment/index.html +++ b/bss segment/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/byte/index.html b/byte/index.html index b3e785130..ee204c03b 100644 --- a/byte/index.html +++ b/byte/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/call-stack/index.html b/call-stack/index.html index ee042cbd8..20e4751fe 100644 --- a/call-stack/index.html +++ b/call-stack/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/callable-object/index.html b/callable-object/index.html index 1f1caaebf..df95b8839 100644 --- a/callable-object/index.html +++ b/callable-object/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/callback functions/index.html b/callback functions/index.html index 669dbdc38..60a93c7ce 100644 --- a/callback functions/index.html +++ b/callback functions/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/child-class/index.html b/child-class/index.html index c332786d0..c0f2761ac 100644 --- a/child-class/index.html +++ b/child-class/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/class-instance/index.html b/class-instance/index.html index fc322b9be..acd21071e 100644 --- a/class-instance/index.html +++ b/class-instance/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/class-template-argument-deduction/index.html b/class-template-argument-deduction/index.html index 2e288fe0c..bb500ed76 100644 --- a/class-template-argument-deduction/index.html +++ b/class-template-argument-deduction/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/class-template/index.html b/class-template/index.html index 32a376cde..8788a64c5 100644 --- a/class-template/index.html +++ b/class-template/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/class-type/index.html b/class-type/index.html index e995a85ce..cc53848ab 100644 --- a/class-type/index.html +++ b/class-type/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/closure/index.html b/closure/index.html index ab135ad54..d22b96645 100644 --- a/closure/index.html +++ b/closure/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/code segment/index.html b/code segment/index.html index de4511f74..d2ff976a4 100644 --- a/code segment/index.html +++ b/code segment/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/compile-time/index.html b/compile-time/index.html index 0bba7ed3e..bc23a4e52 100644 --- a/compile-time/index.html +++ b/compile-time/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/compiler/index.html b/compiler/index.html index 2a1705979..2ab372062 100644 --- a/compiler/index.html +++ b/compiler/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/composite-types/index.html b/composite-types/index.html index 9f62a29c3..116bbb373 100644 --- a/composite-types/index.html +++ b/composite-types/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/const-cast/index.html b/const-cast/index.html index e8f12ef52..f21d4d60f 100644 --- a/const-cast/index.html +++ b/const-cast/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/const-member-function/index.html b/const-member-function/index.html index 63b26f18a..e9f639e93 100644 --- a/const-member-function/index.html +++ b/const-member-function/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/const-pointer/index.html b/const-pointer/index.html index 47631ca9a..b18b32481 100644 --- a/const-pointer/index.html +++ b/const-pointer/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/constant expression/index.html b/constant expression/index.html index bb0334261..f98e00588 100644 --- a/constant expression/index.html +++ b/constant expression/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/constructor/index.html b/constructor/index.html index 7de4ee324..2607e05bb 100644 --- a/constructor/index.html +++ b/constructor/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/container-adapter/index.html b/container-adapter/index.html index ee5ba41e0..fba96b1ea 100644 --- a/container-adapter/index.html +++ b/container-adapter/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/container-class/index.html b/container-class/index.html index b660dbbfa..d776706a0 100644 --- a/container-class/index.html +++ b/container-class/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/converting-constructors/index.html b/converting-constructors/index.html index 6e2f7666b..50e62716a 100644 --- a/converting-constructors/index.html +++ b/converting-constructors/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/copy-assignment-operator/index.html b/copy-assignment-operator/index.html index a39274248..cf44d913d 100644 --- a/copy-assignment-operator/index.html +++ b/copy-assignment-operator/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/copy-constructor/index.html b/copy-constructor/index.html index 5f83deb54..d8294affa 100644 --- a/copy-constructor/index.html +++ b/copy-constructor/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/copy-constructors/index.html b/copy-constructors/index.html index 0bb8cf98c..58f853691 100644 --- a/copy-constructors/index.html +++ b/copy-constructors/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/copy-initialization/index.html b/copy-initialization/index.html index 3c32bb1ad..6883ae4b8 100644 --- a/copy-initialization/index.html +++ b/copy-initialization/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/copy-semantics/index.html b/copy-semantics/index.html index da01d7776..a9097a3a2 100644 --- a/copy-semantics/index.html +++ b/copy-semantics/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git "a/copy-swap\344\271\240\350\257\255/index.html" "b/copy-swap\344\271\240\350\257\255/index.html" index 444c70908..7ae110391 100644 --- "a/copy-swap\344\271\240\350\257\255/index.html" +++ "b/copy-swap\344\271\240\350\257\255/index.html" @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/covariant/index.html b/covariant/index.html index c7f318e01..75d405700 100644 --- a/covariant/index.html +++ b/covariant/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/dangling/index.html b/dangling/index.html index 0e81f5b9d..f25fe86e7 100644 --- a/dangling/index.html +++ b/dangling/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/data segment/index.html b/data segment/index.html index 580ec7555..aeb9570e0 100644 --- a/data segment/index.html +++ b/data segment/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/declaration/index.html b/declaration/index.html index 77d868a4b..d187b6400 100644 --- a/declaration/index.html +++ b/declaration/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/deep-copy/index.html b/deep-copy/index.html index a51441dc8..a92eeb753 100644 --- a/deep-copy/index.html +++ b/deep-copy/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/default-constructor/index.html b/default-constructor/index.html index 7e02f14ef..9d141a2b8 100644 --- a/default-constructor/index.html +++ b/default-constructor/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/default-initialization/index.html b/default-initialization/index.html index cb7dc1860..1fc46b8e4 100644 --- a/default-initialization/index.html +++ b/default-initialization/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/default-member-initializer/index.html b/default-member-initializer/index.html index 30bcaa506..3623e7d22 100644 --- a/default-member-initializer/index.html +++ b/default-member-initializer/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/definition/index.html b/definition/index.html index cfce08c8e..2d8d79b4b 100644 --- a/definition/index.html +++ b/definition/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/delegating-constructors/index.html b/delegating-constructors/index.html index 392d8f6f6..6eba7db8e 100644 --- a/delegating-constructors/index.html +++ b/delegating-constructors/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/dependency/index.html b/dependency/index.html index 3b0b72b6b..ecedb3ed5 100644 --- a/dependency/index.html +++ b/dependency/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/dereference-operator/index.html b/dereference-operator/index.html index 849a1f06b..0f69f5f0e 100644 --- a/dereference-operator/index.html +++ b/dereference-operator/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/derived-class/index.html b/derived-class/index.html index 30fa902f4..e947250da 100644 --- a/derived-class/index.html +++ b/derived-class/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/designated-initializers/index.html b/designated-initializers/index.html index 9c5b97813..8239f481a 100644 --- a/designated-initializers/index.html +++ b/designated-initializers/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/destructor/index.html b/destructor/index.html index e626f5e1b..527106830 100644 --- a/destructor/index.html +++ b/destructor/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/direct-initialization/index.html b/direct-initialization/index.html index 62ab111ae..5e59825d9 100644 --- a/direct-initialization/index.html +++ b/direct-initialization/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/downcasting/index.html b/downcasting/index.html index 5d91fa8b6..a7c1b9597 100644 --- a/downcasting/index.html +++ b/downcasting/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/dumb-pointer/index.html b/dumb-pointer/index.html index 0693a26c4..fbed3a042 100644 --- a/dumb-pointer/index.html +++ b/dumb-pointer/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/dynamic-array/index.html b/dynamic-array/index.html index e06cd2b0d..cab3d3116 100644 --- a/dynamic-array/index.html +++ b/dynamic-array/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/dynamic-casts/index.html b/dynamic-casts/index.html index af33dfb4c..7443de756 100644 --- a/dynamic-casts/index.html +++ b/dynamic-casts/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/dynamic-duration/index.html b/dynamic-duration/index.html index 10c75dc9c..39d7b8d66 100644 --- a/dynamic-duration/index.html +++ b/dynamic-duration/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/dynamic-initialization/index.html b/dynamic-initialization/index.html index 050d6a05d..282b84db4 100644 --- a/dynamic-initialization/index.html +++ b/dynamic-initialization/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/dynamic-memory-allocation/index.html b/dynamic-memory-allocation/index.html index 84292bc5e..77c6fedda 100644 --- a/dynamic-memory-allocation/index.html +++ b/dynamic-memory-allocation/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/enum-class/index.html b/enum-class/index.html index e291c41f6..ca4c215c0 100644 --- a/enum-class/index.html +++ b/enum-class/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/evaluation/index.html b/evaluation/index.html index dd2e71c39..86d9c4415 100644 --- a/evaluation/index.html +++ b/evaluation/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/exception-safety-issue/index.html b/exception-safety-issue/index.html index 29a424d04..7b4bc4f10 100644 --- a/exception-safety-issue/index.html +++ b/exception-safety-issue/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/explicit-type-conversion/index.html b/explicit-type-conversion/index.html index cc79845ec..eaf6fb042 100644 --- a/explicit-type-conversion/index.html +++ b/explicit-type-conversion/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/expression-type/index.html b/expression-type/index.html index a7ea73704..b482c1ee1 100644 --- a/expression-type/index.html +++ b/expression-type/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/external-linkage/index.html b/external-linkage/index.html index 342e6bdd0..c249c3cbb 100644 --- a/external-linkage/index.html +++ b/external-linkage/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/external-variable/index.html b/external-variable/index.html index e300c1f52..d5bf5fea8 100644 --- a/external-variable/index.html +++ b/external-variable/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/extraction-operator/index.html b/extraction-operator/index.html index e2d05c42b..4b05ee2b5 100644 --- a/extraction-operator/index.html +++ b/extraction-operator/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/fallthrough/index.html b/fallthrough/index.html index f00b2e36b..773014cbe 100644 --- a/fallthrough/index.html +++ b/fallthrough/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/file-scope/index.html b/file-scope/index.html index e5a69a5fd..707261c84 100644 --- a/file-scope/index.html +++ b/file-scope/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/fixed-array/index.html b/fixed-array/index.html index e43849848..cda0fa5ca 100644 --- a/fixed-array/index.html +++ b/fixed-array/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/format-group/index.html b/format-group/index.html index 4dceef481..eaa97d1f1 100644 --- a/format-group/index.html +++ b/format-group/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/forward-declaration/index.html b/forward-declaration/index.html index d2077693e..96897d5e6 100644 --- a/forward-declaration/index.html +++ b/forward-declaration/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/friend-class/index.html b/friend-class/index.html index b20dfa59d..9864ecab1 100644 --- a/friend-class/index.html +++ b/friend-class/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/friend-function/index.html b/friend-function/index.html index 0d605185b..942ddec4e 100644 --- a/friend-function/index.html +++ b/friend-function/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/function-instance/index.html b/function-instance/index.html index f635e928d..34939b845 100644 --- a/function-instance/index.html +++ b/function-instance/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/function-literal/index.html b/function-literal/index.html index b59b492b2..e9fcc58a1 100644 --- a/function-literal/index.html +++ b/function-literal/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/function-pointer/index.html b/function-pointer/index.html index 0320f628d..a959dec1d 100644 --- a/function-pointer/index.html +++ b/function-pointer/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/function-prototype/index.html b/function-prototype/index.html index 2a3f99ef4..aba483bb5 100644 --- a/function-prototype/index.html +++ b/function-prototype/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/function-template-instantiation/index.html b/function-template-instantiation/index.html index 230a71c87..fafc913a0 100644 --- a/function-template-instantiation/index.html +++ b/function-template-instantiation/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/function-template/index.html b/function-template/index.html index 7fd15a691..267aff3e3 100644 --- a/function-template/index.html +++ b/function-template/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/generic-algorithm/index.html b/generic-algorithm/index.html index d4af37135..cc56cb5ee 100644 --- a/generic-algorithm/index.html +++ b/generic-algorithm/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/global-variable/index.html b/global-variable/index.html index cc6c89026..241e59ff4 100644 --- a/global-variable/index.html +++ b/global-variable/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/going-out-of-scope/index.html b/going-out-of-scope/index.html index 077b1c1fa..6bfe460a6 100644 --- a/going-out-of-scope/index.html +++ b/going-out-of-scope/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/header-guard/index.html b/header-guard/index.html index 6711feff9..7e2933ee9 100644 --- a/header-guard/index.html +++ b/header-guard/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/heap/index.html b/heap/index.html index cab461215..cc76d1ef8 100644 --- a/heap/index.html +++ b/heap/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/identifier/index.html b/identifier/index.html index 8f3a1ab12..af39bf2cb 100644 --- a/identifier/index.html +++ b/identifier/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/identity/index.html b/identity/index.html index 81559b16c..58c8df8d0 100644 --- a/identity/index.html +++ b/identity/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/images/onepage/Drawing 2023-02-11 13.33.50.excalidraw/index.html b/images/onepage/Drawing 2023-02-11 13.33.50.excalidraw/index.html index 360ffe385..05abc1436 100644 --- a/images/onepage/Drawing 2023-02-11 13.33.50.excalidraw/index.html +++ b/images/onepage/Drawing 2023-02-11 13.33.50.excalidraw/index.html @@ -3256,8 +3256,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/images/onepage/Scripts/Downloaded/Add Link and Open Page/index.html b/images/onepage/Scripts/Downloaded/Add Link and Open Page/index.html index ac16efea8..68b070994 100644 --- a/images/onepage/Scripts/Downloaded/Add Link and Open Page/index.html +++ b/images/onepage/Scripts/Downloaded/Add Link and Open Page/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/images/onepage/Scripts/Downloaded/Box Selected Elements/index.html b/images/onepage/Scripts/Downloaded/Box Selected Elements/index.html index 96860aac5..759cbb1d1 100644 --- a/images/onepage/Scripts/Downloaded/Box Selected Elements/index.html +++ b/images/onepage/Scripts/Downloaded/Box Selected Elements/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/images/onepage/Scripts/Downloaded/Bullet Point/index.html b/images/onepage/Scripts/Downloaded/Bullet Point/index.html index 5243c2b94..fe865e8d0 100644 --- a/images/onepage/Scripts/Downloaded/Bullet Point/index.html +++ b/images/onepage/Scripts/Downloaded/Bullet Point/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/images/onepage/Scripts/Downloaded/Convert freedraw to line/index.html b/images/onepage/Scripts/Downloaded/Convert freedraw to line/index.html index 9e0519790..fc5da02a3 100644 --- a/images/onepage/Scripts/Downloaded/Convert freedraw to line/index.html +++ b/images/onepage/Scripts/Downloaded/Convert freedraw to line/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/images/onepage/Scripts/Downloaded/Convert text to link with folder and alias/index.html b/images/onepage/Scripts/Downloaded/Convert text to link with folder and alias/index.html index 9737101ce..85d5a7d99 100644 --- a/images/onepage/Scripts/Downloaded/Convert text to link with folder and alias/index.html +++ b/images/onepage/Scripts/Downloaded/Convert text to link with folder and alias/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/images/onepage/Scripts/Downloaded/Expand rectangles horizontally keep text centered/index.html b/images/onepage/Scripts/Downloaded/Expand rectangles horizontally keep text centered/index.html index 9b46cd78a..0612affb4 100644 --- a/images/onepage/Scripts/Downloaded/Expand rectangles horizontally keep text centered/index.html +++ b/images/onepage/Scripts/Downloaded/Expand rectangles horizontally keep text centered/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/images/onepage/Scripts/Downloaded/Expand rectangles horizontally/index.html b/images/onepage/Scripts/Downloaded/Expand rectangles horizontally/index.html index b1909c14a..4f3c1fdcf 100644 --- a/images/onepage/Scripts/Downloaded/Expand rectangles horizontally/index.html +++ b/images/onepage/Scripts/Downloaded/Expand rectangles horizontally/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/images/onepage/Scripts/Downloaded/Expand rectangles vertically keep text centered/index.html b/images/onepage/Scripts/Downloaded/Expand rectangles vertically keep text centered/index.html index 39cdfd8ed..bfbfa0bf5 100644 --- a/images/onepage/Scripts/Downloaded/Expand rectangles vertically keep text centered/index.html +++ b/images/onepage/Scripts/Downloaded/Expand rectangles vertically keep text centered/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/images/onepage/Scripts/Downloaded/Expand rectangles vertically/index.html b/images/onepage/Scripts/Downloaded/Expand rectangles vertically/index.html index a007b60d9..f8e10f258 100644 --- a/images/onepage/Scripts/Downloaded/Expand rectangles vertically/index.html +++ b/images/onepage/Scripts/Downloaded/Expand rectangles vertically/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/images/onepage/Scripts/Downloaded/Fixed spacing/index.html b/images/onepage/Scripts/Downloaded/Fixed spacing/index.html index a20ce76c6..25b79f222 100644 --- a/images/onepage/Scripts/Downloaded/Fixed spacing/index.html +++ b/images/onepage/Scripts/Downloaded/Fixed spacing/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/images/onepage/Scripts/Downloaded/Fixed vertical distance/index.html b/images/onepage/Scripts/Downloaded/Fixed vertical distance/index.html index e8ed9bd2f..370003f64 100644 --- a/images/onepage/Scripts/Downloaded/Fixed vertical distance/index.html +++ b/images/onepage/Scripts/Downloaded/Fixed vertical distance/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/images/onepage/Scripts/Downloaded/Modify stroke width of selected elements/index.html b/images/onepage/Scripts/Downloaded/Modify stroke width of selected elements/index.html index 728052f0f..1b388f539 100644 --- a/images/onepage/Scripts/Downloaded/Modify stroke width of selected elements/index.html +++ b/images/onepage/Scripts/Downloaded/Modify stroke width of selected elements/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/images/onepage/Scripts/Downloaded/Normalize Selected Arrows/index.html b/images/onepage/Scripts/Downloaded/Normalize Selected Arrows/index.html index cda17391c..24fafdb1b 100644 --- a/images/onepage/Scripts/Downloaded/Normalize Selected Arrows/index.html +++ b/images/onepage/Scripts/Downloaded/Normalize Selected Arrows/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/images/onepage/Scripts/Downloaded/Organic Line/index.html b/images/onepage/Scripts/Downloaded/Organic Line/index.html index 8712346f9..aba2ac690 100644 --- a/images/onepage/Scripts/Downloaded/Organic Line/index.html +++ b/images/onepage/Scripts/Downloaded/Organic Line/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/images/onepage/Scripts/Downloaded/Repeat Elements/index.html b/images/onepage/Scripts/Downloaded/Repeat Elements/index.html index 8a3a9dbb4..39654e9bd 100644 --- a/images/onepage/Scripts/Downloaded/Repeat Elements/index.html +++ b/images/onepage/Scripts/Downloaded/Repeat Elements/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/images/onepage/Scripts/Downloaded/Set Dimensions/index.html b/images/onepage/Scripts/Downloaded/Set Dimensions/index.html index be76b9f08..ba6b0fae8 100644 --- a/images/onepage/Scripts/Downloaded/Set Dimensions/index.html +++ b/images/onepage/Scripts/Downloaded/Set Dimensions/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/images/onepage/Scripts/Downloaded/Set Text Alignment/index.html b/images/onepage/Scripts/Downloaded/Set Text Alignment/index.html index bdc8c515a..c04327a49 100644 --- a/images/onepage/Scripts/Downloaded/Set Text Alignment/index.html +++ b/images/onepage/Scripts/Downloaded/Set Text Alignment/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/images/onepage/Scripts/Downloaded/Split text by lines/index.html b/images/onepage/Scripts/Downloaded/Split text by lines/index.html index b4d4cc705..57c87ea78 100644 --- a/images/onepage/Scripts/Downloaded/Split text by lines/index.html +++ b/images/onepage/Scripts/Downloaded/Split text by lines/index.html @@ -3256,8 +3256,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/images/onepage/Scripts/Downloaded/TheBrain-navigation/index.html b/images/onepage/Scripts/Downloaded/TheBrain-navigation/index.html index e36b7e290..21f548d3e 100644 --- a/images/onepage/Scripts/Downloaded/TheBrain-navigation/index.html +++ b/images/onepage/Scripts/Downloaded/TheBrain-navigation/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/images/onepage/Template.excalidraw/index.html b/images/onepage/Template.excalidraw/index.html index 5aeef4284..e587d6e67 100644 --- a/images/onepage/Template.excalidraw/index.html +++ b/images/onepage/Template.excalidraw/index.html @@ -3256,8 +3256,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/images/onepage/c++-basic-onepage/index.html b/images/onepage/c++-basic-onepage/index.html index b8211393a..1406d1769 100644 --- a/images/onepage/c++-basic-onepage/index.html +++ b/images/onepage/c++-basic-onepage/index.html @@ -3256,8 +3256,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/images/onepage/moc-onepage/index.html b/images/onepage/moc-onepage/index.html index 6e4109ca6..ea871c91f 100644 --- a/images/onepage/moc-onepage/index.html +++ b/images/onepage/moc-onepage/index.html @@ -3256,8 +3256,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/immediate-functions/index.html b/immediate-functions/index.html index 8b28b22d7..7b0204d48 100644 --- a/immediate-functions/index.html +++ b/immediate-functions/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/implicit-constructor/index.html b/implicit-constructor/index.html index 508aa295b..558817240 100644 --- a/implicit-constructor/index.html +++ b/implicit-constructor/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/implicit-object/index.html b/implicit-object/index.html index 3af920e85..8c003954b 100644 --- a/implicit-object/index.html +++ b/implicit-object/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/implicit-type-conversion/index.html b/implicit-type-conversion/index.html index 051397aad..8dc20cd98 100644 --- a/implicit-type-conversion/index.html +++ b/implicit-type-conversion/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/index.html b/index.html index 30eef7428..14ab17c1b 100644 --- a/index.html +++ b/index.html @@ -3258,8 +3258,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/inheritance/index.html b/inheritance/index.html index bdcbb10c1..fb766f9dd 100644 --- a/inheritance/index.html +++ b/inheritance/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/initialization/index.html b/initialization/index.html index 1fb782a83..ce8be95e7 100644 --- a/initialization/index.html +++ b/initialization/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/initializer-list/index.html b/initializer-list/index.html index 895a4f4ba..6ef7800ea 100644 --- a/initializer-list/index.html +++ b/initializer-list/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/initializer/index.html b/initializer/index.html index 05c874fe0..7ec394e75 100644 --- a/initializer/index.html +++ b/initializer/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/inline-function/index.html b/inline-function/index.html index a8a5dc2df..985fd305b 100644 --- a/inline-function/index.html +++ b/inline-function/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/inline-namespace/index.html b/inline-namespace/index.html index caf2dd3d0..ed47c4177 100644 --- a/inline-namespace/index.html +++ b/inline-namespace/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/inline-variables/index.html b/inline-variables/index.html index 7e0dfa01d..5748d5d8f 100644 --- a/inline-variables/index.html +++ b/inline-variables/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/input-manipulators/index.html b/input-manipulators/index.html index 0160cd669..35b5f5353 100644 --- a/input-manipulators/index.html +++ b/input-manipulators/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/insertion-operator/index.html b/insertion-operator/index.html index b9d645028..6eb480248 100644 --- a/insertion-operator/index.html +++ b/insertion-operator/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/instance/index.html b/instance/index.html index 8e2b7d7ba..2d4e8a31b 100644 --- a/instance/index.html +++ b/instance/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/instantiated/index.html b/instantiated/index.html index 68ef0b64d..5e7b87905 100644 --- a/instantiated/index.html +++ b/instantiated/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/instuction/index.html b/instuction/index.html index ef2ffb0d8..9aa57d64d 100644 --- a/instuction/index.html +++ b/instuction/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/integral-type/index.html b/integral-type/index.html index 3b763150f..a6d79f350 100644 --- a/integral-type/index.html +++ b/integral-type/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/interface-class/index.html b/interface-class/index.html index a9e017576..8fa75b0cb 100644 --- a/interface-class/index.html +++ b/interface-class/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/internal-linkage/index.html b/internal-linkage/index.html index b9b1d3c2d..5e6fde666 100644 --- a/internal-linkage/index.html +++ b/internal-linkage/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/internal-variable/index.html b/internal-variable/index.html index 3192693c8..606b2eda4 100644 --- a/internal-variable/index.html +++ b/internal-variable/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/iterating/index.html b/iterating/index.html index ad4dc9fc1..4b4f668fb 100644 --- a/iterating/index.html +++ b/iterating/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/iterator/index.html b/iterator/index.html index ff8513dc2..f2b51c07c 100644 --- a/iterator/index.html +++ b/iterator/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/lambda/index.html b/lambda/index.html index f1a35505f..b8a3e54e2 100644 --- a/lambda/index.html +++ b/lambda/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/link/index.html b/link/index.html index ea2f04a1c..9595eca67 100644 --- a/link/index.html +++ b/link/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/linkage/index.html b/linkage/index.html index 0b4d43c75..9199a10ed 100644 --- a/linkage/index.html +++ b/linkage/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/linker/index.html b/linker/index.html index 9a8e534f0..a98399cb1 100644 --- a/linker/index.html +++ b/linker/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/list assignment operator/index.html b/list assignment operator/index.html index b23b3c227..220788014 100644 --- a/list assignment operator/index.html +++ b/list assignment operator/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/list-constructor/index.html b/list-constructor/index.html index 08f749d88..e920280c5 100644 --- a/list-constructor/index.html +++ b/list-constructor/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/list-initialization/index.html b/list-initialization/index.html index b3962c1b6..b6d9536fb 100644 --- a/list-initialization/index.html +++ b/list-initialization/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/literals/index.html b/literals/index.html index 6b2b98d2d..8158e6787 100644 --- a/literals/index.html +++ b/literals/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/local-variable/index.html b/local-variable/index.html index d49beb092..9d2b3f3b7 100644 --- a/local-variable/index.html +++ b/local-variable/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/lvalue-reference/index.html b/lvalue-reference/index.html index 6f3de4678..129eef5c5 100644 --- a/lvalue-reference/index.html +++ b/lvalue-reference/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/lvalue/index.html b/lvalue/index.html index cb973030d..1f692d940 100644 --- a/lvalue/index.html +++ b/lvalue/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/macro/index.html b/macro/index.html index ed7a7f725..4eef1fdb9 100644 --- a/macro/index.html +++ b/macro/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/member-access-operator/index.html b/member-access-operator/index.html index a5686a3c1..3a1cb7160 100644 --- a/member-access-operator/index.html +++ b/member-access-operator/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/member-function/index.html b/member-function/index.html index 483d27e09..ed9c19e54 100644 --- a/member-function/index.html +++ b/member-function/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/member-initializer-list/index.html b/member-initializer-list/index.html index 17a664d36..1c3f6ebd6 100644 --- a/member-initializer-list/index.html +++ b/member-initializer-list/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/member-selection-operator/index.html b/member-selection-operator/index.html index 12b1561c9..c510ae6b1 100644 --- a/member-selection-operator/index.html +++ b/member-selection-operator/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/member-types/index.html b/member-types/index.html index 94f0f13cf..ecc810720 100644 --- a/member-types/index.html +++ b/member-types/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/member-variable/index.html b/member-variable/index.html index 88ae1b8cb..2a4e57ccc 100644 --- a/member-variable/index.html +++ b/member-variable/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/memberwise initialization/index.html b/memberwise initialization/index.html index a11b0b7b3..cce99219d 100644 --- a/memberwise initialization/index.html +++ b/memberwise initialization/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/memberwise-assignment/index.html b/memberwise-assignment/index.html index f939a7c4b..87a2ff2ef 100644 --- a/memberwise-assignment/index.html +++ b/memberwise-assignment/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/memberwise-copy/index.html b/memberwise-copy/index.html index 5da4321a4..40e19f173 100644 --- a/memberwise-copy/index.html +++ b/memberwise-copy/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/memory-leak/index.html b/memory-leak/index.html index a99b33d13..edc34fd8c 100644 --- a/memory-leak/index.html +++ b/memory-leak/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/mixin/index.html b/mixin/index.html index 789309193..d9840fac6 100644 --- a/mixin/index.html +++ b/mixin/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/move-assignment-operator/index.html b/move-assignment-operator/index.html index 6fd3cb123..6f3e1a02a 100644 --- a/move-assignment-operator/index.html +++ b/move-assignment-operator/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/move-constructor/index.html b/move-constructor/index.html index 8e5722c57..e22b5b6c9 100644 --- a/move-constructor/index.html +++ b/move-constructor/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/move-semantics/index.html b/move-semantics/index.html index ef4d3e4e0..0858e3821 100644 --- a/move-semantics/index.html +++ b/move-semantics/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/named-cast/index.html b/named-cast/index.html index 470c58fb3..c5ba5d521 100644 --- a/named-cast/index.html +++ b/named-cast/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/namespace/index.html b/namespace/index.html index 5472cd505..7265f9250 100644 --- a/namespace/index.html +++ b/namespace/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/narrowing-convertions/index.html b/narrowing-convertions/index.html index cc845665a..f0b22649b 100644 --- a/narrowing-convertions/index.html +++ b/narrowing-convertions/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/non-static-member-initialization/index.html b/non-static-member-initialization/index.html index 395eefd36..ef1a13569 100644 --- a/non-static-member-initialization/index.html +++ b/non-static-member-initialization/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/numeric promotions/index.html b/numeric promotions/index.html index c253e3e33..1d50a6f6d 100644 --- a/numeric promotions/index.html +++ b/numeric promotions/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/numeric-conversions/index.html b/numeric-conversions/index.html index b75f18bd7..e31ea3059 100644 --- a/numeric-conversions/index.html +++ b/numeric-conversions/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/object-composition/index.html b/object-composition/index.html index 433703b53..00574e9a7 100644 --- a/object-composition/index.html +++ b/object-composition/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/object-like-macros/index.html b/object-like-macros/index.html index 3d74ec00f..27075197b 100644 --- a/object-like-macros/index.html +++ b/object-like-macros/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/object-slicing/index.html b/object-slicing/index.html index b8cfde25e..971d63d36 100644 --- a/object-slicing/index.html +++ b/object-slicing/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/object/index.html b/object/index.html index b468cbcd9..3f7cff26b 100644 --- a/object/index.html +++ b/object/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/one-definition-rule/index.html b/one-definition-rule/index.html index 9ec149fc8..a8d316515 100644 --- a/one-definition-rule/index.html +++ b/one-definition-rule/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/operands/index.html b/operands/index.html index 77efd4d3a..1345dab68 100644 --- a/operands/index.html +++ b/operands/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/operator/index.html b/operator/index.html index 94e98fe2c..1b41bed16 100644 --- a/operator/index.html +++ b/operator/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/optional-parameter/index.html b/optional-parameter/index.html index 8f93e7042..e2a0b51dc 100644 --- a/optional-parameter/index.html +++ b/optional-parameter/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/out-of-scope/index.html b/out-of-scope/index.html index 091aaf2e5..40387e407 100644 --- a/out-of-scope/index.html +++ b/out-of-scope/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/overload-resolution/index.html b/overload-resolution/index.html index 9255c4c11..aba94a007 100644 --- a/overload-resolution/index.html +++ b/overload-resolution/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/overload/index.html b/overload/index.html index 85288477e..2174ea14c 100644 --- a/overload/index.html +++ b/overload/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/override/index.html b/override/index.html index 4ba9d8be7..bad4c31f3 100644 --- a/override/index.html +++ b/override/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/parameters/index.html b/parameters/index.html index aaf922323..2117c8498 100644 --- a/parameters/index.html +++ b/parameters/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/parent-class/index.html b/parent-class/index.html index b532b34eb..e318a10c1 100644 --- a/parent-class/index.html +++ b/parent-class/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/pass-by-address/index.html b/pass-by-address/index.html index aefb77601..ffb9d0a25 100644 --- a/pass-by-address/index.html +++ b/pass-by-address/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/pass-by-reference/index.html b/pass-by-reference/index.html index 02e61c66d..5e4ddc7d6 100644 --- a/pass-by-reference/index.html +++ b/pass-by-reference/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/pass-by-value/index.html b/pass-by-value/index.html index 7c646b366..7ddb39977 100644 --- a/pass-by-value/index.html +++ b/pass-by-value/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/placeholder-types/index.html b/placeholder-types/index.html index 91dc2b9e0..663be7210 100644 --- a/placeholder-types/index.html +++ b/placeholder-types/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/pointer-to-const/index.html b/pointer-to-const/index.html index acafa5c2d..323d039c4 100644 --- a/pointer-to-const/index.html +++ b/pointer-to-const/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/polymorphism/index.html b/polymorphism/index.html index e5bcee0e0..0ad219233 100644 --- a/polymorphism/index.html +++ b/polymorphism/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/preprocessor-directive/index.html b/preprocessor-directive/index.html index 6b91a4eb9..c18efdc26 100644 --- a/preprocessor-directive/index.html +++ b/preprocessor-directive/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/preprocessor/index.html b/preprocessor/index.html index 725b1667f..ae1942627 100644 --- a/preprocessor/index.html +++ b/preprocessor/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/private-inheritance/index.html b/private-inheritance/index.html index 98cd77436..6e9426559 100644 --- a/private-inheritance/index.html +++ b/private-inheritance/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/private-member/index.html b/private-member/index.html index c9fb36074..fcd1032bf 100644 --- a/private-member/index.html +++ b/private-member/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/protected-inheritance/index.html b/protected-inheritance/index.html index f3f738fea..14efcb4f0 100644 --- a/protected-inheritance/index.html +++ b/protected-inheritance/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/protected-members/index.html b/protected-members/index.html index 3059c7c7b..e4ce0b456 100644 --- a/protected-members/index.html +++ b/protected-members/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/public-inheritance/index.html b/public-inheritance/index.html index 2f844e8ad..8c68f5478 100644 --- a/public-inheritance/index.html +++ b/public-inheritance/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/public-member/index.html b/public-member/index.html index 10e8a82e4..76008209d 100644 --- a/public-member/index.html +++ b/public-member/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/pure-declaration/index.html b/pure-declaration/index.html index 292f41160..5e08b27cd 100644 --- a/pure-declaration/index.html +++ b/pure-declaration/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/pure-virtual/index.html b/pure-virtual/index.html index baed1ba5a..59462663c 100644 --- a/pure-virtual/index.html +++ b/pure-virtual/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/qualifier/index.html b/qualifier/index.html index 67aa1b45e..bbc336f43 100644 --- a/qualifier/index.html +++ b/qualifier/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/quiz/index.html b/quiz/index.html index c50882316..e384a047e 100644 --- a/quiz/index.html +++ b/quiz/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/random-access-memory/index.html b/random-access-memory/index.html index 8a3816fcf..43f4505a7 100644 --- a/random-access-memory/index.html +++ b/random-access-memory/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/range-based-for-loops/index.html b/range-based-for-loops/index.html index 79db0b6b3..3dd982f75 100644 --- a/range-based-for-loops/index.html +++ b/range-based-for-loops/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/raw-pointer/index.html b/raw-pointer/index.html index c722e3a76..c1e67d797 100644 --- a/raw-pointer/index.html +++ b/raw-pointer/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/refactoring/index.html b/refactoring/index.html index 724b1c175..ccd25150d 100644 --- a/refactoring/index.html +++ b/refactoring/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/reference-container/index.html b/reference-container/index.html index d5d735a14..13245f336 100644 --- a/reference-container/index.html +++ b/reference-container/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/reflexive association/index.html b/reflexive association/index.html index 5011f32e8..5ac688942 100644 --- a/reflexive association/index.html +++ b/reflexive association/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/reinterpret-casts/index.html b/reinterpret-casts/index.html index 354a4450b..8449c9907 100644 --- a/reinterpret-casts/index.html +++ b/reinterpret-casts/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/return-by-address/index.html b/return-by-address/index.html index c040d6142..4f9a7b6f2 100644 --- a/return-by-address/index.html +++ b/return-by-address/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/return-by-reference/index.html b/return-by-reference/index.html index b608c07a8..eb7b5233f 100644 --- a/return-by-reference/index.html +++ b/return-by-reference/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/return-by-value/index.html b/return-by-value/index.html index 377e5d6e6..2d702ba3c 100644 --- a/return-by-value/index.html +++ b/return-by-value/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/return-type-deduction/index.html b/return-type-deduction/index.html index 6c463ef44..895a6a5b7 100644 --- a/return-type-deduction/index.html +++ b/return-type-deduction/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/rounding-error/index.html b/rounding-error/index.html index 3922c3faa..d56ac2429 100644 --- a/rounding-error/index.html +++ b/rounding-error/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/runtime/index.html b/runtime/index.html index 0efda0ef8..813f67935 100644 --- a/runtime/index.html +++ b/runtime/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/rvalue-reference/index.html b/rvalue-reference/index.html index 3278e851c..8743d3f91 100644 --- a/rvalue-reference/index.html +++ b/rvalue-reference/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/rvalue/index.html b/rvalue/index.html index 97b68442d..b0c009a1d 100644 --- a/rvalue/index.html +++ b/rvalue/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/scaling/index.html b/scaling/index.html index d5897c84d..18f4c3552 100644 --- a/scaling/index.html +++ b/scaling/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/scope-resolution-operator/index.html b/scope-resolution-operator/index.html index ec0bc7be9..d9b772c07 100644 --- a/scope-resolution-operator/index.html +++ b/scope-resolution-operator/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/scope/index.html b/scope/index.html index 6b93f5527..9e898ff86 100644 --- a/scope/index.html +++ b/scope/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/scoped-enumerations/index.html b/scoped-enumerations/index.html index 12d8114de..ab1e1aae9 100644 --- a/scoped-enumerations/index.html +++ b/scoped-enumerations/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/search/search_index.json b/search/search_index.json index 7e2b9ff8d..a6d6f2c8b 100644 --- a/search/search_index.json +++ b/search/search_index.json @@ -1 +1 @@ -{"config":{"indexing":"full","lang":["en"],"min_search_length":3,"prebuild_index":false,"separator":"[\\s\\-]+"},"docs":[{"location":"","text":"LearnCpp \u4e2d\u6587\u7ffb\u8bd1\u7ad9\u70b9 \u672c\u7ad9\u51fa\u4e8e\u5b66\u4e60\u7684\u76ee\u7684\uff0c\u5bf9 https://www.learncpp.com \u7ad9\u70b9\u7684\u5185\u5bb9\u8fdb\u884c\u4e86\u7ffb\u8bd1\u3002Learn C++ \u662f\u6211\u770b\u5230\u7684\u6700\u57fa\u7840\u3001\u6700\u7ec6\u81f4\u4e5f\u6700\u5b9e\u9645\u7684C++\u521d\u5b66\u8005\u6559\u7a0b\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u8be5\u7f51\u7ad9\u5b8c\u5168\u514d\u8d39\u800c\u4e14\u4ece2007\u5e74\u6d3b\u8dc3\u81f3\u4eca\uff082022\u5e74\uff09\uff0c\u5e0c\u671b\u5927\u5bb6\u5728\u9605\u8bfb\u672c\u4e2d\u6587\u8bd1\u672c\u7684\u540c\u65f6\uff0c\u80fd\u591f\u5230\u539f\u7f51\u7ad9\u53bb\u770b\u770b\uff0c\u901a\u8fc7\u6d4f\u89c8\u5e7f\u544a\u6216\u8d5e\u52a9\u7684\u65b9\u5f0f\u652f\u6301\u539f\u4f5c\u8005Alex\u3002 \u6b64\u5916\uff0c\u7531\u4e8e\u539f\u8bfe\u7a0b\u975e\u5e38\u57fa\u7840\uff0c\u4e8b\u65e0\u5de8\u7ec6\u5730\u4ecb\u7ecd\u4e86\u8bed\u8a00\u7684\u65b9\u65b9\u9762\u9762\uff0c\u5bf9\u4e8e\u6709\u5176\u4ed6\u8bed\u8a00\u7f16\u7a0b\u57fa\u7840\u7684\u8bfb\u8005\u6765\u8bf4\u53ef\u80fd\u6709\u4e9b\u5570\u55e6\uff0c\u56e0\u6b64\u8bd1\u8005\u5728\u7ffb\u8bd1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4e5f\u5bf9\u6bcf\u8282\u8bfe\u7684\u77e5\u8bc6\u70b9\u8fdb\u884c\u4e86\u63d0\u70bc\uff0c\u5c06\u6bcf\u8282\u8bfe\u4e2d\u6700\u4e3a\u7cbe\u534e\u7684\u90e8\u5206\u5217\u5728\u6587\u7ae0\u5f00\u5934\u90e8\u5206\uff0c\u4f9b\u6b64\u7c7b\u8bfb\u8005\u53c2\u8003\u3002 \u7ffb\u8bd1\u987a\u5e8f\uff08\u6309\u7ae0\u8282\uff09 \u57fa\u7840\uff1a1\u30012\u30014\u30015\u30016\u30018\u30019\u300111\u300121\u300122\u300123\u30017\u300112 \u9762\u5411\u5bf9\u8c61\uff1a13\u300114\u300115\u300116\u300117\u300118\u300119\u300120 \u5176\u4ed6\uff1a3\u3001M\u3001O\u3001A\u3001B\u3001C \u672c\u9879\u76ee\u7684\u4f18\u52bf \u8001\u53f8\u673a\u4e2d\u6587\u7cbe\u8bd1\uff08\u5e9f\u8bdd\uff09 \u66f4\u52a0\u7f8e\u89c2\u7684\u6392\u7248\u548c\u6e32\u67d3\u6548\u679c \u65e0\u5e7f\u544a \u63d0\u53d6\u4e86\u5feb\u901f\u8bb0\u5fc6\u70b9\u7528\u4e8e\u590d\u4e60 \u901a\u8fc7\u5bc6\u96c6\u7684\u4ea4\u53c9\u5f15\u7528\u3001\u5185\u90e8\u94fe\u63a5\u548ctag\uff0c\u5e2e\u52a9\u8bfb\u8005\u66f4\u52a0\u65b9\u4fbf\u5730\u627e\u5230\u76f8\u5173\u5185\u5bb9\u5e76\u5efa\u7acb\u7f51\u72b6\u601d\u7ef4 \u672c\u9879\u76ee\u7684\u52a3\u52bf \u548c\u539f\u7248\u672c\u5728\u66f4\u65b0\u901f\u5ea6\u4e0a\u5b58\u5728\u5dee\u5f02\uff0c\u66f4\u65b0\u548c\u52d8\u8bef\u90fd\u662f\u6ede\u540e\u7684\uff08\u5e9f\u8bdd\uff09 \u6682\u672a\u5305\u542b\u6bcf\u8282\u8bfe\u7684\u5c0f\u6d4b\u9a8c\u90e8\u5206 \u6ca1\u6709\u5305\u542b\u539f\u6587\u4e0b\u65b9\u7684\u7f51\u53cb\u8ba8\u8bba\u5185\u5bb9 \u8bd1\u8005\u7684\u5176\u4ed6\u7ffb\u8bd1\u9879\u76ee\uff1a \u9879\u76ee Star \u8ba1\u7b97\u673a\u6559\u80b2\u4e2d\u7f3a\u5931\u7684\u4e00\u8bfe - The Missing Semester awesome-python-cn awesome-go-cn","title":"\u5173\u4e8e\u672c\u7ad9"},{"location":"#learncpp","text":"\u672c\u7ad9\u51fa\u4e8e\u5b66\u4e60\u7684\u76ee\u7684\uff0c\u5bf9 https://www.learncpp.com \u7ad9\u70b9\u7684\u5185\u5bb9\u8fdb\u884c\u4e86\u7ffb\u8bd1\u3002Learn C++ \u662f\u6211\u770b\u5230\u7684\u6700\u57fa\u7840\u3001\u6700\u7ec6\u81f4\u4e5f\u6700\u5b9e\u9645\u7684C++\u521d\u5b66\u8005\u6559\u7a0b\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u8be5\u7f51\u7ad9\u5b8c\u5168\u514d\u8d39\u800c\u4e14\u4ece2007\u5e74\u6d3b\u8dc3\u81f3\u4eca\uff082022\u5e74\uff09\uff0c\u5e0c\u671b\u5927\u5bb6\u5728\u9605\u8bfb\u672c\u4e2d\u6587\u8bd1\u672c\u7684\u540c\u65f6\uff0c\u80fd\u591f\u5230\u539f\u7f51\u7ad9\u53bb\u770b\u770b\uff0c\u901a\u8fc7\u6d4f\u89c8\u5e7f\u544a\u6216\u8d5e\u52a9\u7684\u65b9\u5f0f\u652f\u6301\u539f\u4f5c\u8005Alex\u3002 \u6b64\u5916\uff0c\u7531\u4e8e\u539f\u8bfe\u7a0b\u975e\u5e38\u57fa\u7840\uff0c\u4e8b\u65e0\u5de8\u7ec6\u5730\u4ecb\u7ecd\u4e86\u8bed\u8a00\u7684\u65b9\u65b9\u9762\u9762\uff0c\u5bf9\u4e8e\u6709\u5176\u4ed6\u8bed\u8a00\u7f16\u7a0b\u57fa\u7840\u7684\u8bfb\u8005\u6765\u8bf4\u53ef\u80fd\u6709\u4e9b\u5570\u55e6\uff0c\u56e0\u6b64\u8bd1\u8005\u5728\u7ffb\u8bd1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4e5f\u5bf9\u6bcf\u8282\u8bfe\u7684\u77e5\u8bc6\u70b9\u8fdb\u884c\u4e86\u63d0\u70bc\uff0c\u5c06\u6bcf\u8282\u8bfe\u4e2d\u6700\u4e3a\u7cbe\u534e\u7684\u90e8\u5206\u5217\u5728\u6587\u7ae0\u5f00\u5934\u90e8\u5206\uff0c\u4f9b\u6b64\u7c7b\u8bfb\u8005\u53c2\u8003\u3002","title":"LearnCpp \u4e2d\u6587\u7ffb\u8bd1\u7ad9\u70b9"},{"location":"#_1","text":"\u57fa\u7840\uff1a1\u30012\u30014\u30015\u30016\u30018\u30019\u300111\u300121\u300122\u300123\u30017\u300112 \u9762\u5411\u5bf9\u8c61\uff1a13\u300114\u300115\u300116\u300117\u300118\u300119\u300120 \u5176\u4ed6\uff1a3\u3001M\u3001O\u3001A\u3001B\u3001C \u672c\u9879\u76ee\u7684\u4f18\u52bf \u8001\u53f8\u673a\u4e2d\u6587\u7cbe\u8bd1\uff08\u5e9f\u8bdd\uff09 \u66f4\u52a0\u7f8e\u89c2\u7684\u6392\u7248\u548c\u6e32\u67d3\u6548\u679c \u65e0\u5e7f\u544a \u63d0\u53d6\u4e86\u5feb\u901f\u8bb0\u5fc6\u70b9\u7528\u4e8e\u590d\u4e60 \u901a\u8fc7\u5bc6\u96c6\u7684\u4ea4\u53c9\u5f15\u7528\u3001\u5185\u90e8\u94fe\u63a5\u548ctag\uff0c\u5e2e\u52a9\u8bfb\u8005\u66f4\u52a0\u65b9\u4fbf\u5730\u627e\u5230\u76f8\u5173\u5185\u5bb9\u5e76\u5efa\u7acb\u7f51\u72b6\u601d\u7ef4 \u672c\u9879\u76ee\u7684\u52a3\u52bf \u548c\u539f\u7248\u672c\u5728\u66f4\u65b0\u901f\u5ea6\u4e0a\u5b58\u5728\u5dee\u5f02\uff0c\u66f4\u65b0\u548c\u52d8\u8bef\u90fd\u662f\u6ede\u540e\u7684\uff08\u5e9f\u8bdd\uff09 \u6682\u672a\u5305\u542b\u6bcf\u8282\u8bfe\u7684\u5c0f\u6d4b\u9a8c\u90e8\u5206 \u6ca1\u6709\u5305\u542b\u539f\u6587\u4e0b\u65b9\u7684\u7f51\u53cb\u8ba8\u8bba\u5185\u5bb9 \u8bd1\u8005\u7684\u5176\u4ed6\u7ffb\u8bd1\u9879\u76ee\uff1a \u9879\u76ee Star \u8ba1\u7b97\u673a\u6559\u80b2\u4e2d\u7f3a\u5931\u7684\u4e00\u8bfe - The Missing Semester awesome-python-cn awesome-go-cn","title":"\u7ffb\u8bd1\u987a\u5e8f\uff08\u6309\u7ae0\u8282\uff09"},{"location":"0-8-A-few-common-C%2B%2B-problems/","tags":["setup"],"text":"","title":"0.8 - \u4e00\u4e9b\u5178\u578b\u7684 C++ \u95ee\u9898"},{"location":"1-1-Statements-and-the-structure-of-a-program/","tags":["statements"],"text":"\u7ae0\u8282\u7b80\u4ecb \u6b22\u8fce\u6765\u5230\u6211\u4eec C++\u6559\u7a0b\u7684\u7b2c\u4e00\u4e2a\u4e3b\u8981\u7ae0\u8282\uff01 \u672c\u7ae0\u8282\u5c06\u9996\u5148\u4ecb\u7ecd\u6709\u5173 C++ \u7a0b\u5e8f\u6700\u57fa\u672c\u7684\u8bdd\u9898\u3002\u7531\u4e8e\u672c\u7ae0\u8282\u6d89\u53ca\u7684\u8bdd\u9898\u8f83\u591a\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u4f1a\u5bf9\u6bcf\u4e00\u4e2a\u90e8\u5206\u90fd\u8fdb\u884c\u76f8\u5bf9\u7b80\u5355\u7684\u4ecb\u7ecd\uff0c\u5176\u76ee\u7684\u53ea\u662f\u4e3a\u4e86\u8ba9\u4f60\u80fd\u591f\u7406\u89e3\u6784\u6210 C++\u7a0b\u5e8f\u7684\u57fa\u672c\u5143\u7d20\u6709\u54ea\u4e9b\u3002\u5728\u5b8c\u6210\u672c\u7ae0\u8282\u7684\u5b66\u4e60\u540e\uff0c\u4f60\u5e94\u5f53\u53ef\u4ee5\u72ec\u7acb\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\u4e86\u3002 \u5728\u540e\u7eed\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u4f1a\u590d\u4e60\u5e76\u6df1\u5165\u63a2\u8ba8\u8fd9\u4e9b\u8bdd\u9898\uff0c\u540c\u65f6\u4ecb\u7ecd\u5c24\u5176\u884d\u751f\u51fa\u7684\u4e00\u4e9b\u65b0\u7684\u6982\u5ff5\u3002 \u4e3a\u4e86\u4f7f\u6bcf\u8282\u8bfe\u90fd\u53ef\u4ee5\u4fdd\u6301\u9002\u5f53\u7684\u4f53\u91cf\uff0c\u6709\u4e9b\u8bdd\u9898\u88ab\u5206\u5272\u6210\u4e86\u591a\u8282\u8bfe\u3002\u5982\u679c\u4f60\u89c9\u5f97\u6709\u4e9b\u91cd\u8981\u7684\u6982\u5ff5\u5728\u4e00\u8282\u8bfe\u4e2d\u5c1a\u672a\u4ecb\u7ecd\uff0c\u90a3\u4e48\u5b83\u5f88\u53ef\u80fd\u4f1a\u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u8bb2\u89e3\u3002 \u8bed\u53e5 \u8ba1\u7b97\u673a\u7a0b\u5e8f\u6307\u7684\u662f\u63a7\u5236\u8ba1\u7b97\u673a\u5b8c\u6210\u67d0\u4ef6\u4e8b\u6240\u9700\u8981\u7684\u4e00\u7cfb\u5217 \u6307\u4ee4(instuction) \u3002 \u8bed\u53e5 \u662f\u6307\u4ee4\u7684\u4e00\u79cd\uff0c\u5b83\u63a7\u5236\u8ba1\u7b97\u673a\u5b8c\u6210\u67d0\u4e9b\u64cd\u4f5c\u3002 \u5c31\u76ee\u524d\u800c\u8a00\uff0c\u8bed\u53e5\u662f C++\u7a0b\u5e8f\u4e2d\u6700\u5e38\u89c1\u7684\u4e00\u79cd\u6307\u4ee4\u7c7b\u578b\u3002\u8fd9\u4e3b\u8981\u662f\u56e0\u4e3a\u5b83\u4eec\u662f C++\u8bed\u8a00\u4e2d\u6700\u7b80\u5355\u7684\u3001\u72ec\u7acb\u7684\u8ba1\u7b97\u5355\u5143\u3002\u4ece\u8fd9\u4e2a\u610f\u4e49\u4e0a\u6765\u8bb2\uff0c\u8bed\u53e5\u4e4b\u4e8e\u7f16\u7a0b\u8bed\u8a00\uff0c\u5c31\u597d\u6bd4\u53e5\u5b50\u4e4b\u4e8e\u81ea\u7136\u8bed\u8a00\u3002\u5f53\u4f60\u9700\u8981\u5411\u5176\u4ed6\u4eba\u4f20\u9012\u4fe1\u606f\u7684\u65f6\u5019\uff0c\u901a\u5e38\u6211\u4eec\u4f1a\u4f7f\u7528\u53e5\u5b50\uff0c\u800c\u4e0d\u662f\u4e00\u4e9b\u968f\u673a\u7684\u8bcd\u8bed\u6216\u97f3\u8282\u3002 \u5728 C++\u4e2d\uff0c\u6211\u4eec\u4e00\u822c\u4e5f\u662f\u901a\u8fc7\u8bed\u53e5\u5411\u7a0b\u5e8f\u4f20\u8fbe\u6211\u4eec\u7684\u610f\u56fe\u7684\u3002 \u5927\u591a\u6570\uff08\u4f46\u4e0d\u662f\u5168\u90e8\uff09\u7684 C++\u8bed\u53e5\u4ee5\u5206\u53f7\u7ed3\u5c3e\u3002\u5982\u679c\u4f60\u770b\u5230\u4e00\u884c\u4ee5\u5206\u53f7\u7ed3\u5c3e\u7684\u4ee3\u7801\uff0c\u90a3\u4e48\u5b83\u591a\u534a\u5c31\u662f\u4e00\u6761\u8bed\u53e5\u4e86\u3002 \u5728 C++\u8fd9\u6837\u7684\u9ad8\u7ea7\u8bed\u8a00\u4e2d\uff0c\u4e00\u6761\u8bed\u53e5\u53ef\u80fd\u4f1a\u88ab\u7f16\u8bd1\u4e3a\u591a\u6761\u673a\u5668\u6307\u4ee4\u3002 \u6269\u5c55\u9605\u8bfb C++\u4e2d\u6709\u5f88\u591a\u4e0d\u540c\u7c7b\u578b\u7684\u8bed\u53e5\uff1a \u58f0\u660e\u8bed\u53e5 Declaration statements \u8df3\u8f6c\u8bed\u53e5 Jump statements \u8868\u8fbe\u5f0f\u8bed\u53e5 Expression statements \u590d\u5408\u8bed\u53e5 Compound statements \u9009\u62e9\u8bed\u53e5\uff08\u6761\u4ef6\uff09Selection statements (conditionals) \u8fed\u4ee3\u8bed\u53e5\uff08\u5faa\u73af\uff09Iteration statements (loops) Try \u8bed\u53e5\u5757 Try blocks \u5728\u7ed3\u675f\u672c\u6559\u7a0b\u7684\u5b66\u4e60\u540e\uff0c\u4f60\u5c06\u638c\u63e1\u4e0a\u8ff0\u5168\u90e8\u7684\u8bed\u53e5\u7c7b\u578b\uff01 \u51fd\u6570\u548c main \u51fd\u6570 \u5728 C++ \u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5408\u5e76\u4f7f\u7528\u4e00\u7ec4\u8bed\u53e5\uff0c\u8fd9\u4e00\u7ec4\u8bed\u53e5\u79f0\u4e3a\u4e00\u4e2a\u51fd\u6570\u3002\u51fd\u6570\u5c31\u662f\u4e00\u7ec4\u987a\u5e8f\u6267\u884c\uff08\u4ece\u4e0a\u5411\u4e0b\u6309\u987a\u5e8f\u6267\u884c\uff09\u7684\u8bed\u53e5\u7684\u96c6\u5408\u3002\u5728\u6211\u4eec\u5b66\u4e60\u7f16\u7a0b\u7a0b\u5e8f\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u4f1a\u7f16\u5199\u81ea\u5df1\u7684\u51fd\u6570\uff0c\u6839\u636e\u6211\u4eec\u7684\u76ee\u7684\u6df7\u5408\u6216\u4f7f\u7528\u8bed\u53e5\uff08\u5177\u4f53\u505a\u6cd5\u5c06\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\uff09\u3002 \u6cd5\u5219 \u6bcf\u4e2a C++\u7a0b\u5e8f\u90fd\u5fc5\u987b\u5305\u542b\u4e00\u4e2a\u51fd\u6570\u540d\u4e3a main \u7684\u51fd\u6570(main \u5168\u90e8\u4e3a\u5c0f\u5199\u5b57\u6bcd)\u3002\u5f53\u7a0b\u5e8f\u5f00\u59cb\u8fd0\u884c\u7684\u65f6\u5019\uff0c main \u51fd\u6570\u4e2d\u7684\u4ee3\u7801\u4f1a\u88ab\u987a\u5e8f\u6267\u884c\u3002 \u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u901a\u5e38\u4f1a\u5728\u5b8c\u6210 main \u51fd\u6570\u4e2d\u6700\u540e\u4e00\u6761\u8bed\u53e5\u540e\u7ec8\u6b62\uff08\u5c3d\u7ba1\u4e5f\u53ef\u80fd\u63d0\u524d\u7ec8\u6b62\u6216\u8005\u5728\u7ed3\u675f\u540e\u6267\u884c\u4e00\u4e9b\u6e05\u7406\u64cd\u4f5c\uff09\u3002 \u7f16\u5199\u7684\u51fd\u6570\u4e00\u822c\u662f\u4e3a\u4e86\u5b8c\u6210\u67d0\u4e2a\u7279\u5b9a\u7684\u5de5\u4f5c\u3002\u4f8b\u5982\uff0c\u540d\u4e3a \"max\" \u7684\u51fd\u6570\u4e00\u822c\u7528\u4e8e\u4e24\u4e2a\u6570\u5b57\u6bd4\u5927\u5c0f\u3002\u800c\u540d\u4e3a \u201ccalculateGrade\u201d \u7684\u51fd\u6570\u5219\u7528\u4e8e\u8ba1\u7b97\u5b66\u751f\u6210\u7ee9\uff08grade\uff09\u3002\u6211\u4eec\u4f1a\u5728\u51fd\u6570\u4e00\u8282\u4e2d\u5bf9\u5176\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u6bd5\u7adf\u51fd\u6570\u662f\u6784\u5efa\u7a0b\u5e8f\u7684\u6700\u5e38\u7528\u7684\u5de5\u5177\u3002 \u4f5c\u8005\u6ce8 \u5f53\u6211\u4eec\u8c08\u8bba\u51fd\u6570\u65f6\uff0c\u901a\u5e38\u4f1a\u5728\u51fd\u6570\u540d\u540e\u9762\u52a0\u4e0a\u4e00\u7ec4\u62ec\u53f7\u4f5c\u4e3a\u4e00\u79cd\u901f\u8bb0\u6cd5\u3002\u4f8b\u5982\uff0c\u5f53\u4f60\u770b\u5230 main() \u6216\u8005 doSomething() \u7684\u65f6\u5019\uff0c\u4f60\u5e94\u8be5\u6e05\u695a\u6b64\u65f6\u6211\u4eec\u5728\u8c08\u8bba\u4e00\u4e2a\u540d\u4e3a main \u6216\u8005 doSomething \u7684\u51fd\u6570\u3002\u8fd9\u4e48\u505a\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5c06\u51fd\u6570\u548c\u5176\u4ed6\u7c7b\u578b\u7684\u540d\u79f0\u533a\u5206\u5f00\u6765\uff08\u4f8b\u5982\u53d8\u91cf\u540d\uff09\uff0c\u540c\u65f6\u4e0d\u5fc5\u6bcf\u6b21\u90fd\u52a0\u4e0a\u201c\u51fd\u6570\u201d\u4e8c\u5b57\u3002 Hello world! \u7a0b\u5e8f\u5256\u6790 \u73b0\u5728\uff0c\u4f60\u5df2\u7ecf\u521d\u6b65\u4e86\u89e3\u4e86\u4ec0\u4e48\u662f\u8bed\u53e5\u4ee5\u53ca\u4ec0\u4e48\u662f\u51fd\u6570\uff0c\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u56de\u5230 \u201cHello world\u201d \u7a0b\u5e8f\u5e76\u9010\u884c\u5bf9\u5176\u8fdb\u884c\u5206\u6790\u3002 1 2 3 4 5 6 7 #include int main () { std :: cout << \"Hello world!\" ; return 0 ; } \u7b2c\u4e00\u884c \u6bd4\u8f83\u7279\u6b8a\uff0c\u79f0\u4e3a \u9884\u5904\u7406\u5668\u6307\u4ee4 (preprocessor directive) \u3002\u8be5\u6307\u4ee4\u8868\u660e\u7528\u6237\u9700\u8981\u4f7f\u7528 iostream \u5e93\u7684\u5185\u5bb9\uff0c\u8be5\u5e93\u5c5e\u4e8e C++\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u4f7f\u7528\u8be5\u5e93\u63d0\u4f9b\u7684\u529f\u80fd\u4ece\u63a7\u5236\u53f0(console)\u8bfb\u5199\u6587\u672c\u3002\u6211\u4eec\u9700\u8981\u8fd9\u4e00\u884c\u4ee3\u7801\uff0c\u662f\u4e3a\u4e86\u5728\u7b2c\u4e94\u884c\u4f7f\u7528 std:: cout \u3002\u5982\u679c\u5c06\u8fd9\u4e00\u884c\u5220\u9664\uff0c\u5219\u4f1a\u5bfc\u81f4\u7b2c\u4e94\u884c\u7f16\u8bd1\u65f6\u62a5\u9519\uff0c\u56e0\u4e3a\u8fd9\u6837 \u7f16\u8bd1\u5668(compiler) \u5c31\u4e0d\u77e5\u9053 std:: cout \u662f\u4ec0\u4e48\u4e86\u3002 \u7b2c\u4e8c\u884c \u662f\u7a7a\u884c\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c06\u5176\u5ffd\u7565\u3002\u5b83\u7684\u5b58\u5728\u4ec5\u4ec5\u662f\u4e3a\u4e86\u8ba9\u7a0b\u5e8f\u53ef\u8bfb\u6027\u53d8\u5f97\u66f4\u597d\uff08\u5c06 #include \u9884\u5904\u7406\u5668\u6307\u4ee4\u548c\u7a0b\u5e8f\u7684\u5176\u4ed6\u90e8\u5206\u5206\u5272\u5f00\u6765\uff09\u3002 \u7b2c\u4e09\u884c \u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u6b64\u5904\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a main \u7684\u51fd\u6570\u3002\u4e4b\u524d\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u5230\uff0c\u6bcf\u4e2a C++\u7a0b\u5e8f\u90fd\u5fc5\u987b\u6709\u4e00\u4e2a main \u51fd\u6570\uff0c\u5426\u5219\u5c06\u4f1a \u94fe\u63a5(link) \u5931\u8d25\u3002 \u7b2c\u56db\u884c \u548c \u7b2c\u4e03\u884c \u544a\u8bc9\u7f16\u8bd1\u5668\u63a5\u4e0b\u6765\u54ea\u4e9b\u884c\u4ee3\u7801\u5c5e\u4e8e main \u51fd\u6570\u3002\u6240\u6709\u4f4d\u4e8e\u7b2c\u56db\u884c\u548c\u7b2c\u4e03\u884c\u62ec\u53f7\u5185\u7684\u4ee3\u7801\uff0c\u90fd\u5c5e\u4e8e main \u51fd\u6570\u7684\u4e00\u90e8\u5206\uff0c\u79f0\u4e3a\u51fd\u6570\u4f53\u3002 \u7b2c\u4e94\u884c \u662f main \u51fd\u6570\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\uff0c\u8be5\u8bed\u53e5\u4e5f\u662f\u7a0b\u5e8f\u6267\u884c\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u3002 std:: cout \uff08\u8868\u793a\u201ccharacter output\u201d\uff0c\u5373\u5b57\u7b26\u8f93\u51fa\uff09\u548c << \u8fd0\u7b97\u7b26\u53ef\u4ee5\u5c06\u5b57\u6bcd\u6216\u6570\u5b57\u4ece\u63a7\u5236\u53f0\u8f93\u51fa\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u5b57\u7b26\u4e32\u6587\u672c \u201cHello world!\u201d \u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002\u8fd9\u884c\u4ee3\u7801\u7684\u6267\u884c\u6548\u679c\u5c31\u662f\u4e3a\u7a0b\u5e8f\u521b\u5efa\u4e00\u4e2a\u53ef\u89c6\u5316\u7684\u8f93\u51fa\u7ed3\u679c\u3002 \u7b2c\u516d\u884c \u662f\u4e00\u4e2a\u8fd4\u56de\u8bed\u53e5\u3002\u5f53\u7a0b\u5e8f\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u5b83\u9700\u8981\u5411\u64cd\u4f5c\u7cfb\u7edf\u8fd4\u56de\u4e00\u4e2a\u503c\u6765\u8868\u660e\u5176\u6267\u884c\u7684\u7ed3\u679c\u662f\u6210\u529f\u8fd8\u662f\u5931\u8d25\u3002\u8be5\u8fd4\u56de\u8bed\u53e5\u5411\u64cd\u4f5c\u7cfb\u7edf\u8fd4\u56de\u4e86 0\uff0c\u8868\u793a\"\u4e00\u5207\u6b63\u5e38\"\u3002\u8fd9\u662f\u672c\u7a0b\u5e8f\u6267\u884c\u7684\u6700\u540e\u4e00\u6761\u8bed\u53e5\u3002 \u6211\u4eec\u7f16\u5199\u7684\u6240\u6709\u7a0b\u5e8f\uff0c\u90fd\u4f1a\u9075\u5faa\u4e0a\u8ff0\u901a\u7528\u6a21\u677f\uff08\u6216\u5176\u53d8\u79cd\u7248\u672c\uff09\u3002 \u4f5c\u8005\u6ce8 \u5982\u679c\u4e0a\u8ff0\u89e3\u91ca\u4ecd\u7136\u6709\u8ba9\u4f60\u56f0\u60d1\u7684\u5730\u65b9\uff0c\u522b\u62c5\u5fc3\uff0c\u8fd9\u5f88\u6b63\u5e38\u3002\u8fd9\u91cc\u7684\u76ee\u7684\u53ea\u662f\u8ba9\u4f60\u5bf9\u7a0b\u5e8f\u6709\u4e00\u4e2a\u5b8f\u89c2\u7684\u6982\u5ff5\uff0c\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u5c06\u4f1a\u5bf9\u76f8\u5173\u7684\u5185\u5bb9\u8fdb\u884c\u8be6\u7ec6\u89e3\u91ca\u5e76\u8f85\u4ee5\u6848\u4f8b\u3002 \u4f60\u53ef\u4ee5\u81ea\u884c\u7f16\u8bd1\u5e76\u8fd0\u884c\u4e0a\u8ff0\u7a0b\u5e8f\uff0c\u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u4f60\u5c06\u5728\u63a7\u5236\u53f0\u4e2d\u770b\u5230\u5982\u4e0b\u6253\u5370\u4fe1\u606f\uff1a 1 Hello world! \u5982\u679c\u4f60\u5728\u7f16\u8bd1\u6216\u8fd0\u884c\u7a0b\u5e8f\u65f6\u9047\u5230\u4e86\u95ee\u9898\uff0c\u8bf7\u53c2\u8003 \u4e00\u4e9b\u5178\u578b\u7684 C++ \u95ee\u9898 \u8bed\u6cd5\u548c\u8bed\u6cd5\u9519\u8bef \u82f1\u8bed\u4e2d\u7684\u53e5\u5b50\u57fa\u4e8e\u4f60\u6240\u5b66\u7684\u8bed\u6cd5\u89c4\u5219(grammatical rules)\u6765\u6784\u5efa\u7684\u3002\u4f8b\u5982\uff0c\u901a\u5e38\u4e00\u4e2a\u53e5\u5b50\u4f1a\u4ee5\u53e5\u53f7\u7ed3\u5c3e\u3002\u5728\u4e00\u95e8\u8bed\u8a00\u4e2d\uff0c\u89c4\u5b9a\u4e00\u4e2a\u53e5\u5b50\u5e94\u8be5\u5982\u4f55\u6784\u5efa\u7684\u89c4\u5219\uff0c\u79f0\u4e3a \u8bed\u6cd5 \uff08\u8bd1\u6ce8\uff1a\u53e5\u6cd5\u5728\u540e\u7eed\u7684\u7ffb\u8bd1\u4e2d\u5c06\u4f7f\u7528\u8bed\u6cd5\u4ee3\u66ff\uff0c\u89c1 \u7ffb\u8bd1\u7ea6\u5b9a \uff09\u3002\u5982\u679c\u4f60\u5fd8\u8bb0\u4e86\u5728\u4e24\u4e2a\u53e5\u5b50\u4e4b\u95f4\u5199\u4e0b\u53e5\u53f7\uff0c\u90a3\u4e48\u5c31\u4f1a\u8fdd\u53cd\u4e86\u82f1\u8bed\u7684\u53e5\u6cd5\u3002 C++ \u4e5f\u6709\u5b83\u7684 \u8bed\u6cd5 \u89c4\u5219\uff0c\u5b83\u89c4\u5b9a\u4e86\u4e00\u4e2a\u7a0b\u5e8f\u5e94\u8be5\u5982\u4f55\u88ab\u7f16\u5199\u624d\u80fd\u88ab\u8ba4\u4e3a\u662f\u5408\u6cd5\u7684\u3002\u5728\u7f16\u8bd1\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u7f16\u8bd1\u5668\u9700\u8981\u786e\u4fdd\u4f60\u7684\u7a0b\u5e8f\u6ee1\u8db3\u57fa\u672c\u7684 C++\u8bed\u6cd5\u3002\u5982\u679c\u4f60\u8fdd\u53cd\u4e86\u67d0\u6761\u89c4\u5219\uff0c\u7f16\u8bd1\u5668\u5c31\u4f1a\u5411\u4f60\u62a5\u544a\u4e00\u4e2a\u9519\u8bef\u3002 \u5982\u679c\uff0c\u6211\u4eec\u5c06\u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\u7b2c\u4e94\u884c\u7684\u5206\u53f7\u7701\u7565\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f 1 2 3 4 5 6 7 #include int main () { std :: cout << \"Hello world!\" return 0 ; } \u4f60\u53ef\u4ee5\u8bd5\u8bd5\u53bb\u7f16\u8bd1\u4e00\u4e0b\u8be5\u7a0b\u5e8f\u3002 Visual Studio \u5728\u7f16\u8bd1\u65f6\u4f1a\u62a5\u544a\u5982\u4e0b\u9519\u8bef (\u4f60\u7684\u7f16\u8bd1\u5668\u5728\u8868\u8ff0\u8be5\u9519\u8bef\u65f6\u53ef\u80fd\u4f1a\u4f7f\u7528\u4e0d\u540c\u7684\u8bed\u8a00)\uff1a 1 c:\\vcprojects\\test1.cpp(6): error C2143: syntax error : missing ';' before 'return' \u8be5\u9519\u8bef\u4fe1\u606f\u662f\u544a\u8bc9\u4f60\uff0c\u9519\u8bef\u53d1\u751f\u5728\u7b2c\u516d\u884c\uff1a\u7a0b\u5e8f\u671f\u671b\u5728 return \u8bed\u53e5\u524d\u9762\u68c0\u6d4b\u5230\u4e00\u4e2a\u5206\u53f7\uff0c\u4f46\u5b83\u5374\u6ca1\u6709\u3002\u5c3d\u7ba1\u7f16\u8bd1\u5668\u544a\u8bc9\u4e86\u6211\u4eec\u8be5\u8bed\u6cd5\u95ee\u9898\u53d1\u751f\u7684\u884c\u53f7\uff0c\u5176\u5b9e\u9645\u53d1\u751f\u7684\u4f4d\u7f6e\u5374\u5728\u4e0a\u4e00\u884c\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u95ee\u9898\u51fa\u5728 \u7b2c\u4e94\u884c \u7684\u672b\u5c3e\uff08\u7f16\u8bd1\u5668\u5728\u8bfb\u53d6\u5230\u7b2c\u516d\u884c\u65f6\u624d\u80fd\u53d1\u73b0\u8be5\u95ee\u9898\uff09\u3002 \u8bed\u6cd5\u9519\u8bef\u5728\u7f16\u7a0b\u65f6\u5f88\u5e38\u89c1\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u8fd9\u79cd\u7c7b\u578b\u7684\u9519\u8bef\u901a\u5e38\u6bd4\u8f83\u5bb9\u6613\u5b9a\u4f4d\u548c\u4fee\u590d\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u4f1a\u660e\u786e\u5730\u6307\u5411\u9519\u8bef\u53d1\u751f\u7684\u4f4d\u7f6e\u3002\u7a0b\u5e8f\u7684\u7f16\u8bd1\u53ea\u6709\u5728\u6240\u6709\u7684\u9519\u8bef\u90fd\u88ab\u4fee\u590d\u540e\u624d\u80fd\u591f\u5b8c\u6210\u3002 \u4f60\u53ef\u4ee5\u8bd5\u7740\u4ece\u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\u5220\u9664\u67d0\u4e9b\u5b57\u7b26\uff0c\u751a\u81f3\u5220\u9664\u6574\u884c\uff0c\u7136\u540e\u89c2\u5bdf\u7f16\u8bd1\u5668\u7684\u62a5\u9519\u4fe1\u606f\u3002\u73b0\u5728\uff0c\u5c06\u5206\u53f7\u5199\u5728\u7b2c\u4e94\u884c\u7684\u672b\u5c3e\u5e76\u5220\u9664 1\u30013 \u6216\u8005 4 \u884c\uff0c\u770b\u770b\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f","title":"1.1 - \u7a0b\u5e8f\u7ed3\u6784\u548c\u8bed\u53e5"},{"location":"1-1-Statements-and-the-structure-of-a-program/#_1","text":"\u6b22\u8fce\u6765\u5230\u6211\u4eec C++\u6559\u7a0b\u7684\u7b2c\u4e00\u4e2a\u4e3b\u8981\u7ae0\u8282\uff01 \u672c\u7ae0\u8282\u5c06\u9996\u5148\u4ecb\u7ecd\u6709\u5173 C++ \u7a0b\u5e8f\u6700\u57fa\u672c\u7684\u8bdd\u9898\u3002\u7531\u4e8e\u672c\u7ae0\u8282\u6d89\u53ca\u7684\u8bdd\u9898\u8f83\u591a\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u4f1a\u5bf9\u6bcf\u4e00\u4e2a\u90e8\u5206\u90fd\u8fdb\u884c\u76f8\u5bf9\u7b80\u5355\u7684\u4ecb\u7ecd\uff0c\u5176\u76ee\u7684\u53ea\u662f\u4e3a\u4e86\u8ba9\u4f60\u80fd\u591f\u7406\u89e3\u6784\u6210 C++\u7a0b\u5e8f\u7684\u57fa\u672c\u5143\u7d20\u6709\u54ea\u4e9b\u3002\u5728\u5b8c\u6210\u672c\u7ae0\u8282\u7684\u5b66\u4e60\u540e\uff0c\u4f60\u5e94\u5f53\u53ef\u4ee5\u72ec\u7acb\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\u4e86\u3002 \u5728\u540e\u7eed\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u4f1a\u590d\u4e60\u5e76\u6df1\u5165\u63a2\u8ba8\u8fd9\u4e9b\u8bdd\u9898\uff0c\u540c\u65f6\u4ecb\u7ecd\u5c24\u5176\u884d\u751f\u51fa\u7684\u4e00\u4e9b\u65b0\u7684\u6982\u5ff5\u3002 \u4e3a\u4e86\u4f7f\u6bcf\u8282\u8bfe\u90fd\u53ef\u4ee5\u4fdd\u6301\u9002\u5f53\u7684\u4f53\u91cf\uff0c\u6709\u4e9b\u8bdd\u9898\u88ab\u5206\u5272\u6210\u4e86\u591a\u8282\u8bfe\u3002\u5982\u679c\u4f60\u89c9\u5f97\u6709\u4e9b\u91cd\u8981\u7684\u6982\u5ff5\u5728\u4e00\u8282\u8bfe\u4e2d\u5c1a\u672a\u4ecb\u7ecd\uff0c\u90a3\u4e48\u5b83\u5f88\u53ef\u80fd\u4f1a\u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u8bb2\u89e3\u3002","title":"\u7ae0\u8282\u7b80\u4ecb"},{"location":"1-1-Statements-and-the-structure-of-a-program/#_2","text":"\u8ba1\u7b97\u673a\u7a0b\u5e8f\u6307\u7684\u662f\u63a7\u5236\u8ba1\u7b97\u673a\u5b8c\u6210\u67d0\u4ef6\u4e8b\u6240\u9700\u8981\u7684\u4e00\u7cfb\u5217 \u6307\u4ee4(instuction) \u3002 \u8bed\u53e5 \u662f\u6307\u4ee4\u7684\u4e00\u79cd\uff0c\u5b83\u63a7\u5236\u8ba1\u7b97\u673a\u5b8c\u6210\u67d0\u4e9b\u64cd\u4f5c\u3002 \u5c31\u76ee\u524d\u800c\u8a00\uff0c\u8bed\u53e5\u662f C++\u7a0b\u5e8f\u4e2d\u6700\u5e38\u89c1\u7684\u4e00\u79cd\u6307\u4ee4\u7c7b\u578b\u3002\u8fd9\u4e3b\u8981\u662f\u56e0\u4e3a\u5b83\u4eec\u662f C++\u8bed\u8a00\u4e2d\u6700\u7b80\u5355\u7684\u3001\u72ec\u7acb\u7684\u8ba1\u7b97\u5355\u5143\u3002\u4ece\u8fd9\u4e2a\u610f\u4e49\u4e0a\u6765\u8bb2\uff0c\u8bed\u53e5\u4e4b\u4e8e\u7f16\u7a0b\u8bed\u8a00\uff0c\u5c31\u597d\u6bd4\u53e5\u5b50\u4e4b\u4e8e\u81ea\u7136\u8bed\u8a00\u3002\u5f53\u4f60\u9700\u8981\u5411\u5176\u4ed6\u4eba\u4f20\u9012\u4fe1\u606f\u7684\u65f6\u5019\uff0c\u901a\u5e38\u6211\u4eec\u4f1a\u4f7f\u7528\u53e5\u5b50\uff0c\u800c\u4e0d\u662f\u4e00\u4e9b\u968f\u673a\u7684\u8bcd\u8bed\u6216\u97f3\u8282\u3002 \u5728 C++\u4e2d\uff0c\u6211\u4eec\u4e00\u822c\u4e5f\u662f\u901a\u8fc7\u8bed\u53e5\u5411\u7a0b\u5e8f\u4f20\u8fbe\u6211\u4eec\u7684\u610f\u56fe\u7684\u3002 \u5927\u591a\u6570\uff08\u4f46\u4e0d\u662f\u5168\u90e8\uff09\u7684 C++\u8bed\u53e5\u4ee5\u5206\u53f7\u7ed3\u5c3e\u3002\u5982\u679c\u4f60\u770b\u5230\u4e00\u884c\u4ee5\u5206\u53f7\u7ed3\u5c3e\u7684\u4ee3\u7801\uff0c\u90a3\u4e48\u5b83\u591a\u534a\u5c31\u662f\u4e00\u6761\u8bed\u53e5\u4e86\u3002 \u5728 C++\u8fd9\u6837\u7684\u9ad8\u7ea7\u8bed\u8a00\u4e2d\uff0c\u4e00\u6761\u8bed\u53e5\u53ef\u80fd\u4f1a\u88ab\u7f16\u8bd1\u4e3a\u591a\u6761\u673a\u5668\u6307\u4ee4\u3002 \u6269\u5c55\u9605\u8bfb C++\u4e2d\u6709\u5f88\u591a\u4e0d\u540c\u7c7b\u578b\u7684\u8bed\u53e5\uff1a \u58f0\u660e\u8bed\u53e5 Declaration statements \u8df3\u8f6c\u8bed\u53e5 Jump statements \u8868\u8fbe\u5f0f\u8bed\u53e5 Expression statements \u590d\u5408\u8bed\u53e5 Compound statements \u9009\u62e9\u8bed\u53e5\uff08\u6761\u4ef6\uff09Selection statements (conditionals) \u8fed\u4ee3\u8bed\u53e5\uff08\u5faa\u73af\uff09Iteration statements (loops) Try \u8bed\u53e5\u5757 Try blocks \u5728\u7ed3\u675f\u672c\u6559\u7a0b\u7684\u5b66\u4e60\u540e\uff0c\u4f60\u5c06\u638c\u63e1\u4e0a\u8ff0\u5168\u90e8\u7684\u8bed\u53e5\u7c7b\u578b\uff01","title":"\u8bed\u53e5"},{"location":"1-1-Statements-and-the-structure-of-a-program/#main","text":"\u5728 C++ \u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5408\u5e76\u4f7f\u7528\u4e00\u7ec4\u8bed\u53e5\uff0c\u8fd9\u4e00\u7ec4\u8bed\u53e5\u79f0\u4e3a\u4e00\u4e2a\u51fd\u6570\u3002\u51fd\u6570\u5c31\u662f\u4e00\u7ec4\u987a\u5e8f\u6267\u884c\uff08\u4ece\u4e0a\u5411\u4e0b\u6309\u987a\u5e8f\u6267\u884c\uff09\u7684\u8bed\u53e5\u7684\u96c6\u5408\u3002\u5728\u6211\u4eec\u5b66\u4e60\u7f16\u7a0b\u7a0b\u5e8f\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u4f1a\u7f16\u5199\u81ea\u5df1\u7684\u51fd\u6570\uff0c\u6839\u636e\u6211\u4eec\u7684\u76ee\u7684\u6df7\u5408\u6216\u4f7f\u7528\u8bed\u53e5\uff08\u5177\u4f53\u505a\u6cd5\u5c06\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\uff09\u3002 \u6cd5\u5219 \u6bcf\u4e2a C++\u7a0b\u5e8f\u90fd\u5fc5\u987b\u5305\u542b\u4e00\u4e2a\u51fd\u6570\u540d\u4e3a main \u7684\u51fd\u6570(main \u5168\u90e8\u4e3a\u5c0f\u5199\u5b57\u6bcd)\u3002\u5f53\u7a0b\u5e8f\u5f00\u59cb\u8fd0\u884c\u7684\u65f6\u5019\uff0c main \u51fd\u6570\u4e2d\u7684\u4ee3\u7801\u4f1a\u88ab\u987a\u5e8f\u6267\u884c\u3002 \u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u901a\u5e38\u4f1a\u5728\u5b8c\u6210 main \u51fd\u6570\u4e2d\u6700\u540e\u4e00\u6761\u8bed\u53e5\u540e\u7ec8\u6b62\uff08\u5c3d\u7ba1\u4e5f\u53ef\u80fd\u63d0\u524d\u7ec8\u6b62\u6216\u8005\u5728\u7ed3\u675f\u540e\u6267\u884c\u4e00\u4e9b\u6e05\u7406\u64cd\u4f5c\uff09\u3002 \u7f16\u5199\u7684\u51fd\u6570\u4e00\u822c\u662f\u4e3a\u4e86\u5b8c\u6210\u67d0\u4e2a\u7279\u5b9a\u7684\u5de5\u4f5c\u3002\u4f8b\u5982\uff0c\u540d\u4e3a \"max\" \u7684\u51fd\u6570\u4e00\u822c\u7528\u4e8e\u4e24\u4e2a\u6570\u5b57\u6bd4\u5927\u5c0f\u3002\u800c\u540d\u4e3a \u201ccalculateGrade\u201d \u7684\u51fd\u6570\u5219\u7528\u4e8e\u8ba1\u7b97\u5b66\u751f\u6210\u7ee9\uff08grade\uff09\u3002\u6211\u4eec\u4f1a\u5728\u51fd\u6570\u4e00\u8282\u4e2d\u5bf9\u5176\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u6bd5\u7adf\u51fd\u6570\u662f\u6784\u5efa\u7a0b\u5e8f\u7684\u6700\u5e38\u7528\u7684\u5de5\u5177\u3002 \u4f5c\u8005\u6ce8 \u5f53\u6211\u4eec\u8c08\u8bba\u51fd\u6570\u65f6\uff0c\u901a\u5e38\u4f1a\u5728\u51fd\u6570\u540d\u540e\u9762\u52a0\u4e0a\u4e00\u7ec4\u62ec\u53f7\u4f5c\u4e3a\u4e00\u79cd\u901f\u8bb0\u6cd5\u3002\u4f8b\u5982\uff0c\u5f53\u4f60\u770b\u5230 main() \u6216\u8005 doSomething() \u7684\u65f6\u5019\uff0c\u4f60\u5e94\u8be5\u6e05\u695a\u6b64\u65f6\u6211\u4eec\u5728\u8c08\u8bba\u4e00\u4e2a\u540d\u4e3a main \u6216\u8005 doSomething \u7684\u51fd\u6570\u3002\u8fd9\u4e48\u505a\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5c06\u51fd\u6570\u548c\u5176\u4ed6\u7c7b\u578b\u7684\u540d\u79f0\u533a\u5206\u5f00\u6765\uff08\u4f8b\u5982\u53d8\u91cf\u540d\uff09\uff0c\u540c\u65f6\u4e0d\u5fc5\u6bcf\u6b21\u90fd\u52a0\u4e0a\u201c\u51fd\u6570\u201d\u4e8c\u5b57\u3002","title":"\u51fd\u6570\u548c main \u51fd\u6570"},{"location":"1-1-Statements-and-the-structure-of-a-program/#hello-world","text":"\u73b0\u5728\uff0c\u4f60\u5df2\u7ecf\u521d\u6b65\u4e86\u89e3\u4e86\u4ec0\u4e48\u662f\u8bed\u53e5\u4ee5\u53ca\u4ec0\u4e48\u662f\u51fd\u6570\uff0c\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u56de\u5230 \u201cHello world\u201d \u7a0b\u5e8f\u5e76\u9010\u884c\u5bf9\u5176\u8fdb\u884c\u5206\u6790\u3002 1 2 3 4 5 6 7 #include int main () { std :: cout << \"Hello world!\" ; return 0 ; } \u7b2c\u4e00\u884c \u6bd4\u8f83\u7279\u6b8a\uff0c\u79f0\u4e3a \u9884\u5904\u7406\u5668\u6307\u4ee4 (preprocessor directive) \u3002\u8be5\u6307\u4ee4\u8868\u660e\u7528\u6237\u9700\u8981\u4f7f\u7528 iostream \u5e93\u7684\u5185\u5bb9\uff0c\u8be5\u5e93\u5c5e\u4e8e C++\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u4f7f\u7528\u8be5\u5e93\u63d0\u4f9b\u7684\u529f\u80fd\u4ece\u63a7\u5236\u53f0(console)\u8bfb\u5199\u6587\u672c\u3002\u6211\u4eec\u9700\u8981\u8fd9\u4e00\u884c\u4ee3\u7801\uff0c\u662f\u4e3a\u4e86\u5728\u7b2c\u4e94\u884c\u4f7f\u7528 std:: cout \u3002\u5982\u679c\u5c06\u8fd9\u4e00\u884c\u5220\u9664\uff0c\u5219\u4f1a\u5bfc\u81f4\u7b2c\u4e94\u884c\u7f16\u8bd1\u65f6\u62a5\u9519\uff0c\u56e0\u4e3a\u8fd9\u6837 \u7f16\u8bd1\u5668(compiler) \u5c31\u4e0d\u77e5\u9053 std:: cout \u662f\u4ec0\u4e48\u4e86\u3002 \u7b2c\u4e8c\u884c \u662f\u7a7a\u884c\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c06\u5176\u5ffd\u7565\u3002\u5b83\u7684\u5b58\u5728\u4ec5\u4ec5\u662f\u4e3a\u4e86\u8ba9\u7a0b\u5e8f\u53ef\u8bfb\u6027\u53d8\u5f97\u66f4\u597d\uff08\u5c06 #include \u9884\u5904\u7406\u5668\u6307\u4ee4\u548c\u7a0b\u5e8f\u7684\u5176\u4ed6\u90e8\u5206\u5206\u5272\u5f00\u6765\uff09\u3002 \u7b2c\u4e09\u884c \u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u6b64\u5904\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a main \u7684\u51fd\u6570\u3002\u4e4b\u524d\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u5230\uff0c\u6bcf\u4e2a C++\u7a0b\u5e8f\u90fd\u5fc5\u987b\u6709\u4e00\u4e2a main \u51fd\u6570\uff0c\u5426\u5219\u5c06\u4f1a \u94fe\u63a5(link) \u5931\u8d25\u3002 \u7b2c\u56db\u884c \u548c \u7b2c\u4e03\u884c \u544a\u8bc9\u7f16\u8bd1\u5668\u63a5\u4e0b\u6765\u54ea\u4e9b\u884c\u4ee3\u7801\u5c5e\u4e8e main \u51fd\u6570\u3002\u6240\u6709\u4f4d\u4e8e\u7b2c\u56db\u884c\u548c\u7b2c\u4e03\u884c\u62ec\u53f7\u5185\u7684\u4ee3\u7801\uff0c\u90fd\u5c5e\u4e8e main \u51fd\u6570\u7684\u4e00\u90e8\u5206\uff0c\u79f0\u4e3a\u51fd\u6570\u4f53\u3002 \u7b2c\u4e94\u884c \u662f main \u51fd\u6570\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\uff0c\u8be5\u8bed\u53e5\u4e5f\u662f\u7a0b\u5e8f\u6267\u884c\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u3002 std:: cout \uff08\u8868\u793a\u201ccharacter output\u201d\uff0c\u5373\u5b57\u7b26\u8f93\u51fa\uff09\u548c << \u8fd0\u7b97\u7b26\u53ef\u4ee5\u5c06\u5b57\u6bcd\u6216\u6570\u5b57\u4ece\u63a7\u5236\u53f0\u8f93\u51fa\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u5b57\u7b26\u4e32\u6587\u672c \u201cHello world!\u201d \u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002\u8fd9\u884c\u4ee3\u7801\u7684\u6267\u884c\u6548\u679c\u5c31\u662f\u4e3a\u7a0b\u5e8f\u521b\u5efa\u4e00\u4e2a\u53ef\u89c6\u5316\u7684\u8f93\u51fa\u7ed3\u679c\u3002 \u7b2c\u516d\u884c \u662f\u4e00\u4e2a\u8fd4\u56de\u8bed\u53e5\u3002\u5f53\u7a0b\u5e8f\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u5b83\u9700\u8981\u5411\u64cd\u4f5c\u7cfb\u7edf\u8fd4\u56de\u4e00\u4e2a\u503c\u6765\u8868\u660e\u5176\u6267\u884c\u7684\u7ed3\u679c\u662f\u6210\u529f\u8fd8\u662f\u5931\u8d25\u3002\u8be5\u8fd4\u56de\u8bed\u53e5\u5411\u64cd\u4f5c\u7cfb\u7edf\u8fd4\u56de\u4e86 0\uff0c\u8868\u793a\"\u4e00\u5207\u6b63\u5e38\"\u3002\u8fd9\u662f\u672c\u7a0b\u5e8f\u6267\u884c\u7684\u6700\u540e\u4e00\u6761\u8bed\u53e5\u3002 \u6211\u4eec\u7f16\u5199\u7684\u6240\u6709\u7a0b\u5e8f\uff0c\u90fd\u4f1a\u9075\u5faa\u4e0a\u8ff0\u901a\u7528\u6a21\u677f\uff08\u6216\u5176\u53d8\u79cd\u7248\u672c\uff09\u3002 \u4f5c\u8005\u6ce8 \u5982\u679c\u4e0a\u8ff0\u89e3\u91ca\u4ecd\u7136\u6709\u8ba9\u4f60\u56f0\u60d1\u7684\u5730\u65b9\uff0c\u522b\u62c5\u5fc3\uff0c\u8fd9\u5f88\u6b63\u5e38\u3002\u8fd9\u91cc\u7684\u76ee\u7684\u53ea\u662f\u8ba9\u4f60\u5bf9\u7a0b\u5e8f\u6709\u4e00\u4e2a\u5b8f\u89c2\u7684\u6982\u5ff5\uff0c\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u5c06\u4f1a\u5bf9\u76f8\u5173\u7684\u5185\u5bb9\u8fdb\u884c\u8be6\u7ec6\u89e3\u91ca\u5e76\u8f85\u4ee5\u6848\u4f8b\u3002 \u4f60\u53ef\u4ee5\u81ea\u884c\u7f16\u8bd1\u5e76\u8fd0\u884c\u4e0a\u8ff0\u7a0b\u5e8f\uff0c\u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u4f60\u5c06\u5728\u63a7\u5236\u53f0\u4e2d\u770b\u5230\u5982\u4e0b\u6253\u5370\u4fe1\u606f\uff1a 1 Hello world! \u5982\u679c\u4f60\u5728\u7f16\u8bd1\u6216\u8fd0\u884c\u7a0b\u5e8f\u65f6\u9047\u5230\u4e86\u95ee\u9898\uff0c\u8bf7\u53c2\u8003 \u4e00\u4e9b\u5178\u578b\u7684 C++ \u95ee\u9898","title":"Hello world! \u7a0b\u5e8f\u5256\u6790"},{"location":"1-1-Statements-and-the-structure-of-a-program/#_3","text":"\u82f1\u8bed\u4e2d\u7684\u53e5\u5b50\u57fa\u4e8e\u4f60\u6240\u5b66\u7684\u8bed\u6cd5\u89c4\u5219(grammatical rules)\u6765\u6784\u5efa\u7684\u3002\u4f8b\u5982\uff0c\u901a\u5e38\u4e00\u4e2a\u53e5\u5b50\u4f1a\u4ee5\u53e5\u53f7\u7ed3\u5c3e\u3002\u5728\u4e00\u95e8\u8bed\u8a00\u4e2d\uff0c\u89c4\u5b9a\u4e00\u4e2a\u53e5\u5b50\u5e94\u8be5\u5982\u4f55\u6784\u5efa\u7684\u89c4\u5219\uff0c\u79f0\u4e3a \u8bed\u6cd5 \uff08\u8bd1\u6ce8\uff1a\u53e5\u6cd5\u5728\u540e\u7eed\u7684\u7ffb\u8bd1\u4e2d\u5c06\u4f7f\u7528\u8bed\u6cd5\u4ee3\u66ff\uff0c\u89c1 \u7ffb\u8bd1\u7ea6\u5b9a \uff09\u3002\u5982\u679c\u4f60\u5fd8\u8bb0\u4e86\u5728\u4e24\u4e2a\u53e5\u5b50\u4e4b\u95f4\u5199\u4e0b\u53e5\u53f7\uff0c\u90a3\u4e48\u5c31\u4f1a\u8fdd\u53cd\u4e86\u82f1\u8bed\u7684\u53e5\u6cd5\u3002 C++ \u4e5f\u6709\u5b83\u7684 \u8bed\u6cd5 \u89c4\u5219\uff0c\u5b83\u89c4\u5b9a\u4e86\u4e00\u4e2a\u7a0b\u5e8f\u5e94\u8be5\u5982\u4f55\u88ab\u7f16\u5199\u624d\u80fd\u88ab\u8ba4\u4e3a\u662f\u5408\u6cd5\u7684\u3002\u5728\u7f16\u8bd1\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u7f16\u8bd1\u5668\u9700\u8981\u786e\u4fdd\u4f60\u7684\u7a0b\u5e8f\u6ee1\u8db3\u57fa\u672c\u7684 C++\u8bed\u6cd5\u3002\u5982\u679c\u4f60\u8fdd\u53cd\u4e86\u67d0\u6761\u89c4\u5219\uff0c\u7f16\u8bd1\u5668\u5c31\u4f1a\u5411\u4f60\u62a5\u544a\u4e00\u4e2a\u9519\u8bef\u3002 \u5982\u679c\uff0c\u6211\u4eec\u5c06\u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\u7b2c\u4e94\u884c\u7684\u5206\u53f7\u7701\u7565\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f 1 2 3 4 5 6 7 #include int main () { std :: cout << \"Hello world!\" return 0 ; } \u4f60\u53ef\u4ee5\u8bd5\u8bd5\u53bb\u7f16\u8bd1\u4e00\u4e0b\u8be5\u7a0b\u5e8f\u3002 Visual Studio \u5728\u7f16\u8bd1\u65f6\u4f1a\u62a5\u544a\u5982\u4e0b\u9519\u8bef (\u4f60\u7684\u7f16\u8bd1\u5668\u5728\u8868\u8ff0\u8be5\u9519\u8bef\u65f6\u53ef\u80fd\u4f1a\u4f7f\u7528\u4e0d\u540c\u7684\u8bed\u8a00)\uff1a 1 c:\\vcprojects\\test1.cpp(6): error C2143: syntax error : missing ';' before 'return' \u8be5\u9519\u8bef\u4fe1\u606f\u662f\u544a\u8bc9\u4f60\uff0c\u9519\u8bef\u53d1\u751f\u5728\u7b2c\u516d\u884c\uff1a\u7a0b\u5e8f\u671f\u671b\u5728 return \u8bed\u53e5\u524d\u9762\u68c0\u6d4b\u5230\u4e00\u4e2a\u5206\u53f7\uff0c\u4f46\u5b83\u5374\u6ca1\u6709\u3002\u5c3d\u7ba1\u7f16\u8bd1\u5668\u544a\u8bc9\u4e86\u6211\u4eec\u8be5\u8bed\u6cd5\u95ee\u9898\u53d1\u751f\u7684\u884c\u53f7\uff0c\u5176\u5b9e\u9645\u53d1\u751f\u7684\u4f4d\u7f6e\u5374\u5728\u4e0a\u4e00\u884c\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u95ee\u9898\u51fa\u5728 \u7b2c\u4e94\u884c \u7684\u672b\u5c3e\uff08\u7f16\u8bd1\u5668\u5728\u8bfb\u53d6\u5230\u7b2c\u516d\u884c\u65f6\u624d\u80fd\u53d1\u73b0\u8be5\u95ee\u9898\uff09\u3002 \u8bed\u6cd5\u9519\u8bef\u5728\u7f16\u7a0b\u65f6\u5f88\u5e38\u89c1\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u8fd9\u79cd\u7c7b\u578b\u7684\u9519\u8bef\u901a\u5e38\u6bd4\u8f83\u5bb9\u6613\u5b9a\u4f4d\u548c\u4fee\u590d\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u4f1a\u660e\u786e\u5730\u6307\u5411\u9519\u8bef\u53d1\u751f\u7684\u4f4d\u7f6e\u3002\u7a0b\u5e8f\u7684\u7f16\u8bd1\u53ea\u6709\u5728\u6240\u6709\u7684\u9519\u8bef\u90fd\u88ab\u4fee\u590d\u540e\u624d\u80fd\u591f\u5b8c\u6210\u3002 \u4f60\u53ef\u4ee5\u8bd5\u7740\u4ece\u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\u5220\u9664\u67d0\u4e9b\u5b57\u7b26\uff0c\u751a\u81f3\u5220\u9664\u6574\u884c\uff0c\u7136\u540e\u89c2\u5bdf\u7f16\u8bd1\u5668\u7684\u62a5\u9519\u4fe1\u606f\u3002\u73b0\u5728\uff0c\u5c06\u5206\u53f7\u5199\u5728\u7b2c\u4e94\u884c\u7684\u672b\u5c3e\u5e76\u5220\u9664 1\u30013 \u6216\u8005 4 \u884c\uff0c\u770b\u770b\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f","title":"\u8bed\u6cd5\u548c\u8bed\u6cd5\u9519\u8bef"},{"location":"1-10-Introduction-to-expressions/","tags":["expression"],"text":"\u8868\u8fbe\u5f0f \u8003\u8651\u4ee5\u4e0b\u8bed\u53e5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // five() is a function that returns the value 5 int five () { return 5 ; } int main () { int a { 2 }; // initialize variable a with literal value 2 int b { 2 + 3 }; // initialize variable b with computed value 5 int c { ( 2 * 3 ) + 4 }; // initialize variable c with computed value 10 int d { b }; // initialize variable d with variable value 5 int e { five () }; // initialize variable e with function return value 5 return 0 ; } \u4e0a\u8ff0\u5404\u8bed\u53e5\u90fd\u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\u3002\u6ce8\u610f\u4e0a\u8ff0\u53d8\u91cf\u7684\u521d\u59cb\u5316\u503c\u7684\u6784\u5efa\uff0c\u4f7f\u7528\u4e86\u4e0d\u540c\u7684\u5143\u7d20\uff1a\u5b57\u9762\u91cf\u3001\u53d8\u91cf\u3001\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u3002\u7136\u800c\uff0cC++\u4f1a\u5c06\u4e0a\u8ff0\u6240\u6709\u7684\u5f62\u5f0f\uff0c\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5355\u503c\uff0c\u5e76\u7528\u8be5\u503c\u6765\u5b8c\u6210\u53d8\u91cf\u7684\u521d\u59cb\u5316\u3002 \u4e0a\u8ff0\u521d\u59cb\u5316\u503c\u4e4b\u95f4\u6709\u4ec0\u4e48\u5171\u540c\u70b9\u5462\uff1f\u5b83\u4eec\u90fd\u5229\u7528\u4e86\u8868\u8fbe\u5f0f\u3002 \u8868\u8fbe\u5f0f \u662f\u5b57\u9762\u91cf\u3001\u53d8\u91cf\u3001\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u8c03\u7528\u7684\u7ed3\u5408\u4f53\u4e14\u6700\u7ec8\u53ef\u4ee5\u4ea7\u751f\u4e00\u4e2a\u5355\u503c\u3002\u6267\u884c\u8868\u8fbe\u5f0f\u7684\u8fc7\u7a0b\u79f0\u4e3a\u6c42\u503c\uff0c\u6700\u7ec8\u751f\u6210\u7684\u503c\u5219\u79f0\u4e3a\u8868\u8fbe\u5f0f\u7684 \u7ed3\u679c \u3002 \u5f53\u8868\u8fbe\u5f0f\u5b8c\u6210\u6c42\u503c\u540e\uff0c\u8868\u8fbe\u5f0f\u4e2d\u7684\u6bcf\u4e00\u9879\uff08term\uff09\u4e5f\u90fd\u5b8c\u6210\u4e86\u6c42\u503c\uff0c\u76f4\u5230\u5f97\u5230\u4e00\u4e2a\u6700\u7ec8\u7684\u5355\u503c\u7ed3\u679c\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u8868\u8fbe\u5f0f\u7684\u4f8b\u5b50\uff0c\u8868\u8fbe\u5f0f\u540e\u9762\u7684\u6ce8\u91ca\u89e3\u91ca\u4e86\u5b83\u7684\u6c42\u503c\u65b9\u6cd5\u3002 1 2 3 4 5 6 7 2 // 2 \u662f\u4e00\u4e2a\u5b57\u9762\u91cf\uff0c\u6c42\u503c\u7ed3\u679c\u4e3a 2 \"Hello world!\" // \"Hello world!\" \u662f\u4e00\u4e2a\u5b57\u9762\u91cf\uff0c\u6c42\u503c\u7ed3\u679c\u4e3a\u5b57\u7b26\u4e32 \"Hello world!\" x // x \u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u6c42\u503c\u7ed3\u679c\u5373 x \u7684\u503c 2 + 3 // \u8fd0\u7b97\u7b26 + \u5c06 2 \u548c 3 \u76f8\u52a0\u5f97\u5230 5 x = 2 + 3 // 2 + 3 \u6c42\u503c\u5f97\u5230 5\uff0c\u7136\u540e\u8d4b\u503c\u7ed9\u53d8\u91cf x std :: cout << x // x \u6c42\u503c\u5f97\u5230 x \u7684\u503c\uff0c\u7136\u540e\u5c06\u5176\u6253\u5370\u5230\u63a7\u5236\u53f0\u754c\u9762\u4e0a five () // \u5bf9\u51fd\u6570 five() \u7684\u8fd4\u56de\u503c\u6c42\u503c \u6b63\u5982\u4f60\u770b\u5230\u7684\u90a3\u6837\uff0c\u5b57\u9762\u91cf\u6c42\u503c\u7684\u7ed3\u679c\u5c31\u662f\u5176\u672c\u8eab\u3002\u53d8\u91cf\u6c42\u503c\u7684\u7ed3\u679c\uff0c\u662f\u53d8\u91cf\u7684\u503c\u3002\u867d\u7136\u51fd\u6570\u8c03\u7528\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\uff0c\u4f46\u662f\u5728\u8868\u8fbe\u5f0f\u7684\u8bed\u5883\u4e2d\uff0c\u51fd\u6570\u8c03\u7528\u7684\u6c42\u503c\u7ed3\u679c\u662f\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002\u8fd0\u7b97\u7b26\u7684\u6c42\u503c\u5219\u5141\u8bb8\u6211\u4eec\u5408\u5e76\u591a\u4e2a\u503c\u5e76\u6700\u7ec8\u5f97\u5230\u4e00\u4e2a\u65b0\u503c\u3002 \u6ce8\u610f\uff0c\u8868\u8fbe\u5f0f\u5e76\u4e0d\u4ee5\u5206\u53f7\u7ed3\u5c3e\uff0c\u800c\u4e14\u4e5f\u4e0d\u80fd\u4f5c\u4e3a\u5355\u72ec\u7684\u5143\u7d20\u8fdb\u884c\u7f16\u8bd1\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u8981\u7f16\u8bd1 x = 5 \uff0c\u4f60\u7684\u7f16\u8bd1\u5668\u5c06\u4f1a\u4fdd\u5b58\uff08\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4f1a\u63d0\u793a\u4f60\u7f3a\u5c11\u5206\u53f7\uff09\u3002\u5b9e\u9645\u4e0a\uff0c\u8868\u8fbe\u5f0f\u603b\u662f\u4f5c\u4e3a\u8bed\u53e5\u7684\u4e00\u90e8\u5206\u8fdb\u884c\u6c42\u503c\u7684\u3002 \u4f8b\u5982\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u6761\u8bed\u53e5\uff1a 1 int x { 2 + 3 }; // 2 + 3 \u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\u5b83\u4e0d\u4ee5\u5206\u53f7\u7ed3\u5c3e -- \u5206\u53f7\u5e94\u8be5\u4f4d\u4e8e\u5305\u542b\u8be5\u8868\u8fbe\u5f0f\u7684\u8bed\u53e5\u7684\u672b\u5c3e \u5982\u679c\u4f60\u60f3\u8981\u6df1\u7a76\u8be5\u8868\u8fbe\u5f0f\u7684\u8bed\u6cd5\uff0c\u5b83\u5e94\u8be5\u662f\u5982\u4e0b\u8fd9\u79cd\u5f62\u5f0f\uff1a 1 type identifier { expression }; Type \u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u7684\u7c7b\u578b\uff08\u8fd9\u91cc\u4f7f\u7528\u4e86 int \uff09\uff0c\u6807\u8bc6\u7b26\u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u7684\u53d8\u91cf\u540d\uff08\u8fd9\u91cc\u4f7f\u7528\u4e86 x \uff09\u3002\u8868\u8fbe\u5f0f\u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u7684\u8868\u8fbe\u5f0f\uff08\u8fd9\u91cc\u4f7f\u7528\u4e86 2+3\uff0c\u5373\u4e24\u4e2a\u5b57\u9762\u91cf\u548c\u4e00\u4e2a\u8fd0\u7b97\u7b26\uff09\u3002 \u5173\u952e\u4fe1\u606f \u5728 C++ \u4e2d\uff0c\u4efb\u4f55\u5141\u8bb8\u4f60\u4f7f\u7528\u4e00\u4e2a\u5355\u503c\u7684\u5730\u65b9\uff0c\u4f60\u90fd\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u8868\u8fbe\u5f0f\u6765\u66ff\u4ee3\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u6700\u7ec8\u4f1a\u6c42\u503c\u5f97\u5230\u4e00\u4e2a\u5355\u503c\u3002 \u8868\u8fbe\u5f0f\u8bed\u53e5 \u6709\u4e9b\u8868\u8fbe\u5f0f\u672c\u8eab\u662f\u6709\u5176\u7279\u5b9a\u7528\u9014\u7684\uff08\u4f8b\u5982 x = 5 )\u3002\u4f46\u662f\uff0c\u524d\u9762\u6211\u4eec\u4e5f\u8bf4\u8fc7\uff0c\u8868\u8fbe\u5f0f\u5fc5\u987b\u662f\u8bed\u53e5\u7684\u4e00\u90e8\u5206\uff0c\u90a3\u4e48\u5982\u4f55\u624d\u80fd\u5355\u72ec\u5730\u4f7f\u7528\u8fd9\u4e9b\u7279\u5b9a\u7684\u8868\u8fbe\u5f0f\u5462\uff1f \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8868\u8fbe\u5f0f\u8f6c\u6362\u4e3a\u4e00\u4e2a\u7b49\u4ef7\u7684\u8bed\u53e5\uff08\u79f0\u4e3a\u8868\u8fbe\u5f0f\u8bed\u53e5\uff09\u3002 \u8868\u8fbe\u5f0f\u8bed\u53e5 \u5305\u542b\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u7136\u540e\u7d27\u63a5\u7740\u4e00\u4e2a\u5206\u53f7\u3002\u5f53\u8be5\u8bed\u53e5\u6267\u884c\u65f6\uff0c\u8868\u8fbe\u5f0f\u5c31\u4f1a\u88ab\u6c42\u503c\uff08\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u4f1a\u88ab\u4e22\u5f03\uff09\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4efb\u610f\u8868\u8fbe\u5f0f\uff08\u4f8b\u5982 x = 5 \uff09\u8f6c\u6362\u6210\u7b49\u4ef7\u7684\u8868\u8fbe\u5f0f\u8bed\u53e5\uff08\u4f8b\u5982\uff0c x = 5; \uff09\u5e76\u7f16\u8bd1\u3002 \u6ce8\u610f\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u8bd1\u8868\u8fbe\u5f0f\u8bed\u53e5\uff0c\u4f46\u8fd9\u6837\u505a\u6709\u65f6\u662f\u6ca1\u6709\u610f\u4e49\u7684\uff08\u4f8b\u5982 2 * 3; \uff09\uff0c\u5b83\u7684\u6c42\u503c\u7ed3\u679c\u4e3a 6\uff0c\u7136\u540e 6 \u5c31\u88ab\u4e22\u5f03\u4e86\u3002 \u6cd5\u5219 \u8868\u8fbe\u5f0f\u4e2d\u8ba1\u7b97\u5f97\u5230\u7684\u503c\u4f1a\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u540e\u88ab\u4e22\u5f03\u3002","title":"1.10 - \u8868\u8fbe\u5f0f\u7b80\u4ecb"},{"location":"1-10-Introduction-to-expressions/#_1","text":"\u8003\u8651\u4ee5\u4e0b\u8bed\u53e5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // five() is a function that returns the value 5 int five () { return 5 ; } int main () { int a { 2 }; // initialize variable a with literal value 2 int b { 2 + 3 }; // initialize variable b with computed value 5 int c { ( 2 * 3 ) + 4 }; // initialize variable c with computed value 10 int d { b }; // initialize variable d with variable value 5 int e { five () }; // initialize variable e with function return value 5 return 0 ; } \u4e0a\u8ff0\u5404\u8bed\u53e5\u90fd\u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\u3002\u6ce8\u610f\u4e0a\u8ff0\u53d8\u91cf\u7684\u521d\u59cb\u5316\u503c\u7684\u6784\u5efa\uff0c\u4f7f\u7528\u4e86\u4e0d\u540c\u7684\u5143\u7d20\uff1a\u5b57\u9762\u91cf\u3001\u53d8\u91cf\u3001\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u3002\u7136\u800c\uff0cC++\u4f1a\u5c06\u4e0a\u8ff0\u6240\u6709\u7684\u5f62\u5f0f\uff0c\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5355\u503c\uff0c\u5e76\u7528\u8be5\u503c\u6765\u5b8c\u6210\u53d8\u91cf\u7684\u521d\u59cb\u5316\u3002 \u4e0a\u8ff0\u521d\u59cb\u5316\u503c\u4e4b\u95f4\u6709\u4ec0\u4e48\u5171\u540c\u70b9\u5462\uff1f\u5b83\u4eec\u90fd\u5229\u7528\u4e86\u8868\u8fbe\u5f0f\u3002 \u8868\u8fbe\u5f0f \u662f\u5b57\u9762\u91cf\u3001\u53d8\u91cf\u3001\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u8c03\u7528\u7684\u7ed3\u5408\u4f53\u4e14\u6700\u7ec8\u53ef\u4ee5\u4ea7\u751f\u4e00\u4e2a\u5355\u503c\u3002\u6267\u884c\u8868\u8fbe\u5f0f\u7684\u8fc7\u7a0b\u79f0\u4e3a\u6c42\u503c\uff0c\u6700\u7ec8\u751f\u6210\u7684\u503c\u5219\u79f0\u4e3a\u8868\u8fbe\u5f0f\u7684 \u7ed3\u679c \u3002 \u5f53\u8868\u8fbe\u5f0f\u5b8c\u6210\u6c42\u503c\u540e\uff0c\u8868\u8fbe\u5f0f\u4e2d\u7684\u6bcf\u4e00\u9879\uff08term\uff09\u4e5f\u90fd\u5b8c\u6210\u4e86\u6c42\u503c\uff0c\u76f4\u5230\u5f97\u5230\u4e00\u4e2a\u6700\u7ec8\u7684\u5355\u503c\u7ed3\u679c\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u8868\u8fbe\u5f0f\u7684\u4f8b\u5b50\uff0c\u8868\u8fbe\u5f0f\u540e\u9762\u7684\u6ce8\u91ca\u89e3\u91ca\u4e86\u5b83\u7684\u6c42\u503c\u65b9\u6cd5\u3002 1 2 3 4 5 6 7 2 // 2 \u662f\u4e00\u4e2a\u5b57\u9762\u91cf\uff0c\u6c42\u503c\u7ed3\u679c\u4e3a 2 \"Hello world!\" // \"Hello world!\" \u662f\u4e00\u4e2a\u5b57\u9762\u91cf\uff0c\u6c42\u503c\u7ed3\u679c\u4e3a\u5b57\u7b26\u4e32 \"Hello world!\" x // x \u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u6c42\u503c\u7ed3\u679c\u5373 x \u7684\u503c 2 + 3 // \u8fd0\u7b97\u7b26 + \u5c06 2 \u548c 3 \u76f8\u52a0\u5f97\u5230 5 x = 2 + 3 // 2 + 3 \u6c42\u503c\u5f97\u5230 5\uff0c\u7136\u540e\u8d4b\u503c\u7ed9\u53d8\u91cf x std :: cout << x // x \u6c42\u503c\u5f97\u5230 x \u7684\u503c\uff0c\u7136\u540e\u5c06\u5176\u6253\u5370\u5230\u63a7\u5236\u53f0\u754c\u9762\u4e0a five () // \u5bf9\u51fd\u6570 five() \u7684\u8fd4\u56de\u503c\u6c42\u503c \u6b63\u5982\u4f60\u770b\u5230\u7684\u90a3\u6837\uff0c\u5b57\u9762\u91cf\u6c42\u503c\u7684\u7ed3\u679c\u5c31\u662f\u5176\u672c\u8eab\u3002\u53d8\u91cf\u6c42\u503c\u7684\u7ed3\u679c\uff0c\u662f\u53d8\u91cf\u7684\u503c\u3002\u867d\u7136\u51fd\u6570\u8c03\u7528\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\uff0c\u4f46\u662f\u5728\u8868\u8fbe\u5f0f\u7684\u8bed\u5883\u4e2d\uff0c\u51fd\u6570\u8c03\u7528\u7684\u6c42\u503c\u7ed3\u679c\u662f\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002\u8fd0\u7b97\u7b26\u7684\u6c42\u503c\u5219\u5141\u8bb8\u6211\u4eec\u5408\u5e76\u591a\u4e2a\u503c\u5e76\u6700\u7ec8\u5f97\u5230\u4e00\u4e2a\u65b0\u503c\u3002 \u6ce8\u610f\uff0c\u8868\u8fbe\u5f0f\u5e76\u4e0d\u4ee5\u5206\u53f7\u7ed3\u5c3e\uff0c\u800c\u4e14\u4e5f\u4e0d\u80fd\u4f5c\u4e3a\u5355\u72ec\u7684\u5143\u7d20\u8fdb\u884c\u7f16\u8bd1\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u8981\u7f16\u8bd1 x = 5 \uff0c\u4f60\u7684\u7f16\u8bd1\u5668\u5c06\u4f1a\u4fdd\u5b58\uff08\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4f1a\u63d0\u793a\u4f60\u7f3a\u5c11\u5206\u53f7\uff09\u3002\u5b9e\u9645\u4e0a\uff0c\u8868\u8fbe\u5f0f\u603b\u662f\u4f5c\u4e3a\u8bed\u53e5\u7684\u4e00\u90e8\u5206\u8fdb\u884c\u6c42\u503c\u7684\u3002 \u4f8b\u5982\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u6761\u8bed\u53e5\uff1a 1 int x { 2 + 3 }; // 2 + 3 \u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\u5b83\u4e0d\u4ee5\u5206\u53f7\u7ed3\u5c3e -- \u5206\u53f7\u5e94\u8be5\u4f4d\u4e8e\u5305\u542b\u8be5\u8868\u8fbe\u5f0f\u7684\u8bed\u53e5\u7684\u672b\u5c3e \u5982\u679c\u4f60\u60f3\u8981\u6df1\u7a76\u8be5\u8868\u8fbe\u5f0f\u7684\u8bed\u6cd5\uff0c\u5b83\u5e94\u8be5\u662f\u5982\u4e0b\u8fd9\u79cd\u5f62\u5f0f\uff1a 1 type identifier { expression }; Type \u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u7684\u7c7b\u578b\uff08\u8fd9\u91cc\u4f7f\u7528\u4e86 int \uff09\uff0c\u6807\u8bc6\u7b26\u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u7684\u53d8\u91cf\u540d\uff08\u8fd9\u91cc\u4f7f\u7528\u4e86 x \uff09\u3002\u8868\u8fbe\u5f0f\u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u7684\u8868\u8fbe\u5f0f\uff08\u8fd9\u91cc\u4f7f\u7528\u4e86 2+3\uff0c\u5373\u4e24\u4e2a\u5b57\u9762\u91cf\u548c\u4e00\u4e2a\u8fd0\u7b97\u7b26\uff09\u3002 \u5173\u952e\u4fe1\u606f \u5728 C++ \u4e2d\uff0c\u4efb\u4f55\u5141\u8bb8\u4f60\u4f7f\u7528\u4e00\u4e2a\u5355\u503c\u7684\u5730\u65b9\uff0c\u4f60\u90fd\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u8868\u8fbe\u5f0f\u6765\u66ff\u4ee3\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u6700\u7ec8\u4f1a\u6c42\u503c\u5f97\u5230\u4e00\u4e2a\u5355\u503c\u3002","title":"\u8868\u8fbe\u5f0f"},{"location":"1-10-Introduction-to-expressions/#_2","text":"\u6709\u4e9b\u8868\u8fbe\u5f0f\u672c\u8eab\u662f\u6709\u5176\u7279\u5b9a\u7528\u9014\u7684\uff08\u4f8b\u5982 x = 5 )\u3002\u4f46\u662f\uff0c\u524d\u9762\u6211\u4eec\u4e5f\u8bf4\u8fc7\uff0c\u8868\u8fbe\u5f0f\u5fc5\u987b\u662f\u8bed\u53e5\u7684\u4e00\u90e8\u5206\uff0c\u90a3\u4e48\u5982\u4f55\u624d\u80fd\u5355\u72ec\u5730\u4f7f\u7528\u8fd9\u4e9b\u7279\u5b9a\u7684\u8868\u8fbe\u5f0f\u5462\uff1f \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8868\u8fbe\u5f0f\u8f6c\u6362\u4e3a\u4e00\u4e2a\u7b49\u4ef7\u7684\u8bed\u53e5\uff08\u79f0\u4e3a\u8868\u8fbe\u5f0f\u8bed\u53e5\uff09\u3002 \u8868\u8fbe\u5f0f\u8bed\u53e5 \u5305\u542b\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u7136\u540e\u7d27\u63a5\u7740\u4e00\u4e2a\u5206\u53f7\u3002\u5f53\u8be5\u8bed\u53e5\u6267\u884c\u65f6\uff0c\u8868\u8fbe\u5f0f\u5c31\u4f1a\u88ab\u6c42\u503c\uff08\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u4f1a\u88ab\u4e22\u5f03\uff09\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4efb\u610f\u8868\u8fbe\u5f0f\uff08\u4f8b\u5982 x = 5 \uff09\u8f6c\u6362\u6210\u7b49\u4ef7\u7684\u8868\u8fbe\u5f0f\u8bed\u53e5\uff08\u4f8b\u5982\uff0c x = 5; \uff09\u5e76\u7f16\u8bd1\u3002 \u6ce8\u610f\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u8bd1\u8868\u8fbe\u5f0f\u8bed\u53e5\uff0c\u4f46\u8fd9\u6837\u505a\u6709\u65f6\u662f\u6ca1\u6709\u610f\u4e49\u7684\uff08\u4f8b\u5982 2 * 3; \uff09\uff0c\u5b83\u7684\u6c42\u503c\u7ed3\u679c\u4e3a 6\uff0c\u7136\u540e 6 \u5c31\u88ab\u4e22\u5f03\u4e86\u3002 \u6cd5\u5219 \u8868\u8fbe\u5f0f\u4e2d\u8ba1\u7b97\u5f97\u5230\u7684\u503c\u4f1a\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u540e\u88ab\u4e22\u5f03\u3002","title":"\u8868\u8fbe\u5f0f\u8bed\u53e5"},{"location":"1-11-Developing-your-first-program/","text":"\u524d\u9762\u7684\u8bfe\u7a0b\u5df2\u7ecf\u4ecb\u7ecd\u4e86\u4e0d\u5c11\u672f\u8bed\u548c\u6982\u5ff5\uff0c\u8fd9\u4e9b\u672f\u8bed\u548c\u6982\u5ff5\u51e0\u4e4e\u4f1a\u5728\u6211\u4eec\u7f16\u5199\u7684\u4efb\u4f55\u7a0b\u5e8f\u4e2d\u51fa\u73b0\u3002\u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u8fd0\u7528\u6240\u5b66\u7684\u5185\u5bb9\uff0c\u52a8\u624b\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\u3002 \u5c0f\u7a0b\u5e8f\uff1a\u8f93\u5165\u7684\u6570\u5b57\u4e58\u4ee5 2 \u9996\u5148\uff0c\u6211\u4eec\u4f1a\u8981\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6574\u578b\u6570\u5e76\u7b49\u5f85\u7528\u6237\u5b8c\u6210\u8f93\u5165\uff0c\u7136\u540e\u6211\u4eec\u4f1a\u5c06\u8be5\u503c\u4e58\u4ee5 2 \u5e76\u544a\u8bc9\u7528\u6237\u8ba1\u7b97\u7ed3\u679c\u3002\u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff08\u5047\u8bbe\u8f93\u5165\u503c\u4e3a 4\uff09\uff1a 1 2 Enter an integer: 4 Double that number is: 8 \u4e0b\u9762\u8ba9\u6211\u4eec\u6765 \u9010\u6b65 \u5b8c\u6210\u4e0a\u8ff0\u529f\u80fd\uff1a \u6700\u4f73\u5b9e\u8df5 \u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u4f1a\u53bb\u5c1d\u8bd5\u4e00\u6b21\u6027\u5b8c\u6210\u6574\u4e2a\u7a0b\u5e8f\u7684\u7f16\u5199\uff0c\u7136\u540e\u4fbf\u4f1a\u56e0\u4e3a\u5927\u91cf\u62a5\u9519\u4fe1\u606f\u800c\u611f\u5230\u65e0\u6240\u9002\u4ece\u3002\u6bd4\u8f83\u597d\u7684\u65b9\u5f0f\u662f\u6bcf\u6b21\u6dfb\u52a0\u4e00\u90e8\u5206\u5185\u5bb9\u5e76\u4fdd\u8bc1\u5176\u80fd\u591f\u901a\u8fc7\u7f16\u8bd1\u548c\u901a\u8fc7\u6d4b\u8bd5\u3002\u5f53\u4f60\u80fd\u591f\u786e\u4fdd\u5176\u6b63\u786e\u5de5\u4f5c\u540e\uff0c\u518d\u7f16\u5199\u63a5\u4e0b\u6765\u7684\u4ee3\u7801\u3002 \u672c\u8282\u8bfe\u4e5f\u4f1a\u91c7\u53d6\u4e0a\u8ff0\u7b56\u7565\u3002\u6211\u4eec\u4f1a\u5c06\u7a0b\u5e8f\u7684\u5b9e\u73b0\u5206\u4e3a\u591a\u4e2a\u6b65\u9aa4\uff0c\u8bf7\u5c06\u5404\u6b65\u9aa4\u4e2d\u7684\u4ee3\u7801 \u8f93\u5165 \uff08\u4e0d\u8981\u590d\u5236\u7c98\u8d34\uff09\u8ba1\u7b97\u673a\uff0c\u7f16\u8bd1\u5e76\u4e14\u8fd0\u884c\u3002 \u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u63a7\u5236\u53f0\u7a0b\u5e8f\u9879\u76ee\u3002 \u73b0\u5728\uff0c\u7f16\u5199\u4e00\u4e2a\u57fa\u672c\u7684\u7a0b\u5e8f\u6846\u67b6\u3002\u6211\u4eec\u77e5\u9053\uff0c\u7f16\u5199\u4e00\u4e2a main() \u7a0b\u5e8f\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u6b65\u9aa4\uff08\u56e0\u4e3a C++\u7a0b\u5e8f\u5fc5\u987b\u5177\u6709\u4e00\u4e2a main \u51fd\u6570\uff09\u3002\u5982\u679c\u4f60\u7684 IDE \u6ca1\u6709\u4e3a\u4f60\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u7a7a\u51fd\u6570\u7684\u8bdd\uff0c\u8bf7\u50cf\u4e0b\u9762\u8fd9\u6837\u81ea\u884c\u7f16\u5199\u4e00\u4e2a\uff1a 1 2 3 4 int main () { return 0 ; } \u7531\u4e8e\u9700\u8981\u5411\u63a7\u5236\u53f0\u8f93\u51fa\u4fe1\u606f\u4ee5\u53ca\u4ece\u7528\u6237\u83b7\u53d6\u6570\uff0c\u56e0\u6b64\u5fc5\u987b\u8981\u5305\u542b iostream \u5934\u6587\u4ef6\u4ee5\u4fbf\u80fd\u591f\u8bbf\u95ee std:: cout \u548c std:: cin \u3002 1 2 3 4 5 6 #include int main () { return 0 ; } \u73b0\u5728\uff0c\u5411\u7528\u6237\u53d1\u9001\u4fe1\u606f\uff0c\u63d0\u793a\u4ed6\u8f93\u5165\u4e00\u4e2a\u6574\u578b\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << \"Enter an integer: \" ; return 0 ; } \u81f3\u6b64\uff0c\u8be5\u7a0b\u5e8f\u5e94\u8be5\u80fd\u591f\u5f97\u5230\u5982\u4e0b\u7684\u8f93\u51fa\u7ed3\u679c\uff1a 1 Enter an integer: \u7136\u540e\u7a0b\u5e8f\u5c31\u7ec8\u6b62\u4e86\u3002 \u63a5\u4e0b\u6765\uff0c\u9700\u8981\u4ece\u7528\u6237\u83b7\u53d6\u8f93\u5165\u4fe1\u606f\uff0c\u8fd9\u91cc\u4f7f\u7528 std:: cin \u548c operator>> \u6765\u5b8c\u6210\u3002\u4f46\u662f\u6211\u4eec\u5fc5\u987b\u5b9a\u4e49\u4e00\u4e2a\u7528\u6765\u5b58\u653e\u8f93\u5165\u7ed3\u679c\u7684\u53d8\u91cf\u3002 1 2 3 4 5 6 7 8 9 10 11 #include int main () { std :: cout << \"Enter an integer: \" ; int num { }; // define variable num as an integer variable std :: cin << num ; // get integer value from user's keyboard return 0 ; } \u662f\u65f6\u5019\u518d\u6b21\u7f16\u8bd1\u7a0b\u5e8f\u4e86... \u7cdf\u7cd5\uff01\u4f5c\u8005\u7684 Visual Studio 2017 \u62a5\u544a\u4e86\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 5 6 7 1>------ Build started: Project: Double, Configuration: Release Win32 ------ 1>Double.cpp 1>c:\\vcprojects\\double\\double.cpp(8): error C2678: binary '<<': no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion) 1>c:\\vcprojects\\double\\double.cpp: note: could be 'built-in C++ operator<<(bool, int)' 1>c:\\vcprojects\\double\\double.cpp: note: while trying to match the argument list '(std::istream, int)' 1>Done building project \"Double.vcxproj\" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== \u6709\u7f16\u8bd1\u9519\u8bef\uff01 \u9996\u5148\uff0c\u7531\u4e8e\u8fd9\u6b21\u7f16\u8bd1\u53d1\u751f\u5728\u6700\u65b0\u4e00\u6b21\u4ee3\u7801\u4fee\u6539\u4e4b\u540e\uff0c\u7136\u540e\u5c31\u62a5\u9519\u4e86\uff0c\u8bf4\u660e\u9519\u8bef\u662f\u5728\u6700\u540e\u4e00\u6b21\u4fee\u6539\u4ee3\u7801\u65f6\u88ab\u5f15\u5165\u7684\uff087\u30018 \u4e24\u884c\uff09\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u5206\u6790\u7684\u4ee3\u7801\u76f8\u5bf9\u4e8e\u5168\u90e8\u4ee3\u7801\u8d77\u59cb\u662f\u5f88\u5c11\u7684\u3002\u7b2c\u4e03\u884c\u975e\u5e38\u7b80\u5355\uff08\u4ec5\u4ec5\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53d8\u91cf\uff09\uff0c\u9519\u8bef\u5f88\u53ef\u80fd\u4e0d\u5728\u8fd9\u4e00\u884c\u3002\u8fd9\u6837\u4e00\u6765\u7b2c\u516b\u884c\u7684\u5acc\u7591\u5c31\u5f88\u5927\u4e86\u3002 \u5176\u6b21\uff0c\u8fd9\u91cc\u7684\u62a5\u9519\u4fe1\u606f\u5e76\u4e0d\u662f\u975e\u5e38\u6613\u8bfb\u3002\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u4ece\u4e2d\u5bfb\u627e\u4e00\u4e9b\u5173\u952e\u4fe1\u606f\uff1a\u7f16\u8bd1\u5668\u544a\u8bc9\u6211\u4eec\uff0c\u9519\u8bef\u53d1\u9001\u5728\u7b2c\u516b\u884c\u3002\u8fd9\u610f\u5473\u7740\u9519\u8bef\u5f88\u53ef\u80fd\u53d1\u751f\u5728\u7b2c\u516b\u884c\u6216\u7b2c\u516b\u884c\u4e4b\u524d\uff0c\u8fd9\u4e5f\u5370\u8bc1\u4e86\u6211\u4eec\u4e4b\u524d\u7684\u731c\u60f3\u3002\u7136\u540e\uff0c\u7f16\u8bd1\u5668\u544a\u8bc9\u6211\u4eec\uff0c\u5b83\u6ca1\u6709\u80fd\u591f\u627e\u5230\u4e00\u4e2a\u80fd\u591f\u5728\u7b26\u53f7\u5de6\u4fa7\u63a5\u53d7\u4e00\u4e2a std:: istream \u7c7b\u578b\uff08\u5373 std:: cin \u7684\u7c7b\u578b\uff09\u7684\u64cd\u4f5c\u6570\u7684 << \u8fd0\u7b97\u7b26\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u8fd0\u7b97\u7b26 << \u4e0d\u77e5\u9053\u5982\u4f55\u5904\u7406 std:: cin \u3002\u56e0\u6b64\uff0c\u5f15\u8d77\u9519\u8bef\u7684\u8981\u4e48\u662f\u4f7f\u7528 std:: cin \u4e0d\u5f53\uff0c\u8981\u4e48\u662f\u4f7f\u7528 << \u4e0d\u5f53\u3002 \u770b\u51fa\u95ee\u9898\u4e86\u5417\uff1f\u5982\u679c\u8fd8\u6ca1\u6709\uff0c\u90a3\u518d\u4ed4\u7ec6\u770b\u770b\u3002 \u4e0b\u9762\u4ee3\u7801\u662f\u6b63\u786e\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { std :: cout << \"Enter an integer: \" ; int num { }; std :: cin >> num ; // std::cin uses operator >>, not operator < // worst version int main () { std :: cout << \"Enter an integer: \" ; int num { }; std :: cin >> num ; num = num * 2 ; // double num's value, then assign that value back to num std :: cout << \"Double that number is: \" << num << '\\n' ; return 0 ; } \u5728\u8fd9\u4e2a\u65b9\u6848\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u8868\u8fbe\u5f0f\u5c06 num \u7684\u503c\u4e58\u4ee5 2\uff0c\u7136\u540e\u5c06\u5176\u518d\u6b21\u8d4b\u503c\u7ed9 num \u3002\u6b64\u65f6\uff0c num \u7684\u503c\u5c31\u662f\u4e58\u4ee5 2 \u540e\u7684\u7ed3\u679c\u4e86\u3002 \u4e3a\u4ec0\u4e48\u8bf4\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u597d\u5462\uff1f \u5728\u8d4b\u503c\u8bed\u53e5\u4e4b\u524d\uff0c num \u5305\u542b\u7684\u662f\u7528\u6237\u7684\u8f93\u5165\uff0c\u5728\u8d4b\u503c\u8bed\u53e5\u4e4b\u540e\uff0c\u5b83\u5305\u542b\u7684\u662f\u4e00\u4e2a\u4e0d\u540c\u7684\u503c\u3002\u8fd9\u4f1a\u4ee4\u4eba\u611f\u5230\u56f0\u60d1\u3002 \u901a\u8fc7\u4e3a\u63a5\u6536\u8f93\u5165\u7684\u53d8\u91cf\u8d4b\u65b0\u503c\uff0c\u6211\u4eec\u8986\u76d6\u4e86\u5b83\u539f\u6709\u7684\u503c\uff0c\u90a3\u4e48\u5982\u679c\u5728\u540e\u7eed\u6269\u5c55\u7a0b\u5e8f\u7684\u65f6\u5019\u5e0c\u671b\u518d\u6b21\u4f7f\u7528\u8fd9\u4e2a\u8f93\u5165\u503c\u6765\u505a\u4e9b\u522b\u7684\u4e8b\u60c5\u5c31\u65e0\u6cd5\u5b9e\u73b0\u4e86\uff08\u4f8b\u5982\u5c06\u8f93\u5165\u7ed3\u679c\u4e58\u4ee5 3\uff09\uff0c\u56e0\u4e3a\u5b83\u5df2\u7ecf\u88ab\u4e22\u5f03\u4e86\u3002 \u8fd8\u4e0d\u9519\u7684\u89e3\u51b3\u65b9\u6848 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // less-bad version int main () { std :: cout << \"Enter an integer: \" ; int num { }; std :: cin >> num ; int doublenum { num * 2 }; // define a new variable and initialize it with num * 2 std :: cout << \"Double that number is: \" << doublenum << '\\n' ; // then print the value of that variable here return 0 ; } \u8fd9\u4e2a\u65b9\u6848\u975e\u5e38\u7684\u76f4\u767d\uff0c\u5b83\u89e3\u51b3\u4e86\u4e0a\u4e00\u79cd\u65b9\u6848\u4e2d\u4f1a\u9762\u4e34\u7684\u4e24\u4e2a\u95ee\u9898\u3002 \u8fd9\u4e2a\u65b9\u6848\u7684\u4e3b\u8981\u7f3a\u70b9\u5728\u4e8e\uff0c\u8fd9\u91cc\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\uff08\u7740\u65e0\u7591\u589e\u52a0\u4e86\u590d\u6742\u6027\uff09\u6765\u5b58\u50a8\u4e00\u4e2a\u4e00\u6b21\u6027\u7684\u503c\u3002\u4e00\u5b9a\u8fd8\u6709\u66f4\u597d\u7684\u65b9\u6cd5\u3002 \u6700\u4f73\u65b9\u6848 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // preferred version int main () { std :: cout << \"Enter an integer: \" ; int num { }; std :: cin >> num ; std :: cout << \"Double that number is: \" << num * 2 << '\\n' ; // use an expression to multiply num * 2 at the point where we are going to print it return 0 ; } \u8fd9\u79cd\u65b9\u6cd5\u662f\u8fd9\u4e00\u7cfb\u5217\u89e3\u51b3\u65b9\u6848\u4e2d\u6700\u63a8\u8350\u7684\u3002\u5f53 std:: cout \u6267\u884c\u7684\u65f6\u5019\uff0c\u8868\u8fbe\u5f0f num * 2 \u4f1a\u8fdb\u884c\u6c42\u503c\uff0c\u5176\u7ed3\u679c\u5c31\u662f\u4e24\u500d\u7684 num \u503c\u3002\u8be5\u503c\u4f1a\u88ab\u6253\u5370\u5904\u7406\uff0c\u540c\u65f6\u53d8\u91cf num \u7684\u503c\u5e76\u6ca1\u6709\u88ab\u4fee\u6539\uff0c\u65b9\u4fbf\u6211\u4eec\u540e\u7eed\u7ee7\u7eed\u4f7f\u7528\u5b83\u3002 \u8fd9\u4e2a\u7248\u672c\u662f\u6211\u4eec\u63d0\u4f9b\u7684\u53c2\u8003\u7b54\u6848\u3002 \u4f5c\u8005\u6ce8 \u7f16\u7a0b\u7684\u9996\u8981\u76ee\u6807\uff0c\u662f\u8ba9\u7a0b\u5e8f\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u3002\u4e0d\u80fd\u5de5\u4f5c\u7684\u7a0b\u5e8f\u4e0d\u8bba\u5199\u7684\u591a\u597d s\uff0c\u90fd\u662f\u6ca1\u6709\u610f\u4e49\u7684\u3002 \u7136\u800c\uff0c\u6211\u5f88\u559c\u6b22\u7684\u4e00\u53e5\u540d\u8a00\u662f\u8fd9\u4e48\u8bf4\u7684\uff1a\u4f60\u5fc5\u987b\u5199\u8fc7\u4e00\u904d\u4ee3\u7801\u4e4b\u540e\uff0c\u624d\u80fd\u77e5\u9053\u4f60\u6700\u521d\u5e94\u8be5\u5982\u4f55\u53bb\u5199\u5b83\u3002\u8fd9\u53e5\u8bdd\u9053\u51fa\u4e86\u8fd9\u6837\u4e00\u4e2a\u4e8b\u5b9e\uff0c\u6700\u4f73\u7684\u89e3\u51b3\u65b9\u6848\u5f80\u5f80\u4e0d\u660e\u663e\u7684\uff0c\u800c\u6700\u5148\u60f3\u5230\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5f80\u5f80\u53c8\u4e0d\u662f\u6700\u4f73\u7684\u3002 \u5728\u6211\u4eec\u4e13\u6ce8\u4e8e\u5982\u4f55\u89e3\u51b3\u95ee\u9898\u5e76\u4f7f\u5f97\u7a0b\u5e8f\u8fd0\u884c\u8d77\u6765\u7684\u65f6\u5019\uff0c\u82b1\u5927\u91cf\u7684\u65f6\u95f4\u53d6\u7422\u78e8\u8fd9\u4e9b\u6211\u4eec\u751a\u81f3\u90fd\u4e0d\u77e5\u9053\u662f\u5426\u6700\u7ec8\u4f1a\u88ab\u4f7f\u7528\u7684\u4ee3\u7801\uff0c\u662f\u4e0d\u5212\u7b97\u3002\u56e0\u6b64\u6211\u4eec\u9700\u8981\u53e6\u8f9f\u8e4a\u5f84\uff0c\u6682\u65f6\u8df3\u8fc7\u5f02\u5e38\u5904\u7406\u548c\u6ce8\u91ca\uff0c\u4ee3\u7801\u4e2d\u5145\u65a5\u7740\u5927\u91cf\u7684\u5e2e\u52a9\u5b9a\u4f4d\u95ee\u9898\u7684\u8c03\u8bd5\u4ee3\u7801\u4e5f\u65e0\u6240\u8c13\uff0c\u6211\u4eec\u8981\u5b66\u4f1a\u6478\u7740\u77f3\u5934\u8fc7\u6cb3\uff0c\u800c\u5f53\u4e00\u4e2a\u65b9\u6cd5\u5f7b\u5e95\u884c\u4e0d\u901a\u7684\u65f6\u5019\uff0c\u5c31\u6362\u4e00\u79cd\u65b9\u6cd5\u4ece\u65b0\u5f00\u59cb\u3002 \u8fd9\u4e48\u505a\u7684\u7ed3\u679c\u5c31\u662f\uff0c\u6700\u521d\u7248\u672c\u7684\u4ee3\u7801\u5f80\u5f80\u7f3a\u4e4f\u6761\u7406\u3001\u5065\u58ee\u6027\u4e5f\u4e0d\u4f73\u3001\u53ef\u8bfb\u6027\u4e0d\u597d\uff0c\u5c31\u66f4\u522b\u63d0\u4ec0\u4e48\u7b80\u4ecb\u660e\u4e86\u4e86\u3002\u56e0\u6b64\uff0c\u5f53\u4f60\u7684\u4ee3\u7801\u53ef\u4ee5\u5de5\u4f5c\u4e4b\u540e\uff0c\u4f60\u7684\u5de5\u4f5c\u8fd8\u6ca1\u6709\u5b8c\u6210\uff08\u51fa\u53d1\u4f60\u7f16\u5199\u7684\u4ee3\u7801\u662f\u4e00\u6b21\u6027\u7684\uff09\u3002\u63a5\u4e0b\u6765\uff0c\u4f60\u9700\u8981\u6e05\u7406\u4f60\u7684\u4ee3\u7801\uff0c\u5305\u62ec\u5220\u9664\uff08\u6216\u6ce8\u91ca\u6389\uff09\u5e2e\u52a9\u8c03\u8bd5\u7684\u4e34\u65f6\u6027\u4ee3\u7801\u3001\u7f16\u5199\u6ce8\u91ca\u3001\u5904\u7406\u53ef\u80fd\u53d1\u751f\u7684\u9519\u8bef\u3001\u7f8e\u5316\u4ee3\u7801\u7684\u6392\u7248\u5e76\u786e\u4fdd\u4ee3\u7801\u9075\u5faa\u4e86\u76f8\u5173\u7684\u6700\u4f73\u5b9e\u8df5\u3002\u6b64\u5916\uff0c\u4f60\u7684\u4ee3\u7801\u53ef\u80fd\u5e76\u4e0d\u662f\u6700\u7cbe\u7b80\u7684\uff0c\u4e5f\u8bb8\u91cc\u9762\u8fd8\u5b58\u5728\u7740\u53ef\u4ee5\u88ab\u5408\u5e76\u7684\u5197\u4f59\u903b\u8f91\uff0c\u6216\u8005\u5b58\u5728\u7740\u53ef\u4ee5\u88ab\u5408\u5e76\u7684\u8bed\u53e5\uff0c\u4ea6\u6216\u662f\u6709\u4e9b\u53d8\u91cf\u662f\u6ca1\u6709\u5fc5\u8981\u7684\uff0c\u4ee5\u53ca\u5176\u4ed6\u5404\u79cd\u53ef\u4ee5\u88ab\u7b80\u5316\u7684\u5e38\u89c1\u3002\u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u90fd\u8feb\u5207\u7684\u60f3\u8981\u53bb\u4f18\u5316\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u800c\u5b9e\u9645\u4e0a\u7a0b\u5e8f\u7684\u53ef\u7ef4\u62a4\u6027\u624d\u662f\u5b83\u4eec\u6700\u5e94\u8be5\u4f18\u5148\u8003\u8651\u7684\u4e8b\u60c5\u3002 \u672c\u6559\u7a0b\u63d0\u4f9b\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5f88\u5c11\u662f\u7b2c\u4e00\u6b21\u5c31\u5f97\u5230\u6700\u4f73\u89e3\u51b3\u65b9\u6848\u7684\u3002\u5b9e\u9645\u4e0a\uff0c\u5b83\u4eec\u90fd\u7ecf\u8fc7\u4e86\u4e0d\u65ad\u7684\u4fee\u6539\uff0c\u76f4\u5230\u6211\u4eec\u8ba4\u4e3a\u6ca1\u6709\u4ec0\u4e48\u53ef\u4ee5\u518d\u4f18\u5316\u7684\u4e86\u3002\u5373\u4f7f\u662f\u8fd9\u6837\uff0c\u5f88\u591a\u65f6\u5019\u8bfb\u8005\u8fd8\u662f\u80fd\u591f\u5411\u6211\u4eec\u63d0\u51fa\u5f88\u591a\u4f18\u5316\u7684\u5efa\u8bae\uff01 \u8bf4\u8fd9\u4e48\u591a\uff0c\u6211\u60f3\u8868\u8fbe\u7684\u662f\uff1a\u5982\u679c\u4f60\u6ca1\u6709\u4e00\u6b21\u6027\u60f3\u5230\u4e00\u4e2a\u6700\u4f18\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u8bf7\u4e0d\u8981\u4e3a\u6b64\u611f\u5230\u6cae\u4e27\uff0c\u8fd9\u5f88\u6b63\u5e38\u3002\u7cbe\u76ca\u6c42\u7cbe\u5728\u7f16\u7a0b\u4e2d\u662f\u4e00\u4e2a\u8fed\u4ee3\u7684\u8fc7\u7a0b\u3002 \u4f5c\u8005\u6ce8 \u8fd8\u6709\u4e00\u4ef6\u4e8b\uff1a\u4f60\u53ef\u80fd\u4f1a\u60f3\uff0c\u201dC++\u4e2d\u6709\u8fd9\u4e48\u591a\u7684\u89c4\u5219\u548c\u6982\u5ff5\uff0c\u6211\u600e\u4e48\u624d\u80fd\u8bb0\u4f4f\u5b83\u4eec\u5462\uff1f\u201c \u7b80\u5355\u6765\u8bf4\uff0c\u4f60\u5e76\u4e0d\u9700\u8981\u8bb0\u4f4f\u5b83\u4eec\u3002\u4f7f\u7528 C++\uff0c\u4e00\u65b9\u9762\u5728\u4e8e\u4f7f\u7528\u4f60\u5df2\u7ecf\u638c\u63e1\u7684\u77e5\u8bc6\uff0c\u53e6\u4e00\u65b9\u9762\u5728\u4e8e\u638c\u63e1\u5982\u4f55\u67e5\u627e\u5e76\u4f7f\u7528\u4f60\u6ca1\u6709\u638c\u63e1\u7684\u77e5\u8bc6\u3002 \u5f53\u4f60\u7b2c\u4e00\u6b21\u9605\u8bfb\u672c\u6559\u7a0b\u7684\u65f6\u5019\uff0c\u4e0d\u8981\u628a\u6ce8\u610f\u529b\u653e\u5728\u8bb0\u5fc6\u89c4\u5219\u4e0a\uff0c\u800c\u662f\u8981\u91cd\u70b9\u7406\u89e3\u4f7f\u7528C++\u4f60\u80fd\u505a\u54ea\u4e9b\u4e8b\u3002\u7136\u540e\uff0c\u5f53\u4f60\u771f\u6b63\u9700\u8981\u5728\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u67d0\u4e2a\u529f\u80fd\u7684\u65f6\u5019\uff0c\u4f60\u53ef\u4ee5\u518d\u56de\u6765\uff08\u6216\u8005\u5230\u5176\u4ed6\u7f51\u7ad9\uff09\u53bb\u5237\u65b0\u5de9\u56fa\u4f60\u7684\u77e5\u8bc6\u5e76\u638c\u63e1\u5e94\u8be5\u5982\u4f55\u53bb\u505a\u3002","title":"1.11 - \u5f00\u53d1\u4f60\u7684\u7b2c\u4e00\u4e2a\u7a0b\u5e8f"},{"location":"1-11-Developing-your-first-program/#2","text":"\u9996\u5148\uff0c\u6211\u4eec\u4f1a\u8981\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6574\u578b\u6570\u5e76\u7b49\u5f85\u7528\u6237\u5b8c\u6210\u8f93\u5165\uff0c\u7136\u540e\u6211\u4eec\u4f1a\u5c06\u8be5\u503c\u4e58\u4ee5 2 \u5e76\u544a\u8bc9\u7528\u6237\u8ba1\u7b97\u7ed3\u679c\u3002\u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff08\u5047\u8bbe\u8f93\u5165\u503c\u4e3a 4\uff09\uff1a 1 2 Enter an integer: 4 Double that number is: 8 \u4e0b\u9762\u8ba9\u6211\u4eec\u6765 \u9010\u6b65 \u5b8c\u6210\u4e0a\u8ff0\u529f\u80fd\uff1a \u6700\u4f73\u5b9e\u8df5 \u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u4f1a\u53bb\u5c1d\u8bd5\u4e00\u6b21\u6027\u5b8c\u6210\u6574\u4e2a\u7a0b\u5e8f\u7684\u7f16\u5199\uff0c\u7136\u540e\u4fbf\u4f1a\u56e0\u4e3a\u5927\u91cf\u62a5\u9519\u4fe1\u606f\u800c\u611f\u5230\u65e0\u6240\u9002\u4ece\u3002\u6bd4\u8f83\u597d\u7684\u65b9\u5f0f\u662f\u6bcf\u6b21\u6dfb\u52a0\u4e00\u90e8\u5206\u5185\u5bb9\u5e76\u4fdd\u8bc1\u5176\u80fd\u591f\u901a\u8fc7\u7f16\u8bd1\u548c\u901a\u8fc7\u6d4b\u8bd5\u3002\u5f53\u4f60\u80fd\u591f\u786e\u4fdd\u5176\u6b63\u786e\u5de5\u4f5c\u540e\uff0c\u518d\u7f16\u5199\u63a5\u4e0b\u6765\u7684\u4ee3\u7801\u3002 \u672c\u8282\u8bfe\u4e5f\u4f1a\u91c7\u53d6\u4e0a\u8ff0\u7b56\u7565\u3002\u6211\u4eec\u4f1a\u5c06\u7a0b\u5e8f\u7684\u5b9e\u73b0\u5206\u4e3a\u591a\u4e2a\u6b65\u9aa4\uff0c\u8bf7\u5c06\u5404\u6b65\u9aa4\u4e2d\u7684\u4ee3\u7801 \u8f93\u5165 \uff08\u4e0d\u8981\u590d\u5236\u7c98\u8d34\uff09\u8ba1\u7b97\u673a\uff0c\u7f16\u8bd1\u5e76\u4e14\u8fd0\u884c\u3002 \u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u63a7\u5236\u53f0\u7a0b\u5e8f\u9879\u76ee\u3002 \u73b0\u5728\uff0c\u7f16\u5199\u4e00\u4e2a\u57fa\u672c\u7684\u7a0b\u5e8f\u6846\u67b6\u3002\u6211\u4eec\u77e5\u9053\uff0c\u7f16\u5199\u4e00\u4e2a main() \u7a0b\u5e8f\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u6b65\u9aa4\uff08\u56e0\u4e3a C++\u7a0b\u5e8f\u5fc5\u987b\u5177\u6709\u4e00\u4e2a main \u51fd\u6570\uff09\u3002\u5982\u679c\u4f60\u7684 IDE \u6ca1\u6709\u4e3a\u4f60\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u7a7a\u51fd\u6570\u7684\u8bdd\uff0c\u8bf7\u50cf\u4e0b\u9762\u8fd9\u6837\u81ea\u884c\u7f16\u5199\u4e00\u4e2a\uff1a 1 2 3 4 int main () { return 0 ; } \u7531\u4e8e\u9700\u8981\u5411\u63a7\u5236\u53f0\u8f93\u51fa\u4fe1\u606f\u4ee5\u53ca\u4ece\u7528\u6237\u83b7\u53d6\u6570\uff0c\u56e0\u6b64\u5fc5\u987b\u8981\u5305\u542b iostream \u5934\u6587\u4ef6\u4ee5\u4fbf\u80fd\u591f\u8bbf\u95ee std:: cout \u548c std:: cin \u3002 1 2 3 4 5 6 #include int main () { return 0 ; } \u73b0\u5728\uff0c\u5411\u7528\u6237\u53d1\u9001\u4fe1\u606f\uff0c\u63d0\u793a\u4ed6\u8f93\u5165\u4e00\u4e2a\u6574\u578b\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << \"Enter an integer: \" ; return 0 ; } \u81f3\u6b64\uff0c\u8be5\u7a0b\u5e8f\u5e94\u8be5\u80fd\u591f\u5f97\u5230\u5982\u4e0b\u7684\u8f93\u51fa\u7ed3\u679c\uff1a 1 Enter an integer: \u7136\u540e\u7a0b\u5e8f\u5c31\u7ec8\u6b62\u4e86\u3002 \u63a5\u4e0b\u6765\uff0c\u9700\u8981\u4ece\u7528\u6237\u83b7\u53d6\u8f93\u5165\u4fe1\u606f\uff0c\u8fd9\u91cc\u4f7f\u7528 std:: cin \u548c operator>> \u6765\u5b8c\u6210\u3002\u4f46\u662f\u6211\u4eec\u5fc5\u987b\u5b9a\u4e49\u4e00\u4e2a\u7528\u6765\u5b58\u653e\u8f93\u5165\u7ed3\u679c\u7684\u53d8\u91cf\u3002 1 2 3 4 5 6 7 8 9 10 11 #include int main () { std :: cout << \"Enter an integer: \" ; int num { }; // define variable num as an integer variable std :: cin << num ; // get integer value from user's keyboard return 0 ; } \u662f\u65f6\u5019\u518d\u6b21\u7f16\u8bd1\u7a0b\u5e8f\u4e86... \u7cdf\u7cd5\uff01\u4f5c\u8005\u7684 Visual Studio 2017 \u62a5\u544a\u4e86\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 5 6 7 1>------ Build started: Project: Double, Configuration: Release Win32 ------ 1>Double.cpp 1>c:\\vcprojects\\double\\double.cpp(8): error C2678: binary '<<': no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion) 1>c:\\vcprojects\\double\\double.cpp: note: could be 'built-in C++ operator<<(bool, int)' 1>c:\\vcprojects\\double\\double.cpp: note: while trying to match the argument list '(std::istream, int)' 1>Done building project \"Double.vcxproj\" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== \u6709\u7f16\u8bd1\u9519\u8bef\uff01 \u9996\u5148\uff0c\u7531\u4e8e\u8fd9\u6b21\u7f16\u8bd1\u53d1\u751f\u5728\u6700\u65b0\u4e00\u6b21\u4ee3\u7801\u4fee\u6539\u4e4b\u540e\uff0c\u7136\u540e\u5c31\u62a5\u9519\u4e86\uff0c\u8bf4\u660e\u9519\u8bef\u662f\u5728\u6700\u540e\u4e00\u6b21\u4fee\u6539\u4ee3\u7801\u65f6\u88ab\u5f15\u5165\u7684\uff087\u30018 \u4e24\u884c\uff09\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u5206\u6790\u7684\u4ee3\u7801\u76f8\u5bf9\u4e8e\u5168\u90e8\u4ee3\u7801\u8d77\u59cb\u662f\u5f88\u5c11\u7684\u3002\u7b2c\u4e03\u884c\u975e\u5e38\u7b80\u5355\uff08\u4ec5\u4ec5\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53d8\u91cf\uff09\uff0c\u9519\u8bef\u5f88\u53ef\u80fd\u4e0d\u5728\u8fd9\u4e00\u884c\u3002\u8fd9\u6837\u4e00\u6765\u7b2c\u516b\u884c\u7684\u5acc\u7591\u5c31\u5f88\u5927\u4e86\u3002 \u5176\u6b21\uff0c\u8fd9\u91cc\u7684\u62a5\u9519\u4fe1\u606f\u5e76\u4e0d\u662f\u975e\u5e38\u6613\u8bfb\u3002\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u4ece\u4e2d\u5bfb\u627e\u4e00\u4e9b\u5173\u952e\u4fe1\u606f\uff1a\u7f16\u8bd1\u5668\u544a\u8bc9\u6211\u4eec\uff0c\u9519\u8bef\u53d1\u9001\u5728\u7b2c\u516b\u884c\u3002\u8fd9\u610f\u5473\u7740\u9519\u8bef\u5f88\u53ef\u80fd\u53d1\u751f\u5728\u7b2c\u516b\u884c\u6216\u7b2c\u516b\u884c\u4e4b\u524d\uff0c\u8fd9\u4e5f\u5370\u8bc1\u4e86\u6211\u4eec\u4e4b\u524d\u7684\u731c\u60f3\u3002\u7136\u540e\uff0c\u7f16\u8bd1\u5668\u544a\u8bc9\u6211\u4eec\uff0c\u5b83\u6ca1\u6709\u80fd\u591f\u627e\u5230\u4e00\u4e2a\u80fd\u591f\u5728\u7b26\u53f7\u5de6\u4fa7\u63a5\u53d7\u4e00\u4e2a std:: istream \u7c7b\u578b\uff08\u5373 std:: cin \u7684\u7c7b\u578b\uff09\u7684\u64cd\u4f5c\u6570\u7684 << \u8fd0\u7b97\u7b26\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u8fd0\u7b97\u7b26 << \u4e0d\u77e5\u9053\u5982\u4f55\u5904\u7406 std:: cin \u3002\u56e0\u6b64\uff0c\u5f15\u8d77\u9519\u8bef\u7684\u8981\u4e48\u662f\u4f7f\u7528 std:: cin \u4e0d\u5f53\uff0c\u8981\u4e48\u662f\u4f7f\u7528 << \u4e0d\u5f53\u3002 \u770b\u51fa\u95ee\u9898\u4e86\u5417\uff1f\u5982\u679c\u8fd8\u6ca1\u6709\uff0c\u90a3\u518d\u4ed4\u7ec6\u770b\u770b\u3002 \u4e0b\u9762\u4ee3\u7801\u662f\u6b63\u786e\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { std :: cout << \"Enter an integer: \" ; int num { }; std :: cin >> num ; // std::cin uses operator >>, not operator < // worst version int main () { std :: cout << \"Enter an integer: \" ; int num { }; std :: cin >> num ; num = num * 2 ; // double num's value, then assign that value back to num std :: cout << \"Double that number is: \" << num << '\\n' ; return 0 ; } \u5728\u8fd9\u4e2a\u65b9\u6848\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u8868\u8fbe\u5f0f\u5c06 num \u7684\u503c\u4e58\u4ee5 2\uff0c\u7136\u540e\u5c06\u5176\u518d\u6b21\u8d4b\u503c\u7ed9 num \u3002\u6b64\u65f6\uff0c num \u7684\u503c\u5c31\u662f\u4e58\u4ee5 2 \u540e\u7684\u7ed3\u679c\u4e86\u3002 \u4e3a\u4ec0\u4e48\u8bf4\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u597d\u5462\uff1f \u5728\u8d4b\u503c\u8bed\u53e5\u4e4b\u524d\uff0c num \u5305\u542b\u7684\u662f\u7528\u6237\u7684\u8f93\u5165\uff0c\u5728\u8d4b\u503c\u8bed\u53e5\u4e4b\u540e\uff0c\u5b83\u5305\u542b\u7684\u662f\u4e00\u4e2a\u4e0d\u540c\u7684\u503c\u3002\u8fd9\u4f1a\u4ee4\u4eba\u611f\u5230\u56f0\u60d1\u3002 \u901a\u8fc7\u4e3a\u63a5\u6536\u8f93\u5165\u7684\u53d8\u91cf\u8d4b\u65b0\u503c\uff0c\u6211\u4eec\u8986\u76d6\u4e86\u5b83\u539f\u6709\u7684\u503c\uff0c\u90a3\u4e48\u5982\u679c\u5728\u540e\u7eed\u6269\u5c55\u7a0b\u5e8f\u7684\u65f6\u5019\u5e0c\u671b\u518d\u6b21\u4f7f\u7528\u8fd9\u4e2a\u8f93\u5165\u503c\u6765\u505a\u4e9b\u522b\u7684\u4e8b\u60c5\u5c31\u65e0\u6cd5\u5b9e\u73b0\u4e86\uff08\u4f8b\u5982\u5c06\u8f93\u5165\u7ed3\u679c\u4e58\u4ee5 3\uff09\uff0c\u56e0\u4e3a\u5b83\u5df2\u7ecf\u88ab\u4e22\u5f03\u4e86\u3002","title":"\u4e0d\u597d\u7684\u89e3\u51b3\u65b9\u6848\uff1a"},{"location":"1-11-Developing-your-first-program/#_2","text":"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // less-bad version int main () { std :: cout << \"Enter an integer: \" ; int num { }; std :: cin >> num ; int doublenum { num * 2 }; // define a new variable and initialize it with num * 2 std :: cout << \"Double that number is: \" << doublenum << '\\n' ; // then print the value of that variable here return 0 ; } \u8fd9\u4e2a\u65b9\u6848\u975e\u5e38\u7684\u76f4\u767d\uff0c\u5b83\u89e3\u51b3\u4e86\u4e0a\u4e00\u79cd\u65b9\u6848\u4e2d\u4f1a\u9762\u4e34\u7684\u4e24\u4e2a\u95ee\u9898\u3002 \u8fd9\u4e2a\u65b9\u6848\u7684\u4e3b\u8981\u7f3a\u70b9\u5728\u4e8e\uff0c\u8fd9\u91cc\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\uff08\u7740\u65e0\u7591\u589e\u52a0\u4e86\u590d\u6742\u6027\uff09\u6765\u5b58\u50a8\u4e00\u4e2a\u4e00\u6b21\u6027\u7684\u503c\u3002\u4e00\u5b9a\u8fd8\u6709\u66f4\u597d\u7684\u65b9\u6cd5\u3002","title":"\u8fd8\u4e0d\u9519\u7684\u89e3\u51b3\u65b9\u6848"},{"location":"1-11-Developing-your-first-program/#_3","text":"1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // preferred version int main () { std :: cout << \"Enter an integer: \" ; int num { }; std :: cin >> num ; std :: cout << \"Double that number is: \" << num * 2 << '\\n' ; // use an expression to multiply num * 2 at the point where we are going to print it return 0 ; } \u8fd9\u79cd\u65b9\u6cd5\u662f\u8fd9\u4e00\u7cfb\u5217\u89e3\u51b3\u65b9\u6848\u4e2d\u6700\u63a8\u8350\u7684\u3002\u5f53 std:: cout \u6267\u884c\u7684\u65f6\u5019\uff0c\u8868\u8fbe\u5f0f num * 2 \u4f1a\u8fdb\u884c\u6c42\u503c\uff0c\u5176\u7ed3\u679c\u5c31\u662f\u4e24\u500d\u7684 num \u503c\u3002\u8be5\u503c\u4f1a\u88ab\u6253\u5370\u5904\u7406\uff0c\u540c\u65f6\u53d8\u91cf num \u7684\u503c\u5e76\u6ca1\u6709\u88ab\u4fee\u6539\uff0c\u65b9\u4fbf\u6211\u4eec\u540e\u7eed\u7ee7\u7eed\u4f7f\u7528\u5b83\u3002 \u8fd9\u4e2a\u7248\u672c\u662f\u6211\u4eec\u63d0\u4f9b\u7684\u53c2\u8003\u7b54\u6848\u3002 \u4f5c\u8005\u6ce8 \u7f16\u7a0b\u7684\u9996\u8981\u76ee\u6807\uff0c\u662f\u8ba9\u7a0b\u5e8f\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u3002\u4e0d\u80fd\u5de5\u4f5c\u7684\u7a0b\u5e8f\u4e0d\u8bba\u5199\u7684\u591a\u597d s\uff0c\u90fd\u662f\u6ca1\u6709\u610f\u4e49\u7684\u3002 \u7136\u800c\uff0c\u6211\u5f88\u559c\u6b22\u7684\u4e00\u53e5\u540d\u8a00\u662f\u8fd9\u4e48\u8bf4\u7684\uff1a\u4f60\u5fc5\u987b\u5199\u8fc7\u4e00\u904d\u4ee3\u7801\u4e4b\u540e\uff0c\u624d\u80fd\u77e5\u9053\u4f60\u6700\u521d\u5e94\u8be5\u5982\u4f55\u53bb\u5199\u5b83\u3002\u8fd9\u53e5\u8bdd\u9053\u51fa\u4e86\u8fd9\u6837\u4e00\u4e2a\u4e8b\u5b9e\uff0c\u6700\u4f73\u7684\u89e3\u51b3\u65b9\u6848\u5f80\u5f80\u4e0d\u660e\u663e\u7684\uff0c\u800c\u6700\u5148\u60f3\u5230\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5f80\u5f80\u53c8\u4e0d\u662f\u6700\u4f73\u7684\u3002 \u5728\u6211\u4eec\u4e13\u6ce8\u4e8e\u5982\u4f55\u89e3\u51b3\u95ee\u9898\u5e76\u4f7f\u5f97\u7a0b\u5e8f\u8fd0\u884c\u8d77\u6765\u7684\u65f6\u5019\uff0c\u82b1\u5927\u91cf\u7684\u65f6\u95f4\u53d6\u7422\u78e8\u8fd9\u4e9b\u6211\u4eec\u751a\u81f3\u90fd\u4e0d\u77e5\u9053\u662f\u5426\u6700\u7ec8\u4f1a\u88ab\u4f7f\u7528\u7684\u4ee3\u7801\uff0c\u662f\u4e0d\u5212\u7b97\u3002\u56e0\u6b64\u6211\u4eec\u9700\u8981\u53e6\u8f9f\u8e4a\u5f84\uff0c\u6682\u65f6\u8df3\u8fc7\u5f02\u5e38\u5904\u7406\u548c\u6ce8\u91ca\uff0c\u4ee3\u7801\u4e2d\u5145\u65a5\u7740\u5927\u91cf\u7684\u5e2e\u52a9\u5b9a\u4f4d\u95ee\u9898\u7684\u8c03\u8bd5\u4ee3\u7801\u4e5f\u65e0\u6240\u8c13\uff0c\u6211\u4eec\u8981\u5b66\u4f1a\u6478\u7740\u77f3\u5934\u8fc7\u6cb3\uff0c\u800c\u5f53\u4e00\u4e2a\u65b9\u6cd5\u5f7b\u5e95\u884c\u4e0d\u901a\u7684\u65f6\u5019\uff0c\u5c31\u6362\u4e00\u79cd\u65b9\u6cd5\u4ece\u65b0\u5f00\u59cb\u3002 \u8fd9\u4e48\u505a\u7684\u7ed3\u679c\u5c31\u662f\uff0c\u6700\u521d\u7248\u672c\u7684\u4ee3\u7801\u5f80\u5f80\u7f3a\u4e4f\u6761\u7406\u3001\u5065\u58ee\u6027\u4e5f\u4e0d\u4f73\u3001\u53ef\u8bfb\u6027\u4e0d\u597d\uff0c\u5c31\u66f4\u522b\u63d0\u4ec0\u4e48\u7b80\u4ecb\u660e\u4e86\u4e86\u3002\u56e0\u6b64\uff0c\u5f53\u4f60\u7684\u4ee3\u7801\u53ef\u4ee5\u5de5\u4f5c\u4e4b\u540e\uff0c\u4f60\u7684\u5de5\u4f5c\u8fd8\u6ca1\u6709\u5b8c\u6210\uff08\u51fa\u53d1\u4f60\u7f16\u5199\u7684\u4ee3\u7801\u662f\u4e00\u6b21\u6027\u7684\uff09\u3002\u63a5\u4e0b\u6765\uff0c\u4f60\u9700\u8981\u6e05\u7406\u4f60\u7684\u4ee3\u7801\uff0c\u5305\u62ec\u5220\u9664\uff08\u6216\u6ce8\u91ca\u6389\uff09\u5e2e\u52a9\u8c03\u8bd5\u7684\u4e34\u65f6\u6027\u4ee3\u7801\u3001\u7f16\u5199\u6ce8\u91ca\u3001\u5904\u7406\u53ef\u80fd\u53d1\u751f\u7684\u9519\u8bef\u3001\u7f8e\u5316\u4ee3\u7801\u7684\u6392\u7248\u5e76\u786e\u4fdd\u4ee3\u7801\u9075\u5faa\u4e86\u76f8\u5173\u7684\u6700\u4f73\u5b9e\u8df5\u3002\u6b64\u5916\uff0c\u4f60\u7684\u4ee3\u7801\u53ef\u80fd\u5e76\u4e0d\u662f\u6700\u7cbe\u7b80\u7684\uff0c\u4e5f\u8bb8\u91cc\u9762\u8fd8\u5b58\u5728\u7740\u53ef\u4ee5\u88ab\u5408\u5e76\u7684\u5197\u4f59\u903b\u8f91\uff0c\u6216\u8005\u5b58\u5728\u7740\u53ef\u4ee5\u88ab\u5408\u5e76\u7684\u8bed\u53e5\uff0c\u4ea6\u6216\u662f\u6709\u4e9b\u53d8\u91cf\u662f\u6ca1\u6709\u5fc5\u8981\u7684\uff0c\u4ee5\u53ca\u5176\u4ed6\u5404\u79cd\u53ef\u4ee5\u88ab\u7b80\u5316\u7684\u5e38\u89c1\u3002\u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u90fd\u8feb\u5207\u7684\u60f3\u8981\u53bb\u4f18\u5316\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u800c\u5b9e\u9645\u4e0a\u7a0b\u5e8f\u7684\u53ef\u7ef4\u62a4\u6027\u624d\u662f\u5b83\u4eec\u6700\u5e94\u8be5\u4f18\u5148\u8003\u8651\u7684\u4e8b\u60c5\u3002 \u672c\u6559\u7a0b\u63d0\u4f9b\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5f88\u5c11\u662f\u7b2c\u4e00\u6b21\u5c31\u5f97\u5230\u6700\u4f73\u89e3\u51b3\u65b9\u6848\u7684\u3002\u5b9e\u9645\u4e0a\uff0c\u5b83\u4eec\u90fd\u7ecf\u8fc7\u4e86\u4e0d\u65ad\u7684\u4fee\u6539\uff0c\u76f4\u5230\u6211\u4eec\u8ba4\u4e3a\u6ca1\u6709\u4ec0\u4e48\u53ef\u4ee5\u518d\u4f18\u5316\u7684\u4e86\u3002\u5373\u4f7f\u662f\u8fd9\u6837\uff0c\u5f88\u591a\u65f6\u5019\u8bfb\u8005\u8fd8\u662f\u80fd\u591f\u5411\u6211\u4eec\u63d0\u51fa\u5f88\u591a\u4f18\u5316\u7684\u5efa\u8bae\uff01 \u8bf4\u8fd9\u4e48\u591a\uff0c\u6211\u60f3\u8868\u8fbe\u7684\u662f\uff1a\u5982\u679c\u4f60\u6ca1\u6709\u4e00\u6b21\u6027\u60f3\u5230\u4e00\u4e2a\u6700\u4f18\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u8bf7\u4e0d\u8981\u4e3a\u6b64\u611f\u5230\u6cae\u4e27\uff0c\u8fd9\u5f88\u6b63\u5e38\u3002\u7cbe\u76ca\u6c42\u7cbe\u5728\u7f16\u7a0b\u4e2d\u662f\u4e00\u4e2a\u8fed\u4ee3\u7684\u8fc7\u7a0b\u3002 \u4f5c\u8005\u6ce8 \u8fd8\u6709\u4e00\u4ef6\u4e8b\uff1a\u4f60\u53ef\u80fd\u4f1a\u60f3\uff0c\u201dC++\u4e2d\u6709\u8fd9\u4e48\u591a\u7684\u89c4\u5219\u548c\u6982\u5ff5\uff0c\u6211\u600e\u4e48\u624d\u80fd\u8bb0\u4f4f\u5b83\u4eec\u5462\uff1f\u201c \u7b80\u5355\u6765\u8bf4\uff0c\u4f60\u5e76\u4e0d\u9700\u8981\u8bb0\u4f4f\u5b83\u4eec\u3002\u4f7f\u7528 C++\uff0c\u4e00\u65b9\u9762\u5728\u4e8e\u4f7f\u7528\u4f60\u5df2\u7ecf\u638c\u63e1\u7684\u77e5\u8bc6\uff0c\u53e6\u4e00\u65b9\u9762\u5728\u4e8e\u638c\u63e1\u5982\u4f55\u67e5\u627e\u5e76\u4f7f\u7528\u4f60\u6ca1\u6709\u638c\u63e1\u7684\u77e5\u8bc6\u3002 \u5f53\u4f60\u7b2c\u4e00\u6b21\u9605\u8bfb\u672c\u6559\u7a0b\u7684\u65f6\u5019\uff0c\u4e0d\u8981\u628a\u6ce8\u610f\u529b\u653e\u5728\u8bb0\u5fc6\u89c4\u5219\u4e0a\uff0c\u800c\u662f\u8981\u91cd\u70b9\u7406\u89e3\u4f7f\u7528C++\u4f60\u80fd\u505a\u54ea\u4e9b\u4e8b\u3002\u7136\u540e\uff0c\u5f53\u4f60\u771f\u6b63\u9700\u8981\u5728\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u67d0\u4e2a\u529f\u80fd\u7684\u65f6\u5019\uff0c\u4f60\u53ef\u4ee5\u518d\u56de\u6765\uff08\u6216\u8005\u5230\u5176\u4ed6\u7f51\u7ad9\uff09\u53bb\u5237\u65b0\u5de9\u56fa\u4f60\u7684\u77e5\u8bc6\u5e76\u638c\u63e1\u5e94\u8be5\u5982\u4f55\u53bb\u505a\u3002","title":"\u6700\u4f73\u65b9\u6848"},{"location":"1-2-comments/","tags":["comments"],"text":"Key Takeaway \u5728\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u7ea7\u522b\uff0c\u4f7f\u7528\u6ce8\u91ca\u6765\u63cf\u8ff0\u201c\u662f\u4ec0\u4e48\u201d\u3002 \u5728\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u5185\u90e8\uff0c\u4f7f\u7528\u6ce8\u91ca\u6765\u63cf\u8ff0\u201c\u600e\u4e48\u505a\u201d\u3002 \u5728\u8bed\u53e5\u7ea7\u522b\uff0c\u4f7f\u7528\u6ce8\u91ca\u6765\u63cf\u8ff0\u201c\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u505a\u201d\u3002 \u6ce8\u91ca\u662f\u76f4\u63a5\u63d2\u5165\u5230\u7a0b\u5e8f\u6e90\u4ee3\u7801\uff0c\u4f9b\u7a0b\u5e8f\u5458\u9605\u8bfb\u7684\u7b14\u8bb0\u3002\u6ce8\u91ca\u88ab\u7f16\u8bd1\u5668\u5ffd\u7565\uff0c\u4ec5\u4f9b\u7a0b\u5e8f\u5458\u4f7f\u7528\u3002 \u5728C++\u4e2d\u6709\u4e24\u79cd\u4e0d\u540c\u98ce\u683c\u7684\u6ce8\u91ca\uff0c\u5b83\u4eec\u90fd\u6709\u76f8\u540c\u7684\u76ee\u7684\uff1a\u5e2e\u52a9\u7a0b\u5e8f\u5458\u4ee5\u67d0\u79cd\u65b9\u5f0f\u8bb0\u5f55\u4ee3\u7801\u3002 \u5355\u884c\u6ce8\u91ca \u4f7f\u7528 // \u5f00\u5934\u53ef\u4ee5\u521b\u5efa\u5355\u884c\u6ce8\u91ca\uff0c\u8be5\u7b26\u53f7\u544a\u8bc9\u7f16\u8bd1\u5668\u5ffd\u7565\u8be5\u7b26\u53f7\u4e4b\u540e\u5230\u8be5\u884c\u672b\u5c3e\u7684\u5168\u90e8\u5185\u5bb9\u3002\u4f8b\u5982\uff1a 1 std :: cout << \"Hello world!\" ; // Everything from here to the end of the line is ignored \u4e00\u822c\u6765\u8bb2\uff0c\u5355\u884c\u6ce8\u91ca\u7528\u4e8e\u5bf9\u67d0\u884c\u4ee3\u7801\u8fdb\u884c\u5feb\u901f\u6ce8\u91ca\u3002 1 2 3 std :: cout << \"Hello world! \\n \" ; // std::cout lives in the iostream library std :: cout << \"It is very nice to meet you! \\n \" ; // these comments make the code hard to read std :: cout << \"Yeah! \\n \" ; // especially when lines are different lengths \u5c06\u6ce8\u91ca\u5199\u5728\u4ee3\u7801\u884c\u7684\u53f3\u4fa7\uff0c\u4f1a\u8ba9\u4ee3\u7801\u548c\u6ce8\u91ca\u7684\u53ef\u8bfb\u6027\u53d8\u5dee\uff0c\u5c24\u5176\u662f\u8fd9\u884c\u6bd4\u8f83\u957f\u7684\u60c5\u51b5\u3002\u5982\u679c\u8be5\u884c\u4e0d\u957f\uff0c\u6ce8\u91ca\u5c31\u653e\u5728\u540e\u9762\u5427\uff0c\u901a\u5e38\u653e\u7f6e\u5728\u4e0b\u4e00\u4e2a\u5236\u8868\u7b26\u7684\u4f4d\u7f6e\uff0c\u50cf\u8fd9\u6837\uff1a 1 2 3 std :: cout << \"Hello world! \\n \" ; // std::cout lives in the iostream library std :: cout << \"It is very nice to meet you! \\n \" ; // this is much easier to read std :: cout << \"Yeah! \\n \" ; // don't you think so? \u4f46\u662f\uff0c\u5982\u679c\u884c\u5f88\u957f\uff0c\u5c06\u6ce8\u91ca\u653e\u5728\u53f3\u8fb9\u4f1a\u4f7f\u8fd9\u4e00\u884c\u53d8\u5f97\u7279\u522b\u957f\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5355\u884c\u6ce8\u91ca\u901a\u5e38\u653e\u5728\u5b83\u6240\u6ce8\u91ca\u7684\u884c\u4e4b\u4e0a\uff1a 1 2 3 4 5 6 7 8 // std::cout lives in the iostream library std :: cout << \"Hello world! \\n \" ; // this is much easier to read std :: cout << \"It is very nice to meet you! \\n \" ; // don't you think so? std :: cout << \"Yeah! \\n \" ; \u4f5c\u8005\u6ce8 \u4e0a\u9762\u7684\u8bed\u53e5\u662f\u6211\u4eec\u7b2c\u4e00\u6b21\u9047\u5230\u201c\u4ee3\u7801\u7247\u6bb5\u201d\u3002\u201c\u4ee3\u7801\u7247\u6bb5\u201d\u4e0d\u662f\u5b8c\u6574\u7684\u7a0b\u5e8f\uff0c\u6240\u4ee5\u5b83\u4eec\u4e0d\u80fd\u88ab\u7f16\u8bd1\u3002\u5b83\u4eec\u7684\u5b58\u5728\u662f\u4e3a\u4e86\u4ee5\u7b80\u6d01\u7684\u65b9\u5f0f\u6f14\u793a\u7279\u5b9a\u7684\u6982\u5ff5\u3002 \u5982\u679c\u4f60\u60f3\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\u7247\u6bb5\uff0c\u5219\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5b8c\u6574\u7684\u7a0b\u5e8f\uff0c\u4ee5\u4fbf\u7f16\u8bd1\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u8be5\u7a0b\u5e8f\u770b\u8d77\u6765\u50cf\u8fd9\u6837: 1 2 3 4 5 6 7 8 #include int main () { // Replace this line with the snippet of code you'd like to compile return 0 ; } \u591a\u884c\u6ce8\u91ca /* \u548c */ \u7b26\u53f7\u5bf9\u8868\u793aC\u98ce\u683c\u7684\u591a\u884c\u6ce8\u91ca\u3002\u5728\u8fd9\u4e24\u4e2a\u7b26\u53f7\u4e4b\u95f4\u7684\u4efb\u610f\u5185\u5bb9\u90fd\u4f1a\u88ab\u7f16\u8bd1\u5668\u5ffd\u7565 1 2 3 /* This is a multi-line comment. This line will be ignored. So will this one. */ \u56e0\u4e3a\u7b26\u53f7\u4e4b\u95f4\u7684\u6240\u6709\u5185\u5bb9\u90fd\u88ab\u5ffd\u7565\u4e86\uff0c\u4f60\u6709\u65f6\u4f1a\u770b\u5230\u7a0b\u5e8f\u5458\u201c\u7f8e\u5316\u201d\u4ed6\u4eec\u7684\u591a\u884c\u6ce8\u91ca\uff1a 1 2 3 4 /* This is a multi-line comment. * the matching asterisks to the left * can make this easier to read */ \u591a\u884c\u6837\u5f0f\u7684\u6ce8\u91ca\u4e0d\u80fd\u5d4c\u5957\u3002\u56e0\u6b64\uff0c\u4ee5\u4e0b\u60c5\u51b5\u5c06\u4ea7\u751f\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\uff1a 1 2 /* This is a multi-line /* comment */ this is not inside the comment */ // The above comment ends at the first */, not the second */ \u5f53\u7f16\u8bd1\u5668\u5c1d\u8bd5\u7f16\u8bd1\u5b83\u65f6\uff0c\u5b83\u5c06\u5ffd\u7565\u4ece\u7b2c\u4e00\u4e2a /* \u5230\u7b2c\u4e00\u4e2a */ \u4e2d\u7684\u6240\u6709\u5185\u5bb9\u3002\u56e0\u4e3a\u201c this is not inside the comment */ \u201d\u4e0d\u88ab\u8ba4\u4e3a\u662f\u6ce8\u91ca\u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u7f16\u8bd1\u5b83\u3002\u8fd9\u5c06\u4e0d\u53ef\u907f\u514d\u5730\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8bed\u6cd5\u9ad8\u4eae\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a\u6ce8\u91ca\u5177\u6709\u7279\u5b9a\u7684\u989c\u8272\uff0c\u6240\u4ee5\u4e0d\u5c5e\u4e8e\u6ce8\u91ca\u7684\u4e00\u90e8\u5206\u5f88\u5bb9\u6613\u88ab\u770b\u51fa\u6765\u3002 \u6ce8\u610f \u4e0d\u8981\u5728\u5176\u4ed6\u591a\u884c\u6ce8\u91ca\u4e2d\u4f7f\u7528\u591a\u884c\u6ce8\u91ca\u3002\u5c06\u5355\u884c\u6ce8\u91ca\u5c01\u88c5\u5728\u591a\u884c\u6ce8\u91ca\u4e2d\u662f\u53ef\u4ee5\u7684\u3002 \u6b63\u786e\u5730\u4f7f\u7528\u6ce8\u91ca \u901a\u5e38\uff0c\u6ce8\u91ca\u5e94\u8be5\u7528\u4e8e\u5b8c\u6210\u4e09\u4ef6\u4e8b\u3002\u7b2c\u4e00\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u7ed9\u5b9a\u7684\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\uff0c\u6ce8\u91ca\u6700\u597d\u7528\u6765\u63cf\u8ff0\u8be5\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u7684\u529f\u80fd\u3002\u5b83\u4eec\u901a\u5e38\u653e\u5728\u6587\u4ef6\u6216\u5e93\u7684\u9876\u90e8\uff0c\u6216\u7d27\u6328\u7740\u51fd\u6570\u7684\u524d\u9762\u3002\u4f8b\u5982: 1 // This program calculates the student's final grade based on his test and homework scores. 1 // This function uses Newton's method to approximate the root of a given equation. 1 // The following lines generate a random item based on rarity, level, and a weight factor. \u6240\u6709\u8fd9\u4e9b\u6ce8\u91ca\u90fd\u53ef\u4ee5\u8ba9\u8bfb\u8005\u5728\u4e0d\u67e5\u770b\u5b9e\u9645\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u5f88\u597d\u5730\u4e86\u89e3\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u5b9e\u73b0\u4e86\u4ec0\u4e48\u529f\u80fd\u3002\u7528\u6237(\u53ef\u80fd\u662f\u5176\u4ed6\u4eba\uff0c\u5982\u679c\u4f60\u8bd5\u56fe\u91cd\u7528\u4ee5\u524d\u7f16\u5199\u7684\u4ee3\u7801\uff0c\u4e5f\u53ef\u80fd\u662f\u4f60\u81ea\u5df1)\u4e00\u773c\u5c31\u80fd\u770b\u51fa\u4ee3\u7801\u662f\u5426\u4e0e\u4ed6\u6216\u5979\u60f3\u8981\u5b9e\u73b0\u7684\u76ee\u6807\u76f8\u5173\u3002\u5728\u56e2\u961f\u4e2d\u5de5\u4f5c\u65f6\uff0c\u8fd9\u4e00\u70b9\u5c24\u5176\u91cd\u8981\uff0c\u56e0\u4e3a\u4e0d\u662f\u6bcf\u4e2a\u4eba\u90fd\u719f\u6089\u6240\u6709\u7684\u4ee3\u7801\u3002 \u7b2c\u4e8c\uff0c\u5728\u4e0a\u9762\u63cf\u8ff0\u7684\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u4e2d\uff0c\u6ce8\u91ca\u53ef\u4ee5\u7528\u6765\u63cf\u8ff0\u4ee3\u7801\u5c06\u5982\u4f55\u5b9e\u73b0\u5b83\u7684\u76ee\u6807\u3002 1 2 3 /* To calculate the final grade, we sum all the weighted midterm and homework scores and then divide by the number of scores to assign a percentage, which is used to calculate a letter grade. */ 1 2 3 4 5 6 // To generate a random item, we're going to do the following: // 1) Put all of the items of the desired rarity on a list // 2) Calculate a probability for each item based on level and weight factor // 3) Choose a random number // 4) Figure out which item that random number corresponds to // 5) Return the appropriate item \u8fd9\u4e9b\u6ce8\u91ca\u8ba9\u7528\u6237\u4e86\u89e3\u4ee3\u7801\u5c06\u5982\u4f55\u5b9e\u73b0\u5176\u76ee\u6807\uff0c\u800c\u4e0d\u5fc5\u4e86\u89e3\u6bcf\u4e00\u884c\u4ee3\u7801\u7684\u529f\u80fd\u3002 \u7b2c\u4e09\uff0c\u5728\u8bed\u53e5\u7ea7\u522b\uff0c\u6ce8\u91ca\u5e94\u8be5\u88ab\u7528\u6765\u63cf\u8ff0\u4ee3\u7801 \u4e3a\u4ec0\u4e48 \u505a\u67d0\u4e8b\uff0c\u800c\u4e0d\u662f\u88ab\u7528\u6765\u89e3\u91ca\u8be5\u884c\u4ee3\u7801\u662f\u5728 \u505a\u4ec0\u4e48\u4e8b \u3002\u5982\u679c\u4f60\u7f16\u5199\u7684\u4ee3\u7801\u975e\u5e38\u590d\u6742\uff0c\u4ee5\u81f3\u4e8e\u9700\u8981\u6ce8\u91ca\u6765\u89e3\u91ca\u8bed\u53e5\u5728\u505a\u4ec0\u4e48\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u91cd\u5199\u8bed\u53e5\uff0c\u800c\u4e0d\u662f\u4e3a\u5176\u6dfb\u52a0\u6ce8\u91ca\u3002 \u4e0b\u9762\u662f\u4e00\u4e9b\u5177\u4f53\u7684\u4f8b\u5b50\uff08\u6709\u597d\u6709\u574f\uff09\uff1a \u53cd\u9762\u6559\u6750\uff1a 1 2 // Set sight range to 0 sight = 0 ; \u7406\u7531\uff1a\u770b\u8bed\u53e5\u5c31\u77e5\u9053\u5b83\u662f\u5728\u7ed9 sight \u8d4b\u503c\uff0c\u65e0\u9700\u6ce8\u91ca\u3002 \u6b63\u786e\u793a\u4f8b\uff1a 1 2 // The player just drank a potion of blindness and can not see anything sight = 0 ; \u7406\u7531\uff1a\u8bf4\u660e\u4e3a\u4ec0\u4e48\u8981\u628a player \u7684 sight \u8bbe\u7f6e\u4e3a0\u3002 \u53cd\u9762\u6559\u6750\uff1a 1 2 // Calculate the cost of the items cost = quantity * 2 * storePrice ; \u7406\u7531\uff1a\u5f88\u663e\u7136\u662f\u5728\u8ba1\u7b97 cost \uff0c\u4f46\u662f\u6ca1\u6709\u89e3\u91ca\u4e3a\u4ec0\u4e48\u8981\u4e58\u4ee52\uff1f \u6b63\u786e\u793a\u4f8b\uff1a 1 2 // \u6211\u4eec\u9700\u8981\u5c06\u6570\u91cf\u4e58\u4ee52\u56e0\u4e3a\u5b83\u4eec\u662f\u6210\u5bf9\u8d2d\u4e70\u7684 cost = quantity * 2 * storePrice ; \u7406\u7531\uff1a\u8fd9\u6837\u5c31\u80fd\u7406\u89e3\u516c\u5f0f\u7684\u542b\u4e49\u4e86\u3002 \u7a0b\u5e8f\u5458\u5e38\u5e38\u4e0d\u5f97\u4e0d\u5728\u7528\u4e00\u79cd\u65b9\u6cd5\u89e3\u51b3\u95ee\u9898\uff0c\u8fd8\u662f\u7528\u53e6\u4e00\u79cd\u65b9\u6cd5\u89e3\u51b3\u95ee\u9898\u4e4b\u95f4\u505a\u51fa\u8270\u96be\u7684\u51b3\u5b9a\u3002\u6ce8\u91ca\u662f\u63d0\u9192\u4f60\u81ea\u5df1(\u6216\u544a\u8bc9\u522b\u4eba)\u505a\u51fa\u8fd9\u4e2a\u51b3\u5b9a\u800c\u4e0d\u662f\u53e6\u4e00\u4e2a\u51b3\u5b9a\u7684\u539f\u56e0\u7684\u597d\u65b9\u6cd5\u3002 \u6b63\u786e\u793a\u4f8b\uff1a 1 2 // We decided to use a linked list instead of an array because // arrays do insertion too slowly. 1 2 // We're going to use Newton's method to find the root of a number because // there is no deterministic way to solve these equations. \u6700\u540e\uff0c\u5e94\u8be5\u4ee5\u4e00\u79cd\u5bf9\u4e0d\u77e5\u9053\u4ee3\u7801\u662f\u505a\u4ec0\u4e48\u7684\u4eba\u6765\u8bf4\u6709\u610f\u4e49\u7684\u65b9\u5f0f\u7f16\u5199\u6ce8\u91ca\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u5458\u4f1a\u8bf4:\u201c\u8fd9\u5f88\u660e\u663e\uff01\u6211\u4e0d\u4f1a\u5fd8\u8bb0\u8fd9\u4ef6\u4e8b\u7684\u3002\u201d\u4f60\u731c\u600e\u4e48\u7740\uff1f\u8fd9\u5e76\u4e0d\u660e\u663e\uff0c\u4f60\u4f1a\u60ca\u8bb6\u4e8e\u81ea\u5df1\u6709\u591a\u5065\u5fd8\u3002\u4f60(\u6216\u5176\u4ed6\u4eba)\u4ee5\u540e\u4f1a\u611f\u8c22\u4f60\u7528\u4eba\u7c7b\u8bed\u8a00\u5199\u4e0b\u4ee3\u7801\u7684\u5185\u5bb9\u3001\u65b9\u5f0f\u548c\u539f\u56e0\u3002\u9605\u8bfb\u4e00\u884c\u884c\u4ee3\u7801\u5f88\u5bb9\u6613\uff0c\u4f46\u7406\u89e3\u5176\u8981\u5b8c\u6210\u7684\u76ee\u6807\u5374\u4e0d\u7b80\u5355\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u4f60\u7684\u4ee3\u7801\u505a\u5927\u91cf\u7684\u6ce8\u91ca\uff0c\u5199\u6ce8\u91ca\u65f6\u5c31\u50cf\u5bf9\u4e00\u4e2a\u4e0d\u77e5\u9053\u4ee3\u7801\u662f\u505a\u4ec0\u4e48\u7684\u4eba\u8bf4\u8bdd\u4e00\u6837\u3002\u4e0d\u8981\u4ee5\u4e3a\u4f60\u4f1a\u8bb0\u5f97\u4e3a\u4ec0\u4e48\u4f60\u505a\u4e86\u7279\u5b9a\u7684\u9009\u62e9\u3002 \u4f5c\u8005\u6ce8 \u5728\u672c\u7cfb\u5217\u6559\u7a0b\u7684\u5176\u4f59\u90e8\u5206\u4e2d\uff0c\u6211\u4eec\u5c06\u5728\u4ee3\u7801\u5757\u4e2d\u4f7f\u7528\u6ce8\u91ca\u6765\u8ba9\u4f60\u5173\u6ce8\u7279\u5b9a\u7684\u5185\u5bb9\uff0c\u6216\u8005\u5e2e\u52a9\u8bf4\u660e\u4ee3\u7801\u662f\u5982\u4f55\u5de5\u4f5c\u7684(\u540c\u65f6\u786e\u4fdd\u7a0b\u5e8f\u80fd\u591f\u7f16\u8bd1)\u3002\u7cbe\u660e\u7684\u8bfb\u8005\u4f1a\u6ce8\u610f\u5230\uff0c\u6309\u7167\u4e0a\u8ff0\u6807\u51c6\uff0c\u5927\u591a\u6570\u8bc4\u8bba\u90fd\u5f88\u7cdf\u7cd5\u3002:)\u5f53\u4f60\u9605\u8bfb\u5269\u4e0b\u7684\u6559\u7a0b\u65f6\uff0c\u8bf7\u8bb0\u4f4f\uff0c\u8fd9\u4e9b\u6ce8\u91ca\u662f\u4e3a\u6559\u80b2\u76ee\u7684\u670d\u52a1\u7684\uff0c\u800c\u4e0d\u662f\u8bd5\u56fe\u6f14\u793a\u597d\u7684\u6ce8\u91ca\u662f\u4ec0\u4e48\u6837\u5b50\u7684\u3002 \u5c06\u4ee3\u7801\u6ce8\u91ca\u6389 \u5c06\u4e00\u884c\u6216\u591a\u884c\u4ee3\u7801\u8f6c\u6362\u4e3a\u6ce8\u91ca\u79f0\u4e3a\u6ce8\u91ca\u6389\u4ee3\u7801\u3002\u8fd9\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u7684\u65b9\u6cd5(\u4e34\u65f6)\u5c06\u90e8\u5206\u4ee3\u7801\u6392\u9664\u5728\u7f16\u8bd1\u540e\u7684\u7a0b\u5e8f\u4e2d\u3002 \u8981\u6ce8\u91ca\u6389\u4e00\u884c\u4ee3\u7801\uff0c\u53ea\u9700\u4f7f\u7528 // \u6837\u5f0f\u6ce8\u91ca\u5c06\u4e00\u884c\u4ee3\u7801\u4e34\u65f6\u53d8\u6210\u6ce8\u91ca: \u672a\u6ce8\u91ca\u6389\uff1a 1 std :: cout << 1 ; \u6ce8\u91ca\u6389\uff1a 1 // std::cout << 1; \u8981\u6ce8\u91ca\u6389\u4ee3\u7801\u5757\uff0c\u53ef\u4ee5\u5728\u591a\u884c\u4ee3\u7801\u4e0a\u4f7f\u7528 // \uff0c\u6216\u8005\u4f7f\u7528 /* */ \u6ce8\u91ca\u5c06\u4ee3\u7801\u5757\u4e34\u65f6\u8f6c\u6362\u4e3a\u6ce8\u91ca\u3002 \u672a\u6ce8\u91ca\u6389\uff1a 1 2 3 std :: cout << 1 ; std :: cout << 2 ; std :: cout << 3 ; \u6ce8\u91ca\u6389\uff1a 1 2 3 // std::cout << 1; // std::cout << 2; // std::cout << 3; \u6216 1 2 3 4 5 /* std::cout << 1; std::cout << 2; std::cout << 3; */ \u4f60\u9700\u8981\u8fd9\u4e48\u505a\u7684\u539f\u56e0\u6709\u5f88\u591a\uff1a \u4f60\u6b63\u5728\u5904\u7406\u4e00\u6bb5\u5c1a\u672a\u7f16\u8bd1\u7684\u65b0\u4ee3\u7801\uff0c\u9700\u8981\u8fd0\u884c\u8be5\u7a0b\u5e8f\u3002\u5982\u679c\u6709\u7f16\u8bd1\u5668\u9519\u8bef\uff0c\u7f16\u8bd1\u5668\u5c06\u4e0d\u5141\u8bb8\u7f16\u8bd1\u4ee3\u7801\u3002\u6ce8\u91ca\u6389\u4e0d\u80fd\u7f16\u8bd1\u7684\u4ee3\u7801\u5c06\u5141\u8bb8\u7a0b\u5e8f\u7f16\u8bd1\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u518d\u6b21\u8fd0\u884c\u3002\u51c6\u5907\u597d\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u53d6\u6d88\u5bf9\u4ee3\u7801\u7684\u6ce8\u91ca\uff0c\u5e76\u7ee7\u7eed\u5904\u7406\u5b83\u3002 \u4f60\u5df2\u7ecf\u7f16\u5199\u4e86\u53ef\u7f16\u8bd1\u4f46\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u7684\u65b0\u4ee3\u7801\uff0c\u5e76\u4e14\u76f4\u5230\u4ee5\u540e\u624d\u6709\u65f6\u95f4\u4fee\u590d\u5b83\u3002\u6ce8\u91ca\u6389\u635f\u574f\u7684\u4ee3\u7801\u5c06\u786e\u4fdd\u635f\u574f\u7684\u4ee3\u7801\u4e0d\u4f1a\u6267\u884c\u5e76\u5bfc\u81f4\u95ee\u9898\uff0c\u76f4\u5230\u4f60\u80fd\u591f\u4fee\u590d\u5b83\u3002 \u627e\u5230\u9519\u8bef\u7684\u6765\u6e90\u3002\u5982\u679c\u7a0b\u5e8f\u6ca1\u6709\u4ea7\u751f\u9884\u671f\u7684\u7ed3\u679c(\u6216\u5d29\u6e83)\uff0c\u6709\u65f6\u53ef\u4ee5\u7981\u7528\u90e8\u5206\u4ee3\u7801\uff0c\u770b\u770b\u662f\u5426\u53ef\u4ee5\u5206\u79bb\u51fa\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\u7684\u539f\u56e0\u3002\u5982\u679c\u6ce8\u91ca\u6389\u4e86\u4e00\u884c\u6216\u591a\u884c\u4ee3\u7801\u540e\u7a0b\u5e8f\u5f00\u59cb\u6309\u9884\u671f\u8fd0\u884c(\u6216\u505c\u6b62\u5d29\u6e83)\uff0c\u90a3\u4e48\u9519\u8bef\u53ef\u80fd\u4f4d\u4e8e\u6700\u540e\u6ce8\u91ca\u6389\u7684\u4ee3\u7801\u4e2d\u3002 \u4f60\u5e0c\u671b\u7528\u53e6\u4e00\u6bb5\u4ee3\u7801\u66ff\u6362\u4e00\u6bb5\u4ee3\u7801\uff0c\u4f46\u4f60\u6682\u65f6\u4e0d\u60f3\u5220\u9664\u539f\u4ee3\u7801\uff0c\u6b64\u65f6\u53ef\u4ee5\u5c06\u5176\u5148\u6ce8\u91ca\u6389\uff0c\u4fdd\u7559\u5728\u90a3\u91cc\u4ee5\u4f9b\u53c2\u8003\uff0c\u76f4\u5230\u65b0\u4ee3\u7801\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u4e3a\u6b62\u3002\u4e00\u65e6\u786e\u5b9a\u65b0\u4ee3\u7801\u6b63\u5e38\u5de5\u4f5c\uff0c\u5c31\u53ef\u4ee5\u5220\u9664\u6ce8\u91ca\u6389\u7684\u65e7\u4ee3\u7801\u3002\u5982\u679c\u4f60\u4e0d\u80fd\u8ba9\u4f60\u7684\u65b0\u4ee3\u7801\u5de5\u4f5c\uff0c\u4f60\u603b\u662f\u53ef\u4ee5\u5220\u9664\u65b0\u4ee3\u7801\u548c\u53d6\u6d88\u6ce8\u91ca\u65e7\u4ee3\u7801\u6062\u590d\u5230\u4f60\u4ee5\u524d\u7684\u4ee3\u7801\u3002 \u6ce8\u91ca\u6389\u4ee3\u7801\u662f\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u5e38\u89c1\u7684\u4e8b\u60c5\uff0c\u56e0\u6b64\u8bb8\u591aIDE\u90fd\u652f\u6301\u6ce8\u91ca\u6389\u9009\u4e2d\u7684\u4ee3\u7801\u90e8\u5206\u3002\u4f7f\u7528\u65b9\u5f0f\u56e0 IDE \u800c\u5f02\u3002 For Visual Studio users \u4f60\u53ef\u4ee5\u901a\u8fc7\u7f16\u8f91\u83dc\u5355>\u9ad8\u7ea7>\u6ce8\u91ca\u9009\u62e9(\u6216\u53d6\u6d88\u6ce8\u91ca\u9009\u62e9)\u6765\u6ce8\u91ca\u6216\u53d6\u6d88\u6ce8\u91ca\u9009\u62e9\u3002 For Code::Blocks users \u4f60\u53ef\u4ee5\u901a\u8fc7\u7f16\u8f91\u83dc\u5355>\u6ce8\u91ca\u6765\u6ce8\u91ca\u6216\u53d6\u6d88\u6ce8\u91ca\u9009\u62e9\u3002 \u5c0f\u8d34\u58eb \u5982\u679c\u4f60\u53ea\u4f7f\u7528\u5355\u884c\u6ce8\u91ca\uff0c\u90a3\u4e48\u4f60\u603b\u662f\u53ef\u4ee5\u4f7f\u7528\u591a\u884c\u6ce8\u91ca\u6765\u6ce8\u91ca\u6389\u4ee3\u7801\u800c\u4e0d\u4ea7\u751f\u51b2\u7a81\u3002\u53cd\u4e4b\u5219\u4f1a\u6bd4\u8f83\u9ebb\u70e6\u3002 \u5982\u679c\u4f60\u786e\u5b9e\u9700\u8981\u6ce8\u91ca\u6389\u4e00\u4e2a\u5305\u542b\u591a\u884c\u6ce8\u91ca\u7684\u4ee3\u7801\u5757\uff0c\u4f60\u4e5f\u53ef\u4ee5\u8003\u8651\u4f7f\u7528 # if 0 \u9884\u5904\u7406\u5668\u6307\u4ee4 2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb \u5c0f\u7ed3 \u5728\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u7ea7\u522b\uff0c\u4f7f\u7528\u6ce8\u91ca\u6765\u63cf\u8ff0\u201c\u662f\u4ec0\u4e48\u201d\u3002 \u5728\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u5185\u90e8\uff0c\u4f7f\u7528\u6ce8\u91ca\u6765\u63cf\u8ff0\u201c\u600e\u4e48\u505a\u201d\u3002 \u5728\u8bed\u53e5\u7ea7\u522b\uff0c\u4f7f\u7528\u6ce8\u91ca\u6765\u63cf\u8ff0\u201c\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u505a\u201d\u3002","title":"1.2 - \u6ce8\u91ca"},{"location":"1-2-comments/#_1","text":"\u4f7f\u7528 // \u5f00\u5934\u53ef\u4ee5\u521b\u5efa\u5355\u884c\u6ce8\u91ca\uff0c\u8be5\u7b26\u53f7\u544a\u8bc9\u7f16\u8bd1\u5668\u5ffd\u7565\u8be5\u7b26\u53f7\u4e4b\u540e\u5230\u8be5\u884c\u672b\u5c3e\u7684\u5168\u90e8\u5185\u5bb9\u3002\u4f8b\u5982\uff1a 1 std :: cout << \"Hello world!\" ; // Everything from here to the end of the line is ignored \u4e00\u822c\u6765\u8bb2\uff0c\u5355\u884c\u6ce8\u91ca\u7528\u4e8e\u5bf9\u67d0\u884c\u4ee3\u7801\u8fdb\u884c\u5feb\u901f\u6ce8\u91ca\u3002 1 2 3 std :: cout << \"Hello world! \\n \" ; // std::cout lives in the iostream library std :: cout << \"It is very nice to meet you! \\n \" ; // these comments make the code hard to read std :: cout << \"Yeah! \\n \" ; // especially when lines are different lengths \u5c06\u6ce8\u91ca\u5199\u5728\u4ee3\u7801\u884c\u7684\u53f3\u4fa7\uff0c\u4f1a\u8ba9\u4ee3\u7801\u548c\u6ce8\u91ca\u7684\u53ef\u8bfb\u6027\u53d8\u5dee\uff0c\u5c24\u5176\u662f\u8fd9\u884c\u6bd4\u8f83\u957f\u7684\u60c5\u51b5\u3002\u5982\u679c\u8be5\u884c\u4e0d\u957f\uff0c\u6ce8\u91ca\u5c31\u653e\u5728\u540e\u9762\u5427\uff0c\u901a\u5e38\u653e\u7f6e\u5728\u4e0b\u4e00\u4e2a\u5236\u8868\u7b26\u7684\u4f4d\u7f6e\uff0c\u50cf\u8fd9\u6837\uff1a 1 2 3 std :: cout << \"Hello world! \\n \" ; // std::cout lives in the iostream library std :: cout << \"It is very nice to meet you! \\n \" ; // this is much easier to read std :: cout << \"Yeah! \\n \" ; // don't you think so? \u4f46\u662f\uff0c\u5982\u679c\u884c\u5f88\u957f\uff0c\u5c06\u6ce8\u91ca\u653e\u5728\u53f3\u8fb9\u4f1a\u4f7f\u8fd9\u4e00\u884c\u53d8\u5f97\u7279\u522b\u957f\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5355\u884c\u6ce8\u91ca\u901a\u5e38\u653e\u5728\u5b83\u6240\u6ce8\u91ca\u7684\u884c\u4e4b\u4e0a\uff1a 1 2 3 4 5 6 7 8 // std::cout lives in the iostream library std :: cout << \"Hello world! \\n \" ; // this is much easier to read std :: cout << \"It is very nice to meet you! \\n \" ; // don't you think so? std :: cout << \"Yeah! \\n \" ; \u4f5c\u8005\u6ce8 \u4e0a\u9762\u7684\u8bed\u53e5\u662f\u6211\u4eec\u7b2c\u4e00\u6b21\u9047\u5230\u201c\u4ee3\u7801\u7247\u6bb5\u201d\u3002\u201c\u4ee3\u7801\u7247\u6bb5\u201d\u4e0d\u662f\u5b8c\u6574\u7684\u7a0b\u5e8f\uff0c\u6240\u4ee5\u5b83\u4eec\u4e0d\u80fd\u88ab\u7f16\u8bd1\u3002\u5b83\u4eec\u7684\u5b58\u5728\u662f\u4e3a\u4e86\u4ee5\u7b80\u6d01\u7684\u65b9\u5f0f\u6f14\u793a\u7279\u5b9a\u7684\u6982\u5ff5\u3002 \u5982\u679c\u4f60\u60f3\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\u7247\u6bb5\uff0c\u5219\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5b8c\u6574\u7684\u7a0b\u5e8f\uff0c\u4ee5\u4fbf\u7f16\u8bd1\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u8be5\u7a0b\u5e8f\u770b\u8d77\u6765\u50cf\u8fd9\u6837: 1 2 3 4 5 6 7 8 #include int main () { // Replace this line with the snippet of code you'd like to compile return 0 ; }","title":"\u5355\u884c\u6ce8\u91ca"},{"location":"1-2-comments/#_2","text":"/* \u548c */ \u7b26\u53f7\u5bf9\u8868\u793aC\u98ce\u683c\u7684\u591a\u884c\u6ce8\u91ca\u3002\u5728\u8fd9\u4e24\u4e2a\u7b26\u53f7\u4e4b\u95f4\u7684\u4efb\u610f\u5185\u5bb9\u90fd\u4f1a\u88ab\u7f16\u8bd1\u5668\u5ffd\u7565 1 2 3 /* This is a multi-line comment. This line will be ignored. So will this one. */ \u56e0\u4e3a\u7b26\u53f7\u4e4b\u95f4\u7684\u6240\u6709\u5185\u5bb9\u90fd\u88ab\u5ffd\u7565\u4e86\uff0c\u4f60\u6709\u65f6\u4f1a\u770b\u5230\u7a0b\u5e8f\u5458\u201c\u7f8e\u5316\u201d\u4ed6\u4eec\u7684\u591a\u884c\u6ce8\u91ca\uff1a 1 2 3 4 /* This is a multi-line comment. * the matching asterisks to the left * can make this easier to read */ \u591a\u884c\u6837\u5f0f\u7684\u6ce8\u91ca\u4e0d\u80fd\u5d4c\u5957\u3002\u56e0\u6b64\uff0c\u4ee5\u4e0b\u60c5\u51b5\u5c06\u4ea7\u751f\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\uff1a 1 2 /* This is a multi-line /* comment */ this is not inside the comment */ // The above comment ends at the first */, not the second */ \u5f53\u7f16\u8bd1\u5668\u5c1d\u8bd5\u7f16\u8bd1\u5b83\u65f6\uff0c\u5b83\u5c06\u5ffd\u7565\u4ece\u7b2c\u4e00\u4e2a /* \u5230\u7b2c\u4e00\u4e2a */ \u4e2d\u7684\u6240\u6709\u5185\u5bb9\u3002\u56e0\u4e3a\u201c this is not inside the comment */ \u201d\u4e0d\u88ab\u8ba4\u4e3a\u662f\u6ce8\u91ca\u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u7f16\u8bd1\u5b83\u3002\u8fd9\u5c06\u4e0d\u53ef\u907f\u514d\u5730\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8bed\u6cd5\u9ad8\u4eae\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a\u6ce8\u91ca\u5177\u6709\u7279\u5b9a\u7684\u989c\u8272\uff0c\u6240\u4ee5\u4e0d\u5c5e\u4e8e\u6ce8\u91ca\u7684\u4e00\u90e8\u5206\u5f88\u5bb9\u6613\u88ab\u770b\u51fa\u6765\u3002 \u6ce8\u610f \u4e0d\u8981\u5728\u5176\u4ed6\u591a\u884c\u6ce8\u91ca\u4e2d\u4f7f\u7528\u591a\u884c\u6ce8\u91ca\u3002\u5c06\u5355\u884c\u6ce8\u91ca\u5c01\u88c5\u5728\u591a\u884c\u6ce8\u91ca\u4e2d\u662f\u53ef\u4ee5\u7684\u3002","title":"\u591a\u884c\u6ce8\u91ca"},{"location":"1-2-comments/#_3","text":"\u901a\u5e38\uff0c\u6ce8\u91ca\u5e94\u8be5\u7528\u4e8e\u5b8c\u6210\u4e09\u4ef6\u4e8b\u3002\u7b2c\u4e00\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u7ed9\u5b9a\u7684\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\uff0c\u6ce8\u91ca\u6700\u597d\u7528\u6765\u63cf\u8ff0\u8be5\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u7684\u529f\u80fd\u3002\u5b83\u4eec\u901a\u5e38\u653e\u5728\u6587\u4ef6\u6216\u5e93\u7684\u9876\u90e8\uff0c\u6216\u7d27\u6328\u7740\u51fd\u6570\u7684\u524d\u9762\u3002\u4f8b\u5982: 1 // This program calculates the student's final grade based on his test and homework scores. 1 // This function uses Newton's method to approximate the root of a given equation. 1 // The following lines generate a random item based on rarity, level, and a weight factor. \u6240\u6709\u8fd9\u4e9b\u6ce8\u91ca\u90fd\u53ef\u4ee5\u8ba9\u8bfb\u8005\u5728\u4e0d\u67e5\u770b\u5b9e\u9645\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u5f88\u597d\u5730\u4e86\u89e3\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u5b9e\u73b0\u4e86\u4ec0\u4e48\u529f\u80fd\u3002\u7528\u6237(\u53ef\u80fd\u662f\u5176\u4ed6\u4eba\uff0c\u5982\u679c\u4f60\u8bd5\u56fe\u91cd\u7528\u4ee5\u524d\u7f16\u5199\u7684\u4ee3\u7801\uff0c\u4e5f\u53ef\u80fd\u662f\u4f60\u81ea\u5df1)\u4e00\u773c\u5c31\u80fd\u770b\u51fa\u4ee3\u7801\u662f\u5426\u4e0e\u4ed6\u6216\u5979\u60f3\u8981\u5b9e\u73b0\u7684\u76ee\u6807\u76f8\u5173\u3002\u5728\u56e2\u961f\u4e2d\u5de5\u4f5c\u65f6\uff0c\u8fd9\u4e00\u70b9\u5c24\u5176\u91cd\u8981\uff0c\u56e0\u4e3a\u4e0d\u662f\u6bcf\u4e2a\u4eba\u90fd\u719f\u6089\u6240\u6709\u7684\u4ee3\u7801\u3002 \u7b2c\u4e8c\uff0c\u5728\u4e0a\u9762\u63cf\u8ff0\u7684\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u4e2d\uff0c\u6ce8\u91ca\u53ef\u4ee5\u7528\u6765\u63cf\u8ff0\u4ee3\u7801\u5c06\u5982\u4f55\u5b9e\u73b0\u5b83\u7684\u76ee\u6807\u3002 1 2 3 /* To calculate the final grade, we sum all the weighted midterm and homework scores and then divide by the number of scores to assign a percentage, which is used to calculate a letter grade. */ 1 2 3 4 5 6 // To generate a random item, we're going to do the following: // 1) Put all of the items of the desired rarity on a list // 2) Calculate a probability for each item based on level and weight factor // 3) Choose a random number // 4) Figure out which item that random number corresponds to // 5) Return the appropriate item \u8fd9\u4e9b\u6ce8\u91ca\u8ba9\u7528\u6237\u4e86\u89e3\u4ee3\u7801\u5c06\u5982\u4f55\u5b9e\u73b0\u5176\u76ee\u6807\uff0c\u800c\u4e0d\u5fc5\u4e86\u89e3\u6bcf\u4e00\u884c\u4ee3\u7801\u7684\u529f\u80fd\u3002 \u7b2c\u4e09\uff0c\u5728\u8bed\u53e5\u7ea7\u522b\uff0c\u6ce8\u91ca\u5e94\u8be5\u88ab\u7528\u6765\u63cf\u8ff0\u4ee3\u7801 \u4e3a\u4ec0\u4e48 \u505a\u67d0\u4e8b\uff0c\u800c\u4e0d\u662f\u88ab\u7528\u6765\u89e3\u91ca\u8be5\u884c\u4ee3\u7801\u662f\u5728 \u505a\u4ec0\u4e48\u4e8b \u3002\u5982\u679c\u4f60\u7f16\u5199\u7684\u4ee3\u7801\u975e\u5e38\u590d\u6742\uff0c\u4ee5\u81f3\u4e8e\u9700\u8981\u6ce8\u91ca\u6765\u89e3\u91ca\u8bed\u53e5\u5728\u505a\u4ec0\u4e48\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u91cd\u5199\u8bed\u53e5\uff0c\u800c\u4e0d\u662f\u4e3a\u5176\u6dfb\u52a0\u6ce8\u91ca\u3002 \u4e0b\u9762\u662f\u4e00\u4e9b\u5177\u4f53\u7684\u4f8b\u5b50\uff08\u6709\u597d\u6709\u574f\uff09\uff1a \u53cd\u9762\u6559\u6750\uff1a 1 2 // Set sight range to 0 sight = 0 ; \u7406\u7531\uff1a\u770b\u8bed\u53e5\u5c31\u77e5\u9053\u5b83\u662f\u5728\u7ed9 sight \u8d4b\u503c\uff0c\u65e0\u9700\u6ce8\u91ca\u3002 \u6b63\u786e\u793a\u4f8b\uff1a 1 2 // The player just drank a potion of blindness and can not see anything sight = 0 ; \u7406\u7531\uff1a\u8bf4\u660e\u4e3a\u4ec0\u4e48\u8981\u628a player \u7684 sight \u8bbe\u7f6e\u4e3a0\u3002 \u53cd\u9762\u6559\u6750\uff1a 1 2 // Calculate the cost of the items cost = quantity * 2 * storePrice ; \u7406\u7531\uff1a\u5f88\u663e\u7136\u662f\u5728\u8ba1\u7b97 cost \uff0c\u4f46\u662f\u6ca1\u6709\u89e3\u91ca\u4e3a\u4ec0\u4e48\u8981\u4e58\u4ee52\uff1f \u6b63\u786e\u793a\u4f8b\uff1a 1 2 // \u6211\u4eec\u9700\u8981\u5c06\u6570\u91cf\u4e58\u4ee52\u56e0\u4e3a\u5b83\u4eec\u662f\u6210\u5bf9\u8d2d\u4e70\u7684 cost = quantity * 2 * storePrice ; \u7406\u7531\uff1a\u8fd9\u6837\u5c31\u80fd\u7406\u89e3\u516c\u5f0f\u7684\u542b\u4e49\u4e86\u3002 \u7a0b\u5e8f\u5458\u5e38\u5e38\u4e0d\u5f97\u4e0d\u5728\u7528\u4e00\u79cd\u65b9\u6cd5\u89e3\u51b3\u95ee\u9898\uff0c\u8fd8\u662f\u7528\u53e6\u4e00\u79cd\u65b9\u6cd5\u89e3\u51b3\u95ee\u9898\u4e4b\u95f4\u505a\u51fa\u8270\u96be\u7684\u51b3\u5b9a\u3002\u6ce8\u91ca\u662f\u63d0\u9192\u4f60\u81ea\u5df1(\u6216\u544a\u8bc9\u522b\u4eba)\u505a\u51fa\u8fd9\u4e2a\u51b3\u5b9a\u800c\u4e0d\u662f\u53e6\u4e00\u4e2a\u51b3\u5b9a\u7684\u539f\u56e0\u7684\u597d\u65b9\u6cd5\u3002 \u6b63\u786e\u793a\u4f8b\uff1a 1 2 // We decided to use a linked list instead of an array because // arrays do insertion too slowly. 1 2 // We're going to use Newton's method to find the root of a number because // there is no deterministic way to solve these equations. \u6700\u540e\uff0c\u5e94\u8be5\u4ee5\u4e00\u79cd\u5bf9\u4e0d\u77e5\u9053\u4ee3\u7801\u662f\u505a\u4ec0\u4e48\u7684\u4eba\u6765\u8bf4\u6709\u610f\u4e49\u7684\u65b9\u5f0f\u7f16\u5199\u6ce8\u91ca\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u5458\u4f1a\u8bf4:\u201c\u8fd9\u5f88\u660e\u663e\uff01\u6211\u4e0d\u4f1a\u5fd8\u8bb0\u8fd9\u4ef6\u4e8b\u7684\u3002\u201d\u4f60\u731c\u600e\u4e48\u7740\uff1f\u8fd9\u5e76\u4e0d\u660e\u663e\uff0c\u4f60\u4f1a\u60ca\u8bb6\u4e8e\u81ea\u5df1\u6709\u591a\u5065\u5fd8\u3002\u4f60(\u6216\u5176\u4ed6\u4eba)\u4ee5\u540e\u4f1a\u611f\u8c22\u4f60\u7528\u4eba\u7c7b\u8bed\u8a00\u5199\u4e0b\u4ee3\u7801\u7684\u5185\u5bb9\u3001\u65b9\u5f0f\u548c\u539f\u56e0\u3002\u9605\u8bfb\u4e00\u884c\u884c\u4ee3\u7801\u5f88\u5bb9\u6613\uff0c\u4f46\u7406\u89e3\u5176\u8981\u5b8c\u6210\u7684\u76ee\u6807\u5374\u4e0d\u7b80\u5355\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u4f60\u7684\u4ee3\u7801\u505a\u5927\u91cf\u7684\u6ce8\u91ca\uff0c\u5199\u6ce8\u91ca\u65f6\u5c31\u50cf\u5bf9\u4e00\u4e2a\u4e0d\u77e5\u9053\u4ee3\u7801\u662f\u505a\u4ec0\u4e48\u7684\u4eba\u8bf4\u8bdd\u4e00\u6837\u3002\u4e0d\u8981\u4ee5\u4e3a\u4f60\u4f1a\u8bb0\u5f97\u4e3a\u4ec0\u4e48\u4f60\u505a\u4e86\u7279\u5b9a\u7684\u9009\u62e9\u3002 \u4f5c\u8005\u6ce8 \u5728\u672c\u7cfb\u5217\u6559\u7a0b\u7684\u5176\u4f59\u90e8\u5206\u4e2d\uff0c\u6211\u4eec\u5c06\u5728\u4ee3\u7801\u5757\u4e2d\u4f7f\u7528\u6ce8\u91ca\u6765\u8ba9\u4f60\u5173\u6ce8\u7279\u5b9a\u7684\u5185\u5bb9\uff0c\u6216\u8005\u5e2e\u52a9\u8bf4\u660e\u4ee3\u7801\u662f\u5982\u4f55\u5de5\u4f5c\u7684(\u540c\u65f6\u786e\u4fdd\u7a0b\u5e8f\u80fd\u591f\u7f16\u8bd1)\u3002\u7cbe\u660e\u7684\u8bfb\u8005\u4f1a\u6ce8\u610f\u5230\uff0c\u6309\u7167\u4e0a\u8ff0\u6807\u51c6\uff0c\u5927\u591a\u6570\u8bc4\u8bba\u90fd\u5f88\u7cdf\u7cd5\u3002:)\u5f53\u4f60\u9605\u8bfb\u5269\u4e0b\u7684\u6559\u7a0b\u65f6\uff0c\u8bf7\u8bb0\u4f4f\uff0c\u8fd9\u4e9b\u6ce8\u91ca\u662f\u4e3a\u6559\u80b2\u76ee\u7684\u670d\u52a1\u7684\uff0c\u800c\u4e0d\u662f\u8bd5\u56fe\u6f14\u793a\u597d\u7684\u6ce8\u91ca\u662f\u4ec0\u4e48\u6837\u5b50\u7684\u3002","title":"\u6b63\u786e\u5730\u4f7f\u7528\u6ce8\u91ca"},{"location":"1-2-comments/#_4","text":"\u5c06\u4e00\u884c\u6216\u591a\u884c\u4ee3\u7801\u8f6c\u6362\u4e3a\u6ce8\u91ca\u79f0\u4e3a\u6ce8\u91ca\u6389\u4ee3\u7801\u3002\u8fd9\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u7684\u65b9\u6cd5(\u4e34\u65f6)\u5c06\u90e8\u5206\u4ee3\u7801\u6392\u9664\u5728\u7f16\u8bd1\u540e\u7684\u7a0b\u5e8f\u4e2d\u3002 \u8981\u6ce8\u91ca\u6389\u4e00\u884c\u4ee3\u7801\uff0c\u53ea\u9700\u4f7f\u7528 // \u6837\u5f0f\u6ce8\u91ca\u5c06\u4e00\u884c\u4ee3\u7801\u4e34\u65f6\u53d8\u6210\u6ce8\u91ca: \u672a\u6ce8\u91ca\u6389\uff1a 1 std :: cout << 1 ; \u6ce8\u91ca\u6389\uff1a 1 // std::cout << 1; \u8981\u6ce8\u91ca\u6389\u4ee3\u7801\u5757\uff0c\u53ef\u4ee5\u5728\u591a\u884c\u4ee3\u7801\u4e0a\u4f7f\u7528 // \uff0c\u6216\u8005\u4f7f\u7528 /* */ \u6ce8\u91ca\u5c06\u4ee3\u7801\u5757\u4e34\u65f6\u8f6c\u6362\u4e3a\u6ce8\u91ca\u3002 \u672a\u6ce8\u91ca\u6389\uff1a 1 2 3 std :: cout << 1 ; std :: cout << 2 ; std :: cout << 3 ; \u6ce8\u91ca\u6389\uff1a 1 2 3 // std::cout << 1; // std::cout << 2; // std::cout << 3; \u6216 1 2 3 4 5 /* std::cout << 1; std::cout << 2; std::cout << 3; */ \u4f60\u9700\u8981\u8fd9\u4e48\u505a\u7684\u539f\u56e0\u6709\u5f88\u591a\uff1a \u4f60\u6b63\u5728\u5904\u7406\u4e00\u6bb5\u5c1a\u672a\u7f16\u8bd1\u7684\u65b0\u4ee3\u7801\uff0c\u9700\u8981\u8fd0\u884c\u8be5\u7a0b\u5e8f\u3002\u5982\u679c\u6709\u7f16\u8bd1\u5668\u9519\u8bef\uff0c\u7f16\u8bd1\u5668\u5c06\u4e0d\u5141\u8bb8\u7f16\u8bd1\u4ee3\u7801\u3002\u6ce8\u91ca\u6389\u4e0d\u80fd\u7f16\u8bd1\u7684\u4ee3\u7801\u5c06\u5141\u8bb8\u7a0b\u5e8f\u7f16\u8bd1\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u518d\u6b21\u8fd0\u884c\u3002\u51c6\u5907\u597d\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u53d6\u6d88\u5bf9\u4ee3\u7801\u7684\u6ce8\u91ca\uff0c\u5e76\u7ee7\u7eed\u5904\u7406\u5b83\u3002 \u4f60\u5df2\u7ecf\u7f16\u5199\u4e86\u53ef\u7f16\u8bd1\u4f46\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u7684\u65b0\u4ee3\u7801\uff0c\u5e76\u4e14\u76f4\u5230\u4ee5\u540e\u624d\u6709\u65f6\u95f4\u4fee\u590d\u5b83\u3002\u6ce8\u91ca\u6389\u635f\u574f\u7684\u4ee3\u7801\u5c06\u786e\u4fdd\u635f\u574f\u7684\u4ee3\u7801\u4e0d\u4f1a\u6267\u884c\u5e76\u5bfc\u81f4\u95ee\u9898\uff0c\u76f4\u5230\u4f60\u80fd\u591f\u4fee\u590d\u5b83\u3002 \u627e\u5230\u9519\u8bef\u7684\u6765\u6e90\u3002\u5982\u679c\u7a0b\u5e8f\u6ca1\u6709\u4ea7\u751f\u9884\u671f\u7684\u7ed3\u679c(\u6216\u5d29\u6e83)\uff0c\u6709\u65f6\u53ef\u4ee5\u7981\u7528\u90e8\u5206\u4ee3\u7801\uff0c\u770b\u770b\u662f\u5426\u53ef\u4ee5\u5206\u79bb\u51fa\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\u7684\u539f\u56e0\u3002\u5982\u679c\u6ce8\u91ca\u6389\u4e86\u4e00\u884c\u6216\u591a\u884c\u4ee3\u7801\u540e\u7a0b\u5e8f\u5f00\u59cb\u6309\u9884\u671f\u8fd0\u884c(\u6216\u505c\u6b62\u5d29\u6e83)\uff0c\u90a3\u4e48\u9519\u8bef\u53ef\u80fd\u4f4d\u4e8e\u6700\u540e\u6ce8\u91ca\u6389\u7684\u4ee3\u7801\u4e2d\u3002 \u4f60\u5e0c\u671b\u7528\u53e6\u4e00\u6bb5\u4ee3\u7801\u66ff\u6362\u4e00\u6bb5\u4ee3\u7801\uff0c\u4f46\u4f60\u6682\u65f6\u4e0d\u60f3\u5220\u9664\u539f\u4ee3\u7801\uff0c\u6b64\u65f6\u53ef\u4ee5\u5c06\u5176\u5148\u6ce8\u91ca\u6389\uff0c\u4fdd\u7559\u5728\u90a3\u91cc\u4ee5\u4f9b\u53c2\u8003\uff0c\u76f4\u5230\u65b0\u4ee3\u7801\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u4e3a\u6b62\u3002\u4e00\u65e6\u786e\u5b9a\u65b0\u4ee3\u7801\u6b63\u5e38\u5de5\u4f5c\uff0c\u5c31\u53ef\u4ee5\u5220\u9664\u6ce8\u91ca\u6389\u7684\u65e7\u4ee3\u7801\u3002\u5982\u679c\u4f60\u4e0d\u80fd\u8ba9\u4f60\u7684\u65b0\u4ee3\u7801\u5de5\u4f5c\uff0c\u4f60\u603b\u662f\u53ef\u4ee5\u5220\u9664\u65b0\u4ee3\u7801\u548c\u53d6\u6d88\u6ce8\u91ca\u65e7\u4ee3\u7801\u6062\u590d\u5230\u4f60\u4ee5\u524d\u7684\u4ee3\u7801\u3002 \u6ce8\u91ca\u6389\u4ee3\u7801\u662f\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u5e38\u89c1\u7684\u4e8b\u60c5\uff0c\u56e0\u6b64\u8bb8\u591aIDE\u90fd\u652f\u6301\u6ce8\u91ca\u6389\u9009\u4e2d\u7684\u4ee3\u7801\u90e8\u5206\u3002\u4f7f\u7528\u65b9\u5f0f\u56e0 IDE \u800c\u5f02\u3002","title":"\u5c06\u4ee3\u7801\u6ce8\u91ca\u6389"},{"location":"1-2-comments/#for-visual-studio-users","text":"\u4f60\u53ef\u4ee5\u901a\u8fc7\u7f16\u8f91\u83dc\u5355>\u9ad8\u7ea7>\u6ce8\u91ca\u9009\u62e9(\u6216\u53d6\u6d88\u6ce8\u91ca\u9009\u62e9)\u6765\u6ce8\u91ca\u6216\u53d6\u6d88\u6ce8\u91ca\u9009\u62e9\u3002","title":"For Visual Studio users"},{"location":"1-2-comments/#for-codeblocks-users","text":"\u4f60\u53ef\u4ee5\u901a\u8fc7\u7f16\u8f91\u83dc\u5355>\u6ce8\u91ca\u6765\u6ce8\u91ca\u6216\u53d6\u6d88\u6ce8\u91ca\u9009\u62e9\u3002 \u5c0f\u8d34\u58eb \u5982\u679c\u4f60\u53ea\u4f7f\u7528\u5355\u884c\u6ce8\u91ca\uff0c\u90a3\u4e48\u4f60\u603b\u662f\u53ef\u4ee5\u4f7f\u7528\u591a\u884c\u6ce8\u91ca\u6765\u6ce8\u91ca\u6389\u4ee3\u7801\u800c\u4e0d\u4ea7\u751f\u51b2\u7a81\u3002\u53cd\u4e4b\u5219\u4f1a\u6bd4\u8f83\u9ebb\u70e6\u3002 \u5982\u679c\u4f60\u786e\u5b9e\u9700\u8981\u6ce8\u91ca\u6389\u4e00\u4e2a\u5305\u542b\u591a\u884c\u6ce8\u91ca\u7684\u4ee3\u7801\u5757\uff0c\u4f60\u4e5f\u53ef\u4ee5\u8003\u8651\u4f7f\u7528 # if 0 \u9884\u5904\u7406\u5668\u6307\u4ee4 2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb","title":"For Code::Blocks users"},{"location":"1-2-comments/#_5","text":"\u5728\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u7ea7\u522b\uff0c\u4f7f\u7528\u6ce8\u91ca\u6765\u63cf\u8ff0\u201c\u662f\u4ec0\u4e48\u201d\u3002 \u5728\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u5185\u90e8\uff0c\u4f7f\u7528\u6ce8\u91ca\u6765\u63cf\u8ff0\u201c\u600e\u4e48\u505a\u201d\u3002 \u5728\u8bed\u53e5\u7ea7\u522b\uff0c\u4f7f\u7528\u6ce8\u91ca\u6765\u63cf\u8ff0\u201c\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u505a\u201d\u3002","title":"\u5c0f\u7ed3"},{"location":"1-3-Introduction-to-objects-and-variables/","tags":["object","variable"],"text":"Key Takeaway \u5c3d\u7ba1\u5728\u8bed\u8a00\u5c42\u9762\uff0c\u4f7f\u7528\u5355\u4e00\u8bed\u53e5\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\u662f\u88ab\u5141\u8bb8\u7684\uff0c\u4f46\u662f\u5728\u5b9e\u8df5\u4e2d\u5e94\u5f53\u5c3d\u91cf\u907f\u514d\u8fd9\u6837\u53bb\u505a\uff08\u5373\u65f6\u5b83\u4eec\u5177\u6709\u76f8\u540c\u7684\u7c7b\u578b\uff09\u3002\u6b63\u786e\u7684\u505a\u6cd5\u662f\u4e00\u4e2a\u8bed\u53e5\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\uff08\u5e76\u4f7f\u7528\u5355\u884c\u6ce8\u91ca\u89e3\u91ca\u8be5\u53d8\u91cf\u7684\u7528\u9014\uff09\u3002 C++ \u53ea\u80fd\u901a\u8fc7\u5bf9\u8c61\u6765\u95f4\u63a5\u5730\u8bbf\u95ee\u5185\u5b58 \u5bf9\u8c61\u5373\u5b58\u653e\u5728\u5185\u5b58\u4e2d\u5177\u6709\u503c\u4ee5\u53ca\u5176\u4ed6\u5c5e\u6027\u7684\u6570\u636e C++ \u4e2d\u7684\u5bf9\u8c61\u6307\u7684\u662f\u53d8\u91cf\u548c\u6570\u636e\u7ed3\u6784\uff0c\u4f46\u4e0d\u5305\u62ec\u51fd\u6570 \u5bf9\u8c61\u53ef\u4ee5\u662f\u547d\u540d\u7684\u6216\u672a\u547d\u540d\u7684\uff0c\u547d\u540d\u7684\u5bf9\u8c61\u79f0\u4e3a\u53d8\u91cf \u53d8\u91cf\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u88ab\u5b9e\u4f8b\u5316\uff0c\u5373\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u3001\u4e3a\u5176\u5206\u914d\u5185\u5b58\u5e76\u5c06\u7ed3\u679c\u4fdd\u5b58\u5728\u53d8\u91cf\u4e2d \u6570\u636e\u7c7b\u578b\u7528\u4e8e\u544a\u8bc9\u7f16\u8bd1\u5668\u5b83\u8981\u5b58\u50a8\u6216\u89e3\u6790\u7684\u53d8\u91cf\u662f\u4ec0\u4e48\u683c\u5f0f \u5c3d\u91cf\u907f\u514d\u5728\u4e00\u884c\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\uff0c\u5bb9\u6613\u51fa\u73b0\u5fd8\u8bb0\u521d\u59cb\u5316\u7684\u60c5\u51b5 \u6570\u636e \u5728 1.1 \u7a0b\u5e8f\u7ed3\u6784\u548c\u8bed\u53e5 \u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u7ec4\u6210\u4e00\u4e2a\u7a0b\u5e8f\u6240\u6d89\u53ca\u7684\u5927\u591a\u6570\u8bed\u53e5\u5e76\u4e14\u5c06\u5b83\u4eec\u5c01\u88c5\u6210\u4e86\u51fd\u6570\u3002\u8fd9\u4e9b\u8bed\u53e5\u80fd\u591f\u6267\u884c\u4e00\u7cfb\u5217\u64cd\u4f5c\u5e76\u6309\u7167\uff08\u4f46\u613f\uff09\u9884\u671f\u8f93\u51fa\u6b63\u786e\u7684\u7ed3\u679c\u3002 \u4f46\u662f\uff0c\u7a0b\u5e8f\u7a76\u7adf\u662f\u5982\u4f55\u751f\u6210\u8fd9\u4e9b\u7ed3\u679c\u7684\u5462\uff1f\u5b83\u4eec\u662f\u901a\u8fc7\u64cd\u4f5c\uff08\u8bfb\u3001\u4fee\u6539\u3001\u5199\uff09\u6570\u636e\u6765\u5b8c\u6210\u8fd9\u4e00\u8fc7\u7a0b\u7684\u3002\u5728\u8ba1\u7b97\u673a\u9886\u57df\uff0c\u6570\u636e\u6307\u7684\u662f\u4efb\u4f55\u53ef\u4ee5\u88ab\u8ba1\u7b97\u673a\u79fb\u52a8\u3001\u5904\u7406\u548c\u5b58\u50a8\u7684\u4fe1\u606f\u3002 \u5173\u952e\u4fe1\u606f \u6240\u8c13\u7a0b\u5e8f\uff0c\u5373\u4e00\u7cfb\u5217\u7528\u4e8e\u64cd\u4f5c\u6570\u636e\u5e76\u8f93\u51fa\u9884\u671f\u7ed3\u679c\u7684\u6307\u4ee4 \u4e00\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u9014\u5f84\u6765\u83b7\u53d6\u6570\u636e\uff0c\u4f8b\u5982\u901a\u8fc7\u6587\u4ef6\u3001\u6570\u636e\u5e93\u3001\u7f51\u7edc\u3001\u7528\u6237\u8f93\u5165\u6765\u83b7\u53d6\uff0c\u7a0b\u5e8f\u5458\u751a\u81f3\u53ef\u4ee5\u76f4\u63a5\u5c06\u6570\u636e\u5d4c\u5165\u5728\u7a0b\u5e8f\u6e90\u7801\u4e2d\u3002\u5728\u524d\u9762\u8bfe\u7a0b\u4e2d\u7f16\u5199\u7684 \u201cHello world\u201d \u7a0b\u5e8f\u4e2d\uff0c\u201cHello world\u201d\u8fd9\u4e2a\u5b57\u7b26\u4e32\u4fbf\u662f\u76f4\u63a5\u5d4c\u5165\u5728\u7a0b\u5e8f\u4e2d\u7684\u3002\u968f\u540e\uff0c\u8be5\u7a0b\u5e8f\u5bf9\u8be5\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\uff0c\u5c06\u5176\u663e\u793a\u5728\u663e\u793a\u5668\u4e0a\u3002 \u6570\u636e\u5728\u8ba1\u7b97\u673a\u4e2d\u5b58\u50a8\u65f6\uff0c\u5f80\u5f80\u4ee5\u4e00\u79cd\u66f4\u9ad8\u6548\u66f4\u4fbf\u4e8e\u5904\u7406\u7684\u683c\u5f0f\u6765\u5b58\u50a8\uff08\u5f80\u5f80\u4e0d\u80fd\u591f\u88ab\u4eba\u7c7b\u76f4\u63a5\u9605\u8bfb\uff09\u3002\u56e0\u6b64\uff0c\u5f53 \u201cHello World\u201d \u7a0b\u5e8f\u88ab\u7f16\u8bd1\u540e\uff0c\u5b57\u7b26\u4e32 \u201cHello World\u201d \u4f1a\u88ab\u8f6c\u6362\u4e3a\u4e00\u79cd\u66f4\u4e3a\u9ad8\u6548\u7684\u683c\u5f0f\u6765\u5b58\u50a8\u5e76\u63d0\u4f9b\u7ed9\u7a0b\u5e8f\u4f7f\u7528\uff08\u4e8c\u8fdb\u5236\u683c\u5f0f\u2014\u2014\u6211\u4eec\u5c06\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\uff09 \u5bf9\u8c61\u548c\u53d8\u91cf \u6240\u6709\u8ba1\u7b97\u673a\u90fd\u6709\u5185\u5b58\uff0c\u5373 RAM \uff08\u968f\u673a\u8bbf\u95ee\u5185\u5b58\uff09\uff0c \u53ef\u4f9b\u7a0b\u5e8f\u4f7f\u7528\u3002\u4f60\u53ef\u4ee5\u5c06 RAM \u770b\u505a\u662f\u4e00\u7cfb\u5217\u5177\u6709\u6807\u53f7\u7684\u201c\u4fe1\u7bb1\u201d\uff0c\u5728\u7a0b\u5e8f\u8fd0\u884c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5b83\u4eec\u53ef\u4ee5\u5b58\u653e\u4e00\u7cfb\u5217\u7684\u6570\u636e\u7247\u6bb5\u3002\u4e00\u4e2a\u5355\u72ec\u7684\u5b58\u653e\u5728\u5185\u5b58\u4e2d\u7684\u6570\u636e\uff0c\u79f0\u4e4b\u4e3a\u503c\uff08value\uff09\u3002 \u5728\u4e00\u4e9b\u6bd4\u8f83\u53e4\u8001\u7684\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff08\u4f8b\u5982 Apple Basic\uff09\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u8fd9\u4e9b\u201d\u4fe1\u7bb1\u201c\uff08\u5bf9\u5e94\u7684\u8bed\u53e5\u7c7b\u4f3c\u4e8e \u83b7\u53d6\u6807\u53f7\u4e3a7532\u7684\u4fe1\u7bb1\u4e2d\u7684\u6570\u636e \uff09 \u5728 C++ \u4e2d\uff0c\u76f4\u63a5\u8bbf\u95ee\u5185\u5b58\u662f\u4e0d\u88ab\u5141\u8bb8\u7684\uff0c\u6211\u4eec\u53ea\u80fd\u591f\u901a\u8fc7\u67d0\u4e2a\u5bf9\u8c61\u95f4\u63a5\u5730\u8bbf\u95ee\u5185\u5b58\u3002\u6240\u8c13\u5bf9\u8c61\uff0c\u5373\u5b58\u653e\u5728\u67d0\u4e2a\u533a\u57df\uff08\u4e00\u822c\u6765\u8bb2\u4e3a\u5185\u5b58\uff09\uff0c\u5177\u6709\u503c\u4ee5\u53ca\u5176\u4ed6\u5c5e\u6027\uff08\u540e\u7eed\u8bfe\u7a0b\u4f1a\u7740\u91cd\u4ecb\u7ecd\uff09\u7684\u6570\u636e\u3002\u7f16\u8bd1\u5668\u548c\u64cd\u4f5c\u7cfb\u7edf\u4e3a\u5bf9\u8c61\u5206\u914d\u5185\u5b58\u7684\u8fc7\u7a0b\uff0c\u8d85\u51fa\u4e86\u672c\u8bfe\u7a0b\u7684\u8303\u56f4\u3002\u4f46\u662f\uff0c\u6211\u4eec\u9700\u8981\u77e5\u9053\u7684\u662f\uff0c \u83b7\u53d6\u6807\u53f7\u4e3a7532\u7684\u4fe1\u7bb1\u4e2d\u7684\u6570\u636e \u662f\u4e0d\u53ef\u884c\u7684\uff0c\u53d6\u800c\u4ee3\u4e4b\u7684\u5e94\u5f53\u662f \u83b7\u53d6\u67d0\u4e2a\u5bf9\u8c61\u6240\u4fdd\u5b58\u7684\u503c \u3002\u8fd9\u610f\u5473\u7740\uff0c\u6211\u4eec\u53ef\u4ee5\u4e13\u6ce8\u4e8e\u4f7f\u7528\u5bf9\u8c61\u6765\u5b58\u50a8\u548c\u8bfb\u53d6\u6570\u636e\uff0c\u800c\u4e0d\u5fc5\u62c5\u5fc3\u5b83\u4eec\u5b9e\u9645\u4f7f\u7528\u7684\u5185\u5b58\u3002 \u5bf9\u8c61\u53ef\u4ee5\u662f\u547d\u540d\u7684\uff0c\u6216\u672a\u547d\u540d\u7684\u3002\u4e00\u4e2a\u547d\u540d\u7684\u5bf9\u8c61\u88ab\u79f0\u4e3a\u53d8\u91cf\uff0c\u800c\u8be5\u5bf9\u8c61\u7684\u540d\u5b57\u672c\u8eab\uff0c\u5219\u79f0\u4e3a \u6807\u8bc6\u7b26(identifier) \u3002\u5728\u6211\u4eec\u7684\u7a0b\u5e8f\u4e2d\uff0c\u5927\u591a\u6570\u7684\u5bf9\u8c61\u90fd\u5c5e\u4e8e\u53d8\u91cf\u3002 \u4f5c\u8005\u6ce8 \u5728\u4e00\u822c\u7684\u7f16\u7a0b\u4e2d\uff0c \u5bf9\u8c61 \u8fd9\u4e2a\u672f\u8bed\u901a\u5e38\u6307\u4ee3\u7684\u662f\u67d0\u4e00\u4e2a\u53d8\u91cf\u3001\u6570\u636e\u7ed3\u6784\u6216\u51fd\u6570\u3002\u5728 C++ \u4e2d\uff0c \u5bf9\u8c61 \u8fd9\u4e2a\u672f\u8bed\u7684\u542b\u4e49\u8981\u66f4\u52a0\u5c40\u9650\u4e00\u4e9b\uff0c\u5b83\u5e76\u4e0d\u5305\u62ec\u51fd\u6570\u3002 \u53d8\u91cf\u7684\u5b9e\u4f8b\u5316 \u4e3a\u4e86\u521b\u5efa\u53d8\u91cf\uff0c\u6211\u4eec\u9700\u8981\u4e00\u79cd\u88ab\u79f0\u4e3a\u201d\u5b9a\u4e49\u201c\u7684\u7279\u6b8a\u7c7b\u578b\u7684\u58f0\u660e\u8bed\u53e5\uff08\u7a0d\u540e\u4f1a\u6f84\u6e05\u58f0\u660e\u548c\u5b9a\u4e49\u4e4b\u95f4\u7684\u533a\u522b\uff09\u3002 \u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u540d\u4e3ax\u7684\u53d8\u91cf\uff0c\u6848\u4f8b\u5982\u4e0b\uff1a 1 int x ; // define a variable named x, of type int \u5728\u7f16\u8bd1\u9636\u6bb5\uff0c\u5f53\u7f16\u8bd1\u5668\u89e3\u6790\u5230\u8be5\u8bed\u53e5\u65f6\uff0c\u5b83\u4f1a\u8bb0\u5f55\u4e0b\u6b64\u65f6\u5b9a\u4e49\u7684\u4e00\u4e2a\u540d\u4e3ax\u3001\u7c7b\u578b\u4e3a int \u7684\u53d8\u91cf\u3002\uff08\u7a0d\u540e\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u7c7b\u578b\u7cfb\u7edf\uff09\u3002\u4ece\u6b64\u4ee5\u540e\uff08\u6709\u4e00\u5b9a\u7684\u9650\u5236\u6761\u4ef6\uff0c\u7a0d\u540e\u4ecb\u7ecd\uff09\uff0c\u6bcf\u5f53\u7f16\u8bd1\u5668\u770b\u5230\u6807\u8bc6\u7b26 x\uff0c\u5b83\u5c31\u77e5\u9053\u53bb\u5f15\u7528\u8fd9\u4e2a\u53d8\u91cf\u3002 \u5728\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u5019\uff08\u79f0\u4e3a\u8fd0\u884c\u65f6\uff09\uff0c\u8be5\u53d8\u91cf\u4f1a\u88ab \u5b9e\u4f8b\u5316 \u3002\u5b9e\u4f8b\u5316\u8fd9\u4e2a\u8bcd\u53ea\u662f\u542c\u8d77\u6765\u6709\u70b9\u7384\u4e4e\u7f62\u4e86\uff0c\u5b83\u7684\u542b\u4e49\u5f88\u7b80\u5355\u2014\u2014\u521b\u5efa\u5bf9\u8c61\u5e76\u4e3a\u5176\u5206\u914d\u4e00\u4e2a\u5185\u5b58\u5730\u5740\u3002\u53d8\u91cf\u5728\u4f7f\u7528\u524d\uff08\u5b58\u50a8\u503c\u524d\uff09\u5fc5\u987b\u5b9e\u4f8b\u5316\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u5047\u8bbe\u53d8\u91cfx\u5b9e\u4f8b\u5316\u540e\u5bf9\u5e94\u7684\u5185\u5b58\u5730\u5740\u4e3a140\uff0c\u4e4b\u540e\u7a0b\u5e8f\u5728\u4efb\u4f55\u65f6\u5019\u4f7f\u7528\u8be5\u53d8\u91cfx\u65f6\uff0c\u5b83\u90fd\u4f1a\u53d6\u8bbf\u95ee\u5185\u5b58\u5730\u5740140\u3002\u4e00\u4e2a\u5b9e\u4f8b\u5316\u4e4b\u540e\u7684\u5bf9\u8c61\uff0c\u901a\u5e38\u88ab\u79f0\u4e3a \u5b9e\u4f8b(instance) \u3002 \u6570\u636e\u7c7b\u578b \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u4e86\uff0c\u53d8\u91cf\u5c31\u662f\u67d0\u4e2a\u547d\u540d\u4e4b\u540e\u7684\u5b58\u50a8\u533a\u57df\uff0c\u5b83\u53ef\u4ee5\u5b58\u653e\u6570\u636e\uff08\u5982\u4f55\u5b58\u653e\u5c06\u5728\u540e\u7eed\u7684\u7ae0\u8282\u8ba8\u8bba\uff09\u3002\u6570\u636e\u7c7b\u578b\uff08\u4e00\u822c\u7b80\u79f0\u4e3a\u7c7b\u578b\uff09\u53ef\u4ee5\u544a\u77e5\u7f16\u8bd1\u5668\u5b83\u8981\u5b58\u50a8\u7684\u8be5\u6570\u636e\u7684\u503c\u662f\u4ec0\u4e48\u7c7b\u578b\uff08\u4f8b\u5982\uff0c\u6570\u5b57\uff0c\u5b57\u6bcd\uff0c\u6587\u672c\u7b49\u7b49\uff09\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf x \u7684\u7c7b\u578b\u662f int\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u8be5\u53d8\u91cf x \u8868\u793a\u7684\u662f\u4e00\u4e2a\u6574\u578b\u7684\u503c\u3002\u6574\u578b\u8868\u793a\u8be5\u6570\u5b57\u4e0d\u9700\u8981\u5c0f\u6570\u90e8\u5206\u5c31\u53ef\u4ee5\u8868\u793a\uff0c\u4f8b\u5982 4\uff0c27\uff0c0\uff0c-2 \u6216\u8005 -12\u3002\u957f\u8bdd\u77ed\u8bf4\uff0c\u8fd9\u91cc x \u662f\u4e00\u4e2a\u6574\u578b\u53d8\u91cf\u3002 \u5728 C++ \u4e2d\uff0c\u53d8\u91cf\u7684\u7c7b\u578b\u5fc5\u987b\u5728 \u7f16\u8bd1\u65f6 \u786e\u5b9a\u3002\u6b64\u5916\uff0c\u8be5\u7c7b\u578b\u5728\u4e0b\u4e00\u6b21\u7f16\u8bd1\u524d\uff0c\u4e0d\u80fd\u53d1\u751f\u6539\u53d8\u3002\u8fd9\u610f\u5473\u7740\uff0c\u8be5\u6574\u578b\u53d8\u91cf\u53ea\u80fd\u59cb\u7ec8\u5b58\u653e\u4e00\u4e2a\u6574\u578b\u7684\u503c\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u5b83\u5b58\u653e\u5176\u4ed6\u7c7b\u578b\u7684\u503c\uff0c\u4f60\u5fc5\u987b\u4f7f\u7528\u53e6\u5916\u7684\u53d8\u91cf\u3002 \u6574\u578b\u53ea\u662f C++ \u539f\u751f\u652f\u6301\u7684\u591a\u79cd\u7c7b\u578b\u4e2d\u7684\u4e00\u79cd\u3002\u4e3a\u4e86\u66f4\u597d\u7684\u89e3\u91ca\uff0c\u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff0c\u8fd9\u91cc\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b\u578b\u4e3a double \u7684\u53d8\u91cf\uff1a 1 double width ; // define a variable named width, of type double C++ \u5141\u8bb8\u7528\u6237\u521b\u5efa\u81ea\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\u3002\u6211\u4eec\u4f1a\u5728\u5c06\u6765\u7684\u8bfe\u7a0b\u4e2d\u5927\u91cf\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\uff0c\u8fd9\u662f C++ \u8bed\u8a00\u5f3a\u5927\u7684\u539f\u56e0\u4e4b\u4e00\u3002 \u5728\u4ecb\u7ecd\u6027\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u53ea\u4f7f\u7528\u6574\u578b\u53d8\u91cf\uff0c\u56e0\u5b83\u4eec\u66f4\u52a0\u7b80\u5355\uff0c\u4f46\u662f\u6211\u4eec\u5f88\u5feb\u5c31\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u63a2\u7d22\u5927\u91cf\u7684\u5176\u4ed6 C++ \u652f\u6301\u7684\u6570\u636e\u7c7b\u578b\u3002 \u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf \u5728\u4e00\u4e2a\u8bed\u53e5\u4e2d\u5b9a\u4e49\u591a\u4e2a\u76f8\u540c\u7c7b\u578b\u7684\u53d8\u91cf\u5f53\u7136\u4e5f\u662f\u53ef\u4ee5\u7684\uff0c\u53ea\u9700\u8981\u7528\u9017\u53f7\u5c06\u53d8\u91cf\u540d\u5206\u5272\u5f00\u6765\u5373\u53ef\u3002\u4e0b\u9762\u4e24\u4e2a\u4ee3\u7801\u7247\u6bb5\u7684\u6548\u679c\u662f\u4e00\u6837\u7684\uff1a 1 2 int a ; int b ; 1 int a , b ; \u5982\u679c\u5c5e\u4e8e\u4e0a\u8ff0\u65b9\u5f0f\u6765\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\uff0c\u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u4f1a\u72af\u4e0b\u4e24\u79cd\u9519\u8bef\uff08\u8fd9\u6ca1\u6709\u4ec0\u4e48\u5927\u4e0d\u4e86\u7684\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u4f1a\u6355\u83b7\u8fd9\u4e9b\u9519\u8bef\u5e76\u8981\u6c42\u4f60\u8fdb\u884c\u4fee\u590d\uff09\uff1a \u7b2c\u4e00\u79cd\u9519\u8bef\u662f\u4e3a\u6bcf\u4e2a\u53d8\u91cf\u90fd\u6dfb\u52a0\u7c7b\u578b\u8bf4\u660e\u3002 1 2 3 int a , int b ; // wrong (compiler error) int a , b ; // correct \u7b2c\u4e8c\u79cd\u9519\u8bef\u662f\u5728\u4e00\u4e2a\u8bed\u53e5\u4e2d\u5b9a\u4e49\u4e24\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u8fd9\u662f\u4e0d\u88ab\u5141\u8bb8\u7684\u3002\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u53d8\u91cf\u5fc5\u987b\u5206\u522b\u5b9a\u4e49\u3002 1 2 3 4 5 6 7 int a , double b ; // wrong (compiler error) int a ; double b ; // correct (but not recommended) // correct and recommended (easier to read) int a ; double b ; \u6700\u4f73\u5b9e\u8df5 \u5c3d\u7ba1\u5728\u8bed\u8a00\u5c42\u9762\uff0c\u4f7f\u7528\u5355\u4e00\u8bed\u53e5\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\u662f\u88ab\u5141\u8bb8\u7684\uff0c\u4f46\u662f\u5728\u5b9e\u8df5\u4e2d\u5e94\u5f53\u5c3d\u91cf\u907f\u514d\u8fd9\u6837\u53bb\u505a\uff08\u5373\u65f6\u5b83\u4eec\u5177\u6709\u76f8\u540c\u7684\u7c7b\u578b\uff09\u3002\u6b63\u786e\u7684\u505a\u6cd5\u662f\u4e00\u4e2a\u8bed\u53e5\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\uff08\u5e76\u4f7f\u7528\u5355\u884c\u6ce8\u91ca\u89e3\u91ca\u8be5\u53d8\u91cf\u7684\u7528\u9014\uff09\u3002 \u5c0f\u7ed3 \u5728 C++ \u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u53d8\u91cf\u6765\u8bbf\u95ee\u5185\u5b58\u3002\u53d8\u91cf\u5177\u6709\u6807\u8bc6\u7b26\u3001\u7c7b\u578b\u548c\u503c\uff08\u4ee5\u53ca\u5176\u4ed6\u4e00\u4e9b\u4e0e\u6b64\u5904\u4e0d\u76f8\u5173\u7684\u5c5e\u6027\uff09\u3002\u53d8\u91cf\u7c7b\u578b\u7528\u4e8e\u786e\u5b9a\u5185\u5b58\u4e2d\u7684\u503c\u5e94\u8be5\u88ab\u5982\u4f55\u89e3\u6790\u3002 \u5728\u4e0b\u4e00\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5982\u4f55\u4e3a\u53d8\u91cf\u8d4b\u503c\u5e76\u4f7f\u7528\u5b83\u4eec\u3002","title":"1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf"},{"location":"1-3-Introduction-to-objects-and-variables/#_1","text":"\u5728 1.1 \u7a0b\u5e8f\u7ed3\u6784\u548c\u8bed\u53e5 \u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u7ec4\u6210\u4e00\u4e2a\u7a0b\u5e8f\u6240\u6d89\u53ca\u7684\u5927\u591a\u6570\u8bed\u53e5\u5e76\u4e14\u5c06\u5b83\u4eec\u5c01\u88c5\u6210\u4e86\u51fd\u6570\u3002\u8fd9\u4e9b\u8bed\u53e5\u80fd\u591f\u6267\u884c\u4e00\u7cfb\u5217\u64cd\u4f5c\u5e76\u6309\u7167\uff08\u4f46\u613f\uff09\u9884\u671f\u8f93\u51fa\u6b63\u786e\u7684\u7ed3\u679c\u3002 \u4f46\u662f\uff0c\u7a0b\u5e8f\u7a76\u7adf\u662f\u5982\u4f55\u751f\u6210\u8fd9\u4e9b\u7ed3\u679c\u7684\u5462\uff1f\u5b83\u4eec\u662f\u901a\u8fc7\u64cd\u4f5c\uff08\u8bfb\u3001\u4fee\u6539\u3001\u5199\uff09\u6570\u636e\u6765\u5b8c\u6210\u8fd9\u4e00\u8fc7\u7a0b\u7684\u3002\u5728\u8ba1\u7b97\u673a\u9886\u57df\uff0c\u6570\u636e\u6307\u7684\u662f\u4efb\u4f55\u53ef\u4ee5\u88ab\u8ba1\u7b97\u673a\u79fb\u52a8\u3001\u5904\u7406\u548c\u5b58\u50a8\u7684\u4fe1\u606f\u3002 \u5173\u952e\u4fe1\u606f \u6240\u8c13\u7a0b\u5e8f\uff0c\u5373\u4e00\u7cfb\u5217\u7528\u4e8e\u64cd\u4f5c\u6570\u636e\u5e76\u8f93\u51fa\u9884\u671f\u7ed3\u679c\u7684\u6307\u4ee4 \u4e00\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u9014\u5f84\u6765\u83b7\u53d6\u6570\u636e\uff0c\u4f8b\u5982\u901a\u8fc7\u6587\u4ef6\u3001\u6570\u636e\u5e93\u3001\u7f51\u7edc\u3001\u7528\u6237\u8f93\u5165\u6765\u83b7\u53d6\uff0c\u7a0b\u5e8f\u5458\u751a\u81f3\u53ef\u4ee5\u76f4\u63a5\u5c06\u6570\u636e\u5d4c\u5165\u5728\u7a0b\u5e8f\u6e90\u7801\u4e2d\u3002\u5728\u524d\u9762\u8bfe\u7a0b\u4e2d\u7f16\u5199\u7684 \u201cHello world\u201d \u7a0b\u5e8f\u4e2d\uff0c\u201cHello world\u201d\u8fd9\u4e2a\u5b57\u7b26\u4e32\u4fbf\u662f\u76f4\u63a5\u5d4c\u5165\u5728\u7a0b\u5e8f\u4e2d\u7684\u3002\u968f\u540e\uff0c\u8be5\u7a0b\u5e8f\u5bf9\u8be5\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\uff0c\u5c06\u5176\u663e\u793a\u5728\u663e\u793a\u5668\u4e0a\u3002 \u6570\u636e\u5728\u8ba1\u7b97\u673a\u4e2d\u5b58\u50a8\u65f6\uff0c\u5f80\u5f80\u4ee5\u4e00\u79cd\u66f4\u9ad8\u6548\u66f4\u4fbf\u4e8e\u5904\u7406\u7684\u683c\u5f0f\u6765\u5b58\u50a8\uff08\u5f80\u5f80\u4e0d\u80fd\u591f\u88ab\u4eba\u7c7b\u76f4\u63a5\u9605\u8bfb\uff09\u3002\u56e0\u6b64\uff0c\u5f53 \u201cHello World\u201d \u7a0b\u5e8f\u88ab\u7f16\u8bd1\u540e\uff0c\u5b57\u7b26\u4e32 \u201cHello World\u201d \u4f1a\u88ab\u8f6c\u6362\u4e3a\u4e00\u79cd\u66f4\u4e3a\u9ad8\u6548\u7684\u683c\u5f0f\u6765\u5b58\u50a8\u5e76\u63d0\u4f9b\u7ed9\u7a0b\u5e8f\u4f7f\u7528\uff08\u4e8c\u8fdb\u5236\u683c\u5f0f\u2014\u2014\u6211\u4eec\u5c06\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\uff09","title":"\u6570\u636e"},{"location":"1-3-Introduction-to-objects-and-variables/#_2","text":"\u6240\u6709\u8ba1\u7b97\u673a\u90fd\u6709\u5185\u5b58\uff0c\u5373 RAM \uff08\u968f\u673a\u8bbf\u95ee\u5185\u5b58\uff09\uff0c \u53ef\u4f9b\u7a0b\u5e8f\u4f7f\u7528\u3002\u4f60\u53ef\u4ee5\u5c06 RAM \u770b\u505a\u662f\u4e00\u7cfb\u5217\u5177\u6709\u6807\u53f7\u7684\u201c\u4fe1\u7bb1\u201d\uff0c\u5728\u7a0b\u5e8f\u8fd0\u884c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5b83\u4eec\u53ef\u4ee5\u5b58\u653e\u4e00\u7cfb\u5217\u7684\u6570\u636e\u7247\u6bb5\u3002\u4e00\u4e2a\u5355\u72ec\u7684\u5b58\u653e\u5728\u5185\u5b58\u4e2d\u7684\u6570\u636e\uff0c\u79f0\u4e4b\u4e3a\u503c\uff08value\uff09\u3002 \u5728\u4e00\u4e9b\u6bd4\u8f83\u53e4\u8001\u7684\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff08\u4f8b\u5982 Apple Basic\uff09\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u8fd9\u4e9b\u201d\u4fe1\u7bb1\u201c\uff08\u5bf9\u5e94\u7684\u8bed\u53e5\u7c7b\u4f3c\u4e8e \u83b7\u53d6\u6807\u53f7\u4e3a7532\u7684\u4fe1\u7bb1\u4e2d\u7684\u6570\u636e \uff09 \u5728 C++ \u4e2d\uff0c\u76f4\u63a5\u8bbf\u95ee\u5185\u5b58\u662f\u4e0d\u88ab\u5141\u8bb8\u7684\uff0c\u6211\u4eec\u53ea\u80fd\u591f\u901a\u8fc7\u67d0\u4e2a\u5bf9\u8c61\u95f4\u63a5\u5730\u8bbf\u95ee\u5185\u5b58\u3002\u6240\u8c13\u5bf9\u8c61\uff0c\u5373\u5b58\u653e\u5728\u67d0\u4e2a\u533a\u57df\uff08\u4e00\u822c\u6765\u8bb2\u4e3a\u5185\u5b58\uff09\uff0c\u5177\u6709\u503c\u4ee5\u53ca\u5176\u4ed6\u5c5e\u6027\uff08\u540e\u7eed\u8bfe\u7a0b\u4f1a\u7740\u91cd\u4ecb\u7ecd\uff09\u7684\u6570\u636e\u3002\u7f16\u8bd1\u5668\u548c\u64cd\u4f5c\u7cfb\u7edf\u4e3a\u5bf9\u8c61\u5206\u914d\u5185\u5b58\u7684\u8fc7\u7a0b\uff0c\u8d85\u51fa\u4e86\u672c\u8bfe\u7a0b\u7684\u8303\u56f4\u3002\u4f46\u662f\uff0c\u6211\u4eec\u9700\u8981\u77e5\u9053\u7684\u662f\uff0c \u83b7\u53d6\u6807\u53f7\u4e3a7532\u7684\u4fe1\u7bb1\u4e2d\u7684\u6570\u636e \u662f\u4e0d\u53ef\u884c\u7684\uff0c\u53d6\u800c\u4ee3\u4e4b\u7684\u5e94\u5f53\u662f \u83b7\u53d6\u67d0\u4e2a\u5bf9\u8c61\u6240\u4fdd\u5b58\u7684\u503c \u3002\u8fd9\u610f\u5473\u7740\uff0c\u6211\u4eec\u53ef\u4ee5\u4e13\u6ce8\u4e8e\u4f7f\u7528\u5bf9\u8c61\u6765\u5b58\u50a8\u548c\u8bfb\u53d6\u6570\u636e\uff0c\u800c\u4e0d\u5fc5\u62c5\u5fc3\u5b83\u4eec\u5b9e\u9645\u4f7f\u7528\u7684\u5185\u5b58\u3002 \u5bf9\u8c61\u53ef\u4ee5\u662f\u547d\u540d\u7684\uff0c\u6216\u672a\u547d\u540d\u7684\u3002\u4e00\u4e2a\u547d\u540d\u7684\u5bf9\u8c61\u88ab\u79f0\u4e3a\u53d8\u91cf\uff0c\u800c\u8be5\u5bf9\u8c61\u7684\u540d\u5b57\u672c\u8eab\uff0c\u5219\u79f0\u4e3a \u6807\u8bc6\u7b26(identifier) \u3002\u5728\u6211\u4eec\u7684\u7a0b\u5e8f\u4e2d\uff0c\u5927\u591a\u6570\u7684\u5bf9\u8c61\u90fd\u5c5e\u4e8e\u53d8\u91cf\u3002 \u4f5c\u8005\u6ce8 \u5728\u4e00\u822c\u7684\u7f16\u7a0b\u4e2d\uff0c \u5bf9\u8c61 \u8fd9\u4e2a\u672f\u8bed\u901a\u5e38\u6307\u4ee3\u7684\u662f\u67d0\u4e00\u4e2a\u53d8\u91cf\u3001\u6570\u636e\u7ed3\u6784\u6216\u51fd\u6570\u3002\u5728 C++ \u4e2d\uff0c \u5bf9\u8c61 \u8fd9\u4e2a\u672f\u8bed\u7684\u542b\u4e49\u8981\u66f4\u52a0\u5c40\u9650\u4e00\u4e9b\uff0c\u5b83\u5e76\u4e0d\u5305\u62ec\u51fd\u6570\u3002","title":"\u5bf9\u8c61\u548c\u53d8\u91cf"},{"location":"1-3-Introduction-to-objects-and-variables/#_3","text":"\u4e3a\u4e86\u521b\u5efa\u53d8\u91cf\uff0c\u6211\u4eec\u9700\u8981\u4e00\u79cd\u88ab\u79f0\u4e3a\u201d\u5b9a\u4e49\u201c\u7684\u7279\u6b8a\u7c7b\u578b\u7684\u58f0\u660e\u8bed\u53e5\uff08\u7a0d\u540e\u4f1a\u6f84\u6e05\u58f0\u660e\u548c\u5b9a\u4e49\u4e4b\u95f4\u7684\u533a\u522b\uff09\u3002 \u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u540d\u4e3ax\u7684\u53d8\u91cf\uff0c\u6848\u4f8b\u5982\u4e0b\uff1a 1 int x ; // define a variable named x, of type int \u5728\u7f16\u8bd1\u9636\u6bb5\uff0c\u5f53\u7f16\u8bd1\u5668\u89e3\u6790\u5230\u8be5\u8bed\u53e5\u65f6\uff0c\u5b83\u4f1a\u8bb0\u5f55\u4e0b\u6b64\u65f6\u5b9a\u4e49\u7684\u4e00\u4e2a\u540d\u4e3ax\u3001\u7c7b\u578b\u4e3a int \u7684\u53d8\u91cf\u3002\uff08\u7a0d\u540e\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u7c7b\u578b\u7cfb\u7edf\uff09\u3002\u4ece\u6b64\u4ee5\u540e\uff08\u6709\u4e00\u5b9a\u7684\u9650\u5236\u6761\u4ef6\uff0c\u7a0d\u540e\u4ecb\u7ecd\uff09\uff0c\u6bcf\u5f53\u7f16\u8bd1\u5668\u770b\u5230\u6807\u8bc6\u7b26 x\uff0c\u5b83\u5c31\u77e5\u9053\u53bb\u5f15\u7528\u8fd9\u4e2a\u53d8\u91cf\u3002 \u5728\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u5019\uff08\u79f0\u4e3a\u8fd0\u884c\u65f6\uff09\uff0c\u8be5\u53d8\u91cf\u4f1a\u88ab \u5b9e\u4f8b\u5316 \u3002\u5b9e\u4f8b\u5316\u8fd9\u4e2a\u8bcd\u53ea\u662f\u542c\u8d77\u6765\u6709\u70b9\u7384\u4e4e\u7f62\u4e86\uff0c\u5b83\u7684\u542b\u4e49\u5f88\u7b80\u5355\u2014\u2014\u521b\u5efa\u5bf9\u8c61\u5e76\u4e3a\u5176\u5206\u914d\u4e00\u4e2a\u5185\u5b58\u5730\u5740\u3002\u53d8\u91cf\u5728\u4f7f\u7528\u524d\uff08\u5b58\u50a8\u503c\u524d\uff09\u5fc5\u987b\u5b9e\u4f8b\u5316\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u5047\u8bbe\u53d8\u91cfx\u5b9e\u4f8b\u5316\u540e\u5bf9\u5e94\u7684\u5185\u5b58\u5730\u5740\u4e3a140\uff0c\u4e4b\u540e\u7a0b\u5e8f\u5728\u4efb\u4f55\u65f6\u5019\u4f7f\u7528\u8be5\u53d8\u91cfx\u65f6\uff0c\u5b83\u90fd\u4f1a\u53d6\u8bbf\u95ee\u5185\u5b58\u5730\u5740140\u3002\u4e00\u4e2a\u5b9e\u4f8b\u5316\u4e4b\u540e\u7684\u5bf9\u8c61\uff0c\u901a\u5e38\u88ab\u79f0\u4e3a \u5b9e\u4f8b(instance) \u3002","title":"\u53d8\u91cf\u7684\u5b9e\u4f8b\u5316"},{"location":"1-3-Introduction-to-objects-and-variables/#_4","text":"\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u4e86\uff0c\u53d8\u91cf\u5c31\u662f\u67d0\u4e2a\u547d\u540d\u4e4b\u540e\u7684\u5b58\u50a8\u533a\u57df\uff0c\u5b83\u53ef\u4ee5\u5b58\u653e\u6570\u636e\uff08\u5982\u4f55\u5b58\u653e\u5c06\u5728\u540e\u7eed\u7684\u7ae0\u8282\u8ba8\u8bba\uff09\u3002\u6570\u636e\u7c7b\u578b\uff08\u4e00\u822c\u7b80\u79f0\u4e3a\u7c7b\u578b\uff09\u53ef\u4ee5\u544a\u77e5\u7f16\u8bd1\u5668\u5b83\u8981\u5b58\u50a8\u7684\u8be5\u6570\u636e\u7684\u503c\u662f\u4ec0\u4e48\u7c7b\u578b\uff08\u4f8b\u5982\uff0c\u6570\u5b57\uff0c\u5b57\u6bcd\uff0c\u6587\u672c\u7b49\u7b49\uff09\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf x \u7684\u7c7b\u578b\u662f int\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u8be5\u53d8\u91cf x \u8868\u793a\u7684\u662f\u4e00\u4e2a\u6574\u578b\u7684\u503c\u3002\u6574\u578b\u8868\u793a\u8be5\u6570\u5b57\u4e0d\u9700\u8981\u5c0f\u6570\u90e8\u5206\u5c31\u53ef\u4ee5\u8868\u793a\uff0c\u4f8b\u5982 4\uff0c27\uff0c0\uff0c-2 \u6216\u8005 -12\u3002\u957f\u8bdd\u77ed\u8bf4\uff0c\u8fd9\u91cc x \u662f\u4e00\u4e2a\u6574\u578b\u53d8\u91cf\u3002 \u5728 C++ \u4e2d\uff0c\u53d8\u91cf\u7684\u7c7b\u578b\u5fc5\u987b\u5728 \u7f16\u8bd1\u65f6 \u786e\u5b9a\u3002\u6b64\u5916\uff0c\u8be5\u7c7b\u578b\u5728\u4e0b\u4e00\u6b21\u7f16\u8bd1\u524d\uff0c\u4e0d\u80fd\u53d1\u751f\u6539\u53d8\u3002\u8fd9\u610f\u5473\u7740\uff0c\u8be5\u6574\u578b\u53d8\u91cf\u53ea\u80fd\u59cb\u7ec8\u5b58\u653e\u4e00\u4e2a\u6574\u578b\u7684\u503c\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u5b83\u5b58\u653e\u5176\u4ed6\u7c7b\u578b\u7684\u503c\uff0c\u4f60\u5fc5\u987b\u4f7f\u7528\u53e6\u5916\u7684\u53d8\u91cf\u3002 \u6574\u578b\u53ea\u662f C++ \u539f\u751f\u652f\u6301\u7684\u591a\u79cd\u7c7b\u578b\u4e2d\u7684\u4e00\u79cd\u3002\u4e3a\u4e86\u66f4\u597d\u7684\u89e3\u91ca\uff0c\u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff0c\u8fd9\u91cc\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b\u578b\u4e3a double \u7684\u53d8\u91cf\uff1a 1 double width ; // define a variable named width, of type double C++ \u5141\u8bb8\u7528\u6237\u521b\u5efa\u81ea\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\u3002\u6211\u4eec\u4f1a\u5728\u5c06\u6765\u7684\u8bfe\u7a0b\u4e2d\u5927\u91cf\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\uff0c\u8fd9\u662f C++ \u8bed\u8a00\u5f3a\u5927\u7684\u539f\u56e0\u4e4b\u4e00\u3002 \u5728\u4ecb\u7ecd\u6027\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u53ea\u4f7f\u7528\u6574\u578b\u53d8\u91cf\uff0c\u56e0\u5b83\u4eec\u66f4\u52a0\u7b80\u5355\uff0c\u4f46\u662f\u6211\u4eec\u5f88\u5feb\u5c31\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u63a2\u7d22\u5927\u91cf\u7684\u5176\u4ed6 C++ \u652f\u6301\u7684\u6570\u636e\u7c7b\u578b\u3002","title":"\u6570\u636e\u7c7b\u578b"},{"location":"1-3-Introduction-to-objects-and-variables/#_5","text":"\u5728\u4e00\u4e2a\u8bed\u53e5\u4e2d\u5b9a\u4e49\u591a\u4e2a\u76f8\u540c\u7c7b\u578b\u7684\u53d8\u91cf\u5f53\u7136\u4e5f\u662f\u53ef\u4ee5\u7684\uff0c\u53ea\u9700\u8981\u7528\u9017\u53f7\u5c06\u53d8\u91cf\u540d\u5206\u5272\u5f00\u6765\u5373\u53ef\u3002\u4e0b\u9762\u4e24\u4e2a\u4ee3\u7801\u7247\u6bb5\u7684\u6548\u679c\u662f\u4e00\u6837\u7684\uff1a 1 2 int a ; int b ; 1 int a , b ; \u5982\u679c\u5c5e\u4e8e\u4e0a\u8ff0\u65b9\u5f0f\u6765\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\uff0c\u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u4f1a\u72af\u4e0b\u4e24\u79cd\u9519\u8bef\uff08\u8fd9\u6ca1\u6709\u4ec0\u4e48\u5927\u4e0d\u4e86\u7684\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u4f1a\u6355\u83b7\u8fd9\u4e9b\u9519\u8bef\u5e76\u8981\u6c42\u4f60\u8fdb\u884c\u4fee\u590d\uff09\uff1a \u7b2c\u4e00\u79cd\u9519\u8bef\u662f\u4e3a\u6bcf\u4e2a\u53d8\u91cf\u90fd\u6dfb\u52a0\u7c7b\u578b\u8bf4\u660e\u3002 1 2 3 int a , int b ; // wrong (compiler error) int a , b ; // correct \u7b2c\u4e8c\u79cd\u9519\u8bef\u662f\u5728\u4e00\u4e2a\u8bed\u53e5\u4e2d\u5b9a\u4e49\u4e24\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u8fd9\u662f\u4e0d\u88ab\u5141\u8bb8\u7684\u3002\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u53d8\u91cf\u5fc5\u987b\u5206\u522b\u5b9a\u4e49\u3002 1 2 3 4 5 6 7 int a , double b ; // wrong (compiler error) int a ; double b ; // correct (but not recommended) // correct and recommended (easier to read) int a ; double b ; \u6700\u4f73\u5b9e\u8df5 \u5c3d\u7ba1\u5728\u8bed\u8a00\u5c42\u9762\uff0c\u4f7f\u7528\u5355\u4e00\u8bed\u53e5\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\u662f\u88ab\u5141\u8bb8\u7684\uff0c\u4f46\u662f\u5728\u5b9e\u8df5\u4e2d\u5e94\u5f53\u5c3d\u91cf\u907f\u514d\u8fd9\u6837\u53bb\u505a\uff08\u5373\u65f6\u5b83\u4eec\u5177\u6709\u76f8\u540c\u7684\u7c7b\u578b\uff09\u3002\u6b63\u786e\u7684\u505a\u6cd5\u662f\u4e00\u4e2a\u8bed\u53e5\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\uff08\u5e76\u4f7f\u7528\u5355\u884c\u6ce8\u91ca\u89e3\u91ca\u8be5\u53d8\u91cf\u7684\u7528\u9014\uff09\u3002","title":"\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf"},{"location":"1-3-Introduction-to-objects-and-variables/#_6","text":"\u5728 C++ \u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u53d8\u91cf\u6765\u8bbf\u95ee\u5185\u5b58\u3002\u53d8\u91cf\u5177\u6709\u6807\u8bc6\u7b26\u3001\u7c7b\u578b\u548c\u503c\uff08\u4ee5\u53ca\u5176\u4ed6\u4e00\u4e9b\u4e0e\u6b64\u5904\u4e0d\u76f8\u5173\u7684\u5c5e\u6027\uff09\u3002\u53d8\u91cf\u7c7b\u578b\u7528\u4e8e\u786e\u5b9a\u5185\u5b58\u4e2d\u7684\u503c\u5e94\u8be5\u88ab\u5982\u4f55\u89e3\u6790\u3002 \u5728\u4e0b\u4e00\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5982\u4f55\u4e3a\u53d8\u91cf\u8d4b\u503c\u5e76\u4f7f\u7528\u5b83\u4eec\u3002","title":"\u5c0f\u7ed3"},{"location":"1-4-Variable-assignment-and-initialization/","tags":["variable","initialization"],"text":"\u5728\u4e0a\u4e00\u8282\u8bfe 1.3 \u5bf9\u8c61\u548c\u53d8\u91cf \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u3002\u5728\u8fd9\u4e00\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5982\u4f55\u4e3a\u53d8\u91cf\u8d4b\u503c\u5e76\u4f7f\u7528\u5b83\u5b58\u50a8\u7684\u503c\u3002 \u9996\u5148\u56de\u5fc6\u4e00\u4e0b\uff0c\u4e0b\u9762\u8fd9\u4e2a\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c\u6211\u4eec\u5728\u5185\u5b58\u4e2d\u4e3a\u4e00\u4e2a\u540d\u4e3a x \u7684\u6574\u578b\u53d8\u91cf\u5206\u914d\u4e86\u5185\u5b58\uff0c\u7136\u540e\u7c7b\u4f3c\u5730\uff0c\u53c8\u521b\u5efa\u4e86\u4e24\u4e2a\u6574\u578b\u53d8\u91cf\uff0c\u5206\u522b\u53eb\u505a y \u548c z\uff1a 1 2 int x ; // define an integer variable named x int y , z ; // define two integer variables, named y and z \u53d8\u91cf\u8d4b\u503c \u53d8\u91cf\u521b\u5efa\u5b8c\u6bd5\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd0\u7b97\u7b26 = \u4e3a\u5176\u8d4b\u503c\uff08\u5728\u53e6\u5916\u4e00\u4e2a\u5355\u72ec\u7684\u8bed\u53e5\u4e2d\uff09\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u79f0\u4e3a\u62f7\u8d1d\u8d4b\u503c\uff08\u6216\u8d4b\u503c\uff09\u3002 1 2 3 4 int width ; // define an integer variable named width width = 5 ; // copy assignment of value 5 into variable width // variable width now has value 5 \u62f7\u8d1d\u8d4b\u503c\u540d\u5b57\u7684\u6765\u5386\uff0c\u6e90\u4e8e\u4e0a\u8ff0\u64cd\u4f5c\u5c06\u8d4b\u503c\u8fd0\u7b97\u7b26\u53f3\u4fa7\u7684\u503c\u62f7\u8d1d\u540e\u8d4b\u503c\u7ed9\u4e86\u5de6\u4fa7\u7684\u53d8\u91cf\u3002= \u79f0\u4e3a\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5148\u540e\u4e24\u6b21\u4f7f\u7528\u4e86\u8d4b\u503c\u64cd\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int width ; width = 5 ; // copy assignment of value 5 into variable width // variable width now has value 5 width = 7 ; // change value stored in variable width to 7 // variable width now has value 7 return 0 ; } \u5728\u6211\u4eec\u628a 7 \u8d4b\u503c\u7ed9\u53d8\u91cf width \u7684\u65f6\u5019\uff0c\u4e4b\u524d\u7684\u503c\uff085\uff09\u5c31\u88ab\u8986\u76d6\u6389\u4e86\u3002\u4e00\u822c\u7684\u53d8\u91cf\u53ea\u80fd\u591f\u5b58\u50a8\u4e00\u4e2a\u503c\u3002 \u6ce8\u610f \u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u72af\u7684\u9519\u8bef\u4e4b\u4e00\u5c31\u662f\u5c06\u8d4b\u503c\u8fd0\u7b97\u7b26( = )\u548c\u76f8\u7b49\u6bd4\u8f83\u8fd0\u7b97\u7b26( == )\u5f04\u6df7\u6dc6\u3002\u8d4b\u503c\u8fd0\u7b97\u7b26\u7528\u4e8e\u7ed9\u53d8\u91cf\u8d4b\u503c\uff0c\u800c\u76f8\u7b49\u6bd4\u8f83\u8fd0\u7b97\u7b26\u5219\u7528\u4e8e\u6821\u9a8c\u4e24\u4e2a\u64cd\u4f5c\u6570\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002 \u521d\u59cb\u5316 \u4e0a\u8ff0\u8d4b\u503c\u8fc7\u7a0b\u7684\u7f3a\u70b9\u4e4b\u4e00\uff0c\u662f\u5b83\u9700\u8981\u4e24\u6761\u8bed\u53e5\u624d\u80fd\u5b8c\u6210\uff1a\u9996\u5148\u5b9a\u4e49\u53d8\u91cf\uff0c\u7136\u540e\u4e3a\u53d8\u91cf\u8d4b\u503c\u3002 \u8fd9\u4e24\u6b65\u5176\u5b9e\u662f\u53ef\u4ee5\u5408\u5e76\u7684\u3002\u5728\u5b9a\u4e49\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u8fd9\u4e2a\u53d8\u91cf\u8bbe\u7f6e\u4e00\u4e2a\u521d\u59cb\u503c\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a\u521d\u59cb\u5316\u3002\u7528\u4e8e\u521d\u59cb\u5316\u53d8\u91cf\u7684\u8fd9\u4e2a\u503c\uff0c\u79f0\u4e3a initializer \u3002 C++ \u4e2d\u7684\u521d\u59cb\u5316\u662f\u51fa\u5947\u7684\u590d\u6742\uff0c\u8fd9\u91cc\u5411\u4f60\u5c55\u793a\u4e00\u4e2a\u7b80\u5316\u540e\u7684\u7248\u672c\u3002 \u5728 C++ \u4e2d\u6709 4 \u79cd\u57fa\u672c\u7684\u53d8\u91cf\u521d\u59cb\u5316\u65b9\u6cd5\uff1a 1 2 3 4 int a ; // \u65e0\u521d\u59cb\u5316\u503c int b = 5 ; // \u5728\u7b49\u53f7\u540e\u653e\u7f6e\u521d\u59cb\u5316\u503c int c ( 6 ); // \u5728\u62ec\u53f7\u4e2d\u653e\u7f6e\u521d\u59cb\u5316\u503c int d { 7 }; // \u5728\u82b1\u62ec\u53f7\u4e2d\u653e\u7f6e\u521d\u59cb\u5316\u503c \u4f60\u53ef\u80fd\u5df2\u7ecf\u6ce8\u610f\u5230\u4e86\u4e0a\u8ff0\u4ee3\u7801\u7247\u6bb5\u4e2d\u989d\u5916\u7684\u7a7a\u683c\uff08\u4f8b\u5982 int d{7}; \uff09\u3002\u662f\u5426\u4f7f\u7528\u8fd9\u4e9b\u7a7a\u683c\u6765\u589e\u5f3a\u53ef\u8bfb\u6027\uff0c\u5c5e\u4e8e\u4e00\u79cd\u4e2a\u4eba\u504f\u597d\u3002 \u9ed8\u8ba4\u521d\u59cb\u5316 \u5728\u4e0d\u63d0\u4f9b\u4efb\u4f55\u521d\u59cb\u5316\u503c\u7684\u65f6\u5019\uff08\u4f8b\u5982\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u53d8\u91cf a \uff09\uff0c\u5c06\u8fdb\u884c\u9ed8\u8ba4\u521d\u59cb\u5316\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u9ed8\u8ba4\u521d\u59cb\u5316\u4f1a\u5bfc\u81f4\u53d8\u91cf\u5b58\u653e\u4e00\u4e2a\u4e0d\u786e\u5b9a\u503c\u3002\u76f8\u5173\u5185\u5bb9\u6211\u4eec\u4f1a\u5728 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u62f7\u8d1d\u521d\u59cb\u5316 \u5982\u679c\u7b49\u53f7\u540e\u9762\u63d0\u4f9b\u4e86\u521d\u59cb\u5316\u503c\uff0c\u6b64\u65f6\u5c06\u8fdb\u884c\u62f7\u8d1d\u521d\u59cb\u5316\u3002\u62f7\u8d1d\u521d\u59cb\u5316\u7ee7\u627f\u81ea C \u8bed\u8a00\u3002 1 int width = 5 ; // copy initialization of value 5 into variable width \u548c\u62f7\u8d1d\u8d4b\u503c\u5f88\u7c7b\u4f3c\uff0c\u62f7\u8d1d\u521d\u59cb\u5316\u4f1a\u5c06\u7b49\u53f7\u53f3\u8fb9\u7684\u503c\u62f7\u8d1d\u5230\u5de6\u4fa7\u521b\u5efa\u7684\u53d8\u91cf\u3002 \u5bf9\u4e8e\u4e0a\u8ff0\u7b80\u5355\u7c7b\u578b int \u800c\u8a00\uff0c\u62f7\u8d1d\u521d\u59cb\u5316\u662f\u975e\u5e38\u9ad8\u6548\u7684\u3002\u7136\u800c\uff0c\u5728\u5904\u7406\u590d\u6742\u7c7b\u578b\u65f6\uff0c\u62f7\u8d1d\u521d\u59cb\u5316\u5219\u662f\u4f4e\u6548\u7684\u3002 \u76f4\u63a5\u521d\u59cb\u5316 \u5f53\u6211\u4eec\u5c06\u521d\u59cb\u5316\u503c\u7f6e\u4e8e\u62ec\u53f7\u5185\u65f6\uff0c\u5c06\u8fdb\u884c\u76f4\u63a5\u521d\u59cb\u5316\u3002 1 int width ( 5 ); // direct initialization of value 5 into variable width \u5bf9\u4e8e int \u8fd9\u6837\u7684\u7b80\u5355\u6570\u636e\u7c7b\u578b\u800c\u8a00\uff0c\u62f7\u8d1d\u521d\u59cb\u5316\u548c\u76f4\u63a5\u521d\u59cb\u5316\u672c\u8d28\u4e0a\u51e0\u4e4e\u6ca1\u4ec0\u4e48\u533a\u522b\u3002\u5bf9\u4e8e\u590d\u6742\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u76f4\u63a5\u521d\u59cb\u5316\u76f8\u5bf9\u4e8e\u62f7\u8d1d\u521d\u59cb\u5316\u5219\u66f4\u52a0\u9ad8\u6548\u3002 \u62ec\u53f7\u521d\u59cb\u5316 \u4e0d\u5e78\u7684\u662f\uff0c\u76f4\u63a5\u521d\u59cb\u5316\u5e76\u4e0d\u9002\u7528\u4e8e\u6240\u6709\u7c7b\u578b\uff08\u4f8b\u5982\u521d\u59cb\u5316\u4e00\u4e2a\u5305\u542b\u4e00\u4e2a\u5217\u8868\u7684\u5bf9\u8c61\uff09\u3002\u4e3a\u4e86\u63d0\u4f9b\u66f4\u4e3a\u4e00\u81f4\u7684\u521d\u59cb\u5316\u673a\u5236\uff0cC++ \u652f\u6301\u4e86\u57fa\u4e8e\u82b1\u62ec\u53f7\u7684 \u62ec\u53f7\u521d\u59cb\u5316 \uff08\u4e5f\u79f0\u4e3a \u7edf\u4e00\u521d\u59cb\u5316 \u6216 \u5217\u8868\u521d\u59cb\u5316 \uff09\u3002 \u62ec\u53f7\u521d\u59cb\u5316\u6709\u4e09\u79cd\u5f62\u5f0f\u3002 1 2 3 int width { 5 }; // \u76f4\u63a5\u62ec\u53f7\u521d\u59cb\u5316\uff08\u63a8\u8350\uff09 int height = { 6 }; // \u62f7\u8d1d\u62ec\u53f7\u521d\u59cb\u5316 int depth {}; // \u503c\u521d\u59cb\u5316\uff08\u8be6\u89c1\u4e0b\u8282\uff09 \u76f4\u63a5\u62ec\u53f7\u521d\u59cb\u5316\u548c\u62f7\u8d1d\u62ec\u53f7\u521d\u59cb\u5316\u51e0\u4e4e\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u4f46\u901a\u5e38\u66f4\u63a8\u8350\u76f4\u63a5\u62ec\u53f7\u521d\u59cb\u5316\u3002 \u62ec\u53f7\u521d\u59cb\u5316\u7684\u4e00\u4e2a\u989d\u5916\u4f18\u70b9\u662f\uff0c \u62ec\u53f7\u521d\u59cb\u5316\u4e0d\u5141\u8bb8\u9690\u5f0f\u7f29\u7a84\u8f6c\u6362 \u7684\u53d1\u751f\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u62ec\u53f7\u521d\u59cb\u5316\u53bb\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u5982\u679c\u521d\u59cb\u5316\u503c\u5e76\u4e0d\u80fd\u88ab\u201d\u5b89\u5168\u7684\u201c\u5b58\u50a8\uff0c\u7f16\u8bd1\u5668\u4f1a\u629b\u51fa\u4e00\u4e2a\u544a\u8b66\u6216\u9519\u8bef\u3002\u4f8b\u5982\uff1a 1 int width { 4.5 }; // \u9519\u8bef: not all double values fit into an int \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c\u6211\u4eec\u5c1d\u8bd5\u5c06\u4e00\u4e2a\u5305\u542b\u4e86\u5c0f\u6570\u90e8\u5206\u7684\u503c 4.5 \u8d4b\u503c\u7ed9\u4e00\u4e2a\u6574\u578b\u53d8\u91cf\uff08\u53ea\u80fd\u5b58\u50a8\u6574\u6570\u90e8\u5206\uff09\u3002\u62f7\u8d1d\u548c\u76f4\u63a5\u521d\u59cb\u5316\u90fd\u4f1a\u4e22\u5f03\u5c0f\u6570\u90e8\u5206\uff0c\u6700\u7ec8\u7ed3\u679c\u5c31\u662f\u6b63\u6570\u90e8\u5206 4 \u88ab\u5b58\u50a8\u5728\u4e86\u53d8\u91cf width \u4e2d\u3002\u800c\u5bf9\u4e8e\u62ec\u53f7\u521d\u59cb\u5316\uff0c\u4e0a\u8ff0\u64cd\u4f5c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\uff08\u901a\u5e38\u6765\u8bb2\u8fd9\u662f\u4ef6\u597d\u4e8b\uff0c\u56e0\u4e3a\u5f88\u5c11\u6709\u6545\u610f\u4e22\u5931\u6570\u636e\u7cbe\u5ea6\u7684\u9700\u6c42\uff09\u3002\u4e0d\u4f1a\u9020\u6210\u6f5c\u5728\u6570\u636e\u4e22\u5931\u7684\u7c7b\u578b\u8f6c\u6362\u5219\u662f\u5141\u8bb8\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u53ea\u8981\u6761\u4ef6\u5141\u8bb8\uff0c\u5e94\u8be5\u5c3d\u53ef\u80fd\u4f7f\u7528\u62ec\u53f7\u521d\u59cb\u5316\u3002 Q & A Q\uff1bC++ \u63d0\u4f9b\u4e86\u62f7\u8d1d\u521d\u59cb\u5316\u3001\u76f4\u63a5\u521d\u59cb\u5316\u548c\u62ec\u53f7\u521d\u59cb\u5316\u3002\u90a3\u4e48\u662f\u5426\u5b58\u5728\u76f4\u63a5\u8d4b\u503c\u548c\u62ec\u53f7\u8d4b\u503c\u5462\uff1f A\uff1aC++ \u5e76\u4e0d\u652f\u6301\u76f4\u63a5\u8d4b\u503c\u548c\u62ec\u53f7\u8d4b\u503c\u8bed\u6cd5 \u503c\u521d\u59cb\u5316\u548c\u96f6\u521d\u59cb\u5316 \u5982\u679c\u4f7f\u7528\u7a7a\u7684\u82b1\u62ec\u53f7\u6765\u521d\u59cb\u5316\u53d8\u91cf\uff0c\u5219\u4f1a\u8fdb\u884c\u503c\u521d\u59cb\u5316\uff0c\u503c\u521d\u59cb\u5316\u4f1a\u628a\u53d8\u91cf\u521d\u59cb\u503c\u8bbe\u7f6e\u4e3a 0\uff08\u6216\u8005\u7a7a\uff0c\u5982\u679c\u7a7a\u503c\u5bf9\u4e8e\u7ed9\u5b9a\u7c7b\u578b\u662f\u5408\u9002\u7684\u521d\u503c\u7684\u8bdd\uff09\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u521d\u59cb\u5316\u4e3a\u5168 0\uff0c\u4e5f\u79f0\u4e4b\u4e3a\u96f6\u521d\u59cb\u5316\u3002 1 int width {}; // zero initialization to value 0 Q & A Q: \u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u4f7f\u7528 { 0 } \uff0c\u662f\u4ec0\u4e48\u65f6\u5019\u4f7f\u7528 {}? A: \u5982\u679c\u4f60\u4f7f\u7528\u7684\u7684\u786e\u662f 0 \u8fd9\u4e2a\u5177\u4f53\u503c\uff0c\u90a3\u4e48\u5c31\u5e94\u5f53\u660e\u786e\u5730\u5c06\u5176\u521d\u59cb\u5316\u4e3a 0 1 2 int x { 0 }; // explicit initialization to value 0 std::cout << x; // we're using that zero value A: \u5982\u679c\u521d\u59cb\u5316\u7684\u503c\u53ea\u662f\u4e34\u65f6\u503c\u4e14\u5373\u5c06\u88ab\u66ff\u6362\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u503c\u521d\u59cb\u5316\u3002 1 2 int x {}; // value initialization std::cin >> x; // we're immediately replacing that value \u4e60\u60ef\u6027\u5730\u521d\u59cb\u5316\u53d8\u91cf \u53d8\u91cf\u5728\u521b\u5efa\u65f6\uff0c\u5c31\u5e94\u8be5\u521d\u59cb\u5316\u3002\u4e5f\u8bb8\u4f60\u5c06\u6765\u4f1a\u9047\u5230\u67d0\u79cd\u7279\u4f8b\uff08\u4f8b\u5982\u67d0\u6bb5\u6027\u80fd\u53ca\u5176\u5173\u952e\u7684\u4ee3\u7801\u4e2d\u6240\u5305\u542b\u7684\u5927\u91cf\u53d8\u91cf\uff09\u5141\u8bb8\u4f60\u4e0d\u53bb\u521d\u59cb\u5316\u53d8\u91cf\uff0c\u524d\u63d0\u662f\u4f60\u662f\u6709\u610f\u800c\u4e3a\u4e4b\u7684\uff08\u907f\u514d\u56e0\u4e3a\u5fd8\u8bb0\u4e86\u800c\u6ca1\u6709\u521d\u59cb\u5316\u53d8\u91cf\u7684\u8fd9\u79cd\u60c5\u51b5\uff09 \u5173\u4e8e\u8fd9\u4e2a\u8bdd\u9898\u7684\u8ba8\u8bba\uff0cBjarne Stroustrup (C++\u4e4b\u7236) \u548c Herb Sutter (C++ \u5927\u5e08) \u63d0\u4f9b\u4e86\u4ed6\u4eec\u7684\u89c1\u89e3\uff0c\u8be6\u7ec6\u5185\u5bb9\u8bf7\u53c2\u8003 \u8fd9\u91cc . \u6211\u4eec\u4f1a\u5728 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a \u4e2d\u8be6\u7ec6\u8ba8\u8bba\u5f53\u6211\u4eec\u8bd5\u56fe\u4f7f\u7528\u4e00\u4e2a\u6ca1\u6709\u88ab\u201d\u6070\u5f53\u5b9a\u4e49\u201c\u7684\u53d8\u91cf\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002 !!! question Q&A 1 \u5728\u521b\u5efa\u53d8\u91cf\u65f6\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\u3002 \u521d\u59cb\u5316\u591a\u4e2a\u53d8\u91cf \u5728\u4e0a\u4e00\u8282\u8bfe\u4e2d\u6211\u4eec\u63d0\u5230\uff0cC++\u652f\u6301\u4f7f\u7528\u4e00\u4e2a\u8bed\u53e5\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\uff08\u901a\u8fc7\u9017\u53f7\u5206\u5272\u53d8\u91cf\u540d\uff09\uff1a 1 int a , b ; \u540c\u65f6\u6211\u4eec\u4e5f\u63d0\u5230\uff0c\u6700\u4f73\u5b9e\u8df5\u5176\u5b9e\u662f\u907f\u514d\u4f7f\u7528\u8be5\u8bed\u6cd5\u3002\u4e0d\u8fc7\uff0c\u7531\u4e8e\u6211\u4eec\u96be\u514d\u8fd8\u662f\u4f1a\u9047\u5230\u6b64\u7c7b\u98ce\u683c\u7684\u4ee3\u7801\uff0c\u5bf9\u5b83\u591a\u4e9b\u4e86\u89e3\u4e5f\u672a\u5c1d\u4e0d\u662f\u4ef6\u597d\u4e8b\u3002\u540c\u65f6\u4e5f\u80fd\u591f\u518d\u6b21\u63d0\u9192\u4f60\u907f\u514d\u4f7f\u7528\u8fd9\u79cd\u8bed\u6cd5\u3002 \u4f60\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u521d\u59cb\u5316\u591a\u4e2a\u53d8\u91cf\uff1a 1 2 3 int a = 5 , b = 6 ; // copy initialization int c ( 7 ), d ( 8 ); // direct initialization int e { 9 }, f { 10 }; // brace initialization (preferred) \u4e0d\u5e78\u7684\u662f\uff0c\u8fd9\u91cc\u6709\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u5751\u3002\u5f53\u7a0b\u5e8f\u5458\u9519\u8bef\u5730\u4f7f\u7528\u4e86\u4e00\u4e2a\u521d\u59cb\u5316\u8bed\u53e5\u5e76\u671f\u671b\u5b83\u80fd\u591f\u521d\u59cb\u5316\u591a\u4e2a\u53d8\u91cf\u65f6\uff0c\u9519\u8bef\u4fbf\u53ef\u80fd\u53d1\u751f\uff1a 1 2 3 int a , b = 5 ; // wrong (a is not initialized!) int a = 5 , b = 5 ; // correct \u5728\u7b2c\u4e00\u884c\u8bed\u53e5\u4e2d\uff0c\u53d8\u91cf a \u5e76\u6ca1\u6709\u88ab\u521d\u59cb\u5316\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5bf9\u6b64\u53d1\u51fa\u63d0\u9192\uff0c\u4e5f\u53ef\u80fd\u4e0d\u4f1a\u3002\u5982\u679c\u7f16\u8bd1\u5668\u5ffd\u7565\u4e86\u8be5\u95ee\u9898\uff0c\u90a3\u4e48\u4f60\u7684\u7a0b\u5e8f\u5c06\u53ef\u80fd\u56e0\u6b64\u95f4\u6b47\u6027\u5730\u5d29\u6e83\u6216\u8f93\u51fa\u9519\u8bef\u7ed3\u679c\u3002\u6211\u4eec\u7a0d\u540e\u5c31\u4f1a\u4ecb\u7ecd\u4f7f\u7528\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\u4f1a\u5e26\u6765\u54ea\u4e9b\u95ee\u9898\u3002 \u6709\u4e00\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8bb0\u5fc6\u5e76\u907f\u514d\u4e0a\u8ff0\u95ee\u9898\uff0c\u5982\u679c\u4f60\u8003\u8651\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\u6216\u8005\u62ec\u53f7\u521d\u59cb\u5316\u7684\u573a\u666f\uff0c\u4e0a\u8ff0\u5199\u6cd5\u663e\u800c\u6613\u89c1\u662f\u6709\u95ee\u9898\u7684\uff1a 1 2 int a , b ( 5 ); int c , d { 5 }; \u4f7f\u7528\u4e0a\u9762\u8fd9\u79cd\u5199\u6cd5\u6765\u8bf4\u660e\uff0c\u770b\u8d77\u6765\u66f4\u6e05\u695a\uff0c5 \u88ab\u7528\u4e8e\u521d\u59cb\u5316 b \u6216\u8005 d \uff0c\u800c\u4e0d\u662f a \u6216\u8005 c","title":"1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316"},{"location":"1-4-Variable-assignment-and-initialization/#_1","text":"\u53d8\u91cf\u521b\u5efa\u5b8c\u6bd5\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd0\u7b97\u7b26 = \u4e3a\u5176\u8d4b\u503c\uff08\u5728\u53e6\u5916\u4e00\u4e2a\u5355\u72ec\u7684\u8bed\u53e5\u4e2d\uff09\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u79f0\u4e3a\u62f7\u8d1d\u8d4b\u503c\uff08\u6216\u8d4b\u503c\uff09\u3002 1 2 3 4 int width ; // define an integer variable named width width = 5 ; // copy assignment of value 5 into variable width // variable width now has value 5 \u62f7\u8d1d\u8d4b\u503c\u540d\u5b57\u7684\u6765\u5386\uff0c\u6e90\u4e8e\u4e0a\u8ff0\u64cd\u4f5c\u5c06\u8d4b\u503c\u8fd0\u7b97\u7b26\u53f3\u4fa7\u7684\u503c\u62f7\u8d1d\u540e\u8d4b\u503c\u7ed9\u4e86\u5de6\u4fa7\u7684\u53d8\u91cf\u3002= \u79f0\u4e3a\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5148\u540e\u4e24\u6b21\u4f7f\u7528\u4e86\u8d4b\u503c\u64cd\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int width ; width = 5 ; // copy assignment of value 5 into variable width // variable width now has value 5 width = 7 ; // change value stored in variable width to 7 // variable width now has value 7 return 0 ; } \u5728\u6211\u4eec\u628a 7 \u8d4b\u503c\u7ed9\u53d8\u91cf width \u7684\u65f6\u5019\uff0c\u4e4b\u524d\u7684\u503c\uff085\uff09\u5c31\u88ab\u8986\u76d6\u6389\u4e86\u3002\u4e00\u822c\u7684\u53d8\u91cf\u53ea\u80fd\u591f\u5b58\u50a8\u4e00\u4e2a\u503c\u3002 \u6ce8\u610f \u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u72af\u7684\u9519\u8bef\u4e4b\u4e00\u5c31\u662f\u5c06\u8d4b\u503c\u8fd0\u7b97\u7b26( = )\u548c\u76f8\u7b49\u6bd4\u8f83\u8fd0\u7b97\u7b26( == )\u5f04\u6df7\u6dc6\u3002\u8d4b\u503c\u8fd0\u7b97\u7b26\u7528\u4e8e\u7ed9\u53d8\u91cf\u8d4b\u503c\uff0c\u800c\u76f8\u7b49\u6bd4\u8f83\u8fd0\u7b97\u7b26\u5219\u7528\u4e8e\u6821\u9a8c\u4e24\u4e2a\u64cd\u4f5c\u6570\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002","title":"\u53d8\u91cf\u8d4b\u503c"},{"location":"1-4-Variable-assignment-and-initialization/#_2","text":"\u4e0a\u8ff0\u8d4b\u503c\u8fc7\u7a0b\u7684\u7f3a\u70b9\u4e4b\u4e00\uff0c\u662f\u5b83\u9700\u8981\u4e24\u6761\u8bed\u53e5\u624d\u80fd\u5b8c\u6210\uff1a\u9996\u5148\u5b9a\u4e49\u53d8\u91cf\uff0c\u7136\u540e\u4e3a\u53d8\u91cf\u8d4b\u503c\u3002 \u8fd9\u4e24\u6b65\u5176\u5b9e\u662f\u53ef\u4ee5\u5408\u5e76\u7684\u3002\u5728\u5b9a\u4e49\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u8fd9\u4e2a\u53d8\u91cf\u8bbe\u7f6e\u4e00\u4e2a\u521d\u59cb\u503c\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a\u521d\u59cb\u5316\u3002\u7528\u4e8e\u521d\u59cb\u5316\u53d8\u91cf\u7684\u8fd9\u4e2a\u503c\uff0c\u79f0\u4e3a initializer \u3002 C++ \u4e2d\u7684\u521d\u59cb\u5316\u662f\u51fa\u5947\u7684\u590d\u6742\uff0c\u8fd9\u91cc\u5411\u4f60\u5c55\u793a\u4e00\u4e2a\u7b80\u5316\u540e\u7684\u7248\u672c\u3002 \u5728 C++ \u4e2d\u6709 4 \u79cd\u57fa\u672c\u7684\u53d8\u91cf\u521d\u59cb\u5316\u65b9\u6cd5\uff1a 1 2 3 4 int a ; // \u65e0\u521d\u59cb\u5316\u503c int b = 5 ; // \u5728\u7b49\u53f7\u540e\u653e\u7f6e\u521d\u59cb\u5316\u503c int c ( 6 ); // \u5728\u62ec\u53f7\u4e2d\u653e\u7f6e\u521d\u59cb\u5316\u503c int d { 7 }; // \u5728\u82b1\u62ec\u53f7\u4e2d\u653e\u7f6e\u521d\u59cb\u5316\u503c \u4f60\u53ef\u80fd\u5df2\u7ecf\u6ce8\u610f\u5230\u4e86\u4e0a\u8ff0\u4ee3\u7801\u7247\u6bb5\u4e2d\u989d\u5916\u7684\u7a7a\u683c\uff08\u4f8b\u5982 int d{7}; \uff09\u3002\u662f\u5426\u4f7f\u7528\u8fd9\u4e9b\u7a7a\u683c\u6765\u589e\u5f3a\u53ef\u8bfb\u6027\uff0c\u5c5e\u4e8e\u4e00\u79cd\u4e2a\u4eba\u504f\u597d\u3002","title":"\u521d\u59cb\u5316"},{"location":"1-4-Variable-assignment-and-initialization/#_3","text":"\u5728\u4e0d\u63d0\u4f9b\u4efb\u4f55\u521d\u59cb\u5316\u503c\u7684\u65f6\u5019\uff08\u4f8b\u5982\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u53d8\u91cf a \uff09\uff0c\u5c06\u8fdb\u884c\u9ed8\u8ba4\u521d\u59cb\u5316\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u9ed8\u8ba4\u521d\u59cb\u5316\u4f1a\u5bfc\u81f4\u53d8\u91cf\u5b58\u653e\u4e00\u4e2a\u4e0d\u786e\u5b9a\u503c\u3002\u76f8\u5173\u5185\u5bb9\u6211\u4eec\u4f1a\u5728 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"\u9ed8\u8ba4\u521d\u59cb\u5316"},{"location":"1-4-Variable-assignment-and-initialization/#_4","text":"\u5982\u679c\u7b49\u53f7\u540e\u9762\u63d0\u4f9b\u4e86\u521d\u59cb\u5316\u503c\uff0c\u6b64\u65f6\u5c06\u8fdb\u884c\u62f7\u8d1d\u521d\u59cb\u5316\u3002\u62f7\u8d1d\u521d\u59cb\u5316\u7ee7\u627f\u81ea C \u8bed\u8a00\u3002 1 int width = 5 ; // copy initialization of value 5 into variable width \u548c\u62f7\u8d1d\u8d4b\u503c\u5f88\u7c7b\u4f3c\uff0c\u62f7\u8d1d\u521d\u59cb\u5316\u4f1a\u5c06\u7b49\u53f7\u53f3\u8fb9\u7684\u503c\u62f7\u8d1d\u5230\u5de6\u4fa7\u521b\u5efa\u7684\u53d8\u91cf\u3002 \u5bf9\u4e8e\u4e0a\u8ff0\u7b80\u5355\u7c7b\u578b int \u800c\u8a00\uff0c\u62f7\u8d1d\u521d\u59cb\u5316\u662f\u975e\u5e38\u9ad8\u6548\u7684\u3002\u7136\u800c\uff0c\u5728\u5904\u7406\u590d\u6742\u7c7b\u578b\u65f6\uff0c\u62f7\u8d1d\u521d\u59cb\u5316\u5219\u662f\u4f4e\u6548\u7684\u3002","title":"\u62f7\u8d1d\u521d\u59cb\u5316"},{"location":"1-4-Variable-assignment-and-initialization/#_5","text":"\u5f53\u6211\u4eec\u5c06\u521d\u59cb\u5316\u503c\u7f6e\u4e8e\u62ec\u53f7\u5185\u65f6\uff0c\u5c06\u8fdb\u884c\u76f4\u63a5\u521d\u59cb\u5316\u3002 1 int width ( 5 ); // direct initialization of value 5 into variable width \u5bf9\u4e8e int \u8fd9\u6837\u7684\u7b80\u5355\u6570\u636e\u7c7b\u578b\u800c\u8a00\uff0c\u62f7\u8d1d\u521d\u59cb\u5316\u548c\u76f4\u63a5\u521d\u59cb\u5316\u672c\u8d28\u4e0a\u51e0\u4e4e\u6ca1\u4ec0\u4e48\u533a\u522b\u3002\u5bf9\u4e8e\u590d\u6742\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u76f4\u63a5\u521d\u59cb\u5316\u76f8\u5bf9\u4e8e\u62f7\u8d1d\u521d\u59cb\u5316\u5219\u66f4\u52a0\u9ad8\u6548\u3002","title":"\u76f4\u63a5\u521d\u59cb\u5316"},{"location":"1-4-Variable-assignment-and-initialization/#_6","text":"\u4e0d\u5e78\u7684\u662f\uff0c\u76f4\u63a5\u521d\u59cb\u5316\u5e76\u4e0d\u9002\u7528\u4e8e\u6240\u6709\u7c7b\u578b\uff08\u4f8b\u5982\u521d\u59cb\u5316\u4e00\u4e2a\u5305\u542b\u4e00\u4e2a\u5217\u8868\u7684\u5bf9\u8c61\uff09\u3002\u4e3a\u4e86\u63d0\u4f9b\u66f4\u4e3a\u4e00\u81f4\u7684\u521d\u59cb\u5316\u673a\u5236\uff0cC++ \u652f\u6301\u4e86\u57fa\u4e8e\u82b1\u62ec\u53f7\u7684 \u62ec\u53f7\u521d\u59cb\u5316 \uff08\u4e5f\u79f0\u4e3a \u7edf\u4e00\u521d\u59cb\u5316 \u6216 \u5217\u8868\u521d\u59cb\u5316 \uff09\u3002 \u62ec\u53f7\u521d\u59cb\u5316\u6709\u4e09\u79cd\u5f62\u5f0f\u3002 1 2 3 int width { 5 }; // \u76f4\u63a5\u62ec\u53f7\u521d\u59cb\u5316\uff08\u63a8\u8350\uff09 int height = { 6 }; // \u62f7\u8d1d\u62ec\u53f7\u521d\u59cb\u5316 int depth {}; // \u503c\u521d\u59cb\u5316\uff08\u8be6\u89c1\u4e0b\u8282\uff09 \u76f4\u63a5\u62ec\u53f7\u521d\u59cb\u5316\u548c\u62f7\u8d1d\u62ec\u53f7\u521d\u59cb\u5316\u51e0\u4e4e\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u4f46\u901a\u5e38\u66f4\u63a8\u8350\u76f4\u63a5\u62ec\u53f7\u521d\u59cb\u5316\u3002 \u62ec\u53f7\u521d\u59cb\u5316\u7684\u4e00\u4e2a\u989d\u5916\u4f18\u70b9\u662f\uff0c \u62ec\u53f7\u521d\u59cb\u5316\u4e0d\u5141\u8bb8\u9690\u5f0f\u7f29\u7a84\u8f6c\u6362 \u7684\u53d1\u751f\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u62ec\u53f7\u521d\u59cb\u5316\u53bb\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u5982\u679c\u521d\u59cb\u5316\u503c\u5e76\u4e0d\u80fd\u88ab\u201d\u5b89\u5168\u7684\u201c\u5b58\u50a8\uff0c\u7f16\u8bd1\u5668\u4f1a\u629b\u51fa\u4e00\u4e2a\u544a\u8b66\u6216\u9519\u8bef\u3002\u4f8b\u5982\uff1a 1 int width { 4.5 }; // \u9519\u8bef: not all double values fit into an int \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c\u6211\u4eec\u5c1d\u8bd5\u5c06\u4e00\u4e2a\u5305\u542b\u4e86\u5c0f\u6570\u90e8\u5206\u7684\u503c 4.5 \u8d4b\u503c\u7ed9\u4e00\u4e2a\u6574\u578b\u53d8\u91cf\uff08\u53ea\u80fd\u5b58\u50a8\u6574\u6570\u90e8\u5206\uff09\u3002\u62f7\u8d1d\u548c\u76f4\u63a5\u521d\u59cb\u5316\u90fd\u4f1a\u4e22\u5f03\u5c0f\u6570\u90e8\u5206\uff0c\u6700\u7ec8\u7ed3\u679c\u5c31\u662f\u6b63\u6570\u90e8\u5206 4 \u88ab\u5b58\u50a8\u5728\u4e86\u53d8\u91cf width \u4e2d\u3002\u800c\u5bf9\u4e8e\u62ec\u53f7\u521d\u59cb\u5316\uff0c\u4e0a\u8ff0\u64cd\u4f5c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\uff08\u901a\u5e38\u6765\u8bb2\u8fd9\u662f\u4ef6\u597d\u4e8b\uff0c\u56e0\u4e3a\u5f88\u5c11\u6709\u6545\u610f\u4e22\u5931\u6570\u636e\u7cbe\u5ea6\u7684\u9700\u6c42\uff09\u3002\u4e0d\u4f1a\u9020\u6210\u6f5c\u5728\u6570\u636e\u4e22\u5931\u7684\u7c7b\u578b\u8f6c\u6362\u5219\u662f\u5141\u8bb8\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u53ea\u8981\u6761\u4ef6\u5141\u8bb8\uff0c\u5e94\u8be5\u5c3d\u53ef\u80fd\u4f7f\u7528\u62ec\u53f7\u521d\u59cb\u5316\u3002 Q & A Q\uff1bC++ \u63d0\u4f9b\u4e86\u62f7\u8d1d\u521d\u59cb\u5316\u3001\u76f4\u63a5\u521d\u59cb\u5316\u548c\u62ec\u53f7\u521d\u59cb\u5316\u3002\u90a3\u4e48\u662f\u5426\u5b58\u5728\u76f4\u63a5\u8d4b\u503c\u548c\u62ec\u53f7\u8d4b\u503c\u5462\uff1f A\uff1aC++ \u5e76\u4e0d\u652f\u6301\u76f4\u63a5\u8d4b\u503c\u548c\u62ec\u53f7\u8d4b\u503c\u8bed\u6cd5","title":"\u62ec\u53f7\u521d\u59cb\u5316"},{"location":"1-4-Variable-assignment-and-initialization/#_7","text":"\u5982\u679c\u4f7f\u7528\u7a7a\u7684\u82b1\u62ec\u53f7\u6765\u521d\u59cb\u5316\u53d8\u91cf\uff0c\u5219\u4f1a\u8fdb\u884c\u503c\u521d\u59cb\u5316\uff0c\u503c\u521d\u59cb\u5316\u4f1a\u628a\u53d8\u91cf\u521d\u59cb\u503c\u8bbe\u7f6e\u4e3a 0\uff08\u6216\u8005\u7a7a\uff0c\u5982\u679c\u7a7a\u503c\u5bf9\u4e8e\u7ed9\u5b9a\u7c7b\u578b\u662f\u5408\u9002\u7684\u521d\u503c\u7684\u8bdd\uff09\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u521d\u59cb\u5316\u4e3a\u5168 0\uff0c\u4e5f\u79f0\u4e4b\u4e3a\u96f6\u521d\u59cb\u5316\u3002 1 int width {}; // zero initialization to value 0 Q & A Q: \u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u4f7f\u7528 { 0 } \uff0c\u662f\u4ec0\u4e48\u65f6\u5019\u4f7f\u7528 {}? A: \u5982\u679c\u4f60\u4f7f\u7528\u7684\u7684\u786e\u662f 0 \u8fd9\u4e2a\u5177\u4f53\u503c\uff0c\u90a3\u4e48\u5c31\u5e94\u5f53\u660e\u786e\u5730\u5c06\u5176\u521d\u59cb\u5316\u4e3a 0 1 2 int x { 0 }; // explicit initialization to value 0 std::cout << x; // we're using that zero value A: \u5982\u679c\u521d\u59cb\u5316\u7684\u503c\u53ea\u662f\u4e34\u65f6\u503c\u4e14\u5373\u5c06\u88ab\u66ff\u6362\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u503c\u521d\u59cb\u5316\u3002 1 2 int x {}; // value initialization std::cin >> x; // we're immediately replacing that value","title":"\u503c\u521d\u59cb\u5316\u548c\u96f6\u521d\u59cb\u5316"},{"location":"1-4-Variable-assignment-and-initialization/#_8","text":"\u53d8\u91cf\u5728\u521b\u5efa\u65f6\uff0c\u5c31\u5e94\u8be5\u521d\u59cb\u5316\u3002\u4e5f\u8bb8\u4f60\u5c06\u6765\u4f1a\u9047\u5230\u67d0\u79cd\u7279\u4f8b\uff08\u4f8b\u5982\u67d0\u6bb5\u6027\u80fd\u53ca\u5176\u5173\u952e\u7684\u4ee3\u7801\u4e2d\u6240\u5305\u542b\u7684\u5927\u91cf\u53d8\u91cf\uff09\u5141\u8bb8\u4f60\u4e0d\u53bb\u521d\u59cb\u5316\u53d8\u91cf\uff0c\u524d\u63d0\u662f\u4f60\u662f\u6709\u610f\u800c\u4e3a\u4e4b\u7684\uff08\u907f\u514d\u56e0\u4e3a\u5fd8\u8bb0\u4e86\u800c\u6ca1\u6709\u521d\u59cb\u5316\u53d8\u91cf\u7684\u8fd9\u79cd\u60c5\u51b5\uff09 \u5173\u4e8e\u8fd9\u4e2a\u8bdd\u9898\u7684\u8ba8\u8bba\uff0cBjarne Stroustrup (C++\u4e4b\u7236) \u548c Herb Sutter (C++ \u5927\u5e08) \u63d0\u4f9b\u4e86\u4ed6\u4eec\u7684\u89c1\u89e3\uff0c\u8be6\u7ec6\u5185\u5bb9\u8bf7\u53c2\u8003 \u8fd9\u91cc . \u6211\u4eec\u4f1a\u5728 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a \u4e2d\u8be6\u7ec6\u8ba8\u8bba\u5f53\u6211\u4eec\u8bd5\u56fe\u4f7f\u7528\u4e00\u4e2a\u6ca1\u6709\u88ab\u201d\u6070\u5f53\u5b9a\u4e49\u201c\u7684\u53d8\u91cf\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002 !!! question Q&A 1 \u5728\u521b\u5efa\u53d8\u91cf\u65f6\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\u3002","title":"\u4e60\u60ef\u6027\u5730\u521d\u59cb\u5316\u53d8\u91cf"},{"location":"1-4-Variable-assignment-and-initialization/#_9","text":"\u5728\u4e0a\u4e00\u8282\u8bfe\u4e2d\u6211\u4eec\u63d0\u5230\uff0cC++\u652f\u6301\u4f7f\u7528\u4e00\u4e2a\u8bed\u53e5\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\uff08\u901a\u8fc7\u9017\u53f7\u5206\u5272\u53d8\u91cf\u540d\uff09\uff1a 1 int a , b ; \u540c\u65f6\u6211\u4eec\u4e5f\u63d0\u5230\uff0c\u6700\u4f73\u5b9e\u8df5\u5176\u5b9e\u662f\u907f\u514d\u4f7f\u7528\u8be5\u8bed\u6cd5\u3002\u4e0d\u8fc7\uff0c\u7531\u4e8e\u6211\u4eec\u96be\u514d\u8fd8\u662f\u4f1a\u9047\u5230\u6b64\u7c7b\u98ce\u683c\u7684\u4ee3\u7801\uff0c\u5bf9\u5b83\u591a\u4e9b\u4e86\u89e3\u4e5f\u672a\u5c1d\u4e0d\u662f\u4ef6\u597d\u4e8b\u3002\u540c\u65f6\u4e5f\u80fd\u591f\u518d\u6b21\u63d0\u9192\u4f60\u907f\u514d\u4f7f\u7528\u8fd9\u79cd\u8bed\u6cd5\u3002 \u4f60\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u521d\u59cb\u5316\u591a\u4e2a\u53d8\u91cf\uff1a 1 2 3 int a = 5 , b = 6 ; // copy initialization int c ( 7 ), d ( 8 ); // direct initialization int e { 9 }, f { 10 }; // brace initialization (preferred) \u4e0d\u5e78\u7684\u662f\uff0c\u8fd9\u91cc\u6709\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u5751\u3002\u5f53\u7a0b\u5e8f\u5458\u9519\u8bef\u5730\u4f7f\u7528\u4e86\u4e00\u4e2a\u521d\u59cb\u5316\u8bed\u53e5\u5e76\u671f\u671b\u5b83\u80fd\u591f\u521d\u59cb\u5316\u591a\u4e2a\u53d8\u91cf\u65f6\uff0c\u9519\u8bef\u4fbf\u53ef\u80fd\u53d1\u751f\uff1a 1 2 3 int a , b = 5 ; // wrong (a is not initialized!) int a = 5 , b = 5 ; // correct \u5728\u7b2c\u4e00\u884c\u8bed\u53e5\u4e2d\uff0c\u53d8\u91cf a \u5e76\u6ca1\u6709\u88ab\u521d\u59cb\u5316\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5bf9\u6b64\u53d1\u51fa\u63d0\u9192\uff0c\u4e5f\u53ef\u80fd\u4e0d\u4f1a\u3002\u5982\u679c\u7f16\u8bd1\u5668\u5ffd\u7565\u4e86\u8be5\u95ee\u9898\uff0c\u90a3\u4e48\u4f60\u7684\u7a0b\u5e8f\u5c06\u53ef\u80fd\u56e0\u6b64\u95f4\u6b47\u6027\u5730\u5d29\u6e83\u6216\u8f93\u51fa\u9519\u8bef\u7ed3\u679c\u3002\u6211\u4eec\u7a0d\u540e\u5c31\u4f1a\u4ecb\u7ecd\u4f7f\u7528\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\u4f1a\u5e26\u6765\u54ea\u4e9b\u95ee\u9898\u3002 \u6709\u4e00\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8bb0\u5fc6\u5e76\u907f\u514d\u4e0a\u8ff0\u95ee\u9898\uff0c\u5982\u679c\u4f60\u8003\u8651\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\u6216\u8005\u62ec\u53f7\u521d\u59cb\u5316\u7684\u573a\u666f\uff0c\u4e0a\u8ff0\u5199\u6cd5\u663e\u800c\u6613\u89c1\u662f\u6709\u95ee\u9898\u7684\uff1a 1 2 int a , b ( 5 ); int c , d { 5 }; \u4f7f\u7528\u4e0a\u9762\u8fd9\u79cd\u5199\u6cd5\u6765\u8bf4\u660e\uff0c\u770b\u8d77\u6765\u66f4\u6e05\u695a\uff0c5 \u88ab\u7528\u4e8e\u521d\u59cb\u5316 b \u6216\u8005 d \uff0c\u800c\u4e0d\u662f a \u6216\u8005 c","title":"\u521d\u59cb\u5316\u591a\u4e2a\u53d8\u91cf"},{"location":"1-5-Introduction-to-iostream-cout-cin-and-endl/","tags":["iostream"],"text":"\u672c\u8282\u5c06\u91cd\u70b9\u4ecb\u7ecd std::cout \u3002\u6211\u4eec\u5728 Hello world! \u7a0b\u5e8f\u4e2d\u5df2\u7ecf\u4f7f\u7528\u5b83\u5411\u63a7\u5236\u53f0\u8f93\u51fa\u8fc7\u6587\u672c Hello world! \u3002\u6211\u4eec\u8fd8\u5c06\u4ecb\u7ecd\u5982\u4f55\u4ece\u7528\u6237\u83b7\u53d6\u8f93\u5165\uff0c\u4f7f\u7a0b\u5e8f\u5177\u5907\u4e0e\u7528\u6237\u4ea4\u4e92\u7684\u80fd\u529b\u3002 \u8f93\u5165\u8f93\u51fa\u5e93 \u8f93\u5165\u8f93\u51fa\u5e93 ( IO \u5e93) \u662f C++ \u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u7528\u4e8e\u5904\u7406\u57fa\u672c\u7684\u8f93\u5165\u548c\u8f93\u51fa\u3002\u6211\u4eec\u4f1a\u4f7f\u7528\u8be5\u5e93\u63d0\u4f9b\u7684\u529f\u80fd\u4ece\u952e\u76d8\u83b7\u53d6\u8f93\u5165\u5e76\u5411\u63a7\u5236\u53f0\u8f93\u51fa\u6570\u636e\u3002 iostream \u4e2d\u7684 io \u6307\u4ee3\u7684\u662f\u8f93\u5165\u8f93\u51fa\uff08 input/output \uff09\u3002 \u4e3a\u4e86\u4f7f\u7528 iostream \u5e93\u63d0\u4f9b\u7684\u529f\u80fd\uff0c\u6211\u4eec\u9700\u8981\u5728\u4efb\u4f55\u9700\u8981\u4f7f\u7528\u8be5\u5e93\u7684\u6e90\u6587\u4ef6\u7684\u9876\u90e8\u5305\u542b iostream \u7684\u5934\u6587\u4ef6\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 #include // rest of code that uses iostream functionality here std:: cout iostream \u5e93\u4e2d\u5305\u542b\u4e86\u4e00\u4e9b\u9884\u5b9a\u4e49\u7684\u53d8\u91cf\u4f9b\u6211\u4eec\u4f7f\u7528\uff0c\u5176\u4e2d\u6700\u6709\u7528\u7684\u5f53\u5c5e std::cout \uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u5411\u63a7\u5236\u53f0\u6253\u5370\u6587\u672c\uff0c cout \u4ee3\u8868\u7684\u542b\u4e49\u5c31\u662f\u5b57\u7b26\u8f93\u51fa\uff08character output\uff09\u3002 \u63d0\u9192\u4e00\u4e0b\uff0c\u4e0b\u9762\u65e2\u662f\u4e4b\u524d\u7684 Hello world \u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 #include // for std::cout int main () { std :: cout << \"Hello world!\" ; // print Hello world! to console return 0 ; } \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c iostream \u5df2\u7ecf\u88ab\u5305\u542b\u4e86\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u8bbf\u95ee std::cout \u3002\u5728\u4e3b\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 std::cout \uff0c\u5e76\u914d\u5408\u63d2\u5165\u8fd0\u7b97\u7b26\uff08 << \uff09\u5c06\u6587\u672c Hello world!\u53d1\u9001\u5230\u63a7\u5236\u53f0\u5e76\u6253\u5370\u51fa\u6765\u3002 std::cout \u4e0d\u4ec5\u53ef\u4ee5\u6253\u5370\u6587\u672c\uff0c\u8fd8\u53ef\u4ee5\u6253\u5370\u6570\u5b57\uff1a 1 2 3 4 5 6 7 8 #include // for std::cout int main () { std :: cout << 4 ; // print 4 to console return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 4 \u5b83\u8fd8\u53ef\u4ee5\u7528\u6765\u6253\u5370\u53d8\u91cf\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 #include // for std::cout int main () { int x { 5 }; // define integer variable x, initialized with value 5 std :: cout << x ; // print value of x (5) to console return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 5 \u4e3a\u4e86\u5728\u4e00\u884c\u4e2d\u6253\u5370\u591a\u4e2a\u5185\u5bb9\uff0c\u53ef\u4ee5\u591a\u6b21\u4f7f\u7528\u63d2\u5165\u8fd0\u7b97\u7b26\u5c06\u591a\u4e2a\u8981\u8f93\u51fa\u7684\u5185\u5bb9\u8fde\u63a5\u8d77\u6765\u5e76\u8f93\u51fa\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 #include // for std::cout int main () { std :: cout << \"Hello\" << \" world!\" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Hello world! \u53e6\u5916\u4e00\u4e2a\u4f8b\u5b50\u5982\u4e0b\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728\u4e00\u6761\u8bed\u53e5\u4e2d\u540c\u65f6\u6253\u5370\u4e86\u6587\u672c\u548c\u53d8\u91cf\u503c\uff1a 1 2 3 4 5 6 7 8 #include // for std::cout int main () { int x { 5 }; std :: cout << \"x is equal to: \" << x ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 x is equal to: 5 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 \u8ba8\u8bba std:: \u524d\u7f00\u7684\u4f5c\u7528 std::endl \u4f60\u89c9\u5f97\u4e0b\u9762\u7684\u4ee3\u7801\u6253\u5370\u7684\u7ed3\u679c\u662f\u4ec0\u4e48\u5462\uff1f 1 2 3 4 5 6 7 8 #include // for std::cout int main () { std :: cout << \"Hi!\" ; std :: cout << \"My name is Alex.\" ; return 0 ; } \u770b\u5230\u4e0b\u9762\u7684\u7ed3\u679c\u4f60\u4f1a\u89c9\u5f97\u5403\u60ca\u5417\uff1a 1 Hi!My name is Alex. \u663e\u7136\uff0c\u5c06\u4e00\u6761\u8bed\u53e5\u62c6\u5206\u4e3a\u4e24\u884c\uff0c\u5e76\u4e0d\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u7ed3\u679c\u3002 \u5982\u679c\u6211\u4eec\u5e0c\u671b\u5206\u884c\u6253\u5370\uff0c\u5219\u5fc5\u987b\u8981\u544a\u77e5\u63a7\u5236\u53f0\u5c06\u5149\u6807\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\u3002 \u4e00\u79cd\u65b9\u5f0f\u662f\u4f7f\u7528 std::endl \u3002\u5f53\u4f7f\u7528 std::cout \u8fdb\u884c\u8f93\u51fa\u65f6\uff0c std::endl \u4f1a\u6253\u5370\u4e00\u4e2a\u6362\u884c\u7b26\uff08\u4fc3\u4f7f\u5149\u6807\u88ab\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\u7684\u5f00\u5934\uff09\u3002\u56e0\u6b64\uff0c endl \u8868\u793a\u7684\u662f\u7ed3\u675f\u8be5\u884c\uff08end line\uff09\u3002 \u4f8b\u7a0b\uff1a 1 2 3 4 5 6 7 8 9 #include // for std::cout and std::endl int main () { std :: cout << \"Hi!\" << std :: endl ; // std::endl will cause the cursor to move to the next line of the console std :: cout << \"My name is Alex.\" << std :: endl ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 Hi! My name is Alex. \u5c0f\u8d34\u58eb \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u7b2c\u4e8c\u4e2a std::endl \u4ece\u6280\u672f\u4e0a\u8bb2\u5e76\u65e0\u5fc5\u8981\uff0c\u56e0\u4e3a\u5728\u6267\u884c\u5b8c\u8fd9\u884c\u4ee3\u7801\u540e\u7a0b\u5e8f\u4f1a\u7acb\u5373\u505c\u6b62\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u5176\u5b9e\u8fd8\u6709\u4e24\u4e2a\u6709\u7528\u7684\u76ee\u7684\uff1a\u9996\u5148\uff0c\u5b83\u53ef\u4ee5\u8868\u660e\u8be5\u884c\u7684\u5185\u5bb9\u5df2\u7ecf\u88ab\u201d\u5b8c\u6574\u8f93\u51fa\u201c \uff08\u4e0e\u4e4b\u76f8\u5bf9\u7684\u662f\u90e8\u5206\u8f93\u51fa\uff0c\u5373\u540e\u7eed\u4ee3\u7801\u4e2d\u8fd8\u6709\u9700\u8981\u8f93\u51fa\u7684\u90e8\u5206\uff09\u3002\u5176\u6b21\uff0c\u5982\u679c\u5c06\u6765\u6211\u4eec\u5728\u540e\u9762\u6dfb\u52a0\u5176\u4ed6\u989d\u5916\u7684\u8f93\u51fa\uff0c\u5c31\u4e0d\u9700\u8981\u4fee\u6539\u5df2\u6709\u7684\u4ee3\u7801\u4e86\u3002\u56e0\u6b64\u4e0d\u59a8\u52a0\u4e0a\u5b83\u3002 std:: endl vs \\n \u4f7f\u7528 std::endl \u6362\u884c\u7684\u6548\u7387\u7a0d\u5fae\u6709\u70b9\u4f4e\uff0c\u56e0\u4e3a\u5b83\u901a\u5e38\u9700\u8981\u5b8c\u6210\u4e24\u4ef6\u4e8b\uff1a\u5c06\u5149\u6807\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\uff0c\u7136\u540e\u786e\u4fdd\u8f93\u51fa\u7ed3\u679c\u9a6c\u4e0a\u663e\u793a\u5728\u5c4f\u5e55\u4e0a\uff08\u79f0\u4e3a\u5237\u65b0\u8f93\u51fa\uff09\u3002\u5f53\u4f7f\u7528 std::cout \u8fdb\u884c\u8f93\u51fa\u65f6\uff0c std::cout \u672c\u6765\u5c31\u4f1a\u5237\u65b0\u8f93\u51fa\uff08\u5373\u4f7f\u6ca1\u6709\u5237\u65b0\uff0c\u901a\u5e38\u4e5f\u4e0d\u4f1a\u4ea7\u751f\u4ec0\u4e48\u95ee\u9898\uff09\u3002\u56e0\u6b64\uff0c\u4f7f\u7528 std::endl \u6765\u5237\u65b0\u8f93\u51fa\u5c31\u6709\u4e9b\u591a\u4f59\u4e86\u3002 \u56e0\u6b64\uff0c\u4f7f\u7528\u6362\u884c\u5b57\u7b26( \\n )\u4e00\u822c\u6765\u8bb2\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u2018\\n\u2019 \u7b26\u53f7\u4f1a\u5c06\u5149\u6807\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\uff0c\u4f46\u662f\u5b83\u5e76\u4e0d\u4f1a\u8bf7\u6c42\u5237\u65b0\uff0c\u56e0\u6b64\u5728\u65e0\u9700\u7279\u522b\u5237\u65b0\u65f6\u53ef\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\u3002\u2018\\n\u2019 \u5b57\u7b26\u8fd8\u66f4\u6613\u8bfb\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u66f4\u7b80\u6d01\uff0c\u800c\u4e14\u8fd8\u53ef\u4ee5\u5d4c\u5165\u5728\u5df2\u6709\u7684\u6587\u672c\u4e2d\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u5c55\u73b0\u4e86\u4f7f\u7528 \u2018\\n\u2019 \u7684\u4e24\u79cd\u4e0d\u540c\u65b9\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 #include // for std::cout int main () { int x { 5 }; std :: cout << \"x is equal to: \" << x << '\\n' ; // Using '\\n' standalone std :: cout << \"And that's all, folks! \\n \" ; // Using '\\n' embedded into a double-quoted piece of text (note: no single quotes when used this way) return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 x is equal to: 5 And that's all, folks! \u6ce8\u610f\uff0c\u5728\u5355\u72ec\u4f7f\u7528\u6362\u884c\u7b26 \u2018\\n\u2019 \u7684\u65f6\u5019\uff0c\u5355\u5f15\u53f7\u662f\u5fc5\u987b\u7684\u3002\u5f53\u5d4c\u5165\u5728\u5176\u4ed6\u5df2\u7ecf\u88ab\u53cc\u5f15\u53f7\u5305\u88f9\u7684\u6587\u672c\u4e2d\u65f6\uff0c\u5219\u65e0\u9700\u518d\u4f7f\u7528\u5355\u5f15\u53f7\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u66f4\u8be6\u7ec6\u7684\u4ecb\u7ecd\u6362\u884c\u7b26 \u2018\\n\u2019 ( 4.11 - \u5b57\u7b26 )\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u8f93\u51fa\u65f6\u4f18\u5148\u4f7f\u7528 \u2018\\n\u2019 \u800c\u4e0d\u662f std::endl \u6ce8\u610f \u2018\\n\u2019\u4f7f\u7528\u7684\u662f\u53cd\u659c\u6760\uff08\u548c\u5176\u4ed6\u7279\u6b8a\u5b57\u7b26\u4e00\u6837\uff09\uff0c\u800c\u4e0d\u662f\u659c\u6760\u3002\u4f7f\u7528\u659c\u6760\uff08\u4f8b\u5982 '/n'\uff09\u53ef\u80fd\u4f1a\u5e26\u6765\u65e0\u6cd5\u9884\u6599\u7684\u7ed3\u679c\u3002 std:: cin std::cin \u662f isstream \u4e2d\u9884\u5b9a\u4e49\u7684\u53e6\u5916\u4e00\u4e2a\u53d8\u91cf\u3002\u4e0e\u7528\u4e8e\u8f93\u51fa\u7684 std::cout \u4e0d\u540c\uff0c std::cin (\u8868\u793a\u5b57\u7b26\u8f93\u5165\uff0c\u201ccharacter input\u201d) \u914d\u5408\u63d0\u53d6\u8fd0\u7b97\u7b26( >> )\uff0c\u53ef\u4ee5\u4ece\u952e\u76d8\u8bfb\u53d6\u8f93\u5165\u3002\u5f53\u7136\uff0c\u8f93\u5165\u7684\u7ed3\u679c\u5fc5\u987b\u5b58\u653e\u5728\u53d8\u91cf\u4e2d\u624d\u53ef\u4ee5\u88ab\u4f7f\u7528\u3002 1 2 3 4 5 6 7 8 9 10 11 12 #include // for std::cout and std::cin int main () { std :: cout << \"Enter a number: \" ; // ask user for a number int x { }; // define variable x to hold user input (and zero-initialize it) std :: cin >> x ; // get number from keyboard and store it in variable x std :: cout << \"You entered \" << x << '\\n' ; return 0 ; } \u8bf7\u5c1d\u8bd5\u81ea\u884c\u7f16\u8bd1\u548c\u8fd0\u884c\u4e0a\u8ff0\u7a0b\u5e8f\u3002\u5728\u6267\u884c\u7a0b\u5e8f\u65f6\uff0c\u7b2c\u4e94\u884c\u4ee3\u7801\u4f1a\u6253\u5370\u201cEnter a number: \u201c\uff0c\u5f53\u6267\u884c\u5230\u7b2c\u516b\u884c\u662f\uff0c\u7a0b\u5e8f\u4f1a\u7b49\u5f85\u7528\u6237\u8f93\u5165\u3002\u5f53\u4f60\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff08\u5e76\u6309\u4e0b\u56de\u8f66\uff09\u540e\uff0c\u5b83\u4f1a\u88ab\u8d4b\u503c\u7ed9\u53d8\u91cf x\u3002\u6700\u540e\uff0c\u7b2c\u5341\u884c\u4ee3\u7801\u4f1a\u6253\u5370 \u201cYou entered \u201d \u4ee5\u53ca\u4f60\u521a\u624d\u8f93\u5165\u7684\u6570\u5b57\u3002 \u4f8b\u5982\uff1a 1 2 Enter a number: 4 You entered 4 \u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u6bd4\u8f83\u7b80\u5355\u5730\u4ece\u7528\u6237\u83b7\u53d6\u8f93\u5165\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u5185\u5bb9\u4e2d\u5927\u91cf\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u3002\u6ce8\u610f\uff0c\u5728\u83b7\u53d6\u8f93\u5165\u65f6\u5e76\u4e0d\u9700\u8981\u6362\u884c\u7b26\uff0c\u56e0\u4e3a\u7528\u6237\u4f1a\u81ea\u884c\u6309\u4e0b\u56de\u8f66\u952e\uff0c\u6b64\u65f6\u5149\u6807\u4fbf\u4f1a\u88ab\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\u3002 \u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u754c\u9762\u5728\u8f93\u5165\u6570\u5b57\u540e\u9a6c\u4e0a\u5c31\u5173\u95ed\u4e86\uff0c\u8bf7\u53c2\u8003 0.8 -- A few common C++ problems \u6765\u83b7\u53d6\u5e2e\u52a9\u3002 \u4e0e\u8f93\u51fa\u7c7b\u4f3c\uff0c\u4e00\u884c\u4ee3\u7801\u4e5f\u53ef\u4ee5\u5b8c\u6210\u591a\u4e2a\u503c\u7684\u8f93\u5165\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // for std::cout and std::cin int main () { std :: cout << \"Enter two numbers separated by a space: \" ; int x { }; // define variable x to hold user input (and zero-initialize it) int y { }; // define variable y to hold user input (and zero-initialize it) std :: cin >> x >> y ; // get two numbers and store in variable x and y respectively std :: cout << \"You entered \" << x << \" and \" << y << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Enter two numbers separated by a space: 5 6 You entered 5 and 6 \u6700\u4f73\u5b9e\u8df5 \u53d8\u91cf\u5728\u7528\u4e8e\u63a5\u6536\u7528\u6237\u8f93\u5165\uff08\u4f8b\u5982\u901a\u8fc7 std::cin \uff09\u7684\u6570\u636e\u4e4b\u524d\uff0c\u662f\u5426\u5e94\u8be5\u8fdb\u884c\u521d\u59cb\u5316\u8fd9\u4e2a\u95ee\u9898\u6709\u5f88\u591a\u4e89\u8bba\uff0c\u56e0\u4e3a\u7528\u6237\u7684\u8f93\u5165\u4f1a\u8986\u76d6\u521d\u59cb\u5316\u7684\u503c\u3002\u4e0e\u6211\u4eec\u4e4b\u524d\u7684\u5efa\u8bae\u7c7b\u4f3c\uff0c\u53d8\u91cf\u5728\u4f7f\u7528\u524d\u90fd\u5fc5\u987b\u8981\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u56e0\u6b64\u8fd9\u91cc\u7684\u6700\u4f73\u5b9e\u8df5\u4ecd\u7136\u662f\uff0c\u53d8\u91cf\u8981\u5148\u8fdb\u884c\u521d\u59cb\u5316\u518d\u7528\u4e8e\u63a5\u6536\u7528\u6237\u8f93\u5165\u3002 \u6211\u4eec\u4f1a\u5728 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406 \u4e2d\u8ba8\u8bba std::cin \u5982\u4f55\u5e94\u5bf9\u975e\u6cd5\u8f93\u5165\u3002 \u6269\u5c55\u9605\u8bfb C++ \u7684\u8f93\u5165\u8f93\u51fa\u5e93\u5e76\u6ca1\u6709\u63d0\u4f9b\u4e00\u79cd\u65e0\u9700\u6309\u4e0b\u56de\u8f66\u5c31\u53ef\u4ee5\u83b7\u53d6\u8f93\u5165\u7ed3\u679c\u7684\u65b9\u6cd5\u3002\u5982\u679c\u4f60\u5e0c\u671b\u5b9e\u73b0\u8be5\u529f\u80fd\uff0c\u5c06\u4e0d\u5f97\u4e0d\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u3002\u5982\u679c\u662f\u5f00\u53d1\u63a7\u5236\u53f0\u5e94\u7528\u7a0b\u5e8f\uff0c\u6211\u4eec\u63a8\u8350\u5c1d\u8bd5 pdcurses \u3001 FXTUI \u6216 cpp-terminal \u3002\u53e6\u5916\uff0c\u5f88\u591a\u56fe\u5f62\u7528\u6237\u5e93\u754c\u9762\u5e93\u4e5f\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u7684\u529f\u80fd\u3002 \u5c0f\u7ed3 \u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u5e38\u4f1a\u6df7\u6dc6 std::cin \u3001 std::cout \u3001\u63d2\u5165\u8fd0\u7b97\u7b26 ( << ) \u548c\u63d0\u53d6\u8fd0\u7b97\u7b26 ( >> )\u3002\u4e0b\u9762\u8fd9\u4e9b\u6280\u5de7\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8bb0\u5fc6\uff1a std::cin \u548c std::cout \u603b\u662f\u4f4d\u4e8e\u8bed\u53e5\u7684\u5de6\u4fa7\u3002 std::cout \u7528\u4e8e\u8f93\u51fa (cout = character output) std::cin \u7528\u4e8e\u8f93\u5165 (cin = character input) << \u914d\u5408 std::cout \u4f7f\u7528\uff0c\u540c\u65f6\u4e5f\u8868\u660e\u4e86\u6570\u636e\u7684\u6d41\u52a8\u65b9\u5411 (\u5047\u8bbe std::cout \u8868\u793a\u7684\u662f\u63a7\u5236\u53f0\uff0c\u5219\u8f93\u51fa\u7684\u6570\u636e\u4ece\u53d8\u91cf\u6d41\u5411\u63a7\u5236\u53f0\uff09\u3002 std::cout << 4 \u5c31\u8868\u793a\u5c06 4 \u53d1\u9001\u5230\u63a7\u5236\u53f0\u3002 >> \u914d\u5408 std::cin \u4f7f\u7528\uff0c\u540c\u65f6\u4e5f\u8868\u660e\u4e86\u6570\u636e\u7684\u6d41\u52a8\u65b9\u5411 (\u5047\u8bbe std::cin \u8868\u793a\u7684\u662f\u952e\u76d8\uff0c\u5219\u8f93\u5165\u7684\u6570\u636e\u662f\u4ece\u952e\u76d8\u6d41\u5411\u53d8\u91cf\uff09\u3002 std::cin >> x \u5c31\u8868\u793a\u4ece\u952e\u76d8\u83b7\u53d6\u7684\u7528\u6237\u8f93\u5165\u88ab\u5b58\u653e\u5230\u53d8\u91cf x \u4e2d \u66f4\u591a\u5173\u4e8e\u8fd0\u7b97\u7b26\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u5c06\u5728 1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"1.5 - iostream \u7b80\u4ecb\uff1acout\uff0ccin \u548c endl"},{"location":"1-5-Introduction-to-iostream-cout-cin-and-endl/#_1","text":"\u8f93\u5165\u8f93\u51fa\u5e93 ( IO \u5e93) \u662f C++ \u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u7528\u4e8e\u5904\u7406\u57fa\u672c\u7684\u8f93\u5165\u548c\u8f93\u51fa\u3002\u6211\u4eec\u4f1a\u4f7f\u7528\u8be5\u5e93\u63d0\u4f9b\u7684\u529f\u80fd\u4ece\u952e\u76d8\u83b7\u53d6\u8f93\u5165\u5e76\u5411\u63a7\u5236\u53f0\u8f93\u51fa\u6570\u636e\u3002 iostream \u4e2d\u7684 io \u6307\u4ee3\u7684\u662f\u8f93\u5165\u8f93\u51fa\uff08 input/output \uff09\u3002 \u4e3a\u4e86\u4f7f\u7528 iostream \u5e93\u63d0\u4f9b\u7684\u529f\u80fd\uff0c\u6211\u4eec\u9700\u8981\u5728\u4efb\u4f55\u9700\u8981\u4f7f\u7528\u8be5\u5e93\u7684\u6e90\u6587\u4ef6\u7684\u9876\u90e8\u5305\u542b iostream \u7684\u5934\u6587\u4ef6\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 #include // rest of code that uses iostream functionality here","title":"\u8f93\u5165\u8f93\u51fa\u5e93"},{"location":"1-5-Introduction-to-iostream-cout-cin-and-endl/#std-cout","text":"iostream \u5e93\u4e2d\u5305\u542b\u4e86\u4e00\u4e9b\u9884\u5b9a\u4e49\u7684\u53d8\u91cf\u4f9b\u6211\u4eec\u4f7f\u7528\uff0c\u5176\u4e2d\u6700\u6709\u7528\u7684\u5f53\u5c5e std::cout \uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u5411\u63a7\u5236\u53f0\u6253\u5370\u6587\u672c\uff0c cout \u4ee3\u8868\u7684\u542b\u4e49\u5c31\u662f\u5b57\u7b26\u8f93\u51fa\uff08character output\uff09\u3002 \u63d0\u9192\u4e00\u4e0b\uff0c\u4e0b\u9762\u65e2\u662f\u4e4b\u524d\u7684 Hello world \u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 #include // for std::cout int main () { std :: cout << \"Hello world!\" ; // print Hello world! to console return 0 ; } \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c iostream \u5df2\u7ecf\u88ab\u5305\u542b\u4e86\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u8bbf\u95ee std::cout \u3002\u5728\u4e3b\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 std::cout \uff0c\u5e76\u914d\u5408\u63d2\u5165\u8fd0\u7b97\u7b26\uff08 << \uff09\u5c06\u6587\u672c Hello world!\u53d1\u9001\u5230\u63a7\u5236\u53f0\u5e76\u6253\u5370\u51fa\u6765\u3002 std::cout \u4e0d\u4ec5\u53ef\u4ee5\u6253\u5370\u6587\u672c\uff0c\u8fd8\u53ef\u4ee5\u6253\u5370\u6570\u5b57\uff1a 1 2 3 4 5 6 7 8 #include // for std::cout int main () { std :: cout << 4 ; // print 4 to console return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 4 \u5b83\u8fd8\u53ef\u4ee5\u7528\u6765\u6253\u5370\u53d8\u91cf\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 #include // for std::cout int main () { int x { 5 }; // define integer variable x, initialized with value 5 std :: cout << x ; // print value of x (5) to console return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 5 \u4e3a\u4e86\u5728\u4e00\u884c\u4e2d\u6253\u5370\u591a\u4e2a\u5185\u5bb9\uff0c\u53ef\u4ee5\u591a\u6b21\u4f7f\u7528\u63d2\u5165\u8fd0\u7b97\u7b26\u5c06\u591a\u4e2a\u8981\u8f93\u51fa\u7684\u5185\u5bb9\u8fde\u63a5\u8d77\u6765\u5e76\u8f93\u51fa\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 #include // for std::cout int main () { std :: cout << \"Hello\" << \" world!\" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Hello world! \u53e6\u5916\u4e00\u4e2a\u4f8b\u5b50\u5982\u4e0b\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728\u4e00\u6761\u8bed\u53e5\u4e2d\u540c\u65f6\u6253\u5370\u4e86\u6587\u672c\u548c\u53d8\u91cf\u503c\uff1a 1 2 3 4 5 6 7 8 #include // for std::cout int main () { int x { 5 }; std :: cout << \"x is equal to: \" << x ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 x is equal to: 5 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 \u8ba8\u8bba std:: \u524d\u7f00\u7684\u4f5c\u7528","title":"std:: cout"},{"location":"1-5-Introduction-to-iostream-cout-cin-and-endl/#stdendl","text":"\u4f60\u89c9\u5f97\u4e0b\u9762\u7684\u4ee3\u7801\u6253\u5370\u7684\u7ed3\u679c\u662f\u4ec0\u4e48\u5462\uff1f 1 2 3 4 5 6 7 8 #include // for std::cout int main () { std :: cout << \"Hi!\" ; std :: cout << \"My name is Alex.\" ; return 0 ; } \u770b\u5230\u4e0b\u9762\u7684\u7ed3\u679c\u4f60\u4f1a\u89c9\u5f97\u5403\u60ca\u5417\uff1a 1 Hi!My name is Alex. \u663e\u7136\uff0c\u5c06\u4e00\u6761\u8bed\u53e5\u62c6\u5206\u4e3a\u4e24\u884c\uff0c\u5e76\u4e0d\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u7ed3\u679c\u3002 \u5982\u679c\u6211\u4eec\u5e0c\u671b\u5206\u884c\u6253\u5370\uff0c\u5219\u5fc5\u987b\u8981\u544a\u77e5\u63a7\u5236\u53f0\u5c06\u5149\u6807\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\u3002 \u4e00\u79cd\u65b9\u5f0f\u662f\u4f7f\u7528 std::endl \u3002\u5f53\u4f7f\u7528 std::cout \u8fdb\u884c\u8f93\u51fa\u65f6\uff0c std::endl \u4f1a\u6253\u5370\u4e00\u4e2a\u6362\u884c\u7b26\uff08\u4fc3\u4f7f\u5149\u6807\u88ab\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\u7684\u5f00\u5934\uff09\u3002\u56e0\u6b64\uff0c endl \u8868\u793a\u7684\u662f\u7ed3\u675f\u8be5\u884c\uff08end line\uff09\u3002 \u4f8b\u7a0b\uff1a 1 2 3 4 5 6 7 8 9 #include // for std::cout and std::endl int main () { std :: cout << \"Hi!\" << std :: endl ; // std::endl will cause the cursor to move to the next line of the console std :: cout << \"My name is Alex.\" << std :: endl ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 Hi! My name is Alex. \u5c0f\u8d34\u58eb \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u7b2c\u4e8c\u4e2a std::endl \u4ece\u6280\u672f\u4e0a\u8bb2\u5e76\u65e0\u5fc5\u8981\uff0c\u56e0\u4e3a\u5728\u6267\u884c\u5b8c\u8fd9\u884c\u4ee3\u7801\u540e\u7a0b\u5e8f\u4f1a\u7acb\u5373\u505c\u6b62\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u5176\u5b9e\u8fd8\u6709\u4e24\u4e2a\u6709\u7528\u7684\u76ee\u7684\uff1a\u9996\u5148\uff0c\u5b83\u53ef\u4ee5\u8868\u660e\u8be5\u884c\u7684\u5185\u5bb9\u5df2\u7ecf\u88ab\u201d\u5b8c\u6574\u8f93\u51fa\u201c \uff08\u4e0e\u4e4b\u76f8\u5bf9\u7684\u662f\u90e8\u5206\u8f93\u51fa\uff0c\u5373\u540e\u7eed\u4ee3\u7801\u4e2d\u8fd8\u6709\u9700\u8981\u8f93\u51fa\u7684\u90e8\u5206\uff09\u3002\u5176\u6b21\uff0c\u5982\u679c\u5c06\u6765\u6211\u4eec\u5728\u540e\u9762\u6dfb\u52a0\u5176\u4ed6\u989d\u5916\u7684\u8f93\u51fa\uff0c\u5c31\u4e0d\u9700\u8981\u4fee\u6539\u5df2\u6709\u7684\u4ee3\u7801\u4e86\u3002\u56e0\u6b64\u4e0d\u59a8\u52a0\u4e0a\u5b83\u3002","title":"std::endl"},{"location":"1-5-Introduction-to-iostream-cout-cin-and-endl/#std-endl-vs-n","text":"\u4f7f\u7528 std::endl \u6362\u884c\u7684\u6548\u7387\u7a0d\u5fae\u6709\u70b9\u4f4e\uff0c\u56e0\u4e3a\u5b83\u901a\u5e38\u9700\u8981\u5b8c\u6210\u4e24\u4ef6\u4e8b\uff1a\u5c06\u5149\u6807\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\uff0c\u7136\u540e\u786e\u4fdd\u8f93\u51fa\u7ed3\u679c\u9a6c\u4e0a\u663e\u793a\u5728\u5c4f\u5e55\u4e0a\uff08\u79f0\u4e3a\u5237\u65b0\u8f93\u51fa\uff09\u3002\u5f53\u4f7f\u7528 std::cout \u8fdb\u884c\u8f93\u51fa\u65f6\uff0c std::cout \u672c\u6765\u5c31\u4f1a\u5237\u65b0\u8f93\u51fa\uff08\u5373\u4f7f\u6ca1\u6709\u5237\u65b0\uff0c\u901a\u5e38\u4e5f\u4e0d\u4f1a\u4ea7\u751f\u4ec0\u4e48\u95ee\u9898\uff09\u3002\u56e0\u6b64\uff0c\u4f7f\u7528 std::endl \u6765\u5237\u65b0\u8f93\u51fa\u5c31\u6709\u4e9b\u591a\u4f59\u4e86\u3002 \u56e0\u6b64\uff0c\u4f7f\u7528\u6362\u884c\u5b57\u7b26( \\n )\u4e00\u822c\u6765\u8bb2\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u2018\\n\u2019 \u7b26\u53f7\u4f1a\u5c06\u5149\u6807\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\uff0c\u4f46\u662f\u5b83\u5e76\u4e0d\u4f1a\u8bf7\u6c42\u5237\u65b0\uff0c\u56e0\u6b64\u5728\u65e0\u9700\u7279\u522b\u5237\u65b0\u65f6\u53ef\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\u3002\u2018\\n\u2019 \u5b57\u7b26\u8fd8\u66f4\u6613\u8bfb\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u66f4\u7b80\u6d01\uff0c\u800c\u4e14\u8fd8\u53ef\u4ee5\u5d4c\u5165\u5728\u5df2\u6709\u7684\u6587\u672c\u4e2d\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u5c55\u73b0\u4e86\u4f7f\u7528 \u2018\\n\u2019 \u7684\u4e24\u79cd\u4e0d\u540c\u65b9\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 #include // for std::cout int main () { int x { 5 }; std :: cout << \"x is equal to: \" << x << '\\n' ; // Using '\\n' standalone std :: cout << \"And that's all, folks! \\n \" ; // Using '\\n' embedded into a double-quoted piece of text (note: no single quotes when used this way) return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 x is equal to: 5 And that's all, folks! \u6ce8\u610f\uff0c\u5728\u5355\u72ec\u4f7f\u7528\u6362\u884c\u7b26 \u2018\\n\u2019 \u7684\u65f6\u5019\uff0c\u5355\u5f15\u53f7\u662f\u5fc5\u987b\u7684\u3002\u5f53\u5d4c\u5165\u5728\u5176\u4ed6\u5df2\u7ecf\u88ab\u53cc\u5f15\u53f7\u5305\u88f9\u7684\u6587\u672c\u4e2d\u65f6\uff0c\u5219\u65e0\u9700\u518d\u4f7f\u7528\u5355\u5f15\u53f7\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u66f4\u8be6\u7ec6\u7684\u4ecb\u7ecd\u6362\u884c\u7b26 \u2018\\n\u2019 ( 4.11 - \u5b57\u7b26 )\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u8f93\u51fa\u65f6\u4f18\u5148\u4f7f\u7528 \u2018\\n\u2019 \u800c\u4e0d\u662f std::endl \u6ce8\u610f \u2018\\n\u2019\u4f7f\u7528\u7684\u662f\u53cd\u659c\u6760\uff08\u548c\u5176\u4ed6\u7279\u6b8a\u5b57\u7b26\u4e00\u6837\uff09\uff0c\u800c\u4e0d\u662f\u659c\u6760\u3002\u4f7f\u7528\u659c\u6760\uff08\u4f8b\u5982 '/n'\uff09\u53ef\u80fd\u4f1a\u5e26\u6765\u65e0\u6cd5\u9884\u6599\u7684\u7ed3\u679c\u3002","title":"std:: endl vs \\n"},{"location":"1-5-Introduction-to-iostream-cout-cin-and-endl/#std-cin","text":"std::cin \u662f isstream \u4e2d\u9884\u5b9a\u4e49\u7684\u53e6\u5916\u4e00\u4e2a\u53d8\u91cf\u3002\u4e0e\u7528\u4e8e\u8f93\u51fa\u7684 std::cout \u4e0d\u540c\uff0c std::cin (\u8868\u793a\u5b57\u7b26\u8f93\u5165\uff0c\u201ccharacter input\u201d) \u914d\u5408\u63d0\u53d6\u8fd0\u7b97\u7b26( >> )\uff0c\u53ef\u4ee5\u4ece\u952e\u76d8\u8bfb\u53d6\u8f93\u5165\u3002\u5f53\u7136\uff0c\u8f93\u5165\u7684\u7ed3\u679c\u5fc5\u987b\u5b58\u653e\u5728\u53d8\u91cf\u4e2d\u624d\u53ef\u4ee5\u88ab\u4f7f\u7528\u3002 1 2 3 4 5 6 7 8 9 10 11 12 #include // for std::cout and std::cin int main () { std :: cout << \"Enter a number: \" ; // ask user for a number int x { }; // define variable x to hold user input (and zero-initialize it) std :: cin >> x ; // get number from keyboard and store it in variable x std :: cout << \"You entered \" << x << '\\n' ; return 0 ; } \u8bf7\u5c1d\u8bd5\u81ea\u884c\u7f16\u8bd1\u548c\u8fd0\u884c\u4e0a\u8ff0\u7a0b\u5e8f\u3002\u5728\u6267\u884c\u7a0b\u5e8f\u65f6\uff0c\u7b2c\u4e94\u884c\u4ee3\u7801\u4f1a\u6253\u5370\u201cEnter a number: \u201c\uff0c\u5f53\u6267\u884c\u5230\u7b2c\u516b\u884c\u662f\uff0c\u7a0b\u5e8f\u4f1a\u7b49\u5f85\u7528\u6237\u8f93\u5165\u3002\u5f53\u4f60\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff08\u5e76\u6309\u4e0b\u56de\u8f66\uff09\u540e\uff0c\u5b83\u4f1a\u88ab\u8d4b\u503c\u7ed9\u53d8\u91cf x\u3002\u6700\u540e\uff0c\u7b2c\u5341\u884c\u4ee3\u7801\u4f1a\u6253\u5370 \u201cYou entered \u201d \u4ee5\u53ca\u4f60\u521a\u624d\u8f93\u5165\u7684\u6570\u5b57\u3002 \u4f8b\u5982\uff1a 1 2 Enter a number: 4 You entered 4 \u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u6bd4\u8f83\u7b80\u5355\u5730\u4ece\u7528\u6237\u83b7\u53d6\u8f93\u5165\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u5185\u5bb9\u4e2d\u5927\u91cf\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u3002\u6ce8\u610f\uff0c\u5728\u83b7\u53d6\u8f93\u5165\u65f6\u5e76\u4e0d\u9700\u8981\u6362\u884c\u7b26\uff0c\u56e0\u4e3a\u7528\u6237\u4f1a\u81ea\u884c\u6309\u4e0b\u56de\u8f66\u952e\uff0c\u6b64\u65f6\u5149\u6807\u4fbf\u4f1a\u88ab\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\u3002 \u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u754c\u9762\u5728\u8f93\u5165\u6570\u5b57\u540e\u9a6c\u4e0a\u5c31\u5173\u95ed\u4e86\uff0c\u8bf7\u53c2\u8003 0.8 -- A few common C++ problems \u6765\u83b7\u53d6\u5e2e\u52a9\u3002 \u4e0e\u8f93\u51fa\u7c7b\u4f3c\uff0c\u4e00\u884c\u4ee3\u7801\u4e5f\u53ef\u4ee5\u5b8c\u6210\u591a\u4e2a\u503c\u7684\u8f93\u5165\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // for std::cout and std::cin int main () { std :: cout << \"Enter two numbers separated by a space: \" ; int x { }; // define variable x to hold user input (and zero-initialize it) int y { }; // define variable y to hold user input (and zero-initialize it) std :: cin >> x >> y ; // get two numbers and store in variable x and y respectively std :: cout << \"You entered \" << x << \" and \" << y << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Enter two numbers separated by a space: 5 6 You entered 5 and 6 \u6700\u4f73\u5b9e\u8df5 \u53d8\u91cf\u5728\u7528\u4e8e\u63a5\u6536\u7528\u6237\u8f93\u5165\uff08\u4f8b\u5982\u901a\u8fc7 std::cin \uff09\u7684\u6570\u636e\u4e4b\u524d\uff0c\u662f\u5426\u5e94\u8be5\u8fdb\u884c\u521d\u59cb\u5316\u8fd9\u4e2a\u95ee\u9898\u6709\u5f88\u591a\u4e89\u8bba\uff0c\u56e0\u4e3a\u7528\u6237\u7684\u8f93\u5165\u4f1a\u8986\u76d6\u521d\u59cb\u5316\u7684\u503c\u3002\u4e0e\u6211\u4eec\u4e4b\u524d\u7684\u5efa\u8bae\u7c7b\u4f3c\uff0c\u53d8\u91cf\u5728\u4f7f\u7528\u524d\u90fd\u5fc5\u987b\u8981\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u56e0\u6b64\u8fd9\u91cc\u7684\u6700\u4f73\u5b9e\u8df5\u4ecd\u7136\u662f\uff0c\u53d8\u91cf\u8981\u5148\u8fdb\u884c\u521d\u59cb\u5316\u518d\u7528\u4e8e\u63a5\u6536\u7528\u6237\u8f93\u5165\u3002 \u6211\u4eec\u4f1a\u5728 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406 \u4e2d\u8ba8\u8bba std::cin \u5982\u4f55\u5e94\u5bf9\u975e\u6cd5\u8f93\u5165\u3002 \u6269\u5c55\u9605\u8bfb C++ \u7684\u8f93\u5165\u8f93\u51fa\u5e93\u5e76\u6ca1\u6709\u63d0\u4f9b\u4e00\u79cd\u65e0\u9700\u6309\u4e0b\u56de\u8f66\u5c31\u53ef\u4ee5\u83b7\u53d6\u8f93\u5165\u7ed3\u679c\u7684\u65b9\u6cd5\u3002\u5982\u679c\u4f60\u5e0c\u671b\u5b9e\u73b0\u8be5\u529f\u80fd\uff0c\u5c06\u4e0d\u5f97\u4e0d\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u3002\u5982\u679c\u662f\u5f00\u53d1\u63a7\u5236\u53f0\u5e94\u7528\u7a0b\u5e8f\uff0c\u6211\u4eec\u63a8\u8350\u5c1d\u8bd5 pdcurses \u3001 FXTUI \u6216 cpp-terminal \u3002\u53e6\u5916\uff0c\u5f88\u591a\u56fe\u5f62\u7528\u6237\u5e93\u754c\u9762\u5e93\u4e5f\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u7684\u529f\u80fd\u3002","title":"std:: cin"},{"location":"1-5-Introduction-to-iostream-cout-cin-and-endl/#_2","text":"\u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u5e38\u4f1a\u6df7\u6dc6 std::cin \u3001 std::cout \u3001\u63d2\u5165\u8fd0\u7b97\u7b26 ( << ) \u548c\u63d0\u53d6\u8fd0\u7b97\u7b26 ( >> )\u3002\u4e0b\u9762\u8fd9\u4e9b\u6280\u5de7\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8bb0\u5fc6\uff1a std::cin \u548c std::cout \u603b\u662f\u4f4d\u4e8e\u8bed\u53e5\u7684\u5de6\u4fa7\u3002 std::cout \u7528\u4e8e\u8f93\u51fa (cout = character output) std::cin \u7528\u4e8e\u8f93\u5165 (cin = character input) << \u914d\u5408 std::cout \u4f7f\u7528\uff0c\u540c\u65f6\u4e5f\u8868\u660e\u4e86\u6570\u636e\u7684\u6d41\u52a8\u65b9\u5411 (\u5047\u8bbe std::cout \u8868\u793a\u7684\u662f\u63a7\u5236\u53f0\uff0c\u5219\u8f93\u51fa\u7684\u6570\u636e\u4ece\u53d8\u91cf\u6d41\u5411\u63a7\u5236\u53f0\uff09\u3002 std::cout << 4 \u5c31\u8868\u793a\u5c06 4 \u53d1\u9001\u5230\u63a7\u5236\u53f0\u3002 >> \u914d\u5408 std::cin \u4f7f\u7528\uff0c\u540c\u65f6\u4e5f\u8868\u660e\u4e86\u6570\u636e\u7684\u6d41\u52a8\u65b9\u5411 (\u5047\u8bbe std::cin \u8868\u793a\u7684\u662f\u952e\u76d8\uff0c\u5219\u8f93\u5165\u7684\u6570\u636e\u662f\u4ece\u952e\u76d8\u6d41\u5411\u53d8\u91cf\uff09\u3002 std::cin >> x \u5c31\u8868\u793a\u4ece\u952e\u76d8\u83b7\u53d6\u7684\u7528\u6237\u8f93\u5165\u88ab\u5b58\u653e\u5230\u53d8\u91cf x \u4e2d \u66f4\u591a\u5173\u4e8e\u8fd0\u7b97\u7b26\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u5c06\u5728 1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"\u5c0f\u7ed3"},{"location":"1-6-Uninitialized-variables-and-undefined-behavior/","tags":["undefined behavior","initialization"],"text":"\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf \u4e0d\u540c\u4e8e\u6709\u4e9b\u7f16\u7a0b\u8bed\u8a00\uff0cC/C++ \u5e76\u4e0d\u4f1a\u81ea\u52a8\u5730\u5c06\u5927\u90e8\u5206\u53d8\u91cf\u521d\u59cb\u5316\u4e3a\u67d0\u4e2a\u7ed9\u5b9a\u503c\uff08\u4f8b\u59820\uff09\u3002\u56e0\u6b64\uff0c\u5f53\u7f16\u8bd1\u5668\u4e3a\u53d8\u91cf\u5206\u914d\u5185\u5b58\u5730\u5740\u65f6\uff0c\u9ed8\u8ba4\u7684\u521d\u503c\u5c06\u4f1a\u662f\u4efb\u4f55\u6070\u597d\u4f4d\u4e8e\u8be5\u5730\u5740\u7684\u503c\uff08\u5783\u573e\u503c\uff09\u3002\u4e00\u4e2a\u6ca1\u6709\u88ab\u660e\u786e\u8d4b\u4e88\u6307\u5b9a\u521d\u503c\uff08\u901a\u8fc7\u521d\u59cb\u5316\u6216\u8d4b\u503c\uff09\u7684\u53d8\u91cf\u79f0\u4e3a\u672a\u521d\u59cb\u5316\u53d8\u91cf\u3002 \u4f5c\u8005\u6ce8 \u5f88\u591a\u8bfb\u8005\u60f3\u5f53\u7136\u5730\u8ba4\u4e3a\uff0c\u201d\u521d\u59cb\u5316\u7684\u201c\u548c\"\u672a\u521d\u59cb\u5316\u7684\"\u662f\u4e00\u5bf9\u4e25\u683c\u7684\u53cd\u4e49\u8bcd\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u3002\u521d\u59cb\u5316\u610f\u5473\u7740\u5bf9\u8c61\u5728\u88ab\u5b9a\u4e49\u7684\u65f6\u5019\u5373\u83b7\u5f97\u4e86\u4e00\u4e2a\u7ed9\u5b9a\u7684\u521d\u59cb\u503c\u3002\u800c\u672a\u521d\u59cb\u5316\u5219\u8868\u793a\uff0c\u5bf9\u8c61\u6ca1\u6709\u88ab\u660e\u786e\u5730\u6307\u5b9a\u4efb\u4f55\u503c\uff08\u901a\u8fc7\u5404\u79cd\u9014\u5f84\uff0c\u5305\u62ec\u8d4b\u503c\uff09\u3002\u56e0\u6b64\uff0c\u4e00\u4e2a\u5bf9\u8c61\u5982\u679c\u6ca1\u6709\u88ab\u201d\u521d\u59cb\u5316\u201c\uff0c\u4f46\u901a\u8fc7\u8d4b\u503c\u5f97\u5230\u4e86\u4e00\u4e2a\u521d\u503c\uff0c\u5b83\u4e5f\u4e0d\u518d\u662f\u201d\u672a\u521d\u59cb\u5316\u201c\u72b6\u6001\uff08\u56e0\u4e3a\u5b83\u5df2\u7ecf\u5f97\u5230\u4e86\u4e00\u4e2a\u503c\uff09 \u7b80\u5355\u603b\u7ed3\u4e00\u4e0b\uff1a - \u521d\u59cb\u5316\uff1a\u5bf9\u8c61\u5728\u88ab\u5b9a\u4e49\u7684\u540c\u65f6\u4e5f\u88ab\u8d4b\u4e88\u4e86\u521d\u503c - \u8d4b\u503c\uff1a\u5bf9\u8c61\u5728\u5b9a\u4e49\u540e\uff0c\u88ab\u8d4b\u4e88\u4e86\u4e00\u4e2a\u503c - \u672a\u521d\u59cb\u5316\uff1a\u5bf9\u8c61\u59cb\u7ec8\u6ca1\u6709\u80fd\u591f\u83b7\u5f97\u4e00\u4e2a\u660e\u786e\u6307\u5b9a\u7684\u521d\u503c\u3002 \u9898\u5916\u8bdd \u4e0d\u8fdb\u884c\u9ed8\u8ba4\u521d\u59cb\u5316\u8fd9\u4e00\u7b56\u7565\u662f\u4eceC\u8bed\u8a00\u7ee7\u627f\u800c\u6765\uff0c\u4e3b\u8981\u51fa\u4e8e\u6027\u80fd\u4f18\u5316\u8003\u8651\uff0c\u56e0\u4e3a\u5f53\u65f6\u7684\u8ba1\u7b97\u673a\u8fd0\u884c\u901f\u5ea6\u8fd8\u5f88\u6162\u3002\u4f60\u53ef\u4ee5\u60f3\u8c61\u4e00\u4e0b\uff0c\u5047\u8bbe\u6211\u4eec\u9700\u8981\u4ece\u6587\u4ef6\u8bfb\u53d6100000\u6761\u6570\u636e\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u4f60\u9700\u8981\u521b\u5efa100000\u4e2a\u53d8\u91cf\u5e76\u4f7f\u7528\u8bfb\u53d6\u7684\u6570\u636e\u6765\u586b\u5145\u5b83\u4eec\u3002 \u5982\u679c C++ \u9700\u8981\u5728\u521b\u5efa\u8fd9\u4e9b\u53d8\u91cf\u65f6\uff0c\u4f7f\u7528\u9ed8\u8ba4\u503c\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u8fd9\u5c31\u610f\u5473\u7740\u9700\u8981\u6267\u884c100000\u6b21\u521d\u59cb\u5316\uff08\u521d\u59cb\u5316\u901f\u5ea6\u5f88\u6162\uff09\uff0c\u5176\u5e26\u6765\u7684\u597d\u5904\u4e5f\u5e76\u4e0d\u5927\uff08\u6bd5\u7adf\u8fd9\u4e9b\u521d\u59cb\u5316\u5f88\u5feb\u5c06\u4f1a\u88ab\u771f\u5b9e\u503c\u8986\u76d6\uff09\u3002 \u73b0\u5982\u4eca\uff0c\u6211\u4eec\u5219\u5e94\u8be5\u7262\u8bb0\u5bf9\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u5176\u5e26\u6765\u7684\u597d\u5904\u5df2\u7ecf\u8fdc\u5927\u4e8e\u5176\u5f00\u9500\u3002\u5f53\u4f60\u719f\u7ec3\u638c\u63e1\u8fd9\u95e8\u8bed\u8a00\u540e\uff0c\u4e5f\u8bb8\u4e5f\u4f1a\u9047\u5230\u4e3a\u4e86\u4f18\u5316\u6027\u80fd\u800c\u653e\u5f03\u521d\u59cb\u5316\u7684\u60c5\u51b5\u3002\u4f46\u8fd9\u6837\u7684\u64cd\u4f5c\u4e00\u5b9a\u662f\u4f60\u6df1\u601d\u719f\u8651\u540e\u6709\u610f\u800c\u4e3a\u4e4b\u7684\u3002 \u4f7f\u7528\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\u7684\u503c\uff0c\u53ef\u80fd\u4f1a\u5e26\u6765\u96be\u4ee5\u9884\u6599\u7684\u7ed3\u679c\u3002\u4ee5\u5982\u4e0b\u4ee3\u7801\u4e3a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { // define an integer variable named x int x ; // this variable is uninitialized because we haven't given it a value // print the value of x to the screen std :: cout << x ; // who knows what we'll get, because x is uninitialized return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u8ba1\u7b97\u673a\u4f1a\u4e3ax\u5206\u914d\u67d0\u7a7a\u95f2\u7684\u5185\u5b58\u3002\u8be5\u5185\u5b58\u4e2d\u5b58\u653e\u7684\u503c\u4f1a\u901a\u8fc7 std::cout \u8f93\u51fa\u5230\u63a7\u5236\u53f0\u5e76\u6253\u5370\uff0c\u5176\u6253\u5370\u51fa\u7684\u7ed3\u679c\u53ef\u80fd\u662f\u67d0\u4e2a\u503c\uff08\u88ab\u5f53\u505a\u6574\u578b\u6765\u89e3\u6790\uff09\u3002\u4f46\u7a76\u7adf\u662f\u4ec0\u4e48\u503c\u5462\uff1f\u7b54\u6848\u662f\uff1a\u201d\u6ca1\u6709\u4eba\u77e5\u9053\u201c\uff0c\u6216\u8005\u8bf4\uff0c\u6bcf\u6b21\u8fd0\u884c\u7a0b\u5e8f\uff0c\u7b54\u6848\u90fd\u53ef\u80fd\u662f\u4e0d\u4e00\u6837\u7684\u3002\u4f5c\u8005\u5728\u4f7f\u7528 Visual Studio \u8fdb\u884c\u6d4b\u8bd5\u7684\u65f6\u5019\uff0c\u6253\u5370\u51fa\u7684\u503c\u662f 7177728 \uff0c\u518d\u6b21\u8fd0\u884c\u65f6\u6253\u5370\u7684\u503c\u5c31\u7f16\u7a0b\u4e86 5277592 \u3002\u4f60\u53ef\u4ee5\u81ea\u5df1\u5c1d\u8bd5\u5e76\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\uff08\u4e0d\u7528\u62c5\u5fc3\uff0c\u8fd9\u5e76\u4e0d\u4f1a\u635f\u574f\u4f60\u7684\u7535\u8111\uff09\u3002 \u6ce8\u610f \u6709\u4e00\u4e9b\u7f16\u8bd1\u5668\uff0c\u4f8b\u5982 Visual Studio\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fdebug\u7248\u672c\u914d\u7f6e\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u5c06\u5185\u5b58\u521d\u59cb\u5316\u4e3a\u67d0\u4e9b\u9884\u8bbe\u503c\u3002\u4f46\u662f\u5f53\u4f60\u4f7f\u7528release\u7248\u672c\u914d\u7f6e\u65f6\uff0c\u5219\u4e0d\u4f1a\u6709\u4e0a\u8ff0\u884c\u4e3a\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u60f3\u8981\u81ea\u884c\u5c1d\u8bd5\u4e0a\u8ff0\u7a0b\u5e8f\uff0c\u8bf7\u786e\u4fdd\u81ea\u5df1\u4f7f\u7528\u7684\u662frelease\u7248\u672c\u7684\u6784\u5efa\u914d\u7f6e (\u53c2\u8003 0.9 -- Configuring your compiler: Build configurations ). \u4f8b\u5982\uff0c\u5f53\u4f60\u5728Visual Studio\u4e2d\u4f7f\u7528debug\u914d\u7f6e\u7f16\u8bd1\u5e76\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u65f6\uff0c\u8f93\u51fa\u7ed3\u679c\u59cb\u7ec8\u4f1a\u662f -858993460\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u503c\uff08\u88ab\u89e3\u91ca\u4e3a\u6574\u578b\uff09\u662f Visual Studio \u5728 debug \u914d\u7f6e\u4e0b\u521d\u59cb\u5316\u5185\u5b58\u4f7f\u7528\u7684\u503c\u3002 \u5927\u591a\u6570\u7684\u73b0\u4ee3\u7f16\u8bd1\u5668\u90fd\u80fd\u591f\u68c0\u6d4b\u5230\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u88ab\u8d4b\u4e88\u4e86\u521d\u503c\u3002\u5982\u679c\u80fd\u591f\u68c0\u6d4b\u5230\uff0c\u5219\u5728\u7f16\u8bd1\u65f6\u4f1a\u4ea7\u751f\u4e00\u4e2a\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u5728 Visual Studio \u4e2d\u7f16\u8bd1\u4e0a\u8ff0\u7a0b\u5e8f\u65f6\uff0c\u4f1a\u8f93\u51fa\u5982\u4e0b\u7684\u544a\u8b66\u4fe1\u606f\uff1a 1 c:\\VCprojects\\test\\test.cpp(11) : warning C4700: uninitialized local variable 'x' used \u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u4e0d\u5141\u8bb8\u4f60\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\uff0c\u53ef\u4ee5\u91c7\u7528\u5982\u4e0b\u65b9\u5f0f\u7ed5\u8fc7\u9650\u5236\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include void doNothing ( int & ) // Don't worry about what & is for now, we're just using it to trick the compiler into thinking variable x is used { } int main () { // define an integer variable named x int x ; // this variable is uninitialized doNothing ( x ); // make the compiler think we're assigning a value to this variable // print the value of x to the screen (who knows what we'll get, because x is uninitialized) std :: cout << x ; return 0 ; } \u4f7f\u7528\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\u662f\u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u72af\u7684\u9519\u8bef\u4e4b\u4e00\uff0c\u4e0d\u5e78\u7684\u662f\uff0c\u8be5\u9519\u8bef\u4e5f\u5e38\u5e38\u4f1a\u57cb\u4e0b\u6700\u96be\u4ee5\u8c03\u8bd5\u7684\u95ee\u9898\uff08\u56e0\u4e3a\u7a0b\u5e8f\u53ef\u80fd\u53ef\u4ee5\u6b63\u786e\u8fd0\u884c\uff0c\u5982\u679c\u8be5\u5185\u5b58\u4e2d\u6070\u597d\u5305\u542b\u4e86\u4e00\u4e2a\u5408\u7406\u7684\u503c\uff0c\u4f8b\u59820\uff09\u3002 \u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728\u6700\u4f73\u5b9e\u8df5\u4e2d\u6211\u4eec\u5f3a\u8c03\uff0c\u4e00\u5b9a\u8981\u786e\u4fdd\u521d\u59cb\u5316\u53d8\u91cf\u3002 \u672a\u5b9a\u4e49\u884c\u4e3a \u4f7f\u7528\u672a\u5b9a\u4e49\u53d8\u91cf\u4e2d\u7684\u503c\uff0c\u662f\u6211\u4eec\u9047\u5230\u7684\u7b2c\u4e00\u79cd\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u4f8b\u5b50\u3002\u672a\u5b9a\u4e49\u884c\u4e3a\uff08\u5e38\u5e38\u7f29\u5199\u4e3a UB\uff09\u6307\u7684\u662f\u6267\u884c\u4ee3\u7801\u7684\u884c\u4e3a\u6ca1\u6709\u88ab C++ \u8bed\u8a00\u660e\u786e\u5b9a\u4e49\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cC++ \u8bed\u8a00\u5e76\u6ca1\u6709\u660e\u786e\u7684\u89c4\u5219\u80fd\u591f\u7528\u4e8e\u786e\u5b9a\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002\u4ece\u7ed3\u679c\u4e0a\u6765\u770b\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u4e86\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\uff0c\u5219\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 \u4ee3\u7801\u7684\u672a\u5b9a\u4e49\u884c\u4e3a\u53ef\u80fd\u8868\u73b0\u51fa\u5982\u4e0b\u75c7\u72b6\uff1a \u7a0b\u5e8f\u6bcf\u6b21\u8fd0\u884c\u90fd\u53ef\u80fd\u5f97\u5230\u4e0d\u540c\u7684\u7ed3\u679c\uff1b \u7a0b\u5e8f\u6bcf\u6b21\u8fd0\u884c\u90fd\u8f93\u51fa\u76f8\u540c\u7684\u9519\u8bef\u7ed3\u679c\uff1b \u7a0b\u5e8f\u6bcf\u6b21\u8fd0\u884c\u7684\u884c\u4e3a\u90fd\u4e0d\u4e00\u81f4 (\u6709\u65f6\u5019\u7ed3\u679c\u6b63\u786e\uff0c\u6709\u65f6\u5019\u4e0d\u6b63\u786e)\uff1b \u7a0b\u5e8f\u770b\u8d77\u6765\u80fd\u591f\u6b63\u786e\u8fd0\u884c\uff0c\u4f46\u662f\u4e00\u6bb5\u65f6\u95f4\u540e\u53c8\u4f1a\u8f93\u51fa\u9519\u8bef\u7684\u7ed3\u679c\uff1b \u7a0b\u5e8f\u5d29\u6e83\u9000\u51fa\uff0c\u8fd9\u79cd\u5d29\u6e83\u53ef\u80fd\u5728\u7a0b\u5e8f\u542f\u52a8\u540e\u7acb\u5373\u53d1\u751f\uff0c\u4e5f\u53ef\u80fd\u8fd0\u884c\u4e00\u6bb5\u65f6\u95f4\u540e\u53d1\u751f\uff1b \u7a0b\u5e8f\u7ecf\u67d0\u4e9b\u7f16\u8bd1\u5668\u7f16\u8bd1\u540e\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\uff0c\u4f46\u6709\u4e9b\u7f16\u8bd1\u5668\u5219\u4e0d\u884c\uff1b \u7a0b\u5e8f\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u76f4\u5230\u4f60\u4fee\u6539\u4e86\u67d0\u4e9b\u770b\u4e0a\u6beb\u4e0d\u76f8\u5e72\u7684\u4ee3\u7801\u3002 \u6216\u8005\uff0c\u7a0b\u5e8f\u751a\u81f3\u80fd\u591f\u6b63\u786e\u8fd0\u884c\u5e76\u5f97\u5230\u6b63\u786e\u7ed3\u679c\u3002\u6240\u8c13\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u5c31\u662f\u4f60\u6c38\u8fdc\u4e0d\u77e5\u9053\u4f60\u80fd\u5f97\u5230\u4ec0\u4e48\u6837\u7684\u7ed3\u679c\uff0c\u65e0\u6cd5\u9884\u6d4b\u6bcf\u6b21\u8fd0\u884c\u7684\u884c\u4e3a\uff0c\u4e5f\u65e0\u6cd5\u9884\u6d4b\u4f60\u505a\u51fa\u67d0\u4e9b\u4fee\u6539\u540e\u7684\u5f71\u54cd\u3002 \u5728 C++ \u4e2d\uff0c\u5982\u679c\u4f60\u4e0d\u591f\u6d88\u606f\uff0c\u90a3\u4e48\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u9014\u5f84\u6709\u5f88\u591a\u3002\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u9047\u5230\u5177\u4f53\u95ee\u9898\u65f6\u8fdb\u884c\u8bb2\u89e3\u3002\u8bf7\u6ce8\u610f\u8bb0\u5f55\u8fd9\u4e9b\u6848\u4f8b\u5e76\u5207\u8bb0\u8981\u907f\u514d\u5b83\u4eec\u3002 \u6cd5\u5219 \u6ce8\u610f\u907f\u514d\u53ef\u80fd\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u5404\u79cd\u60c5\u51b5\uff0c\u4f8b\u5982\u4f7f\u7528\u672a\u521d\u59cb\u5316\u53d8\u91cf\u3002 \u4f5c\u8005\u6ce8 \u8bfb\u8005\u8bc4\u8bba\u4e2d\u6700\u5e38\u89c1\u7684\u95ee\u9898\u4e4b\u4e00\u4fbf\u662f\uff1a\u201d\u4f60\u8bf4\u4e0d\u80fd\u8fd9\u6837\u505a\uff0c\u4f46\u6211\u504f\u504f\u8fd9\u6837\u505a\u4e86\uff0c\u800c\u4e14\u7a0b\u5e8f\u4e5f\u80fd\u591f\u6b63\u5e38\u8fd0\u884c\uff01\u4e3a\u4ec0\u4e48\uff1f\u201c \u5173\u4e8e\u8fd9\u4e2a\u95ee\u9898\u901a\u5e38\u6709\u4e24\u4e2a\u7b54\u6848\u3002\u6700\u5e38\u89c1\u7684\u7b54\u6848\u662f\uff0c\u4f60\u7684\u7a0b\u5e8f\u5b9e\u9645\u4e0a\u8868\u73b0\u51fa\u4e86\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u53ea\u4e0d\u8fc7\u8be5\u672a\u5b9a\u4e49\u884c\u4e3a\u6070\u5de7\uff08\u6682\u65f6\uff09\u8f93\u51fa\u4e86\u4f60\u671f\u671b\u7684\u7ed3\u679c\u3002\u6539\u5929\uff08\u6216\u8005\u4f7f\u7528\u5176\u4ed6\u7f16\u8bd1\u5668\u3001\u4f7f\u7528\u5176\u4ed6\u8ba1\u7b97\u673a\uff09\u518d\u8fd0\u884c\u65f6\uff0c\u53ef\u80fd\u672a\u5fc5\u5982\u6b64\u3002 \u8fd9\u4e2a\u95ee\u9898\u8fd8\u53ef\u4ee5\u8fd9\u6837\u56de\u7b54\uff1a\u6709\u4e9b\u7f16\u8bd1\u5668\u7684\u4f5c\u8005\uff0c\u5728\u5b9e\u73b0\u7279\u5b9a\u8bed\u8a00\u7684\u9700\u6c42\u65f6\uff0c\u8ba4\u4e3a\u67d0\u4e9b\u89c4\u5219\u8fc7\u4e8e\u4e25\u683c\u662f\u6ca1\u6709\u5fc5\u8981\u7684\u3002\u4f8b\u5982\uff0c\u6807\u51c6\u53ef\u80fd\u4f1a\u8bf4\uff0cX \u5fc5\u987b\u4f4d\u4e8e Y \u4e4b\u524d\u53d1\u751f\u3002\u4f46\u662f\u7f16\u8bd1\u5668\u4f5c\u8005\u786e\u8ba4\u4e3a\u8fd9\u4e48\u505a\u662f\u6ca1\u6709\u5fc5\u8981\u7684\uff0c\u5e76\u4e14\u4f7f\u5f97 Y \u80fd\u591f\u5728\u6ca1\u6709\u6267\u884cX\u7684\u524d\u63d0\u4e0b\u6b63\u786e\u5de5\u4f5c\u3002\u8fd9\u4e48\u505a\u4e0d\u4f1a\u5f71\u54cd\u5230\u6b63\u786e\u7684\u4ee3\u7801\uff0c\u4f46\u662f\u53ef\u80fd\u4f1a\u9020\u6210\u4e0d\u7b26\u5408\u89c4\u8303\u7684\u4ee3\u7801\u80fd\u591f\u6b63\u786e\u6267\u884c\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u8bf4\uff1a\u4f60\u4f7f\u7528\u7684\u7f16\u8bd1\u5668\u53ef\u80fd\u5e76\u6ca1\u6709\u5b8c\u5168\u9075\u5b88\u6807\u51c6\u3002\u8fd9\u79cd\u60c5\u51b5\u662f\u5b58\u5728\u7684\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5173\u95ed\u7f16\u8bd1\u5668\u7684\u6269\u5c55\u529f\u80fd\u6765\u907f\u514d\u8fd9\u4e9b\u95ee\u9898\uff0c\u5177\u4f53\u5185\u5bb9\u8bf7\u53c2\u8003 0.10 -- Configuring your compiler: Compiler extensions .","title":"1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a"},{"location":"1-6-Uninitialized-variables-and-undefined-behavior/#_1","text":"\u4e0d\u540c\u4e8e\u6709\u4e9b\u7f16\u7a0b\u8bed\u8a00\uff0cC/C++ \u5e76\u4e0d\u4f1a\u81ea\u52a8\u5730\u5c06\u5927\u90e8\u5206\u53d8\u91cf\u521d\u59cb\u5316\u4e3a\u67d0\u4e2a\u7ed9\u5b9a\u503c\uff08\u4f8b\u59820\uff09\u3002\u56e0\u6b64\uff0c\u5f53\u7f16\u8bd1\u5668\u4e3a\u53d8\u91cf\u5206\u914d\u5185\u5b58\u5730\u5740\u65f6\uff0c\u9ed8\u8ba4\u7684\u521d\u503c\u5c06\u4f1a\u662f\u4efb\u4f55\u6070\u597d\u4f4d\u4e8e\u8be5\u5730\u5740\u7684\u503c\uff08\u5783\u573e\u503c\uff09\u3002\u4e00\u4e2a\u6ca1\u6709\u88ab\u660e\u786e\u8d4b\u4e88\u6307\u5b9a\u521d\u503c\uff08\u901a\u8fc7\u521d\u59cb\u5316\u6216\u8d4b\u503c\uff09\u7684\u53d8\u91cf\u79f0\u4e3a\u672a\u521d\u59cb\u5316\u53d8\u91cf\u3002 \u4f5c\u8005\u6ce8 \u5f88\u591a\u8bfb\u8005\u60f3\u5f53\u7136\u5730\u8ba4\u4e3a\uff0c\u201d\u521d\u59cb\u5316\u7684\u201c\u548c\"\u672a\u521d\u59cb\u5316\u7684\"\u662f\u4e00\u5bf9\u4e25\u683c\u7684\u53cd\u4e49\u8bcd\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u3002\u521d\u59cb\u5316\u610f\u5473\u7740\u5bf9\u8c61\u5728\u88ab\u5b9a\u4e49\u7684\u65f6\u5019\u5373\u83b7\u5f97\u4e86\u4e00\u4e2a\u7ed9\u5b9a\u7684\u521d\u59cb\u503c\u3002\u800c\u672a\u521d\u59cb\u5316\u5219\u8868\u793a\uff0c\u5bf9\u8c61\u6ca1\u6709\u88ab\u660e\u786e\u5730\u6307\u5b9a\u4efb\u4f55\u503c\uff08\u901a\u8fc7\u5404\u79cd\u9014\u5f84\uff0c\u5305\u62ec\u8d4b\u503c\uff09\u3002\u56e0\u6b64\uff0c\u4e00\u4e2a\u5bf9\u8c61\u5982\u679c\u6ca1\u6709\u88ab\u201d\u521d\u59cb\u5316\u201c\uff0c\u4f46\u901a\u8fc7\u8d4b\u503c\u5f97\u5230\u4e86\u4e00\u4e2a\u521d\u503c\uff0c\u5b83\u4e5f\u4e0d\u518d\u662f\u201d\u672a\u521d\u59cb\u5316\u201c\u72b6\u6001\uff08\u56e0\u4e3a\u5b83\u5df2\u7ecf\u5f97\u5230\u4e86\u4e00\u4e2a\u503c\uff09 \u7b80\u5355\u603b\u7ed3\u4e00\u4e0b\uff1a - \u521d\u59cb\u5316\uff1a\u5bf9\u8c61\u5728\u88ab\u5b9a\u4e49\u7684\u540c\u65f6\u4e5f\u88ab\u8d4b\u4e88\u4e86\u521d\u503c - \u8d4b\u503c\uff1a\u5bf9\u8c61\u5728\u5b9a\u4e49\u540e\uff0c\u88ab\u8d4b\u4e88\u4e86\u4e00\u4e2a\u503c - \u672a\u521d\u59cb\u5316\uff1a\u5bf9\u8c61\u59cb\u7ec8\u6ca1\u6709\u80fd\u591f\u83b7\u5f97\u4e00\u4e2a\u660e\u786e\u6307\u5b9a\u7684\u521d\u503c\u3002 \u9898\u5916\u8bdd \u4e0d\u8fdb\u884c\u9ed8\u8ba4\u521d\u59cb\u5316\u8fd9\u4e00\u7b56\u7565\u662f\u4eceC\u8bed\u8a00\u7ee7\u627f\u800c\u6765\uff0c\u4e3b\u8981\u51fa\u4e8e\u6027\u80fd\u4f18\u5316\u8003\u8651\uff0c\u56e0\u4e3a\u5f53\u65f6\u7684\u8ba1\u7b97\u673a\u8fd0\u884c\u901f\u5ea6\u8fd8\u5f88\u6162\u3002\u4f60\u53ef\u4ee5\u60f3\u8c61\u4e00\u4e0b\uff0c\u5047\u8bbe\u6211\u4eec\u9700\u8981\u4ece\u6587\u4ef6\u8bfb\u53d6100000\u6761\u6570\u636e\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u4f60\u9700\u8981\u521b\u5efa100000\u4e2a\u53d8\u91cf\u5e76\u4f7f\u7528\u8bfb\u53d6\u7684\u6570\u636e\u6765\u586b\u5145\u5b83\u4eec\u3002 \u5982\u679c C++ \u9700\u8981\u5728\u521b\u5efa\u8fd9\u4e9b\u53d8\u91cf\u65f6\uff0c\u4f7f\u7528\u9ed8\u8ba4\u503c\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u8fd9\u5c31\u610f\u5473\u7740\u9700\u8981\u6267\u884c100000\u6b21\u521d\u59cb\u5316\uff08\u521d\u59cb\u5316\u901f\u5ea6\u5f88\u6162\uff09\uff0c\u5176\u5e26\u6765\u7684\u597d\u5904\u4e5f\u5e76\u4e0d\u5927\uff08\u6bd5\u7adf\u8fd9\u4e9b\u521d\u59cb\u5316\u5f88\u5feb\u5c06\u4f1a\u88ab\u771f\u5b9e\u503c\u8986\u76d6\uff09\u3002 \u73b0\u5982\u4eca\uff0c\u6211\u4eec\u5219\u5e94\u8be5\u7262\u8bb0\u5bf9\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u5176\u5e26\u6765\u7684\u597d\u5904\u5df2\u7ecf\u8fdc\u5927\u4e8e\u5176\u5f00\u9500\u3002\u5f53\u4f60\u719f\u7ec3\u638c\u63e1\u8fd9\u95e8\u8bed\u8a00\u540e\uff0c\u4e5f\u8bb8\u4e5f\u4f1a\u9047\u5230\u4e3a\u4e86\u4f18\u5316\u6027\u80fd\u800c\u653e\u5f03\u521d\u59cb\u5316\u7684\u60c5\u51b5\u3002\u4f46\u8fd9\u6837\u7684\u64cd\u4f5c\u4e00\u5b9a\u662f\u4f60\u6df1\u601d\u719f\u8651\u540e\u6709\u610f\u800c\u4e3a\u4e4b\u7684\u3002 \u4f7f\u7528\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\u7684\u503c\uff0c\u53ef\u80fd\u4f1a\u5e26\u6765\u96be\u4ee5\u9884\u6599\u7684\u7ed3\u679c\u3002\u4ee5\u5982\u4e0b\u4ee3\u7801\u4e3a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { // define an integer variable named x int x ; // this variable is uninitialized because we haven't given it a value // print the value of x to the screen std :: cout << x ; // who knows what we'll get, because x is uninitialized return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u8ba1\u7b97\u673a\u4f1a\u4e3ax\u5206\u914d\u67d0\u7a7a\u95f2\u7684\u5185\u5b58\u3002\u8be5\u5185\u5b58\u4e2d\u5b58\u653e\u7684\u503c\u4f1a\u901a\u8fc7 std::cout \u8f93\u51fa\u5230\u63a7\u5236\u53f0\u5e76\u6253\u5370\uff0c\u5176\u6253\u5370\u51fa\u7684\u7ed3\u679c\u53ef\u80fd\u662f\u67d0\u4e2a\u503c\uff08\u88ab\u5f53\u505a\u6574\u578b\u6765\u89e3\u6790\uff09\u3002\u4f46\u7a76\u7adf\u662f\u4ec0\u4e48\u503c\u5462\uff1f\u7b54\u6848\u662f\uff1a\u201d\u6ca1\u6709\u4eba\u77e5\u9053\u201c\uff0c\u6216\u8005\u8bf4\uff0c\u6bcf\u6b21\u8fd0\u884c\u7a0b\u5e8f\uff0c\u7b54\u6848\u90fd\u53ef\u80fd\u662f\u4e0d\u4e00\u6837\u7684\u3002\u4f5c\u8005\u5728\u4f7f\u7528 Visual Studio \u8fdb\u884c\u6d4b\u8bd5\u7684\u65f6\u5019\uff0c\u6253\u5370\u51fa\u7684\u503c\u662f 7177728 \uff0c\u518d\u6b21\u8fd0\u884c\u65f6\u6253\u5370\u7684\u503c\u5c31\u7f16\u7a0b\u4e86 5277592 \u3002\u4f60\u53ef\u4ee5\u81ea\u5df1\u5c1d\u8bd5\u5e76\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\uff08\u4e0d\u7528\u62c5\u5fc3\uff0c\u8fd9\u5e76\u4e0d\u4f1a\u635f\u574f\u4f60\u7684\u7535\u8111\uff09\u3002 \u6ce8\u610f \u6709\u4e00\u4e9b\u7f16\u8bd1\u5668\uff0c\u4f8b\u5982 Visual Studio\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fdebug\u7248\u672c\u914d\u7f6e\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u5c06\u5185\u5b58\u521d\u59cb\u5316\u4e3a\u67d0\u4e9b\u9884\u8bbe\u503c\u3002\u4f46\u662f\u5f53\u4f60\u4f7f\u7528release\u7248\u672c\u914d\u7f6e\u65f6\uff0c\u5219\u4e0d\u4f1a\u6709\u4e0a\u8ff0\u884c\u4e3a\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u60f3\u8981\u81ea\u884c\u5c1d\u8bd5\u4e0a\u8ff0\u7a0b\u5e8f\uff0c\u8bf7\u786e\u4fdd\u81ea\u5df1\u4f7f\u7528\u7684\u662frelease\u7248\u672c\u7684\u6784\u5efa\u914d\u7f6e (\u53c2\u8003 0.9 -- Configuring your compiler: Build configurations ). \u4f8b\u5982\uff0c\u5f53\u4f60\u5728Visual Studio\u4e2d\u4f7f\u7528debug\u914d\u7f6e\u7f16\u8bd1\u5e76\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u65f6\uff0c\u8f93\u51fa\u7ed3\u679c\u59cb\u7ec8\u4f1a\u662f -858993460\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u503c\uff08\u88ab\u89e3\u91ca\u4e3a\u6574\u578b\uff09\u662f Visual Studio \u5728 debug \u914d\u7f6e\u4e0b\u521d\u59cb\u5316\u5185\u5b58\u4f7f\u7528\u7684\u503c\u3002 \u5927\u591a\u6570\u7684\u73b0\u4ee3\u7f16\u8bd1\u5668\u90fd\u80fd\u591f\u68c0\u6d4b\u5230\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u88ab\u8d4b\u4e88\u4e86\u521d\u503c\u3002\u5982\u679c\u80fd\u591f\u68c0\u6d4b\u5230\uff0c\u5219\u5728\u7f16\u8bd1\u65f6\u4f1a\u4ea7\u751f\u4e00\u4e2a\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u5728 Visual Studio \u4e2d\u7f16\u8bd1\u4e0a\u8ff0\u7a0b\u5e8f\u65f6\uff0c\u4f1a\u8f93\u51fa\u5982\u4e0b\u7684\u544a\u8b66\u4fe1\u606f\uff1a 1 c:\\VCprojects\\test\\test.cpp(11) : warning C4700: uninitialized local variable 'x' used \u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u4e0d\u5141\u8bb8\u4f60\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\uff0c\u53ef\u4ee5\u91c7\u7528\u5982\u4e0b\u65b9\u5f0f\u7ed5\u8fc7\u9650\u5236\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include void doNothing ( int & ) // Don't worry about what & is for now, we're just using it to trick the compiler into thinking variable x is used { } int main () { // define an integer variable named x int x ; // this variable is uninitialized doNothing ( x ); // make the compiler think we're assigning a value to this variable // print the value of x to the screen (who knows what we'll get, because x is uninitialized) std :: cout << x ; return 0 ; } \u4f7f\u7528\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\u662f\u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u72af\u7684\u9519\u8bef\u4e4b\u4e00\uff0c\u4e0d\u5e78\u7684\u662f\uff0c\u8be5\u9519\u8bef\u4e5f\u5e38\u5e38\u4f1a\u57cb\u4e0b\u6700\u96be\u4ee5\u8c03\u8bd5\u7684\u95ee\u9898\uff08\u56e0\u4e3a\u7a0b\u5e8f\u53ef\u80fd\u53ef\u4ee5\u6b63\u786e\u8fd0\u884c\uff0c\u5982\u679c\u8be5\u5185\u5b58\u4e2d\u6070\u597d\u5305\u542b\u4e86\u4e00\u4e2a\u5408\u7406\u7684\u503c\uff0c\u4f8b\u59820\uff09\u3002 \u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728\u6700\u4f73\u5b9e\u8df5\u4e2d\u6211\u4eec\u5f3a\u8c03\uff0c\u4e00\u5b9a\u8981\u786e\u4fdd\u521d\u59cb\u5316\u53d8\u91cf\u3002","title":"\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf"},{"location":"1-6-Uninitialized-variables-and-undefined-behavior/#_2","text":"\u4f7f\u7528\u672a\u5b9a\u4e49\u53d8\u91cf\u4e2d\u7684\u503c\uff0c\u662f\u6211\u4eec\u9047\u5230\u7684\u7b2c\u4e00\u79cd\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u4f8b\u5b50\u3002\u672a\u5b9a\u4e49\u884c\u4e3a\uff08\u5e38\u5e38\u7f29\u5199\u4e3a UB\uff09\u6307\u7684\u662f\u6267\u884c\u4ee3\u7801\u7684\u884c\u4e3a\u6ca1\u6709\u88ab C++ \u8bed\u8a00\u660e\u786e\u5b9a\u4e49\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cC++ \u8bed\u8a00\u5e76\u6ca1\u6709\u660e\u786e\u7684\u89c4\u5219\u80fd\u591f\u7528\u4e8e\u786e\u5b9a\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002\u4ece\u7ed3\u679c\u4e0a\u6765\u770b\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u4e86\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\uff0c\u5219\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 \u4ee3\u7801\u7684\u672a\u5b9a\u4e49\u884c\u4e3a\u53ef\u80fd\u8868\u73b0\u51fa\u5982\u4e0b\u75c7\u72b6\uff1a \u7a0b\u5e8f\u6bcf\u6b21\u8fd0\u884c\u90fd\u53ef\u80fd\u5f97\u5230\u4e0d\u540c\u7684\u7ed3\u679c\uff1b \u7a0b\u5e8f\u6bcf\u6b21\u8fd0\u884c\u90fd\u8f93\u51fa\u76f8\u540c\u7684\u9519\u8bef\u7ed3\u679c\uff1b \u7a0b\u5e8f\u6bcf\u6b21\u8fd0\u884c\u7684\u884c\u4e3a\u90fd\u4e0d\u4e00\u81f4 (\u6709\u65f6\u5019\u7ed3\u679c\u6b63\u786e\uff0c\u6709\u65f6\u5019\u4e0d\u6b63\u786e)\uff1b \u7a0b\u5e8f\u770b\u8d77\u6765\u80fd\u591f\u6b63\u786e\u8fd0\u884c\uff0c\u4f46\u662f\u4e00\u6bb5\u65f6\u95f4\u540e\u53c8\u4f1a\u8f93\u51fa\u9519\u8bef\u7684\u7ed3\u679c\uff1b \u7a0b\u5e8f\u5d29\u6e83\u9000\u51fa\uff0c\u8fd9\u79cd\u5d29\u6e83\u53ef\u80fd\u5728\u7a0b\u5e8f\u542f\u52a8\u540e\u7acb\u5373\u53d1\u751f\uff0c\u4e5f\u53ef\u80fd\u8fd0\u884c\u4e00\u6bb5\u65f6\u95f4\u540e\u53d1\u751f\uff1b \u7a0b\u5e8f\u7ecf\u67d0\u4e9b\u7f16\u8bd1\u5668\u7f16\u8bd1\u540e\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\uff0c\u4f46\u6709\u4e9b\u7f16\u8bd1\u5668\u5219\u4e0d\u884c\uff1b \u7a0b\u5e8f\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u76f4\u5230\u4f60\u4fee\u6539\u4e86\u67d0\u4e9b\u770b\u4e0a\u6beb\u4e0d\u76f8\u5e72\u7684\u4ee3\u7801\u3002 \u6216\u8005\uff0c\u7a0b\u5e8f\u751a\u81f3\u80fd\u591f\u6b63\u786e\u8fd0\u884c\u5e76\u5f97\u5230\u6b63\u786e\u7ed3\u679c\u3002\u6240\u8c13\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u5c31\u662f\u4f60\u6c38\u8fdc\u4e0d\u77e5\u9053\u4f60\u80fd\u5f97\u5230\u4ec0\u4e48\u6837\u7684\u7ed3\u679c\uff0c\u65e0\u6cd5\u9884\u6d4b\u6bcf\u6b21\u8fd0\u884c\u7684\u884c\u4e3a\uff0c\u4e5f\u65e0\u6cd5\u9884\u6d4b\u4f60\u505a\u51fa\u67d0\u4e9b\u4fee\u6539\u540e\u7684\u5f71\u54cd\u3002 \u5728 C++ \u4e2d\uff0c\u5982\u679c\u4f60\u4e0d\u591f\u6d88\u606f\uff0c\u90a3\u4e48\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u9014\u5f84\u6709\u5f88\u591a\u3002\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u9047\u5230\u5177\u4f53\u95ee\u9898\u65f6\u8fdb\u884c\u8bb2\u89e3\u3002\u8bf7\u6ce8\u610f\u8bb0\u5f55\u8fd9\u4e9b\u6848\u4f8b\u5e76\u5207\u8bb0\u8981\u907f\u514d\u5b83\u4eec\u3002 \u6cd5\u5219 \u6ce8\u610f\u907f\u514d\u53ef\u80fd\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u5404\u79cd\u60c5\u51b5\uff0c\u4f8b\u5982\u4f7f\u7528\u672a\u521d\u59cb\u5316\u53d8\u91cf\u3002 \u4f5c\u8005\u6ce8 \u8bfb\u8005\u8bc4\u8bba\u4e2d\u6700\u5e38\u89c1\u7684\u95ee\u9898\u4e4b\u4e00\u4fbf\u662f\uff1a\u201d\u4f60\u8bf4\u4e0d\u80fd\u8fd9\u6837\u505a\uff0c\u4f46\u6211\u504f\u504f\u8fd9\u6837\u505a\u4e86\uff0c\u800c\u4e14\u7a0b\u5e8f\u4e5f\u80fd\u591f\u6b63\u5e38\u8fd0\u884c\uff01\u4e3a\u4ec0\u4e48\uff1f\u201c \u5173\u4e8e\u8fd9\u4e2a\u95ee\u9898\u901a\u5e38\u6709\u4e24\u4e2a\u7b54\u6848\u3002\u6700\u5e38\u89c1\u7684\u7b54\u6848\u662f\uff0c\u4f60\u7684\u7a0b\u5e8f\u5b9e\u9645\u4e0a\u8868\u73b0\u51fa\u4e86\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u53ea\u4e0d\u8fc7\u8be5\u672a\u5b9a\u4e49\u884c\u4e3a\u6070\u5de7\uff08\u6682\u65f6\uff09\u8f93\u51fa\u4e86\u4f60\u671f\u671b\u7684\u7ed3\u679c\u3002\u6539\u5929\uff08\u6216\u8005\u4f7f\u7528\u5176\u4ed6\u7f16\u8bd1\u5668\u3001\u4f7f\u7528\u5176\u4ed6\u8ba1\u7b97\u673a\uff09\u518d\u8fd0\u884c\u65f6\uff0c\u53ef\u80fd\u672a\u5fc5\u5982\u6b64\u3002 \u8fd9\u4e2a\u95ee\u9898\u8fd8\u53ef\u4ee5\u8fd9\u6837\u56de\u7b54\uff1a\u6709\u4e9b\u7f16\u8bd1\u5668\u7684\u4f5c\u8005\uff0c\u5728\u5b9e\u73b0\u7279\u5b9a\u8bed\u8a00\u7684\u9700\u6c42\u65f6\uff0c\u8ba4\u4e3a\u67d0\u4e9b\u89c4\u5219\u8fc7\u4e8e\u4e25\u683c\u662f\u6ca1\u6709\u5fc5\u8981\u7684\u3002\u4f8b\u5982\uff0c\u6807\u51c6\u53ef\u80fd\u4f1a\u8bf4\uff0cX \u5fc5\u987b\u4f4d\u4e8e Y \u4e4b\u524d\u53d1\u751f\u3002\u4f46\u662f\u7f16\u8bd1\u5668\u4f5c\u8005\u786e\u8ba4\u4e3a\u8fd9\u4e48\u505a\u662f\u6ca1\u6709\u5fc5\u8981\u7684\uff0c\u5e76\u4e14\u4f7f\u5f97 Y \u80fd\u591f\u5728\u6ca1\u6709\u6267\u884cX\u7684\u524d\u63d0\u4e0b\u6b63\u786e\u5de5\u4f5c\u3002\u8fd9\u4e48\u505a\u4e0d\u4f1a\u5f71\u54cd\u5230\u6b63\u786e\u7684\u4ee3\u7801\uff0c\u4f46\u662f\u53ef\u80fd\u4f1a\u9020\u6210\u4e0d\u7b26\u5408\u89c4\u8303\u7684\u4ee3\u7801\u80fd\u591f\u6b63\u786e\u6267\u884c\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u8bf4\uff1a\u4f60\u4f7f\u7528\u7684\u7f16\u8bd1\u5668\u53ef\u80fd\u5e76\u6ca1\u6709\u5b8c\u5168\u9075\u5b88\u6807\u51c6\u3002\u8fd9\u79cd\u60c5\u51b5\u662f\u5b58\u5728\u7684\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5173\u95ed\u7f16\u8bd1\u5668\u7684\u6269\u5c55\u529f\u80fd\u6765\u907f\u514d\u8fd9\u4e9b\u95ee\u9898\uff0c\u5177\u4f53\u5185\u5bb9\u8bf7\u53c2\u8003 0.10 -- Configuring your compiler: Compiler extensions .","title":"\u672a\u5b9a\u4e49\u884c\u4e3a"},{"location":"1-7-Keywords-and-naming-identifiers/","tags":["identifier"],"text":"Key Takeaway \u5173\u952e\u5b57 C++ \u9884\u7559\u4e86 92 \u4e2a\u8bcd\u81ea\u7528(\u5bf9\u4e8e C++20 \u800c\u8a00) \u3002\u8fd9\u4e9b\u8bcd\u79f0\u4e3a \u5173\u952e\u5b57 (\u6216\u4fdd\u7559\u5b57)\uff0c\u6bcf\u4e2a\u5173\u952e\u5b57\u5728C++\u4e2d\u90fd\u6709\u5176\u7279\u5b9a\u7684\u542b\u4e49\u3002 \u4e0b\u8868\u5217\u4e3e\u4e86 C++ 20 \u4e2d\u7684\u5168\u90e8\u5173\u952e\u5b57\uff1a \u5173\u952e\u5b57\u540e\u6709\u6807\u8bc6 C++20 \u7684\uff0c\u5c5e\u4e8e C++20 \u65b0\u589e\u7684\u5173\u952e\u5b57\u3002\u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u4e0d\u652f\u6301 C++ 20\uff08\u6216\u8005\u5176\u5177\u5907 C++20 \u529f\u80fd\u4f46\u9ed8\u8ba4\u662f\u5173\u95ed\u7684\uff09\uff0c\u8fd9\u4e9b\u5173\u952e\u5b57\u5c06\u4e0d\u5177\u5907\u5b9e\u9645\u7528\u9014\u3002 C++ \u8fd8\u5b9a\u4e49\u4e86\u4e00\u4e9b\u7279\u6b8a\u7684\u6807\u8bc6\u7b26\uff1a override \u3001 final \u3001 import \u548c module \u3002\u8fd9\u4e9b\u6807\u8bc6\u7b26\u5728\u7279\u5b9a\u4e0a\u4e0b\u6587\u4e2d\u5177\u6709\u7279\u6b8a\u7684\u542b\u4e49\uff0c\u4f46\u5b83\u4eec\u5e76\u4e0d\u662fC++\u4fdd\u7559\u5b57\u3002 \u4f60\u5df2\u7ecf\u63a5\u89e6\u8fc7\u5176\u4e2d\u7684\u4e00\u4e9b\u5173\u952e\u5b57\uff0c\u5305\u62ec int \u548c return \u3002\u8fd9\u4e9b\u5173\u952e\u5b57\u3001\u6807\u8bc6\u7b26\u4ee5\u53ca\u4e00\u7ec4\u8fd0\u7b97\u7b26\u5c31\u6784\u6210\u4e86 C++ \u8bed\u8a00\u7684\u5168\u90e8\uff08\u9884\u5904\u7406\u5668\u6307\u4ee4\u9664\u5916\uff09\u3002\u7531\u4e8e\u5173\u952e\u5b57\u548c\u7279\u6b8a\u7684\u6807\u8bc6\u7b26\u5177\u6709\u7279\u6b8a\u542b\u4e49\uff0c\u56e0\u6b64\u4f60\u7684 IDE \u901a\u5e38\u4f1a\u5c06\u5176\u6807\u8bb0\u4e3a\u4e0d\u540c\u7684\u989c\u8272\u4ee5\u4f7f\u5176\u66f4\u52a0\u9192\u76ee\u3002 \u5728\u5b8c\u6210\u4e86\u672c\u6559\u7a0b\u7684\u5b66\u4e60\u540e\uff0c\u4f60\u4f1a\u7406\u89e3\u4e0a\u8ff0\u5173\u952e\u5b57\u4e2d\u51e0\u4e4e\u5168\u90e8\u7684\u529f\u80fd\uff01 \u547d\u540d\u89c4\u5219 \u63d0\u9192\u4e00\u4e0b\uff0c\u53d8\u91cf\uff08\u6216\u51fd\u6570\u3001\u7c7b\u578b\u4ee5\u53ca\u5176\u4ed6\u4efb\u4f55\u5143\u7d20\uff09\u7684\u540d\u79f0\u90fd\u79f0\u4e3a\u6807\u8bc6\u7b26\u3002C++ \u5141\u8bb8\u4f60\u7075\u6d3b\u5730\u9009\u62e9\u6807\u8bc6\u7b26\u3002\u4e0d\u8fc7\uff0c\u5728\u9009\u62e9\u547d\u540d\u6807\u8bc6\u7b26\u65f6\uff0c\u4f60\u9700\u8981\u9075\u5faa\u4ee5\u4e0b\u89c4\u5219\uff1a \u6807\u8bc6\u7b26\u4e0d\u80fd\u662f\u5173\u952e\u5b57\uff0c\u5173\u952e\u5b57\u662f\u8bed\u8a00\u7684\u4fdd\u7559\u5b57\uff1b \u6807\u8bc6\u7b26\u53ea\u80fd\u4f7f\u7528\u5927\u5c0f\u5199\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6807\u8bc6\u7b26\u4e2d\u4e0d\u80fd\u5305\u542b\u7b26\u53f7\uff08\u9664\u4e86\u4e0b\u5212\u7ebf\uff09\u6216\u8005\u7a7a\u767d\u7b26\uff08\u7a7a\u683c\u6216\u5236\u8868\u7b26\uff09\uff1b \u6807\u8bc6\u7b26\u5fc5\u987b\u4ee5\u5b57\u6bcd\uff08\u5927\u5c0f\u5199\u5747\u53ef\uff09\u6216\u4e0b\u5212\u7ebf\u5f00\u5934\uff0c\u4e0d\u80fd\u591f\u4ee5\u6570\u5b57\u5f00\u5934\uff1b C++ \u662f\u5927\u5c0f\u5199\u654f\u611f\u7684\uff0c\u56e0\u6b64\u5b83\u4f1a\u533a\u5206\u5927\u5c0f\u5199\u5b57\u6bcd\u3002 nvalue \u548c nValue \u662f\u4e0d\u540c\u7684\uff0c\u548c NVALUE \u4e5f\u4e0d\u540c\uff0c \u547d\u540d\u6700\u4f73\u5b9e\u8df5 \u73b0\u5728\uff0c\u60f3\u5fc5\u4f60\u5df2\u7ecf\u77e5\u9053 \u5982\u4f55 \u4e3a\u53d8\u91cf\u547d\u540d\u4e86\uff0c\u63a5\u4e0b\u6765\u8ba9\u6211\u4eec\u804a\u804a\uff0c\u4f60 \u5e94\u8be5\u5982\u4f55 \u4e3a\u53d8\u91cf\uff08\u6216\u51fd\u6570\uff09\u547d\u540d\u3002 \u9996\u5148\uff0cC++ \u4e2d\u7ea6\u5b9a\u4fd7\u6210\u7684\u662f\uff0c\u53d8\u91cf\u540d\u5e94\u8be5\u4ee5\u5c0f\u5199\u5b57\u6bcd\u5f00\u5934\u3002\u5982\u679c\u53d8\u91cf\u540d\u662f\u4e00\u4e2a\u5355\u8bcd\uff0c\u5219\u6574\u4e2a\u5355\u8bcd\u90fd\u5e94\u8be5\u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd\u3002 1 2 3 4 5 int value ; // correct int Value ; // incorrect (should start with lower case letter) int VALUE ; // incorrect (should start with lower case letter) int VaLuE ; // incorrect (see your psychiatrist) ;) \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u51fd\u6570\u540d\u540c\u6837\u4e5f\u662f\u5c0f\u5199\u5b57\u6bcd\u5f00\u5934\u7684\uff08\u5c3d\u7ba1\u5728\u8fd9\u4e00\u70b9\u4e0a\u6709\u5f88\u591a\u4e0d\u540c\u610f\u89c1\uff09\u3002\u6211\u4eec\u4f1a\u9075\u5faa\u8fd9\u4e00\u60ef\u4f8b\uff0c\u56e0\u4e3a main \u51fd\u6570\u5c31\u662f\u5c0f\u5199\u5b57\u6bcd\u5f00\u5934\u7684\uff08\u800c\u4e14 main \u51fd\u6570\u662f\u4efb\u4f55\u7a0b\u5e8f\u90fd\u9700\u8981\u6709\u7684\uff09\u3002\u6b64\u5916\uff0cC++\u6807\u51c6\u5e93\u4e2d\u7684\u6240\u6709\u51fd\u6570\u4e5f\u90fd\u9075\u5faa\u8fd9\u4e00\u539f\u5219\u3002 \u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684\u547d\u540d\u6807\u8bc6\u7b26\u901a\u5e38\u7528\u4e8e\u547d\u540d\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u578b\uff08\u4f8b\u5982\u7ed3\u6784\u4f53\u3001\u7c7b\u548c\u679a\u4e3e\uff0c\u7a0d\u540e\u6211\u4eec\u4f1a\u4ecb\u7ecd\u8fd9\u4e9b\u7c7b\u578b\uff09\u3002 \u5982\u679c\u53d8\u91cf\u540d\u6216\u51fd\u6570\u540d\u7531\u591a\u4e2a\u5355\u8bcd\u7ec4\u6210\uff0c\u5e38\u89c1\u7684\u547d\u540d\u60ef\u4f8b\u6709\u4e24\u79cd\uff0c\u4e00\u79cd\u662f\u4f7f\u7528\u4e0b\u5212\u7ebf\u5206\u5272\u5355\u8bcd\uff0c\u79f0\u4e3a \u86c7\u5f62\u547d\u540d\u6cd5(snake case) \uff0c\u53e6\u5916\u4e00\u79cd\u662f\u4f7f\u7528\u5927\u5c0f\u5199\u95f4\u9694\u7684\u65b9\u6cd5\uff08\u6709\u65f6\u5019\u79f0\u4e3a\u9a7c\u5cf0\u547d\u540d\u6cd5\uff0c\u56e0\u4e3a\u5927\u5199\u5b57\u6bcd\u6bd4\u8f83\u7a81\u51fa\uff0c\u5c31\u50cf\u9a86\u9a7c\u7684\u9a7c\u5cf0\u4e00\u6837\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 int my_variable_name ; // correct (separated by underscores/snake_case) int my_function_name (); // correct (separated by underscores/snake_case) int myVariableName ; // correct (intercapped/CamelCase) int myFunctionName (); // correct (intercapped/CamelCase) int my variable name ; // invalid (whitespace not allowed) int my function name (); // invalid (whitespace not allowed) int MyVariableName ; // valid but incorrect (should start with lower case letter) int MyFunctionName (); // valid but incorrect (should start with lower case letter) \u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528\u9a7c\u5cf0\u547d\u540d\u6cd5\uff0c\u56e0\u4e3a\u5b83\u53ef\u8bfb\u6027\u66f4\u597d\uff08\u5bf9\u4e8e\u86c7\u5f62\u547d\u540d\u6cd5\u6765\u8bf4\uff0c\u6709\u65f6\u4e0b\u5212\u7ebf\u5bb9\u6613\u548c\u7a7a\u683c\u6df7\u6dc6\uff09\u3002\u4f46\u662f\uff0c\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u5f88\u5e38\u89c1\uff0cC++ \u7684\u6807\u51c6\u5e93\u5728\u547d\u540d\u53d8\u91cf\u548c\u51fd\u6570\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u7684\u5c31\u662f\u57fa\u4e8e\u4e0b\u5212\u7ebf\u7684\u86c7\u5f62\u547d\u540d\u6cd5\u3002\u6709\u65f6\u5019\uff0c\u4f60\u751a\u81f3\u4f1a\u9047\u5230\u6df7\u7528\u4e24\u79cd\u547d\u540d\u6cd5\u7684\u60c5\u51b5\uff0c\u86c7\u5f62\u547d\u540d\u6cd5\u7528\u4e8e\u53d8\u91cf\u540d\u800c\u9a7c\u5cf0\u547d\u540d\u6cd5\u5219\u7528\u4e8e\u51fd\u6570\u540d\u3002 \u5982\u679c\u4f60\u7684\u5de5\u4f5c\u662f\u5728\u5176\u4ed6\u4eba\u7684\u4ee3\u7801\u4e0a\u8fdb\u884c\u5f00\u53d1\uff0c\u90a3\u4e48\u8fd9\u4e9b\u5c31\u4e0d\u91cd\u8981\u4e86\u3002\u56e0\u4e3a\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6700\u597d\u7684\u505a\u6cd5\u662f\u9075\u5faa\u4ed6\u4eba\u7684\u547d\u540d\u89c4\u5219\u800c\u4e0d\u662f\u6b7b\u677f\u7684\u9075\u5faa\u4e0a\u8ff0\u539f\u5219\u3002 \u6cd5\u5219 \u5f53\u57fa\u4e8e\u5df2\u6709\u7684\u4ee3\u7801\u8fdb\u884c\u5f00\u53d1\u65f6\uff0c\u8bf7\u4f7f\u7528\u8be5\u4ee3\u7801\u7684\u547d\u540d\u7ea6\u5b9a\uff08\u65e2\u662f\u5b83\u7684\u98ce\u683c\u4e0d\u7b26\u5408\u6700\u4f73\u5b9e\u8df5\uff09\u3002\u5f53\u4f60\u7f16\u5199\u4e00\u4e2a\u65b0\u7684\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u8bf7\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\u3002 \u5176\u6b21\uff0c\u547d\u540d\u65f6\u5e94\u5f53\u907f\u514d\u4ee5\u4e0b\u5212\u7ebf\u5f00\u5934\uff0c\u4e0b\u5212\u7ebf\u5f00\u5934\u7684\u540d\u79f0\u5e38\u5e38\u88ab\u64cd\u4f5c\u7cfb\u7edf\u3001\u5e93\u6216\u7f16\u8bd1\u5668\u9884\u7559\u4f7f\u7528\u3002 \u7b2c\u4e09\uff0c\u547d\u540d\u5e94\u5f53\u80fd\u591f\u8868\u660e\u8be5\u53d8\u91cf\u5b58\u50a8\u6570\u636e\u7684\u542b\u4e49\uff08\u5c24\u5176\u662f\u5f53\u8ba1\u91cf\u5355\u4f4d\u4e0d\u660e\u663e\u7684\u65f6\u5019\uff09\u3002\u53d8\u91cf\u540d\u5e94\u8be5\u80fd\u591f\u5e2e\u52a9\u5176\u4ed6\u5bf9\u4f60\u4ee3\u7801\u4e0d\u751a\u4e86\u89e3\u7684\u5f00\u53d1\u8005\u5feb\u901f\u7406\u89e3\u5176\u7528\u9014\u3002\u751a\u81f3\uff0c\u5f53\u4f60\u5728\u4e09\u4e2a\u6708\u4e4b\u540e\u518d\u770b\u81ea\u5df1\u7684\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u4f60\u90fd\u6709\u53ef\u80fd\u770b\u4e0d\u61c2\u81ea\u5df1\u7684\u4ee3\u7801\uff0c\u90a3\u65f6\u5019\u4f60\u5c31\u4f1a\u5e86\u5e78\u81ea\u5df1\u8d77\u4e86\u6613\u4e8e\u7406\u89e3\u3001\u542b\u4e49\u6e05\u6670\u7684\u53d8\u91cf\u540d\u3002 \u4e0d\u8fc7\uff0c\u4e3a\u4e00\u4e9b\u65e0\u5173\u7d27\u8981\u7684\u53d8\u91cf\u53d6\u4e00\u4e2a\u8fc7\u5206\u590d\u6742\u7684\u540d\u79f0\u53cd\u5012\u6709\u788d\u4e8e\u7406\u89e3\uff0c\u5176\u5371\u5bb3\u4e0d\u4e9a\u4e8e\u7ed9\u91cd\u8981\u7684\u53d8\u91cf\u53d6\u4e00\u4e2a\u610f\u4e49\u6a21\u7cca\u7684\u540d\u5b57\u3002\u56e0\u6b64\uff0c\u547d\u540d\u7684\u4e00\u4e2a\u539f\u5219\u53ef\u4ee5\u662f\uff0c\u8ba9\u53d8\u91cf\u540d\u7684\u957f\u5ea6\u7b26\u5408\u5176\u88ab\u4f7f\u7528\u7684\u9891\u5ea6\u3002\u65e0\u5173\u7d27\u8981\u7684\u53d8\u91cf\u540d\u5e94\u8be5\u4f7f\u7528\u66f4\u77ed\u7684\u53d8\u91cf\u540d\uff08\u4f8b\u5982 i \uff09\u3002\u88ab\u5e7f\u6cdb\u4f7f\u7528\u7684\u53d8\u91cf\uff08\u4f8b\u5982\u67d0\u4e2a\u5728\u591a\u5904\u88ab\u8c03\u7528\u7684\u51fd\u6570\uff09\u5219\u503c\u5f97\u62e5\u6709\u4e00\u4e2a\u66f4\u957f\u3001\u66f4\u5177\u89e3\u91ca\u6027\u7684\u53d8\u91cf\u540d\uff08\u4f8b\u5982\uff0c openFileOnDisk \u4f1a\u6bd4 open \u66f4\u5408\u9002\uff09\u3002 \u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\uff0c\u90fd\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u7f29\u5199\u3002\u5c3d\u7ba1\u4f7f\u7528\u7f29\u5199\u53ef\u4ee5\u7a0d\u5fae\u63d0\u9ad8\u4f60\u6572\u4ee3\u7801\u7684\u901f\u5ea6\uff0c\u5b83\u4f1a\u5bfc\u81f4\u4f60\u7684\u4ee3\u7801\u96be\u4ee5\u88ab\u7406\u89e3\u3002\u65e2\u662f\u6709\u4e9b\u7f29\u5199\u5177\u6709\u660e\u786e\u7684\u542b\u4e49\uff0c\u4f46\u8bfb\u8005\u5728\u9047\u5230\u5b83\u7684\u65f6\u5019\u4ecd\u7136\u9700\u8981\u82b1\u4e9b\u65f6\u95f4\u6765\u641e\u6e05\u695a\u5b83\u7684\u542b\u4e49\u3002\u4ee3\u7801\u88ab\u9605\u8bfb\u7684\u65f6\u95f4\u8fdc\u957f\u4e8e\u5b83\u88ab\u7f16\u5199\u7684\u65f6\u95f4\uff0c\u4f60\u5728\u5199\u4ee3\u7801\u4e0a\u8282\u7701\u4e0b\u4e86\u7684\u65f6\u95f4\uff0c\u5c06\u6765\u90fd\u4f1a\u6d6a\u8d39\u5728\u5176\u4ed6\u9605\u8bfb\u8005\uff08\u751a\u81f3\u662f\u4f60\u81ea\u5df1\uff09\u7684\u8eab\u4e0a\u3002\u5982\u679c\u4f60\u5e0c\u671b\u52a0\u5feb\u7f16\u5199\u4ee3\u7801\u7684\u901f\u5ea6\uff0c\u8bf7\u4f7f\u7528\u7f16\u8f91\u5668\u7684\u81ea\u52a8\u8865\u5168\u529f\u80fd\u3002 \u6700\u540e\uff0c\u6e05\u6670\u660e\u786e\u7684\u6ce8\u91ca\u4e5f\u5927\u6709\u52a9\u76ca\u3002\u4f8b\u5982\uff0c\u53d8\u91cf numberOfChars \u7528\u4e8e\u5b58\u653e\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u7684\u4e2a\u6570\u3002\u4f46\u662f\uff0c\u5b57\u7b26\u4e32\u201cHello World!\u201d\u4e2d\u7a76\u7adf\u5305\u542b\u51e0\u4e2a\u5b57\u7b26\u5462\uff1f10\u4e2a\uff0c11\u4e2a\u8fd8\u662f12\u4e2a\uff1f\u8fd9\u53d6\u51b3\u4e8e\u4f60\u662f\u5426\u8003\u8651\u7a7a\u683c\u548c\u6807\u70b9\u7b26\u53f7\u3002\u4f46\u662f\uff0c\u5c06\u8be5\u53d8\u91cf\u547d\u540d\u4e3a numberOfCharsIncludingWhitespaceAndPunctuation \u5c31\u592a\u957f\u4e86\uff0c\u6700\u597d\u7684\u65b9\u5f0f\u662f\u5728\u5b9a\u4e49\u53d8\u91cf\u65f6\u4f7f\u7528\u6ce8\u91ca\u7684\u65b9\u5f0f\u5e2e\u52a9\u8bfb\u8005\u53bb\u7406\u89e3\u3002 1 2 // holds number of chars in a piece of text -- including whitespace and punctuation! int numberOfChars ;","title":"1.7 - \u5173\u952e\u5b57\u548c\u6807\u8bc6\u7b26"},{"location":"1-7-Keywords-and-naming-identifiers/#_1","text":"C++ \u9884\u7559\u4e86 92 \u4e2a\u8bcd\u81ea\u7528(\u5bf9\u4e8e C++20 \u800c\u8a00) \u3002\u8fd9\u4e9b\u8bcd\u79f0\u4e3a \u5173\u952e\u5b57 (\u6216\u4fdd\u7559\u5b57)\uff0c\u6bcf\u4e2a\u5173\u952e\u5b57\u5728C++\u4e2d\u90fd\u6709\u5176\u7279\u5b9a\u7684\u542b\u4e49\u3002 \u4e0b\u8868\u5217\u4e3e\u4e86 C++ 20 \u4e2d\u7684\u5168\u90e8\u5173\u952e\u5b57\uff1a \u5173\u952e\u5b57\u540e\u6709\u6807\u8bc6 C++20 \u7684\uff0c\u5c5e\u4e8e C++20 \u65b0\u589e\u7684\u5173\u952e\u5b57\u3002\u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u4e0d\u652f\u6301 C++ 20\uff08\u6216\u8005\u5176\u5177\u5907 C++20 \u529f\u80fd\u4f46\u9ed8\u8ba4\u662f\u5173\u95ed\u7684\uff09\uff0c\u8fd9\u4e9b\u5173\u952e\u5b57\u5c06\u4e0d\u5177\u5907\u5b9e\u9645\u7528\u9014\u3002 C++ \u8fd8\u5b9a\u4e49\u4e86\u4e00\u4e9b\u7279\u6b8a\u7684\u6807\u8bc6\u7b26\uff1a override \u3001 final \u3001 import \u548c module \u3002\u8fd9\u4e9b\u6807\u8bc6\u7b26\u5728\u7279\u5b9a\u4e0a\u4e0b\u6587\u4e2d\u5177\u6709\u7279\u6b8a\u7684\u542b\u4e49\uff0c\u4f46\u5b83\u4eec\u5e76\u4e0d\u662fC++\u4fdd\u7559\u5b57\u3002 \u4f60\u5df2\u7ecf\u63a5\u89e6\u8fc7\u5176\u4e2d\u7684\u4e00\u4e9b\u5173\u952e\u5b57\uff0c\u5305\u62ec int \u548c return \u3002\u8fd9\u4e9b\u5173\u952e\u5b57\u3001\u6807\u8bc6\u7b26\u4ee5\u53ca\u4e00\u7ec4\u8fd0\u7b97\u7b26\u5c31\u6784\u6210\u4e86 C++ \u8bed\u8a00\u7684\u5168\u90e8\uff08\u9884\u5904\u7406\u5668\u6307\u4ee4\u9664\u5916\uff09\u3002\u7531\u4e8e\u5173\u952e\u5b57\u548c\u7279\u6b8a\u7684\u6807\u8bc6\u7b26\u5177\u6709\u7279\u6b8a\u542b\u4e49\uff0c\u56e0\u6b64\u4f60\u7684 IDE \u901a\u5e38\u4f1a\u5c06\u5176\u6807\u8bb0\u4e3a\u4e0d\u540c\u7684\u989c\u8272\u4ee5\u4f7f\u5176\u66f4\u52a0\u9192\u76ee\u3002 \u5728\u5b8c\u6210\u4e86\u672c\u6559\u7a0b\u7684\u5b66\u4e60\u540e\uff0c\u4f60\u4f1a\u7406\u89e3\u4e0a\u8ff0\u5173\u952e\u5b57\u4e2d\u51e0\u4e4e\u5168\u90e8\u7684\u529f\u80fd\uff01","title":"\u5173\u952e\u5b57"},{"location":"1-7-Keywords-and-naming-identifiers/#_2","text":"\u63d0\u9192\u4e00\u4e0b\uff0c\u53d8\u91cf\uff08\u6216\u51fd\u6570\u3001\u7c7b\u578b\u4ee5\u53ca\u5176\u4ed6\u4efb\u4f55\u5143\u7d20\uff09\u7684\u540d\u79f0\u90fd\u79f0\u4e3a\u6807\u8bc6\u7b26\u3002C++ \u5141\u8bb8\u4f60\u7075\u6d3b\u5730\u9009\u62e9\u6807\u8bc6\u7b26\u3002\u4e0d\u8fc7\uff0c\u5728\u9009\u62e9\u547d\u540d\u6807\u8bc6\u7b26\u65f6\uff0c\u4f60\u9700\u8981\u9075\u5faa\u4ee5\u4e0b\u89c4\u5219\uff1a \u6807\u8bc6\u7b26\u4e0d\u80fd\u662f\u5173\u952e\u5b57\uff0c\u5173\u952e\u5b57\u662f\u8bed\u8a00\u7684\u4fdd\u7559\u5b57\uff1b \u6807\u8bc6\u7b26\u53ea\u80fd\u4f7f\u7528\u5927\u5c0f\u5199\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6807\u8bc6\u7b26\u4e2d\u4e0d\u80fd\u5305\u542b\u7b26\u53f7\uff08\u9664\u4e86\u4e0b\u5212\u7ebf\uff09\u6216\u8005\u7a7a\u767d\u7b26\uff08\u7a7a\u683c\u6216\u5236\u8868\u7b26\uff09\uff1b \u6807\u8bc6\u7b26\u5fc5\u987b\u4ee5\u5b57\u6bcd\uff08\u5927\u5c0f\u5199\u5747\u53ef\uff09\u6216\u4e0b\u5212\u7ebf\u5f00\u5934\uff0c\u4e0d\u80fd\u591f\u4ee5\u6570\u5b57\u5f00\u5934\uff1b C++ \u662f\u5927\u5c0f\u5199\u654f\u611f\u7684\uff0c\u56e0\u6b64\u5b83\u4f1a\u533a\u5206\u5927\u5c0f\u5199\u5b57\u6bcd\u3002 nvalue \u548c nValue \u662f\u4e0d\u540c\u7684\uff0c\u548c NVALUE \u4e5f\u4e0d\u540c\uff0c","title":"\u547d\u540d\u89c4\u5219"},{"location":"1-7-Keywords-and-naming-identifiers/#_3","text":"\u73b0\u5728\uff0c\u60f3\u5fc5\u4f60\u5df2\u7ecf\u77e5\u9053 \u5982\u4f55 \u4e3a\u53d8\u91cf\u547d\u540d\u4e86\uff0c\u63a5\u4e0b\u6765\u8ba9\u6211\u4eec\u804a\u804a\uff0c\u4f60 \u5e94\u8be5\u5982\u4f55 \u4e3a\u53d8\u91cf\uff08\u6216\u51fd\u6570\uff09\u547d\u540d\u3002 \u9996\u5148\uff0cC++ \u4e2d\u7ea6\u5b9a\u4fd7\u6210\u7684\u662f\uff0c\u53d8\u91cf\u540d\u5e94\u8be5\u4ee5\u5c0f\u5199\u5b57\u6bcd\u5f00\u5934\u3002\u5982\u679c\u53d8\u91cf\u540d\u662f\u4e00\u4e2a\u5355\u8bcd\uff0c\u5219\u6574\u4e2a\u5355\u8bcd\u90fd\u5e94\u8be5\u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd\u3002 1 2 3 4 5 int value ; // correct int Value ; // incorrect (should start with lower case letter) int VALUE ; // incorrect (should start with lower case letter) int VaLuE ; // incorrect (see your psychiatrist) ;) \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u51fd\u6570\u540d\u540c\u6837\u4e5f\u662f\u5c0f\u5199\u5b57\u6bcd\u5f00\u5934\u7684\uff08\u5c3d\u7ba1\u5728\u8fd9\u4e00\u70b9\u4e0a\u6709\u5f88\u591a\u4e0d\u540c\u610f\u89c1\uff09\u3002\u6211\u4eec\u4f1a\u9075\u5faa\u8fd9\u4e00\u60ef\u4f8b\uff0c\u56e0\u4e3a main \u51fd\u6570\u5c31\u662f\u5c0f\u5199\u5b57\u6bcd\u5f00\u5934\u7684\uff08\u800c\u4e14 main \u51fd\u6570\u662f\u4efb\u4f55\u7a0b\u5e8f\u90fd\u9700\u8981\u6709\u7684\uff09\u3002\u6b64\u5916\uff0cC++\u6807\u51c6\u5e93\u4e2d\u7684\u6240\u6709\u51fd\u6570\u4e5f\u90fd\u9075\u5faa\u8fd9\u4e00\u539f\u5219\u3002 \u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684\u547d\u540d\u6807\u8bc6\u7b26\u901a\u5e38\u7528\u4e8e\u547d\u540d\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u578b\uff08\u4f8b\u5982\u7ed3\u6784\u4f53\u3001\u7c7b\u548c\u679a\u4e3e\uff0c\u7a0d\u540e\u6211\u4eec\u4f1a\u4ecb\u7ecd\u8fd9\u4e9b\u7c7b\u578b\uff09\u3002 \u5982\u679c\u53d8\u91cf\u540d\u6216\u51fd\u6570\u540d\u7531\u591a\u4e2a\u5355\u8bcd\u7ec4\u6210\uff0c\u5e38\u89c1\u7684\u547d\u540d\u60ef\u4f8b\u6709\u4e24\u79cd\uff0c\u4e00\u79cd\u662f\u4f7f\u7528\u4e0b\u5212\u7ebf\u5206\u5272\u5355\u8bcd\uff0c\u79f0\u4e3a \u86c7\u5f62\u547d\u540d\u6cd5(snake case) \uff0c\u53e6\u5916\u4e00\u79cd\u662f\u4f7f\u7528\u5927\u5c0f\u5199\u95f4\u9694\u7684\u65b9\u6cd5\uff08\u6709\u65f6\u5019\u79f0\u4e3a\u9a7c\u5cf0\u547d\u540d\u6cd5\uff0c\u56e0\u4e3a\u5927\u5199\u5b57\u6bcd\u6bd4\u8f83\u7a81\u51fa\uff0c\u5c31\u50cf\u9a86\u9a7c\u7684\u9a7c\u5cf0\u4e00\u6837\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 int my_variable_name ; // correct (separated by underscores/snake_case) int my_function_name (); // correct (separated by underscores/snake_case) int myVariableName ; // correct (intercapped/CamelCase) int myFunctionName (); // correct (intercapped/CamelCase) int my variable name ; // invalid (whitespace not allowed) int my function name (); // invalid (whitespace not allowed) int MyVariableName ; // valid but incorrect (should start with lower case letter) int MyFunctionName (); // valid but incorrect (should start with lower case letter) \u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528\u9a7c\u5cf0\u547d\u540d\u6cd5\uff0c\u56e0\u4e3a\u5b83\u53ef\u8bfb\u6027\u66f4\u597d\uff08\u5bf9\u4e8e\u86c7\u5f62\u547d\u540d\u6cd5\u6765\u8bf4\uff0c\u6709\u65f6\u4e0b\u5212\u7ebf\u5bb9\u6613\u548c\u7a7a\u683c\u6df7\u6dc6\uff09\u3002\u4f46\u662f\uff0c\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u5f88\u5e38\u89c1\uff0cC++ \u7684\u6807\u51c6\u5e93\u5728\u547d\u540d\u53d8\u91cf\u548c\u51fd\u6570\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u7684\u5c31\u662f\u57fa\u4e8e\u4e0b\u5212\u7ebf\u7684\u86c7\u5f62\u547d\u540d\u6cd5\u3002\u6709\u65f6\u5019\uff0c\u4f60\u751a\u81f3\u4f1a\u9047\u5230\u6df7\u7528\u4e24\u79cd\u547d\u540d\u6cd5\u7684\u60c5\u51b5\uff0c\u86c7\u5f62\u547d\u540d\u6cd5\u7528\u4e8e\u53d8\u91cf\u540d\u800c\u9a7c\u5cf0\u547d\u540d\u6cd5\u5219\u7528\u4e8e\u51fd\u6570\u540d\u3002 \u5982\u679c\u4f60\u7684\u5de5\u4f5c\u662f\u5728\u5176\u4ed6\u4eba\u7684\u4ee3\u7801\u4e0a\u8fdb\u884c\u5f00\u53d1\uff0c\u90a3\u4e48\u8fd9\u4e9b\u5c31\u4e0d\u91cd\u8981\u4e86\u3002\u56e0\u4e3a\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6700\u597d\u7684\u505a\u6cd5\u662f\u9075\u5faa\u4ed6\u4eba\u7684\u547d\u540d\u89c4\u5219\u800c\u4e0d\u662f\u6b7b\u677f\u7684\u9075\u5faa\u4e0a\u8ff0\u539f\u5219\u3002 \u6cd5\u5219 \u5f53\u57fa\u4e8e\u5df2\u6709\u7684\u4ee3\u7801\u8fdb\u884c\u5f00\u53d1\u65f6\uff0c\u8bf7\u4f7f\u7528\u8be5\u4ee3\u7801\u7684\u547d\u540d\u7ea6\u5b9a\uff08\u65e2\u662f\u5b83\u7684\u98ce\u683c\u4e0d\u7b26\u5408\u6700\u4f73\u5b9e\u8df5\uff09\u3002\u5f53\u4f60\u7f16\u5199\u4e00\u4e2a\u65b0\u7684\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u8bf7\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\u3002 \u5176\u6b21\uff0c\u547d\u540d\u65f6\u5e94\u5f53\u907f\u514d\u4ee5\u4e0b\u5212\u7ebf\u5f00\u5934\uff0c\u4e0b\u5212\u7ebf\u5f00\u5934\u7684\u540d\u79f0\u5e38\u5e38\u88ab\u64cd\u4f5c\u7cfb\u7edf\u3001\u5e93\u6216\u7f16\u8bd1\u5668\u9884\u7559\u4f7f\u7528\u3002 \u7b2c\u4e09\uff0c\u547d\u540d\u5e94\u5f53\u80fd\u591f\u8868\u660e\u8be5\u53d8\u91cf\u5b58\u50a8\u6570\u636e\u7684\u542b\u4e49\uff08\u5c24\u5176\u662f\u5f53\u8ba1\u91cf\u5355\u4f4d\u4e0d\u660e\u663e\u7684\u65f6\u5019\uff09\u3002\u53d8\u91cf\u540d\u5e94\u8be5\u80fd\u591f\u5e2e\u52a9\u5176\u4ed6\u5bf9\u4f60\u4ee3\u7801\u4e0d\u751a\u4e86\u89e3\u7684\u5f00\u53d1\u8005\u5feb\u901f\u7406\u89e3\u5176\u7528\u9014\u3002\u751a\u81f3\uff0c\u5f53\u4f60\u5728\u4e09\u4e2a\u6708\u4e4b\u540e\u518d\u770b\u81ea\u5df1\u7684\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u4f60\u90fd\u6709\u53ef\u80fd\u770b\u4e0d\u61c2\u81ea\u5df1\u7684\u4ee3\u7801\uff0c\u90a3\u65f6\u5019\u4f60\u5c31\u4f1a\u5e86\u5e78\u81ea\u5df1\u8d77\u4e86\u6613\u4e8e\u7406\u89e3\u3001\u542b\u4e49\u6e05\u6670\u7684\u53d8\u91cf\u540d\u3002 \u4e0d\u8fc7\uff0c\u4e3a\u4e00\u4e9b\u65e0\u5173\u7d27\u8981\u7684\u53d8\u91cf\u53d6\u4e00\u4e2a\u8fc7\u5206\u590d\u6742\u7684\u540d\u79f0\u53cd\u5012\u6709\u788d\u4e8e\u7406\u89e3\uff0c\u5176\u5371\u5bb3\u4e0d\u4e9a\u4e8e\u7ed9\u91cd\u8981\u7684\u53d8\u91cf\u53d6\u4e00\u4e2a\u610f\u4e49\u6a21\u7cca\u7684\u540d\u5b57\u3002\u56e0\u6b64\uff0c\u547d\u540d\u7684\u4e00\u4e2a\u539f\u5219\u53ef\u4ee5\u662f\uff0c\u8ba9\u53d8\u91cf\u540d\u7684\u957f\u5ea6\u7b26\u5408\u5176\u88ab\u4f7f\u7528\u7684\u9891\u5ea6\u3002\u65e0\u5173\u7d27\u8981\u7684\u53d8\u91cf\u540d\u5e94\u8be5\u4f7f\u7528\u66f4\u77ed\u7684\u53d8\u91cf\u540d\uff08\u4f8b\u5982 i \uff09\u3002\u88ab\u5e7f\u6cdb\u4f7f\u7528\u7684\u53d8\u91cf\uff08\u4f8b\u5982\u67d0\u4e2a\u5728\u591a\u5904\u88ab\u8c03\u7528\u7684\u51fd\u6570\uff09\u5219\u503c\u5f97\u62e5\u6709\u4e00\u4e2a\u66f4\u957f\u3001\u66f4\u5177\u89e3\u91ca\u6027\u7684\u53d8\u91cf\u540d\uff08\u4f8b\u5982\uff0c openFileOnDisk \u4f1a\u6bd4 open \u66f4\u5408\u9002\uff09\u3002 \u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\uff0c\u90fd\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u7f29\u5199\u3002\u5c3d\u7ba1\u4f7f\u7528\u7f29\u5199\u53ef\u4ee5\u7a0d\u5fae\u63d0\u9ad8\u4f60\u6572\u4ee3\u7801\u7684\u901f\u5ea6\uff0c\u5b83\u4f1a\u5bfc\u81f4\u4f60\u7684\u4ee3\u7801\u96be\u4ee5\u88ab\u7406\u89e3\u3002\u65e2\u662f\u6709\u4e9b\u7f29\u5199\u5177\u6709\u660e\u786e\u7684\u542b\u4e49\uff0c\u4f46\u8bfb\u8005\u5728\u9047\u5230\u5b83\u7684\u65f6\u5019\u4ecd\u7136\u9700\u8981\u82b1\u4e9b\u65f6\u95f4\u6765\u641e\u6e05\u695a\u5b83\u7684\u542b\u4e49\u3002\u4ee3\u7801\u88ab\u9605\u8bfb\u7684\u65f6\u95f4\u8fdc\u957f\u4e8e\u5b83\u88ab\u7f16\u5199\u7684\u65f6\u95f4\uff0c\u4f60\u5728\u5199\u4ee3\u7801\u4e0a\u8282\u7701\u4e0b\u4e86\u7684\u65f6\u95f4\uff0c\u5c06\u6765\u90fd\u4f1a\u6d6a\u8d39\u5728\u5176\u4ed6\u9605\u8bfb\u8005\uff08\u751a\u81f3\u662f\u4f60\u81ea\u5df1\uff09\u7684\u8eab\u4e0a\u3002\u5982\u679c\u4f60\u5e0c\u671b\u52a0\u5feb\u7f16\u5199\u4ee3\u7801\u7684\u901f\u5ea6\uff0c\u8bf7\u4f7f\u7528\u7f16\u8f91\u5668\u7684\u81ea\u52a8\u8865\u5168\u529f\u80fd\u3002 \u6700\u540e\uff0c\u6e05\u6670\u660e\u786e\u7684\u6ce8\u91ca\u4e5f\u5927\u6709\u52a9\u76ca\u3002\u4f8b\u5982\uff0c\u53d8\u91cf numberOfChars \u7528\u4e8e\u5b58\u653e\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u7684\u4e2a\u6570\u3002\u4f46\u662f\uff0c\u5b57\u7b26\u4e32\u201cHello World!\u201d\u4e2d\u7a76\u7adf\u5305\u542b\u51e0\u4e2a\u5b57\u7b26\u5462\uff1f10\u4e2a\uff0c11\u4e2a\u8fd8\u662f12\u4e2a\uff1f\u8fd9\u53d6\u51b3\u4e8e\u4f60\u662f\u5426\u8003\u8651\u7a7a\u683c\u548c\u6807\u70b9\u7b26\u53f7\u3002\u4f46\u662f\uff0c\u5c06\u8be5\u53d8\u91cf\u547d\u540d\u4e3a numberOfCharsIncludingWhitespaceAndPunctuation \u5c31\u592a\u957f\u4e86\uff0c\u6700\u597d\u7684\u65b9\u5f0f\u662f\u5728\u5b9a\u4e49\u53d8\u91cf\u65f6\u4f7f\u7528\u6ce8\u91ca\u7684\u65b9\u5f0f\u5e2e\u52a9\u8bfb\u8005\u53bb\u7406\u89e3\u3002 1 2 // holds number of chars in a piece of text -- including whitespace and punctuation! int numberOfChars ;","title":"\u547d\u540d\u6700\u4f73\u5b9e\u8df5"},{"location":"1-8-Whitespace-and-basic-formatting/","tags":["formatting"],"text":"\u7a7a\u767d \u8fd9\u4e2a\u672f\u8bed\uff0c\u6307\u7684\u662f\u90a3\u4e9b\u51fa\u4e8e\u6392\u7248\u76ee\u7684\u800c\u4f7f\u7528\u7684\u7a7a\u767d\u5b57\u7b26\u3002\u5728 C++ \u4e2d\uff0c\u7a7a\u767d\u6307\u7684\u662f\u7a7a\u683c\u7b26\u3001\u5236\u8868\u7b26\u548c\u6362\u884c\u7b26\u3002C++ \u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u5ffd\u7565\u7a7a\u767d\uff08\u5904\u7406\u6587\u672c\u5b57\u9762\u91cf\u65f6\u662f\u4e00\u4e2a\u7279\u4f8b\uff09\u3002\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u8bf4 C++ \u662f\u4e00\u4e2a\u7a7a\u683c\u65e0\u5173\uff08whitespace-independent\uff09\u7684\u8bed\u8a00\u3002 \u56e0\u6b64\uff0c\u4e0b\u9762\u51e0\u884c\u4ee3\u7801\u7684\u6548\u679c\u5176\u5b9e\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 std :: cout << \"Hello world!\" ; std :: cout << \"Hello world!\" ; std :: cout << \"Hello world!\" ; std :: cout << \"Hello world!\" ; \u6700\u540e\u4e00\u4e2a\u8bed\u53e5\u5373\u4f7f\u88ab\u5206\u5272\u4e3a\u4e24\u884c\uff0c\u5b83\u4e5f\u80fd\u591f\u6b63\u5e38\u7f16\u8bd1\u3002 \u4e0b\u9762\u7684\u51fd\u6570\u6548\u679c\u4e5f\u5b8c\u5168\u4e00\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int add ( int x , int y ) { return x + y ; } int add ( int x , int y ) { return x + y ; } int add ( int x , int y ) { return x + y ; } int add ( int x , int y ) { return x + y ; } \u4e00\u4e2a\u4f8b\u5916\u662f\uff0cC++\u7f16\u8bd1\u5668\u5e76\u4e0d\u4f1a\u5ffd\u7565\u5f15\u53f7\u5305\u88f9\u7684\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u767d\uff0c\u4f8b\u5982 \"Hello world!\" \u3002 1 \"Hello world!\" \u4e0a\u9762\u7684\u5b57\u7b26\u4e32\u548c\u4e0b\u9762\u7684\u5b57\u7b26\u4e32\u662f\u4e0d\u4e00\u6837\u7684\u3002 1 \"Hello world!\" \u4e24\u4e2a\u5b57\u7b26\u4e32\u90fd\u4f1a\u88ab\u539f\u5c01\u4e0d\u52a8\u7684\u6253\u5370\u5904\u7406\u3002 \u5728\u5f15\u53f7\u5305\u88f9\u7684\u5b57\u7b26\u4e32\u4e2d\uff0c\u6362\u884c\u662f\u4e0d\u5408\u6cd5\u7684\uff1a 1 2 std :: cout << \"Hello world ! \"; // Not allowed! \u53ea\u4f7f\u7528\u7a7a\u767d\u5b57\u7b26\uff08\u7a7a\u683c\u3001\u6362\u884c\u548c\u56de\u8f66\uff09\u5206\u5272\u7684\u6587\u672c\uff0c\u6700\u7ec8\u5c06\u4f1a\u88ab\u8fde\u63a5\u5728\u4e00\u8d77\uff1a 1 2 std :: cout << \"Hello \" \"world!\" ; // prints \"Hello world!\" \u53e6\u5916\u4e00\u4e2a\u7279\u4f8b\u662f\uff0cC++ \u7684\u7f16\u8bd1\u5668\u4f1a\u5173\u6ce8\u5355\u884c\u6ce8\u91ca\uff08 // \uff09\u4e2d\u7684\u7a7a\u767d\u7b26\u3002\u5355\u884c\u6ce8\u91ca\u53ea\u4f1a\u5230\u8be5\u884c\u672b\u5c3e\u622a\u6b62\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u6709\u95ee\u9898\u7684\uff1a 1 2 std :: cout << \"Hello world!\" ; // Here is a single-line comment this is not part of the comment \u57fa\u672c\u4ee3\u7801\u683c\u5f0f \u548c\u5176\u4ed6\u8bed\u8a00\u4e0d\u540c\uff0cC++ \u5e76\u4e0d\u4f1a\u5f3a\u5236\u7a0b\u5e8f\u5458\u4f7f\u7528\u67d0\u79cd\u6392\u7248\u98ce\u683c\uff08\u8fd8\u8bb0\u5f97\u5417\uff1f\u76f8\u4fe1\u7a0b\u5e8f\u5458\uff01\uff09\u3002C++ \u4e2d\u6709\u5f88\u591a\u53ef\u4ee5\u4f7f\u7528\u7684\u6392\u7248\u65b9\u5f0f\uff0c\u5173\u4e8e\u54ea\u4e00\u79cd\u624d\u662f\u6700\u4f73\u7684\u65b9\u5f0f\uff0c\u4f60\u53ef\u80fd\u4f1a\u6709\u4e0d\u540c\u7684\u770b\u6cd5\u3002\u6211\u4eec\u7684\u57fa\u672c\u539f\u5219\u662f\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\u6700\u5f3a\u3001\u4e00\u6267\u884c\u6700\u597d\u7684\u90a3\u79cd\u6392\u7248\u98ce\u683c\uff0c\u5c31\u662f\u6700\u597d\u7684\u3002 \u672c\u6559\u7a0b\u5efa\u8bae\u7684\u6392\u7248\u89c4\u8303\u5982\u4e0b\uff1a 1.\u4f7f\u7528\u5236\u8868\u7b26\u548c\u7a7a\u683c\u8fdb\u884c\u7f29\u8fdb\u90fd\u662f\u5141\u8bb8\u7684 (\u5927\u591a\u6570\u7684 IDE \u90fd\u53ef\u4ee5\u8bbe\u7f6e\u81ea\u52a8\u5c06\u5236\u8868\u7b26\u8f6c\u6362\u4e3a\u89c4\u5b9a\u4e2a\u6570\u7684\u7a7a\u683c\uff09\u3002\u5f00\u53d1\u8005\u4f7f\u7528\u7a7a\u683c\u8fdb\u884c\u6392\u7248\u5c24\u5176\u5185\u5728\u7684\u903b\u8f91\uff0c\u6bd5\u7adf\u4f7f\u7528\u201d\u7a7a\u683c\u201c\u6765\u521b\u5efa\u4ee3\u7801\u4e4b\u95f4\u7684\u201d\u7a7a\u767d\u201c\u662f\u5929\u7ecf\u5730\u4e49\u7684\u3002\u4f7f\u7528\u7a7a\u683c\u6dfb\u52a0\u7684\u7a7a\u767d\uff0c\u4e0d\u8bba\u5728\u4ec0\u4e48\u7f16\u8f91\u5668\u4e0b\uff0c\u770b\u8d77\u6765\u90fd\u662f\u4e00\u81f4\u7684\u3002\u800c\u4f7f\u7528\u5236\u8868\u7b26\u8fdb\u884c\u7f29\u8fdb\u7684\u7a0b\u5e8f\u5458\u4f1a\u5bf9\u4e0a\u8ff0\u884c\u4e3a\u611f\u5230\u56f0\u60d1\uff0c\u5236\u8868\u7b26\u5c31\u662f\u4e3a\u4e86\u7f29\u8fdb\u800c\u5b58\u5728\u7684\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u7528\u5462\uff1f\u66f4\u4f55\u51b5\u4f60\u8fd8\u53ef\u4ee5\u81ea\u7531\u63a7\u5236\u5176\u7f29\u8fdb\u7684\u5bbd\u5ea6\u3002\u8fd9\u4e2a\u95ee\u9898\u6ca1\u6709\u6240\u8c13\u201d\u6b63\u786e\u201c\u7684\u7b54\u6848\uff0c\u4e89\u8bba\u5b83\u7684\u5bf9\u9519\u662f\u6ca1\u6709\u610f\u4e49\u7684\uff0c\u5c31\u597d\u6bd4\u4e89\u8bba\u7cbd\u5b50\u5e94\u8be5\u662f\u751c\u7684\u8fd8\u662f\u54b8\u7684\uff0c\u56e0\u4e3a\u8fd9\u5b8c\u5168\u53d6\u51b3\u4e8e\u4f60\u81ea\u5df1\u7684\u53e3\u5473\u3002 \u4e0d\u8bba\u90a3\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u5efa\u8bae\u4f60\u4f7f\u75284\u4e2a\u7a7a\u683c\u7684\u5bbd\u5ea6\u4f5c\u4e3a\u7f29\u8fdb\u7684\u5bbd\u5ea6\u3002\u6709\u4e9bIDE\u9ed8\u8ba4\u7684\u7f29\u8fdb\u5bbd\u5ea6\u662f3\u4e2a\u7a7a\u683c\uff0c\u5176\u5b9e\u4e5f\u662f\u53ef\u4ee5\u7684\u3002 2.\u51fd\u6570\u62ec\u53f7\u7684\u6392\u7248\u6709\u4e24\u79cd\u53ef\u4ee5\u63a5\u53d7\u7684\u65b9\u5f0f\u3002 Google C++ \u98ce\u683c\u6307\u5357\u5efa\u8bae\u7684\u98ce\u683c\u662f\u82b1\u62ec\u53f7\u4e0d\u6362\u884c\uff1a 1 2 int main () { } \u8fd9\u4e48\u505a\u7684\u4f9d\u636e\u662f\uff0c\u5b83\u80fd\u591f\u51cf\u5c11\u7eb5\u5411\u7a7a\u767d\u7684\u4ea7\u751f\uff08\u62ec\u53f7\u5355\u72ec\u5360\u7528\u4e00\u884c\u662f\u4e0d\u5212\u7b97\u7684\uff09\uff0c\u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u5728\u6709\u9650\u7684\u5c4f\u5e55\u4e2d\u663e\u793a\u66f4\u591a\u7684\u4ee3\u7801\uff0c\u8fd9\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u7684\u7406\u89e3\u4ee3\u7801\u3002 \u4e0d\u8fc7\uff0c\u6211\u4eec\u8fd8\u662f\u503e\u5411\u4e8e\u4f7f\u7528\u53e6\u5916\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u5373\u82b1\u62ec\u53f7\u53e6\u8d77\u4e00\u884c\uff1a 1 2 3 int main () { } \u8fd9\u4e48\u505a\u53ef\u4ee5\u589e\u5f3a\u53ef\u8bfb\u6027\uff0c\u56e0\u4e3a\u4e00\u5bf9\u82b1\u62ec\u53f7\u603b\u662f\u5177\u6709\u76f8\u540c\u7684\u7f29\u8fdb\uff0c\u8fd9\u4e48\u505a\u53ef\u4ee5\u51cf\u5c11\u72af\u9519\u7684\u51e0\u7387\u3002\u5982\u679c\u7f16\u8bd1\u5668\u62a5\u544a\u4e86\u62ec\u53f7\u4e0d\u5339\u914d\u7684\u9519\u8bef\uff0c\u90a3\u4f60\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u627e\u5230\u95ee\u9898\u6240\u5728\u3002 3.\u6bcf\u4e00\u5c42\u82b1\u62ec\u53f7\u4e2d\u7684\u8bed\u53e5\u90fd\u5fc5\u987b\u8fdb\u884c\u7f29\u8fdb\u4e00\u7ea7\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 int main () { std :: cout << \"Hello world! \\n \" ; // tabbed in one tab (4 spaces) std :: cout << \"Nice to meet you. \\n \" ; // tabbed in one tab (4 spaces) } 4.\u6bcf\u4e00\u884c\u4ee3\u7801\u90fd\u4e0d\u5e94\u8be5\u592a\u957f\u3002\u5e38\u89c1\u7684\u89c4\u5219\u662f\uff1a\u5355\u884c\u4e0d\u80fd\u8d85\u8fc780\u4e2a\u5b57\u7b26\u3002\u5982\u679c\u4e00\u884c\u4ee3\u7801\u592a\u957f\uff0c\u5b83\u5c31\u5e94\u5f53\u88ab\u5206\u5272\u4e3a\u591a\u884c\uff08\u5728\u5408\u9002\u7684\u4f4d\u7f6e\u5206\u5272\uff09\u3002\u5177\u4f53\u64cd\u4f5c\u65f6\u53ef\u4ee5\u5c06\u5404\u4e2a\u5b50\u884c\u989d\u5916\u7f29\u8fdb\u4e00\u7ea7\uff0c\u6216\u8005\uff0c\u5982\u679c\u5b50\u884c\u4e4b\u95f4\u975e\u5e38\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u5c06\u5b83\u4eec\u90fd\u5bf9\u9f50\u5230\u4e0a\u9762\u7684\u884c\uff08\u53ef\u8bfb\u6027\u66f4\u597d\uff09\u3002 1 2 3 4 5 6 7 8 9 10 int main () { std :: cout << \"This is a really, really, really, really, really, really, really, \" \"really long line \\n \" ; // one extra indentation for continuation line std :: cout << \"This is another really, really, really, really, really, really, really, \" \"really long line \\n \" ; // text aligned with the previous line for continuation line std :: cout << \"This one is short \\n \" ; } \u8fd9\u4e48\u505a\u53ef\u4ee5\u4f7f\u5f97\u6bcf\u884c\u4ee3\u7801\u90fd\u5177\u6709\u5f88\u597d\u7684\u53ef\u8bfb\u6027\u3002\u5728\u5bbd\u5c4f\u663e\u793a\u5668\u4e0a\uff0c\u8fd9\u4e48\u505a\u8fd8\u53ef\u4ee5\u5141\u8bb8\u4f60\u5c06\u4e24\u4e2a\u4ee3\u7801\u7a97\u53e3\u5e76\u6392\u663e\u793a\uff0c\u8f7b\u677e\u5730\u5bf9\u6bd4\u5b83\u4eec\u7684\u5dee\u5f02\u3002 \u6700\u4f73\u5b9e\u8df5 \u5355\u884c\u4ee3\u7801\u957f\u5ea6\u4e0d\u5e94\u8be5\u8d85\u8fc780\u4e2a\u5b57\u7b26\u3002 \u5c0f\u8d34\u58eb \u5f88\u591a\u7f16\u8f91\u5668\u90fd\u96c6\u6210\uff08\u6216\u901a\u8fc7\u63d2\u4ef6/\u6269\u5c55\uff09\u4e86 \u5217\u6307\u793a\u5668 \uff0c\u80fd\u591f\u663e\u793a\u51fa\u7279\u5b9a\u5217\u7684\u4f4d\u7f6e\uff08\u4f8b\u598280\u4e2a\u5b57\u7b26\u5904\uff09\u3002\u56e0\u6b64\u4f60\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5224\u65ad\u4f60\u7684\u4ee3\u7801\u884c\u662f\u4e0d\u662f\u8d85\u957f\u4e86\u3002\u641c\u7d22\u7f16\u8f91\u5668\u540d+\u201cColumn guide\u201d\u6765\u67e5\u8be2\u4f60\u7684\u7f16\u8f91\u5668\u662f\u5426\u652f\u6301\u8be5\u7279\u6027\u3002 \u8bd1\u6ce8\uff1a\u5728 VS Code \u4e2d\uff0c\u8fd9\u4e00\u7279\u6027\u53eb\u505a editor.rulers \uff0c\u53ef\u4ee5\u5728\u8bbe\u7f6e\u4e2d\u914d\u7f6e\u5176\u51fa\u73b0\u7684\u4f4d\u7f6e\uff0c\u4f8b\u5982 editor.rulers=[80] 5.\u5982\u679c\u4e00\u4e2a\u8f83\u957f\u7684\u884c\u88ab\u57fa\u4e8e\u8fd0\u7b97\u7b26\u5206\u5272\u4e3a\u4e86\u591a\u4e2a\u77ed\u884c\uff0c\u90a3\u4e48\u8fd0\u7b97\u7b26\u5e94\u8be5\u653e\u7f6e\u5728\u4e0b\u4e00\u884c\u7684\u5f00\u5934\u800c\u4e0d\u662f\u4e0a\u4e00\u884c\u7684\u7ed3\u5c3e\uff1a 1 2 3 std :: cout << 3 + 4 + 5 + 6 * 7 * 8 ; \u8fd9\u4e48\u505a\u53ef\u4ee5\u660e\u786e\u7684\u8868\u660e\u4e0b\u4e00\u884c\u662f\u627f\u63a5\u81ea\u4e0a\u4e00\u884c\u7684\uff0c\u800c\u4e14\u4f60\u53ef\u4ee5\u5c06\u8fd0\u7b97\u7b26\u5de6\u5bf9\u9f50\uff0c\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002 6.\u4f7f\u7528\u7a7a\u683c\u5c06\u503c\u3001\u6ce8\u91ca\u5bf9\u9f50\uff0c\u6216\u5728\u4ee3\u7801\u6bb5\u4e4b\u95f4\u6dfb\u52a0\u7a7a\u683c\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002 \u53ef\u8bfb\u6027\u4e0d\u4f73\uff1a 1 2 3 4 cost = 57 ; pricePerItem = 24 ; value = 5 ; numberOfItems = 17 ; \u53ef\u8bfb\u6027\u4f73\uff1a 1 2 3 4 cost = 57 ; pricePerItem = 24 ; value = 5 ; numberOfItems = 17 ; \u53ef\u8bfb\u6027\u4e0d\u4f73\uff1a 1 2 3 std :: cout << \"Hello world! \\n \" ; // cout lives in the iostream library std :: cout << \"It is very nice to meet you! \\n \" ; // these comments make the code hard to read std :: cout << \"Yeah! \\n \" ; // especially when lines are different lengths \u53ef\u8bfb\u6027\u4f73\uff1a 1 2 3 std :: cout << \"Hello world! \\n \" ; // cout lives in the iostream library std :: cout << \"It is very nice to meet you! \\n \" ; // these comments are easier to read std :: cout << \"Yeah! \\n \" ; // especially when all lined up \u53ef\u8bfb\u6027\u4e0d\u4f73\uff1a 1 2 3 4 5 6 // cout lives in the iostream library std :: cout << \"Hello world! \\n \" ; // these comments make the code hard to read std :: cout << \"It is very nice to meet you! \\n \" ; // especially when all bunched together std :: cout << \"Yeah! \\n \" ; \u53ef\u8bfb\u6027\u4f73\uff1a 1 2 3 4 5 6 7 8 // cout lives in the iostream library std :: cout << \"Hello world! \\n \" ; // these comments are easier to read std :: cout << \"It is very nice to meet you! \\n \" ; // when separated by whitespace std :: cout << \"Yeah! \\n \" ; \u672c\u6559\u7a0b\u5c06\u59cb\u7ec8\u8d2f\u5f7b\u4e0a\u8ff0\u6392\u7248\u7ea6\u5b9a\uff0c\u5b83\u4eec\u7ec8\u5c06\u4f1a\u79f0\u4e3a\u4f60\u4e60\u60ef\u7684\u4e00\u90e8\u5206\u3002\u5728\u6211\u4eec\u4ecb\u7ecd\u4fe1\u7684\u8bed\u8a00\u7279\u6027\u7684\u65f6\u5019\uff0c\u4e5f\u4f1a\u5bf9\u5e94\u5730\u4ecb\u7ecd\u5176\u9002\u7528\u7684\u4ee3\u7801\u6392\u7248\u98ce\u683c\u3002 C++ \u7ed9\u4e0e\u4e86\u4f60\u9009\u62e9\u4efb\u4f55\u4f60\u559c\u6b22\u7684\u98ce\u683c\u7684\u6743\u5229\uff0c\u4f46\u662f\uff0c\u6211\u4eec\u4ecd\u7136\u5f3a\u70c8\u63a8\u8350\u4f60\u4f7f\u7528\u4e0e\u6211\u4eec\u76f8\u540c\u7684\u4ee3\u7801\u98ce\u683c\uff0c\u8fd9\u4e9b\u89c4\u5219\u90fd\u662f\u5343\u9524\u767e\u70bc\uff0c\u88ab\u65e0\u6570\u4eba\u6d4b\u8bd5\u540e\u5f97\u5230\u7684\u7ecf\u9a8c\uff0c\u5b83\u4eec\u80fd\u591f\u5728\u7f16\u7a0b\u8def\u4e0a\u52a9\u4f60\u4e00\u81c2\u4e4b\u529b\u3002\u5f53\u7136\uff0c\u5982\u679c\u4f60\u5728\u57fa\u4e8e\u4ed6\u4eba\u7684\u4ee3\u7801\u8fdb\u884c\u5de5\u4f5c\uff0c\u8bf7\u9002\u5e94\u5e76\u4f7f\u7528\u4ed6\u4eec\u7684\u4ee3\u7801\u98ce\u683c\uff0c\u98ce\u683c\u4e00\u81f4\u6027\u7684\u4f18\u5148\u7ea7\u8981\u9ad8\u4e8e\u5176\u4ed6\u3002 \u81ea\u52a8\u6392\u7248 \u5927\u591a\u6570\u7684\u73b0\u4ee3 IDE \u90fd\u53ef\u4ee5\u5e2e\u52a9\u4f60\u6392\u7248\u4f60\u7684\u4ee3\u7801\uff08\u4f8b\u5982\uff0c\u5728\u4f60\u521b\u5efa\u51fd\u6570\u7684\u65f6\u5019\uff0cIDE\u5c31\u4f1a\u81ea\u52a8\u5e2e\u4f60\u7f29\u8fdb\u51fd\u6570\u4f53\u5185\u7684\u8bed\u53e5\uff09\u3002 \u7136\u800c\uff0c\u5f53\u4f60\u6dfb\u52a0\u3001\u5220\u9664\u4ee3\u7801\uff0c\u6216\u8005\u4fee\u6539\u4e86IDE\u7684\u9ed8\u8ba4\u6392\u7248\u89c4\u5219\u540e\uff0c\u53c8\u6216\u8005\u4f60\u4ece\u5176\u4ed6\u5730\u65b9\u62f7\u8d1d\u4e86\u4ee3\u7801\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6392\u7248\u53ef\u80fd\u4f1a\u53d8\u5f97\u4e00\u56e2\u7cdf\u3002\u4fee\u590d\u90e8\u5206\u6216\u6574\u4e2a\u6587\u4ef6\u4e2d\u7684\u6392\u7248\u95ee\u9898\u662f\u975e\u5e38\u5934\u75bc\u7684\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u73b0\u4ee3DIE\u901a\u5e38\u90fd\u63d0\u4f9b\u4e86\u81ea\u52a8\u6392\u7248\u7684\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u5bf9\u4f60\u9009\u62e9\u7684\u90e8\u5206\u6216\u8005\u6574\u4e2a\u6587\u4ef6\u8fdb\u884c\u91cd\u65b0\u6392\u7248\u3002 For Visual Studio users \u5728 Visual Studio \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 Edit > Advanced > Format Document and Edit > Advanced > Format Selection \u6765\u914d\u7f6e\u81ea\u52a8\u6392\u7248\u3002 For Code::Blocks users \u5728 Code::Blocks \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u9f20\u6807\u53f3\u952e\uff0c\u9009\u62e9 Format use AStyle \u6765\u4f7f\u7528\u3002 \u4e3a\u4e86\u4f7f\u7528\u65b9\u4fbf\uff0c\u63a8\u8350\u4e3a\u81ea\u52a8\u6392\u7248\u529f\u80fd\u7ed1\u5b9a\u4e00\u4e2a\u952e\u76d8\u5feb\u6377\u952e\u3002 \u5c0f\u8d34\u58eb \u4f7f\u7528\u81ea\u52a8\u6392\u7248\u529f\u80fd\uff0c\u662f\u4fdd\u6301\u4ee3\u7801\u98ce\u683c\u4e00\u81f4\u6027\u7684\u597d\u529e\u6cd5\u3002","title":"1.8 - \u7a7a\u767d\u7b26\u548c\u57fa\u672c\u6392\u7248"},{"location":"1-8-Whitespace-and-basic-formatting/#_1","text":"\u548c\u5176\u4ed6\u8bed\u8a00\u4e0d\u540c\uff0cC++ \u5e76\u4e0d\u4f1a\u5f3a\u5236\u7a0b\u5e8f\u5458\u4f7f\u7528\u67d0\u79cd\u6392\u7248\u98ce\u683c\uff08\u8fd8\u8bb0\u5f97\u5417\uff1f\u76f8\u4fe1\u7a0b\u5e8f\u5458\uff01\uff09\u3002C++ \u4e2d\u6709\u5f88\u591a\u53ef\u4ee5\u4f7f\u7528\u7684\u6392\u7248\u65b9\u5f0f\uff0c\u5173\u4e8e\u54ea\u4e00\u79cd\u624d\u662f\u6700\u4f73\u7684\u65b9\u5f0f\uff0c\u4f60\u53ef\u80fd\u4f1a\u6709\u4e0d\u540c\u7684\u770b\u6cd5\u3002\u6211\u4eec\u7684\u57fa\u672c\u539f\u5219\u662f\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\u6700\u5f3a\u3001\u4e00\u6267\u884c\u6700\u597d\u7684\u90a3\u79cd\u6392\u7248\u98ce\u683c\uff0c\u5c31\u662f\u6700\u597d\u7684\u3002 \u672c\u6559\u7a0b\u5efa\u8bae\u7684\u6392\u7248\u89c4\u8303\u5982\u4e0b\uff1a 1.\u4f7f\u7528\u5236\u8868\u7b26\u548c\u7a7a\u683c\u8fdb\u884c\u7f29\u8fdb\u90fd\u662f\u5141\u8bb8\u7684 (\u5927\u591a\u6570\u7684 IDE \u90fd\u53ef\u4ee5\u8bbe\u7f6e\u81ea\u52a8\u5c06\u5236\u8868\u7b26\u8f6c\u6362\u4e3a\u89c4\u5b9a\u4e2a\u6570\u7684\u7a7a\u683c\uff09\u3002\u5f00\u53d1\u8005\u4f7f\u7528\u7a7a\u683c\u8fdb\u884c\u6392\u7248\u5c24\u5176\u5185\u5728\u7684\u903b\u8f91\uff0c\u6bd5\u7adf\u4f7f\u7528\u201d\u7a7a\u683c\u201c\u6765\u521b\u5efa\u4ee3\u7801\u4e4b\u95f4\u7684\u201d\u7a7a\u767d\u201c\u662f\u5929\u7ecf\u5730\u4e49\u7684\u3002\u4f7f\u7528\u7a7a\u683c\u6dfb\u52a0\u7684\u7a7a\u767d\uff0c\u4e0d\u8bba\u5728\u4ec0\u4e48\u7f16\u8f91\u5668\u4e0b\uff0c\u770b\u8d77\u6765\u90fd\u662f\u4e00\u81f4\u7684\u3002\u800c\u4f7f\u7528\u5236\u8868\u7b26\u8fdb\u884c\u7f29\u8fdb\u7684\u7a0b\u5e8f\u5458\u4f1a\u5bf9\u4e0a\u8ff0\u884c\u4e3a\u611f\u5230\u56f0\u60d1\uff0c\u5236\u8868\u7b26\u5c31\u662f\u4e3a\u4e86\u7f29\u8fdb\u800c\u5b58\u5728\u7684\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u7528\u5462\uff1f\u66f4\u4f55\u51b5\u4f60\u8fd8\u53ef\u4ee5\u81ea\u7531\u63a7\u5236\u5176\u7f29\u8fdb\u7684\u5bbd\u5ea6\u3002\u8fd9\u4e2a\u95ee\u9898\u6ca1\u6709\u6240\u8c13\u201d\u6b63\u786e\u201c\u7684\u7b54\u6848\uff0c\u4e89\u8bba\u5b83\u7684\u5bf9\u9519\u662f\u6ca1\u6709\u610f\u4e49\u7684\uff0c\u5c31\u597d\u6bd4\u4e89\u8bba\u7cbd\u5b50\u5e94\u8be5\u662f\u751c\u7684\u8fd8\u662f\u54b8\u7684\uff0c\u56e0\u4e3a\u8fd9\u5b8c\u5168\u53d6\u51b3\u4e8e\u4f60\u81ea\u5df1\u7684\u53e3\u5473\u3002 \u4e0d\u8bba\u90a3\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u5efa\u8bae\u4f60\u4f7f\u75284\u4e2a\u7a7a\u683c\u7684\u5bbd\u5ea6\u4f5c\u4e3a\u7f29\u8fdb\u7684\u5bbd\u5ea6\u3002\u6709\u4e9bIDE\u9ed8\u8ba4\u7684\u7f29\u8fdb\u5bbd\u5ea6\u662f3\u4e2a\u7a7a\u683c\uff0c\u5176\u5b9e\u4e5f\u662f\u53ef\u4ee5\u7684\u3002 2.\u51fd\u6570\u62ec\u53f7\u7684\u6392\u7248\u6709\u4e24\u79cd\u53ef\u4ee5\u63a5\u53d7\u7684\u65b9\u5f0f\u3002 Google C++ \u98ce\u683c\u6307\u5357\u5efa\u8bae\u7684\u98ce\u683c\u662f\u82b1\u62ec\u53f7\u4e0d\u6362\u884c\uff1a 1 2 int main () { } \u8fd9\u4e48\u505a\u7684\u4f9d\u636e\u662f\uff0c\u5b83\u80fd\u591f\u51cf\u5c11\u7eb5\u5411\u7a7a\u767d\u7684\u4ea7\u751f\uff08\u62ec\u53f7\u5355\u72ec\u5360\u7528\u4e00\u884c\u662f\u4e0d\u5212\u7b97\u7684\uff09\uff0c\u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u5728\u6709\u9650\u7684\u5c4f\u5e55\u4e2d\u663e\u793a\u66f4\u591a\u7684\u4ee3\u7801\uff0c\u8fd9\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u7684\u7406\u89e3\u4ee3\u7801\u3002 \u4e0d\u8fc7\uff0c\u6211\u4eec\u8fd8\u662f\u503e\u5411\u4e8e\u4f7f\u7528\u53e6\u5916\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u5373\u82b1\u62ec\u53f7\u53e6\u8d77\u4e00\u884c\uff1a 1 2 3 int main () { } \u8fd9\u4e48\u505a\u53ef\u4ee5\u589e\u5f3a\u53ef\u8bfb\u6027\uff0c\u56e0\u4e3a\u4e00\u5bf9\u82b1\u62ec\u53f7\u603b\u662f\u5177\u6709\u76f8\u540c\u7684\u7f29\u8fdb\uff0c\u8fd9\u4e48\u505a\u53ef\u4ee5\u51cf\u5c11\u72af\u9519\u7684\u51e0\u7387\u3002\u5982\u679c\u7f16\u8bd1\u5668\u62a5\u544a\u4e86\u62ec\u53f7\u4e0d\u5339\u914d\u7684\u9519\u8bef\uff0c\u90a3\u4f60\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u627e\u5230\u95ee\u9898\u6240\u5728\u3002 3.\u6bcf\u4e00\u5c42\u82b1\u62ec\u53f7\u4e2d\u7684\u8bed\u53e5\u90fd\u5fc5\u987b\u8fdb\u884c\u7f29\u8fdb\u4e00\u7ea7\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 int main () { std :: cout << \"Hello world! \\n \" ; // tabbed in one tab (4 spaces) std :: cout << \"Nice to meet you. \\n \" ; // tabbed in one tab (4 spaces) } 4.\u6bcf\u4e00\u884c\u4ee3\u7801\u90fd\u4e0d\u5e94\u8be5\u592a\u957f\u3002\u5e38\u89c1\u7684\u89c4\u5219\u662f\uff1a\u5355\u884c\u4e0d\u80fd\u8d85\u8fc780\u4e2a\u5b57\u7b26\u3002\u5982\u679c\u4e00\u884c\u4ee3\u7801\u592a\u957f\uff0c\u5b83\u5c31\u5e94\u5f53\u88ab\u5206\u5272\u4e3a\u591a\u884c\uff08\u5728\u5408\u9002\u7684\u4f4d\u7f6e\u5206\u5272\uff09\u3002\u5177\u4f53\u64cd\u4f5c\u65f6\u53ef\u4ee5\u5c06\u5404\u4e2a\u5b50\u884c\u989d\u5916\u7f29\u8fdb\u4e00\u7ea7\uff0c\u6216\u8005\uff0c\u5982\u679c\u5b50\u884c\u4e4b\u95f4\u975e\u5e38\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u5c06\u5b83\u4eec\u90fd\u5bf9\u9f50\u5230\u4e0a\u9762\u7684\u884c\uff08\u53ef\u8bfb\u6027\u66f4\u597d\uff09\u3002 1 2 3 4 5 6 7 8 9 10 int main () { std :: cout << \"This is a really, really, really, really, really, really, really, \" \"really long line \\n \" ; // one extra indentation for continuation line std :: cout << \"This is another really, really, really, really, really, really, really, \" \"really long line \\n \" ; // text aligned with the previous line for continuation line std :: cout << \"This one is short \\n \" ; } \u8fd9\u4e48\u505a\u53ef\u4ee5\u4f7f\u5f97\u6bcf\u884c\u4ee3\u7801\u90fd\u5177\u6709\u5f88\u597d\u7684\u53ef\u8bfb\u6027\u3002\u5728\u5bbd\u5c4f\u663e\u793a\u5668\u4e0a\uff0c\u8fd9\u4e48\u505a\u8fd8\u53ef\u4ee5\u5141\u8bb8\u4f60\u5c06\u4e24\u4e2a\u4ee3\u7801\u7a97\u53e3\u5e76\u6392\u663e\u793a\uff0c\u8f7b\u677e\u5730\u5bf9\u6bd4\u5b83\u4eec\u7684\u5dee\u5f02\u3002 \u6700\u4f73\u5b9e\u8df5 \u5355\u884c\u4ee3\u7801\u957f\u5ea6\u4e0d\u5e94\u8be5\u8d85\u8fc780\u4e2a\u5b57\u7b26\u3002 \u5c0f\u8d34\u58eb \u5f88\u591a\u7f16\u8f91\u5668\u90fd\u96c6\u6210\uff08\u6216\u901a\u8fc7\u63d2\u4ef6/\u6269\u5c55\uff09\u4e86 \u5217\u6307\u793a\u5668 \uff0c\u80fd\u591f\u663e\u793a\u51fa\u7279\u5b9a\u5217\u7684\u4f4d\u7f6e\uff08\u4f8b\u598280\u4e2a\u5b57\u7b26\u5904\uff09\u3002\u56e0\u6b64\u4f60\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5224\u65ad\u4f60\u7684\u4ee3\u7801\u884c\u662f\u4e0d\u662f\u8d85\u957f\u4e86\u3002\u641c\u7d22\u7f16\u8f91\u5668\u540d+\u201cColumn guide\u201d\u6765\u67e5\u8be2\u4f60\u7684\u7f16\u8f91\u5668\u662f\u5426\u652f\u6301\u8be5\u7279\u6027\u3002 \u8bd1\u6ce8\uff1a\u5728 VS Code \u4e2d\uff0c\u8fd9\u4e00\u7279\u6027\u53eb\u505a editor.rulers \uff0c\u53ef\u4ee5\u5728\u8bbe\u7f6e\u4e2d\u914d\u7f6e\u5176\u51fa\u73b0\u7684\u4f4d\u7f6e\uff0c\u4f8b\u5982 editor.rulers=[80] 5.\u5982\u679c\u4e00\u4e2a\u8f83\u957f\u7684\u884c\u88ab\u57fa\u4e8e\u8fd0\u7b97\u7b26\u5206\u5272\u4e3a\u4e86\u591a\u4e2a\u77ed\u884c\uff0c\u90a3\u4e48\u8fd0\u7b97\u7b26\u5e94\u8be5\u653e\u7f6e\u5728\u4e0b\u4e00\u884c\u7684\u5f00\u5934\u800c\u4e0d\u662f\u4e0a\u4e00\u884c\u7684\u7ed3\u5c3e\uff1a 1 2 3 std :: cout << 3 + 4 + 5 + 6 * 7 * 8 ; \u8fd9\u4e48\u505a\u53ef\u4ee5\u660e\u786e\u7684\u8868\u660e\u4e0b\u4e00\u884c\u662f\u627f\u63a5\u81ea\u4e0a\u4e00\u884c\u7684\uff0c\u800c\u4e14\u4f60\u53ef\u4ee5\u5c06\u8fd0\u7b97\u7b26\u5de6\u5bf9\u9f50\uff0c\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002 6.\u4f7f\u7528\u7a7a\u683c\u5c06\u503c\u3001\u6ce8\u91ca\u5bf9\u9f50\uff0c\u6216\u5728\u4ee3\u7801\u6bb5\u4e4b\u95f4\u6dfb\u52a0\u7a7a\u683c\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002 \u53ef\u8bfb\u6027\u4e0d\u4f73\uff1a 1 2 3 4 cost = 57 ; pricePerItem = 24 ; value = 5 ; numberOfItems = 17 ; \u53ef\u8bfb\u6027\u4f73\uff1a 1 2 3 4 cost = 57 ; pricePerItem = 24 ; value = 5 ; numberOfItems = 17 ; \u53ef\u8bfb\u6027\u4e0d\u4f73\uff1a 1 2 3 std :: cout << \"Hello world! \\n \" ; // cout lives in the iostream library std :: cout << \"It is very nice to meet you! \\n \" ; // these comments make the code hard to read std :: cout << \"Yeah! \\n \" ; // especially when lines are different lengths \u53ef\u8bfb\u6027\u4f73\uff1a 1 2 3 std :: cout << \"Hello world! \\n \" ; // cout lives in the iostream library std :: cout << \"It is very nice to meet you! \\n \" ; // these comments are easier to read std :: cout << \"Yeah! \\n \" ; // especially when all lined up \u53ef\u8bfb\u6027\u4e0d\u4f73\uff1a 1 2 3 4 5 6 // cout lives in the iostream library std :: cout << \"Hello world! \\n \" ; // these comments make the code hard to read std :: cout << \"It is very nice to meet you! \\n \" ; // especially when all bunched together std :: cout << \"Yeah! \\n \" ; \u53ef\u8bfb\u6027\u4f73\uff1a 1 2 3 4 5 6 7 8 // cout lives in the iostream library std :: cout << \"Hello world! \\n \" ; // these comments are easier to read std :: cout << \"It is very nice to meet you! \\n \" ; // when separated by whitespace std :: cout << \"Yeah! \\n \" ; \u672c\u6559\u7a0b\u5c06\u59cb\u7ec8\u8d2f\u5f7b\u4e0a\u8ff0\u6392\u7248\u7ea6\u5b9a\uff0c\u5b83\u4eec\u7ec8\u5c06\u4f1a\u79f0\u4e3a\u4f60\u4e60\u60ef\u7684\u4e00\u90e8\u5206\u3002\u5728\u6211\u4eec\u4ecb\u7ecd\u4fe1\u7684\u8bed\u8a00\u7279\u6027\u7684\u65f6\u5019\uff0c\u4e5f\u4f1a\u5bf9\u5e94\u5730\u4ecb\u7ecd\u5176\u9002\u7528\u7684\u4ee3\u7801\u6392\u7248\u98ce\u683c\u3002 C++ \u7ed9\u4e0e\u4e86\u4f60\u9009\u62e9\u4efb\u4f55\u4f60\u559c\u6b22\u7684\u98ce\u683c\u7684\u6743\u5229\uff0c\u4f46\u662f\uff0c\u6211\u4eec\u4ecd\u7136\u5f3a\u70c8\u63a8\u8350\u4f60\u4f7f\u7528\u4e0e\u6211\u4eec\u76f8\u540c\u7684\u4ee3\u7801\u98ce\u683c\uff0c\u8fd9\u4e9b\u89c4\u5219\u90fd\u662f\u5343\u9524\u767e\u70bc\uff0c\u88ab\u65e0\u6570\u4eba\u6d4b\u8bd5\u540e\u5f97\u5230\u7684\u7ecf\u9a8c\uff0c\u5b83\u4eec\u80fd\u591f\u5728\u7f16\u7a0b\u8def\u4e0a\u52a9\u4f60\u4e00\u81c2\u4e4b\u529b\u3002\u5f53\u7136\uff0c\u5982\u679c\u4f60\u5728\u57fa\u4e8e\u4ed6\u4eba\u7684\u4ee3\u7801\u8fdb\u884c\u5de5\u4f5c\uff0c\u8bf7\u9002\u5e94\u5e76\u4f7f\u7528\u4ed6\u4eec\u7684\u4ee3\u7801\u98ce\u683c\uff0c\u98ce\u683c\u4e00\u81f4\u6027\u7684\u4f18\u5148\u7ea7\u8981\u9ad8\u4e8e\u5176\u4ed6\u3002","title":"\u57fa\u672c\u4ee3\u7801\u683c\u5f0f"},{"location":"1-8-Whitespace-and-basic-formatting/#_2","text":"\u5927\u591a\u6570\u7684\u73b0\u4ee3 IDE \u90fd\u53ef\u4ee5\u5e2e\u52a9\u4f60\u6392\u7248\u4f60\u7684\u4ee3\u7801\uff08\u4f8b\u5982\uff0c\u5728\u4f60\u521b\u5efa\u51fd\u6570\u7684\u65f6\u5019\uff0cIDE\u5c31\u4f1a\u81ea\u52a8\u5e2e\u4f60\u7f29\u8fdb\u51fd\u6570\u4f53\u5185\u7684\u8bed\u53e5\uff09\u3002 \u7136\u800c\uff0c\u5f53\u4f60\u6dfb\u52a0\u3001\u5220\u9664\u4ee3\u7801\uff0c\u6216\u8005\u4fee\u6539\u4e86IDE\u7684\u9ed8\u8ba4\u6392\u7248\u89c4\u5219\u540e\uff0c\u53c8\u6216\u8005\u4f60\u4ece\u5176\u4ed6\u5730\u65b9\u62f7\u8d1d\u4e86\u4ee3\u7801\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6392\u7248\u53ef\u80fd\u4f1a\u53d8\u5f97\u4e00\u56e2\u7cdf\u3002\u4fee\u590d\u90e8\u5206\u6216\u6574\u4e2a\u6587\u4ef6\u4e2d\u7684\u6392\u7248\u95ee\u9898\u662f\u975e\u5e38\u5934\u75bc\u7684\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u73b0\u4ee3DIE\u901a\u5e38\u90fd\u63d0\u4f9b\u4e86\u81ea\u52a8\u6392\u7248\u7684\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u5bf9\u4f60\u9009\u62e9\u7684\u90e8\u5206\u6216\u8005\u6574\u4e2a\u6587\u4ef6\u8fdb\u884c\u91cd\u65b0\u6392\u7248\u3002 For Visual Studio users \u5728 Visual Studio \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 Edit > Advanced > Format Document and Edit > Advanced > Format Selection \u6765\u914d\u7f6e\u81ea\u52a8\u6392\u7248\u3002 For Code::Blocks users \u5728 Code::Blocks \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u9f20\u6807\u53f3\u952e\uff0c\u9009\u62e9 Format use AStyle \u6765\u4f7f\u7528\u3002 \u4e3a\u4e86\u4f7f\u7528\u65b9\u4fbf\uff0c\u63a8\u8350\u4e3a\u81ea\u52a8\u6392\u7248\u529f\u80fd\u7ed1\u5b9a\u4e00\u4e2a\u952e\u76d8\u5feb\u6377\u952e\u3002 \u5c0f\u8d34\u58eb \u4f7f\u7528\u81ea\u52a8\u6392\u7248\u529f\u80fd\uff0c\u662f\u4fdd\u6301\u4ee3\u7801\u98ce\u683c\u4e00\u81f4\u6027\u7684\u597d\u529e\u6cd5\u3002","title":"\u81ea\u52a8\u6392\u7248"},{"location":"1-9-Introduction-to-literals-and-operators/","tags":["literals","operator"],"text":"\u5b57\u9762\u91cf \u8003\u8651\u4e0b\u9762\u4e24\u6761\u8bed\u53e5\uff1a 1 2 std :: cout << \"Hello world!\" ; int x { 5 }; \u8fd9\u91cc\u7684 \u201cHello world!\u201d \u548c 5 \u6709\u4ec0\u4e48\u5171\u540c\u70b9\uff1f\u5b83\u4eec\u90fd\u662f\u5b57\u9762\u91cf\u3002 literals \uff08\u4e5f\u79f0\u4e3a\u5b57\u9762\u91cf\u5e38\u91cf\uff09\u6307\u7684\u662f\u88ab\u76f4\u63a5\u63d2\u5165\u5230\u6e90\u7801\u4e2d\u7684\u5e38\u91cf\u3002 \u5b57\u9762\u91cf\u548c\u53d8\u91cf\u90fd\u5177\u6709\u4e00\u4e2a\u503c\uff08\u53ca\u5176\u7c7b\u578b\uff09\u3002\u4f46\u662f\uff0c\u5b57\u9762\u91cf\u7684\u503c\u662f\u56fa\u5b9a\u7684\uff0c\u800c\u4e14\u4e0d\u80fd\u88ab\u4fee\u6539\uff08\u6240\u4ee5\u6210\u4e3a\u5e38\u91cf\uff09\uff0c\u800c\u53d8\u91cf\u7684\u503c\u662f\u53ef\u4ee5\u6539\u53d8\u7684\uff0c\u4e0d\u8bba\u662f\u901a\u8fc7\u521d\u59cb\u5316\u8fd8\u662f\u901a\u8fc7\u8d4b\u503c\u3002 \u64cd\u4f5c\u7b26 \u6570\u5b66\u4e2d\u7684\u8fd0\u7b97\u6307\u7684\u662f\u4e00\u4e2a\u8ba1\u7b97\u64cd\u4f5c\uff0c\u5b83\u6d89\u53ca\u52300\u4e2a\u6216\u591a\u4e2a\u8f93\u5165\u503c\uff08\u79f0\u4e3a \u64cd\u4f5c\u6570 \uff09\uff0c\u5e76\u80fd\u591f\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u503c\uff08\u79f0\u4e3a\u8f93\u51fa\u503c\uff09\u3002\u8fd9\u4e2a\u7279\u5b9a\u7684\u8ba1\u7b97\u64cd\u4f5c\uff0c\u901a\u8fc7\u67d0\u79cd\u7ed3\u6784\uff08\u901a\u5e38\u662f\u4e00\u4e2a\u6216\u4e00\u5bf9\u7b26\u53f7\uff09\u6765\u6307\u5b9a\uff0c\u8fd9\u79cd\u7ed3\u6784\u79f0\u4e4b\u4e3a \u8fd0\u7b97\u7b26(operator) \u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u90fd\u77e5\u9053 2 + 3 = 5 \u3002\u5728\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4e2d\uff0c2\u548c3\u79f0\u4e4b\u4e3a\u8fd0\u7b97\u7b26\uff0c\u800c\u7b26\u53f7 + \u5219\u79f0\u4e3a\u8fd0\u7b97\u7b26\uff0c\u8be5\u8fd0\u7b97\u7b26\u544a\u8bc9\u6211\u4eec\u6b64\u65f6\u5c06\u5bf9\u4e24\u4e2a\u64cd\u4f5c\u6570\u6267\u884c\u4e00\u4e2a\u52a0\u6cd5\u64cd\u4f5c\uff0c\u5e76\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u503c 5\u3002 \u4f5c\u8005\u6ce8 \u5bf9\u4e8e\u7b26\u53f7\u7c7b\u578b\u7684\u8fd0\u7b97\u7b26\uff0c\u5e38\u7528\u7684\u672f\u8bed\u8868\u793a\u65b9\u6cd5\u662f\u5c06\u8fd0\u7b97\u7b26\u672c\u8eab\u5199\u5728operator\u4e4b\u540e\uff0c\u5176\u539f\u56e0\u4f1a\u968f\u7740\u6211\u4eec\u6df1\u5165\u4e86\u89e3\u8fd0\u7b97\u7b26\u540e\u53d8\u5f97\u6e05\u6670\u3002 \u4f8b\u5982\uff0c\u52a0\u6cd5\u8fd0\u7b97\u7b26\u4f1a\u5199\u4f5c operator+ \uff0c\u63d0\u53d6\u8fd0\u7b97\u7b26\u5219\u5199\u4f5c operator>> . \u5bf9\u4e8e\u6570\u5b66\u4e2d\u5e38\u7528\u6807\u51c6\u8fd0\u7b97\u7b26\u4f60\u5e94\u5f53\u5df2\u7ecf\u975e\u5e38\u719f\u6089\u4e86\uff0c\u5305\u62ec\u52a0( + )\u3001\u51cf( - )\u3001\u4e58( * )\u3001\u9664( / )\u3002\u5728 C++ \u4e2d\uff0c\u8d4b\u503c\u53f7( = )\u4e5f\u662f\u4e00\u4e2a\u8fd0\u7b97\u7b26\uff0c\u6b64\u5916\u8fd8\u6709 \u63d2\u5165\u8fd0\u7b97\u7b26 << \u548c\u63d0\u53d6\u8fd0\u7b97\u7b26 >> \u3002\u6709\u4e9b\u8fd0\u7b97\u7b26\u5305\u542b\u591a\u4e2a\u7b26\u53f7\uff0c\u4f8b\u5982\u76f8\u7b49\u6bd4\u8f83\u8fd0\u7b97\u7b26( == )\u3002\u8fd8\u6709\u4e00\u4e9b\u8fd0\u7b97\u7b26\u5219\u662f\u4ee5\u5355\u8bcd\u7684\u5f62\u5f0f\u51fa\u73b0\u7684\uff08\u4f8b\u5982\uff1a new \uff0c delete \u548c throw \uff09\u3002 \u4e00\u4e2a\u8fd0\u7b97\u7b26\u4f7f\u7528\u7684\u64cd\u4f5c\u6570\u7684\u4e2a\u6570\uff0c\u79f0\u4e3a\u8be5\u8fd0\u7b97\u7b26\u7684 \u5143\u6570(arity) \u3002C++ \u4e2d\u7684\u8fd0\u7b97\u7b26\u5177\u6709\u4e09\u79cd\u4e0d\u540c\u7684\u5143\u6570\uff1a \u4e00\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e00\u4e2a\u64cd\u4f5c\u6570\u3002 - \u662f\u4e00\u4e2a\u4e00\u5143\u8fd0\u7b97\u7b26\u7684\u5178\u578b\uff0c\u4f8b\u5982\uff0c -5 \u3002\u5b83\u4f7f\u7528\u4e00\u4e2a\u5b57\u9762\u91cf\u64cd\u4f5c\u65705\uff0c\u5e76\u5c06\u5176\u7b26\u53f7\u53cd\u8f6c\uff0c\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u8f93\u51fa\u91cf -5\u3002 \u4e8c\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e24\u4e2a\u64cd\u4f5c\u6570\uff08\u5de6\u53f3\u64cd\u4f5c\u6570\uff09\u3002\u4f8b\u5982 3 + 4 \uff0c\u8fd0\u7b97\u7b26 + \u5c06\u5de6\u64cd\u4f5c\u6570 3 \u548c\u53f3\u64cd\u4f5c\u6570 4\u6267\u884c\u52a0\u6cd5\u8fd0\u7b97\uff0c\u8f93\u51fa\u7ed3\u679c 7\u3002\u63d2\u5165\u8fd0\u7b97\u7b26( << ) \u548c\u63d0\u53d6\u8fd0\u7b97\u7b26( >> )\u4e5f\u662f\u4e8c\u5143\u8fd0\u7b97\u7b26\u3002\u5b83\u7684\u5de6\u4fa7\u4e3a std::cout \u6216 std::cin \uff0c\u53f3\u4fa7\u662f\u8981\u8f93\u51fa\u7684\u503c\u6216\u63a5\u6536\u8f93\u5165\u7684\u53d8\u91cf\u3002 \u4e09\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e09\u4e2a\u64cd\u4f5c\u6570\u3002C++ \u4e2d\u53ea\u6709\u4e00\u79cd\u4e09\u5143\u8fd0\u7b97\u7b26\uff0c\u7a0d\u540e\u6211\u4eec\u4f1a\u4ecb\u7ecd\u3002 \u6ce8\u610f\uff0c\u6709\u4e9b\u8fd0\u7b97\u7b26\u5728\u4e0d\u540c\u7684\u4f7f\u7528\u6761\u4ef6\u4e0b\u5177\u6709\u4e0d\u540c\u7684\u542b\u4e49\u3002\u4f8b\u5982 - \u5c31\u6709\u4e24\u79cd\u4e0d\u540c\u7684\u542b\u4e49\u3002\u5b83\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u5143\u8fd0\u7b97\u7b26\u53d6\u4e00\u4e2a\u503c\u7684\u76f8\u53cd\u6570\uff08\u4f8b\u5982\u5c065\u8f6c\u6362\u4e3a-5\uff0c\u6216\u8005\u53cd\u8fc7\u6765\uff09\uff0c\u6216\u8005\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u4e8c\u5143\u8fd0\u7b97\u7b26\u51cf\u53f7\u4f7f\u7528\uff08\u4f8b\u5982 4 - 3 \uff09\u3002 \u8fd0\u7b97\u7b26\u94fe \u8fd0\u7b97\u7b26\u53ef\u4ee5\u8fdb\u884c\u94fe\u5f0f\u64cd\u4f5c\uff0c\u8fd9\u6837\u4e00\u6765\uff0c\u4e00\u4e2a\u8fd0\u7b97\u7b26\u4fbf\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5176\u4ed6\u8fd0\u7b97\u7b26\u7684\u8f93\u51fa\u7ed3\u679c\u3002\u4f8b\u5982\uff1a 2 * 3 + 4 \uff0c\u4e58\u6cd5\u9996\u5148\u6267\u884c\u5e76\u5f97\u5230\u7ed3\u679c6\uff0c\u7136\u540e\u5b83\u5c31\u6210\u4e3a\u4e86\u52a0\u53f7\u7684\u505a\u64cd\u4f5c\u6570\u3002\u63a5\u4e0b\u6765\u6267\u884c\u52a0\u6cd5\u5f97\u5230\u65b0\u503c10\u3002 \u5728\u8bb2\u89e3\u8fd0\u7b97\u7b26\u65f6\uff0c\u6211\u4eec\u4f1a\u6df1\u5165\u63a2\u8ba8\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u3002\u73b0\u5728\u4f60\u53ea\u9700\u8981\u77e5\u9053\uff0c\u5b83\u7684\u4f18\u5148\u7ea7\u548c\u6570\u5b66\u4e2d\u7684\u4f18\u5148\u7ea7\u662f\u4e00\u6837\u7684\uff1a\u62ec\u53f7\u4f18\u5148\u7ea7\u6700\u9ad8\uff0c\u7136\u540e\u662f\u6307\u6570\u8fd0\u7b97\u7b26\uff0c\u7136\u540e\u662f\u4e58\u9664\uff0c\u518d\u7136\u540e\u662f\u52a0\u51cf\u3002\u8be5\u4f18\u5148\u7ea7\u987a\u5e8f\u5e38\u5e38\u7f29\u5199\u4e3a PEMDAS \uff08\u6216\u8005\u5c55\u5f00\u4e3a\u4e0b\u9762\u8fd9\u79cd\u5e2e\u52a9\u8bb0\u5fc6\u7684\u5f62\u5f0f\uff1a\u201cPlease Excuse My Dear Aunt Sally\u201d\uff09 \u4f5c\u8005\u6ce8 \u5728\u6709\u4e9b\u56fd\u5bb6\uff0cPEMDAS \u8868\u8ff0\u4e3a PEDMAS\u3001BEDMAS\u3001BODMAS \u6216 BIDMAS\u3002","title":"1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26"},{"location":"1-9-Introduction-to-literals-and-operators/#_1","text":"\u8003\u8651\u4e0b\u9762\u4e24\u6761\u8bed\u53e5\uff1a 1 2 std :: cout << \"Hello world!\" ; int x { 5 }; \u8fd9\u91cc\u7684 \u201cHello world!\u201d \u548c 5 \u6709\u4ec0\u4e48\u5171\u540c\u70b9\uff1f\u5b83\u4eec\u90fd\u662f\u5b57\u9762\u91cf\u3002 literals \uff08\u4e5f\u79f0\u4e3a\u5b57\u9762\u91cf\u5e38\u91cf\uff09\u6307\u7684\u662f\u88ab\u76f4\u63a5\u63d2\u5165\u5230\u6e90\u7801\u4e2d\u7684\u5e38\u91cf\u3002 \u5b57\u9762\u91cf\u548c\u53d8\u91cf\u90fd\u5177\u6709\u4e00\u4e2a\u503c\uff08\u53ca\u5176\u7c7b\u578b\uff09\u3002\u4f46\u662f\uff0c\u5b57\u9762\u91cf\u7684\u503c\u662f\u56fa\u5b9a\u7684\uff0c\u800c\u4e14\u4e0d\u80fd\u88ab\u4fee\u6539\uff08\u6240\u4ee5\u6210\u4e3a\u5e38\u91cf\uff09\uff0c\u800c\u53d8\u91cf\u7684\u503c\u662f\u53ef\u4ee5\u6539\u53d8\u7684\uff0c\u4e0d\u8bba\u662f\u901a\u8fc7\u521d\u59cb\u5316\u8fd8\u662f\u901a\u8fc7\u8d4b\u503c\u3002","title":"\u5b57\u9762\u91cf"},{"location":"1-9-Introduction-to-literals-and-operators/#_2","text":"\u6570\u5b66\u4e2d\u7684\u8fd0\u7b97\u6307\u7684\u662f\u4e00\u4e2a\u8ba1\u7b97\u64cd\u4f5c\uff0c\u5b83\u6d89\u53ca\u52300\u4e2a\u6216\u591a\u4e2a\u8f93\u5165\u503c\uff08\u79f0\u4e3a \u64cd\u4f5c\u6570 \uff09\uff0c\u5e76\u80fd\u591f\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u503c\uff08\u79f0\u4e3a\u8f93\u51fa\u503c\uff09\u3002\u8fd9\u4e2a\u7279\u5b9a\u7684\u8ba1\u7b97\u64cd\u4f5c\uff0c\u901a\u8fc7\u67d0\u79cd\u7ed3\u6784\uff08\u901a\u5e38\u662f\u4e00\u4e2a\u6216\u4e00\u5bf9\u7b26\u53f7\uff09\u6765\u6307\u5b9a\uff0c\u8fd9\u79cd\u7ed3\u6784\u79f0\u4e4b\u4e3a \u8fd0\u7b97\u7b26(operator) \u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u90fd\u77e5\u9053 2 + 3 = 5 \u3002\u5728\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4e2d\uff0c2\u548c3\u79f0\u4e4b\u4e3a\u8fd0\u7b97\u7b26\uff0c\u800c\u7b26\u53f7 + \u5219\u79f0\u4e3a\u8fd0\u7b97\u7b26\uff0c\u8be5\u8fd0\u7b97\u7b26\u544a\u8bc9\u6211\u4eec\u6b64\u65f6\u5c06\u5bf9\u4e24\u4e2a\u64cd\u4f5c\u6570\u6267\u884c\u4e00\u4e2a\u52a0\u6cd5\u64cd\u4f5c\uff0c\u5e76\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u503c 5\u3002 \u4f5c\u8005\u6ce8 \u5bf9\u4e8e\u7b26\u53f7\u7c7b\u578b\u7684\u8fd0\u7b97\u7b26\uff0c\u5e38\u7528\u7684\u672f\u8bed\u8868\u793a\u65b9\u6cd5\u662f\u5c06\u8fd0\u7b97\u7b26\u672c\u8eab\u5199\u5728operator\u4e4b\u540e\uff0c\u5176\u539f\u56e0\u4f1a\u968f\u7740\u6211\u4eec\u6df1\u5165\u4e86\u89e3\u8fd0\u7b97\u7b26\u540e\u53d8\u5f97\u6e05\u6670\u3002 \u4f8b\u5982\uff0c\u52a0\u6cd5\u8fd0\u7b97\u7b26\u4f1a\u5199\u4f5c operator+ \uff0c\u63d0\u53d6\u8fd0\u7b97\u7b26\u5219\u5199\u4f5c operator>> . \u5bf9\u4e8e\u6570\u5b66\u4e2d\u5e38\u7528\u6807\u51c6\u8fd0\u7b97\u7b26\u4f60\u5e94\u5f53\u5df2\u7ecf\u975e\u5e38\u719f\u6089\u4e86\uff0c\u5305\u62ec\u52a0( + )\u3001\u51cf( - )\u3001\u4e58( * )\u3001\u9664( / )\u3002\u5728 C++ \u4e2d\uff0c\u8d4b\u503c\u53f7( = )\u4e5f\u662f\u4e00\u4e2a\u8fd0\u7b97\u7b26\uff0c\u6b64\u5916\u8fd8\u6709 \u63d2\u5165\u8fd0\u7b97\u7b26 << \u548c\u63d0\u53d6\u8fd0\u7b97\u7b26 >> \u3002\u6709\u4e9b\u8fd0\u7b97\u7b26\u5305\u542b\u591a\u4e2a\u7b26\u53f7\uff0c\u4f8b\u5982\u76f8\u7b49\u6bd4\u8f83\u8fd0\u7b97\u7b26( == )\u3002\u8fd8\u6709\u4e00\u4e9b\u8fd0\u7b97\u7b26\u5219\u662f\u4ee5\u5355\u8bcd\u7684\u5f62\u5f0f\u51fa\u73b0\u7684\uff08\u4f8b\u5982\uff1a new \uff0c delete \u548c throw \uff09\u3002 \u4e00\u4e2a\u8fd0\u7b97\u7b26\u4f7f\u7528\u7684\u64cd\u4f5c\u6570\u7684\u4e2a\u6570\uff0c\u79f0\u4e3a\u8be5\u8fd0\u7b97\u7b26\u7684 \u5143\u6570(arity) \u3002C++ \u4e2d\u7684\u8fd0\u7b97\u7b26\u5177\u6709\u4e09\u79cd\u4e0d\u540c\u7684\u5143\u6570\uff1a \u4e00\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e00\u4e2a\u64cd\u4f5c\u6570\u3002 - \u662f\u4e00\u4e2a\u4e00\u5143\u8fd0\u7b97\u7b26\u7684\u5178\u578b\uff0c\u4f8b\u5982\uff0c -5 \u3002\u5b83\u4f7f\u7528\u4e00\u4e2a\u5b57\u9762\u91cf\u64cd\u4f5c\u65705\uff0c\u5e76\u5c06\u5176\u7b26\u53f7\u53cd\u8f6c\uff0c\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u8f93\u51fa\u91cf -5\u3002 \u4e8c\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e24\u4e2a\u64cd\u4f5c\u6570\uff08\u5de6\u53f3\u64cd\u4f5c\u6570\uff09\u3002\u4f8b\u5982 3 + 4 \uff0c\u8fd0\u7b97\u7b26 + \u5c06\u5de6\u64cd\u4f5c\u6570 3 \u548c\u53f3\u64cd\u4f5c\u6570 4\u6267\u884c\u52a0\u6cd5\u8fd0\u7b97\uff0c\u8f93\u51fa\u7ed3\u679c 7\u3002\u63d2\u5165\u8fd0\u7b97\u7b26( << ) \u548c\u63d0\u53d6\u8fd0\u7b97\u7b26( >> )\u4e5f\u662f\u4e8c\u5143\u8fd0\u7b97\u7b26\u3002\u5b83\u7684\u5de6\u4fa7\u4e3a std::cout \u6216 std::cin \uff0c\u53f3\u4fa7\u662f\u8981\u8f93\u51fa\u7684\u503c\u6216\u63a5\u6536\u8f93\u5165\u7684\u53d8\u91cf\u3002 \u4e09\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e09\u4e2a\u64cd\u4f5c\u6570\u3002C++ \u4e2d\u53ea\u6709\u4e00\u79cd\u4e09\u5143\u8fd0\u7b97\u7b26\uff0c\u7a0d\u540e\u6211\u4eec\u4f1a\u4ecb\u7ecd\u3002 \u6ce8\u610f\uff0c\u6709\u4e9b\u8fd0\u7b97\u7b26\u5728\u4e0d\u540c\u7684\u4f7f\u7528\u6761\u4ef6\u4e0b\u5177\u6709\u4e0d\u540c\u7684\u542b\u4e49\u3002\u4f8b\u5982 - \u5c31\u6709\u4e24\u79cd\u4e0d\u540c\u7684\u542b\u4e49\u3002\u5b83\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u5143\u8fd0\u7b97\u7b26\u53d6\u4e00\u4e2a\u503c\u7684\u76f8\u53cd\u6570\uff08\u4f8b\u5982\u5c065\u8f6c\u6362\u4e3a-5\uff0c\u6216\u8005\u53cd\u8fc7\u6765\uff09\uff0c\u6216\u8005\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u4e8c\u5143\u8fd0\u7b97\u7b26\u51cf\u53f7\u4f7f\u7528\uff08\u4f8b\u5982 4 - 3 \uff09\u3002","title":"\u64cd\u4f5c\u7b26"},{"location":"1-9-Introduction-to-literals-and-operators/#_3","text":"\u8fd0\u7b97\u7b26\u53ef\u4ee5\u8fdb\u884c\u94fe\u5f0f\u64cd\u4f5c\uff0c\u8fd9\u6837\u4e00\u6765\uff0c\u4e00\u4e2a\u8fd0\u7b97\u7b26\u4fbf\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5176\u4ed6\u8fd0\u7b97\u7b26\u7684\u8f93\u51fa\u7ed3\u679c\u3002\u4f8b\u5982\uff1a 2 * 3 + 4 \uff0c\u4e58\u6cd5\u9996\u5148\u6267\u884c\u5e76\u5f97\u5230\u7ed3\u679c6\uff0c\u7136\u540e\u5b83\u5c31\u6210\u4e3a\u4e86\u52a0\u53f7\u7684\u505a\u64cd\u4f5c\u6570\u3002\u63a5\u4e0b\u6765\u6267\u884c\u52a0\u6cd5\u5f97\u5230\u65b0\u503c10\u3002 \u5728\u8bb2\u89e3\u8fd0\u7b97\u7b26\u65f6\uff0c\u6211\u4eec\u4f1a\u6df1\u5165\u63a2\u8ba8\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u3002\u73b0\u5728\u4f60\u53ea\u9700\u8981\u77e5\u9053\uff0c\u5b83\u7684\u4f18\u5148\u7ea7\u548c\u6570\u5b66\u4e2d\u7684\u4f18\u5148\u7ea7\u662f\u4e00\u6837\u7684\uff1a\u62ec\u53f7\u4f18\u5148\u7ea7\u6700\u9ad8\uff0c\u7136\u540e\u662f\u6307\u6570\u8fd0\u7b97\u7b26\uff0c\u7136\u540e\u662f\u4e58\u9664\uff0c\u518d\u7136\u540e\u662f\u52a0\u51cf\u3002\u8be5\u4f18\u5148\u7ea7\u987a\u5e8f\u5e38\u5e38\u7f29\u5199\u4e3a PEMDAS \uff08\u6216\u8005\u5c55\u5f00\u4e3a\u4e0b\u9762\u8fd9\u79cd\u5e2e\u52a9\u8bb0\u5fc6\u7684\u5f62\u5f0f\uff1a\u201cPlease Excuse My Dear Aunt Sally\u201d\uff09 \u4f5c\u8005\u6ce8 \u5728\u6709\u4e9b\u56fd\u5bb6\uff0cPEMDAS \u8868\u8ff0\u4e3a PEDMAS\u3001BEDMAS\u3001BODMAS \u6216 BIDMAS\u3002","title":"\u8fd0\u7b97\u7b26\u94fe"},{"location":"1-x-Chapter-1-summary-and-quiz/","tags":["summary"],"text":"\u7ae0\u8282\u56de\u987e \u8bed\u53e5 \u662f\u4e00\u79cd\u6307\u4ee4\uff0c\u8ba9\u7a0b\u5e8f\u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\u3002\u8bed\u53e5\u901a\u5e38\u4ee5\u5206\u53f7\u7ed3\u5c3e\u3002 \u51fd\u6570 \u662f\u4e00\u7ec4\u5e8f\u5217\u6267\u884c\u7684\u8bed\u53e5\u3002\u6bcf\u4e2a C++\u7a0b\u5e8f\u90fd\u5fc5\u987b\u5305\u542b\u4e00\u4e2a main \u51fd\u6570\u3002\u5f53\u4f60\u6267\u884c\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u9996\u5148\u4f1a\u4ece main \u51fd\u6570\u5f00\u59cb\u6267\u884c\u3002 \u8fd9\u4e9b\u63a7\u5236 C++ \u5143\u7d20\u7ec4\u7ec7\u5f62\u5f0f\u7684\u89c4\u5219\uff0c\u79f0\u4e3a \u8bed\u6cd5 \u3002\u5f53\u4f60\u8fdd\u53cd\u8bed\u8a00\u7684\u8bed\u6cd5\u89c4\u5219\u65f6\uff0c\u5c31\u4f1a\u4ea7\u751f\u8bed\u6cd5\u9519\u8bef\u3002 \u6ce8\u91ca \u5141\u8bb8\u4f60\u5728\u4ee3\u7801\u4e2d\u7559\u4e0b\u7b14\u8bb0\u3002C++ \u652f\u6301\u4e24\u79cd\u7c7b\u578b\u7684\u6ce8\u91ca\u3002\u884c\u5185\u6ce8\u91ca\u4ee5 // \u5f00\u5934\u3002\u6bb5\u843d\u6ce8\u91ca\u5219\u4ee5 /* \u5f00\u5934\uff0c\u4ee5 */ \u7ed3\u675f\u3002\u8bf7\u4e0d\u8981\u5d4c\u5957\u6ce8\u91ca\u3002 \u4f60\u53ef\u4ee5\u901a\u8fc7\u6ce8\u91ca\u6765\u4e34\u65f6\u5c4f\u853d\u4e00\u884c\u6216\u4e00\u6bb5\u4ee3\u7801\uff0c\u4e0a\u8ff0\u64cd\u4f5c\u79f0\u4e3a\u6ce8\u91ca\u6389\uff08commenting out\uff09\u4ee3\u7801\u3002 \u6570\u636e \u662f\u4e00\u7cfb\u5217\u7b26\u53f7\u5e76\u53ef\u4ee5\u88ab\u89e3\u6790\u4e3a\u67d0\u79cd\u542b\u4e49\u3002\u4e00\u6bb5\u88ab\u5b58\u653e\u5728\u5185\u5b58\u4e2d\u7684\u6570\u636e\u79f0\u4e3a \u503c \u3002 \u53d8\u91cf \u662f\u4e00\u6bb5\u547d\u540d\u7684\u5185\u5b58\uff0c\u73b0\u5728\u6211\u4eec\u4fbf\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5b58\u653e\u503c\u4e86\u3002\u53d8\u91cf\u7684\u540d\u79f0\u4e3a \u6807\u8bc6\u7b26(identifier) \u3002\u521b\u5efa\u53d8\u91cf\u9700\u8981\u901a\u8fc7\u58f0\u660e\u8bed\u53e5\u3002\u5f53\u7a0b\u5e8f\u5f00\u59cb\u8fd0\u884c\u7684\u65f6\u5019\uff0c\u53d8\u91cf\u5c31\u4f1a\u88ab \u5b9e\u4f8b\u5316 \uff0c\u5373\u5b83\u4f1a\u88ab\u5173\u8054\u5230\u67d0\u4e2a\u5185\u5b58\u5730\u5740 \u3002 \u6570\u636e\u7c7b\u578b \u53ef\u4ee5\u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u4f55\u5c06\u4e00\u6bb5\u6570\u636e\u89e3\u91ca\u4e3a\u5177\u6709\u542b\u4e49\u7684\u503c\u3002\u6574\u578b\u6307\u7684\u662f\u4e0d\u5305\u542b\u5c0f\u6570\u90e8\u5206\u7684\u503c\uff0c\u4f8b\u5982 4\u300127\u30010\u3001-2 \u6216-12\u3002 \u62f7\u8d1d\u8d4b\u503c (\u901a\u8fc7 operator= ) \u53ef\u4ee5\u88ab\u7528\u6765\u5411\u67d0\u4e2a\u5df2\u7ecf\u88ab\u521b\u5efa\u7684\u53d8\u91cf\u8d4b\u503c\u3002 \u521d\u59cb\u5316 \u7528\u4e8e\u5728\u53d8\u91cf\u88ab\u521b\u5efa\u7684\u540c\u65f6\u4e3a\u5176\u6307\u5b9a\u3002C++ \u652f\u6301\u4e09\u79cd\u7c7b\u578b\u7684\u521d\u59cb\u5316\uff1a \u62f7\u8d1d\u521d\u59cb\u5316 \u3001 \u76f4\u63a5\u521d\u59cb\u5316 \u548c \u7edf\u4e00\u521d\u59cb\u5316 \uff08\u4e5f\u79f0\u4e3a \u5217\u8868\u521d\u59cb\u5316 \u6216 \u62ec\u53f7\u521d\u59cb\u5316 \uff09\u3002 \u4f60\u5e94\u8be5\u4f18\u5148\u4f7f\u7528\u7edf\u4e00\u521d\u59cb\u5316\u800c\u4e0d\u662f\u5176\u4ed6\u521d\u59cb\u5316\u65b9\u5f0f\uff0c\u6b64\u5916\uff0c\u4f7f\u7528\u521d\u59cb\u5316\u4e5f\u4f18\u4e8e\u8d4b\u503c\u3002 \u5c3d\u7ba1\u5728\u4e00\u4e2a\u8bed\u53e5\u4e2d\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\uff0c\u4f46\u662f\u6700\u597d\u8fd8\u662f\u6bcf\u884c\u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cf\u3002 std:: cout \u548c operator<< \u53ef\u4ee5\u5c06\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u4f5c\u4e3a\u6587\u672c\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002 std:: endl \u4f1a\u8f93\u51fa\u4e00\u4e2a\u6362\u884c\u7b26\uff0c\u63a7\u5236\u63a7\u5236\u53f0\u5149\u6807\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\u3002 std:: cin \u548c operator>> \u53ef\u4ee5\u4ece\u952e\u76d8\u83b7\u53d6\u8f93\u5165\u503c\u3002 \u6ca1\u6709\u88ab\u6307\u5b9a\u503c\u7684\u53d8\u91cf\uff0c\u79f0\u4e3a\u672a\u521d\u59cb\u5316\u53d8\u91cf\u3002\u5c1d\u8bd5\u83b7\u53d6\u672a\u521d\u59cb\u5316\u53d8\u91cf\u7684\u503c\uff0c\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \uff0c\u672a\u5b9a\u4e49\u884c\u4e3a\u6709\u591a\u79cd\u8868\u73b0\u5f62\u5f0f\u3002 C++ \u9884\u7559\u4e86\u4e00\u7ec4\u540d\u79f0\uff0c\u79f0\u4e3a \u5173\u952e\u5b57 \u3002\u8fd9\u4e9b\u5173\u952e\u5b57\u5728 C++\u4e2d\u5177\u6709\u7279\u6b8a\u7684\u542b\u4e49\uff0c\u4e0d\u80fd\u88ab\u7528\u4f5c\u53d8\u91cf\u540d\u3002 \u5b57\u9762\u91cf \u6307\u7684\u662f\u76f4\u63a5\u63d2\u5165\u6e90\u7801\u4e2d\u7684\u56fa\u5b9a\u503c\u3002\u4f8b\u5982 5 \u548c \u201cHello world!\u201d\u3002 \u8fd0\u7b97\u6307\u7684\u662f\u4e00\u6b21\u6570\u5b66\u8ba1\u7b97\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u6d89\u53ca\u52300\u4e2a\u6216\u591a\u4e2a\u8f93\u5165\u503c\uff0c\u79f0\u4e3a \u64cd\u4f5c\u6570 \u3002\u5177\u4f53\u7684\u8fd0\u7b97\u901a\u8fc7\u8fd0\u7b97\u7b26\u6765\u6307\u5b9a\u3002\u8fd0\u7b97\u7684\u7ed3\u679c\u4f1a\u4ea7\u751f\u4e00\u4e2a\u8f93\u51fa\u503c\u3002 \u4e00\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e00\u4e2a\u64cd\u4f5c\u6570\u3002 \u4e8c\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e24\u4e2a\u64cd\u4f5c\u6570\uff0c\u901a\u5e38\u79f0\u4e3a\u5de6\u64cd\u4f5c\u6570\u548c\u53f3\u64cd\u4f5c\u6570\u3002 \u4e09\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e09\u4e2a\u64cd\u4f5c\u6570\u3002 \u8868\u8fbe\u5f0f \u662f\u4e00\u7ec4\u5b57\u9762\u91cf\u3001\u53d8\u91cf\u3001\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u8c03\u7528\u7684\u7ec4\u5408\uff0c\u5b83\u7ecf\u8fc7\u6c42\u503c\u540e\uff0c\u4f1a\u5f97\u5230\u4e00\u4e2a\u5355\u4e00\u503c\u3002\u8ba1\u7b97\u5f97\u5230\u8be5\u5355\u4e00\u503c\u7684\u8fc7\u7a0b\u79f0\u4e4b\u4e3a \u6c42\u503c(evaluation) \uff0c\u800c\u8be5\u503c\u79f0\u4e3a\u8868\u8fbe\u5f0f\u7684 \u7ed3\u679c \u3002 \u901a\u8fc7\u5728\u8868\u8fbe\u5f0f\u7ed3\u5c3e\u6dfb\u52a0\u5206\u53f7\uff0c\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a \u8868\u8fbe\u5f0f\u8bed\u53e5 \u3002 \u7f16\u7a0b\u4e0d\u662f\u4ef6\u5bb9\u6613\u4e8b\uff0c\u4f60\u7684\u7a0b\u5e8f\u7684\u7b2c\u4e00\u4e2a\u7248\u672c\u4e0d\u53ef\u80fd\u662f\u5b8c\u7f8e\u7684\u3002\u8ba9\u7a0b\u5e8f\u80fd\u591f\u8fd0\u884c\u8d77\u6765\uff0c\u662f\u7f16\u7a0b\u7684\u9996\u8981\u4efb\u52a1\uff0c\u7136\u540e\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f18\u5316\u4f7f\u5176\u53d8\u5f97\u66f4\u597d\u3002","title":"1.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - C++\u57fa\u7840"},{"location":"1-x-Chapter-1-summary-and-quiz/#_1","text":"\u8bed\u53e5 \u662f\u4e00\u79cd\u6307\u4ee4\uff0c\u8ba9\u7a0b\u5e8f\u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\u3002\u8bed\u53e5\u901a\u5e38\u4ee5\u5206\u53f7\u7ed3\u5c3e\u3002 \u51fd\u6570 \u662f\u4e00\u7ec4\u5e8f\u5217\u6267\u884c\u7684\u8bed\u53e5\u3002\u6bcf\u4e2a C++\u7a0b\u5e8f\u90fd\u5fc5\u987b\u5305\u542b\u4e00\u4e2a main \u51fd\u6570\u3002\u5f53\u4f60\u6267\u884c\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u9996\u5148\u4f1a\u4ece main \u51fd\u6570\u5f00\u59cb\u6267\u884c\u3002 \u8fd9\u4e9b\u63a7\u5236 C++ \u5143\u7d20\u7ec4\u7ec7\u5f62\u5f0f\u7684\u89c4\u5219\uff0c\u79f0\u4e3a \u8bed\u6cd5 \u3002\u5f53\u4f60\u8fdd\u53cd\u8bed\u8a00\u7684\u8bed\u6cd5\u89c4\u5219\u65f6\uff0c\u5c31\u4f1a\u4ea7\u751f\u8bed\u6cd5\u9519\u8bef\u3002 \u6ce8\u91ca \u5141\u8bb8\u4f60\u5728\u4ee3\u7801\u4e2d\u7559\u4e0b\u7b14\u8bb0\u3002C++ \u652f\u6301\u4e24\u79cd\u7c7b\u578b\u7684\u6ce8\u91ca\u3002\u884c\u5185\u6ce8\u91ca\u4ee5 // \u5f00\u5934\u3002\u6bb5\u843d\u6ce8\u91ca\u5219\u4ee5 /* \u5f00\u5934\uff0c\u4ee5 */ \u7ed3\u675f\u3002\u8bf7\u4e0d\u8981\u5d4c\u5957\u6ce8\u91ca\u3002 \u4f60\u53ef\u4ee5\u901a\u8fc7\u6ce8\u91ca\u6765\u4e34\u65f6\u5c4f\u853d\u4e00\u884c\u6216\u4e00\u6bb5\u4ee3\u7801\uff0c\u4e0a\u8ff0\u64cd\u4f5c\u79f0\u4e3a\u6ce8\u91ca\u6389\uff08commenting out\uff09\u4ee3\u7801\u3002 \u6570\u636e \u662f\u4e00\u7cfb\u5217\u7b26\u53f7\u5e76\u53ef\u4ee5\u88ab\u89e3\u6790\u4e3a\u67d0\u79cd\u542b\u4e49\u3002\u4e00\u6bb5\u88ab\u5b58\u653e\u5728\u5185\u5b58\u4e2d\u7684\u6570\u636e\u79f0\u4e3a \u503c \u3002 \u53d8\u91cf \u662f\u4e00\u6bb5\u547d\u540d\u7684\u5185\u5b58\uff0c\u73b0\u5728\u6211\u4eec\u4fbf\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5b58\u653e\u503c\u4e86\u3002\u53d8\u91cf\u7684\u540d\u79f0\u4e3a \u6807\u8bc6\u7b26(identifier) \u3002\u521b\u5efa\u53d8\u91cf\u9700\u8981\u901a\u8fc7\u58f0\u660e\u8bed\u53e5\u3002\u5f53\u7a0b\u5e8f\u5f00\u59cb\u8fd0\u884c\u7684\u65f6\u5019\uff0c\u53d8\u91cf\u5c31\u4f1a\u88ab \u5b9e\u4f8b\u5316 \uff0c\u5373\u5b83\u4f1a\u88ab\u5173\u8054\u5230\u67d0\u4e2a\u5185\u5b58\u5730\u5740 \u3002 \u6570\u636e\u7c7b\u578b \u53ef\u4ee5\u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u4f55\u5c06\u4e00\u6bb5\u6570\u636e\u89e3\u91ca\u4e3a\u5177\u6709\u542b\u4e49\u7684\u503c\u3002\u6574\u578b\u6307\u7684\u662f\u4e0d\u5305\u542b\u5c0f\u6570\u90e8\u5206\u7684\u503c\uff0c\u4f8b\u5982 4\u300127\u30010\u3001-2 \u6216-12\u3002 \u62f7\u8d1d\u8d4b\u503c (\u901a\u8fc7 operator= ) \u53ef\u4ee5\u88ab\u7528\u6765\u5411\u67d0\u4e2a\u5df2\u7ecf\u88ab\u521b\u5efa\u7684\u53d8\u91cf\u8d4b\u503c\u3002 \u521d\u59cb\u5316 \u7528\u4e8e\u5728\u53d8\u91cf\u88ab\u521b\u5efa\u7684\u540c\u65f6\u4e3a\u5176\u6307\u5b9a\u3002C++ \u652f\u6301\u4e09\u79cd\u7c7b\u578b\u7684\u521d\u59cb\u5316\uff1a \u62f7\u8d1d\u521d\u59cb\u5316 \u3001 \u76f4\u63a5\u521d\u59cb\u5316 \u548c \u7edf\u4e00\u521d\u59cb\u5316 \uff08\u4e5f\u79f0\u4e3a \u5217\u8868\u521d\u59cb\u5316 \u6216 \u62ec\u53f7\u521d\u59cb\u5316 \uff09\u3002 \u4f60\u5e94\u8be5\u4f18\u5148\u4f7f\u7528\u7edf\u4e00\u521d\u59cb\u5316\u800c\u4e0d\u662f\u5176\u4ed6\u521d\u59cb\u5316\u65b9\u5f0f\uff0c\u6b64\u5916\uff0c\u4f7f\u7528\u521d\u59cb\u5316\u4e5f\u4f18\u4e8e\u8d4b\u503c\u3002 \u5c3d\u7ba1\u5728\u4e00\u4e2a\u8bed\u53e5\u4e2d\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\uff0c\u4f46\u662f\u6700\u597d\u8fd8\u662f\u6bcf\u884c\u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cf\u3002 std:: cout \u548c operator<< \u53ef\u4ee5\u5c06\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u4f5c\u4e3a\u6587\u672c\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002 std:: endl \u4f1a\u8f93\u51fa\u4e00\u4e2a\u6362\u884c\u7b26\uff0c\u63a7\u5236\u63a7\u5236\u53f0\u5149\u6807\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\u3002 std:: cin \u548c operator>> \u53ef\u4ee5\u4ece\u952e\u76d8\u83b7\u53d6\u8f93\u5165\u503c\u3002 \u6ca1\u6709\u88ab\u6307\u5b9a\u503c\u7684\u53d8\u91cf\uff0c\u79f0\u4e3a\u672a\u521d\u59cb\u5316\u53d8\u91cf\u3002\u5c1d\u8bd5\u83b7\u53d6\u672a\u521d\u59cb\u5316\u53d8\u91cf\u7684\u503c\uff0c\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \uff0c\u672a\u5b9a\u4e49\u884c\u4e3a\u6709\u591a\u79cd\u8868\u73b0\u5f62\u5f0f\u3002 C++ \u9884\u7559\u4e86\u4e00\u7ec4\u540d\u79f0\uff0c\u79f0\u4e3a \u5173\u952e\u5b57 \u3002\u8fd9\u4e9b\u5173\u952e\u5b57\u5728 C++\u4e2d\u5177\u6709\u7279\u6b8a\u7684\u542b\u4e49\uff0c\u4e0d\u80fd\u88ab\u7528\u4f5c\u53d8\u91cf\u540d\u3002 \u5b57\u9762\u91cf \u6307\u7684\u662f\u76f4\u63a5\u63d2\u5165\u6e90\u7801\u4e2d\u7684\u56fa\u5b9a\u503c\u3002\u4f8b\u5982 5 \u548c \u201cHello world!\u201d\u3002 \u8fd0\u7b97\u6307\u7684\u662f\u4e00\u6b21\u6570\u5b66\u8ba1\u7b97\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u6d89\u53ca\u52300\u4e2a\u6216\u591a\u4e2a\u8f93\u5165\u503c\uff0c\u79f0\u4e3a \u64cd\u4f5c\u6570 \u3002\u5177\u4f53\u7684\u8fd0\u7b97\u901a\u8fc7\u8fd0\u7b97\u7b26\u6765\u6307\u5b9a\u3002\u8fd0\u7b97\u7684\u7ed3\u679c\u4f1a\u4ea7\u751f\u4e00\u4e2a\u8f93\u51fa\u503c\u3002 \u4e00\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e00\u4e2a\u64cd\u4f5c\u6570\u3002 \u4e8c\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e24\u4e2a\u64cd\u4f5c\u6570\uff0c\u901a\u5e38\u79f0\u4e3a\u5de6\u64cd\u4f5c\u6570\u548c\u53f3\u64cd\u4f5c\u6570\u3002 \u4e09\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e09\u4e2a\u64cd\u4f5c\u6570\u3002 \u8868\u8fbe\u5f0f \u662f\u4e00\u7ec4\u5b57\u9762\u91cf\u3001\u53d8\u91cf\u3001\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u8c03\u7528\u7684\u7ec4\u5408\uff0c\u5b83\u7ecf\u8fc7\u6c42\u503c\u540e\uff0c\u4f1a\u5f97\u5230\u4e00\u4e2a\u5355\u4e00\u503c\u3002\u8ba1\u7b97\u5f97\u5230\u8be5\u5355\u4e00\u503c\u7684\u8fc7\u7a0b\u79f0\u4e4b\u4e3a \u6c42\u503c(evaluation) \uff0c\u800c\u8be5\u503c\u79f0\u4e3a\u8868\u8fbe\u5f0f\u7684 \u7ed3\u679c \u3002 \u901a\u8fc7\u5728\u8868\u8fbe\u5f0f\u7ed3\u5c3e\u6dfb\u52a0\u5206\u53f7\uff0c\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a \u8868\u8fbe\u5f0f\u8bed\u53e5 \u3002 \u7f16\u7a0b\u4e0d\u662f\u4ef6\u5bb9\u6613\u4e8b\uff0c\u4f60\u7684\u7a0b\u5e8f\u7684\u7b2c\u4e00\u4e2a\u7248\u672c\u4e0d\u53ef\u80fd\u662f\u5b8c\u7f8e\u7684\u3002\u8ba9\u7a0b\u5e8f\u80fd\u591f\u8fd0\u884c\u8d77\u6765\uff0c\u662f\u7f16\u7a0b\u7684\u9996\u8981\u4efb\u52a1\uff0c\u7136\u540e\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f18\u5316\u4f7f\u5176\u53d8\u5f97\u66f4\u597d\u3002","title":"\u7ae0\u8282\u56de\u987e"},{"location":"10-1-Introduction-to-program-defined-user-defined-types/","tags":["type"],"text":"Key Takeaway \u81ea\u5b9a\u4e49\u7c7b\u578b\u6307\u4ee3\u6240\u6709\u975eC++\u6838\u5fc3\u7c7b\u578b\uff0c\u5305\u62ec\u6807\u51c6\u5e93 \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u624d\u6307\u4ee3\u7f16\u5199\u5f53\u524d\u7a0b\u5e8f\u65f6\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u578b \u7c7b\u578b\u5b9a\u4e49\u5728\u67d0\u79cd\u7a0b\u5ea6\u4e0a\u662f\u4e0d\u53d7\u9650\u4e8e\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u7684\uff1a\u7c7b\u578b\u7684\u5b9a\u4e49\u53ef\u4ee5\u51fa\u73b0\u5728\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\u3002\u56e0\u4e3a\u7f16\u8bd1\u5668\u5fc5\u987b\u770b\u5230\u5b8c\u6574\u7c7b\u578b\u5b9a\u4e49\u624d\u80fd\u5de5\u4f5c\uff08\u786e\u5b9a\u5185\u5b58\u5206\u914d\u5927\u5c0f\uff09\u3002\u4f46\u662f\u540c\u4e00\u4e2a\u6587\u4ef6\u4e2d\u53ea\u80fd\u51fa\u73b0\u4e00\u6b21\uff0c\u8fd9\u4e2a\u662f\u6c38\u6052\u7684\u771f\u7406 \u57fa\u672c\u6570\u636e\u7c7b\u578b\u662f C++ \u8bed\u8a00\u7684\u6838\u5fc3\u90e8\u5206\uff0c\u8bbe\u8ba1\u5b83\u4eec\u7684\u76ee\u7684\u5c31\u662f\u4e3a\u4e86\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u62ff\u6765\u5c31\u7528\u3002\u4f8b\u5982\uff0c\u5f53\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a int \u6216\u8005 double \u7c7b\u578b\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53ea\u9700\u8981\uff1a 1 2 int x ; // define variable of fundamental type 'int' double d ; // define variable of fundamental type 'double' \u5bf9\u4e8e\u90a3\u4e9b\u7531\u57fa\u672c\u6570\u636e\u7c7b\u578b\u901a\u8fc7\u7b80\u5355\u6269\u5c55\u800c\u5f97\u5230\u7684 \u590d\u5408\u6570\u636e\u7c7b\u578b \u4e5f\u662f\u4e00\u6837\u7684\uff08\u5305\u62ec\u51fd\u6570\u3001\u6307\u9488\u3001\u5f15\u7528\u548c\u6570\u7ec4\uff09\uff1a 1 2 3 4 void fcn ( int ) {}; // define a function of type void()(int) int * ptr ; // define variable of compound type 'pointer to int' int & ref ; // define variable of compound type 'reference to int' int arr [ 5 ]; // define an array of 5 integers of type int[5] (we'll cover this in a future chapter) \u8fd9\u4e9b\u7ec4\u5408\u53ef\u4ee5\u6b63\u786e\u5de5\u4f5c\u662f\u56e0\u4e3aC++\u8bed\u8a00\u5df2\u7ecf\u77e5\u6653\u8fd9\u4e9b\u6570\u636e\u7c7b\u578b\u540d\uff08\u548c\u7b26\u53f7\uff09\u7684\u542b\u4e49\u2014\u2014\u56e0\u6b64\u6211\u4eec\u4e0d\u5fc5\u63d0\u4f9b\u6216\u5bfc\u5165\u5176\u5b9a\u4e49\u3002 \u800c\u5bf9\u4e8e\u4f7f\u7528\u7c7b\u578b\u522b\u540d\uff08 8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d \uff09\u7684\u60c5\u51b5\u6765\u8bf4\uff0c\u7c7b\u578b\u522b\u540d\u4f7f\u6211\u4eec\u53ef\u4ee5\u4e3a\u4e00\u4e2a\u5df2\u77e5\u7c7b\u578b\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u540d\u5b57\uff0c\u56e0\u6b64\u7c7b\u578b\u522b\u540d\u5b9e\u9645\u4e0a\u5411\u7a0b\u5e8f\u5f15\u5165\u4e86\u4e00\u4e2a\u65b0\u7684\u6807\u8bc6\u7b26\uff0c\u6240\u4ee5\u7c7b\u578b\u522b\u540d\u5fc5\u987b\u5148\u5b9a\u4e49\u540e\u4f7f\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include using length = int ; // define a type alias with identifier 'length' int main () { length x { 5 }; // we can use 'length' here since we defined it above std :: cout << x ; return 0 ; } \u5982\u679c\u6ca1\u6709\u5bf9 length \u5b9a\u4e49\uff0c\u7f16\u8bd1\u5668\u5c06\u65e0\u6cd5\u77e5\u9053\u5728\u6211\u4eec\u4f7f\u7528\u5b83\u5b9a\u4e49\u53d8\u91cf\u65f6\u77e5\u6653\u5176\u542b\u4e49\u3002\u5bf9 length \u7684\u5b9a\u4e49\u5e76\u6ca1\u6709\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\u2014\u2014\u5b83\u53ea\u662f\u544a\u8bc9\u7f16\u8bd1\u5668 length \u4f60\u7a0d\u540e\u53ef\u4ee5\u4f7f\u7528\u5b83\u5b9a\u4e49\u53d8\u91cf\u3002 \u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u662f\u4ec0\u4e48\uff1f \u5728 9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b \u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u66fe\u7ecf\u4ecb\u7ecd\u8fc7\u5982\u4f55\u5b58\u50a8\u4e00\u4e2a\u5206\u6570\u3002\u5bf9\u4e8e\u5206\u6570\u6765\u8bf4\uff0c\u5b83\u5305\u62ec\u5206\u5b50\u548c\u5206\u6bcd\u4e24\u4e2a\u90e8\u5206\uff0c\u800c\u4e14\u5b83\u4eec\u4e4b\u95f4\u662f\u5b58\u5728\u6982\u5ff5\u6027\u8054\u7cfb\u7684\u3002\u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba\u4f7f\u7528\u4e24\u4e2a\u6574\u578b\u5206\u522b\u8868\u793a\u5206\u5b50\u5206\u6bcd\u53ef\u80fd\u5e26\u6765\u7684\u95ee\u9898\u3002 \u5982\u679cC++\u6709\u80fd\u591f\u8868\u793a\u5206\u6570\u7684\u5185\u5efa\u7c7b\u578b\u5c31\u597d\u4e86\u2014\u2014\u4f46\u53ef\u60dc\u5e76\u6ca1\u6709\u3002\u8fd8\u6709\u6570\u767e\u79cd\u5176\u4ed6\u53ef\u80fd\u6709\u7528\u7684\u7c7b\u578bC++\u6ca1\u6709\u5305\u542b\uff0c\u56e0\u4e3a\u5b83\u4e0d\u53ef\u80fd\u9884\u6d4b\u4eba\u4eec\u53ef\u80fd\u9700\u8981\u7684\u6240\u6709\u4e1c\u897f(\u66f4\u4e0d\u7528\u8bf4\u5b9e\u73b0\u548c\u6d4b\u8bd5\u90a3\u4e9b\u4e1c\u897f\u4e86)\u3002 \u76f8\u53cd\uff0cC++\u63d0\u4f9b\u4e86\u89e3\u51b3\u8be5\u95ee\u9898\u7684\u53e6\u5916\u4e00\u79cd\u9014\u5f84\uff1a\u5141\u8bb8\u6211\u4eec\u5728\u7a0b\u5e8f\u4e2d\u521b\u5efa\u5168\u65b0\u7684\u81ea\u5b9a\u4e49\u7c7b\u578b\uff01\u8fd9\u6837\u7684\u7c7b\u578b\u901a\u5e38\u79f0\u4e3a\u7528\u6237\u5b9a\u4e49\u7c7b\u578b(\u5c3d\u7ba1\u6211\u4eec\u8ba4\u4e3a\u672f\u8bed\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u66f4\u597d\u2014\u2014\u6211\u4eec\u5c06\u5728\u672c\u8bfe\u540e\u9762\u8ba8\u8bba\u5176\u533a\u522b)\u3002C++\u6709\u4e24\u7c7b\u590d\u5408\u7c7b\u578b\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a\u679a\u4e3e\u7c7b\u578b(\u5305\u62ec \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u548c \u6709\u4f5c\u7528\u57df\u679a\u4e3e )\u548c\u7c7b\u7c7b\u578b(\u5305\u62ec\u7ed3\u6784\u4f53\u3001\u7c7b\u548c\u8054\u5408\u4f53)\u3002 \u5b9a\u4e49\u4e00\u4e2a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b \u5c31\u50cf \u7c7b\u578b\u522b\u540d \u4e00\u6837\uff0c\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u4e5f\u5fc5\u987b\u5148\u5b9a\u4e49\u540e\u4f7f\u7528\u3002\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7684\u5b9a\u4e49\u79f0\u4e3a\u7c7b\u578b\u5b9a\u4e49\u3002 \u5c3d\u7ba1\u7ed3\u6784\u4f53\u8fd8\u5c1a\u672a\u4ecb\u7ecd\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u5148\u4ece\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u4e86\u89e3\u5230\uff0c\u81ea\u5b9a\u4e49\u7c7b\u578b Fraction \u548c\u8be5\u7c7b\u578b\u5bf9\u8c61\u662f\u5982\u4f55\u521d\u59cb\u5316\u7684\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // Define a program-defined type named Fraction so the compiler understands what a Fraction is // (we'll explain what a struct is and how to use them later in this chapter) // This only defines what a Fraction type looks like, it doesn't create one struct Fraction { int numerator {}; int denominator {}; }; // Now we can make use of our Fraction type int main () { Fraction f { 3 , 4 }; // this actually instantiates a Fraction object named f return 0 ; } \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 struct \u5173\u952e\u5b57\u6765\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a Fraction \u7684\u65b0\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b(\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\uff0c\u56e0\u6b64\u5b83\u53ef\u4ee5\u5728\u6587\u4ef6\u7684\u5176\u4ed6\u5730\u65b9\u4f7f\u7528)\u3002\u8fd9\u4e0d\u4f1a\u5206\u914d\u4efb\u4f55\u5185\u5b58\u2014\u2014\u5b83\u53ea\u662f\u544a\u8bc9\u7f16\u8bd1\u5668 Fraction \u662f\u4ec0\u4e48\u6837\u5b50\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u7a0d\u540e\u518d\u4e3a Fraction \u7c7b\u578b\u7684\u5bf9\u8c61\u5206\u914d\u5185\u5b58\u3002\u7136\u540e\uff0c\u5728 main() \u5185\u90e8\uff0c\u5b9e\u4f8b\u5316(\u5e76\u521d\u59cb\u5316)\u4e00\u4e2a\u540d\u4e3a f \u7684 Fraction \u7c7b\u578b\u53d8\u91cf\u3002 \u7c7b\u578b\u5b9a\u4e49\u603b\u662f\u4ee5\u5206\u53f7\u7ed3\u5c3e\u3002\u6ca1\u6709\u5728\u7c7b\u578b\u5b9a\u4e49\u7684\u672b\u5c3e\u5305\u542b\u5206\u53f7\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u7a0b\u5e8f\u5458\u9519\u8bef\uff0c\u800c\u4e14\u8fd9\u4e2a\u9519\u8bef\u5f88\u96be\u8c03\u8bd5\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u5728\u7c7b\u578b\u5b9a\u4e49\u540e\u9762\u7684\u884c\u62a5\u9519\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u4ece\u4e0a\u9762\u4f8b\u5b50\u7684 Fraction \u5b9a\u4e49(\u7b2c8\u884c)\u672b\u5c3e\u5220\u9664\u5206\u53f7\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u56e0\u4e3a main() \u7684\u5b9a\u4e49(\u7b2c11\u884c)\u62a5\u9519\u3002 \u6ce8\u610f \u4e0d\u8981\u5fd8\u8bb0\u4ee5\u5206\u53f7\u7ed3\u675f\u7c7b\u578b\u5b9a\u4e49\uff0c\u5426\u5219\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u5728\u4e0b\u4e00\u884c\u4ee3\u7801\u4e2d\u51fa\u9519\u3002 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe( 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u4e2d\u5c55\u793a\u66f4\u591a\u5b9a\u4e49\u548c\u4f7f\u7528\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7684\u4f8b\u5b50\uff0c\u5e76\u4e14\u6211\u4eec\u5c06\u4ece 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 \u5f00\u59cb\u4ecb\u7ecd\u7ed3\u6784\u4f53\u3002 \u547d\u540d\u4e00\u4e2a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b \u6309\u7167\u60ef\u4f8b\uff0c\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u540d\u5b57\u5e94\u8be5\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff0c\u800c\u4e14\u4e0d\u8981\u6dfb\u52a0\u4efb\u4f55\u7684\u540e\u7f00(\u4f8b\u5982\uff1a Fraction \uff0c\u800c\u4e0d\u662f fraction \uff0c fraction_t \u6216\u8005 Fraction_t )\u3002 \u6700\u4f73\u5b9e\u8df5 \u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u59cb\u547d\u540d\u81ea\u5b9a\u4e49\u7c7b\u578b\uff0c\u4e0d\u8981\u4f7f\u7528\u540e\u7f00\u3002 \u7531\u4e8e\u7c7b\u578b\u540d\u548c\u53d8\u91cf\u540d\u4e4b\u95f4\u7684\u76f8\u4f3c\u6027\uff0c\u65b0\u624b\u7a0b\u5e8f\u5458\u65f6\u5e38\u4f1a\u89c9\u5f97\u4e0b\u9762\u8fd9\u6837\u7684\u53d8\u91cf\u5b9a\u4e49\u4ee4\u4eba\u56f0\u60d1: 1 Fraction fraction {}; // \u5b9e\u4f8b\u5316\u4e00\u4e2a\u540d\u4e3a fraction \u7684 Fraction \u7c7b\u578b\u7684\u5bf9\u8c61 \u4f46\u8fd9\u4e0e\u5176\u4ed6\u7c7b\u578b\u7684\u53d8\u91cf\u5b9a\u4e49\u5e76\u6ca1\u6709\u4ec0\u4e48\u533a\u522b\uff1a\u7c7b\u578b( Fraction )\u5728\u524d(\u56e0\u4e3a Fraction \u662f\u5927\u5199\u7684\uff0c\u6211\u4eec\u77e5\u9053\u5b83\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7c7b\u578b)\uff0c\u7136\u540e\u662f\u53d8\u91cf\u540d( fraction )\uff0c\u7136\u540e\u662f\u4e00\u4e2a\u53ef\u9009\u7684\u521d\u59cb\u5316\u503c\u3002\u56e0\u4e3aC++\u662f\u533a\u5206\u5927\u5c0f\u5199\u7684\uff0c\u6240\u4ee5\u8fd9\u91cc\u4e0d\u5b58\u5728\u547d\u540d\u51b2\u7a81! \u5728\u591a\u6587\u4ef6\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b \u6bcf\u4e2a\u4f7f\u7528\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7684\u4ee3\u7801\u6587\u4ef6\u5728\u4f7f\u7528\u4e4b\u524d\u90fd\u9700\u8981\u770b\u5230\u5b8c\u6574\u7684\u7c7b\u578b\u5b9a\u4e49\u3002 \u524d\u5411\u58f0\u660e \u662f\u4e0d\u591f\u7684\u3002\u770b\u5230\u5b8c\u6574\u5b9a\u4e49\u662f\u5fc5\u9700\u7684\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u9700\u8981\u77e5\u9053\u8981\u4e3a\u8be5\u7c7b\u578b\u7684\u5bf9\u8c61\u5206\u914d\u591a\u5c11\u5185\u5b58\u3002 \u4e3a\u4e86\u80fd\u5c06\u5b9a\u4e49\u5f15\u5165\u9700\u8981\u4f7f\u7528\u8be5\u7c7b\u578b\u5b9a\u4e49\u7684\u6587\u4ef6\uff0c\u81ea\u5b9a\u4e49\u7c7b\u578b\u901a\u5e38\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u4f7f\u7528 #included \u5bfc\u5165\u4efb\u4f55\u9700\u8981\u8be5\u5b9a\u4e49\u7684\u6587\u4ef6\u3002\u8fd9\u4e9b\u5934\u6587\u4ef6\u901a\u5e38\u548c\u7c7b\u578b\u5177\u6709\u76f8\u540c\u7684\u540d\u5b57\uff08\u4f8b\u5982 Fraction \u5e94\u8be5\u5b9a\u4e49\u5728 Fraction.h \u4e2d\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u53ea\u5728\u4e00\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\u4f7f\u7528\u7684\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u5e94\u8be5\u5728\u8be5\u4ee3\u7801\u6587\u4ef6\u4e2d\u5c3d\u53ef\u80fd\u9760\u8fd1\u7b2c\u4e00\u4e2a\u4f7f\u7528\u70b9\u5b9a\u4e49\u3002 \u5728\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\u4f7f\u7528\u7684\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u5e94\u8be5\u5728\u4e0e\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u540c\u540d\u7684\u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\uff0c\u7136\u540e\u6839\u636e\u9700\u8981\u5728\u4ee3\u7801\u6587\u4ef6\u4e2d\u4f7f\u7528 #include \u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5982\u679c\u6211\u4eec\u628a\u6211\u4eec\u7684 Fraction \u7c7b\u578b\u79fb\u52a8\u5230\u4e00\u4e2a\u5934\u6587\u4ef6(\u540d\u4e3a Fraction.h )\uff0c\u8fd9\u6837\u5b83\u5c31\u53ef\u4ee5\u5305\u542b\u5728\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d: Fraction.h 1 2 3 4 5 6 7 8 9 10 11 12 13 #ifndef FRACTION_H #define FRACTION_H // Define a new type named Fraction // This only defines what a Fraction looks like, it doesn't create one // Note that this is a full definition, not a forward declaration struct Fraction { int numerator {}; int denominator {}; }; #endif Fraction.cpp 1 2 3 4 5 6 7 8 9 #include \"Fraction.h\" // include our Fraction definition in this code file // Now we can make use of our Fraction type int main () { Fraction f { 3 , 4 }; // this actually creates a Fraction object named f return 0 ; } \u7c7b\u578b\u5b9a\u4e49\u90e8\u5206\u4e0a\u8c41\u514d\u4e8e\u5355\u4e00\u5b9a\u4e49\u539f\u5219 \u5728\u8bfe\u7a0b 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 \u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \uff0c\u8be5\u89c4\u5219\u8981\u6c42\u6bcf\u4e2a\u51fd\u6570\u548c\u5168\u5c40\u53d8\u91cf\u5728 \u6bcf\u4e2a\u7a0b\u5e8f \u4e2d\u53ea\u6709\u4e00\u4e2a\u5b9a\u4e49\u3002\u8981\u5728\u4e0d\u5305\u542b\u5b9a\u4e49\u7684\u6587\u4ef6\u4e2d\u4f7f\u7528\u7ed9\u5b9a\u51fd\u6570\u6216\u5168\u5c40\u53d8\u91cf\uff0c\u9700\u8981 \u524d\u5411\u58f0\u660e (\u901a\u5e38\u901a\u8fc7\u5934\u6587\u4ef6\u5b9e\u73b0)\u3002\u8fd9\u662f\u53ef\u884c\u7684\uff0c\u56e0\u4e3a\u5f53\u6d89\u53ca\u5230\u51fd\u6570\u548c\u975econstexpr\u53d8\u91cf\u65f6\uff0c\u7f16\u8bd1\u5668\u53ea\u9700\u8981\u58f0\u660e\u5c31\u53ef\u4ee5 \uff0c\u7136\u540e\u94fe\u63a5\u5668\u4f1a\u5c06\u6240\u9700\u7684\u4e1c\u897f\u90fd\u94fe\u63a5\u8d77\u6765\u3002 \u4f46\u662f\uff0c\u8fd9\u4e2a\u505a\u6cd5\u5e76\u4e0d\u9002\u7528\u4e8e\u7c7b\u578b\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u901a\u5e38\u9700\u8981\u770b\u5230\u5b8c\u6574\u7684\u7c7b\u578b\u5b9a\u4e49\u624d\u80fd\u4f7f\u7528\u7ed9\u5b9a\u7684\u7c7b\u578b\u3002\u6211\u4eec\u5fc5\u987b\u80fd\u591f\u5c06\u5b8c\u6574\u7684\u7c7b\u578b\u5b9a\u4e49\u5bfc\u5165\u5230\u6bcf\u4e2a\u9700\u8981\u5b83\u7684\u4ee3\u7801\u6587\u4ef6\u3002 \u4e3a\u6b64\uff0c \u7c7b\u578b\u5b9a\u4e49\u5728\u67d0\u79cd\u7a0b\u5ea6\u4e0a\u662f\u4e0d\u53d7\u9650\u4e8e\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u7684\uff1a\u7c7b\u578b\u7684\u5b9a\u4e49\u53ef\u4ee5\u51fa\u73b0\u5728\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\u3002 \u5176\u5b9e\u4f60\u5df2\u7ecf\u5728\u4e0d\u7ecf\u610f\u4e4b\u95f4\u8fd9\u4e48\u505a\u4e86\uff1a\u5982\u679c\u7a0b\u5e8f\u6709\u4e24\u4e2a\u4ee3\u7801\u6587\u4ef6\uff0c\u4e14\u90fd #include \uff0c\u90a3\u4e48\u6211\u4eec\u5b9e\u9645\u4e0a\u5c06\u6240\u6709\u8f93\u5165/\u8f93\u51fa\u7c7b\u578b\u5b9a\u4e49\u90fd\u5bfc\u5165\u5230\u4e86\u8fd9\u4e24\u4e2a\u6587\u4ef6\u4e2d\u3002 \u6709\u4e24\u70b9\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0c\u7c7b\u578b\u5b9a\u4e49\u5728\u540c\u4e00\u4e2a\u6587\u4ef6\u4e2d\u53ea\u80fd\u51fa\u73b0\u4e00\u6b21(\u8fd9\u901a\u5e38\u4e0d\u662f\u95ee\u9898\uff0c\u56e0\u4e3a \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e \u53ef\u4ee5\u9632\u6b62\u8be5\u95ee\u9898\u51fa\u73b0)\u3002\u5176\u6b21\uff0c\u7ed9\u5b9a\u7c7b\u578b\u7684\u6240\u6709\u7c7b\u578b\u5b9a\u4e49\u5fc5\u987b\u76f8\u540c\uff0c\u5426\u5219\u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u547d\u540d\u6cd5\uff1a\u7528\u6237\u5b9a\u4e49\u7c7b\u578b vs \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b \u672f\u8bed\u201c\u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u201d\uff08\u81ea\u5b9a\u4e49\u7c7b\u578b\uff09\u6709\u65f6\u4f1a\u51fa\u73b0\u5728\u65e5\u5e38\u5bf9\u8bdd\u4e2d\uff0c\u4e5f\u4f1a\u5728C++\u8bed\u8a00\u6807\u51c6\u4e2d\u63d0\u5230(\u4f46\u6ca1\u6709\u5b9a\u4e49)\u3002\u5728\u975e\u6b63\u5f0f\u7684\u5bf9\u8bdd\u4e2d\uff0c\u8fd9\u4e2a\u672f\u8bed\u5f80\u5f80\u610f\u5473\u7740\u201c\u4f60\u81ea\u5df1\u5b9a\u4e49\u7684\u7c7b\u578b\u201d(\u5982\u4e0a\u9762\u7684\u5206\u6570\u7c7b\u578b\u4f8b\u5b50)\u3002 \u6709\u65f6\u8fd9\u4e5f\u5305\u62ec\u7c7b\u578b\u522b\u540d \u3002 \u7136\u800c\uff0c\u5728C++\u8bed\u8a00\u6807\u51c6\u7684\u884c\u6587\u8bed\u5883\u4e2d\uff0c\u4efb\u4f55\u6ca1\u6709\u5b9a\u4e49\u4e3a\u6838\u5fc3C++\u8bed\u8a00\u4e00\u90e8\u5206\u7684\u7c7b\u578b(\u6362\u53e5\u8bdd\u8bf4\uff0c\u975e\u57fa\u672c\u7c7b\u578b)\u90fd\u662f\u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u3002\u56e0\u6b64\uff0c\u5728C++\u6807\u51c6\u5e93\u4e2d\u5b9a\u4e49\u7684\u7c7b\u578b(\u4f8b\u5982 std::string )\u7406\u8bba\u4e0a\u4e5f\u7b97\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u5c31\u50cf\u4f60\u81ea\u5df1\u5b9a\u4e49\u7684\u4efb\u4f55\u7c7b\u578b\u4e00\u6837\u3002 \u4e3a\u4e86\u533a\u522b\uff0cC++ 20\u8bed\u8a00\u6807\u51c6\u5c06\u672f\u8bed\u201c\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u201d\u5b9a\u4e49\u4e3a\u4ec5\u6307\u4ee3\u4f60\u81ea\u5df1\u5b9a\u4e49\u7684\u7c7b\u578b\u3002\u5f53\u6211\u4eec\u8ba8\u8bba\u8fd9\u4e9b\u7c7b\u578b\u65f6\uff0c\u66f4\u559c\u6b22\u4f7f\u7528\u8fd9\u4e2a\u672f\u8bed\uff0c\u56e0\u4e3a\u5b83\u66f4\u52a0\u7cbe\u786e\u3002 \u7c7b\u578b \u542b\u4e49 \u4f8b\u5b50 \u57fa\u672c\u7c7b\u578b \u5185\u5efa\u4e8e C++ \u8bed\u8a00\u7684\u6838\u5fc3\u90e8\u5206 int , std::nullptr_t \u7528\u6237\u5b9a\u4e49\u7c7b\u578b \u975e\u57fa\u7840\u6570\u636e\u7c7b\u578b(\u975e\u6b63\u5f0f\u573a\u5408\u6307\u4ee3\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b) std::string , Fraction \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b \u7528\u6237\u81ea\u5b9a\u4e49\u7684\u7c7b\u6216\u679a\u4e3e\u7c7b\u578b Fraction","title":"10.1 - \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7b80\u4ecb"},{"location":"10-1-Introduction-to-program-defined-user-defined-types/#_1","text":"\u5728 9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b \u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u66fe\u7ecf\u4ecb\u7ecd\u8fc7\u5982\u4f55\u5b58\u50a8\u4e00\u4e2a\u5206\u6570\u3002\u5bf9\u4e8e\u5206\u6570\u6765\u8bf4\uff0c\u5b83\u5305\u62ec\u5206\u5b50\u548c\u5206\u6bcd\u4e24\u4e2a\u90e8\u5206\uff0c\u800c\u4e14\u5b83\u4eec\u4e4b\u95f4\u662f\u5b58\u5728\u6982\u5ff5\u6027\u8054\u7cfb\u7684\u3002\u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba\u4f7f\u7528\u4e24\u4e2a\u6574\u578b\u5206\u522b\u8868\u793a\u5206\u5b50\u5206\u6bcd\u53ef\u80fd\u5e26\u6765\u7684\u95ee\u9898\u3002 \u5982\u679cC++\u6709\u80fd\u591f\u8868\u793a\u5206\u6570\u7684\u5185\u5efa\u7c7b\u578b\u5c31\u597d\u4e86\u2014\u2014\u4f46\u53ef\u60dc\u5e76\u6ca1\u6709\u3002\u8fd8\u6709\u6570\u767e\u79cd\u5176\u4ed6\u53ef\u80fd\u6709\u7528\u7684\u7c7b\u578bC++\u6ca1\u6709\u5305\u542b\uff0c\u56e0\u4e3a\u5b83\u4e0d\u53ef\u80fd\u9884\u6d4b\u4eba\u4eec\u53ef\u80fd\u9700\u8981\u7684\u6240\u6709\u4e1c\u897f(\u66f4\u4e0d\u7528\u8bf4\u5b9e\u73b0\u548c\u6d4b\u8bd5\u90a3\u4e9b\u4e1c\u897f\u4e86)\u3002 \u76f8\u53cd\uff0cC++\u63d0\u4f9b\u4e86\u89e3\u51b3\u8be5\u95ee\u9898\u7684\u53e6\u5916\u4e00\u79cd\u9014\u5f84\uff1a\u5141\u8bb8\u6211\u4eec\u5728\u7a0b\u5e8f\u4e2d\u521b\u5efa\u5168\u65b0\u7684\u81ea\u5b9a\u4e49\u7c7b\u578b\uff01\u8fd9\u6837\u7684\u7c7b\u578b\u901a\u5e38\u79f0\u4e3a\u7528\u6237\u5b9a\u4e49\u7c7b\u578b(\u5c3d\u7ba1\u6211\u4eec\u8ba4\u4e3a\u672f\u8bed\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u66f4\u597d\u2014\u2014\u6211\u4eec\u5c06\u5728\u672c\u8bfe\u540e\u9762\u8ba8\u8bba\u5176\u533a\u522b)\u3002C++\u6709\u4e24\u7c7b\u590d\u5408\u7c7b\u578b\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a\u679a\u4e3e\u7c7b\u578b(\u5305\u62ec \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u548c \u6709\u4f5c\u7528\u57df\u679a\u4e3e )\u548c\u7c7b\u7c7b\u578b(\u5305\u62ec\u7ed3\u6784\u4f53\u3001\u7c7b\u548c\u8054\u5408\u4f53)\u3002","title":"\u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u662f\u4ec0\u4e48\uff1f"},{"location":"10-1-Introduction-to-program-defined-user-defined-types/#_2","text":"\u5c31\u50cf \u7c7b\u578b\u522b\u540d \u4e00\u6837\uff0c\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u4e5f\u5fc5\u987b\u5148\u5b9a\u4e49\u540e\u4f7f\u7528\u3002\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7684\u5b9a\u4e49\u79f0\u4e3a\u7c7b\u578b\u5b9a\u4e49\u3002 \u5c3d\u7ba1\u7ed3\u6784\u4f53\u8fd8\u5c1a\u672a\u4ecb\u7ecd\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u5148\u4ece\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u4e86\u89e3\u5230\uff0c\u81ea\u5b9a\u4e49\u7c7b\u578b Fraction \u548c\u8be5\u7c7b\u578b\u5bf9\u8c61\u662f\u5982\u4f55\u521d\u59cb\u5316\u7684\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // Define a program-defined type named Fraction so the compiler understands what a Fraction is // (we'll explain what a struct is and how to use them later in this chapter) // This only defines what a Fraction type looks like, it doesn't create one struct Fraction { int numerator {}; int denominator {}; }; // Now we can make use of our Fraction type int main () { Fraction f { 3 , 4 }; // this actually instantiates a Fraction object named f return 0 ; } \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 struct \u5173\u952e\u5b57\u6765\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a Fraction \u7684\u65b0\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b(\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\uff0c\u56e0\u6b64\u5b83\u53ef\u4ee5\u5728\u6587\u4ef6\u7684\u5176\u4ed6\u5730\u65b9\u4f7f\u7528)\u3002\u8fd9\u4e0d\u4f1a\u5206\u914d\u4efb\u4f55\u5185\u5b58\u2014\u2014\u5b83\u53ea\u662f\u544a\u8bc9\u7f16\u8bd1\u5668 Fraction \u662f\u4ec0\u4e48\u6837\u5b50\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u7a0d\u540e\u518d\u4e3a Fraction \u7c7b\u578b\u7684\u5bf9\u8c61\u5206\u914d\u5185\u5b58\u3002\u7136\u540e\uff0c\u5728 main() \u5185\u90e8\uff0c\u5b9e\u4f8b\u5316(\u5e76\u521d\u59cb\u5316)\u4e00\u4e2a\u540d\u4e3a f \u7684 Fraction \u7c7b\u578b\u53d8\u91cf\u3002 \u7c7b\u578b\u5b9a\u4e49\u603b\u662f\u4ee5\u5206\u53f7\u7ed3\u5c3e\u3002\u6ca1\u6709\u5728\u7c7b\u578b\u5b9a\u4e49\u7684\u672b\u5c3e\u5305\u542b\u5206\u53f7\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u7a0b\u5e8f\u5458\u9519\u8bef\uff0c\u800c\u4e14\u8fd9\u4e2a\u9519\u8bef\u5f88\u96be\u8c03\u8bd5\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u5728\u7c7b\u578b\u5b9a\u4e49\u540e\u9762\u7684\u884c\u62a5\u9519\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u4ece\u4e0a\u9762\u4f8b\u5b50\u7684 Fraction \u5b9a\u4e49(\u7b2c8\u884c)\u672b\u5c3e\u5220\u9664\u5206\u53f7\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u56e0\u4e3a main() \u7684\u5b9a\u4e49(\u7b2c11\u884c)\u62a5\u9519\u3002 \u6ce8\u610f \u4e0d\u8981\u5fd8\u8bb0\u4ee5\u5206\u53f7\u7ed3\u675f\u7c7b\u578b\u5b9a\u4e49\uff0c\u5426\u5219\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u5728\u4e0b\u4e00\u884c\u4ee3\u7801\u4e2d\u51fa\u9519\u3002 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe( 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u4e2d\u5c55\u793a\u66f4\u591a\u5b9a\u4e49\u548c\u4f7f\u7528\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7684\u4f8b\u5b50\uff0c\u5e76\u4e14\u6211\u4eec\u5c06\u4ece 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 \u5f00\u59cb\u4ecb\u7ecd\u7ed3\u6784\u4f53\u3002","title":"\u5b9a\u4e49\u4e00\u4e2a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b"},{"location":"10-1-Introduction-to-program-defined-user-defined-types/#_3","text":"\u6309\u7167\u60ef\u4f8b\uff0c\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u540d\u5b57\u5e94\u8be5\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff0c\u800c\u4e14\u4e0d\u8981\u6dfb\u52a0\u4efb\u4f55\u7684\u540e\u7f00(\u4f8b\u5982\uff1a Fraction \uff0c\u800c\u4e0d\u662f fraction \uff0c fraction_t \u6216\u8005 Fraction_t )\u3002 \u6700\u4f73\u5b9e\u8df5 \u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u59cb\u547d\u540d\u81ea\u5b9a\u4e49\u7c7b\u578b\uff0c\u4e0d\u8981\u4f7f\u7528\u540e\u7f00\u3002 \u7531\u4e8e\u7c7b\u578b\u540d\u548c\u53d8\u91cf\u540d\u4e4b\u95f4\u7684\u76f8\u4f3c\u6027\uff0c\u65b0\u624b\u7a0b\u5e8f\u5458\u65f6\u5e38\u4f1a\u89c9\u5f97\u4e0b\u9762\u8fd9\u6837\u7684\u53d8\u91cf\u5b9a\u4e49\u4ee4\u4eba\u56f0\u60d1: 1 Fraction fraction {}; // \u5b9e\u4f8b\u5316\u4e00\u4e2a\u540d\u4e3a fraction \u7684 Fraction \u7c7b\u578b\u7684\u5bf9\u8c61 \u4f46\u8fd9\u4e0e\u5176\u4ed6\u7c7b\u578b\u7684\u53d8\u91cf\u5b9a\u4e49\u5e76\u6ca1\u6709\u4ec0\u4e48\u533a\u522b\uff1a\u7c7b\u578b( Fraction )\u5728\u524d(\u56e0\u4e3a Fraction \u662f\u5927\u5199\u7684\uff0c\u6211\u4eec\u77e5\u9053\u5b83\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7c7b\u578b)\uff0c\u7136\u540e\u662f\u53d8\u91cf\u540d( fraction )\uff0c\u7136\u540e\u662f\u4e00\u4e2a\u53ef\u9009\u7684\u521d\u59cb\u5316\u503c\u3002\u56e0\u4e3aC++\u662f\u533a\u5206\u5927\u5c0f\u5199\u7684\uff0c\u6240\u4ee5\u8fd9\u91cc\u4e0d\u5b58\u5728\u547d\u540d\u51b2\u7a81!","title":"\u547d\u540d\u4e00\u4e2a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b"},{"location":"10-1-Introduction-to-program-defined-user-defined-types/#_4","text":"\u6bcf\u4e2a\u4f7f\u7528\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7684\u4ee3\u7801\u6587\u4ef6\u5728\u4f7f\u7528\u4e4b\u524d\u90fd\u9700\u8981\u770b\u5230\u5b8c\u6574\u7684\u7c7b\u578b\u5b9a\u4e49\u3002 \u524d\u5411\u58f0\u660e \u662f\u4e0d\u591f\u7684\u3002\u770b\u5230\u5b8c\u6574\u5b9a\u4e49\u662f\u5fc5\u9700\u7684\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u9700\u8981\u77e5\u9053\u8981\u4e3a\u8be5\u7c7b\u578b\u7684\u5bf9\u8c61\u5206\u914d\u591a\u5c11\u5185\u5b58\u3002 \u4e3a\u4e86\u80fd\u5c06\u5b9a\u4e49\u5f15\u5165\u9700\u8981\u4f7f\u7528\u8be5\u7c7b\u578b\u5b9a\u4e49\u7684\u6587\u4ef6\uff0c\u81ea\u5b9a\u4e49\u7c7b\u578b\u901a\u5e38\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u4f7f\u7528 #included \u5bfc\u5165\u4efb\u4f55\u9700\u8981\u8be5\u5b9a\u4e49\u7684\u6587\u4ef6\u3002\u8fd9\u4e9b\u5934\u6587\u4ef6\u901a\u5e38\u548c\u7c7b\u578b\u5177\u6709\u76f8\u540c\u7684\u540d\u5b57\uff08\u4f8b\u5982 Fraction \u5e94\u8be5\u5b9a\u4e49\u5728 Fraction.h \u4e2d\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u53ea\u5728\u4e00\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\u4f7f\u7528\u7684\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u5e94\u8be5\u5728\u8be5\u4ee3\u7801\u6587\u4ef6\u4e2d\u5c3d\u53ef\u80fd\u9760\u8fd1\u7b2c\u4e00\u4e2a\u4f7f\u7528\u70b9\u5b9a\u4e49\u3002 \u5728\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\u4f7f\u7528\u7684\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u5e94\u8be5\u5728\u4e0e\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u540c\u540d\u7684\u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\uff0c\u7136\u540e\u6839\u636e\u9700\u8981\u5728\u4ee3\u7801\u6587\u4ef6\u4e2d\u4f7f\u7528 #include \u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5982\u679c\u6211\u4eec\u628a\u6211\u4eec\u7684 Fraction \u7c7b\u578b\u79fb\u52a8\u5230\u4e00\u4e2a\u5934\u6587\u4ef6(\u540d\u4e3a Fraction.h )\uff0c\u8fd9\u6837\u5b83\u5c31\u53ef\u4ee5\u5305\u542b\u5728\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d: Fraction.h 1 2 3 4 5 6 7 8 9 10 11 12 13 #ifndef FRACTION_H #define FRACTION_H // Define a new type named Fraction // This only defines what a Fraction looks like, it doesn't create one // Note that this is a full definition, not a forward declaration struct Fraction { int numerator {}; int denominator {}; }; #endif Fraction.cpp 1 2 3 4 5 6 7 8 9 #include \"Fraction.h\" // include our Fraction definition in this code file // Now we can make use of our Fraction type int main () { Fraction f { 3 , 4 }; // this actually creates a Fraction object named f return 0 ; }","title":"\u5728\u591a\u6587\u4ef6\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b"},{"location":"10-1-Introduction-to-program-defined-user-defined-types/#_5","text":"\u5728\u8bfe\u7a0b 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 \u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \uff0c\u8be5\u89c4\u5219\u8981\u6c42\u6bcf\u4e2a\u51fd\u6570\u548c\u5168\u5c40\u53d8\u91cf\u5728 \u6bcf\u4e2a\u7a0b\u5e8f \u4e2d\u53ea\u6709\u4e00\u4e2a\u5b9a\u4e49\u3002\u8981\u5728\u4e0d\u5305\u542b\u5b9a\u4e49\u7684\u6587\u4ef6\u4e2d\u4f7f\u7528\u7ed9\u5b9a\u51fd\u6570\u6216\u5168\u5c40\u53d8\u91cf\uff0c\u9700\u8981 \u524d\u5411\u58f0\u660e (\u901a\u5e38\u901a\u8fc7\u5934\u6587\u4ef6\u5b9e\u73b0)\u3002\u8fd9\u662f\u53ef\u884c\u7684\uff0c\u56e0\u4e3a\u5f53\u6d89\u53ca\u5230\u51fd\u6570\u548c\u975econstexpr\u53d8\u91cf\u65f6\uff0c\u7f16\u8bd1\u5668\u53ea\u9700\u8981\u58f0\u660e\u5c31\u53ef\u4ee5 \uff0c\u7136\u540e\u94fe\u63a5\u5668\u4f1a\u5c06\u6240\u9700\u7684\u4e1c\u897f\u90fd\u94fe\u63a5\u8d77\u6765\u3002 \u4f46\u662f\uff0c\u8fd9\u4e2a\u505a\u6cd5\u5e76\u4e0d\u9002\u7528\u4e8e\u7c7b\u578b\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u901a\u5e38\u9700\u8981\u770b\u5230\u5b8c\u6574\u7684\u7c7b\u578b\u5b9a\u4e49\u624d\u80fd\u4f7f\u7528\u7ed9\u5b9a\u7684\u7c7b\u578b\u3002\u6211\u4eec\u5fc5\u987b\u80fd\u591f\u5c06\u5b8c\u6574\u7684\u7c7b\u578b\u5b9a\u4e49\u5bfc\u5165\u5230\u6bcf\u4e2a\u9700\u8981\u5b83\u7684\u4ee3\u7801\u6587\u4ef6\u3002 \u4e3a\u6b64\uff0c \u7c7b\u578b\u5b9a\u4e49\u5728\u67d0\u79cd\u7a0b\u5ea6\u4e0a\u662f\u4e0d\u53d7\u9650\u4e8e\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u7684\uff1a\u7c7b\u578b\u7684\u5b9a\u4e49\u53ef\u4ee5\u51fa\u73b0\u5728\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\u3002 \u5176\u5b9e\u4f60\u5df2\u7ecf\u5728\u4e0d\u7ecf\u610f\u4e4b\u95f4\u8fd9\u4e48\u505a\u4e86\uff1a\u5982\u679c\u7a0b\u5e8f\u6709\u4e24\u4e2a\u4ee3\u7801\u6587\u4ef6\uff0c\u4e14\u90fd #include \uff0c\u90a3\u4e48\u6211\u4eec\u5b9e\u9645\u4e0a\u5c06\u6240\u6709\u8f93\u5165/\u8f93\u51fa\u7c7b\u578b\u5b9a\u4e49\u90fd\u5bfc\u5165\u5230\u4e86\u8fd9\u4e24\u4e2a\u6587\u4ef6\u4e2d\u3002 \u6709\u4e24\u70b9\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0c\u7c7b\u578b\u5b9a\u4e49\u5728\u540c\u4e00\u4e2a\u6587\u4ef6\u4e2d\u53ea\u80fd\u51fa\u73b0\u4e00\u6b21(\u8fd9\u901a\u5e38\u4e0d\u662f\u95ee\u9898\uff0c\u56e0\u4e3a \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e \u53ef\u4ee5\u9632\u6b62\u8be5\u95ee\u9898\u51fa\u73b0)\u3002\u5176\u6b21\uff0c\u7ed9\u5b9a\u7c7b\u578b\u7684\u6240\u6709\u7c7b\u578b\u5b9a\u4e49\u5fc5\u987b\u76f8\u540c\uff0c\u5426\u5219\u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002","title":"\u7c7b\u578b\u5b9a\u4e49\u90e8\u5206\u4e0a\u8c41\u514d\u4e8e\u5355\u4e00\u5b9a\u4e49\u539f\u5219"},{"location":"10-1-Introduction-to-program-defined-user-defined-types/#vs","text":"\u672f\u8bed\u201c\u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u201d\uff08\u81ea\u5b9a\u4e49\u7c7b\u578b\uff09\u6709\u65f6\u4f1a\u51fa\u73b0\u5728\u65e5\u5e38\u5bf9\u8bdd\u4e2d\uff0c\u4e5f\u4f1a\u5728C++\u8bed\u8a00\u6807\u51c6\u4e2d\u63d0\u5230(\u4f46\u6ca1\u6709\u5b9a\u4e49)\u3002\u5728\u975e\u6b63\u5f0f\u7684\u5bf9\u8bdd\u4e2d\uff0c\u8fd9\u4e2a\u672f\u8bed\u5f80\u5f80\u610f\u5473\u7740\u201c\u4f60\u81ea\u5df1\u5b9a\u4e49\u7684\u7c7b\u578b\u201d(\u5982\u4e0a\u9762\u7684\u5206\u6570\u7c7b\u578b\u4f8b\u5b50)\u3002 \u6709\u65f6\u8fd9\u4e5f\u5305\u62ec\u7c7b\u578b\u522b\u540d \u3002 \u7136\u800c\uff0c\u5728C++\u8bed\u8a00\u6807\u51c6\u7684\u884c\u6587\u8bed\u5883\u4e2d\uff0c\u4efb\u4f55\u6ca1\u6709\u5b9a\u4e49\u4e3a\u6838\u5fc3C++\u8bed\u8a00\u4e00\u90e8\u5206\u7684\u7c7b\u578b(\u6362\u53e5\u8bdd\u8bf4\uff0c\u975e\u57fa\u672c\u7c7b\u578b)\u90fd\u662f\u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u3002\u56e0\u6b64\uff0c\u5728C++\u6807\u51c6\u5e93\u4e2d\u5b9a\u4e49\u7684\u7c7b\u578b(\u4f8b\u5982 std::string )\u7406\u8bba\u4e0a\u4e5f\u7b97\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u5c31\u50cf\u4f60\u81ea\u5df1\u5b9a\u4e49\u7684\u4efb\u4f55\u7c7b\u578b\u4e00\u6837\u3002 \u4e3a\u4e86\u533a\u522b\uff0cC++ 20\u8bed\u8a00\u6807\u51c6\u5c06\u672f\u8bed\u201c\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u201d\u5b9a\u4e49\u4e3a\u4ec5\u6307\u4ee3\u4f60\u81ea\u5df1\u5b9a\u4e49\u7684\u7c7b\u578b\u3002\u5f53\u6211\u4eec\u8ba8\u8bba\u8fd9\u4e9b\u7c7b\u578b\u65f6\uff0c\u66f4\u559c\u6b22\u4f7f\u7528\u8fd9\u4e2a\u672f\u8bed\uff0c\u56e0\u4e3a\u5b83\u66f4\u52a0\u7cbe\u786e\u3002 \u7c7b\u578b \u542b\u4e49 \u4f8b\u5b50 \u57fa\u672c\u7c7b\u578b \u5185\u5efa\u4e8e C++ \u8bed\u8a00\u7684\u6838\u5fc3\u90e8\u5206 int , std::nullptr_t \u7528\u6237\u5b9a\u4e49\u7c7b\u578b \u975e\u57fa\u7840\u6570\u636e\u7c7b\u578b(\u975e\u6b63\u5f0f\u573a\u5408\u6307\u4ee3\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b) std::string , Fraction \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b \u7528\u6237\u81ea\u5b9a\u4e49\u7684\u7c7b\u6216\u679a\u4e3e\u7c7b\u578b Fraction","title":"\u547d\u540d\u6cd5\uff1a\u7528\u6237\u5b9a\u4e49\u7c7b\u578b vs \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b"},{"location":"10-10-class-templates/","tags":["class template"],"text":"Key Takeaway \u5728 [[8-13-Function-templates|8.13 - \u51fd\u6570\u6a21\u677f] \u4e2d\u6211\u4eec\u63d0\u5230\uff0c\u4e3a\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u5206\u522b\u521b\u5efa\uff08\u91cd\u8f7d\uff09\u4e0d\u540c\u7684\u51fd\u6570\u662f\u4e00\u9879\u56f0\u96be\u4e14\u6ca1\u5fc5\u8981\u7684\u5de5\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include // function to calculate the greater of two int values int max ( int x , int y ) { return ( x > y ) ? x : y ; } // almost identical function to calculate the greater of two double values // the only difference is the type information double max ( double x , double y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max ( 5 , 6 ); // calls max(int, int) std :: cout << '\\n' ; std :: cout << max ( 1.2 , 3.4 ); // calls max(double, double) return 0 ; } \u8be5\u95ee\u9898\u7684\u6700\u7ec8\u89e3\u51b3\u65b9\u6848\u662f\u521b\u5efa\u4e00\u4e2a \u51fd\u6570\u6a21\u677f \u5e76\u8ba9\u7f16\u8bd1\u5668\u6839\u636e\u6240\u9700\u7684\u7c7b\u578b\u4e3a\u4f60\u5b9e\u4f8b\u5316\u5bf9\u5e94\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // a single function template for max template < typename T > T max ( T x , T y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max ( 5 , 6 ); // instantiates and calls max(int, int) std :: cout << '\\n' ; std :: cout << max ( 1.2 , 3.4 ); // instantiates and calls max(double, double) return 0 ; } \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u5728 8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316 \u4e2d\u4ecb\u7ecd\u4e86\u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316\u3002 \u805a\u5408\u7c7b\u578b\u4e5f\u9762\u4e34\u7c7b\u4f3c\u7684\u95ee\u9898 \u5bf9\u4e8e\u805a\u5408\u7c7b\u578b(\u5305\u62ec\u7ed3\u6784/\u7c7b/\u8054\u5408\u548c\u6570\u7ec4)\uff0c\u6211\u4eec\u4e5f\u9047\u5230\u4e86\u7c7b\u4f3c\u7684\u95ee\u9898\u3002 \u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5176\u4e2d\u9700\u8981\u5904\u7406\u4e00\u5bf9 int \u503c\uff0c\u5e76\u9700\u8981\u786e\u5b9a\u8fd9\u4e24\u4e2a\u6570\u5b57\u4e2d\u54ea\u4e00\u4e2a\u66f4\u5927\u3002\u6211\u4eec\u53ef\u4ee5\u5199\u8fd9\u6837\u4e00\u4e2a\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include struct Pair { int first {}; int second {}; }; constexpr int max ( Pair p ) // pass by value because Pair is small { return ( p . first > p . second ? p . first : p . second ); } int main () { Pair p1 { 5 , 6 }; std :: cout << max ( p1 ) << \" is larger \\n \" ; return 0 ; } \u4e00\u6bb5\u65f6\u95f4\u540e\uff0c\u6211\u4eec\u53d1\u73b0\u6211\u4eec\u9700\u8981\u5904\u7406\u4e00\u5bf9 double \u7c7b\u578b\u7684\u503c\uff0c\u90a3\u4e48\u6211\u4eec\u5fc5\u987b\u66f4\u65b0\u7a0b\u5e8f\uff0c\u6dfb\u52a0\u53e6\u5916\u4e00\u4e2a Pair \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include struct Pair { int first {}; int second {}; }; struct Pair // compile error: erroneous redefinition of Pair { double first {}; double second {}; }; constexpr int max ( Pair p ) { return ( p . first > p . second ? p . first : p . second ); } constexpr double max ( Pair p ) // compile error: overloaded function differs only by return type { return ( p . first > p . second ? p . first : p . second ); } int main () { Pair p1 { 5 , 6 }; std :: cout << max ( p1 ) << \" is larger \\n \" ; Pair p2 { 1.2 , 3.4 }; std :: cout << max ( p2 ) << \" is larger \\n \" ; return 0 ; } \u5f88\u9057\u61be\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u662f\u4e0d\u80fd\u7f16\u8bd1\u7684\uff0c\u800c\u4e14\u8fd9\u91cc\u6709\u5f88\u591a\u95ee\u9898\uff1a \u9996\u5148\uff0c\u548c\u51fd\u6570\u4e0d\u540c\uff0c\u7c7b\u578b\u662f\u4e0d\u80fd\u88ab \u91cd\u8f7d \u7684\u3002\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u5c06\u7b2c\u4e8c\u4e2a double \u6210\u5458\u7248\u672c\u7684 Pair \u770b\u505a\u662f\u7b2c\u4e00\u4e2a Pair \u7684\u91cd\u590d\u5b9a\u4e49\u3002\u7b2c\u4e8c\uff0c\u5c3d\u7ba1\u51fd\u6570\u53ef\u4ee5\u88ab\u91cd\u8f7d\uff0c\u4f46\u4e24\u4e2a max(Pair) \u51fd\u6570\u53ea\u6709\u8fd4\u56de\u503c\u4e0d\u540c\uff0c\u800c\u51fd\u6570\u91cd\u8f7d\u4e0d\u80fd\u4ec5\u4ec5\u901a\u8fc7\u4e0d\u540c\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u533a\u5206\u3002\u7b2c\u4e09\uff0c\u4ee3\u7801\u6709\u5f88\u591a\u5197\u4f59\u3002\u6bcf\u4e2a Pair \u7ed3\u6784\u4f53\u90fd\u662f\u76f8\u540c\u7684\uff08\u53ea\u662f\u6570\u636e\u7c7b\u578b\u4e0d\u540c\uff09\uff0c max(Pair) \u51fd\u6570\u4e5f\u662f (\u53ea\u6709\u8fd4\u56de\u503c\u4e0d\u540c)\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ed9 Pair \u8d77\u4e0d\u540c\u7684\u540d\u5b57\u6765\u89e3\u51b3\u524d\u4e24\u4e2a\u95ee\u9898\uff08\u4f8b\u5982 Pairint \u548c Pairdouble )\u3002\u4f46\u662f\u6211\u4eec\u5fc5\u987b\u8bb0\u4f4f\u5168\u90e8\u7684\u547d\u540d\u65b9\u6848\uff0c\u5e76\u4e3a\u6211\u4eec\u60f3\u8981\u7684\u6bcf\u4e00\u4e2a\u989d\u5916\u7684\u7c7b\u578b\u514b\u9686\u4e00\u5806\u4ee3\u7801\uff0c\u8fd9\u5e76\u4e0d\u80fd\u89e3\u51b3\u5197\u4f59\u95ee\u9898\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u6709\u66f4\u597d\u7684\u529e\u6cd5\uff01 \u4f5c\u8005\u6ce8 \u5728\u7ee7\u7eed\u9605\u8bfb\u4e4b\u524d\uff0c\u5982\u679c\u4f60\u8fd8\u4e0d\u719f\u6089\u51fd\u6570\u6a21\u677f\u3001\u6a21\u677f\u7c7b\u578b\u6216\u8005\u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u63a8\u8350\u590d\u4e60\u4e00\u4e0b 8.13 - \u51fd\u6570\u6a21\u677f \u548c 8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316 \u3002 \u7c7b\u6a21\u677f \u5c31\u50cf\u51fd\u6570\u6a21\u677f\u662f\u5b9e\u4f8b\u5316\u51fd\u6570\u7684\u6a21\u677f\u5b9a\u4e49\u4e00\u6837\uff0c\u7c7b\u6a21\u677f\u662f\u5b9e\u4f8b\u5316\u7c7b\u7c7b\u578b\u7684\u6a21\u677f\u5b9a\u4e49\u3002 \u63d0\u9192 \u6240\u8c13 \u201c \u7c7b\u7c7b\u578b \u201d \u6307\u7684\u662f\u7ed3\u6784\u4f53\u3001\u7c7b\u6216\u8054\u5408\u4f53\u3002\u867d\u7136\u6211\u4eec\u4ee5\u7ed3\u6784\u4f53\u7684\u7c7b\u6a21\u677f\u8fdb\u884c\u6f14\u793a\uff0c\u4f46\u662f\u5b83\u540c\u6837\u9002\u7528\u4e8e\u7c7b\u3002 \u56de\u5fc6\u4e00\u4e0b\uff0c\u8fd9\u91cc\u6709\u4e00\u4e2a\u6574\u578b\u6210\u5458\u7c7b\u578b\u7684 Pair \u7ed3\u6784\u4f53\u5b9a\u4e49\uff1a 1 2 3 4 5 struct Pair { int first {}; int second {}; }; \u8ba9\u6211\u4eec\u4f7f\u7528\u7c7b\u6a21\u677f\u91cd\u5199\u4e00\u4e0b\u8be5\u7ed3\u6784\u4f53\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include template < typename T > struct Pair { T first {}; T second {}; }; int main () { Pair < int > p1 { 5 , 6 }; // instantiates Pair and creates object p1 std :: cout << p1 . first << ' ' << p1 . second << '\\n' ; Pair < double > p2 { 1.2 , 3.4 }; // instantiates Pair and creates object p2 std :: cout << p2 . first << ' ' << p2 . second << '\\n' ; Pair < double > p3 { 7.8 , 9.0 }; // creates object p3 using prior definition for Pair std :: cout << p3 . first << ' ' << p3 . second << '\\n' ; return 0 ; } \u5c31\u50cf\u51fd\u6570\u6a21\u677f\u4e00\u6837\uff0c\u6211\u4eec\u4ece\u6a21\u677f\u5f62\u53c2\u58f0\u660e\u5f00\u59cb\u5b9a\u4e49\u7c7b\u6a21\u677f\uff0c\u5373\u5148\u662f template \u5173\u952e\u5b57\u3002\u63a5\u4e0b\u6765\uff0c\u5728\u5c16\u62ec\u53f7( <> )\u4e2d\u6307\u5b9a\u7c7b\u6a21\u677f\u5c06\u4f7f\u7528\u7684\u6240\u6709\u6a21\u677f\u7c7b\u578b\u3002\u5bf9\u4e8e\u6211\u4eec\u9700\u8981\u7684\u6bcf\u4e2a\u6a21\u677f\u7c7b\u578b\uff0c\u9700\u8981\u4f7f\u7528\u5173\u952e\u5b57 typename (\u9996\u9009)\u6216 class (\u975e\u9996\u9009)\uff0c\u540e\u9762\u8ddf\u7740\u6a21\u677f\u7c7b\u578b\u7684\u540d\u79f0(\u4f8b\u5982\u3002\u201cT\u201d)\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u7684\u4e24\u4e2a\u6210\u5458\u90fd\u662f\u76f8\u540c\u7684\u7c7b\u578b\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u9700\u8981\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\u3002 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u50cf\u5f80\u5e38\u4e00\u6837\u5b9a\u4e49\u7ed3\u6784\uff0c\u53ea\u4e0d\u8fc7\u6211\u4eec\u53ef\u4ee5\u5728\u9700\u8981\u6a21\u677f\u7c7b\u578b\u7684\u5730\u65b9\u4f7f\u7528\u6a21\u677f\u7c7b\u578b( T )\uff0c\u4ee5\u4fbf\u7a0d\u540e\u7528\u5b9e\u9645\u7c7b\u578b\u66ff\u6362\u6a21\u677f\u7c7b\u578b\u3002\u5c31\u662f\u8fd9\u6837\uff01\u7c7b\u6a21\u677f\u7684\u5b9a\u4e49\u5c31\u5b8c\u6210\u4e86\u3002 \u5728 main \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9700\u8981\u8981\u7684\u4efb\u4f55\u7c7b\u578b\u6765\u5b9e\u4f8b\u5316 Pair \u5bf9\u8c61\u3002\u9996\u5148\uff0c\u6211\u4eec\u5b9e\u4f8b\u5316\u4e00\u4e2a Pair \u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u56e0\u4e3a Pair \u7684\u7c7b\u578b\u5b9a\u4e49\u8fd8\u4e0d\u5b58\u5728\uff0c\u7f16\u8bd1\u5668\u4f7f\u7528\u7c7b\u6a21\u677f\u5b9e\u4f8b\u5316\u4e00\u4e2a\u540d\u4e3a Pair \u7684\u7ed3\u6784\u4f53\u7c7b\u578b\u5b9a\u4e49\uff0c\u5176\u4e2d\u6240\u6709\u51fa\u73b0\u7684\u6a21\u677f\u7c7b\u578b T \u90fd\u88ab\u7c7b\u578b int \u66ff\u6362\u3002 \u63a5\u4e0b\u6765\uff0c\u518d\u5b9e\u4f8b\u5316\u4e00\u4e2a Pair \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u6b64\u65f6\u4f1a\u5b9e\u4f8b\u5316\u4e00\u4e2a Pair \u7c7b\u578b\u7684\u7ed3\u6784\u4f53\u5b9a\u4e49\uff0c\u5176\u4e2d T \u4f1a\u88ab\u66ff\u6362\u4e3a double \u3002\u5bf9\u4e8e p3 \u6765\u8bf4\uff0c Pair \u5df2\u7ecf\u88ab\u5b9e\u4f8b\u5316\u4e86\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u4f7f\u7528\u4e4b\u524d\u7684\u5b9a\u4e49\u3002 \u63a5\u4e0b\u6765\u662f\u4e0e\u4e0a\u9762\u76f8\u540c\u7684\u4f8b\u5b50\uff0c\u663e\u793a\u4e86\u7f16\u8bd1\u5668\u5728\u6240\u6709\u6a21\u677f\u5b9e\u4f8b\u5316\u5b8c\u6210\u540e\u5b9e\u9645\u7f16\u8bd1\u7684\u5185\u5bb9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include // A declaration for our Pair class template // (we don't need the definition any more since it's not used) template < typename T > struct Pair ; // Explicitly define what Pair looks like template <> // tells the compiler this is a template type with no template parameters struct Pair < int > { int first {}; int second {}; }; // Explicitly define what Pair looks like template <> // tells the compiler this is a template type with no template parameters struct Pair < double > { double first {}; double second {}; }; int main () { Pair < int > p1 { 5 , 6 }; // instantiates Pair and creates object p1 std :: cout << p1 . first << ' ' << p1 . second << '\\n' ; Pair < double > p2 { 1.2 , 3.4 }; // instantiates Pair and creates object p2 std :: cout << p2 . first << ' ' << p2 . second << '\\n' ; Pair < double > p3 { 7.8 , 9.0 }; // creates object p3 using prior definition for Pair std :: cout << p3 . first << ' ' << p3 . second << '\\n' ; return 0 ; } \u4f60\u53ef\u4ee5\u76f4\u63a5\u7f16\u8bd1\u6b64\u793a\u4f8b\uff0c\u5b83\u80fd\u591f\u6309\u9884\u671f\u5de5\u4f5c! \u6269\u5c55\u9605\u8bfb \u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86\u4e00\u4e2a\u79f0\u4e3a \u7c7b\u6a21\u677f\u7279\u5316 \u7684\u7279\u6027(\u4f1a\u5728 19.4 -- Class template specialization \u4e2d\u4ecb\u7ecd)\u3002\u76ee\u524d\u6211\u4eec\u8fd8\u4e0d\u9700\u8981\u638c\u63e1\u8be5\u7279\u6027\u7684\u77e5\u8bc6\u3002 \u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u7c7b\u6a21\u677f \u73b0\u5728\u8ba9\u6211\u4eec\u56de\u5230\u8ba9 max() \u51fd\u6570\u914d\u5408\u4e0d\u540c\u7c7b\u578b\u5de5\u4f5c\u7684\u6311\u6218\u3002\u56e0\u4e3a\u7f16\u8bd1\u5668\u5c06 Pair \u548c Pair \u770b\u505a\u5355\u72ec\u7684\u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6309\u5f62\u53c2\u7c7b\u578b\u533a\u5206\u7684\u91cd\u8f7d\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 constexpr int max ( Pair < int > p ) { return ( p . first > p . second ? p . first : p . second ); } constexpr double max ( Pair < double > p ) // okay: overloaded function differentiated by parameter type { return ( p . first > p . second ? p . first : p . second ); } \u867d\u7136\u4e0a\u8ff0\u4ee3\u7801\u53ef\u4ee5\u7f16\u8bd1\u4e86\uff0c\u4f46\u662f\u5b83\u6ca1\u6709\u89e3\u51b3\u4ee3\u7801\u5197\u4f59\u7684\u95ee\u9898\u3002\u6211\u4eec\u5b9e\u9645\u4e0a\u5e0c\u671b\u4e00\u4e2a\u51fd\u6570\u80fd\u591f\u63a5\u53d7\u4efb\u4f55\u7c7b\u578b\u7684\u53c2\u6570\u3002\u6362\u8a00\u4e4b\u6211\u4eec\u5e0c\u671b\u51fd\u6570\u53ef\u4ee5\u4f7f\u7528 Pair \u7c7b\u578b\u7684\u5f62\u53c2\uff0c\u5176\u4e2d T \u662f\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\u53c2\u6570\u3002\u56e0\u6b64\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u51fd\u6570\u6a21\u677f\u6765\u5b8c\u6210\u76f8\u5e94\u7684\u5de5\u4f5c\uff01 \u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u4f8b\u5b50\uff0c max() \u88ab\u5b9e\u73b0\u4e3a\u4e00\u4e2a\u51fd\u6570\u6a21\u677f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include template < typename T > struct Pair { T first {}; T second {}; }; template < typename T > constexpr T max ( Pair < T > p ) { return ( p . first > p . second ? p . first : p . second ); } int main () { Pair < int > p1 { 5 , 6 }; std :: cout << max < int > ( p1 ) << \" is larger \\n \" ; // explicit call to max Pair < double > p2 { 1.2 , 3.4 }; std :: cout << max ( p2 ) << \" is larger \\n \" ; // call to max using template argument deduction (prefer this) return 0 ; } max() \u51fd\u6570\u6a21\u677f\u975e\u5e38\u76f4\u767d\u3002\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u5411\u5176\u4f20\u9012 Pair \uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u5fc5\u987b\u4e86\u89e3 T \u662f\u4ec0\u4e48\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u5728\u51fd\u6570\u5f00\u5934\u51fa\u6dfb\u52a0\u6a21\u677f\u53c2\u6570\u58f0\u660e\uff0c\u5b9a\u4e49\u6a21\u677f\u7c7b\u578b T \u3002\u968f\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528 T \u4f5c\u4e3a\u8fd4\u56de\u7c7b\u578b\uff0c\u4ee5\u53ca\u6a21\u677f\u53c2\u6570 Pair \u3002 \u56e0\u4e3a max() \u51fd\u6570\u7684\u8c03\u7528\u57fa\u4e8e Pair \u7c7b\u578b \u5b9e\u53c2 \uff0c\u7f16\u8bd1\u5668\u4f1a\u57fa\u4e8e\u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316\u51fd\u6570 int max(Pair) \uff0c\u6a21\u677f\u4e2d\u7684 T \u88ab\u66ff\u6362\u4e3a\u4e86 int \u3002\u7f16\u8bd1\u5668\u5b9e\u9645\u4e0a\u5b9e\u4f8b\u5316\u4e86\u8fd9\u6837\u4e00\u4e2a\u51fd\u6570\uff1a 1 2 3 4 5 template <> constexpr int max ( Pair < int > p ) { return ( p . first > p . second ? p . first : p . second ); } \u4e0e\u6240\u6709\u5bf9\u51fd\u6570\u6a21\u677f\u7684\u8c03\u7528\u4e00\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u663e\u5f0f\u5730\u6307\u5b9a\u6a21\u677f\u7c7b\u578b\u5b9e\u53c2(\u4f8b\u5982 max(p1) )\uff0c\u4e5f\u53ef\u4ee5\u9690\u5f0f\u5730\u4f7f\u7528(\u4f8b\u5982 max(p2) )\uff0c\u5e76\u8ba9\u7f16\u8bd1\u5668\u4f7f\u7528 \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad \u6765\u786e\u5b9a\u6a21\u677f\u7c7b\u578b\u5b9e\u53c2\u5e94\u8be5\u662f\u4ec0\u4e48\u7c7b\u578b\u7684\u3002 \u540c\u65f6\u4f7f\u7528\u6a21\u677f\u7c7b\u578b\u548c\u975e\u6a21\u677f\u7c7b\u578b\u7684\u7c7b\u6a21\u677f \u7c7b\u6a21\u677f\u7684\u6210\u5458\u53ef\u4ee5\u4f7f\u7528\u6a21\u677f\u7c7b\u578b\uff0c\u4e5f\u53ef\u4ee5\u540c\u65f6\u6709\u53e6\u5916\u4e00\u90e8\u5206\u6210\u5458\u4f7f\u7528\u666e\u901a\u7c7b\u578b\uff08\u975e\u6a21\u677f\uff09\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 template < typename T > struct Foo { T first {}; // first will have whatever type T is replaced with int second {}; // second will always have type int, regardless of what type T is }; \u4e0a\u8ff0\u4ee3\u7801\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\uff1a first \u7684\u7c7b\u578b\u7531 T \u6307\u5b9a\uff0c\u800c second \u7c7b\u578b\u5219\u603b\u662f int \u3002 \u5177\u6709\u591a\u4e2a\u6a21\u677f\u7c7b\u578b\u7684\u7c7b\u6a21\u677f \u7c7b\u6a21\u677f\u4e5f\u53ef\u4ee5\u6709\u591a\u4e2a\u6a21\u677f\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b Pair \u7c7b\u7684\u4e24\u4e2a\u6210\u5458\u80fd\u591f\u5177\u6709\u4e0d\u540c\u7684\u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4e24\u79cd\u6a21\u677f\u7c7b\u578b\u5b9a\u4e49 Pair \u7c7b\u6a21\u677f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include template < typename T , typename U > struct Pair { T first {}; U second {}; }; template < typename T , typename U > void print ( Pair < T , U > p ) { std :: cout << '[' << p . first << \", \" << p . second << ']' ; } int main () { Pair < int , double > p1 { 1 , 2.3 }; // a pair holding an int and a double Pair < double , int > p2 { 4.5 , 6 }; // a pair holding a double and an int Pair < int , int > p3 { 7 , 8 }; // a pair holding two ints print ( p2 ); return 0 ; } \u8981\u5b9a\u4e49\u591a\u4e2a\u6a21\u677f\u7c7b\u578b\uff0c\u5728\u6a21\u677f\u53c2\u6570\u58f0\u660e\u4e2d\uff0c\u7528\u9017\u53f7\u5206\u9694\u6bcf\u4e2a\u6240\u9700\u7684\u6a21\u677f\u7c7b\u578b\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u79cd\u4e0d\u540c\u7684\u6a21\u677f\u7c7b\u578b\uff0c\u4e00\u79cd\u540d\u4e3a T \uff0c\u53e6\u4e00\u79cd\u540d\u4e3a U \u3002 T \u548c U \u7684\u5b9e\u9645\u6a21\u677f\u7c7b\u578b\u5b9e\u53c2\u53ef\u4ee5\u4e0d\u540c(\u5c31\u50cf\u4e0a\u9762\u7684 p1 \u548c p2 \u4e00\u6837)\uff0c\u4e5f\u53ef\u4ee5\u76f8\u540c(\u5c31\u50cf p3 \u4e00\u6837)\u3002 std::pair \u56e0\u4e3a\u5728C++\u4e2d\u4f7f\u7528\u4e00\u5bf9\u6570\u636e\u662f\u5f88\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u56e0\u6b64C++\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a std::pair \u7684\u7c7b\u6a21\u677f(\u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d) \uff0c\u5b83\u7684\u53e6\u4e00\u548c\u4f7f\u7528\u4e86\u591a\u4e2a\u6a21\u677f\u7c7b\u578b\u7684 Pair \u7c7b\u4e00\u6837\u3002\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 std::pair \u6765\u66ff\u6362\u6211\u4eec\u5b9a\u4e49\u7684 pair \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include template < typename T , typename U > void print ( std :: pair < T , U > p ) { std :: cout << '[' << p . first << \", \" << p . second << ']' ; } int main () { std :: pair < int , double > p1 { 1 , 2.3 }; // a pair holding an int and a double std :: pair < double , int > p2 { 4.5 , 6 }; // a pair holding a double and an int std :: pair < int , int > p3 { 7 , 8 }; // a pair holding two ints print ( p2 ); return 0 ; } \u672c\u8282\u8bfe\u6211\u4eec\u5b9a\u4e49\u4e86\u81ea\u5df1\u7684 Pair \u7c7b\u4ee5\u89e3\u91ca\u5176\u5de5\u4f5c\u539f\u7406\uff0c\u4f46\u662f\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\uff0c\u6211\u4eec\u63a8\u8350\u4f7f\u7528 std::pair \u3002 \u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528\u7c7b\u6a21\u677f \u5c31\u50cf\u51fd\u6570\u6a21\u677f\u4e00\u6837\uff0c\u7c7b\u6a21\u677f\u901a\u5e38\u662f\u5728\u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\uff0c\u56e0\u6b64\u5b83\u4eec\u53ef\u4ee5\u88ab\u5305\u542b\u5230\u4efb\u4f55\u9700\u8981\u5b83\u4eec\u7684\u4ee3\u7801\u6587\u4ef6\u4e2d\u3002\u6a21\u677f\u5b9a\u4e49\u548c\u7c7b\u578b\u5b9a\u4e49\u90fd\u4e0d\u53d7 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u7684\u7ea6\u675f\uff0c\u6240\u4ee5\u8fd9\u4e0d\u4f1a\u5bfc\u81f4\u95ee\u9898\uff1a pair.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #ifndef PAIR_H #define PAIR_H template < typename T > struct Pair { T first {}; T second {}; }; template < typename T > constexpr T max ( Pair < T > p ) { return ( p . first > p . second ? p . first : p . second ); } #endif foo.cpp 1 2 3 4 5 6 7 8 #include \"pair.h\" #include void foo () { Pair < int > p1 { 1 , 2 }; std :: cout << max ( p1 ) << \" is larger \\n \" ; } main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include \"pair.h\" #include void foo (); // forward declaration for function foo() int main () { Pair < double > p2 { 3.4 , 5.6 }; std :: cout << max ( p2 ) << \" is larger \\n \" ; foo (); return 0 ; }","title":"10.10 - \u7c7b\u6a21\u677f"},{"location":"10-10-class-templates/#_1","text":"\u5bf9\u4e8e\u805a\u5408\u7c7b\u578b(\u5305\u62ec\u7ed3\u6784/\u7c7b/\u8054\u5408\u548c\u6570\u7ec4)\uff0c\u6211\u4eec\u4e5f\u9047\u5230\u4e86\u7c7b\u4f3c\u7684\u95ee\u9898\u3002 \u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5176\u4e2d\u9700\u8981\u5904\u7406\u4e00\u5bf9 int \u503c\uff0c\u5e76\u9700\u8981\u786e\u5b9a\u8fd9\u4e24\u4e2a\u6570\u5b57\u4e2d\u54ea\u4e00\u4e2a\u66f4\u5927\u3002\u6211\u4eec\u53ef\u4ee5\u5199\u8fd9\u6837\u4e00\u4e2a\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include struct Pair { int first {}; int second {}; }; constexpr int max ( Pair p ) // pass by value because Pair is small { return ( p . first > p . second ? p . first : p . second ); } int main () { Pair p1 { 5 , 6 }; std :: cout << max ( p1 ) << \" is larger \\n \" ; return 0 ; } \u4e00\u6bb5\u65f6\u95f4\u540e\uff0c\u6211\u4eec\u53d1\u73b0\u6211\u4eec\u9700\u8981\u5904\u7406\u4e00\u5bf9 double \u7c7b\u578b\u7684\u503c\uff0c\u90a3\u4e48\u6211\u4eec\u5fc5\u987b\u66f4\u65b0\u7a0b\u5e8f\uff0c\u6dfb\u52a0\u53e6\u5916\u4e00\u4e2a Pair \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include struct Pair { int first {}; int second {}; }; struct Pair // compile error: erroneous redefinition of Pair { double first {}; double second {}; }; constexpr int max ( Pair p ) { return ( p . first > p . second ? p . first : p . second ); } constexpr double max ( Pair p ) // compile error: overloaded function differs only by return type { return ( p . first > p . second ? p . first : p . second ); } int main () { Pair p1 { 5 , 6 }; std :: cout << max ( p1 ) << \" is larger \\n \" ; Pair p2 { 1.2 , 3.4 }; std :: cout << max ( p2 ) << \" is larger \\n \" ; return 0 ; } \u5f88\u9057\u61be\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u662f\u4e0d\u80fd\u7f16\u8bd1\u7684\uff0c\u800c\u4e14\u8fd9\u91cc\u6709\u5f88\u591a\u95ee\u9898\uff1a \u9996\u5148\uff0c\u548c\u51fd\u6570\u4e0d\u540c\uff0c\u7c7b\u578b\u662f\u4e0d\u80fd\u88ab \u91cd\u8f7d \u7684\u3002\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u5c06\u7b2c\u4e8c\u4e2a double \u6210\u5458\u7248\u672c\u7684 Pair \u770b\u505a\u662f\u7b2c\u4e00\u4e2a Pair \u7684\u91cd\u590d\u5b9a\u4e49\u3002\u7b2c\u4e8c\uff0c\u5c3d\u7ba1\u51fd\u6570\u53ef\u4ee5\u88ab\u91cd\u8f7d\uff0c\u4f46\u4e24\u4e2a max(Pair) \u51fd\u6570\u53ea\u6709\u8fd4\u56de\u503c\u4e0d\u540c\uff0c\u800c\u51fd\u6570\u91cd\u8f7d\u4e0d\u80fd\u4ec5\u4ec5\u901a\u8fc7\u4e0d\u540c\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u533a\u5206\u3002\u7b2c\u4e09\uff0c\u4ee3\u7801\u6709\u5f88\u591a\u5197\u4f59\u3002\u6bcf\u4e2a Pair \u7ed3\u6784\u4f53\u90fd\u662f\u76f8\u540c\u7684\uff08\u53ea\u662f\u6570\u636e\u7c7b\u578b\u4e0d\u540c\uff09\uff0c max(Pair) \u51fd\u6570\u4e5f\u662f (\u53ea\u6709\u8fd4\u56de\u503c\u4e0d\u540c)\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ed9 Pair \u8d77\u4e0d\u540c\u7684\u540d\u5b57\u6765\u89e3\u51b3\u524d\u4e24\u4e2a\u95ee\u9898\uff08\u4f8b\u5982 Pairint \u548c Pairdouble )\u3002\u4f46\u662f\u6211\u4eec\u5fc5\u987b\u8bb0\u4f4f\u5168\u90e8\u7684\u547d\u540d\u65b9\u6848\uff0c\u5e76\u4e3a\u6211\u4eec\u60f3\u8981\u7684\u6bcf\u4e00\u4e2a\u989d\u5916\u7684\u7c7b\u578b\u514b\u9686\u4e00\u5806\u4ee3\u7801\uff0c\u8fd9\u5e76\u4e0d\u80fd\u89e3\u51b3\u5197\u4f59\u95ee\u9898\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u6709\u66f4\u597d\u7684\u529e\u6cd5\uff01 \u4f5c\u8005\u6ce8 \u5728\u7ee7\u7eed\u9605\u8bfb\u4e4b\u524d\uff0c\u5982\u679c\u4f60\u8fd8\u4e0d\u719f\u6089\u51fd\u6570\u6a21\u677f\u3001\u6a21\u677f\u7c7b\u578b\u6216\u8005\u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u63a8\u8350\u590d\u4e60\u4e00\u4e0b 8.13 - \u51fd\u6570\u6a21\u677f \u548c 8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316 \u3002","title":"\u805a\u5408\u7c7b\u578b\u4e5f\u9762\u4e34\u7c7b\u4f3c\u7684\u95ee\u9898"},{"location":"10-10-class-templates/#_2","text":"\u5c31\u50cf\u51fd\u6570\u6a21\u677f\u662f\u5b9e\u4f8b\u5316\u51fd\u6570\u7684\u6a21\u677f\u5b9a\u4e49\u4e00\u6837\uff0c\u7c7b\u6a21\u677f\u662f\u5b9e\u4f8b\u5316\u7c7b\u7c7b\u578b\u7684\u6a21\u677f\u5b9a\u4e49\u3002 \u63d0\u9192 \u6240\u8c13 \u201c \u7c7b\u7c7b\u578b \u201d \u6307\u7684\u662f\u7ed3\u6784\u4f53\u3001\u7c7b\u6216\u8054\u5408\u4f53\u3002\u867d\u7136\u6211\u4eec\u4ee5\u7ed3\u6784\u4f53\u7684\u7c7b\u6a21\u677f\u8fdb\u884c\u6f14\u793a\uff0c\u4f46\u662f\u5b83\u540c\u6837\u9002\u7528\u4e8e\u7c7b\u3002 \u56de\u5fc6\u4e00\u4e0b\uff0c\u8fd9\u91cc\u6709\u4e00\u4e2a\u6574\u578b\u6210\u5458\u7c7b\u578b\u7684 Pair \u7ed3\u6784\u4f53\u5b9a\u4e49\uff1a 1 2 3 4 5 struct Pair { int first {}; int second {}; }; \u8ba9\u6211\u4eec\u4f7f\u7528\u7c7b\u6a21\u677f\u91cd\u5199\u4e00\u4e0b\u8be5\u7ed3\u6784\u4f53\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include template < typename T > struct Pair { T first {}; T second {}; }; int main () { Pair < int > p1 { 5 , 6 }; // instantiates Pair and creates object p1 std :: cout << p1 . first << ' ' << p1 . second << '\\n' ; Pair < double > p2 { 1.2 , 3.4 }; // instantiates Pair and creates object p2 std :: cout << p2 . first << ' ' << p2 . second << '\\n' ; Pair < double > p3 { 7.8 , 9.0 }; // creates object p3 using prior definition for Pair std :: cout << p3 . first << ' ' << p3 . second << '\\n' ; return 0 ; } \u5c31\u50cf\u51fd\u6570\u6a21\u677f\u4e00\u6837\uff0c\u6211\u4eec\u4ece\u6a21\u677f\u5f62\u53c2\u58f0\u660e\u5f00\u59cb\u5b9a\u4e49\u7c7b\u6a21\u677f\uff0c\u5373\u5148\u662f template \u5173\u952e\u5b57\u3002\u63a5\u4e0b\u6765\uff0c\u5728\u5c16\u62ec\u53f7( <> )\u4e2d\u6307\u5b9a\u7c7b\u6a21\u677f\u5c06\u4f7f\u7528\u7684\u6240\u6709\u6a21\u677f\u7c7b\u578b\u3002\u5bf9\u4e8e\u6211\u4eec\u9700\u8981\u7684\u6bcf\u4e2a\u6a21\u677f\u7c7b\u578b\uff0c\u9700\u8981\u4f7f\u7528\u5173\u952e\u5b57 typename (\u9996\u9009)\u6216 class (\u975e\u9996\u9009)\uff0c\u540e\u9762\u8ddf\u7740\u6a21\u677f\u7c7b\u578b\u7684\u540d\u79f0(\u4f8b\u5982\u3002\u201cT\u201d)\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u7684\u4e24\u4e2a\u6210\u5458\u90fd\u662f\u76f8\u540c\u7684\u7c7b\u578b\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u9700\u8981\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\u3002 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u50cf\u5f80\u5e38\u4e00\u6837\u5b9a\u4e49\u7ed3\u6784\uff0c\u53ea\u4e0d\u8fc7\u6211\u4eec\u53ef\u4ee5\u5728\u9700\u8981\u6a21\u677f\u7c7b\u578b\u7684\u5730\u65b9\u4f7f\u7528\u6a21\u677f\u7c7b\u578b( T )\uff0c\u4ee5\u4fbf\u7a0d\u540e\u7528\u5b9e\u9645\u7c7b\u578b\u66ff\u6362\u6a21\u677f\u7c7b\u578b\u3002\u5c31\u662f\u8fd9\u6837\uff01\u7c7b\u6a21\u677f\u7684\u5b9a\u4e49\u5c31\u5b8c\u6210\u4e86\u3002 \u5728 main \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9700\u8981\u8981\u7684\u4efb\u4f55\u7c7b\u578b\u6765\u5b9e\u4f8b\u5316 Pair \u5bf9\u8c61\u3002\u9996\u5148\uff0c\u6211\u4eec\u5b9e\u4f8b\u5316\u4e00\u4e2a Pair \u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u56e0\u4e3a Pair \u7684\u7c7b\u578b\u5b9a\u4e49\u8fd8\u4e0d\u5b58\u5728\uff0c\u7f16\u8bd1\u5668\u4f7f\u7528\u7c7b\u6a21\u677f\u5b9e\u4f8b\u5316\u4e00\u4e2a\u540d\u4e3a Pair \u7684\u7ed3\u6784\u4f53\u7c7b\u578b\u5b9a\u4e49\uff0c\u5176\u4e2d\u6240\u6709\u51fa\u73b0\u7684\u6a21\u677f\u7c7b\u578b T \u90fd\u88ab\u7c7b\u578b int \u66ff\u6362\u3002 \u63a5\u4e0b\u6765\uff0c\u518d\u5b9e\u4f8b\u5316\u4e00\u4e2a Pair \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u6b64\u65f6\u4f1a\u5b9e\u4f8b\u5316\u4e00\u4e2a Pair \u7c7b\u578b\u7684\u7ed3\u6784\u4f53\u5b9a\u4e49\uff0c\u5176\u4e2d T \u4f1a\u88ab\u66ff\u6362\u4e3a double \u3002\u5bf9\u4e8e p3 \u6765\u8bf4\uff0c Pair \u5df2\u7ecf\u88ab\u5b9e\u4f8b\u5316\u4e86\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u4f7f\u7528\u4e4b\u524d\u7684\u5b9a\u4e49\u3002 \u63a5\u4e0b\u6765\u662f\u4e0e\u4e0a\u9762\u76f8\u540c\u7684\u4f8b\u5b50\uff0c\u663e\u793a\u4e86\u7f16\u8bd1\u5668\u5728\u6240\u6709\u6a21\u677f\u5b9e\u4f8b\u5316\u5b8c\u6210\u540e\u5b9e\u9645\u7f16\u8bd1\u7684\u5185\u5bb9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include // A declaration for our Pair class template // (we don't need the definition any more since it's not used) template < typename T > struct Pair ; // Explicitly define what Pair looks like template <> // tells the compiler this is a template type with no template parameters struct Pair < int > { int first {}; int second {}; }; // Explicitly define what Pair looks like template <> // tells the compiler this is a template type with no template parameters struct Pair < double > { double first {}; double second {}; }; int main () { Pair < int > p1 { 5 , 6 }; // instantiates Pair and creates object p1 std :: cout << p1 . first << ' ' << p1 . second << '\\n' ; Pair < double > p2 { 1.2 , 3.4 }; // instantiates Pair and creates object p2 std :: cout << p2 . first << ' ' << p2 . second << '\\n' ; Pair < double > p3 { 7.8 , 9.0 }; // creates object p3 using prior definition for Pair std :: cout << p3 . first << ' ' << p3 . second << '\\n' ; return 0 ; } \u4f60\u53ef\u4ee5\u76f4\u63a5\u7f16\u8bd1\u6b64\u793a\u4f8b\uff0c\u5b83\u80fd\u591f\u6309\u9884\u671f\u5de5\u4f5c! \u6269\u5c55\u9605\u8bfb \u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86\u4e00\u4e2a\u79f0\u4e3a \u7c7b\u6a21\u677f\u7279\u5316 \u7684\u7279\u6027(\u4f1a\u5728 19.4 -- Class template specialization \u4e2d\u4ecb\u7ecd)\u3002\u76ee\u524d\u6211\u4eec\u8fd8\u4e0d\u9700\u8981\u638c\u63e1\u8be5\u7279\u6027\u7684\u77e5\u8bc6\u3002","title":"\u7c7b\u6a21\u677f"},{"location":"10-10-class-templates/#_3","text":"\u73b0\u5728\u8ba9\u6211\u4eec\u56de\u5230\u8ba9 max() \u51fd\u6570\u914d\u5408\u4e0d\u540c\u7c7b\u578b\u5de5\u4f5c\u7684\u6311\u6218\u3002\u56e0\u4e3a\u7f16\u8bd1\u5668\u5c06 Pair \u548c Pair \u770b\u505a\u5355\u72ec\u7684\u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6309\u5f62\u53c2\u7c7b\u578b\u533a\u5206\u7684\u91cd\u8f7d\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 constexpr int max ( Pair < int > p ) { return ( p . first > p . second ? p . first : p . second ); } constexpr double max ( Pair < double > p ) // okay: overloaded function differentiated by parameter type { return ( p . first > p . second ? p . first : p . second ); } \u867d\u7136\u4e0a\u8ff0\u4ee3\u7801\u53ef\u4ee5\u7f16\u8bd1\u4e86\uff0c\u4f46\u662f\u5b83\u6ca1\u6709\u89e3\u51b3\u4ee3\u7801\u5197\u4f59\u7684\u95ee\u9898\u3002\u6211\u4eec\u5b9e\u9645\u4e0a\u5e0c\u671b\u4e00\u4e2a\u51fd\u6570\u80fd\u591f\u63a5\u53d7\u4efb\u4f55\u7c7b\u578b\u7684\u53c2\u6570\u3002\u6362\u8a00\u4e4b\u6211\u4eec\u5e0c\u671b\u51fd\u6570\u53ef\u4ee5\u4f7f\u7528 Pair \u7c7b\u578b\u7684\u5f62\u53c2\uff0c\u5176\u4e2d T \u662f\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\u53c2\u6570\u3002\u56e0\u6b64\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u51fd\u6570\u6a21\u677f\u6765\u5b8c\u6210\u76f8\u5e94\u7684\u5de5\u4f5c\uff01 \u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u4f8b\u5b50\uff0c max() \u88ab\u5b9e\u73b0\u4e3a\u4e00\u4e2a\u51fd\u6570\u6a21\u677f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include template < typename T > struct Pair { T first {}; T second {}; }; template < typename T > constexpr T max ( Pair < T > p ) { return ( p . first > p . second ? p . first : p . second ); } int main () { Pair < int > p1 { 5 , 6 }; std :: cout << max < int > ( p1 ) << \" is larger \\n \" ; // explicit call to max Pair < double > p2 { 1.2 , 3.4 }; std :: cout << max ( p2 ) << \" is larger \\n \" ; // call to max using template argument deduction (prefer this) return 0 ; } max() \u51fd\u6570\u6a21\u677f\u975e\u5e38\u76f4\u767d\u3002\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u5411\u5176\u4f20\u9012 Pair \uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u5fc5\u987b\u4e86\u89e3 T \u662f\u4ec0\u4e48\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u5728\u51fd\u6570\u5f00\u5934\u51fa\u6dfb\u52a0\u6a21\u677f\u53c2\u6570\u58f0\u660e\uff0c\u5b9a\u4e49\u6a21\u677f\u7c7b\u578b T \u3002\u968f\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528 T \u4f5c\u4e3a\u8fd4\u56de\u7c7b\u578b\uff0c\u4ee5\u53ca\u6a21\u677f\u53c2\u6570 Pair \u3002 \u56e0\u4e3a max() \u51fd\u6570\u7684\u8c03\u7528\u57fa\u4e8e Pair \u7c7b\u578b \u5b9e\u53c2 \uff0c\u7f16\u8bd1\u5668\u4f1a\u57fa\u4e8e\u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316\u51fd\u6570 int max(Pair) \uff0c\u6a21\u677f\u4e2d\u7684 T \u88ab\u66ff\u6362\u4e3a\u4e86 int \u3002\u7f16\u8bd1\u5668\u5b9e\u9645\u4e0a\u5b9e\u4f8b\u5316\u4e86\u8fd9\u6837\u4e00\u4e2a\u51fd\u6570\uff1a 1 2 3 4 5 template <> constexpr int max ( Pair < int > p ) { return ( p . first > p . second ? p . first : p . second ); } \u4e0e\u6240\u6709\u5bf9\u51fd\u6570\u6a21\u677f\u7684\u8c03\u7528\u4e00\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u663e\u5f0f\u5730\u6307\u5b9a\u6a21\u677f\u7c7b\u578b\u5b9e\u53c2(\u4f8b\u5982 max(p1) )\uff0c\u4e5f\u53ef\u4ee5\u9690\u5f0f\u5730\u4f7f\u7528(\u4f8b\u5982 max(p2) )\uff0c\u5e76\u8ba9\u7f16\u8bd1\u5668\u4f7f\u7528 \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad \u6765\u786e\u5b9a\u6a21\u677f\u7c7b\u578b\u5b9e\u53c2\u5e94\u8be5\u662f\u4ec0\u4e48\u7c7b\u578b\u7684\u3002","title":"\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u7c7b\u6a21\u677f"},{"location":"10-10-class-templates/#_4","text":"\u7c7b\u6a21\u677f\u7684\u6210\u5458\u53ef\u4ee5\u4f7f\u7528\u6a21\u677f\u7c7b\u578b\uff0c\u4e5f\u53ef\u4ee5\u540c\u65f6\u6709\u53e6\u5916\u4e00\u90e8\u5206\u6210\u5458\u4f7f\u7528\u666e\u901a\u7c7b\u578b\uff08\u975e\u6a21\u677f\uff09\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 template < typename T > struct Foo { T first {}; // first will have whatever type T is replaced with int second {}; // second will always have type int, regardless of what type T is }; \u4e0a\u8ff0\u4ee3\u7801\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\uff1a first \u7684\u7c7b\u578b\u7531 T \u6307\u5b9a\uff0c\u800c second \u7c7b\u578b\u5219\u603b\u662f int \u3002","title":"\u540c\u65f6\u4f7f\u7528\u6a21\u677f\u7c7b\u578b\u548c\u975e\u6a21\u677f\u7c7b\u578b\u7684\u7c7b\u6a21\u677f"},{"location":"10-10-class-templates/#_5","text":"\u7c7b\u6a21\u677f\u4e5f\u53ef\u4ee5\u6709\u591a\u4e2a\u6a21\u677f\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b Pair \u7c7b\u7684\u4e24\u4e2a\u6210\u5458\u80fd\u591f\u5177\u6709\u4e0d\u540c\u7684\u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4e24\u79cd\u6a21\u677f\u7c7b\u578b\u5b9a\u4e49 Pair \u7c7b\u6a21\u677f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include template < typename T , typename U > struct Pair { T first {}; U second {}; }; template < typename T , typename U > void print ( Pair < T , U > p ) { std :: cout << '[' << p . first << \", \" << p . second << ']' ; } int main () { Pair < int , double > p1 { 1 , 2.3 }; // a pair holding an int and a double Pair < double , int > p2 { 4.5 , 6 }; // a pair holding a double and an int Pair < int , int > p3 { 7 , 8 }; // a pair holding two ints print ( p2 ); return 0 ; } \u8981\u5b9a\u4e49\u591a\u4e2a\u6a21\u677f\u7c7b\u578b\uff0c\u5728\u6a21\u677f\u53c2\u6570\u58f0\u660e\u4e2d\uff0c\u7528\u9017\u53f7\u5206\u9694\u6bcf\u4e2a\u6240\u9700\u7684\u6a21\u677f\u7c7b\u578b\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u79cd\u4e0d\u540c\u7684\u6a21\u677f\u7c7b\u578b\uff0c\u4e00\u79cd\u540d\u4e3a T \uff0c\u53e6\u4e00\u79cd\u540d\u4e3a U \u3002 T \u548c U \u7684\u5b9e\u9645\u6a21\u677f\u7c7b\u578b\u5b9e\u53c2\u53ef\u4ee5\u4e0d\u540c(\u5c31\u50cf\u4e0a\u9762\u7684 p1 \u548c p2 \u4e00\u6837)\uff0c\u4e5f\u53ef\u4ee5\u76f8\u540c(\u5c31\u50cf p3 \u4e00\u6837)\u3002","title":"\u5177\u6709\u591a\u4e2a\u6a21\u677f\u7c7b\u578b\u7684\u7c7b\u6a21\u677f"},{"location":"10-10-class-templates/#stdpair","text":"\u56e0\u4e3a\u5728C++\u4e2d\u4f7f\u7528\u4e00\u5bf9\u6570\u636e\u662f\u5f88\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u56e0\u6b64C++\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a std::pair \u7684\u7c7b\u6a21\u677f(\u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d) \uff0c\u5b83\u7684\u53e6\u4e00\u548c\u4f7f\u7528\u4e86\u591a\u4e2a\u6a21\u677f\u7c7b\u578b\u7684 Pair \u7c7b\u4e00\u6837\u3002\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 std::pair \u6765\u66ff\u6362\u6211\u4eec\u5b9a\u4e49\u7684 pair \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include template < typename T , typename U > void print ( std :: pair < T , U > p ) { std :: cout << '[' << p . first << \", \" << p . second << ']' ; } int main () { std :: pair < int , double > p1 { 1 , 2.3 }; // a pair holding an int and a double std :: pair < double , int > p2 { 4.5 , 6 }; // a pair holding a double and an int std :: pair < int , int > p3 { 7 , 8 }; // a pair holding two ints print ( p2 ); return 0 ; } \u672c\u8282\u8bfe\u6211\u4eec\u5b9a\u4e49\u4e86\u81ea\u5df1\u7684 Pair \u7c7b\u4ee5\u89e3\u91ca\u5176\u5de5\u4f5c\u539f\u7406\uff0c\u4f46\u662f\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\uff0c\u6211\u4eec\u63a8\u8350\u4f7f\u7528 std::pair \u3002","title":"std::pair"},{"location":"10-10-class-templates/#_6","text":"\u5c31\u50cf\u51fd\u6570\u6a21\u677f\u4e00\u6837\uff0c\u7c7b\u6a21\u677f\u901a\u5e38\u662f\u5728\u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\uff0c\u56e0\u6b64\u5b83\u4eec\u53ef\u4ee5\u88ab\u5305\u542b\u5230\u4efb\u4f55\u9700\u8981\u5b83\u4eec\u7684\u4ee3\u7801\u6587\u4ef6\u4e2d\u3002\u6a21\u677f\u5b9a\u4e49\u548c\u7c7b\u578b\u5b9a\u4e49\u90fd\u4e0d\u53d7 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u7684\u7ea6\u675f\uff0c\u6240\u4ee5\u8fd9\u4e0d\u4f1a\u5bfc\u81f4\u95ee\u9898\uff1a pair.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #ifndef PAIR_H #define PAIR_H template < typename T > struct Pair { T first {}; T second {}; }; template < typename T > constexpr T max ( Pair < T > p ) { return ( p . first > p . second ? p . first : p . second ); } #endif foo.cpp 1 2 3 4 5 6 7 8 #include \"pair.h\" #include void foo () { Pair < int > p1 { 1 , 2 }; std :: cout << max ( p1 ) << \" is larger \\n \" ; } main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include \"pair.h\" #include void foo (); // forward declaration for function foo() int main () { Pair < double > p2 { 3.4 , 5.6 }; std :: cout << max ( p2 ) << \" is larger \\n \" ; foo (); return 0 ; }","title":"\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528\u7c7b\u6a21\u677f"},{"location":"10-11-class-template-argument-deduction-and-deduction%20-guides/","tags":["class template","CTAD","C++17"],"text":"Key Takeaway \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad (CTAD) (C++17) \u4eceC++ 17\u5f00\u59cb\uff0c\u5f53\u4ece\u7c7b\u6a21\u677f\u5b9e\u4f8b\u5316\u4e00\u4e2a\u5bf9\u8c61\u65f6\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u4ece\u5bf9\u8c61\u7684\u521d\u59cb\u5316\u5f0f\u7684\u7c7b\u578b\u63a8\u65ad\u51fa\u6a21\u677f\u7c7b\u578b( \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad \uff0c\u7b80\u79f0CTAD)\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 #include // for std::pair int main () { std :: pair < int , int > p1 { 1 , 2 }; // explicitly specify class template std::pair (C++11 onward) std :: pair p2 { 1 , 2 }; // CTAD used to deduce std::pair from the initializers (C++17) return 0 ; } CTAD \u53ea\u6709\u5728\u7c7b\u6a21\u677f\u5217\u8868\u4e2d\u6ca1\u6709\u63d0\u4f9b\u4efb\u4f55\u53c2\u6570\u65f6\u624d\u4f1a\u8fdb\u884c\uff0c\u56e0\u6b64\u4e0b\u9762\u4ee3\u7801\u4e2d\u7684\u4e24\u79cd\u65b9\u5f0f\u90fd\u662f\u9519\u8bef\u7684\uff1a 1 2 3 4 5 6 7 8 9 #include // for std::pair int main () { std :: pair <> p1 { 1 , 2 }; // \u9519\u8bef: \u6a21\u677f\u53c2\u6570\u592a\u5c11, \u4e24\u4e2a\u53c2\u6570\u90fd\u4e0d\u4f1a\u63a8\u65ad std :: pair < int > p2 { 3 , 4 }; // \u9519\u8bef: \u6a21\u677f\u53c2\u6570\u592a\u5c11,\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e0d\u4f1a\u88ab\u63a8\u65ad return 0 ; } \u4f5c\u8005\u6ce8 \u672c\u7f51\u7ad9\u4eca\u540e\u7684\u8bb8\u591a\u8bfe\u7a0b\u90fd\u5229\u7528\u4e86CTAD\u3002\u5982\u679c\u4f7f\u7528C++14\u6807\u51c6\u7f16\u8bd1\u8fd9\u4e9b\u793a\u4f8b\uff0c\u5c06\u4f1a\u5f97\u5230\u4e00\u4e2a\u5173\u4e8e\u7f3a\u5c11\u6a21\u677f\u53c2\u6570\u7684\u9519\u8bef\u3002\u60a8\u9700\u8981\u663e\u5f0f\u5730\u5c06\u8fd9\u4e9b\u53c2\u6570\u6dfb\u52a0\u5230\u793a\u4f8b\u4e2d\uff0c\u4ee5\u4f7f\u5176\u80fd\u591f\u7f16\u8bd1\u3002 \u6a21\u677f\u53c2\u6570\u63a8\u65ad\u6307\u5357 (C++17) \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0cCTAD\u53ef\u4ee5\u5f00\u7bb1\u5373\u7528\u3002\u7136\u800c\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u9700\u8981\u4e00\u4e9b\u989d\u5916\u7684\u5e2e\u52a9\uff0c\u4ee5\u7406\u89e3\u5982\u4f55\u6b63\u786e\u5730\u63a8\u5bfc\u6a21\u677f\u5b9e\u53c2\u3002 \u4f60\u53ef\u80fd\u4f1a\u60ca\u8bb6\u5730\u53d1\u73b0\u4e0b\u9762\u7684\u7a0b\u5e8f(\u5b83\u51e0\u4e4e\u4e0e\u4e0a\u9762\u4f7f\u7528 std::pair \u7684\u4f8b\u5b50\u76f8\u540c)\u4e0d\u80fd\u5728C++ 17\u4e2d\u7f16\u8bd1\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // define our own Pair type template < typename T , typename U > struct Pair { T first {}; U second {}; }; int main () { Pair < int , int > p1 { 1 , 2 }; // ok: we're explicitly specifying the template arguments Pair p2 { 1 , 2 }; // compile error in C++17 return 0 ; } \u5982\u679c\u4f60\u5728C++17\u4e2d\u7f16\u8bd1\u5b83\uff0c\u4f60\u53ef\u80fd\u4f1a\u5f97\u5230\u4e00\u4e9b\u5173\u4e8e\u201c\u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u5bfc\u5931\u8d25\u201d\u6216\u201c\u65e0\u6cd5\u63a8\u5bfc\u6a21\u677f\u5b9e\u53c2\u201d\u6216\u201c\u6ca1\u6709\u53ef\u884c\u7684\u6784\u9020\u51fd\u6570\u6216\u63a8\u5bfc\u6307\u5357\u201d\u7684\u9519\u8bef\u3002\u8fd9\u662f\u56e0\u4e3a\u5728C++17\u4e2d\uff0cCTAD\u4e0d\u77e5\u9053\u5982\u4f55\u63a8\u5bfc\u805a\u5408\u7c7b\u6a21\u677f\u7684\u6a21\u677f\u5b9e\u53c2\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u7f16\u8bd1\u5668\u63d0\u4f9b\u6307\u5357\uff0c\u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u4f55\u63a8\u65ad\u7ed9\u5b9a\u7c7b\u6a21\u677f\u7684\u6a21\u677f\u5b9e\u53c2\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u4e3a\u7f16\u8bd1\u5668\u63d0\u4f9b\u4e86\u63a8\u65ad\u6307\u5357\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 template < typename T , typename U > struct Pair { T first {}; U second {}; }; // \u6b64\u5904\u4e3a Pair \u63d0\u4f9b\u63a8\u65ad\u6307\u5357 // \u4f7f\u7528\u53c2\u6570 T \u548c U \u521d\u59cb\u5316\u7684Pair\u5bf9\u8c61\u5e94\u8be5\u63a8\u65ad\u4e3a Pair template < typename T , typename U > Pair ( T , U ) -> Pair < T , U > ; int main () { Pair < int , int > p1 { 1 , 2 }; // \u663e\u5f0f\u6307\u5b9a\u7c7b\u6a21\u677fPair (C++11 onward) Pair p2 { 1 , 2 }; // CTAD \u4f7f\u7528\u521d\u59cb\u5316\u503c\u63a8\u65ad Pair (C++17) return 0 ; } \u4e0a\u9762\u7684\u4f8b\u5b50\u53ef\u4ee5\u5728C++17\u4e2d\u6210\u529f\u7f16\u8bd1\u3002 \u6b64\u5904 Pair \u7c7b\u7684\u7c7b\u578b\u63a8\u65ad\u6307\u5357\u5f88\u7b80\u5355\uff0c\u8ba9\u6211\u4eec\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u5427\u3002 1 2 3 4 // \u6b64\u5904\u4e3a Pair \u63d0\u4f9b\u63a8\u65ad\u6307\u5357 // \u4f7f\u7528\u53c2\u6570 T \u548c U \u521d\u59cb\u5316\u7684Pair\u5bf9\u8c61\u5e94\u8be5\u63a8\u65ad\u4e3a Pair template < typename T , typename U > Pair ( T , U ) -> Pair < T , U > ; \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u548c Pair \u7c7b\u4e00\u6837\u7684\u6a21\u677f\u5b9a\u4e49\uff0c\u56e0\u4e3a\u63a8\u65ad\u5b9a\u4e49\u7684\u76ee\u7684\u5c31\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u4f55\u63a8\u65ad Pair \u7c7b\u578b\u3002\u63a5\u4e0b\u6765\uff0c\u5728\u7bad\u5934\u7b26\u53f7\u7684\u53f3\u4fa7\uff0c\u6211\u4eec\u63d0\u4f9b\u4e86\u7528\u4e8e\u5e2e\u52a9\u7f16\u8bd1\u5668\u8fdb\u884c\u63a8\u65ad\u7684\u7c7b\u578b\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5e0c\u671b\u7f16\u8bd1\u5668\u80fd\u591f\u4e3a Pair \u7c7b\u578b\u7684\u5bf9\u8c61\u8fdb\u884c\u6a21\u677f\u7c7b\u578b\u63a8\u65ad\u3002\u6700\u540e\uff0c\u5728\u7bad\u5934\u7b26\u53f7\u7684\u5de6\u4fa7\uff0c\u6211\u4eec\u544a\u8bc9\u7f16\u8bd1\u5668\u5e94\u8be5\u5173\u6ce8\u4ec0\u4e48\u6837\u7684\u58f0\u660e\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7f16\u8bd1\u5668\u88ab\u8981\u6c42\u5173\u6ce8\u540d\u4e3a Pair \u4e14\u6709\u4e24\u4e2a\u5f62\u53c2(\u4e00\u4e2a\u662f T \u7c7b\u578b\uff0c\u4e00\u4e2a\u662f U \u7c7b\u578b)\u7684\u58f0\u660e\u3002\u8fd9\u91cc\u4e5f\u53ef\u4ee5\u5199\u4f5c Pair(T t, U u) ( t \u548c u \u662f\u53c2\u6570\u7684\u540d\u5b57\uff0c\u4f46\u662f\u56e0\u4e3a\u6211\u4eec\u4e0d\u9700\u8981\u4f7f\u7528 t \u548c u \uff0c\u6240\u4ee5\u4e5f\u6ca1\u5fc5\u8981\u7ed9\u5b83\u4eec\u4e00\u4e2a\u540d\u5b57)\u3002 \u4e0a\u8ff0\u64cd\u4f5c\u7efc\u5408\u8d77\u6765\uff0c\u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u679c\u770b\u5230\u6709\u4e00\u4e2a Pair \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u4e14\u6709\u4e24\u4e2a \u5b9e\u53c2 \uff0c\u5219\u5e94\u8be5\u5c06\u5176\u63a8\u65ad\u4e3a\u7c7b\u578b Pair \u3002 \u6240\u4ee5\u5f53\u7f16\u8bd1\u5668\u770b\u5230 Pair p2{ 1, 2 }; \u65f6\uff0c\u5b83\u4f1a\u8bf4\uff1a\u201c\u5662\uff0c\u8fd9\u662f\u4e00\u4e2a Pair \u5bf9\u8c61\uff0c\u6709\u4e24\u4e2a\u5b9e\u53c2 int \u548c int \uff0c\u6240\u4ee5\u57fa\u4e8e\u63a8\u65ad\u6307\u5357\uff0c\u6211\u5e94\u8be5\u5c06\u5176\u63a8\u65ad\u4e3a\u7c7b\u578b Pair \u201d \u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u63a5\u53d7\u5355\u4e00\u6a21\u677f\u7c7b\u578b\u7684 Pair \u7684\u7c7b\u4f3c\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 template < typename T > struct Pair { T first {}; T second {}; }; // Here's a deduction guide for our Pair // pair objects initialized with arguments of type T and T should deduce to Pair template < typename T > Pair ( T , T ) -> Pair < T > ; int main () { Pair < int > p1 { 1 , 2 }; // explicitly specify class template Pair (C++11 onward) Pair p2 { 1 , 2 }; // CTAD used to deduce Pair from the initializers (C++17) return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7c7b\u578b\u63a8\u65ad\u6307\u5357\u5c06 Pair(T, T) ( Pair \u7c7b\u578b\u4e14\u6709\u4e24\u4e2a\u7c7b\u578b\u4e3aT\u7684\u53c2\u6570 T ) \u63a8\u65ad\u4e3a\u7c7b\u578b Pair \u3002 \u4f5c\u8005\u6ce8 \u5173\u4e8e\u7c7b\u578b\u63a8\u65ad\u6307\u5357\u7684\u4e00\u4e9b\u5907\u6ce8\u3002 \u9996\u5148\uff0c std::pair (\u4ee5\u53ca\u5176\u4ed6\u6807\u51c6\u5e93\u6a21\u677f\u7c7b\u578b)\u662f\u6709\u9884\u5b9a\u4e49\u7684\u7c7b\u578b\u63a8\u65ad\u6307\u5357\u7684\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u4f7f\u7528 std::pair \u7684\u4f8b\u5b50\u662f\u53ef\u4ee5\u5728 C++17 \u4e2d\u76f4\u63a5\u7f16\u8bd1\u7684\u3002 \u5176\u6b21\uff0cC++20 \u4e3a\u7f16\u8bd1\u5668\u63d0\u4f9b\u4e86\u4e3a\u805a\u5408\u7c7b\u578b\u81ea\u52a8\u751f\u6210\u7c7b\u578b\u63a8\u65ad\u6307\u5357\u7684\u80fd\u529b\uff0c\u56e0\u6b64\u524d\u9762\u4f7f\u7528 Pair \u7684\u4f8b\u5b50\u53ef\u4ee5\u5728C++20\u7f16\u8bd1\u5668\u4e2d\u7f16\u8bd1\u3002\u8fd9\u91cc\u5047\u8bbe\u4f60\u7684\u7f16\u8bd1\u5668\u652f\u6301P1816\u7279\u6027\uff0c\u5728\u7f16\u5199\u672c\u6587\u65f6\uff0cgcc\u548cVisual Studio\u652f\u6301P1816\u7279\u6027\uff0c\u800cClang\u4e0d\u652f\u6301\u3002","title":"10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD"},{"location":"10-11-class-template-argument-deduction-and-deduction%20-guides/#ctad-c17","text":"\u4eceC++ 17\u5f00\u59cb\uff0c\u5f53\u4ece\u7c7b\u6a21\u677f\u5b9e\u4f8b\u5316\u4e00\u4e2a\u5bf9\u8c61\u65f6\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u4ece\u5bf9\u8c61\u7684\u521d\u59cb\u5316\u5f0f\u7684\u7c7b\u578b\u63a8\u65ad\u51fa\u6a21\u677f\u7c7b\u578b( \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad \uff0c\u7b80\u79f0CTAD)\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 #include // for std::pair int main () { std :: pair < int , int > p1 { 1 , 2 }; // explicitly specify class template std::pair (C++11 onward) std :: pair p2 { 1 , 2 }; // CTAD used to deduce std::pair from the initializers (C++17) return 0 ; } CTAD \u53ea\u6709\u5728\u7c7b\u6a21\u677f\u5217\u8868\u4e2d\u6ca1\u6709\u63d0\u4f9b\u4efb\u4f55\u53c2\u6570\u65f6\u624d\u4f1a\u8fdb\u884c\uff0c\u56e0\u6b64\u4e0b\u9762\u4ee3\u7801\u4e2d\u7684\u4e24\u79cd\u65b9\u5f0f\u90fd\u662f\u9519\u8bef\u7684\uff1a 1 2 3 4 5 6 7 8 9 #include // for std::pair int main () { std :: pair <> p1 { 1 , 2 }; // \u9519\u8bef: \u6a21\u677f\u53c2\u6570\u592a\u5c11, \u4e24\u4e2a\u53c2\u6570\u90fd\u4e0d\u4f1a\u63a8\u65ad std :: pair < int > p2 { 3 , 4 }; // \u9519\u8bef: \u6a21\u677f\u53c2\u6570\u592a\u5c11,\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e0d\u4f1a\u88ab\u63a8\u65ad return 0 ; } \u4f5c\u8005\u6ce8 \u672c\u7f51\u7ad9\u4eca\u540e\u7684\u8bb8\u591a\u8bfe\u7a0b\u90fd\u5229\u7528\u4e86CTAD\u3002\u5982\u679c\u4f7f\u7528C++14\u6807\u51c6\u7f16\u8bd1\u8fd9\u4e9b\u793a\u4f8b\uff0c\u5c06\u4f1a\u5f97\u5230\u4e00\u4e2a\u5173\u4e8e\u7f3a\u5c11\u6a21\u677f\u53c2\u6570\u7684\u9519\u8bef\u3002\u60a8\u9700\u8981\u663e\u5f0f\u5730\u5c06\u8fd9\u4e9b\u53c2\u6570\u6dfb\u52a0\u5230\u793a\u4f8b\u4e2d\uff0c\u4ee5\u4f7f\u5176\u80fd\u591f\u7f16\u8bd1\u3002","title":"\u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad (CTAD)\u00a0(C++17)"},{"location":"10-11-class-template-argument-deduction-and-deduction%20-guides/#c17","text":"\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0cCTAD\u53ef\u4ee5\u5f00\u7bb1\u5373\u7528\u3002\u7136\u800c\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u9700\u8981\u4e00\u4e9b\u989d\u5916\u7684\u5e2e\u52a9\uff0c\u4ee5\u7406\u89e3\u5982\u4f55\u6b63\u786e\u5730\u63a8\u5bfc\u6a21\u677f\u5b9e\u53c2\u3002 \u4f60\u53ef\u80fd\u4f1a\u60ca\u8bb6\u5730\u53d1\u73b0\u4e0b\u9762\u7684\u7a0b\u5e8f(\u5b83\u51e0\u4e4e\u4e0e\u4e0a\u9762\u4f7f\u7528 std::pair \u7684\u4f8b\u5b50\u76f8\u540c)\u4e0d\u80fd\u5728C++ 17\u4e2d\u7f16\u8bd1\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // define our own Pair type template < typename T , typename U > struct Pair { T first {}; U second {}; }; int main () { Pair < int , int > p1 { 1 , 2 }; // ok: we're explicitly specifying the template arguments Pair p2 { 1 , 2 }; // compile error in C++17 return 0 ; } \u5982\u679c\u4f60\u5728C++17\u4e2d\u7f16\u8bd1\u5b83\uff0c\u4f60\u53ef\u80fd\u4f1a\u5f97\u5230\u4e00\u4e9b\u5173\u4e8e\u201c\u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u5bfc\u5931\u8d25\u201d\u6216\u201c\u65e0\u6cd5\u63a8\u5bfc\u6a21\u677f\u5b9e\u53c2\u201d\u6216\u201c\u6ca1\u6709\u53ef\u884c\u7684\u6784\u9020\u51fd\u6570\u6216\u63a8\u5bfc\u6307\u5357\u201d\u7684\u9519\u8bef\u3002\u8fd9\u662f\u56e0\u4e3a\u5728C++17\u4e2d\uff0cCTAD\u4e0d\u77e5\u9053\u5982\u4f55\u63a8\u5bfc\u805a\u5408\u7c7b\u6a21\u677f\u7684\u6a21\u677f\u5b9e\u53c2\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u7f16\u8bd1\u5668\u63d0\u4f9b\u6307\u5357\uff0c\u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u4f55\u63a8\u65ad\u7ed9\u5b9a\u7c7b\u6a21\u677f\u7684\u6a21\u677f\u5b9e\u53c2\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u4e3a\u7f16\u8bd1\u5668\u63d0\u4f9b\u4e86\u63a8\u65ad\u6307\u5357\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 template < typename T , typename U > struct Pair { T first {}; U second {}; }; // \u6b64\u5904\u4e3a Pair \u63d0\u4f9b\u63a8\u65ad\u6307\u5357 // \u4f7f\u7528\u53c2\u6570 T \u548c U \u521d\u59cb\u5316\u7684Pair\u5bf9\u8c61\u5e94\u8be5\u63a8\u65ad\u4e3a Pair template < typename T , typename U > Pair ( T , U ) -> Pair < T , U > ; int main () { Pair < int , int > p1 { 1 , 2 }; // \u663e\u5f0f\u6307\u5b9a\u7c7b\u6a21\u677fPair (C++11 onward) Pair p2 { 1 , 2 }; // CTAD \u4f7f\u7528\u521d\u59cb\u5316\u503c\u63a8\u65ad Pair (C++17) return 0 ; } \u4e0a\u9762\u7684\u4f8b\u5b50\u53ef\u4ee5\u5728C++17\u4e2d\u6210\u529f\u7f16\u8bd1\u3002 \u6b64\u5904 Pair \u7c7b\u7684\u7c7b\u578b\u63a8\u65ad\u6307\u5357\u5f88\u7b80\u5355\uff0c\u8ba9\u6211\u4eec\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u5427\u3002 1 2 3 4 // \u6b64\u5904\u4e3a Pair \u63d0\u4f9b\u63a8\u65ad\u6307\u5357 // \u4f7f\u7528\u53c2\u6570 T \u548c U \u521d\u59cb\u5316\u7684Pair\u5bf9\u8c61\u5e94\u8be5\u63a8\u65ad\u4e3a Pair template < typename T , typename U > Pair ( T , U ) -> Pair < T , U > ; \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u548c Pair \u7c7b\u4e00\u6837\u7684\u6a21\u677f\u5b9a\u4e49\uff0c\u56e0\u4e3a\u63a8\u65ad\u5b9a\u4e49\u7684\u76ee\u7684\u5c31\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u4f55\u63a8\u65ad Pair \u7c7b\u578b\u3002\u63a5\u4e0b\u6765\uff0c\u5728\u7bad\u5934\u7b26\u53f7\u7684\u53f3\u4fa7\uff0c\u6211\u4eec\u63d0\u4f9b\u4e86\u7528\u4e8e\u5e2e\u52a9\u7f16\u8bd1\u5668\u8fdb\u884c\u63a8\u65ad\u7684\u7c7b\u578b\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5e0c\u671b\u7f16\u8bd1\u5668\u80fd\u591f\u4e3a Pair \u7c7b\u578b\u7684\u5bf9\u8c61\u8fdb\u884c\u6a21\u677f\u7c7b\u578b\u63a8\u65ad\u3002\u6700\u540e\uff0c\u5728\u7bad\u5934\u7b26\u53f7\u7684\u5de6\u4fa7\uff0c\u6211\u4eec\u544a\u8bc9\u7f16\u8bd1\u5668\u5e94\u8be5\u5173\u6ce8\u4ec0\u4e48\u6837\u7684\u58f0\u660e\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7f16\u8bd1\u5668\u88ab\u8981\u6c42\u5173\u6ce8\u540d\u4e3a Pair \u4e14\u6709\u4e24\u4e2a\u5f62\u53c2(\u4e00\u4e2a\u662f T \u7c7b\u578b\uff0c\u4e00\u4e2a\u662f U \u7c7b\u578b)\u7684\u58f0\u660e\u3002\u8fd9\u91cc\u4e5f\u53ef\u4ee5\u5199\u4f5c Pair(T t, U u) ( t \u548c u \u662f\u53c2\u6570\u7684\u540d\u5b57\uff0c\u4f46\u662f\u56e0\u4e3a\u6211\u4eec\u4e0d\u9700\u8981\u4f7f\u7528 t \u548c u \uff0c\u6240\u4ee5\u4e5f\u6ca1\u5fc5\u8981\u7ed9\u5b83\u4eec\u4e00\u4e2a\u540d\u5b57)\u3002 \u4e0a\u8ff0\u64cd\u4f5c\u7efc\u5408\u8d77\u6765\uff0c\u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u679c\u770b\u5230\u6709\u4e00\u4e2a Pair \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u4e14\u6709\u4e24\u4e2a \u5b9e\u53c2 \uff0c\u5219\u5e94\u8be5\u5c06\u5176\u63a8\u65ad\u4e3a\u7c7b\u578b Pair \u3002 \u6240\u4ee5\u5f53\u7f16\u8bd1\u5668\u770b\u5230 Pair p2{ 1, 2 }; \u65f6\uff0c\u5b83\u4f1a\u8bf4\uff1a\u201c\u5662\uff0c\u8fd9\u662f\u4e00\u4e2a Pair \u5bf9\u8c61\uff0c\u6709\u4e24\u4e2a\u5b9e\u53c2 int \u548c int \uff0c\u6240\u4ee5\u57fa\u4e8e\u63a8\u65ad\u6307\u5357\uff0c\u6211\u5e94\u8be5\u5c06\u5176\u63a8\u65ad\u4e3a\u7c7b\u578b Pair \u201d \u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u63a5\u53d7\u5355\u4e00\u6a21\u677f\u7c7b\u578b\u7684 Pair \u7684\u7c7b\u4f3c\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 template < typename T > struct Pair { T first {}; T second {}; }; // Here's a deduction guide for our Pair // pair objects initialized with arguments of type T and T should deduce to Pair template < typename T > Pair ( T , T ) -> Pair < T > ; int main () { Pair < int > p1 { 1 , 2 }; // explicitly specify class template Pair (C++11 onward) Pair p2 { 1 , 2 }; // CTAD used to deduce Pair from the initializers (C++17) return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7c7b\u578b\u63a8\u65ad\u6307\u5357\u5c06 Pair(T, T) ( Pair \u7c7b\u578b\u4e14\u6709\u4e24\u4e2a\u7c7b\u578b\u4e3aT\u7684\u53c2\u6570 T ) \u63a8\u65ad\u4e3a\u7c7b\u578b Pair \u3002 \u4f5c\u8005\u6ce8 \u5173\u4e8e\u7c7b\u578b\u63a8\u65ad\u6307\u5357\u7684\u4e00\u4e9b\u5907\u6ce8\u3002 \u9996\u5148\uff0c std::pair (\u4ee5\u53ca\u5176\u4ed6\u6807\u51c6\u5e93\u6a21\u677f\u7c7b\u578b)\u662f\u6709\u9884\u5b9a\u4e49\u7684\u7c7b\u578b\u63a8\u65ad\u6307\u5357\u7684\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u4f7f\u7528 std::pair \u7684\u4f8b\u5b50\u662f\u53ef\u4ee5\u5728 C++17 \u4e2d\u76f4\u63a5\u7f16\u8bd1\u7684\u3002 \u5176\u6b21\uff0cC++20 \u4e3a\u7f16\u8bd1\u5668\u63d0\u4f9b\u4e86\u4e3a\u805a\u5408\u7c7b\u578b\u81ea\u52a8\u751f\u6210\u7c7b\u578b\u63a8\u65ad\u6307\u5357\u7684\u80fd\u529b\uff0c\u56e0\u6b64\u524d\u9762\u4f7f\u7528 Pair \u7684\u4f8b\u5b50\u53ef\u4ee5\u5728C++20\u7f16\u8bd1\u5668\u4e2d\u7f16\u8bd1\u3002\u8fd9\u91cc\u5047\u8bbe\u4f60\u7684\u7f16\u8bd1\u5668\u652f\u6301P1816\u7279\u6027\uff0c\u5728\u7f16\u5199\u672c\u6587\u65f6\uff0cgcc\u548cVisual Studio\u652f\u6301P1816\u7279\u6027\uff0c\u800cClang\u4e0d\u652f\u6301\u3002","title":"\u6a21\u677f\u53c2\u6570\u63a8\u65ad\u6307\u5357 (C++17)"},{"location":"10-2-unscoped-enumerations/","tags":["enum","unscoped enumerations"],"text":"Key Takeaway \u4f7f\u7528 enum \u5173\u952e\u5b57\u5b9a\u4e49\u7684\u679a\u4e3e\u662f \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \uff0c\u5b83\u7684\u679a\u4e3e\u503c\u88ab\u66b4\u9732\u5728\u548c\u679a\u4e3e\u7c7b\u578b\u5b9a\u4e49\u7684\u4f5c\u7528\u57df\u4e2d\uff0c\u56e0\u6b64\u66f4\u9002\u5408\u88ab\u76f4\u63a5\u5b9a\u4e49\u5728\u4f7f\u7528\u5b83\u7684\u7c7b\u4e2d \u4e00\u4e2a\u679a\u4e3e\u7c7b\u578b\u7684\u679a\u4e3e\u503c\u4e0d\u80fd\u7528\u4e8e\u53e6\u4e00\u4e2a\u679a\u4e3e\u7c7b\u578b\u7684\u5bf9\u8c61 C++ \u4e2d\u5185\u7f6e\u4e86\u5f88\u591a\u6709\u7528\u7684\u57fa\u7840\u6570\u636e\u7c7b\u578b\u548c\u7b26\u5408\u7c7b\u578b\uff08\u5206\u522b\u5728 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb \u548c 9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b \u4e2d\u8fdb\u884c\u4e86\u4ecb\u7ecd\uff09\u3002\u4f46\u662f\u8fd9\u4e9b\u5185\u7f6e\u6570\u636e\u7c7b\u578b\u5e76\u4e0d\u662f\u603b\u80fd\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u8981\u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u7684\u7a0b\u5e8f\u9700\u8981\u8ffd\u8e2a\u4e00\u4e2a\u82f9\u679c\u7684\u989c\u8272\u662f\u7ea2\u8272\u3001\u9ec4\u8272\u8fd8\u662f\u7eff\u8272\u3002\u4ea6\u6216\u8005\u6211\u4eec\u9700\u8981\u63cf\u8ff0\u4e00\u4ef6T\u6064\u7684\u53ef\u9009\u989c\u8272\uff08\u901a\u8fc7\u4e00\u4e2a\u989c\u8272\u5217\u8868\uff09\u3002\u5982\u679c\u53ea\u80fd\u4f7f\u7528\u57fa\u7840\u7c7b\u578b\u7684\u8bdd\uff0c\u4f60\u4f1a\u600e\u4e48\u505a\uff1f \u4f60\u53ef\u80fd\u4f1a\u628a\u989c\u8272\u5b58\u50a8\u4e3a\u6574\u578b\u503c\uff0c\u5e76\u9690\u5f0f\u5730\u5c06\u5176\u5173\u8054\u5230\u5bf9\u5e94\u7684\u989c\u8272(0 = red , 1 = green, 2 = blue)\uff1a 1 2 3 4 5 6 7 int main () { int appleColor { 0 }; // my apple is red int shirtColor { 1 }; // my shirt is green return 0 ; } \u4f46\u662f\u8fd9\u4e48\u505a\u5e76\u4e0d\u76f4\u89c2\uff0c\u800c\u4e14\u6211\u4eec\u8ba8\u8bba\u8fc7\u4e3a\u4ec0\u4e48\u4f7f\u7528\u9b54\u9b3c\u6570\u5b57\u662f\u4e0d\u5229\u7684\uff08 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \uff09\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7b26\u53f7\u5e38\u91cf\u6765\u907f\u514d\u9b54\u9b3c\u6570\u5b57\u7684\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 constexpr int red { 0 }; constexpr int green { 1 }; constexpr int blue { 2 }; int main () { int appleColor { red }; int shirtColor { green }; return 0 ; } \u5c3d\u7ba1\u53ef\u8bfb\u6027\u53d8\u597d\u4e86\u4e00\u4e9b\uff0c\u7a0b\u5e8f\u5458\u4ecd\u7136\u9700\u8981\u7406\u89e3 appleColor \u548c shirtColor ( int \u7c7b\u578b)\u5b58\u653e\u7684\u662f\u53e6\u5916\u4e00\u4e9b\u8868\u793a\u989c\u8272\u7684\u7b26\u53f7\u5e38\u91cf\uff08\u800c\u8fd9\u4e9b\u5e38\u91cf\u5f88\u53ef\u80fd\u88ab\u5b9a\u4e49\u5728\u5176\u4ed6\u5730\u65b9\uff0c\u751a\u81f3\u662f\u5176\u4ed6\u6587\u4ef6\u4e2d\uff09\u3002 \u4f7f\u7528 \u522b\u540d \u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u53ef\u8bfb\u6027\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 using Color = int ; // define a type alias named Color // The following color values should be used for a Color constexpr Color red { 0 }; constexpr Color green { 1 }; constexpr Color blue { 2 }; int main () { Color appleColor { red }; Color shirtColor { green }; return 0 ; } \u53ef\u8bfb\u6027\u66f4\u597d\u4e86\u3002\u4f46\u662f\u7a0b\u5e8f\u5458\u4ecd\u7136\u9700\u8981\u7406\u89e3\uff0c\u8fd9\u4e9b\u8868\u793a\u989c\u8272\u7684\u7b26\u53f7\u5e38\u91cf\u9700\u8981\u914d\u5408 Color \u7c7b\u578b\u7684\u53d8\u91cf\u6765\u4f7f\u7528\u3002\u4e0d\u8fc7\u81f3\u5c11\u73b0\u5728\u8fd9\u4e9b\u7c7b\u578b\u5177\u6709\u4e00\u4e2a\u552f\u4e00\u7684\u522b\u540d Color \uff0c\u6211\u4eec\u53ef\u4ee5\u641c\u7d22\u5b83\u5e76\u627e\u5230\u76f8\u5173\u7684\u7b26\u53f7\u5e38\u91cf\u5b9a\u4e49\u3002 \u4f46\u662f\uff0c\u56e0\u4e3a Color \u53ea\u662f int \u7684\u522b\u540d\uff0c\u6240\u4ee5\u6211\u4eec\u4ecd\u7136\u65e0\u6cd5\u4fdd\u8bc1\u8fd9\u4e9b\u7b26\u53f7\u5e38\u91cf\u88ab\u6b63\u786e\u5730\u4f7f\u7528\u3002\u6211\u4eec\u4ecd\u7136\u7531\u53ef\u80fd\u72af\u5982\u4e0b\u7684\u9519\u8bef\u3002 1 Color eyeColor { 8 }; // \u8bed\u6cd5\u6b63\u786e\uff0c\u4f46\u8bed\u4e49\u9519\u8bef\uff088\u4e0d\u662f\u4e00\u4e2a\u88ab\u5b9a\u4e49\u7684\u989c\u8272\u503c\uff09 \u4e0d\u4ec5\u5982\u6b64\uff0c\u5f53\u6211\u4eec\u8c03\u8bd5\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u8fd9\u4e9b\u53d8\u91cf\u90fd\u4f1a\u663e\u793a\u4e3a\u6574\u578b\u503c\uff0c\u800c\u4e0d\u662f\u5176\u7b26\u53f7\u542b\u4e49 ( red )\uff0c\u8fd9\u65e0\u7591\u4f1a\u8ba9debug\u53d8\u5f97\u66f4\u52a0\u56f0\u96be\u3002 \u5e78\u8fd0\u5730\u662f\uff0c\u6211\u4eec\u8fd8\u6709\u66f4\u597d\u7684\u529e\u6cd5\u3002 \u679a\u4e3e \u679a\u4e3e(\u4e5f\u79f0\u4e3a\u679a\u4e3e\u7c7b\u578b\u548c enum ) \u662f\u4e00\u79cd\u590d\u5408\u7c7b\u578b\u3002\u5728\u679a\u4e3e\u7c7b\u578b\u4e2d\uff0c\u6bcf\u4e2a\u53ef\u80fd\u7684\u503c\u90fd\u88ab\u5b9a\u4e49\u4e3a\u4e00\u4e2a \u7b26\u53f7\u5e38\u91cf \uff08\u79f0\u4e3a\u679a\u4e3e\u503c\uff09\u3002 \u56e0\u4e3a\u679a\u4e3e\u5c5e\u4e8e\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\uff08 10.1 - \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7b80\u4ecb \uff09\uff0c\u6240\u4ee5\u679a\u4e3e\u7c7b\u578b\u5728\u4f7f\u7528\u524d\u5fc5\u987b\u5148\u8fdb\u884c\u5b9a\u4e49\u3002 C++ \u652f\u6301\u4e24\u79cd\u7c7b\u578b\u7684\u679a\u4e3e\uff1a 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u548c 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u901a\u8fc7 enum \u5173\u952e\u5b57\u5b9a\u4e49\u3002 \u679a\u4e3e\u7c7b\u578b\u901a\u8fc7\u5b9e\u4f8b\u6765\u5b66\u4e60\u6548\u679c\u66f4\u597d\u3002\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u6765\u8868\u793a\u989c\u8272\u503c\u5427\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // \u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\uff0c\u540d\u4e3a Color enum Color { // \u679a\u4e3e\u503c\u5b9a\u4e49\u5728\u8fd9\u91cc // \u8fd9\u4e9b\u7b26\u53f7\u5e38\u91cf\u5b9a\u4e49\u4e86\u6240\u6709\u53ef\u80fd\u7684\u503c // \u679a\u4e3e\u503c\u901a\u8fc7\u9017\u53f7\u5206\u5272\uff0c\u800c\u4e0d\u662f\u5206\u53f7 red , green , blue , // \u7ed3\u5c3e\u7684\u9017\u53f7\u53ef\u6709\u53ef\u65e0\uff0c\u4f46\u662f\u63a8\u8350\u5199\u4e0a }; // \u679a\u4e3e\u5b9a\u4e49\u5fc5\u987b\u4ee5\u5206\u53f7\u7ed3\u5c3e int main () { // \u5b9a\u4e49\u4e00\u4e9bColor\u7c7b\u578b\u7684\u53d8\u91cf Color apple { red }; // my apple is red Color shirt { green }; // my shirt is green Color cup { blue }; // my cup is blue Color socks { white }; // \u9519\u8bef: white \u4e0d\u662f\u53ef\u7528\u7684\u679a\u4e3e\u503c Color hat { 2 }; // \u9519\u8bef: 2 \u9875\u4e0d\u662fColor\u7684\u679a\u4e3e\u503c return 0 ; } \u6211\u4eec\u9996\u5148\u4f7f\u7528 enum \u5173\u952e\u5b57\u6765\u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u6211\u4eec\u6b63\u5728\u5b9a\u4e49\u4e00\u4e2a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\uff0c\u6211\u4eec\u5c06\u5176\u547d\u540d\u4e3a Color \u3002 \u968f\u540e\uff0c\u5728\u82b1\u62ec\u53f7\u91cc\u9762\u5b9a\u4e49 Color \u7c7b\u578b\u7684\u679a\u4e3e\u503c\uff1a red \uff0c green \u548c blue \u3002\u8fd9\u4e9b\u679a\u4e3e\u503c\u6307\u5b9a\u4e86 Color \u7c7b\u578b\u80fd\u591f\u8868\u793a\u6216\u5b58\u50a8\u7684\u6240\u6709\u53ef\u80fd\u503c\u3002\u6bcf\u4e2a\u679a\u4e3e\u503c\u9700\u8981\u901a\u8fc7\u9017\u53f7\uff08\u800c\u4e0d\u662f\u5206\u53f7\uff09\u9694\u5f00\u2014\u2014\u6700\u540e\u4e00\u4e2a\u679a\u4e3e\u503c\u540e\u9762\u53ef\u4ee5\u6709\u9017\u53f7\u4e5f\u53ef\u4ee5\u6ca1\u6709\uff0c\u52a0\u4e0a\u9017\u53f7\u770b\u8d77\u6765\u4e00\u81f4\u6027\u66f4\u597d\u3002 Color \u7684\u5b9a\u4e49\u4ee5\u5206\u53f7\u7ed3\u5c3e\u3002\u81f3\u6b64\uff0c\u679a\u4e3e\u7c7b\u578b Color \u5c31\u5b9a\u4e49\u5b8c\u6210\u4e86\uff01 \u5728 main() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u521d\u59cb\u5316\u4e86\u4e09\u4e2a Color \u7c7b\u578b\u7684\u53d8\u91cf\uff1a apple \u88ab\u521d\u59cb\u5316\u4e3a red \uff0c shirt \u521d\u59cb\u5316\u4e3a green \uff0c cup \u521d\u59cb\u5316\u4e3a blue \u3002 \u7a0b\u5e8f\u4f1a\u4e3a\u8fd9\u4e09\u4e2a\u5bf9\u8c61\u5206\u914d\u5185\u5b58\u3002\u6ce8\u610f\uff0c\u5f53\u521d\u59cb\u5316\u679a\u4e3e\u7c7b\u578b\u65f6\uff0c\u6240\u7528\u7684\u521d\u59cb\u5316\u503c\u5fc5\u987b\u662f\u8be5\u7c7b\u578b\u5b9a\u4e49\u8fc7\u7684\u679a\u4e3e\u503c\u3002\u53d8\u91cf socks \u548c hat \u5e76\u4e0d\u80fd\u6210\u529f\u5b9a\u4e49\uff0c\u56e0\u4e3a white \u548c 2 \u5e76\u4e0d\u662f Color \u7684\u679a\u4e3e\u503c\u3002 \u4f5c\u8005\u6ce8 \u547d\u540d\u89c4\u5219\u5c0f\u7ed3: - \u679a\u4e3e\u548c\u679a\u4e3e\u7c7b\u578b\u6307\u7684\u662f\u7c7b\u578b\u672c\u8eab(\u4f8b\u5982 Color ) - \u679a\u4e3e\u503c\u6307\u7684\u662f\u679a\u4e3e\u4e2d\u53ef\u80fd\u503c\u6240\u5bf9\u5e94\u7684\u7b26\u53f7\u5e38\u91cf(\u4f8b\u5982 red ) \u679a\u4e3e\u7c7b\u578b\u548c\u679a\u4e3e\u503c\u7684\u547d\u540d \u6309\u7167\u60ef\u4f8b\uff0c\u679a\u4e3e\u7c7b\u578b\u7684\u540d\u5b57\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff08\u89c4\u5219\u9002\u7528\u4e8e\u6240\u6709\u81ea\u5b9a\u4e49\u7c7b\u578b\uff09\u3002 \u6ce8\u610f \u679a\u4e3e\u7c7b\u578b\u4e0d\u4e00\u5b9a\u8981\u6709\u540d\u5b57\uff0c\u4f46\u662f\u73b0\u4ee3C++\u63a8\u8350\u907f\u514d\u4f7f\u7528\u533f\u540d\u7684\u679a\u4e3e\u3002 \u679a\u4e3e\u503c\u5fc5\u987b\u6709\u540d\u5b57\u3002\u53ef\u60dc\u7684\u662f\uff0c\u5bf9\u4e8e\u679a\u4e3e\u503c\u7684\u540d\u5b57\u6682\u65f6\u8fd8\u6ca1\u6709\u7ea6\u5b9a\u4fd7\u6210\u7684\u547d\u540d\u89c4\u8303\u3002\u901a\u5e38\u7684\u9009\u62e9\u6709\uff1a\u5c0f\u5199\u5b57\u6bcd\u5f00\u5934\uff08\u4f8b\u5982 red \uff09\uff0c\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff08\u4f8b\u5982 Red \uff0c\u5168\u5927\u5199\uff08\u4f8b\u5982 RED \uff09\u3001\u5168\u5927\u5199\u4e14\u6dfb\u52a0\u5c06\u679a\u4e3e\u7c7b\u578b\u4f5c\u4e3a\u524d\u7f00\uff08\u4f8b\u5982 COLOR_RED \uff09\uff0c\u518d\u6216\u8005\u4f7f\u7528 k \u4f5c\u4e3a\u524d\u7f00\u5e76\u914d\u5408\u5927\u5c0f\u5199\u4ea4\u66ff(kColorRed)\u3002 \u73b0\u4ee3C++\u6307\u5357\u901a\u5e38\u5efa\u8bae\u907f\u514d\u4f7f\u7528\u5168\u5927\u5199\u7684\u547d\u540d\u7ea6\u5b9a\uff0c\u56e0\u4e3a\u5168\u5927\u5199\u901a\u5e38\u7528\u4e8e\u9884\u5904\u7406\u5668\u5b8f\uff0c\u53ef\u80fd\u4f1a\u53d1\u751f\u51b2\u7a81\u3002\u6211\u4eec\u8fd8\u5efa\u8bae\u907f\u514d\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684\u7ea6\u5b9a\uff0c\u56e0\u4e3a\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684\u540d\u79f0\u901a\u5e38\u4e3a\u81ea\u5b9a\u4e49\u7c7b\u578b\u4fdd\u7559\u3002 \u6700\u4f73\u5b9e\u8df5 \u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u547d\u540d\u679a\u4e3e\u7c7b\u578b\u3002\u4ee5\u5c0f\u5199\u5b57\u6bcd\u5f00\u5934\u547d\u540d\u679a\u4e3e\u503c\u3002 \u679a\u4e3e\u7c7b\u578b\u662f\u53ef\u533a\u5206\u7c7b\u578b \u4f60\u521b\u5efa\u7684\u6bcf\u4e2a\u679a\u4e3e\u7c7b\u578b\u90fd\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u4e0d\u540c\u7684\u7c7b\u578b\uff0c\u8fd9\u610f\u5473\u7740\u7f16\u8bd1\u5668\u53ef\u4ee5\u5c06\u5176\u4e0e\u5176\u4ed6\u7c7b\u578b\u533a\u5206\u5f00\u6765(\u4e0d\u50cf typedefs \u6216\u7c7b\u578b\u522b\u540d\uff0c\u5b83\u4eec\u662f\u4e00\u79cd\u522b\u540d\uff0c\u4e0e\u5176\u5bf9\u5e94\u7684\u7c7b\u578b\u5e76\u6ca1\u6709\u533a\u522b)\u3002 \u679a\u4e3e\u7c7b\u578b\u662f\u4e0d\u540c\u7684\uff0c\u56e0\u6b64\u4e00\u79cd\u679a\u4e3e\u7c7b\u578b\u4e2d\u679a\u4e3e\u503c\u4e5f\u4e0d\u80fd\u7528\u4e8e\u53e6\u4e00\u79cd\u679a\u4e3e\u7c7b\u578b\u7684\u5bf9\u8c61: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 enum Pet { cat , dog , pig , whale , }; enum Color { black , red , blue , }; int main () { Pet myPet { black }; // \u7f16\u8bd1\u9519\u8bef: black \u4e0d\u662f Pet \u7684\u679a\u4e3e\u503c Color shirt { pig }; // \u7f16\u8bd1\u9519\u8bef: pig \u4e0d\u662f Color \u7684\u679a\u4e3e\u503c return 0 ; } \u4f60\u53ef\u80fd\u4e5f\u4e0d\u60f3\u8981\u4e00\u4ef6\u201dpig\u201c\u886c\u886b\u3002 \u4f7f\u7528\u679a\u4e3e \u56e0\u4e3a\u679a\u4e3e\u5668\u662f\u63cf\u8ff0\u6027\u7684\uff0c\u5b83\u4eec\u5bf9\u4e8e\u589e\u5f3a\u4ee3\u7801\u6587\u6863\u6027\u548c\u53ef\u8bfb\u6027\u975e\u5e38\u6709\u7528\u3002\u5f53\u4f60\u7684\u76f8\u5173\u5e38\u91cf\u7684\u96c6\u5408\u5f88\u5c0f\uff0c\u5e76\u4e14\u5bf9\u8c61\u6bcf\u6b21\u53ea\u9700\u8981\u4fdd\u5b58\u5176\u4e2d\u4e00\u4e2a\u503c\u65f6\uff0c\u679a\u4e3e\u7c7b\u578b\u662f\u6700\u5408\u9002\u7684\u3002 \u661f\u671f\u51e0\u3001\u65b9\u5411\u6216\u8005\u4e00\u526f\u724c\u4e2d\u7684\u82b1\u8272\u90fd\u662f\u5e38\u89c1\u7684\u679a\u4e3e\u5b9a\u4e49\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 enum DaysOfWeek { sunday , monday , tuesday , wednesday , thursday , friday , saturday , }; enum CardinalDirections { north , east , south , west , }; enum CardSuits { clubs , diamonds , hearts , spades , }; \u6709\u65f6\u5019\u88ab\u8c03\u51fd\u6570\u9700\u8981\u50cf\u4e3b\u8c03\u51fd\u6570\u8fd4\u56de\u72b6\u6001\u7801\u4ee5\u8868\u660e\u81ea\u5df1\u662f\u5426\u6267\u884c\u6210\u529f\u3002\u4f20\u7edf\u505a\u6cd5\u662f\u4f7f\u7528\u8d1f\u503c\u6765\u4ee3\u8868\u4e0d\u540c\u7684\u9519\u8bef\u7801\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 int readFileContents () { if ( ! openFile ()) return -1 ; if ( ! readFile ()) return -2 ; if ( ! parseFile ()) return -3 ; return 0 ; // success } \u4f46\u662f\uff0c\u8fd9\u4e9b\u9b54\u9b3c\u6570\u5b57\u542b\u4e49\u975e\u5e38\u6a21\u7cca\u3002\u66f4\u597d\u7684\u529e\u6cd5\u662f\u4f7f\u7528\u679a\u4e3e\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 enum FileReadResult { readResultSuccess , readResultErrorFileOpen , readResultErrorFileRead , readResultErrorFileParse , }; FileReadResult readFileContents () { if ( ! openFile ()) return readResultErrorFileOpen ; if ( ! readFile ()) return readResultErrorFileRead ; if ( ! parseFile ()) return readResultErrorFileParse ; return readResultSuccess ; } \u7136\u540e\u8c03\u7528\u8005\u53ef\u4ee5\u5c06\u8fd4\u56de\u503c\u548c\u7279\u5b9a\u7684\u679a\u4e3e\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u76f8\u5bf9\u4e8e\u76f4\u63a5\u548c\u6574\u578b\u6570\u6bd4\u8f83\uff0c\u8fd9\u79cd\u65b9\u6cd5\u53ef\u8bfb\u6027\u66f4\u597d\u3002 1 2 3 4 5 6 7 8 if ( readFileContents () == readResultSuccess ) { // do something } else { // print error message } \u679a\u4e3e\u7c7b\u578b\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6807\u8bc6\u7b80\u5355\u6e38\u620f\u4e2d\u7684\u9053\u5177\u3001\u602a\u7269\u6216\u5730\u5f62\u7c7b\u578b\u3002\u57fa\u672c\u4e0a\uff0c\u4efb\u4f55\u76f8\u5173\u5bf9\u8c61\u7684\u5c0f\u96c6\u5408\u90fd\u53ef\u4ee5\u4f7f\u7528\u679a\u4e3e\u8868\u793a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 enum ItemType { sword , torch , potion , }; int main () { ItemType holding { torch }; return 0 ; } \u679a\u4e3e\u7c7b\u578b\u4e5f\u53ef\u4ee5\u7528\u4e8e\u51fd\u6570\u5f62\u53c2\uff0c\u8868\u793a\u7528\u6237\u7684\u8f93\u5165\u7684\u4e0d\u540c\u9009\u9879\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 enum SortOrder { alphabetical , alphabeticalReverse , numerical , }; void sortData ( SortOrder order ) { if ( order == alphabetical ) // sort data in forwards alphabetical order else if ( order == alphabeticalReverse ) // sort data in backwards alphabetical order else if ( order == numerical ) // sort data numerically } \u5f88\u591a\u7f16\u7a0b\u8bed\u8a00\u4f1a\u4f7f\u7528\u679a\u4e3e\u6765\u5b9a\u4e49\u5e03\u5c14\u7c7b\u578b\u2014\u2014\u6bd5\u7adf\u5e03\u5c14\u7c7b\u578b\u672c\u8d28\u4e0a\u5c31\u662f\u4e00\u4e2a\u5177\u6709\u4e24\u4e2a\u679a\u4e3e\u503c\u7684\u679a\u4e3e\u7c7b\u578b\uff1a false \u548c true \uff01\u4f46\u662f\uff0cC++ \u4e2d\u7684 true \u548c false \u662f\u5173\u952e\u5b57\u800c\u4e0d\u662f\u679a\u4e3e\u503c\u3002 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u4f5c\u7528\u57df \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u4e4b\u6240\u4ee5\u88ab\u8d4b\u4e88\u8fd9\u6837\u7684\u540d\u5b57\uff0c \u662f\u56e0\u4e3a\u5b83\u4f1a\u5c06\u5176\u5b9a\u4e49\u7684\u679a\u4e3e\u503c\u66b4\u9732\u5728\u548c\u81ea\u5df1\u4e00\u6837\u7684\u4f5c\u7528\u57df\u4e2d \uff08\u800c\u4e0d\u662f\u50cf\u547d\u540d\u7a7a\u95f4\u90a3\u6837\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u4f5c\u7528\u57df\u7a7a\u95f4\uff09\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 enum Color // \u8be5\u679a\u4e3e\u7c7b\u578b\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u5b9a\u4e49 { red , // \u6240\u4ee5 red \u4e5f\u4f4d\u4e8e\u5168\u5c40\u4f5c\u7528\u57df green , blue , }; int main () { Color apple { red }; // apple \u662f red \u7684 return 0 ; } Color \u679a\u4e3e\u7c7b\u578b\u88ab\u5b9a\u4e49\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\uff0c\u56e0\u6b64\u5b83\u6240\u6709\u679a\u4e3e\u503c( red \u3001 green \u548c blue ) \u90fd\u4f1a\u88ab\u653e\u5230\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u3002\u8fd9\u65e0\u7591\u4f1a\u6c61\u67d3\u5168\u5c40\u4f5c\u7528\u57df\uff0c\u800c\u4e14\u663e\u8457\u63d0\u9ad8\u4e86\u547d\u540d\u51b2\u7a81\u7684\u53ef\u80fd\u6027\u3002 \u540c\u6837\u5730\uff0c\u7531\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u76f8\u540c\u7684\u679a\u4e3e\u503c\u540d\u79f0\u4e0d\u80fd\u7528\u5728\u4e0d\u540c\u7684\u679a\u4e3e\u7c7b\u578b\u4e2d\uff08\u5982\u679c\u5b83\u4eec\u5728\u4e00\u4e2a\u4f5c\u7528\u57df\u4e2d\u88ab\u5b9a\u4e49\u7684\u8bdd\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 enum Color { red , green , blue , // blue \u5728\u5168\u5c40\u4f5c\u7528\u57df }; enum Feeling { happy , tired , blue , // \u9519\u8bef\uff1a\u547d\u540d\u51b2\u7a81 }; int main () { Color apple { red }; // my apple is red Feeling me { happy }; // I'm happy right now (even though my program doesn't compile) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4e24\u4e2a \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u90fd\u88ab\u5b9a\u4e49\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\uff0c\u4f46\u662f\u5b83\u4eec\u90fd\u5305\u542b\u4e86\u4e00\u4e2a\u540c\u540d\u7684\u679a\u4e3e\u503c blue \u3002\u8fd9\u65e0\u7591\u4f1a\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\uff0c\u7a0b\u5e8f\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\u3002 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u8fd8\u4e3a\u5b83\u4eec\u7684\u679a\u4e3e\u503c\u63d0\u4f9b\u547d\u540d\u4f5c\u7528(\u975e\u5e38\u7c7b\u4f3c\u4e8e \u547d\u540d\u7a7a\u95f4 \u4e3a\u5176\u4e2d\u58f0\u660e\u7684\u53d8\u91cf\u540d\u63d0\u4f9b\u547d\u540d\u4f5c\u7528\u57df\u533a\u57df)\u3002\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u6309\u4ee5\u4e0b\u65b9\u5f0f\u8bbf\u95ee\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7684\u679a\u4e3e\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 enum Color { red , green , blue , // blue \u4f4d\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4 }; int main () { Color apple { red }; // \u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 Color raspberry { Color :: red }; // \u4e5f\u53ef\u4ee5\u901a\u8fc7Color\u8bbf\u95ee return 0 ; } \u4e0d\u8fc7\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u679a\u4e3e\u503c\u4e0d\u4f1a\u901a\u8fc7 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u6765\u8bbf\u95ee\u3002 \u907f\u514d\u679a\u4e3e\u503c\u7684\u547d\u540d\u51b2\u7a81 \u6709\u5f88\u591a\u5e38\u89c1\u7684\u65b9\u6cd5\u53ef\u4ee5\u907f\u514d\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u503c\u7684\u547d\u540d\u51b2\u7a81\u3002\u4e00\u79cd\u9009\u62e9\u662f\u5728\u547d\u540d\u679a\u4e3e\u503c\u524d\uff0c\u52a0\u4e0a\u679a\u4e3e\u672c\u8eab\u7684\u540d\u79f0\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 enum Color { color_red , color_blue , color_green , }; enum Feeling { feeling_happy , feeling_tired , feeling_blue , // no longer has a naming collision with color_blue }; int main () { Color paint { color_blue }; Feeling me { feeling_blue }; return 0 ; } \u867d\u7136\u8fd9\u4ecd\u7136\u4f1a\u6c61\u67d3\u547d\u540d\u7a7a\u95f4\uff0c\u4f46\u901a\u8fc7\u4f7f\u540d\u79f0\u66f4\u957f\u3001\u66f4\u552f\u4e00\uff0c\u51cf\u5c11\u4e86\u547d\u540d\u51b2\u7a81\u7684\u673a\u4f1a\u3002 \u4e00\u4e2a\u66f4\u597d\u7684\u9009\u62e9\u662f\u5c06\u679a\u4e3e\u7c7b\u578b\u653e\u5728\u80fd\u591f\u63d0\u4f9b\u5355\u72ec\u4f5c\u7528\u57df\u7684\u4e1c\u897f\u4e2d\uff0c\u4f8b\u5982\u547d\u540d\u7a7a\u95f4: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 namespace color { // The names Color, red, blue, and green are defined inside namespace color enum Color { red , green , blue , }; } namespace feeling { enum Feeling { happy , tired , blue , // feeling::blue doesn't collide with color::blue }; } int main () { color :: Color paint { color :: blue }; feeling :: Feeling me { feeling :: blue }; return 0 ; } \u4f46\u8fd9\u610f\u5473\u7740\u6211\u4eec\u73b0\u5728\u5fc5\u987b\u5728\u679a\u4e3e\u548c\u679a\u4e3e\u503c\u540d\u79f0\u524d\u9762\u52a0\u4e0a\u4f5c\u7528\u57df\u540d\u79f0\u3002 \u6269\u5c55\u9605\u8bfb \u7c7b\u4e5f\u80fd\u591f\u63d0\u4f9b\u4f5c\u7528\u57df\uff0c\u901a\u5e38\u5c06\u4e0e\u7c7b\u76f8\u5173\u7684\u679a\u4e3e\u7c7b\u578b\u76f4\u63a5\u5b9a\u4e49\u5728\u7c7b\u5185\u3002\u6211\u4eec\u4f1a\u5728 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b \u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002 \u53e6\u5916\u4e00\u4e2a\u63a5\u8fd1\u65b9\u6848\u662f\u4f7f\u7528 \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e \u4f1a\u5728 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u6700\u4f73\u5b9e\u8df5 \u6700\u597d\u5c06\u679a\u4e3e\u653e\u5728\u4e00\u4e2a\u547d\u540d\u7684\u4f5c\u7528\u57df\u4e2d(\u4f8b\u5982\u547d\u540d\u7a7a\u95f4\u6216\u7c7b)\uff0c\u8fd9\u6837\u679a\u4e3e\u503c\u5c31\u4e0d\u4f1a\u6c61\u67d3\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u3002 \u6bd4\u8f83\u679a\u4e3e\u503c \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7b49\u4e8e\u53f7\u6216\u4e0d\u7b49\u53f7 ( operator== \u548c operator!= ) \u6765\u5224\u65ad\u4e24\u4e2a\u679a\u4e3e\u503c\u662f\u5426\u76f8\u540c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include enum Color { red , green , blue , }; int main () { Color shirt { blue }; if ( shirt == blue ) // if the shirt is blue std :: cout << \"Your shirt is blue!\" ; else std :: cout << \"Your shirt is not blue!\" ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 if \u8bed\u53e5\u6765\u6d4b\u8bd5 shirt \u662f\u5426\u7b49\u4e8e blue \u3002\u8fd9\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u57fa\u4e8e\u679a\u4e3e\u6240\u6301\u6709\u7684\u679a\u4e3e\u503c\u6765\u6309\u7167\u6761\u4ef6\u6267\u884c\u7a0b\u5e8f\u7684\u65b9\u6cd5\u3002 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u66f4\u591a\u5730\u4f7f\u7528\u5b83\u3002","title":"10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b"},{"location":"10-2-unscoped-enumerations/#_1","text":"\u679a\u4e3e(\u4e5f\u79f0\u4e3a\u679a\u4e3e\u7c7b\u578b\u548c enum ) \u662f\u4e00\u79cd\u590d\u5408\u7c7b\u578b\u3002\u5728\u679a\u4e3e\u7c7b\u578b\u4e2d\uff0c\u6bcf\u4e2a\u53ef\u80fd\u7684\u503c\u90fd\u88ab\u5b9a\u4e49\u4e3a\u4e00\u4e2a \u7b26\u53f7\u5e38\u91cf \uff08\u79f0\u4e3a\u679a\u4e3e\u503c\uff09\u3002 \u56e0\u4e3a\u679a\u4e3e\u5c5e\u4e8e\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\uff08 10.1 - \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7b80\u4ecb \uff09\uff0c\u6240\u4ee5\u679a\u4e3e\u7c7b\u578b\u5728\u4f7f\u7528\u524d\u5fc5\u987b\u5148\u8fdb\u884c\u5b9a\u4e49\u3002 C++ \u652f\u6301\u4e24\u79cd\u7c7b\u578b\u7684\u679a\u4e3e\uff1a 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u548c 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09","title":"\u679a\u4e3e"},{"location":"10-2-unscoped-enumerations/#_2","text":"\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u901a\u8fc7 enum \u5173\u952e\u5b57\u5b9a\u4e49\u3002 \u679a\u4e3e\u7c7b\u578b\u901a\u8fc7\u5b9e\u4f8b\u6765\u5b66\u4e60\u6548\u679c\u66f4\u597d\u3002\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u6765\u8868\u793a\u989c\u8272\u503c\u5427\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // \u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\uff0c\u540d\u4e3a Color enum Color { // \u679a\u4e3e\u503c\u5b9a\u4e49\u5728\u8fd9\u91cc // \u8fd9\u4e9b\u7b26\u53f7\u5e38\u91cf\u5b9a\u4e49\u4e86\u6240\u6709\u53ef\u80fd\u7684\u503c // \u679a\u4e3e\u503c\u901a\u8fc7\u9017\u53f7\u5206\u5272\uff0c\u800c\u4e0d\u662f\u5206\u53f7 red , green , blue , // \u7ed3\u5c3e\u7684\u9017\u53f7\u53ef\u6709\u53ef\u65e0\uff0c\u4f46\u662f\u63a8\u8350\u5199\u4e0a }; // \u679a\u4e3e\u5b9a\u4e49\u5fc5\u987b\u4ee5\u5206\u53f7\u7ed3\u5c3e int main () { // \u5b9a\u4e49\u4e00\u4e9bColor\u7c7b\u578b\u7684\u53d8\u91cf Color apple { red }; // my apple is red Color shirt { green }; // my shirt is green Color cup { blue }; // my cup is blue Color socks { white }; // \u9519\u8bef: white \u4e0d\u662f\u53ef\u7528\u7684\u679a\u4e3e\u503c Color hat { 2 }; // \u9519\u8bef: 2 \u9875\u4e0d\u662fColor\u7684\u679a\u4e3e\u503c return 0 ; } \u6211\u4eec\u9996\u5148\u4f7f\u7528 enum \u5173\u952e\u5b57\u6765\u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u6211\u4eec\u6b63\u5728\u5b9a\u4e49\u4e00\u4e2a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\uff0c\u6211\u4eec\u5c06\u5176\u547d\u540d\u4e3a Color \u3002 \u968f\u540e\uff0c\u5728\u82b1\u62ec\u53f7\u91cc\u9762\u5b9a\u4e49 Color \u7c7b\u578b\u7684\u679a\u4e3e\u503c\uff1a red \uff0c green \u548c blue \u3002\u8fd9\u4e9b\u679a\u4e3e\u503c\u6307\u5b9a\u4e86 Color \u7c7b\u578b\u80fd\u591f\u8868\u793a\u6216\u5b58\u50a8\u7684\u6240\u6709\u53ef\u80fd\u503c\u3002\u6bcf\u4e2a\u679a\u4e3e\u503c\u9700\u8981\u901a\u8fc7\u9017\u53f7\uff08\u800c\u4e0d\u662f\u5206\u53f7\uff09\u9694\u5f00\u2014\u2014\u6700\u540e\u4e00\u4e2a\u679a\u4e3e\u503c\u540e\u9762\u53ef\u4ee5\u6709\u9017\u53f7\u4e5f\u53ef\u4ee5\u6ca1\u6709\uff0c\u52a0\u4e0a\u9017\u53f7\u770b\u8d77\u6765\u4e00\u81f4\u6027\u66f4\u597d\u3002 Color \u7684\u5b9a\u4e49\u4ee5\u5206\u53f7\u7ed3\u5c3e\u3002\u81f3\u6b64\uff0c\u679a\u4e3e\u7c7b\u578b Color \u5c31\u5b9a\u4e49\u5b8c\u6210\u4e86\uff01 \u5728 main() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u521d\u59cb\u5316\u4e86\u4e09\u4e2a Color \u7c7b\u578b\u7684\u53d8\u91cf\uff1a apple \u88ab\u521d\u59cb\u5316\u4e3a red \uff0c shirt \u521d\u59cb\u5316\u4e3a green \uff0c cup \u521d\u59cb\u5316\u4e3a blue \u3002 \u7a0b\u5e8f\u4f1a\u4e3a\u8fd9\u4e09\u4e2a\u5bf9\u8c61\u5206\u914d\u5185\u5b58\u3002\u6ce8\u610f\uff0c\u5f53\u521d\u59cb\u5316\u679a\u4e3e\u7c7b\u578b\u65f6\uff0c\u6240\u7528\u7684\u521d\u59cb\u5316\u503c\u5fc5\u987b\u662f\u8be5\u7c7b\u578b\u5b9a\u4e49\u8fc7\u7684\u679a\u4e3e\u503c\u3002\u53d8\u91cf socks \u548c hat \u5e76\u4e0d\u80fd\u6210\u529f\u5b9a\u4e49\uff0c\u56e0\u4e3a white \u548c 2 \u5e76\u4e0d\u662f Color \u7684\u679a\u4e3e\u503c\u3002 \u4f5c\u8005\u6ce8 \u547d\u540d\u89c4\u5219\u5c0f\u7ed3: - \u679a\u4e3e\u548c\u679a\u4e3e\u7c7b\u578b\u6307\u7684\u662f\u7c7b\u578b\u672c\u8eab(\u4f8b\u5982 Color ) - \u679a\u4e3e\u503c\u6307\u7684\u662f\u679a\u4e3e\u4e2d\u53ef\u80fd\u503c\u6240\u5bf9\u5e94\u7684\u7b26\u53f7\u5e38\u91cf(\u4f8b\u5982 red )","title":"\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b"},{"location":"10-2-unscoped-enumerations/#_3","text":"\u6309\u7167\u60ef\u4f8b\uff0c\u679a\u4e3e\u7c7b\u578b\u7684\u540d\u5b57\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff08\u89c4\u5219\u9002\u7528\u4e8e\u6240\u6709\u81ea\u5b9a\u4e49\u7c7b\u578b\uff09\u3002 \u6ce8\u610f \u679a\u4e3e\u7c7b\u578b\u4e0d\u4e00\u5b9a\u8981\u6709\u540d\u5b57\uff0c\u4f46\u662f\u73b0\u4ee3C++\u63a8\u8350\u907f\u514d\u4f7f\u7528\u533f\u540d\u7684\u679a\u4e3e\u3002 \u679a\u4e3e\u503c\u5fc5\u987b\u6709\u540d\u5b57\u3002\u53ef\u60dc\u7684\u662f\uff0c\u5bf9\u4e8e\u679a\u4e3e\u503c\u7684\u540d\u5b57\u6682\u65f6\u8fd8\u6ca1\u6709\u7ea6\u5b9a\u4fd7\u6210\u7684\u547d\u540d\u89c4\u8303\u3002\u901a\u5e38\u7684\u9009\u62e9\u6709\uff1a\u5c0f\u5199\u5b57\u6bcd\u5f00\u5934\uff08\u4f8b\u5982 red \uff09\uff0c\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff08\u4f8b\u5982 Red \uff0c\u5168\u5927\u5199\uff08\u4f8b\u5982 RED \uff09\u3001\u5168\u5927\u5199\u4e14\u6dfb\u52a0\u5c06\u679a\u4e3e\u7c7b\u578b\u4f5c\u4e3a\u524d\u7f00\uff08\u4f8b\u5982 COLOR_RED \uff09\uff0c\u518d\u6216\u8005\u4f7f\u7528 k \u4f5c\u4e3a\u524d\u7f00\u5e76\u914d\u5408\u5927\u5c0f\u5199\u4ea4\u66ff(kColorRed)\u3002 \u73b0\u4ee3C++\u6307\u5357\u901a\u5e38\u5efa\u8bae\u907f\u514d\u4f7f\u7528\u5168\u5927\u5199\u7684\u547d\u540d\u7ea6\u5b9a\uff0c\u56e0\u4e3a\u5168\u5927\u5199\u901a\u5e38\u7528\u4e8e\u9884\u5904\u7406\u5668\u5b8f\uff0c\u53ef\u80fd\u4f1a\u53d1\u751f\u51b2\u7a81\u3002\u6211\u4eec\u8fd8\u5efa\u8bae\u907f\u514d\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684\u7ea6\u5b9a\uff0c\u56e0\u4e3a\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684\u540d\u79f0\u901a\u5e38\u4e3a\u81ea\u5b9a\u4e49\u7c7b\u578b\u4fdd\u7559\u3002 \u6700\u4f73\u5b9e\u8df5 \u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u547d\u540d\u679a\u4e3e\u7c7b\u578b\u3002\u4ee5\u5c0f\u5199\u5b57\u6bcd\u5f00\u5934\u547d\u540d\u679a\u4e3e\u503c\u3002","title":"\u679a\u4e3e\u7c7b\u578b\u548c\u679a\u4e3e\u503c\u7684\u547d\u540d"},{"location":"10-2-unscoped-enumerations/#_4","text":"\u4f60\u521b\u5efa\u7684\u6bcf\u4e2a\u679a\u4e3e\u7c7b\u578b\u90fd\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u4e0d\u540c\u7684\u7c7b\u578b\uff0c\u8fd9\u610f\u5473\u7740\u7f16\u8bd1\u5668\u53ef\u4ee5\u5c06\u5176\u4e0e\u5176\u4ed6\u7c7b\u578b\u533a\u5206\u5f00\u6765(\u4e0d\u50cf typedefs \u6216\u7c7b\u578b\u522b\u540d\uff0c\u5b83\u4eec\u662f\u4e00\u79cd\u522b\u540d\uff0c\u4e0e\u5176\u5bf9\u5e94\u7684\u7c7b\u578b\u5e76\u6ca1\u6709\u533a\u522b)\u3002 \u679a\u4e3e\u7c7b\u578b\u662f\u4e0d\u540c\u7684\uff0c\u56e0\u6b64\u4e00\u79cd\u679a\u4e3e\u7c7b\u578b\u4e2d\u679a\u4e3e\u503c\u4e5f\u4e0d\u80fd\u7528\u4e8e\u53e6\u4e00\u79cd\u679a\u4e3e\u7c7b\u578b\u7684\u5bf9\u8c61: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 enum Pet { cat , dog , pig , whale , }; enum Color { black , red , blue , }; int main () { Pet myPet { black }; // \u7f16\u8bd1\u9519\u8bef: black \u4e0d\u662f Pet \u7684\u679a\u4e3e\u503c Color shirt { pig }; // \u7f16\u8bd1\u9519\u8bef: pig \u4e0d\u662f Color \u7684\u679a\u4e3e\u503c return 0 ; } \u4f60\u53ef\u80fd\u4e5f\u4e0d\u60f3\u8981\u4e00\u4ef6\u201dpig\u201c\u886c\u886b\u3002","title":"\u679a\u4e3e\u7c7b\u578b\u662f\u53ef\u533a\u5206\u7c7b\u578b"},{"location":"10-2-unscoped-enumerations/#_5","text":"\u56e0\u4e3a\u679a\u4e3e\u5668\u662f\u63cf\u8ff0\u6027\u7684\uff0c\u5b83\u4eec\u5bf9\u4e8e\u589e\u5f3a\u4ee3\u7801\u6587\u6863\u6027\u548c\u53ef\u8bfb\u6027\u975e\u5e38\u6709\u7528\u3002\u5f53\u4f60\u7684\u76f8\u5173\u5e38\u91cf\u7684\u96c6\u5408\u5f88\u5c0f\uff0c\u5e76\u4e14\u5bf9\u8c61\u6bcf\u6b21\u53ea\u9700\u8981\u4fdd\u5b58\u5176\u4e2d\u4e00\u4e2a\u503c\u65f6\uff0c\u679a\u4e3e\u7c7b\u578b\u662f\u6700\u5408\u9002\u7684\u3002 \u661f\u671f\u51e0\u3001\u65b9\u5411\u6216\u8005\u4e00\u526f\u724c\u4e2d\u7684\u82b1\u8272\u90fd\u662f\u5e38\u89c1\u7684\u679a\u4e3e\u5b9a\u4e49\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 enum DaysOfWeek { sunday , monday , tuesday , wednesday , thursday , friday , saturday , }; enum CardinalDirections { north , east , south , west , }; enum CardSuits { clubs , diamonds , hearts , spades , }; \u6709\u65f6\u5019\u88ab\u8c03\u51fd\u6570\u9700\u8981\u50cf\u4e3b\u8c03\u51fd\u6570\u8fd4\u56de\u72b6\u6001\u7801\u4ee5\u8868\u660e\u81ea\u5df1\u662f\u5426\u6267\u884c\u6210\u529f\u3002\u4f20\u7edf\u505a\u6cd5\u662f\u4f7f\u7528\u8d1f\u503c\u6765\u4ee3\u8868\u4e0d\u540c\u7684\u9519\u8bef\u7801\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 int readFileContents () { if ( ! openFile ()) return -1 ; if ( ! readFile ()) return -2 ; if ( ! parseFile ()) return -3 ; return 0 ; // success } \u4f46\u662f\uff0c\u8fd9\u4e9b\u9b54\u9b3c\u6570\u5b57\u542b\u4e49\u975e\u5e38\u6a21\u7cca\u3002\u66f4\u597d\u7684\u529e\u6cd5\u662f\u4f7f\u7528\u679a\u4e3e\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 enum FileReadResult { readResultSuccess , readResultErrorFileOpen , readResultErrorFileRead , readResultErrorFileParse , }; FileReadResult readFileContents () { if ( ! openFile ()) return readResultErrorFileOpen ; if ( ! readFile ()) return readResultErrorFileRead ; if ( ! parseFile ()) return readResultErrorFileParse ; return readResultSuccess ; } \u7136\u540e\u8c03\u7528\u8005\u53ef\u4ee5\u5c06\u8fd4\u56de\u503c\u548c\u7279\u5b9a\u7684\u679a\u4e3e\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u76f8\u5bf9\u4e8e\u76f4\u63a5\u548c\u6574\u578b\u6570\u6bd4\u8f83\uff0c\u8fd9\u79cd\u65b9\u6cd5\u53ef\u8bfb\u6027\u66f4\u597d\u3002 1 2 3 4 5 6 7 8 if ( readFileContents () == readResultSuccess ) { // do something } else { // print error message } \u679a\u4e3e\u7c7b\u578b\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6807\u8bc6\u7b80\u5355\u6e38\u620f\u4e2d\u7684\u9053\u5177\u3001\u602a\u7269\u6216\u5730\u5f62\u7c7b\u578b\u3002\u57fa\u672c\u4e0a\uff0c\u4efb\u4f55\u76f8\u5173\u5bf9\u8c61\u7684\u5c0f\u96c6\u5408\u90fd\u53ef\u4ee5\u4f7f\u7528\u679a\u4e3e\u8868\u793a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 enum ItemType { sword , torch , potion , }; int main () { ItemType holding { torch }; return 0 ; } \u679a\u4e3e\u7c7b\u578b\u4e5f\u53ef\u4ee5\u7528\u4e8e\u51fd\u6570\u5f62\u53c2\uff0c\u8868\u793a\u7528\u6237\u7684\u8f93\u5165\u7684\u4e0d\u540c\u9009\u9879\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 enum SortOrder { alphabetical , alphabeticalReverse , numerical , }; void sortData ( SortOrder order ) { if ( order == alphabetical ) // sort data in forwards alphabetical order else if ( order == alphabeticalReverse ) // sort data in backwards alphabetical order else if ( order == numerical ) // sort data numerically } \u5f88\u591a\u7f16\u7a0b\u8bed\u8a00\u4f1a\u4f7f\u7528\u679a\u4e3e\u6765\u5b9a\u4e49\u5e03\u5c14\u7c7b\u578b\u2014\u2014\u6bd5\u7adf\u5e03\u5c14\u7c7b\u578b\u672c\u8d28\u4e0a\u5c31\u662f\u4e00\u4e2a\u5177\u6709\u4e24\u4e2a\u679a\u4e3e\u503c\u7684\u679a\u4e3e\u7c7b\u578b\uff1a false \u548c true \uff01\u4f46\u662f\uff0cC++ \u4e2d\u7684 true \u548c false \u662f\u5173\u952e\u5b57\u800c\u4e0d\u662f\u679a\u4e3e\u503c\u3002","title":"\u4f7f\u7528\u679a\u4e3e"},{"location":"10-2-unscoped-enumerations/#_6","text":"\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u4e4b\u6240\u4ee5\u88ab\u8d4b\u4e88\u8fd9\u6837\u7684\u540d\u5b57\uff0c \u662f\u56e0\u4e3a\u5b83\u4f1a\u5c06\u5176\u5b9a\u4e49\u7684\u679a\u4e3e\u503c\u66b4\u9732\u5728\u548c\u81ea\u5df1\u4e00\u6837\u7684\u4f5c\u7528\u57df\u4e2d \uff08\u800c\u4e0d\u662f\u50cf\u547d\u540d\u7a7a\u95f4\u90a3\u6837\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u4f5c\u7528\u57df\u7a7a\u95f4\uff09\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 enum Color // \u8be5\u679a\u4e3e\u7c7b\u578b\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u5b9a\u4e49 { red , // \u6240\u4ee5 red \u4e5f\u4f4d\u4e8e\u5168\u5c40\u4f5c\u7528\u57df green , blue , }; int main () { Color apple { red }; // apple \u662f red \u7684 return 0 ; } Color \u679a\u4e3e\u7c7b\u578b\u88ab\u5b9a\u4e49\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\uff0c\u56e0\u6b64\u5b83\u6240\u6709\u679a\u4e3e\u503c( red \u3001 green \u548c blue ) \u90fd\u4f1a\u88ab\u653e\u5230\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u3002\u8fd9\u65e0\u7591\u4f1a\u6c61\u67d3\u5168\u5c40\u4f5c\u7528\u57df\uff0c\u800c\u4e14\u663e\u8457\u63d0\u9ad8\u4e86\u547d\u540d\u51b2\u7a81\u7684\u53ef\u80fd\u6027\u3002 \u540c\u6837\u5730\uff0c\u7531\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u76f8\u540c\u7684\u679a\u4e3e\u503c\u540d\u79f0\u4e0d\u80fd\u7528\u5728\u4e0d\u540c\u7684\u679a\u4e3e\u7c7b\u578b\u4e2d\uff08\u5982\u679c\u5b83\u4eec\u5728\u4e00\u4e2a\u4f5c\u7528\u57df\u4e2d\u88ab\u5b9a\u4e49\u7684\u8bdd\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 enum Color { red , green , blue , // blue \u5728\u5168\u5c40\u4f5c\u7528\u57df }; enum Feeling { happy , tired , blue , // \u9519\u8bef\uff1a\u547d\u540d\u51b2\u7a81 }; int main () { Color apple { red }; // my apple is red Feeling me { happy }; // I'm happy right now (even though my program doesn't compile) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4e24\u4e2a \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u90fd\u88ab\u5b9a\u4e49\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\uff0c\u4f46\u662f\u5b83\u4eec\u90fd\u5305\u542b\u4e86\u4e00\u4e2a\u540c\u540d\u7684\u679a\u4e3e\u503c blue \u3002\u8fd9\u65e0\u7591\u4f1a\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\uff0c\u7a0b\u5e8f\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\u3002 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u8fd8\u4e3a\u5b83\u4eec\u7684\u679a\u4e3e\u503c\u63d0\u4f9b\u547d\u540d\u4f5c\u7528(\u975e\u5e38\u7c7b\u4f3c\u4e8e \u547d\u540d\u7a7a\u95f4 \u4e3a\u5176\u4e2d\u58f0\u660e\u7684\u53d8\u91cf\u540d\u63d0\u4f9b\u547d\u540d\u4f5c\u7528\u57df\u533a\u57df)\u3002\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u6309\u4ee5\u4e0b\u65b9\u5f0f\u8bbf\u95ee\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7684\u679a\u4e3e\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 enum Color { red , green , blue , // blue \u4f4d\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4 }; int main () { Color apple { red }; // \u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 Color raspberry { Color :: red }; // \u4e5f\u53ef\u4ee5\u901a\u8fc7Color\u8bbf\u95ee return 0 ; } \u4e0d\u8fc7\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u679a\u4e3e\u503c\u4e0d\u4f1a\u901a\u8fc7 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u6765\u8bbf\u95ee\u3002","title":"\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u4f5c\u7528\u57df"},{"location":"10-2-unscoped-enumerations/#_7","text":"\u6709\u5f88\u591a\u5e38\u89c1\u7684\u65b9\u6cd5\u53ef\u4ee5\u907f\u514d\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u503c\u7684\u547d\u540d\u51b2\u7a81\u3002\u4e00\u79cd\u9009\u62e9\u662f\u5728\u547d\u540d\u679a\u4e3e\u503c\u524d\uff0c\u52a0\u4e0a\u679a\u4e3e\u672c\u8eab\u7684\u540d\u79f0\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 enum Color { color_red , color_blue , color_green , }; enum Feeling { feeling_happy , feeling_tired , feeling_blue , // no longer has a naming collision with color_blue }; int main () { Color paint { color_blue }; Feeling me { feeling_blue }; return 0 ; } \u867d\u7136\u8fd9\u4ecd\u7136\u4f1a\u6c61\u67d3\u547d\u540d\u7a7a\u95f4\uff0c\u4f46\u901a\u8fc7\u4f7f\u540d\u79f0\u66f4\u957f\u3001\u66f4\u552f\u4e00\uff0c\u51cf\u5c11\u4e86\u547d\u540d\u51b2\u7a81\u7684\u673a\u4f1a\u3002 \u4e00\u4e2a\u66f4\u597d\u7684\u9009\u62e9\u662f\u5c06\u679a\u4e3e\u7c7b\u578b\u653e\u5728\u80fd\u591f\u63d0\u4f9b\u5355\u72ec\u4f5c\u7528\u57df\u7684\u4e1c\u897f\u4e2d\uff0c\u4f8b\u5982\u547d\u540d\u7a7a\u95f4: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 namespace color { // The names Color, red, blue, and green are defined inside namespace color enum Color { red , green , blue , }; } namespace feeling { enum Feeling { happy , tired , blue , // feeling::blue doesn't collide with color::blue }; } int main () { color :: Color paint { color :: blue }; feeling :: Feeling me { feeling :: blue }; return 0 ; } \u4f46\u8fd9\u610f\u5473\u7740\u6211\u4eec\u73b0\u5728\u5fc5\u987b\u5728\u679a\u4e3e\u548c\u679a\u4e3e\u503c\u540d\u79f0\u524d\u9762\u52a0\u4e0a\u4f5c\u7528\u57df\u540d\u79f0\u3002 \u6269\u5c55\u9605\u8bfb \u7c7b\u4e5f\u80fd\u591f\u63d0\u4f9b\u4f5c\u7528\u57df\uff0c\u901a\u5e38\u5c06\u4e0e\u7c7b\u76f8\u5173\u7684\u679a\u4e3e\u7c7b\u578b\u76f4\u63a5\u5b9a\u4e49\u5728\u7c7b\u5185\u3002\u6211\u4eec\u4f1a\u5728 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b \u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002 \u53e6\u5916\u4e00\u4e2a\u63a5\u8fd1\u65b9\u6848\u662f\u4f7f\u7528 \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e \u4f1a\u5728 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u6700\u4f73\u5b9e\u8df5 \u6700\u597d\u5c06\u679a\u4e3e\u653e\u5728\u4e00\u4e2a\u547d\u540d\u7684\u4f5c\u7528\u57df\u4e2d(\u4f8b\u5982\u547d\u540d\u7a7a\u95f4\u6216\u7c7b)\uff0c\u8fd9\u6837\u679a\u4e3e\u503c\u5c31\u4e0d\u4f1a\u6c61\u67d3\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u3002","title":"\u907f\u514d\u679a\u4e3e\u503c\u7684\u547d\u540d\u51b2\u7a81"},{"location":"10-2-unscoped-enumerations/#_8","text":"\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7b49\u4e8e\u53f7\u6216\u4e0d\u7b49\u53f7 ( operator== \u548c operator!= ) \u6765\u5224\u65ad\u4e24\u4e2a\u679a\u4e3e\u503c\u662f\u5426\u76f8\u540c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include enum Color { red , green , blue , }; int main () { Color shirt { blue }; if ( shirt == blue ) // if the shirt is blue std :: cout << \"Your shirt is blue!\" ; else std :: cout << \"Your shirt is not blue!\" ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 if \u8bed\u53e5\u6765\u6d4b\u8bd5 shirt \u662f\u5426\u7b49\u4e8e blue \u3002\u8fd9\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u57fa\u4e8e\u679a\u4e3e\u6240\u6301\u6709\u7684\u679a\u4e3e\u503c\u6765\u6309\u7167\u6761\u4ef6\u6267\u884c\u7a0b\u5e8f\u7684\u65b9\u6cd5\u3002 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u66f4\u591a\u5730\u4f7f\u7528\u5b83\u3002","title":"\u6bd4\u8f83\u679a\u4e3e\u503c"},{"location":"10-3-unscoped-enumeration-input-and-output/","tags":["enum","unscoped enumerations"],"text":"Key Takeaway \u5728\u4e0a\u8282\u8bfe\u4e2d( 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b )\u6211\u4eec\u63d0\u5230\uff0c\u679a\u4e3e\u662f\u4e00\u79cd \u7b26\u53f7\u5e38\u91cf \u3002\u4f46\u662f\u6211\u4eec\u6ca1\u8bf4\u7684\u662f\uff0c\u679a\u4e3e\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u6574\u578b\u7b26\u53f7\u5e38\u91cf\u3002\u56e0\u6b64\uff0c\u679a\u4e3e\u7c7b\u578b\u4e2d\u4fdd\u5b58\u7684\u5176\u5b9e\u662f\u4e00\u4e2a\u6574\u578b\u503c\u3002 \u5728\u8fd9\u4e00\u70b9\u4e0a\uff0c\u679a\u4e3e\u7c7b\u578b\u7c7b\u4f3c\u4e8e\u5b57\u7b26 ( 4.11 - \u5b57\u7b26 )\u3002\u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 char ch { 'A' }; char \u5b9e\u9645\u4e0a\u5c31\u662f\u4e00\u4e2a1\u4e2a\u5b57\u8282\u7684\u6574\u578b\u6570\uff0c\u5b57\u7b26 A \u4f1a\u88ab\u8f6c\u6362\u4e3a\u6574\u578b\u6570\uff08\u8f6c\u6362\u4e3a65\uff09\u5e76\u50a8\u5b58\u3002 \u5f53\u6211\u4eec\u5b9a\u4e49\u679a\u4e3e\u7684\u65f6\u5019\uff0c\u6bcf\u4e2a\u679a\u4e3e\u503c\u4f1a\u6839\u636e\u5176\u6240\u5728\u4f4d\u7f6e\u88ab\u81ea\u52a8\u8d4b\u503c\u4e00\u4e2a\u6574\u578b\u503c\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u7b2c\u4e00\u4e2a\u679a\u4e3e\u503c\u4f1a\u88ab\u8d4b\u503c\u4e3a0\uff0c\u540e\u9762\u7684\u679a\u4e3e\u503c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u6bcf\u4e2a\u90fd\u6bd4\u524d\u4e00\u4e2a\u503c\u5927\u4e00\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include enum Color { black , // assigned 0 red , // assigned 1 blue , // assigned 2 green , // assigned 3 white , // assigned 4 cyan , // assigned 5 yellow , // assigned 6 magenta , // assigned 7 }; int main () { Color shirt { blue }; // This actually stores the integral value 2 return 0 ; } \u6211\u4eec\u4e5f\u53ef\u4ee5\u663e\u5f0f\u5730\u5b9a\u4e49\u679a\u4e3e\u6570\u7684\u503c\uff0c\u800c\u4e14\u8fd9\u4e9b\u6574\u6570\u503c\u53ef\u4ee5\u662f\u6574\u6570\u6216\u8d1f\u6570\uff0c\u5e76\u4e14\u53ef\u4ee5\u4e0e\u5176\u4ed6\u679a\u4e3e\u503c\u5171\u4eab\u76f8\u540c\u7684\u503c\u3002\u4efb\u4f55\u672a\u5b9a\u4e49\u7684\u679a\u4e3e\u6570\u90fd\u88ab\u8d4b\u4e88\u4e00\u4e2a\u6bd4\u524d\u4e00\u4e2a\u679a\u4e3e\u6570\u59271\u7684\u503c\u3002 1 2 3 4 5 6 7 8 9 enum Animal { cat = -3 , dog , // assigned -2 pig , // assigned -1 horse = 5 , giraffe = 5 , // shares same value as horse chicken , // assigned 6 }; \u6ce8\u610f\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c horse \u548c giraffe \u5177\u6709\u76f8\u540c\u7684\u503c\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4e9b\u679a\u4e3e\u503c\u662f\u65e0\u533a\u522b\u7684\u2014\u2014 horse \u548c giraffe \u662f\u53ef\u4ee5\u4e92\u6362\u7684\u3002\u5c3d\u7ba1C++\u5141\u8bb8\u4f60\u8fd9\u4e48\u505a\uff0c\u4f46\u662f\u6211\u4eec\u5e94\u8be5\u5c3d\u91cf\u907f\u514d\u5c06\u4e00\u4e2a\u679a\u4e3e\u7c7b\u578b\u4e2d\u7684\u4e24\u4e2a\u679a\u4e3e\u503c\u8d4b\u503c\u4e3a\u76f8\u540c\u7684\u6570\u3002 \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u7ed9\u679a\u4e3e\u503c\u663e\u5f0f\u8d4b\u503c\uff0c\u9664\u975e\u6709\u4ee4\u4eba\u4fe1\u670d\u7684\u7406\u7531\u8fd9\u6837\u505a\u3002 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u4f1a\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u6574\u578b\u503c \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include enum Color { black , // assigned 0 red , // assigned 1 blue , // assigned 2 green , // assigned 3 white , // assigned 4 cyan , // assigned 5 yellow , // assigned 6 magenta , // assigned 7 }; int main () { Color shirt { blue }; std :: cout << \"Your shirt is \" << shirt ; // what does this do? return 0 ; } \u56e0\u4e3a\u679a\u4e3e\u7c7b\u578b\u4fdd\u5b58\u7684\u662f\u6574\u578b\u503c\uff0c\u6240\u4ee5\u5982\u4f60\u6240\u671f\u5f85\u7684\u90a3\u6837\uff0c\u4e0a\u9762\u7684\u7a0b\u5e8f\u6253\u5370\uff1a 1 Your shirt is 2 \u5f53\u679a\u4e3e\u7c7b\u578b\u7528\u4e8e\u51fd\u6570\u8c03\u7528\u6216\u4e0e\u8fd0\u7b97\u7b26\u4e00\u8d77\u4f7f\u7528\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u9996\u5148\u5c1d\u8bd5\u67e5\u627e\u4e0e\u679a\u4e3e\u7c7b\u578b\u5339\u914d\u7684\u51fd\u6570\u6216\u8fd0\u7b97\u7b26\u3002\u4f8b\u5982\uff0c\u5f53\u7f16\u8bd1\u5668\u8bd5\u56fe\u7f16\u8bd1 std::cout << shirt \u65f6\uff0c\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u67e5\u770b operator<< \u662f\u5426\u77e5\u9053\u5982\u4f55\u5c06 Color \u7c7b\u578b\u7684\u5bf9\u8c61(\u56e0\u4e3a shirt \u7684\u7c7b\u578b\u662f Color )\u6253\u5370\u5230 std::cout \u3002\u5b83\u4e0d\u77e5\u9053\u3002 \u5982\u679c\u7f16\u8bd1\u5668\u65e0\u6cd5\u627e\u5230\u5339\u914d\u7684\u8fd0\u7b97\u7b26\uff0c\u5219\u5b83\u4f1a\u5c06 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u6574\u578b\u3002\u56e0\u4e3a std::cout \u77e5\u9053\u5982\u4f55\u6253\u5370\u4e00\u4e2a\u6574\u578b\uff0c\u6240\u4ee5 shirt \u4f1a\u88ab\u8f6c\u6362\u4e3a\u6574\u578b\u5e76\u6253\u5370\u51fa2\u3002 \u6253\u5370\u679a\u4e3e\u503c\u7684\u540d\u5b57 \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5c06\u679a\u4e3e\u8f6c\u6362\u4e3a\u6574\u578b\u6253\u5370\u51fa\u6765\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c\u3002\u6211\u4eec\u80af\u5b9a\u662f\u5e0c\u671b\u80fd\u591f\u5c06\u679a\u4e3e\u503c\u7684\u672c\u540d\u6253\u5370\u51fa\u6765 (\u4f8b\u5982 blue )\u3002\u4f46\u662f\uff0c\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e00\u70b9\u6211\u4eec\u9700\u8981\u4e00\u4e9b\u65b9\u6cd5\u5c06\u679a\u4e3e\u5728\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u6253\u5370\u51fa\u6765( \"blue\" )\u3002 \u622a\u81f3C++ 20, C++\u8fd8\u6ca1\u6709\u63d0\u4f9b\u4efb\u4f55\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u81ea\u5df1\u5bfb\u627e\u89e3\u51b3\u65b9\u6848\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u8fd9\u5e76\u4e0d\u96be\u3002\u5b9e\u73b0\u8fd9\u4e00\u70b9\u7684\u5178\u578b\u65b9\u6cd5\u662f\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u679a\u4e3e\u7c7b\u578b\u4f5c\u4e3a\u53c2\u6570\uff0c\u7136\u540e\u8f93\u51fa\u76f8\u5e94\u7684\u5b57\u7b26\u4e32(\u6216\u5c06\u5b57\u7b26\u4e32\u8fd4\u56de\u7ed9\u8c03\u7528\u8005)\u3002 \u5178\u578b\u7684\u65b9\u6cd5\u662f\u5224\u65ad\u5f53\u524d\u9700\u8981\u6253\u5370\u7684\u662f\u54ea\u4e2a\u7684\u679a\u4e3e\u503c\uff1a 1 2 3 4 5 6 7 8 // Using if-else for this is inefficient void printColor ( Color color ) { if ( color == black ) std :: cout << \"black\" ; else if ( color == red ) std :: cout << \"red\" ; else if ( color == blue ) std :: cout << \"blue\" ; else std :: cout << \"???\" ; } \u4f46\u662f\uff0c\u4e3a\u6b64\u4f7f\u7528\u4e00\u7cfb\u5217if-else\u8bed\u53e5\u662f\u4f4e\u6548\u7684\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u591a\u6b21\u6bd4\u8f83\u624d\u80fd\u627e\u5230\u5339\u914d\u3002\u5b8c\u6210\u540c\u6837\u4efb\u52a1\u7684\u4e00\u79cd\u66f4\u6709\u6548\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 switch \u8bed\u53e5\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8fd8\u5c06\u4ee5 std::string \u7684\u5f62\u5f0f\u8fd4\u56de Color \uff0c\u4ee5\u7ed9\u8c03\u7528\u8005\u66f4\u591a\u7684\u7075\u6d3b\u6027\u6765\u5bf9\u540d\u79f0\u505a\u4efb\u4f55\u60f3\u505a\u7684\u4e8b\u60c5(\u5305\u62ec\u6253\u5370\u5b83): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include #include enum Color { black , red , blue , }; // We'll show a better version of this for C++17 below std :: string getColor ( Color color ) { switch ( color ) { case black : return \"black\" ; case red : return \"red\" ; case blue : return \"blue\" ; default : return \"???\" ; } } int main () { Color shirt { blue }; std :: cout << \"Your shirt is \" << getColor ( shirt ) << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Your shirt is blue \u8fd9\u4e48\u505a\u6bd4\u4f7f\u7528\u591a\u4e2aif-else\u5224\u65ad\u66f4\u9ad8\u6548\uff08\u56e0\u4e3aswitch\u8bed\u53e5\u6bd4if\u66f4\u9ad8\u6548\uff09\uff0c\u800c\u4e14\u53ef\u8bfb\u6027\u4e5f\u66f4\u597d\u3002\u4f46\u662f\uff0c\u8fd9\u4e2a\u7248\u672c\u7684\u4ee3\u7801\u4e5f\u4e0d\u662f\u5f88\u9ad8\u6548\uff0c\u56e0\u4e3a\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u90fd\u5fc5\u987b\u521b\u5efa\u5e76\u8fd4\u56de\u4e00\u4e2a std::string (\u5f00\u9500\u5f88\u5927)\u3002 \u5728 C++17 \u4e2d\uff0c\u66f4\u9ad8\u6548\u7684\u505a\u6cd5\u662f\u5c06 std::string \u66ff\u6362\u4e3a std::string_view \u3002 std::string_view \u4f1a\u4ee5\u67d0\u79cd\u62f7\u8d1d\u5f00\u9500\u66f4\u5c0f\u7684\u65b9\u5f0f\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include #include // C++17 enum Color { black , red , blue , }; constexpr std :: string_view getColor ( Color color ) // C++17 { switch ( color ) { case black : return \"black\" ; case red : return \"red\" ; case blue : return \"blue\" ; default : return \"???\" ; } } int main () { Color shirt { blue }; std :: cout << \"Your shirt is \" << getColor ( shirt ) << '\\n' ; return 0 ; } COPY \u76f8\u5173\u5185\u5bb9 Constexpr \u8fd4\u56de\u7c7b\u578b\u5728 6.14 - Constexpr \u548c consteval \u51fd\u6570 \u4e2d\u4ecb\u7ecd\u3002 \u8ba9 operator<< \u77e5\u9053\u5982\u4f55\u6253\u5370\u679a\u4e3e\u503c \u5c3d\u7ba1\u4e0a\u9762\u7684\u4f8b\u5b50\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\uff0c\u4f46\u662f\u6211\u4eec\u5fc5\u987b\u8981\u80fd\u591f\u8bb0\u4f4f\u54ea\u4e2a\u51fd\u6570\u662f\u7528\u6765\u83b7\u53d6\u679a\u4e3e\u503c\u540d\u5b57\u7684\u3002\u5c3d\u7ba1\u591a\u6570\u60c5\u51b5\u4e0b\u8fd9\u4e5f\u5e76\u4e0d\u9ebb\u70e6\uff0c\u4f46\u662f\u5982\u679c\u679a\u4e3e\u7c7b\u578b\u5f88\u591a\u7684\u65f6\u5019\uff0c\u8fd8\u662f\u5f88\u53ef\u80fd\u4f1a\u5e26\u6765\u95ee\u9898\u3002\u4f7f\u7528\u8fd0\u7b97\u7b26\u91cd\u8f7d\uff0c\u6211\u4eec\u53ef\u4ee5\u6559\u4f1a\u8fd0\u7b97\u7b26 operator<< \u5982\u4f55\u6253\u5370\u7a0b\u5e8f\u5b9a\u4e49\u679a\u4e3e\u7c7b\u578b\uff01\u7531\u4e8e\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u5148\u5c06\u4e0b\u9762\u7684\u4ee3\u7801\u5f53\u505a\u4e00\u79cd\u9b54\u6cd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include enum Color { black , red , blue , }; // Teach operator<< how to print a Color // Consider this magic for now since we haven't explained any of the concepts it uses yet // std::ostream is the type of std::cout // The return type and parameter type are references (to prevent copies from being made)! std :: ostream & operator << ( std :: ostream & out , Color color ) { switch ( color ) { case black : out << \"black\" ; break ; case red : out << \"red\" ; break ; case blue : out << \"blue\" ; break ; default : out << \"???\" ; break ; } return out ; } int main () { Color shirt { blue }; std :: cout << \"Your shirt is \" << shirt ; // it works! return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Your shirt is blue \u6269\u5c55\u9605\u8bfb \u5982\u679c\u4f60\u597d\u5947\u7684\u8bdd\uff0c\u8fd9\u91cc\u7b80\u5355\u4ecb\u7ecd\u4e00\u4e0b\u4e0a\u9762\u4ee3\u7801\u7684\u5de5\u4f5c\u539f\u7406\u3002\u5f53\u6211\u4eec\u5c1d\u8bd5\u901a\u8fc7 std::cout \u548c operator<< \u6253\u5370 shirt \u65f6\uff0c\u7f16\u8bd1\u5668\u53d1\u73b0\u88ab\u91cd\u8f7d\u7684 operator<< \u53ef\u4ee5\u914d\u5408 Color \u7c7b\u578b\u7684\u5bf9\u8c61\u5de5\u4f5c\u3002\u4e8e\u662f\u91cd\u8f7d\u7684 operator<< \u88ab\u8c03\u7528\uff0c\u53c2\u6570 out \u662f std::cout \uff0c\u53c2\u6570 color \u5219\u662f shirt \u3002\u56e0\u4e3a out \u662f std::cout \u7684\u5f15\u7528\uff0c\u6240\u4ee5 out << \"blue\" \u5c31\u662f\u5c06 \"blue\" \u9001\u5230 std::cout \u6253\u5370\u3002 \u6211\u4eec\u4f1a\u5728 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 \u4e2d\u4ecb\u7ecdIO\u8fd0\u7b97\u7b26\u7684\u91cd\u8f7d\u3002\u76ee\u524d\u4f60\u53ef\u4ee5\u5148\u62f7\u8d1d\u8fd9\u4efd\u4ee3\u7801\u5e76\u7528\u4e8e\u4f60\u81ea\u5df1\u7684\u679a\u4e3e\u7c7b\u578b\u3002 \u679a\u4e3e\u7684\u5927\u5c0f\u548c\u57fa\u7c7b\u578b \u679a\u4e3e\u7c7b\u578b\u88ab\u8ba4\u4e3a\u662f\u6574\u6570\u7c7b\u578b\u5bb6\u65cf\u7684\u4e00\u90e8\u5206\uff0c\u7531\u7f16\u8bd1\u5668\u51b3\u5b9a\u4e3a\u679a\u4e3e\u53d8\u91cf\u5206\u914d\u591a\u5c11\u5185\u5b58\u3002C++\u6807\u51c6\u89c4\u5b9a\uff0c\u679a\u4e3e\u7684\u5927\u5c0f\u9700\u8981\u5927\u5230\u8db3\u4ee5\u8868\u793a\u6240\u6709\u679a\u4e3e\u503c\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5b83\u5c06\u4f7f\u679a\u4e3e\u53d8\u91cf\u7684\u5927\u5c0f\u4e0e\u6807\u51c6\u7684 int \u76f8\u540c\u3002 \u4f46\u662f\uff0c\u53ef\u4ee5\u6307\u5b9a\u4e0d\u540c\u7684\u57fa\u7840\u7c7b\u578b\u3002\u4f8b\u5982\uff0c \u5982\u679c\u4f60\u5728\u5f00\u53d1\u67d0\u4e2a\u5e26\u5bbd\u654f\u611f\u7684\u7a0b\u5e8f(\u4f8b\u5982\u901a\u8fc7\u7f51\u7edc\u53d1\u9001\u6570\u636e)\uff0c\u90a3\u4e48\u6b64\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u6307\u5b9a\u4e00\u4e2a\u66f4\u5c0f\u7684\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 // Use an 8-bit unsigned integer as the enum base enum Color : std :: uint8_t { black , red , blue , }; \u7531\u4e8e\u679a\u4e3e\u6570\u901a\u5e38\u4e0d\u7528\u4e8e\u7b97\u672f\u6216\u4e0e\u6574\u6570\u6bd4\u8f83\uff0c\u5982\u679c\u9700\u8981\uff0c\u4f7f\u7528\u65e0\u7b26\u53f7\u6574\u6570\u901a\u5e38\u662f\u5b89\u5168\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u4ec5\u5728\u5fc5\u8981\u65f6\u6307\u5b9a\u679a\u4e3e\u7684\u57fa\u7c7b\u578b \u6574\u578b\u8f6c\u6362\u4e3a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e \u867d\u7136\u7f16\u8bd1\u5668\u53ef\u4ee5\u9690\u5f0f\u5730\u5c06 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u4f46\u5b83\u5c06\u4e0d\u80fd\u9690\u5f0f\u5730\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u3002\u4ee5\u4e0b\u64cd\u4f5c\u5c06\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include enum Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { Pet pet { 2 }; // compile error: integer value 2 won't implicitly convert to a Pet pet = 3 ; // compile error: integer value 3 won't implicitly convert to a Pet return 0 ; } \u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002 \u9996\u5148\uff0c\u53ef\u4ee5\u4f7f\u7528 static_cast \u5f3a\u5236\u7f16\u8bd1\u5668\u5c06\u4e00\u4e2a\u6574\u6570\u8f6c\u6362\u4e3a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include enum Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { Pet pet { static_cast < Pet > ( 2 ) }; // convert integer 2 to a Pet pet = static_cast < Pet > ( 3 ); // our pig evolved into a whale! return 0 ; } \u7a0d\u540e\u6211\u4eec\u4f1a\u4e3e\u4e00\u4e2a\u4f7f\u7528\u8be5\u65b9\u6cd5\u7684\u4f8b\u5b50\u3002 \u6b64\u5916\uff0c\u5728 C++17 \u4e2d\uff0c \u5982\u679c\u4e00\u4e2a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u6709\u4e00\u4e2a\u57fa\u7c7b\u578b\uff0c\u5219\u7f16\u8bd1\u5668\u5141\u8bb8\u4f60\u4f7f\u7528\u6574\u578b\u521d\u59cb\u5316\uff08\u975e\u8d4b\u503c\uff09\u8be5\u679a\u4e3e\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include enum Pet : int // we've specified a base { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { Pet pet { 2 }; // ok: can initialize with integer pet = 3 ; // compile error: can not assign with integer return 0 ; } \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u503c\u7684\u8f93\u5165 \u7531\u4e8e Pet \u662f\u4e00\u4e2a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\uff0c\u6240\u4ee5C++\u5e76\u4e0d\u77e5\u9053\u5982\u4f55\u4ece std::cin \u8f93\u5165\u4e00\u4e2a Pet \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include enum Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { Pet pet { pig }; std :: cin >> pet ; // compile error, std::cin doesn't know how to input a Pet return 0 ; } \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u8bfb\u5165\u4e00\u4e2a\u6574\u578b\u6570\uff0c\u7136\u540e\u4f7f\u7528 static_cast \u5c06\u5176 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u4e3a\u5408\u9002\u7684\u679a\u4e3e\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include enum Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { std :: cout << \"Enter a pet (0=cat, 1=dog, 2=pig, 3=whale): \" ; int input {}; std :: cin >> input ; // input an integer Pet pet { static_cast < Pet > ( input ) }; // static_cast our integer to a Pet return 0 ; } \u6269\u5c55\u9605\u8bfb \u7c7b\u4f3c\u4e8e\u91cd\u8f7d operator<< \u4f7f\u5176\u80fd\u591f\u8f93\u51fa\u679a\u4e3e\u7c7b\u578b\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u91cd\u8f7d operator>> \u4f7f\u5176\u6307\u5b9a\u5982\u4f55\u8f93\u5165\u4e00\u4e2a\u679a\u4e3e\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include enum Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; // Consider this magic for now // We pass pet by reference so we can have the function modify its value std :: istream & operator >> ( std :: istream & in , Pet & pet ) { int input {}; in >> input ; // input an integer pet = static_cast < Pet > ( input ); return in ; } int main () { std :: cout << \"Enter a pet (0=cat, 1=dog, 2=pig, 3=whale): \" ; Pet pet {}; std :: cin >> pet ; // input our pet using std::cin std :: cout << pet << '\\n' ; // prove that it worked return 0 ; } \u540c\u6837\u5730\uff0c\u4f60\u53ef\u4ee5\u5148\u4f7f\u7528\u8fd9\u6bb5\u795e\u5947\u7684\u4ee3\u7801(\u56e0\u4e3a\u6211\u4eec\u8fd8\u6ca1\u6709\u89e3\u91ca\u5b83\u80cc\u540e\u7684\u6982\u5ff5)\uff0c\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5b83\u7684\u539f\u7406\u3002","title":"10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa"},{"location":"10-3-unscoped-enumeration-input-and-output/#_1","text":"\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include enum Color { black , // assigned 0 red , // assigned 1 blue , // assigned 2 green , // assigned 3 white , // assigned 4 cyan , // assigned 5 yellow , // assigned 6 magenta , // assigned 7 }; int main () { Color shirt { blue }; std :: cout << \"Your shirt is \" << shirt ; // what does this do? return 0 ; } \u56e0\u4e3a\u679a\u4e3e\u7c7b\u578b\u4fdd\u5b58\u7684\u662f\u6574\u578b\u503c\uff0c\u6240\u4ee5\u5982\u4f60\u6240\u671f\u5f85\u7684\u90a3\u6837\uff0c\u4e0a\u9762\u7684\u7a0b\u5e8f\u6253\u5370\uff1a 1 Your shirt is 2 \u5f53\u679a\u4e3e\u7c7b\u578b\u7528\u4e8e\u51fd\u6570\u8c03\u7528\u6216\u4e0e\u8fd0\u7b97\u7b26\u4e00\u8d77\u4f7f\u7528\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u9996\u5148\u5c1d\u8bd5\u67e5\u627e\u4e0e\u679a\u4e3e\u7c7b\u578b\u5339\u914d\u7684\u51fd\u6570\u6216\u8fd0\u7b97\u7b26\u3002\u4f8b\u5982\uff0c\u5f53\u7f16\u8bd1\u5668\u8bd5\u56fe\u7f16\u8bd1 std::cout << shirt \u65f6\uff0c\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u67e5\u770b operator<< \u662f\u5426\u77e5\u9053\u5982\u4f55\u5c06 Color \u7c7b\u578b\u7684\u5bf9\u8c61(\u56e0\u4e3a shirt \u7684\u7c7b\u578b\u662f Color )\u6253\u5370\u5230 std::cout \u3002\u5b83\u4e0d\u77e5\u9053\u3002 \u5982\u679c\u7f16\u8bd1\u5668\u65e0\u6cd5\u627e\u5230\u5339\u914d\u7684\u8fd0\u7b97\u7b26\uff0c\u5219\u5b83\u4f1a\u5c06 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u6574\u578b\u3002\u56e0\u4e3a std::cout \u77e5\u9053\u5982\u4f55\u6253\u5370\u4e00\u4e2a\u6574\u578b\uff0c\u6240\u4ee5 shirt \u4f1a\u88ab\u8f6c\u6362\u4e3a\u6574\u578b\u5e76\u6253\u5370\u51fa2\u3002","title":"\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u4f1a\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u6574\u578b\u503c"},{"location":"10-3-unscoped-enumeration-input-and-output/#_2","text":"\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5c06\u679a\u4e3e\u8f6c\u6362\u4e3a\u6574\u578b\u6253\u5370\u51fa\u6765\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c\u3002\u6211\u4eec\u80af\u5b9a\u662f\u5e0c\u671b\u80fd\u591f\u5c06\u679a\u4e3e\u503c\u7684\u672c\u540d\u6253\u5370\u51fa\u6765 (\u4f8b\u5982 blue )\u3002\u4f46\u662f\uff0c\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e00\u70b9\u6211\u4eec\u9700\u8981\u4e00\u4e9b\u65b9\u6cd5\u5c06\u679a\u4e3e\u5728\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u6253\u5370\u51fa\u6765( \"blue\" )\u3002 \u622a\u81f3C++ 20, C++\u8fd8\u6ca1\u6709\u63d0\u4f9b\u4efb\u4f55\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u81ea\u5df1\u5bfb\u627e\u89e3\u51b3\u65b9\u6848\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u8fd9\u5e76\u4e0d\u96be\u3002\u5b9e\u73b0\u8fd9\u4e00\u70b9\u7684\u5178\u578b\u65b9\u6cd5\u662f\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u679a\u4e3e\u7c7b\u578b\u4f5c\u4e3a\u53c2\u6570\uff0c\u7136\u540e\u8f93\u51fa\u76f8\u5e94\u7684\u5b57\u7b26\u4e32(\u6216\u5c06\u5b57\u7b26\u4e32\u8fd4\u56de\u7ed9\u8c03\u7528\u8005)\u3002 \u5178\u578b\u7684\u65b9\u6cd5\u662f\u5224\u65ad\u5f53\u524d\u9700\u8981\u6253\u5370\u7684\u662f\u54ea\u4e2a\u7684\u679a\u4e3e\u503c\uff1a 1 2 3 4 5 6 7 8 // Using if-else for this is inefficient void printColor ( Color color ) { if ( color == black ) std :: cout << \"black\" ; else if ( color == red ) std :: cout << \"red\" ; else if ( color == blue ) std :: cout << \"blue\" ; else std :: cout << \"???\" ; } \u4f46\u662f\uff0c\u4e3a\u6b64\u4f7f\u7528\u4e00\u7cfb\u5217if-else\u8bed\u53e5\u662f\u4f4e\u6548\u7684\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u591a\u6b21\u6bd4\u8f83\u624d\u80fd\u627e\u5230\u5339\u914d\u3002\u5b8c\u6210\u540c\u6837\u4efb\u52a1\u7684\u4e00\u79cd\u66f4\u6709\u6548\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 switch \u8bed\u53e5\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8fd8\u5c06\u4ee5 std::string \u7684\u5f62\u5f0f\u8fd4\u56de Color \uff0c\u4ee5\u7ed9\u8c03\u7528\u8005\u66f4\u591a\u7684\u7075\u6d3b\u6027\u6765\u5bf9\u540d\u79f0\u505a\u4efb\u4f55\u60f3\u505a\u7684\u4e8b\u60c5(\u5305\u62ec\u6253\u5370\u5b83): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include #include enum Color { black , red , blue , }; // We'll show a better version of this for C++17 below std :: string getColor ( Color color ) { switch ( color ) { case black : return \"black\" ; case red : return \"red\" ; case blue : return \"blue\" ; default : return \"???\" ; } } int main () { Color shirt { blue }; std :: cout << \"Your shirt is \" << getColor ( shirt ) << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Your shirt is blue \u8fd9\u4e48\u505a\u6bd4\u4f7f\u7528\u591a\u4e2aif-else\u5224\u65ad\u66f4\u9ad8\u6548\uff08\u56e0\u4e3aswitch\u8bed\u53e5\u6bd4if\u66f4\u9ad8\u6548\uff09\uff0c\u800c\u4e14\u53ef\u8bfb\u6027\u4e5f\u66f4\u597d\u3002\u4f46\u662f\uff0c\u8fd9\u4e2a\u7248\u672c\u7684\u4ee3\u7801\u4e5f\u4e0d\u662f\u5f88\u9ad8\u6548\uff0c\u56e0\u4e3a\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u90fd\u5fc5\u987b\u521b\u5efa\u5e76\u8fd4\u56de\u4e00\u4e2a std::string (\u5f00\u9500\u5f88\u5927)\u3002 \u5728 C++17 \u4e2d\uff0c\u66f4\u9ad8\u6548\u7684\u505a\u6cd5\u662f\u5c06 std::string \u66ff\u6362\u4e3a std::string_view \u3002 std::string_view \u4f1a\u4ee5\u67d0\u79cd\u62f7\u8d1d\u5f00\u9500\u66f4\u5c0f\u7684\u65b9\u5f0f\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include #include // C++17 enum Color { black , red , blue , }; constexpr std :: string_view getColor ( Color color ) // C++17 { switch ( color ) { case black : return \"black\" ; case red : return \"red\" ; case blue : return \"blue\" ; default : return \"???\" ; } } int main () { Color shirt { blue }; std :: cout << \"Your shirt is \" << getColor ( shirt ) << '\\n' ; return 0 ; } COPY \u76f8\u5173\u5185\u5bb9 Constexpr \u8fd4\u56de\u7c7b\u578b\u5728 6.14 - Constexpr \u548c consteval \u51fd\u6570 \u4e2d\u4ecb\u7ecd\u3002","title":"\u6253\u5370\u679a\u4e3e\u503c\u7684\u540d\u5b57"},{"location":"10-3-unscoped-enumeration-input-and-output/#operator","text":"\u5c3d\u7ba1\u4e0a\u9762\u7684\u4f8b\u5b50\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\uff0c\u4f46\u662f\u6211\u4eec\u5fc5\u987b\u8981\u80fd\u591f\u8bb0\u4f4f\u54ea\u4e2a\u51fd\u6570\u662f\u7528\u6765\u83b7\u53d6\u679a\u4e3e\u503c\u540d\u5b57\u7684\u3002\u5c3d\u7ba1\u591a\u6570\u60c5\u51b5\u4e0b\u8fd9\u4e5f\u5e76\u4e0d\u9ebb\u70e6\uff0c\u4f46\u662f\u5982\u679c\u679a\u4e3e\u7c7b\u578b\u5f88\u591a\u7684\u65f6\u5019\uff0c\u8fd8\u662f\u5f88\u53ef\u80fd\u4f1a\u5e26\u6765\u95ee\u9898\u3002\u4f7f\u7528\u8fd0\u7b97\u7b26\u91cd\u8f7d\uff0c\u6211\u4eec\u53ef\u4ee5\u6559\u4f1a\u8fd0\u7b97\u7b26 operator<< \u5982\u4f55\u6253\u5370\u7a0b\u5e8f\u5b9a\u4e49\u679a\u4e3e\u7c7b\u578b\uff01\u7531\u4e8e\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u5148\u5c06\u4e0b\u9762\u7684\u4ee3\u7801\u5f53\u505a\u4e00\u79cd\u9b54\u6cd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include enum Color { black , red , blue , }; // Teach operator<< how to print a Color // Consider this magic for now since we haven't explained any of the concepts it uses yet // std::ostream is the type of std::cout // The return type and parameter type are references (to prevent copies from being made)! std :: ostream & operator << ( std :: ostream & out , Color color ) { switch ( color ) { case black : out << \"black\" ; break ; case red : out << \"red\" ; break ; case blue : out << \"blue\" ; break ; default : out << \"???\" ; break ; } return out ; } int main () { Color shirt { blue }; std :: cout << \"Your shirt is \" << shirt ; // it works! return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Your shirt is blue \u6269\u5c55\u9605\u8bfb \u5982\u679c\u4f60\u597d\u5947\u7684\u8bdd\uff0c\u8fd9\u91cc\u7b80\u5355\u4ecb\u7ecd\u4e00\u4e0b\u4e0a\u9762\u4ee3\u7801\u7684\u5de5\u4f5c\u539f\u7406\u3002\u5f53\u6211\u4eec\u5c1d\u8bd5\u901a\u8fc7 std::cout \u548c operator<< \u6253\u5370 shirt \u65f6\uff0c\u7f16\u8bd1\u5668\u53d1\u73b0\u88ab\u91cd\u8f7d\u7684 operator<< \u53ef\u4ee5\u914d\u5408 Color \u7c7b\u578b\u7684\u5bf9\u8c61\u5de5\u4f5c\u3002\u4e8e\u662f\u91cd\u8f7d\u7684 operator<< \u88ab\u8c03\u7528\uff0c\u53c2\u6570 out \u662f std::cout \uff0c\u53c2\u6570 color \u5219\u662f shirt \u3002\u56e0\u4e3a out \u662f std::cout \u7684\u5f15\u7528\uff0c\u6240\u4ee5 out << \"blue\" \u5c31\u662f\u5c06 \"blue\" \u9001\u5230 std::cout \u6253\u5370\u3002 \u6211\u4eec\u4f1a\u5728 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 \u4e2d\u4ecb\u7ecdIO\u8fd0\u7b97\u7b26\u7684\u91cd\u8f7d\u3002\u76ee\u524d\u4f60\u53ef\u4ee5\u5148\u62f7\u8d1d\u8fd9\u4efd\u4ee3\u7801\u5e76\u7528\u4e8e\u4f60\u81ea\u5df1\u7684\u679a\u4e3e\u7c7b\u578b\u3002","title":"\u8ba9\u00a0operator<<\u00a0\u77e5\u9053\u5982\u4f55\u6253\u5370\u679a\u4e3e\u503c"},{"location":"10-3-unscoped-enumeration-input-and-output/#_3","text":"\u679a\u4e3e\u7c7b\u578b\u88ab\u8ba4\u4e3a\u662f\u6574\u6570\u7c7b\u578b\u5bb6\u65cf\u7684\u4e00\u90e8\u5206\uff0c\u7531\u7f16\u8bd1\u5668\u51b3\u5b9a\u4e3a\u679a\u4e3e\u53d8\u91cf\u5206\u914d\u591a\u5c11\u5185\u5b58\u3002C++\u6807\u51c6\u89c4\u5b9a\uff0c\u679a\u4e3e\u7684\u5927\u5c0f\u9700\u8981\u5927\u5230\u8db3\u4ee5\u8868\u793a\u6240\u6709\u679a\u4e3e\u503c\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5b83\u5c06\u4f7f\u679a\u4e3e\u53d8\u91cf\u7684\u5927\u5c0f\u4e0e\u6807\u51c6\u7684 int \u76f8\u540c\u3002 \u4f46\u662f\uff0c\u53ef\u4ee5\u6307\u5b9a\u4e0d\u540c\u7684\u57fa\u7840\u7c7b\u578b\u3002\u4f8b\u5982\uff0c \u5982\u679c\u4f60\u5728\u5f00\u53d1\u67d0\u4e2a\u5e26\u5bbd\u654f\u611f\u7684\u7a0b\u5e8f(\u4f8b\u5982\u901a\u8fc7\u7f51\u7edc\u53d1\u9001\u6570\u636e)\uff0c\u90a3\u4e48\u6b64\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u6307\u5b9a\u4e00\u4e2a\u66f4\u5c0f\u7684\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 // Use an 8-bit unsigned integer as the enum base enum Color : std :: uint8_t { black , red , blue , }; \u7531\u4e8e\u679a\u4e3e\u6570\u901a\u5e38\u4e0d\u7528\u4e8e\u7b97\u672f\u6216\u4e0e\u6574\u6570\u6bd4\u8f83\uff0c\u5982\u679c\u9700\u8981\uff0c\u4f7f\u7528\u65e0\u7b26\u53f7\u6574\u6570\u901a\u5e38\u662f\u5b89\u5168\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u4ec5\u5728\u5fc5\u8981\u65f6\u6307\u5b9a\u679a\u4e3e\u7684\u57fa\u7c7b\u578b","title":"\u679a\u4e3e\u7684\u5927\u5c0f\u548c\u57fa\u7c7b\u578b"},{"location":"10-3-unscoped-enumeration-input-and-output/#_4","text":"\u867d\u7136\u7f16\u8bd1\u5668\u53ef\u4ee5\u9690\u5f0f\u5730\u5c06 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u4f46\u5b83\u5c06\u4e0d\u80fd\u9690\u5f0f\u5730\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u3002\u4ee5\u4e0b\u64cd\u4f5c\u5c06\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include enum Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { Pet pet { 2 }; // compile error: integer value 2 won't implicitly convert to a Pet pet = 3 ; // compile error: integer value 3 won't implicitly convert to a Pet return 0 ; } \u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002 \u9996\u5148\uff0c\u53ef\u4ee5\u4f7f\u7528 static_cast \u5f3a\u5236\u7f16\u8bd1\u5668\u5c06\u4e00\u4e2a\u6574\u6570\u8f6c\u6362\u4e3a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include enum Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { Pet pet { static_cast < Pet > ( 2 ) }; // convert integer 2 to a Pet pet = static_cast < Pet > ( 3 ); // our pig evolved into a whale! return 0 ; } \u7a0d\u540e\u6211\u4eec\u4f1a\u4e3e\u4e00\u4e2a\u4f7f\u7528\u8be5\u65b9\u6cd5\u7684\u4f8b\u5b50\u3002 \u6b64\u5916\uff0c\u5728 C++17 \u4e2d\uff0c \u5982\u679c\u4e00\u4e2a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u6709\u4e00\u4e2a\u57fa\u7c7b\u578b\uff0c\u5219\u7f16\u8bd1\u5668\u5141\u8bb8\u4f60\u4f7f\u7528\u6574\u578b\u521d\u59cb\u5316\uff08\u975e\u8d4b\u503c\uff09\u8be5\u679a\u4e3e\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include enum Pet : int // we've specified a base { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { Pet pet { 2 }; // ok: can initialize with integer pet = 3 ; // compile error: can not assign with integer return 0 ; }","title":"\u6574\u578b\u8f6c\u6362\u4e3a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e"},{"location":"10-3-unscoped-enumeration-input-and-output/#_5","text":"\u7531\u4e8e Pet \u662f\u4e00\u4e2a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\uff0c\u6240\u4ee5C++\u5e76\u4e0d\u77e5\u9053\u5982\u4f55\u4ece std::cin \u8f93\u5165\u4e00\u4e2a Pet \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include enum Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { Pet pet { pig }; std :: cin >> pet ; // compile error, std::cin doesn't know how to input a Pet return 0 ; } \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u8bfb\u5165\u4e00\u4e2a\u6574\u578b\u6570\uff0c\u7136\u540e\u4f7f\u7528 static_cast \u5c06\u5176 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u4e3a\u5408\u9002\u7684\u679a\u4e3e\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include enum Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { std :: cout << \"Enter a pet (0=cat, 1=dog, 2=pig, 3=whale): \" ; int input {}; std :: cin >> input ; // input an integer Pet pet { static_cast < Pet > ( input ) }; // static_cast our integer to a Pet return 0 ; } \u6269\u5c55\u9605\u8bfb \u7c7b\u4f3c\u4e8e\u91cd\u8f7d operator<< \u4f7f\u5176\u80fd\u591f\u8f93\u51fa\u679a\u4e3e\u7c7b\u578b\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u91cd\u8f7d operator>> \u4f7f\u5176\u6307\u5b9a\u5982\u4f55\u8f93\u5165\u4e00\u4e2a\u679a\u4e3e\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include enum Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; // Consider this magic for now // We pass pet by reference so we can have the function modify its value std :: istream & operator >> ( std :: istream & in , Pet & pet ) { int input {}; in >> input ; // input an integer pet = static_cast < Pet > ( input ); return in ; } int main () { std :: cout << \"Enter a pet (0=cat, 1=dog, 2=pig, 3=whale): \" ; Pet pet {}; std :: cin >> pet ; // input our pet using std::cin std :: cout << pet << '\\n' ; // prove that it worked return 0 ; } \u540c\u6837\u5730\uff0c\u4f60\u53ef\u4ee5\u5148\u4f7f\u7528\u8fd9\u6bb5\u795e\u5947\u7684\u4ee3\u7801(\u56e0\u4e3a\u6211\u4eec\u8fd8\u6ca1\u6709\u89e3\u91ca\u5b83\u80cc\u540e\u7684\u6982\u5ff5)\uff0c\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5b83\u7684\u539f\u7406\u3002","title":"\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u503c\u7684\u8f93\u5165"},{"location":"10-4-scoped-enumerations-enum-classes/","tags":["enum","scoped enumerations","C++20","C++17"],"text":"Key Takeaway \u5c3d\u7ba1 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u5728C++\u4e2d\u662f\u53ef\u533a\u5206\u7c7b\u578b\uff0c\u4f46\u5b83\u4eec\u5e76\u4e0d\u662f \u7c7b\u578b\u5b89\u5168 \u7684\uff0c\u800c\u4e14\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u5b83\u5141\u8bb8\u4f60\u505a\u51fa\u4e00\u4e9b\u4e0d\u5408\u7406\u7684\u64cd\u4f5c\u3002\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include int main () { enum Color { red , blue , }; enum Fruit { banana , apple , }; Color color { red }; Fruit fruit { banana }; if ( color == fruit ) // \u7f16\u8bd1\u5668\u4f1a\u5c06 color \u548c fruit \u5f53\u505a\u6574\u578b\u8fdb\u884c\u6bd4\u8f83 std :: cout << \"color and fruit are equal \\n \" ; // \u5e76\u8ba4\u4e3a\u5b83\u4eec\u76f8\u7b49 else std :: cout << \"color and fruit are not equal \\n \" ; return 0 ; } \u6253\u5370\uff1a 1 color and fruit are equal \u5f53 color \u548c fruit \u8fdb\u884c\u6bd4\u8f83\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u9996\u5148\u5224\u65ad\u5b83\u662f\u5426\u77e5\u9053\u5982\u4f55\u6bd4\u8f83 Color \u548c Fruit \u3002\u5b83\u5e76\u4e0d\u77e5\u9053\u3002\u7136\u540e\uff0c\u5b83\u5c31\u4f1a\u5c1d\u8bd5\u5c06 Color \u6216 Fruit \u8f6c\u6362\u4e3a\u6574\u578b\u5e76\u7ee7\u7eed\u67e5\u770b\u662f\u6709\u5339\u914d\u7684\u6bd4\u8f83\u65b9\u6cd5\u3002\u6700\u7ec8\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c06\u4e8c\u8005\u90fd\u8f6c\u6362\u4e3a\u6574\u578b\u5e76\u8fdb\u884c\u6bd4\u8f83\u3002\u56e0\u4e3a color \u548c fruit \u7684\u679a\u4e3e\u503c\u90fd\u8f6c\u6362\u4e3a0\uff0c\u6240\u4ee5\u5b83\u4eec\u662f\u76f8\u7b49\u7684\u3002 \u8fd9\u663e\u7136\u4e0d\u5408\u7406\uff0c\u56e0\u4e3a color \u548c fruit \u6765\u81ea\u4e24\u4e2a\u4e0d\u540c\u7684\u679a\u4e3e\u7c7b\u578b\uff0c\u5b83\u4eec\u672c\u6765\u662f\u4e0d\u5177\u6709\u53ef\u6bd4\u6027\u7684\u3002\u5bf9\u4e8e\u4e00\u822c\u7684\u679a\u4e3e\u6765\u8bf4\uff0c\u6211\u4eec\u5e76\u6ca1\u6709\u7b80\u5355\u7684\u529e\u6cd5\u653e\u7f6e\u6b64\u7c7b\u4e8b\u60c5\u53d1\u751f\u3002 \u56e0\u4e3a\u4e0a\u8ff0\u95ee\u9898\u7684\u5b58\u5728\uff0c\u4ee5\u53ca \u547d\u540d\u7a7a\u95f4 \u6c61\u67d3\u95ee\u9898\uff0cC++\u8bbe\u8ba1\u8005\u51b3\u5b9a\u63d0\u4f9b\u4e00\u4e2a\u5e72\u51c0\u5229\u843d\u5730\u89e3\u51b3\u65b9\u6848\u3002 \u6709\u4f5c\u7528\u57df\u679a\u4e3e \u8fd9\u4e2a\u89e3\u51b3\u65b9\u6848\u5c31\u662f \u6709\u4f5c\u7528\u57df\u679a\u4e3e (\u5728C++\u4e2d\u901a\u5e38\u79f0\u4e3a \u679a\u4e3e\u7c7b \uff0c\u81f3\u4e8e\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u53eb\u5b83\uff0c\u5f88\u5feb\u5c31\u4f1a\u4e0d\u8a00\u81ea\u660e\u4e86)\u3002 \u6709\u4f5c\u7528\u57df\u679a\u4e3e\u548c\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\uff08 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \uff09\u7528\u6cd5\u7c7b\u4f3c\uff0c\u4e0d\u8fc7\u5b83\u4eec\u6709\u4e24\u4e2a\u4e3b\u8981\u7684\u533a\u522b\uff1a\u5176\u4e00\uff0c\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u662f\u5f3a\u7c7b\u578b\u7684(\u5b83\u4e0d\u4f1a\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u6574\u578b) \uff0c\u5176\u4e8c\u5b83\u5177\u6709\u5f3a\u4f5c\u7528\u57df\u7ea6\u675f (\u5b83\u7684\u679a\u4e3e\u503c \u53ea\u4f1a \u88ab\u653e\u7f6e\u5728\u679a\u4e3e\u7c7b\u7684\u4f5c\u7528\u57df\u4e2d)\u3002 \u521b\u5efa\u6709\u4f5c\u7528\u57df\u679a\u4e3e\uff0c\u9700\u8981\u4f7f\u7528\u5173\u952e\u5b57 enum class \u3002\u5b9a\u4e49\u7684\u5176\u4ed6\u90e8\u5206\u5219\u548c\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u4e00\u6837\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include int main () { enum class Color // \"enum class\" \u5b9a\u4e49\u4e86\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b { red , // red \u662f Color \u4f5c\u7528\u57df\u7684\u6210\u5458 blue , }; enum class Fruit { banana , // banana \u662f Fruit \u4f5c\u7528\u57df\u7684\u6210\u5458 apple , }; Color color { Color :: red }; // \u6ce8\u610f: red \u4e0d\u80fd\u591f\u88ab\u76f4\u63a5\u8bbf\u95ee\uff0c\u5fc5\u987b\u4f7f\u7528 Color::red Fruit fruit { Fruit :: banana }; // note: banana \u4e0d\u80fd\u591f\u88ab\u76f4\u63a5\u8bbf\u95ee, \u5fc5\u987b\u4f7f\u7528 Fruit::banana if ( color == fruit ) // \u7f16\u8bd1\u9519\u8bef\uff1a\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053\u5982\u4f55\u6bd4\u8f83 Color \u548c Fruit std :: cout << \"color and fruit are equal \\n \" ; else std :: cout << \"color and fruit are not equal \\n \" ; return 0 ; } \u7f16\u8bd1\u5668\u4f1a\u62a5\u544a\u7a0b\u5e8f\u7684\u7b2c19\u884c\u5b58\u5728\u9519\u8bef\uff0c\u56e0\u4e3a\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u4e0d\u80fd\u591f\u88ab\u8f6c\u6362\u4e3a\u4efb\u4f55\u53ef\u6bd4\u8f83\u7684\u5176\u4ed6\u7c7b\u578b\u3002 \u9898\u5916\u8bdd class \u5173\u952e\u5b57 (\u4ee5\u53ca static \u5173\u952e\u5b57)\u662f C++ \u4e2d\u6700\u88ab\u201c\u6ee5\u7528\u201d\u7684\u5173\u952e\u5b57\uff0c\u5b83\u5728\u4e0d\u540c\u8bed\u5883\u4e0b\u5177\u6709\u4e0d\u540c\u7684\u542b\u4e49\u3002\u5c3d\u7ba1\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u4f7f\u7528\u4e86 class \u5173\u952e\u5b57\uff0c\u4f46\u662f\u5b83\u4eec\u5e76\u4e0d\u662f\u7c7b\u7c7b\u578b\uff08\u7c7b\u7c7b\u578b\u5305\u62ec\u7ed3\u6784\u4f53\u3001\u7c7b\u548c\u8054\u5408\u4f53\uff09\u3002 \u6709\u4f5c\u7528\u57df\u679a\u4e3e\u4f1a\u5b9a\u4e49\u81ea\u5df1\u7684\u4f5c\u7528\u57df \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u4f1a\u5c06\u5176\u679a\u4e3e\u503c\u548c\u81ea\u5df1\u653e\u7f6e\u5728\u76f8\u540c\u7684\u4f5c\u7528\u57df\u4e2d\uff0c\u800c\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u4f1a\u5c06\u5b83\u7684\u679a\u4e3e\u503c\u653e\u7f6e\u5728\u81ea\u5df1\u5b9a\u4e49\u7684\u4f5c\u7528\u57df\u4e2d\u3002\u6362\u8a00\u4e4b\uff0c\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u540c\u65f6\u662f\u5176\u679a\u4e3e\u503c\u7684\u547d\u540d\u7a7a\u95f4\u3002\u8fd9\u79cd\u81ea\u5e26\u7684\u4f5c\u7528\u57df\u6709\u52a9\u4e8e\u907f\u514d\u547d\u540d\u7a7a\u95f4\u6c61\u67d3\u548c\u6f5c\u5728\u7684\u547d\u540d\u51b2\u7a81\u3002 \u8981\u8bbf\u95ee\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u503c\uff0c\u5c31\u50cf\u5b83\u4f4d\u4e8e\u4e0e\u4f5c\u7528\u57df\u679a\u4e3e\u540c\u540d\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\u4e00\u6837: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { enum class Color // \"enum class\" defines this as a scoped enum rather than an unscoped enum { red , // red is considered part of Color's scope region blue , }; std :: cout << red << '\\n' ; // compile error: red not defined in this scope region std :: cout << Color :: red << '\\n' ; // compile error: std::cout doesn't know how to print this (will not implicitly convert to int) Color color { Color :: blue }; // okay return 0 ; } \u56e0\u4e3a\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u4e3a\u679a\u4e3e\u503c\u63d0\u4f9b\u4e86\u5b83\u4eec\u81ea\u5df1\u7684\u9690\u5f0f\u540d\u79f0\u7a7a\u95f4\uff0c\u6240\u4ee5\u6ca1\u6709\u5fc5\u8981\u5c06\u4f5c\u7528\u57df\u679a\u4e3e\u653e\u5728\u53e6\u4e00\u4e2a\u4f5c\u7528\u57df(\u6bd4\u5982\u540d\u79f0\u7a7a\u95f4)\u4e2d\uff0c\u9664\u975e\u6709\u5176\u4ed6\u4ee4\u4eba\u4fe1\u670d\u7684\u7406\u7531\u8fd9\u6837\u505a\uff0c\u56e0\u4e3a\u8fd9\u6837\u505a\u662f\u591a\u4f59\u7684\u3002 \u6709\u4f5c\u7528\u57df\u679a\u4e3e\u4e0d\u4f1a\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u6574\u578b \u548c\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u4e0d\u540c\uff0c\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u4e0d\u4f1a\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u6574\u578b\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u662f\u6709\u76ca\u5730\uff0c\u56e0\u4e3a\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u8fd9\u6837\u7684\u8f6c\u6362\u90fd\u4e0d\u5408\u7406\uff0c\u6240\u4ee5\u8fd9\u6709\u52a9\u4e8e\u5e2e\u6211\u4eec\u907f\u514d\u8bed\u4e49\u9519\u8bef\uff0c\u4f8b\u5982\u6bd4\u8f83\u4e24\u4e2a\u4e0d\u540c\u679a\u4e3e\u7c7b\u578b\u4e2d\u7684\u679a\u4e3e\u503c\u6216\u8005\u4f7f\u7528\u679a\u4e3e\u503c\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\uff08 red + 5 \uff09\u3002 \u6ce8\u610f\uff0c\u4f60\u53ef\u4ee5\u6bd4\u8f83\u6765\u81ea\u76f8\u540c\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u4e2d\u7684\u679a\u4e3e\u503c(\u56e0\u4e3a\u5b83\u4eec\u5177\u6709\u76f8\u540c\u7684\u7c7b\u578b)\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { enum class Color { red , blue , }; Color shirt { Color :: red }; if ( shirt == Color :: red ) // Color \u548c Color \u7684\u6bd4\u8f83\u662f\u53ef\u4ee5\u7684 std :: cout << \"The shirt is red! \\n \" ; else if ( shirt == Color :: blue ) std :: cout << \"The shirt is blue! \\n \" ; return 0 ; } \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5c06\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u503c\u4f5c\u4e3a\u6574\u6570\u5904\u7406\u4e5f\u662f\u6709\u7528\u7684\u3002\u5728\u8fd9\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 static_cast \u663e\u5f0f\u5730\u5c06\u679a\u4e3e\u503c\u8f6c\u6362\u4e3a\u6574\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { enum class Color { red , blue , }; Color color { Color :: blue }; std :: cout << color << '\\n' ; // won't work, because there's no implicit conversion to int std :: cout << static_cast < int > ( color ) << '\\n' ; // will print 1 return 0 ; } \u53cd\u8fc7\u6765\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06\u4e00\u4e2a\u6574\u578b\u8f6c\u6362\u4e3a\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u503c\uff0c\u8fd9\u5728\u5904\u7406\u8f93\u5165\u65f6\u5c24\u5176\u6709\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include enum class Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { std :: cout << \"Enter a pet (0=cat, 1=dog, 2=pig, 3=whale): \" ; int input {}; std :: cin >> input ; // input an integer Pet pet { static_cast < Pet > ( input ) }; // static_cast our integer to a Pet return 0 ; } \u5728 C++17 \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u6574\u578b\u6570\uff08\u65e0\u9700\u7c7b\u578b\u8f6c\u6362\uff09\u76f4\u63a5\u521d\u59cb\u5316\u6709\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u800c\u4e14\u4e0d\u50cf\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\uff0c\u4f60\u4e0d\u9700\u8981\u6307\u660e\u4e00\u4e2a\u57fa\u7c7b\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u63a8\u8350\u4f7f\u7528\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u800c\u975e\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\uff0c\u9664\u975e\u4f60\u6709\u8db3\u591f\u7684\u7406\u7531\u4e0d\u8fd9\u4e48\u505a\u3002 \u5c3d\u7ba1\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u63d0\u4f9b\u4e86\u5f88\u591a\u597d\u7684\u529f\u80fd\uff0c\u4f46\u662f\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u5728C++\u4e2d\u4ecd\u7136\u975e\u5e38\u5e38\u89c1\uff0c\u56e0\u4e3a\u5728\u6709\u4e9b\u573a\u5408\u4e0b\u6211\u4eec\u5e0c\u671b\u679a\u4e3e\u503c\u80fd\u591f\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u6574\u578b\uff08\u603b\u662f\u4f7f\u7528 static_cast \u8fdb\u884c\u8f6c\u6362\u6709\u4e9b\u9ebb\u70e6\uff09\uff0c\u800c\u4e14\u6709\u65f6\u6211\u4eec\u4e5f\u4e0d\u9700\u8981\u989d\u5916\u7684\u547d\u540d\u7a7a\u95f4\u5206\u9694\u3002 \u7b80\u5316\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u503c\u5230\u6574\u578b\u7684\u8f6c\u6362(\u8fdb\u9636\u8bdd\u9898) \u6709\u4f5c\u7528\u57df\u679a\u4e3e\u5f88\u597d\uff0c\u4f46\u5b83\u4e0d\u80fd\u591f\u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u8fd9\u4e00\u7279\u70b9\u6709\u65f6\u53ef\u80fd\u4f1a\u662f\u4e00\u4e2a\u75db\u70b9\u3002\u5982\u679c\u9700\u8981\u7ecf\u5e38\u5c06\u4f5c\u7528\u57df\u679a\u4e3e\u8f6c\u6362\u4e3a\u6574\u6570(\u4f8b\u5982\uff0c\u5728\u5e0c\u671b\u4f7f\u7528\u4f5c\u7528\u57df\u679a\u4e3e\u4f5c\u4e3a\u6570\u7ec4\u4e0b\u6807\u7684\u60c5\u51b5\u4e0b)\uff0c\u90a3\u4e48\u6bcf\u6b21\u9700\u8981\u8f6c\u6362\u65f6\u90fd\u5fc5\u987b\u4f7f\u7528 static_cast \uff0c\u8fd9\u65e0\u7591\u4f1a\u4f7f\u4ee3\u7801\u770b\u8d77\u6765\u975e\u5e38\u6df7\u4e71 \u5982\u679c\u4f60\u6709\u8fd9\u6837\u7684\u9700\u6c42\uff0c\u5e0c\u671b\u4f7f\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u503c\u80fd\u591f\u66f4\u5bb9\u6613\u5730\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u90a3\u4e48\u53ef\u4ee5\u5c1d\u8bd5\u91cd\u8f7d\u4e00\u5143 operator+ \u6765\u6267\u884c\u8fd9\u79cd\u8f6c\u6362\u3002\u6211\u4eec\u8fd8\u6ca1\u6709\u89e3\u91ca\u8fd9\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u6240\u4ee5\u73b0\u5728\u5c31\u628a\u5b83\u5f53\u6210\u9b54\u672f\u5427\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include enum class Animals { chicken , // 0 dog , // 1 cat , // 2 elephant , // 3 duck , // 4 snake , // 5 maxAnimals , }; // Overload the unary + operator to convert Animals to the underlying type // adapted from https://stackoverflow.com/a/42198760, thanks to Pixelchemist for the idea constexpr auto operator + ( Animals a ) noexcept { return static_cast < std :: underlying_type_t < Animals >> ( a ); } int main () { std :: cout << + Animals :: elephant << '\\n' ; // convert Animals::elephant to an integer using unary operator+ return 0 ; } \u6253\u5370\uff1a 1 3 \u6b64\u65b9\u6cd5\u53ef\u907f\u514d\u4e0d\u7ecf\u610f\u5730\u9690\u5f0f\u8f6c\u6362\uff0c\u540c\u65f6\u8fd8\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u5730\u663e\u793a\u8f6c\u6362\u65b9\u6cd5\u3002 using enum \u8bed\u53e5 \uff08C++20\uff09 C++20 \u5f15\u5165\u4e86 using enum \u8bed\u53e5\uff0c\u5b83\u53ef\u4ee5\u5c06\u6bcf\u4e2a\u679a\u4e3e\u4f5c\u7528\u57df\u4e2d\u7684\u679a\u4e3e\u503c\u5168\u90e8\u5bfc\u5165\u5230\u5f53\u524d\u4f5c\u7528\u57df\u3002\u5f53\u914d\u5408 \u679a\u4e3e\u7c7b \u4f7f\u7528\u65f6\uff0c\u53ef\u4ee5\u907f\u514d\u4f7f\u7528\u679a\u4e3e\u7c7b\u524d\u7f00\u8bbf\u95ee\u679a\u4e3e\u503c\u3002 \u8fd9\u4e2a\u7279\u6027\u975e\u5e38\u6709\u7528\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u91cd\u590d\u8f93\u5165\u5927\u91cf\u76f8\u540c\u524d\u7f00\u7684\u60c5\u51b5\u4e0b\uff0c\u4f8b\u5982\u5728\u4e0b\u9762\u7684switch\u8bed\u53e5\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include enum class Color { black , red , blue , }; constexpr std :: string_view getColor ( Color color ) { using enum Color ; // bring all Color enumerators into current scope (C++20) // We can now access the enumerators of Color without using a Color:: prefix switch ( color ) { case black : return \"black\" ; // note: black instead of Color::black case red : return \"red\" ; case blue : return \"blue\" ; default : return \"???\" ; } } int main () { Color shirt { Color :: blue }; std :: cout << \"Your shirt is \" << getColor ( shirt ) << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c Color \u662f\u4e00\u4e2a\u679a\u4e3e\u7c7b\uff0c\u6240\u4ee5\u6211\u4eec\u901a\u5e38\u9700\u8981\u4f7f\u7528\u5b8c\u5168\u9650\u5b9a\u540d\u6765\u8bbf\u95ee\u5176\u679a\u4e3e\u503c (\u4f8b\u5982 Color::blue )\u3002\u4f46\u662f\uff0c\u5728 getColor() \u51fd\u6570\u4e2d, \u7531\u4e8e\u6211\u4eec\u4f7f\u7528\u4e86 using enum Color; \uff0c\u56e0\u6b64\u53ef\u4ee5\u7701\u7565 Color:: \u524d\u7f00\u76f4\u63a5\u8bbf\u95ee\u679a\u4e3e\u503c\u3002 \u8fd9\u6837\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728switch\u8bed\u53e5\u4e2d\u8282\u7701\u5927\u91cf\u91cd\u590d\u3001\u5197\u4f59\u7684\u8f93\u5165\u3002","title":"10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09"},{"location":"10-4-scoped-enumerations-enum-classes/#_1","text":"\u8fd9\u4e2a\u89e3\u51b3\u65b9\u6848\u5c31\u662f \u6709\u4f5c\u7528\u57df\u679a\u4e3e (\u5728C++\u4e2d\u901a\u5e38\u79f0\u4e3a \u679a\u4e3e\u7c7b \uff0c\u81f3\u4e8e\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u53eb\u5b83\uff0c\u5f88\u5feb\u5c31\u4f1a\u4e0d\u8a00\u81ea\u660e\u4e86)\u3002 \u6709\u4f5c\u7528\u57df\u679a\u4e3e\u548c\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\uff08 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \uff09\u7528\u6cd5\u7c7b\u4f3c\uff0c\u4e0d\u8fc7\u5b83\u4eec\u6709\u4e24\u4e2a\u4e3b\u8981\u7684\u533a\u522b\uff1a\u5176\u4e00\uff0c\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u662f\u5f3a\u7c7b\u578b\u7684(\u5b83\u4e0d\u4f1a\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u6574\u578b) \uff0c\u5176\u4e8c\u5b83\u5177\u6709\u5f3a\u4f5c\u7528\u57df\u7ea6\u675f (\u5b83\u7684\u679a\u4e3e\u503c \u53ea\u4f1a \u88ab\u653e\u7f6e\u5728\u679a\u4e3e\u7c7b\u7684\u4f5c\u7528\u57df\u4e2d)\u3002 \u521b\u5efa\u6709\u4f5c\u7528\u57df\u679a\u4e3e\uff0c\u9700\u8981\u4f7f\u7528\u5173\u952e\u5b57 enum class \u3002\u5b9a\u4e49\u7684\u5176\u4ed6\u90e8\u5206\u5219\u548c\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u4e00\u6837\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include int main () { enum class Color // \"enum class\" \u5b9a\u4e49\u4e86\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b { red , // red \u662f Color \u4f5c\u7528\u57df\u7684\u6210\u5458 blue , }; enum class Fruit { banana , // banana \u662f Fruit \u4f5c\u7528\u57df\u7684\u6210\u5458 apple , }; Color color { Color :: red }; // \u6ce8\u610f: red \u4e0d\u80fd\u591f\u88ab\u76f4\u63a5\u8bbf\u95ee\uff0c\u5fc5\u987b\u4f7f\u7528 Color::red Fruit fruit { Fruit :: banana }; // note: banana \u4e0d\u80fd\u591f\u88ab\u76f4\u63a5\u8bbf\u95ee, \u5fc5\u987b\u4f7f\u7528 Fruit::banana if ( color == fruit ) // \u7f16\u8bd1\u9519\u8bef\uff1a\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053\u5982\u4f55\u6bd4\u8f83 Color \u548c Fruit std :: cout << \"color and fruit are equal \\n \" ; else std :: cout << \"color and fruit are not equal \\n \" ; return 0 ; } \u7f16\u8bd1\u5668\u4f1a\u62a5\u544a\u7a0b\u5e8f\u7684\u7b2c19\u884c\u5b58\u5728\u9519\u8bef\uff0c\u56e0\u4e3a\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u4e0d\u80fd\u591f\u88ab\u8f6c\u6362\u4e3a\u4efb\u4f55\u53ef\u6bd4\u8f83\u7684\u5176\u4ed6\u7c7b\u578b\u3002 \u9898\u5916\u8bdd class \u5173\u952e\u5b57 (\u4ee5\u53ca static \u5173\u952e\u5b57)\u662f C++ \u4e2d\u6700\u88ab\u201c\u6ee5\u7528\u201d\u7684\u5173\u952e\u5b57\uff0c\u5b83\u5728\u4e0d\u540c\u8bed\u5883\u4e0b\u5177\u6709\u4e0d\u540c\u7684\u542b\u4e49\u3002\u5c3d\u7ba1\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u4f7f\u7528\u4e86 class \u5173\u952e\u5b57\uff0c\u4f46\u662f\u5b83\u4eec\u5e76\u4e0d\u662f\u7c7b\u7c7b\u578b\uff08\u7c7b\u7c7b\u578b\u5305\u62ec\u7ed3\u6784\u4f53\u3001\u7c7b\u548c\u8054\u5408\u4f53\uff09\u3002","title":"\u6709\u4f5c\u7528\u57df\u679a\u4e3e"},{"location":"10-4-scoped-enumerations-enum-classes/#_2","text":"\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u4f1a\u5c06\u5176\u679a\u4e3e\u503c\u548c\u81ea\u5df1\u653e\u7f6e\u5728\u76f8\u540c\u7684\u4f5c\u7528\u57df\u4e2d\uff0c\u800c\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u4f1a\u5c06\u5b83\u7684\u679a\u4e3e\u503c\u653e\u7f6e\u5728\u81ea\u5df1\u5b9a\u4e49\u7684\u4f5c\u7528\u57df\u4e2d\u3002\u6362\u8a00\u4e4b\uff0c\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u540c\u65f6\u662f\u5176\u679a\u4e3e\u503c\u7684\u547d\u540d\u7a7a\u95f4\u3002\u8fd9\u79cd\u81ea\u5e26\u7684\u4f5c\u7528\u57df\u6709\u52a9\u4e8e\u907f\u514d\u547d\u540d\u7a7a\u95f4\u6c61\u67d3\u548c\u6f5c\u5728\u7684\u547d\u540d\u51b2\u7a81\u3002 \u8981\u8bbf\u95ee\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u503c\uff0c\u5c31\u50cf\u5b83\u4f4d\u4e8e\u4e0e\u4f5c\u7528\u57df\u679a\u4e3e\u540c\u540d\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\u4e00\u6837: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { enum class Color // \"enum class\" defines this as a scoped enum rather than an unscoped enum { red , // red is considered part of Color's scope region blue , }; std :: cout << red << '\\n' ; // compile error: red not defined in this scope region std :: cout << Color :: red << '\\n' ; // compile error: std::cout doesn't know how to print this (will not implicitly convert to int) Color color { Color :: blue }; // okay return 0 ; } \u56e0\u4e3a\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u4e3a\u679a\u4e3e\u503c\u63d0\u4f9b\u4e86\u5b83\u4eec\u81ea\u5df1\u7684\u9690\u5f0f\u540d\u79f0\u7a7a\u95f4\uff0c\u6240\u4ee5\u6ca1\u6709\u5fc5\u8981\u5c06\u4f5c\u7528\u57df\u679a\u4e3e\u653e\u5728\u53e6\u4e00\u4e2a\u4f5c\u7528\u57df(\u6bd4\u5982\u540d\u79f0\u7a7a\u95f4)\u4e2d\uff0c\u9664\u975e\u6709\u5176\u4ed6\u4ee4\u4eba\u4fe1\u670d\u7684\u7406\u7531\u8fd9\u6837\u505a\uff0c\u56e0\u4e3a\u8fd9\u6837\u505a\u662f\u591a\u4f59\u7684\u3002","title":"\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u4f1a\u5b9a\u4e49\u81ea\u5df1\u7684\u4f5c\u7528\u57df"},{"location":"10-4-scoped-enumerations-enum-classes/#_3","text":"\u548c\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u4e0d\u540c\uff0c\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u4e0d\u4f1a\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u6574\u578b\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u662f\u6709\u76ca\u5730\uff0c\u56e0\u4e3a\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u8fd9\u6837\u7684\u8f6c\u6362\u90fd\u4e0d\u5408\u7406\uff0c\u6240\u4ee5\u8fd9\u6709\u52a9\u4e8e\u5e2e\u6211\u4eec\u907f\u514d\u8bed\u4e49\u9519\u8bef\uff0c\u4f8b\u5982\u6bd4\u8f83\u4e24\u4e2a\u4e0d\u540c\u679a\u4e3e\u7c7b\u578b\u4e2d\u7684\u679a\u4e3e\u503c\u6216\u8005\u4f7f\u7528\u679a\u4e3e\u503c\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\uff08 red + 5 \uff09\u3002 \u6ce8\u610f\uff0c\u4f60\u53ef\u4ee5\u6bd4\u8f83\u6765\u81ea\u76f8\u540c\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u4e2d\u7684\u679a\u4e3e\u503c(\u56e0\u4e3a\u5b83\u4eec\u5177\u6709\u76f8\u540c\u7684\u7c7b\u578b)\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { enum class Color { red , blue , }; Color shirt { Color :: red }; if ( shirt == Color :: red ) // Color \u548c Color \u7684\u6bd4\u8f83\u662f\u53ef\u4ee5\u7684 std :: cout << \"The shirt is red! \\n \" ; else if ( shirt == Color :: blue ) std :: cout << \"The shirt is blue! \\n \" ; return 0 ; } \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5c06\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u503c\u4f5c\u4e3a\u6574\u6570\u5904\u7406\u4e5f\u662f\u6709\u7528\u7684\u3002\u5728\u8fd9\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 static_cast \u663e\u5f0f\u5730\u5c06\u679a\u4e3e\u503c\u8f6c\u6362\u4e3a\u6574\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { enum class Color { red , blue , }; Color color { Color :: blue }; std :: cout << color << '\\n' ; // won't work, because there's no implicit conversion to int std :: cout << static_cast < int > ( color ) << '\\n' ; // will print 1 return 0 ; } \u53cd\u8fc7\u6765\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06\u4e00\u4e2a\u6574\u578b\u8f6c\u6362\u4e3a\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u503c\uff0c\u8fd9\u5728\u5904\u7406\u8f93\u5165\u65f6\u5c24\u5176\u6709\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include enum class Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { std :: cout << \"Enter a pet (0=cat, 1=dog, 2=pig, 3=whale): \" ; int input {}; std :: cin >> input ; // input an integer Pet pet { static_cast < Pet > ( input ) }; // static_cast our integer to a Pet return 0 ; } \u5728 C++17 \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u6574\u578b\u6570\uff08\u65e0\u9700\u7c7b\u578b\u8f6c\u6362\uff09\u76f4\u63a5\u521d\u59cb\u5316\u6709\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u800c\u4e14\u4e0d\u50cf\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\uff0c\u4f60\u4e0d\u9700\u8981\u6307\u660e\u4e00\u4e2a\u57fa\u7c7b\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u63a8\u8350\u4f7f\u7528\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u800c\u975e\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\uff0c\u9664\u975e\u4f60\u6709\u8db3\u591f\u7684\u7406\u7531\u4e0d\u8fd9\u4e48\u505a\u3002 \u5c3d\u7ba1\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u63d0\u4f9b\u4e86\u5f88\u591a\u597d\u7684\u529f\u80fd\uff0c\u4f46\u662f\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u5728C++\u4e2d\u4ecd\u7136\u975e\u5e38\u5e38\u89c1\uff0c\u56e0\u4e3a\u5728\u6709\u4e9b\u573a\u5408\u4e0b\u6211\u4eec\u5e0c\u671b\u679a\u4e3e\u503c\u80fd\u591f\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u6574\u578b\uff08\u603b\u662f\u4f7f\u7528 static_cast \u8fdb\u884c\u8f6c\u6362\u6709\u4e9b\u9ebb\u70e6\uff09\uff0c\u800c\u4e14\u6709\u65f6\u6211\u4eec\u4e5f\u4e0d\u9700\u8981\u989d\u5916\u7684\u547d\u540d\u7a7a\u95f4\u5206\u9694\u3002","title":"\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u4e0d\u4f1a\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u6574\u578b"},{"location":"10-4-scoped-enumerations-enum-classes/#_4","text":"\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u5f88\u597d\uff0c\u4f46\u5b83\u4e0d\u80fd\u591f\u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u8fd9\u4e00\u7279\u70b9\u6709\u65f6\u53ef\u80fd\u4f1a\u662f\u4e00\u4e2a\u75db\u70b9\u3002\u5982\u679c\u9700\u8981\u7ecf\u5e38\u5c06\u4f5c\u7528\u57df\u679a\u4e3e\u8f6c\u6362\u4e3a\u6574\u6570(\u4f8b\u5982\uff0c\u5728\u5e0c\u671b\u4f7f\u7528\u4f5c\u7528\u57df\u679a\u4e3e\u4f5c\u4e3a\u6570\u7ec4\u4e0b\u6807\u7684\u60c5\u51b5\u4e0b)\uff0c\u90a3\u4e48\u6bcf\u6b21\u9700\u8981\u8f6c\u6362\u65f6\u90fd\u5fc5\u987b\u4f7f\u7528 static_cast \uff0c\u8fd9\u65e0\u7591\u4f1a\u4f7f\u4ee3\u7801\u770b\u8d77\u6765\u975e\u5e38\u6df7\u4e71 \u5982\u679c\u4f60\u6709\u8fd9\u6837\u7684\u9700\u6c42\uff0c\u5e0c\u671b\u4f7f\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u503c\u80fd\u591f\u66f4\u5bb9\u6613\u5730\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u90a3\u4e48\u53ef\u4ee5\u5c1d\u8bd5\u91cd\u8f7d\u4e00\u5143 operator+ \u6765\u6267\u884c\u8fd9\u79cd\u8f6c\u6362\u3002\u6211\u4eec\u8fd8\u6ca1\u6709\u89e3\u91ca\u8fd9\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u6240\u4ee5\u73b0\u5728\u5c31\u628a\u5b83\u5f53\u6210\u9b54\u672f\u5427\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include enum class Animals { chicken , // 0 dog , // 1 cat , // 2 elephant , // 3 duck , // 4 snake , // 5 maxAnimals , }; // Overload the unary + operator to convert Animals to the underlying type // adapted from https://stackoverflow.com/a/42198760, thanks to Pixelchemist for the idea constexpr auto operator + ( Animals a ) noexcept { return static_cast < std :: underlying_type_t < Animals >> ( a ); } int main () { std :: cout << + Animals :: elephant << '\\n' ; // convert Animals::elephant to an integer using unary operator+ return 0 ; } \u6253\u5370\uff1a 1 3 \u6b64\u65b9\u6cd5\u53ef\u907f\u514d\u4e0d\u7ecf\u610f\u5730\u9690\u5f0f\u8f6c\u6362\uff0c\u540c\u65f6\u8fd8\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u5730\u663e\u793a\u8f6c\u6362\u65b9\u6cd5\u3002","title":"\u7b80\u5316\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u503c\u5230\u6574\u578b\u7684\u8f6c\u6362(\u8fdb\u9636\u8bdd\u9898)"},{"location":"10-4-scoped-enumerations-enum-classes/#using-enum-c20","text":"C++20 \u5f15\u5165\u4e86 using enum \u8bed\u53e5\uff0c\u5b83\u53ef\u4ee5\u5c06\u6bcf\u4e2a\u679a\u4e3e\u4f5c\u7528\u57df\u4e2d\u7684\u679a\u4e3e\u503c\u5168\u90e8\u5bfc\u5165\u5230\u5f53\u524d\u4f5c\u7528\u57df\u3002\u5f53\u914d\u5408 \u679a\u4e3e\u7c7b \u4f7f\u7528\u65f6\uff0c\u53ef\u4ee5\u907f\u514d\u4f7f\u7528\u679a\u4e3e\u7c7b\u524d\u7f00\u8bbf\u95ee\u679a\u4e3e\u503c\u3002 \u8fd9\u4e2a\u7279\u6027\u975e\u5e38\u6709\u7528\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u91cd\u590d\u8f93\u5165\u5927\u91cf\u76f8\u540c\u524d\u7f00\u7684\u60c5\u51b5\u4e0b\uff0c\u4f8b\u5982\u5728\u4e0b\u9762\u7684switch\u8bed\u53e5\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include enum class Color { black , red , blue , }; constexpr std :: string_view getColor ( Color color ) { using enum Color ; // bring all Color enumerators into current scope (C++20) // We can now access the enumerators of Color without using a Color:: prefix switch ( color ) { case black : return \"black\" ; // note: black instead of Color::black case red : return \"red\" ; case blue : return \"blue\" ; default : return \"???\" ; } } int main () { Color shirt { Color :: blue }; std :: cout << \"Your shirt is \" << getColor ( shirt ) << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c Color \u662f\u4e00\u4e2a\u679a\u4e3e\u7c7b\uff0c\u6240\u4ee5\u6211\u4eec\u901a\u5e38\u9700\u8981\u4f7f\u7528\u5b8c\u5168\u9650\u5b9a\u540d\u6765\u8bbf\u95ee\u5176\u679a\u4e3e\u503c (\u4f8b\u5982 Color::blue )\u3002\u4f46\u662f\uff0c\u5728 getColor() \u51fd\u6570\u4e2d, \u7531\u4e8e\u6211\u4eec\u4f7f\u7528\u4e86 using enum Color; \uff0c\u56e0\u6b64\u53ef\u4ee5\u7701\u7565 Color:: \u524d\u7f00\u76f4\u63a5\u8bbf\u95ee\u679a\u4e3e\u503c\u3002 \u8fd9\u6837\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728switch\u8bed\u53e5\u4e2d\u8282\u7701\u5927\u91cf\u91cd\u590d\u3001\u5197\u4f59\u7684\u8f93\u5165\u3002","title":"using enum\u00a0\u8bed\u53e5\u00a0\uff08C++20\uff09"},{"location":"10-5-Introduction-to-structs-members-and-member-selection/","tags":["struct","members","member selection"],"text":"Key Takeaway \u5728\u7f16\u7a0b\u4e2d\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u4f7f\u7528\u591a\u4e2a\u53d8\u91cf\u6765\u8868\u793a\u66f4\u590d\u6742\u7684\u5bf9\u8c61\uff0c\u6b63\u5982\u5728\u4e4b\u524d\u7ae0\u8282\u4e2d( 9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b )\u4ecb\u7ecd\u7684\u90a3\u6837\uff0c\u5206\u6570\u662f\u7531\u4e24\u4e2a\u5177\u6709\u5173\u8054\u7684\u6570\u5b66\u5bf9\u8c61\u2014\u2014\u5206\u5b50\u5206\u6bcd\u7ec4\u6210\u7684\u3002 \u6216\u8005\uff0c\u5047\u8bbe\u6211\u4eec\u60f3\u8981\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5176\u4e2d\u9700\u8981\u5b58\u50a8\u5173\u4e8e\u516c\u53f8\u5458\u5de5\u7684\u4fe1\u606f\u3002\u4e3a\u6b64\u6211\u4eec\u9700\u8981\u8ffd\u8e2a\u8bf8\u5982\u5458\u5de5\u59d3\u540d\u3001\u5934\u8854\u3001\u5e74\u9f84\u3001\u5458\u5de5id\u3001\u7ecf\u7406id\u3001\u5de5\u8d44\u3001\u751f\u65e5\u3001\u96c7\u4f63\u65e5\u671f\u7b49\u5c5e\u6027\u3002 1 2 3 4 5 6 7 8 9 10 11 12 std :: string name ; std :: string title ; int age ; int id ; int managerId ; double wage ; int birthdayYear ; int birthdayMonth ; int birthdayDay ; int hireYear ; int hireMonth ; int hireDay ; \u7136\u800c\uff0c\u8fd9\u79cd\u65b9\u5f0f\u5b58\u5728\u8bb8\u591a\u95ee\u9898\u3002\u9996\u5148\uff0c\u6211\u4eec\u4e0d\u6e05\u695a\u8fd9\u4e9b\u53d8\u91cf\u662f\u5426\u771f\u7684\u76f8\u5173(\u4f60\u5fc5\u987b\u9605\u8bfb\u6ce8\u91ca\uff0c\u6216\u8005\u67e5\u770b\u5b83\u4eec\u5728\u4e0a\u4e0b\u6587\u4e2d\u662f\u5982\u4f55\u4f7f\u7528\u7684)\u3002\u5176\u6b21\uff0c\u73b0\u5728\u670912\u4e2a\u53d8\u91cf\u9700\u8981\u7ba1\u7406\u3002\u5982\u679c\u6211\u4eec\u60f3\u8981\u5c06\u8fd9\u4e2a\u96c7\u5458\u4f20\u9012\u7ed9\u4e00\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u5fc5\u987b\u4f20\u901212\u4e2a\u53c2\u6570(\u5e76\u4e14\u987a\u5e8f\u6b63\u786e)\uff0c\u8fd9\u5c06\u4f7f\u6211\u4eec\u7684\u51fd\u6570\u539f\u578b\u548c\u51fd\u6570\u8c03\u7528\u53d8\u5f97\u6df7\u4e71\u3002\u65e2\u7136\u51fd\u6570\u53ea\u80fd\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u90a3\u4e48\u51fd\u6570\u5982\u4f55\u8fd4\u56de\u4e00\u4e2a\u96c7\u5458\u5462? \u5982\u679c\u6211\u4eec\u60f3\u8981\u4e00\u4e2a\u4ee5\u4e0a\u7684\u5458\u5de5\uff0c\u6211\u4eec\u9700\u8981\u4e3a\u6bcf\u4e2a\u989d\u5916\u7684\u5458\u5de5\u5b9a\u4e4912\u4e2a\u4ee5\u4e0a\u7684\u53d8\u91cf(\u6bcf\u4e2a\u53d8\u91cf\u90fd\u9700\u8981\u4e00\u4e2a\u60df\u4e00\u7684\u540d\u79f0)\uff01\u8fd9\u663e\u7136\u662f\u65e0\u6cd5\u6269\u5c55\u7684\u3002\u6211\u4eec\u771f\u6b63\u9700\u8981\u7684\u662f\u67d0\u79cd\u65b9\u6cd5\u6765\u7ec4\u7ec7\u6240\u6709\u8fd9\u4e9b\u76f8\u5173\u7684\u6570\u636e\u5757\uff0c\u4f7f\u5b83\u4eec\u66f4\u5bb9\u6613\u7ba1\u7406\u3002 \u5e78\u8fd0\u7684\u662f\uff0cC++ \u63d0\u4f9b\u4e86\u4e24\u4e2a\u7b26\u5408\u7c7b\u578b\u7528\u4e8e\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\uff1a\u7ed3\u6784\u4f53\u548c\u7c7b\u3002\u7ed3\u6784\u4f53\u662f\u4e00\u79cd\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b( 10.1 - \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7b80\u4ecb ) \uff0c\u4f7f\u7528\u7ed3\u6784\u4f53\u6211\u4eec\u53ef\u4ee5\u628a\u591a\u4e2a\u53d8\u91cf\u805a\u5408\u6210\u4e00\u4e2a\u5355\u72ec\u7684\u7c7b\u578b\u3002\u7a0d\u540e\u4f60\u5c06\u4f1a\u770b\u5230\uff0c\u8fd9\u4f7f\u5f97\u7ba1\u7406\u591a\u4e2a\u76f8\u5173\u8054\u53d8\u91cf\u53d8\u5f97\u975e\u5e38\u7b80\u5355\uff01 \u5b9a\u4e49\u7ed3\u6784\u4f53 \u56e0\u4e3a\u7ed3\u6784\u662f\u7a0b\u5e8f\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u6240\u4ee5\u5728\u5f00\u59cb\u4f7f\u7528\u7ed3\u6784\u7c7b\u578b\u4e4b\u524d\uff0c\u6211\u4eec\u5fc5\u987b\u5148\u544a\u8bc9\u7f16\u8bd1\u5668\u7ed3\u6784\u7c7b\u578b\u662f\u4ec0\u4e48\u6837\u5b50\u7684\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5316\u5458\u5de5\u7684\u7ed3\u6784\u5b9a\u4e49\u793a\u4f8b\uff1a 1 2 3 4 5 6 struct Employee { int id {}; int age {}; double wage {}; }; struct \u5173\u952e\u5b57\u7528\u4e8e\u544a\u8bc9\u7f16\u8bd1\u5668\u6b64\u5904\u8981\u5b9a\u4e49\u4e00\u4e2a\u7ed3\u6784\u4f53\u2014\u2014\u540d\u4e3a Employee \uff08\u56e0\u4e3a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7684\u547d\u540d\u901a\u5e38\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff09\u3002 \u968f\u540e\uff0c\u5728\u5927\u62ec\u53f7\u4e2d\uff0c\u5b9a\u4e49\u4e86 Employee \u6240\u9700\u8981\u7684\u5404\u4e2a\u53d8\u91cf\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6bcf\u4e2a Employee \u521b\u5efa\u4e863\u4e2a\u53d8\u91cf\uff1a int id \u3001 int age \u548c double wage \u3002\u8fd9\u4e9b\u53d8\u91cf\u662f\u7ed3\u6784\u4f53\u7684\u4e00\u90e8\u5206\uff0c\u79f0\u4e3a\u6570\u636e\u6210\u5458\uff08\u6216\u8005 \u6210\u5458\u53d8\u91cf \uff09\u3002 \u5c0f\u8d34\u58eb \u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f1a\u7ecf\u5e38\u7528\u5230\u201c\u6210\u5458\u201d\u8fd9\u4e2a\u8bcd\uff0c\u6240\u4ee5\u4e00\u5b9a\u8981\u8bb0\u4f4f\u5b83\u7684\u610f\u601d\u3002 \u6b63\u5982\u53ef\u4ee5\u4f7f\u7528\u7a7a\u7684\u5927\u62ec\u53f7\u5bf9\u4e00\u822c\u53d8\u91cf\u8fdb\u884c \u521d\u59cb\u5316 \u4e00\u6837 ( 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 ) \uff0c\u7a7a\u7684\u5927\u62ec\u53f7\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5728\u521b\u5efa Employee \u65f6\u5bf9\u5176\u6210\u5458\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 ( 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 )\u3002 \u6700\u540e\uff0c\u5728\u5b9a\u4e49\u7ed3\u675f\u5904\u6dfb\u52a0\u5206\u53f7\u3002 \u63d0\u9192\u4e00\u4e0b\uff0c\u8fd9\u91cc\u7684 Employee \u53ea\u662f\u4e00\u4e2a\u7c7b\u578b\u5b9a\u4e49\u2014\u2014\u5e76\u6ca1\u6709\u5b9e\u9645\u521b\u5efa\u4efb\u4f55\u5bf9\u8c61\u3002 \u5b9a\u4e49\u7ed3\u6784\u4f53\u5bf9\u8c61 \u4e3a\u4e86\u4f7f\u7528 Employee \u7c7b\u578b\uff0c\u5b9a\u4e49\u4e00\u4e2a Employee \u7c7b\u578b\u7684\u53d8\u91cf\u5373\u53ef\uff1a 1 Employee joe ; // Employee is the type, joe is the variable name \u6b64\u5904\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a joe \u7684 Employee \u7c7b\u578b\u53d8\u91cf\u3002\u5b9a\u4e49 joe \u65f6\uff0c\u5c06\u521b\u5efa\u4e00\u4e2a Employee \u5bf9\u8c61\uff0c\u5e76\u6309\u987a\u5e8f\u521b\u5efa\u5176\u4e2d\u76843\u4e2a\u6570\u636e\u6210\u5458(\u7136\u540e\u521d\u59cb\u5316\u503c)\u3002 \u5c31\u50cf\u4efb\u4f55\u5176\u4ed6\u7c7b\u578b\u4e00\u6837\uff0c\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2a\u6b64\u7c7b\u578b\u53d8\u91cf: 1 2 Employee joe ; // create an Employee struct for Joe Employee frank ; // create an Employee struct for Frank \u8bbf\u95ee\u6210\u5458 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c joe \u6307\u7684\u662f\u6574\u4e2a\u7ed3\u6784\u4f53\u5bf9\u8c61(\u5176\u4e2d\u5305\u542b\u6210\u5458\u53d8\u91cf)\u3002\u8981\u8bbf\u95ee\u7279\u5b9a\u7684\u6210\u5458\u53d8\u91cf\uff0c\u9700\u8981\u5728\u7ed3\u6784\u53d8\u91cf\u540d\u548c\u6210\u5458\u540d\u4e4b\u95f4\u4f7f\u7528 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 \u3002\u4f8b\u5982\uff0c\u8981\u8bbf\u95ee joe \u7684\u5e74\u9f84\u6210\u5458\uff0c\u9700\u8981\u5199\u4f5c joe.age \u3002 \u7ed3\u6784\u4f53\u6210\u5458\u53d8\u91cf\u5c31\u50cf\u666e\u901a\u53d8\u91cf\u4e00\u6837\u5de5\u4f5c\uff0c\u56e0\u6b64\u53ef\u4ee5\u5bf9\u5b83\u4eec\u8fdb\u884c\u4e00\u822c\u7684\u64cd\u4f5c\uff0c\u5305\u62ec\u8d4b\u503c\u3001\u8fd0\u7b97\u3001\u6bd4\u8f83\u7b49\u2026\u2026 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe ; joe . age = 32 ; // use member selection operator (.) to select the age member of variable joe std :: cout << joe . age << '\\n' ; // print joe's age return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 32 \u4f7f\u7528\u7ed3\u6784\u4f53\u7684\u6700\u5927\u6709\u65f6\u5728\u4e8e\uff0c\u4f60\u53ea\u9700\u8981\u4e3a\u6bcf\u4e2a\u7ed3\u6784\u4f53\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf(\u6210\u5458\u540d\u662f\u7ed3\u6784\u4f53\u5b9a\u4e49\u4e2d\u56fa\u5b9a\u7684\u90e8\u5206)\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9e\u4f8b\u5316\u4e86\u4e24\u4e2a Employee \u5bf9\u8c61\uff1a joe \u548c frank \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe ; joe . id = 14 ; joe . age = 32 ; joe . wage = 60000.0 ; Employee frank ; frank . id = 15 ; frank . age = 28 ; frank . wage = 45000.0 ; int totalAge { joe . age + frank . age }; if ( joe . wage > frank . wage ) std :: cout << \"Joe makes more than Frank \\n \" ; else if ( joe . wage < frank . wage ) std :: cout << \"Joe makes less than Frank \\n \" ; else std :: cout << \"Joe and Frank make the same amount \\n \" ; // Frank got a promotion frank . wage += 5000.0 ; // Today is Joe's birthday ++ joe . age ; // use pre-increment to increment Joe's age by 1 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f88\u5bb9\u6613\u5224\u65ad\u54ea\u4e9b\u6210\u5458\u53d8\u91cf\u5c5e\u4e8e Joe \uff0c\u54ea\u4e9b\u5c5e\u4e8e Frank \u3002\u8fd9\u63d0\u4f9b\u4e86\u6bd4\u5355\u4e2a\u53d8\u91cf\u66f4\u9ad8\u5c42\u6b21\u7684\u7ec4\u7ec7\u65b9\u6cd5\u3002\u6b64\u5916\uff0c\u7531\u4e8e Joe \u7684\u6210\u5458\u548c Frank \u7684\u6210\u5458\u5177\u6709\u76f8\u540c\u7684\u540d\u79f0\uff0c\u5f53\u4f60\u6709\u591a\u4e2a\u76f8\u540c\u7ed3\u6784\u7c7b\u578b\u7684\u53d8\u91cf\u65f6\uff0c\u8fd9\u5c06\u63d0\u4f9b\u4e00\u81f4\u6027\u3002 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u7ee7\u7eed\u63a2\u7d22\u7ed3\u6784\u4f53\uff0c\u4ee5\u53ca\u5982\u4f55\u521d\u59cb\u5316\u5b83\u4eec\u3002","title":"10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9"},{"location":"10-5-Introduction-to-structs-members-and-member-selection/#_1","text":"\u56e0\u4e3a\u7ed3\u6784\u662f\u7a0b\u5e8f\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u6240\u4ee5\u5728\u5f00\u59cb\u4f7f\u7528\u7ed3\u6784\u7c7b\u578b\u4e4b\u524d\uff0c\u6211\u4eec\u5fc5\u987b\u5148\u544a\u8bc9\u7f16\u8bd1\u5668\u7ed3\u6784\u7c7b\u578b\u662f\u4ec0\u4e48\u6837\u5b50\u7684\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5316\u5458\u5de5\u7684\u7ed3\u6784\u5b9a\u4e49\u793a\u4f8b\uff1a 1 2 3 4 5 6 struct Employee { int id {}; int age {}; double wage {}; }; struct \u5173\u952e\u5b57\u7528\u4e8e\u544a\u8bc9\u7f16\u8bd1\u5668\u6b64\u5904\u8981\u5b9a\u4e49\u4e00\u4e2a\u7ed3\u6784\u4f53\u2014\u2014\u540d\u4e3a Employee \uff08\u56e0\u4e3a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7684\u547d\u540d\u901a\u5e38\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff09\u3002 \u968f\u540e\uff0c\u5728\u5927\u62ec\u53f7\u4e2d\uff0c\u5b9a\u4e49\u4e86 Employee \u6240\u9700\u8981\u7684\u5404\u4e2a\u53d8\u91cf\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6bcf\u4e2a Employee \u521b\u5efa\u4e863\u4e2a\u53d8\u91cf\uff1a int id \u3001 int age \u548c double wage \u3002\u8fd9\u4e9b\u53d8\u91cf\u662f\u7ed3\u6784\u4f53\u7684\u4e00\u90e8\u5206\uff0c\u79f0\u4e3a\u6570\u636e\u6210\u5458\uff08\u6216\u8005 \u6210\u5458\u53d8\u91cf \uff09\u3002 \u5c0f\u8d34\u58eb \u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f1a\u7ecf\u5e38\u7528\u5230\u201c\u6210\u5458\u201d\u8fd9\u4e2a\u8bcd\uff0c\u6240\u4ee5\u4e00\u5b9a\u8981\u8bb0\u4f4f\u5b83\u7684\u610f\u601d\u3002 \u6b63\u5982\u53ef\u4ee5\u4f7f\u7528\u7a7a\u7684\u5927\u62ec\u53f7\u5bf9\u4e00\u822c\u53d8\u91cf\u8fdb\u884c \u521d\u59cb\u5316 \u4e00\u6837 ( 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 ) \uff0c\u7a7a\u7684\u5927\u62ec\u53f7\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5728\u521b\u5efa Employee \u65f6\u5bf9\u5176\u6210\u5458\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 ( 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 )\u3002 \u6700\u540e\uff0c\u5728\u5b9a\u4e49\u7ed3\u675f\u5904\u6dfb\u52a0\u5206\u53f7\u3002 \u63d0\u9192\u4e00\u4e0b\uff0c\u8fd9\u91cc\u7684 Employee \u53ea\u662f\u4e00\u4e2a\u7c7b\u578b\u5b9a\u4e49\u2014\u2014\u5e76\u6ca1\u6709\u5b9e\u9645\u521b\u5efa\u4efb\u4f55\u5bf9\u8c61\u3002","title":"\u5b9a\u4e49\u7ed3\u6784\u4f53"},{"location":"10-5-Introduction-to-structs-members-and-member-selection/#_2","text":"\u4e3a\u4e86\u4f7f\u7528 Employee \u7c7b\u578b\uff0c\u5b9a\u4e49\u4e00\u4e2a Employee \u7c7b\u578b\u7684\u53d8\u91cf\u5373\u53ef\uff1a 1 Employee joe ; // Employee is the type, joe is the variable name \u6b64\u5904\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a joe \u7684 Employee \u7c7b\u578b\u53d8\u91cf\u3002\u5b9a\u4e49 joe \u65f6\uff0c\u5c06\u521b\u5efa\u4e00\u4e2a Employee \u5bf9\u8c61\uff0c\u5e76\u6309\u987a\u5e8f\u521b\u5efa\u5176\u4e2d\u76843\u4e2a\u6570\u636e\u6210\u5458(\u7136\u540e\u521d\u59cb\u5316\u503c)\u3002 \u5c31\u50cf\u4efb\u4f55\u5176\u4ed6\u7c7b\u578b\u4e00\u6837\uff0c\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2a\u6b64\u7c7b\u578b\u53d8\u91cf: 1 2 Employee joe ; // create an Employee struct for Joe Employee frank ; // create an Employee struct for Frank","title":"\u5b9a\u4e49\u7ed3\u6784\u4f53\u5bf9\u8c61"},{"location":"10-5-Introduction-to-structs-members-and-member-selection/#_3","text":"\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c joe \u6307\u7684\u662f\u6574\u4e2a\u7ed3\u6784\u4f53\u5bf9\u8c61(\u5176\u4e2d\u5305\u542b\u6210\u5458\u53d8\u91cf)\u3002\u8981\u8bbf\u95ee\u7279\u5b9a\u7684\u6210\u5458\u53d8\u91cf\uff0c\u9700\u8981\u5728\u7ed3\u6784\u53d8\u91cf\u540d\u548c\u6210\u5458\u540d\u4e4b\u95f4\u4f7f\u7528 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 \u3002\u4f8b\u5982\uff0c\u8981\u8bbf\u95ee joe \u7684\u5e74\u9f84\u6210\u5458\uff0c\u9700\u8981\u5199\u4f5c joe.age \u3002 \u7ed3\u6784\u4f53\u6210\u5458\u53d8\u91cf\u5c31\u50cf\u666e\u901a\u53d8\u91cf\u4e00\u6837\u5de5\u4f5c\uff0c\u56e0\u6b64\u53ef\u4ee5\u5bf9\u5b83\u4eec\u8fdb\u884c\u4e00\u822c\u7684\u64cd\u4f5c\uff0c\u5305\u62ec\u8d4b\u503c\u3001\u8fd0\u7b97\u3001\u6bd4\u8f83\u7b49\u2026\u2026 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe ; joe . age = 32 ; // use member selection operator (.) to select the age member of variable joe std :: cout << joe . age << '\\n' ; // print joe's age return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 32 \u4f7f\u7528\u7ed3\u6784\u4f53\u7684\u6700\u5927\u6709\u65f6\u5728\u4e8e\uff0c\u4f60\u53ea\u9700\u8981\u4e3a\u6bcf\u4e2a\u7ed3\u6784\u4f53\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf(\u6210\u5458\u540d\u662f\u7ed3\u6784\u4f53\u5b9a\u4e49\u4e2d\u56fa\u5b9a\u7684\u90e8\u5206)\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9e\u4f8b\u5316\u4e86\u4e24\u4e2a Employee \u5bf9\u8c61\uff1a joe \u548c frank \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe ; joe . id = 14 ; joe . age = 32 ; joe . wage = 60000.0 ; Employee frank ; frank . id = 15 ; frank . age = 28 ; frank . wage = 45000.0 ; int totalAge { joe . age + frank . age }; if ( joe . wage > frank . wage ) std :: cout << \"Joe makes more than Frank \\n \" ; else if ( joe . wage < frank . wage ) std :: cout << \"Joe makes less than Frank \\n \" ; else std :: cout << \"Joe and Frank make the same amount \\n \" ; // Frank got a promotion frank . wage += 5000.0 ; // Today is Joe's birthday ++ joe . age ; // use pre-increment to increment Joe's age by 1 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f88\u5bb9\u6613\u5224\u65ad\u54ea\u4e9b\u6210\u5458\u53d8\u91cf\u5c5e\u4e8e Joe \uff0c\u54ea\u4e9b\u5c5e\u4e8e Frank \u3002\u8fd9\u63d0\u4f9b\u4e86\u6bd4\u5355\u4e2a\u53d8\u91cf\u66f4\u9ad8\u5c42\u6b21\u7684\u7ec4\u7ec7\u65b9\u6cd5\u3002\u6b64\u5916\uff0c\u7531\u4e8e Joe \u7684\u6210\u5458\u548c Frank \u7684\u6210\u5458\u5177\u6709\u76f8\u540c\u7684\u540d\u79f0\uff0c\u5f53\u4f60\u6709\u591a\u4e2a\u76f8\u540c\u7ed3\u6784\u7c7b\u578b\u7684\u53d8\u91cf\u65f6\uff0c\u8fd9\u5c06\u63d0\u4f9b\u4e00\u81f4\u6027\u3002 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u7ee7\u7eed\u63a2\u7d22\u7ed3\u6784\u4f53\uff0c\u4ee5\u53ca\u5982\u4f55\u521d\u59cb\u5316\u5b83\u4eec\u3002","title":"\u8bbf\u95ee\u6210\u5458"},{"location":"10-6-struct-aggregate-initialization/","tags":["struct","aggregate initialization","C++20"],"text":"Key Takeaway \u6570\u636e\u6210\u5458\u9ed8\u8ba4\u4e0d\u4f1a\u88ab\u521d\u59cb\u5316\uff0c\u9664\u975e\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\u3002 \u5982\u679c\u805a\u5408\u7c7b\u578b\u88ab\u521d\u59cb\u5316\u4e86\uff0c\u90a3\u4e48\u5373\u4fbf\u6ca1\u6709\u5bf9\u6bcf\u4e2a\u6210\u5458\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u5219\u8be5\u6210\u5458\u4f1a\u4f7f\u7528\u81ea\u5df1\u7684\u521d\u59cb\u5316\u503c\uff08\u53ef\u4ee5\u662f0\uff0c\u4e5f\u53ef\u4ee5\u662f\u5176\u4ed6\u6307\u5b9a\u503c\uff0c\u7531\u5176\u81ea\u8eab\u7684\u62ec\u53f7\u521d\u59cb\u5316\u51b3\u5b9a\uff09 \u5217\u8868\u521d\u59cb\u5316\u805a\u5408\u7c7b\u578b\u65f6\uff0c\u6309\u7167 \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \u8fdb\u884c\uff0c\u6b64\u65f6\u5982\u679c\u63d2\u5165\u65b0\u58f0\u660e\u7684\u6210\u5458\u5230\u524d\u9762\uff0c\u5219\u5bf9\u5e94\u7684\u5217\u8868\u5fc5\u987b\u79fb\u52a8\uff0c\u800c\u7f16\u8bd1\u5668\u5e76\u4e0d\u4f1a\u68c0\u6d4b\u5230\u8fd9\u7c7b\u95ee\u9898\u3002 \u5728\u5411\u805a\u5408\u4e2d\u6dfb\u52a0\u65b0\u6210\u5458\u65f6\uff0c\u6700\u5b89\u5168\u7684\u505a\u6cd5\u662f\u5c06\u5176\u6dfb\u52a0\u5230\u5b9a\u4e49\u5217\u8868\u7684\u5e95\u90e8\uff0c\u8fd9\u6837\u5176\u4ed6\u6210\u5458\u7684\u521d\u59cb\u5316\u5f0f\u5c31\u4e0d\u9700\u8981\u8c03\u6574\u987a\u5e8f\u3002 \u5728\u4e0a\u8282\u8bfe\u4e2d( 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 )\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u5982\u4f55\u5b9a\u4e49\u3001\u521d\u59cb\u5316\u4e00\u4e2a\u7ed3\u6784\u4f53\u4ee5\u53ca\u5982\u4f55\u8bbf\u95ee\u5176\u6210\u5458\u3002\u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5982\u4f55\u521d\u59cb\u5316\u4e00\u4e2a\u7ed3\u6784\u4f53\u3002 \u6570\u636e\u6210\u5458\u9ed8\u8ba4\u4e0d\u4f1a\u88ab\u521d\u59cb\u5316 \u548c\u666e\u901a\u53d8\u91cf\u7c7b\u4f3c\uff0c\u6570\u636e\u6210\u5458\u5e76\u4e0d\u4f1a\u88ab\u9ed8\u8ba4\u521d\u59cb\u5316\u3002\u8003\u8651\u4e0b\u9762\u7684\u7ed3\u6784\u4f53\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include struct Employee { int id ; // note: no initializer here int age ; double wage ; }; int main () { Employee joe ; // note: no initializer here either std :: cout << joe . id << '\\n' ; return 0 ; } \u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u63d0\u4f9b\u4efb\u4f55\u521d\u59cb\u5316\u503c\uff0c\u6240\u4ee5\u5f53\u5b9e\u4f8b\u5316 joe \u65f6\uff0c joe.id \u3001 joe.age \u548c joe.wage \u5904\u4e8e\u672a\u521d\u59cb\u5316\u72b6\u6001\u3002\u5f53\u6211\u4eec\u8bd5\u56fe\u6253\u5370 joe.id \u7684\u503c\u65f6\uff0c\u5c06\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4f46\u662f\uff0c\u5728\u5411\u4f60\u5c55\u793a\u5982\u4f55\u521d\u59cb\u5316\u7ed3\u6784\u4e4b\u524d\uff0c\u8ba9\u6211\u4eec\u5148\u7ed5\u8def\u8d70\u4e00\u5c0f\u6bb5\u8def\u3002 \u4ec0\u4e48\u662f\u805a\u5408\uff1f \u5728\u7f16\u7a0b\u9886\u57df\uff0c \u805a\u5408\u6570\u636e\u7c7b\u578b \u6307\u7684\u662f==\u4efb\u4f55\u5305\u542b\u591a\u4e2a\u6570\u636e\u6210\u5458\u7684\u7c7b\u578b==\u3002\u67d0\u4e9b\u7c7b\u578b\u7684\u805a\u5408\u7c7b\u578b\u5141\u8bb8\u6210\u5458\u5177\u6709\u4e0d\u540c\u7684\u7c7b\u578b(\u4f8b\u5982\u7ed3\u6784)\uff0c\u800c\u5176\u4ed6\u7c7b\u578b\u7684\u805a\u5408\u8981\u6c42\u6240\u6709\u6210\u5458\u5fc5\u987b\u5177\u6709\u5355\u4e00\u7c7b\u578b(\u4f8b\u5982\u6570\u7ec4)\u3002 \u5728C++\u4e2d\uff0c\u805a\u5408\u6570\u636e\u7c7b\u578b\u7684\u5b9a\u4e49\u66f4\u5c40\u9650\u4e5f\u66f4\u590d\u6742\u3002 \u6269\u5c55\u9605\u8bfb C++ \u4e2d\u7684\u805a\u5408\u6570\u636e\u7c7b\u578b\u5fc5\u987b\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\uff1a \u662f\u4e00\u4e2a \u7c7b\u7c7b\u578b (\u5305\u62ec struct, class \u6216 union) \u6216\u6570\u7ec4 (\u5305\u62ec\u5185\u7f6e\u6570\u7ec4\u6216 std::array )\uff1b \u6ca1\u6709\u79c1\u6709\u6216\u53d7\u4fdd\u62a4\u7684\u975e\u9759\u6001\u6570\u636e\u6210\u5458\uff1b \u6ca1\u6709\u7528\u6237\u58f0\u660e\u7684\u6216\u7ee7\u627f\u7684 \u6784\u9020\u51fd\u6570 \uff1b \u6ca1\u6709 \u57fa\u7c7b \uff1b \u6ca1\u6709 \u865a\u51fd\u6570 \uff1b \u5148\u628aC++\u805a\u5408\u7684\u7cbe\u786e\u5b9a\u4e49\u653e\u5728\u4e00\u8fb9\uff0c\u5728\u8fd9\u4e00\u70b9\u4e0a\u9700\u8981\u7406\u89e3\u7684\u91cd\u8981\u4e8b\u60c5\u662f\uff0c \u53ea\u6709\u6570\u636e\u6210\u5458\u7684\u7ed3\u6784(\u8fd9\u662f\u6211\u4eec\u5728\u8fd9\u4e9b\u8bfe\u7a0b\u4e2d\u521b\u5efa\u7684\u552f\u4e00\u7c7b\u578b\u7684\u7ed3\u6784)\u662f\u805a\u5408\u6570\u636e\u7c7b\u578b\u3002\u6570\u7ec4(\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u7ae0\u8ba8\u8bba)\u4e5f\u662f\u805a\u5408\u3002 \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 \u56e0\u4e3a\u666e\u901a\u53d8\u91cf\u53ea\u80fd\u4fdd\u5b58\u4e00\u4e2a\u503c\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u9700\u8981\u63d0\u4f9b\u4e00\u4e2a\u521d\u59cb\u5316\u503c\uff1a 1 int x { 5 }; \u4f46\u662f\uff0c\u7ed3\u6784\u4f53\u4e2d\u6709\u591a\u4e2a\u6210\u5458\uff1a 1 2 3 4 5 6 struct Employee { int id {}; int age {}; double wage {}; }; \u5f53\u5b9a\u4e49\u4e00\u4e2a\u5177\u6709\u7ed3\u6784\u7c7b\u578b\u7684\u5bf9\u8c61\u65f6\uff0c\u6211\u4eec\u80fd\u591f\u5728\u521d\u59cb\u5316\u7ed3\u6784\u4f53\u65f6\u540c\u65f6\u521d\u59cb\u5316\u591a\u4e2a\u6210\u5458\u7684\u65b9\u6cd5\uff1a 1 Employee joe ; // \u5982\u4f55\u521d\u59cb\u5316 joe.id, joe.age \u548c joe.wage? \u805a\u5408\u6570\u636e\u7c7b\u578b\u4f7f\u7528\u4e00\u79cd\u79f0\u4e3a \u805a\u5408\u521d\u59cb\u5316 \u7684\u521d\u59cb\u5316\u5f62\u5f0f\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u76f4\u63a5\u521d\u59cb\u5316\u805a\u5408\u7684\u6210\u5458\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u63d0\u4f9b\u4e00\u4e2a \u521d\u59cb\u5316\u503c\u5217\u8868 \u4f5c\u4e3a\u521d\u59cb\u5316\u503c\u3002\u521d\u59cb\u5316\u503c\u5217\u8868\u5c31\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u521d\u59cb\u5316\u503c\uff0c\u4ee5\u9017\u53f7\u5206\u5272\u7684\u503c\u5217\u8868\u3002 \u5c31\u50cf\u666e\u901a\u53d8\u91cf\u53ef\u4ee5\u88ab \u62f7\u8d1d\u521d\u59cb\u5316 \u3001 \u76f4\u63a5\u521d\u59cb\u5316 \u6216 \u5217\u8868\u521d\u59cb\u5316 \u4e00\u6837\uff0c \u805a\u5408\u521d\u59cb\u5316\u4e5f\u6709\u4e09\u79cd\u5f62\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee frank = { 1 , 32 , 60000.0 }; // \u62f7\u8d1d\u5217\u8868\u521d\u59cb\u5316\uff0c\u4f7f\u7528\u5927\u62ec\u53f7 Employee robert ( 3 , 45 , 62500.0 ); // \u4f7f\u7528\u5c0f\u62ec\u53f7\u7684\u76f4\u63a5\u521d\u59cb\u5316(C++20) Employee joe { 2 , 28 , 45000.0 }; // \u4f7f\u7528\u5927\u62ec\u53f7\u5217\u8868\u7684\u5217\u8868\u521d\u59cb\u5316\uff08\u63a8\u8350\uff09 return 0 ; } \u4e0a\u9762\u8fd9\u4e09\u79cd\u521d\u59cb\u5316\u5f62\u5f0f\u90fd\u4f1a\u8fdb\u884c \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \uff0c\u5373\u7ed3\u6784\u4f53\u6210\u5458\u4f1a\u6309\u7167\u5176\u58f0\u660e\u7684\u987a\u5e8f\u8fdb\u884c\u521d\u59cb\u5316\u3002\u56e0\u6b64\uff0c Employee joe { 2, 28, 45000.0 }; \u9996\u5148\u521d\u59cb\u5316 joe.id \u4e3a2\uff0c\u7136\u540e\u5c06 joe.age \u521d\u59cb\u5316\u4e3a28\uff0c\u6700\u540e\u5c06 joe.wage \u521d\u59cb\u5316\u4e3a 45000.0 \u3002 \u6700\u4f73\u5b9e\u8df5 \u63a8\u8350\u4f7f\u7528\u62ec\u53f7\u5217\u8868\u5f62\u5f0f\uff08\u975e\u62f7\u8d1d\uff09\u8fdb\u884c\u805a\u5408\u521d\u59cb\u5316\u3002 \u521d\u59cb\u5316\u503c\u5217\u8868\u4e2d\u7f3a\u5931\u7684\u503c \u5982\u679c\u805a\u5408\u6570\u636e\u7c7b\u578b\u88ab\u521d\u59cb\u5316\uff0c\u4f46\u521d\u59cb\u5316\u503c\u5217\u8868\u4e2d\u7684\u503c\u4e2a\u6570\u5c11\u4e8e\u6210\u5458\u4e2a\u6570\uff0c\u5219\u5269\u4f59\u7684\u6210\u5458\u4f1a\u88ab \u503c\u521d\u59cb\u5316 \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 2 , 28 }; // joe.wage \u88ab\u503c\u521d\u59cb\u5316\u4e3a 0.0 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c joe.id \u88ab\u521d\u59cb\u5316\u4e3a2\uff0c joe.age \u88ab\u521d\u59cb\u5316\u4e3a28\uff0c\u56e0\u4e3a joe.wage \u6ca1\u6709\u88ab\u663e\u5f0f\u6307\u5b9a\u521d\u59cb\u5316\u503c\uff0c\u5219\u88ab\u503c\u521d\u59cb\u5316\u4e3a0.0\u3002 \u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u7a7a\u7684\u521d\u59cb\u5316\u5217\u8868\u5bf9\u7ed3\u6784\u7684\u6240\u6709\u6210\u5458\u8fdb\u884c\u503c\u521d\u59cb\u5316\uff1a 1 Employee joe {}; // value-initialize all members \u8bd1\u8005\u6ce8 \u5173\u4e8e\u9ed8\u8ba4\u521d\u59cb\u5316\u8bf7\u53c2\u8003 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 \u3002 const \u7ed3\u6784\u4f53 \u7ed3\u6784\u4f53\u7c7b\u578b\u4e5f\u53ef\u4ee5\u662fconst\u7684\uff0c\u800c\u4e14\u548c\u666e\u901aconst\u53d8\u91cf\u4e00\u6837\u5fc5\u987b\u88ab\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 struct Rectangle { double length {}; double width {}; }; int main () { const Rectangle unit { 1.0 , 1.0 }; const Rectangle zero { }; // value-initialize all members return 0 ; } \u6307\u5b9a\u521d\u59cb\u5316\uff08 C++20 \uff09 \u4f7f\u7528\u5217\u8868\u521d\u59cb\u5316\u7ed3\u6784\u4f53\u65f6\uff0c\u662f\u6309\u7167\u6210\u5458\u58f0\u660e\u7684\u987a\u5e8f\u521d\u59cb\u5316\u7684\u3002 1 2 3 4 5 6 7 8 9 10 struct Foo { int a {}; int c {}; }; int main () { Foo f { 1 , 3 }; // f.a = 1, f.c = 3 } \u90a3\u4e48\u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u5982\u679c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u6210\u5458\uff0c\u800c\u4e14\u5e76\u6ca1\u6709\u628a\u5b83\u58f0\u660e\u6210\u6700\u540e\u4e00\u4e2a\uff1a 1 2 3 4 5 6 7 8 9 10 11 struct Foo { int a {}; int b {}; // just added int c {}; }; int main () { Foo f { 1 , 3 }; // now, f.a = 1, f.b = 3, f.c = 0 } \u90a3\u4e48\u4f60\u6240\u6709\u4f7f\u7528\u521d\u59cb\u5316\u503c\u5217\u8868\u7684\u5730\u65b9\uff0c\u90fd\u8981\u5bf9\u5e94\u7684\u79fb\u52a8\u3002\u66f4\u7cdf\u7684\u662f\uff0c\u7f16\u8bd1\u5668\u5e76\u4e0d\u4f1a\u68c0\u6d4b\u5230\u8be5\u95ee\u9898\uff0c\u6bd5\u7adf\u5728\u8bed\u6cd5\u5c42\u9762\u5b83\u662f\u6709\u6548\u7684\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cC++20 \u4e3a\u7ed3\u6784\u4f53\u6210\u5458\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b0\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u2014\u2014 \u6307\u5b9a\u521d\u59cb\u5316 \u3002\u6307\u5b9a\u521d\u59cb\u5316\u5141\u8bb8\u6211\u4eec\u663e\u793a\u5730\u5c06\u521d\u59cb\u5316\u503c\u6620\u5c04\u5230\u6240\u9700\u521d\u59cb\u5316\u7684\u6210\u5458\u3002\u4f46\u662f\uff0c\u5217\u8868\u7684\u987a\u5e8f\u4ecd\u7136\u8981\u6c42\u6309\u7167\u6210\u5458\u5b9a\u4e49\u7684\u987a\u5e8f\u5b9a\u4e49\uff0c\u5426\u5219\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002\u6ca1\u6709\u5728\u5217\u8868\u4e2d\u51fa\u73b0\u7684\u6210\u5458\u4ecd\u7136\u8fdb\u884c\u503c\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct Foo { int a { }; int b { }; int c { }; }; int main () { Foo f1 { . a { 1 }, . c { 3 } }; // ok: f.a = 1, f.b = 0 (\u672a\u51fa\u73b0\uff0c\u503c\u521d\u59cb\u5316), f.c = 3 Foo f2 { . b { 2 }, . a { 1 } }; // \u9519\u8bef: \u521d\u59cb\u5316\u987a\u5e8f\u548c\u6210\u5458\u58f0\u660e\u987a\u5e8f\u4e0d\u5339\u914d return 0 ; } \u6307\u5b9a\u521d\u59cb\u5316\u5f88\u4e0d\u9519\uff0c\u56e0\u4e3a\u5b83\u4eec\u63d0\u4f9b\u4e86\u67d0\u79cd\u7a0b\u5ea6\u7684\u81ea\u6ce8\u91ca\uff0c\u5e76\u6709\u52a9\u4e8e\u786e\u4fdd\u4f60\u4e0d\u4f1a\u65e0\u610f\u4e2d\u6df7\u6dc6\u521d\u59cb\u5316\u503c\u7684\u987a\u5e8f\u3002\u4f46\u662f\uff0c\u6307\u5b9a\u521d\u59cb\u5316\u5f0f\u4e5f\u4f1a\u4f7f\u521d\u59cb\u5316\u5f0f\u5217\u8868\u53d8\u5f97\u975e\u5e38\u6df7\u4e71\uff0c\u56e0\u6b64\u6211\u4eec\u76ee\u524d\u4e0d\u5efa\u8bae\u5c06\u5176\u4f5c\u4e3a\u6700\u4f73\u5b9e\u8df5\u4f7f\u7528\u3002 \u53e6\u5916\uff0c\u7531\u4e8e\u6ca1\u6709\u5f3a\u5236\u89c4\u5b9a\u5728\u521d\u59cb\u5316\u805a\u5408\u7684\u6240\u6709\u5730\u65b9\u90fd\u4e00\u81f4\u4f7f\u7528\u6307\u5b9a\u521d\u59cb\u5316\u5f0f\uff0c\u56e0\u6b64\u907f\u514d\u5411\u73b0\u6709\u805a\u5408\u5b9a\u4e49\u7684\u4e2d\u95f4\u6dfb\u52a0\u65b0\u6210\u5458\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\uff0c\u4ee5\u907f\u514d\u521d\u59cb\u5316\u5f0f\u8f6c\u79fb\u7684\u98ce\u9669\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u5411\u805a\u5408\u4e2d\u6dfb\u52a0\u65b0\u6210\u5458\u65f6\uff0c\u6700\u5b89\u5168\u7684\u505a\u6cd5\u662f\u5c06\u5176\u6dfb\u52a0\u5230\u5b9a\u4e49\u5217\u8868\u7684\u5e95\u90e8\uff0c\u8fd9\u6837\u5176\u4ed6\u6210\u5458\u7684\u521d\u59cb\u5316\u5f0f\u5c31\u4e0d\u9700\u8981\u8c03\u6574\u987a\u5e8f\u3002 \u4f7f\u7528\u521d\u59cb\u5316\u503c\u5217\u8868\u8fdb\u884c\u8d4b\u503c \u5982\u4e0a\u4e00\u8bfe\u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u7ed3\u6784\u4f53\u7684\u6210\u5458\u5355\u72ec\u8d4b\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 32 , 60000.0 }; joe . age = 33 ; // Joe had a birthday joe . wage = 66000.0 ; // and got a raise return 0 ; } \u5982\u679c\u8981\u64cd\u4f5c\u7684\u662f\u4e2a\u522b\u6210\u5458\uff0c\u90a3\u662f\u6ca1\u6709\u95ee\u9898\u7684\u3002\u4f46\u662f\u4e00\u65e6\u8981\u64cd\u4f5c\u5f88\u591a\u6210\u5458\u65f6\uff0c\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u9ebb\u70e6\u3002\u7c7b\u4f3c\u4e8e\u53ef\u4ee5\u7528 \u521d\u59cb\u5316\u503c\u5217\u8868 \u521d\u59cb\u5316\u7ed3\u6784\u4f53\u4e00\u6837\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u521d\u59cb\u5316\u503c\u5217\u8868\u8fdb\u884c\u8d4b\u503c\uff08\u8fdb\u884c \u6210\u5458\u4f9d\u6b21\u8d4b\u503c \uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 32 , 60000.0 }; joe = { joe . id , 33 , 66000.0 }; // Joe had a birthday and got a raise return 0 ; } \u6ce8\u610f\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u5e0c\u671b\u6539\u53d8 joe.id \u7684\u503c\uff0c\u6240\u4ee5\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 joe.id \u4f5c\u4e3a\u5360\u4f4d\u7b26\uff0c\u5c06 joe.id \u8d4b\u503c\u7ed9 joe.id \u3002\u867d\u7136\u4e0d\u592a\u4f18\u96c5\u3002 \u4f7f\u7528\u6307\u5b9a\u521d\u59cb\u5316\u8d4b\u503c\uff08C++20\uff09 \u6307\u5b9a\u521d\u59cb\u5316\u4e5f\u53ef\u4ee5\u7528\u6765\u8d4b\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 32 , 60000.0 }; joe = { . id = joe . id , . age = 33 , . wage = 66000.0 }; // Joe had a birthday and got a raise return 0 ; } \u4efb\u4f55\u6ca1\u6709\u5728\u8fd9\u79cd\u8d4b\u503c\u4e2d\u6307\u5b9a\u7684\u6210\u5458\u90fd\u5c06\u88ab\u8d4b\u503c\u7ed9\u7528\u4e8e \u503c\u521d\u59cb\u5316 \u7684\u503c\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4e0d\u4e3a joe.id \u63d0\u4f9b\u6307\u5b9a\u521d\u59cb\u5316\u503c\uff0c\u5219 joe.id \u4f1a\u88ab\u8d4b\u503c\u4e3a0\u3002","title":"10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316"},{"location":"10-6-struct-aggregate-initialization/#_1","text":"\u548c\u666e\u901a\u53d8\u91cf\u7c7b\u4f3c\uff0c\u6570\u636e\u6210\u5458\u5e76\u4e0d\u4f1a\u88ab\u9ed8\u8ba4\u521d\u59cb\u5316\u3002\u8003\u8651\u4e0b\u9762\u7684\u7ed3\u6784\u4f53\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include struct Employee { int id ; // note: no initializer here int age ; double wage ; }; int main () { Employee joe ; // note: no initializer here either std :: cout << joe . id << '\\n' ; return 0 ; } \u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u63d0\u4f9b\u4efb\u4f55\u521d\u59cb\u5316\u503c\uff0c\u6240\u4ee5\u5f53\u5b9e\u4f8b\u5316 joe \u65f6\uff0c joe.id \u3001 joe.age \u548c joe.wage \u5904\u4e8e\u672a\u521d\u59cb\u5316\u72b6\u6001\u3002\u5f53\u6211\u4eec\u8bd5\u56fe\u6253\u5370 joe.id \u7684\u503c\u65f6\uff0c\u5c06\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4f46\u662f\uff0c\u5728\u5411\u4f60\u5c55\u793a\u5982\u4f55\u521d\u59cb\u5316\u7ed3\u6784\u4e4b\u524d\uff0c\u8ba9\u6211\u4eec\u5148\u7ed5\u8def\u8d70\u4e00\u5c0f\u6bb5\u8def\u3002","title":"\u6570\u636e\u6210\u5458\u9ed8\u8ba4\u4e0d\u4f1a\u88ab\u521d\u59cb\u5316"},{"location":"10-6-struct-aggregate-initialization/#_2","text":"\u5728\u7f16\u7a0b\u9886\u57df\uff0c \u805a\u5408\u6570\u636e\u7c7b\u578b \u6307\u7684\u662f==\u4efb\u4f55\u5305\u542b\u591a\u4e2a\u6570\u636e\u6210\u5458\u7684\u7c7b\u578b==\u3002\u67d0\u4e9b\u7c7b\u578b\u7684\u805a\u5408\u7c7b\u578b\u5141\u8bb8\u6210\u5458\u5177\u6709\u4e0d\u540c\u7684\u7c7b\u578b(\u4f8b\u5982\u7ed3\u6784)\uff0c\u800c\u5176\u4ed6\u7c7b\u578b\u7684\u805a\u5408\u8981\u6c42\u6240\u6709\u6210\u5458\u5fc5\u987b\u5177\u6709\u5355\u4e00\u7c7b\u578b(\u4f8b\u5982\u6570\u7ec4)\u3002 \u5728C++\u4e2d\uff0c\u805a\u5408\u6570\u636e\u7c7b\u578b\u7684\u5b9a\u4e49\u66f4\u5c40\u9650\u4e5f\u66f4\u590d\u6742\u3002 \u6269\u5c55\u9605\u8bfb C++ \u4e2d\u7684\u805a\u5408\u6570\u636e\u7c7b\u578b\u5fc5\u987b\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\uff1a \u662f\u4e00\u4e2a \u7c7b\u7c7b\u578b (\u5305\u62ec struct, class \u6216 union) \u6216\u6570\u7ec4 (\u5305\u62ec\u5185\u7f6e\u6570\u7ec4\u6216 std::array )\uff1b \u6ca1\u6709\u79c1\u6709\u6216\u53d7\u4fdd\u62a4\u7684\u975e\u9759\u6001\u6570\u636e\u6210\u5458\uff1b \u6ca1\u6709\u7528\u6237\u58f0\u660e\u7684\u6216\u7ee7\u627f\u7684 \u6784\u9020\u51fd\u6570 \uff1b \u6ca1\u6709 \u57fa\u7c7b \uff1b \u6ca1\u6709 \u865a\u51fd\u6570 \uff1b \u5148\u628aC++\u805a\u5408\u7684\u7cbe\u786e\u5b9a\u4e49\u653e\u5728\u4e00\u8fb9\uff0c\u5728\u8fd9\u4e00\u70b9\u4e0a\u9700\u8981\u7406\u89e3\u7684\u91cd\u8981\u4e8b\u60c5\u662f\uff0c \u53ea\u6709\u6570\u636e\u6210\u5458\u7684\u7ed3\u6784(\u8fd9\u662f\u6211\u4eec\u5728\u8fd9\u4e9b\u8bfe\u7a0b\u4e2d\u521b\u5efa\u7684\u552f\u4e00\u7c7b\u578b\u7684\u7ed3\u6784)\u662f\u805a\u5408\u6570\u636e\u7c7b\u578b\u3002\u6570\u7ec4(\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u7ae0\u8ba8\u8bba)\u4e5f\u662f\u805a\u5408\u3002","title":"\u4ec0\u4e48\u662f\u805a\u5408\uff1f"},{"location":"10-6-struct-aggregate-initialization/#_3","text":"\u56e0\u4e3a\u666e\u901a\u53d8\u91cf\u53ea\u80fd\u4fdd\u5b58\u4e00\u4e2a\u503c\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u9700\u8981\u63d0\u4f9b\u4e00\u4e2a\u521d\u59cb\u5316\u503c\uff1a 1 int x { 5 }; \u4f46\u662f\uff0c\u7ed3\u6784\u4f53\u4e2d\u6709\u591a\u4e2a\u6210\u5458\uff1a 1 2 3 4 5 6 struct Employee { int id {}; int age {}; double wage {}; }; \u5f53\u5b9a\u4e49\u4e00\u4e2a\u5177\u6709\u7ed3\u6784\u7c7b\u578b\u7684\u5bf9\u8c61\u65f6\uff0c\u6211\u4eec\u80fd\u591f\u5728\u521d\u59cb\u5316\u7ed3\u6784\u4f53\u65f6\u540c\u65f6\u521d\u59cb\u5316\u591a\u4e2a\u6210\u5458\u7684\u65b9\u6cd5\uff1a 1 Employee joe ; // \u5982\u4f55\u521d\u59cb\u5316 joe.id, joe.age \u548c joe.wage? \u805a\u5408\u6570\u636e\u7c7b\u578b\u4f7f\u7528\u4e00\u79cd\u79f0\u4e3a \u805a\u5408\u521d\u59cb\u5316 \u7684\u521d\u59cb\u5316\u5f62\u5f0f\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u76f4\u63a5\u521d\u59cb\u5316\u805a\u5408\u7684\u6210\u5458\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u63d0\u4f9b\u4e00\u4e2a \u521d\u59cb\u5316\u503c\u5217\u8868 \u4f5c\u4e3a\u521d\u59cb\u5316\u503c\u3002\u521d\u59cb\u5316\u503c\u5217\u8868\u5c31\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u521d\u59cb\u5316\u503c\uff0c\u4ee5\u9017\u53f7\u5206\u5272\u7684\u503c\u5217\u8868\u3002 \u5c31\u50cf\u666e\u901a\u53d8\u91cf\u53ef\u4ee5\u88ab \u62f7\u8d1d\u521d\u59cb\u5316 \u3001 \u76f4\u63a5\u521d\u59cb\u5316 \u6216 \u5217\u8868\u521d\u59cb\u5316 \u4e00\u6837\uff0c \u805a\u5408\u521d\u59cb\u5316\u4e5f\u6709\u4e09\u79cd\u5f62\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee frank = { 1 , 32 , 60000.0 }; // \u62f7\u8d1d\u5217\u8868\u521d\u59cb\u5316\uff0c\u4f7f\u7528\u5927\u62ec\u53f7 Employee robert ( 3 , 45 , 62500.0 ); // \u4f7f\u7528\u5c0f\u62ec\u53f7\u7684\u76f4\u63a5\u521d\u59cb\u5316(C++20) Employee joe { 2 , 28 , 45000.0 }; // \u4f7f\u7528\u5927\u62ec\u53f7\u5217\u8868\u7684\u5217\u8868\u521d\u59cb\u5316\uff08\u63a8\u8350\uff09 return 0 ; } \u4e0a\u9762\u8fd9\u4e09\u79cd\u521d\u59cb\u5316\u5f62\u5f0f\u90fd\u4f1a\u8fdb\u884c \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \uff0c\u5373\u7ed3\u6784\u4f53\u6210\u5458\u4f1a\u6309\u7167\u5176\u58f0\u660e\u7684\u987a\u5e8f\u8fdb\u884c\u521d\u59cb\u5316\u3002\u56e0\u6b64\uff0c Employee joe { 2, 28, 45000.0 }; \u9996\u5148\u521d\u59cb\u5316 joe.id \u4e3a2\uff0c\u7136\u540e\u5c06 joe.age \u521d\u59cb\u5316\u4e3a28\uff0c\u6700\u540e\u5c06 joe.wage \u521d\u59cb\u5316\u4e3a 45000.0 \u3002 \u6700\u4f73\u5b9e\u8df5 \u63a8\u8350\u4f7f\u7528\u62ec\u53f7\u5217\u8868\u5f62\u5f0f\uff08\u975e\u62f7\u8d1d\uff09\u8fdb\u884c\u805a\u5408\u521d\u59cb\u5316\u3002","title":"\u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316"},{"location":"10-6-struct-aggregate-initialization/#_4","text":"\u5982\u679c\u805a\u5408\u6570\u636e\u7c7b\u578b\u88ab\u521d\u59cb\u5316\uff0c\u4f46\u521d\u59cb\u5316\u503c\u5217\u8868\u4e2d\u7684\u503c\u4e2a\u6570\u5c11\u4e8e\u6210\u5458\u4e2a\u6570\uff0c\u5219\u5269\u4f59\u7684\u6210\u5458\u4f1a\u88ab \u503c\u521d\u59cb\u5316 \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 2 , 28 }; // joe.wage \u88ab\u503c\u521d\u59cb\u5316\u4e3a 0.0 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c joe.id \u88ab\u521d\u59cb\u5316\u4e3a2\uff0c joe.age \u88ab\u521d\u59cb\u5316\u4e3a28\uff0c\u56e0\u4e3a joe.wage \u6ca1\u6709\u88ab\u663e\u5f0f\u6307\u5b9a\u521d\u59cb\u5316\u503c\uff0c\u5219\u88ab\u503c\u521d\u59cb\u5316\u4e3a0.0\u3002 \u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u7a7a\u7684\u521d\u59cb\u5316\u5217\u8868\u5bf9\u7ed3\u6784\u7684\u6240\u6709\u6210\u5458\u8fdb\u884c\u503c\u521d\u59cb\u5316\uff1a 1 Employee joe {}; // value-initialize all members \u8bd1\u8005\u6ce8 \u5173\u4e8e\u9ed8\u8ba4\u521d\u59cb\u5316\u8bf7\u53c2\u8003 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 \u3002","title":"\u521d\u59cb\u5316\u503c\u5217\u8868\u4e2d\u7f3a\u5931\u7684\u503c"},{"location":"10-6-struct-aggregate-initialization/#const","text":"\u7ed3\u6784\u4f53\u7c7b\u578b\u4e5f\u53ef\u4ee5\u662fconst\u7684\uff0c\u800c\u4e14\u548c\u666e\u901aconst\u53d8\u91cf\u4e00\u6837\u5fc5\u987b\u88ab\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 struct Rectangle { double length {}; double width {}; }; int main () { const Rectangle unit { 1.0 , 1.0 }; const Rectangle zero { }; // value-initialize all members return 0 ; }","title":"const \u7ed3\u6784\u4f53"},{"location":"10-6-struct-aggregate-initialization/#c20","text":"\u4f7f\u7528\u5217\u8868\u521d\u59cb\u5316\u7ed3\u6784\u4f53\u65f6\uff0c\u662f\u6309\u7167\u6210\u5458\u58f0\u660e\u7684\u987a\u5e8f\u521d\u59cb\u5316\u7684\u3002 1 2 3 4 5 6 7 8 9 10 struct Foo { int a {}; int c {}; }; int main () { Foo f { 1 , 3 }; // f.a = 1, f.c = 3 } \u90a3\u4e48\u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u5982\u679c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u6210\u5458\uff0c\u800c\u4e14\u5e76\u6ca1\u6709\u628a\u5b83\u58f0\u660e\u6210\u6700\u540e\u4e00\u4e2a\uff1a 1 2 3 4 5 6 7 8 9 10 11 struct Foo { int a {}; int b {}; // just added int c {}; }; int main () { Foo f { 1 , 3 }; // now, f.a = 1, f.b = 3, f.c = 0 } \u90a3\u4e48\u4f60\u6240\u6709\u4f7f\u7528\u521d\u59cb\u5316\u503c\u5217\u8868\u7684\u5730\u65b9\uff0c\u90fd\u8981\u5bf9\u5e94\u7684\u79fb\u52a8\u3002\u66f4\u7cdf\u7684\u662f\uff0c\u7f16\u8bd1\u5668\u5e76\u4e0d\u4f1a\u68c0\u6d4b\u5230\u8be5\u95ee\u9898\uff0c\u6bd5\u7adf\u5728\u8bed\u6cd5\u5c42\u9762\u5b83\u662f\u6709\u6548\u7684\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cC++20 \u4e3a\u7ed3\u6784\u4f53\u6210\u5458\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b0\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u2014\u2014 \u6307\u5b9a\u521d\u59cb\u5316 \u3002\u6307\u5b9a\u521d\u59cb\u5316\u5141\u8bb8\u6211\u4eec\u663e\u793a\u5730\u5c06\u521d\u59cb\u5316\u503c\u6620\u5c04\u5230\u6240\u9700\u521d\u59cb\u5316\u7684\u6210\u5458\u3002\u4f46\u662f\uff0c\u5217\u8868\u7684\u987a\u5e8f\u4ecd\u7136\u8981\u6c42\u6309\u7167\u6210\u5458\u5b9a\u4e49\u7684\u987a\u5e8f\u5b9a\u4e49\uff0c\u5426\u5219\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002\u6ca1\u6709\u5728\u5217\u8868\u4e2d\u51fa\u73b0\u7684\u6210\u5458\u4ecd\u7136\u8fdb\u884c\u503c\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct Foo { int a { }; int b { }; int c { }; }; int main () { Foo f1 { . a { 1 }, . c { 3 } }; // ok: f.a = 1, f.b = 0 (\u672a\u51fa\u73b0\uff0c\u503c\u521d\u59cb\u5316), f.c = 3 Foo f2 { . b { 2 }, . a { 1 } }; // \u9519\u8bef: \u521d\u59cb\u5316\u987a\u5e8f\u548c\u6210\u5458\u58f0\u660e\u987a\u5e8f\u4e0d\u5339\u914d return 0 ; } \u6307\u5b9a\u521d\u59cb\u5316\u5f88\u4e0d\u9519\uff0c\u56e0\u4e3a\u5b83\u4eec\u63d0\u4f9b\u4e86\u67d0\u79cd\u7a0b\u5ea6\u7684\u81ea\u6ce8\u91ca\uff0c\u5e76\u6709\u52a9\u4e8e\u786e\u4fdd\u4f60\u4e0d\u4f1a\u65e0\u610f\u4e2d\u6df7\u6dc6\u521d\u59cb\u5316\u503c\u7684\u987a\u5e8f\u3002\u4f46\u662f\uff0c\u6307\u5b9a\u521d\u59cb\u5316\u5f0f\u4e5f\u4f1a\u4f7f\u521d\u59cb\u5316\u5f0f\u5217\u8868\u53d8\u5f97\u975e\u5e38\u6df7\u4e71\uff0c\u56e0\u6b64\u6211\u4eec\u76ee\u524d\u4e0d\u5efa\u8bae\u5c06\u5176\u4f5c\u4e3a\u6700\u4f73\u5b9e\u8df5\u4f7f\u7528\u3002 \u53e6\u5916\uff0c\u7531\u4e8e\u6ca1\u6709\u5f3a\u5236\u89c4\u5b9a\u5728\u521d\u59cb\u5316\u805a\u5408\u7684\u6240\u6709\u5730\u65b9\u90fd\u4e00\u81f4\u4f7f\u7528\u6307\u5b9a\u521d\u59cb\u5316\u5f0f\uff0c\u56e0\u6b64\u907f\u514d\u5411\u73b0\u6709\u805a\u5408\u5b9a\u4e49\u7684\u4e2d\u95f4\u6dfb\u52a0\u65b0\u6210\u5458\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\uff0c\u4ee5\u907f\u514d\u521d\u59cb\u5316\u5f0f\u8f6c\u79fb\u7684\u98ce\u9669\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u5411\u805a\u5408\u4e2d\u6dfb\u52a0\u65b0\u6210\u5458\u65f6\uff0c\u6700\u5b89\u5168\u7684\u505a\u6cd5\u662f\u5c06\u5176\u6dfb\u52a0\u5230\u5b9a\u4e49\u5217\u8868\u7684\u5e95\u90e8\uff0c\u8fd9\u6837\u5176\u4ed6\u6210\u5458\u7684\u521d\u59cb\u5316\u5f0f\u5c31\u4e0d\u9700\u8981\u8c03\u6574\u987a\u5e8f\u3002","title":"\u6307\u5b9a\u521d\u59cb\u5316\uff08\u00a0C++20 \uff09"},{"location":"10-6-struct-aggregate-initialization/#_5","text":"\u5982\u4e0a\u4e00\u8bfe\u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u7ed3\u6784\u4f53\u7684\u6210\u5458\u5355\u72ec\u8d4b\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 32 , 60000.0 }; joe . age = 33 ; // Joe had a birthday joe . wage = 66000.0 ; // and got a raise return 0 ; } \u5982\u679c\u8981\u64cd\u4f5c\u7684\u662f\u4e2a\u522b\u6210\u5458\uff0c\u90a3\u662f\u6ca1\u6709\u95ee\u9898\u7684\u3002\u4f46\u662f\u4e00\u65e6\u8981\u64cd\u4f5c\u5f88\u591a\u6210\u5458\u65f6\uff0c\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u9ebb\u70e6\u3002\u7c7b\u4f3c\u4e8e\u53ef\u4ee5\u7528 \u521d\u59cb\u5316\u503c\u5217\u8868 \u521d\u59cb\u5316\u7ed3\u6784\u4f53\u4e00\u6837\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u521d\u59cb\u5316\u503c\u5217\u8868\u8fdb\u884c\u8d4b\u503c\uff08\u8fdb\u884c \u6210\u5458\u4f9d\u6b21\u8d4b\u503c \uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 32 , 60000.0 }; joe = { joe . id , 33 , 66000.0 }; // Joe had a birthday and got a raise return 0 ; } \u6ce8\u610f\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u5e0c\u671b\u6539\u53d8 joe.id \u7684\u503c\uff0c\u6240\u4ee5\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 joe.id \u4f5c\u4e3a\u5360\u4f4d\u7b26\uff0c\u5c06 joe.id \u8d4b\u503c\u7ed9 joe.id \u3002\u867d\u7136\u4e0d\u592a\u4f18\u96c5\u3002","title":"\u4f7f\u7528\u521d\u59cb\u5316\u503c\u5217\u8868\u8fdb\u884c\u8d4b\u503c"},{"location":"10-6-struct-aggregate-initialization/#c20_1","text":"\u6307\u5b9a\u521d\u59cb\u5316\u4e5f\u53ef\u4ee5\u7528\u6765\u8d4b\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 32 , 60000.0 }; joe = { . id = joe . id , . age = 33 , . wage = 66000.0 }; // Joe had a birthday and got a raise return 0 ; } \u4efb\u4f55\u6ca1\u6709\u5728\u8fd9\u79cd\u8d4b\u503c\u4e2d\u6307\u5b9a\u7684\u6210\u5458\u90fd\u5c06\u88ab\u8d4b\u503c\u7ed9\u7528\u4e8e \u503c\u521d\u59cb\u5316 \u7684\u503c\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4e0d\u4e3a joe.id \u63d0\u4f9b\u6307\u5b9a\u521d\u59cb\u5316\u503c\uff0c\u5219 joe.id \u4f1a\u88ab\u8d4b\u503c\u4e3a0\u3002","title":"\u4f7f\u7528\u6307\u5b9a\u521d\u59cb\u5316\u8d4b\u503c\uff08C++20\uff09"},{"location":"10-7-default-member-initialization/","tags":["struct","initialization"],"text":"Key Takeaway \u5b9e\u4f8b\u5316\u805a\u5408\u7c7b\u578b\u5bf9\u8c61\u65f6\u5982\u679c\u6ca1\u6709\u63d0\u4f9b\u521d\u59cb\u5316\u503c\uff0c\u5219\u8868\u793a\u6ca1\u6709\u521d\u59cb\u5316\u8be5\u805a\u5408\u7c7b\u578b\u3002\u6b64\u65f6\u5b83\u7684\u6210\u5458\u4f1a\u8fdb\u884c\u9ed8\u8ba4\u521d\u59cb\u5316\u3002\u6ca1\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\u7684\u6210\u5458\u5219\u4e3a\u672a\u521d\u59cb\u5316\u72b6\u6001\u3002 \u521d\u59cb\u5316\u805a\u5408\u7c7b\u578b\u5bf9\u8c61\u65f6\uff0c\u5982\u679c\u521d\u59cb\u5316\u503c\u5c11\u4e8e\u6210\u5458\uff0c\u5219\u5269\u4f59\u7684\u6210\u5458\u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\uff0c\u5982\u679c\u6ca1\u6709\u63d0\u4f9b\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u5219\u503c\u521d\u59cb\u5316\u4e3a0. \u5728\u5b9a\u4e49\u7ed3\u6784\u4f53\uff08\u6216\u7c7b\uff09\u7c7b\u578b\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u6210\u5458\u90fd\u63d0\u4f9b\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\u4f5c\u4e3a\u5176\u5b9a\u4e49\u7684\u4e00\u90e8\u5206\u3002 \u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \uff0c\u800c\u8fd9\u4e2a\u521d\u59cb\u5316\u503c\u79f0\u4e3a \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u503c \u3002 \u4e3e\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 struct Something { int x ; // no initialization value (bad) int y {}; // value-initialized by default int z { 2 }; // explicit default value }; int main () { Something s1 ; // s1.x is uninitialized, s1.y is 0, and s1.z is 2 return 0 ; } \u5728\u4e0a\u9762 Something \u7684\u5b9a\u4e49\u4e2d\uff0c x \u5e76\u6ca1\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\u3001 y \u8fdb\u884c\u9ed8\u8ba4\u7684\u503c\u521d\u59cb\u5316\u800c z \u5219\u88ab\u521d\u59cb\u5316\u4e3a2\u3002 \u5982\u679c\u5728 \u5b9e\u4f8b\u5316 Something \u7c7b\u578b\u7684\u5bf9\u8c61\u65f6\u6ca1\u6709\u63d0\u4f9b\u663e\u5f0f\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f1a\u4f7f\u7528\u6210\u5458\u7684\u8fd9\u4e9b\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c\u3002 s1 \u5bf9\u8c61\u6ca1\u6709\u521d\u59cb\u5316\u503c\uff0c\u6240\u4ee5 s1 \u7684\u6210\u5458\u4eec\u4f1a\u88ab\u521d\u59cb\u5316\u4e3a\u5b83\u4eec\u7684\u9ed8\u8ba4\u503c\u3002 s1.x \u5219\u56e0\u4e3a\u6ca1\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\u800c\u4fdd\u6301\u672a\u521d\u59cb\u5316\u72b6\u6001\u3002 s1.y \u8fdb\u884c\u9ed8\u8ba4\u503c\u521d\u59cb\u5316\uff0c\u5f97\u5230\u521d\u503c0\uff0c\u800c s1.z \u5219\u88ab\u521d\u59cb\u5316\u4e3a 2 \u3002 \u6ce8\u610f\uff0c\u5373\u4fbf\u6211\u4eec\u5e76\u6ca1\u6709\u4e3a s1.z \u63d0\u4f9b\u663e\u5f0f\u5730\u521d\u59cb\u5316\u503c\uff0c\u5b83\u4ecd\u7136\u88ab\u521d\u59cb\u5316\u4e3a\u4e00\u4e2a\u975e\u96f6\u503c\uff0c\u56e0\u4e3a\u6211\u4eec\u4e3a\u5176\u63d0\u4f9b\u4e86\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c\u3002 \u5173\u952e\u4fe1\u606f \u4f7f\u7528\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u5f0f(\u6216\u6211\u4eec\u5c06\u5728\u540e\u9762\u4ecb\u7ecd\u7684\u5176\u4ed6\u673a\u5236)\uff0c\u5373\u4f7f\u6ca1\u6709\u63d0\u4f9b\u663e\u5f0f\u521d\u59cb\u5316\u5f0f\uff0c\u7ed3\u6784\u548c\u7c7b\u4e5f\u53ef\u4ee5\u81ea\u6211\u521d\u59cb\u5316\uff01 \u663e\u5f0f\u521d\u59cb\u5316\u503c\u4f18\u5148\u7ea7\u9ad8\u4e8e\u9ed8\u8ba4\u521d\u59cb\u5316\u503c \u5217\u8868\u521d\u59cb\u5316\u5f0f\u4e2d\u7684\u663e\u5f0f\u503c\u603b\u662f\u4f18\u5148\u4e8e\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 struct Something { int x ; // no default initialization value (bad) int y {}; // value-initialized by default int z { 2 }; // explicit default value }; int main () { Something s2 { 5 , 6 , 7 }; // use explicit initializers for s2.x, s2.y, and s2.z (no default values are used) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cs2 \u7684\u6bcf\u4e2a\u6210\u5458\u90fd\u5177\u6709\u663e\u5f0f\u5730\u521d\u59cb\u5316\u503c\uff0c\u6240\u4ee5\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u5e76\u4e0d\u751f\u6548\u3002 s2.x \u3001 s2.y \u548c s2.z \u5206\u522b\u88ab\u521d\u59cb\u5316\u4e3a 5 \u3001 6 \u548c 7 \u3002 \u5b58\u5728\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\u4e14\u521d\u59cb\u5316\u5217\u8868\u6ca1\u6709\u63d0\u4f9b\u521d\u59cb\u5316\u503c\u7684\u60c5\u51b5 \u5728\u4e0a\u4e00\u8282\u8bfe\u4e2d\uff08 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 \uff09\u6211\u4eec\u63d0\u5230\uff0c \u5982\u679c\u805a\u5408\u6570\u636e\u7ed3\u6784\u88ab\u521d\u59cb\u5316\u4e86\uff0c\u4f46\u521d\u59cb\u5316\u503c\u7684\u4e2a\u6570\u6bd4\u5b9e\u9645\u6210\u5458\u8981\u5c11\uff0c\u5219\u5269\u4e0b\u7684\u6210\u5458\u53d8\u91cf\u5219\u4f1a\u8fdb\u884c\u503c\u521d\u59cb\u5316\u3002\u4f46\u662f\uff0c\u5982\u679c\u8be5\u6210\u5458\u5177\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f1a\u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 struct Something { int x ; // no default initialization value (bad) int y {}; // value-initialized by default int z { 2 }; // explicit default value }; int main () { Something s3 {}; // value initialize s3.x, use default values for s3.y and s3.z return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c s3 \u7684\u521d\u59cb\u5316\u503c\u5217\u8868\u662f\u4e00\u4e2a\u7a7a\u5217\u8868\uff0c\u5373\u6ca1\u6709\u63d0\u4f9b\u4efb\u4f55\u7684\u663e\u5f0f\u521d\u59cb\u5316\u503c\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u63d0\u4f9b\u4e86\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f7f\u7528\u8be5\u503c\uff0c\u5426\u5219\u5219\u4f7f\u7528 \u503c\u521d\u59cb\u5316 \u3002\u56e0\u6b64 s3.x (\u6ca1\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c) \u4f1a\u88ab\u503c\u521d\u59cb\u5316\u4e3a0\u3002 s3.y \u7684\u503c\u88ab\u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a0\uff0c\u800c s3.z \u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a 2\u3002 \u590d\u4e60\uff1a\u521d\u59cb\u5316\u7684\u53ef\u80fd\u6027 \u5982\u679c\u805a\u5408\u5bf9\u8c61\u5b9a\u4e49\u65f6\u6709 \u521d\u59cb\u5316\u503c\u5217\u8868 \uff08\u5b9e\u4f8b\u5316\u7684\u540c\u65f6\u521d\u59cb\u5316\uff09\uff1a \u5982\u679c\u5177\u6709\u663e\u5f0f\u7684\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f7f\u7528\u8be5\u503c\uff1b \u5982\u679c\u6709\u7f3a\u5931\u7684\u521d\u59cb\u5316\u503c\uff0c\u4f46\u5b58\u5728\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c\u8fdb\u884c \u9ed8\u8ba4\u521d\u59cb\u5316 \uff1b \u5982\u679c\u6709\u7f3a\u5931\u7684\u521d\u59cb\u5316\u503c\uff0c\u4f46\u662f\u6ca1\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f7f\u7528 \u503c\u521d\u59cb\u5316 \u3002 \u5982\u679c\u805a\u5408\u5bf9\u8c61\u5b9a\u4e49\u65f6\u6ca1\u6709\u521d\u59cb\u5316\u503c\u5217\u8868\uff08\u5b9e\u4f8b\u5316\u65f6\u672a\u8fdb\u884c\u521d\u59cb\u5316\uff09\uff1a \u5982\u679c\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c\u8fdb\u884c\u9ed8\u8ba4\u521d\u59cb\u5316\uff1b \u5982\u679c\u4e0d\u5b58\u5728\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u5219\u5bf9\u5e94\u6210\u5458\u4fdd\u6301\u672a\u521d\u59cb\u5316\u72b6\u6001\u3002 \u6210\u5458\u603b\u662f\u6309\u7167\u58f0\u660e\u7684\u987a\u5e8f\u521d\u59cb\u5316\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5bf9\u4e0a\u8ff0\u5404\u79cd\u53ef\u80fd\u6027\u8fdb\u884c\u4e86\u603b\u7ed3\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 struct Something { int x ; // no default initialization value (bad) int y {}; // value-initialized by default int z { 2 }; // explicit default value }; int main () { Something s1 ; // No initializer list: s1.x is uninitialized, s1.y and s1.z use defaults Something s2 { 5 , 6 , 7 }; // Explicit initializers: s2.x, s2.y, and s2.z use explicit values (no default values are used) Something s3 {}; // Missing initializers: s3.x is value initialized, s3.y and s3.z use defaults return 0 ; } \u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5c24\u5176\u8981\u6ce8\u610f\u7684\u662f s1.x \u3002\u56e0\u4e3a s1 \u6ca1\u6709\u521d\u59cb\u5316\u503c\u5217\u8868\uff0c\u800c\u4e14 x \u4e5f\u6ca1\u6709\u9ed8\u8ba4\u7684\u6210\u5458\u521d\u59cb\u5316\u503c\uff0c\u6240\u4ee5\u5b83\u5904\u4e8e\u672a\u521d\u59cb\u5316\u72b6\u6001\uff08\u8fd9\u662f\u4e0d\u5bf9\u7684\uff0c\u56e0\u4e3a\u53d8\u91cf\u4e00\u5b9a\u8981\u521d\u59cb\u5316\u624d\u597d\uff09\u3002 \u5e94\u5f53\u59cb\u7ec8\u4e3a\u6210\u5458\u63d0\u4f9b\u9ed8\u8ba4\u521d\u59cb\u5316\u503c \u4e3a\u4e86\u907f\u514d\u51fa\u73b0\u672a\u521d\u59cb\u5316\u7684\u6210\u5458\uff0c\u9700\u786e\u4fdd\u6bcf\u4e2a\u6210\u5458\u90fd\u6709\u4e00\u4e2a\u9ed8\u8ba4\u503c(\u8981\u4e48\u662f\u663e\u5f0f\u9ed8\u8ba4\u503c\uff0c\u8981\u4e48\u662f\u7a7a\u7684\u82b1\u62ec\u53f7\u5bf9)\u3002\u8fd9\u6837\uff0c\u65e0\u8bba\u662f\u5426\u63d0\u4f9b\u521d\u59cb\u5316\u5217\u8868\uff0c\u6210\u5458\u90fd\u5c06\u88ab\u521d\u59cb\u5316\u4e3a\u67d0\u4e2a\u503c\u3002 \u8003\u8651\u4e0b\u9762\u4f8b\u5b50\u4e2d\u4e2d\u7684\u7ed3\u6784\u4f53\uff0c\u5b83\u7684\u6240\u6709\u6210\u5458\u90fd\u6709\u9ed8\u8ba4\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 struct Fraction { int numerator { }; // we should use { 0 } here, but for the sake of example we'll use value initialization instead int denominator { 1 }; }; int main () { Fraction f1 ; // f1.numerator value initialized to 0, f1.denominator defaulted to 1 Fraction f2 {}; // f2.numerator value initialized to 0, f2.denominator defaulted to 1 Fraction f3 { 6 }; // f3.numerator initialized to 6, f3.denominator defaulted to 1 Fraction f4 { 5 , 8 }; // f4.numerator initialized to 5, f4.denominator initialized to 8 return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u7684\u6210\u5458\u90fd\u4f1a\u88ab\u521d\u59cb\u5316\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e3a\u6240\u6709\u6210\u5458\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002\u8fd9\u786e\u4fdd\u4e86\u5373\u4f7f\u53d8\u91cf\u5b9a\u4e49\u4e0d\u5305\u542b\u521d\u59cb\u5316\u5217\u8868\uff0c\u6210\u5458\u4e5f\u4f1a\u88ab\u521d\u59cb\u5316\u3002 \u9ed8\u8ba4\u521d\u59cb\u5316 vs \u805a\u5408\u7c7b\u578b\u7684\u503c\u521d\u59cb\u5316 \u56de\u987e\u4e00\u4e0b\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u4e24\u4e2a\u521d\u59cb\u5316\u65b9\u5f0f\uff1a 1 2 Fraction f1 ; // f1.numerator value initialized to 0, f1.denominator defaulted to 1 Fraction f2 {}; // f2.numerator value initialized to 0, f2.denominator defaulted to 1 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c f1 \u662f\u9ed8\u8ba4\u521d\u59cb\u5316\uff0c\u800c f2 \u662f\u503c\u521d\u59cb\u5316\u3002\u5c3d\u7ba1\u7ed3\u679c\u662f\u4e00\u6837\u7684 ( numerator \u521d\u59cb\u5316\u4e3a0\u800c denominator \u521d\u59cb\u5316\u4e3a1)\u3002\u6240\u4ee5\uff0c\u5e94\u8be5\u4f7f\u7528\u54ea\u79cd\u65b9\u5f0f\u5462\uff1f \u503c\u521d\u59cb\u5316( f2 )\u662f\u66f4\u5b89\u5168\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u786e\u4fdd\u5373\u4f7f\u6210\u5458\u6ca1\u6709\u521d\u59cb\u5316\u503c\u4e5f\u80fd\u901a\u8fc7\u503c\u521d\u59cb\u5316\u88ab\u521d\u59cb\u5316(\u5c3d\u7ba1\u6211\u4eec\u5e94\u8be5\u4e3a\u6bcf\u4e2a\u6210\u5458\u90fd\u63d0\u4f9b\u9ed8\u8ba4\u503c\uff0c\u8be5\u65b9\u6cd5\u53ef\u4ee5\u9632\u6b62\u6211\u4eec\u9057\u6f0f)\u3002 \u503c\u521d\u59cb\u5316\u8fd8\u6709\u4e00\u4e2a\u597d\u5904\u2014\u2014\u5b83\u4e0e\u521d\u59cb\u5316\u5176\u4ed6\u7c7b\u578b\u7684\u5bf9\u8c61\u7684\u65b9\u5f0f\u4e00\u81f4\u3002\u4e00\u81f4\u6027\u6709\u52a9\u4e8e\u9632\u6b62\u9519\u8bef\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u6ca1\u6709\u4e3a\u805a\u5408\u63d0\u4f9b\u663e\u5f0f\u521d\u59cb\u5316\u503c\uff0c\u5219\u9996\u9009\u503c\u521d\u59cb\u5316(\u5e26\u7a7a\u5927\u62ec\u53f7\u521d\u59cb\u5316\u5668)\u800c\u4e0d\u662f\u9ed8\u8ba4\u521d\u59cb\u5316(\u4e0d\u5e26\u5927\u62ec\u53f7)\u3002 \u4e5f\u5c31\u662f\u8bf4\uff0c\u7a0b\u5e8f\u5458\u5bf9\u7c7b\u7c7b\u578b\u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\u800c\u4e0d\u662f\u503c\u521d\u59cb\u5316\u662f\u5f88\u5e38\u89c1\u7684\u3002\u8fd9\u90e8\u5206\u662f\u7531\u4e8e\u5386\u53f2\u539f\u56e0(\u56e0\u4e3a\u503c\u521d\u59cb\u5316\u76f4\u5230C++ 11\u624d\u5f15\u5165)\uff0c\u90e8\u5206\u662f\u56e0\u4e3a\u5728\u7c7b\u4f3c\u7684\u60c5\u51b5\u4e0b(\u5bf9\u4e8e\u975e\u805a\u5408)\uff0c\u9ed8\u8ba4\u521d\u59cb\u5316\u66f4\u9ad8\u6548(\u6211\u4eec\u5728 13.5 -\u6784\u9020\u51fd\u6570 \u4e2d\u8ba8\u8bba\u4e86\u8fd9\u79cd\u60c5\u51b5)\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u4e0d\u4f1a\u5728\u8fd9\u4e9b\u6559\u7a0b\u4e2d\u5f3a\u5236\u4f7f\u7528\u7ed3\u6784\u548c\u7c7b\u7684\u503c\u521d\u59cb\u5316\uff0c\u4f46\u6211\u4eec\u5f3a\u70c8\u5efa\u8bae\u8fd9\u6837\u505a\u3002","title":"10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316"},{"location":"10-7-default-member-initialization/#_1","text":"\u5217\u8868\u521d\u59cb\u5316\u5f0f\u4e2d\u7684\u663e\u5f0f\u503c\u603b\u662f\u4f18\u5148\u4e8e\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 struct Something { int x ; // no default initialization value (bad) int y {}; // value-initialized by default int z { 2 }; // explicit default value }; int main () { Something s2 { 5 , 6 , 7 }; // use explicit initializers for s2.x, s2.y, and s2.z (no default values are used) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cs2 \u7684\u6bcf\u4e2a\u6210\u5458\u90fd\u5177\u6709\u663e\u5f0f\u5730\u521d\u59cb\u5316\u503c\uff0c\u6240\u4ee5\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u5e76\u4e0d\u751f\u6548\u3002 s2.x \u3001 s2.y \u548c s2.z \u5206\u522b\u88ab\u521d\u59cb\u5316\u4e3a 5 \u3001 6 \u548c 7 \u3002","title":"\u663e\u5f0f\u521d\u59cb\u5316\u503c\u4f18\u5148\u7ea7\u9ad8\u4e8e\u9ed8\u8ba4\u521d\u59cb\u5316\u503c"},{"location":"10-7-default-member-initialization/#_2","text":"\u5728\u4e0a\u4e00\u8282\u8bfe\u4e2d\uff08 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 \uff09\u6211\u4eec\u63d0\u5230\uff0c \u5982\u679c\u805a\u5408\u6570\u636e\u7ed3\u6784\u88ab\u521d\u59cb\u5316\u4e86\uff0c\u4f46\u521d\u59cb\u5316\u503c\u7684\u4e2a\u6570\u6bd4\u5b9e\u9645\u6210\u5458\u8981\u5c11\uff0c\u5219\u5269\u4e0b\u7684\u6210\u5458\u53d8\u91cf\u5219\u4f1a\u8fdb\u884c\u503c\u521d\u59cb\u5316\u3002\u4f46\u662f\uff0c\u5982\u679c\u8be5\u6210\u5458\u5177\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f1a\u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 struct Something { int x ; // no default initialization value (bad) int y {}; // value-initialized by default int z { 2 }; // explicit default value }; int main () { Something s3 {}; // value initialize s3.x, use default values for s3.y and s3.z return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c s3 \u7684\u521d\u59cb\u5316\u503c\u5217\u8868\u662f\u4e00\u4e2a\u7a7a\u5217\u8868\uff0c\u5373\u6ca1\u6709\u63d0\u4f9b\u4efb\u4f55\u7684\u663e\u5f0f\u521d\u59cb\u5316\u503c\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u63d0\u4f9b\u4e86\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f7f\u7528\u8be5\u503c\uff0c\u5426\u5219\u5219\u4f7f\u7528 \u503c\u521d\u59cb\u5316 \u3002\u56e0\u6b64 s3.x (\u6ca1\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c) \u4f1a\u88ab\u503c\u521d\u59cb\u5316\u4e3a0\u3002 s3.y \u7684\u503c\u88ab\u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a0\uff0c\u800c s3.z \u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a 2\u3002","title":"\u5b58\u5728\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\u4e14\u521d\u59cb\u5316\u5217\u8868\u6ca1\u6709\u63d0\u4f9b\u521d\u59cb\u5316\u503c\u7684\u60c5\u51b5"},{"location":"10-7-default-member-initialization/#_3","text":"\u5982\u679c\u805a\u5408\u5bf9\u8c61\u5b9a\u4e49\u65f6\u6709 \u521d\u59cb\u5316\u503c\u5217\u8868 \uff08\u5b9e\u4f8b\u5316\u7684\u540c\u65f6\u521d\u59cb\u5316\uff09\uff1a \u5982\u679c\u5177\u6709\u663e\u5f0f\u7684\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f7f\u7528\u8be5\u503c\uff1b \u5982\u679c\u6709\u7f3a\u5931\u7684\u521d\u59cb\u5316\u503c\uff0c\u4f46\u5b58\u5728\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c\u8fdb\u884c \u9ed8\u8ba4\u521d\u59cb\u5316 \uff1b \u5982\u679c\u6709\u7f3a\u5931\u7684\u521d\u59cb\u5316\u503c\uff0c\u4f46\u662f\u6ca1\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f7f\u7528 \u503c\u521d\u59cb\u5316 \u3002 \u5982\u679c\u805a\u5408\u5bf9\u8c61\u5b9a\u4e49\u65f6\u6ca1\u6709\u521d\u59cb\u5316\u503c\u5217\u8868\uff08\u5b9e\u4f8b\u5316\u65f6\u672a\u8fdb\u884c\u521d\u59cb\u5316\uff09\uff1a \u5982\u679c\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c\u8fdb\u884c\u9ed8\u8ba4\u521d\u59cb\u5316\uff1b \u5982\u679c\u4e0d\u5b58\u5728\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u5219\u5bf9\u5e94\u6210\u5458\u4fdd\u6301\u672a\u521d\u59cb\u5316\u72b6\u6001\u3002 \u6210\u5458\u603b\u662f\u6309\u7167\u58f0\u660e\u7684\u987a\u5e8f\u521d\u59cb\u5316\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5bf9\u4e0a\u8ff0\u5404\u79cd\u53ef\u80fd\u6027\u8fdb\u884c\u4e86\u603b\u7ed3\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 struct Something { int x ; // no default initialization value (bad) int y {}; // value-initialized by default int z { 2 }; // explicit default value }; int main () { Something s1 ; // No initializer list: s1.x is uninitialized, s1.y and s1.z use defaults Something s2 { 5 , 6 , 7 }; // Explicit initializers: s2.x, s2.y, and s2.z use explicit values (no default values are used) Something s3 {}; // Missing initializers: s3.x is value initialized, s3.y and s3.z use defaults return 0 ; } \u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5c24\u5176\u8981\u6ce8\u610f\u7684\u662f s1.x \u3002\u56e0\u4e3a s1 \u6ca1\u6709\u521d\u59cb\u5316\u503c\u5217\u8868\uff0c\u800c\u4e14 x \u4e5f\u6ca1\u6709\u9ed8\u8ba4\u7684\u6210\u5458\u521d\u59cb\u5316\u503c\uff0c\u6240\u4ee5\u5b83\u5904\u4e8e\u672a\u521d\u59cb\u5316\u72b6\u6001\uff08\u8fd9\u662f\u4e0d\u5bf9\u7684\uff0c\u56e0\u4e3a\u53d8\u91cf\u4e00\u5b9a\u8981\u521d\u59cb\u5316\u624d\u597d\uff09\u3002","title":"\u590d\u4e60\uff1a\u521d\u59cb\u5316\u7684\u53ef\u80fd\u6027"},{"location":"10-7-default-member-initialization/#_4","text":"\u4e3a\u4e86\u907f\u514d\u51fa\u73b0\u672a\u521d\u59cb\u5316\u7684\u6210\u5458\uff0c\u9700\u786e\u4fdd\u6bcf\u4e2a\u6210\u5458\u90fd\u6709\u4e00\u4e2a\u9ed8\u8ba4\u503c(\u8981\u4e48\u662f\u663e\u5f0f\u9ed8\u8ba4\u503c\uff0c\u8981\u4e48\u662f\u7a7a\u7684\u82b1\u62ec\u53f7\u5bf9)\u3002\u8fd9\u6837\uff0c\u65e0\u8bba\u662f\u5426\u63d0\u4f9b\u521d\u59cb\u5316\u5217\u8868\uff0c\u6210\u5458\u90fd\u5c06\u88ab\u521d\u59cb\u5316\u4e3a\u67d0\u4e2a\u503c\u3002 \u8003\u8651\u4e0b\u9762\u4f8b\u5b50\u4e2d\u4e2d\u7684\u7ed3\u6784\u4f53\uff0c\u5b83\u7684\u6240\u6709\u6210\u5458\u90fd\u6709\u9ed8\u8ba4\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 struct Fraction { int numerator { }; // we should use { 0 } here, but for the sake of example we'll use value initialization instead int denominator { 1 }; }; int main () { Fraction f1 ; // f1.numerator value initialized to 0, f1.denominator defaulted to 1 Fraction f2 {}; // f2.numerator value initialized to 0, f2.denominator defaulted to 1 Fraction f3 { 6 }; // f3.numerator initialized to 6, f3.denominator defaulted to 1 Fraction f4 { 5 , 8 }; // f4.numerator initialized to 5, f4.denominator initialized to 8 return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u7684\u6210\u5458\u90fd\u4f1a\u88ab\u521d\u59cb\u5316\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e3a\u6240\u6709\u6210\u5458\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002\u8fd9\u786e\u4fdd\u4e86\u5373\u4f7f\u53d8\u91cf\u5b9a\u4e49\u4e0d\u5305\u542b\u521d\u59cb\u5316\u5217\u8868\uff0c\u6210\u5458\u4e5f\u4f1a\u88ab\u521d\u59cb\u5316\u3002","title":"\u5e94\u5f53\u59cb\u7ec8\u4e3a\u6210\u5458\u63d0\u4f9b\u9ed8\u8ba4\u521d\u59cb\u5316\u503c"},{"location":"10-7-default-member-initialization/#vs","text":"\u56de\u987e\u4e00\u4e0b\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u4e24\u4e2a\u521d\u59cb\u5316\u65b9\u5f0f\uff1a 1 2 Fraction f1 ; // f1.numerator value initialized to 0, f1.denominator defaulted to 1 Fraction f2 {}; // f2.numerator value initialized to 0, f2.denominator defaulted to 1 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c f1 \u662f\u9ed8\u8ba4\u521d\u59cb\u5316\uff0c\u800c f2 \u662f\u503c\u521d\u59cb\u5316\u3002\u5c3d\u7ba1\u7ed3\u679c\u662f\u4e00\u6837\u7684 ( numerator \u521d\u59cb\u5316\u4e3a0\u800c denominator \u521d\u59cb\u5316\u4e3a1)\u3002\u6240\u4ee5\uff0c\u5e94\u8be5\u4f7f\u7528\u54ea\u79cd\u65b9\u5f0f\u5462\uff1f \u503c\u521d\u59cb\u5316( f2 )\u662f\u66f4\u5b89\u5168\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u786e\u4fdd\u5373\u4f7f\u6210\u5458\u6ca1\u6709\u521d\u59cb\u5316\u503c\u4e5f\u80fd\u901a\u8fc7\u503c\u521d\u59cb\u5316\u88ab\u521d\u59cb\u5316(\u5c3d\u7ba1\u6211\u4eec\u5e94\u8be5\u4e3a\u6bcf\u4e2a\u6210\u5458\u90fd\u63d0\u4f9b\u9ed8\u8ba4\u503c\uff0c\u8be5\u65b9\u6cd5\u53ef\u4ee5\u9632\u6b62\u6211\u4eec\u9057\u6f0f)\u3002 \u503c\u521d\u59cb\u5316\u8fd8\u6709\u4e00\u4e2a\u597d\u5904\u2014\u2014\u5b83\u4e0e\u521d\u59cb\u5316\u5176\u4ed6\u7c7b\u578b\u7684\u5bf9\u8c61\u7684\u65b9\u5f0f\u4e00\u81f4\u3002\u4e00\u81f4\u6027\u6709\u52a9\u4e8e\u9632\u6b62\u9519\u8bef\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u6ca1\u6709\u4e3a\u805a\u5408\u63d0\u4f9b\u663e\u5f0f\u521d\u59cb\u5316\u503c\uff0c\u5219\u9996\u9009\u503c\u521d\u59cb\u5316(\u5e26\u7a7a\u5927\u62ec\u53f7\u521d\u59cb\u5316\u5668)\u800c\u4e0d\u662f\u9ed8\u8ba4\u521d\u59cb\u5316(\u4e0d\u5e26\u5927\u62ec\u53f7)\u3002 \u4e5f\u5c31\u662f\u8bf4\uff0c\u7a0b\u5e8f\u5458\u5bf9\u7c7b\u7c7b\u578b\u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\u800c\u4e0d\u662f\u503c\u521d\u59cb\u5316\u662f\u5f88\u5e38\u89c1\u7684\u3002\u8fd9\u90e8\u5206\u662f\u7531\u4e8e\u5386\u53f2\u539f\u56e0(\u56e0\u4e3a\u503c\u521d\u59cb\u5316\u76f4\u5230C++ 11\u624d\u5f15\u5165)\uff0c\u90e8\u5206\u662f\u56e0\u4e3a\u5728\u7c7b\u4f3c\u7684\u60c5\u51b5\u4e0b(\u5bf9\u4e8e\u975e\u805a\u5408)\uff0c\u9ed8\u8ba4\u521d\u59cb\u5316\u66f4\u9ad8\u6548(\u6211\u4eec\u5728 13.5 -\u6784\u9020\u51fd\u6570 \u4e2d\u8ba8\u8bba\u4e86\u8fd9\u79cd\u60c5\u51b5)\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u4e0d\u4f1a\u5728\u8fd9\u4e9b\u6559\u7a0b\u4e2d\u5f3a\u5236\u4f7f\u7528\u7ed3\u6784\u548c\u7c7b\u7684\u503c\u521d\u59cb\u5316\uff0c\u4f46\u6211\u4eec\u5f3a\u70c8\u5efa\u8bae\u8fd9\u6837\u505a\u3002","title":"\u9ed8\u8ba4\u521d\u59cb\u5316 vs \u805a\u5408\u7c7b\u578b\u7684\u503c\u521d\u59cb\u5316"},{"location":"10-8-struct-passing-and-miscellany/","tags":["struct"],"text":"Key Takeaway \u5047\u8bbe\u4e00\u4e2a\u5458\u5de5\u75313\u4e2a\u677e\u6563\u53d8\u91cf\u8868\u793a\uff1a 1 2 3 4 5 6 7 8 int main () { int id { 1 }; int age { 24 }; double wage { 52400.0 }; return 0 ; } \u5982\u679c\u6211\u4eec\u60f3\u628a\u8fd9\u4e2a\u96c7\u5458\u4f20\u9012\u7ed9\u4e00\u4e2a\u51fd\u6570\uff0c\u5219\u987b\u4f20\u9012\u4e09\u4e2a\u53d8\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void printEmployee ( int id , int age , double wage ) { std :: cout << \"ID: \" << id << '\\n' ; std :: cout << \"Age: \" << age << '\\n' ; std :: cout << \"Wage: \" << wage << '\\n' ; } int main () { int id { 1 }; int age { 24 }; double wage { 52400.0 }; printEmployee ( id , age , wage ); return 0 ; } \u867d\u7136\u4f20\u90123\u4e2a\u53d8\u91cf\u5e76\u4e0d\u662f\u90a3\u4e48\u7cdf\u7cd5\uff0c\u4f46\u8003\u8651\u4e00\u4e2a\u670910\u621612\u4e2a\u6210\u5458\u7684\u7ed3\u6784\u4f53\u3002\u72ec\u7acb\u5730\u4f20\u9012\u6bcf\u4e2a\u53d8\u91cf\u65e2\u8017\u65f6\u53c8\u5bb9\u6613\u51fa\u9519\u3002\u6b64\u5916\uff0c\u5982\u679c\u6211\u4eec\u5411\u96c7\u5458\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u5c5e\u6027(\u4f8b\u5982\u540d\u79f0)\uff0c\u6211\u4eec\u73b0\u5728\u5fc5\u987b\u4fee\u6539\u6240\u6709\u51fd\u6570\u58f0\u660e\u3001\u5b9a\u4e49\u548c\u51fd\u6570\u8c03\u7528\uff0c\u4ee5\u63a5\u53d7\u65b0\u7684\u5f62\u53c2\u548c\u5b9e\u53c2\uff01 \u6309\u5f15\u7528\u4f20\u9012\u7ed3\u6784\u4f53 \u4f7f\u7528\u7ed3\u6784\u4f53\u800c\u4e0d\u662f\u5355\u4e2a\u53d8\u91cf\u7684\u4e00\u5927\u4f18\u70b9\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6574\u4e2a\u7ed3\u6784\u4f20\u9012\u7ed9\u9700\u8981\u4e0e\u6210\u5458\u4e00\u8d77\u5de5\u4f5c\u7684\u51fd\u6570\u3002\u7ed3\u6784\u4f53\u901a\u5e38\u901a\u8fc7(const)\u5f15\u7528\u4f20\u9012\uff0c\u4ee5\u907f\u514d\u62f7\u8d1d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include struct Employee { int id {}; int age {}; double wage {}; }; void printEmployee ( const Employee & employee ) // note pass by reference here { std :: cout << \"ID: \" << employee . id << '\\n' ; std :: cout << \"Age: \" << employee . age << '\\n' ; std :: cout << \"Wage: \" << employee . wage << '\\n' ; } int main () { Employee joe { 14 , 32 , 24.15 }; Employee frank { 15 , 28 , 18.27 }; // Print Joe's information printEmployee ( joe ); std :: cout << '\\n' ; // Print Frank's information printEmployee ( frank ); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06 Employee \u4f5c\u4e3a\u6574\u4f53\u4f20\u9012\u7ed9 printEmployee() (\u4f20\u9012\u4e86\u91cf\u8bcd\uff0c\u4e00\u6b21\u662f joe \u4e00\u6b21\u662f frank )\u3002 \u4e0a\u9762\u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 3 4 5 6 7 ID: 14 Age: 32 Wage: 24.15 ID: 15 Age: 28 Wage: 18.27 \u56e0\u4e3a\u4f20\u9012\u7684\u662f\u6574\u4e2a\u7ed3\u6784\u5bf9\u8c61(\u800c\u4e0d\u662f\u5355\u4e2a\u6210\u5458)\uff0c\u6240\u4ee5\u65e0\u8bba\u7ed3\u6784\u5bf9\u8c61\u6709\u591a\u5c11\u6210\u5458\uff0c\u6211\u4eec\u53ea\u9700\u8981\u4e00\u4e2a\u5f62\u53c2\u3002\u800c\u4e14\uff0c\u5728\u5c06\u6765\uff0c\u5982\u679c\u9700\u8981\u5411 Employee \u7ed3\u6784\u4f53\u4e2d\u6dfb\u52a0\u65b0\u6210\u5458\uff0c\u4e5f\u4e0d\u5fc5\u66f4\u6539\u51fd\u6570\u58f0\u660e\u6216\u51fd\u6570\u8c03\u7528\uff01\u65b0\u6210\u5458\u5c06\u81ea\u52a8\u88ab\u5305\u62ec\u5728\u5185\u3002 \u8fd4\u56de\u7ed3\u6784\u4f53 \u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u51fd\u6570\u9700\u8981\u8fd4\u56de\u4e09\u7ef4\u7b1b\u5361\u5c14\u7a7a\u95f4\u4e2d\u7684\u4e00\u4e2a\u70b9\u3002\u8fd9\u6837\u7684\u70b9\u67093\u4e2a\u5c5e\u6027\uff1a x \u5750\u6807\u3001 y \u5750\u6807\u548c z \u5750\u6807\u3002\u4f46\u662f\u51fd\u6570\u53ea\u80fd\u8fd4\u56de\u4e00\u4e2a\u503c\u3002\u6b64\u65f6\u5e94\u8be5\u5982\u4f55\u5c063\u4e2a\u5750\u6807\u5168\u90e8\u8fd4\u56de\u7ed9\u7528\u6237? \u6700\u5e38\u89c1\u7684\u529e\u6cd5\u662f\u8fd4\u56de\u4e00\u4e2a\u7ed3\u6784\u4f53\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include struct Point3d { double x { 0.0 }; double y { 0.0 }; double z { 0.0 }; }; Point3d getZeroPoint () { // We can create a variable and return the variable (we'll improve this below) Point3d temp { 0.0 , 0.0 , 0.0 }; return temp ; } int main () { Point3d zero { getZeroPoint () }; if ( zero . x == 0.0 && zero . y == 0.0 && zero . z == 0.0 ) std :: cout << \"The point is zero \\n \" ; else std :: cout << \"The point is not zero \\n \" ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 The point is zero \u7ed3\u6784\u4f53\u901a\u5e38 \u6309\u503c\u8fd4\u56de \uff0c\u8fd9\u6837\u5c31\u4e0d\u4f1a\u4ea7\u751f \u60ac\u5782 \u5f15\u7528\u3002 \u8fd4\u56de\u533f\u540d\u7ed3\u6784\u4f53 getZeroPoint() \u51fd\u6570\u4e2d\u521b\u5efa\u4e86\u4e00\u4e2a\u5bf9\u8c61( temp )\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u76f4\u63a5\u8fd4\u56de\uff1a 1 2 3 4 5 6 Point3d getZeroPoint () { // We can create a variable and return the variable (we'll improve this below) Point3d temp { 0.0 , 0.0 , 0.0 }; return temp ; } \u8fd9\u4e2a\u547d\u540d\u5bf9\u8c61 ( temp ) \u5bf9\u53ef\u8bfb\u6027\u5e76\u6ca1\u6709\u4ec0\u4e48\u5e2e\u52a9\u3002 \u6211\u4eec\u53ef\u4ee5\u5bf9\u51fd\u6570\u7a0d\u52a0\u4fee\u6539\uff0c\u4f7f\u5176\u8fd4\u56de\u4e00\u4e2a\u4e34\u65f6\u7684\u533f\u540d\u5bf9\u8c61\uff1a 1 2 3 4 Point3d getZeroPoint () { return Point3d { 0.0 , 0.0 , 0.0 }; // return an unnamed Point3d } \u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u521b\u5efa\u4e86\u4e00\u4e2a\u4e34\u65f6\u7684 Point3d \u5bf9\u8c61\u5e76\u5c06\u5176 \u6309\u503c\u8fd4\u56de \uff08\u62f7\u8d1d\u8fd4\u56de\uff09\u7ed9\u8c03\u7528\u8005\u3002\u8be5\u5bf9\u8c61\u4f1a\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u6ce8\u610f\u8fd9\u662f\u591a\u4e48\u7684\u7b80\u6d01(\u4e00\u884c\u76f8\u6bd4\u4e24\u884c\uff0c\u5e76\u4e14\u4e0d\u9700\u8981\u5224\u65ad temp \u662f\u5426\u4f7f\u7528\u4e86\u591a\u6b21)\u3002 \u5728\u51fd\u6570\u6709\u663e\u5f0f\u8fd4\u56de\u7c7b\u578b\u7684\u60c5\u51b5\u4e0b(\u4f8b\u5982: Point3d )\u800c\u4e0d\u662f\u4f7f\u7528\u7c7b\u578b\u6f14\u7ece(\u4e00\u4e2a auto \u8fd4\u56de\u7c7b\u578b)\uff0c\u6211\u4eec\u751a\u81f3\u53ef\u4ee5\u5728return\u8bed\u53e5\u4e2d\u7701\u7565\u7c7b\u578b: 1 2 3 4 5 6 Point3d getZeroPoint () { // We already specified the type at the function declaration // so we don't need to do so here again return { 0.0 , 0.0 , 0.0 }; // return an unnamed Point3d } \u8fd8\u8981\u6ce8\u610f\uff0c\u56e0\u4e3a\u5728\u672c\u4f8b\u4e2d\u6211\u4eec\u8fd4\u56de\u7684\u90fd\u662f\u96f6\u503c\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7a7a\u5927\u62ec\u53f7\u6765\u8fd4\u56de\u4e00\u4e2a\u503c\u521d\u59cb\u5316\u7684 Point3d \uff1a 1 2 3 4 5 Point3d getZeroPoint () { // We can use empty curly braces to value-initialize all members return {}; } \u5305\u542b\u7a0b\u5e8f\u81ea\u5b9a\u4e49\u7c7b\u578b\u6210\u5458\u7684\u7ed3\u6784\u4f53 \u5728C++\u4e2d\uff0c\u7ed3\u6784\u4f53(\u548c\u7c7b)\u7684\u6210\u5458\u53ef\u4ee5\u662f\u5176\u4ed6\u7a0b\u5e8f\u5b9a\u4e49\u7684\u7c7b\u578b\u3002\u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\u3002 \u9996\u5148\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\uff08\u5728\u5168\u5c40\u4f5c\u7528\u57df\uff09\uff0c\u7136\u540e\u5c06\u5176\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7684\u6210\u5458\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include struct Employee { int id {}; int age {}; double wage {}; }; struct Company { int numberOfEmployees {}; Employee CEO {}; // Employee is a struct within the Company struct }; int main () { Company myCompany { 7 , { 1 , 32 , 55000.0 } }; // Nested initialization list to initialize Employee std :: cout << myCompany . CEO . wage ; // print the CEO's wage } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a Employee \u7ed3\u6784\u4f53\uff0c\u7136\u540e\u5c06\u5176\u4f5c\u4e3a\u53e6\u4e00\u4e2a Company \u7ed3\u6784\u4f53\u7684\u6210\u5458\u3002 \u5f53 Company \u521d\u59cb\u5316\u65f6\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u7684 \u521d\u59cb\u5316\u503c\u5217\u8868 \u521d\u59cb\u5316 Employee \u3002\u5982\u679c\u6211\u4eec\u60f3\u77e5\u9053\u67d0\u4e2aCEO\u7684\u85aa\u6c34\u662f\u591a\u5c11\uff0c\u5219\u9700\u8981\u4f7f\u7528\u4e24\u6b21 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 \uff1a myCompany.CEO.wage; \u5176\u6b21\uff0c\u7c7b\u578b\u4e5f\u53ef\u4ee5\u88ab\u5d4c\u5957\u5b9a\u4e49\u5728\u5176\u4ed6\u7c7b\u578b\u4e2d\uff0c\u641c\u6613\u5982\u679c Employee \u53ea\u4f1a\u4f5c\u4e3a Company \u7684\u6210\u5458\u4f7f\u7528\uff0c\u5219\u53ef\u4ee5\u5c06\u5176\u5b9a\u4e49\u5728 Company \u7ed3\u6784\u4f53\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include struct Company { struct Employee // accessed via Company::Employee { int id {}; int age {}; double wage {}; }; int numberOfEmployees {}; Employee CEO {}; // Employee is a struct within the Company struct }; int main () { Company myCompany { 7 , { 1 , 32 , 55000.0 } }; // Nested initialization list to initialize Employee std :: cout << myCompany . CEO . wage ; // print the CEO's wage } \u8fd9\u79cd\u5f62\u5f0f\u901a\u5e38\u4f1a\u5728\u7c7b\u4e2d\u4f7f\u7528\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u4ecb\u7ecd 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b \u3002 \u7ed3\u6784\u4f53\u5927\u5c0f\u548c\u6570\u636e\u7ed3\u6784\u5bf9\u9f50 \u901a\u5e38\u6211\u4eec\u4f1a\u8ba4\u4e3a\u7ed3\u6784\u4f53\u7684\u5927\u5c0f\u5c31\u662f\u5176\u6240\u6709\u6210\u5458\u5927\u5c0f\u7684\u603b\u548c\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u4e00\u5b9a\uff01 \u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include struct Foo { short a {}; int b {}; double c {}; }; int main () { std :: cout << \"The size of Foo is \" << sizeof ( Foo ) << '\\n' ; return 0 ; } \u5728\u5f88\u591a\u5e73\u53f0\u4e0a\uff0cshort \u662f 2 \u5b57\u8282\u3001int \u662f 4 \u5b57\u8282\u800c double \u662f 8 \u5b57\u8282\uff0c\u6240\u4ee5\u6211\u4eec\u4f1a\u8ba4\u4e3a sizeof(Foo) \u7684\u957f\u5ea6\u4e3a 2 + 4 + 8 = 14 \u5b57\u8282\u3002\u4f46\u662f\uff0c\u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\u6253\u5370\u7ed3\u679c\u4e3a\u3002 1 The size of Foo is 16 \u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u8ba1\u7b97\u7684\u7ed3\u679c\uff0c\u53ea\u662f\u7ed3\u6784\u4f53\u6700\u5c0f\u7684\u5927\u5c0f\uff0c\u5373\u7ed3\u6784\u4f53\u7684\u5927\u5c0f\u7684\u6700\u5c0f\u503c\u4e3a\u5176\u6240\u6709\u6210\u5458\u7684\u5927\u5c0f\u7684\u603b\u548c\u3002\u4f46\u662f\u5b83\u4e5f\u53ef\u4ee5\u5360\u636e\u66f4\u591a\u7a7a\u95f4\u3002\u5904\u4e8e\u6027\u80fd\u4f18\u5316\u7684\u8003\u8651\uff0c\u7f16\u8bd1\u5668\u6709\u65f6\u4f1a\u5728\u7ed3\u6784\u4f53\u4e2d\u6dfb\u52a0\u4e00\u4e9b\u201c\u95f4\u9699\u201d\uff08\u79f0\u4e3a \u7ed3\u6784\u4f53\u586b\u5145 \uff09 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c Foo \u7ed3\u6784\u4f53\u4f1a\u88ab\u7f16\u8bd1\u5668\u63d2\u5165\u4e00\u4e2a2\u5b57\u8282\u7684\u4e0d\u53ef\u89c1\u7684\u586b\u5145\uff08\u4f4d\u4e8e\u6210\u5458a\u4e4b\u540e\uff09\uff0c\u4f7f\u5f97\u7ed3\u6784\u4f53\u7684\u5927\u5c0f\u4ece14\u53d8\u4e3a16\u3002 \u6269\u5c55\u9605\u8bfb \u7f16\u8bd1\u5668\u4e4b\u6240\u4ee5\u5bf9\u7ed3\u6784\u4f53\u8fdb\u884c\u586b\u5145\u7684\u539f\u56e0\uff0c\u8d85\u51fa\u4e86\u672c\u6559\u7a0b\u7684\u8303\u56f4\uff0c\u5982\u679c\u60a8\u6709\u5174\u8da3\u53ef\u4ee5\u9605\u8bfb\u7ef4\u57fa\u767e\u79d1\u4e0a\u5173\u4e8e \u6570\u636e\u7ed3\u6784\u5bf9\u9f50 \u7684\u5185\u5bb9\u3002\u8be5\u77e5\u8bc6\u70b9\u5c5e\u4e8e\u6269\u5c55\u9605\u8bfb\u5185\u5bb9\uff0c\u5b83\u5e76\u4e0d\u662f\u638c\u63e1\u7ed3\u6784\u4f53\u6216C++\u5fc5\u5907\u7684\u77e5\u8bc6\uff01 \u8fd9\u5b9e\u9645\u4e0a\u4f1a\u5bf9\u7ed3\u6784\u7684\u5927\u5c0f\u4ea7\u751f\u76f8\u5f53\u5927\u7684\u5f71\u54cd\uff0c\u5982\u4e0b\u9762\u7684\u7a0b\u5e8f\u6240\u793a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include struct Foo1 { short a {}; short qq {}; // note: qq is defined here int b {}; double c {}; }; struct Foo2 { short a {}; int b {}; double c {}; short qq {}; // note: qq is defined here }; int main () { std :: cout << \"The size of Foo1 is \" << sizeof ( Foo1 ) << '\\n' ; std :: cout << \"The size of Foo2 is \" << sizeof ( Foo2 ) << '\\n' ; return 0 ; } \u6ce8\u610f\uff0c\u5c3d\u7ba1 Foo1 \u548c Foo2 \u5177\u6709\u76f8\u540c\u7684\u6210\u5458\uff0c\u4f46\u662f\u5176\u4e2d\u6210\u5458 qq \u58f0\u660e\u7684\u4f4d\u7f6e\u662f\u4e0d\u540c\u7684\uff0c\u7a0b\u5e8f\u6253\u5370\uff1a 1 2 The size of Foo1 is 16 The size of Foo2 is 24 \u7ed3\u6784\u4f53\u662f\u91cd\u8981\u7684\u7a0b\u5e8f\u7ec4\u6210\u90e8\u5206 \u867d\u7136\u7ed3\u6784\u672c\u8eab\u5f88\u6709\u7528\uff0c\u4f46\u7c7b(\u7c7b\u662fC++\u548c\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u6838\u5fc3)\u76f4\u63a5\u6784\u5efa\u5728\u6211\u4eec\u8fd9\u91cc\u4ecb\u7ecd\u7684\u6982\u5ff5\u4e4b\u4e0a\u3002\u5728\u5f88\u597d\u5730\u7406\u89e3\u7ed3\u6784\u4f53(\u7279\u522b\u662f\u6570\u636e\u6210\u5458\u3001\u6210\u5458\u9009\u62e9\u548c\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316)\u4e4b\u540e\uff0c\u5b66\u4e60\u7c7b\u4e5f\u4f1a\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\u3002","title":"10.8 - \u7ed3\u6784\u4f53\u4f20\u9012\u53ca\u5176\u4ed6"},{"location":"10-8-struct-passing-and-miscellany/#_1","text":"\u4f7f\u7528\u7ed3\u6784\u4f53\u800c\u4e0d\u662f\u5355\u4e2a\u53d8\u91cf\u7684\u4e00\u5927\u4f18\u70b9\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6574\u4e2a\u7ed3\u6784\u4f20\u9012\u7ed9\u9700\u8981\u4e0e\u6210\u5458\u4e00\u8d77\u5de5\u4f5c\u7684\u51fd\u6570\u3002\u7ed3\u6784\u4f53\u901a\u5e38\u901a\u8fc7(const)\u5f15\u7528\u4f20\u9012\uff0c\u4ee5\u907f\u514d\u62f7\u8d1d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include struct Employee { int id {}; int age {}; double wage {}; }; void printEmployee ( const Employee & employee ) // note pass by reference here { std :: cout << \"ID: \" << employee . id << '\\n' ; std :: cout << \"Age: \" << employee . age << '\\n' ; std :: cout << \"Wage: \" << employee . wage << '\\n' ; } int main () { Employee joe { 14 , 32 , 24.15 }; Employee frank { 15 , 28 , 18.27 }; // Print Joe's information printEmployee ( joe ); std :: cout << '\\n' ; // Print Frank's information printEmployee ( frank ); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06 Employee \u4f5c\u4e3a\u6574\u4f53\u4f20\u9012\u7ed9 printEmployee() (\u4f20\u9012\u4e86\u91cf\u8bcd\uff0c\u4e00\u6b21\u662f joe \u4e00\u6b21\u662f frank )\u3002 \u4e0a\u9762\u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 3 4 5 6 7 ID: 14 Age: 32 Wage: 24.15 ID: 15 Age: 28 Wage: 18.27 \u56e0\u4e3a\u4f20\u9012\u7684\u662f\u6574\u4e2a\u7ed3\u6784\u5bf9\u8c61(\u800c\u4e0d\u662f\u5355\u4e2a\u6210\u5458)\uff0c\u6240\u4ee5\u65e0\u8bba\u7ed3\u6784\u5bf9\u8c61\u6709\u591a\u5c11\u6210\u5458\uff0c\u6211\u4eec\u53ea\u9700\u8981\u4e00\u4e2a\u5f62\u53c2\u3002\u800c\u4e14\uff0c\u5728\u5c06\u6765\uff0c\u5982\u679c\u9700\u8981\u5411 Employee \u7ed3\u6784\u4f53\u4e2d\u6dfb\u52a0\u65b0\u6210\u5458\uff0c\u4e5f\u4e0d\u5fc5\u66f4\u6539\u51fd\u6570\u58f0\u660e\u6216\u51fd\u6570\u8c03\u7528\uff01\u65b0\u6210\u5458\u5c06\u81ea\u52a8\u88ab\u5305\u62ec\u5728\u5185\u3002","title":"\u6309\u5f15\u7528\u4f20\u9012\u7ed3\u6784\u4f53"},{"location":"10-8-struct-passing-and-miscellany/#_2","text":"\u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u51fd\u6570\u9700\u8981\u8fd4\u56de\u4e09\u7ef4\u7b1b\u5361\u5c14\u7a7a\u95f4\u4e2d\u7684\u4e00\u4e2a\u70b9\u3002\u8fd9\u6837\u7684\u70b9\u67093\u4e2a\u5c5e\u6027\uff1a x \u5750\u6807\u3001 y \u5750\u6807\u548c z \u5750\u6807\u3002\u4f46\u662f\u51fd\u6570\u53ea\u80fd\u8fd4\u56de\u4e00\u4e2a\u503c\u3002\u6b64\u65f6\u5e94\u8be5\u5982\u4f55\u5c063\u4e2a\u5750\u6807\u5168\u90e8\u8fd4\u56de\u7ed9\u7528\u6237? \u6700\u5e38\u89c1\u7684\u529e\u6cd5\u662f\u8fd4\u56de\u4e00\u4e2a\u7ed3\u6784\u4f53\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include struct Point3d { double x { 0.0 }; double y { 0.0 }; double z { 0.0 }; }; Point3d getZeroPoint () { // We can create a variable and return the variable (we'll improve this below) Point3d temp { 0.0 , 0.0 , 0.0 }; return temp ; } int main () { Point3d zero { getZeroPoint () }; if ( zero . x == 0.0 && zero . y == 0.0 && zero . z == 0.0 ) std :: cout << \"The point is zero \\n \" ; else std :: cout << \"The point is not zero \\n \" ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 The point is zero \u7ed3\u6784\u4f53\u901a\u5e38 \u6309\u503c\u8fd4\u56de \uff0c\u8fd9\u6837\u5c31\u4e0d\u4f1a\u4ea7\u751f \u60ac\u5782 \u5f15\u7528\u3002","title":"\u8fd4\u56de\u7ed3\u6784\u4f53"},{"location":"10-8-struct-passing-and-miscellany/#_3","text":"getZeroPoint() \u51fd\u6570\u4e2d\u521b\u5efa\u4e86\u4e00\u4e2a\u5bf9\u8c61( temp )\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u76f4\u63a5\u8fd4\u56de\uff1a 1 2 3 4 5 6 Point3d getZeroPoint () { // We can create a variable and return the variable (we'll improve this below) Point3d temp { 0.0 , 0.0 , 0.0 }; return temp ; } \u8fd9\u4e2a\u547d\u540d\u5bf9\u8c61 ( temp ) \u5bf9\u53ef\u8bfb\u6027\u5e76\u6ca1\u6709\u4ec0\u4e48\u5e2e\u52a9\u3002 \u6211\u4eec\u53ef\u4ee5\u5bf9\u51fd\u6570\u7a0d\u52a0\u4fee\u6539\uff0c\u4f7f\u5176\u8fd4\u56de\u4e00\u4e2a\u4e34\u65f6\u7684\u533f\u540d\u5bf9\u8c61\uff1a 1 2 3 4 Point3d getZeroPoint () { return Point3d { 0.0 , 0.0 , 0.0 }; // return an unnamed Point3d } \u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u521b\u5efa\u4e86\u4e00\u4e2a\u4e34\u65f6\u7684 Point3d \u5bf9\u8c61\u5e76\u5c06\u5176 \u6309\u503c\u8fd4\u56de \uff08\u62f7\u8d1d\u8fd4\u56de\uff09\u7ed9\u8c03\u7528\u8005\u3002\u8be5\u5bf9\u8c61\u4f1a\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u6ce8\u610f\u8fd9\u662f\u591a\u4e48\u7684\u7b80\u6d01(\u4e00\u884c\u76f8\u6bd4\u4e24\u884c\uff0c\u5e76\u4e14\u4e0d\u9700\u8981\u5224\u65ad temp \u662f\u5426\u4f7f\u7528\u4e86\u591a\u6b21)\u3002 \u5728\u51fd\u6570\u6709\u663e\u5f0f\u8fd4\u56de\u7c7b\u578b\u7684\u60c5\u51b5\u4e0b(\u4f8b\u5982: Point3d )\u800c\u4e0d\u662f\u4f7f\u7528\u7c7b\u578b\u6f14\u7ece(\u4e00\u4e2a auto \u8fd4\u56de\u7c7b\u578b)\uff0c\u6211\u4eec\u751a\u81f3\u53ef\u4ee5\u5728return\u8bed\u53e5\u4e2d\u7701\u7565\u7c7b\u578b: 1 2 3 4 5 6 Point3d getZeroPoint () { // We already specified the type at the function declaration // so we don't need to do so here again return { 0.0 , 0.0 , 0.0 }; // return an unnamed Point3d } \u8fd8\u8981\u6ce8\u610f\uff0c\u56e0\u4e3a\u5728\u672c\u4f8b\u4e2d\u6211\u4eec\u8fd4\u56de\u7684\u90fd\u662f\u96f6\u503c\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7a7a\u5927\u62ec\u53f7\u6765\u8fd4\u56de\u4e00\u4e2a\u503c\u521d\u59cb\u5316\u7684 Point3d \uff1a 1 2 3 4 5 Point3d getZeroPoint () { // We can use empty curly braces to value-initialize all members return {}; }","title":"\u8fd4\u56de\u533f\u540d\u7ed3\u6784\u4f53"},{"location":"10-8-struct-passing-and-miscellany/#_4","text":"\u5728C++\u4e2d\uff0c\u7ed3\u6784\u4f53(\u548c\u7c7b)\u7684\u6210\u5458\u53ef\u4ee5\u662f\u5176\u4ed6\u7a0b\u5e8f\u5b9a\u4e49\u7684\u7c7b\u578b\u3002\u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\u3002 \u9996\u5148\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\uff08\u5728\u5168\u5c40\u4f5c\u7528\u57df\uff09\uff0c\u7136\u540e\u5c06\u5176\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7684\u6210\u5458\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include struct Employee { int id {}; int age {}; double wage {}; }; struct Company { int numberOfEmployees {}; Employee CEO {}; // Employee is a struct within the Company struct }; int main () { Company myCompany { 7 , { 1 , 32 , 55000.0 } }; // Nested initialization list to initialize Employee std :: cout << myCompany . CEO . wage ; // print the CEO's wage } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a Employee \u7ed3\u6784\u4f53\uff0c\u7136\u540e\u5c06\u5176\u4f5c\u4e3a\u53e6\u4e00\u4e2a Company \u7ed3\u6784\u4f53\u7684\u6210\u5458\u3002 \u5f53 Company \u521d\u59cb\u5316\u65f6\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u7684 \u521d\u59cb\u5316\u503c\u5217\u8868 \u521d\u59cb\u5316 Employee \u3002\u5982\u679c\u6211\u4eec\u60f3\u77e5\u9053\u67d0\u4e2aCEO\u7684\u85aa\u6c34\u662f\u591a\u5c11\uff0c\u5219\u9700\u8981\u4f7f\u7528\u4e24\u6b21 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 \uff1a myCompany.CEO.wage; \u5176\u6b21\uff0c\u7c7b\u578b\u4e5f\u53ef\u4ee5\u88ab\u5d4c\u5957\u5b9a\u4e49\u5728\u5176\u4ed6\u7c7b\u578b\u4e2d\uff0c\u641c\u6613\u5982\u679c Employee \u53ea\u4f1a\u4f5c\u4e3a Company \u7684\u6210\u5458\u4f7f\u7528\uff0c\u5219\u53ef\u4ee5\u5c06\u5176\u5b9a\u4e49\u5728 Company \u7ed3\u6784\u4f53\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include struct Company { struct Employee // accessed via Company::Employee { int id {}; int age {}; double wage {}; }; int numberOfEmployees {}; Employee CEO {}; // Employee is a struct within the Company struct }; int main () { Company myCompany { 7 , { 1 , 32 , 55000.0 } }; // Nested initialization list to initialize Employee std :: cout << myCompany . CEO . wage ; // print the CEO's wage } \u8fd9\u79cd\u5f62\u5f0f\u901a\u5e38\u4f1a\u5728\u7c7b\u4e2d\u4f7f\u7528\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u4ecb\u7ecd 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b \u3002","title":"\u5305\u542b\u7a0b\u5e8f\u81ea\u5b9a\u4e49\u7c7b\u578b\u6210\u5458\u7684\u7ed3\u6784\u4f53"},{"location":"10-8-struct-passing-and-miscellany/#_5","text":"\u901a\u5e38\u6211\u4eec\u4f1a\u8ba4\u4e3a\u7ed3\u6784\u4f53\u7684\u5927\u5c0f\u5c31\u662f\u5176\u6240\u6709\u6210\u5458\u5927\u5c0f\u7684\u603b\u548c\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u4e00\u5b9a\uff01 \u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include struct Foo { short a {}; int b {}; double c {}; }; int main () { std :: cout << \"The size of Foo is \" << sizeof ( Foo ) << '\\n' ; return 0 ; } \u5728\u5f88\u591a\u5e73\u53f0\u4e0a\uff0cshort \u662f 2 \u5b57\u8282\u3001int \u662f 4 \u5b57\u8282\u800c double \u662f 8 \u5b57\u8282\uff0c\u6240\u4ee5\u6211\u4eec\u4f1a\u8ba4\u4e3a sizeof(Foo) \u7684\u957f\u5ea6\u4e3a 2 + 4 + 8 = 14 \u5b57\u8282\u3002\u4f46\u662f\uff0c\u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\u6253\u5370\u7ed3\u679c\u4e3a\u3002 1 The size of Foo is 16 \u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u8ba1\u7b97\u7684\u7ed3\u679c\uff0c\u53ea\u662f\u7ed3\u6784\u4f53\u6700\u5c0f\u7684\u5927\u5c0f\uff0c\u5373\u7ed3\u6784\u4f53\u7684\u5927\u5c0f\u7684\u6700\u5c0f\u503c\u4e3a\u5176\u6240\u6709\u6210\u5458\u7684\u5927\u5c0f\u7684\u603b\u548c\u3002\u4f46\u662f\u5b83\u4e5f\u53ef\u4ee5\u5360\u636e\u66f4\u591a\u7a7a\u95f4\u3002\u5904\u4e8e\u6027\u80fd\u4f18\u5316\u7684\u8003\u8651\uff0c\u7f16\u8bd1\u5668\u6709\u65f6\u4f1a\u5728\u7ed3\u6784\u4f53\u4e2d\u6dfb\u52a0\u4e00\u4e9b\u201c\u95f4\u9699\u201d\uff08\u79f0\u4e3a \u7ed3\u6784\u4f53\u586b\u5145 \uff09 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c Foo \u7ed3\u6784\u4f53\u4f1a\u88ab\u7f16\u8bd1\u5668\u63d2\u5165\u4e00\u4e2a2\u5b57\u8282\u7684\u4e0d\u53ef\u89c1\u7684\u586b\u5145\uff08\u4f4d\u4e8e\u6210\u5458a\u4e4b\u540e\uff09\uff0c\u4f7f\u5f97\u7ed3\u6784\u4f53\u7684\u5927\u5c0f\u4ece14\u53d8\u4e3a16\u3002 \u6269\u5c55\u9605\u8bfb \u7f16\u8bd1\u5668\u4e4b\u6240\u4ee5\u5bf9\u7ed3\u6784\u4f53\u8fdb\u884c\u586b\u5145\u7684\u539f\u56e0\uff0c\u8d85\u51fa\u4e86\u672c\u6559\u7a0b\u7684\u8303\u56f4\uff0c\u5982\u679c\u60a8\u6709\u5174\u8da3\u53ef\u4ee5\u9605\u8bfb\u7ef4\u57fa\u767e\u79d1\u4e0a\u5173\u4e8e \u6570\u636e\u7ed3\u6784\u5bf9\u9f50 \u7684\u5185\u5bb9\u3002\u8be5\u77e5\u8bc6\u70b9\u5c5e\u4e8e\u6269\u5c55\u9605\u8bfb\u5185\u5bb9\uff0c\u5b83\u5e76\u4e0d\u662f\u638c\u63e1\u7ed3\u6784\u4f53\u6216C++\u5fc5\u5907\u7684\u77e5\u8bc6\uff01 \u8fd9\u5b9e\u9645\u4e0a\u4f1a\u5bf9\u7ed3\u6784\u7684\u5927\u5c0f\u4ea7\u751f\u76f8\u5f53\u5927\u7684\u5f71\u54cd\uff0c\u5982\u4e0b\u9762\u7684\u7a0b\u5e8f\u6240\u793a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include struct Foo1 { short a {}; short qq {}; // note: qq is defined here int b {}; double c {}; }; struct Foo2 { short a {}; int b {}; double c {}; short qq {}; // note: qq is defined here }; int main () { std :: cout << \"The size of Foo1 is \" << sizeof ( Foo1 ) << '\\n' ; std :: cout << \"The size of Foo2 is \" << sizeof ( Foo2 ) << '\\n' ; return 0 ; } \u6ce8\u610f\uff0c\u5c3d\u7ba1 Foo1 \u548c Foo2 \u5177\u6709\u76f8\u540c\u7684\u6210\u5458\uff0c\u4f46\u662f\u5176\u4e2d\u6210\u5458 qq \u58f0\u660e\u7684\u4f4d\u7f6e\u662f\u4e0d\u540c\u7684\uff0c\u7a0b\u5e8f\u6253\u5370\uff1a 1 2 The size of Foo1 is 16 The size of Foo2 is 24","title":"\u7ed3\u6784\u4f53\u5927\u5c0f\u548c\u6570\u636e\u7ed3\u6784\u5bf9\u9f50"},{"location":"10-8-struct-passing-and-miscellany/#_6","text":"\u867d\u7136\u7ed3\u6784\u672c\u8eab\u5f88\u6709\u7528\uff0c\u4f46\u7c7b(\u7c7b\u662fC++\u548c\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u6838\u5fc3)\u76f4\u63a5\u6784\u5efa\u5728\u6211\u4eec\u8fd9\u91cc\u4ecb\u7ecd\u7684\u6982\u5ff5\u4e4b\u4e0a\u3002\u5728\u5f88\u597d\u5730\u7406\u89e3\u7ed3\u6784\u4f53(\u7279\u522b\u662f\u6570\u636e\u6210\u5458\u3001\u6210\u5458\u9009\u62e9\u548c\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316)\u4e4b\u540e\uff0c\u5b66\u4e60\u7c7b\u4e5f\u4f1a\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\u3002","title":"\u7ed3\u6784\u4f53\u662f\u91cd\u8981\u7684\u7a0b\u5e8f\u7ec4\u6210\u90e8\u5206"},{"location":"10-9-member-selection-with-pointers-and-references/","tags":["struct"],"text":"Key Takeaway \u7ed3\u6784\u4f53\u548c\u7ed3\u6784\u4f53\u5f15\u7528\u7684\u6210\u5458\u9009\u62e9 \u5728 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 \u4e2d\u6211\u4eec\u63d0\u5230\uff0c\u8bbf\u95ee\u7ed3\u6784\u4f53\u7684\u6210\u5458\u53ef\u4ee5\u4f7f\u7528 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 34 , 65000.0 }; // Use member selection operator (.) to select a member from struct object ++ joe . age ; // Joe had a birthday joe . wage = 68000.0 ; // Joe got a promotion return 0 ; } \u56e0\u4e3a\u5bf9\u8c61\u7684\u5f15\u7528\u5176\u5b9e\u5c31\u662f\u5bf9\u8c61\u672c\u8eab\uff0c\u6240\u4ee5\u6211\u4eec\u4e5f\u53ef\u4ee5\u5bf9\u5f15\u7528\u4f7f\u7528\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26\u6765\u8bbf\u95ee\u7ed3\u6784\u4f53\u6210\u5458\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include struct Employee { int id {}; int age {}; double wage {}; }; void printEmployee ( const Employee & e ) { // Use member selection opeartor (.) to select member from reference to struct std :: cout << \"Id: \" << e . id << '\\n' ; std :: cout << \" Age: \" << e . age << '\\n' ; std :: cout << \" Wage: \" << e . wage << '\\n' ; } int main () { Employee joe { 1 , 34 , 65000.0 }; ++ joe . age ; joe . wage = 68000.0 ; printEmployee ( joe ); return 0 ; } \u7ed3\u6784\u4f53\u6307\u9488\u7684\u6210\u5458\u9009\u62e9 \u4e0d\u8fc7\uff0c\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26\u5e76\u4e0d\u80fd\u88ab\u7528\u4e8e\u6307\u5411\u7ed3\u6784\u4f53\u7684\u6307\u9488\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 34 , 65000.0 }; ++ joe . age ; joe . wage = 68000.0 ; Employee * ptr { & joe }; std :: cout << ptr . id << '\\n' ; // Compile error: can't use operator. with pointers return 0 ; } \u5bf9\u4e8e\u666e\u901a\u53d8\u91cf\u548c\u5f15\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5b83\u3002\u800c\u5bf9\u4e8e\u4fdd\u5b58\u7740\u5730\u5740\u7684\u6307\u9488\u6765\u8bf4\uff0c\u6211\u4eec\u5fc5\u987b\u5728\u8bbf\u95ee\u5bf9\u8c61\u524d\uff0c\u9996\u5148\u5bf9\u5730\u5740\u89e3\u5f15\u7528\u4ee5\u83b7\u5f97\u8be5\u5bf9\u8c61\u3002 \u6240\u4ee5\uff0c\u901a\u8fc7\u7ed3\u6784\u4f53\u8bbf\u95ee\u6210\u5458\u7684\u4e00\u79cd\u65b9\u5f0f\u5e94\u8be5\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 34 , 65000.0 }; ++ joe . age ; joe . wage = 68000.0 ; Employee * ptr { & joe }; std :: cout << ( * ptr ). id << '\\n' ; // Not great but works: First dereference ptr, then use member selection return 0 ; } \u4f46\u662f\uff0c\u8fd9\u5e76\u4e0d\u4f18\u96c5\uff0c\u5c24\u5176\u662f\u8fd9\u91cc\u5fc5\u987b\u4f7f\u7528\u62ec\u53f7\u4ee5\u786e\u4fdd\u6b63\u786e\u7684\u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u3002 \u4e3a\u4e86\u80fd\u591f\u63d0\u4f9b\u4e00\u4e2a\u66f4\u7b80\u6d01\u7684\u8bed\u6cd5\uff0cC++\u63d0\u4f9b\u4e86\u6307\u9488\u8fd0\u7b97\u7b26\u8fdb\u884c\u6210\u5458\u9009\u62e9\uff08\u6709\u65f6\u5019\u79f0\u4e3a\u7bad\u5934\u8fd0\u7b97\u7b26\uff09\u4f7f\u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u5bf9\u8c61\u6307\u9488\u6765\u8bbf\u95ee\u5176\u6210\u5458\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 34 , 65000.0 }; ++ joe . age ; joe . wage = 68000.0 ; Employee * ptr { & joe }; std :: cout << ptr -> id << '\\n' ; // Better: use -> to select member from pointer to object return 0 ; } \u6307\u9488\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26\u548c\u666e\u901a\u7684\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26\u7528\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u662f\u5b83\u4f1a\u9996\u5148\u8fdb\u884c\u9690\u5f0f\u7684\u89e3\u5f15\u7528\u3002\u8be5\u7bad\u5934\u8fd0\u7b97\u7b26\u4e0d\u4ec5\u66f4\u5bb9\u6613\u5199\uff0c\u4e5f\u66f4\u4e0d\u5bb9\u6613\u51fa\u9519\uff0c\u56e0\u4e3a\u5b83\u4f1a\u9690\u5f0f\u8fdb\u884c\u89e3\u5f15\u7528\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u9700\u8981\u64cd\u5fc3\u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u7684\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u5728\u901a\u8fc7\u7ed3\u6784\u4f53\u6307\u9488\u8bbf\u95ee\u5176\u6210\u5458\u65f6\uff0c\u5e94\u8be5\u603b\u662f\u4f7f\u7528\u7bad\u5934\u8fd0\u7b97\u7b26\u6765\u5b8c\u6210\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u4f7f\u7528\u6307\u9488\u8bbf\u95ee\u6210\u5458\u7684\u503c\u65f6\uff0c\u4f7f\u7528 -> ;\u800c\u4e0d\u662f . \u540c\u65f6\u4f7f\u7528\u6307\u9488\u548c\u975e\u6307\u9488\u6210\u5458\u8bbf\u95ee \u6210\u5458\u9009\u62e9\u64cd\u4f5c\u7b26\u59cb\u7ec8\u5e94\u7528\u4e8e\u5f53\u524d\u9009\u5b9a\u7684\u53d8\u91cf\u3002\u5982\u679c\u6307\u9488\u548c\u666e\u901a\u6210\u5458\u53d8\u91cf\u6df7\u5408\u4f7f\u7528\uff0c\u5219\u6210\u5458\u9009\u62e9 . \u548c -> \u662f\u6309\u987a\u5e8f\u4f7f\u7528\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include struct Paw { int claws {}; }; struct Animal { std :: string name {}; Paw paw {}; }; int main () { Animal puma { \"Puma\" , { 5 } }; Animal * ptr { & puma }; // ptr is a pointer, use -> // paw is not a pointer, use . std :: cout << ( ptr -> paw ). claws << '\\n' ; return 0 ; } \u6ce8\u610f (ptr->paw).claws \u4e2d\u7684\u62ec\u53f7\u662f\u53ef\u9009\u7684\uff0c\u56e0\u4e3a operator-> and operator. \u90fd\u662f\u4ece\u5de6\u5411\u53f3\u8fdb\u884c\u8fd0\u7b97\u7684\uff0c\u4f46\u662f\u6dfb\u52a0\u62ec\u53f7\u53ef\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002","title":"10.9 - \u57fa\u4e8e\u6307\u9488\u548c\u5f15\u7528\u7684\u6210\u5458\u9009\u62e9"},{"location":"10-9-member-selection-with-pointers-and-references/#_1","text":"\u5728 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 \u4e2d\u6211\u4eec\u63d0\u5230\uff0c\u8bbf\u95ee\u7ed3\u6784\u4f53\u7684\u6210\u5458\u53ef\u4ee5\u4f7f\u7528 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 34 , 65000.0 }; // Use member selection operator (.) to select a member from struct object ++ joe . age ; // Joe had a birthday joe . wage = 68000.0 ; // Joe got a promotion return 0 ; } \u56e0\u4e3a\u5bf9\u8c61\u7684\u5f15\u7528\u5176\u5b9e\u5c31\u662f\u5bf9\u8c61\u672c\u8eab\uff0c\u6240\u4ee5\u6211\u4eec\u4e5f\u53ef\u4ee5\u5bf9\u5f15\u7528\u4f7f\u7528\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26\u6765\u8bbf\u95ee\u7ed3\u6784\u4f53\u6210\u5458\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include struct Employee { int id {}; int age {}; double wage {}; }; void printEmployee ( const Employee & e ) { // Use member selection opeartor (.) to select member from reference to struct std :: cout << \"Id: \" << e . id << '\\n' ; std :: cout << \" Age: \" << e . age << '\\n' ; std :: cout << \" Wage: \" << e . wage << '\\n' ; } int main () { Employee joe { 1 , 34 , 65000.0 }; ++ joe . age ; joe . wage = 68000.0 ; printEmployee ( joe ); return 0 ; }","title":"\u7ed3\u6784\u4f53\u548c\u7ed3\u6784\u4f53\u5f15\u7528\u7684\u6210\u5458\u9009\u62e9"},{"location":"10-9-member-selection-with-pointers-and-references/#_2","text":"\u4e0d\u8fc7\uff0c\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26\u5e76\u4e0d\u80fd\u88ab\u7528\u4e8e\u6307\u5411\u7ed3\u6784\u4f53\u7684\u6307\u9488\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 34 , 65000.0 }; ++ joe . age ; joe . wage = 68000.0 ; Employee * ptr { & joe }; std :: cout << ptr . id << '\\n' ; // Compile error: can't use operator. with pointers return 0 ; } \u5bf9\u4e8e\u666e\u901a\u53d8\u91cf\u548c\u5f15\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5b83\u3002\u800c\u5bf9\u4e8e\u4fdd\u5b58\u7740\u5730\u5740\u7684\u6307\u9488\u6765\u8bf4\uff0c\u6211\u4eec\u5fc5\u987b\u5728\u8bbf\u95ee\u5bf9\u8c61\u524d\uff0c\u9996\u5148\u5bf9\u5730\u5740\u89e3\u5f15\u7528\u4ee5\u83b7\u5f97\u8be5\u5bf9\u8c61\u3002 \u6240\u4ee5\uff0c\u901a\u8fc7\u7ed3\u6784\u4f53\u8bbf\u95ee\u6210\u5458\u7684\u4e00\u79cd\u65b9\u5f0f\u5e94\u8be5\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 34 , 65000.0 }; ++ joe . age ; joe . wage = 68000.0 ; Employee * ptr { & joe }; std :: cout << ( * ptr ). id << '\\n' ; // Not great but works: First dereference ptr, then use member selection return 0 ; } \u4f46\u662f\uff0c\u8fd9\u5e76\u4e0d\u4f18\u96c5\uff0c\u5c24\u5176\u662f\u8fd9\u91cc\u5fc5\u987b\u4f7f\u7528\u62ec\u53f7\u4ee5\u786e\u4fdd\u6b63\u786e\u7684\u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u3002 \u4e3a\u4e86\u80fd\u591f\u63d0\u4f9b\u4e00\u4e2a\u66f4\u7b80\u6d01\u7684\u8bed\u6cd5\uff0cC++\u63d0\u4f9b\u4e86\u6307\u9488\u8fd0\u7b97\u7b26\u8fdb\u884c\u6210\u5458\u9009\u62e9\uff08\u6709\u65f6\u5019\u79f0\u4e3a\u7bad\u5934\u8fd0\u7b97\u7b26\uff09\u4f7f\u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u5bf9\u8c61\u6307\u9488\u6765\u8bbf\u95ee\u5176\u6210\u5458\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 34 , 65000.0 }; ++ joe . age ; joe . wage = 68000.0 ; Employee * ptr { & joe }; std :: cout << ptr -> id << '\\n' ; // Better: use -> to select member from pointer to object return 0 ; } \u6307\u9488\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26\u548c\u666e\u901a\u7684\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26\u7528\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u662f\u5b83\u4f1a\u9996\u5148\u8fdb\u884c\u9690\u5f0f\u7684\u89e3\u5f15\u7528\u3002\u8be5\u7bad\u5934\u8fd0\u7b97\u7b26\u4e0d\u4ec5\u66f4\u5bb9\u6613\u5199\uff0c\u4e5f\u66f4\u4e0d\u5bb9\u6613\u51fa\u9519\uff0c\u56e0\u4e3a\u5b83\u4f1a\u9690\u5f0f\u8fdb\u884c\u89e3\u5f15\u7528\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u9700\u8981\u64cd\u5fc3\u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u7684\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u5728\u901a\u8fc7\u7ed3\u6784\u4f53\u6307\u9488\u8bbf\u95ee\u5176\u6210\u5458\u65f6\uff0c\u5e94\u8be5\u603b\u662f\u4f7f\u7528\u7bad\u5934\u8fd0\u7b97\u7b26\u6765\u5b8c\u6210\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u4f7f\u7528\u6307\u9488\u8bbf\u95ee\u6210\u5458\u7684\u503c\u65f6\uff0c\u4f7f\u7528 -> ;\u800c\u4e0d\u662f .","title":"\u7ed3\u6784\u4f53\u6307\u9488\u7684\u6210\u5458\u9009\u62e9"},{"location":"10-9-member-selection-with-pointers-and-references/#_3","text":"\u6210\u5458\u9009\u62e9\u64cd\u4f5c\u7b26\u59cb\u7ec8\u5e94\u7528\u4e8e\u5f53\u524d\u9009\u5b9a\u7684\u53d8\u91cf\u3002\u5982\u679c\u6307\u9488\u548c\u666e\u901a\u6210\u5458\u53d8\u91cf\u6df7\u5408\u4f7f\u7528\uff0c\u5219\u6210\u5458\u9009\u62e9 . \u548c -> \u662f\u6309\u987a\u5e8f\u4f7f\u7528\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include struct Paw { int claws {}; }; struct Animal { std :: string name {}; Paw paw {}; }; int main () { Animal puma { \"Puma\" , { 5 } }; Animal * ptr { & puma }; // ptr is a pointer, use -> // paw is not a pointer, use . std :: cout << ( ptr -> paw ). claws << '\\n' ; return 0 ; } \u6ce8\u610f (ptr->paw).claws \u4e2d\u7684\u62ec\u53f7\u662f\u53ef\u9009\u7684\uff0c\u56e0\u4e3a operator-> and operator. \u90fd\u662f\u4ece\u5de6\u5411\u53f3\u8fdb\u884c\u8fd0\u7b97\u7684\uff0c\u4f46\u662f\u6dfb\u52a0\u62ec\u53f7\u53ef\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002","title":"\u540c\u65f6\u4f7f\u7528\u6307\u9488\u548c\u975e\u6307\u9488\u6210\u5458\u8bbf\u95ee"},{"location":"10-x-Chapter-10-comprehensive-quiz/","tags":["summary"],"text":"10.1 - \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7b80\u4ecb \u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u662f\u4e00\u79cd\u81ea\u5b9a\u4e49\u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u521b\u5efa\u5b83\u7528\u4e8e\u67d0\u4e9b\u76ee\u7684\u3002\u57fa\u4e8e\u679a\u4e3e\u7c7b\u578b\u548cclass\u7c7b\u578b\uff08\u5305\u62ec\u7ed3\u6784\u4f53\u3001\u7c7b\u548c\u8054\u5408\uff09\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u9700\u8981\u7684\u81ea\u5b9a\u4e49\u7c7b\u578b\u3002\u81ea\u5b9a\u4e49\u7c7b\u578b\u5728\u4f7f\u7528\u524d\u5fc5\u987b\u5148\u5b9a\u4e49\uff0c\u5b9a\u4e49\u5b83\u7684\u8fc7\u7a0b\u5219\u79f0\u4e3a \u7c7b\u578b\u5b9a\u4e49 \u3002\u7c7b\u578b\u5b9a\u4e49\u4e0d\u9700\u8981\u9075\u5faa \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u3002 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b 10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa \u679a\u4e3e\uff08\u79f0\u4e3a\u679a\u4e3e\u7c7b\u578b\u6216enum\uff09\u662f\u4e00\u79cd\u590d\u5408\u6570\u636e\u7c7b\u578b\uff0c\u5b83\u7684\u6bcf\u4e2a\u53ef\u80fd\u503c\u90fd\u88ab\u5b9a\u4e49\u4e3a \u7b26\u53f7\u5e38\u91cf \uff08\u79f0\u4e3a\u679a\u4e3e\u503c\uff09\u3002\u679a\u4e3e\u503c\u662f\u4e00\u79cd\u786e\u5207\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4e5f\u5c31\u662f\u8bf4\u7f16\u8bd1\u5668\u53ef\u4ee5\u533a\u5206\u5b83\u548c\u5176\u4ed6\u7c7b\u578b\uff08\u4e0d\u50cf \u7c7b\u578b\u522b\u540d \u90a3\u6837\uff09\u3002 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u4f1a\u5c06\u81ea\u5df1\u7684\u679a\u4e3e\u503c\u540d\u79f0\u5bfc\u5165\u5230\u4e0e\u81ea\u5df1\u76f8\u540c\u7684\u4f5c\u7528\u57df\u4e2d(\u800c\u4e0d\u662f\u50cf\u547d\u540d\u7a7a\u95f4\u90a3\u6837\u521b\u5efa\u65b0\u7684\u4f5c\u7528\u57df\u533a\u57df)\u3002\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u4f1a\u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a\u6574\u578b\u6570\u3002 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e \u4e0e\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7684\u5de5\u4f5c\u539f\u7406\u7c7b\u4f3c\uff0c\u4f46\u5b83\u4eec\u662f\u5f3a\u7c7b\u578b\u7684(\u5b83\u4eec\u4e0d\u4f1a\u9690\u5f0f\u8f6c\u6362\u4e3a\u6574\u6570)\u548c\u5f3a\u4f5c\u7528\u57df\u7684(\u679a\u4e3e\u6570\u53ea\u653e\u5728\u679a\u4e3e\u7684\u4f5c\u7528\u57df\u533a\u57df\u4e2d)\u3002 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 \u7ed3\u6784\u4f53\u662f\u4e00\u79cd\u81ea\u5b9a\u4e49\u7c7b\u578b\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u5c06\u591a\u4e2a\u53d8\u91cf\u6253\u5305\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u7c7b\u578b\u3002\u53d8\u91cf\u662f\u7ed3\u6784\u4f53\uff08\u6216\u7c7b\uff09\u7684\u4e00\u90e8\u5206\uff0c\u79f0\u4e3a\u6570\u636e\u6210\u5458\u6216 \u6210\u5458\u53d8\u91cf \u3002\u8bbf\u95ee\u6210\u5458\u51fd\u6570\u9700\u8981\u5728\u7ed3\u6784\u4f53\u53d8\u91cf\u540d\uff08\u666e\u901a\u7ed3\u6784\u4f53\u6216\u7ed3\u6784\u4f53\u5f15\u7528\uff09\u548c\u6210\u5458\u540d\u4e4b\u95f4\u4f7f\u7528 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 \uff08 operator . \uff09\uff0c\u6216\u8005\u4f7f\u7528\u6307\u9488\u8fd0\u7b97\u7b26( operator-> ) \uff08\u5bf9\u4e8e\u7ed3\u6784\u4f53\u6307\u9488\u800c\u8a00\uff09\u3002 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 \u5728\u4e00\u822c\u7f16\u7a0b\u9886\u57df\uff0c \u805a\u5408\u6570\u636e\u7c7b\u578b \uff08\u901a\u5e38\u79f0\u4e3a\u805a\u5408\uff09\u662f\u6307\u4efb\u4f55\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u6570\u636e\u6210\u5458\u7684\u7c7b\u578b\u3002\u5728 C++ \u4e2d\uff0c\u53ea\u5305\u542b\u6570\u636e\u6210\u5458\u7684\u6570\u7ec4\u548c\u7ed3\u6784\u4f53\u662f\u805a\u5408\u3002 \u805a\u5408\u4f7f\u7528\u4e00\u79cd\u88ab\u79f0\u4e3a \u805a\u5408\u521d\u59cb\u5316 \u7684\u521d\u59cb\u5316\u5f62\u5f0f\uff0c\u4f7f\u7528\u805a\u5408\u521d\u59cb\u5316\u53ef\u4ee5\u76f4\u63a5\u521d\u59cb\u5316\u805a\u5408\u7684\u6210\u5458\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u9700\u8981\u63d0\u4f9b\u4e00\u4e2a\u521d\u59cb\u5316\u5217\u8868\u4f5c\u4e3a\u521d\u59cb\u5316\u503c\uff0c\u800c\u6240\u8c13\u521d\u59cb\u5316\u5217\u8868\u5c31\u662f\u4e00\u4e2a\u4ee5\u9017\u53f7\u5206\u5272\u503c\u7684\u5217\u8868\u3002\u805a\u5408\u521d\u59cb\u5316\u4f7f\u7528 \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \u65b9\u5f0f\uff0c\u5373\u7ed3\u6784\u4f53\u7684\u6bcf\u4e2a\u6210\u5458\u4f9d\u7167\u5176\u58f0\u660e\u987a\u5e8f\u8fdb\u884c\u521d\u59cb\u5316\u3002 \u5728 C++20 \u4e2d\uff0c\u4f7f\u7528 \u6307\u5b9a\u521d\u59cb\u5316 \u53ef\u4ee5\u663e\u5f0f\u5730\u6307\u5b9a\u5c06\u521d\u59cb\u5316\u503c\u6620\u5c04\u5230\u54ea\u4e2a\u6210\u5458\u3002\u7ed3\u6784\u4f53\u6210\u5458\u5fc5\u987b\u6309\u7167\u58f0\u660e\u7684\u987a\u5e8f\u521d\u59cb\u5316\uff0c\u5426\u5219\u4f1a\u62a5\u9519\u3002 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 - \u5b9a\u4e49\u7ed3\u6784(\u6216\u7c7b)\u7c7b\u578b\u65f6\uff0c\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u6210\u5458\u63d0\u4f9b\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u4f5c\u4e3a\u7c7b\u578b\u5b9a\u4e49\u7684\u4e00\u90e8\u5206\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \uff0c\u521d\u59cb\u5316\u503c\u79f0\u4e3a \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c \u3002 10.8 - \u7ed3\u6784\u4f53\u4f20\u9012\u53ca\u5176\u4ed6 \u51fa\u4e8e\u6027\u80fd\u539f\u56e0\uff0c\u7f16\u8bd1\u5668\u6709\u65f6\u4f1a\u5728\u7ed3\u6784\u4e2d\u6dfb\u52a0\u95f4\u9699(\u8fd9\u79f0\u4e3a\u586b\u5145)\uff0c\u56e0\u6b64\u7ed3\u6784\u4f53\u7684\u5927\u5c0f\u53ef\u80fd\u5927\u4e8e\u5176\u6210\u5458\u7684\u5927\u5c0f\u4e4b\u548c\u3002 10.9 - \u57fa\u4e8e\u6307\u9488\u548c\u5f15\u7528\u7684\u6210\u5458\u9009\u62e9 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD \u7c7b\u6a21\u677f \u662f\u5b9e\u4f8b\u5316\u7c7b\u7c7b\u578b(\u7ed3\u6784\u3001\u7c7b\u6216\u8054\u5408)\u7684\u6a21\u677f\u5b9a\u4e49\u3002 \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad(CTAD) \u662f C++17\u7684\u4e00\u4e2a\u7279\u6027\uff0c\u5b83\u5141\u8bb8\u7f16\u8bd1\u5668\u4ece\u521d\u59cb\u5316\u5f0f\u4e2d\u63a8\u65ad\u6a21\u677f\u7c7b\u578b\u5b9e\u53c2\u3002","title":"10.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u679a\u4e3e\u548c\u7ed3\u6784\u4f53"},{"location":"10-x-Chapter-10-comprehensive-quiz/#-","text":"10.8 - \u7ed3\u6784\u4f53\u4f20\u9012\u53ca\u5176\u4ed6 \u51fa\u4e8e\u6027\u80fd\u539f\u56e0\uff0c\u7f16\u8bd1\u5668\u6709\u65f6\u4f1a\u5728\u7ed3\u6784\u4e2d\u6dfb\u52a0\u95f4\u9699(\u8fd9\u79f0\u4e3a\u586b\u5145)\uff0c\u56e0\u6b64\u7ed3\u6784\u4f53\u7684\u5927\u5c0f\u53ef\u80fd\u5927\u4e8e\u5176\u6210\u5458\u7684\u5927\u5c0f\u4e4b\u548c\u3002 10.9 - \u57fa\u4e8e\u6307\u9488\u548c\u5f15\u7528\u7684\u6210\u5458\u9009\u62e9 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD \u7c7b\u6a21\u677f \u662f\u5b9e\u4f8b\u5316\u7c7b\u7c7b\u578b(\u7ed3\u6784\u3001\u7c7b\u6216\u8054\u5408)\u7684\u6a21\u677f\u5b9a\u4e49\u3002 \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad(CTAD) \u662f C++17\u7684\u4e00\u4e2a\u7279\u6027\uff0c\u5b83\u5141\u8bb8\u7f16\u8bd1\u5668\u4ece\u521d\u59cb\u5316\u5f0f\u4e2d\u63a8\u65ad\u6a21\u677f\u7c7b\u578b\u5b9e\u53c2\u3002","title":"- \u5b9a\u4e49\u7ed3\u6784(\u6216\u7c7b)\u7c7b\u578b\u65f6\uff0c\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u6210\u5458\u63d0\u4f9b\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u4f5c\u4e3a\u7c7b\u578b\u5b9a\u4e49\u7684\u4e00\u90e8\u5206\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\uff0c\u521d\u59cb\u5316\u503c\u79f0\u4e3a\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c\u3002"},{"location":"10-y-using-a-language-reference/","tags":["summary"],"text":"\u6839\u636e\u4f60\u5b66\u4e60C++\u6240\u5904\u7684\u9636\u6bb5\uff0c\u672c\u7f51\u7ad9\u53ef\u80fd\u662f\u4f60\u5728\u67d0\u4e2a\u9636\u6bb5\u5b66\u4e60C++\u7684\u552f\u4e00\u8d44\u6599\u3002LearnCpp.com \u4ee5\u4e00\u79cd\u65b0\u624b\u53cb\u597d\u7684\u65b9\u5f0f\u5411\u4f60\u4ecb\u7ecd\u4e86C++\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4f46\u662f\u5b83\u4e0d\u53ef\u80fd\u6db5\u76d6C++\u7684\u65b9\u65b9\u9762\u9762\u3002\u5f53\u4f60\u5f00\u59cb\u63a2\u7d22\u672c\u6559\u7a0b\u4ee5\u5916\u7684\u8bdd\u9898\u65f6\uff0c\u4f60\u9700\u8981\u5b66\u4f1a\u5982\u4f55\u5229\u7528\u5176\u4ed6\u7684\u8d44\u6e90\u6765\u8fdb\u884c\u5b66\u4e60\u3002 Stack Overflow \u662f\u4e00\u4e2a\u95ee\u7b54\u7f51\u7ad9\uff0c\u5728\u8fd9\u91cc\u4f60\u53ef\u4ee5\u95ee\u95ee\u9898(\u4f60\u53ef\u4ee5\u5728\u8fd9\u91cc\u627e\u5230\u4e0e\u4f60\u6709\u76f8\u540c\u95ee\u9898\u7684\u4eba\u5e76\u4e14\u9605\u8bfb\u8fd9\u4e9b\u95ee\u9898\u7684\u7b54\u6848)\u3002\u4e0d\u8fc7\uff0c\u591a\u6570\u60c5\u51b5\u4e0b\u6211\u4eec\u8fd8\u662f\u5efa\u8bae\u4f60\u5148\u53bb\u770b\u770bC++\u8bed\u8a00\u7684\u53c2\u8003\u624b\u518c\u3002\u548c\u6559\u7a0b\u4e0d\u540c\uff0c\u6559\u7a0b\u901a\u5e38\u4e13\u6ce8\u4e8e\u67d0\u4e9b\u91cd\u8981\u8bdd\u9898\u5e76\u4f7f\u7528\u901a\u4fd7\u7684\u8bed\u8a00\u5411\u4f60\u89e3\u91ca\u76f8\u5173\u7684\u6982\u5ff5\uff0c\u4f7f\u5f97\u8bed\u8a00\u5b66\u4e60\u66f4\u5bb9\u6613\u3002\u4f46\u662f\u53c2\u8003\u624b\u518c\u4e0d\u540c\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u4f7f\u7528\u6b63\u5f0f\u7684\u672f\u8bed\uff0c\u7cbe\u786e\u5730\u63cf\u8ff0C++\u4e2d\u7684\u6982\u5ff5\u3002\u56e0\u6b64\uff0c\u53c2\u8003\u624b\u518c\u901a\u5e38\u5f88\u5168\u9762\u3001\u7cbe\u786e\uff0c\u4f46\u6bd4\u8f83\u96be\u4ee5\u7406\u89e3\u3002 \u672c\u8282\u8bfe\u6211\u4eec\u4f1a\u5411\u4f60\u4ecb\u7ecd\u5982\u4f55\u9ad8\u6548\u5730\u4f7f\u7528 cppreference \uff0c\u8fd9\u662f\u4e00\u4e2a\u6d41\u884c\u7684C++\u6807\u51c6\u53c2\u8003\u6587\u6863\u7f51\u7ad9\uff0c\u672c\u6559\u7a0b\u81ea\u59cb\u81f3\u7ec8\u591a\u6b21\u53c2\u8003\u4e86\u8be5\u7f51\u7ad9\u7684\u5185\u5bb9\u3002\u6211\u4eec\u4f1a\u4f7f\u7528\u4e09\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u5411\u4f60\u5c55\u793a\u5982\u4f55\u53bb\u4f7f\u7528\u8be5\u53c2\u8003\u7f51\u7ad9\u3002 \u603b\u89c8 Cppreference \u7684\u9996\u9875\u662f\u4e00\u4e2a \u603b\u89c8\u9875\u9762 \u5176\u4e2d\u5217\u51fa\u4e86\u8bed\u8a00\u7684\u6838\u5fc3\u529f\u80fd\u548c\u6807\u51c6\u5e93\uff1a \u867d\u7136\u5728\u8fd9\u4e2a\u9875\u9762\u4e2d\uff0c\u4f60\u53ef\u4ee5\u770b\u5230cppreference\u63d0\u4f9b\u7684\u6240\u6709\u5185\u5bb9\u7684\u76ee\u5f55\uff0c\u4f46\u901a\u5e38\u4f7f\u7528\u641c\u7d22\u529f\u80fd\u6216\u641c\u7d22\u5f15\u64ce\u66f4\u5bb9\u6613\u627e\u5230\u4f60\u9700\u8981\u7684\u5185\u5bb9\u3002\u5f53\u4f60\u5b8c\u6210\u4e86LearnCpp.com\u4e0a\u7684\u6559\u7a0b\u4e4b\u540e\uff0c\u8be5\u9875\u9762\u662f\u4f60\u7edd\u4f73\u7684\u4e0b\u4e00\u7ad9\u76ee\u7684\u5730\u3002\u5728\u8fd9\u91cc\u4f60\u53ef\u4ee5\u6df1\u5165\u7814\u7a76\u6807\u51c6\u5e93\uff0c\u5e76\u67e5\u770bC++\u8fd8\u63d0\u4f9b\u4e86\u54ea\u4e9b\u4f60\u8fd8\u4e0d\u77e5\u9053\u7684\u529f\u80fd\u3002 \u8868\u7684\u4e0a\u534a\u90e8\u5206\u663e\u793a\u4e86C++\u5f53\u524d\u7684\u7279\u6027\uff0c\u800c\u4e0b\u534a\u90e8\u5206\u5219\u5c55\u793a\u4e86\u6280\u672f\u89c4\u8303\uff0c\u8fd9\u4e9b\u7279\u6027\u53ef\u80fd\u4f1a\u5728\u672a\u6765\u7684\u7248\u672c\u4e2d\u6dfb\u52a0\u5230C++\uff0c\u4e5f\u53ef\u80fd\u4e0d\u4f1a\u6dfb\u52a0\u5230C++\uff0c\u6216\u8005\u5df2\u7ecf\u90e8\u5206\u88ab\u8bed\u8a00\u63a5\u53d7\u3002\u5982\u679c\u60a8\u60f3\u4e86\u89e3\u5373\u5c06\u63a8\u51fa\u7684\u65b0\u529f\u80fd\uff0c\u8fd9\u5c06\u975e\u5e38\u6709\u7528\u3002 \u4eceC++ 11\u5f00\u59cb\uff0ccppreference\u4f1a\u7528\u5f15\u5165\u8be5\u529f\u80fd\u7684\u6807\u51c6\u7248\u672c\u6765\u6807\u8bb0\u5bf9\u5e94\u7684\u529f\u80fd\u3002\u6807\u51c6\u7248\u672c\u662f\u4e0a\u9762\u56fe\u7247\u4e2d\u4e00\u4e9b\u94fe\u63a5\u65c1\u8fb9\u7684\u7eff\u8272\u5c0f\u6570\u5b57\u3002\u90a3\u4e9b\u6ca1\u6709\u6807\u8bb0\u6807\u51c6\u7248\u672c\u7684\u529f\u80fd\uff0c\u5219\u662f\u5728 C++ 98/03 \u4e2d\u5c31\u5df2\u7ecf\u6709\u7684\u529f\u80fd\u3002\u6211\u4eec\u4e0d\u4ec5\u4f1a\u5728\u603b\u89c8\u9875\u9762\u770b\u5230\u6807\u51c6\u7248\u672c\u53f7\uff0c\u5b83\u5728cppreference\u4e0a\u968f\u5904\u53ef\u89c1\uff0c\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u786e\u5207\u5730\u77e5\u9053\u5728\u7279\u5b9a\u7684C++\u7248\u672c\u4e2d\u4ec0\u4e48\u529f\u80fd\u53ef\u7528\uff0c\u4ec0\u4e48\u529f\u80fd\u4e0d\u53ef\u7528\u3002 \u63d0\u9192 C++ \u7684\u7248\u672c\u6709\uff1aC++98, C++03, C++11, C++14, C++17, C++20\u3002C++23 \u662f\u4e0b\u4e00\u4e2a\u8fed\u4ee3\u7684\u7248\u672c\u53f7\u3002 \u6ce8\u610f \u5982\u679c\u4f60\u4f7f\u7528\u641c\u7d22\u5f15\u64ce\uff0c\u800c\u67d0\u4e2a\u6280\u672f\u89c4\u8303\u521a\u521a\u88ab\u63a5\u53d7\u4e3a\u6807\u51c6\uff0c\u90a3\u4e48\u4f60\u53ef\u80fd\u4f1a\u94fe\u63a5\u5230\u67d0\u4e2a\u6280\u672f\u89c4\u8303\u800c\u4e0d\u662f\u5b98\u65b9\u53c2\u8003\uff0c\u8fd9\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002 \u5c0f\u8d34\u58eb cppreference\u4e0d\u4ec5\u662fC++\u7684\u53c2\u8003\uff0c\u4e5f\u662fC\u7684\u53c2\u8003\u3002\u56e0\u4e3aC++\u548cC\u5171\u4eab\u4e00\u4e9b\u51fd\u6570(\u53ef\u80fd\u6709\u6240\u4e0d\u540c)\uff0c\u4f60\u53ef\u80fd\u4f1a\u641c\u7d22\u540e\u88ab\u5f15\u5bfc\u5230C\u8bed\u8a00\u7684\u53c2\u8003\u4e2d\u3002\u5728cppreference\u9876\u90e8\u7684URL\u548c\u5bfc\u822a\u680f\u603b\u662f\u4f1a\u663e\u793a\u60a8\u6b63\u5728\u6d4f\u89c8\u7684\u662fC\u8fd8\u662fC++\u53c2\u8003\u3002 \u6848\u4f8b\u4e00\uff1a std::string::length \u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u9996\u5148\u7814\u7a76\u4e00\u4e2a\u5df2\u7ecf\u5b66\u4e60\u8fc7\u7684\u51fd\u6570\u2014\u2014 std::string::length \uff0c\u5b83\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002 \u5728cppreference\u7684\u53f3\u4e0a\u89d2\uff0c\u641c\u7d22\u201cstring\u201d\u3002\u6b64\u65f6\u505a\u4f1a\u663e\u793a\u4e00\u957f\u4e32\u7c7b\u578b\u548c\u51fd\u6570\uff0c\u76ee\u524d\u53ea\u6709\u6700\u4e0a\u9762\u7684\u662f\u76f8\u5173\u7684\u3002 \u5f53\u6211\u4eec\u4e5f\u53ef\u4ee5\u76f4\u63a5\u641c\u7d22\u201cstring length\u201d\uff0c\u4e0d\u8fc7\u4e3a\u4e86\u5411\u4f60\u66f4\u5168\u9762\u5730\u5c55\u793a\u5404\u79cd\u60c5\u51b5\uff0c\u8fd9\u91cc\u6211\u4eec\u6545\u610f\u820d\u8fd1\u6c42\u8fdc\u4e86\u3002\u70b9\u51fb \u201cStrings library\u201d \u4f1a\u5c06\u6211\u4eec\u5e26\u5230\u4e00\u4e2a\u9875\u9762\uff0c\u8be5\u9875\u9762\u5c55\u793a\u4e86C++\u652f\u6301\u7684\u5404\u79cd\u5b57\u7b26\u4e32\u529f\u80fd\u3002 \u5728 \u201c std::basic_string \u201d \u5c0f\u8282\u4e0b\u65b9\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e00\u7cfb\u5217\u7684 typedefs \uff0c\u5176\u4e2d\u5c31\u6709 std::string \u3002 \u70b9\u51fb \u201c std::string \u201d \u4f1a\u8df3\u8f6c\u5230 std::basic_string \u9875\u9762\u3002\u7f51\u7ad9\u4e2d\u5e76\u6ca1\u6709\u5173\u4e8e std::string \u7684\u9875\u9762\u56e0\u4e3a std::string \u662f typedef \u7684 std::basic_string \u3002\u5728 typedef \u5217\u8868\u4e2d\u53ef\u4ee5\u770b\u5230\u3002 \u8868\u793a\u8be5\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u5176\u7c7b\u578b\u90fd\u662f char \u3002\u5728\u8fd9\u91cc\u53ef\u4ee5\u770b\u5230\uff0cC++\u7684\u5b57\u7b26\u4e32\uff0c\u5176\u5b57\u7b26\u652f\u6301\u4e0d\u540c\u79cd\u7c7b\u7684\u5b57\u7b26\u3002\u5f53\u9700\u8981\u4f7f\u7528Unicode\u800c\u975eASCII\u65f6\uff0c\u8fd9\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002 \u8fd8\u662f\u8fd9\u4e2a\u9875\u9762\uff0c\u7ee7\u7eed\u5f80\u4e0b\u770b\uff0c\u6b64\u65f6\u53ef\u4ee5\u770b\u5230 \u6210\u5458\u51fd\u6570\u5217\u8868 Member functions (\u8be5\u7c7b\u578b\u652f\u6301\u7684\u65b9\u6cd5)\u3002\u5982\u679c\u4f60\u60f3\u77e5\u9053\u67d0\u4e2a\u7c7b\u578b\u53ef\u4ee5\u505a\u4ec0\u4e48\u6837\u7684\u64cd\u4f5c\uff0c\u76f4\u63a5\u770b\u8fd9\u4e2a\u8868\u5c31\u975e\u5e38\u6e05\u695a\u4e86\u3002\u5728\u8fd9\u4e2a\u8868\u4e2d\uff0c\u53ef\u4ee5\u627e\u5230 length \u65b9\u6cd5(\u548c size )\u3002 \u70b9\u51fb\u8be5\u65b9\u6cd5\u7684\u94fe\u63a5\uff0c\u9875\u9762\u8df3\u8f6c\u5230 length \u548c size \u7684\u4ecb\u7ecd \uff08\u8fd9\u4e24\u5176\u5b9e\u662f\u4e00\u56de\u4e8b\uff09\u3002 \u6bcf\u4e2a\u9875\u9762\u7684\u9876\u90e8\u9996\u5148\u5448\u73b0\u7684\u662f\u529f\u80fd\u548c\u8bed\u6cd5\u3001\u91cd\u8f7d\u6216\u58f0\u660e\u7684\u7b80\u77ed\u6458\u8981\u3002 \u8fd9\u4e2a\u9875\u9762\u7684\u6807\u9898\u663e\u793a\u4e86\u7c7b\u548c\u51fd\u6570\u7684\u540d\u79f0\u4ee5\u53ca\u6240\u6709\u6a21\u677f\u53c2\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u5ffd\u7565\u8fd9\u90e8\u5206\u3002\u5728\u6807\u9898\u4e0b\u9762\uff0c\u53ef\u4ee5\u627e\u5230\u6240\u6709\u7684\u91cd\u8f7d\u51fd\u6570(\u5177\u6709\u76f8\u540c\u540d\u79f0\u7684\u51fd\u6570\u7684\u4e0d\u540c\u7248\u672c)\u4ee5\u53ca\u5b83\u4eec\u5e94\u7528\u7684\u8bed\u8a00\u6807\u51c6\u3002 \u518d\u4e0b\u9762\uff0c\u662f\u51fd\u6570\u7684 \u5f62\u53c2 \u5217\u8868\uff0c\u4ee5\u53ca\u8fd4\u56de\u503c\u7c7b\u578b\u3002 \u56e0\u4e3a std::string::length \u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u51fd\u6570\uff0c\u6240\u4ee5\u8be5\u9875\u9762\u4e2d\u5e76\u6ca1\u6709\u5f88\u591a\u5185\u5bb9\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u9875\u9762\u4f1a\u5305\u542b\u4f7f\u7528\u4e86\u76f8\u5173\u7279\u6027\u7684\u4f8b\u5b50\uff0c\u4f8b\u5982\uff1a \u9664\u975e\u4f60\u5df2\u7ecf\u5b8c\u5168\u638c\u63e1C++\uff0c\u5426\u5219\u793a\u4f8b\u4e2d\u603b\u4f1a\u6709\u4f60\u8fd8\u6ca1\u89c1\u8fc7\u7684\u7279\u6027\u3002\u5982\u679c\u6709\u8db3\u591f\u591a\u7684\u4f8b\u5b50\uff0c\u4f60\u53ef\u80fd\u80fd\u591f\u7406\u89e3\u8db3\u591f\u591a\u7684\u5185\u5bb9\uff0c\u4ece\u800c\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u51fd\u6570\u53ca\u5176\u529f\u80fd\u3002\u5982\u679c\u793a\u4f8b\u592a\u590d\u6742\uff0c\u4e5f\u53ef\u4ee5\u5728\u5176\u4ed6\u5730\u65b9\u641c\u7d22\u793a\u4f8b\uff0c\u6216\u8005\u9605\u8bfb\u4e0d\u7406\u89e3\u90e8\u5206\u7684\u53c2\u8003\u8d44\u6599(\u53ef\u4ee5\u5355\u51fb\u793a\u4f8b\u4e2d\u7684\u51fd\u6570\u548c\u7c7b\u578b\u6765\u67e5\u770b\u5b83\u4eec\u7684\u529f\u80fd)\u3002 \u73b0\u5728\u6211\u4eec\u77e5\u9053\u4e86 std::string::length \u7684\u4f5c\u7528\uff0c\u4e0d\u8fc7\u8fd9\u6ca1\u4ec0\u4e48\u65b0\u9c9c\u7684\uff0c\u6bd5\u7adf\u6211\u4eec\u4e4b\u524d\u5c31\u5df2\u7ecf\u638c\u63e1\u8be5\u51fd\u6570\u4e86\u3002\u63a5\u4e0b\u6765\u770b\u770b\u65b0\u7684\u4e1c\u897f\u5427\uff01 \u6848\u4f8b\u4e8c\uff1a std::cin.ignore \u5728 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7 std::cin.ignore \uff0c\u5b83\u53ef\u4ee5\u5ffd\u7565\u6362\u884c\u4e4b\u524d\u7684\u5168\u90e8\u5185\u5bb9\u3002\u8fd9\u4e2a\u51fd\u6570\u7684\u5176\u4e2d\u4e00\u4e2a\u5f62\u53c2\u53c8\u957f\u53c8\u5570\u55e6\uff0c\u5f88\u96be\u88ab\u8bb0\u4f4f\u3002\u662f\u4ec0\u4e48\u6765\u7740\uff1f\u600e\u4e48\u7528\u6765\u7740\uff1f\u8ba9\u6211\u4eec\u67e5\u67e5\u770b\u3002 \u5728\u641c\u7d22\u6765\u8f93\u5165 \u201cstd::cin.ignore\u201d \uff1a std::cin, std::wcin - \u6211\u4eec\u8981\u627e\u7684\u662f .ignore \uff0c\u800c\u4e0d\u662f std::cin \uff1b std::basic_istream::ignore - \u8fd9\u662f\u5565\uff1f\u770b\u4e0d\u61c2\u5148\u8df3\u8fc7\u5427\uff1b std::ignore - \u4e0d\u662f\u8fd9\u4e2a\uff1b std::basic_istream - \u4e5f\u4e0d\u662f\u8fd9\u4e2a\u3002 \u6ca1\u641c\u5230\uff1f\u73b0\u5728\u548b\u6574\uff1f\u8ba9\u6211\u4eec\u5148\u8fdb\u5165 std::cin \uff0c\u7136\u540e\u987a\u85e4\u6478\u74dc\u5427\u3002\u8fd9\u4e2a\u9875\u9762\u4e0a\u4e5f\u6ca1\u6709\u660e\u663e\u4e0e\u6211\u4eec\u9700\u8981\u627e\u7684\u4e1c\u897f\u76f8\u5173\u7684\u5185\u5bb9\u3002\u5728\u9875\u9762\u7684\u9876\u90e8\uff0c\u53ef\u4ee5\u770b\u5230 std::cin \u548c std::wcin \u7684\u58f0\u660e\uff0c\u540c\u65f6\u5b83\u8fd8\u544a\u8bc9\u6211\u4eec\u4f7f\u7528 std::cin \u65f6\u5e94\u8be5\u5305\u542b\u54ea\u4e2a\u5934\u6587\u4ef6\uff1a \u53ef\u4ee5\u770b\u51fa std::cin \u662f std::istream \u7c7b\u578b\u7684\u4e00\u4e2a\u5bf9\u8c61\u3002\u63a5\u4e0b\u6765\u70b9\u51fb std::istream \u7684\u94fe\u63a5\uff1a \u7b49\u7b49\uff01 \u6211\u4e4b\u524d\u641c\u7d22 \u201c std::cin.ignore \u201d \u7684\u65f6\u5019\u662f\u641c\u5230\u8fc7 std::basic_istream \u7684\u3002\u539f\u6765 istream \u5c31\u662f basic_istream \u7684 typedef \u554a\u3002\u4e5f\u8bb8\u6211\u4eec\u5e76\u6ca1\u6709\u641c\u7d22\u9519\uff01 \u9875\u9762\u5411\u4e0b\u6ed1\u52a8\uff0c\u53ef\u4ee5\u770b\u5230\u5f88\u591a\u719f\u6089\u7684\u51fd\u6570\uff1a \u8fd9\u91cc\u5f88\u591a\u51fd\u6570\u6211\u4eec\u90fd\u7528\u8fc7\uff1a operator>> , get , getline , ignore \u3002\u53ef\u4ee5\u6d4f\u89c8\u4e00\u4e0b\u8fd8\u6709\u54ea\u4e9b\u51fd\u6570\uff0c\u7136\u540e\u70b9\u51fb ignore \uff0c\u8fd9\u5c31\u662f\u6211\u4eec\u8981\u627e\u7684\u51fd\u6570\u3002 \u5728\u9875\u9762\u7684\u9876\u90e8\u6709\u51fd\u6570\u7b7e\u540d\u3001\u51fd\u6570\u7b80\u4ecb\u53ca\u5176\u4e24\u4e2a\u53c2\u6570\u7684\u529f\u80fd\u63cf\u8ff0\u3002\u5f62\u53c2\u540e\u9762\u7684 = \u7b26\u53f7\u8868\u793a\u9ed8\u8ba4\u5b9e\u53c2(\u5728\u8bfe\u7a0b 8.12 -\u9ed8\u8ba4\u53c2\u6570 \u4e2d\u8ba8\u8bba\u8fc7)\u3002\u5982\u679c\u6ca1\u6709\u4e3a\u5177\u6709\u9ed8\u8ba4\u503c\u7684\u5f62\u53c2\u63d0\u4f9b\u5b9e\u53c2\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002 \u63a5\u4e0b\u6765\u7684\u65e0\u5e8f\u5217\u8868\u4e2d\u5305\u542b\u4e09\u6761\u5185\u5bb9\u3002\u53ef\u4ee5\u770b\u5230 std::numeric_limits::max() \u5728 std::cin.ignore \u4e2d\u662f\u7531\u7279\u6b8a\u542b\u4e49\u7684\uff0c\u5b83\u53ef\u4ee5\u5ffd\u7565\u5b57\u7b26\u8ba1\u6570\u68c0\u67e5\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c std::cin.ignore \u4f1a\u5ffd\u7565\u5206\u9694\u7b26\u524d\u6240\u6709\u7684\u5b57\u7b26\uff0c\u6216\u8005\u76f4\u5230\u8017\u5c3d\u9700\u8981\u5173\u6ce8\u7684\u5b57\u7b26\u4e2a\u6570\u3002 \u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u77e5\u9053\u8fd9\u4e2a\u51fd\u6570\u662f\u5e72\u561b\u7684\uff0c\u4f46\u662f\u5fd8\u8bb0\u4e86\u53c2\u6570\u548c\u8fd4\u56de\u503c\uff0c\u6b64\u65f6\u4f60\u4e0d\u9700\u8981\u9605\u8bfb\u51fd\u6570\u7684\u5b8c\u6574\u63cf\u8ff0\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f60\u53ea\u9700\u6d4f\u89c8\u4e00\u4e0b\u53c2\u6570\u5217\u8868\u548c\u8fd4\u56de\u503c\u5c31\u8db3\u591f\u4e86\u3002 \u8fd9\u91cc\u7684\u53c2\u6570\u4ecb\u7ecd\u5f88\u7b80\u6d01\u3002\u5b83\u5e76\u6ca1\u6709\u5305\u62ec std::numeric_limits::max() \u7684\u7279\u6b8a\u542b\u4e49\uff0c\u4f46\u662f\u8fd9\u4e9b\u7b80\u6d01\u7684\u53c2\u6570\u4ecb\u7ecd\u4e00\u822c\u60c5\u51b5\u4e0b\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u56de\u5fc6\u8d77\u4e00\u4e9b\u5173\u952e\u7684\u4fe1\u606f\u3002 \u6848\u4f8b\u4e09\uff1a\u8bed\u6cd5 \u9664\u4e86\u6807\u51c6\u5e93\uff0ccppreference\u8fd8\u8bb0\u5f55\u4e86C++\u8bed\u8a00\u7684\u8bed\u6cd5\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u6709\u6548\u7684\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include int getUserInput () { int i {}; std :: cin >> i ; return i ; } int main () { std :: cout << \"How many bananas did you eat today? \\n \" ; if ( int iBananasEaten { getUserInput () }; iBananasEaten <= 2 ) { std :: cout << \"Yummy \\n \" ; } else { std :: cout << iBananasEaten << \" is a lot! \\n \" ; } return 0 ; } \u4e3a\u4ec0\u4e48\u5728 if-statement \u7684\u6761\u4ef6\u4e2d\u6709\u4e00\u4e2a\u53d8\u91cf\u5b9a\u4e49\uff1f\u5728\u641c\u7d22\u5f15\u64ce\u4e2d\u641c\u7d22\u201ccppreference if statement\u201d\u6765\u4e86\u89e3\u5b83\u7684\u529f\u80fd\u3002\u641c\u7d22\u5f15\u64ce\u4f1a\u5c06\u6211\u4eec\u5e26\u5230 if statements \u3002\u5728\u9875\u9762\u5f00\u59cb\u7684\u5730\u65b9\u5c31\u6709\u5176\u76f8\u5173\u7684\u8bed\u6cd5\u8bf4\u660e\u3002 \u5728\u9875\u9762\u7684\u53f3\u534a\u90e8\u5206\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e0d\u540c\u8bed\u6cd5\u4e0eC++\u7248\u672c\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u5bf9\u4e8e if-statement \uff0c\u4eceC++17\u5f00\u59cb\uff0c\u5982\u679c\u4f60\u5ffd\u7565\u6240\u6709\u53ef\u9009\u7684\u90e8\u5206\uff0c\u90a3\u4e48\u5c31\u662f\u6211\u4eec\u5e38\u7528\u7684 if-statement \u7684\u5f62\u5f0f\u3002\u5728 condition \u4e4b\u524d\uff0c\u6709\u4e00\u4e2a\u53ef\u9009\u7684\u90e8\u5206\u79f0\u4e3a init-statement \uff0c\u770b\u8d77\u6765\u8fd9\u90e8\u5206\u5c31\u662f\u548c\u4e4b\u524d\u4f8b\u5b50\u4e2d\u7684\u4ee3\u7801\u662f\u76f8\u5173\u7684\u3002 1 2 if ( init - statement condition ) statement - true if ( init - statement condition ) statement - true else statement - false \u5728\u8bed\u6cd5\u53c2\u8003\u4e0b\u65b9\uff0c\u6709\u5bf9\u8bed\u6cd5\u7684\u6bcf\u4e2a\u90e8\u5206\u7684\u89e3\u91ca\uff0c\u5176\u4e2d\u5c31\u5305\u62ec init-statement \u2014\u2014\u5b83\u901a\u5e38\u662f\u4e00\u4e2a\u5e26\u6709\u521d\u59cb\u5316\u503c\u7684\u53d8\u91cf\u58f0\u660e\u3002 \u4e0b\u9762\u662f if-statements \u7684\u8bed\u6cd5\u89e3\u91ca\u548c\u4e00\u4e9b\u7b80\u5355\u7684\u4f8b\u5b50: \u6211\u4eec\u4e86\u89e3 if-statements \u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u4f46\u662f\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u6ca1\u6709\u5305\u542b init-statement \u7684\u7528\u6cd5\u3002\u6240\u4ee5\u63a5\u7740\u5411\u4e0b\u770b\uff0c\u627e\u627e\u770b\u6709\u6ca1\u6709\u4e13\u95e8\u4ecb\u7ecd\u5e26\u521d\u59cb\u5316\u503c\u7684 if-statements \u7684\u5185\u5bb9\u3002 \u8fd9\u91cc\u9996\u5148\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 init-statement \uff0c\u73b0\u5728\u6211\u4eec\u5c31\u77e5\u9053\u4e4b\u524d\u4ee3\u7801\u4e2d\u7684if\u8bed\u53e5\u662f\u600e\u4e48\u56de\u4e8b\u4e86\u3002\u5176\u5b9e\u5b83\u5c31\u662f\u5c06\u4e00\u822c\u7684\u53d8\u91cf\u58f0\u660e\u548cif\u8bed\u53e5\u5408\u5e76\u8d77\u6765\u7f62\u4e86\u3002 \u63a5\u4e0b\u6765\u7684\u53e5\u5b50\u5f88\u6709\u610f\u601d\uff0c\u5b83\u544a\u8bc9\u6211\u4eec init-statement \u58f0\u660e\u7684\u53d8\u91cf\uff0c\u5728if\u8bed\u53e5\u7684true\u548cfalse\u8bed\u53e5\u5757\u4e2d\u90fd\u80fd\u4f7f\u7528\u3002\u5982\u679c\u4f60\u4e4b\u524d\u5929\u771f\u7684\u8ba4\u4e3a\u8fd9\u4e2a\u53d8\u91cf\u53ea\u80fd\u5728true\u8bed\u53e5\u5757\u4e2d\u4f7f\u7528\u7684\u8bdd\uff0c\u8be5\u8bf4\u660e\u5e94\u8be5\u4f1a\u8ba9\u4f60\u770b\u5230\u6709\u70b9\u610f\u5916\u3002 init-statement \u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86\u4e00\u4e9b\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u8fc7\u7684\u7279\u6027\uff0c\u4f46\u6211\u4eec\u5e76\u4e0d\u9700\u8981\u770b\u61c2\u6240\u6709\u5185\u5bb9\u5c31\u53ef\u4ee5\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 init-statement \u3002\u6240\u4ee5\u76ee\u524d\u53ef\u4ee5\u5148\u8df3\u8fc7\u8ba9\u4eba\u56f0\u60d1\u7684\u5185\u5bb9\uff0c\u4e13\u6ce8\u4e8e\u6211\u4eec\u719f\u7cfb\u7684\u90e8\u5206\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // Iterators, we don't know them. Skip. if ( auto it = m . find ( 10 ); it != m . end ()) { return it -> second . size (); } // [10], what's that? Skip. if ( char buf [ 10 ]; std :: fgets ( buf , 10 , stdin )) { m [ 0 ] += buf ; } // std::lock_guard, we don't know that, but it's some type. We know what types are! if ( std :: lock_guard lock ( mx ); shared_flag ) { unsafe_ping (); shared_flag = false ; } // This is easy, that's an int! if ( int s ; int count = ReadBytesWithSignal ( & s )) { publish ( count ); raise ( s ); } // Whew, no thanks! if ( auto keywords = { \"if\" , \"for\" , \"while\" }; std :: any_of ( keywords . begin (), keywords . end (), [ & s ]( const char * kw ) { return s == kw ; })) { std :: cerr << \"Token must not be a keyword \\n \" ; } \u6700\u7b80\u5355\u7684\u4f8b\u5b50\u4f3c\u4e4e\u662f\u4f7f\u7528\u4e86 int \u7684\u90a3\u4e2a\uff0c\u4f46\u662f\u5b83\u7684\u5206\u53f7\u540e\u9762\u53c8\u662f\u4e00\u4e2a\u5b9a\u4e49\uff0c\u770b\u4e0a\u53bb\u6709\u70b9\u5947\u602a\u3002\u518d\u770b\u4e00\u4e2a std::lock_guard \u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 if ( std :: lock_guard lock ( mx ); shared_flag ) { unsafe_ping (); shared_flag = false ; } \u8fd9\u91cc\u5b9a\u4e49\u4e86\u53d8\u91cf( lock )\uff0c\u7136\u540e\u662f\u5206\u53f7\uff0c\u7136\u540e\u662f\u6761\u4ef6\u3002\u8fd9\u4e2a\u53cd\u5012\u662f\u6700\u63a5\u8fd1\u6211\u4eec\u4ee3\u7801\u7684\u4f8b\u5b50\u3002 \u5173\u4e8e cppreference \u51c6\u786e\u6027\u7684\u6ce8\u610f\u4e8b\u9879 \u5176\u5b9e\uff0ccppreference \u5e76\u4e0d\u662f\u4e00\u4e2a\u5b98\u65b9\u6587\u6863\u6e90\u2014\u2014\u76f8\u53cd\uff0c\u5b83\u662f\u4e00\u4e2awiki\u767e\u79d1\u5e94\u7528\u3002\u4f7f\u7528wiki\uff0c\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u6dfb\u52a0\u548c\u4fee\u6539\u5185\u5bb9\u2014\u2014\u5185\u5bb9\u6765\u81ea\u793e\u533a\u3002\u867d\u7136\u8fd9\u610f\u5473\u7740\u4eba\u4eec\u5f88\u5bb9\u6613\u6dfb\u52a0\u9519\u8bef\u7684\u4fe1\u606f\uff0c\u4f46\u9519\u8bef\u7684\u4fe1\u606f\u901a\u5e38\u4f1a\u5f88\u5feb\u88ab\u53d1\u73b0\u5e76\u5220\u9664\uff0c\u4f7fcppreference\u6210\u4e3a\u4e00\u4e2a\u53ef\u9760\u7684\u6765\u6e90\u3002 C++\u6807\u51c6 \u662f\u552f\u4e00\u7684\u5b98\u65b9\u6587\u6863( github )\u3002\u867d\u7136\u5b83\u662f\u6b63\u5f0f\u7684\u6587\u6863\uff0c\u4f46\u662f\u5b83\u5e76\u4e0d\u662f\u4e00\u4e2a\u6613\u7528\u7684\u53c2\u8003\u624b\u518c\u3002","title":"10.y - \u5b66\u4f1a\u4f7f\u7528C++\u53c2\u8003\u624b\u518c"},{"location":"10-y-using-a-language-reference/#_1","text":"Cppreference \u7684\u9996\u9875\u662f\u4e00\u4e2a \u603b\u89c8\u9875\u9762 \u5176\u4e2d\u5217\u51fa\u4e86\u8bed\u8a00\u7684\u6838\u5fc3\u529f\u80fd\u548c\u6807\u51c6\u5e93\uff1a \u867d\u7136\u5728\u8fd9\u4e2a\u9875\u9762\u4e2d\uff0c\u4f60\u53ef\u4ee5\u770b\u5230cppreference\u63d0\u4f9b\u7684\u6240\u6709\u5185\u5bb9\u7684\u76ee\u5f55\uff0c\u4f46\u901a\u5e38\u4f7f\u7528\u641c\u7d22\u529f\u80fd\u6216\u641c\u7d22\u5f15\u64ce\u66f4\u5bb9\u6613\u627e\u5230\u4f60\u9700\u8981\u7684\u5185\u5bb9\u3002\u5f53\u4f60\u5b8c\u6210\u4e86LearnCpp.com\u4e0a\u7684\u6559\u7a0b\u4e4b\u540e\uff0c\u8be5\u9875\u9762\u662f\u4f60\u7edd\u4f73\u7684\u4e0b\u4e00\u7ad9\u76ee\u7684\u5730\u3002\u5728\u8fd9\u91cc\u4f60\u53ef\u4ee5\u6df1\u5165\u7814\u7a76\u6807\u51c6\u5e93\uff0c\u5e76\u67e5\u770bC++\u8fd8\u63d0\u4f9b\u4e86\u54ea\u4e9b\u4f60\u8fd8\u4e0d\u77e5\u9053\u7684\u529f\u80fd\u3002 \u8868\u7684\u4e0a\u534a\u90e8\u5206\u663e\u793a\u4e86C++\u5f53\u524d\u7684\u7279\u6027\uff0c\u800c\u4e0b\u534a\u90e8\u5206\u5219\u5c55\u793a\u4e86\u6280\u672f\u89c4\u8303\uff0c\u8fd9\u4e9b\u7279\u6027\u53ef\u80fd\u4f1a\u5728\u672a\u6765\u7684\u7248\u672c\u4e2d\u6dfb\u52a0\u5230C++\uff0c\u4e5f\u53ef\u80fd\u4e0d\u4f1a\u6dfb\u52a0\u5230C++\uff0c\u6216\u8005\u5df2\u7ecf\u90e8\u5206\u88ab\u8bed\u8a00\u63a5\u53d7\u3002\u5982\u679c\u60a8\u60f3\u4e86\u89e3\u5373\u5c06\u63a8\u51fa\u7684\u65b0\u529f\u80fd\uff0c\u8fd9\u5c06\u975e\u5e38\u6709\u7528\u3002 \u4eceC++ 11\u5f00\u59cb\uff0ccppreference\u4f1a\u7528\u5f15\u5165\u8be5\u529f\u80fd\u7684\u6807\u51c6\u7248\u672c\u6765\u6807\u8bb0\u5bf9\u5e94\u7684\u529f\u80fd\u3002\u6807\u51c6\u7248\u672c\u662f\u4e0a\u9762\u56fe\u7247\u4e2d\u4e00\u4e9b\u94fe\u63a5\u65c1\u8fb9\u7684\u7eff\u8272\u5c0f\u6570\u5b57\u3002\u90a3\u4e9b\u6ca1\u6709\u6807\u8bb0\u6807\u51c6\u7248\u672c\u7684\u529f\u80fd\uff0c\u5219\u662f\u5728 C++ 98/03 \u4e2d\u5c31\u5df2\u7ecf\u6709\u7684\u529f\u80fd\u3002\u6211\u4eec\u4e0d\u4ec5\u4f1a\u5728\u603b\u89c8\u9875\u9762\u770b\u5230\u6807\u51c6\u7248\u672c\u53f7\uff0c\u5b83\u5728cppreference\u4e0a\u968f\u5904\u53ef\u89c1\uff0c\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u786e\u5207\u5730\u77e5\u9053\u5728\u7279\u5b9a\u7684C++\u7248\u672c\u4e2d\u4ec0\u4e48\u529f\u80fd\u53ef\u7528\uff0c\u4ec0\u4e48\u529f\u80fd\u4e0d\u53ef\u7528\u3002 \u63d0\u9192 C++ \u7684\u7248\u672c\u6709\uff1aC++98, C++03, C++11, C++14, C++17, C++20\u3002C++23 \u662f\u4e0b\u4e00\u4e2a\u8fed\u4ee3\u7684\u7248\u672c\u53f7\u3002 \u6ce8\u610f \u5982\u679c\u4f60\u4f7f\u7528\u641c\u7d22\u5f15\u64ce\uff0c\u800c\u67d0\u4e2a\u6280\u672f\u89c4\u8303\u521a\u521a\u88ab\u63a5\u53d7\u4e3a\u6807\u51c6\uff0c\u90a3\u4e48\u4f60\u53ef\u80fd\u4f1a\u94fe\u63a5\u5230\u67d0\u4e2a\u6280\u672f\u89c4\u8303\u800c\u4e0d\u662f\u5b98\u65b9\u53c2\u8003\uff0c\u8fd9\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002 \u5c0f\u8d34\u58eb cppreference\u4e0d\u4ec5\u662fC++\u7684\u53c2\u8003\uff0c\u4e5f\u662fC\u7684\u53c2\u8003\u3002\u56e0\u4e3aC++\u548cC\u5171\u4eab\u4e00\u4e9b\u51fd\u6570(\u53ef\u80fd\u6709\u6240\u4e0d\u540c)\uff0c\u4f60\u53ef\u80fd\u4f1a\u641c\u7d22\u540e\u88ab\u5f15\u5bfc\u5230C\u8bed\u8a00\u7684\u53c2\u8003\u4e2d\u3002\u5728cppreference\u9876\u90e8\u7684URL\u548c\u5bfc\u822a\u680f\u603b\u662f\u4f1a\u663e\u793a\u60a8\u6b63\u5728\u6d4f\u89c8\u7684\u662fC\u8fd8\u662fC++\u53c2\u8003\u3002","title":"\u603b\u89c8"},{"location":"10-y-using-a-language-reference/#stdstringlength","text":"\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u9996\u5148\u7814\u7a76\u4e00\u4e2a\u5df2\u7ecf\u5b66\u4e60\u8fc7\u7684\u51fd\u6570\u2014\u2014 std::string::length \uff0c\u5b83\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002 \u5728cppreference\u7684\u53f3\u4e0a\u89d2\uff0c\u641c\u7d22\u201cstring\u201d\u3002\u6b64\u65f6\u505a\u4f1a\u663e\u793a\u4e00\u957f\u4e32\u7c7b\u578b\u548c\u51fd\u6570\uff0c\u76ee\u524d\u53ea\u6709\u6700\u4e0a\u9762\u7684\u662f\u76f8\u5173\u7684\u3002 \u5f53\u6211\u4eec\u4e5f\u53ef\u4ee5\u76f4\u63a5\u641c\u7d22\u201cstring length\u201d\uff0c\u4e0d\u8fc7\u4e3a\u4e86\u5411\u4f60\u66f4\u5168\u9762\u5730\u5c55\u793a\u5404\u79cd\u60c5\u51b5\uff0c\u8fd9\u91cc\u6211\u4eec\u6545\u610f\u820d\u8fd1\u6c42\u8fdc\u4e86\u3002\u70b9\u51fb \u201cStrings library\u201d \u4f1a\u5c06\u6211\u4eec\u5e26\u5230\u4e00\u4e2a\u9875\u9762\uff0c\u8be5\u9875\u9762\u5c55\u793a\u4e86C++\u652f\u6301\u7684\u5404\u79cd\u5b57\u7b26\u4e32\u529f\u80fd\u3002 \u5728 \u201c std::basic_string \u201d \u5c0f\u8282\u4e0b\u65b9\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e00\u7cfb\u5217\u7684 typedefs \uff0c\u5176\u4e2d\u5c31\u6709 std::string \u3002 \u70b9\u51fb \u201c std::string \u201d \u4f1a\u8df3\u8f6c\u5230 std::basic_string \u9875\u9762\u3002\u7f51\u7ad9\u4e2d\u5e76\u6ca1\u6709\u5173\u4e8e std::string \u7684\u9875\u9762\u56e0\u4e3a std::string \u662f typedef \u7684 std::basic_string \u3002\u5728 typedef \u5217\u8868\u4e2d\u53ef\u4ee5\u770b\u5230\u3002 \u8868\u793a\u8be5\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u5176\u7c7b\u578b\u90fd\u662f char \u3002\u5728\u8fd9\u91cc\u53ef\u4ee5\u770b\u5230\uff0cC++\u7684\u5b57\u7b26\u4e32\uff0c\u5176\u5b57\u7b26\u652f\u6301\u4e0d\u540c\u79cd\u7c7b\u7684\u5b57\u7b26\u3002\u5f53\u9700\u8981\u4f7f\u7528Unicode\u800c\u975eASCII\u65f6\uff0c\u8fd9\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002 \u8fd8\u662f\u8fd9\u4e2a\u9875\u9762\uff0c\u7ee7\u7eed\u5f80\u4e0b\u770b\uff0c\u6b64\u65f6\u53ef\u4ee5\u770b\u5230 \u6210\u5458\u51fd\u6570\u5217\u8868 Member functions (\u8be5\u7c7b\u578b\u652f\u6301\u7684\u65b9\u6cd5)\u3002\u5982\u679c\u4f60\u60f3\u77e5\u9053\u67d0\u4e2a\u7c7b\u578b\u53ef\u4ee5\u505a\u4ec0\u4e48\u6837\u7684\u64cd\u4f5c\uff0c\u76f4\u63a5\u770b\u8fd9\u4e2a\u8868\u5c31\u975e\u5e38\u6e05\u695a\u4e86\u3002\u5728\u8fd9\u4e2a\u8868\u4e2d\uff0c\u53ef\u4ee5\u627e\u5230 length \u65b9\u6cd5(\u548c size )\u3002 \u70b9\u51fb\u8be5\u65b9\u6cd5\u7684\u94fe\u63a5\uff0c\u9875\u9762\u8df3\u8f6c\u5230 length \u548c size \u7684\u4ecb\u7ecd \uff08\u8fd9\u4e24\u5176\u5b9e\u662f\u4e00\u56de\u4e8b\uff09\u3002 \u6bcf\u4e2a\u9875\u9762\u7684\u9876\u90e8\u9996\u5148\u5448\u73b0\u7684\u662f\u529f\u80fd\u548c\u8bed\u6cd5\u3001\u91cd\u8f7d\u6216\u58f0\u660e\u7684\u7b80\u77ed\u6458\u8981\u3002 \u8fd9\u4e2a\u9875\u9762\u7684\u6807\u9898\u663e\u793a\u4e86\u7c7b\u548c\u51fd\u6570\u7684\u540d\u79f0\u4ee5\u53ca\u6240\u6709\u6a21\u677f\u53c2\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u5ffd\u7565\u8fd9\u90e8\u5206\u3002\u5728\u6807\u9898\u4e0b\u9762\uff0c\u53ef\u4ee5\u627e\u5230\u6240\u6709\u7684\u91cd\u8f7d\u51fd\u6570(\u5177\u6709\u76f8\u540c\u540d\u79f0\u7684\u51fd\u6570\u7684\u4e0d\u540c\u7248\u672c)\u4ee5\u53ca\u5b83\u4eec\u5e94\u7528\u7684\u8bed\u8a00\u6807\u51c6\u3002 \u518d\u4e0b\u9762\uff0c\u662f\u51fd\u6570\u7684 \u5f62\u53c2 \u5217\u8868\uff0c\u4ee5\u53ca\u8fd4\u56de\u503c\u7c7b\u578b\u3002 \u56e0\u4e3a std::string::length \u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u51fd\u6570\uff0c\u6240\u4ee5\u8be5\u9875\u9762\u4e2d\u5e76\u6ca1\u6709\u5f88\u591a\u5185\u5bb9\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u9875\u9762\u4f1a\u5305\u542b\u4f7f\u7528\u4e86\u76f8\u5173\u7279\u6027\u7684\u4f8b\u5b50\uff0c\u4f8b\u5982\uff1a \u9664\u975e\u4f60\u5df2\u7ecf\u5b8c\u5168\u638c\u63e1C++\uff0c\u5426\u5219\u793a\u4f8b\u4e2d\u603b\u4f1a\u6709\u4f60\u8fd8\u6ca1\u89c1\u8fc7\u7684\u7279\u6027\u3002\u5982\u679c\u6709\u8db3\u591f\u591a\u7684\u4f8b\u5b50\uff0c\u4f60\u53ef\u80fd\u80fd\u591f\u7406\u89e3\u8db3\u591f\u591a\u7684\u5185\u5bb9\uff0c\u4ece\u800c\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u51fd\u6570\u53ca\u5176\u529f\u80fd\u3002\u5982\u679c\u793a\u4f8b\u592a\u590d\u6742\uff0c\u4e5f\u53ef\u4ee5\u5728\u5176\u4ed6\u5730\u65b9\u641c\u7d22\u793a\u4f8b\uff0c\u6216\u8005\u9605\u8bfb\u4e0d\u7406\u89e3\u90e8\u5206\u7684\u53c2\u8003\u8d44\u6599(\u53ef\u4ee5\u5355\u51fb\u793a\u4f8b\u4e2d\u7684\u51fd\u6570\u548c\u7c7b\u578b\u6765\u67e5\u770b\u5b83\u4eec\u7684\u529f\u80fd)\u3002 \u73b0\u5728\u6211\u4eec\u77e5\u9053\u4e86 std::string::length \u7684\u4f5c\u7528\uff0c\u4e0d\u8fc7\u8fd9\u6ca1\u4ec0\u4e48\u65b0\u9c9c\u7684\uff0c\u6bd5\u7adf\u6211\u4eec\u4e4b\u524d\u5c31\u5df2\u7ecf\u638c\u63e1\u8be5\u51fd\u6570\u4e86\u3002\u63a5\u4e0b\u6765\u770b\u770b\u65b0\u7684\u4e1c\u897f\u5427\uff01","title":"\u6848\u4f8b\u4e00\uff1astd::string::length"},{"location":"10-y-using-a-language-reference/#stdcinignore","text":"\u5728 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7 std::cin.ignore \uff0c\u5b83\u53ef\u4ee5\u5ffd\u7565\u6362\u884c\u4e4b\u524d\u7684\u5168\u90e8\u5185\u5bb9\u3002\u8fd9\u4e2a\u51fd\u6570\u7684\u5176\u4e2d\u4e00\u4e2a\u5f62\u53c2\u53c8\u957f\u53c8\u5570\u55e6\uff0c\u5f88\u96be\u88ab\u8bb0\u4f4f\u3002\u662f\u4ec0\u4e48\u6765\u7740\uff1f\u600e\u4e48\u7528\u6765\u7740\uff1f\u8ba9\u6211\u4eec\u67e5\u67e5\u770b\u3002 \u5728\u641c\u7d22\u6765\u8f93\u5165 \u201cstd::cin.ignore\u201d \uff1a std::cin, std::wcin - \u6211\u4eec\u8981\u627e\u7684\u662f .ignore \uff0c\u800c\u4e0d\u662f std::cin \uff1b std::basic_istream::ignore - \u8fd9\u662f\u5565\uff1f\u770b\u4e0d\u61c2\u5148\u8df3\u8fc7\u5427\uff1b std::ignore - \u4e0d\u662f\u8fd9\u4e2a\uff1b std::basic_istream - \u4e5f\u4e0d\u662f\u8fd9\u4e2a\u3002 \u6ca1\u641c\u5230\uff1f\u73b0\u5728\u548b\u6574\uff1f\u8ba9\u6211\u4eec\u5148\u8fdb\u5165 std::cin \uff0c\u7136\u540e\u987a\u85e4\u6478\u74dc\u5427\u3002\u8fd9\u4e2a\u9875\u9762\u4e0a\u4e5f\u6ca1\u6709\u660e\u663e\u4e0e\u6211\u4eec\u9700\u8981\u627e\u7684\u4e1c\u897f\u76f8\u5173\u7684\u5185\u5bb9\u3002\u5728\u9875\u9762\u7684\u9876\u90e8\uff0c\u53ef\u4ee5\u770b\u5230 std::cin \u548c std::wcin \u7684\u58f0\u660e\uff0c\u540c\u65f6\u5b83\u8fd8\u544a\u8bc9\u6211\u4eec\u4f7f\u7528 std::cin \u65f6\u5e94\u8be5\u5305\u542b\u54ea\u4e2a\u5934\u6587\u4ef6\uff1a \u53ef\u4ee5\u770b\u51fa std::cin \u662f std::istream \u7c7b\u578b\u7684\u4e00\u4e2a\u5bf9\u8c61\u3002\u63a5\u4e0b\u6765\u70b9\u51fb std::istream \u7684\u94fe\u63a5\uff1a \u7b49\u7b49\uff01 \u6211\u4e4b\u524d\u641c\u7d22 \u201c std::cin.ignore \u201d \u7684\u65f6\u5019\u662f\u641c\u5230\u8fc7 std::basic_istream \u7684\u3002\u539f\u6765 istream \u5c31\u662f basic_istream \u7684 typedef \u554a\u3002\u4e5f\u8bb8\u6211\u4eec\u5e76\u6ca1\u6709\u641c\u7d22\u9519\uff01 \u9875\u9762\u5411\u4e0b\u6ed1\u52a8\uff0c\u53ef\u4ee5\u770b\u5230\u5f88\u591a\u719f\u6089\u7684\u51fd\u6570\uff1a \u8fd9\u91cc\u5f88\u591a\u51fd\u6570\u6211\u4eec\u90fd\u7528\u8fc7\uff1a operator>> , get , getline , ignore \u3002\u53ef\u4ee5\u6d4f\u89c8\u4e00\u4e0b\u8fd8\u6709\u54ea\u4e9b\u51fd\u6570\uff0c\u7136\u540e\u70b9\u51fb ignore \uff0c\u8fd9\u5c31\u662f\u6211\u4eec\u8981\u627e\u7684\u51fd\u6570\u3002 \u5728\u9875\u9762\u7684\u9876\u90e8\u6709\u51fd\u6570\u7b7e\u540d\u3001\u51fd\u6570\u7b80\u4ecb\u53ca\u5176\u4e24\u4e2a\u53c2\u6570\u7684\u529f\u80fd\u63cf\u8ff0\u3002\u5f62\u53c2\u540e\u9762\u7684 = \u7b26\u53f7\u8868\u793a\u9ed8\u8ba4\u5b9e\u53c2(\u5728\u8bfe\u7a0b 8.12 -\u9ed8\u8ba4\u53c2\u6570 \u4e2d\u8ba8\u8bba\u8fc7)\u3002\u5982\u679c\u6ca1\u6709\u4e3a\u5177\u6709\u9ed8\u8ba4\u503c\u7684\u5f62\u53c2\u63d0\u4f9b\u5b9e\u53c2\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002 \u63a5\u4e0b\u6765\u7684\u65e0\u5e8f\u5217\u8868\u4e2d\u5305\u542b\u4e09\u6761\u5185\u5bb9\u3002\u53ef\u4ee5\u770b\u5230 std::numeric_limits::max() \u5728 std::cin.ignore \u4e2d\u662f\u7531\u7279\u6b8a\u542b\u4e49\u7684\uff0c\u5b83\u53ef\u4ee5\u5ffd\u7565\u5b57\u7b26\u8ba1\u6570\u68c0\u67e5\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c std::cin.ignore \u4f1a\u5ffd\u7565\u5206\u9694\u7b26\u524d\u6240\u6709\u7684\u5b57\u7b26\uff0c\u6216\u8005\u76f4\u5230\u8017\u5c3d\u9700\u8981\u5173\u6ce8\u7684\u5b57\u7b26\u4e2a\u6570\u3002 \u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u77e5\u9053\u8fd9\u4e2a\u51fd\u6570\u662f\u5e72\u561b\u7684\uff0c\u4f46\u662f\u5fd8\u8bb0\u4e86\u53c2\u6570\u548c\u8fd4\u56de\u503c\uff0c\u6b64\u65f6\u4f60\u4e0d\u9700\u8981\u9605\u8bfb\u51fd\u6570\u7684\u5b8c\u6574\u63cf\u8ff0\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f60\u53ea\u9700\u6d4f\u89c8\u4e00\u4e0b\u53c2\u6570\u5217\u8868\u548c\u8fd4\u56de\u503c\u5c31\u8db3\u591f\u4e86\u3002 \u8fd9\u91cc\u7684\u53c2\u6570\u4ecb\u7ecd\u5f88\u7b80\u6d01\u3002\u5b83\u5e76\u6ca1\u6709\u5305\u62ec std::numeric_limits::max() \u7684\u7279\u6b8a\u542b\u4e49\uff0c\u4f46\u662f\u8fd9\u4e9b\u7b80\u6d01\u7684\u53c2\u6570\u4ecb\u7ecd\u4e00\u822c\u60c5\u51b5\u4e0b\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u56de\u5fc6\u8d77\u4e00\u4e9b\u5173\u952e\u7684\u4fe1\u606f\u3002","title":"\u6848\u4f8b\u4e8c\uff1astd::cin.ignore"},{"location":"10-y-using-a-language-reference/#_2","text":"\u9664\u4e86\u6807\u51c6\u5e93\uff0ccppreference\u8fd8\u8bb0\u5f55\u4e86C++\u8bed\u8a00\u7684\u8bed\u6cd5\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u6709\u6548\u7684\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include int getUserInput () { int i {}; std :: cin >> i ; return i ; } int main () { std :: cout << \"How many bananas did you eat today? \\n \" ; if ( int iBananasEaten { getUserInput () }; iBananasEaten <= 2 ) { std :: cout << \"Yummy \\n \" ; } else { std :: cout << iBananasEaten << \" is a lot! \\n \" ; } return 0 ; } \u4e3a\u4ec0\u4e48\u5728 if-statement \u7684\u6761\u4ef6\u4e2d\u6709\u4e00\u4e2a\u53d8\u91cf\u5b9a\u4e49\uff1f\u5728\u641c\u7d22\u5f15\u64ce\u4e2d\u641c\u7d22\u201ccppreference if statement\u201d\u6765\u4e86\u89e3\u5b83\u7684\u529f\u80fd\u3002\u641c\u7d22\u5f15\u64ce\u4f1a\u5c06\u6211\u4eec\u5e26\u5230 if statements \u3002\u5728\u9875\u9762\u5f00\u59cb\u7684\u5730\u65b9\u5c31\u6709\u5176\u76f8\u5173\u7684\u8bed\u6cd5\u8bf4\u660e\u3002 \u5728\u9875\u9762\u7684\u53f3\u534a\u90e8\u5206\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e0d\u540c\u8bed\u6cd5\u4e0eC++\u7248\u672c\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u5bf9\u4e8e if-statement \uff0c\u4eceC++17\u5f00\u59cb\uff0c\u5982\u679c\u4f60\u5ffd\u7565\u6240\u6709\u53ef\u9009\u7684\u90e8\u5206\uff0c\u90a3\u4e48\u5c31\u662f\u6211\u4eec\u5e38\u7528\u7684 if-statement \u7684\u5f62\u5f0f\u3002\u5728 condition \u4e4b\u524d\uff0c\u6709\u4e00\u4e2a\u53ef\u9009\u7684\u90e8\u5206\u79f0\u4e3a init-statement \uff0c\u770b\u8d77\u6765\u8fd9\u90e8\u5206\u5c31\u662f\u548c\u4e4b\u524d\u4f8b\u5b50\u4e2d\u7684\u4ee3\u7801\u662f\u76f8\u5173\u7684\u3002 1 2 if ( init - statement condition ) statement - true if ( init - statement condition ) statement - true else statement - false \u5728\u8bed\u6cd5\u53c2\u8003\u4e0b\u65b9\uff0c\u6709\u5bf9\u8bed\u6cd5\u7684\u6bcf\u4e2a\u90e8\u5206\u7684\u89e3\u91ca\uff0c\u5176\u4e2d\u5c31\u5305\u62ec init-statement \u2014\u2014\u5b83\u901a\u5e38\u662f\u4e00\u4e2a\u5e26\u6709\u521d\u59cb\u5316\u503c\u7684\u53d8\u91cf\u58f0\u660e\u3002 \u4e0b\u9762\u662f if-statements \u7684\u8bed\u6cd5\u89e3\u91ca\u548c\u4e00\u4e9b\u7b80\u5355\u7684\u4f8b\u5b50: \u6211\u4eec\u4e86\u89e3 if-statements \u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u4f46\u662f\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u6ca1\u6709\u5305\u542b init-statement \u7684\u7528\u6cd5\u3002\u6240\u4ee5\u63a5\u7740\u5411\u4e0b\u770b\uff0c\u627e\u627e\u770b\u6709\u6ca1\u6709\u4e13\u95e8\u4ecb\u7ecd\u5e26\u521d\u59cb\u5316\u503c\u7684 if-statements \u7684\u5185\u5bb9\u3002 \u8fd9\u91cc\u9996\u5148\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 init-statement \uff0c\u73b0\u5728\u6211\u4eec\u5c31\u77e5\u9053\u4e4b\u524d\u4ee3\u7801\u4e2d\u7684if\u8bed\u53e5\u662f\u600e\u4e48\u56de\u4e8b\u4e86\u3002\u5176\u5b9e\u5b83\u5c31\u662f\u5c06\u4e00\u822c\u7684\u53d8\u91cf\u58f0\u660e\u548cif\u8bed\u53e5\u5408\u5e76\u8d77\u6765\u7f62\u4e86\u3002 \u63a5\u4e0b\u6765\u7684\u53e5\u5b50\u5f88\u6709\u610f\u601d\uff0c\u5b83\u544a\u8bc9\u6211\u4eec init-statement \u58f0\u660e\u7684\u53d8\u91cf\uff0c\u5728if\u8bed\u53e5\u7684true\u548cfalse\u8bed\u53e5\u5757\u4e2d\u90fd\u80fd\u4f7f\u7528\u3002\u5982\u679c\u4f60\u4e4b\u524d\u5929\u771f\u7684\u8ba4\u4e3a\u8fd9\u4e2a\u53d8\u91cf\u53ea\u80fd\u5728true\u8bed\u53e5\u5757\u4e2d\u4f7f\u7528\u7684\u8bdd\uff0c\u8be5\u8bf4\u660e\u5e94\u8be5\u4f1a\u8ba9\u4f60\u770b\u5230\u6709\u70b9\u610f\u5916\u3002 init-statement \u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86\u4e00\u4e9b\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u8fc7\u7684\u7279\u6027\uff0c\u4f46\u6211\u4eec\u5e76\u4e0d\u9700\u8981\u770b\u61c2\u6240\u6709\u5185\u5bb9\u5c31\u53ef\u4ee5\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 init-statement \u3002\u6240\u4ee5\u76ee\u524d\u53ef\u4ee5\u5148\u8df3\u8fc7\u8ba9\u4eba\u56f0\u60d1\u7684\u5185\u5bb9\uff0c\u4e13\u6ce8\u4e8e\u6211\u4eec\u719f\u7cfb\u7684\u90e8\u5206\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // Iterators, we don't know them. Skip. if ( auto it = m . find ( 10 ); it != m . end ()) { return it -> second . size (); } // [10], what's that? Skip. if ( char buf [ 10 ]; std :: fgets ( buf , 10 , stdin )) { m [ 0 ] += buf ; } // std::lock_guard, we don't know that, but it's some type. We know what types are! if ( std :: lock_guard lock ( mx ); shared_flag ) { unsafe_ping (); shared_flag = false ; } // This is easy, that's an int! if ( int s ; int count = ReadBytesWithSignal ( & s )) { publish ( count ); raise ( s ); } // Whew, no thanks! if ( auto keywords = { \"if\" , \"for\" , \"while\" }; std :: any_of ( keywords . begin (), keywords . end (), [ & s ]( const char * kw ) { return s == kw ; })) { std :: cerr << \"Token must not be a keyword \\n \" ; } \u6700\u7b80\u5355\u7684\u4f8b\u5b50\u4f3c\u4e4e\u662f\u4f7f\u7528\u4e86 int \u7684\u90a3\u4e2a\uff0c\u4f46\u662f\u5b83\u7684\u5206\u53f7\u540e\u9762\u53c8\u662f\u4e00\u4e2a\u5b9a\u4e49\uff0c\u770b\u4e0a\u53bb\u6709\u70b9\u5947\u602a\u3002\u518d\u770b\u4e00\u4e2a std::lock_guard \u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 if ( std :: lock_guard lock ( mx ); shared_flag ) { unsafe_ping (); shared_flag = false ; } \u8fd9\u91cc\u5b9a\u4e49\u4e86\u53d8\u91cf( lock )\uff0c\u7136\u540e\u662f\u5206\u53f7\uff0c\u7136\u540e\u662f\u6761\u4ef6\u3002\u8fd9\u4e2a\u53cd\u5012\u662f\u6700\u63a5\u8fd1\u6211\u4eec\u4ee3\u7801\u7684\u4f8b\u5b50\u3002","title":"\u6848\u4f8b\u4e09\uff1a\u8bed\u6cd5"},{"location":"10-y-using-a-language-reference/#cppreference","text":"\u5176\u5b9e\uff0ccppreference \u5e76\u4e0d\u662f\u4e00\u4e2a\u5b98\u65b9\u6587\u6863\u6e90\u2014\u2014\u76f8\u53cd\uff0c\u5b83\u662f\u4e00\u4e2awiki\u767e\u79d1\u5e94\u7528\u3002\u4f7f\u7528wiki\uff0c\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u6dfb\u52a0\u548c\u4fee\u6539\u5185\u5bb9\u2014\u2014\u5185\u5bb9\u6765\u81ea\u793e\u533a\u3002\u867d\u7136\u8fd9\u610f\u5473\u7740\u4eba\u4eec\u5f88\u5bb9\u6613\u6dfb\u52a0\u9519\u8bef\u7684\u4fe1\u606f\uff0c\u4f46\u9519\u8bef\u7684\u4fe1\u606f\u901a\u5e38\u4f1a\u5f88\u5feb\u88ab\u53d1\u73b0\u5e76\u5220\u9664\uff0c\u4f7fcppreference\u6210\u4e3a\u4e00\u4e2a\u53ef\u9760\u7684\u6765\u6e90\u3002 C++\u6807\u51c6 \u662f\u552f\u4e00\u7684\u5b98\u65b9\u6587\u6863( github )\u3002\u867d\u7136\u5b83\u662f\u6b63\u5f0f\u7684\u6587\u6863\uff0c\u4f46\u662f\u5b83\u5e76\u4e0d\u662f\u4e00\u4e2a\u6613\u7528\u7684\u53c2\u8003\u624b\u518c\u3002","title":"\u5173\u4e8e cppreference \u51c6\u786e\u6027\u7684\u6ce8\u610f\u4e8b\u9879"},{"location":"11-1-Arrays-Part-I/","tags":["array"],"text":"Key Takeaway \u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5355\u4e2a\u5143\u7d20\uff0c\u9700\u8981\u4f7f\u7528\u6570\u7ec4\u540d\u3001\u4e0b\u6807\u64cd\u4f5c\u7b26( [] )\u548c\u4e0b\u6807(\u6216\u7d22\u5f15)\uff0c\u4e0b\u6807\u4ece0\u5f00\u59cb\u8ba1\u6570 int prime[5]{}; //\u521d\u59cb\u5316\u6570\u7ec4\u4e3a0 \u5728 C++ \u4e2d\u6570\u7ec4 \u4e0b\u6807(subscripts) \u5fc5\u987b\u662f\u6574\u578b\u7c7b\u578b\uff0c\u5305\u62ec char , short , int , long , long long \u7b49\uff0c\u5947\u602a\u7684\u662f\u5e03\u5c14\u7c7b\u578b\u7adf\u7136\u4e5f\u53ef\u4ee5\uff08 false \u5bf9\u5e94\u7d22\u5f15\u4e3a0\uff0c true \u5bf9\u5e94\u7d22\u5f15\u4e3a1\uff09\u3002 \u6570\u7ec4\u7684\u4e0b\u6807\u53ef\u4ee5\u662f \u5b57\u9762\u91cf \u3001\u53d8\u91cf\uff08 const \u6216\u975e const \u7c7b\u578b\uff09\u6216\u8005\u662f\u4e00\u4e2a\u6c42\u503c\u7ed3\u679c\u4e3a\u6574\u5f62\u7c7b\u578b\u7684\u8868\u8fbe\u5f0f\u3002 \u6570\u7ec4\u7684\u957f\u5ea6(\u65b9\u62ec\u53f7\u4e4b\u95f4)\u5fc5\u987b\u662f \u7f16\u8bd1\u65f6\u5e38\u91cf \uff0c\u975e const \u53d8\u91cf\u6216\u8005 \u8fd0\u884c\u65f6 \u5e38\u91cf \u6ce8\u610f\uff1a\u8fd9\u4e00\u7ae0\u76f8\u5bf9\u4e8e\u524d\u9762\u51e0\u7ae0\u4f1a\u66f4\u6709\u96be\u5ea6\u3002\u5982\u679c\u4f60\u611f\u6cc4\u6c14\uff0c\u4e00\u5b9a\u8981\u575a\u6301\u4f4f\u3002\u597d\u620f\u8fd8\u5728\u540e\u9762\u5462\uff01 \u5728 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u901a\u8fc7\u7ed3\u6784\u4f53\u5c06\u591a\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u805a\u5408\u4e3a\u4e00\u4e2a\u53d8\u91cf\u3002\u8fd9\u9879\u80fd\u529b\u5728\u6211\u4eec\u9700\u8981\u5bf9\u4e00\u4e2a\u5355\u72ec\u7684\u5bf9\u8c61\u8fdb\u884c\u5efa\u6a21\u65f6\u662f\u5f88\u6709\u7528\u7684\u3002\u4e0d\u8fc7\uff0c\u5f53\u6211\u4eec\u9700\u8981\u4fdd\u5b58\u4e00\u7cfb\u5217\u76f8\u5173\u7684\u5b9e\u4f8b\u65f6\uff0c\u8fd9\u4e2a\u7ed3\u6784\u4f53\u5c31\u6392\u4e0d\u4e0a\u7528\u573a\u4e86\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u7ed3\u6784\u4f53\u5e76\u4e0d\u662f C++ \u4e2d\u552f\u4e00\u80fd\u805a\u5408\u6570\u636e\u7684\u7c7b\u578b\u3002 \u6570\u7ec4 \u53ef\u4ee5\u5c06\u4e00\u7cfb\u5217\u5177\u6709\u76f8\u540c\u7c7b\u578b\u7684\u53d8\u91cf\u805a\u5408\u4e3a\u4e00\u4e2a\u53d8\u91cf\u3002 \u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u60a8\u60f3\u8981\u8bb0\u5f55\u4e00\u4e2a\u73ed\u7ea7 30 \u4e2a\u5b66\u751f\u7684\u8003\u8bd5\u6210\u7ee9\u3002\u5982\u679c\u6ca1\u6709\u6570\u7ec4\uff0c\u4f60\u5c06\u4e0d\u5f97\u4e0d\u5206\u914d 30 \u4e2a\u51e0\u4e4e\u76f8\u540c\u7684\u53d8\u91cf\uff01 1 2 3 4 5 6 // allocate 30 integer variables (each with a different name) int testScoreStudent1 {}; int testScoreStudent2 {}; int testScoreStudent3 {}; // ... int testScoreStudent30 {}; \u6570\u7ec4\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u7684\u6570\u7ec4\u5b9a\u4e49\u672c\u8d28\u4e0a\u4e0e\u4e0a\u9762\u7684\u4ee3\u7801\u662f\u7b49\u4ef7\u7684: 1 int testScore [ 30 ]{}; // allocate 30 integer variables in a fixed array \u5728\u6570\u7ec4\u53d8\u91cf\u58f0\u660e\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u65b9\u62ec\u53f7( [] )\u544a\u8bc9\u7f16\u8bd1\u5668\u8fd9\u662f\u4e00\u4e2a\u6570\u7ec4\u53d8\u91cf(\u800c\u4e0d\u662f\u666e\u901a\u7684\u53d8\u91cf)\uff0c\u4ee5\u53ca\u5206\u914d\u591a\u5c11\u53d8\u91cf(\u79f0\u4e3a \u6570\u7ec4\u957f\u5ea6 )\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a\u540d\u4e3a testScore \u7684\u56fa\u5b9a\u6570\u7ec4\uff0c\u957f\u5ea6\u4e3a30\u3002 \u56fa\u5b9a\u6570\u7ec4 (\u4e5f\u79f0\u4e3a \u56fa\u5b9a\u957f\u5ea6\u6570\u7ec4 \u6216 \u56fa\u5b9a\u5927\u5c0f\u6570\u7ec4 )\u662f\u5728\u7f16\u8bd1\u65f6\u5df2\u77e5\u957f\u5ea6\u7684\u6570\u7ec4\u3002 \u5f53testScore \u5b9e\u4f8b\u5316\u65f6\uff0c\u5c06\u5206\u914d30\u4e2a\u6574\u6570\u3002 \u6570\u7ec4\u5143\u7d20\u548c\u4e0b\u6807 \u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u53d8\u91cf\u90fd\u88ab\u79f0\u4e3a \u5143\u7d20 \u3002\u5143\u7d20\u6ca1\u6709\u81ea\u5df1\u7684\u540d\u79f0\u3002\u800c\u5982\u679c\u8981\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5355\u4e2a\u5143\u7d20\uff0c\u5219\u9700\u8981\u4f7f\u7528\u6570\u7ec4\u540d\u3001\u4e0b\u6807\u64cd\u4f5c\u7b26( [] )\u548c\u4e00\u4e2a\u88ab\u79f0\u4e3a\u4e0b\u6807(\u6216\u7d22\u5f15)\u7684\u5f62\u53c2\u6765\u544a\u8bc9\u7f16\u8bd1\u5668\u9700\u8981\u54ea\u4e2a\u5143\u7d20\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a\u6570\u7ec4\u7684\u53d6\u4e0b\u6807(subscripting)\u6216\u7d22\u5f15(indexing)\u64cd\u4f5c\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f testScore[0] \u3002\u7b2c\u4e8c\u4e2a\u662f testScore[1] \u3002\u7b2c\u5341\u4e2a\u662f testScore[9] \u3002 testScore \u6570\u7ec4\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u662f testScore[29] \u3002\u8fd9\u5f88\u597d\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u518d\u9700\u8981\u8ddf\u8e2a\u4e00\u5806\u4e0d\u540c\u7684(\u4f46\u76f8\u5173\u7684)\u540d\u79f0\u2014\u2014\u6211\u4eec\u53ea\u9700\u6539\u53d8\u4e0b\u6807\u6765\u8bbf\u95ee\u4e0d\u540c\u7684\u5143\u7d20\u3002 \u91cd\u8981 \uff1a\u4e0e\u65e5\u5e38\u751f\u6d3b\u4e2d\u6211\u4eec\u901a\u5e38\u4ece1\u5f00\u59cb\u8ba1\u6570\u4e0d\u540c\uff0c\u5728C++\u4e2d\uff0c\u6570\u7ec4\u603b\u662f\u4ece0\u5f00\u59cb\u8ba1\u6570\uff01 \u5bf9\u4e8e\u957f\u5ea6\u4e3a N \u7684\u6570\u7ec4\uff0c\u6570\u7ec4\u5143\u7d20\u7f16\u53f7\u4ece0\u5230N-1\u3002\u8fd9\u88ab\u79f0\u4e3a\u6570\u7ec4\u7684 \u8303\u56f4 \u3002 \u6570\u7ec4\u7a0b\u5e8f\u5b9e\u4f8b \u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u7a0b\u5e8f\uff0c\u5c06\u6570\u7ec4\u7684\u5b9a\u4e49\u548c\u7d22\u5f15\u653e\u5728\u4e00\u8d77: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { int prime [ 5 ]{}; // hold the first 5 prime numbers prime [ 0 ] = 2 ; // The first element has index 0 prime [ 1 ] = 3 ; prime [ 2 ] = 5 ; prime [ 3 ] = 7 ; prime [ 4 ] = 11 ; // The last element has index 4 (array length-1) std :: cout << \"The lowest prime number is: \" << prime [ 0 ] << '\\n' ; std :: cout << \"The sum of the first 5 primes is: \" << prime [ 0 ] + prime [ 1 ] + prime [ 2 ] + prime [ 3 ] + prime [ 4 ] << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 The lowest prime number is: 2 The sum of the first 5 primes is: 28 \u6570\u7ec4\u6570\u636e\u7c7b\u578b \u6570\u7ec4\u53ef\u4ee5\u7531\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u7ec4\u6210\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a double \u7c7b\u578b\u7684\u6570\u7ec4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { double batteryLifeInHours [ 3 ]{}; // \u5206\u914d 3 \u4e2a doubles batteryLifeInHours [ 0 ] = 2.0 ; batteryLifeInHours [ 1 ] = 3.0 ; batteryLifeInHours [ 2 ] = 4.3 ; std :: cout << \"The average battery life is \" << ( batteryLifeInHours [ 0 ] + batteryLifeInHours [ 1 ] + batteryLifeInHours [ 2 ]) / 3.0 << \" hour(s) \\n \" ; return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 The average battery life is 3.1 hour(s) \u6570\u7ec4\u4e5f\u53ef\u4ee5\u7531\u7ed3\u6784\u4f53\u7ec4\u6210\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 struct Rectangle { int length {}; int width {}; }; Rectangle rects [ 5 ]{}; // declare an array of 5 Rectangle \u8981\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7ed3\u6784\u4f53\u5143\u7d20\u7684\u6210\u5458\uff0c\u9996\u5148\u9009\u62e9\u60f3\u8981\u8bbf\u95ee\u7684\u6570\u7ec4\u5143\u7d20\uff0c\u7136\u540e\u4f7f\u7528\u6210\u5458\u9009\u62e9\u64cd\u4f5c\u7b26\u9009\u62e9\u60f3\u8981\u8bbf\u95ee\u7684\u6210\u5458\uff1a 1 rects [ 0 ]. length = 24 ; \u6570\u7ec4\u751a\u81f3\u53ef\u4ee5\u7531\u6570\u7ec4\u7ec4\u6210\uff0c\u8fd9\u4e2a\u4f1a\u5728\u4eca\u540e\u7684\u8bfe\u7a0b\u4e2d\u8ba8\u8bba\u3002 \u6570\u7ec4\u4e0b\u6807 \u5728 C++ \u4e2d\u6570\u7ec4 \u4e0b\u6807(subscripts) \u5fc5\u987b\u662f\u6574\u578b\u7c7b\u578b\uff0c\u5305\u62ec char , short , int , long , long long \u7b49\uff0c\u5947\u602a\u7684\u662f\u5e03\u5c14\u7c7b\u578b\u7adf\u7136\u4e5f\u53ef\u4ee5\uff08 false \u5bf9\u5e94\u7d22\u5f15\u4e3a0\uff0c true \u5bf9\u5e94\u7d22\u5f15\u4e3a1\uff09\u3002\u6570\u7ec4\u7684\u4e0b\u6807\u53ef\u4ee5\u662f \u5b57\u9762\u91cf \u3001\u53d8\u91cf\uff08 const \u6216\u975e const \u7c7b\u578b\uff09\u6216\u8005\u662f\u4e00\u4e2a\u6c42\u503c\u7ed3\u679c\u4e3a\u6574\u5f62\u7c7b\u578b\u7684\u8868\u8fbe\u5f0f\u3002 \u4e0b\u9762\u662f\u4e00\u4e9b\u6570\u7ec4\u4e0b\u6807\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 int array [ 5 ]{}; // \u58f0\u660e\u957f\u5ea6\u4e3a 5 \u7684\u6570\u7ec4 // \u4f7f\u7528\u5b57\u9762\u91cf\u7d22\u5f15: array [ 1 ] = 7 ; // ok // \u4f7f\u7528\u679a\u4e3e\u7d22\u5f15 enum Animals { animal_cat = 2 }; array [ animal_cat ] = 4 ; // ok // \u4f7f\u7528\u53d8\u91cf\uff08\u975e const\uff09\u4f5c\u4e3a\u7d22\u5f15 int index { 3 }; array [ index ] = 7 ; // ok // \u4f7f\u7528\u6c42\u503c\u7ed3\u679c\u4e3a\u6574\u5f62\u7c7b\u578b\u7684\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u7d22\u5f15 array [ 1 + 2 ] = 7 ; // ok \u58f0\u660e\u56fa\u5b9a\u957f\u5ea6\u6570\u7ec4 \u58f0\u660e\u56fa\u5b9a\u6570\u7ec4\u65f6\uff0c\u6570\u7ec4\u7684\u957f\u5ea6(\u65b9\u62ec\u53f7\u4e4b\u95f4)\u5fc5\u987b\u662f \u7f16\u8bd1\u65f6\u5e38\u91cf \u3002\u8fd9\u662f\u56e0\u4e3a\u56fa\u5b9a\u6570\u7ec4\u7684\u957f\u5ea6\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u77e5\u9053\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u58f0\u660e\u56fa\u5b9a\u6570\u7ec4\u7684\u4e0d\u540c\u65b9\u6cd5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // \u4f7f\u7528\u5b57\u9762\u91cf\u5e38\u91cf int numberOfLessonsPerDay [ 7 ]{}; // Ok //\u4f7f\u7528 constexpr \u7b26\u53f7\u5e38\u91cf constexpr int daysPerWeek { 7 }; int numberOfLessonsPerDay [ daysPerWeek ]{}; // Ok // \u4f7f\u7528\u679a\u4e3e enum DaysOfWeek { monday , tuesday , wednesday , thursday , friday , saturday , sunday , maxDaysOfWeek }; int numberOfLessonsPerDay [ maxDaysOfWeek ]{}; // Ok // \u4f7f\u7528\u5b8f #define DAYS_PER_WEEK 7 int numberOfLessonsPerDay [ DAYS_PER_WEEK ]{}; // \u53ef\u4ee5\uff0c\u4f46\u662f\u4e0d\u8981\u8fd9\u4e48\u505a\uff08\u5e94\u5f53\u4f7f\u7528 constexpr \u7b26\u53f7\u5e38\u91cf\uff09 \u6ce8\u610f\uff0c\u975e const \u53d8\u91cf\u6216\u8005 \u8fd0\u884c\u65f6 \u5e38\u91cf\u662f\u4e0d\u884c\u7684\uff1a 1 2 3 4 5 6 7 8 9 // \u4f7f\u7528\u975e const \u53d8\u91cf int daysPerWeek {}; std :: cin >> daysPerWeek ; int numberOfLessonsPerDay [ daysPerWeek ]{}; // Not ok -- daysPerWeek \u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf // \u4f7f\u7528\u8fd0\u884c\u65f6\u5e38\u91cf int temp { 5 }; const int daysPerWeek { temp }; // daysPerWeek \u76f4\u5230\u8fd0\u884c\u65f6\u5728\u76f4\u5230\uff0c\u6240\u4ee5\u662f\u8fd0\u884c\u65f6\u5e38\u91cf\u800c\u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf int numberOfLessonsPerDay [ daysPerWeek ]{}; // Not ok \u6ce8\u610f\uff0c\u5728\u6700\u540e\u4e24\u79cd\u60c5\u51b5\u4e0b\u4f1a\u4ea7\u751f\u9519\u8bef\uff0c\u56e0\u4e3a\u6570\u7ec4\u7684\u957f\u5ea6\u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u6709\u4e9b\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5141\u8bb8\u8fd9\u4e9b\u7c7b\u578b\u7684\u6570\u7ec4(\u51fa\u4e8e\u4e0eC99\u517c\u5bb9\u7684\u539f\u56e0)\uff0c\u4f46\u5b83\u4eec\u5728++\u4e2d\u662f\u65e0\u6548\u7684\uff0c\u4e0d\u5e94\u8be5\u5728C++\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u3002\u5982\u679c\u7f16\u8bd1\u5668\u5141\u8bb8\u8fd9\u4e9b\u6570\u7ec4\uff0c\u90a3\u4e48\u60a8\u53ef\u80fd\u5fd8\u8bb0\u7981\u7528\u7f16\u8bd1\u5668\u6269\u5c55\u3002 \u52a8\u6001\u6570\u7ec4 \u56e0\u4e3a\u56fa\u5b9a\u6570\u7ec4\u5728\u7f16\u8bd1\u65f6\u5206\u914d\u5185\u5b58\uff0c\u56e0\u6b64\u4f1a\u5e26\u6765\u4e24\u4e2a\u9650\u5236\uff1a \u56fa\u5b9a\u6570\u7ec4\u7684\u957f\u5ea6\u4e0d\u80fd\u57fa\u4e8e\u7528\u6237\u8f93\u5165\u6216\u8fd0\u884c\u65f6\u8ba1\u7b97\u7684\u5176\u4ed6\u503c\u6765\u8bbe\u5b9a\u3002 \u56fa\u5b9a\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u56fa\u5b9a\u7684\u4e0d\u80fd\u6539\u53d8\u3002 \u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4e9b\u9650\u5236\u4e0d\u5408\u7406\u7684\u3002\u5e78\u8fd0\u7684\u662f\uff0cC++\u652f\u6301\u7b2c\u4e8c\u79cd\u6570\u7ec4\uff0c\u79f0\u4e3a \u52a8\u6001\u6570\u7ec4 \u3002\u52a8\u6001\u6570\u7ec4\u7684\u957f\u5ea6\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u8bbe\u7f6e\uff0c\u4e5f\u53ef\u4ee5\u66f4\u6539\u5176\u957f\u5ea6\u3002\u7136\u800c\uff0c\u52a8\u6001\u6570\u7ec4\u7684\u5b9e\u4f8b\u5316\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\uff0c\u56e0\u6b64\u6211\u4eec\u5c06\u5728\u540e\u9762\u4ecb\u7ecd\u5b83\u4eec\u3002 \u5c0f\u7ed3 \u56fa\u5b9a\u6570\u7ec4\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5206\u914d\u548c\u4f7f\u7528\u76f8\u540c\u7c7b\u578b\u7684\u591a\u4e2a\u53d8\u91cf\uff0c\u4f46\u6570\u7ec4\u7684\u957f\u5ea6\u5728\u7f16\u8bd1\u65f6\u5fc5\u987b\u662f\u5df2\u77e5\u7684\u3002 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u56fa\u5b9a\u6570\u7ec4\u7684\u4e3b\u9898\u3002","title":"11.1 - \u6570\u7ec4\uff08\u7b2c\u4e00\u90e8\u5206\uff09"},{"location":"11-1-Arrays-Part-I/#_1","text":"\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u53d8\u91cf\u90fd\u88ab\u79f0\u4e3a \u5143\u7d20 \u3002\u5143\u7d20\u6ca1\u6709\u81ea\u5df1\u7684\u540d\u79f0\u3002\u800c\u5982\u679c\u8981\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5355\u4e2a\u5143\u7d20\uff0c\u5219\u9700\u8981\u4f7f\u7528\u6570\u7ec4\u540d\u3001\u4e0b\u6807\u64cd\u4f5c\u7b26( [] )\u548c\u4e00\u4e2a\u88ab\u79f0\u4e3a\u4e0b\u6807(\u6216\u7d22\u5f15)\u7684\u5f62\u53c2\u6765\u544a\u8bc9\u7f16\u8bd1\u5668\u9700\u8981\u54ea\u4e2a\u5143\u7d20\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a\u6570\u7ec4\u7684\u53d6\u4e0b\u6807(subscripting)\u6216\u7d22\u5f15(indexing)\u64cd\u4f5c\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f testScore[0] \u3002\u7b2c\u4e8c\u4e2a\u662f testScore[1] \u3002\u7b2c\u5341\u4e2a\u662f testScore[9] \u3002 testScore \u6570\u7ec4\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u662f testScore[29] \u3002\u8fd9\u5f88\u597d\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u518d\u9700\u8981\u8ddf\u8e2a\u4e00\u5806\u4e0d\u540c\u7684(\u4f46\u76f8\u5173\u7684)\u540d\u79f0\u2014\u2014\u6211\u4eec\u53ea\u9700\u6539\u53d8\u4e0b\u6807\u6765\u8bbf\u95ee\u4e0d\u540c\u7684\u5143\u7d20\u3002 \u91cd\u8981 \uff1a\u4e0e\u65e5\u5e38\u751f\u6d3b\u4e2d\u6211\u4eec\u901a\u5e38\u4ece1\u5f00\u59cb\u8ba1\u6570\u4e0d\u540c\uff0c\u5728C++\u4e2d\uff0c\u6570\u7ec4\u603b\u662f\u4ece0\u5f00\u59cb\u8ba1\u6570\uff01 \u5bf9\u4e8e\u957f\u5ea6\u4e3a N \u7684\u6570\u7ec4\uff0c\u6570\u7ec4\u5143\u7d20\u7f16\u53f7\u4ece0\u5230N-1\u3002\u8fd9\u88ab\u79f0\u4e3a\u6570\u7ec4\u7684 \u8303\u56f4 \u3002","title":"\u6570\u7ec4\u5143\u7d20\u548c\u4e0b\u6807"},{"location":"11-1-Arrays-Part-I/#_2","text":"\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u7a0b\u5e8f\uff0c\u5c06\u6570\u7ec4\u7684\u5b9a\u4e49\u548c\u7d22\u5f15\u653e\u5728\u4e00\u8d77: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { int prime [ 5 ]{}; // hold the first 5 prime numbers prime [ 0 ] = 2 ; // The first element has index 0 prime [ 1 ] = 3 ; prime [ 2 ] = 5 ; prime [ 3 ] = 7 ; prime [ 4 ] = 11 ; // The last element has index 4 (array length-1) std :: cout << \"The lowest prime number is: \" << prime [ 0 ] << '\\n' ; std :: cout << \"The sum of the first 5 primes is: \" << prime [ 0 ] + prime [ 1 ] + prime [ 2 ] + prime [ 3 ] + prime [ 4 ] << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 The lowest prime number is: 2 The sum of the first 5 primes is: 28","title":"\u6570\u7ec4\u7a0b\u5e8f\u5b9e\u4f8b"},{"location":"11-1-Arrays-Part-I/#_3","text":"\u6570\u7ec4\u53ef\u4ee5\u7531\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u7ec4\u6210\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a double \u7c7b\u578b\u7684\u6570\u7ec4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { double batteryLifeInHours [ 3 ]{}; // \u5206\u914d 3 \u4e2a doubles batteryLifeInHours [ 0 ] = 2.0 ; batteryLifeInHours [ 1 ] = 3.0 ; batteryLifeInHours [ 2 ] = 4.3 ; std :: cout << \"The average battery life is \" << ( batteryLifeInHours [ 0 ] + batteryLifeInHours [ 1 ] + batteryLifeInHours [ 2 ]) / 3.0 << \" hour(s) \\n \" ; return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 The average battery life is 3.1 hour(s) \u6570\u7ec4\u4e5f\u53ef\u4ee5\u7531\u7ed3\u6784\u4f53\u7ec4\u6210\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 struct Rectangle { int length {}; int width {}; }; Rectangle rects [ 5 ]{}; // declare an array of 5 Rectangle \u8981\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7ed3\u6784\u4f53\u5143\u7d20\u7684\u6210\u5458\uff0c\u9996\u5148\u9009\u62e9\u60f3\u8981\u8bbf\u95ee\u7684\u6570\u7ec4\u5143\u7d20\uff0c\u7136\u540e\u4f7f\u7528\u6210\u5458\u9009\u62e9\u64cd\u4f5c\u7b26\u9009\u62e9\u60f3\u8981\u8bbf\u95ee\u7684\u6210\u5458\uff1a 1 rects [ 0 ]. length = 24 ; \u6570\u7ec4\u751a\u81f3\u53ef\u4ee5\u7531\u6570\u7ec4\u7ec4\u6210\uff0c\u8fd9\u4e2a\u4f1a\u5728\u4eca\u540e\u7684\u8bfe\u7a0b\u4e2d\u8ba8\u8bba\u3002","title":"\u6570\u7ec4\u6570\u636e\u7c7b\u578b"},{"location":"11-1-Arrays-Part-I/#_4","text":"\u5728 C++ \u4e2d\u6570\u7ec4 \u4e0b\u6807(subscripts) \u5fc5\u987b\u662f\u6574\u578b\u7c7b\u578b\uff0c\u5305\u62ec char , short , int , long , long long \u7b49\uff0c\u5947\u602a\u7684\u662f\u5e03\u5c14\u7c7b\u578b\u7adf\u7136\u4e5f\u53ef\u4ee5\uff08 false \u5bf9\u5e94\u7d22\u5f15\u4e3a0\uff0c true \u5bf9\u5e94\u7d22\u5f15\u4e3a1\uff09\u3002\u6570\u7ec4\u7684\u4e0b\u6807\u53ef\u4ee5\u662f \u5b57\u9762\u91cf \u3001\u53d8\u91cf\uff08 const \u6216\u975e const \u7c7b\u578b\uff09\u6216\u8005\u662f\u4e00\u4e2a\u6c42\u503c\u7ed3\u679c\u4e3a\u6574\u5f62\u7c7b\u578b\u7684\u8868\u8fbe\u5f0f\u3002 \u4e0b\u9762\u662f\u4e00\u4e9b\u6570\u7ec4\u4e0b\u6807\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 int array [ 5 ]{}; // \u58f0\u660e\u957f\u5ea6\u4e3a 5 \u7684\u6570\u7ec4 // \u4f7f\u7528\u5b57\u9762\u91cf\u7d22\u5f15: array [ 1 ] = 7 ; // ok // \u4f7f\u7528\u679a\u4e3e\u7d22\u5f15 enum Animals { animal_cat = 2 }; array [ animal_cat ] = 4 ; // ok // \u4f7f\u7528\u53d8\u91cf\uff08\u975e const\uff09\u4f5c\u4e3a\u7d22\u5f15 int index { 3 }; array [ index ] = 7 ; // ok // \u4f7f\u7528\u6c42\u503c\u7ed3\u679c\u4e3a\u6574\u5f62\u7c7b\u578b\u7684\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u7d22\u5f15 array [ 1 + 2 ] = 7 ; // ok","title":"\u6570\u7ec4\u4e0b\u6807"},{"location":"11-1-Arrays-Part-I/#_5","text":"\u58f0\u660e\u56fa\u5b9a\u6570\u7ec4\u65f6\uff0c\u6570\u7ec4\u7684\u957f\u5ea6(\u65b9\u62ec\u53f7\u4e4b\u95f4)\u5fc5\u987b\u662f \u7f16\u8bd1\u65f6\u5e38\u91cf \u3002\u8fd9\u662f\u56e0\u4e3a\u56fa\u5b9a\u6570\u7ec4\u7684\u957f\u5ea6\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u77e5\u9053\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u58f0\u660e\u56fa\u5b9a\u6570\u7ec4\u7684\u4e0d\u540c\u65b9\u6cd5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // \u4f7f\u7528\u5b57\u9762\u91cf\u5e38\u91cf int numberOfLessonsPerDay [ 7 ]{}; // Ok //\u4f7f\u7528 constexpr \u7b26\u53f7\u5e38\u91cf constexpr int daysPerWeek { 7 }; int numberOfLessonsPerDay [ daysPerWeek ]{}; // Ok // \u4f7f\u7528\u679a\u4e3e enum DaysOfWeek { monday , tuesday , wednesday , thursday , friday , saturday , sunday , maxDaysOfWeek }; int numberOfLessonsPerDay [ maxDaysOfWeek ]{}; // Ok // \u4f7f\u7528\u5b8f #define DAYS_PER_WEEK 7 int numberOfLessonsPerDay [ DAYS_PER_WEEK ]{}; // \u53ef\u4ee5\uff0c\u4f46\u662f\u4e0d\u8981\u8fd9\u4e48\u505a\uff08\u5e94\u5f53\u4f7f\u7528 constexpr \u7b26\u53f7\u5e38\u91cf\uff09 \u6ce8\u610f\uff0c\u975e const \u53d8\u91cf\u6216\u8005 \u8fd0\u884c\u65f6 \u5e38\u91cf\u662f\u4e0d\u884c\u7684\uff1a 1 2 3 4 5 6 7 8 9 // \u4f7f\u7528\u975e const \u53d8\u91cf int daysPerWeek {}; std :: cin >> daysPerWeek ; int numberOfLessonsPerDay [ daysPerWeek ]{}; // Not ok -- daysPerWeek \u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf // \u4f7f\u7528\u8fd0\u884c\u65f6\u5e38\u91cf int temp { 5 }; const int daysPerWeek { temp }; // daysPerWeek \u76f4\u5230\u8fd0\u884c\u65f6\u5728\u76f4\u5230\uff0c\u6240\u4ee5\u662f\u8fd0\u884c\u65f6\u5e38\u91cf\u800c\u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf int numberOfLessonsPerDay [ daysPerWeek ]{}; // Not ok \u6ce8\u610f\uff0c\u5728\u6700\u540e\u4e24\u79cd\u60c5\u51b5\u4e0b\u4f1a\u4ea7\u751f\u9519\u8bef\uff0c\u56e0\u4e3a\u6570\u7ec4\u7684\u957f\u5ea6\u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u6709\u4e9b\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5141\u8bb8\u8fd9\u4e9b\u7c7b\u578b\u7684\u6570\u7ec4(\u51fa\u4e8e\u4e0eC99\u517c\u5bb9\u7684\u539f\u56e0)\uff0c\u4f46\u5b83\u4eec\u5728++\u4e2d\u662f\u65e0\u6548\u7684\uff0c\u4e0d\u5e94\u8be5\u5728C++\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u3002\u5982\u679c\u7f16\u8bd1\u5668\u5141\u8bb8\u8fd9\u4e9b\u6570\u7ec4\uff0c\u90a3\u4e48\u60a8\u53ef\u80fd\u5fd8\u8bb0\u7981\u7528\u7f16\u8bd1\u5668\u6269\u5c55\u3002","title":"\u58f0\u660e\u56fa\u5b9a\u957f\u5ea6\u6570\u7ec4"},{"location":"11-1-Arrays-Part-I/#_6","text":"\u56e0\u4e3a\u56fa\u5b9a\u6570\u7ec4\u5728\u7f16\u8bd1\u65f6\u5206\u914d\u5185\u5b58\uff0c\u56e0\u6b64\u4f1a\u5e26\u6765\u4e24\u4e2a\u9650\u5236\uff1a \u56fa\u5b9a\u6570\u7ec4\u7684\u957f\u5ea6\u4e0d\u80fd\u57fa\u4e8e\u7528\u6237\u8f93\u5165\u6216\u8fd0\u884c\u65f6\u8ba1\u7b97\u7684\u5176\u4ed6\u503c\u6765\u8bbe\u5b9a\u3002 \u56fa\u5b9a\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u56fa\u5b9a\u7684\u4e0d\u80fd\u6539\u53d8\u3002 \u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4e9b\u9650\u5236\u4e0d\u5408\u7406\u7684\u3002\u5e78\u8fd0\u7684\u662f\uff0cC++\u652f\u6301\u7b2c\u4e8c\u79cd\u6570\u7ec4\uff0c\u79f0\u4e3a \u52a8\u6001\u6570\u7ec4 \u3002\u52a8\u6001\u6570\u7ec4\u7684\u957f\u5ea6\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u8bbe\u7f6e\uff0c\u4e5f\u53ef\u4ee5\u66f4\u6539\u5176\u957f\u5ea6\u3002\u7136\u800c\uff0c\u52a8\u6001\u6570\u7ec4\u7684\u5b9e\u4f8b\u5316\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\uff0c\u56e0\u6b64\u6211\u4eec\u5c06\u5728\u540e\u9762\u4ecb\u7ecd\u5b83\u4eec\u3002","title":"\u52a8\u6001\u6570\u7ec4"},{"location":"11-1-Arrays-Part-I/#_7","text":"\u56fa\u5b9a\u6570\u7ec4\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5206\u914d\u548c\u4f7f\u7528\u76f8\u540c\u7c7b\u578b\u7684\u591a\u4e2a\u53d8\u91cf\uff0c\u4f46\u6570\u7ec4\u7684\u957f\u5ea6\u5728\u7f16\u8bd1\u65f6\u5fc5\u987b\u662f\u5df2\u77e5\u7684\u3002 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u56fa\u5b9a\u6570\u7ec4\u7684\u4e3b\u9898\u3002","title":"\u5c0f\u7ed3"},{"location":"11-10-C-style-string-symbolic-constants/","tags":["symbolic constants"],"text":"Key Takeaway \u521d\u59cb\u5316C\u5b57\u7b26\u4e32\u5e38\u91cf\u7684\u4e24\u79cd\u65b9\u6cd5\u2014\u2014\u56fa\u5b9a\u6570\u7ec4\u548c\u6307\u9488 char myName[]{ \"Alex\" }; // \u56fa\u5b9a\u6570\u7ec4 const char* myName{ \"Alex\" };//\u6307\u5411\u7b26\u53f7\u5e38\u91cf\u7684\u6307\u9488 \u7b26\u53f7\u5e38\u91cf\u65b9\u5f0f\u521d\u59cb\u5316\u7684C\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u53ef\u80fd\u4f1a\u4f4d\u4e8e\u5185\u5b58\u4e2d\u7684\u53ea\u8bfb\u533a\u57df\uff0c\u5e76\u4e14\u76f8\u540c\u7684\u5b57\u7b26\u4e32\u53d8\u91cf\u53ef\u80fd\u4f1a\u6307\u5411\u540c\u4e00\u5757\u5185\u5b58\uff0c\u56e0\u6b64\u6700\u597d\u5b9a\u4e49\u4e3aconst \u7b26\u53f7\u5e38\u91cf\u65b9\u5f0f\u521d\u59cb\u5316\u7684C\u98ce\u683c\u5b57\u7b26\u4e32\u4f4d\u4e8e\u53ea\u8bfb\u5185\u5b58\u4e2d\u65f6\uff0c\u5b83\u5177\u6709\u5168\u5c40\u4f5c\u7528\u57df\u3002 C \u98ce\u683c\u5b57\u7b26\u4e32\u5e38\u91cf \u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u521d\u59cb\u5316\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char myName []{ \"Alex\" }; // \u56fa\u5b9a\u6570\u7ec4 std :: cout << myName << '\\n' ; return 0 ; } C++ \u4e5f\u652f\u6301\u4f7f\u7528\u6307\u9488\u6307\u5411\u7b26\u53f7\u5e38\u91cf\u6765\u521b\u5efaC\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { const char * myName { \"Alex\" }; // pointer to symbolic constant std :: cout << myName << '\\n' ; return 0 ; } \u5c3d\u7ba1\u4ece\u5c42\u5e8f\u8fd0\u884c\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u4e0a\u9762\u4e24\u5957\u4ee3\u7801\u7684\u7ed3\u679c\u662f\u4e00\u6837\u7684\uff0c\u4f46C++\u5728\u5904\u7406\u4e0a\u8ff0\u4e24\u79cd\u4ee3\u7801\u65f6\uff0c\u5176\u5185\u5b58\u5206\u914d\u7684\u5904\u7406\u7ec6\u8282\u662f\u4e0d\u540c\u7684\u3002 \u5bf9\u4e8e\u56fa\u5b9a\u957f\u5ea6\u6570\u7ec4\u7684\u4f8b\u5b50\u6765\u8bf4\uff0c\u7a0b\u5e8f\u4f1a\u5728\u5185\u5b58\u4e2d\u5206\u914d\u4e00\u4e2a\u957f\u5ea6\u4e3a5\u7684\u6570\u7ec4\uff0c\u540c\u65f6\u5c06\u5185\u5b58\u521d\u59cb\u5316\u4e3a \u201c Alex\\0 \u201d\uff0c\u56e0\u4e3a\u8be5\u5185\u5b58\u5df2\u7ecf\u88ab\u5206\u914d\u7ed9\u8be5\u6570\u7ec4\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u81ea\u7531\u5730\u4fee\u6539\u6570\u7ec4\u7684\u5185\u5bb9\u3002\u8fd9\u4e2a\u6570\u7ec4\u4f1a\u88ab\u5f53\u505a\u4e00\u822c\u7684\u5c40\u90e8\u53d8\u91cf\u6765\u5bf9\u5f85\uff0c\u56e0\u6b64\u5f53\u5b83 \u79bb\u5f00\u4f5c\u7528\u57df \u65f6\uff0c\u5176\u6240\u4f7f\u7528\u7684\u5185\u5b58\u4f1a\u88ab\u91ca\u653e\u4ee5\u4fbf\u5176\u4ed6\u53d8\u91cf\u4f7f\u7528\u3002 \u800c\u5bf9\u4e8e\u7b26\u53f7\u5e38\u91cf\u7684\u4f8b\u5b50\u4e2d\u6765\u8bf4\uff0c\u7f16\u8bd1\u5668\u5982\u4f55\u5904\u7406\u53d6\u51b3\u4e8e\u5177\u4f53\u5b9e\u73b0\u3002 \u4e00\u822c \u6765\u8bb2\uff0c\u7f16\u8bd1\u5668\u4f1a\u628a\u201c Alex\\0 \u201d \u653e\u5728\u67d0\u5904\u53ea\u8bfb\u5185\u5b58\uff0c\u7136\u540e\u4f7f\u7528\u4e00\u4e2a\u6307\u9488\u6307\u5411\u5b83\u3002\u56e0\u4e3a\u8be5\u5185\u5b58\u662f\u53ea\u8bfb\u7684\uff0c\u6240\u4ee5\u6700\u597d\u5c06\u8be5\u5b57\u7b26\u4e32\u8bbe\u7f6e\u4e3aconst\u3002 \u7f16\u8bd1\u5668\u4f1a\u51fa\u4e8e\u6027\u80fd\u4f18\u5316\u7684\u8003\u91cf\uff0c\u5c06\u591a\u4e2a\u76f8\u540c\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u5408\u5e76\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u503c\u3002\u4f8b\u5982\uff1a 1 2 const char * name1 { \"Alex\" }; const char * name2 { \"Alex\" }; \u8fd9\u662f\u4e24\u4e2a\u5177\u6709\u76f8\u540c\u503c\u7684\u4e0d\u540c\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u3002\u7f16\u8bd1\u5668\u53ef\u4ee5\u9009\u62e9\u5c06\u5b83\u4eec\u7ec4\u5408\u6210\u4e00\u4e2a\u5171\u4eab\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u503c\uff0c name1 \u548c name2 \u90fd\u6307\u5411\u76f8\u540c\u7684\u5730\u5740\u3002\u56e0\u6b64\uff0c\u5982\u679c name1 \u4e0d\u662f const \uff0c\u90a3\u4e48\u5bf9 name1 \u8fdb\u884c\u66f4\u6539\u4e5f\u4f1a\u5f71\u54cd name2 (\u8fd9\u53ef\u80fd\u662f\u610f\u6599\u4e4b\u5916\u7684)\u3002 \u7531\u4e8e\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u88ab\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\u7684\u56fa\u5b9a\u4f4d\u7f6e\uff0c\u56e0\u6b64\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u5177\u6709\u9759\u6001\u6301\u7eed\u65f6\u95f4\uff0c\u800c\u4e0d\u662f\u81ea\u52a8\u6301\u7eed\u65f6\u95f4(\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4eec\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u9500\u6bc1\uff0c\u800c\u4e0d\u662f\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u5757\u7ed3\u675f\u65f6\u9500\u6bc1)\u3002\u8fd9\u610f\u5473\u7740\u5f53\u6211\u4eec\u4f7f\u7528\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u65f6\uff0c\u6211\u4eec\u4e0d\u5fc5\u62c5\u5fc3\u4f5c\u7528\u57df\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u8fd9\u4e48\u505a\u662f\u53ef\u4ee5\u7684\uff1a 1 2 3 4 const char * getName () { return \"Alex\" ; } \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c getName() \u5c06\u8fd4\u56de\u4e00\u4e2a\u6307\u5411C\u98ce\u683c\u5b57\u7b26\u4e32 \"Alex\" \u7684\u6307\u9488\u3002\u5982\u679c\u8be5\u51fd\u6570\u6309\u5730\u5740\u8fd4\u56de\u4efb\u4f55\u5176\u4ed6\u5c40\u90e8\u53d8\u91cf\uff0c\u5219\u8be5\u53d8\u91cf\u5c06\u5728 getName() '\u7ed3\u675f\u65f6\u9500\u6bc1\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u60ac\u7a7a\u6307\u9488\u7ed9\u8c03\u7528\u8005\u3002\u4f46\u662f\uff0c\u56e0\u4e3a\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u5177\u6709\u9759\u6001\u6301\u7eed\u65f6\u95f4\uff0c\u6240\u4ee5\u5f53 getName() \u7ec8\u6b62\u65f6\uff0c \"Alex\" \u4e0d\u4f1a\u88ab\u9500\u6bc1\uff0c\u56e0\u6b64\u8c03\u7528\u8005\u4ecd\u7136\u53ef\u4ee5\u8bbf\u95ee\u5b83\u3002 C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u5728\u5f88\u591a\u65e7\u7684\u6216\u5e95\u5c42\u4ee3\u7801\u4e2d\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u5360\u7528\u7684\u5185\u5b58\u975e\u5e38\u5c0f\u3002\u73b0\u4ee3\u4ee3\u7801\u5e94\u8be5\u66f4\u503e\u5411\u4e8e\u4f7f\u7528 std::string \u548c std::string_view \uff0c\u56e0\u4e3a\u5b83\u4eec\u63d0\u4f9b\u4e86\u5b89\u5168\u4e14\u6613\u7528\u7684\u5b57\u7b26\u4e32\u8bbf\u95ee\u3002 std::cout \u548c\u5b57\u7b26\u6307\u9488 \u5b66\u5230\u8fd9\u4e00\u7ae0\u7684\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u5df2\u7ecf\u6ce8\u610f\u5230\uff0c std::cout \u53ef\u4ee5\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6307\u9488\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int nArray [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; char cArray []{ \"Hello!\" }; const char * name { \"Alex\" }; std :: cout << nArray << '\\n' ; // nArray will decay to type int* std :: cout << cArray << '\\n' ; // cArray will decay to type char* std :: cout << name << '\\n' ; // name is already type char* return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u6253\u5370\u4e0b\u9762\u7684\u4fe1\u606f\uff1a 1 2 3 003AF738 Hello! Alex \u4e3a\u4ec0\u4e48\u6574\u578b\u6570\u7ec4\u6253\u5370\u7684\u5c31\u662f\u5730\u5740\uff0c\u800c\u5b57\u7b26\u6570\u7ec4\u5c31\u80fd\u6253\u5370\u5b57\u7b26\u4e32\uff1f \u8fd9\u662f\u56e0\u4e3a std::cout \u53ef\u4ee5\u63a8\u65ad\u4f60\u7684\u610f\u56fe\u3002\u5982\u679c\u4f60\u4f20\u5165\u4e86\u4e00\u4e2a\u975e\u5b57\u7b26\u7c7b\u578b\u7684\u6307\u9488\uff0c\u5b83\u4f1a\u6253\u5370\u8be5\u6307\u9488\u7684\u5185\u5bb9\uff08\u6307\u9488\u6301\u6709\u7684\u5730\u5740\u503c\uff09\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u4f20\u9012\u7684\u662f\u4e00\u4e2a char* \u6216 const char* \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u5b83\u4f1a\u5047\u5b9a\u4f60\u5e0c\u671b\u6253\u5370\u7684\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u56e0\u6b64\uff0c\u5b83\u6253\u5370\u7684\u662f\u6307\u9488\u6307\u5411\u7684\u5b57\u7b26\u4e32\u7684\u503c\u800c\u4e0d\u662f\u6307\u9488\u6301\u6709\u7684\u5730\u5740\u503c\u3002 \u5c3d\u7ba1\uff0c99%\u7684\u60c5\u51b5\u4e0b\u90fd\u662f\u6ca1\u95ee\u9898\u7684\uff0c\u4f46\u662f\u5176\u4ecd\u6709\u53ef\u80fd\u4ea7\u751f\u975e\u9884\u671f\u7684\u7ed3\u679c\u3002\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char c { 'Q' }; //\u6ce8\u610f\u8fd9\u91cc\u662f\u4e00\u4e2a\u5b57\u7b26 std :: cout << & c ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u5458\u6253\u7b97\u6253\u5370\u53d8\u91cfC\u7684\u5730\u5740\u3002\u7136\u800c\uff0c &c \u7684\u7c7b\u578b\u662f char* \uff0c\u6240\u4ee5 std::cout \u5c06\u5176\u4f5c\u4e3a\u5b57\u7b26\u4e32\u6253\u5370\uff01 \u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\uff0c\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 Q\u2560\u2560\u2560\u2560\u255c\u25614;\u00bf\u25a0A \u4e3a\u4ec0\u4e48\u8fd9\u6837\u5462\uff1f std::cout \u5047\u8bbe &c (\u7c7b\u578b\u4e3a char* )\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u6240\u4ee5\u5b83\u6253\u5370\u4e86\u201cQ\u201d\uff0c\u7136\u540e\u7ee7\u7eed\u6253\u5370\u63a5\u4e0b\u6765\u5185\u5b58\u4e2d\uff08\u4e00\u5806\u5783\u573e\u503c\uff09\u3002\u6700\u7ec8\uff0c\u5b83\u9047\u5230\u4e86\u4fdd\u5b580\u503c\u7684\u5185\u5b58\u4e2d\uff0c\u5e76\u5c06\u5176\u89e3\u91ca\u4e3a\u7a7a\u7ec8\u6b62\u7b26\u3002\u4f60\u770b\u5230\u7684\u53ef\u80fd\u4f1a\u4e0d\u540c\uff0c\u8fd9\u53d6\u51b3\u4e8e\u53d8\u91cfC\u540e\u9762\u7684\u5185\u5b58\u3002 \u8fd9\u79cd\u60c5\u51b5\u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\u4e0d\u592a\u53ef\u80fd\u53d1\u751f(\u56e0\u4e3a\u60a8\u4e0d\u592a\u53ef\u80fd\u771f\u7684\u60f3\u6253\u5370\u5185\u5b58\u5730\u5740)\uff0c\u4f46\u5b83\u8bf4\u660e\u4e86\u4e8b\u60c5\u662f\u5982\u4f55\u5728\u5e55\u540e\u5de5\u4f5c\u7684\uff0c\u4ee5\u53ca\u7a0b\u5e8f\u662f\u5982\u4f55\u5728\u4e0d\u7ecf\u610f\u95f4\u504f\u79bb\u8f68\u9053\u7684\u3002","title":"11.10 - C \u98ce\u683c\u5b57\u7b26\u4e32\u7b26\u53f7\u5e38\u91cf"},{"location":"11-10-C-style-string-symbolic-constants/#c","text":"\u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u521d\u59cb\u5316\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char myName []{ \"Alex\" }; // \u56fa\u5b9a\u6570\u7ec4 std :: cout << myName << '\\n' ; return 0 ; } C++ \u4e5f\u652f\u6301\u4f7f\u7528\u6307\u9488\u6307\u5411\u7b26\u53f7\u5e38\u91cf\u6765\u521b\u5efaC\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { const char * myName { \"Alex\" }; // pointer to symbolic constant std :: cout << myName << '\\n' ; return 0 ; } \u5c3d\u7ba1\u4ece\u5c42\u5e8f\u8fd0\u884c\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u4e0a\u9762\u4e24\u5957\u4ee3\u7801\u7684\u7ed3\u679c\u662f\u4e00\u6837\u7684\uff0c\u4f46C++\u5728\u5904\u7406\u4e0a\u8ff0\u4e24\u79cd\u4ee3\u7801\u65f6\uff0c\u5176\u5185\u5b58\u5206\u914d\u7684\u5904\u7406\u7ec6\u8282\u662f\u4e0d\u540c\u7684\u3002 \u5bf9\u4e8e\u56fa\u5b9a\u957f\u5ea6\u6570\u7ec4\u7684\u4f8b\u5b50\u6765\u8bf4\uff0c\u7a0b\u5e8f\u4f1a\u5728\u5185\u5b58\u4e2d\u5206\u914d\u4e00\u4e2a\u957f\u5ea6\u4e3a5\u7684\u6570\u7ec4\uff0c\u540c\u65f6\u5c06\u5185\u5b58\u521d\u59cb\u5316\u4e3a \u201c Alex\\0 \u201d\uff0c\u56e0\u4e3a\u8be5\u5185\u5b58\u5df2\u7ecf\u88ab\u5206\u914d\u7ed9\u8be5\u6570\u7ec4\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u81ea\u7531\u5730\u4fee\u6539\u6570\u7ec4\u7684\u5185\u5bb9\u3002\u8fd9\u4e2a\u6570\u7ec4\u4f1a\u88ab\u5f53\u505a\u4e00\u822c\u7684\u5c40\u90e8\u53d8\u91cf\u6765\u5bf9\u5f85\uff0c\u56e0\u6b64\u5f53\u5b83 \u79bb\u5f00\u4f5c\u7528\u57df \u65f6\uff0c\u5176\u6240\u4f7f\u7528\u7684\u5185\u5b58\u4f1a\u88ab\u91ca\u653e\u4ee5\u4fbf\u5176\u4ed6\u53d8\u91cf\u4f7f\u7528\u3002 \u800c\u5bf9\u4e8e\u7b26\u53f7\u5e38\u91cf\u7684\u4f8b\u5b50\u4e2d\u6765\u8bf4\uff0c\u7f16\u8bd1\u5668\u5982\u4f55\u5904\u7406\u53d6\u51b3\u4e8e\u5177\u4f53\u5b9e\u73b0\u3002 \u4e00\u822c \u6765\u8bb2\uff0c\u7f16\u8bd1\u5668\u4f1a\u628a\u201c Alex\\0 \u201d \u653e\u5728\u67d0\u5904\u53ea\u8bfb\u5185\u5b58\uff0c\u7136\u540e\u4f7f\u7528\u4e00\u4e2a\u6307\u9488\u6307\u5411\u5b83\u3002\u56e0\u4e3a\u8be5\u5185\u5b58\u662f\u53ea\u8bfb\u7684\uff0c\u6240\u4ee5\u6700\u597d\u5c06\u8be5\u5b57\u7b26\u4e32\u8bbe\u7f6e\u4e3aconst\u3002 \u7f16\u8bd1\u5668\u4f1a\u51fa\u4e8e\u6027\u80fd\u4f18\u5316\u7684\u8003\u91cf\uff0c\u5c06\u591a\u4e2a\u76f8\u540c\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u5408\u5e76\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u503c\u3002\u4f8b\u5982\uff1a 1 2 const char * name1 { \"Alex\" }; const char * name2 { \"Alex\" }; \u8fd9\u662f\u4e24\u4e2a\u5177\u6709\u76f8\u540c\u503c\u7684\u4e0d\u540c\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u3002\u7f16\u8bd1\u5668\u53ef\u4ee5\u9009\u62e9\u5c06\u5b83\u4eec\u7ec4\u5408\u6210\u4e00\u4e2a\u5171\u4eab\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u503c\uff0c name1 \u548c name2 \u90fd\u6307\u5411\u76f8\u540c\u7684\u5730\u5740\u3002\u56e0\u6b64\uff0c\u5982\u679c name1 \u4e0d\u662f const \uff0c\u90a3\u4e48\u5bf9 name1 \u8fdb\u884c\u66f4\u6539\u4e5f\u4f1a\u5f71\u54cd name2 (\u8fd9\u53ef\u80fd\u662f\u610f\u6599\u4e4b\u5916\u7684)\u3002 \u7531\u4e8e\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u88ab\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\u7684\u56fa\u5b9a\u4f4d\u7f6e\uff0c\u56e0\u6b64\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u5177\u6709\u9759\u6001\u6301\u7eed\u65f6\u95f4\uff0c\u800c\u4e0d\u662f\u81ea\u52a8\u6301\u7eed\u65f6\u95f4(\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4eec\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u9500\u6bc1\uff0c\u800c\u4e0d\u662f\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u5757\u7ed3\u675f\u65f6\u9500\u6bc1)\u3002\u8fd9\u610f\u5473\u7740\u5f53\u6211\u4eec\u4f7f\u7528\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u65f6\uff0c\u6211\u4eec\u4e0d\u5fc5\u62c5\u5fc3\u4f5c\u7528\u57df\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u8fd9\u4e48\u505a\u662f\u53ef\u4ee5\u7684\uff1a 1 2 3 4 const char * getName () { return \"Alex\" ; } \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c getName() \u5c06\u8fd4\u56de\u4e00\u4e2a\u6307\u5411C\u98ce\u683c\u5b57\u7b26\u4e32 \"Alex\" \u7684\u6307\u9488\u3002\u5982\u679c\u8be5\u51fd\u6570\u6309\u5730\u5740\u8fd4\u56de\u4efb\u4f55\u5176\u4ed6\u5c40\u90e8\u53d8\u91cf\uff0c\u5219\u8be5\u53d8\u91cf\u5c06\u5728 getName() '\u7ed3\u675f\u65f6\u9500\u6bc1\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u60ac\u7a7a\u6307\u9488\u7ed9\u8c03\u7528\u8005\u3002\u4f46\u662f\uff0c\u56e0\u4e3a\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u5177\u6709\u9759\u6001\u6301\u7eed\u65f6\u95f4\uff0c\u6240\u4ee5\u5f53 getName() \u7ec8\u6b62\u65f6\uff0c \"Alex\" \u4e0d\u4f1a\u88ab\u9500\u6bc1\uff0c\u56e0\u6b64\u8c03\u7528\u8005\u4ecd\u7136\u53ef\u4ee5\u8bbf\u95ee\u5b83\u3002 C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u5728\u5f88\u591a\u65e7\u7684\u6216\u5e95\u5c42\u4ee3\u7801\u4e2d\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u5360\u7528\u7684\u5185\u5b58\u975e\u5e38\u5c0f\u3002\u73b0\u4ee3\u4ee3\u7801\u5e94\u8be5\u66f4\u503e\u5411\u4e8e\u4f7f\u7528 std::string \u548c std::string_view \uff0c\u56e0\u4e3a\u5b83\u4eec\u63d0\u4f9b\u4e86\u5b89\u5168\u4e14\u6613\u7528\u7684\u5b57\u7b26\u4e32\u8bbf\u95ee\u3002","title":"C \u98ce\u683c\u5b57\u7b26\u4e32\u5e38\u91cf"},{"location":"11-10-C-style-string-symbolic-constants/#stdcout","text":"\u5b66\u5230\u8fd9\u4e00\u7ae0\u7684\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u5df2\u7ecf\u6ce8\u610f\u5230\uff0c std::cout \u53ef\u4ee5\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6307\u9488\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int nArray [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; char cArray []{ \"Hello!\" }; const char * name { \"Alex\" }; std :: cout << nArray << '\\n' ; // nArray will decay to type int* std :: cout << cArray << '\\n' ; // cArray will decay to type char* std :: cout << name << '\\n' ; // name is already type char* return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u6253\u5370\u4e0b\u9762\u7684\u4fe1\u606f\uff1a 1 2 3 003AF738 Hello! Alex \u4e3a\u4ec0\u4e48\u6574\u578b\u6570\u7ec4\u6253\u5370\u7684\u5c31\u662f\u5730\u5740\uff0c\u800c\u5b57\u7b26\u6570\u7ec4\u5c31\u80fd\u6253\u5370\u5b57\u7b26\u4e32\uff1f \u8fd9\u662f\u56e0\u4e3a std::cout \u53ef\u4ee5\u63a8\u65ad\u4f60\u7684\u610f\u56fe\u3002\u5982\u679c\u4f60\u4f20\u5165\u4e86\u4e00\u4e2a\u975e\u5b57\u7b26\u7c7b\u578b\u7684\u6307\u9488\uff0c\u5b83\u4f1a\u6253\u5370\u8be5\u6307\u9488\u7684\u5185\u5bb9\uff08\u6307\u9488\u6301\u6709\u7684\u5730\u5740\u503c\uff09\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u4f20\u9012\u7684\u662f\u4e00\u4e2a char* \u6216 const char* \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u5b83\u4f1a\u5047\u5b9a\u4f60\u5e0c\u671b\u6253\u5370\u7684\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u56e0\u6b64\uff0c\u5b83\u6253\u5370\u7684\u662f\u6307\u9488\u6307\u5411\u7684\u5b57\u7b26\u4e32\u7684\u503c\u800c\u4e0d\u662f\u6307\u9488\u6301\u6709\u7684\u5730\u5740\u503c\u3002 \u5c3d\u7ba1\uff0c99%\u7684\u60c5\u51b5\u4e0b\u90fd\u662f\u6ca1\u95ee\u9898\u7684\uff0c\u4f46\u662f\u5176\u4ecd\u6709\u53ef\u80fd\u4ea7\u751f\u975e\u9884\u671f\u7684\u7ed3\u679c\u3002\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char c { 'Q' }; //\u6ce8\u610f\u8fd9\u91cc\u662f\u4e00\u4e2a\u5b57\u7b26 std :: cout << & c ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u5458\u6253\u7b97\u6253\u5370\u53d8\u91cfC\u7684\u5730\u5740\u3002\u7136\u800c\uff0c &c \u7684\u7c7b\u578b\u662f char* \uff0c\u6240\u4ee5 std::cout \u5c06\u5176\u4f5c\u4e3a\u5b57\u7b26\u4e32\u6253\u5370\uff01 \u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\uff0c\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 Q\u2560\u2560\u2560\u2560\u255c\u25614;\u00bf\u25a0A \u4e3a\u4ec0\u4e48\u8fd9\u6837\u5462\uff1f std::cout \u5047\u8bbe &c (\u7c7b\u578b\u4e3a char* )\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u6240\u4ee5\u5b83\u6253\u5370\u4e86\u201cQ\u201d\uff0c\u7136\u540e\u7ee7\u7eed\u6253\u5370\u63a5\u4e0b\u6765\u5185\u5b58\u4e2d\uff08\u4e00\u5806\u5783\u573e\u503c\uff09\u3002\u6700\u7ec8\uff0c\u5b83\u9047\u5230\u4e86\u4fdd\u5b580\u503c\u7684\u5185\u5b58\u4e2d\uff0c\u5e76\u5c06\u5176\u89e3\u91ca\u4e3a\u7a7a\u7ec8\u6b62\u7b26\u3002\u4f60\u770b\u5230\u7684\u53ef\u80fd\u4f1a\u4e0d\u540c\uff0c\u8fd9\u53d6\u51b3\u4e8e\u53d8\u91cfC\u540e\u9762\u7684\u5185\u5b58\u3002 \u8fd9\u79cd\u60c5\u51b5\u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\u4e0d\u592a\u53ef\u80fd\u53d1\u751f(\u56e0\u4e3a\u60a8\u4e0d\u592a\u53ef\u80fd\u771f\u7684\u60f3\u6253\u5370\u5185\u5b58\u5730\u5740)\uff0c\u4f46\u5b83\u8bf4\u660e\u4e86\u4e8b\u60c5\u662f\u5982\u4f55\u5728\u5e55\u540e\u5de5\u4f5c\u7684\uff0c\u4ee5\u53ca\u7a0b\u5e8f\u662f\u5982\u4f55\u5728\u4e0d\u7ecf\u610f\u95f4\u504f\u79bb\u8f68\u9053\u7684\u3002","title":"std::cout \u548c\u5b57\u7b26\u6307\u9488"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/","tags":["new","delete","dynamic-memory"],"text":"Key Takeaway \u52a8\u6001\u5185\u5b58\u5206\u914d\u7684\u5fc5\u8981\u6027 C++ \u652f\u6301\u4e09\u79cd\u57fa\u672c\u7c7b\u578b\u7684\u5185\u5b58\u5206\u914d \uff0c\u4f60\u5e94\u8be5\u5df2\u7ecf\u89c1\u8fc7\u5176\u4e2d\u7684\u4e24\u79cd\u4e86\uff1a \u9759\u6001\u5185\u5b58\u5206\u914d \u53d1\u751f\u5728\u9759\u6001\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u521b\u5efa\u65f6\u3002\u5f53\u7a0b\u5e8f\u542f\u52a8\u65f6\uff0c\u6b64\u7c7b\u53d8\u91cf\u7684\u5185\u5b58\u4f1a\u88ab\u5206\u914d\u5e76\u6301\u7eed\u5230\u7a0b\u5e8f\u7ed3\u675f\uff0c\u8d2f\u7a7f\u7a0b\u5e8f\u7684\u6574\u4e2a\u751f\u547d\u5468\u671f\uff1b \u81ea\u52a8\u5185\u5b58\u5206\u914d \u53d1\u751f\u5728\u51fd\u6570\u5f62\u53c2\u548c\u5c40\u90e8\u53d8\u91cf\u521b\u5efa\u65f6\u3002\u8fd9\u4e9b\u53d8\u91cf\u7684\u5185\u5b58\u4f1a\u5728\u8fdb\u5165\u76f8\u5e94\u7684\u8bed\u53e5\u5757\u65f6\u88ab\u81ea\u52a8\u5206\u914d\uff0c\u800c\u5f53\u8bed\u53e5\u5757\u9000\u51fa\u65f6\u5219\u4f1a\u81ea\u52a8\u91ca\u653e\uff1b \u52a8\u6001\u5185\u5b58\u5206\u914d \u5219\u662f\u672c\u8282\u8bfe\u8981\u4ecb\u7ecd\u7684\u3002 \u9759\u6001\u5185\u5b58\u5206\u914d\u548c\u81ea\u52a8\u5185\u5b58\u5206\u914d\u6709\u4e24\u4e2a\u5171\u540c\u70b9\uff1a \u53d8\u91cf\u548c\u6570\u7ec4\u7684\u5927\u5c0f\u5fc5\u987b\u5728 \u7f16\u8bd1\u65f6 \u5c31\u5df2\u77e5\uff1b \u5185\u5b58\u7684\u5206\u914d\u548c\u91ca\u653e\u662f\u81ea\u52a8\u8fdb\u884c\u7684\uff08\u5728\u53d8\u91cf\u5b9e\u4f8b\u5316\u548c\u9500\u6bc1\u65f6\uff09\u3002 \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u662f\u5df2\u7ecf\u8db3\u591f\u4e86\u3002\u4f46\u662f\uff0c\u6709\u65f6\u5019\u8fd9\u4e24\u4e2a\u7279\u70b9\u4f1a\u5e26\u6765\u9650\u5236\uff0c\u901a\u5e38\u662f\u5728\u5904\u7406\u5916\u90e8(\u7528\u6237\u6216\u6587\u4ef6)\u8f93\u5165\u65f6\u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u80fd\u60f3\u4f7f\u7528\u4e00\u4e2a\u5b57\u7b26\u4e32\u6765\u4fdd\u5b58\u67d0\u4eba\u7684\u540d\u5b57\uff0c\u4f46\u5728\u7528\u6237\u8f93\u5165\u540d\u5b57\u4e4b\u524d\uff0c\u6211\u4eec\u5e76\u4e0d\u77e5\u9053\u540d\u5b57\u6709\u591a\u957f\u3002\u6216\u8005\u6211\u4eec\u53ef\u80fd\u60f3\u4ece\u78c1\u76d8\u8bfb\u5165\u4e00\u4e9b\u8bb0\u5f55\uff0c\u4f46\u4e8b\u5148\u4e0d\u77e5\u9053\u6709\u591a\u5c11\u8bb0\u5f55\u3002\u6216\u8005\u6211\u4eec\u6b63\u5728\u521b\u9020\u4e00\u6b3e\u602a\u7269\u6570\u91cf\u52a8\u6001\u53d8\u5316\u7684\u6e38\u620f(\u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\uff0c\u4e00\u4e9b\u602a\u7269\u4f1a\u6b7b\u4ea1\uff0c\u65b0\u602a\u7269\u4f1a\u51fa\u73b0)\u3002 \u5982\u679c\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u5c31\u58f0\u660e\u53d8\u91cf\u6216\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u90a3\u6211\u4eec\u80fd\u505a\u7684\u5c31\u662f\u5c1d\u8bd5\u731c\u6d4b\u9700\u8981\u7684\u53d8\u91cf\u7684\u5927\u5c0f\u7684\u6700\u5927\u503c\u5e76\u671f\u671b\u5b9e\u9645\u4f7f\u7528\u4e2d\u8be5\u5185\u5b58\u662f\u8db3\u591f\u7684\uff1a 1 2 3 4 char name [ 25 ]; // \u671f\u671b\u7528\u6237\u7684\u59d3\u540d\u4e0d\u8d85\u8fc725\u4e2a\u5b57\u7b26 Record record [ 500 ]; // \u5e0c\u671b\u6570\u636e\u7684\u8bb0\u5f55\u6570\u5c0f\u4e8e500\u6761 Monster monster [ 40 ]; // \u6700\u591a 40 \u4e2a\u602a\u7269 Polygon rendering [ 30000 ]; // 3d \u6e32\u67d3\u4e0d\u8d85\u8fc73\u4e07\u4e2a\u591a\u8fb9\u5f62 \u8fd9\u79cd\u89e3\u51b3\u65b9\u6848\u662f\u5f88\u5dee\u52b2\u7684\uff0c\u539f\u56e0\u6709\u56db\u6761\uff1a \u9996\u5148\uff0c\u5982\u679c\u5b9e\u9645\u6ca1\u6709\u4f7f\u7528\u8be5\u53d8\u91cf\uff0c\u5219\u4f1a\u5bfc\u81f4\u5185\u5b58\u6d6a\u8d39\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u4e3a\u6bcf\u4e2a\u540d\u79f0\u5206\u914d25\u4e2a\u5b57\u7b26\uff0c\u4f46\u540d\u79f0\u5e73\u5747\u53ea\u670912\u4e2a\u5b57\u7b26\u957f\uff0c\u90a3\u4e48\u7533\u8bf7\u7684\u5185\u5b58\u662f\u5b9e\u9645\u4f7f\u7528\u7684\u4e24\u500d\u591a\u3002\u6216\u8005\uff0c\u5bf9\u4e8e\u4e0a\u9762 rendering \u6570\u7ec4\u7684\u4f8b\u5b50\u6765\u8bf4\uff1a\u5982\u679c\u6e32\u67d3\u53ea\u4f7f\u752810000\u4e2a\u591a\u8fb9\u5f62\uff0c\u90a3\u4e48\u5c06\u670920000\u4e2a\u591a\u8fb9\u5f62\u7684\u5185\u5b58\u88ab\u989d\u5916\u7533\u8bf7\u5355\u6ca1\u6709\u88ab\u4f7f\u7528! \u7b2c\u4e8c\uff0c\u6211\u4eec\u5982\u4f55\u5224\u65ad\u54ea\u4e9b\u5185\u5b58\u4f4d\u5b9e\u9645\u4e0a\u88ab\u4f7f\u7528\u4e86\u5462\uff1f\u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0c\u8fd9\u5f88\u7b80\u5355\uff1a\u4ee5 \\0 \u5f00\u5934\u7684\u5b57\u7b26\u4e32\u663e\u7136\u6ca1\u6709\u88ab\u4f7f\u7528\u3002\u4f46\u662f\u5bf9\u4e8e monster[24] \u6765\u8bf4\u5462\uff1f\u5b83\u4ecd\u7136\u5728\u88ab\u4f7f\u7528\u5417\uff1f\u6211\u4eec\u9700\u8981\u67d0\u79cd\u65b9\u6cd5\u6765\u533a\u5206\u4ecd\u7136\u5728\u4f7f\u7528\u7684\u53d8\u91cf\u548c\u4e0d\u518d\u88ab\u4f7f\u7528\u7684\u53d8\u91cf\uff0c\u8fd9\u589e\u52a0\u4e86\u590d\u6742\u6027\uff0c\u5e76\u53ef\u80fd\u4f1a\u5360\u7528\u989d\u5916\u7684\u5185\u5b58\u3002 \u7b2c\u4e09\uff0c\u5927\u591a\u6570\u666e\u901a\u53d8\u91cf(\u5305\u62ec\u56fa\u5b9a\u6570\u7ec4)\u88ab\u5206\u914d\u5230\u79f0\u4e3a \u6808 \u7684\u5185\u5b58\u4e2d\u3002\u7a0b\u5e8f\u7684\u5806\u6808\u80fd\u591f\u4f7f\u7528\u7684\u5185\u5b58\u901a\u5e38\u975e\u5e38\u5c0f\u2014\u2014Visual Studio \u9ed8\u8ba4\u5806\u6808\u5927\u5c0f\u4e3a 1MB\u3002\u5982\u679c\u8d85\u8fc7\u8fd9\u4e2a\u5927\u5c0f\uff0c\u5c31\u4f1a\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5c31\u4f1a\u505c\u6b62\u7a0b\u5e8f\u3002 \u5728 Visual Studio \u4e2d\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\u65f6\u53d1\u751f\u7684\u60c5\u51b5\uff1a 1 2 3 4 int main () { int array [ 1000000 ]; // allocate 1 million integers (probably 4MB of memory) } \u5c06\u5185\u5b58\u9650\u5236\u4e3a1MB\u5bf9\u4e8e\u8bb8\u591a\u7a0b\u5e8f\u6765\u8bf4\u90fd\u662f\u6709\u95ee\u9898\u7684\uff0c\u7279\u522b\u662f\u90a3\u4e9b\u5904\u7406\u56fe\u5f62\u7684\u7a0b\u5e8f\u3002\u5728Visual Studio\u4e2d\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\u65f6\u53d1\u751f\u7684\u60c5\u51b5: \u7b2c\u56db\uff0c\u4e5f\u662f\u6700\u91cd\u8981\u7684\uff0c\u5b83\u4f1a\u5bfc\u81f4\u4eba\u4e3a\u7684\u9650\u5236\uff0c\u5e76\u53ef\u80fd\u5bfc\u81f4\u6ea2\u51fa\u3002\u5982\u679c\u7528\u6237\u8bd5\u56fe\u4ece\u78c1\u76d8\u8bfb\u5165600\u6761\u8bb0\u5f55\uff0c\u4f46\u6211\u4eec\u53ea\u5206\u914d\u4e86\u6700\u591a500\u6761\u8bb0\u5f55\u7684\u5185\u5b58\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u60c5\u51b5\uff1f\u6b64\u65f6\u8981\u4e48\u7ed9\u7528\u6237\u53d1\u9001\u4e00\u4e2a\u9519\u8bef\uff0c\u8981\u6c42\u5176\u53ea\u8bfb\u53d6500\u6761\u8bb0\u5f55\uff0c\u8981\u4e48(\u5728\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u6839\u672c\u4e0d\u5904\u7406\u8fd9\u79cd\u60c5\u51b5) record \u6570\u7ec4\u6ea2\u51fa\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u8fd9\u4e9b\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7\u52a8\u6001\u5185\u5b58\u5206\u914d\u8f7b\u677e\u89e3\u51b3\u3002 \u52a8\u6001\u5185\u5b58\u5206\u914d \u662f\u8fd0\u884c\u7a0b\u5e8f\u5728\u9700\u8981\u65f6\u5411\u64cd\u4f5c\u7cfb\u7edf\u8bf7\u6c42\u5185\u5b58\u7684\u4e00\u79cd\u65b9\u6cd5\u3002\u8fd9\u4e2a\u5185\u5b58\u4e0d\u662f\u6765\u81ea\u7a0b\u5e8f\u6709\u9650\u7684\u5806\u6808\u5185\u5b58\u2014\u2014\u76f8\u53cd\uff0c\u5b83\u662f\u4ece\u4e00\u4e2a\u66f4\u5927\u7684\u5185\u5b58\u6c60\u4e2d\u5206\u914d\u7684\uff0c\u7531\u64cd\u4f5c\u7cfb\u7edf\u7ba1\u7406\uff0c\u79f0\u4e3a \u5806 \u3002\u5728\u73b0\u4ee3\u673a\u5668\u4e0a\uff0c\u5806\u7684\u5927\u5c0f\u53ef\u4ee5\u8fbe\u5230\u5343\u5146\u5b57\u8282\u3002 \u5355\u4e00\u53d8\u91cf\u7684\u52a8\u6001\u5185\u5b58\u5206\u914d \u4e3a \u4e00\u4e2a \u53d8\u91cf\u5206\u914d\u5185\u5b58\uff0c\u6211\u4eec\u4f7f\u7528 new \u7684\u6807\u91cf\u5f62\u5f0f\uff1a 1 new int ; // dynamically allocate an integer (and discard the result) \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5411\u64cd\u4f5c\u7cfb\u7edf\u7533\u8bf7\u4e00\u4e2a\u6574\u578b\u7684\u5185\u5b58\u3002 new \u8fd0\u7b97\u7b26\u4f1a\u4f7f\u7528\u7533\u8bf7\u6765\u7684\u5185\u5b58\u521b\u5efa\u8fd9\u4e2a\u5bf9\u8c61\uff0c\u7136\u540e\u8fd4\u56de\u6307\u5411\u8be5\u5185\u5b58\u7684\u6307\u9488\u3002 \u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8fd4\u56de\u503c\u8d4b\u503c\u7ed9\u4e00\u4e2a\u6307\u9488\u53d8\u91cf\uff0c\u5e76\u901a\u8fc7\u5b83\u5728\u540e\u7eed\u7684\u7a0b\u5e8f\u4e2d\u8bbf\u95ee\u8fd9\u5757\u5185\u5b58\u3002 1 int * ptr { new int }; // dynamically allocate an integer and assign the address to ptr so we can access it later \u5728\u540e\u7eed\u7684\u7a0b\u5e8f\u4e2d\u901a\u8fc7 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u8bbf\u95ee\u8be5\u5185\u5b58\uff1a 1 * ptr = 7 ; // assign value of 7 to allocated memory \u5982\u679c\u4f60\u4e4b\u524d\u4e0d\u77e5\u9053\u6307\u9488\u6709\u4ec0\u4e48\u7528\uff0c\u90a3\u4e48\u73b0\u5728\u5e94\u8be5\u6e05\u695a\u4e86\uff0c\u81f3\u5c11\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u6ca1\u6709\u4e00\u4e2a\u6307\u9488\u6765\u4fdd\u5b58\u521a\u521a\u5206\u914d\u7684\u5185\u5b58\u5730\u5740\uff0c\u6211\u4eec\u5c31\u6ca1\u6709\u529e\u6cd5\u8bbf\u95ee\u521a\u521a\u5206\u914d\u7ed9\u6211\u4eec\u7684\u5185\u5b58! \u52a8\u6001\u5185\u5b58\u5206\u914d\u7684\u539f\u7406\u662f\u4ec0\u4e48\uff1f \u8ba1\u7b97\u673a\u4e2d\u7684\u5185\u5b58\u53ef\u4f9b\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u3002\u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u5c06\u7a0b\u5e8f\u52a0\u8f7d\u5230\u4e00\u5757\u5185\u5b58\u4e2d\u3002\u8fd9\u5757\u5185\u5b58\u88ab\u4f60\u7684\u7a0b\u5e8f\u5206\u5272\u6210\u51e0\u4e2a\u4e0d\u540c\u7684\u90e8\u5206\uff0c\u6bcf\u4e2a\u90e8\u5206\u4e5f\u90fd\u6709\u5176\u7279\u5b9a\u7684\u4f5c\u7528\u3002\u5176\u4e2d\u4e00\u5c0f\u90e8\u5206\u5305\u542b\u4f60\u7684\u7a0b\u5e8f\u4ee3\u7801\uff0c\u53e6\u5916\u4e00\u5c0f\u90e8\u5206\u5219\u7528\u4e8e\u7a0b\u5e8f\u6b63\u5e38\u8fd0\u884c\u65f6\u4f7f\u7528\uff08\u8bb0\u5f55\u51fd\u6570\u8c03\u7528\u3001\u521b\u5efa\u548c\u9500\u6bc1\u5168\u5c40\u53d8\u91cf\u548c\u4e34\u65f6\u53d8\u91cf\uff0c\u7b49\u7b49\uff09\u3002\u6211\u4eec\u4f1a\u5728\u540e\u9762\u8fdb\u884c\u66f4\u8be6\u7ec6\u4ecb\u7ecd\u3002\u4f46\u662f\uff0c\u8fd8\u6709\u5f88\u591a\u5f88\u591a\u7684\u5185\u5b58\u5c31\u53ea\u662f\u653e\u5728\u90a3\u91cc\uff0c\u7b49\u5f85\u4f60\u7684\u7a0b\u5e8f\u8bf7\u6c42\u4f7f\u7528\u3002 \u548c\u9759\u6001\u5185\u5b58\u3001\u81ea\u52a8\u5185\u5b58\u4e0d\u540c\u7684\u662f\uff0c\u7a0b\u5e8f\u81ea\u5df1\u9700\u8981\u8d1f\u8d23\u8bf7\u6c42\u548c\u91ca\u653e\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u3002 \u52a8\u6001\u5206\u914d\u53d8\u91cf\u7684\u521d\u59cb\u5316 \u5f53\u4f60\u52a8\u6001\u5206\u914d\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7 \u76f4\u63a5\u521d\u59cb\u5316 \u6216 \u7edf\u4e00\u521d\u59cb\u5316 \u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\uff1a 1 2 int * ptr1 { new int ( 5 ) }; // \u76f4\u63a5\u521d\u59cb\u5316 int * ptr2 { new int { 6 } }; // \u7edf\u4e00\u521d\u59cb\u5316 \u5220\u9664\u5355\u4e00\u53d8\u91cf \u5728\u4f7f\u7528\u5b8c\u52a8\u6001\u5206\u914d\u7684\u53d8\u91cf\u65f6\uff0c\u6211\u4eec\u9700\u8981\u663e\u5f0f\u5730\u544a\u8bc9C++\u91ca\u653e\u5185\u5b58\u4ee5\u4fbf\u91cd\u7528\u3002\u5bf9\u4e8e\u5355\u4e2a\u53d8\u91cf\uff0c\u8fd9\u662f\u901a\u8fc7\u6807\u91cf(\u975e\u6570\u7ec4)\u5f62\u5f0f\u7684 delete \u64cd\u4f5c\u7b26\u6765\u5b8c\u6210\u7684\uff1a 1 2 3 // assume ptr has previously been allocated with operator new delete ptr ; // return the memory pointed to by ptr to the operating system ptr = nullptr ; // set ptr to be a null pointer \u5220\u9664\u5185\u5b58\u662f\u4ec0\u4e48\u610f\u601d\uff1f delete \u8fd0\u7b97\u7b26\u5e76\u6ca1\u6709 \u5b9e\u9645\u4e0a \u5220\u9664\u4efb\u4f55\u4e1c\u897f\uff0c\u5b83\u53ea\u662f\u5c06\u6307\u9488\u6307\u5411\u7684\u5185\u5b58\u8fd8\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u7f62\u4e86\u3002\u64cd\u4f5c\u7cfb\u7edf\u968f\u5373\u4fbf\u53ef\u4ee5\u5c06\u8be5\u5185\u5b58\u5206\u914d\u7ed9\u5176\u4ed6\u5e94\u7528\u4f7f\u7528\uff08\u6216\u8005\u7ed9\u5f53\u524d\u5e94\u7528\u4f7f\u7528\u4e5f\u53ef\u4ee5\uff09\u3002 \u5c3d\u7ba1\u770b\u4e0a\u53bb\u6211\u4eec\u662f\u5728 delete \u4e00\u4e2a\u53d8\u91cf\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\uff01\u8be5\u6307\u9488\u53d8\u91cf\u4ecd\u7136\u5904\u4e8e\u8be5\u4f5c\u7528\u57df\uff0c\u800c\u4e14\u4e5f\u53ef\u4ee5\u88ab\u8d4b\u4e88\u65b0\u503c\u3002 \u6ce8\u610f\uff0c\u5220\u9664\u4e0d\u6307\u5411\u52a8\u6001\u5206\u914d\u5185\u5b58\u7684\u6307\u9488\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e0d\u597d\u7684\u4e8b\u60c5\u53d1\u751f\u3002 \u60ac\u5782\u6307\u9488 C++\u6ca1\u6709\u89c4\u5b9a\u88ab\u91ca\u653e\u7684\u5185\u5bb9\u5176\u5185\u5bb9\u5e94\u8be5\u662f\u4ec0\u4e48\uff0c\u4e5f\u6ca1\u6709\u89c4\u5b9a\u88ab\u5220\u9664\u7684\u6307\u9488\u7684\u503c\u5e94\u8be5\u662f\u4ec0\u4e48\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd4\u56de\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u7684\u5185\u5b58\u5c06\u5305\u542b\u5b83\u5728\u8fd4\u56de\u4e4b\u524d\u7684\u76f8\u540c\u503c\uff0c\u5e76\u4e14\u6307\u9488\u5c06\u6307\u5411\u73b0\u5728\u91ca\u653e\u7684\u5185\u5b58\u3002 \u6267\u884c\u5df2\u7ecf\u88ab\u91ca\u653e\u7684\u5185\u5b58\u7684\u6307\u9488\uff0c\u79f0\u4e3a \u60ac\u5782 \u6307\u9488\u3002\u95f4\u63a5\u8bbf\u95ee\u6216\u5220\u9664\u60ac\u5782\u6307\u9488\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int * ptr { new int }; // \u52a8\u6001\u5206\u914d\u4e00\u4e2a\u6574\u578b * ptr = 7 ; // \u5411\u8be5\u5185\u5b58\u8d4b\u4e00\u4e2a\u503c delete ptr ; // \u5185\u5b58\u8fd8\u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff0cptr\u73b0\u5728\u662f\u60ac\u5782\u6307\u9488 std :: cout << * ptr ; // \u95f4\u63a5\u8bbf\u95ee\u60ac\u5782\u6307\u9488\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a delete ptr ; // \u518d\u6b21\u91ca\u653e\u5df2\u7ecf\u88ab\u91ca\u653e\u7684\u5185\u5b58\u4e5f\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u4e4b\u524d\u5206\u914d\u7ed9\u5df2\u5206\u914d\u5185\u5b58\u7684\u503c7\u53ef\u80fd\u4ecd\u7136\u5b58\u5728\uff0c\u4f46\u8be5\u5185\u5b58\u5730\u5740\u7684\u503c\u53ef\u80fd\u5df2\u7ecf\u53d1\u751f\u4e86\u53d8\u5316\u3002\u5185\u5b58\u4e5f\u6709\u53ef\u80fd\u88ab\u5206\u914d\u7ed9\u53e6\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f(\u6216\u64cd\u4f5c\u7cfb\u7edf\u81ea\u5df1\u4f7f\u7528)\uff0c\u8bd5\u56fe\u8bbf\u95ee\u8be5\u5185\u5b58\u5c06\u5bfc\u81f4\u64cd\u4f5c\u7cfb\u7edf\u5173\u95ed\u7a0b\u5e8f\u3002 \u91ca\u653e\u5185\u5b58\u53ef\u80fd\u4f1a\u5bfc\u81f4\u591a\u4e2a\u60ac\u5782\u6307\u9488\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int * ptr { new int {} }; // \u52a8\u6001\u5206\u914d\u4e00\u4e2a\u6574\u578b int * otherPtr { ptr }; // otherPtr \u6307\u5411\u76f8\u540c\u7684\u5185\u5b58 delete ptr ; // \u5185\u5b58\u8fd8\u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff0cptr,otherPtr \u73b0\u5728\u662f\u60ac\u5782\u6307\u9488 ptr = nullptr ; // ptr \u73b0\u5728\u662fnullptr // \u4f46\u662f, otherPtr \u4ecd\u7136\u662f\u60ac\u5782\u6307\u9488 return 0 ; } \u901a\u8fc7\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u907f\u514d\u4e0a\u8ff0\u95ee\u9898\u3002 \u9996\u5148\uff0c\u5c3d\u91cf\u907f\u514d\u6709\u591a\u4e2a\u6307\u9488\u6307\u5411\u540c\u4e00\u5757\u52a8\u6001\u5185\u5b58\u3002\u5982\u679c\u8fd9\u662f\u4e0d\u53ef\u80fd\u7684\uff0c\u8981\u6e05\u695a\u54ea\u4e9b\u6307\u9488\u201c\u62e5\u6709\u201d\u5185\u5b58(\u5e76\u8d1f\u8d23\u5220\u9664\u5b83)\uff0c\u54ea\u4e9b\u6307\u9488\u53ea\u662f\u5728\u8bbf\u95ee\u5b83\u3002 \u5176\u6b21\uff0c\u5728\u5220\u9664\u6307\u9488\u65f6\uff0c\u5982\u679c\u8be5\u6307\u9488\u4e0d\u4f1a\u7acb\u5373\u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u5219\u5c06\u8be5\u6307\u9488\u8bbe\u7f6e\u4e3anullptr\u3002\u6211\u4eec\u5c06\u66f4\u591a\u5730\u8ba8\u8bba\u7a7a\u6307\u9488\uff0c\u4ee5\u53ca\u5b83\u4eec\u4e3a\u4ec0\u4e48\u6709\u7528\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c06\u5df2\u5220\u9664\u7684\u6307\u9488\u8bbe\u7f6e\u4e3anullptr\uff0c\u9664\u975e\u5b83\u4eec\u968f\u540e\u5c06\u7acb\u5373\u8d85\u51fa\u4f5c\u7528\u57df\u3002 new \u8fd0\u7b97\u7b26\u53ef\u80fd\u6267\u884c\u5931\u8d25 \u5f53\u5411\u64cd\u4f5c\u7cfb\u7edf\u8bf7\u6c42\u5185\u5b58\u65f6\uff0c\u5728\u6781\u5c11\u6570\u60c5\u51b5\u4e0b\uff0c\u64cd\u4f5c\u7cfb\u7edf\u53ef\u80fd\u5df2\u7ecf\u6ca1\u6709\u4efb\u4f55\u53ef\u7528\u7684\u5185\u5b58\u3002 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5982\u679c new \u5931\u8d25\uff0c\u5219\u629b\u51fa bad_alloc \u5f02\u5e38\u3002\u5982\u679c\u8fd9\u4e2a\u5f02\u5e38\u6ca1\u6709\u5f97\u5230\u6b63\u786e\u5904\u7406(\u5b83\u4e0d\u4f1a\u5f97\u5230\u6b63\u786e\u5904\u7406\uff0c\u56e0\u4e3a\u6211\u4eec\u8fd8\u6ca1\u6709\u8ba8\u8bba\u5f02\u5e38\u6216\u5f02\u5e38\u5904\u7406)\uff0c\u7a0b\u5e8f\u5c06\u7ec8\u6b62(\u5d29\u6e83)\uff0c\u5e76\u51fa\u73b0\u4e00\u4e2a\u672a\u5904\u7406\u7684\u5f02\u5e38\u9519\u8bef\u3002 \u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u8ba9 new \u629b\u51fa\u5f02\u5e38(\u6216\u8ba9\u7a0b\u5e8f\u5d29\u6e83)\u662f\u4e0d\u53ef\u53d6\u7684\uff0c\u56e0\u6b64\uff0c\u5982\u679c\u65e0\u6cd5\u5206\u914d\u5185\u5b58\uff0c\u53ef\u4ee5\u4f7f\u7528\u53e6\u4e00\u79cd\u5f62\u5f0f\u7684 new \u6765\u544a\u8bc9 new \u8fd4\u56de\u7a7a\u6307\u9488\u3002\u8fd9\u662f\u901a\u8fc7\u5728 new \u5173\u952e\u5b57\u548c\u5206\u914d\u7c7b\u578b\u4e4b\u95f4\u6dfb\u52a0\u5e38\u91cf std::nothrow \u6765\u5b9e\u73b0\u7684\uff1a 1 int * value { new ( std :: nothrow ) int }; // \u5982\u679c\u6ca1\u6709\u5206\u914d\u5230\u5185\u5b58\uff0c\u5219\u6307\u9488\u88ab\u7f6e\u4e3anull \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c new \u5206\u914d\u5185\u5b58\u5931\u8d25\uff0c\u5b83\u5c06\u8fd4\u56de\u4e00\u4e2a\u7a7a\u6307\u9488\u800c\u4e0d\u662f\u5df2\u5206\u914d\u5185\u5b58\u7684\u5730\u5740\u3002 \u6ce8\u610f\uff0c\u5982\u679c\u4f60\u5c1d\u8bd5\u901a\u8fc7\u6b64\u6307\u9488\u8fdb\u884c\u95f4\u63a5\u64cd\u4f5c\uff0c\u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a (\u6700\u6709\u53ef\u80fd\u7684\u662f\u7a0b\u5e8f\u5d29\u6e83)\u3002\u56e0\u6b64\uff0c\u6700\u4f73\u5b9e\u8df5\u662f\u5728\u4f7f\u7528\u5206\u914d\u7684\u5185\u5b58\u4e4b\u524d\u68c0\u67e5\u6240\u6709\u5185\u5b58\u8bf7\u6c42\uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u5b9e\u9645\u4e0a\u6210\u529f\u4e86\u3002 1 2 3 4 5 6 int * value { new ( std :: nothrow ) int {} }; // ask for an integer's worth of memory if ( ! value ) // handle case where new returned null { // Do error handling here std :: cerr << \"Could not allocate memory \\n \" ; } \u56e0\u4e3a\u901a\u8fc7 new \u7533\u8bf7\u5185\u5b58\u5f88\u5c11\u4f1a\u5931\u8d25(\u5728\u5f00\u53d1\u73af\u5883\u4e2d\u51e0\u4e4e\u4ece\u4e0d\u5931\u8d25)\uff0c\u6240\u4ee5\u7ecf\u5e38\u4f1a\u5fd8\u8bb0\u6267\u884c\u6b64\u68c0\u67e5\uff01 \u7a7a\u6307\u9488\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d \u7a7a\u6307\u9488 (\u8bbe\u7f6e\u4e3a nullptr \u7684\u6307\u9488) \u5728\u5904\u7406\u52a8\u6001\u5185\u5b58\u5206\u914d\u65f6\u5f88\u6709\u7528\u3002\u5728\u52a8\u6001\u5185\u5b58\u5206\u914d\u7684\u4e0a\u4e0b\u6587\u4e2d\uff0c\u7a7a\u6307\u9488\u4ee3\u8868\u7740\u201d\u6ca1\u6709\u5185\u5b58\u88ab\u5206\u914d\u7ed9\u8fd9\u4e2a\u6307\u9488\u201c\u3002\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u6765\u5206\u914d\u5185\u5b58\uff1a 1 2 3 // If ptr isn't already allocated, allocate it if ( ! ptr ) ptr = new int ; \u5220\u9664\u7a7a\u6307\u9488\u6ca1\u6548\u679c\u3002\u56e0\u6b64\u6ca1\u5fc5\u8981\u8fd9\u4e48\u505a\uff1a 1 2 if ( ptr ) delete ptr ; \u76f4\u63a5\u8fd9\u4e48\u505a\u5c31\u53ef\u4ee5\uff1a 1 delete ptr ; \u5982\u679c ptr \u662f\u975e\u7a7a\u7684\uff0c\u90a3\u4e48\u5b83\u6307\u5411\u7684\u5185\u5b58\u5c06\u88ab\u91ca\u653e\uff0c\u5982\u679c ptr \u662f\u7a7a\uff0c\u5219\u4ec0\u4e48\u90fd\u4e0d\u4f1a\u53d1\u751f\u3002 \u5185\u5b58\u6cc4\u6f0f \u9664\u975e\u663e\u5f0f\u5730\u91ca\u653e\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u6216\u7a0b\u5e8f\u7ed3\u675f(\u5e76\u4e14\u64cd\u4f5c\u7cfb\u7edf\u6e05\u7406\u5b83\uff0c\u5047\u8bbe\u60a8\u7684\u64cd\u4f5c\u7cfb\u7edf\u8fd9\u4e48\u505a)\uff0c\u5426\u5219\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u4e00\u76f4\u5904\u4e8e\u88ab\u5206\u914d\u72b6\u6001\u3002\u4f46\u662f\uff0c\u7528\u4e8e\u4fdd\u5b58\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u5730\u5740\u7684\u6307\u9488\u9075\u5faa\u672c\u5730\u53d8\u91cf\u7684\u6b63\u5e38\u4f5c\u7528\u57df\u89c4\u5219\u3002\u8fd9\u79cd\u4e0d\u5339\u914d\u4f1a\u4ea7\u751f\u6709\u8da3\u7684\u95ee\u9898\u3002 \u8003\u8651\u4e0b\u9762\u7684\u51fd\u6570\uff1a 1 2 3 4 void doSomething () { int * ptr { new int {} }; } \u8fd9\u4e2a\u51fd\u6570\u52a8\u6001\u5206\u914d\u4e86\u4e00\u4e2a\u6574\u6570\uff0c\u4f46\u5e76\u6ca1\u6709\u7528 delete \u91ca\u653e\u5b83\u3002\u56e0\u4e3a\u6307\u9488\u53d8\u91cf\u53ea\u662f\u666e\u901a\u53d8\u91cf\uff0c\u5f53\u51fd\u6570\u7ed3\u675f\u65f6\uff0c ptr \u79bb\u5f00\u4f5c\u7528\u57df \u3002\u7531\u4e8e ptr \u662f\u4fdd\u5b58\u52a8\u6001\u5206\u914d\u7684\u6574\u6570\u5730\u5740\u7684\u552f\u4e00\u53d8\u91cf\uff0c\u5f53 ptr \u88ab\u9500\u6bc1\u65f6\uff0c\u5bf9\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u5c31\u6ca1\u6709\u66f4\u591a\u7684\u5f15\u7528\u4e86\u3002\u8fd9\u610f\u5473\u7740\u7a0b\u5e8f\u73b0\u5728\u201c\u4e22\u5931\u201d\u4e86\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u7684\u5730\u5740\u3002\u56e0\u6b64\uff0c\u65e0\u6cd5\u518d\u5220\u9664\u8fd9\u4e2a\u52a8\u6001\u5206\u914d\u7684\u6574\u6570\u4e86\u3002 \u8fd9\u4e2a\u73b0\u8c61\u79f0\u4e3a \u5185\u5b58\u6cc4\u6f0f \u3002\u5f53\u7a0b\u5e8f\u5728\u5c06\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u8fd4\u56de\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u4e4b\u524d\u4e22\u5931\u4e86\u5b83\u7684\u5730\u5740\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u5185\u5b58\u6cc4\u6f0f\u3002\u5f53\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\u65f6\uff0c\u7a0b\u5e8f\u5c06\u65e0\u6cd5\u5220\u9664\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\uff0c\u56e0\u4e3a\u5b83\u4e0d\u518d\u77e5\u9053\u5185\u5b58\u5730\u5740\u4e86\u3002\u64cd\u4f5c\u7cfb\u7edf\u4e5f\u4e0d\u80fd\u4f7f\u7528\u8be5\u5185\u5b58\uff0c\u56e0\u4e3a\u8be5\u5185\u5b58\u88ab\u8ba4\u4e3a\u662f\u7a0b\u5e8f\u4ecd\u5728\u4f7f\u7528\u4e2d\u3002 \u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u5185\u5b58\u6cc4\u6f0f\u4f1a\u6d88\u8017\u6389\u53ef\u7528\u7684\u5185\u5b58\uff0c\u4e0d\u4ec5\u4f7f\u8fd9\u4e2a\u7a0b\u5e8f\u53ef\u7528\u7684\u5185\u5b58\u51cf\u5c11\uff0c\u800c\u4e14\u4f7f\u5176\u4ed6\u7a0b\u5e8f\u53ef\u7528\u7684\u5185\u5b58\u51cf\u5c11\u3002\u6709\u4e25\u91cd\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\u7684\u7a0b\u5e8f\u4f1a\u8017\u5c3d\u6240\u6709\u53ef\u7528\u5185\u5b58\uff0c\u5bfc\u81f4\u6574\u4e2a\u673a\u5668\u8fd0\u884c\u7f13\u6162\u751a\u81f3\u5d29\u6e83\u3002\u53ea\u6709\u5728\u7a0b\u5e8f\u7ec8\u6b62\u540e\uff0c\u64cd\u4f5c\u7cfb\u7edf\u624d\u80fd\u6e05\u7406\u548c\u201c\u56de\u6536\u201d\u6240\u6709\u6cc4\u6f0f\u7684\u5185\u5b58\u3002 \u5c3d\u7ba1\u6307\u9488\u8d85\u51fa\u4f5c\u7528\u57df\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\uff0c\u4f46\u8fd8\u6709\u5176\u4ed6\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u7684\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6301\u6709\u52a8\u6001\u5206\u914d\u5185\u5b58\u5730\u5740\u7684\u6307\u9488\u88ab\u5206\u914d\u4e86\u53e6\u4e00\u4e2a\u503c\uff0c\u4e5f\u4f1a\u53d1\u751f\u5185\u5b58\u6cc4\u6f0f\uff1a 1 2 3 int value = 5 ; int * ptr { new int {} }; // allocate memory ptr = & value ; // old address lost, memory leak results \u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728\u91cd\u65b0\u8d4b\u503c\u4e4b\u524d\u5220\u9664\u6307\u9488\u6765\u89e3\u51b3\uff1a 1 2 3 4 int value { 5 }; int * ptr { new int {} }; // allocate memory delete ptr ; // return memory back to operating system ptr = & value ; // reassign pointer to address of value \u7c7b\u4f3c\u5730\uff0c\u91cd\u590d\u5206\u914d\u5185\u5b58\u4e5f\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\uff1a 1 2 int * ptr { new int {} }; ptr = new int {}; // old address lost, memory leak results \u4ece\u7b2c\u4e8c\u6b21\u5206\u914d\u5185\u5b58\u65f6\u8fd4\u56de\u7684\u5730\u5740\u5c06\u8986\u76d6\u7b2c\u4e00\u4e2a\u5206\u914d\u7684\u5730\u5740\u3002\u56e0\u6b64\uff0c\u7b2c\u4e00\u5757\u5185\u5b58\u6cc4\u6f0f\u4e86\uff01 \u7c7b\u4f3c\u5730\uff0c\u53ef\u4ee5\u901a\u8fc7\u786e\u4fdd\u5728\u91cd\u65b0\u8d4b\u503c\u4e4b\u524d\u5220\u9664\u6307\u9488\u6765\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002 \u7ed3\u8bba \u64cd\u4f5c\u7b26 new \u548c delete \u5141\u8bb8\u6211\u4eec\u4e3a\u7a0b\u5e8f\u52a8\u6001\u5206\u914d\u5355\u4e2a\u53d8\u91cf\u3002 \u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u5177\u6709 \u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u5e76\u4e14\u5728\u91ca\u653e\u5b83\u6216\u7a0b\u5e8f\u7ec8\u6b62\u4e4b\u524d\u5c06\u4fdd\u6301\u5206\u914d\u72b6\u6001\u3002 \u6ce8\u610f\u4e0d\u8981\u901a\u8fc7\u60ac\u5782\u6216\u7a7a\u6307\u9488\u6267\u884c\u95f4\u63a5\u8bbf\u95ee\u64cd\u4f5c\u3002 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u770b\u770b\u5982\u4f55\u4f7f\u7528 new \u548c delete \u6765\u5206\u914d\u548c\u5220\u9664\u6570\u7ec4\u3002","title":"11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/#_1","text":"C++ \u652f\u6301\u4e09\u79cd\u57fa\u672c\u7c7b\u578b\u7684\u5185\u5b58\u5206\u914d \uff0c\u4f60\u5e94\u8be5\u5df2\u7ecf\u89c1\u8fc7\u5176\u4e2d\u7684\u4e24\u79cd\u4e86\uff1a \u9759\u6001\u5185\u5b58\u5206\u914d \u53d1\u751f\u5728\u9759\u6001\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u521b\u5efa\u65f6\u3002\u5f53\u7a0b\u5e8f\u542f\u52a8\u65f6\uff0c\u6b64\u7c7b\u53d8\u91cf\u7684\u5185\u5b58\u4f1a\u88ab\u5206\u914d\u5e76\u6301\u7eed\u5230\u7a0b\u5e8f\u7ed3\u675f\uff0c\u8d2f\u7a7f\u7a0b\u5e8f\u7684\u6574\u4e2a\u751f\u547d\u5468\u671f\uff1b \u81ea\u52a8\u5185\u5b58\u5206\u914d \u53d1\u751f\u5728\u51fd\u6570\u5f62\u53c2\u548c\u5c40\u90e8\u53d8\u91cf\u521b\u5efa\u65f6\u3002\u8fd9\u4e9b\u53d8\u91cf\u7684\u5185\u5b58\u4f1a\u5728\u8fdb\u5165\u76f8\u5e94\u7684\u8bed\u53e5\u5757\u65f6\u88ab\u81ea\u52a8\u5206\u914d\uff0c\u800c\u5f53\u8bed\u53e5\u5757\u9000\u51fa\u65f6\u5219\u4f1a\u81ea\u52a8\u91ca\u653e\uff1b \u52a8\u6001\u5185\u5b58\u5206\u914d \u5219\u662f\u672c\u8282\u8bfe\u8981\u4ecb\u7ecd\u7684\u3002 \u9759\u6001\u5185\u5b58\u5206\u914d\u548c\u81ea\u52a8\u5185\u5b58\u5206\u914d\u6709\u4e24\u4e2a\u5171\u540c\u70b9\uff1a \u53d8\u91cf\u548c\u6570\u7ec4\u7684\u5927\u5c0f\u5fc5\u987b\u5728 \u7f16\u8bd1\u65f6 \u5c31\u5df2\u77e5\uff1b \u5185\u5b58\u7684\u5206\u914d\u548c\u91ca\u653e\u662f\u81ea\u52a8\u8fdb\u884c\u7684\uff08\u5728\u53d8\u91cf\u5b9e\u4f8b\u5316\u548c\u9500\u6bc1\u65f6\uff09\u3002 \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u662f\u5df2\u7ecf\u8db3\u591f\u4e86\u3002\u4f46\u662f\uff0c\u6709\u65f6\u5019\u8fd9\u4e24\u4e2a\u7279\u70b9\u4f1a\u5e26\u6765\u9650\u5236\uff0c\u901a\u5e38\u662f\u5728\u5904\u7406\u5916\u90e8(\u7528\u6237\u6216\u6587\u4ef6)\u8f93\u5165\u65f6\u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u80fd\u60f3\u4f7f\u7528\u4e00\u4e2a\u5b57\u7b26\u4e32\u6765\u4fdd\u5b58\u67d0\u4eba\u7684\u540d\u5b57\uff0c\u4f46\u5728\u7528\u6237\u8f93\u5165\u540d\u5b57\u4e4b\u524d\uff0c\u6211\u4eec\u5e76\u4e0d\u77e5\u9053\u540d\u5b57\u6709\u591a\u957f\u3002\u6216\u8005\u6211\u4eec\u53ef\u80fd\u60f3\u4ece\u78c1\u76d8\u8bfb\u5165\u4e00\u4e9b\u8bb0\u5f55\uff0c\u4f46\u4e8b\u5148\u4e0d\u77e5\u9053\u6709\u591a\u5c11\u8bb0\u5f55\u3002\u6216\u8005\u6211\u4eec\u6b63\u5728\u521b\u9020\u4e00\u6b3e\u602a\u7269\u6570\u91cf\u52a8\u6001\u53d8\u5316\u7684\u6e38\u620f(\u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\uff0c\u4e00\u4e9b\u602a\u7269\u4f1a\u6b7b\u4ea1\uff0c\u65b0\u602a\u7269\u4f1a\u51fa\u73b0)\u3002 \u5982\u679c\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u5c31\u58f0\u660e\u53d8\u91cf\u6216\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u90a3\u6211\u4eec\u80fd\u505a\u7684\u5c31\u662f\u5c1d\u8bd5\u731c\u6d4b\u9700\u8981\u7684\u53d8\u91cf\u7684\u5927\u5c0f\u7684\u6700\u5927\u503c\u5e76\u671f\u671b\u5b9e\u9645\u4f7f\u7528\u4e2d\u8be5\u5185\u5b58\u662f\u8db3\u591f\u7684\uff1a 1 2 3 4 char name [ 25 ]; // \u671f\u671b\u7528\u6237\u7684\u59d3\u540d\u4e0d\u8d85\u8fc725\u4e2a\u5b57\u7b26 Record record [ 500 ]; // \u5e0c\u671b\u6570\u636e\u7684\u8bb0\u5f55\u6570\u5c0f\u4e8e500\u6761 Monster monster [ 40 ]; // \u6700\u591a 40 \u4e2a\u602a\u7269 Polygon rendering [ 30000 ]; // 3d \u6e32\u67d3\u4e0d\u8d85\u8fc73\u4e07\u4e2a\u591a\u8fb9\u5f62 \u8fd9\u79cd\u89e3\u51b3\u65b9\u6848\u662f\u5f88\u5dee\u52b2\u7684\uff0c\u539f\u56e0\u6709\u56db\u6761\uff1a \u9996\u5148\uff0c\u5982\u679c\u5b9e\u9645\u6ca1\u6709\u4f7f\u7528\u8be5\u53d8\u91cf\uff0c\u5219\u4f1a\u5bfc\u81f4\u5185\u5b58\u6d6a\u8d39\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u4e3a\u6bcf\u4e2a\u540d\u79f0\u5206\u914d25\u4e2a\u5b57\u7b26\uff0c\u4f46\u540d\u79f0\u5e73\u5747\u53ea\u670912\u4e2a\u5b57\u7b26\u957f\uff0c\u90a3\u4e48\u7533\u8bf7\u7684\u5185\u5b58\u662f\u5b9e\u9645\u4f7f\u7528\u7684\u4e24\u500d\u591a\u3002\u6216\u8005\uff0c\u5bf9\u4e8e\u4e0a\u9762 rendering \u6570\u7ec4\u7684\u4f8b\u5b50\u6765\u8bf4\uff1a\u5982\u679c\u6e32\u67d3\u53ea\u4f7f\u752810000\u4e2a\u591a\u8fb9\u5f62\uff0c\u90a3\u4e48\u5c06\u670920000\u4e2a\u591a\u8fb9\u5f62\u7684\u5185\u5b58\u88ab\u989d\u5916\u7533\u8bf7\u5355\u6ca1\u6709\u88ab\u4f7f\u7528! \u7b2c\u4e8c\uff0c\u6211\u4eec\u5982\u4f55\u5224\u65ad\u54ea\u4e9b\u5185\u5b58\u4f4d\u5b9e\u9645\u4e0a\u88ab\u4f7f\u7528\u4e86\u5462\uff1f\u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0c\u8fd9\u5f88\u7b80\u5355\uff1a\u4ee5 \\0 \u5f00\u5934\u7684\u5b57\u7b26\u4e32\u663e\u7136\u6ca1\u6709\u88ab\u4f7f\u7528\u3002\u4f46\u662f\u5bf9\u4e8e monster[24] \u6765\u8bf4\u5462\uff1f\u5b83\u4ecd\u7136\u5728\u88ab\u4f7f\u7528\u5417\uff1f\u6211\u4eec\u9700\u8981\u67d0\u79cd\u65b9\u6cd5\u6765\u533a\u5206\u4ecd\u7136\u5728\u4f7f\u7528\u7684\u53d8\u91cf\u548c\u4e0d\u518d\u88ab\u4f7f\u7528\u7684\u53d8\u91cf\uff0c\u8fd9\u589e\u52a0\u4e86\u590d\u6742\u6027\uff0c\u5e76\u53ef\u80fd\u4f1a\u5360\u7528\u989d\u5916\u7684\u5185\u5b58\u3002 \u7b2c\u4e09\uff0c\u5927\u591a\u6570\u666e\u901a\u53d8\u91cf(\u5305\u62ec\u56fa\u5b9a\u6570\u7ec4)\u88ab\u5206\u914d\u5230\u79f0\u4e3a \u6808 \u7684\u5185\u5b58\u4e2d\u3002\u7a0b\u5e8f\u7684\u5806\u6808\u80fd\u591f\u4f7f\u7528\u7684\u5185\u5b58\u901a\u5e38\u975e\u5e38\u5c0f\u2014\u2014Visual Studio \u9ed8\u8ba4\u5806\u6808\u5927\u5c0f\u4e3a 1MB\u3002\u5982\u679c\u8d85\u8fc7\u8fd9\u4e2a\u5927\u5c0f\uff0c\u5c31\u4f1a\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5c31\u4f1a\u505c\u6b62\u7a0b\u5e8f\u3002 \u5728 Visual Studio \u4e2d\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\u65f6\u53d1\u751f\u7684\u60c5\u51b5\uff1a 1 2 3 4 int main () { int array [ 1000000 ]; // allocate 1 million integers (probably 4MB of memory) } \u5c06\u5185\u5b58\u9650\u5236\u4e3a1MB\u5bf9\u4e8e\u8bb8\u591a\u7a0b\u5e8f\u6765\u8bf4\u90fd\u662f\u6709\u95ee\u9898\u7684\uff0c\u7279\u522b\u662f\u90a3\u4e9b\u5904\u7406\u56fe\u5f62\u7684\u7a0b\u5e8f\u3002\u5728Visual Studio\u4e2d\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\u65f6\u53d1\u751f\u7684\u60c5\u51b5: \u7b2c\u56db\uff0c\u4e5f\u662f\u6700\u91cd\u8981\u7684\uff0c\u5b83\u4f1a\u5bfc\u81f4\u4eba\u4e3a\u7684\u9650\u5236\uff0c\u5e76\u53ef\u80fd\u5bfc\u81f4\u6ea2\u51fa\u3002\u5982\u679c\u7528\u6237\u8bd5\u56fe\u4ece\u78c1\u76d8\u8bfb\u5165600\u6761\u8bb0\u5f55\uff0c\u4f46\u6211\u4eec\u53ea\u5206\u914d\u4e86\u6700\u591a500\u6761\u8bb0\u5f55\u7684\u5185\u5b58\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u60c5\u51b5\uff1f\u6b64\u65f6\u8981\u4e48\u7ed9\u7528\u6237\u53d1\u9001\u4e00\u4e2a\u9519\u8bef\uff0c\u8981\u6c42\u5176\u53ea\u8bfb\u53d6500\u6761\u8bb0\u5f55\uff0c\u8981\u4e48(\u5728\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u6839\u672c\u4e0d\u5904\u7406\u8fd9\u79cd\u60c5\u51b5) record \u6570\u7ec4\u6ea2\u51fa\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u8fd9\u4e9b\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7\u52a8\u6001\u5185\u5b58\u5206\u914d\u8f7b\u677e\u89e3\u51b3\u3002 \u52a8\u6001\u5185\u5b58\u5206\u914d \u662f\u8fd0\u884c\u7a0b\u5e8f\u5728\u9700\u8981\u65f6\u5411\u64cd\u4f5c\u7cfb\u7edf\u8bf7\u6c42\u5185\u5b58\u7684\u4e00\u79cd\u65b9\u6cd5\u3002\u8fd9\u4e2a\u5185\u5b58\u4e0d\u662f\u6765\u81ea\u7a0b\u5e8f\u6709\u9650\u7684\u5806\u6808\u5185\u5b58\u2014\u2014\u76f8\u53cd\uff0c\u5b83\u662f\u4ece\u4e00\u4e2a\u66f4\u5927\u7684\u5185\u5b58\u6c60\u4e2d\u5206\u914d\u7684\uff0c\u7531\u64cd\u4f5c\u7cfb\u7edf\u7ba1\u7406\uff0c\u79f0\u4e3a \u5806 \u3002\u5728\u73b0\u4ee3\u673a\u5668\u4e0a\uff0c\u5806\u7684\u5927\u5c0f\u53ef\u4ee5\u8fbe\u5230\u5343\u5146\u5b57\u8282\u3002","title":"\u52a8\u6001\u5185\u5b58\u5206\u914d\u7684\u5fc5\u8981\u6027"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/#_2","text":"\u4e3a \u4e00\u4e2a \u53d8\u91cf\u5206\u914d\u5185\u5b58\uff0c\u6211\u4eec\u4f7f\u7528 new \u7684\u6807\u91cf\u5f62\u5f0f\uff1a 1 new int ; // dynamically allocate an integer (and discard the result) \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5411\u64cd\u4f5c\u7cfb\u7edf\u7533\u8bf7\u4e00\u4e2a\u6574\u578b\u7684\u5185\u5b58\u3002 new \u8fd0\u7b97\u7b26\u4f1a\u4f7f\u7528\u7533\u8bf7\u6765\u7684\u5185\u5b58\u521b\u5efa\u8fd9\u4e2a\u5bf9\u8c61\uff0c\u7136\u540e\u8fd4\u56de\u6307\u5411\u8be5\u5185\u5b58\u7684\u6307\u9488\u3002 \u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8fd4\u56de\u503c\u8d4b\u503c\u7ed9\u4e00\u4e2a\u6307\u9488\u53d8\u91cf\uff0c\u5e76\u901a\u8fc7\u5b83\u5728\u540e\u7eed\u7684\u7a0b\u5e8f\u4e2d\u8bbf\u95ee\u8fd9\u5757\u5185\u5b58\u3002 1 int * ptr { new int }; // dynamically allocate an integer and assign the address to ptr so we can access it later \u5728\u540e\u7eed\u7684\u7a0b\u5e8f\u4e2d\u901a\u8fc7 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u8bbf\u95ee\u8be5\u5185\u5b58\uff1a 1 * ptr = 7 ; // assign value of 7 to allocated memory \u5982\u679c\u4f60\u4e4b\u524d\u4e0d\u77e5\u9053\u6307\u9488\u6709\u4ec0\u4e48\u7528\uff0c\u90a3\u4e48\u73b0\u5728\u5e94\u8be5\u6e05\u695a\u4e86\uff0c\u81f3\u5c11\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u6ca1\u6709\u4e00\u4e2a\u6307\u9488\u6765\u4fdd\u5b58\u521a\u521a\u5206\u914d\u7684\u5185\u5b58\u5730\u5740\uff0c\u6211\u4eec\u5c31\u6ca1\u6709\u529e\u6cd5\u8bbf\u95ee\u521a\u521a\u5206\u914d\u7ed9\u6211\u4eec\u7684\u5185\u5b58!","title":"\u5355\u4e00\u53d8\u91cf\u7684\u52a8\u6001\u5185\u5b58\u5206\u914d"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/#_3","text":"\u8ba1\u7b97\u673a\u4e2d\u7684\u5185\u5b58\u53ef\u4f9b\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u3002\u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u5c06\u7a0b\u5e8f\u52a0\u8f7d\u5230\u4e00\u5757\u5185\u5b58\u4e2d\u3002\u8fd9\u5757\u5185\u5b58\u88ab\u4f60\u7684\u7a0b\u5e8f\u5206\u5272\u6210\u51e0\u4e2a\u4e0d\u540c\u7684\u90e8\u5206\uff0c\u6bcf\u4e2a\u90e8\u5206\u4e5f\u90fd\u6709\u5176\u7279\u5b9a\u7684\u4f5c\u7528\u3002\u5176\u4e2d\u4e00\u5c0f\u90e8\u5206\u5305\u542b\u4f60\u7684\u7a0b\u5e8f\u4ee3\u7801\uff0c\u53e6\u5916\u4e00\u5c0f\u90e8\u5206\u5219\u7528\u4e8e\u7a0b\u5e8f\u6b63\u5e38\u8fd0\u884c\u65f6\u4f7f\u7528\uff08\u8bb0\u5f55\u51fd\u6570\u8c03\u7528\u3001\u521b\u5efa\u548c\u9500\u6bc1\u5168\u5c40\u53d8\u91cf\u548c\u4e34\u65f6\u53d8\u91cf\uff0c\u7b49\u7b49\uff09\u3002\u6211\u4eec\u4f1a\u5728\u540e\u9762\u8fdb\u884c\u66f4\u8be6\u7ec6\u4ecb\u7ecd\u3002\u4f46\u662f\uff0c\u8fd8\u6709\u5f88\u591a\u5f88\u591a\u7684\u5185\u5b58\u5c31\u53ea\u662f\u653e\u5728\u90a3\u91cc\uff0c\u7b49\u5f85\u4f60\u7684\u7a0b\u5e8f\u8bf7\u6c42\u4f7f\u7528\u3002 \u548c\u9759\u6001\u5185\u5b58\u3001\u81ea\u52a8\u5185\u5b58\u4e0d\u540c\u7684\u662f\uff0c\u7a0b\u5e8f\u81ea\u5df1\u9700\u8981\u8d1f\u8d23\u8bf7\u6c42\u548c\u91ca\u653e\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u3002","title":"\u52a8\u6001\u5185\u5b58\u5206\u914d\u7684\u539f\u7406\u662f\u4ec0\u4e48\uff1f"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/#_4","text":"\u5f53\u4f60\u52a8\u6001\u5206\u914d\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7 \u76f4\u63a5\u521d\u59cb\u5316 \u6216 \u7edf\u4e00\u521d\u59cb\u5316 \u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\uff1a 1 2 int * ptr1 { new int ( 5 ) }; // \u76f4\u63a5\u521d\u59cb\u5316 int * ptr2 { new int { 6 } }; // \u7edf\u4e00\u521d\u59cb\u5316","title":"\u52a8\u6001\u5206\u914d\u53d8\u91cf\u7684\u521d\u59cb\u5316"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/#_5","text":"\u5728\u4f7f\u7528\u5b8c\u52a8\u6001\u5206\u914d\u7684\u53d8\u91cf\u65f6\uff0c\u6211\u4eec\u9700\u8981\u663e\u5f0f\u5730\u544a\u8bc9C++\u91ca\u653e\u5185\u5b58\u4ee5\u4fbf\u91cd\u7528\u3002\u5bf9\u4e8e\u5355\u4e2a\u53d8\u91cf\uff0c\u8fd9\u662f\u901a\u8fc7\u6807\u91cf(\u975e\u6570\u7ec4)\u5f62\u5f0f\u7684 delete \u64cd\u4f5c\u7b26\u6765\u5b8c\u6210\u7684\uff1a 1 2 3 // assume ptr has previously been allocated with operator new delete ptr ; // return the memory pointed to by ptr to the operating system ptr = nullptr ; // set ptr to be a null pointer","title":"\u5220\u9664\u5355\u4e00\u53d8\u91cf"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/#_6","text":"delete \u8fd0\u7b97\u7b26\u5e76\u6ca1\u6709 \u5b9e\u9645\u4e0a \u5220\u9664\u4efb\u4f55\u4e1c\u897f\uff0c\u5b83\u53ea\u662f\u5c06\u6307\u9488\u6307\u5411\u7684\u5185\u5b58\u8fd8\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u7f62\u4e86\u3002\u64cd\u4f5c\u7cfb\u7edf\u968f\u5373\u4fbf\u53ef\u4ee5\u5c06\u8be5\u5185\u5b58\u5206\u914d\u7ed9\u5176\u4ed6\u5e94\u7528\u4f7f\u7528\uff08\u6216\u8005\u7ed9\u5f53\u524d\u5e94\u7528\u4f7f\u7528\u4e5f\u53ef\u4ee5\uff09\u3002 \u5c3d\u7ba1\u770b\u4e0a\u53bb\u6211\u4eec\u662f\u5728 delete \u4e00\u4e2a\u53d8\u91cf\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\uff01\u8be5\u6307\u9488\u53d8\u91cf\u4ecd\u7136\u5904\u4e8e\u8be5\u4f5c\u7528\u57df\uff0c\u800c\u4e14\u4e5f\u53ef\u4ee5\u88ab\u8d4b\u4e88\u65b0\u503c\u3002 \u6ce8\u610f\uff0c\u5220\u9664\u4e0d\u6307\u5411\u52a8\u6001\u5206\u914d\u5185\u5b58\u7684\u6307\u9488\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e0d\u597d\u7684\u4e8b\u60c5\u53d1\u751f\u3002","title":"\u5220\u9664\u5185\u5b58\u662f\u4ec0\u4e48\u610f\u601d\uff1f"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/#_7","text":"C++\u6ca1\u6709\u89c4\u5b9a\u88ab\u91ca\u653e\u7684\u5185\u5bb9\u5176\u5185\u5bb9\u5e94\u8be5\u662f\u4ec0\u4e48\uff0c\u4e5f\u6ca1\u6709\u89c4\u5b9a\u88ab\u5220\u9664\u7684\u6307\u9488\u7684\u503c\u5e94\u8be5\u662f\u4ec0\u4e48\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd4\u56de\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u7684\u5185\u5b58\u5c06\u5305\u542b\u5b83\u5728\u8fd4\u56de\u4e4b\u524d\u7684\u76f8\u540c\u503c\uff0c\u5e76\u4e14\u6307\u9488\u5c06\u6307\u5411\u73b0\u5728\u91ca\u653e\u7684\u5185\u5b58\u3002 \u6267\u884c\u5df2\u7ecf\u88ab\u91ca\u653e\u7684\u5185\u5b58\u7684\u6307\u9488\uff0c\u79f0\u4e3a \u60ac\u5782 \u6307\u9488\u3002\u95f4\u63a5\u8bbf\u95ee\u6216\u5220\u9664\u60ac\u5782\u6307\u9488\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int * ptr { new int }; // \u52a8\u6001\u5206\u914d\u4e00\u4e2a\u6574\u578b * ptr = 7 ; // \u5411\u8be5\u5185\u5b58\u8d4b\u4e00\u4e2a\u503c delete ptr ; // \u5185\u5b58\u8fd8\u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff0cptr\u73b0\u5728\u662f\u60ac\u5782\u6307\u9488 std :: cout << * ptr ; // \u95f4\u63a5\u8bbf\u95ee\u60ac\u5782\u6307\u9488\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a delete ptr ; // \u518d\u6b21\u91ca\u653e\u5df2\u7ecf\u88ab\u91ca\u653e\u7684\u5185\u5b58\u4e5f\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u4e4b\u524d\u5206\u914d\u7ed9\u5df2\u5206\u914d\u5185\u5b58\u7684\u503c7\u53ef\u80fd\u4ecd\u7136\u5b58\u5728\uff0c\u4f46\u8be5\u5185\u5b58\u5730\u5740\u7684\u503c\u53ef\u80fd\u5df2\u7ecf\u53d1\u751f\u4e86\u53d8\u5316\u3002\u5185\u5b58\u4e5f\u6709\u53ef\u80fd\u88ab\u5206\u914d\u7ed9\u53e6\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f(\u6216\u64cd\u4f5c\u7cfb\u7edf\u81ea\u5df1\u4f7f\u7528)\uff0c\u8bd5\u56fe\u8bbf\u95ee\u8be5\u5185\u5b58\u5c06\u5bfc\u81f4\u64cd\u4f5c\u7cfb\u7edf\u5173\u95ed\u7a0b\u5e8f\u3002 \u91ca\u653e\u5185\u5b58\u53ef\u80fd\u4f1a\u5bfc\u81f4\u591a\u4e2a\u60ac\u5782\u6307\u9488\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int * ptr { new int {} }; // \u52a8\u6001\u5206\u914d\u4e00\u4e2a\u6574\u578b int * otherPtr { ptr }; // otherPtr \u6307\u5411\u76f8\u540c\u7684\u5185\u5b58 delete ptr ; // \u5185\u5b58\u8fd8\u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff0cptr,otherPtr \u73b0\u5728\u662f\u60ac\u5782\u6307\u9488 ptr = nullptr ; // ptr \u73b0\u5728\u662fnullptr // \u4f46\u662f, otherPtr \u4ecd\u7136\u662f\u60ac\u5782\u6307\u9488 return 0 ; } \u901a\u8fc7\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u907f\u514d\u4e0a\u8ff0\u95ee\u9898\u3002 \u9996\u5148\uff0c\u5c3d\u91cf\u907f\u514d\u6709\u591a\u4e2a\u6307\u9488\u6307\u5411\u540c\u4e00\u5757\u52a8\u6001\u5185\u5b58\u3002\u5982\u679c\u8fd9\u662f\u4e0d\u53ef\u80fd\u7684\uff0c\u8981\u6e05\u695a\u54ea\u4e9b\u6307\u9488\u201c\u62e5\u6709\u201d\u5185\u5b58(\u5e76\u8d1f\u8d23\u5220\u9664\u5b83)\uff0c\u54ea\u4e9b\u6307\u9488\u53ea\u662f\u5728\u8bbf\u95ee\u5b83\u3002 \u5176\u6b21\uff0c\u5728\u5220\u9664\u6307\u9488\u65f6\uff0c\u5982\u679c\u8be5\u6307\u9488\u4e0d\u4f1a\u7acb\u5373\u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u5219\u5c06\u8be5\u6307\u9488\u8bbe\u7f6e\u4e3anullptr\u3002\u6211\u4eec\u5c06\u66f4\u591a\u5730\u8ba8\u8bba\u7a7a\u6307\u9488\uff0c\u4ee5\u53ca\u5b83\u4eec\u4e3a\u4ec0\u4e48\u6709\u7528\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c06\u5df2\u5220\u9664\u7684\u6307\u9488\u8bbe\u7f6e\u4e3anullptr\uff0c\u9664\u975e\u5b83\u4eec\u968f\u540e\u5c06\u7acb\u5373\u8d85\u51fa\u4f5c\u7528\u57df\u3002","title":"\u60ac\u5782\u6307\u9488"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/#new","text":"\u5f53\u5411\u64cd\u4f5c\u7cfb\u7edf\u8bf7\u6c42\u5185\u5b58\u65f6\uff0c\u5728\u6781\u5c11\u6570\u60c5\u51b5\u4e0b\uff0c\u64cd\u4f5c\u7cfb\u7edf\u53ef\u80fd\u5df2\u7ecf\u6ca1\u6709\u4efb\u4f55\u53ef\u7528\u7684\u5185\u5b58\u3002 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5982\u679c new \u5931\u8d25\uff0c\u5219\u629b\u51fa bad_alloc \u5f02\u5e38\u3002\u5982\u679c\u8fd9\u4e2a\u5f02\u5e38\u6ca1\u6709\u5f97\u5230\u6b63\u786e\u5904\u7406(\u5b83\u4e0d\u4f1a\u5f97\u5230\u6b63\u786e\u5904\u7406\uff0c\u56e0\u4e3a\u6211\u4eec\u8fd8\u6ca1\u6709\u8ba8\u8bba\u5f02\u5e38\u6216\u5f02\u5e38\u5904\u7406)\uff0c\u7a0b\u5e8f\u5c06\u7ec8\u6b62(\u5d29\u6e83)\uff0c\u5e76\u51fa\u73b0\u4e00\u4e2a\u672a\u5904\u7406\u7684\u5f02\u5e38\u9519\u8bef\u3002 \u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u8ba9 new \u629b\u51fa\u5f02\u5e38(\u6216\u8ba9\u7a0b\u5e8f\u5d29\u6e83)\u662f\u4e0d\u53ef\u53d6\u7684\uff0c\u56e0\u6b64\uff0c\u5982\u679c\u65e0\u6cd5\u5206\u914d\u5185\u5b58\uff0c\u53ef\u4ee5\u4f7f\u7528\u53e6\u4e00\u79cd\u5f62\u5f0f\u7684 new \u6765\u544a\u8bc9 new \u8fd4\u56de\u7a7a\u6307\u9488\u3002\u8fd9\u662f\u901a\u8fc7\u5728 new \u5173\u952e\u5b57\u548c\u5206\u914d\u7c7b\u578b\u4e4b\u95f4\u6dfb\u52a0\u5e38\u91cf std::nothrow \u6765\u5b9e\u73b0\u7684\uff1a 1 int * value { new ( std :: nothrow ) int }; // \u5982\u679c\u6ca1\u6709\u5206\u914d\u5230\u5185\u5b58\uff0c\u5219\u6307\u9488\u88ab\u7f6e\u4e3anull \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c new \u5206\u914d\u5185\u5b58\u5931\u8d25\uff0c\u5b83\u5c06\u8fd4\u56de\u4e00\u4e2a\u7a7a\u6307\u9488\u800c\u4e0d\u662f\u5df2\u5206\u914d\u5185\u5b58\u7684\u5730\u5740\u3002 \u6ce8\u610f\uff0c\u5982\u679c\u4f60\u5c1d\u8bd5\u901a\u8fc7\u6b64\u6307\u9488\u8fdb\u884c\u95f4\u63a5\u64cd\u4f5c\uff0c\u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a (\u6700\u6709\u53ef\u80fd\u7684\u662f\u7a0b\u5e8f\u5d29\u6e83)\u3002\u56e0\u6b64\uff0c\u6700\u4f73\u5b9e\u8df5\u662f\u5728\u4f7f\u7528\u5206\u914d\u7684\u5185\u5b58\u4e4b\u524d\u68c0\u67e5\u6240\u6709\u5185\u5b58\u8bf7\u6c42\uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u5b9e\u9645\u4e0a\u6210\u529f\u4e86\u3002 1 2 3 4 5 6 int * value { new ( std :: nothrow ) int {} }; // ask for an integer's worth of memory if ( ! value ) // handle case where new returned null { // Do error handling here std :: cerr << \"Could not allocate memory \\n \" ; } \u56e0\u4e3a\u901a\u8fc7 new \u7533\u8bf7\u5185\u5b58\u5f88\u5c11\u4f1a\u5931\u8d25(\u5728\u5f00\u53d1\u73af\u5883\u4e2d\u51e0\u4e4e\u4ece\u4e0d\u5931\u8d25)\uff0c\u6240\u4ee5\u7ecf\u5e38\u4f1a\u5fd8\u8bb0\u6267\u884c\u6b64\u68c0\u67e5\uff01","title":"new \u8fd0\u7b97\u7b26\u53ef\u80fd\u6267\u884c\u5931\u8d25"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/#_8","text":"\u7a7a\u6307\u9488 (\u8bbe\u7f6e\u4e3a nullptr \u7684\u6307\u9488) \u5728\u5904\u7406\u52a8\u6001\u5185\u5b58\u5206\u914d\u65f6\u5f88\u6709\u7528\u3002\u5728\u52a8\u6001\u5185\u5b58\u5206\u914d\u7684\u4e0a\u4e0b\u6587\u4e2d\uff0c\u7a7a\u6307\u9488\u4ee3\u8868\u7740\u201d\u6ca1\u6709\u5185\u5b58\u88ab\u5206\u914d\u7ed9\u8fd9\u4e2a\u6307\u9488\u201c\u3002\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u6765\u5206\u914d\u5185\u5b58\uff1a 1 2 3 // If ptr isn't already allocated, allocate it if ( ! ptr ) ptr = new int ; \u5220\u9664\u7a7a\u6307\u9488\u6ca1\u6548\u679c\u3002\u56e0\u6b64\u6ca1\u5fc5\u8981\u8fd9\u4e48\u505a\uff1a 1 2 if ( ptr ) delete ptr ; \u76f4\u63a5\u8fd9\u4e48\u505a\u5c31\u53ef\u4ee5\uff1a 1 delete ptr ; \u5982\u679c ptr \u662f\u975e\u7a7a\u7684\uff0c\u90a3\u4e48\u5b83\u6307\u5411\u7684\u5185\u5b58\u5c06\u88ab\u91ca\u653e\uff0c\u5982\u679c ptr \u662f\u7a7a\uff0c\u5219\u4ec0\u4e48\u90fd\u4e0d\u4f1a\u53d1\u751f\u3002","title":"\u7a7a\u6307\u9488\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/#_9","text":"\u9664\u975e\u663e\u5f0f\u5730\u91ca\u653e\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u6216\u7a0b\u5e8f\u7ed3\u675f(\u5e76\u4e14\u64cd\u4f5c\u7cfb\u7edf\u6e05\u7406\u5b83\uff0c\u5047\u8bbe\u60a8\u7684\u64cd\u4f5c\u7cfb\u7edf\u8fd9\u4e48\u505a)\uff0c\u5426\u5219\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u4e00\u76f4\u5904\u4e8e\u88ab\u5206\u914d\u72b6\u6001\u3002\u4f46\u662f\uff0c\u7528\u4e8e\u4fdd\u5b58\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u5730\u5740\u7684\u6307\u9488\u9075\u5faa\u672c\u5730\u53d8\u91cf\u7684\u6b63\u5e38\u4f5c\u7528\u57df\u89c4\u5219\u3002\u8fd9\u79cd\u4e0d\u5339\u914d\u4f1a\u4ea7\u751f\u6709\u8da3\u7684\u95ee\u9898\u3002 \u8003\u8651\u4e0b\u9762\u7684\u51fd\u6570\uff1a 1 2 3 4 void doSomething () { int * ptr { new int {} }; } \u8fd9\u4e2a\u51fd\u6570\u52a8\u6001\u5206\u914d\u4e86\u4e00\u4e2a\u6574\u6570\uff0c\u4f46\u5e76\u6ca1\u6709\u7528 delete \u91ca\u653e\u5b83\u3002\u56e0\u4e3a\u6307\u9488\u53d8\u91cf\u53ea\u662f\u666e\u901a\u53d8\u91cf\uff0c\u5f53\u51fd\u6570\u7ed3\u675f\u65f6\uff0c ptr \u79bb\u5f00\u4f5c\u7528\u57df \u3002\u7531\u4e8e ptr \u662f\u4fdd\u5b58\u52a8\u6001\u5206\u914d\u7684\u6574\u6570\u5730\u5740\u7684\u552f\u4e00\u53d8\u91cf\uff0c\u5f53 ptr \u88ab\u9500\u6bc1\u65f6\uff0c\u5bf9\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u5c31\u6ca1\u6709\u66f4\u591a\u7684\u5f15\u7528\u4e86\u3002\u8fd9\u610f\u5473\u7740\u7a0b\u5e8f\u73b0\u5728\u201c\u4e22\u5931\u201d\u4e86\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u7684\u5730\u5740\u3002\u56e0\u6b64\uff0c\u65e0\u6cd5\u518d\u5220\u9664\u8fd9\u4e2a\u52a8\u6001\u5206\u914d\u7684\u6574\u6570\u4e86\u3002 \u8fd9\u4e2a\u73b0\u8c61\u79f0\u4e3a \u5185\u5b58\u6cc4\u6f0f \u3002\u5f53\u7a0b\u5e8f\u5728\u5c06\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u8fd4\u56de\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u4e4b\u524d\u4e22\u5931\u4e86\u5b83\u7684\u5730\u5740\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u5185\u5b58\u6cc4\u6f0f\u3002\u5f53\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\u65f6\uff0c\u7a0b\u5e8f\u5c06\u65e0\u6cd5\u5220\u9664\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\uff0c\u56e0\u4e3a\u5b83\u4e0d\u518d\u77e5\u9053\u5185\u5b58\u5730\u5740\u4e86\u3002\u64cd\u4f5c\u7cfb\u7edf\u4e5f\u4e0d\u80fd\u4f7f\u7528\u8be5\u5185\u5b58\uff0c\u56e0\u4e3a\u8be5\u5185\u5b58\u88ab\u8ba4\u4e3a\u662f\u7a0b\u5e8f\u4ecd\u5728\u4f7f\u7528\u4e2d\u3002 \u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u5185\u5b58\u6cc4\u6f0f\u4f1a\u6d88\u8017\u6389\u53ef\u7528\u7684\u5185\u5b58\uff0c\u4e0d\u4ec5\u4f7f\u8fd9\u4e2a\u7a0b\u5e8f\u53ef\u7528\u7684\u5185\u5b58\u51cf\u5c11\uff0c\u800c\u4e14\u4f7f\u5176\u4ed6\u7a0b\u5e8f\u53ef\u7528\u7684\u5185\u5b58\u51cf\u5c11\u3002\u6709\u4e25\u91cd\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\u7684\u7a0b\u5e8f\u4f1a\u8017\u5c3d\u6240\u6709\u53ef\u7528\u5185\u5b58\uff0c\u5bfc\u81f4\u6574\u4e2a\u673a\u5668\u8fd0\u884c\u7f13\u6162\u751a\u81f3\u5d29\u6e83\u3002\u53ea\u6709\u5728\u7a0b\u5e8f\u7ec8\u6b62\u540e\uff0c\u64cd\u4f5c\u7cfb\u7edf\u624d\u80fd\u6e05\u7406\u548c\u201c\u56de\u6536\u201d\u6240\u6709\u6cc4\u6f0f\u7684\u5185\u5b58\u3002 \u5c3d\u7ba1\u6307\u9488\u8d85\u51fa\u4f5c\u7528\u57df\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\uff0c\u4f46\u8fd8\u6709\u5176\u4ed6\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u7684\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6301\u6709\u52a8\u6001\u5206\u914d\u5185\u5b58\u5730\u5740\u7684\u6307\u9488\u88ab\u5206\u914d\u4e86\u53e6\u4e00\u4e2a\u503c\uff0c\u4e5f\u4f1a\u53d1\u751f\u5185\u5b58\u6cc4\u6f0f\uff1a 1 2 3 int value = 5 ; int * ptr { new int {} }; // allocate memory ptr = & value ; // old address lost, memory leak results \u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728\u91cd\u65b0\u8d4b\u503c\u4e4b\u524d\u5220\u9664\u6307\u9488\u6765\u89e3\u51b3\uff1a 1 2 3 4 int value { 5 }; int * ptr { new int {} }; // allocate memory delete ptr ; // return memory back to operating system ptr = & value ; // reassign pointer to address of value \u7c7b\u4f3c\u5730\uff0c\u91cd\u590d\u5206\u914d\u5185\u5b58\u4e5f\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\uff1a 1 2 int * ptr { new int {} }; ptr = new int {}; // old address lost, memory leak results \u4ece\u7b2c\u4e8c\u6b21\u5206\u914d\u5185\u5b58\u65f6\u8fd4\u56de\u7684\u5730\u5740\u5c06\u8986\u76d6\u7b2c\u4e00\u4e2a\u5206\u914d\u7684\u5730\u5740\u3002\u56e0\u6b64\uff0c\u7b2c\u4e00\u5757\u5185\u5b58\u6cc4\u6f0f\u4e86\uff01 \u7c7b\u4f3c\u5730\uff0c\u53ef\u4ee5\u901a\u8fc7\u786e\u4fdd\u5728\u91cd\u65b0\u8d4b\u503c\u4e4b\u524d\u5220\u9664\u6307\u9488\u6765\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002","title":"\u5185\u5b58\u6cc4\u6f0f"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/#_10","text":"\u64cd\u4f5c\u7b26 new \u548c delete \u5141\u8bb8\u6211\u4eec\u4e3a\u7a0b\u5e8f\u52a8\u6001\u5206\u914d\u5355\u4e2a\u53d8\u91cf\u3002 \u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u5177\u6709 \u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u5e76\u4e14\u5728\u91ca\u653e\u5b83\u6216\u7a0b\u5e8f\u7ec8\u6b62\u4e4b\u524d\u5c06\u4fdd\u6301\u5206\u914d\u72b6\u6001\u3002 \u6ce8\u610f\u4e0d\u8981\u901a\u8fc7\u60ac\u5782\u6216\u7a7a\u6307\u9488\u6267\u884c\u95f4\u63a5\u8bbf\u95ee\u64cd\u4f5c\u3002 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u770b\u770b\u5982\u4f55\u4f7f\u7528 new \u548c delete \u6765\u5206\u914d\u548c\u5220\u9664\u6570\u7ec4\u3002","title":"\u7ed3\u8bba"},{"location":"11-12-Dynamically-allocating-arrays/","tags":["arrays"],"text":"Key Takeaway \u4f7f\u7528 new[] \u521b\u5efa\u52a8\u6001\u6570\u7ec4\uff0c\u5176\u957f\u5ea6\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6307\u5b9a\uff0c\u4e0d\u9700\u8981\u662f\u5e38\u6570\uff0c\u4f46\u5176\u7c7b\u578b\u5fc5\u987b\u662f\u80fd\u591f\u88ab\u8f6c\u6362\u4e3a std::size_t \u7684\u7c7b\u578b \u4f7f\u7528 new int[legth]{} \u5206\u914d\u5e76\u521d\u59cb\u5316\u6570\u7ec4\u4e3a\u51680\u3002\u4eceC++11\u5f00\u59cb\uff0c \u521d\u59cb\u5316\u503c\u5217\u8868 \u53ef\u4ee5\u7528\u4e8e\u52a8\u6001\u6570\u7ec4\uff1a new int[5]{ 9, 7, 5, 3, 1 } \u5220\u9664\u52a8\u6001\u6570\u7ec4\u7684\u5185\u5b58\u9700\u8981\u4f7f\u7528 delete [] \uff0c\u65e0\u9700\u6307\u5b9a\u5927\u5c0f \u9664\u4e86\u4e3a\u5355\u4e2a\u53d8\u91cf\u52a8\u6001\u5206\u914d\u5185\u5b58\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4e3a\u6570\u7ec4\u52a8\u6001\u5206\u914d\u5185\u5b58\u3002\u56fa\u5b9a\u6570\u7ec4\u7684\u5927\u5c0f\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u5df2\u77e5\uff0c\u800c\u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4\u5219\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u786e\u5b9a\u957f\u5ea6\u3002 \u4e3a\u4e86\u5206\u914d\u52a8\u6001\u6570\u7ec4\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528 new \u548c delete \u7684\u6570\u7ec4\u5f62\u5f0f( new[] \u548c delete[] )\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int main () { std :: cout << \"Enter a positive integer: \" ; int length {}; std :: cin >> length ; int * array { new int [ length ]{} }; // \u4f7f\u7528\u6570\u7ec4 new\uff0c\u6ce8\u610f\u8fd9\u91cc\u7684length\u4e0d\u9700\u8981\u662f\u5e38\u6570\uff01 std :: cout << \"I just allocated an array of integers of length \" << length << '\\n' ; array [ 0 ] = 5 ; // \u5143\u7d20 0 \u8d4b\u503c\u4e3a 5 delete [] array ; // \u4f7f\u7528\u6570\u7ec4 delete \u91ca\u653e\u5185\u5b58 // \u6b64\u5904\u4e0d\u9700\u8981\u5c06 array \u8bbe\u7f6e\u4e3a nullptr/0 \u56e0\u4e3a\u5b83\u9a6c\u4e0a\u5c31\u4f1a\u79bb\u5f00\u4f5c\u7528\u57df\u88ab\u9500\u6bc1 return 0 ; } \u56e0\u4e3a\u6211\u4eec\u5206\u914d\u7684\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u6240\u4ee5C++\u6307\u5b9a\u5e94\u8be5\u4f7f\u7528\u6570\u7ec4\u7248\u672c\u7684 new \u800c\u4e0d\u662f\u6807\u91cf\u7248\u672c\u7684 new \u3002\u5b9e\u9645\u4e0a\uff0c\u5373\u4f7f [] \u6ca1\u6709\u653e\u5728 new \u540e\u9762\uff0c\u8c03\u7528\u7684\u4e5f\u662f new [] \u3002 \u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4\u957f\u5ea6\uff0c\u5176\u7c7b\u578b\u5fc5\u987b\u662f\u80fd\u591f\u88ab\u8f6c\u6362\u4e3a std::size_t \u7684\u7c7b\u578b \u3002\u5728\u5b9e\u8df5\u4e2d\uff0c\u4f7f\u7528 int \u4f5c\u4e3a\u957f\u5ea6\u662f\u53ef\u4ee5\u7684\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a std::size_t \u3002 \u4f5c\u8005\u6ce8 \u6709\u4eba\u4f1a\u8bf4\uff0c\u56e0\u4e3a\u6570\u7ec4\u7248\u672c\u7684 new \u9700\u8981 size_t \u7c7b\u578b\u7684\u957f\u5ea6\u503c\uff0c\u6240\u4ee5\u6211\u4eec\u5e94\u8be5\u4f7f\u7528 size_t \u6216\u8005\u80fd\u591f\u88ab \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u6210 size_t \u7684\u53d8\u91cf\u7c7b\u578b\u3002 \u4f46\u662f\uff0c\u6211\u8ba4\u4e3a\u8fd9\u4e2a\u8bba\u70b9\u6ca1\u6709\u8bf4\u670d\u529b\uff0c\u539f\u56e0\u6709\u5f88\u591a\u3002\u9996\u5148\uff0c\u5b83\u8fdd\u80cc\u4e86\u4f7f\u7528\u6709\u7b26\u53f7\u6574\u6570\u800c\u4e0d\u662f\u65e0\u7b26\u53f7\u6574\u6570\u7684\u6700\u4f73\u5b9e\u8df5\u3002\u5176\u6b21\uff0c\u5f53\u4f7f\u7528\u6574\u6570\u957f\u5ea6\u521b\u5efa\u52a8\u6001\u6570\u7ec4\u65f6\uff0c\u6309\u7167\u60ef\u4f8b\u5e94\u8be5\u8fd9\u6837\u505a: 1 double * ptr { new double [ 5 ] }; 5 \u662f\u4e00\u4e2a\u6574\u578b \u5b57\u9762\u91cf \uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u9690\u5f0f\u8f6c\u6362\u4e3a size_t \u3002\u5728C++23\u4e4b\u524d\uff0c\u6211\u4eec\u6ca1\u6709\u529e\u6cd5\u5728\u4e0d\u4f7f\u7528 static_cast \u7684\u60c5\u51b5\u4e0b\u521b\u5efa\u4e00\u4e2a size_t \u5b57\u9762\u91cf\u3002\u5982\u679cC++\u7684\u8bbe\u8ba1\u8005\u8981\u6c42\u6211\u4eec\u5728\u8fd9\u91cc\u662f size_t \u7684\u8bdd\uff0c\u4ed6\u80af\u5b9a\u4f1a\u63d0\u4f9b\u4e00\u4e2a\u521b\u5efa size_t \u5b57\u9762\u7684\u65b9\u6cd5\u3002 \u6700\u5e38\u89c1\u7684\u53cd\u5bf9\u610f\u89c1\u662f\uff0c\u4e00\u4e9b\u8fc2\u8150\u7684\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5c06\u6b64\u6807\u8bb0\u4e3a\u6709\u7b26\u53f7/\u65e0\u7b26\u53f7\u8f6c\u6362\u9519\u8bef(\u56e0\u4e3a\u6211\u4eec\u603b\u662f\u5c06\u8b66\u544a\u89c6\u4e3a\u9519\u8bef)\u3002\u7136\u800c\uff0c\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5373\u4f7f\u542f\u7528\u4e86\u6b64\u7c7b\u8b66\u544a(' -Wconversion ')\uff0c GCC\u4e5f\u4e0d\u4f1a\u5c06\u5176\u6807\u8bb0\u4e3a\u6709\u7b26\u53f7/\u65e0\u7b26\u53f7\u8f6c\u6362\u9519\u8bef\u3002 \u867d\u7136\u4f7f\u7528 size_t \u4f5c\u4e3a\u52a8\u6001\u5206\u914d\u6570\u7ec4\u7684\u957f\u5ea6\u6ca1\u6709\u4ec0\u4e48\u9519\uff0c\u4f46\u5728\u672c\u7cfb\u5217\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u4e0d\u4f1a\u62d8\u4e8e\u8981\u6c42\u4f7f\u7528\u5b83\u3002 \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u6b64\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u662f\u4ece\u4e0d\u540c\u7684\u5730\u65b9\u5206\u914d\u7684\uff0c\u800c\u4e0d\u662f\u7528\u4e8e\u56fa\u5b9a\u6570\u7ec4\u7684\u5185\u5b58\uff0c\u56e0\u6b64\u6570\u7ec4\u7684\u5927\u5c0f\u53ef\u4ee5\u5f88\u5927\u3002\u4f60\u53ef\u4ee5\u8fd0\u884c\u4e0a\u9762\u7684\u7a0b\u5e8f\u5e76\u5206\u914d\u957f\u5ea6\u4e3a1,000,000(\u751a\u81f3\u53ef\u80fd\u662f100,000,000)\u7684\u6570\u7ec4\u3002\u8bd5\u4e00\u8bd5\uff01\u6b63\u56e0\u4e3a\u5982\u6b64\uff0c\u5728C++\u4e2d\u9700\u8981\u5206\u914d\u5927\u91cf\u5185\u5b58\u7684\u7a0b\u5e8f\u901a\u5e38\u9700\u8981\u5206\u914d\u52a8\u6001\u5185\u5b58\u3002 \u52a8\u6001\u5730\u5220\u9664\u6570\u7ec4 \u5220\u9664\u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4\u9700\u8981\u6570\u7ec4\u7248\u672c\u7684 delete \uff0c\u5373 delete[] \u3002 \u8fd9\u544a\u8bc9CPU\u5b83\u9700\u8981\u6e05\u7406\u591a\u4e2a\u53d8\u91cf\u800c\u4e0d\u662f\u5355\u4e2a\u53d8\u91cf\u3002\u65b0\u7a0b\u5e8f\u5458\u5728\u5904\u7406\u52a8\u6001\u5185\u5b58\u5206\u914d\u65f6\u6700\u5e38\u72af\u7684\u9519\u8bef\u4e4b\u4e00\u662f\u5728\u5220\u9664\u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4\u65f6\u4f7f\u7528delete\u800c\u4e0d\u662f delete[] \u3002\u5728\u6570\u7ec4\u4e0a\u4f7f\u7528\u6807\u91cf\u7248\u672c\u7684 delete \u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \uff0c\u5982\u6570\u636e\u635f\u574f\u3001\u5185\u5b58\u6cc4\u6f0f\u3001\u7a0b\u5e8f\u5d29\u6e83\u6216\u5176\u4ed6\u95ee\u9898\u3002 \u5173\u4e8e delete[] \u7684\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u662f\uff0c delete \u5982\u4f55\u77e5\u9053\u5e94\u8be5\u91ca\u653e\u591a\u5c11\u5185\u5b58\u5462\uff1f\u5b9e\u9645\u4e0a\uff0c new[] \u4f1a\u8d1f\u8d23\u8ffd\u8e2a\u4e3a\u53d8\u91cf\u5206\u914d\u7684\u5185\u5b58\u7684\u5927\u5c0f\uff0c\u6240\u4ee5 delete[] \u80fd\u591f\u5220\u9664\u8be5\u53d8\u91cf\u5b9e\u9645\u5206\u914d\u7684\u5185\u5b58\u3002\u4f46\u662f\uff0c\u8fd9\u6bb5\u5185\u5b58\u7684\u957f\u5ea6\u503c\u5e76\u4e0d\u80fd\u591f\u88ab\u5f00\u53d1\u8005\u83b7\u53d6\u3002 \u52a8\u6001\u6570\u7ec4\u548c\u56fa\u5b9a\u6570\u7ec4\u51e0\u4e4e\u662f\u4e00\u6837\u7684 \u5728 11.8 - \u6307\u9488\u548c\u6570\u7ec4 \u4e2d\uff0c\u6211\u4eec\u77e5\u9053\u56fa\u5b9a\u6570\u7ec4\u53d8\u91cf\u4f1a\u6301\u6709\u6307\u5411\u6570\u7ec4\u9996\u4e2a\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u3002\u540c\u65f6\u6211\u4eec\u8fd8\u77e5\u9053\uff0c\u56fa\u5b9a\u6570\u7ec4\u5728\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\uff0c\u4f1a\u9000\u5316\u4e3a\u6307\u9488\uff0c\u6b64\u65f6\u6570\u7ec4\u7684\u957f\u5ea6\u5c31\u65e0\u6cd5\u5728\u51fd\u6570\u5185\u88ab\u83b7\u53d6\u4e86\u3002(\u6b64\u65f6 sizeof() \u4e5f\u4e0d\u53ef\u7528\u4e86)\u3002 \u5bf9\u4e8e\u52a8\u6001\u6570\u7ec4\u6765\u8bf4\uff0c\u5b83\u751f\u6765\u5c31\u662f\u4e00\u4e2a\u6307\u5411\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u6307\u9488\u3002\u56e0\u6b64\uff0c\u5b83\u4e5f\u4e0d\u77e5\u9053\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u4f7f\u7528\u52a8\u6001\u6570\u7ec4\u548c\u56fa\u5b9a\u6570\u7ec4\u4e0a\u662f\u4e00\u81f4\u7684\uff0c\u4f46\u662f\u7a0b\u5e8f\u5458\u9700\u8981\u8d1f\u8d23\u91ca\u653e\u52a8\u6001\u6570\u7ec4\u7684\u5185\u5b58\u3002 \u521d\u59cb\u5316\u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4 \u5982\u679c\u4f60\u60f3\u8981\u5c06\u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4\u521d\u59cb\u5316\u4e3a0\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 int * array { new int [ length ]{} }; \u5728C++11\u4e4b\u524d\uff0c\u5e76\u6ca1\u6709\u5c06\u52a8\u6001\u6570\u7ec4\u521d\u59cb\u5316\u4e3a\u975e0\u7684\u7b80\u4fbf\u65b9\u6cd5\uff08 \u521d\u59cb\u5316\u503c\u5217\u8868 \u53ea\u5bf9\u56fa\u5b9a\u6570\u7ec4\u6709\u6548\uff09\u3002\u8fd9\u5c31\u610f\u5473\u7740\u4f60\u5fc5\u987b\u4f7f\u7528\u5faa\u73af\u4f9d\u6b21\u4e3a\u6bcf\u4e2a\u5143\u7d20\u8d4b\u503c\u624d\u884c\u3002 1 2 3 4 5 6 int * array = new int [ 5 ]; array [ 0 ] = 9 ; array [ 1 ] = 7 ; array [ 2 ] = 5 ; array [ 3 ] = 3 ; array [ 4 ] = 1 ; \u592a\u9ebb\u70e6\u4e86\uff01 \u4f46\u662f\uff0c\u4eceC++11\u5f00\u59cb\uff0c \u521d\u59cb\u5316\u503c\u5217\u8868 \u4e5f\u53ef\u4ee5\u7528\u4e8e\u521d\u59cb\u5316\u52a8\u6001\u6570\u7ec4\u4e86\uff01 1 2 3 4 int fixedArray [ 5 ] = { 9 , 7 , 5 , 3 , 1 }; // initialize a fixed array before C++11 int * array { new int [ 5 ]{ 9 , 7 , 5 , 3 , 1 } }; // initialize a dynamic array since C++11 // To prevent writing the type twice, we can use auto. This is often done for types with long names. auto * array { new int [ 5 ]{ 9 , 7 , 5 , 3 , 1 } }; \u6ce8\u610f\uff0c\u5728\u6570\u7ec4\u957f\u5ea6\u548c\u521d\u59cb\u5316\u503c\u5217\u8868\u4e4b\u95f4\u6ca1\u6709\u7b49\u53f7\u3002 \u4e3a\u4e86\u4fdd\u6301\u4e00\u81f4\uff0c\u56fa\u5b9a\u6570\u7ec4\u4e5f\u53ef\u4ee5\u4f7f\u7528 \u7edf\u4e00\u521d\u59cb\u5316 \u7684\u65b9\u5f0f\u8fdb\u884c\u521d\u59cb\u5316\u3002 1 2 int fixedArray []{ 9 , 7 , 5 , 3 , 1 }; // initialize a fixed array in C++11 char fixedArray []{ \"Hello, world!\" }; // initialize a fixed array in C++11 \u663e\u5f0f\u58f0\u660e\u6570\u7ec4\u7684\u5927\u5c0f\u662f\u53ef\u9009\u7684\u3002 \u8c03\u6574\u6570\u7ec4\u5927\u5c0f \u52a8\u6001\u5206\u914d\u6570\u7ec4\u5141\u8bb8\u4f60\u5728\u5206\u914d\u65f6\u8bbe\u7f6e\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u4f46\u662f\uff0cC++\u4e0d\u63d0\u4f9b\u8c03\u6574\u5df2\u7ecf\u5206\u914d\u7684\u6570\u7ec4\u5927\u5c0f\u7684\u5185\u7f6e\u65b9\u6cd5\u3002\u53ef\u4ee5\u901a\u8fc7\u52a8\u6001\u5206\u914d\u65b0\u6570\u7ec4\u3001\u590d\u5236\u5143\u7d20\u5e76\u5220\u9664\u65e7\u6570\u7ec4\u6765\u89e3\u51b3\u8fd9\u4e2a\u9650\u5236\u3002\u7136\u800c\uff0c\u8fd9\u5f88\u5bb9\u6613\u51fa\u9519\uff0c\u7279\u522b\u662f\u5f53\u5143\u7d20\u7c7b\u578b\u662f\u7c7b(\u7c7b\u6709\u7279\u6b8a\u7684\u89c4\u5219\u63a7\u5236\u5b83\u4eec\u7684\u521b\u5efa\u65b9\u5f0f)\u65f6\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u5e94\u8be5\u907f\u514d\u81ea\u5df1\u624b\u52a8\u8c03\u6574\u6570\u7ec4\u7684\u5927\u5c0f\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u5982\u679c\u4f60\u9700\u8981\u8be5\u529f\u80fd\uff0cC++\u63d0\u4f9b\u4e86\u652f\u6301\u8c03\u6574\u5927\u5c0f\u7684\u5bb9\u5668 std::vector \u3002\u53c2\u89c1\uff1a 11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb \u3002","title":"11.12 - \u52a8\u6001\u5206\u914d\u6570\u7ec4"},{"location":"11-12-Dynamically-allocating-arrays/#_1","text":"\u5220\u9664\u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4\u9700\u8981\u6570\u7ec4\u7248\u672c\u7684 delete \uff0c\u5373 delete[] \u3002 \u8fd9\u544a\u8bc9CPU\u5b83\u9700\u8981\u6e05\u7406\u591a\u4e2a\u53d8\u91cf\u800c\u4e0d\u662f\u5355\u4e2a\u53d8\u91cf\u3002\u65b0\u7a0b\u5e8f\u5458\u5728\u5904\u7406\u52a8\u6001\u5185\u5b58\u5206\u914d\u65f6\u6700\u5e38\u72af\u7684\u9519\u8bef\u4e4b\u4e00\u662f\u5728\u5220\u9664\u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4\u65f6\u4f7f\u7528delete\u800c\u4e0d\u662f delete[] \u3002\u5728\u6570\u7ec4\u4e0a\u4f7f\u7528\u6807\u91cf\u7248\u672c\u7684 delete \u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \uff0c\u5982\u6570\u636e\u635f\u574f\u3001\u5185\u5b58\u6cc4\u6f0f\u3001\u7a0b\u5e8f\u5d29\u6e83\u6216\u5176\u4ed6\u95ee\u9898\u3002 \u5173\u4e8e delete[] \u7684\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u662f\uff0c delete \u5982\u4f55\u77e5\u9053\u5e94\u8be5\u91ca\u653e\u591a\u5c11\u5185\u5b58\u5462\uff1f\u5b9e\u9645\u4e0a\uff0c new[] \u4f1a\u8d1f\u8d23\u8ffd\u8e2a\u4e3a\u53d8\u91cf\u5206\u914d\u7684\u5185\u5b58\u7684\u5927\u5c0f\uff0c\u6240\u4ee5 delete[] \u80fd\u591f\u5220\u9664\u8be5\u53d8\u91cf\u5b9e\u9645\u5206\u914d\u7684\u5185\u5b58\u3002\u4f46\u662f\uff0c\u8fd9\u6bb5\u5185\u5b58\u7684\u957f\u5ea6\u503c\u5e76\u4e0d\u80fd\u591f\u88ab\u5f00\u53d1\u8005\u83b7\u53d6\u3002","title":"\u52a8\u6001\u5730\u5220\u9664\u6570\u7ec4"},{"location":"11-12-Dynamically-allocating-arrays/#_2","text":"\u5728 11.8 - \u6307\u9488\u548c\u6570\u7ec4 \u4e2d\uff0c\u6211\u4eec\u77e5\u9053\u56fa\u5b9a\u6570\u7ec4\u53d8\u91cf\u4f1a\u6301\u6709\u6307\u5411\u6570\u7ec4\u9996\u4e2a\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u3002\u540c\u65f6\u6211\u4eec\u8fd8\u77e5\u9053\uff0c\u56fa\u5b9a\u6570\u7ec4\u5728\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\uff0c\u4f1a\u9000\u5316\u4e3a\u6307\u9488\uff0c\u6b64\u65f6\u6570\u7ec4\u7684\u957f\u5ea6\u5c31\u65e0\u6cd5\u5728\u51fd\u6570\u5185\u88ab\u83b7\u53d6\u4e86\u3002(\u6b64\u65f6 sizeof() \u4e5f\u4e0d\u53ef\u7528\u4e86)\u3002 \u5bf9\u4e8e\u52a8\u6001\u6570\u7ec4\u6765\u8bf4\uff0c\u5b83\u751f\u6765\u5c31\u662f\u4e00\u4e2a\u6307\u5411\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u6307\u9488\u3002\u56e0\u6b64\uff0c\u5b83\u4e5f\u4e0d\u77e5\u9053\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u4f7f\u7528\u52a8\u6001\u6570\u7ec4\u548c\u56fa\u5b9a\u6570\u7ec4\u4e0a\u662f\u4e00\u81f4\u7684\uff0c\u4f46\u662f\u7a0b\u5e8f\u5458\u9700\u8981\u8d1f\u8d23\u91ca\u653e\u52a8\u6001\u6570\u7ec4\u7684\u5185\u5b58\u3002","title":"\u52a8\u6001\u6570\u7ec4\u548c\u56fa\u5b9a\u6570\u7ec4\u51e0\u4e4e\u662f\u4e00\u6837\u7684"},{"location":"11-12-Dynamically-allocating-arrays/#_3","text":"\u5982\u679c\u4f60\u60f3\u8981\u5c06\u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4\u521d\u59cb\u5316\u4e3a0\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 int * array { new int [ length ]{} }; \u5728C++11\u4e4b\u524d\uff0c\u5e76\u6ca1\u6709\u5c06\u52a8\u6001\u6570\u7ec4\u521d\u59cb\u5316\u4e3a\u975e0\u7684\u7b80\u4fbf\u65b9\u6cd5\uff08 \u521d\u59cb\u5316\u503c\u5217\u8868 \u53ea\u5bf9\u56fa\u5b9a\u6570\u7ec4\u6709\u6548\uff09\u3002\u8fd9\u5c31\u610f\u5473\u7740\u4f60\u5fc5\u987b\u4f7f\u7528\u5faa\u73af\u4f9d\u6b21\u4e3a\u6bcf\u4e2a\u5143\u7d20\u8d4b\u503c\u624d\u884c\u3002 1 2 3 4 5 6 int * array = new int [ 5 ]; array [ 0 ] = 9 ; array [ 1 ] = 7 ; array [ 2 ] = 5 ; array [ 3 ] = 3 ; array [ 4 ] = 1 ; \u592a\u9ebb\u70e6\u4e86\uff01 \u4f46\u662f\uff0c\u4eceC++11\u5f00\u59cb\uff0c \u521d\u59cb\u5316\u503c\u5217\u8868 \u4e5f\u53ef\u4ee5\u7528\u4e8e\u521d\u59cb\u5316\u52a8\u6001\u6570\u7ec4\u4e86\uff01 1 2 3 4 int fixedArray [ 5 ] = { 9 , 7 , 5 , 3 , 1 }; // initialize a fixed array before C++11 int * array { new int [ 5 ]{ 9 , 7 , 5 , 3 , 1 } }; // initialize a dynamic array since C++11 // To prevent writing the type twice, we can use auto. This is often done for types with long names. auto * array { new int [ 5 ]{ 9 , 7 , 5 , 3 , 1 } }; \u6ce8\u610f\uff0c\u5728\u6570\u7ec4\u957f\u5ea6\u548c\u521d\u59cb\u5316\u503c\u5217\u8868\u4e4b\u95f4\u6ca1\u6709\u7b49\u53f7\u3002 \u4e3a\u4e86\u4fdd\u6301\u4e00\u81f4\uff0c\u56fa\u5b9a\u6570\u7ec4\u4e5f\u53ef\u4ee5\u4f7f\u7528 \u7edf\u4e00\u521d\u59cb\u5316 \u7684\u65b9\u5f0f\u8fdb\u884c\u521d\u59cb\u5316\u3002 1 2 int fixedArray []{ 9 , 7 , 5 , 3 , 1 }; // initialize a fixed array in C++11 char fixedArray []{ \"Hello, world!\" }; // initialize a fixed array in C++11 \u663e\u5f0f\u58f0\u660e\u6570\u7ec4\u7684\u5927\u5c0f\u662f\u53ef\u9009\u7684\u3002","title":"\u521d\u59cb\u5316\u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4"},{"location":"11-12-Dynamically-allocating-arrays/#_4","text":"\u52a8\u6001\u5206\u914d\u6570\u7ec4\u5141\u8bb8\u4f60\u5728\u5206\u914d\u65f6\u8bbe\u7f6e\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u4f46\u662f\uff0cC++\u4e0d\u63d0\u4f9b\u8c03\u6574\u5df2\u7ecf\u5206\u914d\u7684\u6570\u7ec4\u5927\u5c0f\u7684\u5185\u7f6e\u65b9\u6cd5\u3002\u53ef\u4ee5\u901a\u8fc7\u52a8\u6001\u5206\u914d\u65b0\u6570\u7ec4\u3001\u590d\u5236\u5143\u7d20\u5e76\u5220\u9664\u65e7\u6570\u7ec4\u6765\u89e3\u51b3\u8fd9\u4e2a\u9650\u5236\u3002\u7136\u800c\uff0c\u8fd9\u5f88\u5bb9\u6613\u51fa\u9519\uff0c\u7279\u522b\u662f\u5f53\u5143\u7d20\u7c7b\u578b\u662f\u7c7b(\u7c7b\u6709\u7279\u6b8a\u7684\u89c4\u5219\u63a7\u5236\u5b83\u4eec\u7684\u521b\u5efa\u65b9\u5f0f)\u65f6\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u5e94\u8be5\u907f\u514d\u81ea\u5df1\u624b\u52a8\u8c03\u6574\u6570\u7ec4\u7684\u5927\u5c0f\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u5982\u679c\u4f60\u9700\u8981\u8be5\u529f\u80fd\uff0cC++\u63d0\u4f9b\u4e86\u652f\u6301\u8c03\u6574\u5927\u5c0f\u7684\u5bb9\u5668 std::vector \u3002\u53c2\u89c1\uff1a 11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb \u3002","title":"\u8c03\u6574\u6570\u7ec4\u5927\u5c0f"},{"location":"11-13-For-each-loops/","tags":["for-each","C++11","C++20"],"text":"Key Takeaway \u5728 for-each \u5faa\u73af\u4e2d\u58f0\u660e\u5143\u7d20\u65f6\uff0c\u5982\u679c\u5143\u7d20\u662f\u975e\u57fa\u672c\u7c7b\u578b\uff0c\u5219\u5c06\u5176\u58f0\u660e\u4e3a\u5f15\u7528\u6216\u5e38\u6570\u5f15\u7528\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002 for-each \u5faa\u73af\u5fc5\u987b\u77e5\u9053\u88ab\u904d\u5386\u5bb9\u5668\u7684\u5927\u5c0f\uff0c\u6240\u4ee5\u5b83\u4e0d\u80fd\u914d\u5408\u9000\u5316\u4e3a\u6307\u9488\u7684\u6570\u7ec4\u6216\u52a8\u6001\u6570\u7ec4\u5de5\u4f5c For-each \u5faa\u73af\u4e0d\u4ec5\u80fd\u591f\u914d\u5408\u56fa\u5b9a\u6570\u7ec4\u4e00\u8d77\u4f7f\u7528\uff0c\u5b83\u53ef\u4ee5\u548c\u591a\u79cd\u7c7b\u5217\u8868\u7684\u6570\u636e\u7ed3\u6784\u4e00\u8d77\u5de5\u4f5c\uff0c\u4f8b\u5982vector(\u4f8b\u5982 std::vector )\u3001\u94fe\u8868\u3001\u6811\u548c\u6620\u5c04\u3002 \u901a\u8fc7\u653e\u7f6e\u5728\u5faa\u73af\u53d8\u91cf\u7684\u524d\u9762\u7684 init-statement \u53ef\u4ee5\u50cf\u666e\u901afor\u5faa\u73af\u4e00\u6837\u521b\u5efa\u4e00\u4e2a\u7d22\u5f15(C++20)\u5e76\u5728\u5faa\u73af\u4e2d\u5bf9\u5176\u8fdb\u884c\u9012\u589e\u3002\u5728C++20\u4e4b\u524d\uff0c\u5fc5\u987b\u5728\u5faa\u73af\u5916\u58f0\u660e\u8fd9\u7d22\u5f15\uff0c\u6ce8\u610f\u53ef\u80fd\u4f1a\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\u7684\u60c5\u51b5\u3002 \u5728 11.3 - \u6570\u7ec4\u548c\u5faa\u73af \u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include // std::size int main () { constexpr int scores []{ 84 , 92 , 76 , 81 , 56 }; constexpr int numStudents { std :: size ( scores ) }; int maxScore { 0 }; // keep track of our largest score for ( int student { 0 }; student < numStudents ; ++ student ) { if ( scores [ student ] > maxScore ) { maxScore = scores [ student ]; } } std :: cout << \"The best score was \" << maxScore << '\\n' ; return 0 ; } \u5c3d\u7ba1\u4f7f\u7528 for \u5faa\u73af\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u904d\u5386\u4e00\u4e2a\u6570\u7ec4\uff0c\u4f46\u662f\u5728\u771f\u6b63\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u8fd9\u79cd\u65b9\u5f0f\u5bb9\u6613\u51fa\u9519\uff0c\u800c\u4e14\u4e5f\u5bb9\u6613\u72af \u5dee\u4e00\u9519\u8bef \u3002 \u4f7f\u7528 for-each \u5faa\u73af\uff08\u4e5f\u79f0\u4e3a \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \uff09\u53ef\u4ee5\u66f4\u52a0\u7b80\u4fbf\u3001\u66f4\u52a0\u5b89\u5168\u5730\u904d\u5386\u6570\u7ec4\uff08\u6216\u5176\u4ed6\u7c7b\u4f3c\u7684\u6570\u636e\u7ed3\u6784\uff09\u4e2d\u7684\u5143\u7d20\u3002 For-each \u5faa\u73af for-each \u5faa\u73af\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a 1 2 for ( element_declaration : array ) statement ; \u5f53\u6267\u884c\u6b64\u8bed\u53e5\u65f6\uff0c\u5faa\u73af\u5c06\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5c06\u5f53\u524d\u6570\u7ec4\u5143\u7d20\u7684\u503c\u8d4b\u7ed9 element_declaration \u4e2d\u58f0\u660e\u7684\u53d8\u91cf\u3002\u4e3a\u4e86\u83b7\u5f97\u6700\u4f73\u7ed3\u679c\uff0c element_declaration \u5e94\u8be5\u5177\u6709\u4e0e\u6570\u7ec4\u5143\u7d20\u76f8\u540c\u7684\u7c7b\u578b\uff0c\u5426\u5219\u5c06\u53d1\u751f\u7c7b\u578b\u8f6c\u6362\u3002 \u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5b83\u4f7f\u7528 for-each \u5faa\u73af\u6765\u6253\u5370\u6590\u6ce2\u90a3\u5951\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { constexpr int fibonacci []{ 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 }; for ( int number : fibonacci ) // iterate over array fibonacci { std :: cout << number << ' ' ; // we access the array element for this iteration through variable number } std :: cout << '\\n' ; return 0 ; } \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 0 1 1 2 3 5 8 13 21 34 55 89 \u8ba9\u6211\u4eec\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\u4e0a\u9762\u4ee3\u7801\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u9996\u5148\uff0c\u6267\u884c for \u5faa\u73af\uff0c\u53d8\u91cf number \u7684\u503c\u9996\u5148\u88ab\u8bbe\u7f6e\u4e3a\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u4e5f\u5c31\u662f 0\u3002\u7a0b\u5e8f\u968f\u540e\u6253\u5370\u8be5\u503c\uff0c\u5373\u6253\u53700.\u7136\u540e\uff0cfor \u5faa\u73af\u518d\u6b21\u6267\u884c\uff0c number \u88ab\u8bbe\u7f6e\u4e3a\u6570\u7ec4\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff0c\u53731\u3002\u7136\u540e\u6253\u53701\u3002for \u5faa\u73af\u4e0d\u65ad\u5730\u6267\u884c\uff0c\u4f9d\u6b21\u6253\u5370\u51fa\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u76f4\u5230\u904d\u5386\u5b8c\u5168\u90e8\u6570\u7ec4\u3002\u7136\u540e\uff0c\u5faa\u73af\u622a\u6b62\u4e86\uff0c\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c\uff08\u8fd4\u56de0\u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff09\u3002 \u6ce8\u610f\uff0c\u8fd9\u91cc\u7684 number \u5e76\u4e0d\u662f\u4e00\u4e2a\u6570\u7ec4\u7d22\u5f15\u3002\u5b83\u672c\u8eab\u88ab\u8d4b\u503c\u4e3a\u5f53\u524d\u904d\u5386\u5230\u7684\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002 For each \u5faa\u73af\u548c auto \u5173\u952e\u5b57 \u56e0\u4e3a element_declaration \u5e94\u8be5\u4e0e\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5c5e\u4e8e\u540c\u4e00\u7c7b\u578b\uff0c\u90a3\u4e48\u6700\u7406\u60f3\u7684\u65b9\u5f0f\u5c31\u662f\u4f7f\u7528 auto \u5173\u952e\u5b57\u58f0\u660e\u8be5\u53d8\u91cf\u5e76\u8ba9C++\u4e3a\u6211\u4eec\u81ea\u52a8\u63a8\u65ad\u5176\u7c7b\u578b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { constexpr int fibonacci []{ 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 }; for ( auto number : fibonacci ) // type is auto, so number has its type deduced from the fibonacci array { std :: cout << number << ' ' ; } std :: cout << '\\n' ; return 0 ; } For-each \u5faa\u73af\u548c\u5f15\u7528 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c element \u88ab\u58f0\u660e\u4e3a\u4e00\u4e2a\u503c\u53d8\u91cf\uff1a 1 2 3 4 5 std :: string array []{ \"peter\" , \"likes\" , \"frozen\" , \"yogurt\" }; for ( auto element : array ) // element will be a copy of the current array element { std :: cout << element << ' ' ; } \u8fd9\u610f\u5473\u7740\u5728\u904d\u5386\u6570\u7ec4\u65f6\uff0c\u5f53\u524d\u5143\u7d20\u4f1a\u88ab\u62f7\u8d1d\u5230 element \u3002\u62f7\u8d1d\u6570\u7ec4\u5143\u7d20\u7684\u5f00\u9500\u4f1a\u5f88\u5927\uff0c\u800c\u4e14\u591a\u6570\u60c5\u51b5\u4e0b\u6211\u4eec\u5b9e\u9645\u4e0a\u60f3\u8981\u8bbf\u95ee\u539f\u672c\u7684\u5143\u7d20\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u58f0\u660e\u4e3a\u5f15\u7528\u7c7b\u578b\uff1a 1 2 3 4 5 std :: string array []{ \"peter\" , \"likes\" , \"frozen\" , \"yogurt\" }; for ( auto & element : array ) // The ampersand makes element a reference to the actual array element, preventing a copy from being made { std :: cout << element << ' ' ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c element \u662f\u5f53\u524d\u8fed\u4ee3\u6570\u7ec4\u5143\u7d20\u7684\u5f15\u7528\uff0c\u4ece\u800c\u907f\u514d\u4e86\u590d\u5236\u3002\u6b64\u5916\uff0c\u5bf9 element \u7684\u4efb\u4f55\u4fee\u6539\u90fd\u4f1a\u5f71\u54cd\u6b63\u5728\u8fed\u4ee3\u7684\u6570\u7ec4\uff0c\u5982\u679c element \u662f\u4e00\u4e2a\u666e\u901a\u53d8\u91cf\uff0c\u8fd9\u662f\u4e0d\u53ef\u80fd\u7684\u3002 \u5f53\u7136\uff0c\u5982\u679c\u4f60\u6253\u7b97\u4ee5\u53ea\u8bfb\u7684\u65b9\u5f0f\u4f7f\u7528\u5f15\u7528\uff0c\u5c06\u5b83\u8bbe\u4e3a const \u662f\u4e2a\u597d\u4e3b\u610f\uff1a 1 2 3 4 5 std :: string array []{ \"peter\" , \"likes\" , \"frozen\" , \"yogurt\" }; for ( const auto & element : array ) // element is a const reference to the currently iterated array element { std :: cout << element << ' ' ; } \u6700\u4f73\u5b9e\u8df5 \u5728 for-each \u5faa\u73af\u4e2d\u58f0\u660e\u5143\u7d20\u65f6\uff0c\u5982\u679c\u5143\u7d20\u662f\u975e\u57fa\u672c\u7c7b\u578b\uff0c\u5219\u5c06\u5176\u58f0\u660e\u4e3a\u5f15\u7528\u6216\u5e38\u6570\u5f15\u7528\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002 \u4f7f\u7528 for-each \u5faa\u73af\u91cd\u5199\u6700\u9ad8\u5206\u6570\u7684\u4f8b\u5b50 \u63a5\u4e0b\u6765\uff0c\u4f7f\u7528 for-each \u91cd\u5199\u5f00\u5934\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { constexpr int scores []{ 84 , 92 , 76 , 81 , 56 }; int maxScore { 0 }; // keep track of our largest score for ( auto score : scores ) // iterate over array scores, assigning each value in turn to variable score { if ( score > maxScore ) { maxScore = score ; } } std :: cout << \"The best score was \" << maxScore << '\\n' ; return 0 ; } \u6ce8\u610f\uff0c\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u4e0d\u518d\u9700\u8981\u901a\u8fc7\u4e0b\u6807\u7d22\u5f15\u6570\u7ec4\u6216\u83b7\u53d6\u6570\u7ec4\u5927\u5c0f\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u53d8\u91cf score \u76f4\u63a5\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u3002\u6570\u7ec4\u5fc5\u987b\u6709\u5927\u5c0f\u4fe1\u606f\uff0c\u9000\u5316\u4e3a\u6307\u9488\u7684\u6570\u7ec4\u4e0d\u80fd\u5728for-each\u5faa\u73af\u4e2d\u4f7f\u7528\u3002 For-each \u5faa\u73af\u548c\u975e\u6570\u7ec4\u53d8\u91cf For-each \u5faa\u73af\u4e0d\u4ec5\u80fd\u914d\u5408\u56fa\u5b9a\u6570\u7ec4\u4e00\u8d77\u4f7f\u7528\uff0c\u5b83\u53ef\u4ee5\u548c\u591a\u79cd\u7c7b\u5217\u8868\u7684\u6570\u636e\u7ed3\u6784\u4e00\u8d77\u5de5\u4f5c\uff0c\u4f8b\u5982vector(\u4f8b\u5982 std::vector )\u3001\u94fe\u8868\u3001\u6811\u548c\u6620\u5c04 \u3002 \u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u8fd9\u4e9b\u5185\u5bb9\uff0c\u6240\u4ee5\u4f60\u4e0d\u77e5\u9053\u4e5f\u6ca1\u5173\u7cfb\u3002\u4f60\u73b0\u5728\u53ea\u9700\u8981\u8bb0\u4f4f\uff0cfor-each\u5faa\u73af\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u3001\u901a\u7528\u7684\u904d\u5386\u65b9\u6cd5\uff08\u4e0d\u4ec5\u4ec5\u662f\u904d\u5386\u6570\u7ec4\u800c\u5df2\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include int main () { std :: vector fibonacci { 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 }; // note use of std::vector here rather than a fixed array // Before C++17 // std::vector fibonacci{ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 }; for ( auto number : fibonacci ) { std :: cout << number << ' ' ; } std :: cout << '\\n' ; return 0 ; } For-each \u4e0d\u80fd\u548c\u6307\u5411\u6570\u7ec4\u7684\u6307\u9488\u4e00\u8d77\u5de5\u4f5c \u4e3a\u4e86\u904d\u5386\u6570\u7ec4\uff0cfor-each\u9700\u8981\u77e5\u9053\u6570\u7ec4\u7684\u5927\u5c0f\u3002\u56e0\u4e3a\u5df2\u7ecf\u9000\u5316\u4e3a\u6307\u9488\u7684\u6570\u7ec4\u4e0d\u518d\u5305\u542b\u6570\u7ec4\u5927\u5c0f\u7684\u4fe1\u606f\uff0c\u56e0\u6b64for-each\u5faa\u73af\u5c06\u65e0\u6cd5\u914d\u5408\u5b83\u4eec\u4e00\u8d77\u5de5\u4f5c\uff01 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int sumArray ( const int array []) // array is a pointer { int sum { 0 }; for ( auto number : array ) // compile error, the size of array isn't known { sum += number ; } return sum ; } int main () { constexpr int array []{ 9 , 7 , 5 , 3 , 1 }; std :: cout << sumArray ( array ) << '\\n' ; // array decays into a pointer here return 0 ; } \u540c\u6837\uff0c\u7531\u4e8e\u540c\u6837\u7684\u539f\u56e0\uff0c\u52a8\u6001\u6570\u7ec4\u4e5f\u4e0d\u80fd\u7528\u4e8efor-each\u5faa\u73af\u3002 \u80fd\u591f\u83b7\u53d6\u5f53\u524d\u5143\u7d20\u7684\u7d22\u5f15\u5417\uff1f For-each \u5faa\u73af\u4e0d\u80fd\u591f\u63d0\u4f9b\u8bbf\u95ee\u6570\u7ec4\u7d22\u5f15\u7684\u76f4\u63a5\u65b9\u6cd5\u3002\u8fd9\u662f\u56e0\u4e3a\u5f88\u591a\u914d\u5408for-each\u5faa\u73af\u5de5\u4f5c\u7684\u6570\u636e\u7ed3\u6784\uff08\u4f8b\u5982\u94fe\u8868\uff09\u5e76\u4e0d\u80fd\u591f\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5143\u7d20\u3002 \u4eceC++20\u5f00\u59cb\uff0c\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af\u53ef\u4ee5\u4e0e\u4e00\u4e2a\u521d\u59cb\u5316\u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u5c31\u50cf\u666e\u901afor\u5faa\u73af\u4e2d\u7684\u521d\u59cb\u5316\u8bed\u53e5\u4e00\u6837\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 init-statement \u6765\u521b\u5efa\u624b\u52a8\u7d22\u5f15\u8ba1\u6570\u5668\uff0c\u800c\u4e0d\u4f1a\u7834\u574ffor\u5faa\u73af\u6240\u5728\u7684\u51fd\u6570\u3002 init-statement \u5e94\u8be5\u653e\u7f6e\u5728\u5faa\u73af\u53d8\u91cf\u7684\u524d\u9762\uff1a 1 2 for ( init - statement ; element_declaration : array ) statement ; \u5728\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u6709\u4e24\u4e2a\u901a\u8fc7\u7d22\u5f15\u5173\u8054\u7684\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u540d\u5b57\u4e3a\u201c names[3] \u201d\u7684\u5b66\u751f\u7684\u5206\u6570\u4e3a\u201c scores[3] \u201d\u3002\u6bcf\u5f53\u53d1\u73b0\u4e00\u4e2a\u65b0\u7684\u9ad8\u5206\u5b66\u751f\u65f6\uff0c\u6211\u4eec\u5c31\u6253\u5370\u51fa\u4ed6\u4eec\u7684\u540d\u5b57\u548c\u4e0e\u524d\u4e00\u4e2a\u9ad8\u5206\u7684\u5206\u5dee\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include int main () { std :: string names []{ \"Alex\" , \"Betty\" , \"Caroline\" , \"Dave\" , \"Emily\" }; // Names of the students constexpr int scores []{ 84 , 92 , 76 , 81 , 56 }; int maxScore { 0 }; for ( int i { 0 }; auto score : scores ) // i is the index of the current element { if ( score > maxScore ) { std :: cout << names [ i ] << \" beat the previous best score of \" << maxScore << \" by \" << ( score - maxScore ) << \" points! \\n \" ; maxScore = score ; } ++ i ; } std :: cout << \"The best score was \" << maxScore << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 Alex beat the previous best score of 0 by 84 points! Betty beat the previous best score of 84 by 8 points! The best score was 92 \u8fd9\u91cc\u7684 int i{ 0 }; \u662f init-statement \uff0c\u5b83\u4f1a\u5728\u5faa\u73af\u6267\u884c\u65f6\u6267\u884c\u4e00\u6b21\u3002\u5728\u6bcf\u6b21\u5faa\u73af\u65f6\uff0c i \u5c31\u88ab\u9012\u589e1\uff0c\u7c7b\u4f3c\u4e0e\u666e\u901a\u7684\u5faa\u73af\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u6211\u4eec\u5728\u5faa\u73af\u4e2d\u4f7f\u7528 continue \u7684\u8bdd\uff0c ++i \u4f1a\u88ab\u8df3\u8fc7\uff0c\u5bfc\u81f4\u975e\u9884\u671f\u7684\u7ed3\u679c\u3002\u5982\u679c\u4f60\u9700\u8981\u4f7f\u7528 continue \u7684\u8bdd\uff0c\u8bf7\u786e\u4fdd i \u7684\u9012\u589e\u5728 continue \u4e4b\u524d\u8fdb\u884c\u3002 \u5728C++20\u4e4b\u524d\uff0c\u7d22\u5f15\u53d8\u91cf i \u5fc5\u987b\u5728\u5faa\u73af\u4e4b\u5916\u58f0\u660e\uff0c\u4f46\u8fd9\u53ef\u80fd\u5bfc\u81f4\u540d\u79f0\u51b2\u7a81\uff0c\u4f8b\u5982\u5f53\u6211\u4eec\u60f3\u5728\u540e\u9762\u7684\u51fd\u6570\u4e2d\u5b9a\u4e49\u53e6\u4e00\u4e2a\u540d\u4e3a i \u7684\u53d8\u91cf\u65f6\u3002 \u7ed3\u8bba \u5f53\u9700\u8981\u6309\u6b63\u5411\u987a\u5e8f\u8bbf\u95ee\u6240\u6709\u6570\u7ec4\u5143\u7d20\u65f6\uff0cFor-each \u5faa\u73af\u4e3a\u904d\u5386\u6570\u7ec4\u63d0\u4f9b\u4e86\u4e00\u79cd\u9ad8\u7ea7\u8bed\u6cd5\u3002\u5728\u53ef\u4ee5\u4f7f\u7528\u5b83\u7684\u60c5\u51b5\u4e0b\uff0c\u5b83\u5e94\u8be5\u4f18\u4e8e\u6807\u51c6\u7684for\u5faa\u73af\u3002\u4e3a\u4e86\u9632\u6b62\u590d\u5236\u6bcf\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u5c06\u5143\u7d20\u58f0\u660e\u4e3a\u5f15\u7528\u3002","title":"11.13 - for-each \u5faa\u73af"},{"location":"11-13-For-each-loops/#for-each","text":"for-each \u5faa\u73af\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a 1 2 for ( element_declaration : array ) statement ; \u5f53\u6267\u884c\u6b64\u8bed\u53e5\u65f6\uff0c\u5faa\u73af\u5c06\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5c06\u5f53\u524d\u6570\u7ec4\u5143\u7d20\u7684\u503c\u8d4b\u7ed9 element_declaration \u4e2d\u58f0\u660e\u7684\u53d8\u91cf\u3002\u4e3a\u4e86\u83b7\u5f97\u6700\u4f73\u7ed3\u679c\uff0c element_declaration \u5e94\u8be5\u5177\u6709\u4e0e\u6570\u7ec4\u5143\u7d20\u76f8\u540c\u7684\u7c7b\u578b\uff0c\u5426\u5219\u5c06\u53d1\u751f\u7c7b\u578b\u8f6c\u6362\u3002 \u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5b83\u4f7f\u7528 for-each \u5faa\u73af\u6765\u6253\u5370\u6590\u6ce2\u90a3\u5951\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { constexpr int fibonacci []{ 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 }; for ( int number : fibonacci ) // iterate over array fibonacci { std :: cout << number << ' ' ; // we access the array element for this iteration through variable number } std :: cout << '\\n' ; return 0 ; } \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 0 1 1 2 3 5 8 13 21 34 55 89 \u8ba9\u6211\u4eec\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\u4e0a\u9762\u4ee3\u7801\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u9996\u5148\uff0c\u6267\u884c for \u5faa\u73af\uff0c\u53d8\u91cf number \u7684\u503c\u9996\u5148\u88ab\u8bbe\u7f6e\u4e3a\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u4e5f\u5c31\u662f 0\u3002\u7a0b\u5e8f\u968f\u540e\u6253\u5370\u8be5\u503c\uff0c\u5373\u6253\u53700.\u7136\u540e\uff0cfor \u5faa\u73af\u518d\u6b21\u6267\u884c\uff0c number \u88ab\u8bbe\u7f6e\u4e3a\u6570\u7ec4\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff0c\u53731\u3002\u7136\u540e\u6253\u53701\u3002for \u5faa\u73af\u4e0d\u65ad\u5730\u6267\u884c\uff0c\u4f9d\u6b21\u6253\u5370\u51fa\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u76f4\u5230\u904d\u5386\u5b8c\u5168\u90e8\u6570\u7ec4\u3002\u7136\u540e\uff0c\u5faa\u73af\u622a\u6b62\u4e86\uff0c\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c\uff08\u8fd4\u56de0\u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff09\u3002 \u6ce8\u610f\uff0c\u8fd9\u91cc\u7684 number \u5e76\u4e0d\u662f\u4e00\u4e2a\u6570\u7ec4\u7d22\u5f15\u3002\u5b83\u672c\u8eab\u88ab\u8d4b\u503c\u4e3a\u5f53\u524d\u904d\u5386\u5230\u7684\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002","title":"For-each \u5faa\u73af"},{"location":"11-13-For-each-loops/#for-each-auto","text":"\u56e0\u4e3a element_declaration \u5e94\u8be5\u4e0e\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5c5e\u4e8e\u540c\u4e00\u7c7b\u578b\uff0c\u90a3\u4e48\u6700\u7406\u60f3\u7684\u65b9\u5f0f\u5c31\u662f\u4f7f\u7528 auto \u5173\u952e\u5b57\u58f0\u660e\u8be5\u53d8\u91cf\u5e76\u8ba9C++\u4e3a\u6211\u4eec\u81ea\u52a8\u63a8\u65ad\u5176\u7c7b\u578b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { constexpr int fibonacci []{ 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 }; for ( auto number : fibonacci ) // type is auto, so number has its type deduced from the fibonacci array { std :: cout << number << ' ' ; } std :: cout << '\\n' ; return 0 ; }","title":"For each \u5faa\u73af\u548c auto\u5173\u952e\u5b57"},{"location":"11-13-For-each-loops/#for-each_1","text":"\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c element \u88ab\u58f0\u660e\u4e3a\u4e00\u4e2a\u503c\u53d8\u91cf\uff1a 1 2 3 4 5 std :: string array []{ \"peter\" , \"likes\" , \"frozen\" , \"yogurt\" }; for ( auto element : array ) // element will be a copy of the current array element { std :: cout << element << ' ' ; } \u8fd9\u610f\u5473\u7740\u5728\u904d\u5386\u6570\u7ec4\u65f6\uff0c\u5f53\u524d\u5143\u7d20\u4f1a\u88ab\u62f7\u8d1d\u5230 element \u3002\u62f7\u8d1d\u6570\u7ec4\u5143\u7d20\u7684\u5f00\u9500\u4f1a\u5f88\u5927\uff0c\u800c\u4e14\u591a\u6570\u60c5\u51b5\u4e0b\u6211\u4eec\u5b9e\u9645\u4e0a\u60f3\u8981\u8bbf\u95ee\u539f\u672c\u7684\u5143\u7d20\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u58f0\u660e\u4e3a\u5f15\u7528\u7c7b\u578b\uff1a 1 2 3 4 5 std :: string array []{ \"peter\" , \"likes\" , \"frozen\" , \"yogurt\" }; for ( auto & element : array ) // The ampersand makes element a reference to the actual array element, preventing a copy from being made { std :: cout << element << ' ' ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c element \u662f\u5f53\u524d\u8fed\u4ee3\u6570\u7ec4\u5143\u7d20\u7684\u5f15\u7528\uff0c\u4ece\u800c\u907f\u514d\u4e86\u590d\u5236\u3002\u6b64\u5916\uff0c\u5bf9 element \u7684\u4efb\u4f55\u4fee\u6539\u90fd\u4f1a\u5f71\u54cd\u6b63\u5728\u8fed\u4ee3\u7684\u6570\u7ec4\uff0c\u5982\u679c element \u662f\u4e00\u4e2a\u666e\u901a\u53d8\u91cf\uff0c\u8fd9\u662f\u4e0d\u53ef\u80fd\u7684\u3002 \u5f53\u7136\uff0c\u5982\u679c\u4f60\u6253\u7b97\u4ee5\u53ea\u8bfb\u7684\u65b9\u5f0f\u4f7f\u7528\u5f15\u7528\uff0c\u5c06\u5b83\u8bbe\u4e3a const \u662f\u4e2a\u597d\u4e3b\u610f\uff1a 1 2 3 4 5 std :: string array []{ \"peter\" , \"likes\" , \"frozen\" , \"yogurt\" }; for ( const auto & element : array ) // element is a const reference to the currently iterated array element { std :: cout << element << ' ' ; } \u6700\u4f73\u5b9e\u8df5 \u5728 for-each \u5faa\u73af\u4e2d\u58f0\u660e\u5143\u7d20\u65f6\uff0c\u5982\u679c\u5143\u7d20\u662f\u975e\u57fa\u672c\u7c7b\u578b\uff0c\u5219\u5c06\u5176\u58f0\u660e\u4e3a\u5f15\u7528\u6216\u5e38\u6570\u5f15\u7528\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002","title":"For-each \u5faa\u73af\u548c\u5f15\u7528"},{"location":"11-13-For-each-loops/#for-each_2","text":"\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528 for-each \u91cd\u5199\u5f00\u5934\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { constexpr int scores []{ 84 , 92 , 76 , 81 , 56 }; int maxScore { 0 }; // keep track of our largest score for ( auto score : scores ) // iterate over array scores, assigning each value in turn to variable score { if ( score > maxScore ) { maxScore = score ; } } std :: cout << \"The best score was \" << maxScore << '\\n' ; return 0 ; } \u6ce8\u610f\uff0c\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u4e0d\u518d\u9700\u8981\u901a\u8fc7\u4e0b\u6807\u7d22\u5f15\u6570\u7ec4\u6216\u83b7\u53d6\u6570\u7ec4\u5927\u5c0f\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u53d8\u91cf score \u76f4\u63a5\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u3002\u6570\u7ec4\u5fc5\u987b\u6709\u5927\u5c0f\u4fe1\u606f\uff0c\u9000\u5316\u4e3a\u6307\u9488\u7684\u6570\u7ec4\u4e0d\u80fd\u5728for-each\u5faa\u73af\u4e2d\u4f7f\u7528\u3002","title":"\u4f7f\u7528 for-each \u5faa\u73af\u91cd\u5199\u6700\u9ad8\u5206\u6570\u7684\u4f8b\u5b50"},{"location":"11-13-For-each-loops/#for-each_3","text":"For-each \u5faa\u73af\u4e0d\u4ec5\u80fd\u914d\u5408\u56fa\u5b9a\u6570\u7ec4\u4e00\u8d77\u4f7f\u7528\uff0c\u5b83\u53ef\u4ee5\u548c\u591a\u79cd\u7c7b\u5217\u8868\u7684\u6570\u636e\u7ed3\u6784\u4e00\u8d77\u5de5\u4f5c\uff0c\u4f8b\u5982vector(\u4f8b\u5982 std::vector )\u3001\u94fe\u8868\u3001\u6811\u548c\u6620\u5c04 \u3002 \u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u8fd9\u4e9b\u5185\u5bb9\uff0c\u6240\u4ee5\u4f60\u4e0d\u77e5\u9053\u4e5f\u6ca1\u5173\u7cfb\u3002\u4f60\u73b0\u5728\u53ea\u9700\u8981\u8bb0\u4f4f\uff0cfor-each\u5faa\u73af\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u3001\u901a\u7528\u7684\u904d\u5386\u65b9\u6cd5\uff08\u4e0d\u4ec5\u4ec5\u662f\u904d\u5386\u6570\u7ec4\u800c\u5df2\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include int main () { std :: vector fibonacci { 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 }; // note use of std::vector here rather than a fixed array // Before C++17 // std::vector fibonacci{ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 }; for ( auto number : fibonacci ) { std :: cout << number << ' ' ; } std :: cout << '\\n' ; return 0 ; }","title":"For-each \u5faa\u73af\u548c\u975e\u6570\u7ec4\u53d8\u91cf"},{"location":"11-13-For-each-loops/#for-each_4","text":"\u4e3a\u4e86\u904d\u5386\u6570\u7ec4\uff0cfor-each\u9700\u8981\u77e5\u9053\u6570\u7ec4\u7684\u5927\u5c0f\u3002\u56e0\u4e3a\u5df2\u7ecf\u9000\u5316\u4e3a\u6307\u9488\u7684\u6570\u7ec4\u4e0d\u518d\u5305\u542b\u6570\u7ec4\u5927\u5c0f\u7684\u4fe1\u606f\uff0c\u56e0\u6b64for-each\u5faa\u73af\u5c06\u65e0\u6cd5\u914d\u5408\u5b83\u4eec\u4e00\u8d77\u5de5\u4f5c\uff01 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int sumArray ( const int array []) // array is a pointer { int sum { 0 }; for ( auto number : array ) // compile error, the size of array isn't known { sum += number ; } return sum ; } int main () { constexpr int array []{ 9 , 7 , 5 , 3 , 1 }; std :: cout << sumArray ( array ) << '\\n' ; // array decays into a pointer here return 0 ; } \u540c\u6837\uff0c\u7531\u4e8e\u540c\u6837\u7684\u539f\u56e0\uff0c\u52a8\u6001\u6570\u7ec4\u4e5f\u4e0d\u80fd\u7528\u4e8efor-each\u5faa\u73af\u3002","title":"For-each \u4e0d\u80fd\u548c\u6307\u5411\u6570\u7ec4\u7684\u6307\u9488\u4e00\u8d77\u5de5\u4f5c"},{"location":"11-13-For-each-loops/#_1","text":"For-each \u5faa\u73af\u4e0d\u80fd\u591f\u63d0\u4f9b\u8bbf\u95ee\u6570\u7ec4\u7d22\u5f15\u7684\u76f4\u63a5\u65b9\u6cd5\u3002\u8fd9\u662f\u56e0\u4e3a\u5f88\u591a\u914d\u5408for-each\u5faa\u73af\u5de5\u4f5c\u7684\u6570\u636e\u7ed3\u6784\uff08\u4f8b\u5982\u94fe\u8868\uff09\u5e76\u4e0d\u80fd\u591f\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5143\u7d20\u3002 \u4eceC++20\u5f00\u59cb\uff0c\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af\u53ef\u4ee5\u4e0e\u4e00\u4e2a\u521d\u59cb\u5316\u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u5c31\u50cf\u666e\u901afor\u5faa\u73af\u4e2d\u7684\u521d\u59cb\u5316\u8bed\u53e5\u4e00\u6837\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 init-statement \u6765\u521b\u5efa\u624b\u52a8\u7d22\u5f15\u8ba1\u6570\u5668\uff0c\u800c\u4e0d\u4f1a\u7834\u574ffor\u5faa\u73af\u6240\u5728\u7684\u51fd\u6570\u3002 init-statement \u5e94\u8be5\u653e\u7f6e\u5728\u5faa\u73af\u53d8\u91cf\u7684\u524d\u9762\uff1a 1 2 for ( init - statement ; element_declaration : array ) statement ; \u5728\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u6709\u4e24\u4e2a\u901a\u8fc7\u7d22\u5f15\u5173\u8054\u7684\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u540d\u5b57\u4e3a\u201c names[3] \u201d\u7684\u5b66\u751f\u7684\u5206\u6570\u4e3a\u201c scores[3] \u201d\u3002\u6bcf\u5f53\u53d1\u73b0\u4e00\u4e2a\u65b0\u7684\u9ad8\u5206\u5b66\u751f\u65f6\uff0c\u6211\u4eec\u5c31\u6253\u5370\u51fa\u4ed6\u4eec\u7684\u540d\u5b57\u548c\u4e0e\u524d\u4e00\u4e2a\u9ad8\u5206\u7684\u5206\u5dee\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include int main () { std :: string names []{ \"Alex\" , \"Betty\" , \"Caroline\" , \"Dave\" , \"Emily\" }; // Names of the students constexpr int scores []{ 84 , 92 , 76 , 81 , 56 }; int maxScore { 0 }; for ( int i { 0 }; auto score : scores ) // i is the index of the current element { if ( score > maxScore ) { std :: cout << names [ i ] << \" beat the previous best score of \" << maxScore << \" by \" << ( score - maxScore ) << \" points! \\n \" ; maxScore = score ; } ++ i ; } std :: cout << \"The best score was \" << maxScore << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 Alex beat the previous best score of 0 by 84 points! Betty beat the previous best score of 84 by 8 points! The best score was 92 \u8fd9\u91cc\u7684 int i{ 0 }; \u662f init-statement \uff0c\u5b83\u4f1a\u5728\u5faa\u73af\u6267\u884c\u65f6\u6267\u884c\u4e00\u6b21\u3002\u5728\u6bcf\u6b21\u5faa\u73af\u65f6\uff0c i \u5c31\u88ab\u9012\u589e1\uff0c\u7c7b\u4f3c\u4e0e\u666e\u901a\u7684\u5faa\u73af\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u6211\u4eec\u5728\u5faa\u73af\u4e2d\u4f7f\u7528 continue \u7684\u8bdd\uff0c ++i \u4f1a\u88ab\u8df3\u8fc7\uff0c\u5bfc\u81f4\u975e\u9884\u671f\u7684\u7ed3\u679c\u3002\u5982\u679c\u4f60\u9700\u8981\u4f7f\u7528 continue \u7684\u8bdd\uff0c\u8bf7\u786e\u4fdd i \u7684\u9012\u589e\u5728 continue \u4e4b\u524d\u8fdb\u884c\u3002 \u5728C++20\u4e4b\u524d\uff0c\u7d22\u5f15\u53d8\u91cf i \u5fc5\u987b\u5728\u5faa\u73af\u4e4b\u5916\u58f0\u660e\uff0c\u4f46\u8fd9\u53ef\u80fd\u5bfc\u81f4\u540d\u79f0\u51b2\u7a81\uff0c\u4f8b\u5982\u5f53\u6211\u4eec\u60f3\u5728\u540e\u9762\u7684\u51fd\u6570\u4e2d\u5b9a\u4e49\u53e6\u4e00\u4e2a\u540d\u4e3a i \u7684\u53d8\u91cf\u65f6\u3002","title":"\u80fd\u591f\u83b7\u53d6\u5f53\u524d\u5143\u7d20\u7684\u7d22\u5f15\u5417\uff1f"},{"location":"11-13-For-each-loops/#_2","text":"\u5f53\u9700\u8981\u6309\u6b63\u5411\u987a\u5e8f\u8bbf\u95ee\u6240\u6709\u6570\u7ec4\u5143\u7d20\u65f6\uff0cFor-each \u5faa\u73af\u4e3a\u904d\u5386\u6570\u7ec4\u63d0\u4f9b\u4e86\u4e00\u79cd\u9ad8\u7ea7\u8bed\u6cd5\u3002\u5728\u53ef\u4ee5\u4f7f\u7528\u5b83\u7684\u60c5\u51b5\u4e0b\uff0c\u5b83\u5e94\u8be5\u4f18\u4e8e\u6807\u51c6\u7684for\u5faa\u73af\u3002\u4e3a\u4e86\u9632\u6b62\u590d\u5236\u6bcf\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u5c06\u5143\u7d20\u58f0\u660e\u4e3a\u5f15\u7528\u3002","title":"\u7ed3\u8bba"},{"location":"11-14-Void-pointers/","tags":["void"],"text":"void \u6307\u9488 \uff0c\u4e5f\u88ab\u79f0\u4e4b\u4e3a\u6cdb\u578b\u6307\u9488\uff0c\u662f\u4e00\u79cd\u53ef\u4ee5\u6307\u5411\u4efb\u610f\u7c7b\u578b\u5bf9\u8c61\u7684\u6307\u9488\uff01void\u6307\u9488\u7684\u58f0\u660e\u7c7b\u4f3c\u4e8e\u666e\u901a\u6307\u9488\uff0c\u4f7f\u7528 void \u5173\u952e\u5b57\u6807\u8bc6\u6307\u9488\u7c7b\u578b\uff1a 1 void * ptr ; // ptr is a void pointer void \u6307\u9488\u53ef\u4ee5\u6307\u5411\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u7684\u5bf9\u8c61\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int nValue ; float fValue ; struct Something { int n ; float f ; }; Something sValue ; void * ptr ; ptr = & nValue ; // valid ptr = & fValue ; // valid ptr = & sValue ; // valid \u4e0d\u8fc7\uff0c\u7531\u4e8evoid\u6307\u9488\u5e76\u4e0d\u77e5\u9053\u5b83\u6240\u6307\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u6240\u4ee5\u5806void\u6307\u9488\u8fdb\u884c \u89e3\u5f15\u7528 \u662f\u975e\u6cd5\u64cd\u4f5c\u3002void \u6307\u9488\u5fc5\u987b\u9996\u5148\u88ab\u8f6c\u6362\u4e3a\u5176\u4ed6\u7279\u5b9a\u7c7b\u578b\u7684\u6307\u9488\uff0c\u7136\u540e\u624d\u80fd\u8fdb\u884c\u89e3\u5f15\u7528\u3002 1 2 3 4 5 6 7 8 int value { 5 }; void * voidPtr { & value }; // std::cout << *voidPtr << '\\n'; // illegal: dereference of void pointer int * intPtr { static_cast < int *> ( voidPtr ) }; // \u9996\u5148\u5c06void\u6307\u9488\u8f6c\u6362\u4e3aint\u6307\u9488 std :: cout << * intPtr << '\\n' ; // \u7136\u540e\u89e3\u5f15\u7528\u5f97\u5230\u7ed3\u679c \u8fd0\u884c\u7ed3\u679c\uff1a 1 5 \u63a5\u4e0b\u6765\u7684\u95ee\u9898\u662f\uff1a\u5982\u679c void \u6307\u9488\u4e0d\u77e5\u9053\u5b83\u6240\u6307\u7684\u5bf9\u8c61\u662f\u4ec0\u4e48\u7c7b\u578b\u7684\u8bdd\uff0c\u6211\u4eec\u53c8\u662f\u5982\u4f55\u5f97\u5e94\u8be5\u5c06\u5b83\u8f6c\u6362\u6210\u4ec0\u4e48\u7c7b\u578b\u5462\uff1f\u5b9e\u9645\u4e0a\uff0c\u4f60\u5fc5\u987b\u81ea\u5df1\u53bb\u8ddf\u8e2a\u8fd9\u4e2a\u7c7b\u578b\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 void \u6307\u9488\u7684\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include #include enum class Type { tInt , // note: we can't use \"int\" here because it's a keyword, so we'll use \"tInt\" instead tFloat , tCString }; void printValue ( void * ptr , Type type ) { switch ( type ) { case Type :: tInt : std :: cout << * static_cast < int *> ( ptr ) << '\\n' ; // cast to int pointer and perform indirection break ; case Type :: tFloat : std :: cout << * static_cast < float *> ( ptr ) << '\\n' ; // cast to float pointer and perform indirection break ; case Type :: tCString : std :: cout << static_cast < char *> ( ptr ) << '\\n' ; // cast to char pointer (no indirection) // std::cout will treat char* as a C-style string // if we were to perform indirection through the result, then we'd just print the single char that ptr is pointing to break ; default : assert ( false && \"type not found\" ); break ; } } int main () { int nValue { 5 }; float fValue { 7.5f }; char szValue []{ \"Mollie\" }; printValue ( & nValue , Type :: tInt ); printValue ( & fValue , Type :: tFloat ); printValue ( szValue , Type :: tCString ); return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 5 7.5 Mollie void \u6307\u9488\u5176\u4ed6\u7ec6\u8282 void \u6307\u9488\u53ef\u4ee5\u88ab\u8bbe\u7f6e\u4e3a null \u503c\uff1a 1 void * ptr { nullptr }; // ptr is a void pointer that is currently a null pointer \u5c3d\u7ba1\u4e00\u4e9b\u7f16\u8bd1\u5668\u5141\u8bb8\u5220\u9664\u6307\u5411\u52a8\u6001\u5206\u914d\u5185\u5b58\u7684\u7a7a\u6307\u9488\uff0c\u4f46\u5e94\u8be5\u907f\u514d\u8fd9\u6837\u505a\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e0d\u80fd\u5728void\u6307\u9488\u4e0a\u505a\u6307\u9488\u7b97\u672f\u8fd0\u7b97\u3002\u8fd9\u662f\u56e0\u4e3a\u6307\u9488\u7b97\u672f\u8981\u6c42\u6307\u9488\u77e5\u9053\u5b83\u6240\u6307\u5411\u7684\u5bf9\u8c61\u7684\u5927\u5c0f\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u9002\u5f53\u5730\u589e\u52a0\u6216\u51cf\u5c11\u6307\u9488\u3002 \u6ce8\u610f\uff0c\u6ca1\u6709void\u5f15\u7528\u8fd9\u79cd\u4e1c\u897f\u3002\u8fd9\u662f\u56e0\u4e3avoid\u5f15\u7528\u7684\u7c7b\u578b\u662fvoid &\uff0c\u5e76\u4e14\u4e0d\u77e5\u9053\u5b83\u5f15\u7528\u7684\u662f\u4ec0\u4e48\u7c7b\u578b\u7684\u503c\u3002 \u5c0f\u7ed3 \u4e00\u822c\u6765\u8bf4\uff0c\u9664\u975e\u7edd\u5bf9\u5fc5\u8981\uff0c\u5426\u5219\u5e94\u8be5\u907f\u514d\u4f7f\u7528void\u6307\u9488\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ef\u4ee5\u907f\u514d\u7c7b\u578b\u68c0\u67e5\uff0c\u8fd9\u4f7f\u5f97\u4f60\u5728\u4e0d\u7ecf\u610f\u95f4\u72af\u7684\u9519\u8bef\u65e0\u6cd5\u88ab\u7f16\u8bd1\u5668\u53d1\u73b0\u3002\u4f8b\u5982\uff1a 1 2 int nValue { 5 }; printValue ( & nValue , Type :: tCString ); \u4f46\u8c01\u77e5\u9053\u7ed3\u679c\u4f1a\u600e\u6837\u5462\uff01 \u5c3d\u7ba1\u4e0a\u9762\u7684\u51fd\u6570\u770b\u8d77\u6765\u50cf\u662f\u4e00\u79cd\u8ba9\u4e00\u4e2a\u51fd\u6570\u5904\u7406\u591a\u4e2a\u6570\u636e\u7c7b\u578b\u7684\u7b80\u6d01\u65b9\u6cd5\uff0c\u4f46C++\u5b9e\u9645\u4e0a\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u597d\u7684\u65b9\u6cd5(\u901a\u8fc7\u51fd\u6570\u91cd\u8f7d)\u6765\u505a\u540c\u6837\u7684\u4e8b\u60c5\uff0c\u8be5\u65b9\u6cd5\u4fdd\u7559\u4e86\u7c7b\u578b\u68c0\u67e5\uff0c\u4ee5\u907f\u514d\u6ee5\u7528\u3002\u8bb8\u591a\u5176\u4ed6\u5730\u65b9\u66fe\u7ecf\u4f7f\u7528void\u6307\u9488\u6765\u5904\u7406\u591a\u4e2a\u6570\u636e\u7c7b\u578b\uff0c\u73b0\u5728\u53ef\u4ee5\u4f7f\u7528\u6a21\u677f\u6765\u66f4\u597d\u5730\u5b8c\u6210\uff0c\u6a21\u677f\u4e5f\u63d0\u4f9b\u4e86\u5f3a\u7c7b\u578b\u68c0\u67e5\u3002 \u4f46\u662f\uff0c\u5728\u975e\u5e38\u5076\u7136\u7684\u60c5\u51b5\u4e0b\uff0cvoid\u6307\u9488\u4e5f\u662f\u5f88\u6709\u7528\u7684\u3002\u4f46\u662f\u5728\u4f7f\u7528\u524d\u8981\u9996\u5148\u786e\u4fdd\u6ca1\u6709\u66f4\u597d(\u66f4\u5b89\u5168)\u7684\u65b9\u6cd5\u6765\u4f7f\u7528\u5176\u4ed6\u8bed\u8a00\u673a\u5236\u5b8c\u6210\u540c\u6837\u7684\u4e8b\u60c5\uff01","title":"11.14 \u2014 void \u6307\u9488"},{"location":"11-14-Void-pointers/#void","text":"void \u6307\u9488\u53ef\u4ee5\u88ab\u8bbe\u7f6e\u4e3a null \u503c\uff1a 1 void * ptr { nullptr }; // ptr is a void pointer that is currently a null pointer \u5c3d\u7ba1\u4e00\u4e9b\u7f16\u8bd1\u5668\u5141\u8bb8\u5220\u9664\u6307\u5411\u52a8\u6001\u5206\u914d\u5185\u5b58\u7684\u7a7a\u6307\u9488\uff0c\u4f46\u5e94\u8be5\u907f\u514d\u8fd9\u6837\u505a\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e0d\u80fd\u5728void\u6307\u9488\u4e0a\u505a\u6307\u9488\u7b97\u672f\u8fd0\u7b97\u3002\u8fd9\u662f\u56e0\u4e3a\u6307\u9488\u7b97\u672f\u8981\u6c42\u6307\u9488\u77e5\u9053\u5b83\u6240\u6307\u5411\u7684\u5bf9\u8c61\u7684\u5927\u5c0f\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u9002\u5f53\u5730\u589e\u52a0\u6216\u51cf\u5c11\u6307\u9488\u3002 \u6ce8\u610f\uff0c\u6ca1\u6709void\u5f15\u7528\u8fd9\u79cd\u4e1c\u897f\u3002\u8fd9\u662f\u56e0\u4e3avoid\u5f15\u7528\u7684\u7c7b\u578b\u662fvoid &\uff0c\u5e76\u4e14\u4e0d\u77e5\u9053\u5b83\u5f15\u7528\u7684\u662f\u4ec0\u4e48\u7c7b\u578b\u7684\u503c\u3002","title":"void \u6307\u9488\u5176\u4ed6\u7ec6\u8282"},{"location":"11-14-Void-pointers/#_1","text":"\u4e00\u822c\u6765\u8bf4\uff0c\u9664\u975e\u7edd\u5bf9\u5fc5\u8981\uff0c\u5426\u5219\u5e94\u8be5\u907f\u514d\u4f7f\u7528void\u6307\u9488\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ef\u4ee5\u907f\u514d\u7c7b\u578b\u68c0\u67e5\uff0c\u8fd9\u4f7f\u5f97\u4f60\u5728\u4e0d\u7ecf\u610f\u95f4\u72af\u7684\u9519\u8bef\u65e0\u6cd5\u88ab\u7f16\u8bd1\u5668\u53d1\u73b0\u3002\u4f8b\u5982\uff1a 1 2 int nValue { 5 }; printValue ( & nValue , Type :: tCString ); \u4f46\u8c01\u77e5\u9053\u7ed3\u679c\u4f1a\u600e\u6837\u5462\uff01 \u5c3d\u7ba1\u4e0a\u9762\u7684\u51fd\u6570\u770b\u8d77\u6765\u50cf\u662f\u4e00\u79cd\u8ba9\u4e00\u4e2a\u51fd\u6570\u5904\u7406\u591a\u4e2a\u6570\u636e\u7c7b\u578b\u7684\u7b80\u6d01\u65b9\u6cd5\uff0c\u4f46C++\u5b9e\u9645\u4e0a\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u597d\u7684\u65b9\u6cd5(\u901a\u8fc7\u51fd\u6570\u91cd\u8f7d)\u6765\u505a\u540c\u6837\u7684\u4e8b\u60c5\uff0c\u8be5\u65b9\u6cd5\u4fdd\u7559\u4e86\u7c7b\u578b\u68c0\u67e5\uff0c\u4ee5\u907f\u514d\u6ee5\u7528\u3002\u8bb8\u591a\u5176\u4ed6\u5730\u65b9\u66fe\u7ecf\u4f7f\u7528void\u6307\u9488\u6765\u5904\u7406\u591a\u4e2a\u6570\u636e\u7c7b\u578b\uff0c\u73b0\u5728\u53ef\u4ee5\u4f7f\u7528\u6a21\u677f\u6765\u66f4\u597d\u5730\u5b8c\u6210\uff0c\u6a21\u677f\u4e5f\u63d0\u4f9b\u4e86\u5f3a\u7c7b\u578b\u68c0\u67e5\u3002 \u4f46\u662f\uff0c\u5728\u975e\u5e38\u5076\u7136\u7684\u60c5\u51b5\u4e0b\uff0cvoid\u6307\u9488\u4e5f\u662f\u5f88\u6709\u7528\u7684\u3002\u4f46\u662f\u5728\u4f7f\u7528\u524d\u8981\u9996\u5148\u786e\u4fdd\u6ca1\u6709\u66f4\u597d(\u66f4\u5b89\u5168)\u7684\u65b9\u6cd5\u6765\u4f7f\u7528\u5176\u4ed6\u8bed\u8a00\u673a\u5236\u5b8c\u6210\u540c\u6837\u7684\u4e8b\u60c5\uff01","title":"\u5c0f\u7ed3"},{"location":"11-15-Pointers-to-pointers-and-dynamic-multidimensional-arrays/","tags":["pointer"],"text":"Key Takeaway \u672c\u8bfe\u7a0b\u662f\u53ef\u9009\u7684\uff0c\u9002\u7528\u4e8e\u60f3\u8981\u4e86\u89e3\u66f4\u591aC++\u77e5\u8bc6\u7684\u9ad8\u7ea7\u8bfb\u8005\u3002\u672a\u6765\u7684\u8bfe\u7a0b\u5e76\u4e0d\u4f1a\u5efa\u7acb\u5728\u8fd9\u8282\u8bfe\u4e0a\u3002 \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u6b63\u662f\u4f60\u6240\u671f\u671b\u7684\u90a3\u6837\uff1a\u5b83\u662f\u4fdd\u5b58\u53e6\u4e00\u4e2a\u6307\u9488\u5730\u5740\u7684\u6307\u9488\u3002 \u6307\u5411\u6307\u9488\u7684\u6307\u9488 \u6307\u5411int\u578b\u7684\u666e\u901a\u6307\u9488\u4f7f\u7528\u5355\u4e2a\u661f\u53f7\u6765\u58f0\u660e\uff1a 1 int * ptr ; // pointer to an int, one asterisk \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u4f7f\u7528\u4e24\u4e2a\u661f\u53f7\u6765\u58f0\u660e\uff1a 1 int ** ptrptr ; // pointer to a pointer to an int, two asterisks \u4e00\u4e2a\u6307\u5411int\u578b\u7684\u666e\u901a\u6307\u9488\u4f7f\u7528\u4e00\u4e2a\u661f\u53f7\u58f0\u660e\uff1a\u6307\u5411\u6307\u9488\u7684\u6307\u9488\u548c\u666e\u901a\u6307\u9488\u7684\u5de5\u4f5c\u65b9\u5f0f\u4e00\u6837\u2014\u2014\u4f60\u53ef\u4ee5\u89e3\u5f15\u7528\u5b83\u6765\u68c0\u7d22\u6307\u5411\u7684\u503c\u3002\u56e0\u4e3a\u8be5\u503c\u672c\u8eab\u662f\u4e00\u4e2a\u6307\u9488\uff0c\u6240\u4ee5\u53ef\u4ee5\u518d\u6b21\u89e3\u9664\u5f15\u7528\u4ee5\u83b7\u5f97\u5e95\u5c42\u503c\u3002\u8fd9\u4e9b\u89e3\u5f15\u7528\u53ef\u4ee5\u8fde\u7eed\u6267\u884c: 1 2 3 4 5 6 7 int value { 5 }; int * ptr { & value }; std :: cout << * ptr << '\\n' ; // Dereference pointer to int to get int value int ** ptrptr { & ptr }; std :: cout << ** ptrptr << '\\n' ; // dereference to get pointer to int, dereference again to get int value \u4e0a\u8ff0\u7a0b\u5e8f\u6253\u5370\uff1a 1 2 5 5 \u6ce8\u610f\uff1a\u6307\u9488\u7684\u6307\u9488\u4e0d\u80fd\u76f4\u63a5\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u503c\uff1a 1 2 int value { 5 }; int ** ptrptr { && value }; // not valid \u8fd9\u662f\u56e0\u4e3a\u53d6\u5730\u5740\u64cd\u4f5c\u7b26\u9700\u8981\u4e00\u4e2a \u5de6\u503c \u4f5c\u4e3a\u64cd\u4f5c\u6570\uff0c\u4f46\u662f &value \u662f\u4e00\u4e2a \u53f3\u503c \u3002 \u4f46\u662f\uff0c\u5b83\u53ef\u4ee5\u88ab\u8bbe\u7f6e\u4e3a\u7a7a\u6307\u9488\uff1a 1 int ** ptrptr { nullptr }; \u6307\u9488\u6570\u7ec4 \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u6709\u4e00\u4e9b\u7528\u9014\u3002\u6700\u5e38\u89c1\u7684\u7528\u6cd5\u662f\u52a8\u6001\u5206\u914d\u6307\u9488\u6570\u7ec4\uff1a 1 int ** array { new int * [ 10 ] }; // allocate an array of 10 int pointers \u4e0a\u8ff0\u4ee3\u7801\u548c\u4e00\u822c\u7684\u52a8\u6001\u6570\u7ec4\u884c\u4e3a\u4e00\u81f4\uff0c\u53ea\u4e0d\u8fc7\u6570\u7ec4\u5143\u7d20\u662f\u201c\u6574\u578b\u6307\u9488\u201d\u800c\u4e0d\u662f\u6574\u578b\u7f62\u4e86\u3002 \u4e8c\u7ef4\u52a8\u6001\u6570\u7ec4 \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u7684\u53e6\u5916\u4e00\u4e2a\u5e38\u89c1\u7528\u9014\u662f\u5206\u914d\u591a\u7ef4\u6570\u7ec4(\u53c2\u89c1 11.5 - \u591a\u7ef4\u6570\u7ec4 )\u3002 \u4e8c\u7ef4\u56fa\u5b9a\u6570\u7ec4\u7684\u58f0\u660e\u975e\u5e38\u7b80\u5355\uff1a 1 int array [ 10 ][ 5 ]; \u4f46\u662f\u52a8\u6001\u6570\u7ec4\u5374\u7a0d\u5fae\u6709\u4e9b\u590d\u6742\u3002\u4f60\u53ef\u80fd\u4f1a\u5c1d\u8bd5\u8fd9\u6837\u505a\uff1a 1 int ** array { new int [ 10 ][ 5 ] }; // won\u2019t work! \u4f46\u8fd9\u6837\u505a\u662f\u9519\u8bef\u7684\u3002 \u53f3\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5b9e\u73b0\uff0c\u5982\u679c\u5217\u662f\u56fa\u5b9a\u7684\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 2 int x { 7 }; // non-constant int ( * array )[ 5 ] { new int [ x ][ 5 ] }; // rightmost dimension must be constexpr \u4e3a\u786e\u4fdd\u6b63\u786e\u7684\u4f18\u5148\u7ea7\uff0c\u8fd9\u91cc\u7684\u62ec\u53f7\u662f\u5fc5\u987b\u7684\u3002\u8fd9\u91cc\u7279\u522b\u9002\u5408\u4f7f\u7528 \u7c7b\u578b\u63a8\u65ad \uff1a 1 2 int x { 7 }; // non-constant auto array { new int [ x ][ 5 ] }; // so much simpler! \u4e0d\u5e78\u7684\u662f\uff0c\u5982\u679c\u6700\u53f3\u8fb9\u7684\u6570\u7ec4\u7ef4\u5ea6\u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\uff0c\u8fd9\u4e2a\u76f8\u5bf9\u7b80\u5355\u7684\u89e3\u51b3\u65b9\u6848\u5c31\u4e0d\u8d77\u4f5c\u7528\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u64cd\u4f5c\u5c31\u66f4\u590d\u6742\u4e86\u3002\u9996\u5148\uff0c\u6211\u4eec\u5206\u914d\u4e00\u4e2a\u6307\u9488\u6570\u7ec4(\u5982\u4e0a\u6240\u8ff0)\u3002\u7136\u540e\u904d\u5386\u6307\u9488\u6570\u7ec4\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u5206\u914d\u4e00\u4e2a\u52a8\u6001\u6570\u7ec4\u3002\u6211\u4eec\u7684\u52a8\u6001\u4e8c\u7ef4\u6570\u7ec4\u662f\u52a8\u6001\u4e00\u7ef4\u6570\u7ec4\u7684\u52a8\u6001\u4e00\u7ef4\u6570\u7ec4! 1 2 3 int ** array { new int * [ 10 ] }; // allocate an array of 10 int pointers \u2014 these are our rows for ( int count { 0 }; count < 10 ; ++ count ) array [ count ] = new int [ 5 ]; // these are our columns \u8bbf\u95ee\u6570\u7ec4\u7684\u65b9\u5f0f\u548c\u5e73\u5e38\u4e00\u6837\uff1a 1 array [ 9 ][ 4 ] = 3 ; // This is the same as (array[9])[4] = 3; \u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\uff0c\u7531\u4e8e\u6bcf\u4e2a\u6570\u7ec4\u5217\u90fd\u662f\u72ec\u7acb\u52a8\u6001\u5206\u914d\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u52a8\u6001\u5206\u914d\u975e\u77e9\u5f62\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u4e09\u89d2\u5f62\u6570\u7ec4\uff1a 1 2 3 int ** array { new int * [ 10 ] }; // allocate an array of 10 int pointers \u2014 these are our rows for ( int count { 0 }; count < 10 ; ++ count ) array [ count ] = new int [ count + 1 ]; // these are our columns COPY In the above example, note that array[0] is an array of length 1, array[1] is an array of length 2, etc\u2026 Deallocating a dynamically allocated two-dimensional array using this method requires a loop as well: 1 2 3 for ( int count { 0 }; count < 10 ; ++ count ) delete [] array [ count ]; delete [] array ; // this needs to be done last COPY Note that we delete the array in the opposite order that we created it (elements first, then the array itself). If we delete array before the array columns, then we\u2019d have to access deallocated memory to delete the array columns. And that would result in undefined behavior. Because allocating and deallocating two-dimensional arrays is complex and easy to mess up, it\u2019s often easier to \u201cflatten\u201d a two-dimensional array (of size x by y) into a one-dimensional array of size x * y : 1 2 3 4 5 6 7 // Instead of this: int ** array { new int * [ 10 ] }; // allocate an array of 10 int pointers \u2014 these are our rows for ( int count { 0 }; count < 10 ; ++ count ) array [ count ] = new int [ 5 ]; // these are our columns // Do this int * array { new int [ 50 ] }; // a 10x5 array flattened into a single array COPY Simple math can then be used to convert a row and column index for a rectangular two-dimensional array into a single index for a one-dimensional array: 1 2 3 4 5 6 7 int getSingleIndex ( int row , int col , int numberOfColumnsInArray ) { return ( row * numberOfColumnsInArray ) + col ; } // set array[9,4] to 3 using our flattened array array [ getSingleIndex ( 9 , 4 , 5 )] = 3 ; COPY \u901a\u8fc7\u5730\u5740\u4f20\u9012\u6307\u9488 \u5c31\u50cf\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\u5f62\u53c2\u6765\u66f4\u6539\u4f20\u5165\u7684\u5e95\u5c42\u53c2\u6570\u7684\u5b9e\u9645\u503c\u4e00\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6307\u9488\u4f20\u9012\u7ed9\u4e00\u4e2a\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\uff0c\u7136\u540e\u4f7f\u7528\u8be5\u6307\u9488\u6765\u66f4\u6539\u5b83\u6240\u6307\u5411\u7684\u6307\u9488\u7684\u503c(\u8fd8\u4e0d\u660e\u767d\u5417?) However, if we want a function to be able to modify what a pointer argument points to, this is generally better done using a reference to a pointer instead. This is covered in lesson 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 \u6307\u9488\u7684\u6307\u9488\u7684\u6307\u9488 \u58f0\u660e\u6307\u5411\u6307\u9488\u7684\u6307\u9488\u7684\u6307\u9488\u4e5f\u662f\u53ef\u4ee5\u7684\uff1a 1 int *** ptrx3 ; \u8fd9\u6837\u53ef\u4ee5\u52a8\u6001\u5206\u914d\u4e00\u4e2a\u4e09\u7ef4\u6570\u7ec4\u3002\u7136\u800c\uff0c\u8fd9\u6837\u505a\u5c06\u9700\u8981\u4e00\u4e2a\u5faa\u73af\u4e2d\u7684\u5faa\u73af\uff0c\u5e76\u4e14\u8981\u5f97\u5230\u6b63\u786e\u7684\u7ed3\u679c\u662f\u6781\u5176\u590d\u6742\u7684\u3002 \u4f60\u751a\u81f3\u53ef\u4ee5\u58f0\u660e\u4e00\u4e2a\u56db\u91cd\u6307\u9488\uff1a 1 int **** ptrx4 ; \u53ea\u8981\u4f60\u613f\u610f\uff0c\u66f4\u9ad8\u5c42\u7684\u6307\u9488\u4e5f\u662f\u53ef\u4ee5\u7684\u3002 \u4f46\u662f\uff0c\u73b0\u5b9e\u4e2d\u5e76\u4e0d\u4f1a\u51fa\u73b0\u8fd9\u6837\u7684\u5b9a\u4e49\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u4f1a\u4f7f\u7528\u8fd9\u4e48\u591a\u5c42\u6b21\u7684\u95f4\u63a5\u5173\u7cfb\u3002 \u5c0f\u7ed3 \u6211\u4eec\u5efa\u8bae\u907f\u514d\u4f7f\u7528\u6307\u5411\u6307\u9488\u7684\u6307\u9488\uff0c\u9664\u975e\u6ca1\u6709\u5176\u4ed6\u53ef\u7528\u7684\u9009\u9879\uff0c\u56e0\u4e3a\u5b83\u4eec\u4f7f\u7528\u8d77\u6765\u5f88\u590d\u6742\uff0c\u800c\u4e14\u6709\u6f5c\u5728\u7684\u5371\u9669\u3002\u7528\u666e\u901a\u6307\u9488\u89e3\u9664\u5bf9\u7a7a\u6307\u9488\u6216\u60ac\u6d6e\u6307\u9488\u7684\u5f15\u7528\u975e\u5e38\u7b80\u5355\u2014\u2014\u4f7f\u7528\u6307\u5411\u6307\u9488\u7684\u6307\u9488\u66f4\u52a0\u5bb9\u6613\uff0c\u56e0\u4e3a\u5fc5\u987b\u6267\u884c\u53cc\u91cd\u89e3\u9664\u5f15\u7528\u624d\u80fd\u5f97\u5230\u5e95\u5c42\u503c\uff01","title":"11.15 \u2014 \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u548c\u591a\u7ef4\u6570\u7ec4"},{"location":"11-15-Pointers-to-pointers-and-dynamic-multidimensional-arrays/#_1","text":"\u6307\u5411int\u578b\u7684\u666e\u901a\u6307\u9488\u4f7f\u7528\u5355\u4e2a\u661f\u53f7\u6765\u58f0\u660e\uff1a 1 int * ptr ; // pointer to an int, one asterisk \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u4f7f\u7528\u4e24\u4e2a\u661f\u53f7\u6765\u58f0\u660e\uff1a 1 int ** ptrptr ; // pointer to a pointer to an int, two asterisks \u4e00\u4e2a\u6307\u5411int\u578b\u7684\u666e\u901a\u6307\u9488\u4f7f\u7528\u4e00\u4e2a\u661f\u53f7\u58f0\u660e\uff1a\u6307\u5411\u6307\u9488\u7684\u6307\u9488\u548c\u666e\u901a\u6307\u9488\u7684\u5de5\u4f5c\u65b9\u5f0f\u4e00\u6837\u2014\u2014\u4f60\u53ef\u4ee5\u89e3\u5f15\u7528\u5b83\u6765\u68c0\u7d22\u6307\u5411\u7684\u503c\u3002\u56e0\u4e3a\u8be5\u503c\u672c\u8eab\u662f\u4e00\u4e2a\u6307\u9488\uff0c\u6240\u4ee5\u53ef\u4ee5\u518d\u6b21\u89e3\u9664\u5f15\u7528\u4ee5\u83b7\u5f97\u5e95\u5c42\u503c\u3002\u8fd9\u4e9b\u89e3\u5f15\u7528\u53ef\u4ee5\u8fde\u7eed\u6267\u884c: 1 2 3 4 5 6 7 int value { 5 }; int * ptr { & value }; std :: cout << * ptr << '\\n' ; // Dereference pointer to int to get int value int ** ptrptr { & ptr }; std :: cout << ** ptrptr << '\\n' ; // dereference to get pointer to int, dereference again to get int value \u4e0a\u8ff0\u7a0b\u5e8f\u6253\u5370\uff1a 1 2 5 5 \u6ce8\u610f\uff1a\u6307\u9488\u7684\u6307\u9488\u4e0d\u80fd\u76f4\u63a5\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u503c\uff1a 1 2 int value { 5 }; int ** ptrptr { && value }; // not valid \u8fd9\u662f\u56e0\u4e3a\u53d6\u5730\u5740\u64cd\u4f5c\u7b26\u9700\u8981\u4e00\u4e2a \u5de6\u503c \u4f5c\u4e3a\u64cd\u4f5c\u6570\uff0c\u4f46\u662f &value \u662f\u4e00\u4e2a \u53f3\u503c \u3002 \u4f46\u662f\uff0c\u5b83\u53ef\u4ee5\u88ab\u8bbe\u7f6e\u4e3a\u7a7a\u6307\u9488\uff1a 1 int ** ptrptr { nullptr };","title":"\u6307\u5411\u6307\u9488\u7684\u6307\u9488"},{"location":"11-15-Pointers-to-pointers-and-dynamic-multidimensional-arrays/#_2","text":"\u6307\u5411\u6307\u9488\u7684\u6307\u9488\u6709\u4e00\u4e9b\u7528\u9014\u3002\u6700\u5e38\u89c1\u7684\u7528\u6cd5\u662f\u52a8\u6001\u5206\u914d\u6307\u9488\u6570\u7ec4\uff1a 1 int ** array { new int * [ 10 ] }; // allocate an array of 10 int pointers \u4e0a\u8ff0\u4ee3\u7801\u548c\u4e00\u822c\u7684\u52a8\u6001\u6570\u7ec4\u884c\u4e3a\u4e00\u81f4\uff0c\u53ea\u4e0d\u8fc7\u6570\u7ec4\u5143\u7d20\u662f\u201c\u6574\u578b\u6307\u9488\u201d\u800c\u4e0d\u662f\u6574\u578b\u7f62\u4e86\u3002","title":"\u6307\u9488\u6570\u7ec4"},{"location":"11-15-Pointers-to-pointers-and-dynamic-multidimensional-arrays/#_3","text":"\u6307\u5411\u6307\u9488\u7684\u6307\u9488\u7684\u53e6\u5916\u4e00\u4e2a\u5e38\u89c1\u7528\u9014\u662f\u5206\u914d\u591a\u7ef4\u6570\u7ec4(\u53c2\u89c1 11.5 - \u591a\u7ef4\u6570\u7ec4 )\u3002 \u4e8c\u7ef4\u56fa\u5b9a\u6570\u7ec4\u7684\u58f0\u660e\u975e\u5e38\u7b80\u5355\uff1a 1 int array [ 10 ][ 5 ]; \u4f46\u662f\u52a8\u6001\u6570\u7ec4\u5374\u7a0d\u5fae\u6709\u4e9b\u590d\u6742\u3002\u4f60\u53ef\u80fd\u4f1a\u5c1d\u8bd5\u8fd9\u6837\u505a\uff1a 1 int ** array { new int [ 10 ][ 5 ] }; // won\u2019t work! \u4f46\u8fd9\u6837\u505a\u662f\u9519\u8bef\u7684\u3002 \u53f3\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5b9e\u73b0\uff0c\u5982\u679c\u5217\u662f\u56fa\u5b9a\u7684\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 2 int x { 7 }; // non-constant int ( * array )[ 5 ] { new int [ x ][ 5 ] }; // rightmost dimension must be constexpr \u4e3a\u786e\u4fdd\u6b63\u786e\u7684\u4f18\u5148\u7ea7\uff0c\u8fd9\u91cc\u7684\u62ec\u53f7\u662f\u5fc5\u987b\u7684\u3002\u8fd9\u91cc\u7279\u522b\u9002\u5408\u4f7f\u7528 \u7c7b\u578b\u63a8\u65ad \uff1a 1 2 int x { 7 }; // non-constant auto array { new int [ x ][ 5 ] }; // so much simpler! \u4e0d\u5e78\u7684\u662f\uff0c\u5982\u679c\u6700\u53f3\u8fb9\u7684\u6570\u7ec4\u7ef4\u5ea6\u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\uff0c\u8fd9\u4e2a\u76f8\u5bf9\u7b80\u5355\u7684\u89e3\u51b3\u65b9\u6848\u5c31\u4e0d\u8d77\u4f5c\u7528\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u64cd\u4f5c\u5c31\u66f4\u590d\u6742\u4e86\u3002\u9996\u5148\uff0c\u6211\u4eec\u5206\u914d\u4e00\u4e2a\u6307\u9488\u6570\u7ec4(\u5982\u4e0a\u6240\u8ff0)\u3002\u7136\u540e\u904d\u5386\u6307\u9488\u6570\u7ec4\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u5206\u914d\u4e00\u4e2a\u52a8\u6001\u6570\u7ec4\u3002\u6211\u4eec\u7684\u52a8\u6001\u4e8c\u7ef4\u6570\u7ec4\u662f\u52a8\u6001\u4e00\u7ef4\u6570\u7ec4\u7684\u52a8\u6001\u4e00\u7ef4\u6570\u7ec4! 1 2 3 int ** array { new int * [ 10 ] }; // allocate an array of 10 int pointers \u2014 these are our rows for ( int count { 0 }; count < 10 ; ++ count ) array [ count ] = new int [ 5 ]; // these are our columns \u8bbf\u95ee\u6570\u7ec4\u7684\u65b9\u5f0f\u548c\u5e73\u5e38\u4e00\u6837\uff1a 1 array [ 9 ][ 4 ] = 3 ; // This is the same as (array[9])[4] = 3; \u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\uff0c\u7531\u4e8e\u6bcf\u4e2a\u6570\u7ec4\u5217\u90fd\u662f\u72ec\u7acb\u52a8\u6001\u5206\u914d\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u52a8\u6001\u5206\u914d\u975e\u77e9\u5f62\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u4e09\u89d2\u5f62\u6570\u7ec4\uff1a 1 2 3 int ** array { new int * [ 10 ] }; // allocate an array of 10 int pointers \u2014 these are our rows for ( int count { 0 }; count < 10 ; ++ count ) array [ count ] = new int [ count + 1 ]; // these are our columns COPY In the above example, note that array[0] is an array of length 1, array[1] is an array of length 2, etc\u2026 Deallocating a dynamically allocated two-dimensional array using this method requires a loop as well: 1 2 3 for ( int count { 0 }; count < 10 ; ++ count ) delete [] array [ count ]; delete [] array ; // this needs to be done last COPY Note that we delete the array in the opposite order that we created it (elements first, then the array itself). If we delete array before the array columns, then we\u2019d have to access deallocated memory to delete the array columns. And that would result in undefined behavior. Because allocating and deallocating two-dimensional arrays is complex and easy to mess up, it\u2019s often easier to \u201cflatten\u201d a two-dimensional array (of size x by y) into a one-dimensional array of size x * y : 1 2 3 4 5 6 7 // Instead of this: int ** array { new int * [ 10 ] }; // allocate an array of 10 int pointers \u2014 these are our rows for ( int count { 0 }; count < 10 ; ++ count ) array [ count ] = new int [ 5 ]; // these are our columns // Do this int * array { new int [ 50 ] }; // a 10x5 array flattened into a single array COPY Simple math can then be used to convert a row and column index for a rectangular two-dimensional array into a single index for a one-dimensional array: 1 2 3 4 5 6 7 int getSingleIndex ( int row , int col , int numberOfColumnsInArray ) { return ( row * numberOfColumnsInArray ) + col ; } // set array[9,4] to 3 using our flattened array array [ getSingleIndex ( 9 , 4 , 5 )] = 3 ; COPY","title":"\u4e8c\u7ef4\u52a8\u6001\u6570\u7ec4"},{"location":"11-15-Pointers-to-pointers-and-dynamic-multidimensional-arrays/#_4","text":"\u5c31\u50cf\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\u5f62\u53c2\u6765\u66f4\u6539\u4f20\u5165\u7684\u5e95\u5c42\u53c2\u6570\u7684\u5b9e\u9645\u503c\u4e00\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6307\u9488\u4f20\u9012\u7ed9\u4e00\u4e2a\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\uff0c\u7136\u540e\u4f7f\u7528\u8be5\u6307\u9488\u6765\u66f4\u6539\u5b83\u6240\u6307\u5411\u7684\u6307\u9488\u7684\u503c(\u8fd8\u4e0d\u660e\u767d\u5417?) However, if we want a function to be able to modify what a pointer argument points to, this is generally better done using a reference to a pointer instead. This is covered in lesson 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2","title":"\u901a\u8fc7\u5730\u5740\u4f20\u9012\u6307\u9488"},{"location":"11-15-Pointers-to-pointers-and-dynamic-multidimensional-arrays/#_5","text":"\u58f0\u660e\u6307\u5411\u6307\u9488\u7684\u6307\u9488\u7684\u6307\u9488\u4e5f\u662f\u53ef\u4ee5\u7684\uff1a 1 int *** ptrx3 ; \u8fd9\u6837\u53ef\u4ee5\u52a8\u6001\u5206\u914d\u4e00\u4e2a\u4e09\u7ef4\u6570\u7ec4\u3002\u7136\u800c\uff0c\u8fd9\u6837\u505a\u5c06\u9700\u8981\u4e00\u4e2a\u5faa\u73af\u4e2d\u7684\u5faa\u73af\uff0c\u5e76\u4e14\u8981\u5f97\u5230\u6b63\u786e\u7684\u7ed3\u679c\u662f\u6781\u5176\u590d\u6742\u7684\u3002 \u4f60\u751a\u81f3\u53ef\u4ee5\u58f0\u660e\u4e00\u4e2a\u56db\u91cd\u6307\u9488\uff1a 1 int **** ptrx4 ; \u53ea\u8981\u4f60\u613f\u610f\uff0c\u66f4\u9ad8\u5c42\u7684\u6307\u9488\u4e5f\u662f\u53ef\u4ee5\u7684\u3002 \u4f46\u662f\uff0c\u73b0\u5b9e\u4e2d\u5e76\u4e0d\u4f1a\u51fa\u73b0\u8fd9\u6837\u7684\u5b9a\u4e49\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u4f1a\u4f7f\u7528\u8fd9\u4e48\u591a\u5c42\u6b21\u7684\u95f4\u63a5\u5173\u7cfb\u3002","title":"\u6307\u9488\u7684\u6307\u9488\u7684\u6307\u9488"},{"location":"11-15-Pointers-to-pointers-and-dynamic-multidimensional-arrays/#_6","text":"\u6211\u4eec\u5efa\u8bae\u907f\u514d\u4f7f\u7528\u6307\u5411\u6307\u9488\u7684\u6307\u9488\uff0c\u9664\u975e\u6ca1\u6709\u5176\u4ed6\u53ef\u7528\u7684\u9009\u9879\uff0c\u56e0\u4e3a\u5b83\u4eec\u4f7f\u7528\u8d77\u6765\u5f88\u590d\u6742\uff0c\u800c\u4e14\u6709\u6f5c\u5728\u7684\u5371\u9669\u3002\u7528\u666e\u901a\u6307\u9488\u89e3\u9664\u5bf9\u7a7a\u6307\u9488\u6216\u60ac\u6d6e\u6307\u9488\u7684\u5f15\u7528\u975e\u5e38\u7b80\u5355\u2014\u2014\u4f7f\u7528\u6307\u5411\u6307\u9488\u7684\u6307\u9488\u66f4\u52a0\u5bb9\u6613\uff0c\u56e0\u4e3a\u5fc5\u987b\u6267\u884c\u53cc\u91cd\u89e3\u9664\u5f15\u7528\u624d\u80fd\u5f97\u5230\u5e95\u5c42\u503c\uff01","title":"\u5c0f\u7ed3"},{"location":"11-16-An-introduction-to-std-array/","tags":["std::array","C++17"],"text":"Key Takeaway - \u5728\u4e0a\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86 \u56fa\u5b9a\u6570\u7ec4 \u548c \u52a8\u6001\u6570\u7ec4 \u3002\u5c3d\u7ba1\u8fd9\u4e24\u79cd\u6570\u7ec4\u90fd\u662fC++\u81ea\u5e26\u7684\uff0c\u4f46\u662f\u5b83\u4eec\u7f3a\u70b9\u53ef\u4e0d\u5c11\uff1a\u56fa\u5b9a\u6570\u7ec4\u4f1a\u9000\u5316\u4e3a\u6307\u9488\uff0c\u9000\u5316\u548c\u5c06\u4f1a\u4e22\u5931\u6570\u7ec4\u7684\u957f\u5ea6\u4fe1\u606f\u3002\u52a8\u6001\u6570\u7ec4\u5728\u91ca\u653e\u65f6\u5bb9\u6613\u51fa\u73b0\u95ee\u9898\uff0c\u800c\u4e14\u5728\u91cd\u65b0\u8c03\u6574\u5927\u5c0f\u65f6\u4e5f\u5f88\u96be\u505a\u5230\u4e0d\u51fa\u9519\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\uff0cC++\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86 std::array \u548c std::vector \u4e24\u4e2a\u5bb9\u5668\u7c7b\uff0c\u4f7f\u5f97\u6570\u7ec4\u7ba1\u7406\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u4e86\u3002\u6211\u4eec\u4f1a\u5728\u672c\u8282\u8bfe\u4e2d\u4ecb\u7ecd std::array \uff0c\u7136\u540e\u5728\u4e0b\u8282\u8bfe\u4e2d\u4ecb\u7ecd std::vector \u3002 std::array std::array \u5b9e\u73b0\u4e86\u56fa\u5b9a\u6570\u7ec4\u7684\u529f\u80fd\uff0c\u4e14\u5728\u4f20\u5165\u51fd\u6570\u65f6\u4e0d\u4f1a\u9000\u5316\u4e3a\u6307\u9488\u3002 std::array \u88ab\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d\u4e14\u4f4d\u4e8e std \u547d\u540d\u7a7a\u95f4 \u5185\u3002 \u58f0\u660e std::array \u53d8\u91cf\u5f88\u7b80\u5355\uff1a 1 2 3 #include std :: array < int , 3 > myArray ; // \u58f0\u660e\u4e00\u4e2a\u957f\u5ea6\u4e3a3\u7684\u6574\u578b\u6570\u7ec4 \u548c\u539f\u751f\u7684\u56fa\u5b9a\u6570\u7ec4\u5b9e\u73b0\u7c7b\u4f3c\uff0c std::array \u7684\u957f\u5ea6\u5fc5\u987b\u5728 \u7f16\u8bd1\u65f6 \u786e\u5b9a\u3002 std::array \u53ef\u4ee5\u901a\u8fc7 \u521d\u59cb\u5316\u503c\u5217\u8868 \u6216 \u5217\u8868\u521d\u59cb\u5316 \u7684\u65b9\u5f0f\u8fdb\u884c\u521d\u59cb\u5316\uff1a 1 2 std :: array < int , 5 > myArray = { 9 , 7 , 5 , 3 , 1 }; // \u521d\u59cb\u5316\u503c\u5217\u8868 std :: array < int , 5 > myArray2 { 9 , 7 , 5 , 3 , 1 }; // \u5217\u8868\u521d\u59cb\u5316 \u548cC++\u81ea\u5e26\u7684\u56fa\u5b9a\u6570\u7ec4\u4e0d\u540c\u7684\u662f\uff0c\u5728\u4f7f\u7528 std::array \u7684\u65f6\u5019\uff0c\u4f60\u4e0d\u80fd\u7701\u7565\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u5373\u4f7f\u63d0\u4f9b\u4e86\u521d\u59cb\u5316\u503c\u4e5f\u4e0d\u884c\uff1a 1 2 std :: array < int , > myArray { 9 , 7 , 5 , 3 , 1 }; // \u4e0d\u5408\u6cd5\uff0c\u5fc5\u987b\u63d0\u4f9b\u957f\u5ea6 std :: array < int > myArray { 9 , 7 , 5 , 3 , 1 }; // \u4e0d\u5408\u6cd5\uff0c\u5fc5\u987b\u63d0\u4f9b\u957f\u5ea6 \u4e0d\u8fc7\uff0c\u4eceC++17\u5f00\u59cb\uff0c std::array \u4e5f\u652f\u6301\u7701\u7565\u7c7b\u578b\u548c\u5927\u5c0f\u4e86\u3002\u5982\u679c\u6570\u7ec4\u88ab\u663e\u5f0f\u5730\u521d\u59cb\u5316\uff0c\u5219\u7c7b\u578b\u548c\u5927\u5c0f\u53ef\u4ee5\u4e00\u8d77\u88ab\u5ffd\u7565\u3002 1 2 std :: array myArray { 9 , 7 , 5 , 3 , 1 }; // \u901a\u8fc7\u7c7b\u578b\u63a8\u65ad\u5f97\u5230 std::array std :: array myArray { 9.7 , 7.31 }; // \u901a\u8fc7\u7c7b\u578b\u63a8\u65ad\u5f97\u5230 std::array \u6211\u4eec\u63a8\u8350\u4f7f\u7528\u4e0a\u9762\u8fd9\u79cd\u5ffd\u7565\u7c7b\u578b\u548c\u5927\u5c0f\u7684\u58f0\u660e\u65b9\u5f0f\uff0c\u9664\u975e\u4f60\u7684\u7f16\u8bd1\u5668\u4e0d\u652f\u6301C++17\uff0c\u90a3\u4e48\u4f60\u5c31\u5fc5\u987b\u663e\u5f0f\u5730\u6307\u5b9a\u4e86\u7c7b\u578b\u548c\u5927\u5c0f\u3002 1 2 3 4 5 // std::array myArray { 9, 7, 5, 3, 1 }; // Since C++17 std :: array < int , 5 > myArray { 9 , 7 , 5 , 3 , 1 }; // Before C++17 // std::array myArray { 9.7, 7.31 }; // Since C++17 std :: array < double , 2 > myArray { 9.7 , 7.31 }; // Before C++17 \u5728 C++20 \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u5143\u7d20\u7c7b\u578b\uff0c\u4f46\u662f\u5ffd\u7565\u6570\u7ec4\u957f\u5ea6\u3002\u8fd9\u4e48\u505a\u7684\u65f6\u5019\uff0c\u521b\u5efa std::array \u65f6\u770b\u8d77\u6765\u66f4\u50cf\u662f\u521b\u5efa\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u6570\u7ec4\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 std::to_array \u6765\u521b\u5efa\u6570\u7ec4\uff08\u6307\u5b9a\u5143\u7d20\u7c7b\u578b\u4f46\u81ea\u52a8\u63a8\u65ad\u957f\u5ea6\uff09\uff1a 1 2 3 auto myArray1 { std :: to_array < int , 5 > ({ 9 , 7 , 5 , 3 , 1 }) }; // Specify type and size auto myArray2 { std :: to_array < int > ({ 9 , 7 , 5 , 3 , 1 }) }; // Specify type only, deduce size auto myArray3 { std :: to_array ({ 9 , 7 , 5 , 3 , 1 }) }; // Deduce type and size \u4e0d\u5e78\u7684\u662f\uff0c\u4f7f\u7528 std::to_array \u521b\u5efa\u6570\u7ec4\u65f6\u7684\u5f00\u9500\u6bd4\u76f4\u63a5\u4f7f\u7528 std::array \u521b\u5efa\u6570\u7ec4\u5f00\u9500\u66f4\u5927\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u5c06\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u6570\u7ec4\u7684\u5143\u7d20\u62f7\u8d1d\u5230 std::array \u3002\u51fa\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u5982\u679c\u9700\u8981\u5728\u5faa\u73af\u4e2d\u591a\u6b21\u521b\u5efa\u6570\u7ec4\u65f6\uff0c\u5e94\u8be5\u907f\u514d\u4f7f\u7528 std::to_array \u3002 \u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 \u521d\u59cb\u5316\u503c\u5217\u8868 \u4e3a\u6570\u7ec4\u8d4b\u503c\u3002 1 2 3 4 std :: array < int , 5 > myArray ; myArray = { 0 , 1 , 2 , 3 , 4 }; // okay myArray = { 9 , 8 , 7 }; // okay, elements 3 and 4 are set to zero! myArray = { 0 , 1 , 2 , 3 , 4 , 5 }; // not allowed, too many elements in initializer list! \u6076\u610f\u4f7f\u7528\u4e0b\u6807\u8fd0\u7b97\u7b26\u8bbf\u95ee std::array \u6570\u7ec4\u4e2d\u7684\u503c\uff1a 1 2 std :: cout << myArray [ 1 ] << '\\n' ; myArray [ 2 ] = 6 ; \u548c\u5185\u7f6e\u7684\u6570\u7ec4\u7c7b\u4f3c\uff0c\u4e0b\u6807\u8fd0\u7b97\u7b26\u5e76\u6ca1\u6709\u5b9e\u73b0\u8d8a\u754c\u68c0\u67e5\u3002\u5f53\u4f20\u5165\u975e\u6cd5\u7d22\u5f15\u65f6\uff0c\u4f1a\u53d1\u751f\u5f88\u4e0d\u597d\u7684\u4e8b\u60c5\u3002 std::array \u8fd8\u652f\u6301\u53e6\u5916\u4e00\u79cd\u5143\u7d20\u8bbf\u95ee\u65b9\u5f0f\uff08\u4f7f\u7528 at() \u51fd\u6570\uff09\uff0c\u5b83\u4f1a\u5728\uff08 \u8fd0\u884c\u65f6 \uff09\u8fdb\u884c\u8d8a\u754c\u68c0\u67e5\uff1a 1 2 3 std :: array myArray { 9 , 7 , 5 , 3 , 1 }; myArray . at ( 1 ) = 6 ; // array element 1 is valid, sets array element 1 to value 6 myArray . at ( 9 ) = 10 ; // array element 9 is invalid, will throw a runtime error \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u8c03\u7528 myArray.at(1) \u65f6\u4f1a\u786e\u4fdd\u6240\u4ee51\u662f\u5408\u6cd5\u7684\u3002\u7531\u4e8e1\u786e\u5b9e\u662f\u5408\u6cd5\u7d22\u5f15\uff0c\u6240\u4ee5\u5b83\u4f1a\u8fd4\u56de\u5bf9\u5e94\u7684\u5143\u7d20\u7684\u5f15\u7528\u3002\u6211\u4eec\u5c066\u8d4b\u503c\u7ed9\u8be5\u5f15\u7528\u3002\u4e0d\u8fc7\uff0c\u5f53\u6211\u4eec\u8c03\u7528 myArray.at(9) \u65f6\u5c31\u4f1a\u5931\u8d25\uff0c\u56e0\u4e3a\u7d22\u5f159\u8d8a\u754c\u4e86\u3002\u5f53\u8d8a\u754c\u53d1\u751f\u65f6\uff0c at() \u51fd\u6570\u4f1a\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\uff08\u5177\u4f53\u6765\u8bf4\u5b83\u4f1a\u629b\u51fa\u4e00\u4e2a std::out_of_range \u5f02\u5e38\uff09\uff0c\u800c\u4e0d\u4f1a\u8fd4\u56de\u5bf9\u5e94\u7684\u7d22\u5f15\u3002\u56e0\u4e3a\u9700\u8981\u8fdb\u884c\u8d8a\u754c\u68c0\u67e5\uff0c\u6240\u4ee5 at() \u7684\u901f\u5ea6\u4f1a\u6bd4\u4e0b\u6807\u8fd0\u7b97\u7b26\u6162\uff0c\u4f46\u5b83\u66f4\u5b89\u5168\u3002 std::array \u4f1a\u5728 \u79bb\u5f00\u4f5c\u7528\u57df \u65f6\u6e05\u7406\u81ea\u5df1\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u8fdb\u884c\u624b\u52a8\u6e05\u7406\u3002 \u6570\u7ec4\u5927\u5c0f\u548c\u6392\u5e8f size() \u51fd\u6570\u53ef\u4ee5\u7528\u6765\u83b7\u53d6 std::array \u7684\u957f\u5ea6\uff1a 1 2 std :: array myArray { 9.0 , 7.2 , 5.4 , 3.6 , 1.8 }; std :: cout << \"length: \" << myArray . size () << '\\n' ; \u7a0b\u5e8f\u6253\u5370\uff1a 1 length: 5 \u56e0\u4e3a std::array \u4e0d\u4f1a\u5728\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\u9000\u5316\u4e3a\u6307\u9488\uff0c\u6240\u4ee5 size() \u5728\u51fd\u6570\u5185\u90e8\u4e5f\u53ef\u4ee5\u8c03\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include void printLength ( const std :: array < double , 5 >& myArray ) { std :: cout << \"length: \" << myArray . size () << '\\n' ; } int main () { std :: array myArray { 9.0 , 7.2 , 5.4 , 3.6 , 1.8 }; printLength ( myArray ); return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 length: 5 \u6ce8\u610f\uff0c\u5728\u6807\u51c6\u5e93\u4e2d size \u4e00\u8bcd\u8868\u793a\u6570\u7ec4\u7684\u957f\u5ea6\u2014\u2014\u4e0d\u8981\u628a\u5b83\u548c sizeof() \u641e\u6df7\u4e86\uff0c\u5f53\u5bf9\u539f\u751f\u7684\u56fa\u5b9a\u6570\u7ec4\u4f7f\u7528 sizeof \u65f6\uff0c\u8fd4\u56de\u7684\u662f\u8be5\u6570\u7ec4\u5360\u7528\u7684\u5b9e\u9645\u5185\u5b58\u7684\u5927\u5c0f\uff08\u5143\u7d20\u5927\u5c0f\u4e58\u4ee5\u6570\u7ec4\u957f\u5ea6\uff09\u3002\u662f\u7684\uff0c\u8fd9\u8fb9\u547d\u540d\u7684\u4e00\u81f4\u6027\u4fdd\u6301\u7684\u4e0d\u597d\u3002 \u8fd8\u8981\u6ce8\u610f\uff0c\u5f53 \u6309\u5f15\u7528\u4f20\u9012 std::array \u65f6\u3002\u53ef\u4ee5\u9632\u6b62\u7f16\u8bd1\u5668\u521b\u5efa std::array \u7684\u62f7\u8d1d\uff08\u6709\u52a9\u4e8e\u63d0\u5347\u6027\u80fd\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u6309\u5f15\u7528\u4f20\u9012\u6216\u6309const\u5f15\u7528\u4f20\u9012\u7684\u65b9\u5f0f\u5c06 std::array \u4f20\u5165\u51fd\u6570\u3002 \u56e0\u4e3a std::array \u7684\u957f\u5ea6\u603b\u662f\u5df2\u77e5\u7684\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u4f7f\u7528 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u6765\u904d\u5386\uff1a 1 2 3 4 std :: array myArray { 9 , 7 , 5 , 3 , 1 }; for ( int element : myArray ) std :: cout << element << ' ' ; \u4f7f\u7528 std::sort \u53ef\u4ee5\u5bf9 std::array \u8fdb\u884c\u6392\u5e8f\uff0c\u8be5\u51fd\u6570\u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // for std::sort #include #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; std :: sort ( myArray . begin (), myArray . end ()); // \u4ece\u524d\u5411\u540e\u6392\u5e8f // std::sort(myArray.rbegin(), myArray.rend()); // \u4ece\u540e\u5411\u524d\u6392\u5e8f for ( int element : myArray ) std :: cout << element << ' ' ; std :: cout << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 1 3 5 7 9 \u5411\u51fd\u6570\u4f20\u9012\u4e0d\u540c\u957f\u5ea6\u7684 std::array \u5bf9\u4e8e std::array \u6765\u8bf4\uff0c\u5143\u7d20\u7684\u7c7b\u578b\u548c\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u6570\u7ec4\u7c7b\u578b\u7684\u4e00\u90e8\u5206\u3002\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u4f7f\u7528 std::array \u4f5c\u4e3a\u51fd\u6570 \u5f62\u53c2 \u65f6\uff0c\u5fc5\u987b\u6307\u660e\u5143\u7d20\u7c7b\u578b\u548c\u6570\u7ec4\u957f\u5ea6 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include void printArray ( const std :: array < int , 5 >& myArray ) { for ( auto element : myArray ) std :: cout << element << ' ' ; std :: cout << '\\n' ; } int main () { std :: array myArray5 { 9.0 , 7.2 , 5.4 , 3.6 , 1.8 }; // type deduced as std::array printArray ( myArray5 ); // error: printArray expects a std::array return 0 ; } \u8fd9\u4e48\u505a\u7684\u5f0a\u7aef\u5728\u4e8e\uff0c\u8be5\u51fd\u6570\u4fbf\u53ea\u80fd\u4f7f\u7528\u7279\u5b9a\u5143\u7d20\u7c7b\u578b\u548c\u6570\u7ec4\u957f\u5ea6\u7684\u6570\u7ec4\u4f5c\u4e3a \u5b9e\u53c2 \uff0c\u4f46\u662f\u5982\u679c\u6211\u4eec\u5e0c\u671b\u51fd\u6570\u80fd\u591f\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u5143\u7d20\u4e14\u957f\u5ea6\u4e0d\u540c\u7684\u6570\u7ec4\u65f6\uff0c\u5e94\u8be5\u600e\u4e48\u505a\u5462\uff1f\u96be\u9053\u8981\u521b\u5efa\u591a\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\u6765\u5904\u7406\u5417\uff1f\u8fd9\u663e\u7136\u4f1a\u5e26\u6765\u5f88\u591a\u5f88\u591a\u7684\u5197\u4f59\u4ee3\u7801\u3002 \u5e78\u8fd0\u5730\u662f\uff0cC++\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u6a21\u677f\u6765\u5b9e\u73b0\u4e0a\u8ff0\u529f\u80fd\u3002 \u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6a21\u677f\u5316\u51fd\u6570\u5e76\u5c06\u6240\u6709\u7684\u7c7b\u578b\u4fe1\u606f\u53c2\u6570\u5316\uff0cC++\u4f1a\u6839\u636e\u6a21\u677f\u5728\u9700\u8981\u65f6\u81ea\u52a8\u521b\u5efa\u201c\u771f\u5b9e\u201d\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include #include // printArray is a template function template < typename T , std :: size_t size > // parameterize the element type and size void printArray ( const std :: array < T , size >& myArray ) { for ( auto element : myArray ) std :: cout << element << ' ' ; std :: cout << '\\n' ; } int main () { std :: array myArray5 { 9.0 , 7.2 , 5.4 , 3.6 , 1.8 }; printArray ( myArray5 ); std :: array myArray7 { 9.0 , 7.2 , 5.4 , 3.6 , 1.8 , 1.2 , 0.7 }; printArray ( myArray7 ); return 0 ; } \u76f8\u5173\u5185\u5bb9 \u51fd\u6570\u6a21\u677f\u4f1a\u5728 8.13 - \u51fd\u6570\u6a21\u677f \u4e2d\u4ecb\u7ecd\u3002 \u57fa\u4e8e size_type \u5bf9 std::array \u8fdb\u884c\u4eba\u5de5\u7d22\u5f15 \u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\uff1a\u4e0b\u9762\u7684\u4ee3\u7801\u6709\u4ec0\u4e48\u95ee\u9898\uff1f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; // Iterate through the array and print the value of the elements for ( int i { 0 }; i < myArray . size (); ++ i ) std :: cout << myArray [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u8fd9\u4e2a\u95ee\u9898\u7684\u7b54\u6848\u662f\uff1a\u4ee3\u7801\u4e2d\u53ef\u80fd\u4f1a\u51fa\u73b0 signed/unsigned \u4e0d\u5339\u914d\u7684\u95ee\u9898\u3002 size() \u51fd\u6570\u548c\u4e0b\u6807\u7d22\u5f15\u4f7f\u7528\u4e86\u4e00\u79cd\u88ab\u79f0\u4e3a size_type \u7684\u7c7b\u578b\uff0c\u8be5\u7c7b\u578b\u5728C++\u6807\u51c6\u4e2d\u88ab\u5b9a\u4e49\u4e3a\u65e0\u7b26\u53f7\u6574\u578b\u7c7b\u578b\u3002\u800c\u5728\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c\u5faa\u73af\u7d22\u5f15 i \u662f\u4e00\u4e2a\u6709\u7b26\u53f7\u6574\u578b\u3002\u56e0\u6b64\u5728\u8fdb\u884c i < myArray.size() \u6bd4\u8f83\u65f6\u4f1a\u51fa\u73b0\u7c7b\u578b\u4e0d\u5339\u914d\u7684\u95ee\u9898\u3002 \u6709\u8da3\u7684\u662f\uff0c size_type \u8fd8\u4e0d\u662f\u4e00\u4e2a\u5168\u5c40\u7c7b\u578b\uff08\u4f8b\u5982\uff0c int \u6216 std::size_t \uff09\uff0c\u5b83\u5b9e\u9645\u4e0a\u88ab\u5b9a\u4e49\u5728 std::array \u5185\u90e8 (C++\u5141\u8bb8\u5d4c\u5957\u5b9a\u4e49)\u3002\u8fd9\u5c31\u610f\u5473\u7740\uff0c\u5f53\u4f60\u9700\u8981\u4f7f\u7528 size_type \u65f6\uff0c\u5fc5\u987b\u6dfb\u52a0\u5b8c\u6574\u7684\u524d\u7f00(\u53ef\u4ee5\u5c06 std::array \u770b\u505a\u662f\u5b83\u7684\u547d\u540d\u7a7a\u95f4)\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c \u201csize_type\u201d \u7684\u5b8c\u6574\u524d\u7f00\u662f std::array::size_type ! \u6240\u4ee5\uff0c\u4e0a\u9762\u4ee3\u7801\u7684\u6b63\u786e\u5199\u6cd5\u5e94\u8be5\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; // std::array::size_type is the return type of size()! for ( std :: array < int , 5 >:: size_type i { 0 }; i < myArray . size (); ++ i ) std :: cout << myArray [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u53ef\u8bfb\u6027\u5e76\u4e0d\u597d\u3002\u5e78\u8fd0\u5730\u662f\uff0c std::array::size_type \u5b9e\u9645\u4e0a\u662f std::size_t \u7684\u522b\u540d\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 std::size_t \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include // std::size_t #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; for ( std :: size_t i { 0 }; i < myArray . size (); ++ i ) std :: cout << myArray [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u66f4\u597d\u7684\u529e\u6cd5\u662f==\u907f\u514d\u5bf9 std::array \u8fdb\u884c\u4eba\u5de5\u7d22\u5f15\uff0c\u800c\u662f\u5e94\u8be5\u4f7f\u7528 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u6216 \u8fed\u4ee3\u5668 \u3002== \u4e00\u5b9a\u8981\u8bb0\u4f4f\uff0c\u5f53\u65e0\u7b26\u53f7\u6574\u578b\u5230\u8fbe\u5b83\u4eec\u7684\u6700\u5927\u503c\u540e\uff0c\u4f1a\u4ea7\u751f\u53cd\u8f6c\u3002\u5e38\u89c1\u7684\u9519\u8bef\u662f\u5bf9\u4e00\u4e2a\u6240\u6709\u8fdb\u884c\u9012\u51cf\uff0c\u5230\u8fbe0\u540e\u4ecd\u7136\u9012\u51cf\u5219\u4f1a\u5bfc\u81f4\u65e0\u7b26\u53f7\u6574\u578b\u53cd\u8f6c\u4e3a\u6700\u5927\u503c\u3002\u5728for\u5faa\u73af\u7684\u8bfe\u7a0b\u4e2d\u4f60\u5df2\u7ecf\u89c1\u8fc7\u4e0b\u9762\u7684\u4f8b\u5b50\u4e86\uff0c\u8fd9\u91cc\u518d\u590d\u4e60\u4e00\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; // \u9006\u5e8f\u6253\u5370\u7d20\u7ec4 // \u56e0\u4e3ai\u4e0d\u9700\u8981\u521d\u59cb\u5316\u4e3a0\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528auto // Bad: for ( auto i { myArray . size () - 1 }; i >= 0 ; -- i ) std :: cout << myArray [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u4e0a\u9762\u7684\u4ee3\u7801\u4f1a\u5f97\u5230\u4e00\u4e2a\u6b7b\u5faa\u73af\uff0c\u5f53 i \u53cd\u8f6c\u7684\u65f6\u5019\uff0c\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u8fd9\u91cc\u5b9e\u9645\u4e0a\u5b58\u5728\u4e24\u4e2a\u95ee\u9898\uff0c\u5982\u679c myArray \u662f\u7a7a\u7684\uff0c\u5219 size() \u4f1a\u8fd4\u56de 0 (\u5bf9\u4e8e std::array \u6765\u8bf4\u662f\u53ef\u4ee5\u7684)\uff0c\u6b64\u65f6 myArray.size() - 1 \u5c31\u4f1a\u53cd\u8f6c\u3002\u53e6\u5916\u4e00\u4e2a\u95ee\u9898\u5728\u4e8e\uff0c\u4e0d\u8bba\u6570\u7ec4\u4e2d\u6709\u591a\u5c11\u5143\u7d20\uff0c i >= 0 \u603b\u662f\u4e3a true \uff0c\u56e0\u4e3a\u65e0\u7b26\u53f7\u6574\u578b\u5c31\u4e0d\u53ef\u80fd\u5c0f\u4e8e0\u3002 \u53ef\u884c\u7684\u53cd\u5411for\u5faa\u73af\u5e94\u8be5\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; // Print the array in reverse order. for ( auto i { myArray . size () }; i -- > 0 ; ) std :: cout << myArray [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6211\u4eec\u5728\u6761\u4ef6\u8bed\u53e5\u4e2d\uff0c\u4f7f\u7528\u540e\u7f00 -- \u5bf9\u7d22\u5f15\u8fdb\u884c\u9012\u51cf\u3002\u8be5\u6761\u4ef6\u5224\u65ad\u4f1a\u5728\u6bcf\u6b21\u8fed\u4ee3\u524d\u8fdb\u884c\uff0c\u5305\u62ec\u7b2c\u4e00\u6b21\u8fed\u4ee3\u3002\u5728\u7b2c\u4e00\u6b21\u8fed\u4ee3\u65f6\uff0c i \u662f myArray.size() - 1 \uff0c\u56e0\u4e3a i \u5728\u6761\u4ef6\u4e2d\u9012\u51cf\u3002\u5f53 i \u7b49\u4e8e0\u65f6\uff0c\u5373\u5c06\u53d1\u751f\u53cd\u8f6c\uff0c\u4f46\u662f\u6b64\u65f6\u6761\u4ef6\u8bed\u53e5\u5df2\u7ecf\u4e0d\u662f true \u4e86\uff0c\u6240\u4ee5\u5faa\u73af\u4f1a\u505c\u6b62\u3002\u5b9e\u9645\u4e0a\uff0c i \u7684\u786e\u4f1a\u5728\u6700\u540e\u4e00\u4e2a -- \u65f6\u53cd\u8f6c\uff0c\u4e0d\u8fc7\u6b64\u65f6\u5faa\u73af\u5df2\u7ecf\u505c\u6b62\uff0c\u8be5\u503c\u5e76\u4e0d\u4f1a\u88ab\u5b9e\u9645\u4f7f\u7528\u3002 \u7ed3\u6784\u4f53\u6570\u7ec4 std::array \u663e\u7136\u4e0d\u53ef\u80fd\u53ea\u80fd\u5b58\u653e\u6570\u503c\u5143\u7d20\u3002\u4efb\u4f55\u80fd\u591f\u5728\u666e\u901a\u6570\u7ec4\u4e2d\u4f7f\u7528\u7684\u6570\u636e\u7c7b\u578b\uff0c\u540c\u6837\u4e5f\u80fd\u591f\u5728 std::array \u4e2d\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u7ed3\u6784\u4f53\u5143\u7d20\u7684 std::array \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include struct House { int number {}; int stories {}; int roomsPerStory {}; }; int main () { std :: array < House , 3 > houses {}; houses [ 0 ] = { 13 , 4 , 30 }; houses [ 1 ] = { 14 , 3 , 10 }; houses [ 2 ] = { 15 , 3 , 40 }; for ( const auto & house : houses ) { std :: cout << \"House number \" << house . number << \" has \" << ( house . stories * house . roomsPerStory ) << \" rooms \\n \" ; } return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 House number 13 has 120 rooms House number 14 has 30 rooms House number 15 has 120 rooms \u4e0d\u8fc7\uff0c\u5f53\u6211\u4eec\u9700\u8981\u521d\u59cb\u5316\u4e00\u4e2a\u6570\u7ec4\uff0c\u4e14\u6570\u7ec4\u5143\u7d20\u7684\u521d\u59cb\u5316\u4e5f\u9700\u8981\u4e00\u4e2a\u5217\u8868\u503c\u65f6\uff08\u6bd4\u5982\u7ed3\u6784\u4f53\u6570\u7ec4\uff09\uff0c\u4ee3\u7801\u4f1a\u770b\u8d77\u6765\u602a\u602a\u7684\u3002\u521d\u59cb\u5316\u6b64\u7c7b std::array \u65f6\uff0c\u4f60\u53ef\u80fd\u4f1a\u5c1d\u8bd5\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 // Doesn't work. std :: array < House , 3 > houses { { 13 , 4 , 30 }, { 14 , 3 , 10 }, { 15 , 3 , 40 } }; \u4f46\u8fd9\u79cd\u65b9\u6cd5\u5b9e\u9645\u4e0a\u662f\u65e0\u6548\u7684\u3002 \u56e0\u4e3a std::array \u7684\u5b9a\u4e49\u4e2d\u5305\u542b\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u6570\u7ec4\u6210\u5458 \uff08\u5176\u5177\u4f53\u540d\u5b57\u53d6\u51b3\u4e8e\u5b9e\u73b0\uff09\u3002\u6240\u4ee5\u5f53\u6211\u4eec\u50cf\u4e0a\u9762\u90a3\u6837\u521d\u59cb\u5316 houses \u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u6309\u7167\u4e0b\u9762\u7684\u65b9\u5f0f\u7406\u89e3\u8be5\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 // Doesn't work. std :: array < House , 3 > houses { // \u521d\u59cb\u5316 houses { 13 , 4 , 30 }, // \u521d\u59cb\u5316 std::array \u7ed3\u6784\u4e2d\u7684C\u98ce\u683c\u6570\u7ec4\u6210\u5458 { 14 , 3 , 10 }, // ? { 15 , 3 , 40 } // ? }; \u7f16\u8bd1\u5668\u4f1a\u5c06 { 13, 4, 30 } \u89e3\u91ca\u4e3a\u6210\u5458\u6570\u7ec4\u7684\u521d\u59cb\u5316\u503c\u3002\u5176\u6548\u679c\u76f8\u5f53\u4e8e\u4f7f\u7528\u8be5\u521d\u59cb\u5316\u503c\u521d\u59cb\u5316\u4e86\u4e0b\u6807\u4e3a\u7684\u7ed3\u6784\u4f53\uff0c\u5269\u4f59\u7684\u7ed3\u6784\u4f53\u5143\u7d20\u5219\u8fdb\u884c\u4e86 0\u521d\u59cb\u5316 \u3002\u968f\u540e\uff0c\u7f16\u8bd1\u5668\u4f1a\u53d1\u73b0\u6211\u4eec\u63d0\u4f9b\u4e86\u4e24\u4e2a\u989d\u5916\u7684\u521d\u59cb\u5316\u503c( { 14, 3, 10 } \u548c { 15, 3, 40 } ) \uff0c\u6240\u4ee5\u5b83\u4f1a\u4ea7\u751f\u4e00\u4e2a\u9519\u8bef\u4fe1\u606f\uff0c\u63d0\u9192\u5f00\u53d1\u8005\u521d\u59cb\u5316\u503c\u8fc7\u591a\u3002 \u6b63\u786e\u7684\u65b9\u6cd5\u662f\u518d\u6dfb\u52a0\u4e00\u5c42\u5927\u62ec\u53f7\uff1a 1 2 3 4 5 6 7 8 // This works as expected std :: array < House , 3 > houses { // initializer for houses { // \u989d\u5916\u7684\u5927\u62ec\u53f7\u8868\u660e\u63a5\u4e0b\u6765\u521d\u59cb\u5316\u6570\u7ec4\u6210\u5458 { 13 , 4 , 30 }, // initializer for array element 0 { 14 , 3 , 10 }, // initializer for array element 1 { 15 , 3 , 40 }, // initializer for array element 2 } }; \u6ce8\u610f\uff0c\u8be5\u5927\u62ec\u53f7\u662f\u5fc5\u8981\u7684\uff08\u8868\u660e\u5f00\u59cb\u521d\u59cb\u5316 std::array \u4e2d\u7684\u6570\u7ec4\u6210\u5458\uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5bf9\u6bcf\u4e2a\u7ed3\u6784\u4f53\u6210\u5458\u8fdb\u884c\u521d\u59cb\u5316\u4e86\u3002 \u8fd9\u5c31\u89e3\u91ca\u4e86\u4e3a\u4ec0\u4e48\u5728\u4f7f\u7528 std::array \u65f6\uff0c\u521d\u59cb\u5316\u503c\u5fc5\u987b\u591a\u4e00\u5c42\u5927\u62ec\u53f7\u3002 \u5c0f\u7ed3 std::array \u53ef\u4ee5\u5f88\u597d\u5730\u4ee3\u66ff\u5185\u7f6e\u7684\u56fa\u5b9a\u6570\u7ec4\u3002\u5b83\u6548\u7387\u5f88\u9ad8\uff0c\u552f\u4e00\u7684\u7f3a\u70b9\u5c31\u662f\u5b83\u7684\u8bed\u6cd5\u7a0d\u5fae\u6709\u70b9\u522b\u626d\uff1a\u4f60\u5fc5\u987b\u6307\u5b9a\u6570\u7ec4\u7684\u957f\u5ea6\uff08\u9664\u975e\u4f60\u7701\u7565\u7c7b\u578b\uff0c\u4e0d\u7136\u7f16\u8bd1\u5668\u4e0d\u4f1a\u57fa\u4e8e\u521d\u59cb\u5316\u503c\u81ea\u52a8\u8ba1\u7b97\u957f\u5ea6\uff0c\u4f46\u662f\u8fd9\u5e76\u4e0d\u603b\u662f\u53ef\u884c\uff09\uff0c\u800c\u4e14\u5728\u7d22\u5f15\u65f6\u8fd8\u5b58\u5728\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u4e0d\u5339\u914d\u7684\u95ee\u9898\u3002\u4e0d\u8fc7\uff0c\u7455\u4e0d\u63a9\u745c\uff0c\u6211\u4eec\u8fd8\u662f\u63a8\u8350\u4f7f\u7528 std::array \u6765\u4ee3\u66ff\u5185\u5efa\u6570\u7ec4\u3002","title":"11.16 \u2014 \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb"},{"location":"11-16-An-introduction-to-std-array/#stdarray","text":"std::array \u5b9e\u73b0\u4e86\u56fa\u5b9a\u6570\u7ec4\u7684\u529f\u80fd\uff0c\u4e14\u5728\u4f20\u5165\u51fd\u6570\u65f6\u4e0d\u4f1a\u9000\u5316\u4e3a\u6307\u9488\u3002 std::array \u88ab\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d\u4e14\u4f4d\u4e8e std \u547d\u540d\u7a7a\u95f4 \u5185\u3002 \u58f0\u660e std::array \u53d8\u91cf\u5f88\u7b80\u5355\uff1a 1 2 3 #include std :: array < int , 3 > myArray ; // \u58f0\u660e\u4e00\u4e2a\u957f\u5ea6\u4e3a3\u7684\u6574\u578b\u6570\u7ec4 \u548c\u539f\u751f\u7684\u56fa\u5b9a\u6570\u7ec4\u5b9e\u73b0\u7c7b\u4f3c\uff0c std::array \u7684\u957f\u5ea6\u5fc5\u987b\u5728 \u7f16\u8bd1\u65f6 \u786e\u5b9a\u3002 std::array \u53ef\u4ee5\u901a\u8fc7 \u521d\u59cb\u5316\u503c\u5217\u8868 \u6216 \u5217\u8868\u521d\u59cb\u5316 \u7684\u65b9\u5f0f\u8fdb\u884c\u521d\u59cb\u5316\uff1a 1 2 std :: array < int , 5 > myArray = { 9 , 7 , 5 , 3 , 1 }; // \u521d\u59cb\u5316\u503c\u5217\u8868 std :: array < int , 5 > myArray2 { 9 , 7 , 5 , 3 , 1 }; // \u5217\u8868\u521d\u59cb\u5316 \u548cC++\u81ea\u5e26\u7684\u56fa\u5b9a\u6570\u7ec4\u4e0d\u540c\u7684\u662f\uff0c\u5728\u4f7f\u7528 std::array \u7684\u65f6\u5019\uff0c\u4f60\u4e0d\u80fd\u7701\u7565\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u5373\u4f7f\u63d0\u4f9b\u4e86\u521d\u59cb\u5316\u503c\u4e5f\u4e0d\u884c\uff1a 1 2 std :: array < int , > myArray { 9 , 7 , 5 , 3 , 1 }; // \u4e0d\u5408\u6cd5\uff0c\u5fc5\u987b\u63d0\u4f9b\u957f\u5ea6 std :: array < int > myArray { 9 , 7 , 5 , 3 , 1 }; // \u4e0d\u5408\u6cd5\uff0c\u5fc5\u987b\u63d0\u4f9b\u957f\u5ea6 \u4e0d\u8fc7\uff0c\u4eceC++17\u5f00\u59cb\uff0c std::array \u4e5f\u652f\u6301\u7701\u7565\u7c7b\u578b\u548c\u5927\u5c0f\u4e86\u3002\u5982\u679c\u6570\u7ec4\u88ab\u663e\u5f0f\u5730\u521d\u59cb\u5316\uff0c\u5219\u7c7b\u578b\u548c\u5927\u5c0f\u53ef\u4ee5\u4e00\u8d77\u88ab\u5ffd\u7565\u3002 1 2 std :: array myArray { 9 , 7 , 5 , 3 , 1 }; // \u901a\u8fc7\u7c7b\u578b\u63a8\u65ad\u5f97\u5230 std::array std :: array myArray { 9.7 , 7.31 }; // \u901a\u8fc7\u7c7b\u578b\u63a8\u65ad\u5f97\u5230 std::array \u6211\u4eec\u63a8\u8350\u4f7f\u7528\u4e0a\u9762\u8fd9\u79cd\u5ffd\u7565\u7c7b\u578b\u548c\u5927\u5c0f\u7684\u58f0\u660e\u65b9\u5f0f\uff0c\u9664\u975e\u4f60\u7684\u7f16\u8bd1\u5668\u4e0d\u652f\u6301C++17\uff0c\u90a3\u4e48\u4f60\u5c31\u5fc5\u987b\u663e\u5f0f\u5730\u6307\u5b9a\u4e86\u7c7b\u578b\u548c\u5927\u5c0f\u3002 1 2 3 4 5 // std::array myArray { 9, 7, 5, 3, 1 }; // Since C++17 std :: array < int , 5 > myArray { 9 , 7 , 5 , 3 , 1 }; // Before C++17 // std::array myArray { 9.7, 7.31 }; // Since C++17 std :: array < double , 2 > myArray { 9.7 , 7.31 }; // Before C++17 \u5728 C++20 \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u5143\u7d20\u7c7b\u578b\uff0c\u4f46\u662f\u5ffd\u7565\u6570\u7ec4\u957f\u5ea6\u3002\u8fd9\u4e48\u505a\u7684\u65f6\u5019\uff0c\u521b\u5efa std::array \u65f6\u770b\u8d77\u6765\u66f4\u50cf\u662f\u521b\u5efa\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u6570\u7ec4\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 std::to_array \u6765\u521b\u5efa\u6570\u7ec4\uff08\u6307\u5b9a\u5143\u7d20\u7c7b\u578b\u4f46\u81ea\u52a8\u63a8\u65ad\u957f\u5ea6\uff09\uff1a 1 2 3 auto myArray1 { std :: to_array < int , 5 > ({ 9 , 7 , 5 , 3 , 1 }) }; // Specify type and size auto myArray2 { std :: to_array < int > ({ 9 , 7 , 5 , 3 , 1 }) }; // Specify type only, deduce size auto myArray3 { std :: to_array ({ 9 , 7 , 5 , 3 , 1 }) }; // Deduce type and size \u4e0d\u5e78\u7684\u662f\uff0c\u4f7f\u7528 std::to_array \u521b\u5efa\u6570\u7ec4\u65f6\u7684\u5f00\u9500\u6bd4\u76f4\u63a5\u4f7f\u7528 std::array \u521b\u5efa\u6570\u7ec4\u5f00\u9500\u66f4\u5927\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u5c06\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u6570\u7ec4\u7684\u5143\u7d20\u62f7\u8d1d\u5230 std::array \u3002\u51fa\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u5982\u679c\u9700\u8981\u5728\u5faa\u73af\u4e2d\u591a\u6b21\u521b\u5efa\u6570\u7ec4\u65f6\uff0c\u5e94\u8be5\u907f\u514d\u4f7f\u7528 std::to_array \u3002 \u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 \u521d\u59cb\u5316\u503c\u5217\u8868 \u4e3a\u6570\u7ec4\u8d4b\u503c\u3002 1 2 3 4 std :: array < int , 5 > myArray ; myArray = { 0 , 1 , 2 , 3 , 4 }; // okay myArray = { 9 , 8 , 7 }; // okay, elements 3 and 4 are set to zero! myArray = { 0 , 1 , 2 , 3 , 4 , 5 }; // not allowed, too many elements in initializer list! \u6076\u610f\u4f7f\u7528\u4e0b\u6807\u8fd0\u7b97\u7b26\u8bbf\u95ee std::array \u6570\u7ec4\u4e2d\u7684\u503c\uff1a 1 2 std :: cout << myArray [ 1 ] << '\\n' ; myArray [ 2 ] = 6 ; \u548c\u5185\u7f6e\u7684\u6570\u7ec4\u7c7b\u4f3c\uff0c\u4e0b\u6807\u8fd0\u7b97\u7b26\u5e76\u6ca1\u6709\u5b9e\u73b0\u8d8a\u754c\u68c0\u67e5\u3002\u5f53\u4f20\u5165\u975e\u6cd5\u7d22\u5f15\u65f6\uff0c\u4f1a\u53d1\u751f\u5f88\u4e0d\u597d\u7684\u4e8b\u60c5\u3002 std::array \u8fd8\u652f\u6301\u53e6\u5916\u4e00\u79cd\u5143\u7d20\u8bbf\u95ee\u65b9\u5f0f\uff08\u4f7f\u7528 at() \u51fd\u6570\uff09\uff0c\u5b83\u4f1a\u5728\uff08 \u8fd0\u884c\u65f6 \uff09\u8fdb\u884c\u8d8a\u754c\u68c0\u67e5\uff1a 1 2 3 std :: array myArray { 9 , 7 , 5 , 3 , 1 }; myArray . at ( 1 ) = 6 ; // array element 1 is valid, sets array element 1 to value 6 myArray . at ( 9 ) = 10 ; // array element 9 is invalid, will throw a runtime error \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u8c03\u7528 myArray.at(1) \u65f6\u4f1a\u786e\u4fdd\u6240\u4ee51\u662f\u5408\u6cd5\u7684\u3002\u7531\u4e8e1\u786e\u5b9e\u662f\u5408\u6cd5\u7d22\u5f15\uff0c\u6240\u4ee5\u5b83\u4f1a\u8fd4\u56de\u5bf9\u5e94\u7684\u5143\u7d20\u7684\u5f15\u7528\u3002\u6211\u4eec\u5c066\u8d4b\u503c\u7ed9\u8be5\u5f15\u7528\u3002\u4e0d\u8fc7\uff0c\u5f53\u6211\u4eec\u8c03\u7528 myArray.at(9) \u65f6\u5c31\u4f1a\u5931\u8d25\uff0c\u56e0\u4e3a\u7d22\u5f159\u8d8a\u754c\u4e86\u3002\u5f53\u8d8a\u754c\u53d1\u751f\u65f6\uff0c at() \u51fd\u6570\u4f1a\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\uff08\u5177\u4f53\u6765\u8bf4\u5b83\u4f1a\u629b\u51fa\u4e00\u4e2a std::out_of_range \u5f02\u5e38\uff09\uff0c\u800c\u4e0d\u4f1a\u8fd4\u56de\u5bf9\u5e94\u7684\u7d22\u5f15\u3002\u56e0\u4e3a\u9700\u8981\u8fdb\u884c\u8d8a\u754c\u68c0\u67e5\uff0c\u6240\u4ee5 at() \u7684\u901f\u5ea6\u4f1a\u6bd4\u4e0b\u6807\u8fd0\u7b97\u7b26\u6162\uff0c\u4f46\u5b83\u66f4\u5b89\u5168\u3002 std::array \u4f1a\u5728 \u79bb\u5f00\u4f5c\u7528\u57df \u65f6\u6e05\u7406\u81ea\u5df1\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u8fdb\u884c\u624b\u52a8\u6e05\u7406\u3002","title":"std::array"},{"location":"11-16-An-introduction-to-std-array/#_1","text":"size() \u51fd\u6570\u53ef\u4ee5\u7528\u6765\u83b7\u53d6 std::array \u7684\u957f\u5ea6\uff1a 1 2 std :: array myArray { 9.0 , 7.2 , 5.4 , 3.6 , 1.8 }; std :: cout << \"length: \" << myArray . size () << '\\n' ; \u7a0b\u5e8f\u6253\u5370\uff1a 1 length: 5 \u56e0\u4e3a std::array \u4e0d\u4f1a\u5728\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\u9000\u5316\u4e3a\u6307\u9488\uff0c\u6240\u4ee5 size() \u5728\u51fd\u6570\u5185\u90e8\u4e5f\u53ef\u4ee5\u8c03\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include void printLength ( const std :: array < double , 5 >& myArray ) { std :: cout << \"length: \" << myArray . size () << '\\n' ; } int main () { std :: array myArray { 9.0 , 7.2 , 5.4 , 3.6 , 1.8 }; printLength ( myArray ); return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 length: 5 \u6ce8\u610f\uff0c\u5728\u6807\u51c6\u5e93\u4e2d size \u4e00\u8bcd\u8868\u793a\u6570\u7ec4\u7684\u957f\u5ea6\u2014\u2014\u4e0d\u8981\u628a\u5b83\u548c sizeof() \u641e\u6df7\u4e86\uff0c\u5f53\u5bf9\u539f\u751f\u7684\u56fa\u5b9a\u6570\u7ec4\u4f7f\u7528 sizeof \u65f6\uff0c\u8fd4\u56de\u7684\u662f\u8be5\u6570\u7ec4\u5360\u7528\u7684\u5b9e\u9645\u5185\u5b58\u7684\u5927\u5c0f\uff08\u5143\u7d20\u5927\u5c0f\u4e58\u4ee5\u6570\u7ec4\u957f\u5ea6\uff09\u3002\u662f\u7684\uff0c\u8fd9\u8fb9\u547d\u540d\u7684\u4e00\u81f4\u6027\u4fdd\u6301\u7684\u4e0d\u597d\u3002 \u8fd8\u8981\u6ce8\u610f\uff0c\u5f53 \u6309\u5f15\u7528\u4f20\u9012 std::array \u65f6\u3002\u53ef\u4ee5\u9632\u6b62\u7f16\u8bd1\u5668\u521b\u5efa std::array \u7684\u62f7\u8d1d\uff08\u6709\u52a9\u4e8e\u63d0\u5347\u6027\u80fd\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u6309\u5f15\u7528\u4f20\u9012\u6216\u6309const\u5f15\u7528\u4f20\u9012\u7684\u65b9\u5f0f\u5c06 std::array \u4f20\u5165\u51fd\u6570\u3002 \u56e0\u4e3a std::array \u7684\u957f\u5ea6\u603b\u662f\u5df2\u77e5\u7684\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u4f7f\u7528 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u6765\u904d\u5386\uff1a 1 2 3 4 std :: array myArray { 9 , 7 , 5 , 3 , 1 }; for ( int element : myArray ) std :: cout << element << ' ' ; \u4f7f\u7528 std::sort \u53ef\u4ee5\u5bf9 std::array \u8fdb\u884c\u6392\u5e8f\uff0c\u8be5\u51fd\u6570\u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // for std::sort #include #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; std :: sort ( myArray . begin (), myArray . end ()); // \u4ece\u524d\u5411\u540e\u6392\u5e8f // std::sort(myArray.rbegin(), myArray.rend()); // \u4ece\u540e\u5411\u524d\u6392\u5e8f for ( int element : myArray ) std :: cout << element << ' ' ; std :: cout << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 1 3 5 7 9","title":"\u6570\u7ec4\u5927\u5c0f\u548c\u6392\u5e8f"},{"location":"11-16-An-introduction-to-std-array/#stdarray_1","text":"\u5bf9\u4e8e std::array \u6765\u8bf4\uff0c\u5143\u7d20\u7684\u7c7b\u578b\u548c\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u6570\u7ec4\u7c7b\u578b\u7684\u4e00\u90e8\u5206\u3002\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u4f7f\u7528 std::array \u4f5c\u4e3a\u51fd\u6570 \u5f62\u53c2 \u65f6\uff0c\u5fc5\u987b\u6307\u660e\u5143\u7d20\u7c7b\u578b\u548c\u6570\u7ec4\u957f\u5ea6 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include void printArray ( const std :: array < int , 5 >& myArray ) { for ( auto element : myArray ) std :: cout << element << ' ' ; std :: cout << '\\n' ; } int main () { std :: array myArray5 { 9.0 , 7.2 , 5.4 , 3.6 , 1.8 }; // type deduced as std::array printArray ( myArray5 ); // error: printArray expects a std::array return 0 ; } \u8fd9\u4e48\u505a\u7684\u5f0a\u7aef\u5728\u4e8e\uff0c\u8be5\u51fd\u6570\u4fbf\u53ea\u80fd\u4f7f\u7528\u7279\u5b9a\u5143\u7d20\u7c7b\u578b\u548c\u6570\u7ec4\u957f\u5ea6\u7684\u6570\u7ec4\u4f5c\u4e3a \u5b9e\u53c2 \uff0c\u4f46\u662f\u5982\u679c\u6211\u4eec\u5e0c\u671b\u51fd\u6570\u80fd\u591f\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u5143\u7d20\u4e14\u957f\u5ea6\u4e0d\u540c\u7684\u6570\u7ec4\u65f6\uff0c\u5e94\u8be5\u600e\u4e48\u505a\u5462\uff1f\u96be\u9053\u8981\u521b\u5efa\u591a\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\u6765\u5904\u7406\u5417\uff1f\u8fd9\u663e\u7136\u4f1a\u5e26\u6765\u5f88\u591a\u5f88\u591a\u7684\u5197\u4f59\u4ee3\u7801\u3002 \u5e78\u8fd0\u5730\u662f\uff0cC++\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u6a21\u677f\u6765\u5b9e\u73b0\u4e0a\u8ff0\u529f\u80fd\u3002 \u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6a21\u677f\u5316\u51fd\u6570\u5e76\u5c06\u6240\u6709\u7684\u7c7b\u578b\u4fe1\u606f\u53c2\u6570\u5316\uff0cC++\u4f1a\u6839\u636e\u6a21\u677f\u5728\u9700\u8981\u65f6\u81ea\u52a8\u521b\u5efa\u201c\u771f\u5b9e\u201d\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include #include // printArray is a template function template < typename T , std :: size_t size > // parameterize the element type and size void printArray ( const std :: array < T , size >& myArray ) { for ( auto element : myArray ) std :: cout << element << ' ' ; std :: cout << '\\n' ; } int main () { std :: array myArray5 { 9.0 , 7.2 , 5.4 , 3.6 , 1.8 }; printArray ( myArray5 ); std :: array myArray7 { 9.0 , 7.2 , 5.4 , 3.6 , 1.8 , 1.2 , 0.7 }; printArray ( myArray7 ); return 0 ; } \u76f8\u5173\u5185\u5bb9 \u51fd\u6570\u6a21\u677f\u4f1a\u5728 8.13 - \u51fd\u6570\u6a21\u677f \u4e2d\u4ecb\u7ecd\u3002","title":"\u5411\u51fd\u6570\u4f20\u9012\u4e0d\u540c\u957f\u5ea6\u7684 std::array"},{"location":"11-16-An-introduction-to-std-array/#size_type-stdarray","text":"\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\uff1a\u4e0b\u9762\u7684\u4ee3\u7801\u6709\u4ec0\u4e48\u95ee\u9898\uff1f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; // Iterate through the array and print the value of the elements for ( int i { 0 }; i < myArray . size (); ++ i ) std :: cout << myArray [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u8fd9\u4e2a\u95ee\u9898\u7684\u7b54\u6848\u662f\uff1a\u4ee3\u7801\u4e2d\u53ef\u80fd\u4f1a\u51fa\u73b0 signed/unsigned \u4e0d\u5339\u914d\u7684\u95ee\u9898\u3002 size() \u51fd\u6570\u548c\u4e0b\u6807\u7d22\u5f15\u4f7f\u7528\u4e86\u4e00\u79cd\u88ab\u79f0\u4e3a size_type \u7684\u7c7b\u578b\uff0c\u8be5\u7c7b\u578b\u5728C++\u6807\u51c6\u4e2d\u88ab\u5b9a\u4e49\u4e3a\u65e0\u7b26\u53f7\u6574\u578b\u7c7b\u578b\u3002\u800c\u5728\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c\u5faa\u73af\u7d22\u5f15 i \u662f\u4e00\u4e2a\u6709\u7b26\u53f7\u6574\u578b\u3002\u56e0\u6b64\u5728\u8fdb\u884c i < myArray.size() \u6bd4\u8f83\u65f6\u4f1a\u51fa\u73b0\u7c7b\u578b\u4e0d\u5339\u914d\u7684\u95ee\u9898\u3002 \u6709\u8da3\u7684\u662f\uff0c size_type \u8fd8\u4e0d\u662f\u4e00\u4e2a\u5168\u5c40\u7c7b\u578b\uff08\u4f8b\u5982\uff0c int \u6216 std::size_t \uff09\uff0c\u5b83\u5b9e\u9645\u4e0a\u88ab\u5b9a\u4e49\u5728 std::array \u5185\u90e8 (C++\u5141\u8bb8\u5d4c\u5957\u5b9a\u4e49)\u3002\u8fd9\u5c31\u610f\u5473\u7740\uff0c\u5f53\u4f60\u9700\u8981\u4f7f\u7528 size_type \u65f6\uff0c\u5fc5\u987b\u6dfb\u52a0\u5b8c\u6574\u7684\u524d\u7f00(\u53ef\u4ee5\u5c06 std::array \u770b\u505a\u662f\u5b83\u7684\u547d\u540d\u7a7a\u95f4)\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c \u201csize_type\u201d \u7684\u5b8c\u6574\u524d\u7f00\u662f std::array::size_type ! \u6240\u4ee5\uff0c\u4e0a\u9762\u4ee3\u7801\u7684\u6b63\u786e\u5199\u6cd5\u5e94\u8be5\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; // std::array::size_type is the return type of size()! for ( std :: array < int , 5 >:: size_type i { 0 }; i < myArray . size (); ++ i ) std :: cout << myArray [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u53ef\u8bfb\u6027\u5e76\u4e0d\u597d\u3002\u5e78\u8fd0\u5730\u662f\uff0c std::array::size_type \u5b9e\u9645\u4e0a\u662f std::size_t \u7684\u522b\u540d\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 std::size_t \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include // std::size_t #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; for ( std :: size_t i { 0 }; i < myArray . size (); ++ i ) std :: cout << myArray [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u66f4\u597d\u7684\u529e\u6cd5\u662f==\u907f\u514d\u5bf9 std::array \u8fdb\u884c\u4eba\u5de5\u7d22\u5f15\uff0c\u800c\u662f\u5e94\u8be5\u4f7f\u7528 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u6216 \u8fed\u4ee3\u5668 \u3002== \u4e00\u5b9a\u8981\u8bb0\u4f4f\uff0c\u5f53\u65e0\u7b26\u53f7\u6574\u578b\u5230\u8fbe\u5b83\u4eec\u7684\u6700\u5927\u503c\u540e\uff0c\u4f1a\u4ea7\u751f\u53cd\u8f6c\u3002\u5e38\u89c1\u7684\u9519\u8bef\u662f\u5bf9\u4e00\u4e2a\u6240\u6709\u8fdb\u884c\u9012\u51cf\uff0c\u5230\u8fbe0\u540e\u4ecd\u7136\u9012\u51cf\u5219\u4f1a\u5bfc\u81f4\u65e0\u7b26\u53f7\u6574\u578b\u53cd\u8f6c\u4e3a\u6700\u5927\u503c\u3002\u5728for\u5faa\u73af\u7684\u8bfe\u7a0b\u4e2d\u4f60\u5df2\u7ecf\u89c1\u8fc7\u4e0b\u9762\u7684\u4f8b\u5b50\u4e86\uff0c\u8fd9\u91cc\u518d\u590d\u4e60\u4e00\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; // \u9006\u5e8f\u6253\u5370\u7d20\u7ec4 // \u56e0\u4e3ai\u4e0d\u9700\u8981\u521d\u59cb\u5316\u4e3a0\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528auto // Bad: for ( auto i { myArray . size () - 1 }; i >= 0 ; -- i ) std :: cout << myArray [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u4e0a\u9762\u7684\u4ee3\u7801\u4f1a\u5f97\u5230\u4e00\u4e2a\u6b7b\u5faa\u73af\uff0c\u5f53 i \u53cd\u8f6c\u7684\u65f6\u5019\uff0c\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u8fd9\u91cc\u5b9e\u9645\u4e0a\u5b58\u5728\u4e24\u4e2a\u95ee\u9898\uff0c\u5982\u679c myArray \u662f\u7a7a\u7684\uff0c\u5219 size() \u4f1a\u8fd4\u56de 0 (\u5bf9\u4e8e std::array \u6765\u8bf4\u662f\u53ef\u4ee5\u7684)\uff0c\u6b64\u65f6 myArray.size() - 1 \u5c31\u4f1a\u53cd\u8f6c\u3002\u53e6\u5916\u4e00\u4e2a\u95ee\u9898\u5728\u4e8e\uff0c\u4e0d\u8bba\u6570\u7ec4\u4e2d\u6709\u591a\u5c11\u5143\u7d20\uff0c i >= 0 \u603b\u662f\u4e3a true \uff0c\u56e0\u4e3a\u65e0\u7b26\u53f7\u6574\u578b\u5c31\u4e0d\u53ef\u80fd\u5c0f\u4e8e0\u3002 \u53ef\u884c\u7684\u53cd\u5411for\u5faa\u73af\u5e94\u8be5\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; // Print the array in reverse order. for ( auto i { myArray . size () }; i -- > 0 ; ) std :: cout << myArray [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6211\u4eec\u5728\u6761\u4ef6\u8bed\u53e5\u4e2d\uff0c\u4f7f\u7528\u540e\u7f00 -- \u5bf9\u7d22\u5f15\u8fdb\u884c\u9012\u51cf\u3002\u8be5\u6761\u4ef6\u5224\u65ad\u4f1a\u5728\u6bcf\u6b21\u8fed\u4ee3\u524d\u8fdb\u884c\uff0c\u5305\u62ec\u7b2c\u4e00\u6b21\u8fed\u4ee3\u3002\u5728\u7b2c\u4e00\u6b21\u8fed\u4ee3\u65f6\uff0c i \u662f myArray.size() - 1 \uff0c\u56e0\u4e3a i \u5728\u6761\u4ef6\u4e2d\u9012\u51cf\u3002\u5f53 i \u7b49\u4e8e0\u65f6\uff0c\u5373\u5c06\u53d1\u751f\u53cd\u8f6c\uff0c\u4f46\u662f\u6b64\u65f6\u6761\u4ef6\u8bed\u53e5\u5df2\u7ecf\u4e0d\u662f true \u4e86\uff0c\u6240\u4ee5\u5faa\u73af\u4f1a\u505c\u6b62\u3002\u5b9e\u9645\u4e0a\uff0c i \u7684\u786e\u4f1a\u5728\u6700\u540e\u4e00\u4e2a -- \u65f6\u53cd\u8f6c\uff0c\u4e0d\u8fc7\u6b64\u65f6\u5faa\u73af\u5df2\u7ecf\u505c\u6b62\uff0c\u8be5\u503c\u5e76\u4e0d\u4f1a\u88ab\u5b9e\u9645\u4f7f\u7528\u3002","title":"\u57fa\u4e8esize_type \u5bf9 std::array \u8fdb\u884c\u4eba\u5de5\u7d22\u5f15"},{"location":"11-16-An-introduction-to-std-array/#_2","text":"std::array \u663e\u7136\u4e0d\u53ef\u80fd\u53ea\u80fd\u5b58\u653e\u6570\u503c\u5143\u7d20\u3002\u4efb\u4f55\u80fd\u591f\u5728\u666e\u901a\u6570\u7ec4\u4e2d\u4f7f\u7528\u7684\u6570\u636e\u7c7b\u578b\uff0c\u540c\u6837\u4e5f\u80fd\u591f\u5728 std::array \u4e2d\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u7ed3\u6784\u4f53\u5143\u7d20\u7684 std::array \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include struct House { int number {}; int stories {}; int roomsPerStory {}; }; int main () { std :: array < House , 3 > houses {}; houses [ 0 ] = { 13 , 4 , 30 }; houses [ 1 ] = { 14 , 3 , 10 }; houses [ 2 ] = { 15 , 3 , 40 }; for ( const auto & house : houses ) { std :: cout << \"House number \" << house . number << \" has \" << ( house . stories * house . roomsPerStory ) << \" rooms \\n \" ; } return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 House number 13 has 120 rooms House number 14 has 30 rooms House number 15 has 120 rooms \u4e0d\u8fc7\uff0c\u5f53\u6211\u4eec\u9700\u8981\u521d\u59cb\u5316\u4e00\u4e2a\u6570\u7ec4\uff0c\u4e14\u6570\u7ec4\u5143\u7d20\u7684\u521d\u59cb\u5316\u4e5f\u9700\u8981\u4e00\u4e2a\u5217\u8868\u503c\u65f6\uff08\u6bd4\u5982\u7ed3\u6784\u4f53\u6570\u7ec4\uff09\uff0c\u4ee3\u7801\u4f1a\u770b\u8d77\u6765\u602a\u602a\u7684\u3002\u521d\u59cb\u5316\u6b64\u7c7b std::array \u65f6\uff0c\u4f60\u53ef\u80fd\u4f1a\u5c1d\u8bd5\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 // Doesn't work. std :: array < House , 3 > houses { { 13 , 4 , 30 }, { 14 , 3 , 10 }, { 15 , 3 , 40 } }; \u4f46\u8fd9\u79cd\u65b9\u6cd5\u5b9e\u9645\u4e0a\u662f\u65e0\u6548\u7684\u3002 \u56e0\u4e3a std::array \u7684\u5b9a\u4e49\u4e2d\u5305\u542b\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u6570\u7ec4\u6210\u5458 \uff08\u5176\u5177\u4f53\u540d\u5b57\u53d6\u51b3\u4e8e\u5b9e\u73b0\uff09\u3002\u6240\u4ee5\u5f53\u6211\u4eec\u50cf\u4e0a\u9762\u90a3\u6837\u521d\u59cb\u5316 houses \u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u6309\u7167\u4e0b\u9762\u7684\u65b9\u5f0f\u7406\u89e3\u8be5\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 // Doesn't work. std :: array < House , 3 > houses { // \u521d\u59cb\u5316 houses { 13 , 4 , 30 }, // \u521d\u59cb\u5316 std::array \u7ed3\u6784\u4e2d\u7684C\u98ce\u683c\u6570\u7ec4\u6210\u5458 { 14 , 3 , 10 }, // ? { 15 , 3 , 40 } // ? }; \u7f16\u8bd1\u5668\u4f1a\u5c06 { 13, 4, 30 } \u89e3\u91ca\u4e3a\u6210\u5458\u6570\u7ec4\u7684\u521d\u59cb\u5316\u503c\u3002\u5176\u6548\u679c\u76f8\u5f53\u4e8e\u4f7f\u7528\u8be5\u521d\u59cb\u5316\u503c\u521d\u59cb\u5316\u4e86\u4e0b\u6807\u4e3a\u7684\u7ed3\u6784\u4f53\uff0c\u5269\u4f59\u7684\u7ed3\u6784\u4f53\u5143\u7d20\u5219\u8fdb\u884c\u4e86 0\u521d\u59cb\u5316 \u3002\u968f\u540e\uff0c\u7f16\u8bd1\u5668\u4f1a\u53d1\u73b0\u6211\u4eec\u63d0\u4f9b\u4e86\u4e24\u4e2a\u989d\u5916\u7684\u521d\u59cb\u5316\u503c( { 14, 3, 10 } \u548c { 15, 3, 40 } ) \uff0c\u6240\u4ee5\u5b83\u4f1a\u4ea7\u751f\u4e00\u4e2a\u9519\u8bef\u4fe1\u606f\uff0c\u63d0\u9192\u5f00\u53d1\u8005\u521d\u59cb\u5316\u503c\u8fc7\u591a\u3002 \u6b63\u786e\u7684\u65b9\u6cd5\u662f\u518d\u6dfb\u52a0\u4e00\u5c42\u5927\u62ec\u53f7\uff1a 1 2 3 4 5 6 7 8 // This works as expected std :: array < House , 3 > houses { // initializer for houses { // \u989d\u5916\u7684\u5927\u62ec\u53f7\u8868\u660e\u63a5\u4e0b\u6765\u521d\u59cb\u5316\u6570\u7ec4\u6210\u5458 { 13 , 4 , 30 }, // initializer for array element 0 { 14 , 3 , 10 }, // initializer for array element 1 { 15 , 3 , 40 }, // initializer for array element 2 } }; \u6ce8\u610f\uff0c\u8be5\u5927\u62ec\u53f7\u662f\u5fc5\u8981\u7684\uff08\u8868\u660e\u5f00\u59cb\u521d\u59cb\u5316 std::array \u4e2d\u7684\u6570\u7ec4\u6210\u5458\uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5bf9\u6bcf\u4e2a\u7ed3\u6784\u4f53\u6210\u5458\u8fdb\u884c\u521d\u59cb\u5316\u4e86\u3002 \u8fd9\u5c31\u89e3\u91ca\u4e86\u4e3a\u4ec0\u4e48\u5728\u4f7f\u7528 std::array \u65f6\uff0c\u521d\u59cb\u5316\u503c\u5fc5\u987b\u591a\u4e00\u5c42\u5927\u62ec\u53f7\u3002","title":"\u7ed3\u6784\u4f53\u6570\u7ec4"},{"location":"11-16-An-introduction-to-std-array/#_3","text":"std::array \u53ef\u4ee5\u5f88\u597d\u5730\u4ee3\u66ff\u5185\u7f6e\u7684\u56fa\u5b9a\u6570\u7ec4\u3002\u5b83\u6548\u7387\u5f88\u9ad8\uff0c\u552f\u4e00\u7684\u7f3a\u70b9\u5c31\u662f\u5b83\u7684\u8bed\u6cd5\u7a0d\u5fae\u6709\u70b9\u522b\u626d\uff1a\u4f60\u5fc5\u987b\u6307\u5b9a\u6570\u7ec4\u7684\u957f\u5ea6\uff08\u9664\u975e\u4f60\u7701\u7565\u7c7b\u578b\uff0c\u4e0d\u7136\u7f16\u8bd1\u5668\u4e0d\u4f1a\u57fa\u4e8e\u521d\u59cb\u5316\u503c\u81ea\u52a8\u8ba1\u7b97\u957f\u5ea6\uff0c\u4f46\u662f\u8fd9\u5e76\u4e0d\u603b\u662f\u53ef\u884c\uff09\uff0c\u800c\u4e14\u5728\u7d22\u5f15\u65f6\u8fd8\u5b58\u5728\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u4e0d\u5339\u914d\u7684\u95ee\u9898\u3002\u4e0d\u8fc7\uff0c\u7455\u4e0d\u63a9\u745c\uff0c\u6211\u4eec\u8fd8\u662f\u63a8\u8350\u4f7f\u7528 std::array \u6765\u4ee3\u66ff\u5185\u5efa\u6570\u7ec4\u3002","title":"\u5c0f\u7ed3"},{"location":"11-17-An-introduction-to-std-vector/","tags":["vector","C++17"],"text":"Key Takeaway \u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86 std::array \uff0c\u5b83\u63d0\u4f9b\u4e86C++\u5185\u5efa\u56fa\u5b9a\u6570\u7ec4\u76f8\u540c\u7684\u529f\u80fd\uff0c\u4f46\u662f\u66f4\u5b89\u5168\u4e5f\u66f4\u597d\u7528\u3002 \u7c7b\u4f3c\u5730\uff0cC++ \u6807\u51c6\u5e93\u4e5f\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u529f\u80fd\uff0c\u4f7f\u5f97\u4f7f\u7528\u52a8\u6001\u6570\u7ec4\u53ef\u4ee5\u66f4\u52a0\u5b89\u5168\u548c\u6613\u7528\u3002\u8fd9\u4e2a\u529f\u80fd\u5c31\u662f std::vector \u3002 \u4e0d\u540c\u4e8e std::array \u5c3d\u53ef\u80fd\u4eff\u7167\u56fa\u5b9a\u6570\u7ec4\u7684\u529f\u80fd\uff0c std::vector \u5219\u63d0\u4f9b\u4e86\u5f88\u591a\u6570\u7ec4\u7684\u989d\u5916\u529f\u80fd\uff0c\u56e0\u6b64 std::vector \u4e5f\u6210\u4e3a\u4e86C++\u4e2d\u6700\u6709\u7528\u7684\u5de5\u5177\u4e4b\u4e00\u3002 std::vector \u7b80\u4ecb std::vector \u5728C++03\u4e2d\u88ab\u5f15\u5165\uff0c\u5b83\u63d0\u4f9b\u4e86 dynamic-array \u7684\u529f\u80fd\u5e76\u4e14\u53ef\u4ee5\u81ea\u6211\u7ba1\u7406\u5185\u5b58\u7684\u7533\u8bf7\u548c\u91ca\u653e\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u4f60\u53ef\u4ee5\u5728 \u8fd0\u884c\u65f6 \u521b\u5efa\u6570\u7ec4\u5e76\u6307\u5b9a\u5176\u957f\u5ea6\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528 new \u548c delete \u6765\u624b\u52a8\u7ba1\u7406\u5185\u5b58\u7684\u7533\u8bf7\u91ca\u653e\u3002 std::vector \u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d\u3002 \u58f0\u660e\u4e00\u4e2a std::vector \u5f88\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 #include // \u5728\u58f0\u660e\u65f6\u65e0\u9700\u6307\u5b9a\u957f\u5ea6 std :: vector < int > array ; std :: vector < int > array2 = { 9 , 7 , 5 , 3 , 1 }; // \u4f7f\u7528\u521d\u59cb\u5316\u503c\u5217\u8868\u8fdb\u884c\u521d\u59cb\u5316(\u5728C++11\u4e4b\u524d) std :: vector < int > array3 { 9 , 7 , 5 , 3 , 1 }; // \u4f7f\u7528\u7edf\u4e00\u521d\u59cb\u5316 //\u548cstd::array\u4e00\u6837\uff0c\u4eceC++17\u5f00\u59cb\uff0c\u7c7b\u578b\u53ef\u4ee5\u7701\u7565 std :: vector array4 { 9 , 7 , 5 , 3 , 1 }; // deduced to std::vector \u6ce8\u610f\uff0c\u4e0d\u8bba\u662f\u521d\u59cb\u5316\u4e0e\u5426\uff0c\u4f60\u90fd\u4e0d\u9700\u8981\u5728\u7f16\u8bd1\u65f6\u6307\u5b9a\u6570\u7ec4\u957f\u5ea6\u3002\u8fd9\u662f\u56e0\u4e3a std::vector \u53ef\u4ee5\u5728\u9700\u8981\u65f6\u81ea\u884c\u5206\u914d\u5185\u5b58\u3002 \u548c std::array \u4e00\u6837\uff0c\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u53ef\u4ee5\u4f7f\u7528 \u4e0b\u6807\u8fd0\u7b97\u7b26 \uff08\u4e0d\u8fdb\u884c\u8d8a\u754c\u68c0\u67e5\uff09\u6216 at() \u51fd\u6570\uff08\u8fdb\u884c\u8d8a\u754c\u68c0\u67e5\uff09\u8bbf\u95ee\uff1a 1 2 array [ 6 ] = 2 ; // no bounds checking array . at ( 7 ) = 3 ; // does bounds checking \u5bf9\u4e8e\u4e0a\u9762\u4e24\u79cd\u60c5\u51b5\uff0c\u5982\u679c\u4f60\u8bbf\u95ee\u8d85\u51fa\u6570\u7ec4\u7ed3\u5c3e\u7684\u5143\u7d20\uff0cvector\u5e76\u4e0d\u4f1a\u81ea\u52a8\u8c03\u6574\u957f\u5ea6\u3002 \u5728 C++11 \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 \u521d\u59cb\u5316\u503c\u5217\u8868 \u4e3a std::vector \u8d4b\u503c\uff1a 1 2 array = { 0 , 1 , 2 , 3 , 4 }; // okay, array length is now 5 array = { 9 , 8 , 7 }; // okay, array length is now 3 \u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0cvector\u53ef\u4ee5\u6839\u636e\u5143\u7d20\u7684\u4e2a\u6570\u8c03\u6574\u5176\u957f\u5ea6\u3002 \u81ea\u6211\u6e05\u7406\uff0c\u907f\u514d\u5185\u5b58\u6cc4\u6f0f \u5f53\u4e00\u4e2avector\u53d8\u91cf\u8d85\u51fa\u4f5c\u7528\u57df\u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u91ca\u653e\u5b83\u6240\u4f7f\u7528\u7684\u5185\u5b58(\u5982\u679c\u6709\u5fc5\u8981)\u3002\u8fd9\u4e0d\u4ec5\u65b9\u4fbf(\u56e0\u4e3a\u60a8\u4e0d\u5fc5\u81ea\u5df1\u505a)\uff0c\u8fd8\u6709\u52a9\u4e8e\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\u3002\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\uff1a 1 2 3 4 5 6 7 8 9 10 11 void doSomething ( bool earlyExit ) { int * array { new int [ 5 ] { 9 , 7 , 5 , 3 , 1 } }; // \u4f7f\u7528new\u5206\u914d\u5185\u5b58 if ( earlyExit ) return ; // \u9000\u51fa\u51fd\u6570\u800c\u4e0d\u4f1a\u91ca\u653e\u5185\u5b58 // do stuff here delete [] array ; // \u5e76\u4e0d\u4f1a\u88ab\u8c03\u7528 } COPY \u5982\u679c earlyExit \u88ab\u8bbe\u7f6e\u4e3a true \uff0c\u5219\u6570\u7ec4\u7684\u5185\u5b58\u4e0d\u4f1a\u88ab\u91ca\u653e\uff0c\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\u3002 \u4f46\u662f\uff0c\u5982\u679c array \u662f std::vector \u7684\u8bdd\uff0c\u5c31\u4e0d\u4f1a\u51fa\u73b0\u95ee\u9898\uff0c\u56e0\u4e3a array \u4f1a\u5728 \u79bb\u5f00\u4f5c\u7528\u57df \u65f6\u81ea\u52a8\u91ca\u653e\u5185\u5b58(\u4e0d\u8bba\u51fd\u6570\u662f\u5426\u6b63\u786e\u9000\u51fa)\u3002\u8fd9\u4e5f\u8bf4\u660e\u4f7f\u7528 std::vector \u6bd4\u81ea\u5df1\u7ba1\u7406\u5185\u5b58\u8981\u66f4\u52a0\u5b89\u5168\u3002 Vectors \u80fd\u591f\u4fdd\u5b58\u5176\u957f\u5ea6\u4fe1\u606f \u4e0d\u540c\u4e8e\u5185\u5efa\u7684\u52a8\u6001\u6570\u7ec4\uff08\u4e0d\u77e5\u9053\u5176\u6307\u5411\u6570\u7ec4\u7684\u7a0b\u5ea6\uff09\uff0c std::vector \u80fd\u591f\u4fdd\u5b58\u5176\u957f\u5ea6\u3002\u4f7f\u7528 size() \u51fd\u6570\u5c31\u53ef\u4ee5\u5f97\u77e5 vector \u7684\u957f\u5ea6\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include void printLength ( const std :: vector < int >& array ) { std :: cout << \"The length is: \" << array . size () << '\\n' ; } int main () { std :: vector array { 9 , 7 , 5 , 3 , 1 }; printLength ( array ); std :: vector < int > empty {}; printLength ( empty ); return 0 ; } \u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 The length is: 5 The length is: 0 \u548c std::array \u4e00\u6837\uff0c size() \u8fd4\u56de\u7684\u662f size_type \u7c7b\u578b\u7684\u503c (\u5b8c\u6574\u7684\u5f62\u5f0f\u4e3a std::vector::size_type )\uff0c\u5b83\u662f\u65e0\u7b26\u53f7\u6574\u578b\u6570\u3002 \u8c03\u6574 vector \u5927\u5c0f \u8c03\u6574\u5185\u7f6e\u7684\u52a8\u6001\u6570\u7ec4\u7684\u5927\u5c0f\u662f\u5f88\u590d\u6742\u7684\uff0c\u800c\u8c03\u6574 std::vector \u7684\u5927\u5c0f\u5219\u5f88\u7b80\u5355\uff0c\u8c03\u7528 resize() \u5373\u53ef\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: vector array { 0 , 1 , 2 }; array . resize ( 5 ); // set size to 5 std :: cout << \"The length is: \" << array . size () << '\\n' ; for ( int i : array ) std :: cout << i << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6253\u5370\uff1a 1 2 The length is: 5 0 1 2 0 0 \u6709\u4e24\u4ef6\u4e8b\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0c\u5f53\u8c03\u6574 vector \u7684\u5927\u5c0f\u65f6\uff0c\u5df2\u7ecf\u5b58\u5728\u7684\u5143\u7d20\u4f1a\u88ab\u4fdd\u5b58\u4e0b\u6765\u3002\u7b2c\u4e8c\uff0c\u65b0\u7684\u5143\u7d20\u4f1a\u88ab\u521d\u59cb\u5316\u4e3a\u8be5\u7c7b\u578b\u7684\u9ed8\u8ba4\u503c\uff08\u6574\u578b\u662f0\uff09\u3002 Vectors \u7684\u5927\u5c0f\u4e5f\u53ef\u4ee5\u7f29\u5c0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: vector array { 0 , 1 , 2 , 3 , 4 }; array . resize ( 3 ); // set length to 3 std :: cout << \"The length is: \" << array . size () << '\\n' ; for ( int i : array ) std :: cout << i << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 The length is: 3 0 1 2 \u8c03\u6574vector\u5927\u5c0f\u7684\u5f00\u9500\u662f\u5f88\u5927\u7684\uff0c\u56e0\u6b64\u5e94\u8be5\u5c3d\u91cf\u5c11\u505a\u3002\u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a\u5177\u6709\u7279\u5b9a\u6570\u91cf\u5143\u7d20\u7684\u5411\u91cf\uff0c\u4f46\u5728\u58f0\u660e\u65f6\u4e0d\u77e5\u9053\u5143\u7d20\u7684\u503c\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u9ed8\u8ba4\u5143\u7d20\u7684\u5411\u91cf\uff0c\u5982\u4e0b\u6240\u793a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include int main () { // \u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\uff0c\u521b\u5efa\u4e00\u4e2a\u5305\u542b5\u4e2a\u5143\u7d20\u7684vector // \u6bcf\u4e2a\u5143\u7d20\u90fd\u88ab\u521d\u59cb\u5316\u4e3a0\u3002\u5982\u679c\u4f60\u4f7f\u7528\u62ec\u53f7\u521d\u59cb\u5316\u7684\u8bdd\uff0c\u5219v // vector\u4f1a\u5305\u542b\u4e00\u4e2a\u5143\u7d20\uff0c\u503c\u4e3a5 std :: vector < int > array ( 5 ); std :: cout << \"The length is: \" << array . size () << '\\n' ; for ( int i : array ) std :: cout << i << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6253\u5370\uff1a 1 2 The length is: 5 0 0 0 0 0 \u5c0f\u8d34\u58eb \u6211\u4eec\u4f1a\u5728 16.7 - std::initializer_list \u4e2d\u4ecb\u7ecd \u76f4\u63a5\u521d\u59cb\u5316 \u548c \u62ec\u53f7\u521d\u59cb\u5316 \u7684\u4e0d\u540c\u3002\u7ecf\u9a8c\u6cd5\u5219\u662f\uff1a\u5bf9\u4e8e\u4f60\u4e0d\u60f3\u521d\u59cb\u5316\u7684\u5217\u8868\u578b\u7c7b\u578b\uff0c\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\u3002 \u538b\u7f29\u5e03\u5c14\u503c std::vector \u8fd8\u6709\u53e6\u5916\u4e00\u4e2a\u5f88\u9177\u7684\u5c0f\u7279\u6027\uff0c\u5b83\u4f1a\u628a8\u4e2a\u5e03\u5c14\u7c7b\u578b\u7684\u5143\u7d20\u538b\u7f29\u4e3a\u4e00\u4e2a\u5b57\u8282\uff01\u8fd9\u4e00\u5207\u90fd\u662f\u5728\u5e55\u540e\u5b8c\u6210\u7684\uff0c\u5bf9\u4f60\u4f7f\u7528 std::vector \u5e76\u6ca1\u6709\u5f71\u54cd\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: vector < bool > array { true , false , false , true , true }; std :: cout << \"The length is: \" << array . size () << '\\n' ; for ( int i : array ) std :: cout << i << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 The length is: 5 1 0 0 1 1 \u66f4\u591a\u5185\u5bb9 \u6ce8\u610f\uff0c\u672c\u8282\u8bfe\u53ea\u662f\u5bf9 std::vector \u8fdb\u884c\u7b80\u4ecb\uff0c\u5728 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a \u4e2d\uff0c\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5b83\u7684\u4e00\u4e9b\u989d\u5916\u7684\u529f\u80fd\uff0c\u5305\u62ec\u957f\u5ea6\u548c\u5bb9\u91cf\u7684\u533a\u522b\uff0c\u5e76\u4e14\u8be6\u7ec6\u4ecb\u7ecd std::vector \u662f\u5982\u4f55\u7ba1\u7406\u5185\u5b58\u7684\u3002 \u7ed3\u8bba \u56e0\u4e3a std::vector \u7c7b\u578b\u662f\u53d8\u91cf\u53ef\u4ee5\u81ea\u5df1\u7ba1\u7406\u5185\u5b58\uff08\u6709\u52a9\u4e8e\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\uff09\u5e76\u4e14\u80fd\u591f\u8bb0\u5f55\u81ea\u8eab\u957f\u5ea6\uff0c\u6240\u4ee5\u6211\u4eec\u63a8\u8350\u5728\u5927\u591a\u6570\u9700\u8981\u4f7f\u7528\u52a8\u6001\u6570\u7ec4\u7684\u65f6\u5019\uff0c\u4f7f\u7528 std::vector \u3002","title":"11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb"},{"location":"11-17-An-introduction-to-std-vector/#stdvector","text":"std::vector \u5728C++03\u4e2d\u88ab\u5f15\u5165\uff0c\u5b83\u63d0\u4f9b\u4e86 dynamic-array \u7684\u529f\u80fd\u5e76\u4e14\u53ef\u4ee5\u81ea\u6211\u7ba1\u7406\u5185\u5b58\u7684\u7533\u8bf7\u548c\u91ca\u653e\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u4f60\u53ef\u4ee5\u5728 \u8fd0\u884c\u65f6 \u521b\u5efa\u6570\u7ec4\u5e76\u6307\u5b9a\u5176\u957f\u5ea6\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528 new \u548c delete \u6765\u624b\u52a8\u7ba1\u7406\u5185\u5b58\u7684\u7533\u8bf7\u91ca\u653e\u3002 std::vector \u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d\u3002 \u58f0\u660e\u4e00\u4e2a std::vector \u5f88\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 #include // \u5728\u58f0\u660e\u65f6\u65e0\u9700\u6307\u5b9a\u957f\u5ea6 std :: vector < int > array ; std :: vector < int > array2 = { 9 , 7 , 5 , 3 , 1 }; // \u4f7f\u7528\u521d\u59cb\u5316\u503c\u5217\u8868\u8fdb\u884c\u521d\u59cb\u5316(\u5728C++11\u4e4b\u524d) std :: vector < int > array3 { 9 , 7 , 5 , 3 , 1 }; // \u4f7f\u7528\u7edf\u4e00\u521d\u59cb\u5316 //\u548cstd::array\u4e00\u6837\uff0c\u4eceC++17\u5f00\u59cb\uff0c\u7c7b\u578b\u53ef\u4ee5\u7701\u7565 std :: vector array4 { 9 , 7 , 5 , 3 , 1 }; // deduced to std::vector \u6ce8\u610f\uff0c\u4e0d\u8bba\u662f\u521d\u59cb\u5316\u4e0e\u5426\uff0c\u4f60\u90fd\u4e0d\u9700\u8981\u5728\u7f16\u8bd1\u65f6\u6307\u5b9a\u6570\u7ec4\u957f\u5ea6\u3002\u8fd9\u662f\u56e0\u4e3a std::vector \u53ef\u4ee5\u5728\u9700\u8981\u65f6\u81ea\u884c\u5206\u914d\u5185\u5b58\u3002 \u548c std::array \u4e00\u6837\uff0c\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u53ef\u4ee5\u4f7f\u7528 \u4e0b\u6807\u8fd0\u7b97\u7b26 \uff08\u4e0d\u8fdb\u884c\u8d8a\u754c\u68c0\u67e5\uff09\u6216 at() \u51fd\u6570\uff08\u8fdb\u884c\u8d8a\u754c\u68c0\u67e5\uff09\u8bbf\u95ee\uff1a 1 2 array [ 6 ] = 2 ; // no bounds checking array . at ( 7 ) = 3 ; // does bounds checking \u5bf9\u4e8e\u4e0a\u9762\u4e24\u79cd\u60c5\u51b5\uff0c\u5982\u679c\u4f60\u8bbf\u95ee\u8d85\u51fa\u6570\u7ec4\u7ed3\u5c3e\u7684\u5143\u7d20\uff0cvector\u5e76\u4e0d\u4f1a\u81ea\u52a8\u8c03\u6574\u957f\u5ea6\u3002 \u5728 C++11 \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 \u521d\u59cb\u5316\u503c\u5217\u8868 \u4e3a std::vector \u8d4b\u503c\uff1a 1 2 array = { 0 , 1 , 2 , 3 , 4 }; // okay, array length is now 5 array = { 9 , 8 , 7 }; // okay, array length is now 3 \u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0cvector\u53ef\u4ee5\u6839\u636e\u5143\u7d20\u7684\u4e2a\u6570\u8c03\u6574\u5176\u957f\u5ea6\u3002","title":"std::vector \u7b80\u4ecb"},{"location":"11-17-An-introduction-to-std-vector/#_1","text":"\u5f53\u4e00\u4e2avector\u53d8\u91cf\u8d85\u51fa\u4f5c\u7528\u57df\u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u91ca\u653e\u5b83\u6240\u4f7f\u7528\u7684\u5185\u5b58(\u5982\u679c\u6709\u5fc5\u8981)\u3002\u8fd9\u4e0d\u4ec5\u65b9\u4fbf(\u56e0\u4e3a\u60a8\u4e0d\u5fc5\u81ea\u5df1\u505a)\uff0c\u8fd8\u6709\u52a9\u4e8e\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\u3002\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\uff1a 1 2 3 4 5 6 7 8 9 10 11 void doSomething ( bool earlyExit ) { int * array { new int [ 5 ] { 9 , 7 , 5 , 3 , 1 } }; // \u4f7f\u7528new\u5206\u914d\u5185\u5b58 if ( earlyExit ) return ; // \u9000\u51fa\u51fd\u6570\u800c\u4e0d\u4f1a\u91ca\u653e\u5185\u5b58 // do stuff here delete [] array ; // \u5e76\u4e0d\u4f1a\u88ab\u8c03\u7528 } COPY \u5982\u679c earlyExit \u88ab\u8bbe\u7f6e\u4e3a true \uff0c\u5219\u6570\u7ec4\u7684\u5185\u5b58\u4e0d\u4f1a\u88ab\u91ca\u653e\uff0c\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\u3002 \u4f46\u662f\uff0c\u5982\u679c array \u662f std::vector \u7684\u8bdd\uff0c\u5c31\u4e0d\u4f1a\u51fa\u73b0\u95ee\u9898\uff0c\u56e0\u4e3a array \u4f1a\u5728 \u79bb\u5f00\u4f5c\u7528\u57df \u65f6\u81ea\u52a8\u91ca\u653e\u5185\u5b58(\u4e0d\u8bba\u51fd\u6570\u662f\u5426\u6b63\u786e\u9000\u51fa)\u3002\u8fd9\u4e5f\u8bf4\u660e\u4f7f\u7528 std::vector \u6bd4\u81ea\u5df1\u7ba1\u7406\u5185\u5b58\u8981\u66f4\u52a0\u5b89\u5168\u3002","title":"\u81ea\u6211\u6e05\u7406\uff0c\u907f\u514d\u5185\u5b58\u6cc4\u6f0f"},{"location":"11-17-An-introduction-to-std-vector/#vectors","text":"\u4e0d\u540c\u4e8e\u5185\u5efa\u7684\u52a8\u6001\u6570\u7ec4\uff08\u4e0d\u77e5\u9053\u5176\u6307\u5411\u6570\u7ec4\u7684\u7a0b\u5ea6\uff09\uff0c std::vector \u80fd\u591f\u4fdd\u5b58\u5176\u957f\u5ea6\u3002\u4f7f\u7528 size() \u51fd\u6570\u5c31\u53ef\u4ee5\u5f97\u77e5 vector \u7684\u957f\u5ea6\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include void printLength ( const std :: vector < int >& array ) { std :: cout << \"The length is: \" << array . size () << '\\n' ; } int main () { std :: vector array { 9 , 7 , 5 , 3 , 1 }; printLength ( array ); std :: vector < int > empty {}; printLength ( empty ); return 0 ; } \u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 The length is: 5 The length is: 0 \u548c std::array \u4e00\u6837\uff0c size() \u8fd4\u56de\u7684\u662f size_type \u7c7b\u578b\u7684\u503c (\u5b8c\u6574\u7684\u5f62\u5f0f\u4e3a std::vector::size_type )\uff0c\u5b83\u662f\u65e0\u7b26\u53f7\u6574\u578b\u6570\u3002","title":"Vectors \u80fd\u591f\u4fdd\u5b58\u5176\u957f\u5ea6\u4fe1\u606f"},{"location":"11-17-An-introduction-to-std-vector/#vector","text":"\u8c03\u6574\u5185\u7f6e\u7684\u52a8\u6001\u6570\u7ec4\u7684\u5927\u5c0f\u662f\u5f88\u590d\u6742\u7684\uff0c\u800c\u8c03\u6574 std::vector \u7684\u5927\u5c0f\u5219\u5f88\u7b80\u5355\uff0c\u8c03\u7528 resize() \u5373\u53ef\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: vector array { 0 , 1 , 2 }; array . resize ( 5 ); // set size to 5 std :: cout << \"The length is: \" << array . size () << '\\n' ; for ( int i : array ) std :: cout << i << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6253\u5370\uff1a 1 2 The length is: 5 0 1 2 0 0 \u6709\u4e24\u4ef6\u4e8b\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0c\u5f53\u8c03\u6574 vector \u7684\u5927\u5c0f\u65f6\uff0c\u5df2\u7ecf\u5b58\u5728\u7684\u5143\u7d20\u4f1a\u88ab\u4fdd\u5b58\u4e0b\u6765\u3002\u7b2c\u4e8c\uff0c\u65b0\u7684\u5143\u7d20\u4f1a\u88ab\u521d\u59cb\u5316\u4e3a\u8be5\u7c7b\u578b\u7684\u9ed8\u8ba4\u503c\uff08\u6574\u578b\u662f0\uff09\u3002 Vectors \u7684\u5927\u5c0f\u4e5f\u53ef\u4ee5\u7f29\u5c0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: vector array { 0 , 1 , 2 , 3 , 4 }; array . resize ( 3 ); // set length to 3 std :: cout << \"The length is: \" << array . size () << '\\n' ; for ( int i : array ) std :: cout << i << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 The length is: 3 0 1 2 \u8c03\u6574vector\u5927\u5c0f\u7684\u5f00\u9500\u662f\u5f88\u5927\u7684\uff0c\u56e0\u6b64\u5e94\u8be5\u5c3d\u91cf\u5c11\u505a\u3002\u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a\u5177\u6709\u7279\u5b9a\u6570\u91cf\u5143\u7d20\u7684\u5411\u91cf\uff0c\u4f46\u5728\u58f0\u660e\u65f6\u4e0d\u77e5\u9053\u5143\u7d20\u7684\u503c\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u9ed8\u8ba4\u5143\u7d20\u7684\u5411\u91cf\uff0c\u5982\u4e0b\u6240\u793a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include int main () { // \u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\uff0c\u521b\u5efa\u4e00\u4e2a\u5305\u542b5\u4e2a\u5143\u7d20\u7684vector // \u6bcf\u4e2a\u5143\u7d20\u90fd\u88ab\u521d\u59cb\u5316\u4e3a0\u3002\u5982\u679c\u4f60\u4f7f\u7528\u62ec\u53f7\u521d\u59cb\u5316\u7684\u8bdd\uff0c\u5219v // vector\u4f1a\u5305\u542b\u4e00\u4e2a\u5143\u7d20\uff0c\u503c\u4e3a5 std :: vector < int > array ( 5 ); std :: cout << \"The length is: \" << array . size () << '\\n' ; for ( int i : array ) std :: cout << i << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6253\u5370\uff1a 1 2 The length is: 5 0 0 0 0 0 \u5c0f\u8d34\u58eb \u6211\u4eec\u4f1a\u5728 16.7 - std::initializer_list \u4e2d\u4ecb\u7ecd \u76f4\u63a5\u521d\u59cb\u5316 \u548c \u62ec\u53f7\u521d\u59cb\u5316 \u7684\u4e0d\u540c\u3002\u7ecf\u9a8c\u6cd5\u5219\u662f\uff1a\u5bf9\u4e8e\u4f60\u4e0d\u60f3\u521d\u59cb\u5316\u7684\u5217\u8868\u578b\u7c7b\u578b\uff0c\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\u3002","title":"\u8c03\u6574 vector \u5927\u5c0f"},{"location":"11-17-An-introduction-to-std-vector/#_2","text":"std::vector \u8fd8\u6709\u53e6\u5916\u4e00\u4e2a\u5f88\u9177\u7684\u5c0f\u7279\u6027\uff0c\u5b83\u4f1a\u628a8\u4e2a\u5e03\u5c14\u7c7b\u578b\u7684\u5143\u7d20\u538b\u7f29\u4e3a\u4e00\u4e2a\u5b57\u8282\uff01\u8fd9\u4e00\u5207\u90fd\u662f\u5728\u5e55\u540e\u5b8c\u6210\u7684\uff0c\u5bf9\u4f60\u4f7f\u7528 std::vector \u5e76\u6ca1\u6709\u5f71\u54cd\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: vector < bool > array { true , false , false , true , true }; std :: cout << \"The length is: \" << array . size () << '\\n' ; for ( int i : array ) std :: cout << i << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 The length is: 5 1 0 0 1 1","title":"\u538b\u7f29\u5e03\u5c14\u503c"},{"location":"11-17-An-introduction-to-std-vector/#_3","text":"\u6ce8\u610f\uff0c\u672c\u8282\u8bfe\u53ea\u662f\u5bf9 std::vector \u8fdb\u884c\u7b80\u4ecb\uff0c\u5728 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a \u4e2d\uff0c\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5b83\u7684\u4e00\u4e9b\u989d\u5916\u7684\u529f\u80fd\uff0c\u5305\u62ec\u957f\u5ea6\u548c\u5bb9\u91cf\u7684\u533a\u522b\uff0c\u5e76\u4e14\u8be6\u7ec6\u4ecb\u7ecd std::vector \u662f\u5982\u4f55\u7ba1\u7406\u5185\u5b58\u7684\u3002","title":"\u66f4\u591a\u5185\u5bb9"},{"location":"11-17-An-introduction-to-std-vector/#_4","text":"\u56e0\u4e3a std::vector \u7c7b\u578b\u662f\u53d8\u91cf\u53ef\u4ee5\u81ea\u5df1\u7ba1\u7406\u5185\u5b58\uff08\u6709\u52a9\u4e8e\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\uff09\u5e76\u4e14\u80fd\u591f\u8bb0\u5f55\u81ea\u8eab\u957f\u5ea6\uff0c\u6240\u4ee5\u6211\u4eec\u63a8\u8350\u5728\u5927\u591a\u6570\u9700\u8981\u4f7f\u7528\u52a8\u6001\u6570\u7ec4\u7684\u65f6\u5019\uff0c\u4f7f\u7528 std::vector \u3002","title":"\u7ed3\u8bba"},{"location":"11-18-Introduction-to-iterators/","tags":["iterator"],"text":"Key Takeaway \u6570\u7ec4\uff08\u6216\u5176\u4ed6\u6570\u636e\u7ed3\u6784\uff09\u7684\u904d\u5386\u662f\u7f16\u7a0b\u4e2d\u4e00\u9879\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u4ecb\u7ecd\u8fc7\u591a\u79cd\u904d\u5386\u65b9\u6cd5\uff1a\u4f7f\u7528\u5faa\u73af\u548c\u7d22\u5f15( for-loops \u548c while loops )\uff0c\u4f7f\u7528\u6307\u9488\u548c\u6307\u9488\u8fd0\u7b97\uff0c\u4f7f\u7528 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include #include #include int main () { // In C++17, the type of variable data is deduced to std::array // If you get an error compiling this example, see the warning below std :: array data { 0 , 1 , 2 , 3 , 4 , 5 , 6 }; std :: size_t length { std :: size ( data ) }; // while-loop with explicit index std :: size_t index { 0 }; while ( index < length ) { std :: cout << data [ index ] << ' ' ; ++ index ; } std :: cout << '\\n' ; // for-loop with explicit index for ( index = 0 ; index < length ; ++ index ) { std :: cout << data [ index ] << ' ' ; } std :: cout << '\\n' ; // for-loop with pointer (Note: ptr can't be const, because we increment it) for ( auto ptr { & data [ 0 ] }; ptr != ( & data [ 0 ] + length ); ++ ptr ) { std :: cout << * ptr << ' ' ; } std :: cout << '\\n' ; // ranged-based for loop for ( int i : data ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u6ce8\u610f \u6ce8\u610f\uff0c\u672c\u8282\u8bfe\u7684\u4f8b\u5b50\u4f7f\u7528\u4e86C++17\u7684\u7279\u6027\uff1a \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad \u3002\u5373\u901a\u8fc7\u53d8\u91cf\u7684\u521d\u59cb\u5316\u503c\u63a8\u65ad\u6a21\u677f\u5b9e\u53c2\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53\u7f16\u8bd1\u5668\u770b\u5230 std::array data{ 0, 1, 2, 3, 4, 5, 6 }; \u65f6\uff0c\u5b83\u4f1a\u63a8\u65ad\u51fa\u6211\u4eec\u5e0c\u671b\u4f7f\u7528 std::array data { 0, 1, 2, 3, 4, 5, 6 }; \u3002 \u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u4e0d\u652f\u6301C++17\uff0c\u4f60\u4f1a\u5f97\u5230\u8fd9\u6837\u7684\u7f16\u8bd1\u9519\u8bef\uff1a\u201cmissing template arguments before \u2018data\u2019\u201d\u3002\u6b64\u65f6\u4f60\u6700\u597d\u6309\u7167 0.12 -- Configuring your compiler: Choosing a language standard \u4ecb\u7ecd\u7684\u65b9\u6cd5\u6253\u5f00C++17\u652f\u6301\u3002\u5982\u679c\u5b9e\u5728\u4e0d\u884c\uff0c\u4f60\u53ef\u4ee5\u5c06\u8fd9\u4e00\u884c\u66ff\u6362\u4e3a\u663e\u5f0f\u7684\u58f0\u660e(e.g. replace std::array data{ 0, 1, 2, 3, 4, 5, 6 }; with std::array data { 0, 1, 2, 3, 4, 5, 6 }; \u5982\u679c\u6211\u4eec\u53ea\u9700\u8981\u901a\u8fc7\u7d22\u5f15\u83b7\u53d6\u5143\u7d20\u7684\u8bdd\uff08\u800c\u4e0d\u9700\u8981\u4f7f\u7528\u7d22\u5f15\uff09\uff0c\u90a3\u4e48\u4f7f\u7528\u57fa\u4e8e\u7d22\u5f15\u7684\u904d\u5386\u9700\u8981\u5f88\u591a\u4e0d\u5fc5\u8981\u7684\u952e\u76d8\u8f93\u5165\u3002\u800c\u4e14\uff0c\u8be5\u65b9\u6cd5\u4e5f\u4ec5\u4ec5\u9002\u7528\u4e8e\u652f\u6301\u5143\u7d20\u76f4\u63a5\u8bbf\u95ee\u7684\u5bb9\u5668\uff08\u6570\u7ec4\u652f\u6301\uff0c\u4f46\u662f\u5176\u4ed6\u7c7b\u578b\u7684\u5bb9\u5668\u4f8b\u5982\u94fe\u8868\u5c31\u4e0d\u652f\u6301\uff09\u3002 \u4f7f\u7528\u6307\u9488\u904d\u5386\u66f4\u662f\u5570\u55e6\uff0c\u800c\u4e14\u5bf9\u4e8e\u4e0d\u4e86\u89e3\u6307\u9488\u8fd0\u7b97\u89c4\u5219\u7684\u4eba\u6765\u8bf4\uff0c\u53ef\u8bfb\u6027\u975e\u5e38\u5dee\u3002\u6307\u9488\u904d\u5386\u4e5f\u53ea\u9002\u7528\u4e8e\u5143\u7d20\u5728\u5185\u5b58\u4e2d\u8fde\u7eed\u5b58\u5728\u7684\u60c5\u51b5\uff08\u6570\u7ec4\u662f\u8fd9\u6837\u7684\uff0c\u4f46\u662f\u5176\u4ed6\u7c7b\u578b\u7684\u5bb9\u5668\u53ef\u4e0d\u662f\uff0c\u4f8b\u5982\u94fe\u8868\u3001\u6811\u548c\u6620\u5c04\uff09\u3002 \u6269\u5c55\u9605\u8bfb \u6307\u9488(\u5728\u4e0d\u4f7f\u7528\u6307\u9488\u8fd0\u7b97\u65f6)\u4e5f\u53ef\u4ee5\u7528\u4e8e\u904d\u5386\u4e00\u4e9b\u975e\u987a\u5e8f\u578b\u6570\u636e\u7ed3\u6784\u3002\u5728\u4e00\u4e2a\u94fe\u8868\u4e2d\uff0c\u6bcf\u4e2a\u5143\u7d20\u90fd\u901a\u8fc7\u6307\u9488\u6307\u5411\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u6cbf\u7740\u6307\u9488\u94fe\u904d\u5386\u5404\u4e2a\u5143\u7d20\u3002 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u5c31\u6709\u610f\u601d\u4e86\uff0c\u5b83\u904d\u5386\u5bb9\u5668\u7684\u5177\u4f53\u673a\u5236\u9690\u85cf\u4e8e\u5e55\u540e\u2014\u2014\u800c\u4e14\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e\u5404\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\uff08\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6811\u3001\u6620\u5c04\u7b49\uff09\u3002\u5b83\u7684\u539f\u7406\u662f\u4ec0\u4e48\u5462\uff1f\u5b83\u4f7f\u7528\u4e86\u8fed\u4ee3\u5668\u3002 \u8fed\u4ee3\u5668 \u8fed\u4ee3\u5668 \u662f\u4e00\u4e2a\u7528\u4e8e\u904d\u5386\u5bb9\u5668\uff08\u4f8b\u5982\u6570\u7ec4\u4e2d\u7684\u503c\u6216\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\uff09\u7684\u5bf9\u8c61\uff0c\u5b83\u5728\u79fb\u52a8\u7684\u8fc7\u7a0b\u4e2d\u63d0\u4f9b\u4e86\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8bbf\u95ee\u7684\u80fd\u529b\u3002 \u5bb9\u5668\u53ef\u4ee5\u63d0\u4f9b\u4e0d\u540c\u7c7b\u578b\u7684\u8fed\u4ee3\u5668\u3002\u4f8b\u5982\uff0c\u6570\u7ec4\u5bb9\u5668\u4f1a\u63d0\u4f9b\u5411\u524d\u904d\u5386\u6574\u4e2a\u6570\u7ec4\u7684\u524d\u5411\u8fed\u4ee3\u5668\u548c\u7528\u4e8e\u9006\u5e8f\u904d\u5386\u6570\u7ec4\u7684\u9006\u5e8f\u8fed\u4ee3\u5668\u3002 \u4e00\u65e6\u521b\u5efa\u4e86\u5408\u9002\u7684\u8fed\u4ee3\u5668\uff0c\u7a0b\u5e8f\u5458\u5c31\u53ef\u4ee5\u4f7f\u7528\u8be5\u8fed\u4ee3\u5668\u63d0\u4f9b\u7684\u63a5\u53e3\u6765\u904d\u5386\u548c\u8bbf\u95ee\u5bb9\u5668\u4e2d\u7684\u5143\u7d20\uff0c\u800c\u4e0d\u9700\u8981\u64cd\u5fc3\u5177\u4f53\u7684\u904d\u5386\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff0c\u4e5f\u4e0d\u9700\u8981\u64cd\u5fc3\u5bb9\u5668\u4e2d\u7684\u6570\u636e\u662f\u5982\u4f55\u5b58\u50a8\u7684\u3002\u540c\u65f6\uff0c\u56e0\u4e3aC++\u4e2d\u7684\u8fed\u4ee3\u5668\u901a\u5e38\u63d0\u4f9b\u4e86\u76f8\u540c\u7684\u63a5\u53e3\u7528\u4e8e\u904d\u5386\uff08 ++ \u64cd\u4f5c\u7b26\u7528\u4e8e\u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5143\u7d20\uff09\u548c\u8bbf\u95ee\uff08 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u7528\u4e8e\u8bbf\u95ee\u5143\u7d20\uff09\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u81f4\u7684\u5ba2\u6237\u7aef\u4ee3\u7801\u6765\u904d\u5386\u5404\u79cd\u5404\u6837\u7684\u5bb9\u5668\u3002 \u6307\u9488\u4f5c\u4e3a\u8fed\u4ee3\u5668 \u5176\u5b9e\uff0c\u6700\u7b80\u5355\u7684\u8fed\u4ee3\u5668\u5c31\u662f\u6307\u9488\u3002\u6307\u9488\uff08\u4f7f\u7528\u6307\u9488\u8fd0\u7b97\uff09\u53ef\u4ee5\u7528\u4e8e\u904d\u5386\u987a\u5e8f\u5b58\u653e\u5728\u5185\u5b58\u4e2d\u7684\u6570\u636e\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5148\u590d\u4e60\u4e00\u4e0b\u6307\u9488\u548c\u6307\u9488\u8fd0\u7b97\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include int main () { std :: array data { 0 , 1 , 2 , 3 , 4 , 5 , 6 }; auto begin { & data [ 0 ] }; // note that this points to one spot beyond the last element auto end { begin + std :: size ( data ) }; // for-loop with pointer for ( auto ptr { begin }; ptr != end ; ++ ptr ) // ++ to move to next element { std :: cout << * ptr << ' ' ; // Indirection to get value of current element } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\uff1a 1 0 1 2 3 4 5 6 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u53d8\u91cf\uff1a begin (\u6307\u5411\u5bb9\u5668\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20)\u548c end (\u6807\u8bb0\u4e3a\u7ed3\u675f\u70b9)\u3002\u5bf9\u4e8e\u6570\u7ec4\u4e2d\u6765\u8bf4\uff0c\u6211\u4eec\u901a\u5e38\u5047\u5b9a\u6570\u7ec4\u591a\u5305\u542b\u4e00\u4e2a\u5143\u7d20\uff0c\u800c\u7ed3\u675f\u6807\u8bb0\u5e94\u8bbe\u7f6e\u4e3a\u8be5\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002 \u7136\u540e\uff0c\u6307\u9488\u4ece begin \u904d\u5386\u5230 end \uff0c\u800c\u4e14\u6240\u5904\u4f4d\u7f6e\u7684\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u8bbf\u95ee\u3002 \u6ce8\u610f \u4f60\u53ef\u80fd\u4f1a\u5c1d\u8bd5\u4f7f\u7528\u53d6\u5730\u5740\u548c\u6570\u7ec4\u8bed\u6cd5\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\u83b7\u53d6\u7ed3\u675f\u6807\u8bb0\uff1a 1 int * end { & data [ std :: size ( data )] }; \u8fd9\u4e48\u505a\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \uff0c\u56e0\u4e3a\u5bf9\u4e8e\u4e0b\u6807\u6765\u8bf4\uff0c data[std::size(data)] \u6240\u8bbf\u95ee\u7684\u5143\u7d20\u5df2\u7ecf\u8d8a\u754c\u4e86\u3002 \u5e94\u8be5\u8fd9\u4e48\u505a 1 int * end { data . data () + std :: size ( data ) }; // data() \u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5143\u7d20 \u6807\u51c6\u5e93\u8fed\u4ee3\u5668 \u904d\u5386\u662f\u5982\u6b64\u7684\u666e\u904d\uff0c\u6240\u4ee5\u6807\u51c6\u5e93\u4e2d\u6240\u6709\u7684\u5bb9\u5668\u90fd\u652f\u6301\u8fed\u4ee3\u3002\u800c\u4e14\u4e0d\u9700\u8981\u4f7f\u7528\u6307\u9488\u5e76\u8ba1\u7b97\u8d77\u6b62\u70b9\u7684\u4f4d\u7f6e\uff0c\u800c\u662f\u53ef\u4ee5\u51fd\u6570 begin() \u548c end() \u76f4\u63a5\u83b7\u53d6\u8fed\u4ee3\u5668\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include int main () { std :: array array { 1 , 2 , 3 }; // \u901a\u8fc7begin\u548cend\u51fd\u6570\u76f4\u63a5\u83b7\u53d6\u8d77\u6b62\u70b9 auto begin { array . begin () }; auto end { array . end () }; for ( auto p { begin }; p != end ; ++ p ) // ++ \u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5143\u7d20 { std :: cout << * p << ' ' ; // \u95f4\u63a5\u8bbf\u95ee\u5f53\u524d\u5143\u7d20 } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 1 2 3 iterator \u5934\u6587\u4ef6\u8fd8\u5305\u542b\u4e86\u4e24\u4e2a\u6cdb\u578b\u51fd\u6570 std::begin \u548c std::end \uff0c\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u8fed\u4ee3\u5668\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include #include // For std::begin and std::end int main () { std :: array array { 1 , 2 , 3 }; // Use std::begin and std::end to get the begin and end points. auto begin { std :: begin ( array ) }; auto end { std :: end ( array ) }; for ( auto p { begin }; p != end ; ++ p ) // ++ to move to next element { std :: cout << * p << ' ' ; // Indirection to get value of current element } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u4e00\u6837\uff1a 1 1 2 3 \u73b0\u5728\u4e0d\u8981\u62c5\u5fc3\u8fed\u4ee3\u5668\u7684\u7c7b\u578b\uff0c\u6211\u4eec\u5c06\u5728\u540e\u9762\u7684\u7ae0\u8282\u4e2d\u518d\u6b21\u4ecb\u7ecd\u8fed\u4ee3\u5668\u3002\u91cd\u8981\u7684\u662f\uff0c\u8fed\u4ee3\u5668\u8d1f\u8d23\u5904\u7406\u904d\u5386\u5bb9\u5668\u6240\u9700\u7684\u5404\u9879\u7ec6\u8282\u3002\u6211\u4eec\u53ea\u9700\u8981\u56db\u4e2a\u4e1c\u897f\uff1a\u8d77\u70b9\u3001\u7ec8\u70b9\u3001\u5c06\u8fed\u4ee3\u5668\u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5143\u7d20(\u6216\u7ed3\u675f)\u7684\u64cd\u4f5c\u7b26++\u548c\u83b7\u53d6\u5f53\u524d\u5143\u7d20\u503c\u7684\u64cd\u4f5c\u7b26*\u3002 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u6240\u6709\u5177\u6709 begin() \u548c end() \u6210\u5458\u51fd\u6570\u7684\u7c7b\u578b\uff0c\u6216\u8005\u53ef\u4ee5\u914d\u5408 std::begin() \u548c std::end() \u4f7f\u7528\u7684\u7c7b\u578b\uff0c\u90fd\u53ef\u4ee5\u7528\u4e8e \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include int main () { std :: array array { 1 , 2 , 3 }; // This does exactly the same as the loop we used before. for ( int i : array ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u5b9e\u9645\u4e0a\uff0c\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af\u4f1a\u5728\u80cc\u5730\u91cc\u8c03\u7528\u88ab\u904d\u5386\u7c7b\u578b\u7684 begin() \u548c end() \u3002 std::array \u662f\u5177\u6709 begin \u548c end \u6210\u5458\u51fd\u6570\u7684\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u88ab\u7528\u4e8e\u57fa\u4e8e\u8303\u56f4\u7684\u5faa\u73af\u3002C\u8bed\u8a00\u98ce\u683c\u7684\u56fa\u5b9a\u6570\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7 std::begin \u548c std::end \u6765\u83b7\u53d6\u8d77\u6b62\u70b9\uff0c\u6240\u4ee5\u5b83\u4e5f\u53ef\u4ee5\u88ab\u7528\u4e8e\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af\u3002\u52a8\u6001\u6570\u7ec4\u5c31\u4e0d\u597d\u4f7f\u4e86\uff0c\u56e0\u4e3a std::end \u51fd\u6570\u5bf9\u5b83\u65e0\u6548\uff08\u7c7b\u578b\u4fe1\u606f\u4e0d\u5305\u542b\u6570\u7ec4\u957f\u5ea6\uff09\u3002 \u7a0d\u540e\u4f60\u5c06\u5b66\u4e60\u5982\u4f55\u5411\u7c7b\u578b\u4e2d\u6dfb\u52a0\u51fd\u6570\uff0c\u4ee5\u4fbf\u5b83\u4eec\u4e5f\u53ef\u4ee5\u88ab\u7528\u4e8e\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af\u3002 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u5e76\u4e0d\u662f\u552f\u4e00\u4f7f\u7528\u8fed\u4ee3\u5668\u7684\u5730\u65b9\u3002\u8fed\u4ee3\u5668\u4e5f\u7528\u5728 std::sort \u548c\u5176\u4ed6\u7b97\u6cd5\u4e2d\u3002\u5728\u4e86\u89e3\u8fed\u4ee3\u5668\u4e4b\u540e\u4f60\u4f1a\u53d1\u73b0\uff0c\u6807\u51c6\u5e93\u4e2d\u5927\u91cf\u5730\u4f7f\u7528\u4e86\u8fed\u4ee3\u5668\u3002 \u65e0\u6548\u8fed\u4ee3\u5668\uff08\u60ac\u5782\u8fed\u4ee3\u5668\uff09 Much like pointers and references, iterators can be left \u201cdangling\u201d if the elements being iterated over change address or are destroyed. When this happens, we say the iterator has been invalidated. Accessing an invalidated iterator produces undefined behavior. \u5c31\u50cf\u6307\u9488\u548c\u5f15\u7528\u4e00\u6837\uff0c\u5982\u679c\u8fed\u4ee3\u5668\u7684\u5143\u7d20\u5730\u5740\u88ab\u4fee\u6539\u6216\u672c\u8eab\u88ab\u9500\u6bc1\u65f6\uff0c\u8fed\u4ee3\u5668\u53ef\u80fd\u4f1a\u5904\u4e8e \u60ac\u5782 \u72b6\u6001\u3002\u5f53\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\u65f6\uff0c\u6211\u4eec\u8bf4\u8fed\u4ee3\u5668\u5df2\u5931\u6548\u3002\u8bbf\u95ee\u65e0\u6548\u7684\u8fed\u4ee3\u5668\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e00\u4e9b\u4fee\u6539\u5bb9\u5668\u7684\u64cd\u4f5c(\u4f8b\u5982\u5411 std::vector \u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20)\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5bb9\u5668\u4e2d\u7684\u5143\u7d20\u5730\u5740\u6539\u53d8\u3002\u5f53\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\u65f6\uff0c\u6307\u5411\u8fd9\u4e9b\u5143\u7d20\u7684\u73b0\u6709\u8fed\u4ee3\u5668\u5c06\u5931\u6548\u3002\u597d\u7684C++\u53c2\u8003\u6587\u6863\u5e94\u8be5\u8bf4\u660e\u54ea\u4e9b\u5bb9\u5668\u64cd\u4f5c\u53ef\u80fd\u6216\u5c06\u4f7f\u8fed\u4ee3\u5668\u5931\u6548\u3002 \u201cIterator invalidation\u201d section of std::vector on cppreference \u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u3002 \u5931\u6548\u7684\u4f8b\u5b50\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include int main () { std :: vector v { 1 , 2 , 3 , 4 , 5 , 6 , 7 }; auto it { v . begin () }; ++ it ; // \u79fb\u52a8\u5230\u7b2c\u4e8c\u4e2a\u5143\u7d20 std :: cout << * it << '\\n' ; // ok: \u6253\u53702 v . erase ( it ); // erase \u5f53\u524d\u904d\u5386\u5230\u7684\u5143\u7d20 // erase() \u4f7f\u5f97\u88ab\u6307\u5411\u88ab\u5220\u9664\u5143\u7d20\u7684\u8fed\u4ee3\u5668\u5931\u6548 // \u6240\u4ee5\u8fed\u4ee3\u5668\"it\" \u73b0\u5728\u662f\u65e0\u6548\u7684 ++ it ; // \u672a\u5b9a\u4e49\u884c\u4e3a std :: cout << * it << '\\n' ; // \u672a\u5b9a\u4e49\u884c\u4e3a return 0 ; }","title":"11.18 \u2014 \u8fed\u4ee3\u5668\u7b80\u4ecb"},{"location":"11-18-Introduction-to-iterators/#_1","text":"\u8fed\u4ee3\u5668 \u662f\u4e00\u4e2a\u7528\u4e8e\u904d\u5386\u5bb9\u5668\uff08\u4f8b\u5982\u6570\u7ec4\u4e2d\u7684\u503c\u6216\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\uff09\u7684\u5bf9\u8c61\uff0c\u5b83\u5728\u79fb\u52a8\u7684\u8fc7\u7a0b\u4e2d\u63d0\u4f9b\u4e86\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8bbf\u95ee\u7684\u80fd\u529b\u3002 \u5bb9\u5668\u53ef\u4ee5\u63d0\u4f9b\u4e0d\u540c\u7c7b\u578b\u7684\u8fed\u4ee3\u5668\u3002\u4f8b\u5982\uff0c\u6570\u7ec4\u5bb9\u5668\u4f1a\u63d0\u4f9b\u5411\u524d\u904d\u5386\u6574\u4e2a\u6570\u7ec4\u7684\u524d\u5411\u8fed\u4ee3\u5668\u548c\u7528\u4e8e\u9006\u5e8f\u904d\u5386\u6570\u7ec4\u7684\u9006\u5e8f\u8fed\u4ee3\u5668\u3002 \u4e00\u65e6\u521b\u5efa\u4e86\u5408\u9002\u7684\u8fed\u4ee3\u5668\uff0c\u7a0b\u5e8f\u5458\u5c31\u53ef\u4ee5\u4f7f\u7528\u8be5\u8fed\u4ee3\u5668\u63d0\u4f9b\u7684\u63a5\u53e3\u6765\u904d\u5386\u548c\u8bbf\u95ee\u5bb9\u5668\u4e2d\u7684\u5143\u7d20\uff0c\u800c\u4e0d\u9700\u8981\u64cd\u5fc3\u5177\u4f53\u7684\u904d\u5386\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff0c\u4e5f\u4e0d\u9700\u8981\u64cd\u5fc3\u5bb9\u5668\u4e2d\u7684\u6570\u636e\u662f\u5982\u4f55\u5b58\u50a8\u7684\u3002\u540c\u65f6\uff0c\u56e0\u4e3aC++\u4e2d\u7684\u8fed\u4ee3\u5668\u901a\u5e38\u63d0\u4f9b\u4e86\u76f8\u540c\u7684\u63a5\u53e3\u7528\u4e8e\u904d\u5386\uff08 ++ \u64cd\u4f5c\u7b26\u7528\u4e8e\u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5143\u7d20\uff09\u548c\u8bbf\u95ee\uff08 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u7528\u4e8e\u8bbf\u95ee\u5143\u7d20\uff09\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u81f4\u7684\u5ba2\u6237\u7aef\u4ee3\u7801\u6765\u904d\u5386\u5404\u79cd\u5404\u6837\u7684\u5bb9\u5668\u3002","title":"\u8fed\u4ee3\u5668"},{"location":"11-18-Introduction-to-iterators/#_2","text":"\u5176\u5b9e\uff0c\u6700\u7b80\u5355\u7684\u8fed\u4ee3\u5668\u5c31\u662f\u6307\u9488\u3002\u6307\u9488\uff08\u4f7f\u7528\u6307\u9488\u8fd0\u7b97\uff09\u53ef\u4ee5\u7528\u4e8e\u904d\u5386\u987a\u5e8f\u5b58\u653e\u5728\u5185\u5b58\u4e2d\u7684\u6570\u636e\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5148\u590d\u4e60\u4e00\u4e0b\u6307\u9488\u548c\u6307\u9488\u8fd0\u7b97\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include int main () { std :: array data { 0 , 1 , 2 , 3 , 4 , 5 , 6 }; auto begin { & data [ 0 ] }; // note that this points to one spot beyond the last element auto end { begin + std :: size ( data ) }; // for-loop with pointer for ( auto ptr { begin }; ptr != end ; ++ ptr ) // ++ to move to next element { std :: cout << * ptr << ' ' ; // Indirection to get value of current element } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\uff1a 1 0 1 2 3 4 5 6 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u53d8\u91cf\uff1a begin (\u6307\u5411\u5bb9\u5668\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20)\u548c end (\u6807\u8bb0\u4e3a\u7ed3\u675f\u70b9)\u3002\u5bf9\u4e8e\u6570\u7ec4\u4e2d\u6765\u8bf4\uff0c\u6211\u4eec\u901a\u5e38\u5047\u5b9a\u6570\u7ec4\u591a\u5305\u542b\u4e00\u4e2a\u5143\u7d20\uff0c\u800c\u7ed3\u675f\u6807\u8bb0\u5e94\u8bbe\u7f6e\u4e3a\u8be5\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002 \u7136\u540e\uff0c\u6307\u9488\u4ece begin \u904d\u5386\u5230 end \uff0c\u800c\u4e14\u6240\u5904\u4f4d\u7f6e\u7684\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u8bbf\u95ee\u3002 \u6ce8\u610f \u4f60\u53ef\u80fd\u4f1a\u5c1d\u8bd5\u4f7f\u7528\u53d6\u5730\u5740\u548c\u6570\u7ec4\u8bed\u6cd5\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\u83b7\u53d6\u7ed3\u675f\u6807\u8bb0\uff1a 1 int * end { & data [ std :: size ( data )] }; \u8fd9\u4e48\u505a\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \uff0c\u56e0\u4e3a\u5bf9\u4e8e\u4e0b\u6807\u6765\u8bf4\uff0c data[std::size(data)] \u6240\u8bbf\u95ee\u7684\u5143\u7d20\u5df2\u7ecf\u8d8a\u754c\u4e86\u3002 \u5e94\u8be5\u8fd9\u4e48\u505a 1 int * end { data . data () + std :: size ( data ) }; // data() \u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5143\u7d20","title":"\u6307\u9488\u4f5c\u4e3a\u8fed\u4ee3\u5668"},{"location":"11-18-Introduction-to-iterators/#_3","text":"\u904d\u5386\u662f\u5982\u6b64\u7684\u666e\u904d\uff0c\u6240\u4ee5\u6807\u51c6\u5e93\u4e2d\u6240\u6709\u7684\u5bb9\u5668\u90fd\u652f\u6301\u8fed\u4ee3\u3002\u800c\u4e14\u4e0d\u9700\u8981\u4f7f\u7528\u6307\u9488\u5e76\u8ba1\u7b97\u8d77\u6b62\u70b9\u7684\u4f4d\u7f6e\uff0c\u800c\u662f\u53ef\u4ee5\u51fd\u6570 begin() \u548c end() \u76f4\u63a5\u83b7\u53d6\u8fed\u4ee3\u5668\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include int main () { std :: array array { 1 , 2 , 3 }; // \u901a\u8fc7begin\u548cend\u51fd\u6570\u76f4\u63a5\u83b7\u53d6\u8d77\u6b62\u70b9 auto begin { array . begin () }; auto end { array . end () }; for ( auto p { begin }; p != end ; ++ p ) // ++ \u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5143\u7d20 { std :: cout << * p << ' ' ; // \u95f4\u63a5\u8bbf\u95ee\u5f53\u524d\u5143\u7d20 } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 1 2 3 iterator \u5934\u6587\u4ef6\u8fd8\u5305\u542b\u4e86\u4e24\u4e2a\u6cdb\u578b\u51fd\u6570 std::begin \u548c std::end \uff0c\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u8fed\u4ee3\u5668\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include #include // For std::begin and std::end int main () { std :: array array { 1 , 2 , 3 }; // Use std::begin and std::end to get the begin and end points. auto begin { std :: begin ( array ) }; auto end { std :: end ( array ) }; for ( auto p { begin }; p != end ; ++ p ) // ++ to move to next element { std :: cout << * p << ' ' ; // Indirection to get value of current element } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u4e00\u6837\uff1a 1 1 2 3 \u73b0\u5728\u4e0d\u8981\u62c5\u5fc3\u8fed\u4ee3\u5668\u7684\u7c7b\u578b\uff0c\u6211\u4eec\u5c06\u5728\u540e\u9762\u7684\u7ae0\u8282\u4e2d\u518d\u6b21\u4ecb\u7ecd\u8fed\u4ee3\u5668\u3002\u91cd\u8981\u7684\u662f\uff0c\u8fed\u4ee3\u5668\u8d1f\u8d23\u5904\u7406\u904d\u5386\u5bb9\u5668\u6240\u9700\u7684\u5404\u9879\u7ec6\u8282\u3002\u6211\u4eec\u53ea\u9700\u8981\u56db\u4e2a\u4e1c\u897f\uff1a\u8d77\u70b9\u3001\u7ec8\u70b9\u3001\u5c06\u8fed\u4ee3\u5668\u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5143\u7d20(\u6216\u7ed3\u675f)\u7684\u64cd\u4f5c\u7b26++\u548c\u83b7\u53d6\u5f53\u524d\u5143\u7d20\u503c\u7684\u64cd\u4f5c\u7b26*\u3002","title":"\u6807\u51c6\u5e93\u8fed\u4ee3\u5668"},{"location":"11-18-Introduction-to-iterators/#for","text":"\u6240\u6709\u5177\u6709 begin() \u548c end() \u6210\u5458\u51fd\u6570\u7684\u7c7b\u578b\uff0c\u6216\u8005\u53ef\u4ee5\u914d\u5408 std::begin() \u548c std::end() \u4f7f\u7528\u7684\u7c7b\u578b\uff0c\u90fd\u53ef\u4ee5\u7528\u4e8e \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include int main () { std :: array array { 1 , 2 , 3 }; // This does exactly the same as the loop we used before. for ( int i : array ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u5b9e\u9645\u4e0a\uff0c\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af\u4f1a\u5728\u80cc\u5730\u91cc\u8c03\u7528\u88ab\u904d\u5386\u7c7b\u578b\u7684 begin() \u548c end() \u3002 std::array \u662f\u5177\u6709 begin \u548c end \u6210\u5458\u51fd\u6570\u7684\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u88ab\u7528\u4e8e\u57fa\u4e8e\u8303\u56f4\u7684\u5faa\u73af\u3002C\u8bed\u8a00\u98ce\u683c\u7684\u56fa\u5b9a\u6570\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7 std::begin \u548c std::end \u6765\u83b7\u53d6\u8d77\u6b62\u70b9\uff0c\u6240\u4ee5\u5b83\u4e5f\u53ef\u4ee5\u88ab\u7528\u4e8e\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af\u3002\u52a8\u6001\u6570\u7ec4\u5c31\u4e0d\u597d\u4f7f\u4e86\uff0c\u56e0\u4e3a std::end \u51fd\u6570\u5bf9\u5b83\u65e0\u6548\uff08\u7c7b\u578b\u4fe1\u606f\u4e0d\u5305\u542b\u6570\u7ec4\u957f\u5ea6\uff09\u3002 \u7a0d\u540e\u4f60\u5c06\u5b66\u4e60\u5982\u4f55\u5411\u7c7b\u578b\u4e2d\u6dfb\u52a0\u51fd\u6570\uff0c\u4ee5\u4fbf\u5b83\u4eec\u4e5f\u53ef\u4ee5\u88ab\u7528\u4e8e\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af\u3002 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u5e76\u4e0d\u662f\u552f\u4e00\u4f7f\u7528\u8fed\u4ee3\u5668\u7684\u5730\u65b9\u3002\u8fed\u4ee3\u5668\u4e5f\u7528\u5728 std::sort \u548c\u5176\u4ed6\u7b97\u6cd5\u4e2d\u3002\u5728\u4e86\u89e3\u8fed\u4ee3\u5668\u4e4b\u540e\u4f60\u4f1a\u53d1\u73b0\uff0c\u6807\u51c6\u5e93\u4e2d\u5927\u91cf\u5730\u4f7f\u7528\u4e86\u8fed\u4ee3\u5668\u3002","title":"\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af"},{"location":"11-18-Introduction-to-iterators/#_4","text":"Much like pointers and references, iterators can be left \u201cdangling\u201d if the elements being iterated over change address or are destroyed. When this happens, we say the iterator has been invalidated. Accessing an invalidated iterator produces undefined behavior. \u5c31\u50cf\u6307\u9488\u548c\u5f15\u7528\u4e00\u6837\uff0c\u5982\u679c\u8fed\u4ee3\u5668\u7684\u5143\u7d20\u5730\u5740\u88ab\u4fee\u6539\u6216\u672c\u8eab\u88ab\u9500\u6bc1\u65f6\uff0c\u8fed\u4ee3\u5668\u53ef\u80fd\u4f1a\u5904\u4e8e \u60ac\u5782 \u72b6\u6001\u3002\u5f53\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\u65f6\uff0c\u6211\u4eec\u8bf4\u8fed\u4ee3\u5668\u5df2\u5931\u6548\u3002\u8bbf\u95ee\u65e0\u6548\u7684\u8fed\u4ee3\u5668\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e00\u4e9b\u4fee\u6539\u5bb9\u5668\u7684\u64cd\u4f5c(\u4f8b\u5982\u5411 std::vector \u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20)\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5bb9\u5668\u4e2d\u7684\u5143\u7d20\u5730\u5740\u6539\u53d8\u3002\u5f53\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\u65f6\uff0c\u6307\u5411\u8fd9\u4e9b\u5143\u7d20\u7684\u73b0\u6709\u8fed\u4ee3\u5668\u5c06\u5931\u6548\u3002\u597d\u7684C++\u53c2\u8003\u6587\u6863\u5e94\u8be5\u8bf4\u660e\u54ea\u4e9b\u5bb9\u5668\u64cd\u4f5c\u53ef\u80fd\u6216\u5c06\u4f7f\u8fed\u4ee3\u5668\u5931\u6548\u3002 \u201cIterator invalidation\u201d section of std::vector on cppreference \u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u3002 \u5931\u6548\u7684\u4f8b\u5b50\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include int main () { std :: vector v { 1 , 2 , 3 , 4 , 5 , 6 , 7 }; auto it { v . begin () }; ++ it ; // \u79fb\u52a8\u5230\u7b2c\u4e8c\u4e2a\u5143\u7d20 std :: cout << * it << '\\n' ; // ok: \u6253\u53702 v . erase ( it ); // erase \u5f53\u524d\u904d\u5386\u5230\u7684\u5143\u7d20 // erase() \u4f7f\u5f97\u88ab\u6307\u5411\u88ab\u5220\u9664\u5143\u7d20\u7684\u8fed\u4ee3\u5668\u5931\u6548 // \u6240\u4ee5\u8fed\u4ee3\u5668\"it\" \u73b0\u5728\u662f\u65e0\u6548\u7684 ++ it ; // \u672a\u5b9a\u4e49\u884c\u4e3a std :: cout << * it << '\\n' ; // \u672a\u5b9a\u4e49\u884c\u4e3a return 0 ; }","title":"\u65e0\u6548\u8fed\u4ee3\u5668\uff08\u60ac\u5782\u8fed\u4ee3\u5668\uff09"},{"location":"11-19-Introduction-to-standard-library-algorithms/","tags":["algorithms","C++20"],"text":"Key Takeaway \u4e2d\u63d0\u4f9b\u4e86\u5f88\u591a\u597d\u7528\u7684\u7b97\u6cd5\uff0c\u7528\u4e8e\u5bf9\u5bb9\u5668\u8fdb\u884c\u67e5\u627e\u3001\u641c\u7d22\u548c\u8ba1\u6570\u3002 std::find \u51fd\u6570\u7528\u4e8e\u67e5\u627e\u67d0\u4e2a\u503c\u5728\u5143\u7d20\u4e2d\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\u3002 std::find \u6709\u4e09\u4e2a\u53c2\u6570\uff1a\u5e8f\u5217\u4e2d\u8d77\u59cb\u5143\u7d20\u7684\u8fed\u4ee3\u5668\u3001\u7ec8\u70b9\u5143\u7d20\u7684\u8fed\u4ee3\u5668\u3001\u9700\u8981\u641c\u7d22\u7684\u503c\u3002\u8be5\u51fd\u6570\u4f1a\u8fd4\u56de\u6307\u5411\u76ee\u6807\u5143\u7d20\u7684\u8fed\u4ee3\u5668\uff08\u5982\u679c\u627e\u5230\u7684\u8bdd\uff09\uff0c\u6216\u8005\u6307\u5411\u5bb9\u5668\u7684\u672b\u5c3e\uff08\u5982\u679c\u6ca1\u627e\u7684\u8bdd\uff09\u3002 std::find_if \u5728\u5bb9\u5668\u4e2d\u67e5\u627e\u662f\u5426\u6709\u4e00\u4e2a\u6ee1\u8db3\u67d0\u79cd\u6761\u4ef6\u7684\u503c\uff08\u4f8b\u5982\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u67d0\u4e2a\u7279\u5b9a\u7684\u5b50\u4e32\uff09 std::count \u548c std::count_if \u7528\u4e8e\u641c\u7d22\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u5e76\u5bf9\u5176\u51fa\u73b0\u6b21\u6570\u8fdb\u884c\u7edf\u8ba1\u3002 std::sort \u9ed8\u8ba4\u5bf9\u6570\u7ec4\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\uff0c\u4f46\u662f\u53ef\u4ee5\u63a5\u53d7\u7b2c\u4e09\u4e2a\u53c2\u6570\uff0c\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219 std::for_each \u63a5\u53d7\u4e00\u4e2a\u5217\u8868\u4f5c\u4e3a\u8f93\u5165\uff0c\u7136\u540e\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u51fd\u6570\u3002\u652f\u6301\u5e76\u884c\u5904\u7406\u3002 \u65b0\u624b\u7a0b\u5e8f\u5458\u53ef\u80fd\u4f1a\u753b\u4e0a\u5927\u91cf\u7684\u65f6\u95f4\u6765\u7f16\u5199\u5faa\u73af\u4ee3\u7801\u5904\u7406\u5f88\u591a\u7b80\u5355\u7684\u4efb\u52a1\uff0c\u4f8b\u5982\u6392\u5e8f\u3001\u8ba1\u6570\u6216\u8005\u662f\u641c\u7d22\u6570\u7ec4\u3002\u8fd9\u4e9b\u5faa\u73af\u53ef\u80fd\u5f88\u5bb9\u6613\u5e26\u6765\u95ee\u9898\uff0c\u4e00\u65b9\u9762\u672c\u8eab\u7f16\u5199\u6570\u7ec4\u5faa\u73af\u5c31\u5bb9\u6613\u4ea7\u751f\u95ee\u9898\uff0c\u53e6\u5916\u4e00\u65b9\u9762\u53ef\u7ef4\u62a4\u6027\u4e5f\u5f88\u4e0d\u597d\uff0c\u56e0\u4e3a\u5faa\u73af\u4ee3\u7801\u901a\u5e38\u6bd4\u8f83\u96be\u4ee5\u7406\u89e3\u3002 \u56e0\u4e3a\u641c\u7d22\u3001\u7edf\u8ba1\u548c\u6392\u5e8f\u662f\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u6240\u4ee5C++\u6807\u51c6\u5e93\u4e3a\u6b64\u63d0\u4f9b\u4e86\u975e\u5e38\u591a\u7684\u51fd\u6570\uff0c\u4ee5\u4fbf\u4f60\u4f7f\u7528\u5be5\u5be5\u51e0\u53e5\u4ee3\u7801\u5c31\u53ef\u4ee5\u5b8c\u6210\u8fd9\u4e9b\u4efb\u52a1\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u6807\u51c6\u5e93\u7684\u51fd\u6570\u4e45\u7ecf\u68c0\u9a8c\u3001\u9ad8\u6548\u3001\u53ef\u4ee5\u914d\u5408\u591a\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u5bb9\u5668\u4f7f\u7528\uff0c\u800c\u4e14\u5b83\u4eec\u4e2d\u591a\u5c11\u90fd\u652f\u6301\u5e76\u884c\uff08\u4f7f\u7528\u591a\u4e2aCPU\u7ebf\u7a0b\u540c\u65f6\u5de5\u4f5c\u4ee5\u4fbf\u66f4\u5feb\u901f\u7684\u5b8c\u6210\u4efb\u52a1\uff09\u3002 \u6807\u51c6\u7b97\u6cd5\u5e93\u63d0\u4f9b\u7684\u529f\u80fd\u5927\u4f53\u53ef\u4ee5\u5206\u4e3a\u4ee5\u4e0b\u4e09\u7c7b\uff1a \u67e5\u9a8c\u7c7b\uff08Inspectors\uff09\u2014\u2014 \u7528\u4e8e\u67e5\u770b\uff08\u4e0d\u4fee\u6539\uff09\u5bb9\u5668\u4e2d\u7684\u6570\u636e\u3002\u5305\u62ec\u641c\u7d22\u548c\u7edf\u8ba1\u3002 \u4fee\u6539\u7c7b\uff08Mutators\uff09\u2014\u2014 \u7528\u4e8e\u4fee\u6539\u5bb9\u5668\u4e2d\u7684\u6570\u636e\u3002\u4f8b\u5982\u6392\u5e8f\u548c\u6d17\u724c\u3002 \u8f85\u52a9\u7c7b\uff08Facilitators\uff09\u2014\u2014 \u7528\u4e8e\u57fa\u4e8e\u6570\u636e\u751f\u6210\u7ed3\u679c\u3002\u4f8b\u5982\u6570\u503c\u76f8\u4e58\u6216\u8005\u786e\u5b9a\u5143\u7d20\u5bf9\u7684\u6392\u5e8f\u987a\u5e8f\u3002 \u8fd9\u4e9b\u7b97\u6cd5\u90fd\u88ab\u5b9a\u4e49\u5728 algorithms \u5e93\u4e2d\u3002\u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5176\u4e2d\u6700\u5e38\u7528\u7684\u4e00\u4e9b\u7b97\u6cd5\u2014\u2014\u4f46\u662f\u5b9e\u9645\u4e0a\u8be5\u7b97\u6cd5\u5e93\u63d0\u4f9b\u7684\u7b97\u6cd5\u8fdc\u8fdc\u4e0d\u6b62\u8fd9\u4e9b\uff0c\u5f3a\u70c8\u5efa\u8bae\u4f60\u70b9\u51fb\u94fe\u63a5\u53bb\u5230\u53c2\u8003\u624b\u518c\uff0c\u770b\u770b\u5b83\u7684\u5168\u90e8\u5185\u5bb9\u3002 \u6ce8\u610f\uff1a\u6240\u6709\u8fd9\u4e9b\u7b97\u6cd5\u90fd\u4f7f\u7528\u4e86 \u8fed\u4ee3\u5668 \uff0c\u5982\u679c\u4f60\u8fd8\u4e0d\u719f\u6089\u8fed\u4ee3\u5668\u7684\u8bdd\uff0c\u518d\u53bb\u5b66\u4e60\u4e00\u4e0b 11.18 \u2014 \u8fed\u4ee3\u5668\u7b80\u4ecb \u5427\u3002 \u4f7f\u7528 std::find \u67e5\u627e\u7279\u5b9a\u503c\u7684\u5143\u7d20 std::find \u51fd\u6570\u7528\u4e8e\u67e5\u627e\u67d0\u4e2a\u503c\u5728\u5143\u7d20\u4e2d\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\u3002 std::find \u6709\u4e09\u4e2a\u53c2\u6570\uff1a\u5e8f\u5217\u4e2d\u8d77\u59cb\u5143\u7d20\u7684\u8fed\u4ee3\u5668\u3001\u7ec8\u70b9\u5143\u7d20\u7684\u8fed\u4ee3\u5668\u3001\u9700\u8981\u641c\u7d22\u7684\u503c\u3002\u8be5\u51fd\u6570\u4f1a\u8fd4\u56de\u6307\u5411\u76ee\u6807\u5143\u7d20\u7684\u8fed\u4ee3\u5668\uff08\u5982\u679c\u627e\u5230\u7684\u8bdd\uff09\uff0c\u6216\u8005\u6307\u5411\u5bb9\u5668\u7684\u672b\u5c3e\uff08\u5982\u679c\u6ca1\u627e\u7684\u8bdd\uff09\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include #include int main () { std :: array arr { 13 , 90 , 99 , 5 , 40 , 80 }; std :: cout << \"Enter a value to search for and replace with: \" ; int search {}; int replace {}; std :: cin >> search >> replace ; // \u5ffd\u7565\u8f93\u5165\u5408\u6cd5\u6027\u68c0\u67e5 // std::find \u8fd4\u56de\u6307\u5411\u627e\u5230\u5143\u7d20\u7684\u8fed\u4ee3\u5668\uff08\u6216\u8005\u662f\u6307\u5411\u5bb9\u5668\u672b\u5c3e\uff09 // \u6211\u4eec\u5c06\u8be5\u5143\u7d20\u5b58\u653e\u5230\u4e00\u4e2a\u53d8\u91cf\u4e2d\uff0c\u901a\u8fc7\u7c7b\u578b\u63a8\u65ad\u6765\u83b7\u53d6\u8fed\u4ee3\u5668\u7684\u7c7b\u578b auto found { std :: find ( arr . begin (), arr . end (), search ) }; // \u6ca1\u6709\u627e\u5230\u5219\u8fd4\u56de\u6307\u5411\u672b\u5c3e\u7684\u8fed\u4ee3\u5668 // \u53ef\u4ee5\u4f7f\u7528end()\u51fd\u6570\u83b7\u53d6\u672b\u5c3e\u5e76\u548c\u8fed\u4ee3\u5668\u8fdb\u884c\u6bd4\u8f83 if ( found == arr . end ()) { std :: cout << \"Could not find \" << search << '\\n' ; } else { // \u66ff\u6362 * found = replace ; } for ( int i : arr ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u5143\u7d20\u5b58\u5728\u7684\u4f8b\u5b50\uff1a 1 2 Enter a value to search for and replace with: 5 234 13 90 99 234 40 80 \u5143\u7d20\u4e0d\u5b58\u5728\u7684\u4f8b\u5b50\uff1a 1 2 3 Enter a value to search for and replace with: 0 234 Could not find 0 13 90 99 5 40 80 \u4f7f\u7528 std::find_if \u67e5\u627e\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20 \u6709\u7684\u65f6\u5019\u6211\u4eec\u9700\u8981\u5728\u5bb9\u5668\u4e2d\u67e5\u627e\u662f\u5426\u6709\u4e00\u4e2a\u6ee1\u8db3\u67d0\u79cd\u6761\u4ef6\u7684\u503c\uff08\u4f8b\u5982\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u67d0\u4e2a\u7279\u5b9a\u7684\u5b50\u4e32\uff09\uff0c\u800c\u4e0d\u662f\u67d0\u4e2a\u5177\u4f53\u7684\u503c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c std::find_if \u5f88\u597d\u7528\u3002 std::find_if \u51fd\u6570\u7684\u5de5\u4f5c\u65b9\u5f0f\u548c std::find \u5f88\u7c7b\u4f3c\uff0c\u4f46\u662f\u6211\u4eec\u4e0d\u9700\u8981\u4f20\u9012\u4e00\u4e2a\u5177\u4f53\u7684\u503c\uff0c\u800c\u662f\u4f20\u9012\u4e00\u4e2a \u53ef\u8c03\u7528\u5bf9\u8c61 \uff0c\u4f8b\u5982\u51fd\u6570\u6307\u9488\uff08\u6216\u8005 lambda\u8868\u8fbe\u5f0f \uff09\u3002 \u5f53\u5143\u7d20\u88ab\u4f9d\u6b21\u904d\u5386\u65f6\uff0c std::find_if \u4f1a\u8c03\u7528\u51fd\u6570\uff08\u901a\u8fc7\u5b9e\u53c2\u4f20\u5165\u7684\u51fd\u6570\uff09\uff0c\u800c\u8be5\u51fd\u6570\u4f1a\u8fd4\u56de true \u6216 false \u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86 std::find_if \u6765\u67e5\u627e\u5305\u542b nut \u5b50\u4e32\u7684\u5143\u7d20\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include #include #include // Our function will return true if the element matches bool containsNut ( std :: string_view str ) { // std::string_view::find returns std::string_view::npos if it doesn't find // the substring. Otherwise it returns the index where the substring occurs // in str. return ( str . find ( \"nut\" ) != std :: string_view :: npos ); } int main () { std :: array < std :: string_view , 4 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" }; // Scan our array to see if any elements contain the \"nut\" substring auto found { std :: find_if ( arr . begin (), arr . end (), containsNut ) }; if ( found == arr . end ()) { std :: cout << \"No nuts \\n \" ; } else { std :: cout << \"Found \" << * found << '\\n' ; } return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Found walnut \u5982\u679c\u6211\u4eec\u81ea\u5df1\u7f16\u5199\u4e0a\u9762\u7684\u793a\u4f8b\uff0c\u5219\u81f3\u5c11\u9700\u8981\u4e09\u4e2a\u5faa\u73af(\u4e00\u4e2a\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u4e24\u4e2a\u5faa\u73af\u5339\u914d\u5b50\u5b57\u7b26\u4e32)\u3002\u6807\u51c6\u5e93\u51fd\u6570\u5141\u8bb8\u6211\u4eec\u5728\u77ed\u77ed\u51e0\u884c\u4ee3\u7801\u4e2d\u5b8c\u6210\u76f8\u540c\u7684\u4e8b\u60c5\uff01 \u4f7f\u7528 std::count \u548c std::count_if \u8fdb\u884c\u8ba1\u6570 std::count \u548c std::count_if \u7528\u4e8e\u641c\u7d22\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u5e76\u5bf9\u5176\u51fa\u73b0\u6b21\u6570\u8fdb\u884c\u7edf\u8ba1\u3002 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u7edf\u8ba1\u6709\u591a\u5c11\u4e2a\u5143\u7d20\u5305\u542b\"nut\"\u5b50\u4e32\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include #include #include bool containsNut ( std :: string_view str ) { return ( str . find ( \"nut\" ) != std :: string_view :: npos ); } int main () { std :: array < std :: string_view , 5 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" , \"peanut\" }; auto nuts { std :: count_if ( arr . begin (), arr . end (), containsNut ) }; std :: cout << \"Counted \" << nuts << \" nut(s) \\n \" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Counted 2 nut(s) \u4f7f\u7528 std::sort \u8fdb\u884c\u81ea\u5b9a\u4e49\u6392\u5e8f \u6211\u4eec\u66fe\u4f7f\u7528 std::sort \u5bf9\u6570\u7ec4\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\uff0c\u4f46\u5b9e\u9645\u4e0a std::sort \u8fd8\u53ef\u4ee5\u505a\u5f88\u591a\u4e8b\u3002\u4e00\u4e2a\u7248\u672c\u7684 std::sort \u53ef\u4ee5\u63a5\u53d7\u7b2c\u4e09\u4e2a\u53c2\u6570\uff0c\u4f7f\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u51fd\u6570\u5b9a\u4e49\u5982\u4f55\u6392\u5e8f\u3002\u8be5\u51fd\u6570\u9700\u8981\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\u7528\u4e8e\u6bd4\u8f83\uff0c\u5982\u679c\u7b2c\u4e00\u4e2a\u6570\u5e94\u8be5\u88ab\u6392\u5728\u7b2c\u4e8c\u4e2a\u6570\u4e4b\u524d\uff0c\u5e94\u8be5\u8fd4\u56de true \u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c std::sort \u4f1a\u6309\u7167\u5347\u5e8f\u8fdb\u884c\u6392\u5e8f\u3002 \u4e0b\u9762\uff0c\u6211\u4eec\u4f7f\u7528 std::sort \u5bf9\u6570\u7ec4\u8fdb\u884c\u964d\u5e8f\u6392\u5e8f\uff08\u901a\u8fc7\u81ea\u5b9a\u4e49\u6bd4\u8f83\u51fd\u6570 greater \u5b9e\u73b0\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include #include bool greater ( int a , int b ) { // Order @a before @b if @a is greater than @b. return ( a > b ); } int main () { std :: array arr { 13 , 90 , 99 , 5 , 40 , 80 }; // Pass greater to std::sort std :: sort ( arr . begin (), arr . end (), greater ); for ( int i : arr ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 99 90 80 40 13 5 \u540c\u6837\uff0c\u6211\u4eec\u4e0d\u7528\u81ea\u5df1\u7f16\u5199\u5faa\u73af\uff0c\u53ea\u9700\u51e0\u884c\u4ee3\u7801\u5c31\u53ef\u4ee5\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f! \u6211\u4eec\u7684 greater \u51fd\u6570\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff0c\u4f46\u6211\u4eec\u6ca1\u6709\u4f20\u9012\u7ed9\u5b83\u4efb\u4f55\u53c2\u6570\uff0c\u90a3\u4e48\u5b83\u4eec\u4ece\u4f55\u800c\u6765\u5462?\u5f53\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u6ca1\u6709\u5706\u62ec\u53f7 () \u7684\u51fd\u6570\u65f6\uff0c\u5b83\u53ea\u662f\u4e00\u4e2a\u51fd\u6570\u6307\u9488\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u3002\u4f60\u53ef\u80fd\u8fd8\u8bb0\u5f97\uff0c\u5f53\u6211\u4eec\u8bd5\u56fe\u6253\u5370\u4e00\u4e2a\u6ca1\u6709\u62ec\u53f7\u7684\u51fd\u6570\u65f6\uff0c std::cout \u8f93\u51fa\u7684\u662f\"1\"\u3002 std::sort \u4f1a\u4f7f\u7528\u8fd9\u4e2a\u6307\u9488\u8c03\u7528\u51fd\u6570\u5e76\u5c06\u6570\u7ec4\u4e2d\u7684\u4e24\u4e2a\u5143\u7d20\u4f5c\u4e3a\u5b9e\u53c2\u3002\u6211\u4eec\u4e0d\u77e5\u9053\u54ea\u4e9b\u5143\u7d20\u4f1a\u88ab greater \u4f7f\u7528\uff0c\u8fd9\u53d6\u51b3\u4e8e\u5e95\u5c42\u7684\u6392\u5e8f\u7b97\u6cd5\u3002\u6211\u4eec\u5c06\u5728\u540e\u9762\u7684\u7ae0\u8282\u4e2d\u8be6\u7ec6\u8ba8\u8bba\u51fd\u6570\u6307\u9488\u3002 \u5c0f\u8d34\u58eb \u56e0\u4e3a\u964d\u5e8f\u6392\u5e8f\u4e5f\u975e\u5e38\u5e38\u89c1\uff0c\u6240\u4ee5C++\u63d0\u4f9b\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7c7b\u578b( std::greater )\u7528\u4e8e\u964d\u5e8f\u6392\u5e8f(\u5b83\u5b9a\u4e49\u5728 functional \u5934\u6587\u4ef6\u4e2d)\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u66ff\u6362\uff1a 1 std :: sort ( arr . begin (), arr . end (), greater ); // \u8c03\u7528\u81ea\u5b9a\u4e49\u7684 greater \u51fd\u6570 \u4e3a 1 2 3 std :: sort ( arr . begin (), arr . end (), std :: greater {}); // \u4f7f\u7528\u6807\u51c6\u5e93\u7684 greater \u6bd4\u8f83\u51fd\u6570 // \u5728 C++17 \u4e4b\u524d\uff0c\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a std::greater \u4f7f\u7528\u7684\u5143\u7d20\u7c7b\u578b std :: sort ( arr . begin (), arr . end (), std :: greater < int > {}); // \u4f7f\u7528\u6807\u51c6\u5e93\u7684 greater \u6bd4\u8f83\u51fd\u6570 \u6ce8\u610f\uff0c std::greater{} \u9700\u8981\u4f7f\u7528\u5927\u62ec\u53f7\u521d\u59cb\u5316\uff0c\u56e0\u4e3a\u5b83\u5e76\u4e0d\u662f\u4e00\u4e2a\u53ef\u8c03\u7528\u7684\u51fd\u6570\u3002\u5b83\u662f\u4e00\u4e2a\u7c7b\u578b\uff0c\u4e3a\u4e86\u4f7f\u7528\u5b83\uff0c\u6211\u4eec\u5fc5\u987b\u5c06\u5176\u5b9e\u4f8b\u5316\u4e3a\u4e00\u4e2a\u5bf9\u8c61\u3002\u82b1\u62ec\u53f7\u521d\u59cb\u5316\u518d\u8fd9\u91cc\u4f1a\u5b9e\u4f8b\u5316\u4e00\u4e2a\u533f\u540d\u5bf9\u8c61\uff08\u6700\u7ec8\u4f1a\u88ab\u4f20\u9012\u7ed9 std::sort \uff09\u3002 \u6269\u5c55\u9605\u8bfb \u8fd9\u91cc\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u4e0b std::sort \u662f\u5982\u4f55\u4f7f\u7528\u6bd4\u8f83\u51fd\u6570\u7684\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4fee\u6539\u4e00\u4e0b 11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f \u4e2d\u5b9e\u73b0\u7684\u9009\u62e9\u6392\u5e8f\u7b97\u6cd5\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include #include void sort ( int * begin , int * end ) { for ( auto startElement { begin }; startElement != end ; ++ startElement ) { auto smallestElement { startElement }; // std::next returns a pointer to the next element, just like (startElement + 1) would. for ( auto currentElement { std :: next ( startElement ) }; currentElement != end ; ++ currentElement ) { if ( * currentElement < * smallestElement ) { smallestElement = currentElement ; } } std :: swap ( * startElement , * smallestElement ); } } int main () { int array []{ 2 , 1 , 9 , 4 , 5 }; sort ( std :: begin ( array ), std :: end ( array )); for ( auto i : array ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u5230\u8fd9\u91cc\u4f4d\u7f6e\uff0c\u8fd8\u6ca1\u6709\u4efb\u4f55\u65b0\u4e1c\u897f\uff0c sort \u4e5f\u603b\u662f\u5bf9\u5143\u7d20\u8fdb\u884c\u4ece\u4f4e\u5230\u9ad8\u7684\u6392\u5e8f\u3002\u4e3a\u4e86\u6dfb\u52a0\u6bd4\u8f83\u51fd\u6570\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u65b0\u7684\u7c7b\u578b std::function \u7528\u4e8e\u4fdd\u5b58\u4e00\u4e2a2\u4e2a\u6574\u578b\u53c2\u6570\u4e14\u8fd4\u56de\u5e03\u5c14\u7c7b\u578b\u7684\u51fd\u6570\u3002\u4f60\u53ef\u4ee5\u6682\u65f6\u5c06\u5176\u7406\u89e3\u4e3a\u67d0\u79cd\u9ed1\u79d1\u6280\uff0c\u6211\u4eec\u7a0d\u540e\u4f1a\u5728 \u7b2c\u5341\u4e8c\u7ae0 \u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002 1 void sort ( int * begin , int * end , std :: function < bool ( int , int ) > compare ) \u6211\u4eec\u73b0\u5728\u53ef\u4ee5\u5c06\u4e00\u4e2a greater \u51fd\u6570\u4f20\u9012\u7ed9 sort \uff0c\u4f46\u662f sort \u53c8\u5e94\u8be5\u5982\u4f55\u4f7f\u7528\u5b83\u5462\uff1f\u6211\u4eec\u9700\u8981\u5c06\u4e0b\u9762\u4ee3\u7801 1 if ( * currentElement < * smallestElement ) \u66ff\u6362\u4e3a\uff1a 1 if ( compare ( * currentElement , * smallestElement )) \u63a5\u4e0b\u6765\uff0c sort \u7684\u8c03\u7528\u8005\u5c31\u53ef\u4ee5\u51b3\u5b9a\u5982\u4f55\u6bd4\u8f83\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include // std::function #include #include #include // sort accepts a comparison function void sort ( int * begin , int * end , std :: function < bool ( int , int ) > compare ) { for ( auto startElement { begin }; startElement != end ; ++ startElement ) { auto smallestElement { startElement }; for ( auto currentElement { std :: next ( startElement ) }; currentElement != end ; ++ currentElement ) { // the comparison function is used to check if the current element should be ordered // before the currently \"smallest\" element. if ( compare ( * currentElement , * smallestElement )) { smallestElement = currentElement ; } } std :: swap ( * startElement , * smallestElement ); } } int main () { int array []{ 2 , 1 , 9 , 4 , 5 }; // use std::greater to sort in descending order // (We have to use the global namespace selector to prevent a collision // between our sort function and std::sort.) :: sort ( std :: begin ( array ), std :: end ( array ), std :: greater {}); for ( auto i : array ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u4f7f\u7528 std::for_each \u5bf9\u5bb9\u5668\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c std::for_each \u63a5\u53d7\u4e00\u4e2a\u5217\u8868\u4f5c\u4e3a\u8f93\u5165\uff0c\u7136\u540e\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u51fd\u6570\u3002\u5f53\u6211\u4eec\u9700\u8981\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u8fdb\u884c\u76f8\u540c\u7684\u64cd\u4f5c\u65f6\u8fd9\u5c31\u975e\u5e38\u6709\u7528\uff0c \u5728\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 std::for_each \u51fd\u6570\u5c06\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u7ffb\u500d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include #include void doubleNumber ( int & i ) { i *= 2 ; } int main () { std :: array arr { 1 , 2 , 3 , 4 }; std :: for_each ( arr . begin (), arr . end (), doubleNumber ); for ( int i : arr ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 4 6 8 \u4e0a\u8ff0\u529f\u80fd\u5728\u65b0\u624b\u7a0b\u5e8f\u5458\u770b\u6765\uff0c\u4f3c\u4e4e\u6ca1\u4ec0\u4e48\u7528\uff0c\u56e0\u4e3a\u76f8\u540c\u7684\u64cd\u4f5c\u5b8c\u5168\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u6765\u5b8c\u6210\uff0c\u800c\u4e14\u4ee3\u7801\u4f1a\u66f4\u7b80\u77ed\u3002\u4f46\u662f std::for_each \u6709\u5b83\u7684\u4f18\u52bf\u3002\u8ba9\u6211\u4eec\u6bd4\u8f83\u4e00\u4e0b\u4e24\u79cd\u65b9\u5f0f\u3002 1 2 3 4 5 6 7 std :: ranges :: for_each ( arr , doubleNumber ); // \u4ece C++20\u5f00\u59cb\uff0c\u6211\u4eec\u4e0d\u5fc5\u518d\u4f7f\u7528 begin() \u548c end(). // std::for_each(arr.begin(), arr.end(), doubleNumber); // \u5728 C++20 \u4e4b\u524d\u7684\u5f62\u5f0f for ( auto & i : arr ) { doubleNumber ( i ); } \u4f7f\u7528 std::for_each \u65f6\uff0c\u7a0b\u5e8f\u7684\u76ee\u7684\u662f\u975e\u5e38\u6e05\u6670\u7684\u2014\u2014\u5bf9 arr \u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8c03\u7528 doubleNumber \u3002 \u5728for\u5faa\u73af\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5fc5\u987b\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf i \uff0c\u5728\u7a0b\u5e8f\u5458\u75b2\u60eb\u6216\u8005\u5206\u5fc3\u7684\u65f6\u5019\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5e26\u6765\u4e00\u4e9b\u95ee\u9898\u3002\u4e00\u65b9\u9762\uff0c\u5982\u679c\u6211\u4eec\u4e0d\u4f7f\u7528auto\u7684\u8bdd\uff0c\u6b64\u5904\u4f1a\u53d1\u751f \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u3002\u6211\u4eec\u4e5f\u53ef\u80fd\u4f1a\u5fd8\u8bb0\u4f7f\u7528 & \uff0c\u8fd9\u6837\u7684\u8bdd doubleNumber \u51fd\u6570\u5c06\u65e0\u6cd5\u4fee\u6539\u6570\u7ec4\u5143\u7d20\u3002\u6211\u4eec\u8fd8\u53ef\u80fd\u4f1a\u8bef\u5c06\u5176\u4ed6\u53d8\u91cf\u4f20\u9012\u7ed9 doubleNumber \u3002\u800c\u8fd9\u4e9b\u95ee\u9898\u5728\u4f7f\u7528 std::for_each \u65f6\u90fd\u4e0d\u4f1a\u53d1\u751f\u3002 \u4e0d\u4ec5\u5982\u6b64\uff0c std::for_each \u8fd8\u53ef\u4ee5\u8df3\u8fc7\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u4f8b\u5982 std::next \u5c31\u53ef\u4ee5\u5b9e\u73b0\u8be5\u529f\u80fd\u3002 1 2 std :: for_each ( std :: next ( arr . begin ()), arr . end (), doubleNumber ); // Now arr is [1, 4, 6, 8]. The first element wasn't doubled. \u8fd9\u5728 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u4e2d\u662f\u65e0\u6cd5\u5b9e\u73b0\u7684\u3002 \u548c\u5f88\u591a\u7b97\u6cd5\u4e00\u6837\uff0c std::for_each \u4e5f\u53ef\u4ee5\u901a\u8fc7\u5e76\u884c\u7684\u65b9\u5f0f\u6765\u52a0\u901f\u5904\u7406\uff0c\u8fd9\u5728\u5e94\u5bf9\u5927\u578b\u9879\u76ee\u6216\u5927\u6570\u636e\u65f6\u6548\u7387\u8981\u6bd4 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u9ad8\u5f97\u591a\u3002 \u6267\u884c\u987a\u5e8f \u6ce8\u610f\uff0c\u7b97\u6cd5\u5e93\u4e2d\u7684\u5927\u591a\u6570\u7b97\u6cd5\u90fd\u4e0d\u4fdd\u8bc1\u7279\u5b9a\u7684\u6267\u884c\u987a\u5e8f\u3002\u5bf9\u4e8e\u8fd9\u6837\u7684\u7b97\u6cd5\uff0c\u8bf7\u6ce8\u610f\u786e\u4fdd\u4f20\u5165\u7684\u4efb\u4f55\u51fd\u6570\u90fd\u4e0d\u4f9d\u8d56\u67d0\u79cd\u987a\u5e8f\u6267\u884c\uff0c\u56e0\u4e3a\u5176\u5728\u6bcf\u4e2a\u7f16\u8bd1\u5668\u4e0a\u8c03\u7528\u7684\u987a\u5e8f\u53ef\u80fd\u4e0d\u76f8\u540c\u3002 \u4e0b\u9762\u8fd9\u4e9b\u51fd\u6570\u662f\u53ef\u4ee5\u4fdd\u8bc1\u6267\u884c\u987a\u5e8f\u7684\uff1a std::for_each \u3001 std::copy \u3001 std::copy_backward \u3001 std::move \u548c std::move_backward \u3002 \u6700\u4f73\u5b9e\u8df5 \u9664\u975e\u6709\u7279\u6b8a\u8bf4\u660e\uff0c\u5426\u5219\u4e0d\u8981\u5047\u8bbe\u6807\u51c6\u5e93\u4e2d\u7684\u7b97\u6cd5\u5177\u6709\u7279\u5b9a\u7684\u6267\u884c\u987a\u5e8f\u3002 std::for_each , std::copy , std::copy_backward , std::move , and std::move_backward \u662f\u987a\u5e8f\u6267\u884c\u7684\u3002 C++20 \u4e2d\u7684\u8303\u56f4 \u5728\u4f7f\u7528\u7b97\u6cd5\u65f6\u603b\u662f\u8981\u4f20\u5165 arr.begin() \u548c arr.end() \u5176\u5b9e\u633a\u70e6\u4eba\u7684\u3002\u4e0d\u8fc7\u5728C++20\u4e2d\uff0c\u5c31\u4e0d\u9700\u8981\u8fd9\u6837\u505a\u4e86\uff0c\u76f4\u63a5\u4f7f\u7528 arr \u5373\u53ef\u3002\u8fd9\u65e0\u7591\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002 \u7ed3\u8bba \u7b97\u6cd5\u5e93\u6709\u5927\u91cf\u6709\u7528\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u4f60\u7684\u4ee3\u7801\u66f4\u7b80\u5355\u3001\u66f4\u5065\u58ee\u3002\u5728\u8fd9\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u53ea\u6d89\u53ca\u4e00\u4e2a\u5f88\u5c0f\u7684\u5b50\u96c6\uff0c\u4f46\u662f\u56e0\u4e3a\u8fd9\u4e9b\u51fd\u6570\u4e2d\u7684\u5927\u591a\u6570\u5de5\u4f5c\u65b9\u5f0f\u975e\u5e38\u76f8\u4f3c\uff0c\u4e00\u65e6\u4f60\u77e5\u9053\u4e86\u5176\u4e2d\u4e00\u4e9b\u51fd\u6570\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u4f60\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u4e2d\u7684\u5927\u591a\u6570\u3002 \u6700\u4f73\u5b9e\u8df5 \u5efa\u8bae\u4f7f\u7528\u7b97\u6cd5\u5e93\u4e2d\u7684\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u7f16\u5199\u81ea\u5df1\u7684\u51fd\u6570\u6765\u5b8c\u6210\u76f8\u540c\u7684\u4efb\u52a1","title":"11.19 \u2014 \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb"},{"location":"11-19-Introduction-to-standard-library-algorithms/#stdfind","text":"std::find \u51fd\u6570\u7528\u4e8e\u67e5\u627e\u67d0\u4e2a\u503c\u5728\u5143\u7d20\u4e2d\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\u3002 std::find \u6709\u4e09\u4e2a\u53c2\u6570\uff1a\u5e8f\u5217\u4e2d\u8d77\u59cb\u5143\u7d20\u7684\u8fed\u4ee3\u5668\u3001\u7ec8\u70b9\u5143\u7d20\u7684\u8fed\u4ee3\u5668\u3001\u9700\u8981\u641c\u7d22\u7684\u503c\u3002\u8be5\u51fd\u6570\u4f1a\u8fd4\u56de\u6307\u5411\u76ee\u6807\u5143\u7d20\u7684\u8fed\u4ee3\u5668\uff08\u5982\u679c\u627e\u5230\u7684\u8bdd\uff09\uff0c\u6216\u8005\u6307\u5411\u5bb9\u5668\u7684\u672b\u5c3e\uff08\u5982\u679c\u6ca1\u627e\u7684\u8bdd\uff09\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include #include int main () { std :: array arr { 13 , 90 , 99 , 5 , 40 , 80 }; std :: cout << \"Enter a value to search for and replace with: \" ; int search {}; int replace {}; std :: cin >> search >> replace ; // \u5ffd\u7565\u8f93\u5165\u5408\u6cd5\u6027\u68c0\u67e5 // std::find \u8fd4\u56de\u6307\u5411\u627e\u5230\u5143\u7d20\u7684\u8fed\u4ee3\u5668\uff08\u6216\u8005\u662f\u6307\u5411\u5bb9\u5668\u672b\u5c3e\uff09 // \u6211\u4eec\u5c06\u8be5\u5143\u7d20\u5b58\u653e\u5230\u4e00\u4e2a\u53d8\u91cf\u4e2d\uff0c\u901a\u8fc7\u7c7b\u578b\u63a8\u65ad\u6765\u83b7\u53d6\u8fed\u4ee3\u5668\u7684\u7c7b\u578b auto found { std :: find ( arr . begin (), arr . end (), search ) }; // \u6ca1\u6709\u627e\u5230\u5219\u8fd4\u56de\u6307\u5411\u672b\u5c3e\u7684\u8fed\u4ee3\u5668 // \u53ef\u4ee5\u4f7f\u7528end()\u51fd\u6570\u83b7\u53d6\u672b\u5c3e\u5e76\u548c\u8fed\u4ee3\u5668\u8fdb\u884c\u6bd4\u8f83 if ( found == arr . end ()) { std :: cout << \"Could not find \" << search << '\\n' ; } else { // \u66ff\u6362 * found = replace ; } for ( int i : arr ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u5143\u7d20\u5b58\u5728\u7684\u4f8b\u5b50\uff1a 1 2 Enter a value to search for and replace with: 5 234 13 90 99 234 40 80 \u5143\u7d20\u4e0d\u5b58\u5728\u7684\u4f8b\u5b50\uff1a 1 2 3 Enter a value to search for and replace with: 0 234 Could not find 0 13 90 99 5 40 80","title":"\u4f7f\u7528 std::find \u67e5\u627e\u7279\u5b9a\u503c\u7684\u5143\u7d20"},{"location":"11-19-Introduction-to-standard-library-algorithms/#stdfind_if","text":"\u6709\u7684\u65f6\u5019\u6211\u4eec\u9700\u8981\u5728\u5bb9\u5668\u4e2d\u67e5\u627e\u662f\u5426\u6709\u4e00\u4e2a\u6ee1\u8db3\u67d0\u79cd\u6761\u4ef6\u7684\u503c\uff08\u4f8b\u5982\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u67d0\u4e2a\u7279\u5b9a\u7684\u5b50\u4e32\uff09\uff0c\u800c\u4e0d\u662f\u67d0\u4e2a\u5177\u4f53\u7684\u503c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c std::find_if \u5f88\u597d\u7528\u3002 std::find_if \u51fd\u6570\u7684\u5de5\u4f5c\u65b9\u5f0f\u548c std::find \u5f88\u7c7b\u4f3c\uff0c\u4f46\u662f\u6211\u4eec\u4e0d\u9700\u8981\u4f20\u9012\u4e00\u4e2a\u5177\u4f53\u7684\u503c\uff0c\u800c\u662f\u4f20\u9012\u4e00\u4e2a \u53ef\u8c03\u7528\u5bf9\u8c61 \uff0c\u4f8b\u5982\u51fd\u6570\u6307\u9488\uff08\u6216\u8005 lambda\u8868\u8fbe\u5f0f \uff09\u3002 \u5f53\u5143\u7d20\u88ab\u4f9d\u6b21\u904d\u5386\u65f6\uff0c std::find_if \u4f1a\u8c03\u7528\u51fd\u6570\uff08\u901a\u8fc7\u5b9e\u53c2\u4f20\u5165\u7684\u51fd\u6570\uff09\uff0c\u800c\u8be5\u51fd\u6570\u4f1a\u8fd4\u56de true \u6216 false \u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86 std::find_if \u6765\u67e5\u627e\u5305\u542b nut \u5b50\u4e32\u7684\u5143\u7d20\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include #include #include // Our function will return true if the element matches bool containsNut ( std :: string_view str ) { // std::string_view::find returns std::string_view::npos if it doesn't find // the substring. Otherwise it returns the index where the substring occurs // in str. return ( str . find ( \"nut\" ) != std :: string_view :: npos ); } int main () { std :: array < std :: string_view , 4 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" }; // Scan our array to see if any elements contain the \"nut\" substring auto found { std :: find_if ( arr . begin (), arr . end (), containsNut ) }; if ( found == arr . end ()) { std :: cout << \"No nuts \\n \" ; } else { std :: cout << \"Found \" << * found << '\\n' ; } return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Found walnut \u5982\u679c\u6211\u4eec\u81ea\u5df1\u7f16\u5199\u4e0a\u9762\u7684\u793a\u4f8b\uff0c\u5219\u81f3\u5c11\u9700\u8981\u4e09\u4e2a\u5faa\u73af(\u4e00\u4e2a\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u4e24\u4e2a\u5faa\u73af\u5339\u914d\u5b50\u5b57\u7b26\u4e32)\u3002\u6807\u51c6\u5e93\u51fd\u6570\u5141\u8bb8\u6211\u4eec\u5728\u77ed\u77ed\u51e0\u884c\u4ee3\u7801\u4e2d\u5b8c\u6210\u76f8\u540c\u7684\u4e8b\u60c5\uff01","title":"\u4f7f\u7528 std::find_if \u67e5\u627e\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20"},{"location":"11-19-Introduction-to-standard-library-algorithms/#stdcount-stdcount_if","text":"std::count \u548c std::count_if \u7528\u4e8e\u641c\u7d22\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u5e76\u5bf9\u5176\u51fa\u73b0\u6b21\u6570\u8fdb\u884c\u7edf\u8ba1\u3002 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u7edf\u8ba1\u6709\u591a\u5c11\u4e2a\u5143\u7d20\u5305\u542b\"nut\"\u5b50\u4e32\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include #include #include bool containsNut ( std :: string_view str ) { return ( str . find ( \"nut\" ) != std :: string_view :: npos ); } int main () { std :: array < std :: string_view , 5 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" , \"peanut\" }; auto nuts { std :: count_if ( arr . begin (), arr . end (), containsNut ) }; std :: cout << \"Counted \" << nuts << \" nut(s) \\n \" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Counted 2 nut(s)","title":"\u4f7f\u7528 std::count \u548c std::count_if \u8fdb\u884c\u8ba1\u6570"},{"location":"11-19-Introduction-to-standard-library-algorithms/#stdsort","text":"\u6211\u4eec\u66fe\u4f7f\u7528 std::sort \u5bf9\u6570\u7ec4\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\uff0c\u4f46\u5b9e\u9645\u4e0a std::sort \u8fd8\u53ef\u4ee5\u505a\u5f88\u591a\u4e8b\u3002\u4e00\u4e2a\u7248\u672c\u7684 std::sort \u53ef\u4ee5\u63a5\u53d7\u7b2c\u4e09\u4e2a\u53c2\u6570\uff0c\u4f7f\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u51fd\u6570\u5b9a\u4e49\u5982\u4f55\u6392\u5e8f\u3002\u8be5\u51fd\u6570\u9700\u8981\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\u7528\u4e8e\u6bd4\u8f83\uff0c\u5982\u679c\u7b2c\u4e00\u4e2a\u6570\u5e94\u8be5\u88ab\u6392\u5728\u7b2c\u4e8c\u4e2a\u6570\u4e4b\u524d\uff0c\u5e94\u8be5\u8fd4\u56de true \u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c std::sort \u4f1a\u6309\u7167\u5347\u5e8f\u8fdb\u884c\u6392\u5e8f\u3002 \u4e0b\u9762\uff0c\u6211\u4eec\u4f7f\u7528 std::sort \u5bf9\u6570\u7ec4\u8fdb\u884c\u964d\u5e8f\u6392\u5e8f\uff08\u901a\u8fc7\u81ea\u5b9a\u4e49\u6bd4\u8f83\u51fd\u6570 greater \u5b9e\u73b0\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include #include bool greater ( int a , int b ) { // Order @a before @b if @a is greater than @b. return ( a > b ); } int main () { std :: array arr { 13 , 90 , 99 , 5 , 40 , 80 }; // Pass greater to std::sort std :: sort ( arr . begin (), arr . end (), greater ); for ( int i : arr ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 99 90 80 40 13 5 \u540c\u6837\uff0c\u6211\u4eec\u4e0d\u7528\u81ea\u5df1\u7f16\u5199\u5faa\u73af\uff0c\u53ea\u9700\u51e0\u884c\u4ee3\u7801\u5c31\u53ef\u4ee5\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f! \u6211\u4eec\u7684 greater \u51fd\u6570\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff0c\u4f46\u6211\u4eec\u6ca1\u6709\u4f20\u9012\u7ed9\u5b83\u4efb\u4f55\u53c2\u6570\uff0c\u90a3\u4e48\u5b83\u4eec\u4ece\u4f55\u800c\u6765\u5462?\u5f53\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u6ca1\u6709\u5706\u62ec\u53f7 () \u7684\u51fd\u6570\u65f6\uff0c\u5b83\u53ea\u662f\u4e00\u4e2a\u51fd\u6570\u6307\u9488\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u3002\u4f60\u53ef\u80fd\u8fd8\u8bb0\u5f97\uff0c\u5f53\u6211\u4eec\u8bd5\u56fe\u6253\u5370\u4e00\u4e2a\u6ca1\u6709\u62ec\u53f7\u7684\u51fd\u6570\u65f6\uff0c std::cout \u8f93\u51fa\u7684\u662f\"1\"\u3002 std::sort \u4f1a\u4f7f\u7528\u8fd9\u4e2a\u6307\u9488\u8c03\u7528\u51fd\u6570\u5e76\u5c06\u6570\u7ec4\u4e2d\u7684\u4e24\u4e2a\u5143\u7d20\u4f5c\u4e3a\u5b9e\u53c2\u3002\u6211\u4eec\u4e0d\u77e5\u9053\u54ea\u4e9b\u5143\u7d20\u4f1a\u88ab greater \u4f7f\u7528\uff0c\u8fd9\u53d6\u51b3\u4e8e\u5e95\u5c42\u7684\u6392\u5e8f\u7b97\u6cd5\u3002\u6211\u4eec\u5c06\u5728\u540e\u9762\u7684\u7ae0\u8282\u4e2d\u8be6\u7ec6\u8ba8\u8bba\u51fd\u6570\u6307\u9488\u3002 \u5c0f\u8d34\u58eb \u56e0\u4e3a\u964d\u5e8f\u6392\u5e8f\u4e5f\u975e\u5e38\u5e38\u89c1\uff0c\u6240\u4ee5C++\u63d0\u4f9b\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7c7b\u578b( std::greater )\u7528\u4e8e\u964d\u5e8f\u6392\u5e8f(\u5b83\u5b9a\u4e49\u5728 functional \u5934\u6587\u4ef6\u4e2d)\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u66ff\u6362\uff1a 1 std :: sort ( arr . begin (), arr . end (), greater ); // \u8c03\u7528\u81ea\u5b9a\u4e49\u7684 greater \u51fd\u6570 \u4e3a 1 2 3 std :: sort ( arr . begin (), arr . end (), std :: greater {}); // \u4f7f\u7528\u6807\u51c6\u5e93\u7684 greater \u6bd4\u8f83\u51fd\u6570 // \u5728 C++17 \u4e4b\u524d\uff0c\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a std::greater \u4f7f\u7528\u7684\u5143\u7d20\u7c7b\u578b std :: sort ( arr . begin (), arr . end (), std :: greater < int > {}); // \u4f7f\u7528\u6807\u51c6\u5e93\u7684 greater \u6bd4\u8f83\u51fd\u6570 \u6ce8\u610f\uff0c std::greater{} \u9700\u8981\u4f7f\u7528\u5927\u62ec\u53f7\u521d\u59cb\u5316\uff0c\u56e0\u4e3a\u5b83\u5e76\u4e0d\u662f\u4e00\u4e2a\u53ef\u8c03\u7528\u7684\u51fd\u6570\u3002\u5b83\u662f\u4e00\u4e2a\u7c7b\u578b\uff0c\u4e3a\u4e86\u4f7f\u7528\u5b83\uff0c\u6211\u4eec\u5fc5\u987b\u5c06\u5176\u5b9e\u4f8b\u5316\u4e3a\u4e00\u4e2a\u5bf9\u8c61\u3002\u82b1\u62ec\u53f7\u521d\u59cb\u5316\u518d\u8fd9\u91cc\u4f1a\u5b9e\u4f8b\u5316\u4e00\u4e2a\u533f\u540d\u5bf9\u8c61\uff08\u6700\u7ec8\u4f1a\u88ab\u4f20\u9012\u7ed9 std::sort \uff09\u3002 \u6269\u5c55\u9605\u8bfb \u8fd9\u91cc\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u4e0b std::sort \u662f\u5982\u4f55\u4f7f\u7528\u6bd4\u8f83\u51fd\u6570\u7684\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4fee\u6539\u4e00\u4e0b 11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f \u4e2d\u5b9e\u73b0\u7684\u9009\u62e9\u6392\u5e8f\u7b97\u6cd5\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include #include void sort ( int * begin , int * end ) { for ( auto startElement { begin }; startElement != end ; ++ startElement ) { auto smallestElement { startElement }; // std::next returns a pointer to the next element, just like (startElement + 1) would. for ( auto currentElement { std :: next ( startElement ) }; currentElement != end ; ++ currentElement ) { if ( * currentElement < * smallestElement ) { smallestElement = currentElement ; } } std :: swap ( * startElement , * smallestElement ); } } int main () { int array []{ 2 , 1 , 9 , 4 , 5 }; sort ( std :: begin ( array ), std :: end ( array )); for ( auto i : array ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u5230\u8fd9\u91cc\u4f4d\u7f6e\uff0c\u8fd8\u6ca1\u6709\u4efb\u4f55\u65b0\u4e1c\u897f\uff0c sort \u4e5f\u603b\u662f\u5bf9\u5143\u7d20\u8fdb\u884c\u4ece\u4f4e\u5230\u9ad8\u7684\u6392\u5e8f\u3002\u4e3a\u4e86\u6dfb\u52a0\u6bd4\u8f83\u51fd\u6570\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u65b0\u7684\u7c7b\u578b std::function \u7528\u4e8e\u4fdd\u5b58\u4e00\u4e2a2\u4e2a\u6574\u578b\u53c2\u6570\u4e14\u8fd4\u56de\u5e03\u5c14\u7c7b\u578b\u7684\u51fd\u6570\u3002\u4f60\u53ef\u4ee5\u6682\u65f6\u5c06\u5176\u7406\u89e3\u4e3a\u67d0\u79cd\u9ed1\u79d1\u6280\uff0c\u6211\u4eec\u7a0d\u540e\u4f1a\u5728 \u7b2c\u5341\u4e8c\u7ae0 \u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002 1 void sort ( int * begin , int * end , std :: function < bool ( int , int ) > compare ) \u6211\u4eec\u73b0\u5728\u53ef\u4ee5\u5c06\u4e00\u4e2a greater \u51fd\u6570\u4f20\u9012\u7ed9 sort \uff0c\u4f46\u662f sort \u53c8\u5e94\u8be5\u5982\u4f55\u4f7f\u7528\u5b83\u5462\uff1f\u6211\u4eec\u9700\u8981\u5c06\u4e0b\u9762\u4ee3\u7801 1 if ( * currentElement < * smallestElement ) \u66ff\u6362\u4e3a\uff1a 1 if ( compare ( * currentElement , * smallestElement )) \u63a5\u4e0b\u6765\uff0c sort \u7684\u8c03\u7528\u8005\u5c31\u53ef\u4ee5\u51b3\u5b9a\u5982\u4f55\u6bd4\u8f83\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include // std::function #include #include #include // sort accepts a comparison function void sort ( int * begin , int * end , std :: function < bool ( int , int ) > compare ) { for ( auto startElement { begin }; startElement != end ; ++ startElement ) { auto smallestElement { startElement }; for ( auto currentElement { std :: next ( startElement ) }; currentElement != end ; ++ currentElement ) { // the comparison function is used to check if the current element should be ordered // before the currently \"smallest\" element. if ( compare ( * currentElement , * smallestElement )) { smallestElement = currentElement ; } } std :: swap ( * startElement , * smallestElement ); } } int main () { int array []{ 2 , 1 , 9 , 4 , 5 }; // use std::greater to sort in descending order // (We have to use the global namespace selector to prevent a collision // between our sort function and std::sort.) :: sort ( std :: begin ( array ), std :: end ( array ), std :: greater {}); for ( auto i : array ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; }","title":"\u4f7f\u7528 std::sort \u8fdb\u884c\u81ea\u5b9a\u4e49\u6392\u5e8f"},{"location":"11-19-Introduction-to-standard-library-algorithms/#stdfor_each","text":"std::for_each \u63a5\u53d7\u4e00\u4e2a\u5217\u8868\u4f5c\u4e3a\u8f93\u5165\uff0c\u7136\u540e\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u51fd\u6570\u3002\u5f53\u6211\u4eec\u9700\u8981\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u8fdb\u884c\u76f8\u540c\u7684\u64cd\u4f5c\u65f6\u8fd9\u5c31\u975e\u5e38\u6709\u7528\uff0c \u5728\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 std::for_each \u51fd\u6570\u5c06\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u7ffb\u500d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include #include void doubleNumber ( int & i ) { i *= 2 ; } int main () { std :: array arr { 1 , 2 , 3 , 4 }; std :: for_each ( arr . begin (), arr . end (), doubleNumber ); for ( int i : arr ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 4 6 8 \u4e0a\u8ff0\u529f\u80fd\u5728\u65b0\u624b\u7a0b\u5e8f\u5458\u770b\u6765\uff0c\u4f3c\u4e4e\u6ca1\u4ec0\u4e48\u7528\uff0c\u56e0\u4e3a\u76f8\u540c\u7684\u64cd\u4f5c\u5b8c\u5168\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u6765\u5b8c\u6210\uff0c\u800c\u4e14\u4ee3\u7801\u4f1a\u66f4\u7b80\u77ed\u3002\u4f46\u662f std::for_each \u6709\u5b83\u7684\u4f18\u52bf\u3002\u8ba9\u6211\u4eec\u6bd4\u8f83\u4e00\u4e0b\u4e24\u79cd\u65b9\u5f0f\u3002 1 2 3 4 5 6 7 std :: ranges :: for_each ( arr , doubleNumber ); // \u4ece C++20\u5f00\u59cb\uff0c\u6211\u4eec\u4e0d\u5fc5\u518d\u4f7f\u7528 begin() \u548c end(). // std::for_each(arr.begin(), arr.end(), doubleNumber); // \u5728 C++20 \u4e4b\u524d\u7684\u5f62\u5f0f for ( auto & i : arr ) { doubleNumber ( i ); } \u4f7f\u7528 std::for_each \u65f6\uff0c\u7a0b\u5e8f\u7684\u76ee\u7684\u662f\u975e\u5e38\u6e05\u6670\u7684\u2014\u2014\u5bf9 arr \u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8c03\u7528 doubleNumber \u3002 \u5728for\u5faa\u73af\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5fc5\u987b\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf i \uff0c\u5728\u7a0b\u5e8f\u5458\u75b2\u60eb\u6216\u8005\u5206\u5fc3\u7684\u65f6\u5019\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5e26\u6765\u4e00\u4e9b\u95ee\u9898\u3002\u4e00\u65b9\u9762\uff0c\u5982\u679c\u6211\u4eec\u4e0d\u4f7f\u7528auto\u7684\u8bdd\uff0c\u6b64\u5904\u4f1a\u53d1\u751f \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u3002\u6211\u4eec\u4e5f\u53ef\u80fd\u4f1a\u5fd8\u8bb0\u4f7f\u7528 & \uff0c\u8fd9\u6837\u7684\u8bdd doubleNumber \u51fd\u6570\u5c06\u65e0\u6cd5\u4fee\u6539\u6570\u7ec4\u5143\u7d20\u3002\u6211\u4eec\u8fd8\u53ef\u80fd\u4f1a\u8bef\u5c06\u5176\u4ed6\u53d8\u91cf\u4f20\u9012\u7ed9 doubleNumber \u3002\u800c\u8fd9\u4e9b\u95ee\u9898\u5728\u4f7f\u7528 std::for_each \u65f6\u90fd\u4e0d\u4f1a\u53d1\u751f\u3002 \u4e0d\u4ec5\u5982\u6b64\uff0c std::for_each \u8fd8\u53ef\u4ee5\u8df3\u8fc7\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u4f8b\u5982 std::next \u5c31\u53ef\u4ee5\u5b9e\u73b0\u8be5\u529f\u80fd\u3002 1 2 std :: for_each ( std :: next ( arr . begin ()), arr . end (), doubleNumber ); // Now arr is [1, 4, 6, 8]. The first element wasn't doubled. \u8fd9\u5728 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u4e2d\u662f\u65e0\u6cd5\u5b9e\u73b0\u7684\u3002 \u548c\u5f88\u591a\u7b97\u6cd5\u4e00\u6837\uff0c std::for_each \u4e5f\u53ef\u4ee5\u901a\u8fc7\u5e76\u884c\u7684\u65b9\u5f0f\u6765\u52a0\u901f\u5904\u7406\uff0c\u8fd9\u5728\u5e94\u5bf9\u5927\u578b\u9879\u76ee\u6216\u5927\u6570\u636e\u65f6\u6548\u7387\u8981\u6bd4 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u9ad8\u5f97\u591a\u3002","title":"\u4f7f\u7528 std::for_each \u5bf9\u5bb9\u5668\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c"},{"location":"11-19-Introduction-to-standard-library-algorithms/#_1","text":"\u6ce8\u610f\uff0c\u7b97\u6cd5\u5e93\u4e2d\u7684\u5927\u591a\u6570\u7b97\u6cd5\u90fd\u4e0d\u4fdd\u8bc1\u7279\u5b9a\u7684\u6267\u884c\u987a\u5e8f\u3002\u5bf9\u4e8e\u8fd9\u6837\u7684\u7b97\u6cd5\uff0c\u8bf7\u6ce8\u610f\u786e\u4fdd\u4f20\u5165\u7684\u4efb\u4f55\u51fd\u6570\u90fd\u4e0d\u4f9d\u8d56\u67d0\u79cd\u987a\u5e8f\u6267\u884c\uff0c\u56e0\u4e3a\u5176\u5728\u6bcf\u4e2a\u7f16\u8bd1\u5668\u4e0a\u8c03\u7528\u7684\u987a\u5e8f\u53ef\u80fd\u4e0d\u76f8\u540c\u3002 \u4e0b\u9762\u8fd9\u4e9b\u51fd\u6570\u662f\u53ef\u4ee5\u4fdd\u8bc1\u6267\u884c\u987a\u5e8f\u7684\uff1a std::for_each \u3001 std::copy \u3001 std::copy_backward \u3001 std::move \u548c std::move_backward \u3002 \u6700\u4f73\u5b9e\u8df5 \u9664\u975e\u6709\u7279\u6b8a\u8bf4\u660e\uff0c\u5426\u5219\u4e0d\u8981\u5047\u8bbe\u6807\u51c6\u5e93\u4e2d\u7684\u7b97\u6cd5\u5177\u6709\u7279\u5b9a\u7684\u6267\u884c\u987a\u5e8f\u3002 std::for_each , std::copy , std::copy_backward , std::move , and std::move_backward \u662f\u987a\u5e8f\u6267\u884c\u7684\u3002","title":"\u6267\u884c\u987a\u5e8f"},{"location":"11-19-Introduction-to-standard-library-algorithms/#c20","text":"\u5728\u4f7f\u7528\u7b97\u6cd5\u65f6\u603b\u662f\u8981\u4f20\u5165 arr.begin() \u548c arr.end() \u5176\u5b9e\u633a\u70e6\u4eba\u7684\u3002\u4e0d\u8fc7\u5728C++20\u4e2d\uff0c\u5c31\u4e0d\u9700\u8981\u8fd9\u6837\u505a\u4e86\uff0c\u76f4\u63a5\u4f7f\u7528 arr \u5373\u53ef\u3002\u8fd9\u65e0\u7591\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002","title":"C++20 \u4e2d\u7684\u8303\u56f4"},{"location":"11-19-Introduction-to-standard-library-algorithms/#_2","text":"\u7b97\u6cd5\u5e93\u6709\u5927\u91cf\u6709\u7528\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u4f60\u7684\u4ee3\u7801\u66f4\u7b80\u5355\u3001\u66f4\u5065\u58ee\u3002\u5728\u8fd9\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u53ea\u6d89\u53ca\u4e00\u4e2a\u5f88\u5c0f\u7684\u5b50\u96c6\uff0c\u4f46\u662f\u56e0\u4e3a\u8fd9\u4e9b\u51fd\u6570\u4e2d\u7684\u5927\u591a\u6570\u5de5\u4f5c\u65b9\u5f0f\u975e\u5e38\u76f8\u4f3c\uff0c\u4e00\u65e6\u4f60\u77e5\u9053\u4e86\u5176\u4e2d\u4e00\u4e9b\u51fd\u6570\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u4f60\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u4e2d\u7684\u5927\u591a\u6570\u3002 \u6700\u4f73\u5b9e\u8df5 \u5efa\u8bae\u4f7f\u7528\u7b97\u6cd5\u5e93\u4e2d\u7684\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u7f16\u5199\u81ea\u5df1\u7684\u51fd\u6570\u6765\u5b8c\u6210\u76f8\u540c\u7684\u4efb\u52a1","title":"\u7ed3\u8bba"},{"location":"11-2-Arrays-Part-II/","tags":["array","C++17","C++20"],"text":"Key Takeaway \u4f7f\u7528 list-initialization \u65b9\u5f0f\u521d\u59cb\u5316\u6570\u7ec4 int prime[5]{ 2,3, 5,7, 11 }; \u521d\u59cb\u5316\u5217\u8868\u4e2d\u7684\u5143\u7d20\u4e2a\u6570\u8d85\u8fc7\u6570\u7ec4\u80fd\u5bb9\u7eb3\u7684\u4e2a\u6570\u65f6\u4f1a\u62a5\u9519\uff0c\u5c11\u4e8e\u65f6\u5269\u4e0b\u7684\u4f1a\u88ab\u521d\u59cb\u5316\u4e3a 0 \u521d\u59cb\u5316\u4e00\u4e2a\u5168 0 \u6570\u7ec4\uff1a int prime[5]{}; \u5982\u679c\u7701\u7565\u521d\u59cb\u5316\u5217\u8868\uff0c\u5219\u6570\u7ec4\u5143\u7d20\u4e3a\u672a\u521d\u59cb\u5316\u72b6\u6001\uff0c\u9664\u975e\u5b83\u4eec\u662f class \u7c7b\u578b\u7684\u3002 \u4e0d\u8bba\u6570\u7ec4\u5143\u7d20\u53ef\u4ee5\u5728\u6ca1\u6709\u521d\u59cb\u5316\u5217\u8868\u65f6\u4f1a\u4e0d\u4f1a\u88ab\u9ed8\u8ba4\u521d\u59cb\u5316\uff0c\u90fd\u5e94\u5f53\u663e\u5f0f\u5730\u521d\u59cb\u5316\u6570\u7ec4\u3002 \u4f7f\u7528\u5217\u8868\u521d\u59cb\u5316\u5bf9\u6570\u7ec4\u521d\u59cb\u5316\u65f6\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u81ea\u52a8\u8ba1\u7b97\u6570\u7ec4\u957f\u5ea6\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u58f0\u660e\u6570\u7ec4\u7684\u957f\u5ea6\u3002 \u53ef\u4ee5\u6dfb\u52a0\u4e86\u4e00\u4e2a\u540d\u4e3a max_XXX \u7684\u989d\u5916\u679a\u4e3e\u503c\u3002\u6b64\u679a\u4e3e\u503c\u5728\u6570\u7ec4\u58f0\u660e\u671f\u95f4\u4f7f\u7528\uff0c\u4ee5\u786e\u4fdd\u6570\u7ec4\u5177\u6709\u9002\u5f53\u7684\u957f\u5ea6(\u56e0\u4e3a\u6570\u7ec4\u957f\u5ea6\u5e94\u8be5\u6bd4\u6700\u5927\u7d22\u5f15\u5927 1)\u3002\u8fd9\u5bf9\u4e8e\u4ee3\u7801\u53ef\u8bfb\u6027\u975e\u5e38\u91cd\u8981\uff0c\u5f53\u6211\u4eec\u6dfb\u52a0\u65b0\u7684\u679a\u4e3e\u503c\u65f6\uff0c\u6570\u7ec4\u5c06\u81ea\u52a8\u8c03\u6574\u5927\u5c0f\uff1a \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e \uff08\u679a\u4e3e\u7c7b\uff09\u5e76\u4e0d\u80fd\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u6574\u578b \u4f7f\u7528\u666e\u901a\u679a\u4e3e\u7c7b\u578b\u5e76\u5c06\u5176\u653e\u7f6e\u5728\u67d0\u4e2a\u4f5c\u7528\u57df\u4e2d\uff0c\u6bd4\u4f7f\u7528\u679a\u4e3e\u7c7b\u66f4\u597d\u3002 \u6570\u7ec4\u662f\u4f20\u5730\u5740\u4e0d\u662f\u4f20\u503c\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u60f3\u786e\u4fdd\u4e00\u4e2a\u51fd\u6570\u4e0d\u4f1a\u4fee\u6539\u4f20\u9012\u7ed9\u5b83\u7684\u6570\u7ec4\u5143\u7d20\uff0c\u53ef\u4ee5\u5c06\u6570\u7ec4\u8bbe\u7f6e\u4e3a const \u5934\u6587\u4ef6\u4e2d\u7684 std: size() \u51fd\u6570\u53ef\u4ee5\u7528\u6765\u5224\u65ad\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u4f46\u4e0d\u80fd\u7528\u4e8e\u6570\u7ec4\u88ab\u5f53\u505a\u53c2\u6570\u4f20\u9012\u7684\u60c5\u5f62\uff08C++17\uff09 sizeof \u64cd\u4f5c\u7b26\u53ef\u4ee5\u7528\u4e8e\u6570\u7ec4\uff0c\u5b83\u4f1a\u8fd4\u56de \u6570\u7ec4\u7684\u5927\u5c0f \uff08\u6570\u7ec4\u957f\u5ea6\u4e58\u4ee5\u6570\u7ec4\u5143\u7d20\u7684\u5927\u5c0f\uff09\uff0c\u4f7f\u7528\u6570\u7ec4\u5927\u5c0f\u9664\u4ee5\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u5927\u5c0f\u53ef\u4ee5\u5f97\u5230\u6570\u7ec4\u957f\u5ea6 \u5c06 sizeof \u5e94\u7528\u4e8e\u88ab\u4f20\u5165\u51fd\u6570\u7684\u6570\u7ec4\u65f6\uff0c\u4e0d\u4f1a\u50cf std:: size() \u4e00\u6837\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\uff0c\u53d6\u800c\u4ee3\u4e4b\u7684\u662f\uff0c\u5b83\u4f1a\u8fd4\u56de \u6307\u9488\u7684\u957f\u5ea6 \u3002 \u4e66\u63a5\u4e0a\u6587\uff08 11.1 - \u6570\u7ec4\uff08\u7b2c\u4e00\u90e8\u5206\uff09 \uff09\u3002 \u56fa\u5b9a\u6570\u7ec4\u521d\u59cb\u5316 \u6570\u7ec4\u5143\u7d20\u4f1a\u88ab\u5f53\u505a\u666e\u901a\u53d8\u91cf\u6765\u5bf9\u5f85\uff0c\u56e0\u6b64\uff0c\u5b83\u4eec\u5728\u521b\u5efa\u65f6\u9ed8\u8ba4\u662f\u4e0d\u521d\u59cb\u5316\u7684\u3002 \u201c\u521d\u59cb\u5316\u201d\u6570\u7ec4\u7684\u65b9\u5f0f\u4e4b\u4e00\uff0c\u662f\u9010\u4e2a\u5143\u7d20\u8fdb\u884c\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 int prime [ 5 ]; // hold the first 5 prime numbers prime [ 0 ] = 2 ; prime [ 1 ] = 3 ; prime [ 2 ] = 5 ; prime [ 3 ] = 7 ; prime [ 4 ] = 11 ; \u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u5f88\u9ebb\u70e6\uff0c\u51e0\u4e4e\u4e0d\u53ef\u80fd\u7528\u6765\u521d\u59cb\u5316\u975e\u5e38\u957f\u7684\u6570\u7ec4\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u4e0e\u5176\u8bf4\u8fd9\u662f\u5728\u521d\u59cb\u5316\uff0c\u5012\u4e0d\u5982\u8bf4\u662f\u5728\u8d4b\u503c\u3002\u5982\u679c\u6570\u7ec4\u662f const \u7c7b\u578b\u7684\u8bdd\uff0c\u8d4b\u503c\u662f\u884c\u4e0d\u901a\u7684\u3002 \u5e78\u597d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 \u521d\u59cb\u5316\u503c\u5217\u8868(initializer list) \u6765\u521d\u59cb\u5316\u6574\u4e2a\u6570\u7ec4\u3002\u4e0b\u9762\u4f8b\u5b50\u4e2d\u521d\u59cb\u5316\u7684\u6570\u7ec4\u548c\u4e0a\u4e00\u4e2a\u4f8b\u5b50\u7684\u201d\u521d\u59cb\u5316\u201c\u7ed3\u679c\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff1a 1 int prime [ 5 ]{ 2 , 3 , 5 , 7 , 11 }; // use initializer list to initialize the fixed array \u5982\u679c\u5217\u8868\u4e2d\u521d\u59cb\u5316\u503c\u7684\u6570\u91cf\u8d85\u8fc7\u4e86\u6570\u7ec4\u6240\u80fd\u5bb9\u7eb3\u7684\u6570\u91cf\uff0c\u7f16\u8bd1\u5668\u5c06\u4f1a\u62a5\u544a\u4e00\u4e2a\u9519\u8bef\u3002 \u4f46\u662f\uff0c\u5982\u679c\u5217\u8868\u4e2d\u521d\u59cb\u5316\u5668\u7684\u6570\u91cf\u5c11\u4e8e\u6570\u7ec4\u6240\u80fd\u5bb9\u7eb3\u7684\u6570\u91cf\uff0c\u5219\u5269\u4f59\u7684\u5143\u7d20\u5c06\u88ab\u521d\u59cb\u5316\u4e3a 0 (\u6216\u8005\uff0c 0 \u4f1a\u88ab\u8f6c\u6362\u5176\u4ed6\u975e\u6574\u578b\u7684\u503c\u2014\u2014\u4f8b\u5982\uff0c\u5bf9\u4e8e double \u7c7b\u578b\uff0c 0 \u4f1a\u8f6c\u6362\u4e3a 0.0 )\u3002\u8fd9\u88ab\u79f0\u4e3a \u96f6\u521d\u59cb\u5316(zero initialization) \u3002 \u8bf7\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int array [ 5 ]{ 7 , 4 , 5 }; // only initialize first 3 elements std :: cout << array [ 0 ] << '\\n' ; std :: cout << array [ 1 ] << '\\n' ; std :: cout << array [ 2 ] << '\\n' ; std :: cout << array [ 3 ] << '\\n' ; std :: cout << array [ 4 ] << '\\n' ; return 0 ; } \u6253\u5370\uff1a 1 2 3 4 5 7 4 5 0 0 \u56e0\u6b64\uff0c\u8981\u5c06\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u521d\u59cb\u5316\u4e3a 0\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a: 1 2 3 4 5 6 7 8 // Initialize all elements to 0 int array [ 5 ]{ }; // Initialize all elements to 0.0 double array [ 5 ]{ }; // Initialize all elements to an empty string std :: string array [ 5 ]{ }; \u5982\u679c\u7701\u7565\u521d\u59cb\u5316\u5217\u8868\uff0c\u5219\u6570\u7ec4\u5143\u7d20\u4e3a\u672a\u521d\u59cb\u5316\u72b6\u6001\uff0c\u9664\u975e\u5b83\u4eec\u662f class \u7c7b\u578b\u7684\u3002 1 2 3 4 5 6 7 8 // \u672a\u521d\u59cb\u5316 int array [ 5 ]; // \u672a\u521d\u59cb\u5316 double array [ 5 ]; // \u521d\u59cb\u5316\u4e3a\u7a7a\u5b57\u7b26\u4e32 std :: string array [ 5 ]; \u6700\u4f73\u5b9e\u8df5 \u4e0d\u8bba\u6570\u7ec4\u5143\u7d20\u53ef\u4ee5\u5728\u6ca1\u6709\u521d\u59cb\u5316\u5217\u8868\u65f6\u4f1a\u4e0d\u4f1a\u88ab\u9ed8\u8ba4\u521d\u59cb\u5316\uff0c\u90fd\u5e94\u5f53\u663e\u5f0f\u5730\u521d\u59cb\u5316\u6570\u7ec4\u3002 \u7701\u7565\u6570\u7ec4\u957f\u5ea6 \u5982\u679c\u60a8\u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u521d\u59cb\u5316\u4e00\u4e2a\u56fa\u5b9a\u957f\u5ea6\u6570\u7ec4\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u81ea\u52a8\u8ba1\u7b97\u51fa\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u7701\u7565\u6570\u7ec4\u957f\u5ea6\u7684\u58f0\u660e\u3002 \u4e0b\u9762\u4e24\u884c\u662f\u7b49\u4ef7\u7684: 1 2 int array [ 5 ]{ 0 , 1 , 2 , 3 , 4 }; // explicitly define the length of the array int array []{ 0 , 1 , 2 , 3 , 4 }; // let the initializer list set length of the array \u8fd9\u4e48\u505a\u7684\u597d\u5904\u53ef\u4e0d\u4ec5\u4ec5\u662f\u65b9\u4fbf\uff0c\u540c\u65f6\u53ef\u4ee5\u907f\u514d\u4f60\u5728\u4fee\u6539\u4e86\u5143\u7d20\u540e\u8fd8\u8981\u66f4\u65b0\u6570\u7ec4\u957f\u5ea6\u58f0\u660e\u3002 \u6570\u7ec4\u548c\u679a\u4e3e \u5bf9\u4e8e\u6570\u7ec4\u6765\u8bf4\uff0c\u8df5\u884c\u4ee3\u7801\u5373\u6587\u6863\u7684\u6700\u5927\u969c\u788d\uff0c\u5c31\u662f\u6574\u6570\u7d22\u5f15\u65e0\u6cd5\u5411\u7a0b\u5e8f\u5458\u63d0\u4f9b\u5173\u4e8e\u7d22\u5f15\u542b\u4e49\u7684\u4efb\u4f55\u4fe1\u606f\u3002\u5047\u8bbe\u4e00\u4e2a\u6709 5 \u4e2a\u5b66\u751f\u7684\u73ed\u7ea7: 1 2 3 constexpr int numberOfStudents { 5 }; int testScores [ numberOfStudents ]{}; testScores [ 2 ] = 76 ; testScores[2] \u8868\u793a\u7684\u662f\u8c01\u7684\u5206\u6570\uff1f\u6211\u4eec\u770b\u4e0d\u51fa\u6765\uff01 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u529e\u6cd5\u662f\u4f7f\u7528\u679a\u4e3e\u7c7b\u578b\uff0c\u6bcf\u4e2a\u679a\u4e3e\u53d8\u91cf\u5bf9\u5e94\u4e00\u4e2a\u6570\u7ec4\u7d22\u5f15\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 enum StudentNames { kenny , // 0 kyle , // 1 stan , // 2 butters , // 3 cartman , // 4 max_students // 5 }; int main () { int testScores [ max_students ]{}; // allocate 5 integers testScores [ stan ] = 76 ; return 0 ; } \u8fd9\u6837\uff0c\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u6240\u4ee3\u8868\u7684\u542b\u4e49\u5c31\u66f4\u52a0\u6e05\u6670\u4e86\u3002\u6ce8\u610f\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e2a\u540d\u4e3a max_students \u7684\u989d\u5916\u679a\u4e3e\u503c\u3002\u6b64\u679a\u4e3e\u503c\u5728\u6570\u7ec4\u58f0\u660e\u671f\u95f4\u4f7f\u7528\uff0c\u4ee5\u786e\u4fdd\u6570\u7ec4\u5177\u6709\u9002\u5f53\u7684\u957f\u5ea6(\u56e0\u4e3a\u6570\u7ec4\u957f\u5ea6\u5e94\u8be5\u6bd4\u6700\u5927\u7d22\u5f15\u5927 1)\u3002\u8fd9\u5bf9\u4e8e\u4ee3\u7801\u53ef\u8bfb\u6027\u975e\u5e38\u91cd\u8981\uff0c\u5f53\u6211\u4eec\u6dfb\u52a0\u65b0\u7684\u679a\u4e3e\u503c\u65f6\uff0c\u6570\u7ec4\u5c06\u81ea\u52a8\u8c03\u6574\u5927\u5c0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 enum StudentNames { kenny , // 0 kyle , // 1 stan , // 2 butters , // 3 cartman , // 4 wendy , // 5 max_students // 6 }; int main () { int testScores [ max_students ]{}; // allocate 6 integers testScores [ stan ] = 76 ; // still works return 0 ; } \u6ce8\u610f\uff0c\u8fd9\u4e2a\u6280\u5de7\u53ea\u6709\u5728\u4f60\u4e0d\u9700\u8981\u624b\u52a8\u4fee\u6539\u679a\u4e3e\u503c\u5bf9\u5e94\u7684\u6570\u503c\u65f6\u624d\u6709\u7528\u3002 \u6570\u7ec4\u548c\u679a\u4e3e\u7c7b \u679a\u4e3e\u7c7b(enum class) \u5e76\u4e0d\u80fd\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u6574\u578b\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u5c1d\u8bd5\u7f16\u5199\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 enum class StudentNames { kenny , // 0 kyle , // 1 stan , // 2 butters , // 3 cartman , // 4 wendy , // 5 max_students // 6 }; int main () { int testScores [ StudentNames :: max_students ]{}; // allocate 6 integers testScores [ StudentNames :: stan ] = 76 ; return 0 ; } \u4f60\u4f1a\u5f97\u5230\u4e00\u4e2a\u7f16\u8bd1\u9519\u8bef\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06\u679a\u4e3e\u503c\u8f6c\u6362\u4e3a\u6574\u578b\u3002 1 2 3 4 5 6 7 int main () { int testScores [ static_cast < int > ( StudentNames :: max_students )]{}; // allocate 6 integers testScores [ static_cast < int > ( StudentNames :: stan )] = 76 ; return 0 ; } \u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u663e\u7136\u5f88\u9ebb\u70e6\u3002\u6240\u4ee5\uff0c\u4f7f\u7528\u666e\u901a\u679a\u4e3e\u7c7b\u578b\u5e76\u5c06\u5176\u653e\u7f6e\u5728\u67d0\u4e2a\u4f5c\u7528\u57df\u4e2d\uff0c\u6bd4\u4f7f\u7528\u679a\u4e3e\u7c7b\u66f4\u597d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 namespace StudentNames { enum StudentNames { kenny , // 0 kyle , // 1 stan , // 2 butters , // 3 cartman , // 4 wendy , // 5 max_students // 6 }; } int main () { int testScores [ StudentNames :: max_students ]{}; // allocate 6 integers testScores [ StudentNames :: stan ] = 76 ; return 0 ; } \u5c06\u6570\u7ec4\u4f20\u9012\u7ed9\u51fd\u6570 \u867d\u7136\u4e4d\u4e00\u770b\u5411\u51fd\u6570\u4f20\u9012\u6570\u7ec4\u548c\u4f20\u9012\u666e\u901a\u53d8\u91cf\u597d\u50cf\u6ca1\u4ec0\u4e48\u533a\u522b\uff0c\u4f46\u5728\u672c\u8d28\u4e0a\uff0cC++\u5bf9\u4f20\u9012\u6570\u7ec4\u7684\u5904\u7406\u662f\u4e0d\u540c\u7684\u3002 \u666e\u901a\u53d8\u91cf\u901a\u8fc7 \u6309\u503c\u4f20\u9012 \u7684\u65b9\u5f0f\u4f20\u5165\u51fd\u6570\u2014\u2014C++\u5c06\u5b9e\u53c2\u7684\u503c\u590d\u5236\u5230\u51fd\u6570\u5f62\u53c2\u4e2d\u3002\u56e0\u4e3a\u5f62\u53c2\u662f\u4e00\u4e2a\u526f\u672c\uff0c\u6539\u53d8\u5f62\u53c2\u7684\u503c\u5e76\u4e0d\u4f1a\u6539\u53d8\u539f\u59cb\u5b9e\u53c2\u7684\u503c\u3002 \u4f46\u662f\uff0c \u7531\u4e8e\u590d\u5236\u5927\u578b\u6570\u7ec4\u7684\u5f00\u9500\u975e\u5e38\u5927\uff0c\u6240\u4ee5 C++\u5728\u5c06\u6570\u7ec4\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\u4e0d\u4f1a\u590d\u5236\u6570\u7ec4\u3002\u76f8\u53cd\uff0c\u5b83\u4f1a\u4f20\u9012 \u6570\u7ec4\u672c\u8eab \u3002\u8fd9\u6837\u505a\u7684\u526f\u4f5c\u7528\u662f\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u66f4\u6539\u6570\u7ec4\u5143\u7d20\u7684\u503c\uff01 \u4e0b\u9762\u7684\u4f8b\u5b50\u8bf4\u660e\u4e86\u8fd9\u4e2a\u6982\u5ff5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include void passValue ( int value ) // value \u662f\u5b9e\u53c2\u7684\u526f\u672c { value = 99 ; // \u4fee\u6539 value \u7684\u503c\u4e0d\u4f1a\u5f71\u54cd\u539f\u503c } void passArray ( int prime [ 5 ]) // prime \u662f\u6570\u7ec4\u672c\u8eab { prime [ 0 ] = 11 ; // \u4fee\u6539 prime \u5143\u7d20\u7684\u503c\u4f1a\u5b9e\u9645\u4fee\u6539\u6570\u7ec4 prime [ 1 ] = 7 ; prime [ 2 ] = 5 ; prime [ 3 ] = 3 ; prime [ 4 ] = 2 ; } int main () { int value { 1 }; std :: cout << \"before passValue: \" << value << '\\n' ; passValue ( value ); std :: cout << \"after passValue: \" << value << '\\n' ; int prime [ 5 ]{ 2 , 3 , 5 , 7 , 11 }; std :: cout << \"before passArray: \" << prime [ 0 ] << \" \" << prime [ 1 ] << \" \" << prime [ 2 ] << \" \" << prime [ 3 ] << \" \" << prime [ 4 ] << '\\n' ; passArray ( prime ); std :: cout << \"after passArray: \" << prime [ 0 ] << \" \" << prime [ 1 ] << \" \" << prime [ 2 ] << \" \" << prime [ 3 ] << \" \" << prime [ 4 ] << '\\n' ; return 0 ; } 1 2 3 4 before passValue: 1 after passValue: 1 before passArray: 2 3 5 7 11 after passArray: 11 7 5 3 2 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c value \u6ca1\u6709\u5728 main() \u88ab\u4e2d\u6539\u53d8\uff0c\u56e0\u4e3a passValue() \u51fd\u6570\u4e2d\u7684\u53c2\u6570\u503c\u662f main() \u51fd\u6570\u4e2d\u53d8\u91cf\u503c\u7684\u526f\u672c\uff0c\u800c\u4e0d\u662f\u5b9e\u9645\u7684\u53d8\u91cf\u3002\u7136\u800c\uff0c\u7531\u4e8e passArray() \u51fd\u6570\u4e2d\u7684\u6570\u7ec4\u662f\u6570\u7ec4\u672c\u8eab\uff0c passArray() \u80fd\u591f\u76f4\u63a5\u6539\u53d8\u5143\u7d20\u7684\u503c\uff01 \u4e0a\u8ff0\u73b0\u8c61\u80cc\u540e\u7684\u539f\u56e0\uff0c\u548c C++ \u5b9e\u73b0\u6570\u7ec4\u7684\u65b9\u5f0f\u6709\u5173\uff0c\u6211\u4eec\u4f1a\u5728 11.8 - \u6307\u9488\u548c\u6570\u7ec4 \u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002\u73b0\u5728\u4f60\u53ef\u4ee5\u6682\u65f6\u8ba4\u4e3a\u8fd9\u662f\u8bed\u8a00\u4e2d\u7684\u4e00\u4e2a\u201d\u602a\u5f02\u201c\u7279\u6027\u3002 \u6ce8\u610f\uff0c\u5982\u679c\u4f60\u60f3\u786e\u4fdd\u4e00\u4e2a\u51fd\u6570\u4e0d\u4f1a\u4fee\u6539\u4f20\u9012\u7ed9\u5b83\u7684\u6570\u7ec4\u5143\u7d20\uff0c\u4f60\u53ef\u4ee5\u5c06\u6570\u7ec4\u8bbe\u7f6e\u4e3a const : 1 2 3 4 5 6 7 8 9 10 // \u5373\u4f7f prime \u662f\u5b9e\u9645\u4e0a\u6570\u7ec4\uff0c\u5728\u51fd\u6570\u4e2d\u5b83\u4e5f\u4f1a\u88ab\u770b\u505a\u662f\u5e38\u91cf\u3002 void passArray ( const int prime [ 5 ]) { // so each of these lines will cause a compile error! prime [ 0 ] = 11 ; prime [ 1 ] = 7 ; prime [ 2 ] = 5 ; prime [ 3 ] = 3 ; prime [ 4 ] = 2 ; } \u786e\u5b9a\u6570\u7ec4\u957f\u5ea6 \u5934\u6587\u4ef6\u4e2d\u7684 std: size() \u51fd\u6570\u53ef\u4ee5\u7528\u6765\u5224\u65ad\u6570\u7ec4\u7684\u957f\u5ea6\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include // for std::size int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << \"The array has: \" << std :: size ( array ) << \" elements \\n \" ; return 0 ; } \u6253\u5370\uff1a 1 The array has: 8 elements \u6ce8\u610f\uff0c\u7531\u4e8e C++ \u51fd\u6570\u4f20\u9012\u6570\u7ec4\u7684\u7279\u6b8a\u6027\uff0c\u4e0a\u9762\u7684\u65b9\u6cd5\u4e0d\u80fd\u7528\u4e8e\u6570\u7ec4\u88ab\u4f20\u9012\u5230\u51fd\u6570\u4e2d\u7684\u60c5\u5f62\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include void printSize ( int array []) { std :: cout << std :: size ( array ) << '\\n' ; // Error } int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << std :: size ( array ) << '\\n' ; // will print the size of the array printSize ( array ); return 0 ; } std:: size() \u4e5f\u53ef\u4ee5\u7528\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\u5bf9\u8c61\uff08\u4f8b\u5982 std:: array \u548c std:: vector \uff09\uff0c\u800c\u4e14\u5982\u679c\u4f60\u5bf9\u88ab\u4f20\u5165\u51fd\u6570\u7684\u6570\u7ec4\u4f7f\u7528 std: size() \u65f6\uff0c\u4f1a\u4ea7\u751f\u7f16\u8bd1\u62a5\u9519\u3002\u6ce8\u610f\uff0c std:: size \u4f1a\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u65e0\u7b26\u53f7\u503c\uff0c\u5982\u679c\u4f60\u9700\u8981\u4f7f\u7528\u6709\u7b26\u53f7\u503c\uff0c\u5219\u8981\u4e48\u5c06\u5176\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u503c\uff0c\u6216\u8005\u4f7f\u7528 C++20 \u4e2d\u63d0\u4f9b\u7684 std:: ssize() (signed size)\u3002 \u8bd1\u8005\u6ce8 \u4e3a\u4ec0\u4e48 C++20 \u8981\u5f15\u5165 std: ssize() std:: size() \u662f\u5728 C++17 \u4e2d\u5f15\u5165\u7684\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u65e7\u7248\u672c\u7684\u7f16\u8bd1\u5668\uff0c\u90a3\u4e48\u53ea\u597d\u4f7f\u7528 sizeof \u8fd0\u7b97\u7b26\u3002 sizeof \u8fd0\u7b97\u7b26\u4f7f\u7528\u8d77\u6765\u4e0d\u5982 std: size() \u65b9\u4fbf\uff0c\u800c\u4e14\u6709\u4e9b\u5730\u65b9\u8981\u7279\u522b\u6ce8\u610f\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u517c\u5bb9 C++17 \u7684\u7f16\u8bd1\u5668\uff0c \u53ef\u4ee5\u76f4\u63a5\u8df3\u5230 \u6570\u7ec4\u8bbf\u95ee\u8d8a\u754c \u4e00\u8282\u3002 sizeof \u64cd\u4f5c\u7b26\u53ef\u4ee5\u7528\u4e8e\u6570\u7ec4\uff0c\u5b83\u4f1a\u8fd4\u56de \u6570\u7ec4\u7684\u5927\u5c0f \uff08\u6570\u7ec4\u957f\u5ea6\u4e58\u4ee5\u6570\u7ec4\u5143\u7d20\u7684\u5927\u5c0f\uff09\u3002 1 2 3 4 5 6 7 8 9 10 #include int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << sizeof ( array ) << '\\n' ; // will print the size of the array multiplied by the size of an int std :: cout << sizeof ( int ) << '\\n' ; return 0 ; } \u5728\u6574\u578b\u5927\u5c0f\u4e3a 4 \u5b57\u8282\u7684\u673a\u5668\u4e0a\uff1a 1 2 32 4 \u7ecf\u5178\u6280\u5de7\uff1a\u5c06\u6570\u7ec4\u7684\u5927\u5c0f\u9664\u4ee5\u6570\u7ec4\u5143\u7d20\u7684\u5927\u5c0f\uff0c\u5c31\u53ef\u4ee5\u5f97\u5230\u6570\u7ec4\u7684\u957f\u5ea6\u3002 1 2 3 4 5 6 7 8 9 #include int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << \"The array has: \" << sizeof ( array ) / sizeof ( array [ 0 ]) << \" elements \\n \" ; return 0 ; } \u6253\u5370\uff1a 1 The array has: 8 elements \u8fd9\u662f\u600e\u4e48\u56de\u4e8b\uff1f\u9996\u5148\uff0c\u8bf7\u6ce8\u610f\uff0c\u6574\u4e2a\u6570\u7ec4\u7684\u5927\u5c0f\u7b49\u4e8e\u6570\u7ec4\u7684\u957f\u5ea6\u4e58\u4ee5\u5143\u7d20\u7684\u5927\u5c0f\uff0c\u5373 array size = array length * element size \u3002 \u516c\u5f0f\u53d8\u5f62\u4e00\u4e0b\u5c31\u53ef\u4ee5\u5f97\u5230\uff1a array length = array size / element size \u3002 sizeof(array) \u5373\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u800c sizeof(array[0]) \u5219\u662f\u6570\u7ec4\u5143\u7d20\u7684\u5927\u5c0f\u3002\u5e26\u5165\u516c\u5f0f\u5f97\u5230\u6570\u7ec4 length = sizeof(array) / sizeof(array[0]) \u3002\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u6570\u7ec4\u5143\u7d20 0 \u4f5c\u4e3a\u6765\u8ba1\u7b97\u6570\u7ec4\u5143\u7d20\u7684\u5927\u5c0f\uff0c\u56e0\u4e3a\u65e0\u8bba\u6570\u7ec4\u957f\u5ea6\u662f\u591a\u5c11\uff0c\u7b2c\u4e00\u4e2a\u5143\u7d20\u603b\u662f\u5b58\u5728\u7684\u3002 \u5c06 sizeof \u5e94\u7528\u4e8e\u88ab\u4f20\u5165\u51fd\u6570\u7684\u6570\u7ec4\u65f6\uff0c\u4e0d\u4f1a\u50cf std:: size() \u4e00\u6837\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\uff0c\u53d6\u800c\u4ee3\u4e4b\u7684\u662f\uff0c\u5b83\u4f1a\u8fd4\u56de \u6307\u9488\u7684\u957f\u5ea6 \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include void printSize ( int array []) { std :: cout << sizeof ( array ) / sizeof ( array [ 0 ]) << '\\n' ; } int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << sizeof ( array ) / sizeof ( array [ 0 ]) << '\\n' ; printSize ( array ); return 0 ; } \u540c\u6837\u5047\u8bbe 8 \u5b57\u8282\u6307\u9488\u548c 4 \u5b57\u8282\u6574\u6570\uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 8 2 \u4f5c\u8005\u6ce8 \u5982\u679c\u8bd5\u56fe\u5bf9\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u6570\u7ec4\u4f7f\u7528 sizeof()\uff0c\u6b63\u786e\u914d\u7f6e\u7684\u7f16\u8bd1\u5668\u5e94\u8be5\u6253\u5370\u4e00\u4e2a\u8b66\u544a\u3002 \u5728 main() \u4e2d\u8ba1\u7b97\u6570\u7ec4\u957f\u5ea6\u662f\u6b63\u786e\u7684\uff0c\u4f46\u662f printSize() \u4e2d\u7684 sizeof() \u8fd4\u56de\u4e86 8 (\u6307\u9488\u7684\u5927\u5c0f)\uff0c 8 \u9664\u4ee5 4 \u7b49\u4e8e 2 \u3002 \u56e0\u6b64\uff0c\u5728\u6570\u7ec4\u4e0a\u4f7f\u7528 sizeof() \u65f6\u8981\u5c0f\u5fc3! \u6ce8\u610f\uff1a\u5728\u901a\u5e38\u7684\u7528\u6cd5\u4e2d\uff0c\u672f\u8bed\u201c\u6570\u7ec4\u5927\u5c0f\u201d\u548c\u201c\u6570\u7ec4\u957f\u5ea6\u201d\u90fd\u662f\u6700\u5e38\u7528\u4e8e\u6307\u6570\u7ec4\u7684\u957f\u5ea6(\u6570\u7ec4\u7684\u5927\u5c0f\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u662f\u6ca1\u6709\u7528\u7684\uff0c\u9664\u4e86\u6211\u4eec\u4e0a\u9762\u5c55\u793a\u7684\u6280\u5de7)\u3002 \u6570\u7ec4\u8bbf\u95ee\u8d8a\u754c \u8bb0\u4f4f\uff0c\u957f\u5ea6\u4e3a N \u7684\u6570\u7ec4\uff0c\u5176\u4e0b\u6807\u4ece 0 \u5230 N-1 \u3002\u5982\u679c\u4f60\u8bd5\u56fe\u8bbf\u95ee\u7684\u6570\u7ec4\u4e0b\u6807\uff0c\u5728\u8fd9\u4e2a\u8303\u56f4\u4e4b\u5916\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f \u8003\u8651\u4ee5\u4e0b\u7a0b\u5e8f: 1 2 3 4 5 6 7 int main () { int prime [ 5 ]{}; // hold the first 5 prime numbers prime [ 5 ] = 13 ; return 0 ; } \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u6570\u7ec4\u957f\u5ea6\u4e3a 5 \uff0c\u4f46\u6211\u4eec\u8bd5\u56fe\u5c06\u7b2c 6 \u4e2a\u5143\u7d20(\u7d22\u5f15 5)\u5199\u5165 prime \u6570\u7ec4\u3002 C++ \u4e0d\u4f1a\u68c0\u67e5\u4e0b\u6807\u662f\u5426\u5408\u6cd5\u3002\u6240\u4ee5\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c13 \u7684\u503c\u4f1a\u88ab\u63d2\u5165\u5230\u5185\u5b58\u4e2d\uff08\u7b2c 6 \u4e2a\u5143\u7d20\u4f4d\u7f6e\u7684\u5185\u5b58\u53ef\u7528\u7684\u8bdd\uff09\u3002\u5f53\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\u65f6\uff0c\u60a8\u5c06\u5f97\u5230\u672a\u5b9a\u4e49\u7684\u884c\u4e3a\u2014\u2014\u4f8b\u5982\uff0c\u8fd9\u53ef\u80fd\u4f1a\u8986\u76d6\u53e6\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u6216\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002 \u5c3d\u7ba1\u4e0d\u5e38\u89c1\uff0c\u4f46\u5728 C++\u4e2d\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8d1f\u6570\u7d22\u5f15\uff0c\u8fd9\u4e5f\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002 \u6cd5\u5219 \u5f53\u4f7f\u7528\u6570\u7ec4\u65f6\uff0c\u786e\u4fdd\u4f60\u7684\u7d22\u5f15\u5728\u6709\u6548\u8303\u56f4\u5185","title":"11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09"},{"location":"11-2-Arrays-Part-II/#_1","text":"\u6570\u7ec4\u5143\u7d20\u4f1a\u88ab\u5f53\u505a\u666e\u901a\u53d8\u91cf\u6765\u5bf9\u5f85\uff0c\u56e0\u6b64\uff0c\u5b83\u4eec\u5728\u521b\u5efa\u65f6\u9ed8\u8ba4\u662f\u4e0d\u521d\u59cb\u5316\u7684\u3002 \u201c\u521d\u59cb\u5316\u201d\u6570\u7ec4\u7684\u65b9\u5f0f\u4e4b\u4e00\uff0c\u662f\u9010\u4e2a\u5143\u7d20\u8fdb\u884c\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 int prime [ 5 ]; // hold the first 5 prime numbers prime [ 0 ] = 2 ; prime [ 1 ] = 3 ; prime [ 2 ] = 5 ; prime [ 3 ] = 7 ; prime [ 4 ] = 11 ; \u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u5f88\u9ebb\u70e6\uff0c\u51e0\u4e4e\u4e0d\u53ef\u80fd\u7528\u6765\u521d\u59cb\u5316\u975e\u5e38\u957f\u7684\u6570\u7ec4\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u4e0e\u5176\u8bf4\u8fd9\u662f\u5728\u521d\u59cb\u5316\uff0c\u5012\u4e0d\u5982\u8bf4\u662f\u5728\u8d4b\u503c\u3002\u5982\u679c\u6570\u7ec4\u662f const \u7c7b\u578b\u7684\u8bdd\uff0c\u8d4b\u503c\u662f\u884c\u4e0d\u901a\u7684\u3002 \u5e78\u597d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 \u521d\u59cb\u5316\u503c\u5217\u8868(initializer list) \u6765\u521d\u59cb\u5316\u6574\u4e2a\u6570\u7ec4\u3002\u4e0b\u9762\u4f8b\u5b50\u4e2d\u521d\u59cb\u5316\u7684\u6570\u7ec4\u548c\u4e0a\u4e00\u4e2a\u4f8b\u5b50\u7684\u201d\u521d\u59cb\u5316\u201c\u7ed3\u679c\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff1a 1 int prime [ 5 ]{ 2 , 3 , 5 , 7 , 11 }; // use initializer list to initialize the fixed array \u5982\u679c\u5217\u8868\u4e2d\u521d\u59cb\u5316\u503c\u7684\u6570\u91cf\u8d85\u8fc7\u4e86\u6570\u7ec4\u6240\u80fd\u5bb9\u7eb3\u7684\u6570\u91cf\uff0c\u7f16\u8bd1\u5668\u5c06\u4f1a\u62a5\u544a\u4e00\u4e2a\u9519\u8bef\u3002 \u4f46\u662f\uff0c\u5982\u679c\u5217\u8868\u4e2d\u521d\u59cb\u5316\u5668\u7684\u6570\u91cf\u5c11\u4e8e\u6570\u7ec4\u6240\u80fd\u5bb9\u7eb3\u7684\u6570\u91cf\uff0c\u5219\u5269\u4f59\u7684\u5143\u7d20\u5c06\u88ab\u521d\u59cb\u5316\u4e3a 0 (\u6216\u8005\uff0c 0 \u4f1a\u88ab\u8f6c\u6362\u5176\u4ed6\u975e\u6574\u578b\u7684\u503c\u2014\u2014\u4f8b\u5982\uff0c\u5bf9\u4e8e double \u7c7b\u578b\uff0c 0 \u4f1a\u8f6c\u6362\u4e3a 0.0 )\u3002\u8fd9\u88ab\u79f0\u4e3a \u96f6\u521d\u59cb\u5316(zero initialization) \u3002 \u8bf7\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int array [ 5 ]{ 7 , 4 , 5 }; // only initialize first 3 elements std :: cout << array [ 0 ] << '\\n' ; std :: cout << array [ 1 ] << '\\n' ; std :: cout << array [ 2 ] << '\\n' ; std :: cout << array [ 3 ] << '\\n' ; std :: cout << array [ 4 ] << '\\n' ; return 0 ; } \u6253\u5370\uff1a 1 2 3 4 5 7 4 5 0 0 \u56e0\u6b64\uff0c\u8981\u5c06\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u521d\u59cb\u5316\u4e3a 0\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a: 1 2 3 4 5 6 7 8 // Initialize all elements to 0 int array [ 5 ]{ }; // Initialize all elements to 0.0 double array [ 5 ]{ }; // Initialize all elements to an empty string std :: string array [ 5 ]{ }; \u5982\u679c\u7701\u7565\u521d\u59cb\u5316\u5217\u8868\uff0c\u5219\u6570\u7ec4\u5143\u7d20\u4e3a\u672a\u521d\u59cb\u5316\u72b6\u6001\uff0c\u9664\u975e\u5b83\u4eec\u662f class \u7c7b\u578b\u7684\u3002 1 2 3 4 5 6 7 8 // \u672a\u521d\u59cb\u5316 int array [ 5 ]; // \u672a\u521d\u59cb\u5316 double array [ 5 ]; // \u521d\u59cb\u5316\u4e3a\u7a7a\u5b57\u7b26\u4e32 std :: string array [ 5 ]; \u6700\u4f73\u5b9e\u8df5 \u4e0d\u8bba\u6570\u7ec4\u5143\u7d20\u53ef\u4ee5\u5728\u6ca1\u6709\u521d\u59cb\u5316\u5217\u8868\u65f6\u4f1a\u4e0d\u4f1a\u88ab\u9ed8\u8ba4\u521d\u59cb\u5316\uff0c\u90fd\u5e94\u5f53\u663e\u5f0f\u5730\u521d\u59cb\u5316\u6570\u7ec4\u3002","title":"\u56fa\u5b9a\u6570\u7ec4\u521d\u59cb\u5316"},{"location":"11-2-Arrays-Part-II/#_2","text":"\u5982\u679c\u60a8\u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u521d\u59cb\u5316\u4e00\u4e2a\u56fa\u5b9a\u957f\u5ea6\u6570\u7ec4\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u81ea\u52a8\u8ba1\u7b97\u51fa\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u7701\u7565\u6570\u7ec4\u957f\u5ea6\u7684\u58f0\u660e\u3002 \u4e0b\u9762\u4e24\u884c\u662f\u7b49\u4ef7\u7684: 1 2 int array [ 5 ]{ 0 , 1 , 2 , 3 , 4 }; // explicitly define the length of the array int array []{ 0 , 1 , 2 , 3 , 4 }; // let the initializer list set length of the array \u8fd9\u4e48\u505a\u7684\u597d\u5904\u53ef\u4e0d\u4ec5\u4ec5\u662f\u65b9\u4fbf\uff0c\u540c\u65f6\u53ef\u4ee5\u907f\u514d\u4f60\u5728\u4fee\u6539\u4e86\u5143\u7d20\u540e\u8fd8\u8981\u66f4\u65b0\u6570\u7ec4\u957f\u5ea6\u58f0\u660e\u3002","title":"\u7701\u7565\u6570\u7ec4\u957f\u5ea6"},{"location":"11-2-Arrays-Part-II/#_3","text":"\u5bf9\u4e8e\u6570\u7ec4\u6765\u8bf4\uff0c\u8df5\u884c\u4ee3\u7801\u5373\u6587\u6863\u7684\u6700\u5927\u969c\u788d\uff0c\u5c31\u662f\u6574\u6570\u7d22\u5f15\u65e0\u6cd5\u5411\u7a0b\u5e8f\u5458\u63d0\u4f9b\u5173\u4e8e\u7d22\u5f15\u542b\u4e49\u7684\u4efb\u4f55\u4fe1\u606f\u3002\u5047\u8bbe\u4e00\u4e2a\u6709 5 \u4e2a\u5b66\u751f\u7684\u73ed\u7ea7: 1 2 3 constexpr int numberOfStudents { 5 }; int testScores [ numberOfStudents ]{}; testScores [ 2 ] = 76 ; testScores[2] \u8868\u793a\u7684\u662f\u8c01\u7684\u5206\u6570\uff1f\u6211\u4eec\u770b\u4e0d\u51fa\u6765\uff01 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u529e\u6cd5\u662f\u4f7f\u7528\u679a\u4e3e\u7c7b\u578b\uff0c\u6bcf\u4e2a\u679a\u4e3e\u53d8\u91cf\u5bf9\u5e94\u4e00\u4e2a\u6570\u7ec4\u7d22\u5f15\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 enum StudentNames { kenny , // 0 kyle , // 1 stan , // 2 butters , // 3 cartman , // 4 max_students // 5 }; int main () { int testScores [ max_students ]{}; // allocate 5 integers testScores [ stan ] = 76 ; return 0 ; } \u8fd9\u6837\uff0c\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u6240\u4ee3\u8868\u7684\u542b\u4e49\u5c31\u66f4\u52a0\u6e05\u6670\u4e86\u3002\u6ce8\u610f\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e2a\u540d\u4e3a max_students \u7684\u989d\u5916\u679a\u4e3e\u503c\u3002\u6b64\u679a\u4e3e\u503c\u5728\u6570\u7ec4\u58f0\u660e\u671f\u95f4\u4f7f\u7528\uff0c\u4ee5\u786e\u4fdd\u6570\u7ec4\u5177\u6709\u9002\u5f53\u7684\u957f\u5ea6(\u56e0\u4e3a\u6570\u7ec4\u957f\u5ea6\u5e94\u8be5\u6bd4\u6700\u5927\u7d22\u5f15\u5927 1)\u3002\u8fd9\u5bf9\u4e8e\u4ee3\u7801\u53ef\u8bfb\u6027\u975e\u5e38\u91cd\u8981\uff0c\u5f53\u6211\u4eec\u6dfb\u52a0\u65b0\u7684\u679a\u4e3e\u503c\u65f6\uff0c\u6570\u7ec4\u5c06\u81ea\u52a8\u8c03\u6574\u5927\u5c0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 enum StudentNames { kenny , // 0 kyle , // 1 stan , // 2 butters , // 3 cartman , // 4 wendy , // 5 max_students // 6 }; int main () { int testScores [ max_students ]{}; // allocate 6 integers testScores [ stan ] = 76 ; // still works return 0 ; } \u6ce8\u610f\uff0c\u8fd9\u4e2a\u6280\u5de7\u53ea\u6709\u5728\u4f60\u4e0d\u9700\u8981\u624b\u52a8\u4fee\u6539\u679a\u4e3e\u503c\u5bf9\u5e94\u7684\u6570\u503c\u65f6\u624d\u6709\u7528\u3002","title":"\u6570\u7ec4\u548c\u679a\u4e3e"},{"location":"11-2-Arrays-Part-II/#_4","text":"\u679a\u4e3e\u7c7b(enum class) \u5e76\u4e0d\u80fd\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u6574\u578b\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u5c1d\u8bd5\u7f16\u5199\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 enum class StudentNames { kenny , // 0 kyle , // 1 stan , // 2 butters , // 3 cartman , // 4 wendy , // 5 max_students // 6 }; int main () { int testScores [ StudentNames :: max_students ]{}; // allocate 6 integers testScores [ StudentNames :: stan ] = 76 ; return 0 ; } \u4f60\u4f1a\u5f97\u5230\u4e00\u4e2a\u7f16\u8bd1\u9519\u8bef\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06\u679a\u4e3e\u503c\u8f6c\u6362\u4e3a\u6574\u578b\u3002 1 2 3 4 5 6 7 int main () { int testScores [ static_cast < int > ( StudentNames :: max_students )]{}; // allocate 6 integers testScores [ static_cast < int > ( StudentNames :: stan )] = 76 ; return 0 ; } \u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u663e\u7136\u5f88\u9ebb\u70e6\u3002\u6240\u4ee5\uff0c\u4f7f\u7528\u666e\u901a\u679a\u4e3e\u7c7b\u578b\u5e76\u5c06\u5176\u653e\u7f6e\u5728\u67d0\u4e2a\u4f5c\u7528\u57df\u4e2d\uff0c\u6bd4\u4f7f\u7528\u679a\u4e3e\u7c7b\u66f4\u597d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 namespace StudentNames { enum StudentNames { kenny , // 0 kyle , // 1 stan , // 2 butters , // 3 cartman , // 4 wendy , // 5 max_students // 6 }; } int main () { int testScores [ StudentNames :: max_students ]{}; // allocate 6 integers testScores [ StudentNames :: stan ] = 76 ; return 0 ; }","title":"\u6570\u7ec4\u548c\u679a\u4e3e\u7c7b"},{"location":"11-2-Arrays-Part-II/#_5","text":"\u867d\u7136\u4e4d\u4e00\u770b\u5411\u51fd\u6570\u4f20\u9012\u6570\u7ec4\u548c\u4f20\u9012\u666e\u901a\u53d8\u91cf\u597d\u50cf\u6ca1\u4ec0\u4e48\u533a\u522b\uff0c\u4f46\u5728\u672c\u8d28\u4e0a\uff0cC++\u5bf9\u4f20\u9012\u6570\u7ec4\u7684\u5904\u7406\u662f\u4e0d\u540c\u7684\u3002 \u666e\u901a\u53d8\u91cf\u901a\u8fc7 \u6309\u503c\u4f20\u9012 \u7684\u65b9\u5f0f\u4f20\u5165\u51fd\u6570\u2014\u2014C++\u5c06\u5b9e\u53c2\u7684\u503c\u590d\u5236\u5230\u51fd\u6570\u5f62\u53c2\u4e2d\u3002\u56e0\u4e3a\u5f62\u53c2\u662f\u4e00\u4e2a\u526f\u672c\uff0c\u6539\u53d8\u5f62\u53c2\u7684\u503c\u5e76\u4e0d\u4f1a\u6539\u53d8\u539f\u59cb\u5b9e\u53c2\u7684\u503c\u3002 \u4f46\u662f\uff0c \u7531\u4e8e\u590d\u5236\u5927\u578b\u6570\u7ec4\u7684\u5f00\u9500\u975e\u5e38\u5927\uff0c\u6240\u4ee5 C++\u5728\u5c06\u6570\u7ec4\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\u4e0d\u4f1a\u590d\u5236\u6570\u7ec4\u3002\u76f8\u53cd\uff0c\u5b83\u4f1a\u4f20\u9012 \u6570\u7ec4\u672c\u8eab \u3002\u8fd9\u6837\u505a\u7684\u526f\u4f5c\u7528\u662f\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u66f4\u6539\u6570\u7ec4\u5143\u7d20\u7684\u503c\uff01 \u4e0b\u9762\u7684\u4f8b\u5b50\u8bf4\u660e\u4e86\u8fd9\u4e2a\u6982\u5ff5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include void passValue ( int value ) // value \u662f\u5b9e\u53c2\u7684\u526f\u672c { value = 99 ; // \u4fee\u6539 value \u7684\u503c\u4e0d\u4f1a\u5f71\u54cd\u539f\u503c } void passArray ( int prime [ 5 ]) // prime \u662f\u6570\u7ec4\u672c\u8eab { prime [ 0 ] = 11 ; // \u4fee\u6539 prime \u5143\u7d20\u7684\u503c\u4f1a\u5b9e\u9645\u4fee\u6539\u6570\u7ec4 prime [ 1 ] = 7 ; prime [ 2 ] = 5 ; prime [ 3 ] = 3 ; prime [ 4 ] = 2 ; } int main () { int value { 1 }; std :: cout << \"before passValue: \" << value << '\\n' ; passValue ( value ); std :: cout << \"after passValue: \" << value << '\\n' ; int prime [ 5 ]{ 2 , 3 , 5 , 7 , 11 }; std :: cout << \"before passArray: \" << prime [ 0 ] << \" \" << prime [ 1 ] << \" \" << prime [ 2 ] << \" \" << prime [ 3 ] << \" \" << prime [ 4 ] << '\\n' ; passArray ( prime ); std :: cout << \"after passArray: \" << prime [ 0 ] << \" \" << prime [ 1 ] << \" \" << prime [ 2 ] << \" \" << prime [ 3 ] << \" \" << prime [ 4 ] << '\\n' ; return 0 ; } 1 2 3 4 before passValue: 1 after passValue: 1 before passArray: 2 3 5 7 11 after passArray: 11 7 5 3 2 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c value \u6ca1\u6709\u5728 main() \u88ab\u4e2d\u6539\u53d8\uff0c\u56e0\u4e3a passValue() \u51fd\u6570\u4e2d\u7684\u53c2\u6570\u503c\u662f main() \u51fd\u6570\u4e2d\u53d8\u91cf\u503c\u7684\u526f\u672c\uff0c\u800c\u4e0d\u662f\u5b9e\u9645\u7684\u53d8\u91cf\u3002\u7136\u800c\uff0c\u7531\u4e8e passArray() \u51fd\u6570\u4e2d\u7684\u6570\u7ec4\u662f\u6570\u7ec4\u672c\u8eab\uff0c passArray() \u80fd\u591f\u76f4\u63a5\u6539\u53d8\u5143\u7d20\u7684\u503c\uff01 \u4e0a\u8ff0\u73b0\u8c61\u80cc\u540e\u7684\u539f\u56e0\uff0c\u548c C++ \u5b9e\u73b0\u6570\u7ec4\u7684\u65b9\u5f0f\u6709\u5173\uff0c\u6211\u4eec\u4f1a\u5728 11.8 - \u6307\u9488\u548c\u6570\u7ec4 \u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002\u73b0\u5728\u4f60\u53ef\u4ee5\u6682\u65f6\u8ba4\u4e3a\u8fd9\u662f\u8bed\u8a00\u4e2d\u7684\u4e00\u4e2a\u201d\u602a\u5f02\u201c\u7279\u6027\u3002 \u6ce8\u610f\uff0c\u5982\u679c\u4f60\u60f3\u786e\u4fdd\u4e00\u4e2a\u51fd\u6570\u4e0d\u4f1a\u4fee\u6539\u4f20\u9012\u7ed9\u5b83\u7684\u6570\u7ec4\u5143\u7d20\uff0c\u4f60\u53ef\u4ee5\u5c06\u6570\u7ec4\u8bbe\u7f6e\u4e3a const : 1 2 3 4 5 6 7 8 9 10 // \u5373\u4f7f prime \u662f\u5b9e\u9645\u4e0a\u6570\u7ec4\uff0c\u5728\u51fd\u6570\u4e2d\u5b83\u4e5f\u4f1a\u88ab\u770b\u505a\u662f\u5e38\u91cf\u3002 void passArray ( const int prime [ 5 ]) { // so each of these lines will cause a compile error! prime [ 0 ] = 11 ; prime [ 1 ] = 7 ; prime [ 2 ] = 5 ; prime [ 3 ] = 3 ; prime [ 4 ] = 2 ; }","title":"\u5c06\u6570\u7ec4\u4f20\u9012\u7ed9\u51fd\u6570"},{"location":"11-2-Arrays-Part-II/#_6","text":" \u5934\u6587\u4ef6\u4e2d\u7684 std: size() \u51fd\u6570\u53ef\u4ee5\u7528\u6765\u5224\u65ad\u6570\u7ec4\u7684\u957f\u5ea6\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include // for std::size int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << \"The array has: \" << std :: size ( array ) << \" elements \\n \" ; return 0 ; } \u6253\u5370\uff1a 1 The array has: 8 elements \u6ce8\u610f\uff0c\u7531\u4e8e C++ \u51fd\u6570\u4f20\u9012\u6570\u7ec4\u7684\u7279\u6b8a\u6027\uff0c\u4e0a\u9762\u7684\u65b9\u6cd5\u4e0d\u80fd\u7528\u4e8e\u6570\u7ec4\u88ab\u4f20\u9012\u5230\u51fd\u6570\u4e2d\u7684\u60c5\u5f62\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include void printSize ( int array []) { std :: cout << std :: size ( array ) << '\\n' ; // Error } int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << std :: size ( array ) << '\\n' ; // will print the size of the array printSize ( array ); return 0 ; } std:: size() \u4e5f\u53ef\u4ee5\u7528\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\u5bf9\u8c61\uff08\u4f8b\u5982 std:: array \u548c std:: vector \uff09\uff0c\u800c\u4e14\u5982\u679c\u4f60\u5bf9\u88ab\u4f20\u5165\u51fd\u6570\u7684\u6570\u7ec4\u4f7f\u7528 std: size() \u65f6\uff0c\u4f1a\u4ea7\u751f\u7f16\u8bd1\u62a5\u9519\u3002\u6ce8\u610f\uff0c std:: size \u4f1a\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u65e0\u7b26\u53f7\u503c\uff0c\u5982\u679c\u4f60\u9700\u8981\u4f7f\u7528\u6709\u7b26\u53f7\u503c\uff0c\u5219\u8981\u4e48\u5c06\u5176\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u503c\uff0c\u6216\u8005\u4f7f\u7528 C++20 \u4e2d\u63d0\u4f9b\u7684 std:: ssize() (signed size)\u3002 \u8bd1\u8005\u6ce8 \u4e3a\u4ec0\u4e48 C++20 \u8981\u5f15\u5165 std: ssize() std:: size() \u662f\u5728 C++17 \u4e2d\u5f15\u5165\u7684\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u65e7\u7248\u672c\u7684\u7f16\u8bd1\u5668\uff0c\u90a3\u4e48\u53ea\u597d\u4f7f\u7528 sizeof \u8fd0\u7b97\u7b26\u3002 sizeof \u8fd0\u7b97\u7b26\u4f7f\u7528\u8d77\u6765\u4e0d\u5982 std: size() \u65b9\u4fbf\uff0c\u800c\u4e14\u6709\u4e9b\u5730\u65b9\u8981\u7279\u522b\u6ce8\u610f\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u517c\u5bb9 C++17 \u7684\u7f16\u8bd1\u5668\uff0c \u53ef\u4ee5\u76f4\u63a5\u8df3\u5230 \u6570\u7ec4\u8bbf\u95ee\u8d8a\u754c \u4e00\u8282\u3002 sizeof \u64cd\u4f5c\u7b26\u53ef\u4ee5\u7528\u4e8e\u6570\u7ec4\uff0c\u5b83\u4f1a\u8fd4\u56de \u6570\u7ec4\u7684\u5927\u5c0f \uff08\u6570\u7ec4\u957f\u5ea6\u4e58\u4ee5\u6570\u7ec4\u5143\u7d20\u7684\u5927\u5c0f\uff09\u3002 1 2 3 4 5 6 7 8 9 10 #include int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << sizeof ( array ) << '\\n' ; // will print the size of the array multiplied by the size of an int std :: cout << sizeof ( int ) << '\\n' ; return 0 ; } \u5728\u6574\u578b\u5927\u5c0f\u4e3a 4 \u5b57\u8282\u7684\u673a\u5668\u4e0a\uff1a 1 2 32 4 \u7ecf\u5178\u6280\u5de7\uff1a\u5c06\u6570\u7ec4\u7684\u5927\u5c0f\u9664\u4ee5\u6570\u7ec4\u5143\u7d20\u7684\u5927\u5c0f\uff0c\u5c31\u53ef\u4ee5\u5f97\u5230\u6570\u7ec4\u7684\u957f\u5ea6\u3002 1 2 3 4 5 6 7 8 9 #include int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << \"The array has: \" << sizeof ( array ) / sizeof ( array [ 0 ]) << \" elements \\n \" ; return 0 ; } \u6253\u5370\uff1a 1 The array has: 8 elements \u8fd9\u662f\u600e\u4e48\u56de\u4e8b\uff1f\u9996\u5148\uff0c\u8bf7\u6ce8\u610f\uff0c\u6574\u4e2a\u6570\u7ec4\u7684\u5927\u5c0f\u7b49\u4e8e\u6570\u7ec4\u7684\u957f\u5ea6\u4e58\u4ee5\u5143\u7d20\u7684\u5927\u5c0f\uff0c\u5373 array size = array length * element size \u3002 \u516c\u5f0f\u53d8\u5f62\u4e00\u4e0b\u5c31\u53ef\u4ee5\u5f97\u5230\uff1a array length = array size / element size \u3002 sizeof(array) \u5373\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u800c sizeof(array[0]) \u5219\u662f\u6570\u7ec4\u5143\u7d20\u7684\u5927\u5c0f\u3002\u5e26\u5165\u516c\u5f0f\u5f97\u5230\u6570\u7ec4 length = sizeof(array) / sizeof(array[0]) \u3002\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u6570\u7ec4\u5143\u7d20 0 \u4f5c\u4e3a\u6765\u8ba1\u7b97\u6570\u7ec4\u5143\u7d20\u7684\u5927\u5c0f\uff0c\u56e0\u4e3a\u65e0\u8bba\u6570\u7ec4\u957f\u5ea6\u662f\u591a\u5c11\uff0c\u7b2c\u4e00\u4e2a\u5143\u7d20\u603b\u662f\u5b58\u5728\u7684\u3002 \u5c06 sizeof \u5e94\u7528\u4e8e\u88ab\u4f20\u5165\u51fd\u6570\u7684\u6570\u7ec4\u65f6\uff0c\u4e0d\u4f1a\u50cf std:: size() \u4e00\u6837\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\uff0c\u53d6\u800c\u4ee3\u4e4b\u7684\u662f\uff0c\u5b83\u4f1a\u8fd4\u56de \u6307\u9488\u7684\u957f\u5ea6 \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include void printSize ( int array []) { std :: cout << sizeof ( array ) / sizeof ( array [ 0 ]) << '\\n' ; } int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << sizeof ( array ) / sizeof ( array [ 0 ]) << '\\n' ; printSize ( array ); return 0 ; } \u540c\u6837\u5047\u8bbe 8 \u5b57\u8282\u6307\u9488\u548c 4 \u5b57\u8282\u6574\u6570\uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 8 2 \u4f5c\u8005\u6ce8 \u5982\u679c\u8bd5\u56fe\u5bf9\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u6570\u7ec4\u4f7f\u7528 sizeof()\uff0c\u6b63\u786e\u914d\u7f6e\u7684\u7f16\u8bd1\u5668\u5e94\u8be5\u6253\u5370\u4e00\u4e2a\u8b66\u544a\u3002 \u5728 main() \u4e2d\u8ba1\u7b97\u6570\u7ec4\u957f\u5ea6\u662f\u6b63\u786e\u7684\uff0c\u4f46\u662f printSize() \u4e2d\u7684 sizeof() \u8fd4\u56de\u4e86 8 (\u6307\u9488\u7684\u5927\u5c0f)\uff0c 8 \u9664\u4ee5 4 \u7b49\u4e8e 2 \u3002 \u56e0\u6b64\uff0c\u5728\u6570\u7ec4\u4e0a\u4f7f\u7528 sizeof() \u65f6\u8981\u5c0f\u5fc3! \u6ce8\u610f\uff1a\u5728\u901a\u5e38\u7684\u7528\u6cd5\u4e2d\uff0c\u672f\u8bed\u201c\u6570\u7ec4\u5927\u5c0f\u201d\u548c\u201c\u6570\u7ec4\u957f\u5ea6\u201d\u90fd\u662f\u6700\u5e38\u7528\u4e8e\u6307\u6570\u7ec4\u7684\u957f\u5ea6(\u6570\u7ec4\u7684\u5927\u5c0f\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u662f\u6ca1\u6709\u7528\u7684\uff0c\u9664\u4e86\u6211\u4eec\u4e0a\u9762\u5c55\u793a\u7684\u6280\u5de7)\u3002","title":"\u786e\u5b9a\u6570\u7ec4\u957f\u5ea6"},{"location":"11-2-Arrays-Part-II/#_7","text":"\u8bb0\u4f4f\uff0c\u957f\u5ea6\u4e3a N \u7684\u6570\u7ec4\uff0c\u5176\u4e0b\u6807\u4ece 0 \u5230 N-1 \u3002\u5982\u679c\u4f60\u8bd5\u56fe\u8bbf\u95ee\u7684\u6570\u7ec4\u4e0b\u6807\uff0c\u5728\u8fd9\u4e2a\u8303\u56f4\u4e4b\u5916\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f \u8003\u8651\u4ee5\u4e0b\u7a0b\u5e8f: 1 2 3 4 5 6 7 int main () { int prime [ 5 ]{}; // hold the first 5 prime numbers prime [ 5 ] = 13 ; return 0 ; } \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u6570\u7ec4\u957f\u5ea6\u4e3a 5 \uff0c\u4f46\u6211\u4eec\u8bd5\u56fe\u5c06\u7b2c 6 \u4e2a\u5143\u7d20(\u7d22\u5f15 5)\u5199\u5165 prime \u6570\u7ec4\u3002 C++ \u4e0d\u4f1a\u68c0\u67e5\u4e0b\u6807\u662f\u5426\u5408\u6cd5\u3002\u6240\u4ee5\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c13 \u7684\u503c\u4f1a\u88ab\u63d2\u5165\u5230\u5185\u5b58\u4e2d\uff08\u7b2c 6 \u4e2a\u5143\u7d20\u4f4d\u7f6e\u7684\u5185\u5b58\u53ef\u7528\u7684\u8bdd\uff09\u3002\u5f53\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\u65f6\uff0c\u60a8\u5c06\u5f97\u5230\u672a\u5b9a\u4e49\u7684\u884c\u4e3a\u2014\u2014\u4f8b\u5982\uff0c\u8fd9\u53ef\u80fd\u4f1a\u8986\u76d6\u53e6\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u6216\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002 \u5c3d\u7ba1\u4e0d\u5e38\u89c1\uff0c\u4f46\u5728 C++\u4e2d\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8d1f\u6570\u7d22\u5f15\uff0c\u8fd9\u4e5f\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002 \u6cd5\u5219 \u5f53\u4f7f\u7528\u6570\u7ec4\u65f6\uff0c\u786e\u4fdd\u4f60\u7684\u7d22\u5f15\u5728\u6709\u6548\u8303\u56f4\u5185","title":"\u6570\u7ec4\u8bbf\u95ee\u8d8a\u754c"},{"location":"11-3-Arrays-and-loops/","tags":["array","loop"],"text":"Key Takeaway \u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u5982\u679c\u9700\u8981\u8ba1\u7b97\u4e00\u4e2a\u73ed\u7ea7\u5b66\u751f\u7684\u5e73\u5747\u6210\u7ee9\u7684\u8bdd\uff0c\u4f7f\u7528\u591a\u4e2a\u53d8\u91cf\u7684\u8bdd\u53ef\u4ee5\u8fd9\u6837\u505a: 1 2 3 4 5 6 7 8 9 int numStudents { 5 }; int score0 { 84 }; int score1 { 92 }; int score2 { 76 }; int score3 { 81 }; int score4 { 56 }; int totalScore { score0 + score1 + score2 + score3 + score4 }; auto averageScore { static_cast < double > ( totalScore ) / numStudents }; \u771f\u662f\u4e0d\u5c11\u53d8\u91cf\u554a\u2014\u2014\u800c\u8fd9\u4ec5\u4ec5\u662f\u8ba1\u7b975\u4e2a\u5b66\u751f\u7684\u5e73\u5747\u503c! \u60f3\u8c61\u4e00\u4e0b\u5982\u679c\u662f30\u4e2a\u5b66\u751f\u6216150\u4e2a\u5b66\u751f\uff0c\u90a3\u5f97\u662f\u591a\u5927\u7684\u5de5\u4f5c\u91cf\u554a\u3002 \u53e6\u5916\uff0c\u5982\u679c\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u5b66\u751f\uff0c\u5219\u5fc5\u987b\u58f0\u660e\u3001\u521d\u59cb\u5316\u4e00\u4e2a\u65b0\u53d8\u91cf\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230 totalScore \u8ba1\u7b97\u4e2d\u3002 \u6bcf\u6b21\u4fee\u6539\u65e7\u4ee3\u7801\u65f6\uff0c\u90fd\u6709\u5f15\u5165\u9519\u8bef\u7684\u98ce\u9669\u3002 \u4f7f\u7528\u6570\u7ec4\u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u597d\u7684\u89e3\u51b3\u65b9\u6848: 1 2 3 4 int scores []{ 84 , 92 , 76 , 81 , 56 }; int numStudents { static_cast < int > ( std :: size ( scores )) }; // \u9700\u8981 C++17 \u548c \u5934\u6587\u4ef6 int totalScore { scores [ 0 ] + scores [ 1 ] + scores [ 2 ] + scores [ 3 ] + scores [ 4 ] }; auto averageScore { static_cast < double > ( totalScore ) / numStudents }; \u8fd9\u4e48\u505a\u7684\u786e\u53ef\u4ee5\u5927\u5927\u51cf\u5c11\u58f0\u660e\u53d8\u91cf\u7684\u6570\u91cf\uff0c\u4f46 totalScore \u4ecd\u7136\u9700\u8981\u5355\u72ec\u5217\u51fa\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u3002\u5982\u4e0a\u6240\u8ff0\uff0c\u6539\u53d8\u5b66\u751f\u4eba\u6570\u610f\u5473\u7740\u9700\u8981\u624b\u52a8\u8c03\u6574\u603b\u6210\u7ee9\u516c\u5f0f\u3002 \u5982\u679c\u6709\u4e00\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5faa\u73af\u6211\u4eec\u7684\u6570\u7ec4\u5e76\u76f4\u63a5\u8ba1\u7b97 totalScore \u5c31\u597d\u4e86\u3002 \u6570\u7ec4\u7684\u5faa\u73af\u904d\u5386 \u5728\u4e0a\u4e00\u8282\u8bfe\u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u6570\u7ec4\u7684\u4e0b\u6807\u4e0d\u9700\u8981\u662f\u4e00\u4e2a\u5e38\u91cf\u503c\u2014\u2014\u5b83\u53ef\u4ee5\u662f\u4e00\u4e2a\u53d8\u91cf\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u53d8\u91cf\u4f5c\u4e3a\u6570\u7ec4\u7d22\u5f15\u6765\u5faa\u73af\u904d\u5386\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\uff0c\u5e76\u5bf9\u5b83\u4eec\u6267\u884c\u4e00\u4e9b\u8ba1\u7b97\u3002\u8fd9\u662f\u975e\u5e38\u5e38\u89c1\u7684\uff0c\u6570\u7ec4\u548c\u5faa\u73af\u51e0\u4e4e\u603b\u662f\u5f62\u5f71\u4e0d\u79bb\u5730\u51fa\u73b0\u3002\u4f7f\u7528\u5faa\u73af\u6765\u4f9d\u6b21\u8bbf\u95ee\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u901a\u5e38\u88ab\u79f0\u4e3a \u904d\u5386(iterating) \u6570\u7ec4\u3002 \u4f7f\u7528 for \u5faa\u73af\u6765\u91cd\u65b0\u7f16\u5199\u4e0a\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 constexpr int scores []{ 84 , 92 , 76 , 81 , 56 }; constexpr int numStudents { static_cast < int > ( std :: size ( scores )) }; // const int numStudents{ sizeof(scores) / sizeof(scores[0]) }; // use this instead if not C++17 capable int totalScore { 0 }; // use a loop to calculate totalScore for ( int student { 0 }; student < numStudents ; ++ student ) // \u8fd9\u91cc student{0} \u5176\u5b9e\u5c31\u662f i=0 totalScore += scores [ student ]; auto averageScore { static_cast < double > ( totalScore ) / numStudents }; \u8fd9\u79cd\u65b9\u6848\u5728\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u65b9\u9762\u90fd\u5f88\u7406\u60f3\u3002\u901a\u8fc7\u5faa\u73af\u6765\u9010\u4e2a\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\uff0c\u516c\u5f0f\u53ef\u4ee5\u6839\u636e\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u4e2a\u6570\u81ea\u52a8\u8c03\u6574\u3002\u8fd9\u610f\u5473\u7740\u5f53\u6709\u65b0\u7684\u5b66\u751f\u65f6\uff0c\u4e0d\u9700\u8981\u624b\u52a8\u6dfb\u52a0\u8be5\u5b66\u751f\u7684\u5206\u6570\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u5faa\u73af\u641c\u7d22\u6570\u7ec4\u7684\u4f8b\u5b50\uff0c\u4ee5\u786e\u5b9a\u73ed\u7ea7\u4e2d\u7684\u6700\u9ad8\u5206\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include // for std::size int main () { // scores are 0 (worst) to 100 (best) constexpr int scores []{ 84 , 92 , 76 , 81 , 56 }; constexpr int numStudents { static_cast < int > ( std :: size ( scores )) }; int maxScore { 0 }; // keep track of our largest score for ( int student { 0 }; student < numStudents ; ++ student ) { if ( scores [ student ] > maxScore ) { maxScore = scores [ student ]; } } std :: cout << \"The best score was \" << maxScore << '\\n' ; return 0 ; } \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u540d\u4e3a maxScore \u7684\u975e\u5faa\u73af\u53d8\u91cf\u6765\u8ddf\u8e2a\u5f53\u524d\u7684\u6700\u9ad8\u5206\u3002 maxScore \u88ab\u521d\u59cb\u5316\u4e3a 0 \u8868\u793a\u8fd8\u6ca1\u6709\u5b58\u50a8\u4efb\u4f55\u5206\u6570\u3002\u7136\u540e\uff0c\u904d\u5386\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u53d1\u73b0\u5206\u6570\u6bd4\u4e4b\u524d\u770b\u5230\u7684\u4efb\u4f55\u5206\u6570\u90fd\u9ad8\uff0c\u5219\u5c06 maxScore \u8bbe\u7f6e\u4e3a\u8be5\u503c\u3002\u56e0\u6b64\uff0c maxScore \u603b\u662f\u8868\u793a\u5f53\u524d\u7684\u6700\u5927\u503c\u3002\u5f53\u5230\u8fbe\u6570\u7ec4\u672b\u5c3e\u65f6\uff0c maxScore \u5c31\u662f\u6574\u4e2a\u6570\u7ec4\u4e2d\u7684\u6700\u9ad8\u5206\u3002 \u6570\u7ec4\u548c\u5faa\u73af \u6570\u7ec4\u7684\u5faa\u73af\u904d\u5386\u901a\u5e38\u88ab\u7528\u6765\u5b8c\u6210\u4e00\u4e0b\u4e09\u4ef6\u4e8b\uff1a \u8ba1\u7b97\u6570\u503c (\u4f8b\u5982\uff1a\u5e73\u5747\u503c\u6216\u603b\u548c)\uff1b \u67e5\u627e\u67d0\u4e2a\u503c\uff08\u4f8b\u5982\uff1a\u6700\u5927\u503c\u3001\u6700\u5c0f\u503c\uff09\uff1b \u6570\u7ec4\u6392\u5e8f\uff08\u4f8b\u5982\uff1a\u5347\u5e8f\u3001\u964d\u5e8f\uff09\u3002 \u5728\u8ba1\u7b97\u6570\u503c\u65f6\uff0c\u53d8\u91cf\u901a\u5e38\u7528\u4e8e\u4fdd\u5b58\u7528\u4e8e\u8ba1\u7b97\u6700\u7ec8\u503c\u7684\u4e2d\u95f4\u7ed3\u679c\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8ba1\u7b97\u4e00\u4e2a\u5e73\u5747\u5206\u6570\uff0c totalScore \u4fdd\u5b58\u5230\u76ee\u524d\u4e3a\u6b62\u6240\u68c0\u67e5\u7684\u6240\u6709\u5143\u7d20\u7684\u603b\u5206\u6570\u3002 \u5728\u67e5\u627e\u67d0\u4e2a\u503c\u662f\uff0c\u53d8\u91cf\u901a\u5e38\u7528\u6765\u4fdd\u5b58\u5230\u76ee\u524d\u4e3a\u6b62\u770b\u5230\u7684\u6700\u4f73\u5019\u9009\u503c(\u6216\u6700\u4f73\u5019\u9009\u503c\u7684\u7d22\u5f15)\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u6765\u67e5\u627e\u6700\u4f73\u5206\u6570\uff0c maxScore \u7528\u4e8e\u4fdd\u5b58\u5230\u76ee\u524d\u4e3a\u6b62\u9047\u5230\u7684\u6700\u9ad8\u5206\u6570\u3002 \u6570\u7ec4\u6392\u5e8f\u8981\u590d\u6742\u4e00\u4e9b\uff0c\u56e0\u4e3a\u5b83\u901a\u5e38\u6d89\u53ca\u5d4c\u5957\u5faa\u73af\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u4ecb\u7ecd\u6570\u7ec4\u6392\u5e8f\u3002 \u6570\u7ec4\u7684\u201d\u5dee\u4e00\u201c\u9519\u8bef \u5bf9\u6570\u7ec4\u4f7f\u7528\u5faa\u73af\u6700\u68d8\u624b\u7684\u95ee\u9898\u4e4b\u4e00\u662f\u786e\u5b9a\u6b63\u786e\u7684\u8fed\u4ee3\u6b21\u6570\uff0c\u5f88\u5bb9\u6613\u72af \u5dee\u4e00\u9519\u8bef(Off-by-one) \u3002\u8bbf\u95ee\u5927\u4e8e\u6570\u7ec4\u957f\u5ea6\u7684\u5143\u7d20\u53ef\u80fd\u4f1a\u4ea7\u751f\u53ef\u6015\u7684\u540e\u679c\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include int main () { constexpr int scores []{ 84 , 92 , 76 , 81 , 56 }; constexpr int numStudents { static_cast < int > ( std :: size ( scores )) }; int maxScore { 0 }; // keep track of our largest score for ( int student { 0 }; student <= numStudents ; ++ student ) { if ( scores [ student ] > maxScore ) { maxScore = scores [ student ]; } } std :: cout << \"The best score was \" << maxScore << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u7684\u95ee\u9898\u662f for \u5faa\u73af\u4e2d\u7684\u6761\u4ef6\u662f\u9519\u8bef\u7684\uff01\u6570\u7ec4\u53ea\u6709 5 \u4e2a\u5143\u7d20\uff0c\u7d22\u5f15\u4ece 0 \u5230 4 \u3002\u4f46\u662f\uff0c\u8fd9\u4e2a\u6570\u7ec4\u4ece 0 \u5faa\u73af\u5230 5 \u3002\u56e0\u6b64\uff0c\u5728\u6700\u540e\u4e00\u6b21\u8fed\u4ee3\u65f6\uff0c\u6570\u7ec4\u5c06\u6267\u884c\u5982\u4e0b\u8bed\u53e5: 1 2 3 4 if ( scores [ 5 ] > maxScore ) { maxScore = scores [ 5 ]; } \u4f46\u5b9e\u9645\u4e0a scores[5] \u662f\u672a\u5b9a\u4e49\u7684\uff01 \u8bbf\u95ee\u5b83\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u6700\u6709\u53ef\u80fd\u7684\u662f\u8bbf\u95ee scores[5] \u4f1a\u5f97\u5230\u4e00\u4e2a \u5783\u573e\u503c \uff0c\u6b64\u65f6 maxScore \u5c06\u662f\u9519\u8bef\u7684\u3002 \u7136\u800c\uff0c\u60f3\u8c61\u4e00\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u65e0\u610f\u4e2d\u7ed9 array[5] \u8d4b\u4e86\u4e00\u4e2a\u503c\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u6211\u4eec\u53ef\u80fd\u4f1a\u8986\u76d6\u53e6\u4e00\u4e2a\u53d8\u91cf(\u6216\u5b83\u7684\u4e00\u90e8\u5206)\uff0c\u6216\u8005\u53ef\u80fd\u7834\u574f\u67d0\u4e9b\u4e1c\u897f\u2014\u2014\u8fd9\u4e9b\u7c7b\u578b\u7684bug \u662f\u5f88\u96be\u5b9a\u4f4d\u7684\uff01 \u56e0\u6b64\uff0c\u5728\u5faa\u73af\u904d\u5386\u6570\u7ec4\u65f6\uff0c\u4e00\u5b9a\u8981\u53cd\u590d\u68c0\u67e5\u5faa\u73af\u6761\u4ef6\uff0c\u4ee5\u786e\u4fdd\u4e0d\u4f1a\u5f15\u5165 \u5dee\u4e00\u9519\u8bef \u3002","title":"11.3 - \u6570\u7ec4\u548c\u5faa\u73af"},{"location":"11-3-Arrays-and-loops/#_1","text":"\u5728\u4e0a\u4e00\u8282\u8bfe\u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u6570\u7ec4\u7684\u4e0b\u6807\u4e0d\u9700\u8981\u662f\u4e00\u4e2a\u5e38\u91cf\u503c\u2014\u2014\u5b83\u53ef\u4ee5\u662f\u4e00\u4e2a\u53d8\u91cf\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u53d8\u91cf\u4f5c\u4e3a\u6570\u7ec4\u7d22\u5f15\u6765\u5faa\u73af\u904d\u5386\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\uff0c\u5e76\u5bf9\u5b83\u4eec\u6267\u884c\u4e00\u4e9b\u8ba1\u7b97\u3002\u8fd9\u662f\u975e\u5e38\u5e38\u89c1\u7684\uff0c\u6570\u7ec4\u548c\u5faa\u73af\u51e0\u4e4e\u603b\u662f\u5f62\u5f71\u4e0d\u79bb\u5730\u51fa\u73b0\u3002\u4f7f\u7528\u5faa\u73af\u6765\u4f9d\u6b21\u8bbf\u95ee\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u901a\u5e38\u88ab\u79f0\u4e3a \u904d\u5386(iterating) \u6570\u7ec4\u3002 \u4f7f\u7528 for \u5faa\u73af\u6765\u91cd\u65b0\u7f16\u5199\u4e0a\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 constexpr int scores []{ 84 , 92 , 76 , 81 , 56 }; constexpr int numStudents { static_cast < int > ( std :: size ( scores )) }; // const int numStudents{ sizeof(scores) / sizeof(scores[0]) }; // use this instead if not C++17 capable int totalScore { 0 }; // use a loop to calculate totalScore for ( int student { 0 }; student < numStudents ; ++ student ) // \u8fd9\u91cc student{0} \u5176\u5b9e\u5c31\u662f i=0 totalScore += scores [ student ]; auto averageScore { static_cast < double > ( totalScore ) / numStudents }; \u8fd9\u79cd\u65b9\u6848\u5728\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u65b9\u9762\u90fd\u5f88\u7406\u60f3\u3002\u901a\u8fc7\u5faa\u73af\u6765\u9010\u4e2a\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\uff0c\u516c\u5f0f\u53ef\u4ee5\u6839\u636e\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u4e2a\u6570\u81ea\u52a8\u8c03\u6574\u3002\u8fd9\u610f\u5473\u7740\u5f53\u6709\u65b0\u7684\u5b66\u751f\u65f6\uff0c\u4e0d\u9700\u8981\u624b\u52a8\u6dfb\u52a0\u8be5\u5b66\u751f\u7684\u5206\u6570\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u5faa\u73af\u641c\u7d22\u6570\u7ec4\u7684\u4f8b\u5b50\uff0c\u4ee5\u786e\u5b9a\u73ed\u7ea7\u4e2d\u7684\u6700\u9ad8\u5206\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include // for std::size int main () { // scores are 0 (worst) to 100 (best) constexpr int scores []{ 84 , 92 , 76 , 81 , 56 }; constexpr int numStudents { static_cast < int > ( std :: size ( scores )) }; int maxScore { 0 }; // keep track of our largest score for ( int student { 0 }; student < numStudents ; ++ student ) { if ( scores [ student ] > maxScore ) { maxScore = scores [ student ]; } } std :: cout << \"The best score was \" << maxScore << '\\n' ; return 0 ; } \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u540d\u4e3a maxScore \u7684\u975e\u5faa\u73af\u53d8\u91cf\u6765\u8ddf\u8e2a\u5f53\u524d\u7684\u6700\u9ad8\u5206\u3002 maxScore \u88ab\u521d\u59cb\u5316\u4e3a 0 \u8868\u793a\u8fd8\u6ca1\u6709\u5b58\u50a8\u4efb\u4f55\u5206\u6570\u3002\u7136\u540e\uff0c\u904d\u5386\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u53d1\u73b0\u5206\u6570\u6bd4\u4e4b\u524d\u770b\u5230\u7684\u4efb\u4f55\u5206\u6570\u90fd\u9ad8\uff0c\u5219\u5c06 maxScore \u8bbe\u7f6e\u4e3a\u8be5\u503c\u3002\u56e0\u6b64\uff0c maxScore \u603b\u662f\u8868\u793a\u5f53\u524d\u7684\u6700\u5927\u503c\u3002\u5f53\u5230\u8fbe\u6570\u7ec4\u672b\u5c3e\u65f6\uff0c maxScore \u5c31\u662f\u6574\u4e2a\u6570\u7ec4\u4e2d\u7684\u6700\u9ad8\u5206\u3002","title":"\u6570\u7ec4\u7684\u5faa\u73af\u904d\u5386"},{"location":"11-3-Arrays-and-loops/#_2","text":"\u6570\u7ec4\u7684\u5faa\u73af\u904d\u5386\u901a\u5e38\u88ab\u7528\u6765\u5b8c\u6210\u4e00\u4e0b\u4e09\u4ef6\u4e8b\uff1a \u8ba1\u7b97\u6570\u503c (\u4f8b\u5982\uff1a\u5e73\u5747\u503c\u6216\u603b\u548c)\uff1b \u67e5\u627e\u67d0\u4e2a\u503c\uff08\u4f8b\u5982\uff1a\u6700\u5927\u503c\u3001\u6700\u5c0f\u503c\uff09\uff1b \u6570\u7ec4\u6392\u5e8f\uff08\u4f8b\u5982\uff1a\u5347\u5e8f\u3001\u964d\u5e8f\uff09\u3002 \u5728\u8ba1\u7b97\u6570\u503c\u65f6\uff0c\u53d8\u91cf\u901a\u5e38\u7528\u4e8e\u4fdd\u5b58\u7528\u4e8e\u8ba1\u7b97\u6700\u7ec8\u503c\u7684\u4e2d\u95f4\u7ed3\u679c\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8ba1\u7b97\u4e00\u4e2a\u5e73\u5747\u5206\u6570\uff0c totalScore \u4fdd\u5b58\u5230\u76ee\u524d\u4e3a\u6b62\u6240\u68c0\u67e5\u7684\u6240\u6709\u5143\u7d20\u7684\u603b\u5206\u6570\u3002 \u5728\u67e5\u627e\u67d0\u4e2a\u503c\u662f\uff0c\u53d8\u91cf\u901a\u5e38\u7528\u6765\u4fdd\u5b58\u5230\u76ee\u524d\u4e3a\u6b62\u770b\u5230\u7684\u6700\u4f73\u5019\u9009\u503c(\u6216\u6700\u4f73\u5019\u9009\u503c\u7684\u7d22\u5f15)\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u6765\u67e5\u627e\u6700\u4f73\u5206\u6570\uff0c maxScore \u7528\u4e8e\u4fdd\u5b58\u5230\u76ee\u524d\u4e3a\u6b62\u9047\u5230\u7684\u6700\u9ad8\u5206\u6570\u3002 \u6570\u7ec4\u6392\u5e8f\u8981\u590d\u6742\u4e00\u4e9b\uff0c\u56e0\u4e3a\u5b83\u901a\u5e38\u6d89\u53ca\u5d4c\u5957\u5faa\u73af\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u4ecb\u7ecd\u6570\u7ec4\u6392\u5e8f\u3002","title":"\u6570\u7ec4\u548c\u5faa\u73af"},{"location":"11-3-Arrays-and-loops/#_3","text":"\u5bf9\u6570\u7ec4\u4f7f\u7528\u5faa\u73af\u6700\u68d8\u624b\u7684\u95ee\u9898\u4e4b\u4e00\u662f\u786e\u5b9a\u6b63\u786e\u7684\u8fed\u4ee3\u6b21\u6570\uff0c\u5f88\u5bb9\u6613\u72af \u5dee\u4e00\u9519\u8bef(Off-by-one) \u3002\u8bbf\u95ee\u5927\u4e8e\u6570\u7ec4\u957f\u5ea6\u7684\u5143\u7d20\u53ef\u80fd\u4f1a\u4ea7\u751f\u53ef\u6015\u7684\u540e\u679c\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include int main () { constexpr int scores []{ 84 , 92 , 76 , 81 , 56 }; constexpr int numStudents { static_cast < int > ( std :: size ( scores )) }; int maxScore { 0 }; // keep track of our largest score for ( int student { 0 }; student <= numStudents ; ++ student ) { if ( scores [ student ] > maxScore ) { maxScore = scores [ student ]; } } std :: cout << \"The best score was \" << maxScore << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u7684\u95ee\u9898\u662f for \u5faa\u73af\u4e2d\u7684\u6761\u4ef6\u662f\u9519\u8bef\u7684\uff01\u6570\u7ec4\u53ea\u6709 5 \u4e2a\u5143\u7d20\uff0c\u7d22\u5f15\u4ece 0 \u5230 4 \u3002\u4f46\u662f\uff0c\u8fd9\u4e2a\u6570\u7ec4\u4ece 0 \u5faa\u73af\u5230 5 \u3002\u56e0\u6b64\uff0c\u5728\u6700\u540e\u4e00\u6b21\u8fed\u4ee3\u65f6\uff0c\u6570\u7ec4\u5c06\u6267\u884c\u5982\u4e0b\u8bed\u53e5: 1 2 3 4 if ( scores [ 5 ] > maxScore ) { maxScore = scores [ 5 ]; } \u4f46\u5b9e\u9645\u4e0a scores[5] \u662f\u672a\u5b9a\u4e49\u7684\uff01 \u8bbf\u95ee\u5b83\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u6700\u6709\u53ef\u80fd\u7684\u662f\u8bbf\u95ee scores[5] \u4f1a\u5f97\u5230\u4e00\u4e2a \u5783\u573e\u503c \uff0c\u6b64\u65f6 maxScore \u5c06\u662f\u9519\u8bef\u7684\u3002 \u7136\u800c\uff0c\u60f3\u8c61\u4e00\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u65e0\u610f\u4e2d\u7ed9 array[5] \u8d4b\u4e86\u4e00\u4e2a\u503c\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u6211\u4eec\u53ef\u80fd\u4f1a\u8986\u76d6\u53e6\u4e00\u4e2a\u53d8\u91cf(\u6216\u5b83\u7684\u4e00\u90e8\u5206)\uff0c\u6216\u8005\u53ef\u80fd\u7834\u574f\u67d0\u4e9b\u4e1c\u897f\u2014\u2014\u8fd9\u4e9b\u7c7b\u578b\u7684bug \u662f\u5f88\u96be\u5b9a\u4f4d\u7684\uff01 \u56e0\u6b64\uff0c\u5728\u5faa\u73af\u904d\u5386\u6570\u7ec4\u65f6\uff0c\u4e00\u5b9a\u8981\u53cd\u590d\u68c0\u67e5\u5faa\u73af\u6761\u4ef6\uff0c\u4ee5\u786e\u4fdd\u4e0d\u4f1a\u5f15\u5165 \u5dee\u4e00\u9519\u8bef \u3002","title":"\u6570\u7ec4\u7684\u201d\u5dee\u4e00\u201c\u9519\u8bef"},{"location":"11-4-Sorting-an-array-using-selection-sort/","tags":["array","sort"],"text":"Key Takeaway \u6392\u5e8f \u6570\u7ec4\u6392\u5e8f\u662f\u5c06\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u6309\u7279\u5b9a\u987a\u5e8f\u6392\u5217\u7684\u8fc7\u7a0b\u3002\u6392\u5e8f\u5728\u5f88\u591a\u65f6\u5019\u90fd\u662f\u6709\u7528\u7684\u3002\u4f8b\u5982\uff0c\u7535\u5b50\u90ae\u4ef6\u7a0b\u5e8f\u901a\u5e38\u9700\u8981\u6309\u7167\u6536\u4ef6\u65f6\u95f4\u987a\u5e8f\u663e\u793a\u7535\u5b50\u90ae\u4ef6\uff0c\u56e0\u4e3a\u6700\u65b0\u7684\u7535\u5b50\u90ae\u4ef6\u901a\u5e38\u88ab\u8ba4\u4e3a\u5f53\u524d\u9700\u8981\u5173\u6ce8\u7684\u3002\u5f53\u4f60\u6253\u5f00\u4f60\u7684\u8054\u7cfb\u4eba\u5217\u8868\u65f6\uff0c\u540d\u5b57\u901a\u5e38\u662f\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u5217\u7684\uff0c\u56e0\u4e3a\u8fd9\u6837\u66f4\u5bb9\u6613\u627e\u5230\u4f60\u8981\u627e\u7684\u540d\u5b57\u3002\u8fd9\u4e24\u4e2a\u4f8b\u5b50\u90fd\u6d89\u53ca\u5728\u663e\u793a\u6570\u636e\u4e4b\u524d\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\u3002 \u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u53ef\u4ee5\u63d0\u9ad8\u641c\u7d22\u6570\u7ec4\u7684\u6548\u7387\uff0c\u4e0d\u4ec5\u5bf9\u4eba\u7c7b\uff0c\u5bf9\u8ba1\u7b97\u673a\u4e5f\u662f\u5982\u6b64\u3002\u4f8b\u5982\uff0c\u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u60f3\u77e5\u9053\u4e00\u4e2a\u540d\u5b57\u662f\u5426\u51fa\u73b0\u5728\u4e00\u4e2a\u540d\u5b57\u5217\u8868\u4e2d\u3002\u4e3a\u4e86\u67e5\u770b\u540d\u79f0\u662f\u5426\u5728\u5217\u8868\u4e2d\uff0c\u6211\u4eec\u5fc5\u987b\u68c0\u67e5\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u4ee5\u786e\u5b9a\u540d\u79f0\u662f\u5426\u51fa\u73b0\u3002\u5bf9\u4e8e\u5305\u542b\u8bb8\u591a\u5143\u7d20\u7684\u6570\u7ec4\uff0c\u641c\u7d22\u6240\u6709\u5143\u7d20\u7684\u5f00\u9500\u53ef\u80fd\u5f88\u5927\u3002 \u4f46\u662f\uff0c\u73b0\u5728\u5047\u8bbe\u6211\u4eec\u7684\u540d\u79f0\u6570\u7ec4\u662f\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u5e8f\u7684\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u53ea\u9700\u8981\u641c\u7d22\u5230\u5728\u5b57\u6bcd\u987a\u5e8f\u4e0a\u6bd4\u6211\u4eec\u8981\u67e5\u627e\u7684\u540d\u79f0\u5927\u7684\u540d\u79f0\u3002\u8fd9\u65f6\uff0c\u5982\u679c\u6211\u4eec\u8fd8\u6ca1\u6709\u627e\u5230\u8fd9\u4e2a\u540d\u5b57\uff0c\u6211\u4eec\u5c31\u77e5\u9053\u5b83\u4e0d\u5b58\u5728\u4e8e\u6570\u7ec4\u7684\u5176\u4ed6\u90e8\u5206\uff0c\u56e0\u4e3a\u6211\u4eec\u5728\u6570\u7ec4\u4e2d\u6ca1\u6709\u770b\u5230\u7684\u6240\u6709\u540d\u5b57\u90fd\u4fdd\u8bc1\u5728\u5b57\u6bcd\u987a\u5e8f\u4e0a\u66f4\u5927! \u5176\u5b9e\u8fd8\u6709\u66f4\u597d\u7684\u7b97\u6cd5\u6765\u641c\u7d22\u6392\u5e8f\u6570\u7ec4\u3002\u4f7f\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u641c\u7d22\u4e00\u4e2a\u5305\u542b1,000,000\u4e2a\u5143\u7d20\u7684\u6392\u5e8f\u6570\u7ec4\uff0c\u53ea\u9700\u8981\u8fdb\u884c20\u6b21\u6bd4\u8f83!\u5f53\u7136\uff0c\u5176\u7f3a\u70b9\u662f\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u7684\u4ee3\u4ef7\u76f8\u5bf9\u8f83\u9ad8\uff0c\u800c\u4e14\u901a\u5e38\u4e0d\u503c\u5f97\u4e3a\u4e86\u5feb\u901f\u641c\u7d22\u800c\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u9664\u975e\u60a8\u8981\u591a\u6b21\u641c\u7d22\u5b83\u3002 \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u540e\u5c31\u4e0d\u9700\u8981\u518d\u641c\u7d22\u4e86\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u627e\u5230\u6700\u597d\u7684\u8003\u8bd5\u6210\u7ee9\u3002\u5982\u679c\u6570\u7ec4\u662f\u65e0\u5e8f\u7684\uff0c\u6211\u4eec\u5fc5\u987b\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u4ee5\u627e\u5230\u6700\u5927\u7684\u6d4b\u8bd5\u5206\u6570\u3002\u5982\u679c\u5217\u8868\u662f\u6392\u5e8f\u7684\uff0c\u6700\u597d\u7684\u6d4b\u8bd5\u5206\u6570\u5c06\u5728\u7b2c\u4e00\u4e2a\u6216\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e(\u53d6\u51b3\u4e8e\u6211\u4eec\u662f\u6309\u5347\u5e8f\u8fd8\u662f\u964d\u5e8f\u6392\u5e8f)\uff0c\u6240\u4ee5\u6211\u4eec\u6839\u672c\u4e0d\u9700\u8981\u641c\u7d22! \u6392\u5e8f\u7684\u539f\u7406 \u6392\u5e8f\u901a\u5e38\u662f\u901a\u8fc7\u53cd\u590d\u6bd4\u8f83\u6570\u7ec4\u5143\u7d20\u5bf9\u6765\u6267\u884c\u7684\uff0c\u5982\u679c\u5b83\u4eec\u6ee1\u8db3\u67d0\u4e9b\u9884\u5b9a\u4e49\u7684\u6761\u4ef6\uff0c\u5219\u4ea4\u6362\u5b83\u4eec\u3002\u6839\u636e\u4f7f\u7528\u7684\u6392\u5e8f\u7b97\u6cd5\u7684\u4e0d\u540c\uff0c\u6bd4\u8f83\u8fd9\u4e9b\u5143\u7d20\u7684\u987a\u5e8f\u4e5f\u4e0d\u540c\u3002\u6807\u51c6\u53d6\u51b3\u4e8e\u5217\u8868\u5c06\u5982\u4f55\u6392\u5e8f(\u4f8b\u5982\uff0c\u5347\u5e8f\u6216\u964d\u5e8f)\u3002 \u8981\u4ea4\u6362\u4e24\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528c++\u6807\u51c6\u5e93\u4e2d\u7684 std::swap() \u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5728 \u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include int main () { int x { 2 }; int y { 4 }; std :: cout << \"Before swap: x = \" << x << \", y = \" << y << '\\n' ; std :: swap ( x , y ); // swap the values of x and y std :: cout << \"After swap: x = \" << x << \", y = \" << y << '\\n' ; return 0 ; } \u7a0b\u5e8f\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Before swap: x = 2, y = 4 After swap: x = 4, y = 2 \u6ce8\u610f\uff0c\u4f7f\u7528 swap \u540e\uff0c x \u548c y \u7684\u503c\u5c31\u88ab\u4ea4\u6362\u4e86\uff01 \u9009\u62e9\u6392\u5e8f \u6709\u5f88\u591a\u65b9\u6cd5\u53ef\u4ee5\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002 \u9009\u62e9\u6392\u5e8f \u53ef\u80fd\u662f\u6700\u5bb9\u6613\u7406\u89e3\u7684\u6392\u5e8f\uff0c\u8fd9\u4f7f\u5b83\u5f88\u9002\u5408\u4f5c\u4e3a\u6559\u5b66\u793a\u4f8b\uff08\u5c3d\u7ba1\u5b83\u7684\u6392\u5e8f\u901f\u5ea6\u5e76\u4e0d\u5feb\uff09\u3002 \u9009\u62e9\u6392\u5e8f\u7684\u6b65\u9aa4\u5982\u4e0b\uff08\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\uff09\uff1a \u4ece\u7d22\u5f15 0\u5f00\u59cb\uff0c\u904d\u5386\u6570\u7ec4\u67e5\u627e\u6700\u5c0f\u7684\u503c\uff1b \u4ea4\u6362\u6700\u5c0f\u503c\u548c\u7d22\u5f15\u4f4d\u7f6e\u4e3a 0 \u7684\u5143\u7d20\uff1b \u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u7d22\u5f15\u91cd\u590d\u4e0a\u8ff0\u6b65\u9aa4\u3002 \u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u8981\u627e\u5230\u6570\u7ec4\u4e2d\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u7136\u540e\u628a\u5b83\u4ea4\u6362\u5230\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u3002\u7136\u540e\u6211\u4eec\u8981\u627e\u5230\u4e0b\u4e00\u4e2a\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u7136\u540e\u628a\u5b83\u4ea4\u6362\u5230\u7b2c\u4e8c\u4e2a\u4f4d\u7f6e\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5c06\u91cd\u590d\u8fdb\u884c\uff0c\u76f4\u5230\u8017\u5c3d\u6240\u6709\u5143\u7d20\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u5173\u4e8e5\u4e2a\u5143\u7d20\u7684\u7b97\u6cd5\u7684\u4f8b\u5b50\u3002\u8ba9\u6211\u4eec\u4ece\u4e00\u4e2a\u793a\u4f8b\u6570\u7ec4\u5f00\u59cb { 30, 50, 20, 10, 40 } \u4ece\u7d22\u5f15 0 \u5f00\u59cb\uff0c\u627e\u5230\u6700\u5c0f\u7684\u5143\u7d20 { 30, 50, 20, 10 , 40 } \u4ea4\u6362\u5230\u7d22\u5f15 0 \u7684\u4f4d\u7f6e { 10 , 50, 20, 30 , 40 } \u7b2c\u4e00\u4e2a\u5143\u7d20\u5df2\u7ecf\u6392\u5e8f\u5b8c\u6210\uff0c\u540e\u9762\u5c31\u4e0d\u7528\u7ba1\u4e86\u3002\u7136\u540e\u4ece\u7d22\u5f15 1 \u5f00\u59cb\uff0c\u7ee7\u7eed\u67e5\u627e\u6700\u5c0f\u7684\u5143\u7d20\uff1a { 10 , 50, 20 , 30, 40 } \u5c06\u627e\u5230\u7684\u503c\u4ea4\u6362\u5230\u7d22\u5f15 1 \u7684\u4f4d\u7f6e\uff1a { 10 , 20 , 50 , 30, 40 } \u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u5ffd\u7565\u524d\u4e24\u4e2a\u5143\u7d20\u3002\u627e\u5230\u4ece\u7d22\u5f152\u5f00\u59cb\u7684\u6700\u5c0f\u5143\u7d20\uff1a { 10 , 20 , 50, 30 , 40 } \u7136\u540e\u5c06\u5b83\u4e0e\u7d22\u5f152\u4e2d\u7684\u5143\u7d20\u4ea4\u6362\uff1a { 10 , 20 , 30 , 50 , 40 } \u627e\u5230\u4ece\u7d22\u5f153\u5f00\u59cb\u7684\u6700\u5c0f\u5143\u7d20\uff1a { 10 , 20 , 30 , 50, 40 } \u7136\u540e\u5c06\u5b83\u4e0e\u7d22\u5f153\u4e2d\u7684\u5143\u7d20\u4ea4\u6362\uff1a { 10 , 20 , 30 , 40 , 50 } \u6700\u540e\uff0c\u627e\u5230\u4ece\u7d22\u5f154\u5f00\u59cb\u7684\u6700\u5c0f\u5143\u7d20\uff1a { 10 , 20 , 30 , 40 , 50 } \u7136\u540e\u5c06\u5b83\u4e0e\u7d22\u5f154\u4e2d\u7684\u5143\u7d20\u4ea4\u6362(\u5b9e\u9645\u4e0a\u4ec0\u4e48\u4e5f\u6ca1\u505a)\uff1a { 10 , 20 , 30 , 40 , 50 } \u5b8c\u6210\uff01 { 10, 20, 30, 40, 50 } \u6ce8\u610f\uff0c\u6700\u540e\u4e00\u6b21\u6bd4\u8f83\u603b\u662f\u4e0e\u81ea\u8eab\u8fdb\u884c\u6bd4\u8f83(\u8fd9\u662f\u5197\u4f59\u7684)\uff0c\u56e0\u6b64\u6211\u4eec\u5b9e\u9645\u4e0a\u53ef\u4ee5\u5728\u6570\u7ec4\u7ed3\u675f\u524d\u505c\u6b621\u4e2a\u5143\u7d20\u3002 C++ \u4e2d\u7684\u9009\u62e9\u6392\u5e8f C++ \u7684\u5b9e\u73b0\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include #include #include int main () { int array []{ 30 , 50 , 20 , 10 , 40 }; constexpr int length { static_cast < int > ( std :: size ( array )) }; // Step through each element of the array // (except the last one, which will already be sorted by the time we get there) for ( int startIndex { 0 }; startIndex < length - 1 ; ++ startIndex ) { // smallestIndex is the index of the smallest element we\u2019ve encountered this iteration // Start by assuming the smallest element is the first element of this iteration int smallestIndex { startIndex }; // Then look for a smaller element in the rest of the array for ( int currentIndex { startIndex + 1 }; currentIndex < length ; ++ currentIndex ) { // If we've found an element that is smaller than our previously found smallest if ( array [ currentIndex ] < array [ smallestIndex ]) // then keep track of it smallestIndex = currentIndex ; } // smallestIndex is now the smallest element in the remaining array // swap our start element with our smallest element (this sorts it into the correct place) std :: swap ( array [ startIndex ], array [ smallestIndex ]); } // Now that the whole array is sorted, print our sorted array as proof it works for ( int index { 0 }; index < length ; ++ index ) std :: cout << array [ index ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7b97\u6cd5\u6700\u4ee4\u4eba\u56f0\u60d1\u7684\u90e8\u5206\u662f\u4e00\u4e2a\u5faa\u73af\u4e2d\u7684\u5faa\u73af(\u79f0\u4e3a \u5d4c\u5957\u5faa\u73af )\u3002\u5916\u5c42\u5faa\u73af(startIndex)\u9010\u4e2a\u904d\u5386\u6bcf\u4e2a\u5143\u7d20\u3002\u5bf9\u4e8e\u5916\u5c42\u5faa\u73af\u7684\u6bcf\u6b21\u8fed\u4ee3\uff0c\u4f7f\u7528\u5185\u90e8\u5faa\u73af(currentIndex)\u67e5\u627e\u5269\u4f59\u6570\u7ec4\u4e2d\u6700\u5c0f\u7684\u5143\u7d20(\u4ecestartIndex+1\u5f00\u59cb)\u3002smallstindex\u8ddf\u8e2a\u5185\u90e8\u5faa\u73af\u627e\u5230\u7684\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\u3002\u7136\u540e\u7528startIndex\u4ea4\u6362smallstindex\u3002\u6700\u540e\uff0c\u5916\u5c42\u5faa\u73af(startIndex)\u524d\u8fdb\u4e00\u4e2a\u5143\u7d20\uff0c\u7136\u540e\u91cd\u590d\u8fd9\u4e2a\u8fc7\u7a0b\u3002 \u63d0\u793a:\u5982\u679c\u60a8\u5728\u5f04\u6e05\u695a\u4e0a\u8ff0\u7a0b\u5e8f\u5982\u4f55\u5de5\u4f5c\u65b9\u9762\u6709\u56f0\u96be\uff0c\u90a3\u4e48\u5728\u4e00\u5f20\u7eb8\u4e0a\u5b8c\u6210\u4e00\u4e2a\u793a\u4f8b\u6848\u4f8b\u4f1a\u5f88\u6709\u5e2e\u52a9\u3002\u5728\u7eb8\u5f20\u7684\u9876\u90e8\u6c34\u5e73\u5199\u5165\u8d77\u59cb(\u672a\u6392\u5e8f)\u6570\u7ec4\u5143\u7d20\u3002\u753b\u7bad\u5934\u8868\u793astartIndex\u3001currentIndex\u548csmallstindex\u6b63\u5728\u7d22\u5f15\u54ea\u4e9b\u5143\u7d20\u3002\u624b\u52a8\u8ddf\u8e2a\u6574\u4e2a\u7a0b\u5e8f\uff0c\u5e76\u5728\u7d22\u5f15\u53d8\u5316\u65f6\u91cd\u65b0\u7ed8\u5236\u7bad\u5934\u3002\u5bf9\u4e8e\u5916\u5c42\u5faa\u73af\u7684\u6bcf\u6b21\u8fed\u4ee3\uff0c\u5f00\u59cb\u4e00\u4e2a\u65b0\u884c\uff0c\u663e\u793a\u6570\u7ec4\u7684\u5f53\u524d\u72b6\u6001\u3002 \u540d\u79f0\u7684\u6392\u5e8f\u4f7f\u7528\u76f8\u540c\u7684\u7b97\u6cd5\u3002\u53ea\u9700\u5c06\u6570\u7ec4\u7c7b\u578b\u4ece int \u6539\u4e3a std::string \uff0c\u5e76\u4f7f\u7528\u9002\u5f53\u7684\u503c\u521d\u59cb\u5316\u5373\u53ef\u3002 std::sort \u56e0\u4e3a\u6570\u7ec4\u7684\u6392\u5e8f\u975e\u5e38\u5e38\u89c1\uff0cC++\u6807\u51c6\u5e93\u4e2d\u5305\u542b\u4e86\u4e00\u4e2a\u540d\u4e3a std::sort \u7684\u6392\u5e8f\u51fd\u6570\u3002 std::sort \u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u50cf\u8fd9\u6837\u5728\u6570\u7ec4\u4e2d\u8c03\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // for std::sort #include #include // for std::size int main () { int array []{ 30 , 50 , 20 , 10 , 40 }; std :: sort ( std :: begin ( array ), std :: end ( array )); for ( int i { 0 }; i < static_cast < int > ( std :: size ( array )); ++ i ) std :: cout << array [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c std::sort \u6309\u5347\u5e8f\u6392\u5e8f\uff0c\u4f7f\u7528 operator< \u6bd4\u8f83\u5143\u7d20\u5bf9\uff0c\u5e76\u5728\u5fc5\u8981\u65f6\u4ea4\u6362\u5b83\u4eec(\u5f88\u50cf\u4e0a\u9762\u7684\u9009\u62e9\u6392\u5e8f\u793a\u4f8b\u6240\u505a\u7684)\u3002 \u6211\u4eec\u5c06\u5728\u4ee5\u540e\u7684\u7ae0\u8282\u4e2d\u66f4\u591a\u5730\u8ba8\u8bba std::sort \u3002","title":"11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f"},{"location":"11-4-Sorting-an-array-using-selection-sort/#_1","text":"\u6570\u7ec4\u6392\u5e8f\u662f\u5c06\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u6309\u7279\u5b9a\u987a\u5e8f\u6392\u5217\u7684\u8fc7\u7a0b\u3002\u6392\u5e8f\u5728\u5f88\u591a\u65f6\u5019\u90fd\u662f\u6709\u7528\u7684\u3002\u4f8b\u5982\uff0c\u7535\u5b50\u90ae\u4ef6\u7a0b\u5e8f\u901a\u5e38\u9700\u8981\u6309\u7167\u6536\u4ef6\u65f6\u95f4\u987a\u5e8f\u663e\u793a\u7535\u5b50\u90ae\u4ef6\uff0c\u56e0\u4e3a\u6700\u65b0\u7684\u7535\u5b50\u90ae\u4ef6\u901a\u5e38\u88ab\u8ba4\u4e3a\u5f53\u524d\u9700\u8981\u5173\u6ce8\u7684\u3002\u5f53\u4f60\u6253\u5f00\u4f60\u7684\u8054\u7cfb\u4eba\u5217\u8868\u65f6\uff0c\u540d\u5b57\u901a\u5e38\u662f\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u5217\u7684\uff0c\u56e0\u4e3a\u8fd9\u6837\u66f4\u5bb9\u6613\u627e\u5230\u4f60\u8981\u627e\u7684\u540d\u5b57\u3002\u8fd9\u4e24\u4e2a\u4f8b\u5b50\u90fd\u6d89\u53ca\u5728\u663e\u793a\u6570\u636e\u4e4b\u524d\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\u3002 \u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u53ef\u4ee5\u63d0\u9ad8\u641c\u7d22\u6570\u7ec4\u7684\u6548\u7387\uff0c\u4e0d\u4ec5\u5bf9\u4eba\u7c7b\uff0c\u5bf9\u8ba1\u7b97\u673a\u4e5f\u662f\u5982\u6b64\u3002\u4f8b\u5982\uff0c\u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u60f3\u77e5\u9053\u4e00\u4e2a\u540d\u5b57\u662f\u5426\u51fa\u73b0\u5728\u4e00\u4e2a\u540d\u5b57\u5217\u8868\u4e2d\u3002\u4e3a\u4e86\u67e5\u770b\u540d\u79f0\u662f\u5426\u5728\u5217\u8868\u4e2d\uff0c\u6211\u4eec\u5fc5\u987b\u68c0\u67e5\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u4ee5\u786e\u5b9a\u540d\u79f0\u662f\u5426\u51fa\u73b0\u3002\u5bf9\u4e8e\u5305\u542b\u8bb8\u591a\u5143\u7d20\u7684\u6570\u7ec4\uff0c\u641c\u7d22\u6240\u6709\u5143\u7d20\u7684\u5f00\u9500\u53ef\u80fd\u5f88\u5927\u3002 \u4f46\u662f\uff0c\u73b0\u5728\u5047\u8bbe\u6211\u4eec\u7684\u540d\u79f0\u6570\u7ec4\u662f\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u5e8f\u7684\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u53ea\u9700\u8981\u641c\u7d22\u5230\u5728\u5b57\u6bcd\u987a\u5e8f\u4e0a\u6bd4\u6211\u4eec\u8981\u67e5\u627e\u7684\u540d\u79f0\u5927\u7684\u540d\u79f0\u3002\u8fd9\u65f6\uff0c\u5982\u679c\u6211\u4eec\u8fd8\u6ca1\u6709\u627e\u5230\u8fd9\u4e2a\u540d\u5b57\uff0c\u6211\u4eec\u5c31\u77e5\u9053\u5b83\u4e0d\u5b58\u5728\u4e8e\u6570\u7ec4\u7684\u5176\u4ed6\u90e8\u5206\uff0c\u56e0\u4e3a\u6211\u4eec\u5728\u6570\u7ec4\u4e2d\u6ca1\u6709\u770b\u5230\u7684\u6240\u6709\u540d\u5b57\u90fd\u4fdd\u8bc1\u5728\u5b57\u6bcd\u987a\u5e8f\u4e0a\u66f4\u5927! \u5176\u5b9e\u8fd8\u6709\u66f4\u597d\u7684\u7b97\u6cd5\u6765\u641c\u7d22\u6392\u5e8f\u6570\u7ec4\u3002\u4f7f\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u641c\u7d22\u4e00\u4e2a\u5305\u542b1,000,000\u4e2a\u5143\u7d20\u7684\u6392\u5e8f\u6570\u7ec4\uff0c\u53ea\u9700\u8981\u8fdb\u884c20\u6b21\u6bd4\u8f83!\u5f53\u7136\uff0c\u5176\u7f3a\u70b9\u662f\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u7684\u4ee3\u4ef7\u76f8\u5bf9\u8f83\u9ad8\uff0c\u800c\u4e14\u901a\u5e38\u4e0d\u503c\u5f97\u4e3a\u4e86\u5feb\u901f\u641c\u7d22\u800c\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u9664\u975e\u60a8\u8981\u591a\u6b21\u641c\u7d22\u5b83\u3002 \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u540e\u5c31\u4e0d\u9700\u8981\u518d\u641c\u7d22\u4e86\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u627e\u5230\u6700\u597d\u7684\u8003\u8bd5\u6210\u7ee9\u3002\u5982\u679c\u6570\u7ec4\u662f\u65e0\u5e8f\u7684\uff0c\u6211\u4eec\u5fc5\u987b\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u4ee5\u627e\u5230\u6700\u5927\u7684\u6d4b\u8bd5\u5206\u6570\u3002\u5982\u679c\u5217\u8868\u662f\u6392\u5e8f\u7684\uff0c\u6700\u597d\u7684\u6d4b\u8bd5\u5206\u6570\u5c06\u5728\u7b2c\u4e00\u4e2a\u6216\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e(\u53d6\u51b3\u4e8e\u6211\u4eec\u662f\u6309\u5347\u5e8f\u8fd8\u662f\u964d\u5e8f\u6392\u5e8f)\uff0c\u6240\u4ee5\u6211\u4eec\u6839\u672c\u4e0d\u9700\u8981\u641c\u7d22!","title":"\u6392\u5e8f"},{"location":"11-4-Sorting-an-array-using-selection-sort/#_2","text":"\u6392\u5e8f\u901a\u5e38\u662f\u901a\u8fc7\u53cd\u590d\u6bd4\u8f83\u6570\u7ec4\u5143\u7d20\u5bf9\u6765\u6267\u884c\u7684\uff0c\u5982\u679c\u5b83\u4eec\u6ee1\u8db3\u67d0\u4e9b\u9884\u5b9a\u4e49\u7684\u6761\u4ef6\uff0c\u5219\u4ea4\u6362\u5b83\u4eec\u3002\u6839\u636e\u4f7f\u7528\u7684\u6392\u5e8f\u7b97\u6cd5\u7684\u4e0d\u540c\uff0c\u6bd4\u8f83\u8fd9\u4e9b\u5143\u7d20\u7684\u987a\u5e8f\u4e5f\u4e0d\u540c\u3002\u6807\u51c6\u53d6\u51b3\u4e8e\u5217\u8868\u5c06\u5982\u4f55\u6392\u5e8f(\u4f8b\u5982\uff0c\u5347\u5e8f\u6216\u964d\u5e8f)\u3002 \u8981\u4ea4\u6362\u4e24\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528c++\u6807\u51c6\u5e93\u4e2d\u7684 std::swap() \u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5728 \u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include int main () { int x { 2 }; int y { 4 }; std :: cout << \"Before swap: x = \" << x << \", y = \" << y << '\\n' ; std :: swap ( x , y ); // swap the values of x and y std :: cout << \"After swap: x = \" << x << \", y = \" << y << '\\n' ; return 0 ; } \u7a0b\u5e8f\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Before swap: x = 2, y = 4 After swap: x = 4, y = 2 \u6ce8\u610f\uff0c\u4f7f\u7528 swap \u540e\uff0c x \u548c y \u7684\u503c\u5c31\u88ab\u4ea4\u6362\u4e86\uff01","title":"\u6392\u5e8f\u7684\u539f\u7406"},{"location":"11-4-Sorting-an-array-using-selection-sort/#_3","text":"\u6709\u5f88\u591a\u65b9\u6cd5\u53ef\u4ee5\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002 \u9009\u62e9\u6392\u5e8f \u53ef\u80fd\u662f\u6700\u5bb9\u6613\u7406\u89e3\u7684\u6392\u5e8f\uff0c\u8fd9\u4f7f\u5b83\u5f88\u9002\u5408\u4f5c\u4e3a\u6559\u5b66\u793a\u4f8b\uff08\u5c3d\u7ba1\u5b83\u7684\u6392\u5e8f\u901f\u5ea6\u5e76\u4e0d\u5feb\uff09\u3002 \u9009\u62e9\u6392\u5e8f\u7684\u6b65\u9aa4\u5982\u4e0b\uff08\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\uff09\uff1a \u4ece\u7d22\u5f15 0\u5f00\u59cb\uff0c\u904d\u5386\u6570\u7ec4\u67e5\u627e\u6700\u5c0f\u7684\u503c\uff1b \u4ea4\u6362\u6700\u5c0f\u503c\u548c\u7d22\u5f15\u4f4d\u7f6e\u4e3a 0 \u7684\u5143\u7d20\uff1b \u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u7d22\u5f15\u91cd\u590d\u4e0a\u8ff0\u6b65\u9aa4\u3002 \u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u8981\u627e\u5230\u6570\u7ec4\u4e2d\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u7136\u540e\u628a\u5b83\u4ea4\u6362\u5230\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u3002\u7136\u540e\u6211\u4eec\u8981\u627e\u5230\u4e0b\u4e00\u4e2a\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u7136\u540e\u628a\u5b83\u4ea4\u6362\u5230\u7b2c\u4e8c\u4e2a\u4f4d\u7f6e\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5c06\u91cd\u590d\u8fdb\u884c\uff0c\u76f4\u5230\u8017\u5c3d\u6240\u6709\u5143\u7d20\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u5173\u4e8e5\u4e2a\u5143\u7d20\u7684\u7b97\u6cd5\u7684\u4f8b\u5b50\u3002\u8ba9\u6211\u4eec\u4ece\u4e00\u4e2a\u793a\u4f8b\u6570\u7ec4\u5f00\u59cb { 30, 50, 20, 10, 40 } \u4ece\u7d22\u5f15 0 \u5f00\u59cb\uff0c\u627e\u5230\u6700\u5c0f\u7684\u5143\u7d20 { 30, 50, 20, 10 , 40 } \u4ea4\u6362\u5230\u7d22\u5f15 0 \u7684\u4f4d\u7f6e { 10 , 50, 20, 30 , 40 } \u7b2c\u4e00\u4e2a\u5143\u7d20\u5df2\u7ecf\u6392\u5e8f\u5b8c\u6210\uff0c\u540e\u9762\u5c31\u4e0d\u7528\u7ba1\u4e86\u3002\u7136\u540e\u4ece\u7d22\u5f15 1 \u5f00\u59cb\uff0c\u7ee7\u7eed\u67e5\u627e\u6700\u5c0f\u7684\u5143\u7d20\uff1a { 10 , 50, 20 , 30, 40 } \u5c06\u627e\u5230\u7684\u503c\u4ea4\u6362\u5230\u7d22\u5f15 1 \u7684\u4f4d\u7f6e\uff1a { 10 , 20 , 50 , 30, 40 } \u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u5ffd\u7565\u524d\u4e24\u4e2a\u5143\u7d20\u3002\u627e\u5230\u4ece\u7d22\u5f152\u5f00\u59cb\u7684\u6700\u5c0f\u5143\u7d20\uff1a { 10 , 20 , 50, 30 , 40 } \u7136\u540e\u5c06\u5b83\u4e0e\u7d22\u5f152\u4e2d\u7684\u5143\u7d20\u4ea4\u6362\uff1a { 10 , 20 , 30 , 50 , 40 } \u627e\u5230\u4ece\u7d22\u5f153\u5f00\u59cb\u7684\u6700\u5c0f\u5143\u7d20\uff1a { 10 , 20 , 30 , 50, 40 } \u7136\u540e\u5c06\u5b83\u4e0e\u7d22\u5f153\u4e2d\u7684\u5143\u7d20\u4ea4\u6362\uff1a { 10 , 20 , 30 , 40 , 50 } \u6700\u540e\uff0c\u627e\u5230\u4ece\u7d22\u5f154\u5f00\u59cb\u7684\u6700\u5c0f\u5143\u7d20\uff1a { 10 , 20 , 30 , 40 , 50 } \u7136\u540e\u5c06\u5b83\u4e0e\u7d22\u5f154\u4e2d\u7684\u5143\u7d20\u4ea4\u6362(\u5b9e\u9645\u4e0a\u4ec0\u4e48\u4e5f\u6ca1\u505a)\uff1a { 10 , 20 , 30 , 40 , 50 } \u5b8c\u6210\uff01 { 10, 20, 30, 40, 50 } \u6ce8\u610f\uff0c\u6700\u540e\u4e00\u6b21\u6bd4\u8f83\u603b\u662f\u4e0e\u81ea\u8eab\u8fdb\u884c\u6bd4\u8f83(\u8fd9\u662f\u5197\u4f59\u7684)\uff0c\u56e0\u6b64\u6211\u4eec\u5b9e\u9645\u4e0a\u53ef\u4ee5\u5728\u6570\u7ec4\u7ed3\u675f\u524d\u505c\u6b621\u4e2a\u5143\u7d20\u3002","title":"\u9009\u62e9\u6392\u5e8f"},{"location":"11-4-Sorting-an-array-using-selection-sort/#c","text":"C++ \u7684\u5b9e\u73b0\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include #include #include int main () { int array []{ 30 , 50 , 20 , 10 , 40 }; constexpr int length { static_cast < int > ( std :: size ( array )) }; // Step through each element of the array // (except the last one, which will already be sorted by the time we get there) for ( int startIndex { 0 }; startIndex < length - 1 ; ++ startIndex ) { // smallestIndex is the index of the smallest element we\u2019ve encountered this iteration // Start by assuming the smallest element is the first element of this iteration int smallestIndex { startIndex }; // Then look for a smaller element in the rest of the array for ( int currentIndex { startIndex + 1 }; currentIndex < length ; ++ currentIndex ) { // If we've found an element that is smaller than our previously found smallest if ( array [ currentIndex ] < array [ smallestIndex ]) // then keep track of it smallestIndex = currentIndex ; } // smallestIndex is now the smallest element in the remaining array // swap our start element with our smallest element (this sorts it into the correct place) std :: swap ( array [ startIndex ], array [ smallestIndex ]); } // Now that the whole array is sorted, print our sorted array as proof it works for ( int index { 0 }; index < length ; ++ index ) std :: cout << array [ index ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7b97\u6cd5\u6700\u4ee4\u4eba\u56f0\u60d1\u7684\u90e8\u5206\u662f\u4e00\u4e2a\u5faa\u73af\u4e2d\u7684\u5faa\u73af(\u79f0\u4e3a \u5d4c\u5957\u5faa\u73af )\u3002\u5916\u5c42\u5faa\u73af(startIndex)\u9010\u4e2a\u904d\u5386\u6bcf\u4e2a\u5143\u7d20\u3002\u5bf9\u4e8e\u5916\u5c42\u5faa\u73af\u7684\u6bcf\u6b21\u8fed\u4ee3\uff0c\u4f7f\u7528\u5185\u90e8\u5faa\u73af(currentIndex)\u67e5\u627e\u5269\u4f59\u6570\u7ec4\u4e2d\u6700\u5c0f\u7684\u5143\u7d20(\u4ecestartIndex+1\u5f00\u59cb)\u3002smallstindex\u8ddf\u8e2a\u5185\u90e8\u5faa\u73af\u627e\u5230\u7684\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\u3002\u7136\u540e\u7528startIndex\u4ea4\u6362smallstindex\u3002\u6700\u540e\uff0c\u5916\u5c42\u5faa\u73af(startIndex)\u524d\u8fdb\u4e00\u4e2a\u5143\u7d20\uff0c\u7136\u540e\u91cd\u590d\u8fd9\u4e2a\u8fc7\u7a0b\u3002 \u63d0\u793a:\u5982\u679c\u60a8\u5728\u5f04\u6e05\u695a\u4e0a\u8ff0\u7a0b\u5e8f\u5982\u4f55\u5de5\u4f5c\u65b9\u9762\u6709\u56f0\u96be\uff0c\u90a3\u4e48\u5728\u4e00\u5f20\u7eb8\u4e0a\u5b8c\u6210\u4e00\u4e2a\u793a\u4f8b\u6848\u4f8b\u4f1a\u5f88\u6709\u5e2e\u52a9\u3002\u5728\u7eb8\u5f20\u7684\u9876\u90e8\u6c34\u5e73\u5199\u5165\u8d77\u59cb(\u672a\u6392\u5e8f)\u6570\u7ec4\u5143\u7d20\u3002\u753b\u7bad\u5934\u8868\u793astartIndex\u3001currentIndex\u548csmallstindex\u6b63\u5728\u7d22\u5f15\u54ea\u4e9b\u5143\u7d20\u3002\u624b\u52a8\u8ddf\u8e2a\u6574\u4e2a\u7a0b\u5e8f\uff0c\u5e76\u5728\u7d22\u5f15\u53d8\u5316\u65f6\u91cd\u65b0\u7ed8\u5236\u7bad\u5934\u3002\u5bf9\u4e8e\u5916\u5c42\u5faa\u73af\u7684\u6bcf\u6b21\u8fed\u4ee3\uff0c\u5f00\u59cb\u4e00\u4e2a\u65b0\u884c\uff0c\u663e\u793a\u6570\u7ec4\u7684\u5f53\u524d\u72b6\u6001\u3002 \u540d\u79f0\u7684\u6392\u5e8f\u4f7f\u7528\u76f8\u540c\u7684\u7b97\u6cd5\u3002\u53ea\u9700\u5c06\u6570\u7ec4\u7c7b\u578b\u4ece int \u6539\u4e3a std::string \uff0c\u5e76\u4f7f\u7528\u9002\u5f53\u7684\u503c\u521d\u59cb\u5316\u5373\u53ef\u3002","title":"C++ \u4e2d\u7684\u9009\u62e9\u6392\u5e8f"},{"location":"11-4-Sorting-an-array-using-selection-sort/#stdsort","text":"\u56e0\u4e3a\u6570\u7ec4\u7684\u6392\u5e8f\u975e\u5e38\u5e38\u89c1\uff0cC++\u6807\u51c6\u5e93\u4e2d\u5305\u542b\u4e86\u4e00\u4e2a\u540d\u4e3a std::sort \u7684\u6392\u5e8f\u51fd\u6570\u3002 std::sort \u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u50cf\u8fd9\u6837\u5728\u6570\u7ec4\u4e2d\u8c03\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // for std::sort #include #include // for std::size int main () { int array []{ 30 , 50 , 20 , 10 , 40 }; std :: sort ( std :: begin ( array ), std :: end ( array )); for ( int i { 0 }; i < static_cast < int > ( std :: size ( array )); ++ i ) std :: cout << array [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c std::sort \u6309\u5347\u5e8f\u6392\u5e8f\uff0c\u4f7f\u7528 operator< \u6bd4\u8f83\u5143\u7d20\u5bf9\uff0c\u5e76\u5728\u5fc5\u8981\u65f6\u4ea4\u6362\u5b83\u4eec(\u5f88\u50cf\u4e0a\u9762\u7684\u9009\u62e9\u6392\u5e8f\u793a\u4f8b\u6240\u505a\u7684)\u3002 \u6211\u4eec\u5c06\u5728\u4ee5\u540e\u7684\u7ae0\u8282\u4e2d\u66f4\u591a\u5730\u8ba8\u8bba std::sort \u3002","title":"std::sort"},{"location":"11-5-Multidimensional-Arrays/","tags":["array"],"text":"Key Takeaway \u4f7f\u7528 \u5d4c\u5957\u5927\u62ec\u53f7 \u521d\u59cb\u5316\u4e8c\u7ef4\u6570\u7ec4 \u5e26\u6709\u521d\u59cb\u5316\u5217\u8868\u7684\u4e8c\u7ef4\u6570\u7ec4\u53ea\u80fd\u5ffd\u7565\u6700\u5de6\u8fb9\u7684\u957f\u5ea6\u58f0\u660e \u6570\u7ec4\u7684\u5143\u7d20\u53ef\u4ee5\u662f\u4efb\u4f55\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6570\u7ec4\uff01\u6570\u7ec4\u7684\u6570\u7ec4\u88ab\u79f0\u4e3a \u591a\u7ef4\u6570\u7ec4 1 int array [ 3 ][ 5 ]; // a 3-element array of 5-element arrays \u56e0\u4e3a\u6570\u7ec4\u6709\u4e24\u4e2a\u4e0b\u6807\uff0c\u6240\u4ee5\u8fd9\u662f\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u3002 \u5728\u4e8c\u7ef4\u6570\u7ec4\u4e2d\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u7b2c\u4e00\u4e2a(\u5de6)\u4e0b\u6807\u89c6\u4e3a\u884c\uff0c\u800c\u5c06\u7b2c\u4e8c\u4e2a(\u53f3)\u4e0b\u6807\u89c6\u4e3a\u5217\u3002\u8fd9\u88ab\u79f0\u4e3a \u884c\u4e3b\u5e8f \u3002\u4e0a\u8ff0\u4e8c\u7ef4\u6570\u7ec4\u7684\u5e03\u5c40\u5982\u4e0b: 1 2 3 [0][0] [0][1] [0][2] [0][3] [0][4] // row 0 [1][0] [1][1] [1][2] [1][3] [1][4] // row 1 [2][0] [2][1] [2][2] [2][3] [2][4] // row 2 \u540c\u65f6\u4f7f\u7528\u4e24\u4e2a\u4e0b\u6807\u5c31\u53ef\u4ee5\u8bbf\u95ee\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff1a 1 array [ 2 ][ 3 ] = 7 ; \u521d\u59cb\u5316\u4e8c\u7ef4\u6570\u7ec4 \u8981\u521d\u59cb\u5316\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 \u5d4c\u5957\u5927\u62ec\u53f7 \uff0c\u6bcf\u7ec4\u6570\u5b57\u4ee3\u8868\u4e00\u884c\uff1a 1 2 3 4 5 6 int array [ 3 ][ 5 ] { { 1 , 2 , 3 , 4 , 5 }, // row 0 { 6 , 7 , 8 , 9 , 10 }, // row 1 { 11 , 12 , 13 , 14 , 15 } // row 2 }; \u5c3d\u7ba1\u6709\u4e9b\u7f16\u8bd1\u5668\u5141\u8bb8\u4f60\u7701\u7565\u5185\u5c42\u5927\u62ec\u53f7\uff0c\u4f46\u6211\u4eec\u5f3a\u70c8\u5efa\u8bae\u60a8\u65e0\u8bba\u5982\u4f55\u90fd\u5305\u542b\u5b83\u4eec\uff0c\u8fd9\u65e2\u662f\u4e3a\u4e86\u53ef\u8bfb\u6027\uff0c\u4e5f\u662f\u56e0\u4e3aC++\u4f1a\u7528 0 \u66ff\u6362\u7f3a\u5c11\u7684\u521d\u59cb\u5316\u503c\u3002 1 2 3 4 5 6 int array [ 3 ][ 5 ] { { 1 , 2 }, // row 0 = 1, 2, 0, 0, 0 { 6 , 7 , 8 }, // row 1 = 6, 7, 8, 0, 0 { 11 , 12 , 13 , 14 } // row 2 = 11, 12, 13, 14, 0 }; \u5e26\u6709\u521d\u59cb\u5316\u5217\u8868\u7684\u4e8c\u7ef4\u6570\u7ec4\u53ea\u80fd\u5ffd\u7565\u6700\u5de6\u8fb9\u7684\u957f\u5ea6\u58f0\u660e\uff1a 1 2 3 4 5 6 int array [][ 5 ] { { 1 , 2 , 3 , 4 , 5 }, { 6 , 7 , 8 , 9 , 10 }, { 11 , 12 , 13 , 14 , 15 } }; \u7f16\u8bd1\u5668\u53ef\u4ee5\u8ba1\u7b97\u51fa\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u4f46\u662f\uff0c\u4e0b\u9762\u8fd9\u6837\u662f\u4e0d\u5141\u8bb8\u7684\uff1a 1 2 3 4 5 int array [][] { { 1 , 2 , 3 , 4 }, { 5 , 6 , 7 , 8 } }; \u5c31\u50cf\u666e\u901a\u6570\u7ec4\u4e00\u6837\uff0c\u591a\u7ef4\u6570\u7ec4\u4ecd\u7136\u53ef\u4ee5\u521d\u59cb\u5316\u4e3a0\uff0c\u5982\u4e0b\u6240\u793a: 1 int array [ 3 ][ 5 ]{}; \u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20 \u8bbf\u95ee\u4e8c\u7ef4\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\u9700\u8981\u4e24\u4e2a\u5faa\u73af\uff1a\u4e00\u4e2a\u7528\u4e8e\u884c\uff0c\u4e00\u4e2a\u7528\u4e8e\u5217\u3002\u7531\u4e8e\u4e8c\u7ef4\u6570\u7ec4\u901a\u5e38\u662f\u9010\u884c\u8bbf\u95ee\u7684\uff0c\u6240\u4ee5 \u884c\u7d22\u5f15\u901a\u5e38\u7528\u4f5c\u5916\u90e8\u5faa\u73af \u3002 1 2 3 4 5 6 7 for ( int row { 0 }; row < numRows ; ++ row ) // step through the rows in the array { for ( int col { 0 }; col < numCols ; ++ col ) // step through each element in the row { std :: cout << array [ row ][ col ]; } } \u5728 C++11 \u4e2d\uff0c for-each \u5faa\u73af\u4e5f\u53ef\u4ee5\u7528\u4e8e\u591a\u7ef4\u6570\u7ec4\uff0c\u6211\u4eec\u4f1a\u5728 11.11 - for-each \u5faa\u73af \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u8d85\u8fc7\u4e8c\u7ef4\u7684\u591a\u7ef4\u6570\u7ec4 \u591a\u7ef4\u6570\u7ec4\u7684\u7ef4\u6570\u53ef\u4ee5\u8d85\u8fc72\u7ef4\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4e09\u7ef4\u6570\u7ec4\u7684\u58f0\u660e\uff1a 1 int array [ 5 ][ 4 ][ 3 ]; \u4e09\u7ef4\u6570\u7ec4\u6ca1\u529e\u6cd5\u4ee5\u7b26\u5408\u76f4\u89c9\u7684\u65b9\u5f0f\u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u6765\u521d\u59cb\u5316\uff0c\u6240\u4ee5\u6700\u597d\u5c06\u5176\u521d\u59cb\u5316\u4e3a0\u7136\u540e\u518d\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u663e\u5f0f\u5730\u521d\u59cb\u5316\u3002 \u8bbf\u95ee\u4e09\u7ef4\u6570\u7ec4\u5143\u7d20\u7684\u65b9\u6cd5\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u662f\u7c7b\u4f3c\u7684\uff1a 1 std :: cout << array [ 3 ][ 1 ][ 2 ]; \u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u7684\u4f8b\u5b50 \u8ba9\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u7684\u5b9e\u9645\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include int main () { constexpr int numRows { 10 }; constexpr int numCols { 10 }; // Declare a 10x10 array int product [ numRows ][ numCols ]{}; // Calculate a multiplication table for ( int row { 1 }; row < numRows ; ++ row ) { for ( int col { 1 }; col < numCols ; ++ col ) { product [ row ][ col ] = row * col ; } } // Print the table for ( int row { 1 }; row < numRows ; ++ row ) { for ( int col { 1 }; col < numCols ; ++ col ) { std :: cout << product [ row ][ col ] << '\\t' ; } std :: cout << '\\n' ; } return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u8ba1\u7b97\u5e76\u6253\u53701\u52309(\u5305\u62ec9)\u4e4b\u95f4\u6240\u6709\u503c\u7684\u4e58\u6cd5\u8868\u3002\u6ce8\u610f\uff0c\u5728\u6253\u5370\u8868\u65f6\uff0cfor\u5faa\u73af\u4ece1\u5f00\u59cb\uff0c\u800c\u4e0d\u662f\u4ece0\u5f00\u59cb\u3002\u8f93\u51fa\u5982\u4e0b: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 48 54 7 14 21 28 35 42 49 56 63 8 16 24 32 40 48 56 64 72 9 18 27 36 45 54 63 72 81 \u4e8c\u7ef4\u6570\u7ec4\u901a\u5e38\u7528\u4e8e\u57fa\u4e8e\u8d34\u56fe\u7684\u6e38\u620f\u4e2d\uff0c\u5176\u4e2d\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u4ee3\u8868\u4e00\u4e2a\u8d34\u56fe\u3002\u5b83\u4eec\u4e5f\u88ab\u7528\u4e8e3d\u8ba1\u7b97\u673a\u56fe\u5f62(\u4f5c\u4e3a\u77e9\u9635)\uff0c\u4ee5\u65cb\u8f6c\u3001\u7f29\u653e\u548c\u8868\u793a\u5f62\u72b6\u3002","title":"11.5 - \u591a\u7ef4\u6570\u7ec4"},{"location":"11-5-Multidimensional-Arrays/#_1","text":"\u8981\u521d\u59cb\u5316\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 \u5d4c\u5957\u5927\u62ec\u53f7 \uff0c\u6bcf\u7ec4\u6570\u5b57\u4ee3\u8868\u4e00\u884c\uff1a 1 2 3 4 5 6 int array [ 3 ][ 5 ] { { 1 , 2 , 3 , 4 , 5 }, // row 0 { 6 , 7 , 8 , 9 , 10 }, // row 1 { 11 , 12 , 13 , 14 , 15 } // row 2 }; \u5c3d\u7ba1\u6709\u4e9b\u7f16\u8bd1\u5668\u5141\u8bb8\u4f60\u7701\u7565\u5185\u5c42\u5927\u62ec\u53f7\uff0c\u4f46\u6211\u4eec\u5f3a\u70c8\u5efa\u8bae\u60a8\u65e0\u8bba\u5982\u4f55\u90fd\u5305\u542b\u5b83\u4eec\uff0c\u8fd9\u65e2\u662f\u4e3a\u4e86\u53ef\u8bfb\u6027\uff0c\u4e5f\u662f\u56e0\u4e3aC++\u4f1a\u7528 0 \u66ff\u6362\u7f3a\u5c11\u7684\u521d\u59cb\u5316\u503c\u3002 1 2 3 4 5 6 int array [ 3 ][ 5 ] { { 1 , 2 }, // row 0 = 1, 2, 0, 0, 0 { 6 , 7 , 8 }, // row 1 = 6, 7, 8, 0, 0 { 11 , 12 , 13 , 14 } // row 2 = 11, 12, 13, 14, 0 }; \u5e26\u6709\u521d\u59cb\u5316\u5217\u8868\u7684\u4e8c\u7ef4\u6570\u7ec4\u53ea\u80fd\u5ffd\u7565\u6700\u5de6\u8fb9\u7684\u957f\u5ea6\u58f0\u660e\uff1a 1 2 3 4 5 6 int array [][ 5 ] { { 1 , 2 , 3 , 4 , 5 }, { 6 , 7 , 8 , 9 , 10 }, { 11 , 12 , 13 , 14 , 15 } }; \u7f16\u8bd1\u5668\u53ef\u4ee5\u8ba1\u7b97\u51fa\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u4f46\u662f\uff0c\u4e0b\u9762\u8fd9\u6837\u662f\u4e0d\u5141\u8bb8\u7684\uff1a 1 2 3 4 5 int array [][] { { 1 , 2 , 3 , 4 }, { 5 , 6 , 7 , 8 } }; \u5c31\u50cf\u666e\u901a\u6570\u7ec4\u4e00\u6837\uff0c\u591a\u7ef4\u6570\u7ec4\u4ecd\u7136\u53ef\u4ee5\u521d\u59cb\u5316\u4e3a0\uff0c\u5982\u4e0b\u6240\u793a: 1 int array [ 3 ][ 5 ]{};","title":"\u521d\u59cb\u5316\u4e8c\u7ef4\u6570\u7ec4"},{"location":"11-5-Multidimensional-Arrays/#_2","text":"\u8bbf\u95ee\u4e8c\u7ef4\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\u9700\u8981\u4e24\u4e2a\u5faa\u73af\uff1a\u4e00\u4e2a\u7528\u4e8e\u884c\uff0c\u4e00\u4e2a\u7528\u4e8e\u5217\u3002\u7531\u4e8e\u4e8c\u7ef4\u6570\u7ec4\u901a\u5e38\u662f\u9010\u884c\u8bbf\u95ee\u7684\uff0c\u6240\u4ee5 \u884c\u7d22\u5f15\u901a\u5e38\u7528\u4f5c\u5916\u90e8\u5faa\u73af \u3002 1 2 3 4 5 6 7 for ( int row { 0 }; row < numRows ; ++ row ) // step through the rows in the array { for ( int col { 0 }; col < numCols ; ++ col ) // step through each element in the row { std :: cout << array [ row ][ col ]; } } \u5728 C++11 \u4e2d\uff0c for-each \u5faa\u73af\u4e5f\u53ef\u4ee5\u7528\u4e8e\u591a\u7ef4\u6570\u7ec4\uff0c\u6211\u4eec\u4f1a\u5728 11.11 - for-each \u5faa\u73af \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20"},{"location":"11-5-Multidimensional-Arrays/#_3","text":"\u591a\u7ef4\u6570\u7ec4\u7684\u7ef4\u6570\u53ef\u4ee5\u8d85\u8fc72\u7ef4\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4e09\u7ef4\u6570\u7ec4\u7684\u58f0\u660e\uff1a 1 int array [ 5 ][ 4 ][ 3 ]; \u4e09\u7ef4\u6570\u7ec4\u6ca1\u529e\u6cd5\u4ee5\u7b26\u5408\u76f4\u89c9\u7684\u65b9\u5f0f\u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u6765\u521d\u59cb\u5316\uff0c\u6240\u4ee5\u6700\u597d\u5c06\u5176\u521d\u59cb\u5316\u4e3a0\u7136\u540e\u518d\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u663e\u5f0f\u5730\u521d\u59cb\u5316\u3002 \u8bbf\u95ee\u4e09\u7ef4\u6570\u7ec4\u5143\u7d20\u7684\u65b9\u6cd5\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u662f\u7c7b\u4f3c\u7684\uff1a 1 std :: cout << array [ 3 ][ 1 ][ 2 ];","title":"\u8d85\u8fc7\u4e8c\u7ef4\u7684\u591a\u7ef4\u6570\u7ec4"},{"location":"11-5-Multidimensional-Arrays/#_4","text":"\u8ba9\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u7684\u5b9e\u9645\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include int main () { constexpr int numRows { 10 }; constexpr int numCols { 10 }; // Declare a 10x10 array int product [ numRows ][ numCols ]{}; // Calculate a multiplication table for ( int row { 1 }; row < numRows ; ++ row ) { for ( int col { 1 }; col < numCols ; ++ col ) { product [ row ][ col ] = row * col ; } } // Print the table for ( int row { 1 }; row < numRows ; ++ row ) { for ( int col { 1 }; col < numCols ; ++ col ) { std :: cout << product [ row ][ col ] << '\\t' ; } std :: cout << '\\n' ; } return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u8ba1\u7b97\u5e76\u6253\u53701\u52309(\u5305\u62ec9)\u4e4b\u95f4\u6240\u6709\u503c\u7684\u4e58\u6cd5\u8868\u3002\u6ce8\u610f\uff0c\u5728\u6253\u5370\u8868\u65f6\uff0cfor\u5faa\u73af\u4ece1\u5f00\u59cb\uff0c\u800c\u4e0d\u662f\u4ece0\u5f00\u59cb\u3002\u8f93\u51fa\u5982\u4e0b: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 48 54 7 14 21 28 35 42 49 56 63 8 16 24 32 40 48 56 64 72 9 18 27 36 45 54 63 72 81 \u4e8c\u7ef4\u6570\u7ec4\u901a\u5e38\u7528\u4e8e\u57fa\u4e8e\u8d34\u56fe\u7684\u6e38\u620f\u4e2d\uff0c\u5176\u4e2d\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u4ee3\u8868\u4e00\u4e2a\u8d34\u56fe\u3002\u5b83\u4eec\u4e5f\u88ab\u7528\u4e8e3d\u8ba1\u7b97\u673a\u56fe\u5f62(\u4f5c\u4e3a\u77e9\u9635)\uff0c\u4ee5\u65cb\u8f6c\u3001\u7f29\u653e\u548c\u8868\u793a\u5f62\u72b6\u3002","title":"\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u7684\u4f8b\u5b50"},{"location":"11-6-C-style-strings/","tags":["c-style","string"],"text":"Key Takeaway C\u8bed\u8a00\u5b57\u7b26\u4e32\u5c31\u662f\u4e00\u4e2a\u5b57\u7b26\u6570\u7ec4\uff0c\u6700\u540e\u63a5\u7740\u4e00\u4e2a\u7a7a\u5b57\u7b26\u3002 \u6253\u5370\u5b57\u7b26\u4e32\u65f6\uff0c\u9047\u5230\u7a7a\u5b57\u7b26\u4f1a\u505c\u6b62\uff0c\u5982\u679c\u7a7a\u5b57\u7b26\u88ab\u8986\u76d6\uff0c\u6253\u5370\u51fd\u6570\u5c31\u4f1a\u4e00\u76f4\u6253\u5370\u76f4\u5230\u9047\u5230\u4e0b\u4e00\u4e2a0 \u8c03\u7528 std::cin.getline(name, std::size(name)); \u53ef\u4ee5\u8bfb\u53d6\u6700\u591a 254 \u4e2a\u5b57\u7b26\u5230 name \uff08\u4e3a\u7a7a\u5b57\u7b26\u4fdd\u7559\u4e00\u4e2a\u7a7a\u95f4\uff01\uff09\u3002\u8d85\u51fa\u7684\u5b57\u7b26\u4f1a\u88ab\u5ffd\u7565\u6389\u3002\u901a\u8fc7\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u907f\u514d\u6570\u7ec4\u6ea2\u51fa\uff01 \u64cd\u4f5cC\u8bed\u8a00\u5b57\u7b26\u4e32\u7684\u5e38\u7528\u51fd\u6570\uff1a strcpy() -- \u5c06\u5b57\u7b26\u4e32\u62f7\u8d1d\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32 strncpy() -- \u5c06\u5b57\u7b26\u4e32\u62f7\u8d1d\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32(\u6307\u5b9abuffer\u5927\u5c0f)\uff0c\u4f46\u4e0d\u80fd\u786e\u4fddnull\u7ed3\u5c3e strcpy_s() \uff0c\u5b83\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u53c2\u6570\u6765\u5b9a\u4e49\u76ee\u6807\u7684\u5927\u5c0f\uff08C++11\uff09 strcat() -- \u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u62fc\u63a5\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u540e\u9762\uff08\u5371\u9669\u64cd\u4f5c\uff09 strncat() -- \u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u62fc\u63a5\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u540e\u9762 (\u4f1a\u68c0\u67e5buffer\u957f\u5ea6) strcmp() -- \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\uff08\u76f8\u7b49\u8fd4\u56de 0\uff09 strncmp() -- \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\uff08\u76f8\u7b49\u8fd4\u56de 0\uff09 \u4e0d\u8981\u4f7f\u7528 C \u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32 \u5728 4.17 - std::string \u7b80\u4ecb \u4e2d\uff0c\u6211\u4eec\u5c06\u5b57\u7b26\u5b9a\u4e49\u4e3a\u4e00\u7cfb\u5217\u5b57\u7b26\u7684\u96c6\u5408\uff0c\u4f8b\u5982 \u201cHello, world!\u201d\u3002\u5b57\u7b26\u4e32c++\u4e2d\u5904\u7406\u6587\u672c\u7684\u4e3b\u8981\u65b9\u5f0f\uff0c\u800c std::string \u53ef\u4ee5\u4f7fC++\u4e2d\u5904\u7406\u5b57\u7b26\u4e32\u53d8\u5f97\u7b80\u5355\u3002 \u73b0\u4ee3C++\u652f\u6301\u4e24\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u5b57\u7b26\u4e32\uff1a std::string (\u4f5c\u4e3a\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206)\u548cC\u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32(\u4eceC\u8bed\u8a00\u7ee7\u627f\u800c\u6765)\u3002\u4e8b\u5b9e\u8bc1\u660e\uff0c std::string \u662f\u4f7f\u7528c\u98ce\u683c\u5b57\u7b26\u4e32\u5b9e\u73b0\u7684\u3002\u5728\u8fd9\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u4e86\u89e3C\u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002 C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 \u5c31\u662f\u4e00\u4e2a\u4ee5\u7a7a\u5b57\u7b26( \\0 )\u7ed3\u5c3e\u7684\u5b57\u7b26\u6570\u7ec4\u3002 \u7a7a\u5b57\u7b26 \u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u5b57\u7b26( '\\0' \uff0c ascii \u7801\u4e3a 0) \uff0c\u7528\u4e8e\u8868\u793a\u5b57\u7b26\u7684\u7ed3\u5c3e\u3002\u6240\u4ee5C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u4e5f\u88ab\u79f0\u4e3a\u7a7a\u5b57\u7b26\u7ed3\u5c3e\u7684\u5b57\u7b26\u4e32\u3002 \u5b9a\u4e49C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u58f0\u660e\u4e00\u4e2a\u5b57\u7b26\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\u3002 1 char myString []{ \"string\" }; \u5c3d\u7ba1 \u201cstring\u201d \u53ea\u67096\u4e2a\u5b57\u6bcd\uff0cC++\u4f1a\u81ea\u52a8\u5728\u5176\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u7a7a\u5b57\u7b26\u8868\u660e\u7ed3\u5c3e\uff08\u4e0d\u9700\u8981\u7a0b\u5e8f\u5458\u81ea\u5df1\u64cd\u4f5c\uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c myString \u6570\u7ec4\u7684\u957f\u5ea6\u5b9e\u9645\u4e0a\u4e3a 7\uff01 \u6211\u4eec\u53ef\u4ee5\u5728\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e2d\u770b\u5230\u8fd9\u4e00\u70b9\u7684\u8bc1\u636e\uff0c\u5b83\u6253\u5370\u51fa\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u4ee5\u53ca\u6240\u6709\u5b57\u7b26\u7684ASCII\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include // for std::size int main () { char myString []{ \"string\" }; const int length { static_cast < int > ( std :: size ( myString )) }; // const int length{ sizeof(myString) / sizeof(myString[0]) }; // use instead if not C++17 capable std :: cout << myString << \" has \" << length << \" characters. \\n \" ; for ( int index { 0 }; index < length ; ++ index ) std :: cout << static_cast < int > ( myString [ index ]) << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 string has 7 characters. 115 116 114 105 110 103 0 0 \u662f\u6dfb\u52a0\u5230\u5b57\u7b26\u4e32\u672b\u5c3e\u7684\u7a7a\u7ed3\u675f\u7b26\u7684ASCII\u7801\u3002 \u5f53\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u58f0\u660e\u5b57\u7b26\u4e32\u65f6\uff0c\u6700\u597d\u4f7f\u7528 [] \uff0c\u5e76\u8ba9\u7f16\u8bd1\u5668\u8ba1\u7b97\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u8fd9\u6837\uff0c\u5982\u679c\u4ee5\u540e\u9700\u8981\u4fee\u6539\u5b57\u7b26\u4e32\uff0c\u5c31\u4e0d\u5fc5\u624b\u52a8\u8c03\u6574\u6570\u7ec4\u7684\u957f\u5ea6\u3002 \u9700\u8981\u6ce8\u610f\u7684\u4e00\u70b9\u662f\uff0cC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u9075\u5faa\u4e0e\u6570\u7ec4\u76f8\u540c\u7684\u89c4\u5219\u3002\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u5728\u521b\u5efa\u65f6\u521d\u59cb\u5316\u5b57\u7b26\u4e32\uff0c\u4f46\u4f60\u4e0d\u80fd\u5728\u4e4b\u540e\u4f7f\u7528\u8d4b\u503c\u64cd\u4f5c\u7b26\u7ed9\u5b83\u8d4b\u503c! 1 2 char myString []{ \"string\" }; // ok myString = \"rope\" ; // not ok! \u56e0\u4e3a C \u98ce\u683c\u5b57\u7b26\u4e32\u662f\u6570\u7ec4\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0b\u6807\u8fd0\u7b97\u7b26 [] \u6765\u6539\u53d8\u5b57\u7b26\u4e32\u4e2d\u7684\u5143\u7d20\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { char myString []{ \"string\" }; myString [ 1 ] = 'p' ; std :: cout << myString << '\\n' ; return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 spring \u5728\u4f7f\u7528 C \u98ce\u683c\u5b57\u7b26\u4e32\u65f6\uff0c std::cout \u4f1a\u4f9d\u6b21\u6253\u5370\u7a7a\u5b57\u7b26\u3002\u5982\u679c\u4f60\u4e0d\u5c0f\u5fc3\u8986\u76d6\u4e86\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u5b57\u7b26\uff08\u4f8b\u5982\uff1a\u7ed9 myString[6] \u8d4b\u503c\uff09\uff0c\u90a3\u4e48\u4f60\u5f97\u5230\u7684\u6253\u5370\u7ed3\u679c\u4e0d\u4ec5\u4ec5\u662f\u5b8c\u6574\u7684\u5b57\u7b26\u4e32\uff0c std::cout \u8fd8\u4f1a\u7ee7\u7eed\u6253\u5370\u76f8\u90bb\u5185\u5b58\u4e2d\u7684\u5185\u5bb9\uff0c\u76f4\u5230\u9047\u5230 0\u3002 \u6ce8\u610f\uff0c\u5b57\u7b26\u6570\u7ec4\u7684\u5927\u5c0f\u6bd4\u5b57\u7b26\u4e32\u5927\u662f\u53ef\u4ee5\u7684\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char name [ 20 ]{ \"Alex\" }; // only use 5 characters (4 letters + null terminator) std :: cout << \"My name is: \" << name << '\\n' ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u201cAlex\u201d \u4f1a\u88ab\u6253\u5370\u51fa\u6765\uff0c\u7136\u540e std::cout \u4f1a\u5728\u9047\u5230\u7a7a\u5b57\u7b26\u65f6\u505c\u6b62\u6253\u5370\u3002\u6570\u7ec4\u4e2d\u540e\u7eed\u5185\u5bb9\u4f1a\u88ab\u5ffd\u7565\u3002 C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u548c std::cin \u5728\u5f88\u591a\u60c5\u51b5\u4e0b\u6211\u4eec\u90fd\u4e0d\u77e5\u9053\u5b57\u7b26\u4e32\u4f1a\u6709\u591a\u957f\u3002\u4f8b\u5982\uff0c\u8003\u8651\u8fd9\u6837\u4e00\u4e2a\u95ee\u9898\uff1a\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u6211\u4eec\u9700\u8981\u8981\u6c42\u7528\u6237\u8f93\u5165\u4ed6\u4eec\u7684\u540d\u5b57\u3002\u4ed6\u4eec\u7684\u540d\u5b57\u6709\u591a\u957f\uff1f\u5728\u7528\u6237\u8f93\u5165\u88ab\u8bfb\u53d6\u4e4b\u524d\uff0c\u6211\u4eec\u90fd\u4e0d\u77e5\u9053\uff01 \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u6bd4\u5b9e\u9645\u9700\u8981\u5927\u7684\u591a\u7684\u6570\u7ec4\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { char name [ 255 ] {}; // declare array large enough to hold 254 characters + null terminator std :: cout << \"Enter your name: \" ; std :: cin >> name ; std :: cout << \"You entered: \" << name << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4e3aname\u5206\u914d\u4e86\u4e00\u4e2a255\u4e2a\u5b57\u7b26\u7684\u6570\u7ec4\u5e76\u5047\u8bbe\u7528\u6237\u4e0d\u4f1a\u8f93\u5165\u8fd9\u4e48\u591a\u5b57\u7b26\u3002\u5c3d\u7ba1\u8fd9\u5728C/C++\u7f16\u7a0b\u4e2d\u5f88\u5e38\u89c1\uff0c\u4f46\u8fd9\u662f\u4e00\u79cd\u7cdf\u7cd5\u7684\u7f16\u7a0b\u5b9e\u8df5\uff0c\u56e0\u4e3a\u5e76\u6ca1\u6709\u529e\u6cd5\u963b\u6b62\u7528\u6237\u8f93\u5165\u8d85\u8fc7254\u4e2a\u5b57\u7b26(\u4e0d\u7ba1\u662f\u65e0\u610f\u7684\uff0c\u8fd8\u662f\u6076\u610f\u7684)\u3002 \u4f7f\u7528 std::cin \u8bfb\u53d6C\u98ce\u683c\u5b57\u7b26\u4e32\u7684\u63a8\u8350\u65b9\u6cd5\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include #include // for std::size int main () { char name [ 255 ] {}; // declare array large enough to hold 254 characters + null terminator std :: cout << \"Enter your name: \" ; std :: cin . getline ( name , std :: size ( name )); std :: cout << \"You entered: \" << name << '\\n' ; return 0 ; } \u8c03\u7528 std::cin.getline(name, std::size(name)); \u53ef\u4ee5\u8bfb\u53d6\u6700\u591a 254 \u4e2a\u5b57\u7b26\u5230 name \uff08\u4e3a\u7a7a\u5b57\u7b26\u4fdd\u7559\u4e00\u4e2a\u7a7a\u95f4\uff01\uff09\u3002\u8d85\u51fa\u7684\u5b57\u7b26\u4f1a\u88ab\u5ffd\u7565\u6389\u3002\u901a\u8fc7\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u907f\u514d\u6570\u7ec4\u6ea2\u51fa\uff01 \u64cd\u4f5c C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 C++\u63d0\u4f9b\u4e86\u8bb8\u591a\u51fd\u6570\u6765\u64cd\u4f5cC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u5b83\u4eec\u662f \u5934\u6587\u4ef6\u7684\u4e00\u90e8\u5206\u3002\u5e38\u7528\u7684\u51fd\u6570\u5217\u4e3e\u5982\u4e0b\uff1a strcpy() \u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u66f4\u5e38\u89c1\u7684\u662f\uff0c\u5b83\u88ab\u7528\u6765\u7ed9\u4e00\u4e2a\u5b57\u7b26\u4e32\u8d4b\u503c: 1 2 3 4 5 6 7 8 9 10 11 12 #include #include int main () { char source []{ \"Copy this!\" }; char dest [ 50 ]; std :: strcpy ( dest , source ); std :: cout << dest << '\\n' ; // prints \"Copy this!\" return 0 ; } \u4e0d\u8fc7\uff0c\u5982\u679c\u4e0d\u5c0f\u5fc3\u7684\u8bdd\uff0c strcpy() \u5f88\u5bb9\u6613\u5bfc\u81f4\u6570\u7ec4\u7684\u6ea2\u51fa\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u76ee\u6807\u5730\u5740\u7684\u5927\u5c0f\u4e0d\u8db3\u4ee5\u5bb9\u7eb3\u5168\u90e8\u5b57\u7b26\u4e32\uff0c\u8fd9\u5c31\u4f1a\u5bfc\u81f4\u6570\u7ec4\u6ea2\u51fa\u3002 1 2 3 4 5 6 7 8 9 10 11 12 #include #include int main () { char source []{ \"Copy this!\" }; char dest [ 5 ]; // note that the length of dest is only 5 chars! std :: strcpy ( dest , source ); // overflow! std :: cout << dest << '\\n' ; return 0 ; } \u8bb8\u591a\u7a0b\u5e8f\u5458\u5efa\u8bae\u4f7f\u7528 strncpy() \uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u6307\u5b9a\u7f13\u51b2\u533a\u7684\u5927\u5c0f\uff0c\u5e76\u786e\u4fdd\u4e0d\u4f1a\u53d1\u751f\u6ea2\u51fa\u3002\u4e0d\u5e78\u7684\u662f\uff0c' strncpy() \u4e0d\u80fd\u786e\u4fdd\u5b57\u7b26\u4e32\u4ee5\u7a7a\u5b57\u7b26\u7ed3\u5c3e\uff0c\u8fd9\u4ecd\u7136\u4e3a\u6570\u7ec4\u6ea2\u51fa\u7559\u4e0b\u4e86\u9690\u60a3\u3002 \u5728C++ 11\u4e2d\uff0c\u9996\u9009 strcpy_s() \uff0c\u5b83\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u53c2\u6570\u6765\u5b9a\u4e49\u76ee\u6807\u7684\u5927\u5c0f\u3002\u7136\u800c\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u7684\u7f16\u8bd1\u5668\u90fd\u652f\u6301\u8fd9\u4e2a\u51fd\u6570\uff0c\u8981\u4f7f\u7528\u5b83\uff0c\u4f60\u5fc5\u987b\u5b9a\u4e49 STDC_WANT_LIB_EXT1 \u5e76\u5c06\u5176\u5b9a\u4e49\u4e3a 1 \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #define __STDC_WANT_LIB_EXT1__ 1 #include // for strcpy_s #include int main () { char source []{ \"Copy this!\" }; char dest [ 5 ]; // note that the length of dest is only 5 chars! strcpy_s ( dest , 5 , source ); // A runtime error will occur in debug mode std :: cout << dest << '\\n' ; return 0 ; } \u56e0\u4e3a\u5e76\u4e0d\u662f\u6240\u6709\u7684\u7f16\u8bd1\u5668\u90fd\u652f\u6301 strcpy_s() \uff0c\u6240\u4ee5 strlcpy() \u662f\u4e00\u4e2a\u5f88\u53d7\u6b22\u8fce\u7684\u9009\u62e9\u2014\u2014\u5c3d\u7ba1\u5b83\u4e0d\u662f\u6807\u51c6\u7684\uff0c\u56e0\u6b64\u5f88\u591a\u7f16\u8bd1\u5668\u90fd\u4e0d\u5305\u542b\u5b83\u3002\u5b83\u4e5f\u6709\u81ea\u5df1\u7684\u4e00\u7cfb\u5217\u95ee\u9898\u3002\u7b80\u800c\u8a00\u4e4b\uff0c\u5982\u679c\u60a8\u9700\u8981\u590d\u5236C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u8fd9\u91cc\u6ca1\u6709\u666e\u904d\u63a8\u8350\u7684\u89e3\u51b3\u65b9\u6848\u3002 \u53e6\u4e00\u4e2a\u6709\u7528\u7684\u51fd\u6570\u662f strlen() \u51fd\u6570\uff0c\u5b83\u8fd4\u56deC\u98ce\u683c\u5b57\u7b26\u4e32\u7684\u957f\u5ea6(\u6ca1\u6709\u7a7a\u5b57\u7b26)\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include #include // for std::size int main () { char name [ 20 ]{ \"Alex\" }; // only use 5 characters (4 letters + null terminator) std :: cout << \"My name is: \" << name << '\\n' ; std :: cout << name << \" has \" << std :: strlen ( name ) << \" letters. \\n \" ; std :: cout << name << \" has \" << std :: size ( name ) << \" characters in the array. \\n \" ; // use sizeof(name) / sizeof(name[0]) if not C++17 capable return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 My name is: Alex Alex has 4 letters. Alex has 20 characters in the array. \u6ce8\u610f\u8fa8\u6790 strlen() \u548c std::size() \u7684\u533a\u522b\u3002 strlen() \u4f1a\u8fd4\u56de\u7a7a\u5b57\u7b26\u524d\u6240\u6709\u5b57\u7b26\u7684\u4e2a\u6570\uff0c\u800c std::size (\u6216\u8005 sizeof() \u6280\u5de7)\u5219\u4f1a\u8fd4\u56de\u6574\u4e2a\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u4e0d\u7ba1\u6570\u7ec4\u5185\u90e8\u5b58\u653e\u4e86\u4ec0\u4e48\u3002 \u5176\u4ed6\u51fd\u6570\uff1a strcat() -- \u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u62fc\u63a5\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u540e\u9762\uff08\u5371\u9669\u64cd\u4f5c\uff09 strncat() -- \u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u62fc\u63a5\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u540e\u9762 (\u4f1a\u68c0\u67e5buffer\u957f\u5ea6) strcmp() -- \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\uff08\u76f8\u7b49\u8fd4\u56de 0\uff09 strncmp() -- \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\uff08\u76f8\u7b49\u8fd4\u56de 0\uff09 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u672c\u8bfe\u4e2d\u7684\u4e00\u4e9b\u6982\u5ff5\u7f16\u5199\u7684\u793a\u4f8b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include #include // for std::size int main () { // Ask the user to enter a string char buffer [ 255 ] {}; std :: cout << \"Enter a string: \" ; std :: cin . getline ( buffer , std :: size ( buffer )); int spacesFound { 0 }; int bufferLength { static_cast < int > ( std :: strlen ( buffer )) }; // Loop through all of the characters the user entered for ( int index { 0 }; index < bufferLength ; ++ index ) { // If the current character is a space, count it if ( buffer [ index ] == ' ' ) ++ spacesFound ; } std :: cout << \"You typed \" << spacesFound << \" spaces! \\n \" ; return 0 ; } \u6ce8\u610f\u6211\u4eec\u5728\u5faa\u73af\u5916\u4f7f\u7528 strlen(buffer) \uff0c\u8fd9\u6837\u5b83\u4e4b\u540e\u8ba1\u7b97\u4e00\u6b21\uff0c\u800c\u4e0d\u662f\u6bcf\u6b21\u5faa\u73af\u90fd\u53bb\u8ba1\u7b97\u3002 \u4e0d\u8981\u4f7f\u7528 C \u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32 \u4e86\u89e3C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u662f\u5f88\u91cd\u8981\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u5728\u5f88\u591a\u4ee3\u7801\u4e2d\u90fd\u6709\u4f7f\u7528\u3002\u7136\u800c\uff0c\u65e2\u7136\u5df2\u7ecf\u89e3\u91ca\u4e86\u5b83\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u5efa\u8bae\u4f60\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u5b83\u4eec\uff01\u9664\u975e\u6709\u7279\u5b9a\u7684\u3001\u4ee4\u4eba\u4fe1\u670d\u7684\u7406\u7531\u4f7f\u7528C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u5426\u5219\u8bf7\u4f7f\u7528std::string(\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d)\u7c7b\u578b\u7684\u5b57\u7b26\u4e32\uff0c\u5b83\u66f4\u7b80\u5355\u3001\u66f4\u5b89\u5168\u4e5f\u66f4\u7075\u6d3b\u3002\u5728\u6781\u5c11\u6570\u60c5\u51b5\u4e0b\uff0c\u4f60\u786e\u5b9e\u9700\u8981\u4f7f\u7528\u56fa\u5b9a\u7684\u7f13\u51b2\u533a\u5927\u5c0f\u548cC\u98ce\u683c\u7684\u5b57\u7b26\u4e32(\u4f8b\u5982\u5185\u5b58\u6709\u9650\u7684\u8bbe\u5907)\uff0c\u6211\u4eec\u5efa\u8bae\u4f7f\u7528\u4e00\u4e2a\u6d4b\u8bd5\u826f\u597d\u7684\u7b2c\u4e09\u65b9\u5b57\u7b26\u4e32\u5e93\uff0c\u6216 std::string_view \uff0c\u8fd9\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u8ba8\u8bba\u3002 \u6cd5\u5219 \u4f7f\u7528 std::string \u6216 std::string_view \u6765\u4ee3\u66ff C \u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002","title":"11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32"},{"location":"11-6-C-style-strings/#c","text":"C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 \u5c31\u662f\u4e00\u4e2a\u4ee5\u7a7a\u5b57\u7b26( \\0 )\u7ed3\u5c3e\u7684\u5b57\u7b26\u6570\u7ec4\u3002 \u7a7a\u5b57\u7b26 \u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u5b57\u7b26( '\\0' \uff0c ascii \u7801\u4e3a 0) \uff0c\u7528\u4e8e\u8868\u793a\u5b57\u7b26\u7684\u7ed3\u5c3e\u3002\u6240\u4ee5C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u4e5f\u88ab\u79f0\u4e3a\u7a7a\u5b57\u7b26\u7ed3\u5c3e\u7684\u5b57\u7b26\u4e32\u3002 \u5b9a\u4e49C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u58f0\u660e\u4e00\u4e2a\u5b57\u7b26\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\u3002 1 char myString []{ \"string\" }; \u5c3d\u7ba1 \u201cstring\u201d \u53ea\u67096\u4e2a\u5b57\u6bcd\uff0cC++\u4f1a\u81ea\u52a8\u5728\u5176\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u7a7a\u5b57\u7b26\u8868\u660e\u7ed3\u5c3e\uff08\u4e0d\u9700\u8981\u7a0b\u5e8f\u5458\u81ea\u5df1\u64cd\u4f5c\uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c myString \u6570\u7ec4\u7684\u957f\u5ea6\u5b9e\u9645\u4e0a\u4e3a 7\uff01 \u6211\u4eec\u53ef\u4ee5\u5728\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e2d\u770b\u5230\u8fd9\u4e00\u70b9\u7684\u8bc1\u636e\uff0c\u5b83\u6253\u5370\u51fa\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u4ee5\u53ca\u6240\u6709\u5b57\u7b26\u7684ASCII\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include // for std::size int main () { char myString []{ \"string\" }; const int length { static_cast < int > ( std :: size ( myString )) }; // const int length{ sizeof(myString) / sizeof(myString[0]) }; // use instead if not C++17 capable std :: cout << myString << \" has \" << length << \" characters. \\n \" ; for ( int index { 0 }; index < length ; ++ index ) std :: cout << static_cast < int > ( myString [ index ]) << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 string has 7 characters. 115 116 114 105 110 103 0 0 \u662f\u6dfb\u52a0\u5230\u5b57\u7b26\u4e32\u672b\u5c3e\u7684\u7a7a\u7ed3\u675f\u7b26\u7684ASCII\u7801\u3002 \u5f53\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u58f0\u660e\u5b57\u7b26\u4e32\u65f6\uff0c\u6700\u597d\u4f7f\u7528 [] \uff0c\u5e76\u8ba9\u7f16\u8bd1\u5668\u8ba1\u7b97\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u8fd9\u6837\uff0c\u5982\u679c\u4ee5\u540e\u9700\u8981\u4fee\u6539\u5b57\u7b26\u4e32\uff0c\u5c31\u4e0d\u5fc5\u624b\u52a8\u8c03\u6574\u6570\u7ec4\u7684\u957f\u5ea6\u3002 \u9700\u8981\u6ce8\u610f\u7684\u4e00\u70b9\u662f\uff0cC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u9075\u5faa\u4e0e\u6570\u7ec4\u76f8\u540c\u7684\u89c4\u5219\u3002\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u5728\u521b\u5efa\u65f6\u521d\u59cb\u5316\u5b57\u7b26\u4e32\uff0c\u4f46\u4f60\u4e0d\u80fd\u5728\u4e4b\u540e\u4f7f\u7528\u8d4b\u503c\u64cd\u4f5c\u7b26\u7ed9\u5b83\u8d4b\u503c! 1 2 char myString []{ \"string\" }; // ok myString = \"rope\" ; // not ok! \u56e0\u4e3a C \u98ce\u683c\u5b57\u7b26\u4e32\u662f\u6570\u7ec4\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0b\u6807\u8fd0\u7b97\u7b26 [] \u6765\u6539\u53d8\u5b57\u7b26\u4e32\u4e2d\u7684\u5143\u7d20\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { char myString []{ \"string\" }; myString [ 1 ] = 'p' ; std :: cout << myString << '\\n' ; return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 spring \u5728\u4f7f\u7528 C \u98ce\u683c\u5b57\u7b26\u4e32\u65f6\uff0c std::cout \u4f1a\u4f9d\u6b21\u6253\u5370\u7a7a\u5b57\u7b26\u3002\u5982\u679c\u4f60\u4e0d\u5c0f\u5fc3\u8986\u76d6\u4e86\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u5b57\u7b26\uff08\u4f8b\u5982\uff1a\u7ed9 myString[6] \u8d4b\u503c\uff09\uff0c\u90a3\u4e48\u4f60\u5f97\u5230\u7684\u6253\u5370\u7ed3\u679c\u4e0d\u4ec5\u4ec5\u662f\u5b8c\u6574\u7684\u5b57\u7b26\u4e32\uff0c std::cout \u8fd8\u4f1a\u7ee7\u7eed\u6253\u5370\u76f8\u90bb\u5185\u5b58\u4e2d\u7684\u5185\u5bb9\uff0c\u76f4\u5230\u9047\u5230 0\u3002 \u6ce8\u610f\uff0c\u5b57\u7b26\u6570\u7ec4\u7684\u5927\u5c0f\u6bd4\u5b57\u7b26\u4e32\u5927\u662f\u53ef\u4ee5\u7684\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char name [ 20 ]{ \"Alex\" }; // only use 5 characters (4 letters + null terminator) std :: cout << \"My name is: \" << name << '\\n' ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u201cAlex\u201d \u4f1a\u88ab\u6253\u5370\u51fa\u6765\uff0c\u7136\u540e std::cout \u4f1a\u5728\u9047\u5230\u7a7a\u5b57\u7b26\u65f6\u505c\u6b62\u6253\u5370\u3002\u6570\u7ec4\u4e2d\u540e\u7eed\u5185\u5bb9\u4f1a\u88ab\u5ffd\u7565\u3002","title":"C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32"},{"location":"11-6-C-style-strings/#c-stdcin","text":"\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\u6211\u4eec\u90fd\u4e0d\u77e5\u9053\u5b57\u7b26\u4e32\u4f1a\u6709\u591a\u957f\u3002\u4f8b\u5982\uff0c\u8003\u8651\u8fd9\u6837\u4e00\u4e2a\u95ee\u9898\uff1a\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u6211\u4eec\u9700\u8981\u8981\u6c42\u7528\u6237\u8f93\u5165\u4ed6\u4eec\u7684\u540d\u5b57\u3002\u4ed6\u4eec\u7684\u540d\u5b57\u6709\u591a\u957f\uff1f\u5728\u7528\u6237\u8f93\u5165\u88ab\u8bfb\u53d6\u4e4b\u524d\uff0c\u6211\u4eec\u90fd\u4e0d\u77e5\u9053\uff01 \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u6bd4\u5b9e\u9645\u9700\u8981\u5927\u7684\u591a\u7684\u6570\u7ec4\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { char name [ 255 ] {}; // declare array large enough to hold 254 characters + null terminator std :: cout << \"Enter your name: \" ; std :: cin >> name ; std :: cout << \"You entered: \" << name << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4e3aname\u5206\u914d\u4e86\u4e00\u4e2a255\u4e2a\u5b57\u7b26\u7684\u6570\u7ec4\u5e76\u5047\u8bbe\u7528\u6237\u4e0d\u4f1a\u8f93\u5165\u8fd9\u4e48\u591a\u5b57\u7b26\u3002\u5c3d\u7ba1\u8fd9\u5728C/C++\u7f16\u7a0b\u4e2d\u5f88\u5e38\u89c1\uff0c\u4f46\u8fd9\u662f\u4e00\u79cd\u7cdf\u7cd5\u7684\u7f16\u7a0b\u5b9e\u8df5\uff0c\u56e0\u4e3a\u5e76\u6ca1\u6709\u529e\u6cd5\u963b\u6b62\u7528\u6237\u8f93\u5165\u8d85\u8fc7254\u4e2a\u5b57\u7b26(\u4e0d\u7ba1\u662f\u65e0\u610f\u7684\uff0c\u8fd8\u662f\u6076\u610f\u7684)\u3002 \u4f7f\u7528 std::cin \u8bfb\u53d6C\u98ce\u683c\u5b57\u7b26\u4e32\u7684\u63a8\u8350\u65b9\u6cd5\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include #include // for std::size int main () { char name [ 255 ] {}; // declare array large enough to hold 254 characters + null terminator std :: cout << \"Enter your name: \" ; std :: cin . getline ( name , std :: size ( name )); std :: cout << \"You entered: \" << name << '\\n' ; return 0 ; } \u8c03\u7528 std::cin.getline(name, std::size(name)); \u53ef\u4ee5\u8bfb\u53d6\u6700\u591a 254 \u4e2a\u5b57\u7b26\u5230 name \uff08\u4e3a\u7a7a\u5b57\u7b26\u4fdd\u7559\u4e00\u4e2a\u7a7a\u95f4\uff01\uff09\u3002\u8d85\u51fa\u7684\u5b57\u7b26\u4f1a\u88ab\u5ffd\u7565\u6389\u3002\u901a\u8fc7\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u907f\u514d\u6570\u7ec4\u6ea2\u51fa\uff01","title":"C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u548c std::cin"},{"location":"11-6-C-style-strings/#c_1","text":"C++\u63d0\u4f9b\u4e86\u8bb8\u591a\u51fd\u6570\u6765\u64cd\u4f5cC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u5b83\u4eec\u662f \u5934\u6587\u4ef6\u7684\u4e00\u90e8\u5206\u3002\u5e38\u7528\u7684\u51fd\u6570\u5217\u4e3e\u5982\u4e0b\uff1a strcpy() \u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u66f4\u5e38\u89c1\u7684\u662f\uff0c\u5b83\u88ab\u7528\u6765\u7ed9\u4e00\u4e2a\u5b57\u7b26\u4e32\u8d4b\u503c: 1 2 3 4 5 6 7 8 9 10 11 12 #include #include int main () { char source []{ \"Copy this!\" }; char dest [ 50 ]; std :: strcpy ( dest , source ); std :: cout << dest << '\\n' ; // prints \"Copy this!\" return 0 ; } \u4e0d\u8fc7\uff0c\u5982\u679c\u4e0d\u5c0f\u5fc3\u7684\u8bdd\uff0c strcpy() \u5f88\u5bb9\u6613\u5bfc\u81f4\u6570\u7ec4\u7684\u6ea2\u51fa\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u76ee\u6807\u5730\u5740\u7684\u5927\u5c0f\u4e0d\u8db3\u4ee5\u5bb9\u7eb3\u5168\u90e8\u5b57\u7b26\u4e32\uff0c\u8fd9\u5c31\u4f1a\u5bfc\u81f4\u6570\u7ec4\u6ea2\u51fa\u3002 1 2 3 4 5 6 7 8 9 10 11 12 #include #include int main () { char source []{ \"Copy this!\" }; char dest [ 5 ]; // note that the length of dest is only 5 chars! std :: strcpy ( dest , source ); // overflow! std :: cout << dest << '\\n' ; return 0 ; } \u8bb8\u591a\u7a0b\u5e8f\u5458\u5efa\u8bae\u4f7f\u7528 strncpy() \uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u6307\u5b9a\u7f13\u51b2\u533a\u7684\u5927\u5c0f\uff0c\u5e76\u786e\u4fdd\u4e0d\u4f1a\u53d1\u751f\u6ea2\u51fa\u3002\u4e0d\u5e78\u7684\u662f\uff0c' strncpy() \u4e0d\u80fd\u786e\u4fdd\u5b57\u7b26\u4e32\u4ee5\u7a7a\u5b57\u7b26\u7ed3\u5c3e\uff0c\u8fd9\u4ecd\u7136\u4e3a\u6570\u7ec4\u6ea2\u51fa\u7559\u4e0b\u4e86\u9690\u60a3\u3002 \u5728C++ 11\u4e2d\uff0c\u9996\u9009 strcpy_s() \uff0c\u5b83\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u53c2\u6570\u6765\u5b9a\u4e49\u76ee\u6807\u7684\u5927\u5c0f\u3002\u7136\u800c\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u7684\u7f16\u8bd1\u5668\u90fd\u652f\u6301\u8fd9\u4e2a\u51fd\u6570\uff0c\u8981\u4f7f\u7528\u5b83\uff0c\u4f60\u5fc5\u987b\u5b9a\u4e49 STDC_WANT_LIB_EXT1 \u5e76\u5c06\u5176\u5b9a\u4e49\u4e3a 1 \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #define __STDC_WANT_LIB_EXT1__ 1 #include // for strcpy_s #include int main () { char source []{ \"Copy this!\" }; char dest [ 5 ]; // note that the length of dest is only 5 chars! strcpy_s ( dest , 5 , source ); // A runtime error will occur in debug mode std :: cout << dest << '\\n' ; return 0 ; } \u56e0\u4e3a\u5e76\u4e0d\u662f\u6240\u6709\u7684\u7f16\u8bd1\u5668\u90fd\u652f\u6301 strcpy_s() \uff0c\u6240\u4ee5 strlcpy() \u662f\u4e00\u4e2a\u5f88\u53d7\u6b22\u8fce\u7684\u9009\u62e9\u2014\u2014\u5c3d\u7ba1\u5b83\u4e0d\u662f\u6807\u51c6\u7684\uff0c\u56e0\u6b64\u5f88\u591a\u7f16\u8bd1\u5668\u90fd\u4e0d\u5305\u542b\u5b83\u3002\u5b83\u4e5f\u6709\u81ea\u5df1\u7684\u4e00\u7cfb\u5217\u95ee\u9898\u3002\u7b80\u800c\u8a00\u4e4b\uff0c\u5982\u679c\u60a8\u9700\u8981\u590d\u5236C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u8fd9\u91cc\u6ca1\u6709\u666e\u904d\u63a8\u8350\u7684\u89e3\u51b3\u65b9\u6848\u3002 \u53e6\u4e00\u4e2a\u6709\u7528\u7684\u51fd\u6570\u662f strlen() \u51fd\u6570\uff0c\u5b83\u8fd4\u56deC\u98ce\u683c\u5b57\u7b26\u4e32\u7684\u957f\u5ea6(\u6ca1\u6709\u7a7a\u5b57\u7b26)\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include #include // for std::size int main () { char name [ 20 ]{ \"Alex\" }; // only use 5 characters (4 letters + null terminator) std :: cout << \"My name is: \" << name << '\\n' ; std :: cout << name << \" has \" << std :: strlen ( name ) << \" letters. \\n \" ; std :: cout << name << \" has \" << std :: size ( name ) << \" characters in the array. \\n \" ; // use sizeof(name) / sizeof(name[0]) if not C++17 capable return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 My name is: Alex Alex has 4 letters. Alex has 20 characters in the array. \u6ce8\u610f\u8fa8\u6790 strlen() \u548c std::size() \u7684\u533a\u522b\u3002 strlen() \u4f1a\u8fd4\u56de\u7a7a\u5b57\u7b26\u524d\u6240\u6709\u5b57\u7b26\u7684\u4e2a\u6570\uff0c\u800c std::size (\u6216\u8005 sizeof() \u6280\u5de7)\u5219\u4f1a\u8fd4\u56de\u6574\u4e2a\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u4e0d\u7ba1\u6570\u7ec4\u5185\u90e8\u5b58\u653e\u4e86\u4ec0\u4e48\u3002 \u5176\u4ed6\u51fd\u6570\uff1a strcat() -- \u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u62fc\u63a5\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u540e\u9762\uff08\u5371\u9669\u64cd\u4f5c\uff09 strncat() -- \u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u62fc\u63a5\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u540e\u9762 (\u4f1a\u68c0\u67e5buffer\u957f\u5ea6) strcmp() -- \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\uff08\u76f8\u7b49\u8fd4\u56de 0\uff09 strncmp() -- \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\uff08\u76f8\u7b49\u8fd4\u56de 0\uff09 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u672c\u8bfe\u4e2d\u7684\u4e00\u4e9b\u6982\u5ff5\u7f16\u5199\u7684\u793a\u4f8b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include #include // for std::size int main () { // Ask the user to enter a string char buffer [ 255 ] {}; std :: cout << \"Enter a string: \" ; std :: cin . getline ( buffer , std :: size ( buffer )); int spacesFound { 0 }; int bufferLength { static_cast < int > ( std :: strlen ( buffer )) }; // Loop through all of the characters the user entered for ( int index { 0 }; index < bufferLength ; ++ index ) { // If the current character is a space, count it if ( buffer [ index ] == ' ' ) ++ spacesFound ; } std :: cout << \"You typed \" << spacesFound << \" spaces! \\n \" ; return 0 ; } \u6ce8\u610f\u6211\u4eec\u5728\u5faa\u73af\u5916\u4f7f\u7528 strlen(buffer) \uff0c\u8fd9\u6837\u5b83\u4e4b\u540e\u8ba1\u7b97\u4e00\u6b21\uff0c\u800c\u4e0d\u662f\u6bcf\u6b21\u5faa\u73af\u90fd\u53bb\u8ba1\u7b97\u3002","title":"\u64cd\u4f5c C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32"},{"location":"11-6-C-style-strings/#c_2","text":"\u4e86\u89e3C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u662f\u5f88\u91cd\u8981\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u5728\u5f88\u591a\u4ee3\u7801\u4e2d\u90fd\u6709\u4f7f\u7528\u3002\u7136\u800c\uff0c\u65e2\u7136\u5df2\u7ecf\u89e3\u91ca\u4e86\u5b83\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u5efa\u8bae\u4f60\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u5b83\u4eec\uff01\u9664\u975e\u6709\u7279\u5b9a\u7684\u3001\u4ee4\u4eba\u4fe1\u670d\u7684\u7406\u7531\u4f7f\u7528C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u5426\u5219\u8bf7\u4f7f\u7528std::string(\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d)\u7c7b\u578b\u7684\u5b57\u7b26\u4e32\uff0c\u5b83\u66f4\u7b80\u5355\u3001\u66f4\u5b89\u5168\u4e5f\u66f4\u7075\u6d3b\u3002\u5728\u6781\u5c11\u6570\u60c5\u51b5\u4e0b\uff0c\u4f60\u786e\u5b9e\u9700\u8981\u4f7f\u7528\u56fa\u5b9a\u7684\u7f13\u51b2\u533a\u5927\u5c0f\u548cC\u98ce\u683c\u7684\u5b57\u7b26\u4e32(\u4f8b\u5982\u5185\u5b58\u6709\u9650\u7684\u8bbe\u5907)\uff0c\u6211\u4eec\u5efa\u8bae\u4f7f\u7528\u4e00\u4e2a\u6d4b\u8bd5\u826f\u597d\u7684\u7b2c\u4e09\u65b9\u5b57\u7b26\u4e32\u5e93\uff0c\u6216 std::string_view \uff0c\u8fd9\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u8ba8\u8bba\u3002 \u6cd5\u5219 \u4f7f\u7528 std::string \u6216 std::string_view \u6765\u4ee3\u66ff C \u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002","title":"\u4e0d\u8981\u4f7f\u7528 C \u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32"},{"location":"11-7-std-string-view-part-2/","tags":["std::string_view","C++17"],"text":"Key Takeaway std:: string_view \u63d0\u4f9b\u4e86\u4e00\u4e2a\u89c2\u5bdf\u5b57\u7b26\u4e32\u7684\u89c6\u56fe\uff0c\u5b57\u7b26\u4e32\u672c\u8eab\u5b58\u653e\u5728\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d \u521b\u5efa std:: string_view \u65f6\u4e0d\u4f1a\u53d1\u751f\u590d\u5236\uff0c\u4f46\u662f\u4fee\u6539\u65f6\u4f1a\u5f71\u54cd\u5230\u6240\u6709\u7684\u5bf9\u8c61\u3002 remove_prefix \u548c remove_suffix \u5206\u522b\u7528\u4e8e\u4ece\u5b57\u7b26\u4e32\u89c6\u56fe\u7684\u5de6\u4fa7\u548c\u53f3\u4fa7\u5220\u9664\u5b57\u7b26\uff08\u4e0d\u4f1a\u5f71\u54cd\u5230\u539f\u5b57\u7b26\u4e32\uff09 \u8981\u5c06 std:: string_view \u8f6c\u6362\u4e3aC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u8f6c\u6362\u4e3a std:: string \u4f7f\u7528 str.c_str() \u83b7\u53d6C\u98ce\u683c\u5b57\u7b26\u4e32 \u5982\u679c\u6211\u4eec\u60f3\u5199\u4e00\u4e2a\u63a5\u53d7\u5b57\u7b26\u4e32\u5f62\u53c2\u7684\u51fd\u6570\uff0c\u5c06\u5f62\u53c2\u8bbe\u7f6e\u4e3a std:: string_view \u662f\u6700\u7075\u6d3b\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u9ad8\u6548\u5730\u914d\u5408C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u53c2\u6570(\u5305\u62ec\u5b57\u7b26\u4e32\u5b57\u9762\u91cf)\u3001 std:: string \u53c2\u6570(\u5c06\u9690\u5f0f\u8f6c\u6362\u4e3a' std:: string_view ')\u548c std:: string_view \u53c2\u6570\u6765\u5de5\u4f5c\uff1a \u4f18\u5148\u4f7f\u7528 std:: string_view ( \u6309\u503c\u4f20\u9012 )\u800c\u4e0d\u662f const std:: string& \uff0c\u9664\u975e\u4f60\u9700\u8981\u8c03\u7528\u5176\u4ed6\u8981\u6c42\u4f7f\u7528C\u98ce\u683c\u5b57\u7b26\u4e32\u6216 std::string \u7684\u51fd\u6570\u3002 \u786e\u4fdd std::string_view \u89c2\u5bdf\u7684\u5b57\u7b26\u4e32\u4e0d\u4f1a \u79bb\u5f00\u4f5c\u7528\u57df \uff0c\u4e5f\u4e0d\u4f1a\u88ab\u4fee\u6539\u3002 \u56e0\u4e3a std::string_view \u7684\u751f\u547d\u5468\u671f\u662f\u72ec\u7acb\u4e8e\u5b83\u201c\u89c2\u5bdf\u201d\u7684\u5b57\u7b26\u4e32\u7684\uff08\u4e5f\u5c31\u662f\u8bf4\u8be5\u5b57\u7b26\u4e32\u5bf9\u8c61\u53ef\u4ee5\u5728\u5148\u4e8e std::string_view \u88ab\u9500\u6bc1)\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u8bbf\u95ee std::string_view \u5c31\u4f1a\u9020\u6210 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u53ea\u6709\u5728 std::string_view \u6ca1\u6709\u88ab\u4fee\u6539\u7684\u60c5\u51b5\u4e0b\u4e14\u5b57\u7b26\u4e32\u4ee5\u7a7a\u7ed3\u675f\u7b26\u7ed3\u5c3e\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528 std::string_view::data() \u5982\u679c\u5b57\u7b26\u4e32\u6ca1\u6709\u4ee5\u7a7a\u7ed3\u675f\u7b26\u7ed3\u5c3e\uff0c\u5219 std::string_view::data() \u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 \u4f5c\u8005\u6ce8 \u672c\u8bfe\u7a0b\u7684\u90e8\u5206\u5185\u5bb9\u88ab\u79fb\u52a8\u5230\u4e86 4.18 - std:: string_view \u7b80\u4ecb \u3002\u56e0\u6b64\uff0c\u8fd9\u8282\u8bfe\u4e2d\u6709\u90e8\u5206\u5c1a\u672a\u6e05\u7406\u7684\u91cd\u590d\u90e8\u5206\u3002 \u5728\u4e0a\u4e00\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86 C \u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u4f46\u662f\u4f7f\u7528\u5b83\u4eec\u7684\u98ce\u9669\u5f88\u9ad8\u3002\u5c3d\u7ba1 C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u7684\u6027\u80fd\u5f88\u597d\uff0c\u4f46\u662f\u5b83\u4eec\u5e76\u4e0d\u6613\u7528\uff0c\u5b89\u5168\u6027\u4e5f\u4e0d\u5982 std:: string \u3002 \u4f46\u662f std:: string (\u5728 4.17 - std:: string \u7b80\u4ecb \u4e2d\u4ecb\u7ecd)\u4e5f\u6709\u5b83\u7684\u7f3a\u70b9\uff0c\u5c24\u5176\u662f\u5f53\u6211\u4eec\u9700\u8981\u5e38\u91cf\u5b57\u7b26\u4e32\u7684\u65f6\u5019\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include int main () { char text []{ \"hello\" }; std :: string str { text }; std :: string more { str }; std :: cout << text << ' ' << str << ' ' << more << '\\n' ; return 0 ; } \u548c\u671f\u671b\u7684\u4e00\u6837\uff0c\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 hello hello hello main \u51fd\u6570\u4f1a\u5c06\u5b57\u7b26\u4e32 \"hello\" \u590d\u5236 3 \u6b21\uff0c\u603b\u5171\u5f97\u5230 4 \u4e2a\u526f\u672c\u3002\u9996\u5148\u662f\u5b57\u7b26\u4e32\u5b57\u9762\u91cf \u201chello\u201d \uff0c\u5b83\u5728\u7f16\u8bd1\u65f6\u5df2\u77e5\uff0c\u5e76\u5b58\u50a8\u5728\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d\u3002\u5f53\u6211\u4eec\u521b\u5efa char[] \u65f6\uff0c\u4f1a\u521b\u5efa\u4e00\u4e2a\u526f\u672c\u3002\u63a5\u4e0b\u6765\u7684\u4e24\u4e2a std:: string \u5bf9\u8c61\u5206\u522b\u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u4e32\u526f\u672c\u3002\u56e0\u4e3a std:: string \u88ab\u8bbe\u8ba1\u4e3a\u53ef\u4fee\u6539\u7684\uff0c\u6bcf\u4e2a std:: string \u5fc5\u987b\u5305\u542b\u5b83\u81ea\u5df1\u7684\u5b57\u7b26\u4e32\u526f\u672c\uff0c\u8fd9\u6837\u7ed9\u5b9a\u7684 std:: string \u53ef\u4ee5\u88ab\u4fee\u6539\u800c\u4e0d\u5f71\u54cd\u4efb\u4f55\u5176\u4ed6\u7684 std:: string \u5bf9\u8c61\u3002 \u5373\u4f7f const std:: string \u4e5f\u5b58\u5728\u8fd9\u4e2a\u95ee\u9898\uff0c\u5373\u4f7f\u5b83\u4eec\u4e0d\u80fd\u4fee\u6539\u3002 std:: string_view \u60f3\u8c61\u8fd9\u6837\u4e00\u4e2a\u573a\u666f\uff0c\u4f60\u5750\u5728\u5bb6\u4e2d\uff0c\u4ece\u7a97\u6237\u671b\u5411\u8857\u9053\u7684\u65f6\u5019\u4f60\u770b\u5230\u4e00\u8f86\u6c7d\u8f66\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u5b83\uff0c\u4f46\u4f60\u5374\u65e0\u6cd5\u89e6\u6478\u6216\u8005\u79fb\u52a8\u5b83\u3002\u4f60\u7684\u7a97\u6237\u53ea\u662f\u4e3a\u4f60\u63d0\u4f9b\u4e86\u4e00\u4e2a\u53ef\u4ee5\u89c2\u5bdf\u6c7d\u8f66\u7684\u89c6\u56fe\uff0c\u800c\u8f66\u5219\u662f\u5b8c\u5168\u72ec\u7acb\u7684\u5bf9\u8c61\u3002 C++17 \u5f15\u5165\u4e86\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u53e6\u5916\u4e00\u79cd\u65b9\u5f0f\u2014\u2014 std:: string_view \uff0c\u5b83\u901a\u8fc7 \u5934\u6587\u4ef6\u63d0\u4f9b\u3002 \u548c std:: string \u4e0d\u540c\u7684\u662f\uff0c\u5b83\u4e0d\u4f1a\u4fdd\u5b58\u4e00\u4efd\u5b57\u7b26\u4e32\u7684\u526f\u672c\uff0c std:: string_view \u53ea\u662f\u63d0\u4f9b\u4e86\u4e00\u4e2a\u88ab\u5b9a\u4e49\u5728 \u5176\u4ed6\u5730\u65b9 \u7684\u5b57\u7b26\u4e32\u7684\u89c6\u56fe\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 std:: string_view \u91cd\u65b0\u7f16\u5199\u4e0a\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include int main () { std :: string_view text { \"hello\" }; // view the text \"hello\", which is stored in the binary std :: string_view str { text }; // view of the same \"hello\" std :: string_view more { str }; // view of the same \"hello\" std :: cout << text << ' ' << str << ' ' << more << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u662f\u76f8\u540c\u7684\uff0c\u4f46\u8fd9\u6b21\u4e0d\u518d\u9700\u8981\u521b\u5efa\u5b57\u7b26\u4e32 \u201chello\u201d \u7684\u526f\u672c\u3002\u5b57\u7b26\u4e32 \"hello\" \u5b58\u50a8\u5728\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d\uff0c\u4e0d\u4f1a\u5728\u8fd0\u884c\u65f6\u5206\u914d\u3002 text \u53ea\u662f\u5b57\u7b26\u4e32 \"hello\" \u4e0a\u7684\u4e00\u4e2a\u89c6\u56fe\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u521b\u5efa\u526f\u672c\u3002\u5f53\u6211\u4eec\u590d\u5236 std:: string_view \u65f6\uff0c\u65b0\u7684 std:: string_view \u89c2\u5bdf\u5230\u7684\u5b57\u7b26\u4e32\u4e0e\u88ab\u590d\u5236\u7684 std:: string_view \u662f\u76f8\u540c\u3002\u8fd9\u610f\u5473\u7740 str \u548c more \u90fd\u4e0d\u4f1a\u521b\u5efa\u4efb\u4f55\u526f\u672c\u3002\u5b83\u4eec\u662f\u73b0\u6709\u5b57\u7b26\u4e32 \"hello\" \u7684\u89c6\u56fe\u3002 std:: string_view \u4e0d\u4ec5\u4ec5\u5feb\uff0c\u800c\u4e14\u8fd8\u5177\u6709 std:: string \u7684\u5927\u90e8\u5206\u529f\u80fd\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include int main () { std :: string_view str { \"Trains are fast!\" }; std :: cout << str . length () << '\\n' ; // 16 std :: cout << str . substr ( 0 , str . find ( ' ' )) << '\\n' ; // Trains std :: cout << ( str == \"Trains are fast!\" ) << '\\n' ; // 1 // Since C++20 std :: cout << str . starts_with ( \"Boats\" ) << '\\n' ; // 0 std :: cout << str . ends_with ( \"fast!\" ) << '\\n' ; // 1 std :: cout << str << '\\n' ; // Trains are fast! return 0 ; } \u7531\u4e8e std:: string_view \u5e76\u4e0d\u4f1a\u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u526f\u672c\uff0c\u5f53\u6211\u4eec\u4fee\u6539 std:: string_view \u7684\u65f6\u5019\uff0c\u88ab\u89c2\u5bdf\u7684\u5b57\u7b26\u4e32\u4e5f\u4f1a\u88ab\u4fee\u6539\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { char arr []{ \"Gold\" }; std :: string_view str { arr }; std :: cout << str << '\\n' ; // Gold // Change 'd' to 'f' in arr arr [ 3 ] = 'f' ; std :: cout << str << '\\n' ; // Golf return 0 ; } \u6211\u4eec\u4fee\u6539\u4e86 arr \uff0c\u4f46\u662f str \u4e5f\u88ab\u4fee\u6539\u4e86\u3002\u8fd9\u662f\u56e0\u4e3a\u5b83\u4eec\u5b9e\u9645\u4e0a\u5171\u4eab\u7684\u662f\u540c\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u5982\u679c\u4f60\u9700\u8981\u4f7f\u7528 std:: string_view \uff0c\u90a3\u4e48\u6700\u597d\u4e0d\u8981\u53bb\u4fee\u6539\u5b83\u5bf9\u5e94\u7684\u5b57\u7b26\u4e32\uff0c\u907f\u514d\u9020\u6210\u56f0\u6270\u548c\u9519\u8bef\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528 std:: string_view \u4ee3\u66ff C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u3002 \u5bf9\u4e8e \u53ea\u8bfb \u5b57\u7b26\u4e32\uff0c\u4f18\u5148\u4f7f\u7528 std:: string_view \u800c\u4e0d\u662f std:: string \u3002\u9664\u975e\u4f60\u5df2\u7ecf\u5177\u6709\u4e86\u4e00\u4e2a std:: string \u3002 \u89c6\u56fe\u4fee\u6539\u51fd\u6570 \u56de\u5230\u4e4b\u524d\u6709\u5173\u201d\u7a97\u6237\u201c\u7684\u6bd4\u55bb\uff0c\u8003\u8651\u4e00\u4e2a\u6709\u201d\u7a97\u5e18\u201c\u7684\u7a97\u6237\u3002\u901a\u8fc7\u62c9\u4e0a\u5de6\u4fa7\u6216\u53f3\u4fa7\u7684\u7a97\u5e18\uff0c\u6211\u4eec\u53ef\u4ee5\u9650\u5236\u80fd\u591f\u4ece\u7a97\u6237\u770b\u5230\u7684\u8303\u56f4\uff0c\u4f46\u662f\u8fd9\u5e76\u4e0d\u4f1a\u5b9e\u9645\u6539\u53d8\u7a97\u6237\u5916\u7684\u7269\u4f53\uff0c\u6211\u4eec\u53ea\u662f\u51cf\u5c0f\u4e86\u89c6\u89d2\u800c\u5df2\u3002 \u7c7b\u4f3c\u5730\uff0c std:: string_view \u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\u7528\u4e8e\u64cd\u4f5c\u89c2\u5bdf\u5b57\u7b26\u4e32\u7684\u89c6\u56fe\u3002\u901a\u8fc7\u8fd9\u4e9b\u51fd\u6570\u6211\u4eec\u53ef\u4ee5\u4fee\u6539\u770b\u5230\u7684\u5b57\u7b26\u4e32\uff0c\u4f46\u4e0d\u4f1a\u4fee\u6539\u88ab\u89c2\u5bdf\u7684\u5b57\u7b26\u4e32\u672c\u8eab\u3002 remove_prefix \u7528\u4e8e\u4ece\u89c6\u56fe\u7684\u5de6\u4fa7\u5220\u9664\u5b57\u7b26\uff0c\u800c remove_suffix \u5219\u7528\u4e8e\u4ece\u89c6\u56fe\u7684\u53f3\u4fa7\u5220\u9664\u5b57\u7b26\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include int main () { std :: string_view str { \"Peach\" }; std :: cout << str << '\\n' ; // Ignore the first character. str . remove_prefix ( 1 ); std :: cout << str << '\\n' ; // Ignore the last 2 characters. str . remove_suffix ( 2 ); std :: cout << str << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 3 Peach each ea \u4e0e\u771f\u6b63\u7684\u7a97\u5e18\u4e0d\u540c\uff0c std:: string_view \u4e0d\u80fd\u88ab\u91cd\u65b0\u6253\u5f00\u3002\u4e00\u65e6\u4f60\u7f29\u5c0f\u4e86\u533a\u57df\uff0c\u91cd\u65b0\u6269\u5927\u5b83\u7684\u552f\u4e00\u65b9\u6cd5\u5c31\u662f\u901a\u8fc7\u91cd\u65b0\u5206\u914d\u6e90\u5b57\u7b26\u4e32\u7ed9\u5b83\u6765\u91cd\u7f6e\u89c6\u56fe\u3002 std::string_view \u53ef\u4ee5\u914d\u5408\u6ca1\u6709\u7a7a\u5b57\u7b26\u7ed3\u675f\u7b26\u7684\u5b57\u7b26\u4e32 C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u548c std:: string \u4e0d\u540c\uff0c std:: string_view \u4e0d\u4f7f\u7528\u7a7a\u7ed3\u675f\u7b26\u6765\u6807\u8bb0\u5b57\u7b26\u4e32\u7684\u7ed3\u675f\u3002\u76f8\u53cd\uff0c\u5b83\u77e5\u9053\u5b57\u7b26\u4e32\u5728\u54ea\u91cc\u7ed3\u675f\uff0c\u56e0\u4e3a\u5b83\u4f1a\u8ddf\u8e2a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include // For std::size #include int main () { // No null-terminator. char vowels []{ 'a' , 'e' , 'i' , 'o' , 'u' }; // vowels \u5e76\u6ca1\u6709\u5305\u542b\u7ed3\u675f\u7b26\uff0c\u6211\u4eec\u9700\u8981\u4f20\u9012\u957f\u5ea6 // \u56e0\u4e3a vowels \u662f\u6570\u7ec4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 std::size \u6765\u83b7\u53d6\u957f\u5ea6 std :: string_view str { vowels , std :: size ( vowels ) }; std :: cout << str << '\\n' ; // \u8fd9\u662f\u5b89\u5168\u7684\u3002std::cout \u77e5\u9053\u5982\u4f55\u6253\u5370 std::string_view. return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 aeiou \u5c06 std::string_view \u8f6c\u6362\u4e3a C \u98ce\u683c\u5b57\u7b26\u4e32 \u4e00\u4e9b\u65e7\u7684\u51fd\u6570(\u6bd4\u5982\u65e7\u7684 strlen \u51fd\u6570)\u4ecd\u7136\u671f\u671b\u4f7f\u7528C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002\u8981\u5c06 std:: string_view \u8f6c\u6362\u4e3aC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u8f6c\u6362\u4e3a std:: string \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include #include #include int main () { std :: string_view sv { \"balloon\" }; sv . remove_suffix ( 3 ); // Create a std::string from the std::string_view std :: string str { sv }; // Get the null-terminated C-style string. auto szNullTerminated { str . c_str () }; // Pass the null-terminated string to the function that we want to use. std :: cout << str << \" has \" << std :: strlen ( szNullTerminated ) << \" letter(s) \\n \" ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 ball has 4 letter(s) \u4e0d\u8fc7\uff0c\u6bcf\u6b21 std:: string_view \u9700\u8981\u88ab\u4f5c\u4e3aC\u98ce\u683c\u5b57\u7b26\u4e32\u4f20\u9012\u65f6\uff0c\u90fd\u9700\u8981\u521b\u5efa\u4e00\u4e2a std::string \uff0c\u8fd9\u4e48\u505a\u7684\u5f00\u9500\u662f\u975e\u5e38\u5927\u7684\uff0c\u56e0\u6b64\u5e94\u8be5\u5c3d\u91cf\u907f\u514d\u8fd9\u4e48\u505a\u3002 \u901a\u8fc7 const std::string& \u8fd8\u662f std::string_view \u6765\u4f20\u9012\u5b57\u7b26\u4e32\uff1f \u4e00\u4e2a\u7ecf\u5e38\u9047\u5230\u7684\u95ee\u9898\u662f\uff1a\u5e94\u8be5\u901a\u8fc7 const std::string& \u8fd8\u662f std::string_view \u4f20\u9012\u5b57\u7b26\u4e32\uff1f\u90a3\u79cd\u65b9\u5f0f\u6700\u597d\uff1f \u5982\u679c\u6211\u4eec\u60f3\u5199\u4e00\u4e2a\u63a5\u53d7\u5b57\u7b26\u4e32\u5f62\u53c2\u7684\u51fd\u6570\uff0c\u5c06\u5f62\u53c2\u8bbe\u7f6e\u4e3a std::string_view \u662f\u6700\u7075\u6d3b\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u9ad8\u6548\u5730\u914d\u5408C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u53c2\u6570(\u5305\u62ec\u5b57\u7b26\u4e32\u5b57\u9762\u91cf)\u3001 std::string \u53c2\u6570(\u5c06\u9690\u5f0f\u8f6c\u6362\u4e3a std::string_view ')\u548c std::string_view \u53c2\u6570\u6765\u5de5\u4f5c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include #include void printSV ( std :: string_view sv ) { std :: cout << sv << '\\n' ; } int main () { std :: string s { \"Hello, world\" }; std :: string_view sv { s }; printSV ( s ); // ok: pass std::string printSV ( sv ); // ok: pass std::string_view printSV ( \"Hello, world\" ); // ok: pass C-style string literal return 0 ; } \u6ce8\u610f\uff0c\u6211\u4eec\u901a\u8fc7\u503c\u4f20\u9012 std::string_view \uff0c\u800c\u4e0d\u662f\u901a\u8fc7 const \u5f15\u7528\u7c7b\u578b\u6765\u4f20\u9012\u3002\u8fd9\u662f\u56e0\u4e3a std::string_view \u7684\u590d\u5236\u901f\u5ea6\u901a\u5e38\u8f83\u5feb\uff0c\u800c \u6309\u503c\u4f20\u9012 \u5bf9\u4e8e\u590d\u5236\u5f00\u9500\u8f83\u5c0f\u7684\u7c7b\u578b\u6765\u8bf4\u662f\u6700\u4f18\u7684\u3002 \u6709\u4e00\u79cd\u60c5\u51b5\uff0c\u5c06\u5f62\u53c2\u8bbe\u7f6e\u4e3a const std::string& \u901a\u5e38\u66f4\u597d\uff1a\u5982\u679c\u4f60\u7684\u51fd\u6570\u9700\u8981\u8c03\u7528\u5176\u4ed6\u63a5\u53d7C\u98ce\u683c\u5b57\u7b26\u4e32\u6216 std::string \u5f62\u53c2\u7684\u51fd\u6570\uff0c\u90a3\u4e48 const std::string& \u53ef\u80fd\u662f\u66f4\u597d\u7684\u9009\u62e9\uff0c\u56e0\u4e3a std::string_view \u4e0d\u80fd\u4fdd\u8bc1\u4ee5\u7a7a\u5b57\u7b26\u7ed3\u675f(\u800c\u8fd9\u662fC\u98ce\u683c\u5b57\u7b26\u4e32\u51fd\u6570\u6240\u671f\u671b\u7684)\uff0c\u5e76\u4e14\u4e0d\u80fd\u6709\u6548\u5730\u8f6c\u6362\u56de std::string \u3002 \u6700\u4f73\u5b9e\u8df5 \u4f18\u5148\u4f7f\u7528 std:: string_view ( \u6309\u503c\u4f20\u9012 )\u800c\u4e0d\u662f const std:: string& \uff0c\u9664\u975e\u4f60\u9700\u8981\u8c03\u7528\u5176\u4ed6\u8981\u6c42\u4f7f\u7528C\u98ce\u683c\u5b57\u7b26\u4e32\u6216 std::string \u7684\u51fd\u6570\u3002 \u4f5c\u8005\u6ce8 \u540e\u9762\u7684\u4e00\u4e9b\u8bfe\u7a0b\u662f\u5728\u5f15\u5165 std::string_view \u4e4b\u524d\u7f16\u5199\u7684\uff0c\u6240\u4ee5\u4ecd\u7136\u4f7f\u7528\u7684\u662f const std::string& \uff08\u5b9e\u9645\u4e0a\u7528 std::string_view \u66f4\u597d\uff09\uff0c\u6211\u4eec\u6b63\u5728\u7740\u624b\u6e05\u7406\u76f8\u5173\u7684\u4ee3\u7801\u3002 \u6240\u6709\u6743\u95ee\u9898 std::string_view \u7684\u751f\u547d\u5468\u671f\u662f\u72ec\u7acb\u4e8e\u5b83\u201c\u89c2\u5bdf\u201d\u7684\u5b57\u7b26\u4e32\u7684\uff08\u4e5f\u5c31\u662f\u8bf4\u8be5\u5b57\u7b26\u4e32\u5bf9\u8c61\u53ef\u4ee5\u5728\u5148\u4e8e std::string_view \u88ab\u9500\u6bc1)\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u8bbf\u95ee std::string_view \u5c31\u4f1a\u9020\u6210 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 std::string_view \u8868\u793a\u7684\u5b57\u7b26\u4e32\u5fc5\u987b\u662f\u5728\u5176\u4ed6\u5730\u65b9\u5df2\u7ecf\u88ab\u521b\u5efa\u7684\u5b57\u7b26\u4e32\u3002\u5b83\u53ef\u80fd\u662f\u4e00\u4e2a\u968f\u7740\u7a0b\u5e8f\u542f\u52a8\u505c\u6b62\u800c\u521b\u5efa\u9500\u6bc1\u7684 \u5b57\u9762\u91cf \uff0c\u4e5f\u53ef\u80fd\u662f\u4e00\u4e2a std::string \uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5f53 str::string \u9500\u6bc1\u65f6\u8be5\u5b57\u7b26\u4e32\u4e5f\u5c31\u4e0d\u5b58\u5728\u4e86\u3002 std:: string_view \u81ea\u8eab\u4e0d\u80fd\u521b\u5efa\u4efb\u4f55\u7684\u5b57\u7b26\u4e32\uff0c\u5b83\u53ea\u662f\u89c2\u5bdf\u67d0\u4e2a\u5b57\u7b26\u4e32\u7684\u89c6\u56fe\u3002 \u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\u5c31\u51fa\u73b0\u4e86\u6240\u6709\u6743\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include #include #include std :: string_view askForName () { std :: cout << \"What's your name? \\n \" ; // Use a std::string, because std::cin needs to modify it. std :: string name {}; std :: cin >> name ; // We're switching to std::string_view for demonstrative purposes only. // If you already have a std::string, there's no reason to switch to // a std::string_view. std :: string_view view { name }; std :: cout << \"Hello \" << view << '\\n' ; return view ; } // name \u53d8\u91cf\u88ab\u9500\u6bc1\uff0c\u5bf9\u5e94\u7684\u5b57\u7b26\u4e32\u4e5f\u9500\u6bc1\u4e86 int main () { std :: string_view view { askForName () }; // view \u6b63\u5728\u4e3a\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u5b57\u7b26\u4e32\u63d0\u4f9b\u89c6\u56fe std :: cout << \"Your name is \" << view << '\\n' ; // \u672a\u5b9a\u4e49\u884c\u4e3a return 0 ; } 1 2 3 4 What's your name? nascardriver Hello nascardriver Your name is \ufffdP@\ufffdP@ \u5728 askForName() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u53d8\u91cf name \u5e76\u4e14\u901a\u8fc7 std::cin \u5bf9\u5176\u8fdb\u884c\u4e86\u8d4b\u503c\u3002\u7136\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u53d8\u91cf view \uff0c\u901a\u8fc7\u8fd9\u4e2a\u53d8\u91cf\u6211\u4eec\u53ef\u201c\u89c2\u5bdf\u201d\u4e0a\u8ff0\u5b57\u7b26\u4e32\u3002\u5728\u51fd\u6570\u7ed3\u675f\u65f6\uff0c\u6211\u4eec\u8fd4\u56de view \uff0c\u4f46\u662f\u8be5\u53d8\u91cf\u5b9e\u9645\u89c2\u5bdf\u7684\u5b57\u7b26\u4e32 ( name ) \u5df2\u7ecf\u88ab\u9500\u6bc1\u4e86\uff0c\u56e0\u6b64 view \u73b0\u5728\u6307\u5411\u7684\u662f\u4e00\u5757\u5df2\u7ecf\u88ab\u91ca\u653e\u7684\u5185\u5b58\u3002\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a \u60ac\u5782(dangling) std::string_view . \u5728 mian \u51fd\u6570\u4e2d\u8bbf\u95ee\u88ab\u8fd4\u56de\u7684 std::string_view \u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\uff0c\u4f1a\u6253\u5370\u5947\u602a\u7684\u5b57\u7b26\u3002 \u95ee\u9898\u53d1\u751f\u7684\u539f\u56e0\u5728\u4e8e\u4f60\u901a\u8fc7 std::string \u521b\u5efa\u4e86\u4e00\u4e2a std::string_view \uff0c\u7136\u540e\u53c8\u4fee\u6539\u4e86 std::string \u3002\u4fee\u6539 std::string \u4f1a\u5bfc\u81f4\u5185\u90e8\u5b57\u7b26\u4e32\u88ab\u9500\u6bc1\u6216\u88ab\u66ff\u6362\uff08\u5728\u4ee3\u7801\u7684\u5176\u4ed6\u90e8\u5206\uff09\u3002 std::string_view \u603b\u662f\u6307\u5411\u8be5\u5b57\u7b26\u4e32\uff0c \u4f46\u8be5\u5b57\u7b26\u4e32\u53ef\u80fd\u5df2\u7ecf\u4e0d\u662f\u5b83\u539f\u6765\u7684\u6a21\u6837\u6216\u8005\u5df2\u7ecf\u4e0d\u5b58\u5728\u4e86\u3002 \u6ce8\u610f \u786e\u4fdd std::string_view \u89c2\u5bdf\u7684\u5b57\u7b26\u4e32\u4e0d\u4f1a \u79bb\u5f00\u4f5c\u7528\u57df \uff0c\u4e5f\u4e0d\u4f1a\u88ab\u4fee\u6539\u3002 \u4f7f\u7528 data() \u51fd\u6570\u6765\u6253\u5f00\u201d\u7a97\u6237\u201c std::string_view \u89c2\u5bdf\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u901a\u8fc7 data() \u51fd\u6570\u6765\u8bbf\u95ee\uff0c\u5b83\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u4e00\u4e2aC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u5f0f\u63d0\u4f9b\u4e86\u4e00\u79cd\u5feb\u901f\u8bbf\u95ee\u88ab\u89c2\u5bdf\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3002\u4f46\u662f\u5b83\u53ea\u80fd\u5728 std::string_view \u89c6\u56fe\u6ca1\u6709\u88ab\u4fee\u6539\u7684\u65f6\u5019\u4f7f\u7528(\u4f8b\u5982\u901a\u8fc7 remove_prefix \u6216 remove_suffix \u4fee\u6539\u89c6\u56fe)\uff0c\uff0c\u8be5\u5b57\u7b26\u4e32\u662f\u6709\u7a7a\u7ed3\u675f\u7b26\u7684\u3002 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c std::strlen \u51fd\u6570\u4e0d\u80fd\u8bc6\u522b std:: string_view \uff0c\u56e0\u6b64\u6211\u4eec\u5fc5\u987b\u4f20\u5165 str.data() \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include // For std::strlen #include #include int main () { std :: string_view str { \"balloon\" }; std :: cout << str << '\\n' ; // We use std::strlen because it's simple, this could be any other function // that needs a null-terminated string. // It's okay to use data() because we haven't modified the view, and the // string is null-terminated. std :: cout << std :: strlen ( str . data ()) << '\\n' ; return 0 ; } 1 2 balloon 7 \u5f53 std:: string_view \u88ab\u4fee\u6539\u540e\uff0c data() \u5c31\u4e0d\u80fd\u5b8c\u6210\u6211\u4eec\u671f\u671b\u7684\u4efb\u52a1\u4e86\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5f53\u89c6\u56fe\u88ab\u4fee\u6539\u540e data() \u7684\u884c\u4e3a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include #include int main () { std :: string_view str { \"balloon\" }; // Remove the \"b\" str . remove_prefix ( 1 ); // remove the \"oon\" str . remove_suffix ( 3 ); // Remember that the above doesn't modify the string, it only changes // the region that str is observing. std :: cout << str << \" has \" << std :: strlen ( str . data ()) << \" letter(s) \\n \" ; std :: cout << \"str.data() is \" << str . data () << '\\n' ; std :: cout << \"str is \" << str << '\\n' ; return 0 ; } 1 2 3 all has 6 letter(s) str.data() is alloon str is all \u8fd9\u663e\u7136\u4e0d\u662f\u6211\u4eec\u671f\u671b\u7684\uff0c\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528 data() \u6765\u8bbf\u95ee\u4e00\u4e2a\u5df2\u7ecf\u88ab\u4fee\u6539\u7684 std::string_view \u3002\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u4fe1\u606f\u5728\u6211\u4eec\u4f7f\u7528 data() \u65f6\u5c31\u4e22\u5931\u4e86\uff0c std:: strlen \u548c std:: cout \u4f1a\u6301\u7eed\u8bfb\u53d6\u76f4\u5230\u9047\u5230\u7b2c\u4e00\u4e2a\u7a7a\u7ed3\u675f\u7b26\uff0c\u4e5f\u5c31\u662f\u201cballoon\u201d\u7684\u672b\u5c3e\u3002 \u6ce8\u610f \u53ea\u6709\u5728 std::string_view \u6ca1\u6709\u88ab\u4fee\u6539\u7684\u60c5\u51b5\u4e0b\u4e14\u5b57\u7b26\u4e32\u4ee5\u7a7a\u7ed3\u675f\u7b26\u7ed3\u5c3e\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528 std:: string_view:: data() \u5982\u679c\u5b57\u7b26\u4e32\u6ca1\u6709\u4ee5\u7a7a\u7ed3\u675f\u7b26\u7ed3\u5c3e\uff0c\u5219 std:: string_view:: data() \u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 \u4e0d\u5b8c\u6574\u7684\u5b9e\u73b0 \u4f5c\u4e3a\u4e00\u4e2a\u76f8\u5bf9\u8f83\u65b0\u7684\u7279\u6027\u6765\u8bf4\uff0c std::string_view \u7684\u5b9e\u73b0\u5b8c\u6210\u5ea6\u5e76\u4e0d\u5982\u9884\u671f\u3002 ```cpp hl_lines=\"5,6\" std::string s{ \"hello\" }; std::string_view v{ \"world\" }; // \u4e0d\u80fd\u5de5\u4f5c std::cout << (s + v) << '\\n'; std::cout << (v + s) << '\\n'; // \u53ef\u80fd\u4e0d\u5b89\u5168\uff0c\u4e5f\u53ef\u80fd\u5e76\u4e0d\u662f\u6211\u4eec\u5e0c\u671b\u7684\u505a\u6cd5\uff0c\u56e0\u4e3a\u6211\u4eec\u6b64\u65f6\u5c06std::string_view\u5f53\u505a // C\u98ce\u683c\u5b57\u7b26\u4e32\u6765\u4f7f\u7528 std::cout << (s + v.data()) << '\\n'; std::cout << (v.data() + s) << '\\n'; // \u53ef\u4ee5, \u53ef\u4ee5\u4f46\u662f\u4e0d\u4f18\u96c5\uff0c\u800c\u4e14\u6d6a\u8d39\u8d44\u6e90\uff0c\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u6784\u5efa\u4e00\u4e2a\u65b0\u7684 std::string. std::cout << (s + std::string{ v }) << '\\n'; std::cout << (std::string{ v } + s) << '\\n'; std::cout << (s + static_cast (v)) << '\\n'; std::cout << (static_cast (v) + s) << '\\n'; ``` \u6ca1\u9053\u7406\u4e0d\u80fd\u4f7f\u7528\u7b2c\u4e94\u884c\u548c\u7b2c\u516d\u884c\u7684\u65b9\u5f0f\uff0c\u8fd9\u4e2a\u7279\u6027\u53ef\u80fd\u4f1a\u5728\u672a\u6765\u7684C++\u7248\u672c\u4e2d\u88ab\u652f\u6301\u3002","title":"11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09"},{"location":"11-7-std-string-view-part-2/#std-string_view","text":"\u60f3\u8c61\u8fd9\u6837\u4e00\u4e2a\u573a\u666f\uff0c\u4f60\u5750\u5728\u5bb6\u4e2d\uff0c\u4ece\u7a97\u6237\u671b\u5411\u8857\u9053\u7684\u65f6\u5019\u4f60\u770b\u5230\u4e00\u8f86\u6c7d\u8f66\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u5b83\uff0c\u4f46\u4f60\u5374\u65e0\u6cd5\u89e6\u6478\u6216\u8005\u79fb\u52a8\u5b83\u3002\u4f60\u7684\u7a97\u6237\u53ea\u662f\u4e3a\u4f60\u63d0\u4f9b\u4e86\u4e00\u4e2a\u53ef\u4ee5\u89c2\u5bdf\u6c7d\u8f66\u7684\u89c6\u56fe\uff0c\u800c\u8f66\u5219\u662f\u5b8c\u5168\u72ec\u7acb\u7684\u5bf9\u8c61\u3002 C++17 \u5f15\u5165\u4e86\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u53e6\u5916\u4e00\u79cd\u65b9\u5f0f\u2014\u2014 std:: string_view \uff0c\u5b83\u901a\u8fc7 \u5934\u6587\u4ef6\u63d0\u4f9b\u3002 \u548c std:: string \u4e0d\u540c\u7684\u662f\uff0c\u5b83\u4e0d\u4f1a\u4fdd\u5b58\u4e00\u4efd\u5b57\u7b26\u4e32\u7684\u526f\u672c\uff0c std:: string_view \u53ea\u662f\u63d0\u4f9b\u4e86\u4e00\u4e2a\u88ab\u5b9a\u4e49\u5728 \u5176\u4ed6\u5730\u65b9 \u7684\u5b57\u7b26\u4e32\u7684\u89c6\u56fe\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 std:: string_view \u91cd\u65b0\u7f16\u5199\u4e0a\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include int main () { std :: string_view text { \"hello\" }; // view the text \"hello\", which is stored in the binary std :: string_view str { text }; // view of the same \"hello\" std :: string_view more { str }; // view of the same \"hello\" std :: cout << text << ' ' << str << ' ' << more << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u662f\u76f8\u540c\u7684\uff0c\u4f46\u8fd9\u6b21\u4e0d\u518d\u9700\u8981\u521b\u5efa\u5b57\u7b26\u4e32 \u201chello\u201d \u7684\u526f\u672c\u3002\u5b57\u7b26\u4e32 \"hello\" \u5b58\u50a8\u5728\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d\uff0c\u4e0d\u4f1a\u5728\u8fd0\u884c\u65f6\u5206\u914d\u3002 text \u53ea\u662f\u5b57\u7b26\u4e32 \"hello\" \u4e0a\u7684\u4e00\u4e2a\u89c6\u56fe\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u521b\u5efa\u526f\u672c\u3002\u5f53\u6211\u4eec\u590d\u5236 std:: string_view \u65f6\uff0c\u65b0\u7684 std:: string_view \u89c2\u5bdf\u5230\u7684\u5b57\u7b26\u4e32\u4e0e\u88ab\u590d\u5236\u7684 std:: string_view \u662f\u76f8\u540c\u3002\u8fd9\u610f\u5473\u7740 str \u548c more \u90fd\u4e0d\u4f1a\u521b\u5efa\u4efb\u4f55\u526f\u672c\u3002\u5b83\u4eec\u662f\u73b0\u6709\u5b57\u7b26\u4e32 \"hello\" \u7684\u89c6\u56fe\u3002 std:: string_view \u4e0d\u4ec5\u4ec5\u5feb\uff0c\u800c\u4e14\u8fd8\u5177\u6709 std:: string \u7684\u5927\u90e8\u5206\u529f\u80fd\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include int main () { std :: string_view str { \"Trains are fast!\" }; std :: cout << str . length () << '\\n' ; // 16 std :: cout << str . substr ( 0 , str . find ( ' ' )) << '\\n' ; // Trains std :: cout << ( str == \"Trains are fast!\" ) << '\\n' ; // 1 // Since C++20 std :: cout << str . starts_with ( \"Boats\" ) << '\\n' ; // 0 std :: cout << str . ends_with ( \"fast!\" ) << '\\n' ; // 1 std :: cout << str << '\\n' ; // Trains are fast! return 0 ; } \u7531\u4e8e std:: string_view \u5e76\u4e0d\u4f1a\u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u526f\u672c\uff0c\u5f53\u6211\u4eec\u4fee\u6539 std:: string_view \u7684\u65f6\u5019\uff0c\u88ab\u89c2\u5bdf\u7684\u5b57\u7b26\u4e32\u4e5f\u4f1a\u88ab\u4fee\u6539\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { char arr []{ \"Gold\" }; std :: string_view str { arr }; std :: cout << str << '\\n' ; // Gold // Change 'd' to 'f' in arr arr [ 3 ] = 'f' ; std :: cout << str << '\\n' ; // Golf return 0 ; } \u6211\u4eec\u4fee\u6539\u4e86 arr \uff0c\u4f46\u662f str \u4e5f\u88ab\u4fee\u6539\u4e86\u3002\u8fd9\u662f\u56e0\u4e3a\u5b83\u4eec\u5b9e\u9645\u4e0a\u5171\u4eab\u7684\u662f\u540c\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u5982\u679c\u4f60\u9700\u8981\u4f7f\u7528 std:: string_view \uff0c\u90a3\u4e48\u6700\u597d\u4e0d\u8981\u53bb\u4fee\u6539\u5b83\u5bf9\u5e94\u7684\u5b57\u7b26\u4e32\uff0c\u907f\u514d\u9020\u6210\u56f0\u6270\u548c\u9519\u8bef\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528 std:: string_view \u4ee3\u66ff C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u3002 \u5bf9\u4e8e \u53ea\u8bfb \u5b57\u7b26\u4e32\uff0c\u4f18\u5148\u4f7f\u7528 std:: string_view \u800c\u4e0d\u662f std:: string \u3002\u9664\u975e\u4f60\u5df2\u7ecf\u5177\u6709\u4e86\u4e00\u4e2a std:: string \u3002","title":"std:: string_view"},{"location":"11-7-std-string-view-part-2/#_1","text":"\u56de\u5230\u4e4b\u524d\u6709\u5173\u201d\u7a97\u6237\u201c\u7684\u6bd4\u55bb\uff0c\u8003\u8651\u4e00\u4e2a\u6709\u201d\u7a97\u5e18\u201c\u7684\u7a97\u6237\u3002\u901a\u8fc7\u62c9\u4e0a\u5de6\u4fa7\u6216\u53f3\u4fa7\u7684\u7a97\u5e18\uff0c\u6211\u4eec\u53ef\u4ee5\u9650\u5236\u80fd\u591f\u4ece\u7a97\u6237\u770b\u5230\u7684\u8303\u56f4\uff0c\u4f46\u662f\u8fd9\u5e76\u4e0d\u4f1a\u5b9e\u9645\u6539\u53d8\u7a97\u6237\u5916\u7684\u7269\u4f53\uff0c\u6211\u4eec\u53ea\u662f\u51cf\u5c0f\u4e86\u89c6\u89d2\u800c\u5df2\u3002 \u7c7b\u4f3c\u5730\uff0c std:: string_view \u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\u7528\u4e8e\u64cd\u4f5c\u89c2\u5bdf\u5b57\u7b26\u4e32\u7684\u89c6\u56fe\u3002\u901a\u8fc7\u8fd9\u4e9b\u51fd\u6570\u6211\u4eec\u53ef\u4ee5\u4fee\u6539\u770b\u5230\u7684\u5b57\u7b26\u4e32\uff0c\u4f46\u4e0d\u4f1a\u4fee\u6539\u88ab\u89c2\u5bdf\u7684\u5b57\u7b26\u4e32\u672c\u8eab\u3002 remove_prefix \u7528\u4e8e\u4ece\u89c6\u56fe\u7684\u5de6\u4fa7\u5220\u9664\u5b57\u7b26\uff0c\u800c remove_suffix \u5219\u7528\u4e8e\u4ece\u89c6\u56fe\u7684\u53f3\u4fa7\u5220\u9664\u5b57\u7b26\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include int main () { std :: string_view str { \"Peach\" }; std :: cout << str << '\\n' ; // Ignore the first character. str . remove_prefix ( 1 ); std :: cout << str << '\\n' ; // Ignore the last 2 characters. str . remove_suffix ( 2 ); std :: cout << str << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 3 Peach each ea \u4e0e\u771f\u6b63\u7684\u7a97\u5e18\u4e0d\u540c\uff0c std:: string_view \u4e0d\u80fd\u88ab\u91cd\u65b0\u6253\u5f00\u3002\u4e00\u65e6\u4f60\u7f29\u5c0f\u4e86\u533a\u57df\uff0c\u91cd\u65b0\u6269\u5927\u5b83\u7684\u552f\u4e00\u65b9\u6cd5\u5c31\u662f\u901a\u8fc7\u91cd\u65b0\u5206\u914d\u6e90\u5b57\u7b26\u4e32\u7ed9\u5b83\u6765\u91cd\u7f6e\u89c6\u56fe\u3002","title":"\u89c6\u56fe\u4fee\u6539\u51fd\u6570"},{"location":"11-7-std-string-view-part-2/#stdstring_view","text":"C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u548c std:: string \u4e0d\u540c\uff0c std:: string_view \u4e0d\u4f7f\u7528\u7a7a\u7ed3\u675f\u7b26\u6765\u6807\u8bb0\u5b57\u7b26\u4e32\u7684\u7ed3\u675f\u3002\u76f8\u53cd\uff0c\u5b83\u77e5\u9053\u5b57\u7b26\u4e32\u5728\u54ea\u91cc\u7ed3\u675f\uff0c\u56e0\u4e3a\u5b83\u4f1a\u8ddf\u8e2a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include // For std::size #include int main () { // No null-terminator. char vowels []{ 'a' , 'e' , 'i' , 'o' , 'u' }; // vowels \u5e76\u6ca1\u6709\u5305\u542b\u7ed3\u675f\u7b26\uff0c\u6211\u4eec\u9700\u8981\u4f20\u9012\u957f\u5ea6 // \u56e0\u4e3a vowels \u662f\u6570\u7ec4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 std::size \u6765\u83b7\u53d6\u957f\u5ea6 std :: string_view str { vowels , std :: size ( vowels ) }; std :: cout << str << '\\n' ; // \u8fd9\u662f\u5b89\u5168\u7684\u3002std::cout \u77e5\u9053\u5982\u4f55\u6253\u5370 std::string_view. return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 aeiou","title":"std::string_view \u53ef\u4ee5\u914d\u5408\u6ca1\u6709\u7a7a\u5b57\u7b26\u7ed3\u675f\u7b26\u7684\u5b57\u7b26\u4e32"},{"location":"11-7-std-string-view-part-2/#stdstring_view-c","text":"\u4e00\u4e9b\u65e7\u7684\u51fd\u6570(\u6bd4\u5982\u65e7\u7684 strlen \u51fd\u6570)\u4ecd\u7136\u671f\u671b\u4f7f\u7528C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002\u8981\u5c06 std:: string_view \u8f6c\u6362\u4e3aC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u8f6c\u6362\u4e3a std:: string \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include #include #include int main () { std :: string_view sv { \"balloon\" }; sv . remove_suffix ( 3 ); // Create a std::string from the std::string_view std :: string str { sv }; // Get the null-terminated C-style string. auto szNullTerminated { str . c_str () }; // Pass the null-terminated string to the function that we want to use. std :: cout << str << \" has \" << std :: strlen ( szNullTerminated ) << \" letter(s) \\n \" ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 ball has 4 letter(s) \u4e0d\u8fc7\uff0c\u6bcf\u6b21 std:: string_view \u9700\u8981\u88ab\u4f5c\u4e3aC\u98ce\u683c\u5b57\u7b26\u4e32\u4f20\u9012\u65f6\uff0c\u90fd\u9700\u8981\u521b\u5efa\u4e00\u4e2a std::string \uff0c\u8fd9\u4e48\u505a\u7684\u5f00\u9500\u662f\u975e\u5e38\u5927\u7684\uff0c\u56e0\u6b64\u5e94\u8be5\u5c3d\u91cf\u907f\u514d\u8fd9\u4e48\u505a\u3002","title":"\u5c06\u00a0std::string_view\u00a0\u8f6c\u6362\u4e3a C \u98ce\u683c\u5b57\u7b26\u4e32"},{"location":"11-7-std-string-view-part-2/#const-stdstring-stdstring_view","text":"\u4e00\u4e2a\u7ecf\u5e38\u9047\u5230\u7684\u95ee\u9898\u662f\uff1a\u5e94\u8be5\u901a\u8fc7 const std::string& \u8fd8\u662f std::string_view \u4f20\u9012\u5b57\u7b26\u4e32\uff1f\u90a3\u79cd\u65b9\u5f0f\u6700\u597d\uff1f \u5982\u679c\u6211\u4eec\u60f3\u5199\u4e00\u4e2a\u63a5\u53d7\u5b57\u7b26\u4e32\u5f62\u53c2\u7684\u51fd\u6570\uff0c\u5c06\u5f62\u53c2\u8bbe\u7f6e\u4e3a std::string_view \u662f\u6700\u7075\u6d3b\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u9ad8\u6548\u5730\u914d\u5408C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u53c2\u6570(\u5305\u62ec\u5b57\u7b26\u4e32\u5b57\u9762\u91cf)\u3001 std::string \u53c2\u6570(\u5c06\u9690\u5f0f\u8f6c\u6362\u4e3a std::string_view ')\u548c std::string_view \u53c2\u6570\u6765\u5de5\u4f5c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include #include void printSV ( std :: string_view sv ) { std :: cout << sv << '\\n' ; } int main () { std :: string s { \"Hello, world\" }; std :: string_view sv { s }; printSV ( s ); // ok: pass std::string printSV ( sv ); // ok: pass std::string_view printSV ( \"Hello, world\" ); // ok: pass C-style string literal return 0 ; } \u6ce8\u610f\uff0c\u6211\u4eec\u901a\u8fc7\u503c\u4f20\u9012 std::string_view \uff0c\u800c\u4e0d\u662f\u901a\u8fc7 const \u5f15\u7528\u7c7b\u578b\u6765\u4f20\u9012\u3002\u8fd9\u662f\u56e0\u4e3a std::string_view \u7684\u590d\u5236\u901f\u5ea6\u901a\u5e38\u8f83\u5feb\uff0c\u800c \u6309\u503c\u4f20\u9012 \u5bf9\u4e8e\u590d\u5236\u5f00\u9500\u8f83\u5c0f\u7684\u7c7b\u578b\u6765\u8bf4\u662f\u6700\u4f18\u7684\u3002 \u6709\u4e00\u79cd\u60c5\u51b5\uff0c\u5c06\u5f62\u53c2\u8bbe\u7f6e\u4e3a const std::string& \u901a\u5e38\u66f4\u597d\uff1a\u5982\u679c\u4f60\u7684\u51fd\u6570\u9700\u8981\u8c03\u7528\u5176\u4ed6\u63a5\u53d7C\u98ce\u683c\u5b57\u7b26\u4e32\u6216 std::string \u5f62\u53c2\u7684\u51fd\u6570\uff0c\u90a3\u4e48 const std::string& \u53ef\u80fd\u662f\u66f4\u597d\u7684\u9009\u62e9\uff0c\u56e0\u4e3a std::string_view \u4e0d\u80fd\u4fdd\u8bc1\u4ee5\u7a7a\u5b57\u7b26\u7ed3\u675f(\u800c\u8fd9\u662fC\u98ce\u683c\u5b57\u7b26\u4e32\u51fd\u6570\u6240\u671f\u671b\u7684)\uff0c\u5e76\u4e14\u4e0d\u80fd\u6709\u6548\u5730\u8f6c\u6362\u56de std::string \u3002 \u6700\u4f73\u5b9e\u8df5 \u4f18\u5148\u4f7f\u7528 std:: string_view ( \u6309\u503c\u4f20\u9012 )\u800c\u4e0d\u662f const std:: string& \uff0c\u9664\u975e\u4f60\u9700\u8981\u8c03\u7528\u5176\u4ed6\u8981\u6c42\u4f7f\u7528C\u98ce\u683c\u5b57\u7b26\u4e32\u6216 std::string \u7684\u51fd\u6570\u3002 \u4f5c\u8005\u6ce8 \u540e\u9762\u7684\u4e00\u4e9b\u8bfe\u7a0b\u662f\u5728\u5f15\u5165 std::string_view \u4e4b\u524d\u7f16\u5199\u7684\uff0c\u6240\u4ee5\u4ecd\u7136\u4f7f\u7528\u7684\u662f const std::string& \uff08\u5b9e\u9645\u4e0a\u7528 std::string_view \u66f4\u597d\uff09\uff0c\u6211\u4eec\u6b63\u5728\u7740\u624b\u6e05\u7406\u76f8\u5173\u7684\u4ee3\u7801\u3002","title":"\u901a\u8fc7 const std::string& \u8fd8\u662f std::string_view \u6765\u4f20\u9012\u5b57\u7b26\u4e32\uff1f"},{"location":"11-7-std-string-view-part-2/#_2","text":"std::string_view \u7684\u751f\u547d\u5468\u671f\u662f\u72ec\u7acb\u4e8e\u5b83\u201c\u89c2\u5bdf\u201d\u7684\u5b57\u7b26\u4e32\u7684\uff08\u4e5f\u5c31\u662f\u8bf4\u8be5\u5b57\u7b26\u4e32\u5bf9\u8c61\u53ef\u4ee5\u5728\u5148\u4e8e std::string_view \u88ab\u9500\u6bc1)\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u8bbf\u95ee std::string_view \u5c31\u4f1a\u9020\u6210 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 std::string_view \u8868\u793a\u7684\u5b57\u7b26\u4e32\u5fc5\u987b\u662f\u5728\u5176\u4ed6\u5730\u65b9\u5df2\u7ecf\u88ab\u521b\u5efa\u7684\u5b57\u7b26\u4e32\u3002\u5b83\u53ef\u80fd\u662f\u4e00\u4e2a\u968f\u7740\u7a0b\u5e8f\u542f\u52a8\u505c\u6b62\u800c\u521b\u5efa\u9500\u6bc1\u7684 \u5b57\u9762\u91cf \uff0c\u4e5f\u53ef\u80fd\u662f\u4e00\u4e2a std::string \uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5f53 str::string \u9500\u6bc1\u65f6\u8be5\u5b57\u7b26\u4e32\u4e5f\u5c31\u4e0d\u5b58\u5728\u4e86\u3002 std:: string_view \u81ea\u8eab\u4e0d\u80fd\u521b\u5efa\u4efb\u4f55\u7684\u5b57\u7b26\u4e32\uff0c\u5b83\u53ea\u662f\u89c2\u5bdf\u67d0\u4e2a\u5b57\u7b26\u4e32\u7684\u89c6\u56fe\u3002 \u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\u5c31\u51fa\u73b0\u4e86\u6240\u6709\u6743\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include #include #include std :: string_view askForName () { std :: cout << \"What's your name? \\n \" ; // Use a std::string, because std::cin needs to modify it. std :: string name {}; std :: cin >> name ; // We're switching to std::string_view for demonstrative purposes only. // If you already have a std::string, there's no reason to switch to // a std::string_view. std :: string_view view { name }; std :: cout << \"Hello \" << view << '\\n' ; return view ; } // name \u53d8\u91cf\u88ab\u9500\u6bc1\uff0c\u5bf9\u5e94\u7684\u5b57\u7b26\u4e32\u4e5f\u9500\u6bc1\u4e86 int main () { std :: string_view view { askForName () }; // view \u6b63\u5728\u4e3a\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u5b57\u7b26\u4e32\u63d0\u4f9b\u89c6\u56fe std :: cout << \"Your name is \" << view << '\\n' ; // \u672a\u5b9a\u4e49\u884c\u4e3a return 0 ; } 1 2 3 4 What's your name? nascardriver Hello nascardriver Your name is \ufffdP@\ufffdP@ \u5728 askForName() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u53d8\u91cf name \u5e76\u4e14\u901a\u8fc7 std::cin \u5bf9\u5176\u8fdb\u884c\u4e86\u8d4b\u503c\u3002\u7136\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u53d8\u91cf view \uff0c\u901a\u8fc7\u8fd9\u4e2a\u53d8\u91cf\u6211\u4eec\u53ef\u201c\u89c2\u5bdf\u201d\u4e0a\u8ff0\u5b57\u7b26\u4e32\u3002\u5728\u51fd\u6570\u7ed3\u675f\u65f6\uff0c\u6211\u4eec\u8fd4\u56de view \uff0c\u4f46\u662f\u8be5\u53d8\u91cf\u5b9e\u9645\u89c2\u5bdf\u7684\u5b57\u7b26\u4e32 ( name ) \u5df2\u7ecf\u88ab\u9500\u6bc1\u4e86\uff0c\u56e0\u6b64 view \u73b0\u5728\u6307\u5411\u7684\u662f\u4e00\u5757\u5df2\u7ecf\u88ab\u91ca\u653e\u7684\u5185\u5b58\u3002\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a \u60ac\u5782(dangling) std::string_view . \u5728 mian \u51fd\u6570\u4e2d\u8bbf\u95ee\u88ab\u8fd4\u56de\u7684 std::string_view \u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\uff0c\u4f1a\u6253\u5370\u5947\u602a\u7684\u5b57\u7b26\u3002 \u95ee\u9898\u53d1\u751f\u7684\u539f\u56e0\u5728\u4e8e\u4f60\u901a\u8fc7 std::string \u521b\u5efa\u4e86\u4e00\u4e2a std::string_view \uff0c\u7136\u540e\u53c8\u4fee\u6539\u4e86 std::string \u3002\u4fee\u6539 std::string \u4f1a\u5bfc\u81f4\u5185\u90e8\u5b57\u7b26\u4e32\u88ab\u9500\u6bc1\u6216\u88ab\u66ff\u6362\uff08\u5728\u4ee3\u7801\u7684\u5176\u4ed6\u90e8\u5206\uff09\u3002 std::string_view \u603b\u662f\u6307\u5411\u8be5\u5b57\u7b26\u4e32\uff0c \u4f46\u8be5\u5b57\u7b26\u4e32\u53ef\u80fd\u5df2\u7ecf\u4e0d\u662f\u5b83\u539f\u6765\u7684\u6a21\u6837\u6216\u8005\u5df2\u7ecf\u4e0d\u5b58\u5728\u4e86\u3002 \u6ce8\u610f \u786e\u4fdd std::string_view \u89c2\u5bdf\u7684\u5b57\u7b26\u4e32\u4e0d\u4f1a \u79bb\u5f00\u4f5c\u7528\u57df \uff0c\u4e5f\u4e0d\u4f1a\u88ab\u4fee\u6539\u3002","title":"\u6240\u6709\u6743\u95ee\u9898"},{"location":"11-7-std-string-view-part-2/#data","text":"std::string_view \u89c2\u5bdf\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u901a\u8fc7 data() \u51fd\u6570\u6765\u8bbf\u95ee\uff0c\u5b83\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u4e00\u4e2aC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u5f0f\u63d0\u4f9b\u4e86\u4e00\u79cd\u5feb\u901f\u8bbf\u95ee\u88ab\u89c2\u5bdf\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3002\u4f46\u662f\u5b83\u53ea\u80fd\u5728 std::string_view \u89c6\u56fe\u6ca1\u6709\u88ab\u4fee\u6539\u7684\u65f6\u5019\u4f7f\u7528(\u4f8b\u5982\u901a\u8fc7 remove_prefix \u6216 remove_suffix \u4fee\u6539\u89c6\u56fe)\uff0c\uff0c\u8be5\u5b57\u7b26\u4e32\u662f\u6709\u7a7a\u7ed3\u675f\u7b26\u7684\u3002 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c std::strlen \u51fd\u6570\u4e0d\u80fd\u8bc6\u522b std:: string_view \uff0c\u56e0\u6b64\u6211\u4eec\u5fc5\u987b\u4f20\u5165 str.data() \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include // For std::strlen #include #include int main () { std :: string_view str { \"balloon\" }; std :: cout << str << '\\n' ; // We use std::strlen because it's simple, this could be any other function // that needs a null-terminated string. // It's okay to use data() because we haven't modified the view, and the // string is null-terminated. std :: cout << std :: strlen ( str . data ()) << '\\n' ; return 0 ; } 1 2 balloon 7 \u5f53 std:: string_view \u88ab\u4fee\u6539\u540e\uff0c data() \u5c31\u4e0d\u80fd\u5b8c\u6210\u6211\u4eec\u671f\u671b\u7684\u4efb\u52a1\u4e86\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5f53\u89c6\u56fe\u88ab\u4fee\u6539\u540e data() \u7684\u884c\u4e3a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include #include int main () { std :: string_view str { \"balloon\" }; // Remove the \"b\" str . remove_prefix ( 1 ); // remove the \"oon\" str . remove_suffix ( 3 ); // Remember that the above doesn't modify the string, it only changes // the region that str is observing. std :: cout << str << \" has \" << std :: strlen ( str . data ()) << \" letter(s) \\n \" ; std :: cout << \"str.data() is \" << str . data () << '\\n' ; std :: cout << \"str is \" << str << '\\n' ; return 0 ; } 1 2 3 all has 6 letter(s) str.data() is alloon str is all \u8fd9\u663e\u7136\u4e0d\u662f\u6211\u4eec\u671f\u671b\u7684\uff0c\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528 data() \u6765\u8bbf\u95ee\u4e00\u4e2a\u5df2\u7ecf\u88ab\u4fee\u6539\u7684 std::string_view \u3002\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u4fe1\u606f\u5728\u6211\u4eec\u4f7f\u7528 data() \u65f6\u5c31\u4e22\u5931\u4e86\uff0c std:: strlen \u548c std:: cout \u4f1a\u6301\u7eed\u8bfb\u53d6\u76f4\u5230\u9047\u5230\u7b2c\u4e00\u4e2a\u7a7a\u7ed3\u675f\u7b26\uff0c\u4e5f\u5c31\u662f\u201cballoon\u201d\u7684\u672b\u5c3e\u3002 \u6ce8\u610f \u53ea\u6709\u5728 std::string_view \u6ca1\u6709\u88ab\u4fee\u6539\u7684\u60c5\u51b5\u4e0b\u4e14\u5b57\u7b26\u4e32\u4ee5\u7a7a\u7ed3\u675f\u7b26\u7ed3\u5c3e\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528 std:: string_view:: data() \u5982\u679c\u5b57\u7b26\u4e32\u6ca1\u6709\u4ee5\u7a7a\u7ed3\u675f\u7b26\u7ed3\u5c3e\uff0c\u5219 std:: string_view:: data() \u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u3002","title":"\u4f7f\u7528data()\u51fd\u6570\u6765\u6253\u5f00\u201d\u7a97\u6237\u201c"},{"location":"11-7-std-string-view-part-2/#_3","text":"\u4f5c\u4e3a\u4e00\u4e2a\u76f8\u5bf9\u8f83\u65b0\u7684\u7279\u6027\u6765\u8bf4\uff0c std::string_view \u7684\u5b9e\u73b0\u5b8c\u6210\u5ea6\u5e76\u4e0d\u5982\u9884\u671f\u3002 ```cpp hl_lines=\"5,6\" std::string s{ \"hello\" }; std::string_view v{ \"world\" }; // \u4e0d\u80fd\u5de5\u4f5c std::cout << (s + v) << '\\n'; std::cout << (v + s) << '\\n'; // \u53ef\u80fd\u4e0d\u5b89\u5168\uff0c\u4e5f\u53ef\u80fd\u5e76\u4e0d\u662f\u6211\u4eec\u5e0c\u671b\u7684\u505a\u6cd5\uff0c\u56e0\u4e3a\u6211\u4eec\u6b64\u65f6\u5c06std::string_view\u5f53\u505a // C\u98ce\u683c\u5b57\u7b26\u4e32\u6765\u4f7f\u7528 std::cout << (s + v.data()) << '\\n'; std::cout << (v.data() + s) << '\\n'; // \u53ef\u4ee5, \u53ef\u4ee5\u4f46\u662f\u4e0d\u4f18\u96c5\uff0c\u800c\u4e14\u6d6a\u8d39\u8d44\u6e90\uff0c\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u6784\u5efa\u4e00\u4e2a\u65b0\u7684 std::string. std::cout << (s + std::string{ v }) << '\\n'; std::cout << (std::string{ v } + s) << '\\n'; std::cout << (s + static_cast (v)) << '\\n'; std::cout << (static_cast (v) + s) << '\\n'; ``` \u6ca1\u9053\u7406\u4e0d\u80fd\u4f7f\u7528\u7b2c\u4e94\u884c\u548c\u7b2c\u516d\u884c\u7684\u65b9\u5f0f\uff0c\u8fd9\u4e2a\u7279\u6027\u53ef\u80fd\u4f1a\u5728\u672a\u6765\u7684C++\u7248\u672c\u4e2d\u88ab\u652f\u6301\u3002","title":"\u4e0d\u5b8c\u6574\u7684\u5b9e\u73b0"},{"location":"11-8-Pointers-and-arrays/","tags":["array","pointer"],"text":"Key Takeaway \u5728\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u6570\u7ec4\u65f6\uff0c\u6570\u7ec4\u4f1a \u9000\u5316 \uff08\u9690\u5f0f\u8f6c\u6362\uff09\u4e3a\u4e00\u4e2a\u6307\u9488 \u5f53\u5b9a\u4e49\u51fd\u6570\u7684\u53c2\u6570\u65f6\uff0c\u6700\u597d\u4f7f\u7528\u6307\u9488\u8bed\u6cd5( * )\u800c\u4e0d\u662f\u6570\u7ec4\u8bed\u6cd5( [] )\u3002 \u6570\u7ec4\u9000\u5316\u4e3a\u6307\u9488\u4f20\u9012\u5230\u51fd\u6570\u540e\uff0c\u4fee\u6539\u8be5\u6307\u9488\u4f1a\u4fee\u6539\u539f\u6570\u7ec4 \u4f5c\u4e3a\u7ed3\u6784\u6216\u7c7b\u7684\u4e00\u90e8\u5206\u7684\u6570\u7ec4\u4e0d\u4f1a\u5728\u5c06\u6574\u4e2a\u7ed3\u6784\u6216\u7c7b\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\u53d1\u751f\u9000\u5316\u3002\u5982\u679c\u6709\u9700\u8981\uff0c\u53ef\u4ee5\u7528\u8fd9\u79cd\u65b9\u5f0f\u9632\u6b62\u6570\u7ec4\u9000\u5316\u4e3a\u6307\u9488\u3002 \u5728 C++ \u4e2d\uff0c\u6307\u9488\u548c\u6570\u7ec4\u672c\u8d28\u4e0a\u662f\u76f8\u4e92\u8054\u7cfb\u7684\u3002 \u6570\u7ec4\u9000\u5316 \u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\uff1a 1 int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; // declare a fixed array of 5 integers \u5bf9\u4e8e\u6211\u4eec\u6765\u8bf4\uff0c\u8fd9\u662f\u4e00\u4e2a\u5305\u542b\u4e94\u4e2a\u6574\u6570\u7684\u6570\u7ec4\uff0c\u4f46\u662f\u5bf9\u4e8e\u7f16\u8bd1\u5668\u6765\u8bf4\uff0c\u5b83\u662f\u4e00\u4e2a int[5] \u7c7b\u578b\u7684\u6570\u7ec4\u3002\u6211\u4eec\u53ef\u4ee5\u77e5\u9053 array[0] , array[1] , array[2] , array[3] , \u548c array[4] \u7684\u503c (\u5206\u522b\u4e3a 9\u30017\u30015\u30013 \u548c 1 )\u3002 \u5728\u51e0\u4e4e\u6240\u6709\u60c5\u51b5\u4e0b\uff08\u4e24\u79cd\u4f8b\u5916\u60c5\u51b5\u7a0d\u540e\u4ecb\u7ecd\uff09\uff0c\u5f53\u6211\u4eec\u5728\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u6570\u7ec4\u65f6\uff0c\u6570\u7ec4\u4f1a \u9000\u5316 \uff08\u9690\u5f0f\u8f6c\u6362\uff09\u4e3a\u4e00\u4e2a\u6307\u9488\u3002\u53c2\u8003\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; // \u6253\u5370\u6570\u7ec4\u7684\u9996\u5730\u5740 std :: cout << \"Element 0 has address: \" << & array [ 0 ] << '\\n' ; // \u6253\u5370\u6570\u7ec4\u9000\u5316\u540e\u7684\u6307\u9488 std :: cout << \"The array decays to a pointer holding address: \" << array << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u5185\u5bb9\uff1a 1 2 Element 0 has address: 0042FD5C The array decays to a pointer holding address: 0042FD5C \u8ba4\u4e3a\u6570\u7ec4\u548c\u6307\u5411\u6570\u7ec4\u7684\u6307\u9488\u5b8c\u5168\u4e00\u6837\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9519\u8bef\u3002\u5b83\u4eec\u5e76\u4e0d\u662f\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6570\u7ec4\u7684\u7c7b\u578b\u662f\u201c int[5] \u201d\u5b83\u7684\u503c\u5c31\u662f\u6570\u7ec4\u7684\u5404\u4e2a\u5143\u7d20\u3002\u800c\u6307\u5411\u6570\u7ec4\u7684\u6307\u9488\uff0c\u5176\u7c7b\u578b\u4e3a\u201c int* \u201d\uff0c\u5176\u503c\u4e3a\u6570\u7ec4\u9996\u4e2a\u5143\u7d20\u7684\u5730\u5740\u3002 \u7a0d\u540e\u6211\u4eec\u5c31\u4f1a\u4ecb\u7ecd\u5b83\u4eec\u7684\u5177\u4f53\u533a\u522b\u4f53\u73b0\u5728\u54ea\u91cc\u3002 \u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4e5f\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u6765\u8bbf\u95ee\uff08\u4e0b\u8282\u8bfe\u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\uff09\uff0c\u4f46\u662f\u6211\u4eec\u4e0d\u80fd\u901a\u8fc7\u6307\u9488\u6765\u83b7\u53d6\u6570\u7ec4\u7684\u7c7b\u578b\u4fe1\u606f\u3002 \u7136\u800c\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u8fd8\u662f\u53ef\u4ee5\u4ee5\u76f8\u540c\u7684\u65b9\u5f0f\u5bf9\u5f85\u56fa\u5b9a\u6570\u7ec4\u548c\u6307\u9488\u3002 \u4f8b\u5982\uff0c\u53ef\u4ee5\u5bf9\u6570\u7ec4\u89e3\u5f15\u7528\u4ee5\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u503c: 1 2 3 4 5 6 7 int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; // Deferencing an array returns the first element (element 0) std :: cout << * array ; // will print 9! char name []{ \"Jason\" }; // C-style string (also an array) std :: cout << * name << '\\n' ; // will print 'J' \u6ce8\u610f\uff0c\u6211\u4eec\u6ca1\u6709\u5bf9\u6570\u7ec4\u672c\u8eab\u8fdb\u884c\u89e3\u5f15\u7528\u3002\u6570\u7ec4\u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a\u4e86\u6307\u9488( int* \u7c7b\u578b)\uff0c\u5e76\u5bf9\u6307\u9488\u89e3\u5f15\u7528\uff0c\u4ee5\u83b7\u5f97\u6307\u9488\u6240\u4fdd\u5b58\u7684\u5185\u5b58\u5730\u5740\u7684\u503c(\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u503c)\u3002 \u6211\u4eec\u8fd8\u53ef\u4ee5\u8ba9\u4e00\u4e2a\u6307\u9488\u6307\u5411\u6570\u7ec4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; std :: cout << * array << '\\n' ; // will print 9 int * ptr { array }; std :: cout << * ptr << '\\n' ; // will print 9 return 0 ; } \u6570\u7ec4\u9000\u5316\u4e3a\u4e86 int* \u7c7b\u578b\u7684\u6307\u9488\uff0c\u800c ptr \u4e5f\u662f\u6b64\u7c7b\u578b\u7684\u6307\u9488\u3002 \u6307\u9488\u548c\u56fa\u5b9a\u6570\u7ec4\u7684\u5dee\u5f02 \u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u56fa\u5b9a\u6570\u7ec4\u548c\u6307\u9488\u4e4b\u95f4\u7684\u7c7b\u578b\u5dee\u5f02\u4f1a\u5e26\u6765\u95ee\u9898\u3002\u8fd9\u4e9b\u95ee\u9898\u6709\u52a9\u4e8e\u6211\u4eec\u89e3\u91ca\u56fa\u5b9a\u6570\u7ec4\u548c\u6307\u9488\u7684\u4e0d\u540c\u3002 \u4e3b\u8981\u7684\u5dee\u5f02\u53d1\u751f\u5728\u4f7f\u7528 sizeof() \u64cd\u4f5c\u7b26\u65f6\u3002\u5f53\u7528\u4e8e\u56fa\u5b9a\u6570\u7ec4\u65f6\uff0c sizeof \u8fd4\u56de\u6574\u4e2a\u6570\u7ec4\u7684\u5927\u5c0f( array length * element size )\u3002\u5f53\u7528\u4e8e\u6307\u9488\u65f6\uff0c sizeof \u8fd4\u56de\u6307\u9488\u7684\u5927\u5c0f(\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d)\u3002\u4e0b\u9762\u7684\u7a0b\u5e8f\u8bf4\u660e\u4e86\u8fd9\u4e00\u70b9: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; std :: cout << sizeof ( array ) << '\\n' ; // will print sizeof(int) * array length int * ptr { array }; std :: cout << sizeof ( ptr ) << '\\n' ; // will print the size of a pointer return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 2 20 4 \u56fa\u5b9a\u6570\u7ec4\u77e5\u9053\u5b83\u6240\u6307\u5411\u7684\u6570\u7ec4\u6709\u591a\u957f\uff0c\u800c\u6307\u5411\u6570\u7ec4\u7684\u6307\u9488\u5e76\u4e0d\u77e5\u9053\u6570\u7ec4\u6709\u591a\u957f\u3002 \u7b2c\u4e8c\u4e2a\u533a\u522b\u53d1\u751f\u5728\u4f7f\u7528\u53d6\u5730\u5740\u64cd\u4f5c\u7b26( & )\u65f6\u3002\u5bf9\u6307\u9488\u4f7f\u7528\u53d6\u5730\u5740\u64cd\u4f5c\u7b26\uff0c\u4f1a\u5f97\u5230\u6307\u9488\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u3002\u5bf9\u6570\u7ec4\u4f7f\u7528\u53d6\u5730\u5740\u64cd\u4f5c\u7b26\uff0c\u5c06\u8fd4\u56de\u6307\u5411\u5730\u5740\u7684\u6307\u9488\uff0c\u8fd9\u4e2a\u6307\u9488\u6307\u5411\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u4f46\u662f\u7c7b\u578b\u4fe1\u606f\u4e0d\u540c(\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c &array \u7684\u7c7b\u578b\u662f int(*)[5] )\u3002\u4f60\u4e0d\u592a\u53ef\u80fd\u4f1a\u7528\u5230\u5b83\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; std :: cout << array << '\\n' ; // \u7c7b\u578b\u4e3a int[5], \u6253\u5370 009DF9D4 std :: cout << & array << '\\n' ; // \u7c7b\u578b\u4e3a int(*)[5], \u6253\u5370 009DF9D4 std :: cout << '\\n' ; int * ptr { array }; std :: cout << ptr << '\\n' ; // \u7c7b\u578b\u4e3a int*, \u6253\u5370 009DF9D4 std :: cout << & ptr << '\\n' ; // \u7c7b\u578b\u4e3a int**, \u6253\u5370 009DF9C8 return 0 ; } // h/t to reader PacMan for this example \u5c06\u56fa\u5b9a\u6570\u7ec4\u4f20\u9012\u7ed9\u51fd\u6570 \u5728 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u62f7\u8d1d\u5927\u578b\u6570\u7ec4\u7684\u5f00\u9500\u662f\u5f88\u5927\u7684\uff0cC++\u5728\u5c06\u6570\u7ec4\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\uff0c\u5e76\u4e0d\u4f1a\u5c06\u5176\u62f7\u8d1d\u4e00\u4efd\u3002\u5b9e\u9645\u4e0a\uff0c\u56fa\u5b9a\u6570\u7ec4\u5728\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\u4e5f\u4f1a\u9000\u5316\u4e3a\u6307\u9488\uff0c\u8be5\u6307\u9488\u4f1a\u88ab\u4f20\u9012\u7ed9\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void printSize ( int * array ) { // array is treated as a pointer here std :: cout << sizeof ( array ) << '\\n' ; // prints the size of a pointer, not the size of the array! } int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << sizeof ( array ) << '\\n' ; // will print sizeof(int) * array length printSize ( array ); // the array argument decays into a pointer here return 0 ; } 1 2 32 4 \u6ce8\u610f\uff0c\u5373\u4f7f\u5f62\u53c2\u58f0\u660e\u4e3a\u56fa\u5b9a\u6570\u7ec4\uff0c\u4e5f\u4f1a\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include // C++ will implicitly convert parameter array[] to *array void printSize ( int array []) { // array is treated as a pointer here, not a fixed array std :: cout << sizeof ( array ) << '\\n' ; // prints the size of a pointer, not the size of the array! } int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << sizeof ( array ) << '\\n' ; // will print sizeof(int) * array length printSize ( array ); // the array argument decays into a pointer here return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 32 4 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cC++ \u9690\u5f0f\u5730\u5c06\u4f7f\u7528\u6570\u7ec4\u8bed\u6cd5( [] )\u7684\u53c2\u6570\u8f6c\u6362\u4e3a\u6307\u9488\u8bed\u6cd5( * )\u3002\u8fd9\u610f\u5473\u7740\u4e0b\u9762\u4e24\u4e2a\u51fd\u6570\u7684\u58f0\u660e\u662f\u76f8\u540c\u7684\uff1a 1 2 void printSize ( int array []); void printSize ( int * array ); \u6709\u4e9b\u7a0b\u5e8f\u5458\u66f4\u559c\u6b22\u4f7f\u7528 [] \u8bed\u6cd5\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u6e05\u695a\u5730\u8868\u660e\u51fd\u6570\u9700\u8981\u4e00\u4e2a\u6570\u7ec4\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u6307\u9488\u3002\u7136\u800c\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7531\u4e8e\u6307\u9488\u5e76\u4e0d\u77e5\u9053\u6570\u7ec4\u6709\u591a\u5927\uff0c\u6240\u4ee5\u65e0\u8bba\u5982\u4f55\u90fd\u9700\u8981\u5c06\u6570\u7ec4\u5927\u5c0f\u4f5c\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u53c2\u6570\u6765\u4f20\u9012(\u5b57\u7b26\u4e32\u662f\u4e00\u4e2a\u7279\u4f8b\uff0c\u56e0\u4e3a\u5b83\u4eec\u4ee5 null \u7ed3\u5c3e)\u3002 \u6211\u4eec\u5efa\u8bae\u4f7f\u7528\u6307\u9488\u8bed\u6cd5\uff0c\u56e0\u4e3a\u5b83\u8868\u660e\u5f62\u53c2\u88ab\u89c6\u4e3a\u6307\u9488\uff0c\u800c\u4e0d\u662f\u56fa\u5b9a\u7684\u6570\u7ec4\u3002\u6b64\u5916\uff0c\u67d0\u4e9b\u64cd\u4f5c\uff08\u5982 sizeof() \uff09\u4f1a\u5c06\u8be5\u5f62\u53c2\u4f5c\u4e3a\u6307\u9488\u8fdb\u884c\u64cd\u4f5c\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u5b9a\u4e49\u51fd\u6570\u7684\u53c2\u6570\u65f6\uff0c\u6700\u597d\u4f7f\u7528\u6307\u9488\u8bed\u6cd5( * )\u800c\u4e0d\u662f\u6570\u7ec4\u8bed\u6cd5( [] )\u3002 \u4f20\u5730\u5740 \u6570\u7ec4\u5728\u4f20\u9012\u7ed9\u51fd\u6570\u65f6 \u9000\u5316 \u4e3a\u6307\u9488\u7684\u4e8b\u5b9e\uff0c\u89e3\u91ca\u4e86\u4e3a\u4ec0\u4e48\u5728\u51fd\u6570\u4e2d\u66f4\u6539\u6570\u7ec4\u4f1a\u66f4\u6539\u4f20\u5165\u7684\u5b9e\u9645\u6570\u7ec4\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include // parameter ptr contains a copy of the array's address void changeArray ( int * ptr ) { * ptr = 5 ; // so changing an array element changes the _actual_ array } int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << \"Element 0 has value: \" << array [ 0 ] << '\\n' ; changeArray ( array ); std :: cout << \"Element 0 has value: \" << array [ 0 ] << '\\n' ; return 0 ; } 1 2 Element 0 has value: 1 Element 0 has value: 5 \u5f53 changeArray() \u88ab\u8c03\u7528\u65f6\uff0c\u6570\u7ec4\u9000\u5316\u4e3a\u4e00\u4e2a\u6307\u9488\uff0c\u8be5\u6307\u9488\u7684\u503c(\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740)\u88ab\u590d\u5236\u5230 changeArray() \u51fd\u6570\u7684 ptr \u5f62\u53c2\u4e2d\u3002\u867d\u7136 ptr \u4e2d\u7684\u503c\u662f\u6570\u7ec4\u5730\u5740\u7684\u526f\u672c\uff0c\u4f46 ptr \u4ecd\u7136\u6307\u5411\u5b9e\u9645\u7684\u6570\u7ec4(\u4e0d\u662f\u526f\u672c!)\u56e0\u6b64\uff0c\u5f53\u5bf9 ptr \u89e3\u5f15\u7528\u65f6\uff0c\u8bbf\u95ee\u7684\u5143\u7d20\u5b9e\u9645\u4e0a\u662f\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20! \u7cbe\u660e\u7684\u8bfb\u8005\u4f1a\u6ce8\u610f\u5230\u8fd9\u79cd\u73b0\u8c61\u4e5f\u9002\u7528\u4e8e\u6307\u5411\u975e\u6570\u7ec4\u503c\u7684\u6307\u9488\u3002 \u7ed3\u6784\u4f53\u548c\u7c7b\u4e2d\u7684\u6570\u7ec4\u4e0d\u4f1a\u9000\u5316 \u6700\u540e\uff0c\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u4f5c\u4e3a\u7ed3\u6784\u6216\u7c7b\u7684\u4e00\u90e8\u5206\u7684\u6570\u7ec4\u4e0d\u4f1a\u5728\u5c06\u6574\u4e2a\u7ed3\u6784\u6216\u7c7b\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\u53d1\u751f\u9000\u5316\u3002\u5982\u679c\u6709\u9700\u8981\uff0c\u53ef\u4ee5\u7528\u8fd9\u79cd\u65b9\u5f0f\u9632\u6b62\u6570\u7ec4\u9000\u5316\u4e3a\u6307\u9488\u3002\u5728\u7f16\u5199\u5229\u7528\u6570\u7ec4\u7684\u7c7b\u65f6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u662f\u6709\u7528\u7684 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u6307\u9488\u7b97\u672f\uff0c\u5e76\u8ba8\u8bba\u6570\u7ec4\u7d22\u5f15\u7684\u5de5\u4f5c\u539f\u7406\u3002","title":"11.8 - \u6307\u9488\u548c\u6570\u7ec4"},{"location":"11-8-Pointers-and-arrays/#_1","text":"\u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\uff1a 1 int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; // declare a fixed array of 5 integers \u5bf9\u4e8e\u6211\u4eec\u6765\u8bf4\uff0c\u8fd9\u662f\u4e00\u4e2a\u5305\u542b\u4e94\u4e2a\u6574\u6570\u7684\u6570\u7ec4\uff0c\u4f46\u662f\u5bf9\u4e8e\u7f16\u8bd1\u5668\u6765\u8bf4\uff0c\u5b83\u662f\u4e00\u4e2a int[5] \u7c7b\u578b\u7684\u6570\u7ec4\u3002\u6211\u4eec\u53ef\u4ee5\u77e5\u9053 array[0] , array[1] , array[2] , array[3] , \u548c array[4] \u7684\u503c (\u5206\u522b\u4e3a 9\u30017\u30015\u30013 \u548c 1 )\u3002 \u5728\u51e0\u4e4e\u6240\u6709\u60c5\u51b5\u4e0b\uff08\u4e24\u79cd\u4f8b\u5916\u60c5\u51b5\u7a0d\u540e\u4ecb\u7ecd\uff09\uff0c\u5f53\u6211\u4eec\u5728\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u6570\u7ec4\u65f6\uff0c\u6570\u7ec4\u4f1a \u9000\u5316 \uff08\u9690\u5f0f\u8f6c\u6362\uff09\u4e3a\u4e00\u4e2a\u6307\u9488\u3002\u53c2\u8003\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; // \u6253\u5370\u6570\u7ec4\u7684\u9996\u5730\u5740 std :: cout << \"Element 0 has address: \" << & array [ 0 ] << '\\n' ; // \u6253\u5370\u6570\u7ec4\u9000\u5316\u540e\u7684\u6307\u9488 std :: cout << \"The array decays to a pointer holding address: \" << array << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u5185\u5bb9\uff1a 1 2 Element 0 has address: 0042FD5C The array decays to a pointer holding address: 0042FD5C \u8ba4\u4e3a\u6570\u7ec4\u548c\u6307\u5411\u6570\u7ec4\u7684\u6307\u9488\u5b8c\u5168\u4e00\u6837\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9519\u8bef\u3002\u5b83\u4eec\u5e76\u4e0d\u662f\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6570\u7ec4\u7684\u7c7b\u578b\u662f\u201c int[5] \u201d\u5b83\u7684\u503c\u5c31\u662f\u6570\u7ec4\u7684\u5404\u4e2a\u5143\u7d20\u3002\u800c\u6307\u5411\u6570\u7ec4\u7684\u6307\u9488\uff0c\u5176\u7c7b\u578b\u4e3a\u201c int* \u201d\uff0c\u5176\u503c\u4e3a\u6570\u7ec4\u9996\u4e2a\u5143\u7d20\u7684\u5730\u5740\u3002 \u7a0d\u540e\u6211\u4eec\u5c31\u4f1a\u4ecb\u7ecd\u5b83\u4eec\u7684\u5177\u4f53\u533a\u522b\u4f53\u73b0\u5728\u54ea\u91cc\u3002 \u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4e5f\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u6765\u8bbf\u95ee\uff08\u4e0b\u8282\u8bfe\u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\uff09\uff0c\u4f46\u662f\u6211\u4eec\u4e0d\u80fd\u901a\u8fc7\u6307\u9488\u6765\u83b7\u53d6\u6570\u7ec4\u7684\u7c7b\u578b\u4fe1\u606f\u3002 \u7136\u800c\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u8fd8\u662f\u53ef\u4ee5\u4ee5\u76f8\u540c\u7684\u65b9\u5f0f\u5bf9\u5f85\u56fa\u5b9a\u6570\u7ec4\u548c\u6307\u9488\u3002 \u4f8b\u5982\uff0c\u53ef\u4ee5\u5bf9\u6570\u7ec4\u89e3\u5f15\u7528\u4ee5\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u503c: 1 2 3 4 5 6 7 int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; // Deferencing an array returns the first element (element 0) std :: cout << * array ; // will print 9! char name []{ \"Jason\" }; // C-style string (also an array) std :: cout << * name << '\\n' ; // will print 'J' \u6ce8\u610f\uff0c\u6211\u4eec\u6ca1\u6709\u5bf9\u6570\u7ec4\u672c\u8eab\u8fdb\u884c\u89e3\u5f15\u7528\u3002\u6570\u7ec4\u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a\u4e86\u6307\u9488( int* \u7c7b\u578b)\uff0c\u5e76\u5bf9\u6307\u9488\u89e3\u5f15\u7528\uff0c\u4ee5\u83b7\u5f97\u6307\u9488\u6240\u4fdd\u5b58\u7684\u5185\u5b58\u5730\u5740\u7684\u503c(\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u503c)\u3002 \u6211\u4eec\u8fd8\u53ef\u4ee5\u8ba9\u4e00\u4e2a\u6307\u9488\u6307\u5411\u6570\u7ec4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; std :: cout << * array << '\\n' ; // will print 9 int * ptr { array }; std :: cout << * ptr << '\\n' ; // will print 9 return 0 ; } \u6570\u7ec4\u9000\u5316\u4e3a\u4e86 int* \u7c7b\u578b\u7684\u6307\u9488\uff0c\u800c ptr \u4e5f\u662f\u6b64\u7c7b\u578b\u7684\u6307\u9488\u3002","title":"\u6570\u7ec4\u9000\u5316"},{"location":"11-8-Pointers-and-arrays/#_2","text":"\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u56fa\u5b9a\u6570\u7ec4\u548c\u6307\u9488\u4e4b\u95f4\u7684\u7c7b\u578b\u5dee\u5f02\u4f1a\u5e26\u6765\u95ee\u9898\u3002\u8fd9\u4e9b\u95ee\u9898\u6709\u52a9\u4e8e\u6211\u4eec\u89e3\u91ca\u56fa\u5b9a\u6570\u7ec4\u548c\u6307\u9488\u7684\u4e0d\u540c\u3002 \u4e3b\u8981\u7684\u5dee\u5f02\u53d1\u751f\u5728\u4f7f\u7528 sizeof() \u64cd\u4f5c\u7b26\u65f6\u3002\u5f53\u7528\u4e8e\u56fa\u5b9a\u6570\u7ec4\u65f6\uff0c sizeof \u8fd4\u56de\u6574\u4e2a\u6570\u7ec4\u7684\u5927\u5c0f( array length * element size )\u3002\u5f53\u7528\u4e8e\u6307\u9488\u65f6\uff0c sizeof \u8fd4\u56de\u6307\u9488\u7684\u5927\u5c0f(\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d)\u3002\u4e0b\u9762\u7684\u7a0b\u5e8f\u8bf4\u660e\u4e86\u8fd9\u4e00\u70b9: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; std :: cout << sizeof ( array ) << '\\n' ; // will print sizeof(int) * array length int * ptr { array }; std :: cout << sizeof ( ptr ) << '\\n' ; // will print the size of a pointer return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 2 20 4 \u56fa\u5b9a\u6570\u7ec4\u77e5\u9053\u5b83\u6240\u6307\u5411\u7684\u6570\u7ec4\u6709\u591a\u957f\uff0c\u800c\u6307\u5411\u6570\u7ec4\u7684\u6307\u9488\u5e76\u4e0d\u77e5\u9053\u6570\u7ec4\u6709\u591a\u957f\u3002 \u7b2c\u4e8c\u4e2a\u533a\u522b\u53d1\u751f\u5728\u4f7f\u7528\u53d6\u5730\u5740\u64cd\u4f5c\u7b26( & )\u65f6\u3002\u5bf9\u6307\u9488\u4f7f\u7528\u53d6\u5730\u5740\u64cd\u4f5c\u7b26\uff0c\u4f1a\u5f97\u5230\u6307\u9488\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u3002\u5bf9\u6570\u7ec4\u4f7f\u7528\u53d6\u5730\u5740\u64cd\u4f5c\u7b26\uff0c\u5c06\u8fd4\u56de\u6307\u5411\u5730\u5740\u7684\u6307\u9488\uff0c\u8fd9\u4e2a\u6307\u9488\u6307\u5411\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u4f46\u662f\u7c7b\u578b\u4fe1\u606f\u4e0d\u540c(\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c &array \u7684\u7c7b\u578b\u662f int(*)[5] )\u3002\u4f60\u4e0d\u592a\u53ef\u80fd\u4f1a\u7528\u5230\u5b83\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; std :: cout << array << '\\n' ; // \u7c7b\u578b\u4e3a int[5], \u6253\u5370 009DF9D4 std :: cout << & array << '\\n' ; // \u7c7b\u578b\u4e3a int(*)[5], \u6253\u5370 009DF9D4 std :: cout << '\\n' ; int * ptr { array }; std :: cout << ptr << '\\n' ; // \u7c7b\u578b\u4e3a int*, \u6253\u5370 009DF9D4 std :: cout << & ptr << '\\n' ; // \u7c7b\u578b\u4e3a int**, \u6253\u5370 009DF9C8 return 0 ; } // h/t to reader PacMan for this example","title":"\u6307\u9488\u548c\u56fa\u5b9a\u6570\u7ec4\u7684\u5dee\u5f02"},{"location":"11-8-Pointers-and-arrays/#_3","text":"\u5728 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u62f7\u8d1d\u5927\u578b\u6570\u7ec4\u7684\u5f00\u9500\u662f\u5f88\u5927\u7684\uff0cC++\u5728\u5c06\u6570\u7ec4\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\uff0c\u5e76\u4e0d\u4f1a\u5c06\u5176\u62f7\u8d1d\u4e00\u4efd\u3002\u5b9e\u9645\u4e0a\uff0c\u56fa\u5b9a\u6570\u7ec4\u5728\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\u4e5f\u4f1a\u9000\u5316\u4e3a\u6307\u9488\uff0c\u8be5\u6307\u9488\u4f1a\u88ab\u4f20\u9012\u7ed9\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void printSize ( int * array ) { // array is treated as a pointer here std :: cout << sizeof ( array ) << '\\n' ; // prints the size of a pointer, not the size of the array! } int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << sizeof ( array ) << '\\n' ; // will print sizeof(int) * array length printSize ( array ); // the array argument decays into a pointer here return 0 ; } 1 2 32 4 \u6ce8\u610f\uff0c\u5373\u4f7f\u5f62\u53c2\u58f0\u660e\u4e3a\u56fa\u5b9a\u6570\u7ec4\uff0c\u4e5f\u4f1a\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include // C++ will implicitly convert parameter array[] to *array void printSize ( int array []) { // array is treated as a pointer here, not a fixed array std :: cout << sizeof ( array ) << '\\n' ; // prints the size of a pointer, not the size of the array! } int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << sizeof ( array ) << '\\n' ; // will print sizeof(int) * array length printSize ( array ); // the array argument decays into a pointer here return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 32 4 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cC++ \u9690\u5f0f\u5730\u5c06\u4f7f\u7528\u6570\u7ec4\u8bed\u6cd5( [] )\u7684\u53c2\u6570\u8f6c\u6362\u4e3a\u6307\u9488\u8bed\u6cd5( * )\u3002\u8fd9\u610f\u5473\u7740\u4e0b\u9762\u4e24\u4e2a\u51fd\u6570\u7684\u58f0\u660e\u662f\u76f8\u540c\u7684\uff1a 1 2 void printSize ( int array []); void printSize ( int * array ); \u6709\u4e9b\u7a0b\u5e8f\u5458\u66f4\u559c\u6b22\u4f7f\u7528 [] \u8bed\u6cd5\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u6e05\u695a\u5730\u8868\u660e\u51fd\u6570\u9700\u8981\u4e00\u4e2a\u6570\u7ec4\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u6307\u9488\u3002\u7136\u800c\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7531\u4e8e\u6307\u9488\u5e76\u4e0d\u77e5\u9053\u6570\u7ec4\u6709\u591a\u5927\uff0c\u6240\u4ee5\u65e0\u8bba\u5982\u4f55\u90fd\u9700\u8981\u5c06\u6570\u7ec4\u5927\u5c0f\u4f5c\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u53c2\u6570\u6765\u4f20\u9012(\u5b57\u7b26\u4e32\u662f\u4e00\u4e2a\u7279\u4f8b\uff0c\u56e0\u4e3a\u5b83\u4eec\u4ee5 null \u7ed3\u5c3e)\u3002 \u6211\u4eec\u5efa\u8bae\u4f7f\u7528\u6307\u9488\u8bed\u6cd5\uff0c\u56e0\u4e3a\u5b83\u8868\u660e\u5f62\u53c2\u88ab\u89c6\u4e3a\u6307\u9488\uff0c\u800c\u4e0d\u662f\u56fa\u5b9a\u7684\u6570\u7ec4\u3002\u6b64\u5916\uff0c\u67d0\u4e9b\u64cd\u4f5c\uff08\u5982 sizeof() \uff09\u4f1a\u5c06\u8be5\u5f62\u53c2\u4f5c\u4e3a\u6307\u9488\u8fdb\u884c\u64cd\u4f5c\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u5b9a\u4e49\u51fd\u6570\u7684\u53c2\u6570\u65f6\uff0c\u6700\u597d\u4f7f\u7528\u6307\u9488\u8bed\u6cd5( * )\u800c\u4e0d\u662f\u6570\u7ec4\u8bed\u6cd5( [] )\u3002","title":"\u5c06\u56fa\u5b9a\u6570\u7ec4\u4f20\u9012\u7ed9\u51fd\u6570"},{"location":"11-8-Pointers-and-arrays/#_4","text":"\u6570\u7ec4\u5728\u4f20\u9012\u7ed9\u51fd\u6570\u65f6 \u9000\u5316 \u4e3a\u6307\u9488\u7684\u4e8b\u5b9e\uff0c\u89e3\u91ca\u4e86\u4e3a\u4ec0\u4e48\u5728\u51fd\u6570\u4e2d\u66f4\u6539\u6570\u7ec4\u4f1a\u66f4\u6539\u4f20\u5165\u7684\u5b9e\u9645\u6570\u7ec4\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include // parameter ptr contains a copy of the array's address void changeArray ( int * ptr ) { * ptr = 5 ; // so changing an array element changes the _actual_ array } int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << \"Element 0 has value: \" << array [ 0 ] << '\\n' ; changeArray ( array ); std :: cout << \"Element 0 has value: \" << array [ 0 ] << '\\n' ; return 0 ; } 1 2 Element 0 has value: 1 Element 0 has value: 5 \u5f53 changeArray() \u88ab\u8c03\u7528\u65f6\uff0c\u6570\u7ec4\u9000\u5316\u4e3a\u4e00\u4e2a\u6307\u9488\uff0c\u8be5\u6307\u9488\u7684\u503c(\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740)\u88ab\u590d\u5236\u5230 changeArray() \u51fd\u6570\u7684 ptr \u5f62\u53c2\u4e2d\u3002\u867d\u7136 ptr \u4e2d\u7684\u503c\u662f\u6570\u7ec4\u5730\u5740\u7684\u526f\u672c\uff0c\u4f46 ptr \u4ecd\u7136\u6307\u5411\u5b9e\u9645\u7684\u6570\u7ec4(\u4e0d\u662f\u526f\u672c!)\u56e0\u6b64\uff0c\u5f53\u5bf9 ptr \u89e3\u5f15\u7528\u65f6\uff0c\u8bbf\u95ee\u7684\u5143\u7d20\u5b9e\u9645\u4e0a\u662f\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20! \u7cbe\u660e\u7684\u8bfb\u8005\u4f1a\u6ce8\u610f\u5230\u8fd9\u79cd\u73b0\u8c61\u4e5f\u9002\u7528\u4e8e\u6307\u5411\u975e\u6570\u7ec4\u503c\u7684\u6307\u9488\u3002","title":"\u4f20\u5730\u5740"},{"location":"11-8-Pointers-and-arrays/#_5","text":"\u6700\u540e\uff0c\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u4f5c\u4e3a\u7ed3\u6784\u6216\u7c7b\u7684\u4e00\u90e8\u5206\u7684\u6570\u7ec4\u4e0d\u4f1a\u5728\u5c06\u6574\u4e2a\u7ed3\u6784\u6216\u7c7b\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\u53d1\u751f\u9000\u5316\u3002\u5982\u679c\u6709\u9700\u8981\uff0c\u53ef\u4ee5\u7528\u8fd9\u79cd\u65b9\u5f0f\u9632\u6b62\u6570\u7ec4\u9000\u5316\u4e3a\u6307\u9488\u3002\u5728\u7f16\u5199\u5229\u7528\u6570\u7ec4\u7684\u7c7b\u65f6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u662f\u6709\u7528\u7684 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u6307\u9488\u7b97\u672f\uff0c\u5e76\u8ba8\u8bba\u6570\u7ec4\u7d22\u5f15\u7684\u5de5\u4f5c\u539f\u7406\u3002","title":"\u7ed3\u6784\u4f53\u548c\u7c7b\u4e2d\u7684\u6570\u7ec4\u4e0d\u4f1a\u9000\u5316"},{"location":"11-9-Pointer-arithmetic-and-array-indexing/","tags":["Pointer arithmetic"],"text":"Key Takeaway \u6307\u9488+1\uff0c\u79fb\u52a8\u7684\u5185\u5b58\u5730\u5740\u6570\u53d6\u51b3\u4e8e\u6307\u9488\u6307\u5411\u7684\u5bf9\u8c61\u7684\u5927\u5c0f\uff0c\u6bcf\u6b21\u79fb\u52a8\u4e00\u4e2a\u5bf9\u8c61\u7684\u957f\u5ea6\u3002 C++\u7684 algorithms \u5e93\u63d0\u4f9b\u4e86 std::count_if \uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u57fa\u4e8e\u67d0\u4e2a\u6761\u4ef6\u6765\u7edf\u8ba1\u5143\u7d20 std::begin \u548c std::end \u53ef\u4ee5\u8fd4\u56de\u6307\u5411\u56fa\u5b9a\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u8fed\u4ee3\u5668\u548c\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u540e\u9762\u7684\u4f2a\u5143\u7d20\u7684\u8fed\u4ee3\u5668 std::end \u8fd4\u56de\u7684\u8fed\u4ee3\u5668\u53ea\u4f5c\u4e3a\u6807\u8bb0\u6765\u4f7f\u7528\uff0c\u5bf9\u5176\u8fdb\u884c\u8bbf\u95ee\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \uff0c\u56e0\u4e3a\u5b83\u4e0d\u6307\u5411\u67d0\u4e2a\u5b9e\u9645\u7684\u5143\u7d20\u3002 \u6307\u9488\u8fd0\u7b97 \u5728 C++ \u4e2d\u4f60\u53ef\u4ee5\u5bf9\u6307\u9488\u8fdb\u884c\u52a0\u51cf\u8fd0\u7b97\u3002\u5982\u679c ptr \u6307\u5411\u4e00\u4e2a\u6574\u578b\uff0c\u90a3\u4e48 ptr + 1 \u5219\u4f1a\u6307\u5411\u4e0b\u5185\u5b58\u4e2d\u4e0b\u4e00\u4e2a\u6574\u578b\u5bf9\u8c61\u3002 ptr - 1 \u5219\u4f1a\u6307\u5411\u524d\u4e00\u4e2a\u6574\u5f62\u5bf9\u8c61\u3002 \u6ce8\u610f\uff0c ptr + 1 \u8fd4\u56de\u7684\u5e76\u4e0d\u662f ptr \u6307\u9488\u6307\u5411\u7684\u4e0b\u4e00\u4e2a\u5185\u5b58\u5730\u5740\uff0c\u800c\u662f\u6307\u9488\u6240\u6307\u5bf9\u8c61\u7684\u4e0b\u4e00\u4e2a\u540c\u7c7b\u578b\u5bf9\u8c61\u7684\u5730\u5740\u3002\u5982\u679c\u6307\u9488\u6307\u5411\u6574\u578b\uff08\u5047\u8bbe\u4e3a4\u5b57\u8282\uff09\uff0c\u90a3\u4e48 ptr+3 \u5219\u8fd4\u56de3\u4e2a\u6574\u578b\uff0812\u5b57\u8282\uff09\u540e\u7684\u5730\u5740\u3002\u5982\u679c ptr \u6307\u5411\u7684\u662f char \u7c7b\u578b\u7684\u5bf9\u8c61\uff08\u603b\u662f1\u5b57\u8282\uff09\uff0c\u5219 ptr + 3 \u4f1a\u8fd4\u56de ptr \u540e\u97623\u4e2a\u5b57\u8282\u7684\u5730\u5740\u3002 \u5728\u8ba1\u7b97\u6307\u9488\u7b97\u672f\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u65f6\uff0c\u7f16\u8bd1\u5668\u603b\u662f\u5c06\u6574\u6570\u64cd\u4f5c\u6570\u4e58\u4ee5\u6240\u6307\u5411\u5bf9\u8c61\u7684\u5927\u5c0f\u3002\u8fd9\u53eb\u505a \u6307\u9488\u7f29\u653e(scaling) \u3002 \u8003\u8651\u4ee5\u4e0b\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int value { 7 }; int * ptr { & value }; std :: cout << ptr << '\\n' ; std :: cout << ptr + 1 << '\\n' ; std :: cout << ptr + 2 << '\\n' ; std :: cout << ptr + 3 << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 0012FF7C 0012FF80 0012FF84 0012FF88 \u4e0a\u8ff0\u5730\u5740\u4e4b\u95f4\u76f8\u5dee4\u5b57\u8282(\u5341\u516d\u8fdb\u5236 7C + 4 = 80)\uff0c\u8fd9\u662f\u56e0\u4e3a\u5728\u7b14\u8005\u7535\u8111\u4e0a\u6574\u578b\u5927\u5c0f\u4e3a4\u5b57\u8282\u3002 \u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\u4f7f\u7528 short \u4ee3\u66ff\u4e86 int \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { short value { 7 }; short * ptr { & value }; std :: cout << ptr << '\\n' ; std :: cout << ptr + 1 << '\\n' ; std :: cout << ptr + 2 << '\\n' ; std :: cout << ptr + 3 << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 0012FF7C 0012FF7E 0012FF80 0012FF82 \u56e0\u4e3a short \u7684\u957f\u5ea6\u4e3a 2 \u5b57\u8282\uff0c\u6240\u4ee5\u5730\u5740\u4e4b\u95f4\u76f8\u5dee 2 \u5b57\u8282\u3002 \u6570\u7ec4\u5143\u7d20\u6309\u987a\u5e8f\u5206\u5e03\u5728\u5185\u5b58\u4e2d \u901a\u8fc7\u53d6\u5730\u5740\u8fd0\u7b97\u7b26( & )\uff0c\u6211\u4eec\u53ef\u4ee5\u68c0\u9a8c\u6570\u7ec4\u5143\u7d20\u662f\u5426\u662f\u6309\u7167\u987a\u5e8f\u6392\u5217\u5728\u5185\u5b58\u4e2d\u7684\uff0c0, 1, 2, \u2026 \u5143\u7d20\u5f7c\u6b64\u76f8\u90bb\uff0c\u6309\u7167\u987a\u5e8f\u6392\u5217\u5728\u5185\u5b58\u4e2d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { int array []{ 9 , 7 , 5 , 3 , 1 }; std :: cout << \"Element 0 is at address: \" << & array [ 0 ] << '\\n' ; std :: cout << \"Element 1 is at address: \" << & array [ 1 ] << '\\n' ; std :: cout << \"Element 2 is at address: \" << & array [ 2 ] << '\\n' ; std :: cout << \"Element 3 is at address: \" << & array [ 3 ] << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 Element 0 is at address: 0041FE9C Element 1 is at address: 0041FEA0 Element 2 is at address: 0041FEA4 Element 3 is at address: 0041FEA8 \u6ce8\u610f\uff0c\u6bcf\u4e2a\u5730\u5740\u4e4b\u95f4\u90fd\u95f4\u96944\u5b57\u8282\uff0c\u8fd9\u6b63\u662f\u7b14\u8005\u7535\u8111\u4e0a\u6574\u578b\u6570\u7684\u5927\u5c0f\u3002 \u6307\u9488\u7b97\u6570\u3001\u6570\u7ec4\u548c\u7d22\u5f15\u80cc\u540e\u7684\u9b54\u6cd5 \u4e0a\u9762\u5df2\u7ecf\u4ecb\u7ecd\u4e86\uff0c\u6570\u7ec4\u662f\u4e00\u7cfb\u5217\u987a\u5e8f\u6392\u5217\u7684\u5185\u5b58\u3002 \u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86\uff0c\u56fa\u5b9a\u6570\u7ec4\u53ef\u4ee5\u9000\u5316\u4e3a\u6307\u9488\uff0c\u8be5\u6307\u9488\u6307\u5411\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002 \u540c\u65f6\u6211\u4eec\u8fd8\u77e5\u9053\uff0c\u5bf9\u6307\u9488\u52a01\u53ef\u4ee5\u5f97\u5230\u8be5\u6307\u9488\u6240\u6307\u5730\u5740\u540e\u9762\u4e00\u4e2a\u5bf9\u8c61\u7684\u5730\u5740\u3002 \u4ee5\u4f60\uff0c\u5bf9\u6570\u7ec4\u52a01\u5c31\u53ef\u4ee5\u5f97\u5230\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff0c\u901a\u8fc7\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u8fdb\u884c\u9a8c\u8bc1\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int array []{ 9 , 7 , 5 , 3 , 1 }; std :: cout << & array [ 1 ] << '\\n' ; // print memory address of array element 1 std :: cout << array + 1 << '\\n' ; // print memory address of array pointer + 1 std :: cout << array [ 1 ] << '\\n' ; // prints 7 std :: cout << * ( array + 1 ) << '\\n' ; // prints 7 (note the parenthesis required here) return 0 ; } \u6ce8\u610f\uff0c\u5f53\u901a\u8fc7\u6307\u9488\u8fd0\u7b97\u7684\u7ed3\u679c\u8fdb\u884c\u95f4\u63a5\u64cd\u4f5c\u65f6\uff0c\u5fc5\u987b\u4f7f\u7528\u5706\u62ec\u53f7\u6765\u786e\u4fdd\u64cd\u4f5c\u7b26\u7684\u4f18\u5148\u7ea7\u662f\u6b63\u786e\u7684\uff0c\u56e0\u4e3a operator * \u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e operator + \u3002 \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 0017FB80 0017FB80 7 7 \u4e8b\u5b9e\u8bc1\u660e\uff0c\u5f53\u7f16\u8bd1\u5668\u770b\u5230\u4e0b\u6807\u64cd\u4f5c\u7b26( [] )\u65f6\uff0c\u5b83\u5b9e\u9645\u4e0a\u4f1a\u5c06\u5176\u8f6c\u6362\u4e3a\u6307\u9488\u52a0\u6cd5\u548c\u95f4\u63a5\u64cd\u4f5c\uff01 array[n] \u7b49\u540c\u4e8e *(array + n) \uff0c\u5176\u4e2d n \u4e3a\u6574\u6570\u3002\u4e0b\u6807\u64cd\u4f5c\u7b26 [] \u7684\u5b58\u5728\u65e2\u662f\u4e3a\u4e86\u7f8e\u89c2\uff0c\u4e5f\u662f\u4e3a\u4e86\u4fbf\u4e8e\u4f7f\u7528\u3002 \u4f7f\u7528\u6307\u9488\u904d\u5386\u6570\u7ec4 \u53ef\u4ee5\u4f7f\u7528\u6307\u9488\u548c\u6307\u9488\u7b97\u672f\u6765\u904d\u5386\u6570\u7ec4\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u5e38\u89c1(\u4f7f\u7528\u4e0b\u6807\u901a\u5e38\u66f4\u5bb9\u6613\u9605\u8bfb\u548c\u66f4\u5c11\u7684\u9519\u8bef)\uff0c\u4f46\u7684\u786e\u662f\u53ef\u884c\u7684\u3002\u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include #include // for std::size bool isVowel ( char ch ) { switch ( ch ) { case 'A' : case 'a' : case 'E' : case 'e' : case 'I' : case 'i' : case 'O' : case 'o' : case 'U' : case 'u' : return true ; default : return false ; } } int main () { char name []{ \"Mollie\" }; int arrayLength { static_cast < int > ( std :: size ( name )) }; int numVowels { 0 }; for ( char * ptr { name }; ptr != ( name + arrayLength ); ++ ptr ) { if ( isVowel ( * ptr )) { ++ numVowels ; } } std :: cout << name << \" has \" << numVowels << \" vowels. \\n \" ; return 0 ; } \u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f\u8fd9\u4e2a\u7a0b\u5e8f\u4f7f\u7528\u4e00\u4e2a\u6307\u9488\u6765\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u8bb0\u4f4f\u6570\u7ec4\u4f1a\u9000\u5316\u4e3a\u6307\u5411\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u6307\u9488\u3002\u56e0\u6b64\uff0c\u901a\u8fc7\u4f7f\u7528 name \u521d\u59cb\u5316 ptr \uff0c ptr \u5c06\u6307\u5411\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u5f53\u8c03\u7528 isVowel(*ptr) \u65f6\uff0c\u901a\u8fc7 ptr \u5bf9\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u95f4\u63a5\u64cd\u4f5c\uff0c\u5982\u679c\u5143\u7d20\u662f\u5143\u97f3\uff0c numVowels \u5c06\u9012\u589e\u3002\u7136\u540e\uff0cfor\u5faa\u73af\u4f7f\u7528 ++ \u64cd\u4f5c\u7b26\u5c06\u6307\u9488\u5411\u524d\u79fb\u52a8\u5230\u6570\u7ec4\u4e2d\u7684\u4e0b\u4e00\u4e2a\u5b57\u7b26\u3002\u5f53\u68c0\u67e5\u5b8c\u6240\u6709\u5b57\u7b26\u65f6\uff0cfor\u5faa\u73af\u7ec8\u6b62\u3002 \u4e0a\u8ff0\u7a0b\u5e8f\u4ea7\u751f\u7684\u7ed3\u679c\u662f\uff1a 1 Mollie has 3 vowels \u56e0\u4e3a\u8ba1\u6570\u662f\u975e\u5e38\u5e38\u89c1\u7684\u7b97\u6cd5\uff0c\u6240\u4ee5C++\u7684 algorithms \u5e93\u63d0\u4f9b\u4e86 std::count_if \uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u57fa\u4e8e\u67d0\u4e2a\u6761\u4ef6\u6765\u7edf\u8ba1\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u4e0a\u9762\u7684for\u5faa\u73af\u4f7f\u7528 std::count_if \u8fdb\u884c\u6539\u5199\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include #include #include // for std::begin and std::end bool isVowel ( char ch ) { switch ( ch ) { case 'A' : case 'a' : case 'E' : case 'e' : case 'I' : case 'i' : case 'O' : case 'o' : case 'U' : case 'u' : return true ; default : return false ; } } int main () { char name []{ \"Mollie\" }; // walk through all the elements of name and count how many calls to isVowel return true auto numVowels { std :: count_if ( std :: begin ( name ), std :: end ( name ), isVowel ) }; std :: cout << name << \" has \" << numVowels << \" vowels. \\n \" ; return 0 ; } std::begin \u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u8fed\u4ee3\u5668\uff08\u6307\u9488\uff09\uff0c\u800c std::end \u8fd4\u56de\u7684\u662f\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u540e\u4e00\u4e2a\u4f4d\u7f6e\u3002 std::end \u8fd4\u56de\u7684\u8fed\u4ee3\u5668\u53ea\u4f5c\u4e3a\u6807\u8bb0\u6765\u4f7f\u7528\uff0c\u5bf9\u5176\u8fdb\u884c\u8bbf\u95ee\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u56e0\u4e3a\u5b83\u4e0d\u6307\u5411\u67d0\u4e2a\u5b9e\u9645\u7684\u5143\u7d20\u3002 std::begin \u548c std::end \u53ea\u80fd\u7528\u4e8e\u5df2\u77e5\u957f\u5ea6\u7684\u6570\u7ec4\uff0c\u5982\u679c\u6570\u7ec4\u9000\u5316\u4e3a\u6307\u9488\uff0c\u5219\u9700\u8981\u624b\u52a8\u8ba1\u7b97begin\u548cend\u3002 1 2 3 4 5 // nameLength is the number of elements in the array. std :: count_if ( name , name + nameLength , isVowel ) // Don't do this. Accessing invalid indexes causes undefined behavior. // std::count_if(name, &name[nameLength], isVowel) \u6ce8\u610f\uff0c\u6211\u4eec\u8ba1\u7b97\u7684\u662f name + nameLength \uff0c\u800c\u4e0d\u662f name + nameLength - 1 \uff0c\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u7684\u4e0d\u662f\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u800c\u662f\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u540e\u9762\u7684\u4e00\u4e2a\u201d\u4f2a\u5143\u7d20\u201c\u3002 \u50cf\u8fd9\u6837\u8ba1\u7b97\u6570\u7ec4\u7684\u5f00\u59cb\u548c\u7ed3\u675f\uff0c\u9002\u7528\u4e8e\u6240\u6709\u9700\u8981\u5f00\u59cb\u548c\u7ed3\u675f\u53c2\u6570\u7684\u7b97\u6cd5\u3002","title":"11.9 - \u6307\u9488\u8fd0\u7b97\u548c\u6570\u7ec4\u7d22\u5f15"},{"location":"11-9-Pointer-arithmetic-and-array-indexing/#_1","text":"\u5728 C++ \u4e2d\u4f60\u53ef\u4ee5\u5bf9\u6307\u9488\u8fdb\u884c\u52a0\u51cf\u8fd0\u7b97\u3002\u5982\u679c ptr \u6307\u5411\u4e00\u4e2a\u6574\u578b\uff0c\u90a3\u4e48 ptr + 1 \u5219\u4f1a\u6307\u5411\u4e0b\u5185\u5b58\u4e2d\u4e0b\u4e00\u4e2a\u6574\u578b\u5bf9\u8c61\u3002 ptr - 1 \u5219\u4f1a\u6307\u5411\u524d\u4e00\u4e2a\u6574\u5f62\u5bf9\u8c61\u3002 \u6ce8\u610f\uff0c ptr + 1 \u8fd4\u56de\u7684\u5e76\u4e0d\u662f ptr \u6307\u9488\u6307\u5411\u7684\u4e0b\u4e00\u4e2a\u5185\u5b58\u5730\u5740\uff0c\u800c\u662f\u6307\u9488\u6240\u6307\u5bf9\u8c61\u7684\u4e0b\u4e00\u4e2a\u540c\u7c7b\u578b\u5bf9\u8c61\u7684\u5730\u5740\u3002\u5982\u679c\u6307\u9488\u6307\u5411\u6574\u578b\uff08\u5047\u8bbe\u4e3a4\u5b57\u8282\uff09\uff0c\u90a3\u4e48 ptr+3 \u5219\u8fd4\u56de3\u4e2a\u6574\u578b\uff0812\u5b57\u8282\uff09\u540e\u7684\u5730\u5740\u3002\u5982\u679c ptr \u6307\u5411\u7684\u662f char \u7c7b\u578b\u7684\u5bf9\u8c61\uff08\u603b\u662f1\u5b57\u8282\uff09\uff0c\u5219 ptr + 3 \u4f1a\u8fd4\u56de ptr \u540e\u97623\u4e2a\u5b57\u8282\u7684\u5730\u5740\u3002 \u5728\u8ba1\u7b97\u6307\u9488\u7b97\u672f\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u65f6\uff0c\u7f16\u8bd1\u5668\u603b\u662f\u5c06\u6574\u6570\u64cd\u4f5c\u6570\u4e58\u4ee5\u6240\u6307\u5411\u5bf9\u8c61\u7684\u5927\u5c0f\u3002\u8fd9\u53eb\u505a \u6307\u9488\u7f29\u653e(scaling) \u3002 \u8003\u8651\u4ee5\u4e0b\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int value { 7 }; int * ptr { & value }; std :: cout << ptr << '\\n' ; std :: cout << ptr + 1 << '\\n' ; std :: cout << ptr + 2 << '\\n' ; std :: cout << ptr + 3 << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 0012FF7C 0012FF80 0012FF84 0012FF88 \u4e0a\u8ff0\u5730\u5740\u4e4b\u95f4\u76f8\u5dee4\u5b57\u8282(\u5341\u516d\u8fdb\u5236 7C + 4 = 80)\uff0c\u8fd9\u662f\u56e0\u4e3a\u5728\u7b14\u8005\u7535\u8111\u4e0a\u6574\u578b\u5927\u5c0f\u4e3a4\u5b57\u8282\u3002 \u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\u4f7f\u7528 short \u4ee3\u66ff\u4e86 int \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { short value { 7 }; short * ptr { & value }; std :: cout << ptr << '\\n' ; std :: cout << ptr + 1 << '\\n' ; std :: cout << ptr + 2 << '\\n' ; std :: cout << ptr + 3 << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 0012FF7C 0012FF7E 0012FF80 0012FF82 \u56e0\u4e3a short \u7684\u957f\u5ea6\u4e3a 2 \u5b57\u8282\uff0c\u6240\u4ee5\u5730\u5740\u4e4b\u95f4\u76f8\u5dee 2 \u5b57\u8282\u3002","title":"\u6307\u9488\u8fd0\u7b97"},{"location":"11-9-Pointer-arithmetic-and-array-indexing/#_2","text":"\u901a\u8fc7\u53d6\u5730\u5740\u8fd0\u7b97\u7b26( & )\uff0c\u6211\u4eec\u53ef\u4ee5\u68c0\u9a8c\u6570\u7ec4\u5143\u7d20\u662f\u5426\u662f\u6309\u7167\u987a\u5e8f\u6392\u5217\u5728\u5185\u5b58\u4e2d\u7684\uff0c0, 1, 2, \u2026 \u5143\u7d20\u5f7c\u6b64\u76f8\u90bb\uff0c\u6309\u7167\u987a\u5e8f\u6392\u5217\u5728\u5185\u5b58\u4e2d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { int array []{ 9 , 7 , 5 , 3 , 1 }; std :: cout << \"Element 0 is at address: \" << & array [ 0 ] << '\\n' ; std :: cout << \"Element 1 is at address: \" << & array [ 1 ] << '\\n' ; std :: cout << \"Element 2 is at address: \" << & array [ 2 ] << '\\n' ; std :: cout << \"Element 3 is at address: \" << & array [ 3 ] << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 Element 0 is at address: 0041FE9C Element 1 is at address: 0041FEA0 Element 2 is at address: 0041FEA4 Element 3 is at address: 0041FEA8 \u6ce8\u610f\uff0c\u6bcf\u4e2a\u5730\u5740\u4e4b\u95f4\u90fd\u95f4\u96944\u5b57\u8282\uff0c\u8fd9\u6b63\u662f\u7b14\u8005\u7535\u8111\u4e0a\u6574\u578b\u6570\u7684\u5927\u5c0f\u3002","title":"\u6570\u7ec4\u5143\u7d20\u6309\u987a\u5e8f\u5206\u5e03\u5728\u5185\u5b58\u4e2d"},{"location":"11-9-Pointer-arithmetic-and-array-indexing/#_3","text":"\u4e0a\u9762\u5df2\u7ecf\u4ecb\u7ecd\u4e86\uff0c\u6570\u7ec4\u662f\u4e00\u7cfb\u5217\u987a\u5e8f\u6392\u5217\u7684\u5185\u5b58\u3002 \u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86\uff0c\u56fa\u5b9a\u6570\u7ec4\u53ef\u4ee5\u9000\u5316\u4e3a\u6307\u9488\uff0c\u8be5\u6307\u9488\u6307\u5411\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002 \u540c\u65f6\u6211\u4eec\u8fd8\u77e5\u9053\uff0c\u5bf9\u6307\u9488\u52a01\u53ef\u4ee5\u5f97\u5230\u8be5\u6307\u9488\u6240\u6307\u5730\u5740\u540e\u9762\u4e00\u4e2a\u5bf9\u8c61\u7684\u5730\u5740\u3002 \u4ee5\u4f60\uff0c\u5bf9\u6570\u7ec4\u52a01\u5c31\u53ef\u4ee5\u5f97\u5230\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff0c\u901a\u8fc7\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u8fdb\u884c\u9a8c\u8bc1\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int array []{ 9 , 7 , 5 , 3 , 1 }; std :: cout << & array [ 1 ] << '\\n' ; // print memory address of array element 1 std :: cout << array + 1 << '\\n' ; // print memory address of array pointer + 1 std :: cout << array [ 1 ] << '\\n' ; // prints 7 std :: cout << * ( array + 1 ) << '\\n' ; // prints 7 (note the parenthesis required here) return 0 ; } \u6ce8\u610f\uff0c\u5f53\u901a\u8fc7\u6307\u9488\u8fd0\u7b97\u7684\u7ed3\u679c\u8fdb\u884c\u95f4\u63a5\u64cd\u4f5c\u65f6\uff0c\u5fc5\u987b\u4f7f\u7528\u5706\u62ec\u53f7\u6765\u786e\u4fdd\u64cd\u4f5c\u7b26\u7684\u4f18\u5148\u7ea7\u662f\u6b63\u786e\u7684\uff0c\u56e0\u4e3a operator * \u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e operator + \u3002 \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 0017FB80 0017FB80 7 7 \u4e8b\u5b9e\u8bc1\u660e\uff0c\u5f53\u7f16\u8bd1\u5668\u770b\u5230\u4e0b\u6807\u64cd\u4f5c\u7b26( [] )\u65f6\uff0c\u5b83\u5b9e\u9645\u4e0a\u4f1a\u5c06\u5176\u8f6c\u6362\u4e3a\u6307\u9488\u52a0\u6cd5\u548c\u95f4\u63a5\u64cd\u4f5c\uff01 array[n] \u7b49\u540c\u4e8e *(array + n) \uff0c\u5176\u4e2d n \u4e3a\u6574\u6570\u3002\u4e0b\u6807\u64cd\u4f5c\u7b26 [] \u7684\u5b58\u5728\u65e2\u662f\u4e3a\u4e86\u7f8e\u89c2\uff0c\u4e5f\u662f\u4e3a\u4e86\u4fbf\u4e8e\u4f7f\u7528\u3002","title":"\u6307\u9488\u7b97\u6570\u3001\u6570\u7ec4\u548c\u7d22\u5f15\u80cc\u540e\u7684\u9b54\u6cd5"},{"location":"11-9-Pointer-arithmetic-and-array-indexing/#_4","text":"\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\u548c\u6307\u9488\u7b97\u672f\u6765\u904d\u5386\u6570\u7ec4\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u5e38\u89c1(\u4f7f\u7528\u4e0b\u6807\u901a\u5e38\u66f4\u5bb9\u6613\u9605\u8bfb\u548c\u66f4\u5c11\u7684\u9519\u8bef)\uff0c\u4f46\u7684\u786e\u662f\u53ef\u884c\u7684\u3002\u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include #include // for std::size bool isVowel ( char ch ) { switch ( ch ) { case 'A' : case 'a' : case 'E' : case 'e' : case 'I' : case 'i' : case 'O' : case 'o' : case 'U' : case 'u' : return true ; default : return false ; } } int main () { char name []{ \"Mollie\" }; int arrayLength { static_cast < int > ( std :: size ( name )) }; int numVowels { 0 }; for ( char * ptr { name }; ptr != ( name + arrayLength ); ++ ptr ) { if ( isVowel ( * ptr )) { ++ numVowels ; } } std :: cout << name << \" has \" << numVowels << \" vowels. \\n \" ; return 0 ; } \u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f\u8fd9\u4e2a\u7a0b\u5e8f\u4f7f\u7528\u4e00\u4e2a\u6307\u9488\u6765\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u8bb0\u4f4f\u6570\u7ec4\u4f1a\u9000\u5316\u4e3a\u6307\u5411\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u6307\u9488\u3002\u56e0\u6b64\uff0c\u901a\u8fc7\u4f7f\u7528 name \u521d\u59cb\u5316 ptr \uff0c ptr \u5c06\u6307\u5411\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u5f53\u8c03\u7528 isVowel(*ptr) \u65f6\uff0c\u901a\u8fc7 ptr \u5bf9\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u95f4\u63a5\u64cd\u4f5c\uff0c\u5982\u679c\u5143\u7d20\u662f\u5143\u97f3\uff0c numVowels \u5c06\u9012\u589e\u3002\u7136\u540e\uff0cfor\u5faa\u73af\u4f7f\u7528 ++ \u64cd\u4f5c\u7b26\u5c06\u6307\u9488\u5411\u524d\u79fb\u52a8\u5230\u6570\u7ec4\u4e2d\u7684\u4e0b\u4e00\u4e2a\u5b57\u7b26\u3002\u5f53\u68c0\u67e5\u5b8c\u6240\u6709\u5b57\u7b26\u65f6\uff0cfor\u5faa\u73af\u7ec8\u6b62\u3002 \u4e0a\u8ff0\u7a0b\u5e8f\u4ea7\u751f\u7684\u7ed3\u679c\u662f\uff1a 1 Mollie has 3 vowels \u56e0\u4e3a\u8ba1\u6570\u662f\u975e\u5e38\u5e38\u89c1\u7684\u7b97\u6cd5\uff0c\u6240\u4ee5C++\u7684 algorithms \u5e93\u63d0\u4f9b\u4e86 std::count_if \uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u57fa\u4e8e\u67d0\u4e2a\u6761\u4ef6\u6765\u7edf\u8ba1\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u4e0a\u9762\u7684for\u5faa\u73af\u4f7f\u7528 std::count_if \u8fdb\u884c\u6539\u5199\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include #include #include // for std::begin and std::end bool isVowel ( char ch ) { switch ( ch ) { case 'A' : case 'a' : case 'E' : case 'e' : case 'I' : case 'i' : case 'O' : case 'o' : case 'U' : case 'u' : return true ; default : return false ; } } int main () { char name []{ \"Mollie\" }; // walk through all the elements of name and count how many calls to isVowel return true auto numVowels { std :: count_if ( std :: begin ( name ), std :: end ( name ), isVowel ) }; std :: cout << name << \" has \" << numVowels << \" vowels. \\n \" ; return 0 ; } std::begin \u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u8fed\u4ee3\u5668\uff08\u6307\u9488\uff09\uff0c\u800c std::end \u8fd4\u56de\u7684\u662f\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u540e\u4e00\u4e2a\u4f4d\u7f6e\u3002 std::end \u8fd4\u56de\u7684\u8fed\u4ee3\u5668\u53ea\u4f5c\u4e3a\u6807\u8bb0\u6765\u4f7f\u7528\uff0c\u5bf9\u5176\u8fdb\u884c\u8bbf\u95ee\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u56e0\u4e3a\u5b83\u4e0d\u6307\u5411\u67d0\u4e2a\u5b9e\u9645\u7684\u5143\u7d20\u3002 std::begin \u548c std::end \u53ea\u80fd\u7528\u4e8e\u5df2\u77e5\u957f\u5ea6\u7684\u6570\u7ec4\uff0c\u5982\u679c\u6570\u7ec4\u9000\u5316\u4e3a\u6307\u9488\uff0c\u5219\u9700\u8981\u624b\u52a8\u8ba1\u7b97begin\u548cend\u3002 1 2 3 4 5 // nameLength is the number of elements in the array. std :: count_if ( name , name + nameLength , isVowel ) // Don't do this. Accessing invalid indexes causes undefined behavior. // std::count_if(name, &name[nameLength], isVowel) \u6ce8\u610f\uff0c\u6211\u4eec\u8ba1\u7b97\u7684\u662f name + nameLength \uff0c\u800c\u4e0d\u662f name + nameLength - 1 \uff0c\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u7684\u4e0d\u662f\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u800c\u662f\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u540e\u9762\u7684\u4e00\u4e2a\u201d\u4f2a\u5143\u7d20\u201c\u3002 \u50cf\u8fd9\u6837\u8ba1\u7b97\u6570\u7ec4\u7684\u5f00\u59cb\u548c\u7ed3\u675f\uff0c\u9002\u7528\u4e8e\u6240\u6709\u9700\u8981\u5f00\u59cb\u548c\u7ed3\u675f\u53c2\u6570\u7684\u7b97\u6cd5\u3002","title":"\u4f7f\u7528\u6307\u9488\u904d\u5386\u6570\u7ec4"},{"location":"11-x-chapter-11-comprehensive-quiz/","tags":["summary"],"text":"11.1 - \u6570\u7ec4\uff08\u7b2c\u4e00\u90e8\u5206\uff09 \u4f7f\u7528\u6570\u7ec4\u53ef\u4ee5\u57fa\u4e8e\u4e00\u4e2a\u6807\u8bc6\u7b26\u6765\u5b58\u50a8\u548c\u8bbf\u95ee\u4e00\u7cfb\u5217\u76f8\u540c\u7c7b\u578b\u7684\u53d8\u91cf\u3002\u6570\u7ec4 \u5143\u6570 \u53ef\u4ee5\u901a\u8fc7\u4e0b\u6807\u8fd0\u7b97\u7b26( [] )\u6765\u8bbf\u95ee\u3002\u6ce8\u610f\uff0c\u8bbf\u95ee\u6570\u7ec4\u65f6\u6570\u7ec4\u4e0b\u6807\u4e0d\u80fd\u8d8a\u754c\u3002\u6570\u7ec4\u7684\u521d\u59cb\u5316\u53ef\u4ee5\u4f7f\u7528\u521d\u59cb\u5316\u503c\u5217\u8868\u6216 uniform-initialization \u3002 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 \u56fa\u5b9a\u6570\u7ec4\u5fc5\u987b\u5728 \u7f16\u8bd1\u65f6 \u786e\u5b9a\u957f\u5ea6\u3002\u56fa\u5b9a\u6570\u7ec4\u5728\u6c42\u503c\u6216\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\u901a\u5e38\u4f1a\u9000\u5316\u4e3a\u6307\u9488\u3002 11.3 - \u6570\u7ec4\u548c\u5faa\u73af 11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f \u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u904d\u5386\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f \u5dee\u4e00\u9519\u8bef \uff0c\u907f\u514d\u904d\u5386\u6570\u7ec4\u65f6\u8bbf\u95ee\u8d8a\u754c\u3002\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af\u5728\u6570\u7ec4\u6ca1\u6709\u9000\u5316\u4e3a\u6307\u9488\u65f6\u662f\u975e\u5e38\u6709\u7528\u7684\u3002 11.5 - \u591a\u7ef4\u6570\u7ec4 \u4f7f\u7528\u591a\u4e2a\u7d22\u5f15\u53ef\u4ee5\u521b\u5efa\u591a\u7ef4\u6570\u7ec4\u3002 11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 11.10 - C \u98ce\u683c\u5b57\u7b26\u4e32\u7b26\u53f7\u5e38\u91cf 11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 \u6570\u7ec4\u53ef\u4ee5\u88ab\u7528\u6765\u521b\u5efaC\u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002\u4e00\u822c\u6765\u8bb2\u4f60\u4e0d\u5e94\u8be5\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u521b\u5efa\u5b57\u7b26\u4e32\uff0c\u800c\u662f\u5e94\u8be5\u4f7f\u7528 std::string_view \u548c std::string \u3002 11.8 - \u6307\u9488\u548c\u6570\u7ec4 11.9 - \u6307\u9488\u8fd0\u7b97\u548c\u6570\u7ec4\u7d22\u5f15 \u6307\u9488\u662f\u90a3\u4e9b\u4fdd\u5b58\u7740\u53e6\u5916\u4e00\u4e2a\u53d8\u91cf\u7684\u5730\u5740\u7684\u53d8\u91cf\u3002 \u53d6\u5730\u5740\u64cd\u4f5c\u7b26& \u53ef\u4ee5\u7528\u4e8e\u83b7\u5f97\u67d0\u4e2a\u53d8\u91cf\u7684\u5730\u5740\u3002\u800c\u95f4\u63a5\u8fd0\u7b97\u7b26\uff08 \u89e3\u5f15\u7528 \uff09\u5219\u53ef\u4ee5\u7528\u4e8e\u53d6\u5f97\u6307\u9488\u6240\u6307\u5730\u5740\u4fdd\u5b58\u7684\u503c\u3002 \u7a7a\u6307\u9488\u6ca1\u6709\u6307\u5411\u4efb\u4f55\u5730\u5740\u3002\u6307\u9488\u53ef\u4ee5\u901a\u8fc7\u521d\u59cb\u5316\u6216\u8d4b\u503c\u4e3a nullptr \u7684\u65b9\u5f0f\u5b9a\u4e49\u4e3a\u7a7a\u6307\u9488\u3002\u5220\u9664\u7a7a\u6307\u9488\u662f\u6ca1\u6709\u95ee\u9898\u7684\u3002 \u6307\u5411\u6570\u7ec4\u7684\u6307\u9488\u5e76\u4e0d\u77e5\u9053\u8be5\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u591a\u5c11\u3002\u8fd9\u4e5f\u610f\u5473\u7740 sizeof() \u8fd0\u7b97\u7b26\u548c \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u65e0\u6cd5\u6b63\u786e\u5de5\u4f5c\u3002 11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d new \u548c delete \u8fd0\u7b97\u7b26\u53ef\u4ee5\u4e3a\u53d8\u91cf\u6216\u6570\u7ec4\u5206\u914d\u52a8\u6001\u5185\u5b58\u3002\u5c3d\u7ba1\u6982\u7387\u5f88\u4f4e\uff0c\u4f46\u64cd\u4f5c\u7b26 new \u662f\u6709\u53ef\u80fd\u4f1a\u5931\u8d25\u7684\uff08\u64cd\u4f5c\u7cfb\u7edf\u5185\u5b58\u8017\u5c3d\u65f6\uff09\u3002\u5982\u679c\u4f60\u7f16\u5199\u7684\u8f6f\u4ef6\u8fd0\u884c\u5728\u5185\u5b58\u6709\u9650\u7684\u5e73\u53f0\u4e0a\u65f6\uff0c\u8bf7\u786e\u4fdd\u68c0\u67e5 new \u64cd\u4f5c\u7b26\u662f\u5426\u6210\u529f\u6267\u884c\u3002 \u5f53\u9700\u8981\u5220\u9664\u6570\u7ec4\u65f6\uff0c\u8bf7\u4f7f\u7528\u6570\u7ec4\u5220\u9664( delete[] ) \u3002\u6307\u5411\u5df2\u7ecf\u88ab\u91ca\u653e\u7684\u5185\u5b58\u5730\u5740\u7684\u6307\u9488\uff0c\u79f0\u4e3a \u60ac\u5782 \u6307\u9488\u3002\u4f7f\u7528\u9519\u8bef\u7684\u5220\u9664\u8fd0\u7b97\u7b26 delete \u6216\u8005\u5bf9\u60ac\u5782\u6307\u9488\u89e3\u5f15\u7528\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u5f53\u6307\u5411\u8be5\u5185\u5b58\u7684\u6700\u540e\u4e00\u4e2a\u6307\u9488\u8d85\u51fa\u4f5c\u7528\u57df\u65f6\uff0c\u672a\u80fd\u5220\u9664\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002 11.12 - \u52a8\u6001\u5206\u914d\u6570\u7ec4 \u4e00\u822c\u7684\u53d8\u91cf\uff0c\u7cfb\u7edf\u4f1a\u5728\u4e00\u5757\u6709\u9650\u7684\u5185\u5b58\u533a\u2014\u2014\u6808\u4e0a\u4e3a\u5176\u5206\u914d\u5185\u5b58\u3002\u52a8\u6001\u5206\u914d\u5185\u5b58\u7684\u53d8\u91cf\u5219\u4f4d\u4e8e\u4e00\u5757\u901a\u7528\u7684\u5185\u5b58\u533a\u57df\u2014\u2014\u5806\u4e0a\u3002 \u6307\u5411const\u7c7b\u578b\u503c\u7684\u6307\u9488\u4f1a\u5c06\u5b83\u6240\u6307\u7684\u503c\u770b\u505a\u5e38\u91cf\u3002 1 2 int value { 5 }; const int * ptr { & value }; // this is okay, ptr is pointing to a \"const int\" const \u6307\u9488\u5219\u662f\u8868\u793a\u6307\u9488\u672c\u8eab\u7684\u5185\u5bb9\uff08\u6301\u6709\u7684\u5730\u5740\uff09\u5728\u521d\u59cb\u5316\u540e\u4e0d\u80fd\u88ab\u6539\u53d8\u3002 1 2 int value { 5 }; int * const ptr { & value }; // ptr is const, but *ptr is non-const \u5f15\u7528\u662f\u53d8\u91cf\u7684\u522b\u540d\u3002\u5f15\u7528\u7684\u58f0\u660e\u9700\u8981\u4f7f\u7528 & \u53f7\uff0c\u4f46\u662f\u5728\u8fd9\u4e2a\u8bed\u5883\u4e0b\u5e76\u4e0d\u8868\u793a \u89e3\u5f15\u7528 \u3002\u5f15\u7528\u5e26\u6709\u9690\u542b\u7684\u201c\u5e38\u91cf\u201d\u542b\u4e49\uff0c\u5b83\u4eec\u5fc5\u987b\u88ab\u521d\u59cb\u5316\uff0c\u6b64\u540e\u5c06\u4e0d\u80fd\u591f\u518d\u4e3a\u5176\u8d4b\u4e88\u65b0\u7684\u503c\u3002\u4f7f\u7528\u5f15\u7528\u53ef\u4ee5\u5728\u51fd\u6570\u4f20\u503c\u6216\u8fd4\u56de\u503c\u65f6\u907f\u514d\u62f7\u8d1d\u3002 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26( -> )\u7528\u4e8e\u9009\u62e9\u6307\u9488\u6240\u6307\u7ed3\u6784\u4f53\u4e2d\u7684\u67d0\u4e2a\u6210\u5458\u3002\u5b83\u7b49\u6548\u4e8e\u95f4\u63a5\u8fd0\u7b97\u7b26\u52a0\u4e0a\u4e00\u822c\u7684\u6210\u5458\u8bbf\u95ee\u7b26\uff08 . \uff09\u3002 11.14 - void \u6307\u9488 void \u7c7b\u578b\u7684\u6307\u9488\u53ef\u4ee5\u6307\u5411\u4efb\u4f55\u7c7b\u578b\u7684\u6570\u636e\uff0c\u4f46\u662f\u4e0d\u80fd\u76f4\u63a5\u5bf9\u5b83\u4eec\u4f7f\u7528\u95f4\u63a5\u8fd0\u7b97\u7b26\uff08 \u89e3\u5f15\u7528 \uff09\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06\u5b83\u8fd8\u539f\u4e3a\u539f\u672c\u7684\u6307\u9488\u7c7b\u578b\u3002\u4e0d\u8fc7\u8fd9\u53d6\u51b3\u4e8e\u4f60\u662f\u5426\u8fd8\u8bb0\u5f97\u5b83\u539f\u672c\u662f\u4ec0\u4e48\u7c7b\u578b\u3002 11.15 \u2014 \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u548c\u591a\u7ef4\u6570\u7ec4 \u901a\u8fc7\u6307\u9488\u7684\u6307\u9488\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u6307\u5411\u5176\u4ed6\u6307\u9488\u7684\u6307\u9488\u3002 11.16 \u2014 \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb std::array \u63d0\u4f9b\u4e86C++\u5185\u7f6e\u6570\u7ec4\u7684\u5168\u90e8\uff08\u751a\u81f3\uff09\u66f4\u591a\u7684\u529f\u80fd\uff0c\u800c\u4e14\u5b83\u4e0d\u4f1a\u9000\u5316\u4e3a\u6307\u9488\u3002\u901a\u5e38\u6765\u8bf4\u4f7f\u7528\u5b83\u6765\u66ff\u6362\u5185\u7f6e\u6570\u7ec4\u662f\u66f4\u52a0\u7684\u9009\u62e9\u3002 11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb std::vector \u63d0\u4f9b\u4e86\u52a8\u6001\u6570\u7ec4\u7684\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u81ea\u884c\u7ba1\u7406\u5185\u5b58\u5e76\u8bb0\u5f55\u6570\u7ec4\u7684\u5927\u5c0f\u3002\u901a\u5e38\u4f7f\u7528\u5b83\u6765\u66ff\u6362\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\u662f\u66f4\u52a0\u7684\u9009\u62e9\u3002 11.18 \u2014 \u8fed\u4ee3\u5668\u7b80\u4ecb \u7531\u4e8e \u8fed\u4ee3\u5668 \u7684\u5b58\u5728\uff0c\u6211\u4eec\u5176\u5b9e\u5e76\u4e0d\u9700\u8981\u53bb\u4e86\u89e3\u5bb9\u5668\u7684\u5177\u4f53\u5b9e\u73b0\u5c31\u53ef\u4ee5\u5b8c\u6210\u5faa\u73af\u904d\u5386\u3002 11.19 \u2014 \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb algorithms \u5e93\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u5f88\u591a\u8d81\u624b\u7684\u51fd\u6570\uff0c\u642d\u914d\u8fed\u4ee3\u5668\uff08\u4ee5\u53ca\u540e\u9762\u5b66\u5230\u7684lambda\u51fd\u6570\uff09\uff0c\u5b83\u4eec\u6784\u6210\u4e86C++\u8bed\u8a00\u975e\u5e38\u91cd\u8981\u7684\u4e00\u90e8\u5206\u3002","title":"11.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5206\u914d"},{"location":"12-1-function-pointers/","tags":["function"],"text":"Key Takeaway \u5728 9.6 - \u6307\u9488\u7b80\u4ecb \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u6307\u9488\uff0c\u6307\u9488\u662f\u4e00\u4e2a\u4fdd\u5b58\u7740\u5176\u4ed6\u5730\u5740\u53d8\u91cf\u7684\u53d8\u91cf\u3002 \u51fd\u6570\u6307\u9488 \u4e5f\u662f\u7c7b\u4f3c\u7684\uff0c\u53ea\u4e0d\u8fc7\u5b83\u6307\u5411\u7684\u4e0d\u662f\u53d8\u91cf\uff0c\u800c\u662f\u4e00\u4e2a\u51fd\u6570\uff01 \u8003\u8651\u4e0b\u9762\u51fd\u6570\uff1a 1 2 3 4 int foo () { return 5 ; } foo \u662f\u51fd\u6570\u540d\uff0c\u4f46\u662f\u51fd\u6570\u7684\u7c7b\u578b\u662f\u4ec0\u4e48\uff1f\u51fd\u6570\u6709\u5b83\u81ea\u5df1\u7684 \u5de6\u503c \u51fd\u6570\u7c7b\u578b\u2014\u2014\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570\u7c7b\u578b\u662f\u8fd4\u56de\u6574\u578b\u5e76\u4e14\u4e0d\u63a5\u53d7\u53c2\u6570\u3002\u548c\u53d8\u91cf\u7c7b\u4f3c\uff0c\u51fd\u6570\u4e5f\u5728\u5b58\u653e\u5728\u5185\u5b58\u4e2d\u3002 \u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u8df3\u8f6c\u5230\u88ab\u8c03\u7528\u51fd\u6570\u7684\u5730\u5740\u53bb\u6267\u884c\uff1a 1 2 3 4 5 6 7 8 9 10 11 int foo () // code for foo starts at memory address 0x002717f0 { return 5 ; } int main () { foo (); // jump to address 0x002717f0 return 0 ; } \u5728\u4f60\u7684\u7f16\u7a0b\u751f\u6daf\u4e2d\u53ef\u80fd\u4f1a\u72af\u8fd9\u6837\u7684\u9519\u8bef\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int foo () // code starts at memory address 0x002717f0 { return 5 ; } int main () { std :: cout << foo << '\\n' ; // we meant to call foo(), but instead we're printing foo itself! return 0 ; } \u672c\u6765\u6211\u4eec\u662f\u60f3\u8981\u8c03\u7528\u51fd\u6570foo\u5e76\u6253\u5370\u5b83\u7684\u8fd4\u56de\u503c\u7684\uff0c\u4f46\u662f\u5374\u65e0\u610f\u4e2d\u76f4\u63a5\u4f7f\u7528 std::cout \u6253\u5370\u51fd\u6570\u672c\u8eab\u3002\u6b64\u65f6\u4f1a\u5f97\u5230\u4ec0\u4e48\u6837\u7684\u7ed3\u679c\u5462\uff1f \u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\u4f1a\u8f93\u51fa\u5982\u4e0b\u7ed3\u679c\uff1a 1 0x002717f0 \u4f46\u662f\uff0c\u5728\u4f60\u7684\u7535\u8111\u4e0a\u53ef\u80fd\u4f1a\u6253\u5370\u5176\u4ed6\u503c\uff08\u4f8b\u5982 1\uff09\uff0c\u8fd9\u53d6\u51b3\u4e8e\u4f60\u7684\u7f16\u8bd1\u5668\u5982\u4f55\u5c06\u51fd\u6570\u6307\u9488\u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b\u3002\u5982\u679c\u4f60\u7684\u673a\u5668\u6ca1\u6709\u6253\u5370\u51fd\u6570\u5730\u5740\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u5c06\u51fd\u6570\u5f3a\u5236\u8f6c\u6362\u4e3a\u7a7a\u6307\u9488\u5e76\u6253\u5370\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int foo () // code starts at memory address 0x002717f0 { return 5 ; } int main () { std :: cout << reinterpret_cast < void *> ( foo ) << '\\n' ; // \u544a\u8bc9 C++ \u5c06foo\u89e3\u6790\u4e3a\u7a7a\u6307\u9488 return 0 ; } \u5c31\u50cf\u53ef\u4ee5\u58f0\u660e\u4e00\u4e2a\u6307\u5411\u666e\u901a\u53d8\u91cf\u7684\u975econst\u6307\u9488\u4e00\u6837\uff0c\u4e5f\u53ef\u4ee5\u58f0\u660e\u4e00\u4e2a\u6307\u5411\u51fd\u6570\u7684\u975econst\u6307\u9488\u3002\u5728\u672c\u8bfe\u7684\u5176\u4f59\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u7814\u7a76\u8fd9\u4e9b\u51fd\u6570\u6307\u9488\u53ca\u5176\u7528\u6cd5\u3002\u51fd\u6570\u6307\u9488\u662f\u4e00\u4e2a\u76f8\u5f53\u9ad8\u7ea7\u7684\u4e3b\u9898\uff0c\u5bf9\u4e8e\u53ea\u5e0c\u671b\u4e86\u89e3C++\u57fa\u7840\u77e5\u8bc6\u7684\u4eba\u6765\u8bf4\uff0c\u672c\u8bfe\u7684\u5176\u4f59\u90e8\u5206\u53ef\u4ee5\u5b89\u5168\u5730\u8df3\u8fc7\u6216\u7565\u8fc7\u3002 \u6307\u5411\u51fd\u6570\u7684\u6307\u9488 \u521b\u5efa\u975econst\u51fd\u6570\u6307\u9488\u7684\u8bed\u6cd5\u662fC++\u4e2d\u6700\u4e11\u964b\u7684\u8bed\u6cd5\u4e4b\u4e00\uff1a 1 2 // fcnPtr is a pointer to a function that takes no arguments and returns an integer int ( * fcnPtr )(); \u4e0a\u9762\u4ee3\u7801\u4e2d\u7684 fcnPtr \u4e0a\u4e00\u4e2a\u51fd\u6570\u6307\u9488\uff0c\u5b83\u6ca1\u6709\u5f62\u53c2\u4e14\u8fd4\u56de\u6574\u578b\u3002 fcnPtr \u53ef\u4ee5\u6267\u884c\u4efb\u4f55\u8be5\u7c7b\u578b\u7684\u51fd\u6570\u3002 *fcnPtr \u4e24\u8fb9\u7684\u62ec\u53f7\u662f\u5fc5\u987b\u7684\uff0c\u5b83\u53ef\u4ee5\u786e\u4fdd\u4f18\u5148\u7ea7\u662f\u6b63\u786e\u7684\uff0c\u5426\u5219 int* fcnPtr() \u5c31\u4f1a\u88ab\u89e3\u6790\u4e3a\u540d\u4e3a fcnPtr \uff0c\u4e0d\u63a5\u53d7\u53c2\u6570\uff0c\u8fd4\u56de\u6574\u578b\u6307\u9488\u7684\u51fd\u6570\u7684\u524d\u5411\u58f0\u660e\u3002 \u521b\u5efaconst\u51fd\u6570\u6307\u9488\uff0c\u9700\u8981\u5c06const\u5173\u952e\u5b57\u6dfb\u52a0\u5728\u661f\u53f7\u540e\u9762\uff1a 1 int ( * const fcnPtr )(); \u5982\u679c\u628aconst\u653e\u5728int\u524d\u9762\uff0c\u5219\u8868\u793a\u51fd\u6570\u6307\u9488\u6307\u5411\u7684\u51fd\u6570\u8fd4\u56de\u4e00\u4e2aconst\u6574\u578b\u3002 \u5c06\u51fd\u6570\u8d4b\u503c\u7ed9\u51fd\u6570\u6307\u9488 \u51fd\u6570\u6307\u9488\u53ef\u4ee5\u7528\u51fd\u6570\u521d\u59cb\u5316(\u975econst\u51fd\u6570\u6307\u9488\u53ef\u4ee5\u88ab\u8d4b\u503c\u4e3a\u51fd\u6570)\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u76f4\u63a5\u4f7f\u7528\u4e86 foo \uff0c\u5e76\u4e14\u5b83\u5df2\u7ecf\u88ab\u8f6c\u6362\u4e3a\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u3002\u4e0e\u6307\u5411\u53d8\u91cf\u7684\u6307\u9488\u4e00\u6837\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528 &foo \u6765\u83b7\u5f97\u6307\u5411 foo \u7684\u51fd\u6570\u6307\u9488\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 int foo () { return 5 ; } int goo () { return 6 ; } int main () { int ( * fcnPtr )(){ & foo }; // fcnPtr points to function foo fcnPtr = & goo ; // fcnPtr now points to function goo return 0 ; } \u4e0b\u9762\u4ee3\u7801\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u9519\u8bef\uff1a 1 fcnPtr = goo (); \u8be5\u884c\u4ee3\u7801\u5c06 goo() \u7684\u8fd4\u56de\u503c(\u7c7b\u578b\u4e3a int ) \u8d4b\u503c\u7ed9 fcnPtr (\u5b9e\u9645\u671f\u671b\u7684\u7c7b\u578b\u662f int(*)() )\uff0c\u8fd9\u5e76\u4e0d\u662f\u6211\u4eec\u7684\u672c\u610f\u3002\u6211\u4eec\u5176\u5b9e\u5e0c\u671b fcnPtr \u88ab\u8d4b\u503c\u4e3a goo \u7684\u5730\u5740\uff0c\u800c\u4e0d\u662f goo() \u7684 So no parentheses are needed. \u6ce8\u610f\uff0c\u51fd\u6570\u6307\u9488\u7684\u7c7b\u578b\uff08\u8fd4\u56de\u503c\u548c\u53c2\u6570\uff09\u5fc5\u987b\u548c\u51fd\u6570\u7c7b\u578b\u5339\u914d\uff0c\u4e00\u4e9b\u5b9e\u4f8b\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 // function prototypes int foo (); double goo (); int hoo ( int x ); // function pointer assignments int ( * fcnPtr1 )(){ & foo }; // \u5bf9 int ( * fcnPtr2 )(){ & goo }; // \u9519 -- \u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u6b63\u786e double ( * fcnPtr4 )(){ & goo }; // \u5bf9 fcnPtr1 = & hoo ; // \u9519 -- fcnPtr1 \u4e0d\u63a5\u53d7\u53c2\u6570\uff0c\u548c hoo() \u4e0d\u5339\u914d int ( * fcnPtr3 )( int ){ & hoo }; // \u5bf9 \u4e0e\u57fa\u672c\u7c7b\u578b\u4e0d\u540c\uff0cc++ \u4f1a\u5728\u9700\u8981\u65f6\u9690\u5f0f\u5730\u5c06\u51fd\u6570\u8f6c\u6362\u4e3a\u51fd\u6570\u6307\u9488(\u56e0\u6b64\u4f60==\u4e0d\u9700\u8981\u4f7f\u7528address-of\u64cd\u4f5c\u7b26(&)\u6765\u83b7\u53d6\u51fd\u6570\u7684\u5730\u5740==)\u3002\u4f46\u662f\uff0c\u5b83\u4e0d\u4f1a\u9690\u5f0f\u5730\u5c06\u51fd\u6570\u6307\u9488\u8f6c\u6362\u4e3a\u7a7a\u6307\u9488\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002 \u51fd\u6570\u6307\u9488\u53ef\u4ee5\u88ab\u521d\u59cb\u5316\u6216\u8d4b\u503c\u4e3a nullptr \uff1a 1 int ( * fcnptr )() { nullptr }; // \u5bf9 \u4f7f\u7528\u51fd\u6570\u6307\u9488\u8c03\u7528\u51fd\u6570 \u51fd\u6570\u6307\u9488\u7684\u53e6\u4e00\u4e2a\u4e3b\u8981\u529f\u80fd\u662f\u4f7f\u7528\u5b83\u6765\u8c03\u7528\u51fd\u6570\u3002\u901a\u8fc7\u51fd\u6570\u6307\u9488\u8c03\u7528\u51fd\u6570\u6709\u4e24\u4e2a\u65b9\u6cd5\uff0c\u4e00\u662f\u901a\u8fc7\u663e\u5f0f\u89e3\u5f15\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int foo ( int x ) { return x ; } int main () { int ( * fcnPtr )( int ){ & foo }; // Initialize fcnPtr with function foo ( * fcnPtr )( 5 ); // call function foo(5) through fcnPtr. return 0 ; } \u7b2c\u4e8c\u79cd\u65b9\u6cd5\u662f\u901a\u8fc7\u9690\u5f0f\u89e3\u5f15\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int foo ( int x ) { return x ; } int main () { int ( * fcnPtr )( int ){ & foo }; // Initialize fcnPtr with function foo fcnPtr ( 5 ); // call function foo(5) through fcnPtr. return 0 ; } \u5982\u4f60\u6240\u89c1\uff0c\u4f7f\u7528\u9690\u5f0f\u89e3\u5f15\u7528\u8c03\u7528\u51fd\u6570\u770b\u8d77\u6765\u548c\u6b63\u5e38\u7684\u51fd\u6570\u8c03\u7528\u4e00\u6a21\u4e00\u6837\uff0c\u56e0\u4e3a\u51fd\u6570\u540d\u672c\u6765\u5c31\u662f\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\uff01\u4f46\u662f\uff0c\u6709\u4e9b\u65e7\u7684\u7f16\u8bd1\u5668\u5e76\u4e0d\u652f\u6301\u8fd9\u79cd\u9690\u5f0f\u89e3\u5f15\u7528\u7684\u65b9\u5f0f\uff0c\u4f46\u662f\u73b0\u4ee3\u7f16\u8bd1\u5668\u90fd\u652f\u6301\u3002 \u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a\u9ed8\u8ba4\u5f62\u53c2\u4e0d\u9002\u7528\u4e8e\u901a\u8fc7\u51fd\u6570\u6307\u9488\u8c03\u7528\u7684\u51fd\u6570\u3002\u9ed8\u8ba4\u5f62\u53c2\u5728\u7f16\u8bd1\u65f6\u89e3\u6790(\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u4e3a\u9ed8\u8ba4\u5f62\u53c2\u63d0\u4f9b\u5b9e\u53c2\uff0c\u7f16\u8bd1\u5668\u5c06\u5728\u7f16\u8bd1\u4ee3\u7801\u65f6\u4e3a\u60a8\u63d0\u4f9b\u4e00\u4e2a\u5b9e\u53c2)\u3002\u4f46\u662f\uff0c\u51fd\u6570\u6307\u9488\u5728\u8fd0\u884c\u65f6\u89e3\u6790\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528\u51fd\u6570\u6307\u9488\u8fdb\u884c\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u65e0\u6cd5\u89e3\u6790\u9ed8\u8ba4\u5f62\u53c2\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5fc5\u987b\u663e\u5f0f\u5730\u4f20\u5165\u4efb\u4f55\u9ed8\u8ba4\u53c2\u6570\u7684\u503c\u3002 \u8fd8\u8981\u6ce8\u610f\uff0c\u56e0\u4e3a\u51fd\u6570\u6307\u9488\u53ef\u4ee5\u8bbe\u7f6e\u4e3anullptr\uff0c\u6240\u4ee5\u5728\u8c03\u7528\u51fd\u6570\u6307\u9488\u4e4b\u524d\uff0c\u6700\u597d\u901a\u8fc7\u65ad\u8a00\u6216\u6709\u6761\u4ef6\u6d4b\u8bd5\u5224\u65ad\u5b83\u662f\u5426\u662f\u7a7a\u6307\u9488\u3002\u5c31\u50cf\u666e\u901a\u6307\u9488\u4e00\u6837\uff0c\u5bf9\u7a7a\u51fd\u6570\u6307\u9488\u7684\u89e3\u5f15\u7528\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 int foo ( int x ) { return x ; } int main () { int ( * fcnPtr )( int ){ & foo }; // Initialize fcnPtr with function foo if ( fcnPtr ) // make sure fcnPtr isn't a null pointer fcnPtr ( 5 ); // otherwise this will lead to undefined behavior return 0 ; } \u5c06\u51fd\u6570\u4f5c\u4e3a\u5b9e\u53c2\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570 \u51fd\u6570\u6307\u9488\u6700\u5927\u7684\u7528\u9014\u5176\u5b9e\u662f\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\u3002\u8fd9\u79cd\u4f5c\u4e3a\u5b9e\u53c2\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\u7684\u51fd\u6570\uff0c\u79f0\u4e3a \u56de\u8c03\u51fd\u6570 \u3002 \u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u5047\u8bbe\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u6267\u884c\u4e00\u9879\u4efb\u52a1(\u4f8b\u5982\u5bf9\u6570\u7ec4\u6392\u5e8f)\uff0c\u4f46\u4f60\u5e0c\u671b\u7528\u6237\u80fd\u591f\u5b9a\u4e49\u5982\u4f55\u6267\u884c\u8be5\u4efb\u52a1\u7684\u7279\u5b9a\u90e8\u5206(\u4f8b\u5982\u6570\u7ec4\u662f\u6309\u5347\u5e8f\u8fd8\u662f\u964d\u5e8f\u6392\u5e8f)\u3002\u8ba9\u6211\u4eec\u4ed4\u7ec6\u770b\u770b\u8fd9\u4e2a\u95ee\u9898\u662f\u5982\u4f55\u5177\u4f53\u5e94\u7528\u4e8e\u6392\u5e8f\u7684\uff0c\u4f5c\u4e3a\u4e00\u4e2a\u53ef\u4ee5\u63a8\u5e7f\u5230\u5176\u4ed6\u7c7b\u4f3c\u95ee\u9898\u7684\u4f8b\u5b50\u3002 \u8bb8\u591a\u57fa\u4e8e\u6bd4\u8f83\u7684\u6392\u5e8f\u7b97\u6cd5\u90fd\u57fa\u4e8e\u7c7b\u4f3c\u7684\u6982\u5ff5\uff1a\u6392\u5e8f\u7b97\u6cd5\u904d\u5386\u5217\u8868\uff0c\u5bf9\u6570\u5b57\u5bf9\u8fdb\u884c\u6bd4\u8f83\uff0c\u5e76\u57fa\u4e8e\u8fd9\u4e9b\u6bd4\u8f83\u7684\u7ed3\u679c\u5bf9\u6570\u5b57\u8fdb\u884c\u91cd\u65b0\u6392\u5e8f\u3002\u56e0\u6b64\uff0c\u901a\u8fc7\u6539\u53d8\u6bd4\u8f83\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6539\u53d8\u7b97\u6cd5\u7684\u6392\u5e8f\u65b9\u5f0f\uff0c\u800c\u4e0d\u5f71\u54cd\u5176\u4ed6\u6392\u5e8f\u4ee3\u7801\u3002 \u4e0b\u9762\u662f\u4e4b\u524d\u7684\u9009\u62e9\u6392\u5e8f\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // for std::swap void SelectionSort ( int * array , int size ) { // Step through each element of the array for ( int startIndex { 0 }; startIndex < ( size - 1 ); ++ startIndex ) { // smallestIndex is the index of the smallest element we've encountered so far. int smallestIndex { startIndex }; // Look for smallest element remaining in the array (starting at startIndex+1) for ( int currentIndex { startIndex + 1 }; currentIndex < size ; ++ currentIndex ) { // If the current element is smaller than our previously found smallest if ( array [ smallestIndex ] > array [ currentIndex ]) // COMPARISON DONE HERE { // This is the new smallest number for this iteration smallestIndex = currentIndex ; } } // Swap our start element with our smallest element std :: swap ( array [ startIndex ], array [ smallestIndex ]); } } \u8ba9\u6211\u4eec\u7528\u4e00\u4e2a\u51fd\u6570\u6765\u66ff\u6362\u8fd9\u91cc\u7684\u6bd4\u8f83\u3002\u6bd4\u8f83\u51fd\u6570\u6bd4\u8f83\u4e24\u4e2a\u6574\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\u6765\u6307\u793a\u5143\u7d20\u662f\u5426\u5e94\u8be5\u4ea4\u6362\uff0c\u5b83\u770b\u8d77\u6765\u50cf\u8fd9\u6837: 1 2 3 4 bool ascending ( int x , int y ) { return x > y ; // swap if the first element is greater than the second } \u4f7f\u7528 ascending() \u51fd\u6570\u4fee\u6539\u540e\u7684\u6392\u5e8f\u7b97\u6cd5\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // for std::swap void SelectionSort ( int * array , int size ) { // Step through each element of the array for ( int startIndex { 0 }; startIndex < ( size - 1 ); ++ startIndex ) { // smallestIndex is the index of the smallest element we've encountered so far. int smallestIndex { startIndex }; // Look for smallest element remaining in the array (starting at startIndex+1) for ( int currentIndex { startIndex + 1 }; currentIndex < size ; ++ currentIndex ) { // If the current element is smaller than our previously found smallest if ( ascending ( array [ smallestIndex ], array [ currentIndex ])) // COMPARISON DONE HERE { // This is the new smallest number for this iteration smallestIndex = currentIndex ; } } // Swap our start element with our smallest element std :: swap ( array [ startIndex ], array [ smallestIndex ]); } } \u73b0\u5728\uff0c\u4e3a\u4e86\u8ba9\u8c03\u7528\u8005\u51b3\u5b9a\u5982\u4f55\u8fdb\u884c\u6392\u5e8f\uff0c\u6211\u4eec\u4e0d\u4f7f\u7528\u56fa\u5b9a\u7684\u6bd4\u8f83\u51fd\u6570\uff0c\u800c\u662f\u5141\u8bb8\u8c03\u7528\u8005\u63d0\u4f9b\u4ed6\u4eec\u81ea\u5df1\u7684\u6392\u5e8f\u51fd\u6570\uff01\u8fd9\u662f\u901a\u8fc7\u51fd\u6570\u6307\u9488\u5b8c\u6210\u7684\u3002 \u56e0\u4e3a\u8c03\u7528\u8005\u7684\u6bd4\u8f83\u51fd\u6570\u5c06\u6bd4\u8f83\u4e24\u4e2a\u6574\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u6240\u4ee5\u6307\u5411\u8fd9\u6837\u4e00\u4e2a\u51fd\u6570\u7684\u6307\u9488\u770b\u8d77\u6765\u50cf\u8fd9\u6837: 1 bool ( * comparisonFcn )( int , int ); \u56e0\u6b64\uff0c\u6211\u4eec\u5c06\u5141\u8bb8\u8c03\u7528\u8005\u5c06\u4e00\u4e2a\u6307\u5411\u4ed6\u4eec\u60f3\u8981\u7684\u6bd4\u8f83\u51fd\u6570\u7684\u6307\u9488\u4f5c\u4e3a\u7b2c\u4e09\u4e2a\u5f62\u53c2\u4f20\u9012\u7ed9\u6392\u5e8f\u4f8b\u7a0b\uff0c\u7136\u540e\u6211\u4eec\u5c06\u4f7f\u7528\u8c03\u7528\u8005\u7684\u51fd\u6570\u8fdb\u884c\u6bd4\u8f83\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u51fd\u6570\u6307\u9488\u5f62\u53c2\u8fdb\u884c\u7528\u6237\u5b9a\u4e49\u6bd4\u8f83\u7684\u9009\u62e9\u6392\u5e8f\u7684\u5b8c\u6574\u793a\u4f8b\uff0c\u4ee5\u53ca\u5982\u4f55\u8c03\u7528\u5b83\u7684\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 #include // for std::swap #include // Note our user-defined comparison is the third parameter void selectionSort ( int * array , int size , bool ( * comparisonFcn )( int , int )) { // Step through each element of the array for ( int startIndex { 0 }; startIndex < ( size - 1 ); ++ startIndex ) { // bestIndex is the index of the smallest/largest element we've encountered so far. int bestIndex { startIndex }; // Look for smallest/largest element remaining in the array (starting at startIndex+1) for ( int currentIndex { startIndex + 1 }; currentIndex < size ; ++ currentIndex ) { // If the current element is smaller/larger than our previously found smallest if ( comparisonFcn ( array [ bestIndex ], array [ currentIndex ])) // COMPARISON DONE HERE { // This is the new smallest/largest number for this iteration bestIndex = currentIndex ; } } // Swap our start element with our smallest/largest element std :: swap ( array [ startIndex ], array [ bestIndex ]); } } // Here is a comparison function that sorts in ascending order // (Note: it's exactly the same as the previous ascending() function) bool ascending ( int x , int y ) { return x > y ; // swap if the first element is greater than the second } // Here is a comparison function that sorts in descending order bool descending ( int x , int y ) { return x < y ; // swap if the second element is greater than the first } // This function prints out the values in the array void printArray ( int * array , int size ) { for ( int index { 0 }; index < size ; ++ index ) { std :: cout << array [ index ] << ' ' ; } std :: cout << '\\n' ; } int main () { int array [ 9 ]{ 3 , 7 , 9 , 5 , 6 , 1 , 8 , 2 , 4 }; // Sort the array in descending order using the descending() function selectionSort ( array , 9 , descending ); printArray ( array , 9 ); // Sort the array in ascending order using the ascending() function selectionSort ( array , 9 , ascending ); printArray ( array , 9 ); return 0 ; } \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 \u9177\u4e0d\u9177\uff1f\u6211\u4eec\u7ed9\u4e86\u8c03\u7528\u8005\u63a7\u5236\u9009\u62e9\u6392\u5e8f\u5982\u4f55\u5de5\u4f5c\u7684\u80fd\u529b\u3002 \u8c03\u7528\u8005\u751a\u81f3\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u201c\u5947\u602a\u7684\u201d\u6bd4\u8f83\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 bool evensFirst ( int x , int y ) { // if x is even and y is odd, x goes first (no swap needed) if (( x % 2 == 0 ) && ! ( y % 2 == 0 )) return false ; // if x is odd and y is even, y goes first (swap needed) if ( ! ( x % 2 == 0 ) && ( y % 2 == 0 )) return true ; // otherwise sort in ascending order return ascending ( x , y ); } int main () { int array [ 9 ]{ 3 , 7 , 9 , 5 , 6 , 1 , 8 , 2 , 4 }; selectionSort ( array , 9 , evensFirst ); printArray ( array , 9 ); return 0 ; } \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 4 6 8 1 3 5 7 9 \u5982\u4f60\u6240\u89c1\uff0c\u5728\u6b64\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\u51fd\u6570\u6307\u9488\u662f\u4e00\u79cd\u5f88\u597d\u65b9\u5f0f\uff0c\u8c03\u7528\u8005\u53ef\u4ee5\u5c06\u81ea\u5df1\u7684\u529f\u80fd\u201c\u6302\u94a9\u201d\u5230\u4f60\u4ee5\u524d\u7f16\u5199\u548c\u6d4b\u8bd5\u8fc7\u7684\u5185\u5bb9\u4e2d\uff0c\u8fd9\u6709\u52a9\u4e8e\u4fc3\u8fdb\u4ee3\u7801\u91cd\u7528\uff01\u4ee5\u524d\uff0c\u5982\u679c\u5e0c\u671b\u6309\u964d\u5e8f\u6392\u5217\u4e00\u4e2a\u6570\u7ec4\uff0c\u6309\u5347\u5e8f\u6392\u5217\u53e6\u4e00\u4e2a\u6570\u7ec4\uff0c\u5219\u9700\u8981\u591a\u4e2a\u7248\u672c\u7684\u6392\u5e8f\u4f8b\u7a0b\u3002\u73b0\u5728\u4f60\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7248\u672c\u5e76\u6309\u8c03\u7528\u8005\u5e0c\u671b\u7684\u4efb\u4f55\u65b9\u5f0f\u6392\u5e8f\uff01 \u6ce8\u610f\uff1a \u5982\u679c\u51fd\u6570\u5f62\u53c2\u662f\u51fd\u6570\u7c7b\u578b\u7684\uff0c\u5b83\u5c06\u88ab\u8f6c\u6362\u4e3a\u6307\u5411\u51fd\u6570\u7c7b\u578b\u7684\u6307\u9488 \u3002\u8fd9\u610f\u5473\u7740\uff1a 1 void selectionSort ( int * array , int size , bool ( * comparisonFcn )( int , int )) \u7b49\u4ef7\u4e8e\uff1a 1 void selectionSort ( int * array , int size , bool comparisonFcn ( int , int )) \u8fd9\u53ea\u9002\u7528\u4e8e\u51fd\u6570\u5f62\u53c2\uff0c\u800c\u4e0d\u9002\u7528\u4e8e\u72ec\u7acb\u7684\u51fd\u6570\u6307\u9488\uff0c\u56e0\u6b64\u4f7f\u7528\u6bd4\u8f83\u6709\u9650\u3002 \u63d0\u4f9b\u9ed8\u8ba4\u51fd\u6570 \u5982\u679c\u4f60\u8981\u5141\u8bb8\u8c03\u7528\u65b9\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\uff0c\u90a3\u4e48\u4e3a\u8c03\u7528\u65b9\u63d0\u4f9b\u4e00\u4e9b\u6807\u51c6\u51fd\u6570\u4ee5\u65b9\u4fbf\u4f7f\u7528\u901a\u5e38\u662f\u5f88\u6709\u7528\u7684\u3002\u4f8b\u5982\uff0c\u5728\u4e0a\u9762\u7684\u9009\u62e9\u6392\u5e8f\u793a\u4f8b\u4e2d\uff0c\u63d0\u4f9b ascending() \u548c descending() \u51fd\u6570\u4ee5\u53ca selectionSort() \u51fd\u6570\u5c06\u4f7f\u8c03\u7528\u8005\u7684\u5de5\u4f5c\u66f4\u52a0\u8f7b\u677e\uff0c\u56e0\u4e3a\u4ed6\u4eec\u4e0d\u5fc5\u6bcf\u6b21\u60f3\u8981\u4f7f\u7528 ascending() \u548c descending() \u51fd\u6570\u65f6\u90fd\u91cd\u5199\u5b83\u4eec\u3002 \u4f60\u751a\u81f3\u53ef\u4ee5\u8bbe\u7f6e\u5176\u4e2d\u4e00\u4e2a\u4f5c\u4e3a\u9ed8\u8ba4\u53c2\u6570\uff1a 1 2 // Default the sort to ascending sort void selectionSort ( int * array , int size , bool ( * comparisonFcn )( int , int ) = ascending ); \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u7528\u6237\u6b63\u5e38\u8c03\u7528 selectionSort \uff08\u4e0d\u4f7f\u7528\u51fd\u6570\u6307\u9488\uff09\uff0c comparisonFcn \u5f62\u53c2 \u9ed8\u8ba4\u662f\u5347\u5e8f\u7684\u3002\u4f60\u5fc5\u987b\u786e\u4fdd ascending \u51fd\u6570\u5728\u6b64\u4e4b\u524d\u88ab\u5b9a\u4e49\uff0c\u5426\u5219\u7f16\u8bd1\u5668\u5c06\u4f1a\u62a5\u544a ascending \u627e\u4e0d\u5230\u3002 \u4f7f\u7528\u7c7b\u578b\u522b\u540d\u8ba9\u51fd\u6570\u6307\u9488\u770b\u8d77\u6765\u66f4\u4f18\u96c5 \u5b9e\u8bdd\u5b9e\u8bf4\u2014\u2014\u51fd\u6570\u6307\u9488\u7684\u8bed\u6cd5\u7684\u786e\u5f88\u96be\u770b\u3002\u4e0d\u8fc7\uff0c \u7c7b\u578b\u522b\u540d \u53ef\u4ee5\u7528\u6765\u4f7f\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\u770b\u8d77\u6765\u66f4\u50cf\u5e38\u89c4\u53d8\u91cf \uff1a 1 using ValidateFunction = bool ( * )( int , int ); \u7c7b\u578b\u522b\u540d \u201cValidateFunction\u201d \u662f\u4e00\u4e2a\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\uff0c\u8be5\u6307\u9488\u63a5\u53d7\u4e24\u4e2a\u6574\u578b\u5f62\u53c2\u5e76\u8fd4\u56de\u4e00\u4e2abool\u7c7b\u578b\u503c\u3002 \u6b64\u65f6\u6211\u4eec\u5c31\u53ef\u4ee5\u4e0d\u8fd9\u6837\u505a\uff1a 1 bool validate ( int x , int y , bool ( * fcnPtr )( int , int )); // \u4e11\u964b \u4f60\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 bool validate ( int x , int y , ValidateFunction pfcn ) // \u4f18\u96c5 \u4f7f\u7528 std::function \u5b9a\u4e49\u548c\u5b58\u50a8\u51fd\u6570\u6307\u9488\u8fd8\u6709\u4e00\u4e2a\u529e\u6cd5\uff0c\u5373\u4f7f\u7528 std::function \uff0c\u5b83\u5b9a\u4e49\u5728\u6807\u51c6\u5e93 \u5934\u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u5c06\u51fd\u6570\u6307\u9488\u5b9a\u4e49\u4e3a\u4e00\u4e2a std::function \u5bf9\u8c61\uff1a 1 2 #include bool validate ( int x , int y , std :: function < bool ( int , int ) > fcn ); // std::function method that returns a bool and takes two int parameters \u5982\u4f60\u6240\u89c1\uff0c\u8fd4\u56de\u503c\u7c7b\u578b\u5b9a\u4e49\u5728\u5c16\u62ec\u53f7\u4e2d\uff0c\u53c2\u6570\u7c7b\u578b\u5219\u88ab\u5b9a\u4e49\u5728\u62ec\u53f7\u4e2d\u3002\u5982\u679c\u6ca1\u6709\u53c2\u6570\u7684\u8bdd\uff0c\u62ec\u53f7\u53ef\u4ee5\u7559\u7a7a\u3002 \u4f7f\u7528 std::function \u66f4\u65b0\u4e4b\u524d\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include int foo () { return 5 ; } int goo () { return 6 ; } int main () { std :: function < int () > fcnPtr { & foo }; // declare function pointer that returns an int and takes no parameters fcnPtr = & goo ; // fcnPtr now points to function goo std :: cout << fcnPtr () << '\\n' ; // call the function just like normal return 0 ; } \u7c7b\u578b\u522b\u540d\u4e5f\u53ef\u4ee5\u5e2e\u52a9 std::function \u63d0\u9ad8\u53ef\u8bfb\u6027\uff1a 1 2 using ValidateFunctionRaw = bool ( * )( int , int ); // type alias to raw function pointer using ValidateFunction = std :: function < bool ( int , int ) > ; // type alias to std::function \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c std::function \u53ea\u5141\u8bb8\u901a\u8fc7\u9690\u5f0f\u89e3\u5f15\u7528(e.g. fcnPtr() )\u8c03\u7528\u51fd\u6570\uff0c\u800c\u4e0d\u652f\u6301\u663e\u793a\u89e3\u5f15\u7528\u8c03\u7528\u51fd\u6570 (e.g. (*fcnPtr)() )\u3002 C++17 \u4e2d \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD \u53ef\u4ee5\u7528\u6765\u4ece\u521d\u59cb\u5316\u503c\u76f4\u63a5\u63a8\u65ad std::function \u7684\u7c7b\u578b\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5199 std::function fcnPtr{ &foo }; \u800c\u4e0d\u9700\u8981\u5199 std::function fcnPtr{ &foo }; \uff0c\u8ba9\u7f16\u8bd1\u5668\u81ea\u5df1\u63a8\u65ad\u6a21\u677f\u53c2\u6570\u3002\u4f46\u662f CTAD \u5e76\u4e0d\u80fd\u5728\u5b9a\u4e49\u7c7b\u578b\u522b\u540d\u65f6\u4f7f\u7528\uff0c\u56e0\u4e3a\u6b64\u65f6\u6211\u4eec\u5e76\u6ca1\u6709\u63d0\u4f9b\u521d\u59cb\u5316\u503c\u3002 \u51fd\u6570\u6307\u9488\u7684\u7c7b\u578b\u63a8\u65ad auto \u5173\u952e\u5b57\u53ef\u4ee5\u63a8\u65ad\u666e\u901a\u53d8\u91cf\u7c7b\u578b\uff0c auto \u5173\u952e\u5b57\u4e5f\u53ef\u4ee5\u63a8\u65ad\u51fd\u6570\u6307\u9488\u7c7b\u578b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int foo ( int x ) { return x ; } int main () { auto fcnPtr { & foo }; std :: cout << fcnPtr ( 5 ) << '\\n' ; return 0 ; } \u8fd9\u5b8c\u5168\u7b26\u5408\u60a8\u7684\u9884\u671f\uff0c\u800c\u4e14\u8bed\u6cd5\u975e\u5e38\u7b80\u6d01\u3002\u5f53\u7136\uff0c\u7f3a\u70b9\u662f\u5173\u4e8e\u51fd\u6570\u7684\u5f62\u53c2\u7c7b\u578b\u548c\u8fd4\u56de\u7c7b\u578b\u7684\u6240\u6709\u7ec6\u8282\u90fd\u662f\u9690\u85cf\u7684\uff0c\u56e0\u6b64\u5728\u8c03\u7528\u51fd\u6570\u6216\u4f7f\u7528\u5176\u8fd4\u56de\u503c\u65f6\u5f88\u5bb9\u6613\u51fa\u9519\u3002 \u5c0f\u7ed3 \u5982\u679c\u4f60\u5e0c\u671b\u5c06\u51fd\u6570\u5b58\u50a8\u5728\u6570\u7ec4(\u6216\u5176\u4ed6\u7ed3\u6784)\u4e2d\uff0c\u6216\u8005\u9700\u8981\u5c06\u51fd\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u65f6\uff0c\u51fd\u6570\u6307\u9488\u975e\u5e38\u6709\u7528\u3002\u56e0\u4e3a\u58f0\u660e\u51fd\u6570\u6307\u9488\u7684\u8bed\u6cd5\u5f88\u96be\u770b\u800c\u4e14\u5bb9\u6613\u51fa\u9519\uff0c\u6211\u4eec\u5efa\u8bae\u4f7f\u7528 std::function \u3002\u5728\u51fd\u6570\u6307\u9488\u7c7b\u578b\u53ea\u4f7f\u7528\u4e00\u6b21\u7684\u5730\u65b9(\u4f8b\u5982\u5355\u4e2a\u5f62\u53c2\u6216\u8fd4\u56de\u503c)\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 std::function \u3002\u5728\u591a\u6b21\u4f7f\u7528\u51fd\u6570\u6307\u9488\u7c7b\u578b\u7684\u5730\u65b9\uff0c\u521b\u5efa std::function \u7684\u7c7b\u578b\u522b\u540d\u5219\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002","title":"12.1 - \u51fd\u6570\u6307\u9488"},{"location":"12-1-function-pointers/#_1","text":"\u521b\u5efa\u975econst\u51fd\u6570\u6307\u9488\u7684\u8bed\u6cd5\u662fC++\u4e2d\u6700\u4e11\u964b\u7684\u8bed\u6cd5\u4e4b\u4e00\uff1a 1 2 // fcnPtr is a pointer to a function that takes no arguments and returns an integer int ( * fcnPtr )(); \u4e0a\u9762\u4ee3\u7801\u4e2d\u7684 fcnPtr \u4e0a\u4e00\u4e2a\u51fd\u6570\u6307\u9488\uff0c\u5b83\u6ca1\u6709\u5f62\u53c2\u4e14\u8fd4\u56de\u6574\u578b\u3002 fcnPtr \u53ef\u4ee5\u6267\u884c\u4efb\u4f55\u8be5\u7c7b\u578b\u7684\u51fd\u6570\u3002 *fcnPtr \u4e24\u8fb9\u7684\u62ec\u53f7\u662f\u5fc5\u987b\u7684\uff0c\u5b83\u53ef\u4ee5\u786e\u4fdd\u4f18\u5148\u7ea7\u662f\u6b63\u786e\u7684\uff0c\u5426\u5219 int* fcnPtr() \u5c31\u4f1a\u88ab\u89e3\u6790\u4e3a\u540d\u4e3a fcnPtr \uff0c\u4e0d\u63a5\u53d7\u53c2\u6570\uff0c\u8fd4\u56de\u6574\u578b\u6307\u9488\u7684\u51fd\u6570\u7684\u524d\u5411\u58f0\u660e\u3002 \u521b\u5efaconst\u51fd\u6570\u6307\u9488\uff0c\u9700\u8981\u5c06const\u5173\u952e\u5b57\u6dfb\u52a0\u5728\u661f\u53f7\u540e\u9762\uff1a 1 int ( * const fcnPtr )(); \u5982\u679c\u628aconst\u653e\u5728int\u524d\u9762\uff0c\u5219\u8868\u793a\u51fd\u6570\u6307\u9488\u6307\u5411\u7684\u51fd\u6570\u8fd4\u56de\u4e00\u4e2aconst\u6574\u578b\u3002","title":"\u6307\u5411\u51fd\u6570\u7684\u6307\u9488"},{"location":"12-1-function-pointers/#_2","text":"\u51fd\u6570\u6307\u9488\u53ef\u4ee5\u7528\u51fd\u6570\u521d\u59cb\u5316(\u975econst\u51fd\u6570\u6307\u9488\u53ef\u4ee5\u88ab\u8d4b\u503c\u4e3a\u51fd\u6570)\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u76f4\u63a5\u4f7f\u7528\u4e86 foo \uff0c\u5e76\u4e14\u5b83\u5df2\u7ecf\u88ab\u8f6c\u6362\u4e3a\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u3002\u4e0e\u6307\u5411\u53d8\u91cf\u7684\u6307\u9488\u4e00\u6837\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528 &foo \u6765\u83b7\u5f97\u6307\u5411 foo \u7684\u51fd\u6570\u6307\u9488\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 int foo () { return 5 ; } int goo () { return 6 ; } int main () { int ( * fcnPtr )(){ & foo }; // fcnPtr points to function foo fcnPtr = & goo ; // fcnPtr now points to function goo return 0 ; } \u4e0b\u9762\u4ee3\u7801\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u9519\u8bef\uff1a 1 fcnPtr = goo (); \u8be5\u884c\u4ee3\u7801\u5c06 goo() \u7684\u8fd4\u56de\u503c(\u7c7b\u578b\u4e3a int ) \u8d4b\u503c\u7ed9 fcnPtr (\u5b9e\u9645\u671f\u671b\u7684\u7c7b\u578b\u662f int(*)() )\uff0c\u8fd9\u5e76\u4e0d\u662f\u6211\u4eec\u7684\u672c\u610f\u3002\u6211\u4eec\u5176\u5b9e\u5e0c\u671b fcnPtr \u88ab\u8d4b\u503c\u4e3a goo \u7684\u5730\u5740\uff0c\u800c\u4e0d\u662f goo() \u7684 So no parentheses are needed. \u6ce8\u610f\uff0c\u51fd\u6570\u6307\u9488\u7684\u7c7b\u578b\uff08\u8fd4\u56de\u503c\u548c\u53c2\u6570\uff09\u5fc5\u987b\u548c\u51fd\u6570\u7c7b\u578b\u5339\u914d\uff0c\u4e00\u4e9b\u5b9e\u4f8b\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 // function prototypes int foo (); double goo (); int hoo ( int x ); // function pointer assignments int ( * fcnPtr1 )(){ & foo }; // \u5bf9 int ( * fcnPtr2 )(){ & goo }; // \u9519 -- \u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u6b63\u786e double ( * fcnPtr4 )(){ & goo }; // \u5bf9 fcnPtr1 = & hoo ; // \u9519 -- fcnPtr1 \u4e0d\u63a5\u53d7\u53c2\u6570\uff0c\u548c hoo() \u4e0d\u5339\u914d int ( * fcnPtr3 )( int ){ & hoo }; // \u5bf9 \u4e0e\u57fa\u672c\u7c7b\u578b\u4e0d\u540c\uff0cc++ \u4f1a\u5728\u9700\u8981\u65f6\u9690\u5f0f\u5730\u5c06\u51fd\u6570\u8f6c\u6362\u4e3a\u51fd\u6570\u6307\u9488(\u56e0\u6b64\u4f60==\u4e0d\u9700\u8981\u4f7f\u7528address-of\u64cd\u4f5c\u7b26(&)\u6765\u83b7\u53d6\u51fd\u6570\u7684\u5730\u5740==)\u3002\u4f46\u662f\uff0c\u5b83\u4e0d\u4f1a\u9690\u5f0f\u5730\u5c06\u51fd\u6570\u6307\u9488\u8f6c\u6362\u4e3a\u7a7a\u6307\u9488\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002 \u51fd\u6570\u6307\u9488\u53ef\u4ee5\u88ab\u521d\u59cb\u5316\u6216\u8d4b\u503c\u4e3a nullptr \uff1a 1 int ( * fcnptr )() { nullptr }; // \u5bf9","title":"\u5c06\u51fd\u6570\u8d4b\u503c\u7ed9\u51fd\u6570\u6307\u9488"},{"location":"12-1-function-pointers/#_3","text":"\u51fd\u6570\u6307\u9488\u7684\u53e6\u4e00\u4e2a\u4e3b\u8981\u529f\u80fd\u662f\u4f7f\u7528\u5b83\u6765\u8c03\u7528\u51fd\u6570\u3002\u901a\u8fc7\u51fd\u6570\u6307\u9488\u8c03\u7528\u51fd\u6570\u6709\u4e24\u4e2a\u65b9\u6cd5\uff0c\u4e00\u662f\u901a\u8fc7\u663e\u5f0f\u89e3\u5f15\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int foo ( int x ) { return x ; } int main () { int ( * fcnPtr )( int ){ & foo }; // Initialize fcnPtr with function foo ( * fcnPtr )( 5 ); // call function foo(5) through fcnPtr. return 0 ; } \u7b2c\u4e8c\u79cd\u65b9\u6cd5\u662f\u901a\u8fc7\u9690\u5f0f\u89e3\u5f15\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int foo ( int x ) { return x ; } int main () { int ( * fcnPtr )( int ){ & foo }; // Initialize fcnPtr with function foo fcnPtr ( 5 ); // call function foo(5) through fcnPtr. return 0 ; } \u5982\u4f60\u6240\u89c1\uff0c\u4f7f\u7528\u9690\u5f0f\u89e3\u5f15\u7528\u8c03\u7528\u51fd\u6570\u770b\u8d77\u6765\u548c\u6b63\u5e38\u7684\u51fd\u6570\u8c03\u7528\u4e00\u6a21\u4e00\u6837\uff0c\u56e0\u4e3a\u51fd\u6570\u540d\u672c\u6765\u5c31\u662f\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\uff01\u4f46\u662f\uff0c\u6709\u4e9b\u65e7\u7684\u7f16\u8bd1\u5668\u5e76\u4e0d\u652f\u6301\u8fd9\u79cd\u9690\u5f0f\u89e3\u5f15\u7528\u7684\u65b9\u5f0f\uff0c\u4f46\u662f\u73b0\u4ee3\u7f16\u8bd1\u5668\u90fd\u652f\u6301\u3002 \u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a\u9ed8\u8ba4\u5f62\u53c2\u4e0d\u9002\u7528\u4e8e\u901a\u8fc7\u51fd\u6570\u6307\u9488\u8c03\u7528\u7684\u51fd\u6570\u3002\u9ed8\u8ba4\u5f62\u53c2\u5728\u7f16\u8bd1\u65f6\u89e3\u6790(\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u4e3a\u9ed8\u8ba4\u5f62\u53c2\u63d0\u4f9b\u5b9e\u53c2\uff0c\u7f16\u8bd1\u5668\u5c06\u5728\u7f16\u8bd1\u4ee3\u7801\u65f6\u4e3a\u60a8\u63d0\u4f9b\u4e00\u4e2a\u5b9e\u53c2)\u3002\u4f46\u662f\uff0c\u51fd\u6570\u6307\u9488\u5728\u8fd0\u884c\u65f6\u89e3\u6790\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528\u51fd\u6570\u6307\u9488\u8fdb\u884c\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u65e0\u6cd5\u89e3\u6790\u9ed8\u8ba4\u5f62\u53c2\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5fc5\u987b\u663e\u5f0f\u5730\u4f20\u5165\u4efb\u4f55\u9ed8\u8ba4\u53c2\u6570\u7684\u503c\u3002 \u8fd8\u8981\u6ce8\u610f\uff0c\u56e0\u4e3a\u51fd\u6570\u6307\u9488\u53ef\u4ee5\u8bbe\u7f6e\u4e3anullptr\uff0c\u6240\u4ee5\u5728\u8c03\u7528\u51fd\u6570\u6307\u9488\u4e4b\u524d\uff0c\u6700\u597d\u901a\u8fc7\u65ad\u8a00\u6216\u6709\u6761\u4ef6\u6d4b\u8bd5\u5224\u65ad\u5b83\u662f\u5426\u662f\u7a7a\u6307\u9488\u3002\u5c31\u50cf\u666e\u901a\u6307\u9488\u4e00\u6837\uff0c\u5bf9\u7a7a\u51fd\u6570\u6307\u9488\u7684\u89e3\u5f15\u7528\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 int foo ( int x ) { return x ; } int main () { int ( * fcnPtr )( int ){ & foo }; // Initialize fcnPtr with function foo if ( fcnPtr ) // make sure fcnPtr isn't a null pointer fcnPtr ( 5 ); // otherwise this will lead to undefined behavior return 0 ; }","title":"\u4f7f\u7528\u51fd\u6570\u6307\u9488\u8c03\u7528\u51fd\u6570"},{"location":"12-1-function-pointers/#_4","text":"\u51fd\u6570\u6307\u9488\u6700\u5927\u7684\u7528\u9014\u5176\u5b9e\u662f\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\u3002\u8fd9\u79cd\u4f5c\u4e3a\u5b9e\u53c2\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\u7684\u51fd\u6570\uff0c\u79f0\u4e3a \u56de\u8c03\u51fd\u6570 \u3002 \u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u5047\u8bbe\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u6267\u884c\u4e00\u9879\u4efb\u52a1(\u4f8b\u5982\u5bf9\u6570\u7ec4\u6392\u5e8f)\uff0c\u4f46\u4f60\u5e0c\u671b\u7528\u6237\u80fd\u591f\u5b9a\u4e49\u5982\u4f55\u6267\u884c\u8be5\u4efb\u52a1\u7684\u7279\u5b9a\u90e8\u5206(\u4f8b\u5982\u6570\u7ec4\u662f\u6309\u5347\u5e8f\u8fd8\u662f\u964d\u5e8f\u6392\u5e8f)\u3002\u8ba9\u6211\u4eec\u4ed4\u7ec6\u770b\u770b\u8fd9\u4e2a\u95ee\u9898\u662f\u5982\u4f55\u5177\u4f53\u5e94\u7528\u4e8e\u6392\u5e8f\u7684\uff0c\u4f5c\u4e3a\u4e00\u4e2a\u53ef\u4ee5\u63a8\u5e7f\u5230\u5176\u4ed6\u7c7b\u4f3c\u95ee\u9898\u7684\u4f8b\u5b50\u3002 \u8bb8\u591a\u57fa\u4e8e\u6bd4\u8f83\u7684\u6392\u5e8f\u7b97\u6cd5\u90fd\u57fa\u4e8e\u7c7b\u4f3c\u7684\u6982\u5ff5\uff1a\u6392\u5e8f\u7b97\u6cd5\u904d\u5386\u5217\u8868\uff0c\u5bf9\u6570\u5b57\u5bf9\u8fdb\u884c\u6bd4\u8f83\uff0c\u5e76\u57fa\u4e8e\u8fd9\u4e9b\u6bd4\u8f83\u7684\u7ed3\u679c\u5bf9\u6570\u5b57\u8fdb\u884c\u91cd\u65b0\u6392\u5e8f\u3002\u56e0\u6b64\uff0c\u901a\u8fc7\u6539\u53d8\u6bd4\u8f83\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6539\u53d8\u7b97\u6cd5\u7684\u6392\u5e8f\u65b9\u5f0f\uff0c\u800c\u4e0d\u5f71\u54cd\u5176\u4ed6\u6392\u5e8f\u4ee3\u7801\u3002 \u4e0b\u9762\u662f\u4e4b\u524d\u7684\u9009\u62e9\u6392\u5e8f\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // for std::swap void SelectionSort ( int * array , int size ) { // Step through each element of the array for ( int startIndex { 0 }; startIndex < ( size - 1 ); ++ startIndex ) { // smallestIndex is the index of the smallest element we've encountered so far. int smallestIndex { startIndex }; // Look for smallest element remaining in the array (starting at startIndex+1) for ( int currentIndex { startIndex + 1 }; currentIndex < size ; ++ currentIndex ) { // If the current element is smaller than our previously found smallest if ( array [ smallestIndex ] > array [ currentIndex ]) // COMPARISON DONE HERE { // This is the new smallest number for this iteration smallestIndex = currentIndex ; } } // Swap our start element with our smallest element std :: swap ( array [ startIndex ], array [ smallestIndex ]); } } \u8ba9\u6211\u4eec\u7528\u4e00\u4e2a\u51fd\u6570\u6765\u66ff\u6362\u8fd9\u91cc\u7684\u6bd4\u8f83\u3002\u6bd4\u8f83\u51fd\u6570\u6bd4\u8f83\u4e24\u4e2a\u6574\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\u6765\u6307\u793a\u5143\u7d20\u662f\u5426\u5e94\u8be5\u4ea4\u6362\uff0c\u5b83\u770b\u8d77\u6765\u50cf\u8fd9\u6837: 1 2 3 4 bool ascending ( int x , int y ) { return x > y ; // swap if the first element is greater than the second } \u4f7f\u7528 ascending() \u51fd\u6570\u4fee\u6539\u540e\u7684\u6392\u5e8f\u7b97\u6cd5\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // for std::swap void SelectionSort ( int * array , int size ) { // Step through each element of the array for ( int startIndex { 0 }; startIndex < ( size - 1 ); ++ startIndex ) { // smallestIndex is the index of the smallest element we've encountered so far. int smallestIndex { startIndex }; // Look for smallest element remaining in the array (starting at startIndex+1) for ( int currentIndex { startIndex + 1 }; currentIndex < size ; ++ currentIndex ) { // If the current element is smaller than our previously found smallest if ( ascending ( array [ smallestIndex ], array [ currentIndex ])) // COMPARISON DONE HERE { // This is the new smallest number for this iteration smallestIndex = currentIndex ; } } // Swap our start element with our smallest element std :: swap ( array [ startIndex ], array [ smallestIndex ]); } } \u73b0\u5728\uff0c\u4e3a\u4e86\u8ba9\u8c03\u7528\u8005\u51b3\u5b9a\u5982\u4f55\u8fdb\u884c\u6392\u5e8f\uff0c\u6211\u4eec\u4e0d\u4f7f\u7528\u56fa\u5b9a\u7684\u6bd4\u8f83\u51fd\u6570\uff0c\u800c\u662f\u5141\u8bb8\u8c03\u7528\u8005\u63d0\u4f9b\u4ed6\u4eec\u81ea\u5df1\u7684\u6392\u5e8f\u51fd\u6570\uff01\u8fd9\u662f\u901a\u8fc7\u51fd\u6570\u6307\u9488\u5b8c\u6210\u7684\u3002 \u56e0\u4e3a\u8c03\u7528\u8005\u7684\u6bd4\u8f83\u51fd\u6570\u5c06\u6bd4\u8f83\u4e24\u4e2a\u6574\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u6240\u4ee5\u6307\u5411\u8fd9\u6837\u4e00\u4e2a\u51fd\u6570\u7684\u6307\u9488\u770b\u8d77\u6765\u50cf\u8fd9\u6837: 1 bool ( * comparisonFcn )( int , int ); \u56e0\u6b64\uff0c\u6211\u4eec\u5c06\u5141\u8bb8\u8c03\u7528\u8005\u5c06\u4e00\u4e2a\u6307\u5411\u4ed6\u4eec\u60f3\u8981\u7684\u6bd4\u8f83\u51fd\u6570\u7684\u6307\u9488\u4f5c\u4e3a\u7b2c\u4e09\u4e2a\u5f62\u53c2\u4f20\u9012\u7ed9\u6392\u5e8f\u4f8b\u7a0b\uff0c\u7136\u540e\u6211\u4eec\u5c06\u4f7f\u7528\u8c03\u7528\u8005\u7684\u51fd\u6570\u8fdb\u884c\u6bd4\u8f83\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u51fd\u6570\u6307\u9488\u5f62\u53c2\u8fdb\u884c\u7528\u6237\u5b9a\u4e49\u6bd4\u8f83\u7684\u9009\u62e9\u6392\u5e8f\u7684\u5b8c\u6574\u793a\u4f8b\uff0c\u4ee5\u53ca\u5982\u4f55\u8c03\u7528\u5b83\u7684\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 #include // for std::swap #include // Note our user-defined comparison is the third parameter void selectionSort ( int * array , int size , bool ( * comparisonFcn )( int , int )) { // Step through each element of the array for ( int startIndex { 0 }; startIndex < ( size - 1 ); ++ startIndex ) { // bestIndex is the index of the smallest/largest element we've encountered so far. int bestIndex { startIndex }; // Look for smallest/largest element remaining in the array (starting at startIndex+1) for ( int currentIndex { startIndex + 1 }; currentIndex < size ; ++ currentIndex ) { // If the current element is smaller/larger than our previously found smallest if ( comparisonFcn ( array [ bestIndex ], array [ currentIndex ])) // COMPARISON DONE HERE { // This is the new smallest/largest number for this iteration bestIndex = currentIndex ; } } // Swap our start element with our smallest/largest element std :: swap ( array [ startIndex ], array [ bestIndex ]); } } // Here is a comparison function that sorts in ascending order // (Note: it's exactly the same as the previous ascending() function) bool ascending ( int x , int y ) { return x > y ; // swap if the first element is greater than the second } // Here is a comparison function that sorts in descending order bool descending ( int x , int y ) { return x < y ; // swap if the second element is greater than the first } // This function prints out the values in the array void printArray ( int * array , int size ) { for ( int index { 0 }; index < size ; ++ index ) { std :: cout << array [ index ] << ' ' ; } std :: cout << '\\n' ; } int main () { int array [ 9 ]{ 3 , 7 , 9 , 5 , 6 , 1 , 8 , 2 , 4 }; // Sort the array in descending order using the descending() function selectionSort ( array , 9 , descending ); printArray ( array , 9 ); // Sort the array in ascending order using the ascending() function selectionSort ( array , 9 , ascending ); printArray ( array , 9 ); return 0 ; } \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 \u9177\u4e0d\u9177\uff1f\u6211\u4eec\u7ed9\u4e86\u8c03\u7528\u8005\u63a7\u5236\u9009\u62e9\u6392\u5e8f\u5982\u4f55\u5de5\u4f5c\u7684\u80fd\u529b\u3002 \u8c03\u7528\u8005\u751a\u81f3\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u201c\u5947\u602a\u7684\u201d\u6bd4\u8f83\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 bool evensFirst ( int x , int y ) { // if x is even and y is odd, x goes first (no swap needed) if (( x % 2 == 0 ) && ! ( y % 2 == 0 )) return false ; // if x is odd and y is even, y goes first (swap needed) if ( ! ( x % 2 == 0 ) && ( y % 2 == 0 )) return true ; // otherwise sort in ascending order return ascending ( x , y ); } int main () { int array [ 9 ]{ 3 , 7 , 9 , 5 , 6 , 1 , 8 , 2 , 4 }; selectionSort ( array , 9 , evensFirst ); printArray ( array , 9 ); return 0 ; } \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 4 6 8 1 3 5 7 9 \u5982\u4f60\u6240\u89c1\uff0c\u5728\u6b64\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\u51fd\u6570\u6307\u9488\u662f\u4e00\u79cd\u5f88\u597d\u65b9\u5f0f\uff0c\u8c03\u7528\u8005\u53ef\u4ee5\u5c06\u81ea\u5df1\u7684\u529f\u80fd\u201c\u6302\u94a9\u201d\u5230\u4f60\u4ee5\u524d\u7f16\u5199\u548c\u6d4b\u8bd5\u8fc7\u7684\u5185\u5bb9\u4e2d\uff0c\u8fd9\u6709\u52a9\u4e8e\u4fc3\u8fdb\u4ee3\u7801\u91cd\u7528\uff01\u4ee5\u524d\uff0c\u5982\u679c\u5e0c\u671b\u6309\u964d\u5e8f\u6392\u5217\u4e00\u4e2a\u6570\u7ec4\uff0c\u6309\u5347\u5e8f\u6392\u5217\u53e6\u4e00\u4e2a\u6570\u7ec4\uff0c\u5219\u9700\u8981\u591a\u4e2a\u7248\u672c\u7684\u6392\u5e8f\u4f8b\u7a0b\u3002\u73b0\u5728\u4f60\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7248\u672c\u5e76\u6309\u8c03\u7528\u8005\u5e0c\u671b\u7684\u4efb\u4f55\u65b9\u5f0f\u6392\u5e8f\uff01 \u6ce8\u610f\uff1a \u5982\u679c\u51fd\u6570\u5f62\u53c2\u662f\u51fd\u6570\u7c7b\u578b\u7684\uff0c\u5b83\u5c06\u88ab\u8f6c\u6362\u4e3a\u6307\u5411\u51fd\u6570\u7c7b\u578b\u7684\u6307\u9488 \u3002\u8fd9\u610f\u5473\u7740\uff1a 1 void selectionSort ( int * array , int size , bool ( * comparisonFcn )( int , int )) \u7b49\u4ef7\u4e8e\uff1a 1 void selectionSort ( int * array , int size , bool comparisonFcn ( int , int )) \u8fd9\u53ea\u9002\u7528\u4e8e\u51fd\u6570\u5f62\u53c2\uff0c\u800c\u4e0d\u9002\u7528\u4e8e\u72ec\u7acb\u7684\u51fd\u6570\u6307\u9488\uff0c\u56e0\u6b64\u4f7f\u7528\u6bd4\u8f83\u6709\u9650\u3002","title":"\u5c06\u51fd\u6570\u4f5c\u4e3a\u5b9e\u53c2\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570"},{"location":"12-1-function-pointers/#_5","text":"\u5982\u679c\u4f60\u8981\u5141\u8bb8\u8c03\u7528\u65b9\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\uff0c\u90a3\u4e48\u4e3a\u8c03\u7528\u65b9\u63d0\u4f9b\u4e00\u4e9b\u6807\u51c6\u51fd\u6570\u4ee5\u65b9\u4fbf\u4f7f\u7528\u901a\u5e38\u662f\u5f88\u6709\u7528\u7684\u3002\u4f8b\u5982\uff0c\u5728\u4e0a\u9762\u7684\u9009\u62e9\u6392\u5e8f\u793a\u4f8b\u4e2d\uff0c\u63d0\u4f9b ascending() \u548c descending() \u51fd\u6570\u4ee5\u53ca selectionSort() \u51fd\u6570\u5c06\u4f7f\u8c03\u7528\u8005\u7684\u5de5\u4f5c\u66f4\u52a0\u8f7b\u677e\uff0c\u56e0\u4e3a\u4ed6\u4eec\u4e0d\u5fc5\u6bcf\u6b21\u60f3\u8981\u4f7f\u7528 ascending() \u548c descending() \u51fd\u6570\u65f6\u90fd\u91cd\u5199\u5b83\u4eec\u3002 \u4f60\u751a\u81f3\u53ef\u4ee5\u8bbe\u7f6e\u5176\u4e2d\u4e00\u4e2a\u4f5c\u4e3a\u9ed8\u8ba4\u53c2\u6570\uff1a 1 2 // Default the sort to ascending sort void selectionSort ( int * array , int size , bool ( * comparisonFcn )( int , int ) = ascending ); \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u7528\u6237\u6b63\u5e38\u8c03\u7528 selectionSort \uff08\u4e0d\u4f7f\u7528\u51fd\u6570\u6307\u9488\uff09\uff0c comparisonFcn \u5f62\u53c2 \u9ed8\u8ba4\u662f\u5347\u5e8f\u7684\u3002\u4f60\u5fc5\u987b\u786e\u4fdd ascending \u51fd\u6570\u5728\u6b64\u4e4b\u524d\u88ab\u5b9a\u4e49\uff0c\u5426\u5219\u7f16\u8bd1\u5668\u5c06\u4f1a\u62a5\u544a ascending \u627e\u4e0d\u5230\u3002","title":"\u63d0\u4f9b\u9ed8\u8ba4\u51fd\u6570"},{"location":"12-1-function-pointers/#_6","text":"\u5b9e\u8bdd\u5b9e\u8bf4\u2014\u2014\u51fd\u6570\u6307\u9488\u7684\u8bed\u6cd5\u7684\u786e\u5f88\u96be\u770b\u3002\u4e0d\u8fc7\uff0c \u7c7b\u578b\u522b\u540d \u53ef\u4ee5\u7528\u6765\u4f7f\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\u770b\u8d77\u6765\u66f4\u50cf\u5e38\u89c4\u53d8\u91cf \uff1a 1 using ValidateFunction = bool ( * )( int , int ); \u7c7b\u578b\u522b\u540d \u201cValidateFunction\u201d \u662f\u4e00\u4e2a\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\uff0c\u8be5\u6307\u9488\u63a5\u53d7\u4e24\u4e2a\u6574\u578b\u5f62\u53c2\u5e76\u8fd4\u56de\u4e00\u4e2abool\u7c7b\u578b\u503c\u3002 \u6b64\u65f6\u6211\u4eec\u5c31\u53ef\u4ee5\u4e0d\u8fd9\u6837\u505a\uff1a 1 bool validate ( int x , int y , bool ( * fcnPtr )( int , int )); // \u4e11\u964b \u4f60\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 bool validate ( int x , int y , ValidateFunction pfcn ) // \u4f18\u96c5","title":"\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u8ba9\u51fd\u6570\u6307\u9488\u770b\u8d77\u6765\u66f4\u4f18\u96c5"},{"location":"12-1-function-pointers/#stdfunction","text":"\u5b9a\u4e49\u548c\u5b58\u50a8\u51fd\u6570\u6307\u9488\u8fd8\u6709\u4e00\u4e2a\u529e\u6cd5\uff0c\u5373\u4f7f\u7528 std::function \uff0c\u5b83\u5b9a\u4e49\u5728\u6807\u51c6\u5e93 \u5934\u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u5c06\u51fd\u6570\u6307\u9488\u5b9a\u4e49\u4e3a\u4e00\u4e2a std::function \u5bf9\u8c61\uff1a 1 2 #include bool validate ( int x , int y , std :: function < bool ( int , int ) > fcn ); // std::function method that returns a bool and takes two int parameters \u5982\u4f60\u6240\u89c1\uff0c\u8fd4\u56de\u503c\u7c7b\u578b\u5b9a\u4e49\u5728\u5c16\u62ec\u53f7\u4e2d\uff0c\u53c2\u6570\u7c7b\u578b\u5219\u88ab\u5b9a\u4e49\u5728\u62ec\u53f7\u4e2d\u3002\u5982\u679c\u6ca1\u6709\u53c2\u6570\u7684\u8bdd\uff0c\u62ec\u53f7\u53ef\u4ee5\u7559\u7a7a\u3002 \u4f7f\u7528 std::function \u66f4\u65b0\u4e4b\u524d\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include int foo () { return 5 ; } int goo () { return 6 ; } int main () { std :: function < int () > fcnPtr { & foo }; // declare function pointer that returns an int and takes no parameters fcnPtr = & goo ; // fcnPtr now points to function goo std :: cout << fcnPtr () << '\\n' ; // call the function just like normal return 0 ; } \u7c7b\u578b\u522b\u540d\u4e5f\u53ef\u4ee5\u5e2e\u52a9 std::function \u63d0\u9ad8\u53ef\u8bfb\u6027\uff1a 1 2 using ValidateFunctionRaw = bool ( * )( int , int ); // type alias to raw function pointer using ValidateFunction = std :: function < bool ( int , int ) > ; // type alias to std::function \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c std::function \u53ea\u5141\u8bb8\u901a\u8fc7\u9690\u5f0f\u89e3\u5f15\u7528(e.g. fcnPtr() )\u8c03\u7528\u51fd\u6570\uff0c\u800c\u4e0d\u652f\u6301\u663e\u793a\u89e3\u5f15\u7528\u8c03\u7528\u51fd\u6570 (e.g. (*fcnPtr)() )\u3002 C++17 \u4e2d \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD \u53ef\u4ee5\u7528\u6765\u4ece\u521d\u59cb\u5316\u503c\u76f4\u63a5\u63a8\u65ad std::function \u7684\u7c7b\u578b\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5199 std::function fcnPtr{ &foo }; \u800c\u4e0d\u9700\u8981\u5199 std::function fcnPtr{ &foo }; \uff0c\u8ba9\u7f16\u8bd1\u5668\u81ea\u5df1\u63a8\u65ad\u6a21\u677f\u53c2\u6570\u3002\u4f46\u662f CTAD \u5e76\u4e0d\u80fd\u5728\u5b9a\u4e49\u7c7b\u578b\u522b\u540d\u65f6\u4f7f\u7528\uff0c\u56e0\u4e3a\u6b64\u65f6\u6211\u4eec\u5e76\u6ca1\u6709\u63d0\u4f9b\u521d\u59cb\u5316\u503c\u3002","title":"\u4f7f\u7528 std::function"},{"location":"12-1-function-pointers/#_7","text":"auto \u5173\u952e\u5b57\u53ef\u4ee5\u63a8\u65ad\u666e\u901a\u53d8\u91cf\u7c7b\u578b\uff0c auto \u5173\u952e\u5b57\u4e5f\u53ef\u4ee5\u63a8\u65ad\u51fd\u6570\u6307\u9488\u7c7b\u578b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int foo ( int x ) { return x ; } int main () { auto fcnPtr { & foo }; std :: cout << fcnPtr ( 5 ) << '\\n' ; return 0 ; } \u8fd9\u5b8c\u5168\u7b26\u5408\u60a8\u7684\u9884\u671f\uff0c\u800c\u4e14\u8bed\u6cd5\u975e\u5e38\u7b80\u6d01\u3002\u5f53\u7136\uff0c\u7f3a\u70b9\u662f\u5173\u4e8e\u51fd\u6570\u7684\u5f62\u53c2\u7c7b\u578b\u548c\u8fd4\u56de\u7c7b\u578b\u7684\u6240\u6709\u7ec6\u8282\u90fd\u662f\u9690\u85cf\u7684\uff0c\u56e0\u6b64\u5728\u8c03\u7528\u51fd\u6570\u6216\u4f7f\u7528\u5176\u8fd4\u56de\u503c\u65f6\u5f88\u5bb9\u6613\u51fa\u9519\u3002","title":"\u51fd\u6570\u6307\u9488\u7684\u7c7b\u578b\u63a8\u65ad"},{"location":"12-1-function-pointers/#_8","text":"\u5982\u679c\u4f60\u5e0c\u671b\u5c06\u51fd\u6570\u5b58\u50a8\u5728\u6570\u7ec4(\u6216\u5176\u4ed6\u7ed3\u6784)\u4e2d\uff0c\u6216\u8005\u9700\u8981\u5c06\u51fd\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u65f6\uff0c\u51fd\u6570\u6307\u9488\u975e\u5e38\u6709\u7528\u3002\u56e0\u4e3a\u58f0\u660e\u51fd\u6570\u6307\u9488\u7684\u8bed\u6cd5\u5f88\u96be\u770b\u800c\u4e14\u5bb9\u6613\u51fa\u9519\uff0c\u6211\u4eec\u5efa\u8bae\u4f7f\u7528 std::function \u3002\u5728\u51fd\u6570\u6307\u9488\u7c7b\u578b\u53ea\u4f7f\u7528\u4e00\u6b21\u7684\u5730\u65b9(\u4f8b\u5982\u5355\u4e2a\u5f62\u53c2\u6216\u8fd4\u56de\u503c)\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 std::function \u3002\u5728\u591a\u6b21\u4f7f\u7528\u51fd\u6570\u6307\u9488\u7c7b\u578b\u7684\u5730\u65b9\uff0c\u521b\u5efa std::function \u7684\u7c7b\u578b\u522b\u540d\u5219\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002","title":"\u5c0f\u7ed3"},{"location":"12-2-the-stack-and-the-heap/","tags":["stack","heap"],"text":"Key Takeaway \u4e00\u4e2a\u7a0b\u5e8f\u7684\u5185\u5b58\u53ef\u4ee5\u88ab\u5206\u4e3a\u51e0\u4e2a\u4e0d\u540c\u7684\u533a\u57df\uff0c\u79f0\u4e3a \u5185\u5b58\u6bb5 \uff1a \u4ee3\u7801\u6bb5(code\u6bb5) (\u4e5f\u79f0\u4e3a text \u6bb5)\uff0c\u7f16\u8bd1\u540e\u7684\u7a0b\u5e8f\u5c31\u4f4d\u4e8e\u8be5\u6bb5\u3002\u4ee3\u7801\u6bb5\u901a\u5e38\u662f\u53ea\u8bfb\u7684\uff1b bss\u6bb5 (\u4e5f\u79f0\u4e3a\u672a\u521d\u59cb\u5316\u6570\u636e\u6bb5)\uff0c\u8fd9\u91cc\u5b58\u653e 0\u521d\u59cb\u5316 \u7684 \u5168\u5c40\u53d8\u91cf \u548c \u9759\u6001\u53d8\u91cf \uff1b \u6570\u636e\u6bb5 (\u4e5f\u79f0\u4e3a\u521d\u59cb\u5316\u6570\u636e\u6bb5)\uff0c\u8fd9\u91cc\u5b58\u653e\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\u548c\u9759\u6001\u53d8\u91cf\uff1b \u5806 \uff1a\u52a8\u6001\u53d8\u91cf\u7684\u5185\u5b58\u662f\u4ece\u5806\u4e2d\u5206\u914d\u7684\uff1b \u6808 \uff1a \u5f62\u53c2 \u3001\u5c40\u90e8\u53d8\u91cf\u548c\u5176\u4ed6\u51fd\u6570\u76f8\u5173\u7684\u4fe1\u606f\u90fd\u5b58\u653e\u5728\u8fd9\u91cc\u3002 \u672c\u8282\u8bfe\uff0c\u6211\u4eec\u5c06\u4e3b\u8981\u5173\u6ce8\u5806\u548c\u6808\uff0c\u5b83\u4eec\u662f\u8fd9\u51e0\u4e2a\u5185\u5b58\u6bb5\u4e2d\u6700\u201d\u6709\u610f\u601d\u201c\u7684\u3002 \u5806\u5185\u5b58\u6bb5 \u5806\u6bb5(\u4e5f\u79f0\u4e3a\u201c\u81ea\u7531\u5b58\u50a8\u533a\u201d)\u8ddf\u8e2a\u7528\u4e8e\u52a8\u6001\u5185\u5b58\u5206\u914d\u7684\u5185\u5b58\u3002\u6211\u4eec\u5df2\u7ecf\u5728 11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d \u4e2d\u4ecb\u7ecd\u4e86\u5806\uff0c\u6240\u4ee5\u8fd9\u91cc\u53ea\u662f\u590d\u4e60\u4e00\u4e0b\uff1a \u5728 C++ \u4e2d\uff0c\u5f53\u4f60\u4f7f\u7528 new \u5206\u914d\u5185\u5b58\u65f6\uff0c\u8be5\u5185\u5b58\u5c31\u4f1a\u4f4d\u4e8e\u5806\u6bb5\u3002 1 2 int * ptr { new int }; // ptr is assigned 4 bytes in the heap int * array { new int [ 10 ] }; // array is assigned 40 bytes in the heap \u8be5\u5185\u5b58\u7684\u5730\u5740\u901a\u8fc7\u64cd\u4f5c\u7b26new\u8fd4\u56de\uff0c\u7136\u540e\u53ef\u4ee5\u5b58\u50a8\u5728\u6307\u9488\u4e2d\u3002\u4f60\u4e0d\u5fc5\u62c5\u5fc3\u5b9a\u4f4d\u7a7a\u95f2\u5185\u5b58\u5e76\u5c06\u5176\u5206\u914d\u7ed9\u7528\u6237\u7684\u5e95\u5c42\u673a\u5236\u3002\u7136\u800c\uff0c\u4f60\u9700\u8981\u77e5\u9053\u7684\u662f\uff0c\u8fde\u7eed\u5185\u5b58\u8bf7\u6c42\u53ef\u80fd\u4e0d\u4f1a\u5206\u914d\u8fde\u7eed\u7684\u5185\u5b58\u5730\u5740\uff01 1 2 3 int * ptr1 { new int }; int * ptr2 { new int }; // ptr1 and ptr2 may not have sequential addresses \u5f53\u52a8\u6001\u5206\u914d\u7684\u53d8\u91cf\u88ab\u5220\u9664\u65f6\uff0c\u5185\u5b58\u88ab\u201c\u8fd4\u56de\u201d\u5230\u5806\u4e2d\uff0c\u7136\u540e\u53ef\u4ee5\u5728\u63a5\u6536\u5230\u672a\u6765\u7684\u5206\u914d\u8bf7\u6c42\u65f6\u91cd\u65b0\u5206\u914d\u3002\u8bf7\u8bb0\u4f4f\uff0c\u5220\u9664\u6307\u9488\u5e76\u4e0d\u5220\u9664\u53d8\u91cf\uff0c\u5b83\u53ea\u662f\u5c06\u76f8\u5173\u5730\u5740\u7684\u5185\u5b58\u8fd4\u56de\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u3002 \u5806\u6709\u4f18\u70b9\u548c\u7f3a\u70b9\uff1a \u5728\u5806\u4e0a\u5206\u914d\u5185\u5b58\u76f8\u5bf9\u5f88\u6162\uff1b \u5df2\u5206\u914d\u7684\u5185\u5b58\u4e00\u76f4\u4fdd\u6301\u5206\u914d\u72b6\u6001\uff0c\u76f4\u5230\u5b83\u88ab\u7279\u522b\u5730\u91ca\u653e(\u6ce8\u610f\u5185\u5b58\u6cc4\u6f0f)\u6216\u5e94\u7528\u7a0b\u5e8f\u7ed3\u675f(\u8fd9\u65f6\u64cd\u4f5c\u7cfb\u7edf\u5e94\u8be5\u6e05\u7406\u5b83)\uff1b \u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u5fc5\u987b\u901a\u8fc7\u6307\u9488\u8bbf\u95ee\u3002\u89e3\u5f15\u7528\u6307\u9488\u6bd4\u76f4\u63a5\u8bbf\u95ee\u53d8\u91cf\u6bd4\u8f83\u6162\uff1b \u56e0\u4e3a\u5806\u662f\u4e00\u4e2a\u5f88\u5927\u7684\u5185\u5b58\u6c60\uff0c\u6240\u4ee5\u53ef\u4ee5\u5728\u8fd9\u91cc\u5206\u914d\u5927\u578b\u6570\u7ec4\u3001\u7ed3\u6784\u6216\u7c7b\u3002 \u8c03\u7528\u6808 \u8c03\u7528\u6808 \u6240\u626e\u6f14\u7684\u89d2\u8272\u66f4\u52a0\u6709\u610f\u601d\u3002\u8c03\u7528\u6808\u4f1a\u8ffd\u8e2a\u6240\u6709\u6d3b\u52a8\u51fd\u6570\uff08\u5df2\u7ecf\u88ab\u8c03\u7528\u4f46\u5c1a\u672a\u7ed3\u675f\uff09\u4ece\u5f00\u59cb\u5230\u5f53\u524d\u65f6\u95f4\u70b9\u7684\u4fe1\u606f\uff0c\u5e76\u4e14\u8d1f\u8d23\u4e3a\u51fd\u6570 \u5f62\u53c2 \u548c\u5c40\u90e8\u53d8\u91cf\u5206\u914d\u5185\u5b58\u3002 \u8c03\u7528\u6808\u4f7f\u7528\u6570\u636e\u7ed3\u6784\u2014\u2014\u6808\u5b9e\u73b0\u3002\u6240\u4ee5\u5728\u8ba8\u8bba\u8c03\u7528\u6808\u7684\u5de5\u4f5c\u539f\u7406\u4e4b\u524d\uff0c\u6211\u4eec\u5fc5\u987b\u8981\u6307\u5b9a\u6808\u662f\u4e00\u79cd\u4ec0\u4e48\u6837\u7684\u6570\u636e\u7ed3\u6784\u3002 \u6570\u636e\u7ed3\u6784\u2014\u2014\u6808 \u6570\u636e\u7ed3\u6784 \u662f\u4e00\u79cd\u7ec4\u7ec7\u6570\u636e\u4ee5\u4fbf\u6709\u6548\u5730\u4f7f\u7528\u6570\u636e\u7684\u7f16\u7a0b\u673a\u5236\uff0c\u4f60\u5df2\u7ecf\u4e86\u89e3\u4e86\u51e0\u79cd\u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u6570\u7ec4\u548c\u7ed3\u6784\u4f53\u3002\u8fd9\u4e24\u79cd\u6570\u636e\u7ed3\u6784\u90fd\u63d0\u4f9b\u4e86\u6709\u6548\u5730\u5b58\u50a8\u6570\u636e\u548c\u8bbf\u95ee\u6570\u636e\u7684\u673a\u5236\u3002\u8fd8\u6709\u8bb8\u591a\u5728\u7f16\u7a0b\u4e2d\u7ecf\u5e38\u4f7f\u7528\u7684\u9644\u52a0\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u76f8\u5f53\u4e00\u90e8\u5206\u662f\u5728\u6807\u51c6\u5e93\u4e2d\u5b9e\u73b0\u7684\uff0c\u6808\u5c31\u662f\u5176\u4e2d\u4e4b\u4e00\u3002 \u60f3\u60f3\u81ea\u52a9\u9910\u5385\u91cc\u7684\u4e00\u53e0\u76d8\u5b50\u3002\u56e0\u4e3a\u6bcf\u4e2a\u76d8\u5b50\u90fd\u5f88\u91cd\uff0c\u800c\u4e14\u5b83\u4eec\u662f\u5806\u53e0\u7684\uff0c\u6240\u4ee5\u4f60\u5b9e\u9645\u4e0a\u53ea\u80fd\u505a\u4e09\u4ef6\u4e8b\u4e2d\u7684\u4e00\u4ef6: \u770b\u5230\u6700\u9876\u5c42\u76d8\u5b50\u7684\u8868\u9762\uff1b \u62ff\u8d70\u6700\u4e0a\u9762\u4e00\u4e2a\u76d8\u5b50\uff08\u6f0f\u51fa\u4e0b\u9762\u4e00\u4e2a\u76d8\u5b50\uff09\uff1b \u5c06\u4e00\u4e2a\u65b0\u7684\u76d8\u5b50\u653e\u5728\u6700\u4e0a\u9762\uff08\u906e\u853d\u4e0b\u9762\u4e00\u4e2a\u76d8\u5b50\uff09\u3002 \u5728\u8ba1\u7b97\u673a\u7f16\u7a0b\u4e2d\uff0c\u6808\u662f\u4e00\u79cd\u5bb9\u7eb3\u591a\u4e2a\u53d8\u91cf\u7684\u5bb9\u5668\u6570\u636e\u7ed3\u6784(\u5f88\u50cf\u6570\u7ec4)\u3002\u7136\u800c\uff0c\u6570\u7ec4\u5141\u8bb8\u4f60\u6309\u4efb\u4f55\u987a\u5e8f\u8bbf\u95ee\u548c\u4fee\u6539\u5143\u7d20(\u79f0\u4e3a \u968f\u673a\u8bbf\u95ee )\uff0c\u800c\u6808\u5219\u6709\u66f4\u591a\u7684\u9650\u5236\u3002\u53ef\u4ee5\u5728\u5806\u6808\u4e0a\u6267\u884c\u7684\u64cd\u4f5c\u5bf9\u5e94\u4e8e\u4e0a\u9762\u63d0\u5230\u7684\u4e09\u4ef6\u4e8b\uff1a \u67e5\u770b\u6808\u9876\u5143\u7d20(\u901a\u5e38\u901a\u8fc7 top() \u51fd\u6570\u6216 peek() )\uff1b \u83b7\u53d6\u5e76\u79fb\u9664\u6808\u9876\u5143\u7d20(\u901a\u8fc7 pop() \u5b8c\u6210)\uff1b \u5c06\u4e00\u4e2a\u65b0\u5143\u7d20\u653e\u7f6e\u5728\u6808\u9876(\u901a\u8fc7 push() \u5b8c\u6210)\u3002 \u6808\u662f\u4e00\u4e2a\u540e\u5165\u5148\u51fa(LIFO)\u7684\u6570\u636e\u7ed3\u6784\u3002\u6700\u540e\u88ab\u538b\u5165\u6808\u7684\u5143\u7d20\u4f1a\u7b2c\u4e00\u4e2a\u88ab\u5f39\u51fa\u3002\u5982\u679c\u4f60\u5c06\u4e00\u4e2a\u65b0\u7684\u76d8\u5b50\u653e\u5728\u76d8\u5b50\u5806\u9876\u4e0a\u7684\u8bdd\uff0c\u90a3\u4f60\u7b2c\u4e00\u4e2a\u62ff\u8d70\u7684\u76d8\u5b50\u4e5f\u53ea\u80fd\u662f\u5b83\u3002\u5f53\u65b0\u7684\u5143\u7d20\u88ab\u538b\u5165\u6808\u65f6\uff0c\u6808\u5c31\u4f1a\u589e\u957f\u3002\u5f53\u5143\u7d20\u88ab\u79fb\u9664\u65f6\uff0c\u5219\u6808\u4f1a\u6536\u7f29\u3002 \u4f8b\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u77ed\u7684\u5e8f\u5217\uff0c\u5c55\u793a\u4e86\u6808\u4e0a\u7684push\u548cpop\u662f\u5982\u4f55\u5de5\u4f5c\u7684: 1 2 3 4 5 6 7 8 9 10 11 Stack: empty Push 1 Stack: 1 Push 2 Stack: 1 2 Push 3 Stack: 1 2 3 Pop Stack: 1 2 Pop Stack: 1 \u5bf9\u4e8e\u8c03\u7528\u6808\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u76d8\u5b50\u7c7b\u6bd4\u662f\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u7c7b\u6bd4\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u505a\u4e00\u4e2a\u66f4\u597d\u7684\u7c7b\u6bd4\u3002\u8003\u8651\u4e00\u5806\u90ae\u7bb1\uff0c\u5b83\u4eec\u90fd\u5806\u53e0\u5728\u4e00\u8d77\u3002\u6bcf\u4e2a\u90ae\u7bb1\u53ea\u80fd\u88c5\u4e00\u4ef6\u7269\u54c1\uff0c\u800c\u4e14\u6240\u6709\u90ae\u7bb1\u90fd\u662f\u7a7a\u7684\u3002\u6b64\u5916\uff0c\u6bcf\u4e2a\u90ae\u7bb1\u90fd\u56fa\u5b9a\u5728\u5b83\u4e0b\u9762\u7684\u90ae\u7bb1\u4e0a\uff0c\u56e0\u6b64\u90ae\u7bb1\u7684\u6570\u91cf\u4e0d\u80fd\u66f4\u6539\u3002\u5982\u679c\u4e0d\u80fd\u66f4\u6539\u90ae\u7bb1\u7684\u6570\u91cf\uff0c\u5982\u4f55\u83b7\u5f97\u7c7b\u4f3c\u4e8e\u5806\u6808\u7684\u884c\u4e3a? \u9996\u5148\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u6807\u8bb0(\u6bd4\u5982\u4fbf\u5229\u8d34)\u6765\u8ddf\u8e2a\u6700\u4e0b\u9762\u7684\u7a7a\u90ae\u7bb1\u7684\u4f4d\u7f6e\u3002\u5728\u5f00\u59cb\u65f6\uff0c\u8fd9\u5c06\u662f\u6700\u4f4e\u7684\u90ae\u7bb1(\u5728\u5806\u6808\u7684\u5e95\u90e8)\u3002\u5f53\u6211\u4eec\u5c06\u4e00\u4e2a\u9879\u76ee\u63a8\u5165\u90ae\u7bb1\u5806\u6808\u65f6\uff0c\u6211\u4eec\u5c06\u5b83\u653e\u5165\u6709\u6807\u8bb0\u7684\u90ae\u7bb1\u4e2d(\u5373\u7b2c\u4e00\u4e2a\u7a7a\u90ae\u7bb1)\uff0c\u5e76\u5c06\u6807\u8bb0\u5411\u4e0a\u79fb\u52a8\u4e00\u4e2a\u90ae\u7bb1\u3002\u5f53\u6211\u4eec\u4ece\u5806\u6808\u4e2d\u5f39\u51fa\u4e00\u4e2a\u9879\u76ee\u65f6\uff0c\u6211\u4eec\u5c06\u6807\u8bb0\u5411\u4e0b\u79fb\u52a8\u4e00\u4e2a\u90ae\u7bb1(\u56e0\u6b64\u5b83\u6307\u5411\u9876\u90e8\u7684\u975e\u7a7a\u90ae\u7bb1)\uff0c\u5e76\u4ece\u8be5\u90ae\u7bb1\u4e2d\u5220\u9664\u8be5\u9879\u76ee\u3002\u4efb\u4f55\u4f4e\u4e8e\u6807\u8bb0\u7684\u4e1c\u897f\u90fd\u88ab\u8ba4\u4e3a\u662f\u201c\u5728\u6808\u4e0a\u201d\u3002\u6807\u8bb0\u5904\u6216\u6807\u8bb0\u4e0a\u65b9\u7684\u4efb\u4f55\u4e1c\u897f\u90fd\u4e0d\u5728\u6808\u4e0a\u3002 \u8c03\u7528\u6808\u5185\u5b58\u6bb5 \u8c03\u7528\u6808\u5185\u5b58\u6bb5\u4fdd\u5b58\u7528\u4e8e\u8c03\u7528\u6808\u7684\u5185\u5b58\u3002\u5f53\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\uff0c main() \u51fd\u6570\u88ab\u64cd\u4f5c\u7cfb\u7edf\u538b\u5230\u8c03\u7528\u6808\u4e0a\u3002\u7136\u540e\u7a0b\u5e8f\u5f00\u59cb\u6267\u884c\u3002 \u51fd\u6570\u8c03\u7528\u65f6\u5c06\u51fd\u6570\u538b\u5165\u8c03\u7528\u6808\u3002\u5f53\u5f53\u524d\u51fd\u6570\u7ed3\u675f\u65f6\uff0c\u8be5\u51fd\u6570\u4ece\u8c03\u7528\u6808\u4e2d\u5f39\u51fa\u3002\u56e0\u6b64\uff0c\u901a\u8fc7\u67e5\u770b\u538b\u5165\u8c03\u7528\u6808\u4e0a\u7684\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e3a\u5230\u8fbe\u5f53\u524d\u6267\u884c\u70b9\u800c\u8c03\u7528\u7684\u6240\u6709\u51fd\u6570\u3002 \u90ae\u7bb1\u7684\u6bd4\u55bb\u5f88\u597d\u5730\u63cf\u8ff0\u4e86\u8c03\u7528\u6808\u7684\u5de5\u4f5c\u539f\u7406\u3002\u6808\u672c\u8eab\u662f\u4e00\u6bb5\u56fa\u5b9a\u957f\u5ea6\u7684\u5185\u5b58\u3002\u90ae\u7bb1\u53ef\u4ee5\u88ab\u770b\u505a\u662f\u5185\u5b58\u5730\u5740\uff0c\u800c\u538b\u5165\u6216\u5f39\u51fa\u7684\u5185\u5bb9\u79f0\u4e3a \u6808\u5e27 \u3002\u6808\u5e27\u8ffd\u8e2a\u548c\u67d0\u4e2a\u51fd\u6570\u8c03\u7528\u76f8\u5173\u7684\u5168\u90e8\u6570\u636e\u3002\u6bd4\u55bb\u4e2d\u7684\u201c\u6807\u8bb0\u201d\u662f\u4e00\u4e2a\u88ab\u79f0\u4e3a\u5806\u6808\u6307\u9488(SP)\u7684\u5bc4\u5b58\u5668(CPU\u4e2d\u7684\u4e00\u5c0f\u5757\u5185\u5b58)\u3002\u5806\u6808\u6307\u9488\u7528\u4e8e\u8ffd\u8e2a\u5f53\u524d\u7684\u6808\u9876\u4f4d\u7f6e\u3002 \u6211\u4eec\u8fd8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\uff1a\u5f53\u4ece\u8c03\u7528\u6808\u4e2d\u5f39\u51fa\u5185\u5bb9\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5e76\u4e0d\u9700\u8981\u5b9e\u9645\u6e05\u7406\u6216\u6e05\u96f6\u88ab\u5f39\u51fa\u7684\u6808\u5e27\u2014\u2014\u53ea\u9700\u8981\u5c06\u6808\u9876\u6307\u9488\u5411\u4e0b\u79fb\u52a8\u5373\u53ef\u3002\u76f8\u5e94\u7684\u5185\u5bb9\u5df2\u7ecf\u4e0d\u88ab\u8ba4\u4e3a\u662f\u6808\u7684\u4e00\u90e8\u5206\u4e86\uff08\u6808\u6307\u9488\u4f1a\u5728\u8be5\u5730\u5740\u6216\u8be5\u5730\u5740\u7684\u4e0b\u65b9\uff09\uff0c\u6240\u4ee5\u5b83\u4e5f\u4e0d\u4f1a\u88ab\u5bfb\u5740\u3002\u5982\u679c\u7a0d\u540e\u6211\u4eec\u5c06\u65b0\u7684\u6808\u5e27\u538b\u5165\u4e86\u8be5\u5185\u5b58\u5730\u5740\uff0c\u5219\u5176\u5185\u5bb9\u6b63\u597d\u5c31\u88ab\u8986\u76d6\u6389\u4e86\u3002 \u8c03\u7528\u6808\u5b9e\u4f8b \u8ba9\u6211\u4eec\u66f4\u8be6\u7ec6\u5730\u7814\u7a76\u8c03\u7528\u6808\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u4e0b\u9762\u662f\u8c03\u7528\u51fd\u6570\u65f6\u7684\u6b65\u9aa4\uff1a \u7a0b\u5e8f\u4e2d\u53d1\u751f\u51fd\u6570\u8c03\u7528\uff1b \u6808\u5e27\u88ab\u6784\u5efa\u5e76\u538b\u5165\u8c03\u7528\u6808\u3002\u6808\u5e27\u5305\u62ec\uff1a \u51fd\u6570\u8c03\u7528\u540e\u6307\u4ee4\u7684\u5730\u5740\uff08\u8fd4\u56de\u5730\u5740\uff09\u3002CPU\u901a\u8fc7\u5b83\u77e5\u9053\u51fd\u6570\u9000\u51fa\u65f6\u5e94\u8be5\u8fd4\u56de\u5230\u7684\u5730\u5740\uff1b \u6240\u6709\u51fd\u6570\u7684\u5b9e\u53c2\uff1b \u6240\u6709\u5c40\u90e8\u53d8\u91cf\u7684\u5185\u5b58\uff1b \u4fdd\u5b58\u88ab\u51fd\u6570\u4fee\u6539\u4e14\u9700\u8981\u5728\u51fd\u6570\u8fd4\u56de\u65f6\u6062\u590d\u7684\u5bc4\u5b58\u5668 CPU \u8df3\u8f6c\u5230\u51fd\u6570\u8d77\u70b9\uff1b \u51fd\u6570\u4e2d\u7684\u6307\u4ee4\u5f00\u59cb\u88ab\u6267\u884c\u3002 \u5f53\u51fd\u6570\u7ed3\u675f\u65f6\uff0c\u4f1a\u6267\u884c\u4e0b\u9762\u7684\u6b65\u9aa4\uff1a \u5bc4\u5b58\u5668\u4ece\u8c03\u7528\u6808\u6062\u590d\uff1b \u6808\u5e27\u5f39\u51fa\u8c03\u7528\u6808\u3002\u6b64\u65f6\u4f1a\u91ca\u653e\u5c40\u90e8\u53d8\u91cf\u548c\u5b9e\u53c2\u7684\u5185\u5b58\uff1b \u5904\u7406\u8fd4\u56de\u503c\uff1b CPU\u7ee7\u7eed\u4ece\u8fd4\u56de\u5730\u5740\u6267\u884c\u3002 \u6839\u636e\u8ba1\u7b97\u673a\u7684\u4f53\u7cfb\u7ed3\u6784\u7684\u4e0d\u540c\uff0c\u53ef\u4ee5\u6709\u8bb8\u591a\u4e0d\u540c\u7684\u65b9\u6cd5\u5904\u7406\u8fd4\u56de\u503c\u3002\u6709\u4e9b\u4f53\u7cfb\u7ed3\u6784\u5c06\u8fd4\u56de\u503c\u4f5c\u4e3a\u5806\u6808\u6846\u67b6\u7684\u4e00\u90e8\u5206\uff0c\u5176\u4ed6\u5219\u4f7f\u7528CPU\u5bc4\u5b58\u5668\u3002 \u901a\u5e38\uff0c\u4e86\u89e3\u8c03\u7528\u5806\u6808\u5982\u4f55\u5de5\u4f5c\u7684\u6240\u6709\u7ec6\u8282\u5e76\u4e0d\u91cd\u8981\u3002\u4f46\u662f\uff0c\u4e86\u89e3\u51fd\u6570\u5728\u8c03\u7528\u65f6\u88ab\u6709\u6548\u5730\u63a8\u5165\u5806\u6808\uff0c\u5728\u8fd4\u56de\u65f6\u5f39\u51fa\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u4e86\u89e3\u9012\u5f52\u6240\u9700\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u4ee5\u53ca\u5728\u8c03\u8bd5\u65f6\u6709\u7528\u7684\u5176\u4ed6\u4e00\u4e9b\u6982\u5ff5\u3002 \u63d0\u793a\uff1a\u5728\u67d0\u4e9b\u4f53\u7cfb\u7ed3\u6784\u4e0a\uff0c\u8c03\u7528\u5806\u6808\u4ece\u5185\u5b58\u5730\u57400\u589e\u957f\u3002\u5728\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u5b83\u4f1a\u5411\u5185\u5b58\u5730\u57400\u589e\u957f\u3002\u56e0\u6b64\uff0c\u65b0\u63a8\u7684\u5806\u6808\u5e27\u53ef\u80fd\u6bd4\u4ee5\u524d\u7684\u6808\u5e27\u6709\u66f4\u9ad8\u6216\u66f4\u4f4e\u7684\u5185\u5b58\u5730\u5740\u3002 \u4e00\u4e2a\u7b80\u5355\u7684\u8c03\u7528\u6808\u4f8b\u5b50 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7b80\u5355\u7684\u5e94\u7528\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int foo ( int x ) { // b return x ; } // foo is popped off the call stack here int main () { // a foo ( 5 ); // foo is pushed on the call stack here // c return 0 ; } \u5728\u5bf9\u5e94\u8c03\u7528\u6808\u6807\u8bb0\u70b9\u5904\u770b\u8d77\u6765\u662f\u8fd9\u6837\u7684\uff1a a: 1 main() b: 1 2 foo() (including parameter x) main() c: 1 main() \u5806\u6808\u6ea2\u51fa \u6808\u7684\u5927\u5c0f\u662f\u6709\u9650\u7684\uff0c\u56e0\u6b64\u5b83\u53ea\u80fd\u4fdd\u5b58\u6709\u9650\u7684\u4fe1\u606f\u3002\u5bf9\u4e8eWindows\u4e0a\u7684Visual Studio \u6765\u8bf4\uff0c\u6808\u7684\u9ed8\u8ba4\u5927\u5c0f\u4e3a 1MB\u3002\u800c\u5bf9\u4e8eLinux\u4e0a\u7684 g++/Clang\uff0c\u6808\u7684\u9ed8\u8ba4\u5927\u5c0f\u4e3a8MB\u3002\u5982\u679c\u7a0b\u5e8f\u89c6\u56fe\u5728\u6808\u4e0a\u4fdd\u5b58\u8fc7\u591a\u7684\u4fe1\u606f\uff0c\u5c31\u4f1a\u5bfc\u81f4 \u5806\u6808\u6ea2\u51fa \u3002\u5f53\u6808\u4e0a\u7684\u6240\u6709\u5185\u5b58\u90fd\u88ab\u5206\u914d\u51fa\u53bb\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u5806\u6808\u6ea2\u51fa\u2014\u2014\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u518d\u6b21\u5206\u914d\u7684\u5185\u5b58\u5c31\u4f1a\u6ea2\u51fa\u5230\u5176\u4ed6\u5185\u5b58\u6bb5\u3002 \u5806\u6808\u6ea2\u51fa\u7684\u4e3b\u8981\u539f\u56e0\u662f\u5728\u6808\u4e0a\u5206\u914d\u4e86\u8fc7\u591a\u7684\u53d8\u91cf\uff0c\u6216\u8005\u6267\u884c\u4e86\u8fc7\u591a\u7684\u51fd\u6570\u8c03\u7528\uff08\u51fd\u6570A\u8c03\u7528\u51fd\u6570B\u8c03\u7528\u51fd\u6570C\u8c03\u7528\u51fd\u6570D\u7b49\u7b49\uff09\u3002\u5bf9\u4e8e\u73b0\u4ee3\u64cd\u4f5c\u7cfb\u7edf\u6765\u8bf4\uff0c\u5806\u6808\u6ea2\u51fa\u4f1a\u5bfc\u81f4\u64cd\u4f5c\u7cfb\u7edf\u62a5\u544a\u975e\u6cd5\u8bbf\u95ee\u5e76\u7ec8\u6b62\u7a0b\u5e8f\u3002 \u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\u5c31\u6709\u53ef\u80fd\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\u3002\u4f60\u53ef\u4ee5\u5728\u4f60\u7684\u7cfb\u7edf\u4e0a\u8bd5\u8bd5\u770b\u7a0b\u5e8f\u4f1a\u4e0d\u4f1a\u5d29\u6e83\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { int stack [ 10000000 ]; std :: cout << \"hi\" << stack [ 0 ]; // we'll use stack[0] here so the compiler won't optimize the array away return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u8bd5\u56fe\u5728\u5806\u6808\u4e0a\u5206\u914d\u4e00\u4e2a\u5de8\u5927\u7684(\u53ef\u80fd\u662f40MB)\u6570\u7ec4\u3002\u56e0\u4e3a\u5806\u6808\u4e0d\u591f\u5927\uff0c\u65e0\u6cd5\u5904\u7406\u8fd9\u4e2a\u6570\u7ec4\uff0c\u6570\u7ec4\u5206\u914d\u6ea2\u51fa\u5230\u7a0b\u5e8f\u4e0d\u5141\u8bb8\u4f7f\u7528\u7684\u5185\u5b58\u90e8\u5206\u3002 \u5728 Windows (Visual Studio) \u4e0a\uff0c\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 HelloWorld.exe (process 15916) exited with code -1073741571. -1073741571 \u7b49\u4e8e\u5341\u516d\u8fdb\u5236\u7684 c0000005 \uff0c\u5b83\u5728 Windows \u4e2d\u8868\u793a\u975e\u6cd5\u8bbf\u95ee\u3002\u6ce8\u610f\u201chi\u201d\u5e76\u6ca1\u6709\u88ab\u6253\u5370\u51fa\u6765\uff0c\u56e0\u4e3a\u8fd8\u6ca1\u5230\u8fd9\u4e00\u6b65\u7a0b\u5e8f\u5c31\u5d29\u6e83\u4e86\u3002 \u4e0b\u9762\u662f\u53e6\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5b83\u4f1a\u56e0\u4e3a\u4e0d\u540c\u7684\u539f\u56e0\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include void foo () { foo (); std :: cout << \"hi\" ; } int main () { foo (); return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6bcf\u6b21\u8c03\u7528\u51fd\u6570 foo() \u65f6\uff0c\u6808\u5e27\u90fd\u4f1a\u88ab\u63a8\u5165\u5806\u6808\u3002\u56e0\u4e3a foo() \u65e0\u9650\u5730\u8c03\u7528\u81ea\u8eab\uff0c\u6700\u7ec8\u6808\u5c06\u8017\u5c3d\u5185\u5b58\u5e76\u5bfc\u81f4\u6ea2\u51fa\u3002 \u6808\u7684\u4f18\u7f3a\u70b9\uff1a \u5728\u6808\u4e0a\u5206\u914d\u5185\u5b58\u76f8\u5bf9\u8f83\u5feb\uff1b \u5728\u6808\u4e0a\u5206\u914d\u7684\u5185\u5b58\u53ea\u8981\u5728\u4f5c\u7528\u57df\u5c31\u4f1a\u4e00\u76f4\u5728\u6808\u4e0a\uff0c\u5728\u5f39\u51fa\u6808\u65f6\u4f1a\u88ab\u91ca\u653e\uff1b \u6240\u6709\u5728\u6808\u4e0a\u5206\u914d\u7684\u5185\u5b58\u90fd\u662f\u5728\u7f16\u8bd1\u65f6\u5c31\u5df2\u77e5\u7684\uff0c\u56e0\u6b64\u8fd9\u4e9b\u5185\u5b58\u53ef\u4ee5\u901a\u8fc7\u53d8\u91cf\u76f4\u63a5\u8bbf\u95ee\uff1b \u56e0\u4e3a\u6808\u76f8\u5bf9\u6765\u8bf4\u5f88\u5c0f\uff0c\u6240\u4ee5\u5728\u6808\u4e0a\u5206\u914d\u5927\u91cf\u7684\u5185\u5b58\u662f\u4e0d\u660e\u667a\u7684\u3002\u8fd9\u5176\u4e2d\u5c31\u5305\u62ec\u521b\u5efa\u5f88\u5927\u7684\u5c40\u90e8\u53d8\u91cf\u6570\u7ec4\u6216\u5927\u578b\u7ed3\u6784\u4f53\u3002 \u4f5c\u8005\u6ce8 \u6b64\u8bc4\u8bba \u6709\u4e00\u4e9b\u9644\u52a0\u7684(\u7b80\u5316\u7684)\u4fe1\u606f\uff0c\u5173\u4e8e\u5806\u6808\u4e0a\u7684\u53d8\u91cf\u662f\u5982\u4f55\u5e03\u5c40\u7684\uff0c\u4ee5\u53ca\u5728\u8fd0\u884c\u65f6\u5982\u4f55\u63a5\u6536\u5b9e\u9645\u7684\u5185\u5b58\u5730\u5740\u3002","title":"12.2 - \u6808\u548c\u5806"},{"location":"12-2-the-stack-and-the-heap/#_1","text":"\u5806\u6bb5(\u4e5f\u79f0\u4e3a\u201c\u81ea\u7531\u5b58\u50a8\u533a\u201d)\u8ddf\u8e2a\u7528\u4e8e\u52a8\u6001\u5185\u5b58\u5206\u914d\u7684\u5185\u5b58\u3002\u6211\u4eec\u5df2\u7ecf\u5728 11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d \u4e2d\u4ecb\u7ecd\u4e86\u5806\uff0c\u6240\u4ee5\u8fd9\u91cc\u53ea\u662f\u590d\u4e60\u4e00\u4e0b\uff1a \u5728 C++ \u4e2d\uff0c\u5f53\u4f60\u4f7f\u7528 new \u5206\u914d\u5185\u5b58\u65f6\uff0c\u8be5\u5185\u5b58\u5c31\u4f1a\u4f4d\u4e8e\u5806\u6bb5\u3002 1 2 int * ptr { new int }; // ptr is assigned 4 bytes in the heap int * array { new int [ 10 ] }; // array is assigned 40 bytes in the heap \u8be5\u5185\u5b58\u7684\u5730\u5740\u901a\u8fc7\u64cd\u4f5c\u7b26new\u8fd4\u56de\uff0c\u7136\u540e\u53ef\u4ee5\u5b58\u50a8\u5728\u6307\u9488\u4e2d\u3002\u4f60\u4e0d\u5fc5\u62c5\u5fc3\u5b9a\u4f4d\u7a7a\u95f2\u5185\u5b58\u5e76\u5c06\u5176\u5206\u914d\u7ed9\u7528\u6237\u7684\u5e95\u5c42\u673a\u5236\u3002\u7136\u800c\uff0c\u4f60\u9700\u8981\u77e5\u9053\u7684\u662f\uff0c\u8fde\u7eed\u5185\u5b58\u8bf7\u6c42\u53ef\u80fd\u4e0d\u4f1a\u5206\u914d\u8fde\u7eed\u7684\u5185\u5b58\u5730\u5740\uff01 1 2 3 int * ptr1 { new int }; int * ptr2 { new int }; // ptr1 and ptr2 may not have sequential addresses \u5f53\u52a8\u6001\u5206\u914d\u7684\u53d8\u91cf\u88ab\u5220\u9664\u65f6\uff0c\u5185\u5b58\u88ab\u201c\u8fd4\u56de\u201d\u5230\u5806\u4e2d\uff0c\u7136\u540e\u53ef\u4ee5\u5728\u63a5\u6536\u5230\u672a\u6765\u7684\u5206\u914d\u8bf7\u6c42\u65f6\u91cd\u65b0\u5206\u914d\u3002\u8bf7\u8bb0\u4f4f\uff0c\u5220\u9664\u6307\u9488\u5e76\u4e0d\u5220\u9664\u53d8\u91cf\uff0c\u5b83\u53ea\u662f\u5c06\u76f8\u5173\u5730\u5740\u7684\u5185\u5b58\u8fd4\u56de\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u3002 \u5806\u6709\u4f18\u70b9\u548c\u7f3a\u70b9\uff1a \u5728\u5806\u4e0a\u5206\u914d\u5185\u5b58\u76f8\u5bf9\u5f88\u6162\uff1b \u5df2\u5206\u914d\u7684\u5185\u5b58\u4e00\u76f4\u4fdd\u6301\u5206\u914d\u72b6\u6001\uff0c\u76f4\u5230\u5b83\u88ab\u7279\u522b\u5730\u91ca\u653e(\u6ce8\u610f\u5185\u5b58\u6cc4\u6f0f)\u6216\u5e94\u7528\u7a0b\u5e8f\u7ed3\u675f(\u8fd9\u65f6\u64cd\u4f5c\u7cfb\u7edf\u5e94\u8be5\u6e05\u7406\u5b83)\uff1b \u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u5fc5\u987b\u901a\u8fc7\u6307\u9488\u8bbf\u95ee\u3002\u89e3\u5f15\u7528\u6307\u9488\u6bd4\u76f4\u63a5\u8bbf\u95ee\u53d8\u91cf\u6bd4\u8f83\u6162\uff1b \u56e0\u4e3a\u5806\u662f\u4e00\u4e2a\u5f88\u5927\u7684\u5185\u5b58\u6c60\uff0c\u6240\u4ee5\u53ef\u4ee5\u5728\u8fd9\u91cc\u5206\u914d\u5927\u578b\u6570\u7ec4\u3001\u7ed3\u6784\u6216\u7c7b\u3002","title":"\u5806\u5185\u5b58\u6bb5"},{"location":"12-2-the-stack-and-the-heap/#_2","text":"\u8c03\u7528\u6808 \u6240\u626e\u6f14\u7684\u89d2\u8272\u66f4\u52a0\u6709\u610f\u601d\u3002\u8c03\u7528\u6808\u4f1a\u8ffd\u8e2a\u6240\u6709\u6d3b\u52a8\u51fd\u6570\uff08\u5df2\u7ecf\u88ab\u8c03\u7528\u4f46\u5c1a\u672a\u7ed3\u675f\uff09\u4ece\u5f00\u59cb\u5230\u5f53\u524d\u65f6\u95f4\u70b9\u7684\u4fe1\u606f\uff0c\u5e76\u4e14\u8d1f\u8d23\u4e3a\u51fd\u6570 \u5f62\u53c2 \u548c\u5c40\u90e8\u53d8\u91cf\u5206\u914d\u5185\u5b58\u3002 \u8c03\u7528\u6808\u4f7f\u7528\u6570\u636e\u7ed3\u6784\u2014\u2014\u6808\u5b9e\u73b0\u3002\u6240\u4ee5\u5728\u8ba8\u8bba\u8c03\u7528\u6808\u7684\u5de5\u4f5c\u539f\u7406\u4e4b\u524d\uff0c\u6211\u4eec\u5fc5\u987b\u8981\u6307\u5b9a\u6808\u662f\u4e00\u79cd\u4ec0\u4e48\u6837\u7684\u6570\u636e\u7ed3\u6784\u3002","title":"\u8c03\u7528\u6808"},{"location":"12-2-the-stack-and-the-heap/#_3","text":"\u6570\u636e\u7ed3\u6784 \u662f\u4e00\u79cd\u7ec4\u7ec7\u6570\u636e\u4ee5\u4fbf\u6709\u6548\u5730\u4f7f\u7528\u6570\u636e\u7684\u7f16\u7a0b\u673a\u5236\uff0c\u4f60\u5df2\u7ecf\u4e86\u89e3\u4e86\u51e0\u79cd\u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u6570\u7ec4\u548c\u7ed3\u6784\u4f53\u3002\u8fd9\u4e24\u79cd\u6570\u636e\u7ed3\u6784\u90fd\u63d0\u4f9b\u4e86\u6709\u6548\u5730\u5b58\u50a8\u6570\u636e\u548c\u8bbf\u95ee\u6570\u636e\u7684\u673a\u5236\u3002\u8fd8\u6709\u8bb8\u591a\u5728\u7f16\u7a0b\u4e2d\u7ecf\u5e38\u4f7f\u7528\u7684\u9644\u52a0\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u76f8\u5f53\u4e00\u90e8\u5206\u662f\u5728\u6807\u51c6\u5e93\u4e2d\u5b9e\u73b0\u7684\uff0c\u6808\u5c31\u662f\u5176\u4e2d\u4e4b\u4e00\u3002 \u60f3\u60f3\u81ea\u52a9\u9910\u5385\u91cc\u7684\u4e00\u53e0\u76d8\u5b50\u3002\u56e0\u4e3a\u6bcf\u4e2a\u76d8\u5b50\u90fd\u5f88\u91cd\uff0c\u800c\u4e14\u5b83\u4eec\u662f\u5806\u53e0\u7684\uff0c\u6240\u4ee5\u4f60\u5b9e\u9645\u4e0a\u53ea\u80fd\u505a\u4e09\u4ef6\u4e8b\u4e2d\u7684\u4e00\u4ef6: \u770b\u5230\u6700\u9876\u5c42\u76d8\u5b50\u7684\u8868\u9762\uff1b \u62ff\u8d70\u6700\u4e0a\u9762\u4e00\u4e2a\u76d8\u5b50\uff08\u6f0f\u51fa\u4e0b\u9762\u4e00\u4e2a\u76d8\u5b50\uff09\uff1b \u5c06\u4e00\u4e2a\u65b0\u7684\u76d8\u5b50\u653e\u5728\u6700\u4e0a\u9762\uff08\u906e\u853d\u4e0b\u9762\u4e00\u4e2a\u76d8\u5b50\uff09\u3002 \u5728\u8ba1\u7b97\u673a\u7f16\u7a0b\u4e2d\uff0c\u6808\u662f\u4e00\u79cd\u5bb9\u7eb3\u591a\u4e2a\u53d8\u91cf\u7684\u5bb9\u5668\u6570\u636e\u7ed3\u6784(\u5f88\u50cf\u6570\u7ec4)\u3002\u7136\u800c\uff0c\u6570\u7ec4\u5141\u8bb8\u4f60\u6309\u4efb\u4f55\u987a\u5e8f\u8bbf\u95ee\u548c\u4fee\u6539\u5143\u7d20(\u79f0\u4e3a \u968f\u673a\u8bbf\u95ee )\uff0c\u800c\u6808\u5219\u6709\u66f4\u591a\u7684\u9650\u5236\u3002\u53ef\u4ee5\u5728\u5806\u6808\u4e0a\u6267\u884c\u7684\u64cd\u4f5c\u5bf9\u5e94\u4e8e\u4e0a\u9762\u63d0\u5230\u7684\u4e09\u4ef6\u4e8b\uff1a \u67e5\u770b\u6808\u9876\u5143\u7d20(\u901a\u5e38\u901a\u8fc7 top() \u51fd\u6570\u6216 peek() )\uff1b \u83b7\u53d6\u5e76\u79fb\u9664\u6808\u9876\u5143\u7d20(\u901a\u8fc7 pop() \u5b8c\u6210)\uff1b \u5c06\u4e00\u4e2a\u65b0\u5143\u7d20\u653e\u7f6e\u5728\u6808\u9876(\u901a\u8fc7 push() \u5b8c\u6210)\u3002 \u6808\u662f\u4e00\u4e2a\u540e\u5165\u5148\u51fa(LIFO)\u7684\u6570\u636e\u7ed3\u6784\u3002\u6700\u540e\u88ab\u538b\u5165\u6808\u7684\u5143\u7d20\u4f1a\u7b2c\u4e00\u4e2a\u88ab\u5f39\u51fa\u3002\u5982\u679c\u4f60\u5c06\u4e00\u4e2a\u65b0\u7684\u76d8\u5b50\u653e\u5728\u76d8\u5b50\u5806\u9876\u4e0a\u7684\u8bdd\uff0c\u90a3\u4f60\u7b2c\u4e00\u4e2a\u62ff\u8d70\u7684\u76d8\u5b50\u4e5f\u53ea\u80fd\u662f\u5b83\u3002\u5f53\u65b0\u7684\u5143\u7d20\u88ab\u538b\u5165\u6808\u65f6\uff0c\u6808\u5c31\u4f1a\u589e\u957f\u3002\u5f53\u5143\u7d20\u88ab\u79fb\u9664\u65f6\uff0c\u5219\u6808\u4f1a\u6536\u7f29\u3002 \u4f8b\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u77ed\u7684\u5e8f\u5217\uff0c\u5c55\u793a\u4e86\u6808\u4e0a\u7684push\u548cpop\u662f\u5982\u4f55\u5de5\u4f5c\u7684: 1 2 3 4 5 6 7 8 9 10 11 Stack: empty Push 1 Stack: 1 Push 2 Stack: 1 2 Push 3 Stack: 1 2 3 Pop Stack: 1 2 Pop Stack: 1 \u5bf9\u4e8e\u8c03\u7528\u6808\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u76d8\u5b50\u7c7b\u6bd4\u662f\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u7c7b\u6bd4\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u505a\u4e00\u4e2a\u66f4\u597d\u7684\u7c7b\u6bd4\u3002\u8003\u8651\u4e00\u5806\u90ae\u7bb1\uff0c\u5b83\u4eec\u90fd\u5806\u53e0\u5728\u4e00\u8d77\u3002\u6bcf\u4e2a\u90ae\u7bb1\u53ea\u80fd\u88c5\u4e00\u4ef6\u7269\u54c1\uff0c\u800c\u4e14\u6240\u6709\u90ae\u7bb1\u90fd\u662f\u7a7a\u7684\u3002\u6b64\u5916\uff0c\u6bcf\u4e2a\u90ae\u7bb1\u90fd\u56fa\u5b9a\u5728\u5b83\u4e0b\u9762\u7684\u90ae\u7bb1\u4e0a\uff0c\u56e0\u6b64\u90ae\u7bb1\u7684\u6570\u91cf\u4e0d\u80fd\u66f4\u6539\u3002\u5982\u679c\u4e0d\u80fd\u66f4\u6539\u90ae\u7bb1\u7684\u6570\u91cf\uff0c\u5982\u4f55\u83b7\u5f97\u7c7b\u4f3c\u4e8e\u5806\u6808\u7684\u884c\u4e3a? \u9996\u5148\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u6807\u8bb0(\u6bd4\u5982\u4fbf\u5229\u8d34)\u6765\u8ddf\u8e2a\u6700\u4e0b\u9762\u7684\u7a7a\u90ae\u7bb1\u7684\u4f4d\u7f6e\u3002\u5728\u5f00\u59cb\u65f6\uff0c\u8fd9\u5c06\u662f\u6700\u4f4e\u7684\u90ae\u7bb1(\u5728\u5806\u6808\u7684\u5e95\u90e8)\u3002\u5f53\u6211\u4eec\u5c06\u4e00\u4e2a\u9879\u76ee\u63a8\u5165\u90ae\u7bb1\u5806\u6808\u65f6\uff0c\u6211\u4eec\u5c06\u5b83\u653e\u5165\u6709\u6807\u8bb0\u7684\u90ae\u7bb1\u4e2d(\u5373\u7b2c\u4e00\u4e2a\u7a7a\u90ae\u7bb1)\uff0c\u5e76\u5c06\u6807\u8bb0\u5411\u4e0a\u79fb\u52a8\u4e00\u4e2a\u90ae\u7bb1\u3002\u5f53\u6211\u4eec\u4ece\u5806\u6808\u4e2d\u5f39\u51fa\u4e00\u4e2a\u9879\u76ee\u65f6\uff0c\u6211\u4eec\u5c06\u6807\u8bb0\u5411\u4e0b\u79fb\u52a8\u4e00\u4e2a\u90ae\u7bb1(\u56e0\u6b64\u5b83\u6307\u5411\u9876\u90e8\u7684\u975e\u7a7a\u90ae\u7bb1)\uff0c\u5e76\u4ece\u8be5\u90ae\u7bb1\u4e2d\u5220\u9664\u8be5\u9879\u76ee\u3002\u4efb\u4f55\u4f4e\u4e8e\u6807\u8bb0\u7684\u4e1c\u897f\u90fd\u88ab\u8ba4\u4e3a\u662f\u201c\u5728\u6808\u4e0a\u201d\u3002\u6807\u8bb0\u5904\u6216\u6807\u8bb0\u4e0a\u65b9\u7684\u4efb\u4f55\u4e1c\u897f\u90fd\u4e0d\u5728\u6808\u4e0a\u3002","title":"\u6570\u636e\u7ed3\u6784\u2014\u2014\u6808"},{"location":"12-2-the-stack-and-the-heap/#_4","text":"\u8c03\u7528\u6808\u5185\u5b58\u6bb5\u4fdd\u5b58\u7528\u4e8e\u8c03\u7528\u6808\u7684\u5185\u5b58\u3002\u5f53\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\uff0c main() \u51fd\u6570\u88ab\u64cd\u4f5c\u7cfb\u7edf\u538b\u5230\u8c03\u7528\u6808\u4e0a\u3002\u7136\u540e\u7a0b\u5e8f\u5f00\u59cb\u6267\u884c\u3002 \u51fd\u6570\u8c03\u7528\u65f6\u5c06\u51fd\u6570\u538b\u5165\u8c03\u7528\u6808\u3002\u5f53\u5f53\u524d\u51fd\u6570\u7ed3\u675f\u65f6\uff0c\u8be5\u51fd\u6570\u4ece\u8c03\u7528\u6808\u4e2d\u5f39\u51fa\u3002\u56e0\u6b64\uff0c\u901a\u8fc7\u67e5\u770b\u538b\u5165\u8c03\u7528\u6808\u4e0a\u7684\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e3a\u5230\u8fbe\u5f53\u524d\u6267\u884c\u70b9\u800c\u8c03\u7528\u7684\u6240\u6709\u51fd\u6570\u3002 \u90ae\u7bb1\u7684\u6bd4\u55bb\u5f88\u597d\u5730\u63cf\u8ff0\u4e86\u8c03\u7528\u6808\u7684\u5de5\u4f5c\u539f\u7406\u3002\u6808\u672c\u8eab\u662f\u4e00\u6bb5\u56fa\u5b9a\u957f\u5ea6\u7684\u5185\u5b58\u3002\u90ae\u7bb1\u53ef\u4ee5\u88ab\u770b\u505a\u662f\u5185\u5b58\u5730\u5740\uff0c\u800c\u538b\u5165\u6216\u5f39\u51fa\u7684\u5185\u5bb9\u79f0\u4e3a \u6808\u5e27 \u3002\u6808\u5e27\u8ffd\u8e2a\u548c\u67d0\u4e2a\u51fd\u6570\u8c03\u7528\u76f8\u5173\u7684\u5168\u90e8\u6570\u636e\u3002\u6bd4\u55bb\u4e2d\u7684\u201c\u6807\u8bb0\u201d\u662f\u4e00\u4e2a\u88ab\u79f0\u4e3a\u5806\u6808\u6307\u9488(SP)\u7684\u5bc4\u5b58\u5668(CPU\u4e2d\u7684\u4e00\u5c0f\u5757\u5185\u5b58)\u3002\u5806\u6808\u6307\u9488\u7528\u4e8e\u8ffd\u8e2a\u5f53\u524d\u7684\u6808\u9876\u4f4d\u7f6e\u3002 \u6211\u4eec\u8fd8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\uff1a\u5f53\u4ece\u8c03\u7528\u6808\u4e2d\u5f39\u51fa\u5185\u5bb9\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5e76\u4e0d\u9700\u8981\u5b9e\u9645\u6e05\u7406\u6216\u6e05\u96f6\u88ab\u5f39\u51fa\u7684\u6808\u5e27\u2014\u2014\u53ea\u9700\u8981\u5c06\u6808\u9876\u6307\u9488\u5411\u4e0b\u79fb\u52a8\u5373\u53ef\u3002\u76f8\u5e94\u7684\u5185\u5bb9\u5df2\u7ecf\u4e0d\u88ab\u8ba4\u4e3a\u662f\u6808\u7684\u4e00\u90e8\u5206\u4e86\uff08\u6808\u6307\u9488\u4f1a\u5728\u8be5\u5730\u5740\u6216\u8be5\u5730\u5740\u7684\u4e0b\u65b9\uff09\uff0c\u6240\u4ee5\u5b83\u4e5f\u4e0d\u4f1a\u88ab\u5bfb\u5740\u3002\u5982\u679c\u7a0d\u540e\u6211\u4eec\u5c06\u65b0\u7684\u6808\u5e27\u538b\u5165\u4e86\u8be5\u5185\u5b58\u5730\u5740\uff0c\u5219\u5176\u5185\u5bb9\u6b63\u597d\u5c31\u88ab\u8986\u76d6\u6389\u4e86\u3002","title":"\u8c03\u7528\u6808\u5185\u5b58\u6bb5"},{"location":"12-2-the-stack-and-the-heap/#_5","text":"\u8ba9\u6211\u4eec\u66f4\u8be6\u7ec6\u5730\u7814\u7a76\u8c03\u7528\u6808\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u4e0b\u9762\u662f\u8c03\u7528\u51fd\u6570\u65f6\u7684\u6b65\u9aa4\uff1a \u7a0b\u5e8f\u4e2d\u53d1\u751f\u51fd\u6570\u8c03\u7528\uff1b \u6808\u5e27\u88ab\u6784\u5efa\u5e76\u538b\u5165\u8c03\u7528\u6808\u3002\u6808\u5e27\u5305\u62ec\uff1a \u51fd\u6570\u8c03\u7528\u540e\u6307\u4ee4\u7684\u5730\u5740\uff08\u8fd4\u56de\u5730\u5740\uff09\u3002CPU\u901a\u8fc7\u5b83\u77e5\u9053\u51fd\u6570\u9000\u51fa\u65f6\u5e94\u8be5\u8fd4\u56de\u5230\u7684\u5730\u5740\uff1b \u6240\u6709\u51fd\u6570\u7684\u5b9e\u53c2\uff1b \u6240\u6709\u5c40\u90e8\u53d8\u91cf\u7684\u5185\u5b58\uff1b \u4fdd\u5b58\u88ab\u51fd\u6570\u4fee\u6539\u4e14\u9700\u8981\u5728\u51fd\u6570\u8fd4\u56de\u65f6\u6062\u590d\u7684\u5bc4\u5b58\u5668 CPU \u8df3\u8f6c\u5230\u51fd\u6570\u8d77\u70b9\uff1b \u51fd\u6570\u4e2d\u7684\u6307\u4ee4\u5f00\u59cb\u88ab\u6267\u884c\u3002 \u5f53\u51fd\u6570\u7ed3\u675f\u65f6\uff0c\u4f1a\u6267\u884c\u4e0b\u9762\u7684\u6b65\u9aa4\uff1a \u5bc4\u5b58\u5668\u4ece\u8c03\u7528\u6808\u6062\u590d\uff1b \u6808\u5e27\u5f39\u51fa\u8c03\u7528\u6808\u3002\u6b64\u65f6\u4f1a\u91ca\u653e\u5c40\u90e8\u53d8\u91cf\u548c\u5b9e\u53c2\u7684\u5185\u5b58\uff1b \u5904\u7406\u8fd4\u56de\u503c\uff1b CPU\u7ee7\u7eed\u4ece\u8fd4\u56de\u5730\u5740\u6267\u884c\u3002 \u6839\u636e\u8ba1\u7b97\u673a\u7684\u4f53\u7cfb\u7ed3\u6784\u7684\u4e0d\u540c\uff0c\u53ef\u4ee5\u6709\u8bb8\u591a\u4e0d\u540c\u7684\u65b9\u6cd5\u5904\u7406\u8fd4\u56de\u503c\u3002\u6709\u4e9b\u4f53\u7cfb\u7ed3\u6784\u5c06\u8fd4\u56de\u503c\u4f5c\u4e3a\u5806\u6808\u6846\u67b6\u7684\u4e00\u90e8\u5206\uff0c\u5176\u4ed6\u5219\u4f7f\u7528CPU\u5bc4\u5b58\u5668\u3002 \u901a\u5e38\uff0c\u4e86\u89e3\u8c03\u7528\u5806\u6808\u5982\u4f55\u5de5\u4f5c\u7684\u6240\u6709\u7ec6\u8282\u5e76\u4e0d\u91cd\u8981\u3002\u4f46\u662f\uff0c\u4e86\u89e3\u51fd\u6570\u5728\u8c03\u7528\u65f6\u88ab\u6709\u6548\u5730\u63a8\u5165\u5806\u6808\uff0c\u5728\u8fd4\u56de\u65f6\u5f39\u51fa\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u4e86\u89e3\u9012\u5f52\u6240\u9700\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u4ee5\u53ca\u5728\u8c03\u8bd5\u65f6\u6709\u7528\u7684\u5176\u4ed6\u4e00\u4e9b\u6982\u5ff5\u3002 \u63d0\u793a\uff1a\u5728\u67d0\u4e9b\u4f53\u7cfb\u7ed3\u6784\u4e0a\uff0c\u8c03\u7528\u5806\u6808\u4ece\u5185\u5b58\u5730\u57400\u589e\u957f\u3002\u5728\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u5b83\u4f1a\u5411\u5185\u5b58\u5730\u57400\u589e\u957f\u3002\u56e0\u6b64\uff0c\u65b0\u63a8\u7684\u5806\u6808\u5e27\u53ef\u80fd\u6bd4\u4ee5\u524d\u7684\u6808\u5e27\u6709\u66f4\u9ad8\u6216\u66f4\u4f4e\u7684\u5185\u5b58\u5730\u5740\u3002","title":"\u8c03\u7528\u6808\u5b9e\u4f8b"},{"location":"12-2-the-stack-and-the-heap/#_6","text":"\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7b80\u5355\u7684\u5e94\u7528\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int foo ( int x ) { // b return x ; } // foo is popped off the call stack here int main () { // a foo ( 5 ); // foo is pushed on the call stack here // c return 0 ; } \u5728\u5bf9\u5e94\u8c03\u7528\u6808\u6807\u8bb0\u70b9\u5904\u770b\u8d77\u6765\u662f\u8fd9\u6837\u7684\uff1a a: 1 main() b: 1 2 foo() (including parameter x) main() c: 1 main()","title":"\u4e00\u4e2a\u7b80\u5355\u7684\u8c03\u7528\u6808\u4f8b\u5b50"},{"location":"12-2-the-stack-and-the-heap/#_7","text":"\u6808\u7684\u5927\u5c0f\u662f\u6709\u9650\u7684\uff0c\u56e0\u6b64\u5b83\u53ea\u80fd\u4fdd\u5b58\u6709\u9650\u7684\u4fe1\u606f\u3002\u5bf9\u4e8eWindows\u4e0a\u7684Visual Studio \u6765\u8bf4\uff0c\u6808\u7684\u9ed8\u8ba4\u5927\u5c0f\u4e3a 1MB\u3002\u800c\u5bf9\u4e8eLinux\u4e0a\u7684 g++/Clang\uff0c\u6808\u7684\u9ed8\u8ba4\u5927\u5c0f\u4e3a8MB\u3002\u5982\u679c\u7a0b\u5e8f\u89c6\u56fe\u5728\u6808\u4e0a\u4fdd\u5b58\u8fc7\u591a\u7684\u4fe1\u606f\uff0c\u5c31\u4f1a\u5bfc\u81f4 \u5806\u6808\u6ea2\u51fa \u3002\u5f53\u6808\u4e0a\u7684\u6240\u6709\u5185\u5b58\u90fd\u88ab\u5206\u914d\u51fa\u53bb\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u5806\u6808\u6ea2\u51fa\u2014\u2014\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u518d\u6b21\u5206\u914d\u7684\u5185\u5b58\u5c31\u4f1a\u6ea2\u51fa\u5230\u5176\u4ed6\u5185\u5b58\u6bb5\u3002 \u5806\u6808\u6ea2\u51fa\u7684\u4e3b\u8981\u539f\u56e0\u662f\u5728\u6808\u4e0a\u5206\u914d\u4e86\u8fc7\u591a\u7684\u53d8\u91cf\uff0c\u6216\u8005\u6267\u884c\u4e86\u8fc7\u591a\u7684\u51fd\u6570\u8c03\u7528\uff08\u51fd\u6570A\u8c03\u7528\u51fd\u6570B\u8c03\u7528\u51fd\u6570C\u8c03\u7528\u51fd\u6570D\u7b49\u7b49\uff09\u3002\u5bf9\u4e8e\u73b0\u4ee3\u64cd\u4f5c\u7cfb\u7edf\u6765\u8bf4\uff0c\u5806\u6808\u6ea2\u51fa\u4f1a\u5bfc\u81f4\u64cd\u4f5c\u7cfb\u7edf\u62a5\u544a\u975e\u6cd5\u8bbf\u95ee\u5e76\u7ec8\u6b62\u7a0b\u5e8f\u3002 \u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\u5c31\u6709\u53ef\u80fd\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\u3002\u4f60\u53ef\u4ee5\u5728\u4f60\u7684\u7cfb\u7edf\u4e0a\u8bd5\u8bd5\u770b\u7a0b\u5e8f\u4f1a\u4e0d\u4f1a\u5d29\u6e83\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { int stack [ 10000000 ]; std :: cout << \"hi\" << stack [ 0 ]; // we'll use stack[0] here so the compiler won't optimize the array away return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u8bd5\u56fe\u5728\u5806\u6808\u4e0a\u5206\u914d\u4e00\u4e2a\u5de8\u5927\u7684(\u53ef\u80fd\u662f40MB)\u6570\u7ec4\u3002\u56e0\u4e3a\u5806\u6808\u4e0d\u591f\u5927\uff0c\u65e0\u6cd5\u5904\u7406\u8fd9\u4e2a\u6570\u7ec4\uff0c\u6570\u7ec4\u5206\u914d\u6ea2\u51fa\u5230\u7a0b\u5e8f\u4e0d\u5141\u8bb8\u4f7f\u7528\u7684\u5185\u5b58\u90e8\u5206\u3002 \u5728 Windows (Visual Studio) \u4e0a\uff0c\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 HelloWorld.exe (process 15916) exited with code -1073741571. -1073741571 \u7b49\u4e8e\u5341\u516d\u8fdb\u5236\u7684 c0000005 \uff0c\u5b83\u5728 Windows \u4e2d\u8868\u793a\u975e\u6cd5\u8bbf\u95ee\u3002\u6ce8\u610f\u201chi\u201d\u5e76\u6ca1\u6709\u88ab\u6253\u5370\u51fa\u6765\uff0c\u56e0\u4e3a\u8fd8\u6ca1\u5230\u8fd9\u4e00\u6b65\u7a0b\u5e8f\u5c31\u5d29\u6e83\u4e86\u3002 \u4e0b\u9762\u662f\u53e6\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5b83\u4f1a\u56e0\u4e3a\u4e0d\u540c\u7684\u539f\u56e0\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include void foo () { foo (); std :: cout << \"hi\" ; } int main () { foo (); return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6bcf\u6b21\u8c03\u7528\u51fd\u6570 foo() \u65f6\uff0c\u6808\u5e27\u90fd\u4f1a\u88ab\u63a8\u5165\u5806\u6808\u3002\u56e0\u4e3a foo() \u65e0\u9650\u5730\u8c03\u7528\u81ea\u8eab\uff0c\u6700\u7ec8\u6808\u5c06\u8017\u5c3d\u5185\u5b58\u5e76\u5bfc\u81f4\u6ea2\u51fa\u3002 \u6808\u7684\u4f18\u7f3a\u70b9\uff1a \u5728\u6808\u4e0a\u5206\u914d\u5185\u5b58\u76f8\u5bf9\u8f83\u5feb\uff1b \u5728\u6808\u4e0a\u5206\u914d\u7684\u5185\u5b58\u53ea\u8981\u5728\u4f5c\u7528\u57df\u5c31\u4f1a\u4e00\u76f4\u5728\u6808\u4e0a\uff0c\u5728\u5f39\u51fa\u6808\u65f6\u4f1a\u88ab\u91ca\u653e\uff1b \u6240\u6709\u5728\u6808\u4e0a\u5206\u914d\u7684\u5185\u5b58\u90fd\u662f\u5728\u7f16\u8bd1\u65f6\u5c31\u5df2\u77e5\u7684\uff0c\u56e0\u6b64\u8fd9\u4e9b\u5185\u5b58\u53ef\u4ee5\u901a\u8fc7\u53d8\u91cf\u76f4\u63a5\u8bbf\u95ee\uff1b \u56e0\u4e3a\u6808\u76f8\u5bf9\u6765\u8bf4\u5f88\u5c0f\uff0c\u6240\u4ee5\u5728\u6808\u4e0a\u5206\u914d\u5927\u91cf\u7684\u5185\u5b58\u662f\u4e0d\u660e\u667a\u7684\u3002\u8fd9\u5176\u4e2d\u5c31\u5305\u62ec\u521b\u5efa\u5f88\u5927\u7684\u5c40\u90e8\u53d8\u91cf\u6570\u7ec4\u6216\u5927\u578b\u7ed3\u6784\u4f53\u3002 \u4f5c\u8005\u6ce8 \u6b64\u8bc4\u8bba \u6709\u4e00\u4e9b\u9644\u52a0\u7684(\u7b80\u5316\u7684)\u4fe1\u606f\uff0c\u5173\u4e8e\u5806\u6808\u4e0a\u7684\u53d8\u91cf\u662f\u5982\u4f55\u5e03\u5c40\u7684\uff0c\u4ee5\u53ca\u5728\u8fd0\u884c\u65f6\u5982\u4f55\u63a5\u6536\u5b9e\u9645\u7684\u5185\u5b58\u5730\u5740\u3002","title":"\u5806\u6808\u6ea2\u51fa"},{"location":"12-3-std-vector-capacity-and-stack-behavior/","tags":["vector","stack"],"text":"Key Takeaway \u5728 11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86 std::vector \u4ee5\u53ca\u5982\u4f55\u5c06\u5176\u5f53\u505a\u52a8\u6001\u6570\u7ec4\u4f7f\u7528\uff08\u53ef\u4ee5\u8bb0\u5f55\u81ea\u8eab\u957f\u5ea6\u4e5f\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u5927\u5c0f\uff09\u3002 \u5c3d\u7ba1\u8fd9\u662f std::vector \u6700\u5e38\u7528\u7684\u529f\u80fd\uff0c std::vector \u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u5176\u4ed6\u7684\u5c5e\u6027\u548c\u529f\u80fd\u4f7f\u5176\u5728\u522b\u7684\u65b9\u9762\u4e5f\u5f88\u6709\u7528\u3002 \u957f\u5ea6\u548c\u5bb9\u91cf \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 int * array { new int [ 10 ] { 1 , 2 , 3 , 4 , 5 } }; \u6211\u4eec\u4f1a\u8bf4\u8fd9\u4e2a\u6570\u7ec4\u7684\u957f\u5ea6\u662f10\uff0c\u5373\u4f7f\u6211\u4eec\u53ea\u4f7f\u7528\u4e86\u5206\u914d\u7684\u5143\u7d20\u4e2d\u76845\u4e2a\u3002 \u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u53ea\u60f3\u904d\u5386\u5df2\u7ecf\u521d\u59cb\u5316\u7684\u5143\u7d20\uff0c\u800c\u4fdd\u7559\u672a\u4f7f\u7528\u7684\u5143\u7d20\u4ee5\u5907\u5c06\u6765\u6269\u5c55\u5462\uff1f\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u5206\u522b\u8ddf\u8e2a\u201c\u4f7f\u7528\u201d\u4e86\u591a\u5c11\u5143\u7d20\u548c\u5206\u914d\u4e86\u591a\u5c11\u5143\u7d20\u3002\u4e0e\u53ea\u8bb0\u4f4f\u5176\u957f\u5ea6\u7684\u5185\u7f6e\u6570\u7ec4\u6216 std::array \u4e0d\u540c\uff0c std::vector \u5305\u542b\u4e24\u4e2a\u72ec\u7acb\u7684\u5c5e\u6027\uff1a\u957f\u5ea6\u548c\u5bb9\u91cf\u3002\u5728 std::vector \u7684\u8bed\u5883\u4e2d\uff0c length \u662f\u6570\u7ec4\u4e2d\u4f7f\u7528\u7684\u5143\u7d20\u6570\u91cf\uff0c\u800c capacity \u662f\u5185\u5b58\u4e2d\u5206\u914d\u7684\u5143\u7d20\u6570\u91cf\u3002 \u8ba9\u6211\u4eec\u518d\u770b\u4e00\u4e0b\u4e4b\u524d\u7684\u4e00\u4e2a std::vector \u7684\u4e00\u4e2a\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: vector array { 0 , 1 , 2 }; array . resize ( 5 ); // set length to 5 std :: cout << \"The length is: \" << array . size () << '\\n' ; for ( auto element : array ) std :: cout << element << ' ' ; return 0 ; }; 1 2 The length is: 5 0 1 2 0 0 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 resize() \u51fd\u6570\u5c06vector\u7684\u957f\u5ea6\u8bbe\u7f6e\u4e3a5\u3002\u8fd9\u544a\u8bc9\u53d8\u91cf\u6570\u7ec4\u6211\u4eec\u6253\u7b97\u4f7f\u7528\u6570\u7ec4\u7684\u524d5\u4e2a\u5143\u7d20\uff0c\u6240\u4ee5\u5b83\u5e94\u8be5\u628a\u8fd9\u4e9b\u5143\u7d20\u5f53\u505a\u6d3b\u52a8\u7684\u3002\u7136\u800c\uff0c\u8fd9\u5e26\u6765\u4e86\u4e00\u4e2a\u6709\u8da3\u7684\u95ee\u9898\uff1a\u8fd9\u4e2a\u6570\u7ec4\u7684\u5bb9\u91cf\u662f\u591a\u5c11? \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 capacity() \u51fd\u6570\u67e5\u8be2 std::vector \u7684\u5bb9\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include #include int main () { std :: vector array { 0 , 1 , 2 }; array . resize ( 5 ); // set length to 5 std :: cout << \"The length is: \" << array . size () << '\\n' ; std :: cout << \"The capacity is: \" << array . capacity () << '\\n' ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\uff0c\u8f93\u51fa\u5982\u4e0b\uff1a 1 2 The length is: 5 The capacity is: 5 \u5728\u672c\u4f8b\u4e2d\uff0c resize() \u51fd\u6570\u4f7f std::vector \u6539\u53d8\u4e86\u5b83\u7684\u957f\u5ea6\u548c\u5bb9\u91cf\u3002\u6ce8\u610f\uff0c\u5bb9\u91cf\u4fdd\u8bc1\u81f3\u5c11\u4e0e\u6570\u7ec4\u957f\u5ea6\u4e00\u6837\u5927(\u4f46\u4e5f\u53ef\u4ee5\u66f4\u5927)\uff0c\u5426\u5219\u8bbf\u95ee\u6570\u7ec4\u672b\u5c3e\u7684\u5143\u7d20\u5c06\u5728\u5206\u914d\u7684\u5185\u5b58\u4e4b\u5916\uff01 \u957f\u5ea6\u548c\u5bb9\u91cf\u2014\u2014\u66f4\u591a\u7ec6\u8282 \u4e3a\u4ec0\u4e48\u8981\u533a\u5206\u957f\u5ea6\u548c\u5bb9\u91cf\uff1fvector\u4f1a\u5728\u9700\u8981\u7684\u65f6\u5019\u91cd\u65b0\u5206\u914d\u5b83\u7684\u5185\u5b58\uff0c\u4f46\u662f\u5b83\u5e0c\u671b\u5c3d\u91cf\u907f\u514d\u91cd\u65b0\u5206\u914d\uff0c\u56e0\u4e3a\u8c03\u6574\u6570\u7ec4\u7684\u5927\u5c0f\u8ba1\u7b97\u5f00\u9500\u5f88\u5927\u3002\u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include #include int main () { std :: vector array {}; array = { 0 , 1 , 2 , 3 , 4 }; // okay, array length = 5 std :: cout << \"length: \" << array . size () << \" capacity: \" << array . capacity () << '\\n' ; array = { 9 , 8 , 7 }; // okay, array length is now 3! std :: cout << \"length: \" << array . size () << \" capacity: \" << array . capacity () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 length: 5 capacity: 5 length: 3 capacity: 5 \u6ce8\u610f\uff0c\u867d\u7136\u6211\u4eec\u4e3avector\u5206\u914d\u4e86\u4e00\u4e2a\u66f4\u5c0f\u7684\u6570\u7ec4\uff0c\u4f46\u5b83\u5e76\u6ca1\u6709\u91cd\u65b0\u5206\u914d\u5b83\u7684\u5185\u5b58(\u5bb9\u91cf\u4ecd\u7136\u662f5)\u3002\u5b83\u53ea\u662f\u66f4\u6539\u4e86\u5b83\u7684\u957f\u5ea6\uff0c\u56e0\u6b64\u5b83\u77e5\u9053\u6b64\u65f6\u53ea\u6709\u524d3\u4e2a\u5143\u7d20\u6709\u6548\u3002 \u6570\u7ec4\u4e0b\u6807\u548c at() \u57fa\u4e8e\u957f\u5ea6\u800c\u975e\u5bb9\u91cf \u4e0b\u6807\u8fd0\u7b97\u7b26\u548c at() \u51fd\u6570\u662f\u57fa\u4e8evector\u7684\u957f\u5ea6\u800c\u975e\u5bb9\u91cf\u5de5\u4f5c\u7684\u3002\u5bf9\u4e8e\u524d\u4f8b\u4e2d\u7684array\uff0c\u5b83\u7684\u957f\u5ea6\u662f3\uff0c\u5bb9\u91cf\u662f5\u3002\u90a3\u4e48\u5982\u679c\u6211\u4eec\u8bbf\u95ee\u7d22\u5f154\u7684\u5143\u7d20\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f\u7ed3\u679c\u663e\u7136\u662f\u4f1a\u8bbf\u95ee\u5931\u8d25\uff0c\u56e0\u4e3a4\u5df2\u7ecf\u8d85\u8fc7\u4e86\u6570\u7ec4\u7684\u957f\u5ea6\u3002 \u6ce8\u610f\uff1avector\u5e76\u4e0d\u4f1a\u56e0\u4e3a\u4e0b\u6807\u8fd0\u7b97\u7b26\u548c at() \u7684\u8c03\u7528\u800c\u8c03\u6574\u5927\u5c0f\uff01 std::vector \u7684\u7c7b\u6808\u884c\u4e3a \u5982\u679c\u4e0b\u6807\u64cd\u4f5c\u7b26\u548c at() \u51fd\u6570\u662f\u57fa\u4e8e\u6570\u7ec4\u957f\u5ea6\u7684\uff0c\u5e76\u4e14\u5bb9\u91cf\u603b\u662f\u81f3\u5c11\u4e0e\u6570\u7ec4\u957f\u5ea6\u4e00\u6837\u5927\uff0c\u4e3a\u4ec0\u4e48\u8fd8\u8981\u62c5\u5fc3\u5bb9\u91cf\u5462\uff1f\u867d\u7136 std::vector \u53ef\u4ee5\u7528\u4f5c\u52a8\u6001\u6570\u7ec4\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u4f5c\u5806\u6808\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u75283\u4e2a\u4e0e\u6808\u64cd\u4f5c\u76f8\u5339\u914d\u7684\u51fd\u6570\uff1a push_back() \uff1a\u5143\u7d20\u538b\u6808\uff1b back( ) \uff1a\u8fd4\u56de\u6808\u9876\u5143\u7d20\uff1b pop_back() \uff1a\u4ece\u6808\u4e2d\u5f39\u51fa\u5143\u7d20\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include void printStack ( const std :: vector < int >& stack ) { for ( auto element : stack ) std :: cout << element << ' ' ; std :: cout << \"(cap \" << stack . capacity () << \" length \" << stack . size () << \") \\n \" ; } int main () { std :: vector < int > stack {}; printStack ( stack ); stack . push_back ( 5 ); // push_back() \u538b\u6808 printStack ( stack ); stack . push_back ( 3 ); printStack ( stack ); stack . push_back ( 2 ); printStack ( stack ); std :: cout << \"top: \" << stack . back () << '\\n' ; // back() \u8fd4\u56de\u6700\u540e\u4e00\u4e2a\u5143\u7d20 stack . pop_back (); // pop_back() \u5f39\u51fa\u5143\u7d20 printStack ( stack ); stack . pop_back (); printStack ( stack ); stack . pop_back (); printStack ( stack ); return 0 ; } COPY This prints: 1 2 3 4 5 6 7 8 (cap 0 length 0) 5 (cap 1 length 1) 5 3 (cap 2 length 2) 5 3 2 (cap 3 length 3) top: 2 5 3 (cap 3 length 2) 5 (cap 3 length 1) (cap 3 length 0) \u548c\u4e0b\u6807\u8fd0\u7b97\u7b26\u548c at() \u4e0d\u540c\uff0c\u6808\u64cd\u4f5c\u51fd\u6570\u4f1a\u5728\u9700\u8981\u65f6\u8c03\u6574 std::vector \u7684\u5927\u5c0f\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u3002vector \u7684\u5927\u5c0f\u88ab\u8c03\u6574\u4e863\u6b21(capacity \u4ece 0 \u5230 1\uff0c 1 \u5230 2\uff0c2 \u5230 3)\u3002 \u56e0\u4e3a\u8c03\u6574vector\u7684\u5927\u5c0f\u4ee3\u4ef7\u5f88\u5927\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 reserve() \u51fd\u6570\u9884\u5148\u544a\u8bc9vector\u5206\u914d\u4e00\u5b9a\u91cf\u7684\u5bb9\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include void printStack ( const std :: vector < int >& stack ) { for ( auto element : stack ) std :: cout << element << ' ' ; std :: cout << \"(cap \" << stack . capacity () << \" length \" << stack . size () << \") \\n \" ; } int main () { std :: vector < int > stack {}; stack . reserve ( 5 ); // \u5c06\u5bb9\u91cf\u8bbe\u7f6e\u4e3a5 printStack ( stack ); stack . push_back ( 5 ); printStack ( stack ); stack . push_back ( 3 ); printStack ( stack ); stack . push_back ( 2 ); printStack ( stack ); std :: cout << \"top: \" << stack . back () << '\\n' ; stack . pop_back (); printStack ( stack ); stack . pop_back (); printStack ( stack ); stack . pop_back (); printStack ( stack ); return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 3 4 5 6 7 8 (cap 5 length 0) 5 (cap 5 length 1) 5 3 (cap 5 length 2) 5 3 2 (cap 5 length 3) top: 2 5 3 (cap 5 length 2) 5 (cap 5 length 1) (cap 5 length 0) \u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5bb9\u91cf\u88ab\u9884\u8bbe\u4e3a5\uff0c\u5e76\u4e14\u5728\u7a0b\u5e8f\u7684\u751f\u547d\u5468\u671f\u4e2d\u6ca1\u6709\u53d8\u5316\u3002 Vectors \u53ef\u80fd\u4f1a\u5206\u914d\u591a\u4f59\u7684\u5bb9\u91cf \u5f53\u8c03\u6574\u4e00\u4e2avector\u7684\u5927\u5c0f\u65f6\uff0c\u8be5vector\u53ef\u80fd\u4f1a\u5206\u914d\u6bd4\u6240\u9700\u66f4\u591a\u7684\u5bb9\u91cf\u3002\u8fd9\u6837\u505a\u662f\u4e3a\u4e86\u4e3a\u5176\u4ed6\u5143\u7d20\u63d0\u4f9b\u4e00\u4e9b\u201c\u5598\u606f\u7684\u7a7a\u95f4\u201d\uff0c\u4ece\u800c\u6700\u5c0f\u5316\u6240\u9700\u7684\u8c03\u6574\u64cd\u4f5c\u7684\u6570\u91cf\u3002\u8ba9\u6211\u4eec\u6765\u770b\u770b\u8fd9\u4e2a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include int main () { std :: vector v { 0 , 1 , 2 , 3 , 4 }; std :: cout << \"size: \" << v . size () << \" cap: \" << v . capacity () << '\\n' ; v . push_back ( 5 ); // add another element std :: cout << \"size: \" << v . size () << \" cap: \" << v . capacity () << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u673a\u5668\u4e0a\u6253\u5370\uff1a 1 2 size: 5 cap: 5 size: 6 cap: 7 \u5f53\u6211\u4eec\u4f7f\u7528 push_back() \u6dfb\u52a0\u4e00\u4e2a\u65b0\u5143\u7d20\u65f6\uff0c\u6211\u4eec\u7684\u5411\u91cf\u53ea\u9700\u89816\u4e2a\u5143\u7d20\u7684\u7a7a\u95f4\uff0c\u4f46\u5206\u914d\u4e867\u4e2a\u7a7a\u95f4\u3002\u8fd9\u6837\u505a\u7684\u76ee\u7684\u662f\uff0c\u5982\u679c\u6211\u4eec\u8981 push_back() \u53e6\u4e00\u4e2a\u5143\u7d20\uff0c\u5b83\u4e0d\u9700\u8981\u7acb\u5373\u8c03\u6574\u5927\u5c0f\u3002 \u662f\u5426\u3001\u4f55\u65f6\u4ee5\u53ca\u5206\u914d\u591a\u5c11\u989d\u5916\u7684\u5bb9\u91cf\u53d6\u51b3\u4e8e\u7f16\u8bd1\u5668\u5b9e\u73b0\u8005\u3002","title":"12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a"},{"location":"12-3-std-vector-capacity-and-stack-behavior/#_1","text":"\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 int * array { new int [ 10 ] { 1 , 2 , 3 , 4 , 5 } }; \u6211\u4eec\u4f1a\u8bf4\u8fd9\u4e2a\u6570\u7ec4\u7684\u957f\u5ea6\u662f10\uff0c\u5373\u4f7f\u6211\u4eec\u53ea\u4f7f\u7528\u4e86\u5206\u914d\u7684\u5143\u7d20\u4e2d\u76845\u4e2a\u3002 \u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u53ea\u60f3\u904d\u5386\u5df2\u7ecf\u521d\u59cb\u5316\u7684\u5143\u7d20\uff0c\u800c\u4fdd\u7559\u672a\u4f7f\u7528\u7684\u5143\u7d20\u4ee5\u5907\u5c06\u6765\u6269\u5c55\u5462\uff1f\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u5206\u522b\u8ddf\u8e2a\u201c\u4f7f\u7528\u201d\u4e86\u591a\u5c11\u5143\u7d20\u548c\u5206\u914d\u4e86\u591a\u5c11\u5143\u7d20\u3002\u4e0e\u53ea\u8bb0\u4f4f\u5176\u957f\u5ea6\u7684\u5185\u7f6e\u6570\u7ec4\u6216 std::array \u4e0d\u540c\uff0c std::vector \u5305\u542b\u4e24\u4e2a\u72ec\u7acb\u7684\u5c5e\u6027\uff1a\u957f\u5ea6\u548c\u5bb9\u91cf\u3002\u5728 std::vector \u7684\u8bed\u5883\u4e2d\uff0c length \u662f\u6570\u7ec4\u4e2d\u4f7f\u7528\u7684\u5143\u7d20\u6570\u91cf\uff0c\u800c capacity \u662f\u5185\u5b58\u4e2d\u5206\u914d\u7684\u5143\u7d20\u6570\u91cf\u3002 \u8ba9\u6211\u4eec\u518d\u770b\u4e00\u4e0b\u4e4b\u524d\u7684\u4e00\u4e2a std::vector \u7684\u4e00\u4e2a\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: vector array { 0 , 1 , 2 }; array . resize ( 5 ); // set length to 5 std :: cout << \"The length is: \" << array . size () << '\\n' ; for ( auto element : array ) std :: cout << element << ' ' ; return 0 ; }; 1 2 The length is: 5 0 1 2 0 0 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 resize() \u51fd\u6570\u5c06vector\u7684\u957f\u5ea6\u8bbe\u7f6e\u4e3a5\u3002\u8fd9\u544a\u8bc9\u53d8\u91cf\u6570\u7ec4\u6211\u4eec\u6253\u7b97\u4f7f\u7528\u6570\u7ec4\u7684\u524d5\u4e2a\u5143\u7d20\uff0c\u6240\u4ee5\u5b83\u5e94\u8be5\u628a\u8fd9\u4e9b\u5143\u7d20\u5f53\u505a\u6d3b\u52a8\u7684\u3002\u7136\u800c\uff0c\u8fd9\u5e26\u6765\u4e86\u4e00\u4e2a\u6709\u8da3\u7684\u95ee\u9898\uff1a\u8fd9\u4e2a\u6570\u7ec4\u7684\u5bb9\u91cf\u662f\u591a\u5c11? \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 capacity() \u51fd\u6570\u67e5\u8be2 std::vector \u7684\u5bb9\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include #include int main () { std :: vector array { 0 , 1 , 2 }; array . resize ( 5 ); // set length to 5 std :: cout << \"The length is: \" << array . size () << '\\n' ; std :: cout << \"The capacity is: \" << array . capacity () << '\\n' ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\uff0c\u8f93\u51fa\u5982\u4e0b\uff1a 1 2 The length is: 5 The capacity is: 5 \u5728\u672c\u4f8b\u4e2d\uff0c resize() \u51fd\u6570\u4f7f std::vector \u6539\u53d8\u4e86\u5b83\u7684\u957f\u5ea6\u548c\u5bb9\u91cf\u3002\u6ce8\u610f\uff0c\u5bb9\u91cf\u4fdd\u8bc1\u81f3\u5c11\u4e0e\u6570\u7ec4\u957f\u5ea6\u4e00\u6837\u5927(\u4f46\u4e5f\u53ef\u4ee5\u66f4\u5927)\uff0c\u5426\u5219\u8bbf\u95ee\u6570\u7ec4\u672b\u5c3e\u7684\u5143\u7d20\u5c06\u5728\u5206\u914d\u7684\u5185\u5b58\u4e4b\u5916\uff01","title":"\u957f\u5ea6\u548c\u5bb9\u91cf"},{"location":"12-3-std-vector-capacity-and-stack-behavior/#_2","text":"\u4e3a\u4ec0\u4e48\u8981\u533a\u5206\u957f\u5ea6\u548c\u5bb9\u91cf\uff1fvector\u4f1a\u5728\u9700\u8981\u7684\u65f6\u5019\u91cd\u65b0\u5206\u914d\u5b83\u7684\u5185\u5b58\uff0c\u4f46\u662f\u5b83\u5e0c\u671b\u5c3d\u91cf\u907f\u514d\u91cd\u65b0\u5206\u914d\uff0c\u56e0\u4e3a\u8c03\u6574\u6570\u7ec4\u7684\u5927\u5c0f\u8ba1\u7b97\u5f00\u9500\u5f88\u5927\u3002\u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include #include int main () { std :: vector array {}; array = { 0 , 1 , 2 , 3 , 4 }; // okay, array length = 5 std :: cout << \"length: \" << array . size () << \" capacity: \" << array . capacity () << '\\n' ; array = { 9 , 8 , 7 }; // okay, array length is now 3! std :: cout << \"length: \" << array . size () << \" capacity: \" << array . capacity () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 length: 5 capacity: 5 length: 3 capacity: 5 \u6ce8\u610f\uff0c\u867d\u7136\u6211\u4eec\u4e3avector\u5206\u914d\u4e86\u4e00\u4e2a\u66f4\u5c0f\u7684\u6570\u7ec4\uff0c\u4f46\u5b83\u5e76\u6ca1\u6709\u91cd\u65b0\u5206\u914d\u5b83\u7684\u5185\u5b58(\u5bb9\u91cf\u4ecd\u7136\u662f5)\u3002\u5b83\u53ea\u662f\u66f4\u6539\u4e86\u5b83\u7684\u957f\u5ea6\uff0c\u56e0\u6b64\u5b83\u77e5\u9053\u6b64\u65f6\u53ea\u6709\u524d3\u4e2a\u5143\u7d20\u6709\u6548\u3002","title":"\u957f\u5ea6\u548c\u5bb9\u91cf\u2014\u2014\u66f4\u591a\u7ec6\u8282"},{"location":"12-3-std-vector-capacity-and-stack-behavior/#at","text":"\u4e0b\u6807\u8fd0\u7b97\u7b26\u548c at() \u51fd\u6570\u662f\u57fa\u4e8evector\u7684\u957f\u5ea6\u800c\u975e\u5bb9\u91cf\u5de5\u4f5c\u7684\u3002\u5bf9\u4e8e\u524d\u4f8b\u4e2d\u7684array\uff0c\u5b83\u7684\u957f\u5ea6\u662f3\uff0c\u5bb9\u91cf\u662f5\u3002\u90a3\u4e48\u5982\u679c\u6211\u4eec\u8bbf\u95ee\u7d22\u5f154\u7684\u5143\u7d20\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f\u7ed3\u679c\u663e\u7136\u662f\u4f1a\u8bbf\u95ee\u5931\u8d25\uff0c\u56e0\u4e3a4\u5df2\u7ecf\u8d85\u8fc7\u4e86\u6570\u7ec4\u7684\u957f\u5ea6\u3002 \u6ce8\u610f\uff1avector\u5e76\u4e0d\u4f1a\u56e0\u4e3a\u4e0b\u6807\u8fd0\u7b97\u7b26\u548c at() \u7684\u8c03\u7528\u800c\u8c03\u6574\u5927\u5c0f\uff01","title":"\u6570\u7ec4\u4e0b\u6807\u548c at()\u57fa\u4e8e\u957f\u5ea6\u800c\u975e\u5bb9\u91cf"},{"location":"12-3-std-vector-capacity-and-stack-behavior/#stdvector","text":"\u5982\u679c\u4e0b\u6807\u64cd\u4f5c\u7b26\u548c at() \u51fd\u6570\u662f\u57fa\u4e8e\u6570\u7ec4\u957f\u5ea6\u7684\uff0c\u5e76\u4e14\u5bb9\u91cf\u603b\u662f\u81f3\u5c11\u4e0e\u6570\u7ec4\u957f\u5ea6\u4e00\u6837\u5927\uff0c\u4e3a\u4ec0\u4e48\u8fd8\u8981\u62c5\u5fc3\u5bb9\u91cf\u5462\uff1f\u867d\u7136 std::vector \u53ef\u4ee5\u7528\u4f5c\u52a8\u6001\u6570\u7ec4\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u4f5c\u5806\u6808\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u75283\u4e2a\u4e0e\u6808\u64cd\u4f5c\u76f8\u5339\u914d\u7684\u51fd\u6570\uff1a push_back() \uff1a\u5143\u7d20\u538b\u6808\uff1b back( ) \uff1a\u8fd4\u56de\u6808\u9876\u5143\u7d20\uff1b pop_back() \uff1a\u4ece\u6808\u4e2d\u5f39\u51fa\u5143\u7d20\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include void printStack ( const std :: vector < int >& stack ) { for ( auto element : stack ) std :: cout << element << ' ' ; std :: cout << \"(cap \" << stack . capacity () << \" length \" << stack . size () << \") \\n \" ; } int main () { std :: vector < int > stack {}; printStack ( stack ); stack . push_back ( 5 ); // push_back() \u538b\u6808 printStack ( stack ); stack . push_back ( 3 ); printStack ( stack ); stack . push_back ( 2 ); printStack ( stack ); std :: cout << \"top: \" << stack . back () << '\\n' ; // back() \u8fd4\u56de\u6700\u540e\u4e00\u4e2a\u5143\u7d20 stack . pop_back (); // pop_back() \u5f39\u51fa\u5143\u7d20 printStack ( stack ); stack . pop_back (); printStack ( stack ); stack . pop_back (); printStack ( stack ); return 0 ; } COPY This prints: 1 2 3 4 5 6 7 8 (cap 0 length 0) 5 (cap 1 length 1) 5 3 (cap 2 length 2) 5 3 2 (cap 3 length 3) top: 2 5 3 (cap 3 length 2) 5 (cap 3 length 1) (cap 3 length 0) \u548c\u4e0b\u6807\u8fd0\u7b97\u7b26\u548c at() \u4e0d\u540c\uff0c\u6808\u64cd\u4f5c\u51fd\u6570\u4f1a\u5728\u9700\u8981\u65f6\u8c03\u6574 std::vector \u7684\u5927\u5c0f\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u3002vector \u7684\u5927\u5c0f\u88ab\u8c03\u6574\u4e863\u6b21(capacity \u4ece 0 \u5230 1\uff0c 1 \u5230 2\uff0c2 \u5230 3)\u3002 \u56e0\u4e3a\u8c03\u6574vector\u7684\u5927\u5c0f\u4ee3\u4ef7\u5f88\u5927\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 reserve() \u51fd\u6570\u9884\u5148\u544a\u8bc9vector\u5206\u914d\u4e00\u5b9a\u91cf\u7684\u5bb9\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include void printStack ( const std :: vector < int >& stack ) { for ( auto element : stack ) std :: cout << element << ' ' ; std :: cout << \"(cap \" << stack . capacity () << \" length \" << stack . size () << \") \\n \" ; } int main () { std :: vector < int > stack {}; stack . reserve ( 5 ); // \u5c06\u5bb9\u91cf\u8bbe\u7f6e\u4e3a5 printStack ( stack ); stack . push_back ( 5 ); printStack ( stack ); stack . push_back ( 3 ); printStack ( stack ); stack . push_back ( 2 ); printStack ( stack ); std :: cout << \"top: \" << stack . back () << '\\n' ; stack . pop_back (); printStack ( stack ); stack . pop_back (); printStack ( stack ); stack . pop_back (); printStack ( stack ); return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 3 4 5 6 7 8 (cap 5 length 0) 5 (cap 5 length 1) 5 3 (cap 5 length 2) 5 3 2 (cap 5 length 3) top: 2 5 3 (cap 5 length 2) 5 (cap 5 length 1) (cap 5 length 0) \u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5bb9\u91cf\u88ab\u9884\u8bbe\u4e3a5\uff0c\u5e76\u4e14\u5728\u7a0b\u5e8f\u7684\u751f\u547d\u5468\u671f\u4e2d\u6ca1\u6709\u53d8\u5316\u3002","title":"std::vector \u7684\u7c7b\u6808\u884c\u4e3a"},{"location":"12-3-std-vector-capacity-and-stack-behavior/#vectors","text":"\u5f53\u8c03\u6574\u4e00\u4e2avector\u7684\u5927\u5c0f\u65f6\uff0c\u8be5vector\u53ef\u80fd\u4f1a\u5206\u914d\u6bd4\u6240\u9700\u66f4\u591a\u7684\u5bb9\u91cf\u3002\u8fd9\u6837\u505a\u662f\u4e3a\u4e86\u4e3a\u5176\u4ed6\u5143\u7d20\u63d0\u4f9b\u4e00\u4e9b\u201c\u5598\u606f\u7684\u7a7a\u95f4\u201d\uff0c\u4ece\u800c\u6700\u5c0f\u5316\u6240\u9700\u7684\u8c03\u6574\u64cd\u4f5c\u7684\u6570\u91cf\u3002\u8ba9\u6211\u4eec\u6765\u770b\u770b\u8fd9\u4e2a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include int main () { std :: vector v { 0 , 1 , 2 , 3 , 4 }; std :: cout << \"size: \" << v . size () << \" cap: \" << v . capacity () << '\\n' ; v . push_back ( 5 ); // add another element std :: cout << \"size: \" << v . size () << \" cap: \" << v . capacity () << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u673a\u5668\u4e0a\u6253\u5370\uff1a 1 2 size: 5 cap: 5 size: 6 cap: 7 \u5f53\u6211\u4eec\u4f7f\u7528 push_back() \u6dfb\u52a0\u4e00\u4e2a\u65b0\u5143\u7d20\u65f6\uff0c\u6211\u4eec\u7684\u5411\u91cf\u53ea\u9700\u89816\u4e2a\u5143\u7d20\u7684\u7a7a\u95f4\uff0c\u4f46\u5206\u914d\u4e867\u4e2a\u7a7a\u95f4\u3002\u8fd9\u6837\u505a\u7684\u76ee\u7684\u662f\uff0c\u5982\u679c\u6211\u4eec\u8981 push_back() \u53e6\u4e00\u4e2a\u5143\u7d20\uff0c\u5b83\u4e0d\u9700\u8981\u7acb\u5373\u8c03\u6574\u5927\u5c0f\u3002 \u662f\u5426\u3001\u4f55\u65f6\u4ee5\u53ca\u5206\u914d\u591a\u5c11\u989d\u5916\u7684\u5bb9\u91cf\u53d6\u51b3\u4e8e\u7f16\u8bd1\u5668\u5b9e\u73b0\u8005\u3002","title":"Vectors \u53ef\u80fd\u4f1a\u5206\u914d\u591a\u4f59\u7684\u5bb9\u91cf"},{"location":"12-4-recursion/","tags":["recursion"],"text":"\u5728 C++ \u4e2d\uff0c\u81ea\u5df1\u8c03\u7528\u81ea\u5df1\u7684\u51fd\u6570\u79f0\u4e3a\u9012\u5f52\u51fd\u6570\u3002\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u662f\u4e00\u4e2a\u9012\u5f52\u51fd\u6570\u7684\u4f8b\u5b50\uff08\u4e0d\u4f73\u7684\u5199\u6cd5\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include void countDown ( int count ) { std :: cout << \"push \" << count << '\\n' ; countDown ( count -1 ); // countDown() calls itself recursively } int main () { countDown ( 5 ); return 0 ; } \u5f53 countDown(5) \u88ab\u8c03\u7528\u65f6\uff0c\u6253\u5370\u4e86 \u201cpush 5\u201d\u3002\u7136\u540e countDown(4) \u88ab\u8c03\u7528\uff0c\u5b83\u6253\u5370 \u201cpush 4\u201d \u5e76\u8c03\u7528 countDown(3) \u3002 countDown(3) \u4f1a\u6253\u5370 \u201cpush 3\u201d \u5e76\u8c03\u7528 countDown(2) \u3002 countDown(n) \u8c03\u7528\u7684\u4e00\u7cfb\u5217 countDown(n-1) \uff0c\u5f62\u6210\u7684\u9012\u5f52\u8c03\u7528\u7b49\u4ef7\u4e8e\u4e00\u4e2a\u6b7b\u5faa\u73af\u3002 \u5728 12.2 - \u6808\u548c\u5806 \u4e2d\u6211\u4eec\u5b66\u8fc7\uff0c\u6240\u6709\u7684\u51fd\u6570\u8c03\u7528\u90fd\u9700\u8981\u5c06\u76f8\u5e94\u7684\u6570\u636e\u653e\u5728\u8c03\u7528\u6808\u4e0a\u3002\u56e0\u4e3a countDown() \u51fd\u6570\u4ece\u4e0d\u8fd4\u56de\uff08\u53ea\u662f\u4e0d\u65ad\u5730\u8c03\u7528 countDown() )\uff0c\u56e0\u6b64\u76f8\u5173\u7684\u51fd\u6570\u4fe1\u606f\u4ece\u6765\u6ca1\u6709\u88ab\u4ece\u6808\u4e0a\u5f39\u51fa\u8fc7\u3002\u56e0\u6b64\uff0c\u5728\u5230\u8fbe\u67d0\u4e2a\u6781\u9650\u65f6\uff0c\u8ba1\u7b97\u673a\u5c31\u4f1a\u8017\u5c3d\u6808\u4e0a\u5185\u5b58\uff0c\u8fdb\u800c\u53d1\u751f\u5806\u6808\u6ea2\u51fa\uff0c\u7a0b\u5e8f\u5d29\u6e83\u3002\u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\uff0c\u7a0b\u5e8f\u4f1a\u5728count\u5230-11732\u65f6\u5d29\u6e83\uff01 \u9012\u5f52\u7684\u7ec8\u6b62\u6761\u4ef6 \u9012\u5f52\u51fd\u6570\u8c03\u7528\u7684\u5de5\u4f5c\u65b9\u5f0f\u901a\u5e38\u4e0e\u666e\u901a\u51fd\u6570\u8c03\u7528\u4e00\u6837\u3002\u7136\u800c\uff0c\u4e0a\u9762\u7684\u7a0b\u5e8f\u8bf4\u660e\u4e86\u9012\u5f52\u51fd\u6570\u6700\u91cd\u8981\u7684\u533a\u522b\uff1a\u9012\u5f52\u51fd\u6570\u5fc5\u987b\u5305\u542b\u9012\u5f52\u7ec8\u6b62\u6761\u4ef6\uff0c\u5426\u5219\u5b83\u4eec\u5c06\u201c\u6c38\u8fdc\u201d\u8fd0\u884c(\u5b9e\u9645\u4e0a\uff0c\u76f4\u5230\u8c03\u7528\u5806\u6808\u8017\u5c3d\u5185\u5b58)\u3002\u9012\u5f52\u7ec8\u6b62\u6761\u4ef6\u2014\u2014\u5f53\u6ee1\u8db3\u8be5\u6761\u4ef6\u65f6\uff0c\u5c06\u5bfc\u81f4\u9012\u5f52\u51fd\u6570\u505c\u6b62\u8c03\u7528\u81ea\u8eab\u3002 \u9012\u5f52\u7ec8\u6b62\u6761\u4ef6\u901a\u5e38\u6d89\u53ca\u4f7f\u7528if\u8bed\u53e5\u3002\u4e0b\u9762\u662f\u6211\u4eec\u7684\u51fd\u6570\u91cd\u65b0\u8bbe\u8ba1\u4e86\u4e00\u4e2a\u7ec8\u6b62\u6761\u4ef6(\u548c\u4e00\u4e9b\u989d\u5916\u7684\u8f93\u51fa)\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void countDown ( int count ) { std :: cout << \"push \" << count << '\\n' ; if ( count > 1 ) // termination condition countDown ( count -1 ); std :: cout << \"pop \" << count << '\\n' ; } int main () { countDown ( 5 ); return 0 ; } \u8fd0\u884c\u7a0b\u5e8f\uff0c countDown() \u4f1a\u5f00\u59cb\u6253\u5370\uff1a 1 2 3 4 5 push 5 push 4 push 3 push 2 push 1 \u5982\u679c\u4f60\u73b0\u5728\u67e5\u770b\u8c03\u7528\u6808\uff0c\u4f60\u4f1a\u770b\u5230\u4ee5\u4e0b\u5185\u5bb9\uff1a 1 2 3 4 5 6 countDown(1) countDown(2) countDown(3) countDown(4) countDown(5) main() \u7531\u4e8e\u7ec8\u6b62\u6761\u4ef6\u5b58\u5728\uff0c countDown(1) \u4e0d\u8c03\u7528 countDown(0) \u2014\u2014\u76f8\u53cd\uff0c\" if\u8bed\u53e5\"\u4e0d\u6267\u884c\uff0c\u56e0\u6b64\u5b83\u6253\u5370\" pop 1 \"\uff0c\u7136\u540e\u7ec8\u6b62\u3002\u6b64\u65f6\uff0c\u4ece\u6808\u4e2d\u5f39\u51fa countDown(1) \uff0c\u63a7\u5236\u8fd4\u56de countDown(2) \u3002 countDown(2) \u5728 countDown(1) \u88ab\u8c03\u7528\u540e\u7ee7\u7eed\u6267\u884c\uff0c\u56e0\u6b64\u5b83\u6253\u5370\" pop 2 \"\u7136\u540e\u7ec8\u6b62\u3002\u9012\u5f52\u51fd\u6570\u8c03\u7528\u968f\u540e\u4ece\u5806\u6808\u4e2d\u5f39\u51fa\uff0c\u76f4\u5230\u6240\u6709\u7684 countDown \u5b9e\u4f8b\u90fd\u88ab\u5220\u9664\u3002 \u56e0\u6b64\uff0c\u7a0b\u5e8f\u6253\u5370\u7684\u5b8c\u6574\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 push 5 push 4 push 3 push 2 push 1 pop 1 pop 2 pop 3 pop 4 pop 5 \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u201cpush\u201d\u8f93\u51fa\u662f\u6309\u524d\u5411\u987a\u5e8f\u53d1\u751f\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u53d1\u751f\u5728\u9012\u5f52\u51fd\u6570\u8c03\u7528\u4e4b\u524d\u3002\u201cpop\u201d\u8f93\u51fa\u4ee5\u76f8\u53cd\u7684\u987a\u5e8f\u51fa\u73b0\uff0c\u56e0\u4e3a\u5b83\u4eec\u51fa\u73b0\u5728\u9012\u5f52\u51fd\u6570\u8c03\u7528\u4e4b\u540e\uff0c\u56e0\u4e3a\u51fd\u6570\u4ece\u6808\u4e2d\u5f39\u51fa(\u5176\u53d1\u751f\u7684\u987a\u5e8f\u4e0e\u653e\u5165\u5b83\u4eec\u7684\u987a\u5e8f\u76f8\u53cd)\u3002 \u66f4\u6709\u7528\u7684\u4f8b\u5b50 \u73b0\u5728\u6211\u4eec\u5df2\u7ecf\u8ba8\u8bba\u4e86\u9012\u5f52\u51fd\u6570\u8c03\u7528\u7684\u57fa\u672c\u673a\u5236\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u53e6\u4e00\u4e2a\u66f4\u5178\u578b\u7684\u9012\u5f52\u51fd\u6570: 1 2 3 4 5 6 7 8 9 10 11 // return the sum of all the integers between 1 (inclusive) and sumto (inclusive) // returns 0 for negative numbers int sumTo ( int sumto ) { if ( sumto <= 0 ) return 0 ; // base case (termination condition) when user passed in an unexpected argument (0 or negative) if ( sumto == 1 ) return 1 ; // normal base case (termination condition) return sumTo ( sumto - 1 ) + sumto ; // recursive function call } \u4ec5\u901a\u8fc7\u89c2\u5bdf\u9012\u5f52\u7a0b\u5e8f\u901a\u5e38\u5f88\u96be\u7406\u89e3\u9012\u5f52\u8c03\u7528\u3002\u5f53\u6211\u4eec\u8c03\u7528\u5177\u6709\u7279\u5b9a\u503c\u7684\u9012\u5f52\u51fd\u6570\u65f6\uff0c\u770b\u770b\u4f1a\u53d1\u751f\u4ec0\u4e48\u901a\u5e38\u662f\u6709\u6307\u5bfc\u610f\u4e49\u7684\u3002\u8ba9\u6211\u4eec\u770b\u770b\u5f53\u6211\u4eec\u8c03\u7528\u8fd9\u4e2a\u53c2\u6570\u4e3a sumto = 5 \u7684\u51fd\u6570\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002 1 2 3 4 5 sumTo(5) called, 5 <= 1 is false, so we return sumTo(4) + 5. sumTo(4) called, 4 <= 1 is false, so we return sumTo(3) + 4. sumTo(3) called, 3 <= 1 is false, so we return sumTo(2) + 3. sumTo(2) called, 2 <= 1 is false, so we return sumTo(1) + 2. sumTo(1) called, 1 <= 1 is true, so we return 1. This is the termination condition. \u73b0\u5728\u6211\u4eec\u5c55\u5f00\u8c03\u7528\u6808(\u5728\u8fd4\u56de\u65f6\u5c06\u6bcf\u4e2a\u51fd\u6570\u4ece\u8c03\u7528\u6808\u4e2d\u5f39\u51fa): 1 2 3 4 5 sumTo(1) returns 1. sumTo(2) returns sumTo(1) + 2, which is 1 + 2 = 3. sumTo(3) returns sumTo(2) + 3, which is 3 + 3 = 6. sumTo(4) returns sumTo(3) + 4, which is 6 + 4 = 10. sumTo(5) returns sumTo(4) + 5, which is 10 + 5 = 15. \u8fd9\u91cc\u53ef\u4ee5\u5f88\u660e\u663e\u7684\u770b\u51fa\uff0c\u6bcf\u6b21\u76f8\u52a0\u503c\u4f1a\u9012\u589e1\uff0c\u7136\u540e\u518d\u52a0\u4e0a\u4f20\u5165\u7684\u503c\u3002 \u56e0\u4e3a\u5355\u770b\u9012\u5f52\u51fd\u6570\u53ef\u80fd\u5f88\u96be\u7406\u89e3\uff0c\u6240\u4ee5\u597d\u7684\u6ce8\u91ca\u7279\u522b\u91cd\u8981\u3002 \u6ce8\u610f\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 sumto - 1 \u800c\u4e0d\u662f --sumto \u3002\u8fd9\u662f\u56e0\u4e3a operator-- \u662f\u6709\u526f\u4f5c\u7528\u7684\uff0c\u800c\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u591a\u6b21\u4f7f\u7528\u4e00\u4e2a\u6709\u526f\u4f5c\u7528\u7684\u53d8\u91cf\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u4f7f\u7528 sumto - 1 \u53ef\u4ee5\u907f\u514d\u526f\u4f5c\u7528\uff0c\u4f7f\u5f97 sumto \u53ef\u4ee5\u5b89\u5168\u5730\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u591a\u6b21\u4f7f\u7528\u3002 \u9012\u5f52\u7b97\u6cd5 \u4f7f\u7528\u9012\u5f52\u51fd\u6570\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5982\u4e0b\uff1a\u9996\u5148\u627e\u5230\u95ee\u9898\u5b50\u96c6\u7684\u89e3(\u9012\u5f52\u5730)\uff0c\u7136\u540e\u4fee\u6539\u5b50\u95ee\u9898\u7684\u89e3\u4ee5\u5f97\u5230\u95ee\u9898\u7684\u89e3\u3002\u5728\u4e0a\u8ff0\u7b97\u6cd5\u4e2d\uff0c sumTo(value) \u9996\u5148\u6c42\u89e3 sumTo(value-1) \uff0c\u7136\u540e\u5c06\u53d8\u91cf value \u7684\u503c\u76f8\u52a0\uff0c\u6c42\u51fa sumTo(value) \u7684\u89e3\u3002 \u5728\u8bb8\u591a\u9012\u5f52\u7b97\u6cd5\u4e2d\uff0c\u4e00\u4e9b\u8f93\u5165\u4f1a\u5f97\u5230\u5f88\u7b80\u5355\u7684\u8f93\u51fa\u3002\u4f8b\u5982\uff0c sumTo(1) \u7684\u8f93\u51fa\u4e3a1(\u4f60\u53ef\u4ee5\u5728\u5934\u8111\u4e2d\u8ba1\u7b97)\uff0c\u5e76\u4e14\u6ca1\u6709\u5fc5\u8981\u518d\u8fdb\u4e00\u6b65\u9012\u5f52\u4e86\u3002\u8fd9\u79cd\u60c5\u51b5\u79f0\u4e3a\u2014\u2014\u57fa\u672c\u6761\u4ef6\u3002\u57fa\u672c\u6761\u4ef6\u901a\u5e38\u4f5c\u4e3a\u7b97\u6cd5\u7684\u7ec8\u6b62\u6761\u4ef6\u3002\u901a\u8fc7\u8003\u8651\u8f93\u51650\u30011\u3001\" \"\u3001\"\u6216null\u7684\u8f93\u51fa\uff0c\u901a\u5e38\u53ef\u4ee5\u8bc6\u522b\u4e3a\u57fa\u672c\u6761\u4ef6\u3002 \u6590\u6ce2\u90a3\u5951\u6570\u5217 \u6700\u8457\u540d\u7684\u6570\u5b66\u9012\u5f52\u7b97\u6cd5\u4e4b\u4e00\u662f\u6590\u6ce2\u90a3\u5951\u6570\u5217\u3002\u6590\u6ce2\u90a3\u5951\u6570\u5217\u51fa\u73b0\u5728\u81ea\u7136\u754c\u7684\u8bb8\u591a\u5730\u65b9\uff0c\u5982\u6811\u6728\u7684\u5206\u679d\u3001\u8d1d\u58f3\u7684\u87ba\u65cb\u5f62\u3001\u83e0\u841d\u7684\u679c\u5b9e\u3001\u4e0d\u5377\u66f2\u7684\u8568\u7c7b\u53f6\u5b50\u548c\u677e\u679c\u7684\u6392\u5217\u3002 \u8fd9\u662f\u4e00\u5f20\u6590\u6ce2\u90a3\u5951\u87ba\u65cb\u56fe: \u6bcf\u4e00\u4e2a\u6590\u6ce2\u90a3\u5951\u6570\u5217\u90fd\u662f\u8be5\u6570\u5217\u6240\u5728\u6b63\u65b9\u5f62\u7684\u8fb9\u957f\u3002 \u6590\u6ce2\u90a3\u5951\u6570\u5217\u5728\u6570\u5b66\u4e0a\u5b9a\u4e49\u4e3a: 1 2 3 4 5 F(n) = 0 if n = 0 1 if n = 1 f(n-1) + f(n-2) if n > 1 \u56e0\u6b64\uff0c\u7f16\u5199\u4e00\u4e2a(\u4e0d\u662f\u5f88\u9ad8\u6548\u7684)\u9012\u5f52\u51fd\u6570\u6765\u8ba1\u7b97\u7b2cn\u4e2a\u6590\u6ce2\u90a3\u5951\u6570\u662f\u76f8\u5f53\u7b80\u5355\u7684: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int fibonacci ( int count ) { if ( count == 0 ) return 0 ; // base case (termination condition) if ( count == 1 ) return 1 ; // base case (termination condition) return fibonacci ( count -1 ) + fibonacci ( count -2 ); } // And a main program to display the first 13 Fibonacci numbers int main () { for ( int count { 0 }; count < 13 ; ++ count ) std :: cout << fibonacci ( count ) << ' ' ; return 0 ; } \u8fd0\u884c\u8be5\u7a0b\u5e8f\u4f1a\u4ea7\u751f\u4ee5\u4e0b\u7ed3\u679c: 1 0 1 1 2 3 5 8 13 21 34 55 89 144 \u4f60\u4f1a\u6ce8\u610f\u5230\uff0c\u8fd9\u6b63\u662f\u6590\u6ce2\u90a3\u5951\u87ba\u65cb\u56fe\u4e2d\u51fa\u73b0\u7684\u6570\u5b57\u3002 \u8bb0\u5fc6\u5316\u7b97\u6cd5 \u4e0a\u9762\u7684\u9012\u5f52\u6590\u6ce2\u90a3\u5951\u7b97\u6cd5\u4e0d\u662f\u5f88\u9ad8\u6548\uff0c\u90e8\u5206\u539f\u56e0\u662f\u6bcf\u6b21\u8c03\u7528\u4e00\u4e2a\u6590\u6ce2\u90a3\u5951\u90fd\u4f1a\u5bfc\u81f4\u53e6\u5916\u4e24\u4e2a\u6590\u6ce2\u90a3\u5951\u8c03\u7528\u3002\u8fd9\u5c06\u4ea7\u751f\u6307\u6570\u7ea7\u7684\u51fd\u6570\u8c03\u7528\u6b21\u6570(\u5b9e\u9645\u4e0a\uff0c\u4e0a\u9762\u7684\u793a\u4f8b\u8c03\u7528 fibonacci() 1205\u6b21!)\u6709\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u51cf\u5c11\u5fc5\u8981\u7684\u8c03\u7528\u6b21\u6570\u3002\u4e00\u79cd\u662f\u53eb\u505a \u8bb0\u5fc6\u5316 \u7684\u6280\u672f\uff0c\u5c06\u5f00\u9500\u5f88\u5927\u7684\u51fd\u6570\u8c03\u7528\u7684\u7ed3\u679c\u7f13\u5b58\u8d77\u6765\uff0c\u4ee5\u4fbf\u5728\u518d\u6b21\u51fa\u73b0\u76f8\u540c\u7684\u8f93\u5165\u65f6\u8fd4\u56de\u7ed3\u679c\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u8bb0\u5fc6\u7248\u7684\u9012\u5f52\u6590\u6ce2\u90a3\u5951\u7b97\u6cd5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include // h/t to potterman28wxcv for a variant of this code int fibonacci ( int count ) { // We'll use a static std::vector to cache calculated results static std :: vector results { 0 , 1 }; // If we've already seen this count, then use the cache'd result if ( count < static_cast < int > ( std :: size ( results ))) return results [ count ]; // Otherwise calculate the new result and add it results . push_back ( fibonacci ( count - 1 ) + fibonacci ( count - 2 )); return results [ count ]; } // And a main program to display the first 13 Fibonacci numbers int main () { for ( int count { 0 }; count < 13 ; ++ count ) std :: cout << fibonacci ( count ) << ' ' ; return 0 ; } \u8fd9\u4e2a\u8bb0\u5fc6\u7248\u672c\u8fdb\u884c\u4e8635\u6b21\u51fd\u6570\u8c03\u7528\uff0c\u6bd4\u539f\u59cb\u7b97\u6cd5\u76841205\u6b21\u597d\u5f97\u591a\u3002 \u9012\u5f52\u548c\u8fed\u4ee3 \u5173\u4e8e\u9012\u5f52\u51fd\u6570\u7ecf\u5e38\u88ab\u95ee\u5230\u7684\u4e00\u4e2a\u95ee\u9898\u662f\uff0c\u201c\u5982\u679c\u53ef\u4ee5\u8fed\u4ee3\u5730\u6267\u884c\u8bb8\u591a\u76f8\u540c\u7684\u4efb\u52a1(\u4f7f\u7528for \u5faa\u73af\u6216while\u5faa\u73af)\uff0c\u4e3a\u4ec0\u4e48\u8fd8\u8981\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u5462?\u201d\u4e8b\u5b9e\u8bc1\u660e\uff0c\u4f60\u603b\u662f\u53ef\u4ee5\u8fed\u4ee3\u5730\u89e3\u51b3\u9012\u5f52\u95ee\u9898\u2014\u2014\u7136\u800c\uff0c\u5bf9\u4e8e\u590d\u6742\u7684\u95ee\u9898\uff0c\u9012\u5f52\u7248\u672c\u7684\u7f16\u5199(\u548c\u8bfb\u53d6)\u901a\u5e38\u8981\u7b80\u5355\u5f97\u591a\u3002\u4f8b\u5982\uff0c\u867d\u7136\u53ef\u4ee5\u8fed\u4ee3\u5730\u7f16\u5199Fibonacci\u51fd\u6570\uff0c\u4f46\u8fd9\u6709\u70b9\u56f0\u96be!(\u8bd5\u4e00\u8bd5!) \u8fed\u4ee3\u51fd\u6570(\u4f7f\u7528for\u5faa\u73af\u6216while\u5faa\u73af\u7684\u51fd\u6570)\u51e0\u4e4e\u603b\u662f\u6bd4\u9012\u5f52\u51fd\u6570\u66f4\u9ad8\u6548\u3002\u8fd9\u662f\u56e0\u4e3a\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u6808\u5e27\u7684\u63a8\u5165\u548c\u5f39\u51fa\u90fd\u4f1a\u4ea7\u751f\u4e00\u5b9a\u7684\u5f00\u9500\u3002\u8fed\u4ee3\u51fd\u6570\u907f\u514d\u4e86\u8fd9\u79cd\u5f00\u9500\u3002 \u8fd9\u5e76\u4e0d\u662f\u8bf4\u8fed\u4ee3\u51fd\u6570\u603b\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u6709\u65f6\u5019\uff0c\u51fd\u6570\u7684\u9012\u5f52\u5b9e\u73b0\u662f\u5982\u6b64\u7684\u7b80\u6d01\u548c\u5bb9\u6613\u9075\u5faa\uff0c\u4ee5\u81f3\u4e8e\u4e3a\u4e86\u53ef\u7ef4\u62a4\u6027\u7684\u597d\u5904\uff0c\u4ea7\u751f\u4e00\u70b9\u989d\u5916\u7684\u5f00\u9500\u662f\u503c\u5f97\u7684\uff0c\u7279\u522b\u662f\u5728\u7b97\u6cd5\u4e0d\u9700\u8981\u591a\u6b21\u9012\u5f52\u6765\u627e\u5230\u4e00\u4e2a\u89e3\u51b3\u65b9\u6848\u7684\u60c5\u51b5\u4e0b\u3002 \u5728\u6ee1\u8db3\u4e0b\u9762\u6761\u4ef6\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u9012\u5f52\u901a\u5e38\u662f\u66f4\u597d\u7684\u9009\u62e9\uff1a \u5f53\u9012\u5f52\u4ee3\u7801\u66f4\u5bb9\u6613\u7f16\u5199\u65f6\uff1b \u9012\u5f52\u7684\u6df1\u5ea6\u6709\u9650(\u4f8b\u5982\uff0c\u5b9e\u9645\u4e0a\u6ca1\u529e\u6cd5\u7f16\u5199\u5341\u4e07\u5c42\u9012\u5f52\u7684\u7b97\u6cd5\uff09\uff1b \u5f53\u8fed\u4ee3\u7248\u672c\u7684\u7b97\u6cd5\u9700\u8981\u624b\u5de5\u7ba1\u7406\u6808\u65f6\uff1b \u6027\u80fd\u8981\u6c42\u4e0d\u9ad8 \u4f46\u662f\uff0c\u5982\u679c\u9012\u5f52\u7b97\u6cd5\u66f4\u5bb9\u6613\u5b9e\u73b0\uff0c\u90a3\u4e48\u53ef\u4ee5\u5148\u9012\u5f52\u5730\u5f00\u59cb\uff0c\u7136\u540e\u518d\u4f18\u5316\u4e3a\u8fed\u4ee3\u7b97\u6cd5\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e00\u822c\u6765\u8bf4\uff0c\u8fed\u4ee3\u6bd4\u9012\u5f52\u66f4\u53d7\u6b22\u8fce\uff0c\u9664\u975e\u9012\u5f52\u786e\u5b9e\u6709\u610f\u4e49\u3002","title":"12.4 - \u9012\u5f52"},{"location":"12-4-recursion/#_1","text":"\u9012\u5f52\u51fd\u6570\u8c03\u7528\u7684\u5de5\u4f5c\u65b9\u5f0f\u901a\u5e38\u4e0e\u666e\u901a\u51fd\u6570\u8c03\u7528\u4e00\u6837\u3002\u7136\u800c\uff0c\u4e0a\u9762\u7684\u7a0b\u5e8f\u8bf4\u660e\u4e86\u9012\u5f52\u51fd\u6570\u6700\u91cd\u8981\u7684\u533a\u522b\uff1a\u9012\u5f52\u51fd\u6570\u5fc5\u987b\u5305\u542b\u9012\u5f52\u7ec8\u6b62\u6761\u4ef6\uff0c\u5426\u5219\u5b83\u4eec\u5c06\u201c\u6c38\u8fdc\u201d\u8fd0\u884c(\u5b9e\u9645\u4e0a\uff0c\u76f4\u5230\u8c03\u7528\u5806\u6808\u8017\u5c3d\u5185\u5b58)\u3002\u9012\u5f52\u7ec8\u6b62\u6761\u4ef6\u2014\u2014\u5f53\u6ee1\u8db3\u8be5\u6761\u4ef6\u65f6\uff0c\u5c06\u5bfc\u81f4\u9012\u5f52\u51fd\u6570\u505c\u6b62\u8c03\u7528\u81ea\u8eab\u3002 \u9012\u5f52\u7ec8\u6b62\u6761\u4ef6\u901a\u5e38\u6d89\u53ca\u4f7f\u7528if\u8bed\u53e5\u3002\u4e0b\u9762\u662f\u6211\u4eec\u7684\u51fd\u6570\u91cd\u65b0\u8bbe\u8ba1\u4e86\u4e00\u4e2a\u7ec8\u6b62\u6761\u4ef6(\u548c\u4e00\u4e9b\u989d\u5916\u7684\u8f93\u51fa)\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void countDown ( int count ) { std :: cout << \"push \" << count << '\\n' ; if ( count > 1 ) // termination condition countDown ( count -1 ); std :: cout << \"pop \" << count << '\\n' ; } int main () { countDown ( 5 ); return 0 ; } \u8fd0\u884c\u7a0b\u5e8f\uff0c countDown() \u4f1a\u5f00\u59cb\u6253\u5370\uff1a 1 2 3 4 5 push 5 push 4 push 3 push 2 push 1 \u5982\u679c\u4f60\u73b0\u5728\u67e5\u770b\u8c03\u7528\u6808\uff0c\u4f60\u4f1a\u770b\u5230\u4ee5\u4e0b\u5185\u5bb9\uff1a 1 2 3 4 5 6 countDown(1) countDown(2) countDown(3) countDown(4) countDown(5) main() \u7531\u4e8e\u7ec8\u6b62\u6761\u4ef6\u5b58\u5728\uff0c countDown(1) \u4e0d\u8c03\u7528 countDown(0) \u2014\u2014\u76f8\u53cd\uff0c\" if\u8bed\u53e5\"\u4e0d\u6267\u884c\uff0c\u56e0\u6b64\u5b83\u6253\u5370\" pop 1 \"\uff0c\u7136\u540e\u7ec8\u6b62\u3002\u6b64\u65f6\uff0c\u4ece\u6808\u4e2d\u5f39\u51fa countDown(1) \uff0c\u63a7\u5236\u8fd4\u56de countDown(2) \u3002 countDown(2) \u5728 countDown(1) \u88ab\u8c03\u7528\u540e\u7ee7\u7eed\u6267\u884c\uff0c\u56e0\u6b64\u5b83\u6253\u5370\" pop 2 \"\u7136\u540e\u7ec8\u6b62\u3002\u9012\u5f52\u51fd\u6570\u8c03\u7528\u968f\u540e\u4ece\u5806\u6808\u4e2d\u5f39\u51fa\uff0c\u76f4\u5230\u6240\u6709\u7684 countDown \u5b9e\u4f8b\u90fd\u88ab\u5220\u9664\u3002 \u56e0\u6b64\uff0c\u7a0b\u5e8f\u6253\u5370\u7684\u5b8c\u6574\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 push 5 push 4 push 3 push 2 push 1 pop 1 pop 2 pop 3 pop 4 pop 5 \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u201cpush\u201d\u8f93\u51fa\u662f\u6309\u524d\u5411\u987a\u5e8f\u53d1\u751f\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u53d1\u751f\u5728\u9012\u5f52\u51fd\u6570\u8c03\u7528\u4e4b\u524d\u3002\u201cpop\u201d\u8f93\u51fa\u4ee5\u76f8\u53cd\u7684\u987a\u5e8f\u51fa\u73b0\uff0c\u56e0\u4e3a\u5b83\u4eec\u51fa\u73b0\u5728\u9012\u5f52\u51fd\u6570\u8c03\u7528\u4e4b\u540e\uff0c\u56e0\u4e3a\u51fd\u6570\u4ece\u6808\u4e2d\u5f39\u51fa(\u5176\u53d1\u751f\u7684\u987a\u5e8f\u4e0e\u653e\u5165\u5b83\u4eec\u7684\u987a\u5e8f\u76f8\u53cd)\u3002","title":"\u9012\u5f52\u7684\u7ec8\u6b62\u6761\u4ef6"},{"location":"12-4-recursion/#_2","text":"\u73b0\u5728\u6211\u4eec\u5df2\u7ecf\u8ba8\u8bba\u4e86\u9012\u5f52\u51fd\u6570\u8c03\u7528\u7684\u57fa\u672c\u673a\u5236\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u53e6\u4e00\u4e2a\u66f4\u5178\u578b\u7684\u9012\u5f52\u51fd\u6570: 1 2 3 4 5 6 7 8 9 10 11 // return the sum of all the integers between 1 (inclusive) and sumto (inclusive) // returns 0 for negative numbers int sumTo ( int sumto ) { if ( sumto <= 0 ) return 0 ; // base case (termination condition) when user passed in an unexpected argument (0 or negative) if ( sumto == 1 ) return 1 ; // normal base case (termination condition) return sumTo ( sumto - 1 ) + sumto ; // recursive function call } \u4ec5\u901a\u8fc7\u89c2\u5bdf\u9012\u5f52\u7a0b\u5e8f\u901a\u5e38\u5f88\u96be\u7406\u89e3\u9012\u5f52\u8c03\u7528\u3002\u5f53\u6211\u4eec\u8c03\u7528\u5177\u6709\u7279\u5b9a\u503c\u7684\u9012\u5f52\u51fd\u6570\u65f6\uff0c\u770b\u770b\u4f1a\u53d1\u751f\u4ec0\u4e48\u901a\u5e38\u662f\u6709\u6307\u5bfc\u610f\u4e49\u7684\u3002\u8ba9\u6211\u4eec\u770b\u770b\u5f53\u6211\u4eec\u8c03\u7528\u8fd9\u4e2a\u53c2\u6570\u4e3a sumto = 5 \u7684\u51fd\u6570\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002 1 2 3 4 5 sumTo(5) called, 5 <= 1 is false, so we return sumTo(4) + 5. sumTo(4) called, 4 <= 1 is false, so we return sumTo(3) + 4. sumTo(3) called, 3 <= 1 is false, so we return sumTo(2) + 3. sumTo(2) called, 2 <= 1 is false, so we return sumTo(1) + 2. sumTo(1) called, 1 <= 1 is true, so we return 1. This is the termination condition. \u73b0\u5728\u6211\u4eec\u5c55\u5f00\u8c03\u7528\u6808(\u5728\u8fd4\u56de\u65f6\u5c06\u6bcf\u4e2a\u51fd\u6570\u4ece\u8c03\u7528\u6808\u4e2d\u5f39\u51fa): 1 2 3 4 5 sumTo(1) returns 1. sumTo(2) returns sumTo(1) + 2, which is 1 + 2 = 3. sumTo(3) returns sumTo(2) + 3, which is 3 + 3 = 6. sumTo(4) returns sumTo(3) + 4, which is 6 + 4 = 10. sumTo(5) returns sumTo(4) + 5, which is 10 + 5 = 15. \u8fd9\u91cc\u53ef\u4ee5\u5f88\u660e\u663e\u7684\u770b\u51fa\uff0c\u6bcf\u6b21\u76f8\u52a0\u503c\u4f1a\u9012\u589e1\uff0c\u7136\u540e\u518d\u52a0\u4e0a\u4f20\u5165\u7684\u503c\u3002 \u56e0\u4e3a\u5355\u770b\u9012\u5f52\u51fd\u6570\u53ef\u80fd\u5f88\u96be\u7406\u89e3\uff0c\u6240\u4ee5\u597d\u7684\u6ce8\u91ca\u7279\u522b\u91cd\u8981\u3002 \u6ce8\u610f\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 sumto - 1 \u800c\u4e0d\u662f --sumto \u3002\u8fd9\u662f\u56e0\u4e3a operator-- \u662f\u6709\u526f\u4f5c\u7528\u7684\uff0c\u800c\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u591a\u6b21\u4f7f\u7528\u4e00\u4e2a\u6709\u526f\u4f5c\u7528\u7684\u53d8\u91cf\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u4f7f\u7528 sumto - 1 \u53ef\u4ee5\u907f\u514d\u526f\u4f5c\u7528\uff0c\u4f7f\u5f97 sumto \u53ef\u4ee5\u5b89\u5168\u5730\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u591a\u6b21\u4f7f\u7528\u3002","title":"\u66f4\u6709\u7528\u7684\u4f8b\u5b50"},{"location":"12-4-recursion/#_3","text":"\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5982\u4e0b\uff1a\u9996\u5148\u627e\u5230\u95ee\u9898\u5b50\u96c6\u7684\u89e3(\u9012\u5f52\u5730)\uff0c\u7136\u540e\u4fee\u6539\u5b50\u95ee\u9898\u7684\u89e3\u4ee5\u5f97\u5230\u95ee\u9898\u7684\u89e3\u3002\u5728\u4e0a\u8ff0\u7b97\u6cd5\u4e2d\uff0c sumTo(value) \u9996\u5148\u6c42\u89e3 sumTo(value-1) \uff0c\u7136\u540e\u5c06\u53d8\u91cf value \u7684\u503c\u76f8\u52a0\uff0c\u6c42\u51fa sumTo(value) \u7684\u89e3\u3002 \u5728\u8bb8\u591a\u9012\u5f52\u7b97\u6cd5\u4e2d\uff0c\u4e00\u4e9b\u8f93\u5165\u4f1a\u5f97\u5230\u5f88\u7b80\u5355\u7684\u8f93\u51fa\u3002\u4f8b\u5982\uff0c sumTo(1) \u7684\u8f93\u51fa\u4e3a1(\u4f60\u53ef\u4ee5\u5728\u5934\u8111\u4e2d\u8ba1\u7b97)\uff0c\u5e76\u4e14\u6ca1\u6709\u5fc5\u8981\u518d\u8fdb\u4e00\u6b65\u9012\u5f52\u4e86\u3002\u8fd9\u79cd\u60c5\u51b5\u79f0\u4e3a\u2014\u2014\u57fa\u672c\u6761\u4ef6\u3002\u57fa\u672c\u6761\u4ef6\u901a\u5e38\u4f5c\u4e3a\u7b97\u6cd5\u7684\u7ec8\u6b62\u6761\u4ef6\u3002\u901a\u8fc7\u8003\u8651\u8f93\u51650\u30011\u3001\" \"\u3001\"\u6216null\u7684\u8f93\u51fa\uff0c\u901a\u5e38\u53ef\u4ee5\u8bc6\u522b\u4e3a\u57fa\u672c\u6761\u4ef6\u3002","title":"\u9012\u5f52\u7b97\u6cd5"},{"location":"12-4-recursion/#_4","text":"\u6700\u8457\u540d\u7684\u6570\u5b66\u9012\u5f52\u7b97\u6cd5\u4e4b\u4e00\u662f\u6590\u6ce2\u90a3\u5951\u6570\u5217\u3002\u6590\u6ce2\u90a3\u5951\u6570\u5217\u51fa\u73b0\u5728\u81ea\u7136\u754c\u7684\u8bb8\u591a\u5730\u65b9\uff0c\u5982\u6811\u6728\u7684\u5206\u679d\u3001\u8d1d\u58f3\u7684\u87ba\u65cb\u5f62\u3001\u83e0\u841d\u7684\u679c\u5b9e\u3001\u4e0d\u5377\u66f2\u7684\u8568\u7c7b\u53f6\u5b50\u548c\u677e\u679c\u7684\u6392\u5217\u3002 \u8fd9\u662f\u4e00\u5f20\u6590\u6ce2\u90a3\u5951\u87ba\u65cb\u56fe: \u6bcf\u4e00\u4e2a\u6590\u6ce2\u90a3\u5951\u6570\u5217\u90fd\u662f\u8be5\u6570\u5217\u6240\u5728\u6b63\u65b9\u5f62\u7684\u8fb9\u957f\u3002 \u6590\u6ce2\u90a3\u5951\u6570\u5217\u5728\u6570\u5b66\u4e0a\u5b9a\u4e49\u4e3a: 1 2 3 4 5 F(n) = 0 if n = 0 1 if n = 1 f(n-1) + f(n-2) if n > 1 \u56e0\u6b64\uff0c\u7f16\u5199\u4e00\u4e2a(\u4e0d\u662f\u5f88\u9ad8\u6548\u7684)\u9012\u5f52\u51fd\u6570\u6765\u8ba1\u7b97\u7b2cn\u4e2a\u6590\u6ce2\u90a3\u5951\u6570\u662f\u76f8\u5f53\u7b80\u5355\u7684: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int fibonacci ( int count ) { if ( count == 0 ) return 0 ; // base case (termination condition) if ( count == 1 ) return 1 ; // base case (termination condition) return fibonacci ( count -1 ) + fibonacci ( count -2 ); } // And a main program to display the first 13 Fibonacci numbers int main () { for ( int count { 0 }; count < 13 ; ++ count ) std :: cout << fibonacci ( count ) << ' ' ; return 0 ; } \u8fd0\u884c\u8be5\u7a0b\u5e8f\u4f1a\u4ea7\u751f\u4ee5\u4e0b\u7ed3\u679c: 1 0 1 1 2 3 5 8 13 21 34 55 89 144 \u4f60\u4f1a\u6ce8\u610f\u5230\uff0c\u8fd9\u6b63\u662f\u6590\u6ce2\u90a3\u5951\u87ba\u65cb\u56fe\u4e2d\u51fa\u73b0\u7684\u6570\u5b57\u3002","title":"\u6590\u6ce2\u90a3\u5951\u6570\u5217"},{"location":"12-4-recursion/#_5","text":"\u4e0a\u9762\u7684\u9012\u5f52\u6590\u6ce2\u90a3\u5951\u7b97\u6cd5\u4e0d\u662f\u5f88\u9ad8\u6548\uff0c\u90e8\u5206\u539f\u56e0\u662f\u6bcf\u6b21\u8c03\u7528\u4e00\u4e2a\u6590\u6ce2\u90a3\u5951\u90fd\u4f1a\u5bfc\u81f4\u53e6\u5916\u4e24\u4e2a\u6590\u6ce2\u90a3\u5951\u8c03\u7528\u3002\u8fd9\u5c06\u4ea7\u751f\u6307\u6570\u7ea7\u7684\u51fd\u6570\u8c03\u7528\u6b21\u6570(\u5b9e\u9645\u4e0a\uff0c\u4e0a\u9762\u7684\u793a\u4f8b\u8c03\u7528 fibonacci() 1205\u6b21!)\u6709\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u51cf\u5c11\u5fc5\u8981\u7684\u8c03\u7528\u6b21\u6570\u3002\u4e00\u79cd\u662f\u53eb\u505a \u8bb0\u5fc6\u5316 \u7684\u6280\u672f\uff0c\u5c06\u5f00\u9500\u5f88\u5927\u7684\u51fd\u6570\u8c03\u7528\u7684\u7ed3\u679c\u7f13\u5b58\u8d77\u6765\uff0c\u4ee5\u4fbf\u5728\u518d\u6b21\u51fa\u73b0\u76f8\u540c\u7684\u8f93\u5165\u65f6\u8fd4\u56de\u7ed3\u679c\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u8bb0\u5fc6\u7248\u7684\u9012\u5f52\u6590\u6ce2\u90a3\u5951\u7b97\u6cd5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include // h/t to potterman28wxcv for a variant of this code int fibonacci ( int count ) { // We'll use a static std::vector to cache calculated results static std :: vector results { 0 , 1 }; // If we've already seen this count, then use the cache'd result if ( count < static_cast < int > ( std :: size ( results ))) return results [ count ]; // Otherwise calculate the new result and add it results . push_back ( fibonacci ( count - 1 ) + fibonacci ( count - 2 )); return results [ count ]; } // And a main program to display the first 13 Fibonacci numbers int main () { for ( int count { 0 }; count < 13 ; ++ count ) std :: cout << fibonacci ( count ) << ' ' ; return 0 ; } \u8fd9\u4e2a\u8bb0\u5fc6\u7248\u672c\u8fdb\u884c\u4e8635\u6b21\u51fd\u6570\u8c03\u7528\uff0c\u6bd4\u539f\u59cb\u7b97\u6cd5\u76841205\u6b21\u597d\u5f97\u591a\u3002","title":"\u8bb0\u5fc6\u5316\u7b97\u6cd5"},{"location":"12-4-recursion/#_6","text":"\u5173\u4e8e\u9012\u5f52\u51fd\u6570\u7ecf\u5e38\u88ab\u95ee\u5230\u7684\u4e00\u4e2a\u95ee\u9898\u662f\uff0c\u201c\u5982\u679c\u53ef\u4ee5\u8fed\u4ee3\u5730\u6267\u884c\u8bb8\u591a\u76f8\u540c\u7684\u4efb\u52a1(\u4f7f\u7528for \u5faa\u73af\u6216while\u5faa\u73af)\uff0c\u4e3a\u4ec0\u4e48\u8fd8\u8981\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u5462?\u201d\u4e8b\u5b9e\u8bc1\u660e\uff0c\u4f60\u603b\u662f\u53ef\u4ee5\u8fed\u4ee3\u5730\u89e3\u51b3\u9012\u5f52\u95ee\u9898\u2014\u2014\u7136\u800c\uff0c\u5bf9\u4e8e\u590d\u6742\u7684\u95ee\u9898\uff0c\u9012\u5f52\u7248\u672c\u7684\u7f16\u5199(\u548c\u8bfb\u53d6)\u901a\u5e38\u8981\u7b80\u5355\u5f97\u591a\u3002\u4f8b\u5982\uff0c\u867d\u7136\u53ef\u4ee5\u8fed\u4ee3\u5730\u7f16\u5199Fibonacci\u51fd\u6570\uff0c\u4f46\u8fd9\u6709\u70b9\u56f0\u96be!(\u8bd5\u4e00\u8bd5!) \u8fed\u4ee3\u51fd\u6570(\u4f7f\u7528for\u5faa\u73af\u6216while\u5faa\u73af\u7684\u51fd\u6570)\u51e0\u4e4e\u603b\u662f\u6bd4\u9012\u5f52\u51fd\u6570\u66f4\u9ad8\u6548\u3002\u8fd9\u662f\u56e0\u4e3a\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u6808\u5e27\u7684\u63a8\u5165\u548c\u5f39\u51fa\u90fd\u4f1a\u4ea7\u751f\u4e00\u5b9a\u7684\u5f00\u9500\u3002\u8fed\u4ee3\u51fd\u6570\u907f\u514d\u4e86\u8fd9\u79cd\u5f00\u9500\u3002 \u8fd9\u5e76\u4e0d\u662f\u8bf4\u8fed\u4ee3\u51fd\u6570\u603b\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u6709\u65f6\u5019\uff0c\u51fd\u6570\u7684\u9012\u5f52\u5b9e\u73b0\u662f\u5982\u6b64\u7684\u7b80\u6d01\u548c\u5bb9\u6613\u9075\u5faa\uff0c\u4ee5\u81f3\u4e8e\u4e3a\u4e86\u53ef\u7ef4\u62a4\u6027\u7684\u597d\u5904\uff0c\u4ea7\u751f\u4e00\u70b9\u989d\u5916\u7684\u5f00\u9500\u662f\u503c\u5f97\u7684\uff0c\u7279\u522b\u662f\u5728\u7b97\u6cd5\u4e0d\u9700\u8981\u591a\u6b21\u9012\u5f52\u6765\u627e\u5230\u4e00\u4e2a\u89e3\u51b3\u65b9\u6848\u7684\u60c5\u51b5\u4e0b\u3002 \u5728\u6ee1\u8db3\u4e0b\u9762\u6761\u4ef6\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u9012\u5f52\u901a\u5e38\u662f\u66f4\u597d\u7684\u9009\u62e9\uff1a \u5f53\u9012\u5f52\u4ee3\u7801\u66f4\u5bb9\u6613\u7f16\u5199\u65f6\uff1b \u9012\u5f52\u7684\u6df1\u5ea6\u6709\u9650(\u4f8b\u5982\uff0c\u5b9e\u9645\u4e0a\u6ca1\u529e\u6cd5\u7f16\u5199\u5341\u4e07\u5c42\u9012\u5f52\u7684\u7b97\u6cd5\uff09\uff1b \u5f53\u8fed\u4ee3\u7248\u672c\u7684\u7b97\u6cd5\u9700\u8981\u624b\u5de5\u7ba1\u7406\u6808\u65f6\uff1b \u6027\u80fd\u8981\u6c42\u4e0d\u9ad8 \u4f46\u662f\uff0c\u5982\u679c\u9012\u5f52\u7b97\u6cd5\u66f4\u5bb9\u6613\u5b9e\u73b0\uff0c\u90a3\u4e48\u53ef\u4ee5\u5148\u9012\u5f52\u5730\u5f00\u59cb\uff0c\u7136\u540e\u518d\u4f18\u5316\u4e3a\u8fed\u4ee3\u7b97\u6cd5\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e00\u822c\u6765\u8bf4\uff0c\u8fed\u4ee3\u6bd4\u9012\u5f52\u66f4\u53d7\u6b22\u8fce\uff0c\u9664\u975e\u9012\u5f52\u786e\u5b9e\u6709\u610f\u4e49\u3002","title":"\u9012\u5f52\u548c\u8fed\u4ee3"},{"location":"12-5-command-line-arguments/","tags":["argument"],"text":"Key Takeaway \u547d\u4ee4\u884c\u53c2\u6570\u7684\u7528\u9014 \u5728 0.5 -- Introduction to the compiler, linker, and libraries \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u7f16\u8bd1\u548c\u94fe\u63a5\u540e\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u3002\u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u5219\u4f1a\u4ece main() \u51fd\u6570\u7684\u9876\u90e8\u5f00\u59cb\u8fd0\u884c\u3002 main() \u51fd\u6570\u5b9a\u4e49\u5982\u4e0b\uff1a 1 int main () \u6ce8\u610f\uff0c\u8fd9\u91cc\u7684 main() \u51fd\u6570\u6ca1\u6709 \u5f62\u53c2 \u3002\u4f46\u662f\u5f88\u591a\u7a0b\u5e8f\u5176\u5b9e\u9700\u8981\u57fa\u4e8e\u8f93\u5165\u6765\u5224\u65ad\u5982\u4f55\u5f00\u59cb\u5de5\u4f5c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a\u8bfb\u53d6\u56fe\u7247\u4f5c\u4e3a\u7f29\u7565\u56fe\u7684\u7a0b\u5e8f Thumbnail \u3002\u90a3\u4e48 Thumbnail \u5e94\u8be5\u8bfb\u53d6\u54ea\u4e2a\u56fe\u7247\u5e76\u5904\u7406\u5462\uff1f\u7528\u6237\u5fc5\u987b\u6709\u529e\u6cd5\u544a\u8bc9\u7a0b\u5e8f\u5e94\u8be5\u6253\u5f00\u54ea\u4e2a\u6587\u4ef6\uff0c\u6b64\u65f6\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // Program: Thumbnail #include #include int main () { std :: cout << \"Please enter an image filename to create a thumbnail for: \" ; std :: string filename {}; std :: cin >> filename ; // open image file // create thumbnail // output thumbnail } \u4f46\u662f\uff0c\u8fd9\u4e48\u505a\u6709\u4e00\u4e2a\u6f5c\u5728\u7684\u95ee\u9898\u3002\u6bcf\u6b21\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u90fd\u5fc5\u987b\u7b49\u5f85\u7528\u6237\u7684\u8f93\u5165\u624d\u80fd\u7ee7\u7eed\u5de5\u4f5c\u3002\u5982\u679c\u6211\u4eec\u6bcf\u6b21\u90fd\u662f\u624b\u52a8\u542f\u52a8\u8be5\u7a0b\u5e8f\u7684\u8bdd\uff0c\u8fd9\u4f3c\u4e4e\u4e5f\u6ca1\u4ec0\u4e48\u95ee\u9898\u3002\u4f46\u662f\u5f88\u591a\u65f6\u5019\u8fd9\u4e48\u505a\u662f\u4e0d\u5408\u9002\u7684\uff0c\u4f8b\u5982\u4f60\u9700\u8981\u5bf9\u591a\u4e2a\u6587\u4ef6\u6267\u884c\u8be5\u7a0b\u5e8f\u6216\u8005\u4ece\u53e6\u5916\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\u8c03\u7528\u8be5\u7a0b\u5e8f\u3002 \u8ba9\u6211\u4eec\u8fdb\u4e00\u6b65\u7814\u7a76\u8fd9\u4e9b\u6848\u4f8b\u3002 \u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u4f60\u5e0c\u671b\u4e3a\u7ed9\u5b9a\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u56fe\u50cf\u6587\u4ef6\u521b\u5efa\u7f29\u7565\u56fe\u3002\u4f60\u4f1a\u600e\u4e48\u505a?\u53ea\u8981\u76ee\u5f55\u4e2d\u6709\u56fe\u50cf\uff0c\u4f60\u5c31\u53ef\u4ee5\u591a\u6b21\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u624b\u52a8\u952e\u5165\u6bcf\u4e2a\u6587\u4ef6\u540d\u3002\u7136\u800c\uff0c\u5982\u679c\u6709\u6570\u767e\u4e2a\u56fe\u50cf\uff0c\u8fd9\u53ef\u80fd\u9700\u8981\u4e00\u6574\u5929\uff01\u4e00\u4e2a\u597d\u7684\u89e3\u51b3\u65b9\u6848\u662f\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\u5faa\u73af\u904d\u5386\u76ee\u5f55\u4e2d\u7684\u6bcf\u4e2a\u6587\u4ef6\u540d\uff0c\u4e3a\u6bcf\u4e2a\u6587\u4ef6\u8c03\u7528\u4e00\u6b21 Thumbnail \u3002 \u73b0\u5728\u8003\u8651\u8fd9\u6837\u4e00\u4e2a\u60c5\u51b5\uff0c\u4f60\u6b63\u5728\u8fd0\u8425\u4e00\u4e2a\u7f51\u7ad9\uff0c\u4f60\u60f3\u8ba9\u4f60\u7684\u7f51\u7ad9\u5728\u7528\u6237\u6bcf\u6b21\u4e0a\u4f20\u56fe\u7247\u5230\u4f60\u7684\u7f51\u7ad9\u65f6\u521b\u5efa\u4e00\u4e2a\u7f29\u7565\u56fe\u3002\u8fd9\u4e2a\u7a0b\u5e8f\u6ca1\u6709\u8bbe\u7f6e\u4e3a\u63a5\u53d7\u6765\u81ea\u7f51\u7edc\u7684\u8f93\u5165\uff0c\u90a3\u4e48\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e0a\u4f20\u8005\u5982\u4f55\u8f93\u5165\u6587\u4ef6\u540d\u5462?\u4e00\u4e2a\u597d\u7684\u89e3\u51b3\u65b9\u6848\u662f\u8ba9\u4f60\u7684web\u670d\u52a1\u5668\u5728\u4e0a\u4f20\u540e\u81ea\u52a8\u8c03\u7528 Thumbnail \u3002 \u5728\u8fd9\u4e24\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u786e\u5b9e\u9700\u8981\u4e00\u79cd\u65b9\u6cd5\uff0c\u8ba9\u4e00\u4e2a\u5916\u90e8\u7a0b\u5e8f\u5728 Thumbnail \u542f\u52a8\u65f6\u5c06\u6587\u4ef6\u540d\u4f5c\u4e3a\u8f93\u5165\u4f20\u9012\u7ed9 Thumbnail \u7a0b\u5e8f\uff0c\u800c\u4e0d\u662f\u8ba9 Thumbnail \u5728\u542f\u52a8\u540e\u7b49\u5f85\u7528\u6237\u8f93\u5165\u6587\u4ef6\u540d\u3002 \u547d\u4ee4\u884c\u53c2\u6570 \u662f\u53ef\u9009\u7684\u5b57\u7b26\u4e32\u53c2\u6570\uff0c\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u7531\u64cd\u4f5c\u7cfb\u7edf\u4f20\u9012\u7ed9\u5b83\u3002\u7136\u540e\u7a0b\u5e8f\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u4f5c\u4e3a\u8f93\u5165(\u6216\u5ffd\u7565\u5b83\u4eec)\u3002\u5c31\u50cf\u51fd\u6570\u5f62\u53c2\u4e3a\u51fd\u6570\u63d0\u4f9b\u4e86\u5411\u53e6\u4e00\u4e2a\u51fd\u6570\u63d0\u4f9b\u8f93\u5165\u7684\u65b9\u6cd5\u4e00\u6837\uff0c\u547d\u4ee4\u884c\u5b9e\u53c2\u4e3a\u4eba\u4eec\u6216\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u5411\u7a0b\u5e8f\u63d0\u4f9b\u8f93\u5165\u7684\u65b9\u6cd5\u3002 \u4f20\u9012\u547d\u4ee4\u884c\u53c2\u6570 \u53ef\u6267\u884c\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u540d\u79f0\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u3002\u4f8b\u5982\uff0c\u8981\u8fd0\u884cWindows\u673a\u5668\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u53ef\u6267\u884c\u6587\u4ef6\" WordCount \"\uff0c\u4f60\u53ef\u4ee5\u8f93\u5165: 1 WordCount \u5728UNIX\u7c7b\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u4f7f\u7528\uff1a 1 ./WordCount \u7ed9 WordCount \u4f20\u9012\u53c2\u6570\u5f88\u7b80\u5355\uff0c\u53ea\u8981\u5c06\u53c2\u6570\u653e\u5728\u7a0b\u5e8f\u540d\u540e\u9762\u5373\u53ef\uff1a 1 WordCount Myfile.txt \u5f53 WordCount \u6267\u884c\u65f6 Myfile.txt \u4f1a\u4f5c\u4e3a\u547d\u4ee4\u884c\u5b9e\u53c2\u4f20\u5165\uff0c\u5982\u679c\u7a0b\u5e8f\u9700\u8981\u591a\u4e2a\u53c2\u6570\uff0c\u5219\u9700\u8981\u5c06\u5b83\u4eec\u7528\u7a7a\u683c\u9694\u5f00\uff1a 1 WordCount Myfile.txt Myotherfile.txt \u5982\u679c\u4eceIDE\u8fd0\u884c\u7a0b\u5e8f\uff0cIDE\u5e94\u8be5\u63d0\u4f9b\u8f93\u5165\u547d\u4ee4\u884c\u53c2\u6570\u7684\u65b9\u6cd5\u3002 \u5728Microsoft Visual Studio\u4e2d\uff0c\u5728\u89e3\u51b3\u65b9\u6848\u8d44\u6e90\u7ba1\u7406\u5668\u4e2d\u53f3\u952e\u5355\u51fb\u9879\u76ee\uff0c\u7136\u540e\u9009\u62e9\u5c5e\u6027\u3002\u6253\u5f00\u201cConfiguration Properties\u201d\uff0c\u9009\u62e9\u201cDebugging\u201d\u3002\u5728\u53f3\u7a97\u683c\u4e2d\uff0c\u6709\u4e00\u884c\u79f0\u4e3a\u201c\u547d\u4ee4\u53c2\u6570\u201d\u3002\u4f60\u53ef\u4ee5\u5728\u8fd9\u91cc\u8f93\u5165\u547d\u4ee4\u884c\u53c2\u6570\u8fdb\u884c\u6d4b\u8bd5\uff0c\u5f53\u4f60\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u5b83\u4eec\u5c06\u81ea\u52a8\u4f20\u9012\u7ed9\u4f60\u7684\u7a0b\u5e8f\u3002 \u5728Code::Blocks\u4e2d\uff0c\u9009\u62e9\" Project -> Set program 's arguments \"\u3002 \u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570 \u73b0\u5728\u4f60\u5df2\u7ecf\u77e5\u9053\u5982\u4f55\u5411\u7a0b\u5e8f\u63d0\u4f9b\u547d\u4ee4\u884c\u53c2\u6570\u4e86\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u4eceC++\u7a0b\u5e8f\u4e2d\u8bbf\u95ee\u5b83\u4eec\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u79cd\u4e0d\u540c\u4e8e\u4ee5\u5f80\u7684 main() \u5f62\u5f0f\u3002 main() \u7684\u8fd9\u79cd\u65b0\u5f62\u5f0f\u6709\u4e24\u4e2a\u53c2\u6570(\u6309\u7167\u7ea6\u5b9a\u547d\u540d\u4e3a argc \u548c argv )\uff1a 1 int main ( int argc , char * argv []) \u6709\u65f6\u4f60\u4f1a\u770b\u5230\u5b83\u88ab\u5199\u6210\uff1a 1 int main ( int argc , char ** argv ) \u5c3d\u7ba1\u8fd9\u4e24\u79cd\u8868\u793a\u662f\u76f8\u540c\u7684\uff0c\u4f46\u6211\u4eec\u66f4\u559c\u6b22\u7b2c\u4e00\u79cd\u8868\u793a\uff0c\u56e0\u4e3a\u5b83\u66f4\u76f4\u89c2\uff0c\u5bb9\u6613\u7406\u89e3\u3002 argc \u662f\u4e00\u4e2a\u6574\u6570\u53c2\u6570\uff0c\u5305\u542b\u4f20\u9012\u7ed9\u7a0b\u5e8f\u7684\u53c2\u6570\u6570\u91cf(\u8bf7\u8003\u8651\uff1aargc = arg ument c ount)\u3002 argc \u6700\u5c0f\u662f1\uff0c\u56e0\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\u603b\u662f\u7a0b\u5e8f\u672c\u8eab\u7684\u540d\u79f0\u3002\u7528\u6237\u63d0\u4f9b\u7684\u6bcf\u4e2a\u547d\u4ee4\u884c\u53c2\u6570\u5c06\u5bfc\u81f4 argc \u589e\u52a01\u3002 argv \u662f\u5b58\u50a8\u5b9e\u9645\u53c2\u6570\u503c\u7684\u5730\u65b9(\u8bf7\u8003\u8651\uff1aargv = arg ument v values\uff0c\u867d\u7136\u5b83\u7684\u6b63\u786e\u540d\u79f0\u662f\u201c\u53c2\u6570\u5411\u91cf\u201d)\u3002\u5c3d\u7ba1argv\u7684\u58f0\u660e\u770b\u8d77\u6765\u5f88\u5413\u4eba\uff0c\u4f46argv\u5b9e\u9645\u4e0a\u53ea\u662f\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u6570\u7ec4\u3002\u8fd9\u4e2a\u6570\u7ec4\u7684\u957f\u5ea6\u662fargc\u3002 \u8ba9\u6211\u4eec\u5199\u4e00\u4e2a\u540d\u4e3a\u201cMyArgs\u201d\u7684\u5c0f\u7a0b\u5e8f\u6765\u6253\u5370\u6240\u6709\u547d\u4ee4\u884c\u53c2\u6570\u7684\u503c: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // Program: MyArgs #include int main ( int argc , char * argv []) { std :: cout << \"There are \" << argc << \" arguments: \\n \" ; // Loop through each argument and print its number and value for ( int count { 0 }; count < argc ; ++ count ) { std :: cout << count << ' ' << argv [ count ] << '\\n' ; } return 0 ; } \u73b0\u5728\uff0c\u5f53\u6211\u4eec\u7528\u547d\u4ee4\u884c\u53c2\u6570\u201cMyfile.txt\u201d\u548c\u201c100\u201d\u8c03\u7528\u8fd9\u4e2a\u7a0b\u5e8f(MyArgs)\u65f6\uff0c\u8f93\u51fa\u5c06\u5982\u4e0b\u6240\u793a: 1 2 3 4 There are 3 arguments: 0 C:\\MyArgs 1 Myfile.txt 2 100 \u53c2\u65700\u662f\u6b63\u5728\u8fd0\u884c\u7684\u5f53\u524d\u7a0b\u5e8f\u7684\u8def\u5f84\u548c\u540d\u79f0\u3002\u672c\u4f8b\u4e2d\u7684\u53c2\u65701\u548c2\u662f\u6211\u4eec\u4f20\u5165\u7684\u4e24\u4e2a\u547d\u4ee4\u884c\u53c2\u6570\u3002 \u5904\u7406\u6570\u503c\u53c2\u6570 \u547d\u4ee4\u884c\u53c2\u6570\u603b\u662f\u4f5c\u4e3a\u5b57\u7b26\u4e32\u4f20\u9012\uff0c\u5373\u4f7f\u63d0\u4f9b\u7684\u503c\u672c\u8d28\u4e0a\u662f\u6570\u503c\u3002\u82e5\u8981\u5c06\u547d\u4ee4\u884c\u53c2\u6570\u7528\u4f5c\u6570\u5b57\uff0c\u5fc5\u987b\u5c06\u5176\u4ece\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\u3002\u4e0d\u5e78\u7684\u662f\uff0c\u5728C++\u4e2d\uff0c\u5b57\u7b26\u4e32\u8f6c\u6570\u5b57\u6bd4\u4f60\u60f3\u8c61\u7684\u8981\u590d\u6742\u3002 \u5728C++\u4e2d\u9700\u8981\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include #include // for std::stringstream #include int main ( int argc , char * argv []) { if ( argc <= 1 ) { // On some operating systems, argv[0] can end up as an empty string instead of the program's name. // We'll conditionalize our response on whether argv[0] is empty or not. if ( argv [ 0 ]) std :: cout << \"Usage: \" << argv [ 0 ] << \" \" << '\\n' ; else std :: cout << \"Usage: \" << '\\n' ; return 1 ; } std :: stringstream convert { argv [ 1 ] }; // \u521b\u5efa\u4e00\u4e2a stringstream \u53d8\u91cf convert\uff0c\u4f7f\u7528 argv[1] \u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316 int myint {}; if ( ! ( convert >> myint )) // do the conversion myint = 0 ; // if conversion fails, set myint to a default value std :: cout << \"Got integer: \" << myint << '\\n' ; return 0 ; } \u8fd0\u884c\u8be5\u7a0b\u5e8f\u5e76\u5c06\"567\"\u4f5c\u4e3a\u53c2\u6570\uff1a 1 Got integer: 567 std::stringstream \u548c std::cin \u7684\u5de5\u4f5c\u65b9\u5f0f\u7c7b\u4f3c\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d argv[1] \u9996\u5148\u7528\u4e8e\u521d\u59cb\u5316 stringstream \uff0c\u6240\u4ee5\u7a0d\u540e\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 >> \u5c06\u5176\u503c\u63d0\u53d6\u5230\u4e00\u4e2a\u6574\u578b\u53d8\u91cf ( \u7c7b\u4f3c\u4e8e std::cin )\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u7ae0\u8282\u4e2d\u5bf9 std::stringstream \u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002 \u64cd\u4f5c\u7cfb\u7edf\u9996\u5148\u89e3\u6790\u547d\u4ee4\u884c \u5f53\u4f60\u5728\u547d\u4ee4\u884c(\u6216\u4eceIDE\u8fd0\u884c\u60a8\u7684\u7a0b\u5e8f)\u4e0a\u952e\u5165\u67d0\u4e9b\u5185\u5bb9\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u9700\u8981\u8d1f\u8d23\u89e3\u6790\u6216\u8f6c\u53d1\u76f8\u5e94\u7684\u8bf7\u6c42\u3002\u8fd9\u4e0d\u4ec5\u6d89\u53ca\u5230\u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u8fd8\u6d89\u53ca\u5230\u89e3\u6790\u4efb\u4f55\u53c2\u6570\uff0c\u4ee5\u786e\u5b9a\u5e94\u8be5\u5982\u4f55\u5904\u7406\u5b83\u4eec\u5e76\u5c06\u5b83\u4eec\u4f20\u9012\u7ed9\u5e94\u7528\u7a0b\u5e8f\u3002 \u4e00\u822c\u6765\u8bf4\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5bf9\u4e8e\u5982\u4f55\u5904\u7406\u53cc\u5f15\u53f7\u548c\u53cd\u659c\u6760\u7b49\u7279\u6b8a\u5b57\u7b26\u6709\u7279\u6b8a\u7684\u89c4\u5219\u3002 \u4f8b\u5982\uff1a 1 MyArgs Hello world! \u6253\u5370\uff1a 1 2 3 4 There are 3 arguments: 0 C:\\MyArgs 1 Hello 2 world! \u901a\u5e38\uff0c\u4ee5\u53cc\u5f15\u53f7\u4f20\u9012\u7684\u5b57\u7b26\u4e32\u88ab\u8ba4\u4e3a\u662f\u540c\u4e00\u5b57\u7b26\u4e32\u7684\u4e00\u90e8\u5206: 1 MyArgs \"Hello world!\" \u6253\u5370\uff1a 1 2 3 There are 2 arguments: 0 C:\\MyArgs 1 Hello world! \u5927\u591a\u6570\u64cd\u4f5c\u7cfb\u7edf\u90fd\u5141\u8bb8\u4f60\u901a\u8fc7\u8f6c\u8bd1\u53cc\u5f15\u53f7\u6765\u4f7f\u7528\u5b57\u9762\u53cc\u5f15\u53f7\uff1a 1 MyArgs \\\"Hello world!\\\" \u6253\u5370\uff1a 1 2 3 4 There are 3 arguments: 0 C:\\MyArgs 1 \"Hello 2 world!\" \u5176\u4ed6\u5b57\u7b26\u53ef\u80fd\u4e5f\u9700\u8981\u8f6c\u4e49\uff0c\u8fd9\u53d6\u51b3\u4e8e\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u5982\u4f55\u89e3\u91ca\u5b83\u4eec\u3002 \u5c0f\u7ed3 \u547d\u4ee4\u884c\u53c2\u6570\u4e3a\u7528\u6237\u6216\u5176\u4ed6\u7a0b\u5e8f\u5728\u542f\u52a8\u65f6\u5411\u7a0b\u5e8f\u4f20\u9012\u8f93\u5165\u6570\u636e\u63d0\u4f9b\u4e86\u4e00\u79cd\u5f88\u597d\u7684\u65b9\u5f0f\u3002\u5bf9\u4e8e\u4efb\u4f55\u7a0b\u5e8f\u9700\u8981\u7684\u6570\u636e\uff0c\u8bf7\u8003\u8651\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u5141\u8bb8\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u6765\u4f20\u9012\u8fd9\u4e9b\u8f93\u5165\u6570\u636e\u3002\u5982\u679c\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u8be5\u6570\u636e\u6ca1\u6709\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570 \u4f20\u5165\uff0c\u4f60\u4e5f\u662f\u53ef\u4ee5\u68c0\u6d4b\u5230\u7684\uff0c\u6b64\u65f6\u4f60\u53ef\u4ee5\u518d\u8981\u6c42\u7528\u6237\u8fdb\u884c\u8f93\u5165\u3002\u8fd9\u6837\uff0c\u7a0b\u5e8f\u5c31\u53ef\u4ee5\u4ee5\u4efb\u4f55\u4e00\u79cd\u65b9\u5f0f\u8fd0\u884c\u3002","title":"12.5 - \u547d\u4ee4\u884c\u53c2\u6570"},{"location":"12-5-command-line-arguments/#_1","text":"\u5728 0.5 -- Introduction to the compiler, linker, and libraries \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u7f16\u8bd1\u548c\u94fe\u63a5\u540e\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u3002\u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u5219\u4f1a\u4ece main() \u51fd\u6570\u7684\u9876\u90e8\u5f00\u59cb\u8fd0\u884c\u3002 main() \u51fd\u6570\u5b9a\u4e49\u5982\u4e0b\uff1a 1 int main () \u6ce8\u610f\uff0c\u8fd9\u91cc\u7684 main() \u51fd\u6570\u6ca1\u6709 \u5f62\u53c2 \u3002\u4f46\u662f\u5f88\u591a\u7a0b\u5e8f\u5176\u5b9e\u9700\u8981\u57fa\u4e8e\u8f93\u5165\u6765\u5224\u65ad\u5982\u4f55\u5f00\u59cb\u5de5\u4f5c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a\u8bfb\u53d6\u56fe\u7247\u4f5c\u4e3a\u7f29\u7565\u56fe\u7684\u7a0b\u5e8f Thumbnail \u3002\u90a3\u4e48 Thumbnail \u5e94\u8be5\u8bfb\u53d6\u54ea\u4e2a\u56fe\u7247\u5e76\u5904\u7406\u5462\uff1f\u7528\u6237\u5fc5\u987b\u6709\u529e\u6cd5\u544a\u8bc9\u7a0b\u5e8f\u5e94\u8be5\u6253\u5f00\u54ea\u4e2a\u6587\u4ef6\uff0c\u6b64\u65f6\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // Program: Thumbnail #include #include int main () { std :: cout << \"Please enter an image filename to create a thumbnail for: \" ; std :: string filename {}; std :: cin >> filename ; // open image file // create thumbnail // output thumbnail } \u4f46\u662f\uff0c\u8fd9\u4e48\u505a\u6709\u4e00\u4e2a\u6f5c\u5728\u7684\u95ee\u9898\u3002\u6bcf\u6b21\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u90fd\u5fc5\u987b\u7b49\u5f85\u7528\u6237\u7684\u8f93\u5165\u624d\u80fd\u7ee7\u7eed\u5de5\u4f5c\u3002\u5982\u679c\u6211\u4eec\u6bcf\u6b21\u90fd\u662f\u624b\u52a8\u542f\u52a8\u8be5\u7a0b\u5e8f\u7684\u8bdd\uff0c\u8fd9\u4f3c\u4e4e\u4e5f\u6ca1\u4ec0\u4e48\u95ee\u9898\u3002\u4f46\u662f\u5f88\u591a\u65f6\u5019\u8fd9\u4e48\u505a\u662f\u4e0d\u5408\u9002\u7684\uff0c\u4f8b\u5982\u4f60\u9700\u8981\u5bf9\u591a\u4e2a\u6587\u4ef6\u6267\u884c\u8be5\u7a0b\u5e8f\u6216\u8005\u4ece\u53e6\u5916\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\u8c03\u7528\u8be5\u7a0b\u5e8f\u3002 \u8ba9\u6211\u4eec\u8fdb\u4e00\u6b65\u7814\u7a76\u8fd9\u4e9b\u6848\u4f8b\u3002 \u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u4f60\u5e0c\u671b\u4e3a\u7ed9\u5b9a\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u56fe\u50cf\u6587\u4ef6\u521b\u5efa\u7f29\u7565\u56fe\u3002\u4f60\u4f1a\u600e\u4e48\u505a?\u53ea\u8981\u76ee\u5f55\u4e2d\u6709\u56fe\u50cf\uff0c\u4f60\u5c31\u53ef\u4ee5\u591a\u6b21\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u624b\u52a8\u952e\u5165\u6bcf\u4e2a\u6587\u4ef6\u540d\u3002\u7136\u800c\uff0c\u5982\u679c\u6709\u6570\u767e\u4e2a\u56fe\u50cf\uff0c\u8fd9\u53ef\u80fd\u9700\u8981\u4e00\u6574\u5929\uff01\u4e00\u4e2a\u597d\u7684\u89e3\u51b3\u65b9\u6848\u662f\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\u5faa\u73af\u904d\u5386\u76ee\u5f55\u4e2d\u7684\u6bcf\u4e2a\u6587\u4ef6\u540d\uff0c\u4e3a\u6bcf\u4e2a\u6587\u4ef6\u8c03\u7528\u4e00\u6b21 Thumbnail \u3002 \u73b0\u5728\u8003\u8651\u8fd9\u6837\u4e00\u4e2a\u60c5\u51b5\uff0c\u4f60\u6b63\u5728\u8fd0\u8425\u4e00\u4e2a\u7f51\u7ad9\uff0c\u4f60\u60f3\u8ba9\u4f60\u7684\u7f51\u7ad9\u5728\u7528\u6237\u6bcf\u6b21\u4e0a\u4f20\u56fe\u7247\u5230\u4f60\u7684\u7f51\u7ad9\u65f6\u521b\u5efa\u4e00\u4e2a\u7f29\u7565\u56fe\u3002\u8fd9\u4e2a\u7a0b\u5e8f\u6ca1\u6709\u8bbe\u7f6e\u4e3a\u63a5\u53d7\u6765\u81ea\u7f51\u7edc\u7684\u8f93\u5165\uff0c\u90a3\u4e48\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e0a\u4f20\u8005\u5982\u4f55\u8f93\u5165\u6587\u4ef6\u540d\u5462?\u4e00\u4e2a\u597d\u7684\u89e3\u51b3\u65b9\u6848\u662f\u8ba9\u4f60\u7684web\u670d\u52a1\u5668\u5728\u4e0a\u4f20\u540e\u81ea\u52a8\u8c03\u7528 Thumbnail \u3002 \u5728\u8fd9\u4e24\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u786e\u5b9e\u9700\u8981\u4e00\u79cd\u65b9\u6cd5\uff0c\u8ba9\u4e00\u4e2a\u5916\u90e8\u7a0b\u5e8f\u5728 Thumbnail \u542f\u52a8\u65f6\u5c06\u6587\u4ef6\u540d\u4f5c\u4e3a\u8f93\u5165\u4f20\u9012\u7ed9 Thumbnail \u7a0b\u5e8f\uff0c\u800c\u4e0d\u662f\u8ba9 Thumbnail \u5728\u542f\u52a8\u540e\u7b49\u5f85\u7528\u6237\u8f93\u5165\u6587\u4ef6\u540d\u3002 \u547d\u4ee4\u884c\u53c2\u6570 \u662f\u53ef\u9009\u7684\u5b57\u7b26\u4e32\u53c2\u6570\uff0c\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u7531\u64cd\u4f5c\u7cfb\u7edf\u4f20\u9012\u7ed9\u5b83\u3002\u7136\u540e\u7a0b\u5e8f\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u4f5c\u4e3a\u8f93\u5165(\u6216\u5ffd\u7565\u5b83\u4eec)\u3002\u5c31\u50cf\u51fd\u6570\u5f62\u53c2\u4e3a\u51fd\u6570\u63d0\u4f9b\u4e86\u5411\u53e6\u4e00\u4e2a\u51fd\u6570\u63d0\u4f9b\u8f93\u5165\u7684\u65b9\u6cd5\u4e00\u6837\uff0c\u547d\u4ee4\u884c\u5b9e\u53c2\u4e3a\u4eba\u4eec\u6216\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u5411\u7a0b\u5e8f\u63d0\u4f9b\u8f93\u5165\u7684\u65b9\u6cd5\u3002","title":"\u547d\u4ee4\u884c\u53c2\u6570\u7684\u7528\u9014"},{"location":"12-5-command-line-arguments/#_2","text":"\u53ef\u6267\u884c\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u540d\u79f0\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u3002\u4f8b\u5982\uff0c\u8981\u8fd0\u884cWindows\u673a\u5668\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u53ef\u6267\u884c\u6587\u4ef6\" WordCount \"\uff0c\u4f60\u53ef\u4ee5\u8f93\u5165: 1 WordCount \u5728UNIX\u7c7b\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u4f7f\u7528\uff1a 1 ./WordCount \u7ed9 WordCount \u4f20\u9012\u53c2\u6570\u5f88\u7b80\u5355\uff0c\u53ea\u8981\u5c06\u53c2\u6570\u653e\u5728\u7a0b\u5e8f\u540d\u540e\u9762\u5373\u53ef\uff1a 1 WordCount Myfile.txt \u5f53 WordCount \u6267\u884c\u65f6 Myfile.txt \u4f1a\u4f5c\u4e3a\u547d\u4ee4\u884c\u5b9e\u53c2\u4f20\u5165\uff0c\u5982\u679c\u7a0b\u5e8f\u9700\u8981\u591a\u4e2a\u53c2\u6570\uff0c\u5219\u9700\u8981\u5c06\u5b83\u4eec\u7528\u7a7a\u683c\u9694\u5f00\uff1a 1 WordCount Myfile.txt Myotherfile.txt \u5982\u679c\u4eceIDE\u8fd0\u884c\u7a0b\u5e8f\uff0cIDE\u5e94\u8be5\u63d0\u4f9b\u8f93\u5165\u547d\u4ee4\u884c\u53c2\u6570\u7684\u65b9\u6cd5\u3002 \u5728Microsoft Visual Studio\u4e2d\uff0c\u5728\u89e3\u51b3\u65b9\u6848\u8d44\u6e90\u7ba1\u7406\u5668\u4e2d\u53f3\u952e\u5355\u51fb\u9879\u76ee\uff0c\u7136\u540e\u9009\u62e9\u5c5e\u6027\u3002\u6253\u5f00\u201cConfiguration Properties\u201d\uff0c\u9009\u62e9\u201cDebugging\u201d\u3002\u5728\u53f3\u7a97\u683c\u4e2d\uff0c\u6709\u4e00\u884c\u79f0\u4e3a\u201c\u547d\u4ee4\u53c2\u6570\u201d\u3002\u4f60\u53ef\u4ee5\u5728\u8fd9\u91cc\u8f93\u5165\u547d\u4ee4\u884c\u53c2\u6570\u8fdb\u884c\u6d4b\u8bd5\uff0c\u5f53\u4f60\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u5b83\u4eec\u5c06\u81ea\u52a8\u4f20\u9012\u7ed9\u4f60\u7684\u7a0b\u5e8f\u3002 \u5728Code::Blocks\u4e2d\uff0c\u9009\u62e9\" Project -> Set program 's arguments \"\u3002","title":"\u4f20\u9012\u547d\u4ee4\u884c\u53c2\u6570"},{"location":"12-5-command-line-arguments/#_3","text":"\u73b0\u5728\u4f60\u5df2\u7ecf\u77e5\u9053\u5982\u4f55\u5411\u7a0b\u5e8f\u63d0\u4f9b\u547d\u4ee4\u884c\u53c2\u6570\u4e86\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u4eceC++\u7a0b\u5e8f\u4e2d\u8bbf\u95ee\u5b83\u4eec\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u79cd\u4e0d\u540c\u4e8e\u4ee5\u5f80\u7684 main() \u5f62\u5f0f\u3002 main() \u7684\u8fd9\u79cd\u65b0\u5f62\u5f0f\u6709\u4e24\u4e2a\u53c2\u6570(\u6309\u7167\u7ea6\u5b9a\u547d\u540d\u4e3a argc \u548c argv )\uff1a 1 int main ( int argc , char * argv []) \u6709\u65f6\u4f60\u4f1a\u770b\u5230\u5b83\u88ab\u5199\u6210\uff1a 1 int main ( int argc , char ** argv ) \u5c3d\u7ba1\u8fd9\u4e24\u79cd\u8868\u793a\u662f\u76f8\u540c\u7684\uff0c\u4f46\u6211\u4eec\u66f4\u559c\u6b22\u7b2c\u4e00\u79cd\u8868\u793a\uff0c\u56e0\u4e3a\u5b83\u66f4\u76f4\u89c2\uff0c\u5bb9\u6613\u7406\u89e3\u3002 argc \u662f\u4e00\u4e2a\u6574\u6570\u53c2\u6570\uff0c\u5305\u542b\u4f20\u9012\u7ed9\u7a0b\u5e8f\u7684\u53c2\u6570\u6570\u91cf(\u8bf7\u8003\u8651\uff1aargc = arg ument c ount)\u3002 argc \u6700\u5c0f\u662f1\uff0c\u56e0\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\u603b\u662f\u7a0b\u5e8f\u672c\u8eab\u7684\u540d\u79f0\u3002\u7528\u6237\u63d0\u4f9b\u7684\u6bcf\u4e2a\u547d\u4ee4\u884c\u53c2\u6570\u5c06\u5bfc\u81f4 argc \u589e\u52a01\u3002 argv \u662f\u5b58\u50a8\u5b9e\u9645\u53c2\u6570\u503c\u7684\u5730\u65b9(\u8bf7\u8003\u8651\uff1aargv = arg ument v values\uff0c\u867d\u7136\u5b83\u7684\u6b63\u786e\u540d\u79f0\u662f\u201c\u53c2\u6570\u5411\u91cf\u201d)\u3002\u5c3d\u7ba1argv\u7684\u58f0\u660e\u770b\u8d77\u6765\u5f88\u5413\u4eba\uff0c\u4f46argv\u5b9e\u9645\u4e0a\u53ea\u662f\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u6570\u7ec4\u3002\u8fd9\u4e2a\u6570\u7ec4\u7684\u957f\u5ea6\u662fargc\u3002 \u8ba9\u6211\u4eec\u5199\u4e00\u4e2a\u540d\u4e3a\u201cMyArgs\u201d\u7684\u5c0f\u7a0b\u5e8f\u6765\u6253\u5370\u6240\u6709\u547d\u4ee4\u884c\u53c2\u6570\u7684\u503c: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // Program: MyArgs #include int main ( int argc , char * argv []) { std :: cout << \"There are \" << argc << \" arguments: \\n \" ; // Loop through each argument and print its number and value for ( int count { 0 }; count < argc ; ++ count ) { std :: cout << count << ' ' << argv [ count ] << '\\n' ; } return 0 ; } \u73b0\u5728\uff0c\u5f53\u6211\u4eec\u7528\u547d\u4ee4\u884c\u53c2\u6570\u201cMyfile.txt\u201d\u548c\u201c100\u201d\u8c03\u7528\u8fd9\u4e2a\u7a0b\u5e8f(MyArgs)\u65f6\uff0c\u8f93\u51fa\u5c06\u5982\u4e0b\u6240\u793a: 1 2 3 4 There are 3 arguments: 0 C:\\MyArgs 1 Myfile.txt 2 100 \u53c2\u65700\u662f\u6b63\u5728\u8fd0\u884c\u7684\u5f53\u524d\u7a0b\u5e8f\u7684\u8def\u5f84\u548c\u540d\u79f0\u3002\u672c\u4f8b\u4e2d\u7684\u53c2\u65701\u548c2\u662f\u6211\u4eec\u4f20\u5165\u7684\u4e24\u4e2a\u547d\u4ee4\u884c\u53c2\u6570\u3002","title":"\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570"},{"location":"12-5-command-line-arguments/#_4","text":"\u547d\u4ee4\u884c\u53c2\u6570\u603b\u662f\u4f5c\u4e3a\u5b57\u7b26\u4e32\u4f20\u9012\uff0c\u5373\u4f7f\u63d0\u4f9b\u7684\u503c\u672c\u8d28\u4e0a\u662f\u6570\u503c\u3002\u82e5\u8981\u5c06\u547d\u4ee4\u884c\u53c2\u6570\u7528\u4f5c\u6570\u5b57\uff0c\u5fc5\u987b\u5c06\u5176\u4ece\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\u3002\u4e0d\u5e78\u7684\u662f\uff0c\u5728C++\u4e2d\uff0c\u5b57\u7b26\u4e32\u8f6c\u6570\u5b57\u6bd4\u4f60\u60f3\u8c61\u7684\u8981\u590d\u6742\u3002 \u5728C++\u4e2d\u9700\u8981\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include #include // for std::stringstream #include int main ( int argc , char * argv []) { if ( argc <= 1 ) { // On some operating systems, argv[0] can end up as an empty string instead of the program's name. // We'll conditionalize our response on whether argv[0] is empty or not. if ( argv [ 0 ]) std :: cout << \"Usage: \" << argv [ 0 ] << \" \" << '\\n' ; else std :: cout << \"Usage: \" << '\\n' ; return 1 ; } std :: stringstream convert { argv [ 1 ] }; // \u521b\u5efa\u4e00\u4e2a stringstream \u53d8\u91cf convert\uff0c\u4f7f\u7528 argv[1] \u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316 int myint {}; if ( ! ( convert >> myint )) // do the conversion myint = 0 ; // if conversion fails, set myint to a default value std :: cout << \"Got integer: \" << myint << '\\n' ; return 0 ; } \u8fd0\u884c\u8be5\u7a0b\u5e8f\u5e76\u5c06\"567\"\u4f5c\u4e3a\u53c2\u6570\uff1a 1 Got integer: 567 std::stringstream \u548c std::cin \u7684\u5de5\u4f5c\u65b9\u5f0f\u7c7b\u4f3c\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d argv[1] \u9996\u5148\u7528\u4e8e\u521d\u59cb\u5316 stringstream \uff0c\u6240\u4ee5\u7a0d\u540e\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 >> \u5c06\u5176\u503c\u63d0\u53d6\u5230\u4e00\u4e2a\u6574\u578b\u53d8\u91cf ( \u7c7b\u4f3c\u4e8e std::cin )\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u7ae0\u8282\u4e2d\u5bf9 std::stringstream \u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002","title":"\u5904\u7406\u6570\u503c\u53c2\u6570"},{"location":"12-5-command-line-arguments/#_5","text":"\u5f53\u4f60\u5728\u547d\u4ee4\u884c(\u6216\u4eceIDE\u8fd0\u884c\u60a8\u7684\u7a0b\u5e8f)\u4e0a\u952e\u5165\u67d0\u4e9b\u5185\u5bb9\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u9700\u8981\u8d1f\u8d23\u89e3\u6790\u6216\u8f6c\u53d1\u76f8\u5e94\u7684\u8bf7\u6c42\u3002\u8fd9\u4e0d\u4ec5\u6d89\u53ca\u5230\u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u8fd8\u6d89\u53ca\u5230\u89e3\u6790\u4efb\u4f55\u53c2\u6570\uff0c\u4ee5\u786e\u5b9a\u5e94\u8be5\u5982\u4f55\u5904\u7406\u5b83\u4eec\u5e76\u5c06\u5b83\u4eec\u4f20\u9012\u7ed9\u5e94\u7528\u7a0b\u5e8f\u3002 \u4e00\u822c\u6765\u8bf4\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5bf9\u4e8e\u5982\u4f55\u5904\u7406\u53cc\u5f15\u53f7\u548c\u53cd\u659c\u6760\u7b49\u7279\u6b8a\u5b57\u7b26\u6709\u7279\u6b8a\u7684\u89c4\u5219\u3002 \u4f8b\u5982\uff1a 1 MyArgs Hello world! \u6253\u5370\uff1a 1 2 3 4 There are 3 arguments: 0 C:\\MyArgs 1 Hello 2 world! \u901a\u5e38\uff0c\u4ee5\u53cc\u5f15\u53f7\u4f20\u9012\u7684\u5b57\u7b26\u4e32\u88ab\u8ba4\u4e3a\u662f\u540c\u4e00\u5b57\u7b26\u4e32\u7684\u4e00\u90e8\u5206: 1 MyArgs \"Hello world!\" \u6253\u5370\uff1a 1 2 3 There are 2 arguments: 0 C:\\MyArgs 1 Hello world! \u5927\u591a\u6570\u64cd\u4f5c\u7cfb\u7edf\u90fd\u5141\u8bb8\u4f60\u901a\u8fc7\u8f6c\u8bd1\u53cc\u5f15\u53f7\u6765\u4f7f\u7528\u5b57\u9762\u53cc\u5f15\u53f7\uff1a 1 MyArgs \\\"Hello world!\\\" \u6253\u5370\uff1a 1 2 3 4 There are 3 arguments: 0 C:\\MyArgs 1 \"Hello 2 world!\" \u5176\u4ed6\u5b57\u7b26\u53ef\u80fd\u4e5f\u9700\u8981\u8f6c\u4e49\uff0c\u8fd9\u53d6\u51b3\u4e8e\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u5982\u4f55\u89e3\u91ca\u5b83\u4eec\u3002","title":"\u64cd\u4f5c\u7cfb\u7edf\u9996\u5148\u89e3\u6790\u547d\u4ee4\u884c"},{"location":"12-5-command-line-arguments/#_6","text":"\u547d\u4ee4\u884c\u53c2\u6570\u4e3a\u7528\u6237\u6216\u5176\u4ed6\u7a0b\u5e8f\u5728\u542f\u52a8\u65f6\u5411\u7a0b\u5e8f\u4f20\u9012\u8f93\u5165\u6570\u636e\u63d0\u4f9b\u4e86\u4e00\u79cd\u5f88\u597d\u7684\u65b9\u5f0f\u3002\u5bf9\u4e8e\u4efb\u4f55\u7a0b\u5e8f\u9700\u8981\u7684\u6570\u636e\uff0c\u8bf7\u8003\u8651\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u5141\u8bb8\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u6765\u4f20\u9012\u8fd9\u4e9b\u8f93\u5165\u6570\u636e\u3002\u5982\u679c\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u8be5\u6570\u636e\u6ca1\u6709\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570 \u4f20\u5165\uff0c\u4f60\u4e5f\u662f\u53ef\u4ee5\u68c0\u6d4b\u5230\u7684\uff0c\u6b64\u65f6\u4f60\u53ef\u4ee5\u518d\u8981\u6c42\u7528\u6237\u8fdb\u884c\u8f93\u5165\u3002\u8fd9\u6837\uff0c\u7a0b\u5e8f\u5c31\u53ef\u4ee5\u4ee5\u4efb\u4f55\u4e00\u79cd\u65b9\u5f0f\u8fd0\u884c\u3002","title":"\u5c0f\u7ed3"},{"location":"12-6-Ellipsis-and-why-to-avoid-them/","tags":["ellipsis"],"text":"Key Takeaway In all of the functions we\u2019ve seen so far, the number of parameters a function will take must be known in advance (even if they have default values). However, there are certain cases where it can be useful to be able to pass a variable number of parameters to a function. C++ provides a special specifier known as ellipsis (aka \u201c\u2026\u201d) that allow us to do precisely this. Because ellipsis are rarely used, potentially dangerous, and we recommend avoiding their use, this section can be considered optional reading. Functions that use ellipsis take the form: return_type function_name(argument_list, ...) The argument_list is one or more normal function parameters. Note that functions that use ellipsis must have at least one non-ellipsis parameter. Any arguments passed to the function must match the argument_list parameters first. The ellipsis (which are represented as three periods in a row) must always be the last parameter in the function. The ellipsis capture any additional arguments (if there are any). Though it is not quite accurate, it is conceptually useful to think of the ellipsis as an array that holds any additional parameters beyond those in the argument_list. \u7701\u7565\u53f7\u5b9e\u4f8b The best way to learn about ellipsis is by example. So let\u2019s write a simple program that uses ellipsis. Let\u2019s say we want to write a function that calculates the average of a bunch of integers. We\u2019d do it like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include #include // needed to use ellipsis // The ellipsis must be the last parameter // count is how many additional arguments we're passing double findAverage ( int count , ...) { int sum { 0 }; // We access the ellipsis through a va_list, so let's declare one std :: va_list list ; // We initialize the va_list using va_start. The first argument is // the list to initialize. The second argument is the last non-ellipsis // parameter. va_start ( list , count ); // Loop through all the ellipsis values for ( int arg { 0 }; arg < count ; ++ arg ) { // We use va_arg to get values out of our ellipsis // The first argument is the va_list we're using // The second argument is the type of the value sum += va_arg ( list , int ); } // Cleanup the va_list when we're done. va_end ( list ); return static_cast < double > ( sum ) / count ; } int main () { std :: cout << findAverage ( 5 , 1 , 2 , 3 , 4 , 5 ) << '\\n' ; std :: cout << findAverage ( 6 , 1 , 2 , 3 , 4 , 5 , 6 ) << '\\n' ; return 0 ; } COPY This code prints: 1 2 3 3.5 As you can see, this function takes a variable number of parameters! Now, let\u2019s take a look at the components that make up this example. First, we have to include the cstdarg header. This header defines va_list, va_arg, va_start, and va_end, which are macros that we need to use to access the parameters that are part of the ellipsis. We then declare our function that uses the ellipsis. Remember that the argument list must be one or more fixed parameters. In this case, we\u2019re passing in a single integer that tells us how many numbers to average. The ellipsis always comes last. Note that the ellipsis parameter has no name! Instead, we access the values in the ellipsis through a special type known as va_list. It is conceptually useful to think of va_list as a pointer that points to the ellipsis array. First, we declare a va_list, which we\u2019ve called \u201clist\u201d for simplicity. The next thing we need to do is make list point to our ellipsis parameters. We do this by calling va_start(). va_start() takes two parameters: the va_list itself, and the name of the last non-ellipsis parameter in the function. Once va_start() has been called, va_list points to the first parameter in the ellipsis. To get the value of the parameter that va_list currently points to, we use va_arg(). va_arg() also takes two parameters: the va_list itself, and the type of the parameter we\u2019re trying to access. Note that va_arg() also moves the va_list to the next parameter in the ellipsis! Finally, to clean up when we are done, we call va_end(), with va_list as the parameter. Note that va_start() can be called again any time we want to reset the va_list to point to the first parameter in the ellipses again. \u4e3a\u4ec0\u4e48\u7701\u7565\u53f7\u4e0d\u5b89\u5168\uff1a\u7c7b\u578b\u68c0\u67e5\u88ab\u8df3\u8fc7\u4e86 Ellipsis offer the programmer a lot of flexibility to implement functions that can take a variable number of parameters. However, this flexibility comes with some downsides. With regular function parameters, the compiler uses type checking to ensure the types of the function arguments match the types of the function parameters (or can be implicitly converted so they match). This helps ensure you don\u2019t pass a function an integer when it was expecting a string, or vice versa. However, note that ellipsis parameters have no type declarations. When using ellipsis, the compiler completely suspends type checking for ellipsis parameters. This means it is possible to send arguments of any type to the ellipsis! However, the downside is that the compiler will no longer be able to warn you if you call the function with ellipsis arguments that do not make sense. When using the ellipsis, it is completely up to the caller to ensure the function is called with ellipsis arguments that the function can handle. Obviously that leaves quite a bit of room for error (especially if the caller wasn\u2019t the one who wrote the function). Let\u2019s look at an example of a mistake that is pretty subtle: 1 std :: cout << findAverage ( 6 , 1.0 , 2 , 3 , 4 , 5 , 6 ) << '\\n' ; COPY Although this may look harmless enough at first glance, note that the second argument (the first ellipsis argument) is a double instead of an integer. This compiles fine, and produces a somewhat surprising result: 1 1.78782e+008 which is a REALLY big number. How did this happen? As you have learned in previous lessons, a computer stores all data as a sequence of bits. A variable\u2019s type tells the computer how to translate that sequence of bits into a meaningful value. However, you just learned that the ellipsis throw away the variable\u2019s type! Consequently, the only way to get a meaningful value back from the ellipsis is to manually tell va_arg() what the expected type of the next parameter is. This is what the second parameter of va_arg() does. If the actual parameter type doesn\u2019t match the expected parameter type, bad things will usually happen. In the above findAverage program, we told va_arg() that our variables are all expected to have a type of int. Consequently, each call to va_arg() will return the next sequence of bits translated as an integer. In this case, the problem is that the double we passed in as the first ellipsis argument is 8 bytes, whereas va_arg(list, int) will only return 4 bytes of data with each call. Consequently, the first call to va_arg will only read the first 4 bytes of the double (producing a garbage result), and the second call to va_arg will read the second 4 bytes of the double (producing another garbage result). Thus, our overall result is garbage. Because type checking is suspended, the compiler won\u2019t even complain if we do something completely ridiculous, like this: 1 2 int value { 7 }; std :: cout << findAverage ( 6 , 1.0 , 2 , \"Hello, world!\" , 'G' , & value , & findAverage ) << '\\n' ; COPY Believe it or not, this actually compiles just fine, and produces the following result on the author\u2019s machine: 1 1.79766e+008 This result epitomizes the phrase, \u201cGarbage in, garbage out\u201d, which is a popular computer science phrase \u201cused primarily to call attention to the fact that computers, unlike humans, will unquestioningly process the most nonsensical of input data and produce nonsensical output\u201d ( Wikipedia ). So, in summary, type checking on the parameters is suspended, and we have to trust the caller to pass in the right type of parameters. If they don\u2019t, the compiler won\u2019t complain -- our program will just produce garbage (or maybe crash). \u4e3a\u4ec0\u4e48\u7701\u7565\u53f7\u4e0d\u5b89\u5168\uff1a\u7701\u7565\u53f7\u4e0d\u77e5\u9053\u4f20\u5165\u53c2\u6570\u7684\u4e2a\u6570 Not only do the ellipsis throw away the type of the parameters, it also throws away the number of parameters in the ellipsis. This means we have to devise our own solution for keeping track of the number of parameters passed into the ellipsis. Typically, this is done in one of three ways. \u65b9\u6cd51\uff1a\u4f20\u5165\u4e00\u4e2a\u957f\u5ea6\u53c2\u6570 Method #1 is to have one of the fixed parameters represent the number of optional parameters passed. This is the solution we use in the findAverage() example above. However, even here we run into trouble. For example, consider the following call: 1 std :: cout << findAverage ( 6 , 1 , 2 , 3 , 4 , 5 ) << '\\n' ; COPY On the author\u2019s machine at the time of writing, this produced the result: 699773 What happened? We told findAverage() we were going to provide 6 additional values, but we only gave it 5. Consequently, the first five values that va_arg() returns were the ones we passed in. The 6th value it returns was a garbage value somewhere in the stack. Consequently, we got a garbage answer. A more insidious case: 1 std :: cout << findAverage ( 6 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ) << '\\n' ; COPY This produces the answer 3.5, which may look correct at first glance, but omits the last number in the average, because we only told it we were going to provide 6 additional values (and then actually provided 7). These kind of mistakes can be very hard to catch. \u65b9\u6cd52\uff1a \u4f7f\u7528\u54e8\u5175\u503c Method #2 is to use a \u54e8\u5175 value. A sentinel is a special value that is used to terminate a loop when it is encountered. For example, with strings, the null terminator is used as a sentinel value to denote the end of the string. With ellipsis, the sentinel is typically passed in as the last parameter. Here\u2019s an example of findAverage() rewritten to use a sentinel value of -1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include #include // needed to use ellipsis // The ellipsis must be the last parameter double findAverage ( int first , ...) { // We have to deal with the first number specially int sum { first }; // We access the ellipsis through a va_list, so let's declare one std :: va_list list ; // We initialize the va_list using va_start. The first argument is // the list to initialize. The second argument is the last non-ellipsis // parameter. va_start ( list , first ); int count { 1 }; // Loop indefinitely while ( true ) { // We use va_arg to get values out of our ellipsis // The first argument is the va_list we're using // The second argument is the type of the value int arg { va_arg ( list , int ) }; // If this parameter is our sentinel value, stop looping if ( arg == -1 ) break ; sum += arg ; ++ count ; } // Cleanup the va_list when we're done. va_end ( list ); return static_cast < double > ( sum ) / count ; } int main () { std :: cout << findAverage ( 1 , 2 , 3 , 4 , 5 , -1 ) << '\\n' ; std :: cout << findAverage ( 1 , 2 , 3 , 4 , 5 , 6 , -1 ) << '\\n' ; return 0 ; } COPY Note that we no longer need to pass an explicit length as the first parameter. Instead, we pass a sentinel value as the last parameter. However, there are a couple of challenges here. First, C++ requires that we pass at least one fixed parameter. In the previous example, this was our count variable. In this example, the first value is actually part of the numbers to be averaged. So instead of treating the first value to be averaged as part of the ellipsis parameters, we explicitly declare it as a normal parameter. We then need special handling for it inside the function (in this case, we set sum to first instead of 0 to start). Second, this requires the user to pass in the sentinel as the last value. If the user forgets to pass in the sentinel value (or passes in the wrong value), the function will loop continuously until it runs into garbage that matches the sentinel (or crashes). Finally, note that we\u2019ve chosen -1 as our sentinel. That\u2019s fine if we only wanted to find the average of positive numbers, but what if we wanted to include negative numbers? Sentinel values only work well if there is a value that falls outside the valid set of values for the problem you are trying to solve. \u65b9\u6cd5 3: \u4f7f\u7528\u89e3\u7801\u7684\u5b57\u7b26\u4e32 Method #3 involves passing a \u201cdecoder string\u201d that tells the program how to interpret the parameters. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #include #include #include // needed to use ellipsis // The ellipsis must be the last parameter double findAverage ( std :: string_view decoder , ...) { double sum { 0 }; // We access the ellipsis through a va_list, so let's declare one std :: va_list list ; // We initialize the va_list using va_start. The first argument is // the list to initialize. The second argument is the last non-ellipsis // parameter. va_start ( list , decoder ); for ( auto codetype : decoder ) { switch ( codetype ) { case 'i' : sum += va_arg ( list , int ); break ; case 'd' : sum += va_arg ( list , double ); break ; } } // Cleanup the va_list when we're done. va_end ( list ); return sum / std :: size ( decoder ); } int main () { std :: cout << findAverage ( \"iiiii\" , 1 , 2 , 3 , 4 , 5 ) << '\\n' ; std :: cout << findAverage ( \"iiiiii\" , 1 , 2 , 3 , 4 , 5 , 6 ) << '\\n' ; std :: cout << findAverage ( \"iiddi\" , 1 , 2 , 3.5 , 4.5 , 5 ) << '\\n' ; return 0 ; } COPY In this example, we pass a string that encodes both the number of optional variables and their types. The cool thing is that this lets us deal with parameters of different types. However, this method has downsides as well: the decoder string can be a bit cryptic, and if the number or types of the optional parameters don\u2019t match the decoder string precisely, bad things can happen. For those of you coming from C, this is what printf does! \u5b89\u5168\u4f7f\u7528\u7701\u7565\u53f7\u7684\u5efa\u8bae First, if possible, do not use ellipsis at all! Oftentimes, other reasonable solutions are available, even if they require slightly more work. For example, in our findAverage() program, we could have passed in a dynamically sized array of integers instead. This would have provided both strong type checking (to make sure the caller doesn\u2019t try to do something nonsensical) while preserving the ability to pass a variable number of integers to be averaged. Second, if you do use ellipsis, do not mix expected argument types within your ellipsis if possible. Doing so vastly increases the possibility of the caller inadvertently passing in data of the wrong type and va_arg() producing a garbage result. Third, using a count parameter or decoder string as part of the argument list is generally safer than using a sentinel as an ellipsis parameter. This forces the user to pick an appropriate value for the count/decoder parameter, which ensures the ellipsis loop will terminate after a reasonable number of iterations even if it produces a garbage value. \u6269\u5c55\u9605\u8bfb To improve upon ellipses-like functionality, C++11 introduced parameter packs and variadic templates , which offers functionality similar to ellipses, but with strong type checking. However, significant usability challenges impeded adoption of this feature. In C++17, fold expressions were added, which significantly improves the usability of parameter packs, to the point where they are now a viable option. We hope to introduce lessons on these topics in a future site update.","title":"12.6 - \u7701\u7565\u53f7\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83"},{"location":"12-6-Ellipsis-and-why-to-avoid-them/#_1","text":"The best way to learn about ellipsis is by example. So let\u2019s write a simple program that uses ellipsis. Let\u2019s say we want to write a function that calculates the average of a bunch of integers. We\u2019d do it like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include #include // needed to use ellipsis // The ellipsis must be the last parameter // count is how many additional arguments we're passing double findAverage ( int count , ...) { int sum { 0 }; // We access the ellipsis through a va_list, so let's declare one std :: va_list list ; // We initialize the va_list using va_start. The first argument is // the list to initialize. The second argument is the last non-ellipsis // parameter. va_start ( list , count ); // Loop through all the ellipsis values for ( int arg { 0 }; arg < count ; ++ arg ) { // We use va_arg to get values out of our ellipsis // The first argument is the va_list we're using // The second argument is the type of the value sum += va_arg ( list , int ); } // Cleanup the va_list when we're done. va_end ( list ); return static_cast < double > ( sum ) / count ; } int main () { std :: cout << findAverage ( 5 , 1 , 2 , 3 , 4 , 5 ) << '\\n' ; std :: cout << findAverage ( 6 , 1 , 2 , 3 , 4 , 5 , 6 ) << '\\n' ; return 0 ; } COPY This code prints: 1 2 3 3.5 As you can see, this function takes a variable number of parameters! Now, let\u2019s take a look at the components that make up this example. First, we have to include the cstdarg header. This header defines va_list, va_arg, va_start, and va_end, which are macros that we need to use to access the parameters that are part of the ellipsis. We then declare our function that uses the ellipsis. Remember that the argument list must be one or more fixed parameters. In this case, we\u2019re passing in a single integer that tells us how many numbers to average. The ellipsis always comes last. Note that the ellipsis parameter has no name! Instead, we access the values in the ellipsis through a special type known as va_list. It is conceptually useful to think of va_list as a pointer that points to the ellipsis array. First, we declare a va_list, which we\u2019ve called \u201clist\u201d for simplicity. The next thing we need to do is make list point to our ellipsis parameters. We do this by calling va_start(). va_start() takes two parameters: the va_list itself, and the name of the last non-ellipsis parameter in the function. Once va_start() has been called, va_list points to the first parameter in the ellipsis. To get the value of the parameter that va_list currently points to, we use va_arg(). va_arg() also takes two parameters: the va_list itself, and the type of the parameter we\u2019re trying to access. Note that va_arg() also moves the va_list to the next parameter in the ellipsis! Finally, to clean up when we are done, we call va_end(), with va_list as the parameter. Note that va_start() can be called again any time we want to reset the va_list to point to the first parameter in the ellipses again.","title":"\u7701\u7565\u53f7\u5b9e\u4f8b"},{"location":"12-6-Ellipsis-and-why-to-avoid-them/#_2","text":"Ellipsis offer the programmer a lot of flexibility to implement functions that can take a variable number of parameters. However, this flexibility comes with some downsides. With regular function parameters, the compiler uses type checking to ensure the types of the function arguments match the types of the function parameters (or can be implicitly converted so they match). This helps ensure you don\u2019t pass a function an integer when it was expecting a string, or vice versa. However, note that ellipsis parameters have no type declarations. When using ellipsis, the compiler completely suspends type checking for ellipsis parameters. This means it is possible to send arguments of any type to the ellipsis! However, the downside is that the compiler will no longer be able to warn you if you call the function with ellipsis arguments that do not make sense. When using the ellipsis, it is completely up to the caller to ensure the function is called with ellipsis arguments that the function can handle. Obviously that leaves quite a bit of room for error (especially if the caller wasn\u2019t the one who wrote the function). Let\u2019s look at an example of a mistake that is pretty subtle: 1 std :: cout << findAverage ( 6 , 1.0 , 2 , 3 , 4 , 5 , 6 ) << '\\n' ; COPY Although this may look harmless enough at first glance, note that the second argument (the first ellipsis argument) is a double instead of an integer. This compiles fine, and produces a somewhat surprising result: 1 1.78782e+008 which is a REALLY big number. How did this happen? As you have learned in previous lessons, a computer stores all data as a sequence of bits. A variable\u2019s type tells the computer how to translate that sequence of bits into a meaningful value. However, you just learned that the ellipsis throw away the variable\u2019s type! Consequently, the only way to get a meaningful value back from the ellipsis is to manually tell va_arg() what the expected type of the next parameter is. This is what the second parameter of va_arg() does. If the actual parameter type doesn\u2019t match the expected parameter type, bad things will usually happen. In the above findAverage program, we told va_arg() that our variables are all expected to have a type of int. Consequently, each call to va_arg() will return the next sequence of bits translated as an integer. In this case, the problem is that the double we passed in as the first ellipsis argument is 8 bytes, whereas va_arg(list, int) will only return 4 bytes of data with each call. Consequently, the first call to va_arg will only read the first 4 bytes of the double (producing a garbage result), and the second call to va_arg will read the second 4 bytes of the double (producing another garbage result). Thus, our overall result is garbage. Because type checking is suspended, the compiler won\u2019t even complain if we do something completely ridiculous, like this: 1 2 int value { 7 }; std :: cout << findAverage ( 6 , 1.0 , 2 , \"Hello, world!\" , 'G' , & value , & findAverage ) << '\\n' ; COPY Believe it or not, this actually compiles just fine, and produces the following result on the author\u2019s machine: 1 1.79766e+008 This result epitomizes the phrase, \u201cGarbage in, garbage out\u201d, which is a popular computer science phrase \u201cused primarily to call attention to the fact that computers, unlike humans, will unquestioningly process the most nonsensical of input data and produce nonsensical output\u201d ( Wikipedia ). So, in summary, type checking on the parameters is suspended, and we have to trust the caller to pass in the right type of parameters. If they don\u2019t, the compiler won\u2019t complain -- our program will just produce garbage (or maybe crash).","title":"\u4e3a\u4ec0\u4e48\u7701\u7565\u53f7\u4e0d\u5b89\u5168\uff1a\u7c7b\u578b\u68c0\u67e5\u88ab\u8df3\u8fc7\u4e86"},{"location":"12-6-Ellipsis-and-why-to-avoid-them/#_3","text":"Not only do the ellipsis throw away the type of the parameters, it also throws away the number of parameters in the ellipsis. This means we have to devise our own solution for keeping track of the number of parameters passed into the ellipsis. Typically, this is done in one of three ways.","title":"\u4e3a\u4ec0\u4e48\u7701\u7565\u53f7\u4e0d\u5b89\u5168\uff1a\u7701\u7565\u53f7\u4e0d\u77e5\u9053\u4f20\u5165\u53c2\u6570\u7684\u4e2a\u6570"},{"location":"12-6-Ellipsis-and-why-to-avoid-them/#1","text":"Method #1 is to have one of the fixed parameters represent the number of optional parameters passed. This is the solution we use in the findAverage() example above. However, even here we run into trouble. For example, consider the following call: 1 std :: cout << findAverage ( 6 , 1 , 2 , 3 , 4 , 5 ) << '\\n' ; COPY On the author\u2019s machine at the time of writing, this produced the result: 699773 What happened? We told findAverage() we were going to provide 6 additional values, but we only gave it 5. Consequently, the first five values that va_arg() returns were the ones we passed in. The 6th value it returns was a garbage value somewhere in the stack. Consequently, we got a garbage answer. A more insidious case: 1 std :: cout << findAverage ( 6 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ) << '\\n' ; COPY This produces the answer 3.5, which may look correct at first glance, but omits the last number in the average, because we only told it we were going to provide 6 additional values (and then actually provided 7). These kind of mistakes can be very hard to catch.","title":"\u65b9\u6cd51\uff1a\u4f20\u5165\u4e00\u4e2a\u957f\u5ea6\u53c2\u6570"},{"location":"12-6-Ellipsis-and-why-to-avoid-them/#2","text":"Method #2 is to use a \u54e8\u5175 value. A sentinel is a special value that is used to terminate a loop when it is encountered. For example, with strings, the null terminator is used as a sentinel value to denote the end of the string. With ellipsis, the sentinel is typically passed in as the last parameter. Here\u2019s an example of findAverage() rewritten to use a sentinel value of -1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include #include // needed to use ellipsis // The ellipsis must be the last parameter double findAverage ( int first , ...) { // We have to deal with the first number specially int sum { first }; // We access the ellipsis through a va_list, so let's declare one std :: va_list list ; // We initialize the va_list using va_start. The first argument is // the list to initialize. The second argument is the last non-ellipsis // parameter. va_start ( list , first ); int count { 1 }; // Loop indefinitely while ( true ) { // We use va_arg to get values out of our ellipsis // The first argument is the va_list we're using // The second argument is the type of the value int arg { va_arg ( list , int ) }; // If this parameter is our sentinel value, stop looping if ( arg == -1 ) break ; sum += arg ; ++ count ; } // Cleanup the va_list when we're done. va_end ( list ); return static_cast < double > ( sum ) / count ; } int main () { std :: cout << findAverage ( 1 , 2 , 3 , 4 , 5 , -1 ) << '\\n' ; std :: cout << findAverage ( 1 , 2 , 3 , 4 , 5 , 6 , -1 ) << '\\n' ; return 0 ; } COPY Note that we no longer need to pass an explicit length as the first parameter. Instead, we pass a sentinel value as the last parameter. However, there are a couple of challenges here. First, C++ requires that we pass at least one fixed parameter. In the previous example, this was our count variable. In this example, the first value is actually part of the numbers to be averaged. So instead of treating the first value to be averaged as part of the ellipsis parameters, we explicitly declare it as a normal parameter. We then need special handling for it inside the function (in this case, we set sum to first instead of 0 to start). Second, this requires the user to pass in the sentinel as the last value. If the user forgets to pass in the sentinel value (or passes in the wrong value), the function will loop continuously until it runs into garbage that matches the sentinel (or crashes). Finally, note that we\u2019ve chosen -1 as our sentinel. That\u2019s fine if we only wanted to find the average of positive numbers, but what if we wanted to include negative numbers? Sentinel values only work well if there is a value that falls outside the valid set of values for the problem you are trying to solve.","title":"\u65b9\u6cd52\uff1a \u4f7f\u7528\u54e8\u5175\u503c"},{"location":"12-6-Ellipsis-and-why-to-avoid-them/#3","text":"Method #3 involves passing a \u201cdecoder string\u201d that tells the program how to interpret the parameters. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #include #include #include // needed to use ellipsis // The ellipsis must be the last parameter double findAverage ( std :: string_view decoder , ...) { double sum { 0 }; // We access the ellipsis through a va_list, so let's declare one std :: va_list list ; // We initialize the va_list using va_start. The first argument is // the list to initialize. The second argument is the last non-ellipsis // parameter. va_start ( list , decoder ); for ( auto codetype : decoder ) { switch ( codetype ) { case 'i' : sum += va_arg ( list , int ); break ; case 'd' : sum += va_arg ( list , double ); break ; } } // Cleanup the va_list when we're done. va_end ( list ); return sum / std :: size ( decoder ); } int main () { std :: cout << findAverage ( \"iiiii\" , 1 , 2 , 3 , 4 , 5 ) << '\\n' ; std :: cout << findAverage ( \"iiiiii\" , 1 , 2 , 3 , 4 , 5 , 6 ) << '\\n' ; std :: cout << findAverage ( \"iiddi\" , 1 , 2 , 3.5 , 4.5 , 5 ) << '\\n' ; return 0 ; } COPY In this example, we pass a string that encodes both the number of optional variables and their types. The cool thing is that this lets us deal with parameters of different types. However, this method has downsides as well: the decoder string can be a bit cryptic, and if the number or types of the optional parameters don\u2019t match the decoder string precisely, bad things can happen. For those of you coming from C, this is what printf does!","title":"\u65b9\u6cd5 3: \u4f7f\u7528\u89e3\u7801\u7684\u5b57\u7b26\u4e32"},{"location":"12-6-Ellipsis-and-why-to-avoid-them/#_4","text":"First, if possible, do not use ellipsis at all! Oftentimes, other reasonable solutions are available, even if they require slightly more work. For example, in our findAverage() program, we could have passed in a dynamically sized array of integers instead. This would have provided both strong type checking (to make sure the caller doesn\u2019t try to do something nonsensical) while preserving the ability to pass a variable number of integers to be averaged. Second, if you do use ellipsis, do not mix expected argument types within your ellipsis if possible. Doing so vastly increases the possibility of the caller inadvertently passing in data of the wrong type and va_arg() producing a garbage result. Third, using a count parameter or decoder string as part of the argument list is generally safer than using a sentinel as an ellipsis parameter. This forces the user to pick an appropriate value for the count/decoder parameter, which ensures the ellipsis loop will terminate after a reasonable number of iterations even if it produces a garbage value. \u6269\u5c55\u9605\u8bfb To improve upon ellipses-like functionality, C++11 introduced parameter packs and variadic templates , which offers functionality similar to ellipses, but with strong type checking. However, significant usability challenges impeded adoption of this feature. In C++17, fold expressions were added, which significantly improves the usability of parameter packs, to the point where they are now a viable option. We hope to introduce lessons on these topics in a future site update.","title":"\u5b89\u5168\u4f7f\u7528\u7701\u7565\u53f7\u7684\u5efa\u8bae"},{"location":"12-7-introduction-to-lambdas-anonymous-functions/","tags":["lambda","C++14","C++20"],"text":"Key Takeaway \u8bf7\u8003\u8651\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff0c\u6211\u4eec\u5728 11.19 \u2014 \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb \u4e2d\u4ecb\u7ecd\u8fc7\u5b83\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include #include #include // Our function will return true if the element matches bool containsNut ( std :: string_view str ) { // std::string_view::find returns std::string_view::npos if it doesn't find // the substring. Otherwise it returns the index where the substring occurs // in str. return ( str . find ( \"nut\" ) != std :: string_view :: npos ); } int main () { std :: array < std :: string_view , 4 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" }; // Scan our array to see if any elements contain the \"nut\" substring auto found { std :: find_if ( arr . begin (), arr . end (), containsNut ) }; if ( found == arr . end ()) { std :: cout << \"No nuts \\n \" ; } else { std :: cout << \"Found \" << * found << '\\n' ; } return 0 ; } \u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\u641c\u7d22\u5b57\u7b26\u4e32\uff0c\u627e\u5230\u7b2c\u4e00\u4e2a\u5305\u542b \u201cnut\u201d \u5b50\u4e32\u7684\u5143\u7d20\u3002\u56e0\u6b64\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 Found walnut \u867d\u7136\u4ee3\u7801\u53ef\u4ee5\u6b63\u786e\u6267\u884c\uff0c\u4f46\u662f\u8fd8\u6709\u53ef\u6539\u8fdb\u7684\u5730\u65b9\u3002 \u8fd9\u91cc\u95ee\u9898\u7684\u6838\u5fc3\u5728\u4e8e std::find_if \u8981\u6c42\u4f7f\u7528\u8005\u4f20\u9012\u7ed9\u5b83\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5fc5\u987b\u5b9a\u4e49\u4e00\u4e2a\u53ea\u4f7f\u7528\u4e00\u6b21\u7684\u51fd\u6570\uff0c\u8fd8\u5fc5\u987b\u7ed9\u5b83\u8d77\u4e2a\u540d\uff0c\u5e76\u5c06\u5176\u653e\u7f6e\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d(\u56e0\u4e3a\u51fd\u6570\u4e0d\u80fd\u5d4c\u5957!)\u3002\u8fd9\u4e2a\u51fd\u6570\u5f88\u77ed\uff0c\u4ece\u4e00\u884c\u4ee3\u7801\u4e2d\u51e0\u4e4e\u6bd4\u4ece\u540d\u79f0\u548c\u6ce8\u91ca\u4e2d\u66f4\u5bb9\u6613\u8fa8\u522b\u5b83\u7684\u529f\u80fd\u3002 lambda \u4f7f\u7528 lambda \u8868\u8fbe\u5f0f(\u6709\u65f6\u5019\u4e5f\u79f0lambda\u6216 \u95ed\u5305 \u6216\u533f\u540d\u51fd\u6570) \u53ef\u4ee5\u5728\u5176\u4ed6\u51fd\u6570\u4e2d\u5b9a\u4e49\u533f\u540d\u51fd\u6570\u3002\u80fd\u591f\u5d4c\u5957\u5b9a\u4e49\u662f\u5f88\u91cd\u8981\u7684\u7279\u6027\uff0c\u5b83\u53ef\u4ee5\u907f\u514d \u547d\u540d\u7a7a\u95f4 \u4e2d\u7684\u540d\u79f0\u6c61\u67d3\uff0c\u800c\u4e14\u53ef\u4ee5\u5c06\u51fd\u6570\u7684\u5b9a\u4e49\u5c3d\u53ef\u80fd\u9760\u8fd1\u4f7f\u7528\u5b83\u7684\u5730\u65b9\uff08\u907f\u514d\u989d\u5916\u7684\u4e0a\u4e0b\u6587\uff09\u3002 lambda \u7684\u8bed\u6cd5\u662fC++\u4e2d\u6700\u5947\u602a\u7684\u4e1c\u897f\u4e4b\u4e00\uff0c\u9700\u8981\u4e00\u70b9\u65f6\u95f4\u6765\u9002\u5e94\u3002\u533f\u540d\u51fd\u6570\u5f62\u5f0f\u662f: 1 2 3 4 [ captureClause ] ( parameters ) -> returnType { statements ; } \u95ed\u5305\u8bed\u53e5\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u5982\u679c\u4e0d\u9700\u8981\u6355\u83b7\u53d8\u91cf\u7684\u8bdd\uff1b \u5f62\u53c2 \u5217\u8868\u4e5f\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u5982\u679c\u4e0d\u9700\u8981\u5f62\u53c2\u7684\u8bdd\uff1b \u8fd4\u56de\u7c7b\u578b\u662f\u53ef\u9009\u7684\uff0c\u5982\u679c\u7701\u7565\u7684\u8bdd\uff0c\u4f1a\u5047\u5b9a\u4e3a auto (\u4f7f\u7528 \u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad )\u3002\u5c3d\u7ba1\u6211\u4eec\u4e4b\u524d\u8bf4\u8fc7\uff0c \u5e94\u8be5\u907f\u514d\u4f7f\u7528\u51fd\u6570\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u63a8\u65ad \uff0c\u4f46\u662f\u5728\u533f\u540d\u8868\u8fbe\u5f0f\u4e2d\u662f\u53ef\u4ee5\u7528\u7684\uff08\u56e0\u4e3a\u8fd9\u4e9b\u51fd\u6570\u901a\u5e38\u90fd\u975e\u5e38\u7b80\u5355\uff09\u3002 \u56e0\u4e3alambda \u6ca1\u6709\u51fd\u6570\u540d\uff0c\u6240\u4ee5\u4e0d\u5fc5\u4e3a\u5176\u8d77\u540d\u3002 \u9898\u5916\u8bdd \u4e00\u4e2a\u57fa\u672c\u7684\u533f\u540d\u51fd\u6570\u5b9a\u4e49\u770b\u4e0a\u53bb\u50cf\u662f\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 #include int main () { [] {}; // \u7701\u7565\u8fd4\u56de\u503c\u7c7b\u578b, \u65e0\u95ed\u5305\uff0c\u65e0\u53c2\u6570 return 0 ; } \u8ba9\u6211\u4eec\u7528\u533f\u540d\u51fd\u6570\u91cd\u5199\u4e0a\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include #include #include int main () { constexpr std :: array < std :: string_view , 4 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" }; // Define the function right where we use it. const auto found { std :: find_if ( arr . begin (), arr . end (), []( std :: string_view str ) // \u533f\u540d\u51fd\u6570\uff0c\u65e0\u95ed\u5305 { return ( str . find ( \"nut\" ) != std :: string_view :: npos ); }) }; if ( found == arr . end ()) { std :: cout << \"No nuts \\n \" ; } else { std :: cout << \"Found \" << * found << '\\n' ; } return 0 ; } \u548c\u4f7f\u7528\u51fd\u6570\u6307\u9488\u7684\u60c5\u51b5\u4e00\u6837\uff0c\u4ea7\u751f\u76f8\u540c\u7684\u7ed3\u679c: 1 Found walnut \u53ef\u4ee5\u770b\u5230\uff0clambda \u548c\u4e4b\u524d\u7684 containsNut \u51fd\u6570\u975e\u5e38\u7c7b\u578b\u3002\u5b83\u4eec\u7684\u53c2\u6570\u548c\u51fd\u6570\u4f53\u662f\u5b8c\u5168\u4e00\u6837\u7684\u3002\u8fd9\u4e2alambda\u6ca1\u6709\u95ed\u5305\uff08\u4e0b\u8282\u8bfe\u4ecb\u7ecd\uff09\uff0c\u56e0\u4e3a\u6ca1\u5fc5\u8981\u3002\u4e0d\u4ec5\u5982\u6b64\u6211\u4eec\u8fd8\u7701\u7565\u4e86\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u4f46\u662f\u7531\u4e8e operator!= \u8fd4\u56de bool \uff0c\u6240\u4ee5\u8be5lambda\u4e5f\u8fd4\u56de\u5e03\u5c14\u7c7b\u578b\u3002 lambda \u51fd\u6570\u7684\u7c7b\u578b \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728\u9700\u8981\u7684\u5730\u65b9\u5b9a\u4e49\u4e86\u533f\u540d\u51fd\u6570\uff0c\u5b83\u7684\u8fd9\u79cd\u7528\u6cd5\u6709\u65f6\u88ab\u79f0\u4e3a \u51fd\u6570\u5b57\u9762\u91cf \u3002 \u7136\u800c\uff0c\u5728\u540c\u4e00\u884c\u4e2d\u7f16\u5199lambda\u6709\u65f6\u4f1a\u964d\u4f4e\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u5c31\u50cf\u6211\u4eec\u53ef\u4ee5\u7528\u5b57\u9762\u503c(\u6216\u51fd\u6570\u6307\u9488)\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cf\u4ee5\u4f9b\u4ee5\u540e\u4f7f\u7528\u4e00\u6837\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u7528lambda\u5b9a\u4e49\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cf\uff0c\u7136\u540e\u5728\u4ee5\u540e\u4f7f\u7528\u5b83\u3002\u4e00\u4e2a\u6709\u597d\u540d\u5b57\u7684lambda\u51fd\u6570\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u5bb9\u6613\u9605\u8bfb\u3002 \u4f8b\u5982\uff0c\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 std::all_of \u6765\u68c0\u67e5\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u662f\u5426\u4e3a\u5076\u6570\uff1a 1 2 // Bad: We have to read the lambda to understand what's happening. return std :: all_of ( array . begin (), array . end (), []( int i ){ return (( i % 2 ) == 0 ); }); \u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u65b9\u5f0f\u63d0\u5347\u53ef\u8bfb\u6027\u3002 1 2 3 4 5 6 7 8 9 // Good: Instead, we can store the lambda in a named variable and pass it to the function. auto isEven { []( int i ) { return (( i % 2 ) == 0 ); } }; return std :: all_of ( array . begin (), array . end (), isEven ); \u6ce8\u610f\uff0c\u73b0\u5728\u6700\u540e\u4e00\u884c\u4ee3\u7801\u53ef\u4ee5\u5f88\u81ea\u7136\u5730\u88ab\u7406\u89e3\u4e3a\uff1a \u5224\u65ad\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\u662f\u5426\u90fd\u662f\u5076\u6570 \u4e0d\u8fc7\uff0c isEven \u7684\u7c7b\u578b\u662f\u4ec0\u4e48\u5462\uff1f \u4e8b\u5b9e\u8bc1\u660e\uff0clambdas\u6ca1\u6709\u53ef\u4ee5\u663e\u5f0f\u4f7f\u7528\u7684\u7c7b\u578b\u3002\u5f53\u6211\u4eec\u7f16\u5199lambda\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u4e3a\u5176\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684\u7c7b\u578b\uff0c\u4f46\u5bf9\u6211\u4eec\u5e76\u4e0d\u53ef\u89c1\u3002 \u6269\u5c55\u9605\u8bfb \u5b9e\u9645\u4e0a\uff0clambda\u5e76\u4e0d\u662f\u51fd\u6570(\u8fd9\u662f\u5b83\u4eec\u53ef\u4ee5\u907f\u514dC++\u4e0d\u652f\u6301\u5d4c\u5957\u51fd\u6570\u7684\u9650\u5236\u7684\u90e8\u5206\u539f\u56e0)\u3002\u5b83\u4eec\u662f\u4e00\u79cd\u53eb\u505afunctor\u7684\u7279\u6b8a\u5bf9\u8c61\u3002\u51fd\u5b50\u662f\u5305\u542b\u91cd\u8f7d\u7684 operator() \u7684\u5bf9\u8c61\uff0c\u4f7f\u5b83\u4eec\u50cf\u51fd\u6570\u4e00\u6837\u53ef\u8c03\u7528\u3002 \u5c3d\u7ba1\u6211\u4eec\u5e76\u4e0d\u77e5\u9053lambda\u7684\u7c7b\u578b\uff0c\u4f46\u662f\u6211\u4eec\u4ecd\u7136\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5176\u5b58\u653e\u5230\u67d0\u4e2a\u53d8\u91cf\u4e2d\u3002\u5982\u679c\u4e00\u4e2alambda\u6ca1\u6709\u95ed\u5305\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u666e\u901a\u7684\u51fd\u6570\u6307\u9488\u5b58\u653e\u5b83\u3002\u4f7f\u7528 std::function \u6216\u8005\u901a\u8fc7 auto \u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\u4e5f\u662f\u53ef\u4ee5\u7684(\u5373\u4fbf\u6b64\u65f6lambda\u6709\u95ed\u5305\u8bed\u53e5)\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include int main () { // A regular function pointer. Only works with an empty capture clause (empty []). double ( * addNumbers1 )( double , double ){ []( double a , double b ) { return ( a + b ); } }; addNumbers1 ( 1 , 2 ); // Using std::function. The lambda could have a non-empty capture clause (discussed next lesson). std :: function addNumbers2 { // note: pre-C++17, use std::function instead []( double a , double b ) { return ( a + b ); } }; addNumbers2 ( 3 , 4 ); // Using auto. Stores the lambda with its real type. auto addNumbers3 { []( double a , double b ) { return ( a + b ); } }; addNumbers3 ( 5 , 6 ); return 0 ; } \u4f7f\u7528lambda\u7684\u5b9e\u9645\u7c7b\u578b\u7684\u552f\u4e00\u65b9\u6cd5\u662f\u901a\u8fc7 auto \u3002\u4e0e std::function \u76f8\u6bd4\uff0c auto \u8fd8\u6709\u4e00\u4e2a\u597d\u5904\uff0c\u90a3\u5c31\u662f\u6ca1\u6709\u5f00\u9500\u3002 \u4e0d\u5e78\u7684\u662f\uff0c\u5728C++ 20\u4e4b\u524d\uff0c\u6211\u4eec\u4e0d\u80fd\u603b\u662f\u4f7f\u7528 auto \u3002\u5728\u5b9e\u9645lambda\u672a\u77e5\u7684\u60c5\u51b5\u4e0b(\u4f8b\u5982\uff0c\u56e0\u4e3a\u6211\u4eec\u5c06lambda\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u7531\u8c03\u7528\u8005\u51b3\u5b9a\u5c06\u4f20\u5165\u4ec0\u4e48lambda)\uff0c\u6211\u4eec\u4e0d\u80fd\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f7f\u7528 auto \u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528 std::function \u6765\u4ee3\u66ff\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include // We don't know what fn will be. std::function works with regular functions and lambdas. void repeat ( int repetitions , const std :: function < void ( int ) >& fn ) { for ( int i { 0 }; i < repetitions ; ++ i ) { fn ( i ); } } int main () { repeat ( 3 , []( int i ) { std :: cout << i << '\\n' ; }); return 0 ; } \u8f93\u51fa 1 2 3 0 1 2 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u6211\u4eec\u4e0d\u80fd\u4f7f\u7528 auto \u5173\u952e\u5b57\uff0c\u8fd9\u4e48\u505a\u7684\u8bdd\u51fd\u6570\u7684\u8c03\u7528\u8005\u5c31\u65e0\u6cd5\u77e5\u9053 fn \u5e94\u8be5\u6709\u4ec0\u4e48\u6837\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u3002\u8fd9\u4e2a\u9650\u5236\u5728C++20\u5f15\u5165 \u7f29\u5199\u51fd\u6570\u6a21\u677f \u4e4b\u540e\u5c31\u4e0d\u5b58\u5728\u4e86\u3002 \u6b64\u5916\uff0c\u56e0\u4e3a\u5b83\u4eec\u5b9e\u9645\u4e0a\u662f\u6a21\u677f\uff0c\u5177\u6709 auto \u53c2\u6570\u7684\u51fd\u6570\u4e0d\u80fd\u88ab\u5206\u79bb\u5230\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u4e2d\u3002 \u6cd5\u5219 \u9700\u8981\u7528 lambda \u521d\u59cb\u5316\u53d8\u91cf\u65f6\uff0c\u4f7f\u7528 auto \u5173\u952e\u5b57\u3002\u7f6a\u57df\u4e0d\u80fd\u7528 lambda \u521d\u59cb\u5316\u53d8\u91cf\u7684\u573a\u5408\uff0c\u4f7f\u7528 std::function \u3002 \u6cdb\u578blambda\u51fd\u6570 \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0clambda\u5f62\u53c2\u7684\u5de5\u4f5c\u89c4\u5219\u4e0e\u5e38\u89c4\u51fd\u6570\u5f62\u53c2\u76f8\u540c\u3002 \u4e00\u4e2a\u503c\u5f97\u6ce8\u610f\u7684\u4f8b\u5916\u662f\uff0c\u4eceC++ 14\u5f00\u59cb\uff0c\u6211\u4eec\u5141\u8bb8\u4f7f\u7528 auto \u4f5c\u4e3a\u5f62\u53c2\u7c7b\u578b(\u6ce8\u610f:\u5728c++ 20\u4e2d\uff0c\u5e38\u89c4\u51fd\u6570\u4e5f\u53ef\u4ee5\u4f7f\u7528 auto \u4f5c\u4e3a\u5f62\u53c2\u7c7b\u578b)\u3002\u5f53lambda\u6709\u4e00\u4e2a\u6216\u591a\u4e2a auto \u5f62\u53c2\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u4ece\u5bf9lambda\u7684\u8c03\u7528\u4e2d\u63a8\u65ad\u9700\u8981\u4ec0\u4e48\u5f62\u53c2\u7c7b\u578b\u3002 \u56e0\u4e3a\u5e26\u6709\u4e00\u4e2a\u6216\u591a\u4e2a auto \u53c2\u6570\u7684 lambda\u53ef\u4ee5\u6f5c\u5728\u5730\u4e0e\u5404\u79cd\u7c7b\u578b\u4e00\u8d77\u5de5\u4f5c\uff0c\u6240\u4ee5\u5b83\u4eec\u88ab\u79f0\u4e3a\u6cdb\u578blambda\u3002 \u6269\u5c55\u9605\u8bfb \u5f53\u5728lambda\u7684\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\u65f6\uff0c auto \u53ea\u662f\u6a21\u677f\u5f62\u53c2\u7684\u7f29\u5199\u3002 \u770b\u4e00\u4e2a\u6cdb\u578blambda\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include #include #include int main () { constexpr std :: array months { // pre-C++17 use std::array \"January\" , \"February\" , \"March\" , \"April\" , \"May\" , \"June\" , \"July\" , \"August\" , \"September\" , \"October\" , \"November\" , \"December\" }; // Search for two consecutive months that start with the same letter. const auto sameLetter { std :: adjacent_find ( months . begin (), months . end (), []( const auto & a , const auto & b ) { return ( a [ 0 ] == b [ 0 ]); }) }; // Make sure that two months were found. if ( sameLetter != months . end ()) { // std::next returns the next iterator after sameLetter std :: cout << * sameLetter << \" and \" << * std :: next ( sameLetter ) << \" start with the same letter \\n \" ; } return 0 ; } \u8f93\u51fa\uff1a 1 June and July start with the same letter \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 auto \u5f62\u53c2\u6765\u83b7\u53d6\u5b57\u7b26\u4e32\u7684 const \u5f15\u7528\u3002\u7531\u4e8e\u6240\u6709\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\u90fd\u53ef\u4ee5\u901a\u8fc7 operator[] \u8bbf\u95ee\u5355\u4e2a\u5b57\u7b26\u3002\u6240\u4ee5\u6211\u4eec\u65e0\u9700\u5173\u5fc3\u4f20\u5165\u5b57\u7b26\u4e32\u662f std::string \u3001C\u98ce\u683c\u5b57\u7b26\u4e32\u8fd8\u662f\u5176\u4ed6\u5b57\u7b26\u4e32\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u7f16\u5199\u4ecb\u7ecd\u8fd9\u4e9b\u7c7b\u578b\u7684lambda\u8868\u8fbe\u5f0f\uff0c\u5982\u679c\u5c06\u6765\u9700\u8981\u4fee\u6539 months \u7684\u7c7b\u578b\uff0c\u4e5f\u4e0d\u5fc5\u4fee\u6539lambda\u3002 \u4e0d\u8fc7\uff0c\u4f7f\u7528 auto \u5e76\u4e0d\u603b\u662f\u6700\u4f73\u7684\u9009\u62e9\uff0c\u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include #include #include int main () { constexpr std :: array months { // pre-C++17 use std::array \"January\" , \"February\" , \"March\" , \"April\" , \"May\" , \"June\" , \"July\" , \"August\" , \"September\" , \"October\" , \"November\" , \"December\" }; // Count how many months consist of 5 letters const auto fiveLetterMonths { std :: count_if ( months . begin (), months . end (), []( std :: string_view str ) { return ( str . length () == 5 ); }) }; std :: cout << \"There are \" << fiveLetterMonths << \" months with 5 letters \\n \" ; return 0 ; } \u8f93\u51fa\uff1a 1 There are 2 months with 5 letters \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u4f7f\u7528 auto \uff0c\u5219\u4f1a\u63a8\u65ad\u51fa const char* \u3002\u7531\u4e8e C \u98ce\u683c\u5b57\u7b26\u4e32\u5e76\u4e0d\u6613\u7528\uff08\u9664\u4e86 operator[] \uff09\u3002\u6240\u4ee5\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u6211\u4eec\u663e\u5f0f\u5730\u5b9a\u4e49\u53c2\u6570\u4e3a std::string_view \u7c7b\u578b\uff0c\u8fd9\u4f7f\u5f97\u6211\u4eec\u5728\u5904\u7406\u6570\u636e\u65f6\u53ef\u4ee5\u66f4\u5bb9\u6613\u3002(\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u67e5\u8be2string view \u7684\u957f\u5ea6\uff0c\u5373\u4fbf\u4f20\u5165\u7684\u662f\u4e00\u4e2aC\u98ce\u683c\u5b57\u7b26\u4e32\u6570\u7ec4)\u3002 \u6cdb\u578blambda\u548c\u9759\u6001\u53d8\u91cf \u9700\u8981\u6ce8\u610f\u7684\u4e00\u4ef6\u4e8b\u662f\uff0c\u7f16\u8bd1\u5668\u5c06\u4e3a auto \u89e3\u6790\u51fa\u7684\u6bcf\u4e2a\u4e0d\u540c\u7c7b\u578b\u751f\u6210\u552f\u4e00\u7684lambda\u8868\u8fbe\u5f0f\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u4e00\u4e2a\u6cdb\u578blambda\u5982\u4f55\u53d8\u6210\u4e24\u4e2a\u4e0d\u540c\u7684lambda\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include #include #include int main () { // Print a value and count how many times @print has been called. auto print { []( auto value ) { static int callCount { 0 }; std :: cout << callCount ++ << \": \" << value << '\\n' ; } }; print ( \"hello\" ); // 0: hello print ( \"world\" ); // 1: world print ( 1 ); // 0: 1 print ( 2 ); // 1: 2 print ( \"ding dong\" ); // 2: ding dong return 0 ; } \u8f93\u51fa\uff1a 1 2 3 4 5 0: hello 1: world 0: 1 1: 2 2: ding dong \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2alambda\uff0c\u7136\u540e\u4f7f\u7528\u4e24\u4e2a\u4e0d\u540c\u7c7b\u578b\u7684\u53c2\u6570\u5bf9\u5176\u8fdb\u884c\u4e86\u8c03\u7528\uff08\u4e00\u4e2a\u662f\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff0c\u53e6\u4e00\u4e2a\u662f\u6574\u578b\u53c2\u6570\uff09\u3002\u8fd9\u4f1a\u751f\u6210\u4e24\u4e2a\u4e0d\u540c\u7248\u672c\u7684lambda(\u4e00\u4e2a\u662f\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u5f62\u53c2\uff0c\u53e6\u4e00\u4e2a\u5e26\u6709\u6574\u6570\u5f62\u53c2)\u3002 \u5927\u591a\u6570\u65f6\u5019\uff0c\u8fd9\u662f\u65e0\u5173\u7d27\u8981\u7684\u3002\u4f46\u662f\uff0c\u8bf7\u6ce8\u610f\uff0c \u5982\u679c\u6cdb\u578blambda\u4f7f\u7528 \u9759\u6001\u53d8\u91cf \uff0c\u5219\u751f\u6210\u7684lambda\u4e4b\u95f4\u4e0d\u4f1a\u5171\u4eab\u8fd9\u4e9b\u53d8\u91cf\u3002 \u4ece\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u53ef\u4ee5\u770b\u51fa\u8fd9\u4e00\u70b9\uff0c\u5176\u4e2d\u6bcf\u79cd\u7c7b\u578b(\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u548c\u6574\u6570)\u90fd\u6709\u81ea\u5df1\u552f\u4e00\u7684\u8ba1\u6570\uff01\u867d\u7136\u6211\u4eec\u53ea\u5199\u4e86\u4e00\u6b21lambda\uff0c\u4f46\u662f\u751f\u6210\u4e86\u4e24\u4e2alambda\u2014\u2014\u5e76\u4e14\u6bcf\u4e2alambda\u90fd\u6709\u81ea\u5df1\u7684 callCount \u3002\u8981\u5728\u4e24\u4e2a\u751f\u6210\u7684lambda\u4e4b\u95f4\u6709\u4e00\u4e2a\u5171\u4eab\u8ba1\u6570\u5668\uff0c\u6211\u4eec\u5fc5\u987b\u5728lambda\u5916\u90e8\u5b9a\u4e49\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u6216\u4e00\u4e2a\u201c\u9759\u6001\u201d\u5c40\u90e8\u53d8\u91cf\u3002\u6b63\u5982\u5728\u524d\u9762\u7684\u8bfe\u7a0b\u4e2d\u4e86\u5b66\u5230\u7684\uff0c\u5168\u5c40\u548c\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u5bb9\u6613\u5e26\u6765\u95ee\u9898\uff0c\u800c\u4e14\u4f1a\u964d\u4f4e\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u5728\u4e0b\u4e00\u8bfe\u4e2d\u8ba8\u8bbalambda\u6355\u83b7\u4e4b\u540e\uff0c\u6211\u4eec\u5c06\u80fd\u591f\u907f\u514d\u8fd9\u4e9b\u53d8\u91cf\u3002 \u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad\u548c\u5c3e\u968f\u8fd4\u56de\u503c\u7c7b\u578b \u5982\u679c\u4f7f\u7528\u4e86 \u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad \uff0c\u5219 lambda \u7684\u8fd4\u56de\u503c\u7c7b\u578b\u9700\u8981\u4ece return \u8bed\u53e5\u8fdb\u884c\u63a8\u65ad\uff0c\u800c\u4e14\u6240\u6709\u7684\u8fd4\u56de\u7c7b\u578b\u5fc5\u987b\u76f8\u540c\uff08\u5426\u5219\u7f16\u8bd1\u5668\u5c06\u96be\u4ee5\u5224\u65ad\uff09\uff1a \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { auto divide { []( int x , int y , bool intDivision ) { // note: no specified return type if ( intDivision ) return x / y ; // return type is int else return static_cast < double > ( x ) / y ; // ERROR: return type doesn't match previous return type } }; std :: cout << divide ( 3 , 2 , true ) << '\\n' ; std :: cout << divide ( 3 , 2 , false ) << '\\n' ; return 0 ; } \u8fd9\u5c06\u4ea7\u751f\u4e00\u4e2a\u7f16\u8bd1\u9519\u8bef\uff0c\u56e0\u4e3a\u7b2c\u4e00\u4e2a\u8fd4\u56de\u8bed\u53e5\u7684\u8fd4\u56de\u7c7b\u578b(int)\u4e0e\u7b2c\u4e8c\u4e2a\u8fd4\u56de\u8bed\u53e5\u7684\u8fd4\u56de\u7c7b\u578b(double)\u4e0d\u5339\u914d\u3002 \u5728\u8fd4\u56de\u4e0d\u540c\u7c7b\u578b\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u6709\u4e24\u4e2a\u9009\u9879\uff1a \u662f\u5426\u663e\u5f0f\u5f3a\u5236\u8f6c\u6362\u4ee5\u4f7f\u6240\u6709\u8fd4\u56de\u7c7b\u578b\u5339\u914d\uff0c\u8fd8\u662f \u663e\u5f0f\u6307\u5b9alambda\u7684\u8fd4\u56de\u7c7b\u578b\uff0c\u5e76\u8ba9\u7f16\u8bd1\u5668\u6267\u884c\u9690\u5f0f\u8f6c\u6362\u3002 \u7b2c\u4e8c\u79cd\u60c5\u51b5\u901a\u5e38\u662f\u66f4\u597d\u7684\u9009\u62e9: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { // note: explicitly specifying this returns a double auto divide { []( int x , int y , bool intDivision ) -> double { if ( intDivision ) return x / y ; // will do an implicit conversion of result to double else return static_cast < double > ( x ) / y ; } }; std :: cout << divide ( 3 , 2 , true ) << '\\n' ; std :: cout << divide ( 3 , 2 , false ) << '\\n' ; return 0 ; } \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5982\u679c\u4eca\u540e\u4f60\u51b3\u5b9a\u4fee\u6539\u8fd4\u56de\u503c\u7684\u7c7b\u578b\uff0c\u90a3\u4e48\u4f60\u901a\u5e38\u53ea\u9700\u8981\u4fee\u6539lambda\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u5373\u53ef\uff0c\u800c\u4e0d\u5fc5\u4fee\u6539\u5176\u51fd\u6570\u5185\u5bb9\u3002 \u6807\u51c6\u5e93\u4e2d\u7684\u51fd\u6570\u5bf9\u8c61 \u5bf9\u4e8e\u5f88\u591a\u57fa\u672c\u64cd\u4f5c(\u4f8b\u5982\uff1aaddition, negation \u6216 comparison) \u6765\u8bf4\uff0c\u5176\u5b9e\u6211\u4eec\u4e0d\u9700\u8981\u81ea\u5df1\u7f16\u5199lambda\uff0c\u56e0\u4e3a\u6807\u51c6\u5e93\u4e2d\u63d0\u4f9b\u5f88\u591a\u53ef\u4ee5\u4f7f\u7528\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u3002\u5b83\u4eec\u90fd\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include #include bool greater ( int a , int b ) { // Order @a before @b if @a is greater than @b. return ( a > b ); } int main () { std :: array arr { 13 , 90 , 99 , 5 , 40 , 80 }; // Pass greater to std::sort std :: sort ( arr . begin (), arr . end (), greater ); for ( int i : arr ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\uff1a 1 99 90 80 40 13 5 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u5c06 greater \u51fd\u6570\u6539\u5199\u4e3alambda\uff0c\u800c\u662f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 std::greater \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include #include #include // for std::greater int main () { std :: array arr { 13 , 90 , 99 , 5 , 40 , 80 }; // Pass std::greater to std::sort std :: sort ( arr . begin (), arr . end (), std :: greater {}); // note: need curly braces to instantiate object for ( int i : arr ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\uff1a 1 99 90 80 40 13 5 \u5c0f\u7ed3 \u4e0e\u4f7f\u7528\u5faa\u73af\u7684\u89e3\u51b3\u65b9\u6848\u76f8\u6bd4\uff0c\u533f\u540d\u51fd\u6570\u548c\u7b97\u6cd5\u5e93\u53ef\u80fd\u7a0d\u663e\u5f97\u4e0d\u5fc5\u8981\u5730\u590d\u6742\u3002\u7136\u800c\uff0c\u8fd9\u79cd\u7ec4\u5408\u53ef\u4ee5\u5728\u77ed\u77ed\u51e0\u884c\u4ee3\u7801\u4e2d\u5b9e\u73b0\u4e00\u4e9b\u975e\u5e38\u5f3a\u5927\u7684\u64cd\u4f5c\uff0c\u5e76\u4e14\u6bd4\u81ea\u5df1\u7f16\u5199\u5faa\u73af\u66f4\u5177\u6709\u53ef\u8bfb\u6027\u3002\u6700\u91cd\u8981\u7684\u662f\uff0c\u7b97\u6cd5\u5e93\u5177\u6709\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\u5e76\u884c\u6027\uff0c\u8fd9\u662f\u5faa\u73af\u6240\u65e0\u6cd5\u83b7\u5f97\u7684\u3002\u66f4\u65b0\u4f7f\u7528\u5e93\u51fd\u6570\u7684\u6e90\u4ee3\u7801\u6bd4\u66f4\u65b0\u4f7f\u7528\u5faa\u73af\u7684\u4ee3\u7801\u66f4\u5bb9\u6613\u3002 \u533f\u540d\u51fd\u6570\u5f88\u597d\uff0c\u4f46\u5b83\u4eec\u4e0d\u80fd\u5728\u6240\u6709\u60c5\u51b5\u4e0b\u53d6\u4ee3\u5e38\u89c4\u51fd\u6570\u3002\u5bf9\u4e8e\u529f\u80fd\u590d\u6742\u4e14\u9700\u8981\u53ef\u91cd\u7528\u7684\u60c5\u5f62\uff0c\u9996\u9009\u5e38\u89c4\u51fd\u6570\u3002","title":"12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09"},{"location":"12-7-introduction-to-lambdas-anonymous-functions/#lambda","text":"\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f(\u6709\u65f6\u5019\u4e5f\u79f0lambda\u6216 \u95ed\u5305 \u6216\u533f\u540d\u51fd\u6570) \u53ef\u4ee5\u5728\u5176\u4ed6\u51fd\u6570\u4e2d\u5b9a\u4e49\u533f\u540d\u51fd\u6570\u3002\u80fd\u591f\u5d4c\u5957\u5b9a\u4e49\u662f\u5f88\u91cd\u8981\u7684\u7279\u6027\uff0c\u5b83\u53ef\u4ee5\u907f\u514d \u547d\u540d\u7a7a\u95f4 \u4e2d\u7684\u540d\u79f0\u6c61\u67d3\uff0c\u800c\u4e14\u53ef\u4ee5\u5c06\u51fd\u6570\u7684\u5b9a\u4e49\u5c3d\u53ef\u80fd\u9760\u8fd1\u4f7f\u7528\u5b83\u7684\u5730\u65b9\uff08\u907f\u514d\u989d\u5916\u7684\u4e0a\u4e0b\u6587\uff09\u3002 lambda \u7684\u8bed\u6cd5\u662fC++\u4e2d\u6700\u5947\u602a\u7684\u4e1c\u897f\u4e4b\u4e00\uff0c\u9700\u8981\u4e00\u70b9\u65f6\u95f4\u6765\u9002\u5e94\u3002\u533f\u540d\u51fd\u6570\u5f62\u5f0f\u662f: 1 2 3 4 [ captureClause ] ( parameters ) -> returnType { statements ; } \u95ed\u5305\u8bed\u53e5\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u5982\u679c\u4e0d\u9700\u8981\u6355\u83b7\u53d8\u91cf\u7684\u8bdd\uff1b \u5f62\u53c2 \u5217\u8868\u4e5f\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u5982\u679c\u4e0d\u9700\u8981\u5f62\u53c2\u7684\u8bdd\uff1b \u8fd4\u56de\u7c7b\u578b\u662f\u53ef\u9009\u7684\uff0c\u5982\u679c\u7701\u7565\u7684\u8bdd\uff0c\u4f1a\u5047\u5b9a\u4e3a auto (\u4f7f\u7528 \u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad )\u3002\u5c3d\u7ba1\u6211\u4eec\u4e4b\u524d\u8bf4\u8fc7\uff0c \u5e94\u8be5\u907f\u514d\u4f7f\u7528\u51fd\u6570\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u63a8\u65ad \uff0c\u4f46\u662f\u5728\u533f\u540d\u8868\u8fbe\u5f0f\u4e2d\u662f\u53ef\u4ee5\u7528\u7684\uff08\u56e0\u4e3a\u8fd9\u4e9b\u51fd\u6570\u901a\u5e38\u90fd\u975e\u5e38\u7b80\u5355\uff09\u3002 \u56e0\u4e3alambda \u6ca1\u6709\u51fd\u6570\u540d\uff0c\u6240\u4ee5\u4e0d\u5fc5\u4e3a\u5176\u8d77\u540d\u3002 \u9898\u5916\u8bdd \u4e00\u4e2a\u57fa\u672c\u7684\u533f\u540d\u51fd\u6570\u5b9a\u4e49\u770b\u4e0a\u53bb\u50cf\u662f\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 #include int main () { [] {}; // \u7701\u7565\u8fd4\u56de\u503c\u7c7b\u578b, \u65e0\u95ed\u5305\uff0c\u65e0\u53c2\u6570 return 0 ; } \u8ba9\u6211\u4eec\u7528\u533f\u540d\u51fd\u6570\u91cd\u5199\u4e0a\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include #include #include int main () { constexpr std :: array < std :: string_view , 4 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" }; // Define the function right where we use it. const auto found { std :: find_if ( arr . begin (), arr . end (), []( std :: string_view str ) // \u533f\u540d\u51fd\u6570\uff0c\u65e0\u95ed\u5305 { return ( str . find ( \"nut\" ) != std :: string_view :: npos ); }) }; if ( found == arr . end ()) { std :: cout << \"No nuts \\n \" ; } else { std :: cout << \"Found \" << * found << '\\n' ; } return 0 ; } \u548c\u4f7f\u7528\u51fd\u6570\u6307\u9488\u7684\u60c5\u51b5\u4e00\u6837\uff0c\u4ea7\u751f\u76f8\u540c\u7684\u7ed3\u679c: 1 Found walnut \u53ef\u4ee5\u770b\u5230\uff0clambda \u548c\u4e4b\u524d\u7684 containsNut \u51fd\u6570\u975e\u5e38\u7c7b\u578b\u3002\u5b83\u4eec\u7684\u53c2\u6570\u548c\u51fd\u6570\u4f53\u662f\u5b8c\u5168\u4e00\u6837\u7684\u3002\u8fd9\u4e2alambda\u6ca1\u6709\u95ed\u5305\uff08\u4e0b\u8282\u8bfe\u4ecb\u7ecd\uff09\uff0c\u56e0\u4e3a\u6ca1\u5fc5\u8981\u3002\u4e0d\u4ec5\u5982\u6b64\u6211\u4eec\u8fd8\u7701\u7565\u4e86\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u4f46\u662f\u7531\u4e8e operator!= \u8fd4\u56de bool \uff0c\u6240\u4ee5\u8be5lambda\u4e5f\u8fd4\u56de\u5e03\u5c14\u7c7b\u578b\u3002","title":"lambda"},{"location":"12-7-introduction-to-lambdas-anonymous-functions/#lambda_1","text":"\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728\u9700\u8981\u7684\u5730\u65b9\u5b9a\u4e49\u4e86\u533f\u540d\u51fd\u6570\uff0c\u5b83\u7684\u8fd9\u79cd\u7528\u6cd5\u6709\u65f6\u88ab\u79f0\u4e3a \u51fd\u6570\u5b57\u9762\u91cf \u3002 \u7136\u800c\uff0c\u5728\u540c\u4e00\u884c\u4e2d\u7f16\u5199lambda\u6709\u65f6\u4f1a\u964d\u4f4e\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u5c31\u50cf\u6211\u4eec\u53ef\u4ee5\u7528\u5b57\u9762\u503c(\u6216\u51fd\u6570\u6307\u9488)\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cf\u4ee5\u4f9b\u4ee5\u540e\u4f7f\u7528\u4e00\u6837\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u7528lambda\u5b9a\u4e49\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cf\uff0c\u7136\u540e\u5728\u4ee5\u540e\u4f7f\u7528\u5b83\u3002\u4e00\u4e2a\u6709\u597d\u540d\u5b57\u7684lambda\u51fd\u6570\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u5bb9\u6613\u9605\u8bfb\u3002 \u4f8b\u5982\uff0c\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 std::all_of \u6765\u68c0\u67e5\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u662f\u5426\u4e3a\u5076\u6570\uff1a 1 2 // Bad: We have to read the lambda to understand what's happening. return std :: all_of ( array . begin (), array . end (), []( int i ){ return (( i % 2 ) == 0 ); }); \u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u65b9\u5f0f\u63d0\u5347\u53ef\u8bfb\u6027\u3002 1 2 3 4 5 6 7 8 9 // Good: Instead, we can store the lambda in a named variable and pass it to the function. auto isEven { []( int i ) { return (( i % 2 ) == 0 ); } }; return std :: all_of ( array . begin (), array . end (), isEven ); \u6ce8\u610f\uff0c\u73b0\u5728\u6700\u540e\u4e00\u884c\u4ee3\u7801\u53ef\u4ee5\u5f88\u81ea\u7136\u5730\u88ab\u7406\u89e3\u4e3a\uff1a \u5224\u65ad\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\u662f\u5426\u90fd\u662f\u5076\u6570 \u4e0d\u8fc7\uff0c isEven \u7684\u7c7b\u578b\u662f\u4ec0\u4e48\u5462\uff1f \u4e8b\u5b9e\u8bc1\u660e\uff0clambdas\u6ca1\u6709\u53ef\u4ee5\u663e\u5f0f\u4f7f\u7528\u7684\u7c7b\u578b\u3002\u5f53\u6211\u4eec\u7f16\u5199lambda\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u4e3a\u5176\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684\u7c7b\u578b\uff0c\u4f46\u5bf9\u6211\u4eec\u5e76\u4e0d\u53ef\u89c1\u3002 \u6269\u5c55\u9605\u8bfb \u5b9e\u9645\u4e0a\uff0clambda\u5e76\u4e0d\u662f\u51fd\u6570(\u8fd9\u662f\u5b83\u4eec\u53ef\u4ee5\u907f\u514dC++\u4e0d\u652f\u6301\u5d4c\u5957\u51fd\u6570\u7684\u9650\u5236\u7684\u90e8\u5206\u539f\u56e0)\u3002\u5b83\u4eec\u662f\u4e00\u79cd\u53eb\u505afunctor\u7684\u7279\u6b8a\u5bf9\u8c61\u3002\u51fd\u5b50\u662f\u5305\u542b\u91cd\u8f7d\u7684 operator() \u7684\u5bf9\u8c61\uff0c\u4f7f\u5b83\u4eec\u50cf\u51fd\u6570\u4e00\u6837\u53ef\u8c03\u7528\u3002 \u5c3d\u7ba1\u6211\u4eec\u5e76\u4e0d\u77e5\u9053lambda\u7684\u7c7b\u578b\uff0c\u4f46\u662f\u6211\u4eec\u4ecd\u7136\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5176\u5b58\u653e\u5230\u67d0\u4e2a\u53d8\u91cf\u4e2d\u3002\u5982\u679c\u4e00\u4e2alambda\u6ca1\u6709\u95ed\u5305\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u666e\u901a\u7684\u51fd\u6570\u6307\u9488\u5b58\u653e\u5b83\u3002\u4f7f\u7528 std::function \u6216\u8005\u901a\u8fc7 auto \u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\u4e5f\u662f\u53ef\u4ee5\u7684(\u5373\u4fbf\u6b64\u65f6lambda\u6709\u95ed\u5305\u8bed\u53e5)\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include int main () { // A regular function pointer. Only works with an empty capture clause (empty []). double ( * addNumbers1 )( double , double ){ []( double a , double b ) { return ( a + b ); } }; addNumbers1 ( 1 , 2 ); // Using std::function. The lambda could have a non-empty capture clause (discussed next lesson). std :: function addNumbers2 { // note: pre-C++17, use std::function instead []( double a , double b ) { return ( a + b ); } }; addNumbers2 ( 3 , 4 ); // Using auto. Stores the lambda with its real type. auto addNumbers3 { []( double a , double b ) { return ( a + b ); } }; addNumbers3 ( 5 , 6 ); return 0 ; } \u4f7f\u7528lambda\u7684\u5b9e\u9645\u7c7b\u578b\u7684\u552f\u4e00\u65b9\u6cd5\u662f\u901a\u8fc7 auto \u3002\u4e0e std::function \u76f8\u6bd4\uff0c auto \u8fd8\u6709\u4e00\u4e2a\u597d\u5904\uff0c\u90a3\u5c31\u662f\u6ca1\u6709\u5f00\u9500\u3002 \u4e0d\u5e78\u7684\u662f\uff0c\u5728C++ 20\u4e4b\u524d\uff0c\u6211\u4eec\u4e0d\u80fd\u603b\u662f\u4f7f\u7528 auto \u3002\u5728\u5b9e\u9645lambda\u672a\u77e5\u7684\u60c5\u51b5\u4e0b(\u4f8b\u5982\uff0c\u56e0\u4e3a\u6211\u4eec\u5c06lambda\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u7531\u8c03\u7528\u8005\u51b3\u5b9a\u5c06\u4f20\u5165\u4ec0\u4e48lambda)\uff0c\u6211\u4eec\u4e0d\u80fd\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f7f\u7528 auto \u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528 std::function \u6765\u4ee3\u66ff\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include // We don't know what fn will be. std::function works with regular functions and lambdas. void repeat ( int repetitions , const std :: function < void ( int ) >& fn ) { for ( int i { 0 }; i < repetitions ; ++ i ) { fn ( i ); } } int main () { repeat ( 3 , []( int i ) { std :: cout << i << '\\n' ; }); return 0 ; } \u8f93\u51fa 1 2 3 0 1 2 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u6211\u4eec\u4e0d\u80fd\u4f7f\u7528 auto \u5173\u952e\u5b57\uff0c\u8fd9\u4e48\u505a\u7684\u8bdd\u51fd\u6570\u7684\u8c03\u7528\u8005\u5c31\u65e0\u6cd5\u77e5\u9053 fn \u5e94\u8be5\u6709\u4ec0\u4e48\u6837\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u3002\u8fd9\u4e2a\u9650\u5236\u5728C++20\u5f15\u5165 \u7f29\u5199\u51fd\u6570\u6a21\u677f \u4e4b\u540e\u5c31\u4e0d\u5b58\u5728\u4e86\u3002 \u6b64\u5916\uff0c\u56e0\u4e3a\u5b83\u4eec\u5b9e\u9645\u4e0a\u662f\u6a21\u677f\uff0c\u5177\u6709 auto \u53c2\u6570\u7684\u51fd\u6570\u4e0d\u80fd\u88ab\u5206\u79bb\u5230\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u4e2d\u3002 \u6cd5\u5219 \u9700\u8981\u7528 lambda \u521d\u59cb\u5316\u53d8\u91cf\u65f6\uff0c\u4f7f\u7528 auto \u5173\u952e\u5b57\u3002\u7f6a\u57df\u4e0d\u80fd\u7528 lambda \u521d\u59cb\u5316\u53d8\u91cf\u7684\u573a\u5408\uff0c\u4f7f\u7528 std::function \u3002","title":"lambda \u51fd\u6570\u7684\u7c7b\u578b"},{"location":"12-7-introduction-to-lambdas-anonymous-functions/#lambda_2","text":"\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0clambda\u5f62\u53c2\u7684\u5de5\u4f5c\u89c4\u5219\u4e0e\u5e38\u89c4\u51fd\u6570\u5f62\u53c2\u76f8\u540c\u3002 \u4e00\u4e2a\u503c\u5f97\u6ce8\u610f\u7684\u4f8b\u5916\u662f\uff0c\u4eceC++ 14\u5f00\u59cb\uff0c\u6211\u4eec\u5141\u8bb8\u4f7f\u7528 auto \u4f5c\u4e3a\u5f62\u53c2\u7c7b\u578b(\u6ce8\u610f:\u5728c++ 20\u4e2d\uff0c\u5e38\u89c4\u51fd\u6570\u4e5f\u53ef\u4ee5\u4f7f\u7528 auto \u4f5c\u4e3a\u5f62\u53c2\u7c7b\u578b)\u3002\u5f53lambda\u6709\u4e00\u4e2a\u6216\u591a\u4e2a auto \u5f62\u53c2\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u4ece\u5bf9lambda\u7684\u8c03\u7528\u4e2d\u63a8\u65ad\u9700\u8981\u4ec0\u4e48\u5f62\u53c2\u7c7b\u578b\u3002 \u56e0\u4e3a\u5e26\u6709\u4e00\u4e2a\u6216\u591a\u4e2a auto \u53c2\u6570\u7684 lambda\u53ef\u4ee5\u6f5c\u5728\u5730\u4e0e\u5404\u79cd\u7c7b\u578b\u4e00\u8d77\u5de5\u4f5c\uff0c\u6240\u4ee5\u5b83\u4eec\u88ab\u79f0\u4e3a\u6cdb\u578blambda\u3002 \u6269\u5c55\u9605\u8bfb \u5f53\u5728lambda\u7684\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\u65f6\uff0c auto \u53ea\u662f\u6a21\u677f\u5f62\u53c2\u7684\u7f29\u5199\u3002 \u770b\u4e00\u4e2a\u6cdb\u578blambda\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include #include #include int main () { constexpr std :: array months { // pre-C++17 use std::array \"January\" , \"February\" , \"March\" , \"April\" , \"May\" , \"June\" , \"July\" , \"August\" , \"September\" , \"October\" , \"November\" , \"December\" }; // Search for two consecutive months that start with the same letter. const auto sameLetter { std :: adjacent_find ( months . begin (), months . end (), []( const auto & a , const auto & b ) { return ( a [ 0 ] == b [ 0 ]); }) }; // Make sure that two months were found. if ( sameLetter != months . end ()) { // std::next returns the next iterator after sameLetter std :: cout << * sameLetter << \" and \" << * std :: next ( sameLetter ) << \" start with the same letter \\n \" ; } return 0 ; } \u8f93\u51fa\uff1a 1 June and July start with the same letter \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 auto \u5f62\u53c2\u6765\u83b7\u53d6\u5b57\u7b26\u4e32\u7684 const \u5f15\u7528\u3002\u7531\u4e8e\u6240\u6709\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\u90fd\u53ef\u4ee5\u901a\u8fc7 operator[] \u8bbf\u95ee\u5355\u4e2a\u5b57\u7b26\u3002\u6240\u4ee5\u6211\u4eec\u65e0\u9700\u5173\u5fc3\u4f20\u5165\u5b57\u7b26\u4e32\u662f std::string \u3001C\u98ce\u683c\u5b57\u7b26\u4e32\u8fd8\u662f\u5176\u4ed6\u5b57\u7b26\u4e32\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u7f16\u5199\u4ecb\u7ecd\u8fd9\u4e9b\u7c7b\u578b\u7684lambda\u8868\u8fbe\u5f0f\uff0c\u5982\u679c\u5c06\u6765\u9700\u8981\u4fee\u6539 months \u7684\u7c7b\u578b\uff0c\u4e5f\u4e0d\u5fc5\u4fee\u6539lambda\u3002 \u4e0d\u8fc7\uff0c\u4f7f\u7528 auto \u5e76\u4e0d\u603b\u662f\u6700\u4f73\u7684\u9009\u62e9\uff0c\u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include #include #include int main () { constexpr std :: array months { // pre-C++17 use std::array \"January\" , \"February\" , \"March\" , \"April\" , \"May\" , \"June\" , \"July\" , \"August\" , \"September\" , \"October\" , \"November\" , \"December\" }; // Count how many months consist of 5 letters const auto fiveLetterMonths { std :: count_if ( months . begin (), months . end (), []( std :: string_view str ) { return ( str . length () == 5 ); }) }; std :: cout << \"There are \" << fiveLetterMonths << \" months with 5 letters \\n \" ; return 0 ; } \u8f93\u51fa\uff1a 1 There are 2 months with 5 letters \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u4f7f\u7528 auto \uff0c\u5219\u4f1a\u63a8\u65ad\u51fa const char* \u3002\u7531\u4e8e C \u98ce\u683c\u5b57\u7b26\u4e32\u5e76\u4e0d\u6613\u7528\uff08\u9664\u4e86 operator[] \uff09\u3002\u6240\u4ee5\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u6211\u4eec\u663e\u5f0f\u5730\u5b9a\u4e49\u53c2\u6570\u4e3a std::string_view \u7c7b\u578b\uff0c\u8fd9\u4f7f\u5f97\u6211\u4eec\u5728\u5904\u7406\u6570\u636e\u65f6\u53ef\u4ee5\u66f4\u5bb9\u6613\u3002(\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u67e5\u8be2string view \u7684\u957f\u5ea6\uff0c\u5373\u4fbf\u4f20\u5165\u7684\u662f\u4e00\u4e2aC\u98ce\u683c\u5b57\u7b26\u4e32\u6570\u7ec4)\u3002","title":"\u6cdb\u578blambda\u51fd\u6570"},{"location":"12-7-introduction-to-lambdas-anonymous-functions/#lambda_3","text":"\u9700\u8981\u6ce8\u610f\u7684\u4e00\u4ef6\u4e8b\u662f\uff0c\u7f16\u8bd1\u5668\u5c06\u4e3a auto \u89e3\u6790\u51fa\u7684\u6bcf\u4e2a\u4e0d\u540c\u7c7b\u578b\u751f\u6210\u552f\u4e00\u7684lambda\u8868\u8fbe\u5f0f\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u4e00\u4e2a\u6cdb\u578blambda\u5982\u4f55\u53d8\u6210\u4e24\u4e2a\u4e0d\u540c\u7684lambda\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include #include #include int main () { // Print a value and count how many times @print has been called. auto print { []( auto value ) { static int callCount { 0 }; std :: cout << callCount ++ << \": \" << value << '\\n' ; } }; print ( \"hello\" ); // 0: hello print ( \"world\" ); // 1: world print ( 1 ); // 0: 1 print ( 2 ); // 1: 2 print ( \"ding dong\" ); // 2: ding dong return 0 ; } \u8f93\u51fa\uff1a 1 2 3 4 5 0: hello 1: world 0: 1 1: 2 2: ding dong \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2alambda\uff0c\u7136\u540e\u4f7f\u7528\u4e24\u4e2a\u4e0d\u540c\u7c7b\u578b\u7684\u53c2\u6570\u5bf9\u5176\u8fdb\u884c\u4e86\u8c03\u7528\uff08\u4e00\u4e2a\u662f\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff0c\u53e6\u4e00\u4e2a\u662f\u6574\u578b\u53c2\u6570\uff09\u3002\u8fd9\u4f1a\u751f\u6210\u4e24\u4e2a\u4e0d\u540c\u7248\u672c\u7684lambda(\u4e00\u4e2a\u662f\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u5f62\u53c2\uff0c\u53e6\u4e00\u4e2a\u5e26\u6709\u6574\u6570\u5f62\u53c2)\u3002 \u5927\u591a\u6570\u65f6\u5019\uff0c\u8fd9\u662f\u65e0\u5173\u7d27\u8981\u7684\u3002\u4f46\u662f\uff0c\u8bf7\u6ce8\u610f\uff0c \u5982\u679c\u6cdb\u578blambda\u4f7f\u7528 \u9759\u6001\u53d8\u91cf \uff0c\u5219\u751f\u6210\u7684lambda\u4e4b\u95f4\u4e0d\u4f1a\u5171\u4eab\u8fd9\u4e9b\u53d8\u91cf\u3002 \u4ece\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u53ef\u4ee5\u770b\u51fa\u8fd9\u4e00\u70b9\uff0c\u5176\u4e2d\u6bcf\u79cd\u7c7b\u578b(\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u548c\u6574\u6570)\u90fd\u6709\u81ea\u5df1\u552f\u4e00\u7684\u8ba1\u6570\uff01\u867d\u7136\u6211\u4eec\u53ea\u5199\u4e86\u4e00\u6b21lambda\uff0c\u4f46\u662f\u751f\u6210\u4e86\u4e24\u4e2alambda\u2014\u2014\u5e76\u4e14\u6bcf\u4e2alambda\u90fd\u6709\u81ea\u5df1\u7684 callCount \u3002\u8981\u5728\u4e24\u4e2a\u751f\u6210\u7684lambda\u4e4b\u95f4\u6709\u4e00\u4e2a\u5171\u4eab\u8ba1\u6570\u5668\uff0c\u6211\u4eec\u5fc5\u987b\u5728lambda\u5916\u90e8\u5b9a\u4e49\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u6216\u4e00\u4e2a\u201c\u9759\u6001\u201d\u5c40\u90e8\u53d8\u91cf\u3002\u6b63\u5982\u5728\u524d\u9762\u7684\u8bfe\u7a0b\u4e2d\u4e86\u5b66\u5230\u7684\uff0c\u5168\u5c40\u548c\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u5bb9\u6613\u5e26\u6765\u95ee\u9898\uff0c\u800c\u4e14\u4f1a\u964d\u4f4e\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u5728\u4e0b\u4e00\u8bfe\u4e2d\u8ba8\u8bbalambda\u6355\u83b7\u4e4b\u540e\uff0c\u6211\u4eec\u5c06\u80fd\u591f\u907f\u514d\u8fd9\u4e9b\u53d8\u91cf\u3002","title":"\u6cdb\u578blambda\u548c\u9759\u6001\u53d8\u91cf"},{"location":"12-7-introduction-to-lambdas-anonymous-functions/#_1","text":"\u5982\u679c\u4f7f\u7528\u4e86 \u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad \uff0c\u5219 lambda \u7684\u8fd4\u56de\u503c\u7c7b\u578b\u9700\u8981\u4ece return \u8bed\u53e5\u8fdb\u884c\u63a8\u65ad\uff0c\u800c\u4e14\u6240\u6709\u7684\u8fd4\u56de\u7c7b\u578b\u5fc5\u987b\u76f8\u540c\uff08\u5426\u5219\u7f16\u8bd1\u5668\u5c06\u96be\u4ee5\u5224\u65ad\uff09\uff1a \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { auto divide { []( int x , int y , bool intDivision ) { // note: no specified return type if ( intDivision ) return x / y ; // return type is int else return static_cast < double > ( x ) / y ; // ERROR: return type doesn't match previous return type } }; std :: cout << divide ( 3 , 2 , true ) << '\\n' ; std :: cout << divide ( 3 , 2 , false ) << '\\n' ; return 0 ; } \u8fd9\u5c06\u4ea7\u751f\u4e00\u4e2a\u7f16\u8bd1\u9519\u8bef\uff0c\u56e0\u4e3a\u7b2c\u4e00\u4e2a\u8fd4\u56de\u8bed\u53e5\u7684\u8fd4\u56de\u7c7b\u578b(int)\u4e0e\u7b2c\u4e8c\u4e2a\u8fd4\u56de\u8bed\u53e5\u7684\u8fd4\u56de\u7c7b\u578b(double)\u4e0d\u5339\u914d\u3002 \u5728\u8fd4\u56de\u4e0d\u540c\u7c7b\u578b\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u6709\u4e24\u4e2a\u9009\u9879\uff1a \u662f\u5426\u663e\u5f0f\u5f3a\u5236\u8f6c\u6362\u4ee5\u4f7f\u6240\u6709\u8fd4\u56de\u7c7b\u578b\u5339\u914d\uff0c\u8fd8\u662f \u663e\u5f0f\u6307\u5b9alambda\u7684\u8fd4\u56de\u7c7b\u578b\uff0c\u5e76\u8ba9\u7f16\u8bd1\u5668\u6267\u884c\u9690\u5f0f\u8f6c\u6362\u3002 \u7b2c\u4e8c\u79cd\u60c5\u51b5\u901a\u5e38\u662f\u66f4\u597d\u7684\u9009\u62e9: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { // note: explicitly specifying this returns a double auto divide { []( int x , int y , bool intDivision ) -> double { if ( intDivision ) return x / y ; // will do an implicit conversion of result to double else return static_cast < double > ( x ) / y ; } }; std :: cout << divide ( 3 , 2 , true ) << '\\n' ; std :: cout << divide ( 3 , 2 , false ) << '\\n' ; return 0 ; } \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5982\u679c\u4eca\u540e\u4f60\u51b3\u5b9a\u4fee\u6539\u8fd4\u56de\u503c\u7684\u7c7b\u578b\uff0c\u90a3\u4e48\u4f60\u901a\u5e38\u53ea\u9700\u8981\u4fee\u6539lambda\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u5373\u53ef\uff0c\u800c\u4e0d\u5fc5\u4fee\u6539\u5176\u51fd\u6570\u5185\u5bb9\u3002","title":"\u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad\u548c\u5c3e\u968f\u8fd4\u56de\u503c\u7c7b\u578b"},{"location":"12-7-introduction-to-lambdas-anonymous-functions/#_2","text":"\u5bf9\u4e8e\u5f88\u591a\u57fa\u672c\u64cd\u4f5c(\u4f8b\u5982\uff1aaddition, negation \u6216 comparison) \u6765\u8bf4\uff0c\u5176\u5b9e\u6211\u4eec\u4e0d\u9700\u8981\u81ea\u5df1\u7f16\u5199lambda\uff0c\u56e0\u4e3a\u6807\u51c6\u5e93\u4e2d\u63d0\u4f9b\u5f88\u591a\u53ef\u4ee5\u4f7f\u7528\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u3002\u5b83\u4eec\u90fd\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include #include bool greater ( int a , int b ) { // Order @a before @b if @a is greater than @b. return ( a > b ); } int main () { std :: array arr { 13 , 90 , 99 , 5 , 40 , 80 }; // Pass greater to std::sort std :: sort ( arr . begin (), arr . end (), greater ); for ( int i : arr ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\uff1a 1 99 90 80 40 13 5 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u5c06 greater \u51fd\u6570\u6539\u5199\u4e3alambda\uff0c\u800c\u662f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 std::greater \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include #include #include // for std::greater int main () { std :: array arr { 13 , 90 , 99 , 5 , 40 , 80 }; // Pass std::greater to std::sort std :: sort ( arr . begin (), arr . end (), std :: greater {}); // note: need curly braces to instantiate object for ( int i : arr ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\uff1a 1 99 90 80 40 13 5","title":"\u6807\u51c6\u5e93\u4e2d\u7684\u51fd\u6570\u5bf9\u8c61"},{"location":"12-7-introduction-to-lambdas-anonymous-functions/#_3","text":"\u4e0e\u4f7f\u7528\u5faa\u73af\u7684\u89e3\u51b3\u65b9\u6848\u76f8\u6bd4\uff0c\u533f\u540d\u51fd\u6570\u548c\u7b97\u6cd5\u5e93\u53ef\u80fd\u7a0d\u663e\u5f97\u4e0d\u5fc5\u8981\u5730\u590d\u6742\u3002\u7136\u800c\uff0c\u8fd9\u79cd\u7ec4\u5408\u53ef\u4ee5\u5728\u77ed\u77ed\u51e0\u884c\u4ee3\u7801\u4e2d\u5b9e\u73b0\u4e00\u4e9b\u975e\u5e38\u5f3a\u5927\u7684\u64cd\u4f5c\uff0c\u5e76\u4e14\u6bd4\u81ea\u5df1\u7f16\u5199\u5faa\u73af\u66f4\u5177\u6709\u53ef\u8bfb\u6027\u3002\u6700\u91cd\u8981\u7684\u662f\uff0c\u7b97\u6cd5\u5e93\u5177\u6709\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\u5e76\u884c\u6027\uff0c\u8fd9\u662f\u5faa\u73af\u6240\u65e0\u6cd5\u83b7\u5f97\u7684\u3002\u66f4\u65b0\u4f7f\u7528\u5e93\u51fd\u6570\u7684\u6e90\u4ee3\u7801\u6bd4\u66f4\u65b0\u4f7f\u7528\u5faa\u73af\u7684\u4ee3\u7801\u66f4\u5bb9\u6613\u3002 \u533f\u540d\u51fd\u6570\u5f88\u597d\uff0c\u4f46\u5b83\u4eec\u4e0d\u80fd\u5728\u6240\u6709\u60c5\u51b5\u4e0b\u53d6\u4ee3\u5e38\u89c4\u51fd\u6570\u3002\u5bf9\u4e8e\u529f\u80fd\u590d\u6742\u4e14\u9700\u8981\u53ef\u91cd\u7528\u7684\u60c5\u5f62\uff0c\u9996\u9009\u5e38\u89c4\u51fd\u6570\u3002","title":"\u5c0f\u7ed3"},{"location":"12-8-lambda-captures/","tags":["summary"],"text":"Key Takeaway \u6355\u83b7\u8bed\u53e5\u4ee5\u53ca\u6355\u83b7\u503c \u5728\u4e0a\u8282\u8bfe 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb ) \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u6574\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include #include #include int main () { std :: array < std :: string_view , 4 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" }; auto found { std :: find_if ( arr . begin (), arr . end (), []( std :: string_view str ) { return ( str . find ( \"nut\" ) != std :: string_view :: npos ); }) }; if ( found == arr . end ()) { std :: cout << \"No nuts \\n \" ; } else { std :: cout << \"Found \" << * found << '\\n' ; } return 0 ; } \u63a5\u4e0b\u6765\uff0c\u4fee\u6539\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u8ba9\u7528\u6237\u51b3\u5b9a\u9700\u8981\u641c\u7d22\u7684\u5b50\u4e32\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u5e76\u4e0d\u60f3\u4f60\u60f3\u8c61\u7684\u90a3\u4e48\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include #include #include #include int main () { std :: array < std :: string_view , 4 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" }; // Ask the user what to search for. std :: cout << \"search for: \" ; std :: string search {}; std :: cin >> search ; auto found { std :: find_if ( arr . begin (), arr . end (), []( std :: string_view str ) { // \u641c\u7d22 @search \u800c\u4e0d\u662f \"nut\". return ( str . find ( search ) != std :: string_view :: npos ); // \u9519\u8bef: search \u5e76\u4e0d\u80fd\u5728\u8fd9\u91cc\u88ab\u8bbf\u95ee }) }; if ( found == arr . end ()) { std :: cout << \"Not found \\n \" ; } else { std :: cout << \"Found \" << * found << '\\n' ; } return 0 ; } \u8fd9\u6bb5\u4ee3\u7801\u65e0\u6cd5\u7f16\u8bd1\u3002\u4e0e\u5d4c\u5957\u5757(\u5728\u5d4c\u5957\u5757\u8303\u56f4\u5185\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u5757\u4e2d\u5b9a\u4e49\u7684\u4efb\u4f55\u6807\u8bc6\u7b26)\u4e0d\u540c\uff0c lambdas\u53ea\u80fd\u8bbf\u95ee\u7279\u5b9a\u7c7b\u578b\u7684\u6807\u8bc6\u7b26\uff1a\u5168\u5c40\u53d8\u91cf\u3001\u7f16\u8bd1\u65f6\u5df2\u77e5\u7684\u5b9e\u4f53\u548c\u5177\u6709 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u7684\u5b9e\u4f53 \u3002 search \u4e0d\u6ee1\u8db3\u8fd9\u4e9b\u8981\u6c42\uff0c\u6240\u4ee5lambda\u65e0\u6cd5\u770b\u5230\u5b83\u3002\u8fd9\u5c31\u662f\u6355\u83b7\u8bed\u53e5\u7684\u4f5c\u7528\u3002 \u95ed\u5305 \u6355\u83b7\u8bed\u53e5\u7528\u4e8e\u4e3a\u5e2e\u52a9 lambda \u8bbf\u95ee\u5728\u5176\u5185\u90e8\u65e0\u6cd5\u8bbf\u95ee\u7684\u666e\u901a\u53d8\u91cf\u3002\u6211\u4eec\u8981\u505a\u7684\u5c31\u662f\u5c06\u9700\u8981\u8bbf\u95ee\u7684\u5b9e\u4f53\u5217\u4e3e\u5728\u6355\u83b7\u8bed\u53e5\u4e2d\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u8ba9lambda\u8bbf\u95ee search \u53d8\u91cf\uff0c\u6240\u4ee5\u5c06\u5176\u52a0\u5165\u6355\u83b7\u8bed\u53e5\u5373\u53ef\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include #include #include #include #include int main () { std :: array < std :: string_view , 4 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" }; std :: cout << \"search for: \" ; std :: string search {}; std :: cin >> search ; // Capture @search vvvvvv auto found { std :: find_if ( arr . begin (), arr . end (), [ search ]( std :: string_view str ) { return ( str . find ( search ) != std :: string_view :: npos ); }) }; if ( found == arr . end ()) { std :: cout << \"Not found \\n \" ; } else { std :: cout << \"Found \" << * found << '\\n' ; } return 0 ; } \u6ce8\u610f\uff0c\u8fd9\u6837\u7528\u6237\u5c31\u53ef\u4ee5\u5728\u6570\u7ec4\u4e2d\u641c\u7d22\u4efb\u4f55\u503c\u4e86\uff1a \u8f93\u51fa\uff1a 1 2 search for: nana Found banana \u95ed\u5305\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f \u867d\u7136\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684lambda\u770b\u8d77\u6765\u50cf\u662f\u76f4\u63a5\u8bbf\u95ee\u4e86main\u51fd\u6570\u4e2d\u7684 search \u53d8\u91cf\uff0c\u4f46\u4e8b\u5b9e\u5e76\u975e\u5982\u6b64\u3002\u867d\u7136 lambda \u53ef\u80fd\u770b\u8d77\u6765\u50cf\u5d4c\u5957\u5757\uff0c\u4f46\u5b83\u4eec\u7684\u5de5\u4f5c\u65b9\u5f0f\u7565\u6709\u4e0d\u540c(\u800c\u4e14\u8fd9\u79cd\u533a\u522b\u5f88\u91cd\u8981)\u3002 \u5f53\u6267\u884clambda\u5b9a\u4e49\u65f6\uff0c\u5bf9\u4e8elambda\u6355\u83b7\u7684\u6bcf\u4e2a\u53d8\u91cf\uff0c\u5c06\u5728lambda\u5185\u90e8\u751f\u6210\u8be5\u53d8\u91cf\u7684\u514b\u9686(\u5177\u6709\u76f8\u540c\u7684\u540d\u79f0)\u3002\u6b64\u65f6\uff0c\u8fd9\u4e9b\u514b\u9686\u53d8\u91cf\u662f\u4ece\u540c\u540d\u7684\u5916\u90e8\u4f5c\u7528\u57df\u53d8\u91cf\u521d\u59cb\u5316\u7684\u3002 \u56e0\u6b64\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53lambda\u5bf9\u8c61\u88ab\u521b\u5efa\u65f6\uff0clambda\u5c06\u83b7\u5f97\u81ea\u5df1\u7684\u514b\u9686\u53d8\u91cf search \u3002\u8fd9\u4e2a\u514b\u9686\u7684 search \u4e0e main \u51fd\u6570\u4e2d\u7684 search \u5177\u6709\u76f8\u540c\u7684\u503c\uff0c\u56e0\u6b64\u5b83\u7684\u884c\u4e3a\u5c31\u50cf\u8bbf\u95ee main \u51fd\u6570\u4e2d\u7684 search \u4e00\u6837\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u3002 \u867d\u7136\u8fd9\u4e9b\u514b\u9686\u7684\u53d8\u91cf\u5177\u6709\u76f8\u540c\u7684\u540d\u79f0\uff0c \u4f46\u5b83\u4eec\u4e0d\u4e00\u5b9a\u5177\u6709\u4e0e\u539f\u59cb\u53d8\u91cf\u76f8\u540c\u7684\u7c7b\u578b \u3002\u6211\u4eec\u5c06\u5728\u672c\u8bfe\u63a5\u4e0b\u6765\u7684\u7ae0\u8282\u4e2d\u63a2\u8ba8\u8fd9\u4e2a\u95ee\u9898\u3002 \u5173\u952e\u4fe1\u606f \u6355\u83b7\u7684\u53d8\u91cf\u662f\u5916\u5c42\u539f\u59cb\u53d8\u91cf\u7684\u4e00\u4efd\u514b\u9686\uff0c\u800c\u4e0d\u662f\u5b9e\u9645\u7684\u539f\u53d8\u91cf\u3002 \u6269\u5c55\u9605\u8bfb \u5c3d\u7ba1 lambda \u770b\u4e0a\u53bb\u50cf\u662f\u51fd\u6570\uff0c\u4f46\u5176\u5b9e\u5e76\u4e0d\u662f\uff0c\u5b83\u53ea\u662f\u53ef\u4ee5\u88ab\u50cf\u51fd\u6570\u4e00\u6837\u8c03\u7528\u7684\u5bf9\u8c61\u800c\u5df2(\u79f0\u4e3a functor \u2014\u2014 \u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u5982\u4f55\u5b9a\u4e49\u81ea\u5df1\u7684functor\u7684)\u3002 \u5f53\u7f16\u8bd1\u5668\u9047\u5230lambda\u5b9a\u4e49\u65f6\uff0c\u5b83\u4e3a\u8be5lambda\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5bf9\u8c61\u5b9a\u4e49\u3002\u6bcf\u4e2a\u6355\u83b7\u7684\u53d8\u91cf\u90fd\u4f1a\u6210\u4e3a\u5bf9\u8c61\u7684\u6570\u636e\u6210\u5458\u3002 \u5728\u8fd0\u884c\u65f6\uff0c\u5f53\u9047\u5230lambda\u5b9a\u4e49\u65f6\uff0c\u5c06\u5b9e\u4f8b\u5316lambda\u5bf9\u8c61\uff0c\u5e76\u5728\u6b64\u65f6\u521d\u59cb\u5316lambda\u7684\u6210\u5458\u3002 \u9ed8\u8ba4\u6355\u83b7\u7684\u662fconst\u503c \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u53d8\u91cf\u88ab\u6355\u83b7\u4e3a const \u503c\u3002\u8fd9\u610f\u5473\u7740\u5728\u521b\u5efalambda\u65f6\uff0clambda\u6355\u83b7\u5916\u90e8\u4f5c\u7528\u57df\u53d8\u91cf\u7684const\u526f\u672c\uff0c\u8fd9\u610f\u5473\u7740\u4e0d\u5141\u8bb8lambda\u4fee\u6539\u5b83\u4eec\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6355\u83b7\u53d8\u91cf ammo \u5e76\u5c1d\u8bd5\u9012\u51cf\u5b83\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include int main () { int ammo { 10 }; // \u5b9a\u4e49\u4e00\u4e2a lambda \u5e76\u5c06\u5176\u5b58\u653e\u5728 \"shoot\" \u4e2d auto shoot { [ ammo ]() { // \u975e\u6cd5\u64cd\u4f5c, ammo \u662fconst\u7684 -- ammo ; std :: cout << \"Pew! \" << ammo << \" shot(s) left. \\n \" ; } }; // Call the lambda shoot (); std :: cout << ammo << \" shot(s) left \\n \" ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7531\u4e8e\u6355\u83b7\u7684 ammo \u662f const \u53d8\u91cf\u3002\u6240\u4ee5\u5e76\u4e0d\u80fd\u88ab\u4fee\u6539\uff0c\u5426\u5219\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002 \u6355\u83b7\u53d8\u91cf\u8bbe\u7f6e\u4e3a\u53ef\u53d8\u503c \u4e3a\u4e86\u5141\u8bb8\u4fee\u6539\u88ab\u6355\u83b7\u7684\u53d8\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06lambda\u6807\u8bb0\u4e3a mutable \u3002\u5728\u6b64\u4e0a\u4e0b\u6587\u4e2d\uff0c mutable \u5173\u952e\u5b57\u4ece\u6309\u503c\u6355\u83b7\u7684\u5168\u90e8\u53d8\u91cf\u4e2d\u5220\u9664 const \u9650\u5b9a\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include int main () { int ammo { 10 }; auto shoot { // \u5728\u53c2\u6570\u5217\u8868\u540e\u9762\u6dfb\u52a0 mutable \u5173\u952e\u5b57 [ ammo ]() mutable { // \u73b0\u5728\u53ef\u4ee5\u4fee\u6539 ammo \u4e86 -- ammo ; std :: cout << \"Pew! \" << ammo << \" shot(s) left. \\n \" ; } }; shoot (); shoot (); std :: cout << ammo << \" shot(s) left \\n \" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 Pew! 9 shot(s) left. Pew! 8 shot(s) left. 10 shot(s) left \u867d\u7136\u4ee3\u7801\u53ef\u4ee5\u7f16\u8bd1\uff0c\u4f46\u662f\u5b58\u5728\u903b\u8f91\u9519\u8bef\u3002\u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\uff1f\u5f53lambda\u88ab\u8c03\u7528\u65f6\uff0c\u5b83\u6355\u83b7\u7684\u662f ammo \u7684\u526f\u672c\uff0c\u5c3d\u7ba1 lambda \u5c06 ammo \u4ece 10 \u9012\u51cf\u5230 9 \u518d\u5230 8 \uff0c\u4f46\u662f\u5b83\u9012\u51cf\u7684\u662f\u5176\u62f7\u8d1d\uff0c\u800c\u4e0d\u662f\u539f\u672c\u7684\u503c\u3002 \u6ce8\u610f\uff0c ammo \u7684\u503c\u4f1a\u5728\u591a\u6b21lambda\u8c03\u7528\u76f4\u63a5\u4fdd\u6301\uff01 \u6ce8\u610f \u56e0\u4e3a\u6355\u83b7\u7684\u53d8\u91cf\u662flambda\u5bf9\u8c61\u7684\u6210\u5458\uff0c\u6240\u4ee5\u5b83\u4eec\u7684\u503c\u8de8\u591a\u6b21\u8c03\u7528lambda! \u6355\u83b7\u53d8\u91cf\u7684\u5f15\u7528 \u5c31\u50cf\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u6765\u66f4\u6539\u53c2\u6570\u7684\u503c\u4e00\u6837\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u6765\u6355\u83b7\u53d8\u91cf\uff0c\u4ee5\u5141\u8bb8lambda\u4fee\u6539\u53c2\u6570\u7684\u503c\u3002 \u8981\u901a\u8fc7\u5f15\u7528\u6355\u83b7\u53d8\u91cf\uff0c\u9700\u8981\u5728\u6355\u83b7\u7684\u53d8\u91cf\u540d\u524d\u52a0\u4e0a & \u53f7\u3002\u4e0e\u901a\u8fc7\u503c\u6355\u83b7\u7684\u53d8\u91cf\u4e0d\u540c\uff0c\u901a\u8fc7\u5f15\u7528\u6355\u83b7\u7684\u53d8\u91cf\u662f\u975econst\u7684\uff0c\u9664\u975e\u5b83\u4eec\u6355\u83b7\u7684\u53d8\u91cf\u662f const \u3002\u5f53\u4f60\u901a\u5e38\u559c\u6b22\u901a\u8fc7\u5f15\u7528\u5c06\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\u65f6(\u4f8b\u5982\uff0c\u5bf9\u4e8e\u975e\u57fa\u672c\u7c7b\u578b)\uff0c\u901a\u8fc7\u5f15\u7528\u6355\u83b7\u5e94\u8be5\u4f18\u5148\u4e8e\u901a\u8fc7\u503c\u6355\u83b7\u3002 \u4fee\u6539\u4e4b\u524d\u7684\u4f8b\u5b50\uff0c\u6355\u83b7 ammo \u7684\u5f15\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int main () { int ammo { 10 }; auto shoot { // We don't need mutable anymore [ & ammo ]() { // &ammo means ammo is captured by reference // Changes to ammo will affect main's ammo -- ammo ; std :: cout << \"Pew! \" << ammo << \" shot(s) left. \\n \" ; } }; shoot (); std :: cout << ammo << \" shot(s) left \\n \" ; return 0 ; } \u7ed3\u679c\u5982\u6211\u4eec\u671f\u671b\u7684\u90a3\u6837\uff1a 1 2 Pew! 9 shot(s) left. 9 shot(s) left \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528\u6309\u5f15\u7528\u6355\u83b7\u6765\u7edf\u8ba1 std::sort \u5bf9\u6570\u7ec4\u6392\u5e8f\u65f6\u8fdb\u884c\u7684\u6bd4\u8f83\u6b21\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include #include #include struct Car { std :: string make {}; std :: string model {}; }; int main () { std :: array < Car , 3 > cars { { { \"Volkswagen\" , \"Golf\" }, { \"Toyota\" , \"Corolla\" }, { \"Honda\" , \"Civic\" } } }; int comparisons { 0 }; std :: sort ( cars . begin (), cars . end (), // Capture @comparisons by reference. [ & comparisons ]( const auto & a , const auto & b ) { // We captured comparisons by reference. We can modify it without \"mutable\". ++ comparisons ; // Sort the cars by their make. return ( a . make < b . make ); }); std :: cout << \"Comparisons: \" << comparisons << '\\n' ; for ( const auto & car : cars ) { std :: cout << car . make << ' ' << car . model << '\\n' ; } return 0 ; } \u53ef\u80fd\u7684\u8f93\u51fa\u662f\uff1a 1 2 3 4 Comparisons: 2 Honda Civic Toyota Corolla Volkswagen Golf \u6355\u83b7\u591a\u4e2a\u53d8\u91cf \u53ef\u4ee5\u901a\u8fc7\u7528\u9017\u53f7\u5206\u9694\u591a\u4e2a\u53d8\u91cf\u6765\u6355\u83b7\u5b83\u4eec\u3002\u8fd9\u5176\u4e2d\u53ef\u4ee5\u5305\u62ec\u901a\u8fc7\u503c\u6216\u5f15\u7528\u6355\u83b7\u7684\u53d8\u91cf\u7684\u7ec4\u5408\uff1a 1 2 3 4 5 6 int health { 33 }; int armor { 100 }; std :: vector < CEnemy > enemies {}; // Capture health and armor by value, and enemies by reference. [ health , armor , & enemies ](){}; \u9ed8\u8ba4\u6355\u83b7 \u5fc5\u987b\u663e\u5f0f\u5730\u5217\u51fa\u60f3\u8981\u6355\u83b7\u7684\u53d8\u91cf\u662f\u5f88\u9ebb\u70e6\u7684\u3002\u5982\u679c\u4fee\u6539lambda\uff0c\u53ef\u80fd\u4f1a\u5fd8\u8bb0\u6dfb\u52a0\u6216\u5220\u9664\u6355\u83b7\u7684\u53d8\u91cf\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u7f16\u8bd1\u5668\u7684\u5e2e\u52a9\u6765\u81ea\u52a8\u751f\u6210\u9700\u8981\u6355\u83b7\u7684\u53d8\u91cf\u5217\u8868\u3002 \u9ed8\u8ba4\u6355\u83b7(\u4e5f\u79f0\u4e3acapture-default)\u6355\u83b7lambda\u4e2d\u63d0\u5230\u7684\u6240\u6709\u53d8\u91cf\uff0c\u4f46\u4e0d\u4f1a\u6355\u83b7lambda\u4e2d\u672a\u63d0\u53ca\u7684\u53d8\u91cf\u3002 \u6309\u503c\u6355\u83b7\u6240\u6709\u4f7f\u7528\u7684\u53d8\u91cf\uff0c\u4f7f\u7528\u6355\u83b7\u503c = \uff1b \u6309\u5f15\u7528\u6355\u83b7\u6240\u6709\u4f7f\u7528\u7684\u53d8\u91cf\uff0c\u8bf7\u4f7f\u7528\u6355\u83b7\u503c & \u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u9ed8\u8ba4\u503c\u6355\u83b7\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include #include #include int main () { std :: array areas { 100 , 25 , 121 , 40 , 56 }; int width {}; int height {}; std :: cout << \"Enter width and height: \" ; std :: cin >> width >> height ; auto found { std :: find_if ( areas . begin (), areas . end (), [ = ]( int knownArea ) { // will default capture width and height by value return ( width * height == knownArea ); // because they're mentioned here }) }; if ( found == areas . end ()) { std :: cout << \"I don't know this area :( \\n \" ; } else { std :: cout << \"Area found :) \\n \" ; } return 0 ; } \u9ed8\u8ba4\u6355\u83b7\u53ef\u4ee5\u4e0e\u666e\u901a\u6355\u83b7\u6df7\u5408\u4f7f\u7528\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u503c\u6355\u83b7\u4e00\u4e9b\u53d8\u91cf\uff0c\u901a\u8fc7\u5f15\u7528\u6355\u83b7\u53e6\u4e00\u4e9b\u53d8\u91cf\uff0c\u4f46\u662f\u6bcf\u4e2a\u53d8\u91cf\u53ea\u80fd\u6355\u83b7\u4e00\u6b21\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 int health { 33 }; int armor { 100 }; std :: vector < CEnemy > enemies {}; // \u6309\u503c\u6355\u83b7 health \u548c armor \uff0c\u6309\u5f15\u7528\u6355\u83b7 enemies [ health , armor , & enemies ](){}; // \u6309\u5f15\u7528\u6355\u83b7 enemies\uff0c\u5269\u4e0b\u7684\u90fd\u6309\u503c\u6355\u83b7 [ = , & enemies ](){}; // \u6309\u503c\u6355\u83b7 armor\uff0c\u5269\u4e0b\u7684\u90fd\u6309\u5f15\u7528\u6355\u83b7 [ & , armor ](){}; // \u9519\u8bef\uff1a\u5df2\u7ecf\u8868\u660e\u6309\u5f15\u7528\u6355\u83b7\u6240\u6709\u53d8\u91cf [ & , & armor ](){}; // \u9519\u8bef\uff1a\u5df2\u7ecf\u8868\u660e\u6309\u503c\u6355\u83b7\u6240\u6709\u53d8\u91cf [ = , armor ](){}; // \u9519\u8bef\uff1aarmor \u51fa\u73b0\u4e86\u4e24\u6b21 [ armor , & health , & armor ](){}; // \u9519\u8bef\uff1a\u9ed8\u8ba4\u6355\u83b7\u5fc5\u987b\u653e\u5728\u6700\u524d\u9762 [ armor , & ](){}; \u5728\u95ed\u5305\u4e2d\u5b9a\u4e49\u65b0\u7684\u53d8\u91cf \u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5bf9\u6355\u83b7\u7684\u53d8\u91cf\u8fdb\u884c\u7a0d\u8bb8\u4fee\u6539\uff0c\u6216\u8005\u662f\u58f0\u660e\u4e00\u4e2a\u4ec5\u5728 lambda \u4f5c\u7528\u57df\u4e2d\u53ef\u89c1\u7684\u53d8\u91cf\uff0c\u6b64\u65f6\u6211\u4eec\u53ef\u4ee5\u5728\u95ed\u5305\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\u800c\u4e14\u4e0d\u5fc5\u6307\u660e\u5176\u7c7b\u578b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include #include #include int main () { std :: array areas { 100 , 25 , 121 , 40 , 56 }; int width {}; int height {}; std :: cout << \"Enter width and height: \" ; std :: cin >> width >> height ; // \u6211\u4eec\u9700\u8981\u9762\u79ef\uff0c\u4f46\u662f\u7528\u6237\u8f93\u5165\u7684\u662f\u957f\u5bbd\u3002 // \u6240\u4ee5\u9700\u8981\u5728\u641c\u7d22\u9762\u79ef\u524d\u9996\u5148\u8ba1\u7b97\u9762\u79ef auto found { std :: find_if ( areas . begin (), areas . end (), // \u58f0\u660e\u4e00\u4e2a\u4ec5 lambda \u53ef\u89c1\u7684\u53d8\u91cf // userArea \u4f1a\u88ab\u81ea\u52a8\u63a8\u65ad\u4e3a int. [ userArea { width * height }]( int knownArea ) { return ( userArea == knownArea ); }) }; if ( found == areas . end ()) { std :: cout << \"I don't know this area :( \\n \" ; } else { std :: cout << \"Area found :) \\n \" ; } return 0 ; } \u5f53lambda\u88ab\u5b9a\u4e49\u65f6\uff0c userArea \u53ea\u4f1a\u88ab\u8ba1\u7b97\u4e00\u6b21\u3002\u8ba1\u7b97\u7684\u9762\u79ef\u5b58\u50a8\u5728lambda\u5bf9\u8c61\u4e2d\uff0c\u6bcf\u6b21\u8c03\u7528\u90fd\u662f\u76f8\u540c\u7684\u3002\u5982\u679clambda\u662f\u53ef\u53d8\u7684\uff0c\u5e76\u4e14\u4fee\u6539\u4e86\u6355\u83b7\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u5219\u539f\u59cb\u503c\u5c06\u88ab\u8986\u76d6\u3002 \u6700\u4f73\u5b9e\u8df5 \u53ea\u6709\u5f53\u53d8\u91cf\u7684\u5b58\u5728\u65f6\u95f4\u5f88\u77ed\u4e14\u7c7b\u578b\u5f88\u660e\u663e\u65f6\uff0c\u624d\u5728\u6355\u83b7\u4e2d\u7684\u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u53d8\u91cf\u3002\u5426\u5219\uff0c\u6700\u597d\u5728lambda\u4e4b\u5916\u5b9a\u4e49\u53d8\u91cf\u5e76\u6355\u83b7\u5b83\u3002 \u60ac\u5782\u7684\u6355\u83b7\u53d8\u91cf \u53d8\u91cf\u5728\u5b9a\u4e49lambda\u7684\u5730\u65b9\u88ab\u6355\u83b7\u3002\u5982\u679c\u5f15\u7528\u6355\u83b7\u7684\u53d8\u91cf\u5728lambda\u4e4b\u524d\u88ab\u9500\u6bc1\uff0c\u5219lambda\u5c06\u5f97\u5230\u4e00\u4e2a \u60ac\u5782 \u5f15\u7528\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include // \u8fd4\u56de\u4e00\u4e2a lambda auto makeWalrus ( const std :: string & name ) { // \u6309\u5f15\u7528\u6355\u83b7 name \u5e76\u8fd4\u56de\u4e00\u4e2alambda return [ & ]() { std :: cout << \"I am a walrus, my name is \" << name << '\\n' ; // \u672a\u5b9a\u4e49\u884c\u4e3a }; } int main () { // \u521b\u5efa\u4e00\u4e2a\u540d\u4e3aroofus\u7684\u65b0\u7684 walrus // sayName \u662f makeWalrus \u8fd4\u56de\u7684 lambda auto sayName { makeWalrus ( \"Roofus\" ) }; // \u8c03\u7528 makeWalrus \u8fd4\u56de\u7684 lambda sayName (); return 0 ; } \u8c03\u7528 makeWalrus \u4f1a\u4ece\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u201cRoofus\u201d\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u7684 std::string \u3002 makeWalrus \u4e2d\u7684lambda\u4f1a\u6309\u5f15\u7528\u6355\u83b7\u8be5\u5b57\u7b26\u4e32\u3002\u5f53 makeWalrus \u8fd4\u56de\u65f6\uff0c\u8be5\u5b57\u7b26\u4e32\u4f1a\u9500\u6bc1\uff0c\u4f46\u662f lambda \u4ecd\u7136\u4f1a\u4f7f\u7528\u5b83\u7684\u5f15\u7528\u3002\u4e8e\u662f\u5f53\u6211\u4eec\u8c03\u7528 sayName \u65f6\uff0c\u5c31\u4f1a\u8bbf\u95ee\u8be5\u60ac\u5782\u5f15\u7528\uff0c\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u6ce8\u610f\uff0c\u5373\u4f7f name \u662f \u6309\u503c\u4f20\u9012 \u7ed9 makeWalrus \u7684\u3002\u53d8\u91cf name \u4ecd\u7136\u4f1a\u5728 makeWalrus \u7ed3\u675f\u65f6\u9500\u6bc1\uff0clambda\u6301\u6709\u7684\u4ecd\u7136\u662f\u60ac\u5782\u5f15\u7528\u3002 \u6ce8\u610f \u5728\u901a\u8fc7\u5f15\u7528\u6355\u83b7\u53d8\u91cf\u65f6\u8981\u683c\u5916\u5c0f\u5fc3\uff0c\u7279\u522b\u662f\u4f7f\u7528\u9ed8\u8ba4\u5f15\u7528\u6355\u83b7\u65f6\u3002\u6355\u83b7\u7684\u53d8\u91cf\u5fc5\u987b\u6bd4lambda\u5b58\u6d3b\u7684\u65f6\u95f4\u957f\u3002 \u5982\u679c\u6211\u4eec\u60f3\u8981\u5728\u4f7f\u7528lambda\u65f6\u4ecd\u7136\u4fdd\u8bc1 name \u53ef\u7528\uff0c\u5219\u9700\u8981\u5728\u95ed\u5305\u4e2d\u6309\u503c\u6355\u83b7\u8be5\u53d8\u91cf\uff08\u663e\u5f0f\u6307\u5b9a\u6216\u9ed8\u8ba4\u6309\u503c\u6355\u83b7\uff09\u3002 \u65e0\u610f\u95f4\u62f7\u8d1d\u53ef\u53d8\u7684 lambda \u56e0\u4e3alambda\u662f\u5bf9\u8c61\uff0c\u6240\u4ee5\u5b83\u4eec\u53ef\u4ee5\u88ab\u590d\u5236\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u95ee\u9898\u3002\u8003\u8651\u4ee5\u4e0b\u4ee3\u7801: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int main () { int i { 0 }; // Create a new lambda named count auto count { [ i ]() mutable { std :: cout << ++ i << '\\n' ; } }; count (); // invoke count auto otherCount { count }; // create a copy of count // invoke both count and the copy count (); otherCount (); return 0 ; } \u8f93\u51fa\uff1a 1 2 3 1 2 2 \u7a0b\u5e8f\u6ca1\u6709\u6253\u5370 1, 2, 3\uff0c\u800c\u662f\u6253\u5370\u4e86\u4e24\u4e2a2\u3002\u6211\u4eec\u521b\u5efa otherCount \u65f6\u62f7\u8d1d\u4e86 count \uff0c\u540c\u65f6\u4e5f\u62f7\u8d1d\u4e86 count \u5f53\u524d\u7684\u72b6\u6001\u3002 count \u7684 i \u662f 1\uff0c\u6240\u4ee5 otherCount \u7684 i \u4e5f\u662f 1\u3002\u56e0\u4e3a otherCount \u662f count \u7684\u62f7\u8d1d\uff0c\u6240\u4ee5\u5b83\u4eec\u6709\u5404\u81ea\u7684 i \u3002 \u518d\u770b\u4e00\u4e2a\u4e0d\u592a\u660e\u663e\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include void myInvoke ( const std :: function < void () >& fn ) { fn (); } int main () { int i { 0 }; // Increments and prints its local copy of @i. auto count { [ i ]() mutable { std :: cout << ++ i << '\\n' ; } }; myInvoke ( count ); myInvoke ( count ); myInvoke ( count ); return 0 ; } \u8f93\u51fa\uff1a 1 2 3 1 1 1 \u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u5b58\u5728\u76f8\u540c\u7684\u95ee\u9898\uff0c\u4f46\u662f\u66f4\u52a0\u9690\u853d\u3002\u5f53\u901a\u8fc7lambda\u521b\u5efa std::function \u65f6\uff0c std::function \u4f1a\u521b\u5efa lambda \u7684\u62f7\u8d1d\u3002\u56e0\u6b64\u5728\u8c03\u7528 fn() \u65f6\uff0c\u6267\u884c\u7684\u5176\u5b9e\u662flambda \u7684\u62f7\u8d1d\u800c\u4e0d\u662f\u5176\u672c\u8eab\u3002 \u5982\u679c\u6211\u4eec\u9700\u8981\u4f20\u9012\u4e00\u4e2a\u53ef\u53d8\u7684lambda\uff0c\u5e76\u4e14\u60f3\u8981\u907f\u514d\u65e0\u610f\u5730\u590d\u5236\uff0c\u6709\u4e24\u4e2a\u65b9\u6cd5\u3002\u4e00\u79cd\u662f\u4f7f\u7528\u975e\u6355\u83b7lambda\u6765\u4ee3\u66ff\u2014\u2014\u5728\u4e0a\u9762\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5220\u9664\u6355\u83b7\u5e76\u4f7f\u7528\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u6765\u8ddf\u8e2a\u72b6\u6001\u3002\u4f46\u662f\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u5f88\u96be\u8ddf\u8e2a\uff0c\u5e76\u4e14\u4f7f\u6211\u4eec\u7684\u4ee3\u7801\u53ef\u8bfb\u6027\u8f83\u5dee\u3002\u4e00\u4e2a\u66f4\u597d\u7684\u9009\u62e9\u662f\u4ece\u4e00\u5f00\u59cb\u5c31\u963b\u6b62lambda\u7684\u590d\u5236\u3002\u4f46\u662f\u7531\u4e8e\u6211\u4eec\u4e0d\u80fd\u5f71\u54cd std::function (\u6216\u5176\u4ed6\u6807\u51c6\u5e93\u51fd\u6570\u6216\u5bf9\u8c61)\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u90a3\u8981\u5982\u4f55\u624d\u80fd\u505a\u5230\u8fd9\u4e00\u70b9\u5462? \u5e78\u8fd0\u7684\u662f\uff0cC++ \u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u52a0\u65b9\u4fbf\u7684\u7c7b\u578b(\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d) \u2014\u2014\u79f0\u4e3a std::reference_wrapper \u3002\u8be5\u7c7b\u578b\u4f7f\u6211\u4eec\u53ef\u4ee5\u4f20\u5165\u4e00\u4e2a\u666e\u901a\u7c7b\u578b\uff0c\u4f46\u5f53\u505a\u5f15\u7528\u4f7f\u7528\u3002\u66f4\u597d\u5730\u662f\uff0c std::reference_wrapper \u53ef\u4ee5\u901a\u8fc7 std::ref() \u51fd\u6570\u521b\u5efa\u3002\u5982\u679c\u6211\u4eec\u5c06 lambda \u5305\u88c5\u5230 std::reference_wrapper \u4e2d\uff0c\u5219\u6bcf\u5f53\u6709\u4eba\u60f3\u62f7\u8d1d lambda \u65f6\uff0c\u5b83\u4eec\u4f1a\u62f7\u8d1d\u4e00\u4e2a\u5f15\u7528\u800c\u4e0d\u662f\u5bf9\u8c61\u672c\u8eab\u3002 \u4f7f\u7528 std::ref \u66f4\u65b0\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include void myInvoke ( const std :: function < void () >& fn ) { fn (); } int main () { int i { 0 }; // Increments and prints its local copy of @i. auto count { [ i ]() mutable { std :: cout << ++ i << '\\n' ; } }; // std::ref(count) \u4f7f\u5f97 count \u88ab\u5f53\u505a\u5f15\u7528\u5bf9\u5f85 // \u56e0\u6b64, \u4efb\u4f55\u5bf9\u5176\u7684\u62f7\u8d1d\u90fd\u5b9e\u9645\u62f7\u8d1d\u7684\u662f\u5f15\u7528\uff0c\u786e\u4fdd\u59cb\u7ec8\u53ea\u5b58\u5728\u4e00\u4e2a count myInvoke ( std :: ref ( count )); myInvoke ( std :: ref ( count )); myInvoke ( std :: ref ( count )); return 0 ; } \u8f93\u51fa\u7b26\u5408\u9884\u671f\uff1a 1 2 3 1 2 3 \u6ce8\u610f\uff0c invoke \u6309\u503c\u83b7\u53d6 fn \u7ed3\u679c\u4e5f\u662f\u4e00\u6837\u7684\u3002\u5982\u679c\u6211\u4eec\u901a\u8fc7 std::ref \u521b\u5efa\u5b83\uff0c\u5219 std::function \u4e0d\u4f1a\u521b\u5efa lambda \u7684\u526f\u672c\u3002 \u6cd5\u5219 \u6807\u51c6\u5e93\u51fd\u6570\u53ef\u80fd\u4f1a\u590d\u5236\u51fd\u6570\u5bf9\u8c61(\u63d0\u9192:lambda\u662f\u51fd\u6570\u5bf9\u8c61)\u3002\u5982\u679c\u4f60\u60f3\u63d0\u4f9b\u5e26\u6709\u53ef\u53d8\u6355\u83b7\u53d8\u91cf\u7684lambda\uff0c\u53ef\u4ee5\u4f7f\u7528 std::ref \u901a\u8fc7\u5f15\u7528\u4f20\u9012\u5b83\u4eec\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c3d\u91cf\u907f\u514d\u53ef\u53d8\u7684lambda\u3002\u4e0d\u53ef\u53d8lambda\u66f4\u5bb9\u6613\u7406\u89e3\uff0c\u4e0d\u4f1a\u51fa\u73b0\u4e0a\u8ff0\u95ee\u9898\uff0c\u4e5f\u4e0d\u4f1a\u5728\u6dfb\u52a0\u5e76\u884c\u6267\u884c\u65f6\u51fa\u73b0\u66f4\u5371\u9669\u7684\u95ee\u9898\u3002","title":"12.8 - lambda \u95ed\u5305"},{"location":"12-8-lambda-captures/#_1","text":"\u5728\u4e0a\u8282\u8bfe 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb ) \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u6574\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include #include #include int main () { std :: array < std :: string_view , 4 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" }; auto found { std :: find_if ( arr . begin (), arr . end (), []( std :: string_view str ) { return ( str . find ( \"nut\" ) != std :: string_view :: npos ); }) }; if ( found == arr . end ()) { std :: cout << \"No nuts \\n \" ; } else { std :: cout << \"Found \" << * found << '\\n' ; } return 0 ; } \u63a5\u4e0b\u6765\uff0c\u4fee\u6539\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u8ba9\u7528\u6237\u51b3\u5b9a\u9700\u8981\u641c\u7d22\u7684\u5b50\u4e32\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u5e76\u4e0d\u60f3\u4f60\u60f3\u8c61\u7684\u90a3\u4e48\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include #include #include #include int main () { std :: array < std :: string_view , 4 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" }; // Ask the user what to search for. std :: cout << \"search for: \" ; std :: string search {}; std :: cin >> search ; auto found { std :: find_if ( arr . begin (), arr . end (), []( std :: string_view str ) { // \u641c\u7d22 @search \u800c\u4e0d\u662f \"nut\". return ( str . find ( search ) != std :: string_view :: npos ); // \u9519\u8bef: search \u5e76\u4e0d\u80fd\u5728\u8fd9\u91cc\u88ab\u8bbf\u95ee }) }; if ( found == arr . end ()) { std :: cout << \"Not found \\n \" ; } else { std :: cout << \"Found \" << * found << '\\n' ; } return 0 ; } \u8fd9\u6bb5\u4ee3\u7801\u65e0\u6cd5\u7f16\u8bd1\u3002\u4e0e\u5d4c\u5957\u5757(\u5728\u5d4c\u5957\u5757\u8303\u56f4\u5185\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u5757\u4e2d\u5b9a\u4e49\u7684\u4efb\u4f55\u6807\u8bc6\u7b26)\u4e0d\u540c\uff0c lambdas\u53ea\u80fd\u8bbf\u95ee\u7279\u5b9a\u7c7b\u578b\u7684\u6807\u8bc6\u7b26\uff1a\u5168\u5c40\u53d8\u91cf\u3001\u7f16\u8bd1\u65f6\u5df2\u77e5\u7684\u5b9e\u4f53\u548c\u5177\u6709 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u7684\u5b9e\u4f53 \u3002 search \u4e0d\u6ee1\u8db3\u8fd9\u4e9b\u8981\u6c42\uff0c\u6240\u4ee5lambda\u65e0\u6cd5\u770b\u5230\u5b83\u3002\u8fd9\u5c31\u662f\u6355\u83b7\u8bed\u53e5\u7684\u4f5c\u7528\u3002","title":"\u6355\u83b7\u8bed\u53e5\u4ee5\u53ca\u6355\u83b7\u503c"},{"location":"12-8-lambda-captures/#_2","text":"\u6355\u83b7\u8bed\u53e5\u7528\u4e8e\u4e3a\u5e2e\u52a9 lambda \u8bbf\u95ee\u5728\u5176\u5185\u90e8\u65e0\u6cd5\u8bbf\u95ee\u7684\u666e\u901a\u53d8\u91cf\u3002\u6211\u4eec\u8981\u505a\u7684\u5c31\u662f\u5c06\u9700\u8981\u8bbf\u95ee\u7684\u5b9e\u4f53\u5217\u4e3e\u5728\u6355\u83b7\u8bed\u53e5\u4e2d\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u8ba9lambda\u8bbf\u95ee search \u53d8\u91cf\uff0c\u6240\u4ee5\u5c06\u5176\u52a0\u5165\u6355\u83b7\u8bed\u53e5\u5373\u53ef\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include #include #include #include #include int main () { std :: array < std :: string_view , 4 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" }; std :: cout << \"search for: \" ; std :: string search {}; std :: cin >> search ; // Capture @search vvvvvv auto found { std :: find_if ( arr . begin (), arr . end (), [ search ]( std :: string_view str ) { return ( str . find ( search ) != std :: string_view :: npos ); }) }; if ( found == arr . end ()) { std :: cout << \"Not found \\n \" ; } else { std :: cout << \"Found \" << * found << '\\n' ; } return 0 ; } \u6ce8\u610f\uff0c\u8fd9\u6837\u7528\u6237\u5c31\u53ef\u4ee5\u5728\u6570\u7ec4\u4e2d\u641c\u7d22\u4efb\u4f55\u503c\u4e86\uff1a \u8f93\u51fa\uff1a 1 2 search for: nana Found banana","title":"\u95ed\u5305"},{"location":"12-8-lambda-captures/#_3","text":"\u867d\u7136\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684lambda\u770b\u8d77\u6765\u50cf\u662f\u76f4\u63a5\u8bbf\u95ee\u4e86main\u51fd\u6570\u4e2d\u7684 search \u53d8\u91cf\uff0c\u4f46\u4e8b\u5b9e\u5e76\u975e\u5982\u6b64\u3002\u867d\u7136 lambda \u53ef\u80fd\u770b\u8d77\u6765\u50cf\u5d4c\u5957\u5757\uff0c\u4f46\u5b83\u4eec\u7684\u5de5\u4f5c\u65b9\u5f0f\u7565\u6709\u4e0d\u540c(\u800c\u4e14\u8fd9\u79cd\u533a\u522b\u5f88\u91cd\u8981)\u3002 \u5f53\u6267\u884clambda\u5b9a\u4e49\u65f6\uff0c\u5bf9\u4e8elambda\u6355\u83b7\u7684\u6bcf\u4e2a\u53d8\u91cf\uff0c\u5c06\u5728lambda\u5185\u90e8\u751f\u6210\u8be5\u53d8\u91cf\u7684\u514b\u9686(\u5177\u6709\u76f8\u540c\u7684\u540d\u79f0)\u3002\u6b64\u65f6\uff0c\u8fd9\u4e9b\u514b\u9686\u53d8\u91cf\u662f\u4ece\u540c\u540d\u7684\u5916\u90e8\u4f5c\u7528\u57df\u53d8\u91cf\u521d\u59cb\u5316\u7684\u3002 \u56e0\u6b64\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53lambda\u5bf9\u8c61\u88ab\u521b\u5efa\u65f6\uff0clambda\u5c06\u83b7\u5f97\u81ea\u5df1\u7684\u514b\u9686\u53d8\u91cf search \u3002\u8fd9\u4e2a\u514b\u9686\u7684 search \u4e0e main \u51fd\u6570\u4e2d\u7684 search \u5177\u6709\u76f8\u540c\u7684\u503c\uff0c\u56e0\u6b64\u5b83\u7684\u884c\u4e3a\u5c31\u50cf\u8bbf\u95ee main \u51fd\u6570\u4e2d\u7684 search \u4e00\u6837\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u3002 \u867d\u7136\u8fd9\u4e9b\u514b\u9686\u7684\u53d8\u91cf\u5177\u6709\u76f8\u540c\u7684\u540d\u79f0\uff0c \u4f46\u5b83\u4eec\u4e0d\u4e00\u5b9a\u5177\u6709\u4e0e\u539f\u59cb\u53d8\u91cf\u76f8\u540c\u7684\u7c7b\u578b \u3002\u6211\u4eec\u5c06\u5728\u672c\u8bfe\u63a5\u4e0b\u6765\u7684\u7ae0\u8282\u4e2d\u63a2\u8ba8\u8fd9\u4e2a\u95ee\u9898\u3002 \u5173\u952e\u4fe1\u606f \u6355\u83b7\u7684\u53d8\u91cf\u662f\u5916\u5c42\u539f\u59cb\u53d8\u91cf\u7684\u4e00\u4efd\u514b\u9686\uff0c\u800c\u4e0d\u662f\u5b9e\u9645\u7684\u539f\u53d8\u91cf\u3002 \u6269\u5c55\u9605\u8bfb \u5c3d\u7ba1 lambda \u770b\u4e0a\u53bb\u50cf\u662f\u51fd\u6570\uff0c\u4f46\u5176\u5b9e\u5e76\u4e0d\u662f\uff0c\u5b83\u53ea\u662f\u53ef\u4ee5\u88ab\u50cf\u51fd\u6570\u4e00\u6837\u8c03\u7528\u7684\u5bf9\u8c61\u800c\u5df2(\u79f0\u4e3a functor \u2014\u2014 \u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u5982\u4f55\u5b9a\u4e49\u81ea\u5df1\u7684functor\u7684)\u3002 \u5f53\u7f16\u8bd1\u5668\u9047\u5230lambda\u5b9a\u4e49\u65f6\uff0c\u5b83\u4e3a\u8be5lambda\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5bf9\u8c61\u5b9a\u4e49\u3002\u6bcf\u4e2a\u6355\u83b7\u7684\u53d8\u91cf\u90fd\u4f1a\u6210\u4e3a\u5bf9\u8c61\u7684\u6570\u636e\u6210\u5458\u3002 \u5728\u8fd0\u884c\u65f6\uff0c\u5f53\u9047\u5230lambda\u5b9a\u4e49\u65f6\uff0c\u5c06\u5b9e\u4f8b\u5316lambda\u5bf9\u8c61\uff0c\u5e76\u5728\u6b64\u65f6\u521d\u59cb\u5316lambda\u7684\u6210\u5458\u3002","title":"\u95ed\u5305\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f"},{"location":"12-8-lambda-captures/#const","text":"\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u53d8\u91cf\u88ab\u6355\u83b7\u4e3a const \u503c\u3002\u8fd9\u610f\u5473\u7740\u5728\u521b\u5efalambda\u65f6\uff0clambda\u6355\u83b7\u5916\u90e8\u4f5c\u7528\u57df\u53d8\u91cf\u7684const\u526f\u672c\uff0c\u8fd9\u610f\u5473\u7740\u4e0d\u5141\u8bb8lambda\u4fee\u6539\u5b83\u4eec\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6355\u83b7\u53d8\u91cf ammo \u5e76\u5c1d\u8bd5\u9012\u51cf\u5b83\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include int main () { int ammo { 10 }; // \u5b9a\u4e49\u4e00\u4e2a lambda \u5e76\u5c06\u5176\u5b58\u653e\u5728 \"shoot\" \u4e2d auto shoot { [ ammo ]() { // \u975e\u6cd5\u64cd\u4f5c, ammo \u662fconst\u7684 -- ammo ; std :: cout << \"Pew! \" << ammo << \" shot(s) left. \\n \" ; } }; // Call the lambda shoot (); std :: cout << ammo << \" shot(s) left \\n \" ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7531\u4e8e\u6355\u83b7\u7684 ammo \u662f const \u53d8\u91cf\u3002\u6240\u4ee5\u5e76\u4e0d\u80fd\u88ab\u4fee\u6539\uff0c\u5426\u5219\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002","title":"\u9ed8\u8ba4\u6355\u83b7\u7684\u662fconst\u503c"},{"location":"12-8-lambda-captures/#_4","text":"\u4e3a\u4e86\u5141\u8bb8\u4fee\u6539\u88ab\u6355\u83b7\u7684\u53d8\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06lambda\u6807\u8bb0\u4e3a mutable \u3002\u5728\u6b64\u4e0a\u4e0b\u6587\u4e2d\uff0c mutable \u5173\u952e\u5b57\u4ece\u6309\u503c\u6355\u83b7\u7684\u5168\u90e8\u53d8\u91cf\u4e2d\u5220\u9664 const \u9650\u5b9a\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include int main () { int ammo { 10 }; auto shoot { // \u5728\u53c2\u6570\u5217\u8868\u540e\u9762\u6dfb\u52a0 mutable \u5173\u952e\u5b57 [ ammo ]() mutable { // \u73b0\u5728\u53ef\u4ee5\u4fee\u6539 ammo \u4e86 -- ammo ; std :: cout << \"Pew! \" << ammo << \" shot(s) left. \\n \" ; } }; shoot (); shoot (); std :: cout << ammo << \" shot(s) left \\n \" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 Pew! 9 shot(s) left. Pew! 8 shot(s) left. 10 shot(s) left \u867d\u7136\u4ee3\u7801\u53ef\u4ee5\u7f16\u8bd1\uff0c\u4f46\u662f\u5b58\u5728\u903b\u8f91\u9519\u8bef\u3002\u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\uff1f\u5f53lambda\u88ab\u8c03\u7528\u65f6\uff0c\u5b83\u6355\u83b7\u7684\u662f ammo \u7684\u526f\u672c\uff0c\u5c3d\u7ba1 lambda \u5c06 ammo \u4ece 10 \u9012\u51cf\u5230 9 \u518d\u5230 8 \uff0c\u4f46\u662f\u5b83\u9012\u51cf\u7684\u662f\u5176\u62f7\u8d1d\uff0c\u800c\u4e0d\u662f\u539f\u672c\u7684\u503c\u3002 \u6ce8\u610f\uff0c ammo \u7684\u503c\u4f1a\u5728\u591a\u6b21lambda\u8c03\u7528\u76f4\u63a5\u4fdd\u6301\uff01 \u6ce8\u610f \u56e0\u4e3a\u6355\u83b7\u7684\u53d8\u91cf\u662flambda\u5bf9\u8c61\u7684\u6210\u5458\uff0c\u6240\u4ee5\u5b83\u4eec\u7684\u503c\u8de8\u591a\u6b21\u8c03\u7528lambda!","title":"\u6355\u83b7\u53d8\u91cf\u8bbe\u7f6e\u4e3a\u53ef\u53d8\u503c"},{"location":"12-8-lambda-captures/#_5","text":"\u5c31\u50cf\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u6765\u66f4\u6539\u53c2\u6570\u7684\u503c\u4e00\u6837\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u6765\u6355\u83b7\u53d8\u91cf\uff0c\u4ee5\u5141\u8bb8lambda\u4fee\u6539\u53c2\u6570\u7684\u503c\u3002 \u8981\u901a\u8fc7\u5f15\u7528\u6355\u83b7\u53d8\u91cf\uff0c\u9700\u8981\u5728\u6355\u83b7\u7684\u53d8\u91cf\u540d\u524d\u52a0\u4e0a & \u53f7\u3002\u4e0e\u901a\u8fc7\u503c\u6355\u83b7\u7684\u53d8\u91cf\u4e0d\u540c\uff0c\u901a\u8fc7\u5f15\u7528\u6355\u83b7\u7684\u53d8\u91cf\u662f\u975econst\u7684\uff0c\u9664\u975e\u5b83\u4eec\u6355\u83b7\u7684\u53d8\u91cf\u662f const \u3002\u5f53\u4f60\u901a\u5e38\u559c\u6b22\u901a\u8fc7\u5f15\u7528\u5c06\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\u65f6(\u4f8b\u5982\uff0c\u5bf9\u4e8e\u975e\u57fa\u672c\u7c7b\u578b)\uff0c\u901a\u8fc7\u5f15\u7528\u6355\u83b7\u5e94\u8be5\u4f18\u5148\u4e8e\u901a\u8fc7\u503c\u6355\u83b7\u3002 \u4fee\u6539\u4e4b\u524d\u7684\u4f8b\u5b50\uff0c\u6355\u83b7 ammo \u7684\u5f15\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int main () { int ammo { 10 }; auto shoot { // We don't need mutable anymore [ & ammo ]() { // &ammo means ammo is captured by reference // Changes to ammo will affect main's ammo -- ammo ; std :: cout << \"Pew! \" << ammo << \" shot(s) left. \\n \" ; } }; shoot (); std :: cout << ammo << \" shot(s) left \\n \" ; return 0 ; } \u7ed3\u679c\u5982\u6211\u4eec\u671f\u671b\u7684\u90a3\u6837\uff1a 1 2 Pew! 9 shot(s) left. 9 shot(s) left \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528\u6309\u5f15\u7528\u6355\u83b7\u6765\u7edf\u8ba1 std::sort \u5bf9\u6570\u7ec4\u6392\u5e8f\u65f6\u8fdb\u884c\u7684\u6bd4\u8f83\u6b21\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include #include #include struct Car { std :: string make {}; std :: string model {}; }; int main () { std :: array < Car , 3 > cars { { { \"Volkswagen\" , \"Golf\" }, { \"Toyota\" , \"Corolla\" }, { \"Honda\" , \"Civic\" } } }; int comparisons { 0 }; std :: sort ( cars . begin (), cars . end (), // Capture @comparisons by reference. [ & comparisons ]( const auto & a , const auto & b ) { // We captured comparisons by reference. We can modify it without \"mutable\". ++ comparisons ; // Sort the cars by their make. return ( a . make < b . make ); }); std :: cout << \"Comparisons: \" << comparisons << '\\n' ; for ( const auto & car : cars ) { std :: cout << car . make << ' ' << car . model << '\\n' ; } return 0 ; } \u53ef\u80fd\u7684\u8f93\u51fa\u662f\uff1a 1 2 3 4 Comparisons: 2 Honda Civic Toyota Corolla Volkswagen Golf","title":"\u6355\u83b7\u53d8\u91cf\u7684\u5f15\u7528"},{"location":"12-8-lambda-captures/#_6","text":"\u53ef\u4ee5\u901a\u8fc7\u7528\u9017\u53f7\u5206\u9694\u591a\u4e2a\u53d8\u91cf\u6765\u6355\u83b7\u5b83\u4eec\u3002\u8fd9\u5176\u4e2d\u53ef\u4ee5\u5305\u62ec\u901a\u8fc7\u503c\u6216\u5f15\u7528\u6355\u83b7\u7684\u53d8\u91cf\u7684\u7ec4\u5408\uff1a 1 2 3 4 5 6 int health { 33 }; int armor { 100 }; std :: vector < CEnemy > enemies {}; // Capture health and armor by value, and enemies by reference. [ health , armor , & enemies ](){};","title":"\u6355\u83b7\u591a\u4e2a\u53d8\u91cf"},{"location":"12-8-lambda-captures/#_7","text":"\u5fc5\u987b\u663e\u5f0f\u5730\u5217\u51fa\u60f3\u8981\u6355\u83b7\u7684\u53d8\u91cf\u662f\u5f88\u9ebb\u70e6\u7684\u3002\u5982\u679c\u4fee\u6539lambda\uff0c\u53ef\u80fd\u4f1a\u5fd8\u8bb0\u6dfb\u52a0\u6216\u5220\u9664\u6355\u83b7\u7684\u53d8\u91cf\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u7f16\u8bd1\u5668\u7684\u5e2e\u52a9\u6765\u81ea\u52a8\u751f\u6210\u9700\u8981\u6355\u83b7\u7684\u53d8\u91cf\u5217\u8868\u3002 \u9ed8\u8ba4\u6355\u83b7(\u4e5f\u79f0\u4e3acapture-default)\u6355\u83b7lambda\u4e2d\u63d0\u5230\u7684\u6240\u6709\u53d8\u91cf\uff0c\u4f46\u4e0d\u4f1a\u6355\u83b7lambda\u4e2d\u672a\u63d0\u53ca\u7684\u53d8\u91cf\u3002 \u6309\u503c\u6355\u83b7\u6240\u6709\u4f7f\u7528\u7684\u53d8\u91cf\uff0c\u4f7f\u7528\u6355\u83b7\u503c = \uff1b \u6309\u5f15\u7528\u6355\u83b7\u6240\u6709\u4f7f\u7528\u7684\u53d8\u91cf\uff0c\u8bf7\u4f7f\u7528\u6355\u83b7\u503c & \u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u9ed8\u8ba4\u503c\u6355\u83b7\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include #include #include int main () { std :: array areas { 100 , 25 , 121 , 40 , 56 }; int width {}; int height {}; std :: cout << \"Enter width and height: \" ; std :: cin >> width >> height ; auto found { std :: find_if ( areas . begin (), areas . end (), [ = ]( int knownArea ) { // will default capture width and height by value return ( width * height == knownArea ); // because they're mentioned here }) }; if ( found == areas . end ()) { std :: cout << \"I don't know this area :( \\n \" ; } else { std :: cout << \"Area found :) \\n \" ; } return 0 ; } \u9ed8\u8ba4\u6355\u83b7\u53ef\u4ee5\u4e0e\u666e\u901a\u6355\u83b7\u6df7\u5408\u4f7f\u7528\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u503c\u6355\u83b7\u4e00\u4e9b\u53d8\u91cf\uff0c\u901a\u8fc7\u5f15\u7528\u6355\u83b7\u53e6\u4e00\u4e9b\u53d8\u91cf\uff0c\u4f46\u662f\u6bcf\u4e2a\u53d8\u91cf\u53ea\u80fd\u6355\u83b7\u4e00\u6b21\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 int health { 33 }; int armor { 100 }; std :: vector < CEnemy > enemies {}; // \u6309\u503c\u6355\u83b7 health \u548c armor \uff0c\u6309\u5f15\u7528\u6355\u83b7 enemies [ health , armor , & enemies ](){}; // \u6309\u5f15\u7528\u6355\u83b7 enemies\uff0c\u5269\u4e0b\u7684\u90fd\u6309\u503c\u6355\u83b7 [ = , & enemies ](){}; // \u6309\u503c\u6355\u83b7 armor\uff0c\u5269\u4e0b\u7684\u90fd\u6309\u5f15\u7528\u6355\u83b7 [ & , armor ](){}; // \u9519\u8bef\uff1a\u5df2\u7ecf\u8868\u660e\u6309\u5f15\u7528\u6355\u83b7\u6240\u6709\u53d8\u91cf [ & , & armor ](){}; // \u9519\u8bef\uff1a\u5df2\u7ecf\u8868\u660e\u6309\u503c\u6355\u83b7\u6240\u6709\u53d8\u91cf [ = , armor ](){}; // \u9519\u8bef\uff1aarmor \u51fa\u73b0\u4e86\u4e24\u6b21 [ armor , & health , & armor ](){}; // \u9519\u8bef\uff1a\u9ed8\u8ba4\u6355\u83b7\u5fc5\u987b\u653e\u5728\u6700\u524d\u9762 [ armor , & ](){};","title":"\u9ed8\u8ba4\u6355\u83b7"},{"location":"12-8-lambda-captures/#_8","text":"\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5bf9\u6355\u83b7\u7684\u53d8\u91cf\u8fdb\u884c\u7a0d\u8bb8\u4fee\u6539\uff0c\u6216\u8005\u662f\u58f0\u660e\u4e00\u4e2a\u4ec5\u5728 lambda \u4f5c\u7528\u57df\u4e2d\u53ef\u89c1\u7684\u53d8\u91cf\uff0c\u6b64\u65f6\u6211\u4eec\u53ef\u4ee5\u5728\u95ed\u5305\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\u800c\u4e14\u4e0d\u5fc5\u6307\u660e\u5176\u7c7b\u578b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include #include #include int main () { std :: array areas { 100 , 25 , 121 , 40 , 56 }; int width {}; int height {}; std :: cout << \"Enter width and height: \" ; std :: cin >> width >> height ; // \u6211\u4eec\u9700\u8981\u9762\u79ef\uff0c\u4f46\u662f\u7528\u6237\u8f93\u5165\u7684\u662f\u957f\u5bbd\u3002 // \u6240\u4ee5\u9700\u8981\u5728\u641c\u7d22\u9762\u79ef\u524d\u9996\u5148\u8ba1\u7b97\u9762\u79ef auto found { std :: find_if ( areas . begin (), areas . end (), // \u58f0\u660e\u4e00\u4e2a\u4ec5 lambda \u53ef\u89c1\u7684\u53d8\u91cf // userArea \u4f1a\u88ab\u81ea\u52a8\u63a8\u65ad\u4e3a int. [ userArea { width * height }]( int knownArea ) { return ( userArea == knownArea ); }) }; if ( found == areas . end ()) { std :: cout << \"I don't know this area :( \\n \" ; } else { std :: cout << \"Area found :) \\n \" ; } return 0 ; } \u5f53lambda\u88ab\u5b9a\u4e49\u65f6\uff0c userArea \u53ea\u4f1a\u88ab\u8ba1\u7b97\u4e00\u6b21\u3002\u8ba1\u7b97\u7684\u9762\u79ef\u5b58\u50a8\u5728lambda\u5bf9\u8c61\u4e2d\uff0c\u6bcf\u6b21\u8c03\u7528\u90fd\u662f\u76f8\u540c\u7684\u3002\u5982\u679clambda\u662f\u53ef\u53d8\u7684\uff0c\u5e76\u4e14\u4fee\u6539\u4e86\u6355\u83b7\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u5219\u539f\u59cb\u503c\u5c06\u88ab\u8986\u76d6\u3002 \u6700\u4f73\u5b9e\u8df5 \u53ea\u6709\u5f53\u53d8\u91cf\u7684\u5b58\u5728\u65f6\u95f4\u5f88\u77ed\u4e14\u7c7b\u578b\u5f88\u660e\u663e\u65f6\uff0c\u624d\u5728\u6355\u83b7\u4e2d\u7684\u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u53d8\u91cf\u3002\u5426\u5219\uff0c\u6700\u597d\u5728lambda\u4e4b\u5916\u5b9a\u4e49\u53d8\u91cf\u5e76\u6355\u83b7\u5b83\u3002","title":"\u5728\u95ed\u5305\u4e2d\u5b9a\u4e49\u65b0\u7684\u53d8\u91cf"},{"location":"12-8-lambda-captures/#_9","text":"\u53d8\u91cf\u5728\u5b9a\u4e49lambda\u7684\u5730\u65b9\u88ab\u6355\u83b7\u3002\u5982\u679c\u5f15\u7528\u6355\u83b7\u7684\u53d8\u91cf\u5728lambda\u4e4b\u524d\u88ab\u9500\u6bc1\uff0c\u5219lambda\u5c06\u5f97\u5230\u4e00\u4e2a \u60ac\u5782 \u5f15\u7528\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include // \u8fd4\u56de\u4e00\u4e2a lambda auto makeWalrus ( const std :: string & name ) { // \u6309\u5f15\u7528\u6355\u83b7 name \u5e76\u8fd4\u56de\u4e00\u4e2alambda return [ & ]() { std :: cout << \"I am a walrus, my name is \" << name << '\\n' ; // \u672a\u5b9a\u4e49\u884c\u4e3a }; } int main () { // \u521b\u5efa\u4e00\u4e2a\u540d\u4e3aroofus\u7684\u65b0\u7684 walrus // sayName \u662f makeWalrus \u8fd4\u56de\u7684 lambda auto sayName { makeWalrus ( \"Roofus\" ) }; // \u8c03\u7528 makeWalrus \u8fd4\u56de\u7684 lambda sayName (); return 0 ; } \u8c03\u7528 makeWalrus \u4f1a\u4ece\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u201cRoofus\u201d\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u7684 std::string \u3002 makeWalrus \u4e2d\u7684lambda\u4f1a\u6309\u5f15\u7528\u6355\u83b7\u8be5\u5b57\u7b26\u4e32\u3002\u5f53 makeWalrus \u8fd4\u56de\u65f6\uff0c\u8be5\u5b57\u7b26\u4e32\u4f1a\u9500\u6bc1\uff0c\u4f46\u662f lambda \u4ecd\u7136\u4f1a\u4f7f\u7528\u5b83\u7684\u5f15\u7528\u3002\u4e8e\u662f\u5f53\u6211\u4eec\u8c03\u7528 sayName \u65f6\uff0c\u5c31\u4f1a\u8bbf\u95ee\u8be5\u60ac\u5782\u5f15\u7528\uff0c\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u6ce8\u610f\uff0c\u5373\u4f7f name \u662f \u6309\u503c\u4f20\u9012 \u7ed9 makeWalrus \u7684\u3002\u53d8\u91cf name \u4ecd\u7136\u4f1a\u5728 makeWalrus \u7ed3\u675f\u65f6\u9500\u6bc1\uff0clambda\u6301\u6709\u7684\u4ecd\u7136\u662f\u60ac\u5782\u5f15\u7528\u3002 \u6ce8\u610f \u5728\u901a\u8fc7\u5f15\u7528\u6355\u83b7\u53d8\u91cf\u65f6\u8981\u683c\u5916\u5c0f\u5fc3\uff0c\u7279\u522b\u662f\u4f7f\u7528\u9ed8\u8ba4\u5f15\u7528\u6355\u83b7\u65f6\u3002\u6355\u83b7\u7684\u53d8\u91cf\u5fc5\u987b\u6bd4lambda\u5b58\u6d3b\u7684\u65f6\u95f4\u957f\u3002 \u5982\u679c\u6211\u4eec\u60f3\u8981\u5728\u4f7f\u7528lambda\u65f6\u4ecd\u7136\u4fdd\u8bc1 name \u53ef\u7528\uff0c\u5219\u9700\u8981\u5728\u95ed\u5305\u4e2d\u6309\u503c\u6355\u83b7\u8be5\u53d8\u91cf\uff08\u663e\u5f0f\u6307\u5b9a\u6216\u9ed8\u8ba4\u6309\u503c\u6355\u83b7\uff09\u3002","title":"\u60ac\u5782\u7684\u6355\u83b7\u53d8\u91cf"},{"location":"12-8-lambda-captures/#lambda","text":"\u56e0\u4e3alambda\u662f\u5bf9\u8c61\uff0c\u6240\u4ee5\u5b83\u4eec\u53ef\u4ee5\u88ab\u590d\u5236\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u95ee\u9898\u3002\u8003\u8651\u4ee5\u4e0b\u4ee3\u7801: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int main () { int i { 0 }; // Create a new lambda named count auto count { [ i ]() mutable { std :: cout << ++ i << '\\n' ; } }; count (); // invoke count auto otherCount { count }; // create a copy of count // invoke both count and the copy count (); otherCount (); return 0 ; } \u8f93\u51fa\uff1a 1 2 3 1 2 2 \u7a0b\u5e8f\u6ca1\u6709\u6253\u5370 1, 2, 3\uff0c\u800c\u662f\u6253\u5370\u4e86\u4e24\u4e2a2\u3002\u6211\u4eec\u521b\u5efa otherCount \u65f6\u62f7\u8d1d\u4e86 count \uff0c\u540c\u65f6\u4e5f\u62f7\u8d1d\u4e86 count \u5f53\u524d\u7684\u72b6\u6001\u3002 count \u7684 i \u662f 1\uff0c\u6240\u4ee5 otherCount \u7684 i \u4e5f\u662f 1\u3002\u56e0\u4e3a otherCount \u662f count \u7684\u62f7\u8d1d\uff0c\u6240\u4ee5\u5b83\u4eec\u6709\u5404\u81ea\u7684 i \u3002 \u518d\u770b\u4e00\u4e2a\u4e0d\u592a\u660e\u663e\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include void myInvoke ( const std :: function < void () >& fn ) { fn (); } int main () { int i { 0 }; // Increments and prints its local copy of @i. auto count { [ i ]() mutable { std :: cout << ++ i << '\\n' ; } }; myInvoke ( count ); myInvoke ( count ); myInvoke ( count ); return 0 ; } \u8f93\u51fa\uff1a 1 2 3 1 1 1 \u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u5b58\u5728\u76f8\u540c\u7684\u95ee\u9898\uff0c\u4f46\u662f\u66f4\u52a0\u9690\u853d\u3002\u5f53\u901a\u8fc7lambda\u521b\u5efa std::function \u65f6\uff0c std::function \u4f1a\u521b\u5efa lambda \u7684\u62f7\u8d1d\u3002\u56e0\u6b64\u5728\u8c03\u7528 fn() \u65f6\uff0c\u6267\u884c\u7684\u5176\u5b9e\u662flambda \u7684\u62f7\u8d1d\u800c\u4e0d\u662f\u5176\u672c\u8eab\u3002 \u5982\u679c\u6211\u4eec\u9700\u8981\u4f20\u9012\u4e00\u4e2a\u53ef\u53d8\u7684lambda\uff0c\u5e76\u4e14\u60f3\u8981\u907f\u514d\u65e0\u610f\u5730\u590d\u5236\uff0c\u6709\u4e24\u4e2a\u65b9\u6cd5\u3002\u4e00\u79cd\u662f\u4f7f\u7528\u975e\u6355\u83b7lambda\u6765\u4ee3\u66ff\u2014\u2014\u5728\u4e0a\u9762\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5220\u9664\u6355\u83b7\u5e76\u4f7f\u7528\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u6765\u8ddf\u8e2a\u72b6\u6001\u3002\u4f46\u662f\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u5f88\u96be\u8ddf\u8e2a\uff0c\u5e76\u4e14\u4f7f\u6211\u4eec\u7684\u4ee3\u7801\u53ef\u8bfb\u6027\u8f83\u5dee\u3002\u4e00\u4e2a\u66f4\u597d\u7684\u9009\u62e9\u662f\u4ece\u4e00\u5f00\u59cb\u5c31\u963b\u6b62lambda\u7684\u590d\u5236\u3002\u4f46\u662f\u7531\u4e8e\u6211\u4eec\u4e0d\u80fd\u5f71\u54cd std::function (\u6216\u5176\u4ed6\u6807\u51c6\u5e93\u51fd\u6570\u6216\u5bf9\u8c61)\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u90a3\u8981\u5982\u4f55\u624d\u80fd\u505a\u5230\u8fd9\u4e00\u70b9\u5462? \u5e78\u8fd0\u7684\u662f\uff0cC++ \u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u52a0\u65b9\u4fbf\u7684\u7c7b\u578b(\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d) \u2014\u2014\u79f0\u4e3a std::reference_wrapper \u3002\u8be5\u7c7b\u578b\u4f7f\u6211\u4eec\u53ef\u4ee5\u4f20\u5165\u4e00\u4e2a\u666e\u901a\u7c7b\u578b\uff0c\u4f46\u5f53\u505a\u5f15\u7528\u4f7f\u7528\u3002\u66f4\u597d\u5730\u662f\uff0c std::reference_wrapper \u53ef\u4ee5\u901a\u8fc7 std::ref() \u51fd\u6570\u521b\u5efa\u3002\u5982\u679c\u6211\u4eec\u5c06 lambda \u5305\u88c5\u5230 std::reference_wrapper \u4e2d\uff0c\u5219\u6bcf\u5f53\u6709\u4eba\u60f3\u62f7\u8d1d lambda \u65f6\uff0c\u5b83\u4eec\u4f1a\u62f7\u8d1d\u4e00\u4e2a\u5f15\u7528\u800c\u4e0d\u662f\u5bf9\u8c61\u672c\u8eab\u3002 \u4f7f\u7528 std::ref \u66f4\u65b0\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include void myInvoke ( const std :: function < void () >& fn ) { fn (); } int main () { int i { 0 }; // Increments and prints its local copy of @i. auto count { [ i ]() mutable { std :: cout << ++ i << '\\n' ; } }; // std::ref(count) \u4f7f\u5f97 count \u88ab\u5f53\u505a\u5f15\u7528\u5bf9\u5f85 // \u56e0\u6b64, \u4efb\u4f55\u5bf9\u5176\u7684\u62f7\u8d1d\u90fd\u5b9e\u9645\u62f7\u8d1d\u7684\u662f\u5f15\u7528\uff0c\u786e\u4fdd\u59cb\u7ec8\u53ea\u5b58\u5728\u4e00\u4e2a count myInvoke ( std :: ref ( count )); myInvoke ( std :: ref ( count )); myInvoke ( std :: ref ( count )); return 0 ; } \u8f93\u51fa\u7b26\u5408\u9884\u671f\uff1a 1 2 3 1 2 3 \u6ce8\u610f\uff0c invoke \u6309\u503c\u83b7\u53d6 fn \u7ed3\u679c\u4e5f\u662f\u4e00\u6837\u7684\u3002\u5982\u679c\u6211\u4eec\u901a\u8fc7 std::ref \u521b\u5efa\u5b83\uff0c\u5219 std::function \u4e0d\u4f1a\u521b\u5efa lambda \u7684\u526f\u672c\u3002 \u6cd5\u5219 \u6807\u51c6\u5e93\u51fd\u6570\u53ef\u80fd\u4f1a\u590d\u5236\u51fd\u6570\u5bf9\u8c61(\u63d0\u9192:lambda\u662f\u51fd\u6570\u5bf9\u8c61)\u3002\u5982\u679c\u4f60\u60f3\u63d0\u4f9b\u5e26\u6709\u53ef\u53d8\u6355\u83b7\u53d8\u91cf\u7684lambda\uff0c\u53ef\u4ee5\u4f7f\u7528 std::ref \u901a\u8fc7\u5f15\u7528\u4f20\u9012\u5b83\u4eec\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c3d\u91cf\u907f\u514d\u53ef\u53d8\u7684lambda\u3002\u4e0d\u53ef\u53d8lambda\u66f4\u5bb9\u6613\u7406\u89e3\uff0c\u4e0d\u4f1a\u51fa\u73b0\u4e0a\u8ff0\u95ee\u9898\uff0c\u4e5f\u4e0d\u4f1a\u5728\u6dfb\u52a0\u5e76\u884c\u6267\u884c\u65f6\u51fa\u73b0\u66f4\u5371\u9669\u7684\u95ee\u9898\u3002","title":"\u65e0\u610f\u95f4\u62f7\u8d1d\u53ef\u53d8\u7684 lambda"},{"location":"12-x-Chapter-12-comprehensive-quiz/","tags":["summary"],"text":"\u51fd\u6570\u7684 \u5b9e\u53c2 \u53ef\u4ee5\u901a\u8fc7\u4f20\u503c\u3001\u4f20\u5f15\u7528\u6216\u8005\u4f20\u5730\u5740\u7684\u65b9\u5f0f\u6765\u4f20\u9012\u3002 - \u5728\u4f20\u9012\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u679a\u4e3e\u91cf\u65f6\u4f7f\u7528 \u6309\u503c\u4f20\u9012 \uff1b - \u5728\u4f20\u9012\u7ed3\u6784\u4f53\u3001\u7c7b\u6216\u8005\u9700\u8981\u51fd\u6570\u8fdb\u884c\u4fee\u6539\u7684\u5b9e\u53c2\u65f6\uff0c\u4f7f\u7528 \u6309\u5f15\u7528\u4f20\u9012 \uff1b - \u5728\u4f20\u9012\u6307\u9488\u6216\u5185\u7f6e\u6570\u7ec4\u65f6\u4f7f\u7528 \u6309\u5730\u5740\u4f20\u9012 \u3002 \u5982\u679c\u53ef\u4ee5\uff0c\u5c3d\u91cf\u5728\u4f20\u9012\u5f15\u7528\u548c\u5730\u5740\u65f6\u4f7f\u7528 const \u3002 \u503c\u53ef\u4ee5\u901a\u8fc7\u6309\u503c\u8fd4\u56de\u3001\u6309\u5f15\u7528\u8fd4\u56de\u6216\u6309\u5730\u5740\u8fd4\u56de\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6309\u503c\u8fd4\u56de\u662f\u53ef\u4ee5\u7684\uff0c\u4e0d\u8fc7\u6709\u65f6\u5019 \u6309\u5f15\u7528\u8fd4\u56de \u548c \u6309\u5730\u5740\u8fd4\u56de \u4e5f\u5f88\u6709\u7528\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u52a8\u6001\u5206\u914d\u7684\u6570\u636e\u3001\u7ed3\u6784\u4f53\u6216\u7c7b\u65f6\u3002\u5728\u4f7f\u7528\u6309\u5f15\u7528\u8fd4\u56de\u6216\u6309\u5730\u5740\u8fd4\u56de\u65f6\uff0c\u8bb0\u5f97\u8981\u786e\u4fdd\u8fd4\u56de\u7684\u5185\u5bb9\u4e0d\u4f1a \u79bb\u5f00\u4f5c\u7528\u57df \u3002 12.1 - \u51fd\u6570\u6307\u9488 \u51fd\u6570\u6307\u9488\u5141\u8bb8\u6211\u4eec\u5c06\u4e00\u4e2a\u51fd\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u3002\u8fd9\u6709\u52a9\u4e8e\u8c03\u7528\u8005\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u884c\u4e3a\uff0c\u4f8b\u5982\u5217\u8868\u7684\u6392\u5e8f\u7b97\u6cd5\u3002 12.2 - \u6808\u548c\u5806 \u52a8\u6001\u5185\u5b58\u5c06\u5728\u5806\u4e0a\u8fdb\u884c\u5206\u914d\u3002 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a \u51fd\u6570\u8c03\u7528\u65f6\u4f1a\u5728\u8c03\u7528\u6808\u4e2d\u5806\u6808\u8ddf\u8e2a\u4ece\u7a0b\u5e8f\u5f00\u59cb\u5230\u5f53\u524d\u6267\u884c\u70b9\u7684\u6240\u6709\u6d3b\u52a8\u51fd\u6570(\u90a3\u4e9b\u5df2\u88ab\u8c03\u7528\u4f46\u5c1a\u672a\u7ec8\u6b62\u7684\u51fd\u6570)\u3002\u5c40\u90e8\u53d8\u91cf\u4f1a\u5728\u6808\u4e0a\u5206\u914d\uff0c\u4f46\u662f\u6808\u7684\u7a7a\u95f4\u662f\u6709\u9650\u7684\u3002 std::vector \u53ef\u4ee5\u7528\u6765\u6a21\u62df\u6808\u7684\u884c\u4e3a\u3002 12.4 - \u9012\u5f52 \u81ea\u5df1\u8c03\u7528\u81ea\u5df1\u7684\u51fd\u6570\uff0c\u79f0\u4e3a\u9012\u5f52\u51fd\u6570\u3002\u6240\u6709\u7684\u9012\u5f52\u51fd\u6570\u90fd\u5fc5\u987b\u6709\u7ec8\u6b62\u6761\u4ef6\u3002 12.5 - \u547d\u4ee4\u884c\u53c2\u6570 \u547d\u4ee4\u884c\u53c2\u6570\u5141\u8bb8\u7528\u6237\u6216\u5176\u4ed6\u7a0b\u5e8f\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u4e3a\u5176\u4f20\u5165\u6570\u636e\u3002\u547d\u4ee4\u884c\u53c2\u6570\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u90fd\u662fC\u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u671f\u671b\u4f20\u9012\u7684\u662f\u6570\u503c\uff0c\u5219\u8981\u81ea\u884c\u8f6c\u6362\u3002 12.6 - \u7701\u7565\u53f7\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 \u7701\u7565\u53f7\u7528\u4e8e\u4f20\u9012\u4e0d\u5b9a\u957f\u53c2\u6570\u3002\u4f46\u662f\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4e0d\u4f1a\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\uff0c\u4e5f\u4e0d\u77e5\u9053\u4f20\u9012\u4e86\u591a\u5c11\u53c2\u6570\u3002\u7a0b\u5e8f\u5fc5\u987b\u81ea\u884c\u8ffd\u8e2a\u76f8\u5173\u7ec6\u8282\u3002 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb Lambda \u51fd\u6570\u662f\u90a3\u4e9b\u53ef\u4ee5\u5d4c\u5165\u5728\u5176\u4ed6\u51fd\u6570\u4e2d\u7684\u51fd\u6570\u3002\u5b83\u4eec\u4e0d\u9700\u8981\u51fd\u6570\u540d\uff0c\u800c\u4e14\u53ef\u4ee5\u5728\u548c algorithms \u5e93\u7ed3\u5408\u4f7f\u7528\u65f6\u975e\u5e38\u6709\u7528\u3002","title":"12.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570"},{"location":"13-1-welcome-to-object-oriented-programming/","tags":["object"],"text":"Key Takeaway \u5728 1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf \u4e2d\uff0c\u6211\u4eec\u5c06C++\u4e2d\u7684\u5bf9\u8c61\u5b9a\u4e49\u4e3a\uff1a\u201c\u4e00\u6bb5\u53ef\u4ee5\u88ab\u7528\u6765\u5b58\u50a8\u503c\u7684\u5185\u5b58\u201d\u3002\u4e00\u4e2a\u5177\u6709\u540d\u5b57\u7684\u5bf9\u8c61\u79f0\u4e3a\u53d8\u91cf\u3002 \u5728\u4f20\u7edf\u7f16\u7a0b\u9886\u57df\uff08\u672c\u8282\u8bfe\u4e4b\u524d\u4ecb\u7ecd\u7684\u5185\u5bb9\u90fd\u5c5e\u4e8e\u4f20\u7edf\u7f16\u7a0b\u9886\u57df\uff09\uff0c\u7a0b\u5e8f\u57fa\u672c\u4e0a\u5c31\u662f\u4e00\u7cfb\u5217\u8ba1\u7b97\u673a\u6307\u4ee4\uff0c\u5b83\u4eec\u5b9a\u4e49\u4e86\u6570\u636e\uff08\u901a\u8fc7\u5bf9\u8c61\uff09\uff0c\u7136\u540e\u64cd\u4f5c\u8fd9\u4e9b\u6570\u636e\uff08\u901a\u8fc7\u8bed\u53e5\u548c\u51fd\u6570\uff09\u3002\u6570\u636e\u548c\u51fd\u6570\u5171\u540c\u4f5c\u7528\u540c\u4e8e\u8ba1\u7b97\u6240\u9700\u7684\u7ed3\u679c\uff0c\u4f46\u5b83\u4eec\u5404\u81ea\u662f\u72ec\u7acb\u7684\u4e2a\u4f53\u3002\u56e0\u4e3a\u8fd9\u79cd\u72ec\u7acb\u6027\uff0c\u4f20\u7edf\u7684\u7f16\u7a0b\u6280\u672f\u901a\u5e38\u65e0\u6cd5\u4e3a\u73b0\u5b9e\u4e16\u754c\u4e2d\u7684\u4e8b\u52a1\u63d0\u4f9b\u76f4\u89c2\u7684\u8868\u793a\u6cd5\u3002\u7a0b\u5e8f\u5458\u9700\u8981\u5c06\u5c5e\u6027\uff08\u53d8\u91cf\uff09\u548c\u884c\u4e3a\uff08\u51fd\u6570\uff09\u6070\u5f53\u7684\u8054\u7cfb\u8d77\u6765\uff0c\u624d\u80fd\u786e\u4fdd\u7a0b\u5e8f\u6b63\u786e\u5de5\u4f5c\u3002\u5176\u4ee3\u7801\u770b\u4e0a\u53bb\u591a\u662f\u4e0b\u9762\u8fd9\u6837\uff1a 1 driveTo ( you , work ); \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u53c8\u662f\u4ec0\u4e48\u5462\uff1f\u5c31\u50cf\u8bb8\u591a\u4e8b\u60c5\u4e00\u6837\uff0c\u4e5f\u8bb8\u901a\u8fc7\u7c7b\u6bd4\u6700\u5bb9\u6613\u7406\u89e3\u3002\u770b\u770b\u4f60\u5468\u56f4\u7684\u4e8b\u7269\u2014\u2014\u4f60\u770b\u5230\u7684\u4efb\u4f55\u4e1c\u897f\u53ef\u4ee5\u88ab\u79f0\u4e4b\u4e3a\u5bf9\u8c61\uff1a\u4e66\u7c4d\u3001\u5efa\u7b51\u3001\u98df\u7269\u751a\u81f3\u662f\u4f60\u81ea\u5df1\u3002\u5bf9\u8c61\u901a\u5e38\u5305\u542b\u4e24\u4e2a\u4e3b\u8981\u7684\u7ec4\u6210\u90e8\u5206\uff1a1. \u4e00\u7ec4\u76f8\u5173\u7684\u5c5e\u6027\uff08\u4f8b\u5982\uff1a\u91cd\u91cf\u3001\u989c\u8272\u3001\u5c3a\u5bf8\u3001\u4f53\u79ef\u3001\u5f62\u72b6\u7b49\u7b49\uff09\u30022. \u5b83\u53ef\u4ee5\u8868\u73b0\u51fa\u7684\u4e00\u4e9b\u884c\u4e3a\uff08\u4f8b\u5982\uff1a\u88ab\u6253\u5f00\u3001\u52a0\u70ed\u4e1c\u897f\u7b49\u7b49\uff09\u3002\u8fd9\u4e9b\u5c5e\u6027\u548c\u884c\u4e3a\u662f\u5bc6\u4e0d\u53ef\u5206\u7684\u3002 \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\u4e3a\u7a0b\u5e8f\u5458\u63d0\u4f9b\u4e86\u4e00\u79cd\u5c06\u5c5e\u6027\u548c\u884c\u4e3a\u6253\u5305\u6210\u4e00\u4e2a\u81ea\u5305\u542b\u3001\u53ef\u91cd\u7528\u201c\u7ec4\u4ef6\u201d\u7684\u80fd\u529b\u3002\u5176\u4ee3\u7801\u98ce\u683c\u770b\u4e0a\u53bb\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 you . driveTo ( work ); \u4e0a\u9762\u7684\u4ee3\u7801\u4e0d\u4ec5\u8bfb\u8d77\u6765\u66f4\u6e05\u695a\uff0c\u8fd8\u66f4\u6e05\u695a\u76ee\u6807\u662f\u4ec0\u4e48( you )\uff0c\u8c03\u7528\u4e86\u4ec0\u4e48\u884c\u4e3a(\u5f00\u8f66\u5230\u67d0\u4e2a\u5730\u65b9)\u3002\u6211\u4eec\u4e0d\u518d\u662f\u4e13\u6ce8\u4e8e\u7f16\u5199\u51fd\u6570\uff0c\u800c\u662f\u4e13\u6ce8\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u4e86\u4e00\u7cfb\u5217\u88ab\u59a5\u5584\u5b9a\u4e49\u7684\u884c\u4e3a\u7684\u5bf9\u8c61\u3002\u8fd9\u5c31\u662f\u8fd9\u79cd\u8303\u5f0f\u88ab\u79f0\u4e3a\u201c\u9762\u5411\u5bf9\u8c61\u201d\u7684\u539f\u56e0\u3002 \u8fd9\u4e48\u505a\u662f\u7a0b\u5e8f\u5458\u53ef\u4ee5\u4ee5\u66f4\u6a21\u5757\u5316\u7684\u65b9\u5f0f\u7f16\u5199\u7a0b\u5e8f\uff0c\u7a0b\u5e8f\u4e5f\u66f4\u5bb9\u6613\u7f16\u5199\u548c\u7406\u89e3\uff0c\u8fd8\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7a0b\u5ea6\u7684\u4ee3\u7801\u91cd\u7528\u6027\u3002\u901a\u8fc7\u5b9a\u4e49\u5982\u4f55\u4e0e\u5bf9\u8c61\u4ea4\u4e92\uff0c\u4ee5\u53ca\u5bf9\u8c61\u5982\u4f55\u4e0e\u5176\u4ed6\u5bf9\u8c61\u4ea4\u4e92\uff0c\u8fd9\u4e9b\u5bf9\u8c61\u8fd8\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u76f4\u89c2\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6570\u636e\u3002 \u6ce8\u610f\uff0c\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u5e76\u4e0d\u662f\u8981\u53d6\u4ee3\u4f20\u7edf\u7684\u7f16\u7a0b\u65b9\u5f0f\u3002\u5b83\u53ea\u662f\u6211\u4eec\u5904\u7406\u590d\u6742\u95ee\u9898\u65f6\u7684\u53c8\u4e00\u79cd\u5de5\u5177\u7f62\u4e86\u3002 \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u8fd8\u5e26\u6765\u4e86\u5176\u4ed6\u4e00\u4e9b\u6709\u7528\u7684\u6982\u5ff5\uff1a \u7ee7\u627f \u3001 \u5c01\u88c5 \u3001 \u62bd\u8c61 \u548c \u591a\u6001\u6027 \u3002\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u51e0\u7ae0\u6559\u7a0b\u4e2d\u6db5\u76d6\u6240\u6709\u8fd9\u4e9b\u6982\u5ff5\u3002\u9762\u5411\u5bf9\u8c61\u5305\u542b\u4e86\u5f88\u591a\u65b0\u7684\u5185\u5bb9\uff0c\u4f46\u662f\u4e00\u65e6\u4f60\u5bf9\u5b83\u719f\u6089\u8d77\u6765\u5e76\u5bf9\u5b83\u4ea7\u751f\u5174\u8da3\uff0c\u4f60\u53ef\u80fd\u518d\u4e5f\u4e0d\u60f3\u56de\u5230\u7eaf\u7cb9\u7684\u4f20\u7edf\u7f16\u7a0b\u4e2d\u53bb\u4e86\u3002 \u6ce8\u610f\uff0c\u672f\u8bed\"\u5bf9\u8c61\"\u4e00\u8bcd\u4f3c\u4e4e\u5df2\u6709\u4e9b\u4e0d\u582a\u8d1f\u91cd\uff0c\u540c\u65f6\u4e5f\u5e26\u6765\u4e86\u4e0d\u5c0f\u7684\u6df7\u4e71\u3002\u5728\u4f20\u7edf\u7f16\u7a0b\u4e2d\uff0c\u5bf9\u8c61\u662f\u5b58\u50a8\u503c\u7684\u4e00\u5757\u5185\u5b58\u3002\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c\u201c\u5bf9\u8c61\u201d\u610f\u5473\u7740\u5b83\u65e2\u662f\u4f20\u7edf\u7f16\u7a0b\u610f\u4e49\u4e0a\u7684\u5bf9\u8c61\uff0c\u4e5f\u662f\u5c5e\u6027\u548c\u884c\u4e3a\u7684\u7ec4\u5408\u3002\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u5f53\u6211\u4eec\u4f7f\u7528\u672f\u8bed\u201c\u5bf9\u8c61\u201d\u65f6\uff0c\u6211\u4eec\u5c06\u6307\u9762\u5411\u5bf9\u8c61\u610f\u4e49\u4e0a\u7684\u201c\u5bf9\u8c61\u201d\u3002","title":"13.1 - \u6b22\u8fce\u6765\u5230\u9762\u5411\u5bf9\u8c61\u7684\u4e16\u754c"},{"location":"13-10-the-hidden-this-pointer/","tags":["this"],"text":"\u65b0\u624b\u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u5458\u7ecf\u5e38\u95ee\u7684\u4e00\u4e2a\u5173\u4e8e\u7c7b\u7684\u95ee\u9898\u662f\uff0c\u201c\u5f53\u8c03\u7528\u6210\u5458\u51fd\u6570\u65f6\uff0cC++\u5982\u4f55\u786e\u5b9a\u8c03\u7528\u5b83\u7684\u5bf9\u8c61\uff1f\u201c \u5b9e\u9645\u4e0a\uff0cC++ \u4f7f\u7528\u4e00\u4e2a\u540d\u4e3a \u201cthis\u201d\u7684\u9690\u85cf\u6307\u9488\u6765\u8ddf\u8e2a\u8c03\u7528\u6210\u5458\u7684\u5bf9\u8c61\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4fdd\u5b58\u6574\u6570\u5e76\u63d0\u4f9b\u6784\u9020\u51fd\u6570\u548c\u8bbf\u95ee\u51fd\u6570\u7684\u7b80\u5355\u7c7b\u3002\u6ce8\u610f\uff0c\u8fd9\u4e2a\u7c7b\u4e0d\u9700\u8981\u6790\u6784\u51fd\u6570\uff0c\u56e0\u4e3aC++\u81ea\u52a8\u6e05\u7406\u6574\u6570\u6210\u5458\u53d8\u91cf\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Simple { private : int m_id ; public : Simple ( int id ) : m_id { id } { } void setID ( int id ) { m_id = id ; } int getID () { return m_id ; } }; \u4e0b\u9762\u4f8b\u5b50\u663e\u793a\u4e86Simple\u7684\u4f7f\u7528\u65b9\u6cd5\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { Simple simple { 1 }; simple . setID ( 2 ); std :: cout << simple . getID () << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 \u5f53\u6211\u4eec\u8c03\u7528 simple.setID(2); \u65f6\uff0cC++ \u5c31\u80fd\u591f\u77e5\u9053\u8c03\u7528\u7684\u662f simple \u7684 setID() \uff0c\u800c m_id \u5219\u6307\u7684\u662f simple.m_id \u3002\u63a5\u4e0b\u6765\u8ba9\u6211\u4eec\u770b\u770b\u8fd9\u80cc\u540e\u7684\u5de5\u4f5c\u539f\u7406\u3002 \u9690\u85cf\u7684 *this \u6307\u9488 \u89c2\u5bdf\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u8fd9\u4e00\u884c\u4ee3\u7801\uff1a 1 simple . setID ( 2 ); \u5c3d\u7ba1\u51fd\u6570 setID() \u51fd\u6570\u770b\u4e0a\u53bb\u53ea\u63a5\u53d7\u4e86\u4e00\u4e2a\u53c2\u6570\uff0c\u800c\u5b9e\u9645\u4e0a\u5b83\u63a5\u53d7\u4e86\u4e24\u4e2a\u53c2\u6570\uff01\u5f53\u7a0b\u5e8f\u7f16\u8bd1\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c06\u4e0a\u8ff0\u4ee3\u7801\u8f6c\u53d8\u6210\u4e0b\u9762\u7684\u5f62\u5f0f \uff1a 1 setID ( & simple , 2 ); // \u6ce8\u610f\uff0c\u4f5c\u4e3a\u5bf9\u8c61\u7684\u524d\u7f00\u73b0\u5728\u53d8\u6210\u4e86\u51fd\u6570\u7684\u5b9e\u53c2\uff01 \u6ce8\u610f\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u5df2\u7ecf\u53d8\u6210\u4e86\u6807\u51c6\u7684\u51fd\u6570\u8c03\u7528\uff0csimple \u5bf9\u8c61(\u4ee5\u524d\u662f\u4e00\u4e2a\u5bf9\u8c61\u524d\u7f00)\u5219\u901a\u8fc7 \u6309\u5730\u5740\u4f20\u9012 \u7684\u65b9\u5f0f\u4f20\u5165\u4e86\u51fd\u6570\u3002 \u4f46\u8fd9\u53ea\u662f\u7b54\u6848\u7684\u4e00\u534a\uff0c\u7531\u4e8e\u51fd\u6570\u8c03\u7528\u73b0\u5728\u589e\u52a0\u4e86\u4e00\u4e2a\u5b9e\u53c2\uff0c\u56e0\u6b64\u9700\u8981\u4fee\u6539\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u624d\u80fd\u63a5\u53d7(\u5e76\u4f7f\u7528)\u8fd9\u4e2a\u5b9e\u53c2\u4f5c\u4e3a\u5f62\u53c2\u3002\u56e0\u6b64\uff0c\u4e0b\u9762\u7684\u6210\u5458\u51fd\u6570: 1 void setID ( int id ) { m_id = id ; } \u4f1a\u88ab\u7f16\u8bd1\u5668\u8f6c\u6362\u4e3a\u4e0b\u9762\u7684\u5f62\u5f0f\uff1a 1 void setID ( Simple * const this , int id ) { this -> m_id = id ; } \u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u666e\u901a\u7684\u6210\u5458\u51fd\u6570\u65f6\uff0c\u4f1a\u9690\u5f0f\u5730\u4e3a\u5176\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u53c2\u6570 this \u3002 this \u6307\u9488\u662f\u4e00\u4e2a\u9690\u85cf\u7684\u5e38\u91cf\u6307\u9488\uff0c\u5b83\u4fdd\u5b58\u7740\u8be5\u51fd\u6570\u5bf9\u5e94\u5bf9\u8c61\u7684\u5730\u5740\u3002 \u6b64\u5916\uff0c\u8fd8\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u3002\u5728\u6210\u5458\u51fd\u6570\u4e2d\uff0c\u5728\u6210\u5458\u51fd\u6570\u4e2d\uff0c\u4efb\u4f55\u7c7b\u6210\u5458\uff08\u5305\u62ec\u51fd\u6570\u548c\u53d8\u91cf\uff09\u4e5f\u9700\u8981\u88ab\u66f4\u65b0\u4e3a\u5f15\u7528\u5bf9\u8c61\u7684\u5f62\u5f0f\u3002\u8981\u5b8c\u6210\u8fd9\u4e00\u70b9\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4e3a\u5b83\u4eec\u6dfb\u52a0 this-> \u524d\u7f00\u5373\u53ef\u3002\u56e0\u6b64\u5728\u51fd\u6570\u4f53\u4e2d\uff0c m_id (\u6210\u5458\u53d8\u91cf)\u88ab\u8f6c\u53d8\u6210 this->m_id \u3002\u7531\u4e8e this \u6307\u9488\u6307\u5411\u7684\u662f simple \u5bf9\u8c61\u7684\u5730\u5740\uff0c\u56e0\u6b64 this->m_id \u4f1a\u88ab\u89e3\u6790\u4e3a simple.m_id \u3002 \u603b\u7ed3\u4e00\u4e0b\uff1a \u5f53\u8c03\u7528 simple.setID(2) \u65f6\uff0c\u7f16\u8bd1\u5668\u5b9e\u9645\u8c03\u7528\u7684\u662f setID(&simple, 2) \uff1b \u5728 setID() \u4e2d\uff0cthis \u6307\u9488\u4fdd\u5b58\u7740 simple \u5bf9\u8c61\u7684\u5730\u5740\uff1b setID() \u51fd\u6570\u4e2d\u7684\u4efb\u4f55\u6210\u5458\u90fd\u4f1a\u88ab\u6dfb\u52a0 \u201c this-> \u201d \u524d\u7f00\uff0c\u6240\u4ee5\uff0c\u5bf9\u4e8e m_id = id \u8fd9\u6761\u8bed\u53e5\uff0c\u7f16\u8bd1\u5668\u5b9e\u9645\u4e0a\u6267\u884c\u7684\u662f this->m_id = id \uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u5373\u5c06 simple.m_id \u7684\u503c\u66f4\u65b0\u4e3a id \u3002 \u597d\u6d88\u606f\u662f\uff0c\u6240\u6709\u8fd9\u4e9b\u90fd\u662f\u81ea\u52a8\u53d1\u751f\u7684\uff0c\u4f60\u662f\u5426\u8bb0\u5f97\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u5e76\u4e0d\u91cd\u8981\u3002\u4f60\u53ea\u9700\u8bb0\u4f4f\uff0c\u6240\u6709\u666e\u901a\u6210\u5458\u51fd\u6570\u90fd\u6709\u4e00\u4e2a\u201cthis\u201d\u6307\u9488\uff0c\u8be5\u6307\u9488\u6307\u5411\u8c03\u7528\u51fd\u6570\u7684\u5bf9\u8c61\u3002 \u201cthis\u201d \u6307\u9488\u603b\u662f\u6307\u5411\u64cd\u4f5c\u5bf9\u8c61\u81ea\u5df1 \u65b0\u7a0b\u5e8f\u5458\u6709\u65f6\u4f1a\u5bf9\u5b58\u5728\u591a\u5c11\u4e2a\u201cthis\u201d\u6307\u9488\u611f\u5230\u56f0\u60d1\u3002\u6bcf\u4e2a\u6210\u5458\u51fd\u6570\u90fd\u6709\u4e00\u4e2a\u201cthis\u201d\u6307\u9488\u5f62\u53c2\uff0c\u5b83\u88ab\u8bbe\u7f6e\u4e3a\u6b63\u5728\u64cd\u4f5c\u7684\u5bf9\u8c61\u7684\u5730\u5740\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 int main () { Simple A { 1 }; // this = &A inside the Simple constructor Simple B { 2 }; // this = &B inside the Simple constructor A . setID ( 3 ); // this = &A inside member function setID B . setID ( 4 ); // this = &B inside member function setID return 0 ; } \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0cthis\u6307\u9488\u4ea4\u66ff\u5730\u6307\u4ee3\u5bf9\u8c61A\u6216\u8005B\u7684\u5730\u5740\uff0c\u53d6\u51b3\u4e8e\u8c03\u7528\u6210\u5458\u51fd\u6570\u7684\u662fA\u8fd8\u662fB\u3002 \u56e0\u4e3a\u201cthis\u201d\u53ea\u662f\u4e00\u4e2a\u51fd\u6570\u5f62\u53c2\uff0c\u5b83\u4e0d\u4f1a\u7ed9\u7c7b\u589e\u52a0\u4efb\u4f55\u5185\u5b58\u4f7f\u7528\u91cf(\u53ea\u662f\u7ed9\u6210\u5458\u51fd\u6570\u8c03\u7528\u589e\u52a0\u5185\u5b58\u4f7f\u7528\u91cf\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u5f62\u53c2\u9700\u8981\u4f20\u9012\u7ed9\u51fd\u6570\u5e76\u5b58\u50a8\u5728\u5185\u5b58\u4e2d)\u3002 \u663e\u5f0f\u5730\u4f7f\u7528 this \u6307\u9488 \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f60\u4e0d\u9700\u8981\u663e\u5f0f\u5f15\u7528\u201cthis\u201d\u6307\u9488\u3002\u7136\u800c\uff0c\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\u76f4\u63a5\u4f7f\u7528 this \u6307\u9488\u4e5f\u662f\u6709\u7528\u7684\u3002 \u9996\u5148\uff0c\u5982\u679c\u6784\u9020\u51fd\u6570(\u6216\u6210\u5458\u51fd\u6570)\u7684\u5f62\u53c2\u4e0e\u6210\u5458\u53d8\u91cf\u540c\u540d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\" this \"\u6765\u6d88\u9664\u6b67\u4e49\uff1a 1 2 3 4 5 6 7 8 9 10 11 class Something { private : int data ; public : Something ( int data ) { this -> data = data ; // this->data is the member, data is the local parameter } }; \u4e0a\u9762\u4ee3\u7801\u4e2d\u7684\u6784\u9020\u51fd\u6570\uff0c\u5176 \u5f62\u53c2 \u548c\u7c7b\u6210\u5458\u53d8\u91cf\u540c\u540d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c data \u5c06\u8868\u793a\u5f62\u53c2\uff0c\u6240\u4ee5\u4f60\u5fc5\u987b\u4f7f\u7528 \u201cthis->data\u201d \u6765\u8868\u793a\u6210\u5458\u53d8\u91cf\u3002\u5c3d\u7ba1\u8fd9\u4e48\u505a\u4e5f\u662f\u53ef\u4ee5\u88ab\u63a5\u53d7\u7684\u7f16\u7a0b\u98ce\u683c\uff0c\u4f46\u662f\u4f7f\u7528 m_ \u4f5c\u4e3a\u6210\u5458\u53d8\u91cf\u7684\u524d\u7f00\u5c31\u53ef\u4ee5\u4ece\u6e90\u5934\u4e0a\u6d88\u9664\u8fd9\u79cd\u6b67\u4e49\u3002 \u94fe\u5f0f\u8c03\u7528\u6210\u5458\u51fd\u6570 \u53e6\u4e00\u65b9\u9762\uff0c\u6709\u65f6\u5019\u8ba9\u7c7b\u6210\u5458\u51fd\u6570\u8fd4\u56de\u5bf9\u8c61\u672c\u8eab\u4e5f\u662f\u5f88\u6709\u7528\u7684\u3002\u8fd9\u4e48\u505a\u7684\u4e00\u4e2a\u4e3b\u8981\u539f\u56e0\u5c31\u662f\u53ef\u4ee5\u5141\u8bb8\u51fd\u6570\u7684\u94fe\u5f0f\u8c03\u7528\uff0c\u4f7f\u5f97\u591a\u4e2a\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u4f5c\u7528\u4e8e\u4e00\u4e2a\u76f8\u540c\u7684\u5bf9\u8c61\uff01\u5176\u5b9e\u4f60\u5df2\u7ecf\u591a\u6b21\u4f7f\u7528\u8be5\u7279\u6027\u4e86\u3002\u5bf9\u4e8e\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u6765\u8bf4\uff0c std::cout \u8f93\u51fa\u4e86\u4e0d\u6b62\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002 1 std :: cout << \"Hello, \" << userName ; \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c std::cout \u5176\u5b9e\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u800c\u8fd0\u7b97\u7b26 << \u662f\u8be5\u5bf9\u8c61\u7684\u4e00\u4e2a\u6210\u5458\u51fd\u6570\u3002\u7f16\u8bd1\u5668\u4f1a\u6309\u7167\u4e0b\u9762\u7684\u65b9\u5f0f\u5bf9\u4e0a\u8ff0\u4ee3\u7801\u6c42\u503c\uff1a 1 ( std :: cout << \"Hello, \" ) << userName ; \u9996\u5148\uff0c << \u4f7f\u7528 std::cout \u548c\u5b57\u7b26\u4e32\u5b57\u9762\u91cf \u201cHello, \u201d \u5c06 \u201cHello, \u201d \u6253\u5370\u5230\u7ec8\u7aef\u3002\u4e0d\u8fc7\uff0c\u8fd9\u53ea\u662f\u4e0a\u8ff0\u8868\u8fbe\u5f0f\u7684\u4e00\u90e8\u5206\uff0c << \u8fd8\u9700\u8981\u8fd4\u56de\u4e00\u4e2a\u503c\uff08\u6216void\uff09\u3002\u5982\u679c\u5b83\u8fd4\u56de\u7684\u662f void\uff0c\u5219\u7ed3\u679c\u4f1a\u53d8\u4e3a\u4e0b\u9762\u7684\u5f62\u5f0f\uff1a 1 ( void ) << userName ; \u8fd9\u663e\u7136\u4e0d\u5408\u903b\u8f91\uff08\u7f16\u8bd1\u5668\u4f1a\u629b\u51fa\u9519\u8bef\uff09\u3002\u5b9e\u9645\u4e0a\uff0c << \u7684\u8fd4\u56de\u503c\u662f *this \uff0c\u5728\u4e0a\u9762\u7684\u8bed\u5883\u4e0b\uff0cthis \u5c31\u662f std::cout \u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u5f53\u7b2c\u4e00\u4e2a << \u6267\u884c\u5b8c\u6210\u540e\uff0c\u8bed\u53e5\u7b49\u4ef7\u4e8e\u5982\u4e0b\u5f62\u5f0f\uff1a 1 ( std :: cout ) << userName ; \u4e0a\u8ff0\u4ee3\u7801\u8fdb\u800c\u4f1a\u6253\u5370\u7528\u6237\u540d\u3002 \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ea\u9700\u8981\u6307\u5b9a\u5bf9\u8c61\u4e00\u6b21\uff08\u6b64\u4f8b\u4e2d\u4e3a std::cout )\uff0c\u7136\u540e\u6bcf\u6b21\u51fd\u6570\u8c03\u7528\u90fd\u53ef\u4ee5\u628a\u5b83\u4f20\u9012\u7ed9\u63a5\u4e0b\u6765\u7684\u51fd\u6570\uff0c\u662f\u6211\u4eec\u53ef\u4ee5\u94fe\u5f0f\u6267\u884c\u591a\u4e2a\u6307\u4ee4\u3002 \u5728\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u4e2d\u6211\u4eec\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u4e0a\u8ff0\u884c\u4e3a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 class Calc { private : int m_value { 0 }; public : void add ( int value ) { m_value += value ; } void sub ( int value ) { m_value -= value ; } void mult ( int value ) { m_value *= value ; } int getValue () { return m_value ; } }; \u5982\u679c\u4f60\u5e0c\u671b\u5148\u52a05\uff0c\u518d\u51cf3\uff0c\u7136\u540e\u518d\u4e58\u4ee54\uff0c\u90a3\u4e48\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { Calc calc {}; calc . add ( 5 ); // returns void calc . sub ( 3 ); // returns void calc . mult ( 4 ); // returns void std :: cout << calc . getValue () << '\\n' ; return 0 ; } \u4e0d\u8fc7\uff0c\u5982\u679c\u6bcf\u4e2a\u51fd\u6570\u90fd\u8fd4\u56de *this \u7684\u8bdd\uff0c\u5728\u53ef\u4ee5\u94fe\u5f0f\u5730\u8c03\u7528\u5b83\u4eec\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u53ef\u94fe\u5f0f\u8c03\u7528\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class Calc { private : int m_value {}; public : Calc & add ( int value ) { m_value += value ; return * this ; } Calc & sub ( int value ) { m_value -= value ; return * this ; } Calc & mult ( int value ) { m_value *= value ; return * this ; } int getValue () { return m_value ; } }; \u6ce8\u610f\uff0c\u51fd\u6570 add() \uff0c sub() \u548c mult() \u73b0\u5728\u90fd\u8fd4\u56de *this \u3002\u8fd9\u6837\u4e00\u6765\u5c31\u53ef\u4ee5\u5411\u4e0b\u9762\u8fd9\u6837\u8c03\u7528\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { Calc calc {}; calc . add ( 5 ). sub ( 3 ). mult ( 4 ); std :: cout << calc . getValue () << '\\n' ; return 0 ; } \u4e09\u884c\u4ee3\u7801\u88ab\u538b\u7f29\u6210\u4e86\u4e00\u884c\uff01\u6211\u4eec\u518d\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002 \u9996\u5148\uff0c\u8c03\u7528 calc.add(5) \u5c06 5 \u52a0\u5230 m_value \uff0c\u7136\u540e\u8fd4\u56de *this \uff0c\u5b83\u662f calc \u7684\u5f15\u7528\uff0c\u6240\u4ee5 calc \u4f1a\u88ab\u7528\u4e8e\u8c03\u7528\u63a5\u4e0b\u6765\u7684\u51fd\u6570\u3002\u518d\u63a5\u4e0b\u6765\uff0c\u5bf9 calc.sub(3) \u6c42\u503c\uff0c\u5b83\u5c06 3 \u4ece m_value \u51cf\u53bb\u540e\uff0c\u7ee7\u7eed\u8fd4\u56de calc \u3002\u6700\u540e\uff0c calc.mult(4) \u5c06 m_value \u4e58\u4ee5\u56db\u5e76\u8fd4\u56de calc \uff0c\u7531\u4e8e\u4e0d\u9700\u8981\u7ee7\u7eed\u8c03\u7528\u51fd\u6570\uff0c\u8fd9\u4e2a\u8fd4\u56de\u503c\u4e5f\u5c31\u88ab\u5ffd\u7565\u6389\u4e86\u3002 \u56e0\u4e3a\u6bcf\u4e2a\u51fd\u6570\u90fd\u4f1a\u4fee\u6539\u5bf9\u8c61 calc \uff0c\u6240\u4ee5 calc \u7684\u6210\u5458 m_value \u7684\u503c\u6b64\u65f6\u4e3a (((0 + 5) - 3) * 4) \uff0c\u53738\u3002 \u5c0f\u7ed3 \u201cthis\u201d \u6307\u9488\u662f\u4e00\u4e2a\u88ab\u7f16\u8bd1\u5668\u9690\u5f0f\u6dfb\u52a0\u5230\u975e\u9759\u6001\u6210\u5458\u51fd\u6570\u7684\u9690\u85cf\u53c2\u6570\u3002\u591a\u6570\u60c5\u51b5\u4e0b\u4f60\u5e76\u4e0d\u4f1a\u76f4\u63a5\u8bbf\u95ee\u5b83\uff0c\u4f46\u662f\u5f53\u6709\u9700\u8981\u65f6\u4f60\u4f1a\u7528\u7684\u7740\u7684\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cthis\u662f\u4e00\u4e2a\u5e38\u91cf\u6307\u9488\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5b83\u4fee\u6539\u5b83\u6240\u6307\u7684\u5bf9\u8c61\uff0c\u4f46\u662f\u4f60\u4e0d\u80fd\u8ba9\u5b83\u6307\u5411\u522b\u7684\u5bf9\u8c61\u3002 \u5982\u679c\u6211\u4eec\u8ba9\u539f\u672c\u8fd4\u56de void \u7684\u51fd\u6570\u8fd4\u56de *this \uff0c\u5219\u53ef\u4ee5\u4f7f\u8fd9\u4e9b\u51fd\u6570\u80fd\u591f\u88ab\u94fe\u5f0f\u8c03\u7528\u3002 \u901a\u5e38\u5728\u91cd\u8f7d\u7c7b\u7684\u8fd0\u7b97\u7b26\u65f6\u4f1a\u7528\u5230\u8fd9\u4e2a\u7279\u6027 \uff0814\u7ae0\u4e2d\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u8fd0\u7b97\u7b26\u91cd\u8f7d\uff09\u3002","title":"13.10 - \u9690\u85cf\u7684this\u6307\u9488"},{"location":"13-10-the-hidden-this-pointer/#this","text":"\u89c2\u5bdf\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u8fd9\u4e00\u884c\u4ee3\u7801\uff1a 1 simple . setID ( 2 ); \u5c3d\u7ba1\u51fd\u6570 setID() \u51fd\u6570\u770b\u4e0a\u53bb\u53ea\u63a5\u53d7\u4e86\u4e00\u4e2a\u53c2\u6570\uff0c\u800c\u5b9e\u9645\u4e0a\u5b83\u63a5\u53d7\u4e86\u4e24\u4e2a\u53c2\u6570\uff01\u5f53\u7a0b\u5e8f\u7f16\u8bd1\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c06\u4e0a\u8ff0\u4ee3\u7801\u8f6c\u53d8\u6210\u4e0b\u9762\u7684\u5f62\u5f0f \uff1a 1 setID ( & simple , 2 ); // \u6ce8\u610f\uff0c\u4f5c\u4e3a\u5bf9\u8c61\u7684\u524d\u7f00\u73b0\u5728\u53d8\u6210\u4e86\u51fd\u6570\u7684\u5b9e\u53c2\uff01 \u6ce8\u610f\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u5df2\u7ecf\u53d8\u6210\u4e86\u6807\u51c6\u7684\u51fd\u6570\u8c03\u7528\uff0csimple \u5bf9\u8c61(\u4ee5\u524d\u662f\u4e00\u4e2a\u5bf9\u8c61\u524d\u7f00)\u5219\u901a\u8fc7 \u6309\u5730\u5740\u4f20\u9012 \u7684\u65b9\u5f0f\u4f20\u5165\u4e86\u51fd\u6570\u3002 \u4f46\u8fd9\u53ea\u662f\u7b54\u6848\u7684\u4e00\u534a\uff0c\u7531\u4e8e\u51fd\u6570\u8c03\u7528\u73b0\u5728\u589e\u52a0\u4e86\u4e00\u4e2a\u5b9e\u53c2\uff0c\u56e0\u6b64\u9700\u8981\u4fee\u6539\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u624d\u80fd\u63a5\u53d7(\u5e76\u4f7f\u7528)\u8fd9\u4e2a\u5b9e\u53c2\u4f5c\u4e3a\u5f62\u53c2\u3002\u56e0\u6b64\uff0c\u4e0b\u9762\u7684\u6210\u5458\u51fd\u6570: 1 void setID ( int id ) { m_id = id ; } \u4f1a\u88ab\u7f16\u8bd1\u5668\u8f6c\u6362\u4e3a\u4e0b\u9762\u7684\u5f62\u5f0f\uff1a 1 void setID ( Simple * const this , int id ) { this -> m_id = id ; } \u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u666e\u901a\u7684\u6210\u5458\u51fd\u6570\u65f6\uff0c\u4f1a\u9690\u5f0f\u5730\u4e3a\u5176\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u53c2\u6570 this \u3002 this \u6307\u9488\u662f\u4e00\u4e2a\u9690\u85cf\u7684\u5e38\u91cf\u6307\u9488\uff0c\u5b83\u4fdd\u5b58\u7740\u8be5\u51fd\u6570\u5bf9\u5e94\u5bf9\u8c61\u7684\u5730\u5740\u3002 \u6b64\u5916\uff0c\u8fd8\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u3002\u5728\u6210\u5458\u51fd\u6570\u4e2d\uff0c\u5728\u6210\u5458\u51fd\u6570\u4e2d\uff0c\u4efb\u4f55\u7c7b\u6210\u5458\uff08\u5305\u62ec\u51fd\u6570\u548c\u53d8\u91cf\uff09\u4e5f\u9700\u8981\u88ab\u66f4\u65b0\u4e3a\u5f15\u7528\u5bf9\u8c61\u7684\u5f62\u5f0f\u3002\u8981\u5b8c\u6210\u8fd9\u4e00\u70b9\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4e3a\u5b83\u4eec\u6dfb\u52a0 this-> \u524d\u7f00\u5373\u53ef\u3002\u56e0\u6b64\u5728\u51fd\u6570\u4f53\u4e2d\uff0c m_id (\u6210\u5458\u53d8\u91cf)\u88ab\u8f6c\u53d8\u6210 this->m_id \u3002\u7531\u4e8e this \u6307\u9488\u6307\u5411\u7684\u662f simple \u5bf9\u8c61\u7684\u5730\u5740\uff0c\u56e0\u6b64 this->m_id \u4f1a\u88ab\u89e3\u6790\u4e3a simple.m_id \u3002 \u603b\u7ed3\u4e00\u4e0b\uff1a \u5f53\u8c03\u7528 simple.setID(2) \u65f6\uff0c\u7f16\u8bd1\u5668\u5b9e\u9645\u8c03\u7528\u7684\u662f setID(&simple, 2) \uff1b \u5728 setID() \u4e2d\uff0cthis \u6307\u9488\u4fdd\u5b58\u7740 simple \u5bf9\u8c61\u7684\u5730\u5740\uff1b setID() \u51fd\u6570\u4e2d\u7684\u4efb\u4f55\u6210\u5458\u90fd\u4f1a\u88ab\u6dfb\u52a0 \u201c this-> \u201d \u524d\u7f00\uff0c\u6240\u4ee5\uff0c\u5bf9\u4e8e m_id = id \u8fd9\u6761\u8bed\u53e5\uff0c\u7f16\u8bd1\u5668\u5b9e\u9645\u4e0a\u6267\u884c\u7684\u662f this->m_id = id \uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u5373\u5c06 simple.m_id \u7684\u503c\u66f4\u65b0\u4e3a id \u3002 \u597d\u6d88\u606f\u662f\uff0c\u6240\u6709\u8fd9\u4e9b\u90fd\u662f\u81ea\u52a8\u53d1\u751f\u7684\uff0c\u4f60\u662f\u5426\u8bb0\u5f97\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u5e76\u4e0d\u91cd\u8981\u3002\u4f60\u53ea\u9700\u8bb0\u4f4f\uff0c\u6240\u6709\u666e\u901a\u6210\u5458\u51fd\u6570\u90fd\u6709\u4e00\u4e2a\u201cthis\u201d\u6307\u9488\uff0c\u8be5\u6307\u9488\u6307\u5411\u8c03\u7528\u51fd\u6570\u7684\u5bf9\u8c61\u3002","title":"\u9690\u85cf\u7684 *this \u6307\u9488"},{"location":"13-10-the-hidden-this-pointer/#this_1","text":"\u65b0\u7a0b\u5e8f\u5458\u6709\u65f6\u4f1a\u5bf9\u5b58\u5728\u591a\u5c11\u4e2a\u201cthis\u201d\u6307\u9488\u611f\u5230\u56f0\u60d1\u3002\u6bcf\u4e2a\u6210\u5458\u51fd\u6570\u90fd\u6709\u4e00\u4e2a\u201cthis\u201d\u6307\u9488\u5f62\u53c2\uff0c\u5b83\u88ab\u8bbe\u7f6e\u4e3a\u6b63\u5728\u64cd\u4f5c\u7684\u5bf9\u8c61\u7684\u5730\u5740\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 int main () { Simple A { 1 }; // this = &A inside the Simple constructor Simple B { 2 }; // this = &B inside the Simple constructor A . setID ( 3 ); // this = &A inside member function setID B . setID ( 4 ); // this = &B inside member function setID return 0 ; } \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0cthis\u6307\u9488\u4ea4\u66ff\u5730\u6307\u4ee3\u5bf9\u8c61A\u6216\u8005B\u7684\u5730\u5740\uff0c\u53d6\u51b3\u4e8e\u8c03\u7528\u6210\u5458\u51fd\u6570\u7684\u662fA\u8fd8\u662fB\u3002 \u56e0\u4e3a\u201cthis\u201d\u53ea\u662f\u4e00\u4e2a\u51fd\u6570\u5f62\u53c2\uff0c\u5b83\u4e0d\u4f1a\u7ed9\u7c7b\u589e\u52a0\u4efb\u4f55\u5185\u5b58\u4f7f\u7528\u91cf(\u53ea\u662f\u7ed9\u6210\u5458\u51fd\u6570\u8c03\u7528\u589e\u52a0\u5185\u5b58\u4f7f\u7528\u91cf\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u5f62\u53c2\u9700\u8981\u4f20\u9012\u7ed9\u51fd\u6570\u5e76\u5b58\u50a8\u5728\u5185\u5b58\u4e2d)\u3002","title":"\u201cthis\u201d \u6307\u9488\u603b\u662f\u6307\u5411\u64cd\u4f5c\u5bf9\u8c61\u81ea\u5df1"},{"location":"13-10-the-hidden-this-pointer/#this_2","text":"\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f60\u4e0d\u9700\u8981\u663e\u5f0f\u5f15\u7528\u201cthis\u201d\u6307\u9488\u3002\u7136\u800c\uff0c\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\u76f4\u63a5\u4f7f\u7528 this \u6307\u9488\u4e5f\u662f\u6709\u7528\u7684\u3002 \u9996\u5148\uff0c\u5982\u679c\u6784\u9020\u51fd\u6570(\u6216\u6210\u5458\u51fd\u6570)\u7684\u5f62\u53c2\u4e0e\u6210\u5458\u53d8\u91cf\u540c\u540d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\" this \"\u6765\u6d88\u9664\u6b67\u4e49\uff1a 1 2 3 4 5 6 7 8 9 10 11 class Something { private : int data ; public : Something ( int data ) { this -> data = data ; // this->data is the member, data is the local parameter } }; \u4e0a\u9762\u4ee3\u7801\u4e2d\u7684\u6784\u9020\u51fd\u6570\uff0c\u5176 \u5f62\u53c2 \u548c\u7c7b\u6210\u5458\u53d8\u91cf\u540c\u540d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c data \u5c06\u8868\u793a\u5f62\u53c2\uff0c\u6240\u4ee5\u4f60\u5fc5\u987b\u4f7f\u7528 \u201cthis->data\u201d \u6765\u8868\u793a\u6210\u5458\u53d8\u91cf\u3002\u5c3d\u7ba1\u8fd9\u4e48\u505a\u4e5f\u662f\u53ef\u4ee5\u88ab\u63a5\u53d7\u7684\u7f16\u7a0b\u98ce\u683c\uff0c\u4f46\u662f\u4f7f\u7528 m_ \u4f5c\u4e3a\u6210\u5458\u53d8\u91cf\u7684\u524d\u7f00\u5c31\u53ef\u4ee5\u4ece\u6e90\u5934\u4e0a\u6d88\u9664\u8fd9\u79cd\u6b67\u4e49\u3002","title":"\u663e\u5f0f\u5730\u4f7f\u7528 this \u6307\u9488"},{"location":"13-10-the-hidden-this-pointer/#_1","text":"\u53e6\u4e00\u65b9\u9762\uff0c\u6709\u65f6\u5019\u8ba9\u7c7b\u6210\u5458\u51fd\u6570\u8fd4\u56de\u5bf9\u8c61\u672c\u8eab\u4e5f\u662f\u5f88\u6709\u7528\u7684\u3002\u8fd9\u4e48\u505a\u7684\u4e00\u4e2a\u4e3b\u8981\u539f\u56e0\u5c31\u662f\u53ef\u4ee5\u5141\u8bb8\u51fd\u6570\u7684\u94fe\u5f0f\u8c03\u7528\uff0c\u4f7f\u5f97\u591a\u4e2a\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u4f5c\u7528\u4e8e\u4e00\u4e2a\u76f8\u540c\u7684\u5bf9\u8c61\uff01\u5176\u5b9e\u4f60\u5df2\u7ecf\u591a\u6b21\u4f7f\u7528\u8be5\u7279\u6027\u4e86\u3002\u5bf9\u4e8e\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u6765\u8bf4\uff0c std::cout \u8f93\u51fa\u4e86\u4e0d\u6b62\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002 1 std :: cout << \"Hello, \" << userName ; \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c std::cout \u5176\u5b9e\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u800c\u8fd0\u7b97\u7b26 << \u662f\u8be5\u5bf9\u8c61\u7684\u4e00\u4e2a\u6210\u5458\u51fd\u6570\u3002\u7f16\u8bd1\u5668\u4f1a\u6309\u7167\u4e0b\u9762\u7684\u65b9\u5f0f\u5bf9\u4e0a\u8ff0\u4ee3\u7801\u6c42\u503c\uff1a 1 ( std :: cout << \"Hello, \" ) << userName ; \u9996\u5148\uff0c << \u4f7f\u7528 std::cout \u548c\u5b57\u7b26\u4e32\u5b57\u9762\u91cf \u201cHello, \u201d \u5c06 \u201cHello, \u201d \u6253\u5370\u5230\u7ec8\u7aef\u3002\u4e0d\u8fc7\uff0c\u8fd9\u53ea\u662f\u4e0a\u8ff0\u8868\u8fbe\u5f0f\u7684\u4e00\u90e8\u5206\uff0c << \u8fd8\u9700\u8981\u8fd4\u56de\u4e00\u4e2a\u503c\uff08\u6216void\uff09\u3002\u5982\u679c\u5b83\u8fd4\u56de\u7684\u662f void\uff0c\u5219\u7ed3\u679c\u4f1a\u53d8\u4e3a\u4e0b\u9762\u7684\u5f62\u5f0f\uff1a 1 ( void ) << userName ; \u8fd9\u663e\u7136\u4e0d\u5408\u903b\u8f91\uff08\u7f16\u8bd1\u5668\u4f1a\u629b\u51fa\u9519\u8bef\uff09\u3002\u5b9e\u9645\u4e0a\uff0c << \u7684\u8fd4\u56de\u503c\u662f *this \uff0c\u5728\u4e0a\u9762\u7684\u8bed\u5883\u4e0b\uff0cthis \u5c31\u662f std::cout \u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u5f53\u7b2c\u4e00\u4e2a << \u6267\u884c\u5b8c\u6210\u540e\uff0c\u8bed\u53e5\u7b49\u4ef7\u4e8e\u5982\u4e0b\u5f62\u5f0f\uff1a 1 ( std :: cout ) << userName ; \u4e0a\u8ff0\u4ee3\u7801\u8fdb\u800c\u4f1a\u6253\u5370\u7528\u6237\u540d\u3002 \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ea\u9700\u8981\u6307\u5b9a\u5bf9\u8c61\u4e00\u6b21\uff08\u6b64\u4f8b\u4e2d\u4e3a std::cout )\uff0c\u7136\u540e\u6bcf\u6b21\u51fd\u6570\u8c03\u7528\u90fd\u53ef\u4ee5\u628a\u5b83\u4f20\u9012\u7ed9\u63a5\u4e0b\u6765\u7684\u51fd\u6570\uff0c\u662f\u6211\u4eec\u53ef\u4ee5\u94fe\u5f0f\u6267\u884c\u591a\u4e2a\u6307\u4ee4\u3002 \u5728\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u4e2d\u6211\u4eec\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u4e0a\u8ff0\u884c\u4e3a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 class Calc { private : int m_value { 0 }; public : void add ( int value ) { m_value += value ; } void sub ( int value ) { m_value -= value ; } void mult ( int value ) { m_value *= value ; } int getValue () { return m_value ; } }; \u5982\u679c\u4f60\u5e0c\u671b\u5148\u52a05\uff0c\u518d\u51cf3\uff0c\u7136\u540e\u518d\u4e58\u4ee54\uff0c\u90a3\u4e48\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { Calc calc {}; calc . add ( 5 ); // returns void calc . sub ( 3 ); // returns void calc . mult ( 4 ); // returns void std :: cout << calc . getValue () << '\\n' ; return 0 ; } \u4e0d\u8fc7\uff0c\u5982\u679c\u6bcf\u4e2a\u51fd\u6570\u90fd\u8fd4\u56de *this \u7684\u8bdd\uff0c\u5728\u53ef\u4ee5\u94fe\u5f0f\u5730\u8c03\u7528\u5b83\u4eec\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u53ef\u94fe\u5f0f\u8c03\u7528\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class Calc { private : int m_value {}; public : Calc & add ( int value ) { m_value += value ; return * this ; } Calc & sub ( int value ) { m_value -= value ; return * this ; } Calc & mult ( int value ) { m_value *= value ; return * this ; } int getValue () { return m_value ; } }; \u6ce8\u610f\uff0c\u51fd\u6570 add() \uff0c sub() \u548c mult() \u73b0\u5728\u90fd\u8fd4\u56de *this \u3002\u8fd9\u6837\u4e00\u6765\u5c31\u53ef\u4ee5\u5411\u4e0b\u9762\u8fd9\u6837\u8c03\u7528\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { Calc calc {}; calc . add ( 5 ). sub ( 3 ). mult ( 4 ); std :: cout << calc . getValue () << '\\n' ; return 0 ; } \u4e09\u884c\u4ee3\u7801\u88ab\u538b\u7f29\u6210\u4e86\u4e00\u884c\uff01\u6211\u4eec\u518d\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002 \u9996\u5148\uff0c\u8c03\u7528 calc.add(5) \u5c06 5 \u52a0\u5230 m_value \uff0c\u7136\u540e\u8fd4\u56de *this \uff0c\u5b83\u662f calc \u7684\u5f15\u7528\uff0c\u6240\u4ee5 calc \u4f1a\u88ab\u7528\u4e8e\u8c03\u7528\u63a5\u4e0b\u6765\u7684\u51fd\u6570\u3002\u518d\u63a5\u4e0b\u6765\uff0c\u5bf9 calc.sub(3) \u6c42\u503c\uff0c\u5b83\u5c06 3 \u4ece m_value \u51cf\u53bb\u540e\uff0c\u7ee7\u7eed\u8fd4\u56de calc \u3002\u6700\u540e\uff0c calc.mult(4) \u5c06 m_value \u4e58\u4ee5\u56db\u5e76\u8fd4\u56de calc \uff0c\u7531\u4e8e\u4e0d\u9700\u8981\u7ee7\u7eed\u8c03\u7528\u51fd\u6570\uff0c\u8fd9\u4e2a\u8fd4\u56de\u503c\u4e5f\u5c31\u88ab\u5ffd\u7565\u6389\u4e86\u3002 \u56e0\u4e3a\u6bcf\u4e2a\u51fd\u6570\u90fd\u4f1a\u4fee\u6539\u5bf9\u8c61 calc \uff0c\u6240\u4ee5 calc \u7684\u6210\u5458 m_value \u7684\u503c\u6b64\u65f6\u4e3a (((0 + 5) - 3) * 4) \uff0c\u53738\u3002","title":"\u94fe\u5f0f\u8c03\u7528\u6210\u5458\u51fd\u6570"},{"location":"13-10-the-hidden-this-pointer/#_2","text":"\u201cthis\u201d \u6307\u9488\u662f\u4e00\u4e2a\u88ab\u7f16\u8bd1\u5668\u9690\u5f0f\u6dfb\u52a0\u5230\u975e\u9759\u6001\u6210\u5458\u51fd\u6570\u7684\u9690\u85cf\u53c2\u6570\u3002\u591a\u6570\u60c5\u51b5\u4e0b\u4f60\u5e76\u4e0d\u4f1a\u76f4\u63a5\u8bbf\u95ee\u5b83\uff0c\u4f46\u662f\u5f53\u6709\u9700\u8981\u65f6\u4f60\u4f1a\u7528\u7684\u7740\u7684\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cthis\u662f\u4e00\u4e2a\u5e38\u91cf\u6307\u9488\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5b83\u4fee\u6539\u5b83\u6240\u6307\u7684\u5bf9\u8c61\uff0c\u4f46\u662f\u4f60\u4e0d\u80fd\u8ba9\u5b83\u6307\u5411\u522b\u7684\u5bf9\u8c61\u3002 \u5982\u679c\u6211\u4eec\u8ba9\u539f\u672c\u8fd4\u56de void \u7684\u51fd\u6570\u8fd4\u56de *this \uff0c\u5219\u53ef\u4ee5\u4f7f\u8fd9\u4e9b\u51fd\u6570\u80fd\u591f\u88ab\u94fe\u5f0f\u8c03\u7528\u3002 \u901a\u5e38\u5728\u91cd\u8f7d\u7c7b\u7684\u8fd0\u7b97\u7b26\u65f6\u4f1a\u7528\u5230\u8fd9\u4e2a\u7279\u6027 \uff0814\u7ae0\u4e2d\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u8fd0\u7b97\u7b26\u91cd\u8f7d\uff09\u3002","title":"\u5c0f\u7ed3"},{"location":"13-11-class-code-and-header-files/","tags":["class"],"text":"Key Takeaway \u7c7b\u5185\u7684\u51fd\u6570\u662f\u5185\u8054\u7684\uff0c\u4e0d\u53d7 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u7b2c\u4e8c\u6761\u7684\u9650\u5236\uff08\u5b9a\u4e49\u5728\u7a0b\u5e8f\u4e2d\u53ea\u80fd\u51fa\u73b0\u4e00\u6b21\uff09\uff0c\u6240\u4ee5\u53ef\u4ee5\u88ab\u5b9a\u4e49\u5230\u5934\u6587\u4ef6\u4e2d\u3002 \u88ab\u5206\u79bb\u5230\u7c7b\u5916\u7684\u51fd\u6570\u5b9a\u4e49\u662f\u666e\u901a\u51fd\u6570\uff0c\u53d7\u9650\u4e8e\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\uff0c\u56e0\u6b64\u53ea\u80fd\u88ab\u5b9a\u4e49\u5728\u6e90\u6587\u4ef6\u4e2d\u3002 \u6210\u5458\u51fd\u6570\u7684\u9ed8\u8ba4\u53c2\u6570\u5e94\u8be5\u88ab\u58f0\u660e\u5728\u7c7b\u5b9a\u4e49\u4e2d\uff08\u5728\u5934\u6587\u4ef6\u5185\uff09\uff0c\u8fd9\u4e9b\u53c2\u6570\u53ef\u4ee5\u88ab\u4efb\u4f55\u5305\u542b\u4e86\u8be5\u5934\u6587\u4ef6\u7684\u4ee3\u7801\u770b\u5230 \u5728\u7c7b\u5b9a\u4e49\u7684\u5916\u90e8\u5b9a\u4e49\u7c7b\u6210\u5458\u51fd\u6570 \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u7f16\u5199\u7684\u7c7b\u90fd\u5f88\u7b80\u5355\uff0c\u6240\u4ee5\u53ef\u4ee5\u5728\u7c7b\u7684\u5b9a\u4e49\u4e2d\u76f4\u63a5\u5b9e\u73b0\u6210\u5458\u51fd\u6570\u3002 \u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e0b\u5217 Date \u7c7b\u6765\u8bf4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 class Date { private : int m_year ; int m_month ; int m_day ; public : Date ( int year , int month , int day ) { setDate ( year , month , day ); } void setDate ( int year , int month , int day ) { m_year = year ; m_month = month ; m_day = day ; } int getYear () { return m_year ; } int getMonth () { return m_month ; } int getDay () { return m_day ; } }; \u4e0d\u8fc7\uff0c\u968f\u7740\u7c7b\u8d8a\u6765\u8d8a\u590d\u6742\uff0c\u5c06\u6240\u6709\u7684\u6210\u5458\u51fd\u6570\u90fd\u76f4\u63a5\u5b9a\u4e49\u5728\u7c7b\u4e2d\u4f1a\u5bfc\u81f4\u7c7b\u96be\u4ee5\u7ef4\u62a4\u3002\u4f7f\u7528\u4e00\u4e2a\u5df2\u7ecf\u5b9a\u4e49\u597d\u7684\u7c7b\u65f6\uff0c\u53ea\u9700\u8981\u5173\u6ce8\u5176\u516c\u5171\u63a5\u53e3\uff08 \u516c\u6709\u6210\u5458 \u51fd\u6570\uff09\u5373\u53ef\uff0c\u800c\u4e0d\u9700\u8981\u4e86\u89e3\u7c7b\u7684\u5e95\u5c42\u5de5\u4f5c\u539f\u7406\u3002\u6210\u5458\u51fd\u6570\u7684\u5b9e\u73b0\u7ec6\u8282\u53ea\u4f1a\u5bf9\u6211\u4eec\u9020\u6210\u5e72\u6270\u3002 \u5e78\u8fd0\u5730\u662f\uff0cC++ \u652f\u6301\u5c06\u7c7b\u7684\u201c\u58f0\u660e\u90e8\u5206\u201d\u548c\u5b9e\u73b0\u90e8\u5206\u5206\u79bb\u3002\u8fd9\u662f\u901a\u8fc7\u5728\u7c7b\u5b9a\u4e49\u4e4b\u5916\u5b9a\u4e49\u7c7b\u6210\u5458\u51fd\u6570\u6765\u5b9e\u73b0\u7684\u3002\u8981\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u53ea\u9700\u5c06\u7c7b\u7684\u6210\u5458\u51fd\u6570\u6309\u7167\u666e\u901a\u51fd\u6570\u90a3\u6837\u5b9a\u4e49\uff0c\u4f46\u5fc5\u987b\u5728\u51fd\u6570\u524d\u6dfb\u52a0 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 (\u4e0e\u547d\u540d\u7a7a\u95f4\u76f8\u540c)\u3002 \u5bf9\u4e8e\u4e0b\u9762\u8fd9\u4e2a Data \u7c7b\u5b9a\u4e49\u6765\u8bf4\uff0c\u5176\u6784\u9020\u51fd\u6570\u548c setDate() \u51fd\u6570\u88ab\u5b9a\u4e49\u5728\u4e86\u7c7b\u5916\u90e8\u3002\u6ce8\u610f\uff0c\u51fd\u6570\u7684\u539f\u578b\u4ecd\u7136\u5728\u7c7b\u5b9a\u4e49\u4e2d\uff0c\u4f46\u662f\u5176\u5b9e\u9645\u5b9e\u73b0\u88ab\u79fb\u52a8\u5230\u4e86\u7c7b\u5916\u90e8\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 class Date { private : int m_year ; int m_month ; int m_day ; public : Date ( int year , int month , int day ); void SetDate ( int year , int month , int day ); int getYear () { return m_year ; } int getMonth () { return m_month ; } int getDay () { return m_day ; } }; // Date constructor Date :: Date ( int year , int month , int day ) { SetDate ( year , month , day ); } // Date member function void Date :: SetDate ( int year , int month , int day ) { m_month = month ; m_day = day ; m_year = year ; } \u5f88\u7b80\u5355\u5427\u3002\u5bf9\u4e8e\u8fd9\u4e2a\u7c7b\u6765\u8bf4\uff0c\u56e0\u4e3a\u8bbf\u95ee\u51fd\u6570\u901a\u5e38\u53ea\u6709\u4e00\u884c\uff0c\u6240\u4ee5\u5b83\u4eec\u901a\u5e38\u88ab\u4fdd\u7559\u5728\u7c7b\u5b9a\u4e49\u4e2d\u3002 \u5728\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4e00\u4e2a\u5177 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u6784\u9020\u51fd\u6570\u88ab\u5b9a\u4e49\u5728\u4e86\u5916\u90e8\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Calc { private : int m_value = 0 ; public : Calc ( int value = 0 ) : m_value { value } {} Calc & add ( int value ) { m_value += value ; return * this ; } Calc & sub ( int value ) { m_value -= value ; return * this ; } Calc & mult ( int value ) { m_value *= value ; return * this ; } int getValue () { return m_value ; } }; \u6539\u5199\u4e3a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 class Calc { private : int m_value = 0 ; public : Calc ( int value = 0 ); Calc & add ( int value ); Calc & sub ( int value ); Calc & mult ( int value ); int getValue () { return m_value ; } }; Calc :: Calc ( int value ) : m_value { value } { } Calc & Calc :: add ( int value ) { m_value += value ; return * this ; } Calc & Calc :: sub ( int value ) { m_value -= value ; return * this ; } Calc & Calc :: mult ( int value ) { m_value *= value ; return * this ; } \u5c06\u7c7b\u7684\u5b9a\u4e49\u653e\u7f6e\u4e8e\u5934\u6587\u4ef6\u4e2d \u5728 2.11 - \u5934\u6587\u4ef6 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u51fd\u6570\u7684\u58f0\u660e\u53ef\u4ee5\u88ab\u653e\u7f6e\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u591a\u4e2a\u6587\u4ef6\u6216\u8005\u9879\u76ee\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u3002\u5bf9\u4e8e\u7c7b\u6765\u8bf4\u4e5f\u662f\u8fd9\u6837\u7684\u3002\u7c7b\u7684\u5b9a\u4e49\u53ef\u4ee5\u88ab\u653e\u7f6e\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u505a\u540c\u6837\u6709\u52a9\u4e8e\u5728\u591a\u4e2a\u6587\u4ef6\u6216\u9879\u76ee\u4e2d\u4f7f\u7528\u8be5\u7c7b\u3002\u4e00\u822c\u6765\u8bb2\uff0c\u7c7b\u7684\u5b9a\u4e49\u4f1a\u88ab\u653e\u7f6e\u4e0e\u5176\u540c\u540d\u7684\u5934\u6587\u4ef6\u4e2d\uff0c\u800c\u5176\u6210\u5458\u51fd\u6570\u5219\u88ab\u5b9a\u4e49\u5728\u4e0e\u7c7b\u540c\u540d\u7684 .cpp \u6587\u4ef6\u4e2d\u3002 \u4e0b\u9762\u8fd9\u4e2a\u7c7b\u7684\u5b9a\u4e49\u88ab\u5206\u6563\u5728\u4e86\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u4e2d\uff1a Date.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #ifndef DATE_H #define DATE_H class Date { private : int m_year ; int m_month ; int m_day ; public : Date ( int year , int month , int day ); void SetDate ( int year , int month , int day ); int getYear () { return m_year ; } int getMonth () { return m_month ; } int getDay () { return m_day ; } }; #endif Date.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include \"Date.h\" // Date constructor Date :: Date ( int year , int month , int day ) { SetDate ( year , month , day ); } // Date member function void Date :: SetDate ( int year , int month , int day ) { m_month = month ; m_day = day ; m_year = year ; } \u73b0\u5728\uff0c\u4efb\u4f55\u5176\u4ed6\u7684\u9700\u8981\u4f7f\u7528 Date \u7c7b\u7684\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u53ea\u6709 #include \"Date.h\" \u5373\u53ef\u3002\u6ce8\u610f\uff0c Date.cpp \u5fc5\u987b\u88ab\u7f16\u8bd1\u5230\u4efb\u4f55\u4f7f\u7528\u4e86 Date.h \u7684\u9879\u76ee\u4e2d\uff0c\u8fd9\u6837\u94fe\u63a5\u5668\u624d\u80fd\u77e5\u6653 Date \u7684\u5b9e\u73b0\u3002 \u5c06\u7c7b\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u96be\u9053\u4e0d\u4f1a\u8fdd\u53cd\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u5417\uff1f \u5e76\u4e0d\u4f1a\u3002\u5982\u679c\u4f60\u7684\u5934\u6587\u4ef6\u5305\u542b\u4e86\u5408\u9002\u7684 \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e \uff0c\u90a3\u4e48\u7c7b\u7684\u5b9a\u4e49\u5e76\u4e0d\u4f1a\u88ab\u591a\u6b21\u5305\u542b\u5230\u4e00\u4e2a\u6587\u4ef6\u4e2d\u3002 \u7c7b\u578b(\u5305\u62ec\u7c7b)\u4e0d\u53d7 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u7b2c\u4e8c\u6761\u9650\u5236\uff08\u5373\u5728\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u4e00\u4e2a\u53d8\u91cf\u6216\u666e\u901a\u51fd\u6570\u53ea\u80fd\u591f\u6709\u4e00\u4e2a\u5b9a\u4e49\uff09\u3002\u56e0\u6b64\uff0c\u5c06\u7c7b\u5b9a\u4e49 #include \u7c7b\u5b9a\u4e49\u5230\u5230\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\u5e76\u4e0d\u5b58\u5728\u95ee\u9898(\u5982\u679c\u5b58\u5728\u95ee\u9898\uff0c\u7c7b\u5c31\u6ca1\u6709\u591a\u5927\u7528\u5904\u4e86)\u3002 \u5c06\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u96be\u9053\u4e0d\u4f1a\u8fdd\u53cd\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u5417\uff1f \u89c6\u60c5\u51b5\u800c\u5b9a\u3002\u5728\u7c7b\u5b9a\u4e49\u4e2d\u5b9a\u4e49\u7684\u6210\u5458\u51fd\u6570\u88ab\u8ba4\u4e3a\u662f\u9690\u5f0f\u5185\u8054\u7684\u3002 \u5185\u8054\u51fd\u6570 \u4e0d\u53d7\u5355\u5b9a\u4e49\u89c4\u5219\u4e2d\u6bcf\u4e2a\u7a0b\u5e8f\u4e00\u4e2a\u5b9a\u4e49\u90e8\u5206\u7684\u9650\u5236\u3002\u8fd9\u610f\u5473\u7740\u5728\u7c7b\u5b9a\u4e49\u672c\u8eab\u4e2d\u5b9a\u4e49\u666e\u901a\u6210\u5458\u51fd\u6570(\u5982\u8bbf\u95ee\u51fd\u6570)\u6ca1\u6709\u95ee\u9898\u3002 \u5728\u7c7b\u5b9a\u4e49\u4e4b\u5916\u5b9a\u4e49\u7684\u6210\u5458\u51fd\u6570\u88ab\u89c6\u4e3a\u666e\u901a\u51fd\u6570\uff0c\u5e76\u4e14\u670d\u4ece\u5355\u5b9a\u4e49\u89c4\u5219\u4e2d\u6bcf\u4e2a\u7a0b\u5e8f\u90e8\u5206\u7684\u4e00\u4e2a\u5b9a\u4e49\u3002 \u56e0\u6b64\uff0c\u8fd9\u4e9b\u51fd\u6570\u5e94\u8be5\u5b9a\u4e49\u5728\u4ee3\u7801\u6587\u4ef6\u4e2d\uff0c\u800c\u4e0d\u662f\u5728\u5934\u6587\u4ef6\u4e2d\u3002\u4e00\u4e2a\u4f8b\u5916\u662f\u6a21\u677f\u51fd\u6570\uff0c\u5b83\u4eec\u4e5f\u662f\u9690\u5f0f\u5185\u8054\u7684\u3002 \u4f55\u65f6\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u6216\u6e90\u6587\u4ef6\uff1f\u53c8\u4f55\u65f6\u5b9a\u4e49\u5728\u7c7b\u5185\u6216\u7c7b\u5916\uff1f \u60a8\u53ef\u80fd\u4f1a\u503e\u5411\u4e8e\u5c06\u6240\u6709\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u653e\u5728\u7c7b\u5185\u90e8\u7684\u5934\u6587\u4ef6\u4e2d\u3002\u867d\u7136\u8fd9\u6837\u505a\u53ef\u4ee5\u7f16\u8bd1\uff0c\u4f46\u662f\u8fd9\u6837\u505a\u6709\u4e00\u4e9b\u7f3a\u70b9\u3002\u9996\u5148\uff0c\u5982\u4e0a\u6240\u8ff0\uff0c\u8fd9\u4f1a\u4f7f\u7c7b\u5b9a\u4e49\u53d8\u5f97\u6df7\u4e71\u3002\u5176\u6b21\uff0c\u5982\u679c\u66f4\u6539\u4e86\u5934\u6587\u4ef6\u4e2d\u7684\u4efb\u4f55\u4ee3\u7801\uff0c\u5219\u9700\u8981\u91cd\u65b0\u7f16\u8bd1\u5305\u542b\u8be5\u5934\u6587\u4ef6\u7684\u6bcf\u4e2a\u6587\u4ef6\u3002\u8fd9\u53ef\u80fd\u4f1a\u4ea7\u751f\u8fde\u9501\u53cd\u5e94\uff0c\u4e00\u4e2a\u5fae\u5c0f\u7684\u66f4\u6539\u5c31\u4f1a\u5bfc\u81f4\u6574\u4e2a\u7a0b\u5e8f\u9700\u8981\u91cd\u65b0\u7f16\u8bd1(\u8fd9\u53ef\u80fd\u4f1a\u5f88\u6162)\u3002\u5982\u679c\u60a8\u66f4\u6539\u4e86.cpp\u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\uff0c\u5219\u53ea\u9700\u8981\u91cd\u65b0\u7f16\u8bd1 .cpp \u6587\u4ef6\uff01 \u56e0\u6b64\u6211\u4eec\u63a8\u8350\u4e0b\u9762\u7684\u505a\u6cd5\uff1a \u5bf9\u4e8e\u53ea\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528\u800c\u4e0d\u9700\u8981\u91cd\u7528\u7684\u7c7b\u6765\u8bf4\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u4f7f\u7528\u5b83\u4eec\u7684 .cpp \u6587\u4ef6\u4e2d\u5b9a\u4e49\uff1b \u5bf9\u4e8e\u4f1a\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528\u6216\u8005\u671f\u671b\u88ab\u91cd\u7528\u7684\u7c7b\u6765\u8bf4\uff0c\u5c06\u5b83\u4eec\u5b9a\u4e49\u5728\u540c\u540d\u5934\u6587\u4ef6\u4e2d\u3002 \u5bf9\u4e8e\u7b80\u5355\u7684\u6210\u5458\u51fd\u6570\uff08\u7b80\u5355\u7684\u6784\u9020\u51fd\u6570\u3001\u6790\u6784\u51fd\u6570\u6216\u6210\u5458\u8bbf\u95ee\u51fd\u6570\uff09\u6765\u8bf4\uff0c\u5b83\u4eec\u53ef\u4ee5\u88ab\u5b9a\u4e49\u5728\u7c7b\u4e2d\uff1b \u5bf9\u4e8e\u8f83\u590d\u6742\u7684\u6210\u5458\u51fd\u6570\u6765\u8bf4\uff0c\u5b83\u4eec\u5e94\u8be5\u88ab\u5b9a\u4e49\u5728\u4e0e\u7c7b\u540c\u540d\u7684 cpp \u6587\u4ef6\u4e2d\u3002 \u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\uff0c\u5927\u591a\u6570\u7684\u7c7b\u90fd\u5c06\u88ab\u5b9a\u4e49\u5728 .cpp \u6587\u4ef6\u4e2d\uff0c\u540c\u65f6\u6240\u6709\u51fd\u6570\u90fd\u4f1a\u88ab\u76f4\u63a5\u5b9a\u4e49\u5728\u7c7b\u5185\u3002\u8fd9\u53ea\u662f\u4e3a\u4e86\u65b9\u4fbf\uff0c\u540c\u65f6\u53ef\u4ee5\u4fdd\u6301\u793a\u4f8b\u7b80\u77ed\u3002\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u5c06\u7c7b\u653e\u5728\u5b83\u4eec\u81ea\u5df1\u7684\u4ee3\u7801\u548c\u5934\u6587\u4ef6\u4e2d\u8981\u5e38\u89c1\u5f97\u591a\uff0c\u4f60\u5e94\u8be5\u4e60\u60ef\u4e8e\u8fd9\u6837\u505a\u3002 \u9ed8\u8ba4\u53c2\u6570 \u6210\u5458\u51fd\u6570\u7684\u9ed8\u8ba4\u53c2\u6570\u5e94\u8be5\u88ab\u58f0\u660e\u5728\u7c7b\u5b9a\u4e49\u4e2d\uff08\u5728\u5934\u6587\u4ef6\u5185\uff09\uff0c\u8fd9\u4e9b\u53c2\u6570\u53ef\u4ee5\u88ab\u4efb\u4f55\u5305\u542b\u4e86\u8be5\u5934\u6587\u4ef6\u7684\u4ee3\u7801\u770b\u5230\u3002 \u5e93 \u5206\u79bb\u7c7b\u5b9a\u4e49\u548c\u7c7b\u5b9e\u73b0\u5bf9\u4e8e\u5e93\u6765\u8bf4\u662f\u975e\u5e38\u5e38\u89c1\u7684\u3002\u5728\u7f16\u5199\u7a0b\u5e8f\u65f6\uff0c\u7a0b\u5e8f\u4e2d\u4f1a\u5b58\u5728\u5f88\u591a\u88ab #included \u6807\u51c6\u5e93\u7684\u5934\u6587\u4ef6\uff0c\u5982 iostream \u3001 string \u3001 vector \u3001 array \u7b49\u3002\u6ce8\u610f\uff0c\u4f60\u4e0d\u9700\u8981\u5728\u4f60\u7684\u9879\u76ee\u4e2d\u6dfb\u52a0 iostream.cpp , string.cpp , vector.cpp \u6216 array.cpp \u3002\u4f60\u7684\u7a0b\u5e8f\u9700\u8981\u6765\u81ea\u5934\u6587\u4ef6\u7684\u58f0\u660e\uff0c\u4ee5\u4fbf\u7f16\u8bd1\u5668\u9a8c\u8bc1\u4f60\u5199\u7684\u7a0b\u5e8f\u662f\u8bed\u6cd5\u6b63\u786e\u7684\u3002\u4f46\u662f\uff0c\u5c5e\u4e8eC++\u6807\u51c6\u5e93\u7684\u7c7b\u7684 \u5b9e\u73b0 \u5305\u542b\u5728\u9884\u7f16\u8bd1\u6587\u4ef6\u4e2d\uff0c\u8be5\u6587\u4ef6\u4f1a\u5728\u94fe\u63a5\u9636\u6bb5\u88ab\u94fe\u63a5\u8fdb\u53bb\u3002\u4f60\u6c38\u8fdc\u770b\u4e0d\u5230\u4ee3\u7801\u3002 \u9664\u4e86\u4e00\u4e9b\u5f00\u653e\u6e90\u7801\u8f6f\u4ef6(\u5176\u4e2d\u63d0\u4f9b .h \u548c .cpp \u6587\u4ef6)\u4e4b\u5916\uff0c\u5927\u591a\u6570\u7b2c\u4e09\u65b9\u5e93\u53ea\u63d0\u4f9b\u5934\u6587\u4ef6\u4ee5\u53ca\u9884\u7f16\u8bd1\u7684\u5e93\u6587\u4ef6\u3002\u8fd9\u6709\u51e0\u4e2a\u539f\u56e0\uff1a 1. \u94fe\u63a5\u9884\u7f16\u8bd1\u5e93\u6bd4\u6bcf\u6b21\u9700\u8981\u65f6\u91cd\u65b0\u7f16\u8bd1\u5b83\u66f4\u5feb\uff1b 2. \u9884\u7f16\u8bd1\u5e93\u7684\u4e00\u4e2a\u526f\u672c\u53ef\u4ee5\u88ab\u8bb8\u591a\u5e94\u7528\u7a0b\u5e8f\u5171\u4eab\uff0c\u800c\u7f16\u8bd1\u540e\u7684\u4ee3\u7801\u88ab\u7f16\u8bd1\u5230\u6bcf\u4e2a\u4f7f\u7528\u5b83\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4e2d(\u81a8\u80c0\u6587\u4ef6\u5927\u5c0f)\uff1b 3. \u77e5\u8bc6\u4ea7\u6743\u539f\u56e0(\u4f60\u4e0d\u5e0c\u671b\u522b\u4eba\u7a83\u53d6\u4f60\u7684\u4ee3\u7801)\u3002 \u5c06\u81ea\u5df1\u7684\u6587\u4ef6\u5206\u79bb\u4e3a\u58f0\u660e(\u5934\u6587\u4ef6)\u548c\u5b9e\u73b0(\u4ee3\u7801\u6587\u4ef6)\u4e0d\u4ec5\u662f\u4e00\u79cd\u5f88\u597d\u7684\u5f62\u5f0f\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u521b\u5efa\u81ea\u5df1\u7684\u81ea\u5b9a\u4e49\u5e93\u66f4\u5bb9\u6613\u3002\u521b\u5efa\u81ea\u5df1\u7684\u5e93\u8d85\u51fa\u4e86\u672c\u6559\u7a0b\u7684\u8303\u56f4\uff0c\u4f46\u662f\u5c06\u58f0\u660e\u548c\u5b9e\u73b0\u5206\u79bb\u662f\u8fd9\u6837\u505a\u7684\u5148\u51b3\u6761\u4ef6\u3002","title":"13.11 - \u7c7b\u4ee3\u7801\u548c\u5934\u6587\u4ef6"},{"location":"13-11-class-code-and-header-files/#_1","text":"\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u7f16\u5199\u7684\u7c7b\u90fd\u5f88\u7b80\u5355\uff0c\u6240\u4ee5\u53ef\u4ee5\u5728\u7c7b\u7684\u5b9a\u4e49\u4e2d\u76f4\u63a5\u5b9e\u73b0\u6210\u5458\u51fd\u6570\u3002 \u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e0b\u5217 Date \u7c7b\u6765\u8bf4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 class Date { private : int m_year ; int m_month ; int m_day ; public : Date ( int year , int month , int day ) { setDate ( year , month , day ); } void setDate ( int year , int month , int day ) { m_year = year ; m_month = month ; m_day = day ; } int getYear () { return m_year ; } int getMonth () { return m_month ; } int getDay () { return m_day ; } }; \u4e0d\u8fc7\uff0c\u968f\u7740\u7c7b\u8d8a\u6765\u8d8a\u590d\u6742\uff0c\u5c06\u6240\u6709\u7684\u6210\u5458\u51fd\u6570\u90fd\u76f4\u63a5\u5b9a\u4e49\u5728\u7c7b\u4e2d\u4f1a\u5bfc\u81f4\u7c7b\u96be\u4ee5\u7ef4\u62a4\u3002\u4f7f\u7528\u4e00\u4e2a\u5df2\u7ecf\u5b9a\u4e49\u597d\u7684\u7c7b\u65f6\uff0c\u53ea\u9700\u8981\u5173\u6ce8\u5176\u516c\u5171\u63a5\u53e3\uff08 \u516c\u6709\u6210\u5458 \u51fd\u6570\uff09\u5373\u53ef\uff0c\u800c\u4e0d\u9700\u8981\u4e86\u89e3\u7c7b\u7684\u5e95\u5c42\u5de5\u4f5c\u539f\u7406\u3002\u6210\u5458\u51fd\u6570\u7684\u5b9e\u73b0\u7ec6\u8282\u53ea\u4f1a\u5bf9\u6211\u4eec\u9020\u6210\u5e72\u6270\u3002 \u5e78\u8fd0\u5730\u662f\uff0cC++ \u652f\u6301\u5c06\u7c7b\u7684\u201c\u58f0\u660e\u90e8\u5206\u201d\u548c\u5b9e\u73b0\u90e8\u5206\u5206\u79bb\u3002\u8fd9\u662f\u901a\u8fc7\u5728\u7c7b\u5b9a\u4e49\u4e4b\u5916\u5b9a\u4e49\u7c7b\u6210\u5458\u51fd\u6570\u6765\u5b9e\u73b0\u7684\u3002\u8981\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u53ea\u9700\u5c06\u7c7b\u7684\u6210\u5458\u51fd\u6570\u6309\u7167\u666e\u901a\u51fd\u6570\u90a3\u6837\u5b9a\u4e49\uff0c\u4f46\u5fc5\u987b\u5728\u51fd\u6570\u524d\u6dfb\u52a0 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 (\u4e0e\u547d\u540d\u7a7a\u95f4\u76f8\u540c)\u3002 \u5bf9\u4e8e\u4e0b\u9762\u8fd9\u4e2a Data \u7c7b\u5b9a\u4e49\u6765\u8bf4\uff0c\u5176\u6784\u9020\u51fd\u6570\u548c setDate() \u51fd\u6570\u88ab\u5b9a\u4e49\u5728\u4e86\u7c7b\u5916\u90e8\u3002\u6ce8\u610f\uff0c\u51fd\u6570\u7684\u539f\u578b\u4ecd\u7136\u5728\u7c7b\u5b9a\u4e49\u4e2d\uff0c\u4f46\u662f\u5176\u5b9e\u9645\u5b9e\u73b0\u88ab\u79fb\u52a8\u5230\u4e86\u7c7b\u5916\u90e8\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 class Date { private : int m_year ; int m_month ; int m_day ; public : Date ( int year , int month , int day ); void SetDate ( int year , int month , int day ); int getYear () { return m_year ; } int getMonth () { return m_month ; } int getDay () { return m_day ; } }; // Date constructor Date :: Date ( int year , int month , int day ) { SetDate ( year , month , day ); } // Date member function void Date :: SetDate ( int year , int month , int day ) { m_month = month ; m_day = day ; m_year = year ; } \u5f88\u7b80\u5355\u5427\u3002\u5bf9\u4e8e\u8fd9\u4e2a\u7c7b\u6765\u8bf4\uff0c\u56e0\u4e3a\u8bbf\u95ee\u51fd\u6570\u901a\u5e38\u53ea\u6709\u4e00\u884c\uff0c\u6240\u4ee5\u5b83\u4eec\u901a\u5e38\u88ab\u4fdd\u7559\u5728\u7c7b\u5b9a\u4e49\u4e2d\u3002 \u5728\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4e00\u4e2a\u5177 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u6784\u9020\u51fd\u6570\u88ab\u5b9a\u4e49\u5728\u4e86\u5916\u90e8\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Calc { private : int m_value = 0 ; public : Calc ( int value = 0 ) : m_value { value } {} Calc & add ( int value ) { m_value += value ; return * this ; } Calc & sub ( int value ) { m_value -= value ; return * this ; } Calc & mult ( int value ) { m_value *= value ; return * this ; } int getValue () { return m_value ; } }; \u6539\u5199\u4e3a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 class Calc { private : int m_value = 0 ; public : Calc ( int value = 0 ); Calc & add ( int value ); Calc & sub ( int value ); Calc & mult ( int value ); int getValue () { return m_value ; } }; Calc :: Calc ( int value ) : m_value { value } { } Calc & Calc :: add ( int value ) { m_value += value ; return * this ; } Calc & Calc :: sub ( int value ) { m_value -= value ; return * this ; } Calc & Calc :: mult ( int value ) { m_value *= value ; return * this ; }","title":"\u5728\u7c7b\u5b9a\u4e49\u7684\u5916\u90e8\u5b9a\u4e49\u7c7b\u6210\u5458\u51fd\u6570"},{"location":"13-11-class-code-and-header-files/#_2","text":"\u5728 2.11 - \u5934\u6587\u4ef6 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u51fd\u6570\u7684\u58f0\u660e\u53ef\u4ee5\u88ab\u653e\u7f6e\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u591a\u4e2a\u6587\u4ef6\u6216\u8005\u9879\u76ee\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u3002\u5bf9\u4e8e\u7c7b\u6765\u8bf4\u4e5f\u662f\u8fd9\u6837\u7684\u3002\u7c7b\u7684\u5b9a\u4e49\u53ef\u4ee5\u88ab\u653e\u7f6e\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u505a\u540c\u6837\u6709\u52a9\u4e8e\u5728\u591a\u4e2a\u6587\u4ef6\u6216\u9879\u76ee\u4e2d\u4f7f\u7528\u8be5\u7c7b\u3002\u4e00\u822c\u6765\u8bb2\uff0c\u7c7b\u7684\u5b9a\u4e49\u4f1a\u88ab\u653e\u7f6e\u4e0e\u5176\u540c\u540d\u7684\u5934\u6587\u4ef6\u4e2d\uff0c\u800c\u5176\u6210\u5458\u51fd\u6570\u5219\u88ab\u5b9a\u4e49\u5728\u4e0e\u7c7b\u540c\u540d\u7684 .cpp \u6587\u4ef6\u4e2d\u3002 \u4e0b\u9762\u8fd9\u4e2a\u7c7b\u7684\u5b9a\u4e49\u88ab\u5206\u6563\u5728\u4e86\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u4e2d\uff1a Date.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #ifndef DATE_H #define DATE_H class Date { private : int m_year ; int m_month ; int m_day ; public : Date ( int year , int month , int day ); void SetDate ( int year , int month , int day ); int getYear () { return m_year ; } int getMonth () { return m_month ; } int getDay () { return m_day ; } }; #endif Date.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include \"Date.h\" // Date constructor Date :: Date ( int year , int month , int day ) { SetDate ( year , month , day ); } // Date member function void Date :: SetDate ( int year , int month , int day ) { m_month = month ; m_day = day ; m_year = year ; } \u73b0\u5728\uff0c\u4efb\u4f55\u5176\u4ed6\u7684\u9700\u8981\u4f7f\u7528 Date \u7c7b\u7684\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u53ea\u6709 #include \"Date.h\" \u5373\u53ef\u3002\u6ce8\u610f\uff0c Date.cpp \u5fc5\u987b\u88ab\u7f16\u8bd1\u5230\u4efb\u4f55\u4f7f\u7528\u4e86 Date.h \u7684\u9879\u76ee\u4e2d\uff0c\u8fd9\u6837\u94fe\u63a5\u5668\u624d\u80fd\u77e5\u6653 Date \u7684\u5b9e\u73b0\u3002","title":"\u5c06\u7c7b\u7684\u5b9a\u4e49\u653e\u7f6e\u4e8e\u5934\u6587\u4ef6\u4e2d"},{"location":"13-11-class-code-and-header-files/#_3","text":"\u5e76\u4e0d\u4f1a\u3002\u5982\u679c\u4f60\u7684\u5934\u6587\u4ef6\u5305\u542b\u4e86\u5408\u9002\u7684 \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e \uff0c\u90a3\u4e48\u7c7b\u7684\u5b9a\u4e49\u5e76\u4e0d\u4f1a\u88ab\u591a\u6b21\u5305\u542b\u5230\u4e00\u4e2a\u6587\u4ef6\u4e2d\u3002 \u7c7b\u578b(\u5305\u62ec\u7c7b)\u4e0d\u53d7 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u7b2c\u4e8c\u6761\u9650\u5236\uff08\u5373\u5728\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u4e00\u4e2a\u53d8\u91cf\u6216\u666e\u901a\u51fd\u6570\u53ea\u80fd\u591f\u6709\u4e00\u4e2a\u5b9a\u4e49\uff09\u3002\u56e0\u6b64\uff0c\u5c06\u7c7b\u5b9a\u4e49 #include \u7c7b\u5b9a\u4e49\u5230\u5230\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\u5e76\u4e0d\u5b58\u5728\u95ee\u9898(\u5982\u679c\u5b58\u5728\u95ee\u9898\uff0c\u7c7b\u5c31\u6ca1\u6709\u591a\u5927\u7528\u5904\u4e86)\u3002","title":"\u5c06\u7c7b\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u96be\u9053\u4e0d\u4f1a\u8fdd\u53cd\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u5417\uff1f"},{"location":"13-11-class-code-and-header-files/#_4","text":"\u89c6\u60c5\u51b5\u800c\u5b9a\u3002\u5728\u7c7b\u5b9a\u4e49\u4e2d\u5b9a\u4e49\u7684\u6210\u5458\u51fd\u6570\u88ab\u8ba4\u4e3a\u662f\u9690\u5f0f\u5185\u8054\u7684\u3002 \u5185\u8054\u51fd\u6570 \u4e0d\u53d7\u5355\u5b9a\u4e49\u89c4\u5219\u4e2d\u6bcf\u4e2a\u7a0b\u5e8f\u4e00\u4e2a\u5b9a\u4e49\u90e8\u5206\u7684\u9650\u5236\u3002\u8fd9\u610f\u5473\u7740\u5728\u7c7b\u5b9a\u4e49\u672c\u8eab\u4e2d\u5b9a\u4e49\u666e\u901a\u6210\u5458\u51fd\u6570(\u5982\u8bbf\u95ee\u51fd\u6570)\u6ca1\u6709\u95ee\u9898\u3002 \u5728\u7c7b\u5b9a\u4e49\u4e4b\u5916\u5b9a\u4e49\u7684\u6210\u5458\u51fd\u6570\u88ab\u89c6\u4e3a\u666e\u901a\u51fd\u6570\uff0c\u5e76\u4e14\u670d\u4ece\u5355\u5b9a\u4e49\u89c4\u5219\u4e2d\u6bcf\u4e2a\u7a0b\u5e8f\u90e8\u5206\u7684\u4e00\u4e2a\u5b9a\u4e49\u3002 \u56e0\u6b64\uff0c\u8fd9\u4e9b\u51fd\u6570\u5e94\u8be5\u5b9a\u4e49\u5728\u4ee3\u7801\u6587\u4ef6\u4e2d\uff0c\u800c\u4e0d\u662f\u5728\u5934\u6587\u4ef6\u4e2d\u3002\u4e00\u4e2a\u4f8b\u5916\u662f\u6a21\u677f\u51fd\u6570\uff0c\u5b83\u4eec\u4e5f\u662f\u9690\u5f0f\u5185\u8054\u7684\u3002","title":"\u5c06\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u96be\u9053\u4e0d\u4f1a\u8fdd\u53cd\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u5417\uff1f"},{"location":"13-11-class-code-and-header-files/#_5","text":"\u60a8\u53ef\u80fd\u4f1a\u503e\u5411\u4e8e\u5c06\u6240\u6709\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u653e\u5728\u7c7b\u5185\u90e8\u7684\u5934\u6587\u4ef6\u4e2d\u3002\u867d\u7136\u8fd9\u6837\u505a\u53ef\u4ee5\u7f16\u8bd1\uff0c\u4f46\u662f\u8fd9\u6837\u505a\u6709\u4e00\u4e9b\u7f3a\u70b9\u3002\u9996\u5148\uff0c\u5982\u4e0a\u6240\u8ff0\uff0c\u8fd9\u4f1a\u4f7f\u7c7b\u5b9a\u4e49\u53d8\u5f97\u6df7\u4e71\u3002\u5176\u6b21\uff0c\u5982\u679c\u66f4\u6539\u4e86\u5934\u6587\u4ef6\u4e2d\u7684\u4efb\u4f55\u4ee3\u7801\uff0c\u5219\u9700\u8981\u91cd\u65b0\u7f16\u8bd1\u5305\u542b\u8be5\u5934\u6587\u4ef6\u7684\u6bcf\u4e2a\u6587\u4ef6\u3002\u8fd9\u53ef\u80fd\u4f1a\u4ea7\u751f\u8fde\u9501\u53cd\u5e94\uff0c\u4e00\u4e2a\u5fae\u5c0f\u7684\u66f4\u6539\u5c31\u4f1a\u5bfc\u81f4\u6574\u4e2a\u7a0b\u5e8f\u9700\u8981\u91cd\u65b0\u7f16\u8bd1(\u8fd9\u53ef\u80fd\u4f1a\u5f88\u6162)\u3002\u5982\u679c\u60a8\u66f4\u6539\u4e86.cpp\u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\uff0c\u5219\u53ea\u9700\u8981\u91cd\u65b0\u7f16\u8bd1 .cpp \u6587\u4ef6\uff01 \u56e0\u6b64\u6211\u4eec\u63a8\u8350\u4e0b\u9762\u7684\u505a\u6cd5\uff1a \u5bf9\u4e8e\u53ea\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528\u800c\u4e0d\u9700\u8981\u91cd\u7528\u7684\u7c7b\u6765\u8bf4\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u4f7f\u7528\u5b83\u4eec\u7684 .cpp \u6587\u4ef6\u4e2d\u5b9a\u4e49\uff1b \u5bf9\u4e8e\u4f1a\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528\u6216\u8005\u671f\u671b\u88ab\u91cd\u7528\u7684\u7c7b\u6765\u8bf4\uff0c\u5c06\u5b83\u4eec\u5b9a\u4e49\u5728\u540c\u540d\u5934\u6587\u4ef6\u4e2d\u3002 \u5bf9\u4e8e\u7b80\u5355\u7684\u6210\u5458\u51fd\u6570\uff08\u7b80\u5355\u7684\u6784\u9020\u51fd\u6570\u3001\u6790\u6784\u51fd\u6570\u6216\u6210\u5458\u8bbf\u95ee\u51fd\u6570\uff09\u6765\u8bf4\uff0c\u5b83\u4eec\u53ef\u4ee5\u88ab\u5b9a\u4e49\u5728\u7c7b\u4e2d\uff1b \u5bf9\u4e8e\u8f83\u590d\u6742\u7684\u6210\u5458\u51fd\u6570\u6765\u8bf4\uff0c\u5b83\u4eec\u5e94\u8be5\u88ab\u5b9a\u4e49\u5728\u4e0e\u7c7b\u540c\u540d\u7684 cpp \u6587\u4ef6\u4e2d\u3002 \u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\uff0c\u5927\u591a\u6570\u7684\u7c7b\u90fd\u5c06\u88ab\u5b9a\u4e49\u5728 .cpp \u6587\u4ef6\u4e2d\uff0c\u540c\u65f6\u6240\u6709\u51fd\u6570\u90fd\u4f1a\u88ab\u76f4\u63a5\u5b9a\u4e49\u5728\u7c7b\u5185\u3002\u8fd9\u53ea\u662f\u4e3a\u4e86\u65b9\u4fbf\uff0c\u540c\u65f6\u53ef\u4ee5\u4fdd\u6301\u793a\u4f8b\u7b80\u77ed\u3002\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u5c06\u7c7b\u653e\u5728\u5b83\u4eec\u81ea\u5df1\u7684\u4ee3\u7801\u548c\u5934\u6587\u4ef6\u4e2d\u8981\u5e38\u89c1\u5f97\u591a\uff0c\u4f60\u5e94\u8be5\u4e60\u60ef\u4e8e\u8fd9\u6837\u505a\u3002","title":"\u4f55\u65f6\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u6216\u6e90\u6587\u4ef6\uff1f\u53c8\u4f55\u65f6\u5b9a\u4e49\u5728\u7c7b\u5185\u6216\u7c7b\u5916\uff1f"},{"location":"13-11-class-code-and-header-files/#_6","text":"\u6210\u5458\u51fd\u6570\u7684\u9ed8\u8ba4\u53c2\u6570\u5e94\u8be5\u88ab\u58f0\u660e\u5728\u7c7b\u5b9a\u4e49\u4e2d\uff08\u5728\u5934\u6587\u4ef6\u5185\uff09\uff0c\u8fd9\u4e9b\u53c2\u6570\u53ef\u4ee5\u88ab\u4efb\u4f55\u5305\u542b\u4e86\u8be5\u5934\u6587\u4ef6\u7684\u4ee3\u7801\u770b\u5230\u3002","title":"\u9ed8\u8ba4\u53c2\u6570"},{"location":"13-11-class-code-and-header-files/#_7","text":"\u5206\u79bb\u7c7b\u5b9a\u4e49\u548c\u7c7b\u5b9e\u73b0\u5bf9\u4e8e\u5e93\u6765\u8bf4\u662f\u975e\u5e38\u5e38\u89c1\u7684\u3002\u5728\u7f16\u5199\u7a0b\u5e8f\u65f6\uff0c\u7a0b\u5e8f\u4e2d\u4f1a\u5b58\u5728\u5f88\u591a\u88ab #included \u6807\u51c6\u5e93\u7684\u5934\u6587\u4ef6\uff0c\u5982 iostream \u3001 string \u3001 vector \u3001 array \u7b49\u3002\u6ce8\u610f\uff0c\u4f60\u4e0d\u9700\u8981\u5728\u4f60\u7684\u9879\u76ee\u4e2d\u6dfb\u52a0 iostream.cpp , string.cpp , vector.cpp \u6216 array.cpp \u3002\u4f60\u7684\u7a0b\u5e8f\u9700\u8981\u6765\u81ea\u5934\u6587\u4ef6\u7684\u58f0\u660e\uff0c\u4ee5\u4fbf\u7f16\u8bd1\u5668\u9a8c\u8bc1\u4f60\u5199\u7684\u7a0b\u5e8f\u662f\u8bed\u6cd5\u6b63\u786e\u7684\u3002\u4f46\u662f\uff0c\u5c5e\u4e8eC++\u6807\u51c6\u5e93\u7684\u7c7b\u7684 \u5b9e\u73b0 \u5305\u542b\u5728\u9884\u7f16\u8bd1\u6587\u4ef6\u4e2d\uff0c\u8be5\u6587\u4ef6\u4f1a\u5728\u94fe\u63a5\u9636\u6bb5\u88ab\u94fe\u63a5\u8fdb\u53bb\u3002\u4f60\u6c38\u8fdc\u770b\u4e0d\u5230\u4ee3\u7801\u3002 \u9664\u4e86\u4e00\u4e9b\u5f00\u653e\u6e90\u7801\u8f6f\u4ef6(\u5176\u4e2d\u63d0\u4f9b .h \u548c .cpp \u6587\u4ef6)\u4e4b\u5916\uff0c\u5927\u591a\u6570\u7b2c\u4e09\u65b9\u5e93\u53ea\u63d0\u4f9b\u5934\u6587\u4ef6\u4ee5\u53ca\u9884\u7f16\u8bd1\u7684\u5e93\u6587\u4ef6\u3002\u8fd9\u6709\u51e0\u4e2a\u539f\u56e0\uff1a 1. \u94fe\u63a5\u9884\u7f16\u8bd1\u5e93\u6bd4\u6bcf\u6b21\u9700\u8981\u65f6\u91cd\u65b0\u7f16\u8bd1\u5b83\u66f4\u5feb\uff1b 2. \u9884\u7f16\u8bd1\u5e93\u7684\u4e00\u4e2a\u526f\u672c\u53ef\u4ee5\u88ab\u8bb8\u591a\u5e94\u7528\u7a0b\u5e8f\u5171\u4eab\uff0c\u800c\u7f16\u8bd1\u540e\u7684\u4ee3\u7801\u88ab\u7f16\u8bd1\u5230\u6bcf\u4e2a\u4f7f\u7528\u5b83\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4e2d(\u81a8\u80c0\u6587\u4ef6\u5927\u5c0f)\uff1b 3. \u77e5\u8bc6\u4ea7\u6743\u539f\u56e0(\u4f60\u4e0d\u5e0c\u671b\u522b\u4eba\u7a83\u53d6\u4f60\u7684\u4ee3\u7801)\u3002 \u5c06\u81ea\u5df1\u7684\u6587\u4ef6\u5206\u79bb\u4e3a\u58f0\u660e(\u5934\u6587\u4ef6)\u548c\u5b9e\u73b0(\u4ee3\u7801\u6587\u4ef6)\u4e0d\u4ec5\u662f\u4e00\u79cd\u5f88\u597d\u7684\u5f62\u5f0f\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u521b\u5efa\u81ea\u5df1\u7684\u81ea\u5b9a\u4e49\u5e93\u66f4\u5bb9\u6613\u3002\u521b\u5efa\u81ea\u5df1\u7684\u5e93\u8d85\u51fa\u4e86\u672c\u6559\u7a0b\u7684\u8303\u56f4\uff0c\u4f46\u662f\u5c06\u58f0\u660e\u548c\u5b9e\u73b0\u5206\u79bb\u662f\u8fd9\u6837\u505a\u7684\u5148\u51b3\u6761\u4ef6\u3002","title":"\u5e93"},{"location":"13-12-const-class-objects-and-member-functions/","tags":["class","const class"],"text":"Key Takeaway const \u7c7b\u578b\u7684\u5bf9\u8c61\u53ea\u80fd\u8c03\u7528 const \u6210\u5458\u51fd\u6570\u3002 const \u6210\u5458\u51fd\u6570\u53ef\u4ee5\u88ab const \u6216 \u975econst \u7c7b\u578b\u7684\u5bf9\u8c61\u8c03\u7528 \u901a\u8fc7\u5b66\u4e60 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \uff0c\u6211\u4eec\u77e5\u9053\u57fa\u672c\u6570\u636e\u7c7b\u578b( int , double , char \u7b49)\uff0c\u53ef\u4ee5\u901a\u8fc7 const \u5173\u952e\u5b57\u88ab\u5b9a\u4e49\u4e3aconst\u7c7b\u578b\u3002\u540c\u65f6\u6240\u6709\u7684const\u53d8\u91cf\u90fd\u5fc5\u987b\u5728\u521b\u5efa\u65f6\u88ab\u521d\u59cb\u5316\u3002 \u5bf9\u4e8e const \u7c7b\u578b\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u521d\u59cb\u5316\u53ef\u4ee5\u901a\u8fc7 \u62f7\u8d1d\u521d\u59cb\u5316 \u3001 \u76f4\u63a5\u521d\u59cb\u5316 \u6216 \u7edf\u4e00\u521d\u59cb\u5316 \u6765\u5b8c\u6210\uff1a 1 2 3 const int value1 = 5 ; // \u62f7\u8d1d\u521d\u59cb\u5316 const int value2 ( 7 ); // \u76f4\u63a5\u521d\u59cb\u5316 const int value3 { 9 }; // \u7edf\u4e00\u521d\u59cb\u5316 (C++11) const \u7c7b \u540c\u6837\u7684\uff0c\u88ab \u5b9e\u4f8b\u5316 \u7684\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u901a\u8fc7const\u5173\u952e\u5b57\u88ab\u521b\u5efa\u4e3a const \u7c7b\u578b\u3002\u521d\u59cb\u5316\u5219\u662f\u901a\u8fc7 \u6784\u9020\u51fd\u6570 \u5b8c\u6210\u7684\uff1a 1 2 3 const Date date1 ; // \u4f7f\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316 const Date date2 ( 2020 , 10 , 16 ); // \u4f7f\u7528\u5e26\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u8fdb\u884c\u521d\u59cb\u5316 const Date date3 { 2020 , 10 , 16 }; // \u4f7f\u7528\u5e26\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u8fdb\u884c\u521d\u59cb\u5316(C++11) const \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u4e00\u65e6\u901a\u8fc7\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\uff0c\u5176\u4efb\u4f55\u6210\u5458\u53d8\u91cf\u90fd\u4e0d\u518d\u53ef\u4ee5\u88ab\u4fee\u6539\uff0c\u5426\u5219\u663e\u7136\u8fdd\u80cc\u4e86\u5176\u201c\u5e38\u91cf\u6027\u201d\u3002\u4e0d\u8bba\u662f\u76f4\u63a5\u4fee\u6539 \u516c\u6709\u6210\u5458 \u8fd8\u662f\u901a\u8fc7\u516c\u6709\u6210\u5458\u51fd\u6570\u4e3a\u6210\u5458\u53d8\u91cf\u8d4b\u503c\u90fd\u662f\u4e0d\u53ef\u4ee5\u7684\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Something { public : int m_value {}; Something () : m_value { 0 } { } void setValue ( int value ) { m_value = value ; } int getValue () { return m_value ; } }; int main () { const Something something {}; // calls default constructor something . m_value = 5 ; // compiler error: violates const something . setValue ( 5 ); // compiler error: violates const return 0 ; } \u4e0a\u9762\u6d89\u53ca\u53d8\u91cf something \u7684\u4e24\u884c\u90fd\u662f\u975e\u6cd5\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u8981\u4e48\u8bd5\u56fe\u76f4\u63a5\u66f4\u6539\u6210\u5458\u53d8\u91cf\uff0c\u8981\u4e48\u8c03\u7528\u8bd5\u56fe\u66f4\u6539\u6210\u5458\u53d8\u91cf\u7684\u6210\u5458\u51fd\u6570\uff0c\u4ece\u800c\u8fdd\u53cd\u4e86 something \u7684\u5e38\u91cf\u6027\u3002 \u4e0e\u666e\u901a\u53d8\u91cf\u4e00\u6837\uff0c\u5f53\u9700\u8981\u786e\u4fdd\u7c7b\u5bf9\u8c61\u5728\u521b\u5efa\u540e\u4e0d\u518d\u88ab\u4fee\u6539\u65f6\uff0c\u53ef\u4ee5\u5c06\u5b83\u4eec\u8bbe\u7f6e\u4e3aconst\u3002 const \u6210\u5458\u51fd\u6570 \u8003\u8651\u4e0b\u9762\u8fd9\u884c\u4ee3\u7801\uff1a 1 std :: cout << something . getValue (); \u4e0a\u8ff0\u4ee3\u7801\u53ef\u80fd\u4f1a\u8ba9\u4f60\u611f\u5230\u5403\u60ca\uff0c\u5b83\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\uff0c\u5373\u4f7f getValue() \u6ca1\u6709\u53bb\u6539\u53d8\u4efb\u4f55\u6210\u5458\u53d8\u91cf\uff01\u5b9e\u9645\u4e0a\uff0c\u4f5c\u4e3a const \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u5b83\u53ea\u80fd\u663e\u5f0f\u5730\u8c03\u7528 const \u7c7b\u578b\u7684\u6210\u5458\u51fd\u6570\uff0c\u800c getValue() \u5e76\u4e0d\u662f const\u7c7b\u578b\u7684\u3002 const \u6210\u5458\u51fd\u6570 \u662f\u4e00\u7c7b\u6210\u5458\u51fd\u6570\uff0c\u5b83\u4fdd\u8bc1\u81ea\u5df1\u4e0d\u4f1a\u4fee\u6539\u5bf9\u8c61\uff0c\u4e5f\u4e0d\u4f1a\u8c03\u7528\u4efb\u4f55\u975econst\u7c7b\u578b\u7684\u6210\u5458\u51fd\u6570\uff08\u56e0\u4e3a\u5b83\u4eec\u53ef\u80fd\u4f1a\u4fee\u6539\u5bf9\u8c61\uff09\u3002 \u8981\u5c06 getValue() \u58f0\u660e\u4e3a const \u6210\u5458\u51fd\u6570\uff0c\u53ea\u9700\u8981\u5728\u51fd\u6570\u539f\u578b\u4e2d\u4f7f\u7528 const \u5173\u952e\u5b57\u5373\u53ef\uff08\u653e\u5728\u53c2\u6570\u5217\u8868\u540e\uff0c\u51fd\u6570\u4f53\u4e4b\u524d\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 class Something { public : int m_value {}; Something () : m_value { 0 } { } void resetValue () { m_value = 0 ; } void setValue ( int value ) { m_value = value ; } int getValue () const { return m_value ; } // \u6ce8\u610f\u53c2\u6570\u5217\u8868\u4e4b\u540e\uff0c\u51fd\u6570\u4f53\u4e4b\u524d\u7684 const \u5173\u952e\u5b57 }; \u73b0\u5728\uff0c getValue() \u5df2\u7ecf\u662f const \u6210\u5458\u51fd\u6570\u4e86\uff0c\u6b64\u65f6\u5b83\u5c31\u53ef\u4ee5\u88ab\u5176\u4ed6 const \u5bf9\u8c61\u8c03\u7528\u3002 \u5bf9\u4e8e\u5b9a\u4e49\u5728\u7c7b\u5916\u7684\u6210\u5458\u51fd\u6570\u6765\u8bf4\uff0c\u4e0d\u4ec5\u51fd\u6570\u539f\u578b\u4e2d\u9700\u8981const\u5173\u952e\u5b57\uff0c\u51fd\u6570\u5b9a\u4e49\u65f6\u4e5f\u5fc5\u987b\u6dfb\u52a0 const \u5173\u952e\u5b57\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Something { public : int m_value {}; Something () : m_value { 0 } { } void resetValue () { m_value = 0 ; } void setValue ( int value ) { m_value = value ; } int getValue () const ; // \u6ce8\u610f\u6b64\u5904\u7684 const \u5173\u952e\u5b57 }; int Something :: getValue () const // \u8fd9\u91cc\u4e5f\u9700\u8981 { return m_value ; } \u4e0d\u4ec5\u5982\u6b64\uff0c\u4efb\u4f55 const \u6210\u5458\u51fd\u6570\u5982\u679c\u5c1d\u8bd5\u4fee\u6539\u6210\u5458\u53d8\u91cf\u6216\u8005\u8c03\u7528\u975econst\u7c7b\u578b\u7684\u51fd\u6570\uff0c\u90fd\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u4fdd\u5b58\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 class Something { public : int m_value {}; void resetValue () const { m_value = 0 ; } // compile error, const functions can't change member variables. }; \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c resetValue() \u88ab\u6807\u8bb0\u4e3a const \u6210\u5458\u51fd\u6570\uff0c\u4f46\u662f\u5b83\u5c1d\u8bd5\u4fee\u6539 m_value \u3002\u56e0\u6b64\u5bfc\u81f4\u4e86\u7f16\u8bd1\u9519\u8bef\u3002 \u6ce8\u610f\uff0c\u6784\u9020\u51fd\u6570\u4e0d\u80fd\u88ab\u6807\u8bb0\u4e3aconst\u3002\u8fd9\u662f\u56e0\u4e3a\u6784\u9020\u51fd\u6570\u9700\u8981\u80fd\u591f\u521d\u59cb\u5316\u5b83\u4eec\u7684\u6210\u5458\u53d8\u91cf\uff0c\u800cconst\u6784\u9020\u51fd\u6570\u4e0d\u80fd\u8fd9\u6837\u505a\u3002\u56e0\u6b64\uff0c\u8be5\u8bed\u8a00\u4e0d\u5141\u8bb8\u4f7f\u7528const\u6784\u9020\u51fd\u6570\u3002 const\u6210\u5458\u51fd\u6570\u4e5f\u53ef\u4ee5\u7531\u975econst\u5bf9\u8c61\u8c03\u7528\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c06\u4efb\u4f55\u4e0d\u9700\u8981\u4fee\u6539\u5bf9\u8c61\u72b6\u6001\u7684\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u4e3a const\uff0c\u8fd9\u6837\u5b83\u5c31\u53ef\u4ee5\u88ab const \u7c7b\u578b\u7684\u5bf9\u8c61\u8c03\u7528\u3002 \u901a\u8fc7 const \u7c7b\u578b\u5f15\u7528\u4f20\u9012 const \u5bf9\u8c61 \u5c3d\u7ba1\u5b9e\u4f8b\u5316const\u7c7b\u5bf9\u8c61\u662f\u521b\u5efaconst\u5bf9\u8c61\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u4f46\u83b7\u5f97const\u5bf9\u8c61\u66f4\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u901a\u8fc7const\u5f15\u7528\u5c06\u5bf9\u8c61\u4f20\u9012\u7ed9\u51fd\u6570\u3002 \u5728 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u901a\u8fc7\u5e38\u91cf\u5f15\u7528\u4f20\u9012\u7c7b\u53c2\u6570\u65f6\u7684\u7279\u6027\u3002\u56de\u5fc6\u4e00\u4e0b\uff0c \u6309\u503c\u4f20\u9012 \u5bf9\u8c61\u4f1a\u5bfc\u81f4\u5bf9\u8c61\u88ab\u8d4b\u503c\uff08\u6548\u7387\u4f4e\uff09\u2014\u2014\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e76\u4e0d\u9700\u8981\u4e00\u4efd\u62f7\u8d1d\uff0c\u4f7f\u7528\u539f\u59cb\u5bf9\u8c61\u7684\u5f15\u7528\u5c31\u53ef\u4ee5\u4e86\uff0c\u800c\u4e14\u7531\u4e8e\u907f\u514d\u4e86\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\uff0c\u6027\u80fd\u81ea\u7136\u4e5f\u66f4\u597d\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528 const \u7c7b\u578b\u7684\u5f15\u7528\u6765\u786e\u4fdd\u5b9e\u53c2\u4e0d\u88ab\u4fee\u6539\uff0c\u800c\u4e14\u4f7f\u5f97\u51fd\u6570\u53ef\u4ee5\u914d\u5408 \u53f3\u503c \u6765\u4f7f\u7528\uff08\u4f8b\u5982\u5b57\u9762\u91cf\uff09\uff0c\u56e0\u4e3a\u53f3\u503c\u53ea\u80fd\u901a\u8fc7const\u5f15\u7528\u4f20\u9012\u3002 \u4f60\u80fd\u770b\u51fa\u4e0b\u9762\u4ee3\u7801\u4e2d\u7684\u95ee\u9898\u5417\uff1f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include class Date { private : int m_year {}; int m_month {}; int m_day {}; public : Date ( int year , int month , int day ) { setDate ( year , month , day ); } void setDate ( int year , int month , int day ) { m_year = year ; m_month = month ; m_day = day ; } int getYear () { return m_year ; } int getMonth () { return m_month ; } int getDay () { return m_day ; } }; // \u6ce8\u610f\uff1a\u6211\u4eec\u901a\u8fc7\u4f20 const \u5f15\u7528\u6765\u907f\u514d\u62f7\u8d1d data void printDate ( const Date & date ) { std :: cout << date . getYear () << '/' << date . getMonth () << '/' << date . getDay () << '\\n' ; } int main () { Date date { 2016 , 10 , 16 }; printDate ( date ); return 0 ; } \u8fd9\u91cc\u7684\u95ee\u9898\u5728\u4e8e\uff0c\u5728\u51fd\u6570 printDate \u51fd\u6570\u4e2d\uff0c date \u88ab\u5f53\u505aconst\u5bf9\u8c61\uff0c\u56e0\u6b64\u57fa\u4e8econst\u5bf9\u8c61 date \u8c03\u7528 getYear() \u3001 getMonth() \u548c getDay() \u8fd9\u4e9b\u975econst\u6210\u5458\u51fd\u6570\u7684\u65f6\u5019\uff0c\u5c31\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u9700\u8981\u5c06 getYear() \u3001 getMonth() \u548c getDay() \u5b9a\u4e49\u4e3a const\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 class Date { private : int m_year {}; int m_month {}; int m_day {}; public : Date ( int year , int month , int day ) { setDate ( year , month , day ); } // setDate() cannot be const, modifies member variables void setDate ( int year , int month , int day ) { m_year = year ; m_month = month ; m_day = day ; } // The following getters can all be made const int getYear () const { return m_year ; } int getMonth () const { return m_month ; } int getDay () const { return m_day ; } }; \u8fd9\u6837\u4e00\u6765 printDate() \u51fd\u6570\u4e2d\u7684 const date \u5c31\u53ef\u4ee5\u6b63\u5e38\u8c03\u7528 getYear() \u3001 getMonth() \u548c getDay() \u4e86\u3002 const \u6210\u5458\u4e0d\u80fd\u8fd4\u56de\u975econst\u5f15\u7528\u6210\u5458 \u5982\u679c\u6210\u5458\u51fd\u6570\u662f const \u7684\uff0c\u5219 *this \u6307\u9488\u4e5f\u662fconst\u7684\uff0c\u8fd9\u5c31\u610f\u5473\u7740\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6240\u6709\u7c7b\u6210\u5458\u90fd\u4f1a\u88ab\u5f53\u505a\u662fconst\u7684\u3002\u56e0\u6b64\u4e00\u4e2a const \u6210\u5458\u51fd\u6570\u4e0d\u80fd\u8fd4\u56de\u975econst\u5f15\u7528\u6210\u5458\u3002const\u6210\u5458\u51fd\u6570\u53ea\u80fd\u8fd4\u56deconst\u5f15\u7528\u6210\u5458\u3002 \u5728\u4e0b\u4e00\u4e2a\u7ae0\u8282\u6211\u4eec\u4f1a\u770b\u5230\u5177\u4f53\u7684\u4f8b\u5b50\u3002 \u91cd\u8f7d const \u548c \u975e const \u51fd\u6570 \u6700\u540e\uff0c\u5c3d\u7ba1\u4e0d\u5e38\u7528\uff0c\u4f46\u6211\u4eec\u7684\u786e\u53ef\u4ee5\u901a\u8fc7\u91cd\u8f7d\u83b7\u5f97\u540c\u4e00\u51fd\u6570\u7684const\u548c\u975econst\u7248\u672c\u3002\u56e0\u4e3aconst\u9650\u5b9a\u7b26\u88ab\u8ba4\u4e3a\u662f\u51fd\u6570\u7b7e\u540d\u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u5f53\u4e24\u4e2a\u51fd\u6570\u5177\u6709\u4e0d\u540c\u7684 const \u7b7e\u540d\u7684\u51fd\u6570\u4f1a\u88ab\u8ba4\u4e3a\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include class Something { private : std :: string m_value {}; public : Something ( const std :: string & value = \"\" ) : m_value { value } {} const std :: string & getValue () const { return m_value ; } // getValue() for const objects (returns const reference) std :: string & getValue () { return m_value ; } // getValue() for non-const objects (returns non-const reference) }; \u5f53\u4f7f\u7528const\u5bf9\u8c61\u8c03\u7528\u6210\u5458\u51fd\u6570\u65f6\uff0c\u8c03\u7528\u7684\u4fbf\u662f const \u7248\u672c\u7684\u51fd\u6570\u3002\u5bf9\u4e8e\u975econst\u6210\u5458\u800c\u8a00\uff0c\u5219\u8c03\u7528\u7684\u662f\u975econst\u6210\u5458\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { Something something ; something . getValue () = \"Hi\" ; // calls non-const getValue(); const Something something2 ; something2 . getValue (); // calls const getValue(); return 0 ; } \u5f53\u8fd4\u56de\u503c\u9700\u8981\u4e0d\u540c\u7684\u201c\u5e38\u91cf\u6027\u201d\u65f6\uff08\u5373\u5e38\u91cf\u6216\u975e\u5e38\u91cf\uff09\uff0c\u5219\u9700\u8981\u91cd\u8f7dconst\u548c\u975econst\u7248\u672c\u7684\u51fd\u6570\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c getValue() \u7684\u975econst\u7248\u672c\u53ea\u80fd\u914d\u5408\u975econst\u5bf9\u8c61\u6765\u4f7f\u7528\uff0c\u4f46\u662f\u5b83\u66f4\u7075\u6d3b\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u8bfb m_value \uff0c\u6216\u8005\u5199 m_value (\u901a\u8fc7\u8d4b\u503c\u5b57\u7b26\u4e32\" Hi \"\u6765\u5b9e\u73b0)\u3002 getValue() \u7684const\u7248\u672c\u65e2\u53ef\u4ee5\u5904\u7406const\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u5904\u7406\u975econst\u5bf9\u8c61\uff0c\u4f46\u8fd4\u56deconst\u5f15\u7528\uff0c\u4ee5\u786e\u4fdd\u4e0d\u80fd\u4fee\u6539const\u5bf9\u8c61\u7684\u6570\u636e\u3002 \u5c0f\u7ed3 \u56e0\u4e3a\u901a\u8fc7const\u5f15\u7528\u4f20\u9012\u5bf9\u8c61\u662f\u5e38\u89c1\u7684\uff0c\u6240\u4ee5\u7c7b\u5e94\u8be5\u662fconst\u53cb\u597d\u7684\u3002\u8fd9\u610f\u5473\u7740\u5e94\u8be5\u5c06\u4efb\u4f55\u4e0d\u4fee\u6539\u7c7b\u5bf9\u8c61\u7684\u72b6\u6001\u7684\u6210\u5458\u51fd\u6570\u8bbe\u7f6e\u4e3aconst\uff01","title":"13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570"},{"location":"13-12-const-class-objects-and-member-functions/#const","text":"\u540c\u6837\u7684\uff0c\u88ab \u5b9e\u4f8b\u5316 \u7684\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u901a\u8fc7const\u5173\u952e\u5b57\u88ab\u521b\u5efa\u4e3a const \u7c7b\u578b\u3002\u521d\u59cb\u5316\u5219\u662f\u901a\u8fc7 \u6784\u9020\u51fd\u6570 \u5b8c\u6210\u7684\uff1a 1 2 3 const Date date1 ; // \u4f7f\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316 const Date date2 ( 2020 , 10 , 16 ); // \u4f7f\u7528\u5e26\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u8fdb\u884c\u521d\u59cb\u5316 const Date date3 { 2020 , 10 , 16 }; // \u4f7f\u7528\u5e26\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u8fdb\u884c\u521d\u59cb\u5316(C++11) const \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u4e00\u65e6\u901a\u8fc7\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\uff0c\u5176\u4efb\u4f55\u6210\u5458\u53d8\u91cf\u90fd\u4e0d\u518d\u53ef\u4ee5\u88ab\u4fee\u6539\uff0c\u5426\u5219\u663e\u7136\u8fdd\u80cc\u4e86\u5176\u201c\u5e38\u91cf\u6027\u201d\u3002\u4e0d\u8bba\u662f\u76f4\u63a5\u4fee\u6539 \u516c\u6709\u6210\u5458 \u8fd8\u662f\u901a\u8fc7\u516c\u6709\u6210\u5458\u51fd\u6570\u4e3a\u6210\u5458\u53d8\u91cf\u8d4b\u503c\u90fd\u662f\u4e0d\u53ef\u4ee5\u7684\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Something { public : int m_value {}; Something () : m_value { 0 } { } void setValue ( int value ) { m_value = value ; } int getValue () { return m_value ; } }; int main () { const Something something {}; // calls default constructor something . m_value = 5 ; // compiler error: violates const something . setValue ( 5 ); // compiler error: violates const return 0 ; } \u4e0a\u9762\u6d89\u53ca\u53d8\u91cf something \u7684\u4e24\u884c\u90fd\u662f\u975e\u6cd5\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u8981\u4e48\u8bd5\u56fe\u76f4\u63a5\u66f4\u6539\u6210\u5458\u53d8\u91cf\uff0c\u8981\u4e48\u8c03\u7528\u8bd5\u56fe\u66f4\u6539\u6210\u5458\u53d8\u91cf\u7684\u6210\u5458\u51fd\u6570\uff0c\u4ece\u800c\u8fdd\u53cd\u4e86 something \u7684\u5e38\u91cf\u6027\u3002 \u4e0e\u666e\u901a\u53d8\u91cf\u4e00\u6837\uff0c\u5f53\u9700\u8981\u786e\u4fdd\u7c7b\u5bf9\u8c61\u5728\u521b\u5efa\u540e\u4e0d\u518d\u88ab\u4fee\u6539\u65f6\uff0c\u53ef\u4ee5\u5c06\u5b83\u4eec\u8bbe\u7f6e\u4e3aconst\u3002","title":"const \u7c7b"},{"location":"13-12-const-class-objects-and-member-functions/#const_1","text":"\u8003\u8651\u4e0b\u9762\u8fd9\u884c\u4ee3\u7801\uff1a 1 std :: cout << something . getValue (); \u4e0a\u8ff0\u4ee3\u7801\u53ef\u80fd\u4f1a\u8ba9\u4f60\u611f\u5230\u5403\u60ca\uff0c\u5b83\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\uff0c\u5373\u4f7f getValue() \u6ca1\u6709\u53bb\u6539\u53d8\u4efb\u4f55\u6210\u5458\u53d8\u91cf\uff01\u5b9e\u9645\u4e0a\uff0c\u4f5c\u4e3a const \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u5b83\u53ea\u80fd\u663e\u5f0f\u5730\u8c03\u7528 const \u7c7b\u578b\u7684\u6210\u5458\u51fd\u6570\uff0c\u800c getValue() \u5e76\u4e0d\u662f const\u7c7b\u578b\u7684\u3002 const \u6210\u5458\u51fd\u6570 \u662f\u4e00\u7c7b\u6210\u5458\u51fd\u6570\uff0c\u5b83\u4fdd\u8bc1\u81ea\u5df1\u4e0d\u4f1a\u4fee\u6539\u5bf9\u8c61\uff0c\u4e5f\u4e0d\u4f1a\u8c03\u7528\u4efb\u4f55\u975econst\u7c7b\u578b\u7684\u6210\u5458\u51fd\u6570\uff08\u56e0\u4e3a\u5b83\u4eec\u53ef\u80fd\u4f1a\u4fee\u6539\u5bf9\u8c61\uff09\u3002 \u8981\u5c06 getValue() \u58f0\u660e\u4e3a const \u6210\u5458\u51fd\u6570\uff0c\u53ea\u9700\u8981\u5728\u51fd\u6570\u539f\u578b\u4e2d\u4f7f\u7528 const \u5173\u952e\u5b57\u5373\u53ef\uff08\u653e\u5728\u53c2\u6570\u5217\u8868\u540e\uff0c\u51fd\u6570\u4f53\u4e4b\u524d\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 class Something { public : int m_value {}; Something () : m_value { 0 } { } void resetValue () { m_value = 0 ; } void setValue ( int value ) { m_value = value ; } int getValue () const { return m_value ; } // \u6ce8\u610f\u53c2\u6570\u5217\u8868\u4e4b\u540e\uff0c\u51fd\u6570\u4f53\u4e4b\u524d\u7684 const \u5173\u952e\u5b57 }; \u73b0\u5728\uff0c getValue() \u5df2\u7ecf\u662f const \u6210\u5458\u51fd\u6570\u4e86\uff0c\u6b64\u65f6\u5b83\u5c31\u53ef\u4ee5\u88ab\u5176\u4ed6 const \u5bf9\u8c61\u8c03\u7528\u3002 \u5bf9\u4e8e\u5b9a\u4e49\u5728\u7c7b\u5916\u7684\u6210\u5458\u51fd\u6570\u6765\u8bf4\uff0c\u4e0d\u4ec5\u51fd\u6570\u539f\u578b\u4e2d\u9700\u8981const\u5173\u952e\u5b57\uff0c\u51fd\u6570\u5b9a\u4e49\u65f6\u4e5f\u5fc5\u987b\u6dfb\u52a0 const \u5173\u952e\u5b57\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Something { public : int m_value {}; Something () : m_value { 0 } { } void resetValue () { m_value = 0 ; } void setValue ( int value ) { m_value = value ; } int getValue () const ; // \u6ce8\u610f\u6b64\u5904\u7684 const \u5173\u952e\u5b57 }; int Something :: getValue () const // \u8fd9\u91cc\u4e5f\u9700\u8981 { return m_value ; } \u4e0d\u4ec5\u5982\u6b64\uff0c\u4efb\u4f55 const \u6210\u5458\u51fd\u6570\u5982\u679c\u5c1d\u8bd5\u4fee\u6539\u6210\u5458\u53d8\u91cf\u6216\u8005\u8c03\u7528\u975econst\u7c7b\u578b\u7684\u51fd\u6570\uff0c\u90fd\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u4fdd\u5b58\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 class Something { public : int m_value {}; void resetValue () const { m_value = 0 ; } // compile error, const functions can't change member variables. }; \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c resetValue() \u88ab\u6807\u8bb0\u4e3a const \u6210\u5458\u51fd\u6570\uff0c\u4f46\u662f\u5b83\u5c1d\u8bd5\u4fee\u6539 m_value \u3002\u56e0\u6b64\u5bfc\u81f4\u4e86\u7f16\u8bd1\u9519\u8bef\u3002 \u6ce8\u610f\uff0c\u6784\u9020\u51fd\u6570\u4e0d\u80fd\u88ab\u6807\u8bb0\u4e3aconst\u3002\u8fd9\u662f\u56e0\u4e3a\u6784\u9020\u51fd\u6570\u9700\u8981\u80fd\u591f\u521d\u59cb\u5316\u5b83\u4eec\u7684\u6210\u5458\u53d8\u91cf\uff0c\u800cconst\u6784\u9020\u51fd\u6570\u4e0d\u80fd\u8fd9\u6837\u505a\u3002\u56e0\u6b64\uff0c\u8be5\u8bed\u8a00\u4e0d\u5141\u8bb8\u4f7f\u7528const\u6784\u9020\u51fd\u6570\u3002 const\u6210\u5458\u51fd\u6570\u4e5f\u53ef\u4ee5\u7531\u975econst\u5bf9\u8c61\u8c03\u7528\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c06\u4efb\u4f55\u4e0d\u9700\u8981\u4fee\u6539\u5bf9\u8c61\u72b6\u6001\u7684\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u4e3a const\uff0c\u8fd9\u6837\u5b83\u5c31\u53ef\u4ee5\u88ab const \u7c7b\u578b\u7684\u5bf9\u8c61\u8c03\u7528\u3002","title":"const \u6210\u5458\u51fd\u6570"},{"location":"13-12-const-class-objects-and-member-functions/#const-const","text":"\u5c3d\u7ba1\u5b9e\u4f8b\u5316const\u7c7b\u5bf9\u8c61\u662f\u521b\u5efaconst\u5bf9\u8c61\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u4f46\u83b7\u5f97const\u5bf9\u8c61\u66f4\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u901a\u8fc7const\u5f15\u7528\u5c06\u5bf9\u8c61\u4f20\u9012\u7ed9\u51fd\u6570\u3002 \u5728 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u901a\u8fc7\u5e38\u91cf\u5f15\u7528\u4f20\u9012\u7c7b\u53c2\u6570\u65f6\u7684\u7279\u6027\u3002\u56de\u5fc6\u4e00\u4e0b\uff0c \u6309\u503c\u4f20\u9012 \u5bf9\u8c61\u4f1a\u5bfc\u81f4\u5bf9\u8c61\u88ab\u8d4b\u503c\uff08\u6548\u7387\u4f4e\uff09\u2014\u2014\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e76\u4e0d\u9700\u8981\u4e00\u4efd\u62f7\u8d1d\uff0c\u4f7f\u7528\u539f\u59cb\u5bf9\u8c61\u7684\u5f15\u7528\u5c31\u53ef\u4ee5\u4e86\uff0c\u800c\u4e14\u7531\u4e8e\u907f\u514d\u4e86\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\uff0c\u6027\u80fd\u81ea\u7136\u4e5f\u66f4\u597d\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528 const \u7c7b\u578b\u7684\u5f15\u7528\u6765\u786e\u4fdd\u5b9e\u53c2\u4e0d\u88ab\u4fee\u6539\uff0c\u800c\u4e14\u4f7f\u5f97\u51fd\u6570\u53ef\u4ee5\u914d\u5408 \u53f3\u503c \u6765\u4f7f\u7528\uff08\u4f8b\u5982\u5b57\u9762\u91cf\uff09\uff0c\u56e0\u4e3a\u53f3\u503c\u53ea\u80fd\u901a\u8fc7const\u5f15\u7528\u4f20\u9012\u3002 \u4f60\u80fd\u770b\u51fa\u4e0b\u9762\u4ee3\u7801\u4e2d\u7684\u95ee\u9898\u5417\uff1f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include class Date { private : int m_year {}; int m_month {}; int m_day {}; public : Date ( int year , int month , int day ) { setDate ( year , month , day ); } void setDate ( int year , int month , int day ) { m_year = year ; m_month = month ; m_day = day ; } int getYear () { return m_year ; } int getMonth () { return m_month ; } int getDay () { return m_day ; } }; // \u6ce8\u610f\uff1a\u6211\u4eec\u901a\u8fc7\u4f20 const \u5f15\u7528\u6765\u907f\u514d\u62f7\u8d1d data void printDate ( const Date & date ) { std :: cout << date . getYear () << '/' << date . getMonth () << '/' << date . getDay () << '\\n' ; } int main () { Date date { 2016 , 10 , 16 }; printDate ( date ); return 0 ; } \u8fd9\u91cc\u7684\u95ee\u9898\u5728\u4e8e\uff0c\u5728\u51fd\u6570 printDate \u51fd\u6570\u4e2d\uff0c date \u88ab\u5f53\u505aconst\u5bf9\u8c61\uff0c\u56e0\u6b64\u57fa\u4e8econst\u5bf9\u8c61 date \u8c03\u7528 getYear() \u3001 getMonth() \u548c getDay() \u8fd9\u4e9b\u975econst\u6210\u5458\u51fd\u6570\u7684\u65f6\u5019\uff0c\u5c31\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u9700\u8981\u5c06 getYear() \u3001 getMonth() \u548c getDay() \u5b9a\u4e49\u4e3a const\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 class Date { private : int m_year {}; int m_month {}; int m_day {}; public : Date ( int year , int month , int day ) { setDate ( year , month , day ); } // setDate() cannot be const, modifies member variables void setDate ( int year , int month , int day ) { m_year = year ; m_month = month ; m_day = day ; } // The following getters can all be made const int getYear () const { return m_year ; } int getMonth () const { return m_month ; } int getDay () const { return m_day ; } }; \u8fd9\u6837\u4e00\u6765 printDate() \u51fd\u6570\u4e2d\u7684 const date \u5c31\u53ef\u4ee5\u6b63\u5e38\u8c03\u7528 getYear() \u3001 getMonth() \u548c getDay() \u4e86\u3002","title":"\u901a\u8fc7 const \u7c7b\u578b\u5f15\u7528\u4f20\u9012 const \u5bf9\u8c61"},{"location":"13-12-const-class-objects-and-member-functions/#const-const_1","text":"\u5982\u679c\u6210\u5458\u51fd\u6570\u662f const \u7684\uff0c\u5219 *this \u6307\u9488\u4e5f\u662fconst\u7684\uff0c\u8fd9\u5c31\u610f\u5473\u7740\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6240\u6709\u7c7b\u6210\u5458\u90fd\u4f1a\u88ab\u5f53\u505a\u662fconst\u7684\u3002\u56e0\u6b64\u4e00\u4e2a const \u6210\u5458\u51fd\u6570\u4e0d\u80fd\u8fd4\u56de\u975econst\u5f15\u7528\u6210\u5458\u3002const\u6210\u5458\u51fd\u6570\u53ea\u80fd\u8fd4\u56deconst\u5f15\u7528\u6210\u5458\u3002 \u5728\u4e0b\u4e00\u4e2a\u7ae0\u8282\u6211\u4eec\u4f1a\u770b\u5230\u5177\u4f53\u7684\u4f8b\u5b50\u3002","title":"const \u6210\u5458\u4e0d\u80fd\u8fd4\u56de\u975econst\u5f15\u7528\u6210\u5458"},{"location":"13-12-const-class-objects-and-member-functions/#const-const_2","text":"\u6700\u540e\uff0c\u5c3d\u7ba1\u4e0d\u5e38\u7528\uff0c\u4f46\u6211\u4eec\u7684\u786e\u53ef\u4ee5\u901a\u8fc7\u91cd\u8f7d\u83b7\u5f97\u540c\u4e00\u51fd\u6570\u7684const\u548c\u975econst\u7248\u672c\u3002\u56e0\u4e3aconst\u9650\u5b9a\u7b26\u88ab\u8ba4\u4e3a\u662f\u51fd\u6570\u7b7e\u540d\u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u5f53\u4e24\u4e2a\u51fd\u6570\u5177\u6709\u4e0d\u540c\u7684 const \u7b7e\u540d\u7684\u51fd\u6570\u4f1a\u88ab\u8ba4\u4e3a\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include class Something { private : std :: string m_value {}; public : Something ( const std :: string & value = \"\" ) : m_value { value } {} const std :: string & getValue () const { return m_value ; } // getValue() for const objects (returns const reference) std :: string & getValue () { return m_value ; } // getValue() for non-const objects (returns non-const reference) }; \u5f53\u4f7f\u7528const\u5bf9\u8c61\u8c03\u7528\u6210\u5458\u51fd\u6570\u65f6\uff0c\u8c03\u7528\u7684\u4fbf\u662f const \u7248\u672c\u7684\u51fd\u6570\u3002\u5bf9\u4e8e\u975econst\u6210\u5458\u800c\u8a00\uff0c\u5219\u8c03\u7528\u7684\u662f\u975econst\u6210\u5458\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { Something something ; something . getValue () = \"Hi\" ; // calls non-const getValue(); const Something something2 ; something2 . getValue (); // calls const getValue(); return 0 ; } \u5f53\u8fd4\u56de\u503c\u9700\u8981\u4e0d\u540c\u7684\u201c\u5e38\u91cf\u6027\u201d\u65f6\uff08\u5373\u5e38\u91cf\u6216\u975e\u5e38\u91cf\uff09\uff0c\u5219\u9700\u8981\u91cd\u8f7dconst\u548c\u975econst\u7248\u672c\u7684\u51fd\u6570\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c getValue() \u7684\u975econst\u7248\u672c\u53ea\u80fd\u914d\u5408\u975econst\u5bf9\u8c61\u6765\u4f7f\u7528\uff0c\u4f46\u662f\u5b83\u66f4\u7075\u6d3b\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u8bfb m_value \uff0c\u6216\u8005\u5199 m_value (\u901a\u8fc7\u8d4b\u503c\u5b57\u7b26\u4e32\" Hi \"\u6765\u5b9e\u73b0)\u3002 getValue() \u7684const\u7248\u672c\u65e2\u53ef\u4ee5\u5904\u7406const\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u5904\u7406\u975econst\u5bf9\u8c61\uff0c\u4f46\u8fd4\u56deconst\u5f15\u7528\uff0c\u4ee5\u786e\u4fdd\u4e0d\u80fd\u4fee\u6539const\u5bf9\u8c61\u7684\u6570\u636e\u3002","title":"\u91cd\u8f7d const \u548c \u975e const \u51fd\u6570"},{"location":"13-12-const-class-objects-and-member-functions/#_1","text":"\u56e0\u4e3a\u901a\u8fc7const\u5f15\u7528\u4f20\u9012\u5bf9\u8c61\u662f\u5e38\u89c1\u7684\uff0c\u6240\u4ee5\u7c7b\u5e94\u8be5\u662fconst\u53cb\u597d\u7684\u3002\u8fd9\u610f\u5473\u7740\u5e94\u8be5\u5c06\u4efb\u4f55\u4e0d\u4fee\u6539\u7c7b\u5bf9\u8c61\u7684\u72b6\u6001\u7684\u6210\u5458\u51fd\u6570\u8bbe\u7f6e\u4e3aconst\uff01","title":"\u5c0f\u7ed3"},{"location":"13-13-static-member-variables/","tags":["class","static member variable"],"text":"\u91cd\u6e29 static \u5173\u952e\u5b57 \u5728 6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf \u4e2d\u6211\u4eec\u5b66\u4e60\u4e86 static \u5173\u952e\u5b57\uff0c\u5b83\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u5e76\u786e\u4fdd\u5b83\u4e0d\u4f1a\u5728 \u79bb\u5f00\u4f5c\u7528\u57df \u540e\u88ab\u9500\u6bc1\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int generateID () { static int s_id { 0 }; return ++ s_id ; } int main () { std :: cout << generateID () << '\\n' ; std :: cout << generateID () << '\\n' ; std :: cout << generateID () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 1 2 3 \u6ce8\u610f\uff0c s_id \u7684\u503c\u5728\u591a\u6b21\u51fd\u6570\u8c03\u7528\u95f4\u5f97\u4ee5\u4fdd\u7559\u3002 static \u5173\u952e\u5b57\u5728\u7528\u4e8e \u5168\u5c40\u53d8\u91cf \u65f6\uff0c\u8fd8\u6709\u53e6\u5916\u7684\u542b\u4e49\u2014\u2014 \u5b83\u4f1a\u8d4b\u4e88\u8be5\u53d8\u91cf \u5185\u90e8\u94fe\u63a5 \u5c5e\u6027\uff08\u4e0d\u80fd\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u6587\u4ef6\u5916\u4f7f\u7528\uff09\u3002\u56e0\u4e3a\u5168\u5c40\u53d8\u91cf\u5e94\u8be5\u88ab\u675c\u7edd\uff0c\u6240\u4ee5 static \u7684\u8fd9\u65b9\u9762\u5e94\u7528\u5e76\u4e0d\u5e38\u89c1\u3002 \u9759\u6001\u6210\u5458\u53d8\u91cf static \u5173\u952e\u5b57\u5728\u5e94\u7528\u4e8e\u7c7b\u6210\u5458\u65f6\uff0c\u6709\u4e24\u4e2a\u989d\u5916\u7684\u7528\u9014\uff1a \u9759\u6001\u6210\u5458\u53d8\u91cf \u548c \u9759\u6001\u6210\u5458\u51fd\u6570 \u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e24\u79cd static \u7528\u6cd5\u90fd\u975e\u5e38\u7b80\u5355\u76f4\u63a5\u3002\u6211\u4eec\u4f1a\u5728\u672c\u7ae0\u4ecb\u7ecd\u9759\u6001\u6210\u5458\u53d8\u91cf\uff0c\u7136\u540e\u5728\u4e0b\u4e00\u7ae0\u4ecb\u7ecd\u9759\u6001\u6210\u5458\u51fd\u6570\u3002 \u5728\u5f00\u59cb\u5b66\u4e60\u4e3a\u6210\u5458\u53d8\u91cf\u6dfb\u52a0 static \u5173\u952e\u5b57\u4e4b\u524d\uff0c\u8bf7\u5148\u8003\u8651\u4e0b\u9762\u7684\u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include class Something { public : int m_value { 1 }; }; int main () { Something first ; Something second ; first . m_value = 2 ; std :: cout << first . m_value << '\\n' ; std :: cout << second . m_value << '\\n' ; return 0 ; } \u5f53\u6211\u4eec\u5b9e\u4f8b\u5316\u5bf9\u8c61\u7684\u65f6\u5019\uff0c\u5bf9\u8c61\u5404\u81ea\u521b\u5efa\u5176\u6210\u5458\u51fd\u6570\u7684\u4e00\u4efd\u62f7\u8d1d\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a Something \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u6700\u7ec8\u6211\u4eec\u4f1a\u5f97\u5230\u4e24\u4efd m_value : first.m_value \u548c second.m_value \u3002\u5b83\u4eec\u4e24\u4e2a\u662f\u5404\u81ea\u72ec\u7acb\u7684\uff0c\u56e0\u6b64\u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 2 1 \u4f7f\u7528 static \u5173\u952e\u5b57\u53ef\u4ee5\u521b\u5efa\u9759\u6001\u6210\u5458\u53d8\u91cf\u3002\u548c\u666e\u901a\u6210\u5458\u53d8\u91cf\u4e0d\u540c\u7684\u662f\uff0c\u9759\u6001\u6210\u5458\u53d8\u91cf\u5728\u540c\u4e00\u4e2a\u7c7b\u7684\u5bf9\u8c61\u95f4\u662f\u5171\u4eab\u7684\u3002\u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include class Something { public : static int s_value ; }; int Something :: s_value { 1 }; int main () { Something first ; Something second ; first . s_value = 2 ; std :: cout << first . s_value << '\\n' ; std :: cout << second . s_value << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 2 2 \u56e0\u4e3a s_value \u662f\u9759\u6001\u6210\u5458\u53d8\u91cf\uff0c\u6240\u4ee5 s_value \u5728\u5404\u4e2a\u5bf9\u8c61\u95f4\u662f\u5171\u4eab\u7684\u3002\u5176\u7ed3\u679c\u5c31\u662f\uff0c first.s_value is \u4e0e second.s_value \u5b9e\u9645\u4e0a\u662f\u540c\u4e00\u4e2a\u3002\u56e0\u6b64\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 first \u8bbe\u7f6e s_value \u7684\u503c\uff0c\u5e76\u901a\u8fc7 second \u8bbf\u95ee\u5b83\u3002 \u9759\u6001\u6210\u5458\u53d8\u91cf\u5e76\u4e0d\u548c\u7c7b\u5bf9\u8c61\u5173\u8054 \u5c3d\u7ba1\u4f60\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8c61\u6765\u8bbf\u95ee\u9759\u6001\u6210\u5458(\u4f8b\u5982\uff1a first.s_value \u548c second.s_value )\uff0c\u4f46\u5b9e\u9645\u4e0a\u8fd9\u4e9b\u9759\u6001\u6210\u5458\u5728\u5bf9\u8c61\u88ab\u5b9e\u4f8b\u5316\u524d\u5c31\u5b58\u5728\u4e86\u3002\u5b83\u4eec\u66f4\u50cf\u662f\u5168\u5c40\u53d8\u91cf\uff0c\u4f1a\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u521b\u5efa\uff0c\u5728\u7a0b\u5e8f\u9000\u51fa\u65f6\u9500\u6bc1\u3002 \u56e0\u6b64\uff0c\u6700\u597d\u8ba4\u4e3a\u9759\u6001\u6210\u5458\u662f\u5c5e\u4e8e\u7c7b\u672c\u8eab\u7684\uff0c\u800c\u4e0d\u662f\u7c7b\u7684\u67d0\u4e2a\u5b9e\u4f8b\u5bf9\u8c61\u3002\u56e0\u4e3a s_value \u72ec\u7acb\u4e8e\u4efb\u4f55\u7c7b\u5bf9\u8c61\u800c\u5b58\u5728\uff0c\u6240\u4ee5\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7c7b\u540d\u548c \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 (\u5728\u672c\u4f8b\u4e2d\u4e3a Something::s_value )\u8bbf\u95ee\u5b83: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include class Something { public : static int s_value ; // \u58f0\u660e\u9759\u6001\u6210\u5458\u53d8\u91cf }; int Something :: s_value { 1 }; // \u5b9a\u4e49\u9759\u6001\u6210\u5458\u53d8\u91cf int main () { // \u6ce8\u610f\uff1a\u6ca1\u6709\u5b9e\u4f8b\u5316\u4efb\u4f55\u5bf9\u8c61 Something :: s_value = 2 ; std :: cout << Something :: s_value << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c s_value \u662f\u4f7f\u7528\u7c7b\u540d\u8fdb\u884c\u8bbf\u95ee\u7684\uff0c\u800c\u6ca1\u6709\u901a\u8fc7\u4efb\u4f55\u8be5\u7c7b\u7684\u53d8\u91cf\u53bb\u8bbf\u95ee\u3002\u6211\u4eec\u751a\u81f3\u90fd\u8fd8\u6ca1\u6709\u5b9e\u4f8b\u5316\u4efb\u4f55\u8be5\u7c7b\u7684\u5bf9\u8c61\uff0c\u4f46\u662f\u4ecd\u7136\u53ef\u4ee5\u901a\u8fc7 Something::s_value \u6765\u8bbf\u95ee\u5b83\u3002\u8fd9\u79cd\u65b9\u5f0f\u662f\u66f4\u4e3a\u63a8\u8350\u7684\u4f7f\u7528\u9759\u6001\u6210\u5458\u7684\u65b9\u6cd5\u3002 \u6700\u4f73\u5b9e\u8df5 \u901a\u8fc7\u7c7b\u540d\uff08\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\uff09\u6765\u8bbf\u95ee\u9759\u6001\u6210\u5458\u53d8\u91cf\u800c\u4e0d\u662f\u901a\u8fc7\u5bf9\u8c61\u6765\u8bbf\u95ee\uff08\u4f7f\u7528\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26\uff09\u3002 \u5b9a\u4e49\u548c\u521d\u59cb\u5316\u9759\u6001\u6210\u5458\u53d8\u91cf \u5f53\u6211\u4eec\u5728\u7c7b\u5185\u90e8 \u58f0\u660e \u9759\u6001\u6210\u5458\u53d8\u91cf\u65f6\uff0c\u6211\u4eec\u662f\u5728\u544a\u8bc9\u7f16\u8bd1\u5668\u9759\u6001\u6210\u5458\u53d8\u91cf\u7684\u5b58\u5728\uff0c\u800c\u4e0d\u662f\u771f\u6b63\u5b9a\u4e49\u5b83(\u7c7b\u4f3c \u524d\u5411\u58f0\u660e )\u3002\u56e0\u4e3a\u9759\u6001\u6210\u5458\u53d8\u91cf\u4e0d\u662f\u5355\u4e2a\u7c7b\u5bf9\u8c61\u7684\u4e00\u90e8\u5206(\u5b83\u4eec\u4e0e\u5168\u5c40\u53d8\u91cf\u5904\u7406\u76f8\u4f3c\uff0c\u5e76\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u521d\u59cb\u5316)\uff0c\u6240\u4ee5\u5fc5\u987b\u5728\u7c7b\u5916\u90e8\u7684\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u663e\u5f0f\u5730\u5b9a\u4e49\u9759\u6001\u6210\u5458\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u8fd9\u4e00\u884c\u6765\u5b9e\u73b0\u9759\u6001\u6210\u5458\u7684\u5b9a\u4e49 1 int Something :: s_value { 1 }; // \u5b9a\u4e49\u9759\u6001\u6210\u5458\u53d8\u91cf \u8fd9\u4e00\u884c\u6709\u4e24\u4e2a\u76ee\u7684\uff1a\u5b9e\u4f8b\u5316\u9759\u6001\u6210\u5458\u53d8\u91cf(\u5c31\u50cf\u5168\u5c40\u53d8\u91cf\u4e00\u6837)\uff0c\u5e76\u53ef\u9009\u5730\u521d\u59cb\u5316\u5b83\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u63d0\u4f9b\u4e86\u521d\u59cb\u5316\u503c1\u3002\u5982\u679c\u6ca1\u6709\u63d0\u4f9b\u521d\u59cb\u5316\u503c\uff0cC++\u5c06\u8be5\u503c\u521d\u59cb\u5316\u4e3a0\u3002 \u6ce8\u610f\uff0c\u9759\u6001\u6210\u5458\u7684 \u5b9a\u4e49 \u4e0d\u53d7 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u7684\u9650\u5236\uff0c\u60a8\u53ef\u4ee5\u5b9a\u4e49\u548c\u521d\u59cb\u5316\u53d8\u91cf\uff0c\u5373\u4f7f\u5b83\u5728\u7c7b\u4e2d\u58f0\u660e\u4e3aprivate(\u6216protected)\u3002 \u5982\u679c\u7c7b\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u9759\u6001\u6210\u5458\u5b9a\u4e49\u901a\u5e38\u653e\u5728\u7c7b\u7684\u76f8\u5173\u4ee3\u7801\u6587\u4ef6\u4e2d(\u4f8b\u5982 Something.cpp )\u3002\u5982\u679c\u7c7b\u5b9a\u4e49\u5728 .cpp \u6587\u4ef6\u4e2d\uff0c\u5219\u9759\u6001\u6210\u5458\u5b9a\u4e49\u901a\u5e38\u76f4\u63a5\u653e\u5728\u7c7b\u7684\u4e0b\u9762\u3002\u4e0d\u8981\u5c06\u9759\u6001\u6210\u5458\u5b9a\u4e49\u653e\u5728\u5934\u6587\u4ef6\u4e2d(\u5f88\u50cf\u5168\u5c40\u53d8\u91cf\uff0c\u5982\u679c\u5934\u6587\u4ef6\u88ab\u5305\u542b\u4e0d\u6b62\u4e00\u6b21\uff0c\u6700\u7ec8\u5c06\u5f97\u5230\u591a\u4e2a\u5b9a\u4e49\uff0c\u8fd9\u5c06\u5bfc\u81f4\u94fe\u63a5\u5668\u9519\u8bef)\u3002 \u9759\u6001\u6210\u5458\u53d8\u91cf\u7684\u5185\u8054\u521d\u59cb\u5316 \u6709\u4e00\u4e9b\u6377\u5f84\u53ef\u4ee5\u5b9e\u73b0\u4e0a\u8ff0\u76ee\u6807\u3002\u9996\u5148\uff0c\u5f53\u9759\u6001\u6210\u5458\u662fconst\u6574\u578b\u7c7b\u578b(\u5305\u62ecchar\u548cbool)\u6216const enum\u65f6\uff0c\u53ef\u4ee5\u5728\u7c7b\u5b9a\u4e49\u4e2d\u521d\u59cb\u5316\u9759\u6001\u6210\u5458: 1 2 3 4 5 class Whatever { public : static const int s_value { 4 }; // a static const int can be declared and initialized directly }; \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u9759\u6001\u6210\u5458\u53d8\u91cf\u662f const \u7684\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u663e\u5f0f\u5730\u5b9a\u4e49\u5b83\u3002 \u5176\u6b21\uff0c static constexpr \u6210\u5458\u53ef\u4ee5\u5728\u7c7b\u5b9a\u4e49\u4e2d\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 #include class Whatever { public : static constexpr double s_value { 2.2 }; // ok static constexpr std :: array < int , 3 > s_array { 1 , 2 , 3 }; // this even works for classes that support constexpr initialization }; \u6700\u540e\uff0c\u5bf9\u4e8e C++17 \u6765\u8bf4\uff0c\u975e const \u9759\u6001\u6210\u5458\u4e5f\u53ef\u4ee5\u5728\u7c7b\u5b9a\u4e49\u4e2d\u5185\u8054\u5730\u521d\u59cb\u5316\uff1a 1 2 3 4 5 class Whatever { public : static inline int s_value { 4 }; // a static inline int can be declared and initialized directly (C++17) }; \u9759\u6001\u6210\u5458\u53d8\u91cf\u6848\u4f8b \u4e3a\u4ec0\u4e48\u6211\u4eec\u8981\u5728\u7c7b\u4e2d\u4f7f\u7528\u9759\u6001\u53d8\u91cf\u5462\uff1f\u4e00\u4e2a\u6bd4\u8f83\u6709\u7528\u7684\u4f8b\u5b50\u662f\u4e3a\u7c7b\u7684\u6bcf\u4e2a\u5b9e\u4f8b\u8bbe\u7f6e\u4e00\u4e2a\u552f\u4e00\u7684ID\uff0c\u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include class Something { private : static inline int s_idGenerator { 1 }; // C++17 // static int s_idGenerator; // \u5728 C++14 \u6216\u4e4b\u524d\u7684\u7248\u672c\u4e2d\u4f7f\u7528\u8fd9\u4e00\u884c\u4ee3\u7801 int m_id { }; public : Something () : m_id { s_idGenerator ++ } // \u4ece id \u751f\u6210\u5668\u83b7\u53d6\u4e0b\u4e00\u4e2a id {} int getID () const { return m_id ; } }; // For C++14 or older, we have to initialize the non-const static member outside the class definition // Note that we're defining and initializing s_idGenerator even though it is declared as private above. // This is okay since the definition isn't subject to access controls. // int Something::s_idGenerator { 1 }; // start our ID generator with value 1 (uncomment for C++14 or older) int main () { Something first ; Something second ; Something third ; std :: cout << first . getID () << '\\n' ; std :: cout << second . getID () << '\\n' ; std :: cout << third . getID () << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 1 2 3 \u56e0\u4e3a s_idGenerator \u7531\u6240\u6709 Something \u5bf9\u8c61\u5171\u4eab\uff0c\u6240\u4ee5\u5f53\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Something \u5bf9\u8c61\u65f6\uff0c\u6784\u9020\u51fd\u6570\u4ece s_idGenerator \u4e2d\u83b7\u53d6\u5f53\u524d\u503c\uff0c\u5c06\u5176\u9012\u589e\u540e\u4f5c\u4e3a\u4e0b\u4e00\u4e2a\u5bf9\u8c61\u7684ID\u3002\u8fd9\u4fdd\u8bc1\u4e86\u6bcf\u4e2a\u5b9e\u4f8b\u5316\u7684 Something \u5bf9\u8c61\u53ef\u4ee5\u83b7\u53d6\u552f\u4e00\u7684id(\u6309\u521b\u5efa\u987a\u5e8f\u9012\u589e)\u3002\u8fd9\u5728\u8c03\u8bd5\u6570\u7ec4\u4e2d\u7684\u591a\u4e2a\u9879\u65f6\u975e\u5e38\u6709\u5e2e\u52a9\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u6cd5\u6765\u533a\u5206\u5177\u6709\u76f8\u540c\u7c7b\u7c7b\u578b\u7684\u591a\u4e2a\u5bf9\u8c61! \u5f53\u7c7b\u9700\u8981\u521b\u5efa\u7c7b\u5185\u90e8\u7684\u67e5\u627e\u8868(\u4f8b\u5982\uff0c\u7528\u4e8e\u5b58\u50a8\u4e00\u7ec4\u9884\u8ba1\u7b97\u503c\u7684\u6570\u7ec4)\u65f6\uff0c\u9759\u6001\u6210\u5458\u53d8\u91cf\u4e5f\u5f88\u6709\u7528\u3002\u901a\u8fc7\u5c06\u67e5\u627e\u8868\u8bbe\u7f6e\u4e3a\u9759\u6001\u7684\uff0c\u6240\u6709\u5bf9\u8c61\u53ea\u5b58\u5728\u4e00\u4e2a\u526f\u672c\uff0c\u800c\u4e0d\u662f\u4e3a\u6bcf\u4e2a\u5b9e\u4f8b\u5316\u7684\u5bf9\u8c61\u90fd\u521b\u5efa\u4e00\u4e2a\u526f\u672c\u3002\u8fd9\u53ef\u4ee5\u8282\u7701\u5927\u91cf\u7684\u5185\u5b58\u3002","title":"13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf"},{"location":"13-13-static-member-variables/#static","text":"\u5728 6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf \u4e2d\u6211\u4eec\u5b66\u4e60\u4e86 static \u5173\u952e\u5b57\uff0c\u5b83\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u5e76\u786e\u4fdd\u5b83\u4e0d\u4f1a\u5728 \u79bb\u5f00\u4f5c\u7528\u57df \u540e\u88ab\u9500\u6bc1\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int generateID () { static int s_id { 0 }; return ++ s_id ; } int main () { std :: cout << generateID () << '\\n' ; std :: cout << generateID () << '\\n' ; std :: cout << generateID () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 1 2 3 \u6ce8\u610f\uff0c s_id \u7684\u503c\u5728\u591a\u6b21\u51fd\u6570\u8c03\u7528\u95f4\u5f97\u4ee5\u4fdd\u7559\u3002 static \u5173\u952e\u5b57\u5728\u7528\u4e8e \u5168\u5c40\u53d8\u91cf \u65f6\uff0c\u8fd8\u6709\u53e6\u5916\u7684\u542b\u4e49\u2014\u2014 \u5b83\u4f1a\u8d4b\u4e88\u8be5\u53d8\u91cf \u5185\u90e8\u94fe\u63a5 \u5c5e\u6027\uff08\u4e0d\u80fd\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u6587\u4ef6\u5916\u4f7f\u7528\uff09\u3002\u56e0\u4e3a\u5168\u5c40\u53d8\u91cf\u5e94\u8be5\u88ab\u675c\u7edd\uff0c\u6240\u4ee5 static \u7684\u8fd9\u65b9\u9762\u5e94\u7528\u5e76\u4e0d\u5e38\u89c1\u3002","title":"\u91cd\u6e29 static \u5173\u952e\u5b57"},{"location":"13-13-static-member-variables/#_1","text":"static \u5173\u952e\u5b57\u5728\u5e94\u7528\u4e8e\u7c7b\u6210\u5458\u65f6\uff0c\u6709\u4e24\u4e2a\u989d\u5916\u7684\u7528\u9014\uff1a \u9759\u6001\u6210\u5458\u53d8\u91cf \u548c \u9759\u6001\u6210\u5458\u51fd\u6570 \u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e24\u79cd static \u7528\u6cd5\u90fd\u975e\u5e38\u7b80\u5355\u76f4\u63a5\u3002\u6211\u4eec\u4f1a\u5728\u672c\u7ae0\u4ecb\u7ecd\u9759\u6001\u6210\u5458\u53d8\u91cf\uff0c\u7136\u540e\u5728\u4e0b\u4e00\u7ae0\u4ecb\u7ecd\u9759\u6001\u6210\u5458\u51fd\u6570\u3002 \u5728\u5f00\u59cb\u5b66\u4e60\u4e3a\u6210\u5458\u53d8\u91cf\u6dfb\u52a0 static \u5173\u952e\u5b57\u4e4b\u524d\uff0c\u8bf7\u5148\u8003\u8651\u4e0b\u9762\u7684\u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include class Something { public : int m_value { 1 }; }; int main () { Something first ; Something second ; first . m_value = 2 ; std :: cout << first . m_value << '\\n' ; std :: cout << second . m_value << '\\n' ; return 0 ; } \u5f53\u6211\u4eec\u5b9e\u4f8b\u5316\u5bf9\u8c61\u7684\u65f6\u5019\uff0c\u5bf9\u8c61\u5404\u81ea\u521b\u5efa\u5176\u6210\u5458\u51fd\u6570\u7684\u4e00\u4efd\u62f7\u8d1d\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a Something \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u6700\u7ec8\u6211\u4eec\u4f1a\u5f97\u5230\u4e24\u4efd m_value : first.m_value \u548c second.m_value \u3002\u5b83\u4eec\u4e24\u4e2a\u662f\u5404\u81ea\u72ec\u7acb\u7684\uff0c\u56e0\u6b64\u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 2 1 \u4f7f\u7528 static \u5173\u952e\u5b57\u53ef\u4ee5\u521b\u5efa\u9759\u6001\u6210\u5458\u53d8\u91cf\u3002\u548c\u666e\u901a\u6210\u5458\u53d8\u91cf\u4e0d\u540c\u7684\u662f\uff0c\u9759\u6001\u6210\u5458\u53d8\u91cf\u5728\u540c\u4e00\u4e2a\u7c7b\u7684\u5bf9\u8c61\u95f4\u662f\u5171\u4eab\u7684\u3002\u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include class Something { public : static int s_value ; }; int Something :: s_value { 1 }; int main () { Something first ; Something second ; first . s_value = 2 ; std :: cout << first . s_value << '\\n' ; std :: cout << second . s_value << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 2 2 \u56e0\u4e3a s_value \u662f\u9759\u6001\u6210\u5458\u53d8\u91cf\uff0c\u6240\u4ee5 s_value \u5728\u5404\u4e2a\u5bf9\u8c61\u95f4\u662f\u5171\u4eab\u7684\u3002\u5176\u7ed3\u679c\u5c31\u662f\uff0c first.s_value is \u4e0e second.s_value \u5b9e\u9645\u4e0a\u662f\u540c\u4e00\u4e2a\u3002\u56e0\u6b64\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 first \u8bbe\u7f6e s_value \u7684\u503c\uff0c\u5e76\u901a\u8fc7 second \u8bbf\u95ee\u5b83\u3002","title":"\u9759\u6001\u6210\u5458\u53d8\u91cf"},{"location":"13-13-static-member-variables/#_2","text":"\u5c3d\u7ba1\u4f60\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8c61\u6765\u8bbf\u95ee\u9759\u6001\u6210\u5458(\u4f8b\u5982\uff1a first.s_value \u548c second.s_value )\uff0c\u4f46\u5b9e\u9645\u4e0a\u8fd9\u4e9b\u9759\u6001\u6210\u5458\u5728\u5bf9\u8c61\u88ab\u5b9e\u4f8b\u5316\u524d\u5c31\u5b58\u5728\u4e86\u3002\u5b83\u4eec\u66f4\u50cf\u662f\u5168\u5c40\u53d8\u91cf\uff0c\u4f1a\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u521b\u5efa\uff0c\u5728\u7a0b\u5e8f\u9000\u51fa\u65f6\u9500\u6bc1\u3002 \u56e0\u6b64\uff0c\u6700\u597d\u8ba4\u4e3a\u9759\u6001\u6210\u5458\u662f\u5c5e\u4e8e\u7c7b\u672c\u8eab\u7684\uff0c\u800c\u4e0d\u662f\u7c7b\u7684\u67d0\u4e2a\u5b9e\u4f8b\u5bf9\u8c61\u3002\u56e0\u4e3a s_value \u72ec\u7acb\u4e8e\u4efb\u4f55\u7c7b\u5bf9\u8c61\u800c\u5b58\u5728\uff0c\u6240\u4ee5\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7c7b\u540d\u548c \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 (\u5728\u672c\u4f8b\u4e2d\u4e3a Something::s_value )\u8bbf\u95ee\u5b83: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include class Something { public : static int s_value ; // \u58f0\u660e\u9759\u6001\u6210\u5458\u53d8\u91cf }; int Something :: s_value { 1 }; // \u5b9a\u4e49\u9759\u6001\u6210\u5458\u53d8\u91cf int main () { // \u6ce8\u610f\uff1a\u6ca1\u6709\u5b9e\u4f8b\u5316\u4efb\u4f55\u5bf9\u8c61 Something :: s_value = 2 ; std :: cout << Something :: s_value << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c s_value \u662f\u4f7f\u7528\u7c7b\u540d\u8fdb\u884c\u8bbf\u95ee\u7684\uff0c\u800c\u6ca1\u6709\u901a\u8fc7\u4efb\u4f55\u8be5\u7c7b\u7684\u53d8\u91cf\u53bb\u8bbf\u95ee\u3002\u6211\u4eec\u751a\u81f3\u90fd\u8fd8\u6ca1\u6709\u5b9e\u4f8b\u5316\u4efb\u4f55\u8be5\u7c7b\u7684\u5bf9\u8c61\uff0c\u4f46\u662f\u4ecd\u7136\u53ef\u4ee5\u901a\u8fc7 Something::s_value \u6765\u8bbf\u95ee\u5b83\u3002\u8fd9\u79cd\u65b9\u5f0f\u662f\u66f4\u4e3a\u63a8\u8350\u7684\u4f7f\u7528\u9759\u6001\u6210\u5458\u7684\u65b9\u6cd5\u3002 \u6700\u4f73\u5b9e\u8df5 \u901a\u8fc7\u7c7b\u540d\uff08\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\uff09\u6765\u8bbf\u95ee\u9759\u6001\u6210\u5458\u53d8\u91cf\u800c\u4e0d\u662f\u901a\u8fc7\u5bf9\u8c61\u6765\u8bbf\u95ee\uff08\u4f7f\u7528\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26\uff09\u3002","title":"\u9759\u6001\u6210\u5458\u53d8\u91cf\u5e76\u4e0d\u548c\u7c7b\u5bf9\u8c61\u5173\u8054"},{"location":"13-13-static-member-variables/#_3","text":"\u5f53\u6211\u4eec\u5728\u7c7b\u5185\u90e8 \u58f0\u660e \u9759\u6001\u6210\u5458\u53d8\u91cf\u65f6\uff0c\u6211\u4eec\u662f\u5728\u544a\u8bc9\u7f16\u8bd1\u5668\u9759\u6001\u6210\u5458\u53d8\u91cf\u7684\u5b58\u5728\uff0c\u800c\u4e0d\u662f\u771f\u6b63\u5b9a\u4e49\u5b83(\u7c7b\u4f3c \u524d\u5411\u58f0\u660e )\u3002\u56e0\u4e3a\u9759\u6001\u6210\u5458\u53d8\u91cf\u4e0d\u662f\u5355\u4e2a\u7c7b\u5bf9\u8c61\u7684\u4e00\u90e8\u5206(\u5b83\u4eec\u4e0e\u5168\u5c40\u53d8\u91cf\u5904\u7406\u76f8\u4f3c\uff0c\u5e76\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u521d\u59cb\u5316)\uff0c\u6240\u4ee5\u5fc5\u987b\u5728\u7c7b\u5916\u90e8\u7684\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u663e\u5f0f\u5730\u5b9a\u4e49\u9759\u6001\u6210\u5458\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u8fd9\u4e00\u884c\u6765\u5b9e\u73b0\u9759\u6001\u6210\u5458\u7684\u5b9a\u4e49 1 int Something :: s_value { 1 }; // \u5b9a\u4e49\u9759\u6001\u6210\u5458\u53d8\u91cf \u8fd9\u4e00\u884c\u6709\u4e24\u4e2a\u76ee\u7684\uff1a\u5b9e\u4f8b\u5316\u9759\u6001\u6210\u5458\u53d8\u91cf(\u5c31\u50cf\u5168\u5c40\u53d8\u91cf\u4e00\u6837)\uff0c\u5e76\u53ef\u9009\u5730\u521d\u59cb\u5316\u5b83\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u63d0\u4f9b\u4e86\u521d\u59cb\u5316\u503c1\u3002\u5982\u679c\u6ca1\u6709\u63d0\u4f9b\u521d\u59cb\u5316\u503c\uff0cC++\u5c06\u8be5\u503c\u521d\u59cb\u5316\u4e3a0\u3002 \u6ce8\u610f\uff0c\u9759\u6001\u6210\u5458\u7684 \u5b9a\u4e49 \u4e0d\u53d7 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u7684\u9650\u5236\uff0c\u60a8\u53ef\u4ee5\u5b9a\u4e49\u548c\u521d\u59cb\u5316\u53d8\u91cf\uff0c\u5373\u4f7f\u5b83\u5728\u7c7b\u4e2d\u58f0\u660e\u4e3aprivate(\u6216protected)\u3002 \u5982\u679c\u7c7b\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u9759\u6001\u6210\u5458\u5b9a\u4e49\u901a\u5e38\u653e\u5728\u7c7b\u7684\u76f8\u5173\u4ee3\u7801\u6587\u4ef6\u4e2d(\u4f8b\u5982 Something.cpp )\u3002\u5982\u679c\u7c7b\u5b9a\u4e49\u5728 .cpp \u6587\u4ef6\u4e2d\uff0c\u5219\u9759\u6001\u6210\u5458\u5b9a\u4e49\u901a\u5e38\u76f4\u63a5\u653e\u5728\u7c7b\u7684\u4e0b\u9762\u3002\u4e0d\u8981\u5c06\u9759\u6001\u6210\u5458\u5b9a\u4e49\u653e\u5728\u5934\u6587\u4ef6\u4e2d(\u5f88\u50cf\u5168\u5c40\u53d8\u91cf\uff0c\u5982\u679c\u5934\u6587\u4ef6\u88ab\u5305\u542b\u4e0d\u6b62\u4e00\u6b21\uff0c\u6700\u7ec8\u5c06\u5f97\u5230\u591a\u4e2a\u5b9a\u4e49\uff0c\u8fd9\u5c06\u5bfc\u81f4\u94fe\u63a5\u5668\u9519\u8bef)\u3002","title":"\u5b9a\u4e49\u548c\u521d\u59cb\u5316\u9759\u6001\u6210\u5458\u53d8\u91cf"},{"location":"13-13-static-member-variables/#_4","text":"\u6709\u4e00\u4e9b\u6377\u5f84\u53ef\u4ee5\u5b9e\u73b0\u4e0a\u8ff0\u76ee\u6807\u3002\u9996\u5148\uff0c\u5f53\u9759\u6001\u6210\u5458\u662fconst\u6574\u578b\u7c7b\u578b(\u5305\u62ecchar\u548cbool)\u6216const enum\u65f6\uff0c\u53ef\u4ee5\u5728\u7c7b\u5b9a\u4e49\u4e2d\u521d\u59cb\u5316\u9759\u6001\u6210\u5458: 1 2 3 4 5 class Whatever { public : static const int s_value { 4 }; // a static const int can be declared and initialized directly }; \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u9759\u6001\u6210\u5458\u53d8\u91cf\u662f const \u7684\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u663e\u5f0f\u5730\u5b9a\u4e49\u5b83\u3002 \u5176\u6b21\uff0c static constexpr \u6210\u5458\u53ef\u4ee5\u5728\u7c7b\u5b9a\u4e49\u4e2d\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 #include class Whatever { public : static constexpr double s_value { 2.2 }; // ok static constexpr std :: array < int , 3 > s_array { 1 , 2 , 3 }; // this even works for classes that support constexpr initialization }; \u6700\u540e\uff0c\u5bf9\u4e8e C++17 \u6765\u8bf4\uff0c\u975e const \u9759\u6001\u6210\u5458\u4e5f\u53ef\u4ee5\u5728\u7c7b\u5b9a\u4e49\u4e2d\u5185\u8054\u5730\u521d\u59cb\u5316\uff1a 1 2 3 4 5 class Whatever { public : static inline int s_value { 4 }; // a static inline int can be declared and initialized directly (C++17) };","title":"\u9759\u6001\u6210\u5458\u53d8\u91cf\u7684\u5185\u8054\u521d\u59cb\u5316"},{"location":"13-13-static-member-variables/#_5","text":"\u4e3a\u4ec0\u4e48\u6211\u4eec\u8981\u5728\u7c7b\u4e2d\u4f7f\u7528\u9759\u6001\u53d8\u91cf\u5462\uff1f\u4e00\u4e2a\u6bd4\u8f83\u6709\u7528\u7684\u4f8b\u5b50\u662f\u4e3a\u7c7b\u7684\u6bcf\u4e2a\u5b9e\u4f8b\u8bbe\u7f6e\u4e00\u4e2a\u552f\u4e00\u7684ID\uff0c\u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include class Something { private : static inline int s_idGenerator { 1 }; // C++17 // static int s_idGenerator; // \u5728 C++14 \u6216\u4e4b\u524d\u7684\u7248\u672c\u4e2d\u4f7f\u7528\u8fd9\u4e00\u884c\u4ee3\u7801 int m_id { }; public : Something () : m_id { s_idGenerator ++ } // \u4ece id \u751f\u6210\u5668\u83b7\u53d6\u4e0b\u4e00\u4e2a id {} int getID () const { return m_id ; } }; // For C++14 or older, we have to initialize the non-const static member outside the class definition // Note that we're defining and initializing s_idGenerator even though it is declared as private above. // This is okay since the definition isn't subject to access controls. // int Something::s_idGenerator { 1 }; // start our ID generator with value 1 (uncomment for C++14 or older) int main () { Something first ; Something second ; Something third ; std :: cout << first . getID () << '\\n' ; std :: cout << second . getID () << '\\n' ; std :: cout << third . getID () << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 1 2 3 \u56e0\u4e3a s_idGenerator \u7531\u6240\u6709 Something \u5bf9\u8c61\u5171\u4eab\uff0c\u6240\u4ee5\u5f53\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Something \u5bf9\u8c61\u65f6\uff0c\u6784\u9020\u51fd\u6570\u4ece s_idGenerator \u4e2d\u83b7\u53d6\u5f53\u524d\u503c\uff0c\u5c06\u5176\u9012\u589e\u540e\u4f5c\u4e3a\u4e0b\u4e00\u4e2a\u5bf9\u8c61\u7684ID\u3002\u8fd9\u4fdd\u8bc1\u4e86\u6bcf\u4e2a\u5b9e\u4f8b\u5316\u7684 Something \u5bf9\u8c61\u53ef\u4ee5\u83b7\u53d6\u552f\u4e00\u7684id(\u6309\u521b\u5efa\u987a\u5e8f\u9012\u589e)\u3002\u8fd9\u5728\u8c03\u8bd5\u6570\u7ec4\u4e2d\u7684\u591a\u4e2a\u9879\u65f6\u975e\u5e38\u6709\u5e2e\u52a9\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u6cd5\u6765\u533a\u5206\u5177\u6709\u76f8\u540c\u7c7b\u7c7b\u578b\u7684\u591a\u4e2a\u5bf9\u8c61! \u5f53\u7c7b\u9700\u8981\u521b\u5efa\u7c7b\u5185\u90e8\u7684\u67e5\u627e\u8868(\u4f8b\u5982\uff0c\u7528\u4e8e\u5b58\u50a8\u4e00\u7ec4\u9884\u8ba1\u7b97\u503c\u7684\u6570\u7ec4)\u65f6\uff0c\u9759\u6001\u6210\u5458\u53d8\u91cf\u4e5f\u5f88\u6709\u7528\u3002\u901a\u8fc7\u5c06\u67e5\u627e\u8868\u8bbe\u7f6e\u4e3a\u9759\u6001\u7684\uff0c\u6240\u6709\u5bf9\u8c61\u53ea\u5b58\u5728\u4e00\u4e2a\u526f\u672c\uff0c\u800c\u4e0d\u662f\u4e3a\u6bcf\u4e2a\u5b9e\u4f8b\u5316\u7684\u5bf9\u8c61\u90fd\u521b\u5efa\u4e00\u4e2a\u526f\u672c\u3002\u8fd9\u53ef\u4ee5\u8282\u7701\u5927\u91cf\u7684\u5185\u5b58\u3002","title":"\u9759\u6001\u6210\u5458\u53d8\u91cf\u6848\u4f8b"},{"location":"13-14-static-member-functions/","tags":["class","static membe function"],"text":"\u9759\u6001\u6210\u5458\u51fd\u6570 \u5728\u4e0a\u4e00\u8282\u8bfe 13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf \u4e2d\uff0c\u6211\u4eec\u77e5\u9053\u4e86 \u9759\u6001\u6210\u5458\u53d8\u91cf \u662f\u5c5e\u4e8e\u7c7b\u800c\u4e0d\u662f\u7c7b\u5bf9\u8c61\u7684\u3002\u5982\u679c\u9759\u6001\u6210\u5458\u53d8\u91cf\u662f \u516c\u6709\u6210\u5458 \u7684\u8bdd\u6211\u4eec\u81ea\u7136\u53ef\u4ee5\u901a\u8fc7\u7c7b\u540d \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u6765\u8bbf\u95ee\u5b83\uff0c\u4f46\u662f\u5982\u679c\u5b83\u662f \u79c1\u6709\u6210\u5458 \u7684\u8bdd\uff0c\u6211\u4eec\u5e94\u8be5\u5982\u4f55\u8bbf\u95ee\u5b83\u5462\uff1f\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 class Something { private : static int s_value ; }; int Something :: s_value { 1 }; // \u521d\u59cb\u5316\u3002\u5373\u4f7f s_value \u662f\u79c1\u6709\u7684\u4e5f\u53ef\u4ee5\u8fd9\u6837\u505a\uff0c\u56e0\u4e3a\u6b64\u5904\u662f\u5728\u201c\u5b9a\u4e49\u201d int main () { // \u5e94\u8be5\u5982\u4f55\u8bbf\u95ee\u79c1\u6709\u6210\u5458 Something::s_value \u5462\uff1f } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4e0d\u80fd\u5728 main() \u51fd\u6570\u4e2d\u76f4\u63a5\u8bbf\u95ee Something::s_value \uff0c\u56e0\u4e3a\u5b83\u662f\u79c1\u6709\u6210\u5458\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u79c1\u6709\u6210\u5458\u53d8\u91cf\u9700\u8981\u901a\u8fc7\u516c\u6709\u6210\u5458\u51fd\u6570\u6765\u8bbf\u95ee\u3002\u5c3d\u7ba1\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u666e\u901a\u7684\u516c\u6709\u6210\u5458\u51fd\u6570\u6765\u8bbf\u95ee s_value \uff0c\u4f46\u5f88\u663e\u7136\u6211\u4eec\u5fc5\u987b\u5b9e\u4f8b\u5316\u4e00\u4e2a\u5bf9\u8c61\u624d\u80fd\u591f\u8c03\u7528\u8be5\u6210\u5458\u51fd\u6570\uff01\u66f4\u597d\u7684\u529e\u6cd5\u662f\u5c06\u8be5\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u4e3a\u9759\u6001\u7684\u3002 \u548c\u9759\u6001\u6210\u5458\u51fd\u6570\u4e00\u6837\uff0c\u9759\u6001\u6210\u5458\u51fd\u6570\u4e0d\u5c5e\u4e8e\u4efb\u4f55\u5bf9\u8c61\u3002\u4f7f\u7528\u9759\u6001\u6210\u5458\u51fd\u6570\u6765\u8bbf\u95ee s_value \u7684\u65b9\u6cd5\u89c1\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include class Something { private : static int s_value ; public : static int getValue () { return s_value ; } // static member function }; int Something :: s_value { 1 }; // initializer int main () { std :: cout << Something :: getValue () << '\\n' ; } \u56e0\u4e3a\u9759\u6001\u6210\u5458\u51fd\u6570\u4e0d\u5c5e\u4e8e\u4efb\u4f55\u7279\u5b9a\u5bf9\u8c61\uff0c\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528\u7c7b\u540d\u548c\u4f5c\u7528\u57df\u89e3\u6790\u64cd\u4f5c\u7b26\u76f4\u63a5\u8c03\u7528\u5b83\u4eec\u3002\u4e0e\u9759\u6001\u6210\u5458\u53d8\u91cf\u4e00\u6837\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8c61\u8c03\u7528\u5b83\u4eec\uff0c\u4f46\u4e0d\u5efa\u8bae\u8fd9\u6837\u505a\u3002 \u9759\u6001\u6210\u5458\u53d8\u91cf\u6ca1\u6709 *this \u6307\u9488 \u9759\u6001\u6210\u5458\u51fd\u6570\u6709\u4e24\u4e2a\u6709\u8da3\u7684\u7279\u6027\u503c\u5f97\u6ce8\u610f\u3002\u9996\u5148\uff0c\u56e0\u4e3a\u9759\u6001\u6210\u5458\u51fd\u6570\u4e0d\u5c5e\u4e8e\u67d0\u4e2a\u5bf9\u8c61\uff0c\u6240\u4ee5\u5b83\u4eec\u6ca1\u6709 this \u6307\u9488\uff01\u4ed4\u7ec6\u60f3\u60f3\uff0c\u8fd9\u662f\u6709\u9053\u7406\u7684\u2014\u2014 this \u6307\u9488\u603b\u662f\u6307\u5411\u8be5\u6210\u5458\u51fd\u6570\u6b63\u5728\u64cd\u4f5c\u7684\u5bf9\u8c61\u3002\u9759\u6001\u6210\u5458\u51fd\u6570\u4e0d\u80fd\u4f5c\u7528\u4e8e\u5bf9\u8c61\uff0c\u56e0\u6b64\u4e0d\u9700\u8981 this \u6307\u9488\u3002 \u5176\u6b21\uff0c\u9759\u6001\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5176\u4ed6\u9759\u6001\u6210\u5458(\u53d8\u91cf\u6216\u51fd\u6570)\uff0c\u4f46\u4e0d\u80fd\u8bbf\u95ee\u975e\u9759\u6001\u6210\u5458\u3002\u8fd9\u662f\u56e0\u4e3a\u975e\u9759\u6001\u6210\u5458\u5fc5\u987b\u5c5e\u4e8e\u7c7b\u5bf9\u8c61\uff0c\u800c\u9759\u6001\u6210\u5458\u51fd\u6570\u6ca1\u6709\u53ef\u4f7f\u7528\u7684\u7c7b\u5bf9\u8c61! \u53c8\u4e00\u4e2a\u4f8b\u5b50 \u9759\u6001\u6210\u5458\u51fd\u6570\u4e5f\u53ef\u4ee5\u5728\u7c7b\u58f0\u660e\u4e4b\u5916\u5b9a\u4e49\u3002\u8fd9\u4e0e\u666e\u901a\u6210\u5458\u51fd\u6570\u7684\u5de5\u4f5c\u65b9\u5f0f\u76f8\u540c\u3002 \u4e00\u4e2a\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include class IDGenerator { private : static int s_nextID ; // \u9759\u6001\u6210\u5458\u7684\u58f0\u660e public : static int getNextID (); // \u9759\u6001\u51fd\u6570\u7684\u58f0\u660e }; // \u5728\u7c7b\u5916\u5b9a\u4e49\u9759\u6001\u6210\u5458\u3002\u6ce8\u610f\uff0c\u6211\u4eec\u5728\u6b64\u5904\u6ca1\u6709\u4f7f\u7528 static \u5173\u952e\u5b57\u3002 // ID \u4ece 1 \u5f00\u59cb int IDGenerator :: s_nextID { 1 }; // \u7c7b\u5916\u5b9a\u4e49\u9759\u6001\u6210\u5458\u51fd\u6570\u3002\u6ce8\u610f\uff0c\u6211\u4eec\u5728\u6b64\u5904\u6ca1\u6709\u4f7f\u7528 static \u5173\u952e\u5b57\u3002 int IDGenerator::getNextID () { return s_nextID ++ ; } int main () { for ( int count { 0 }; count < 5 ; ++ count ) std :: cout << \"The next ID is: \" << IDGenerator :: getNextID () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 4 5 The next ID is: 1 The next ID is: 2 The next ID is: 3 The next ID is: 4 The next ID is: 5 \u6ce8\u610f\uff0c\u56e0\u4e3a\u8be5\u7c7b\u4e2d\u7684\u6240\u6709\u6570\u636e\u548c\u51fd\u6570\u90fd\u662f\u9759\u6001\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u9700\u8981\u5b9e\u4f8b\u5316\u8be5\u7c7b\u7684\u5bf9\u8c61\u6765\u4f7f\u7528\u5b83\u7684\u529f\u80fd\uff01\u8be5\u7c7b\u5229\u7528\u4e00\u4e2a\u9759\u6001\u6210\u5458\u53d8\u91cf\u6765\u4fdd\u5b58\u4e0b\u4e00\u4e2a\u8981\u5206\u914d\u7684ID\u7684\u503c\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u9759\u6001\u6210\u5458\u51fd\u6570\u6765\u8fd4\u56de\u8be5ID\u5e76\u4f7f\u5176\u9012\u589e\u3002 \u6709\u5173\u5168\u9759\u6001\u6210\u5458\u7c7b\u7684\u4e00\u4e9b\u8b66\u793a \u5bf9\u4e8e\u4e00\u4e2a\u6240\u6709\u6210\u5458\u90fd\u662f\u9759\u6001\u6210\u5458\u7684\u7c7b\u65f6\u8981\u5c0f\u5fc3\u3002\u5c3d\u7ba1\u8fd9\u79cd\u201c\u7eaf\u9759\u6001\u7c7b\u201d(\u4e5f\u79f0\u4e3a\u201c\u5355\u6001\u7c7b\u201d)\u53ef\u80fd\u5f88\u6709\u7528\uff0c\u4f46\u5b83\u4eec\u4e5f\u6709\u4e00\u4e9b\u6f5c\u5728\u7684\u7f3a\u70b9\u3002 \u9996\u5148\uff0c\u56e0\u4e3a\u6240\u6709\u9759\u6001\u6210\u5458\u90fd\u53ea\u5b9e\u4f8b\u5316\u4e00\u6b21\uff0c\u6240\u4ee5\u4e0d\u53ef\u80fd\u6709\u7eaf\u9759\u6001\u7c7b\u7684\u591a\u4e2a\u526f\u672c(\u9664\u975e\u514b\u9686\u8be5\u7c7b\u5e76\u91cd\u547d\u540d\u5b83)\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u9700\u8981\u4e24\u4e2a\u72ec\u7acb\u7684 IDGenerator \u5bf9\u8c61\uff0c\u8fd9\u5bf9\u4e8e\u5355\u4e2a\u7eaf\u9759\u6001\u7c7b\u6765\u8bf4\u662f\u4e0d\u53ef\u80fd\u7684\u3002 \u5176\u6b21\uff0c\u5728\u5173\u4e8e\u5168\u5c40\u53d8\u91cf\u7684\u4e00\u8bfe\u4e2d\uff0c\u4f60\u5df2\u7ecf\u4e86\u89e3\u5230\u4e86\u5168\u5c40\u53d8\u91cf\u7684\u5371\u9669\u6027\uff0c\u56e0\u4e3a\u4efb\u4f55\u4e00\u6bb5\u4ee3\u7801\u90fd\u53ef\u80fd\u6539\u53d8\u5168\u5c40\u53d8\u91cf\u7684\u503c\uff0c\u5e76\u6700\u7ec8\u7834\u574f\u53e6\u4e00\u6bb5\u770b\u8d77\u6765\u4e0d\u76f8\u5173\u7684\u4ee3\u7801\u3002\u5bf9\u4e8e\u7eaf\u9759\u6001\u7c7b\u4e5f\u662f\u5982\u6b64\u3002\u56e0\u4e3a\u6240\u6709\u6210\u5458\u90fd\u5c5e\u4e8e\u7c7b(\u800c\u4e0d\u662f\u7c7b\u7684\u5bf9\u8c61)\uff0c\u800c\u4e14\u7c7b\u58f0\u660e\u901a\u5e38\u5177\u6709\u5168\u5c40\u4f5c\u7528\u57df\uff0c\u6240\u4ee5\u7eaf\u9759\u6001\u7c7b\u672c\u8d28\u4e0a\u76f8\u5f53\u4e8e\u5728\u5168\u5c40\u53ef\u8bbf\u95ee\u7684\u540d\u79f0\u7a7a\u95f4\u4e2d\u58f0\u660e\u51fd\u6570\u548c\u5168\u5c40\u53d8\u91cf\uff0c\u5e76\u5177\u6709\u5168\u5c40\u53d8\u91cf\u6240\u5177\u6709\u7684\u6240\u6709\u5fc5\u8981\u7f3a\u70b9\u3002 C++ \u4e0d\u652f\u6301\u9759\u6001\u6784\u9020\u51fd\u6570 \u65e2\u7136\u53ef\u4ee5\u901a\u8fc7\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u666e\u901a\u6210\u5458\u53d8\u91cf\uff0c\u90a3\u4e48\u4e5f\u5e94\u8be5\u80fd\u591f\u901a\u8fc7\u9759\u6001\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u9759\u6001\u6210\u5458\u53d8\u91cf\u3002\u867d\u7136\u4e00\u4e9b\u73b0\u4ee3\u8bed\u8a00\u786e\u5b9e\u51fa\u4e8e\u8fd9\u4e2a\u76ee\u7684\u6dfb\u52a0\u4e86\u5bf9\u9759\u6001\u6784\u9020\u51fd\u6570\u7684\u652f\u6301\uff0c\u4f46\u4e0d\u5e78\u7684\u662fC++\u4e0d\u662f\u5176\u4e2d\u4e4b\u4e00\u3002 \u5982\u679c\u53ef\u4ee5\u76f4\u63a5\u521d\u59cb\u5316\u9759\u6001\u53d8\u91cf\uff0c\u5219\u4e0d\u9700\u8981\u6784\u9020\u51fd\u6570\uff1a\u53ef\u4ee5\u5728\u5b9a\u4e49\u70b9\u521d\u59cb\u5316\u9759\u6001\u6210\u5458\u53d8\u91cf(\u5373\u4f7f\u5b83\u662f\u79c1\u6709\u7684)\u3002\u6211\u4eec\u5728\u4e0a\u9762\u7684IDGenerator\u793a\u4f8b\u4e2d\u6b63\u662f\u8fd9\u6837\u505a\u7684\u3002\u8fd9\u662f\u53e6\u4e00\u4e2a\u4f8b\u5b50: 1 2 3 4 5 6 7 class MyClass { public : static std :: vector < char > s_mychars ; }; std :: vector < char > MyClass :: s_mychars { 'a' , 'e' , 'i' , 'o' , 'u' }; // \u5728\u5b9a\u4e49\u65f6\u521d\u59cb\u5316\u9759\u6001\u53d8\u91cf \u5982\u679c\u9700\u8981\u6267\u884c\u4ee3\u7801(\u4f8b\u5982\u5faa\u73af)\u624d\u80fd\u521d\u59cb\u5316\u9759\u6001\u6210\u5458\u53d8\u91cf\u7684\u8bdd\uff0c\u6709\u8bb8\u591a\u4e0d\u540c\u7684\u3001\u7b28\u62d9\u7684\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u6709\u4e00\u79cd\u5904\u7406\u65b9\u6cd5\u9002\u7528\u4e8e\u6240\u6709\u53d8\u91cf(\u9759\u6001\u6216\u975e\u9759\u6001)\uff0c\u5373\u4f7f\u7528lambda\u51fd\u6570\u5e76\u7acb\u5373\u8c03\u7528\u5b83\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class MyClass { public : static std :: vector < char > s_mychars ; }; std :: vector < char > MyClass :: s_mychars { []{ // The parameter list of lambdas without parameters can be omitted. // Inside the lambda we can declare another vector and use a loop. std :: vector < char > v {}; for ( char ch { 'a' }; ch <= 'z' ; ++ ch ) { v . push_back ( ch ); } return v ; }() // Call the lambda right away }; \u4e0b\u9762\u7684\u4ee3\u7801\u7ed9\u51fa\u4e86\u4e00\u4e2a\u884c\u4e3a\u66f4\u50cf\u5e38\u89c4\u6784\u9020\u51fd\u6570\u7684\u65b9\u6cd5\u3002\u4f46\u662f\uff0c\u60a8\u53ef\u80fd\u6c38\u8fdc\u90fd\u4e0d\u9700\u8981\u5b83\uff0c\u6240\u4ee5\u5982\u679c\u60a8\u613f\u610f\uff0c\u53ef\u4ee5\u968f\u610f\u8df3\u8fc7\u672c\u8282\u7684\u5176\u4f59\u90e8\u5206\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class MyClass { public : static std :: vector < char > s_mychars ; class init_static // we're defining a nested class named init_static { public : init_static () // the init constructor will initialize our static variable { for ( char ch { 'a' }; ch <= 'z' ; ++ ch ) { s_mychars . push_back ( ch ); } } } ; private : static init_static s_initializer ; // we'll use this static object to ensure the init_static constructor is called }; std :: vector < char > MyClass :: s_mychars {}; // define our static member variable MyClass :: init_static MyClass :: s_initializer {}; // define our static initializer, which will call the init_static constructor, which will initialize s_mychars \u5f53\u5b9a\u4e49\u9759\u6001\u6210\u5458 s_initializer \u65f6\uff0c\u5c06\u8c03\u7528 init_static() \u9ed8\u8ba4\u6784\u9020\u51fd\u6570(\u56e0\u4e3a s_initializer \u662f init_static \u7c7b\u578b)\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u4efb\u4f55\u9759\u6001\u6210\u5458\u53d8\u91cf\u3002\u8fd9\u4e2a\u89e3\u51b3\u65b9\u6848\u7684\u4f18\u70b9\u662f\uff0c\u6240\u6709\u521d\u59cb\u5316\u4ee3\u7801\u90fd\u9690\u85cf\u5728\u5e26\u6709\u9759\u6001\u6210\u5458\u7684\u539f\u59cb\u7c7b\u4e2d\u3002 \u5c0f\u7ed3 \u9759\u6001\u6210\u5458\u51fd\u6570\u53ef\u7528\u4e8e\u914d\u5408\u7c7b\u4e2d\u7684\u9759\u6001\u6210\u5458\u53d8\u91cf\u6765\u5de5\u4f5c\u3002\u7c7b\u5bf9\u8c61\u4e0d\u9700\u8981\u8c03\u7528\u5b83\u4eec\u3002 \u4e00\u4e2a\u7c7b\u7684\u6240\u6709\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u90fd\u662f\u9759\u6001\u7684\u3002\u7136\u800c\uff0c\u6b64\u7c7b\u7c7b\u672c\u8d28\u4e0a\u76f8\u5f53\u4e8e\u5728\u5168\u5c40\u53ef\u8bbf\u95ee\u7684\u540d\u79f0\u7a7a\u95f4\u4e2d\u58f0\u660e\u51fd\u6570\u548c\u5168\u5c40\u53d8\u91cf\uff0c\u901a\u5e38\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u5b83\u4eec\uff0c\u9664\u975e\u6709\u7279\u522b\u597d\u7684\u7406\u7531\u4f7f\u7528\u5b83\u4eec\u3002","title":"13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570"},{"location":"13-14-static-member-functions/#_1","text":"\u5728\u4e0a\u4e00\u8282\u8bfe 13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf \u4e2d\uff0c\u6211\u4eec\u77e5\u9053\u4e86 \u9759\u6001\u6210\u5458\u53d8\u91cf \u662f\u5c5e\u4e8e\u7c7b\u800c\u4e0d\u662f\u7c7b\u5bf9\u8c61\u7684\u3002\u5982\u679c\u9759\u6001\u6210\u5458\u53d8\u91cf\u662f \u516c\u6709\u6210\u5458 \u7684\u8bdd\u6211\u4eec\u81ea\u7136\u53ef\u4ee5\u901a\u8fc7\u7c7b\u540d \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u6765\u8bbf\u95ee\u5b83\uff0c\u4f46\u662f\u5982\u679c\u5b83\u662f \u79c1\u6709\u6210\u5458 \u7684\u8bdd\uff0c\u6211\u4eec\u5e94\u8be5\u5982\u4f55\u8bbf\u95ee\u5b83\u5462\uff1f\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 class Something { private : static int s_value ; }; int Something :: s_value { 1 }; // \u521d\u59cb\u5316\u3002\u5373\u4f7f s_value \u662f\u79c1\u6709\u7684\u4e5f\u53ef\u4ee5\u8fd9\u6837\u505a\uff0c\u56e0\u4e3a\u6b64\u5904\u662f\u5728\u201c\u5b9a\u4e49\u201d int main () { // \u5e94\u8be5\u5982\u4f55\u8bbf\u95ee\u79c1\u6709\u6210\u5458 Something::s_value \u5462\uff1f } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4e0d\u80fd\u5728 main() \u51fd\u6570\u4e2d\u76f4\u63a5\u8bbf\u95ee Something::s_value \uff0c\u56e0\u4e3a\u5b83\u662f\u79c1\u6709\u6210\u5458\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u79c1\u6709\u6210\u5458\u53d8\u91cf\u9700\u8981\u901a\u8fc7\u516c\u6709\u6210\u5458\u51fd\u6570\u6765\u8bbf\u95ee\u3002\u5c3d\u7ba1\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u666e\u901a\u7684\u516c\u6709\u6210\u5458\u51fd\u6570\u6765\u8bbf\u95ee s_value \uff0c\u4f46\u5f88\u663e\u7136\u6211\u4eec\u5fc5\u987b\u5b9e\u4f8b\u5316\u4e00\u4e2a\u5bf9\u8c61\u624d\u80fd\u591f\u8c03\u7528\u8be5\u6210\u5458\u51fd\u6570\uff01\u66f4\u597d\u7684\u529e\u6cd5\u662f\u5c06\u8be5\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u4e3a\u9759\u6001\u7684\u3002 \u548c\u9759\u6001\u6210\u5458\u51fd\u6570\u4e00\u6837\uff0c\u9759\u6001\u6210\u5458\u51fd\u6570\u4e0d\u5c5e\u4e8e\u4efb\u4f55\u5bf9\u8c61\u3002\u4f7f\u7528\u9759\u6001\u6210\u5458\u51fd\u6570\u6765\u8bbf\u95ee s_value \u7684\u65b9\u6cd5\u89c1\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include class Something { private : static int s_value ; public : static int getValue () { return s_value ; } // static member function }; int Something :: s_value { 1 }; // initializer int main () { std :: cout << Something :: getValue () << '\\n' ; } \u56e0\u4e3a\u9759\u6001\u6210\u5458\u51fd\u6570\u4e0d\u5c5e\u4e8e\u4efb\u4f55\u7279\u5b9a\u5bf9\u8c61\uff0c\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528\u7c7b\u540d\u548c\u4f5c\u7528\u57df\u89e3\u6790\u64cd\u4f5c\u7b26\u76f4\u63a5\u8c03\u7528\u5b83\u4eec\u3002\u4e0e\u9759\u6001\u6210\u5458\u53d8\u91cf\u4e00\u6837\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8c61\u8c03\u7528\u5b83\u4eec\uff0c\u4f46\u4e0d\u5efa\u8bae\u8fd9\u6837\u505a\u3002","title":"\u9759\u6001\u6210\u5458\u51fd\u6570"},{"location":"13-14-static-member-functions/#this","text":"\u9759\u6001\u6210\u5458\u51fd\u6570\u6709\u4e24\u4e2a\u6709\u8da3\u7684\u7279\u6027\u503c\u5f97\u6ce8\u610f\u3002\u9996\u5148\uff0c\u56e0\u4e3a\u9759\u6001\u6210\u5458\u51fd\u6570\u4e0d\u5c5e\u4e8e\u67d0\u4e2a\u5bf9\u8c61\uff0c\u6240\u4ee5\u5b83\u4eec\u6ca1\u6709 this \u6307\u9488\uff01\u4ed4\u7ec6\u60f3\u60f3\uff0c\u8fd9\u662f\u6709\u9053\u7406\u7684\u2014\u2014 this \u6307\u9488\u603b\u662f\u6307\u5411\u8be5\u6210\u5458\u51fd\u6570\u6b63\u5728\u64cd\u4f5c\u7684\u5bf9\u8c61\u3002\u9759\u6001\u6210\u5458\u51fd\u6570\u4e0d\u80fd\u4f5c\u7528\u4e8e\u5bf9\u8c61\uff0c\u56e0\u6b64\u4e0d\u9700\u8981 this \u6307\u9488\u3002 \u5176\u6b21\uff0c\u9759\u6001\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5176\u4ed6\u9759\u6001\u6210\u5458(\u53d8\u91cf\u6216\u51fd\u6570)\uff0c\u4f46\u4e0d\u80fd\u8bbf\u95ee\u975e\u9759\u6001\u6210\u5458\u3002\u8fd9\u662f\u56e0\u4e3a\u975e\u9759\u6001\u6210\u5458\u5fc5\u987b\u5c5e\u4e8e\u7c7b\u5bf9\u8c61\uff0c\u800c\u9759\u6001\u6210\u5458\u51fd\u6570\u6ca1\u6709\u53ef\u4f7f\u7528\u7684\u7c7b\u5bf9\u8c61!","title":"\u9759\u6001\u6210\u5458\u53d8\u91cf\u6ca1\u6709*this\u6307\u9488"},{"location":"13-14-static-member-functions/#_2","text":"\u9759\u6001\u6210\u5458\u51fd\u6570\u4e5f\u53ef\u4ee5\u5728\u7c7b\u58f0\u660e\u4e4b\u5916\u5b9a\u4e49\u3002\u8fd9\u4e0e\u666e\u901a\u6210\u5458\u51fd\u6570\u7684\u5de5\u4f5c\u65b9\u5f0f\u76f8\u540c\u3002 \u4e00\u4e2a\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include class IDGenerator { private : static int s_nextID ; // \u9759\u6001\u6210\u5458\u7684\u58f0\u660e public : static int getNextID (); // \u9759\u6001\u51fd\u6570\u7684\u58f0\u660e }; // \u5728\u7c7b\u5916\u5b9a\u4e49\u9759\u6001\u6210\u5458\u3002\u6ce8\u610f\uff0c\u6211\u4eec\u5728\u6b64\u5904\u6ca1\u6709\u4f7f\u7528 static \u5173\u952e\u5b57\u3002 // ID \u4ece 1 \u5f00\u59cb int IDGenerator :: s_nextID { 1 }; // \u7c7b\u5916\u5b9a\u4e49\u9759\u6001\u6210\u5458\u51fd\u6570\u3002\u6ce8\u610f\uff0c\u6211\u4eec\u5728\u6b64\u5904\u6ca1\u6709\u4f7f\u7528 static \u5173\u952e\u5b57\u3002 int IDGenerator::getNextID () { return s_nextID ++ ; } int main () { for ( int count { 0 }; count < 5 ; ++ count ) std :: cout << \"The next ID is: \" << IDGenerator :: getNextID () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 4 5 The next ID is: 1 The next ID is: 2 The next ID is: 3 The next ID is: 4 The next ID is: 5 \u6ce8\u610f\uff0c\u56e0\u4e3a\u8be5\u7c7b\u4e2d\u7684\u6240\u6709\u6570\u636e\u548c\u51fd\u6570\u90fd\u662f\u9759\u6001\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u9700\u8981\u5b9e\u4f8b\u5316\u8be5\u7c7b\u7684\u5bf9\u8c61\u6765\u4f7f\u7528\u5b83\u7684\u529f\u80fd\uff01\u8be5\u7c7b\u5229\u7528\u4e00\u4e2a\u9759\u6001\u6210\u5458\u53d8\u91cf\u6765\u4fdd\u5b58\u4e0b\u4e00\u4e2a\u8981\u5206\u914d\u7684ID\u7684\u503c\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u9759\u6001\u6210\u5458\u51fd\u6570\u6765\u8fd4\u56de\u8be5ID\u5e76\u4f7f\u5176\u9012\u589e\u3002","title":"\u53c8\u4e00\u4e2a\u4f8b\u5b50"},{"location":"13-14-static-member-functions/#_3","text":"\u5bf9\u4e8e\u4e00\u4e2a\u6240\u6709\u6210\u5458\u90fd\u662f\u9759\u6001\u6210\u5458\u7684\u7c7b\u65f6\u8981\u5c0f\u5fc3\u3002\u5c3d\u7ba1\u8fd9\u79cd\u201c\u7eaf\u9759\u6001\u7c7b\u201d(\u4e5f\u79f0\u4e3a\u201c\u5355\u6001\u7c7b\u201d)\u53ef\u80fd\u5f88\u6709\u7528\uff0c\u4f46\u5b83\u4eec\u4e5f\u6709\u4e00\u4e9b\u6f5c\u5728\u7684\u7f3a\u70b9\u3002 \u9996\u5148\uff0c\u56e0\u4e3a\u6240\u6709\u9759\u6001\u6210\u5458\u90fd\u53ea\u5b9e\u4f8b\u5316\u4e00\u6b21\uff0c\u6240\u4ee5\u4e0d\u53ef\u80fd\u6709\u7eaf\u9759\u6001\u7c7b\u7684\u591a\u4e2a\u526f\u672c(\u9664\u975e\u514b\u9686\u8be5\u7c7b\u5e76\u91cd\u547d\u540d\u5b83)\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u9700\u8981\u4e24\u4e2a\u72ec\u7acb\u7684 IDGenerator \u5bf9\u8c61\uff0c\u8fd9\u5bf9\u4e8e\u5355\u4e2a\u7eaf\u9759\u6001\u7c7b\u6765\u8bf4\u662f\u4e0d\u53ef\u80fd\u7684\u3002 \u5176\u6b21\uff0c\u5728\u5173\u4e8e\u5168\u5c40\u53d8\u91cf\u7684\u4e00\u8bfe\u4e2d\uff0c\u4f60\u5df2\u7ecf\u4e86\u89e3\u5230\u4e86\u5168\u5c40\u53d8\u91cf\u7684\u5371\u9669\u6027\uff0c\u56e0\u4e3a\u4efb\u4f55\u4e00\u6bb5\u4ee3\u7801\u90fd\u53ef\u80fd\u6539\u53d8\u5168\u5c40\u53d8\u91cf\u7684\u503c\uff0c\u5e76\u6700\u7ec8\u7834\u574f\u53e6\u4e00\u6bb5\u770b\u8d77\u6765\u4e0d\u76f8\u5173\u7684\u4ee3\u7801\u3002\u5bf9\u4e8e\u7eaf\u9759\u6001\u7c7b\u4e5f\u662f\u5982\u6b64\u3002\u56e0\u4e3a\u6240\u6709\u6210\u5458\u90fd\u5c5e\u4e8e\u7c7b(\u800c\u4e0d\u662f\u7c7b\u7684\u5bf9\u8c61)\uff0c\u800c\u4e14\u7c7b\u58f0\u660e\u901a\u5e38\u5177\u6709\u5168\u5c40\u4f5c\u7528\u57df\uff0c\u6240\u4ee5\u7eaf\u9759\u6001\u7c7b\u672c\u8d28\u4e0a\u76f8\u5f53\u4e8e\u5728\u5168\u5c40\u53ef\u8bbf\u95ee\u7684\u540d\u79f0\u7a7a\u95f4\u4e2d\u58f0\u660e\u51fd\u6570\u548c\u5168\u5c40\u53d8\u91cf\uff0c\u5e76\u5177\u6709\u5168\u5c40\u53d8\u91cf\u6240\u5177\u6709\u7684\u6240\u6709\u5fc5\u8981\u7f3a\u70b9\u3002","title":"\u6709\u5173\u5168\u9759\u6001\u6210\u5458\u7c7b\u7684\u4e00\u4e9b\u8b66\u793a"},{"location":"13-14-static-member-functions/#c","text":"\u65e2\u7136\u53ef\u4ee5\u901a\u8fc7\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u666e\u901a\u6210\u5458\u53d8\u91cf\uff0c\u90a3\u4e48\u4e5f\u5e94\u8be5\u80fd\u591f\u901a\u8fc7\u9759\u6001\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u9759\u6001\u6210\u5458\u53d8\u91cf\u3002\u867d\u7136\u4e00\u4e9b\u73b0\u4ee3\u8bed\u8a00\u786e\u5b9e\u51fa\u4e8e\u8fd9\u4e2a\u76ee\u7684\u6dfb\u52a0\u4e86\u5bf9\u9759\u6001\u6784\u9020\u51fd\u6570\u7684\u652f\u6301\uff0c\u4f46\u4e0d\u5e78\u7684\u662fC++\u4e0d\u662f\u5176\u4e2d\u4e4b\u4e00\u3002 \u5982\u679c\u53ef\u4ee5\u76f4\u63a5\u521d\u59cb\u5316\u9759\u6001\u53d8\u91cf\uff0c\u5219\u4e0d\u9700\u8981\u6784\u9020\u51fd\u6570\uff1a\u53ef\u4ee5\u5728\u5b9a\u4e49\u70b9\u521d\u59cb\u5316\u9759\u6001\u6210\u5458\u53d8\u91cf(\u5373\u4f7f\u5b83\u662f\u79c1\u6709\u7684)\u3002\u6211\u4eec\u5728\u4e0a\u9762\u7684IDGenerator\u793a\u4f8b\u4e2d\u6b63\u662f\u8fd9\u6837\u505a\u7684\u3002\u8fd9\u662f\u53e6\u4e00\u4e2a\u4f8b\u5b50: 1 2 3 4 5 6 7 class MyClass { public : static std :: vector < char > s_mychars ; }; std :: vector < char > MyClass :: s_mychars { 'a' , 'e' , 'i' , 'o' , 'u' }; // \u5728\u5b9a\u4e49\u65f6\u521d\u59cb\u5316\u9759\u6001\u53d8\u91cf \u5982\u679c\u9700\u8981\u6267\u884c\u4ee3\u7801(\u4f8b\u5982\u5faa\u73af)\u624d\u80fd\u521d\u59cb\u5316\u9759\u6001\u6210\u5458\u53d8\u91cf\u7684\u8bdd\uff0c\u6709\u8bb8\u591a\u4e0d\u540c\u7684\u3001\u7b28\u62d9\u7684\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u6709\u4e00\u79cd\u5904\u7406\u65b9\u6cd5\u9002\u7528\u4e8e\u6240\u6709\u53d8\u91cf(\u9759\u6001\u6216\u975e\u9759\u6001)\uff0c\u5373\u4f7f\u7528lambda\u51fd\u6570\u5e76\u7acb\u5373\u8c03\u7528\u5b83\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class MyClass { public : static std :: vector < char > s_mychars ; }; std :: vector < char > MyClass :: s_mychars { []{ // The parameter list of lambdas without parameters can be omitted. // Inside the lambda we can declare another vector and use a loop. std :: vector < char > v {}; for ( char ch { 'a' }; ch <= 'z' ; ++ ch ) { v . push_back ( ch ); } return v ; }() // Call the lambda right away }; \u4e0b\u9762\u7684\u4ee3\u7801\u7ed9\u51fa\u4e86\u4e00\u4e2a\u884c\u4e3a\u66f4\u50cf\u5e38\u89c4\u6784\u9020\u51fd\u6570\u7684\u65b9\u6cd5\u3002\u4f46\u662f\uff0c\u60a8\u53ef\u80fd\u6c38\u8fdc\u90fd\u4e0d\u9700\u8981\u5b83\uff0c\u6240\u4ee5\u5982\u679c\u60a8\u613f\u610f\uff0c\u53ef\u4ee5\u968f\u610f\u8df3\u8fc7\u672c\u8282\u7684\u5176\u4f59\u90e8\u5206\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class MyClass { public : static std :: vector < char > s_mychars ; class init_static // we're defining a nested class named init_static { public : init_static () // the init constructor will initialize our static variable { for ( char ch { 'a' }; ch <= 'z' ; ++ ch ) { s_mychars . push_back ( ch ); } } } ; private : static init_static s_initializer ; // we'll use this static object to ensure the init_static constructor is called }; std :: vector < char > MyClass :: s_mychars {}; // define our static member variable MyClass :: init_static MyClass :: s_initializer {}; // define our static initializer, which will call the init_static constructor, which will initialize s_mychars \u5f53\u5b9a\u4e49\u9759\u6001\u6210\u5458 s_initializer \u65f6\uff0c\u5c06\u8c03\u7528 init_static() \u9ed8\u8ba4\u6784\u9020\u51fd\u6570(\u56e0\u4e3a s_initializer \u662f init_static \u7c7b\u578b)\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u4efb\u4f55\u9759\u6001\u6210\u5458\u53d8\u91cf\u3002\u8fd9\u4e2a\u89e3\u51b3\u65b9\u6848\u7684\u4f18\u70b9\u662f\uff0c\u6240\u6709\u521d\u59cb\u5316\u4ee3\u7801\u90fd\u9690\u85cf\u5728\u5e26\u6709\u9759\u6001\u6210\u5458\u7684\u539f\u59cb\u7c7b\u4e2d\u3002","title":"C++ \u4e0d\u652f\u6301\u9759\u6001\u6784\u9020\u51fd\u6570"},{"location":"13-14-static-member-functions/#_4","text":"\u9759\u6001\u6210\u5458\u51fd\u6570\u53ef\u7528\u4e8e\u914d\u5408\u7c7b\u4e2d\u7684\u9759\u6001\u6210\u5458\u53d8\u91cf\u6765\u5de5\u4f5c\u3002\u7c7b\u5bf9\u8c61\u4e0d\u9700\u8981\u8c03\u7528\u5b83\u4eec\u3002 \u4e00\u4e2a\u7c7b\u7684\u6240\u6709\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u90fd\u662f\u9759\u6001\u7684\u3002\u7136\u800c\uff0c\u6b64\u7c7b\u7c7b\u672c\u8d28\u4e0a\u76f8\u5f53\u4e8e\u5728\u5168\u5c40\u53ef\u8bbf\u95ee\u7684\u540d\u79f0\u7a7a\u95f4\u4e2d\u58f0\u660e\u51fd\u6570\u548c\u5168\u5c40\u53d8\u91cf\uff0c\u901a\u5e38\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u5b83\u4eec\uff0c\u9664\u975e\u6709\u7279\u522b\u597d\u7684\u7406\u7531\u4f7f\u7528\u5b83\u4eec\u3002","title":"\u5c0f\u7ed3"},{"location":"13-15-friend-functions-and-classes/","tags":["class","friend"],"text":"\u672c\u7ae0\u4e2d\u5f88\u5927\u90e8\u5206\u7bc7\u5e45\u4e2d\u6211\u4eec\u90fd\u5728\u5f3a\u8c03\u5c01\u88c5\uff08\u6570\u636e\u79c1\u6709\uff09\u7684\u91cd\u8981\u6027\u3002\u4e0d\u8fc7\uff0c\u6709\u65f6\u5019\u6211\u4eec\u4e5f\u4f1a\u53d1\u73b0\uff0c\u6709\u4e9b\u7c7b\u548c\u51fd\u6570\u4f1a\u548c\u5176\u4ed6\u5176\u4ed6\u5916\u90e8\u7c7b\u5bc6\u5207\u914d\u5408\u5de5\u4f5c\u3002\u4f8b\u5982\uff0c\u6709\u4e00\u4e2a\u7c7b\u8d1f\u8d23\u5b58\u653e\u6570\u636e\uff0c\u800c\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\uff08\u6216\u53e6\u5916\u4e00\u4e2a\u7c7b\uff09\u7528\u4e8e\u5c06\u6570\u636e\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\u3002\u5c3d\u7ba1\u5b58\u50a8\u7c7b\u548c\u663e\u793a\u4ee3\u7801\u51fa\u4e8e\u4fbf\u4e8e\u7ef4\u62a4\u7684\u76ee\u7684\u800c\u88ab\u5206\u5272\u5f00\u6765\uff0c\u4f46\u662f\u663e\u793a\u4ee3\u7801\u5b9e\u9645\u4e0a\u975e\u5e38\u7d27\u5bc6\u5730\u4f9d\u9760\u7740\u5b58\u50a8\u7c7b\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u5c06\u5b58\u50a8\u7c7b\u7684\u7ec6\u8282\u5bf9\u663e\u793a\u4ee3\u7801\u9690\u85cf\u8d77\u6765\u5e76\u65e0\u592a\u591a\u5e2e\u52a9\u3002 \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u6709\u4e24\u79cd\u9009\u62e9\uff1a \u8ba9\u663e\u793a\u4ee3\u7801\u4f7f\u7528\u5b58\u50a8\u7c7b\u7684\u516c\u6709\u51fd\u6570\u3002\u7136\u800c\uff0c\u8fd9\u4e48\u505a\u6709\u51e0\u4e2a\u6f5c\u5728\u7684\u7f3a\u70b9\u3002\u9996\u5148\uff0c\u5fc5\u987b\u5b9a\u4e49\u8fd9\u4e9b\u516c\u5171\u6210\u5458\u51fd\u6570\uff0c\u8fd9\u9700\u8981\u65f6\u95f4\uff0c\u800c\u4e14\u53ef\u80fd\u4f1a\u6253\u4e71\u5b58\u50a8\u7c7b\u7684\u63a5\u53e3\u3002\u5176\u6b21\uff0c\u5b58\u50a8\u7c7b\u53ef\u80fd\u5fc5\u987b\u66b4\u9732\u4e00\u4e9b\u63a5\u53e3\u7ed9\u663e\u793a\u4ee3\u7801\uff0c\u4f46\u662f\u8fd9\u4e9b\u4ee3\u7801\u5e76\u4e0d\u5e94\u8be5\u66b4\u9732\u7ed9\u5176\u4ed6\u6a21\u5757\uff0c\u6211\u4eec\u4e0d\u5e0c\u671b\u5176\u4ed6\u4efb\u4f55\u4eba\u90fd\u80fd\u8bbf\u95ee\u8fd9\u4e9b\u51fd\u6570\u3002\u6ca1\u6709\u529e\u6cd5\u6307\u5b9a\u8fd9\u4e2a\u51fd\u6570\u53ea\u80fd\u88ab\u663e\u793a\u7c7b\u4f7f\u7528\u3002 \u6216\u8005\uff0c\u4f7f\u7528\u53cb\u5143\u7c7b\u548c\u53cb\u5143\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u8ba9\u663e\u793a\u4ee3\u7801\u8bbf\u95ee\u5b58\u50a8\u7c7b\u7684\u79c1\u6709\u7ec6\u8282\u3002\u8fd9\u5141\u8bb8\u663e\u793a\u4ee3\u7801\u76f4\u63a5\u8bbf\u95ee\u5b58\u50a8\u7c7b\u7684\u6240\u6709\u79c1\u6709\u6210\u5458\u548c\u51fd\u6570\uff0c\u540c\u65f6\u5c06\u5176\u4ed6\u6240\u6709\u4eba\u6392\u9664\u5728\u5916\uff01\u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u4ed4\u7ec6\u770b\u770b\u8fd9\u662f\u5982\u4f55\u505a\u5230\u7684\u3002 \u53cb\u5143\u51fd\u6570 \u53cb\u5143\u51fd\u6570 \u53ef\u4ee5\u50cf\u6210\u5458\u51fd\u6570\u4e00\u6837\u8bbf\u95ee\u4e00\u4e2a\u7c7b\u7684 \u79c1\u6709\u6210\u5458 \u3002\u4ece\u5404\u4e2a\u65b9\u9762\u6765\u770b\uff0c\u53cb\u5143\u51fd\u6570\u90fd\u548c\u666e\u901a\u51fd\u6570\u6ca1\u4ec0\u4e48\u533a\u522b\u3002\u53cb\u5143\u51fd\u6570\u53ef\u4ee5\u662f\u666e\u901a\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u662f\u5176\u4ed6\u7c7b\u7684\u6210\u5458\u51fd\u6570\u3002\u58f0\u660e\u4e00\u4e2a\u53cb\u5143\u51fd\u6570\uff0c\u53ea\u9700\u8981\u5728\u4f60\u5e0c\u671b\u6210\u4e3a\u53cb\u5143\u7684\u51fd\u6570\u539f\u578b\u524d\u6dfb\u52a0 friend \u5173\u952e\u5b57\u5373\u53ef\u3002\u5c06\u53cb\u5143\u51fd\u6570\u5b9a\u4e49\u5728private\u8fd8\u662fpublic\u4e0b\u6ca1\u4ec0\u4e48\u533a\u522b\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u53cb\u5143\u51fd\u6570\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 class Accumulator { private : int m_value { 0 }; public : void add ( int value ) { m_value += value ; } // \u4f7f reset() \u51fd\u6570\u6210\u4e3a\u8be5\u7c7b\u7684\u53cb\u5143\u51fd\u6570 friend void reset ( Accumulator & accumulator ); }; // reset() \u73b0\u5728\u662f Accumulator \u7c7b\u7684\u53cb\u5143 void reset ( Accumulator & accumulator ) { // \u53ef\u4ee5\u8bbf\u95ee Accumulator \u7684\u79c1\u6709\u6210\u5458 accumulator . m_value = 0 ; } int main () { Accumulator acc ; acc . add ( 5 ); // add 5 to the accumulator reset ( acc ); // reset the accumulator to 0 return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a\u540d\u4e3a reset() \u7684\u51fd\u6570\uff0c\u5b83\u63a5\u53d7 Accumulator \u7c7b\u7684\u5bf9\u8c61\uff0c\u5e76\u5c06 m_value \u7684\u503c\u8bbe\u7f6e\u4e3a0\u3002\u56e0\u4e3a reset() \u4e0d\u662f Accumulator \u7c7b\u7684\u6210\u5458\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c reset() \u4e0d\u80fd\u8bbf\u95ee Accumulator \u7684\u79c1\u6709\u6210\u5458\u3002\u4f46\u662f\uff0c\u56e0\u4e3a Accumulator \u5df2\u7ecf\u7279\u522b\u58f0\u660e\u4e86\u8fd9\u4e2a reset() \u51fd\u6570\u662f\u8fd9\u4e2a\u7c7b\u7684\u53cb\u5143\uff0c\u6240\u4ee5\u8ba9 reset() \u51fd\u6570\u8bbf\u95ee Accumulator \u7684\u79c1\u6709\u6210\u5458\u3002 \u6ce8\u610f\uff0c\u6211\u4eec\u5fc5\u987b\u5411 reset() \u4f20\u9012\u4e00\u4e2a Accumulator \u5bf9\u8c61\u3002\u8fd9\u662f\u56e0\u4e3a reset() \u4e0d\u662f\u6210\u5458\u51fd\u6570\u3002\u5b83\u6ca1\u6709 *this \u6307\u9488\uff0c\u4e5f\u6ca1\u6709 Accumulator \u5bf9\u8c61\u53ef\u4ee5\u7528\u6765\u64cd\u4f5c\uff0c\u9664\u975e\u7ed9\u5b9a\u4e00\u4e2a\u3002 \u518d\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include class Value { private : int m_value {}; public : Value ( int value ) : m_value { value } { } friend bool isEqual ( const Value & value1 , const Value & value2 ); }; bool isEqual ( const Value & value1 , const Value & value2 ) { return ( value1 . m_value == value2 . m_value ); } int main () { Value v1 { 5 }; Value v2 { 6 }; std :: cout << std :: boolalpha << isEqual ( v1 , v2 ); return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u6211\u4eec\u5c06\u51fd\u6570 isEqual() \u51fd\u6570\u58f0\u660e\u4e3a Value \u7c7b\u7684\u53cb\u5143\u3002 isEqual() \u63a5\u53d7\u4e24\u4e2a Value \u5bf9\u8c61\u4f5c\u4e3a \u5f62\u53c2 \u3002\u56e0\u4e3a isEqual() \u662f Value \u7684\u53cb\u5143\u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u8bbf\u95ee Value \u5bf9\u8c61\u7684\u6240\u6709\u79c1\u6709\u6210\u5458\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\uff0c\u5982\u679c\u76f8\u7b49\u5219\u8fd4\u56de true\u3002 \u867d\u7136\u4e0a\u9762\u7684\u4e24\u4e2a\u4f8b\u5b50\u90fd\u662f\u523b\u610f\u8bbe\u8ba1\u7684\uff0c\u4f46\u7b2c\u4e8c\u4e2a\u4f8b\u5b50\u4e0e\u6211\u4eec\u7a0d\u540e\u4f1a\u8ba8\u8bba\u8fd0\u7b97\u7b26\u91cd\u8f7d\u65f6\u9047\u5230\u7684\u60c5\u51b5\u975e\u5e38\u76f8\u4f3c! \u591a\u4e2a\u53cb\u5143 \u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u540c\u65f6\u6210\u4e3a\u591a\u4e2a\u7c7b\u7684\u53cb\u5143\u3002\u4f8b\u5982\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include class Humidity ; class Temperature { private : int m_temp {}; public : Temperature ( int temp = 0 ) : m_temp { temp } { } friend void printWeather ( const Temperature & temperature , const Humidity & humidity ); }; class Humidity { private : int m_humidity {}; public : Humidity ( int humidity = 0 ) : m_humidity { humidity } { } friend void printWeather ( const Temperature & temperature , const Humidity & humidity ); }; void printWeather ( const Temperature & temperature , const Humidity & humidity ) { std :: cout << \"The temperature is \" << temperature . m_temp << \" and the humidity is \" << humidity . m_humidity << '\\n' ; } int main () { Humidity hum { 10 }; Temperature temp { 12 }; printWeather ( temp , hum ); return 0 ; } \u5173\u4e8e\u8fd9\u4e2a\u793a\u4f8b\uff0c\u6709\u4e24\u70b9\u503c\u5f97\u6ce8\u610f\u3002\u9996\u5148\uff0c\u56e0\u4e3a printWeather \u662f\u8fd9\u4e24\u4e2a\u7c7b\u7684\u53cb\u5143\u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u8bbf\u95ee\u8fd9\u4e24\u4e2a\u7c7b\u7684\u5bf9\u8c61\u7684\u79c1\u6709\u6570\u636e\u3002\u5176\u6b21\uff0c\u6ce8\u610f\u793a\u4f8b\u4e2d\u9876\u90e8\u7684\u8fd9\u4e00\u884c\uff1a 1 class Humidity ; \u8fd9\u662f\u4e00\u4e2a\u7c7b\u539f\u578b\uff0c\u5b83\u544a\u8bc9\u7f16\u8bd1\u5668\u6211\u4eec\u5c06\u5728\u672a\u6765\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a Humidity \u7684\u7c7b\u3002\u5982\u679c\u6ca1\u6709\u8fd9\u4e00\u884c\uff0c\u7f16\u8bd1\u5668\u5728\u89e3\u6790 Temperature \u7c7b\u4e2d printWeather() \u7684\u539f\u578b\u65f6\u5c31\u4e0d\u77e5\u9053 Humidity \u662f\u4ec0\u4e48\u3002\u7c7b\u539f\u578b\u7684\u4f5c\u7528\u4e0e\u51fd\u6570\u539f\u578b\u76f8\u540c\u2014\u2014\u5b83\u4eec\u544a\u8bc9\u7f16\u8bd1\u5668\u67d0\u4e2a\u4e1c\u897f\u662f\u4ec0\u4e48\u6837\u5b50\u7684\uff0c\u4ee5\u4fbf\u73b0\u5728\u53ef\u4ee5\u4f7f\u7528\u5b83\uff0c\u4ee5\u540e\u53ef\u4ee5\u5b9a\u4e49\u5b83\u3002\u7136\u800c\uff0c\u4e0e\u51fd\u6570\u4e0d\u540c\u7684\u662f\uff0c\u7c7b\u6ca1\u6709\u8fd4\u56de\u7c7b\u578b\u6216\u53c2\u6570\uff0c\u56e0\u6b64\u7c7b\u539f\u578b\u603b\u662f\u7b80\u5355\u7684 class ClassName \uff0c\u5176\u4e2d ClassName \u662f\u7c7b\u7684\u540d\u79f0\u3002 \u53cb\u5143\u7c7b \u4e5f\u53ef\u4ee5\u8ba9\u6574\u4e2a\u7c7b\u6210\u4e3a\u53e6\u4e00\u4e2a\u7c7b\u7684\u53cb\u5143\u3002\u8fd9\u4f7f\u53cb\u5143\u7c7b\u7684\u6240\u6709\u6210\u5458\u90fd\u53ef\u4ee5\u8bbf\u95ee\u53e6\u4e00\u4e2a\u7c7b\u7684\u79c1\u6709\u6210\u5458\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include class Storage { private : int m_nValue {}; double m_dValue {}; public : Storage ( int nValue , double dValue ) : m_nValue { nValue }, m_dValue { dValue } { } // \u4f7f Display \u7c7b\u79f0\u4e3a Storage \u7684\u53cb\u5143 friend class Display ; }; class Display { private : bool m_displayIntFirst ; public : Display ( bool displayIntFirst ) : m_displayIntFirst { displayIntFirst } { } void displayItem ( const Storage & storage ) { if ( m_displayIntFirst ) std :: cout << storage . m_nValue << ' ' << storage . m_dValue << '\\n' ; else // display double first std :: cout << storage . m_dValue << ' ' << storage . m_nValue << '\\n' ; } }; int main () { Storage storage { 5 , 6.7 }; Display display { false }; display . displayItem ( storage ); return 0 ; } \u56e0\u4e3a Display \u7c7b\u662f Storage \u7684\u53cb\u5143\uff0c\u6240\u4ee5 Display \u7684\u4efb\u4f55\u6210\u5458\u90fd\u53ef\u4ee5 Storage \u7684\u79c1\u6709\u6210\u5458\u3002\u4e0a\u8ff0\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 6.7 5 \u6709\u5173\u53cb\u5143\u7c7b\uff0c\u8fd8\u6709\u4e00\u4e9b\u4e8b\u60c5\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0c\u5373\u4fbf Display \u662f Storage \u7684\u53cb\u5143\uff0c\u4f46\u662f Display \u5e76\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee Storage \u5bf9\u8c61\u7684 *this \u6307\u9488\u3002\u5176\u6b21\uff0c\u56e0\u4e3a Display \u662f Storage \u7684\u53cb\u5143\u7c7b\uff0c\u4f46\u662f\u8fd9\u4e0d\u4ee3\u8868 Storage \u4e5f\u662f Display \u7684\u53cb\u5143\u3002\u5982\u679c\u4f60\u5e0c\u671b\u4e24\u4e2a\u7c7b\u4e92\u4e3a\u53cb\u5143\uff0c\u963f\u4e48\u5fc5\u987b\u5206\u522b\u5728\u7c7b\u4e2d\u5c06\u5bf9\u65b9\u58f0\u660e\u4e3a\u53cb\u5143\u3002\u6700\u540e\uff0c\u5982\u679cA\u662fB\u7684\u53cb\u5143\uff0c\u800cB\u662fC\u7684\u53cb\u5143\uff0c\u4f46\u4e0d\u4ee3\u8868A\u662fC\u7684\u53cb\u5143\u3002 \u5728\u4f7f\u7528\u53cb\u51fd\u6570\u548c\u7c7b\u65f6\u8981\u5c0f\u5fc3\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u53cb\u51fd\u6570\u6216\u7c7b\u6253\u7834\u5c01\u88c5\u3002\u5982\u679c\u7c7b\u6539\u53d8\u4e86\uff0c\u53cb\u5143\u7684\u4e5f\u9700\u8981\u88ab\u8feb\u6539\u53d8\u3002\u56e0\u6b64\uff0c\u5c3d\u91cf\u51cf\u5c11\u5bf9\u53cb\u51fd\u6570\u548c\u53cb\u5143\u7c7b\u7684\u4f7f\u7528\u3002 \u53cb\u5143\u6210\u5458\u51fd\u6570 \u53ef\u4ee5\u5c06\u5355\u4e2a\u6210\u5458\u51fd\u6570\u8bbe\u4e3a\u53cb\u5143\uff0c\u800c\u4e0d\u662f\u5c06\u6574\u4e2a\u7c7b\u8bbe\u4e3a\u53cb\u5143\u3002\u8fd9\u4e0e\u5c06\u666e\u901a\u51fd\u6570\u8bbe\u4e3a\u53cb\u5143\u7c7b\u4f3c\uff0c\u53ea\u662f\u5728\u4f7f\u7528\u6210\u5458\u51fd\u6570\u7684\u540d\u79f0\u65f6\u5305\u542b\u4e86 className:: \u524d\u7f00\uff08\u4f8b\u5982 Display::displayItem )\u3002 \u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u6bd4\u9884\u671f\u7684\u8981\u590d\u6742\u4e00\u4e9b\u3002\u8ba9\u6211\u4eec\u6539\u5199\u4e00\u4e0b\u524d\u9762\u7684\u793a\u4f8b\uff0c\u4f7f Display::displayItem \u6210\u4e3a\u53cb\u5143\u6210\u5458\u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include class Display ; // Display \u7684\u524d\u5411\u58f0\u660e class Storage { private : int m_nValue {}; double m_dValue {}; public : Storage ( int nValue , double dValue ) : m_nValue { nValue }, m_dValue { dValue } { } // \u4f7f Display::displayItem \u6210\u5458\u51fd\u6570\u6210\u4e3a Storage \u7684\u53cb\u5143 friend void Display :: displayItem ( const Storage & storage ); // \u9519\u8bef: Storage \u6b64\u65f6\u5e76\u4e0d\u77e5\u9053 Display \u7c7b\u7684\u5b8c\u6574\u5b9a\u4e49\u3002 }; class Display { private : bool m_displayIntFirst {}; public : Display ( bool displayIntFirst ) : m_displayIntFirst { displayIntFirst } { } void displayItem ( const Storage & storage ) { if ( m_displayIntFirst ) std :: cout << storage . m_nValue << ' ' << storage . m_dValue << '\\n' ; else // display double first std :: cout << storage . m_dValue << ' ' << storage . m_nValue << '\\n' ; } }; \u5f88\u9057\u61be\uff0c\u8fd9\u4e48\u505a\u5e76\u4e0d\u80fd\u6b63\u786e\u5de5\u4f5c\u3002\u4e3a\u4e86\u8ba9\u4e00\u4e2a\u6210\u5458\u51fd\u6570\u79f0\u4e3a\u53cb\u5143\uff0c\u7f16\u8bd1\u5668\u5fc5\u987b\u770b\u5230\u8be5\u6210\u5458\u51fd\u6570\u7684\u5b8c\u6574\u5b9a\u4e49\u800c\u4e0d\u4ec5\u4ec5\u662f \u524d\u5411\u58f0\u660e \u3002\u56e0\u4e3a Storage \u8fd8\u6ca1\u6709\u770b\u5230 Display \u7684\u5b8c\u6574\u5b9a\u4e49\uff0c\u6240\u4ee5\u5f53\u6211\u4eec\u5728\u6b64\u5904\u58f0\u660e\u53cb\u5143\u51fd\u6570\u65f6\u4f1a\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ea\u8981\u5c06 Display \u7684\u5b9a\u4e49\u79fb\u52a8\u5230 Storage \u524d\u9762\u5373\u53ef\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include class Display { private : bool m_displayIntFirst {}; public : Display ( bool displayIntFirst ) : m_displayIntFirst { displayIntFirst } { } void displayItem ( const Storage & storage ) // error: compiler doesn't know what a Storage is { if ( m_displayIntFirst ) std :: cout << storage . m_nValue << ' ' << storage . m_dValue << '\\n' ; else // display double first std :: cout << storage . m_dValue << ' ' << storage . m_nValue << '\\n' ; } }; class Storage { private : int m_nValue {}; double m_dValue {}; public : Storage ( int nValue , double dValue ) : m_nValue { nValue }, m_dValue { dValue } { } // Make the Display::displayItem member function a friend of the Storage class friend void Display :: displayItem ( const Storage & storage ); // okay now }; \u4e0d\u8fc7\uff0c \u8fd9\u6837\u4e00\u6765\u4f1a\u5e26\u6765\u53e6\u5916\u7684\u95ee\u9898\uff0c\u56e0\u4e3a Display::displayItem() \u4f7f\u7528 Storage \u4f5c\u4e3a\u5f15\u7528\u53c2\u6570\u3002\u800c\u5f53\u6211\u4eec\u5c06 Storage \u7684\u5b9a\u4e49\u79fb\u52a8\u5230 Display \u540e\u9762\u65f6\uff0c \u7f16\u8bd1\u5668\u5c31\u4f1a\u62b1\u6028\u5b83\u4e0d\u77e5\u9053Storage \u662f\u4ec0\u4e48\uff0c\u6211\u4eec\u4e0d\u80fd\u518d\u8c03\u6574\u987a\u5e8f\u4e86\uff0c\u4e0d\u7136\u53c8\u4f1a\u51fa\u73b0\u4e4b\u524d\u7684\u95ee\u9898\u3002 \u5e78\u8fd0\u5730\u662f\uff0c\u53ea\u9700\u8981\u51e0\u6b65\u5c31\u53ef\u4ee5\u89e3\u51b3\u8be5\u95ee\u9898\u3002\u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u5bf9\u4e8e Storage \u7684\u524d\u5411\u58f0\u660e\u3002\u7b2c\u4e8c\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06 Display::displayItem() \u79fb\u52a8\u5230\u7c7b\u5916\u90e8\uff0c\u653e\u5728 Storage \u7c7b\u5b8c\u6574\u5b9a\u4e49\u7684\u540e\u3002 \u770b\u4e0a\u53bb\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 #include class Storage ; // Storage \u7684\u524d\u5411\u58f0\u660e class Display { private : bool m_displayIntFirst {}; public : Display ( bool displayIntFirst ) : m_displayIntFirst { displayIntFirst } { } void displayItem ( const Storage & storage ); // \u4e0a\u9762\u7684\u524d\u5411\u58f0\u660e\u5c31\u662f\u4e3a\u4e86\u8fd9\u4e00\u53e5\u58f0\u660e }; class Storage // Storage \u7c7b\u7684\u5b8c\u6574\u5b9a\u4e49 { private : int m_nValue {}; double m_dValue {}; public : Storage ( int nValue , double dValue ) : m_nValue { nValue }, m_dValue { dValue } { } // \u5c06 Display::displayItem \u58f0\u660e\u4e3a Storage \u7c7b\u7684\u53cb\u5143 (\u5fc5\u987b\u770b\u5230 Display \u7684\u5b8c\u6574\u5b9a\u4e49) friend void Display :: displayItem ( const Storage & storage ); }; // \u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49 Display::displayItem \u4e86\uff0c\u5b83\u5fc5\u987b\u770b\u5230 Storage \u7684\u5b8c\u6574\u5b9a\u4e49 void Display::displayItem ( const Storage & storage ) { if ( m_displayIntFirst ) std :: cout << storage . m_nValue << ' ' << storage . m_dValue << '\\n' ; else // display double first std :: cout << storage . m_dValue << ' ' << storage . m_nValue << '\\n' ; } int main () { Storage storage ( 5 , 6.7 ); Display display ( false ); display . displayItem ( storage ); return 0 ; } \u73b0\u5728\uff0c\u4ee3\u7801\u53ef\u4ee5\u6b63\u5e38\u7f16\u8bd1\u4e86\uff1a Storage \u7684\u524d\u5411\u58f0\u660e\u5bf9\u4e8e Display::displayItem() \u7684\u58f0\u660e\u6765\u8bf4\u8db3\u591f\u4e86\u3002 Display \u7684\u5b8c\u6574\u5b9a\u4e49\u53ef\u4ee5\u786e\u4fdd Display::displayItem() \u88ab\u5b9a\u4e49\u4e3a Storage \u7684\u53cb\u5143\u51fd\u6570\uff0c\u5982\u679c\u4f60\u8fd8\u662f\u611f\u5230\u56f0\u60d1\uff0c\u8bf7\u4ed4\u7ec6\u770b\u770b\u4e0a\u9762\u4ee3\u7801\u4e2d\u7684\u6ce8\u91ca\u3002 \u5982\u679c\u8fd9\u4ee4\u4f60\u611f\u5230\u56f0\u96be\u2014\u2014\u7684\u786e\u662f\u7684\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u8fd9\u4e48\u505a\u7684\u539f\u56e0\u53ea\u662f\u56e0\u4e3a\u6211\u4eec\u8bd5\u56fe\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u5b8c\u6210\u6240\u6709\u5de5\u4f5c\u3002\u66f4\u597d\u7684\u89e3\u51b3\u65b9\u6848\u662f\u5c06\u6bcf\u4e2a\u7c7b\u5b9a\u4e49\u653e\u5728\u5355\u72ec\u7684\u5934\u6587\u4ef6\u4e2d\uff0c\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u653e\u5728\u76f8\u5e94\u7684.cpp\u6587\u4ef6\u4e2d\u3002\u8fd9\u6837\uff0c\u6240\u6709\u7684\u7c7b\u5b9a\u4e49\u90fd\u5c06\u7acb\u5373\u5728.cpp\u6587\u4ef6\u4e2d\u53ef\u89c1\uff0c\u5e76\u4e14\u4e0d\u9700\u8981\u91cd\u65b0\u6392\u5217\u7c7b\u6216\u51fd\u6570! \u5c0f\u7ed3 \u53cb\u5143\u51fd\u6570\u6216\u53cb\u5143\u7c7b\u662f\u53ef\u4ee5\u8bbf\u95ee\u53e6\u4e00\u4e2a\u7c7b\u7684\u79c1\u6709\u6210\u5458\u7684\u51fd\u6570\u6216\u7c7b\uff0c\u5c31\u597d\u50cf\u5b83\u662f\u8be5\u7c7b\u7684\u6210\u5458\u4e00\u6837\u3002\u8fd9\u5141\u8bb8\u53cb\u5143\u51fd\u6570\u6216\u53cb\u5143\u7c7b\u4e0e\u53e6\u4e00\u4e2a\u7c7b\u7d27\u5bc6\u5730\u4e00\u8d77\u5de5\u4f5c\uff0c\u800c\u4e0d\u5fc5\u66b4\u9732\u4e00\u4e2a\u79c1\u6709\u6210\u5458(\u4f8b\u5982\u901a\u8fc7\u8bbf\u95ee\u51fd\u6570)\u3002 \u53cb\u5143\u901a\u5e38\u5728\u5b9a\u4e49\u91cd\u8f7d\u64cd\u4f5c\u7b26(\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u7ae0\u8ba8\u8bba)\u65f6\u4f7f\u7528\uff0c\u6216\u8005\uff08\u5c11\u6570\u65f6\u5019\uff09\u5728\u4e24\u4e2a\u6216\u591a\u4e2a\u7c7b\u9700\u8981\u4ee5\u4eb2\u5bc6\u7684\u65b9\u5f0f\u4e00\u8d77\u5de5\u4f5c\u65f6\u3002 \u6ce8\u610f\uff0c\u8981\u4f7f\u7279\u5b9a\u7684\u6210\u5458\u51fd\u6570\u6210\u4e3a\u53cb\u5143\uff0c\u9996\u5148\u9700\u8981\u770b\u5230\u6210\u5458\u51fd\u6570\u7c7b\u7684\u5b8c\u6574\u5b9a\u4e49\u3002","title":"13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b"},{"location":"13-15-friend-functions-and-classes/#_1","text":"\u53cb\u5143\u51fd\u6570 \u53ef\u4ee5\u50cf\u6210\u5458\u51fd\u6570\u4e00\u6837\u8bbf\u95ee\u4e00\u4e2a\u7c7b\u7684 \u79c1\u6709\u6210\u5458 \u3002\u4ece\u5404\u4e2a\u65b9\u9762\u6765\u770b\uff0c\u53cb\u5143\u51fd\u6570\u90fd\u548c\u666e\u901a\u51fd\u6570\u6ca1\u4ec0\u4e48\u533a\u522b\u3002\u53cb\u5143\u51fd\u6570\u53ef\u4ee5\u662f\u666e\u901a\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u662f\u5176\u4ed6\u7c7b\u7684\u6210\u5458\u51fd\u6570\u3002\u58f0\u660e\u4e00\u4e2a\u53cb\u5143\u51fd\u6570\uff0c\u53ea\u9700\u8981\u5728\u4f60\u5e0c\u671b\u6210\u4e3a\u53cb\u5143\u7684\u51fd\u6570\u539f\u578b\u524d\u6dfb\u52a0 friend \u5173\u952e\u5b57\u5373\u53ef\u3002\u5c06\u53cb\u5143\u51fd\u6570\u5b9a\u4e49\u5728private\u8fd8\u662fpublic\u4e0b\u6ca1\u4ec0\u4e48\u533a\u522b\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u53cb\u5143\u51fd\u6570\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 class Accumulator { private : int m_value { 0 }; public : void add ( int value ) { m_value += value ; } // \u4f7f reset() \u51fd\u6570\u6210\u4e3a\u8be5\u7c7b\u7684\u53cb\u5143\u51fd\u6570 friend void reset ( Accumulator & accumulator ); }; // reset() \u73b0\u5728\u662f Accumulator \u7c7b\u7684\u53cb\u5143 void reset ( Accumulator & accumulator ) { // \u53ef\u4ee5\u8bbf\u95ee Accumulator \u7684\u79c1\u6709\u6210\u5458 accumulator . m_value = 0 ; } int main () { Accumulator acc ; acc . add ( 5 ); // add 5 to the accumulator reset ( acc ); // reset the accumulator to 0 return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a\u540d\u4e3a reset() \u7684\u51fd\u6570\uff0c\u5b83\u63a5\u53d7 Accumulator \u7c7b\u7684\u5bf9\u8c61\uff0c\u5e76\u5c06 m_value \u7684\u503c\u8bbe\u7f6e\u4e3a0\u3002\u56e0\u4e3a reset() \u4e0d\u662f Accumulator \u7c7b\u7684\u6210\u5458\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c reset() \u4e0d\u80fd\u8bbf\u95ee Accumulator \u7684\u79c1\u6709\u6210\u5458\u3002\u4f46\u662f\uff0c\u56e0\u4e3a Accumulator \u5df2\u7ecf\u7279\u522b\u58f0\u660e\u4e86\u8fd9\u4e2a reset() \u51fd\u6570\u662f\u8fd9\u4e2a\u7c7b\u7684\u53cb\u5143\uff0c\u6240\u4ee5\u8ba9 reset() \u51fd\u6570\u8bbf\u95ee Accumulator \u7684\u79c1\u6709\u6210\u5458\u3002 \u6ce8\u610f\uff0c\u6211\u4eec\u5fc5\u987b\u5411 reset() \u4f20\u9012\u4e00\u4e2a Accumulator \u5bf9\u8c61\u3002\u8fd9\u662f\u56e0\u4e3a reset() \u4e0d\u662f\u6210\u5458\u51fd\u6570\u3002\u5b83\u6ca1\u6709 *this \u6307\u9488\uff0c\u4e5f\u6ca1\u6709 Accumulator \u5bf9\u8c61\u53ef\u4ee5\u7528\u6765\u64cd\u4f5c\uff0c\u9664\u975e\u7ed9\u5b9a\u4e00\u4e2a\u3002 \u518d\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include class Value { private : int m_value {}; public : Value ( int value ) : m_value { value } { } friend bool isEqual ( const Value & value1 , const Value & value2 ); }; bool isEqual ( const Value & value1 , const Value & value2 ) { return ( value1 . m_value == value2 . m_value ); } int main () { Value v1 { 5 }; Value v2 { 6 }; std :: cout << std :: boolalpha << isEqual ( v1 , v2 ); return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u6211\u4eec\u5c06\u51fd\u6570 isEqual() \u51fd\u6570\u58f0\u660e\u4e3a Value \u7c7b\u7684\u53cb\u5143\u3002 isEqual() \u63a5\u53d7\u4e24\u4e2a Value \u5bf9\u8c61\u4f5c\u4e3a \u5f62\u53c2 \u3002\u56e0\u4e3a isEqual() \u662f Value \u7684\u53cb\u5143\u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u8bbf\u95ee Value \u5bf9\u8c61\u7684\u6240\u6709\u79c1\u6709\u6210\u5458\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\uff0c\u5982\u679c\u76f8\u7b49\u5219\u8fd4\u56de true\u3002 \u867d\u7136\u4e0a\u9762\u7684\u4e24\u4e2a\u4f8b\u5b50\u90fd\u662f\u523b\u610f\u8bbe\u8ba1\u7684\uff0c\u4f46\u7b2c\u4e8c\u4e2a\u4f8b\u5b50\u4e0e\u6211\u4eec\u7a0d\u540e\u4f1a\u8ba8\u8bba\u8fd0\u7b97\u7b26\u91cd\u8f7d\u65f6\u9047\u5230\u7684\u60c5\u51b5\u975e\u5e38\u76f8\u4f3c!","title":"\u53cb\u5143\u51fd\u6570"},{"location":"13-15-friend-functions-and-classes/#_2","text":"\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u540c\u65f6\u6210\u4e3a\u591a\u4e2a\u7c7b\u7684\u53cb\u5143\u3002\u4f8b\u5982\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include class Humidity ; class Temperature { private : int m_temp {}; public : Temperature ( int temp = 0 ) : m_temp { temp } { } friend void printWeather ( const Temperature & temperature , const Humidity & humidity ); }; class Humidity { private : int m_humidity {}; public : Humidity ( int humidity = 0 ) : m_humidity { humidity } { } friend void printWeather ( const Temperature & temperature , const Humidity & humidity ); }; void printWeather ( const Temperature & temperature , const Humidity & humidity ) { std :: cout << \"The temperature is \" << temperature . m_temp << \" and the humidity is \" << humidity . m_humidity << '\\n' ; } int main () { Humidity hum { 10 }; Temperature temp { 12 }; printWeather ( temp , hum ); return 0 ; } \u5173\u4e8e\u8fd9\u4e2a\u793a\u4f8b\uff0c\u6709\u4e24\u70b9\u503c\u5f97\u6ce8\u610f\u3002\u9996\u5148\uff0c\u56e0\u4e3a printWeather \u662f\u8fd9\u4e24\u4e2a\u7c7b\u7684\u53cb\u5143\u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u8bbf\u95ee\u8fd9\u4e24\u4e2a\u7c7b\u7684\u5bf9\u8c61\u7684\u79c1\u6709\u6570\u636e\u3002\u5176\u6b21\uff0c\u6ce8\u610f\u793a\u4f8b\u4e2d\u9876\u90e8\u7684\u8fd9\u4e00\u884c\uff1a 1 class Humidity ; \u8fd9\u662f\u4e00\u4e2a\u7c7b\u539f\u578b\uff0c\u5b83\u544a\u8bc9\u7f16\u8bd1\u5668\u6211\u4eec\u5c06\u5728\u672a\u6765\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a Humidity \u7684\u7c7b\u3002\u5982\u679c\u6ca1\u6709\u8fd9\u4e00\u884c\uff0c\u7f16\u8bd1\u5668\u5728\u89e3\u6790 Temperature \u7c7b\u4e2d printWeather() \u7684\u539f\u578b\u65f6\u5c31\u4e0d\u77e5\u9053 Humidity \u662f\u4ec0\u4e48\u3002\u7c7b\u539f\u578b\u7684\u4f5c\u7528\u4e0e\u51fd\u6570\u539f\u578b\u76f8\u540c\u2014\u2014\u5b83\u4eec\u544a\u8bc9\u7f16\u8bd1\u5668\u67d0\u4e2a\u4e1c\u897f\u662f\u4ec0\u4e48\u6837\u5b50\u7684\uff0c\u4ee5\u4fbf\u73b0\u5728\u53ef\u4ee5\u4f7f\u7528\u5b83\uff0c\u4ee5\u540e\u53ef\u4ee5\u5b9a\u4e49\u5b83\u3002\u7136\u800c\uff0c\u4e0e\u51fd\u6570\u4e0d\u540c\u7684\u662f\uff0c\u7c7b\u6ca1\u6709\u8fd4\u56de\u7c7b\u578b\u6216\u53c2\u6570\uff0c\u56e0\u6b64\u7c7b\u539f\u578b\u603b\u662f\u7b80\u5355\u7684 class ClassName \uff0c\u5176\u4e2d ClassName \u662f\u7c7b\u7684\u540d\u79f0\u3002","title":"\u591a\u4e2a\u53cb\u5143"},{"location":"13-15-friend-functions-and-classes/#_3","text":"\u4e5f\u53ef\u4ee5\u8ba9\u6574\u4e2a\u7c7b\u6210\u4e3a\u53e6\u4e00\u4e2a\u7c7b\u7684\u53cb\u5143\u3002\u8fd9\u4f7f\u53cb\u5143\u7c7b\u7684\u6240\u6709\u6210\u5458\u90fd\u53ef\u4ee5\u8bbf\u95ee\u53e6\u4e00\u4e2a\u7c7b\u7684\u79c1\u6709\u6210\u5458\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include class Storage { private : int m_nValue {}; double m_dValue {}; public : Storage ( int nValue , double dValue ) : m_nValue { nValue }, m_dValue { dValue } { } // \u4f7f Display \u7c7b\u79f0\u4e3a Storage \u7684\u53cb\u5143 friend class Display ; }; class Display { private : bool m_displayIntFirst ; public : Display ( bool displayIntFirst ) : m_displayIntFirst { displayIntFirst } { } void displayItem ( const Storage & storage ) { if ( m_displayIntFirst ) std :: cout << storage . m_nValue << ' ' << storage . m_dValue << '\\n' ; else // display double first std :: cout << storage . m_dValue << ' ' << storage . m_nValue << '\\n' ; } }; int main () { Storage storage { 5 , 6.7 }; Display display { false }; display . displayItem ( storage ); return 0 ; } \u56e0\u4e3a Display \u7c7b\u662f Storage \u7684\u53cb\u5143\uff0c\u6240\u4ee5 Display \u7684\u4efb\u4f55\u6210\u5458\u90fd\u53ef\u4ee5 Storage \u7684\u79c1\u6709\u6210\u5458\u3002\u4e0a\u8ff0\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 6.7 5 \u6709\u5173\u53cb\u5143\u7c7b\uff0c\u8fd8\u6709\u4e00\u4e9b\u4e8b\u60c5\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0c\u5373\u4fbf Display \u662f Storage \u7684\u53cb\u5143\uff0c\u4f46\u662f Display \u5e76\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee Storage \u5bf9\u8c61\u7684 *this \u6307\u9488\u3002\u5176\u6b21\uff0c\u56e0\u4e3a Display \u662f Storage \u7684\u53cb\u5143\u7c7b\uff0c\u4f46\u662f\u8fd9\u4e0d\u4ee3\u8868 Storage \u4e5f\u662f Display \u7684\u53cb\u5143\u3002\u5982\u679c\u4f60\u5e0c\u671b\u4e24\u4e2a\u7c7b\u4e92\u4e3a\u53cb\u5143\uff0c\u963f\u4e48\u5fc5\u987b\u5206\u522b\u5728\u7c7b\u4e2d\u5c06\u5bf9\u65b9\u58f0\u660e\u4e3a\u53cb\u5143\u3002\u6700\u540e\uff0c\u5982\u679cA\u662fB\u7684\u53cb\u5143\uff0c\u800cB\u662fC\u7684\u53cb\u5143\uff0c\u4f46\u4e0d\u4ee3\u8868A\u662fC\u7684\u53cb\u5143\u3002 \u5728\u4f7f\u7528\u53cb\u51fd\u6570\u548c\u7c7b\u65f6\u8981\u5c0f\u5fc3\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u53cb\u51fd\u6570\u6216\u7c7b\u6253\u7834\u5c01\u88c5\u3002\u5982\u679c\u7c7b\u6539\u53d8\u4e86\uff0c\u53cb\u5143\u7684\u4e5f\u9700\u8981\u88ab\u8feb\u6539\u53d8\u3002\u56e0\u6b64\uff0c\u5c3d\u91cf\u51cf\u5c11\u5bf9\u53cb\u51fd\u6570\u548c\u53cb\u5143\u7c7b\u7684\u4f7f\u7528\u3002","title":"\u53cb\u5143\u7c7b"},{"location":"13-15-friend-functions-and-classes/#_4","text":"\u53ef\u4ee5\u5c06\u5355\u4e2a\u6210\u5458\u51fd\u6570\u8bbe\u4e3a\u53cb\u5143\uff0c\u800c\u4e0d\u662f\u5c06\u6574\u4e2a\u7c7b\u8bbe\u4e3a\u53cb\u5143\u3002\u8fd9\u4e0e\u5c06\u666e\u901a\u51fd\u6570\u8bbe\u4e3a\u53cb\u5143\u7c7b\u4f3c\uff0c\u53ea\u662f\u5728\u4f7f\u7528\u6210\u5458\u51fd\u6570\u7684\u540d\u79f0\u65f6\u5305\u542b\u4e86 className:: \u524d\u7f00\uff08\u4f8b\u5982 Display::displayItem )\u3002 \u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u6bd4\u9884\u671f\u7684\u8981\u590d\u6742\u4e00\u4e9b\u3002\u8ba9\u6211\u4eec\u6539\u5199\u4e00\u4e0b\u524d\u9762\u7684\u793a\u4f8b\uff0c\u4f7f Display::displayItem \u6210\u4e3a\u53cb\u5143\u6210\u5458\u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include class Display ; // Display \u7684\u524d\u5411\u58f0\u660e class Storage { private : int m_nValue {}; double m_dValue {}; public : Storage ( int nValue , double dValue ) : m_nValue { nValue }, m_dValue { dValue } { } // \u4f7f Display::displayItem \u6210\u5458\u51fd\u6570\u6210\u4e3a Storage \u7684\u53cb\u5143 friend void Display :: displayItem ( const Storage & storage ); // \u9519\u8bef: Storage \u6b64\u65f6\u5e76\u4e0d\u77e5\u9053 Display \u7c7b\u7684\u5b8c\u6574\u5b9a\u4e49\u3002 }; class Display { private : bool m_displayIntFirst {}; public : Display ( bool displayIntFirst ) : m_displayIntFirst { displayIntFirst } { } void displayItem ( const Storage & storage ) { if ( m_displayIntFirst ) std :: cout << storage . m_nValue << ' ' << storage . m_dValue << '\\n' ; else // display double first std :: cout << storage . m_dValue << ' ' << storage . m_nValue << '\\n' ; } }; \u5f88\u9057\u61be\uff0c\u8fd9\u4e48\u505a\u5e76\u4e0d\u80fd\u6b63\u786e\u5de5\u4f5c\u3002\u4e3a\u4e86\u8ba9\u4e00\u4e2a\u6210\u5458\u51fd\u6570\u79f0\u4e3a\u53cb\u5143\uff0c\u7f16\u8bd1\u5668\u5fc5\u987b\u770b\u5230\u8be5\u6210\u5458\u51fd\u6570\u7684\u5b8c\u6574\u5b9a\u4e49\u800c\u4e0d\u4ec5\u4ec5\u662f \u524d\u5411\u58f0\u660e \u3002\u56e0\u4e3a Storage \u8fd8\u6ca1\u6709\u770b\u5230 Display \u7684\u5b8c\u6574\u5b9a\u4e49\uff0c\u6240\u4ee5\u5f53\u6211\u4eec\u5728\u6b64\u5904\u58f0\u660e\u53cb\u5143\u51fd\u6570\u65f6\u4f1a\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ea\u8981\u5c06 Display \u7684\u5b9a\u4e49\u79fb\u52a8\u5230 Storage \u524d\u9762\u5373\u53ef\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include class Display { private : bool m_displayIntFirst {}; public : Display ( bool displayIntFirst ) : m_displayIntFirst { displayIntFirst } { } void displayItem ( const Storage & storage ) // error: compiler doesn't know what a Storage is { if ( m_displayIntFirst ) std :: cout << storage . m_nValue << ' ' << storage . m_dValue << '\\n' ; else // display double first std :: cout << storage . m_dValue << ' ' << storage . m_nValue << '\\n' ; } }; class Storage { private : int m_nValue {}; double m_dValue {}; public : Storage ( int nValue , double dValue ) : m_nValue { nValue }, m_dValue { dValue } { } // Make the Display::displayItem member function a friend of the Storage class friend void Display :: displayItem ( const Storage & storage ); // okay now }; \u4e0d\u8fc7\uff0c \u8fd9\u6837\u4e00\u6765\u4f1a\u5e26\u6765\u53e6\u5916\u7684\u95ee\u9898\uff0c\u56e0\u4e3a Display::displayItem() \u4f7f\u7528 Storage \u4f5c\u4e3a\u5f15\u7528\u53c2\u6570\u3002\u800c\u5f53\u6211\u4eec\u5c06 Storage \u7684\u5b9a\u4e49\u79fb\u52a8\u5230 Display \u540e\u9762\u65f6\uff0c \u7f16\u8bd1\u5668\u5c31\u4f1a\u62b1\u6028\u5b83\u4e0d\u77e5\u9053Storage \u662f\u4ec0\u4e48\uff0c\u6211\u4eec\u4e0d\u80fd\u518d\u8c03\u6574\u987a\u5e8f\u4e86\uff0c\u4e0d\u7136\u53c8\u4f1a\u51fa\u73b0\u4e4b\u524d\u7684\u95ee\u9898\u3002 \u5e78\u8fd0\u5730\u662f\uff0c\u53ea\u9700\u8981\u51e0\u6b65\u5c31\u53ef\u4ee5\u89e3\u51b3\u8be5\u95ee\u9898\u3002\u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u5bf9\u4e8e Storage \u7684\u524d\u5411\u58f0\u660e\u3002\u7b2c\u4e8c\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06 Display::displayItem() \u79fb\u52a8\u5230\u7c7b\u5916\u90e8\uff0c\u653e\u5728 Storage \u7c7b\u5b8c\u6574\u5b9a\u4e49\u7684\u540e\u3002 \u770b\u4e0a\u53bb\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 #include class Storage ; // Storage \u7684\u524d\u5411\u58f0\u660e class Display { private : bool m_displayIntFirst {}; public : Display ( bool displayIntFirst ) : m_displayIntFirst { displayIntFirst } { } void displayItem ( const Storage & storage ); // \u4e0a\u9762\u7684\u524d\u5411\u58f0\u660e\u5c31\u662f\u4e3a\u4e86\u8fd9\u4e00\u53e5\u58f0\u660e }; class Storage // Storage \u7c7b\u7684\u5b8c\u6574\u5b9a\u4e49 { private : int m_nValue {}; double m_dValue {}; public : Storage ( int nValue , double dValue ) : m_nValue { nValue }, m_dValue { dValue } { } // \u5c06 Display::displayItem \u58f0\u660e\u4e3a Storage \u7c7b\u7684\u53cb\u5143 (\u5fc5\u987b\u770b\u5230 Display \u7684\u5b8c\u6574\u5b9a\u4e49) friend void Display :: displayItem ( const Storage & storage ); }; // \u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49 Display::displayItem \u4e86\uff0c\u5b83\u5fc5\u987b\u770b\u5230 Storage \u7684\u5b8c\u6574\u5b9a\u4e49 void Display::displayItem ( const Storage & storage ) { if ( m_displayIntFirst ) std :: cout << storage . m_nValue << ' ' << storage . m_dValue << '\\n' ; else // display double first std :: cout << storage . m_dValue << ' ' << storage . m_nValue << '\\n' ; } int main () { Storage storage ( 5 , 6.7 ); Display display ( false ); display . displayItem ( storage ); return 0 ; } \u73b0\u5728\uff0c\u4ee3\u7801\u53ef\u4ee5\u6b63\u5e38\u7f16\u8bd1\u4e86\uff1a Storage \u7684\u524d\u5411\u58f0\u660e\u5bf9\u4e8e Display::displayItem() \u7684\u58f0\u660e\u6765\u8bf4\u8db3\u591f\u4e86\u3002 Display \u7684\u5b8c\u6574\u5b9a\u4e49\u53ef\u4ee5\u786e\u4fdd Display::displayItem() \u88ab\u5b9a\u4e49\u4e3a Storage \u7684\u53cb\u5143\u51fd\u6570\uff0c\u5982\u679c\u4f60\u8fd8\u662f\u611f\u5230\u56f0\u60d1\uff0c\u8bf7\u4ed4\u7ec6\u770b\u770b\u4e0a\u9762\u4ee3\u7801\u4e2d\u7684\u6ce8\u91ca\u3002 \u5982\u679c\u8fd9\u4ee4\u4f60\u611f\u5230\u56f0\u96be\u2014\u2014\u7684\u786e\u662f\u7684\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u8fd9\u4e48\u505a\u7684\u539f\u56e0\u53ea\u662f\u56e0\u4e3a\u6211\u4eec\u8bd5\u56fe\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u5b8c\u6210\u6240\u6709\u5de5\u4f5c\u3002\u66f4\u597d\u7684\u89e3\u51b3\u65b9\u6848\u662f\u5c06\u6bcf\u4e2a\u7c7b\u5b9a\u4e49\u653e\u5728\u5355\u72ec\u7684\u5934\u6587\u4ef6\u4e2d\uff0c\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u653e\u5728\u76f8\u5e94\u7684.cpp\u6587\u4ef6\u4e2d\u3002\u8fd9\u6837\uff0c\u6240\u6709\u7684\u7c7b\u5b9a\u4e49\u90fd\u5c06\u7acb\u5373\u5728.cpp\u6587\u4ef6\u4e2d\u53ef\u89c1\uff0c\u5e76\u4e14\u4e0d\u9700\u8981\u91cd\u65b0\u6392\u5217\u7c7b\u6216\u51fd\u6570!","title":"\u53cb\u5143\u6210\u5458\u51fd\u6570"},{"location":"13-15-friend-functions-and-classes/#_5","text":"\u53cb\u5143\u51fd\u6570\u6216\u53cb\u5143\u7c7b\u662f\u53ef\u4ee5\u8bbf\u95ee\u53e6\u4e00\u4e2a\u7c7b\u7684\u79c1\u6709\u6210\u5458\u7684\u51fd\u6570\u6216\u7c7b\uff0c\u5c31\u597d\u50cf\u5b83\u662f\u8be5\u7c7b\u7684\u6210\u5458\u4e00\u6837\u3002\u8fd9\u5141\u8bb8\u53cb\u5143\u51fd\u6570\u6216\u53cb\u5143\u7c7b\u4e0e\u53e6\u4e00\u4e2a\u7c7b\u7d27\u5bc6\u5730\u4e00\u8d77\u5de5\u4f5c\uff0c\u800c\u4e0d\u5fc5\u66b4\u9732\u4e00\u4e2a\u79c1\u6709\u6210\u5458(\u4f8b\u5982\u901a\u8fc7\u8bbf\u95ee\u51fd\u6570)\u3002 \u53cb\u5143\u901a\u5e38\u5728\u5b9a\u4e49\u91cd\u8f7d\u64cd\u4f5c\u7b26(\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u7ae0\u8ba8\u8bba)\u65f6\u4f7f\u7528\uff0c\u6216\u8005\uff08\u5c11\u6570\u65f6\u5019\uff09\u5728\u4e24\u4e2a\u6216\u591a\u4e2a\u7c7b\u9700\u8981\u4ee5\u4eb2\u5bc6\u7684\u65b9\u5f0f\u4e00\u8d77\u5de5\u4f5c\u65f6\u3002 \u6ce8\u610f\uff0c\u8981\u4f7f\u7279\u5b9a\u7684\u6210\u5458\u51fd\u6570\u6210\u4e3a\u53cb\u5143\uff0c\u9996\u5148\u9700\u8981\u770b\u5230\u6210\u5458\u51fd\u6570\u7c7b\u7684\u5b8c\u6574\u5b9a\u4e49\u3002","title":"\u5c0f\u7ed3"},{"location":"13-16-anonymous-objects/","tags":["class","friend"],"text":"\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ea\u9700\u8981\u4e00\u4e2a\u4e34\u65f6\u7684\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u8003\u8651\u4ee5\u4e0b\u60c5\u51b5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int add ( int x , int y ) { int sum { x + y }; return sum ; } int main () { std :: cout << add ( 5 , 3 ) << '\\n' ; return 0 ; } \u5728 add() \u51fd\u6570\u4e2d\uff0c\u53d8\u91cf sum \u53ea\u88ab\u5f53\u505a\u4e00\u4e2a\u4e34\u65f6\u7684\u5360\u4f4d\u7b26\u53d8\u91cf\u6765\u4f7f\u7528\u3002\u5b83\u5e76\u6ca1\u6709\u5176\u4ed6\u7279\u522b\u7684\u529f\u80fd\u2014\u2014\u5b83\u552f\u4e00\u7684\u529f\u80fd\u5c31\u662f\u4fdd\u5b58\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u5e76\u4f5c\u4e3a\u8fd4\u56de\u503c\u8fd4\u56de\u3002 \u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u533f\u540d\u5bf9\u8c61\u7f16\u5199\u4e00\u4e2a\u66f4\u52a0\u7b80\u5355\u7684 add() \u51fd\u6570\u3002 \u533f\u540d\u5bf9\u8c61 \u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u6ca1\u6709\u540d\u79f0\u7684\u503c\u3002\u56e0\u4e3a\u5b83\u4eec\u6ca1\u6709\u540d\u5b57\uff0c\u6240\u4ee5\u5728\u5b83\u4eec\u88ab\u521b\u5efa\u540e\uff0c\u6211\u4eec\u6ca1\u6709\u529e\u6cd5\u53bb\u5f15\u7528\u5b83\u4eec\u3002\u56e0\u6b64\uff0c\u533f\u540d\u5bf9\u8c61\u53ea\u80fd\u5177\u6709\u201c\u8868\u8fbe\u5f0f\u4f5c\u7528\u57df\u201d\uff0c\u5373\u5b83\u4eec\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u88ab\u521b\u5efa\u3001\u6c42\u503c\u548c\u9500\u6bc1\u3002 \u4e0b\u9762\u4f8b\u5b50\u4e2d\u7684 add() \u51fd\u6570\u4f7f\u7528\u4e86\u533f\u540d\u5bf9\u8c61\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int add ( int x , int y ) { return x + y ; // an anonymous object is created to hold and return the result of x + y } int main () { std :: cout << add ( 5 , 3 ) << '\\n' ; return 0 ; } \u5f53\u8868\u8fbe\u5f0f x + y \u6c42\u503c\u65f6\uff0c\u5b83\u7684\u7ed3\u679c\u4f1a\u88ab\u5b58\u653e\u5230\u4e00\u4e2a\u533f\u540d\u5bf9\u8c61\u4e2d\u3002\u7136\u540e\uff0c\u8be5\u5bf9\u8c61\u7684\u4e00\u4e2a\u62f7\u8d1d\u4f1a\u88ab\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\uff0c\u7136\u540e\u8fd9\u4e2a\u533f\u540d\u5bf9\u8c61\u5c31\u4f1a\u88ab\u9500\u6bc1\u3002 \u8fd9\u4e0d\u4ec5\u9002\u7528\u4e8e\u8fd4\u56de\u503c\uff0c\u4e5f\u9002\u7528\u4e8e\u51fd\u6570\u5f62\u53c2\u3002\u4f8b\u5982\uff0c\u4e0e\u5176\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include void printValue ( int value ) { std :: cout << value ; } int main () { int sum { 5 + 3 }; printValue ( sum ); return 0 ; } \u4e0d\u5982\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void printValue ( int value ) { std :: cout << value ; } int main () { printValue ( 5 + 3 ); return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8868\u8fbe\u5f0f 5+3 \u88ab\u6c42\u503c\u4e3a 8 \uff0c\u7136\u540e\u7ed3\u679c\u88ab\u5b58\u653e\u5728\u533f\u540d\u5bf9\u8c61\u65f6\u3002\u8fd9\u4e2a\u533f\u540d\u5bf9\u8c61\u7684\u62f7\u8d1d\u968f\u540e\u88ab\u4f20\u9012\u7ed9 printValue() \u51fd\u6570\uff0c\u7136\u540e\u88ab\u9500\u6bc1\u3002 \u6ce8\u610f\uff0c\u8fd9\u4f7f\u6211\u4eec\u7684\u4ee3\u7801\u53d8\u5f97\u975e\u5e38\u7b80\u6d01\u2014\u2014\u6211\u4eec\u4e0d\u5fc5\u7528\u53ea\u4f7f\u7528\u4e00\u6b21\u7684\u4e34\u65f6\u53d8\u91cf\u6765\u5f04\u4e71\u4ee3\u7801\u3002 \u533f\u540d\u7c7b\u5bf9\u8c61 \u5c3d\u7ba1\u6211\u4eec\u524d\u9762\u7684\u4f8b\u5b50\u90fd\u662f\u5185\u7f6e\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u662f\u6211\u4eec\u4e5f\u53ef\u4ee5\u6784\u9020\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u533f\u540d\u5bf9\u8c61\u3002\u65b9\u6cd5\u548c\u521b\u5efa\u666e\u901a\u5bf9\u8c61\u7c7b\u4f3c\uff0c\u53ea\u9700\u8981\u7701\u7565\u53d8\u91cf\u540d\u5373\u53ef\u3002 1 2 Cents cents { 5 }; // \u666e\u901a\u53d8\u91cf Cents { 7 }; // \u533f\u540d\u5bf9\u8c61 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c Cents{ 7 } \u4f1a\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u5bf9\u8c61\u5e76\u4f7f\u75287\u4f5c\u4e3a\u5176\u521d\u59cb\u503c\uff0c\u7136\u540e\u8be5\u5bf9\u8c61\u5c31\u88ab\u9500\u6bc1\u4e86\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u533f\u540d\u5bf9\u8c61\u770b\u4e0a\u53bb\u6ca1\u4ec0\u4e48\u7528\u3002\u8ba9\u6211\u4eec\u770b\u53e6\u4e00\u4e2a\u4f8b\u5b50\u5427\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; void print ( const Cents & cents ) { std :: cout << cents . getCents () << \" cents \\n \" ; } int main () { Cents cents { 6 }; print ( cents ); return 0 ; } \u6ce8\u610f\uff0c\u6b64\u793a\u4f8b\u4e0e\u524d\u9762\u4f7f\u7528\u6574\u6570\u7684\u793a\u4f8b\u975e\u5e38\u76f8\u4f3c\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u7684 main() \u51fd\u6570\u5411 print() \u51fd\u6570\u4f20\u9012\u4e86\u4e00\u4e2a Cents \u7c7b\u578b\u7684\u5bf9\u8c61(\u540d\u4e3acents)\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u533f\u540d\u5bf9\u8c61\u6765\u7b80\u5316\u8fd9\u4e2a\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; void print ( const Cents & cents ) { std :: cout << cents . getCents () << \" cents \\n \" ; } int main () { print ( Cents { 6 }); // Note: Now we're passing an anonymous Cents value return 0 ; } \u4f60\u53ef\u80fd\u5e0c\u671b\u7a0b\u5e8f\u6253\u5370\u5982\u4e0b\u5185\u5bb9\uff1a 1 6 cents \u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u518d\u770b\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; Cents add ( const Cents & c1 , const Cents & c2 ) { Cents sum { c1 . getCents () + c2 . getCents () }; return sum ; } int main () { Cents cents1 { 6 }; Cents cents2 { 8 }; Cents sum { add ( cents1 , cents2 ) }; std :: cout << \"I have \" << sum . getCents () << \" cents. \\n \" ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u5f88\u591a\u547d\u540d\u4e3aCents\u7684\u503c\u3002\u5728 add() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a sum \u7684 Cents \u5bf9\u8c61\uff0c\u6211\u4eec\u4f7f\u7528\u5b83\u4f5c\u4e3a\u4e2d\u95f4\u503c\uff0c\u5728\u8fd4\u56de\u00b7\u4e4b\u524d\u4fdd\u5b58\u5b83\u3002\u5728\u51fd\u6570 main() \u4e2d\uff0c\u6211\u4eec\u6709\u53e6\u4e00\u4e2a\u540d\u4e3a sum \u7684 Cents \u5bf9\u8c61\uff0c\u4e5f\u7528\u4f5c\u4e2d\u95f4\u503c\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u533f\u540d\u503c\u6765\u7b80\u5316\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; Cents add ( const Cents & c1 , const Cents & c2 ) { // \u5217\u8868\u521d\u59cb\u5316\u4f1a\u53c2\u8003\u51fd\u6570\u8fd4\u56de\u503c\u7684\u7c7b\u578b // \u7136\u540e\u521b\u5efa\u6240\u9700\u7684\u5bf9\u8c61 return { c1 . getCents () + c2 . getCents () }; // \u8fd4\u56de\u533f\u540d\u7684 Cents value } int main () { Cents cents1 { 6 }; Cents cents2 { 8 }; std :: cout << \"I have \" << add ( cents1 , cents2 ). getCents () << \" cents. \\n \" ; // print anonymous Cents value return 0 ; } \u8fd9\u4e2a\u7248\u672c\u7684 add() \u51fd\u6570\u4e0e\u4e0a\u9762\u7684\u51fd\u6570\u76f8\u540c\uff0c\u53ea\u662f\u5b83\u4f7f\u7528\u4e86\u4e00\u4e2a\u533f\u540d\u7684Cents\u503c\u800c\u4e0d\u662f\u4e00\u4e2a\u547d\u540d\u53d8\u91cf\u3002\u8fd8\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728 main() \u4e2d\uff0c\u6211\u4eec\u4e0d\u518d\u4f7f\u7528\u547d\u540d\u7684 sum \u53d8\u91cf\u4f5c\u4e3a\u4e34\u65f6\u5b58\u50a8\u3002\u76f8\u53cd\uff0c\u6211\u4eec\u4f1a\u533f\u540d\u4f7f\u7528 add() \u7684\u8fd4\u56de\u503c\uff01 \u56e0\u6b64\uff0c\u6211\u4eec\u7684\u7a0b\u5e8f\u66f4\u77ed\u3001\u66f4\u6e05\u6670\uff0c\u800c\u4e14\u901a\u5e38\u66f4\u5bb9\u6613\u7406\u89e3(\u4e00\u65e6\u4f60\u7406\u89e3\u4e86\u8fd9\u4e2a\u6982\u5ff5)\u3002 \u4e8b\u5b9e\u4e0a\uff0c\u56e0\u4e3acent1\u548ccent2\u53ea\u5728\u4e00\u4e2a\u5730\u65b9\u4f7f\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u8fdb\u4e00\u6b65\u533f\u540d\u5316: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; Cents add ( const Cents & c1 , const Cents & c2 ) { return { c1 . getCents () + c2 . getCents () }; // return anonymous Cents value } int main () { std :: cout << \"I have \" << add ( Cents { 6 }, Cents { 8 }). getCents () << \" cents. \\n \" ; // print anonymous Cents value return 0 ; } \u5c0f\u7ed3 \u5728C++\u4e2d\uff0c\u533f\u540d\u5bf9\u8c61\u4e3b\u8981\u7528\u4e8e\u4f20\u9012\u53c2\u6570\u6216\u8fd4\u56de\u503c\u800c\u4e0d\u5fc5\u4e3a\u6b64\u521b\u5efa\u5927\u91cf\u4e34\u65f6\u53d8\u91cf\u3002\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u4e5f\u662f\u533f\u540d\u5b8c\u6210\u7684(\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u5b83\u7684\u5730\u5740\u5fc5\u987b\u5206\u914d\u7ed9\u6307\u9488\uff0c\u5426\u5219\u6211\u4eec\u5c31\u6ca1\u6709\u529e\u6cd5\u5f15\u7528\u5b83)\u3002 \u53e6\u5916\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u56e0\u4e3a\u533f\u540d\u5bf9\u8c61\u5177\u6709\u8868\u8fbe\u5f0f\u4f5c\u7528\u57df\uff0c\u6240\u4ee5\u5b83\u4eec\u53ea\u80fd\u4f7f\u7528\u4e00\u6b21(\u9664\u975e\u7ed1\u5b9a\u5230\u4e00\u4e2a\u56fa\u5b9a\u7684 \u5de6\u503c\u5f15\u7528 \uff0c\u8fd9\u5c06\u5ef6\u957f\u4e34\u65f6\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\uff0c\u4ee5\u5339\u914d\u5f15\u7528\u7684\u751f\u547d\u5468\u671f)\u3002\u5982\u679c\u9700\u8981\u5728\u591a\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u5f15\u7528\u4e00\u4e2a\u503c\uff0c\u5219\u5e94\u8be5\u4f7f\u7528\u547d\u540d\u53d8\u91cf\u3002","title":"13.16 - \u533f\u540d\u5bf9\u8c61"},{"location":"13-16-anonymous-objects/#_1","text":"\u5c3d\u7ba1\u6211\u4eec\u524d\u9762\u7684\u4f8b\u5b50\u90fd\u662f\u5185\u7f6e\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u662f\u6211\u4eec\u4e5f\u53ef\u4ee5\u6784\u9020\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u533f\u540d\u5bf9\u8c61\u3002\u65b9\u6cd5\u548c\u521b\u5efa\u666e\u901a\u5bf9\u8c61\u7c7b\u4f3c\uff0c\u53ea\u9700\u8981\u7701\u7565\u53d8\u91cf\u540d\u5373\u53ef\u3002 1 2 Cents cents { 5 }; // \u666e\u901a\u53d8\u91cf Cents { 7 }; // \u533f\u540d\u5bf9\u8c61 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c Cents{ 7 } \u4f1a\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u5bf9\u8c61\u5e76\u4f7f\u75287\u4f5c\u4e3a\u5176\u521d\u59cb\u503c\uff0c\u7136\u540e\u8be5\u5bf9\u8c61\u5c31\u88ab\u9500\u6bc1\u4e86\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u533f\u540d\u5bf9\u8c61\u770b\u4e0a\u53bb\u6ca1\u4ec0\u4e48\u7528\u3002\u8ba9\u6211\u4eec\u770b\u53e6\u4e00\u4e2a\u4f8b\u5b50\u5427\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; void print ( const Cents & cents ) { std :: cout << cents . getCents () << \" cents \\n \" ; } int main () { Cents cents { 6 }; print ( cents ); return 0 ; } \u6ce8\u610f\uff0c\u6b64\u793a\u4f8b\u4e0e\u524d\u9762\u4f7f\u7528\u6574\u6570\u7684\u793a\u4f8b\u975e\u5e38\u76f8\u4f3c\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u7684 main() \u51fd\u6570\u5411 print() \u51fd\u6570\u4f20\u9012\u4e86\u4e00\u4e2a Cents \u7c7b\u578b\u7684\u5bf9\u8c61(\u540d\u4e3acents)\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u533f\u540d\u5bf9\u8c61\u6765\u7b80\u5316\u8fd9\u4e2a\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; void print ( const Cents & cents ) { std :: cout << cents . getCents () << \" cents \\n \" ; } int main () { print ( Cents { 6 }); // Note: Now we're passing an anonymous Cents value return 0 ; } \u4f60\u53ef\u80fd\u5e0c\u671b\u7a0b\u5e8f\u6253\u5370\u5982\u4e0b\u5185\u5bb9\uff1a 1 6 cents \u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u518d\u770b\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; Cents add ( const Cents & c1 , const Cents & c2 ) { Cents sum { c1 . getCents () + c2 . getCents () }; return sum ; } int main () { Cents cents1 { 6 }; Cents cents2 { 8 }; Cents sum { add ( cents1 , cents2 ) }; std :: cout << \"I have \" << sum . getCents () << \" cents. \\n \" ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u5f88\u591a\u547d\u540d\u4e3aCents\u7684\u503c\u3002\u5728 add() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a sum \u7684 Cents \u5bf9\u8c61\uff0c\u6211\u4eec\u4f7f\u7528\u5b83\u4f5c\u4e3a\u4e2d\u95f4\u503c\uff0c\u5728\u8fd4\u56de\u00b7\u4e4b\u524d\u4fdd\u5b58\u5b83\u3002\u5728\u51fd\u6570 main() \u4e2d\uff0c\u6211\u4eec\u6709\u53e6\u4e00\u4e2a\u540d\u4e3a sum \u7684 Cents \u5bf9\u8c61\uff0c\u4e5f\u7528\u4f5c\u4e2d\u95f4\u503c\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u533f\u540d\u503c\u6765\u7b80\u5316\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; Cents add ( const Cents & c1 , const Cents & c2 ) { // \u5217\u8868\u521d\u59cb\u5316\u4f1a\u53c2\u8003\u51fd\u6570\u8fd4\u56de\u503c\u7684\u7c7b\u578b // \u7136\u540e\u521b\u5efa\u6240\u9700\u7684\u5bf9\u8c61 return { c1 . getCents () + c2 . getCents () }; // \u8fd4\u56de\u533f\u540d\u7684 Cents value } int main () { Cents cents1 { 6 }; Cents cents2 { 8 }; std :: cout << \"I have \" << add ( cents1 , cents2 ). getCents () << \" cents. \\n \" ; // print anonymous Cents value return 0 ; } \u8fd9\u4e2a\u7248\u672c\u7684 add() \u51fd\u6570\u4e0e\u4e0a\u9762\u7684\u51fd\u6570\u76f8\u540c\uff0c\u53ea\u662f\u5b83\u4f7f\u7528\u4e86\u4e00\u4e2a\u533f\u540d\u7684Cents\u503c\u800c\u4e0d\u662f\u4e00\u4e2a\u547d\u540d\u53d8\u91cf\u3002\u8fd8\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728 main() \u4e2d\uff0c\u6211\u4eec\u4e0d\u518d\u4f7f\u7528\u547d\u540d\u7684 sum \u53d8\u91cf\u4f5c\u4e3a\u4e34\u65f6\u5b58\u50a8\u3002\u76f8\u53cd\uff0c\u6211\u4eec\u4f1a\u533f\u540d\u4f7f\u7528 add() \u7684\u8fd4\u56de\u503c\uff01 \u56e0\u6b64\uff0c\u6211\u4eec\u7684\u7a0b\u5e8f\u66f4\u77ed\u3001\u66f4\u6e05\u6670\uff0c\u800c\u4e14\u901a\u5e38\u66f4\u5bb9\u6613\u7406\u89e3(\u4e00\u65e6\u4f60\u7406\u89e3\u4e86\u8fd9\u4e2a\u6982\u5ff5)\u3002 \u4e8b\u5b9e\u4e0a\uff0c\u56e0\u4e3acent1\u548ccent2\u53ea\u5728\u4e00\u4e2a\u5730\u65b9\u4f7f\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u8fdb\u4e00\u6b65\u533f\u540d\u5316: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; Cents add ( const Cents & c1 , const Cents & c2 ) { return { c1 . getCents () + c2 . getCents () }; // return anonymous Cents value } int main () { std :: cout << \"I have \" << add ( Cents { 6 }, Cents { 8 }). getCents () << \" cents. \\n \" ; // print anonymous Cents value return 0 ; }","title":"\u533f\u540d\u7c7b\u5bf9\u8c61"},{"location":"13-16-anonymous-objects/#_2","text":"\u5728C++\u4e2d\uff0c\u533f\u540d\u5bf9\u8c61\u4e3b\u8981\u7528\u4e8e\u4f20\u9012\u53c2\u6570\u6216\u8fd4\u56de\u503c\u800c\u4e0d\u5fc5\u4e3a\u6b64\u521b\u5efa\u5927\u91cf\u4e34\u65f6\u53d8\u91cf\u3002\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u4e5f\u662f\u533f\u540d\u5b8c\u6210\u7684(\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u5b83\u7684\u5730\u5740\u5fc5\u987b\u5206\u914d\u7ed9\u6307\u9488\uff0c\u5426\u5219\u6211\u4eec\u5c31\u6ca1\u6709\u529e\u6cd5\u5f15\u7528\u5b83)\u3002 \u53e6\u5916\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u56e0\u4e3a\u533f\u540d\u5bf9\u8c61\u5177\u6709\u8868\u8fbe\u5f0f\u4f5c\u7528\u57df\uff0c\u6240\u4ee5\u5b83\u4eec\u53ea\u80fd\u4f7f\u7528\u4e00\u6b21(\u9664\u975e\u7ed1\u5b9a\u5230\u4e00\u4e2a\u56fa\u5b9a\u7684 \u5de6\u503c\u5f15\u7528 \uff0c\u8fd9\u5c06\u5ef6\u957f\u4e34\u65f6\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\uff0c\u4ee5\u5339\u914d\u5f15\u7528\u7684\u751f\u547d\u5468\u671f)\u3002\u5982\u679c\u9700\u8981\u5728\u591a\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u5f15\u7528\u4e00\u4e2a\u503c\uff0c\u5219\u5e94\u8be5\u4f7f\u7528\u547d\u540d\u53d8\u91cf\u3002","title":"\u5c0f\u7ed3"},{"location":"13-17-nested-types-in-classes/","tags":["class","friend"],"text":"\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7b80\u77ed\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include enum class FruitType { apple , banana , cherry }; class Fruit { private : FruitType m_type {}; int m_percentageEaten { 0 }; public : Fruit ( FruitType type ) : m_type { type } { } FruitType getType () const { return m_type ; } int getPercentageEaten () const { return m_percentageEaten ; } }; int main () { Fruit apple { FruitType :: apple }; if ( apple . getType () == FruitType :: apple ) std :: cout << \"I am an apple\" ; else std :: cout << \"I am not an apple\" ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u5e76\u6ca1\u6709\u4efb\u4f55\u95ee\u9898\u3002\u4f46\u662f\u56e0\u4e3a \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e \uff08\u679a\u4e3e\u7c7b\uff09 FruitType \u53ea\u4f1a\u5728 Fruit \u7c7b\u4e2d\u4f7f\u7528\uff0c\u6240\u4ee5\u5c06\u5b83\u5b9a\u4e49\u5728\u7c7b\u5916\u4f5c\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684\u679a\u4e3e\u7c7b\u591a\u5c11\u6709\u4e9b\u4e0d\u59a5\u3002 \u5d4c\u5957\u7c7b\u578b \u6570\u636e\u548c\u51fd\u6570\u53ef\u4ee5\u79f0\u4e3a\u7c7b\u7684\u6210\u5458\uff0c\u5728C++\u4e2d\uff0c\u7c7b\u578b\u4e5f\u53ef\u4ee5\u88ab\u5d4c\u5957\u5730\u5b9a\u4e49\u5728\u7c7b\u5185\u3002\u4e3a\u6b64\uff0c\u4f60\u53ea\u9700\u8981\u5c06\u7c7b\u578b\u5b9a\u4e49\u5728\u5185\u5408\u9002\u7684 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u4e0b\u65b9\u5373\u53ef\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u7c7b\u4f3c\u4e8e\u4e4b\u524d\u7684\u4f8b\u5b50\uff0c\u53ea\u4e0d\u8fc7\u679a\u4e3e\u88ab\u5b9a\u4e49\u5728\u4e86\u7c7b\u5185\uff08\u6b64\u5904\u4f7f\u7528\u4e86 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include class Fruit { public : // \u6ce8\u610f\uff1aFruitType \u88ab\u79fb\u52a8\u5230\u4e86\u7c7b\u4e2d\uff0c\u4f4d\u4e8epubic\u90e8\u5206 // \u540c\u65f6\u6211\u4eec\u5c06\u679a\u4e3e\u7c7b\u53d8\u6210\u4e86\u679a\u4e3e enum FruitType { apple , banana , cherry }; private : FruitType m_type {}; int m_percentageEaten { 0 }; public : Fruit ( FruitType type ) : m_type { type } { } FruitType getType () const { return m_type ; } int getPercentageEaten () const { return m_percentageEaten ; } }; int main () { // \u6ce8\u610f\uff0c\u6211\u4eec\u6b64\u5904\u662f\u901a\u8fc7 Fruit \u6765\u8bbf\u95ee FruitType Fruit apple { Fruit :: apple }; if ( apple . getType () == Fruit :: apple ) std :: cout << \"I am an apple\" ; else std :: cout << \"I am not an apple\" ; return 0 ; } \u9996\u5148\uff0c FruitType \u7684\u5b9a\u4e49\u73b0\u5728\u4f4d\u4e8e\u7c7b\u5185\u3002\u5176\u6b21\uff0c\u6211\u4eec\u5c06\u5176\u5b9a\u4e49\u4e3apublic\uff0c\u4ee5\u4fbf\u80fd\u591f\u4ece\u7c7b\u7684\u5916\u90e8\u8bbf\u95ee\u5b83\u3002 \u6b64\u65f6\u7684\u7c7b\u540d\u5b9e\u9645\u4e0a\u5145\u5f53\u7740\u6240\u6709\u5d4c\u5957\u7c7b\u578b\u7684 \u547d\u540d\u7a7a\u95f4 \uff0c\u548c\u4e48\u679a\u4e3e\u7c7b\u6709\u4e9b\u7c7b\u4f3c\u3002\u5728\u4e4b\u524d\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528\u4e86\u679a\u4e3e\u7c7b\uff0c\u6240\u4ee5\u5728\u4f7f\u7528\u679a\u4e3e\u65f6\u5fc5\u987b\u6dfb\u52a0 FruitType:: \u4f5c\u7528\u57df\u9650\u5b9a\u7b26\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a FruitType \u662f\u4e00\u4e2a\u666e\u901a\u7684\u679a\u4e3e\uff0c\u4f46\u662f\u5b83\u4f5c\u4e3a\u8be5\u7c7b\u7684\u4e00\u90e8\u5206\uff0c\u5728\u8bbf\u95ee\u65f6\u4e5f\u5fc5\u987b\u6dfb\u52a0 FruitType:: \u4f5c\u7528\u57df\u9650\u5b9a\u7b26\u3002 \u6ce8\u610f\uff0c\u56e0\u4e3a\u679a\u4e3e\u7c7b\u4e5f\u53ef\u4ee5\u5145\u5f53\u547d\u540d\u7a7a\u95f4\uff0c\u6240\u4ee5\u5982\u679c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u679a\u4e3e\u7c7b\u800c\u4e0d\u662f\u679a\u4e3e\uff0c\u5219\u5728\u8bbf\u95ee\u65f6\u5fc5\u987b\u6dfb\u52a0 Fruit::FruitType:: \u4f5c\u7528\u57df\u9650\u5b9a\u7b26\u3002\u8fd9\u5b9e\u5728\u662f\u6ca1\u6709\u4ee5\uff0c\u6240\u4ee5\u6211\u4eec\u9009\u62e9\u4f7f\u7528\u666e\u901a\u679a\u4e3e\u3002 \u5176\u4ed6\u53ef\u5d4c\u5957\u7c7b\u578b \u867d\u7136\u679a\u4e3e\u53ef\u80fd\u662f\u6700\u5e38\u88ab\u5d4c\u5957\u5728\u7c7b\u4e2d\u7684\u7c7b\u578b\uff0c\u4f46C++\u5141\u8bb8\u4f60\u5728\u7c7b\u4e2d\u5b9a\u4e49\u5176\u4ed6\u7c7b\u578b\uff0c\u4f8b\u5982\u7c7b\u578b\u5b9a\u4e49\u3001\u7c7b\u578b\u522b\u540d\uff0c\u751a\u81f3\u5176\u4ed6\u7c7b! \u4e0e\u7c7b\u7684\u4efb\u4f55\u666e\u901a\u6210\u5458\u4e00\u6837\uff0c\u5d4c\u5957\u7c7b\u5bf9\u5916\u56f4\u7c7b\u7684\u6210\u5458\u5177\u6709\u4e0e\u5916\u56f4\u7c7b\u76f8\u540c\u7684\u8bbf\u95ee\u6743\u9650\u3002\u7136\u800c\uff0c\u5d4c\u5957\u7c7b\u5bf9\u5916\u56f4\u7c7b\u7684\u201cthis\u201d\u6307\u9488\u6ca1\u6709\u4efb\u4f55\u7279\u6b8a\u7684\u8bbf\u95ee\u6743\u9650\u3002 \u5d4c\u5957\u7c7b\u578b\u7684\u53e6\u4e00\u4e2a\u9650\u5236\u662f\u5b83\u4eec\u4e0d\u80fd\u88ab \u524d\u5411\u58f0\u660e \u3002\u7136\u800c\uff0c\u5728\u5b9e\u8df5\u4e2d\u8fd9\u5f88\u5c11\u4f1a\u5e26\u6765\u95ee\u9898\uff0c\u56e0\u4e3a\u6574\u4e2a\u7c7b\u5b9a\u4e49(\u5305\u62ec\u5d4c\u5957\u7c7b\u578b)\u901a\u5e38\u53ef\u4ee5\u5728\u9700\u8981\u7684\u5730\u65b9\u4f7f\u7528\u88ab\u201c #include \u201d\u3002 \u5b9a\u4e49\u5d4c\u5957\u7c7b\u5e76\u4e0d\u5e38\u89c1\uff0c\u4f46C++\u6807\u51c6\u5e93\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u786e\u5b9e\u4f1a\u8fd9\u6837\u505a\uff0c\u6bd4\u5982\u8fed\u4ee3\u5668\u7c7b\u3002","title":"13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b"},{"location":"13-17-nested-types-in-classes/#_1","text":"\u6570\u636e\u548c\u51fd\u6570\u53ef\u4ee5\u79f0\u4e3a\u7c7b\u7684\u6210\u5458\uff0c\u5728C++\u4e2d\uff0c\u7c7b\u578b\u4e5f\u53ef\u4ee5\u88ab\u5d4c\u5957\u5730\u5b9a\u4e49\u5728\u7c7b\u5185\u3002\u4e3a\u6b64\uff0c\u4f60\u53ea\u9700\u8981\u5c06\u7c7b\u578b\u5b9a\u4e49\u5728\u5185\u5408\u9002\u7684 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u4e0b\u65b9\u5373\u53ef\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u7c7b\u4f3c\u4e8e\u4e4b\u524d\u7684\u4f8b\u5b50\uff0c\u53ea\u4e0d\u8fc7\u679a\u4e3e\u88ab\u5b9a\u4e49\u5728\u4e86\u7c7b\u5185\uff08\u6b64\u5904\u4f7f\u7528\u4e86 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include class Fruit { public : // \u6ce8\u610f\uff1aFruitType \u88ab\u79fb\u52a8\u5230\u4e86\u7c7b\u4e2d\uff0c\u4f4d\u4e8epubic\u90e8\u5206 // \u540c\u65f6\u6211\u4eec\u5c06\u679a\u4e3e\u7c7b\u53d8\u6210\u4e86\u679a\u4e3e enum FruitType { apple , banana , cherry }; private : FruitType m_type {}; int m_percentageEaten { 0 }; public : Fruit ( FruitType type ) : m_type { type } { } FruitType getType () const { return m_type ; } int getPercentageEaten () const { return m_percentageEaten ; } }; int main () { // \u6ce8\u610f\uff0c\u6211\u4eec\u6b64\u5904\u662f\u901a\u8fc7 Fruit \u6765\u8bbf\u95ee FruitType Fruit apple { Fruit :: apple }; if ( apple . getType () == Fruit :: apple ) std :: cout << \"I am an apple\" ; else std :: cout << \"I am not an apple\" ; return 0 ; } \u9996\u5148\uff0c FruitType \u7684\u5b9a\u4e49\u73b0\u5728\u4f4d\u4e8e\u7c7b\u5185\u3002\u5176\u6b21\uff0c\u6211\u4eec\u5c06\u5176\u5b9a\u4e49\u4e3apublic\uff0c\u4ee5\u4fbf\u80fd\u591f\u4ece\u7c7b\u7684\u5916\u90e8\u8bbf\u95ee\u5b83\u3002 \u6b64\u65f6\u7684\u7c7b\u540d\u5b9e\u9645\u4e0a\u5145\u5f53\u7740\u6240\u6709\u5d4c\u5957\u7c7b\u578b\u7684 \u547d\u540d\u7a7a\u95f4 \uff0c\u548c\u4e48\u679a\u4e3e\u7c7b\u6709\u4e9b\u7c7b\u4f3c\u3002\u5728\u4e4b\u524d\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528\u4e86\u679a\u4e3e\u7c7b\uff0c\u6240\u4ee5\u5728\u4f7f\u7528\u679a\u4e3e\u65f6\u5fc5\u987b\u6dfb\u52a0 FruitType:: \u4f5c\u7528\u57df\u9650\u5b9a\u7b26\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a FruitType \u662f\u4e00\u4e2a\u666e\u901a\u7684\u679a\u4e3e\uff0c\u4f46\u662f\u5b83\u4f5c\u4e3a\u8be5\u7c7b\u7684\u4e00\u90e8\u5206\uff0c\u5728\u8bbf\u95ee\u65f6\u4e5f\u5fc5\u987b\u6dfb\u52a0 FruitType:: \u4f5c\u7528\u57df\u9650\u5b9a\u7b26\u3002 \u6ce8\u610f\uff0c\u56e0\u4e3a\u679a\u4e3e\u7c7b\u4e5f\u53ef\u4ee5\u5145\u5f53\u547d\u540d\u7a7a\u95f4\uff0c\u6240\u4ee5\u5982\u679c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u679a\u4e3e\u7c7b\u800c\u4e0d\u662f\u679a\u4e3e\uff0c\u5219\u5728\u8bbf\u95ee\u65f6\u5fc5\u987b\u6dfb\u52a0 Fruit::FruitType:: \u4f5c\u7528\u57df\u9650\u5b9a\u7b26\u3002\u8fd9\u5b9e\u5728\u662f\u6ca1\u6709\u4ee5\uff0c\u6240\u4ee5\u6211\u4eec\u9009\u62e9\u4f7f\u7528\u666e\u901a\u679a\u4e3e\u3002","title":"\u5d4c\u5957\u7c7b\u578b"},{"location":"13-17-nested-types-in-classes/#_2","text":"\u867d\u7136\u679a\u4e3e\u53ef\u80fd\u662f\u6700\u5e38\u88ab\u5d4c\u5957\u5728\u7c7b\u4e2d\u7684\u7c7b\u578b\uff0c\u4f46C++\u5141\u8bb8\u4f60\u5728\u7c7b\u4e2d\u5b9a\u4e49\u5176\u4ed6\u7c7b\u578b\uff0c\u4f8b\u5982\u7c7b\u578b\u5b9a\u4e49\u3001\u7c7b\u578b\u522b\u540d\uff0c\u751a\u81f3\u5176\u4ed6\u7c7b! \u4e0e\u7c7b\u7684\u4efb\u4f55\u666e\u901a\u6210\u5458\u4e00\u6837\uff0c\u5d4c\u5957\u7c7b\u5bf9\u5916\u56f4\u7c7b\u7684\u6210\u5458\u5177\u6709\u4e0e\u5916\u56f4\u7c7b\u76f8\u540c\u7684\u8bbf\u95ee\u6743\u9650\u3002\u7136\u800c\uff0c\u5d4c\u5957\u7c7b\u5bf9\u5916\u56f4\u7c7b\u7684\u201cthis\u201d\u6307\u9488\u6ca1\u6709\u4efb\u4f55\u7279\u6b8a\u7684\u8bbf\u95ee\u6743\u9650\u3002 \u5d4c\u5957\u7c7b\u578b\u7684\u53e6\u4e00\u4e2a\u9650\u5236\u662f\u5b83\u4eec\u4e0d\u80fd\u88ab \u524d\u5411\u58f0\u660e \u3002\u7136\u800c\uff0c\u5728\u5b9e\u8df5\u4e2d\u8fd9\u5f88\u5c11\u4f1a\u5e26\u6765\u95ee\u9898\uff0c\u56e0\u4e3a\u6574\u4e2a\u7c7b\u5b9a\u4e49(\u5305\u62ec\u5d4c\u5957\u7c7b\u578b)\u901a\u5e38\u53ef\u4ee5\u5728\u9700\u8981\u7684\u5730\u65b9\u4f7f\u7528\u88ab\u201c #include \u201d\u3002 \u5b9a\u4e49\u5d4c\u5957\u7c7b\u5e76\u4e0d\u5e38\u89c1\uff0c\u4f46C++\u6807\u51c6\u5e93\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u786e\u5b9e\u4f1a\u8fd9\u6837\u505a\uff0c\u6bd4\u5982\u8fed\u4ee3\u5668\u7c7b\u3002","title":"\u5176\u4ed6\u53ef\u5d4c\u5957\u7c7b\u578b"},{"location":"13-18-timing-your-code/","tags":["class","friend"],"text":"\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5224\u65ad\u4e00\u4e2a\u65b9\u6cd5\u7684\u6027\u80fd\u662f\u5426\u6ee1\u8db3\u8981\u6c42\u3002\u6b64\u65f6\u5e94\u8be5\u5982\u4f55\u5224\u65ad\u5462\uff1f \u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u8ba1\u7b97\u4ee3\u7801\u7684\u8fd0\u884c\u65f6\u95f4\u3002C++ 11\u5728 chrono \u5e93\u4e2d\u63d0\u4f9b\u4e86\u4e00\u4e9b\u529f\u80fd\u7528\u4e8e\u5b9e\u73b0\u8ba1\u65f6\u3002\u7136\u800c\uff0c\u4f7f\u7528 chrono \u5e93\u6709\u70b9\u590d\u6742\uff0c\u4e0d\u8fc7\u6211\u4eec\u53ef\u4ee5\u5c06\u6240\u6709\u9700\u8981\u7684\u8ba1\u65f6\u529f\u80fd\u5c01\u88c5\u5230\u4e00\u4e2a\u7c7b\u4e2d\uff0c\u7136\u540e\u5728\u6211\u4eec\u81ea\u5df1\u7684\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u8be5\u7c7b\u3002 \u4e0b\u9762\u662f\u8ba1\u65f6\u7c7b\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include // for std::chrono functions class Timer { private : // \u7c7b\u578b\u522b\u540d\uff0c\u65b9\u4fbf\u8bbf\u95ee\u5d4c\u5957\u7c7b\u578b using Clock = std :: chrono :: steady_clock ; using Second = std :: chrono :: duration < double , std :: ratio < 1 > > ; std :: chrono :: time_point < Clock > m_beg { Clock :: now () }; //\u975e\u9759\u6001\u6210\u5458\u9ed8\u8ba4\u521d\u59cb\u5316 public : void reset () { m_beg = Clock :: now (); } double elapsed () const { return std :: chrono :: duration_cast < Second > ( Clock :: now () - m_beg ). count (); } }; \u5c31\u662f\u8fd9\u6837\uff01\u8981\u4f7f\u7528\u5b83\uff0c\u6211\u4eec\u9700\u8981\u5728\u4e3b\u51fd\u6570\u7684\u9876\u90e8\u5b9e\u4f8b\u5316\u4e00\u4e2a Timer \u5bf9\u8c61(\u6216\u8005\u5728\u4efb\u4f55\u9700\u8981\u8ba1\u65f6\u7684\u5730\u65b9)\uff0c\u7136\u540e\u5728\u6211\u4eec\u60f3\u77e5\u9053\u7a0b\u5e8f\u8fd0\u884c\u5230\u90a3\u4e2a\u70b9\u82b1\u4e86\u591a\u957f\u65f6\u95f4\u7684\u65f6\u5019\u8c03\u7528elapsed()\u6210\u5458\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { Timer t ; // Code to time goes here std :: cout << \"Time elapsed: \" << t . elapsed () << \" seconds \\n \" ; return 0 ; } \u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u5728\u4e00\u4e2a\u5b9e\u9645\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u5b83\uff0c\u6211\u4eec\u5bf9\u4e00\u4e2a\u5305\u542b10000\u4e2a\u5143\u7d20\u7684\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u9996\u5148\uff0c\u4f7f\u7528\u6211\u4eec\u5728\u524d\u4e00\u7ae0\u5f00\u53d1\u7684\u9009\u62e9\u6392\u5e8f\u7b97\u6cd5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 #include #include // for std::chrono functions #include // for std::size_t #include #include // for std::iota const int g_arrayElements { 10000 }; class Timer { private : // Type aliases to make accessing nested type easier using Clock = std :: chrono :: steady_clock ; using Second = std :: chrono :: duration < double , std :: ratio < 1 > > ; std :: chrono :: time_point < Clock > m_beg { Clock :: now () }; public : void reset () { m_beg = Clock :: now (); } double elapsed () const { return std :: chrono :: duration_cast < Second > ( Clock :: now () - m_beg ). count (); } }; void sortArray ( std :: array < int , g_arrayElements >& array ) { // Step through each element of the array // (except the last one, which will already be sorted by the time we get there) for ( std :: size_t startIndex { 0 }; startIndex < ( g_arrayElements - 1 ); ++ startIndex ) { // smallestIndex is the index of the smallest element we\u2019ve encountered this iteration // Start by assuming the smallest element is the first element of this iteration std :: size_t smallestIndex { startIndex }; // Then look for a smaller element in the rest of the array for ( std :: size_t currentIndex { startIndex + 1 }; currentIndex < g_arrayElements ; ++ currentIndex ) { // If we've found an element that is smaller than our previously found smallest if ( array [ currentIndex ] < array [ smallestIndex ]) { // then keep track of it smallestIndex = currentIndex ; } } // smallestIndex is now the smallest element in the remaining array // swap our start element with our smallest element (this sorts it into the correct place) std :: swap ( array [ startIndex ], array [ smallestIndex ]); } } int main () { std :: array < int , g_arrayElements > array ; std :: iota ( array . rbegin (), array . rend (), 1 ); // fill the array with values 10000 to 1 Timer t ; sortArray ( array ); std :: cout << \"Time taken: \" << t . elapsed () << \" seconds \\n \" ; return 0 ; } \u5728\u4f5c\u8005\u7684\u673a\u5668\u4e0a\uff0c\u4e09\u6b21\u8fd0\u884c\u4ea7\u751f\u7684\u65f6\u95f4\u5206\u522b\u4e3a0.0507\u30010.0506\u548c0.0498\u3002\u56e0\u6b64\u6392\u5e8f\u7684\u8017\u65f6\u5dee\u4e0d\u591a0.05\u79d2\u3002 \u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684 std::sort \u8fdb\u884c\u76f8\u540c\u7684\u6d4b\u8bd5\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include // for std::sort #include #include // for std::chrono functions #include // for std::size_t #include #include // for std::iota const int g_arrayElements { 10000 }; class Timer { private : // Type aliases to make accessing nested type easier using Clock = std :: chrono :: steady_clock ; using Second = std :: chrono :: duration < double , std :: ratio < 1 > > ; std :: chrono :: time_point < Clock > m_beg { Clock :: now () }; public : void reset () { m_beg = Clock :: now (); } double elapsed () const { return std :: chrono :: duration_cast < Second > ( Clock :: now () - m_beg ). count (); } }; int main () { std :: array < int , g_arrayElements > array ; std :: iota ( array . rbegin (), array . rend (), 1 ); // fill the array with values 10000 to 1 Timer t ; std :: ranges :: sort ( array ); // Since C++20 // If your compiler isn't C++20-capable // std::sort(array.begin(), array.end()); std :: cout << \"Time taken: \" << t . elapsed () << \" seconds \\n \" ; return 0 ; } \u5728\u4f5c\u8005\u7684\u673a\u5668\u4e0a\uff0c\u8fd0\u884c\u7ed3\u679c\u662f:0.000693,0.000692\u548c0.000699\u3002\u57fa\u672c\u4e0a\u57280.0007\u5de6\u53f3\u3002 \u6362\u53e5\u8bdd\u8bf4\uff0c\u5728\u672c\u4f8b\u4e2d\uff0c std::sort \u6bd4\u6211\u4eec\u81ea\u5df1\u7f16\u5199\u7684\u9009\u62e9\u6392\u5e8f\u5feb100\u500d! \u5173\u4e8e\u8ba1\u65f6\u7684\u51e0\u70b9\u6ce8\u610f\u4e8b\u9879 \u8ba1\u65f6\u5e76\u4e0d\u590d\u6742\uff0c\u4f46\u5176\u7ed3\u679c\u4f1a\u53d7\u5230\u5f88\u591a\u56e0\u7d20\u7684\u5f71\u54cd\uff0c\u77e5\u9053\u8fd9\u4e9b\u56e0\u7d20\u662f\u4ec0\u4e48\u5f88\u91cd\u8981\u3002 \u9996\u5148\uff0c\u786e\u4fdd\u4f60\u4f7f\u7528\u7684\u662frelease\u7248\u672c\uff0c\u800c\u4e0d\u662fdebug\u7248\u672c\u7684\u7a0b\u5e8f\u3002debug\u7248\u672c\u7684\u7a0b\u5e8f\u4f1a\u5173\u95ed\u4f18\u5316\uff0c\u800c\u4f18\u5316\u53ef\u80fd\u4f1a\u5bf9\u7ed3\u679c\u4ea7\u751f\u91cd\u5927\u5f71\u54cd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528debug\u7248\u672c\u65f6\uff0c\u5728\u4f5c\u8005\u7684\u673a\u5668\u4e0a\u8fd0\u884c\u4e0a\u9762\u7684 std::sort \u793a\u4f8b\u82b1\u4e860.0235\u79d2\u2014\u2014\u6162\u4e8633\u500d! \u5176\u6b21\uff0c\u8ba1\u65f6\u7ed3\u679c\u4f1a\u53d7\u5230\u7cfb\u7edf\u5728\u540e\u53f0\u53ef\u80fd\u6267\u884c\u7684\u5176\u4ed6\u64cd\u4f5c\u7684\u5f71\u54cd\u3002\u4e3a\u4e86\u8fbe\u5230\u6700\u597d\u7684\u6548\u679c\uff0c\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u6ca1\u6709\u505a\u4efb\u4f55CPU\u6216\u5185\u5b58\u5bc6\u96c6\u578b\u7684\u4e8b\u60c5(\u4f8b\u5982\u73a9\u6e38\u620f)\u6216\u786c\u76d8\u5bc6\u96c6\u578b\u7684\u4e8b\u60c5(\u4f8b\u5982\u641c\u7d22\u6587\u4ef6\u6216\u8fd0\u884c\u9632\u75c5\u6bd2\u626b\u63cf)\u3002 \u7136\u540e\u6d4b\u91cf\u81f3\u5c113\u6b21\u3002\u5982\u679c\u7ed3\u679c\u90fd\u76f8\u4f3c\uff0c\u53d6\u5e73\u5747\u503c\u3002\u5982\u679c\u6709\u4e00\u4e24\u4e2a\u7ed3\u679c\u4e0d\u540c\uff0c\u5219\u518d\u8fd0\u884c\u7a0b\u5e8f\u51e0\u6b21\uff0c\u76f4\u5230\u60a8\u66f4\u597d\u5730\u4e86\u89e3\u54ea\u4e9b\u662f\u5f02\u5e38\u503c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6709\u65f6\u5019\u4e00\u4e9b\u770b\u4e0a\u53bb\u65e0\u4f24\u5927\u96c5\u7684\u4e8b\u60c5\u7ffb\u5230\u4f1a\u4e25\u91cd\u5f71\u54cd\u7ed3\u679c\u7684\u6b63\u786e\u6027\u3002\u4f8b\u5982\uff0c\u4f60\u7684\u6d4f\u89c8\u5668\u4e4b\u524d\u6253\u5f00\u4e86\u4e00\u4e2a\u7f51\u7ad9\uff0c\u5f53\u8be5\u7f51\u7ad9\u5728\u540e\u53f0\u8f6e\u6362\u5e7f\u544a\u9875\u9762\u65f6\uff0c\u5b83\u9700\u8981\u89e3\u6790\u4e00\u5806javascript\uff0c\u6b64\u65f6\u53ef\u80fd\u4f1a\u6682\u65f6\u5c06\u4f60\u7684CPU\u5229\u7528\u7387\u63d0\u9ad8\u5230100%\u3002\u591a\u6b21\u8fd0\u884c\u6709\u52a9\u4e8e\u8bc6\u522b\u521d\u59cb\u8fd0\u884c\u662f\u5426\u53d7\u5230\u4e86\u6b64\u7c7b\u4e8b\u4ef6\u7684\u5f71\u54cd\u3002 \u7b2c\u4e09\uff0c\u5f53\u5728\u4e24\u7ec4\u4ee3\u7801\u4e4b\u95f4\u8fdb\u884c\u6bd4\u8f83\u65f6\uff0c\u8981\u8b66\u60d5\u5728\u8fd0\u884c\u4e4b\u95f4\u53ef\u80fd\u4f1a\u5f71\u54cd\u8ba1\u65f6\u7684\u53d8\u5316\u3002\u4f60\u7684\u7cfb\u7edf\u53ef\u80fd\u5728\u540e\u53f0\u542f\u52a8\u4e86\u53cd\u75c5\u6bd2\u626b\u63cf\uff0c\u6216\u8005\u4f60\u73b0\u5728\u6b63\u5728\u6d41\u5a92\u4f53\u97f3\u4e50\uff0c\u800c\u4f60\u4e4b\u524d\u6ca1\u6709\u3002\u968f\u673a\u5316\u4e5f\u4f1a\u5f71\u54cd\u65f6\u95f4\u3002\u5982\u679c\u6211\u4eec\u5bf9\u4e00\u4e2a\u5145\u6ee1\u968f\u673a\u6570\u7684\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u7ed3\u679c\u53ef\u80fd\u4f1a\u53d7\u5230\u968f\u673a\u5316\u7684\u5f71\u54cd\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e5f\u662f\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528\u968f\u673a\u5316\u7684\uff0c\u4f46\u8981\u786e\u4fdd\u4f7f\u7528\u56fa\u5b9a\u7684\u79cd\u5b50(\u4f8b\u5982\uff0c\u4e0d\u8981\u4f7f\u7528\u7cfb\u7edf\u65f6\u949f)\uff0c\u8fd9\u6837\u6bcf\u6b21\u8fd0\u884c\u7684\u968f\u673a\u5316\u90fd\u662f\u76f8\u540c\u7684\u3002\u53e6\u5916\uff0c\u786e\u4fdd\u4f60\u6ca1\u6709\u5bf9\u7b49\u5f85\u7528\u6237\u8f93\u5165\u8fdb\u884c\u8ba1\u65f6\uff0c\u56e0\u4e3a\u7528\u6237\u8f93\u5165\u6240\u9700\u7684\u65f6\u95f4\u4e0d\u5e94\u8be5\u662f\u4f60\u8ba1\u65f6\u8003\u8651\u7684\u4e00\u90e8\u5206\u3002","title":"13.18 - \u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6"},{"location":"13-18-timing-your-code/#_1","text":"\u8ba1\u65f6\u5e76\u4e0d\u590d\u6742\uff0c\u4f46\u5176\u7ed3\u679c\u4f1a\u53d7\u5230\u5f88\u591a\u56e0\u7d20\u7684\u5f71\u54cd\uff0c\u77e5\u9053\u8fd9\u4e9b\u56e0\u7d20\u662f\u4ec0\u4e48\u5f88\u91cd\u8981\u3002 \u9996\u5148\uff0c\u786e\u4fdd\u4f60\u4f7f\u7528\u7684\u662frelease\u7248\u672c\uff0c\u800c\u4e0d\u662fdebug\u7248\u672c\u7684\u7a0b\u5e8f\u3002debug\u7248\u672c\u7684\u7a0b\u5e8f\u4f1a\u5173\u95ed\u4f18\u5316\uff0c\u800c\u4f18\u5316\u53ef\u80fd\u4f1a\u5bf9\u7ed3\u679c\u4ea7\u751f\u91cd\u5927\u5f71\u54cd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528debug\u7248\u672c\u65f6\uff0c\u5728\u4f5c\u8005\u7684\u673a\u5668\u4e0a\u8fd0\u884c\u4e0a\u9762\u7684 std::sort \u793a\u4f8b\u82b1\u4e860.0235\u79d2\u2014\u2014\u6162\u4e8633\u500d! \u5176\u6b21\uff0c\u8ba1\u65f6\u7ed3\u679c\u4f1a\u53d7\u5230\u7cfb\u7edf\u5728\u540e\u53f0\u53ef\u80fd\u6267\u884c\u7684\u5176\u4ed6\u64cd\u4f5c\u7684\u5f71\u54cd\u3002\u4e3a\u4e86\u8fbe\u5230\u6700\u597d\u7684\u6548\u679c\uff0c\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u6ca1\u6709\u505a\u4efb\u4f55CPU\u6216\u5185\u5b58\u5bc6\u96c6\u578b\u7684\u4e8b\u60c5(\u4f8b\u5982\u73a9\u6e38\u620f)\u6216\u786c\u76d8\u5bc6\u96c6\u578b\u7684\u4e8b\u60c5(\u4f8b\u5982\u641c\u7d22\u6587\u4ef6\u6216\u8fd0\u884c\u9632\u75c5\u6bd2\u626b\u63cf)\u3002 \u7136\u540e\u6d4b\u91cf\u81f3\u5c113\u6b21\u3002\u5982\u679c\u7ed3\u679c\u90fd\u76f8\u4f3c\uff0c\u53d6\u5e73\u5747\u503c\u3002\u5982\u679c\u6709\u4e00\u4e24\u4e2a\u7ed3\u679c\u4e0d\u540c\uff0c\u5219\u518d\u8fd0\u884c\u7a0b\u5e8f\u51e0\u6b21\uff0c\u76f4\u5230\u60a8\u66f4\u597d\u5730\u4e86\u89e3\u54ea\u4e9b\u662f\u5f02\u5e38\u503c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6709\u65f6\u5019\u4e00\u4e9b\u770b\u4e0a\u53bb\u65e0\u4f24\u5927\u96c5\u7684\u4e8b\u60c5\u7ffb\u5230\u4f1a\u4e25\u91cd\u5f71\u54cd\u7ed3\u679c\u7684\u6b63\u786e\u6027\u3002\u4f8b\u5982\uff0c\u4f60\u7684\u6d4f\u89c8\u5668\u4e4b\u524d\u6253\u5f00\u4e86\u4e00\u4e2a\u7f51\u7ad9\uff0c\u5f53\u8be5\u7f51\u7ad9\u5728\u540e\u53f0\u8f6e\u6362\u5e7f\u544a\u9875\u9762\u65f6\uff0c\u5b83\u9700\u8981\u89e3\u6790\u4e00\u5806javascript\uff0c\u6b64\u65f6\u53ef\u80fd\u4f1a\u6682\u65f6\u5c06\u4f60\u7684CPU\u5229\u7528\u7387\u63d0\u9ad8\u5230100%\u3002\u591a\u6b21\u8fd0\u884c\u6709\u52a9\u4e8e\u8bc6\u522b\u521d\u59cb\u8fd0\u884c\u662f\u5426\u53d7\u5230\u4e86\u6b64\u7c7b\u4e8b\u4ef6\u7684\u5f71\u54cd\u3002 \u7b2c\u4e09\uff0c\u5f53\u5728\u4e24\u7ec4\u4ee3\u7801\u4e4b\u95f4\u8fdb\u884c\u6bd4\u8f83\u65f6\uff0c\u8981\u8b66\u60d5\u5728\u8fd0\u884c\u4e4b\u95f4\u53ef\u80fd\u4f1a\u5f71\u54cd\u8ba1\u65f6\u7684\u53d8\u5316\u3002\u4f60\u7684\u7cfb\u7edf\u53ef\u80fd\u5728\u540e\u53f0\u542f\u52a8\u4e86\u53cd\u75c5\u6bd2\u626b\u63cf\uff0c\u6216\u8005\u4f60\u73b0\u5728\u6b63\u5728\u6d41\u5a92\u4f53\u97f3\u4e50\uff0c\u800c\u4f60\u4e4b\u524d\u6ca1\u6709\u3002\u968f\u673a\u5316\u4e5f\u4f1a\u5f71\u54cd\u65f6\u95f4\u3002\u5982\u679c\u6211\u4eec\u5bf9\u4e00\u4e2a\u5145\u6ee1\u968f\u673a\u6570\u7684\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u7ed3\u679c\u53ef\u80fd\u4f1a\u53d7\u5230\u968f\u673a\u5316\u7684\u5f71\u54cd\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e5f\u662f\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528\u968f\u673a\u5316\u7684\uff0c\u4f46\u8981\u786e\u4fdd\u4f7f\u7528\u56fa\u5b9a\u7684\u79cd\u5b50(\u4f8b\u5982\uff0c\u4e0d\u8981\u4f7f\u7528\u7cfb\u7edf\u65f6\u949f)\uff0c\u8fd9\u6837\u6bcf\u6b21\u8fd0\u884c\u7684\u968f\u673a\u5316\u90fd\u662f\u76f8\u540c\u7684\u3002\u53e6\u5916\uff0c\u786e\u4fdd\u4f60\u6ca1\u6709\u5bf9\u7b49\u5f85\u7528\u6237\u8f93\u5165\u8fdb\u884c\u8ba1\u65f6\uff0c\u56e0\u4e3a\u7528\u6237\u8f93\u5165\u6240\u9700\u7684\u65f6\u95f4\u4e0d\u5e94\u8be5\u662f\u4f60\u8ba1\u65f6\u8003\u8651\u7684\u4e00\u90e8\u5206\u3002","title":"\u5173\u4e8e\u8ba1\u65f6\u7684\u51e0\u70b9\u6ce8\u610f\u4e8b\u9879"},{"location":"13-2-classes-and-class-members/","tags":["class"],"text":"Key Takeaway \u867d\u7136 C++ \u63d0\u4f9b\u4e86\u4e0d\u5c11\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982 char\u3001int\u3001long\u3001float\u3001double \u7b49\u7b49\uff09\uff0c\u5e94\u4ed8\u76f8\u5bf9\u7684\u7b80\u5355\u95ee\u9898\uff0c\u8fd9\u4e9b\u57fa\u672c\u6570\u636e\u7ed3\u6784\u4f3c\u4e4e\u5df2\u7ecf\u8db3\u591f\u4e86\uff0c\u4f46\u662f\u4f7f\u7528\u5b83\u4eec\u89e3\u51b3\u590d\u6742\u95ee\u9898\u7684\u8bdd\u5c31\u4f1a\u663e\u5f97\u529b\u4e0d\u4ece\u5fc3\u3002C++ \u6700\u6709\u7528\u7684\u4e00\u4e2a\u7279\u6027\u5c31\u662f\u5141\u8bb8\u7528\u6237\u9488\u5bf9\u8981\u89e3\u51b3\u7684\u95ee\u9898\u6765\u81ea\u5b9a\u4e49\u6570\u636e\u7c7b\u578b\u3002 \u679a\u4e3e\u7c7b\u578b \u548c \u7ed3\u6784\u4f53 \u662f\u4e24\u79cd\u6211\u4eec\u5df2\u7ecf\u4ecb\u7ecd\u8fc7\u7684\u81ea\u5b9a\u4e49\u6570\u636e\u7c7b\u578b\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u7ed3\u6784\u4f53\u6765\u5b58\u653e\u65e5\u671f\uff1a 1 2 3 4 5 6 struct DateStruct { int year {}; int month {}; int day {}; }; \u679a\u4e3e\u7c7b\u578b\u548c\u7eaf\u6570\u636e\u7ed3\u6784\u4f53\uff08\u53ea\u5305\u542b\u53d8\u91cf\u7684\u7ed3\u6784\u4f53\uff09\u5e38\u7528\u4e8e\u4f20\u7edf\u7684\u975e\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u9886\u57df\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ea\u80fd\u4fdd\u5b58\u6570\u636e\u3002\u6211\u4eec\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u521d\u59cb\u5316\u7ed3\u6784\u4f53\uff1a 1 DateStruct today { 2020 , 10 , 14 }; // use uniform initialization \u73b0\u5728\uff0c\u5982\u679c\u4f60\u60f3\u5c06\u7ed3\u6784\u4f53\u7684\u5185\u5bb9\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\uff0c\u90a3\u4e48\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u51fd\u6570\u6765\u5b8c\u6210\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include struct DateStruct { int year {}; int month {}; int day {}; }; void print ( const DateStruct & date ) { std :: cout << date . year << '/' << date . month << '/' << date . day ; } int main () { DateStruct today { 2020 , 10 , 14 }; // \u4f7f\u7528\u7edf\u4e00\u521d\u59cb\u5316 today . day = 16 ; // \u4f7f\u7528\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26\u9009\u62e9\u4e00\u4e2a\u7ed3\u6784\u4f53\u6210\u5458 print ( today ); return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2020/10/16 \u7c7b\uff08Classes\uff09 \u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u4e16\u754c\u4e2d\uff0c\u6211\u4eec\u4e0d\u4ec5\u9700\u8981\u81ea\u5b9a\u4e49\u7c7b\u578b\u80fd\u591f\u5305\u542b\u6570\u636e\uff0c\u5b83\u8fd8\u9700\u8981\u63d0\u4f9b\u80fd\u591f\u64cd\u4f5c\u8fd9\u4e9b\u6570\u636e\u7684\u51fd\u6570\uff08\u65b9\u6cd5\uff09\u3002\u5728C++\u4e2d\uff0c\u5b9a\u4e49\u8fd9\u6837\u7684\u6570\u636e\u53ef\u4ee5\u4f7f\u7528 class \u5173\u952e\u5b57\u3002\u8be5\u5173\u952e\u5b57\u53ef\u4ee5\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u88ab\u79f0\u4e3a\u7c7b\uff08class\uff09\u7684\u6570\u636e\u7c7b\u578b\u3002 \u5728 C++ \u4e2d\uff0c\u7c7b\u548c\u7ed3\u6784\u4f53\u672c\u8d28\u4e0a\u662f\u76f8\u540c\u7684\u3002\u5b9e\u9645\u4e0a\uff0c\u4e0b\u9762\u4ee3\u7801\u4e2d\u7684\u7ed3\u6784\u4f53\u548c\u7c7b\u51e0\u4e4e\u6ca1\u6709\u533a\u522b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct DateStruct { int year {}; int month {}; int day {}; }; class DateClass { public : int m_year {}; int m_month {}; int m_day {}; }; \u6ce8\u610f\uff0c\u4e0a\u9762\u4ee3\u7801\u4e2d\u7684\u552f\u4e00\u7684\u4e00\u5904\u660e\u663e\u4e0d\u540c\uff0c\u5c31\u662f\u7c7b\u4e2d\u7684public\u5173\u952e\u5b57\u3002\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u8ba8\u8bba\u8be5\u5173\u952e\u5b57\u7684\u4f5c\u7528\u3002 \u4e0e\u58f0\u660e\u7ed3\u6784\u4f53\u7c7b\u4f3c\uff0c\u7c7b\u7684\u58f0\u660e\u5e76\u4e0d\u4f1a\u5b9e\u9645\u5206\u914d\u4efb\u4f55\u5185\u5b58\uff0c\u5b83\u4ec5\u4ec5\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b\u5e94\u8be5\u201c\u662f\u4ec0\u4e48\u6837\u7684\u201d\u3002 \u6ce8\u610f \u4e0e\u5b9a\u4e49\u7ed3\u6784\u4f53\u7c7b\u4f3c\uff0c\u5b9a\u4e49\u7c7b\u7684\u65f6\u5019\u6700\u5bb9\u6613\u72af\u7684\u4e00\u4e2a\u9519\u8bef\u5c31\u662f\u5fd8\u8bb0\u7ed3\u5c3e\u5904\u7684\u5206\u53f7\u3002\u5fd8\u8bb0\u8fd9\u4e2a\u5206\u53f7\u4f1a\u5bfc\u81f4 \u4e0b\u4e00\u884c\u4ee3\u7801 \u7f16\u8bd1\u62a5\u9519\u3002\u73b0\u4ee3\u7f16\u8bd1\u5668\uff0c\u4f8b\u5982 Visual Studio 2010 \u4f1a\u5728\u4f60\u5fd8\u8bb0\u8be5\u5206\u53f7\u65f6\u63d0\u9192\u4f60\uff0c\u4f46\u662f\u4e00\u4e9b\u8f83\u65e9\u7684\u6216\u8005\u7b80\u5355\u7684\u7f16\u8bd1\u5668\u5e76\u4e0d\u4f1a\u63d0\u9192\u4f60\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u95ee\u9898\u96be\u4ee5\u88ab\u53d1\u73b0\u3002 \u7c7b\uff08\u548c\u7ed3\u6784\u4f53\uff09\u7684\u5b9a\u4e49\u7c7b\u4f3c\u4e8e \u84dd\u56fe \u2014\u2014\u5b83\u4eec\u63cf\u8ff0\u4e86\u4e00\u4e2a\u5bf9\u8c61\u5e94\u8be5\u201c\u662f\u4ec0\u4e48\u6837\u7684\u201d\uff0c\u4f46\u5b9a\u4e49\u5e76\u4e0d\u4f1a\u5b9e\u9645\u521b\u5efa\u5bf9\u8c61\u3002\u4e3a\u4e86\u521b\u5efa\u5bf9\u8c61\uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u8be5\u7c7b\u578b\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\uff1a 1 DateClass today { 2020 , 10 , 14 }; // declare a variable of class DateClass \u5c0f\u8d34\u58eb \u5728\u58f0\u660e\u7c7b\u7684\u65f6\u5019\u5c31\u8981\u5bf9\u5176\u6210\u5458\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u3002 \u6210\u5458\u51fd\u6570 \u9664\u4e86\u5b58\u653e\u6570\u636e\u4e4b\u5916\uff0c\u7c7b\uff08\u548c\u7ed3\u6784\u4f53\uff09\u540c\u6837\u8fd8\u53ef\u4ee5\u5305\u542b\u51fd\u6570\uff01\u5b9a\u4e49\u5728\u7c7b\u4e2d\u7684\u51fd\u6570\u79f0\u4e3a \u6210\u5458\u51fd\u6570 \uff08\u6216\u79f0\u4e3a\u65b9\u6cd5\uff09\u3002\u6210\u5458\u51fd\u6570\u7684\u5b9a\u4e49\u53ef\u4ee5\u88ab\u5305\u542b\u5728\u7c7b\u5185\uff0c\u4e5f\u53ef\u4ee5\u5b9a\u4e49\u5728\u7c7b\u5916\u3002\u6211\u4eec\u6682\u65f6\u5c06\u5176\u5b9a\u4e49\u5728\u7c7b\u5185\uff0c\u8fd9\u79cd\u5f62\u5f0f\u76f8\u5bf9\u66f4\u7b80\u5355\uff0c\u7a0d\u540e\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5982\u4f55\u5728\u7c7b\u5916\u5b9a\u4e49\u7c7b\u7684\u65b9\u6cd5\u3002 \u5177\u6709\u6210\u5458\u51fd\u6570\u7684Data\u7c7b\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class DateClass { public : int m_year {}; int m_month {}; int m_day {}; void print () // defines a member function named print() { std :: cout << m_year << '/' << m_month << '/' << m_day ; } }; \u548c\u7ed3\u6784\u4f53\u6210\u5458\u4e00\u6837\uff0c\u7c7b\u7684\u6210\u5458\uff08\u5305\u62ec\u53d8\u91cf\u548c\u51fd\u6570\uff09\u53ef\u4ee5\u901a\u8fc7 \u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26(.) \u6765\u8bbf\u95ee\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include class DateClass { public : int m_year {}; int m_month {}; int m_day {}; void print () { std :: cout << m_year << '/' << m_month << '/' << m_day ; } }; int main () { DateClass today { 2020 , 10 , 14 }; today . m_day = 16 ; // use member selection operator to select a member variable of the class today . print (); // use member selection operator to call a member function of the class return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 2020/10/16 \u89c2\u5bdf\u53ef\u4ee5\u53d1\u73b0\uff0c\u4e0a\u8ff0\u4ee3\u7801\u548c\u4e4b\u524d\u4f7f\u7528\u7ed3\u6784\u4f53\u7684\u7248\u672c\u8fd1\u4e4e\u662f\u4e00\u81f4\u7684\u3002 \u4e0d\u8fc7\uff0c\u5176\u4e2d\u8fd8\u662f\u6709\u51e0\u5904\u4e0d\u540c\u3002\u5728\u4f7f\u7528\u7ed3\u6784\u4f53\u7684\u7248\u672c\u4e2d\uff0c print() \u51fd\u6570\u9700\u8981\u5c06\u7ed3\u6784\u4f53\u672c\u8eab\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\uff0c\u5426\u5219 print() \u51fd\u6570\u5c31\u65e0\u4ece\u5224\u65ad\u6211\u4eec\u8981\u6253\u5370\u7684\u662f\u54ea\u4e2a\u7ed3\u6784\u4f53\u7684\u5185\u5bb9\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u5728\u51fd\u6570\u4e2d\u660e\u786e\u5730\u4f7f\u7528\u8be5\u53c2\u6570\u3002 \u6210\u5458\u51fd\u6570\u7684\u5de5\u4f5c\u65b9\u5f0f\u5219\u7565\u6709\u4e0d\u540c\uff1a\u6240\u6709\u6210\u5458\u51fd\u6570\u7684\u8c03\u7528\u5fc5\u987b\u57fa\u4e8e\u8be5\u7c7b\u7684\u4e00\u4e2a\u5177\u4f53\u5bf9\u8c61\u3002\u5f53\u6211\u4eec\u8c03\u7528 today.print() \u7684\u65f6\u5019\uff0c\u5b9e\u9645\u4e0a\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\u8c03\u7528 today \u5bf9\u8c61\u7684\u6210\u5458\u51fd\u6570 print() \u3002 \u8ba9\u6211\u4eec\u5728\u770b\u4e00\u773c\u6210\u5458\u51fd\u6570 print \u7684\u5b9a\u4e49\uff1a 1 2 3 4 void print () // defines a member function named print() { std :: cout << m_year << '/' << m_month << '/' << m_day ; } m_year \u3001 m_month \u548c m_day \u662f\u8c01\u7684\u53d8\u91cf\uff1f\u5b83\u4eec\u6307\u7684\u662f\u5176\u51fd\u6570\u8c03\u7528\u65f6\u5173\u8054\u7684\u5bf9\u8c61\u6240\u5c5e\u7684\u53d8\u91cf\u3002 \u56e0\u6b64\u5f53\u8c03\u7528 today.print() \u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c06 m_day \u89e3\u6790\u4e3a today.m_day \uff0c\u5c06 m_month \u89e3\u6790\u4e3a today.m_month \uff0c\u5c06 m_year \u89e3\u6790\u4e3a today.m_year \u3002\u5f53\u8c03\u7528 tomorrow.print() \u65f6\uff0c\u5219 m_day \u89e3\u6790\u4e3a tomorrow.m_day \u3002 \u53ef\u89c1\uff0c\u76f8\u5173\u8054\u7684\u5bf9\u8c61\u4f1a\u88ab\u9690\u5f0f\u5730\u4f20\u5165\u6210\u5458\u51fd\u6570\u3002\u56e0\u6b64\uff0c\u5b83\u901a\u5e38\u88ab\u79f0\u4e3a \u9690\u5f0f\u5bf9\u8c61 \u3002 \u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u9690\u5f0f\u5bf9\u8c61\u4f20\u9012\u7684\u5de5\u4f5c\u539f\u7406\u3002 \u5bf9\u4e8e\u975e\u6210\u5458\u51fd\u6570\u6765\u8bf4\uff0c\u6211\u4eec\u5fc5\u987b\u5c06\u5b83\u9700\u8981\u64cd\u4f5c\u7684\u6570\u636e\u4f20\u9012\u7ed9\u51fd\u6570\u3002\u800c\u5bf9\u4e8e\u6210\u5458\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5047\u5b9a\u5b83\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8fd9\u4e2a\u7c7b\u7684\u5bf9\u8c61\u3002 \u4e3a\u6210\u5458\u53d8\u91cf\u6dfb\u52a0 \u201c m_ \u201d \u524d\u7f00\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u533a\u5206\u6210\u5458\u53d8\u91cf\u3001\u51fd\u6570\u53c2\u6570\u548c\u5c40\u90e8\u53d8\u91cf\u3002\u8fd9\u4e48\u505a\u6709\u8bf8\u591a\u597d\u5904\uff1a\u9996\u5148\uff0c\u5f53\u6211\u4eec\u770b\u5230\u5bf9\u5177\u6709 \u201c m_ \u201d\u524d\u7f00\u7684\u53d8\u91cf\u8fdb\u884c\u8d4b\u503c\u65f6\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u6307\u5b9a\u8be5\u64cd\u4f5c\u4f1a\u6539\u53d8\u5f53\u524d\u7c7b\u5b9e\u4f8b\u7684\u72b6\u6001\u3002\u5176\u6b21\uff0c\u51fd\u6570 \u5f62\u53c2 \u6216\u5c40\u90e8\u53d8\u91cf\u88ab\u58f0\u660e\u5728\u5f53\u524d\u51fd\u6570\u4e2d\uff0c\u800c\u6210\u5458\u53d8\u91cf\u5219\u4e0d\u540c\uff0c\u5b83\u88ab\u5b9a\u4e49\u5728\u7c7b\u4e2d\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u77e5\u9053 m_ \u524d\u7f00\u7684\u53d8\u91cf\u7684\u5177\u4f53\u5b9a\u4e49\uff0c\u5c31\u9700\u8981\u53bb\u7c7b\u7684\u5b9a\u4e49\u4e2d\u67e5\u770b\uff0c\u800c\u4e0d\u662f\u5728\u5f53\u524d\u51fd\u6570\u4e2d\u67e5\u770b\u3002 \u6309\u7167\u7ba1\u7406\uff0c\u7c7b\u540d\u901a\u5e38\u662f\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u7c7b\u547d\u540d\u65f6\uff0c\u4f7f\u7528\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u3002 \u518d\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include #include class Employee { public : std :: string m_name {}; int m_id {}; double m_wage {}; // Print employee information to the screen void print () { std :: cout << \"Name: \" << m_name << \" Id: \" << m_id << \" Wage: $\" << m_wage << '\\n' ; } }; int main () { // Declare two employees Employee alex { \"Alex\" , 1 , 25.00 }; Employee joe { \"Joe\" , 2 , 22.25 }; // Print out the employee information alex . print (); joe . print (); return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 Name: Alex Id: 1 Wage: $25 Name: Joe Id: 2 Wage: $22.25 \u5bf9\u4e8e\u975e\u6210\u5458\u51fd\u6570\u6765\u8bf4\uff0c\u5b83\u4e0d\u80fd\u8c03\u7528\u88ab\u5b9a\u4e49\u5728\u5b83\u4e0b\u65b9\uff08\u5373\u540e\u5b9a\u4e49\uff09\u7684\u51fd\u6570\uff0c\u9664\u975e\u5b83\u80fd\u591f\u770b\u5230\u8be5\u51fd\u6570\u7684 \u524d\u5411\u58f0\u660e \uff1a 1 2 3 4 5 6 7 8 void x () { // You can't call y() from here unless the compiler has already seen a forward declaration for y() } void y () { } \u800c\u5bf9\u4e8e\u6210\u5458\u51fd\u6570\u6765\u8bf4\uff0c\u5219\u6ca1\u6709\u8be5\u9650\u5236\uff1a 1 2 3 4 5 6 class foo { public : void x () { y (); } // okay to call y() here, even though y() isn't defined until later in this class void y () { }; }; \u6210\u5458\u7c7b\u578b \u9664\u4e86\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u51fd\u6570\uff0c\u7c7b\u8fd8\u53ef\u4ee5\u5177\u6709 \u6210\u5458\u7c7b\u578b \u6216\u5d4c\u5957\u7c7b\u578b\uff08\u4e5f\u5305\u62ec \u7c7b\u578b\u522b\u540d \uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Employee { public : using IDType = int ; std :: string m_name {}; IDType m_id {}; double m_wage {}; // Print employee information to the screen void print () { std :: cout << \"Name: \" << m_name << \" Id: \" << m_id << \" Wage: $\" << m_wage << '\\n' ; } }; \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7c7b\u540d\u5b9e\u9645\u4e0a\u6210\u4e3a\u4e86\u5176\u5d4c\u5957\u7c7b\u578b\u7684 \u547d\u540d\u7a7a\u95f4 \u3002\u5728\u8be5\u7c7b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 IDType \uff0c\u800c\u5728\u7c7b\u5916\uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528 Employee::IDType \u6765\u8bbf\u95ee\u8be5\u7c7b\u578b\u3002 \u5982\u679c\u5728\u540e\u7eed\u7684\u5f00\u53d1\u4e2d\u6211\u4eec\u53d1\u73b0 int \u7c7b\u578b\u5df2\u7ecf\u65e0\u6cd5\u6ee1\u8db3\u9700\u8981\uff0c\u800c\u5fc5\u987b\u6539\u7528 std::string \uff0c\u6b64\u65f6\u6211\u4eec\u53ea\u9700\u8981\u66f4\u65b0\u8fd9\u4e2a\u7c7b\u578b\u522b\u540d\uff0c\u800c\u4e0d\u9700\u8981\u5c06\u6240\u6709\u7684 int \u90fd\u66ff\u6362\u4e3a std::string \u3002 \u5d4c\u5957\u7c7b\u578b\u4e0d\u80fd\u88ab\u524d\u5411\u58f0\u660e\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5d4c\u5957\u7c7b\u578b\u5e94\u8be5\u53ea\u5728\u5305\u542b\u5b83\u7684\u7c7b\u4e2d\u4f7f\u7528\u3002\u6ce8\u610f\uff0c\u56e0\u4e3a\u7c7b\u4e5f\u662f\u4e00\u79cd\u7c7b\u578b\uff0c\u6240\u4ee5\u7c7b\u53ef\u4ee5\u88ab\u5d4c\u5957\u5728\u7c7b\u4e2d\u2014\u2014\u8fd9\u4e2a\u505a\u6cd5\u5e76\u4e0d\u5e38\u89c1\u800c\u4e14\u901a\u5e38\u53ea\u6709\u9ad8\u7ea7\u7a0b\u5e8f\u5458\u624d\u4f1a\u53bb\u4f7f\u7528\u3002 \u5173\u4e8e C++ \u4e2d\u7ed3\u6784\u4f53\u7684\u4e00\u4e9b\u6ce8\u610f\u4e8b\u9879 \u5728C\u8bed\u8a00\u4e2d\uff0c\u7ed3\u6784\u4f53\u53ea\u6709\u6570\u636e\u6210\u5458\uff0c\u6ca1\u6709\u6210\u5458\u51fd\u6570\u3002\u5728c++\u4e2d\uff0c\u5728\u8bbe\u8ba1\u5b8c\u7c7b(\u4f7f\u7528class\u5173\u952e\u5b57)\u540e\uff0cBjarne Stroustrup\u82b1\u4e86\u4e00\u4e9b\u65f6\u95f4\u8003\u8651\u662f\u5426\u5e94\u8be5\u8d4b\u4e88\u7ed3\u6784\u4f53(\u7ee7\u627f\u81eaC)\u5177\u6709\u6210\u5458\u51fd\u6570\u7684\u80fd\u529b\u3002\u7ecf\u8fc7\u8003\u8651\uff0c\u4ed6\u51b3\u5b9a\u7ed3\u6784\u4f53\u5e94\u8be5\u80fd\u591f\u5177\u6709\u6210\u5458\u51fd\u6570\uff0c\u8fd9\u6837\u4e00\u6765\u53ef\u4ee5\u5728\u67d0\u79cd\u7a0b\u5ea6\u4e0a\u4e3a\u7c7b\u548c\u7ed3\u6784\u4f53\u6307\u5b9a\u7edf\u4e00\u7684\u89c4\u5219\u96c6\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u6211\u4eec\u4f7f\u7528class\u5173\u952e\u5b57\u7f16\u5199\u4e86\u4e0a\u9762\u7684\u7a0b\u5e8f\uff0c\u4f46\u5176\u5b9e\u53ef\u4ee5\u4f7f\u7528struct\u5173\u952e\u5b57\u6765\u4ee3\u66ff\u3002 \u8bb8\u591a\u5f00\u53d1\u8005(\u5305\u62ec\u6211\u81ea\u5df1)\u8ba4\u4e3a\u8fd9\u662f\u4e00\u4e2a\u4e0d\u6b63\u786e\u7684\u51b3\u5b9a\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u5bfc\u81f4\u5371\u9669\u7684\u5047\u8bbe\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4e00\u4e2a\u7c7b\u4f1a\u5728\u9500\u6bc1\u540e\u6e05\u7406\u5b83\u81ea\u5df1\u662f\u5408\u7406\u7684(\u4f8b\u5982\uff0c\u4e00\u4e2a\u5206\u914d\u5185\u5b58\u7684\u7c7b\u4f1a\u5728\u88ab\u9500\u6bc1\u4e4b\u524d\u91ca\u653e\u5b83\u6240\u4f7f\u7528\u7684\u5185\u5b58)\uff0c\u4f46\u5047\u8bbe\u4e00\u4e2a\u7ed3\u6784\u4f53\u4e5f\u4f1a\u8fd9\u4e48\u505a\u5219\u662f\u4e0d\u5b89\u5168\u7684\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5efa\u8bae\u5bf9\u53ea\u6709\u6570\u636e\u7684\u7ed3\u6784\u4f53\u4f7f\u7528struct\u5173\u952e\u5b57\uff0c\u800c\u5bf9\u5b9a\u4e49\u9700\u8981\u6570\u636e\u548c\u51fd\u6570\u6346\u7ed1\u5728\u4e00\u8d77\u7684\u5bf9\u8c61\u65f6\u4f7f\u7528class\u5173\u952e\u5b57\u3002 \u6700\u4f73\u5b9e\u8df5 struct \u5173\u952e\u5b57\u53ea\u9002\u7528\u4e8e\u4ec5\u5305\u542b\u6570\u636e\u7684\u7ed3\u6784\u3002\u800c\u5bf9\u4e8e\u540c\u65f6\u5305\u542b\u6570\u636e\u548c\u51fd\u6570\u7684\u5bf9\u8c61\uff0c\u8bf7\u4f7f\u7528class\u5173\u952e\u5b57\u3002 \u5176\u5b9e\u4f60\u5df2\u7ecf\u5728\u4e0d\u77e5\u60c5\u7684\u60c5\u51b5\u4e0b\u591a\u6b21\u4f7f\u7528class\u4e86\u3002 \u5b9e\u9645\u4e0a\uff0cC++\u6807\u51c6\u5e93\u91cc\u5145\u6ee1\u4e86\u5404\u5f0f\u5404\u6837\u7684\u7c7b\uff0c\u5b83\u4eec\u90fd\u53ef\u4ee5\u65b9\u4fbf\u4f60\u5de5\u4f5c\uff0c std::string \u3001 std::vector \u548c std::array \u90fd\u662fclass\uff01\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u521b\u5efa\u8fd9\u4e9b\u7c7b\u578b\u7684\u5bf9\u8c61\u65f6\uff0c\u4f60\u5c31\u5728\u5b9e\u4f8b\u5316\u4e00\u4e2a\u7c7b\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u800c\u5f53\u4f60\u4f7f\u7528\u8fd9\u4e9b\u5bf9\u8c61\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u8c03\u7528\u7684\u90fd\u662f\u5b83\u4eec\u7684\u6210\u5458\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include #include #include int main () { std :: string s { \"Hello, world!\" }; // instantiate a string class object std :: array < int , 3 > a { 1 , 2 , 3 }; // instantiate an array class object std :: vector < double > v { 1.1 , 2.2 , 3.3 }; // instantiate a vector class object std :: cout << \"length: \" << s . length () << '\\n' ; // call a member function return 0 ; } \u5c0f\u7ed3 class\u5173\u952e\u5b57\u5141\u8bb8\u6211\u4eec\u5728C++\u4e2d\u521b\u5efa\u4e00\u4e2a\u65e2\u53ef\u4ee5\u5305\u542b\u6210\u5458\u53d8\u91cf\u53c8\u53ef\u4ee5\u5305\u542b\u6210\u5458\u51fd\u6570\u7684\u81ea\u5b9a\u4e49\u7c7b\u578b\u3002\u7c7b\u6784\u6210\u4e86\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u57fa\u7840\uff0c\u6211\u4eec\u5c06\u7528\u672c\u7ae0\u7684\u5176\u4f59\u90e8\u5206\u548c\u5c06\u6765\u7684\u8bb8\u591a\u7ae0\u8282\u6765\u63a2\u7d22\u7c7b\u4e3a\u6211\u4eec\u63d0\u4f9b\u7684\u4e00\u5207\u80fd\u529b\u3002","title":"13.2 - \u7c7b\u548c\u7c7b\u6210\u5458"},{"location":"13-2-classes-and-class-members/#classes","text":"\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u4e16\u754c\u4e2d\uff0c\u6211\u4eec\u4e0d\u4ec5\u9700\u8981\u81ea\u5b9a\u4e49\u7c7b\u578b\u80fd\u591f\u5305\u542b\u6570\u636e\uff0c\u5b83\u8fd8\u9700\u8981\u63d0\u4f9b\u80fd\u591f\u64cd\u4f5c\u8fd9\u4e9b\u6570\u636e\u7684\u51fd\u6570\uff08\u65b9\u6cd5\uff09\u3002\u5728C++\u4e2d\uff0c\u5b9a\u4e49\u8fd9\u6837\u7684\u6570\u636e\u53ef\u4ee5\u4f7f\u7528 class \u5173\u952e\u5b57\u3002\u8be5\u5173\u952e\u5b57\u53ef\u4ee5\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u88ab\u79f0\u4e3a\u7c7b\uff08class\uff09\u7684\u6570\u636e\u7c7b\u578b\u3002 \u5728 C++ \u4e2d\uff0c\u7c7b\u548c\u7ed3\u6784\u4f53\u672c\u8d28\u4e0a\u662f\u76f8\u540c\u7684\u3002\u5b9e\u9645\u4e0a\uff0c\u4e0b\u9762\u4ee3\u7801\u4e2d\u7684\u7ed3\u6784\u4f53\u548c\u7c7b\u51e0\u4e4e\u6ca1\u6709\u533a\u522b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct DateStruct { int year {}; int month {}; int day {}; }; class DateClass { public : int m_year {}; int m_month {}; int m_day {}; }; \u6ce8\u610f\uff0c\u4e0a\u9762\u4ee3\u7801\u4e2d\u7684\u552f\u4e00\u7684\u4e00\u5904\u660e\u663e\u4e0d\u540c\uff0c\u5c31\u662f\u7c7b\u4e2d\u7684public\u5173\u952e\u5b57\u3002\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u8ba8\u8bba\u8be5\u5173\u952e\u5b57\u7684\u4f5c\u7528\u3002 \u4e0e\u58f0\u660e\u7ed3\u6784\u4f53\u7c7b\u4f3c\uff0c\u7c7b\u7684\u58f0\u660e\u5e76\u4e0d\u4f1a\u5b9e\u9645\u5206\u914d\u4efb\u4f55\u5185\u5b58\uff0c\u5b83\u4ec5\u4ec5\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b\u5e94\u8be5\u201c\u662f\u4ec0\u4e48\u6837\u7684\u201d\u3002 \u6ce8\u610f \u4e0e\u5b9a\u4e49\u7ed3\u6784\u4f53\u7c7b\u4f3c\uff0c\u5b9a\u4e49\u7c7b\u7684\u65f6\u5019\u6700\u5bb9\u6613\u72af\u7684\u4e00\u4e2a\u9519\u8bef\u5c31\u662f\u5fd8\u8bb0\u7ed3\u5c3e\u5904\u7684\u5206\u53f7\u3002\u5fd8\u8bb0\u8fd9\u4e2a\u5206\u53f7\u4f1a\u5bfc\u81f4 \u4e0b\u4e00\u884c\u4ee3\u7801 \u7f16\u8bd1\u62a5\u9519\u3002\u73b0\u4ee3\u7f16\u8bd1\u5668\uff0c\u4f8b\u5982 Visual Studio 2010 \u4f1a\u5728\u4f60\u5fd8\u8bb0\u8be5\u5206\u53f7\u65f6\u63d0\u9192\u4f60\uff0c\u4f46\u662f\u4e00\u4e9b\u8f83\u65e9\u7684\u6216\u8005\u7b80\u5355\u7684\u7f16\u8bd1\u5668\u5e76\u4e0d\u4f1a\u63d0\u9192\u4f60\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u95ee\u9898\u96be\u4ee5\u88ab\u53d1\u73b0\u3002 \u7c7b\uff08\u548c\u7ed3\u6784\u4f53\uff09\u7684\u5b9a\u4e49\u7c7b\u4f3c\u4e8e \u84dd\u56fe \u2014\u2014\u5b83\u4eec\u63cf\u8ff0\u4e86\u4e00\u4e2a\u5bf9\u8c61\u5e94\u8be5\u201c\u662f\u4ec0\u4e48\u6837\u7684\u201d\uff0c\u4f46\u5b9a\u4e49\u5e76\u4e0d\u4f1a\u5b9e\u9645\u521b\u5efa\u5bf9\u8c61\u3002\u4e3a\u4e86\u521b\u5efa\u5bf9\u8c61\uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u8be5\u7c7b\u578b\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\uff1a 1 DateClass today { 2020 , 10 , 14 }; // declare a variable of class DateClass \u5c0f\u8d34\u58eb \u5728\u58f0\u660e\u7c7b\u7684\u65f6\u5019\u5c31\u8981\u5bf9\u5176\u6210\u5458\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u3002","title":"\u7c7b\uff08Classes\uff09"},{"location":"13-2-classes-and-class-members/#_1","text":"\u9664\u4e86\u5b58\u653e\u6570\u636e\u4e4b\u5916\uff0c\u7c7b\uff08\u548c\u7ed3\u6784\u4f53\uff09\u540c\u6837\u8fd8\u53ef\u4ee5\u5305\u542b\u51fd\u6570\uff01\u5b9a\u4e49\u5728\u7c7b\u4e2d\u7684\u51fd\u6570\u79f0\u4e3a \u6210\u5458\u51fd\u6570 \uff08\u6216\u79f0\u4e3a\u65b9\u6cd5\uff09\u3002\u6210\u5458\u51fd\u6570\u7684\u5b9a\u4e49\u53ef\u4ee5\u88ab\u5305\u542b\u5728\u7c7b\u5185\uff0c\u4e5f\u53ef\u4ee5\u5b9a\u4e49\u5728\u7c7b\u5916\u3002\u6211\u4eec\u6682\u65f6\u5c06\u5176\u5b9a\u4e49\u5728\u7c7b\u5185\uff0c\u8fd9\u79cd\u5f62\u5f0f\u76f8\u5bf9\u66f4\u7b80\u5355\uff0c\u7a0d\u540e\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5982\u4f55\u5728\u7c7b\u5916\u5b9a\u4e49\u7c7b\u7684\u65b9\u6cd5\u3002 \u5177\u6709\u6210\u5458\u51fd\u6570\u7684Data\u7c7b\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class DateClass { public : int m_year {}; int m_month {}; int m_day {}; void print () // defines a member function named print() { std :: cout << m_year << '/' << m_month << '/' << m_day ; } }; \u548c\u7ed3\u6784\u4f53\u6210\u5458\u4e00\u6837\uff0c\u7c7b\u7684\u6210\u5458\uff08\u5305\u62ec\u53d8\u91cf\u548c\u51fd\u6570\uff09\u53ef\u4ee5\u901a\u8fc7 \u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26(.) \u6765\u8bbf\u95ee\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include class DateClass { public : int m_year {}; int m_month {}; int m_day {}; void print () { std :: cout << m_year << '/' << m_month << '/' << m_day ; } }; int main () { DateClass today { 2020 , 10 , 14 }; today . m_day = 16 ; // use member selection operator to select a member variable of the class today . print (); // use member selection operator to call a member function of the class return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 2020/10/16 \u89c2\u5bdf\u53ef\u4ee5\u53d1\u73b0\uff0c\u4e0a\u8ff0\u4ee3\u7801\u548c\u4e4b\u524d\u4f7f\u7528\u7ed3\u6784\u4f53\u7684\u7248\u672c\u8fd1\u4e4e\u662f\u4e00\u81f4\u7684\u3002 \u4e0d\u8fc7\uff0c\u5176\u4e2d\u8fd8\u662f\u6709\u51e0\u5904\u4e0d\u540c\u3002\u5728\u4f7f\u7528\u7ed3\u6784\u4f53\u7684\u7248\u672c\u4e2d\uff0c print() \u51fd\u6570\u9700\u8981\u5c06\u7ed3\u6784\u4f53\u672c\u8eab\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\uff0c\u5426\u5219 print() \u51fd\u6570\u5c31\u65e0\u4ece\u5224\u65ad\u6211\u4eec\u8981\u6253\u5370\u7684\u662f\u54ea\u4e2a\u7ed3\u6784\u4f53\u7684\u5185\u5bb9\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u5728\u51fd\u6570\u4e2d\u660e\u786e\u5730\u4f7f\u7528\u8be5\u53c2\u6570\u3002 \u6210\u5458\u51fd\u6570\u7684\u5de5\u4f5c\u65b9\u5f0f\u5219\u7565\u6709\u4e0d\u540c\uff1a\u6240\u6709\u6210\u5458\u51fd\u6570\u7684\u8c03\u7528\u5fc5\u987b\u57fa\u4e8e\u8be5\u7c7b\u7684\u4e00\u4e2a\u5177\u4f53\u5bf9\u8c61\u3002\u5f53\u6211\u4eec\u8c03\u7528 today.print() \u7684\u65f6\u5019\uff0c\u5b9e\u9645\u4e0a\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\u8c03\u7528 today \u5bf9\u8c61\u7684\u6210\u5458\u51fd\u6570 print() \u3002 \u8ba9\u6211\u4eec\u5728\u770b\u4e00\u773c\u6210\u5458\u51fd\u6570 print \u7684\u5b9a\u4e49\uff1a 1 2 3 4 void print () // defines a member function named print() { std :: cout << m_year << '/' << m_month << '/' << m_day ; } m_year \u3001 m_month \u548c m_day \u662f\u8c01\u7684\u53d8\u91cf\uff1f\u5b83\u4eec\u6307\u7684\u662f\u5176\u51fd\u6570\u8c03\u7528\u65f6\u5173\u8054\u7684\u5bf9\u8c61\u6240\u5c5e\u7684\u53d8\u91cf\u3002 \u56e0\u6b64\u5f53\u8c03\u7528 today.print() \u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c06 m_day \u89e3\u6790\u4e3a today.m_day \uff0c\u5c06 m_month \u89e3\u6790\u4e3a today.m_month \uff0c\u5c06 m_year \u89e3\u6790\u4e3a today.m_year \u3002\u5f53\u8c03\u7528 tomorrow.print() \u65f6\uff0c\u5219 m_day \u89e3\u6790\u4e3a tomorrow.m_day \u3002 \u53ef\u89c1\uff0c\u76f8\u5173\u8054\u7684\u5bf9\u8c61\u4f1a\u88ab\u9690\u5f0f\u5730\u4f20\u5165\u6210\u5458\u51fd\u6570\u3002\u56e0\u6b64\uff0c\u5b83\u901a\u5e38\u88ab\u79f0\u4e3a \u9690\u5f0f\u5bf9\u8c61 \u3002 \u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u9690\u5f0f\u5bf9\u8c61\u4f20\u9012\u7684\u5de5\u4f5c\u539f\u7406\u3002 \u5bf9\u4e8e\u975e\u6210\u5458\u51fd\u6570\u6765\u8bf4\uff0c\u6211\u4eec\u5fc5\u987b\u5c06\u5b83\u9700\u8981\u64cd\u4f5c\u7684\u6570\u636e\u4f20\u9012\u7ed9\u51fd\u6570\u3002\u800c\u5bf9\u4e8e\u6210\u5458\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5047\u5b9a\u5b83\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8fd9\u4e2a\u7c7b\u7684\u5bf9\u8c61\u3002 \u4e3a\u6210\u5458\u53d8\u91cf\u6dfb\u52a0 \u201c m_ \u201d \u524d\u7f00\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u533a\u5206\u6210\u5458\u53d8\u91cf\u3001\u51fd\u6570\u53c2\u6570\u548c\u5c40\u90e8\u53d8\u91cf\u3002\u8fd9\u4e48\u505a\u6709\u8bf8\u591a\u597d\u5904\uff1a\u9996\u5148\uff0c\u5f53\u6211\u4eec\u770b\u5230\u5bf9\u5177\u6709 \u201c m_ \u201d\u524d\u7f00\u7684\u53d8\u91cf\u8fdb\u884c\u8d4b\u503c\u65f6\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u6307\u5b9a\u8be5\u64cd\u4f5c\u4f1a\u6539\u53d8\u5f53\u524d\u7c7b\u5b9e\u4f8b\u7684\u72b6\u6001\u3002\u5176\u6b21\uff0c\u51fd\u6570 \u5f62\u53c2 \u6216\u5c40\u90e8\u53d8\u91cf\u88ab\u58f0\u660e\u5728\u5f53\u524d\u51fd\u6570\u4e2d\uff0c\u800c\u6210\u5458\u53d8\u91cf\u5219\u4e0d\u540c\uff0c\u5b83\u88ab\u5b9a\u4e49\u5728\u7c7b\u4e2d\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u77e5\u9053 m_ \u524d\u7f00\u7684\u53d8\u91cf\u7684\u5177\u4f53\u5b9a\u4e49\uff0c\u5c31\u9700\u8981\u53bb\u7c7b\u7684\u5b9a\u4e49\u4e2d\u67e5\u770b\uff0c\u800c\u4e0d\u662f\u5728\u5f53\u524d\u51fd\u6570\u4e2d\u67e5\u770b\u3002 \u6309\u7167\u7ba1\u7406\uff0c\u7c7b\u540d\u901a\u5e38\u662f\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u7c7b\u547d\u540d\u65f6\uff0c\u4f7f\u7528\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u3002 \u518d\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include #include class Employee { public : std :: string m_name {}; int m_id {}; double m_wage {}; // Print employee information to the screen void print () { std :: cout << \"Name: \" << m_name << \" Id: \" << m_id << \" Wage: $\" << m_wage << '\\n' ; } }; int main () { // Declare two employees Employee alex { \"Alex\" , 1 , 25.00 }; Employee joe { \"Joe\" , 2 , 22.25 }; // Print out the employee information alex . print (); joe . print (); return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 Name: Alex Id: 1 Wage: $25 Name: Joe Id: 2 Wage: $22.25 \u5bf9\u4e8e\u975e\u6210\u5458\u51fd\u6570\u6765\u8bf4\uff0c\u5b83\u4e0d\u80fd\u8c03\u7528\u88ab\u5b9a\u4e49\u5728\u5b83\u4e0b\u65b9\uff08\u5373\u540e\u5b9a\u4e49\uff09\u7684\u51fd\u6570\uff0c\u9664\u975e\u5b83\u80fd\u591f\u770b\u5230\u8be5\u51fd\u6570\u7684 \u524d\u5411\u58f0\u660e \uff1a 1 2 3 4 5 6 7 8 void x () { // You can't call y() from here unless the compiler has already seen a forward declaration for y() } void y () { } \u800c\u5bf9\u4e8e\u6210\u5458\u51fd\u6570\u6765\u8bf4\uff0c\u5219\u6ca1\u6709\u8be5\u9650\u5236\uff1a 1 2 3 4 5 6 class foo { public : void x () { y (); } // okay to call y() here, even though y() isn't defined until later in this class void y () { }; };","title":"\u6210\u5458\u51fd\u6570"},{"location":"13-2-classes-and-class-members/#_2","text":"\u9664\u4e86\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u51fd\u6570\uff0c\u7c7b\u8fd8\u53ef\u4ee5\u5177\u6709 \u6210\u5458\u7c7b\u578b \u6216\u5d4c\u5957\u7c7b\u578b\uff08\u4e5f\u5305\u62ec \u7c7b\u578b\u522b\u540d \uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Employee { public : using IDType = int ; std :: string m_name {}; IDType m_id {}; double m_wage {}; // Print employee information to the screen void print () { std :: cout << \"Name: \" << m_name << \" Id: \" << m_id << \" Wage: $\" << m_wage << '\\n' ; } }; \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7c7b\u540d\u5b9e\u9645\u4e0a\u6210\u4e3a\u4e86\u5176\u5d4c\u5957\u7c7b\u578b\u7684 \u547d\u540d\u7a7a\u95f4 \u3002\u5728\u8be5\u7c7b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 IDType \uff0c\u800c\u5728\u7c7b\u5916\uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528 Employee::IDType \u6765\u8bbf\u95ee\u8be5\u7c7b\u578b\u3002 \u5982\u679c\u5728\u540e\u7eed\u7684\u5f00\u53d1\u4e2d\u6211\u4eec\u53d1\u73b0 int \u7c7b\u578b\u5df2\u7ecf\u65e0\u6cd5\u6ee1\u8db3\u9700\u8981\uff0c\u800c\u5fc5\u987b\u6539\u7528 std::string \uff0c\u6b64\u65f6\u6211\u4eec\u53ea\u9700\u8981\u66f4\u65b0\u8fd9\u4e2a\u7c7b\u578b\u522b\u540d\uff0c\u800c\u4e0d\u9700\u8981\u5c06\u6240\u6709\u7684 int \u90fd\u66ff\u6362\u4e3a std::string \u3002 \u5d4c\u5957\u7c7b\u578b\u4e0d\u80fd\u88ab\u524d\u5411\u58f0\u660e\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5d4c\u5957\u7c7b\u578b\u5e94\u8be5\u53ea\u5728\u5305\u542b\u5b83\u7684\u7c7b\u4e2d\u4f7f\u7528\u3002\u6ce8\u610f\uff0c\u56e0\u4e3a\u7c7b\u4e5f\u662f\u4e00\u79cd\u7c7b\u578b\uff0c\u6240\u4ee5\u7c7b\u53ef\u4ee5\u88ab\u5d4c\u5957\u5728\u7c7b\u4e2d\u2014\u2014\u8fd9\u4e2a\u505a\u6cd5\u5e76\u4e0d\u5e38\u89c1\u800c\u4e14\u901a\u5e38\u53ea\u6709\u9ad8\u7ea7\u7a0b\u5e8f\u5458\u624d\u4f1a\u53bb\u4f7f\u7528\u3002","title":"\u6210\u5458\u7c7b\u578b"},{"location":"13-2-classes-and-class-members/#c","text":"\u5728C\u8bed\u8a00\u4e2d\uff0c\u7ed3\u6784\u4f53\u53ea\u6709\u6570\u636e\u6210\u5458\uff0c\u6ca1\u6709\u6210\u5458\u51fd\u6570\u3002\u5728c++\u4e2d\uff0c\u5728\u8bbe\u8ba1\u5b8c\u7c7b(\u4f7f\u7528class\u5173\u952e\u5b57)\u540e\uff0cBjarne Stroustrup\u82b1\u4e86\u4e00\u4e9b\u65f6\u95f4\u8003\u8651\u662f\u5426\u5e94\u8be5\u8d4b\u4e88\u7ed3\u6784\u4f53(\u7ee7\u627f\u81eaC)\u5177\u6709\u6210\u5458\u51fd\u6570\u7684\u80fd\u529b\u3002\u7ecf\u8fc7\u8003\u8651\uff0c\u4ed6\u51b3\u5b9a\u7ed3\u6784\u4f53\u5e94\u8be5\u80fd\u591f\u5177\u6709\u6210\u5458\u51fd\u6570\uff0c\u8fd9\u6837\u4e00\u6765\u53ef\u4ee5\u5728\u67d0\u79cd\u7a0b\u5ea6\u4e0a\u4e3a\u7c7b\u548c\u7ed3\u6784\u4f53\u6307\u5b9a\u7edf\u4e00\u7684\u89c4\u5219\u96c6\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u6211\u4eec\u4f7f\u7528class\u5173\u952e\u5b57\u7f16\u5199\u4e86\u4e0a\u9762\u7684\u7a0b\u5e8f\uff0c\u4f46\u5176\u5b9e\u53ef\u4ee5\u4f7f\u7528struct\u5173\u952e\u5b57\u6765\u4ee3\u66ff\u3002 \u8bb8\u591a\u5f00\u53d1\u8005(\u5305\u62ec\u6211\u81ea\u5df1)\u8ba4\u4e3a\u8fd9\u662f\u4e00\u4e2a\u4e0d\u6b63\u786e\u7684\u51b3\u5b9a\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u5bfc\u81f4\u5371\u9669\u7684\u5047\u8bbe\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4e00\u4e2a\u7c7b\u4f1a\u5728\u9500\u6bc1\u540e\u6e05\u7406\u5b83\u81ea\u5df1\u662f\u5408\u7406\u7684(\u4f8b\u5982\uff0c\u4e00\u4e2a\u5206\u914d\u5185\u5b58\u7684\u7c7b\u4f1a\u5728\u88ab\u9500\u6bc1\u4e4b\u524d\u91ca\u653e\u5b83\u6240\u4f7f\u7528\u7684\u5185\u5b58)\uff0c\u4f46\u5047\u8bbe\u4e00\u4e2a\u7ed3\u6784\u4f53\u4e5f\u4f1a\u8fd9\u4e48\u505a\u5219\u662f\u4e0d\u5b89\u5168\u7684\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5efa\u8bae\u5bf9\u53ea\u6709\u6570\u636e\u7684\u7ed3\u6784\u4f53\u4f7f\u7528struct\u5173\u952e\u5b57\uff0c\u800c\u5bf9\u5b9a\u4e49\u9700\u8981\u6570\u636e\u548c\u51fd\u6570\u6346\u7ed1\u5728\u4e00\u8d77\u7684\u5bf9\u8c61\u65f6\u4f7f\u7528class\u5173\u952e\u5b57\u3002 \u6700\u4f73\u5b9e\u8df5 struct \u5173\u952e\u5b57\u53ea\u9002\u7528\u4e8e\u4ec5\u5305\u542b\u6570\u636e\u7684\u7ed3\u6784\u3002\u800c\u5bf9\u4e8e\u540c\u65f6\u5305\u542b\u6570\u636e\u548c\u51fd\u6570\u7684\u5bf9\u8c61\uff0c\u8bf7\u4f7f\u7528class\u5173\u952e\u5b57\u3002 \u5176\u5b9e\u4f60\u5df2\u7ecf\u5728\u4e0d\u77e5\u60c5\u7684\u60c5\u51b5\u4e0b\u591a\u6b21\u4f7f\u7528class\u4e86\u3002 \u5b9e\u9645\u4e0a\uff0cC++\u6807\u51c6\u5e93\u91cc\u5145\u6ee1\u4e86\u5404\u5f0f\u5404\u6837\u7684\u7c7b\uff0c\u5b83\u4eec\u90fd\u53ef\u4ee5\u65b9\u4fbf\u4f60\u5de5\u4f5c\uff0c std::string \u3001 std::vector \u548c std::array \u90fd\u662fclass\uff01\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u521b\u5efa\u8fd9\u4e9b\u7c7b\u578b\u7684\u5bf9\u8c61\u65f6\uff0c\u4f60\u5c31\u5728\u5b9e\u4f8b\u5316\u4e00\u4e2a\u7c7b\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u800c\u5f53\u4f60\u4f7f\u7528\u8fd9\u4e9b\u5bf9\u8c61\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u8c03\u7528\u7684\u90fd\u662f\u5b83\u4eec\u7684\u6210\u5458\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include #include #include int main () { std :: string s { \"Hello, world!\" }; // instantiate a string class object std :: array < int , 3 > a { 1 , 2 , 3 }; // instantiate an array class object std :: vector < double > v { 1.1 , 2.2 , 3.3 }; // instantiate a vector class object std :: cout << \"length: \" << s . length () << '\\n' ; // call a member function return 0 ; }","title":"\u5173\u4e8e C++ \u4e2d\u7ed3\u6784\u4f53\u7684\u4e00\u4e9b\u6ce8\u610f\u4e8b\u9879"},{"location":"13-2-classes-and-class-members/#_3","text":"class\u5173\u952e\u5b57\u5141\u8bb8\u6211\u4eec\u5728C++\u4e2d\u521b\u5efa\u4e00\u4e2a\u65e2\u53ef\u4ee5\u5305\u542b\u6210\u5458\u53d8\u91cf\u53c8\u53ef\u4ee5\u5305\u542b\u6210\u5458\u51fd\u6570\u7684\u81ea\u5b9a\u4e49\u7c7b\u578b\u3002\u7c7b\u6784\u6210\u4e86\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u57fa\u7840\uff0c\u6211\u4eec\u5c06\u7528\u672c\u7ae0\u7684\u5176\u4f59\u90e8\u5206\u548c\u5c06\u6765\u7684\u8bb8\u591a\u7ae0\u8282\u6765\u63a2\u7d22\u7c7b\u4e3a\u6211\u4eec\u63d0\u4f9b\u7684\u4e00\u5207\u80fd\u529b\u3002","title":"\u5c0f\u7ed3"},{"location":"13-3-public-vs-private-access-specifiers/","tags":["access specifier"],"text":"Key Takeaway \u516c\u6709\u548c\u79c1\u6709\u6210\u5458 \u8003\u8651\u4e0b\u9762\u7684\u7ed3\u6784\u4f53\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 struct DateStruct // \u6210\u5458\u9ed8\u8ba4\u662f\u516c\u6709\u7684 { int month {}; // public by default, can be accessed by anyone int day {}; // public by default, can be accessed by anyone int year {}; // public by default, can be accessed by anyone }; int main () { DateStruct date ; date . month = 10 ; date . day = 14 ; date . year = 2020 ; return 0 ; } \u5728\u4e0a\u9762\u4f8b\u5b50\u7684 main() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a DateStruct \u7ed3\u6784\u4f53\uff0c\u7136\u540e\u76f4\u63a5\u8bbf\u95ee\u5b83\u7684\u6210\u5458\uff0c\u4ee5\u4fbf\u7ed9\u5b83\u4eec\u8d4b\u503c\u3002\u8fd9\u662f\u53ef\u884c\u7684\uff0c\u56e0\u4e3a\u7ed3\u6784\u4f53\u7684\u6240\u6709\u6210\u5458\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u90fd\u662f\u516c\u6709\u6210\u5458\u3002 \u516c\u6709\u6210\u5458 \u662f\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u7684\u7ed3\u6784\u6216\u7c7b\u7684\u6210\u5458\uff0c\u5305\u62ec\u4ece\u7ed3\u6784\u6216\u7c7b\u5916\u90e8\u7684\u4ee3\u7801\u8bbf\u95ee\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570 main() \u4f4d\u4e8e\u7ed3\u6784\u4f53\u5916\u90e8\uff0c\u4f46\u5b83\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u6210\u5458\u6708\u3001\u65e5\u548c\u5e74\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u516c\u6709\u6210\u5458\u3002 \u7ed3\u6784\u4f53\u6216\u8005\u7c7b\u5916\u90e8\u7684\u4ee3\u7801\u6709\u65f6\u79f0\u4e3a public\uff0cpublic\u53ea\u80fd\u591f\u8bbf\u95ee\u7ed3\u6784\u4f53\u6216\u7c7b\u7684\u516c\u6709\u6210\u5458\u3002 \u8bf7\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u51e0\u4e4e\u7c7b\u4f3c\u7684class\u7248\u672c\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class DateClass // members are private by default { int m_month {}; // private by default, can only be accessed by other members int m_day {}; // private by default, can only be accessed by other members int m_year {}; // private by default, can only be accessed by other members }; int main () { DateClass date ; date . m_month = 10 ; // error date . m_day = 14 ; // error date . m_year = 2020 ; // error return 0 ; } \u7f16\u8bd1\u8fd9\u4e2a\u7a0b\u5e8f\u4f1a\u62a5\u9519\u3002\u8fd9\u662f\u56e0\u4e3a\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u7c7b\u7684\u6240\u6709\u6210\u5458\u90fd\u662f\u79c1\u6709\u7684\u3002 \u79c1\u6709\u6210\u5458 \u53ea\u80fd\u88ab\u8be5\u7c7b\u7684\u5176\u4ed6\u6210\u5458\u8bbf\u95ee(\u4e0d\u80fd\u88abpublic\u8bbf\u95ee)\u3002\u56e0\u4e3a main() \u4e0d\u662f DateClass \u7684\u6210\u5458\uff0c\u6240\u4ee5\u5b83\u4e0d\u80fd\u8bbf\u95ee date \u7684\u79c1\u6709\u6210\u5458\u3002 \u8bbf\u95ee\u4fee\u9970\u7b26 \u5c3d\u7ba1\u7c7b\u6210\u5458\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u79c1\u6709\u7684\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528public\u5173\u952e\u5b57\u5c06\u5b83\u4eec\u8bbe\u4e3a\u516c\u6709\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class DateClass { public : // note use of public keyword here, and the colon int m_month {}; // public, can be accessed by anyone int m_day {}; // public, can be accessed by anyone int m_year {}; // public, can be accessed by anyone }; int main () { DateClass date ; date . m_month = 10 ; // okay because m_month is public date . m_day = 14 ; // okay because m_day is public date . m_year = 2020 ; // okay because m_year is public return 0 ; } \u56e0\u4e3a DateClass \u7684\u6210\u5458\u73b0\u5728\u662f\u516c\u6709\u7684\u4e86\uff0c\u56e0\u6b64\u5b83\u4eec\u53ef\u4ee5\u88ab main() \u8bbf\u95ee\u3002 public \u5173\u952e\u5b57\u53ca\u5176\u540e\u9762\u7684\u5206\u53f7\uff0c\u79f0\u4e3a \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \uff0c\u5b83\u53ef\u4ee5\u8bbe\u7f6e\u8c01\u53ef\u4ee5\u8bbf\u95ee\u5bf9\u5e94\u7684\u6210\u5458\u3002\u6bcf\u4e2a\u6210\u5458\u90fd\u53ef\u4ee5\u4ece\u5176\u524d\u9762\u7684\u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26\u83b7\u53d6\u5bf9\u5e94\u7684\u8bbf\u95ee\u7b49\u7ea7\uff08\u5982\u679c\u6ca1\u6709\u5bf9\u5e94\u7684\u4fee\u9970\u7b26\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u7684\u8bbf\u95ee\u4fee\u9970\u7b26\uff09\u3002 C++\u63d0\u4f9b\u4e863\u4e2a\u4e0d\u540c\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u5173\u952e\u5b57\uff1a public \u3001 private \u548c protected \u3002 public \u548c private \u5206\u522b\u7528\u6765\u4f7f\u5b83\u4eec\u540e\u9762\u7684\u6210\u5458\u6210\u4e3a \u516c\u6709\u6210\u5458 \u6216 \u79c1\u6709\u6210\u5458 \u3002\u7b2c\u4e09\u4e2a\u8bbf\u95ee\u8bf4\u660e\u7b26 protected \u7684\u5de5\u4f5c\u539f\u7406\u4e0e private \u975e\u5e38\u76f8\u4f3c\u3002\u6211\u4eec\u5c06\u5728\u8ba8\u8bba\u7ee7\u627f\u65f6\u8ba8\u8bba private \u548c protected \u7684\u533a\u522b\u3002 \u6df7\u5408\u4f7f\u7528\u8bbf\u95ee\u4fee\u9970\u7b26 \u4e00\u4e2a\u7c7b\u53ef\u4ee5(\u800c\u4e14\u51e0\u4e4e\u603b\u662f)\u4f7f\u7528\u591a\u4e2a\u8bbf\u95ee\u8bf4\u660e\u7b26\u6765\u8bbe\u7f6e\u5176\u6210\u5458\u7684\u8bbf\u95ee\u7ea7\u522b\u3002\u4e00\u4e2a\u7c7b\u4e2d\u53ef\u4ee5\u4f7f\u7528\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u7684\u6570\u91cf\u662f\u6ca1\u6709\u9650\u5236\u7684\u3002 \u4e00\u822c\u6765\u8bf4\uff0c\u6210\u5458\u53d8\u91cf\u901a\u5e38\u8bbe\u4e3a\u79c1\u6709\uff0c\u6210\u5458\u51fd\u6570\u901a\u5e38\u8bbe\u4e3a\u516c\u6709\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u4ed4\u7ec6\u7814\u7a76\u5176\u4e2d\u7684\u539f\u56e0\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c06\u6210\u5458\u53d8\u91cf\u8bbe\u4e3a\u79c1\u6709\uff0c\u5c06\u6210\u5458\u51fd\u6570\u8bbe\u4e3a\u516c\u5171\uff0c\u9664\u975e\u60a8\u6709\u5145\u5206\u7684\u7406\u7531\u4e0d\u8fd9\u6837\u505a\u3002 \u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u540c\u65f6\u4f7f\u7528\u79c1\u6709\u548c\u516c\u5171\u8bbf\u95ee\u7684\u7c7b\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include class DateClass // members are private by default { int m_month {}; // private by default, can only be accessed by other members int m_day {}; // private by default, can only be accessed by other members int m_year {}; // private by default, can only be accessed by other members public : void setDate ( int month , int day , int year ) // public, can be accessed by anyone { // setDate() can access the private members of the class because it is a member of the class itself m_month = month ; m_day = day ; m_year = year ; } void print () // public, can be accessed by anyone { std :: cout << m_month << '/' << m_day << '/' << m_year ; } }; int main () { DateClass date ; date . setDate ( 10 , 14 , 2020 ); // okay, because setDate() is public date . print (); // okay, because print() is public std :: cout << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 10/14/2020 \u6ce8\u610f\uff0c\u5c3d\u7ba1\u6211\u4eec\u4e0d\u80fd\u5728 main \u4e2d\u76f4\u63a5\u8bbf\u95ee\u53d8\u91cf m_month \u3001 m_day \u548c m_year \uff08\u56e0\u4e3a\u5b83\u4eec\u662f\u79c1\u6709\u7684\uff09\uff0c\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u516c\u6709\u6210\u5458\u51fd\u6570 setDate() \u548c print() \u95f4\u63a5\u5730\u8bbf\u95ee\u5b83\u4eec\uff01 \u7c7b\u4e2d\u516c\u6709\u6210\u5458\u7684\u96c6\u5408\u901a\u5e38\u79f0\u4e3a\u8be5\u7c7b\u7684\u516c\u5f00\u63a5\u53e3\u3002\u56e0\u4e3a\u53ea\u6709\u516c\u6709\u6210\u5458\u624d\u80fd\u5728\u7c7b\u5916\u88ab\u8bbf\u95ee\uff0c\u6240\u4ee5\u516c\u5f00\u63a5\u53e3\u5b9e\u9645\u4e0a\u5b9a\u4e49\u4e86\u7a0b\u5e8f\u5e94\u8be5\u5982\u4f55\u4f7f\u7528\u63a5\u53e3\u548c\u7c7b\u8fdb\u884c\u4ea4\u4e92\u3002\u6ce8\u610f\uff0c main() \u53ea\u80fd\u591f\u8bbe\u7f6e\u65e5\u671f\u548c\u6253\u5370\u65e5\u671f\u3002\u8be5\u7c7b\u53ef\u4ee5\u4fdd\u62a4\u81ea\u5df1\u7684\u6210\u5458\u88ab\u76f4\u63a5\u8bbf\u95ee\u6216\u4fee\u6539\u3002 \u6709\u7684\u7a0b\u5e8f\u5458\u559c\u6b22\u5c06\u79c1\u6709\u6210\u5458\u5b9a\u4e49\u5728\u516c\u6709\u6210\u5458\u524d\u9762\uff0c\u56e0\u4e3a\u516c\u6709\u6210\u5458\u901a\u5e38\u4f1a\u4f7f\u7528\u79c1\u6709\u6210\u5458\uff0c\u6240\u4ee5\u5c06\u79c1\u6709\u7684\u5b9a\u4e49\u5728\u524d\u662f\u6709\u9053\u7406\u7684\u3002\u4f46\u662f\uff0c\u53cd\u65b9\u7684\u89c2\u70b9\u4e5f\u5f88\u6709\u529b\uff0c\u4ed6\u4eec\u8ba4\u4e3a\u7528\u6237\u6839\u672c\u4e0d\u5173\u5fc3\u7c7b\u7684\u79c1\u6709\u6210\u5458\uff0c\u6240\u4ee5\u5e94\u8be5\u5c06\u516c\u6709\u6210\u5458\u5b9a\u4e49\u5728\u524d\u9762\u3002\u5176\u5b9e\u4e24\u79cd\u65b9\u6cd5\u90fd\u662f\u53ef\u4ee5\u7684\u3002 \u8bbf\u95ee\u63a7\u5236\u5728\u7c7b\u5c42\u9762\u5de5\u4f5c\uff08\u800c\u975e\u5bf9\u8c61\u5c42\u9762\uff09 \u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include class DateClass // members are private by default { int m_month {}; // private by default, can only be accessed by other members int m_day {}; // private by default, can only be accessed by other members int m_year {}; // private by default, can only be accessed by other members public : void setDate ( int month , int day , int year ) { m_month = month ; m_day = day ; m_year = year ; } void print () { std :: cout << m_month << '/' << m_day << '/' << m_year ; } // Note the addition of this function void copyFrom ( const DateClass & d ) { // Note that we can access the private members of d directly m_month = d . m_month ; m_day = d . m_day ; m_year = d . m_year ; } }; int main () { DateClass date ; date . setDate ( 10 , 14 , 2020 ); // okay, because setDate() is public DateClass copy {}; copy . copyFrom ( date ); // okay, because copyFrom() is public copy . print (); std :: cout << '\\n' ; return 0 ; } \u8be5\u4f8b\u4e2d\u5305\u542b\u4e00\u4e2a C++ \u4e2d\u5e38\u88ab\u5ffd\u7565\u6216\u8bef\u89e3\u7684\u7ec6\u8282\uff0c\u8bbf\u95ee\u63a7\u5236\u662f\u5de5\u4f5c\u5728\u7c7b\u7684\u5c42\u9762\uff0c\u800c\u4e0d\u662f\u5bf9\u8c61\u5c42\u9762\u3002 \u8fd9\u5c31\u610f\u5473\u7740\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee \u7c7b\u4e2d\u7684\u67d0\u4e2a\u79c1\u6709\u6210\u5458 \uff0c\u90a3\u4e48\u8be5\u7c7b\u6240\u6709\u5bf9\u8c61\u4e2d\u7684\u8be5\u79c1\u6709\u6210\u5458\u4e5f\u90fd\u80fd\u591f\u88ab\u8be5\u51fd\u6570\u8bbf\u95ee\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c copyFrom() \u662f\u7c7b DateClass \u7684\u6210\u5458\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee DateClass \u7684\u79c1\u6709\u6210\u5458\u3002\u56e0\u6b64\uff0c copyFrom() \u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u8bbf\u95ee\u8c03\u7528\u5b83\u7684\u5bf9\u8c61\uff08 copy \uff09\uff0c\u8fd8\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5b83\u7684\u5f62\u53c2\uff08 DataClass \u7c7b\u578b\u53d8\u91cf d \uff09\u7684\u79c1\u6709\u6210\u5458\u3002\u5982\u679c\u8be5\u5f62\u53c2d\u662f\u5176\u4ed6\u7c7b\u578b\u7684\uff0c\u5219\u5176\u79c1\u6709\u6210\u5458\u4e0d\u53ef\u4ee5\u88ab\u8bbf\u95ee\u3002 \u8fd9\u4e2a\u7279\u6027\u975e\u5e38\u6709\u7528\uff0c\u5c24\u5176\u662f\u5f53\u6211\u4eec\u9700\u8981\u62f7\u8d1d\u4e00\u4e2a\u5bf9\u8c61\u7ed9\u4e0e\u5b83\u540c\u5c5e\u4e00\u4e2a\u7c7b\u7684\u5bf9\u8c61\u65f6\u3002\u5728\u4e0b\u4e00\u7ae0\u4e2d\uff0c\u6211\u4eec\u8fd8\u4f1a\u8ba8\u8bba << \u8fd0\u7b97\u7b26\u7684\u91cd\u8f7d\uff0c\u5230\u65f6\u5019\u6211\u4eec\u8fd8\u4f1a\u91cd\u65b0\u4f53\u79ef\u8fd9\u4e2a\u95ee\u9898\u3002 \u7ed3\u6784\u4f53 vs \u7c7b \u8bbf\u95ee\u8bf4\u660e\u7b26\u4ecb\u7ecd\u5b8c\u6bd5\uff0c\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u8ba8\u8bbaC++\u4e2d\u7684\u7c7b\u548c\u7ed3\u6784\u4e4b\u95f4\u7684\u5b9e\u9645\u533a\u522b\u4e86\u3002\u7c7b\u7684\u6210\u5458\u9ed8\u8ba4\u4e3aprivate\u3002\u7ed3\u6784\u4f53\u7684\u7684\u6210\u5458\u5219\u9ed8\u8ba4\u4e3apublic\u3002 \u5c31\u662f\u8fd9\u6837! (\u597d\u5427\uff0c\u8fc2\u8150\u4e00\u70b9\uff0c\u5176\u5b9e\u8fd8\u6709\u4e00\u4e2a\u5c0f\u533a\u522b\u2014\u2014\u7ed3\u6784\u4f53\u9ed8\u8ba4\u4ece\u5176\u4ed6\u7c7b\u516c\u5f00\u7ee7\u627f\uff0c\u800c\u7c7b\u5219\u662f\u79c1\u6709\u7ee7\u627f\u3002\u6211\u4eec\u5c06\u5728\u4ee5\u540e\u7684\u7ae0\u8282\u4e2d\u8ba8\u8bba\u8fd9\u610f\u5473\u7740\u4ec0\u4e48\uff0c\u4f46\u8fd9\u4e00\u70b9\u5b9e\u9645\u4e0a\u662f\u65e0\u5173\u7d27\u8981\u7684\uff0c\u56e0\u4e3a\u65e0\u8bba\u5982\u4f55\u4f60\u90fd\u4e0d\u5e94\u8be5\u4f9d\u8d56\u5b83\u7684\u9ed8\u8ba4\u884c\u4e3a)\u3002","title":"13.3 - \u8bbf\u95ee\u4fee\u9970\u7b26 public \u548c private"},{"location":"13-3-public-vs-private-access-specifiers/#_1","text":"\u8003\u8651\u4e0b\u9762\u7684\u7ed3\u6784\u4f53\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 struct DateStruct // \u6210\u5458\u9ed8\u8ba4\u662f\u516c\u6709\u7684 { int month {}; // public by default, can be accessed by anyone int day {}; // public by default, can be accessed by anyone int year {}; // public by default, can be accessed by anyone }; int main () { DateStruct date ; date . month = 10 ; date . day = 14 ; date . year = 2020 ; return 0 ; } \u5728\u4e0a\u9762\u4f8b\u5b50\u7684 main() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a DateStruct \u7ed3\u6784\u4f53\uff0c\u7136\u540e\u76f4\u63a5\u8bbf\u95ee\u5b83\u7684\u6210\u5458\uff0c\u4ee5\u4fbf\u7ed9\u5b83\u4eec\u8d4b\u503c\u3002\u8fd9\u662f\u53ef\u884c\u7684\uff0c\u56e0\u4e3a\u7ed3\u6784\u4f53\u7684\u6240\u6709\u6210\u5458\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u90fd\u662f\u516c\u6709\u6210\u5458\u3002 \u516c\u6709\u6210\u5458 \u662f\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u7684\u7ed3\u6784\u6216\u7c7b\u7684\u6210\u5458\uff0c\u5305\u62ec\u4ece\u7ed3\u6784\u6216\u7c7b\u5916\u90e8\u7684\u4ee3\u7801\u8bbf\u95ee\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570 main() \u4f4d\u4e8e\u7ed3\u6784\u4f53\u5916\u90e8\uff0c\u4f46\u5b83\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u6210\u5458\u6708\u3001\u65e5\u548c\u5e74\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u516c\u6709\u6210\u5458\u3002 \u7ed3\u6784\u4f53\u6216\u8005\u7c7b\u5916\u90e8\u7684\u4ee3\u7801\u6709\u65f6\u79f0\u4e3a public\uff0cpublic\u53ea\u80fd\u591f\u8bbf\u95ee\u7ed3\u6784\u4f53\u6216\u7c7b\u7684\u516c\u6709\u6210\u5458\u3002 \u8bf7\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u51e0\u4e4e\u7c7b\u4f3c\u7684class\u7248\u672c\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class DateClass // members are private by default { int m_month {}; // private by default, can only be accessed by other members int m_day {}; // private by default, can only be accessed by other members int m_year {}; // private by default, can only be accessed by other members }; int main () { DateClass date ; date . m_month = 10 ; // error date . m_day = 14 ; // error date . m_year = 2020 ; // error return 0 ; } \u7f16\u8bd1\u8fd9\u4e2a\u7a0b\u5e8f\u4f1a\u62a5\u9519\u3002\u8fd9\u662f\u56e0\u4e3a\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u7c7b\u7684\u6240\u6709\u6210\u5458\u90fd\u662f\u79c1\u6709\u7684\u3002 \u79c1\u6709\u6210\u5458 \u53ea\u80fd\u88ab\u8be5\u7c7b\u7684\u5176\u4ed6\u6210\u5458\u8bbf\u95ee(\u4e0d\u80fd\u88abpublic\u8bbf\u95ee)\u3002\u56e0\u4e3a main() \u4e0d\u662f DateClass \u7684\u6210\u5458\uff0c\u6240\u4ee5\u5b83\u4e0d\u80fd\u8bbf\u95ee date \u7684\u79c1\u6709\u6210\u5458\u3002","title":"\u516c\u6709\u548c\u79c1\u6709\u6210\u5458"},{"location":"13-3-public-vs-private-access-specifiers/#_2","text":"\u5c3d\u7ba1\u7c7b\u6210\u5458\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u79c1\u6709\u7684\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528public\u5173\u952e\u5b57\u5c06\u5b83\u4eec\u8bbe\u4e3a\u516c\u6709\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class DateClass { public : // note use of public keyword here, and the colon int m_month {}; // public, can be accessed by anyone int m_day {}; // public, can be accessed by anyone int m_year {}; // public, can be accessed by anyone }; int main () { DateClass date ; date . m_month = 10 ; // okay because m_month is public date . m_day = 14 ; // okay because m_day is public date . m_year = 2020 ; // okay because m_year is public return 0 ; } \u56e0\u4e3a DateClass \u7684\u6210\u5458\u73b0\u5728\u662f\u516c\u6709\u7684\u4e86\uff0c\u56e0\u6b64\u5b83\u4eec\u53ef\u4ee5\u88ab main() \u8bbf\u95ee\u3002 public \u5173\u952e\u5b57\u53ca\u5176\u540e\u9762\u7684\u5206\u53f7\uff0c\u79f0\u4e3a \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \uff0c\u5b83\u53ef\u4ee5\u8bbe\u7f6e\u8c01\u53ef\u4ee5\u8bbf\u95ee\u5bf9\u5e94\u7684\u6210\u5458\u3002\u6bcf\u4e2a\u6210\u5458\u90fd\u53ef\u4ee5\u4ece\u5176\u524d\u9762\u7684\u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26\u83b7\u53d6\u5bf9\u5e94\u7684\u8bbf\u95ee\u7b49\u7ea7\uff08\u5982\u679c\u6ca1\u6709\u5bf9\u5e94\u7684\u4fee\u9970\u7b26\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u7684\u8bbf\u95ee\u4fee\u9970\u7b26\uff09\u3002 C++\u63d0\u4f9b\u4e863\u4e2a\u4e0d\u540c\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u5173\u952e\u5b57\uff1a public \u3001 private \u548c protected \u3002 public \u548c private \u5206\u522b\u7528\u6765\u4f7f\u5b83\u4eec\u540e\u9762\u7684\u6210\u5458\u6210\u4e3a \u516c\u6709\u6210\u5458 \u6216 \u79c1\u6709\u6210\u5458 \u3002\u7b2c\u4e09\u4e2a\u8bbf\u95ee\u8bf4\u660e\u7b26 protected \u7684\u5de5\u4f5c\u539f\u7406\u4e0e private \u975e\u5e38\u76f8\u4f3c\u3002\u6211\u4eec\u5c06\u5728\u8ba8\u8bba\u7ee7\u627f\u65f6\u8ba8\u8bba private \u548c protected \u7684\u533a\u522b\u3002","title":"\u8bbf\u95ee\u4fee\u9970\u7b26"},{"location":"13-3-public-vs-private-access-specifiers/#_3","text":"\u4e00\u4e2a\u7c7b\u53ef\u4ee5(\u800c\u4e14\u51e0\u4e4e\u603b\u662f)\u4f7f\u7528\u591a\u4e2a\u8bbf\u95ee\u8bf4\u660e\u7b26\u6765\u8bbe\u7f6e\u5176\u6210\u5458\u7684\u8bbf\u95ee\u7ea7\u522b\u3002\u4e00\u4e2a\u7c7b\u4e2d\u53ef\u4ee5\u4f7f\u7528\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u7684\u6570\u91cf\u662f\u6ca1\u6709\u9650\u5236\u7684\u3002 \u4e00\u822c\u6765\u8bf4\uff0c\u6210\u5458\u53d8\u91cf\u901a\u5e38\u8bbe\u4e3a\u79c1\u6709\uff0c\u6210\u5458\u51fd\u6570\u901a\u5e38\u8bbe\u4e3a\u516c\u6709\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u4ed4\u7ec6\u7814\u7a76\u5176\u4e2d\u7684\u539f\u56e0\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c06\u6210\u5458\u53d8\u91cf\u8bbe\u4e3a\u79c1\u6709\uff0c\u5c06\u6210\u5458\u51fd\u6570\u8bbe\u4e3a\u516c\u5171\uff0c\u9664\u975e\u60a8\u6709\u5145\u5206\u7684\u7406\u7531\u4e0d\u8fd9\u6837\u505a\u3002 \u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u540c\u65f6\u4f7f\u7528\u79c1\u6709\u548c\u516c\u5171\u8bbf\u95ee\u7684\u7c7b\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include class DateClass // members are private by default { int m_month {}; // private by default, can only be accessed by other members int m_day {}; // private by default, can only be accessed by other members int m_year {}; // private by default, can only be accessed by other members public : void setDate ( int month , int day , int year ) // public, can be accessed by anyone { // setDate() can access the private members of the class because it is a member of the class itself m_month = month ; m_day = day ; m_year = year ; } void print () // public, can be accessed by anyone { std :: cout << m_month << '/' << m_day << '/' << m_year ; } }; int main () { DateClass date ; date . setDate ( 10 , 14 , 2020 ); // okay, because setDate() is public date . print (); // okay, because print() is public std :: cout << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 10/14/2020 \u6ce8\u610f\uff0c\u5c3d\u7ba1\u6211\u4eec\u4e0d\u80fd\u5728 main \u4e2d\u76f4\u63a5\u8bbf\u95ee\u53d8\u91cf m_month \u3001 m_day \u548c m_year \uff08\u56e0\u4e3a\u5b83\u4eec\u662f\u79c1\u6709\u7684\uff09\uff0c\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u516c\u6709\u6210\u5458\u51fd\u6570 setDate() \u548c print() \u95f4\u63a5\u5730\u8bbf\u95ee\u5b83\u4eec\uff01 \u7c7b\u4e2d\u516c\u6709\u6210\u5458\u7684\u96c6\u5408\u901a\u5e38\u79f0\u4e3a\u8be5\u7c7b\u7684\u516c\u5f00\u63a5\u53e3\u3002\u56e0\u4e3a\u53ea\u6709\u516c\u6709\u6210\u5458\u624d\u80fd\u5728\u7c7b\u5916\u88ab\u8bbf\u95ee\uff0c\u6240\u4ee5\u516c\u5f00\u63a5\u53e3\u5b9e\u9645\u4e0a\u5b9a\u4e49\u4e86\u7a0b\u5e8f\u5e94\u8be5\u5982\u4f55\u4f7f\u7528\u63a5\u53e3\u548c\u7c7b\u8fdb\u884c\u4ea4\u4e92\u3002\u6ce8\u610f\uff0c main() \u53ea\u80fd\u591f\u8bbe\u7f6e\u65e5\u671f\u548c\u6253\u5370\u65e5\u671f\u3002\u8be5\u7c7b\u53ef\u4ee5\u4fdd\u62a4\u81ea\u5df1\u7684\u6210\u5458\u88ab\u76f4\u63a5\u8bbf\u95ee\u6216\u4fee\u6539\u3002 \u6709\u7684\u7a0b\u5e8f\u5458\u559c\u6b22\u5c06\u79c1\u6709\u6210\u5458\u5b9a\u4e49\u5728\u516c\u6709\u6210\u5458\u524d\u9762\uff0c\u56e0\u4e3a\u516c\u6709\u6210\u5458\u901a\u5e38\u4f1a\u4f7f\u7528\u79c1\u6709\u6210\u5458\uff0c\u6240\u4ee5\u5c06\u79c1\u6709\u7684\u5b9a\u4e49\u5728\u524d\u662f\u6709\u9053\u7406\u7684\u3002\u4f46\u662f\uff0c\u53cd\u65b9\u7684\u89c2\u70b9\u4e5f\u5f88\u6709\u529b\uff0c\u4ed6\u4eec\u8ba4\u4e3a\u7528\u6237\u6839\u672c\u4e0d\u5173\u5fc3\u7c7b\u7684\u79c1\u6709\u6210\u5458\uff0c\u6240\u4ee5\u5e94\u8be5\u5c06\u516c\u6709\u6210\u5458\u5b9a\u4e49\u5728\u524d\u9762\u3002\u5176\u5b9e\u4e24\u79cd\u65b9\u6cd5\u90fd\u662f\u53ef\u4ee5\u7684\u3002","title":"\u6df7\u5408\u4f7f\u7528\u8bbf\u95ee\u4fee\u9970\u7b26"},{"location":"13-3-public-vs-private-access-specifiers/#_4","text":"\u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include class DateClass // members are private by default { int m_month {}; // private by default, can only be accessed by other members int m_day {}; // private by default, can only be accessed by other members int m_year {}; // private by default, can only be accessed by other members public : void setDate ( int month , int day , int year ) { m_month = month ; m_day = day ; m_year = year ; } void print () { std :: cout << m_month << '/' << m_day << '/' << m_year ; } // Note the addition of this function void copyFrom ( const DateClass & d ) { // Note that we can access the private members of d directly m_month = d . m_month ; m_day = d . m_day ; m_year = d . m_year ; } }; int main () { DateClass date ; date . setDate ( 10 , 14 , 2020 ); // okay, because setDate() is public DateClass copy {}; copy . copyFrom ( date ); // okay, because copyFrom() is public copy . print (); std :: cout << '\\n' ; return 0 ; } \u8be5\u4f8b\u4e2d\u5305\u542b\u4e00\u4e2a C++ \u4e2d\u5e38\u88ab\u5ffd\u7565\u6216\u8bef\u89e3\u7684\u7ec6\u8282\uff0c\u8bbf\u95ee\u63a7\u5236\u662f\u5de5\u4f5c\u5728\u7c7b\u7684\u5c42\u9762\uff0c\u800c\u4e0d\u662f\u5bf9\u8c61\u5c42\u9762\u3002 \u8fd9\u5c31\u610f\u5473\u7740\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee \u7c7b\u4e2d\u7684\u67d0\u4e2a\u79c1\u6709\u6210\u5458 \uff0c\u90a3\u4e48\u8be5\u7c7b\u6240\u6709\u5bf9\u8c61\u4e2d\u7684\u8be5\u79c1\u6709\u6210\u5458\u4e5f\u90fd\u80fd\u591f\u88ab\u8be5\u51fd\u6570\u8bbf\u95ee\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c copyFrom() \u662f\u7c7b DateClass \u7684\u6210\u5458\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee DateClass \u7684\u79c1\u6709\u6210\u5458\u3002\u56e0\u6b64\uff0c copyFrom() \u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u8bbf\u95ee\u8c03\u7528\u5b83\u7684\u5bf9\u8c61\uff08 copy \uff09\uff0c\u8fd8\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5b83\u7684\u5f62\u53c2\uff08 DataClass \u7c7b\u578b\u53d8\u91cf d \uff09\u7684\u79c1\u6709\u6210\u5458\u3002\u5982\u679c\u8be5\u5f62\u53c2d\u662f\u5176\u4ed6\u7c7b\u578b\u7684\uff0c\u5219\u5176\u79c1\u6709\u6210\u5458\u4e0d\u53ef\u4ee5\u88ab\u8bbf\u95ee\u3002 \u8fd9\u4e2a\u7279\u6027\u975e\u5e38\u6709\u7528\uff0c\u5c24\u5176\u662f\u5f53\u6211\u4eec\u9700\u8981\u62f7\u8d1d\u4e00\u4e2a\u5bf9\u8c61\u7ed9\u4e0e\u5b83\u540c\u5c5e\u4e00\u4e2a\u7c7b\u7684\u5bf9\u8c61\u65f6\u3002\u5728\u4e0b\u4e00\u7ae0\u4e2d\uff0c\u6211\u4eec\u8fd8\u4f1a\u8ba8\u8bba << \u8fd0\u7b97\u7b26\u7684\u91cd\u8f7d\uff0c\u5230\u65f6\u5019\u6211\u4eec\u8fd8\u4f1a\u91cd\u65b0\u4f53\u79ef\u8fd9\u4e2a\u95ee\u9898\u3002","title":"\u8bbf\u95ee\u63a7\u5236\u5728\u7c7b\u5c42\u9762\u5de5\u4f5c\uff08\u800c\u975e\u5bf9\u8c61\u5c42\u9762\uff09"},{"location":"13-3-public-vs-private-access-specifiers/#vs","text":"\u8bbf\u95ee\u8bf4\u660e\u7b26\u4ecb\u7ecd\u5b8c\u6bd5\uff0c\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u8ba8\u8bbaC++\u4e2d\u7684\u7c7b\u548c\u7ed3\u6784\u4e4b\u95f4\u7684\u5b9e\u9645\u533a\u522b\u4e86\u3002\u7c7b\u7684\u6210\u5458\u9ed8\u8ba4\u4e3aprivate\u3002\u7ed3\u6784\u4f53\u7684\u7684\u6210\u5458\u5219\u9ed8\u8ba4\u4e3apublic\u3002 \u5c31\u662f\u8fd9\u6837! (\u597d\u5427\uff0c\u8fc2\u8150\u4e00\u70b9\uff0c\u5176\u5b9e\u8fd8\u6709\u4e00\u4e2a\u5c0f\u533a\u522b\u2014\u2014\u7ed3\u6784\u4f53\u9ed8\u8ba4\u4ece\u5176\u4ed6\u7c7b\u516c\u5f00\u7ee7\u627f\uff0c\u800c\u7c7b\u5219\u662f\u79c1\u6709\u7ee7\u627f\u3002\u6211\u4eec\u5c06\u5728\u4ee5\u540e\u7684\u7ae0\u8282\u4e2d\u8ba8\u8bba\u8fd9\u610f\u5473\u7740\u4ec0\u4e48\uff0c\u4f46\u8fd9\u4e00\u70b9\u5b9e\u9645\u4e0a\u662f\u65e0\u5173\u7d27\u8981\u7684\uff0c\u56e0\u4e3a\u65e0\u8bba\u5982\u4f55\u4f60\u90fd\u4e0d\u5e94\u8be5\u4f9d\u8d56\u5b83\u7684\u9ed8\u8ba4\u884c\u4e3a)\u3002","title":"\u7ed3\u6784\u4f53 vs \u7c7b"},{"location":"13-4-access-functions-and-encapsulation/","tags":["encapsulation"],"text":"Key Takeaway \u4e3a\u4ec0\u4e48\u8981\u5b9a\u4e49\u79c1\u6709\u6210\u5458\u53d8\u91cf \u4e66\u63a5\u4e0a\u6587\uff0c\u6211\u4eec\u63d0\u5230\uff0c\u7c7b\u6210\u5458\u53d8\u91cf\u901a\u5e38\u662f\u79c1\u6709\u7684\u3002\u6b63\u5728\u5b66\u4e60\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u5f00\u53d1\u4eba\u5458\u901a\u5e38\u5f88\u96be\u7406\u89e3\u8fd9\u4e48\u505a\u7684\u76ee\u7684\u3002\u5176\u5b9e\u6253\u4e2a\u6bd4\u65b9\u4f60\u5c31\u80fd\u591f\u660e\u767d\u4e86\u3002 \u751f\u6d3b\u4e2d\u6211\u4eec\u53ef\u4ee5\u63a5\u89e6\u5230\u8bb8\u591a\u7535\u5b50\u8bbe\u5907\u3002\u4f8b\u5982\u7535\u89c6\u9065\u63a7\u5668\uff0c\u4f60\u53ef\u4ee5\u7528\u5b83\u6765\u6253\u5f00/\u5173\u95ed\u7535\u89c6\u3002\u4f60\u53ef\u4ee5\u5f00\u8f66(\u6216\u8e0f\u677f\u8f66)\u53bb\u4e0a\u73ed\uff0c\u7528\u667a\u80fd\u624b\u673a\u62cd\u7167\u3002\u8fd9\u4e09\u79cd\u770b\u4f3c\u4e0d\u76f8\u5173\u7684\u4e1c\u897f\u5374\u6709\u7740\u4e00\u4e2a\u5171\u540c\u7684\u6a21\u5f0f\uff1a\u5b83\u4eec\u4e3a\u4f60\u63d0\u4f9b\u4e00\u4e2a\u7b80\u5355\u7684\u754c\u9762\u4f7f\u4f60\u53ef\u4ee5(\u4e00\u4e2a\u6309\u94ae\u3001\u4e00\u4e2a\u65b9\u5411\u76d8\u7b49\u7b49\u2026\u2026)\u6267\u884c\u4e00\u4e2a\u52a8\u4f5c\u3002\u7136\u800c\uff0c\u8fd9\u4e9b\u8bbe\u5907\u7684\u5b9e\u9645\u64cd\u4f5c\u65b9\u5f0f\u5374\u4e0d\u4e3a\u4f60\u6240\u77e5\u3002\u5f53\u4f60\u6309\u4e0b\u9065\u63a7\u5668\u4e0a\u7684\u6309\u94ae\u65f6\uff0c\u4f60\u4e0d\u9700\u8981\u77e5\u9053\u5b83\u662f\u5982\u4f55\u4e0e\u7535\u89c6\u901a\u4fe1\u7684\u3002\u5f53\u4f60\u8e29\u6cb9\u95e8\u65f6\uff0c\u4f60\u4e5f\u4e0d\u9700\u8981\u77e5\u9053\u5185\u71c3\u673a\u662f\u5982\u4f55\u8ba9\u8f6e\u5b50\u8f6c\u52a8\u7684\u3002\u5f53\u4f60\u62cd\u7167\u65f6\uff0c\u4f60\u4e0d\u9700\u8981\u77e5\u9053\u4f20\u611f\u5668\u662f\u5982\u4f55\u5c06\u5149\u7ebf\u805a\u96c6\u6210\u50cf\u7d20\u56fe\u50cf\u7684\u3002\u8fd9\u79cd\u63a5\u53e3\u548c\u5b9e\u73b0\u7684\u5206\u79bb\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u4e0d\u4e86\u89e3\u7269\u4f53\u5982\u4f55\u5de5\u4f5c\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u7269\u4f53\u3002\u8fd9\u6781\u5927\u5730\u964d\u4f4e\u4e86\u4f7f\u7528\u8fd9\u4e9b\u5bf9\u8c61\u7684\u590d\u6742\u6027\uff0c\u589e\u52a0\u4e86\u6211\u4eec\u80fd\u591f\u4e0e\u4e4b\u4ea4\u4e92\u7684\u5bf9\u8c61\u6570\u91cf\u3002 \u57fa\u4e8e\u540c\u6837\u7684\u539f\u56e0\uff0c\u5c06\u5b9e\u73b0\u548c\u63a5\u53e3\u5206\u79bb\u5728\u8f6f\u4ef6\u5de5\u7a0b\u4e2d\u4e5f\u662f\u975e\u5e38\u6709\u7528\u7684\u3002 \u5c01\u88c5 \u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c \u5c01\u88c5 \uff08\u6709\u65f6\u4e5f\u79f0\u4e3a\u4fe1\u606f\u5c4f\u853d\uff09\u662f\u6307\u5c06\u5bf9\u8c61\u7684\u5b9e\u73b0\u7ec6\u8282\u5bf9\u7528\u6237\u9690\u85cf\u8d77\u6765\u7684\u8fc7\u7a0b\u3002\u7528\u6237\u5728\u4f7f\u7528\u5bf9\u8c61\u7684\u65f6\u5019\u53ea\u9700\u8981\u901a\u8fc7\u516c\u5f00\u63a5\u53e3\u8bbf\u95ee\u5bf9\u8c61\u5373\u53ef\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u4e0d\u4e86\u89e3\u5bf9\u8c61\u5b9e\u73b0\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u8be5\u5bf9\u8c61\u3002 \u5728 C++ \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u6765\u5b9e\u73b0\u5c01\u88c5\u3002\u901a\u5e38\uff0c\u7c7b\u7684\u6240\u6709\u6210\u5458\u53d8\u91cf\u90fd\u662f\u79c1\u6709\u7684(\u9690\u85cf\u5b9e\u73b0\u7ec6\u8282)\uff0c\u800c\u5927\u591a\u6570\u6210\u5458\u51fd\u6570\u90fd\u662f\u516c\u6709\u7684(\u66b4\u9732\u63a5\u53e3)\u3002\u867d\u7136\u8981\u6c42\u7c7b\u7684\u7528\u6237\u4f7f\u7528\u516c\u5f00\u63a5\u53e3\u4e0e\u7c7b\u4ea4\u4e92\u6bd4\u76f4\u63a5\u63d0\u4f9b\u5bf9\u6210\u5458\u53d8\u91cf\u7684\u516c\u5171\u8bbf\u95ee\u66f4\u7e41\u7410\uff0c\u4f46\u8fd9\u6837\u505a\u5b9e\u9645\u4e0a\u76ca\u5904\u9887\u591a\uff0c\u6709\u52a9\u4e8e\u63d0\u9ad8\u7c7b\u7684\u53ef\u91cd\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002 \u6ce8\u610f\uff1a\u5c01\u88c5\u4e00\u8bcd\u6709\u65f6\u5019\u4e5f\u6307\u4ee3\u5bf9\u6570\u636e\u548c\u51fd\u6570\u7684\u6253\u5305\u3002\u8fd9\u91cc\u6211\u4eec\u53ea\u53d6\u5176\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\u7684\u542b\u4e49\u3002 \u597d\u5904 1 \uff1a\u7c7b\u5c01\u88c5\u53ef\u4ee5\u63d0\u9ad8\u5176\u6613\u7528\u6027\u5e76\u964d\u4f4e\u7a0b\u5e8f\u590d\u6742\u5ea6 \u5bf9\u4e8e\u4e00\u4e2a\u826f\u597d\u5c01\u88c5\u7684\u7c7b\u6765\u8bf4\uff0c\u4f60\u53ea\u9700\u5173\u5fc3\u8be5\u7c7b\u4e3a\u4f60\u63d0\u4f9b\u4e86\u54ea\u4e9b\u516c\u5171\u63a5\u53e3\uff0c\u8fd9\u4e9b\u63a5\u53e3\u9700\u8981\u4ec0\u4e48\u53c2\u6570\uff0c\u5b83\u4eec\u7684\u8fd4\u56de\u503c\u662f\u4ec0\u4e48\u3002\u800c\u8fd9\u4e2a\u7c7b\u7684\u5177\u4f53\u5185\u90e8\u5b9e\u73b0\u5219\u5b8c\u5168\u4e0d\u9700\u8981\u641e\u6e05\u695a\u3002\u4f8b\u5982\uff0c\u7c7b\u4e2d\u4fdd\u5b58\u4e86\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u540d\u5b57\u7684\u5217\u8868\uff0c\u800c\u8fd9\u4e2a\u5217\u8868\u7684\u5b9e\u73b0\u53ef\u4ee5\u662f\u5305\u542bC\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u7684\u52a8\u6001\u6570\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u662f std::array \u3001 std::vector \u3001 std::map \u3001 std::list \u6216\u8005\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u3002\u5728\u4f7f\u7528\u8be5\u7c7b\u7684\u5bf9\u8c61\u65f6\uff0c\u4e0d\u9700\u8981\u4e5f\u4e0d\u5173\u5fc3\u5b83\u4f7f\u7528\u4e86\u54ea\u4e2a\u6570\u636e\u7ed3\u6784\u6765\u4fdd\u5b58\u540d\u5b57\u3002\u8fd9\u65e0\u7591\u6781\u5927\u7a0b\u5ea6\u5730\u964d\u4f4e\u4e86\u4f60\u7684\u7a0b\u5e8f\u7684\u590d\u6742\u5ea6\uff0c\u4e5f\u5c31\u51cf\u5c11\u4e86\u72af\u9519\u8bef\u7684\u673a\u4f1a\u3002\u4e0e\u5176\u4ed6\u7684\u597d\u5904\u76f8\u6bd4\uff0c\u8fd9\u662f\u5c01\u88c5\u5e26\u6765\u7684\u6700\u5927\u597d\u5904\u3002 C++ \u6807\u51c6\u5e93\u4e2d\u7684\u6240\u6709\u7c7b\u90fd\u662f\u5c01\u88c5\u8fc7\u7684\u3002\u4f60\u53ef\u4ee5\u60f3\u8c61\u4e00\u4e0b\uff0c\u5982\u679c\u4f60\u9700\u8981\u4e86\u89e3 std::string , std::vector \u6216 std::cout \u7684\u5b9e\u73b0\u624d\u80fd\u4f7f\u7528\u5b83\u4eec\u7684\u8bdd\uff0c\u5c06\u662f\u591a\u4e48\u53ef\u6015\u7684\u4e00\u4ef6\u4e8b\uff01 \u597d\u5904 2 \uff1a\u7c7b\u5c01\u88c5\u53ef\u4ee5\u4fdd\u62a4\u5176\u6570\u636e\u4e0d\u88ab\u6ee5\u7528 \u5168\u5c40\u53d8\u91cf\u662f\u5371\u9669\u7684\uff0c\u56e0\u4e3a\u4f60\u4e0d\u80fd\u4e25\u683c\u63a7\u5236\u8c01\u53ef\u4ee5\u8bbf\u95ee\u5168\u5c40\u53d8\u91cf\uff0c\u6216\u8005\u4ed6\u4eec\u5e94\u8be5\u5982\u4f55\u4f7f\u7528\u5b83\u3002 \u516c\u6709\u6210\u5458 \u4e5f\u6709\u540c\u6837\u7684\u95ee\u9898\uff0c\u53ea\u662f\u7a0b\u5ea6\u7a0d\u8f7b\u800c\u5df2\u3002 \u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b\u3002\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u5f00\u59cb: 1 2 3 4 5 class MyString { char * m_string ; // we'll dynamically allocate our string here int m_length ; // we need to keep track of the string length }; \u8fd9\u4e24\u4e2a\u53d8\u91cf\u5b58\u5728\u5185\u5728\u7684\u8054\u7cfb: m_length \u5e94\u8be5\u603b\u662f\u7b49\u4e8e m_string \u6301\u6709\u7684\u5b57\u7b26\u4e32\u7684\u957f\u5ea6(\u8fd9\u79cd\u8054\u7cfb\u88ab\u79f0\u4e3a\u4e0d\u53d8\u91cf)\u3002\u5982\u679c m_length \u662fpublic \u7684\uff0c\u90a3\u4e48\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u5728\u4e0d\u6539\u53d8 m_string \u7684\u60c5\u51b5\u4e0b\u6539\u53d8\u5b57\u7b26\u4e32\u7684\u957f\u5ea6(\u53cd\u4e4b\u4ea6\u7136)\u3002\u8fd9\u5c06\u4f7f\u7c7b\u5904\u4e8e\u4e0d\u4e00\u81f4\u7684\u72b6\u6001\uff0c\u4ece\u800c\u53ef\u80fd\u5bfc\u81f4\u5404\u79cd\u5947\u602a\u7684\u95ee\u9898\u3002\u901a\u8fc7\u4f7f m_length \u548c m_string \u6210\u4e3a private \uff0c\u7528\u6237\u88ab\u8feb\u4f7f\u7528\u4efb\u4f55\u53ef\u7528\u7684\u516c\u5171\u6210\u5458\u51fd\u6570\u4e0e\u8be5\u7c7b\u8fdb\u884c\u4ea4\u4e92(\u5e76\u4e14\u8fd9\u4e9b\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u786e\u4fdd m_length \u548c m_string \u603b\u662f\u88ab\u9002\u5f53\u8bbe\u7f6e)\u3002 \u6211\u4eec\u8fd8\u53ef\u4ee5\u9632\u6b62\u7528\u6237\u5728\u4f7f\u7528\u6211\u4eec\u7684\u7c7b\u65f6\u72af\u9519\u8bef\u3002\u8003\u8651\u4e00\u4e2a\u5177\u6709\u516c\u5171\u6570\u7ec4\u6210\u5458\u53d8\u91cf\u7684\u7c7b: 1 2 3 4 5 class IntArray { public : int m_array [ 10 ]; }; \u5982\u679c\u7528\u6237\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u6570\u7ec4\uff0c\u4ed6\u4eec\u53ef\u80fd\u4f1a\u4f7f\u7528\u65e0\u6548\u7684\u7d22\u5f15\u4e0b\u6807\u6570\u7ec4\uff0c\u4ece\u800c\u5f15\u53d1\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\uff1a 1 2 3 4 5 int main () { IntArray array ; array . m_array [ 16 ] = 2 ; // invalid array index, now we overwrote memory that we don't own } \u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u5c06\u6570\u7ec4\u8bbe\u4e3a\u79c1\u6709\uff0c\u5219\u53ef\u4ee5\u5f3a\u5236\u8c03\u7528\u4e00\u4e2a\u9a8c\u8bc1\u7d22\u5f15\u662f\u5426\u6709\u6548\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // For std::size() class IntArray { private : int m_array [ 10 ]; // user can not access this directly any more public : void setValue ( int index , int value ) { // If the index is invalid, do nothing if ( index < 0 || index >= std :: size ( m_array )) return ; m_array [ index ] = value ; } }; \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4fdd\u62a4\u7a0b\u5e8f\u7684\u5b8c\u6574\u6027\u3002\u987a\u5e26\u4e00\u63d0\uff0c std::array \u548c std::vector \u7684 at() \u51fd\u6570\u6240\u505a\u7684\u4e8b\u60c5\u548c\u4e0a\u9762\u7684\u51fd\u6570\u975e\u5e38\u7c7b\u4f3c\uff01 \u597d\u5904 3 \uff1a\u5c01\u88c5\u8fc7\u7684\u7c7b\u66f4\u5bb9\u6613\u4fee\u6539 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7b80\u5355\u7684\u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include class Something { public : int m_value1 ; int m_value2 ; int m_value3 ; }; int main () { Something something ; something . m_value1 = 5 ; std :: cout << something . m_value1 << '\\n' ; } \u867d\u7136\u8fd9\u4e2a\u7a0b\u5e8f\u5de5\u4f5c\u5f97\u5f88\u597d\uff0c\u4f46\u5982\u679c\u7a0d\u540e\u6211\u4eec\u51b3\u5b9a\u91cd\u547d\u540d m_value1 \u6216\u6539\u53d8\u5b83\u7684\u7c7b\u578b\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u6211\u4eec\u4e0d\u4ec5\u7834\u574f\u4e86\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u800c\u4e14\u53ef\u80fd\u7834\u574f\u4e86\u5927\u591a\u6570\u4f7f\u7528 Something \u7c7b\u7684\u7a0b\u5e8f\uff01 \u5c01\u88c5\u4f7f\u6211\u4eec\u80fd\u591f\u5728\u4e0d\u7834\u574f\u6240\u6709\u4f7f\u7528\u7c7b\u7684\u7a0b\u5e8f\u7684\u60c5\u51b5\u4e0b\u6539\u53d8\u7c7b\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002 \u4e0b\u9762\u662f\u8fd9\u4e2a\u7c7b\u7684\u5c01\u88c5\u7248\u672c\uff0c\u5b83\u4f7f\u7528\u51fd\u6570\u8bbf\u95ee m_value1 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include class Something { private : int m_value1 ; int m_value2 ; int m_value3 ; public : void setValue1 ( int value ) { m_value1 = value ; } int getValue1 () { return m_value1 ; } }; int main () { Something something ; something . setValue1 ( 5 ); std :: cout << something . getValue1 () << '\\n' ; } \u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u6539\u53d8\u7c7b\u7684\u5b9e\u73b0\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include class Something { private : int m_value [ 3 ]; // note: we changed the implementation of this class! public : // We have to update any member functions to reflect the new implementation void setValue1 ( int value ) { m_value [ 0 ] = value ; } int getValue1 () { return m_value [ 0 ]; } }; int main () { // But our program still works just fine! Something something ; something . setValue1 ( 5 ); std :: cout << something . getValue1 () << '\\n' ; } \u8bf7\u6ce8\u610f\uff0c\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u66f4\u6539\u7c7b\u7684\u516c\u5171\u63a5\u53e3\u4e2d\u7684\u4efb\u4f55\u51fd\u6570\u7b7e\u540d(\u8fd4\u56de\u7c7b\u578b\u3001\u51fd\u6570\u540d\u6216\u5f62\u53c2)\uff0c\u6240\u4ee5\u4f7f\u7528\u8be5\u7c7b\u7684\u7528\u6237\u4e0d\u4f1a\u611f\u77e5\u5230\u4efb\u4f55\u6539\u53d8\u3002 \u540c\u6837\u5730\uff0c\u5982\u679c\u6709\u4eba\u5728\u665a\u4e0a\u6f5c\u5165\u4f60\u7684\u623f\u5b50\uff0c\u7528\u4e0d\u540c\u7684(\u4f46\u517c\u5bb9\u7684)\u6280\u672f\u66ff\u6362\u4e86\u4f60\u7684\u7535\u89c6\u9065\u63a7\u5668\u7684\u5185\u90e8\uff0c\u4f60\u5728\u4f7f\u7528\u65f6\u53ef\u80fd\u5b8c\u5168\u4e0d\u4f1a\u6ce8\u610f\u5230\uff01 \u597d\u5904 4\uff1a\u5c01\u88c5\u7684\u7c7b\u66f4\u5bb9\u6613\u67e5\u627e\u9519\u8bef \u6700\u540e\uff0c\u5c01\u88c5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u51fa\u73b0\u9519\u8bef\u65f6\u8c03\u8bd5\u7a0b\u5e8f\u3002\u901a\u5e38\uff0c\u5f53\u4e00\u4e2a\u7a0b\u5e8f\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u65f6\uff0c\u5176\u539f\u56e0\u53ef\u80fd\u662f\u56e0\u4e3a\u6211\u4eec\u7684\u4e00\u4e2a\u6210\u5458\u53d8\u91cf\u7684\u503c\u4e0d\u6b63\u786e\u3002\u5982\u679c\u6bcf\u4e2a\u4eba\u90fd\u80fd\u591f\u76f4\u63a5\u8bbf\u95ee\u53d8\u91cf\uff0c\u90a3\u4e48\u8ddf\u8e2a\u54ea\u6bb5\u4ee3\u7801\u4fee\u6539\u4e86\u53d8\u91cf\u5c31\u4f1a\u5f88\u56f0\u96be(\u53ef\u80fd\u662f\u7a0b\u5e8f\u4e2d\u7684\u4efb\u4f55\u4e00\u6bb5\u4ee3\u7801\uff0c\u4f60\u9700\u8981\u5c06\u5b83\u4eec\u5168\u90e8\u5206\u89e3\u4ee5\u627e\u51fa\u662f\u54ea\u4e00\u884c)\u3002\u4f46\u662f\uff0c\u5982\u679c\u6bcf\u4e2a\u4eba\u90fd\u5fc5\u987b\u8c03\u7528\u540c\u4e00\u4e2a\u516c\u5171\u51fd\u6570\u6765\u4fee\u6539\u4e00\u4e2a\u503c\uff0c\u90a3\u4e48\u4f60\u5c31\u53ef\u4ee5\u5c06\u8be5\u51fd\u6570\u8bbe\u4e3a\u65ad\u70b9\uff0c\u5e76\u89c2\u5bdf\u6bcf\u4e2a\u8c03\u7528\u8005\u66f4\u6539\u503c\u7684\u8fc7\u7a0b\uff0c\u76f4\u5230\u53d1\u73b0\u54ea\u91cc\u51fa\u4e86\u95ee\u9898\u3002 \u6210\u5458\u8bbf\u95ee\u51fd\u6570 \u5bf9\u4e8e\u67d0\u4e9b\u7c7b\u6765\u8bf4\uff08\u53d6\u51b3\u4e8e\u8fd9\u4e2a\u7c7b\u8981\u505a\u4ec0\u4e48\uff09\uff0c\u6709\u65f6\u5019\u6211\u4eec\u4f1a\u5e0c\u671b\u80fd\u591f\u76f4\u63a5\u8bfb\u53d6\u6216\u8bbe\u7f6e\u67d0\u4e2a \u79c1\u6709\u6210\u5458 \u53d8\u91cf\u7684\u503c\u3002 \u6210\u5458\u8bbf\u95ee\u51fd\u6570 \u662f\u4e00\u7c7b\u7528\u4e8e\u83b7\u53d6\u6216\u6539\u53d8\u7c7b\u79c1\u6709\u6210\u5458\u53d8\u91cf\u503c\u7684\u7cbe\u7b80\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e String \u7c7b\uff0c\u6210\u5458\u51fd\u6570\u770b\u4e0a\u53bb\u53ef\u80fd\u4f1a\u50cf\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 class MyString { private : char * m_string ; // we'll dynamically allocate our string here int m_length ; // we need to keep track of the string length public : int getLength () { return m_length ; } // access function to get value of m_length }; getLength() \u5c31\u662f\u4e0a\u4e00\u4e2a\u6210\u5458\u8bbf\u95ee\u51fd\u6570\uff0c\u5b83\u8fd4\u56de m_length \u7684\u503c\u3002 \u6210\u5458\u8bbf\u95ee\u51fd\u6570\u901a\u5e38\u6709\u4e24\u79cd\uff1a getters \u548c setters \u3002 Getters (\u6709\u65f6\u4e5f\u79f0\u4e3aaccessor) \u7528\u4e8e\u8fd4\u56de\u67d0\u4e2a\u79c1\u6709\u6210\u5458\u53d8\u91cf\u7684\u503c\u3002 Setters (\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a mutators ) \u5219\u7528\u4e8e\u4e3a\u79c1\u6709\u6210\u5458\u53d8\u91cf\u8bbe\u7f6e\u503c\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u7c7b\uff0c\u5b83\u7684\u6240\u6709\u6210\u5458\u90fd\u5177\u6709 getter \u548c setter \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Date { private : int m_month ; int m_day ; int m_year ; public : int getMonth () { return m_month ; } // getter for month void setMonth ( int month ) { m_month = month ; } // setter for month int getDay () { return m_day ; } // getter for day void setDay ( int day ) { m_day = day ; } // setter for day int getYear () { return m_year ; } // getter for year void setYear ( int year ) { m_year = year ; } // setter for year }; Date \u7c7b\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u5c01\u88c5\u8fc7\u7684\u7ed3\u6784\uff0c\u4f7f\u7528\u8005\u6709\u7406\u7531\u8ba4\u4e3a\u5b83\u4eec\u80fd\u591f\u8bfb\u53d6\u548c\u8bbe\u7f6e\u7c7b\u4e2d\u7684\u5e74\u6708\u65e5\u53d8\u91cf\u3002 MyString \u7c7b\u4e0d\u4ec5\u5305\u542b\u6570\u636e\u2014\u2014\u5b83\u6709\u66f4\u590d\u6742\u7684\u529f\u80fd\uff0c\u5e76\u4e14\u6709\u4e00\u4e2a\u9700\u8981\u7ef4\u62a4\u7684 \u4e0d\u53d8\u91cf \u3002\u6211\u4eec\u6ca1\u6709\u4e3a\u53d8\u91cf m_length \u63d0\u4f9b setter\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u5e0c\u671b\u7528\u6237\u80fd\u591f\u76f4\u63a5\u8bbe\u7f6e\u5b57\u7b26\u4e32\u7684(\u957f\u5ea6\u5e94\u8be5\u53ea\u5728\u5b57\u7b26\u4e32\u53d1\u751f\u66f4\u6539\u65f6\u8bbe\u7f6e)\u3002\u5728\u8fd9\u4e2a\u7c7b\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u76f4\u63a5\u83b7\u53d6\u5b57\u7b26\u4e32\u957f\u5ea6\u662f\u6709\u610f\u4e49\u7684\uff0c\u56e0\u6b64\u63d0\u4f9b\u4e86\u83b7\u53d6\u957f\u5ea6\u503c\u7684getter\u3002 getter\u5e94\u8be5\u63d0\u4f9b\u5bf9\u6570\u636e\u7684\u201c\u53ea\u8bfb\u201d\u8bbf\u95ee\u3002\u56e0\u6b64\uff0c\u6700\u4f73\u5b9e\u8df5\u662f\u5b83\u4eec\u5e94\u8be5\u901a\u8fc7\u503c\u6216const\u5f15\u7528(\u800c\u4e0d\u662f\u901a\u8fc7\u975econst\u5f15\u7528)\u8fd4\u56de\u3002\u8fd4\u56de\u975econst\u5f15\u7528\u7684getter\u5c06\u5141\u8bb8\u8c03\u7528\u8005\u4fee\u6539\u88ab\u5f15\u7528\u7684\u5b9e\u9645\u5bf9\u8c61\uff0c\u8fd9\u8fdd\u53cd\u4e86getter\u7684\u53ea\u8bfb\u7279\u6027(\u5e76\u8fdd\u53cd\u4e86\u5c01\u88c5)\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5f53getter\u8fd4\u56de\u4e00\u4e2a\u975econst\u5f15\u7528\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include class Foo { private : int m_value { 4 }; public : int & getValue () { return m_value ; } // returns a non-const reference }; int main () { Foo f ; // f.m_value is initialized to 4 f . getValue () = 5 ; // use the non-const reference to assign value 5 to m_value std :: cout << f . getValue (); // prints 5 return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 5 \u56e0\u4e3a getValue() \u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u975econst\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8be5\u5f15\u7528\u76f4\u63a5\u4fee\u6539\u88ab\u5176\u5f15\u7528\u7684\u5bf9\u8c61\uff08 m_value \uff09\uff01 \u6700\u4f73\u5b9e\u8df5 getter \u5e94\u8be5 \u6309\u503c\u8fd4\u56de \u6216\u8fd4\u56deconst\u5f15\u7528\u3002 \u6210\u5458\u8bbf\u95ee\u51fd\u6570\u7684\u76f8\u5173\u95ee\u9898 \u5728\u54ea\u4e9b\u60c5\u51b5\u4e0b\u5e94\u8be5\u4f7f\u7528\u8bbf\u95ee\u51fd\u6570\uff0c\u54ea\u4e9b\u60c5\u51b5\u4e0b\u5e94\u8be5\u907f\u514d\u8bbf\u95ee\u51fd\u6570\uff0c\u5bf9\u6b64\u6709\u76f8\u5f53\u591a\u7684\u8ba8\u8bba\u3002\u5c3d\u7ba1\u8bbf\u95ee\u51fd\u6570\u4e0d\u8fdd\u53cd\u5c01\u88c5\u539f\u5219\uff0c\u4f46\u4e00\u4e9b\u5f00\u53d1\u4eba\u5458\u8ba4\u4e3a\u4f7f\u7528\u8bbf\u95ee\u51fd\u6570\u4e0d\u662f\u826f\u597d\u7684OOP\u7c7b\u8bbe\u8ba1(\u8fd9\u4e2a\u4e3b\u9898\u53ef\u4ee5\u5199\u4e00\u672c\u4e66\u6765\u8ba8\u8bba)\u3002 \u5f53\u4e0b\uff0c\u6211\u4eec\u5c06\u63a8\u8350\u4e00\u79cd\u5b9e\u7528\u7684\u65b9\u6cd5\u3002\u5728\u521b\u5efa\u7c7b\u65f6\uff0c\u8bf7\u8003\u8651\u4ee5\u4e0b\u51e0\u70b9\uff1a \u5982\u679c\u4e0d\u9700\u8981\u4ece\u7c7b\u7684\u5916\u90e8\u8bbf\u95ee\u6210\u5458\uff0c\u4e0d\u8981\u4e3a\u8be5\u6210\u5458\u63d0\u4f9b\u8bbf\u95ee\u51fd\u6570\uff1b \u5982\u679c\u9700\u8981\u4ece\u5916\u90e8\u8bbf\u95ee\u6210\u5458\uff0c\u8003\u8651\u662f\u5426\u53ef\u4ee5\u901a\u8fc7\u67d0\u79cd\u884c\u4e3a\u6216\u52a8\u4f5c\u6765\u5b8c\u6210\uff08\u4f8b\u5982\uff0c\u4e0e\u5176\u63d0\u4f9b setAlive(bool) setter\uff0c\u4e0d\u5982\u5b9e\u73b0\u4e00\u4e2a kill() \u51fd\u6570\uff09 \u5982\u679c\u53ef\u4ee5\uff0c\u4ec5\u8003\u8651\u63d0\u4f9b\u4e00\u4e2agetter \u5c0f\u7ed3 \u6b63\u5982\u60a8\u6240\u770b\u5230\u7684\uff0c\u5b9e\u73b0\u5c01\u88c5\u5e76\u4e0d\u4f1a\u5f88\u9ebb\u70e6\uff0c\u5176\u5e26\u6765\u7684\u597d\u5904\u5374\u975e\u5e38\u7684\u591a\u3002\u5c01\u88c5\u7684\u4e3b\u8981\u597d\u5904\u662f\u5141\u8bb8\u7528\u6237\u5728\u4e0d\u4e86\u89e3\u7c7b\u7684\u5185\u90e8\u5b9e\u73b0\u7684\u60c5\u51b5\u4e0b\uff0c\u8f7b\u677e\u5730\u4f7f\u7528\u8be5\u7c7b\u3002\u00f7","title":"13.4 - \u6210\u5458\u8bbf\u95ee\u51fd\u6570\u4e0e\u5c01\u88c5"},{"location":"13-4-access-functions-and-encapsulation/#_1","text":"\u4e66\u63a5\u4e0a\u6587\uff0c\u6211\u4eec\u63d0\u5230\uff0c\u7c7b\u6210\u5458\u53d8\u91cf\u901a\u5e38\u662f\u79c1\u6709\u7684\u3002\u6b63\u5728\u5b66\u4e60\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u5f00\u53d1\u4eba\u5458\u901a\u5e38\u5f88\u96be\u7406\u89e3\u8fd9\u4e48\u505a\u7684\u76ee\u7684\u3002\u5176\u5b9e\u6253\u4e2a\u6bd4\u65b9\u4f60\u5c31\u80fd\u591f\u660e\u767d\u4e86\u3002 \u751f\u6d3b\u4e2d\u6211\u4eec\u53ef\u4ee5\u63a5\u89e6\u5230\u8bb8\u591a\u7535\u5b50\u8bbe\u5907\u3002\u4f8b\u5982\u7535\u89c6\u9065\u63a7\u5668\uff0c\u4f60\u53ef\u4ee5\u7528\u5b83\u6765\u6253\u5f00/\u5173\u95ed\u7535\u89c6\u3002\u4f60\u53ef\u4ee5\u5f00\u8f66(\u6216\u8e0f\u677f\u8f66)\u53bb\u4e0a\u73ed\uff0c\u7528\u667a\u80fd\u624b\u673a\u62cd\u7167\u3002\u8fd9\u4e09\u79cd\u770b\u4f3c\u4e0d\u76f8\u5173\u7684\u4e1c\u897f\u5374\u6709\u7740\u4e00\u4e2a\u5171\u540c\u7684\u6a21\u5f0f\uff1a\u5b83\u4eec\u4e3a\u4f60\u63d0\u4f9b\u4e00\u4e2a\u7b80\u5355\u7684\u754c\u9762\u4f7f\u4f60\u53ef\u4ee5(\u4e00\u4e2a\u6309\u94ae\u3001\u4e00\u4e2a\u65b9\u5411\u76d8\u7b49\u7b49\u2026\u2026)\u6267\u884c\u4e00\u4e2a\u52a8\u4f5c\u3002\u7136\u800c\uff0c\u8fd9\u4e9b\u8bbe\u5907\u7684\u5b9e\u9645\u64cd\u4f5c\u65b9\u5f0f\u5374\u4e0d\u4e3a\u4f60\u6240\u77e5\u3002\u5f53\u4f60\u6309\u4e0b\u9065\u63a7\u5668\u4e0a\u7684\u6309\u94ae\u65f6\uff0c\u4f60\u4e0d\u9700\u8981\u77e5\u9053\u5b83\u662f\u5982\u4f55\u4e0e\u7535\u89c6\u901a\u4fe1\u7684\u3002\u5f53\u4f60\u8e29\u6cb9\u95e8\u65f6\uff0c\u4f60\u4e5f\u4e0d\u9700\u8981\u77e5\u9053\u5185\u71c3\u673a\u662f\u5982\u4f55\u8ba9\u8f6e\u5b50\u8f6c\u52a8\u7684\u3002\u5f53\u4f60\u62cd\u7167\u65f6\uff0c\u4f60\u4e0d\u9700\u8981\u77e5\u9053\u4f20\u611f\u5668\u662f\u5982\u4f55\u5c06\u5149\u7ebf\u805a\u96c6\u6210\u50cf\u7d20\u56fe\u50cf\u7684\u3002\u8fd9\u79cd\u63a5\u53e3\u548c\u5b9e\u73b0\u7684\u5206\u79bb\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u4e0d\u4e86\u89e3\u7269\u4f53\u5982\u4f55\u5de5\u4f5c\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u7269\u4f53\u3002\u8fd9\u6781\u5927\u5730\u964d\u4f4e\u4e86\u4f7f\u7528\u8fd9\u4e9b\u5bf9\u8c61\u7684\u590d\u6742\u6027\uff0c\u589e\u52a0\u4e86\u6211\u4eec\u80fd\u591f\u4e0e\u4e4b\u4ea4\u4e92\u7684\u5bf9\u8c61\u6570\u91cf\u3002 \u57fa\u4e8e\u540c\u6837\u7684\u539f\u56e0\uff0c\u5c06\u5b9e\u73b0\u548c\u63a5\u53e3\u5206\u79bb\u5728\u8f6f\u4ef6\u5de5\u7a0b\u4e2d\u4e5f\u662f\u975e\u5e38\u6709\u7528\u7684\u3002","title":"\u4e3a\u4ec0\u4e48\u8981\u5b9a\u4e49\u79c1\u6709\u6210\u5458\u53d8\u91cf"},{"location":"13-4-access-functions-and-encapsulation/#_2","text":"\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c \u5c01\u88c5 \uff08\u6709\u65f6\u4e5f\u79f0\u4e3a\u4fe1\u606f\u5c4f\u853d\uff09\u662f\u6307\u5c06\u5bf9\u8c61\u7684\u5b9e\u73b0\u7ec6\u8282\u5bf9\u7528\u6237\u9690\u85cf\u8d77\u6765\u7684\u8fc7\u7a0b\u3002\u7528\u6237\u5728\u4f7f\u7528\u5bf9\u8c61\u7684\u65f6\u5019\u53ea\u9700\u8981\u901a\u8fc7\u516c\u5f00\u63a5\u53e3\u8bbf\u95ee\u5bf9\u8c61\u5373\u53ef\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u4e0d\u4e86\u89e3\u5bf9\u8c61\u5b9e\u73b0\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u8be5\u5bf9\u8c61\u3002 \u5728 C++ \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u6765\u5b9e\u73b0\u5c01\u88c5\u3002\u901a\u5e38\uff0c\u7c7b\u7684\u6240\u6709\u6210\u5458\u53d8\u91cf\u90fd\u662f\u79c1\u6709\u7684(\u9690\u85cf\u5b9e\u73b0\u7ec6\u8282)\uff0c\u800c\u5927\u591a\u6570\u6210\u5458\u51fd\u6570\u90fd\u662f\u516c\u6709\u7684(\u66b4\u9732\u63a5\u53e3)\u3002\u867d\u7136\u8981\u6c42\u7c7b\u7684\u7528\u6237\u4f7f\u7528\u516c\u5f00\u63a5\u53e3\u4e0e\u7c7b\u4ea4\u4e92\u6bd4\u76f4\u63a5\u63d0\u4f9b\u5bf9\u6210\u5458\u53d8\u91cf\u7684\u516c\u5171\u8bbf\u95ee\u66f4\u7e41\u7410\uff0c\u4f46\u8fd9\u6837\u505a\u5b9e\u9645\u4e0a\u76ca\u5904\u9887\u591a\uff0c\u6709\u52a9\u4e8e\u63d0\u9ad8\u7c7b\u7684\u53ef\u91cd\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002 \u6ce8\u610f\uff1a\u5c01\u88c5\u4e00\u8bcd\u6709\u65f6\u5019\u4e5f\u6307\u4ee3\u5bf9\u6570\u636e\u548c\u51fd\u6570\u7684\u6253\u5305\u3002\u8fd9\u91cc\u6211\u4eec\u53ea\u53d6\u5176\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\u7684\u542b\u4e49\u3002","title":"\u5c01\u88c5"},{"location":"13-4-access-functions-and-encapsulation/#1","text":"\u5bf9\u4e8e\u4e00\u4e2a\u826f\u597d\u5c01\u88c5\u7684\u7c7b\u6765\u8bf4\uff0c\u4f60\u53ea\u9700\u5173\u5fc3\u8be5\u7c7b\u4e3a\u4f60\u63d0\u4f9b\u4e86\u54ea\u4e9b\u516c\u5171\u63a5\u53e3\uff0c\u8fd9\u4e9b\u63a5\u53e3\u9700\u8981\u4ec0\u4e48\u53c2\u6570\uff0c\u5b83\u4eec\u7684\u8fd4\u56de\u503c\u662f\u4ec0\u4e48\u3002\u800c\u8fd9\u4e2a\u7c7b\u7684\u5177\u4f53\u5185\u90e8\u5b9e\u73b0\u5219\u5b8c\u5168\u4e0d\u9700\u8981\u641e\u6e05\u695a\u3002\u4f8b\u5982\uff0c\u7c7b\u4e2d\u4fdd\u5b58\u4e86\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u540d\u5b57\u7684\u5217\u8868\uff0c\u800c\u8fd9\u4e2a\u5217\u8868\u7684\u5b9e\u73b0\u53ef\u4ee5\u662f\u5305\u542bC\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u7684\u52a8\u6001\u6570\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u662f std::array \u3001 std::vector \u3001 std::map \u3001 std::list \u6216\u8005\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u3002\u5728\u4f7f\u7528\u8be5\u7c7b\u7684\u5bf9\u8c61\u65f6\uff0c\u4e0d\u9700\u8981\u4e5f\u4e0d\u5173\u5fc3\u5b83\u4f7f\u7528\u4e86\u54ea\u4e2a\u6570\u636e\u7ed3\u6784\u6765\u4fdd\u5b58\u540d\u5b57\u3002\u8fd9\u65e0\u7591\u6781\u5927\u7a0b\u5ea6\u5730\u964d\u4f4e\u4e86\u4f60\u7684\u7a0b\u5e8f\u7684\u590d\u6742\u5ea6\uff0c\u4e5f\u5c31\u51cf\u5c11\u4e86\u72af\u9519\u8bef\u7684\u673a\u4f1a\u3002\u4e0e\u5176\u4ed6\u7684\u597d\u5904\u76f8\u6bd4\uff0c\u8fd9\u662f\u5c01\u88c5\u5e26\u6765\u7684\u6700\u5927\u597d\u5904\u3002 C++ \u6807\u51c6\u5e93\u4e2d\u7684\u6240\u6709\u7c7b\u90fd\u662f\u5c01\u88c5\u8fc7\u7684\u3002\u4f60\u53ef\u4ee5\u60f3\u8c61\u4e00\u4e0b\uff0c\u5982\u679c\u4f60\u9700\u8981\u4e86\u89e3 std::string , std::vector \u6216 std::cout \u7684\u5b9e\u73b0\u624d\u80fd\u4f7f\u7528\u5b83\u4eec\u7684\u8bdd\uff0c\u5c06\u662f\u591a\u4e48\u53ef\u6015\u7684\u4e00\u4ef6\u4e8b\uff01","title":"\u597d\u5904 1 \uff1a\u7c7b\u5c01\u88c5\u53ef\u4ee5\u63d0\u9ad8\u5176\u6613\u7528\u6027\u5e76\u964d\u4f4e\u7a0b\u5e8f\u590d\u6742\u5ea6"},{"location":"13-4-access-functions-and-encapsulation/#2","text":"\u5168\u5c40\u53d8\u91cf\u662f\u5371\u9669\u7684\uff0c\u56e0\u4e3a\u4f60\u4e0d\u80fd\u4e25\u683c\u63a7\u5236\u8c01\u53ef\u4ee5\u8bbf\u95ee\u5168\u5c40\u53d8\u91cf\uff0c\u6216\u8005\u4ed6\u4eec\u5e94\u8be5\u5982\u4f55\u4f7f\u7528\u5b83\u3002 \u516c\u6709\u6210\u5458 \u4e5f\u6709\u540c\u6837\u7684\u95ee\u9898\uff0c\u53ea\u662f\u7a0b\u5ea6\u7a0d\u8f7b\u800c\u5df2\u3002 \u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b\u3002\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u5f00\u59cb: 1 2 3 4 5 class MyString { char * m_string ; // we'll dynamically allocate our string here int m_length ; // we need to keep track of the string length }; \u8fd9\u4e24\u4e2a\u53d8\u91cf\u5b58\u5728\u5185\u5728\u7684\u8054\u7cfb: m_length \u5e94\u8be5\u603b\u662f\u7b49\u4e8e m_string \u6301\u6709\u7684\u5b57\u7b26\u4e32\u7684\u957f\u5ea6(\u8fd9\u79cd\u8054\u7cfb\u88ab\u79f0\u4e3a\u4e0d\u53d8\u91cf)\u3002\u5982\u679c m_length \u662fpublic \u7684\uff0c\u90a3\u4e48\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u5728\u4e0d\u6539\u53d8 m_string \u7684\u60c5\u51b5\u4e0b\u6539\u53d8\u5b57\u7b26\u4e32\u7684\u957f\u5ea6(\u53cd\u4e4b\u4ea6\u7136)\u3002\u8fd9\u5c06\u4f7f\u7c7b\u5904\u4e8e\u4e0d\u4e00\u81f4\u7684\u72b6\u6001\uff0c\u4ece\u800c\u53ef\u80fd\u5bfc\u81f4\u5404\u79cd\u5947\u602a\u7684\u95ee\u9898\u3002\u901a\u8fc7\u4f7f m_length \u548c m_string \u6210\u4e3a private \uff0c\u7528\u6237\u88ab\u8feb\u4f7f\u7528\u4efb\u4f55\u53ef\u7528\u7684\u516c\u5171\u6210\u5458\u51fd\u6570\u4e0e\u8be5\u7c7b\u8fdb\u884c\u4ea4\u4e92(\u5e76\u4e14\u8fd9\u4e9b\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u786e\u4fdd m_length \u548c m_string \u603b\u662f\u88ab\u9002\u5f53\u8bbe\u7f6e)\u3002 \u6211\u4eec\u8fd8\u53ef\u4ee5\u9632\u6b62\u7528\u6237\u5728\u4f7f\u7528\u6211\u4eec\u7684\u7c7b\u65f6\u72af\u9519\u8bef\u3002\u8003\u8651\u4e00\u4e2a\u5177\u6709\u516c\u5171\u6570\u7ec4\u6210\u5458\u53d8\u91cf\u7684\u7c7b: 1 2 3 4 5 class IntArray { public : int m_array [ 10 ]; }; \u5982\u679c\u7528\u6237\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u6570\u7ec4\uff0c\u4ed6\u4eec\u53ef\u80fd\u4f1a\u4f7f\u7528\u65e0\u6548\u7684\u7d22\u5f15\u4e0b\u6807\u6570\u7ec4\uff0c\u4ece\u800c\u5f15\u53d1\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\uff1a 1 2 3 4 5 int main () { IntArray array ; array . m_array [ 16 ] = 2 ; // invalid array index, now we overwrote memory that we don't own } \u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u5c06\u6570\u7ec4\u8bbe\u4e3a\u79c1\u6709\uff0c\u5219\u53ef\u4ee5\u5f3a\u5236\u8c03\u7528\u4e00\u4e2a\u9a8c\u8bc1\u7d22\u5f15\u662f\u5426\u6709\u6548\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // For std::size() class IntArray { private : int m_array [ 10 ]; // user can not access this directly any more public : void setValue ( int index , int value ) { // If the index is invalid, do nothing if ( index < 0 || index >= std :: size ( m_array )) return ; m_array [ index ] = value ; } }; \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4fdd\u62a4\u7a0b\u5e8f\u7684\u5b8c\u6574\u6027\u3002\u987a\u5e26\u4e00\u63d0\uff0c std::array \u548c std::vector \u7684 at() \u51fd\u6570\u6240\u505a\u7684\u4e8b\u60c5\u548c\u4e0a\u9762\u7684\u51fd\u6570\u975e\u5e38\u7c7b\u4f3c\uff01","title":"\u597d\u5904 2 \uff1a\u7c7b\u5c01\u88c5\u53ef\u4ee5\u4fdd\u62a4\u5176\u6570\u636e\u4e0d\u88ab\u6ee5\u7528"},{"location":"13-4-access-functions-and-encapsulation/#3","text":"\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7b80\u5355\u7684\u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include class Something { public : int m_value1 ; int m_value2 ; int m_value3 ; }; int main () { Something something ; something . m_value1 = 5 ; std :: cout << something . m_value1 << '\\n' ; } \u867d\u7136\u8fd9\u4e2a\u7a0b\u5e8f\u5de5\u4f5c\u5f97\u5f88\u597d\uff0c\u4f46\u5982\u679c\u7a0d\u540e\u6211\u4eec\u51b3\u5b9a\u91cd\u547d\u540d m_value1 \u6216\u6539\u53d8\u5b83\u7684\u7c7b\u578b\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u6211\u4eec\u4e0d\u4ec5\u7834\u574f\u4e86\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u800c\u4e14\u53ef\u80fd\u7834\u574f\u4e86\u5927\u591a\u6570\u4f7f\u7528 Something \u7c7b\u7684\u7a0b\u5e8f\uff01 \u5c01\u88c5\u4f7f\u6211\u4eec\u80fd\u591f\u5728\u4e0d\u7834\u574f\u6240\u6709\u4f7f\u7528\u7c7b\u7684\u7a0b\u5e8f\u7684\u60c5\u51b5\u4e0b\u6539\u53d8\u7c7b\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002 \u4e0b\u9762\u662f\u8fd9\u4e2a\u7c7b\u7684\u5c01\u88c5\u7248\u672c\uff0c\u5b83\u4f7f\u7528\u51fd\u6570\u8bbf\u95ee m_value1 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include class Something { private : int m_value1 ; int m_value2 ; int m_value3 ; public : void setValue1 ( int value ) { m_value1 = value ; } int getValue1 () { return m_value1 ; } }; int main () { Something something ; something . setValue1 ( 5 ); std :: cout << something . getValue1 () << '\\n' ; } \u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u6539\u53d8\u7c7b\u7684\u5b9e\u73b0\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include class Something { private : int m_value [ 3 ]; // note: we changed the implementation of this class! public : // We have to update any member functions to reflect the new implementation void setValue1 ( int value ) { m_value [ 0 ] = value ; } int getValue1 () { return m_value [ 0 ]; } }; int main () { // But our program still works just fine! Something something ; something . setValue1 ( 5 ); std :: cout << something . getValue1 () << '\\n' ; } \u8bf7\u6ce8\u610f\uff0c\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u66f4\u6539\u7c7b\u7684\u516c\u5171\u63a5\u53e3\u4e2d\u7684\u4efb\u4f55\u51fd\u6570\u7b7e\u540d(\u8fd4\u56de\u7c7b\u578b\u3001\u51fd\u6570\u540d\u6216\u5f62\u53c2)\uff0c\u6240\u4ee5\u4f7f\u7528\u8be5\u7c7b\u7684\u7528\u6237\u4e0d\u4f1a\u611f\u77e5\u5230\u4efb\u4f55\u6539\u53d8\u3002 \u540c\u6837\u5730\uff0c\u5982\u679c\u6709\u4eba\u5728\u665a\u4e0a\u6f5c\u5165\u4f60\u7684\u623f\u5b50\uff0c\u7528\u4e0d\u540c\u7684(\u4f46\u517c\u5bb9\u7684)\u6280\u672f\u66ff\u6362\u4e86\u4f60\u7684\u7535\u89c6\u9065\u63a7\u5668\u7684\u5185\u90e8\uff0c\u4f60\u5728\u4f7f\u7528\u65f6\u53ef\u80fd\u5b8c\u5168\u4e0d\u4f1a\u6ce8\u610f\u5230\uff01","title":"\u597d\u5904 3 \uff1a\u5c01\u88c5\u8fc7\u7684\u7c7b\u66f4\u5bb9\u6613\u4fee\u6539"},{"location":"13-4-access-functions-and-encapsulation/#4","text":"\u6700\u540e\uff0c\u5c01\u88c5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u51fa\u73b0\u9519\u8bef\u65f6\u8c03\u8bd5\u7a0b\u5e8f\u3002\u901a\u5e38\uff0c\u5f53\u4e00\u4e2a\u7a0b\u5e8f\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u65f6\uff0c\u5176\u539f\u56e0\u53ef\u80fd\u662f\u56e0\u4e3a\u6211\u4eec\u7684\u4e00\u4e2a\u6210\u5458\u53d8\u91cf\u7684\u503c\u4e0d\u6b63\u786e\u3002\u5982\u679c\u6bcf\u4e2a\u4eba\u90fd\u80fd\u591f\u76f4\u63a5\u8bbf\u95ee\u53d8\u91cf\uff0c\u90a3\u4e48\u8ddf\u8e2a\u54ea\u6bb5\u4ee3\u7801\u4fee\u6539\u4e86\u53d8\u91cf\u5c31\u4f1a\u5f88\u56f0\u96be(\u53ef\u80fd\u662f\u7a0b\u5e8f\u4e2d\u7684\u4efb\u4f55\u4e00\u6bb5\u4ee3\u7801\uff0c\u4f60\u9700\u8981\u5c06\u5b83\u4eec\u5168\u90e8\u5206\u89e3\u4ee5\u627e\u51fa\u662f\u54ea\u4e00\u884c)\u3002\u4f46\u662f\uff0c\u5982\u679c\u6bcf\u4e2a\u4eba\u90fd\u5fc5\u987b\u8c03\u7528\u540c\u4e00\u4e2a\u516c\u5171\u51fd\u6570\u6765\u4fee\u6539\u4e00\u4e2a\u503c\uff0c\u90a3\u4e48\u4f60\u5c31\u53ef\u4ee5\u5c06\u8be5\u51fd\u6570\u8bbe\u4e3a\u65ad\u70b9\uff0c\u5e76\u89c2\u5bdf\u6bcf\u4e2a\u8c03\u7528\u8005\u66f4\u6539\u503c\u7684\u8fc7\u7a0b\uff0c\u76f4\u5230\u53d1\u73b0\u54ea\u91cc\u51fa\u4e86\u95ee\u9898\u3002","title":"\u597d\u5904 4\uff1a\u5c01\u88c5\u7684\u7c7b\u66f4\u5bb9\u6613\u67e5\u627e\u9519\u8bef"},{"location":"13-4-access-functions-and-encapsulation/#_3","text":"\u5bf9\u4e8e\u67d0\u4e9b\u7c7b\u6765\u8bf4\uff08\u53d6\u51b3\u4e8e\u8fd9\u4e2a\u7c7b\u8981\u505a\u4ec0\u4e48\uff09\uff0c\u6709\u65f6\u5019\u6211\u4eec\u4f1a\u5e0c\u671b\u80fd\u591f\u76f4\u63a5\u8bfb\u53d6\u6216\u8bbe\u7f6e\u67d0\u4e2a \u79c1\u6709\u6210\u5458 \u53d8\u91cf\u7684\u503c\u3002 \u6210\u5458\u8bbf\u95ee\u51fd\u6570 \u662f\u4e00\u7c7b\u7528\u4e8e\u83b7\u53d6\u6216\u6539\u53d8\u7c7b\u79c1\u6709\u6210\u5458\u53d8\u91cf\u503c\u7684\u7cbe\u7b80\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e String \u7c7b\uff0c\u6210\u5458\u51fd\u6570\u770b\u4e0a\u53bb\u53ef\u80fd\u4f1a\u50cf\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 class MyString { private : char * m_string ; // we'll dynamically allocate our string here int m_length ; // we need to keep track of the string length public : int getLength () { return m_length ; } // access function to get value of m_length }; getLength() \u5c31\u662f\u4e0a\u4e00\u4e2a\u6210\u5458\u8bbf\u95ee\u51fd\u6570\uff0c\u5b83\u8fd4\u56de m_length \u7684\u503c\u3002 \u6210\u5458\u8bbf\u95ee\u51fd\u6570\u901a\u5e38\u6709\u4e24\u79cd\uff1a getters \u548c setters \u3002 Getters (\u6709\u65f6\u4e5f\u79f0\u4e3aaccessor) \u7528\u4e8e\u8fd4\u56de\u67d0\u4e2a\u79c1\u6709\u6210\u5458\u53d8\u91cf\u7684\u503c\u3002 Setters (\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a mutators ) \u5219\u7528\u4e8e\u4e3a\u79c1\u6709\u6210\u5458\u53d8\u91cf\u8bbe\u7f6e\u503c\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u7c7b\uff0c\u5b83\u7684\u6240\u6709\u6210\u5458\u90fd\u5177\u6709 getter \u548c setter \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Date { private : int m_month ; int m_day ; int m_year ; public : int getMonth () { return m_month ; } // getter for month void setMonth ( int month ) { m_month = month ; } // setter for month int getDay () { return m_day ; } // getter for day void setDay ( int day ) { m_day = day ; } // setter for day int getYear () { return m_year ; } // getter for year void setYear ( int year ) { m_year = year ; } // setter for year }; Date \u7c7b\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u5c01\u88c5\u8fc7\u7684\u7ed3\u6784\uff0c\u4f7f\u7528\u8005\u6709\u7406\u7531\u8ba4\u4e3a\u5b83\u4eec\u80fd\u591f\u8bfb\u53d6\u548c\u8bbe\u7f6e\u7c7b\u4e2d\u7684\u5e74\u6708\u65e5\u53d8\u91cf\u3002 MyString \u7c7b\u4e0d\u4ec5\u5305\u542b\u6570\u636e\u2014\u2014\u5b83\u6709\u66f4\u590d\u6742\u7684\u529f\u80fd\uff0c\u5e76\u4e14\u6709\u4e00\u4e2a\u9700\u8981\u7ef4\u62a4\u7684 \u4e0d\u53d8\u91cf \u3002\u6211\u4eec\u6ca1\u6709\u4e3a\u53d8\u91cf m_length \u63d0\u4f9b setter\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u5e0c\u671b\u7528\u6237\u80fd\u591f\u76f4\u63a5\u8bbe\u7f6e\u5b57\u7b26\u4e32\u7684(\u957f\u5ea6\u5e94\u8be5\u53ea\u5728\u5b57\u7b26\u4e32\u53d1\u751f\u66f4\u6539\u65f6\u8bbe\u7f6e)\u3002\u5728\u8fd9\u4e2a\u7c7b\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u76f4\u63a5\u83b7\u53d6\u5b57\u7b26\u4e32\u957f\u5ea6\u662f\u6709\u610f\u4e49\u7684\uff0c\u56e0\u6b64\u63d0\u4f9b\u4e86\u83b7\u53d6\u957f\u5ea6\u503c\u7684getter\u3002 getter\u5e94\u8be5\u63d0\u4f9b\u5bf9\u6570\u636e\u7684\u201c\u53ea\u8bfb\u201d\u8bbf\u95ee\u3002\u56e0\u6b64\uff0c\u6700\u4f73\u5b9e\u8df5\u662f\u5b83\u4eec\u5e94\u8be5\u901a\u8fc7\u503c\u6216const\u5f15\u7528(\u800c\u4e0d\u662f\u901a\u8fc7\u975econst\u5f15\u7528)\u8fd4\u56de\u3002\u8fd4\u56de\u975econst\u5f15\u7528\u7684getter\u5c06\u5141\u8bb8\u8c03\u7528\u8005\u4fee\u6539\u88ab\u5f15\u7528\u7684\u5b9e\u9645\u5bf9\u8c61\uff0c\u8fd9\u8fdd\u53cd\u4e86getter\u7684\u53ea\u8bfb\u7279\u6027(\u5e76\u8fdd\u53cd\u4e86\u5c01\u88c5)\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5f53getter\u8fd4\u56de\u4e00\u4e2a\u975econst\u5f15\u7528\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include class Foo { private : int m_value { 4 }; public : int & getValue () { return m_value ; } // returns a non-const reference }; int main () { Foo f ; // f.m_value is initialized to 4 f . getValue () = 5 ; // use the non-const reference to assign value 5 to m_value std :: cout << f . getValue (); // prints 5 return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 5 \u56e0\u4e3a getValue() \u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u975econst\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8be5\u5f15\u7528\u76f4\u63a5\u4fee\u6539\u88ab\u5176\u5f15\u7528\u7684\u5bf9\u8c61\uff08 m_value \uff09\uff01 \u6700\u4f73\u5b9e\u8df5 getter \u5e94\u8be5 \u6309\u503c\u8fd4\u56de \u6216\u8fd4\u56deconst\u5f15\u7528\u3002","title":"\u6210\u5458\u8bbf\u95ee\u51fd\u6570"},{"location":"13-4-access-functions-and-encapsulation/#_4","text":"\u5728\u54ea\u4e9b\u60c5\u51b5\u4e0b\u5e94\u8be5\u4f7f\u7528\u8bbf\u95ee\u51fd\u6570\uff0c\u54ea\u4e9b\u60c5\u51b5\u4e0b\u5e94\u8be5\u907f\u514d\u8bbf\u95ee\u51fd\u6570\uff0c\u5bf9\u6b64\u6709\u76f8\u5f53\u591a\u7684\u8ba8\u8bba\u3002\u5c3d\u7ba1\u8bbf\u95ee\u51fd\u6570\u4e0d\u8fdd\u53cd\u5c01\u88c5\u539f\u5219\uff0c\u4f46\u4e00\u4e9b\u5f00\u53d1\u4eba\u5458\u8ba4\u4e3a\u4f7f\u7528\u8bbf\u95ee\u51fd\u6570\u4e0d\u662f\u826f\u597d\u7684OOP\u7c7b\u8bbe\u8ba1(\u8fd9\u4e2a\u4e3b\u9898\u53ef\u4ee5\u5199\u4e00\u672c\u4e66\u6765\u8ba8\u8bba)\u3002 \u5f53\u4e0b\uff0c\u6211\u4eec\u5c06\u63a8\u8350\u4e00\u79cd\u5b9e\u7528\u7684\u65b9\u6cd5\u3002\u5728\u521b\u5efa\u7c7b\u65f6\uff0c\u8bf7\u8003\u8651\u4ee5\u4e0b\u51e0\u70b9\uff1a \u5982\u679c\u4e0d\u9700\u8981\u4ece\u7c7b\u7684\u5916\u90e8\u8bbf\u95ee\u6210\u5458\uff0c\u4e0d\u8981\u4e3a\u8be5\u6210\u5458\u63d0\u4f9b\u8bbf\u95ee\u51fd\u6570\uff1b \u5982\u679c\u9700\u8981\u4ece\u5916\u90e8\u8bbf\u95ee\u6210\u5458\uff0c\u8003\u8651\u662f\u5426\u53ef\u4ee5\u901a\u8fc7\u67d0\u79cd\u884c\u4e3a\u6216\u52a8\u4f5c\u6765\u5b8c\u6210\uff08\u4f8b\u5982\uff0c\u4e0e\u5176\u63d0\u4f9b setAlive(bool) setter\uff0c\u4e0d\u5982\u5b9e\u73b0\u4e00\u4e2a kill() \u51fd\u6570\uff09 \u5982\u679c\u53ef\u4ee5\uff0c\u4ec5\u8003\u8651\u63d0\u4f9b\u4e00\u4e2agetter","title":"\u6210\u5458\u8bbf\u95ee\u51fd\u6570\u7684\u76f8\u5173\u95ee\u9898"},{"location":"13-4-access-functions-and-encapsulation/#_5","text":"\u6b63\u5982\u60a8\u6240\u770b\u5230\u7684\uff0c\u5b9e\u73b0\u5c01\u88c5\u5e76\u4e0d\u4f1a\u5f88\u9ebb\u70e6\uff0c\u5176\u5e26\u6765\u7684\u597d\u5904\u5374\u975e\u5e38\u7684\u591a\u3002\u5c01\u88c5\u7684\u4e3b\u8981\u597d\u5904\u662f\u5141\u8bb8\u7528\u6237\u5728\u4e0d\u4e86\u89e3\u7c7b\u7684\u5185\u90e8\u5b9e\u73b0\u7684\u60c5\u51b5\u4e0b\uff0c\u8f7b\u677e\u5730\u4f7f\u7528\u8be5\u7c7b\u3002\u00f7","title":"\u5c0f\u7ed3"},{"location":"13-5-constructors/","tags":["constructors"],"text":"\u5982\u679c\u4e00\u4e2a\u7c7b\u6216\u8005\u7ed3\u6784\u4f53\u7684\u6210\u5458\u90fd\u662f \u516c\u6709\u6210\u5458 \uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 \u805a\u5408\u521d\u59cb\u5316 \uff0c\u901a\u8fc7 \u5217\u8868\u521d\u59cb\u5316 \u5bf9\u7c7b\u8fdb\u884c\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 class Foo { public : int m_x {}; int m_y {}; }; int main () { Foo foo { 6 , 7 }; // list-initialization return 0 ; } \u4f46\u662f\uff0c\u4e00\u65e6\u5c06\u5176\u4e2d\u7684\u4efb\u4f55\u6210\u5458\u53d8\u91cf\u8bbe\u4e3a \u79c1\u6709\u6210\u5458 \uff0c\u5c31\u4e0d\u80fd\u518d\u80fd\u591f\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u521d\u59cb\u5316\u7c7b\u4e86\u3002\u9053\u7406\u5f88\u7b80\u5355\uff1a\u5982\u679c\u4f60\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u4e00\u4e2a\u53d8\u91cf(\u56e0\u4e3a\u5b83\u662f\u79c1\u6709\u7684)\uff0c\u4f60\u5c31\u4e0d\u5e94\u88ab\u5141\u8bb8\u76f4\u63a5\u521d\u59cb\u5316\u5b83\u3002 \u90a3\u4e48\uff0c\u5e94\u8be5\u5982\u4f55\u521d\u59cb\u5316\u5305\u542b\u79c1\u6709\u6210\u5458\u7684\u7c7b\u5462\uff1f\u7b54\u6848\u662f\u901a\u8fc7\u6784\u9020\u51fd\u6570\u3002 \u6784\u9020\u51fd\u6570 \u6784\u9020\u51fd\u6570 \u662f\u4e00\u7c7b\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u4f1a\u5728\u5bf9\u8c61\u521b\u5efa\u65f6\u88ab\u81ea\u52a8\u8c03\u7528\u3002\u6784\u9020\u51fd\u6570\u901a\u5e38\u7528\u4e8e\u4f7f\u7528\u7528\u6237\u63d0\u4f9b\u503c\u6765\u521d\u59cb\u5316\u7c7b\u7684\u6210\u5458\u53d8\u91cf\uff0c\u6216\u8005\u7528\u4e8e\u4e3a\u5bf9\u8c61\u7684\u4f7f\u7528\u505a\u597d\u76f8\u5e94\u7684\u51c6\u5907\u5de5\u4f5c\uff08\u4f8b\u5982\uff1a\u6253\u5f00\u6587\u4ef6\u6216\u6570\u636e\u5e93\uff09\u3002 \u6784\u9020\u51fd\u6570\u6267\u884c\u540e\uff0c\u5bf9\u8c61\u5e94\u8be5\u5904\u4e8e\u4e00\u79cd\u88ab\u826f\u597d\u5b9a\u4e49\u3001\u53ef\u4ee5\u4f7f\u7528\u7684\u72b6\u6001\u3002 \u548c\u666e\u901a\u7684\u6210\u5458\u51fd\u6570\u4e0d\u540c\uff0c\u6784\u9020\u51fd\u6570\u7684\u547d\u540d\u5fc5\u987b\u9075\u5faa\u4ee5\u4e0b\u539f\u5219\uff1a \u6784\u9020\u51fd\u6570\u7684\u540d\u5b57\u5fc5\u987b\u548c\u7c7b\u540d\u5b8c\u5168\u4e00\u81f4\uff08\u5305\u62ec\u5927\u5c0f\u5199\uff09\uff1b \u6784\u9020\u51fd\u6570\u4e0d\u8fd4\u56de\u4efb\u4f55\u7c7b\u578b\uff08\u751a\u81f3\u4e0d\u8fd4\u56de void \uff09 \u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u548c\u9ed8\u8ba4\u521d\u59cb\u5316 \u4e0d\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\uff08\u6216\u6240\u6709\u53c2\u6570\u90fd\u6709\u9ed8\u8ba4\u503c\uff09\u7684\u6784\u9020\u51fd\u6570\u79f0\u4e3a \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 \u3002\u5f53\u7528\u6237\u521d\u59cb\u5316\u5bf9\u8c61\u65f6\u4e0d\u63d0\u4f9b\u4efb\u4f55\u53c2\u6570\u65f6\uff0c\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u5c31\u4f1a\u88ab\u8c03\u7528\u3002 \u4e0b\u9762\u4f8b\u5b50\u4e2d\u7684\u7c7b\u5177\u6709\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : Fraction () // default constructor { m_numerator = 0 ; m_denominator = 1 ; } int getNumerator () { return m_numerator ; } int getDenominator () { return m_denominator ; } double getValue () { return static_cast < double > ( m_numerator ) / m_denominator ; } }; int main () { Fraction frac {}; // calls Fraction() default constructor std :: cout << frac . getNumerator () << '/' << frac . getDenominator () << '\\n' ; return 0 ; } Fraction \u7c7b\u7528\u4e8e\u8868\u793a\u4e00\u4e2a\u5206\u6570\uff08\u901a\u8fc7\u5206\u5b50\u548c\u5206\u6bcd\uff09\uff0c\u540c\u65f6\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a Fraction \u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff08\u4e0e\u51fd\u6570\u540c\u540d\uff09\u3002 \u5f53 Fraction frac{}; \u6267\u884c\u65f6\uff0c\u7f16\u8bd1\u5668\u5f97\u77e5\u6211\u4eec\u60f3\u8981\u521d\u59cb\u5316\u4e00\u4e2a Fraction \u7c7b\u578b\u7684\u5bf9\u8c61\uff08\u4e0d\u63d0\u4f9b\u4efb\u4f55\u53c2\u6570\uff09\u3002\u7136\u540e\uff0c\u5bf9\u8c61 frac \u7684\u521d\u59cb\u5316\u5c31\u5f00\u59cb\u4e86\uff0c\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u4e5f\u4f1a\u5728\u6b64\u65f6\u88ab\u8c03\u7528\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a\u9ed8\u8ba4\u521d\u59cb\u5316\uff08\u5c3d\u7ba1\u4e0d\u4e25\u8c28\uff09\u3002\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u8fd0\u884c\u65f6\u548c\u666e\u901a\u51fd\u6570\u4e00\u6837\uff08\u6b64\u5904\u5373\u5c060\u8d4b\u503c\u7ed9 m_numerator \uff0c\u5c06 m_denominator \u8d4b\u503c\u4e3a1 \uff09 \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 0/1 \u503c\u521d\u59cb\u5316 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 \u503c\u521d\u59cb\u5316 \u7684\u65b9\u5f0f\u521d\u59cb\u5316\u5bf9\u8c61\uff1a 1 Fraction frac {}; // Value initialization using empty set of braces \u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528 \u9ed8\u8ba4\u521d\u59cb\u5316 \u6765\u5b9e\u73b0\uff1a 1 Fraction frac ; // Default-initialization, calls default constructor \u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\u548c\u503c\u521d\u59cb\u5316\u6765\u521d\u59cb\u5316\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5176\u7ed3\u679c\u662f\u4e00\u81f4\u7684\uff1a\u5373\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u88ab\u8c03\u7528\u3002 \u6bd4\u8d77\u503c\u521d\u59cb\u5316\uff0c\u5f88\u591a\u7a0b\u5e8f\u5458\u66f4\u503e\u5411\u4e8e\u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\u3002\u8fd9\u662f\u56e0\u4e3a\u5728\u4f7f\u7528\u503c\u521d\u59cb\u5316\u65f6\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u9996\u5148\u5c06\u7c7b\u6210\u5458\u521d\u59cb\u5316\u4e3a0\uff0c\u7136\u540e\u624d\u8c03\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u4ece\u6548\u7387\u7684\u89d2\u5ea6\u6765\u8bb2\u66f4\u4f4e\u6548\u4e00\u4e9b\u3002 \u4e0d\u8fc7\uff0c\u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u4e5f\u6709\u4e0d\u597d\u7684\u5730\u65b9\uff1a\u4f60\u5fc5\u987b\u77e5\u9053\u7c7b\u662f\u5426\u5bf9\u4f1a\u521d\u59cb\u5316\u81ea\u8eab\u3002\u4f8b\u5982\uff0c\u8be5\u7c7b\u7684\u6240\u6709\u6210\u5458\u662f\u4e0d\u662f\u90fd\u5177\u6709\u521d\u59cb\u5316\u503c\uff0c\u6216\u8005\u662f\u5426\u5b58\u5728\u80fd\u591f\u5bf9\u6240\u6709\u6210\u5458\u8fdb\u884c\u521d\u59cb\u5316\u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002 \u5982\u679c\u4f60\u5df2\u7ecf\u77e5\u9053\u6709\u4e2a\u522b\u53d8\u91cf\u4e0d\u5177\u6709\u521d\u59cb\u5316\u503c\uff0c\u90a3\u4f60\u5c31\u8981\u614e\u91cd\u8003\u8651\uff0c\u6b64\u65f6\u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\u662f\u5426\u4f1a\u5e26\u6765\u95ee\u9898\u3002 \u4f8b\u5982\uff0c\u4e0b\u9762\u4ee3\u7801\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include class Fraction { private : // Removed initializers int m_numerator ; int m_denominator ; public : // Removed default-constructor int getNumerator () { return m_numerator ; } int getDenominator () { return m_denominator ; } double getValue () { return static_cast < double > ( m_numerator ) / m_denominator ; } }; int main () { Fraction frac ; // frac is uninitialized, accessing its members causes undefined behavior std :: cout << frac . getNumerator () << '/' << frac . getDenominator () << '\\n' ; return 0 ; } \u867d\u7136\u8fd9\u4e2a\u7c7b\u7684\u786e\u521d\u59cb\u5316\u4e86\u7c7b\u4e2d\u7684\u6240\u6709\u6210\u5458\uff0c\u4f46\u8981\u786e\u4fdd\u9879\u76ee\u4ee3\u7801\u4e2d\u7684\u6240\u6709\u7c7b\u90fd\u8fd9\u6837\u505a\u4e86\u662f\u5f88\u56f0\u96be\u7684\u3002 \u4f18\u5148\u5bf9\u7c7b\u5bf9\u8c61\u8fdb\u884c\u503c\u521d\u59cb\u5316\u662f\u7b80\u5355\u3001\u4e00\u81f4\u7684\uff0c\u5e76\u4e14\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6355\u83b7\u9519\u8bef\uff0c\u7279\u522b\u662f\u5728\u5b66\u4e60\u65f6\u3002 \u6700\u4f73\u5b9e\u8df5 \u521d\u59cb\u5316\u5bf9\u8c61\u65f6\uff0c\u63a8\u8350\u4f7f\u7528\u503c\u521d\u59cb\u5316\u800c\u975e\u9ed8\u8ba4\u521d\u59cb\u5316 \u4f7f\u7528\u5e26\u53c2\u6570\u7684\u51fd\u6570\u8fdb\u884c\u76f4\u63a5\u521d\u59cb\u5316\u548c\u5217\u8868\u521d\u59cb\u5316 \u5c3d\u7ba1\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u5e2e\u6211\u4eec\u786e\u4fdd\u7c7b\u6210\u5458\u5177\u6709\u5408\u7406\u7684\u521d\u503c\uff0c\u4f46\u662f\u66f4\u591a\u65f6\u5019\u6211\u4eec\u8fd8\u662f\u4f1a\u9700\u8981\u5728 \u5b9e\u4f8b\u5316 \u65f6\u6307\u5b9a\u4e00\u4e9b\u7279\u6b8a\u7684\u503c\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u6784\u9020\u51fd\u6570\u662f\u53ef\u4ee5\u88ab\u58f0\u660e\u4e3a\u5141\u8bb8\u63a5\u53d7\u53c2\u6570\u7684\u5f62\u5f0f\u7684\u3002\u4e0b\u9762\u4f8b\u5b50\u4e2d\u7684\u6784\u9020\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u6574\u578b\u53c2\u6570\u5206\u522b\u7528\u4e8e\u5bf9\u5206\u5b50\u5206\u6bcd\u8fdb\u884c\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : Fraction () // default constructor { m_numerator = 0 ; m_denominator = 1 ; } // Constructor with two parameters, one parameter having a default value Fraction ( int numerator , int denominator = 1 ) { assert ( denominator != 0 ); m_numerator = numerator ; m_denominator = denominator ; } int getNumerator () { return m_numerator ; } int getDenominator () { return m_denominator ; } double getValue () { return static_cast < double > ( m_numerator ) / m_denominator ; } }; \u6ce8\u610f\uff0c\u6211\u4eec\u73b0\u5728\u6709\u4e24\u4e2a\u6784\u9020\u51fd\u6570\uff1a\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff08\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u8c03\u7528\uff09\uff0c\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u5f62\u53c2\u3002C++\u652f\u6301 \u91cd\u8f7d \uff0c\u6240\u4ee5\u8fd9\u4e24\u4e2a\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u5728\u548c\u5e73\u5171\u5904\u3002\u4e8b\u5b9e\u4e0a\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5b9a\u4e49\u4efb\u610f\u6570\u91cf\u7684\u6784\u9020\u51fd\u6570\uff0c\u53ea\u8981\u6bcf\u4e2a\u6784\u9020\u51fd\u6570\u5177\u6709\u60df\u4e00\u7684\u7b7e\u540d(\u5f62\u53c2\u7684\u6570\u91cf\u548c\u7c7b\u578b)\u5373\u53ef\u3002 \u90a3\u4e48\uff0c\u6211\u4eec\u5e94\u8be5\u5982\u4f55\u8c03\u7528\u8fd9\u4e2a\u5177\u6709\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u5462\uff1f\u5f88\u7b80\u5355\uff0c\u4f7f\u7528 \u5217\u8868\u521d\u59cb\u5316 \u5373\u53ef\uff1a 1 2 Fraction fiveThirds { 5 , 3 }; // \u5217\u8868\u521d\u59cb\u5316\uff0c\u8c03\u7528 Fraction(int, int) Fraction threeQuarters ( 3 , 4 ); // \u76f4\u63a5\u521d\u59cb\u5316\uff0c\u4e5f\u662f\u8c03\u7528 Fraction(int, int) \u4e00\u5982\u65e2\u5f80\u5730\uff0c \u6211\u4eec\u66f4\u63a8\u8350\u5217\u8868\u521d\u59cb\u5316\u3002\u5728\u540e\u9762\u7684\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u4e5f\u4f1a\u9047\u5230\u9700\u8981\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\u7684\u60c5\u51b5(\u6a21\u677f\u548c std::initializer_list )\u3002\u8fd8\u6709\u53e6\u5916\u4e00\u79cd\u7279\u6b8a\u7684\u6784\u9020\u51fd\u6570\u4f1a\u4f7f\u7528\u62ec\u53f7\u521d\u59cb\u5316\uff08\u5217\u8868\u521d\u59cb\u5316\uff09\u505a\u4e00\u4e9b\u4e0d\u540c\u7684\u4e8b\u60c5\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\u3002 \u6ce8\u610f\uff0c\u7531\u4e8e\u6784\u9020\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a \u5f62\u53c2 \u5177\u6709\u9ed8\u8ba4\u503c\uff0c\u6240\u4ee5\u4e0b\u9762\u7684\u8c03\u7528\u65b9\u5f0f\u4e5f\u662f\u5408\u6cd5\u7684: 1 Fraction six { 6 }; // \u8c03\u7528 Fraction(int, int) \u6784\u9020\u51fd\u6570\uff0c\u7b2c\u4e8c\u4e2a\u5f62\u53c2\u4f1a\u4f7f\u7528\u9ed8\u8ba4\u503c1\u3002 \u6784\u9020\u51fd\u6570\u7684\u9ed8\u8ba4\u53c2\u6570\u548c\u666e\u901a\u51fd\u6570\u7684\u9ed8\u8ba4\u53c2\u6570\u6ca1\u6709\u4ec0\u4e48\u533a\u522b\uff0c\u6240\u4ee5\u5f53\u6211\u4eec\u8c03\u7528 six{ 6 } \u65f6\uff0c Fraction(int, int) \u51fd\u6570\u4f1a\u81ea\u52a8\u4f7f\u75281\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u5f62\u53c2\u7684\u503c\u3002 \u6700\u4f73\u5b9e\u8df5 \u63a8\u8350\u4f7f\u7528\u5927\u62ec\u53f7\u521d\u59cb\u5316\u6765\u521d\u59cb\u7c7b\u5bf9\u8c61\u3002 \u4f7f\u7528\u7b49\u4e8e\u53f7\u8fdb\u884c\u62f7\u8d1d\u521d\u59cb\u5316 \u5c31\u50cf\u57fa\u672c\u53d8\u91cf\u4e00\u6837\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 \u62f7\u8d1d\u521d\u59cb\u5316 \u6765\u521d\u59cb\u5316\u7c7b\uff1a 1 2 Fraction six = Fraction { 6 }; // Copy initialize a Fraction, will call Fraction(6, 1) Fraction seven = 7 ; // Copy initialize a Fraction. The compiler will try to find a way to convert 7 to a Fraction, which will invoke the Fraction(7, 1) constructor. \u4e0d\u8fc7\uff0c\u6700\u597d\u4e0d\u8981\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u6765\u521d\u59cb\u5316\u7c7b\uff0c\u5b83\u53ef\u80fd\u662f\u4f4e\u6548\u7684\u3002\u5c3d\u7ba1 \u76f4\u63a5\u521d\u59cb\u5316 \u3001 \u5217\u8868\u521d\u59cb\u5316 \u548c \u62f7\u8d1d\u521d\u59cb\u5316 \u5728\u521d\u59cb\u5316\u57fa\u672c\u6570\u636e\u7c7b\u578b\u65f6\u7684\u884c\u4e3a\u662f\u4e00\u6837\u7684\uff0c\u4f46\u662f\u62f7\u8d1d\u521d\u59cb\u5316\u5728\u914d\u5408\u7c7b\u7c7b\u578b\u4f7f\u7528\u65f6\u5176\u884c\u4e3a\u662f\u4e0d\u540c\u7684\uff08\u5c3d\u7ba1\u7ed3\u679c\u76f8\u540c\uff09\u3002\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u7ae0\u8282\u4e2d\u8fdb\u884c\u66f4\u4e3a\u8be6\u7ec6\u7684\u4ecb\u7ecd\u3002 \u51cf\u5c11\u6784\u9020\u51fd\u6570\u7684\u6570\u91cf \u50cf\u4e0a\u9762\u90a3\u6837\u4e3a Fraction \u58f0\u660e\u4e24\u4e2a\u6784\u9020\u51fd\u6570\u662f\u591a\u4f59\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) { assert ( denominator != 0 ); m_numerator = numerator ; m_denominator = denominator ; } int getNumerator () { return m_numerator ; } int getDenominator () { return m_denominator ; } double getValue () { return static_cast < double > ( m_numerator ) / m_denominator ; } }; \u5c3d\u7ba1\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u4ecd\u7136\u662f\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u4f46\u662f\u5b83\u73b0\u5728\u4e5f\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u57fa\u4e8e\u7528\u6237\u63d0\u4f9b\u7684\u53c2\u6570\u8fdb\u884c\u521d\u59cb\u5316\u3002 1 2 3 4 Fraction zero ; // will call Fraction(0, 1) Fraction zero {}; // will call Fraction(0, 1) Fraction six { 6 }; // will call Fraction(6, 1) Fraction fiveThirds { 5 , 3 }; // will call Fraction(5, 3) \u5728\u5b9e\u73b0\u6784\u9020\u51fd\u6570\u65f6\uff0c\u8981\u8003\u8651\u5982\u4f55\u901a\u8fc7\u9ed8\u8ba4\u503c\u6765\u51cf\u5c11\u6784\u9020\u51fd\u6570\u7684\u6570\u91cf\u3002 \u5173\u4e8e\u9ed8\u8ba4\u53c2\u6570\u7684\u6ce8\u610f\u4e8b\u9879 \u5b9a\u4e49\u548c\u8c03\u7528\u5177\u6709\u9ed8\u8ba4\u53c2\u6570\u51fd\u6570\u7684\u89c4\u5219 (\u53c2\u89c1\uff1a 8.12 - \u9ed8\u8ba4\u53c2\u6570 ) \u540c\u6837\u4e5f\u9002\u7528\u4e8e\u6784\u9020\u51fd\u6570\u3002\u7b80\u5355\u590d\u4e60\u4e00\u4e0b\uff0c\u5728\u5b9a\u4e49\u5177\u6709\u9ed8\u8ba4\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u65f6\uff0c\u9ed8\u8ba4\u53c2\u6570\u5fc5\u987b\u5728\u975e\u9ed8\u8ba4\u53c2\u6570\u7684\u540e\u9762\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u975e\u9ed8\u8ba4\u53c2\u6570\u4e0d\u80fd\u88ab\u5b9a\u4e49\u5728\u9ed8\u8ba4\u53c2\u6570\u540e\u3002 \u5bf9\u4e8e\u5177\u6709\u591a\u4e2a\u9ed8\u8ba4\u53c2\u6570\uff0c\u4e14\u53c2\u6570\u7c7b\u578b\u4e0d\u540c\u7684\u6784\u9020\u51fd\u6570\uff0c\u4f7f\u7528\u4e0d\u5f53\u4f1a\u5e26\u6765\u51fa\u4e4e\u610f\u6599\u7684\u9519\u8bef\u3002\u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Something { public : // Default constructor Something ( int n = 0 , double d = 1.2 ) // allows us to construct a Something(int, double), Something(int), or Something() { } }; int main () { Something s1 { 1 , 2.4 }; // calls Something(int, double) Something s2 { 1 }; // calls Something(int, double) Something s3 {}; // calls Something(int, double) Something s4 { 2.4 }; // \u65e0\u6cd5\u7f16\u8bd1\uff0c\u56e0\u4e3a\u6ca1\u6709\u80fd\u5339\u914d\u7684\u6784\u9020\u51fd\u6570 Something(double) return 0 ; } \u5bf9\u4e8e s4 \uff0c\u6211\u4eec\u5e0c\u671b\u4f7f\u7528\u4e00\u4e2a double \u7c7b\u578b\u7684\u53c2\u6570\u6784\u9020 Something \u3002\u4e0a\u9762\u7a0b\u5e8f\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\uff0c\u56e0\u4e3a\u89c4\u5219\u4e0d\u5141\u8bb8\u6211\u4eec\u8df3\u8fc7\u9ed8\u8ba4\u53c2\u6570\u524d\u9762\u7684\u9ed8\u8ba4\u53c2\u6570\u4e3a\u540e\u9762\u7684\u9ed8\u8ba4\u53c2\u6570\u63d0\u4f9b\u5177\u4f53\u503c\uff08\u8fd9\u91cc\u5373\u6700\u5de6\u8fb9\u7684 int \u7c7b\u578b\u7684 \u5f62\u53c2 \u4e0d\u80fd\u88ab\u8df3\u8fc7\uff09\u3002 \u5982\u679c\u60f3\u57fa\u4e8e\u4e00\u4e2a double \u7c7b\u578b\u7684\u53d8\u91cf\u6784\u9020 Something \uff0c\u5219\u5fc5\u987b\u518d\u5b9a\u4e49\u4e00\u4e2a\uff08\u975e\u9ed8\u8ba4\uff09\u6784\u9020\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class Something { public : // Default constructor Something ( int n = 0 , double d = 1.2 ) // allows us to construct a Something(int, double), Something(int), or Something() { } Something ( double d ) { } }; int main () { Something s1 { 1 , 2.4 }; // calls Something(int, double) Something s2 { 1 }; // calls Something(int, double) Something s3 {}; // calls Something(int, double) Something s4 { 2.4 }; // calls Something(double) return 0 ; } \u9690\u5f0f\u751f\u6210\u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570 \u5982\u679c\u4f60\u7684\u7c7b\u6ca1\u6709\u5b9a\u4e49\u6784\u9020\u51fd\u6570\uff0c\u5219C++\u53ef\u4ee5\u81ea\u52a8\u4e3a\u4f60\u751f\u6210\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u88ab\u79f0\u4e3a \u9690\u5f0f\u6784\u9020\u51fd\u6570 \u3002 \u8003\u8651\u4e0b\u9762\u7684\u7c7b\u5b9a\u4e49\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Date { private : int m_year { 1900 }; int m_month { 1 }; int m_day { 1 }; // \u7528\u6237\u6ca1\u6709\u63d0\u4f9b\u6784\u9020\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u81ea\u52a8\u751f\u6210\u9ed8\u8ba4\u6784\u9020\u51fd\u6570 }; int main () { Date date {}; return 0 ; } Date \u7c7b\u6ca1\u6709\u6784\u9020\u51fd\u6570\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u4f1a\u4e3a\u5176\u521b\u5efa\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u8be5\u9690\u5f0f\u6784\u9020\u51fd\u6570\uff0c\u5728\u4e0d\u63d0\u4f9b\u53c2\u6570\u7684\u60c5\u51b5\u4e0b\u521b\u5efa Date \u5bf9\u8c61\u3002 \u5f53\u9690\u5f0f\u6784\u9020\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u5177\u6709\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u503c\u7684\u6210\u5458\u4f1a\u88ab\u521d\u59cb\u5316(\u53c2\u8003\uff1a 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 \u548c 13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u5217\u8868 )\u3002 \u53ea\u8981\u4f60\u7684\u7c7b\u4e2d\u6709\u6784\u9020\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u5c31\u4e0d\u4f1a\u4e3a\u4f60\u521b\u5efa\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 class Date { private : int m_year { 1900 }; int m_month { 1 }; int m_day { 1 }; public : Date ( int year , int month , int day ) // normal non-default constructor { m_year = year ; m_month = month ; m_day = day ; } // No implicit constructor provided because we already defined our own constructor }; int main () { Date date {}; // error: Can't instantiate object because default constructor doesn't exist and the compiler won't generate one Date today { 2020 , 1 , 19 }; // today is initialized to Jan 19th, 2020 return 0 ; } \u5982\u679c\u4f60\u7684\u7c7b\u5177\u6709\u5176\u4ed6\u6784\u9020\u51fd\u6570\uff0c\u4f46\u662f\u4f60\u9700\u8981\u7c7b\u652f\u6301\u9ed8\u8ba4\u521d\u59cb\u5316\uff0c\u5219\u53ef\u4ee5\u4e3a\u6784\u9020\u51fd\u6570\u7684\u6bcf\u4e2a\u5f62\u53c2\u6dfb\u52a0\u9ed8\u8ba4\u503c\uff0c\u6216\u8005\u663e\u5f0f\u5730\u5b9a\u4e49\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002 \u5f53\u7136\uff0c\u8fd8\u6709\u7b2c\u4e09\u79cd\u65b9\u5f0f\uff1a\u4f60\u53ef\u4ee5\u4f7f\u7528 default \u5173\u952e\u5b57\u544a\u77e5\u7f16\u8bd1\u5668\u751f\u6210\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 class Date { private : int m_year { 1900 }; int m_month { 1 }; int m_day { 1 }; public : // \u8981\u6c42\u7f16\u8bd1\u5668\u751f\u6210\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u5373\u4f7f // \u7528\u6237\u5df2\u7ecf\u63d0\u4f9b\u4e86\u6784\u9020\u51fd\u6570 Date () = default ; Date ( int year , int month , int day ) // \u666e\u901a\u975e\u9ed8\u8ba4\u6784\u9020\u51fd\u6570 { m_year = year ; m_month = month ; m_day = day ; } }; int main () { Date date {}; // date is initialized to Jan 1st, 1900 Date today { 2020 , 10 , 14 }; // today is initialized to Oct 14th, 2020 return 0 ; } \u5199 default \u6bd4\u5199\u4e00\u4e2a\u7a7a\u7684\u51fd\u6570\u4f53\u66f4\u8d39\u4e8b\uff0c\u4f46\u662f\u5b83\u80fd\u591f\u66f4\u597d\u5730\u8868\u660e\u4f60\u7684\u610f\u56fe\uff08\u521b\u5efa\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff09\uff0c\u800c\u4e14\u4e5f\u66f4\u5b89\u5168\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5bf9\u6ca1\u6709\u521d\u59cb\u5316\u503c\u7684\u51fd\u6570\u8fdb\u884c0\u521d\u59cb\u5316\u3002 default \u5bf9\u4e8e\u5176\u4ed6\u7279\u6b8a\u6784\u9020\u51fd\u6570\u4e5f\u6709\u6548\uff0c\u7b49\u6211\u4eec\u9047\u5230\u65f6\u518d\u8ba8\u8bba\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u7c7b\u5df2\u7ecf\u62e5\u6709\u6784\u9020\u51fd\u6570\uff0c\u4f46\u4f60\u4ecd\u7136\u9700\u8981\u4e00\u4e2a\u4ec0\u4e48\u90fd\u4e0d\u505a\u7684\u6784\u9020\u51fd\u6570(\u4f8b\u5982\uff0c\u6240\u6709\u6210\u5458\u51fd\u6570\u90fd\u901a\u8fc7\u975e\u9759\u6001\u6210\u5458\u51fd\u6570\u521d\u59cb\u5316\u8fdb\u884c\u4e86\u521d\u59cb\u5316)\uff0c\u53ef\u4ee5\u4f7f\u7528 = default\u3002 \u5305\u542b\u7c7b\u7c7b\u578b\u6210\u5458\u7684\u7c7b class \u4e2d\u7684\u6210\u5458\u53d8\u91cf\uff0c\u4e5f\u53ef\u4ee5\u662f\u5176\u4ed6\u7c7b\u5bf9\u8c61\u3002 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5f53\u5916\u5c42\u5bf9\u8c61\u88ab\u521d\u59cb\u5316\u65f6\uff0c\u5185\u90e8\u7684\u8fd9\u4e9b\u5bf9\u8c61\u5176\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u4e5f\u4f1a\u88ab\u8c03\u7528\uff0c\u800c\u4e14\u4f1a\u5728\u6267\u884c\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u4e4b\u524d\u53d1\u751f\u3002 \u4e3e\u4f8b\u6765\u8bf4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include class A { public : A () { std :: cout << \"A \\n \" ; } }; class B { private : A m_a ; // A \u662f B \u7684\u4e00\u4e2a\u6210\u5458 public : B () { std :: cout << \"B \\n \" ; } }; int main () { B b ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 A B \u5f53\u53d8\u91cf b \u88ab\u6784\u5efa\u65f6\uff0c\u6784\u9020\u51fd\u6570 B() \u4f1a\u88ab\u8c03\u7528\uff0c\u5728 B() \u7684\u51fd\u6570\u4f53\u6267\u884c\u524d\uff0c m_a \u5c31\u5f00\u59cb\u521d\u59cb\u5316\u4e86\uff0c\u6b64\u65f6\u4f1a\u8c03\u7528 A \u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u5e76\u6253\u5370 \"A\"\u3002\u7136\u540e\u63a7\u5236\u6743\u8fd4\u8fd8\u7ed9\u6784\u9020\u51fd\u6570 B() \u5e76\u6267\u884c\u5176\u51fd\u6570\u4f53\u3002 \u8fd9\u4e48\u505a\u662f\u6709\u9053\u7406\u7684\uff0c\u56e0\u4e3a\u6784\u9020 B() \u662f\u6709\u53ef\u80fd\u4f1a\u53bb\u4f7f\u7528\u6210\u5458\u53d8\u91cf m_a \u7684\uff0c\u6240\u4ee5\u5b83\u5e94\u8be5\u9996\u5148\u88ab\u521d\u59cb\u5316\u3002 \u4e0a\u9762\u6700\u540e\u4e00\u4e2a\u4f8b\u5b50\u4e2d\uff0c m_a \u662f\u4e00\u4e2aclass\u7c7b\u578b\u7684\u53d8\u91cf\u3002 \u5bf9\u4e8eclass\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u5373\u4f7f\u6211\u4eec\u6ca1\u6709\u663e\u5f0f\u5730\u521d\u59cb\u5316\u5b83\u4eec\uff0c\u5b83\u4e5f\u4f1a\u88ab\u521d\u59cb\u5316\u3002 \u5728\u4e0b\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba\u5982\u4f55\u521d\u59cb\u5316\u8fd9\u4e9bclass\u7c7b\u578b\u7684\u6210\u5458\u53d8\u91cf\u3002 \u6784\u9020\u51fd\u6570\u5c0f\u7ed3 \u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u4f1a\u6709\u8fd9\u6837\u7684\u56f0\u60d1\uff1a\u6784\u9020\u51fd\u6570\u4f1a\u521b\u5efa\u5bf9\u8c61\u5417\uff1f\u5b9e\u9645\u4e0a\u6784\u9020\u51fd\u6570\u5e76\u4e0d\u4f1a\u521b\u5efa\u5bf9\u8c61\u2014\u2014\u7f16\u8bd1\u5668\u4f1a\u5728\u6784\u9020\u51fd\u6570\u8c03\u7528\u524d\u5c31\u4e3a\u5bf9\u8c61\u5206\u914d\u5185\u5b58\u3002 \u6784\u9020\u51fd\u6570\u5b9e\u9645\u4e0a\u6709\u4e24\u4e2a\u76ee\u7684\uff1a \u6784\u9020\u51fd\u6570\u53ef\u4ee5\u51b3\u5b9a\u8c01\u80fd\u591f\u521b\u5efa\u8fd9\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u53ea\u6709\u5f53\u80fd\u591f\u627e\u5230\u5339\u914d\u7684\u6784\u9020\u51fd\u6570\u65f6\uff0c\u8be5\u7c7b\u578b\u7684\u5bf9\u8c61\u624d\u80fd\u591f\u88ab\u521b\u5efa\u3002 \u6784\u9020\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u521d\u59cb\u5316\u5bf9\u8c61\u3002\u81f3\u4e8e\u6784\u9020\u51fd\u6570\u662f\u5426\u4f1a\u8fdb\u884c\u5b9e\u9645\u7684\u521d\u59cb\u5316\u5de5\u4f5c\uff0c\u8fd9\u53d6\u51b3\u4e8e\u7a0b\u5e8f\u5458\u3002\u6784\u9020\u51fd\u6570\u4e0d\u8fdb\u884c\u4efb\u4f55\u7684\u521d\u59cb\u5316\u5de5\u4f5c\uff0c\u4ece\u8bed\u6cd5\u5c42\u9762\u6765\u8bb2\u4e5f\u662f\u5408\u6cd5\u7684\u3002 \u7136\u800c\uff0c\u5c31\u50cf\u521d\u59cb\u5316\u6240\u6709\u5c40\u90e8\u53d8\u91cf\u662f\u6700\u4f73\u5b9e\u8df5\u4e00\u6837\uff0c\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u521d\u59cb\u5316\u6240\u6709\u6210\u5458\u53d8\u91cf\u4e5f\u662f\u6700\u4f73\u5b9e\u8df5\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u6784\u9020\u51fd\u6570\u6216\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u6765\u5b8c\u6210\u3002 \u6700\u4f73\u5b9e\u8df5 \u6c38\u8fdc\u8981\u8bb0\u5f97\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u5168\u90e8\u6210\u5458\u53d8\u91cf\u3002 \u6700\u540e\uff0c\u6784\u9020\u51fd\u6570\u4ec5\u7528\u4e8e\u521b\u5efa\u5bf9\u8c61\u65f6\u7684\u521d\u59cb\u5316\u3002\u4e0d\u8981\u4f7f\u7528\u6784\u9020\u51fd\u6570\u6765\u91cd\u65b0\u521d\u59cb\u5316\u73b0\u6709\u5bf9\u8c61\u3002\u867d\u7136\u53ef\u4ee5\u7f16\u8bd1\uff0c\u4f46\u7ed3\u679c\u4e0d\u4f1a\u662f\u4f60\u60f3\u8981\u7684(\u76f8\u53cd\uff0c\u7f16\u8bd1\u5668\u5c06\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff0c\u7136\u540e\u4e22\u5f03\u5b83)\u3002","title":"13.5 - \u6784\u9020\u51fd\u6570"},{"location":"13-5-constructors/#_1","text":"\u6784\u9020\u51fd\u6570 \u662f\u4e00\u7c7b\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u4f1a\u5728\u5bf9\u8c61\u521b\u5efa\u65f6\u88ab\u81ea\u52a8\u8c03\u7528\u3002\u6784\u9020\u51fd\u6570\u901a\u5e38\u7528\u4e8e\u4f7f\u7528\u7528\u6237\u63d0\u4f9b\u503c\u6765\u521d\u59cb\u5316\u7c7b\u7684\u6210\u5458\u53d8\u91cf\uff0c\u6216\u8005\u7528\u4e8e\u4e3a\u5bf9\u8c61\u7684\u4f7f\u7528\u505a\u597d\u76f8\u5e94\u7684\u51c6\u5907\u5de5\u4f5c\uff08\u4f8b\u5982\uff1a\u6253\u5f00\u6587\u4ef6\u6216\u6570\u636e\u5e93\uff09\u3002 \u6784\u9020\u51fd\u6570\u6267\u884c\u540e\uff0c\u5bf9\u8c61\u5e94\u8be5\u5904\u4e8e\u4e00\u79cd\u88ab\u826f\u597d\u5b9a\u4e49\u3001\u53ef\u4ee5\u4f7f\u7528\u7684\u72b6\u6001\u3002 \u548c\u666e\u901a\u7684\u6210\u5458\u51fd\u6570\u4e0d\u540c\uff0c\u6784\u9020\u51fd\u6570\u7684\u547d\u540d\u5fc5\u987b\u9075\u5faa\u4ee5\u4e0b\u539f\u5219\uff1a \u6784\u9020\u51fd\u6570\u7684\u540d\u5b57\u5fc5\u987b\u548c\u7c7b\u540d\u5b8c\u5168\u4e00\u81f4\uff08\u5305\u62ec\u5927\u5c0f\u5199\uff09\uff1b \u6784\u9020\u51fd\u6570\u4e0d\u8fd4\u56de\u4efb\u4f55\u7c7b\u578b\uff08\u751a\u81f3\u4e0d\u8fd4\u56de void \uff09","title":"\u6784\u9020\u51fd\u6570"},{"location":"13-5-constructors/#_2","text":"\u4e0d\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\uff08\u6216\u6240\u6709\u53c2\u6570\u90fd\u6709\u9ed8\u8ba4\u503c\uff09\u7684\u6784\u9020\u51fd\u6570\u79f0\u4e3a \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 \u3002\u5f53\u7528\u6237\u521d\u59cb\u5316\u5bf9\u8c61\u65f6\u4e0d\u63d0\u4f9b\u4efb\u4f55\u53c2\u6570\u65f6\uff0c\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u5c31\u4f1a\u88ab\u8c03\u7528\u3002 \u4e0b\u9762\u4f8b\u5b50\u4e2d\u7684\u7c7b\u5177\u6709\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : Fraction () // default constructor { m_numerator = 0 ; m_denominator = 1 ; } int getNumerator () { return m_numerator ; } int getDenominator () { return m_denominator ; } double getValue () { return static_cast < double > ( m_numerator ) / m_denominator ; } }; int main () { Fraction frac {}; // calls Fraction() default constructor std :: cout << frac . getNumerator () << '/' << frac . getDenominator () << '\\n' ; return 0 ; } Fraction \u7c7b\u7528\u4e8e\u8868\u793a\u4e00\u4e2a\u5206\u6570\uff08\u901a\u8fc7\u5206\u5b50\u548c\u5206\u6bcd\uff09\uff0c\u540c\u65f6\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a Fraction \u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff08\u4e0e\u51fd\u6570\u540c\u540d\uff09\u3002 \u5f53 Fraction frac{}; \u6267\u884c\u65f6\uff0c\u7f16\u8bd1\u5668\u5f97\u77e5\u6211\u4eec\u60f3\u8981\u521d\u59cb\u5316\u4e00\u4e2a Fraction \u7c7b\u578b\u7684\u5bf9\u8c61\uff08\u4e0d\u63d0\u4f9b\u4efb\u4f55\u53c2\u6570\uff09\u3002\u7136\u540e\uff0c\u5bf9\u8c61 frac \u7684\u521d\u59cb\u5316\u5c31\u5f00\u59cb\u4e86\uff0c\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u4e5f\u4f1a\u5728\u6b64\u65f6\u88ab\u8c03\u7528\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a\u9ed8\u8ba4\u521d\u59cb\u5316\uff08\u5c3d\u7ba1\u4e0d\u4e25\u8c28\uff09\u3002\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u8fd0\u884c\u65f6\u548c\u666e\u901a\u51fd\u6570\u4e00\u6837\uff08\u6b64\u5904\u5373\u5c060\u8d4b\u503c\u7ed9 m_numerator \uff0c\u5c06 m_denominator \u8d4b\u503c\u4e3a1 \uff09 \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 0/1","title":"\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u548c\u9ed8\u8ba4\u521d\u59cb\u5316"},{"location":"13-5-constructors/#_3","text":"\u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 \u503c\u521d\u59cb\u5316 \u7684\u65b9\u5f0f\u521d\u59cb\u5316\u5bf9\u8c61\uff1a 1 Fraction frac {}; // Value initialization using empty set of braces \u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528 \u9ed8\u8ba4\u521d\u59cb\u5316 \u6765\u5b9e\u73b0\uff1a 1 Fraction frac ; // Default-initialization, calls default constructor \u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\u548c\u503c\u521d\u59cb\u5316\u6765\u521d\u59cb\u5316\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5176\u7ed3\u679c\u662f\u4e00\u81f4\u7684\uff1a\u5373\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u88ab\u8c03\u7528\u3002 \u6bd4\u8d77\u503c\u521d\u59cb\u5316\uff0c\u5f88\u591a\u7a0b\u5e8f\u5458\u66f4\u503e\u5411\u4e8e\u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\u3002\u8fd9\u662f\u56e0\u4e3a\u5728\u4f7f\u7528\u503c\u521d\u59cb\u5316\u65f6\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u9996\u5148\u5c06\u7c7b\u6210\u5458\u521d\u59cb\u5316\u4e3a0\uff0c\u7136\u540e\u624d\u8c03\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u4ece\u6548\u7387\u7684\u89d2\u5ea6\u6765\u8bb2\u66f4\u4f4e\u6548\u4e00\u4e9b\u3002 \u4e0d\u8fc7\uff0c\u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u4e5f\u6709\u4e0d\u597d\u7684\u5730\u65b9\uff1a\u4f60\u5fc5\u987b\u77e5\u9053\u7c7b\u662f\u5426\u5bf9\u4f1a\u521d\u59cb\u5316\u81ea\u8eab\u3002\u4f8b\u5982\uff0c\u8be5\u7c7b\u7684\u6240\u6709\u6210\u5458\u662f\u4e0d\u662f\u90fd\u5177\u6709\u521d\u59cb\u5316\u503c\uff0c\u6216\u8005\u662f\u5426\u5b58\u5728\u80fd\u591f\u5bf9\u6240\u6709\u6210\u5458\u8fdb\u884c\u521d\u59cb\u5316\u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002 \u5982\u679c\u4f60\u5df2\u7ecf\u77e5\u9053\u6709\u4e2a\u522b\u53d8\u91cf\u4e0d\u5177\u6709\u521d\u59cb\u5316\u503c\uff0c\u90a3\u4f60\u5c31\u8981\u614e\u91cd\u8003\u8651\uff0c\u6b64\u65f6\u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\u662f\u5426\u4f1a\u5e26\u6765\u95ee\u9898\u3002 \u4f8b\u5982\uff0c\u4e0b\u9762\u4ee3\u7801\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include class Fraction { private : // Removed initializers int m_numerator ; int m_denominator ; public : // Removed default-constructor int getNumerator () { return m_numerator ; } int getDenominator () { return m_denominator ; } double getValue () { return static_cast < double > ( m_numerator ) / m_denominator ; } }; int main () { Fraction frac ; // frac is uninitialized, accessing its members causes undefined behavior std :: cout << frac . getNumerator () << '/' << frac . getDenominator () << '\\n' ; return 0 ; } \u867d\u7136\u8fd9\u4e2a\u7c7b\u7684\u786e\u521d\u59cb\u5316\u4e86\u7c7b\u4e2d\u7684\u6240\u6709\u6210\u5458\uff0c\u4f46\u8981\u786e\u4fdd\u9879\u76ee\u4ee3\u7801\u4e2d\u7684\u6240\u6709\u7c7b\u90fd\u8fd9\u6837\u505a\u4e86\u662f\u5f88\u56f0\u96be\u7684\u3002 \u4f18\u5148\u5bf9\u7c7b\u5bf9\u8c61\u8fdb\u884c\u503c\u521d\u59cb\u5316\u662f\u7b80\u5355\u3001\u4e00\u81f4\u7684\uff0c\u5e76\u4e14\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6355\u83b7\u9519\u8bef\uff0c\u7279\u522b\u662f\u5728\u5b66\u4e60\u65f6\u3002 \u6700\u4f73\u5b9e\u8df5 \u521d\u59cb\u5316\u5bf9\u8c61\u65f6\uff0c\u63a8\u8350\u4f7f\u7528\u503c\u521d\u59cb\u5316\u800c\u975e\u9ed8\u8ba4\u521d\u59cb\u5316","title":"\u503c\u521d\u59cb\u5316"},{"location":"13-5-constructors/#_4","text":"\u5c3d\u7ba1\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u5e2e\u6211\u4eec\u786e\u4fdd\u7c7b\u6210\u5458\u5177\u6709\u5408\u7406\u7684\u521d\u503c\uff0c\u4f46\u662f\u66f4\u591a\u65f6\u5019\u6211\u4eec\u8fd8\u662f\u4f1a\u9700\u8981\u5728 \u5b9e\u4f8b\u5316 \u65f6\u6307\u5b9a\u4e00\u4e9b\u7279\u6b8a\u7684\u503c\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u6784\u9020\u51fd\u6570\u662f\u53ef\u4ee5\u88ab\u58f0\u660e\u4e3a\u5141\u8bb8\u63a5\u53d7\u53c2\u6570\u7684\u5f62\u5f0f\u7684\u3002\u4e0b\u9762\u4f8b\u5b50\u4e2d\u7684\u6784\u9020\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u6574\u578b\u53c2\u6570\u5206\u522b\u7528\u4e8e\u5bf9\u5206\u5b50\u5206\u6bcd\u8fdb\u884c\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : Fraction () // default constructor { m_numerator = 0 ; m_denominator = 1 ; } // Constructor with two parameters, one parameter having a default value Fraction ( int numerator , int denominator = 1 ) { assert ( denominator != 0 ); m_numerator = numerator ; m_denominator = denominator ; } int getNumerator () { return m_numerator ; } int getDenominator () { return m_denominator ; } double getValue () { return static_cast < double > ( m_numerator ) / m_denominator ; } }; \u6ce8\u610f\uff0c\u6211\u4eec\u73b0\u5728\u6709\u4e24\u4e2a\u6784\u9020\u51fd\u6570\uff1a\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff08\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u8c03\u7528\uff09\uff0c\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u5f62\u53c2\u3002C++\u652f\u6301 \u91cd\u8f7d \uff0c\u6240\u4ee5\u8fd9\u4e24\u4e2a\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u5728\u548c\u5e73\u5171\u5904\u3002\u4e8b\u5b9e\u4e0a\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5b9a\u4e49\u4efb\u610f\u6570\u91cf\u7684\u6784\u9020\u51fd\u6570\uff0c\u53ea\u8981\u6bcf\u4e2a\u6784\u9020\u51fd\u6570\u5177\u6709\u60df\u4e00\u7684\u7b7e\u540d(\u5f62\u53c2\u7684\u6570\u91cf\u548c\u7c7b\u578b)\u5373\u53ef\u3002 \u90a3\u4e48\uff0c\u6211\u4eec\u5e94\u8be5\u5982\u4f55\u8c03\u7528\u8fd9\u4e2a\u5177\u6709\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u5462\uff1f\u5f88\u7b80\u5355\uff0c\u4f7f\u7528 \u5217\u8868\u521d\u59cb\u5316 \u5373\u53ef\uff1a 1 2 Fraction fiveThirds { 5 , 3 }; // \u5217\u8868\u521d\u59cb\u5316\uff0c\u8c03\u7528 Fraction(int, int) Fraction threeQuarters ( 3 , 4 ); // \u76f4\u63a5\u521d\u59cb\u5316\uff0c\u4e5f\u662f\u8c03\u7528 Fraction(int, int) \u4e00\u5982\u65e2\u5f80\u5730\uff0c \u6211\u4eec\u66f4\u63a8\u8350\u5217\u8868\u521d\u59cb\u5316\u3002\u5728\u540e\u9762\u7684\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u4e5f\u4f1a\u9047\u5230\u9700\u8981\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\u7684\u60c5\u51b5(\u6a21\u677f\u548c std::initializer_list )\u3002\u8fd8\u6709\u53e6\u5916\u4e00\u79cd\u7279\u6b8a\u7684\u6784\u9020\u51fd\u6570\u4f1a\u4f7f\u7528\u62ec\u53f7\u521d\u59cb\u5316\uff08\u5217\u8868\u521d\u59cb\u5316\uff09\u505a\u4e00\u4e9b\u4e0d\u540c\u7684\u4e8b\u60c5\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\u3002 \u6ce8\u610f\uff0c\u7531\u4e8e\u6784\u9020\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a \u5f62\u53c2 \u5177\u6709\u9ed8\u8ba4\u503c\uff0c\u6240\u4ee5\u4e0b\u9762\u7684\u8c03\u7528\u65b9\u5f0f\u4e5f\u662f\u5408\u6cd5\u7684: 1 Fraction six { 6 }; // \u8c03\u7528 Fraction(int, int) \u6784\u9020\u51fd\u6570\uff0c\u7b2c\u4e8c\u4e2a\u5f62\u53c2\u4f1a\u4f7f\u7528\u9ed8\u8ba4\u503c1\u3002 \u6784\u9020\u51fd\u6570\u7684\u9ed8\u8ba4\u53c2\u6570\u548c\u666e\u901a\u51fd\u6570\u7684\u9ed8\u8ba4\u53c2\u6570\u6ca1\u6709\u4ec0\u4e48\u533a\u522b\uff0c\u6240\u4ee5\u5f53\u6211\u4eec\u8c03\u7528 six{ 6 } \u65f6\uff0c Fraction(int, int) \u51fd\u6570\u4f1a\u81ea\u52a8\u4f7f\u75281\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u5f62\u53c2\u7684\u503c\u3002 \u6700\u4f73\u5b9e\u8df5 \u63a8\u8350\u4f7f\u7528\u5927\u62ec\u53f7\u521d\u59cb\u5316\u6765\u521d\u59cb\u7c7b\u5bf9\u8c61\u3002","title":"\u4f7f\u7528\u5e26\u53c2\u6570\u7684\u51fd\u6570\u8fdb\u884c\u76f4\u63a5\u521d\u59cb\u5316\u548c\u5217\u8868\u521d\u59cb\u5316"},{"location":"13-5-constructors/#_5","text":"\u5c31\u50cf\u57fa\u672c\u53d8\u91cf\u4e00\u6837\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 \u62f7\u8d1d\u521d\u59cb\u5316 \u6765\u521d\u59cb\u5316\u7c7b\uff1a 1 2 Fraction six = Fraction { 6 }; // Copy initialize a Fraction, will call Fraction(6, 1) Fraction seven = 7 ; // Copy initialize a Fraction. The compiler will try to find a way to convert 7 to a Fraction, which will invoke the Fraction(7, 1) constructor. \u4e0d\u8fc7\uff0c\u6700\u597d\u4e0d\u8981\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u6765\u521d\u59cb\u5316\u7c7b\uff0c\u5b83\u53ef\u80fd\u662f\u4f4e\u6548\u7684\u3002\u5c3d\u7ba1 \u76f4\u63a5\u521d\u59cb\u5316 \u3001 \u5217\u8868\u521d\u59cb\u5316 \u548c \u62f7\u8d1d\u521d\u59cb\u5316 \u5728\u521d\u59cb\u5316\u57fa\u672c\u6570\u636e\u7c7b\u578b\u65f6\u7684\u884c\u4e3a\u662f\u4e00\u6837\u7684\uff0c\u4f46\u662f\u62f7\u8d1d\u521d\u59cb\u5316\u5728\u914d\u5408\u7c7b\u7c7b\u578b\u4f7f\u7528\u65f6\u5176\u884c\u4e3a\u662f\u4e0d\u540c\u7684\uff08\u5c3d\u7ba1\u7ed3\u679c\u76f8\u540c\uff09\u3002\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u7ae0\u8282\u4e2d\u8fdb\u884c\u66f4\u4e3a\u8be6\u7ec6\u7684\u4ecb\u7ecd\u3002","title":"\u4f7f\u7528\u7b49\u4e8e\u53f7\u8fdb\u884c\u62f7\u8d1d\u521d\u59cb\u5316"},{"location":"13-5-constructors/#_6","text":"\u50cf\u4e0a\u9762\u90a3\u6837\u4e3a Fraction \u58f0\u660e\u4e24\u4e2a\u6784\u9020\u51fd\u6570\u662f\u591a\u4f59\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) { assert ( denominator != 0 ); m_numerator = numerator ; m_denominator = denominator ; } int getNumerator () { return m_numerator ; } int getDenominator () { return m_denominator ; } double getValue () { return static_cast < double > ( m_numerator ) / m_denominator ; } }; \u5c3d\u7ba1\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u4ecd\u7136\u662f\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u4f46\u662f\u5b83\u73b0\u5728\u4e5f\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u57fa\u4e8e\u7528\u6237\u63d0\u4f9b\u7684\u53c2\u6570\u8fdb\u884c\u521d\u59cb\u5316\u3002 1 2 3 4 Fraction zero ; // will call Fraction(0, 1) Fraction zero {}; // will call Fraction(0, 1) Fraction six { 6 }; // will call Fraction(6, 1) Fraction fiveThirds { 5 , 3 }; // will call Fraction(5, 3) \u5728\u5b9e\u73b0\u6784\u9020\u51fd\u6570\u65f6\uff0c\u8981\u8003\u8651\u5982\u4f55\u901a\u8fc7\u9ed8\u8ba4\u503c\u6765\u51cf\u5c11\u6784\u9020\u51fd\u6570\u7684\u6570\u91cf\u3002","title":"\u51cf\u5c11\u6784\u9020\u51fd\u6570\u7684\u6570\u91cf"},{"location":"13-5-constructors/#_7","text":"\u5b9a\u4e49\u548c\u8c03\u7528\u5177\u6709\u9ed8\u8ba4\u53c2\u6570\u51fd\u6570\u7684\u89c4\u5219 (\u53c2\u89c1\uff1a 8.12 - \u9ed8\u8ba4\u53c2\u6570 ) \u540c\u6837\u4e5f\u9002\u7528\u4e8e\u6784\u9020\u51fd\u6570\u3002\u7b80\u5355\u590d\u4e60\u4e00\u4e0b\uff0c\u5728\u5b9a\u4e49\u5177\u6709\u9ed8\u8ba4\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u65f6\uff0c\u9ed8\u8ba4\u53c2\u6570\u5fc5\u987b\u5728\u975e\u9ed8\u8ba4\u53c2\u6570\u7684\u540e\u9762\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u975e\u9ed8\u8ba4\u53c2\u6570\u4e0d\u80fd\u88ab\u5b9a\u4e49\u5728\u9ed8\u8ba4\u53c2\u6570\u540e\u3002 \u5bf9\u4e8e\u5177\u6709\u591a\u4e2a\u9ed8\u8ba4\u53c2\u6570\uff0c\u4e14\u53c2\u6570\u7c7b\u578b\u4e0d\u540c\u7684\u6784\u9020\u51fd\u6570\uff0c\u4f7f\u7528\u4e0d\u5f53\u4f1a\u5e26\u6765\u51fa\u4e4e\u610f\u6599\u7684\u9519\u8bef\u3002\u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Something { public : // Default constructor Something ( int n = 0 , double d = 1.2 ) // allows us to construct a Something(int, double), Something(int), or Something() { } }; int main () { Something s1 { 1 , 2.4 }; // calls Something(int, double) Something s2 { 1 }; // calls Something(int, double) Something s3 {}; // calls Something(int, double) Something s4 { 2.4 }; // \u65e0\u6cd5\u7f16\u8bd1\uff0c\u56e0\u4e3a\u6ca1\u6709\u80fd\u5339\u914d\u7684\u6784\u9020\u51fd\u6570 Something(double) return 0 ; } \u5bf9\u4e8e s4 \uff0c\u6211\u4eec\u5e0c\u671b\u4f7f\u7528\u4e00\u4e2a double \u7c7b\u578b\u7684\u53c2\u6570\u6784\u9020 Something \u3002\u4e0a\u9762\u7a0b\u5e8f\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\uff0c\u56e0\u4e3a\u89c4\u5219\u4e0d\u5141\u8bb8\u6211\u4eec\u8df3\u8fc7\u9ed8\u8ba4\u53c2\u6570\u524d\u9762\u7684\u9ed8\u8ba4\u53c2\u6570\u4e3a\u540e\u9762\u7684\u9ed8\u8ba4\u53c2\u6570\u63d0\u4f9b\u5177\u4f53\u503c\uff08\u8fd9\u91cc\u5373\u6700\u5de6\u8fb9\u7684 int \u7c7b\u578b\u7684 \u5f62\u53c2 \u4e0d\u80fd\u88ab\u8df3\u8fc7\uff09\u3002 \u5982\u679c\u60f3\u57fa\u4e8e\u4e00\u4e2a double \u7c7b\u578b\u7684\u53d8\u91cf\u6784\u9020 Something \uff0c\u5219\u5fc5\u987b\u518d\u5b9a\u4e49\u4e00\u4e2a\uff08\u975e\u9ed8\u8ba4\uff09\u6784\u9020\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class Something { public : // Default constructor Something ( int n = 0 , double d = 1.2 ) // allows us to construct a Something(int, double), Something(int), or Something() { } Something ( double d ) { } }; int main () { Something s1 { 1 , 2.4 }; // calls Something(int, double) Something s2 { 1 }; // calls Something(int, double) Something s3 {}; // calls Something(int, double) Something s4 { 2.4 }; // calls Something(double) return 0 ; }","title":"\u5173\u4e8e\u9ed8\u8ba4\u53c2\u6570\u7684\u6ce8\u610f\u4e8b\u9879"},{"location":"13-5-constructors/#_8","text":"\u5982\u679c\u4f60\u7684\u7c7b\u6ca1\u6709\u5b9a\u4e49\u6784\u9020\u51fd\u6570\uff0c\u5219C++\u53ef\u4ee5\u81ea\u52a8\u4e3a\u4f60\u751f\u6210\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u88ab\u79f0\u4e3a \u9690\u5f0f\u6784\u9020\u51fd\u6570 \u3002 \u8003\u8651\u4e0b\u9762\u7684\u7c7b\u5b9a\u4e49\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Date { private : int m_year { 1900 }; int m_month { 1 }; int m_day { 1 }; // \u7528\u6237\u6ca1\u6709\u63d0\u4f9b\u6784\u9020\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u81ea\u52a8\u751f\u6210\u9ed8\u8ba4\u6784\u9020\u51fd\u6570 }; int main () { Date date {}; return 0 ; } Date \u7c7b\u6ca1\u6709\u6784\u9020\u51fd\u6570\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u4f1a\u4e3a\u5176\u521b\u5efa\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u8be5\u9690\u5f0f\u6784\u9020\u51fd\u6570\uff0c\u5728\u4e0d\u63d0\u4f9b\u53c2\u6570\u7684\u60c5\u51b5\u4e0b\u521b\u5efa Date \u5bf9\u8c61\u3002 \u5f53\u9690\u5f0f\u6784\u9020\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u5177\u6709\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u503c\u7684\u6210\u5458\u4f1a\u88ab\u521d\u59cb\u5316(\u53c2\u8003\uff1a 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 \u548c 13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u5217\u8868 )\u3002 \u53ea\u8981\u4f60\u7684\u7c7b\u4e2d\u6709\u6784\u9020\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u5c31\u4e0d\u4f1a\u4e3a\u4f60\u521b\u5efa\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 class Date { private : int m_year { 1900 }; int m_month { 1 }; int m_day { 1 }; public : Date ( int year , int month , int day ) // normal non-default constructor { m_year = year ; m_month = month ; m_day = day ; } // No implicit constructor provided because we already defined our own constructor }; int main () { Date date {}; // error: Can't instantiate object because default constructor doesn't exist and the compiler won't generate one Date today { 2020 , 1 , 19 }; // today is initialized to Jan 19th, 2020 return 0 ; } \u5982\u679c\u4f60\u7684\u7c7b\u5177\u6709\u5176\u4ed6\u6784\u9020\u51fd\u6570\uff0c\u4f46\u662f\u4f60\u9700\u8981\u7c7b\u652f\u6301\u9ed8\u8ba4\u521d\u59cb\u5316\uff0c\u5219\u53ef\u4ee5\u4e3a\u6784\u9020\u51fd\u6570\u7684\u6bcf\u4e2a\u5f62\u53c2\u6dfb\u52a0\u9ed8\u8ba4\u503c\uff0c\u6216\u8005\u663e\u5f0f\u5730\u5b9a\u4e49\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002 \u5f53\u7136\uff0c\u8fd8\u6709\u7b2c\u4e09\u79cd\u65b9\u5f0f\uff1a\u4f60\u53ef\u4ee5\u4f7f\u7528 default \u5173\u952e\u5b57\u544a\u77e5\u7f16\u8bd1\u5668\u751f\u6210\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 class Date { private : int m_year { 1900 }; int m_month { 1 }; int m_day { 1 }; public : // \u8981\u6c42\u7f16\u8bd1\u5668\u751f\u6210\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u5373\u4f7f // \u7528\u6237\u5df2\u7ecf\u63d0\u4f9b\u4e86\u6784\u9020\u51fd\u6570 Date () = default ; Date ( int year , int month , int day ) // \u666e\u901a\u975e\u9ed8\u8ba4\u6784\u9020\u51fd\u6570 { m_year = year ; m_month = month ; m_day = day ; } }; int main () { Date date {}; // date is initialized to Jan 1st, 1900 Date today { 2020 , 10 , 14 }; // today is initialized to Oct 14th, 2020 return 0 ; } \u5199 default \u6bd4\u5199\u4e00\u4e2a\u7a7a\u7684\u51fd\u6570\u4f53\u66f4\u8d39\u4e8b\uff0c\u4f46\u662f\u5b83\u80fd\u591f\u66f4\u597d\u5730\u8868\u660e\u4f60\u7684\u610f\u56fe\uff08\u521b\u5efa\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff09\uff0c\u800c\u4e14\u4e5f\u66f4\u5b89\u5168\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5bf9\u6ca1\u6709\u521d\u59cb\u5316\u503c\u7684\u51fd\u6570\u8fdb\u884c0\u521d\u59cb\u5316\u3002 default \u5bf9\u4e8e\u5176\u4ed6\u7279\u6b8a\u6784\u9020\u51fd\u6570\u4e5f\u6709\u6548\uff0c\u7b49\u6211\u4eec\u9047\u5230\u65f6\u518d\u8ba8\u8bba\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u7c7b\u5df2\u7ecf\u62e5\u6709\u6784\u9020\u51fd\u6570\uff0c\u4f46\u4f60\u4ecd\u7136\u9700\u8981\u4e00\u4e2a\u4ec0\u4e48\u90fd\u4e0d\u505a\u7684\u6784\u9020\u51fd\u6570(\u4f8b\u5982\uff0c\u6240\u6709\u6210\u5458\u51fd\u6570\u90fd\u901a\u8fc7\u975e\u9759\u6001\u6210\u5458\u51fd\u6570\u521d\u59cb\u5316\u8fdb\u884c\u4e86\u521d\u59cb\u5316)\uff0c\u53ef\u4ee5\u4f7f\u7528 = default\u3002","title":"\u9690\u5f0f\u751f\u6210\u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570"},{"location":"13-5-constructors/#_9","text":"class \u4e2d\u7684\u6210\u5458\u53d8\u91cf\uff0c\u4e5f\u53ef\u4ee5\u662f\u5176\u4ed6\u7c7b\u5bf9\u8c61\u3002 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5f53\u5916\u5c42\u5bf9\u8c61\u88ab\u521d\u59cb\u5316\u65f6\uff0c\u5185\u90e8\u7684\u8fd9\u4e9b\u5bf9\u8c61\u5176\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u4e5f\u4f1a\u88ab\u8c03\u7528\uff0c\u800c\u4e14\u4f1a\u5728\u6267\u884c\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u4e4b\u524d\u53d1\u751f\u3002 \u4e3e\u4f8b\u6765\u8bf4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include class A { public : A () { std :: cout << \"A \\n \" ; } }; class B { private : A m_a ; // A \u662f B \u7684\u4e00\u4e2a\u6210\u5458 public : B () { std :: cout << \"B \\n \" ; } }; int main () { B b ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 A B \u5f53\u53d8\u91cf b \u88ab\u6784\u5efa\u65f6\uff0c\u6784\u9020\u51fd\u6570 B() \u4f1a\u88ab\u8c03\u7528\uff0c\u5728 B() \u7684\u51fd\u6570\u4f53\u6267\u884c\u524d\uff0c m_a \u5c31\u5f00\u59cb\u521d\u59cb\u5316\u4e86\uff0c\u6b64\u65f6\u4f1a\u8c03\u7528 A \u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u5e76\u6253\u5370 \"A\"\u3002\u7136\u540e\u63a7\u5236\u6743\u8fd4\u8fd8\u7ed9\u6784\u9020\u51fd\u6570 B() \u5e76\u6267\u884c\u5176\u51fd\u6570\u4f53\u3002 \u8fd9\u4e48\u505a\u662f\u6709\u9053\u7406\u7684\uff0c\u56e0\u4e3a\u6784\u9020 B() \u662f\u6709\u53ef\u80fd\u4f1a\u53bb\u4f7f\u7528\u6210\u5458\u53d8\u91cf m_a \u7684\uff0c\u6240\u4ee5\u5b83\u5e94\u8be5\u9996\u5148\u88ab\u521d\u59cb\u5316\u3002 \u4e0a\u9762\u6700\u540e\u4e00\u4e2a\u4f8b\u5b50\u4e2d\uff0c m_a \u662f\u4e00\u4e2aclass\u7c7b\u578b\u7684\u53d8\u91cf\u3002 \u5bf9\u4e8eclass\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u5373\u4f7f\u6211\u4eec\u6ca1\u6709\u663e\u5f0f\u5730\u521d\u59cb\u5316\u5b83\u4eec\uff0c\u5b83\u4e5f\u4f1a\u88ab\u521d\u59cb\u5316\u3002 \u5728\u4e0b\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba\u5982\u4f55\u521d\u59cb\u5316\u8fd9\u4e9bclass\u7c7b\u578b\u7684\u6210\u5458\u53d8\u91cf\u3002","title":"\u5305\u542b\u7c7b\u7c7b\u578b\u6210\u5458\u7684\u7c7b"},{"location":"13-5-constructors/#_10","text":"\u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u4f1a\u6709\u8fd9\u6837\u7684\u56f0\u60d1\uff1a\u6784\u9020\u51fd\u6570\u4f1a\u521b\u5efa\u5bf9\u8c61\u5417\uff1f\u5b9e\u9645\u4e0a\u6784\u9020\u51fd\u6570\u5e76\u4e0d\u4f1a\u521b\u5efa\u5bf9\u8c61\u2014\u2014\u7f16\u8bd1\u5668\u4f1a\u5728\u6784\u9020\u51fd\u6570\u8c03\u7528\u524d\u5c31\u4e3a\u5bf9\u8c61\u5206\u914d\u5185\u5b58\u3002 \u6784\u9020\u51fd\u6570\u5b9e\u9645\u4e0a\u6709\u4e24\u4e2a\u76ee\u7684\uff1a \u6784\u9020\u51fd\u6570\u53ef\u4ee5\u51b3\u5b9a\u8c01\u80fd\u591f\u521b\u5efa\u8fd9\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u53ea\u6709\u5f53\u80fd\u591f\u627e\u5230\u5339\u914d\u7684\u6784\u9020\u51fd\u6570\u65f6\uff0c\u8be5\u7c7b\u578b\u7684\u5bf9\u8c61\u624d\u80fd\u591f\u88ab\u521b\u5efa\u3002 \u6784\u9020\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u521d\u59cb\u5316\u5bf9\u8c61\u3002\u81f3\u4e8e\u6784\u9020\u51fd\u6570\u662f\u5426\u4f1a\u8fdb\u884c\u5b9e\u9645\u7684\u521d\u59cb\u5316\u5de5\u4f5c\uff0c\u8fd9\u53d6\u51b3\u4e8e\u7a0b\u5e8f\u5458\u3002\u6784\u9020\u51fd\u6570\u4e0d\u8fdb\u884c\u4efb\u4f55\u7684\u521d\u59cb\u5316\u5de5\u4f5c\uff0c\u4ece\u8bed\u6cd5\u5c42\u9762\u6765\u8bb2\u4e5f\u662f\u5408\u6cd5\u7684\u3002 \u7136\u800c\uff0c\u5c31\u50cf\u521d\u59cb\u5316\u6240\u6709\u5c40\u90e8\u53d8\u91cf\u662f\u6700\u4f73\u5b9e\u8df5\u4e00\u6837\uff0c\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u521d\u59cb\u5316\u6240\u6709\u6210\u5458\u53d8\u91cf\u4e5f\u662f\u6700\u4f73\u5b9e\u8df5\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u6784\u9020\u51fd\u6570\u6216\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u6765\u5b8c\u6210\u3002 \u6700\u4f73\u5b9e\u8df5 \u6c38\u8fdc\u8981\u8bb0\u5f97\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u5168\u90e8\u6210\u5458\u53d8\u91cf\u3002 \u6700\u540e\uff0c\u6784\u9020\u51fd\u6570\u4ec5\u7528\u4e8e\u521b\u5efa\u5bf9\u8c61\u65f6\u7684\u521d\u59cb\u5316\u3002\u4e0d\u8981\u4f7f\u7528\u6784\u9020\u51fd\u6570\u6765\u91cd\u65b0\u521d\u59cb\u5316\u73b0\u6709\u5bf9\u8c61\u3002\u867d\u7136\u53ef\u4ee5\u7f16\u8bd1\uff0c\u4f46\u7ed3\u679c\u4e0d\u4f1a\u662f\u4f60\u60f3\u8981\u7684(\u76f8\u53cd\uff0c\u7f16\u8bd1\u5668\u5c06\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff0c\u7136\u540e\u4e22\u5f03\u5b83)\u3002","title":"\u6784\u9020\u51fd\u6570\u5c0f\u7ed3"},{"location":"13-6-constructor-member-initializer-lists/","tags":["constructors","initialization"],"text":"\u5728\u4e0a\u4e00\u8282\u8bfe\u4e2d\uff0c\u4e3a\u4e86\u7b80\u5316\u95ee\u9898\uff0c\u6211\u4eec\u5728\u6784\u9020\u51fd\u6570\u4e2d\u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\u5bf9\u6210\u5458\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Something { private : int m_value1 {}; double m_value2 {}; char m_value3 {}; public : Something () { // \u90fd\u5c5e\u4e8e\u8d4b\u503c\uff0c\u800c\u4e0d\u662f\u521d\u59cb\u5316 m_value1 = 1 ; m_value2 = 2.2 ; m_value3 = 'c' ; } }; \u5f53\u6784\u9020\u51fd\u6570\u6267\u884c\u65f6\uff0c m_value1 \uff0c m_value2 \u548c m_value3 \u9996\u5148\u88ab\u521b\u5efa\u3002\u7136\u540e\u6267\u884c\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\uff0c\u5bf9\u6210\u5458\u53d8\u91cf\u8fdb\u884c\u8d4b\u503c\u3002\u4e0a\u9762\u7684\u8fc7\u7a0b\u548c\u4e4b\u524d\u7684\u975e\u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u662f\u975e\u5e38\u7c7b\u4f3c\uff1a 1 2 3 4 5 6 7 int m_value1 {}; double m_value2 {}; char m_value3 {}; m_value1 = 1 ; m_value2 = 2.2 ; m_value3 = 'c' ; \u5c3d\u7ba1\u5408\u4e4e\u8bed\u6cd5\uff0c\u4f46\u662f\u4e0a\u8ff0\u4ee3\u7801\u5e76\u4e0d\u5177\u6709\u826f\u597d\u7684\u7f16\u7a0b\u98ce\u683c\uff08\u800c\u4e14\u6548\u7387\u76f8\u8f83\u4e8e\u521d\u59cb\u5316\u662f\u4e0d\u4f73\u7684\uff09\u3002 \u4e0d\u8fc7\uff0c\u5728\u524d\u9762\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u63d0\u5230\u8fc7\uff0c\u6709\u4e9b\u7c7b\u578b\u7684\u53d8\u91cf\uff08\u4f8b\u5982const\u53d8\u91cf\u6216 \u5f15\u7528 \uff09\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 class Something { private : const int m_value ; public : Something () { m_value = 1 ; // \u9519\u8bef: const \u53d8\u91cf\u4e0d\u80fd\u88ab\u8d4b\u503c } }; \u4e0a\u8ff0\u4ee3\u7801\u7b49\u4ef7\u4e8e\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 const int m_value ; // error: const vars must be initialized with a value m_value = 5 ; // error: const vars can not be assigned to \u5728\u6784\u9020\u51fd\u6570\u5185\u90e8\u5bf9 const \u53d8\u91cf\u6216\u5f15\u7528\u8d4b\u503c\u663e\u7136\u4e0d\u662f\u4e00\u4e2a\u53ef\u884c\u7684\u529e\u6cd5\u3002 \u6210\u5458\u521d\u59cb\u5316\u5217\u8868 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cC++ \u63d0\u4f9b\u4e86\u4e00\u79cd\u5bf9\u6210\u5458\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\uff08\u800c\u4e0d\u662f\u5728\u521b\u5efa\u540e\u8d4b\u503c\uff09\u7684\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u3002\u8bf7\u4e0d\u8981\u628a\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u548c\u7528\u4e8e\u5bf9\u6570\u7ec4\u8fdb\u884c\u8d4b\u503c\u7684 \u521d\u59cb\u5316\u503c\u5217\u8868 \u641e\u6df7\u3002 \u5728 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u521d\u59cb\u5316\u53d8\u91cf\u7684\u65b9\u5f0f\u6709\u4e09\u79cd\uff1a \u62f7\u8d1d\u521d\u59cb\u5316 \u3001 \u76f4\u63a5\u521d\u59cb\u5316 \u548c \u7edf\u4e00\u521d\u59cb\u5316 \u3002 1 2 3 int value1 = 1 ; // copy initialization double value2 ( 2.2 ); // direct initialization char value3 { 'c' }; // uniform initialization \u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u8fdb\u884c\u521d\u59cb\u5316\u57fa\u672c\u4e0a\u548c \u76f4\u63a5\u521d\u59cb\u5316 \u6216 \u7edf\u4e00\u521d\u59cb\u5316 \u662f\u5b8c\u5168\u4e00\u81f4\u7684\u3002 \u4e3e\u4e2a\u4f8b\u5b50\u5c31\u66f4\u5bb9\u6613\u7406\u89e3\u4e86\u3002\u8fd8\u8bb0\u5f97\u4e4b\u524d\u4e3a\u53d8\u91cf\u8d4b\u503c\u7684\u6784\u9020\u51fd\u6570\u5417\uff1f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Something { private : int m_value1 {}; double m_value2 {}; char m_value3 {}; public : Something () { // \u90fd\u662f\u8d4b\u503c\uff0c\u800c\u4e0d\u662f\u521d\u59cb\u5316 m_value1 = 1 ; m_value2 = 2.2 ; m_value3 = 'c' ; } }; \u63a5\u4e0b\u6765\uff0c\u91cd\u5199\u51fd\u6570\uff0c\u4f7f\u7528\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u6765\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include class Something { private : int m_value1 {}; double m_value2 {}; char m_value3 {}; public : Something () : m_value1 { 1 }, m_value2 { 2.2 }, m_value3 { 'c' } // Initialize our member variables { // No need for assignment here } void print () { std :: cout << \"Something(\" << m_value1 << \", \" << m_value2 << \", \" << m_value3 << \") \\n \" ; } }; int main () { Something something {}; something . print (); return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Something(1, 2.2, c) \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u4f4d\u4e8e\u6784\u9020\u51fd\u6570\u53c2\u6570\u5217\u8868\u540e\uff0c\u4ee5\u5192\u53f7 : \u5f00\u5934\uff0c\u540e\u9762\u662f\u4e00\u7cfb\u5217\u53d8\u91cf\u548c\u5b83\u4eec\u7684\u521d\u59cb\u5316\u503c\uff08\u4f7f\u7528\u9017\u53f7\u5206\u9694\uff09\u3002 \u6ce8\u610f\uff0c\u51fd\u6570\u4f53\u5185\u4e0d\u518d\u9700\u8981\u5bf9\u53d8\u91cf\u8fdb\u884c\u8d4b\u503c\uff0c\u56e0\u4e3a\u521d\u59cb\u5316\u7684\u5de5\u4f5c\u5df2\u7ecf\u7531\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u5b8c\u6210\u3002\u540c\u65f6\u8fd8\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u540e\u9762\u5e76\u6ca1\u6709\u5206\u53f7\u3002 \u5f53\u7136\uff0c\u5982\u679c\u80fd\u591f\u901a\u8fc7\u6784\u9020\u51fd\u6570\u6765\u4f20\u9012\u521d\u59cb\u5316\u503c\u5219\u4f1a\u66f4\u6709\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include class Something { private : int m_value1 {}; double m_value2 {}; char m_value3 {}; public : Something ( int value1 , double value2 , char value3 = 'c' ) : m_value1 { value1 }, m_value2 { value2 }, m_value3 { value3 } // \u76f4\u63a5\u521d\u59cb\u5316\u6210\u5458\u53d8\u91cf { // No need for assignment here } void print () { std :: cout << \"Something(\" << m_value1 << \", \" << m_value2 << \", \" << m_value3 << \") \\n \" ; } }; int main () { Something something { 1 , 2.2 }; // value1 = 1, value2=2.2, value3 \u4f7f\u7528\u9ed8\u8ba4\u503c 'c' something . print (); return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Something(1, 2.2, c) \u6ce8\u610f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u9ed8\u8ba4\u5f62\u53c2\u6765\u63d0\u4f9b\u9ed8\u8ba4\u503c\uff0c\u9632\u6b62\u7528\u6237\u6ca1\u6709\u4f20\u9012\u53c2\u6570\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u5bf9\u6210\u5458\u8fdb\u884c\u521d\u59cb\u5316\u800c\u4e0d\u662f\u4f9d\u6b21\u4e3a\u5176\u8d4b\u503c\u3002 \u521d\u59cb\u5316 const \u7c7b\u578b\u6210\u5458\u53d8\u91cf \u7c7b\u53ef\u4ee5\u5305\u542b const \u7c7b\u578b\u7684\u6210\u5458\u53d8\u91cf\u3002const \u6210\u5458\u53d8\u91cf\u548c\u4e00\u822c\u7684 const \u53d8\u91cf\u6ca1\u4ec0\u4e48\u533a\u522b\u2014\u2014\u5b83\u4eec\u90fd\u5fc5\u987b\u88ab\u521d\u59cb\u5316\uff0c\u521d\u59cb\u5316\u540e\u5176\u503c\u5c06\u4e0d\u80fd\u88ab\u6539\u53d8\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6784\u9020\u51fd\u6570\u7684\u521d\u59cb\u5316\u5217\u8868\u5bf9 const \u7c7b\u578b\u7684\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\uff08\u548c\u975econst\u6210\u5458\u4e00\u6837\uff09\uff0c\u800c\u4e14\u521d\u59cb\u5316\u503c\u53ef\u4ee5\u662f\u5e38\u6570\u4e5f\u53ef\u4ee5\u4e0d\u662f\u3002 \u4e0b\u4f8b\u4e2d\u7684\u7c7b\uff0c\u5305\u542b\u4e00\u4e2a const \u7c7b\u578b\u7684\u53d8\u91cf\u3002\u6211\u4eec\u4f7f\u7528\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\uff08\u4f7f\u7528\u7528\u6237\u63d0\u4f9b\u7684\u975econst\u503c\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include class Something { private : const int m_value ; public : Something ( int x ) : m_value { x } // directly initialize our const member variable { } void print () { std :: cout << \"Something(\" << m_value << \") \\n \" ; } }; int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; Something s { x }; s . print (); return 0 ; } \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\uff1a 1 2 Enter an integer: 4 Something(4) \u6cd5\u5219 Const \u6210\u5458\u53d8\u91cf\u5fc5\u987b\u88ab\u521d\u59cb\u5316\u3002 \u4f7f\u7528\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u521d\u59cb\u5316\u6570\u7ec4\u7c7b\u578b\u6210\u5458 \u4e0b\u9762\u4f8b\u5b50\u4e2d\u7684\u7c7b\u5305\u542b\u4e00\u4e2a\u6570\u7ec4\u7c7b\u578b\u7684\u6210\u5458\uff1a 1 2 3 4 5 6 class Something { private : const int m_array [ 5 ]; }; \u5728 C++11 \u4e4b\u524d\uff0c\u4f60\u53ea\u80fd\u901a\u8fc7\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\uff0c\u5bf9\u6570\u7ec4\u6210\u5458\u8fdb\u884c0\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 class Something { private : const int m_array [ 5 ]; public : Something () : m_array {} // zero initialize the member array { } }; \u4e0d\u8fc7\uff0c\u4eceC++11\u5f00\u59cb\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 \u7edf\u4e00\u521d\u59cb\u5316 \u5bf9\u6570\u7ec4\u6210\u5458\u8fdb\u884c\u521d\u59cb\u5316\u4e86\uff1a 1 2 3 4 5 6 7 8 9 10 11 class Something { private : const int m_array [ 5 ]; public : Something () : m_array { 1 , 2 , 3 , 4 , 5 } // use uniform initialization to initialize our member array { } }; \u521d\u59cb\u5316\u7c7b\u7c7b\u578b\u7684\u6210\u5458\u53d8\u91cf \u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e5f\u53ef\u7528\u4e8e\u7c7b\u7c7b\u578b\u6210\u5458\u53d8\u91cf\u7684\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include class A { public : A ( int x = 0 ) { std :: cout << \"A \" << x << '\\n' ; } }; class B { private : A m_a {}; public : B ( int y ) : m_a { y - 1 } // call A(int) constructor to initialize member m_a { std :: cout << \"B \" << y << '\\n' ; } }; int main () { B b { 5 }; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 A 4 B 5 \u5f53\u6784\u9020\u53d8\u91cfb\u65f6\uff0c\u7528\u503c5\u8c03\u7528 B(int) \u6784\u9020\u51fd\u6570\u3002\u5728\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u6267\u884c\u4e4b\u524d\uff0c m_a \u88ab\u521d\u59cb\u5316\uff0c\u8c03\u7528\u503c\u4e3a4\u7684 A(int) \u6784\u9020\u51fd\u6570\uff0c\u6253\u5370\u51fa\u201cA 4\u201d\u3002\u7136\u540e\u63a7\u5236\u8fd4\u56de\u5230B\u6784\u9020\u51fd\u6570\uff0c\u6267\u884cB\u6784\u9020\u51fd\u6570\u7684\u4e3b\u4f53\uff0c\u6253\u5370\u201cB 5\u201d\u3002 \u521d\u59cb\u5316\u5217\u8868\u6392\u7248 C++ \u5141\u8bb8\u7a0b\u5e8f\u5458\u7075\u6d3b\u5730\u6392\u7248\u521d\u59cb\u5316\u503c\u5217\u8868\u3002\u867d\u7136\u4f60\u4e5f\u53ef\u6309\u7167\u81ea\u5df1\u7684\u559c\u597d\u6392\u7248\uff0c\u4f46\u662f\u6211\u4eec\u63a8\u8350\u4ee5\u4e0b\u7684\u65b9\u5f0f\uff1a \u5982\u679c\u51fd\u6570\u540d\u540c\u4e00\u884c\u4e2d\u53ef\u4ee5\u653e\u5f97\u4e0b\u521d\u59cb\u5316\u503c\u5217\u8868\uff0c\u5219\u5c06\u5b83\u4eec\u90fd\u653e\u5728\u8fd9\u4e00\u884c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class Something { private : int m_value1 {}; double m_value2 {}; char m_value3 {}; public : Something () : m_value1 { 1 }, m_value2 { 2.2 }, m_value3 { 'c' } // everything on one line { } }; \u5982\u679c\u653e\u4e0d\u4e0b\uff0c\u5219\u5728\u4e0b\u4e00\u884c\u4e2d\u7f29\u8fdb\u653e\u7f6e\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Something { private : int m_value1 ; double m_value2 ; char m_value3 ; public : Something ( int value1 , double value2 , char value3 = 'c' ) // \u51fd\u6570\u540d\u8fd9\u4e00\u884c\u5df2\u7ecf\u591f\u957f\u4e86 : m_value1 { value1 }, m_value2 { value2 }, m_value3 { value3 } // \u6240\u4ee5\u5c06\u521d\u59cb\u5316\u5217\u8868\u653e\u5728\u4e0b\u4e00\u884c { } }; \u5982\u679c\u8fd9\u6837\u4e5f\u4e0d\u80fd\u5c06\u521d\u59cb\u5316\u503c\u5217\u8868\u653e\u5728\u4e00\u884c\uff0c\u5219\u901a\u8fc7\u7a7a\u683c\u6392\u7248\uff0c\u6bcf\u884c\u4e00\u4e2a\u521d\u59cb\u5316\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class Something { private : int m_value1 {}; double m_value2 {}; char m_value3 {}; float m_value4 {}; public : Something ( int value1 , double value2 , char value3 = 'c' , float value4 = 34.6f ) // this line already has a lot of stuff on it : m_value1 { value1 } // one per line , m_value2 { value2 } , m_value3 { value3 } , m_value4 { value4 } { } }; \u521d\u59cb\u5316\u5217\u8868\u7684\u987a\u5e8f \u4ee4\u4eba\u60ca\u8bb6\u7684\u662f\uff0c\u521d\u59cb\u5316\u5217\u8868\u4e2d\u7684\u53d8\u91cf\u5e76\u4e0d\u4f1a\u6309\u7167\u5b83\u4eec\u5728\u521d\u59cb\u5316\u5217\u8868\u4e2d\u6307\u5b9a\u7684\u987a\u5e8f\u521d\u59cb\u5316\u3002\u5b9e\u9645\u4e0a\uff0c\u5b83\u4eec\u5c06\u6309\u7167\u5728\u53d8\u91cf\u5728\u7c7b\u4e2d\u58f0\u660e\u7684\u987a\u5e8f\u8fdb\u884c\u521d\u59cb\u5316\u3002 \u4e3a\u4e86\u53d6\u5f97\u6700\u4f73\u7684\u6548\u679c\uff0c\u8bf7\u9075\u5faa\u4ee5\u4e0b\u5efa\u8bae\uff1a \u521d\u59cb\u5316\u6210\u5458\u53d8\u91cf\uff0c\u4e0d\u8981\u4f7f\u5b83\u4eec\u4f9d\u8d56\u4e8e\u5176\u4ed6\u6210\u5458\u53d8\u91cf\u7684\u521d\u59cb\u5316(\u6362\u53e5\u8bdd\u8bf4\uff0c\u786e\u4fdd\u5373\u4f7f\u521d\u59cb\u5316\u987a\u5e8f\u4e0d\u540c\uff0c\u4e5f\u80fd\u6b63\u786e\u521d\u59cb\u5316\u6210\u5458\u53d8\u91cf)\u3002 \u6309\u7167\u5728\u7c7b\u4e2d\u58f0\u660e\u53d8\u91cf\u7684\u76f8\u540c\u987a\u5e8f\u7f16\u5199\u521d\u59cb\u5316\u5217\u8868\u3002\u53ea\u8981\u9075\u5faa\u4e86\u524d\u9762\u7684\u5efa\u8bae\uff0c\u8fd9\u4e0d\u662f\u4e25\u683c\u8981\u6c42\u7684\uff0c\u4f46\u662f\u5982\u679c\u4f60\u4e0d\u8fd9\u6837\u505a\uff0c\u5e76\u4e14\u6253\u5f00\u4e86\u6240\u6709\u7684\u8b66\u544a\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u4ea7\u751f\u8b66\u544a\u3002 \u5c0f\u7ed3 \u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u5bf9\u7c7b\u6210\u5458\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u800c\u4e0d\u662f\u4e3a\u5b83\u4eec\u8d4b\u503c\u3002\u8fd9\u662f\u521d\u59cb\u5316\u67d0\u4e9b\u7c7b\u578b\u53d8\u91cf\u7684(\u5982const\u6216reference\u6210\u5458)\u7684\u552f\u4e00\u65b9\u6cd5\uff0c\u800c\u4e14\u5b83\u6bd4\u5728\u6784\u9020\u51fd\u6570\u4f53\u4e2d\u8d4b\u503c\u6027\u80fd\u66f4\u597d\u3002\u6210\u5458\u521d\u59cb\u5316\u5668\u5217\u8868\u65e2\u9002\u7528\u4e8e\u57fa\u672c\u7c7b\u578b\uff0c\u4e5f\u9002\u7528\u4e8e\u4f5c\u4e3a\u7c7b\u672c\u8eab\u7684\u6210\u5458\u3002","title":"13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868"},{"location":"13-6-constructor-member-initializer-lists/#_1","text":"\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cC++ \u63d0\u4f9b\u4e86\u4e00\u79cd\u5bf9\u6210\u5458\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\uff08\u800c\u4e0d\u662f\u5728\u521b\u5efa\u540e\u8d4b\u503c\uff09\u7684\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u3002\u8bf7\u4e0d\u8981\u628a\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u548c\u7528\u4e8e\u5bf9\u6570\u7ec4\u8fdb\u884c\u8d4b\u503c\u7684 \u521d\u59cb\u5316\u503c\u5217\u8868 \u641e\u6df7\u3002 \u5728 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u521d\u59cb\u5316\u53d8\u91cf\u7684\u65b9\u5f0f\u6709\u4e09\u79cd\uff1a \u62f7\u8d1d\u521d\u59cb\u5316 \u3001 \u76f4\u63a5\u521d\u59cb\u5316 \u548c \u7edf\u4e00\u521d\u59cb\u5316 \u3002 1 2 3 int value1 = 1 ; // copy initialization double value2 ( 2.2 ); // direct initialization char value3 { 'c' }; // uniform initialization \u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u8fdb\u884c\u521d\u59cb\u5316\u57fa\u672c\u4e0a\u548c \u76f4\u63a5\u521d\u59cb\u5316 \u6216 \u7edf\u4e00\u521d\u59cb\u5316 \u662f\u5b8c\u5168\u4e00\u81f4\u7684\u3002 \u4e3e\u4e2a\u4f8b\u5b50\u5c31\u66f4\u5bb9\u6613\u7406\u89e3\u4e86\u3002\u8fd8\u8bb0\u5f97\u4e4b\u524d\u4e3a\u53d8\u91cf\u8d4b\u503c\u7684\u6784\u9020\u51fd\u6570\u5417\uff1f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Something { private : int m_value1 {}; double m_value2 {}; char m_value3 {}; public : Something () { // \u90fd\u662f\u8d4b\u503c\uff0c\u800c\u4e0d\u662f\u521d\u59cb\u5316 m_value1 = 1 ; m_value2 = 2.2 ; m_value3 = 'c' ; } }; \u63a5\u4e0b\u6765\uff0c\u91cd\u5199\u51fd\u6570\uff0c\u4f7f\u7528\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u6765\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include class Something { private : int m_value1 {}; double m_value2 {}; char m_value3 {}; public : Something () : m_value1 { 1 }, m_value2 { 2.2 }, m_value3 { 'c' } // Initialize our member variables { // No need for assignment here } void print () { std :: cout << \"Something(\" << m_value1 << \", \" << m_value2 << \", \" << m_value3 << \") \\n \" ; } }; int main () { Something something {}; something . print (); return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Something(1, 2.2, c) \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u4f4d\u4e8e\u6784\u9020\u51fd\u6570\u53c2\u6570\u5217\u8868\u540e\uff0c\u4ee5\u5192\u53f7 : \u5f00\u5934\uff0c\u540e\u9762\u662f\u4e00\u7cfb\u5217\u53d8\u91cf\u548c\u5b83\u4eec\u7684\u521d\u59cb\u5316\u503c\uff08\u4f7f\u7528\u9017\u53f7\u5206\u9694\uff09\u3002 \u6ce8\u610f\uff0c\u51fd\u6570\u4f53\u5185\u4e0d\u518d\u9700\u8981\u5bf9\u53d8\u91cf\u8fdb\u884c\u8d4b\u503c\uff0c\u56e0\u4e3a\u521d\u59cb\u5316\u7684\u5de5\u4f5c\u5df2\u7ecf\u7531\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u5b8c\u6210\u3002\u540c\u65f6\u8fd8\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u540e\u9762\u5e76\u6ca1\u6709\u5206\u53f7\u3002 \u5f53\u7136\uff0c\u5982\u679c\u80fd\u591f\u901a\u8fc7\u6784\u9020\u51fd\u6570\u6765\u4f20\u9012\u521d\u59cb\u5316\u503c\u5219\u4f1a\u66f4\u6709\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include class Something { private : int m_value1 {}; double m_value2 {}; char m_value3 {}; public : Something ( int value1 , double value2 , char value3 = 'c' ) : m_value1 { value1 }, m_value2 { value2 }, m_value3 { value3 } // \u76f4\u63a5\u521d\u59cb\u5316\u6210\u5458\u53d8\u91cf { // No need for assignment here } void print () { std :: cout << \"Something(\" << m_value1 << \", \" << m_value2 << \", \" << m_value3 << \") \\n \" ; } }; int main () { Something something { 1 , 2.2 }; // value1 = 1, value2=2.2, value3 \u4f7f\u7528\u9ed8\u8ba4\u503c 'c' something . print (); return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Something(1, 2.2, c) \u6ce8\u610f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u9ed8\u8ba4\u5f62\u53c2\u6765\u63d0\u4f9b\u9ed8\u8ba4\u503c\uff0c\u9632\u6b62\u7528\u6237\u6ca1\u6709\u4f20\u9012\u53c2\u6570\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u5bf9\u6210\u5458\u8fdb\u884c\u521d\u59cb\u5316\u800c\u4e0d\u662f\u4f9d\u6b21\u4e3a\u5176\u8d4b\u503c\u3002","title":"\u6210\u5458\u521d\u59cb\u5316\u5217\u8868"},{"location":"13-6-constructor-member-initializer-lists/#const","text":"\u7c7b\u53ef\u4ee5\u5305\u542b const \u7c7b\u578b\u7684\u6210\u5458\u53d8\u91cf\u3002const \u6210\u5458\u53d8\u91cf\u548c\u4e00\u822c\u7684 const \u53d8\u91cf\u6ca1\u4ec0\u4e48\u533a\u522b\u2014\u2014\u5b83\u4eec\u90fd\u5fc5\u987b\u88ab\u521d\u59cb\u5316\uff0c\u521d\u59cb\u5316\u540e\u5176\u503c\u5c06\u4e0d\u80fd\u88ab\u6539\u53d8\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6784\u9020\u51fd\u6570\u7684\u521d\u59cb\u5316\u5217\u8868\u5bf9 const \u7c7b\u578b\u7684\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\uff08\u548c\u975econst\u6210\u5458\u4e00\u6837\uff09\uff0c\u800c\u4e14\u521d\u59cb\u5316\u503c\u53ef\u4ee5\u662f\u5e38\u6570\u4e5f\u53ef\u4ee5\u4e0d\u662f\u3002 \u4e0b\u4f8b\u4e2d\u7684\u7c7b\uff0c\u5305\u542b\u4e00\u4e2a const \u7c7b\u578b\u7684\u53d8\u91cf\u3002\u6211\u4eec\u4f7f\u7528\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\uff08\u4f7f\u7528\u7528\u6237\u63d0\u4f9b\u7684\u975econst\u503c\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include class Something { private : const int m_value ; public : Something ( int x ) : m_value { x } // directly initialize our const member variable { } void print () { std :: cout << \"Something(\" << m_value << \") \\n \" ; } }; int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; Something s { x }; s . print (); return 0 ; } \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\uff1a 1 2 Enter an integer: 4 Something(4) \u6cd5\u5219 Const \u6210\u5458\u53d8\u91cf\u5fc5\u987b\u88ab\u521d\u59cb\u5316\u3002","title":"\u521d\u59cb\u5316 const \u7c7b\u578b\u6210\u5458\u53d8\u91cf"},{"location":"13-6-constructor-member-initializer-lists/#_2","text":"\u4e0b\u9762\u4f8b\u5b50\u4e2d\u7684\u7c7b\u5305\u542b\u4e00\u4e2a\u6570\u7ec4\u7c7b\u578b\u7684\u6210\u5458\uff1a 1 2 3 4 5 6 class Something { private : const int m_array [ 5 ]; }; \u5728 C++11 \u4e4b\u524d\uff0c\u4f60\u53ea\u80fd\u901a\u8fc7\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\uff0c\u5bf9\u6570\u7ec4\u6210\u5458\u8fdb\u884c0\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 class Something { private : const int m_array [ 5 ]; public : Something () : m_array {} // zero initialize the member array { } }; \u4e0d\u8fc7\uff0c\u4eceC++11\u5f00\u59cb\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 \u7edf\u4e00\u521d\u59cb\u5316 \u5bf9\u6570\u7ec4\u6210\u5458\u8fdb\u884c\u521d\u59cb\u5316\u4e86\uff1a 1 2 3 4 5 6 7 8 9 10 11 class Something { private : const int m_array [ 5 ]; public : Something () : m_array { 1 , 2 , 3 , 4 , 5 } // use uniform initialization to initialize our member array { } };","title":"\u4f7f\u7528\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u521d\u59cb\u5316\u6570\u7ec4\u7c7b\u578b\u6210\u5458"},{"location":"13-6-constructor-member-initializer-lists/#_3","text":"\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e5f\u53ef\u7528\u4e8e\u7c7b\u7c7b\u578b\u6210\u5458\u53d8\u91cf\u7684\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include class A { public : A ( int x = 0 ) { std :: cout << \"A \" << x << '\\n' ; } }; class B { private : A m_a {}; public : B ( int y ) : m_a { y - 1 } // call A(int) constructor to initialize member m_a { std :: cout << \"B \" << y << '\\n' ; } }; int main () { B b { 5 }; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 A 4 B 5 \u5f53\u6784\u9020\u53d8\u91cfb\u65f6\uff0c\u7528\u503c5\u8c03\u7528 B(int) \u6784\u9020\u51fd\u6570\u3002\u5728\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u6267\u884c\u4e4b\u524d\uff0c m_a \u88ab\u521d\u59cb\u5316\uff0c\u8c03\u7528\u503c\u4e3a4\u7684 A(int) \u6784\u9020\u51fd\u6570\uff0c\u6253\u5370\u51fa\u201cA 4\u201d\u3002\u7136\u540e\u63a7\u5236\u8fd4\u56de\u5230B\u6784\u9020\u51fd\u6570\uff0c\u6267\u884cB\u6784\u9020\u51fd\u6570\u7684\u4e3b\u4f53\uff0c\u6253\u5370\u201cB 5\u201d\u3002","title":"\u521d\u59cb\u5316\u7c7b\u7c7b\u578b\u7684\u6210\u5458\u53d8\u91cf"},{"location":"13-6-constructor-member-initializer-lists/#_4","text":"C++ \u5141\u8bb8\u7a0b\u5e8f\u5458\u7075\u6d3b\u5730\u6392\u7248\u521d\u59cb\u5316\u503c\u5217\u8868\u3002\u867d\u7136\u4f60\u4e5f\u53ef\u6309\u7167\u81ea\u5df1\u7684\u559c\u597d\u6392\u7248\uff0c\u4f46\u662f\u6211\u4eec\u63a8\u8350\u4ee5\u4e0b\u7684\u65b9\u5f0f\uff1a \u5982\u679c\u51fd\u6570\u540d\u540c\u4e00\u884c\u4e2d\u53ef\u4ee5\u653e\u5f97\u4e0b\u521d\u59cb\u5316\u503c\u5217\u8868\uff0c\u5219\u5c06\u5b83\u4eec\u90fd\u653e\u5728\u8fd9\u4e00\u884c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class Something { private : int m_value1 {}; double m_value2 {}; char m_value3 {}; public : Something () : m_value1 { 1 }, m_value2 { 2.2 }, m_value3 { 'c' } // everything on one line { } }; \u5982\u679c\u653e\u4e0d\u4e0b\uff0c\u5219\u5728\u4e0b\u4e00\u884c\u4e2d\u7f29\u8fdb\u653e\u7f6e\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Something { private : int m_value1 ; double m_value2 ; char m_value3 ; public : Something ( int value1 , double value2 , char value3 = 'c' ) // \u51fd\u6570\u540d\u8fd9\u4e00\u884c\u5df2\u7ecf\u591f\u957f\u4e86 : m_value1 { value1 }, m_value2 { value2 }, m_value3 { value3 } // \u6240\u4ee5\u5c06\u521d\u59cb\u5316\u5217\u8868\u653e\u5728\u4e0b\u4e00\u884c { } }; \u5982\u679c\u8fd9\u6837\u4e5f\u4e0d\u80fd\u5c06\u521d\u59cb\u5316\u503c\u5217\u8868\u653e\u5728\u4e00\u884c\uff0c\u5219\u901a\u8fc7\u7a7a\u683c\u6392\u7248\uff0c\u6bcf\u884c\u4e00\u4e2a\u521d\u59cb\u5316\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class Something { private : int m_value1 {}; double m_value2 {}; char m_value3 {}; float m_value4 {}; public : Something ( int value1 , double value2 , char value3 = 'c' , float value4 = 34.6f ) // this line already has a lot of stuff on it : m_value1 { value1 } // one per line , m_value2 { value2 } , m_value3 { value3 } , m_value4 { value4 } { } };","title":"\u521d\u59cb\u5316\u5217\u8868\u6392\u7248"},{"location":"13-6-constructor-member-initializer-lists/#_5","text":"\u4ee4\u4eba\u60ca\u8bb6\u7684\u662f\uff0c\u521d\u59cb\u5316\u5217\u8868\u4e2d\u7684\u53d8\u91cf\u5e76\u4e0d\u4f1a\u6309\u7167\u5b83\u4eec\u5728\u521d\u59cb\u5316\u5217\u8868\u4e2d\u6307\u5b9a\u7684\u987a\u5e8f\u521d\u59cb\u5316\u3002\u5b9e\u9645\u4e0a\uff0c\u5b83\u4eec\u5c06\u6309\u7167\u5728\u53d8\u91cf\u5728\u7c7b\u4e2d\u58f0\u660e\u7684\u987a\u5e8f\u8fdb\u884c\u521d\u59cb\u5316\u3002 \u4e3a\u4e86\u53d6\u5f97\u6700\u4f73\u7684\u6548\u679c\uff0c\u8bf7\u9075\u5faa\u4ee5\u4e0b\u5efa\u8bae\uff1a \u521d\u59cb\u5316\u6210\u5458\u53d8\u91cf\uff0c\u4e0d\u8981\u4f7f\u5b83\u4eec\u4f9d\u8d56\u4e8e\u5176\u4ed6\u6210\u5458\u53d8\u91cf\u7684\u521d\u59cb\u5316(\u6362\u53e5\u8bdd\u8bf4\uff0c\u786e\u4fdd\u5373\u4f7f\u521d\u59cb\u5316\u987a\u5e8f\u4e0d\u540c\uff0c\u4e5f\u80fd\u6b63\u786e\u521d\u59cb\u5316\u6210\u5458\u53d8\u91cf)\u3002 \u6309\u7167\u5728\u7c7b\u4e2d\u58f0\u660e\u53d8\u91cf\u7684\u76f8\u540c\u987a\u5e8f\u7f16\u5199\u521d\u59cb\u5316\u5217\u8868\u3002\u53ea\u8981\u9075\u5faa\u4e86\u524d\u9762\u7684\u5efa\u8bae\uff0c\u8fd9\u4e0d\u662f\u4e25\u683c\u8981\u6c42\u7684\uff0c\u4f46\u662f\u5982\u679c\u4f60\u4e0d\u8fd9\u6837\u505a\uff0c\u5e76\u4e14\u6253\u5f00\u4e86\u6240\u6709\u7684\u8b66\u544a\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u4ea7\u751f\u8b66\u544a\u3002","title":"\u521d\u59cb\u5316\u5217\u8868\u7684\u987a\u5e8f"},{"location":"13-6-constructor-member-initializer-lists/#_6","text":"\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u5bf9\u7c7b\u6210\u5458\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u800c\u4e0d\u662f\u4e3a\u5b83\u4eec\u8d4b\u503c\u3002\u8fd9\u662f\u521d\u59cb\u5316\u67d0\u4e9b\u7c7b\u578b\u53d8\u91cf\u7684(\u5982const\u6216reference\u6210\u5458)\u7684\u552f\u4e00\u65b9\u6cd5\uff0c\u800c\u4e14\u5b83\u6bd4\u5728\u6784\u9020\u51fd\u6570\u4f53\u4e2d\u8d4b\u503c\u6027\u80fd\u66f4\u597d\u3002\u6210\u5458\u521d\u59cb\u5316\u5668\u5217\u8868\u65e2\u9002\u7528\u4e8e\u57fa\u672c\u7c7b\u578b\uff0c\u4e5f\u9002\u7528\u4e8e\u4f5c\u4e3a\u7c7b\u672c\u8eab\u7684\u6210\u5458\u3002","title":"\u5c0f\u7ed3"},{"location":"13-7-non-static-member-initialization/","tags":["static member","initialization"],"text":"\u5f53\u7f16\u5199\u5177\u6709\u591a\u4e2a\u6784\u9020\u51fd\u6570(\u5373\u5927\u591a\u6570\u6784\u9020\u51fd\u6570)\u7684\u7c7b\u65f6\uff0c\u5728\u6bcf\u4e2a\u6784\u9020\u51fd\u6570\u4e2d\u90fd\u4e3a\u6240\u6709\u6210\u5458\u6307\u5b9a\u9ed8\u8ba4\u503c\u4f1a\u4ea7\u751f\u5197\u4f59\u4ee3\u7801\uff0c\u800c\u4e14\u5982\u679c\u66f4\u65b0\u6210\u5458\u7684\u9ed8\u8ba4\u503c\uff0c\u5219\u9700\u8981\u4fee\u6539\u6240\u6709\u7684\u6784\u9020\u51fd\u6570\u3002 \u53ef\u4ee5\u76f4\u63a5\u7ed9\u666e\u901a\u7684\u7c7b\u6210\u5458\u53d8\u91cf(\u90a3\u4e9b\u4e0d\u4f7f\u7528static\u5173\u952e\u5b57\u7684\u53d8\u91cf)\u4e00\u4e2a\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include class Rectangle { private : double m_length { 1.0 }; // m_length \u5177\u6709\u9ed8\u8ba4\u503c 1.0 double m_width { 1.0 }; // m_width \u5177\u6709\u9ed8\u8ba4\u503c 1.0 public : void print () { std :: cout << \"length: \" << m_length << \", width: \" << m_width << '\\n' ; } }; int main () { Rectangle x {}; // x.m_length = 1.0, x.m_width = 1.0 x . print (); return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\uff1a 1 length: 1.0, width: 1.0 \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316(\u4e5f\u79f0\u4e3a\u7c7b\u5185\u6210\u5458\u521d\u59cb\u5316)\u4e3a\u6210\u5458\u53d8\u91cf\u63d0\u4f9b\u9ed8\u8ba4\u503c\uff0c\u5982\u679c\u6784\u9020\u51fd\u6570\u4e0d\u4e3a\u6210\u5458\u672c\u8eab\u63d0\u4f9b\u521d\u59cb\u5316\u503c(\u901a\u8fc7\u6210\u5458\u521d\u59cb\u5316\u5217\u8868)\uff0c\u5219\u6784\u9020\u51fd\u6570\u5c06\u4f7f\u7528\u8fd9\u4e9b\u9ed8\u8ba4\u503c\u3002 \u4e0d\u8fc7\uff0c\u51b3\u5b9a\u5bf9\u8c61\u521b\u5efa\u7684\u4ecd\u7136\u662f\u6784\u9020\u51fd\u6570\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include class Rectangle { private : double m_length { 1.0 }; double m_width { 1.0 }; public : // \u6ce8\u610f\uff1a\u6ca1\u6709\u9ed8\u8ba4\u6784\u9020\u51fd\u6570 Rectangle ( double length , double width ) : m_length { length }, m_width { width } { // m_length \u548c m_width \u4f1a\u88ab\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\uff08\u4e0d\u4f1a\u4f7f\u7528\u5b83\u4eec\u7684\u9ed8\u8ba4\u503c\uff09 } void print () { std :: cout << \"length: \" << m_length << \", width: \" << m_width << '\\n' ; } }; int main () { Rectangle x {}; // \u65e0\u6cd5\u7f16\u8bd1\uff0c\u56e0\u4e3a\u4e0d\u5b58\u5728\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u5373\u4f7f\u6210\u5458\u5177\u6709\u9ed8\u8ba4\u503c return 0 ; } \u5c3d\u7ba1\u6211\u4eec\u4e3a\u6240\u6709\u6210\u5458\u63d0\u4f9b\u4e86\u9ed8\u8ba4\u503c\uff0c\u4f46\u7531\u4e8e\u6ca1\u6709\u63d0\u4f9b\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u56e0\u6b64\u65e0\u6cd5\u521b\u5efa\u4e0d\u5e26\u53c2\u6570\u7684 Rectangle \u5bf9\u8c61\u3002 \u5982\u679c\u63d0\u4f9b\u4e86\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u4e14\u6784\u9020\u51fd\u6570\u901a\u8fc7\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u521d\u59cb\u5316\u6210\u5458\uff0c\u5219\u4f18\u5148\u4f7f\u7528\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u8bf4\u660e\u4e86\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include class Rectangle { private : double m_length { 1.0 }; double m_width { 1.0 }; public : Rectangle ( double length , double width ) : m_length { length }, m_width { width } { // m_length \u548c m_width \u4f1a\u88ab\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\uff08\u4e0d\u4f1a\u4f7f\u7528\u5b83\u4eec\u7684\u9ed8\u8ba4\u503c\uff09 } Rectangle ( double length ) : m_length { length } { // m_length \u88ab\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316 // m_width \u5219\u4f7f\u7528\u9ed8\u8ba4\u503c 1.0 } void print () { std :: cout << \"length: \" << m_length << \", width: \" << m_width << '\\n' ; } }; int main () { Rectangle x { 2.0 , 3.0 }; x . print (); Rectangle y { 4.0 }; y . print (); return 0 ; } 1 2 length: 2.0, width: 3.0 length: 4.0, width: 1.0 \u6ce8\u610f\uff0c\u4f7f\u7528\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u6765\u521d\u59cb\u5316\u6210\u5458\u9700\u8981\u4f7f\u7528\u7b49\u53f7\u6216\u5927\u62ec\u53f7(\u7edf\u4e00)\u521d\u59cb\u5316\u5f0f\u2014\u2014 \u5706\u62ec\u53f7\u521d\u59cb\u5316\u5f62\u5f0f \u5728\u8fd9\u91cc\u4e0d\u8d77\u4f5c\u7528: 1 2 3 4 5 6 class A { int m_a = 1 ; // ok (\u62f7\u8d1d\u521d\u59cb\u5316) int m_b { 2 }; // ok (\u5927\u62ec\u53f7\u521d\u59cb\u5316) int m_c ( 3 ); // \u65e0\u6548 (\u5706\u62ec\u53f7\u521d\u59cb\u5316) }; COPY \u6cd5\u5219 \u63a8\u8350\u4f7f\u7528\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u6765\u4e3a\u6210\u5458\u53d8\u91cf\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002","title":"13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316"},{"location":"13-8-overlapping-and-delegating-constructors/","tags":["overlapping","delegating","constructor"],"text":"Key Takeaway \u5206\u6e05\u695a\u521d\u59cb\u5316\u548c\u8d4b\u503c \u91cd\u53e0\u6784\u9020\u51fd\u6570 \u5f53\u5b9e\u4f8b\u5316\u4e00\u4e2a\u65b0\u5bf9\u8c61\u65f6\uff0c\u5c06\u9690\u5f0f\u8c03\u7528\u8be5\u5bf9\u8c61\u7684\u6784\u9020\u51fd\u6570\u3002\u5177\u6709\u591a\u4e2a\u5177\u6709\u91cd\u53e0\u529f\u80fd\u7684\u6784\u9020\u51fd\u6570\u7684\u7c7b\u5e76\u4e0d\u7f55\u89c1\u3002\u8003\u8651\u4ee5\u4e0b\u7c7b: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Foo { public : Foo () { // code to do A } Foo ( int value ) { // code to do A // code to do B } }; \u8fd9\u4e2a\u7c7b\u6709\u4e24\u4e2a\u6784\u9020\u51fd\u6570\uff1a\u4e00\u4e2a \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 \u4ee5\u53ca\u4e00\u4e2a\u63a5\u53d7\u4e00\u4e2a int \u7c7b\u578b\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u3002\u56e0\u4e3a \u201ccode to do A\u201d \u8fd9\u4e00\u90e8\u5206\u5728\u4e24\u4e2a\u6784\u9020\u51fd\u6570\u4e2d\u90fd\u5b58\u5728\uff0c\u5219\u53ef\u4ee5\u8bf4\u5b83\u4eec\u5728\u6bcf\u4e2a\u6784\u9020\u51fd\u6570\u4e2d\u662f\u91cd\u590d\u7684\u3002 \u8bfe\u7a0b\u5b66\u5230\u8fd9\u91cc\uff0c\u76f8\u6bd4\u4f60\u5df2\u7ecf\u4e86\u89e3\u5230\uff0c\u8981\u5c3d\u53ef\u80fd\u907f\u514d\u91cd\u590d\u4ee3\u7801\uff0c\u6240\u4ee5\u8ba9\u6211\u4eec\u770b\u770b\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u4e00\u4e9b\u65b9\u6cd5\u3002 \u663e\u800c\u6613\u89c1\u7684\u89e3\u51b3\u65b9\u6848\u5e76\u4e0d\u594f\u6548 \u663e\u800c\u6613\u89c1\u7684\u89e3\u51b3\u65b9\u6848\u662f\u8ba9 Foo(int) \u6784\u9020\u51fd\u6570\u8c03\u7528 Foo() \u6784\u9020\u51fd\u6570\u6765\u5b8c\u6210A\u90e8\u5206\u5de5\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Foo { public : Foo () { // code to do A } Foo ( int value ) { Foo (); // use the above constructor to do A (doesn't work) // code to do B } }; \u4e0d\u8fc7\uff0c\u5982\u679c\u4f60\u50cf\u4e0a\u9762\u8fd9\u6837\u5728\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u91cc\u9762\u8c03\u7528\u53e6\u5916\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u7684\u8bdd\uff0c\u7a0b\u5e8f\u867d\u7136\u53ef\u4ee5\u7f16\u8bd1\uff08\u4e5f\u8bb8\u4f1a\u4ea7\u751f\u544a\u8b66\uff09\uff0c\u4f46\u662f\u7edd\u5bf9\u4e0d\u4f1a\u50cf\u4f60\u60f3\u8c61\u7684\u90a3\u6837\u5de5\u4f5c\uff0c\u7136\u540e\u4f60\u53ef\u80fd\u9700\u8981\u82b1\u8d39\u5f88\u591a\u65f6\u95f4\u53bb\u5b9a\u4f4d\u95ee\u9898\u3002\u5b9e\u9645\u4e0a\uff0c\u6784\u9020\u51fd\u6570 Foo(); \u4f1a\u5b9e\u4f8b\u5316\u4e00\u4e2a\u65b0\u7684 Foo \u5bf9\u8c61\u3002 \u6784\u9020\u51fd\u6570\u59d4\u6d3e \u6784\u9020\u51fd\u6570\u53ef\u4ee5\u8c03\u7528\u6765\u81ea\u540c\u4e00\u7c7b\u7684\u5176\u4ed6\u6784\u9020\u51fd\u6570\uff0c\u79f0\u4e3a \u59d4\u6258\u6784\u9020\u51fd\u6570 (\u6216 \u6784\u9020\u51fd\u6570\u94fe )\u3002 \u8981\u8ba9\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u8c03\u7528\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u53ea\u9700\u5728\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u8c03\u7528\u5b83\u3002\u8fd9\u662f\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u7684\u4e00\u79cd\u60c5\u51b5\u3002\u5e94\u7528\u5230\u6211\u4eec\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Foo { private : public : Foo () { // code to do A } Foo ( int value ) : Foo {} // \u8c03\u7528 Foo() \u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u5b8c\u6210A { // code to do B } }; \u8fd9\u5b8c\u5168\u7b26\u5408\u60a8\u7684\u9884\u671f\u3002\u786e\u4fdd\u4ece\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u8c03\u7528\u6784\u9020\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u5728\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u4e2d\u8c03\u7528\u3002 \u4e0b\u9762\u662f\u4f7f\u7528\u59d4\u6258\u6784\u9020\u51fd\u6570\u51cf\u5c11\u5197\u4f59\u4ee3\u7801\u7684\u53e6\u4e00\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include #include class Employee { private : int m_id {}; std :: string m_name {}; public : Employee ( int id = 0 , std :: string_view name = \"\" ) : m_id { id }, m_name { name } { std :: cout << \"Employee \" << m_name << \" created. \\n \" ; } // Use a delegating constructor to minimize redundant code Employee ( std :: string_view name ) : Employee { 0 , name } { } }; \u8fd9\u4e2a\u7c7b\u6709\u4e24\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u5176\u4e2d\u4e00\u4e2a\u59d4\u6258\u7ed9 Employee(int, std::string_view) \u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5197\u4f59\u4ee3\u7801\u7684\u6570\u91cf\u88ab\u6700\u5c0f\u5316\u4e86(\u6211\u4eec\u53ea\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u4f53\u800c\u4e0d\u662f\u4e24\u4e2a)\u3002 \u5173\u4e8e\u59d4\u6258\u6784\u9020\u51fd\u6570\u7684\u4e00\u4e9b\u989d\u5916\u6ce8\u610f\u4e8b\u9879\u3002\u9996\u5148\uff0c\u59d4\u6258\u7ed9\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u7684\u6784\u9020\u51fd\u6570\u672c\u8eab\u4e0d\u5141\u8bb8\u8fdb\u884c\u4efb\u4f55\u6210\u5458\u521d\u59cb\u5316\u3002 \u6784\u9020\u51fd\u6570\u53ef\u4ee5\u59d4\u6258\u6216\u521d\u59cb\u5316\uff0c\u4f46\u4e0d\u80fd\u540c\u65f6\u59d4\u6258\u548c\u521d\u59cb\u5316\u3002 \u5176\u6b21\uff0c\u5982\u679c\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u59d4\u6258\u7ed9\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u800c\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u53c8\u59d4\u6258\u56de\u7b2c\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u3002\u8fd9\u5c06\u5f62\u6210\u4e00\u4e2a\u6b7b\u5faa\u73af\uff0c\u5e76\u5c06\u5bfc\u81f4\u7684\u7a0b\u5e8f\u8017\u5c3d\u5806\u6808\u7a7a\u95f4\u540e\u5d29\u6e83\u3002\u53ef\u4ee5\u901a\u8fc7\u786e\u4fdd\u6240\u6709\u6784\u9020\u51fd\u6570\u89e3\u6790\u4e3a\u975e\u59d4\u6258\u6784\u9020\u51fd\u6570\u6765\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u60a8\u6709\u591a\u4e2a\u5177\u6709\u76f8\u540c\u529f\u80fd\u7684\u6784\u9020\u51fd\u6570\uff0c\u8bf7\u4f7f\u7528\u59d4\u6258\u6784\u9020\u51fd\u6570\u4ee5\u907f\u514d\u91cd\u590d\u4ee3\u7801\u3002 \u4f7f\u7528\u666e\u901a\u6210\u5458\u51fd\u6570\u8fdb\u884c\u542f\u52a8\u914d\u7f6e \u56e0\u4e3a\u6784\u9020\u51fd\u6570\u53ea\u80fd\u7528\u4f5c\u521d\u59cb\u5316\u6216\u59d4\u6258\uff0c\u6240\u4ee5\u5982\u679c\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u9700\u8981\u6267\u884c\u4e00\u4e9b\u5e38\u89c1\u7684\u521d\u59cb\u5316\uff0c\u5c31\u4f1a\u5f88\u9ebb\u70e6\u3002\u8003\u8651\u4ee5\u4e0b\u7c7b: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Foo { private : const int m_value { 0 }; public : Foo () { // \u7528\u4e8e\u4efb\u52a1\u542f\u52a8\u914d\u7f6e\u7684\u4ee3\u7801(\u4f8b\u5982\u6253\u5f00\u6587\u4ef6\u6216\u6570\u636e\u5e93) } Foo ( int value ) : m_value { value } // \u5fc5\u987b\u521d\u59cb\u5316 const \u6210\u5458 m_value { // \u6b64\u65f6\u5e94\u8be5\u5982\u4f55\u6267\u884cFoo()\u4e2d\u7684\u542f\u52a8\u914d\u7f6e\u4ee3\u7801\u5462\uff1f } }; \u6784\u9020\u51fd\u6570 Foo(int) \u8981\u4e48\u521d\u59cb\u5316 m_value \uff0c\u8981\u4e48\u59d4\u6258\u7ed9 Foo() \u6267\u884c\u5176\u4e2d\u7684\u4ee3\u7801\uff0c\u4f46\u9c7c\u548c\u718a\u638c\u4e0d\u53ef\u517c\u5f97\u3002\u4f46\u662f\u5982\u679c\u4e00\u5b9a\u8981\u517c\u5f97\u5462\uff1f\u5f53\u7136\uff0c\u6700\u7b28\u7684\u7684\u529e\u6cd5\u5c31\u662f\u628a\u9700\u8981\u6267\u884c\u7684\u4ee3\u7801\u590d\u5236\u4e00\u4efd\u8fc7\u6765\u3002\u4f46\u8fd9\u65e0\u7591\u4f1a\u5e26\u6765\u5f88\u591a\u91cd\u590d\u4ee3\u7801\uff0c\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\u3002 \u5176\u5b9e\u6784\u9020\u51fd\u6570\u662f\u53ef\u4ee5\u8c03\u7528\u975e\u6784\u9020\u6210\u5458\u51fd\u6570\uff08\u548c\u975e\u6210\u5458\u51fd\u6570\uff09\u7684\uff0c\u56e0\u6b64\u66f4\u597d\u7684\u529e\u6cd5\u662f\u4f7f\u7528\u4e00\u4e2a\u666e\u901a\u51fd\u6570\u6765\u6267\u884c\u8fd9\u4e9b\u4ee3\u7801\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include class Foo { private : const int m_value { 0 }; void setup () // \u8bbe\u4e3a\u79c1\u6709\uff0c\u53ea\u6709\u6784\u9020\u51fd\u6570\u80fd\u591f\u65f6 { // code to do some common setup tasks (e.g. open a file or database) std :: cout << \"Setting things up... \\n \" ; } public : Foo () { setup (); } Foo ( int value ) : m_value { value } // m_value \u662f const \u7c7b\u578b\u5fc5\u987b\u521d\u59cb\u5316 { setup (); } }; int main () { Foo a ; Foo b { 5 }; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u3002\u6211\u4eec\u5b9a\u4e49 setup() \u6210\u5458\u51fd\u6570\u6765\u6267\u884c\u542f\u52a8\u914d\u7f6e\u76f8\u5173\u7684\u4ee3\u7801\uff0c\u7c7b\u4e2d\u7684\u4e24\u4e2a\u6784\u9020\u51fd\u6570\u90fd\u53ef\u4ee5\u8c03\u7528\u5b83\u3002\u6211\u4eec\u5c06\u8be5\u51fd\u6570\u5b9a\u4e49\u4e3a \u79c1\u6709\u6210\u5458 \uff0c\u6240\u4ee5\u53ea\u6709\u8fd9\u4e2a\u7c7b\u7684\u6210\u5458\u53ef\u4ee5\u8bbf\u95ee\u5b83\u3002 \u5f53\u7136\uff0c setup() \u5e76\u4e0d\u662f\u6784\u9020\u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u4e0d\u80fd\u521d\u59cb\u5316\u6210\u5458\u3002 \u5f53\u6784\u9020\u51fd\u6570\u8c03\u7528 setup() \u65f6\uff0c\u7c7b\u6210\u5458\u4eec\u90fd\u5df2\u7ecf\u88ab\u521b\u5efa\u4e86\uff08\u5982\u679c\u63d0\u4f9b\u4e86\u521d\u59cb\u5316\u503c\u5219\u751a\u81f3\u5df2\u7ecf\u5b8c\u6210\u4e86\u521d\u59cb\u5316\uff09\u3002 setup() \u51fd\u6570\u53ea\u80fd\u4e3a\u6210\u5458\u8d4b\u503c\u6216\u8fdb\u884c\u4e00\u4e9b\u5176\u4ed6\u64cd\u4f5c\uff0c\u4f46\u90fd\u53ea\u80fd\u901a\u8fc7\u4e00\u822c\u7684\u8bed\u53e5\u8fdb\u884c\uff08\u4f8b\u5982\u6253\u5f00\u6587\u4ef6\u6216\u6570\u636e\u5e93\uff09\u3002 setup() \u51fd\u6570\u4e0d\u80fd\u521b\u5efa\u6210\u5458\u7684 \u5de6\u503c\u5f15\u7528 \uff0c\u4e5f\u4e0d\u80fd\u4e3a const \u7c7b\u578b\u7684\u6210\u5458\u8d4b\u503c\uff08\u4e24\u8005\u90fd\u9700\u8981\u901a\u8fc7\u521d\u59cb\u5316\u5b8c\u6210\uff09\uff0c\u5f53\u7136\u4e5f\u4e0d\u80fd\u5411\u4efb\u4f55\u4e0d\u652f\u6301\u8d4b\u503c\u64cd\u4f5c\u7684\u6210\u5458\u8d4b\u503c\u3002 \u91cd\u7f6e\u5bf9\u8c61 \u6709\u65f6\u5019\u4f60\u53ef\u80fd\u4f1a\u60f3\u8981\u7f16\u5199\u4e00\u4e2a\u6210\u5458\u51fd\u6570\uff08\u4f8b\u5982 reset() \uff09\u5c06\u7c7b\u5bf9\u8c61\u91cd\u7f6e\u4e3a\u5176\u521d\u59cb\u72b6\u6001\u3002 \u56e0\u4e3a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u4e3a\u7c7b\u6210\u5458\u521d\u59cb\u5316\u6240\u9700\u7684\u503c\uff0c\u6240\u4ee5\u4f60\u53ef\u80fd\u4f1a\u5c1d\u8bd5\u5728 reset() \u51fd\u6570\u4e2d\u8c03\u7528\u6784\u9020\u51fd\u6570\u4ee5\u8fbe\u5230\u76ee\u7684\u3002\u4e0d\u8fc7\uff0c\u76f4\u63a5\u8c03\u7528\u6784\u9020\u51fd\u6570\u901a\u5e38\u4f1a\u5bfc\u81f4\u975e\u9884\u671f\u7684\u884c\u4e3a\uff0c\u5c31\u50cf\u4e4b\u524d\u5c55\u793a\u7684\u90a3\u6837\uff0c\u5b83\u5e76\u4e0d\u80fd\u6b63\u786e\u7684\u5de5\u4f5c\u3002 reset() \u51fd\u6570\u7684\u4e00\u822c\u5b9e\u73b0\u53ef\u80fd\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include class Foo { private : int m_a { 1 }; int m_b { 2 }; public : Foo () { } Foo ( int a , int b ) : m_a { a }, m_b { b } { } void print () { std :: cout << m_a << ' ' << m_b << '\\n' ; } void reset () { m_a = 1 ; m_b = 2 ; } }; int main () { Foo a { 3 , 4 }; a . reset (); a . print (); return 0 ; } \u867d\u7136\u8fd9\u79cd\u5b9e\u73b0\u53ef\u4ee5\u5de5\u4f5c\uff0c\u4f46\u662f\u8fdd\u53cd\u4e86DRY\u9a8c\u8bc1\uff0c\u56e0\u4e3a\u201c\u9ed8\u8ba4\u201d\u503c\u51fa\u73b0\u5728\u4e86\u4e24\u4e2a\u4f4d\u7f6e\uff1a\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u548c reset() \u51fd\u6570\u4f53\u4e2d\u3002\u4e0d\u8fc7\uff0c\u4e5f\u7684\u786e\u6ca1\u6709\u529e\u6cd5\u80fd\u591f\u8ba9 reset() \u76f4\u63a5\u83b7\u53d6\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u503c\u3002 \u4f46\u662f\uff0c\u5982\u679c\u4e00\u4e2a\u7c7b\u662f \u53ef\u8d4b\u503c\u7684 \uff08\u5373\u53ef\u4ee5\u8bbf\u95ee\u5176\u8d4b\u503c\u64cd\u4f5c\u7b26\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\uff0c\u7136\u540e\u5c06\u65b0\u5bf9\u8c61\u8d4b\u503c\u7ed9\u8001\u5bf9\u8c61\uff0c\u5b9e\u73b0\u91cd\u7f6e\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include class Foo { private : int m_a { 5 }; int m_b { 6 }; public : Foo () { } Foo ( int a , int b ) : m_a { a }, m_b { b } { } void print () { std :: cout << m_a << ' ' << m_b << '\\n' ; } void reset () { // consider this a bit of magic for now * this = Foo (); // create new Foo object, then use assignment to overwrite our implicit object } }; int main () { Foo a { 1 , 2 }; a . reset (); a . print (); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u7684 Foo \u5bf9\u8c61(\u5305\u542b\u9ed8\u8ba4\u521d\u503c)\u3002\u7136\u540e\u6211\u4eec\u5c06\u8fd9\u4e2a\u65b0 Foo \u5bf9\u8c61\u8d4b\u503c\u7ed9\u8c03\u7528 reset() \u6210\u5458\u51fd\u6570\u7684\u5bf9\u8c61 ( *this )\u3002\u6b64\u65f6\u7f16\u8bd1\u5668\u4f1a\u8fdb\u884c \u6210\u5458\u4f9d\u6b21\u62f7\u8d1d \u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 13.10 - \u9690\u85cf\u7684this\u6307\u9488 \u4e2d\u4ecb\u7ecdthis\u6307\u9488\uff0c\u7c7b\u5bf9\u8c61\u7684\u8d4b\u503c\u5219\u4f1a\u5728 14.15 -- Overloading the assignment operator \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570"},{"location":"13-8-overlapping-and-delegating-constructors/#_1","text":"\u5f53\u5b9e\u4f8b\u5316\u4e00\u4e2a\u65b0\u5bf9\u8c61\u65f6\uff0c\u5c06\u9690\u5f0f\u8c03\u7528\u8be5\u5bf9\u8c61\u7684\u6784\u9020\u51fd\u6570\u3002\u5177\u6709\u591a\u4e2a\u5177\u6709\u91cd\u53e0\u529f\u80fd\u7684\u6784\u9020\u51fd\u6570\u7684\u7c7b\u5e76\u4e0d\u7f55\u89c1\u3002\u8003\u8651\u4ee5\u4e0b\u7c7b: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Foo { public : Foo () { // code to do A } Foo ( int value ) { // code to do A // code to do B } }; \u8fd9\u4e2a\u7c7b\u6709\u4e24\u4e2a\u6784\u9020\u51fd\u6570\uff1a\u4e00\u4e2a \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 \u4ee5\u53ca\u4e00\u4e2a\u63a5\u53d7\u4e00\u4e2a int \u7c7b\u578b\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u3002\u56e0\u4e3a \u201ccode to do A\u201d \u8fd9\u4e00\u90e8\u5206\u5728\u4e24\u4e2a\u6784\u9020\u51fd\u6570\u4e2d\u90fd\u5b58\u5728\uff0c\u5219\u53ef\u4ee5\u8bf4\u5b83\u4eec\u5728\u6bcf\u4e2a\u6784\u9020\u51fd\u6570\u4e2d\u662f\u91cd\u590d\u7684\u3002 \u8bfe\u7a0b\u5b66\u5230\u8fd9\u91cc\uff0c\u76f8\u6bd4\u4f60\u5df2\u7ecf\u4e86\u89e3\u5230\uff0c\u8981\u5c3d\u53ef\u80fd\u907f\u514d\u91cd\u590d\u4ee3\u7801\uff0c\u6240\u4ee5\u8ba9\u6211\u4eec\u770b\u770b\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u4e00\u4e9b\u65b9\u6cd5\u3002","title":"\u91cd\u53e0\u6784\u9020\u51fd\u6570"},{"location":"13-8-overlapping-and-delegating-constructors/#_2","text":"\u663e\u800c\u6613\u89c1\u7684\u89e3\u51b3\u65b9\u6848\u662f\u8ba9 Foo(int) \u6784\u9020\u51fd\u6570\u8c03\u7528 Foo() \u6784\u9020\u51fd\u6570\u6765\u5b8c\u6210A\u90e8\u5206\u5de5\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Foo { public : Foo () { // code to do A } Foo ( int value ) { Foo (); // use the above constructor to do A (doesn't work) // code to do B } }; \u4e0d\u8fc7\uff0c\u5982\u679c\u4f60\u50cf\u4e0a\u9762\u8fd9\u6837\u5728\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u91cc\u9762\u8c03\u7528\u53e6\u5916\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u7684\u8bdd\uff0c\u7a0b\u5e8f\u867d\u7136\u53ef\u4ee5\u7f16\u8bd1\uff08\u4e5f\u8bb8\u4f1a\u4ea7\u751f\u544a\u8b66\uff09\uff0c\u4f46\u662f\u7edd\u5bf9\u4e0d\u4f1a\u50cf\u4f60\u60f3\u8c61\u7684\u90a3\u6837\u5de5\u4f5c\uff0c\u7136\u540e\u4f60\u53ef\u80fd\u9700\u8981\u82b1\u8d39\u5f88\u591a\u65f6\u95f4\u53bb\u5b9a\u4f4d\u95ee\u9898\u3002\u5b9e\u9645\u4e0a\uff0c\u6784\u9020\u51fd\u6570 Foo(); \u4f1a\u5b9e\u4f8b\u5316\u4e00\u4e2a\u65b0\u7684 Foo \u5bf9\u8c61\u3002","title":"\u663e\u800c\u6613\u89c1\u7684\u89e3\u51b3\u65b9\u6848\u5e76\u4e0d\u594f\u6548"},{"location":"13-8-overlapping-and-delegating-constructors/#_3","text":"\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u8c03\u7528\u6765\u81ea\u540c\u4e00\u7c7b\u7684\u5176\u4ed6\u6784\u9020\u51fd\u6570\uff0c\u79f0\u4e3a \u59d4\u6258\u6784\u9020\u51fd\u6570 (\u6216 \u6784\u9020\u51fd\u6570\u94fe )\u3002 \u8981\u8ba9\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u8c03\u7528\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u53ea\u9700\u5728\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u8c03\u7528\u5b83\u3002\u8fd9\u662f\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u7684\u4e00\u79cd\u60c5\u51b5\u3002\u5e94\u7528\u5230\u6211\u4eec\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Foo { private : public : Foo () { // code to do A } Foo ( int value ) : Foo {} // \u8c03\u7528 Foo() \u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u5b8c\u6210A { // code to do B } }; \u8fd9\u5b8c\u5168\u7b26\u5408\u60a8\u7684\u9884\u671f\u3002\u786e\u4fdd\u4ece\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u8c03\u7528\u6784\u9020\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u5728\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u4e2d\u8c03\u7528\u3002 \u4e0b\u9762\u662f\u4f7f\u7528\u59d4\u6258\u6784\u9020\u51fd\u6570\u51cf\u5c11\u5197\u4f59\u4ee3\u7801\u7684\u53e6\u4e00\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include #include class Employee { private : int m_id {}; std :: string m_name {}; public : Employee ( int id = 0 , std :: string_view name = \"\" ) : m_id { id }, m_name { name } { std :: cout << \"Employee \" << m_name << \" created. \\n \" ; } // Use a delegating constructor to minimize redundant code Employee ( std :: string_view name ) : Employee { 0 , name } { } }; \u8fd9\u4e2a\u7c7b\u6709\u4e24\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u5176\u4e2d\u4e00\u4e2a\u59d4\u6258\u7ed9 Employee(int, std::string_view) \u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5197\u4f59\u4ee3\u7801\u7684\u6570\u91cf\u88ab\u6700\u5c0f\u5316\u4e86(\u6211\u4eec\u53ea\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u4f53\u800c\u4e0d\u662f\u4e24\u4e2a)\u3002 \u5173\u4e8e\u59d4\u6258\u6784\u9020\u51fd\u6570\u7684\u4e00\u4e9b\u989d\u5916\u6ce8\u610f\u4e8b\u9879\u3002\u9996\u5148\uff0c\u59d4\u6258\u7ed9\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u7684\u6784\u9020\u51fd\u6570\u672c\u8eab\u4e0d\u5141\u8bb8\u8fdb\u884c\u4efb\u4f55\u6210\u5458\u521d\u59cb\u5316\u3002 \u6784\u9020\u51fd\u6570\u53ef\u4ee5\u59d4\u6258\u6216\u521d\u59cb\u5316\uff0c\u4f46\u4e0d\u80fd\u540c\u65f6\u59d4\u6258\u548c\u521d\u59cb\u5316\u3002 \u5176\u6b21\uff0c\u5982\u679c\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u59d4\u6258\u7ed9\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u800c\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u53c8\u59d4\u6258\u56de\u7b2c\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u3002\u8fd9\u5c06\u5f62\u6210\u4e00\u4e2a\u6b7b\u5faa\u73af\uff0c\u5e76\u5c06\u5bfc\u81f4\u7684\u7a0b\u5e8f\u8017\u5c3d\u5806\u6808\u7a7a\u95f4\u540e\u5d29\u6e83\u3002\u53ef\u4ee5\u901a\u8fc7\u786e\u4fdd\u6240\u6709\u6784\u9020\u51fd\u6570\u89e3\u6790\u4e3a\u975e\u59d4\u6258\u6784\u9020\u51fd\u6570\u6765\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u60a8\u6709\u591a\u4e2a\u5177\u6709\u76f8\u540c\u529f\u80fd\u7684\u6784\u9020\u51fd\u6570\uff0c\u8bf7\u4f7f\u7528\u59d4\u6258\u6784\u9020\u51fd\u6570\u4ee5\u907f\u514d\u91cd\u590d\u4ee3\u7801\u3002","title":"\u6784\u9020\u51fd\u6570\u59d4\u6d3e"},{"location":"13-8-overlapping-and-delegating-constructors/#_4","text":"\u56e0\u4e3a\u6784\u9020\u51fd\u6570\u53ea\u80fd\u7528\u4f5c\u521d\u59cb\u5316\u6216\u59d4\u6258\uff0c\u6240\u4ee5\u5982\u679c\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u9700\u8981\u6267\u884c\u4e00\u4e9b\u5e38\u89c1\u7684\u521d\u59cb\u5316\uff0c\u5c31\u4f1a\u5f88\u9ebb\u70e6\u3002\u8003\u8651\u4ee5\u4e0b\u7c7b: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Foo { private : const int m_value { 0 }; public : Foo () { // \u7528\u4e8e\u4efb\u52a1\u542f\u52a8\u914d\u7f6e\u7684\u4ee3\u7801(\u4f8b\u5982\u6253\u5f00\u6587\u4ef6\u6216\u6570\u636e\u5e93) } Foo ( int value ) : m_value { value } // \u5fc5\u987b\u521d\u59cb\u5316 const \u6210\u5458 m_value { // \u6b64\u65f6\u5e94\u8be5\u5982\u4f55\u6267\u884cFoo()\u4e2d\u7684\u542f\u52a8\u914d\u7f6e\u4ee3\u7801\u5462\uff1f } }; \u6784\u9020\u51fd\u6570 Foo(int) \u8981\u4e48\u521d\u59cb\u5316 m_value \uff0c\u8981\u4e48\u59d4\u6258\u7ed9 Foo() \u6267\u884c\u5176\u4e2d\u7684\u4ee3\u7801\uff0c\u4f46\u9c7c\u548c\u718a\u638c\u4e0d\u53ef\u517c\u5f97\u3002\u4f46\u662f\u5982\u679c\u4e00\u5b9a\u8981\u517c\u5f97\u5462\uff1f\u5f53\u7136\uff0c\u6700\u7b28\u7684\u7684\u529e\u6cd5\u5c31\u662f\u628a\u9700\u8981\u6267\u884c\u7684\u4ee3\u7801\u590d\u5236\u4e00\u4efd\u8fc7\u6765\u3002\u4f46\u8fd9\u65e0\u7591\u4f1a\u5e26\u6765\u5f88\u591a\u91cd\u590d\u4ee3\u7801\uff0c\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\u3002 \u5176\u5b9e\u6784\u9020\u51fd\u6570\u662f\u53ef\u4ee5\u8c03\u7528\u975e\u6784\u9020\u6210\u5458\u51fd\u6570\uff08\u548c\u975e\u6210\u5458\u51fd\u6570\uff09\u7684\uff0c\u56e0\u6b64\u66f4\u597d\u7684\u529e\u6cd5\u662f\u4f7f\u7528\u4e00\u4e2a\u666e\u901a\u51fd\u6570\u6765\u6267\u884c\u8fd9\u4e9b\u4ee3\u7801\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include class Foo { private : const int m_value { 0 }; void setup () // \u8bbe\u4e3a\u79c1\u6709\uff0c\u53ea\u6709\u6784\u9020\u51fd\u6570\u80fd\u591f\u65f6 { // code to do some common setup tasks (e.g. open a file or database) std :: cout << \"Setting things up... \\n \" ; } public : Foo () { setup (); } Foo ( int value ) : m_value { value } // m_value \u662f const \u7c7b\u578b\u5fc5\u987b\u521d\u59cb\u5316 { setup (); } }; int main () { Foo a ; Foo b { 5 }; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u3002\u6211\u4eec\u5b9a\u4e49 setup() \u6210\u5458\u51fd\u6570\u6765\u6267\u884c\u542f\u52a8\u914d\u7f6e\u76f8\u5173\u7684\u4ee3\u7801\uff0c\u7c7b\u4e2d\u7684\u4e24\u4e2a\u6784\u9020\u51fd\u6570\u90fd\u53ef\u4ee5\u8c03\u7528\u5b83\u3002\u6211\u4eec\u5c06\u8be5\u51fd\u6570\u5b9a\u4e49\u4e3a \u79c1\u6709\u6210\u5458 \uff0c\u6240\u4ee5\u53ea\u6709\u8fd9\u4e2a\u7c7b\u7684\u6210\u5458\u53ef\u4ee5\u8bbf\u95ee\u5b83\u3002 \u5f53\u7136\uff0c setup() \u5e76\u4e0d\u662f\u6784\u9020\u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u4e0d\u80fd\u521d\u59cb\u5316\u6210\u5458\u3002 \u5f53\u6784\u9020\u51fd\u6570\u8c03\u7528 setup() \u65f6\uff0c\u7c7b\u6210\u5458\u4eec\u90fd\u5df2\u7ecf\u88ab\u521b\u5efa\u4e86\uff08\u5982\u679c\u63d0\u4f9b\u4e86\u521d\u59cb\u5316\u503c\u5219\u751a\u81f3\u5df2\u7ecf\u5b8c\u6210\u4e86\u521d\u59cb\u5316\uff09\u3002 setup() \u51fd\u6570\u53ea\u80fd\u4e3a\u6210\u5458\u8d4b\u503c\u6216\u8fdb\u884c\u4e00\u4e9b\u5176\u4ed6\u64cd\u4f5c\uff0c\u4f46\u90fd\u53ea\u80fd\u901a\u8fc7\u4e00\u822c\u7684\u8bed\u53e5\u8fdb\u884c\uff08\u4f8b\u5982\u6253\u5f00\u6587\u4ef6\u6216\u6570\u636e\u5e93\uff09\u3002 setup() \u51fd\u6570\u4e0d\u80fd\u521b\u5efa\u6210\u5458\u7684 \u5de6\u503c\u5f15\u7528 \uff0c\u4e5f\u4e0d\u80fd\u4e3a const \u7c7b\u578b\u7684\u6210\u5458\u8d4b\u503c\uff08\u4e24\u8005\u90fd\u9700\u8981\u901a\u8fc7\u521d\u59cb\u5316\u5b8c\u6210\uff09\uff0c\u5f53\u7136\u4e5f\u4e0d\u80fd\u5411\u4efb\u4f55\u4e0d\u652f\u6301\u8d4b\u503c\u64cd\u4f5c\u7684\u6210\u5458\u8d4b\u503c\u3002","title":"\u4f7f\u7528\u666e\u901a\u6210\u5458\u51fd\u6570\u8fdb\u884c\u542f\u52a8\u914d\u7f6e"},{"location":"13-8-overlapping-and-delegating-constructors/#_5","text":"\u6709\u65f6\u5019\u4f60\u53ef\u80fd\u4f1a\u60f3\u8981\u7f16\u5199\u4e00\u4e2a\u6210\u5458\u51fd\u6570\uff08\u4f8b\u5982 reset() \uff09\u5c06\u7c7b\u5bf9\u8c61\u91cd\u7f6e\u4e3a\u5176\u521d\u59cb\u72b6\u6001\u3002 \u56e0\u4e3a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u4e3a\u7c7b\u6210\u5458\u521d\u59cb\u5316\u6240\u9700\u7684\u503c\uff0c\u6240\u4ee5\u4f60\u53ef\u80fd\u4f1a\u5c1d\u8bd5\u5728 reset() \u51fd\u6570\u4e2d\u8c03\u7528\u6784\u9020\u51fd\u6570\u4ee5\u8fbe\u5230\u76ee\u7684\u3002\u4e0d\u8fc7\uff0c\u76f4\u63a5\u8c03\u7528\u6784\u9020\u51fd\u6570\u901a\u5e38\u4f1a\u5bfc\u81f4\u975e\u9884\u671f\u7684\u884c\u4e3a\uff0c\u5c31\u50cf\u4e4b\u524d\u5c55\u793a\u7684\u90a3\u6837\uff0c\u5b83\u5e76\u4e0d\u80fd\u6b63\u786e\u7684\u5de5\u4f5c\u3002 reset() \u51fd\u6570\u7684\u4e00\u822c\u5b9e\u73b0\u53ef\u80fd\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include class Foo { private : int m_a { 1 }; int m_b { 2 }; public : Foo () { } Foo ( int a , int b ) : m_a { a }, m_b { b } { } void print () { std :: cout << m_a << ' ' << m_b << '\\n' ; } void reset () { m_a = 1 ; m_b = 2 ; } }; int main () { Foo a { 3 , 4 }; a . reset (); a . print (); return 0 ; } \u867d\u7136\u8fd9\u79cd\u5b9e\u73b0\u53ef\u4ee5\u5de5\u4f5c\uff0c\u4f46\u662f\u8fdd\u53cd\u4e86DRY\u9a8c\u8bc1\uff0c\u56e0\u4e3a\u201c\u9ed8\u8ba4\u201d\u503c\u51fa\u73b0\u5728\u4e86\u4e24\u4e2a\u4f4d\u7f6e\uff1a\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u548c reset() \u51fd\u6570\u4f53\u4e2d\u3002\u4e0d\u8fc7\uff0c\u4e5f\u7684\u786e\u6ca1\u6709\u529e\u6cd5\u80fd\u591f\u8ba9 reset() \u76f4\u63a5\u83b7\u53d6\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u503c\u3002 \u4f46\u662f\uff0c\u5982\u679c\u4e00\u4e2a\u7c7b\u662f \u53ef\u8d4b\u503c\u7684 \uff08\u5373\u53ef\u4ee5\u8bbf\u95ee\u5176\u8d4b\u503c\u64cd\u4f5c\u7b26\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\uff0c\u7136\u540e\u5c06\u65b0\u5bf9\u8c61\u8d4b\u503c\u7ed9\u8001\u5bf9\u8c61\uff0c\u5b9e\u73b0\u91cd\u7f6e\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include class Foo { private : int m_a { 5 }; int m_b { 6 }; public : Foo () { } Foo ( int a , int b ) : m_a { a }, m_b { b } { } void print () { std :: cout << m_a << ' ' << m_b << '\\n' ; } void reset () { // consider this a bit of magic for now * this = Foo (); // create new Foo object, then use assignment to overwrite our implicit object } }; int main () { Foo a { 1 , 2 }; a . reset (); a . print (); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u7684 Foo \u5bf9\u8c61(\u5305\u542b\u9ed8\u8ba4\u521d\u503c)\u3002\u7136\u540e\u6211\u4eec\u5c06\u8fd9\u4e2a\u65b0 Foo \u5bf9\u8c61\u8d4b\u503c\u7ed9\u8c03\u7528 reset() \u6210\u5458\u51fd\u6570\u7684\u5bf9\u8c61 ( *this )\u3002\u6b64\u65f6\u7f16\u8bd1\u5668\u4f1a\u8fdb\u884c \u6210\u5458\u4f9d\u6b21\u62f7\u8d1d \u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 13.10 - \u9690\u85cf\u7684this\u6307\u9488 \u4e2d\u4ecb\u7ecdthis\u6307\u9488\uff0c\u7c7b\u5bf9\u8c61\u7684\u8d4b\u503c\u5219\u4f1a\u5728 14.15 -- Overloading the assignment operator \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"\u91cd\u7f6e\u5bf9\u8c61"},{"location":"13-9-destructors/","tags":["destructors"],"text":"\u6790\u6784\u51fd\u6570 \u662f\u53e6\u5916\u4e00\u79cd\u96f7\u53d4\u7684\u7c7b\u6210\u5458\u51fd\u6570\uff0c\u5b83\u4f1a\u5728\u5bf9\u8c61\u9500\u6bc1\u65f6\u81ea\u52a8\u6267\u884c\u3002\u6784\u9020\u51fd\u6570\u51fd\u6570\u7528\u4e8e\u521d\u59cb\u5316\u5bf9\u8c61\uff0c\u800c\u6790\u6784\u51fd\u6570\u5219\u7528\u4e8e\u5bf9\u5bf9\u8c61\u8fdb\u884c\u201c\u6e05\u7406\u201d\u3002 \u5f53\u5bf9\u8c61\u6b63\u5e38\u5730 \u79bb\u5f00\u4f5c\u7528\u57df \u65f6\uff0c\u6216\u8005\u4f7f\u7528 delete \u5173\u952e\u5b57\u663e\u5f0f\u5730\u5220\u9664\u52a8\u6001\u5206\u914d\u7684\u5bf9\u8c61\u65f6\uff0c\u6790\u6784\u51fd\u6570(\u5982\u679c\u5b83\u5b58\u5728)\u5c31\u4f1a\u88ab\u81ea\u52a8\u8c03\u7528\uff0c\u5728\u91ca\u653e\u5185\u5b58\u524d\u6267\u884c\u5fc5\u8981\u7684\u6e05\u7406\u3002\u5bf9\u4e8e\u7b80\u5355\u7c7b(\u90a3\u4e9b\u53ea\u521d\u59cb\u5316\u666e\u901a\u6210\u5458\u53d8\u91cf\u503c\u7684\u7c7b)\uff0c\u4e0d\u9700\u8981\u6790\u6784\u51fd\u6570\uff0c\u56e0\u4e3aC++\u4f1a\u81ea\u52a8\u4e3a\u4f60\u6e05\u7406\u5185\u5b58\u3002 \u7136\u800c\uff0c\u5982\u679c\u4f60\u7684\u7c7b\u5bf9\u8c61\u6301\u6709\u67d0\u4e9b\u8d44\u6e90(\u4f8b\u5982\u52a8\u6001\u5185\u5b58\u3001\u6587\u4ef6\u6216\u6570\u636e\u5e93\u53e5\u67c4)\uff0c\u6216\u8005\u4f60\u9700\u8981\u5728\u5bf9\u8c61\u88ab\u9500\u6bc1\u4e4b\u524d\u8fdb\u884c\u67d0\u4e9b\u201c\u7ef4\u62a4\u201d\u64cd\u4f5c\uff0c\u6790\u6784\u51fd\u6570\u662f\u8fdb\u884c\u8fd9\u4e9b\u64cd\u4f5c\u7684\u597d\u5730\u65b9\uff0c\u56e0\u4e3a\u6790\u6784\u51fd\u6570\u7684\u8c03\u7528\u901a\u5e38\u662f\u5bf9\u8c61\u88ab\u9500\u6bc1\u4e4b\u524d\u53d1\u751f\u7684\u6700\u540e\u4e00\u4ef6\u4e8b\u3002 \u6790\u6784\u51fd\u6570\u547d\u540d \u548c\u6784\u9020\u51fd\u6570\u4e00\u6837\uff0c\u6790\u6784\u51fd\u6570\u4e5f\u6709\u5176\u7279\u6b8a\u7684\u547d\u540d\u89c4\u5219\uff1a \u6790\u6784\u51fd\u6570\u7684\u540d\u5fc5\u987b\u548c\u7c7b\u540d\u5b8c\u5168\u4e00\u81f4\uff0c\u5e76\u4e14\u5728\u6700\u524d\u9762\u6dfb\u52a0\u6ce2\u6d6a\u53f7(~)\uff1b \u6790\u6784\u51fd\u6570\u6ca1\u6709\u4efb\u4f55\u53c2\u6570\uff1b \u6790\u6784\u51fd\u6570\u4e0d\u8fd4\u56de\u4efb\u4f55\u7c7b\u578b\u3002 \u4e00\u4e2a\u7c7b\u53ea\u80fd\u6709\u4e00\u4e2a\u6790\u6784\u51fd\u6570\u3002 \u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6790\u6784\u51fd\u6570\u4e0d\u5e94\u8be5\u88ab\u663e\u5f0f\u5730\u8c03\u7528(\u56e0\u4e3a\u5f53\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\u4f1a\u81ea\u52a8\u8c03\u7528\u6790\u6784\u51fd\u6570)\uff0c\u56e0\u4e3a\u5f88\u5c11\u6709\u60c5\u51b5\u9700\u8981\u591a\u6b21\u6e05\u7406\u5bf9\u8c61\u3002\u4e0d\u8fc7\uff0c\u6790\u6784\u51fd\u6570\u53ef\u4ee5\u5b89\u5168\u5730\u8c03\u7528\u5176\u4ed6\u6210\u5458\u51fd\u6570\uff0c\u56e0\u4e3a\u76f4\u5230\u6790\u6784\u51fd\u6570\u6267\u884c\u4e4b\u540e\u624d\u5bf9\u8c61\u624d\u4f1a\u88ab\u9500\u6bc1\u3002 \u4e00\u4e2a\u6790\u6784\u51fd\u6570\u6848\u4f8b \u8ba9\u6211\u4eec\u6765\u770b\u770b\u4e00\u4e2a\u4f7f\u7528\u6790\u6784\u51fd\u6570\u7684\u7b80\u5355\u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include #include #include class IntArray { private : int * m_array {}; int m_length {}; public : IntArray ( int length ) // \u6784\u9020\u51fd\u6570 { assert ( length > 0 ); m_array = new int [ static_cast < std :: size_t > ( length )]{}; m_length = length ; } ~ IntArray () // \u6790\u6784\u51fd\u6570 { // \u5220\u9664\u4e4b\u524d\u5206\u914d\u7684\u6570\u7ec4 delete [] m_array ; } void setValue ( int index , int value ) { m_array [ index ] = value ; } int getValue ( int index ) { return m_array [ index ]; } int getLength () { return m_length ; } }; int main () { IntArray ar ( 10 ); // \u5206\u914d 10 \u4e2a\u6574\u578b for ( int count { 0 }; count < ar . getLength (); ++ count ) ar . setValue ( count , count + 1 ); std :: cout << \"The value of element 5 is: \" << ar . getValue ( 5 ) << '\\n' ; return 0 ; } // ar \u5728\u6b64\u5904\u88ab\u9500\u6bc1\uff0c\u6240\u4ee5\u6790\u6784\u51fd\u6570 ~IntArray() \u4f1a\u5728\u6b64\u65f6\u88ab\u8c03\u7528 \u5c0f\u8d34\u58eb \u5982\u679c\u4f60\u5728\u7f16\u8bd1\u4e0a\u9762\u7a0b\u5e8f\u65f6\u4ea7\u751f\u5982\u4e0b\u62a5\u9519\uff1a 1 2 3 error: 'class IntArray' has pointer data members [-Werror=effc++]| error: but does not override 'IntArray(const IntArray&)' [-Werror=effc++]| error: or 'operator=(const IntArray&)' [-Werror=effc++]| \u6b64\u65f6\u53ef\u4ee5\u5220\u9664\u7f16\u8bd1\u5668\u9009\u9879 \u201c-Weffc++\u201d\uff0c\u6216\u8005\u5728\u7c7b\u4e2d\u989d\u5916\u6dfb\u52a0\u4e0b\u9762\u4e24\u884c\u4ee3\u7801\uff1a 1 2 IntArray ( const IntArray & ) = delete ; IntArray & operator = ( const IntArray & ) = delete ; \u8be6\u7ec6\u4fe1\u606f\u53c2\u8003 14.14 -- Converting constructors, explicit, and delete \u8f93\u51fa\u7ed3\u679c\uff1a 1 The value of element 5 is: 6 \u5728 main() \u51fd\u6570\u7684\u7b2c\u4e00\u884c\uff0c\u6211\u4eec\u5b9e\u4f8b\u5316\u4e86\u4e00\u4e2a IntArray \u7c7b\u578b\u7684\u5bf9\u8c61 ar \uff0c\u5e76\u4e14\u4f20\u5165\u4e8610\u4f5c\u4e3a\u957f\u5ea6\u3002 \u6b64\u65f6\u4f1a\u8c03\u7528\u6784\u9020\u51fd\u6570\uff0c\u52a8\u6001\u5206\u914d\u6570\u7ec4\u6240\u9700\u7684\u5185\u5b58\u3002\u5185\u5b58\u5fc5\u987b\u52a8\u6001\u5206\u914d\u56e0\u4e3a\u5728 \u7f16\u8bd1\u65f6 \u65e0\u6cd5\u77e5\u9053\u6570\u7ec4\u7684\u957f\u5ea6\u3002 \u5728 main() \u672b\u5c3e\uff0c ar \u79bb\u5f00\u4f5c\u7528\u57df \uff0c\u8c03\u7528\u6790\u6784\u51fd\u6570 ~IntArray() \uff0c\u5220\u9664\u6784\u9020\u51fd\u6570\u4e2d\u4e3a\u6570\u7ec4\u7533\u8bf7\u7684\u5185\u5b58\u3002 \u63d0\u9192 \u5728 11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 \u4e2d\uff0c\u6211\u4eec\u8bb2\u8fc7\uff0c\u62ec\u53f7\u521d\u59cb\u5316\u5e94\u8be5\u88ab\u7528\u4e8e\u521d\u59cb\u5316\u57fa\u4e8e\u957f\u5ea6\u521b\u5efa\u7684\u6570\u7ec4\u3001\u5bb9\u5668\u548c\u5217\u8868\uff08\u76f8\u5bf9\u4e8e\u57fa\u4e8e\u4e00\u7ec4\u5143\u7d20\u521b\u5efa\uff09\u3002\u56e0\u6b64\uff0c\u521d\u59cb\u5316 IntArray \u65f6\u4f7f\u7528\u4e86 IntArray ar ( 10 ); \u3002 \u6784\u9020\u548c\u6790\u6784\u7684\u65f6\u673a \u548c\u4e4b\u524d\u63d0\u5230\u7684\u4e00\u6837\uff0c\u6784\u9020\u51fd\u6570\u4f1a\u5728\u5bf9\u8c61\u521b\u5efa\u65f6\u8c03\u7528\uff0c\u800c\u6790\u6784\u51fd\u6570\u5219\u662f\u5728\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\u8c03\u7528\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f1a\u5728\u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570\u4e2d\u6dfb\u52a0\u6253\u5370\u6765\u5c55\u793a\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include class Simple { private : int m_nID {}; public : Simple ( int nID ) : m_nID { nID } { std :: cout << \"Constructing Simple \" << nID << '\\n' ; } ~ Simple () { std :: cout << \"Destructing Simple\" << m_nID << '\\n' ; } int getID () { return m_nID ; } }; int main () { // \u5728\u6808\u4e0a\u5206\u914d\u4e00\u4e2a Simple \u5bf9\u8c61 Simple simple { 1 }; std :: cout << simple . getID () << '\\n' ; // \u52a8\u6001\u5206\u914d\u4e00\u4e2a Simple \u5bf9\u8c61 Simple * pSimple { new Simple { 2 } }; std :: cout << pSimple -> getID () << '\\n' ; // \u52a8\u6001\u5206\u914d\u7684 pSimple \u5fc5\u987b\u8981\u88abdelete delete pSimple ; return 0 ; } // simple \u5bf9\u8c61\u4f1a\u5728\u6b64\u5904\u79bb\u5f00\u4f5c\u7528\u57df \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 Constructing Simple 1 1 Constructing Simple 2 2 Destructing Simple 2 Destructing Simple 1 \u6ce8\u610f\uff0c\u201cSimple 1\u201d \u662f\u5728 \u201cSimple 2\u201d \u4e4b\u540e\u9500\u6bc1\u7684\uff0c\u56e0\u4e3a\u6211\u4eec\u5728 main \u51fd\u6570\u7ed3\u675f\u524d\u5220\u9664\u4e86 pSimple \uff0c\u800c simple \u5219\u662f\u5728 main() \u7ed3\u675f\u65f6\u624d\u9500\u6bc1\u3002 \u5168\u5c40\u53d8\u91cf\u4f1a\u5728main()\u51fd\u6570\u524d\u6784\u9020\u800c\u5728main()\u7ed3\u675f\u540e\u9500\u6bc1 \u3002 RAII \u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09 \u662f\u4e00\u79cd\u5c06\u8d44\u6e90\u4f7f\u7528\u4e0e\u5177\u6709\u81ea\u52a8\u6301\u7eed\u65f6\u95f4\u7684\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f(\u4f8b\u5982\uff0c\u975e\u52a8\u6001\u5206\u914d\u7684\u5bf9\u8c61)\u7ed1\u5b9a\u5728\u4e00\u8d77\u7684\u7f16\u7a0b\u6280\u672f\u3002\u5728C++\u4e2d\uff0cRAII\u662f\u901a\u8fc7\u5e26\u6709\u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570\u7684\u7c7b\u5b9e\u73b0\u7684\u3002\u8d44\u6e90(\u5982\u5185\u5b58\u3001\u6587\u4ef6\u6216\u6570\u636e\u5e93\u53e5\u67c4\u7b49)\u901a\u5e38\u662f\u5728\u5bf9\u8c61\u7684\u6784\u9020\u51fd\u6570\u4e2d\u83b7\u53d6\u7684(\u4e0d\u8fc7\uff0c\u5982\u679c\u9700\u8981\u7684\u8bdd\u4e5f\u53ef\u4ee5\u5728\u5bf9\u8c61\u521b\u5efa\u4e4b\u540e\u83b7\u53d6)\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u5728\u5bf9\u8c61\u5904\u4e8e\u6d3b\u52a8\u72b6\u6001\u65f6\u4f7f\u7528\u8be5\u8d44\u6e90\u3002\u5f53\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\uff0c\u8d44\u6e90\u5728\u6790\u6784\u51fd\u6570\u4e2d\u88ab\u91ca\u653e\u3002RAII\u7684\u4e3b\u8981\u4f18\u70b9\u662f\u5b83\u6709\u52a9\u4e8e\u9632\u6b62\u8d44\u6e90\u6cc4\u6f0f(\u4f8b\u5982\uff0c\u5185\u5b58\u6ca1\u6709\u88ab\u91ca\u653e)\uff0c\u56e0\u4e3a\u6240\u6709\u8d44\u6e90\u6301\u6709\u5bf9\u8c61\u90fd\u4f1a\u81ea\u52a8\u6e05\u7406\u3002 \u672c\u8282\u8bfe\u5f00\u59cb\u65f6\u7684 IntArray \u7c7b\u662f\u5b9e\u73b0RAII\u7684\u4e00\u4e2a\u4f8b\u5b50\u2014\u2014\u5728\u6784\u9020\u51fd\u6570\u4e2d\u5206\u914d\uff0c\u5728\u6790\u6784\u51fd\u6570\u4e2d\u91ca\u653e\u3002 std::string \u548c std::vector \u5219\u662f\u6807\u51c6\u5e93\u4e2d\u5b9e\u73b0RAII\u7684\u4f8b\u5b50\u2014\u2014\u52a8\u6001\u5185\u5b58\u5728\u521d\u59cb\u5316\u65f6\u83b7\u5f97\uff0c\u5728\u9500\u6bc1\u65f6\u81ea\u52a8\u6e05\u9664\u3002 \u6709\u5173 exit() \u51fd\u6570\u7684\u8b66\u793a \u6ce8\u610f\uff1a\u5f53\u4f7f\u7528 exit() \u51fd\u6570\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u5728\u4e0d\u8c03\u7528\u4efb\u4f55\u6784\u9020\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\u9000\u51fa\u3002\u5982\u679c\u4f60\u4f9d\u8d56\u4e8e\u6784\u9020\u51fd\u6570\u5728\u7a0b\u5e8f\u9000\u51fa\u65f6\u5b8c\u6210\u6e05\u7406\u5de5\u4f5c\uff08\u4f8b\u5982\uff0c\u5199\u65e5\u5fd7\u6216\u5199\u6570\u636e\u5e93\uff09\u3002 \u5c0f\u7ed3 \u5982\u4f60\u6240\u89c1\uff0c\u5f53\u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570\u88ab\u5b9e\u73b0\u540e\uff0c\u7c7b\u5c31\u53ef\u4ee5\u5728\u521d\u59cb\u5316\u81ea\u5df1\u5e76\u5728\u5176\u9500\u6bc1\u65f6\u5b8c\u6210\u6e05\u7406\u5de5\u4f5c\uff0c\u4e0d\u9700\u8981\u7a0b\u5e8f\u5458\u505a\u4efb\u4f55\u7279\u6b8a\u7684\u5de5\u4f5c\uff01\u8fd9\u964d\u4f4e\u4e86\u51fa\u9519\u7684\u53ef\u80fd\u6027\uff0c\u5e76\u4f7f\u7c7b\u66f4\u5bb9\u6613\u4f7f\u7528\u3002","title":"13.9 - \u6790\u6784\u51fd\u6570"},{"location":"13-9-destructors/#_1","text":"\u548c\u6784\u9020\u51fd\u6570\u4e00\u6837\uff0c\u6790\u6784\u51fd\u6570\u4e5f\u6709\u5176\u7279\u6b8a\u7684\u547d\u540d\u89c4\u5219\uff1a \u6790\u6784\u51fd\u6570\u7684\u540d\u5fc5\u987b\u548c\u7c7b\u540d\u5b8c\u5168\u4e00\u81f4\uff0c\u5e76\u4e14\u5728\u6700\u524d\u9762\u6dfb\u52a0\u6ce2\u6d6a\u53f7(~)\uff1b \u6790\u6784\u51fd\u6570\u6ca1\u6709\u4efb\u4f55\u53c2\u6570\uff1b \u6790\u6784\u51fd\u6570\u4e0d\u8fd4\u56de\u4efb\u4f55\u7c7b\u578b\u3002 \u4e00\u4e2a\u7c7b\u53ea\u80fd\u6709\u4e00\u4e2a\u6790\u6784\u51fd\u6570\u3002 \u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6790\u6784\u51fd\u6570\u4e0d\u5e94\u8be5\u88ab\u663e\u5f0f\u5730\u8c03\u7528(\u56e0\u4e3a\u5f53\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\u4f1a\u81ea\u52a8\u8c03\u7528\u6790\u6784\u51fd\u6570)\uff0c\u56e0\u4e3a\u5f88\u5c11\u6709\u60c5\u51b5\u9700\u8981\u591a\u6b21\u6e05\u7406\u5bf9\u8c61\u3002\u4e0d\u8fc7\uff0c\u6790\u6784\u51fd\u6570\u53ef\u4ee5\u5b89\u5168\u5730\u8c03\u7528\u5176\u4ed6\u6210\u5458\u51fd\u6570\uff0c\u56e0\u4e3a\u76f4\u5230\u6790\u6784\u51fd\u6570\u6267\u884c\u4e4b\u540e\u624d\u5bf9\u8c61\u624d\u4f1a\u88ab\u9500\u6bc1\u3002","title":"\u6790\u6784\u51fd\u6570\u547d\u540d"},{"location":"13-9-destructors/#_2","text":"\u8ba9\u6211\u4eec\u6765\u770b\u770b\u4e00\u4e2a\u4f7f\u7528\u6790\u6784\u51fd\u6570\u7684\u7b80\u5355\u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include #include #include class IntArray { private : int * m_array {}; int m_length {}; public : IntArray ( int length ) // \u6784\u9020\u51fd\u6570 { assert ( length > 0 ); m_array = new int [ static_cast < std :: size_t > ( length )]{}; m_length = length ; } ~ IntArray () // \u6790\u6784\u51fd\u6570 { // \u5220\u9664\u4e4b\u524d\u5206\u914d\u7684\u6570\u7ec4 delete [] m_array ; } void setValue ( int index , int value ) { m_array [ index ] = value ; } int getValue ( int index ) { return m_array [ index ]; } int getLength () { return m_length ; } }; int main () { IntArray ar ( 10 ); // \u5206\u914d 10 \u4e2a\u6574\u578b for ( int count { 0 }; count < ar . getLength (); ++ count ) ar . setValue ( count , count + 1 ); std :: cout << \"The value of element 5 is: \" << ar . getValue ( 5 ) << '\\n' ; return 0 ; } // ar \u5728\u6b64\u5904\u88ab\u9500\u6bc1\uff0c\u6240\u4ee5\u6790\u6784\u51fd\u6570 ~IntArray() \u4f1a\u5728\u6b64\u65f6\u88ab\u8c03\u7528 \u5c0f\u8d34\u58eb \u5982\u679c\u4f60\u5728\u7f16\u8bd1\u4e0a\u9762\u7a0b\u5e8f\u65f6\u4ea7\u751f\u5982\u4e0b\u62a5\u9519\uff1a 1 2 3 error: 'class IntArray' has pointer data members [-Werror=effc++]| error: but does not override 'IntArray(const IntArray&)' [-Werror=effc++]| error: or 'operator=(const IntArray&)' [-Werror=effc++]| \u6b64\u65f6\u53ef\u4ee5\u5220\u9664\u7f16\u8bd1\u5668\u9009\u9879 \u201c-Weffc++\u201d\uff0c\u6216\u8005\u5728\u7c7b\u4e2d\u989d\u5916\u6dfb\u52a0\u4e0b\u9762\u4e24\u884c\u4ee3\u7801\uff1a 1 2 IntArray ( const IntArray & ) = delete ; IntArray & operator = ( const IntArray & ) = delete ; \u8be6\u7ec6\u4fe1\u606f\u53c2\u8003 14.14 -- Converting constructors, explicit, and delete \u8f93\u51fa\u7ed3\u679c\uff1a 1 The value of element 5 is: 6 \u5728 main() \u51fd\u6570\u7684\u7b2c\u4e00\u884c\uff0c\u6211\u4eec\u5b9e\u4f8b\u5316\u4e86\u4e00\u4e2a IntArray \u7c7b\u578b\u7684\u5bf9\u8c61 ar \uff0c\u5e76\u4e14\u4f20\u5165\u4e8610\u4f5c\u4e3a\u957f\u5ea6\u3002 \u6b64\u65f6\u4f1a\u8c03\u7528\u6784\u9020\u51fd\u6570\uff0c\u52a8\u6001\u5206\u914d\u6570\u7ec4\u6240\u9700\u7684\u5185\u5b58\u3002\u5185\u5b58\u5fc5\u987b\u52a8\u6001\u5206\u914d\u56e0\u4e3a\u5728 \u7f16\u8bd1\u65f6 \u65e0\u6cd5\u77e5\u9053\u6570\u7ec4\u7684\u957f\u5ea6\u3002 \u5728 main() \u672b\u5c3e\uff0c ar \u79bb\u5f00\u4f5c\u7528\u57df \uff0c\u8c03\u7528\u6790\u6784\u51fd\u6570 ~IntArray() \uff0c\u5220\u9664\u6784\u9020\u51fd\u6570\u4e2d\u4e3a\u6570\u7ec4\u7533\u8bf7\u7684\u5185\u5b58\u3002 \u63d0\u9192 \u5728 11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 \u4e2d\uff0c\u6211\u4eec\u8bb2\u8fc7\uff0c\u62ec\u53f7\u521d\u59cb\u5316\u5e94\u8be5\u88ab\u7528\u4e8e\u521d\u59cb\u5316\u57fa\u4e8e\u957f\u5ea6\u521b\u5efa\u7684\u6570\u7ec4\u3001\u5bb9\u5668\u548c\u5217\u8868\uff08\u76f8\u5bf9\u4e8e\u57fa\u4e8e\u4e00\u7ec4\u5143\u7d20\u521b\u5efa\uff09\u3002\u56e0\u6b64\uff0c\u521d\u59cb\u5316 IntArray \u65f6\u4f7f\u7528\u4e86 IntArray ar ( 10 ); \u3002","title":"\u4e00\u4e2a\u6790\u6784\u51fd\u6570\u6848\u4f8b"},{"location":"13-9-destructors/#_3","text":"\u548c\u4e4b\u524d\u63d0\u5230\u7684\u4e00\u6837\uff0c\u6784\u9020\u51fd\u6570\u4f1a\u5728\u5bf9\u8c61\u521b\u5efa\u65f6\u8c03\u7528\uff0c\u800c\u6790\u6784\u51fd\u6570\u5219\u662f\u5728\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\u8c03\u7528\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f1a\u5728\u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570\u4e2d\u6dfb\u52a0\u6253\u5370\u6765\u5c55\u793a\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include class Simple { private : int m_nID {}; public : Simple ( int nID ) : m_nID { nID } { std :: cout << \"Constructing Simple \" << nID << '\\n' ; } ~ Simple () { std :: cout << \"Destructing Simple\" << m_nID << '\\n' ; } int getID () { return m_nID ; } }; int main () { // \u5728\u6808\u4e0a\u5206\u914d\u4e00\u4e2a Simple \u5bf9\u8c61 Simple simple { 1 }; std :: cout << simple . getID () << '\\n' ; // \u52a8\u6001\u5206\u914d\u4e00\u4e2a Simple \u5bf9\u8c61 Simple * pSimple { new Simple { 2 } }; std :: cout << pSimple -> getID () << '\\n' ; // \u52a8\u6001\u5206\u914d\u7684 pSimple \u5fc5\u987b\u8981\u88abdelete delete pSimple ; return 0 ; } // simple \u5bf9\u8c61\u4f1a\u5728\u6b64\u5904\u79bb\u5f00\u4f5c\u7528\u57df \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 Constructing Simple 1 1 Constructing Simple 2 2 Destructing Simple 2 Destructing Simple 1 \u6ce8\u610f\uff0c\u201cSimple 1\u201d \u662f\u5728 \u201cSimple 2\u201d \u4e4b\u540e\u9500\u6bc1\u7684\uff0c\u56e0\u4e3a\u6211\u4eec\u5728 main \u51fd\u6570\u7ed3\u675f\u524d\u5220\u9664\u4e86 pSimple \uff0c\u800c simple \u5219\u662f\u5728 main() \u7ed3\u675f\u65f6\u624d\u9500\u6bc1\u3002 \u5168\u5c40\u53d8\u91cf\u4f1a\u5728main()\u51fd\u6570\u524d\u6784\u9020\u800c\u5728main()\u7ed3\u675f\u540e\u9500\u6bc1 \u3002","title":"\u6784\u9020\u548c\u6790\u6784\u7684\u65f6\u673a"},{"location":"13-9-destructors/#raii","text":"\u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09 \u662f\u4e00\u79cd\u5c06\u8d44\u6e90\u4f7f\u7528\u4e0e\u5177\u6709\u81ea\u52a8\u6301\u7eed\u65f6\u95f4\u7684\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f(\u4f8b\u5982\uff0c\u975e\u52a8\u6001\u5206\u914d\u7684\u5bf9\u8c61)\u7ed1\u5b9a\u5728\u4e00\u8d77\u7684\u7f16\u7a0b\u6280\u672f\u3002\u5728C++\u4e2d\uff0cRAII\u662f\u901a\u8fc7\u5e26\u6709\u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570\u7684\u7c7b\u5b9e\u73b0\u7684\u3002\u8d44\u6e90(\u5982\u5185\u5b58\u3001\u6587\u4ef6\u6216\u6570\u636e\u5e93\u53e5\u67c4\u7b49)\u901a\u5e38\u662f\u5728\u5bf9\u8c61\u7684\u6784\u9020\u51fd\u6570\u4e2d\u83b7\u53d6\u7684(\u4e0d\u8fc7\uff0c\u5982\u679c\u9700\u8981\u7684\u8bdd\u4e5f\u53ef\u4ee5\u5728\u5bf9\u8c61\u521b\u5efa\u4e4b\u540e\u83b7\u53d6)\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u5728\u5bf9\u8c61\u5904\u4e8e\u6d3b\u52a8\u72b6\u6001\u65f6\u4f7f\u7528\u8be5\u8d44\u6e90\u3002\u5f53\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\uff0c\u8d44\u6e90\u5728\u6790\u6784\u51fd\u6570\u4e2d\u88ab\u91ca\u653e\u3002RAII\u7684\u4e3b\u8981\u4f18\u70b9\u662f\u5b83\u6709\u52a9\u4e8e\u9632\u6b62\u8d44\u6e90\u6cc4\u6f0f(\u4f8b\u5982\uff0c\u5185\u5b58\u6ca1\u6709\u88ab\u91ca\u653e)\uff0c\u56e0\u4e3a\u6240\u6709\u8d44\u6e90\u6301\u6709\u5bf9\u8c61\u90fd\u4f1a\u81ea\u52a8\u6e05\u7406\u3002 \u672c\u8282\u8bfe\u5f00\u59cb\u65f6\u7684 IntArray \u7c7b\u662f\u5b9e\u73b0RAII\u7684\u4e00\u4e2a\u4f8b\u5b50\u2014\u2014\u5728\u6784\u9020\u51fd\u6570\u4e2d\u5206\u914d\uff0c\u5728\u6790\u6784\u51fd\u6570\u4e2d\u91ca\u653e\u3002 std::string \u548c std::vector \u5219\u662f\u6807\u51c6\u5e93\u4e2d\u5b9e\u73b0RAII\u7684\u4f8b\u5b50\u2014\u2014\u52a8\u6001\u5185\u5b58\u5728\u521d\u59cb\u5316\u65f6\u83b7\u5f97\uff0c\u5728\u9500\u6bc1\u65f6\u81ea\u52a8\u6e05\u9664\u3002","title":"RAII"},{"location":"13-9-destructors/#exit","text":"\u6ce8\u610f\uff1a\u5f53\u4f7f\u7528 exit() \u51fd\u6570\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u5728\u4e0d\u8c03\u7528\u4efb\u4f55\u6784\u9020\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\u9000\u51fa\u3002\u5982\u679c\u4f60\u4f9d\u8d56\u4e8e\u6784\u9020\u51fd\u6570\u5728\u7a0b\u5e8f\u9000\u51fa\u65f6\u5b8c\u6210\u6e05\u7406\u5de5\u4f5c\uff08\u4f8b\u5982\uff0c\u5199\u65e5\u5fd7\u6216\u5199\u6570\u636e\u5e93\uff09\u3002","title":"\u6709\u5173 exit() \u51fd\u6570\u7684\u8b66\u793a"},{"location":"13-9-destructors/#_4","text":"\u5982\u4f60\u6240\u89c1\uff0c\u5f53\u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570\u88ab\u5b9e\u73b0\u540e\uff0c\u7c7b\u5c31\u53ef\u4ee5\u5728\u521d\u59cb\u5316\u81ea\u5df1\u5e76\u5728\u5176\u9500\u6bc1\u65f6\u5b8c\u6210\u6e05\u7406\u5de5\u4f5c\uff0c\u4e0d\u9700\u8981\u7a0b\u5e8f\u5458\u505a\u4efb\u4f55\u7279\u6b8a\u7684\u5de5\u4f5c\uff01\u8fd9\u964d\u4f4e\u4e86\u51fa\u9519\u7684\u53ef\u80fd\u6027\uff0c\u5e76\u4f7f\u7c7b\u66f4\u5bb9\u6613\u4f7f\u7528\u3002","title":"\u5c0f\u7ed3"},{"location":"13-x-Chapter-13-comprehensive-quiz/","tags":["summary"],"text":"13.2 - \u7c7b\u548c\u7c7b\u6210\u5458 \u4f7f\u7528\u7c7b\u53ef\u4ee5\u521b\u5efa\u81ea\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5e76\u4e14\u5c06\u6570\u636e\u548c\u51fd\u6570\u548c\u8be5\u6570\u636e\u7c7b\u578b\u7ed1\u5b9a\u5e76\u914d\u5408\u4f7f\u7528\u3002\u7c7b\u4e2d\u7684\u6570\u636e\u548c\u51fd\u6570\u79f0\u4e3a\u6210\u5458\uff0c\u800c\u9009\u62e9\u7c7b\u4e2d\u7684\u6210\u5458\u9700\u8981\u4f7f\u7528\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 . \uff08\u5728\u4f7f\u7528\u6307\u9488\u8bbf\u95ee\u65f6\uff0c\u4f7f\u7528 -> \u8fd0\u7b97\u7b26\uff09\u3002 13.3 - \u8bbf\u95ee\u4fee\u9970\u7b26 public \u548c private \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u5141\u8bb8\u7528\u6237\u9650\u5b9a\u7c7b\u6210\u5458\u53ef\u4ee5\u88ab\u8c01\u8bbf\u95ee\u3002 public \u6210\u5458\u53ef\u4ee5\u88ab\u4efb\u4f55\u4eba\u4f7f\u7528\u3002 private \u6210\u5458\u53ea\u80fd\u88ab\u7c7b\u4e2d\u7684\u5176\u4ed6\u6210\u5458\u8bbf\u95ee\u3002\u7a0d\u540e\u6211\u4eec\u8fd8\u4f1a\u4ecb\u7ecd protected \u6210\u5458\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u7c7b\u6210\u5458\u90fd\u662f private \u7684\uff0c\u800c\u7ed3\u6784\u4f53\u7684\u6210\u5458\u90fd\u662f public \u7684\u3002 13.4 - \u6210\u5458\u8bbf\u95ee\u51fd\u6570\u4e0e\u5c01\u88c5 \u5c01\u88c5 \u662f\u5c06\u6210\u5458\u8bbe\u4e3aprivate\u7684\u8fc7\u7a0b\uff0c\u8fd9\u6837\u5b83\u4eec\u5c31\u4e0d\u80fd\u88ab\u76f4\u63a5\u8bbf\u95ee\u3002\u8fd9\u6709\u52a9\u4e8e\u4fdd\u62a4\u7c7b\u4e0d\u88ab\u6ee5\u7528\u3002 13.5 - \u6784\u9020\u51fd\u6570 \u6784\u9020\u51fd\u6570 \u662f\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\u7684\u51fd\u6570\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u521d\u59cb\u5316\u8be5\u7c7b\u7684\u5bf9\u8c61\u3002\u4e0d\u5177\u6709\u4efb\u4f55 \u5f62\u53c2 \uff08\u6216\u5177\u6709\u5168\u90e8\u9ed8\u8ba4\u5f62\u53c2\uff09\u7684\u6784\u9020\u51fd\u6570\u662f\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002\u5f53\u7528\u6237\u6ca1\u6709\u63d0\u4f9b\u4efb\u4f55\u521d\u59cb\u5316\u503c\u7684\u65f6\u5019\uff0c\u9ed8\u8ba4\u51fd\u6570\u5c06\u4f1a\u88ab\u8c03\u7528\u3002\u4f60\u5e94\u8be5\u81f3\u5c11\u4e3a\u7c7b\u63d0\u4f9b\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u3002 13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u5217\u8868 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u5141\u8bb8\u7528\u6237\u5728\u6784\u9020\u51fd\u6570\u4e2d\u5bf9\u6210\u5458\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\uff08\u800c\u4e0d\u5fc5\u4e3a\u6bcf\u4e2a\u6210\u5458\u53d8\u91cf\u8d4b\u503c\uff09\u3002 13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u5141\u8bb8\u4f60\u5728\u58f0\u660e\u6210\u5458\u65f6\u4e3a\u53d8\u91cf\u76f4\u63a5\u6307\u5b9a\u9ed8\u8ba4\u503c\u3002 13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570 \u6784\u9020\u51fd\u6570 \u4e5f\u53ef\u4ee5\u8c03\u7528\u5176\u4ed6\u6784\u9020\u51fd\u6570\uff08\u79f0\u4e3a \u59d4\u6258\u6784\u9020\u51fd\u6570 \u6216\u6784\u9020\u51fd\u6570\u94fe\uff09\u3002 13.9 - \u6790\u6784\u51fd\u6570 \u6790\u6784\u51fd\u6570 \u662f\u53e6\u5916\u4e00\u7c7b\u7279\u6b8a\u7684\u6784\u9020\u51fd\u6570\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u8ba9\u7c7b\u5bf9\u81ea\u5df1\u8fdb\u884c\u6e05\u7406\u3002\u4efb\u4f55\u5185\u5b58\u56de\u6536\u6216\u9000\u51fa\u6d41\u7a0b\u90fd\u5e94\u8be5\u5728\u6790\u6784\u51fd\u6570\u4e2d\u6267\u884c\u3002 13.10 - \u9690\u85cf\u7684this\u6307\u9488 \u6240\u6709\u7684\u6210\u5458\u51fd\u6570\u90fd\u6709\u4e00\u4e2a\u9690\u85cf\u7684\u6307\u9488 this \uff0c this \u6307\u9488\u6307\u5411\u8fd9\u4e2a\u5bf9\u8c61\u672c\u8eab\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4f60\u5e76\u4e0d\u9700\u8981\u76f4\u63a5\u8bbf\u95ee\u8be5\u6307\u9488\uff0c\u4f46\u662f\u5076\u5c14\u4e5f\u4f1a\u6709\u8fd9\u6837\u7684\u9700\u6c42\u3002 13.11 - \u7c7b\u4ee3\u7801\u548c\u5934\u6587\u4ef6 \u4e00\u4e2a\u597d\u7684\u7f16\u7a0b\u5b9e\u8df5\u662f\u8fd9\u6837\u7684\uff1a\u5c06\u7c7b\u7684\u5b9a\u4e49\u653e\u7f6e\u5728\u5176\u540c\u540d\u7684\u5934\u6587\u4ef6\u5185\uff0c\u7136\u540e\u5c06\u7c7b\u7684\u6210\u5458\u5b9a\u4e49\u5728\u53e6\u4e00\u4e2a\u4e0e\u7c7b\u540c\u540d\u7684 .cpp \u6587\u4ef6\u4e2d\u3002 \u8fd9\u6837\u6709\u52a9\u4e8e\u907f\u514d\u5faa\u73af\u4f9d\u8d56\u3002 13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570 \u5982\u679c\u6210\u5458\u51fd\u6570\u4e0d\u4fee\u6539\u7c7b\u7684\u72b6\u6001\uff0c\u5219\u53ef\u4ee5\uff08\u4e5f\u5e94\u8be5\uff09\u88ab\u58f0\u660e\u4e3aconst\u7c7b\u578b\u3002const \u5bf9\u8c61\u4e5f\u53ea\u80fd\u8c03\u7528const\u6210\u5458\u51fd\u6570\u3002 13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf & 13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570 \u9759\u6001\u6210\u5458\u53d8\u91cf \u5728\u8be5\u7c7b\u7684\u6240\u6709\u5bf9\u8c61\u95f4\u5171\u4eab\u3002\u5b83\u4eec\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8c61\u6765\u8bbf\u95ee\uff0c\u5b83\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u6765\u8bbf\u95ee\u3002 \u9759\u6001\u6210\u5458\u51fd\u6570 \u6ca1\u6709 this \u6307\u9488\uff0c\u5b83\u4eec\u53ea\u80fd\u591f\u88ab\u9759\u6001\u6210\u5458\u53d8\u91cf\u8bbf\u95ee\u3002 13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b \u53cb\u5143\u51fd\u6570 \u88ab\u5f53\u505a\u7c7b\u6210\u5458\u51fd\u6570\u4f7f\u7528\uff08\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u7c7b\u7684\u79c1\u6709\u6570\u636e\uff09\u3002 \u53cb\u5143\u7c7b \u7684\u6240\u6709\u6210\u5458\u51fd\u6570\u90fd\u88ab\u770b\u505a\u662f\u53cb\u5143\u51fd\u6570\u3002 13.16 - \u533f\u540d\u5bf9\u8c61 \u5728\u8868\u8fbe\u5f0f\u6c42\u503c\uff0c\u6216\u8005\u4f20\u9012\u3001\u8fd4\u56de\u503c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u533f\u540d\u5bf9\u8c61\u3002 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b \u4f60\u4e5f\u53ef\u4ee5\u5728\u7c7b\u4e2d\u5d4c\u5957\u7c7b\u578b\u3002\u901a\u5e38\u662f\u679a\u4e3e\u7c7b\u578b\uff0c\u4e0d\u8fc7\u5982\u6709\u9700\u8981\u4e5f\u53ef\u4ee5\u662f\u5176\u4ed6\u7c7b\u578b\uff08\u6216\u8005\u5176\u4ed6\u7c7b\uff09\u3002","title":"13.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u9762\u5411\u5bf9\u8c61\u57fa\u7840"},{"location":"14-1-introduction-to-operator-overloading/","tags":["operator","overload"],"text":"Key Takeaway \u5728\u8bfe\u7a0b 8.9 - \u51fd\u6570\u91cd\u8f7d \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u51fd\u6570\u91cd\u8f7d\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u673a\u5236\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u540d\u79f0\u521b\u5efa\u548c\u89e3\u6790\u591a\u4e2a\u51fd\u6570\u8c03\u7528\uff0c\u53ea\u8981\u6bcf\u4e2a\u51fd\u6570\u90fd\u5177\u6709\u552f\u4e00\u7684\u51fd\u6570\u539f\u578b\u5373\u53ef\u3002\u8fd9\u6837\uff0c\u4f60\u5c31\u53ef\u4ee5\u521b\u5efa\u4e0e\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u4e00\u8d77\u4f7f\u7528\u7684\u51fd\u6570\u53d8\u4f53\uff0c\u800c\u65e0\u9700\u4e3a\u6bcf\u4e2a\u53d8\u4f53\u60f3\u51fa\u4e00\u4e2a\u72ec\u7279\u7684\u540d\u79f0\u3002 \u5728C++\u4e2d\uff0c\u64cd\u4f5c\u7b26\u4e5f\u88ab\u5b9e\u73b0\u4e3a\u51fd\u6570\u3002\u901a\u8fc7\u5728\u64cd\u4f5c\u7b26\u51fd\u6570\u4e0a\u4f7f\u7528\u51fd\u6570\u91cd\u8f7d\uff0c\u4f60\u53ef\u4ee5\u81ea\u5b9a\u4e49\u64cd\u4f5c\u7b26\u7684\u529f\u80fd\uff0c\u4f7f\u5176\u53ef\u4ee5\u4e0e\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u4e00\u8d77\u4f7f\u7528\uff08\u5305\u62ec\u4f60\u7f16\u5199\u7684\u7c7b\uff09\u3002\u4f7f\u7528\u51fd\u6570\u91cd\u8f7d\u6765\u91cd\u8f7d\u64cd\u4f5c\u7b26\u79f0\u4e3a \u64cd\u4f5c\u7b26\u91cd\u8f7d \u3002 \u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u68c0\u67e5\u4e0e\u64cd\u4f5c\u7b26\u91cd\u8f7d\u76f8\u5173\u7684\u4e3b\u9898\u3002 \u64cd\u4f5c\u7b26\u4f5c\u4e3a\u51fd\u6570 \u8003\u8651\u4ee5\u4e0b\u793a\u4f8b\uff1a 1 2 3 int x { 2 }; int y { 3 }; std :: cout << x + y << '\\\\\\\\\\\\\\\\ n ' ; \u7f16\u8bd1\u5668\u63d0\u4f9b\u4e86\u7528\u4e8e\u6574\u6570\u64cd\u4f5c\u6570\u7684\u52a0\u53f7\u8fd0\u7b97\u7b26\uff08 + \uff09\u7684\u5185\u7f6e\u7248\u672c \u2014\u2014 \u8be5\u51fd\u6570\u5c06\u6574\u6570 x \u548c y \u76f8\u52a0\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u7ed3\u679c\u3002\u5f53\u4f60\u770b\u5230\u8868\u8fbe\u5f0f x + y \u65f6\uff0c\u4f60\u53ef\u4ee5\u5c06\u5176\u5728\u5934\u8111\u4e2d\u7ffb\u8bd1\u4e3a\u51fd\u6570\u8c03\u7528 operator+(x, y) \uff08\u5176\u4e2d operator+ \u662f\u51fd\u6570\u7684\u540d\u79f0\uff09\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 double z { 2.0 }; double w { 3.0 }; std :: cout << w + z << '\\\\\\\\\\\\\\\\ n ' ; \u7f16\u8bd1\u5668\u4e5f\u9644\u5e26\u4e86\u53cc\u7cbe\u5ea6\u64cd\u4f5c\u6570\u7684\u52a0\u53f7\u8fd0\u7b97\u7b26\uff08 + \uff09\u7684\u5185\u7f6e\u7248\u672c\u3002\u8868\u8fbe\u5f0f w + z \u53d8\u4e3a\u51fd\u6570\u8c03\u7528 operator+(w, z) \uff0c\u5e76\u4f7f\u7528\u51fd\u6570\u91cd\u8f7d\u6765\u786e\u5b9a\u7f16\u8bd1\u5668\u5e94\u8be5\u8c03\u7528\u6b64\u51fd\u6570\u7684\u53cc\u7cbe\u5ea6\u7248\u672c\u800c\u4e0d\u662f\u6574\u6570\u7248\u672c\u3002 \u73b0\u5728\u8003\u8651\u5982\u679c\u6211\u4eec\u5c1d\u8bd5\u5c06\u4e24\u4e2a\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u7684\u5bf9\u8c61\u76f8\u52a0\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1a 1 2 3 Mystring string1 { \"Hello, \" }; Mystring string2 { \"World!\" }; std :: cout << string1 + string2 << '\\\\\\\\\\\\\\\\ n ' ; \u8fd0\u884c\u7ed3\u679c\u5982\u4f55\uff1f\u5b57\u7b26\u4e32\u201cHello\uff0cWorld\uff01\u201d\u672c\u5e94\u8be5\u5c06\u4f1a\u88ab\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\u3002\u4f46\u662f\uff0c\u7531\u4e8eMystring\u662f\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\uff0c\u7f16\u8bd1\u5668\u6ca1\u6709\u53ef\u7528\u4e8e Mystring \u7c7b\u578b\u64cd\u4f5c\u6570\u7684\u52a0\u53f7\u8fd0\u7b97\u7b26\u3002\u56e0\u6b64\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u544a\u9519\u8bef\u3002\u4e3a\u4e86\u4f7f\u5176\u6309\u7167\u6211\u4eec\u60f3\u8981\u7684\u65b9\u5f0f\u5de5\u4f5c\uff0c\u5219\u9700\u8981\u91cd\u8f7d\u51fd\u6570\u2014\u2014\u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u4f55\u5c06 + \u64cd\u4f5c\u7b26\u4e0e\u4e24\u4e2a Mystring \u7c7b\u578b\u7684\u64cd\u4f5c\u6570\u4e00\u8d77\u4f7f\u7528\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u770b\u5230\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9\u3002 \u89e3\u6790\u91cd\u8f7d\u64cd\u4f5c\u7b26 \u5728\u8ba1\u7b97\u5305\u542b\u64cd\u4f5c\u7b26\u7684\u8868\u8fbe\u5f0f\u65f6\uff0c\u7f16\u8bd1\u5668\u4f7f\u7528\u4ee5\u4e0b\u89c4\u5219\uff1a \u5982\u679c \u6240\u6709 \u64cd\u4f5c\u6570\u90fd\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u5219\u7f16\u8bd1\u5668\u5c06\u8c03\u7528\u5185\u7f6e\u4f8b\u7a0b\uff08\u5982\u679c\u5b58\u5728\uff09\u3002\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u7f16\u8bd1\u5668\u5c06\u751f\u6210\u7f16\u8bd1\u5668\u9519\u8bef\u3002 \u5982\u679c \u4efb\u4f55 \u64cd\u4f5c\u6570\u662f\u7528\u6237\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982\u4f60\u7684\u7c7b\u4e4b\u4e00\u6216\u679a\u4e3e\u7c7b\u578b\uff09\uff0c\u5219\u7f16\u8bd1\u5668\u5c06\u67e5\u770b\u8be5\u7c7b\u578b\u662f\u5426\u5177\u6709\u5339\u914d\u7684\u91cd\u8f7d\u64cd\u4f5c\u7b26\u51fd\u6570\u53ef\u4ee5\u8c03\u7528\u3002\u5982\u679c\u627e\u4e0d\u5230\uff0c\u5b83\u5c06\u5c1d\u8bd5\u5c06\u4e00\u4e2a\u6216\u591a\u4e2a\u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u64cd\u4f5c\u6570\u8f6c\u6362\u4e3a\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u4ee5\u4fbf\u5b83\u53ef\u4ee5\u4f7f\u7528\u5339\u914d\u7684\u5185\u7f6e\u64cd\u4f5c\u7b26\uff08\u901a\u8fc7\u91cd\u8f7d\u578b\u8f6c\u6362\uff0c\u6211\u4eec\u5c06\u5728\u672c\u7ae0\u7684\u540e\u9762\u4ecb\u7ecd\uff09\u3002\u5982\u679c\u5931\u8d25\uff0c\u5219\u5b83\u5c06\u751f\u6210\u7f16\u8bd1\u9519\u8bef\u3002 \u64cd\u4f5c\u7b26\u91cd\u8f7d\u7684\u9650\u5236\u662f\u4ec0\u4e48\uff1f \u9996\u5148\uff0cC++\u4e2d\u51e0\u4e4e\u53ef\u4ee5\u91cd\u8f7d\u6240\u6709\u73b0\u6709\u64cd\u4f5c\u7b26\u3002\u4f8b\u5916\u60c5\u51b5\u662f\uff1a\u6761\u4ef6\uff08 ?: \uff09\uff0csizeof\uff0c\u8303\u56f4\uff08 :: \uff09\uff0c\u6210\u5458\u9009\u62e9\u5668\uff08 . \uff09\uff0c\u6307\u9488\u6210\u5458\u9009\u62e9\u5668\uff08 -> \uff09\uff0ctypeid\u4ee5\u53ca\u8f6c\u6362\u64cd\u4f5c\u7b26\u3002 \u5176\u6b21\uff0c\u4f60\u53ea\u80fd\u91cd\u8f7d\u73b0\u6709\u7684\u64cd\u4f5c\u7b26\u3002\u4f60\u4e0d\u80fd\u521b\u5efa\u65b0\u64cd\u4f5c\u7b26\u6216\u91cd\u547d\u540d\u73b0\u6709\u64cd\u4f5c\u7b26\u3002\u4f8b\u5982\uff0c\u4f60\u4e0d\u80fd\u521b\u5efa\u4e00\u4e2a\u64cd\u4f5c\u7b26 ** \u6765\u6267\u884c\u5e42\u3002 \u7b2c\u4e09\uff0c\u91cd\u8f7d\u64cd\u4f5c\u7b26\u4e2d\u81f3\u5c11\u6709\u4e00\u4e2a\u64cd\u4f5c\u6570\u5fc5\u987b\u662f\u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u3002\u8fd9\u610f\u5473\u7740\u4f60\u4e0d\u80fd\u91cd\u8f7d\u52a0\u53f7\u64cd \u7b2c\u56db\uff0c\u4e0d\u53ef\u80fd\u6539\u53d8\u64cd\u4f5c\u7b26\u652f\u6301\u7684\u64cd\u4f5c\u6570\u7684\u6570\u91cf\u3002 \u6700\u540e\uff0c\u6240\u6709\u64cd\u4f5c\u7b26\u90fd\u4fdd\u6301\u5176\u9ed8\u8ba4\u7684\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u6027\uff08\u65e0\u8bba\u5b83\u4eec\u7528\u4e8e\u4ec0\u4e48\uff09\uff0c\u8fd9\u662f\u4e0d\u80fd\u6539\u53d8\u7684\u3002 \u4e00\u4e9b\u65b0\u7684\u7a0b\u5e8f\u5458\u8bd5\u56fe\u91cd\u8f7d\u4f4d\u8fd0\u7b97XOR\u64cd\u4f5c\u7b26\uff08 ^ \uff09\u6765\u5b9e\u73b0\u6307\u6570\u8fd0\u7b97\u3002\u4f46\u662f\uff0c\u5728C++\u4e2d\uff0c operator^ \u7684\u4f18\u5148\u7ea7\u4f4e\u4e8e\u57fa\u672c\u7b97\u672f\u8fd0\u7b97\u7b26\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u8868\u8fbe\u5f0f\u8bc4\u4f30\u4e0d\u6b63\u786e\u3002 \u5728\u57fa\u672c\u6570\u5b66\u4e2d\uff0c\u6307\u6570\u8fd0\u7b97\u4f18\u5148\u4e8e\u57fa\u672c\u7b97\u672f\u8fd0\u7b97\uff0c\u56e0\u6b64 4 + 3 ^ 2 \u89e3\u6790\u4e3a 4 + (3 ^ 2) => 4 + 9 => 13 \u3002\u4f46\u662f\uff0c\u5728C++\u4e2d\uff0c\u7b97\u672f\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e operator^ \uff0c\u56e0\u6b64 4 + 3 ^ 2 \u89e3\u6790\u4e3a\uff08 4 + 3\uff09^ 2 => 7 ^ 2 => 49 \u3002 \u8981\u6b63\u786e\u4f7f\u7528\u5b83\uff0c\u6bcf\u6b21\u90fd\u663e\u5f0f\u5730\u4e3a\u6307\u6570\u90e8\u5206\u6dfb\u52a0\u62ec\u53f7\uff08\u4f8b\u5982 4 + (3 ^ 2) \uff09\uff0c\u8fd9\u5e76\u4e0d\u76f4\u89c2\uff0c\u5e76\u4e14\u53ef\u80fd\u5b58\u5728\u9519\u8bef\u3002 \u56e0\u6b64\uff0c\u51fa\u4e8e\u8fd9\u4e2a\u4f18\u5148\u7ea7\u95ee\u9898\uff0c\u6700\u597d\u53ea\u4ee5\u7c7b\u4f3c\u4e8e\u5176\u539f\u59cb\u610f\u56fe\u7684\u65b9\u5f0f\u4f7f\u7528\u8fd0\u7b97\u7b26\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u91cd\u8f7d\u64cd\u4f5c\u7b26\u65f6\uff0c\u6700\u597d\u4f7f\u64cd\u4f5c\u7b26\u7684\u529f\u80fd\u5c3d\u53ef\u80fd\u63a5\u8fd1\u64cd\u4f5c\u7b26\u7684\u539f\u59cb\u610f\u56fe\u3002 \u6b64\u5916\uff0c\u7531\u4e8e\u64cd\u4f5c\u7b26\u6ca1\u6709\u63cf\u8ff0\u6027\u7684\u540d\u79f0\uff0c\u5b83\u4eec\u7684\u7528\u9014\u5e76\u4e0d\u603b\u662f\u5f88\u6e05\u695a\u3002\u4f8b\u5982\uff0c operator+ \u53ef\u80fd\u662f\u5b57\u7b26\u4e32\u7c7b\u8fde\u63a5\u5b57\u7b26\u4e32\u7684\u5408\u7406\u9009\u62e9\u3002\u4f46\u662f operator- \u5462\uff1f\u4f60\u671f\u671b\u5b83\u505a\u4ec0\u4e48\uff1f\u8fd9\u4e0d\u6e05\u695a\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u91cd\u8f7d\u64cd\u4f5c\u7b26\u7684\u542b\u4e49\u4e0d\u6e05\u6670\u3001\u4e0d\u76f4\u89c2\uff0c\u8bf7\u4f7f\u7528\u547d\u540d\u51fd\u6570\u4ee3\u66ff\u64cd\u4f5c\u7b26\u91cd\u8f7d\u3002 \u5728\u8fd9\u4e9b\u9650\u5236\u5185\uff0c\u4f60\u4ecd\u7136\u53ef\u4ee5\u627e\u5230\u8bb8\u591a\u6709\u7528\u7684\u529f\u80fd\u6765\u91cd\u8f7d\u4f60\u7684\u81ea\u5b9a\u4e49\u7c7b\uff01\u4f60\u53ef\u4ee5\u91cd\u8f7d + \u8fd0\u7b97\u7b26\u6765\u8fde\u63a5\u4f60\u7684\u7528\u6237\u5b9a\u4e49\u7684\u5b57\u7b26\u4e32\u7c7b\uff0c\u6216\u5c06\u4e24\u4e2a Fraction \u7c7b\u5bf9\u8c61\u76f8\u52a0\u3002\u4f60\u53ef\u4ee5\u91cd\u8f7d << \u8fd0\u7b97\u7b26\uff0c\u4ee5\u4fbf\u8f7b\u677e\u5c06\u4f60\u7684\u7c7b\u6253\u5370\u5230\u5c4f\u5e55\uff08\u6216\u6587\u4ef6\uff09\u3002\u4f60\u53ef\u4ee5\u91cd\u8f7d\u7b49\u5f0f\u8fd0\u7b97\u7b26\uff08 == \uff09\u6765\u6bd4\u8f83\u4e24\u4e2a\u7c7b\u5bf9\u8c61\u3002\u8fd9\u4f7f\u5f97\u8fd0\u7b97\u7b26\u91cd\u8f7d\u6210\u4e3aC ++\u4e2d\u6700\u6709\u7528\u7684\u529f\u80fd\u4e4b\u4e00 \u2014\u2014 \u56e0\u4e3a\u5b83\u5141\u8bb8\u4f60\u4ee5\u66f4\u76f4\u89c2\u7684\u65b9\u5f0f\u4f7f\u7528\u4f60\u7684\u7c7b\u3002 \u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u66f4\u6df1\u5165\u5730\u7814\u7a76\u91cd\u8f7d\u4e0d\u540c\u7c7b\u578b\u7684\u8fd0\u7b97\u7b26\u3002","title":"14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb"},{"location":"14-1-introduction-to-operator-overloading/#_1","text":"\u8003\u8651\u4ee5\u4e0b\u793a\u4f8b\uff1a 1 2 3 int x { 2 }; int y { 3 }; std :: cout << x + y << '\\\\\\\\\\\\\\\\ n ' ; \u7f16\u8bd1\u5668\u63d0\u4f9b\u4e86\u7528\u4e8e\u6574\u6570\u64cd\u4f5c\u6570\u7684\u52a0\u53f7\u8fd0\u7b97\u7b26\uff08 + \uff09\u7684\u5185\u7f6e\u7248\u672c \u2014\u2014 \u8be5\u51fd\u6570\u5c06\u6574\u6570 x \u548c y \u76f8\u52a0\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u7ed3\u679c\u3002\u5f53\u4f60\u770b\u5230\u8868\u8fbe\u5f0f x + y \u65f6\uff0c\u4f60\u53ef\u4ee5\u5c06\u5176\u5728\u5934\u8111\u4e2d\u7ffb\u8bd1\u4e3a\u51fd\u6570\u8c03\u7528 operator+(x, y) \uff08\u5176\u4e2d operator+ \u662f\u51fd\u6570\u7684\u540d\u79f0\uff09\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 double z { 2.0 }; double w { 3.0 }; std :: cout << w + z << '\\\\\\\\\\\\\\\\ n ' ; \u7f16\u8bd1\u5668\u4e5f\u9644\u5e26\u4e86\u53cc\u7cbe\u5ea6\u64cd\u4f5c\u6570\u7684\u52a0\u53f7\u8fd0\u7b97\u7b26\uff08 + \uff09\u7684\u5185\u7f6e\u7248\u672c\u3002\u8868\u8fbe\u5f0f w + z \u53d8\u4e3a\u51fd\u6570\u8c03\u7528 operator+(w, z) \uff0c\u5e76\u4f7f\u7528\u51fd\u6570\u91cd\u8f7d\u6765\u786e\u5b9a\u7f16\u8bd1\u5668\u5e94\u8be5\u8c03\u7528\u6b64\u51fd\u6570\u7684\u53cc\u7cbe\u5ea6\u7248\u672c\u800c\u4e0d\u662f\u6574\u6570\u7248\u672c\u3002 \u73b0\u5728\u8003\u8651\u5982\u679c\u6211\u4eec\u5c1d\u8bd5\u5c06\u4e24\u4e2a\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u7684\u5bf9\u8c61\u76f8\u52a0\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1a 1 2 3 Mystring string1 { \"Hello, \" }; Mystring string2 { \"World!\" }; std :: cout << string1 + string2 << '\\\\\\\\\\\\\\\\ n ' ; \u8fd0\u884c\u7ed3\u679c\u5982\u4f55\uff1f\u5b57\u7b26\u4e32\u201cHello\uff0cWorld\uff01\u201d\u672c\u5e94\u8be5\u5c06\u4f1a\u88ab\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\u3002\u4f46\u662f\uff0c\u7531\u4e8eMystring\u662f\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\uff0c\u7f16\u8bd1\u5668\u6ca1\u6709\u53ef\u7528\u4e8e Mystring \u7c7b\u578b\u64cd\u4f5c\u6570\u7684\u52a0\u53f7\u8fd0\u7b97\u7b26\u3002\u56e0\u6b64\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u544a\u9519\u8bef\u3002\u4e3a\u4e86\u4f7f\u5176\u6309\u7167\u6211\u4eec\u60f3\u8981\u7684\u65b9\u5f0f\u5de5\u4f5c\uff0c\u5219\u9700\u8981\u91cd\u8f7d\u51fd\u6570\u2014\u2014\u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u4f55\u5c06 + \u64cd\u4f5c\u7b26\u4e0e\u4e24\u4e2a Mystring \u7c7b\u578b\u7684\u64cd\u4f5c\u6570\u4e00\u8d77\u4f7f\u7528\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u770b\u5230\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9\u3002","title":"\u64cd\u4f5c\u7b26\u4f5c\u4e3a\u51fd\u6570"},{"location":"14-1-introduction-to-operator-overloading/#_2","text":"\u5728\u8ba1\u7b97\u5305\u542b\u64cd\u4f5c\u7b26\u7684\u8868\u8fbe\u5f0f\u65f6\uff0c\u7f16\u8bd1\u5668\u4f7f\u7528\u4ee5\u4e0b\u89c4\u5219\uff1a \u5982\u679c \u6240\u6709 \u64cd\u4f5c\u6570\u90fd\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u5219\u7f16\u8bd1\u5668\u5c06\u8c03\u7528\u5185\u7f6e\u4f8b\u7a0b\uff08\u5982\u679c\u5b58\u5728\uff09\u3002\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u7f16\u8bd1\u5668\u5c06\u751f\u6210\u7f16\u8bd1\u5668\u9519\u8bef\u3002 \u5982\u679c \u4efb\u4f55 \u64cd\u4f5c\u6570\u662f\u7528\u6237\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982\u4f60\u7684\u7c7b\u4e4b\u4e00\u6216\u679a\u4e3e\u7c7b\u578b\uff09\uff0c\u5219\u7f16\u8bd1\u5668\u5c06\u67e5\u770b\u8be5\u7c7b\u578b\u662f\u5426\u5177\u6709\u5339\u914d\u7684\u91cd\u8f7d\u64cd\u4f5c\u7b26\u51fd\u6570\u53ef\u4ee5\u8c03\u7528\u3002\u5982\u679c\u627e\u4e0d\u5230\uff0c\u5b83\u5c06\u5c1d\u8bd5\u5c06\u4e00\u4e2a\u6216\u591a\u4e2a\u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u64cd\u4f5c\u6570\u8f6c\u6362\u4e3a\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u4ee5\u4fbf\u5b83\u53ef\u4ee5\u4f7f\u7528\u5339\u914d\u7684\u5185\u7f6e\u64cd\u4f5c\u7b26\uff08\u901a\u8fc7\u91cd\u8f7d\u578b\u8f6c\u6362\uff0c\u6211\u4eec\u5c06\u5728\u672c\u7ae0\u7684\u540e\u9762\u4ecb\u7ecd\uff09\u3002\u5982\u679c\u5931\u8d25\uff0c\u5219\u5b83\u5c06\u751f\u6210\u7f16\u8bd1\u9519\u8bef\u3002","title":"\u89e3\u6790\u91cd\u8f7d\u64cd\u4f5c\u7b26"},{"location":"14-1-introduction-to-operator-overloading/#_3","text":"\u9996\u5148\uff0cC++\u4e2d\u51e0\u4e4e\u53ef\u4ee5\u91cd\u8f7d\u6240\u6709\u73b0\u6709\u64cd\u4f5c\u7b26\u3002\u4f8b\u5916\u60c5\u51b5\u662f\uff1a\u6761\u4ef6\uff08 ?: \uff09\uff0csizeof\uff0c\u8303\u56f4\uff08 :: \uff09\uff0c\u6210\u5458\u9009\u62e9\u5668\uff08 . \uff09\uff0c\u6307\u9488\u6210\u5458\u9009\u62e9\u5668\uff08 -> \uff09\uff0ctypeid\u4ee5\u53ca\u8f6c\u6362\u64cd\u4f5c\u7b26\u3002 \u5176\u6b21\uff0c\u4f60\u53ea\u80fd\u91cd\u8f7d\u73b0\u6709\u7684\u64cd\u4f5c\u7b26\u3002\u4f60\u4e0d\u80fd\u521b\u5efa\u65b0\u64cd\u4f5c\u7b26\u6216\u91cd\u547d\u540d\u73b0\u6709\u64cd\u4f5c\u7b26\u3002\u4f8b\u5982\uff0c\u4f60\u4e0d\u80fd\u521b\u5efa\u4e00\u4e2a\u64cd\u4f5c\u7b26 ** \u6765\u6267\u884c\u5e42\u3002 \u7b2c\u4e09\uff0c\u91cd\u8f7d\u64cd\u4f5c\u7b26\u4e2d\u81f3\u5c11\u6709\u4e00\u4e2a\u64cd\u4f5c\u6570\u5fc5\u987b\u662f\u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u3002\u8fd9\u610f\u5473\u7740\u4f60\u4e0d\u80fd\u91cd\u8f7d\u52a0\u53f7\u64cd \u7b2c\u56db\uff0c\u4e0d\u53ef\u80fd\u6539\u53d8\u64cd\u4f5c\u7b26\u652f\u6301\u7684\u64cd\u4f5c\u6570\u7684\u6570\u91cf\u3002 \u6700\u540e\uff0c\u6240\u6709\u64cd\u4f5c\u7b26\u90fd\u4fdd\u6301\u5176\u9ed8\u8ba4\u7684\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u6027\uff08\u65e0\u8bba\u5b83\u4eec\u7528\u4e8e\u4ec0\u4e48\uff09\uff0c\u8fd9\u662f\u4e0d\u80fd\u6539\u53d8\u7684\u3002 \u4e00\u4e9b\u65b0\u7684\u7a0b\u5e8f\u5458\u8bd5\u56fe\u91cd\u8f7d\u4f4d\u8fd0\u7b97XOR\u64cd\u4f5c\u7b26\uff08 ^ \uff09\u6765\u5b9e\u73b0\u6307\u6570\u8fd0\u7b97\u3002\u4f46\u662f\uff0c\u5728C++\u4e2d\uff0c operator^ \u7684\u4f18\u5148\u7ea7\u4f4e\u4e8e\u57fa\u672c\u7b97\u672f\u8fd0\u7b97\u7b26\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u8868\u8fbe\u5f0f\u8bc4\u4f30\u4e0d\u6b63\u786e\u3002 \u5728\u57fa\u672c\u6570\u5b66\u4e2d\uff0c\u6307\u6570\u8fd0\u7b97\u4f18\u5148\u4e8e\u57fa\u672c\u7b97\u672f\u8fd0\u7b97\uff0c\u56e0\u6b64 4 + 3 ^ 2 \u89e3\u6790\u4e3a 4 + (3 ^ 2) => 4 + 9 => 13 \u3002\u4f46\u662f\uff0c\u5728C++\u4e2d\uff0c\u7b97\u672f\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e operator^ \uff0c\u56e0\u6b64 4 + 3 ^ 2 \u89e3\u6790\u4e3a\uff08 4 + 3\uff09^ 2 => 7 ^ 2 => 49 \u3002 \u8981\u6b63\u786e\u4f7f\u7528\u5b83\uff0c\u6bcf\u6b21\u90fd\u663e\u5f0f\u5730\u4e3a\u6307\u6570\u90e8\u5206\u6dfb\u52a0\u62ec\u53f7\uff08\u4f8b\u5982 4 + (3 ^ 2) \uff09\uff0c\u8fd9\u5e76\u4e0d\u76f4\u89c2\uff0c\u5e76\u4e14\u53ef\u80fd\u5b58\u5728\u9519\u8bef\u3002 \u56e0\u6b64\uff0c\u51fa\u4e8e\u8fd9\u4e2a\u4f18\u5148\u7ea7\u95ee\u9898\uff0c\u6700\u597d\u53ea\u4ee5\u7c7b\u4f3c\u4e8e\u5176\u539f\u59cb\u610f\u56fe\u7684\u65b9\u5f0f\u4f7f\u7528\u8fd0\u7b97\u7b26\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u91cd\u8f7d\u64cd\u4f5c\u7b26\u65f6\uff0c\u6700\u597d\u4f7f\u64cd\u4f5c\u7b26\u7684\u529f\u80fd\u5c3d\u53ef\u80fd\u63a5\u8fd1\u64cd\u4f5c\u7b26\u7684\u539f\u59cb\u610f\u56fe\u3002 \u6b64\u5916\uff0c\u7531\u4e8e\u64cd\u4f5c\u7b26\u6ca1\u6709\u63cf\u8ff0\u6027\u7684\u540d\u79f0\uff0c\u5b83\u4eec\u7684\u7528\u9014\u5e76\u4e0d\u603b\u662f\u5f88\u6e05\u695a\u3002\u4f8b\u5982\uff0c operator+ \u53ef\u80fd\u662f\u5b57\u7b26\u4e32\u7c7b\u8fde\u63a5\u5b57\u7b26\u4e32\u7684\u5408\u7406\u9009\u62e9\u3002\u4f46\u662f operator- \u5462\uff1f\u4f60\u671f\u671b\u5b83\u505a\u4ec0\u4e48\uff1f\u8fd9\u4e0d\u6e05\u695a\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u91cd\u8f7d\u64cd\u4f5c\u7b26\u7684\u542b\u4e49\u4e0d\u6e05\u6670\u3001\u4e0d\u76f4\u89c2\uff0c\u8bf7\u4f7f\u7528\u547d\u540d\u51fd\u6570\u4ee3\u66ff\u64cd\u4f5c\u7b26\u91cd\u8f7d\u3002 \u5728\u8fd9\u4e9b\u9650\u5236\u5185\uff0c\u4f60\u4ecd\u7136\u53ef\u4ee5\u627e\u5230\u8bb8\u591a\u6709\u7528\u7684\u529f\u80fd\u6765\u91cd\u8f7d\u4f60\u7684\u81ea\u5b9a\u4e49\u7c7b\uff01\u4f60\u53ef\u4ee5\u91cd\u8f7d + \u8fd0\u7b97\u7b26\u6765\u8fde\u63a5\u4f60\u7684\u7528\u6237\u5b9a\u4e49\u7684\u5b57\u7b26\u4e32\u7c7b\uff0c\u6216\u5c06\u4e24\u4e2a Fraction \u7c7b\u5bf9\u8c61\u76f8\u52a0\u3002\u4f60\u53ef\u4ee5\u91cd\u8f7d << \u8fd0\u7b97\u7b26\uff0c\u4ee5\u4fbf\u8f7b\u677e\u5c06\u4f60\u7684\u7c7b\u6253\u5370\u5230\u5c4f\u5e55\uff08\u6216\u6587\u4ef6\uff09\u3002\u4f60\u53ef\u4ee5\u91cd\u8f7d\u7b49\u5f0f\u8fd0\u7b97\u7b26\uff08 == \uff09\u6765\u6bd4\u8f83\u4e24\u4e2a\u7c7b\u5bf9\u8c61\u3002\u8fd9\u4f7f\u5f97\u8fd0\u7b97\u7b26\u91cd\u8f7d\u6210\u4e3aC ++\u4e2d\u6700\u6709\u7528\u7684\u529f\u80fd\u4e4b\u4e00 \u2014\u2014 \u56e0\u4e3a\u5b83\u5141\u8bb8\u4f60\u4ee5\u66f4\u76f4\u89c2\u7684\u65b9\u5f0f\u4f7f\u7528\u4f60\u7684\u7c7b\u3002 \u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u66f4\u6df1\u5165\u5730\u7814\u7a76\u91cd\u8f7d\u4e0d\u540c\u7c7b\u578b\u7684\u8fd0\u7b97\u7b26\u3002","title":"\u64cd\u4f5c\u7b26\u91cd\u8f7d\u7684\u9650\u5236\u662f\u4ec0\u4e48\uff1f"},{"location":"14-10-overloading-the-parenthesis-operator/","tags":["overload","operator","parenthesis"],"text":"Key Takeaway All of the overloaded operators you have seen so far let you define the type of the operator\u2019s parameters, but not the number of parameters (which is fixed based on the type of the operator). For example, operator== always takes two parameters, whereas operator! always takes one. The parenthesis operator (operator()) is a particularly interesting operator in that it allows you to vary both the type AND number of parameters it takes. There are two things to keep in mind: first, the parenthesis operator must be implemented as a member function. Second, in non-object-oriented C++, the () operator is used to call functions. In the case of classes, operator() is just a normal operator that calls a function (named operator()) like any other overloaded operator. An example Let\u2019s take a look at an example that lends itself to overloading this operator: 1 2 3 4 5 class Matrix { private : double data [ 4 ][ 4 ]{}; }; COPY Matrices are a key component of linear algebra, and are often used to do geometric modeling and 3D computer graphics work. In this case, all you need to recognize is that the Matrix class is a 4 by 4 two-dimensional array of doubles. In the lesson on overloading the subscript operator , you learned that we could overload operator[] to provide direct access to a private one-dimensional array. However, in this case, we want access to a private two-dimensional array. Because operator[] is limited to a single parameter, it is not sufficient to let us index a two-dimensional array. However, because the () operator can take as many parameters as we want it to have, we can declare a version of operator() that takes two integer index parameters, and use it to access our two-dimensional array. Here is an example of this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include // for assert() class Matrix { private : double m_data [ 4 ][ 4 ]{}; public : double & operator ()( int row , int col ); double operator ()( int row , int col ) const ; // for const objects }; double & Matrix::operator ()( int row , int col ) { assert ( col >= 0 && col < 4 ); assert ( row >= 0 && row < 4 ); return m_data [ row ][ col ]; } double Matrix::operator ()( int row , int col ) const { assert ( col >= 0 && col < 4 ); assert ( row >= 0 && row < 4 ); return m_data [ row ][ col ]; } COPY Now we can declare a Matrix and access its elements like this: 1 2 3 4 5 6 7 8 9 10 #include int main () { Matrix matrix ; matrix ( 1 , 2 ) = 4.5 ; std :: cout << matrix ( 1 , 2 ) << '\\n' ; return 0 ; } COPY which produces the result: 4.5 Now, let\u2019s overload the () operator again, this time in a way that takes no parameters at all: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include // for assert() class Matrix { private : double m_data [ 4 ][ 4 ]{}; public : double & operator ()( int row , int col ); double operator ()( int row , int col ) const ; void operator ()(); }; double & Matrix::operator ()( int row , int col ) { assert ( col >= 0 && col < 4 ); assert ( row >= 0 && row < 4 ); return m_data [ row ][ col ]; } double Matrix::operator ()( int row , int col ) const { assert ( col >= 0 && col < 4 ); assert ( row >= 0 && row < 4 ); return m_data [ row ][ col ]; } void Matrix::operator ()() { // reset all elements of the matrix to 0.0 for ( int row { 0 }; row < 4 ; ++ row ) { for ( int col { 0 }; col < 4 ; ++ col ) { m_data [ row ][ col ] = 0.0 ; } } } COPY And here\u2019s our new example: 1 2 3 4 5 6 7 8 9 10 11 #include int main () { Matrix matrix {}; matrix ( 1 , 2 ) = 4.5 ; matrix (); // erase matrix std :: cout << matrix ( 1 , 2 ) << '\\n' ; return 0 ; } COPY which produces the result: 0 Because the () operator is so flexible, it can be tempting to use it for many different purposes. However, this is strongly discouraged, since the () symbol does not really give any indication of what the operator is doing. In our example above, it would be better to have written the erase functionality as a function called clear() or erase(), as matrix.erase() is easier to understand than matrix() (which could do anything!). Having fun with functors Operator() is also commonly overloaded to implement functors (or function object ), which are classes that operate like functions. The advantage of a functor over a normal function is that functors can store data in member variables (since they are classes). Here\u2019s a simple functor: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include class Accumulator { private : int m_counter { 0 }; public : int operator () ( int i ) { return ( m_counter += i ); } }; int main () { Accumulator acc {}; std :: cout << acc ( 10 ) << '\\n' ; // prints 10 std :: cout << acc ( 20 ) << '\\n' ; // prints 30 return 0 ; } COPY Note that using our Accumulator looks just like making a normal function call, but our Accumulator object is storing an accumulated value. You may wonder why we couldn\u2019t do the same thing with a normal function and a static local variable to preserve data between function calls. We could, but because functions only have one global instance, we\u2019d be limited to using it for one thing at a time. With functors, we can instantiate as many separate functor objects as we need and use them all simultaneously. Conclusion Operator() is sometimes overloaded with two parameters to index multidimensional arrays, or to retrieve a subset of a one dimensional array (with the two parameters defining the subset to return). Anything else is probably better written as a member function with a more descriptive name. Operator() is also often overloaded to create functors. Although simple functors (such as the example above) are fairly easily understood, functors are typically used in more advanced programming topics, and deserve their own lesson.","title":"14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26"},{"location":"14-10-overloading-the-parenthesis-operator/#conclusion","text":"Operator() is sometimes overloaded with two parameters to index multidimensional arrays, or to retrieve a subset of a one dimensional array (with the two parameters defining the subset to return). Anything else is probably better written as a member function with a more descriptive name. Operator() is also often overloaded to create functors. Although simple functors (such as the example above) are fairly easily understood, functors are typically used in more advanced programming topics, and deserve their own lesson.","title":"Conclusion"},{"location":"14-11-Overloading-typecasts/","tags":["overload","typecast"],"text":"Key Takeaway In lesson 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast , you learned that C++ allows you to convert one data type to another. The following example shows an int being converted into a double: 1 2 int n { 5 }; auto d { static_cast < double > ( n ) }; // int cast to a double COPY C++ already knows how to convert between the built-in data types. However, it does not know how to convert any of our user-defined classes. That\u2019s where overloading the typecast operators comes into play. User-defined conversions allow us to convert our class into another data type. Take a look at the following class: 1 2 3 4 5 6 7 8 9 10 11 12 13 class Cents { private : int m_cents ; public : Cents ( int cents = 0 ) : m_cents { cents } { } int getCents () const { return m_cents ; } void setCents ( int cents ) { m_cents = cents ; } }; COPY This class is pretty simple: it holds some number of cents as an integer, and provides access functions to get and set the number of cents. It also provides a constructor for converting an int into a Cents. If we can convert an int into a Cents, then doesn\u2019t it also make sense for us to be able to convert a Cents back into an int? In some cases, this might not be true, but in this case, it does make sense. In the following example, we have to use getCents() to convert our Cents variable back into an integer so we can print it using printInt(): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include void printInt ( int value ) { std :: cout << value ; } int main () { Cents cents { 7 }; printInt ( cents . getCents ()); // print 7 std :: cout << '\\n' ; return 0 ; } COPY If we have already written a lot of functions that take integers as parameters, our code will be littered with calls to getCents(), which makes it more messy than it needs to be. To make things easier, we can provide a user-defined conversion by overloading the int typecast. This will allow us to cast our Cents class directly into an int. The following example shows how this is done: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Cents { private : int m_cents ; public : Cents ( int cents = 0 ) : m_cents { cents } { } // Overloaded int cast operator int () const { return m_cents ; } int getCents () const { return m_cents ; } void setCents ( int cents ) { m_cents = cents ; } }; COPY There are three things to note: To overload the function that casts our class to an int, we write a new function in our class called operator int(). Note that there is a space between the word operator and the type we are casting to. User-defined conversions do not take parameters, as there is no way to pass arguments to them. User-defined conversions do not have a return type. C++ assumes you will be returning the correct type. Now in our example, we can call printInt() like this: 1 2 3 4 5 6 7 8 9 10 11 #include int main () { Cents cents { 7 }; printInt ( cents ); // print 7 std :: cout << '\\n' ; return 0 ; } COPY The compiler will first note that function printInt takes an integer parameter. Then it will note that variable cents is not an int. Finally, it will look to see if we\u2019ve provided a way to convert a Cents into an int. Since we have, it will call our operator int() function, which returns an int, and the returned int will be passed to printInt(). We can now also explicitly cast our Cents variable to an int: 1 2 Cents cents { 7 }; int c { static_cast < int > ( cents ) }; COPY You can provide user-defined conversions for any data type you wish, including your own user-defined data types! Here\u2019s a new class called Dollars that provides an overloaded Cents conversion: 1 2 3 4 5 6 7 8 9 10 11 12 13 class Dollars { private : int m_dollars ; public : Dollars ( int dollars = 0 ) : m_dollars { dollars } { } // Allow us to convert Dollars into Cents operator Cents () const { return Cents { m_dollars * 100 }; } }; COPY This allows us to convert a Dollars object directly into a Cents object! This allows you to do something like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include class Cents { private : int m_cents ; public : Cents ( int cents = 0 ) : m_cents { cents } { } // Overloaded int cast operator int () const { return m_cents ; } int getCents () const { return m_cents ; } void setCents ( int cents ) { m_cents = cents ; } }; class Dollars { private : int m_dollars ; public : Dollars ( int dollars = 0 ) : m_dollars { dollars } { } // Allow us to convert Dollars into Cents operator Cents () const { return Cents { m_dollars * 100 }; } }; void printCents ( Cents cents ) { std :: cout << cents ; // cents will be implicitly cast to an int here } int main () { Dollars dollars { 9 }; printCents ( dollars ); // dollars will be implicitly cast to a Cents here std :: cout << '\\n' ; return 0 ; } COPY Consequently, this program will print the value: 1 900 which makes sense, since 9 dollars is 900 cents!","title":"14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26"},{"location":"14-12-the-copy-constructor/","tags":["copy constructor","constructor"],"text":"Key Takeaway - \u521d\u59cb\u5316\u56de\u987e \u7531\u4e8e\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u51e0\u8282\u8bfe\u4e2d\u8ba8\u8bba\u521d\u59cb\u5316\uff0c\u6240\u4ee5\u6709\u5fc5\u8981\u5148\u56de\u987e\u4e00\u4e0b C++ \u652f\u6301\u7684\u51e0\u79cd\u521d\u59cb\u5316\u65b9\u5f0f\uff1a \u76f4\u63a5\u521d\u59cb\u5316 \uff08\u4f7f\u7528\u62ec\u53f7\uff09\u3001 \u7edf\u4e00\u521d\u59cb\u5316 \uff08\u5927\u62ec\u53f7\uff09\u548c \u62f7\u8d1d\u521d\u59cb\u5316 \uff08\u4f7f\u7528\u7b49\u4e8e\u53f7\uff09\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u540c\u65f6\u4f7f\u7528\u4e86\u4e0a\u8ff0\u4e09\u79cd\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } \u76f4\u63a5\u521d\u59cb\u5316\uff1a 1 2 int x ( 5 ); // \u76f4\u63a5\u521d\u59cb\u5316\u4e00\u4e2a\u6574\u578b Fraction fiveThirds ( 5 , 3 ); // \u76f4\u63a5\u521d\u59cb\u5316 Fraction\uff0c\u5373\u8c03\u7528\u6784\u9020\u51fd\u6570 Fraction(int, int) \u5728 C++11 \u4e2d\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7edf\u4e00\u521d\u59cb\u5316\uff1a 1 2 int x { 5 }; // \u7edf\u4e00\u521d\u59cb\u5316\u4e00\u4e2a\u6574\u578b Fraction fiveThirds { 5 , 3 }; // \u7edf\u4e00\u521d\u59cb\u5316 Fraction \uff0c\u4ecd\u7136\u662f\u8c03\u7528\u6784\u9020\u51fd\u6570 Fraction(int, int) \u6700\u540e\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u62f7\u8d1d\u521d\u59cb\u5316\uff1a 1 2 3 int x = 6 ; // \u62f7\u8d1d\u521d\u59cb\u5316\u4e00\u4e2a\u6574\u578b Fraction six = Fraction ( 6 ); // \u62f7\u8d1d\u521d\u59cb\u5316 Fraction, \u4f1a\u8c03\u7528 Fraction(6, 1) Fraction seven = 7 ; // \u62f7\u8d1d\u521d\u59cb\u5316 Fraction\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u5bfb\u627e\u5c06 7 \u8f6c\u6362\u4e3a Fraction \u7684\u65b9\u6cd5\uff0c\u56e0\u6b64\u4f1a\u8c03\u7528\u6784\u9020\u51fd\u6570 Fraction(7, 1) \u901a\u8fc7\u76f4\u63a5\u521d\u59cb\u5316\u6216\u8005\u7edf\u4e00\u521d\u59cb\u5316\uff0c\u5bf9\u8c61\u4f1a\u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u3002\u800c\u5f53\u4f7f\u7528\u62f7\u8d1d\u521d\u59cb\u5316\u65f6\uff0c\u95ee\u9898\u5c31\u6709\u4e00\u70b9\u590d\u6742\u4e86\u3002\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\u4ed4\u7ec6\u63a2\u8ba8\u62f7\u8d1d\u521d\u59cb\u5316\u3002\u4e3a\u4e86\u66f4\u597d\u7684\u6548\u679c\uff0c\u8ba9\u6211\u4eec\u5148\u770b\u770b\u53e6\u5916\u4e00\u4e2a\u8bdd\u9898\u3002 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } int main () { Fraction fiveThirds { 5 , 3 }; // \u7edf\u4e00\u521d\u59cb\u5316 Fraction, \u8c03\u7528 Fraction(int, int) Fraction fCopy { fiveThirds }; // \u7edf\u4e00\u521d\u59cb\u5316 Fraction -- \u8fd9\u91cc\u8c03\u7528\u7684\u662f\u54ea\u4e2a\u6784\u9020\u51fd\u6570\uff1f std :: cout << fCopy << '\\n' ; } \u5f53\u6211\u4eec\u7f16\u8bd1\u6267\u884c\u4e0a\u9762\u7a0b\u5e8f\u65f6\uff0c\u4e00\u5207\u6b63\u5e38\uff0c\u7a0b\u5e8f\u6253\u5370\uff1a 1 5 / 3 \u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u4e0a\u9762\u4ee3\u7801\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002 \u53d8\u91cf fiveThirds \u7684\u521d\u59cb\u5316\u4f7f\u7528\u4e86\u6807\u51c6\u7684\u7edf\u4e00\u521d\u59cb\u5316\u65b9\u5f0f\uff0c\u56e0\u6b64\u4f1a\u8c03\u7528\u6784\u9020\u51fd\u6570 Fraction(int, int) \uff0c\u6ca1\u4ec0\u4e48\u597d\u8bf4\u7684\u3002\u4f46\u662f\u4e0b\u4e00\u884c\u5462\uff1f fCopy \u663e\u7136\u4e5f\u662f\u5728\u521d\u59cb\u5316\uff0c\u800c\u4e14\u521d\u59cb\u5316\u4f1a\u8c03\u7528\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u90a3\u4e48\u4f60\u77e5\u9053\u5b83\u8c03\u7528\u7684\u662f\u4ec0\u4e48\u6784\u9020\u51fd\u6570\u5417\uff1f \u5b9e\u9645\u4e0a\uff0c\u8fd9\u884c\u4ee3\u7801\u4f1a\u8c03\u7528 Fraction \u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u662f\u4e00\u7c7b\u7279\u6b8a\u7684\u6784\u9020\u51fd\u6570\uff0c\u5b83\u4f1a\u901a\u8fc7\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u5bf9\u8c61\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\uff08\u76f8\u540c\u7c7b\u578b\uff09\u3002\u7c7b\u4f3c\u4e8e\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u5982\u679c\u4f60\u4e0d\u63d0\u4f9b\u4e00\u4e2a\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u7684\u8bdd\uff0cC++ \u4f1a\u4e3a\u4f60\u521b\u5efa\u4e00\u4e2apublic\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002\u7531\u4e8e\u7f16\u8bd1\u5668\u5e76\u4e0d\u5f88\u4e86\u89e3\u4f60\u7684\u7c7b\uff0c\u6240\u4ee5\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4f1a\u8fdb\u884c \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \u3002\u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316\u610f\u601d\u5c31\u662f\u6bcf\u4e2a\u6210\u5458\u90fd\u4f1a\u901a\u8fc7\u88ab\u62f7\u8d1d\u5bf9\u8c61\u4e2d\u7684\u5bf9\u5e94\u6210\u5458\u8fdb\u884c\u62f7\u8d1d\u521d\u59cb\u5316\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c fCopy.m_numerator \u4f1a\u901a\u8fc7 fiveThirds.m_numerator \u6765\u521d\u59cb\u5316\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002 \u548c\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u7c7b\u4f3c\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u663e\u5f0f\u5730\u5b9a\u4e49\u4e00\u4e2a\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u5b83\u7684\u5f62\u5f0f\u60f3\u5fc5\u4f60\u5e94\u8be5\u53ef\u4ee5\u731c\u5230\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : // \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } // \u62f7\u8d1d\u6784\u9020\u51fd\u6570 Fraction ( const Fraction & fraction ) : m_numerator { fraction . m_numerator }, m_denominator { fraction . m_denominator } // \u6ce8\u610f\uff1a\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u53c2\u6570 fraction \u7684\u6210\u5458\u53d8\u91cf\uff0c\u56e0\u4e3a\u5b83\u4eec\u90fd\u662f Fraction \u7c7b\u7684 { // no need to check for a denominator of 0 here since fraction must already be a valid Fraction std :: cout << \"Copy constructor called \\n \" ; // just to prove it works } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } int main () { Fraction fiveThirds { 5 , 3 }; // \u76f4\u63a5\u521d\u59cb\u5316\uff0c\u8c03\u7528 Fraction(int, int) \u6784\u9020\u51fd\u6570 Fraction fCopy { fiveThirds }; // \u76f4\u63a5\u521d\u59cb\u5316\u2014\u2014\u4f7f\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570 std :: cout << fCopy << '\\n' ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Copy constructor called 5/3 \u6211\u4eec\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u5b9a\u4e49\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4f7f\u7528\u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316\u65b9\u5f0f\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u5728\u529f\u80fd\u4e0a\u4e0e\u7f16\u8bd1\u5668\u9ed8\u8ba4\u521b\u5efa\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u76f8\u540c\uff0c\u53ea\u662f\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e2a\u8f93\u51fa\u8bed\u53e5\u6765\u8bc1\u660e\u6b63\u5728\u8c03\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002 \u4e0e\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u4e0d\u540c\u7684\u662f\uff0c\u5982\u679c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u80fd\u591f\u6ee1\u8db3\u4f60\u7684\u9700\u8981\uff0c\u90a3\u4e48\u5c3d\u7ba1\u4f7f\u7528\u5b83\u3002 \u6709\u4e00\u4e2a\u5730\u65b9\u9700\u8981\u6ce8\u610f\uff1a\u5728\u4e4b\u524d\u6211\u4eec\u5df2\u7ecf\u770b\u5230\u4e86\u4e00\u4e9b\u91cd\u8f7d\u64cd\u4f5c\u7b26 << \u7684\u4f8b\u5b50\uff0c\u5728\u8fd9\u4e9b\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u80fd\u591f\u8bbf\u95ee\u5f62\u53c2 f1 \u7684\u79c1\u6709\u6210\u5458\uff0c\u56e0\u4e3a\u8be5\u51fd\u6570\u662f Fraction \u7c7b\u7684\u53cb\u5143\u3002\u7c7b\u4f3c\u5730\uff0c\u7c7b\u7684\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u76f8\u540c\u7c7b\u5f62\u53c2\u7684\u79c1\u6709\u6210\u5458\u3002\u56e0\u4e3a Fraction \u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u63a5\u53d7\u540c\u7c7b\u578b\u7684\u5f62\u53c2(\u7528\u4e8e\u590d\u5236)\uff0c\u6240\u4ee5\u6211\u4eec\u80fd\u591f\u76f4\u63a5\u8bbf\u95ee\u5f62\u53c2 Fraction \u7684\u6210\u5458\u3002 \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u7684\u5f62\u53c2\u5fc5\u987b\u662f\u5f15\u7528\u7c7b\u578b \u590d\u5236\u6784\u9020\u51fd\u6570\u7684\u5f62\u53c2\u5fc5\u987b\u662f(const)\u5f15\u7528\u3002\u8fd9\u662f\u6709\u610f\u4e49\u7684\uff1a\u5982\u679c\u5b9e\u53c2\u662f\u6309\u503c\u4f20\u9012\u7684\uff0c\u90a3\u4e48\u6211\u4eec\u9700\u8981\u590d\u5236\u6784\u9020\u51fd\u6570\u5c06\u5b9e\u62f7\u8d1d\u5230\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u7684\u5f62\u53c2\u4e2d(\u8fd9\u5c06\u5bfc\u81f4\u65e0\u9650\u9012\u5f52)\u3002 \u963b\u6b62\u62f7\u8d1d \u901a\u8fc7\u5c06\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u8bbe\u4e3a \u79c1\u6709\u6210\u5458 \uff0c\u6211\u4eec\u53ef\u4ee5\u963b\u6b62\u7c7b\u7684\u62f7\u8d1d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include class Fraction { private : int m_numerator {}; int m_denominator {}; // \u79c1\u6709\u62f7\u8d1d\u6784\u9020\u51fd\u6570 Fraction ( const Fraction & fraction ) : m_numerator { fraction . m_numerator }, m_denominator { fraction . m_denominator } { // no need to check for a denominator of 0 here since fraction must already be a valid Fraction std :: cout << \"Copy constructor called \\n \" ; // just to prove it works } public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } int main () { Fraction fiveThirds { 5 , 3 }; // \u76f4\u63a5\u521d\u59cb\u5316 Fraction\uff0c\u8c03\u7528 Fraction(int, int) Fraction fCopy { fiveThirds }; // \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u662f\u79c1\u6709\u7684\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002 std :: cout << fCopy << '\\n' ; } \u7f16\u8bd1\u7a0b\u5e8f\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\uff0c\u56e0\u4e3a fCopy \u9700\u8981\u7528\u5230\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u4f46\u662f\u7531\u4e8e\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u662f\u79c1\u6709\u7684\uff0c\u5b83\u65e0\u6743\u8bbf\u95ee\u3002 \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u53ef\u80fd\u4f1a\u88ab\u7701\u7565 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } // Copy constructor Fraction ( const Fraction & fraction ) : m_numerator { fraction . m_numerator }, m_denominator { fraction . m_denominator } { // no need to check for a denominator of 0 here since fraction must already be a valid Fraction std :: cout << \"Copy constructor called \\n \" ; // just to prove it works } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } int main () { Fraction fiveThirds { Fraction { 5 , 3 } }; std :: cout << fiveThirds ; return 0 ; } \u8003\u8651\u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8fd0\u884c\u903b\u8f91\u3002\u9996\u5148\uff0c\u6211\u4eec\u76f4\u63a5\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u533f\u540d\u7684 Fraction \u5bf9\u8c61\uff08\u4f7f\u7528 Fraction(int, int) \u6784\u9020\u51fd\u6570\uff09\u3002\u968f\u540e\uff0c\u6211\u4eec\u5c06\u8be5\u533f\u540d\u5bf9\u8c61\u4f5c\u4e3a\u521d\u59cb\u5316\u503c\uff0c\u7528\u4e8e\u521d\u59cb\u5316 Fraction fiveThirds \u3002\u56e0\u4e3a\u533f\u540d\u5bf9\u8c61\u4e5f\u662f Fraction \u7c7b\u578b\u7684\uff0c\u6240\u4ee5\u4f1a\u8c03\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u8fd9\u4e48\u5206\u6790\u6ca1\u9519\u5427\uff1f \u7f16\u8bd1\u5e76\u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u4f60\u53ef\u4ee5\u4f1a\u8ba4\u4e3a\u5176\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 copy constructor called 5/3 \u800c\u5b9e\u9645\u4e0a\u7ed3\u679c\u66f4\u53ef\u80fd\u662f\uff1a 1 5/3 \u4e3a\u4ec0\u4e48\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u6ca1\u6709\u88ab\u8c03\u7528\uff1f \u6ce8\u610f\uff0c\u521d\u59cb\u5316\u533f\u540d\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528\u8be5\u5bf9\u8c61\u76f4\u63a5\u521d\u59cb\u5316\u5b9a\u4e49\u7684\u5bf9\u8c61\u9700\u8981\u4e24\u4e2a\u6b65\u9aa4(\u9996\u5148\u521b\u5efa\u533f\u540d\u5bf9\u8c61\uff0c\u7136\u8c03\u7528\u590d\u5236\u6784\u9020\u51fd\u6570)\u3002\u7136\u800c\uff0c\u521d\u59cb\u5316\u5df2\u5b9a\u4e49\u5bf9\u8c61\u7684\u6700\u7ec8\u7ed3\u679c\u57fa\u672c\u4e0a\u4e0e\u76f4\u63a5\u521d\u59cb\u5316\u76f8\u540c\uff0c\u540e\u8005\u53ea\u9700\u8981\u4e00\u4e2a\u6b65\u9aa4\u3002 \u56e0\u6b64\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u9009\u62e9\u4e0d\u8c03\u7528\u590d\u5236\u6784\u9020\u51fd\u6570\uff0c\u800c\u76f4\u63a5\u8fdb\u884c\u521d\u59cb\u5316\u3002\u4e3a\u4e86\u6027\u80fd\u76ee\u7684\u800c\u7701\u7565\u67d0\u4e9b\u590d\u5236(\u6216\u79fb\u52a8)\u6b65\u9aa4\u7684\u8fc7\u7a0b\u79f0\u4e3a \u7701\u7565 \u6240\u4ee5\u5c3d\u7ba1\u4ee3\u7801\u662f\u8fd9\u6837\u5199\u7684\uff1a 1 Fraction fiveThirds { Fraction { 5 , 3 } }; \u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5c06\u5176\u4f18\u5316\u4e3a\uff1a 1 Fraction fiveThirds { 5 , 3 }; \u4e0a\u9762\u7684\u5f62\u5f0f\u53ea\u9700\u8981\u4e00\u6b21\u6784\u9020\u51fd\u6570\u8c03\u7528( Fraction(int, int) )\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u7701\u7565\u53d1\u751f\u65f6\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u5176\u51fd\u6570\u4f53\u5185\u7684\u4efb\u4f55\u8bed\u53e5\u90fd\u4e0d\u4f1a\u88ab\u6267\u884c\uff0c\u5373\u4f7f\u5b83\u4eec\u4f1a\u4ea7\u751f \u526f\u4f5c\u7528 \uff08\u4f8b\u5982\u8f93\u51fa\u4fe1\u606f\u5230\u5c4f\u5e55\uff09\uff01 \u5728 C++17 \u4e4b\u524d\uff0c\u7f16\u8bd1\u5668\u88ab\u5141\u8bb8\uff08\u4f46\u4e0d\u662f\u5fc5\u987b\uff09\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6267\u884c\u62f7\u8d1d\u7701\u7565\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4ecd\u7136\u662f\u5fc5\u987b\u53ef\u8bbf\u95ee\u7684\uff08\u975e\u79c1\u6709\uff09\uff0c\u5373\u4f7f\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\u88ab\u8c03\u7528\u3002 \u5230\u4e86 C++17\uff0c\u6709\u4e9b\u62f7\u8d1d\u7701\u7565\u5df2\u7ecf\u662f\u5f3a\u5236\u7684\u4e86\uff08\u5305\u62ec\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u8fd9\u79cd\u60c5\u51b5\uff09\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u5f3a\u5236\u7701\u7565\u7684\u60c5\u51b5\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4e0d\u9700\u8981\u662f\u53ef\u8bbf\u95ee\u7684\uff0c\u751a\u81f3\u4e0d\u9700\u8981\u88ab\u5b9a\u4e49\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u4fdd\u8bc1\u4e0d\u8c03\u7528\u5b83\uff01","title":"14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570"},{"location":"14-12-the-copy-constructor/#_1","text":"\u7531\u4e8e\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u51e0\u8282\u8bfe\u4e2d\u8ba8\u8bba\u521d\u59cb\u5316\uff0c\u6240\u4ee5\u6709\u5fc5\u8981\u5148\u56de\u987e\u4e00\u4e0b C++ \u652f\u6301\u7684\u51e0\u79cd\u521d\u59cb\u5316\u65b9\u5f0f\uff1a \u76f4\u63a5\u521d\u59cb\u5316 \uff08\u4f7f\u7528\u62ec\u53f7\uff09\u3001 \u7edf\u4e00\u521d\u59cb\u5316 \uff08\u5927\u62ec\u53f7\uff09\u548c \u62f7\u8d1d\u521d\u59cb\u5316 \uff08\u4f7f\u7528\u7b49\u4e8e\u53f7\uff09\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u540c\u65f6\u4f7f\u7528\u4e86\u4e0a\u8ff0\u4e09\u79cd\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } \u76f4\u63a5\u521d\u59cb\u5316\uff1a 1 2 int x ( 5 ); // \u76f4\u63a5\u521d\u59cb\u5316\u4e00\u4e2a\u6574\u578b Fraction fiveThirds ( 5 , 3 ); // \u76f4\u63a5\u521d\u59cb\u5316 Fraction\uff0c\u5373\u8c03\u7528\u6784\u9020\u51fd\u6570 Fraction(int, int) \u5728 C++11 \u4e2d\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7edf\u4e00\u521d\u59cb\u5316\uff1a 1 2 int x { 5 }; // \u7edf\u4e00\u521d\u59cb\u5316\u4e00\u4e2a\u6574\u578b Fraction fiveThirds { 5 , 3 }; // \u7edf\u4e00\u521d\u59cb\u5316 Fraction \uff0c\u4ecd\u7136\u662f\u8c03\u7528\u6784\u9020\u51fd\u6570 Fraction(int, int) \u6700\u540e\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u62f7\u8d1d\u521d\u59cb\u5316\uff1a 1 2 3 int x = 6 ; // \u62f7\u8d1d\u521d\u59cb\u5316\u4e00\u4e2a\u6574\u578b Fraction six = Fraction ( 6 ); // \u62f7\u8d1d\u521d\u59cb\u5316 Fraction, \u4f1a\u8c03\u7528 Fraction(6, 1) Fraction seven = 7 ; // \u62f7\u8d1d\u521d\u59cb\u5316 Fraction\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u5bfb\u627e\u5c06 7 \u8f6c\u6362\u4e3a Fraction \u7684\u65b9\u6cd5\uff0c\u56e0\u6b64\u4f1a\u8c03\u7528\u6784\u9020\u51fd\u6570 Fraction(7, 1) \u901a\u8fc7\u76f4\u63a5\u521d\u59cb\u5316\u6216\u8005\u7edf\u4e00\u521d\u59cb\u5316\uff0c\u5bf9\u8c61\u4f1a\u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u3002\u800c\u5f53\u4f7f\u7528\u62f7\u8d1d\u521d\u59cb\u5316\u65f6\uff0c\u95ee\u9898\u5c31\u6709\u4e00\u70b9\u590d\u6742\u4e86\u3002\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\u4ed4\u7ec6\u63a2\u8ba8\u62f7\u8d1d\u521d\u59cb\u5316\u3002\u4e3a\u4e86\u66f4\u597d\u7684\u6548\u679c\uff0c\u8ba9\u6211\u4eec\u5148\u770b\u770b\u53e6\u5916\u4e00\u4e2a\u8bdd\u9898\u3002","title":"\u521d\u59cb\u5316\u56de\u987e"},{"location":"14-12-the-copy-constructor/#_2","text":"\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } int main () { Fraction fiveThirds { 5 , 3 }; // \u7edf\u4e00\u521d\u59cb\u5316 Fraction, \u8c03\u7528 Fraction(int, int) Fraction fCopy { fiveThirds }; // \u7edf\u4e00\u521d\u59cb\u5316 Fraction -- \u8fd9\u91cc\u8c03\u7528\u7684\u662f\u54ea\u4e2a\u6784\u9020\u51fd\u6570\uff1f std :: cout << fCopy << '\\n' ; } \u5f53\u6211\u4eec\u7f16\u8bd1\u6267\u884c\u4e0a\u9762\u7a0b\u5e8f\u65f6\uff0c\u4e00\u5207\u6b63\u5e38\uff0c\u7a0b\u5e8f\u6253\u5370\uff1a 1 5 / 3 \u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u4e0a\u9762\u4ee3\u7801\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002 \u53d8\u91cf fiveThirds \u7684\u521d\u59cb\u5316\u4f7f\u7528\u4e86\u6807\u51c6\u7684\u7edf\u4e00\u521d\u59cb\u5316\u65b9\u5f0f\uff0c\u56e0\u6b64\u4f1a\u8c03\u7528\u6784\u9020\u51fd\u6570 Fraction(int, int) \uff0c\u6ca1\u4ec0\u4e48\u597d\u8bf4\u7684\u3002\u4f46\u662f\u4e0b\u4e00\u884c\u5462\uff1f fCopy \u663e\u7136\u4e5f\u662f\u5728\u521d\u59cb\u5316\uff0c\u800c\u4e14\u521d\u59cb\u5316\u4f1a\u8c03\u7528\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u90a3\u4e48\u4f60\u77e5\u9053\u5b83\u8c03\u7528\u7684\u662f\u4ec0\u4e48\u6784\u9020\u51fd\u6570\u5417\uff1f \u5b9e\u9645\u4e0a\uff0c\u8fd9\u884c\u4ee3\u7801\u4f1a\u8c03\u7528 Fraction \u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u662f\u4e00\u7c7b\u7279\u6b8a\u7684\u6784\u9020\u51fd\u6570\uff0c\u5b83\u4f1a\u901a\u8fc7\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u5bf9\u8c61\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\uff08\u76f8\u540c\u7c7b\u578b\uff09\u3002\u7c7b\u4f3c\u4e8e\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u5982\u679c\u4f60\u4e0d\u63d0\u4f9b\u4e00\u4e2a\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u7684\u8bdd\uff0cC++ \u4f1a\u4e3a\u4f60\u521b\u5efa\u4e00\u4e2apublic\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002\u7531\u4e8e\u7f16\u8bd1\u5668\u5e76\u4e0d\u5f88\u4e86\u89e3\u4f60\u7684\u7c7b\uff0c\u6240\u4ee5\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4f1a\u8fdb\u884c \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \u3002\u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316\u610f\u601d\u5c31\u662f\u6bcf\u4e2a\u6210\u5458\u90fd\u4f1a\u901a\u8fc7\u88ab\u62f7\u8d1d\u5bf9\u8c61\u4e2d\u7684\u5bf9\u5e94\u6210\u5458\u8fdb\u884c\u62f7\u8d1d\u521d\u59cb\u5316\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c fCopy.m_numerator \u4f1a\u901a\u8fc7 fiveThirds.m_numerator \u6765\u521d\u59cb\u5316\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002 \u548c\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u7c7b\u4f3c\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u663e\u5f0f\u5730\u5b9a\u4e49\u4e00\u4e2a\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u5b83\u7684\u5f62\u5f0f\u60f3\u5fc5\u4f60\u5e94\u8be5\u53ef\u4ee5\u731c\u5230\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : // \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } // \u62f7\u8d1d\u6784\u9020\u51fd\u6570 Fraction ( const Fraction & fraction ) : m_numerator { fraction . m_numerator }, m_denominator { fraction . m_denominator } // \u6ce8\u610f\uff1a\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u53c2\u6570 fraction \u7684\u6210\u5458\u53d8\u91cf\uff0c\u56e0\u4e3a\u5b83\u4eec\u90fd\u662f Fraction \u7c7b\u7684 { // no need to check for a denominator of 0 here since fraction must already be a valid Fraction std :: cout << \"Copy constructor called \\n \" ; // just to prove it works } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } int main () { Fraction fiveThirds { 5 , 3 }; // \u76f4\u63a5\u521d\u59cb\u5316\uff0c\u8c03\u7528 Fraction(int, int) \u6784\u9020\u51fd\u6570 Fraction fCopy { fiveThirds }; // \u76f4\u63a5\u521d\u59cb\u5316\u2014\u2014\u4f7f\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570 std :: cout << fCopy << '\\n' ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Copy constructor called 5/3 \u6211\u4eec\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u5b9a\u4e49\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4f7f\u7528\u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316\u65b9\u5f0f\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u5728\u529f\u80fd\u4e0a\u4e0e\u7f16\u8bd1\u5668\u9ed8\u8ba4\u521b\u5efa\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u76f8\u540c\uff0c\u53ea\u662f\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e2a\u8f93\u51fa\u8bed\u53e5\u6765\u8bc1\u660e\u6b63\u5728\u8c03\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002 \u4e0e\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u4e0d\u540c\u7684\u662f\uff0c\u5982\u679c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u80fd\u591f\u6ee1\u8db3\u4f60\u7684\u9700\u8981\uff0c\u90a3\u4e48\u5c3d\u7ba1\u4f7f\u7528\u5b83\u3002 \u6709\u4e00\u4e2a\u5730\u65b9\u9700\u8981\u6ce8\u610f\uff1a\u5728\u4e4b\u524d\u6211\u4eec\u5df2\u7ecf\u770b\u5230\u4e86\u4e00\u4e9b\u91cd\u8f7d\u64cd\u4f5c\u7b26 << \u7684\u4f8b\u5b50\uff0c\u5728\u8fd9\u4e9b\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u80fd\u591f\u8bbf\u95ee\u5f62\u53c2 f1 \u7684\u79c1\u6709\u6210\u5458\uff0c\u56e0\u4e3a\u8be5\u51fd\u6570\u662f Fraction \u7c7b\u7684\u53cb\u5143\u3002\u7c7b\u4f3c\u5730\uff0c\u7c7b\u7684\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u76f8\u540c\u7c7b\u5f62\u53c2\u7684\u79c1\u6709\u6210\u5458\u3002\u56e0\u4e3a Fraction \u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u63a5\u53d7\u540c\u7c7b\u578b\u7684\u5f62\u53c2(\u7528\u4e8e\u590d\u5236)\uff0c\u6240\u4ee5\u6211\u4eec\u80fd\u591f\u76f4\u63a5\u8bbf\u95ee\u5f62\u53c2 Fraction \u7684\u6210\u5458\u3002 \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u7684\u5f62\u53c2\u5fc5\u987b\u662f\u5f15\u7528\u7c7b\u578b \u590d\u5236\u6784\u9020\u51fd\u6570\u7684\u5f62\u53c2\u5fc5\u987b\u662f(const)\u5f15\u7528\u3002\u8fd9\u662f\u6709\u610f\u4e49\u7684\uff1a\u5982\u679c\u5b9e\u53c2\u662f\u6309\u503c\u4f20\u9012\u7684\uff0c\u90a3\u4e48\u6211\u4eec\u9700\u8981\u590d\u5236\u6784\u9020\u51fd\u6570\u5c06\u5b9e\u62f7\u8d1d\u5230\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u7684\u5f62\u53c2\u4e2d(\u8fd9\u5c06\u5bfc\u81f4\u65e0\u9650\u9012\u5f52)\u3002","title":"\u62f7\u8d1d\u6784\u9020\u51fd\u6570"},{"location":"14-12-the-copy-constructor/#_3","text":"\u901a\u8fc7\u5c06\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u8bbe\u4e3a \u79c1\u6709\u6210\u5458 \uff0c\u6211\u4eec\u53ef\u4ee5\u963b\u6b62\u7c7b\u7684\u62f7\u8d1d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include class Fraction { private : int m_numerator {}; int m_denominator {}; // \u79c1\u6709\u62f7\u8d1d\u6784\u9020\u51fd\u6570 Fraction ( const Fraction & fraction ) : m_numerator { fraction . m_numerator }, m_denominator { fraction . m_denominator } { // no need to check for a denominator of 0 here since fraction must already be a valid Fraction std :: cout << \"Copy constructor called \\n \" ; // just to prove it works } public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } int main () { Fraction fiveThirds { 5 , 3 }; // \u76f4\u63a5\u521d\u59cb\u5316 Fraction\uff0c\u8c03\u7528 Fraction(int, int) Fraction fCopy { fiveThirds }; // \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u662f\u79c1\u6709\u7684\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002 std :: cout << fCopy << '\\n' ; } \u7f16\u8bd1\u7a0b\u5e8f\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\uff0c\u56e0\u4e3a fCopy \u9700\u8981\u7528\u5230\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u4f46\u662f\u7531\u4e8e\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u662f\u79c1\u6709\u7684\uff0c\u5b83\u65e0\u6743\u8bbf\u95ee\u3002","title":"\u963b\u6b62\u62f7\u8d1d"},{"location":"14-12-the-copy-constructor/#_4","text":"\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } // Copy constructor Fraction ( const Fraction & fraction ) : m_numerator { fraction . m_numerator }, m_denominator { fraction . m_denominator } { // no need to check for a denominator of 0 here since fraction must already be a valid Fraction std :: cout << \"Copy constructor called \\n \" ; // just to prove it works } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } int main () { Fraction fiveThirds { Fraction { 5 , 3 } }; std :: cout << fiveThirds ; return 0 ; } \u8003\u8651\u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8fd0\u884c\u903b\u8f91\u3002\u9996\u5148\uff0c\u6211\u4eec\u76f4\u63a5\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u533f\u540d\u7684 Fraction \u5bf9\u8c61\uff08\u4f7f\u7528 Fraction(int, int) \u6784\u9020\u51fd\u6570\uff09\u3002\u968f\u540e\uff0c\u6211\u4eec\u5c06\u8be5\u533f\u540d\u5bf9\u8c61\u4f5c\u4e3a\u521d\u59cb\u5316\u503c\uff0c\u7528\u4e8e\u521d\u59cb\u5316 Fraction fiveThirds \u3002\u56e0\u4e3a\u533f\u540d\u5bf9\u8c61\u4e5f\u662f Fraction \u7c7b\u578b\u7684\uff0c\u6240\u4ee5\u4f1a\u8c03\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u8fd9\u4e48\u5206\u6790\u6ca1\u9519\u5427\uff1f \u7f16\u8bd1\u5e76\u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u4f60\u53ef\u4ee5\u4f1a\u8ba4\u4e3a\u5176\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 copy constructor called 5/3 \u800c\u5b9e\u9645\u4e0a\u7ed3\u679c\u66f4\u53ef\u80fd\u662f\uff1a 1 5/3 \u4e3a\u4ec0\u4e48\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u6ca1\u6709\u88ab\u8c03\u7528\uff1f \u6ce8\u610f\uff0c\u521d\u59cb\u5316\u533f\u540d\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528\u8be5\u5bf9\u8c61\u76f4\u63a5\u521d\u59cb\u5316\u5b9a\u4e49\u7684\u5bf9\u8c61\u9700\u8981\u4e24\u4e2a\u6b65\u9aa4(\u9996\u5148\u521b\u5efa\u533f\u540d\u5bf9\u8c61\uff0c\u7136\u8c03\u7528\u590d\u5236\u6784\u9020\u51fd\u6570)\u3002\u7136\u800c\uff0c\u521d\u59cb\u5316\u5df2\u5b9a\u4e49\u5bf9\u8c61\u7684\u6700\u7ec8\u7ed3\u679c\u57fa\u672c\u4e0a\u4e0e\u76f4\u63a5\u521d\u59cb\u5316\u76f8\u540c\uff0c\u540e\u8005\u53ea\u9700\u8981\u4e00\u4e2a\u6b65\u9aa4\u3002 \u56e0\u6b64\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u9009\u62e9\u4e0d\u8c03\u7528\u590d\u5236\u6784\u9020\u51fd\u6570\uff0c\u800c\u76f4\u63a5\u8fdb\u884c\u521d\u59cb\u5316\u3002\u4e3a\u4e86\u6027\u80fd\u76ee\u7684\u800c\u7701\u7565\u67d0\u4e9b\u590d\u5236(\u6216\u79fb\u52a8)\u6b65\u9aa4\u7684\u8fc7\u7a0b\u79f0\u4e3a \u7701\u7565 \u6240\u4ee5\u5c3d\u7ba1\u4ee3\u7801\u662f\u8fd9\u6837\u5199\u7684\uff1a 1 Fraction fiveThirds { Fraction { 5 , 3 } }; \u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5c06\u5176\u4f18\u5316\u4e3a\uff1a 1 Fraction fiveThirds { 5 , 3 }; \u4e0a\u9762\u7684\u5f62\u5f0f\u53ea\u9700\u8981\u4e00\u6b21\u6784\u9020\u51fd\u6570\u8c03\u7528( Fraction(int, int) )\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u7701\u7565\u53d1\u751f\u65f6\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u5176\u51fd\u6570\u4f53\u5185\u7684\u4efb\u4f55\u8bed\u53e5\u90fd\u4e0d\u4f1a\u88ab\u6267\u884c\uff0c\u5373\u4f7f\u5b83\u4eec\u4f1a\u4ea7\u751f \u526f\u4f5c\u7528 \uff08\u4f8b\u5982\u8f93\u51fa\u4fe1\u606f\u5230\u5c4f\u5e55\uff09\uff01 \u5728 C++17 \u4e4b\u524d\uff0c\u7f16\u8bd1\u5668\u88ab\u5141\u8bb8\uff08\u4f46\u4e0d\u662f\u5fc5\u987b\uff09\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6267\u884c\u62f7\u8d1d\u7701\u7565\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4ecd\u7136\u662f\u5fc5\u987b\u53ef\u8bbf\u95ee\u7684\uff08\u975e\u79c1\u6709\uff09\uff0c\u5373\u4f7f\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\u88ab\u8c03\u7528\u3002 \u5230\u4e86 C++17\uff0c\u6709\u4e9b\u62f7\u8d1d\u7701\u7565\u5df2\u7ecf\u662f\u5f3a\u5236\u7684\u4e86\uff08\u5305\u62ec\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u8fd9\u79cd\u60c5\u51b5\uff09\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u5f3a\u5236\u7701\u7565\u7684\u60c5\u51b5\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4e0d\u9700\u8981\u662f\u53ef\u8bbf\u95ee\u7684\uff0c\u751a\u81f3\u4e0d\u9700\u8981\u88ab\u5b9a\u4e49\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u4fdd\u8bc1\u4e0d\u8c03\u7528\u5b83\uff01","title":"\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u53ef\u80fd\u4f1a\u88ab\u7701\u7565"},{"location":"14-13-copy-initialization/","tags":["copy initialization"],"text":"Key Takeaway - \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 int x = 5 ; \u8be5\u8bed\u53e5\u4f7f\u7528\u62f7\u8d1d\u521d\u59cb\u5316\u7684\u65b9\u5f0f\u5c06\u4e00\u4e2a\u65b0\u521b\u5efa\u7684\u6574\u578b\u53d8\u91cf x \u7684\u503c\u8bbe\u7f6e\u4e3a 5\u3002 \u800c\u5bf9\u4e8e\u7c7b\u6765\u8bf4\uff0c\u95ee\u9898\u5219\u4f1a\u53d8\u5f97\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\uff0c\u56e0\u4e3a\u7c7b\u5728\u521d\u59cb\u5316\u65f6\u9700\u8981\u4f7f\u7528\u6784\u9020\u51fd\u6570\u3002\u672c\u8282\u8bfe\u6211\u4eec\u4f1a\u4ecb\u7ecd\u4e0e\u7c7b\u7684\u62f7\u8d1d\u521d\u59cb\u5316\u76f8\u5173\u7684\u95ee\u9898\u3002 \u7c7b\u7684\u62f7\u8d1d\u521d\u59cb\u5316 \u8003\u8651\u4e0b\u9762\u7684 Fraction \u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include class Fraction { private : int m_numerator ; int m_denominator ; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator ( numerator ), m_denominator ( denominator ) { assert ( denominator != 0 ); } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } \u8003\u8651\u4e0b\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 int main () { Fraction six = Fraction ( 6 ); std :: cout << six ; return 0 ; } \u7f16\u8bd1\u8fd0\u884c\u4ee3\u7801\uff0c\u7ed3\u679c\u5982\u4f60\u6240\u4ee5\u60f3\u7684\u90a3\u6837\uff1a 1 6/1 \u8fd9\u79cd\u5f62\u5f0f\u7684\u62f7\u8d1d\u6784\u9020\uff0c\u5176\u6c42\u503c\u65b9\u5f0f\u548c\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u4e00\u6837\u7684\uff1a 1 Fraction six ( Fraction ( 6 )); \u5728\u4e0a\u4e00\u8282\u8bfe\u4e2d\uff08 14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \uff09\u6211\u4eec\u5b66\u5230\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u53ef\u80fd\u4f1a\u8c03\u7528 Fraction(int, int) \u4ee5\u53ca Fraction \u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff08\u53ef\u80fd\u4f1a\u88ab\u4f18\u5316\u6389\uff09\u3002\u4e0d\u8fc7\uff0c\u7531\u4e8e\u7701\u7565\u4e0d\u88ab\u4fdd\u8bc1\u4e00\u5b9a\u53d1\u751f\uff0c\u6240\u4ee5\u6700\u597d\u907f\u514d\u4f7f\u7528\u62f7\u8d1d\u521d\u59cb\u5316\u6765\u521d\u59cb\u5316\u7c7b\uff0c\u800c\u6539\u7528 \u7edf\u4e00\u521d\u59cb\u5316 \u3002 \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u4f7f\u7528\u62f7\u8d1d\u521d\u59cb\u5316\uff0c\u4f7f\u7528\u7edf\u4e00\u521d\u59cb\u5316 \u62f7\u8d1d\u521d\u59cb\u5316\u7684\u5176\u4ed6\u4f7f\u7528\u573a\u666f \u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u5730\u65b9\u4f7f\u7528\u4e86\u62f7\u8d1d\u521d\u59cb\u5316\uff0c\u5176\u4e2d\u6709\u4e24\u4e2a\u5730\u65b9\u503c\u5f97\u4e00\u63d0\u3002\u5f53 \u6309\u503c\u4f20\u9012 \u6216 \u6309\u503c\u8fd4\u56de \u7c7b\u65f6\uff0c\u4f1a\u4f7f\u7528\u62f7\u8d1d\u521d\u59cb\u5316\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include #include class Fraction { private : int m_numerator ; int m_denominator ; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator ( numerator ), m_denominator ( denominator ) { assert ( denominator != 0 ); } // Copy constructor Fraction ( const Fraction & copy ) : m_numerator ( copy . m_numerator ), m_denominator ( copy . m_denominator ) { // no need to check for a denominator of 0 here since copy must already be a valid Fraction std :: cout << \"Copy constructor called \\n \" ; // just to prove it works } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); int getNumerator () { return m_numerator ; } void setNumerator ( int numerator ) { m_numerator = numerator ; } }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } Fraction makeNegative ( Fraction f ) // ideally we should do this by const reference { f . setNumerator ( - f . getNumerator ()); return f ; } int main () { Fraction fiveThirds ( 5 , 3 ); std :: cout << makeNegative ( fiveThirds ); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570 makeNegative \u5c06 Fraction \u7c7b\u578b\u7684\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u540c\u65f6\u8fd4\u56de\u6309\u503c\u8fd4\u56de Fraction \u5bf9\u8c61\u3002\u7a0b\u5e8f\u6267\u884c\u540e\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 Copy constructor called Copy constructor called -5/3 \u7b2c\u4e00\u6b21\u8c03\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u662f\u5728 fiveThirds \u88ab\u4f20\u5165 makeNegative() \u4f5c\u4e3a\u5f62\u53c2 f \u7684\u65f6\u5019\u3002\u7b2c\u4e8c\u6b21\u8c03\u7528\u662f\u5728 makeNegative() \u6309\u503c\u8fd4\u56de\u65f6\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5b9e\u53c2\u548c\u8fd4\u56de\u503c\u90fd\u662f \u6309\u503c \u4f20\u9012\u6216\u8fd4\u56de\u7684\uff0c\u6240\u4ee5\u62f7\u8d1d\u6784\u9020\u4e0d\u4f1a\u88ab\u7701\u7565\u3002\u4f46\u662f\uff0c\u5728\u5176\u4ed6\u7684\u4e00\u4e9b\u573a\u5408\uff0c\u5982\u679c\u5b9e\u53c2\u548c\u8fd4\u56de\u503c\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\uff0c\u7f16\u8bd1\u5668\u4ecd\u7136\u53ef\u80fd\u7701\u7565\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include class Something { public : Something () = default ; Something ( const Something & ) { std :: cout << \"Copy constructor called \\n \" ; } }; Something foo () { return Something (); // copy constructor normally called here } Something goo () { Something s ; return s ; // copy constructor normally called here } int main () { std :: cout << \"Initializing s1 \\n \" ; Something s1 = foo (); // copy constructor normally called here std :: cout << \"Initializing s2 \\n \" ; Something s2 = goo (); // copy constructor normally called here } The above program would normally call the copy constructor 4 times -- however, due to copy elision, it\u2019s likely that your compiler will elide most or all of the cases. Visual Studio 2019 elides 3 (it doesn\u2019t elide the case where goo() is returned), and GCC elides all 4 \u4e0a\u9762\u7684\u7a0b\u5e8f\u901a\u5e38\u4f1a\u8c03\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u65704\u6b21\u2014\u2014\u7136\u800c\uff0c\u7531\u4e8e\u7701\u7565\uff0c\u7f16\u8bd1\u5668\u5f88\u53ef\u80fd\u4f1a\u7701\u7565\u5927\u90e8\u5206\u6216\u6240\u6709\u8c03\u7528\u3002Visual Studio 2019\u7701\u7565\u4e863\u4e2a(\u5b83\u6ca1\u6709\u7701\u7565\u8fd4\u56de goo() \u7684\u60c5\u51b5)\uff0c\u800cGCC\u7701\u7565\u4e86\u6240\u67094\u4e2a\u3002","title":"14.13 - \u62f7\u8d1d\u521d\u59cb\u5316"},{"location":"14-13-copy-initialization/#_1","text":"\u8003\u8651\u4e0b\u9762\u7684 Fraction \u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include class Fraction { private : int m_numerator ; int m_denominator ; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator ( numerator ), m_denominator ( denominator ) { assert ( denominator != 0 ); } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } \u8003\u8651\u4e0b\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 int main () { Fraction six = Fraction ( 6 ); std :: cout << six ; return 0 ; } \u7f16\u8bd1\u8fd0\u884c\u4ee3\u7801\uff0c\u7ed3\u679c\u5982\u4f60\u6240\u4ee5\u60f3\u7684\u90a3\u6837\uff1a 1 6/1 \u8fd9\u79cd\u5f62\u5f0f\u7684\u62f7\u8d1d\u6784\u9020\uff0c\u5176\u6c42\u503c\u65b9\u5f0f\u548c\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u4e00\u6837\u7684\uff1a 1 Fraction six ( Fraction ( 6 )); \u5728\u4e0a\u4e00\u8282\u8bfe\u4e2d\uff08 14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \uff09\u6211\u4eec\u5b66\u5230\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u53ef\u80fd\u4f1a\u8c03\u7528 Fraction(int, int) \u4ee5\u53ca Fraction \u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff08\u53ef\u80fd\u4f1a\u88ab\u4f18\u5316\u6389\uff09\u3002\u4e0d\u8fc7\uff0c\u7531\u4e8e\u7701\u7565\u4e0d\u88ab\u4fdd\u8bc1\u4e00\u5b9a\u53d1\u751f\uff0c\u6240\u4ee5\u6700\u597d\u907f\u514d\u4f7f\u7528\u62f7\u8d1d\u521d\u59cb\u5316\u6765\u521d\u59cb\u5316\u7c7b\uff0c\u800c\u6539\u7528 \u7edf\u4e00\u521d\u59cb\u5316 \u3002 \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u4f7f\u7528\u62f7\u8d1d\u521d\u59cb\u5316\uff0c\u4f7f\u7528\u7edf\u4e00\u521d\u59cb\u5316","title":"\u7c7b\u7684\u62f7\u8d1d\u521d\u59cb\u5316"},{"location":"14-13-copy-initialization/#_2","text":"\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u5730\u65b9\u4f7f\u7528\u4e86\u62f7\u8d1d\u521d\u59cb\u5316\uff0c\u5176\u4e2d\u6709\u4e24\u4e2a\u5730\u65b9\u503c\u5f97\u4e00\u63d0\u3002\u5f53 \u6309\u503c\u4f20\u9012 \u6216 \u6309\u503c\u8fd4\u56de \u7c7b\u65f6\uff0c\u4f1a\u4f7f\u7528\u62f7\u8d1d\u521d\u59cb\u5316\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include #include class Fraction { private : int m_numerator ; int m_denominator ; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator ( numerator ), m_denominator ( denominator ) { assert ( denominator != 0 ); } // Copy constructor Fraction ( const Fraction & copy ) : m_numerator ( copy . m_numerator ), m_denominator ( copy . m_denominator ) { // no need to check for a denominator of 0 here since copy must already be a valid Fraction std :: cout << \"Copy constructor called \\n \" ; // just to prove it works } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); int getNumerator () { return m_numerator ; } void setNumerator ( int numerator ) { m_numerator = numerator ; } }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } Fraction makeNegative ( Fraction f ) // ideally we should do this by const reference { f . setNumerator ( - f . getNumerator ()); return f ; } int main () { Fraction fiveThirds ( 5 , 3 ); std :: cout << makeNegative ( fiveThirds ); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570 makeNegative \u5c06 Fraction \u7c7b\u578b\u7684\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u540c\u65f6\u8fd4\u56de\u6309\u503c\u8fd4\u56de Fraction \u5bf9\u8c61\u3002\u7a0b\u5e8f\u6267\u884c\u540e\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 Copy constructor called Copy constructor called -5/3 \u7b2c\u4e00\u6b21\u8c03\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u662f\u5728 fiveThirds \u88ab\u4f20\u5165 makeNegative() \u4f5c\u4e3a\u5f62\u53c2 f \u7684\u65f6\u5019\u3002\u7b2c\u4e8c\u6b21\u8c03\u7528\u662f\u5728 makeNegative() \u6309\u503c\u8fd4\u56de\u65f6\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5b9e\u53c2\u548c\u8fd4\u56de\u503c\u90fd\u662f \u6309\u503c \u4f20\u9012\u6216\u8fd4\u56de\u7684\uff0c\u6240\u4ee5\u62f7\u8d1d\u6784\u9020\u4e0d\u4f1a\u88ab\u7701\u7565\u3002\u4f46\u662f\uff0c\u5728\u5176\u4ed6\u7684\u4e00\u4e9b\u573a\u5408\uff0c\u5982\u679c\u5b9e\u53c2\u548c\u8fd4\u56de\u503c\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\uff0c\u7f16\u8bd1\u5668\u4ecd\u7136\u53ef\u80fd\u7701\u7565\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include class Something { public : Something () = default ; Something ( const Something & ) { std :: cout << \"Copy constructor called \\n \" ; } }; Something foo () { return Something (); // copy constructor normally called here } Something goo () { Something s ; return s ; // copy constructor normally called here } int main () { std :: cout << \"Initializing s1 \\n \" ; Something s1 = foo (); // copy constructor normally called here std :: cout << \"Initializing s2 \\n \" ; Something s2 = goo (); // copy constructor normally called here } The above program would normally call the copy constructor 4 times -- however, due to copy elision, it\u2019s likely that your compiler will elide most or all of the cases. Visual Studio 2019 elides 3 (it doesn\u2019t elide the case where goo() is returned), and GCC elides all 4 \u4e0a\u9762\u7684\u7a0b\u5e8f\u901a\u5e38\u4f1a\u8c03\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u65704\u6b21\u2014\u2014\u7136\u800c\uff0c\u7531\u4e8e\u7701\u7565\uff0c\u7f16\u8bd1\u5668\u5f88\u53ef\u80fd\u4f1a\u7701\u7565\u5927\u90e8\u5206\u6216\u6240\u6709\u8c03\u7528\u3002Visual Studio 2019\u7701\u7565\u4e863\u4e2a(\u5b83\u6ca1\u6709\u7701\u7565\u8fd4\u56de goo() \u7684\u60c5\u51b5)\uff0c\u800cGCC\u7701\u7565\u4e86\u6240\u67094\u4e2a\u3002","title":"\u62f7\u8d1d\u521d\u59cb\u5316\u7684\u5176\u4ed6\u4f7f\u7528\u573a\u666f"},{"location":"14-14-converting-constructors-explicit-and-delete/","tags":["converting constructors","explicit","delete"],"text":"Key Takeaway - \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cC++\u4f1a\u628a\u6240\u6709\u7684\u6784\u9020\u51fd\u6570\u90fd\u5f53\u505a\u9690\u5f0f\u5730\u8f6c\u6362\u64cd\u4f5c\u7b26\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include #include class Fraction { private : int m_numerator ; int m_denominator ; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator ( numerator ), m_denominator ( denominator ) { assert ( denominator != 0 ); } // Copy constructor Fraction ( const Fraction & copy ) : m_numerator ( copy . m_numerator ), m_denominator ( copy . m_denominator ) { // no need to check for a denominator of 0 here since copy must already be a valid Fraction std :: cout << \"Copy constructor called \\n \" ; // just to prove it works } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); int getNumerator () { return m_numerator ; } void setNumerator ( int numerator ) { m_numerator = numerator ; } }; void printFraction ( const Fraction & f ) { std :: cout << f ; } std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } int main () { printFraction ( 6 ); return 0 ; } \u5c3d\u7ba1 printFraction() \u671f\u671b\u63a5\u53d7\u4e00\u4e2a Fraction \uff0c\u4f46\u5b9e\u9645\u4e0a\u6211\u4eec\u7ed9\u5b83\u4f20\u9012\u4e86\u4e00\u4e2a\u5b57\u9762\u91cf 6\u3002\u56e0\u4e3a Fraction \u7684\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u6574\u578b\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u9690\u5f0f\u5730\u5c06\u5b57\u9762\u91cf6\u8f6c\u6362\u4e3a\u4e00\u4e2a Fraction \u5bf9\u8c61\u3002\u5b83\u4f1a\u4f7f\u7528 Fraction(int, int) \u6765\u521d\u59cb\u5316 printFraction() \u7684\u5f62\u53c2 f \u3002 \u7a0b\u5e8f\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 6/1 \u5bf9\u4e8e\u6240\u6709\u7c7b\u578b\u7684\u521d\u59cb\u5316\uff0c\u8be5\u9690\u5f0f\u8f6c\u6362\u90fd\u53ef\u7528\u3002 \u80fd\u591f\u88ab\u7528\u6765\u8fdb\u884c \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u7684\u6784\u9020\u51fd\u6570\uff0c\u79f0\u4e3a \u8f6c\u6362\u6784\u9020\u51fd\u6570 \u3002 explicit \u5173\u952e\u5b57 \u867d\u7136\u5728 Fraction \u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8fdb\u884c\u9690\u5f0f\u8f6c\u6362\u662f\u6709\u610f\u4e49\u7684\uff0c\u4f46\u5728\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u8fd9\u53ef\u80fd\u662f\u4e0d\u53ef\u53d6\u7684\uff0c\u4e5f\u53ef\u80fd\u5bfc\u81f4\u610f\u5916\u7684\u884c\u4e3a: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include #include class MyString { private : std :: string m_string ; public : MyString ( int x ) // allocate string of size x { m_string . resize ( x ); } MyString ( const char * string ) // allocate string to hold string value { m_string = string ; } friend std :: ostream & operator << ( std :: ostream & out , const MyString & s ); }; std :: ostream & operator << ( std :: ostream & out , const MyString & s ) { out << s . m_string ; return out ; } void printString ( const MyString & s ) { std :: cout << s ; } int main () { MyString mine = 'x' ; // Will compile and use MyString(int) std :: cout << mine << '\\n' ; printString ( 'x' ); // Will compile and use MyString(int) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u5b57\u7b26\u6765\u521d\u59cb\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u56e0\u4e3a\u5b57\u7b26\u5b9e\u9645\u4e0a\u662f\u5c5e\u4e8e\u6574\u578b\u5bb6\u65cf\u7684\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u4f7f\u7528 MyString(int) \u6784\u9020\u51fd\u6570\u9690\u5f0f\u5730\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a MyString \u3002\u5f53\u7a0b\u5e8f\u6253\u5370 MyString \u65f6\uff0c\u5176\u7ed3\u679c\u4f1a\u51fa\u4e4e\u6211\u4eec\u7684\u610f\u6599\u3002\u7c7b\u4f3c\u7684\uff0c\u8c03\u7528 printString(\u2018x\u2019) \u4e5f\u4f1a\u5bfc\u81f4\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\uff0c\u7ed3\u679c\u4e5f\u662f\u7c7b\u4f3c\u7684\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u901a\u8fc7 explicit \u5173\u952e\u5b57\u6807\u8bb0\u6784\u9020\u51fd\u6570(\u548c\u8f6c\u6362\u51fd\u6570)\uff0c\u8be5\u5173\u952e\u5b57\u653e\u5728\u51fd\u6570\u540d\u524d\u9762\u3002explicit \u7684\u6784\u9020\u51fd\u6570\u548c\u8f6c\u6362\u51fd\u6570\u5c06\u4e0d\u4f1a\u7528\u4e8e\u9690\u5f0f\u8f6c\u6362\u6216\u62f7\u8d1d\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include #include class MyString { private : std :: string m_string ; public : // explicit keyword makes this constructor ineligible for implicit conversions explicit MyString ( int x ) // allocate string of size x { m_string . resize ( x ); } MyString ( const char * string ) // allocate string to hold string value { m_string = string ; } friend std :: ostream & operator << ( std :: ostream & out , const MyString & s ); }; std :: ostream & operator << ( std :: ostream & out , const MyString & s ) { out << s . m_string ; return out ; } void printString ( const MyString & s ) { std :: cout << s ; } int main () { MyString mine = 'x' ; // \u7f16\u8bd1\u9519\u8bef\uff0c\u56e0\u4e3a MyString(int) \u73b0\u5728\u662f explicit \u7684 std :: cout << mine ; printString ( 'x' ); // \u7f16\u8bd1\u9519\u8bef\uff0cMyString(int) \u4e0d\u80fd\u88ab\u7528\u4e8e\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 return 0 ; } \u4e0a\u9762\u7684\u7a0b\u5e8f\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\uff0c\u56e0\u4e3a MyString(int) \u88ab\u5b9a\u4e49\u4e3a explicit \uff0c\u800c\u4e14\u65e0\u6cd5\u627e\u5230\u4e00\u4e2a\u53ef\u7528\u7684\u80fd\u591f\u5c06 \u2018x\u2019 \u9690\u5f0f\u8f6c\u6362\u4e3a MyString \u7684\u6784\u9020\u51fd\u6570\u3002 \u4e0d\u8fc7\uff0c\u5c06\u6784\u9020\u51fd\u6570\u6807\u8bb0\u4e3a explict \u53ea\u80fd\u9632\u6b62\u9690\u5f0f\u7c7b\u8f6c\u6362\u3002\u663e\u793a\u7684\u7c7b\u578b\u8f6c\u6362\u4ecd\u7136\u662f\u53ef\u4ee5\u7684\uff1a 1 std :: cout << static_cast < MyString > ( 5 ); // Allowed: explicit cast of 5 to MyString(int) \u76f4\u63a5\u521d\u59cb\u5316 \u548c \u7edf\u4e00\u521d\u59cb\u5316 \u4ecd\u4f1a\u5bf9\u53c2\u6570\u8fdb\u884c\u8f6c\u6362\u4f7f\u5176\u5339\u914d\uff08\u7edf\u4e00\u521d\u59cb\u5316\u867d\u7136\u4e0d\u4f1a\u8fdb\u884c \u7f29\u7a84\u8f6c\u6362 \uff0c\u4f46\u662f\u53ef\u80fd\u4f1a\u8fdb\u884c\u5176\u4ed6\u7c7b\u578b\u7684\u8f6c\u6362\uff09\u3002 1 MyString str { 'x' }; // Allowed: initialization parameters may still be implicitly converted to match \u6700\u4f73\u5b9e\u8df5 \u8003\u8651\u5c06\u6784\u9020\u51fd\u6570\u548c\u7528\u6237\u5b9a\u4e49\u8f6c\u6362\u6210\u5458\u51fd\u6570\u8bbe\u7f6e\u4e3a\u663e\u5f0f\u7684\uff0c\u4ee5\u9632\u6b62\u9690\u5f0f\u8f6c\u6362\u9519\u8bef\u3002 delete \u5173\u952e\u5b57 \u5728 MyString \u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u5e0c\u671b\u5b8c\u5168\u7981\u6b62 x \u88ab\u8f6c\u6362\u4e3a MyString (\u65e0\u8bba\u662f\u9690\u5f0f\u7684\u8fd8\u662f\u663e\u5f0f\u7684\uff0c\u56e0\u4e3a\u7ed3\u679c\u7b26\u5408\u76f4\u89c9)\u3002\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u662f\u6dfb\u52a0 MyString(char) \u6784\u9020\u51fd\u6570\uff0c\u5e76\u5c06\u5176\u8bbe\u4e3a\u79c1\u6709: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include class MyString { private : std :: string m_string ; MyString ( char ) // objects of type MyString(char) can't be constructed from outside the class { } public : // explicit keyword makes this constructor ineligible for implicit conversions explicit MyString ( int x ) // allocate string of size x { m_string . resize ( x ); } MyString ( const char * string ) // allocate string to hold string value { m_string = string ; } friend std :: ostream & operator << ( std :: ostream & out , const MyString & s ); }; std :: ostream & operator << ( std :: ostream & out , const MyString & s ) { out << s . m_string ; return out ; } int main () { MyString mine ( 'x' ); // compile error, since MyString(char) is private std :: cout << mine ; return 0 ; } COPY \u4f46\u662f\uff0c\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u4ecd\u7136\u53ef\u4ee5\u5728\u7c7b\u5185\u90e8\u4f7f\u7528(\u79c1\u6709\u8bbf\u95ee\u53ea\u9632\u6b62\u975e\u6210\u5458\u8c03\u7528\u6b64\u51fd\u6570)\u3002 \u66f4\u597d\u7684\u89e3\u51b3\u65b9\u6cd5\u662f\u4f7f\u7528 delete \u5173\u952e\u5b57\u5220\u9664\u51fd\u6570: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include class MyString { private : std :: string m_string ; public : MyString ( char ) = delete ; // \u4efb\u4f55\u4f7f\u7528\u8be5\u51fd\u6570\u7684\u5730\u65b9\u90fd\u4f1a\u62a5\u9519 // explicit \u4f7f\u5f97\u8be5\u6784\u9020\u51fd\u6570\u4e0d\u80fd\u591f\u88ab\u7528\u4e8e\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 explicit MyString ( int x ) // allocate string of size x / { m_string . resize ( x ); } MyString ( const char * string ) // allocate string to hold string value { m_string = string ; } friend std :: ostream & operator << ( std :: ostream & out , const MyString & s ); }; std :: ostream & operator << ( std :: ostream & out , const MyString & s ) { out << s . m_string ; return out ; } int main () { MyString mine ( 'x' ); // compile error, since MyString(char) is deleted std :: cout << mine ; return 0 ; } \u5f53\u51fd\u6570\u88ab delete \u4e4b\u540e\uff0c\u4efb\u4f55\u5bf9\u8be5\u6784\u9020\u51fd\u6570\u7684\u4f7f\u7528\u90fd\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u62a5\u9519\u3002 \u6ce8\u610f\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u91cd\u8f7d\u8fd0\u7b97\u7b26\u4e5f\u53ef\u4ee5\u88ab delete \uff0c\u4ee5\u907f\u514d\u5bf9\u5e94\u7684\u64cd\u4f5c\u3002","title":"14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57"},{"location":"14-14-converting-constructors-explicit-and-delete/#explicit","text":"\u867d\u7136\u5728 Fraction \u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8fdb\u884c\u9690\u5f0f\u8f6c\u6362\u662f\u6709\u610f\u4e49\u7684\uff0c\u4f46\u5728\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u8fd9\u53ef\u80fd\u662f\u4e0d\u53ef\u53d6\u7684\uff0c\u4e5f\u53ef\u80fd\u5bfc\u81f4\u610f\u5916\u7684\u884c\u4e3a: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include #include class MyString { private : std :: string m_string ; public : MyString ( int x ) // allocate string of size x { m_string . resize ( x ); } MyString ( const char * string ) // allocate string to hold string value { m_string = string ; } friend std :: ostream & operator << ( std :: ostream & out , const MyString & s ); }; std :: ostream & operator << ( std :: ostream & out , const MyString & s ) { out << s . m_string ; return out ; } void printString ( const MyString & s ) { std :: cout << s ; } int main () { MyString mine = 'x' ; // Will compile and use MyString(int) std :: cout << mine << '\\n' ; printString ( 'x' ); // Will compile and use MyString(int) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u5b57\u7b26\u6765\u521d\u59cb\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u56e0\u4e3a\u5b57\u7b26\u5b9e\u9645\u4e0a\u662f\u5c5e\u4e8e\u6574\u578b\u5bb6\u65cf\u7684\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u4f7f\u7528 MyString(int) \u6784\u9020\u51fd\u6570\u9690\u5f0f\u5730\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a MyString \u3002\u5f53\u7a0b\u5e8f\u6253\u5370 MyString \u65f6\uff0c\u5176\u7ed3\u679c\u4f1a\u51fa\u4e4e\u6211\u4eec\u7684\u610f\u6599\u3002\u7c7b\u4f3c\u7684\uff0c\u8c03\u7528 printString(\u2018x\u2019) \u4e5f\u4f1a\u5bfc\u81f4\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\uff0c\u7ed3\u679c\u4e5f\u662f\u7c7b\u4f3c\u7684\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u901a\u8fc7 explicit \u5173\u952e\u5b57\u6807\u8bb0\u6784\u9020\u51fd\u6570(\u548c\u8f6c\u6362\u51fd\u6570)\uff0c\u8be5\u5173\u952e\u5b57\u653e\u5728\u51fd\u6570\u540d\u524d\u9762\u3002explicit \u7684\u6784\u9020\u51fd\u6570\u548c\u8f6c\u6362\u51fd\u6570\u5c06\u4e0d\u4f1a\u7528\u4e8e\u9690\u5f0f\u8f6c\u6362\u6216\u62f7\u8d1d\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include #include class MyString { private : std :: string m_string ; public : // explicit keyword makes this constructor ineligible for implicit conversions explicit MyString ( int x ) // allocate string of size x { m_string . resize ( x ); } MyString ( const char * string ) // allocate string to hold string value { m_string = string ; } friend std :: ostream & operator << ( std :: ostream & out , const MyString & s ); }; std :: ostream & operator << ( std :: ostream & out , const MyString & s ) { out << s . m_string ; return out ; } void printString ( const MyString & s ) { std :: cout << s ; } int main () { MyString mine = 'x' ; // \u7f16\u8bd1\u9519\u8bef\uff0c\u56e0\u4e3a MyString(int) \u73b0\u5728\u662f explicit \u7684 std :: cout << mine ; printString ( 'x' ); // \u7f16\u8bd1\u9519\u8bef\uff0cMyString(int) \u4e0d\u80fd\u88ab\u7528\u4e8e\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 return 0 ; } \u4e0a\u9762\u7684\u7a0b\u5e8f\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\uff0c\u56e0\u4e3a MyString(int) \u88ab\u5b9a\u4e49\u4e3a explicit \uff0c\u800c\u4e14\u65e0\u6cd5\u627e\u5230\u4e00\u4e2a\u53ef\u7528\u7684\u80fd\u591f\u5c06 \u2018x\u2019 \u9690\u5f0f\u8f6c\u6362\u4e3a MyString \u7684\u6784\u9020\u51fd\u6570\u3002 \u4e0d\u8fc7\uff0c\u5c06\u6784\u9020\u51fd\u6570\u6807\u8bb0\u4e3a explict \u53ea\u80fd\u9632\u6b62\u9690\u5f0f\u7c7b\u8f6c\u6362\u3002\u663e\u793a\u7684\u7c7b\u578b\u8f6c\u6362\u4ecd\u7136\u662f\u53ef\u4ee5\u7684\uff1a 1 std :: cout << static_cast < MyString > ( 5 ); // Allowed: explicit cast of 5 to MyString(int) \u76f4\u63a5\u521d\u59cb\u5316 \u548c \u7edf\u4e00\u521d\u59cb\u5316 \u4ecd\u4f1a\u5bf9\u53c2\u6570\u8fdb\u884c\u8f6c\u6362\u4f7f\u5176\u5339\u914d\uff08\u7edf\u4e00\u521d\u59cb\u5316\u867d\u7136\u4e0d\u4f1a\u8fdb\u884c \u7f29\u7a84\u8f6c\u6362 \uff0c\u4f46\u662f\u53ef\u80fd\u4f1a\u8fdb\u884c\u5176\u4ed6\u7c7b\u578b\u7684\u8f6c\u6362\uff09\u3002 1 MyString str { 'x' }; // Allowed: initialization parameters may still be implicitly converted to match \u6700\u4f73\u5b9e\u8df5 \u8003\u8651\u5c06\u6784\u9020\u51fd\u6570\u548c\u7528\u6237\u5b9a\u4e49\u8f6c\u6362\u6210\u5458\u51fd\u6570\u8bbe\u7f6e\u4e3a\u663e\u5f0f\u7684\uff0c\u4ee5\u9632\u6b62\u9690\u5f0f\u8f6c\u6362\u9519\u8bef\u3002","title":"explicit \u5173\u952e\u5b57"},{"location":"14-14-converting-constructors-explicit-and-delete/#delete","text":"\u5728 MyString \u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u5e0c\u671b\u5b8c\u5168\u7981\u6b62 x \u88ab\u8f6c\u6362\u4e3a MyString (\u65e0\u8bba\u662f\u9690\u5f0f\u7684\u8fd8\u662f\u663e\u5f0f\u7684\uff0c\u56e0\u4e3a\u7ed3\u679c\u7b26\u5408\u76f4\u89c9)\u3002\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u662f\u6dfb\u52a0 MyString(char) \u6784\u9020\u51fd\u6570\uff0c\u5e76\u5c06\u5176\u8bbe\u4e3a\u79c1\u6709: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include class MyString { private : std :: string m_string ; MyString ( char ) // objects of type MyString(char) can't be constructed from outside the class { } public : // explicit keyword makes this constructor ineligible for implicit conversions explicit MyString ( int x ) // allocate string of size x { m_string . resize ( x ); } MyString ( const char * string ) // allocate string to hold string value { m_string = string ; } friend std :: ostream & operator << ( std :: ostream & out , const MyString & s ); }; std :: ostream & operator << ( std :: ostream & out , const MyString & s ) { out << s . m_string ; return out ; } int main () { MyString mine ( 'x' ); // compile error, since MyString(char) is private std :: cout << mine ; return 0 ; } COPY \u4f46\u662f\uff0c\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u4ecd\u7136\u53ef\u4ee5\u5728\u7c7b\u5185\u90e8\u4f7f\u7528(\u79c1\u6709\u8bbf\u95ee\u53ea\u9632\u6b62\u975e\u6210\u5458\u8c03\u7528\u6b64\u51fd\u6570)\u3002 \u66f4\u597d\u7684\u89e3\u51b3\u65b9\u6cd5\u662f\u4f7f\u7528 delete \u5173\u952e\u5b57\u5220\u9664\u51fd\u6570: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include class MyString { private : std :: string m_string ; public : MyString ( char ) = delete ; // \u4efb\u4f55\u4f7f\u7528\u8be5\u51fd\u6570\u7684\u5730\u65b9\u90fd\u4f1a\u62a5\u9519 // explicit \u4f7f\u5f97\u8be5\u6784\u9020\u51fd\u6570\u4e0d\u80fd\u591f\u88ab\u7528\u4e8e\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 explicit MyString ( int x ) // allocate string of size x / { m_string . resize ( x ); } MyString ( const char * string ) // allocate string to hold string value { m_string = string ; } friend std :: ostream & operator << ( std :: ostream & out , const MyString & s ); }; std :: ostream & operator << ( std :: ostream & out , const MyString & s ) { out << s . m_string ; return out ; } int main () { MyString mine ( 'x' ); // compile error, since MyString(char) is deleted std :: cout << mine ; return 0 ; } \u5f53\u51fd\u6570\u88ab delete \u4e4b\u540e\uff0c\u4efb\u4f55\u5bf9\u8be5\u6784\u9020\u51fd\u6570\u7684\u4f7f\u7528\u90fd\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u62a5\u9519\u3002 \u6ce8\u610f\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u91cd\u8f7d\u8fd0\u7b97\u7b26\u4e5f\u53ef\u4ee5\u88ab delete \uff0c\u4ee5\u907f\u514d\u5bf9\u5e94\u7684\u64cd\u4f5c\u3002","title":"delete \u5173\u952e\u5b57"},{"location":"14-15-overloading-the-assignment-operator/","tags":["overload","assignment"],"text":"Key Takeaway - The assignment operator (operator=) is used to copy values from one object to another already existing object . Assignment vs Copy constructor The purpose of the copy constructor and the assignment operator are almost equivalent -- both copy one object to another. However, the copy constructor initializes new objects, whereas the assignment operator replaces the contents of existing objects. The difference between the copy constructor and the assignment operator causes a lot of confusion for new programmers, but it\u2019s really not all that difficult. Summarizing: If a new object has to be created before the copying can occur, the copy constructor is used (note: this includes passing or returning objects by value). If a new object does not have to be created before the copying can occur, the assignment operator is used. Overloading the assignment operator Overloading the assignment operator (operator=) is fairly straightforward, with one specific caveat that we\u2019ll get to. The assignment operator must be overloaded as a member function. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 #include #include class Fraction { private : int m_numerator { 0 }; int m_denominator { 1 }; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } // Copy constructor Fraction ( const Fraction & copy ) : m_numerator { copy . m_numerator }, m_denominator { copy . m_denominator } { // no need to check for a denominator of 0 here since copy must already be a valid Fraction std :: cout << \"Copy constructor called \\n \" ; // just to prove it works } // Overloaded assignment Fraction & operator = ( const Fraction & fraction ); friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } // A simplistic implementation of operator= (see better implementation below) Fraction & Fraction :: operator = ( const Fraction & fraction ) { // do the copy m_numerator = fraction . m_numerator ; m_denominator = fraction . m_denominator ; // return the existing object so we can chain this operator return * this ; } int main () { Fraction fiveThirds { 5 , 3 }; Fraction f ; f = fiveThirds ; // calls overloaded assignment std :: cout << f ; return 0 ; } COPY This prints: 5/3 This should all be pretty straightforward by now. Our overloaded operator= returns *this, so that we can chain multiple assignments together: 1 2 3 4 5 6 7 8 9 10 int main () { Fraction f1 { 5 , 3 }; Fraction f2 { 7 , 2 }; Fraction f3 { 9 , 5 }; f1 = f2 = f3 ; // chained assignment return 0 ; } COPY Issues due to self-assignment Here\u2019s where things start to get a little more interesting. C++ allows self-assignment: 1 2 3 4 5 6 7 int main () { Fraction f1 { 5 , 3 }; f1 = f1 ; // self assignment return 0 ; } COPY This will call f1.operator=(f1), and under the simplistic implementation above, all of the members will be assigned to themselves. In this particular example, the self-assignment causes each member to be assigned to itself, which has no overall impact, other than wasting time. In most cases, a self-assignment doesn\u2019t need to do anything at all! However, in cases where an assignment operator needs to dynamically assign memory, self-assignment can actually be dangerous: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 #include class MyString { private : char * m_data {}; int m_length {}; public : MyString ( const char * data = nullptr , int length = 0 ) : m_length { length } { if ( length ) { m_data = new char [ length ]; for ( int i { 0 }; i < length ; ++ i ) m_data [ i ] = data [ i ]; } } ~ MyString () { delete [] m_data ; } // Overloaded assignment MyString & operator = ( const MyString & str ); friend std :: ostream & operator << ( std :: ostream & out , const MyString & s ); }; std :: ostream & operator << ( std :: ostream & out , const MyString & s ) { out << s . m_data ; return out ; } // A simplistic implementation of operator= (do not use) MyString & MyString :: operator = ( const MyString & str ) { // if data exists in the current string, delete it if ( m_data ) delete [] m_data ; m_length = str . m_length ; // copy the data from str to the implicit object m_data = new char [ str . m_length ]; for ( int i { 0 }; i < str . m_length ; ++ i ) m_data [ i ] = str . m_data [ i ]; // return the existing object so we can chain this operator return * this ; } int main () { MyString alex ( \"Alex\" , 5 ); // Meet Alex MyString employee ; employee = alex ; // Alex is our newest employee std :: cout << employee ; // Say your name, employee return 0 ; } COPY First, run the program as it is. You\u2019ll see that the program prints \u201cAlex\u201d as it should. Now run the following program: 1 2 3 4 5 6 7 8 int main () { MyString alex { \"Alex\" , 5 }; // Meet Alex alex = alex ; // Alex is himself std :: cout << alex ; // Say your name, Alex return 0 ; } COPY You\u2019ll probably get garbage output. What happened? Consider what happens in the overloaded operator= when the implicit object AND the passed in parameter (str) are both variable alex. In this case, m_data is the same as str.m_data. The first thing that happens is that the function checks to see if the implicit object already has a string. If so, it needs to delete it, so we don\u2019t end up with a memory leak. In this case, m_data is allocated, so the function deletes m_data. But because str is the same as *this, the string that we wanted to copy has been deleted and m_data (and str.m_data) are dangling. Later on, we allocate new memory to m_data (and str.m_data). So when we subsequently copy the data from str.m_data into m_data, we\u2019re copying garbage, because str.m_data was never initialized. Detecting and handling self-assignment Fortunately, we can detect when self-assignment occurs. Here\u2019s an updated implementation of our overloaded operator= for the MyString class: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 MyString & MyString :: operator = ( const MyString & str ) { // self-assignment check if ( this == & str ) return * this ; // if data exists in the current string, delete it if ( m_data ) delete [] m_data ; m_length = str . m_length ; // copy the data from str to the implicit object m_data = new char [ str . m_length ]; for ( int i { 0 }; i < str . m_length ; ++ i ) m_data [ i ] = str . m_data [ i ]; // return the existing object so we can chain this operator return * this ; } COPY By checking if the address of our implicit object is the same as the address of the object being passed in as a parameter, we can have our assignment operator just return immediately without doing any other work. Because this is just a pointer comparison, it should be fast, and does not require operator== to be overloaded. When not to handle self-assignment Typically the self-assignment check is skipped for copy constructors. Because the object being copy constructed is newly created, the only case where the newly created object can be equal to the object being copied is when you try to initialize a newly defined object with itself: 1 someClass c { c }; COPY In such cases, your compiler should warn you that c is an uninitialized variable. Second, the self-assignment check may be omitted in classes that can naturally handle self-assignment. Consider this Fraction class assignment operator that has a self-assignment guard: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // A better implementation of operator= Fraction & Fraction :: operator = ( const Fraction & fraction ) { // self-assignment guard if ( this == & fraction ) return * this ; // do the copy m_numerator = fraction . m_numerator ; // can handle self-assignment m_denominator = fraction . m_denominator ; // can handle self-assignment // return the existing object so we can chain this operator return * this ; } COPY If the self-assignment guard did not exist, this function would still operate correctly during a self-assignment (because all of the operations done by the function can handle self-assignment properly). Because self-assignment is a rare event, some prominent C++ gurus recommend omitting the self-assignment guard even in classes that would benefit from it. We do not recommend this, as we believe it\u2019s a better practice to code defensively and then selectively optimize later. The copy and swap idiom A better way to handle self-assignment issues is via what\u2019s called the copy and swap idiom. There\u2019s a great writeup of how this idiom works on Stack Overflow . Default assignment operator Unlike other operators, the compiler will provide a default public assignment operator for your class if you do not provide one. This assignment operator does memberwise assignment (which is essentially the same as the memberwise initialization that default copy constructors do). Just like other constructors and operators, you can prevent assignments from being made by making your assignment operator private or using the delete keyword: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #include #include class Fraction { private : int m_numerator { 0 }; int m_denominator { 1 }; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } // Copy constructor Fraction ( const Fraction & copy ) = delete ; // Overloaded assignment Fraction & operator = ( const Fraction & fraction ) = delete ; // no copies through assignment! friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } int main () { Fraction fiveThirds { 5 , 3 }; Fraction f ; f = fiveThirds ; // compile error, operator= has been deleted std :: cout << f ; return 0 ; }","title":"14.15 - \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26"},{"location":"14-16-shallow-vs-deep-copying/","tags":["shallow copy","deep copy"],"text":"Key Takeaway - \u6d45\u62f7\u8d1d \u56e0\u4e3a C++ \u5e76\u4e0d\u4e86\u89e3\u4f60\u7684\u7c7b\uff0c\u6240\u4ee5\u9ed8\u8ba4\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4ee5\u53ca\u9ed8\u8ba4\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u4f1a\u4f7f\u7528 \u6210\u5458\u4f9d\u6b21\u62f7\u8d1d \uff08\u4e5f\u79f0\u4e3a \u6d45\u62f7\u8d1d \uff09\u7684\u65b9\u5f0f\u4fe1\u606f\u62f7\u8d1d\u3002\u4e5f\u5c31\u662f\u8bf4\uff0cC++ \u4f1a\u4f9d\u6b21\u62f7\u8d1d\u7c7b\u7684\u6bcf\u4e00\u4e2a\u6210\u5458\uff08\u4f7f\u7528\u7b49\u53f7\u662f\u8c03\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\u3001\u5728\u4f7f\u7528\u62f7\u8d1d\u6784\u9020\u65f6\uff0c\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\uff09\u3002\u5bf9\u4e8e\u7b80\u5355\u7c7b\uff08\u4f8b\u5982\uff0c\u4e0d\u5305\u542b\u52a8\u6001\u5185\u5b58\u7684\u7c7b\uff09\uff0c\u8fd9\u4e48\u505a\u975e\u5e38\u5408\u9002\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include class Fraction { private : int m_numerator { 0 }; int m_denominator { 1 }; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator } , m_denominator { denominator } { assert ( denominator != 0 ); } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } \u8fd9\u91cc\u9ed8\u8ba4\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u9ed8\u8ba4\u8d4b\u503c\u8fd0\u7b97\u7b26\u5f62\u5f0f\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include #include class Fraction { private : int m_numerator { 0 }; int m_denominator { 1 }; public : // \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator } , m_denominator { denominator } { assert ( denominator != 0 ); } // \u9690\u5f0f\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u7684\u4e00\u79cd\u53ef\u80fd\u5b9e\u73b0 Fraction ( const Fraction & f ) : m_numerator { f . m_numerator } , m_denominator { f . m_denominator } { } // \u9690\u5f0f\u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u4e00\u79cd\u53ef\u80fd\u5b9e\u73b0 Fraction & operator = ( const Fraction & fraction ) { // \u907f\u514d\u81ea\u6211\u8d4b\u503c if ( this == & fraction ) return * this ; // \u62f7\u8d1d m_numerator = fraction . m_numerator ; m_denominator = fraction . m_denominator ; // \u8fd4\u56de\u5bf9\u8c61\u672c\u8eab\uff0c\u4f7f\u64cd\u4f5c\u7b26\u53ef\u4ee5\u94fe\u5f0f\u8c03\u7528 return * this ; } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } }; \u6ce8\u610f\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u9ed8\u8ba4\u7248\u672c\u7684\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u5f88\u597d\u5730\u5b8c\u6210\u62f7\u8d1d\uff0c\u6211\u4eec\u5b8c\u5168\u6ca1\u6709\u5fc5\u8981\u7f16\u5199\u81ea\u5df1\u7684\u7248\u672c\u3002 \u4f46\u662f\uff0c\u5728\u8bbe\u8ba1\u6301\u6709\u52a8\u6001\u5185\u5b58\u7684\u7c7b\u65f6\uff0c\u6d45\u62f7\u8d1d\u4f1a\u5e26\u6765\u5f88\u591a\u95ee\u9898\uff01\u56e0\u4e3a\u6d45\u62f7\u8d1d\u5728\u62f7\u8d1d\u6307\u9488\u65f6\uff0c\u53ea\u662f\u62f7\u8d1d\u4e86\u6307\u9488\u7684\u5730\u5740\u2014\u2014\u5b83\u5e76\u6ca1\u6709\u5206\u914d\u65b0\u7684\u5730\u5740\u6216\u62f7\u8d1d\u6307\u9488\u6240\u6307\u7684\u5185\u5bb9\u3002 \u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include // for strlen() #include // for assert() class MyString { private : char * m_data {}; int m_length {}; public : MyString ( const char * source = \"\" ) { assert ( source ); // make sure source isn't a null string // Find the length of the string // Plus one character for a terminator m_length = std :: strlen ( source ) + 1 ; // Allocate a buffer equal to this length m_data = new char [ m_length ]; // Copy the parameter string into our internal buffer for ( int i { 0 }; i < m_length ; ++ i ) m_data [ i ] = source [ i ]; } ~ MyString () // destructor { // We need to deallocate our string delete [] m_data ; } char * getString () { return m_data ; } int getLength () { return m_length ; } }; \u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u7c7b\uff0c\u5b83\u5206\u914d\u4e86\u4e00\u6bb5\u5185\u5b58\u7528\u4e8e\u4fdd\u5b58\u4f20\u5165\u7684\u5b57\u7b26\u4e32\u3002\u6ce8\u610f\uff0c\u6211\u4eec\u5e76\u6ca1\u6709\u5b9a\u4e49 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u6216\u8005\u91cd\u8f7d\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002\u56e0\u6b64\uff0cC++\u4f1a\u63d0\u4f9b\u4e00\u4e2a\u9ed8\u8ba4\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u9ed8\u8ba4\u8d4b\u503c\u8fd0\u7b97\u7b26\u4ee5\u4fbf\u6267\u884c\u6d45\u62f7\u8d1d\u3002\u5176\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u770b\u4e0a\u53bb\u5e94\u8be5\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 MyString :: MyString ( const MyString & source ) : m_length { source . m_length } , m_data { source . m_data } { } \u6ce8\u610f\uff0c m_data \u53ea\u662f source.m_data \u7684\u4e00\u4e2a\u6d45\u62f7\u8d1d\u6307\u9488\uff0c\u6240\u4ee5\u5b83\u4eec\u6307\u5411\u540c\u4e00\u5757\u5185\u5b58\u3002 \u73b0\u5728\uff0c \u8003\u8651\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { MyString hello { \"Hello, world!\" }; { MyString copy { hello }; // \u4f7f\u7528\u9ed8\u8ba4\u62f7\u8d1d\u6784\u9020\u51fd\u6570 } // copy \u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u5728\u6b64\u5904\u4f1a\u9500\u6bc1\u3002\u6240\u4ee5\u5176\u6790\u6784\u51fd\u6570UI\u5220\u9664copy\u6301\u6709\u7684\u5b57\u7b26\u4e32\uff0c\u6240\u4ee5hello\u6b64\u65f6\u6301\u6709\u4e00\u4e2a\u60ac\u5782\u6307\u9488 std :: cout << hello . getString () << '\\n' ; // \u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a return 0 ; } \u867d\u7136\u8fd9\u6bb5\u4ee3\u7801\u770b\u8d77\u6765\u6ca1\u5565\u95ee\u9898\uff0c\u4f46\u5b83\u5305\u542b\u4e00\u4e2a\u6f5c\u5728\u7684\u95ee\u9898\uff0c\u5c06\u5bfc\u81f4\u7a0b\u5e8f\u51fa\u73b0 \u672a\u5b9a\u4e49\u884c\u4e3a \uff01 \u8ba9\u6211\u4eec\u9010\u884c\u5206\u6790\u8fd9\u4e2a\u4f8b\u5b50: 1 MyString hello { \"Hello, world!\" }; \u8fd9\u4e00\u884c\u6ca1\u4ec0\u4e48\u95ee\u9898\u3002\u5b83\u8c03\u7528 MyString \u6784\u9020\u51fd\u6570\uff0c\u5206\u914d\u4e00\u6bb5\u5185\u5b58\uff0c\u7136\u540e\u8ba9 hello.m_data \u6307\u5411\u5b83\uff0c\u7136\u540e\u62f7\u8d1d\u5b57\u7b26\u4e32\u201cHello, world!\u201d \u5230\u8be5\u5185\u5b58\u3002 1 MyString copy { hello }; // \u4f7f\u7528\u9ed8\u8ba4\u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u8fd9\u4e00\u884c\u4ee3\u7801\u770b\u4e0a\u53bb\u4e5f\u4eba\u755c\u65e0\u5bb3\uff0c\u4f46\u662f\u5b83\u5176\u5b9e\u662f\u95ee\u9898\u7684\u6839\u6e90\uff01\u8fd9\u884c\u4ee3\u7801\u5728\u6c42\u503c\u65f6\uff0cC++\u4f1a\u4f7f\u7528\u9ed8\u8ba4\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff08\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u63d0\u4f9b\u81ea\u5df1\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff09\u3002\u8be5\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4f1a\u6267\u884c\u6d45\u62f7\u8d1d\uff0c\u5c06 copy.m_data \u521d\u59cb\u5316\u4e3a hello.m_data \u6301\u6709\u7684\u5730\u5740\u3002\u56e0\u6b64\uff0c copy.m_data \u548c hello.m_data \u6307\u5411\u4e00\u5757\u76f8\u540c\u7684\u5185\u5b58\uff01 1 } // copy \u5728\u6b64\u5904\u9500\u6bc1 \u5f53 copy \u79bb\u5f00\u4f5c\u7528\u57df \u65f6\uff0c MyString \u7684\u6790\u6784\u51fd\u6570\u4f1a\u88ab\u8c03\u7528\u3002\u6790\u6784\u51fd\u6570\u4f1a\u5220\u9664 copy.m_data \u548c hello.m_data \u6240\u6307\u7684\u90a3\u6bb5\u5185\u5b58\uff01\u56e0\u6b64\uff0c\u5f53 copy \u88ab\u5220\u9664\u65f6\uff0c\u5176\u6790\u6784\u8fc7\u7a0b\u4e0d\u53ef\u907f\u514d\u5730\u5f71\u54cd\u4e86 hello \u3002\u53d8\u91cf copy \u968f\u540e\u5c31\u88ab\u9500\u6bc1\u4e86\uff0c\u4f46\u662f hello.m_data \u4ecd\u7136\u6307\u5411\u8fd9\u5757\u88ab\u5220\u9664\u7684\u5185\u5b58\uff08\u975e\u6cd5\u5185\u5b58\uff09\uff01 1 std :: cout << hello . getString () << '\\n' ; // \u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a \u73b0\u5728\u4f60\u80fd\u660e\u767d\u4e3a\u4ec0\u4e48\u7a0b\u5e8f\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u4e86\u5427\u3002\u6211\u4eec\u5df2\u7ecf\u5220\u9664\u4e86 hello \u6240\u6307\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u53c8\u5c1d\u8bd5\u6253\u5370\u8fd9\u5757\u5185\u5b58\u7684\u5185\u5bb9\u3002 \u8fd9\u4e2a\u95ee\u9898\u7684\u6839\u6e90\u662f\u7531\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u6267\u884c\u7684\u6d45\u62f7\u8d1d\u5bfc\u81f4\u7684\u2014\u2014\u5728\u221a\u6784\u9020\u51fd\u6570\u6216\u91cd\u8f7d\u8d4b\u503c\u64cd\u4f5c\u7b26\u4e2d\u5bf9\u6307\u9488\u503c\u8fdb\u884c\u6d45\u51e0\u4e4e\u603b\u662f\u4f1a\u5e26\u6765\u9ebb\u70e6\u3002 \u6df1\u62f7\u8d1d \u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\u7684\u529e\u6cd5\u662f\u5bf9\u4efb\u4f55\u975e\u7a7a\u6307\u9488\u8fdb\u884c\u6df1\u62f7\u8d1d\u3002 \u6df1\u62f7\u8d1d \u4f1a\u9996\u5148\u4e3a\u526f\u672c\u5206\u914d\u5185\u5b58\uff0c\u7136\u540e\u518d\u62f7\u8d1d\u5b9e\u9645\u7684\u503c\uff0c\u8fd9\u6837\u4e00\u6765\u526f\u672c\u6301\u6709\u7684\u5185\u5b58\u5c31\u548c\u6e90\u5185\u5b58\u4e0d\u540c\u4e86\u3002\u526f\u672c\u548c\u539f\u672c\u7531\u4e8e\u6301\u6709\u4e0d\u540c\u7684\u5185\u5b58\uff0c\u6240\u4ee5\u4fbf\u4e0d\u4f1a\u4e92\u76f8\u5f71\u54cd\u3002\u8981\u6267\u884c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u5219\u5fc5\u987b\u7f16\u5199\u6211\u4eec\u81ea\u5df1\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002 \u4ee5 MyString \u4e3a\u4e86\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u5982\u4f55\u8fdb\u884c\u6df1\u62f7\u8d1d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 // assumes m_data is initialized void MyString::deepCopy ( const MyString & source ) { // \u9996\u5148\u5e94\u8be5\u91ca\u653e\u5f53\u524d\u5b58\u653e\u7684\u5b57\u7b26\u4e32 delete [] m_data ; // \u56e0\u4e3a m_length \u4e0d\u662f\u6307\u9488\uff0c\u6240\u4ee5\u6d45\u62f7\u8d1d\u5373\u53ef m_length = source . m_length ; // m_data \u662f\u6307\u9488\uff0c\u6240\u4ee5\u5f53\u5b83\u4e0d\u662f\u7a7a\u6307\u9488\u662f\u5c31\u5fc5\u987b\u8fdb\u884c\u6df1\u62f7\u8d1d if ( source . m_data ) { // \u4e3a\u526f\u672c\u5206\u914d\u5185\u5b58 m_data = new char [ m_length ]; // \u62f7\u8d1d for ( int i { 0 }; i < m_length ; ++ i ) m_data [ i ] = source . m_data [ i ]; } else m_data = nullptr ; } // Copy constructor MyString :: MyString ( const MyString & source ) { deepCopy ( source ); } \u4ece\u4e0a\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u770b\u51fa\uff0c\u6df1\u62f7\u8d1d\u88ab\u6d45\u62f7\u8d1d\u8981\u590d\u6742\u4e0d\u5c11\uff01\u9996\u5148\uff0c\u6211\u4eec\u5fc5\u987b\u539f\u672c\u542b\u6709\u5b57\u7b26\u4e32\uff0811\u884c\uff09\u3002\u5982\u679c\u6709\u7684\u8bdd\uff0c\u5219\u5fc5\u987b\u4e3a\u526f\u672c\u5206\u914d\u8db3\u591f\u7684\u5185\u5b58\u7528\u4e8e\u4fdd\u5b58\u5b57\u7b26\u4e32\u7684\u526f\u672c\uff0814\u884c\uff09\u3002\u6700\u540e\uff0c\u6211\u4eec\u5fc5\u987b\u624b\u52a8\u62f7\u8d1d\u5b57\u7b26\u4e32\uff0817\u884c\u548c18\u884c\uff09\u3002 \u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 // Assignment operator MyString & MyString :: operator = ( const MyString & source ) { // check for self-assignment if ( this != & source ) { // now do the deep copy deepCopy ( source ); } return * this ; } \u6ce8\u610f\uff0c\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26\u548c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u975e\u5e38\u7c7b\u4f3c\uff0c\u4f46\u662f\u6709\u4e09\u5904\u660e\u663e\u7684\u4e0d\u540c\uff1a \u6dfb\u52a0\u4e86\u4e00\u4e2a\u81ea\u6211\u590d\u5236\u68c0\u67e5\uff1b \u8fd4\u56de *this \u6307\u9488\u4e00\u822c\u80fd\u591f\u94fe\u5f0f\u8c03\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\uff1b \u5fc5\u987b\u663e\u5f0f\u5730\u91ca\u653e\u5df2\u7ecf\u6301\u6709\u7684\u5b57\u7b26\u4e32\uff08\u907f\u514d m_data \u91cd\u65b0\u5206\u914d\u65f6\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\uff09 \u3002\u8be5\u64cd\u4f5c\u662f\u5728 deepCopy \u5728\u5b8c\u6210\u7684 \u5f53\u8c03\u7528\u91cd\u8f7d\u8d4b\u503c\u64cd\u4f5c\u7b26\u65f6\uff0c\u88ab\u8d4b\u503c\u7684\u9879\u53ef\u80fd\u5df2\u7ecf\u5305\u542b\u4e86\u4ee5\u524d\u7684\u503c\uff0c\u9700\u8981\u786e\u4fdd\u5728\u4e3a\u65b0\u503c\u5206\u914d\u5185\u5b58\u4e4b\u524d\u5c06\u5176\u6e05\u9664\u3002\u5bf9\u4e8e\u975e\u52a8\u6001\u5206\u914d\u7684\u53d8\u91cf(\u5927\u5c0f\u56fa\u5b9a)\uff0c\u6211\u4eec\u4e0d\u5fc5\u8d39\u5fc3\uff0c\u56e0\u4e3a\u65b0\u503c\u53ea\u662f\u8986\u76d6\u65e7\u503c\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u52a8\u6001\u5206\u914d\u7684\u53d8\u91cf\uff0c\u5728\u5206\u914d\u65b0\u5185\u5b58\u4e4b\u524d\u9700\u8981\u663e\u5f0f\u5730\u91ca\u653e\u65e7\u5185\u5b58\u3002\u5982\u679c\u6211\u4eec\u4e0d\u8fd9\u6837\u505a\uff0c\u4ee3\u7801\u4e0d\u4f1a\u5d29\u6e83\u4f46\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\u2014\u2014\u6bcf\u6b21\u8d4b\u503c\u90fd\u4f1a\u541e\u566c\u53ef\u7528\u7684\u5185\u5b58\u3002 \u66f4\u597d\u7684\u89e3\u51b3\u65b9\u6848 \u6807\u51c6\u5e93\u4e2d\u5904\u7406\u52a8\u6001\u5185\u5b58\u7684\u7c7b\uff0c\u5982 std::string \u548c std::vector \uff0c\u5b83\u4eec\u4f1a\u7ba1\u7406\u81ea\u5df1\u6240\u6709\u7684\u5185\u5b58\uff0c\u5e76\u5177\u6709\u6267\u884c\u6df1\u62f7\u8d1d\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002\u56e0\u6b64\uff0c\u4f60\u4e0d\u5fc5\u81ea\u5df1\u7ba1\u7406\u5b83\u4eec\u7684\u5185\u5b58\uff0c\u53ea\u9700\u50cf\u666e\u901a\u57fa\u672c\u53d8\u91cf\u4e00\u6837\u521d\u59cb\u5316\u6216\u5206\u914d\u5b83\u4eec\uff01\u8fd9\u4f7f\u5f97\u8fd9\u4e9b\u7c7b\u66f4\u5bb9\u6613\u4f7f\u7528\uff0c\u66f4\u4e0d\u5bb9\u6613\u51fa\u9519\uff0c\u800c\u4e14\u4f60\u4e0d\u5fc5\u82b1\u65f6\u95f4\u7f16\u5199\u81ea\u5df1\u7684\u91cd\u8f7d\u51fd\u6570! \u5c0f\u7ed3 \u9ed8\u8ba4\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u9ed8\u8ba4\u8d4b\u503c\u64cd\u4f5c\u7b26\u6267\u884c\u6d45\u590d\u5236\uff0c\u8fd9\u5bf9\u4e8e\u4e0d\u5305\u542b\u52a8\u6001\u5206\u914d\u53d8\u91cf\u7684\u7c7b\u6765\u8bf4\u6ca1\u6709\u95ee\u9898\uff1b \u5177\u6709\u52a8\u6001\u5206\u914d\u53d8\u91cf\u7684\u7c7b\u9700\u8981\u6709\u4e00\u4e2a\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\u6765\u6267\u884c\u6df1\u62f7\u8d1d\uff1b \u6700\u597d\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684\u7c7b\uff0c\u800c\u4e0d\u662f\u81ea\u5df1\u8fdb\u884c\u5185\u5b58\u7ba1\u7406\u3002","title":"14.16 - \u6d45\u62f7\u8d1d\u548c\u6df1\u62f7\u8d1d"},{"location":"14-16-shallow-vs-deep-copying/#_1","text":"\u56e0\u4e3a C++ \u5e76\u4e0d\u4e86\u89e3\u4f60\u7684\u7c7b\uff0c\u6240\u4ee5\u9ed8\u8ba4\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4ee5\u53ca\u9ed8\u8ba4\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u4f1a\u4f7f\u7528 \u6210\u5458\u4f9d\u6b21\u62f7\u8d1d \uff08\u4e5f\u79f0\u4e3a \u6d45\u62f7\u8d1d \uff09\u7684\u65b9\u5f0f\u4fe1\u606f\u62f7\u8d1d\u3002\u4e5f\u5c31\u662f\u8bf4\uff0cC++ \u4f1a\u4f9d\u6b21\u62f7\u8d1d\u7c7b\u7684\u6bcf\u4e00\u4e2a\u6210\u5458\uff08\u4f7f\u7528\u7b49\u53f7\u662f\u8c03\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\u3001\u5728\u4f7f\u7528\u62f7\u8d1d\u6784\u9020\u65f6\uff0c\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\uff09\u3002\u5bf9\u4e8e\u7b80\u5355\u7c7b\uff08\u4f8b\u5982\uff0c\u4e0d\u5305\u542b\u52a8\u6001\u5185\u5b58\u7684\u7c7b\uff09\uff0c\u8fd9\u4e48\u505a\u975e\u5e38\u5408\u9002\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include class Fraction { private : int m_numerator { 0 }; int m_denominator { 1 }; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator } , m_denominator { denominator } { assert ( denominator != 0 ); } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } \u8fd9\u91cc\u9ed8\u8ba4\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u9ed8\u8ba4\u8d4b\u503c\u8fd0\u7b97\u7b26\u5f62\u5f0f\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include #include class Fraction { private : int m_numerator { 0 }; int m_denominator { 1 }; public : // \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator } , m_denominator { denominator } { assert ( denominator != 0 ); } // \u9690\u5f0f\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u7684\u4e00\u79cd\u53ef\u80fd\u5b9e\u73b0 Fraction ( const Fraction & f ) : m_numerator { f . m_numerator } , m_denominator { f . m_denominator } { } // \u9690\u5f0f\u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u4e00\u79cd\u53ef\u80fd\u5b9e\u73b0 Fraction & operator = ( const Fraction & fraction ) { // \u907f\u514d\u81ea\u6211\u8d4b\u503c if ( this == & fraction ) return * this ; // \u62f7\u8d1d m_numerator = fraction . m_numerator ; m_denominator = fraction . m_denominator ; // \u8fd4\u56de\u5bf9\u8c61\u672c\u8eab\uff0c\u4f7f\u64cd\u4f5c\u7b26\u53ef\u4ee5\u94fe\u5f0f\u8c03\u7528 return * this ; } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } }; \u6ce8\u610f\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u9ed8\u8ba4\u7248\u672c\u7684\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u5f88\u597d\u5730\u5b8c\u6210\u62f7\u8d1d\uff0c\u6211\u4eec\u5b8c\u5168\u6ca1\u6709\u5fc5\u8981\u7f16\u5199\u81ea\u5df1\u7684\u7248\u672c\u3002 \u4f46\u662f\uff0c\u5728\u8bbe\u8ba1\u6301\u6709\u52a8\u6001\u5185\u5b58\u7684\u7c7b\u65f6\uff0c\u6d45\u62f7\u8d1d\u4f1a\u5e26\u6765\u5f88\u591a\u95ee\u9898\uff01\u56e0\u4e3a\u6d45\u62f7\u8d1d\u5728\u62f7\u8d1d\u6307\u9488\u65f6\uff0c\u53ea\u662f\u62f7\u8d1d\u4e86\u6307\u9488\u7684\u5730\u5740\u2014\u2014\u5b83\u5e76\u6ca1\u6709\u5206\u914d\u65b0\u7684\u5730\u5740\u6216\u62f7\u8d1d\u6307\u9488\u6240\u6307\u7684\u5185\u5bb9\u3002 \u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include // for strlen() #include // for assert() class MyString { private : char * m_data {}; int m_length {}; public : MyString ( const char * source = \"\" ) { assert ( source ); // make sure source isn't a null string // Find the length of the string // Plus one character for a terminator m_length = std :: strlen ( source ) + 1 ; // Allocate a buffer equal to this length m_data = new char [ m_length ]; // Copy the parameter string into our internal buffer for ( int i { 0 }; i < m_length ; ++ i ) m_data [ i ] = source [ i ]; } ~ MyString () // destructor { // We need to deallocate our string delete [] m_data ; } char * getString () { return m_data ; } int getLength () { return m_length ; } }; \u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u7c7b\uff0c\u5b83\u5206\u914d\u4e86\u4e00\u6bb5\u5185\u5b58\u7528\u4e8e\u4fdd\u5b58\u4f20\u5165\u7684\u5b57\u7b26\u4e32\u3002\u6ce8\u610f\uff0c\u6211\u4eec\u5e76\u6ca1\u6709\u5b9a\u4e49 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u6216\u8005\u91cd\u8f7d\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002\u56e0\u6b64\uff0cC++\u4f1a\u63d0\u4f9b\u4e00\u4e2a\u9ed8\u8ba4\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u9ed8\u8ba4\u8d4b\u503c\u8fd0\u7b97\u7b26\u4ee5\u4fbf\u6267\u884c\u6d45\u62f7\u8d1d\u3002\u5176\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u770b\u4e0a\u53bb\u5e94\u8be5\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 MyString :: MyString ( const MyString & source ) : m_length { source . m_length } , m_data { source . m_data } { } \u6ce8\u610f\uff0c m_data \u53ea\u662f source.m_data \u7684\u4e00\u4e2a\u6d45\u62f7\u8d1d\u6307\u9488\uff0c\u6240\u4ee5\u5b83\u4eec\u6307\u5411\u540c\u4e00\u5757\u5185\u5b58\u3002 \u73b0\u5728\uff0c \u8003\u8651\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { MyString hello { \"Hello, world!\" }; { MyString copy { hello }; // \u4f7f\u7528\u9ed8\u8ba4\u62f7\u8d1d\u6784\u9020\u51fd\u6570 } // copy \u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u5728\u6b64\u5904\u4f1a\u9500\u6bc1\u3002\u6240\u4ee5\u5176\u6790\u6784\u51fd\u6570UI\u5220\u9664copy\u6301\u6709\u7684\u5b57\u7b26\u4e32\uff0c\u6240\u4ee5hello\u6b64\u65f6\u6301\u6709\u4e00\u4e2a\u60ac\u5782\u6307\u9488 std :: cout << hello . getString () << '\\n' ; // \u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a return 0 ; } \u867d\u7136\u8fd9\u6bb5\u4ee3\u7801\u770b\u8d77\u6765\u6ca1\u5565\u95ee\u9898\uff0c\u4f46\u5b83\u5305\u542b\u4e00\u4e2a\u6f5c\u5728\u7684\u95ee\u9898\uff0c\u5c06\u5bfc\u81f4\u7a0b\u5e8f\u51fa\u73b0 \u672a\u5b9a\u4e49\u884c\u4e3a \uff01 \u8ba9\u6211\u4eec\u9010\u884c\u5206\u6790\u8fd9\u4e2a\u4f8b\u5b50: 1 MyString hello { \"Hello, world!\" }; \u8fd9\u4e00\u884c\u6ca1\u4ec0\u4e48\u95ee\u9898\u3002\u5b83\u8c03\u7528 MyString \u6784\u9020\u51fd\u6570\uff0c\u5206\u914d\u4e00\u6bb5\u5185\u5b58\uff0c\u7136\u540e\u8ba9 hello.m_data \u6307\u5411\u5b83\uff0c\u7136\u540e\u62f7\u8d1d\u5b57\u7b26\u4e32\u201cHello, world!\u201d \u5230\u8be5\u5185\u5b58\u3002 1 MyString copy { hello }; // \u4f7f\u7528\u9ed8\u8ba4\u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u8fd9\u4e00\u884c\u4ee3\u7801\u770b\u4e0a\u53bb\u4e5f\u4eba\u755c\u65e0\u5bb3\uff0c\u4f46\u662f\u5b83\u5176\u5b9e\u662f\u95ee\u9898\u7684\u6839\u6e90\uff01\u8fd9\u884c\u4ee3\u7801\u5728\u6c42\u503c\u65f6\uff0cC++\u4f1a\u4f7f\u7528\u9ed8\u8ba4\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff08\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u63d0\u4f9b\u81ea\u5df1\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff09\u3002\u8be5\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4f1a\u6267\u884c\u6d45\u62f7\u8d1d\uff0c\u5c06 copy.m_data \u521d\u59cb\u5316\u4e3a hello.m_data \u6301\u6709\u7684\u5730\u5740\u3002\u56e0\u6b64\uff0c copy.m_data \u548c hello.m_data \u6307\u5411\u4e00\u5757\u76f8\u540c\u7684\u5185\u5b58\uff01 1 } // copy \u5728\u6b64\u5904\u9500\u6bc1 \u5f53 copy \u79bb\u5f00\u4f5c\u7528\u57df \u65f6\uff0c MyString \u7684\u6790\u6784\u51fd\u6570\u4f1a\u88ab\u8c03\u7528\u3002\u6790\u6784\u51fd\u6570\u4f1a\u5220\u9664 copy.m_data \u548c hello.m_data \u6240\u6307\u7684\u90a3\u6bb5\u5185\u5b58\uff01\u56e0\u6b64\uff0c\u5f53 copy \u88ab\u5220\u9664\u65f6\uff0c\u5176\u6790\u6784\u8fc7\u7a0b\u4e0d\u53ef\u907f\u514d\u5730\u5f71\u54cd\u4e86 hello \u3002\u53d8\u91cf copy \u968f\u540e\u5c31\u88ab\u9500\u6bc1\u4e86\uff0c\u4f46\u662f hello.m_data \u4ecd\u7136\u6307\u5411\u8fd9\u5757\u88ab\u5220\u9664\u7684\u5185\u5b58\uff08\u975e\u6cd5\u5185\u5b58\uff09\uff01 1 std :: cout << hello . getString () << '\\n' ; // \u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a \u73b0\u5728\u4f60\u80fd\u660e\u767d\u4e3a\u4ec0\u4e48\u7a0b\u5e8f\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u4e86\u5427\u3002\u6211\u4eec\u5df2\u7ecf\u5220\u9664\u4e86 hello \u6240\u6307\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u53c8\u5c1d\u8bd5\u6253\u5370\u8fd9\u5757\u5185\u5b58\u7684\u5185\u5bb9\u3002 \u8fd9\u4e2a\u95ee\u9898\u7684\u6839\u6e90\u662f\u7531\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u6267\u884c\u7684\u6d45\u62f7\u8d1d\u5bfc\u81f4\u7684\u2014\u2014\u5728\u221a\u6784\u9020\u51fd\u6570\u6216\u91cd\u8f7d\u8d4b\u503c\u64cd\u4f5c\u7b26\u4e2d\u5bf9\u6307\u9488\u503c\u8fdb\u884c\u6d45\u51e0\u4e4e\u603b\u662f\u4f1a\u5e26\u6765\u9ebb\u70e6\u3002","title":"\u6d45\u62f7\u8d1d"},{"location":"14-16-shallow-vs-deep-copying/#_2","text":"\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\u7684\u529e\u6cd5\u662f\u5bf9\u4efb\u4f55\u975e\u7a7a\u6307\u9488\u8fdb\u884c\u6df1\u62f7\u8d1d\u3002 \u6df1\u62f7\u8d1d \u4f1a\u9996\u5148\u4e3a\u526f\u672c\u5206\u914d\u5185\u5b58\uff0c\u7136\u540e\u518d\u62f7\u8d1d\u5b9e\u9645\u7684\u503c\uff0c\u8fd9\u6837\u4e00\u6765\u526f\u672c\u6301\u6709\u7684\u5185\u5b58\u5c31\u548c\u6e90\u5185\u5b58\u4e0d\u540c\u4e86\u3002\u526f\u672c\u548c\u539f\u672c\u7531\u4e8e\u6301\u6709\u4e0d\u540c\u7684\u5185\u5b58\uff0c\u6240\u4ee5\u4fbf\u4e0d\u4f1a\u4e92\u76f8\u5f71\u54cd\u3002\u8981\u6267\u884c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u5219\u5fc5\u987b\u7f16\u5199\u6211\u4eec\u81ea\u5df1\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002 \u4ee5 MyString \u4e3a\u4e86\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u5982\u4f55\u8fdb\u884c\u6df1\u62f7\u8d1d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 // assumes m_data is initialized void MyString::deepCopy ( const MyString & source ) { // \u9996\u5148\u5e94\u8be5\u91ca\u653e\u5f53\u524d\u5b58\u653e\u7684\u5b57\u7b26\u4e32 delete [] m_data ; // \u56e0\u4e3a m_length \u4e0d\u662f\u6307\u9488\uff0c\u6240\u4ee5\u6d45\u62f7\u8d1d\u5373\u53ef m_length = source . m_length ; // m_data \u662f\u6307\u9488\uff0c\u6240\u4ee5\u5f53\u5b83\u4e0d\u662f\u7a7a\u6307\u9488\u662f\u5c31\u5fc5\u987b\u8fdb\u884c\u6df1\u62f7\u8d1d if ( source . m_data ) { // \u4e3a\u526f\u672c\u5206\u914d\u5185\u5b58 m_data = new char [ m_length ]; // \u62f7\u8d1d for ( int i { 0 }; i < m_length ; ++ i ) m_data [ i ] = source . m_data [ i ]; } else m_data = nullptr ; } // Copy constructor MyString :: MyString ( const MyString & source ) { deepCopy ( source ); } \u4ece\u4e0a\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u770b\u51fa\uff0c\u6df1\u62f7\u8d1d\u88ab\u6d45\u62f7\u8d1d\u8981\u590d\u6742\u4e0d\u5c11\uff01\u9996\u5148\uff0c\u6211\u4eec\u5fc5\u987b\u539f\u672c\u542b\u6709\u5b57\u7b26\u4e32\uff0811\u884c\uff09\u3002\u5982\u679c\u6709\u7684\u8bdd\uff0c\u5219\u5fc5\u987b\u4e3a\u526f\u672c\u5206\u914d\u8db3\u591f\u7684\u5185\u5b58\u7528\u4e8e\u4fdd\u5b58\u5b57\u7b26\u4e32\u7684\u526f\u672c\uff0814\u884c\uff09\u3002\u6700\u540e\uff0c\u6211\u4eec\u5fc5\u987b\u624b\u52a8\u62f7\u8d1d\u5b57\u7b26\u4e32\uff0817\u884c\u548c18\u884c\uff09\u3002 \u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 // Assignment operator MyString & MyString :: operator = ( const MyString & source ) { // check for self-assignment if ( this != & source ) { // now do the deep copy deepCopy ( source ); } return * this ; } \u6ce8\u610f\uff0c\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26\u548c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u975e\u5e38\u7c7b\u4f3c\uff0c\u4f46\u662f\u6709\u4e09\u5904\u660e\u663e\u7684\u4e0d\u540c\uff1a \u6dfb\u52a0\u4e86\u4e00\u4e2a\u81ea\u6211\u590d\u5236\u68c0\u67e5\uff1b \u8fd4\u56de *this \u6307\u9488\u4e00\u822c\u80fd\u591f\u94fe\u5f0f\u8c03\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\uff1b \u5fc5\u987b\u663e\u5f0f\u5730\u91ca\u653e\u5df2\u7ecf\u6301\u6709\u7684\u5b57\u7b26\u4e32\uff08\u907f\u514d m_data \u91cd\u65b0\u5206\u914d\u65f6\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\uff09 \u3002\u8be5\u64cd\u4f5c\u662f\u5728 deepCopy \u5728\u5b8c\u6210\u7684 \u5f53\u8c03\u7528\u91cd\u8f7d\u8d4b\u503c\u64cd\u4f5c\u7b26\u65f6\uff0c\u88ab\u8d4b\u503c\u7684\u9879\u53ef\u80fd\u5df2\u7ecf\u5305\u542b\u4e86\u4ee5\u524d\u7684\u503c\uff0c\u9700\u8981\u786e\u4fdd\u5728\u4e3a\u65b0\u503c\u5206\u914d\u5185\u5b58\u4e4b\u524d\u5c06\u5176\u6e05\u9664\u3002\u5bf9\u4e8e\u975e\u52a8\u6001\u5206\u914d\u7684\u53d8\u91cf(\u5927\u5c0f\u56fa\u5b9a)\uff0c\u6211\u4eec\u4e0d\u5fc5\u8d39\u5fc3\uff0c\u56e0\u4e3a\u65b0\u503c\u53ea\u662f\u8986\u76d6\u65e7\u503c\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u52a8\u6001\u5206\u914d\u7684\u53d8\u91cf\uff0c\u5728\u5206\u914d\u65b0\u5185\u5b58\u4e4b\u524d\u9700\u8981\u663e\u5f0f\u5730\u91ca\u653e\u65e7\u5185\u5b58\u3002\u5982\u679c\u6211\u4eec\u4e0d\u8fd9\u6837\u505a\uff0c\u4ee3\u7801\u4e0d\u4f1a\u5d29\u6e83\u4f46\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\u2014\u2014\u6bcf\u6b21\u8d4b\u503c\u90fd\u4f1a\u541e\u566c\u53ef\u7528\u7684\u5185\u5b58\u3002","title":"\u6df1\u62f7\u8d1d"},{"location":"14-16-shallow-vs-deep-copying/#_3","text":"\u6807\u51c6\u5e93\u4e2d\u5904\u7406\u52a8\u6001\u5185\u5b58\u7684\u7c7b\uff0c\u5982 std::string \u548c std::vector \uff0c\u5b83\u4eec\u4f1a\u7ba1\u7406\u81ea\u5df1\u6240\u6709\u7684\u5185\u5b58\uff0c\u5e76\u5177\u6709\u6267\u884c\u6df1\u62f7\u8d1d\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002\u56e0\u6b64\uff0c\u4f60\u4e0d\u5fc5\u81ea\u5df1\u7ba1\u7406\u5b83\u4eec\u7684\u5185\u5b58\uff0c\u53ea\u9700\u50cf\u666e\u901a\u57fa\u672c\u53d8\u91cf\u4e00\u6837\u521d\u59cb\u5316\u6216\u5206\u914d\u5b83\u4eec\uff01\u8fd9\u4f7f\u5f97\u8fd9\u4e9b\u7c7b\u66f4\u5bb9\u6613\u4f7f\u7528\uff0c\u66f4\u4e0d\u5bb9\u6613\u51fa\u9519\uff0c\u800c\u4e14\u4f60\u4e0d\u5fc5\u82b1\u65f6\u95f4\u7f16\u5199\u81ea\u5df1\u7684\u91cd\u8f7d\u51fd\u6570!","title":"\u66f4\u597d\u7684\u89e3\u51b3\u65b9\u6848"},{"location":"14-16-shallow-vs-deep-copying/#_4","text":"\u9ed8\u8ba4\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u9ed8\u8ba4\u8d4b\u503c\u64cd\u4f5c\u7b26\u6267\u884c\u6d45\u590d\u5236\uff0c\u8fd9\u5bf9\u4e8e\u4e0d\u5305\u542b\u52a8\u6001\u5206\u914d\u53d8\u91cf\u7684\u7c7b\u6765\u8bf4\u6ca1\u6709\u95ee\u9898\uff1b \u5177\u6709\u52a8\u6001\u5206\u914d\u53d8\u91cf\u7684\u7c7b\u9700\u8981\u6709\u4e00\u4e2a\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\u6765\u6267\u884c\u6df1\u62f7\u8d1d\uff1b \u6700\u597d\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684\u7c7b\uff0c\u800c\u4e0d\u662f\u81ea\u5df1\u8fdb\u884c\u5185\u5b58\u7ba1\u7406\u3002","title":"\u5c0f\u7ed3"},{"location":"14-17-overloading-operators-and-function-templates/","tags":["shallow copy","deep copy"],"text":"Key Takeaway - In lesson 8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316 \uff0cwe discussed how the compiler will use function templates to instantiate functions, which are then compiled. We also noted that these functions may not compile, if the code in the function template tries to perform some operation that the actual type doesn\u2019t support (such as adding integer value 1 to a std::string ). In this lesson, we\u2019ll take a look at a few examples where our instantiated functions won\u2019t compile because our actual class types don\u2019t support those operators, and show how we can define those operators so that the instantiated functions will then compile. Operators, function calls, and function templates First, let\u2019s create a simple class: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } friend std :: ostream & operator << ( std :: ostream & ostr , const Cents & c ) { ostr << c . m_cents ; return ostr ; } }; COPY and define a max function template: 1 2 3 4 5 template < typename T > const T & max ( T x , T y ) { return ( x < y ) ? y : x ; } COPY Now, let\u2019s see what happens when we try to call max() with object of type Cents : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } friend std :: ostream & operator << ( std :: ostream & ostr , const Cents & c ) { ostr << c . m_cents ; return ostr ; } }; template < typename T > const T & max ( const T & x , const T & y ) { return ( x < y ) ? y : x ; } int main () { Cents nickel { 5 }; Cents dime { 10 }; Cents bigger = max ( nickel , dime ); std :: cout << bigger << \" is bigger \\n \" ; return 0 ; } C++ will create a template instance for max() that looks like this: 1 2 3 4 5 template <> const Cents & max ( const Cents & x , const Cents & y ) { return ( x < y ) ? y : x ; } And then it will try to compile this function. See the problem here? C++ has no idea how to evaluate x < y when x and y are of type Cents ! Consequently, this will produce a compile error. To get around this problem, simply overload operator< for any class we wish to use max with: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } friend bool operator < ( const Cents & c1 , const Cents & c2 ) { return ( c1 . m_cents < c2 . m_cents ); } friend std :: ostream & operator << ( std :: ostream & ostr , const Cents & c ) { ostr << c . m_cents ; return ostr ; } }; template < typename T > const T & max ( const T & x , const T & y ) { return ( x < y ) ? y : x ; } int main () { Cents nickel { 5 }; Cents dime { 10 }; Cents bigger = max ( nickel , dime ); std :: cout << bigger << \" is bigger \\n \" ; return 0 ; } This works as expected, and prints: 1 10 is bigger Another example Let\u2019s do one more example of a function template not working because of missing overloaded operators. The following function template will calculate the average of a number of objects in an array: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include template < typename T > T average ( const T * myArray , int numValues ) { T sum { 0 }; for ( int count { 0 }; count < numValues ; ++ count ) sum += myArray [ count ]; sum /= numValues ; return sum ; } int main () { int intArray [] { 5 , 3 , 2 , 1 , 4 }; std :: cout << average ( intArray , 5 ) << '\\n' ; double doubleArray [] { 3.12 , 3.45 , 9.23 , 6.34 }; std :: cout << average ( doubleArray , 4 ) << '\\n' ; return 0 ; } COPY This produces the values: 1 2 3 5.535 As you can see, it works great for built-in types! Now let\u2019s see what happens when we call this function on our Cents class: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include template < typename T > T average ( const T * myArray , int numValues ) { T sum { 0 }; for ( int count { 0 }; count < numValues ; ++ count ) sum += myArray [ count ]; sum /= numValues ; return sum ; } class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } }; int main () { Cents centsArray [] { Cents { 5 }, Cents { 10 }, Cents { 15 }, Cents { 14 } }; std :: cout << average ( centsArray , 4 ) << '\\n' ; return 0 ; } The compiler goes berserk and produces a ton of error messages! The first error message will be something like this: 1 error C2679: binary << : no operator found which takes a right-hand operand of type Cents (or there is no acceptable conversion) Remember that average() returns a Cents object, and we are trying to stream that object to std::cout using operator<< . However, we haven\u2019t defined the operator<< for our Cents class yet. Let\u2019s do that: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include template < typename T > T average ( const T * myArray , int numValues ) { T sum { 0 }; for ( int count { 0 }; count < numValues ; ++ count ) sum += myArray [ count ]; sum /= numValues ; return sum ; } class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } friend std :: ostream & operator << ( std :: ostream & out , const Cents & cents ) { out << cents . m_cents << \" cents \" ; return out ; } }; int main () { Cents centsArray [] { Cents { 5 }, Cents { 10 }, Cents { 15 }, Cents { 14 } }; std :: cout << average ( centsArray , 4 ) << '\\n' ; return 0 ; } If we compile again, we will get another error: 1 error C2676: binary += : Cents does not define this operator or a conversion to a type acceptable to the predefined operator This error is actually being caused by the function template instance created when we call average(const Cents*, int) . Remember that when we call a templated function, the compiler \u201cstencils\u201d out a copy of the function where the template type parameters (the placeholder types) have been replaced with the actual types in the function call. Here is the function template instance for average() when T is a Cents object: 1 2 3 4 5 6 7 8 9 10 template <> Cents average ( const Cents * myArray , int numValues ) { Cents sum { 0 }; for ( int count { 0 }; count < numValues ; ++ count ) sum += myArray [ count ]; sum /= numValues ; return sum ; } COPY The reason we are getting an error message is because of the following line: 1 sum += myArray [ count ]; COPY In this case, sum is a Cents object, but we have not defined operator+= for Cents objects! We will need to define this function in order for average() to be able to work with Cents . Looking forward, we can see that average() also uses the operator/= , so we will go ahead and define that as well: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include template < typename T > T average ( const T * myArray , int numValues ) { T sum { 0 }; for ( int count { 0 }; count < numValues ; ++ count ) sum += myArray [ count ]; sum /= numValues ; return sum ; } class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } friend std :: ostream & operator << ( std :: ostream & out , const Cents & cents ) { out << cents . m_cents << \" cents \" ; return out ; } Cents & operator += ( const Cents & cents ) { m_cents += cents . m_cents ; return * this ; } Cents & operator /= ( int x ) { m_cents /= x ; return * this ; } }; int main () { Cents centsArray [] { Cents { 5 }, Cents { 10 }, Cents { 15 }, Cents { 14 } }; std :: cout << average ( centsArray , 4 ) << '\\n' ; return 0 ; } COPY Finally, our code will compile and run! Here is the result: 1 11 cents Note that we didn\u2019t have to modify average() at all to make it work with objects of type Cents . We simply had to define the operators used to implement average() for the Cents class, and the compiler took care of the rest!","title":"14.17 - \u91cd\u8f7d\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u6a21\u677f"},{"location":"14-17-overloading-operators-and-function-templates/#another-example","text":"Let\u2019s do one more example of a function template not working because of missing overloaded operators. The following function template will calculate the average of a number of objects in an array: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include template < typename T > T average ( const T * myArray , int numValues ) { T sum { 0 }; for ( int count { 0 }; count < numValues ; ++ count ) sum += myArray [ count ]; sum /= numValues ; return sum ; } int main () { int intArray [] { 5 , 3 , 2 , 1 , 4 }; std :: cout << average ( intArray , 5 ) << '\\n' ; double doubleArray [] { 3.12 , 3.45 , 9.23 , 6.34 }; std :: cout << average ( doubleArray , 4 ) << '\\n' ; return 0 ; } COPY This produces the values: 1 2 3 5.535 As you can see, it works great for built-in types! Now let\u2019s see what happens when we call this function on our Cents class: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include template < typename T > T average ( const T * myArray , int numValues ) { T sum { 0 }; for ( int count { 0 }; count < numValues ; ++ count ) sum += myArray [ count ]; sum /= numValues ; return sum ; } class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } }; int main () { Cents centsArray [] { Cents { 5 }, Cents { 10 }, Cents { 15 }, Cents { 14 } }; std :: cout << average ( centsArray , 4 ) << '\\n' ; return 0 ; } The compiler goes berserk and produces a ton of error messages! The first error message will be something like this: 1 error C2679: binary << : no operator found which takes a right-hand operand of type Cents (or there is no acceptable conversion) Remember that average() returns a Cents object, and we are trying to stream that object to std::cout using operator<< . However, we haven\u2019t defined the operator<< for our Cents class yet. Let\u2019s do that: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include template < typename T > T average ( const T * myArray , int numValues ) { T sum { 0 }; for ( int count { 0 }; count < numValues ; ++ count ) sum += myArray [ count ]; sum /= numValues ; return sum ; } class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } friend std :: ostream & operator << ( std :: ostream & out , const Cents & cents ) { out << cents . m_cents << \" cents \" ; return out ; } }; int main () { Cents centsArray [] { Cents { 5 }, Cents { 10 }, Cents { 15 }, Cents { 14 } }; std :: cout << average ( centsArray , 4 ) << '\\n' ; return 0 ; } If we compile again, we will get another error: 1 error C2676: binary += : Cents does not define this operator or a conversion to a type acceptable to the predefined operator This error is actually being caused by the function template instance created when we call average(const Cents*, int) . Remember that when we call a templated function, the compiler \u201cstencils\u201d out a copy of the function where the template type parameters (the placeholder types) have been replaced with the actual types in the function call. Here is the function template instance for average() when T is a Cents object: 1 2 3 4 5 6 7 8 9 10 template <> Cents average ( const Cents * myArray , int numValues ) { Cents sum { 0 }; for ( int count { 0 }; count < numValues ; ++ count ) sum += myArray [ count ]; sum /= numValues ; return sum ; } COPY The reason we are getting an error message is because of the following line: 1 sum += myArray [ count ]; COPY In this case, sum is a Cents object, but we have not defined operator+= for Cents objects! We will need to define this function in order for average() to be able to work with Cents . Looking forward, we can see that average() also uses the operator/= , so we will go ahead and define that as well: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include template < typename T > T average ( const T * myArray , int numValues ) { T sum { 0 }; for ( int count { 0 }; count < numValues ; ++ count ) sum += myArray [ count ]; sum /= numValues ; return sum ; } class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } friend std :: ostream & operator << ( std :: ostream & out , const Cents & cents ) { out << cents . m_cents << \" cents \" ; return out ; } Cents & operator += ( const Cents & cents ) { m_cents += cents . m_cents ; return * this ; } Cents & operator /= ( int x ) { m_cents /= x ; return * this ; } }; int main () { Cents centsArray [] { Cents { 5 }, Cents { 10 }, Cents { 15 }, Cents { 14 } }; std :: cout << average ( centsArray , 4 ) << '\\n' ; return 0 ; } COPY Finally, our code will compile and run! Here is the result: 1 11 cents Note that we didn\u2019t have to modify average() at all to make it work with objects of type Cents . We simply had to define the operators used to implement average() for the Cents class, and the compiler took care of the rest!","title":"Another example"},{"location":"14-2-overloading-the-arithmetic-operators-using-friend-functions/","tags":["overload","friend","operator"],"text":"Key Takeaway Some of the most commonly used operators in C++ are the arithmetic operators -- that is, the plus operator (+), minus operator (-), multiplication operator (*), and division operator (/). Note that all of the arithmetic operators are binary operators -- meaning they take two operands -- one on each side of the operator. All four of these operators are overloaded in the exact same way. It turns out that there are three different ways to overload operators: the member function way, the friend function way, and the normal function way. In this lesson, we\u2019ll cover the friend function way (because it\u2019s more intuitive for most binary operators). Next lesson, we\u2019ll discuss the normal function way. Finally, in a later lesson in this chapter, we\u2019ll cover the member function way. And, of course, we\u2019ll also summarize when to use each in more detail. Overloading operators using friend functions Consider the following trivial class: 1 2 3 4 5 6 7 8 9 class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } int getCents () const { return m_cents ; } }; COPY The following example shows how to overload operator plus (+) in order to add two \u201cCents\u201d objects together: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } // add Cents + Cents using a friend function friend Cents operator + ( const Cents & c1 , const Cents & c2 ); int getCents () const { return m_cents ; } }; // note: this function is not a member function! Cents operator + ( const Cents & c1 , const Cents & c2 ) { // use the Cents constructor and operator+(int, int) // we can access m_cents directly because this is a friend function return Cents { c1 . m_cents + c2 . m_cents }; } int main () { Cents cents1 { 6 }; Cents cents2 { 8 }; Cents centsSum { cents1 + cents2 }; std :: cout << \"I have \" << centsSum . getCents () << \" cents. \\n \" ; return 0 ; } COPY This produces the result: I have 14 cents. Overloading the plus operator (+) is as simple as declaring a function named operator+, giving it two parameters of the type of the operands we want to add, picking an appropriate return type, and then writing the function. In the case of our Cents object, implementing our operator+() function is very simple. First, the parameter types: in this version of operator+, we are going to add two Cents objects together, so our function will take two objects of type Cents. Second, the return type: our operator+ is going to return a result of type Cents, so that\u2019s our return type. Finally, implementation: to add two Cents objects together, we really need to add the m_cents member from each Cents object. Because our overloaded operator+() function is a friend of the class, we can access the m_cents member of our parameters directly. Also, because m_cents is an integer, and C++ knows how to add integers together using the built-in version of the plus operator that works with integer operands, we can simply use the + operator to do the adding. Overloading the subtraction operator (-) is simple as well: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } // add Cents + Cents using a friend function friend Cents operator + ( const Cents & c1 , const Cents & c2 ); // subtract Cents - Cents using a friend function friend Cents operator - ( const Cents & c1 , const Cents & c2 ); int getCents () const { return m_cents ; } }; // note: this function is not a member function! Cents operator + ( const Cents & c1 , const Cents & c2 ) { // use the Cents constructor and operator+(int, int) // we can access m_cents directly because this is a friend function return Cents { c1 . m_cents + c2 . m_cents }; } // note: this function is not a member function! Cents operator - ( const Cents & c1 , const Cents & c2 ) { // use the Cents constructor and operator-(int, int) // we can access m_cents directly because this is a friend function return Cents ( c1 . m_cents - c2 . m_cents ); } int main () { Cents cents1 { 6 }; Cents cents2 { 2 }; Cents centsSum { cents1 - cents2 }; std :: cout << \"I have \" << centsSum . getCents () << \" cents. \\n \" ; return 0 ; } COPY Overloading the multiplication operator ( ) and the division operator (/) is as easy as defining functions for operator and operator/ respectively. Friend functions can be defined inside the class Even though friend functions are not members of the class, they can still be defined inside the class if desired: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } // add Cents + Cents using a friend function // This function is not considered a member of the class, even though the definition is inside the class friend Cents operator + ( const Cents & c1 , const Cents & c2 ) { // use the Cents constructor and operator+(int, int) // we can access m_cents directly because this is a friend function return Cents { c1 . m_cents + c2 . m_cents }; } int getCents () const { return m_cents ; } }; int main () { Cents cents1 { 6 }; Cents cents2 { 8 }; Cents centsSum { cents1 + cents2 }; std :: cout << \"I have \" << centsSum . getCents () << \" cents. \\n \" ; return 0 ; } COPY We generally don\u2019t recommend this, as non-trivial function definitions are better kept in a separate .cpp file, outside of the class definition. However, we will use this pattern in future tutorials to keep the examples concise. Overloading operators for operands of different types Often it is the case that you want your overloaded operators to work with operands that are different types. For example, if we have Cents(4), we may want to add the integer 6 to this to produce the result Cents(10). When C++ evaluates the expression x + y , x becomes the first parameter, and y becomes the second parameter. When x and y have the same type, it does not matter if you add x + y or y + x -- either way, the same version of operator+ gets called. However, when the operands have different types, x + y does not call the same function as y + x. For example, Cents(4) + 6 would call operator+(Cents, int), and 6 + Cents(4) would call operator+(int, Cents). Consequently, whenever we overload binary operators for operands of different types, we actually need to write two functions -- one for each case. Here is an example of that: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } // add Cents + int using a friend function friend Cents operator + ( const Cents & c1 , int value ); // add int + Cents using a friend function friend Cents operator + ( int value , const Cents & c1 ); int getCents () const { return m_cents ; } }; // note: this function is not a member function! Cents operator + ( const Cents & c1 , int value ) { // use the Cents constructor and operator+(int, int) // we can access m_cents directly because this is a friend function return { c1 . m_cents + value }; } // note: this function is not a member function! Cents operator + ( int value , const Cents & c1 ) { // use the Cents constructor and operator+(int, int) // we can access m_cents directly because this is a friend function return { c1 . m_cents + value }; } int main () { Cents c1 { Cents { 4 } + 6 }; Cents c2 { 6 + Cents { 4 } }; std :: cout << \"I have \" << c1 . getCents () << \" cents. \\n \" ; std :: cout << \"I have \" << c2 . getCents () << \" cents. \\n \" ; return 0 ; } COPY Note that both overloaded functions have the same implementation -- that\u2019s because they do the same thing, they just take their parameters in a different order. Another example Let\u2019s take a look at another example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 #include class MinMax { private : int m_min {}; // The min value seen so far int m_max {}; // The max value seen so far public : MinMax ( int min , int max ) : m_min { min }, m_max { max } { } int getMin () const { return m_min ; } int getMax () const { return m_max ; } friend MinMax operator + ( const MinMax & m1 , const MinMax & m2 ); friend MinMax operator + ( const MinMax & m , int value ); friend MinMax operator + ( int value , const MinMax & m ); }; MinMax operator + ( const MinMax & m1 , const MinMax & m2 ) { // Get the minimum value seen in m1 and m2 int min { m1 . m_min < m2 . m_min ? m1 . m_min : m2 . m_min }; // Get the maximum value seen in m1 and m2 int max { m1 . m_max > m2 . m_max ? m1 . m_max : m2 . m_max }; return { min , max }; } MinMax operator + ( const MinMax & m , int value ) { // Get the minimum value seen in m and value int min { m . m_min < value ? m . m_min : value }; // Get the maximum value seen in m and value int max { m . m_max > value ? m . m_max : value }; return { min , max }; } MinMax operator + ( int value , const MinMax & m ) { // call operator+(MinMax, int) return { m + value }; } int main () { MinMax m1 { 10 , 15 }; MinMax m2 { 8 , 11 }; MinMax m3 { 3 , 12 }; MinMax mFinal { m1 + m2 + 5 + 8 + m3 + 16 }; std :: cout << \"Result: (\" << mFinal . getMin () << \", \" << mFinal . getMax () << \") \\n \" ; return 0 ; } COPY The MinMax class keeps track of the minimum and maximum values that it has seen so far. We have overloaded the + operator 3 times, so that we can add two MinMax objects together, or add integers to MinMax objects. This example produces the result: Result: (3, 16) which you will note is the minimum and maximum values that we added to mFinal. Let\u2019s talk a little bit more about how \u201cMinMax mFinal { m1 + m2 + 5 + 8 + m3 + 16 }\u201d evaluates. Remember that operator+ evaluates from left to right, so m1 + m2 evaluates first. This becomes a call to operator+(m1, m2), which produces the return value MinMax(8, 15). Then MinMax(8, 15) + 5 evaluates next. This becomes a call to operator+(MinMax(8, 15), 5), which produces return value MinMax(5, 15). Then MinMax(5, 15) + 8 evaluates in the same way to produce MinMax(5, 15). Then MinMax(5, 15) + m3 evaluates to produce MinMax(3, 15). And finally, MinMax(3, 15) + 16 evaluates to MinMax(3, 16). This final result is then used to initialize mFinal. In other words, this expression evaluates as \u201cMinMax mFinal = (((((m1 + m2) + 5) + 8) + m3) + 16)\u201d, with each successive operation returning a MinMax object that becomes the left-hand operand for the following operator. Implementing operators using other operators In the above example, note that we defined operator+(int, MinMax) by calling operator+(MinMax, int) (which produces the same result). This allows us to reduce the implementation of operator+(int, MinMax) to a single line, making our code easier to maintain by minimizing redundancy and making the function simpler to understand. It is often possible to define overloaded operators by calling other overloaded operators. You should do so if and when doing so produces simpler code. In cases where the implementation is trivial (e.g. a single line) it may or may not be worth doing this.","title":"14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26"},{"location":"14-3-overloading-operators-using-normal-functions/","tags":["overload","operator"],"text":"Key Takeaway In the previous lesson, we overloaded operator+ as a friend function: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} // add Cents + Cents using a friend function friend Cents operator + ( const Cents & c1 , const Cents & c2 ); int getCents () const { return m_cents ; } }; // note: this function is not a member function! Cents operator + ( const Cents & c1 , const Cents & c2 ) { // use the Cents constructor and operator+(int, int) // we can access m_cents directly because this is a friend function return { c1 . m_cents + c2 . m_cents }; } int main () { Cents cents1 { 6 }; Cents cents2 { 8 }; Cents centsSum { cents1 + cents2 }; std :: cout << \"I have \" << centsSum . getCents () << \" cents. \\n \" ; return 0 ; } COPY Using a friend function to overload an operator is convenient because it gives you direct access to the internal members of the classes you\u2019re operating on. In the initial Cents example above, our friend function version of operator+ accessed member variable m_cents directly. However, if you don\u2019t need that access, you can write your overloaded operators as normal functions. Note that the Cents class above contains an access function (getCents()) that allows us to get at m_cents without having to have direct access to private members. Because of this, we can write our overloaded operator+ as a non-friend: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; // note: this function is not a member function nor a friend function! Cents operator + ( const Cents & c1 , const Cents & c2 ) { // use the Cents constructor and operator+(int, int) // we don't need direct access to private members here return Cents { c1 . getCents () + c2 . getCents () }; } int main () { Cents cents1 { 6 }; Cents cents2 { 8 }; Cents centsSum { cents1 + cents2 }; std :: cout << \"I have \" << centsSum . getCents () << \" cents. \\n \" ; return 0 ; } COPY Because the normal and friend functions work almost identically (they just have different levels of access to private members), we generally won\u2019t differentiate them. The one difference is that the friend function declaration inside the class serves as a prototype as well. With the normal function version, you\u2019ll have to provide your own function prototype. Cents.h: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #ifndef CENTS_H #define CENTS_H class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; // Need to explicitly provide prototype for operator+ so uses of operator+ in other files know this overload exists Cents operator + ( const Cents & c1 , const Cents & c2 ); #endif COPY Cents.cpp: 1 2 3 4 5 6 7 8 9 #include \"Cents.h\" // note: this function is not a member function nor a friend function! Cents operator + ( const Cents & c1 , const Cents & c2 ) { // use the Cents constructor and operator+(int, int) // we don't need direct access to private members here return { c1 . getCents () + c2 . getCents () }; } COPY main.cpp: 1 2 3 4 5 6 7 8 9 10 11 12 #include \"Cents.h\" #include int main () { Cents cents1 { 6 }; Cents cents2 { 8 }; Cents centsSum { cents1 + cents2 }; // without the prototype in Cents.h, this would fail to compile std :: cout << \"I have \" << centsSum . getCents () << \" cents. \\n \" ; return 0 ; } COPY In general, a normal function should be preferred over a friend function if it\u2019s possible to do so with the existing member functions available (the less functions touching your classes\u2019s internals, the better). However, don\u2019t add additional access functions just to overload an operator as a normal function instead of a friend function! Best practice Prefer overloading operators as normal functions instead of friends if it\u2019s possible to do so without adding additional functions.","title":"14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26"},{"location":"14-4-overloading-the-IO-operators/","tags":["overload","operator"],"text":"Key Takeaway For classes that have multiple member variables, printing each of the individual variables on the screen can get tiresome fast. For example, consider the following class: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Point { private : double m_x {}; double m_y {}; double m_z {}; public : Point ( double x = 0.0 , double y = 0.0 , double z = 0.0 ) : m_x { x }, m_y { y }, m_z { z } { } double getX () const { return m_x ; } double getY () const { return m_y ; } double getZ () const { return m_z ; } }; COPY If you wanted to print an instance of this class to the screen, you\u2019d have to do something like this: 1 2 3 4 5 Point point { 5.0 , 6.0 , 7.0 }; std :: cout << \"Point(\" << point . getX () << \", \" << point . getY () << \", \" << point . getZ () << ')' ; COPY Of course, it makes more sense to do this as a reusable function. And in previous examples, you\u2019ve seen us create print() functions that work like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 class Point { private : double m_x {}; double m_y {}; double m_z {}; public : Point ( double x = 0.0 , double y = 0.0 , double z = 0.0 ) : m_x { x }, m_y { y }, m_z { z } { } double getX () const { return m_x ; } double getY () const { return m_y ; } double getZ () const { return m_z ; } void print () const { std :: cout << \"Point(\" << m_x << \", \" << m_y << \", \" << m_z << ')' ; } }; COPY While this is much better, it still has some downsides. Because print() returns void, it can\u2019t be called in the middle of an output statement. Instead, you have to do this: 1 2 3 4 5 6 7 8 int main () { const Point point { 5.0 , 6.0 , 7.0 }; std :: cout << \"My point is: \" ; point . print (); std :: cout << \" in Cartesian space. \\n \" ; } COPY It would be much easier if you could simply type: 1 2 Point point { 5.0 , 6.0 , 7.0 }; cout << \"My point is: \" << point << \" in Cartesian space. \\n \" ; COPY and get the same result. There would be no breaking up output across multiple statements, and no having to remember what you named the print function. Fortunately, by overloading the << operator, you can! Overloading operator<< Overloading operator<< is similar to overloading operator+ (they are both binary operators), except that the parameter types are different. Consider the expression std::cout << point . If the operator is <<, what are the operands? The left operand is the std::cout object, and the right operand is your Point class object. std::cout is actually an object of type std::ostream. Therefore, our overloaded function will look like this: 1 2 // std::ostream is the type for object std::cout friend std :: ostream & operator << ( std :: ostream & out , const Point & point ); COPY Implementation of operator<< for our Point class is fairly straightforward -- because C++ already knows how to output doubles using operator<<, and our members are all doubles, we can simply use operator<< to output the member variables of our Point. Here is the above Point class with the overloaded operator<<. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include class Point { private : double m_x {}; double m_y {}; double m_z {}; public : Point ( double x = 0.0 , double y = 0.0 , double z = 0.0 ) : m_x { x }, m_y { y }, m_z { z } { } friend std :: ostream & operator << ( std :: ostream & out , const Point & point ); }; std :: ostream & operator << ( std :: ostream & out , const Point & point ) { // Since operator<< is a friend of the Point class, we can access Point's members directly. out << \"Point(\" << point . m_x << \", \" << point . m_y << \", \" << point . m_z << ')' ; // actual output done here return out ; // return std::ostream so we can chain calls to operator<< } int main () { const Point point1 { 2.0 , 3.0 , 4.0 }; std :: cout << point1 << '\\n' ; return 0 ; } COPY This is pretty straightforward -- note how similar our output line is to the line in the print() function we wrote previously. The most notable difference is that std::cout has become parameter out (which will be a reference to std::cout when the function is called). The trickiest part here is the return type. With the arithmetic operators, we calculated and returned a single answer by value (because we were creating and returning a new result). However, if you try to return std::ostream by value, you\u2019ll get a compiler error. This happens because std::ostream specifically disallows being copied. In this case, we return the left hand parameter as a reference. This not only prevents a copy of std::ostream from being made, it also allows us to \u201cchain\u201d output commands together, such as std::cout << point << std::endl; You might have initially thought that since operator<< doesn\u2019t return a value to the caller, we should define the function as returning void. But consider what would happen if our operator<< returned void. When the compiler evaluates std::cout << point << '\\n' , due to the precedence/associativity rules, it evaluates this expression as (std::cout << point) << '\\n'; . std::cout << point would call our void-returning overloaded operator<< function, which returns void. Then the partially evaluated expression becomes: void << '\\n'; , which makes no sense! By returning the out parameter as the return type instead, (std::cout<< point) returns std::cout. Then our partially evaluated expression becomes: std::cout << '\\n'; , which then gets evaluated itself! Any time we want our overloaded binary operators to be chainable in such a manner, the left operand should be returned (by reference). Returning the left-hand parameter by reference is okay in this case -- since the left-hand parameter was passed in by the calling function, it must still exist when the called function returns. Therefore, we don\u2019t have to worry about referencing something that will go out of scope and get destroyed when the operator returns. Just to prove it works, consider the following example, which uses the Point class with the overloaded operator<< we wrote above: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include class Point { private : double m_x {}; double m_y {}; double m_z {}; public : Point ( double x = 0.0 , double y = 0.0 , double z = 0.0 ) : m_x { x }, m_y { y }, m_z { z } { } friend std :: ostream & operator << ( std :: ostream & out , const Point & point ); }; std :: ostream & operator << ( std :: ostream & out , const Point & point ) { // Since operator<< is a friend of the Point class, we can access Point's members directly. out << \"Point(\" << point . m_x << \", \" << point . m_y << \", \" << point . m_z << ')' ; return out ; } int main () { Point point1 { 2.0 , 3.5 , 4.0 }; Point point2 { 6.0 , 7.5 , 8.0 }; std :: cout << point1 << ' ' << point2 << '\\n' ; return 0 ; } COPY This produces the following result: Point(2, 3.5, 4) Point(6, 7.5, 8) Overloading operator>> It is also possible to overload the input operator. This is done in a manner analogous to overloading the output operator. The key thing you need to know is that std::cin is an object of type std::istream. Here\u2019s our Point class with an overloaded operator>>: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include class Point { private : double m_x {}; double m_y {}; double m_z {}; public : Point ( double x = 0.0 , double y = 0.0 , double z = 0.0 ) : m_x { x }, m_y { y }, m_z { z } { } friend std :: ostream & operator << ( std :: ostream & out , const Point & point ); friend std :: istream & operator >> ( std :: istream & in , Point & point ); }; std :: ostream & operator << ( std :: ostream & out , const Point & point ) { // Since operator<< is a friend of the Point class, we can access Point's members directly. out << \"Point(\" << point . m_x << \", \" << point . m_y << \", \" << point . m_z << ')' ; return out ; } std :: istream & operator >> ( std :: istream & in , Point & point ) { // Since operator>> is a friend of the Point class, we can access Point's members directly. // note that parameter point must be non-const so we can modify the class members with the input values in >> point . m_x ; in >> point . m_y ; in >> point . m_z ; return in ; } COPY Here\u2019s a sample program using both the overloaded operator<< and operator>>: 1 2 3 4 5 6 7 8 9 10 11 int main () { std :: cout << \"Enter a point: \" ; Point point ; std :: cin >> point ; std :: cout << \"You entered: \" << point << '\\n' ; return 0 ; } COPY Assuming the user enters 3.0 4.5 7.26 as input, the program produces the following result: You entered: Point(3, 4.5, 7.26) Conclusion Overloading operator<< and operator>> make it extremely easy to output your class to screen and accept user input from the console.","title":"14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26"},{"location":"14-5-overloading-operators-using-member-functions/","tags":["overload","operator"],"text":"Key Takeaway In lesson 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 , you learned how to overload the arithmetic operators using friend functions. You also learned you can overload operators as normal functions. Many operators can be overloaded in a different way: as a member function. Overloading operators using a member function is very similar to overloading operators using a friend function. When overloading an operator using a member function: The overloaded operator must be added as a member function of the left operand. The left operand becomes the implicit *this object All other operands become function parameters. As a reminder, here\u2019s how we overloaded operator+ using a friend function: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } // Overload Cents + int friend Cents operator + ( const Cents & cents , int value ); int getCents () const { return m_cents ; } }; // note: this function is not a member function! Cents operator + ( const Cents & cents , int value ) { return Cents ( cents . m_cents + value ); } int main () { Cents cents1 { 6 }; Cents cents2 { cents1 + 2 }; std :: cout << \"I have \" << cents2 . getCents () << \" cents. \\n \" ; return 0 ; } COPY Converting a friend overloaded operator to a member overloaded operator is easy: The overloaded operator is defined as a member instead of a friend (Cents::operator+ instead of friend operator+) The left parameter is removed, because that parameter now becomes the implicit *this object. Inside the function body, all references to the left parameter can be removed (e.g. cents.m_cents becomes m_cents, which implicitly references the *this object). Now, the same operator overloaded using the member function method: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } // Overload Cents + int Cents operator + ( int value ); int getCents () const { return m_cents ; } }; // note: this function is a member function! // the cents parameter in the friend version is now the implicit *this parameter Cents Cents :: operator + ( int value ) { return Cents { m_cents + value }; } int main () { Cents cents1 { 6 }; Cents cents2 { cents1 + 2 }; std :: cout << \"I have \" << cents2 . getCents () << \" cents. \\n \" ; return 0 ; } COPY Note that the usage of the operator does not change (in both cases, cents1 + 2 ), we\u2019ve simply defined the function differently. Our two-parameter friend function becomes a one-parameter member function, with the leftmost parameter in the friend version (cents) becoming the implicit *this parameter in the member function version. Let\u2019s take a closer look at how the expression cents1 + 2 evaluates. In the friend function version, the expression cents1 + 2 becomes function call operator+(cents1, 2). Note that there are two function parameters. This is straightforward. In the member function version, the expression cents1 + 2 becomes function call cents1.operator+(2) . Note that there is now only one explicit function parameter, and cents1 has become an object prefix. However, in lesson 13.10 -- The hidden \u201cthis\u201d pointer , we mentioned that the compiler implicitly converts an object prefix into a hidden leftmost parameter named *this. So in actuality, cents1.operator+(2) becomes operator+(\u00a2s1, 2) , which is almost identical to the friend version. Both cases produce the same result, just in slightly different ways. So if we can overload an operator as a friend or a member, which should we use? In order to answer that question, there\u2019s a few more things you\u2019ll need to know. Not everything can be overloaded as a friend function The assignment (=), subscript ([]), function call (()), and member selection (->) operators must be overloaded as member functions, because the language requires them to be. Not everything can be overloaded as a member function In lesson 14.4 -- Overloading the I/O operators , we overloaded operator<< for our Point class using the friend function method. Here\u2019s a reminder of how we did that: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include class Point { private : double m_x {}, m_y {}, m_z {}; public : Point ( double x = 0.0 , double y = 0.0 , double z = 0.0 ) : m_x { x }, m_y { y }, m_z { z } { } friend std :: ostream & operator << ( std :: ostream & out , const Point & point ); }; std :: ostream & operator << ( std :: ostream & out , const Point & point ) { // Since operator<< is a friend of the Point class, we can access Point's members directly. out << \"Point(\" << point . m_x << \", \" << point . m_y << \", \" << point . m_z << \")\" ; return out ; } int main () { Point point1 { 2.0 , 3.0 , 4.0 }; std :: cout << point1 ; return 0 ; } COPY However, we are not able to overload operator<< as a member function. Why not? Because the overloaded operator must be added as a member of the left operand. In this case, the left operand is an object of type std::ostream. std::ostream is fixed as part of the standard library. We can\u2019t modify the class declaration to add the overload as a member function of std::ostream. This necessitates that operator<< be overloaded as a normal function (preferred) or a friend. Similarly, although we can overload operator+(Cents, int) as a member function (as we did above), we can\u2019t overload operator+(int, Cents) as a member function, because int isn\u2019t a class we can add members to. Typically, we won\u2019t be able to use a member overload if the left operand is either not a class (e.g. int), or it is a class that we can\u2019t modify (e.g. std::ostream). When to use a normal, friend, or member function overload In most cases, the language leaves it up to you to determine whether you want to use the normal/friend or member function version of the overload. However, one of the two is usually a better choice than the other. When dealing with binary operators that don\u2019t modify the left operand (e.g. operator+), the normal or friend function version is typically preferred, because it works for all parameter types (even when the left operand isn\u2019t a class object, or is a class that is not modifiable). The normal or friend function version has the added benefit of \u201csymmetry\u201d, as all operands become explicit parameters (instead of the left operand becoming *this and the right operand becoming an explicit parameter). When dealing with binary operators that do modify the left operand (e.g. operator+=), the member function version is typically preferred. In these cases, the leftmost operand will always be a class type, and having the object being modified become the one pointed to by *this is natural. Because the rightmost operand becomes an explicit parameter, there\u2019s no confusion over who is getting modified and who is getting evaluated. Unary operators are usually overloaded as member functions as well, since the member version has no parameters. The following rules of thumb can help you determine which form is best for a given situation: If you\u2019re overloading assignment (=), subscript ([]), function call (()), or member selection (->), do so as a member function. If you\u2019re overloading a unary operator, do so as a member function. If you\u2019re overloading a binary operator that does not modify its left operand (e.g. operator+), do so as a normal function (preferred) or friend function. If you\u2019re overloading a binary operator that modifies its left operand, but you can\u2019t add members to the class definition of the left operand (e.g. operator<<, which has a left operand of type ostream), do so as a normal function (preferred) or friend function. If you\u2019re overloading a binary operator that modifies its left operand (e.g. operator+=), and you can modify the definition of the left operand, do so as a member function. [ ](https://www.learncpp.com/cpp-tutorial/overloading-unary-operators/)","title":"14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26"},{"location":"14-6-overloading-unary-operators-%2B--and-%21/","tags":["overload","unary","operator"],"text":"Key Takeaway Overloading unary operators Unlike the operators you\u2019ve seen so far, the positive (+), negative (-) and logical not (!) operators all are unary operators, which means they only operate on one operand. Because they only operate on the object they are applied to, typically unary operator overloads are implemented as member functions. All three operands are implemented in an identical manner. Let\u2019s take a look at how we\u2019d implement operator- on the Cents class we used in a previous example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} // Overload -Cents as a member function Cents operator - () const ; int getCents () const { return m_cents ; } }; // note: this function is a member function! Cents Cents :: operator - () const { return - m_cents ; // since return type is a Cents, this does an implicit conversion from int to Cents using the Cents(int) constructor } int main () { const Cents nickle { 5 }; std :: cout << \"A nickle of debt is worth \" << ( - nickle ). getCents () << \" cents \\n \" ; return 0 ; } COPY This should be straightforward. Our overloaded negative operator (-) is a unary operator implemented as a member function, so it takes no parameters (it operates on the *this object). It returns a Cents object that is the negation of the original Cents value. Because operator- does not modify the Cents object, we can (and should) make it a const function (so it can be called on const Cents objects). Note that there\u2019s no confusion between the negative operator- and the minus operator- since they have a different number of parameters. Here\u2019s another example. The ! operator is the logical negation operator -- if an expression evaluates to \u201ctrue\u201d, operator! will return false, and vice-versa. We commonly see this applied to boolean variables to test whether they are true or not: 1 2 3 4 if ( ! isHappy ) std :: cout << \"I am not happy! \\n \" ; else std :: cout << \"I am so happy! \\n \" ; COPY For integers, 0 evaluates to false, and anything else to true, so operator! as applied to integers will return true for an integer value of 0 and false otherwise. Extending the concept, we can say that operator! should evaluate to true if the state of the object is \u201cfalse\u201d, \u201czero\u201d, or whatever the default initialization state is. The following example shows an overload of both operator- and operator! for a user-defined Point class: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include class Point { private : double m_x {}; double m_y {}; double m_z {}; public : Point ( double x = 0.0 , double y = 0.0 , double z = 0.0 ) : m_x { x }, m_y { y }, m_z { z } { } // Convert a Point into its negative equivalent Point operator - () const ; // Return true if the point is set at the origin bool operator ! () const ; double getX () const { return m_x ; } double getY () const { return m_y ; } double getZ () const { return m_z ; } }; // Convert a Point into its negative equivalent Point Point :: operator - () const { return { - m_x , - m_y , - m_z }; } // Return true if the point is set at the origin, false otherwise bool Point :: operator ! () const { return ( m_x == 0.0 && m_y == 0.0 && m_z == 0.0 ); } int main () { Point point {}; // use default constructor to set to (0.0, 0.0, 0.0) if ( ! point ) std :: cout << \"point is set at the origin. \\n \" ; else std :: cout << \"point is not set at the origin. \\n \" ; return 0 ; } COPY The overloaded operator! for this class returns the Boolean value \u201ctrue\u201d if the Point is set to the default value at coordinate (0.0, 0.0, 0.0). Thus, the above code produces the result: point is set at the origin.","title":"14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c !"},{"location":"14-7-overloading-the-comparison-operators/","tags":["overload","unary","operator"],"text":"Key Takeaway In lesson 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 we discussed the six comparison operators. Overloading these comparison operators is comparatively simple (see what I did there?), as they follow the same patterns as we\u2019ve seen in overloading other operators. Because the comparison operators are all binary operators that do not modify their left operands, we will make our overloaded comparison operators friend functions. Here\u2019s an example Car class with an overloaded operator== and operator!=. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #include #include #include class Car { private : std :: string m_make ; std :: string m_model ; public : Car ( std :: string_view make , std :: string_view model ) : m_make { make }, m_model { model } { } friend bool operator == ( const Car & c1 , const Car & c2 ); friend bool operator != ( const Car & c1 , const Car & c2 ); }; bool operator == ( const Car & c1 , const Car & c2 ) { return ( c1 . m_make == c2 . m_make && c1 . m_model == c2 . m_model ); } bool operator != ( const Car & c1 , const Car & c2 ) { return ( c1 . m_make != c2 . m_make || c1 . m_model != c2 . m_model ); } int main () { Car corolla { \"Toyota\" , \"Corolla\" }; Car camry { \"Toyota\" , \"Camry\" }; if ( corolla == camry ) std :: cout << \"a Corolla and Camry are the same. \\n \" ; if ( corolla != camry ) std :: cout << \"a Corolla and Camry are not the same. \\n \" ; return 0 ; } COPY The code here should be straightforward. What about operator< and operator>? What would it mean for a Car to be greater or less than another Car? We typically don\u2019t think about cars this way. Since the results of operator< and operator> would not be immediately intuitive, it may be better to leave these operators undefined. \u6700\u4f73\u5b9e\u8df5 Only define overloaded operators that make intuitive sense for your class. However, there is one common exception to the above recommendation. What if we wanted to sort a list of Cars? In such a case, we might want to overload the comparison operators to return the member (or members) you\u2019re most likely to want to sort on. For example, an overloaded operator< for Cars might sort based on make and model alphabetically. Some of the container classes in the standard library (classes that hold sets of other classes) require an overloaded operator< so they can keep the elements sorted. Here\u2019s a different example overloading all 6 logical comparison operators: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 #include class Cents { private : int m_cents ; public : Cents ( int cents ) : m_cents { cents } {} friend bool operator == ( const Cents & c1 , const Cents & c2 ); friend bool operator != ( const Cents & c1 , const Cents & c2 ); friend bool operator < ( const Cents & c1 , const Cents & c2 ); friend bool operator > ( const Cents & c1 , const Cents & c2 ); friend bool operator <= ( const Cents & c1 , const Cents & c2 ); friend bool operator >= ( const Cents & c1 , const Cents & c2 ); }; bool operator == ( const Cents & c1 , const Cents & c2 ) { return c1 . m_cents == c2 . m_cents ; } bool operator != ( const Cents & c1 , const Cents & c2 ) { return c1 . m_cents != c2 . m_cents ; } bool operator < ( const Cents & c1 , const Cents & c2 ) { return c1 . m_cents < c2 . m_cents ; } bool operator > ( const Cents & c1 , const Cents & c2 ) { return c1 . m_cents > c2 . m_cents ; } bool operator <= ( const Cents & c1 , const Cents & c2 ) { return c1 . m_cents <= c2 . m_cents ; } bool operator >= ( const Cents & c1 , const Cents & c2 ) { return c1 . m_cents >= c2 . m_cents ; } int main () { Cents dime { 10 }; Cents nickel { 5 }; if ( nickel > dime ) std :: cout << \"a nickel is greater than a dime. \\n \" ; if ( nickel >= dime ) std :: cout << \"a nickel is greater than or equal to a dime. \\n \" ; if ( nickel < dime ) std :: cout << \"a dime is greater than a nickel. \\n \" ; if ( nickel <= dime ) std :: cout << \"a dime is greater than or equal to a nickel. \\n \" ; if ( nickel == dime ) std :: cout << \"a dime is equal to a nickel. \\n \" ; if ( nickel != dime ) std :: cout << \"a dime is not equal to a nickel. \\n \" ; return 0 ; } COPY This is also pretty straightforward. Minimizing comparative redundancy In the example above, note how similar the implementation of each of the overloaded comparison operators are. Overloaded comparison operators tend to have a high degree of redundancy, and the more complex the implementation, the more redundancy there will be. Fortunately, many of the comparison operators can be implemented using the other comparison operators: operator!= can be implemented as !(operator==) operator> can be implemented as operator< with the order of the parameters flipped operator>= can be implemented as !(operator<) operator<= can be implemented as !(operator>) This means that we only need to implement logic for operator== and operator<, and then the other four comparison operators can be defined in terms of those two! Here\u2019s an updated Cents example illustrating this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 #include class Cents { private : int m_cents ; public : Cents ( int cents ) : m_cents { cents } {} friend bool operator == ( const Cents & c1 , const Cents & c2 ); friend bool operator != ( const Cents & c1 , const Cents & c2 ); friend bool operator < ( const Cents & c1 , const Cents & c2 ); friend bool operator > ( const Cents & c1 , const Cents & c2 ); friend bool operator <= ( const Cents & c1 , const Cents & c2 ); friend bool operator >= ( const Cents & c1 , const Cents & c2 ); }; bool operator == ( const Cents & c1 , const Cents & c2 ) { return c1 . m_cents == c2 . m_cents ; } bool operator != ( const Cents & c1 , const Cents & c2 ) { return ! ( operator == ( c1 , c2 )); } bool operator < ( const Cents & c1 , const Cents & c2 ) { return c1 . m_cents < c2 . m_cents ; } bool operator > ( const Cents & c1 , const Cents & c2 ) { return operator < ( c2 , c1 ); } bool operator <= ( const Cents & c1 , const Cents & c2 ) { return ! ( operator > ( c1 , c2 )); } bool operator >= ( const Cents & c1 , const Cents & c2 ) { return ! ( operator < ( c1 , c2 )); } int main () { Cents dime { 10 }; Cents nickel { 5 }; if ( nickel > dime ) std :: cout << \"a nickel is greater than a dime. \\n \" ; if ( nickel >= dime ) std :: cout << \"a nickel is greater than or equal to a dime. \\n \" ; if ( nickel < dime ) std :: cout << \"a dime is greater than a nickel. \\n \" ; if ( nickel <= dime ) std :: cout << \"a dime is greater than or equal to a nickel. \\n \" ; if ( nickel == dime ) std :: cout << \"a dime is equal to a nickel. \\n \" ; if ( nickel != dime ) std :: cout << \"a dime is not equal to a nickel. \\n \" ; return 0 ; } COPY This way, if we ever need to change something, we only need to update operator== and operator< instead of all six comparison operators!","title":"14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26"},{"location":"14-7-overloading-the-comparison-operators/#minimizing-comparative-redundancy","text":"In the example above, note how similar the implementation of each of the overloaded comparison operators are. Overloaded comparison operators tend to have a high degree of redundancy, and the more complex the implementation, the more redundancy there will be. Fortunately, many of the comparison operators can be implemented using the other comparison operators: operator!= can be implemented as !(operator==) operator> can be implemented as operator< with the order of the parameters flipped operator>= can be implemented as !(operator<) operator<= can be implemented as !(operator>) This means that we only need to implement logic for operator== and operator<, and then the other four comparison operators can be defined in terms of those two! Here\u2019s an updated Cents example illustrating this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 #include class Cents { private : int m_cents ; public : Cents ( int cents ) : m_cents { cents } {} friend bool operator == ( const Cents & c1 , const Cents & c2 ); friend bool operator != ( const Cents & c1 , const Cents & c2 ); friend bool operator < ( const Cents & c1 , const Cents & c2 ); friend bool operator > ( const Cents & c1 , const Cents & c2 ); friend bool operator <= ( const Cents & c1 , const Cents & c2 ); friend bool operator >= ( const Cents & c1 , const Cents & c2 ); }; bool operator == ( const Cents & c1 , const Cents & c2 ) { return c1 . m_cents == c2 . m_cents ; } bool operator != ( const Cents & c1 , const Cents & c2 ) { return ! ( operator == ( c1 , c2 )); } bool operator < ( const Cents & c1 , const Cents & c2 ) { return c1 . m_cents < c2 . m_cents ; } bool operator > ( const Cents & c1 , const Cents & c2 ) { return operator < ( c2 , c1 ); } bool operator <= ( const Cents & c1 , const Cents & c2 ) { return ! ( operator > ( c1 , c2 )); } bool operator >= ( const Cents & c1 , const Cents & c2 ) { return ! ( operator < ( c1 , c2 )); } int main () { Cents dime { 10 }; Cents nickel { 5 }; if ( nickel > dime ) std :: cout << \"a nickel is greater than a dime. \\n \" ; if ( nickel >= dime ) std :: cout << \"a nickel is greater than or equal to a dime. \\n \" ; if ( nickel < dime ) std :: cout << \"a dime is greater than a nickel. \\n \" ; if ( nickel <= dime ) std :: cout << \"a dime is greater than or equal to a nickel. \\n \" ; if ( nickel == dime ) std :: cout << \"a dime is equal to a nickel. \\n \" ; if ( nickel != dime ) std :: cout << \"a dime is not equal to a nickel. \\n \" ; return 0 ; } COPY This way, if we ever need to change something, we only need to update operator== and operator< instead of all six comparison operators!","title":"Minimizing comparative redundancy"},{"location":"14-8-overloading-the-increment-and-decrement-operators/","tags":["overload","operator"],"text":"Key Takeaway Overloading the increment ( ++ ) and decrement ( -- ) operators is pretty straightforward, with one small exception. There are actually two versions of the increment and decrement operators: a prefix increment and decrement (e.g. ++x; --y; ) and a postfix increment and decrement (e.g. x++; y--; ). Because the increment and decrement operators are both unary operators and they modify their operands, they\u2019re best overloaded as member functions. We\u2019ll tackle the prefix versions first because they\u2019re the most straightforward. Overloading prefix increment and decrement** Prefix increment and decrement are overloaded exactly the same as any normal unary operator. We\u2019ll do this one by example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 #include class Digit { private : int m_digit ; public : Digit ( int digit = 0 ) : m_digit { digit } { } Digit & operator ++ (); Digit & operator -- (); friend std :: ostream & operator << ( std :: ostream & out , const Digit & d ); }; Digit & Digit :: operator ++ () { // If our number is already at 9, wrap around to 0 if ( m_digit == 9 ) m_digit = 0 ; // otherwise just increment to next number else ++ m_digit ; return * this ; } Digit & Digit :: operator -- () { // If our number is already at 0, wrap around to 9 if ( m_digit == 0 ) m_digit = 9 ; // otherwise just decrement to next number else -- m_digit ; return * this ; } std :: ostream & operator << ( std :: ostream & out , const Digit & d ) { out << d . m_digit ; return out ; } int main () { Digit digit ( 8 ); std :: cout << digit ; std :: cout << ++ digit ; std :: cout << ++ digit ; std :: cout << -- digit ; std :: cout << -- digit ; return 0 ; } COPY Our Digit class holds a number between 0 and 9. We\u2019ve overloaded increment and decrement so they increment/decrement the digit, wrapping around if the digit is incremented/decremented out range. This example prints: 1 89098 Note that we return *this. The overloaded increment and decrement operators return the current implicit object so multiple operators can be \u201cchained\u201d together. Overloading postfix increment and decrement** Normally, functions can be overloaded when they have the same name but a different number and/or different type of parameters. However, consider the case of the prefix and postfix increment and decrement operators. Both have the same name (eg. operator++), are unary, and take one parameter of the same type. So how it is possible to differentiate the two when overloading? The C++ language specification has a special case that provides the answer: the compiler looks to see if the overloaded operator has an int parameter. If the overloaded operator has an int parameter, the operator is a postfix overload. If the overloaded operator has no parameter, the operator is a prefix overload. Here is the above Digit class with both prefix and postfix overloads: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 class Digit { private : int m_digit ; public : Digit ( int digit = 0 ) : m_digit { digit } { } Digit & operator ++ (); // prefix has no parameter Digit & operator -- (); // prefix has no parameter Digit operator ++ ( int ); // postfix has an int parameter Digit operator -- ( int ); // postfix has an int parameter friend std :: ostream & operator << ( std :: ostream & out , const Digit & d ); }; // No parameter means this is prefix operator++ Digit & Digit :: operator ++ () { // If our number is already at 9, wrap around to 0 if ( m_digit == 9 ) m_digit = 0 ; // otherwise just increment to next number else ++ m_digit ; return * this ; } // No parameter means this is prefix operator-- Digit & Digit :: operator -- () { // If our number is already at 0, wrap around to 9 if ( m_digit == 0 ) m_digit = 9 ; // otherwise just decrement to next number else -- m_digit ; return * this ; } // int parameter means this is postfix operator++ Digit Digit :: operator ++ ( int ) { // Create a temporary variable with our current digit Digit temp { * this }; // Use prefix operator to increment this digit ++ ( * this ); // apply operator // return temporary result return temp ; // return saved state } // int parameter means this is postfix operator-- Digit Digit :: operator -- ( int ) { // Create a temporary variable with our current digit Digit temp { * this }; // Use prefix operator to decrement this digit -- ( * this ); // apply operator // return temporary result return temp ; // return saved state } std :: ostream & operator << ( std :: ostream & out , const Digit & d ) { out << d . m_digit ; return out ; } int main () { Digit digit ( 5 ); std :: cout << digit ; std :: cout << ++ digit ; // calls Digit::operator++(); std :: cout << digit ++ ; // calls Digit::operator++(int); std :: cout << digit ; std :: cout << -- digit ; // calls Digit::operator--(); std :: cout << digit -- ; // calls Digit::operator--(int); std :: cout << digit ; return 0 ; } COPY This prints 1 5667665 There are a few interesting things going on here. First, note that we\u2019ve distinguished the prefix from the postfix operators by providing an integer dummy parameter on the postfix version. Second, because the dummy parameter is not used in the function implementation, we have not even given it a name. This tells the compiler to treat this variable as a placeholder, which means it won\u2019t warn us that we declared a variable but never used it. Third, note that the prefix and postfix operators do the same job -- they both increment or decrement the object. The difference between the two is in the value they return. The overloaded prefix operators return the object after it has been incremented or decremented. Consequently, overloading these is fairly straightforward. We simply increment or decrement our member variables, and then return *this. The postfix operators, on the other hand, need to return the state of the object before it is incremented or decremented. This leads to a bit of a conundrum -- if we increment or decrement the object, we won\u2019t be able to return the state of the object before it was incremented or decremented. On the other hand, if we return the state of the object before we increment or decrement it, the increment or decrement will never be called. The typical way this problem is solved is to use a temporary variable that holds the value of the object before it is incremented or decremented. Then the object itself can be incremented or decremented. And finally, the temporary variable is returned to the caller. In this way, the caller receives a copy of the object before it was incremented or decremented, but the object itself is incremented or decremented. Note that this means the return value of the overloaded operator must be a non-reference, because we can\u2019t return a reference to a local variable that will be destroyed when the function exits. Also note that this means the postfix operators are typically less efficient than the prefix operators because of the added overhead of instantiating a temporary variable and returning by value instead of reference. Finally, note that we\u2019ve written the post-increment and post-decrement in such a way that it calls the pre-increment and pre-decrement to do most of the work. This cuts down on duplicate code, and makes our class easier to modify in the future.","title":"14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26"},{"location":"14-8-overloading-the-increment-and-decrement-operators/#overloading-prefix-increment-and-decrement","text":"Prefix increment and decrement are overloaded exactly the same as any normal unary operator. We\u2019ll do this one by example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 #include class Digit { private : int m_digit ; public : Digit ( int digit = 0 ) : m_digit { digit } { } Digit & operator ++ (); Digit & operator -- (); friend std :: ostream & operator << ( std :: ostream & out , const Digit & d ); }; Digit & Digit :: operator ++ () { // If our number is already at 9, wrap around to 0 if ( m_digit == 9 ) m_digit = 0 ; // otherwise just increment to next number else ++ m_digit ; return * this ; } Digit & Digit :: operator -- () { // If our number is already at 0, wrap around to 9 if ( m_digit == 0 ) m_digit = 9 ; // otherwise just decrement to next number else -- m_digit ; return * this ; } std :: ostream & operator << ( std :: ostream & out , const Digit & d ) { out << d . m_digit ; return out ; } int main () { Digit digit ( 8 ); std :: cout << digit ; std :: cout << ++ digit ; std :: cout << ++ digit ; std :: cout << -- digit ; std :: cout << -- digit ; return 0 ; } COPY Our Digit class holds a number between 0 and 9. We\u2019ve overloaded increment and decrement so they increment/decrement the digit, wrapping around if the digit is incremented/decremented out range. This example prints: 1 89098 Note that we return *this. The overloaded increment and decrement operators return the current implicit object so multiple operators can be \u201cchained\u201d together.","title":"Overloading prefix increment and decrement**"},{"location":"14-8-overloading-the-increment-and-decrement-operators/#overloading-postfix-increment-and-decrement","text":"Normally, functions can be overloaded when they have the same name but a different number and/or different type of parameters. However, consider the case of the prefix and postfix increment and decrement operators. Both have the same name (eg. operator++), are unary, and take one parameter of the same type. So how it is possible to differentiate the two when overloading? The C++ language specification has a special case that provides the answer: the compiler looks to see if the overloaded operator has an int parameter. If the overloaded operator has an int parameter, the operator is a postfix overload. If the overloaded operator has no parameter, the operator is a prefix overload. Here is the above Digit class with both prefix and postfix overloads: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 class Digit { private : int m_digit ; public : Digit ( int digit = 0 ) : m_digit { digit } { } Digit & operator ++ (); // prefix has no parameter Digit & operator -- (); // prefix has no parameter Digit operator ++ ( int ); // postfix has an int parameter Digit operator -- ( int ); // postfix has an int parameter friend std :: ostream & operator << ( std :: ostream & out , const Digit & d ); }; // No parameter means this is prefix operator++ Digit & Digit :: operator ++ () { // If our number is already at 9, wrap around to 0 if ( m_digit == 9 ) m_digit = 0 ; // otherwise just increment to next number else ++ m_digit ; return * this ; } // No parameter means this is prefix operator-- Digit & Digit :: operator -- () { // If our number is already at 0, wrap around to 9 if ( m_digit == 0 ) m_digit = 9 ; // otherwise just decrement to next number else -- m_digit ; return * this ; } // int parameter means this is postfix operator++ Digit Digit :: operator ++ ( int ) { // Create a temporary variable with our current digit Digit temp { * this }; // Use prefix operator to increment this digit ++ ( * this ); // apply operator // return temporary result return temp ; // return saved state } // int parameter means this is postfix operator-- Digit Digit :: operator -- ( int ) { // Create a temporary variable with our current digit Digit temp { * this }; // Use prefix operator to decrement this digit -- ( * this ); // apply operator // return temporary result return temp ; // return saved state } std :: ostream & operator << ( std :: ostream & out , const Digit & d ) { out << d . m_digit ; return out ; } int main () { Digit digit ( 5 ); std :: cout << digit ; std :: cout << ++ digit ; // calls Digit::operator++(); std :: cout << digit ++ ; // calls Digit::operator++(int); std :: cout << digit ; std :: cout << -- digit ; // calls Digit::operator--(); std :: cout << digit -- ; // calls Digit::operator--(int); std :: cout << digit ; return 0 ; } COPY This prints 1 5667665 There are a few interesting things going on here. First, note that we\u2019ve distinguished the prefix from the postfix operators by providing an integer dummy parameter on the postfix version. Second, because the dummy parameter is not used in the function implementation, we have not even given it a name. This tells the compiler to treat this variable as a placeholder, which means it won\u2019t warn us that we declared a variable but never used it. Third, note that the prefix and postfix operators do the same job -- they both increment or decrement the object. The difference between the two is in the value they return. The overloaded prefix operators return the object after it has been incremented or decremented. Consequently, overloading these is fairly straightforward. We simply increment or decrement our member variables, and then return *this. The postfix operators, on the other hand, need to return the state of the object before it is incremented or decremented. This leads to a bit of a conundrum -- if we increment or decrement the object, we won\u2019t be able to return the state of the object before it was incremented or decremented. On the other hand, if we return the state of the object before we increment or decrement it, the increment or decrement will never be called. The typical way this problem is solved is to use a temporary variable that holds the value of the object before it is incremented or decremented. Then the object itself can be incremented or decremented. And finally, the temporary variable is returned to the caller. In this way, the caller receives a copy of the object before it was incremented or decremented, but the object itself is incremented or decremented. Note that this means the return value of the overloaded operator must be a non-reference, because we can\u2019t return a reference to a local variable that will be destroyed when the function exits. Also note that this means the postfix operators are typically less efficient than the prefix operators because of the added overhead of instantiating a temporary variable and returning by value instead of reference. Finally, note that we\u2019ve written the post-increment and post-decrement in such a way that it calls the pre-increment and pre-decrement to do most of the work. This cuts down on duplicate code, and makes our class easier to modify in the future.","title":"Overloading postfix increment and decrement**"},{"location":"14-9-overloading-the-subscript-operator/","tags":["overload","operator","subscript"],"text":"Key Takeaway When working with arrays, we typically use the subscript operator ([]) to index specific elements of an array: 1 myArray [ 0 ] = 7 ; // put the value 7 in the first element of the array COPY However, consider the following IntList class, which has a member variable that is an array: 1 2 3 4 5 6 7 8 9 10 11 12 class IntList { private : int m_list [ 10 ]{}; }; int main () { IntList list {}; // how do we access elements from m_list? return 0 ; } COPY Because the m_list member variable is private, we can not access it directly from variable list. This means we have no way to directly get or set values in the m_list array. So how do we get or put elements into our list? Without operator overloading, the typical method would be to create access functions: 1 2 3 4 5 6 7 8 9 class IntList { private : int m_list [ 10 ]{}; public : void setItem ( int index , int value ) { m_list [ index ] = value ; } int getItem ( int index ) const { return m_list [ index ]; } }; COPY While this works, it\u2019s not particularly user friendly. Consider the following example: 1 2 3 4 5 6 7 int main () { IntList list {}; list . setItem ( 2 , 3 ); return 0 ; } COPY Are we setting element 2 to the value 3, or element 3 to the value 2? Without seeing the definition of setItem() , it\u2019s simply not clear. You could also just return the entire list and use operator[] to access the element: 1 2 3 4 5 6 7 8 class IntList { private : int m_list [ 10 ]{}; public : int * getList () { return m_list ; } }; COPY While this also works, it\u2019s syntactically odd: 1 2 3 4 5 6 7 int main () { IntList list {}; list . getList ()[ 2 ] = 3 ; return 0 ; } COPY Overloading operator[] However, a better solution in this case is to overload the subscript operator ([]) to allow access to the elements of m_list. The subscript operator is one of the operators that must be overloaded as a member function. An overloaded operator[] function will always take one parameter: the subscript that the user places between the hard braces. In our IntList case, we expect the user to pass in an integer index, and we\u2019ll return an integer value back as a result. 1 2 3 4 5 6 7 8 9 10 11 12 13 class IntList { private : int m_list [ 10 ]{}; public : int & operator [] ( int index ); }; int & IntList :: operator [] ( int index ) { return m_list [ index ]; } COPY Now, whenever we use the subscript operator ([]) on an object of our class, the compiler will return the corresponding element from the m_list member variable! This allows us to both get and set values of m_list directly: 1 2 3 4 5 IntList list {}; list [ 2 ] = 3 ; // set a value std :: cout << list [ 2 ] << '\\n' ; // get a value return 0 ; COPY This is both easy syntactically and from a comprehension standpoint. When list[2] evaluates, the compiler first checks to see if there\u2019s an overloaded operator[] function. If so, it passes the value inside the hard braces (in this case, 2) as an argument to the function. Note that although you can provide a default value for the function parameter, actually using operator[] without a subscript inside is not considered a valid syntax, so there\u2019s no point. Tip C++23 will add support for overloading operator[] with multiple subscripts. Why operator[] returns a reference Let\u2019s take a closer look at how list[2] = 3 evaluates. Because the subscript operator has a higher precedence than the assignment operator, list[2] evaluates first. list[2] calls operator[], which we\u2019ve defined to return a reference to list.m_list[2] . Because operator[] is returning a reference, it returns the actual list.m_list[2] array element. Our partially evaluated expression becomes list.m_list[2] = 3 , which is a straightforward integer assignment. In lesson 9.2 -- Value categories (lvalues and rvalues) , you learned that any value on the left hand side of an assignment statement must be an l-value (which is a variable that has an actual memory address). Because the result of operator[] can be used on the left hand side of an assignment (e.g. list[2] = 3 ), the return value of operator[] must be an l-value. As it turns out, references are always l-values, because you can only take a reference of variables that have memory addresses. So by returning a reference, the compiler is satisfied that we are returning an l-value. Consider what would happen if operator[] returned an integer by value instead of by reference. list[2] would call operator[], which would return the value of list.m_list[2]. For example, if m_list[2] had the value of 6, operator[] would return the value 6. list[2] = 3 would partially evaluate to 6 = 3 , which makes no sense! If you try to do this, the C++ compiler will complain: C:VCProjectsTest.cpp(386) : error C2106: '=' : left operand must be l-value Dealing with const objects In the above IntList example, operator[] is non-const, and we can use it as an l-value to change the state of non-const objects. However, what if our IntList object was const? In this case, we wouldn\u2019t be able to call the non-const version of operator[] because that would allow us to potentially change the state of a const object. The good news is that we can define a non-const and a const version of operator[] separately. The non-const version will be used with non-const objects, and the const version with const-objects. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include class IntList { private : int m_list [ 10 ]{ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 }; // give this class some initial state for this example public : int & operator [] ( int index ); int operator [] ( int index ) const ; // could also return const int& if returning a non-fundamental type }; int & IntList :: operator [] ( int index ) // for non-const objects: can be used for assignment { return m_list [ index ]; } int IntList :: operator [] ( int index ) const // for const objects: can only be used for access { return m_list [ index ]; } int main () { IntList list {}; list [ 2 ] = 3 ; // okay: calls non-const version of operator[] std :: cout << list [ 2 ] << '\\n' ; const IntList clist {}; clist [ 2 ] = 3 ; // compile error: calls const version of operator[], which returns by value. Cannot assign to this because it is an rvalue. std :: cout << clist [ 2 ] << '\\n' ; return 0 ; } COPY If we comment out the line clist[2] = 3 , the above program compiles and executes as expected. Error checking One other advantage of overloading the subscript operator is that we can make it safer than accessing arrays directly. Normally, when accessing arrays, the subscript operator does not check whether the index is valid. For example, the compiler will not complain about the following code: 1 2 int list [ 5 ]{}; list [ 7 ] = 3 ; // index 7 is out of bounds! COPY However, if we know the size of our array, we can make our overloaded subscript operator check to ensure the index is within bounds: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include // for assert() #include // for std::size() class IntList { private : int m_list [ 10 ]{}; public : int & operator [] ( int index ); }; int & IntList :: operator [] ( int index ) { assert ( index >= 0 && index < std :: size ( m_list )); return m_list [ index ]; } COPY In the above example, we have used the assert() function (included in the cassert header) to make sure our index is valid. If the expression inside the assert evaluates to false (which means the user passed in an invalid index), the program will terminate with an error message, which is much better than the alternative (corrupting memory). This is probably the most common method of doing error checking of this sort. Pointers to objects and overloaded operator[] don\u2019t mix If you try to call operator[] on a pointer to an object, C++ will assume you\u2019re trying to index an array of objects of that type. Consider the following example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include // for assert() #include // for std::size() class IntList { private : int m_list [ 10 ]{}; public : int & operator [] ( int index ); }; int & IntList :: operator [] ( int index ) { assert ( index >= 0 && index < std :: size ( m_list )); return m_list [ index ]; } int main () { IntList * list { new IntList {} }; list [ 2 ] = 3 ; // error: this will assume we're accessing index 2 of an array of IntLists delete list ; return 0 ; } COPY Because we can\u2019t assign an integer to an IntList, this won\u2019t compile. However, if assigning an integer was valid, this would compile and run, with undefined results. Rule Make sure you\u2019re not trying to call an overloaded operator[] on a pointer to an object. The proper syntax would be to dereference the pointer first (making sure to use parenthesis since operator[] has higher precedence than operator*), then call operator[]: 1 2 3 4 5 6 7 8 int main () { IntList * list { new IntList {} }; ( * list )[ 2 ] = 3 ; // get our IntList object, then call overloaded operator[] delete list ; return 0 ; } COPY This is ugly and error prone. Better yet, don\u2019t set pointers to your objects if you don\u2019t have to. The function parameter does not need to be an integer As mentioned above, C++ passes what the user types between the hard braces as an argument to the overloaded function. In most cases, this will be an integer value. However, this is not required -- and in fact, you can define that your overloaded operator[] take a value of any type you desire. You could define your overloaded operator[] to take a double, a std::string, or whatever else you like. As a ridiculous example, just so you can see that it works: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include // C++17 class Stupid { private : public : void operator [] ( std :: string_view index ); }; // It doesn't make sense to overload operator[] to print something // but it is the easiest way to show that the function parameter can be a non-integer void Stupid :: operator [] ( std :: string_view index ) { std :: cout << index ; } int main () { Stupid stupid {}; stupid [ \"Hello, world!\" ]; return 0 ; } COPY As you would expect, this prints: Hello, world! Overloading operator[] to take a std::string parameter can be useful when writing certain kinds of classes, such as those that use words as indices. Conclusion The subscript operator is typically overloaded to provide direct access to individual elements from an array (or other similar structure) contained within a class. Because strings are often implemented as arrays of characters, operator[] is often implemented in string classes to allow the user to access a single character of the string.","title":"14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26"},{"location":"14-x-Chapter-14-comprehensive-quiz/","tags":["summary"],"text":"14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb - \u8fd0\u7b97\u7b26 \u91cd\u8f7d \u662f\u51fd\u6570\u91cd\u8f7d\u7684\u4e00\u79cd\u53d8\u5f62\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u4e3a\u4f60\u7684\u7c7b\u91cd\u8f7d\u64cd\u4f5c\u7b26\u3002\u8fd0\u7b97\u7b26\u91cd\u8f7d\u5e94\u8be5\u5c3d\u91cf\u4fdd\u6301\u548c\u5176\u539f\u672c\u542b\u4e49\u7c7b\u4f3c\u7684\u529f\u80fd\u3002\u5982\u679c\u65b0\u7684\u8fd0\u7b97\u7b26\u7684\u542b\u4e49\u4e0d\u660e\u786e\u6216\u8fdd\u53cd\u76f4\u89c9\uff0c\u90a3\u6700\u597d\u8fd8\u662f\u4f7f\u7528\u4e00\u4e2a\u51fd\u6570\u6765\u4ee3\u66ff\u8fd0\u7b97\u7b26\u91cd\u8f7d\u3002 - \u8fd0\u7b97\u7b26\u53ef\u4ee5\u88ab\u91cd\u8f7d\u4e3a\u666e\u901a\u51fd\u6570\u3001 \u53cb\u5143\u51fd\u6570 \u6216\u8005\u6210\u5458\u51fd\u6570\u3002\u4e0b\u9762\u8fd9\u4e9b\u6cd5\u5219\u53ef\u4ee5\u5e2e\u52a9\u4f60\u51b3\u5b9a\u54ea\u79cd\u5f62\u5f0f\u66f4\u9002\u5408\u4f60\uff1a - \u5f53\u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26(=)\uff0c\u4e0b\u6807\u8fd0\u7b97\u7b26( [] )\u3001\u51fd\u6570\u8c03\u7528\u8fd0\u7b97\u7b26( () )\u6216\u8005\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26( -> )\u65f6\uff0c\u91cd\u8f7d\u4e3a \u6210\u5458\u51fd\u6570 \uff1b - \u5f53\u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26\u65f6\uff0c\u91cd\u8f7d\u4e3a \u6210\u5458\u51fd\u6570 \uff1b - \u5f53\u91cd\u8f7d\u9700\u8981\u4fee\u6539\u5176\u5de6\u64cd\u4f5c\u6570\u7684\u4e8c\u5143\u8fd0\u7b97\u7b26\uff08\u4f8b\u5982 += \uff09\u65f6\uff0c\u5982\u679c\u53ef\u4ee5\u5219\u5c3d\u91cf\u91cd\u8f7d\u4e3a \u6210\u5458\u51fd\u6570 \uff1b - \u5f53\u91cd\u8f7d\u4e0d\u9700\u8981\u4fee\u6539\u5176\u5de6\u64cd\u4f5c\u6570\u7684\u4e8c\u5143\u8fd0\u7b97\u7b26\uff08\u4f8b\u5982 + \uff09\u65f6\uff0c\u53ef\u4ee5\u91cd\u8f7d\u4e3a \u6210\u5458\u51fd\u6570 \u6216 \u53cb\u5143\u51fd\u6570 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26 - \u7c7b\u578b\u8f6c\u6362\u4e5f\u53ef\u4ee5\u91cd\u8f7d\uff0c\u7528\u4e8e\u63d0\u4f9b\u8f6c\u6362\u529f\u80fd\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u663e\u5f0f\u6216\u9690\u5f0f\u5730\u5c06\u7c7b\u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b\u3002 14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570 && 14.13 - \u62f7\u8d1d\u521d\u59cb\u5316 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u662f\u4e00\u7c7b\u7279\u6b8a\u7684\u6784\u9020\u51fd\u6570\uff0c\u7528\u4e8e\u4f7f\u7528\u76f8\u540c\u7c7b\u578b\u7684\u4e00\u4e2a\u5bf9\u8c61\u53bb\u521d\u59cb\u5316\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61\u3002\u5f53\u4f7f\u7528\u76f8\u540c\u7c7b\u578b\u7684\u4e00\u4e2a\u5bf9\u8c61\uff0c\u901a\u8fc7\u76f4\u63a5\u521d\u59cb\u5316\u6216 \u7edf\u4e00\u521d\u59cb\u5316 \u7684\u65b9\u5f0f\u521d\u59cb\u5316\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61\u65f6( Fraction f = Fraction(5,3) )\uff0c\u6216\u8005\u4f20\u9012\u3001\u8fd4\u56de\u503c\u65f6\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u5c31\u4f1a\u88ab\u8c03\u7528\u3002 - \u5982\u679c\u4f60\u4e0d\u63d0\u4f9b\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u4e3a\u4f60\u521b\u5efa\u4e00\u4e2a\u3002\u7f16\u8bd1\u5668\u63d0\u4f9b\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4f1a\u4f7f\u7528 \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \uff0c\u5373\u8be5\u5bf9\u8c61\u7684\u6bcf\u4e2a\u6210\u5458\u90fd\u4f1a\u4ece\u7528\u4e8e\u521d\u59cb\u5316\u5b83\u7684\u5bf9\u8c61\u7684\u6bcf\u4e2a\u6210\u5458\u4f9d\u6b21\u62f7\u8d1d\u3002\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u53ef\u80fd\u4f1a\u51fa\u4e8e\u4f18\u5316\u7684\u76ee\u7684\u88ab\u7701\u7565\uff0c\u5373\u4f7f\u5b83\u6709\u526f\u4f5c\u7528\uff0c\u6240\u4ee5\u4e0d\u8981\u671f\u671b\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4e00\u5b9a\u4f1a\u6267\u884c\u3002 14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57 - \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6784\u9020\u51fd\u6570\u4f1a\u88ab\u770b\u505a\u7c7b\u578b\u8f6c\u6362\u6784\u9020\u51fd\u6570\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u9690\u5f0f\u5730\u5c06\u5176\u4ed6\u7c7b\u578b\u7684\u5bf9\u8c61\u8f6c\u6362\u4e3a\u8fd9\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u4e8b\u60c5\u7684\u53d1\u751f\uff0c\u4f60\u53ef\u4ee5\u5728\u6784\u9020\u51fd\u6570\u524d\u9762\u6dfb\u52a0 explicit \u5173\u952e\u5b57\u3002\u4f60\u4e5f\u53ef\u4ee5\u5220\u9664\u7c7b\u4e2d\u7684\u51fd\u6570( =delete )\uff0c\u6bd4\u5982\u5220\u9664\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u6216\u91cd\u8f7d\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002\u5f53\u8c03\u7528\u88ab\u5220\u9664\u7684\u51fd\u6570\u65f6\uff0c\u4f1a\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002 14.15 - \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26 - \u8d4b\u503c\u8fd0\u7b97\u7b26\u53ef\u4ee5\u88ab\u91cd\u8f7d\u5e76\u7528\u4e8e\u7ed9\u4f60\u7684\u7c7b\u8d4b\u503c\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u63d0\u4f9b\u91cd\u8f7d\u7684\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u7f16\u8bd1\u5668\u5219\u4f1a\u81ea\u52a8\u5730\u4e3a\u4f60\u521b\u5efa\u4e00\u4e2a\u3002\u91cd\u8f7d\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u5e94\u8be5\u603b\u662f\u8fdb\u884c\u81ea\u6211\u8d4b\u503c\u68c0\u67e5\uff08\u9664\u975e\u5b83\u80fd\u591f\u81ea\u7136\u5730\u5904\u7406\uff0c\u6216\u8005\u4f60\u9009\u62e9\u4f7f\u7528 copy-swap\u4e60\u8bed ( what is the copy and swap idiom )\uff09\u3002 - \u65b0\u624b\u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u628a \u8d4b\u503c\u8fd0\u7b97\u7b26 \u548c \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u641e\u6df7\uff0c\u5176\u5b9e\u5b83\u4eec\u5f88\u597d\u533a\u5206\uff1a - \u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u5728\u62f7\u8d1d\u524d\uff0c\u5fc5\u987b\u5148\u88ab\u521b\u5efa\uff0c\u5219\u4f1a\u4f7f\u7528 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \uff08\u6ce8\u610f\uff1a\u5305\u62ec\u5bf9\u8c61\u7684\u6309\u503c\u4f20\u9012\u548c\u8fd4\u56de\uff09\uff1b - \u5982\u679c\u5728\u62f7\u8d1d\u524d\uff0c\u4e0d\u9700\u8981\u5148\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\uff0c\u5219\u4f1a\u4f7f\u7528 \u8d4b\u503c\u8fd0\u7b97\u7b26 \u3002 14.16 - \u6d45\u62f7\u8d1d\u548c\u6df1\u62f7\u8d1d - \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u4f1a\u63d0\u4f9b\u4e00\u4e2a\u80fd\u591f\u8fdb\u884c \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u6216\u8d4b\u503c\u8fd0\u7b97\u7b26\uff0c\u5176\u884c\u4e3a\u79f0\u4e3a \u6d45\u62f7\u8d1d \u3002\u800c\u5982\u679c\u4f60\u7684\u7c7b\u4e2d\u5305\u542b\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\uff0c\u6d45\u62f7\u8d1d\u53ef\u80fd\u4f1a\u5bfc\u81f4\u95ee\u9898\u7684\u53d1\u751f\uff0c\u56e0\u4e3a\u591a\u4e2a\u5bf9\u8c61\u6700\u7ec8\u53ef\u80fd\u4f1a\u6307\u5411\u540c\u4e00\u5757\u5185\u5b58\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f60\u5e94\u8be5\u663e\u5f0f\u5730\u5b9a\u4e49\u5bf9\u8c61\u7684\u62f7\u8d1d\uff0c\u5373\u8fdb\u884c \u6df1\u62f7\u8d1d \u3002\u5982\u679c\u4f60\u53ef\u4ee5\u907f\u514d\u81ea\u5df1\u8fdb\u884c\u5185\u5b58\u7ba1\u7406\uff0c\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684\u7c7b\uff0c\u90a3\u5c31\u66f4\u597d\u4e86\u3002","title":"14.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26\u91cd\u8f7d"},{"location":"16-1-object-relationships/","tags":["object"],"text":"Key Takeaway - \u751f\u6d3b\u4e2d\u5145\u6ee1\u4e86\u53cd\u590d\u51fa\u73b0\u7684\u6a21\u5f0f\u3001\u5173\u7cfb\u548c\u7b49\u7ea7\u5173\u7cfb\u3002\u901a\u8fc7\u63a2\u7d22\u548c\u7406\u89e3\u8fd9\u4e9b\u5173\u7cfb\uff0c\u6211\u4eec\u53ef\u4ee5\u6d1e\u5bdf\u73b0\u5b9e\u751f\u6d3b\u4e2d\u7684\u5bf9\u8c61\u662f\u5982\u4f55\u8fd0\u4f5c\u7684\u4ee5\u589e\u5f3a\u6211\u4eec\u5bf9\u8fd9\u4e9b\u5bf9\u8c61\u7684\u7406\u89e3\u3002 \u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u5929\u4f60\u8d70\u5728\u8857\u4e0a\uff0c\u770b\u5230\u4e00\u4e2a\u4eae\u9ec4\u8272\u7684\u7269\u4f53\u8fde\u63a5\u5728\u4e00\u4e2a\u7eff\u8272\u704c\u6728\u72b6\u7684\u7269\u4f53\u4e0a\u3002\u4f60\u53ef\u80fd\u4f1a\u8ba4\u51fa\u4eae\u9ec4\u8272\u7684\u4e1c\u897f\u662f\u82b1\uff0c\u800c\u7eff\u8272\u704c\u6728\u72b6\u7684\u4e1c\u897f\u662f\u690d\u7269\u3002\u5373\u4f7f\u4f60\u4ee5\u524d\u4ece\u672a\u89c1\u8fc7\u8fd9\u79cd\u7279\u6b8a\u7c7b\u578b\u7684\u690d\u7269\uff0c\u4f60\u4e5f\u77e5\u9053\u8fd9\u4e9b\u7eff\u8272\u7684\u4e1c\u897f\u662f\u6811\u53f6\uff0c\u5b83\u4eec\u6536\u96c6\u9633\u5149\u3002\u4f60\u77e5\u9053\u8fd9\u79cd\u82b1\u80fd\u5e2e\u52a9\u690d\u7269\u7e41\u6b96\u3002\u4f60\u8fd8\u4f1a\u77e5\u9053\uff0c\u5982\u679c\u4f60\u6740\u6b7b\u4e86\u690d\u7269\uff0c\u82b1\u4e5f\u4f1a\u6b7b\u53bb\u3002 \u4f46\u4f60\u4ee5\u524d\u4ece\u672a\u89c1\u8fc7\u8fd9\u79cd\u690d\u7269\uff0c\u4f60\u600e\u4e48\u77e5\u9053\u8fd9\u4e9b\u5462\uff1f\u4f60\u77e5\u9053\u8fd9\u4e00\u70b9\u662f\u56e0\u4e3a\u4f60\u7406\u89e3\u690d\u7269\u7684\u62bd\u8c61\u6982\u5ff5\uff0c\u5e76\u4e14\u8ba4\u8bc6\u5230\u8fd9\u4e2a\u690d\u7269\u662f\u8fd9\u4e2a\u62bd\u8c61\u6982\u5ff5\u7684\u4e00\u4e2a\u5b9e\u4f8b\u3002\u4f60\u77e5\u9053\u5927\u591a\u6570\u690d\u7269(\u90e8\u5206)\u662f\u7531\u53f6\u5b50\u7ec4\u6210\u7684\uff0c\u6709\u4e9b\u690d\u7269\u6709\u82b1\u3002\u4f60\u77e5\u9053\u53f6\u5b50\u4f1a\u4e0e\u9633\u5149\u76f8\u4e92\u4f5c\u7528(\u5373\u4f7f\u4f60\u4e0d\u77e5\u9053\u5177\u4f53\u662f\u5982\u4f55\u4f5c\u7528\u7684)\uff0c\u4f60\u4e5f\u77e5\u9053\u82b1\u7684\u5b58\u5728\u4f9d\u8d56\u4e8e\u690d\u7269\u3002\u56e0\u4e3a\u4f60\u77e5\u9053\u6240\u6709\u8fd9\u4e9b\u5173\u4e8e\u690d\u7269\u7684\u62bd\u8c61\u6982\u5ff5\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u63a8\u65ad\u51fa\u5f88\u591a\u5173\u4e8e\u8fd9\u79cd\u690d\u7269\u7684\u8fdb\u4e00\u6b65\u4fe1\u606f\u3002 \u7c7b\u4f3c\u5730\uff0c\u7f16\u7a0b\u4e5f\u5145\u6ee1\u4e86\u91cd\u590d\u7684\u6a21\u5f0f\u3001\u5173\u7cfb\u548c\u5c42\u6b21\u7ed3\u6784\u3002\u7279\u522b\u662f\u5f53\u6d89\u53ca\u5230\u5bf9\u8c61\u65f6\uff0c\u7ba1\u7406\u73b0\u5b9e\u751f\u6d3b\u5bf9\u8c61\u7684\u76f8\u540c\u6a21\u5f0f\u4e5f\u9002\u7528\u4e8e\u6211\u4eec\u81ea\u5df1\u5728\u7a0b\u5e8f\u4e2d\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u901a\u8fc7\u4ed4\u7ec6\u5206\u6790\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\uff0c\u5e76\u7f16\u5199\u66f4\u5177\u53ef\u6269\u5c55\u6027\u7684\u7c7b\u3002 \u5728\u524d\u51e0\u7ae0\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u63a2\u8ba8\u4e86\u4e00\u4e9b\u5173\u4e8e\u5faa\u73af\u6a21\u5f0f\u7684\u60f3\u6cd5\uff1a\u6211\u4eec\u521b\u5efa\u4e86\u5faa\u73af\u548c\u51fd\u6570\uff0c\u4ee5\u5141\u8bb8\u6211\u4eec\u591a\u6b21\u6267\u884c\u7279\u5b9a\u7684\u4efb\u52a1\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u521b\u5efa\u4e86\u81ea\u5df1\u7684\u679a\u4e3e\u3001\u7ed3\u6784\u548c\u7c7b\uff0c\u4ee5\u5141\u8bb8\u6211\u4eec\u5b9e\u4f8b\u5316\u7ed9\u5b9a\u7c7b\u578b\u7684\u5bf9\u8c61\u3002 \u6211\u4eec\u8fd8\u63a2\u8ba8\u4e86\u5c42\u6b21\u7ed3\u6784\u7684\u4e00\u4e9b\u57fa\u672c\u5f62\u5f0f\uff0c\u4f8b\u5982\u6570\u7ec4(\u5b83\u5141\u8bb8\u6211\u4eec\u5c06\u5143\u7d20\u5206\u7ec4\u5230\u66f4\u5927\u7684\u7ed3\u6784\u4e2d)\u548c\u9012\u5f52(\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u7684\u6d3e\u751f\u7248\u672c)\u3002 \u4f46\u662f\uff0c\u6211\u4eec\u8fd8\u6ca1\u6709\u8fc7\u591a\u5730\u5173\u6ce8\u5bf9\u8c61\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u7279\u522b\u662f\u4e0e\u7f16\u7a0b\u76f8\u5173\u7684\u5173\u7cfb\u3002 \u5bf9\u8c61\u4e4b\u95f4\u7684\u5173\u7cfb \u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\uff0c\u4e24\u4e2a\u5bf9\u8c61\u4e4b\u95f4\u53ef\u80fd\u5b58\u5728\u8bb8\u591a\u4e0d\u540c\u7c7b\u578b\u7684\u5173\u7cfb\uff0c\u6211\u4eec\u4f7f\u7528\u7279\u5b9a\u7684\u201c\u5173\u7cfb\u7c7b\u578b\u201d\u8bcd\u6c47\u6765\u63cf\u8ff0\u8fd9\u4e9b\u5173\u7cfb\u3002\u4f8b\u5982\uff1a\u6b63\u65b9\u5f62\u201d\u662f\uff08is-a\uff09\u201d\u4e00\u79cd\u5f62\u72b6\u3002\u6c7d\u8f66\u201c\u6709\uff08has-a\uff09\u201c\u65b9\u5411\u76d8\u3002\u8ba1\u7b97\u673a\u7a0b\u5e8f\u5458\u201c\u4f7f\u7528\uff08uses-a\uff09\u201d\u952e\u76d8\u3002\u4e00\u6735\u82b1\u201c\u4f9d\u8d56\uff08depends-on\uff09\u201d\u871c\u8702\u6388\u7c89\u3002\u5b66\u751f\u662f\u73ed\u7ea7\u7684\u201c\u6210\u5458\uff08member-of\uff09\u201d\u3002\u4f60\u7684\u5927\u8111\u4f5c\u4e3a\u4f60\u7684\u201c\u4e00\u90e8\u5206\uff08part-of\uff09\u201d\u5b58\u5728(\u81f3\u5c11\uff0c\u5982\u679c\u4f60\u5df2\u7ecf\u8d70\u5230\u8fd9\u4e00\u6b65\uff0c\u6211\u4eec\u53ef\u4ee5\u5408\u7406\u5730\u5047\u8bbe\u662f\u8fd9\u6837)\u3002 \u6240\u6709\u8fd9\u4e9b\u5173\u7cfb\u7c7b\u578b\u5728 C++ \u4e2d\u90fd\u6709\u6709\u7528\u7684\u7c7b\u6bd4\u3002 \u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u7d22\u5173\u7cfb\u7c7b\u578b\u201cpart-of\u201d\u3001\u201chas-a\u201d\u3001\u201cuses-a\u201d\u3001\u201cdepends-on\u201d\u548c\u201cmember-of\u201d\u7684\u7ec6\u5fae\u5dee\u522b\uff0c\u5e76\u5c55\u793a\u5b83\u4eec\u5728 C++ \u7c7b\u8bed\u5883\u4e2d\u662f\u5982\u4f55\u4f7f\u7528\u7684\u3002\u6211\u4eec\u8fd8\u5c06\u63a2\u8ba8\u4e00\u4e9b\u5176\u4ed6\u5730\u65b9\u4e0d\u592a\u9002\u5408\u7684\u76f8\u5173\u4e3b\u9898\u3002 \u7136\u540e\uff0c\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u4e24\u7ae0\u4e2d\u901a\u8fc7 C++ \u7684\u7ee7\u627f\u6a21\u578b\u548c\u865a\u51fd\u6570\u6765\u63a2\u7d22\u201cis-a\u201d\u5173\u7cfb\u3002\u662f\u7684\uff0c\u8fd9\u662f\u4e00\u4e2a\u5927\u95ee\u9898\u3002 \u597d\u4e86\uff0c\u80cc\u666f\u4ecb\u7ecd\u5230\u6b64\u4e3a\u6b62\u3002\u8ba9\u6211\u4eec\u5f00\u59cb\u5427\u3002","title":"16.1 - \u5bf9\u8c61\u5173\u7cfb"},{"location":"16-1-object-relationships/#_1","text":"\u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\uff0c\u4e24\u4e2a\u5bf9\u8c61\u4e4b\u95f4\u53ef\u80fd\u5b58\u5728\u8bb8\u591a\u4e0d\u540c\u7c7b\u578b\u7684\u5173\u7cfb\uff0c\u6211\u4eec\u4f7f\u7528\u7279\u5b9a\u7684\u201c\u5173\u7cfb\u7c7b\u578b\u201d\u8bcd\u6c47\u6765\u63cf\u8ff0\u8fd9\u4e9b\u5173\u7cfb\u3002\u4f8b\u5982\uff1a\u6b63\u65b9\u5f62\u201d\u662f\uff08is-a\uff09\u201d\u4e00\u79cd\u5f62\u72b6\u3002\u6c7d\u8f66\u201c\u6709\uff08has-a\uff09\u201c\u65b9\u5411\u76d8\u3002\u8ba1\u7b97\u673a\u7a0b\u5e8f\u5458\u201c\u4f7f\u7528\uff08uses-a\uff09\u201d\u952e\u76d8\u3002\u4e00\u6735\u82b1\u201c\u4f9d\u8d56\uff08depends-on\uff09\u201d\u871c\u8702\u6388\u7c89\u3002\u5b66\u751f\u662f\u73ed\u7ea7\u7684\u201c\u6210\u5458\uff08member-of\uff09\u201d\u3002\u4f60\u7684\u5927\u8111\u4f5c\u4e3a\u4f60\u7684\u201c\u4e00\u90e8\u5206\uff08part-of\uff09\u201d\u5b58\u5728(\u81f3\u5c11\uff0c\u5982\u679c\u4f60\u5df2\u7ecf\u8d70\u5230\u8fd9\u4e00\u6b65\uff0c\u6211\u4eec\u53ef\u4ee5\u5408\u7406\u5730\u5047\u8bbe\u662f\u8fd9\u6837)\u3002 \u6240\u6709\u8fd9\u4e9b\u5173\u7cfb\u7c7b\u578b\u5728 C++ \u4e2d\u90fd\u6709\u6709\u7528\u7684\u7c7b\u6bd4\u3002 \u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u7d22\u5173\u7cfb\u7c7b\u578b\u201cpart-of\u201d\u3001\u201chas-a\u201d\u3001\u201cuses-a\u201d\u3001\u201cdepends-on\u201d\u548c\u201cmember-of\u201d\u7684\u7ec6\u5fae\u5dee\u522b\uff0c\u5e76\u5c55\u793a\u5b83\u4eec\u5728 C++ \u7c7b\u8bed\u5883\u4e2d\u662f\u5982\u4f55\u4f7f\u7528\u7684\u3002\u6211\u4eec\u8fd8\u5c06\u63a2\u8ba8\u4e00\u4e9b\u5176\u4ed6\u5730\u65b9\u4e0d\u592a\u9002\u5408\u7684\u76f8\u5173\u4e3b\u9898\u3002 \u7136\u540e\uff0c\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u4e24\u7ae0\u4e2d\u901a\u8fc7 C++ \u7684\u7ee7\u627f\u6a21\u578b\u548c\u865a\u51fd\u6570\u6765\u63a2\u7d22\u201cis-a\u201d\u5173\u7cfb\u3002\u662f\u7684\uff0c\u8fd9\u662f\u4e00\u4e2a\u5927\u95ee\u9898\u3002 \u597d\u4e86\uff0c\u80cc\u666f\u4ecb\u7ecd\u5230\u6b64\u4e3a\u6b62\u3002\u8ba9\u6211\u4eec\u5f00\u59cb\u5427\u3002","title":"\u5bf9\u8c61\u4e4b\u95f4\u7684\u5173\u7cfb"},{"location":"16-2-composition/","tags":["object","composition"],"text":"Key Takeaway - \u5bf9\u8c61\u7ec4\u5408 \u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\uff0c\u590d\u6742\u7684\u4e1c\u897f\u5f80\u5f80\u90fd\u57fa\u4e8e\u66f4\u5c0f\u7684\u3001\u66f4\u7b80\u5355\u7684\u4e1c\u897f\u6784\u5efa\u3002\u4f8b\u5982\uff0c\u6c7d\u8f66\u662f\u57fa\u4e8e\u8f66\u67b6\u3001\u5f15\u8d77\u3001\u8f6e\u80ce\u3001\u53d8\u901f\u7bb1\u3001\u65b9\u5411\u76d8\u548c\u5f88\u591a\u5176\u4ed6\u90e8\u4ef6\u6784\u5efa\u7684\u3002\u4e2a\u4eba\u7535\u8111\u5219\u662f\u57fa\u4e8eCPU\u3001\u4e3b\u677f\u3001\u5185\u5b58\u7b49\u6784\u5efa\u7684\u3002\u751a\u81f3\u4f60\u81ea\u5df1\u4e5f\u53ef\u4ee5\u5206\u4e3a\u66f4\u5c0f\u7684\u90e8\u5206\uff1a\u5934\u3001\u8eab\u4f53\u3001\u817f\u3001\u624b\u81c2\u7b49\u7b49\u3002\u901a\u8fc7\u7b80\u5355\u5bf9\u8c61\u6784\u5efa\u590d\u6742\u5bf9\u8c61\u7684\u8fc7\u7a0b\u79f0\u4e3a\u5bf9\u8c61 \u7ec4\u5408 \u3002 \u4e00\u822c\u6765\u8bf4\uff0c\u5bf9\u8c61\u7ec4\u5408\u6a21\u578b\u4e2d\u7684\u5bf9\u8c61\u4e4b\u95f4\u662f\u201c\u6709\u4e00\u4e2a\uff08has-a\uff09\u201d\u7684\u5173\u7cfb\u3002\u6c7d\u8f66\u201c\u6709\u4e00\u4e2a\u201d\u53d8\u901f\u7bb1\u3002\u7535\u8111\u201c\u6709\u4e00\u4e2a\u201dCPU\uff0c\u4f60\u201c\u6709\u4e00\u4e2a\u201d\u5fc3\u810f\u3002\u590d\u6742\u5bf9\u8c61\u901a\u5e38\u88ab\u79f0\u4e3a \u6574\u4f53 \u3002\u800c\u7b80\u5355\u7684\u5bf9\u8c61\u901a\u5e38\u88ab\u79f0\u4e3a\u90e8\u4ef6\u6216\u7ec4\u4ef6\u3002 \u5728C++\u4e2d\uff0c\u4f60\u5df2\u7ecf\u770b\u5230\u7ed3\u6784\u548c\u7c7b\u53ef\u4ee5\u62e5\u6709\u5404\u79cd\u7c7b\u578b\u7684\u6570\u636e\u6210\u5458(\u4f8b\u5982\u57fa\u672c\u7c7b\u578b\u6216\u5176\u4ed6\u7c7b)\u3002\u5f53\u4f7f\u7528\u6570\u636e\u6210\u5458\u6784\u5efa\u7c7b\u65f6\uff0c\u5c31\u662f\u5728\u7528\u66f4\u7b80\u5355\u7684\u90e8\u5206\u6784\u9020\u590d\u6742\u7684\u5bf9\u8c61\uff0c\u8fd9\u5c31\u662f\u5bf9\u8c61\u7ec4\u5408\u3002\u56e0\u6b64\uff0c\u7ed3\u6784\u548c\u7c7b\u6709\u65f6\u88ab\u79f0\u4e3a \u7ec4\u5408\u7c7b\u578b \u3002 \u5bf9\u8c61\u7ec4\u5408\u5728C++\u4e2d\u5f88\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u5c06\u66f4\u7b80\u5355\u3001\u66f4\u5bb9\u6613\u7ba1\u7406\u7684\u90e8\u5206\u6765\u7ec4\u5408\u8d77\u6765\u521b\u5efa\u590d\u6742\u7684\u7c7b\u3002\u8fd9\u964d\u4f4e\u4e86\u590d\u6742\u6027\uff0c\u5e76\u4f7f\u6211\u4eec\u53ef\u4ee5\u66f4\u5feb\u5730\u7f16\u5199\u4ee3\u7801\uff0c\u4e5f\u66f4\u4e0d\u5bb9\u6613\u51fa\u9519\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u91cd\u7528\u5df2\u7ecf\u7f16\u5199\u3001\u6d4b\u8bd5\u548c\u9a8c\u8bc1\u8fc7\u7684\u4ee3\u7801\u3002 \u5bf9\u8c61\u7ec4\u5408\u7684\u7c7b\u578b \u5bf9\u8c61\u7ec4\u5408\u6709\u4e24\u79cd\u57fa\u672c\u7684\u5b50\u7c7b\u578b\uff1a\u7ec4\u5408\u548c\u805a\u5408\u3002\u8fd9\u8282\u8bfe\u6211\u4eec\u5c06\u8ba8\u8bba\u7ec4\u5408\uff0c\u4e0b\u8282\u8bfe\u6211\u4eec\u5c06\u8ba8\u8bba \u805a\u5408\u5173\u7cfb \u3002 \u5173\u4e8e\u672f\u8bed\u7684\u8bf4\u660e\uff1a\u672f\u8bed\u201c\u7ec4\u5408\u201d\u901a\u5e38\u540c\u65f6\u6307\u7ec4\u5408\u548c\u805a\u5408\uff0c\u800c\u4e0d\u4ec5\u4ec5\u6307\u7ec4\u5408\u5b50\u7c7b\u578b\u3002\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u5f53\u6211\u4eec\u63d0\u5230\u4e24\u8005\u65f6\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u672f\u8bed\u201c\u5bf9\u8c61\u7ec4\u5408\u201d\uff0c\u5f53\u6211\u4eec\u4e13\u95e8\u63d0\u5230\u7ec4\u5408\u5b50\u7c7b\u578b\u65f6\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u672f\u8bed\u201c\u7ec4\u5408\u201d\u3002 \u7ec4\u5408 \u5408\u683c\u7684\u7ec4\u5408\u5173\u7cfb\u9700\u8981\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\uff1a \u90e8\u5206(\u6210\u5458)\u662f\u5bf9\u8c61(\u7c7b)\u7684\u4e00\u90e8\u5206 \u90e8\u4ef6(\u6210\u5458)\u4e00\u6b21\u53ea\u80fd\u5c5e\u4e8e\u4e00\u4e2a\u5bf9\u8c61(\u7c7b) \u90e8\u5206(\u6210\u5458)\u7684\u5b58\u5728\u7531\u5bf9\u8c61(\u7c7b)\u7ba1\u7406 \u90e8\u5206(\u6210\u5458)\u4e0d\u77e5\u9053\u5bf9\u8c61(\u7c7b)\u7684\u5b58\u5728 \u73b0\u5b9e\u751f\u6d3b\u4e2d\u4e00\u4e2a\u5f88\u597d\u7684\u7ec4\u5408\u4f8b\u5b50\u662f\u4e00\u4e2a\u4eba\u7684\u8eab\u4f53\u548c\u5fc3\u810f\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u8ba9\u6211\u4eec\u66f4\u8be6\u7ec6\u5730\u7814\u7a76\u8fd9\u4e9b\u5185\u5bb9\u3002 \u7ec4\u5408\u5173\u7cfb\u662f\u201c\u90e8\u5206-\u6574\u4f53\u201d\u7684\u5173\u7cfb\uff0c\u5176\u4e2d\u90e8\u5206\u5fc5\u987b\u6784\u6210\u6574\u4e2a\u5bf9\u8c61\u7684\u4e00\u90e8\u5206\u3002\u4f8b\u5982\uff0c\u5fc3\u810f\u662f\u4e00\u4e2a\u4eba\u8eab\u4f53\u7684\u4e00\u90e8\u5206\u3002\u7ec4\u5408\u4e2d\u7684\u90e8\u5206\u53ea\u80fd\u662f\u5c5e\u4e8e \u4e00\u4e2a\u5bf9\u8c61 \u7684\u4e00\u90e8\u5206\u3002\u4f5c\u4e3a\u4e00\u4e2a\u4eba\u8eab\u4f53\u4e00\u90e8\u5206\u7684\u5fc3\u810f\u4e0d\u53ef\u80fd\u540c\u65f6\u662f\u53e6\u4e00\u4e2a\u4eba\u8eab\u4f53\u7684\u4e00\u90e8\u5206\u3002 \u5728\u7ec4\u5408\u5173\u7cfb\u4e2d\uff0c\u5bf9\u8c61\u8d1f\u8d23\u7ba1\u7406\u5404\u4e2a\u90e8\u4ef6\u7684\u5b58\u5728\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u8fd9\u610f\u5473\u7740\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u521b\u5efa\u8be5\u90e8\u4ef6\uff0c\u5728\u9500\u6bc1\u5bf9\u8c61\u65f6\u9500\u6bc1\u8be5\u90e8\u5206\u3002\u4f46\u66f4\u5e7f\u6cdb\u5730\u8bf4\uff0c\u5b83\u610f\u5473\u7740\u5bf9\u8c61\u4ee5\u4e00\u79cd\u4e0d\u9700\u8981\u5bf9\u8c61\u7684\u7528\u6237\u53c2\u4e0e\u7684\u65b9\u5f0f\u7ba1\u7406\u90e8\u4ef6\u7684\u751f\u547d\u5468\u671f\u3002\u4f8b\u5982\uff0c\u5f53\u4e00\u4e2a\u8eab\u4f53\u88ab\u521b\u9020\u51fa\u6765\u65f6\uff0c\u5fc3\u810f\u4e5f\u88ab\u521b\u9020\u51fa\u6765\u4e86\u3002\u5f53\u4e00\u4e2a\u4eba\u7684\u8eab\u4f53\u88ab\u6467\u6bc1\u65f6\uff0c\u4ed6\u7684\u5fc3\u4e5f\u4f1a\u88ab\u6467\u6bc1\u3002\u6b63\u56e0\u4e3a\u5982\u6b64\uff0c\u7ec4\u5408\u6709\u65f6\u88ab\u79f0\u4e3a\u201c\u6b7b\u4ea1\u5173\u7cfb\u201d\u3002 \u6700\u540e\uff0c\u90e8\u5206\u4e0d\u77e5\u9053\u6574\u4f53\u7684\u5b58\u5728\u3002\u4f60\u7684\u5fc3\u5728\u5e78\u798f\u5730\u8fd0\u8f6c\u7740\uff0c\u5374\u6ca1\u6709\u610f\u8bc6\u5230\u5b83\u662f\u4e00\u4e2a\u66f4\u5927\u7ed3\u6784\u7684\u4e00\u90e8\u5206\u3002\u6211\u4eec\u79f0\u4e4b\u4e3a\u5355\u5411\u5173\u7cfb\uff0c\u56e0\u4e3a\u8eab\u4f53\u4e86\u89e3\u5fc3\u810f\uff0c\u4f46\u5fc3\u810f\u4e0d\u4e86\u89e3\u8eab\u4f53\u3002 \u6ce8\u610f\uff0c\u7ec4\u5408\u5173\u7cfb\u4e0e\u90e8\u4ef6\u7684\u53ef\u79fb\u690d\u6027\u65e0\u5173\u3002\u5fc3\u810f\u53ef\u4ee5\u4ece\u4e00\u4e2a\u8eab\u4f53\u79fb\u690d\u5230\u53e6\u4e00\u4e2a\u8eab\u4e0a\u3002\u7136\u800c\uff0c\u5373\u4f7f\u5728\u79fb\u690d\u540e\uff0c\u5b83\u4ecd\u7136\u6ee1\u8db3\u7ec4\u5408\u7684\u5b9a\u4e49(\u5fc3\u810f\u73b0\u5728\u5c5e\u4e8e\u63a5\u53d7\u8005\uff0c\u9664\u975e\u518d\u6b21\u79fb\u690d\uff0c\u5426\u5219\u53ea\u80fd\u662f\u63a5\u53d7\u8005\u5bf9\u8c61\u7684\u4e00\u90e8\u5206)\u3002 \u6211\u4eec\u7684 Fraction \u7c7b\u5c31\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u7ec4\u5408\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 class Fraction { private : int m_numerator ; int m_denominator ; public : Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { } }; \u8fd9\u4e2a\u7c7b\u6709\u4e24\u4e2a\u6570\u636e\u6210\u5458\uff1a\u5206\u5b50\u548c\u5206\u6bcd\u3002\u5206\u5b50\u548c\u5206\u6bcd\u662f\u5206\u6570\u7684\u4e00\u90e8\u5206(\u5305\u542b\u5728\u5206\u6570\u4e2d)\u3002\u5b83\u4eec\u4e0d\u80fd\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u5206\u6570\u3002\u5206\u5b50\u548c\u5206\u6bcd\u5e76\u4e0d\u77e5\u9053\u5b83\u4eec\u662f\u5206\u6570\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u4eec\u53ea\u5305\u542b\u6574\u6570\u3002\u5728\u521b\u5efa Fraction \u5b9e\u4f8b\u65f6\uff0c\u5c06\u521b\u5efa\u5206\u5b50\u548c\u5206\u6bcd\u3002\u5f53\u5206\u6570\u5b9e\u4f8b\u88ab\u7834\u574f\u65f6\uff0c\u5206\u5b50\u548c\u5206\u6bcd\u4e5f\u4f1a\u88ab\u7834\u574f\u3002 \u5c3d\u7ba1\u5bf9\u8c61\u7ec4\u5408\u5173\u7cfb\u63cf\u8ff0\u7684\u662f\u201c\u5177\u6709\u201d\u7684\u5173\u7cfb\uff08\u4eba\u4f53\u6709\u4e00\u4e2a\u5fc3\u810f\u3001\u5206\u6570\u6709\u4e00\u4e2a\u5206\u6bcd\uff09\uff0c\u4f46\u662f\u66f4\u52a0\u7cbe\u786e\u7684\u8bf4\u6cd5\u5176\u5b9e\u662f\uff0c\u7ec4\u5408\u63cf\u8ff0\u7684\u662f\u201c\u90e8\u5206\u4e0e\u6574\u4f53\u201d\u7684\u5173\u7cfb\uff08\u5fc3\u810f\u662f\u4eba\u4f53\u7684\u4e00\u90e8\u5206\u3001\u5206\u5b50\u662f\u5206\u6570\u7684\u4e00\u90e8\u5206\uff09\u3002\u7ec4\u5408\u5173\u7cfb\u901a\u5e38\u7528\u4e8e\u5bf9\u4e00\u4e2a\u7269\u7406\u5173\u7cfb\u8fdb\u884c\u5efa\u6a21\uff0c\u5373\u4e00\u4e2a\u5bf9\u8c61\u88ab\u5305\u542b\u5728\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61\u4e2d\u3002 \u201c\u90e8\u5206\u201d\u5173\u7cfb\u53ef\u4ee5\u662f\u201c\u4e00\u5bf9\u4e00\u7684\u201d\uff0c\u4e5f\u53ef\u4ee5\u662f\u201c\u591a\u5bf9\u4e00\u7684\u201d\u2014\u2014\u4f8b\u5982\uff0c\u8eab\u4f53\u4e2d\u5305\u542b\u4e00\u4e2a\u5fc3\u810f\uff0c\u4f46\u662f\u8eab\u4f53\u53ef\u4ee5\u5305\u542b10\u4e2a\u624b\u6307\uff08\u6700\u7ec8\u88ab\u5b9e\u73b0\u4e3a\u6570\u7ec4\uff09\u3002 \u5b9e\u73b0\u7ec4\u5408\u5173\u7cfb \u7ec4\u5408\u662fC++\u4e2d\u6700\u5bb9\u6613\u5b9e\u73b0\u7684\u5173\u7cfb\u7c7b\u578b\u4e4b\u4e00\u3002\u5b83\u4eec\u901a\u5e38\u88ab\u521b\u5efa\u4e3a\u5177\u6709\u666e\u901a\u6570\u636e\u6210\u5458\u7684\u7ed3\u6784\u6216\u7c7b\u3002\u56e0\u4e3a\u8fd9\u4e9b\u6570\u636e\u6210\u5458\u76f4\u63a5\u4f5c\u4e3a\u7ed3\u6784/\u7c7b\u7684\u4e00\u90e8\u5206\u5b58\u5728\uff0c\u6240\u4ee5\u5b83\u4eec\u7684\u751f\u547d\u5468\u671f\u4e0e\u7c7b\u5b9e\u4f8b\u672c\u8eab\u7684\u751f\u547d\u5468\u671f\u7ed1\u5b9a\u5728\u4e00\u8d77\u3002 \u9700\u8981\u8fdb\u884c\u52a8\u6001\u5206\u914d\u6216\u56de\u6536\u7684\u7ec4\u5408\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\u6570\u636e\u6210\u5458\u5b9e\u73b0\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7ec4\u5408\u7c7b\u5e94\u8be5\u81ea\u5df1\u8d1f\u8d23\u6240\u6709\u5fc5\u8981\u7684\u5185\u5b58\u7ba1\u7406(\u800c\u4e0d\u662f\u7c7b\u7684\u7528\u6237)\u3002 \u4e00\u822c\u6765\u8bf4\uff0c\u5982\u679c\u4f60\u80fd\u4f7f\u7528\u590d\u5408\u8bbe\u8ba1\u4e00\u4e2a\u7c7b\uff0c\u4f60\u5c31\u5e94\u8be5\u4f7f\u7528\u7ec4\u5408\u5173\u7cfb\u8bbe\u8ba1\u4e00\u4e2a\u7c7b\u3002\u4f7f\u7528\u7ec4\u5408\u8bbe\u8ba1\u7684\u7c7b\u662f\u76f4\u63a5\u7684\u3001\u7075\u6d3b\u7684\u548c\u5065\u58ee\u7684(\u56e0\u4e3a\u5b83\u4eec\u5f88\u597d\u5730\u81ea\u884c\u6e05\u7406)\u3002 \u66f4\u591a\u7684\u4f8b\u5b50 \u8bb8\u591a\u6e38\u620f\u90fd\u6709\u5728\u68cb\u76d8\u3001\u5730\u56fe\u6216\u5c4f\u5e55\u4e0a\u79fb\u52a8\u7684\u751f\u7269\u6216\u7269\u4f53\u3002\u6240\u6709\u8fd9\u4e9b\u751f\u7269/\u7269\u4f53\u7684\u4e00\u4e2a\u5171\u540c\u70b9\u662f\uff0c\u5b83\u4eec\u90fd\u6709\u4e00\u4e2a\u4f4d\u7f6e\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u751f\u7269\u7c7b\uff0c\u5b83\u4f7f\u7528\u4e00\u4e2a Point \u7c7b\u6765\u4fdd\u5b58\u751f\u7269\u7684\u4f4d\u7f6e\u3002 \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u8bbe\u8ba1 Point \u7c7b\u3002\u751f\u7269\u5c06\u751f\u6d3b\u57282d\u4e16\u754c\u4e2d\uff0c\u6240\u4ee5 Point \u7c7b\u53ea\u9700\u8981\u6709\u4e24\u4e2a\u7ef4\u5ea6 x \u548c y \u3002\u6211\u4eec\u5047\u8bbe\u4e16\u754c\u662f\u7531\u79bb\u6563\u7684\u6b63\u65b9\u5f62\u7ec4\u6210\uff0c\u6240\u4ee5\u8fd9\u4e9b\u7ef4\u5ea6\u603b\u662f\u6574\u6570\u3002 Point2D.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #ifndef POINT2D_H #define POINT2D_H #include class Point2D { private : int m_x ; int m_y ; public : // \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 Point2D () : m_x { 0 }, m_y { 0 } { } // \u6709\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570 Point2D ( int x , int y ) : m_x { x }, m_y { y } { } // \u91cd\u8f7d\u7684\u8f93\u51fa\u64cd\u4f5c\u7b26 friend std :: ostream & operator << ( std :: ostream & out , const Point2D & point ) { out << '(' << point . m_x << \", \" << point . m_y << ')' ; return out ; } // \u6210\u5458\u8bbf\u95ee\u51fd\u6570 void setPoint ( int x , int y ) { m_x = x ; m_y = y ; } }; #endif \u6ce8\u610f\uff0c\u56e0\u4e3a\u6240\u6709\u51fd\u6570\u7684\u5b9e\u73b0\u90fd\u5728\u5934\u6587\u4ef6\u4e2d\uff08\u4e3a\u4e86\u4fdd\u6301\u4f8b\u5b50\u7b80\u77ed\uff09\uff0c\u6240\u4ee5\u5e76\u6ca1\u6709 Point2D.cpp \u6587\u4ef6\u3002 Point2d \u662f\u5b83\u7684\u90e8\u4ef6\u7684\u7684\u7ec4\u5408\uff1a\u4f4d\u7f6e\u4fe1\u606fx\u548cy\u90fd\u662f Point2D \u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u5b83\u4eec\u7684\u751f\u547d\u5468\u671f\u4e5f\u548c Point2D \u5b9e\u4f8b\u7ed1\u5b9a\u5728\u4e00\u8d77\u3002 \u73b0\u5728\uff0c\u5b9a\u4e49 Creature \u7c7b\u3002 Creature \u6709\u5982\u4e0b\u51e0\u4e2a\u5c5e\u6027\uff1a\u540d\u79f0\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4f4d\u7f6e\u5219\u662f\u4e00\u4e2a Point2D \u7c7b\u3002 Creature.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #ifndef CREATURE_H #define CREATURE_H #include #include #include \"Point2D.h\" class Creature { private : std :: string m_name ; //\u540d\u79f0 Point2D m_location ; //\u4f4d\u7f6e public : Creature ( const std :: string & name , const Point2D & location ) : m_name { name }, m_location { location } { } friend std :: ostream & operator << ( std :: ostream & out , const Creature & creature ) { out << creature . m_name << \" is at \" << creature . m_location ; return out ; } void moveTo ( int x , int y ) { m_location . setPoint ( x , y ); } }; #endif Creature \u540c\u6837\u4e5f\u662f\u5176\u5404\u90e8\u5206\u7684\u7ec4\u5408\u3002\u751f\u7269\u7684\u540d\u5b57\u548c\u4f4d\u7f6e\u5176\u58f0\u660e\u5468\u671f\u90fd\u7ed1\u5b9a\u5230\u4e86 Creature \u3002 \u6700\u7ec8\u7684\u7a0b\u5e8f\u662f\u8fd9\u6837\u7684\u3002 main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include #include #include \"Creature.h\" #include \"Point2D.h\" int main () { std :: cout << \"Enter a name for your creature: \" ; std :: string name ; std :: cin >> name ; Creature creature { name , { 4 , 7 } }; while ( true ) { // print the creature's name and location std :: cout << creature << '\\n' ; std :: cout << \"Enter new X location for creature (-1 to quit): \" ; int x { 0 }; std :: cin >> x ; if ( x == -1 ) break ; std :: cout << \"Enter new Y location for creature (-1 to quit): \" ; int y { 0 }; std :: cin >> y ; if ( y == -1 ) break ; creature . moveTo ( x , y ); } return 0 ; } \u8fd0\u884c\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 Enter a name for your creature: Marvin Marvin is at (4, 7) Enter new X location for creature (-1 to quit): 6 Enter new Y location for creature (-1 to quit): 12 Marvin is at (6, 12) Enter new X location for creature (-1 to quit): 3 Enter new Y location for creature (-1 to quit): 2 Marvin is at (3, 2) Enter new X location for creature (-1 to quit): -1 \u4e0d\u540c\u7c7b\u578b\u7684\u7ec4\u5408 \u5c3d\u7ba1\u5927\u591a\u6570\u7ec4\u5408\u4f1a\u5728\u7ec4\u5408\u5173\u7cfb\u5efa\u7acb\u65f6\uff0c\u76f4\u63a5\u521b\u5efa\u5b83\u4eec\u7684\u90e8\u5206\uff0c\u5e76\u5728\u7ec4\u5408\u88ab\u7834\u574f\u65f6\u76f4\u63a5\u9500\u6bc1\u5b83\u4eec\u7684\u90e8\u5206\uff0c\u4f46\u4e5f\u6709\u4e00\u4e9b\u4e0d\u540c\u7c7b\u578b\u7684\u7ec4\u5408\u4e0d\u6ee1\u8db3\u8fd9\u4e00\u89c4\u5219\u3002 \u4f8b\u5982\uff1a \u7ec4\u5408\u4e5f\u53ef\u4ee5\u5148\u4e0d\u521b\u5efa\u90e8\u5206\uff0c\u800c\u662f\u5728\u9700\u8981\u4f7f\u7528\u5b83\u4eec\u65f6\u5728\u521b\u5efa\u3002\u4f8b\u5982\uff0c\u5b57\u7b26\u4e32\u7c7b\u53ef\u4ee5\u5148\u4e0d\u52a8\u6001\u5206\u914d\u5b57\u7b26\u6570\u7ec4\uff0c\u5f53\u771f\u6b63\u6709\u6570\u636e\u8981\u5b58\u653e\u65f6\u518d\u521b\u5efa\uff1b \u7ec4\u5408\u4f7f\u7528\u7684\u90e8\u5206\u53ef\u4ee5\u662f\u88ab\u4f20\u5165\u7684\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u5b83\u81ea\u5df1\u521b\u5efa\u7684\uff1b \u7ec4\u5408\u4e5f\u53ef\u4ee5\u628a\u9500\u6bc1\u90e8\u5206\u7684\u5de5\u4f5c\u59d4\u6d3e\u7ed9\u5176\u4ed6\u5bf9\u8c61\uff08\u4f8b\u5982\uff0c\u5783\u573e\u56de\u6536\u7a0b\u5e8f\uff09\u3002 \u8fd9\u91cc\u7684\u5173\u952e\u70b9\u662f\uff0c\u7ec4\u5408\u5e94\u8be5\u7ba1\u7406\u5b83\u7684\u5404\u4e2a\u90e8\u5206\uff0c\u800c\u4e0d\u9700\u8981\u7ec4\u5408\u7684\u7528\u6237\u7ba1\u7406\u4efb\u4f55\u4e1c\u897f\u3002 \u7ec4\u5408\u548c\u7c7b\u6210\u5458 \u5f53\u6d89\u53ca\u5230\u5bf9\u8c61\u7ec4\u5408\u65f6\uff0c\u65b0\u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u95ee\u7684\u4e00\u4e2a\u95ee\u9898\u662f\uff0c\u201c\u4ec0\u4e48\u65f6\u5019\u6211\u5e94\u8be5\u4f7f\u7528\u7c7b\u6210\u5458\u800c\u4e0d\u662f\u76f4\u63a5\u4f7f\u7528\u67d0\u4e2a\u7279\u6027\uff1f\u201d\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4e0d\u4f7f\u7528 Point2D``\u7c7b\u6765\u5b9e\u73b0Creature \u7684\u4f4d\u7f6e\uff0c\u800c\u662f\u5411 Creature \u7c7b\u4e2d\u76f4\u63a5\u6dfb\u52a02\u4e2a\u6574\u6570\uff0c\u5e76\u7f16\u5199\u4ee3\u7801\u6765\u5904\u7406\u5b9a\u4f4d\u3002\u7136\u800c\uff0c\u8ba9 Point2D \u6210\u4e3a\u72ec\u7acb\u7684\u7c7b(\u5e76\u6210\u4e3a Creature \u7684\u7684\u6210\u5458)\u6709\u5f88\u591a\u597d\u5904\uff1a \u6bcf\u4e2a\u72ec\u7acb\u7684\u7c7b\u80fd\u591f\u5c3d\u53ef\u80fd\u5730\u4fdd\u6301\u7b80\u6d01\uff0c\u4e13\u6ce8\u4e8e\u5b83\u8981\u5b8c\u6210\u7684\u4efb\u52a1\u3002\u8fd9\u4f7f\u5f97\u8fd9\u4e9b\u7c7b\u66f4\u5bb9\u6613\u7f16\u5199\u3001\u4e5f\u66f4\u5bb9\u6613\u7406\u89e3\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ea\u4e13\u6ce8\u4e00\u4e2a\u529f\u80fd\u3002\u4f8b\u5982\uff0c Point2D \u53ea\u9700\u8981\u5173\u6ce8\u548c\u201c\u70b9\u201d\u76f8\u5173\u7684\u529f\u80fd\uff0c\u663e\u7136\u6709\u52a9\u4e8e\u4fdd\u6301\u7c7b\u7684\u7b80\u6d01\u3002 \u6bcf\u4e2a\u7c7b\u90fd\u662f\u81ea\u5305\u542b\u7684\uff0c\u6240\u4ee5\u5b83\u4eec\u80fd\u591f\u88ab\u590d\u7528\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5176\u4ed6\u5e94\u7528\u4e2d\u590d\u7528 Point2D \u8fd9\u4e2a\u7c7b\u3002\u800c\u4e14\uff0c\u5982\u679c creature \u9700\u8981\u53e6\u5916\u4e00\u4e2a\u70b9\uff08\u4f8b\u5982\uff0c\u8868\u793a\u8981\u53bb\u7684\u76ee\u6807\u70b9\uff09\uff0c\u6b64\u65f6\u53ea\u9700\u8981\u5411\u7c7b\u4e2d\u518d\u6dfb\u52a0\u4e00\u4e2a Point2D \u6210\u5458\u53d8\u91cf\u5373\u53ef\uff1b \u5916\u5c42\u7c7b\u53ef\u4ee5\u5229\u7528\u5176\u7c7b\u6210\u5458\u5b8c\u6210\u66f4\u590d\u6742\u7684\u4efb\u52a1\uff0c\u800c\u4e0d\u9700\u8981\u5728\u4e0d\u540c\u7684\u6210\u5458\u95f4\u534f\u8c03\u6570\u636e\u6d41\uff0c\u8fd9\u663e\u7136\u6709\u52a9\u4e8e\u964d\u4f4e\u5916\u5c42\u7c7b\u7684\u6574\u4f53\u590d\u6742\u5ea6\uff0c\u56e0\u4e3a\u5b83\u5c06\u4efb\u52a1\u59d4\u6d3e\u7ed9\u4e86\u5176\u6210\u5458\uff0c\u4e14\u8be5\u6210\u5458\u5df2\u7ecf\u77e5\u9053\u5e94\u8be5\u5982\u4f55\u5b8c\u6210\u76f8\u5173\u7684\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u5728\u9700\u8981\u79fb\u52a8 Creature \u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u5c06\u4efb\u52a1\u59d4\u6d3e\u7ed9 Point \u7c7b\uff0c\u800c\u8be5\u7c7b\u662f\u77e5\u9053\u5982\u4f55\u8bbe\u7f6e\u4e00\u4e2a\u70b9\u7684\u3002\u56e0\u6b64 Creature \u7c7b\u5728\u5b9e\u73b0\u7684\u65f6\u5019\uff0c\u5c31\u65e0\u9700\u64cd\u5fc3\u8fd9\u4e9b\u95ee\u9898\u4e86\u3002 \u5c0f\u8d34\u58eb \u6839\u636e\u7ecf\u9a8c\u6cd5\u5219\uff0c\u4e00\u4e2a\u7c7b\u5e94\u8be5\u88ab\u8bbe\u8ba1\u4e3a\u4ec5\u5b8c\u6210\u4e00\u4e2a\u4efb\u52a1\u3002\u8fd9\u4e2a\u4efb\u52a1\u8981\u4e48\u662f\u5b58\u50a8\u6216\u4fee\u6539\u67d0\u79cd\u6570\u636e\uff08\u4f8b\u5982\uff0c Point2D \u6216 std::string )\uff0c\u8981\u4e48\u662f\u534f\u8c03\u5b83\u7684\u6210\u5458\u5b8c\u6210\u67d0\u4e9b\u4efb\u52a1\u3002\u4f46\u7406\u60f3\u60c5\u51b5\u4e0b\u4e0d\u5e94\u8be5\u540c\u65f6\u505a\u4e0a\u9762\u4e24\u4ef6\u4e8b\u3002 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c Creature \u7684\u786e\u4e0d\u5e94\u8be5\u64cd\u5fc3 Points \u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff0c\u4e5f\u4e0d\u9700\u8981\u5173\u5fc3name\u662f\u5982\u4f55\u50a8\u5b58\u7684\u3002 Creature \u7684\u4efb\u52a1\u4e0d\u662f\u4e86\u89e3\u8fd9\u4e9b\u7ec6\u8282\uff0c\u800c\u662f\u5982\u4f55\u8981\u5173\u6ce8\u5982\u4f55\u534f\u8c03\u6570\u636e\u6d41\u5e76\u786e\u4fdd\u5176\u6bcf\u4e2a\u6210\u5458\u90fd\u77e5\u9053\u5b83\u4eec\u8981\u505a\u4ec0\u4e48\u3002\u800c\u5982\u4f55\u505a\uff0c\u5219\u7531\u6210\u5458\u81ea\u5df1\u6765\u8d1f\u8d23\u3002","title":"16.2 - \u7ec4\u5408\u5173\u7cfb"},{"location":"16-2-composition/#_1","text":"\u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\uff0c\u590d\u6742\u7684\u4e1c\u897f\u5f80\u5f80\u90fd\u57fa\u4e8e\u66f4\u5c0f\u7684\u3001\u66f4\u7b80\u5355\u7684\u4e1c\u897f\u6784\u5efa\u3002\u4f8b\u5982\uff0c\u6c7d\u8f66\u662f\u57fa\u4e8e\u8f66\u67b6\u3001\u5f15\u8d77\u3001\u8f6e\u80ce\u3001\u53d8\u901f\u7bb1\u3001\u65b9\u5411\u76d8\u548c\u5f88\u591a\u5176\u4ed6\u90e8\u4ef6\u6784\u5efa\u7684\u3002\u4e2a\u4eba\u7535\u8111\u5219\u662f\u57fa\u4e8eCPU\u3001\u4e3b\u677f\u3001\u5185\u5b58\u7b49\u6784\u5efa\u7684\u3002\u751a\u81f3\u4f60\u81ea\u5df1\u4e5f\u53ef\u4ee5\u5206\u4e3a\u66f4\u5c0f\u7684\u90e8\u5206\uff1a\u5934\u3001\u8eab\u4f53\u3001\u817f\u3001\u624b\u81c2\u7b49\u7b49\u3002\u901a\u8fc7\u7b80\u5355\u5bf9\u8c61\u6784\u5efa\u590d\u6742\u5bf9\u8c61\u7684\u8fc7\u7a0b\u79f0\u4e3a\u5bf9\u8c61 \u7ec4\u5408 \u3002 \u4e00\u822c\u6765\u8bf4\uff0c\u5bf9\u8c61\u7ec4\u5408\u6a21\u578b\u4e2d\u7684\u5bf9\u8c61\u4e4b\u95f4\u662f\u201c\u6709\u4e00\u4e2a\uff08has-a\uff09\u201d\u7684\u5173\u7cfb\u3002\u6c7d\u8f66\u201c\u6709\u4e00\u4e2a\u201d\u53d8\u901f\u7bb1\u3002\u7535\u8111\u201c\u6709\u4e00\u4e2a\u201dCPU\uff0c\u4f60\u201c\u6709\u4e00\u4e2a\u201d\u5fc3\u810f\u3002\u590d\u6742\u5bf9\u8c61\u901a\u5e38\u88ab\u79f0\u4e3a \u6574\u4f53 \u3002\u800c\u7b80\u5355\u7684\u5bf9\u8c61\u901a\u5e38\u88ab\u79f0\u4e3a\u90e8\u4ef6\u6216\u7ec4\u4ef6\u3002 \u5728C++\u4e2d\uff0c\u4f60\u5df2\u7ecf\u770b\u5230\u7ed3\u6784\u548c\u7c7b\u53ef\u4ee5\u62e5\u6709\u5404\u79cd\u7c7b\u578b\u7684\u6570\u636e\u6210\u5458(\u4f8b\u5982\u57fa\u672c\u7c7b\u578b\u6216\u5176\u4ed6\u7c7b)\u3002\u5f53\u4f7f\u7528\u6570\u636e\u6210\u5458\u6784\u5efa\u7c7b\u65f6\uff0c\u5c31\u662f\u5728\u7528\u66f4\u7b80\u5355\u7684\u90e8\u5206\u6784\u9020\u590d\u6742\u7684\u5bf9\u8c61\uff0c\u8fd9\u5c31\u662f\u5bf9\u8c61\u7ec4\u5408\u3002\u56e0\u6b64\uff0c\u7ed3\u6784\u548c\u7c7b\u6709\u65f6\u88ab\u79f0\u4e3a \u7ec4\u5408\u7c7b\u578b \u3002 \u5bf9\u8c61\u7ec4\u5408\u5728C++\u4e2d\u5f88\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u5c06\u66f4\u7b80\u5355\u3001\u66f4\u5bb9\u6613\u7ba1\u7406\u7684\u90e8\u5206\u6765\u7ec4\u5408\u8d77\u6765\u521b\u5efa\u590d\u6742\u7684\u7c7b\u3002\u8fd9\u964d\u4f4e\u4e86\u590d\u6742\u6027\uff0c\u5e76\u4f7f\u6211\u4eec\u53ef\u4ee5\u66f4\u5feb\u5730\u7f16\u5199\u4ee3\u7801\uff0c\u4e5f\u66f4\u4e0d\u5bb9\u6613\u51fa\u9519\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u91cd\u7528\u5df2\u7ecf\u7f16\u5199\u3001\u6d4b\u8bd5\u548c\u9a8c\u8bc1\u8fc7\u7684\u4ee3\u7801\u3002","title":"\u5bf9\u8c61\u7ec4\u5408"},{"location":"16-2-composition/#_2","text":"\u5bf9\u8c61\u7ec4\u5408\u6709\u4e24\u79cd\u57fa\u672c\u7684\u5b50\u7c7b\u578b\uff1a\u7ec4\u5408\u548c\u805a\u5408\u3002\u8fd9\u8282\u8bfe\u6211\u4eec\u5c06\u8ba8\u8bba\u7ec4\u5408\uff0c\u4e0b\u8282\u8bfe\u6211\u4eec\u5c06\u8ba8\u8bba \u805a\u5408\u5173\u7cfb \u3002 \u5173\u4e8e\u672f\u8bed\u7684\u8bf4\u660e\uff1a\u672f\u8bed\u201c\u7ec4\u5408\u201d\u901a\u5e38\u540c\u65f6\u6307\u7ec4\u5408\u548c\u805a\u5408\uff0c\u800c\u4e0d\u4ec5\u4ec5\u6307\u7ec4\u5408\u5b50\u7c7b\u578b\u3002\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u5f53\u6211\u4eec\u63d0\u5230\u4e24\u8005\u65f6\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u672f\u8bed\u201c\u5bf9\u8c61\u7ec4\u5408\u201d\uff0c\u5f53\u6211\u4eec\u4e13\u95e8\u63d0\u5230\u7ec4\u5408\u5b50\u7c7b\u578b\u65f6\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u672f\u8bed\u201c\u7ec4\u5408\u201d\u3002","title":"\u5bf9\u8c61\u7ec4\u5408\u7684\u7c7b\u578b"},{"location":"16-2-composition/#_3","text":"\u5408\u683c\u7684\u7ec4\u5408\u5173\u7cfb\u9700\u8981\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\uff1a \u90e8\u5206(\u6210\u5458)\u662f\u5bf9\u8c61(\u7c7b)\u7684\u4e00\u90e8\u5206 \u90e8\u4ef6(\u6210\u5458)\u4e00\u6b21\u53ea\u80fd\u5c5e\u4e8e\u4e00\u4e2a\u5bf9\u8c61(\u7c7b) \u90e8\u5206(\u6210\u5458)\u7684\u5b58\u5728\u7531\u5bf9\u8c61(\u7c7b)\u7ba1\u7406 \u90e8\u5206(\u6210\u5458)\u4e0d\u77e5\u9053\u5bf9\u8c61(\u7c7b)\u7684\u5b58\u5728 \u73b0\u5b9e\u751f\u6d3b\u4e2d\u4e00\u4e2a\u5f88\u597d\u7684\u7ec4\u5408\u4f8b\u5b50\u662f\u4e00\u4e2a\u4eba\u7684\u8eab\u4f53\u548c\u5fc3\u810f\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u8ba9\u6211\u4eec\u66f4\u8be6\u7ec6\u5730\u7814\u7a76\u8fd9\u4e9b\u5185\u5bb9\u3002 \u7ec4\u5408\u5173\u7cfb\u662f\u201c\u90e8\u5206-\u6574\u4f53\u201d\u7684\u5173\u7cfb\uff0c\u5176\u4e2d\u90e8\u5206\u5fc5\u987b\u6784\u6210\u6574\u4e2a\u5bf9\u8c61\u7684\u4e00\u90e8\u5206\u3002\u4f8b\u5982\uff0c\u5fc3\u810f\u662f\u4e00\u4e2a\u4eba\u8eab\u4f53\u7684\u4e00\u90e8\u5206\u3002\u7ec4\u5408\u4e2d\u7684\u90e8\u5206\u53ea\u80fd\u662f\u5c5e\u4e8e \u4e00\u4e2a\u5bf9\u8c61 \u7684\u4e00\u90e8\u5206\u3002\u4f5c\u4e3a\u4e00\u4e2a\u4eba\u8eab\u4f53\u4e00\u90e8\u5206\u7684\u5fc3\u810f\u4e0d\u53ef\u80fd\u540c\u65f6\u662f\u53e6\u4e00\u4e2a\u4eba\u8eab\u4f53\u7684\u4e00\u90e8\u5206\u3002 \u5728\u7ec4\u5408\u5173\u7cfb\u4e2d\uff0c\u5bf9\u8c61\u8d1f\u8d23\u7ba1\u7406\u5404\u4e2a\u90e8\u4ef6\u7684\u5b58\u5728\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u8fd9\u610f\u5473\u7740\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u521b\u5efa\u8be5\u90e8\u4ef6\uff0c\u5728\u9500\u6bc1\u5bf9\u8c61\u65f6\u9500\u6bc1\u8be5\u90e8\u5206\u3002\u4f46\u66f4\u5e7f\u6cdb\u5730\u8bf4\uff0c\u5b83\u610f\u5473\u7740\u5bf9\u8c61\u4ee5\u4e00\u79cd\u4e0d\u9700\u8981\u5bf9\u8c61\u7684\u7528\u6237\u53c2\u4e0e\u7684\u65b9\u5f0f\u7ba1\u7406\u90e8\u4ef6\u7684\u751f\u547d\u5468\u671f\u3002\u4f8b\u5982\uff0c\u5f53\u4e00\u4e2a\u8eab\u4f53\u88ab\u521b\u9020\u51fa\u6765\u65f6\uff0c\u5fc3\u810f\u4e5f\u88ab\u521b\u9020\u51fa\u6765\u4e86\u3002\u5f53\u4e00\u4e2a\u4eba\u7684\u8eab\u4f53\u88ab\u6467\u6bc1\u65f6\uff0c\u4ed6\u7684\u5fc3\u4e5f\u4f1a\u88ab\u6467\u6bc1\u3002\u6b63\u56e0\u4e3a\u5982\u6b64\uff0c\u7ec4\u5408\u6709\u65f6\u88ab\u79f0\u4e3a\u201c\u6b7b\u4ea1\u5173\u7cfb\u201d\u3002 \u6700\u540e\uff0c\u90e8\u5206\u4e0d\u77e5\u9053\u6574\u4f53\u7684\u5b58\u5728\u3002\u4f60\u7684\u5fc3\u5728\u5e78\u798f\u5730\u8fd0\u8f6c\u7740\uff0c\u5374\u6ca1\u6709\u610f\u8bc6\u5230\u5b83\u662f\u4e00\u4e2a\u66f4\u5927\u7ed3\u6784\u7684\u4e00\u90e8\u5206\u3002\u6211\u4eec\u79f0\u4e4b\u4e3a\u5355\u5411\u5173\u7cfb\uff0c\u56e0\u4e3a\u8eab\u4f53\u4e86\u89e3\u5fc3\u810f\uff0c\u4f46\u5fc3\u810f\u4e0d\u4e86\u89e3\u8eab\u4f53\u3002 \u6ce8\u610f\uff0c\u7ec4\u5408\u5173\u7cfb\u4e0e\u90e8\u4ef6\u7684\u53ef\u79fb\u690d\u6027\u65e0\u5173\u3002\u5fc3\u810f\u53ef\u4ee5\u4ece\u4e00\u4e2a\u8eab\u4f53\u79fb\u690d\u5230\u53e6\u4e00\u4e2a\u8eab\u4e0a\u3002\u7136\u800c\uff0c\u5373\u4f7f\u5728\u79fb\u690d\u540e\uff0c\u5b83\u4ecd\u7136\u6ee1\u8db3\u7ec4\u5408\u7684\u5b9a\u4e49(\u5fc3\u810f\u73b0\u5728\u5c5e\u4e8e\u63a5\u53d7\u8005\uff0c\u9664\u975e\u518d\u6b21\u79fb\u690d\uff0c\u5426\u5219\u53ea\u80fd\u662f\u63a5\u53d7\u8005\u5bf9\u8c61\u7684\u4e00\u90e8\u5206)\u3002 \u6211\u4eec\u7684 Fraction \u7c7b\u5c31\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u7ec4\u5408\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 class Fraction { private : int m_numerator ; int m_denominator ; public : Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { } }; \u8fd9\u4e2a\u7c7b\u6709\u4e24\u4e2a\u6570\u636e\u6210\u5458\uff1a\u5206\u5b50\u548c\u5206\u6bcd\u3002\u5206\u5b50\u548c\u5206\u6bcd\u662f\u5206\u6570\u7684\u4e00\u90e8\u5206(\u5305\u542b\u5728\u5206\u6570\u4e2d)\u3002\u5b83\u4eec\u4e0d\u80fd\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u5206\u6570\u3002\u5206\u5b50\u548c\u5206\u6bcd\u5e76\u4e0d\u77e5\u9053\u5b83\u4eec\u662f\u5206\u6570\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u4eec\u53ea\u5305\u542b\u6574\u6570\u3002\u5728\u521b\u5efa Fraction \u5b9e\u4f8b\u65f6\uff0c\u5c06\u521b\u5efa\u5206\u5b50\u548c\u5206\u6bcd\u3002\u5f53\u5206\u6570\u5b9e\u4f8b\u88ab\u7834\u574f\u65f6\uff0c\u5206\u5b50\u548c\u5206\u6bcd\u4e5f\u4f1a\u88ab\u7834\u574f\u3002 \u5c3d\u7ba1\u5bf9\u8c61\u7ec4\u5408\u5173\u7cfb\u63cf\u8ff0\u7684\u662f\u201c\u5177\u6709\u201d\u7684\u5173\u7cfb\uff08\u4eba\u4f53\u6709\u4e00\u4e2a\u5fc3\u810f\u3001\u5206\u6570\u6709\u4e00\u4e2a\u5206\u6bcd\uff09\uff0c\u4f46\u662f\u66f4\u52a0\u7cbe\u786e\u7684\u8bf4\u6cd5\u5176\u5b9e\u662f\uff0c\u7ec4\u5408\u63cf\u8ff0\u7684\u662f\u201c\u90e8\u5206\u4e0e\u6574\u4f53\u201d\u7684\u5173\u7cfb\uff08\u5fc3\u810f\u662f\u4eba\u4f53\u7684\u4e00\u90e8\u5206\u3001\u5206\u5b50\u662f\u5206\u6570\u7684\u4e00\u90e8\u5206\uff09\u3002\u7ec4\u5408\u5173\u7cfb\u901a\u5e38\u7528\u4e8e\u5bf9\u4e00\u4e2a\u7269\u7406\u5173\u7cfb\u8fdb\u884c\u5efa\u6a21\uff0c\u5373\u4e00\u4e2a\u5bf9\u8c61\u88ab\u5305\u542b\u5728\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61\u4e2d\u3002 \u201c\u90e8\u5206\u201d\u5173\u7cfb\u53ef\u4ee5\u662f\u201c\u4e00\u5bf9\u4e00\u7684\u201d\uff0c\u4e5f\u53ef\u4ee5\u662f\u201c\u591a\u5bf9\u4e00\u7684\u201d\u2014\u2014\u4f8b\u5982\uff0c\u8eab\u4f53\u4e2d\u5305\u542b\u4e00\u4e2a\u5fc3\u810f\uff0c\u4f46\u662f\u8eab\u4f53\u53ef\u4ee5\u5305\u542b10\u4e2a\u624b\u6307\uff08\u6700\u7ec8\u88ab\u5b9e\u73b0\u4e3a\u6570\u7ec4\uff09\u3002","title":"\u7ec4\u5408"},{"location":"16-2-composition/#_4","text":"\u7ec4\u5408\u662fC++\u4e2d\u6700\u5bb9\u6613\u5b9e\u73b0\u7684\u5173\u7cfb\u7c7b\u578b\u4e4b\u4e00\u3002\u5b83\u4eec\u901a\u5e38\u88ab\u521b\u5efa\u4e3a\u5177\u6709\u666e\u901a\u6570\u636e\u6210\u5458\u7684\u7ed3\u6784\u6216\u7c7b\u3002\u56e0\u4e3a\u8fd9\u4e9b\u6570\u636e\u6210\u5458\u76f4\u63a5\u4f5c\u4e3a\u7ed3\u6784/\u7c7b\u7684\u4e00\u90e8\u5206\u5b58\u5728\uff0c\u6240\u4ee5\u5b83\u4eec\u7684\u751f\u547d\u5468\u671f\u4e0e\u7c7b\u5b9e\u4f8b\u672c\u8eab\u7684\u751f\u547d\u5468\u671f\u7ed1\u5b9a\u5728\u4e00\u8d77\u3002 \u9700\u8981\u8fdb\u884c\u52a8\u6001\u5206\u914d\u6216\u56de\u6536\u7684\u7ec4\u5408\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\u6570\u636e\u6210\u5458\u5b9e\u73b0\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7ec4\u5408\u7c7b\u5e94\u8be5\u81ea\u5df1\u8d1f\u8d23\u6240\u6709\u5fc5\u8981\u7684\u5185\u5b58\u7ba1\u7406(\u800c\u4e0d\u662f\u7c7b\u7684\u7528\u6237)\u3002 \u4e00\u822c\u6765\u8bf4\uff0c\u5982\u679c\u4f60\u80fd\u4f7f\u7528\u590d\u5408\u8bbe\u8ba1\u4e00\u4e2a\u7c7b\uff0c\u4f60\u5c31\u5e94\u8be5\u4f7f\u7528\u7ec4\u5408\u5173\u7cfb\u8bbe\u8ba1\u4e00\u4e2a\u7c7b\u3002\u4f7f\u7528\u7ec4\u5408\u8bbe\u8ba1\u7684\u7c7b\u662f\u76f4\u63a5\u7684\u3001\u7075\u6d3b\u7684\u548c\u5065\u58ee\u7684(\u56e0\u4e3a\u5b83\u4eec\u5f88\u597d\u5730\u81ea\u884c\u6e05\u7406)\u3002","title":"\u5b9e\u73b0\u7ec4\u5408\u5173\u7cfb"},{"location":"16-2-composition/#_5","text":"\u8bb8\u591a\u6e38\u620f\u90fd\u6709\u5728\u68cb\u76d8\u3001\u5730\u56fe\u6216\u5c4f\u5e55\u4e0a\u79fb\u52a8\u7684\u751f\u7269\u6216\u7269\u4f53\u3002\u6240\u6709\u8fd9\u4e9b\u751f\u7269/\u7269\u4f53\u7684\u4e00\u4e2a\u5171\u540c\u70b9\u662f\uff0c\u5b83\u4eec\u90fd\u6709\u4e00\u4e2a\u4f4d\u7f6e\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u751f\u7269\u7c7b\uff0c\u5b83\u4f7f\u7528\u4e00\u4e2a Point \u7c7b\u6765\u4fdd\u5b58\u751f\u7269\u7684\u4f4d\u7f6e\u3002 \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u8bbe\u8ba1 Point \u7c7b\u3002\u751f\u7269\u5c06\u751f\u6d3b\u57282d\u4e16\u754c\u4e2d\uff0c\u6240\u4ee5 Point \u7c7b\u53ea\u9700\u8981\u6709\u4e24\u4e2a\u7ef4\u5ea6 x \u548c y \u3002\u6211\u4eec\u5047\u8bbe\u4e16\u754c\u662f\u7531\u79bb\u6563\u7684\u6b63\u65b9\u5f62\u7ec4\u6210\uff0c\u6240\u4ee5\u8fd9\u4e9b\u7ef4\u5ea6\u603b\u662f\u6574\u6570\u3002 Point2D.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #ifndef POINT2D_H #define POINT2D_H #include class Point2D { private : int m_x ; int m_y ; public : // \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 Point2D () : m_x { 0 }, m_y { 0 } { } // \u6709\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570 Point2D ( int x , int y ) : m_x { x }, m_y { y } { } // \u91cd\u8f7d\u7684\u8f93\u51fa\u64cd\u4f5c\u7b26 friend std :: ostream & operator << ( std :: ostream & out , const Point2D & point ) { out << '(' << point . m_x << \", \" << point . m_y << ')' ; return out ; } // \u6210\u5458\u8bbf\u95ee\u51fd\u6570 void setPoint ( int x , int y ) { m_x = x ; m_y = y ; } }; #endif \u6ce8\u610f\uff0c\u56e0\u4e3a\u6240\u6709\u51fd\u6570\u7684\u5b9e\u73b0\u90fd\u5728\u5934\u6587\u4ef6\u4e2d\uff08\u4e3a\u4e86\u4fdd\u6301\u4f8b\u5b50\u7b80\u77ed\uff09\uff0c\u6240\u4ee5\u5e76\u6ca1\u6709 Point2D.cpp \u6587\u4ef6\u3002 Point2d \u662f\u5b83\u7684\u90e8\u4ef6\u7684\u7684\u7ec4\u5408\uff1a\u4f4d\u7f6e\u4fe1\u606fx\u548cy\u90fd\u662f Point2D \u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u5b83\u4eec\u7684\u751f\u547d\u5468\u671f\u4e5f\u548c Point2D \u5b9e\u4f8b\u7ed1\u5b9a\u5728\u4e00\u8d77\u3002 \u73b0\u5728\uff0c\u5b9a\u4e49 Creature \u7c7b\u3002 Creature \u6709\u5982\u4e0b\u51e0\u4e2a\u5c5e\u6027\uff1a\u540d\u79f0\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4f4d\u7f6e\u5219\u662f\u4e00\u4e2a Point2D \u7c7b\u3002 Creature.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #ifndef CREATURE_H #define CREATURE_H #include #include #include \"Point2D.h\" class Creature { private : std :: string m_name ; //\u540d\u79f0 Point2D m_location ; //\u4f4d\u7f6e public : Creature ( const std :: string & name , const Point2D & location ) : m_name { name }, m_location { location } { } friend std :: ostream & operator << ( std :: ostream & out , const Creature & creature ) { out << creature . m_name << \" is at \" << creature . m_location ; return out ; } void moveTo ( int x , int y ) { m_location . setPoint ( x , y ); } }; #endif Creature \u540c\u6837\u4e5f\u662f\u5176\u5404\u90e8\u5206\u7684\u7ec4\u5408\u3002\u751f\u7269\u7684\u540d\u5b57\u548c\u4f4d\u7f6e\u5176\u58f0\u660e\u5468\u671f\u90fd\u7ed1\u5b9a\u5230\u4e86 Creature \u3002 \u6700\u7ec8\u7684\u7a0b\u5e8f\u662f\u8fd9\u6837\u7684\u3002 main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include #include #include \"Creature.h\" #include \"Point2D.h\" int main () { std :: cout << \"Enter a name for your creature: \" ; std :: string name ; std :: cin >> name ; Creature creature { name , { 4 , 7 } }; while ( true ) { // print the creature's name and location std :: cout << creature << '\\n' ; std :: cout << \"Enter new X location for creature (-1 to quit): \" ; int x { 0 }; std :: cin >> x ; if ( x == -1 ) break ; std :: cout << \"Enter new Y location for creature (-1 to quit): \" ; int y { 0 }; std :: cin >> y ; if ( y == -1 ) break ; creature . moveTo ( x , y ); } return 0 ; } \u8fd0\u884c\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 Enter a name for your creature: Marvin Marvin is at (4, 7) Enter new X location for creature (-1 to quit): 6 Enter new Y location for creature (-1 to quit): 12 Marvin is at (6, 12) Enter new X location for creature (-1 to quit): 3 Enter new Y location for creature (-1 to quit): 2 Marvin is at (3, 2) Enter new X location for creature (-1 to quit): -1","title":"\u66f4\u591a\u7684\u4f8b\u5b50"},{"location":"16-2-composition/#_6","text":"\u5c3d\u7ba1\u5927\u591a\u6570\u7ec4\u5408\u4f1a\u5728\u7ec4\u5408\u5173\u7cfb\u5efa\u7acb\u65f6\uff0c\u76f4\u63a5\u521b\u5efa\u5b83\u4eec\u7684\u90e8\u5206\uff0c\u5e76\u5728\u7ec4\u5408\u88ab\u7834\u574f\u65f6\u76f4\u63a5\u9500\u6bc1\u5b83\u4eec\u7684\u90e8\u5206\uff0c\u4f46\u4e5f\u6709\u4e00\u4e9b\u4e0d\u540c\u7c7b\u578b\u7684\u7ec4\u5408\u4e0d\u6ee1\u8db3\u8fd9\u4e00\u89c4\u5219\u3002 \u4f8b\u5982\uff1a \u7ec4\u5408\u4e5f\u53ef\u4ee5\u5148\u4e0d\u521b\u5efa\u90e8\u5206\uff0c\u800c\u662f\u5728\u9700\u8981\u4f7f\u7528\u5b83\u4eec\u65f6\u5728\u521b\u5efa\u3002\u4f8b\u5982\uff0c\u5b57\u7b26\u4e32\u7c7b\u53ef\u4ee5\u5148\u4e0d\u52a8\u6001\u5206\u914d\u5b57\u7b26\u6570\u7ec4\uff0c\u5f53\u771f\u6b63\u6709\u6570\u636e\u8981\u5b58\u653e\u65f6\u518d\u521b\u5efa\uff1b \u7ec4\u5408\u4f7f\u7528\u7684\u90e8\u5206\u53ef\u4ee5\u662f\u88ab\u4f20\u5165\u7684\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u5b83\u81ea\u5df1\u521b\u5efa\u7684\uff1b \u7ec4\u5408\u4e5f\u53ef\u4ee5\u628a\u9500\u6bc1\u90e8\u5206\u7684\u5de5\u4f5c\u59d4\u6d3e\u7ed9\u5176\u4ed6\u5bf9\u8c61\uff08\u4f8b\u5982\uff0c\u5783\u573e\u56de\u6536\u7a0b\u5e8f\uff09\u3002 \u8fd9\u91cc\u7684\u5173\u952e\u70b9\u662f\uff0c\u7ec4\u5408\u5e94\u8be5\u7ba1\u7406\u5b83\u7684\u5404\u4e2a\u90e8\u5206\uff0c\u800c\u4e0d\u9700\u8981\u7ec4\u5408\u7684\u7528\u6237\u7ba1\u7406\u4efb\u4f55\u4e1c\u897f\u3002","title":"\u4e0d\u540c\u7c7b\u578b\u7684\u7ec4\u5408"},{"location":"16-2-composition/#_7","text":"\u5f53\u6d89\u53ca\u5230\u5bf9\u8c61\u7ec4\u5408\u65f6\uff0c\u65b0\u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u95ee\u7684\u4e00\u4e2a\u95ee\u9898\u662f\uff0c\u201c\u4ec0\u4e48\u65f6\u5019\u6211\u5e94\u8be5\u4f7f\u7528\u7c7b\u6210\u5458\u800c\u4e0d\u662f\u76f4\u63a5\u4f7f\u7528\u67d0\u4e2a\u7279\u6027\uff1f\u201d\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4e0d\u4f7f\u7528 Point2D``\u7c7b\u6765\u5b9e\u73b0Creature \u7684\u4f4d\u7f6e\uff0c\u800c\u662f\u5411 Creature \u7c7b\u4e2d\u76f4\u63a5\u6dfb\u52a02\u4e2a\u6574\u6570\uff0c\u5e76\u7f16\u5199\u4ee3\u7801\u6765\u5904\u7406\u5b9a\u4f4d\u3002\u7136\u800c\uff0c\u8ba9 Point2D \u6210\u4e3a\u72ec\u7acb\u7684\u7c7b(\u5e76\u6210\u4e3a Creature \u7684\u7684\u6210\u5458)\u6709\u5f88\u591a\u597d\u5904\uff1a \u6bcf\u4e2a\u72ec\u7acb\u7684\u7c7b\u80fd\u591f\u5c3d\u53ef\u80fd\u5730\u4fdd\u6301\u7b80\u6d01\uff0c\u4e13\u6ce8\u4e8e\u5b83\u8981\u5b8c\u6210\u7684\u4efb\u52a1\u3002\u8fd9\u4f7f\u5f97\u8fd9\u4e9b\u7c7b\u66f4\u5bb9\u6613\u7f16\u5199\u3001\u4e5f\u66f4\u5bb9\u6613\u7406\u89e3\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ea\u4e13\u6ce8\u4e00\u4e2a\u529f\u80fd\u3002\u4f8b\u5982\uff0c Point2D \u53ea\u9700\u8981\u5173\u6ce8\u548c\u201c\u70b9\u201d\u76f8\u5173\u7684\u529f\u80fd\uff0c\u663e\u7136\u6709\u52a9\u4e8e\u4fdd\u6301\u7c7b\u7684\u7b80\u6d01\u3002 \u6bcf\u4e2a\u7c7b\u90fd\u662f\u81ea\u5305\u542b\u7684\uff0c\u6240\u4ee5\u5b83\u4eec\u80fd\u591f\u88ab\u590d\u7528\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5176\u4ed6\u5e94\u7528\u4e2d\u590d\u7528 Point2D \u8fd9\u4e2a\u7c7b\u3002\u800c\u4e14\uff0c\u5982\u679c creature \u9700\u8981\u53e6\u5916\u4e00\u4e2a\u70b9\uff08\u4f8b\u5982\uff0c\u8868\u793a\u8981\u53bb\u7684\u76ee\u6807\u70b9\uff09\uff0c\u6b64\u65f6\u53ea\u9700\u8981\u5411\u7c7b\u4e2d\u518d\u6dfb\u52a0\u4e00\u4e2a Point2D \u6210\u5458\u53d8\u91cf\u5373\u53ef\uff1b \u5916\u5c42\u7c7b\u53ef\u4ee5\u5229\u7528\u5176\u7c7b\u6210\u5458\u5b8c\u6210\u66f4\u590d\u6742\u7684\u4efb\u52a1\uff0c\u800c\u4e0d\u9700\u8981\u5728\u4e0d\u540c\u7684\u6210\u5458\u95f4\u534f\u8c03\u6570\u636e\u6d41\uff0c\u8fd9\u663e\u7136\u6709\u52a9\u4e8e\u964d\u4f4e\u5916\u5c42\u7c7b\u7684\u6574\u4f53\u590d\u6742\u5ea6\uff0c\u56e0\u4e3a\u5b83\u5c06\u4efb\u52a1\u59d4\u6d3e\u7ed9\u4e86\u5176\u6210\u5458\uff0c\u4e14\u8be5\u6210\u5458\u5df2\u7ecf\u77e5\u9053\u5e94\u8be5\u5982\u4f55\u5b8c\u6210\u76f8\u5173\u7684\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u5728\u9700\u8981\u79fb\u52a8 Creature \u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u5c06\u4efb\u52a1\u59d4\u6d3e\u7ed9 Point \u7c7b\uff0c\u800c\u8be5\u7c7b\u662f\u77e5\u9053\u5982\u4f55\u8bbe\u7f6e\u4e00\u4e2a\u70b9\u7684\u3002\u56e0\u6b64 Creature \u7c7b\u5728\u5b9e\u73b0\u7684\u65f6\u5019\uff0c\u5c31\u65e0\u9700\u64cd\u5fc3\u8fd9\u4e9b\u95ee\u9898\u4e86\u3002 \u5c0f\u8d34\u58eb \u6839\u636e\u7ecf\u9a8c\u6cd5\u5219\uff0c\u4e00\u4e2a\u7c7b\u5e94\u8be5\u88ab\u8bbe\u8ba1\u4e3a\u4ec5\u5b8c\u6210\u4e00\u4e2a\u4efb\u52a1\u3002\u8fd9\u4e2a\u4efb\u52a1\u8981\u4e48\u662f\u5b58\u50a8\u6216\u4fee\u6539\u67d0\u79cd\u6570\u636e\uff08\u4f8b\u5982\uff0c Point2D \u6216 std::string )\uff0c\u8981\u4e48\u662f\u534f\u8c03\u5b83\u7684\u6210\u5458\u5b8c\u6210\u67d0\u4e9b\u4efb\u52a1\u3002\u4f46\u7406\u60f3\u60c5\u51b5\u4e0b\u4e0d\u5e94\u8be5\u540c\u65f6\u505a\u4e0a\u9762\u4e24\u4ef6\u4e8b\u3002 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c Creature \u7684\u786e\u4e0d\u5e94\u8be5\u64cd\u5fc3 Points \u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff0c\u4e5f\u4e0d\u9700\u8981\u5173\u5fc3name\u662f\u5982\u4f55\u50a8\u5b58\u7684\u3002 Creature \u7684\u4efb\u52a1\u4e0d\u662f\u4e86\u89e3\u8fd9\u4e9b\u7ec6\u8282\uff0c\u800c\u662f\u5982\u4f55\u8981\u5173\u6ce8\u5982\u4f55\u534f\u8c03\u6570\u636e\u6d41\u5e76\u786e\u4fdd\u5176\u6bcf\u4e2a\u6210\u5458\u90fd\u77e5\u9053\u5b83\u4eec\u8981\u505a\u4ec0\u4e48\u3002\u800c\u5982\u4f55\u505a\uff0c\u5219\u7531\u6210\u5458\u81ea\u5df1\u6765\u8d1f\u8d23\u3002","title":"\u7ec4\u5408\u548c\u7c7b\u6210\u5458"},{"location":"16-3-aggregation/","tags":["object","aggregation"],"text":"Key Takeaway \u805a\u5408\u5173\u7cfb\u4e2d\u90e8\u5206\u53ef\u4ee5\u5c5e\u4e8e\u591a\u4e2a\u6574\u4f53\uff0c\u5176\u751f\u547d\u5468\u671f\u4e5f\u72ec\u7acb\u4e8e\u6574\u4f53 \u5b9e\u73b0\u805a\u5408\u5173\u7cfb\u662f\u57fa\u4e8e\u6307\u9488\u548c\u5f15\u7528\u7684\uff0c\u5176\u4ed6\u5730\u65b9\u90fd\u548c\u7ec4\u5408\u5173\u7cfb\u4e00\u6837 \u5728 16.2 - \u7ec4\u5408\u5173\u7cfb \u4e2d\u6211\u4eec\u6307\u51fa\uff0c\u5bf9\u8c61\u7ec4\u5408\u662f\u57fa\u4e8e\u7b80\u5355\u5bf9\u8c61\u521b\u5efa\u590d\u6742\u5bf9\u8c61\u7684\u4e00\u79cd\u65b9\u6cd5\u3002\u540c\u65f6\uff0c\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86\u4e00\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\u7ec4\u5408\u2014\u2014\u7ec4\u5408\u3002\u5728\u7ec4\u5408\u5173\u7cfb\u4e2d\uff0c\u6574\u4f53\u5bf9\u8c61\u8981\u8d1f\u8d23\u7ba1\u7406\u90e8\u5206\u5bf9\u8c61\u7684\u5b58\u5728\u6027\u3002 \u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ecb\u7ecd\u53e6\u5916\u4e00\u79cd\u5bf9\u8c61\u7ec4\u5408\u2014\u2014\u805a\u5408\u3002 \u805a\u5408 \u805a\u5408\u5173\u7cfb\u8981\u6ee1\u8db3\u5982\u4e0b\u51e0\u4e2a\u6761\u4ef6\uff1a \u90e8\u5206\uff08\u6210\u5458\uff09\u662f\u6574\u4f53\u5bf9\u8c61\u7684\u4e00\u90e8\u5206\uff1b \u90e8\u5206\uff08\u6210\u5458\uff09\u53ef\u4ee5\u4e0d\u53ea\u5c5e\u4e8e\u4e00\u4e2a\u6574\u4f53\u5bf9\u8c61\uff1b \u90e8\u5206\uff08\u6210\u5458\uff09\u7684\u751f\u547d\u5468\u671f\u4e0d\u7531\u6574\u4f53\u5bf9\u8c61\u7ba1\u7406\uff1b \u90e8\u5206\uff08\u6210\u5458\uff09\u4e0d\u9700\u8981\u77e5\u9053\u6574\u4f53\u5bf9\u8c61\u7684\u5b58\u5728\u6027\uff1b \u548c\u7ec4\u5408\u7c7b\u4f3c\uff0c\u805a\u5408\u4e5f\u662f\u4e00\u79cd\u201d\u90e8\u5206\u4e0e\u6574\u4f53\u201c\u7684\u5173\u7cfb\uff0c\u90e8\u5206\u88ab\u5305\u542b\u5728\u6574\u4f53\u4e2d\uff0c\u800c\u4e14\u8fd9\u662f\u4e00\u79cd\u5355\u5411\u7684\u5173\u7cfb\u3002\u4f46\u662f\uff0c\u548c\u7ec4\u5408\u4e0d\u540c\uff0c\u805a\u5408\u5bf9\u8c61\u53ef\u4ee5\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u6574\u4f53\uff0c\u800c\u4e14\u6574\u4f53\u5bf9\u8c61\u4e0d\u9700\u8981\u8d1f\u8d23\u7ba1\u7406\u90e8\u5206\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u3002\u5f53\u805a\u5408\u5173\u7cfb\u88ab\u521b\u5efa\u65f6\uff0c\u6574\u4f53\u4e0d\u8d1f\u8d23\u521b\u5efa\u90e8\u5206\u3002\u5f53\u805a\u5408\u5173\u7cfb\u88ab\u7834\u574f\u65f6\uff0c\u6574\u4f53\u4e5f\u4e0d\u8d1f\u8d23\u9500\u6bc1\u90e8\u5206\u3002 \u4e3e\u4f8b\u6765\u8bf4\uff0c\u8003\u8651\u67d0\u4e2a\u4eba\u548c\u5b83\u4f4f\u5740\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u4e00\u4e2a\u4eba\u6709\u4e00\u4e2a\u4f4f\u5740\u3002\u4f46\u662f\u4f4f\u5740\u53ef\u4ee5\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u4eba\uff0c\u4f8b\u5982\u4f60\u548c\u4f60\u7684\u5ba4\u53cb\uff0c\u6216\u8005\u662f\u5176\u4ed6\u4eba\u3002\u800c\u4e14\uff0c\u4f4f\u5740\u4e5f\u4e0d\u662f\u88ab\u4eba\u7ba1\u7406\u7684\u2014\u2014\u5176\u5730\u5740\u751a\u81f3\u65e9\u4e8e\u5c45\u4f4f\u4eba\u5c31\u5b58\u5728\u4e86\uff0c\u800c\u4e14\u4eba\u4e0d\u5728\u7684\u65f6\u5019\u5730\u5740\u4ecd\u7136\u4f1a\u7ee7\u7eed\u5b58\u5728\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4e0a\u9762\u63cf\u8ff0\u7684\u662f\u4e00\u79cd\u805a\u5408\u5173\u7cfb\u3002 \u53e6\u5916\uff0c\u8003\u8651\u6c7d\u8f66\u548c\u5f15\u64ce\u7684\u5173\u7cfb\u3002\u6c7d\u8f66\u7684\u5f15\u64ce\u5c5e\u4e8e\u6c7d\u8f66\u7684\u4e00\u90e8\u5206\u3002\u4f46\u662f\u5c3d\u7ba1\u5f15\u64ce\u662f\u6c7d\u8f66\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u4e5f\u53ef\u4ee5\u5c5e\u4e8e\u5176\u4ed6\u4eba\u6216\u7269\u3002\u4f8b\u5982\uff0c\u5f15\u64ce\u4e5f\u5c5e\u4e8e\u8f66\u4e3b\u3002\u6c7d\u8f66\u672c\u8eab\u5e76\u4e0d\u8d1f\u8d23\u521b\u5efa\u6216\u9500\u6bc1\u5f15\u64ce\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u5c3d\u7ba1\u8f66\u8f86\u77e5\u9053\u5b83\u4f1a\u6709\u4e00\u4e2a\u5f15\u64ce\uff0c\u4f46\u662f\u5f15\u64ce\u5374\u4e0d\u77e5\u9053\u5b83\u662f\u6c7d\u8f66\u7684\u4e00\u90e8\u5206\u3002 \u5f53\u6211\u4eec\u8ba8\u8bba\u5bf9\u7269\u7406\u5bf9\u8c61\u5efa\u6a21\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u201d\u9500\u6bc1\u201c\u4e00\u8bcd\u4f1a\u6bd4\u8f83\u5947\u602a\u3002\u6b64\u65f6\u6709\u7684\u4eba\u53ef\u80fd\u4f1a\u62ac\u6760\u8bf4\uff0c\u5982\u679c\u4e00\u4e2a\u9668\u77f3\u649e\u51fb\u5730\u7403\u6b63\u597d\u7838\u5230\u4e86\u4f60\u7684\u8f66\uff0c\u90a3\u5c82\u4e0d\u662f\u8f66\u548c\u5f15\u64ce\u4e00\u8d77\u88ab\u9500\u6bc1\u4e86\uff1f\u5f53\u7136\uff0c\u4e0d\u8fc7\u8fd9\u662f\u9668\u77f3\u7684\u9519\u3002\u8fd9\u91cc\u7684\u5173\u952e\u70b9\u5728\u4e8e\uff0c\u6c7d\u8f66\u4e0d\u8d1f\u8d23\u9500\u6bc1\u5176\u90e8\u4ef6\uff08\u5916\u529b\u662f\u53ef\u80fd\u7684\uff09\u3002 \u6211\u4eec\u53ef\u4ee5\u8bf4\uff0c\u805a\u5408\u6a21\u578b\u662f\u4e00\u79cd\u201d\u6709\u4e00\u4e2a\u201c\u7684\u5173\u7cfb\uff08\u9662\u7cfb\u6709\u4e00\u4e2a\u8001\u5e08\u3001\u6c7d\u8f66\u6709\u4e00\u4e2a\u5f15\u64ce\uff09\u3002 \u7c7b\u4f3c\u7ec4\u5408\u5173\u7cfb\uff0c\u805a\u5408\u4e2d\u7684\u90e8\u5206\u53ef\u4ee5\u662f\u4e00\u4e2a\u4e5f\u53ef\u4ee5\u662f\u591a\u4e2a\u3002 \u5b9e\u73b0\u805a\u5408\u5173\u7cfb \u56e0\u4e3a\u805a\u5408\u7c7b\u4f3c\u4e8e\u7ec4\u5408\uff0c\u5b83\u4eec\u90fd\u662f\u90e8\u5206-\u6574\u4f53\u5173\u7cfb\uff0c\u6240\u4ee5\u5b83\u4eec\u7684\u5b9e\u73b0\u51e0\u4e4e\u5b8c\u5168\u76f8\u540c\uff0c\u5b83\u4eec\u4e4b\u95f4\u7684\u533a\u522b\u4e3b\u8981\u662f\u8bed\u4e49\u4e0a\u7684\u3002\u5728\u7ec4\u5408\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u666e\u901a\u6210\u5458\u53d8\u91cf(\u6216\u7531\u7ec4\u5408\u7c7b\u5904\u7406\u5206\u914d\u548c\u56de\u6536\u8fc7\u7a0b\u7684\u6307\u9488)\u5c06\u90e8\u4ef6\u6dfb\u52a0\u5230\u7ec4\u5408\u4e2d\u3002 \u5728\u805a\u5408\u4e2d\uff0c\u6211\u4eec\u4ecd\u7136\u4f7f\u7528\u6210\u5458\u53d8\u91cf\u8868\u793a\u90e8\u5206\u5bf9\u8c61\u3002\u4f46\u662f\uff0c \u8fd9\u4e9b\u6210\u5458\u53d8\u91cf\u901a\u5e38\u662f\u5f15\u7528\u6216\u6307\u9488 \uff0c\u7528\u4e8e\u6307\u5411\u5728\u7c7b\u7684\u4f5c\u7528\u57df\u4e4b\u5916\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u805a\u5408\u901a\u5e38\u8981\u4e48\u63a5\u53d7\u5b83\u5c06\u8981\u6307\u5411\u7684\u5bf9\u8c61\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\u53c2\u6570\uff0c\u8981\u4e48\u4ee5\u7a7a\u5f00\u59cb\uff0c\u7136\u540e\u901a\u8fc7\u8bbf\u95ee\u51fd\u6570\u6216\u64cd\u4f5c\u7b26\u6dfb\u52a0\u5b50\u5bf9\u8c61\u3002 \u56e0\u4e3a\u8fd9\u4e9b\u90e8\u5206\u5bf9\u8c61\u5b58\u5728\u4e8e\u7c7b\u7684\u4f5c\u7528\u57df\u4e4b\u5916\uff0c\u6240\u4ee5\u5f53\u7c7b\u88ab\u9500\u6bc1\u65f6\uff0c\u6307\u9488\u6216\u5f15\u7528\u6210\u5458\u53d8\u91cf\u5c06\u88ab\u9500\u6bc1(\u4f46\u4e0d\u4f1a\u88ab\u5220\u9664)\u3002\u56e0\u6b64\uff0c\u8fd9\u4e9b\u90e8\u5206\u672c\u8eab\u4ecd\u7136\u5b58\u5728\u3002 \u8ba9\u6211\u4eec\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e2a\u201c\u6559\u5e08\u548c\u9662\u7cfb\u201d\u7684\u793a\u4f8b\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u505a\u4e00\u4e9b\u7b80\u5316\uff1a\u9996\u5148\uff0c\u9662\u7cfb\u5c06\u53ea\u5bb9\u7eb3\u4e00\u540d\u6559\u5e08\u3002\u5176\u6b21\uff0c\u8001\u5e08\u4e0d\u77e5\u9053\u4ed6\u4eec\u662f\u54ea\u4e2a\u9662\u7cfb\u7684\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include #include class Teacher { private : std :: string m_name {}; public : Teacher ( const std :: string & name ) : m_name { name } { } const std :: string & getName () const { return m_name ; } }; class Department { private : const Teacher & m_teacher ; // This dept holds only one teacher for simplicity, but it could hold many teachers public : Department ( const Teacher & teacher ) : m_teacher { teacher } { } }; int main () { // Create a teacher outside the scope of the Department Teacher bob { \"Bob\" }; // create a teacher { // Create a department and use the constructor parameter to pass // the teacher to it. Department department { bob }; } // department goes out of scope here and is destroyed // bob still exists here, but the department doesn't std :: cout << bob . getName () << \" still exists! \\n \" ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d bob \u7684\u521b\u5efa\u662f\u72ec\u7acb\u4e8e department \u7684\uff0c\u5b83\u901a\u8fc7\u6784\u9020\u51fd\u6570\u88ab\u4f20\u9012\u7ed9\u4e86 department \u3002\u5f53 department \u9500\u6bc1\u7684\u65f6\u5019\u5f15\u7528 m_teacher \u88ab\u9500\u6bc1\u4e86\uff0c\u4f46\u662f teacher \u672c\u8eab\u5e76\u6ca1\u6709\u88ab\u9500\u6bc1\uff0c\u5b83\u4ecd\u7136\u5b58\u5728\u5e76\u4e14\u5728 main() \u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u3002 \u5bf9\u8c61\u5efa\u6a21\u65f6\u5e94\u5f53\u9009\u62e9\u6b63\u786e\u7684\u5bf9\u8c61\u5173\u7cfb \u5c3d\u7ba1\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6559\u5e08\u4e0d\u77e5\u9053\u4ed6\u4eec\u5728\u4e3a\u54ea\u4e2a\u7cfb\u5de5\u4f5c\uff0c\u8fd9\u542c\u8d77\u6765\u6709\u70b9\u8822\uff0c\u4f46\u5728\u7ed9\u5b9a\u7684\u9879\u76ee\u4e0a\u4e0b\u6587\u4e2d\uff0c\u8fd9\u53ef\u80fd\u5b8c\u5168\u6ca1\u95ee\u9898\u3002\u5f53\u51b3\u5b9a\u5b9e\u73b0\u67d0\u79cd\u7c7b\u578b\u7684\u5173\u7cfb\u65f6\uff0c\u5b9e\u73b0\u6ee1\u8db3\u4f60\u9700\u6c42\u7684\u6700\u7b80\u5355\u7684\u5173\u7cfb\uff0c\u800c\u4e0d\u662f\u770b\u8d77\u6765\u6700\u7b26\u5408\u5b9e\u9645\u73af\u5883\u7684\u5173\u7cfb\u3002 \u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u8f66\u95f4\u6a21\u62df\u5668\uff0c\u5219\u5c06\u6c7d\u8f66\u548c\u5f15\u64ce\u4f5c\u4e3a\u4e00\u4e2a\u805a\u5408\u662f\u6070\u5f53\u7684\uff0c\u8fd9\u6837\u5f15\u64ce\u5c31\u53ef\u4ee5\u88ab\u4ece\u6c7d\u8f66\u4e2d\u79fb\u9664\u5e76\u653e\u5728\u67d0\u4e2a\u67b6\u5b50\u4e0a\u4ee5\u5907\u4ee5\u540e\u4f7f\u7528\u3002\u7136\u800c\uff0c\u5982\u679c\u4f60\u6b63\u5728\u7f16\u5199\u6a21\u62df\u8d5b\u8f66\u6e38\u620f\uff0c\u90a3\u53ef\u80fd\u5c06\u6c7d\u8f66\u548c\u5f15\u64ce\u4f5c\u4e3a\u7ec4\u5408\u5b9e\u73b0\u66f4\u6070\u5f53\uff0c\u56e0\u4e3a\u5728\u8be5\u8bed\u5883\u4e2d\uff0c\u5f15\u64ce\u4e0d\u4f1a\u72ec\u7acb\u5b58\u5728\u4e8e\u6c7d\u8f66\u4e4b\u5916\u3002 \u6700\u4f73\u5b9e\u8df5 \u5b9e\u73b0\u6ee1\u8db3\u7a0b\u5e8f\u9700\u8981\u7684\u6700\u7b80\u5355\u7684\u5173\u7cfb\u7c7b\u578b\uff0c\u800c\u4e0d\u662f\u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\u770b\u8d77\u6765\u6b63\u786e\u7684\u5173\u7cfb\u7c7b\u578b\u3002 \u5c0f\u7ed3\uff1a\u7ec4\u5408\u548c\u805a\u5408 \u7ec4\u5408: \u901a\u5e38\u4f7f\u7528\u666e\u901a\u7684\u6210\u5458\u53d8\u91cf\uff1b \u5982\u679c\u7c7b\u81ea\u5df1\u7ba1\u7406\u6210\u5458\u5bf9\u8c61\u7684\u5185\u5b58\u7533\u8bf7\u91ca\u653e\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\uff1b \u8d1f\u8d23\u90e8\u5206\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u9500\u6bc1\u3002 \u805a\u5408: \u901a\u5e38\u4f7f\u7528\u6307\u9488\u548c\u5f15\u7528\u6210\u5458\u5e76\u6307\u5411\u5b58\u5728\u4e8e\u6574\u4f53\u5bf9\u8c61\u4f5c\u7528\u57df\u4e4b\u5916\u7684\u5bf9\u8c61\uff1b \u4e0d\u8d1f\u8d23\u90e8\u5206\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u9500\u6bc1\u3002 \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7ec4\u5408\u548c\u805a\u5408\u7684\u6982\u5ff5\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u7c7b\u4e2d\u81ea\u7531\u6df7\u5408\u4f7f\u7528 \u3002\u7f16\u5199\u4e00\u4e2a\u8d1f\u8d23\u521b\u5efa/\u9500\u6bc1\u67d0\u4e9b\u90e8\u5206\u800c\u4e0d\u8d1f\u8d23\u5176\u4ed6\u90e8\u5206\u7684\u7c7b\u662f\u53ef\u4ee5\u7684\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u7684 Department \u7c7b\u53ef\u4ee5\u6709\u4e00\u4e2a name \u548c\u4e00\u4e2a Teacher \u6210\u5458\u3002\u8fd9\u4e2a name \u901a\u5e38\u662f\u4f5c\u4e3a\u7ec4\u5408\u5173\u7cfb\u6dfb\u52a0\u5230\u8be5\u7c7b\u4e2d\u7684\u5e76\u4e14\u548c Department \u4e00\u8d77\u521b\u5efa\u548c\u9500\u6bc1\u3002\u53e6\u4e00\u65b9\u9762\uff0c Teacher \u5c06\u901a\u8fc7\u805a\u5408\u7684\u65b9\u5f0f\u6dfb\u52a0\u5230 Department \u4e2d\uff0c\u5e76\u72ec\u7acb\u5730\u521b\u5efa/\u9500\u6bc1\u3002 \u867d\u7136\u805a\u5408\u53ef\u80fd\u975e\u5e38\u6709\u7528\uff0c\u4f46\u5b83\u4eec\u4e5f\u66f4\u5371\u9669\uff0c\u56e0\u4e3a\u805a\u5408\u4e0d\u5904\u7406\u5176\u90e8\u5206\u5bf9\u8c61\u7684\u56de\u6536\uff0c\u5176\u9500\u6bc1\u64cd\u4f5c\u88ab\u5206\u914d\u5916\u90e8\u53bb\u505a\u4e86\uff0c\u5982\u679c\u5916\u90e8\u4e0d\u518d\u6709\u6307\u5411\u8be5\u90e8\u5206\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u6216\u8005\u5b83\u53ea\u662f\u5fd8\u8bb0\u8fdb\u884c\u6e05\u7406(\u5047\u8bbe\u7c7b\u5c06\u5904\u7406\u6b64\u64cd\u4f5c)\uff0c\u90a3\u4e48\u5185\u5b58\u5c06\u4f1a\u6cc4\u6f0f\u3002 \u56e0\u6b64\uff0c\u5e94\u8be5\u591a\u7528\u7ec4\u5408\u5c11\u7528\u805a\u5408\u3002 \u63d0\u9192\u548c\u52d8\u8bef \u7531\u4e8e\u5404\u79cd\u5404\u6837\u7684\u5386\u53f2\u539f\u56e0\uff0c\u805a\u5408\u7684\u5b9a\u4e49\u5e76\u4e0d\u50cf\u7ec4\u5408\u7684\u5b9a\u4e49\u90a3\u6837\u7cbe\u786e\u2014\u2014\u56e0\u6b64\u4f60\u53ef\u80fd\u4f1a\u770b\u5230\u5176\u4ed6\u53c2\u8003\u6750\u6599\u5bf9\u5b83\u7684\u5b9a\u4e49\u4e0e\u6211\u4eec\u6240\u505a\u7684\u4e0d\u540c\u3002\u8fd9\u5e76\u4e0d\u8981\u7d27\uff0c\u4f60\u77e5\u9053\u5c31\u53ef\u4ee5\u4e86\u3002 \u6700\u540e\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a\u5728\u8bfe\u7a0b 10.5 -\u7ed3\u6784\u4f53\uff0c\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 \u4e2d\uff0c\u6211\u4eec\u5c06\u805a\u5408\u6570\u636e\u7c7b\u578b(\u5982\u7ed3\u6784\u548c\u7c7b)\u5b9a\u4e49\u4e3a\u5c06\u591a\u4e2a\u53d8\u91cf\u7ec4\u5408\u5728\u4e00\u8d77\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f60\u4eca\u540e\u8fd8\u53ef\u80fd\u63a5\u89e6\u5230 \u805a\u5408\u7c7b \u8fd9\u4e2a\u672f\u8bed\uff0c\u5b83\u88ab\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u7ed3\u6784\u6216\u7c7b\uff0c\u4f46\u6ca1\u6709\u63d0\u4f9b\u6784\u9020\u51fd\u6570\u3001\u6790\u6784\u51fd\u6570\u6216\u91cd\u8f7d\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u4e14\u6240\u6709\u6210\u5458\u90fd\u662f \u516c\u6709\u6210\u5458 \uff0c\u5b83\u4e5f\u4e0d\u4f7f\u7528\u7ee7\u627f\u2014\u2014\u5b83\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u4ec5\u5305\u542b\u6570\u636e\u7684\u7ed3\u6784\u4f53\u3002 std::reference_wrapper \u5728 Department / Teacher \u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728 Department \u4e2d\u4f7f\u7528\u5f15\u7528\u6765\u4fdd\u5b58 Teacher \u3002\u53ef\u4ee5\u8fd9\u4e48\u505a\u7684\u539f\u56e0\u662f\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u53ea\u6709\u4e00\u4e2a Teacher \uff0c\u4f46\u662f\u5982\u679c\u9662\u7cfb\u4e2d\u4e0d\u6b62\u4e00\u4e2a\u8001\u5e08\u5462\uff1f\u6b64\u65f6\u6211\u4eec\u9700\u8981\u5c06\u8001\u5e08\u5b58\u653e\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff08\u4f8b\u5982 std::vector \uff09\u3002\u4f46\u662f\u56fa\u5b9a\u6570\u7ec4\u548c\u5f88\u591a\u6807\u51c6\u5e93\u5bb9\u5668\u5e76\u4e0d\u652f\u6301\u4fdd\u5b58\u5f15\u7528\uff08\u56e0\u4e3a\u94fe\u8868\u5143\u7d20\u5fc5\u987b\u662f\u53ef\u8d4b\u503c\u7684\uff0c\u800c\u5f15\u7528\u4e0d\u53ef\u4ee5\u88ab\u8d4b\u503c\uff09\u3002 1 std :: vector < const Teacher &> m_teachers {}; // Illegal \u5982\u679c\u5f15\u7528\u4e0d\u884c\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\uff0c\u4e0d\u8fc7\u6b64\u65f6\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5b58\u653e\u6216\u4f7f\u7528\u7a7a\u6307\u9488\u7684\u95ee\u9898\u3002\u5728 Department / Teacher \u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4e0d\u5141\u8bb8\u4f7f\u7528\u7a7a\u6307\u9488\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528 std::reference_wrapper \u3002 \u672c\u8d28\u4e0a std::reference_wrapper \u662f\u4e00\u4e2a\u884c\u4e3a\u7c7b\u4f3c\u4e8e\u5f15\u7528\u7684\u7c7b\uff0c\u4f46\u662f\u5b83\u53ef\u4ee5\u88ab\u8d4b\u503c\u548c\u62f7\u8d1d\uff0c\u6240\u4ee5\u80fd\u591f\u914d\u5408 std::vector \u4f7f\u7528\u3002 \u597d\u6d88\u606f\u662f\u4f60\u5b8c\u5168\u4e0d\u9700\u8981\u4e86\u89e3\u8fd9\u80cc\u540e\u7684\u539f\u7406\uff0c\u4f7f\u7528\u5b83\u4f60\u53ea\u9700\u8981\u8bb0\u4f4f\u4e09\u4ef6\u4e8b\uff1a std::reference_wrapper \u4f4d\u4e8e \u5934\u6587\u4ef6\uff1b \u5728\u521b\u5efa std::reference_wrapper \u5305\u88c5\u7684\u5bf9\u8c61\u65f6\uff0c\u8be5\u5bf9\u8c61\u4e0d\u53ef\u4ee5\u662f \u533f\u540d\u5bf9\u8c61 (\u56e0\u4e3a\u533f\u540d\u5bf9\u8c61\u5177\u6709\u8868\u8fbe\u5f0f\u4f5c\u7528\u57df\uff0c\u6240\u4ee5\u4f1a\u5bfc\u81f4 \u60ac\u5782 \u5f15\u7528)\u3002 \u5f53\u9700\u8981\u4ece std::reference_wrapper \u4e2d\u83b7\u53d6\u539f\u5bf9\u8c61\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 get() \u6210\u5458\u65b9\u6cd5\u3002 \u4e0b\u9762\u7684\u4e2d\u6211\u4eec\u5728 std::vector \u4e2d\u4f7f\u7528\u4e86 std::reference_wrapper \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include // std::reference_wrapper #include #include #include int main () { std :: string tom { \"Tom\" }; std :: string berta { \"Berta\" }; std :: vector < std :: reference_wrapper < std :: string >> names { tom , berta }; // these strings are stored by reference, not value std :: string jim { \"Jim\" }; names . push_back ( jim ); for ( auto name : names ) { // Use the get() member function to get the referenced string. name . get () += \" Beam\" ; } std :: cout << jim << '\\n' ; // Jim Beam return 0 ; } \u5982\u679c\u8981\u521b\u5efaconst\u5f15\u7528\uff0c\u5728 std::string \u524d\u52a0\u4e0aconst\u5373\u53ef\uff1a 1 2 // Vector of const references to std::string std :: vector < std :: reference_wrapper < const std :: string >> names { tom , berta };","title":"16.3 - \u805a\u5408\u5173\u7cfb"},{"location":"16-3-aggregation/#_1","text":"\u805a\u5408\u5173\u7cfb\u8981\u6ee1\u8db3\u5982\u4e0b\u51e0\u4e2a\u6761\u4ef6\uff1a \u90e8\u5206\uff08\u6210\u5458\uff09\u662f\u6574\u4f53\u5bf9\u8c61\u7684\u4e00\u90e8\u5206\uff1b \u90e8\u5206\uff08\u6210\u5458\uff09\u53ef\u4ee5\u4e0d\u53ea\u5c5e\u4e8e\u4e00\u4e2a\u6574\u4f53\u5bf9\u8c61\uff1b \u90e8\u5206\uff08\u6210\u5458\uff09\u7684\u751f\u547d\u5468\u671f\u4e0d\u7531\u6574\u4f53\u5bf9\u8c61\u7ba1\u7406\uff1b \u90e8\u5206\uff08\u6210\u5458\uff09\u4e0d\u9700\u8981\u77e5\u9053\u6574\u4f53\u5bf9\u8c61\u7684\u5b58\u5728\u6027\uff1b \u548c\u7ec4\u5408\u7c7b\u4f3c\uff0c\u805a\u5408\u4e5f\u662f\u4e00\u79cd\u201d\u90e8\u5206\u4e0e\u6574\u4f53\u201c\u7684\u5173\u7cfb\uff0c\u90e8\u5206\u88ab\u5305\u542b\u5728\u6574\u4f53\u4e2d\uff0c\u800c\u4e14\u8fd9\u662f\u4e00\u79cd\u5355\u5411\u7684\u5173\u7cfb\u3002\u4f46\u662f\uff0c\u548c\u7ec4\u5408\u4e0d\u540c\uff0c\u805a\u5408\u5bf9\u8c61\u53ef\u4ee5\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u6574\u4f53\uff0c\u800c\u4e14\u6574\u4f53\u5bf9\u8c61\u4e0d\u9700\u8981\u8d1f\u8d23\u7ba1\u7406\u90e8\u5206\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u3002\u5f53\u805a\u5408\u5173\u7cfb\u88ab\u521b\u5efa\u65f6\uff0c\u6574\u4f53\u4e0d\u8d1f\u8d23\u521b\u5efa\u90e8\u5206\u3002\u5f53\u805a\u5408\u5173\u7cfb\u88ab\u7834\u574f\u65f6\uff0c\u6574\u4f53\u4e5f\u4e0d\u8d1f\u8d23\u9500\u6bc1\u90e8\u5206\u3002 \u4e3e\u4f8b\u6765\u8bf4\uff0c\u8003\u8651\u67d0\u4e2a\u4eba\u548c\u5b83\u4f4f\u5740\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u4e00\u4e2a\u4eba\u6709\u4e00\u4e2a\u4f4f\u5740\u3002\u4f46\u662f\u4f4f\u5740\u53ef\u4ee5\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u4eba\uff0c\u4f8b\u5982\u4f60\u548c\u4f60\u7684\u5ba4\u53cb\uff0c\u6216\u8005\u662f\u5176\u4ed6\u4eba\u3002\u800c\u4e14\uff0c\u4f4f\u5740\u4e5f\u4e0d\u662f\u88ab\u4eba\u7ba1\u7406\u7684\u2014\u2014\u5176\u5730\u5740\u751a\u81f3\u65e9\u4e8e\u5c45\u4f4f\u4eba\u5c31\u5b58\u5728\u4e86\uff0c\u800c\u4e14\u4eba\u4e0d\u5728\u7684\u65f6\u5019\u5730\u5740\u4ecd\u7136\u4f1a\u7ee7\u7eed\u5b58\u5728\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4e0a\u9762\u63cf\u8ff0\u7684\u662f\u4e00\u79cd\u805a\u5408\u5173\u7cfb\u3002 \u53e6\u5916\uff0c\u8003\u8651\u6c7d\u8f66\u548c\u5f15\u64ce\u7684\u5173\u7cfb\u3002\u6c7d\u8f66\u7684\u5f15\u64ce\u5c5e\u4e8e\u6c7d\u8f66\u7684\u4e00\u90e8\u5206\u3002\u4f46\u662f\u5c3d\u7ba1\u5f15\u64ce\u662f\u6c7d\u8f66\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u4e5f\u53ef\u4ee5\u5c5e\u4e8e\u5176\u4ed6\u4eba\u6216\u7269\u3002\u4f8b\u5982\uff0c\u5f15\u64ce\u4e5f\u5c5e\u4e8e\u8f66\u4e3b\u3002\u6c7d\u8f66\u672c\u8eab\u5e76\u4e0d\u8d1f\u8d23\u521b\u5efa\u6216\u9500\u6bc1\u5f15\u64ce\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u5c3d\u7ba1\u8f66\u8f86\u77e5\u9053\u5b83\u4f1a\u6709\u4e00\u4e2a\u5f15\u64ce\uff0c\u4f46\u662f\u5f15\u64ce\u5374\u4e0d\u77e5\u9053\u5b83\u662f\u6c7d\u8f66\u7684\u4e00\u90e8\u5206\u3002 \u5f53\u6211\u4eec\u8ba8\u8bba\u5bf9\u7269\u7406\u5bf9\u8c61\u5efa\u6a21\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u201d\u9500\u6bc1\u201c\u4e00\u8bcd\u4f1a\u6bd4\u8f83\u5947\u602a\u3002\u6b64\u65f6\u6709\u7684\u4eba\u53ef\u80fd\u4f1a\u62ac\u6760\u8bf4\uff0c\u5982\u679c\u4e00\u4e2a\u9668\u77f3\u649e\u51fb\u5730\u7403\u6b63\u597d\u7838\u5230\u4e86\u4f60\u7684\u8f66\uff0c\u90a3\u5c82\u4e0d\u662f\u8f66\u548c\u5f15\u64ce\u4e00\u8d77\u88ab\u9500\u6bc1\u4e86\uff1f\u5f53\u7136\uff0c\u4e0d\u8fc7\u8fd9\u662f\u9668\u77f3\u7684\u9519\u3002\u8fd9\u91cc\u7684\u5173\u952e\u70b9\u5728\u4e8e\uff0c\u6c7d\u8f66\u4e0d\u8d1f\u8d23\u9500\u6bc1\u5176\u90e8\u4ef6\uff08\u5916\u529b\u662f\u53ef\u80fd\u7684\uff09\u3002 \u6211\u4eec\u53ef\u4ee5\u8bf4\uff0c\u805a\u5408\u6a21\u578b\u662f\u4e00\u79cd\u201d\u6709\u4e00\u4e2a\u201c\u7684\u5173\u7cfb\uff08\u9662\u7cfb\u6709\u4e00\u4e2a\u8001\u5e08\u3001\u6c7d\u8f66\u6709\u4e00\u4e2a\u5f15\u64ce\uff09\u3002 \u7c7b\u4f3c\u7ec4\u5408\u5173\u7cfb\uff0c\u805a\u5408\u4e2d\u7684\u90e8\u5206\u53ef\u4ee5\u662f\u4e00\u4e2a\u4e5f\u53ef\u4ee5\u662f\u591a\u4e2a\u3002","title":"\u805a\u5408"},{"location":"16-3-aggregation/#_2","text":"\u56e0\u4e3a\u805a\u5408\u7c7b\u4f3c\u4e8e\u7ec4\u5408\uff0c\u5b83\u4eec\u90fd\u662f\u90e8\u5206-\u6574\u4f53\u5173\u7cfb\uff0c\u6240\u4ee5\u5b83\u4eec\u7684\u5b9e\u73b0\u51e0\u4e4e\u5b8c\u5168\u76f8\u540c\uff0c\u5b83\u4eec\u4e4b\u95f4\u7684\u533a\u522b\u4e3b\u8981\u662f\u8bed\u4e49\u4e0a\u7684\u3002\u5728\u7ec4\u5408\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u666e\u901a\u6210\u5458\u53d8\u91cf(\u6216\u7531\u7ec4\u5408\u7c7b\u5904\u7406\u5206\u914d\u548c\u56de\u6536\u8fc7\u7a0b\u7684\u6307\u9488)\u5c06\u90e8\u4ef6\u6dfb\u52a0\u5230\u7ec4\u5408\u4e2d\u3002 \u5728\u805a\u5408\u4e2d\uff0c\u6211\u4eec\u4ecd\u7136\u4f7f\u7528\u6210\u5458\u53d8\u91cf\u8868\u793a\u90e8\u5206\u5bf9\u8c61\u3002\u4f46\u662f\uff0c \u8fd9\u4e9b\u6210\u5458\u53d8\u91cf\u901a\u5e38\u662f\u5f15\u7528\u6216\u6307\u9488 \uff0c\u7528\u4e8e\u6307\u5411\u5728\u7c7b\u7684\u4f5c\u7528\u57df\u4e4b\u5916\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u805a\u5408\u901a\u5e38\u8981\u4e48\u63a5\u53d7\u5b83\u5c06\u8981\u6307\u5411\u7684\u5bf9\u8c61\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\u53c2\u6570\uff0c\u8981\u4e48\u4ee5\u7a7a\u5f00\u59cb\uff0c\u7136\u540e\u901a\u8fc7\u8bbf\u95ee\u51fd\u6570\u6216\u64cd\u4f5c\u7b26\u6dfb\u52a0\u5b50\u5bf9\u8c61\u3002 \u56e0\u4e3a\u8fd9\u4e9b\u90e8\u5206\u5bf9\u8c61\u5b58\u5728\u4e8e\u7c7b\u7684\u4f5c\u7528\u57df\u4e4b\u5916\uff0c\u6240\u4ee5\u5f53\u7c7b\u88ab\u9500\u6bc1\u65f6\uff0c\u6307\u9488\u6216\u5f15\u7528\u6210\u5458\u53d8\u91cf\u5c06\u88ab\u9500\u6bc1(\u4f46\u4e0d\u4f1a\u88ab\u5220\u9664)\u3002\u56e0\u6b64\uff0c\u8fd9\u4e9b\u90e8\u5206\u672c\u8eab\u4ecd\u7136\u5b58\u5728\u3002 \u8ba9\u6211\u4eec\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e2a\u201c\u6559\u5e08\u548c\u9662\u7cfb\u201d\u7684\u793a\u4f8b\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u505a\u4e00\u4e9b\u7b80\u5316\uff1a\u9996\u5148\uff0c\u9662\u7cfb\u5c06\u53ea\u5bb9\u7eb3\u4e00\u540d\u6559\u5e08\u3002\u5176\u6b21\uff0c\u8001\u5e08\u4e0d\u77e5\u9053\u4ed6\u4eec\u662f\u54ea\u4e2a\u9662\u7cfb\u7684\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include #include class Teacher { private : std :: string m_name {}; public : Teacher ( const std :: string & name ) : m_name { name } { } const std :: string & getName () const { return m_name ; } }; class Department { private : const Teacher & m_teacher ; // This dept holds only one teacher for simplicity, but it could hold many teachers public : Department ( const Teacher & teacher ) : m_teacher { teacher } { } }; int main () { // Create a teacher outside the scope of the Department Teacher bob { \"Bob\" }; // create a teacher { // Create a department and use the constructor parameter to pass // the teacher to it. Department department { bob }; } // department goes out of scope here and is destroyed // bob still exists here, but the department doesn't std :: cout << bob . getName () << \" still exists! \\n \" ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d bob \u7684\u521b\u5efa\u662f\u72ec\u7acb\u4e8e department \u7684\uff0c\u5b83\u901a\u8fc7\u6784\u9020\u51fd\u6570\u88ab\u4f20\u9012\u7ed9\u4e86 department \u3002\u5f53 department \u9500\u6bc1\u7684\u65f6\u5019\u5f15\u7528 m_teacher \u88ab\u9500\u6bc1\u4e86\uff0c\u4f46\u662f teacher \u672c\u8eab\u5e76\u6ca1\u6709\u88ab\u9500\u6bc1\uff0c\u5b83\u4ecd\u7136\u5b58\u5728\u5e76\u4e14\u5728 main() \u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u3002","title":"\u5b9e\u73b0\u805a\u5408\u5173\u7cfb"},{"location":"16-3-aggregation/#_3","text":"\u5c3d\u7ba1\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6559\u5e08\u4e0d\u77e5\u9053\u4ed6\u4eec\u5728\u4e3a\u54ea\u4e2a\u7cfb\u5de5\u4f5c\uff0c\u8fd9\u542c\u8d77\u6765\u6709\u70b9\u8822\uff0c\u4f46\u5728\u7ed9\u5b9a\u7684\u9879\u76ee\u4e0a\u4e0b\u6587\u4e2d\uff0c\u8fd9\u53ef\u80fd\u5b8c\u5168\u6ca1\u95ee\u9898\u3002\u5f53\u51b3\u5b9a\u5b9e\u73b0\u67d0\u79cd\u7c7b\u578b\u7684\u5173\u7cfb\u65f6\uff0c\u5b9e\u73b0\u6ee1\u8db3\u4f60\u9700\u6c42\u7684\u6700\u7b80\u5355\u7684\u5173\u7cfb\uff0c\u800c\u4e0d\u662f\u770b\u8d77\u6765\u6700\u7b26\u5408\u5b9e\u9645\u73af\u5883\u7684\u5173\u7cfb\u3002 \u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u8f66\u95f4\u6a21\u62df\u5668\uff0c\u5219\u5c06\u6c7d\u8f66\u548c\u5f15\u64ce\u4f5c\u4e3a\u4e00\u4e2a\u805a\u5408\u662f\u6070\u5f53\u7684\uff0c\u8fd9\u6837\u5f15\u64ce\u5c31\u53ef\u4ee5\u88ab\u4ece\u6c7d\u8f66\u4e2d\u79fb\u9664\u5e76\u653e\u5728\u67d0\u4e2a\u67b6\u5b50\u4e0a\u4ee5\u5907\u4ee5\u540e\u4f7f\u7528\u3002\u7136\u800c\uff0c\u5982\u679c\u4f60\u6b63\u5728\u7f16\u5199\u6a21\u62df\u8d5b\u8f66\u6e38\u620f\uff0c\u90a3\u53ef\u80fd\u5c06\u6c7d\u8f66\u548c\u5f15\u64ce\u4f5c\u4e3a\u7ec4\u5408\u5b9e\u73b0\u66f4\u6070\u5f53\uff0c\u56e0\u4e3a\u5728\u8be5\u8bed\u5883\u4e2d\uff0c\u5f15\u64ce\u4e0d\u4f1a\u72ec\u7acb\u5b58\u5728\u4e8e\u6c7d\u8f66\u4e4b\u5916\u3002 \u6700\u4f73\u5b9e\u8df5 \u5b9e\u73b0\u6ee1\u8db3\u7a0b\u5e8f\u9700\u8981\u7684\u6700\u7b80\u5355\u7684\u5173\u7cfb\u7c7b\u578b\uff0c\u800c\u4e0d\u662f\u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\u770b\u8d77\u6765\u6b63\u786e\u7684\u5173\u7cfb\u7c7b\u578b\u3002","title":"\u5bf9\u8c61\u5efa\u6a21\u65f6\u5e94\u5f53\u9009\u62e9\u6b63\u786e\u7684\u5bf9\u8c61\u5173\u7cfb"},{"location":"16-3-aggregation/#_4","text":"\u7ec4\u5408: \u901a\u5e38\u4f7f\u7528\u666e\u901a\u7684\u6210\u5458\u53d8\u91cf\uff1b \u5982\u679c\u7c7b\u81ea\u5df1\u7ba1\u7406\u6210\u5458\u5bf9\u8c61\u7684\u5185\u5b58\u7533\u8bf7\u91ca\u653e\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\uff1b \u8d1f\u8d23\u90e8\u5206\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u9500\u6bc1\u3002 \u805a\u5408: \u901a\u5e38\u4f7f\u7528\u6307\u9488\u548c\u5f15\u7528\u6210\u5458\u5e76\u6307\u5411\u5b58\u5728\u4e8e\u6574\u4f53\u5bf9\u8c61\u4f5c\u7528\u57df\u4e4b\u5916\u7684\u5bf9\u8c61\uff1b \u4e0d\u8d1f\u8d23\u90e8\u5206\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u9500\u6bc1\u3002 \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7ec4\u5408\u548c\u805a\u5408\u7684\u6982\u5ff5\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u7c7b\u4e2d\u81ea\u7531\u6df7\u5408\u4f7f\u7528 \u3002\u7f16\u5199\u4e00\u4e2a\u8d1f\u8d23\u521b\u5efa/\u9500\u6bc1\u67d0\u4e9b\u90e8\u5206\u800c\u4e0d\u8d1f\u8d23\u5176\u4ed6\u90e8\u5206\u7684\u7c7b\u662f\u53ef\u4ee5\u7684\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u7684 Department \u7c7b\u53ef\u4ee5\u6709\u4e00\u4e2a name \u548c\u4e00\u4e2a Teacher \u6210\u5458\u3002\u8fd9\u4e2a name \u901a\u5e38\u662f\u4f5c\u4e3a\u7ec4\u5408\u5173\u7cfb\u6dfb\u52a0\u5230\u8be5\u7c7b\u4e2d\u7684\u5e76\u4e14\u548c Department \u4e00\u8d77\u521b\u5efa\u548c\u9500\u6bc1\u3002\u53e6\u4e00\u65b9\u9762\uff0c Teacher \u5c06\u901a\u8fc7\u805a\u5408\u7684\u65b9\u5f0f\u6dfb\u52a0\u5230 Department \u4e2d\uff0c\u5e76\u72ec\u7acb\u5730\u521b\u5efa/\u9500\u6bc1\u3002 \u867d\u7136\u805a\u5408\u53ef\u80fd\u975e\u5e38\u6709\u7528\uff0c\u4f46\u5b83\u4eec\u4e5f\u66f4\u5371\u9669\uff0c\u56e0\u4e3a\u805a\u5408\u4e0d\u5904\u7406\u5176\u90e8\u5206\u5bf9\u8c61\u7684\u56de\u6536\uff0c\u5176\u9500\u6bc1\u64cd\u4f5c\u88ab\u5206\u914d\u5916\u90e8\u53bb\u505a\u4e86\uff0c\u5982\u679c\u5916\u90e8\u4e0d\u518d\u6709\u6307\u5411\u8be5\u90e8\u5206\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u6216\u8005\u5b83\u53ea\u662f\u5fd8\u8bb0\u8fdb\u884c\u6e05\u7406(\u5047\u8bbe\u7c7b\u5c06\u5904\u7406\u6b64\u64cd\u4f5c)\uff0c\u90a3\u4e48\u5185\u5b58\u5c06\u4f1a\u6cc4\u6f0f\u3002 \u56e0\u6b64\uff0c\u5e94\u8be5\u591a\u7528\u7ec4\u5408\u5c11\u7528\u805a\u5408\u3002","title":"\u5c0f\u7ed3\uff1a\u7ec4\u5408\u548c\u805a\u5408"},{"location":"16-3-aggregation/#_5","text":"\u7531\u4e8e\u5404\u79cd\u5404\u6837\u7684\u5386\u53f2\u539f\u56e0\uff0c\u805a\u5408\u7684\u5b9a\u4e49\u5e76\u4e0d\u50cf\u7ec4\u5408\u7684\u5b9a\u4e49\u90a3\u6837\u7cbe\u786e\u2014\u2014\u56e0\u6b64\u4f60\u53ef\u80fd\u4f1a\u770b\u5230\u5176\u4ed6\u53c2\u8003\u6750\u6599\u5bf9\u5b83\u7684\u5b9a\u4e49\u4e0e\u6211\u4eec\u6240\u505a\u7684\u4e0d\u540c\u3002\u8fd9\u5e76\u4e0d\u8981\u7d27\uff0c\u4f60\u77e5\u9053\u5c31\u53ef\u4ee5\u4e86\u3002 \u6700\u540e\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a\u5728\u8bfe\u7a0b 10.5 -\u7ed3\u6784\u4f53\uff0c\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 \u4e2d\uff0c\u6211\u4eec\u5c06\u805a\u5408\u6570\u636e\u7c7b\u578b(\u5982\u7ed3\u6784\u548c\u7c7b)\u5b9a\u4e49\u4e3a\u5c06\u591a\u4e2a\u53d8\u91cf\u7ec4\u5408\u5728\u4e00\u8d77\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f60\u4eca\u540e\u8fd8\u53ef\u80fd\u63a5\u89e6\u5230 \u805a\u5408\u7c7b \u8fd9\u4e2a\u672f\u8bed\uff0c\u5b83\u88ab\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u7ed3\u6784\u6216\u7c7b\uff0c\u4f46\u6ca1\u6709\u63d0\u4f9b\u6784\u9020\u51fd\u6570\u3001\u6790\u6784\u51fd\u6570\u6216\u91cd\u8f7d\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u4e14\u6240\u6709\u6210\u5458\u90fd\u662f \u516c\u6709\u6210\u5458 \uff0c\u5b83\u4e5f\u4e0d\u4f7f\u7528\u7ee7\u627f\u2014\u2014\u5b83\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u4ec5\u5305\u542b\u6570\u636e\u7684\u7ed3\u6784\u4f53\u3002","title":"\u63d0\u9192\u548c\u52d8\u8bef"},{"location":"16-3-aggregation/#stdreference_wrapper","text":"\u5728 Department / Teacher \u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728 Department \u4e2d\u4f7f\u7528\u5f15\u7528\u6765\u4fdd\u5b58 Teacher \u3002\u53ef\u4ee5\u8fd9\u4e48\u505a\u7684\u539f\u56e0\u662f\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u53ea\u6709\u4e00\u4e2a Teacher \uff0c\u4f46\u662f\u5982\u679c\u9662\u7cfb\u4e2d\u4e0d\u6b62\u4e00\u4e2a\u8001\u5e08\u5462\uff1f\u6b64\u65f6\u6211\u4eec\u9700\u8981\u5c06\u8001\u5e08\u5b58\u653e\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff08\u4f8b\u5982 std::vector \uff09\u3002\u4f46\u662f\u56fa\u5b9a\u6570\u7ec4\u548c\u5f88\u591a\u6807\u51c6\u5e93\u5bb9\u5668\u5e76\u4e0d\u652f\u6301\u4fdd\u5b58\u5f15\u7528\uff08\u56e0\u4e3a\u94fe\u8868\u5143\u7d20\u5fc5\u987b\u662f\u53ef\u8d4b\u503c\u7684\uff0c\u800c\u5f15\u7528\u4e0d\u53ef\u4ee5\u88ab\u8d4b\u503c\uff09\u3002 1 std :: vector < const Teacher &> m_teachers {}; // Illegal \u5982\u679c\u5f15\u7528\u4e0d\u884c\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\uff0c\u4e0d\u8fc7\u6b64\u65f6\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5b58\u653e\u6216\u4f7f\u7528\u7a7a\u6307\u9488\u7684\u95ee\u9898\u3002\u5728 Department / Teacher \u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4e0d\u5141\u8bb8\u4f7f\u7528\u7a7a\u6307\u9488\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528 std::reference_wrapper \u3002 \u672c\u8d28\u4e0a std::reference_wrapper \u662f\u4e00\u4e2a\u884c\u4e3a\u7c7b\u4f3c\u4e8e\u5f15\u7528\u7684\u7c7b\uff0c\u4f46\u662f\u5b83\u53ef\u4ee5\u88ab\u8d4b\u503c\u548c\u62f7\u8d1d\uff0c\u6240\u4ee5\u80fd\u591f\u914d\u5408 std::vector \u4f7f\u7528\u3002 \u597d\u6d88\u606f\u662f\u4f60\u5b8c\u5168\u4e0d\u9700\u8981\u4e86\u89e3\u8fd9\u80cc\u540e\u7684\u539f\u7406\uff0c\u4f7f\u7528\u5b83\u4f60\u53ea\u9700\u8981\u8bb0\u4f4f\u4e09\u4ef6\u4e8b\uff1a std::reference_wrapper \u4f4d\u4e8e \u5934\u6587\u4ef6\uff1b \u5728\u521b\u5efa std::reference_wrapper \u5305\u88c5\u7684\u5bf9\u8c61\u65f6\uff0c\u8be5\u5bf9\u8c61\u4e0d\u53ef\u4ee5\u662f \u533f\u540d\u5bf9\u8c61 (\u56e0\u4e3a\u533f\u540d\u5bf9\u8c61\u5177\u6709\u8868\u8fbe\u5f0f\u4f5c\u7528\u57df\uff0c\u6240\u4ee5\u4f1a\u5bfc\u81f4 \u60ac\u5782 \u5f15\u7528)\u3002 \u5f53\u9700\u8981\u4ece std::reference_wrapper \u4e2d\u83b7\u53d6\u539f\u5bf9\u8c61\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 get() \u6210\u5458\u65b9\u6cd5\u3002 \u4e0b\u9762\u7684\u4e2d\u6211\u4eec\u5728 std::vector \u4e2d\u4f7f\u7528\u4e86 std::reference_wrapper \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include // std::reference_wrapper #include #include #include int main () { std :: string tom { \"Tom\" }; std :: string berta { \"Berta\" }; std :: vector < std :: reference_wrapper < std :: string >> names { tom , berta }; // these strings are stored by reference, not value std :: string jim { \"Jim\" }; names . push_back ( jim ); for ( auto name : names ) { // Use the get() member function to get the referenced string. name . get () += \" Beam\" ; } std :: cout << jim << '\\n' ; // Jim Beam return 0 ; } \u5982\u679c\u8981\u521b\u5efaconst\u5f15\u7528\uff0c\u5728 std::string \u524d\u52a0\u4e0aconst\u5373\u53ef\uff1a 1 2 // Vector of const references to std::string std :: vector < std :: reference_wrapper < const std :: string >> names { tom , berta };","title":"std::reference_wrapper"},{"location":"16-4-association/","tags":["object","association"],"text":"Key Takeaway - \u5728\u524d\u9762\u7684\u4e24\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u4e24\u79cd\u5bf9\u8c61\u7ec4\u5408\u5173\u7cfb\u2014\u2014\u7ec4\u5408\u548c\u7ee7\u627f\u3002\u5bf9\u8c61\u7ec4\u5408\u7528\u4e8e\u901a\u8fc7\u4e00\u4e2a\u6216\u591a\u4e2a\u7b80\u5355\u5bf9\u8c61\u6784\u5efa\u590d\u6742\u5bf9\u8c61\u3002 \u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u770b\u770b\u4e24\u4e2a\u672c\u6765\u4e0d\u76f8\u5173\u7684\u5bf9\u8c61\u4e4b\u95f4\u7684\u4e00\u79cd\u8f83\u5f31\u7c7b\u578b\u7684\u5173\u7cfb\uff0c\u79f0\u4e3a\u5173\u8054\u3002\u4e0e\u5bf9\u8c61\u7ec4\u5408\u5173\u7cfb\u4e0d\u540c\uff0c\u5728\u5173\u8054\u4e2d\uff0c\u6ca1\u6709\u9690\u542b\u7684\u6574\u4f53/\u90e8\u5206\u5173\u7cfb\u3002 \u5173\u8054 \u5f62\u6210\u5173\u8054\u5173\u7cfb\u65f6\uff0c\u5bf9\u8c61\u548c\u5bf9\u8c61\u4e4b\u95f4\u9700\u8981\u6ee1\u8db3\u4e0b\u9762\u6761\u4ef6\uff1a \u5173\u8054\u7684\u5bf9\u8c61(\u6210\u5458)\u5728\u5176\u4ed6\u65b9\u9762\u4e0e\u5bf9\u8c61(\u7c7b)\u65e0\u5173\uff1b \u5173\u8054\u7684\u5bf9\u8c61(\u6210\u5458)\u53ef\u4ee5\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u5bf9\u8c61(\u7c7b)\uff1b \u5173\u8054\u5bf9\u8c61(\u6210\u5458)\u7684\u5b58\u5728\u6027\u4e0d\u7531\u5bf9\u8c61(\u7c7b)\u7ba1\u7406\uff1b \u5173\u8054\u7684\u5bf9\u8c61(\u6210\u5458)\u53ef\u80fd\u77e5\u9053\u4e5f\u53ef\u80fd\u4e0d\u77e5\u9053\u5bf9\u8c61(\u7c7b)\u7684\u5b58\u5728\u3002 \u5728\u7ec4\u5408\u6216\u805a\u5408\u4e2d\uff0c\u90e8\u5206\u662f\u6574\u4e2a\u5bf9\u8c61\u7684\u4e00\u90e8\u5206\uff0c\u800c\u5728\u5173\u8054\u4e2d\uff0c\u5173\u8054\u5bf9\u8c61\u5728\u5176\u4ed6\u65b9\u9762\u4e0e\u5bf9\u8c61\u65e0\u5173\u3002\u5c31\u50cf\u805a\u5408\u4e00\u6837\uff0c\u5173\u8054\u7684\u5bf9\u8c61\u53ef\u4ee5\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u5bf9\u8c61\uff0c\u5e76\u4e14\u4e0d\u53d7\u8fd9\u4e9b\u5bf9\u8c61\u7684\u7ba1\u7406\u3002\u7136\u800c\uff0c\u4e0e\u805a\u5408(\u805a\u5408\u7684\u5173\u7cfb\u603b\u662f\u5355\u5411\u7684)\u4e0d\u540c\uff0c\u5728\u5173\u8054\u4e2d\uff0c\u5173\u7cfb\u53ef\u80fd\u662f\u5355\u5411\u7684\u6216\u53cc\u5411\u7684(\u5176\u4e2d\u4e24\u4e2a\u5bf9\u8c61\u5f7c\u6b64\u610f\u8bc6\u5230\u5bf9\u65b9)\u3002 \u533b\u751f\u548c\u75c5\u4eba\u4e4b\u95f4\u7684\u5173\u7cfb\u5c31\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u4f8b\u5b50\u3002\u533b\u751f\u663e\u7136\u4e0e\u4ed6\u7684\u75c5\u4eba\u6709\u4e00\u79cd\u5173\u7cfb\uff0c\u4f46\u4ece\u6982\u5ff5\u4e0a\u8bb2\uff0c\u8fd9\u4e0d\u662f\u4e00\u4e2a\u90e8\u5206/\u6574\u4f53(\u5bf9\u8c61\u7ec4\u6210)\u5173\u7cfb\u3002\u4e00\u4e2a\u533b\u751f\u4e00\u5929\u53ef\u4ee5\u770b\u5f88\u591a\u75c5\u4eba\uff0c\u4e00\u4e2a\u75c5\u4eba\u53ef\u4ee5\u770b\u5f88\u591a\u533b\u751f(\u4e5f\u8bb8\u4ed6\u4eec\u60f3\u8981\u7b2c\u4e8c\u4e2a\u610f\u89c1\uff0c\u6216\u8005\u4ed6\u4eec\u6b63\u5728\u770b\u4e0d\u540c\u7c7b\u578b\u7684\u533b\u751f)\u3002\u8fd9\u4e24\u4e2a\u7269\u4f53\u7684\u5bff\u547d\u90fd\u4e0e\u5bf9\u65b9\u65e0\u5173\u3002 \u6211\u4eec\u53ef\u4ee5\u8bf4\u5173\u8054\u5efa\u6a21\u4e3a\u201c\u4f7f\u7528\u4e00\u4e2a\u201d\u5173\u7cfb\u3002\u533b\u751f\u201c\u4f7f\u7528\u201d\u75c5\u4eba(\u4ee5\u8d5a\u53d6\u6536\u5165)\u3002\u75c5\u4eba\u201c\u4f7f\u7528\u201d\u533b\u751f(\u4e3a\u4e86\u4ed6\u4eec\u9700\u8981\u7684\u4efb\u4f55\u5065\u5eb7\u76ee\u7684)\u3002 \u5b9e\u73b0\u5173\u8054\u5173\u7cfb \u56e0\u4e3a\u5173\u8054\u662f\u4e00\u79cd\u6bd4\u8f83\u5bbd\u6cdb\u7684\u5173\u7cfb\u7c7b\u578b\uff0c\u5b83\u4eec\u53ef\u4ee5\u4ee5\u8bb8\u591a\u4e0d\u540c\u7684\u65b9\u5f0f\u5b9e\u73b0\u3002\u7136\u800c\uff0c\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5173\u8054\u662f\u4f7f\u7528\u6307\u9488\u5b9e\u73b0\u7684\uff0c\u5bf9\u8c61\u6307\u5411\u5173\u8054\u5bf9\u8c61\u3002 \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u5b9e\u73b0\u4e00\u4e2a\u53cc\u5411\u7684\u533b\u751f/\u75c5\u4eba\u5173\u7cfb\uff0c\u56e0\u4e3a\u533b\u751f\u77e5\u9053\u8c01\u662f\u4ed6\u4eec\u7684\u75c5\u4eba\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 #include // reference_wrapper #include #include #include // Since Doctor and Patient have a circular dependency, we're going to forward declare Patient class Patient ; class Doctor { private : std :: string m_name {}; std :: vector < std :: reference_wrapper < const Patient >> m_patient {}; public : Doctor ( const std :: string & name ) : m_name { name } { } void addPatient ( Patient & patient ); // We'll implement this function below Patient since we need Patient to be defined at that point friend std :: ostream & operator << ( std :: ostream & out , const Doctor & doctor ); const std :: string & getName () const { return m_name ; } }; class Patient { private : std :: string m_name {}; std :: vector < std :: reference_wrapper < const Doctor >> m_doctor {}; // so that we can use it here // We're going to make addDoctor private because we don't want the public to use it. // They should use Doctor::addPatient() instead, which is publicly exposed void addDoctor ( const Doctor & doctor ) { m_doctor . push_back ( doctor ); } public : Patient ( const std :: string & name ) : m_name { name } { } // We'll implement this function below to parallel operator<<(std::ostream&, const Doctor&) friend std :: ostream & operator << ( std :: ostream & out , const Patient & patient ); const std :: string & getName () const { return m_name ; } // We'll friend Doctor::addPatient() so it can access the private function Patient::addDoctor() friend void Doctor::addPatient ( Patient & patient ); }; void Doctor::addPatient ( Patient & patient ) { // Our doctor will add this patient m_patient . push_back ( patient ); // and the patient will also add this doctor patient . addDoctor ( * this ); } std :: ostream & operator << ( std :: ostream & out , const Doctor & doctor ) { if ( doctor . m_patient . empty ()) { out << doctor . m_name << \" has no patients right now\" ; return out ; } out << doctor . m_name << \" is seeing patients: \" ; for ( const auto & patient : doctor . m_patient ) out << patient . get (). getName () << ' ' ; return out ; } std :: ostream & operator << ( std :: ostream & out , const Patient & patient ) { if ( patient . m_doctor . empty ()) { out << patient . getName () << \" has no doctors right now\" ; return out ; } out << patient . m_name << \" is seeing doctors: \" ; for ( const auto & doctor : patient . m_doctor ) out << doctor . get (). getName () << ' ' ; return out ; } int main () { // Create a Patient outside the scope of the Doctor Patient dave { \"Dave\" }; Patient frank { \"Frank\" }; Patient betsy { \"Betsy\" }; Doctor james { \"James\" }; Doctor scott { \"Scott\" }; james . addPatient ( dave ); scott . addPatient ( dave ); scott . addPatient ( betsy ); std :: cout << james << '\\n' ; std :: cout << scott << '\\n' ; std :: cout << dave << '\\n' ; std :: cout << frank << '\\n' ; std :: cout << betsy << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 2 3 4 5 James is seeing patients: Dave Scott is seeing patients: Dave Betsy Dave is seeing doctors: James Scott Frank has no doctors right now Betsy is seeing doctors: Scott \u4e00\u822c\u6765\u8bf4\uff0c\u5982\u679c\u53ef\u4ee5\u4f7f\u7528\u5355\u5411\u5173\u8054\uff0c\u5219\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u53cc\u5411\u5173\u8054\uff0c\u56e0\u4e3a\u5b83\u4eec\u589e\u52a0\u4e86\u590d\u6742\u6027\uff0c\u800c\u4e14\u5f88\u96be\u5728\u4e0d\u51fa\u9519\u3002 \u53cd\u8eab\u5173\u8054 \u6709\u65f6\u5bf9\u8c61\u53ef\u80fd\u4e0e\u76f8\u540c\u7c7b\u578b\u7684\u5176\u4ed6\u5bf9\u8c61\u6709\u5173\u7cfb\uff0c\u8fd9\u88ab\u79f0\u4e3a \u53cd\u8eab\u5173\u8054 \u3002\u53cd\u8eab\u5173\u8054\u7684\u4e00\u4e2a\u5f88\u597d\u7684\u4f8b\u5b50\u662f\u5927\u5b66\u8bfe\u7a0b\u548c\u5b83\u7684\u5148\u4fee\u8bfe\u7a0b(\u4e5f\u662f\u5927\u5b66\u8bfe\u7a0b)\u4e4b\u95f4\u7684\u5173\u7cfb\u3002 \u8003\u8651\u4e00\u4e2a\u7b80\u5316\u7684\u60c5\u51b5\uff0c\u5176\u4e2d\u4e00\u4e2a\u8bfe\u7a0b\u53ea\u80fd\u6709\u4e00\u4e2a\u5148\u4fee\u8bfe\u7a0b\u3002\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include class Course { private : std :: string m_name ; const Course * m_prerequisite ; public : Course ( const std :: string & name , const Course * prerequisite = nullptr ) : m_name { name }, m_prerequisite { prerequisite } { } }; \u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u7cfb\u5217\u7684\u5173\u8054(\u4e00\u95e8\u8bfe\u7a0b\u6709\u4e00\u4e2a\u5148\u51b3\u6761\u4ef6\uff0c\u53e6\u4e00\u95e8\u8bfe\u7a0b\u4e5f\u6709\u4e00\u4e2a\u5148\u51b3\u6761\u4ef6\uff0c\u7b49\u7b49\u2026\u2026) \u5173\u8054\u53ef\u4ee5\u662f\u95f4\u63a5\u7684 \u5728\u524d\u9762\u7684\u6240\u6709\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u90fd\u4f7f\u7528\u6307\u9488\u6216\u5f15\u7528\u76f4\u63a5\u5c06\u5bf9\u8c61\u8fde\u63a5\u5728\u4e00\u8d77\u3002\u7136\u800c\u5728\u5173\u8054\u4e2d\uff0c\u5e76\u4e0d\u4e00\u5b9a\u8981\u8fd9\u6837\u505a\u3002\u4efb\u4f55\u80fd\u591f\u5efa\u7acb\u4e24\u4e2a\u5bf9\u8c61\u4e4b\u95f4\u5173\u7cfb\u7684\u6570\u636e\u90fd\u53ef\u4ee5\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86 Driver \u7c7b\u5982\u4f55\u5728\u4e0d\u5b9e\u9645\u5305\u542b Car \u6307\u9488\u6216\u5f15\u7528\u6210\u5458\u7684\u60c5\u51b5\u4e0b\u4e0e Car \u5f62\u6210\u5355\u5411\u5173\u8054\u5173\u7cfb\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 #include #include class Car { private : std :: string m_name ; int m_id ; public : Car ( const std :: string & name , int id ) : m_name { name }, m_id { id } { } const std :: string & getName () const { return m_name ; } int getId () const { return m_id ; } }; // Our CarLot is essentially just a static array of Cars and a lookup function to retrieve them. // Because it's static, we don't need to allocate an object of type CarLot to use it class CarLot { private : static Car s_carLot [ 4 ]; public : CarLot () = delete ; // Ensure we don't try to create a CarLot static Car * getCar ( int id ) { for ( int count { 0 }; count < 4 ; ++ count ) { if ( s_carLot [ count ]. getId () == id ) { return & ( s_carLot [ count ]); } } return nullptr ; } }; Car CarLot :: s_carLot [ 4 ]{ { \"Prius\" , 4 }, { \"Corolla\" , 17 }, { \"Accord\" , 84 }, { \"Matrix\" , 62 } }; class Driver { private : std :: string m_name ; int m_carId ; // we're associated with the Car by ID rather than pointer public : Driver ( const std :: string & name , int carId ) : m_name { name }, m_carId { carId } { } const std :: string & getName () const { return m_name ; } int getCarId () const { return m_carId ; } }; int main () { Driver d { \"Franz\" , 17 }; // Franz is driving the car with ID 17 Car * car { CarLot :: getCar ( d . getCarId ()) }; // Get that car from the car lot if ( car ) std :: cout << d . getName () << \" is driving a \" << car -> getName () << '\\n' ; else std :: cout << d . getName () << \" couldn't find his car \\n \" ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c CarLot \u6301\u6709 car \u3002\u800c Driver \u4f5c\u4e3a\u672c\u8eab\u9700\u8981 car \u7684\u5bf9\u8c61\uff0c\u5374\u5e76\u6ca1\u6709\u6307\u5411 Car \u7684\u6307\u9488\u2014\u2014\u4f46\u662f\u5b83\u6709\u8f66\u7684ID\uff0c\u5b83\u53ef\u4ee5\u901a\u8fc7\u8be5ID\u5728\u9700\u8981\u65f6\u4ece CarLot \u4e2d\u83b7\u53d6 car \u3002 \u5728\u8fd9\u4e2a\u7279\u5b9a\u7684\u793a\u4f8b\u4e2d\uff0c\u8fd9\u6837\u505a\u6709\u70b9\u611a\u8822\uff0c\u56e0\u4e3a\u4ece CarLot \u4e2d\u83b7\u53d6 Car \u9700\u8981\u4f4e\u6548\u7684\u67e5\u627e(\u4e4b\u95f4\u76f4\u63a5\u4f7f\u7528\u6307\u9488\u8981\u5feb\u5f97\u591a)\u3002\u7136\u800c\uff0c\u901a\u8fc7\u552f\u4e00ID\u800c\u4e0d\u662f\u6307\u9488\u5f15\u7528\u4e1c\u897f\u6709\u4e00\u4e9b\u4f18\u70b9\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5f15\u7528\u5f53\u524d\u4e0d\u5728\u5185\u5b58\u4e2d\u7684\u5185\u5bb9(\u5b83\u4eec\u53ef\u80fd\u5728\u6587\u4ef6\u4e2d\uff0c\u6216\u8005\u5728\u6570\u636e\u5e93\u4e2d\uff0c\u5e76\u4e14\u53ef\u4ee5\u6309\u9700\u52a0\u8f7d)\u3002\u6b64\u5916\uff0c\u6307\u9488\u53ef\u80fd\u5360\u75284\u62168\u4e2a\u5b57\u8282\u2014\u2014\u5982\u679c\u7a7a\u95f4\u975e\u5e38\u6709\u9650\uff0c\u4e14\u60df\u4e00\u5bf9\u8c61\u7684\u6570\u91cf\u76f8\u5f53\u4f4e\uff0c\u90a3\u4e48\u901a\u8fc78\u4f4d\u621616\u4f4d\u6574\u6570\u5f15\u7528\u5b83\u4eec\u53ef\u4ee5\u8282\u7701\u5927\u91cf\u5185\u5b58\u3002 \u7ec4\u5408 vs \u805a\u5408 vs \u5173\u8054 \u4e0b\u9762\u662f\u4e00\u4e2a\u6c47\u603b\u8868\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8bb0\u4f4f\u7ec4\u5408\u3001\u805a\u5408\u548c\u5173\u8054\u4e4b\u95f4\u7684\u533a\u522b: \u5c5e\u6027 \u7ec4\u5408 \u805a\u5408 \u5173\u8054 \u5173\u7cfb\u7c7b\u578b \u6574\u4f53-\u90e8\u5206 \u6574\u4f53-\u90e8\u5206 \u672c\u4e0d\u76f8\u5173 \u6210\u5458\u662f\u5426\u53ef\u4ee5\u5c5e\u4e8e\u4e0d\u540c\u7684\u6574\u4f53\u5bf9\u8c61 No Yes Yes \u6210\u5458\u7684\u5b58\u5728\u6027\u662f\u5426\u7531\u6574\u4f53\u7ba1\u7406 Yes No No \u65b9\u5411 \u5355\u5411 \u5355\u5411 \u5355\u5411\u6216\u53cc\u5411 \u5173\u7cfb\u52a8\u8bcd Part-of Has-a Uses-a","title":"16.4 - \u5173\u8054\u5173\u7cfb"},{"location":"16-4-association/#_1","text":"\u5f62\u6210\u5173\u8054\u5173\u7cfb\u65f6\uff0c\u5bf9\u8c61\u548c\u5bf9\u8c61\u4e4b\u95f4\u9700\u8981\u6ee1\u8db3\u4e0b\u9762\u6761\u4ef6\uff1a \u5173\u8054\u7684\u5bf9\u8c61(\u6210\u5458)\u5728\u5176\u4ed6\u65b9\u9762\u4e0e\u5bf9\u8c61(\u7c7b)\u65e0\u5173\uff1b \u5173\u8054\u7684\u5bf9\u8c61(\u6210\u5458)\u53ef\u4ee5\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u5bf9\u8c61(\u7c7b)\uff1b \u5173\u8054\u5bf9\u8c61(\u6210\u5458)\u7684\u5b58\u5728\u6027\u4e0d\u7531\u5bf9\u8c61(\u7c7b)\u7ba1\u7406\uff1b \u5173\u8054\u7684\u5bf9\u8c61(\u6210\u5458)\u53ef\u80fd\u77e5\u9053\u4e5f\u53ef\u80fd\u4e0d\u77e5\u9053\u5bf9\u8c61(\u7c7b)\u7684\u5b58\u5728\u3002 \u5728\u7ec4\u5408\u6216\u805a\u5408\u4e2d\uff0c\u90e8\u5206\u662f\u6574\u4e2a\u5bf9\u8c61\u7684\u4e00\u90e8\u5206\uff0c\u800c\u5728\u5173\u8054\u4e2d\uff0c\u5173\u8054\u5bf9\u8c61\u5728\u5176\u4ed6\u65b9\u9762\u4e0e\u5bf9\u8c61\u65e0\u5173\u3002\u5c31\u50cf\u805a\u5408\u4e00\u6837\uff0c\u5173\u8054\u7684\u5bf9\u8c61\u53ef\u4ee5\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u5bf9\u8c61\uff0c\u5e76\u4e14\u4e0d\u53d7\u8fd9\u4e9b\u5bf9\u8c61\u7684\u7ba1\u7406\u3002\u7136\u800c\uff0c\u4e0e\u805a\u5408(\u805a\u5408\u7684\u5173\u7cfb\u603b\u662f\u5355\u5411\u7684)\u4e0d\u540c\uff0c\u5728\u5173\u8054\u4e2d\uff0c\u5173\u7cfb\u53ef\u80fd\u662f\u5355\u5411\u7684\u6216\u53cc\u5411\u7684(\u5176\u4e2d\u4e24\u4e2a\u5bf9\u8c61\u5f7c\u6b64\u610f\u8bc6\u5230\u5bf9\u65b9)\u3002 \u533b\u751f\u548c\u75c5\u4eba\u4e4b\u95f4\u7684\u5173\u7cfb\u5c31\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u4f8b\u5b50\u3002\u533b\u751f\u663e\u7136\u4e0e\u4ed6\u7684\u75c5\u4eba\u6709\u4e00\u79cd\u5173\u7cfb\uff0c\u4f46\u4ece\u6982\u5ff5\u4e0a\u8bb2\uff0c\u8fd9\u4e0d\u662f\u4e00\u4e2a\u90e8\u5206/\u6574\u4f53(\u5bf9\u8c61\u7ec4\u6210)\u5173\u7cfb\u3002\u4e00\u4e2a\u533b\u751f\u4e00\u5929\u53ef\u4ee5\u770b\u5f88\u591a\u75c5\u4eba\uff0c\u4e00\u4e2a\u75c5\u4eba\u53ef\u4ee5\u770b\u5f88\u591a\u533b\u751f(\u4e5f\u8bb8\u4ed6\u4eec\u60f3\u8981\u7b2c\u4e8c\u4e2a\u610f\u89c1\uff0c\u6216\u8005\u4ed6\u4eec\u6b63\u5728\u770b\u4e0d\u540c\u7c7b\u578b\u7684\u533b\u751f)\u3002\u8fd9\u4e24\u4e2a\u7269\u4f53\u7684\u5bff\u547d\u90fd\u4e0e\u5bf9\u65b9\u65e0\u5173\u3002 \u6211\u4eec\u53ef\u4ee5\u8bf4\u5173\u8054\u5efa\u6a21\u4e3a\u201c\u4f7f\u7528\u4e00\u4e2a\u201d\u5173\u7cfb\u3002\u533b\u751f\u201c\u4f7f\u7528\u201d\u75c5\u4eba(\u4ee5\u8d5a\u53d6\u6536\u5165)\u3002\u75c5\u4eba\u201c\u4f7f\u7528\u201d\u533b\u751f(\u4e3a\u4e86\u4ed6\u4eec\u9700\u8981\u7684\u4efb\u4f55\u5065\u5eb7\u76ee\u7684)\u3002","title":"\u5173\u8054"},{"location":"16-4-association/#_2","text":"\u56e0\u4e3a\u5173\u8054\u662f\u4e00\u79cd\u6bd4\u8f83\u5bbd\u6cdb\u7684\u5173\u7cfb\u7c7b\u578b\uff0c\u5b83\u4eec\u53ef\u4ee5\u4ee5\u8bb8\u591a\u4e0d\u540c\u7684\u65b9\u5f0f\u5b9e\u73b0\u3002\u7136\u800c\uff0c\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5173\u8054\u662f\u4f7f\u7528\u6307\u9488\u5b9e\u73b0\u7684\uff0c\u5bf9\u8c61\u6307\u5411\u5173\u8054\u5bf9\u8c61\u3002 \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u5b9e\u73b0\u4e00\u4e2a\u53cc\u5411\u7684\u533b\u751f/\u75c5\u4eba\u5173\u7cfb\uff0c\u56e0\u4e3a\u533b\u751f\u77e5\u9053\u8c01\u662f\u4ed6\u4eec\u7684\u75c5\u4eba\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 #include // reference_wrapper #include #include #include // Since Doctor and Patient have a circular dependency, we're going to forward declare Patient class Patient ; class Doctor { private : std :: string m_name {}; std :: vector < std :: reference_wrapper < const Patient >> m_patient {}; public : Doctor ( const std :: string & name ) : m_name { name } { } void addPatient ( Patient & patient ); // We'll implement this function below Patient since we need Patient to be defined at that point friend std :: ostream & operator << ( std :: ostream & out , const Doctor & doctor ); const std :: string & getName () const { return m_name ; } }; class Patient { private : std :: string m_name {}; std :: vector < std :: reference_wrapper < const Doctor >> m_doctor {}; // so that we can use it here // We're going to make addDoctor private because we don't want the public to use it. // They should use Doctor::addPatient() instead, which is publicly exposed void addDoctor ( const Doctor & doctor ) { m_doctor . push_back ( doctor ); } public : Patient ( const std :: string & name ) : m_name { name } { } // We'll implement this function below to parallel operator<<(std::ostream&, const Doctor&) friend std :: ostream & operator << ( std :: ostream & out , const Patient & patient ); const std :: string & getName () const { return m_name ; } // We'll friend Doctor::addPatient() so it can access the private function Patient::addDoctor() friend void Doctor::addPatient ( Patient & patient ); }; void Doctor::addPatient ( Patient & patient ) { // Our doctor will add this patient m_patient . push_back ( patient ); // and the patient will also add this doctor patient . addDoctor ( * this ); } std :: ostream & operator << ( std :: ostream & out , const Doctor & doctor ) { if ( doctor . m_patient . empty ()) { out << doctor . m_name << \" has no patients right now\" ; return out ; } out << doctor . m_name << \" is seeing patients: \" ; for ( const auto & patient : doctor . m_patient ) out << patient . get (). getName () << ' ' ; return out ; } std :: ostream & operator << ( std :: ostream & out , const Patient & patient ) { if ( patient . m_doctor . empty ()) { out << patient . getName () << \" has no doctors right now\" ; return out ; } out << patient . m_name << \" is seeing doctors: \" ; for ( const auto & doctor : patient . m_doctor ) out << doctor . get (). getName () << ' ' ; return out ; } int main () { // Create a Patient outside the scope of the Doctor Patient dave { \"Dave\" }; Patient frank { \"Frank\" }; Patient betsy { \"Betsy\" }; Doctor james { \"James\" }; Doctor scott { \"Scott\" }; james . addPatient ( dave ); scott . addPatient ( dave ); scott . addPatient ( betsy ); std :: cout << james << '\\n' ; std :: cout << scott << '\\n' ; std :: cout << dave << '\\n' ; std :: cout << frank << '\\n' ; std :: cout << betsy << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 2 3 4 5 James is seeing patients: Dave Scott is seeing patients: Dave Betsy Dave is seeing doctors: James Scott Frank has no doctors right now Betsy is seeing doctors: Scott \u4e00\u822c\u6765\u8bf4\uff0c\u5982\u679c\u53ef\u4ee5\u4f7f\u7528\u5355\u5411\u5173\u8054\uff0c\u5219\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u53cc\u5411\u5173\u8054\uff0c\u56e0\u4e3a\u5b83\u4eec\u589e\u52a0\u4e86\u590d\u6742\u6027\uff0c\u800c\u4e14\u5f88\u96be\u5728\u4e0d\u51fa\u9519\u3002","title":"\u5b9e\u73b0\u5173\u8054\u5173\u7cfb"},{"location":"16-4-association/#_3","text":"\u6709\u65f6\u5bf9\u8c61\u53ef\u80fd\u4e0e\u76f8\u540c\u7c7b\u578b\u7684\u5176\u4ed6\u5bf9\u8c61\u6709\u5173\u7cfb\uff0c\u8fd9\u88ab\u79f0\u4e3a \u53cd\u8eab\u5173\u8054 \u3002\u53cd\u8eab\u5173\u8054\u7684\u4e00\u4e2a\u5f88\u597d\u7684\u4f8b\u5b50\u662f\u5927\u5b66\u8bfe\u7a0b\u548c\u5b83\u7684\u5148\u4fee\u8bfe\u7a0b(\u4e5f\u662f\u5927\u5b66\u8bfe\u7a0b)\u4e4b\u95f4\u7684\u5173\u7cfb\u3002 \u8003\u8651\u4e00\u4e2a\u7b80\u5316\u7684\u60c5\u51b5\uff0c\u5176\u4e2d\u4e00\u4e2a\u8bfe\u7a0b\u53ea\u80fd\u6709\u4e00\u4e2a\u5148\u4fee\u8bfe\u7a0b\u3002\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include class Course { private : std :: string m_name ; const Course * m_prerequisite ; public : Course ( const std :: string & name , const Course * prerequisite = nullptr ) : m_name { name }, m_prerequisite { prerequisite } { } }; \u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u7cfb\u5217\u7684\u5173\u8054(\u4e00\u95e8\u8bfe\u7a0b\u6709\u4e00\u4e2a\u5148\u51b3\u6761\u4ef6\uff0c\u53e6\u4e00\u95e8\u8bfe\u7a0b\u4e5f\u6709\u4e00\u4e2a\u5148\u51b3\u6761\u4ef6\uff0c\u7b49\u7b49\u2026\u2026)","title":"\u53cd\u8eab\u5173\u8054"},{"location":"16-4-association/#_4","text":"\u5728\u524d\u9762\u7684\u6240\u6709\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u90fd\u4f7f\u7528\u6307\u9488\u6216\u5f15\u7528\u76f4\u63a5\u5c06\u5bf9\u8c61\u8fde\u63a5\u5728\u4e00\u8d77\u3002\u7136\u800c\u5728\u5173\u8054\u4e2d\uff0c\u5e76\u4e0d\u4e00\u5b9a\u8981\u8fd9\u6837\u505a\u3002\u4efb\u4f55\u80fd\u591f\u5efa\u7acb\u4e24\u4e2a\u5bf9\u8c61\u4e4b\u95f4\u5173\u7cfb\u7684\u6570\u636e\u90fd\u53ef\u4ee5\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86 Driver \u7c7b\u5982\u4f55\u5728\u4e0d\u5b9e\u9645\u5305\u542b Car \u6307\u9488\u6216\u5f15\u7528\u6210\u5458\u7684\u60c5\u51b5\u4e0b\u4e0e Car \u5f62\u6210\u5355\u5411\u5173\u8054\u5173\u7cfb\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 #include #include class Car { private : std :: string m_name ; int m_id ; public : Car ( const std :: string & name , int id ) : m_name { name }, m_id { id } { } const std :: string & getName () const { return m_name ; } int getId () const { return m_id ; } }; // Our CarLot is essentially just a static array of Cars and a lookup function to retrieve them. // Because it's static, we don't need to allocate an object of type CarLot to use it class CarLot { private : static Car s_carLot [ 4 ]; public : CarLot () = delete ; // Ensure we don't try to create a CarLot static Car * getCar ( int id ) { for ( int count { 0 }; count < 4 ; ++ count ) { if ( s_carLot [ count ]. getId () == id ) { return & ( s_carLot [ count ]); } } return nullptr ; } }; Car CarLot :: s_carLot [ 4 ]{ { \"Prius\" , 4 }, { \"Corolla\" , 17 }, { \"Accord\" , 84 }, { \"Matrix\" , 62 } }; class Driver { private : std :: string m_name ; int m_carId ; // we're associated with the Car by ID rather than pointer public : Driver ( const std :: string & name , int carId ) : m_name { name }, m_carId { carId } { } const std :: string & getName () const { return m_name ; } int getCarId () const { return m_carId ; } }; int main () { Driver d { \"Franz\" , 17 }; // Franz is driving the car with ID 17 Car * car { CarLot :: getCar ( d . getCarId ()) }; // Get that car from the car lot if ( car ) std :: cout << d . getName () << \" is driving a \" << car -> getName () << '\\n' ; else std :: cout << d . getName () << \" couldn't find his car \\n \" ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c CarLot \u6301\u6709 car \u3002\u800c Driver \u4f5c\u4e3a\u672c\u8eab\u9700\u8981 car \u7684\u5bf9\u8c61\uff0c\u5374\u5e76\u6ca1\u6709\u6307\u5411 Car \u7684\u6307\u9488\u2014\u2014\u4f46\u662f\u5b83\u6709\u8f66\u7684ID\uff0c\u5b83\u53ef\u4ee5\u901a\u8fc7\u8be5ID\u5728\u9700\u8981\u65f6\u4ece CarLot \u4e2d\u83b7\u53d6 car \u3002 \u5728\u8fd9\u4e2a\u7279\u5b9a\u7684\u793a\u4f8b\u4e2d\uff0c\u8fd9\u6837\u505a\u6709\u70b9\u611a\u8822\uff0c\u56e0\u4e3a\u4ece CarLot \u4e2d\u83b7\u53d6 Car \u9700\u8981\u4f4e\u6548\u7684\u67e5\u627e(\u4e4b\u95f4\u76f4\u63a5\u4f7f\u7528\u6307\u9488\u8981\u5feb\u5f97\u591a)\u3002\u7136\u800c\uff0c\u901a\u8fc7\u552f\u4e00ID\u800c\u4e0d\u662f\u6307\u9488\u5f15\u7528\u4e1c\u897f\u6709\u4e00\u4e9b\u4f18\u70b9\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5f15\u7528\u5f53\u524d\u4e0d\u5728\u5185\u5b58\u4e2d\u7684\u5185\u5bb9(\u5b83\u4eec\u53ef\u80fd\u5728\u6587\u4ef6\u4e2d\uff0c\u6216\u8005\u5728\u6570\u636e\u5e93\u4e2d\uff0c\u5e76\u4e14\u53ef\u4ee5\u6309\u9700\u52a0\u8f7d)\u3002\u6b64\u5916\uff0c\u6307\u9488\u53ef\u80fd\u5360\u75284\u62168\u4e2a\u5b57\u8282\u2014\u2014\u5982\u679c\u7a7a\u95f4\u975e\u5e38\u6709\u9650\uff0c\u4e14\u60df\u4e00\u5bf9\u8c61\u7684\u6570\u91cf\u76f8\u5f53\u4f4e\uff0c\u90a3\u4e48\u901a\u8fc78\u4f4d\u621616\u4f4d\u6574\u6570\u5f15\u7528\u5b83\u4eec\u53ef\u4ee5\u8282\u7701\u5927\u91cf\u5185\u5b58\u3002","title":"\u5173\u8054\u53ef\u4ee5\u662f\u95f4\u63a5\u7684"},{"location":"16-4-association/#vs-vs","text":"\u4e0b\u9762\u662f\u4e00\u4e2a\u6c47\u603b\u8868\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8bb0\u4f4f\u7ec4\u5408\u3001\u805a\u5408\u548c\u5173\u8054\u4e4b\u95f4\u7684\u533a\u522b: \u5c5e\u6027 \u7ec4\u5408 \u805a\u5408 \u5173\u8054 \u5173\u7cfb\u7c7b\u578b \u6574\u4f53-\u90e8\u5206 \u6574\u4f53-\u90e8\u5206 \u672c\u4e0d\u76f8\u5173 \u6210\u5458\u662f\u5426\u53ef\u4ee5\u5c5e\u4e8e\u4e0d\u540c\u7684\u6574\u4f53\u5bf9\u8c61 No Yes Yes \u6210\u5458\u7684\u5b58\u5728\u6027\u662f\u5426\u7531\u6574\u4f53\u7ba1\u7406 Yes No No \u65b9\u5411 \u5355\u5411 \u5355\u5411 \u5355\u5411\u6216\u53cc\u5411 \u5173\u7cfb\u52a8\u8bcd Part-of Has-a Uses-a","title":"\u7ec4\u5408 vs \u805a\u5408 vs \u5173\u8054"},{"location":"16-5-dependencies/","tags":["object","dependencies"],"text":"Key Takeaway - \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u63a2\u8ba8\u4e863\u79cd\u7c7b\u578b\u7684\u5173\u7cfb\uff1a\u7ec4\u5408\u3001\u805a\u5408\u548c\u5173\u8054\u3002\u6211\u4eec\u628a\u6700\u7b80\u5355\u7684\u4e00\u79cd\u5173\u7cfb\u653e\u5728\u6700\u540e\u4ecb\u7ecd\u2014\u2014\u4f9d\u8d56\u5173\u7cfb\u3002 \u5728\u65e5\u5e38\u4ea4\u8c08\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u672f\u8bed\u4f9d\u8d56\u6027\u6765\u8868\u793a\u4e00\u4e2a\u5bf9\u8c61\u4f9d\u8d56\u4e8e\u53e6\u4e00\u4e2a\u5bf9\u8c61\u6765\u5b8c\u6210\u7ed9\u5b9a\u7684\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u7684\u811a\u9aa8\u6298\u4e86\uff0c\u4f60\u5c31\u9700\u8981\u4f9d\u9760\u62d0\u6756\u6765\u8d70\u52a8\u3002\u82b1\u6735\u4f9d\u9760\u871c\u8702\u6388\u7c89\uff0c\u624d\u80fd\u7ed3\u51fa\u679c\u5b9e\u6216\u7e41\u6b96\u540e\u4ee3\u3002 \u5f53\u4e00\u4e2a\u5bf9\u8c61\u4e3a\u4e86\u5b8c\u6210\u67d0\u4e9b\u7279\u5b9a\u4efb\u52a1\u800c\u8c03\u7528\u53e6\u4e00\u4e2a\u5bf9\u8c61\u7684\u529f\u80fd\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u4f9d\u8d56\u5173\u7cfb\u3002\u8fd9\u662f\u4e00\u79cd\u6bd4\u5173\u8054\u5f31\u7684\u5173\u7cfb\uff0c\u4f46\u662f\uff0c\u5bf9\u6240\u4f9d\u8d56\u5bf9\u8c61\u7684\u4efb\u4f55\u66f4\u6539\u90fd\u53ef\u80fd\u7834\u574f(\u4f9d\u8d56\u7684)\u8c03\u7528\u8005\u7684\u529f\u80fd\u3002\u4f9d\u8d56\u5173\u7cfb\u603b\u662f\u5355\u5411\u7684\u5173\u7cfb\u3002 \u4e00\u4e2a\u60a8\u5df2\u7ecf\u89c1\u8fc7\u5f88\u591a\u6b21\u7684\u4f9d\u8d56\u7684\u597d\u4f8b\u5b50\u662f std::ostream \u3002\u6211\u4eec\u4f7f\u7528 std::ostream \u7684\u7c7b\u4f7f\u7528\u5b83\u662f\u4e3a\u4e86\u5b8c\u6210\u5411\u63a7\u5236\u53f0\u6253\u5370\u5185\u5bb9\u7684\u4efb\u52a1\uff0c\u800c\u4e0d\u662f\u4e3a\u4e86\u5176\u4ed6\u76ee\u7684\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include class Point { private : double m_x {}; double m_y {}; double m_z {}; public : Point ( double x = 0.0 , double y = 0.0 , double z = 0.0 ) : m_x { x }, m_y { y }, m_z { z } { } friend std :: ostream & operator << ( std :: ostream & out , const Point & point ); // Point has a dependency on std::ostream here }; std :: ostream & operator << ( std :: ostream & out , const Point & point ) { // Since operator<< is a friend of the Point class, we can access Point's members directly. out << \"Point(\" << point . m_x << \", \" << point . m_y << \", \" << point . m_z << ')' ; return out ; } int main () { Point point1 { 2.0 , 3.0 , 4.0 }; std :: cout << point1 ; // the program has a dependency on std::cout here return 0 ; } \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c Point \u4e0e std::ostream \u6ca1\u6709\u76f4\u63a5\u5173\u7cfb\uff0c\u4f46\u662f\u5b83\u4f9d\u8d56\u4e8e std::ostream \uff0c\u56e0\u4e3a\u64cd\u4f5c\u7b26 << \u4f7f\u7528 std::ostream \u5c06 Point \u6253\u5370\u5230\u63a7\u5236\u53f0\u3002 C++\u4e2d\u7684\u4f9d\u8d56\u548c\u5173\u8054 \u4f9d\u8d56\u5173\u7cfb\u548c\u5173\u8054\u4e4b\u95f4\u7684\u533a\u522b\u901a\u5e38\u5bb9\u6613\u6df7\u6dc6\u3002 \u5728C++\u4e2d\uff0c\u5173\u8054\u662f\u7c7b\u7ea7\u522b\u4e0a\u4e24\u4e2a\u7c7b\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u4e00\u4e2a\u7c7b\u4fdd\u6301\u4e0e\u5173\u8054\u7c7b\u7684\u76f4\u63a5\u6216\u95f4\u63a5\u201c\u94fe\u63a5\u201d\u4f5c\u4e3a\u6210\u5458\u3002\u4f8b\u5982\uff0c Doctor \u7c7b\u6709\u4e00\u4e2a\u6307\u5411\u5176 Patients \u6210\u5458\u7684\u6307\u9488\u6570\u7ec4\u3002\u4f60\u53ef\u4ee5\u95ee\u533b\u751f\u8c01\u662f\u4ed6\u7684\u75c5\u4eba\u3002 Driver \u4e2d\u6709\u4e00\u4e2a\u6574\u578b\u6210\u5458\uff0c\u8868\u793a driver \u5bf9\u8c61\u62e5\u6709\u7684 Car \u3002\u9a7e\u9a76\u5458\u603b\u662f\u77e5\u9053\u4e0e\u5b83\u76f8\u5173\u8054\u7684\u662f\u4ec0\u4e48\u8f66\u3002 \u4f9d\u8d56\u9879\u901a\u5e38\u4e0d\u662f\u7c7b\u5c42\u9762\u7684\u6982\u5ff5\u2014\u2014\u4e5f\u5c31\u662f\u8bf4\uff0c\u6240\u4f9d\u8d56\u7684\u5bf9\u8c61\u4e0d\u662f\u4f5c\u4e3a\u7c7b\u6210\u5458\u51fa\u73b0\u7684\u3002\u76f8\u53cd\uff0c\u6240\u4f9d\u8d56\u7684\u5bf9\u8c61\u901a\u5e38\u6839\u636e\u9700\u8981\u5b9e\u4f8b\u5316(\u5982\u6253\u5f00\u6587\u4ef6\u5199\u5165\u6570\u636e)\uff0c\u6216\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u5230\u51fd\u6570\u4e2d(\u5982\u4e0a\u9762\u91cd\u8f7d\u64cd\u4f5c\u7b26 << \u4e2d\u7684 std::ostream )\u3002 \u8f7b\u677e\u4e00\u523b \u4f9d\u8d56 (\u8f6c\u8f7d\u81ea xkcd )\uff1a \u5f53\u7136\uff0c\u6211\u4eec\u5176\u5b9e\u90fd\u77e5\u9053\uff0c\u8fd9\u5b9e\u9645\u4e0a\u662f\u4e00\u79cd \u53cd\u8eab\u5173\u8054 \u3002","title":"16.5 - \u4f9d\u8d56\u5173\u7cfb"},{"location":"16-5-dependencies/#c","text":"\u4f9d\u8d56\u5173\u7cfb\u548c\u5173\u8054\u4e4b\u95f4\u7684\u533a\u522b\u901a\u5e38\u5bb9\u6613\u6df7\u6dc6\u3002 \u5728C++\u4e2d\uff0c\u5173\u8054\u662f\u7c7b\u7ea7\u522b\u4e0a\u4e24\u4e2a\u7c7b\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u4e00\u4e2a\u7c7b\u4fdd\u6301\u4e0e\u5173\u8054\u7c7b\u7684\u76f4\u63a5\u6216\u95f4\u63a5\u201c\u94fe\u63a5\u201d\u4f5c\u4e3a\u6210\u5458\u3002\u4f8b\u5982\uff0c Doctor \u7c7b\u6709\u4e00\u4e2a\u6307\u5411\u5176 Patients \u6210\u5458\u7684\u6307\u9488\u6570\u7ec4\u3002\u4f60\u53ef\u4ee5\u95ee\u533b\u751f\u8c01\u662f\u4ed6\u7684\u75c5\u4eba\u3002 Driver \u4e2d\u6709\u4e00\u4e2a\u6574\u578b\u6210\u5458\uff0c\u8868\u793a driver \u5bf9\u8c61\u62e5\u6709\u7684 Car \u3002\u9a7e\u9a76\u5458\u603b\u662f\u77e5\u9053\u4e0e\u5b83\u76f8\u5173\u8054\u7684\u662f\u4ec0\u4e48\u8f66\u3002 \u4f9d\u8d56\u9879\u901a\u5e38\u4e0d\u662f\u7c7b\u5c42\u9762\u7684\u6982\u5ff5\u2014\u2014\u4e5f\u5c31\u662f\u8bf4\uff0c\u6240\u4f9d\u8d56\u7684\u5bf9\u8c61\u4e0d\u662f\u4f5c\u4e3a\u7c7b\u6210\u5458\u51fa\u73b0\u7684\u3002\u76f8\u53cd\uff0c\u6240\u4f9d\u8d56\u7684\u5bf9\u8c61\u901a\u5e38\u6839\u636e\u9700\u8981\u5b9e\u4f8b\u5316(\u5982\u6253\u5f00\u6587\u4ef6\u5199\u5165\u6570\u636e)\uff0c\u6216\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u5230\u51fd\u6570\u4e2d(\u5982\u4e0a\u9762\u91cd\u8f7d\u64cd\u4f5c\u7b26 << \u4e2d\u7684 std::ostream )\u3002","title":"C++\u4e2d\u7684\u4f9d\u8d56\u548c\u5173\u8054"},{"location":"16-5-dependencies/#_1","text":"\u4f9d\u8d56 (\u8f6c\u8f7d\u81ea xkcd )\uff1a \u5f53\u7136\uff0c\u6211\u4eec\u5176\u5b9e\u90fd\u77e5\u9053\uff0c\u8fd9\u5b9e\u9645\u4e0a\u662f\u4e00\u79cd \u53cd\u8eab\u5173\u8054 \u3002","title":"\u8f7b\u677e\u4e00\u523b"},{"location":"16-6-container-classes/","tags":["object","container classes"],"text":"Key Takeaway - \u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\u6211\u4eec\u7ecf\u5e38\u4f1a\u7528\u5230\u5bb9\u5668\u3002\u9ea6\u7247\u662f\u88c5\u5728\u76d2\u5b50\u91cc\u7684\uff0c\u4e66\u4e5f\u6709\u5c01\u9762\u548c\u88c5\u8ba2\uff0c\u4f60\u7684\u8f66\u5e93\u91cc\u5e94\u8be5\u4e5f\u88c5\u4e86\u4e0d\u5c11\u4e1c\u897f\u5427\u3002\u5982\u679c\u6ca1\u6709\u5bb9\u5668\uff0c\u4f7f\u7528\u8fd9\u4e9b\u4e1c\u897f\u7684\u65f6\u5019\u4e00\u5b9a\u4f1a\u975e\u5e38\u7684\u6df7\u4e71\u3002\u5bb9\u5668\u6700\u5927\u7684\u529f\u80fd\uff0c\u5c31\u662f\u5e2e\u52a9\u6211\u4eec\u7ec4\u7ec7\u548c\u5b58\u50a8\u5b58\u5728\u5bb9\u5668\u4e2d\u7684\u4e1c\u897f\u3002 \u7c7b\u4f3c\u7684\uff0c \u5bb9\u5668\u7c7b \u662f\u4e00\u79cd\u88ab\u8bbe\u8ba1\u7528\u6765\u5b58\u653e\u548c\u7ec4\u7ec7\u591a\u79cd\u5176\u4ed6\u7c7b\u578b\uff08\u53ef\u4ee5\u662f\u7c7b\u4e5f\u53ef\u4ee5\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff09\u5b9e\u4f8b\u7684\u7c7b\u3002\u5bb9\u5668\u7c7b\u7684\u79cd\u7c7b\u6709\u5f88\u591a\uff0c\u6bcf\u79cd\u90fd\u6709\u5176\u81ea\u5df1\u7684\u4f18\u52bf\u548c\u52a3\u52bf\uff0c\u4ee5\u53ca\u4f7f\u7528\u9650\u5236\u3002\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6700\u6709\u7528\u7684\u5bb9\u5668\u662f\u6570\u7ec4\uff0c\u4f60\u5728\u524d\u9762\u7684\u4f8b\u5b50\u4e2d\u4ee5\u53ca\u591a\u6b21\u4f7f\u7528\u5b83\u4e86\u3002\u5c3d\u7ba1C++ \u63d0\u4f9b\u4e86\u5185\u5efa\u7684\u6570\u7ec4\u529f\u80fd\uff0c\u7a0b\u5e8f\u5458\u5374\u5927\u591a\u4f7f\u7528\u6570\u7ec4\u5bb9\u5668\u7c7b\uff08 std::array \u6216 std::vector ) \u3002\u548c\u5185\u5efa\u7684\u6570\u7ec4\u4e0d\u540c\uff0c\u8fd9\u4e9b\u6570\u7ec4\u5bb9\u5668\u7c7b\u901a\u5e38\u53ef\u4ee5\u52a8\u6001\u5730\u8c03\u6574\u5176\u5927\u5c0f\uff08\u5f53\u8d44\u6e90\u88ab\u6dfb\u52a0\u6216\u5220\u9664\u65f6\uff09\u3001\u53ef\u4ee5\u5728\u4f20\u9012\u5230\u51fd\u6570\u65f6\u4fdd\u7559\u5176\u5927\u5c0f\u4fe1\u606f\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u8fb9\u754c\u68c0\u67e5\u3002\u8fd9\u4e9b\u529f\u80fd\u4e0d\u4f46\u4f7f\u5f97\u6570\u7ec4\u5bb9\u5668\u7c7b\u7b80\u5355\u6613\u7528\uff0c\u800c\u4e14\u5b89\u5168\u6027\u4e5f\u66f4\u597d\u3002 \u5bb9\u5668\u7c7b\u901a\u5e38\u4f1a\u5b9e\u73b0\u4e00\u7cfb\u5217\u6807\u51c6\u7684\u3001\u6700\u5c0f\u5316\u7684\u529f\u80fd\u3002\u5927\u591a\u6570\u5b9e\u73b0\u826f\u597d\u7684\u5bb9\u5668\u7c7b\u901a\u5e38\u5305\u62ec\u4e0b\u9762\u8fd9\u4e9b\u529f\u80fd\uff1a \u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u5bb9\u5668\uff08\u901a\u8fc7 \u6784\u9020\u51fd\u6570 \uff09\uff1b \u5411\u5bb9\u5668\u6dfb\u52a0\u4e00\u4e2a\u5bf9\u8c61\uff1b \u4ece\u5bb9\u5668\u4e2d\u5220\u9664\u4e00\u4e2a\u5bf9\u8c61\uff1b \u62a5\u544a\u5bb9\u5668\u4e2d\u5bf9\u8c61\u7684\u4e2a\u6570\uff1b \u6e05\u7a7a\u5bb9\u5668\uff1b \u63d0\u4f9b\u5b58\u50a8\u5bf9\u8c61\u7684\u8bbf\u95ee\u65b9\u5f0f\uff1b \u5143\u7d20\u6392\u5e8f\uff08\u53ef\u9009\u7684\uff09\u3002 \u6709\u4e9b\u5bb9\u5668\u4f1a\u5ffd\u7565\u4e0a\u8ff0\u529f\u80fd\u4e2d\u7684\u67d0\u4e9b\u3002\u4f8b\u5982\uff0c\u6570\u7ec4\u5bb9\u5668\u7c7b\u901a\u5e38\u4f1a\u5ffd\u7565\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\uff0c\u56e0\u4e3a\u6570\u7ec4\u6570\u636e\u7ed3\u6784\u5728\u8fdb\u884c\u63d2\u5165\u548c\u5220\u9664\u65f6\u6548\u7387\u5f88\u4f4e\uff0c\u6240\u4ee5\u7c7b\u7684\u8bbe\u8ba1\u8005\u4e0d\u9f13\u52b1\u6211\u4eec\u4f7f\u7528\u8fd9\u4e24\u79cd\u64cd\u4f5c\u3002 \u5bb9\u5668\u7c7b\u5b9e\u73b0\u4e86\u4e00\u79cd \u6210\u5458\u5173\u7cfb \u3002\u4f8b\u5982\uff0c\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u662f\u8be5\u6570\u7ec4\u7684\u4e00\u4e2a\u6210\u5458\uff08\u5c5e\u4e8e\uff09\u3002\u6ce8\u610f\uff0c\u6211\u4eec\u8fd9\u91cc\u8bf4\u7684\u6210\u5458\u662f\u5e73\u5e38\u610f\u4e49\u4e0a\u7684\u6210\u5458\uff0c\u800c\u4e0d\u662f\u6307 \u7c7b\u6210\u5458 \u3002 \u5bb9\u5668\u7c7b\u578b \u5bb9\u5668\u7c7b\u901a\u5e38\u6709\u4e24\u4e2a\u53d8\u79cd\u3002 \u503c\u5bb9\u5668 \u662f \u7ec4\u5408\u5173\u7cfb \uff0c\u5b83\u5b58\u653e\u4e86\u5176\u5143\u7d20\u7684\u62f7\u8d1d\uff08\u56e0\u6b64\u5fc5\u987b\u8d1f\u8d23\u521b\u5efa\u548c\u9500\u6bc1\u8fd9\u4e9b\u62f7\u8d1d\uff09\u3002 \u5f15\u7528\u5bb9\u5668 \u5219\u662f \u805a\u5408\u5173\u7cfb \uff0c\u5b83\u4fdd\u5b58\u4e86\u5143\u7d20\u7684\u6307\u9488\u6216\u5f15\u7528\uff08\u56e0\u6b64\u65e0\u9700\u64cd\u5fc3\u5b83\u4eec\u7684\u521b\u5efa\u548c\u9500\u6bc1\uff09\u3002 \u6570\u7ec4\u5bb9\u5668\u7c7b \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ece\u5934\u7f16\u5199\u4e00\u4e2a\u6574\u578b\u6570\u7ec4\u7c7b\u5e76\u5b9e\u73b0\u5b83\u5e94\u8be5\u63d0\u4f9b\u7684\u5927\u90e8\u5206\u529f\u80fd\u3002\u8be5\u6570\u7ec4\u7c7b\u662f\u4e00\u4e2a \u503c\u5bb9\u5668 \uff0c\u5b83\u4fdd\u5b58\u5143\u7d20\u7684\u62f7\u8d1d\u3002\u548c\u5b83\u4eec\u7684\u540d\u5b57\u6697\u793a\u7684\u90a3\u6837\uff0c\u8fd9\u4e2a\u5bb9\u5668\u4f1a\u4fdd\u5b58\u4e00\u4e2a\u6574\u578b\u6570\u6784\u6210\u7684\u6570\u7ec4\uff0c\u7c7b\u4f3c std::vector \u3002 \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u521b\u5efa IntArray.h \u6587\u4ef6\uff1a 1 2 3 4 5 6 7 8 #ifndef INTARRAY_H #define INTARRAY_H class IntArray { }; #endif IntArray \u9700\u8981\u8ffd\u8e2a\u4e24\u4e2a\u503c\uff1a\u6570\u636e\u548c\u6570\u7ec4\u5927\u5c0f\u3002\u56e0\u4e3a\u6211\u4eec\u5e0c\u671b\u6570\u7ec4\u80fd\u591f\u6539\u53d8\u5176\u5927\u5c0f\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u52a8\u6001\u5206\u914d\u5185\u5b58\uff0c\u56e0\u6b64\u5fc5\u987b\u4f7f\u7528\u6307\u9488\u6765\u4fdd\u5b58\u6570\u636e\u3002 1 2 3 4 5 6 7 8 9 10 11 #ifndef INTARRAY_H #define INTARRAY_H class IntArray { private : int m_length {}; int * m_data {}; }; #endif \u63a5\u4e0b\u6765\uff0c\u6dfb\u52a0\u7528\u4e8e\u521b\u5efa IntArrays \u7684\u6784\u9020\u51fd\u6570\u3002\u8fd9\u91cc\u6211\u4eec\u9700\u8981\u4e24\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u4e00\u4e2a\u521b\u5efa\u7a7a\u7684\u6570\u7ec4\uff0c\u4e00\u4e2a\u5219\u521b\u5efa\u9884\u5b9a\u4e49\u957f\u5ea6\u7684\u6570\u7ec4\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #ifndef INTARRAY_H #define INTARRAY_H #include // for assert() class IntArray { private : int m_length {}; int * m_data {}; public : IntArray () = default ; IntArray ( int length ) : m_length { length } { assert ( length >= 0 ); if ( length > 0 ) m_data = new int [ length ]{}; } }; #endif \u6211\u4eec\u8fd8\u9700\u8981\u521b\u5efa\u7528\u4e8e\u6e05\u7406\u6570\u7ec4\u7684\u51fd\u6570\u3002\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a \u6790\u6784\u51fd\u6570 \uff0c\u5b83\u7684\u529f\u80fd\u5c31\u662f\u91ca\u653e\u6570\u636e\u5360\u7528\u7684\u5185\u5b58\u3002\u7136\u540e\uff0c\u518d\u7f16\u5199\u4e00\u4e2a erase() \u51fd\u6570\u5c06\u6570\u7ec4\u7684\u5143\u7d20\u6e05\u9664\u5e76\u5c06\u957f\u5ea6\u8bbe\u7f6e\u4e3a0\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ~ IntArray () { delete [] m_data ; // \u8fd9\u91cc\u4e0d\u5fc5\u5c06 m_data \u8bbe\u7f6e\u4e3a null \u4e5f\u4e0d\u7528\u5c06 m_length \u8bbe\u7f6e\u4e3a0\uff0c\u56e0\u4e3a\u5bf9\u8c61\u9a6c\u4e0a\u5c31\u4f1a\u88ab\u9500\u6bc1 } void erase () { delete [] m_data ; // \u5fc5\u987b\u786e\u4fdd m_data \u662f nullptr \u5426\u5219\u5c06\u4f1a\u79f0\u4e3a\u60ac\u5782\u6307\u9488 m_data = nullptr ; m_length = 0 ; } \u73b0\u5728\uff0c \u91cd\u8f7d [] \u64cd\u4f5c\u7b26\u7528\u4e8e\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u5728\u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26\u7684\u65f6\u5019\uff0c\u5fc5\u987b\u786e\u4fdd\u7d22\u5f15\u662f\u5408\u6cd5\u7684\uff0c\u8be5\u64cd\u4f5c\u53ef\u4ee5\u4f7f\u7528 assert() \u51fd\u6570\u3002\u6211\u4eec\u8fd8\u9700\u8981\u6dfb\u52a0\u4e00\u4e2a\u51fd\u6570\u7528\u4e8e\u8fd4\u56de\u6570\u7ec4\u957f\u5ea6\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u7c7b\u770b\u4e0a\u53bb\u4f1a\u5411\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #ifndef INTARRAY_H #define INTARRAY_H #include // for assert() class IntArray { private : int m_length {}; int * m_data {}; public : IntArray () = default ; IntArray ( int length ) : m_length { length } { assert ( length >= 0 ); if ( length > 0 ) m_data = new int [ length ]{}; } ~ IntArray () { delete [] m_data ; // \u8fd9\u91cc\u4e0d\u5fc5\u5c06 m_data \u8bbe\u7f6e\u4e3a null \u4e5f\u4e0d\u7528\u5c06 m_length \u8bbe\u7f6e\u4e3a0\uff0c\u56e0\u4e3a\u5bf9\u8c61\u9a6c\u4e0a\u5c31\u4f1a\u88ab\u9500\u6bc1 } void erase () { delete [] m_data ; // \u5fc5\u987b\u786e\u4fdd m_data \u662f nullptr \u5426\u5219\u5c06\u4f1a\u79f0\u4e3a\u60ac\u5782\u6307\u9488 m_data = nullptr ; m_length = 0 ; } int & operator []( int index ) { assert ( index >= 0 && index < m_length ); return m_data [ index ]; } int getLength () const { return m_length ; } }; #endif \u81f3\u6b64\uff0c IntArray \u7c7b\u5df2\u7ecf\u5b9e\u73b0\u5b8c\u6210\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e86\u3002\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6709\u9884\u5b9a\u4e49\u957f\u5ea6\u7684 IntArrays \uff0c\u7136\u540e\u4f7f\u7528\u4e0b\u6807\u8fd0\u7b97\u7b26\u83b7\u53d6\u5143\u7d20\u7684\u503c\u3002 \u4f46\u662f\uff0c IntArray \u4ecd\u7136\u8fd8\u6709\u4e9b\u529f\u80fd\u6ca1\u6709\u88ab\u5b9e\u73b0\u3002\u6570\u7ec4\u5927\u5c0f\u6682\u65f6\u8fd8\u4e0d\u80fd\u6539\u53d8\u3001\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4e5f\u8fd8\u4e0d\u80fd\u88ab\u63d2\u5165\u6216\u5220\u9664\u3001\u4e0d\u4ec5\u5982\u6b64\uff0c\u6570\u7ec4\u5143\u7d20\u4e5f\u8fd8\u4e0d\u80fd\u88ab\u6392\u5e8f\u3002 \u9996\u5148\uff0c\u7f16\u5199\u4ee3\u7801\u4f7f\u5f97\u6570\u7ec4\u53ef\u4ee5\u6539\u53d8\u5176\u5c3a\u5bf8\u3002\u800c\u4e14\uff0c\u6211\u4eec\u4f1a\u5b9e\u73b0\u4e24\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u7b2c\u4e00\u4e2a\u51fd\u6570\u662f reallocate() \uff0c\u8fd9\u4e2a\u51fd\u6570\u5728\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\u7684\u65f6\u5019\uff0c\u4f1a\u9500\u6bc1\u6570\u7ec4\u4e2d\u7684\u5168\u90e8\u5143\u7d20\uff0c\u4f46\u662f\u5b83\u7684\u901f\u5ea6\u5f88\u5feb\u3002\u7b2c\u4e8c\u4e2a\u51fd\u6570\u662f resize() \uff0c\u5b83\u5728\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u4fdd\u6301\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u4f46\u662f\u8be5\u51fd\u6570\u7684\u6548\u7387\u4f1a\u4f4e\u4e00\u4e9b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 // reallocate \u4f1a\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\uff0c\u4f46\u662f\u5b83\u4f1a\u5728\u5de5\u4f5c\u65f6\u9500\u6bc1\u6240\u6709\u7684\u6570\u7ec4\u5143\u7d20\u3002\u8be5\u51fd\u6570\u7684\u901f\u5ea6\u5f88\u5feb void reallocate ( int newLength ) { // \u9996\u5148\uff0c\u5220\u9664\u5168\u90e8\u7684\u5143\u7d20 erase (); // \u5982\u679c\u8981\u521b\u5efa\u7a7a\u6570\u7ec4\uff0c\u6b64\u5904\u8fd4\u56de\u5373\u53ef if ( newLength <= 0 ) return ; // \u5206\u914d\u65b0\u7684\u5143\u7d20 m_data = new int [ newLength ]; m_length = newLength ; } // resize \u4f1a\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\uff0c\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4f1a\u88ab\u4fdd\u5b58\u4e0b\u6765\u3002\u8be5\u51fd\u6570\u7684\u901f\u5ea6\u6bd4\u8f83\u6162 void resize ( int newLength ) { // \u5982\u679c\u6570\u7ec4\u7684\u957f\u5ea6\u5df2\u7ecf\u6ee1\u8db3\u8981\u6c42\uff0c\u5b8c\u6210 if ( newLength == m_length ) return ; // \u5982\u679c\u9700\u8981\u5c06\u6570\u7ec4\u8c03\u6574\u4e3a\u4e00\u4e2a\u7a7a\u6570\u7ec4\uff0c\u5b8c\u6210\u5e76\u8fd4\u56de if ( newLength <= 0 ) { erase (); return ; } // \u5047\u8bbe newLength \u5927\u4e8e\u7b49\u4e8e1\u3002\u8be5\u7b97\u6cd5\u7684\u539f\u7406\u5982\u4e0b\uff1a // \u9996\u5148\uff0c\u5206\u914d\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\u3002\u7136\u540e\u5c06\u65e7\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u90fd\u62f7\u8d1d\u5230\u65b0\u7684\u6570\u7ec4\u4e2d\u3002\u5b8c\u6210\u540e\uff0c\u9500\u6bc1\u65e7\u6570\u7ec4\u3002 // \u7136\u540e\u8ba9 m_data \u6307\u5411\u65b0\u7684\u6570\u7ec4 // \u5206\u914d\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4 int * data { new int [ newLength ] }; // \u786e\u5b9a\u6709\u591a\u5c11\u4e2a\u5143\u7d20\u9700\u8981\u4ece\u65e7\u6570\u7ec4\u4e2d\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\u3002 // \u8fd9\u91cc\u6211\u4eec\u5e0c\u671b\u4ece\u65e7\u6570\u7ec4\u4e2d\u62f7\u8d1d\u5c3d\u53ef\u80fd\u591a\u7684\u5143\u7d20\uff08\u65b0\u6570\u7ec4\u4e0d\u4e00\u5b9a\u6bd4\u65e7\u6570\u7ec4\u5927\uff09 // \u62f7\u8d1d\u7684\u5143\u7d20\u4e2a\u6570\u662f\u65b0\u65e7\u4e24\u4e2a\u6570\u7ec4\u4e2d\u8f83\u5c0f\u7684\u4e00\u4e2a if ( m_length > 0 ) { int elementsToCopy { ( newLength > m_length ) ? m_length : newLength }; // Now copy the elements one by one for ( int index { 0 }; index < elementsToCopy ; ++ index ) data [ index ] = m_data [ index ]; } // \u5220\u9664\u65e7\u6570\u7ec4 delete [] m_data ; // \u5f00\u59cb\u4f7f\u7528\u65b0\u7684\u6570\u7ec4\u4f5c\u4e3a\u6570\u636e\uff01\u6700\u7b80\u5355\u7684\u529e\u6cd5\u5c31\u662f\u8ba9 m_data\u6307\u5411\u65b0\u6570\u7ec4\u7684\u5730\u5740\u3002 // \u7531\u4e8e\u8be5\u6570\u7ec4\u662f\u52a8\u6001\u5206\u914d\u7684\uff0c\u6240\u4ee5\u4e0d\u4f1a\u5728\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\u88ab\u9500\u6bc1 m_data = data ; m_length = newLength ; } \u8fd8\u633a\u590d\u6742\uff01 \u591a\u6570\u7684\u6570\u7ec4\u5bb9\u5668\u7c7b\u4f1a\u6b62\u6b65\u4e8e\u6b64\u3002\u4e0d\u8fc7\uff0c\u4e5f\u8bb8\u4f60\u60f3\u77e5\u9053\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u5e94\u8be5\u5982\u4f55\u5b9e\u73b0\uff0c\u6240\u4ee5\u6211\u4eec\u7ee7\u7eed\u5b9e\u73b0\u8fd9\u4e24\u4e2a\u529f\u80fd\u3002\u5176\u5b9e\u73b0\u65b9\u6cd5\u548c resize() \u975e\u5e38\u7c7b\u4f3c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 void insertBefore ( int value , int index ) { // \u68c0\u67e5\u6570\u7ec4\u7d22\u5f15\u5408\u6cd5\u6027 assert ( index >= 0 && index <= m_length ); // \u9996\u5148\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u5176\u957f\u5ea6\u6bd4\u65e7\u6570\u7ec4\u59271 int * data { new int [ m_length + 1 ] }; // \u62f7\u8d1d\u5230\u5f53\u524d\u7d22\u5f15\u7684\u6240\u6709\u5143\u7d20 for ( int before { 0 }; before < index ; ++ before ) data [ before ] = m_data [ before ]; // \u63d2\u5165\u65b0\u7684\u5143\u7d20\u5230\u65b0\u6570\u7ec4 data [ index ] = value ; // \u7ee7\u7eed\u62f7\u8d1d\u5269\u4f59\u7684\u5143\u7d20 for ( int after { index }; after < m_length ; ++ after ) data [ after + 1 ] = m_data [ after ]; // \u6700\u540e\uff0c\u5220\u9664\u65e7\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528\u65b0\u7684\u6570\u7ec4\u4f5c\u4e3a\u6570\u636e delete [] m_data ; m_data = data ; ++ m_length ; } void remove ( int index ) { // \u68c0\u67e5\u6570\u7ec4\u7d22\u5f15\u5408\u6cd5\u6027 assert ( index >= 0 && index < m_length ); // \u5982\u679c\u8fd9\u5df2\u7ecf\u662f\u6700\u540e\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20\uff0c\u5c06\u6570\u7ec4\u8bbe\u7f6e\u4e3a\u7a7a\u6570\u7ec4\u7136\u540e\u5b8c\u6210 if ( m_length == 1 ) { erase (); return ; } // \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5176\u5927\u5c0f\u6bd4\u65e7\u6570\u7ec4\u5c0f1 int * data { new int [ m_length -1 ] }; // \u62f7\u8d1d\u5230\u7d22\u5f15\u7684\u5168\u90e8\u5143\u7d20 for ( int before { 0 }; before < index ; ++ before ) data [ before ] = m_data [ before ]; // \u7ee7\u7eed\u62f7\u8d1d\u88ab\u5220\u9664\u5143\u7d20\u540e\u9762\u7684\u6240\u6709\u5143\u7d20 for ( int after { index + 1 }; after < m_length ; ++ after ) data [ after -1 ] = m_data [ after ]; // \u6700\u540e\uff0c\u5220\u9664\u65e7\u6570\u7ec4\u5e76\u4f7f\u7528\u65b0\u7684\u6570\u7ec4\u4f5c\u4e3a\u6570\u636e delete [] m_data ; m_data = data ; -- m_length ; } // A couple of additional functions just for convenience void insertAtBeginning ( int value ) { insertBefore ( value , 0 ); } void insertAtEnd ( int value ) { insertBefore ( value , m_length ); } IntArray \u5bb9\u5668\u7c7b\u7684\u5b8c\u6574\u5b9a\u4e49\u5982\u4e0b\uff1a IntArray.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 #ifndef INTARRAY_H #define INTARRAY_H #include // for assert() class IntArray { private : int m_length {}; int * m_data {}; public : IntArray () = default ; IntArray ( int length ) : m_length { length } { assert ( length >= 0 ); if ( length > 0 ) m_data = new int [ length ]{}; } ~ IntArray () { delete [] m_data ; // we don't need to set m_data to null or m_length to 0 here, since the object will be destroyed immediately after this function anyway } void erase () { delete [] m_data ; // We need to make sure we set m_data to nullptr here, otherwise it will // be left pointing at deallocated memory! m_data = nullptr ; m_length = 0 ; } int & operator []( int index ) { assert ( index >= 0 && index < m_length ); return m_data [ index ]; } // reallocate resizes the array. Any existing elements will be destroyed. This function operates quickly. void reallocate ( int newLength ) { // First we delete any existing elements erase (); // If our array is going to be empty now, return here if ( newLength <= 0 ) return ; // Then we have to allocate new elements m_data = new int [ newLength ]; m_length = newLength ; } // resize resizes the array. Any existing elements will be kept. This function operates slowly. void resize ( int newLength ) { // if the array is already the right length, we're done if ( newLength == m_length ) return ; // If we are resizing to an empty array, do that and return if ( newLength <= 0 ) { erase (); return ; } // Now we can assume newLength is at least 1 element. This algorithm // works as follows: First we are going to allocate a new array. Then we // are going to copy elements from the existing array to the new array. // Once that is done, we can destroy the old array, and make m_data // point to the new array. // First we have to allocate a new array int * data { new int [ newLength ] }; // Then we have to figure out how many elements to copy from the existing // array to the new array. We want to copy as many elements as there are // in the smaller of the two arrays. if ( m_length > 0 ) { int elementsToCopy { ( newLength > m_length ) ? m_length : newLength }; // Now copy the elements one by one for ( int index { 0 }; index < elementsToCopy ; ++ index ) data [ index ] = m_data [ index ]; } // Now we can delete the old array because we don't need it any more delete [] m_data ; // And use the new array instead! Note that this simply makes m_data point // to the same address as the new array we dynamically allocated. Because // data was dynamically allocated, it won't be destroyed when it goes out of scope. m_data = data ; m_length = newLength ; } void insertBefore ( int value , int index ) { // Sanity check our index value assert ( index >= 0 && index <= m_length ); // First create a new array one element larger than the old array int * data { new int [ m_length + 1 ] }; // Copy all of the elements up to the index for ( int before { 0 }; before < index ; ++ before ) data [ before ] = m_data [ before ]; // Insert our new element into the new array data [ index ] = value ; // Copy all of the values after the inserted element for ( int after { index }; after < m_length ; ++ after ) data [ after + 1 ] = m_data [ after ]; // Finally, delete the old array, and use the new array instead delete [] m_data ; m_data = data ; ++ m_length ; } void remove ( int index ) { // Sanity check our index value assert ( index >= 0 && index < m_length ); // If we're removing the last element in the array, we can just erase the array and return early if ( m_length == 1 ) { erase (); return ; } // First create a new array one element smaller than the old array int * data { new int [ m_length -1 ] }; // Copy all of the elements up to the index for ( int before { 0 }; before < index ; ++ before ) data [ before ] = m_data [ before ]; // Copy all of the values after the removed element for ( int after { index + 1 }; after < m_length ; ++ after ) data [ after -1 ] = m_data [ after ]; // Finally, delete the old array, and use the new array instead delete [] m_data ; m_data = data ; -- m_length ; } // A couple of additional functions just for convenience void insertAtBeginning ( int value ) { insertBefore ( value , 0 ); } void insertAtEnd ( int value ) { insertBefore ( value , m_length ); } int getLength () const { return m_length ; } }; #endif \u8bd5\u8bd5\u770b\uff0c\u80fd\u4e0d\u80fd\u6b63\u786e\u5de5\u4f5c\uff01 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include #include \"IntArray.h\" int main () { // Declare an array with 10 elements IntArray array ( 10 ); // Fill the array with numbers 1 through 10 for ( int i { 0 }; i < 10 ; ++ i ) array [ i ] = i + 1 ; // Resize the array to 8 elements array . resize ( 8 ); // Insert the number 20 before element with index 5 array . insertBefore ( 20 , 5 ); // Remove the element with index 3 array . remove ( 3 ); // Add 30 and 40 to the end and beginning array . insertAtEnd ( 30 ); array . insertAtBeginning ( 40 ); // Print out all the numbers for ( int i { 0 }; i < array . getLength (); ++ i ) std :: cout << array [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 40 1 2 3 5 20 6 7 8 30 \u867d\u7136\u7f16\u5199\u4e00\u4e2a\u5bb9\u5668\u7c7b\u662f\u975e\u5e38\u590d\u6742\u7684\u4e8b\u60c5\uff0c\u597d\u6d88\u606f\u662f\u4f60\u53ea\u9700\u8981\u7f16\u5199\u4e00\u6b21\u5373\u53ef\u3002\u4e00\u65e6\u67d0\u4e2a\u5bb9\u5668\u7c7b\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\u3002\u90a3\u4e48\u4f60\u63a5\u4e0b\u6765\u5c31\u53ef\u4ee5\u4e0d\u65ad\u5730\u4f7f\u7528\u5b83\u4e86\u3002 \u5c3d\u7ba1\u6211\u4eec\u7684 IntArray \u5bb9\u5668\u4e2d\u5b58\u653e\u7684\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b ( int )\uff0c\u6211\u4eec\u5176\u5b9e\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7528\u6237\u5b9a\u4e49\u7c7b\u578b(\u4f8b\u5982 Point \u7c7b). \u8fd8\u6709\u4e00\u4ef6\u4e8b\uff1a\u5982\u679c\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u7c7b\u53ef\u4ee5\u6ee1\u8db3\u4f60\u7684\u9700\u6c42\uff0c\u90a3\u4f60\u5e94\u8be5\u76f4\u63a5\u4f7f\u7528\u5b83\u800c\u4e0d\u662f\u521b\u5efa\u81ea\u5df1\u7684\u7c7b\u3002\u4f8b\u5982\uff0c\u4e0e\u5176\u4f7f\u7528 IntArray \uff0c\u4e0d\u5982\u4f7f\u7528 std::vector \u3002\u5b83\u662f\u7ecf\u8fc7\u68c0\u9a8c\u7684\uff0c\u6548\u7387\u5f88\u9ad8\uff0c\u5e76\u4e14\u4e0e\u6807\u51c6\u5e93\u4e2d\u7684\u5176\u4ed6\u7c7b\u53ef\u4ee5\u5f88\u597d\u5730\u914d\u5408\u3002\u4f46\u6709\u65f6\u4f60\u9700\u8981\u6807\u51c6\u5e93\u4e2d\u4e0d\u5b58\u5728\u7684\u4e13\u7528\u5bb9\u5668\u7c7b\uff0c\u56e0\u6b64\u6700\u597d\u77e5\u9053\u5728\u9700\u8981\u65f6\u5982\u4f55\u521b\u5efa\u81ea\u5df1\u7684\u5bb9\u5668\u7c7b\u3002\u5728\u4ecb\u7ecd\u4e86\u4e00\u4e9b\u66f4\u57fa\u672c\u7684\u4e3b\u9898\u4e4b\u540e\uff0c\u6211\u4eec\u5c06\u66f4\u591a\u5730\u8ba8\u8bba\u6807\u51c6\u5e93\u4e2d\u7684\u5bb9\u5668\u3002","title":"16.6 - \u5bb9\u5668\u7c7b"},{"location":"16-6-container-classes/#_1","text":"\u5bb9\u5668\u7c7b\u901a\u5e38\u6709\u4e24\u4e2a\u53d8\u79cd\u3002 \u503c\u5bb9\u5668 \u662f \u7ec4\u5408\u5173\u7cfb \uff0c\u5b83\u5b58\u653e\u4e86\u5176\u5143\u7d20\u7684\u62f7\u8d1d\uff08\u56e0\u6b64\u5fc5\u987b\u8d1f\u8d23\u521b\u5efa\u548c\u9500\u6bc1\u8fd9\u4e9b\u62f7\u8d1d\uff09\u3002 \u5f15\u7528\u5bb9\u5668 \u5219\u662f \u805a\u5408\u5173\u7cfb \uff0c\u5b83\u4fdd\u5b58\u4e86\u5143\u7d20\u7684\u6307\u9488\u6216\u5f15\u7528\uff08\u56e0\u6b64\u65e0\u9700\u64cd\u5fc3\u5b83\u4eec\u7684\u521b\u5efa\u548c\u9500\u6bc1\uff09\u3002","title":"\u5bb9\u5668\u7c7b\u578b"},{"location":"16-6-container-classes/#_2","text":"\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ece\u5934\u7f16\u5199\u4e00\u4e2a\u6574\u578b\u6570\u7ec4\u7c7b\u5e76\u5b9e\u73b0\u5b83\u5e94\u8be5\u63d0\u4f9b\u7684\u5927\u90e8\u5206\u529f\u80fd\u3002\u8be5\u6570\u7ec4\u7c7b\u662f\u4e00\u4e2a \u503c\u5bb9\u5668 \uff0c\u5b83\u4fdd\u5b58\u5143\u7d20\u7684\u62f7\u8d1d\u3002\u548c\u5b83\u4eec\u7684\u540d\u5b57\u6697\u793a\u7684\u90a3\u6837\uff0c\u8fd9\u4e2a\u5bb9\u5668\u4f1a\u4fdd\u5b58\u4e00\u4e2a\u6574\u578b\u6570\u6784\u6210\u7684\u6570\u7ec4\uff0c\u7c7b\u4f3c std::vector \u3002 \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u521b\u5efa IntArray.h \u6587\u4ef6\uff1a 1 2 3 4 5 6 7 8 #ifndef INTARRAY_H #define INTARRAY_H class IntArray { }; #endif IntArray \u9700\u8981\u8ffd\u8e2a\u4e24\u4e2a\u503c\uff1a\u6570\u636e\u548c\u6570\u7ec4\u5927\u5c0f\u3002\u56e0\u4e3a\u6211\u4eec\u5e0c\u671b\u6570\u7ec4\u80fd\u591f\u6539\u53d8\u5176\u5927\u5c0f\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u52a8\u6001\u5206\u914d\u5185\u5b58\uff0c\u56e0\u6b64\u5fc5\u987b\u4f7f\u7528\u6307\u9488\u6765\u4fdd\u5b58\u6570\u636e\u3002 1 2 3 4 5 6 7 8 9 10 11 #ifndef INTARRAY_H #define INTARRAY_H class IntArray { private : int m_length {}; int * m_data {}; }; #endif \u63a5\u4e0b\u6765\uff0c\u6dfb\u52a0\u7528\u4e8e\u521b\u5efa IntArrays \u7684\u6784\u9020\u51fd\u6570\u3002\u8fd9\u91cc\u6211\u4eec\u9700\u8981\u4e24\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u4e00\u4e2a\u521b\u5efa\u7a7a\u7684\u6570\u7ec4\uff0c\u4e00\u4e2a\u5219\u521b\u5efa\u9884\u5b9a\u4e49\u957f\u5ea6\u7684\u6570\u7ec4\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #ifndef INTARRAY_H #define INTARRAY_H #include // for assert() class IntArray { private : int m_length {}; int * m_data {}; public : IntArray () = default ; IntArray ( int length ) : m_length { length } { assert ( length >= 0 ); if ( length > 0 ) m_data = new int [ length ]{}; } }; #endif \u6211\u4eec\u8fd8\u9700\u8981\u521b\u5efa\u7528\u4e8e\u6e05\u7406\u6570\u7ec4\u7684\u51fd\u6570\u3002\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a \u6790\u6784\u51fd\u6570 \uff0c\u5b83\u7684\u529f\u80fd\u5c31\u662f\u91ca\u653e\u6570\u636e\u5360\u7528\u7684\u5185\u5b58\u3002\u7136\u540e\uff0c\u518d\u7f16\u5199\u4e00\u4e2a erase() \u51fd\u6570\u5c06\u6570\u7ec4\u7684\u5143\u7d20\u6e05\u9664\u5e76\u5c06\u957f\u5ea6\u8bbe\u7f6e\u4e3a0\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ~ IntArray () { delete [] m_data ; // \u8fd9\u91cc\u4e0d\u5fc5\u5c06 m_data \u8bbe\u7f6e\u4e3a null \u4e5f\u4e0d\u7528\u5c06 m_length \u8bbe\u7f6e\u4e3a0\uff0c\u56e0\u4e3a\u5bf9\u8c61\u9a6c\u4e0a\u5c31\u4f1a\u88ab\u9500\u6bc1 } void erase () { delete [] m_data ; // \u5fc5\u987b\u786e\u4fdd m_data \u662f nullptr \u5426\u5219\u5c06\u4f1a\u79f0\u4e3a\u60ac\u5782\u6307\u9488 m_data = nullptr ; m_length = 0 ; } \u73b0\u5728\uff0c \u91cd\u8f7d [] \u64cd\u4f5c\u7b26\u7528\u4e8e\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u5728\u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26\u7684\u65f6\u5019\uff0c\u5fc5\u987b\u786e\u4fdd\u7d22\u5f15\u662f\u5408\u6cd5\u7684\uff0c\u8be5\u64cd\u4f5c\u53ef\u4ee5\u4f7f\u7528 assert() \u51fd\u6570\u3002\u6211\u4eec\u8fd8\u9700\u8981\u6dfb\u52a0\u4e00\u4e2a\u51fd\u6570\u7528\u4e8e\u8fd4\u56de\u6570\u7ec4\u957f\u5ea6\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u7c7b\u770b\u4e0a\u53bb\u4f1a\u5411\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #ifndef INTARRAY_H #define INTARRAY_H #include // for assert() class IntArray { private : int m_length {}; int * m_data {}; public : IntArray () = default ; IntArray ( int length ) : m_length { length } { assert ( length >= 0 ); if ( length > 0 ) m_data = new int [ length ]{}; } ~ IntArray () { delete [] m_data ; // \u8fd9\u91cc\u4e0d\u5fc5\u5c06 m_data \u8bbe\u7f6e\u4e3a null \u4e5f\u4e0d\u7528\u5c06 m_length \u8bbe\u7f6e\u4e3a0\uff0c\u56e0\u4e3a\u5bf9\u8c61\u9a6c\u4e0a\u5c31\u4f1a\u88ab\u9500\u6bc1 } void erase () { delete [] m_data ; // \u5fc5\u987b\u786e\u4fdd m_data \u662f nullptr \u5426\u5219\u5c06\u4f1a\u79f0\u4e3a\u60ac\u5782\u6307\u9488 m_data = nullptr ; m_length = 0 ; } int & operator []( int index ) { assert ( index >= 0 && index < m_length ); return m_data [ index ]; } int getLength () const { return m_length ; } }; #endif \u81f3\u6b64\uff0c IntArray \u7c7b\u5df2\u7ecf\u5b9e\u73b0\u5b8c\u6210\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e86\u3002\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6709\u9884\u5b9a\u4e49\u957f\u5ea6\u7684 IntArrays \uff0c\u7136\u540e\u4f7f\u7528\u4e0b\u6807\u8fd0\u7b97\u7b26\u83b7\u53d6\u5143\u7d20\u7684\u503c\u3002 \u4f46\u662f\uff0c IntArray \u4ecd\u7136\u8fd8\u6709\u4e9b\u529f\u80fd\u6ca1\u6709\u88ab\u5b9e\u73b0\u3002\u6570\u7ec4\u5927\u5c0f\u6682\u65f6\u8fd8\u4e0d\u80fd\u6539\u53d8\u3001\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4e5f\u8fd8\u4e0d\u80fd\u88ab\u63d2\u5165\u6216\u5220\u9664\u3001\u4e0d\u4ec5\u5982\u6b64\uff0c\u6570\u7ec4\u5143\u7d20\u4e5f\u8fd8\u4e0d\u80fd\u88ab\u6392\u5e8f\u3002 \u9996\u5148\uff0c\u7f16\u5199\u4ee3\u7801\u4f7f\u5f97\u6570\u7ec4\u53ef\u4ee5\u6539\u53d8\u5176\u5c3a\u5bf8\u3002\u800c\u4e14\uff0c\u6211\u4eec\u4f1a\u5b9e\u73b0\u4e24\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u7b2c\u4e00\u4e2a\u51fd\u6570\u662f reallocate() \uff0c\u8fd9\u4e2a\u51fd\u6570\u5728\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\u7684\u65f6\u5019\uff0c\u4f1a\u9500\u6bc1\u6570\u7ec4\u4e2d\u7684\u5168\u90e8\u5143\u7d20\uff0c\u4f46\u662f\u5b83\u7684\u901f\u5ea6\u5f88\u5feb\u3002\u7b2c\u4e8c\u4e2a\u51fd\u6570\u662f resize() \uff0c\u5b83\u5728\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u4fdd\u6301\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u4f46\u662f\u8be5\u51fd\u6570\u7684\u6548\u7387\u4f1a\u4f4e\u4e00\u4e9b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 // reallocate \u4f1a\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\uff0c\u4f46\u662f\u5b83\u4f1a\u5728\u5de5\u4f5c\u65f6\u9500\u6bc1\u6240\u6709\u7684\u6570\u7ec4\u5143\u7d20\u3002\u8be5\u51fd\u6570\u7684\u901f\u5ea6\u5f88\u5feb void reallocate ( int newLength ) { // \u9996\u5148\uff0c\u5220\u9664\u5168\u90e8\u7684\u5143\u7d20 erase (); // \u5982\u679c\u8981\u521b\u5efa\u7a7a\u6570\u7ec4\uff0c\u6b64\u5904\u8fd4\u56de\u5373\u53ef if ( newLength <= 0 ) return ; // \u5206\u914d\u65b0\u7684\u5143\u7d20 m_data = new int [ newLength ]; m_length = newLength ; } // resize \u4f1a\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\uff0c\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4f1a\u88ab\u4fdd\u5b58\u4e0b\u6765\u3002\u8be5\u51fd\u6570\u7684\u901f\u5ea6\u6bd4\u8f83\u6162 void resize ( int newLength ) { // \u5982\u679c\u6570\u7ec4\u7684\u957f\u5ea6\u5df2\u7ecf\u6ee1\u8db3\u8981\u6c42\uff0c\u5b8c\u6210 if ( newLength == m_length ) return ; // \u5982\u679c\u9700\u8981\u5c06\u6570\u7ec4\u8c03\u6574\u4e3a\u4e00\u4e2a\u7a7a\u6570\u7ec4\uff0c\u5b8c\u6210\u5e76\u8fd4\u56de if ( newLength <= 0 ) { erase (); return ; } // \u5047\u8bbe newLength \u5927\u4e8e\u7b49\u4e8e1\u3002\u8be5\u7b97\u6cd5\u7684\u539f\u7406\u5982\u4e0b\uff1a // \u9996\u5148\uff0c\u5206\u914d\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\u3002\u7136\u540e\u5c06\u65e7\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u90fd\u62f7\u8d1d\u5230\u65b0\u7684\u6570\u7ec4\u4e2d\u3002\u5b8c\u6210\u540e\uff0c\u9500\u6bc1\u65e7\u6570\u7ec4\u3002 // \u7136\u540e\u8ba9 m_data \u6307\u5411\u65b0\u7684\u6570\u7ec4 // \u5206\u914d\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4 int * data { new int [ newLength ] }; // \u786e\u5b9a\u6709\u591a\u5c11\u4e2a\u5143\u7d20\u9700\u8981\u4ece\u65e7\u6570\u7ec4\u4e2d\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\u3002 // \u8fd9\u91cc\u6211\u4eec\u5e0c\u671b\u4ece\u65e7\u6570\u7ec4\u4e2d\u62f7\u8d1d\u5c3d\u53ef\u80fd\u591a\u7684\u5143\u7d20\uff08\u65b0\u6570\u7ec4\u4e0d\u4e00\u5b9a\u6bd4\u65e7\u6570\u7ec4\u5927\uff09 // \u62f7\u8d1d\u7684\u5143\u7d20\u4e2a\u6570\u662f\u65b0\u65e7\u4e24\u4e2a\u6570\u7ec4\u4e2d\u8f83\u5c0f\u7684\u4e00\u4e2a if ( m_length > 0 ) { int elementsToCopy { ( newLength > m_length ) ? m_length : newLength }; // Now copy the elements one by one for ( int index { 0 }; index < elementsToCopy ; ++ index ) data [ index ] = m_data [ index ]; } // \u5220\u9664\u65e7\u6570\u7ec4 delete [] m_data ; // \u5f00\u59cb\u4f7f\u7528\u65b0\u7684\u6570\u7ec4\u4f5c\u4e3a\u6570\u636e\uff01\u6700\u7b80\u5355\u7684\u529e\u6cd5\u5c31\u662f\u8ba9 m_data\u6307\u5411\u65b0\u6570\u7ec4\u7684\u5730\u5740\u3002 // \u7531\u4e8e\u8be5\u6570\u7ec4\u662f\u52a8\u6001\u5206\u914d\u7684\uff0c\u6240\u4ee5\u4e0d\u4f1a\u5728\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\u88ab\u9500\u6bc1 m_data = data ; m_length = newLength ; } \u8fd8\u633a\u590d\u6742\uff01 \u591a\u6570\u7684\u6570\u7ec4\u5bb9\u5668\u7c7b\u4f1a\u6b62\u6b65\u4e8e\u6b64\u3002\u4e0d\u8fc7\uff0c\u4e5f\u8bb8\u4f60\u60f3\u77e5\u9053\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u5e94\u8be5\u5982\u4f55\u5b9e\u73b0\uff0c\u6240\u4ee5\u6211\u4eec\u7ee7\u7eed\u5b9e\u73b0\u8fd9\u4e24\u4e2a\u529f\u80fd\u3002\u5176\u5b9e\u73b0\u65b9\u6cd5\u548c resize() \u975e\u5e38\u7c7b\u4f3c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 void insertBefore ( int value , int index ) { // \u68c0\u67e5\u6570\u7ec4\u7d22\u5f15\u5408\u6cd5\u6027 assert ( index >= 0 && index <= m_length ); // \u9996\u5148\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u5176\u957f\u5ea6\u6bd4\u65e7\u6570\u7ec4\u59271 int * data { new int [ m_length + 1 ] }; // \u62f7\u8d1d\u5230\u5f53\u524d\u7d22\u5f15\u7684\u6240\u6709\u5143\u7d20 for ( int before { 0 }; before < index ; ++ before ) data [ before ] = m_data [ before ]; // \u63d2\u5165\u65b0\u7684\u5143\u7d20\u5230\u65b0\u6570\u7ec4 data [ index ] = value ; // \u7ee7\u7eed\u62f7\u8d1d\u5269\u4f59\u7684\u5143\u7d20 for ( int after { index }; after < m_length ; ++ after ) data [ after + 1 ] = m_data [ after ]; // \u6700\u540e\uff0c\u5220\u9664\u65e7\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528\u65b0\u7684\u6570\u7ec4\u4f5c\u4e3a\u6570\u636e delete [] m_data ; m_data = data ; ++ m_length ; } void remove ( int index ) { // \u68c0\u67e5\u6570\u7ec4\u7d22\u5f15\u5408\u6cd5\u6027 assert ( index >= 0 && index < m_length ); // \u5982\u679c\u8fd9\u5df2\u7ecf\u662f\u6700\u540e\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20\uff0c\u5c06\u6570\u7ec4\u8bbe\u7f6e\u4e3a\u7a7a\u6570\u7ec4\u7136\u540e\u5b8c\u6210 if ( m_length == 1 ) { erase (); return ; } // \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5176\u5927\u5c0f\u6bd4\u65e7\u6570\u7ec4\u5c0f1 int * data { new int [ m_length -1 ] }; // \u62f7\u8d1d\u5230\u7d22\u5f15\u7684\u5168\u90e8\u5143\u7d20 for ( int before { 0 }; before < index ; ++ before ) data [ before ] = m_data [ before ]; // \u7ee7\u7eed\u62f7\u8d1d\u88ab\u5220\u9664\u5143\u7d20\u540e\u9762\u7684\u6240\u6709\u5143\u7d20 for ( int after { index + 1 }; after < m_length ; ++ after ) data [ after -1 ] = m_data [ after ]; // \u6700\u540e\uff0c\u5220\u9664\u65e7\u6570\u7ec4\u5e76\u4f7f\u7528\u65b0\u7684\u6570\u7ec4\u4f5c\u4e3a\u6570\u636e delete [] m_data ; m_data = data ; -- m_length ; } // A couple of additional functions just for convenience void insertAtBeginning ( int value ) { insertBefore ( value , 0 ); } void insertAtEnd ( int value ) { insertBefore ( value , m_length ); } IntArray \u5bb9\u5668\u7c7b\u7684\u5b8c\u6574\u5b9a\u4e49\u5982\u4e0b\uff1a IntArray.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 #ifndef INTARRAY_H #define INTARRAY_H #include // for assert() class IntArray { private : int m_length {}; int * m_data {}; public : IntArray () = default ; IntArray ( int length ) : m_length { length } { assert ( length >= 0 ); if ( length > 0 ) m_data = new int [ length ]{}; } ~ IntArray () { delete [] m_data ; // we don't need to set m_data to null or m_length to 0 here, since the object will be destroyed immediately after this function anyway } void erase () { delete [] m_data ; // We need to make sure we set m_data to nullptr here, otherwise it will // be left pointing at deallocated memory! m_data = nullptr ; m_length = 0 ; } int & operator []( int index ) { assert ( index >= 0 && index < m_length ); return m_data [ index ]; } // reallocate resizes the array. Any existing elements will be destroyed. This function operates quickly. void reallocate ( int newLength ) { // First we delete any existing elements erase (); // If our array is going to be empty now, return here if ( newLength <= 0 ) return ; // Then we have to allocate new elements m_data = new int [ newLength ]; m_length = newLength ; } // resize resizes the array. Any existing elements will be kept. This function operates slowly. void resize ( int newLength ) { // if the array is already the right length, we're done if ( newLength == m_length ) return ; // If we are resizing to an empty array, do that and return if ( newLength <= 0 ) { erase (); return ; } // Now we can assume newLength is at least 1 element. This algorithm // works as follows: First we are going to allocate a new array. Then we // are going to copy elements from the existing array to the new array. // Once that is done, we can destroy the old array, and make m_data // point to the new array. // First we have to allocate a new array int * data { new int [ newLength ] }; // Then we have to figure out how many elements to copy from the existing // array to the new array. We want to copy as many elements as there are // in the smaller of the two arrays. if ( m_length > 0 ) { int elementsToCopy { ( newLength > m_length ) ? m_length : newLength }; // Now copy the elements one by one for ( int index { 0 }; index < elementsToCopy ; ++ index ) data [ index ] = m_data [ index ]; } // Now we can delete the old array because we don't need it any more delete [] m_data ; // And use the new array instead! Note that this simply makes m_data point // to the same address as the new array we dynamically allocated. Because // data was dynamically allocated, it won't be destroyed when it goes out of scope. m_data = data ; m_length = newLength ; } void insertBefore ( int value , int index ) { // Sanity check our index value assert ( index >= 0 && index <= m_length ); // First create a new array one element larger than the old array int * data { new int [ m_length + 1 ] }; // Copy all of the elements up to the index for ( int before { 0 }; before < index ; ++ before ) data [ before ] = m_data [ before ]; // Insert our new element into the new array data [ index ] = value ; // Copy all of the values after the inserted element for ( int after { index }; after < m_length ; ++ after ) data [ after + 1 ] = m_data [ after ]; // Finally, delete the old array, and use the new array instead delete [] m_data ; m_data = data ; ++ m_length ; } void remove ( int index ) { // Sanity check our index value assert ( index >= 0 && index < m_length ); // If we're removing the last element in the array, we can just erase the array and return early if ( m_length == 1 ) { erase (); return ; } // First create a new array one element smaller than the old array int * data { new int [ m_length -1 ] }; // Copy all of the elements up to the index for ( int before { 0 }; before < index ; ++ before ) data [ before ] = m_data [ before ]; // Copy all of the values after the removed element for ( int after { index + 1 }; after < m_length ; ++ after ) data [ after -1 ] = m_data [ after ]; // Finally, delete the old array, and use the new array instead delete [] m_data ; m_data = data ; -- m_length ; } // A couple of additional functions just for convenience void insertAtBeginning ( int value ) { insertBefore ( value , 0 ); } void insertAtEnd ( int value ) { insertBefore ( value , m_length ); } int getLength () const { return m_length ; } }; #endif \u8bd5\u8bd5\u770b\uff0c\u80fd\u4e0d\u80fd\u6b63\u786e\u5de5\u4f5c\uff01 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include #include \"IntArray.h\" int main () { // Declare an array with 10 elements IntArray array ( 10 ); // Fill the array with numbers 1 through 10 for ( int i { 0 }; i < 10 ; ++ i ) array [ i ] = i + 1 ; // Resize the array to 8 elements array . resize ( 8 ); // Insert the number 20 before element with index 5 array . insertBefore ( 20 , 5 ); // Remove the element with index 3 array . remove ( 3 ); // Add 30 and 40 to the end and beginning array . insertAtEnd ( 30 ); array . insertAtBeginning ( 40 ); // Print out all the numbers for ( int i { 0 }; i < array . getLength (); ++ i ) std :: cout << array [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 40 1 2 3 5 20 6 7 8 30 \u867d\u7136\u7f16\u5199\u4e00\u4e2a\u5bb9\u5668\u7c7b\u662f\u975e\u5e38\u590d\u6742\u7684\u4e8b\u60c5\uff0c\u597d\u6d88\u606f\u662f\u4f60\u53ea\u9700\u8981\u7f16\u5199\u4e00\u6b21\u5373\u53ef\u3002\u4e00\u65e6\u67d0\u4e2a\u5bb9\u5668\u7c7b\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\u3002\u90a3\u4e48\u4f60\u63a5\u4e0b\u6765\u5c31\u53ef\u4ee5\u4e0d\u65ad\u5730\u4f7f\u7528\u5b83\u4e86\u3002 \u5c3d\u7ba1\u6211\u4eec\u7684 IntArray \u5bb9\u5668\u4e2d\u5b58\u653e\u7684\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b ( int )\uff0c\u6211\u4eec\u5176\u5b9e\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7528\u6237\u5b9a\u4e49\u7c7b\u578b(\u4f8b\u5982 Point \u7c7b). \u8fd8\u6709\u4e00\u4ef6\u4e8b\uff1a\u5982\u679c\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u7c7b\u53ef\u4ee5\u6ee1\u8db3\u4f60\u7684\u9700\u6c42\uff0c\u90a3\u4f60\u5e94\u8be5\u76f4\u63a5\u4f7f\u7528\u5b83\u800c\u4e0d\u662f\u521b\u5efa\u81ea\u5df1\u7684\u7c7b\u3002\u4f8b\u5982\uff0c\u4e0e\u5176\u4f7f\u7528 IntArray \uff0c\u4e0d\u5982\u4f7f\u7528 std::vector \u3002\u5b83\u662f\u7ecf\u8fc7\u68c0\u9a8c\u7684\uff0c\u6548\u7387\u5f88\u9ad8\uff0c\u5e76\u4e14\u4e0e\u6807\u51c6\u5e93\u4e2d\u7684\u5176\u4ed6\u7c7b\u53ef\u4ee5\u5f88\u597d\u5730\u914d\u5408\u3002\u4f46\u6709\u65f6\u4f60\u9700\u8981\u6807\u51c6\u5e93\u4e2d\u4e0d\u5b58\u5728\u7684\u4e13\u7528\u5bb9\u5668\u7c7b\uff0c\u56e0\u6b64\u6700\u597d\u77e5\u9053\u5728\u9700\u8981\u65f6\u5982\u4f55\u521b\u5efa\u81ea\u5df1\u7684\u5bb9\u5668\u7c7b\u3002\u5728\u4ecb\u7ecd\u4e86\u4e00\u4e9b\u66f4\u57fa\u672c\u7684\u4e3b\u9898\u4e4b\u540e\uff0c\u6211\u4eec\u5c06\u66f4\u591a\u5730\u8ba8\u8bba\u6807\u51c6\u5e93\u4e2d\u7684\u5bb9\u5668\u3002","title":"\u6570\u7ec4\u5bb9\u5668\u7c7b"},{"location":"16-7-std-initializer_list/","tags":["object","initializer_list"],"text":"Key Takeaway - \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u6574\u578b\u6570\u7ec4\uff1a 1 int array [ 5 ]; \u5982\u679c\u6211\u4eec\u60f3\u7528\u503c\u521d\u59cb\u5316\u8fd9\u4e2a\u6570\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7 \u521d\u59cb\u5316\u503c\u5217\u8868 \u8bed\u6cd5\u6765\u5b9e\u73b0: 1 2 3 4 5 6 7 8 9 10 #include int main () { int array [] { 5 , 4 , 3 , 2 , 1 }; // \u521d\u59cb\u5316\u503c\u5217\u8868 for ( auto i : array ) std :: cout << i << ' ' ; return 0 ; } \u6253\u5370\uff1a 1 5 4 3 2 1 \u5bf9\u4e8e\u52a8\u6001\u6570\u7ec4\u540c\u6837\u9002\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { auto * array { new int [ 5 ]{ 5 , 4 , 3 , 2 , 1 } }; // \u521d\u59cb\u5316\u503c\u5217\u8868 for ( int count { 0 }; count < 5 ; ++ count ) std :: cout << array [ count ] << ' ' ; delete [] array ; return 0 ; } \u5728\u4e0a\u8282\u8bfe\u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86 \u5bb9\u5668\u7c7b \u7684\u6982\u5ff5\uff0c\u5e76\u4e14\u4ee5 IntArray \u7c7b\u4e3a\u4f8b\u4ecb\u7ecd\u4e86\u6574\u578b\u6570\u7ec4\u5bb9\u5668\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #include // for assert() #include class IntArray { private : int m_length {}; int * m_data {}; public : IntArray () = default ; IntArray ( int length ) : m_length { length } , m_data { new int [ length ]{} } { } ~ IntArray () { delete [] m_data ; // we don't need to set m_data to null or m_length to 0 here, since the object will be destroyed immediately after this function anyway } int & operator []( int index ) { assert ( index >= 0 && index < m_length ); return m_data [ index ]; } int getLength () const { return m_length ; } }; int main () { // \u5982\u679c\u6211\u4eec\u4f7f\u7528\u521d\u59cb\u5316\u503c\u5217\u8868\u5bf9IntArray\u8fdb\u884c\u521d\u59cb\u5316\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f IntArray array { 5 , 4 , 3 , 2 , 1 }; // \u65e0\u6cd5\u7f16\u8bd1 for ( int count { 0 }; count < 5 ; ++ count ) std :: cout << array [ count ] << ' ' ; return 0 ; } \u4e0a\u8ff0\u4ee3\u7801\u5e76\u4e0d\u80fd\u6210\u529f\u7f16\u8bd1\uff0c\u56e0\u4e3a IntArray \u7c7b\u5e76\u4e0d\u77e5\u9053\u5728\u9762\u5bf9\u4e00\u4e2a \u521d\u59cb\u5316\u503c\u5217\u8868 \u65f6\u5e94\u8be5\u505a\u4ec0\u4e48\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5fc5\u987b\u5bf9\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u9010\u4e00\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int main () { IntArray array ( 5 ); array [ 0 ] = 5 ; array [ 1 ] = 4 ; array [ 2 ] = 3 ; array [ 3 ] = 2 ; array [ 4 ] = 1 ; for ( int count { 0 }; count < 5 ; ++ count ) std :: cout << array [ count ] << ' ' ; return 0 ; } \u5f88\u4e0d\u4f18\u96c5\u3002 \u4f7f\u7528 std::initializer_list \u521d\u59cb\u5316 \u5f53\u7f16\u8bd1\u5668\u770b\u5230\u521d\u59cb\u5316\u503c\u5217\u8868\u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u5730\u5c06\u521d\u59cb\u5316\u503c\u5217\u8868\u8f6c\u6362\u4e3a\u4e00\u4e2a std::initializer_list \u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u63a5\u6536 std::initializer_list \u7c7b\u578b\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u7684\u8bdd\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7\u521d\u59cb\u5316\u503c\u5217\u8868\u521b\u5efa\u5bf9\u8c61\u4e86\u3002 std::initializer_list \u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d\u3002 \u5173\u4e8e std::initializer_list \uff0c\u6709\u4e9b\u4e8b\u60c5\u4f60\u5fc5\u987b\u6307\u5b9a\u3002\u548c std::array \u6216\u8005 std::vector \u7c7b\u4f3c\uff0c\u4f60\u5fc5\u987b\u544a\u8bc9 std::initializer_list \u5217\u8868\u4e2d\u7684\u5143\u7d20\u7c7b\u578b\u662f\u4ec0\u4e48\uff0c\u9664\u975e\u4f60\u662f\u7acb\u5373\u5bf9\u5176\u521d\u59cb\u5316\u3002\u56e0\u6b64\u4f60\u5f88\u5c11\u4f1a\u770b\u5230 std::initializer_list \u7684\u6700\u7b80\u5f62\u5f0f\u3002\u591a\u6570\u60c5\u51b5\u4e0b\u4f60\u770b\u5230\u7684\u5e94\u8be5\u662f\u8fd9\u6837\u7684 std::initializer_list \u6216\u8fd9\u6837\u7684 std::initializer_list \u3002 \u7b2c\u4e8c\uff0c std::initializer_list \u6709\u4e00\u4e2a size() \u6210\u5458\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u5143\u7d20\u4e2a\u6570\u3002\u5f53\u6211\u4eec\u9700\u8981\u6307\u5b9a\u4f20\u5165\u7684\u521d\u59cb\u5316\u5217\u8868\u591a\u957f\u65f6\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u6709\u7528\u3002 \u4f7f\u7528 std::initializer_list \u5bf9 IntArray \u6539\u5199\u4e4b\u540e\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 #include // for assert() #include // for std::initializer_list #include class IntArray { private : int m_length {}; int * m_data {}; public : IntArray () = default ; IntArray ( int length ) : m_length { length } , m_data { new int [ length ]{} } { } IntArray ( std :: initializer_list < int > list ) // allow IntArray to be initialized via list initialization : IntArray ( static_cast < int > ( list . size ())) // use delegating constructor to set up initial array { // Now initialize our array from the list int count { 0 }; for ( auto element : list ) { m_data [ count ] = element ; ++ count ; } } ~ IntArray () { delete [] m_data ; // we don't need to set m_data to null or m_length to 0 here, since the object will be destroyed immediately after this function anyway } IntArray ( const IntArray & ) = delete ; // to avoid shallow copies IntArray & operator = ( const IntArray & list ) = delete ; // to avoid shallow copies int & operator []( int index ) { assert ( index >= 0 && index < m_length ); return m_data [ index ]; } int getLength () const { return m_length ; } }; int main () { IntArray array { 5 , 4 , 3 , 2 , 1 }; // initializer list for ( int count { 0 }; count < array . getLength (); ++ count ) std :: cout << array [ count ] << ' ' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u7b26\u5408\u9884\u671f\uff1a 1 5 4 3 2 1 \u5f88\u597d\uff01\u63a5\u4e0b\u6765\u8ba9\u6211\u4eec\u6df1\u5165\u7814\u7a76\u4e00\u4e0b\uff1a IntArray \u7684\u6784\u9020\u51fd\u6570\u63a5\u53d7 std::initializer_list \u4f5c\u4e3a \u5f62\u53c2 \u3002 1 2 3 4 5 6 7 8 9 10 11 IntArray ( std :: initializer_list < int > list ) // \u4f7f\u5f97 IntArray \u53ef\u4ee5\u8fdb\u884c\u5217\u8868\u521d\u59cb\u5316 : IntArray ( static_cast < int > ( list . size ())) // \u4f7f\u7528\u59d4\u6d3e\u6784\u9020\u51fd\u6570\u8bbe\u7f6e\u6570\u7ec4 { // \u4f7f\u7528\u5217\u8868\u5143\u7d20\u521d\u59cb\u5316\u6570\u7ec4 int count { 0 }; for ( int element : list ) { m_data [ count ] = element ; ++ count ; } } \u7b2c\u4e00\u884c\uff1a\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u5c16\u62ec\u53f7\u8868\u660e\u5217\u8868\u4e2d\u5143\u7d20\u7684\u7c7b\u578b\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u8fd9\u662f\u4e00\u4e2a IntArray \uff0c\u6240\u4ee5\u521d\u59cb\u5316\u503c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u81ea\u7136\u9700\u8981\u662f int \u3002\u6ce8\u610f\uff0c\u6211\u4eec\u5e76\u6ca1\u6709 \u6309const\u5f15\u7528\u4f20\u9012 \u3002\u548c std::string_view \u7c7b\u4f3c\uff0c std::initializer_list \u662f\u975e\u5e38\u8f7b\u91cf\u7ea7\u7684\uff0c\u6240\u4ee5\u62f7\u8d1d\u6bd4\u95f4\u63a5\u8bbf\u95ee\u5f00\u9500\u8fd8\u5c0f\u3002 \u7b2c\u4e8c\u884c\uff1a\u6211\u4eec\u5fc5\u987b\u5c06\u5185\u5b58\u5206\u914d\u7684\u4efb\u52a1\u59d4\u6d3e\u7ed9\u5176\u4ed6\u6784\u9020\u51fd\u6570\uff08\u901a\u8fc7\u59d4\u6d3e\u6784\u9020\u51cf\u5c11\u5197\u4f59\u4ee3\u7801\uff09\u3002\u8be5\u59d4\u6d3e\u6784\u9020\u51fd\u6570\u9700\u8981\u6307\u5b9a\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u6240\u4ee5\u6211\u4eec\u901a\u8fc7 list.size() \u83b7\u53d6\u957f\u5ea6\u5e76\u5411\u5b83\u4f20\u9012\u8be5\u4fe1\u606f\u3002\u6ce8\u610f list.size() \u8fd4\u56de\u7684\u662f size_t (\u65e0\u7b26\u53f7)\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u5c06\u5176\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u6574\u578b\u3002\u6211\u4eec\u4f7f\u7528 \u76f4\u63a5\u521d\u59cb\u5316 \u800c\u4e0d\u662f \u62ec\u53f7\u521d\u59cb\u5316 \u56e0\u4e3a\u62ec\u53f7\u521d\u59cb\u5316\u9700\u8981 \u5217\u8868\u6784\u9020\u51fd\u6570 \uff08list constructors\uff09\u3002\u5c3d\u7ba1\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u88ab\u6b63\u786e\u89e3\u6790\uff0c\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\u7684\u65b9\u5f0f\u521d\u59cb\u5316\u5177\u6709\u5217\u8868\u6784\u9020\u51fd\u6570\u7684\u7c7b\u662f\u66f4\u5b89\u5168\u7684\uff08\u5982\u679c\u4f60\u672c\u610f\u4e0d\u662f\u4f7f\u7528\u5217\u8868\u6784\u9020\u51fd\u6570\u7684\u8bdd\uff09\u3002 \u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u4e2d\u7684\u4ee3\u7801\u7528\u4e8e\u5c06\u521d\u59cb\u5316\u5217\u8868\u4e2d\u7684\u503c\u62f7\u8d1d\u5230 IntArray \u4e2d\u3002\u4ee4\u4eba\u8d39\u89e3\u7684\u662f\uff0c std::initializer_list \u5e76\u6ca1\u6709\u63d0\u4f9b\u4e0b\u6807\u8fd0\u7b97\u7b26\u7528\u4e8e\u8bbf\u95ee\u5143\u7d20\u3002\u8fd9\u4e2a\u95ee\u9898\u5728C++\u6807\u51c6\u59d4\u5458\u4f1a\u4e0a\u88ab\u63d0\u53ca\u5f88\u591a\u6b21\u4f46\u6bcf\u4e00\u6b21\u90fd\u4e0d\u4e86\u4e86\u4e4b\u4e86\u3002 \u4e0d\u8fc7\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u65b9\u6cd5\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u6700\u7b80\u5355\u7684\u662f\u4f7f\u7528 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \uff0c\u5b83\u53ef\u4ee5\u4e00\u6b21\u904d\u5386\u521d\u59cb\u5316\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u7136\u540e\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u5143\u7d20\u62f7\u8d1d\u5230\u6211\u4eec\u7684\u5185\u90e8\u6570\u7ec4\u4e2d\u3002 \u6ce8\u610f\uff1a\u521d\u59cb\u5316\u5668\u5217\u8868\u603b\u662f\u503e\u5411\u4e8e\u8c03\u7528\u5339\u914d\u7684 initializer_list \u6784\u9020\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u5176\u4ed6\u53ef\u80fd\u5339\u914d\u7684\u6784\u9020\u51fd\u6570\u3002\u56e0\u6b64\uff0c\u8fd9\u4e2a\u53d8\u91cf\u5b9a\u4e49: 1 IntArray array { 5 }; \u4f1a\u5339\u914d\u5230 IntArray(std::initializer_list) \u800c\u4e0d\u662f IntArray(int) \u3002\u5982\u679c\u4f60\u5e0c\u671b\u5728 \u5217\u8868\u6784\u9020\u51fd\u6570 \u5b58\u5728\u7684\u60c5\u51b5\u4e0b\u5339\u914d IntArray(int) \uff0c\u90a3\u4e48\u4f60\u9700\u8981\u4f7f\u7528 \u62f7\u8d1d\u521d\u59cb\u5316 \u6216\u8005 \u76f4\u63a5\u521d\u59cb\u5316 \u3002\u5bf9\u4e8e std::vector \u548c\u5176\u4ed6\u540c\u65f6\u5177\u6709\u5217\u8868\u6784\u9020\u51fd\u6570\u548c\u7c7b\u4f3c\u6784\u9020\u51fd\u6570\u7684 \u5bb9\u5668\u7c7b \u4e5f\u662f\u4e00\u6837\u7684\u3002 1 2 std :: vector < int > array ( 5 ); // Calls std::vector::vector(std::vector::size_type), 5\u4e2a\u5143\u7d20\u88ab\u521d\u59cb\u5316\u4e3a0 std :: vector < int > array { 5 }; // Calls std::vector::vector(std::initializer_list), \u4e00\u4e2a\u5143\u7d20 5 COPY \u4f7f\u7528 std::initializer_list \u5bf9\u7c7b\u8fdb\u884c\u8d4b\u503c \u91cd\u8f7d\u8d4b\u503c\u64cd\u4f5c\u7b26\u63a5\u53d7 std::initializer_list \u5f62\u53c2\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7 std::initializer_list \u6765\u4e3a\u7c7b\u8d4b\u503c\u4e86\u3002\u5de5\u4f5c\u539f\u7406\u4e0e\u4e0a\u9762\u7c7b\u4f3c\u3002\u5177\u4f53\u7684\u5b9e\u4f8b\u89c1quiz\u90e8\u5206\u3002\uff08\u89c1\u539f\u6587\uff09 \u6ce8\u610f\uff0c\u5982\u679c\u4f60\u5b9e\u73b0\u4e86\u4e00\u4e2a\u63a5\u53d7 std::initializer_list \u7684\u6784\u9020\u51fd\u6570\uff0c\u4f60\u5e94\u8be5\u786e\u4fdd\u4f60\u81f3\u5c11\u505a\u4e86\u4ee5\u4e0b\u5176\u4e2d\u4e4b\u4e00: \u63d0\u4f9b\u91cd\u8f7d\u7684 \u5217\u8868\u8d4b\u503c\u8fd0\u7b97\u7b26 \uff1b \u63d0\u4f9b\u6b63\u786e\u7684 \u6df1\u62f7\u8d1d \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \uff1b \u5220\u9664 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u3002 \u4e3a\u4ec0\u4e48\uff1f\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7c7b(\u4e0d\u6ee1\u8db3\u4e0a\u9762\u4e09\u4e2a\u6761\u4ef6)\u5728\u4f7f\u7528\u5217\u8868\u8d4b\u503c\u8bed\u53e5\u65f6\u53ef\u80fd\u51fa\u73b0\u7684\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 #include // for assert() #include // for std::initializer_list #include class IntArray { private : int m_length {}; int * m_data {}; public : IntArray () = default ; IntArray ( int length ) : m_length { length } , m_data { new int [ length ] {} } { } IntArray ( std :: initializer_list < int > list ) // allow IntArray to be initialized via list initialization : IntArray ( static_cast < int > ( list . size ())) // use delegating constructor to set up initial array { // Now initialize our array from the list int count { 0 }; for ( auto element : list ) { m_data [ count ] = element ; ++ count ; } } ~ IntArray () { delete [] m_data ; } // IntArray(const IntArray&) = delete; // to avoid shallow copies // IntArray& operator=(const IntArray& list) = delete; // to avoid shallow copies int & operator []( int index ) { assert ( index >= 0 && index < m_length ); return m_data [ index ]; } int getLength () const { return m_length ; } }; int main () { IntArray array {}; array = { 1 , 3 , 5 , 7 , 9 , 11 }; // Here's our list assignment statement for ( int count { 0 }; count < array . getLength (); ++ count ) std :: cout << array [ count ] << ' ' ; return 0 ; } \u9996\u5148\uff0c\u7f16\u8bd1\u5668\u4f1a\u6ce8\u610f\u5230\u5e26\u6709 std::initializer_list \u7684\u8d4b\u503c\u51fd\u6570\u4e0d\u5b58\u5728\u3002\u63a5\u4e0b\u6765\uff0c\u5b83\u5c06\u67e5\u627e\u5176\u4ed6\u53ef\u4ee5\u4f7f\u7528\u7684\u8d4b\u503c\u51fd\u6570\uff0c\u5e76\u53d1\u73b0 \u9690\u5f0f\u63d0\u4f9b \u7684 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u3002\u4f46\u662f\uff0c\u6b64\u51fd\u6570\u53ea\u5e94\u8be5\u5728\u521d\u59cb\u5316\u5668\u5217\u8868\u80fd\u591f\u88ab\u8f6c\u6362\u4e3a IntArray \u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002\u56e0\u4e3a {1,3,5,7,9,11} \u662f std::initializer_list \uff0c\u7f16\u8bd1\u5668\u5c06\u4f7f\u7528\u5217\u8868\u6784\u9020\u51fd\u6570\u5c06\u521d\u59cb\u5316\u5217\u8868\u8f6c\u6362\u4e3a\u4e34\u65f6\u7684 IntArray \u3002\u7136\u540e\u8c03\u7528\u9690\u5f0f\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u5c06\u4e34\u65f6\u7684 IntArray \u6d45\u62f7\u8d1d\u5230\u6570\u7ec4\u5bf9\u8c61\u4e2d\u3002 \u8fd9\u6837\u4e00\u6765\uff0c\u4e34\u65f6 IntArray \u7684 m_data \u548c array->m_data \u90fd\u6307\u5411\u540c\u4e00\u4e2a\u5730\u5740\uff08\u56e0\u4e3a \u6d45\u62f7\u8d1d \u7684\u539f\u56e0\uff09\u3002\u7ed3\u679c\u4f60\u5e94\u8be5\u4e5f\u80fd\u9884\u6599\u5230\u4e86\u3002 \u5728\u8d4b\u503c\u8bed\u53e5\u7684\u672b\u5c3e\uff0c\u4e34\u65f6\u7684 IntArray \u4f1a\u88ab\u9500\u6bc1\uff0c\u6b64\u65f6\u5b83\u7684\u6790\u6784\u51fd\u6570\u5c31\u4f1a\u5220\u9664 IntArray \u7684 m_data \u3002\u8fd9\u4f7f\u5f97 array->m_data \u79f0\u4e3a\u4e86\u4e00\u4e2a\u60ac\u5782\u6307\u9488\u3002\u5728\u540e\u7eed\u4f7f\u7528 array->m_data \u7684\u65f6\u5019\uff08\u4e0d\u8bba\u51fa\u4e8e\u4ec0\u4e48\u76ee\u7684\uff0c\u5305\u62ec\u6570\u7ec4 \u79bb\u5f00\u4f5c\u7528\u57df \u6216\u8005\u6790\u6784\u51fd\u6570\u5220\u9664 m_data \u65f6\uff09\uff0c\u90fd\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u63d0\u4f9b\u4e86\u5217\u8868\u6784\u9020\u51fd\u6570\uff0c\u8bf7\u540c\u65f6\u63d0\u4f9b\u5217\u8868\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002 \u5c0f\u7ed3 \u5b9e\u73b0\u4e00\u4e2a\u63a5\u53d7 std::initializer_list \u5f62\u53c2\u7684\u6784\u9020\u51fd\u6570\uff0c\u5141\u8bb8\u6211\u4eec\u5bf9\u81ea\u5b9a\u4e49\u7c7b\u4f7f\u7528\u5217\u8868\u521d\u59cb\u5316\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528 std::initializer_list \u6765\u5b9e\u73b0\u5176\u4ed6\u9700\u8981\u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u7684\u51fd\u6570\uff0c\u4f8b\u5982\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002","title":"16.7 - std::initializer_list"},{"location":"16-7-std-initializer_list/#stdinitializer_list","text":"\u5f53\u7f16\u8bd1\u5668\u770b\u5230\u521d\u59cb\u5316\u503c\u5217\u8868\u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u5730\u5c06\u521d\u59cb\u5316\u503c\u5217\u8868\u8f6c\u6362\u4e3a\u4e00\u4e2a std::initializer_list \u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u63a5\u6536 std::initializer_list \u7c7b\u578b\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u7684\u8bdd\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7\u521d\u59cb\u5316\u503c\u5217\u8868\u521b\u5efa\u5bf9\u8c61\u4e86\u3002 std::initializer_list \u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d\u3002 \u5173\u4e8e std::initializer_list \uff0c\u6709\u4e9b\u4e8b\u60c5\u4f60\u5fc5\u987b\u6307\u5b9a\u3002\u548c std::array \u6216\u8005 std::vector \u7c7b\u4f3c\uff0c\u4f60\u5fc5\u987b\u544a\u8bc9 std::initializer_list \u5217\u8868\u4e2d\u7684\u5143\u7d20\u7c7b\u578b\u662f\u4ec0\u4e48\uff0c\u9664\u975e\u4f60\u662f\u7acb\u5373\u5bf9\u5176\u521d\u59cb\u5316\u3002\u56e0\u6b64\u4f60\u5f88\u5c11\u4f1a\u770b\u5230 std::initializer_list \u7684\u6700\u7b80\u5f62\u5f0f\u3002\u591a\u6570\u60c5\u51b5\u4e0b\u4f60\u770b\u5230\u7684\u5e94\u8be5\u662f\u8fd9\u6837\u7684 std::initializer_list \u6216\u8fd9\u6837\u7684 std::initializer_list \u3002 \u7b2c\u4e8c\uff0c std::initializer_list \u6709\u4e00\u4e2a size() \u6210\u5458\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u5143\u7d20\u4e2a\u6570\u3002\u5f53\u6211\u4eec\u9700\u8981\u6307\u5b9a\u4f20\u5165\u7684\u521d\u59cb\u5316\u5217\u8868\u591a\u957f\u65f6\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u6709\u7528\u3002 \u4f7f\u7528 std::initializer_list \u5bf9 IntArray \u6539\u5199\u4e4b\u540e\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 #include // for assert() #include // for std::initializer_list #include class IntArray { private : int m_length {}; int * m_data {}; public : IntArray () = default ; IntArray ( int length ) : m_length { length } , m_data { new int [ length ]{} } { } IntArray ( std :: initializer_list < int > list ) // allow IntArray to be initialized via list initialization : IntArray ( static_cast < int > ( list . size ())) // use delegating constructor to set up initial array { // Now initialize our array from the list int count { 0 }; for ( auto element : list ) { m_data [ count ] = element ; ++ count ; } } ~ IntArray () { delete [] m_data ; // we don't need to set m_data to null or m_length to 0 here, since the object will be destroyed immediately after this function anyway } IntArray ( const IntArray & ) = delete ; // to avoid shallow copies IntArray & operator = ( const IntArray & list ) = delete ; // to avoid shallow copies int & operator []( int index ) { assert ( index >= 0 && index < m_length ); return m_data [ index ]; } int getLength () const { return m_length ; } }; int main () { IntArray array { 5 , 4 , 3 , 2 , 1 }; // initializer list for ( int count { 0 }; count < array . getLength (); ++ count ) std :: cout << array [ count ] << ' ' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u7b26\u5408\u9884\u671f\uff1a 1 5 4 3 2 1 \u5f88\u597d\uff01\u63a5\u4e0b\u6765\u8ba9\u6211\u4eec\u6df1\u5165\u7814\u7a76\u4e00\u4e0b\uff1a IntArray \u7684\u6784\u9020\u51fd\u6570\u63a5\u53d7 std::initializer_list \u4f5c\u4e3a \u5f62\u53c2 \u3002 1 2 3 4 5 6 7 8 9 10 11 IntArray ( std :: initializer_list < int > list ) // \u4f7f\u5f97 IntArray \u53ef\u4ee5\u8fdb\u884c\u5217\u8868\u521d\u59cb\u5316 : IntArray ( static_cast < int > ( list . size ())) // \u4f7f\u7528\u59d4\u6d3e\u6784\u9020\u51fd\u6570\u8bbe\u7f6e\u6570\u7ec4 { // \u4f7f\u7528\u5217\u8868\u5143\u7d20\u521d\u59cb\u5316\u6570\u7ec4 int count { 0 }; for ( int element : list ) { m_data [ count ] = element ; ++ count ; } } \u7b2c\u4e00\u884c\uff1a\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u5c16\u62ec\u53f7\u8868\u660e\u5217\u8868\u4e2d\u5143\u7d20\u7684\u7c7b\u578b\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u8fd9\u662f\u4e00\u4e2a IntArray \uff0c\u6240\u4ee5\u521d\u59cb\u5316\u503c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u81ea\u7136\u9700\u8981\u662f int \u3002\u6ce8\u610f\uff0c\u6211\u4eec\u5e76\u6ca1\u6709 \u6309const\u5f15\u7528\u4f20\u9012 \u3002\u548c std::string_view \u7c7b\u4f3c\uff0c std::initializer_list \u662f\u975e\u5e38\u8f7b\u91cf\u7ea7\u7684\uff0c\u6240\u4ee5\u62f7\u8d1d\u6bd4\u95f4\u63a5\u8bbf\u95ee\u5f00\u9500\u8fd8\u5c0f\u3002 \u7b2c\u4e8c\u884c\uff1a\u6211\u4eec\u5fc5\u987b\u5c06\u5185\u5b58\u5206\u914d\u7684\u4efb\u52a1\u59d4\u6d3e\u7ed9\u5176\u4ed6\u6784\u9020\u51fd\u6570\uff08\u901a\u8fc7\u59d4\u6d3e\u6784\u9020\u51cf\u5c11\u5197\u4f59\u4ee3\u7801\uff09\u3002\u8be5\u59d4\u6d3e\u6784\u9020\u51fd\u6570\u9700\u8981\u6307\u5b9a\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u6240\u4ee5\u6211\u4eec\u901a\u8fc7 list.size() \u83b7\u53d6\u957f\u5ea6\u5e76\u5411\u5b83\u4f20\u9012\u8be5\u4fe1\u606f\u3002\u6ce8\u610f list.size() \u8fd4\u56de\u7684\u662f size_t (\u65e0\u7b26\u53f7)\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u5c06\u5176\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u6574\u578b\u3002\u6211\u4eec\u4f7f\u7528 \u76f4\u63a5\u521d\u59cb\u5316 \u800c\u4e0d\u662f \u62ec\u53f7\u521d\u59cb\u5316 \u56e0\u4e3a\u62ec\u53f7\u521d\u59cb\u5316\u9700\u8981 \u5217\u8868\u6784\u9020\u51fd\u6570 \uff08list constructors\uff09\u3002\u5c3d\u7ba1\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u88ab\u6b63\u786e\u89e3\u6790\uff0c\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\u7684\u65b9\u5f0f\u521d\u59cb\u5316\u5177\u6709\u5217\u8868\u6784\u9020\u51fd\u6570\u7684\u7c7b\u662f\u66f4\u5b89\u5168\u7684\uff08\u5982\u679c\u4f60\u672c\u610f\u4e0d\u662f\u4f7f\u7528\u5217\u8868\u6784\u9020\u51fd\u6570\u7684\u8bdd\uff09\u3002 \u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u4e2d\u7684\u4ee3\u7801\u7528\u4e8e\u5c06\u521d\u59cb\u5316\u5217\u8868\u4e2d\u7684\u503c\u62f7\u8d1d\u5230 IntArray \u4e2d\u3002\u4ee4\u4eba\u8d39\u89e3\u7684\u662f\uff0c std::initializer_list \u5e76\u6ca1\u6709\u63d0\u4f9b\u4e0b\u6807\u8fd0\u7b97\u7b26\u7528\u4e8e\u8bbf\u95ee\u5143\u7d20\u3002\u8fd9\u4e2a\u95ee\u9898\u5728C++\u6807\u51c6\u59d4\u5458\u4f1a\u4e0a\u88ab\u63d0\u53ca\u5f88\u591a\u6b21\u4f46\u6bcf\u4e00\u6b21\u90fd\u4e0d\u4e86\u4e86\u4e4b\u4e86\u3002 \u4e0d\u8fc7\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u65b9\u6cd5\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u6700\u7b80\u5355\u7684\u662f\u4f7f\u7528 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \uff0c\u5b83\u53ef\u4ee5\u4e00\u6b21\u904d\u5386\u521d\u59cb\u5316\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u7136\u540e\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u5143\u7d20\u62f7\u8d1d\u5230\u6211\u4eec\u7684\u5185\u90e8\u6570\u7ec4\u4e2d\u3002 \u6ce8\u610f\uff1a\u521d\u59cb\u5316\u5668\u5217\u8868\u603b\u662f\u503e\u5411\u4e8e\u8c03\u7528\u5339\u914d\u7684 initializer_list \u6784\u9020\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u5176\u4ed6\u53ef\u80fd\u5339\u914d\u7684\u6784\u9020\u51fd\u6570\u3002\u56e0\u6b64\uff0c\u8fd9\u4e2a\u53d8\u91cf\u5b9a\u4e49: 1 IntArray array { 5 }; \u4f1a\u5339\u914d\u5230 IntArray(std::initializer_list) \u800c\u4e0d\u662f IntArray(int) \u3002\u5982\u679c\u4f60\u5e0c\u671b\u5728 \u5217\u8868\u6784\u9020\u51fd\u6570 \u5b58\u5728\u7684\u60c5\u51b5\u4e0b\u5339\u914d IntArray(int) \uff0c\u90a3\u4e48\u4f60\u9700\u8981\u4f7f\u7528 \u62f7\u8d1d\u521d\u59cb\u5316 \u6216\u8005 \u76f4\u63a5\u521d\u59cb\u5316 \u3002\u5bf9\u4e8e std::vector \u548c\u5176\u4ed6\u540c\u65f6\u5177\u6709\u5217\u8868\u6784\u9020\u51fd\u6570\u548c\u7c7b\u4f3c\u6784\u9020\u51fd\u6570\u7684 \u5bb9\u5668\u7c7b \u4e5f\u662f\u4e00\u6837\u7684\u3002 1 2 std :: vector < int > array ( 5 ); // Calls std::vector::vector(std::vector::size_type), 5\u4e2a\u5143\u7d20\u88ab\u521d\u59cb\u5316\u4e3a0 std :: vector < int > array { 5 }; // Calls std::vector::vector(std::initializer_list), \u4e00\u4e2a\u5143\u7d20 5 COPY","title":"\u4f7f\u7528 std::initializer_list\u521d\u59cb\u5316"},{"location":"16-7-std-initializer_list/#stdinitializer_list_1","text":"\u91cd\u8f7d\u8d4b\u503c\u64cd\u4f5c\u7b26\u63a5\u53d7 std::initializer_list \u5f62\u53c2\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7 std::initializer_list \u6765\u4e3a\u7c7b\u8d4b\u503c\u4e86\u3002\u5de5\u4f5c\u539f\u7406\u4e0e\u4e0a\u9762\u7c7b\u4f3c\u3002\u5177\u4f53\u7684\u5b9e\u4f8b\u89c1quiz\u90e8\u5206\u3002\uff08\u89c1\u539f\u6587\uff09 \u6ce8\u610f\uff0c\u5982\u679c\u4f60\u5b9e\u73b0\u4e86\u4e00\u4e2a\u63a5\u53d7 std::initializer_list \u7684\u6784\u9020\u51fd\u6570\uff0c\u4f60\u5e94\u8be5\u786e\u4fdd\u4f60\u81f3\u5c11\u505a\u4e86\u4ee5\u4e0b\u5176\u4e2d\u4e4b\u4e00: \u63d0\u4f9b\u91cd\u8f7d\u7684 \u5217\u8868\u8d4b\u503c\u8fd0\u7b97\u7b26 \uff1b \u63d0\u4f9b\u6b63\u786e\u7684 \u6df1\u62f7\u8d1d \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \uff1b \u5220\u9664 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u3002 \u4e3a\u4ec0\u4e48\uff1f\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7c7b(\u4e0d\u6ee1\u8db3\u4e0a\u9762\u4e09\u4e2a\u6761\u4ef6)\u5728\u4f7f\u7528\u5217\u8868\u8d4b\u503c\u8bed\u53e5\u65f6\u53ef\u80fd\u51fa\u73b0\u7684\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 #include // for assert() #include // for std::initializer_list #include class IntArray { private : int m_length {}; int * m_data {}; public : IntArray () = default ; IntArray ( int length ) : m_length { length } , m_data { new int [ length ] {} } { } IntArray ( std :: initializer_list < int > list ) // allow IntArray to be initialized via list initialization : IntArray ( static_cast < int > ( list . size ())) // use delegating constructor to set up initial array { // Now initialize our array from the list int count { 0 }; for ( auto element : list ) { m_data [ count ] = element ; ++ count ; } } ~ IntArray () { delete [] m_data ; } // IntArray(const IntArray&) = delete; // to avoid shallow copies // IntArray& operator=(const IntArray& list) = delete; // to avoid shallow copies int & operator []( int index ) { assert ( index >= 0 && index < m_length ); return m_data [ index ]; } int getLength () const { return m_length ; } }; int main () { IntArray array {}; array = { 1 , 3 , 5 , 7 , 9 , 11 }; // Here's our list assignment statement for ( int count { 0 }; count < array . getLength (); ++ count ) std :: cout << array [ count ] << ' ' ; return 0 ; } \u9996\u5148\uff0c\u7f16\u8bd1\u5668\u4f1a\u6ce8\u610f\u5230\u5e26\u6709 std::initializer_list \u7684\u8d4b\u503c\u51fd\u6570\u4e0d\u5b58\u5728\u3002\u63a5\u4e0b\u6765\uff0c\u5b83\u5c06\u67e5\u627e\u5176\u4ed6\u53ef\u4ee5\u4f7f\u7528\u7684\u8d4b\u503c\u51fd\u6570\uff0c\u5e76\u53d1\u73b0 \u9690\u5f0f\u63d0\u4f9b \u7684 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u3002\u4f46\u662f\uff0c\u6b64\u51fd\u6570\u53ea\u5e94\u8be5\u5728\u521d\u59cb\u5316\u5668\u5217\u8868\u80fd\u591f\u88ab\u8f6c\u6362\u4e3a IntArray \u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002\u56e0\u4e3a {1,3,5,7,9,11} \u662f std::initializer_list \uff0c\u7f16\u8bd1\u5668\u5c06\u4f7f\u7528\u5217\u8868\u6784\u9020\u51fd\u6570\u5c06\u521d\u59cb\u5316\u5217\u8868\u8f6c\u6362\u4e3a\u4e34\u65f6\u7684 IntArray \u3002\u7136\u540e\u8c03\u7528\u9690\u5f0f\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u5c06\u4e34\u65f6\u7684 IntArray \u6d45\u62f7\u8d1d\u5230\u6570\u7ec4\u5bf9\u8c61\u4e2d\u3002 \u8fd9\u6837\u4e00\u6765\uff0c\u4e34\u65f6 IntArray \u7684 m_data \u548c array->m_data \u90fd\u6307\u5411\u540c\u4e00\u4e2a\u5730\u5740\uff08\u56e0\u4e3a \u6d45\u62f7\u8d1d \u7684\u539f\u56e0\uff09\u3002\u7ed3\u679c\u4f60\u5e94\u8be5\u4e5f\u80fd\u9884\u6599\u5230\u4e86\u3002 \u5728\u8d4b\u503c\u8bed\u53e5\u7684\u672b\u5c3e\uff0c\u4e34\u65f6\u7684 IntArray \u4f1a\u88ab\u9500\u6bc1\uff0c\u6b64\u65f6\u5b83\u7684\u6790\u6784\u51fd\u6570\u5c31\u4f1a\u5220\u9664 IntArray \u7684 m_data \u3002\u8fd9\u4f7f\u5f97 array->m_data \u79f0\u4e3a\u4e86\u4e00\u4e2a\u60ac\u5782\u6307\u9488\u3002\u5728\u540e\u7eed\u4f7f\u7528 array->m_data \u7684\u65f6\u5019\uff08\u4e0d\u8bba\u51fa\u4e8e\u4ec0\u4e48\u76ee\u7684\uff0c\u5305\u62ec\u6570\u7ec4 \u79bb\u5f00\u4f5c\u7528\u57df \u6216\u8005\u6790\u6784\u51fd\u6570\u5220\u9664 m_data \u65f6\uff09\uff0c\u90fd\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u63d0\u4f9b\u4e86\u5217\u8868\u6784\u9020\u51fd\u6570\uff0c\u8bf7\u540c\u65f6\u63d0\u4f9b\u5217\u8868\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002","title":"\u4f7f\u7528 std::initializer_list \u5bf9\u7c7b\u8fdb\u884c\u8d4b\u503c"},{"location":"16-7-std-initializer_list/#_1","text":"\u5b9e\u73b0\u4e00\u4e2a\u63a5\u53d7 std::initializer_list \u5f62\u53c2\u7684\u6784\u9020\u51fd\u6570\uff0c\u5141\u8bb8\u6211\u4eec\u5bf9\u81ea\u5b9a\u4e49\u7c7b\u4f7f\u7528\u5217\u8868\u521d\u59cb\u5316\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528 std::initializer_list \u6765\u5b9e\u73b0\u5176\u4ed6\u9700\u8981\u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u7684\u51fd\u6570\uff0c\u4f8b\u5982\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002","title":"\u5c0f\u7ed3"},{"location":"16-x-Chapter-16-comprehensive-quiz/","tags":["summary"],"text":"\u590d\u4e60 16.2 - \u7ec4\u5408\u5173\u7cfb \u4f7f\u7528\u7b80\u5355\u5bf9\u8c61\u6784\u5efa\u590d\u6742\u5bf9\u8c61\u7684\u8fc7\u7a0b\uff0c\u79f0\u4e3a \u5bf9\u8c61\u7ec4\u5408 \u3002\u5bf9\u8c61\u7ec4\u5408\u6709\u4e24\u79cd\uff1a \u7ec4\u5408 \u548c \u805a\u5408 \u3002 \u5f53\u4e00\u4e2a\u7c7b\u7684\u7c7b\u6210\u5458\u4e3a\u8be5\u7c7b\u7684 \u4e00\u90e8\u5206 \uff08part-of\uff09\u65f6\uff0c\u5c31\u4ea7\u751f\u4e86\u7ec4\u5408\u5173\u7cfb\u3002\u5728\u7ec4\u5408\u5173\u7cfb\u4e2d\uff0c\u7c7b\u8d1f\u8d23\u7ba1\u7406\u6210\u5458\u662f\u5426\u5b58\u5728\u3002\u4e00\u4e2a\u5408\u683c\u7684\u7ec4\u5408\u5173\u7cfb\u5e94\u8be5\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\uff1a \u90e8\u5206(\u6210\u5458) \u662f\u5bf9\u8c61\uff08\u7c7b\uff09\u7684\u4e00\u90e8\u5206\uff1b \u90e8\u5206(\u6210\u5458) \u540c\u65f6\u53ea\u80fd\u5c5e\u4e8e\u4e00\u4e2a\u5bf9\u8c61\uff08\u7c7b\uff09; \u90e8\u5206(\u6210\u5458) \u7684\u5b58\u5728\u4e0e\u5426\u53d7\u5230\u5bf9\u8c61\uff08\u7c7b\uff09\u7684\u7ba1\u7406\uff1b \u90e8\u5206(\u6210\u5458) \u5e76\u4e0d\u77e5\u9053\u5bf9\u8c61\uff08\u7c7b\uff09\u662f\u5426\u5b58\u5728\u3002 \u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u7ec4\u5408\u5173\u7cfb\u53ef\u4ee5\u4f7f\u7528\u4e00\u822c\u7684\u6210\u5458\u53d8\u91cf\u6765\u5b9e\u73b0\uff0c\u6216\u8005\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\uff0c\u4f46\u5176\u8868\u793a\u7684\u5185\u5b58\u7684\u7533\u8bf7\u548c\u91ca\u653e\u5fc5\u987b\u5b8c\u5168\u7531\u8be5\u7c7b\u6765\u7ba1\u7406\u3002\u5982\u679c\u4f60\u80fd\u591f\u5c06\u4e00\u4e2a\u7c7b\u5b9e\u73b0\u4e3a\u7ec4\u5408\u5173\u7cfb\uff0c\u90a3\u4f60\u5c31\u5e94\u8be5\u5c3d\u91cf\u8fd9\u4e48\u505a\u3002 16.3 - \u805a\u5408\u5173\u7cfb \u5f53\u67d0\u4e2a\u7c7b \u5177\u6709\uff08has\uff09 \u67d0\u4e2a\u6210\u5458\u662f\uff0c \u805a\u5408 \u5173\u7cfb\u5c31\u51fa\u73b0\u4e86\u3002\u5728\u805a\u5408\u5173\u7cfb\u4e2d\uff0c\u7c7b\u5e76\u4e0d\u8d1f\u8d23\u7ba1\u7406\u8be5\u6210\u5458\u7684\u5b58\u5728\u6027\u3002\u4e00\u4e2a\u5408\u683c\u7684\u805a\u5408\u5173\u7cfb\u5e94\u8be5\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\uff1a \u90e8\u5206(\u6210\u5458) \u662f\u5bf9\u8c61\uff08\u7c7b\uff09\u7684\u4e00\u90e8\u5206\uff1b \u90e8\u5206(\u6210\u5458) \u4e0d\u4ec5\u4ec5\u5c5e\u4e8e\u4e00\u4e2a\u5bf9\u8c61\uff08\u7c7b\uff09; \u90e8\u5206(\u6210\u5458) \u7684\u5b58\u5728\u4e0e\u5426\u4e0d\u4f1a\u53d7\u5230\u5bf9\u8c61\uff08\u7c7b\uff09\u7684\u7ba1\u7406\uff1b \u90e8\u5206(\u6210\u5458) \u5e76\u4e0d\u77e5\u9053\u5bf9\u8c61\uff08\u7c7b\uff09\u662f\u5426\u5b58\u5728\u3002 \u805a\u5408\u5173\u7cfb\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u548c\u5f15\u7528\u6765\u5b9e\u73b0\u3002 16.4 - \u5173\u8054\u5173\u7cfb \u5173\u8054 \u662f\u4e00\u79cd\u76f8\u5bf9\u66f4\u52a0\u677e\u6563\u7684\u5173\u7cfb\u3002\u7c7b\u4f7f\u7528\u4e86\u4e00\u4e2a\u4e0e\u5b83\u5e76\u65e0\u76f4\u63a5\u5173\u7cfb\u7684\u5bf9\u8c61\u3002\u53ea\u6709\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\uff0c\u624d\u80fd\u5c06\u4e24\u4e2a\u5bf9\u8c61\u79f0\u4e3a\u5173\u8054\u5173\u7cfb\uff1a \u5173\u8054\u5bf9\u8c61\uff08\u6210\u5458\uff09\u4e0e\u8be5\u5bf9\u8c61\uff08\u7c7b\uff09\u672c\u4e0d\u8be5\u76f8\u5173\uff1b \u5173\u8054\u5bf9\u8c61\uff08\u6210\u5458\uff09\u53ef\u4ee5\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u5bf9\u8c61\uff08\u7c7b\uff09\uff1b \u5173\u8054\u5bf9\u8c61\uff08\u6210\u5458\uff09\u5b58\u5728\u4e0e\u5426\u4e0d\u4f1a\u53d7\u5230\u5bf9\u8c61\uff08\u7c7b\uff09\u7684\u7ba1\u7406\uff1b \u5173\u8054\u5bf9\u8c61\uff08\u6210\u5458\uff09\u5bf9\u5bf9\u8c61\uff08\u6216\u7c7b\uff09\u7684\u5b58\u5728\uff0c\u53ef\u80fd\u77e5\u6653\u4e5f\u53ef\u80fd\u4e0d\u77e5\u6653\u3002 \u5173\u8054\u5173\u7cfb\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u6216\u5f15\u7528\u6765\u5b9e\u73b0\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u66f4\u52a0\u95f4\u63a5\u7684\u65b9\u5f0f\u5b9e\u73b0\uff08\u4f8b\u5982\u901a\u8fc7\u5173\u8054\u5bf9\u8c61\u7684\u5bf9\u5e94\u952e\u6216\u7d22\u5f15\u6765\u4f7f\u7528\u5b83\uff09\u3002 16.5 - \u4f9d\u8d56\u5173\u7cfb \u5f53\u4e00\u4e2a\u7c7b\u4f7f\u7528\u53e6\u4e00\u4e2a\u7c7b\u5b8c\u6210\u67d0\u9879\u4efb\u52a1\u65f6\uff0c\u5b83\u4eec\u4e4b\u95f4\u5c5e\u4e8e \u4f9d\u8d56 \u5173\u7cfb\u3002\u88ab\u4f9d\u8d56\u7684\u5bf9\u8c61\u901a\u5e38\u5e76\u4e0d\u662f\u4f7f\u7528\u5b83\u7684\u5bf9\u8c61\u7684\u4e00\u90e8\u5206\u3002\u88ab\u4f7f\u7528\u7684\u5bf9\u8c61\u901a\u5e38\u662f\u88ab\u4e34\u65f6\u521b\u5efa\u3001\u4f7f\u7528\u7136\u540e\u9500\u6bc1\uff0c\u6216\u8005\u662f\u901a\u8fc7\u6210\u5458\u51fd\u6570\u4f20\u5165\u5e76\u4f7f\u7528\u7684\u5916\u90e8\u8d44\u6e90\u3002 16.6 - \u5bb9\u5668\u7c7b \u5bb9\u5668\u7c7b(container class) \u6307\u7684\u662f\u4e3a\u5176\u4ed6\u4e00\u4e9b\u7c7b\u63d0\u4f9b\u5bb9\u5668\u6765\u5b58\u50a8\u5b83\u4eec\u7684\u7c7b\u3002 \u503c\u5bb9\u5668 \u548c\u5b83\u4fdd\u5b58\u7684\u6570\u636e\u526f\u672c\u5c5e\u4e8e\u7ec4\u5408\u5173\u7cfb\u3002 \u5f15\u7528\u5bb9\u5668 \u662f\u4e00\u79cd\u805a\u5408\u5173\u7cfb\uff0c\u5b83\u4fdd\u5b58\u4e86\u5bb9\u5668\u5916\u90e8\u5bf9\u8c61\u7684\u6307\u9488\u6216\u5f15\u7528\u3002 16.7 - std::initializer_list std::initializer_list \u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u6784\u9020\u51fd\u6570\u3001\u8d4b\u503c\u64cd\u4f5c\u7b26\u548c\u5176\u4ed6\u63a5\u53d7\u5217\u8868\u521d\u59cb\u5316\u53c2\u6570\u7684\u51fd\u6570\u3002 std::initailizer_list \u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d\u3002 \u5c5e\u6027\u7c7b\u578b \u7ec4\u5408 \u805a\u5408 \u5173\u8054 \u4f9d\u8d56 \u5173\u7cfb\u7c7b\u578b \u6574\u4f53/\u90e8\u5206 \u6574\u4f53/\u90e8\u5206 \u672c\u4e0d\u76f8\u5173 \u672c\u4e0d\u76f8\u5173 \u6210\u5458\u662f\u5426\u53ef\u4ee5\u5c5e\u4e8e\u591a\u4e2a\u7c7b No Yes Yes Yes \u6210\u5458\u5b58\u5728\u4e0e\u5426\u662f\u5426\u53d7\u7c7b\u7ba1\u7406 Yes No No No \u65b9\u5411 \u5355\u5411 \u5355\u5411 \u5355\u5411\u6216\u53cc\u5411 \u5355\u5411\u6216\u53cc\u5411 \u5173\u7cfb\u52a8\u8bcd Part-of Has-a Uses-a Depends-on","title":"16.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5bf9\u8c61\u5173\u7cfb"},{"location":"16-x-Chapter-16-comprehensive-quiz/#_1","text":"16.2 - \u7ec4\u5408\u5173\u7cfb \u4f7f\u7528\u7b80\u5355\u5bf9\u8c61\u6784\u5efa\u590d\u6742\u5bf9\u8c61\u7684\u8fc7\u7a0b\uff0c\u79f0\u4e3a \u5bf9\u8c61\u7ec4\u5408 \u3002\u5bf9\u8c61\u7ec4\u5408\u6709\u4e24\u79cd\uff1a \u7ec4\u5408 \u548c \u805a\u5408 \u3002 \u5f53\u4e00\u4e2a\u7c7b\u7684\u7c7b\u6210\u5458\u4e3a\u8be5\u7c7b\u7684 \u4e00\u90e8\u5206 \uff08part-of\uff09\u65f6\uff0c\u5c31\u4ea7\u751f\u4e86\u7ec4\u5408\u5173\u7cfb\u3002\u5728\u7ec4\u5408\u5173\u7cfb\u4e2d\uff0c\u7c7b\u8d1f\u8d23\u7ba1\u7406\u6210\u5458\u662f\u5426\u5b58\u5728\u3002\u4e00\u4e2a\u5408\u683c\u7684\u7ec4\u5408\u5173\u7cfb\u5e94\u8be5\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\uff1a \u90e8\u5206(\u6210\u5458) \u662f\u5bf9\u8c61\uff08\u7c7b\uff09\u7684\u4e00\u90e8\u5206\uff1b \u90e8\u5206(\u6210\u5458) \u540c\u65f6\u53ea\u80fd\u5c5e\u4e8e\u4e00\u4e2a\u5bf9\u8c61\uff08\u7c7b\uff09; \u90e8\u5206(\u6210\u5458) \u7684\u5b58\u5728\u4e0e\u5426\u53d7\u5230\u5bf9\u8c61\uff08\u7c7b\uff09\u7684\u7ba1\u7406\uff1b \u90e8\u5206(\u6210\u5458) \u5e76\u4e0d\u77e5\u9053\u5bf9\u8c61\uff08\u7c7b\uff09\u662f\u5426\u5b58\u5728\u3002 \u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u7ec4\u5408\u5173\u7cfb\u53ef\u4ee5\u4f7f\u7528\u4e00\u822c\u7684\u6210\u5458\u53d8\u91cf\u6765\u5b9e\u73b0\uff0c\u6216\u8005\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\uff0c\u4f46\u5176\u8868\u793a\u7684\u5185\u5b58\u7684\u7533\u8bf7\u548c\u91ca\u653e\u5fc5\u987b\u5b8c\u5168\u7531\u8be5\u7c7b\u6765\u7ba1\u7406\u3002\u5982\u679c\u4f60\u80fd\u591f\u5c06\u4e00\u4e2a\u7c7b\u5b9e\u73b0\u4e3a\u7ec4\u5408\u5173\u7cfb\uff0c\u90a3\u4f60\u5c31\u5e94\u8be5\u5c3d\u91cf\u8fd9\u4e48\u505a\u3002 16.3 - \u805a\u5408\u5173\u7cfb \u5f53\u67d0\u4e2a\u7c7b \u5177\u6709\uff08has\uff09 \u67d0\u4e2a\u6210\u5458\u662f\uff0c \u805a\u5408 \u5173\u7cfb\u5c31\u51fa\u73b0\u4e86\u3002\u5728\u805a\u5408\u5173\u7cfb\u4e2d\uff0c\u7c7b\u5e76\u4e0d\u8d1f\u8d23\u7ba1\u7406\u8be5\u6210\u5458\u7684\u5b58\u5728\u6027\u3002\u4e00\u4e2a\u5408\u683c\u7684\u805a\u5408\u5173\u7cfb\u5e94\u8be5\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\uff1a \u90e8\u5206(\u6210\u5458) \u662f\u5bf9\u8c61\uff08\u7c7b\uff09\u7684\u4e00\u90e8\u5206\uff1b \u90e8\u5206(\u6210\u5458) \u4e0d\u4ec5\u4ec5\u5c5e\u4e8e\u4e00\u4e2a\u5bf9\u8c61\uff08\u7c7b\uff09; \u90e8\u5206(\u6210\u5458) \u7684\u5b58\u5728\u4e0e\u5426\u4e0d\u4f1a\u53d7\u5230\u5bf9\u8c61\uff08\u7c7b\uff09\u7684\u7ba1\u7406\uff1b \u90e8\u5206(\u6210\u5458) \u5e76\u4e0d\u77e5\u9053\u5bf9\u8c61\uff08\u7c7b\uff09\u662f\u5426\u5b58\u5728\u3002 \u805a\u5408\u5173\u7cfb\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u548c\u5f15\u7528\u6765\u5b9e\u73b0\u3002 16.4 - \u5173\u8054\u5173\u7cfb \u5173\u8054 \u662f\u4e00\u79cd\u76f8\u5bf9\u66f4\u52a0\u677e\u6563\u7684\u5173\u7cfb\u3002\u7c7b\u4f7f\u7528\u4e86\u4e00\u4e2a\u4e0e\u5b83\u5e76\u65e0\u76f4\u63a5\u5173\u7cfb\u7684\u5bf9\u8c61\u3002\u53ea\u6709\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\uff0c\u624d\u80fd\u5c06\u4e24\u4e2a\u5bf9\u8c61\u79f0\u4e3a\u5173\u8054\u5173\u7cfb\uff1a \u5173\u8054\u5bf9\u8c61\uff08\u6210\u5458\uff09\u4e0e\u8be5\u5bf9\u8c61\uff08\u7c7b\uff09\u672c\u4e0d\u8be5\u76f8\u5173\uff1b \u5173\u8054\u5bf9\u8c61\uff08\u6210\u5458\uff09\u53ef\u4ee5\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u5bf9\u8c61\uff08\u7c7b\uff09\uff1b \u5173\u8054\u5bf9\u8c61\uff08\u6210\u5458\uff09\u5b58\u5728\u4e0e\u5426\u4e0d\u4f1a\u53d7\u5230\u5bf9\u8c61\uff08\u7c7b\uff09\u7684\u7ba1\u7406\uff1b \u5173\u8054\u5bf9\u8c61\uff08\u6210\u5458\uff09\u5bf9\u5bf9\u8c61\uff08\u6216\u7c7b\uff09\u7684\u5b58\u5728\uff0c\u53ef\u80fd\u77e5\u6653\u4e5f\u53ef\u80fd\u4e0d\u77e5\u6653\u3002 \u5173\u8054\u5173\u7cfb\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u6216\u5f15\u7528\u6765\u5b9e\u73b0\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u66f4\u52a0\u95f4\u63a5\u7684\u65b9\u5f0f\u5b9e\u73b0\uff08\u4f8b\u5982\u901a\u8fc7\u5173\u8054\u5bf9\u8c61\u7684\u5bf9\u5e94\u952e\u6216\u7d22\u5f15\u6765\u4f7f\u7528\u5b83\uff09\u3002 16.5 - \u4f9d\u8d56\u5173\u7cfb \u5f53\u4e00\u4e2a\u7c7b\u4f7f\u7528\u53e6\u4e00\u4e2a\u7c7b\u5b8c\u6210\u67d0\u9879\u4efb\u52a1\u65f6\uff0c\u5b83\u4eec\u4e4b\u95f4\u5c5e\u4e8e \u4f9d\u8d56 \u5173\u7cfb\u3002\u88ab\u4f9d\u8d56\u7684\u5bf9\u8c61\u901a\u5e38\u5e76\u4e0d\u662f\u4f7f\u7528\u5b83\u7684\u5bf9\u8c61\u7684\u4e00\u90e8\u5206\u3002\u88ab\u4f7f\u7528\u7684\u5bf9\u8c61\u901a\u5e38\u662f\u88ab\u4e34\u65f6\u521b\u5efa\u3001\u4f7f\u7528\u7136\u540e\u9500\u6bc1\uff0c\u6216\u8005\u662f\u901a\u8fc7\u6210\u5458\u51fd\u6570\u4f20\u5165\u5e76\u4f7f\u7528\u7684\u5916\u90e8\u8d44\u6e90\u3002 16.6 - \u5bb9\u5668\u7c7b \u5bb9\u5668\u7c7b(container class) \u6307\u7684\u662f\u4e3a\u5176\u4ed6\u4e00\u4e9b\u7c7b\u63d0\u4f9b\u5bb9\u5668\u6765\u5b58\u50a8\u5b83\u4eec\u7684\u7c7b\u3002 \u503c\u5bb9\u5668 \u548c\u5b83\u4fdd\u5b58\u7684\u6570\u636e\u526f\u672c\u5c5e\u4e8e\u7ec4\u5408\u5173\u7cfb\u3002 \u5f15\u7528\u5bb9\u5668 \u662f\u4e00\u79cd\u805a\u5408\u5173\u7cfb\uff0c\u5b83\u4fdd\u5b58\u4e86\u5bb9\u5668\u5916\u90e8\u5bf9\u8c61\u7684\u6307\u9488\u6216\u5f15\u7528\u3002 16.7 - std::initializer_list std::initializer_list \u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u6784\u9020\u51fd\u6570\u3001\u8d4b\u503c\u64cd\u4f5c\u7b26\u548c\u5176\u4ed6\u63a5\u53d7\u5217\u8868\u521d\u59cb\u5316\u53c2\u6570\u7684\u51fd\u6570\u3002 std::initailizer_list \u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d\u3002 \u5c5e\u6027\u7c7b\u578b \u7ec4\u5408 \u805a\u5408 \u5173\u8054 \u4f9d\u8d56 \u5173\u7cfb\u7c7b\u578b \u6574\u4f53/\u90e8\u5206 \u6574\u4f53/\u90e8\u5206 \u672c\u4e0d\u76f8\u5173 \u672c\u4e0d\u76f8\u5173 \u6210\u5458\u662f\u5426\u53ef\u4ee5\u5c5e\u4e8e\u591a\u4e2a\u7c7b No Yes Yes Yes \u6210\u5458\u5b58\u5728\u4e0e\u5426\u662f\u5426\u53d7\u7c7b\u7ba1\u7406 Yes No No No \u65b9\u5411 \u5355\u5411 \u5355\u5411 \u5355\u5411\u6216\u53cc\u5411 \u5355\u5411\u6216\u53cc\u5411 \u5173\u7cfb\u52a8\u8bcd Part-of Has-a Uses-a Depends-on","title":"\u590d\u4e60"},{"location":"17-1-introduction-to-inheritance/","tags":["inheritance"],"text":"Key Takeaway - \u5728\u4e0a\u4e00\u7ae0\u4e2d\u6211\u4eec\u8ba8\u8bba\u4e86 \u5bf9\u8c61\u7ec4\u5408 \u2014\u2014\u590d\u6742\u7684\u7c7b\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u7c7b\u548c\u7c7b\u578b\u7ec4\u6210\u3002\u5bf9\u8c61\u7ec4\u5408\u975e\u5e38\u9002\u5408\u6784\u5efa\u6574\u4f53\u548c\u90e8\u5206\u5177\u6709\u201c\u6709\u4e00\u4e2a\u201d\u8bed\u4e49\u5173\u7cfb\u7684\u5bf9\u8c61\u3002\u4e0d\u8fc7\uff0c\u5bf9\u8c61\u7ec4\u5408\u53ea\u662fC++\u63d0\u4f9b\u7684\u4e24\u79cd\u6784\u5efa\u590d\u6742\u7c7b\u7684\u65b9\u5f0f\u4e4b\u4e00\u3002\u53e6\u5916\u4e00\u79cd\u4e3b\u8981\u65b9\u5f0f\u662f \u7ee7\u627f \uff0c\u5b83\u8868\u793a\u4e24\u4e2a\u5bf9\u8c61\u4e4b\u95f4\u7684\u201c\u662f\u4e00\u4e2a\u201d\u5173\u7cfb\u3002 \u7ee7\u627f\u548c\u7ec4\u5408\u4e0d\u540c\uff0c\u7ec4\u5408\u8bed\u4e49\u6d89\u53ca\u5230\u4e00\u4e2a\u5bf9\u8c61\u5bf9\u53e6\u5916\u4e00\u4e9b\u5bf9\u8c61\u7684\u7ec4\u5408\u548c\u8fde\u63a5\uff0c\u7ee7\u627f\u5219\u662f\u65b0\u5bf9\u8c61\u901a\u8fc7\u76f4\u63a5\u83b7\u53d6\u5176\u4ed6\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u884c\u4e3a\u6765\u6269\u5c55\u6216\u7279\u6b8a\u5316(specializing)\u81ea\u5df1\u3002\u548c\u5bf9\u8c61\u7ec4\u5408\u7c7b\u4f3c\uff0c\u7ee7\u627f\u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\u4e5f\u968f\u5904\u53ef\u89c1\u2014\u2014\u4f60\u7ee7\u627f\u4e86\u7236\u6bcd\u7684\u57fa\u56e0\uff0c\u4ece\u4ed6\u4eec\u8eab\u4e0a\u83b7\u53d6\u4e86\u4e00\u4e9b\u7269\u7406\u7279\u5f81\uff0c\u4f46\u662f\u4f60\u540c\u65f6\u4e5f\u5177\u6709\u4e2a\u6027\u7684\u4e00\u9762\u3002\u79d1\u6280\u4ea7\u54c1\uff08\u8ba1\u7b97\u673a\u3001\u624b\u673a\u7b49\uff09\u4ece\u5b83\u4eec\u7684\u8001\u6b3e\u4e2d\u7ee7\u627f\u4e86\u4e00\u4e9b\u7279\u6027\uff08\u5f53\u9700\u8981\u5411\u540e\u517c\u5bb9\u7684\u65f6\u5019\uff09\u3002\u4f8b\u5982\uff0cIntel \u5954\u817e\u5904\u7406\u5668\u4ece 486 \u5904\u7406\u5668\u4e0a\u7ee7\u627f\u4e86\u5f88\u591a\u529f\u80fd\uff0c\u800c486\u4e5f\u4ece\u66f4\u65e9\u671f\u7684\u5904\u7406\u5668\u4e0a\u7ee7\u627f\u4e86\u4e00\u4e9b\u529f\u80fd\u3002C++ \u7ee7\u627f\u4e86\u5f88\u591a C \u8bed\u8a00\u7684\u7279\u5f81\uff0c\u800c C \u8bed\u8a00\u4e5f\u4ece\u66f4\u65e9\u671f\u7684\u7f16\u7a0b\u8bed\u8a00\u4e2d\u6709\u6240\u501f\u9274\u3002 \u4ee5\u82f9\u679c\u548c\u9999\u8549\u4e3a\u4f8b\u3002\u5c3d\u7ba1\u82f9\u679c\u548c\u9999\u8549\u662f\u4e0d\u540c\u7684\u6c34\u679c\uff0c\u4f46\u662f\u5b83\u4eec\u90fd\u662f \u6c34\u679c \u3002\u56e0\u4e3a\u5b83\u4eec\u90fd\u662f\u6c34\u679c\uff0c\u6240\u4ee5\u4ece\u903b\u8f91\u7684\u89d2\u5ea6\u6765\u8bb2\uff0c\u4efb\u4f55\u6709\u5173\u6c34\u679c\u7684\u4e3a\u771f\u7684\u8bba\u65ad\uff0c\u540c\u6837\u4e5f\u9002\u7528\u4e8e\u82f9\u679c\u548c\u9999\u8549\u3002\u4f8b\u5982\uff0c\u6c34\u679c\u6709\u540d\u5b57\u3001\u989c\u8272\u548c\u5927\u5c0f\u3002\u56e0\u6b64\uff0c\u82f9\u679c\u548c\u9999\u8549\u4e5f\u6709\u540d\u5b57\u3001\u989c\u8272\u548c\u5927\u5c0f\u3002\u6211\u4eec\u53ef\u4ee5\u8bf4\u82f9\u679c\u548c\u9999\u8549\u7ee7\u627f\u4e86\u6c34\u679c\u7684\u4e00\u4e9b\u5c5e\u6027\u3002\u6211\u4eec\u8fd8\u77e5\u9053\uff0c\u6c34\u679c\u9700\u8981\u7ecf\u5386\u4e00\u4e2a\u6210\u719f\u7684\u8fc7\u7a0b\uff0c\u6210\u719f\u4e4b\u540e\u7684\u6c34\u679c\u5c31\u53ef\u4ee5\u5403\u4e86\u3002\u56e0\u4e3a\u82f9\u679c\u548c\u9999\u8549\u662f\u6c34\u679c\uff0c\u6240\u4ee5\u5b83\u4eec\u4e5f\u7ee7\u627f\u4e86\u8fd9\u4e2a\u6210\u719f\u7684\u8fc7\u7a0b\u3002 \u5982\u679c\u901a\u8fc7\u56fe\u8868\u5f62\u5f0f\u6765\u5c55\u73b0\u82f9\u679c\u3001\u9999\u8549\u548c\u6c34\u679c\u7684\u5173\u7cfb\uff0c\u5e94\u8be5\u770b\u4e0a\u53bb\u662f\u8fd9\u4e2a\u6837\u5b50\u7684\uff1a \u4ece\u56fe\u4e2d\u53ef\u4ee5\u770b\u51fa\u4e00\u79cd\u5c42\u6b21\u7ed3\u6784\u3002 \u5c42\u6b21\u7ed3\u6784 \u5c42\u6b21\u7ed3\u6784\u662f\u4e00\u79cd\u5bf9\u8c61\u95f4\u7684\u5173\u7cfb\uff08\u57fa\u4e8e\u56fe\u8868\u8fdb\u884c\u5c55\u793a\uff09\u3002\u5927\u591a\u6570\u7684\u5c42\u6b21\u7ed3\u6784\u80fd\u591f\u4f53\u73b0\u968f\u7740\u65f6\u95f4\u53d8\u5316\u7684\u8fc7\u7a0b (386 -> 486 -> Pentium)\uff0c\u6216\u662f\u4ece\u5bbd\u6cdb\u6982\u5ff5\u9010\u6e10\u5206\u5316\u7684\u8fc7\u7a0b(\u6c34\u679c -> \u82f9\u679c -> \u86c7\u679c)\u3002\u5982\u679c\u4f60\u4e0a\u8fc7\u751f\u7269\u8bfe\u7684\u8bdd\uff0c\u90a3\u4e48\u5e94\u8be5\u4f1a\u4e86\u89e3\u8457\u540d\u7684\u201c \u57df \u3001\u754c\u3001 \u95e8 \u3001 \u7eb2 \u3001 \u76ee \u3001 \u79d1 \u3001 \u5c5e \u3001 \u79cd \u201c\uff0c\u8fd9\u79cd\u5c42\u6b21\u7ed3\u6784\uff08\u4ece\u4e00\u822c\u5230\u7279\u6b8a\uff09 \u8fd9\u662f\u53e6\u4e00\u4e2a\u5c42\u6b21\u7ed3\u6784\u7684\u4f8b\u5b50\uff1a\u6b63\u65b9\u5f62\u662f\u77e9\u5f62\uff0c\u77e9\u5f62\u662f\u56db\u8fb9\u5f62\uff0c\u56db\u8fb9\u5f62\u662f\u4e00\u79cd\u5f62\u72b6\u3002\u76f4\u89d2\u4e09\u89d2\u5f62\u662f\u4e09\u89d2\u5f62\uff0c\u4e09\u89d2\u5f62\u4e5f\u662f\u4e00\u79cd\u5f62\u72b6\u3002\u5c06\u5176\u653e\u5165\u5c42\u6b21\u56fe\u4e2d\uff0c\u770b\u8d77\u6765\u50cf\u8fd9\u6837: \u8fd9\u4e2a\u56fe\u4ece\u4e00\u822c\u7684(\u4e0a)\u5230\u7279\u6b8a\u7684(\u4e0b)\uff0c\u5c42\u6b21\u7ed3\u6784\u4e2d\u7684\u6bcf\u4e2a\u9879\u76ee\u7ee7\u627f\u4e0a\u9762\u9879\u76ee\u7684\u5c5e\u6027\u548c\u884c\u4e3a\u3002 \u5148\u7779\u4e3a\u5feb \u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba\u4f7f\u7528 C++ \u4e2d\u7ee7\u627f\u7684\u57fa\u672c\u65b9\u5f0f\u3002 \u5728\u4e0b\u4e00\u7ae0\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba\u7ee7\u627f\u5982\u4f55\u57fa\u4e8e \u865a\u51fd\u6570 \u6765 \u591a\u6001 \uff08\u9762\u5411\u5bf9\u8c61\u4e2d\u6700\u5e38\u89c1\u7684\u6982\u5ff5\u4e4b\u4e00\uff09\u3002 \u968f\u540e\uff0c\u6211\u4eec\u8fd8\u4f1a\u8ba8\u8bba\u4f7f\u7528\u7ee7\u627f\u800c\u5e26\u6765\u7684\u4e00\u4e9b\u4f18\u52bf\u548c\u52a3\u52bf\u3002","title":"17.1 - \u7ee7\u627f\u7b80\u4ecb"},{"location":"17-1-introduction-to-inheritance/#_1","text":"\u5c42\u6b21\u7ed3\u6784\u662f\u4e00\u79cd\u5bf9\u8c61\u95f4\u7684\u5173\u7cfb\uff08\u57fa\u4e8e\u56fe\u8868\u8fdb\u884c\u5c55\u793a\uff09\u3002\u5927\u591a\u6570\u7684\u5c42\u6b21\u7ed3\u6784\u80fd\u591f\u4f53\u73b0\u968f\u7740\u65f6\u95f4\u53d8\u5316\u7684\u8fc7\u7a0b (386 -> 486 -> Pentium)\uff0c\u6216\u662f\u4ece\u5bbd\u6cdb\u6982\u5ff5\u9010\u6e10\u5206\u5316\u7684\u8fc7\u7a0b(\u6c34\u679c -> \u82f9\u679c -> \u86c7\u679c)\u3002\u5982\u679c\u4f60\u4e0a\u8fc7\u751f\u7269\u8bfe\u7684\u8bdd\uff0c\u90a3\u4e48\u5e94\u8be5\u4f1a\u4e86\u89e3\u8457\u540d\u7684\u201c \u57df \u3001\u754c\u3001 \u95e8 \u3001 \u7eb2 \u3001 \u76ee \u3001 \u79d1 \u3001 \u5c5e \u3001 \u79cd \u201c\uff0c\u8fd9\u79cd\u5c42\u6b21\u7ed3\u6784\uff08\u4ece\u4e00\u822c\u5230\u7279\u6b8a\uff09 \u8fd9\u662f\u53e6\u4e00\u4e2a\u5c42\u6b21\u7ed3\u6784\u7684\u4f8b\u5b50\uff1a\u6b63\u65b9\u5f62\u662f\u77e9\u5f62\uff0c\u77e9\u5f62\u662f\u56db\u8fb9\u5f62\uff0c\u56db\u8fb9\u5f62\u662f\u4e00\u79cd\u5f62\u72b6\u3002\u76f4\u89d2\u4e09\u89d2\u5f62\u662f\u4e09\u89d2\u5f62\uff0c\u4e09\u89d2\u5f62\u4e5f\u662f\u4e00\u79cd\u5f62\u72b6\u3002\u5c06\u5176\u653e\u5165\u5c42\u6b21\u56fe\u4e2d\uff0c\u770b\u8d77\u6765\u50cf\u8fd9\u6837: \u8fd9\u4e2a\u56fe\u4ece\u4e00\u822c\u7684(\u4e0a)\u5230\u7279\u6b8a\u7684(\u4e0b)\uff0c\u5c42\u6b21\u7ed3\u6784\u4e2d\u7684\u6bcf\u4e2a\u9879\u76ee\u7ee7\u627f\u4e0a\u9762\u9879\u76ee\u7684\u5c5e\u6027\u548c\u884c\u4e3a\u3002","title":"\u5c42\u6b21\u7ed3\u6784"},{"location":"17-1-introduction-to-inheritance/#_2","text":"\u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba\u4f7f\u7528 C++ \u4e2d\u7ee7\u627f\u7684\u57fa\u672c\u65b9\u5f0f\u3002 \u5728\u4e0b\u4e00\u7ae0\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba\u7ee7\u627f\u5982\u4f55\u57fa\u4e8e \u865a\u51fd\u6570 \u6765 \u591a\u6001 \uff08\u9762\u5411\u5bf9\u8c61\u4e2d\u6700\u5e38\u89c1\u7684\u6982\u5ff5\u4e4b\u4e00\uff09\u3002 \u968f\u540e\uff0c\u6211\u4eec\u8fd8\u4f1a\u8ba8\u8bba\u4f7f\u7528\u7ee7\u627f\u800c\u5e26\u6765\u7684\u4e00\u4e9b\u4f18\u52bf\u548c\u52a3\u52bf\u3002","title":"\u5148\u7779\u4e3a\u5feb"},{"location":"17-2-basic-inheritance-in-c%2B%2B/","tags":["inheritance"],"text":"Key Takeaway - \u4ece\u62bd\u8c61\u5c42\u9762\u8ba8\u8bba\u8fc7\u7ee7\u627f\u4e4b\u540e\uff0c\u8ba9\u6211\u4eec\u6765\u770b\u770b C++ \u4e2d\u7684\u7ee7\u627f\u5427\u3002 C++\u7684\u7ee7\u627f\u53d1\u751f\u5728\u7c7b\u4e0e\u7c7b\u4e4b\u95f4\u3002\u5728\u7ee7\u627f\u5173\u7cfb\u4e2d\uff0c\u88ab\u7ee7\u627f\u7684\u7c7b\u79f0\u4e3a \u7236\u7c7b(parent class) \u3001 \u57fa\u7c7b(base class) \u6216 \u8d85\u7c7b(super class) \u3002\u800c\u7ee7\u627f\u7236\u7c7b\u7684\u7c7b\uff0c\u5219\u79f0\u4e3a \u5b50\u7c7b(child class / sub class) \u3001 \u6d3e\u751f\u7c7b \u3002 \u5728\u4e0a\u56fe\u4e2d\uff0c\u6c34\u679c\u662f\u7236\u7c7b\uff0c\u82f9\u679c\u548c\u9999\u8549\u90fd\u662f\u5b50\u7c7b\u3002 \u5728\u4e0a\u56fe\u4e2d\uff0c\u4e09\u89d2\u5f62\u65e2\u662f\u5b50\u7c7b\uff08 \u5f62\u72b6 \u7684\u5b50\u7c7b\uff09\uff0c\u4e5f\u662f\u7236\u7c7b\uff08\u5bf9\u76f4\u89d2\u4e09\u89d2\u5f62\u800c\u8a00\uff09\u3002 \u5b50\u7c7b\u4ece\u7236\u7c7b\u7ee7\u627f\u884c\u4e3a(\u6210\u5458\u51fd\u6570)\u548c\u5c5e\u6027(\u6210\u5458\u53d8\u91cf)(\u9700\u8981\u9075\u5b88\u8bbf\u95ee\u9650\u5236\uff0c\u6211\u4eec\u5c06\u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\u8ba8\u8bba)\u3002 \u8fd9\u4e9b\u53d8\u91cf\u548c\u51fd\u6570\u6210\u4e3a\u6d3e\u751f\u7c7b\u7684\u6210\u5458\u3002 \u56e0\u4e3a\u5b50\u7c7b\u4e5f\u662f\u4e00\u4e2a\u6807\u51c6\u7684\u7c7b\uff0c\u6240\u4ee5\u5b83\u4eec(\u5f53\u7136)\u53ef\u4ee5\u6709\u81ea\u5df1\u7684\u7c7b\u6210\u5458\u3002 Person \u7c7b \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528 Person \u8868\u793a\u4e00\u4e2a\u201d\u4eba\u7c7b\u201c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include class Person { // In this example, we're making our members public for simplicity public : std :: string m_name {}; int m_age {}; Person ( const std :: string & name = \"\" , int age = 0 ) : m_name { name }, m_age { age } { } const std :: string & getName () const { return m_name ; } int getAge () const { return m_age ; } }; \u56e0\u4e3a\u8fd9\u4e2a Person \u7c7b\u88ab\u8bbe\u8ba1\u7528\u6765\u8868\u793a\u4e00\u822c\u7684\u4eba\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u5b9a\u4e49\u4e86\u4efb\u4f55\u7c7b\u578b\u7684\u4eba\u90fd\u901a\u7528\u7684\u6210\u5458\u3002\u6bcf\u4e2a\u4eba(\u4e0d\u8bba\u6027\u522b\u3001\u804c\u4e1a\u7b49)\u90fd\u6709\u540d\u5b57\u548c\u5e74\u9f84\uff0c\u6240\u4ee5\u5728\u8fd9\u91cc\u8868\u793a\u51fa\u6765\u3002 \u6ce8\u610f\uff0c\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u6240\u6709\u53d8\u91cf\u548c\u51fd\u6570\u8bbe\u7f6e\u4e3a\u516c\u5171\u7684\u3002\u8fd9\u7eaf\u7cb9\u662f\u4e3a\u4e86\u4f7f\u8fd9\u4e9b\u793a\u4f8b\u4fdd\u6301\u7b80\u5355\u3002\u901a\u5e38\u6211\u4eec\u4f1a\u5c06\u53d8\u91cf\u8bbe\u4e3a\u79c1\u6709\u3002\u6211\u4eec\u5c06\u5728\u672c\u7ae0\u540e\u9762\u8ba8\u8bba\u8bbf\u95ee\u63a7\u5236\u4ee5\u53ca\u5b83\u4eec\u5982\u4f55\u4e0e\u7ee7\u627f\u4ea4\u4e92\u3002 BaseballPlayer \u7c7b \u5047\u8bbe\u6211\u4eec\u60f3\u8981\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\u6765\u8bb0\u5f55\u68d2\u7403\u8fd0\u52a8\u5458\u7684\u4fe1\u606f\u3002\u68d2\u7403\u8fd0\u52a8\u5458\u9700\u8981\u5305\u542b\u7279\u5b9a\u7684\u5173\u4e8e\u68d2\u7403\u8fd0\u52a8\u5458\u7684\u4fe1\u606f\u2014\u2014\u4f8b\u5982\uff0c\u7403\u5458\u7684\u51fb\u7403\u7387\u548c\u672c\u5792\u6253\u6570\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4e0d\u5b8c\u6574\u7684\u7248\u672c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class BaseballPlayer { // In this example, we're making our members public for simplicity public : double m_battingAverage {}; int m_homeRuns {}; BaseballPlayer ( double battingAverage = 0.0 , int homeRuns = 0 ) : m_battingAverage { battingAverage }, m_homeRuns { homeRuns } { } }; \u6211\u4eec\u540c\u65f6\u8fd8\u9700\u8981\u8bb0\u5f55\u8fd0\u52a8\u5458\u7684\u59d3\u540d\u548c\u5e74\u9f84\uff0c\u800c Person \u7c7b\u4e2d\u6b63\u597d\u5305\u542b\u8fd9\u4e9b\u4fe1\u606f\u3002 \u6211\u4eec\u6709\u4e09\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4e3a BaseballPlayer \u7c7b\u6dfb\u52a0\u59d3\u540d\u548c\u5e74\u9f84\uff1a \u76f4\u63a5\u5c06\u59d3\u540d\u548c\u5e74\u9f84\u4f5c\u4e3a\u6210\u5458\u6dfb\u52a0\u5230 BaseballPlayer \u7c7b\u4e2d\u3002\u8fd9\u53ef\u80fd\u662f\u6700\u7cdf\u7cd5\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u6211\u4eec\u590d\u5236\u7684\u662f Person \u7c7b\u4e2d\u5df2\u7ecf\u5b58\u5728\u7684\u4ee3\u7801\u3002\u5bf9 Person \u7684\u4efb\u4f55\u66f4\u65b0\u90fd\u5fc5\u987b\u5728 BaseballPlayer \u4e2d\u8fdb\u884c\u3002 \u4f7f\u7528\u7ec4\u5408\u5c06 Person \u6dfb\u52a0\u4e3a BaseballPlayer \u7684\u6210\u5458\u3002\u4f46\u6211\u4eec\u5fc5\u987b\u95ee\u81ea\u5df1\uff0c\u201c\u4e00\u4e2a\u68d2\u7403\u8fd0\u52a8\u5458\u6709\u4e00\u4e2a\u4eba\u5417\uff1f\u201d \u5e76\u4e0d\u662f\u3002\u6240\u4ee5\u8fd9\u4e0d\u662f\u6b63\u786e\u7684\u8303\u5f0f\u3002 \u8ba9 BaseballPlayer \u4ece Person \u7ee7\u627f\u8fd9\u4e9b\u5c5e\u6027\u3002\u8bb0\u4f4f\uff0c\u7ee7\u627f\u8868\u793a is-a \u5173\u7cfb\u3002\u68d2\u7403\u8fd0\u52a8\u5458\u662f\u4eba\u5417\uff1f\u5f53\u7136\u662f\u3002\u6240\u4ee5\u7ee7\u627f\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002 \u8ba9 BaseballPlayer \u6210\u4e3a\u6d3e\u751f\u7c7b \u8ba9 BaseballPlayer \u7ee7\u627f Person \u7c7b\u7684\u8bed\u6cd5\u5f88\u7b80\u5355\u3002\u5728\u5b8c\u6210 class BaseballPlayer \u58f0\u660e\u540e\uff0c\u4f7f\u7528\u4e00\u4e2a\u5192\u53f7\uff0c\u5916\u52a0 public \u5173\u952e\u5b57\uff0c\u63a5\u4e0a\u9700\u8981\u7ee7\u627f\u7684\u7c7b\u7684\u540d\u5b57\u5373\u53ef\u3002\u8fd9\u79cd\u65b9\u5f0f\u79f0\u4e3a \u516c\u5f00\u7ee7\u627f \u3002\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u518d\u8be6\u7ec6\u8ba8\u8bba\u516c\u5f00\u7ee7\u627f\u7684\u8bdd\u9898\u3002 1 2 3 4 5 6 7 8 9 10 11 12 // BaseballPlayer publicly inheriting Person class BaseballPlayer : public Person { public : double m_battingAverage {}; int m_homeRuns {}; BaseballPlayer ( double battingAverage = 0.0 , int homeRuns = 0 ) : m_battingAverage { battingAverage }, m_homeRuns { homeRuns } { } }; \u4f7f\u7528\u6d3e\u751f\u5173\u7cfb\u56fe\uff0c\u6211\u4eec\u7684\u7ee7\u627f\u770b\u8d77\u6765\u50cf\u8fd9\u6837\uff1a \u5f53 BaseballPlayer \u7ee7\u627f Person \u540e\uff0c BaseballPlayer \u5c31\u83b7\u53d6\u4e86 Person \u7684 \u6210\u5458\u53d8\u91cf \u548c \u6210\u5458\u51fd\u6570 \u3002\u6b64\u5916\uff0c BaseballPlayer \u8fd8\u5b9a\u4e49\u4e86\u4e24\u4e2a\u81ea\u5df1\u7279\u6709\u7684\u6210\u5458\uff1a m_battingAverage \u548c m_homeRuns \u3002\u8fd9\u4e48\u505a\u7684\u539f\u56e0\u5f88\u7b80\u5355\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u5c5e\u6027\u662f\u5c5e\u4e8e BaseballPlayer \u7684\uff0c\u800c\u4e0d\u662f\u4efb\u4f55 Person \u6240\u5171\u6709\u7684\u3002 \u56e0\u6b64\uff0c BaseballPlayer \u5bf9\u8c61\u6700\u7ec8\u4f1a\u5305\u542b4\u4e2a\u6210\u5458\u53d8\u91cf\uff1a m_battingAverage \u548c m_homeRuns \u662f BaseballPlayer \u7279\u6709\u7684\uff0c m_name \u548c m_age \u662f\u7ee7\u627f\u81ea Person \u7684\u3002 \u4e0a\u9762\u8bba\u8ff0\u5f88\u5bb9\u6613\u8bc1\u660e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include #include class Person { public : std :: string m_name {}; int m_age {}; Person ( const std :: string & name = \"\" , int age = 0 ) : m_name { name }, m_age { age } { } const std :: string & getName () const { return m_name ; } int getAge () const { return m_age ; } }; // BaseballPlayer publicly inheriting Person class BaseballPlayer : public Person { public : double m_battingAverage {}; int m_homeRuns {}; BaseballPlayer ( double battingAverage = 0.0 , int homeRuns = 0 ) : m_battingAverage { battingAverage }, m_homeRuns { homeRuns } { } }; int main () { // Create a new BaseballPlayer object BaseballPlayer joe {}; // Assign it a name (we can do this directly because m_name is public) joe . m_name = \"Joe\" ; // Print out the name std :: cout << joe . getName () << '\\n' ; // use the getName() function we've acquired from the Person base class return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 Joe \u4ee3\u7801\u53ef\u4ee5\u6b63\u786e\u5730\u7f16\u8bd1\u8fd0\u884c\uff0c\u56e0\u4e3a joe \u662f BaseballPlayer \uff0c\u800c\u6240\u6709\u7684 BaseballPlayer \u7c7b\u578b\u7684\u5bf9\u8c61\u90fd\u5177\u6709 m_name \u6210\u5458\u53d8\u91cf\u548c getName() \u6210\u5458\u51fd\u6570\uff0c\u5b83\u4eec\u662f\u4ece Person \u7c7b\u7ee7\u627f\u8fc7\u6765\u7684\u3002 Employee \u6d3e\u751f\u7c7b \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u518d\u7f16\u5199\u53e6\u5916\u4e00\u4e2a\u7c7b\u5e76\u7ee7\u627f Person \u3002\u8fd9\u6b21\uff0c\u6211\u4eec\u7f16\u5199\u4e00\u4e2a Employee \u7c7b\u3002\u96c7\u5458\u663e\u7136\u201d\u662f\u4e00\u4e2a\u201c\u4eba\uff0c\u6240\u4ee5\u4f7f\u5176\u7ee7\u627f Person \u7c7b\u4f3c\u662f\u5408\u9002\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // Employee publicly inherits from Person class Employee : public Person { public : double m_hourlySalary {}; long m_employeeID {}; Employee ( double hourlySalary = 0.0 , long employeeID = 0 ) : m_hourlySalary { hourlySalary }, m_employeeID { employeeID } { } void printNameAndSalary () const { std :: cout << m_name << \": \" << m_hourlySalary << '\\n' ; } }; Employee \u4ece Person \u7ee7\u627f\u4e86 m_name \u548c m_age (\u4ee5\u53ca\u4e24\u4e2a \u6210\u5458\u8bbf\u95ee\u51fd\u6570 )\uff0c\u540c\u65f6\u5b83\u8fd8\u6dfb\u52a0\u4e86\u4e24\u4e2a\u989d\u5916\u7684\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u51fd\u6570\u3002\u6ce8\u610f\uff0c printNameAndSalary() \u540c\u65f6\u4f7f\u7528\u4e86\u6765\u81ea\u4e24\u4e2a\u7c7b\u7684\u53d8\u91cf( Employee::m_hourlySalary \u548c Person::m_name )\u3002 \u6d3e\u751f\u7ed3\u6784\u56fe\u5982\u4e0b\uff1a \u5982\u56fe\u6240\u793a\uff0c Employee \u548c BaseballPlayer \u5e76\u6ca1\u6709\u76f4\u63a5\u5173\u7cfb\uff0c\u5373\u4f7f\u5b83\u4eec\u90fd\u6d3e\u751f\u81ea Person \u3002 \u57fa\u4e8e Employee \u7c7b\u7f16\u5199\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #include #include class Person { public : std :: string m_name {}; int m_age {}; const std :: string & getName () const { return m_name ; } int getAge () const { return m_age ; } Person ( const std :: string & name = \"\" , int age = 0 ) : m_name { name }, m_age { age } { } }; // Employee \u516c\u5f00\u7ee7\u627f\u4e86 Person class Employee : public Person { public : double m_hourlySalary {}; long m_employeeID {}; Employee ( double hourlySalary = 0.0 , long employeeID = 0 ) : m_hourlySalary { hourlySalary }, m_employeeID { employeeID } { } void printNameAndSalary () const { std :: cout << m_name << \": \" << m_hourlySalary << '\\n' ; } }; int main () { Employee frank { 20.25 , 12345 }; frank . m_name = \"Frank\" ; // \u53ef\u4ee5\u8fd9\u4e48\u505a\u662f\u56e0\u4e3a m_name \u662f public frank . printNameAndSalary (); return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 Frank: 20.25 \u7ee7\u627f\u94fe \u6d3e\u751f\u7c7b\u672c\u8eab\u4e5f\u662f\u53ef\u4ee5\u88ab\u7ee7\u627f\u7684\u3002\u800c\u4e14\u5728\u64cd\u4f5c\u548c\u4f7f\u7528\u4e0a\u90fd\u6ca1\u6709\u4ec0\u4e48\u7279\u522b\u4e4b\u5904\u3002 \u4f8b\u5982 Supervisor \u7c7b\u672c\u8eab\u662f Employee \u7c7b\u7684\u6d3e\u751f\u7c7b\uff0c Employee \u7c7b\u5219\u662f Person \u7c7b\u7684\u5b50\u7c7b\uff1a 1 2 3 4 5 6 class Supervisor : public Employee { public : // This Supervisor can oversee a max of 5 employees long m_overseesIDs [ 5 ]{}; }; \u6d3e\u751f\u7ed3\u6784\u56fe\u5982\u4e0b\uff1a \u6240\u6709\u7684 Supervisor \u5bf9\u8c61\u90fd\u7ee7\u627f Employee \u548c Person \u7684\u51fd\u6570\u548c\u53d8\u91cf\uff0c\u5e76\u6dfb\u52a0\u81ea\u5df1\u7684 m_overseesIDs \u6210\u5458\u53d8\u91cf\u3002 \u901a\u8fc7\u6784\u5efa\u8fd9\u6837\u7684\u7ee7\u627f\u94fe\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u7ec4\u53ef\u91cd\u7528\u7684\u7c7b\uff0c\u8fd9\u4e9b\u7c7b\u975e\u5e38\u901a\u7528(\u4f4d\u4e8e\u6d3e\u751f\u5c42\u6b21\u7ed3\u6784\u7684\u9876\u90e8)\uff0c\u5e76\u5728\u6bcf\u4e2a\u7ee7\u627f\u7ea7\u522b\u4e0a\u9010\u6e10\u53d8\u5f97\u66f4\u52a0\u5177\u4f53\u3002 \u4e3a\u4ec0\u4e48\u6b64\u7c7b\u7ee7\u627f\u662f\u6709\u7528\u7684\uff1f \u4ece\u57fa\u7c7b\u7ee7\u627f\u610f\u5473\u7740\u4e0d\u5fc5\u5728\u6d3e\u751f\u7c7b\u4e2d\u91cd\u65b0\u5b9a\u4e49\u57fa\u7c7b\u7684\u4fe1\u606f\u3002\u6211\u4eec\u901a\u8fc7\u7ee7\u627f\u81ea\u52a8\u63a5\u6536\u57fa\u7c7b\u7684\u6210\u5458\u51fd\u6570\u548c\u6210\u5458\u53d8\u91cf\uff0c\u7136\u540e\u7b80\u5355\u5730\u6dfb\u52a0\u6240\u9700\u7684\u9644\u52a0\u51fd\u6570\u6216\u6210\u5458\u53d8\u91cf\u3002\u8fd9\u4e0d\u4ec5\u8282\u7701\u4e86\u5de5\u4f5c\uff0c\u800c\u4e14\u8fd8\u610f\u5473\u7740\u5982\u679c\u6211\u4eec\u66f4\u65b0\u6216\u4fee\u6539\u57fa\u7c7b(\u4f8b\u5982\u6dfb\u52a0\u65b0\u51fd\u6570\uff0c\u6216\u4fee\u590dbug)\uff0c\u6211\u4eec\u6240\u6709\u7684\u6d3e\u751f\u7c7b\u5c06\u81ea\u52a8\u7ee7\u627f\u66f4\u6539! \u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u5411 Person \u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u529f\u80fd\uff0c Employee \u548c Supervisor \u90fd\u4f1a\u81ea\u52a8\u83b7\u5f97\u5bf9\u5b83\u7684\u8bbf\u95ee\u6743\u9650\u3002\u5982\u679c\u6211\u4eec\u5411 Employee \u6dfb\u52a0\u4e00\u4e2a\u65b0\u53d8\u91cf\uff0c Supervisor \u4e5f\u5c06\u83b7\u5f97\u5bf9\u5b83\u7684\u8bbf\u95ee\u6743\u3002\u8fd9\u5141\u8bb8\u6211\u4eec\u4ee5\u4e00\u79cd\u7b80\u5355\u3001\u76f4\u89c2\u548c\u4f4e\u7ef4\u62a4\u7684\u65b9\u5f0f\u6784\u9020\u65b0\u7c7b! \u7ed3\u8bba \u901a\u8fc7\u7ee7\u627f\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u5176\u4ed6\u7c7b\u4e2d\u83b7\u53d6\u5176\u6210\u5458\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u91cd\u7528\u3002\u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u63a2\u7d22\u7ee7\u627f\u7684\u5de5\u4f5c\u539f\u7406\u3002","title":"17.2 - C++\u7ee7\u627f\u57fa\u7840"},{"location":"17-2-basic-inheritance-in-c%2B%2B/#person","text":"\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528 Person \u8868\u793a\u4e00\u4e2a\u201d\u4eba\u7c7b\u201c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include class Person { // In this example, we're making our members public for simplicity public : std :: string m_name {}; int m_age {}; Person ( const std :: string & name = \"\" , int age = 0 ) : m_name { name }, m_age { age } { } const std :: string & getName () const { return m_name ; } int getAge () const { return m_age ; } }; \u56e0\u4e3a\u8fd9\u4e2a Person \u7c7b\u88ab\u8bbe\u8ba1\u7528\u6765\u8868\u793a\u4e00\u822c\u7684\u4eba\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u5b9a\u4e49\u4e86\u4efb\u4f55\u7c7b\u578b\u7684\u4eba\u90fd\u901a\u7528\u7684\u6210\u5458\u3002\u6bcf\u4e2a\u4eba(\u4e0d\u8bba\u6027\u522b\u3001\u804c\u4e1a\u7b49)\u90fd\u6709\u540d\u5b57\u548c\u5e74\u9f84\uff0c\u6240\u4ee5\u5728\u8fd9\u91cc\u8868\u793a\u51fa\u6765\u3002 \u6ce8\u610f\uff0c\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u6240\u6709\u53d8\u91cf\u548c\u51fd\u6570\u8bbe\u7f6e\u4e3a\u516c\u5171\u7684\u3002\u8fd9\u7eaf\u7cb9\u662f\u4e3a\u4e86\u4f7f\u8fd9\u4e9b\u793a\u4f8b\u4fdd\u6301\u7b80\u5355\u3002\u901a\u5e38\u6211\u4eec\u4f1a\u5c06\u53d8\u91cf\u8bbe\u4e3a\u79c1\u6709\u3002\u6211\u4eec\u5c06\u5728\u672c\u7ae0\u540e\u9762\u8ba8\u8bba\u8bbf\u95ee\u63a7\u5236\u4ee5\u53ca\u5b83\u4eec\u5982\u4f55\u4e0e\u7ee7\u627f\u4ea4\u4e92\u3002","title":"Person \u7c7b"},{"location":"17-2-basic-inheritance-in-c%2B%2B/#baseballplayer","text":"\u5047\u8bbe\u6211\u4eec\u60f3\u8981\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\u6765\u8bb0\u5f55\u68d2\u7403\u8fd0\u52a8\u5458\u7684\u4fe1\u606f\u3002\u68d2\u7403\u8fd0\u52a8\u5458\u9700\u8981\u5305\u542b\u7279\u5b9a\u7684\u5173\u4e8e\u68d2\u7403\u8fd0\u52a8\u5458\u7684\u4fe1\u606f\u2014\u2014\u4f8b\u5982\uff0c\u7403\u5458\u7684\u51fb\u7403\u7387\u548c\u672c\u5792\u6253\u6570\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4e0d\u5b8c\u6574\u7684\u7248\u672c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class BaseballPlayer { // In this example, we're making our members public for simplicity public : double m_battingAverage {}; int m_homeRuns {}; BaseballPlayer ( double battingAverage = 0.0 , int homeRuns = 0 ) : m_battingAverage { battingAverage }, m_homeRuns { homeRuns } { } }; \u6211\u4eec\u540c\u65f6\u8fd8\u9700\u8981\u8bb0\u5f55\u8fd0\u52a8\u5458\u7684\u59d3\u540d\u548c\u5e74\u9f84\uff0c\u800c Person \u7c7b\u4e2d\u6b63\u597d\u5305\u542b\u8fd9\u4e9b\u4fe1\u606f\u3002 \u6211\u4eec\u6709\u4e09\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4e3a BaseballPlayer \u7c7b\u6dfb\u52a0\u59d3\u540d\u548c\u5e74\u9f84\uff1a \u76f4\u63a5\u5c06\u59d3\u540d\u548c\u5e74\u9f84\u4f5c\u4e3a\u6210\u5458\u6dfb\u52a0\u5230 BaseballPlayer \u7c7b\u4e2d\u3002\u8fd9\u53ef\u80fd\u662f\u6700\u7cdf\u7cd5\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u6211\u4eec\u590d\u5236\u7684\u662f Person \u7c7b\u4e2d\u5df2\u7ecf\u5b58\u5728\u7684\u4ee3\u7801\u3002\u5bf9 Person \u7684\u4efb\u4f55\u66f4\u65b0\u90fd\u5fc5\u987b\u5728 BaseballPlayer \u4e2d\u8fdb\u884c\u3002 \u4f7f\u7528\u7ec4\u5408\u5c06 Person \u6dfb\u52a0\u4e3a BaseballPlayer \u7684\u6210\u5458\u3002\u4f46\u6211\u4eec\u5fc5\u987b\u95ee\u81ea\u5df1\uff0c\u201c\u4e00\u4e2a\u68d2\u7403\u8fd0\u52a8\u5458\u6709\u4e00\u4e2a\u4eba\u5417\uff1f\u201d \u5e76\u4e0d\u662f\u3002\u6240\u4ee5\u8fd9\u4e0d\u662f\u6b63\u786e\u7684\u8303\u5f0f\u3002 \u8ba9 BaseballPlayer \u4ece Person \u7ee7\u627f\u8fd9\u4e9b\u5c5e\u6027\u3002\u8bb0\u4f4f\uff0c\u7ee7\u627f\u8868\u793a is-a \u5173\u7cfb\u3002\u68d2\u7403\u8fd0\u52a8\u5458\u662f\u4eba\u5417\uff1f\u5f53\u7136\u662f\u3002\u6240\u4ee5\u7ee7\u627f\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002","title":"BaseballPlayer \u7c7b"},{"location":"17-2-basic-inheritance-in-c%2B%2B/#baseballplayer_1","text":"\u8ba9 BaseballPlayer \u7ee7\u627f Person \u7c7b\u7684\u8bed\u6cd5\u5f88\u7b80\u5355\u3002\u5728\u5b8c\u6210 class BaseballPlayer \u58f0\u660e\u540e\uff0c\u4f7f\u7528\u4e00\u4e2a\u5192\u53f7\uff0c\u5916\u52a0 public \u5173\u952e\u5b57\uff0c\u63a5\u4e0a\u9700\u8981\u7ee7\u627f\u7684\u7c7b\u7684\u540d\u5b57\u5373\u53ef\u3002\u8fd9\u79cd\u65b9\u5f0f\u79f0\u4e3a \u516c\u5f00\u7ee7\u627f \u3002\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u518d\u8be6\u7ec6\u8ba8\u8bba\u516c\u5f00\u7ee7\u627f\u7684\u8bdd\u9898\u3002 1 2 3 4 5 6 7 8 9 10 11 12 // BaseballPlayer publicly inheriting Person class BaseballPlayer : public Person { public : double m_battingAverage {}; int m_homeRuns {}; BaseballPlayer ( double battingAverage = 0.0 , int homeRuns = 0 ) : m_battingAverage { battingAverage }, m_homeRuns { homeRuns } { } }; \u4f7f\u7528\u6d3e\u751f\u5173\u7cfb\u56fe\uff0c\u6211\u4eec\u7684\u7ee7\u627f\u770b\u8d77\u6765\u50cf\u8fd9\u6837\uff1a \u5f53 BaseballPlayer \u7ee7\u627f Person \u540e\uff0c BaseballPlayer \u5c31\u83b7\u53d6\u4e86 Person \u7684 \u6210\u5458\u53d8\u91cf \u548c \u6210\u5458\u51fd\u6570 \u3002\u6b64\u5916\uff0c BaseballPlayer \u8fd8\u5b9a\u4e49\u4e86\u4e24\u4e2a\u81ea\u5df1\u7279\u6709\u7684\u6210\u5458\uff1a m_battingAverage \u548c m_homeRuns \u3002\u8fd9\u4e48\u505a\u7684\u539f\u56e0\u5f88\u7b80\u5355\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u5c5e\u6027\u662f\u5c5e\u4e8e BaseballPlayer \u7684\uff0c\u800c\u4e0d\u662f\u4efb\u4f55 Person \u6240\u5171\u6709\u7684\u3002 \u56e0\u6b64\uff0c BaseballPlayer \u5bf9\u8c61\u6700\u7ec8\u4f1a\u5305\u542b4\u4e2a\u6210\u5458\u53d8\u91cf\uff1a m_battingAverage \u548c m_homeRuns \u662f BaseballPlayer \u7279\u6709\u7684\uff0c m_name \u548c m_age \u662f\u7ee7\u627f\u81ea Person \u7684\u3002 \u4e0a\u9762\u8bba\u8ff0\u5f88\u5bb9\u6613\u8bc1\u660e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include #include class Person { public : std :: string m_name {}; int m_age {}; Person ( const std :: string & name = \"\" , int age = 0 ) : m_name { name }, m_age { age } { } const std :: string & getName () const { return m_name ; } int getAge () const { return m_age ; } }; // BaseballPlayer publicly inheriting Person class BaseballPlayer : public Person { public : double m_battingAverage {}; int m_homeRuns {}; BaseballPlayer ( double battingAverage = 0.0 , int homeRuns = 0 ) : m_battingAverage { battingAverage }, m_homeRuns { homeRuns } { } }; int main () { // Create a new BaseballPlayer object BaseballPlayer joe {}; // Assign it a name (we can do this directly because m_name is public) joe . m_name = \"Joe\" ; // Print out the name std :: cout << joe . getName () << '\\n' ; // use the getName() function we've acquired from the Person base class return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 Joe \u4ee3\u7801\u53ef\u4ee5\u6b63\u786e\u5730\u7f16\u8bd1\u8fd0\u884c\uff0c\u56e0\u4e3a joe \u662f BaseballPlayer \uff0c\u800c\u6240\u6709\u7684 BaseballPlayer \u7c7b\u578b\u7684\u5bf9\u8c61\u90fd\u5177\u6709 m_name \u6210\u5458\u53d8\u91cf\u548c getName() \u6210\u5458\u51fd\u6570\uff0c\u5b83\u4eec\u662f\u4ece Person \u7c7b\u7ee7\u627f\u8fc7\u6765\u7684\u3002","title":"\u8ba9 BaseballPlayer \u6210\u4e3a\u6d3e\u751f\u7c7b"},{"location":"17-2-basic-inheritance-in-c%2B%2B/#employee","text":"\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u518d\u7f16\u5199\u53e6\u5916\u4e00\u4e2a\u7c7b\u5e76\u7ee7\u627f Person \u3002\u8fd9\u6b21\uff0c\u6211\u4eec\u7f16\u5199\u4e00\u4e2a Employee \u7c7b\u3002\u96c7\u5458\u663e\u7136\u201d\u662f\u4e00\u4e2a\u201c\u4eba\uff0c\u6240\u4ee5\u4f7f\u5176\u7ee7\u627f Person \u7c7b\u4f3c\u662f\u5408\u9002\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // Employee publicly inherits from Person class Employee : public Person { public : double m_hourlySalary {}; long m_employeeID {}; Employee ( double hourlySalary = 0.0 , long employeeID = 0 ) : m_hourlySalary { hourlySalary }, m_employeeID { employeeID } { } void printNameAndSalary () const { std :: cout << m_name << \": \" << m_hourlySalary << '\\n' ; } }; Employee \u4ece Person \u7ee7\u627f\u4e86 m_name \u548c m_age (\u4ee5\u53ca\u4e24\u4e2a \u6210\u5458\u8bbf\u95ee\u51fd\u6570 )\uff0c\u540c\u65f6\u5b83\u8fd8\u6dfb\u52a0\u4e86\u4e24\u4e2a\u989d\u5916\u7684\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u51fd\u6570\u3002\u6ce8\u610f\uff0c printNameAndSalary() \u540c\u65f6\u4f7f\u7528\u4e86\u6765\u81ea\u4e24\u4e2a\u7c7b\u7684\u53d8\u91cf( Employee::m_hourlySalary \u548c Person::m_name )\u3002 \u6d3e\u751f\u7ed3\u6784\u56fe\u5982\u4e0b\uff1a \u5982\u56fe\u6240\u793a\uff0c Employee \u548c BaseballPlayer \u5e76\u6ca1\u6709\u76f4\u63a5\u5173\u7cfb\uff0c\u5373\u4f7f\u5b83\u4eec\u90fd\u6d3e\u751f\u81ea Person \u3002 \u57fa\u4e8e Employee \u7c7b\u7f16\u5199\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #include #include class Person { public : std :: string m_name {}; int m_age {}; const std :: string & getName () const { return m_name ; } int getAge () const { return m_age ; } Person ( const std :: string & name = \"\" , int age = 0 ) : m_name { name }, m_age { age } { } }; // Employee \u516c\u5f00\u7ee7\u627f\u4e86 Person class Employee : public Person { public : double m_hourlySalary {}; long m_employeeID {}; Employee ( double hourlySalary = 0.0 , long employeeID = 0 ) : m_hourlySalary { hourlySalary }, m_employeeID { employeeID } { } void printNameAndSalary () const { std :: cout << m_name << \": \" << m_hourlySalary << '\\n' ; } }; int main () { Employee frank { 20.25 , 12345 }; frank . m_name = \"Frank\" ; // \u53ef\u4ee5\u8fd9\u4e48\u505a\u662f\u56e0\u4e3a m_name \u662f public frank . printNameAndSalary (); return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 Frank: 20.25","title":"Employee \u6d3e\u751f\u7c7b"},{"location":"17-2-basic-inheritance-in-c%2B%2B/#_1","text":"\u6d3e\u751f\u7c7b\u672c\u8eab\u4e5f\u662f\u53ef\u4ee5\u88ab\u7ee7\u627f\u7684\u3002\u800c\u4e14\u5728\u64cd\u4f5c\u548c\u4f7f\u7528\u4e0a\u90fd\u6ca1\u6709\u4ec0\u4e48\u7279\u522b\u4e4b\u5904\u3002 \u4f8b\u5982 Supervisor \u7c7b\u672c\u8eab\u662f Employee \u7c7b\u7684\u6d3e\u751f\u7c7b\uff0c Employee \u7c7b\u5219\u662f Person \u7c7b\u7684\u5b50\u7c7b\uff1a 1 2 3 4 5 6 class Supervisor : public Employee { public : // This Supervisor can oversee a max of 5 employees long m_overseesIDs [ 5 ]{}; }; \u6d3e\u751f\u7ed3\u6784\u56fe\u5982\u4e0b\uff1a \u6240\u6709\u7684 Supervisor \u5bf9\u8c61\u90fd\u7ee7\u627f Employee \u548c Person \u7684\u51fd\u6570\u548c\u53d8\u91cf\uff0c\u5e76\u6dfb\u52a0\u81ea\u5df1\u7684 m_overseesIDs \u6210\u5458\u53d8\u91cf\u3002 \u901a\u8fc7\u6784\u5efa\u8fd9\u6837\u7684\u7ee7\u627f\u94fe\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u7ec4\u53ef\u91cd\u7528\u7684\u7c7b\uff0c\u8fd9\u4e9b\u7c7b\u975e\u5e38\u901a\u7528(\u4f4d\u4e8e\u6d3e\u751f\u5c42\u6b21\u7ed3\u6784\u7684\u9876\u90e8)\uff0c\u5e76\u5728\u6bcf\u4e2a\u7ee7\u627f\u7ea7\u522b\u4e0a\u9010\u6e10\u53d8\u5f97\u66f4\u52a0\u5177\u4f53\u3002","title":"\u7ee7\u627f\u94fe"},{"location":"17-2-basic-inheritance-in-c%2B%2B/#_2","text":"\u4ece\u57fa\u7c7b\u7ee7\u627f\u610f\u5473\u7740\u4e0d\u5fc5\u5728\u6d3e\u751f\u7c7b\u4e2d\u91cd\u65b0\u5b9a\u4e49\u57fa\u7c7b\u7684\u4fe1\u606f\u3002\u6211\u4eec\u901a\u8fc7\u7ee7\u627f\u81ea\u52a8\u63a5\u6536\u57fa\u7c7b\u7684\u6210\u5458\u51fd\u6570\u548c\u6210\u5458\u53d8\u91cf\uff0c\u7136\u540e\u7b80\u5355\u5730\u6dfb\u52a0\u6240\u9700\u7684\u9644\u52a0\u51fd\u6570\u6216\u6210\u5458\u53d8\u91cf\u3002\u8fd9\u4e0d\u4ec5\u8282\u7701\u4e86\u5de5\u4f5c\uff0c\u800c\u4e14\u8fd8\u610f\u5473\u7740\u5982\u679c\u6211\u4eec\u66f4\u65b0\u6216\u4fee\u6539\u57fa\u7c7b(\u4f8b\u5982\u6dfb\u52a0\u65b0\u51fd\u6570\uff0c\u6216\u4fee\u590dbug)\uff0c\u6211\u4eec\u6240\u6709\u7684\u6d3e\u751f\u7c7b\u5c06\u81ea\u52a8\u7ee7\u627f\u66f4\u6539! \u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u5411 Person \u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u529f\u80fd\uff0c Employee \u548c Supervisor \u90fd\u4f1a\u81ea\u52a8\u83b7\u5f97\u5bf9\u5b83\u7684\u8bbf\u95ee\u6743\u9650\u3002\u5982\u679c\u6211\u4eec\u5411 Employee \u6dfb\u52a0\u4e00\u4e2a\u65b0\u53d8\u91cf\uff0c Supervisor \u4e5f\u5c06\u83b7\u5f97\u5bf9\u5b83\u7684\u8bbf\u95ee\u6743\u3002\u8fd9\u5141\u8bb8\u6211\u4eec\u4ee5\u4e00\u79cd\u7b80\u5355\u3001\u76f4\u89c2\u548c\u4f4e\u7ef4\u62a4\u7684\u65b9\u5f0f\u6784\u9020\u65b0\u7c7b!","title":"\u4e3a\u4ec0\u4e48\u6b64\u7c7b\u7ee7\u627f\u662f\u6709\u7528\u7684\uff1f"},{"location":"17-2-basic-inheritance-in-c%2B%2B/#_3","text":"\u901a\u8fc7\u7ee7\u627f\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u5176\u4ed6\u7c7b\u4e2d\u83b7\u53d6\u5176\u6210\u5458\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u91cd\u7528\u3002\u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u63a2\u7d22\u7ee7\u627f\u7684\u5de5\u4f5c\u539f\u7406\u3002","title":"\u7ed3\u8bba"},{"location":"17-3-order-of-construction-of-derived-classes/","tags":["inheritance"],"text":"Key Takeaway - \u5728 17.2 - C++\u7ee7\u627f\u57fa\u7840 \u4e2d\u6211\u4eec\u4e86\u89e3\u5230\uff0c\u7c7b\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f\u5176\u4ed6\u7c7b\u6765\u83b7\u53d6\u5176\u6210\u5458\u53d8\u91cf\u3002\u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u63a2\u8ba8\u5f53\u6d3e\u751f\u7c7b\u88ab \u5b9e\u4f8b\u5316 \u65f6\uff0c\u76f8\u5173\u7c7b\u7684\u6784\u9020\u987a\u5e8f\u662f\u5982\u4f55\u7684\u3002 \u9996\u5148\uff0c\u5b9a\u4e49\u51e0\u4e2a\u7c7b\u4ee5\u4fbf\u66f4\u597d\u5730\u8bf4\u660e\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 class Base { public : int m_id {}; Base ( int id = 0 ) : m_id { id } { } int getId () const { return m_id ; } }; class Derived : public Base { public : double m_cost {}; Derived ( double cost = 0.0 ) : m_cost { cost } { } double getCost () const { return m_cost ; } }; \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c Derived \u6d3e\u751f\u81ea Base \u3002 \u56e0\u4e3a Derived \u7ee7\u627f\u4e86 Base \u7684\u53d8\u91cf\u548c\u51fd\u6570\uff0c\u6240\u4ee5\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a Base \u7684\u8fd9\u90e8\u5206\u5185\u5bb9\u90fd\u88ab \u62f7\u8d1d \u5230\u4e86 Derived \u4e2d\u3002\u5176\u5b9e\u4e0d\u7136\uff0c\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06 Derived \u5206\u4e3a\u4e24\u90e8\u5206\u6765\u770b\uff0c\u4e00\u90e8\u5206\u662f Derived \uff0c\u53e6\u4e00\u90e8\u5206\u662f Base \u3002 \u5bf9\u4e8e\u4e00\u4e2a\u666e\u901a\u7684(\u975e\u6d3e\u751f\u7684)\u7c7b\u65f6\u7684\u5b9e\u4f8b\u5316\uff0c\u76f8\u6bd4\u4f60\u5df2\u7ecf\u5f88\u4e86\u89e3\u4e86\uff1a 1 2 3 4 5 6 int main () { Base base ; return 0 ; } Base \u4e0d\u662f\u4e00\u4e2a \u6d3e\u751f\u7c7b \uff0c\u56e0\u4e3a\u5b83\u6ca1\u6709\u7ee7\u627f\u4efb\u4f55\u5176\u4ed6\u7684\u7c7b\u3002C++\u4f1a\u9996\u5148\u4e3a Base \u5206\u914d\u5185\u5b58\uff0c\u7136\u540e\u8c03\u7528\u5b83\u7684 \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 \u8fdb\u884c\u521d\u59cb\u5316\u3002 \u63a5\u4e0b\u6765\uff0c\u5b9e\u4f8b\u5316\u4e00\u4e2a\u6d3e\u751f\u7c7b\u770b\u770b\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1a 1 2 3 4 5 6 int main () { Derived derived ; return 0 ; } \u5728\u4f60\u7f16\u5199\u5e76\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u4e0d\u4f1a\u6ce8\u610f\u5230\u4efb\u4f55\u7684\u4e0d\u540c\uff08\u76f8\u6bd4\u4e8e\u524d\u9762\u5b9e\u4f8b\u5316\u975e\u6d3e\u751f\u7c7b\u7684\u4f8b\u5b50\uff09\u3002\u4f46\u662f\u5728\u5e95\u5c42\uff0c\u5b83\u4eec\u6240\u8fdb\u884c\u7684\u5de5\u4f5c\u662f\u4e0d\u540c\u7684\u3002\u6b63\u5982\u4e0a\u9762\u63d0\u5230\u7684\u90a3\u6837\uff0c Derived \u5b9e\u9645\u4e0a\u5305\u542b\u4e24\u90e8\u5206\uff1a Base \u90e8\u5206\u548c Derived \u90e8\u5206\u3002C++ \u6784\u5efa\u6d3e\u751f\u5bf9\u8c61\u65f6\u662f\u5206\u9636\u6bb5\u8fdb\u884c\u7684\u3002\u9996\u5148\uff0c\u6700\u4e3a\u57fa\u7840\u7684\u57fa\u7c7b\uff08\u7ee7\u627f\u4f53\u7cfb\u7684\u6700\u9876\u90e8\uff09\u4f1a\u9996\u5148\u88ab\u6784\u5efa\u3002\u7136\u540e\u6bcf\u4e00\u4e2a\u5b50\u7c7b\u4f1a\u6309\u7167\u5c42\u6b21\u7ed3\u6784\u4f9d\u6b21\u6784\u5efa\uff0c\u77e5\u9053\u6700\u540e\u4e00\u4e2a\u5b50\u7c7b\uff08\u7ee7\u627f\u4f53\u7cfb\u6700\u5e95\u7aef\uff09\u6784\u5efa\u5b8c\u6210\u3002 \u56e0\u6b64\uff0c Derived \u88ab\u5b9e\u4f8b\u5316\u65f6\uff0c\u9996\u5148\u88ab\u6784\u9020\u7684\u662f Base \u90e8\u5206\uff08\u4f7f\u7528 Base \u7684\u6784\u9020\u51fd\u6570\uff09\u3002\u4e00\u65e6 Base \u90e8\u5206\u88ab\u521b\u5efa\uff0c Derived \u7684\u90e8\u5206\u5c31\u4f1a\u5f00\u59cb\u6784\u5efa\uff08\u4f7f\u7528 Derived \u7684\u6784\u9020\u51fd\u6570\uff09\u3002\u6b64\u65f6\uff0c\u5df2\u7ecf\u6ca1\u6709\u5176\u4ed6\u5b50\u7c7b\u9700\u8981\u8fdb\u4e00\u6b65\u6784\u5efa\u4e86\uff0c\u5b9e\u4f8b\u5316\u8fc7\u7a0b\u5230\u6b64\u7ed3\u675f\u3002 \u4f7f\u7528\u4e0b\u9762\u4ee3\u7801\u53ef\u4ee5\u5c55\u793a\u4e0a\u8ff0\u8fc7\u7a0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include class Base { public : int m_id {}; Base ( int id = 0 ) : m_id { id } { std :: cout << \"Base \\n \" ; } int getId () const { return m_id ; } }; class Derived : public Base { public : double m_cost {}; Derived ( double cost = 0.0 ) : m_cost { cost } { std :: cout << \"Derived \\n \" ; } double getCost () const { return m_cost ; } }; int main () { std :: cout << \"Instantiating Base \\n \" ; Base base ; std :: cout << \"Instantiating Derived \\n \" ; Derived derived ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 Instantiating Base Base Instantiating Derived Base Derived \u5982\u4f60\u6240\u89c1\uff0c\u5f53\u6211\u4eec\u6784\u9020 Derived \u65f6\uff0c Derived \u4e2d\u7684\u7684 Base \u90e8\u5206\u9996\u5148\u88ab\u6784\u9020\u3002\u9053\u7406\u663e\u800c\u6613\u89c1\uff0c\u4ece\u903b\u8f91\u4e0a\u8bb2\uff0c\u5b69\u5b50\u79bb\u5f00\u7236\u6bcd\u5c31\u4e0d\u80fd\u5b58\u5728\u3002\u8fd9\u4e5f\u662f\u4e00\u79cd\u5b89\u5168\u7684\u505a\u4e8b\u65b9\u5f0f\uff1a\u5b50\u7c7b\u7ecf\u5e38\u4f7f\u7528\u6765\u81ea\u7236\u7c7b\u7684\u53d8\u91cf\u548c\u51fd\u6570\uff0c\u4f46\u7236\u7c7b\u5bf9\u5b50\u7c7b\u4e00\u65e0\u6240\u77e5\u3002\u9996\u5148\u5b9e\u4f8b\u5316\u7236\u7c7b\u53ef\u4ee5\u786e\u4fdd\u5728\u521b\u5efa\u6d3e\u751f\u7c7b\u5e76\u51c6\u5907\u4f7f\u7528\u5b83\u4eec\u65f6\uff0c\u8fd9\u4e9b\u53d8\u91cf\u5df2\u7ecf\u521d\u59cb\u5316\u3002 \u7ee7\u627f\u94fe\u7684\u6784\u9020\u987a\u5e8f \u6709\u65f6\u5019\u4f1a\u51fa\u73b0\u7236\u7c7b\u662f\u5176\u4ed6\u7c7b\u7684\u5b50\u7c7b\u7684\u60c5\u51b5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include class A { public : A () { std :: cout << \"A \\n \" ; } }; class B : public A { public : B () { std :: cout << \"B \\n \" ; } }; class C : public B { public : C () { std :: cout << \"C \\n \" ; } }; class D : public C { public : D () { std :: cout << \"D \\n \" ; } }; \u6211\u4eec\u53ea\u9700\u8981\u8bb0\u5f97\uff0cC++ \u603b\u662f\u9996\u5148\u6784\u5efa\u201d\u7b2c\u4e00\u4e2a\u201c\u6216\u201d\u6700\u57fa\u672c\u201c\u7684\u7c7b\u3002\u7136\u540e\u5b83\u4f1a\u6cbf\u7740\u7ee7\u627f\u6811\u9010\u7ea7\u6784\u9020\u5404\u4e2a\u6d3e\u751f\u7c7b\u3002 \u4e0b\u9762\u4ee3\u7801\u53ef\u4ee5\u5c55\u793a\u6d3e\u751f\u7c7b\u5b9e\u4f8b\u5316\u65f6\u7684\u6784\u9020\u987a\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int main () { std :: cout << \"Constructing A: \\n \" ; A a ; std :: cout << \"Constructing B: \\n \" ; B b ; std :: cout << \"Constructing C: \\n \" ; C c ; std :: cout << \"Constructing D: \\n \" ; D d ; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Constructing A: A Constructing B: A B Constructing C: A B C Constructing D: A B C D \u7ed3\u8bba C++\u662f\u5206\u9636\u6bb5\u6784\u9020\u6d3e\u751f\u7c7b\u7684\uff0c\u4ece\u6700\u57fa\u672c\u7684\u7c7b(\u5728\u7ee7\u627f\u6811\u7684\u9876\u90e8)\u5f00\u59cb\uff0c\u5230\u6700\u540e\u4e00\u4e2a\u5b50\u7c7b(\u5728\u7ee7\u627f\u6811\u7684\u5e95\u90e8)\u7ed3\u675f\u3002\u5728\u6784\u9020\u6bcf\u4e2a\u7c7b\u65f6\uff0c\u8c03\u7528\u8be5\u7c7b\u7684\u9002\u5f53\u6784\u9020\u51fd\u6570\u6765\u521d\u59cb\u5316\u5c5e\u4e8e\u8be5\u7c7b\u7684\u90e8\u5206\u3002 \u6ce8\u610f\uff0c\u672c\u8282\u4e2d\u7684\u793a\u4f8b\u7c7b\u90fd\u4f7f\u7528\u57fa\u7c7b\u9ed8\u8ba4\u6784\u9020\u51fd\u6570(\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1)\u3002\u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u7814\u7a76\u6784\u9020\u51fd\u6570\u5728\u6784\u9020\u6d3e\u751f\u7c7b\u8fc7\u7a0b\u4e2d\u7684\u4f5c\u7528(\u5305\u62ec\u5982\u4f55\u663e\u5f0f\u9009\u62e9\u6d3e\u751f\u7c7b\u8981\u4f7f\u7528\u7684\u57fa\u7c7b\u6784\u9020\u51fd\u6570)\u3002","title":"17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f"},{"location":"17-3-order-of-construction-of-derived-classes/#_1","text":"\u6709\u65f6\u5019\u4f1a\u51fa\u73b0\u7236\u7c7b\u662f\u5176\u4ed6\u7c7b\u7684\u5b50\u7c7b\u7684\u60c5\u51b5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include class A { public : A () { std :: cout << \"A \\n \" ; } }; class B : public A { public : B () { std :: cout << \"B \\n \" ; } }; class C : public B { public : C () { std :: cout << \"C \\n \" ; } }; class D : public C { public : D () { std :: cout << \"D \\n \" ; } }; \u6211\u4eec\u53ea\u9700\u8981\u8bb0\u5f97\uff0cC++ \u603b\u662f\u9996\u5148\u6784\u5efa\u201d\u7b2c\u4e00\u4e2a\u201c\u6216\u201d\u6700\u57fa\u672c\u201c\u7684\u7c7b\u3002\u7136\u540e\u5b83\u4f1a\u6cbf\u7740\u7ee7\u627f\u6811\u9010\u7ea7\u6784\u9020\u5404\u4e2a\u6d3e\u751f\u7c7b\u3002 \u4e0b\u9762\u4ee3\u7801\u53ef\u4ee5\u5c55\u793a\u6d3e\u751f\u7c7b\u5b9e\u4f8b\u5316\u65f6\u7684\u6784\u9020\u987a\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int main () { std :: cout << \"Constructing A: \\n \" ; A a ; std :: cout << \"Constructing B: \\n \" ; B b ; std :: cout << \"Constructing C: \\n \" ; C c ; std :: cout << \"Constructing D: \\n \" ; D d ; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Constructing A: A Constructing B: A B Constructing C: A B C Constructing D: A B C D","title":"\u7ee7\u627f\u94fe\u7684\u6784\u9020\u987a\u5e8f"},{"location":"17-3-order-of-construction-of-derived-classes/#_2","text":"C++\u662f\u5206\u9636\u6bb5\u6784\u9020\u6d3e\u751f\u7c7b\u7684\uff0c\u4ece\u6700\u57fa\u672c\u7684\u7c7b(\u5728\u7ee7\u627f\u6811\u7684\u9876\u90e8)\u5f00\u59cb\uff0c\u5230\u6700\u540e\u4e00\u4e2a\u5b50\u7c7b(\u5728\u7ee7\u627f\u6811\u7684\u5e95\u90e8)\u7ed3\u675f\u3002\u5728\u6784\u9020\u6bcf\u4e2a\u7c7b\u65f6\uff0c\u8c03\u7528\u8be5\u7c7b\u7684\u9002\u5f53\u6784\u9020\u51fd\u6570\u6765\u521d\u59cb\u5316\u5c5e\u4e8e\u8be5\u7c7b\u7684\u90e8\u5206\u3002 \u6ce8\u610f\uff0c\u672c\u8282\u4e2d\u7684\u793a\u4f8b\u7c7b\u90fd\u4f7f\u7528\u57fa\u7c7b\u9ed8\u8ba4\u6784\u9020\u51fd\u6570(\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1)\u3002\u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u7814\u7a76\u6784\u9020\u51fd\u6570\u5728\u6784\u9020\u6d3e\u751f\u7c7b\u8fc7\u7a0b\u4e2d\u7684\u4f5c\u7528(\u5305\u62ec\u5982\u4f55\u663e\u5f0f\u9009\u62e9\u6d3e\u751f\u7c7b\u8981\u4f7f\u7528\u7684\u57fa\u7c7b\u6784\u9020\u51fd\u6570)\u3002","title":"\u7ed3\u8bba"},{"location":"17-4-constructors-and-initialization-of-derived-classes/","tags":["inheritance"],"text":"Key Takeaway - \u5728\u524d\u9762\u4e24\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u63a2\u8ba8\u4e86C++\u4e2d\u7ee7\u627f\u7684\u4e00\u4e9b\u57fa\u7840\u77e5\u8bc6\u4ee5\u53ca\u6d3e\u751f\u7c7b\u521d\u59cb\u5316\u7684\u987a\u5e8f\u3002\u672c\u8282\u8bfe\uff0c\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u4e86\u89e3\u6784\u9020\u51fd\u6570\u5728\u6d3e\u751f\u7c7b\u521d\u59cb\u5316\u4e2d\u7684\u4f5c\u7528\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u4f7f\u7528\u4e0a\u4e00\u8bfe\u4e2d\u5f00\u53d1\u7684 Base \u548c Derived \u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 class Base { public : int m_id {}; Base ( int id = 0 ) : m_id { id } { } int getId () const { return m_id ; } }; class Derived : public Base { public : double m_cost {}; Derived ( double cost = 0.0 ) : m_cost { cost } { } double getCost () const { return m_cost ; } }; \u5bf9\u4e8e\u975e\u6d3e\u751f\u7c7b\u6765\u8bf4\uff0c\u6784\u9020\u51fd\u6570\u53ea\u9700\u8981\u5173\u5fc3\u5b83\u81ea\u5df1\u7684\u6210\u5458\u5373\u53ef\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e Base \u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u50cf\u8fd9\u6837\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff1a 1 2 3 4 5 6 int main () { Base base { 5 }; // use Base(int) constructor return 0 ; } \u4e0b\u9762\u662f\u5b9e\u4f8b\u5316base\u65f6\u5b9e\u9645\u53d1\u751f\u7684\u60c5\u51b5\uff1a \u4e3a base \u5206\u914d\u5185\u5b58\uff1b \u8c03\u7528\u5408\u9002\u7684\u6784\u9020\u51fd\u6570\uff1b \u4f7f\u7528 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u6765\u521d\u59cb\u5316\u53d8\u91cf\uff1b \u6784\u9020\u51fd\u6570\u6267\u884c\u5176\u51fd\u6570\u4f53\u5185\u8bed\u53e5\uff1b \u63a7\u5236\u6743\u8fd4\u8fd8\u7ed9\u8c03\u7528\u8005\u3002 \u5f88\u7b80\u5355\u3002 \u5bf9\u4e8e\u6d3e\u751f\u7c7b\uff0c\u4e8b\u60c5\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\uff1a 1 2 3 4 5 6 int main () { Derived derived { 1.3 }; // use Derived(double) constructor return 0 ; } \u5728\u6d3e\u751f\u7c7b\u5b9e\u4f8b\u5316\u65f6\u4f1a\u6709\u5982\u4e0b\u6b65\u9aa4\uff1a \u5206\u914d\u5185\u5b58(\u6ee1\u8db3 Base \u548c Derived \u7684\u9700\u8981)\uff1b \u8c03\u7528 Derived \u7684\u6784\u9020\u51fd\u6570\uff1b Base \u5bf9\u8c61\u4f1a\u4f7f\u7528\u5408\u9002\u7684 Base \u6784\u9020\u51fd\u6570\u9996\u5148\u521d\u59cb\u5316\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u6784\u9020\u51fd\u6570\uff0c\u5219\u8c03\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff1b \u4f7f\u7528 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u521d\u59cb\u5316\u53d8\u91cf\uff1b \u6784\u9020\u51fd\u6570\u6267\u884c\u5176\u51fd\u6570\u4f53\u5185\u8bed\u53e5\uff1b \u63a7\u5236\u6743\u8fd4\u8fd8\u7ed9\u8c03\u7528\u8005\u3002 \u4e24\u4e2a\u4f8b\u5b50\u4e2d\u7684\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\uff0c\u5728 Derived \u7684\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u505a\u4efb\u4f55\u4e8b\u4e4b\u524d\uff0c Base \u7684\u6784\u9020\u51fd\u6570\u9996\u5148\u4f1a\u88ab\u8c03\u7528\uff0c\u5b83\u4f1a\u521d\u59cb\u5316\u8be5\u5bf9\u8c61\u7684 Base \u90e8\u5206\uff0c\u7136\u540e\u5c06\u63a7\u5236\u6743\u8fd4\u8fd8\u7ed9 Derived \u6784\u9020\u51fd\u6570\uff0c\u7136\u540e Derived \u624d\u80fd\u53bb\u5b8c\u6210\u5b83\u81ea\u5df1\u7684\u5de5\u4f5c\u3002 \u521d\u59cb\u5316\u57fa\u7c7b\u6210\u5458 \u6211\u4eec\u6240\u7f16\u5199\u7684\u6d3e\u751f\u7c7b\u76ee\u524d\u7684\u7f3a\u70b9\u4e4b\u4e00\u662f\uff0c\u5728\u521b\u5efa\u6d3e\u751f\u5bf9\u8c61\u65f6\u6ca1\u6709\u529e\u6cd5\u521d\u59cb\u5316 m_id \u3002\u5982\u679c\u6211\u4eec\u60f3\u5728\u521b\u5efa\u6d3e\u751f\u5bf9\u8c61\u65f6\u540c\u65f6\u8bbe\u7f6e m_cost (\u6765\u81ea\u5bf9\u8c61\u7684 Derived \u90e8\u5206)\u548c m_id (\u6765\u81ea\u5bf9\u8c61\u7684 Base \u90e8\u5206)\uff0c\u8be5\u600e\u4e48\u529e? \u65b0\u624b\u7a0b\u5e8f\u5458\u4f1a\u5c1d\u8bd5\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Derived : public Base { public : double m_cost {}; Derived ( double cost = 0.0 , int id = 0 ) // does not work : m_cost { cost } , m_id { id } { } double getCost () const { return m_cost ; } }; \u60f3\u6cd5\u5f88\u597d\u4f46\u5e76\u4e0d\u5b8c\u5168\u6b63\u786e\u3002\u6211\u4eec\u7684\u786e\u9700\u8981\u4e3a\u6784\u9020\u51fd\u6570\u6dfb\u52a0\u989d\u5916\u7684\u53c2\u6570\uff0c\u5426\u5219C++\u65e0\u4ece\u77e5\u6653\u6211\u4eec\u5e0c\u671b\u7528\u4ec0\u4e48\u503c\u6765\u521d\u59cb\u5316 m_id \u3002 \u4f46\u662f\uff0cC++\u4e0d\u5141\u8bb8\u6211\u4eec\u5728\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u7684\u521d\u59cb\u5316\u7ee7\u627f\u6765\u7684\u6210\u5458\u53d8\u91cf\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6210\u5458\u53d8\u91cf\u7684\u503c\u53ea\u80fd\u5728\u5b83\u6240\u5c5e\u7c7b\u7684\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u8bbe\u7f6e\u3002 \u4e3a\u4ec0\u4e48C++\u8981\u8fd9\u6837\u505a\uff1f\u7b54\u6848\u4e0econst\u53d8\u91cf\u548c\u5f15\u7528\u53d8\u91cf\u6709\u5173\u3002\u8003\u8651\u4e00\u4e0b\u5982\u679c m_id \u662f const \u4f1a\u53d1\u751f\u4ec0\u4e48\u3002\u56e0\u4e3aconst\u53d8\u91cf\u5fc5\u987b\u5728\u521b\u5efa\u65f6\u7528\u4e00\u4e2a\u503c\u521d\u59cb\u5316\uff0c\u6240\u4ee5\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u5fc5\u987b\u5728\u521b\u5efa\u53d8\u91cf\u65f6\u8bbe\u7f6e\u5b83\u7684\u503c\u3002\u4f46\u662f\uff0c\u5f53\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u5b8c\u6210\u65f6\uff0c\u5c06\u6267\u884c\u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u3002\u7136\u540e\u6bcf\u4e2a\u6d3e\u751f\u7c7b\u90fd\u6709\u673a\u4f1a\u521d\u59cb\u5316\u8be5\u53d8\u91cf\uff0c\u53ef\u80fd\u4f1a\u6539\u53d8\u5b83\u7684\u503c!\u901a\u8fc7\u5c06\u53d8\u91cf\u7684\u521d\u59cb\u5316\u9650\u5236\u5728\u8fd9\u4e9b\u53d8\u91cf\u6240\u5c5e\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4e2d\uff0cC++\u9700\u8981\u786e\u4fdd\u6240\u6709\u53d8\u91cf\u53ea\u521d\u59cb\u5316\u4e00\u6b21\u3002 \u6700\u7ec8\u7684\u7ed3\u679c\u662f\u4e0a\u9762\u7684\u793a\u4f8b\u4e0d\u8d77\u4f5c\u7528\uff0c\u56e0\u4e3a m_id \u662f\u4ece Base \u7ee7\u627f\u7684\uff0c\u5e76\u4e14\u53ea\u6709\u975e\u7ee7\u627f\u7684\u53d8\u91cf\u53ef\u4ee5\u5728\u6210\u5458\u521d\u59cb\u5316\u5668\u5217\u8868\u4e2d\u521d\u59cb\u5316\u3002 \u4f46\u662f\uff0c\u7ee7\u627f\u7684\u53d8\u91cf\u4ecd\u7136\u53ef\u4ee5\u5728\u6784\u9020\u51fd\u6570\u4f53\u4e2d\u4f7f\u7528\u8d4b\u503c\u64cd\u4f5c\u66f4\u6539\u5176\u503c\u3002\u56e0\u6b64\uff0c\u65b0\u7a0b\u5e8f\u5458\u901a\u5e38\u4e5f\u4f1a\u8fd9\u6837\u505a: 1 2 3 4 5 6 7 8 9 10 11 12 13 class Derived : public Base { public : double m_cost {}; Derived ( double cost = 0.0 , int id = 0 ) : m_cost { cost } { m_id = id ; } double getCost () const { return m_cost ; } }; \u867d\u7136\u8fd9\u5728\u672c\u4f8b\u4e2d\u5b9e\u9645\u4e0a\u662f\u53ef\u884c\u7684\uff0c\u4f46\u5982\u679c m_id \u662f const \u6216\u5f15\u7528\uff0c\u5219\u4e0d\u53ef\u884c(\u56e0\u4e3a const \u503c\u548c\u5f15\u7528\u5fc5\u987b\u5728\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u521d\u59cb\u5316)\u3002\u5b83\u7684\u6548\u7387\u4e5f\u5f88\u4f4e\uff0c\u56e0\u4e3a m_id \u88ab\u5206\u914d\u4e86\u4e24\u6b21\u503c\uff1a\u4e00\u6b21\u662f\u5728\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\uff0c\u7136\u540e\u662f\u5728\u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u7684\u4e3b\u4f53\u4e2d\u3002\u6700\u540e\uff0c\u5982\u679c\u57fa\u7c7b\u5728\u6784\u9020\u8fc7\u7a0b\u4e2d\u9700\u8981\u8bbf\u95ee\u8fd9\u4e2a\u503c\u600e\u4e48\u529e\uff1f\u5b83\u6ca1\u6709\u529e\u6cd5\u8bbf\u95ee\u5b83\uff0c\u56e0\u4e3a\u5b83\u662f\u5728\u6267\u884c Derived \u6784\u9020\u51fd\u6570\u4e4b\u524d\u624d\u8bbe\u7f6e\u7684(\u8fd9\u57fa\u672c\u4e0a\u662f\u5728\u6700\u540e\u6267\u884c)\u3002 \u90a3\u4e48\uff0c\u6211\u4eec\u5e94\u8be5\u5982\u4f55\u5b50\u521b\u5efa Derived \u7c7b\u5bf9\u8c61\u65f6\u6b63\u786e\u521d\u59cb\u5316 m_id \u5462\uff1f \u5728\u4e0a\u9762\u7684\u8fd9\u4e9b\u4f8b\u5b50\u4e2d\uff0c\u5f53\u6211\u4eec\u521d\u59cb\u5316 Derived \u7c7b\u7684\u5bf9\u8c61\u65f6\uff0c Base \u7c7b\u7684\u90e8\u5206\u90fd\u662f\u901a\u8fc7\u5b83\u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u6765\u521b\u5efa\u7684\u3002\u4e3a\u4ec0\u4e48\u5b83\u603b\u662f\u4f1a\u8c03\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u5462\uff1f\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u8ba9\u4ed6\u4e0d\u8981\u8fd9\u4e48\u505a\u554a\uff01 \u6240\u5e78\uff0cC++ \u5141\u8bb8\u6211\u4eec\u663e\u5f0f\u5730\u6307\u5b9a\u521b\u5efa Base \u7c7b\u65f6\u5e94\u8be5\u4f7f\u7528\u7684\u6784\u9020\u51fd\u6570\uff01\u6211\u4eec\u53ea\u9700\u8981\u5728\u6d3e\u751f\u7c7b\u7684 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u4e2d\u8c03\u7528\u6240\u9700\u7684 Base \u7684\u6784\u9020\u51fd\u6570\u5c31\u53ef\u4ee5\u4e86\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 class Derived : public Base { public : double m_cost {}; Derived ( double cost = 0.0 , int id = 0 ) : Base { id } // Call Base(int) constructor with value id! , m_cost { cost } { } double getCost () const { return m_cost ; } }; \u518d\u6b21\u6267\u884c\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { Derived derived { 1.3 , 5 }; // use Derived(double, int) constructor std :: cout << \"Id: \" << derived . getId () << '\\n' ; std :: cout << \"Cost: \" << derived . getCost () << '\\n' ; return 0 ; } \u57fa\u7c7b\u7684\u6784\u9020\u51fd\u6570 Base(int) \u5c06\u4f1a\u88ab\u7528\u6765\u521d\u59cb\u5316\u6210\u5458 m_id \uff085\uff09\u7136\u540e\u6d3e\u751f\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4f1a\u88ab\u7528\u6765\u521d\u59cb\u5316 m_cost \u5230 1.3 \uff01 \u56e0\u6b64\uff0c\u7a0b\u5e8f\u4f1a\u6253\u5370\uff1a 1 2 Id: 5 Cost: 1.3 \u5177\u4f53\u6765\u8bf4\uff1a \u5185\u5b58\u5206\u914d\uff1b \u6784\u9020\u51fd\u6570 Derived(double, int) \u88ab\u8c03\u7528\uff0c cost = 1.3 \uff0c id = 5 \u3002 \u7f16\u8bd1\u5668\u67e5\u770b\u4f60\u662f\u5426\u6307\u5b9a\u4e86 Base \u7c7b\u7684\u6784\u9020\u51fd\u6570\u3002\u8fd9\u91cc\u6211\u4eec\u6307\u5b9a\u4e86\uff0c\u6240\u4ee5\u5b83\u4f1a\u8c03\u7528 Base(int) \u4e14 id = 5 \u3002 \u57fa\u7c7b\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u5c06 m_id \u8bbe\u7f6e\u4e3a5\uff1b \u57fa\u7c7b\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u6267\u884c\uff0c\u4ec0\u4e48\u90fd\u6ca1\u505a\uff1b \u57fa\u7c7b\u6784\u9020\u51fd\u6570\u8fd4\u56de\uff1b \u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u5c06 m_cost \u8bbe\u7f6e\u4e3a 1.3\uff1b \u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u6267\u884c\uff0c\u4ec0\u4e48\u90fd\u6ca1\u505a\uff1b \u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u8fd4\u56de\u3002 \u770b\u8d77\u6765\u6709\u70b9\u590d\u6742\uff0c\u4f46\u5b9e\u9645\u4e0a\u975e\u5e38\u7b80\u5355\u3002\u6240\u53d1\u751f\u7684\u4e00\u5207\u5c31\u662f Derived \u6784\u9020\u51fd\u6570\u8c03\u7528\u6307\u5b9a\u7684 Base \u6784\u9020\u51fd\u6570\u6765\u521d\u59cb\u5316\u5bf9\u8c61\u7684 Base \u90e8\u5206\u3002\u56e0\u4e3a m_id \u4f4d\u4e8e\u5bf9\u8c61\u7684 Base \u90e8\u5206\uff0c\u6240\u4ee5 Base \u6784\u9020\u51fd\u6570\u662f\u552f\u4e00\u53ef\u4ee5\u521d\u59cb\u5316\u8be5\u503c\u7684\u6784\u9020\u51fd\u6570\u3002 \u6ce8\u610f\uff0cBase\u6784\u9020\u51fd\u6570\u5728 Derived \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u7684\u4ec0\u4e48\u4f4d\u7f6e\u88ab\u8c03\u7528\u5e76\u4e0d\u91cd\u8981\u2014\u2014\u5b83\u603b\u662f\u9996\u5148\u6267\u884c\u3002 \u5c06\u6210\u5458\u8bbe\u4e3a\u79c1\u6709 \u65e2\u7136\u5df2\u7ecf\u77e5\u9053\u4e86\u5982\u4f55\u521d\u59cb\u5316\u57fa\u7c7b\u6210\u5458\uff0c\u5c31\u6ca1\u6709\u5fc5\u8981\u5c06\u6210\u5458\u53d8\u91cf\u4fdd\u6301\u4e3a public \u3002\u6211\u4eec\u518d\u6b21\u5c06\u6210\u5458\u53d8\u91cf\u8bbe\u4e3a\u79c1\u6709\uff0c\u56e0\u4e3a\u5b83\u4eec\u5e94\u8be5\u662f\u79c1\u6709\u7684\u3002 \u5feb\u901f\u56de\u987e\u4e00\u4e0b\uff0c\u516c\u5171\u6210\u5458\u53ef\u4ee5\u88ab\u4efb\u4f55\u4eba\u8bbf\u95ee\u3002\u79c1\u6709\u6210\u5458\u53ea\u80fd\u7531\u540c\u4e00\u7c7b\u7684\u6210\u5458\u51fd\u6570\u8bbf\u95ee\u3002\u6ce8\u610f\uff0c \u8fd9\u610f\u5473\u7740\u6d3e\u751f\u7c7b\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u57fa\u7c7b\u7684\u79c1\u6709\u6210\u5458 \uff01\u6d3e\u751f\u7c7b\u5c06\u9700\u8981\u4f7f\u7528\u8bbf\u95ee\u51fd\u6570\u6765\u8bbf\u95ee\u57fa\u7c7b\u7684\u79c1\u6709\u6210\u5458\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include class Base { private : // our member is now private int m_id {}; public : Base ( int id = 0 ) : m_id { id } { } int getId () const { return m_id ; } }; class Derived : public Base { private : // our member is now private double m_cost ; public : Derived ( double cost = 0.0 , int id = 0 ) : Base { id } // Call Base(int) constructor with value id! , m_cost { cost } { } double getCost () const { return m_cost ; } }; int main () { Derived derived { 1.3 , 5 }; // use Derived(double, int) constructor std :: cout << \"Id: \" << derived . getId () << '\\n' ; std :: cout << \"Cost: \" << derived . getCost () << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06 m_id \u548c m_cost \u8bbe\u4e3a\u79c1\u6709\u3002\u8fd9\u4e48\u505a\u5b8c\u5168\u6ca1\u6709\u95ee\u9898\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u76f8\u5173\u7684\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u8fd9\u4e9b\u6210\u5458\uff0c\u5e76\u901a\u8fc7\u516c\u6709\u7684 \u6210\u5458\u8bbf\u95ee\u51fd\u6570 \u6765\u8bbf\u95ee\u5b83\u4eec\u3002 \u6253\u5370\u7ed3\u679c\u5982\u6211\u4eec\u6240\u60f3\u7684\u90a3\u6837\uff1a 1 2 Id: 5 Cost: 1.3 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u66f4\u591a\u5730\u8ba8\u8bba\u8bbf\u95ee\u8bf4\u660e\u7b26\u3002 \u53e6\u5916\u4e00\u4e2a\u4f8b\u5b50 \u518d\u770b\u770b\u4e4b\u524d\u6211\u4eec\u4f7f\u7528\u8fc7\u7684\u4e00\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include #include class Person { public : std :: string m_name ; int m_age {}; Person ( const std :: string_view name = \"\" , int age = 0 ) : m_name { name }, m_age { age } { } const std :: string & getName () const { return m_name ; } int getAge () const { return m_age ; } }; // BaseballPlayer publicly inheriting Person class BaseballPlayer : public Person { public : double m_battingAverage {}; int m_homeRuns {}; BaseballPlayer ( double battingAverage = 0.0 , int homeRuns = 0 ) : m_battingAverage { battingAverage }, m_homeRuns { homeRuns } { } }; \u5728\u4e4b\u524d\u7684\u4f8b\u5b50\u4e2d\uff0c BaseballPlayer \u53ea\u4f1a\u521d\u59cb\u5316\u5b83\u81ea\u5df1\u7684\u6210\u5458\uff0c\u4e5f\u5e76\u6ca1\u6709\u6307\u5b9a Person \u7684\u6784\u9020\u51fd\u6570\u3002\u8fd9\u610f\u5473\u7740 BaseballPlayer \u5728\u521b\u5efa\u65f6\uff0c\u8c03\u7528\u7684\u90fd\u662f Person \u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u5b83\u4f1a\u5c06\u540d\u5b57\u521d\u59cb\u5316\u4e3a\u7a7a\u767d\u5e76\u5c06\u5e74\u9f84\u521d\u59cb\u5316\u4e3a0\u3002\u56e0\u4e3a\uff0c\u56e0\u4e3a\u5728\u521b\u5efa BaseballPlayer \u65f6\u7ed9\u5b83\u4eec\u4e00\u4e2a\u540d\u79f0\u548c\u5e74\u9f84\u662f\u6709\u610f\u4e49\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u5e94\u8be5\u4fee\u6539\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u6765\u6dfb\u52a0\u8fd9\u4e9b\u53c2\u6570\u3002 \u66f4\u65b0\u4ee3\u7801\uff0c\u8ba9\u7c7b\u4f7f\u7528\u79c1\u6709\u6210\u5458\uff0c\u540c\u65f6\u8ba9 BaseballPlayer \u7c7b\u8c03\u7528\u9002\u5f53\u7684 Person \u6784\u9020\u51fd\u6570\u6765\u521d\u59cb\u5316\u7ee7\u627f\u7684 Person \u6210\u5458\u53d8\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include #include class Person { private : std :: string m_name ; int m_age {}; public : Person ( const std :: string_view name = \"\" , int age = 0 ) : m_name { name }, m_age { age } { } const std :: string & getName () const { return m_name ; } int getAge () const { return m_age ; } }; // BaseballPlayer \u516c\u5f00\u7ee7\u627f Person class BaseballPlayer : public Person { private : double m_battingAverage {}; int m_homeRuns {}; public : BaseballPlayer ( const std :: string_view name = \"\" , int age = 0 , double battingAverage = 0.0 , int homeRuns = 0 ) : Person { name , age } // \u8c03\u7528 Person(const std::string_view, int) \u6765\u521d\u59cb\u5316\u76f8\u5e94\u7684\u6210\u5458 , m_battingAverage { battingAverage }, m_homeRuns { homeRuns } { } double getBattingAverage () const { return m_battingAverage ; } int getHomeRuns () const { return m_homeRuns ; } }; \u73b0\u5728\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { BaseballPlayer pedro { \"Pedro Cerrano\" , 32 , 0.342 , 42 }; std :: cout << pedro . getName () << '\\n' ; std :: cout << pedro . getAge () << '\\n' ; std :: cout << pedro . getBattingAverage () << '\\n' ; std :: cout << pedro . getHomeRuns () << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 2 3 4 Pedro Cerrano 32 0.342 42 \u53ef\u4ee5\u770b\u5230\uff0c\u57fa\u7c7b\u7684\u540d\u79f0\u548c\u5e74\u9f84\u5df2\u6b63\u786e\u521d\u59cb\u5316\uff0c\u6d3e\u751f\u7c7b\u7684\u672c\u5792\u6253\u6570\u548c\u51fb\u7403\u7387\u4e5f\u5df2\u521d\u59cb\u5316\u3002 \u7ee7\u627f\u94fe \u7ee7\u627f\u94fe\u4e2d\u7684\u7c7b\u4ee5\u5b8c\u5168\u76f8\u540c\u7684\u65b9\u5f0f\u5de5\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include class A { public : A ( int a ) { std :: cout << \"A: \" << a << '\\n' ; } }; class B : public A { public : B ( int a , double b ) : A { a } { std :: cout << \"B: \" << b << '\\n' ; } }; class C : public B { public : C ( int a , double b , char c ) : B { a , b } { std :: cout << \"C: \" << c << '\\n' ; } }; int main () { C c { 5 , 4.3 , 'R' }; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7c7bC\u662f\u7531\u7c7bB\u6d3e\u751f\u800c\u6765\u7684\uff0c\u800c\u7c7bB\u53c8\u662f\u7531\u7c7bA\u6d3e\u751f\u800c\u6765\u7684\u3002\u90a3\u4e48\u5f53\u6211\u4eec\u5b9e\u4f8b\u5316\u7c7bC\u7684\u5bf9\u8c61\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462? \u9996\u5148 main() \u4f1a\u8c03\u7528 C(int, double, char) \u3002C \u7684\u6784\u9020\u51fd\u6570\u4f1a\u8c03\u7528 B(int, double) \u3002B\u7684\u6784\u9020\u51fd\u6570\u8c03\u7528 A(int) \u3002\u56e0\u4e3aA\u6ca1\u6709\u7ee7\u627f\u4efb\u4f55\u7c7b\uff0c\u6240\u4ee5\u5b83\u4f1a\u88ab\u7b2c\u4e00\u4e2a\u6784\u9020\u3002A \u88ab\u6784\u9020\u540e\u6253\u5370\u4e86 5\uff0c\u7136\u540e\u63a7\u5236\u6743\u8fd4\u56de\u7ed9B\u3002B\u88ab\u6784\u9020\u65f6\u6253\u5370\u503c 4.3\uff0c\u7136\u540e\u8fd4\u56de\u63a7\u5236\u6743\u7ed9C\u3002C\u88ab\u6784\u9020\u65f6\u6253\u5370\u2018R\u2019\uff0c\u7136\u540e\u5c06\u63a7\u5236\u6743\u8fd4\u56de\u7ed9 main() \u3002\u6784\u9020\u7ed3\u675f\uff01 \u56e0\u6b64\u7a0b\u5e8f\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 A: 5 B: 4.3 C: R \u503c\u5f97\u4e00\u63d0\u7684\u662f\uff0c\u6784\u9020\u51fd\u6570\u53ea\u80fd\u8c03\u7528 \u76f4\u63a5 \u7236\u7c7b\uff08\u57fa\u7c7b\uff09\u4e2d\u7684\u6784\u9020\u51fd\u6570\u3002\u56e0\u6b64\uff0cC\u6784\u9020\u51fd\u6570\u4e0d\u80fd\u76f4\u63a5\u8c03\u7528\u6216\u5c06\u53c2\u6570\u4f20\u9012\u7ed9A\u6784\u9020\u51fd\u6570\u3002C\u6784\u9020\u51fd\u6570\u53ea\u80fd\u8c03\u7528B\u6784\u9020\u51fd\u6570(B\u6784\u9020\u51fd\u6570\u8d1f\u8d23\u8c03\u7528A\u6784\u9020\u51fd\u6570)\u3002 \u6790\u6784\u51fd\u6570 \u5f53\u6d3e\u751f\u7c7b\u88ab\u9500\u6bc1\u65f6\uff0c\u6bcf\u4e2a\u6790\u6784\u51fd\u6570\u5c06\u6309\u6784\u9020\u7684 \u9006\u987a\u5e8f\u8c03\u7528 \u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53C\u88ab\u9500\u6bc1\u65f6\uff0c\u9996\u5148\u8c03\u7528C\u6790\u6784\u51fd\u6570\uff0c\u7136\u540e\u662fB\u6790\u6784\u51fd\u6570\uff0c\u7136\u540e\u662fA\u6790\u6784\u51fd\u6570\u3002 \u5c0f\u7ed3 \u5728\u6784\u9020\u6d3e\u751f\u7c7b\u65f6\uff0c\u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u8d1f\u8d23\u786e\u5b9a\u8c03\u7528\u54ea\u4e2a\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u57fa\u7c7b\u6784\u9020\u51fd\u6570\uff0c\u5c06\u4f7f\u7528\u9ed8\u8ba4\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u627e\u4e0d\u5230\u7f3a\u7701\u57fa\u7c7b\u6784\u9020\u51fd\u6570(\u6216\u7f3a\u7701\u521b\u5efa\u57fa\u7c7b\u6784\u9020\u51fd\u6570)\uff0c\u7f16\u8bd1\u5668\u5c06\u62a5\u9519\u3002\u7136\u540e\u6309\u7167\u4ece\u6700\u57fa\u7c7b\u5230\u6700\u6d3e\u751f\u7c7b\u7684\u987a\u5e8f\u6784\u9020\u7c7b\u3002 \u81f3\u6b64\uff0c\u60a8\u5df2\u7ecf\u8db3\u591f\u4e86\u89e3C++\u7ee7\u627f\uff0c\u53ef\u4ee5\u521b\u5efa\u81ea\u5df1\u7684\u7ee7\u627f\u7c7b\u4e86!","title":"17.4 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u548c\u521d\u59cb\u5316"},{"location":"17-4-constructors-and-initialization-of-derived-classes/#_1","text":"\u6211\u4eec\u6240\u7f16\u5199\u7684\u6d3e\u751f\u7c7b\u76ee\u524d\u7684\u7f3a\u70b9\u4e4b\u4e00\u662f\uff0c\u5728\u521b\u5efa\u6d3e\u751f\u5bf9\u8c61\u65f6\u6ca1\u6709\u529e\u6cd5\u521d\u59cb\u5316 m_id \u3002\u5982\u679c\u6211\u4eec\u60f3\u5728\u521b\u5efa\u6d3e\u751f\u5bf9\u8c61\u65f6\u540c\u65f6\u8bbe\u7f6e m_cost (\u6765\u81ea\u5bf9\u8c61\u7684 Derived \u90e8\u5206)\u548c m_id (\u6765\u81ea\u5bf9\u8c61\u7684 Base \u90e8\u5206)\uff0c\u8be5\u600e\u4e48\u529e? \u65b0\u624b\u7a0b\u5e8f\u5458\u4f1a\u5c1d\u8bd5\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Derived : public Base { public : double m_cost {}; Derived ( double cost = 0.0 , int id = 0 ) // does not work : m_cost { cost } , m_id { id } { } double getCost () const { return m_cost ; } }; \u60f3\u6cd5\u5f88\u597d\u4f46\u5e76\u4e0d\u5b8c\u5168\u6b63\u786e\u3002\u6211\u4eec\u7684\u786e\u9700\u8981\u4e3a\u6784\u9020\u51fd\u6570\u6dfb\u52a0\u989d\u5916\u7684\u53c2\u6570\uff0c\u5426\u5219C++\u65e0\u4ece\u77e5\u6653\u6211\u4eec\u5e0c\u671b\u7528\u4ec0\u4e48\u503c\u6765\u521d\u59cb\u5316 m_id \u3002 \u4f46\u662f\uff0cC++\u4e0d\u5141\u8bb8\u6211\u4eec\u5728\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u7684\u521d\u59cb\u5316\u7ee7\u627f\u6765\u7684\u6210\u5458\u53d8\u91cf\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6210\u5458\u53d8\u91cf\u7684\u503c\u53ea\u80fd\u5728\u5b83\u6240\u5c5e\u7c7b\u7684\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u8bbe\u7f6e\u3002 \u4e3a\u4ec0\u4e48C++\u8981\u8fd9\u6837\u505a\uff1f\u7b54\u6848\u4e0econst\u53d8\u91cf\u548c\u5f15\u7528\u53d8\u91cf\u6709\u5173\u3002\u8003\u8651\u4e00\u4e0b\u5982\u679c m_id \u662f const \u4f1a\u53d1\u751f\u4ec0\u4e48\u3002\u56e0\u4e3aconst\u53d8\u91cf\u5fc5\u987b\u5728\u521b\u5efa\u65f6\u7528\u4e00\u4e2a\u503c\u521d\u59cb\u5316\uff0c\u6240\u4ee5\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u5fc5\u987b\u5728\u521b\u5efa\u53d8\u91cf\u65f6\u8bbe\u7f6e\u5b83\u7684\u503c\u3002\u4f46\u662f\uff0c\u5f53\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u5b8c\u6210\u65f6\uff0c\u5c06\u6267\u884c\u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u3002\u7136\u540e\u6bcf\u4e2a\u6d3e\u751f\u7c7b\u90fd\u6709\u673a\u4f1a\u521d\u59cb\u5316\u8be5\u53d8\u91cf\uff0c\u53ef\u80fd\u4f1a\u6539\u53d8\u5b83\u7684\u503c!\u901a\u8fc7\u5c06\u53d8\u91cf\u7684\u521d\u59cb\u5316\u9650\u5236\u5728\u8fd9\u4e9b\u53d8\u91cf\u6240\u5c5e\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4e2d\uff0cC++\u9700\u8981\u786e\u4fdd\u6240\u6709\u53d8\u91cf\u53ea\u521d\u59cb\u5316\u4e00\u6b21\u3002 \u6700\u7ec8\u7684\u7ed3\u679c\u662f\u4e0a\u9762\u7684\u793a\u4f8b\u4e0d\u8d77\u4f5c\u7528\uff0c\u56e0\u4e3a m_id \u662f\u4ece Base \u7ee7\u627f\u7684\uff0c\u5e76\u4e14\u53ea\u6709\u975e\u7ee7\u627f\u7684\u53d8\u91cf\u53ef\u4ee5\u5728\u6210\u5458\u521d\u59cb\u5316\u5668\u5217\u8868\u4e2d\u521d\u59cb\u5316\u3002 \u4f46\u662f\uff0c\u7ee7\u627f\u7684\u53d8\u91cf\u4ecd\u7136\u53ef\u4ee5\u5728\u6784\u9020\u51fd\u6570\u4f53\u4e2d\u4f7f\u7528\u8d4b\u503c\u64cd\u4f5c\u66f4\u6539\u5176\u503c\u3002\u56e0\u6b64\uff0c\u65b0\u7a0b\u5e8f\u5458\u901a\u5e38\u4e5f\u4f1a\u8fd9\u6837\u505a: 1 2 3 4 5 6 7 8 9 10 11 12 13 class Derived : public Base { public : double m_cost {}; Derived ( double cost = 0.0 , int id = 0 ) : m_cost { cost } { m_id = id ; } double getCost () const { return m_cost ; } }; \u867d\u7136\u8fd9\u5728\u672c\u4f8b\u4e2d\u5b9e\u9645\u4e0a\u662f\u53ef\u884c\u7684\uff0c\u4f46\u5982\u679c m_id \u662f const \u6216\u5f15\u7528\uff0c\u5219\u4e0d\u53ef\u884c(\u56e0\u4e3a const \u503c\u548c\u5f15\u7528\u5fc5\u987b\u5728\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u521d\u59cb\u5316)\u3002\u5b83\u7684\u6548\u7387\u4e5f\u5f88\u4f4e\uff0c\u56e0\u4e3a m_id \u88ab\u5206\u914d\u4e86\u4e24\u6b21\u503c\uff1a\u4e00\u6b21\u662f\u5728\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\uff0c\u7136\u540e\u662f\u5728\u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u7684\u4e3b\u4f53\u4e2d\u3002\u6700\u540e\uff0c\u5982\u679c\u57fa\u7c7b\u5728\u6784\u9020\u8fc7\u7a0b\u4e2d\u9700\u8981\u8bbf\u95ee\u8fd9\u4e2a\u503c\u600e\u4e48\u529e\uff1f\u5b83\u6ca1\u6709\u529e\u6cd5\u8bbf\u95ee\u5b83\uff0c\u56e0\u4e3a\u5b83\u662f\u5728\u6267\u884c Derived \u6784\u9020\u51fd\u6570\u4e4b\u524d\u624d\u8bbe\u7f6e\u7684(\u8fd9\u57fa\u672c\u4e0a\u662f\u5728\u6700\u540e\u6267\u884c)\u3002 \u90a3\u4e48\uff0c\u6211\u4eec\u5e94\u8be5\u5982\u4f55\u5b50\u521b\u5efa Derived \u7c7b\u5bf9\u8c61\u65f6\u6b63\u786e\u521d\u59cb\u5316 m_id \u5462\uff1f \u5728\u4e0a\u9762\u7684\u8fd9\u4e9b\u4f8b\u5b50\u4e2d\uff0c\u5f53\u6211\u4eec\u521d\u59cb\u5316 Derived \u7c7b\u7684\u5bf9\u8c61\u65f6\uff0c Base \u7c7b\u7684\u90e8\u5206\u90fd\u662f\u901a\u8fc7\u5b83\u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u6765\u521b\u5efa\u7684\u3002\u4e3a\u4ec0\u4e48\u5b83\u603b\u662f\u4f1a\u8c03\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u5462\uff1f\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u8ba9\u4ed6\u4e0d\u8981\u8fd9\u4e48\u505a\u554a\uff01 \u6240\u5e78\uff0cC++ \u5141\u8bb8\u6211\u4eec\u663e\u5f0f\u5730\u6307\u5b9a\u521b\u5efa Base \u7c7b\u65f6\u5e94\u8be5\u4f7f\u7528\u7684\u6784\u9020\u51fd\u6570\uff01\u6211\u4eec\u53ea\u9700\u8981\u5728\u6d3e\u751f\u7c7b\u7684 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u4e2d\u8c03\u7528\u6240\u9700\u7684 Base \u7684\u6784\u9020\u51fd\u6570\u5c31\u53ef\u4ee5\u4e86\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 class Derived : public Base { public : double m_cost {}; Derived ( double cost = 0.0 , int id = 0 ) : Base { id } // Call Base(int) constructor with value id! , m_cost { cost } { } double getCost () const { return m_cost ; } }; \u518d\u6b21\u6267\u884c\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { Derived derived { 1.3 , 5 }; // use Derived(double, int) constructor std :: cout << \"Id: \" << derived . getId () << '\\n' ; std :: cout << \"Cost: \" << derived . getCost () << '\\n' ; return 0 ; } \u57fa\u7c7b\u7684\u6784\u9020\u51fd\u6570 Base(int) \u5c06\u4f1a\u88ab\u7528\u6765\u521d\u59cb\u5316\u6210\u5458 m_id \uff085\uff09\u7136\u540e\u6d3e\u751f\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4f1a\u88ab\u7528\u6765\u521d\u59cb\u5316 m_cost \u5230 1.3 \uff01 \u56e0\u6b64\uff0c\u7a0b\u5e8f\u4f1a\u6253\u5370\uff1a 1 2 Id: 5 Cost: 1.3 \u5177\u4f53\u6765\u8bf4\uff1a \u5185\u5b58\u5206\u914d\uff1b \u6784\u9020\u51fd\u6570 Derived(double, int) \u88ab\u8c03\u7528\uff0c cost = 1.3 \uff0c id = 5 \u3002 \u7f16\u8bd1\u5668\u67e5\u770b\u4f60\u662f\u5426\u6307\u5b9a\u4e86 Base \u7c7b\u7684\u6784\u9020\u51fd\u6570\u3002\u8fd9\u91cc\u6211\u4eec\u6307\u5b9a\u4e86\uff0c\u6240\u4ee5\u5b83\u4f1a\u8c03\u7528 Base(int) \u4e14 id = 5 \u3002 \u57fa\u7c7b\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u5c06 m_id \u8bbe\u7f6e\u4e3a5\uff1b \u57fa\u7c7b\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u6267\u884c\uff0c\u4ec0\u4e48\u90fd\u6ca1\u505a\uff1b \u57fa\u7c7b\u6784\u9020\u51fd\u6570\u8fd4\u56de\uff1b \u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u5c06 m_cost \u8bbe\u7f6e\u4e3a 1.3\uff1b \u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u6267\u884c\uff0c\u4ec0\u4e48\u90fd\u6ca1\u505a\uff1b \u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u8fd4\u56de\u3002 \u770b\u8d77\u6765\u6709\u70b9\u590d\u6742\uff0c\u4f46\u5b9e\u9645\u4e0a\u975e\u5e38\u7b80\u5355\u3002\u6240\u53d1\u751f\u7684\u4e00\u5207\u5c31\u662f Derived \u6784\u9020\u51fd\u6570\u8c03\u7528\u6307\u5b9a\u7684 Base \u6784\u9020\u51fd\u6570\u6765\u521d\u59cb\u5316\u5bf9\u8c61\u7684 Base \u90e8\u5206\u3002\u56e0\u4e3a m_id \u4f4d\u4e8e\u5bf9\u8c61\u7684 Base \u90e8\u5206\uff0c\u6240\u4ee5 Base \u6784\u9020\u51fd\u6570\u662f\u552f\u4e00\u53ef\u4ee5\u521d\u59cb\u5316\u8be5\u503c\u7684\u6784\u9020\u51fd\u6570\u3002 \u6ce8\u610f\uff0cBase\u6784\u9020\u51fd\u6570\u5728 Derived \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u7684\u4ec0\u4e48\u4f4d\u7f6e\u88ab\u8c03\u7528\u5e76\u4e0d\u91cd\u8981\u2014\u2014\u5b83\u603b\u662f\u9996\u5148\u6267\u884c\u3002","title":"\u521d\u59cb\u5316\u57fa\u7c7b\u6210\u5458"},{"location":"17-4-constructors-and-initialization-of-derived-classes/#_2","text":"\u65e2\u7136\u5df2\u7ecf\u77e5\u9053\u4e86\u5982\u4f55\u521d\u59cb\u5316\u57fa\u7c7b\u6210\u5458\uff0c\u5c31\u6ca1\u6709\u5fc5\u8981\u5c06\u6210\u5458\u53d8\u91cf\u4fdd\u6301\u4e3a public \u3002\u6211\u4eec\u518d\u6b21\u5c06\u6210\u5458\u53d8\u91cf\u8bbe\u4e3a\u79c1\u6709\uff0c\u56e0\u4e3a\u5b83\u4eec\u5e94\u8be5\u662f\u79c1\u6709\u7684\u3002 \u5feb\u901f\u56de\u987e\u4e00\u4e0b\uff0c\u516c\u5171\u6210\u5458\u53ef\u4ee5\u88ab\u4efb\u4f55\u4eba\u8bbf\u95ee\u3002\u79c1\u6709\u6210\u5458\u53ea\u80fd\u7531\u540c\u4e00\u7c7b\u7684\u6210\u5458\u51fd\u6570\u8bbf\u95ee\u3002\u6ce8\u610f\uff0c \u8fd9\u610f\u5473\u7740\u6d3e\u751f\u7c7b\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u57fa\u7c7b\u7684\u79c1\u6709\u6210\u5458 \uff01\u6d3e\u751f\u7c7b\u5c06\u9700\u8981\u4f7f\u7528\u8bbf\u95ee\u51fd\u6570\u6765\u8bbf\u95ee\u57fa\u7c7b\u7684\u79c1\u6709\u6210\u5458\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include class Base { private : // our member is now private int m_id {}; public : Base ( int id = 0 ) : m_id { id } { } int getId () const { return m_id ; } }; class Derived : public Base { private : // our member is now private double m_cost ; public : Derived ( double cost = 0.0 , int id = 0 ) : Base { id } // Call Base(int) constructor with value id! , m_cost { cost } { } double getCost () const { return m_cost ; } }; int main () { Derived derived { 1.3 , 5 }; // use Derived(double, int) constructor std :: cout << \"Id: \" << derived . getId () << '\\n' ; std :: cout << \"Cost: \" << derived . getCost () << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06 m_id \u548c m_cost \u8bbe\u4e3a\u79c1\u6709\u3002\u8fd9\u4e48\u505a\u5b8c\u5168\u6ca1\u6709\u95ee\u9898\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u76f8\u5173\u7684\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u8fd9\u4e9b\u6210\u5458\uff0c\u5e76\u901a\u8fc7\u516c\u6709\u7684 \u6210\u5458\u8bbf\u95ee\u51fd\u6570 \u6765\u8bbf\u95ee\u5b83\u4eec\u3002 \u6253\u5370\u7ed3\u679c\u5982\u6211\u4eec\u6240\u60f3\u7684\u90a3\u6837\uff1a 1 2 Id: 5 Cost: 1.3 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u66f4\u591a\u5730\u8ba8\u8bba\u8bbf\u95ee\u8bf4\u660e\u7b26\u3002","title":"\u5c06\u6210\u5458\u8bbe\u4e3a\u79c1\u6709"},{"location":"17-4-constructors-and-initialization-of-derived-classes/#_3","text":"\u518d\u770b\u770b\u4e4b\u524d\u6211\u4eec\u4f7f\u7528\u8fc7\u7684\u4e00\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include #include class Person { public : std :: string m_name ; int m_age {}; Person ( const std :: string_view name = \"\" , int age = 0 ) : m_name { name }, m_age { age } { } const std :: string & getName () const { return m_name ; } int getAge () const { return m_age ; } }; // BaseballPlayer publicly inheriting Person class BaseballPlayer : public Person { public : double m_battingAverage {}; int m_homeRuns {}; BaseballPlayer ( double battingAverage = 0.0 , int homeRuns = 0 ) : m_battingAverage { battingAverage }, m_homeRuns { homeRuns } { } }; \u5728\u4e4b\u524d\u7684\u4f8b\u5b50\u4e2d\uff0c BaseballPlayer \u53ea\u4f1a\u521d\u59cb\u5316\u5b83\u81ea\u5df1\u7684\u6210\u5458\uff0c\u4e5f\u5e76\u6ca1\u6709\u6307\u5b9a Person \u7684\u6784\u9020\u51fd\u6570\u3002\u8fd9\u610f\u5473\u7740 BaseballPlayer \u5728\u521b\u5efa\u65f6\uff0c\u8c03\u7528\u7684\u90fd\u662f Person \u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u5b83\u4f1a\u5c06\u540d\u5b57\u521d\u59cb\u5316\u4e3a\u7a7a\u767d\u5e76\u5c06\u5e74\u9f84\u521d\u59cb\u5316\u4e3a0\u3002\u56e0\u4e3a\uff0c\u56e0\u4e3a\u5728\u521b\u5efa BaseballPlayer \u65f6\u7ed9\u5b83\u4eec\u4e00\u4e2a\u540d\u79f0\u548c\u5e74\u9f84\u662f\u6709\u610f\u4e49\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u5e94\u8be5\u4fee\u6539\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u6765\u6dfb\u52a0\u8fd9\u4e9b\u53c2\u6570\u3002 \u66f4\u65b0\u4ee3\u7801\uff0c\u8ba9\u7c7b\u4f7f\u7528\u79c1\u6709\u6210\u5458\uff0c\u540c\u65f6\u8ba9 BaseballPlayer \u7c7b\u8c03\u7528\u9002\u5f53\u7684 Person \u6784\u9020\u51fd\u6570\u6765\u521d\u59cb\u5316\u7ee7\u627f\u7684 Person \u6210\u5458\u53d8\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include #include class Person { private : std :: string m_name ; int m_age {}; public : Person ( const std :: string_view name = \"\" , int age = 0 ) : m_name { name }, m_age { age } { } const std :: string & getName () const { return m_name ; } int getAge () const { return m_age ; } }; // BaseballPlayer \u516c\u5f00\u7ee7\u627f Person class BaseballPlayer : public Person { private : double m_battingAverage {}; int m_homeRuns {}; public : BaseballPlayer ( const std :: string_view name = \"\" , int age = 0 , double battingAverage = 0.0 , int homeRuns = 0 ) : Person { name , age } // \u8c03\u7528 Person(const std::string_view, int) \u6765\u521d\u59cb\u5316\u76f8\u5e94\u7684\u6210\u5458 , m_battingAverage { battingAverage }, m_homeRuns { homeRuns } { } double getBattingAverage () const { return m_battingAverage ; } int getHomeRuns () const { return m_homeRuns ; } }; \u73b0\u5728\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { BaseballPlayer pedro { \"Pedro Cerrano\" , 32 , 0.342 , 42 }; std :: cout << pedro . getName () << '\\n' ; std :: cout << pedro . getAge () << '\\n' ; std :: cout << pedro . getBattingAverage () << '\\n' ; std :: cout << pedro . getHomeRuns () << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 2 3 4 Pedro Cerrano 32 0.342 42 \u53ef\u4ee5\u770b\u5230\uff0c\u57fa\u7c7b\u7684\u540d\u79f0\u548c\u5e74\u9f84\u5df2\u6b63\u786e\u521d\u59cb\u5316\uff0c\u6d3e\u751f\u7c7b\u7684\u672c\u5792\u6253\u6570\u548c\u51fb\u7403\u7387\u4e5f\u5df2\u521d\u59cb\u5316\u3002","title":"\u53e6\u5916\u4e00\u4e2a\u4f8b\u5b50"},{"location":"17-4-constructors-and-initialization-of-derived-classes/#_4","text":"\u7ee7\u627f\u94fe\u4e2d\u7684\u7c7b\u4ee5\u5b8c\u5168\u76f8\u540c\u7684\u65b9\u5f0f\u5de5\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include class A { public : A ( int a ) { std :: cout << \"A: \" << a << '\\n' ; } }; class B : public A { public : B ( int a , double b ) : A { a } { std :: cout << \"B: \" << b << '\\n' ; } }; class C : public B { public : C ( int a , double b , char c ) : B { a , b } { std :: cout << \"C: \" << c << '\\n' ; } }; int main () { C c { 5 , 4.3 , 'R' }; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7c7bC\u662f\u7531\u7c7bB\u6d3e\u751f\u800c\u6765\u7684\uff0c\u800c\u7c7bB\u53c8\u662f\u7531\u7c7bA\u6d3e\u751f\u800c\u6765\u7684\u3002\u90a3\u4e48\u5f53\u6211\u4eec\u5b9e\u4f8b\u5316\u7c7bC\u7684\u5bf9\u8c61\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462? \u9996\u5148 main() \u4f1a\u8c03\u7528 C(int, double, char) \u3002C \u7684\u6784\u9020\u51fd\u6570\u4f1a\u8c03\u7528 B(int, double) \u3002B\u7684\u6784\u9020\u51fd\u6570\u8c03\u7528 A(int) \u3002\u56e0\u4e3aA\u6ca1\u6709\u7ee7\u627f\u4efb\u4f55\u7c7b\uff0c\u6240\u4ee5\u5b83\u4f1a\u88ab\u7b2c\u4e00\u4e2a\u6784\u9020\u3002A \u88ab\u6784\u9020\u540e\u6253\u5370\u4e86 5\uff0c\u7136\u540e\u63a7\u5236\u6743\u8fd4\u56de\u7ed9B\u3002B\u88ab\u6784\u9020\u65f6\u6253\u5370\u503c 4.3\uff0c\u7136\u540e\u8fd4\u56de\u63a7\u5236\u6743\u7ed9C\u3002C\u88ab\u6784\u9020\u65f6\u6253\u5370\u2018R\u2019\uff0c\u7136\u540e\u5c06\u63a7\u5236\u6743\u8fd4\u56de\u7ed9 main() \u3002\u6784\u9020\u7ed3\u675f\uff01 \u56e0\u6b64\u7a0b\u5e8f\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 A: 5 B: 4.3 C: R \u503c\u5f97\u4e00\u63d0\u7684\u662f\uff0c\u6784\u9020\u51fd\u6570\u53ea\u80fd\u8c03\u7528 \u76f4\u63a5 \u7236\u7c7b\uff08\u57fa\u7c7b\uff09\u4e2d\u7684\u6784\u9020\u51fd\u6570\u3002\u56e0\u6b64\uff0cC\u6784\u9020\u51fd\u6570\u4e0d\u80fd\u76f4\u63a5\u8c03\u7528\u6216\u5c06\u53c2\u6570\u4f20\u9012\u7ed9A\u6784\u9020\u51fd\u6570\u3002C\u6784\u9020\u51fd\u6570\u53ea\u80fd\u8c03\u7528B\u6784\u9020\u51fd\u6570(B\u6784\u9020\u51fd\u6570\u8d1f\u8d23\u8c03\u7528A\u6784\u9020\u51fd\u6570)\u3002","title":"\u7ee7\u627f\u94fe"},{"location":"17-4-constructors-and-initialization-of-derived-classes/#_5","text":"\u5f53\u6d3e\u751f\u7c7b\u88ab\u9500\u6bc1\u65f6\uff0c\u6bcf\u4e2a\u6790\u6784\u51fd\u6570\u5c06\u6309\u6784\u9020\u7684 \u9006\u987a\u5e8f\u8c03\u7528 \u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53C\u88ab\u9500\u6bc1\u65f6\uff0c\u9996\u5148\u8c03\u7528C\u6790\u6784\u51fd\u6570\uff0c\u7136\u540e\u662fB\u6790\u6784\u51fd\u6570\uff0c\u7136\u540e\u662fA\u6790\u6784\u51fd\u6570\u3002","title":"\u6790\u6784\u51fd\u6570"},{"location":"17-4-constructors-and-initialization-of-derived-classes/#_6","text":"\u5728\u6784\u9020\u6d3e\u751f\u7c7b\u65f6\uff0c\u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u8d1f\u8d23\u786e\u5b9a\u8c03\u7528\u54ea\u4e2a\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u57fa\u7c7b\u6784\u9020\u51fd\u6570\uff0c\u5c06\u4f7f\u7528\u9ed8\u8ba4\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u627e\u4e0d\u5230\u7f3a\u7701\u57fa\u7c7b\u6784\u9020\u51fd\u6570(\u6216\u7f3a\u7701\u521b\u5efa\u57fa\u7c7b\u6784\u9020\u51fd\u6570)\uff0c\u7f16\u8bd1\u5668\u5c06\u62a5\u9519\u3002\u7136\u540e\u6309\u7167\u4ece\u6700\u57fa\u7c7b\u5230\u6700\u6d3e\u751f\u7c7b\u7684\u987a\u5e8f\u6784\u9020\u7c7b\u3002 \u81f3\u6b64\uff0c\u60a8\u5df2\u7ecf\u8db3\u591f\u4e86\u89e3C++\u7ee7\u627f\uff0c\u53ef\u4ee5\u521b\u5efa\u81ea\u5df1\u7684\u7ee7\u627f\u7c7b\u4e86!","title":"\u5c0f\u7ed3"},{"location":"17-5-inheritance-and-access-specifiers/","tags":["inheritance","access-specifiers"],"text":"Key Takeaway \u6d3e\u751f\u7c7b\u4ece\u57fa\u7c7b\u7ee7\u627f\u4e86\u4e00\u4e9b\u5c5e\u6027\uff0c\u4ece\u5916\u90e8\u6765\u770b\uff0c\u8fd9\u4e9b\u7ee7\u627f\u6765\u7684\u5c5e\u6027\u5c31\u597d\u50cf\u662f\u6d3e\u751f\u7c7b\u7684\u4e00\u6837\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u3002\u5916\u90e8\u4ee3\u7801\u80fd\u5426\u901a\u8fc7\u6d3e\u751f\u7c7b\u8bbf\u95ee\u8fd9\u4e9b\u7ee7\u627f\u6765\u7684\u5c5e\u6027\u65f6\uff0c\u9700\u8981\u53c2\u8003\u7ee7\u627f\u7c7b\u578b\u4f7f\u7528\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u3002\uff08\u964d\u7ea7\u4e3a\u7ee7\u627f\u7b49\u7ea7\uff09 \u6d3e\u751f\u7c7b\u5bf9\u57fa\u7c7b\u6210\u5458\u7684\u8bbf\u95ee\u4e0d\u53d7\u7ee7\u627f\u7c7b\u578b\u5f71\u54cd\uff0c\u53ea\u53c2\u8003\u57fa\u7c7b\u7684\u8bbf\u95ee\u4fee\u9970\u7b26 \u5728\u672c\u7ae0\u524d\u9762\u7684\u8bfe\u7a0b\u4e2d\uff0c\u60a8\u5df2\u7ecf\u4e86\u89e3\u4e86\u57fa\u7ee7\u627f\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u5728\u6211\u4eec\u6240\u6709\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u90fd\u4f7f\u7528\u4e86\u516c\u5f00\u7ee7\u627f\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u7684\u6d3e\u751f\u7c7b\u516c\u5f00\u7ee7\u627f\u57fa\u7c7b\u3002 \u5728\u8fd9\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u4e86\u89e3\u516c\u5f00\u7ee7\u627f\uff0c\u4ee5\u53ca\u53e6\u5916\u4e24\u79cd\u7c7b\u578b\u7684\u7ee7\u627f(\u79c1\u6709\u7684\u548c\u53d7\u4fdd\u62a4\u7684)\u3002\u6211\u4eec\u8fd8\u5c06\u63a2\u8ba8\u4e0d\u540c\u7c7b\u578b\u7684\u7ee7\u627f\u5982\u4f55\u4e0e \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \uff0c\u4ee5\u5141\u8bb8\u6216\u9650\u5236\u5bf9\u6210\u5458\u7684\u8bbf\u95ee\u3002 \u81f3\u6b64\uff0c\u4f60\u5df2\u7ecf\u770b\u5230\u4e86\u79c1\u6709\u8bbf\u95ee\u8bf4\u660e\u7b26\u548c\u516c\u5171\u8bbf\u95ee\u8bf4\u660e\u7b26\uff0c\u5b83\u4eec\u51b3\u5b9a\u8c01\u53ef\u4ee5\u8bbf\u95ee\u7c7b\u7684\u6210\u5458\u3002\u5feb\u901f\u590d\u4e60\u4e00\u4e0b\uff0c \u516c\u6709\u6210\u5458 \u53ef\u4ee5\u88ab\u4efb\u4f55\u4eba\u8bbf\u95ee\u3002 \u79c1\u6709\u6210\u5458 \u53ea\u80fd\u7531\u540c\u4e00\u7c7b\u6216 \u53cb\u5143\u7c7b \u7684\u6210\u5458\u51fd\u6570\u8bbf\u95ee\u3002\u8fd9\u610f\u5473\u7740\u6d3e\u751f\u7c7b\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u57fa\u7c7b\u7684\u79c1\u6709\u6210\u5458\uff01 1 2 3 4 5 6 7 class Base { private : int m_private {}; // can only be accessed by Base members and friends (not derived classes) public : int m_public {}; // can be accessed by anybody }; \u975e\u5e38\u7b80\u5355\u5427\uff0c\u4f60\u5e94\u8be5\u5df2\u7ecf\u80fd\u591f\u5f88\u597d\u5730\u638c\u63e1\u5e76\u4f7f\u7528\u8fd9\u4e9b\u77e5\u8bc6\u4e86\u3002 protected \u8bbf\u95ee\u8bf4\u660e\u7b26 \u5f53\u914d\u5408\u7ee7\u627f\u7c7b\u5de5\u4f5c\u65f6\uff0c\u95ee\u9898\u53d8\u5f97\u590d\u6742\u4e86\u4e00\u4e9b\u3002 C++ \u63d0\u4f9b\u4e86\u7b2c\u4e09\u79cd\u8bbf\u95ee\u8bf4\u660e\u7b26\uff0c\u5b83\u53ea\u5728\u4f7f\u7528\u7ee7\u627f\u7684\u573a\u5408\u4e2d\u6709\u7528\u9014\u3002 protected \u8bbf\u95ee\u8bf4\u660e\u7b26\u5141\u8bb8\u6210\u5458\u88ab\u7c7b\u672c\u8eab\u3001\u53cb\u5143\u7c7b\u548c\u6d3e\u751f\u7c7b\u8bbf\u95ee\u3002\u4f46\u662f\uff0c \u53d7\u4fdd\u62a4\u6210\u5458 \u4e0d\u80fd\u88ab\u5916\u90e8\u7c7b\u8bbf\u95ee\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 class Base { public : int m_public {}; // \u53ef\u4ee5\u88ab\u4efb\u4f55\u4eba\u8bbf\u95ee protected : int m_protected {}; // \u53ef\u4ee5\u88ab Base\u3001\u53cb\u5143\u7c7b\u548c\u6d3e\u751f\u7c7b\u8bbf\u95ee private : int m_private {}; // \u53ea\u80fd\u88ab Base \u548c\u53cb\u5143\u8bbf\u95ee\uff08\u4e0d\u80fd\u88ab\u6d3e\u751f\u7c7b\u76f4\u63a5\u8bbf\u95ee\uff09 }; class Derived : public Base { public : Derived () { m_public = 1 ; // \u5141\u8bb8: \u53ef\u4ee5\u5728\u6d3e\u751f\u7c7b\u4e2d\u8bbf\u95ee\u57fa\u7c7b\u7684\u516c\u6709\u6210\u5458 m_protected = 2 ; // \u5141\u8bb8: \u53ef\u4ee5\u5728\u6d3e\u751f\u7c7b\u4e2d\u8bbf\u95ee\u57fa\u7c7b\u4e2d\u7684\u53d7\u4fdd\u62a4\u6210\u5458 m_private = 3 ; // \u4e0d\u5141\u8bb8\uff1a\u4e0d\u80fd\u5728\u6d3e\u751f\u7c7b\u4e2d\u8bbf\u95ee\u57fa\u7c7b\u7684\u79c1\u6709\u6210\u5458 } }; int main () { Base base ; base . m_public = 1 ; // \u5141\u8bb8: \u53ef\u4ee5\u4ece\u5916\u90e8\u7c7b\u8bbf\u95ee\u516c\u6709\u6210\u5458 base . m_protected = 2 ; // \u4e0d\u5141\u8bb8: \u4e0d\u80fd\u591f\u4ece\u5916\u90e8\u7c7b\u8bbf\u95ee\u516c\u6709\u6210\u5458 base . m_private = 3 ; // \u4e0d\u5141\u8bb8\uff1a\u4e0d\u80fd\u591f\u4ece\u5916\u90e8\u8bbf\u95ee\u79c1\u6709\u6210\u5458 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u53d7\u4fdd\u62a4\u7684\u57fa\u7c7b\u7684\u6210\u5458 m_protected \u53ef\u4ee5\u76f4\u63a5\u7531\u6d3e\u751f\u7c7b\u8bbf\u95ee\uff0c\u4f46\u4e0d\u80fd\u5728\u5916\u90e8\u88ab\u8bbf\u95ee\u3002 \u90a3\u4e48\uff0c\u5e94\u8be5\u5728\u4f55\u65f6\u4f7f\u7528 protected \u8bbf\u95ee\u8bf4\u660e\u7b26\uff1f \u57fa\u7c7b\u4e2d\u7684 \u53d7\u4fdd\u62a4\u6210\u5458 \u53ef\u4ee5\u76f4\u63a5\u88ab\u6d3e\u751f\u7c7b\u8bbf\u95ee\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u4f60\u4ee5\u540e\u66f4\u6539\u4e86\u53d7\u4fdd\u62a4\u5c5e\u6027\u7684\u4efb\u4f55\u5185\u5bb9(\u7c7b\u578b\u3001\u503c\u7684\u542b\u4e49\u7b49\u7b49)\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u540c\u65f6\u66f4\u6539\u57fa\u7c7b\u548c\u6240\u6709\u6d3e\u751f\u7c7b\u3002 \u56e0\u6b64\uff0c\u53d7\u4fdd\u62a4\u8bbf\u95ee\u8bf4\u660e\u7b26\u9002\u7528\u4e8e\u4f60\u4ece\u81ea\u5df1\u7f16\u5199\u7c7b\u4e2d\u7ee7\u627f\u5e76\u4e14\u6d3e\u751f\u7c7b\u6570\u91cf\u5e76\u4e0d\u591a\u7684\u60c5\u51b5\u3002\u8fd9\u6837\uff0c\u5982\u679c\u4f60\u5bf9\u57fa\u7c7b\u7684\u5b9e\u73b0\u8fdb\u884c\u4e86\u66f4\u6539\uff0c\u5e76\u4e14\u56e0\u6b64\u9700\u8981\u5bf9\u6d3e\u751f\u7c7b\u8fdb\u884c\u66f4\u65b0\uff0c\u90a3\u4e48\u53ef\u4ee5\u81ea\u5df1\u8fdb\u884c\u66f4\u65b0(\u5e76\u4e14\u4e0d\u4f1a\u82b1\u8d39\u592a\u957f\u65f6\u95f4\uff0c\u56e0\u4e3a\u6d3e\u751f\u7c7b\u7684\u6570\u91cf\u662f\u6709\u9650\u7684)\u3002 \u5c06\u6210\u5458\u8bbe\u4e3a\u79c1\u6709\u610f\u5473\u7740\u516c\u5171\u7c7b\u548c\u6d3e\u751f\u7c7b\u4e0d\u80fd\u76f4\u63a5\u5bf9\u57fa\u7c7b\u8fdb\u884c\u66f4\u6539\u3002\u8fd9\u6709\u5229\u4e8e\u5c06\u516c\u5171\u7c7b\u6216\u6d3e\u751f\u7c7b\u4e0e\u5b9e\u73b0\u66f4\u6539\u9694\u79bb\u5f00\u6765\uff0c\u5e76\u786e\u4fdd\u9002\u5f53\u5730\u7ef4\u62a4\u4e0d\u53d8\u91cf\u3002\u4f46\u662f\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u4f60\u7684\u7c7b\u53ef\u80fd\u9700\u8981\u4e00\u4e2a\u66f4\u5927\u7684\u516c\u5f00(\u6216\u53d7\u4fdd\u62a4\u7684)\u63a5\u53e3\u6765\u652f\u6301\u516c\u5171\u6216\u6d3e\u751f\u7c7b\u64cd\u4f5c\u6240\u9700\u7684\u6240\u6709\u529f\u80fd\uff0c\u8be5\u63a5\u53e3\u6709\u5176\u81ea\u8eab\u7684\u6784\u5efa\u3001\u6d4b\u8bd5\u548c\u7ef4\u62a4\u6210\u672c\u3002 \u4e00\u822c\u6765\u8bf4\uff0c\u5982\u679c\u53ef\u4ee5\u7684\u8bdd\uff0c\u6700\u597d\u5c06\u6210\u5458\u8bbe\u4e3a\u79c1\u6709\u7684\uff0c\u53ea\u6709\u51c6\u5907\u521b\u5efa\u6d3e\u751f\u7c7b\u4e14\u6784\u5efa\u548c\u7ef4\u62a4\u5230\u90a3\u4e9b\u79c1\u6709\u6210\u5458\u7684\u63a5\u53e3\u7684\u6210\u672c\u8fc7\u9ad8\u65f6\u624d\u4f7f\u7528\u53d7 protected \u8bbf\u95ee\u8bf4\u660e\u7b26\u3002 \u6269\u5c55\u9605\u8bfb \u4f18\u5148\u4f7f\u7528\u79c1\u6709\u6210\u5458\u800c\u4e0d\u662f\u53d7\u4fdd\u62a4\u6210\u5458\u3002 \u4e0d\u540c\u7c7b\u578b\u7684\u7ee7\u627f\u4ee5\u53ca\u5b83\u4eec\u5bf9\u6210\u5458\u8bbf\u95ee\u7684\u5f71\u54cd \u9996\u5148\uff0c\u7ee7\u627f\u7c7b\u578b\u6709\u4e09\u79cd\uff1a \u516c\u5f00\u7ee7\u627f \u3001 \u53d7\u4fdd\u62a4\u7ee7\u627f \u548c \u79c1\u6709\u7ee7\u627f \u3002 \u53ea\u9700\u5728\u7ee7\u627f\u7c7b\u65f6\u6307\u5b9a\u9700\u8981\u7684\u8bbf\u95ee\u7c7b\u578b\u5c31\u53ef\u4ee5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // Inherit from Base publicly class Pub : public Base { }; // Inherit from Base protectedly class Pro : protected Base { }; // Inherit from Base privately class Pri : private Base { }; class Def : Base // Defaults to private inheritance { }; \u5982\u679c\u4e0d\u9009\u62e9\u7ee7\u627f\u7c7b\u578b\uff0cC++\u9ed8\u8ba4\u4e3a\u79c1\u6709\u7ee7\u627f(\u5c31\u50cf\u5982\u679c\u4e0d\u6307\u5b9a\u5176\u4ed6\u7c7b\u578b\uff0c\u6210\u5458\u9ed8\u8ba4\u4e3a\u79c1\u6709\u8bbf\u95ee\u4e00\u6837)\u3002 \u8fd9\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e869\u79cd\u7ec4\u5408\uff1a3\u4e2a \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 (\u516c\u5171\u3001\u79c1\u6709\u548c\u53d7\u4fdd\u62a4)\u548c3\u79cd\u7ee7\u627f\u7c7b\u578b(\u516c\u5171\u3001\u79c1\u6709\u548c\u53d7\u4fdd\u62a4)\u3002 \u90a3\u4e48\u5b83\u4eec\u7684\u533a\u522b\u662f\u4ec0\u4e48\u5462\uff1f\u7b80\u800c\u8a00\u4e4b\uff0c\u5f53\u6210\u5458\u88ab\u7ee7\u627f\u65f6\uff0c\u7ee7\u627f\u6210\u5458\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u53ef\u4ee5\u6839\u636e\u6240\u4f7f\u7528\u7684\u7ee7\u627f\u7c7b\u578b\u66f4\u6539(\u4ec5\u5728\u6d3e\u751f\u7c7b\u4e2d)\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u57fa\u7c7b\u4e2d\u516c\u5171\u7684\u6216\u53d7\u4fdd\u62a4\u7684\u6210\u5458\uff0c\u5728\u6d3e\u751f\u7c7b\u4e2d\u53ef\u80fd\u662f\u5176\u4ed6\u7c7b\u578b\u7684\u3002 \u8fd9\u53ef\u80fd\u770b\u8d77\u6765\u6709\u70b9\u4ee4\u4eba\u56f0\u60d1\uff0c\u4e0d\u8fc7\u4e0d\u8981\u62c5\u5fc3\uff0c\u8fd9\u8282\u8bfe\u5269\u4e0b\u7684\u65f6\u95f4\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e2a\u95ee\u9898\u3002 \u5728\u6211\u4eec\u9010\u6b65\u5b66\u4e60\u8fd9\u4e9b\u4f8b\u5b50\u65f6\uff0c\u8bf7\u8bb0\u4f4f\u4ee5\u4e0b\u89c4\u5219: \u4e00\u4e2a\u7c7b\u603b\u662f\u80fd\u8bbf\u95ee\u5176\u81ea\u5df1\u7684\u6210\u5458\uff08\u975e\u7ee7\u627f\u800c\u6765\u7684\uff09\uff1b \u5916\u90e8\u5728\u8bbf\u95ee\u4e00\u4e2a\u7c7b\u7684\u6210\u5458\u65f6\uff0c\u57fa\u4e8e\u8be5\u6210\u5458\u7684\u8bbf\u95ee\u4fee\u9970\u7b26\u5224\u65ad\u662f\u5426\u80fd\u8bbf\u95ee\uff1b \u6d3e\u751f\u7c7b\u5728\u8bbf\u95ee\u7ee7\u627f\u6765\u7684\u6210\u5458\u65f6\uff0c\u57fa\u4e8e\u5b83\u4ece\u57fa\u7c7b\u7ee7\u627f\u6765\u7684\u8bbf\u95ee\u4fee\u9970\u7b26\u3002\u8fd9\u4e00\u70b9\u4f1a\u6839\u636e\u8bbf\u95ee\u8bf4\u660e\u7b26\u7684\u4e0d\u540c\u548c\u7ee7\u627f\u7c7b\u578b\u7684\u4e0d\u540c\u800c\u6539\u53d8\u3002 \u516c\u5f00\u7ee7\u627f \u516c\u5f00\u7ee7\u627f \u662f\u6700\u5e38\u7528\u7684\u7ee7\u627f\u7c7b\u578b\u3002\u4e8b\u5b9e\u4e0a\uff0c\u4f60\u5f88\u5c11\u4f1a\u770b\u5230\u6216\u4f7f\u7528\u5176\u4ed6\u7c7b\u578b\u7684\u7ee7\u627f\uff0c\u56e0\u6b64\u4f60\u7684\u4e3b\u8981\u5173\u6ce8\u70b9\u5e94\u8be5\u653e\u5728\u7406\u89e3\u8fd9\u4e00\u8282\u4e0a\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u516c\u5f00\u7ee7\u627f\u4e5f\u662f\u6700\u5bb9\u6613\u7406\u89e3\u7684\u3002\u5f53\u516c\u5f00\u7ee7\u627f\u57fa\u7c7b\u65f6\uff0c\u7ee7\u627f\u7684 \u516c\u6709\u6210\u5458 \u4fdd\u6301\u516c\u6709\uff0c\u7ee7\u627f\u7684 \u53d7\u4fdd\u62a4\u6210\u5458 \u4fdd\u6301\u53d7\u4fdd\u62a4\u3002\u7ee7\u627f\u7684\u79c1\u6709\u6210\u5458\u7531\u4e8e\u5728\u57fa\u7c7b\u4e2d\u662f\u79c1\u6709\u7684\u800c\u4e0d\u53ef\u8bbf\u95ee\uff0c\u56e0\u6b64\u4ecd\u7136\u4e0d\u53ef\u8bbf\u95ee\u3002 \u57fa\u7c7b\u4e2d\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u516c\u6709\u7ee7\u627f\u540e\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 Public Public Protected Protected Private Inaccessible \u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u6765\u8bf4\u660e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 class Base { public : int m_public {}; protected : int m_protected {}; private : int m_private {}; }; class Pub : public Base // \u6ce8\u610f: \u516c\u5f00\u7ee7\u627f { // \u516c\u6709\u7ee7\u627f\u610f\u5473\u7740: // \u516c\u6709\u7ee7\u627f\u7684\u6210\u5458\u4fdd\u6301\u516c\u6709 (m_public \u88ab\u5f53\u505a public \u7684) // \u516c\u6709\u7ee7\u627f\u6765\u7684\u53d7\u4fdd\u62a4\u6210\u5458\u4ecd\u7136\u662f\u53d7\u4fdd\u62a4\u7684( m_protected \u662f protected \u7684) // \u516c\u6709\u7ee7\u627f\u6765\u7684\u79c1\u6709\u6210\u5458\u662f\u4e0d\u53ef\u8bbf\u95ee\u7684( m_private \u4e0d\u53ef\u8bbf\u95ee) public : Pub () { m_public = 1 ; // okay: m_public \u53ef\u4ee5\u88ab\u8bbf\u95ee m_protected = 2 ; // okay: m_protected \u53ef\u4ee5\u88ab\u8bbf\u95ee m_private = 3 ; // not okay: m_private \u4e0d\u53ef\u4ee5\u88ab\u6d3e\u751f\u7c7b\u8bbf\u95ee } }; int main () { // \u5916\u90e8\u8bbf\u95ee\u65f6\u57fa\u4e8e\u88ab\u8bbf\u95ee\u7c7b\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u5224\u65ad Base base ; base . m_public = 1 ; // \u53ef\u4ee5: m_public \u5728 Base \u4e2d\u662f\u516c\u6709\u7684 base . m_protected = 2 ; //\u4e0d\u53ef\u4ee5 : m_protected \u5728 Base \u4e2d\u662f\u53d7\u4fdd\u62a4\u7684 base . m_private = 3 ; // \u4e0d\u53ef\u4ee5: m_private \u5728 Base \u4e2d\u662f\u79c1\u6709\u7684 Pub pub ; pub . m_public = 1 ; // \u53ef\u4ee5: m_public \u5728 Pub \u4e2d\u662f\u516c\u6709\u7684 pub . m_protected = 2 ; // \u4e0d\u53ef\u4ee5 : m_protected \u5728 Pub \u4e2d\u662f\u53d7\u4fdd\u62a4\u7684 pub . m_private = 3 ; // \u4e0d\u53ef\u4ee5: m_private \u5728 Pub \u4e2d\u662f\u4e0d\u53ef\u4ee5\u8bbf\u95ee\u7684 return 0 ; } \u8fd9\u4e0e\u4e0a\u9762\u6211\u4eec\u4ecb\u7ecd\u53d7\u4fdd\u62a4\u8bbf\u95ee\u8bf4\u660e\u7b26\u7684\u793a\u4f8b\u76f8\u540c\uff0c\u4e0d\u540c\u7684\u662f\uff0c\u6211\u4eec\u4e5f\u5b9e\u4f8b\u5316\u4e86\u6d3e\u751f\u7c7b\uff0c\u53ea\u662f\u4e3a\u4e86\u8868\u660e\u4f7f\u7528\u516c\u6709\u7ee7\u627f\uff0c\u57fa\u7c7b\u548c\u6d3e\u751f\u7c7b\u7684\u5de5\u4f5c\u65b9\u5f0f\u662f\u76f8\u540c\u7684\u3002 \u9664\u975e\u6709\u7279\u5b9a\u7684\u539f\u56e0\uff0c\u5426\u5219\u4f60\u5e94\u8be5\u4f7f\u7528\u516c\u5171\u7ee7\u627f\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u516c\u6709\u7ee7\u627f\uff0c\u9664\u975e\u4f60\u6709\u7279\u6b8a\u7684\u7406\u7531\u4e0d\u8fd9\u6837\u505a\u3002 \u53d7\u4fdd\u62a4\u7ee7\u627f \u53d7\u4fdd\u62a4\u7684\u7ee7\u627f\u662f\u6700\u4e0d\u5e38\u89c1\u7684\u7ee7\u627f\u65b9\u6cd5\u3002\u9664\u4e86\u5728\u975e\u5e38\u7279\u6b8a\u7684\u60c5\u51b5\u4e0b\uff0c\u5b83\u51e0\u4e4e\u4ece\u4e0d\u88ab\u4f7f\u7528\u3002\u901a\u8fc7\u53d7\u4fdd\u62a4\u7684\u7ee7\u627f\uff0c\u516c\u5171\u6210\u5458\u548c\u53d7\u4fdd\u62a4\u6210\u5458\u6210\u4e3a\u53d7\u4fdd\u62a4\u6210\u5458\uff0c\u800c\u79c1\u6709\u6210\u5458\u4fdd\u6301\u4e0d\u53ef\u8bbf\u95ee\u72b6\u6001\u3002 \u56e0\u4e3a\u8fd9\u79cd\u5f62\u5f0f\u7684\u7ee7\u627f\u975e\u5e38\u7f55\u89c1\uff0c\u6211\u4eec\u5c06\u8df3\u8fc7\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u53ea\u4f7f\u7528\u4e00\u4e2a\u8868\u8fdb\u884c\u603b\u7ed3: \u57fa\u7c7b\u4e2d\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u53d7\u4fdd\u62a4\u7ee7\u627f\u540e\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 Public Protected Protected Protected Private Inaccessible \u79c1\u6709\u7ee7\u627f \u4f7f\u7528\u79c1\u6709\u7ee7\u627f\uff0c\u57fa\u7c7b\u7684\u6240\u6709\u6210\u5458\u90fd\u88ab\u7ee7\u627f\u4e3a\u79c1\u6709\u3002\u8fd9\u610f\u5473\u7740\u79c1\u6709\u6210\u5458\u662f\u4e0d\u53ef\u8bbf\u95ee\u7684\uff0c\u53d7\u4fdd\u62a4\u7684\u548c\u516c\u5171\u6210\u5458\u4e5f\u53d8\u6210\u79c1\u6709\u3002 \u6ce8\u610f\uff0c\u8fd9\u4e0d\u4f1a\u5f71\u54cd\u6d3e\u751f\u7c7b\u8bbf\u95ee\u4ece\u5176\u7236\u7c7b\u7ee7\u627f\u7684\u6210\u5458\u7684\u65b9\u5f0f\uff01\u5b83\u53ea\u5f71\u54cd\u8bd5\u56fe\u901a\u8fc7\u6d3e\u751f\u7c7b\u8bbf\u95ee\u8fd9\u4e9b\u6210\u5458\u7684\u4ee3\u7801\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 class Base { public : int m_public {}; protected : int m_protected {}; private : int m_private {}; }; class Pri : private Base // note: private inheritance { // Private inheritance means: // Public inherited members become private (so m_public is treated as private) // Protected inherited members become private (so m_protected is treated as private) // Private inherited members stay inaccessible (so m_private is inaccessible) public : Pri () { m_public = 1 ; // okay: m_public is now private in Pri m_protected = 2 ; // okay: m_protected is now private in Pri m_private = 3 ; // not okay: derived classes can't access private members in the base class } }; int main () { // Outside access uses the access specifiers of the class being accessed. // In this case, the access specifiers of base. Base base ; base . m_public = 1 ; // okay: m_public is public in Base base . m_protected = 2 ; // not okay: m_protected is protected in Base base . m_private = 3 ; // not okay: m_private is private in Base Pri pri ; pri . m_public = 1 ; // not okay: m_public is now private in Pri pri . m_protected = 2 ; // not okay: m_protected is now private in Pri pri . m_private = 3 ; // not okay: m_private is inaccessible in Pri return 0 ; } \u603b\u7ed3\u5982\u4e0b\uff1a \u57fa\u7c7b\u4e2d\u7684\u8bbf\u95ee\u4fee\u9970\u7b26 \u79c1\u6709\u7ee7\u627f\u540e\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 Public Private Protected Private Private Inaccessible \u5f53\u6d3e\u751f\u7c7b\u4e0e\u57fa\u7c7b\u6ca1\u6709\u660e\u663e\u7684\u5173\u7cfb\uff0c\u4f46\u5728\u5185\u90e8\u4f7f\u7528\u57fa\u7c7b\u5b9e\u73b0\u65f6\uff0c\u79c1\u6709\u7ee7\u627f\u53ef\u80fd\u5f88\u6709\u7528\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u4e0d\u5e0c\u671b\u57fa\u7c7b\u7684\u516c\u5171\u63a5\u53e3\u901a\u8fc7\u6d3e\u751f\u7c7b\u7684\u5bf9\u8c61\u516c\u5f00(\u5982\u679c\u516c\u5f00\u7ee7\u627f\u5c31\u4f1a\u8fd9\u6837)\u3002 \u4e0d\u8fc7\uff0c\u5728\u5b9e\u8df5\u4e2d\uff0c\u5f88\u5c11\u4f7f\u7528\u79c1\u6709\u7ee7\u627f\u3002 \u6700\u540e\u4e00\u4e2a\u4f8b\u5b50 1 2 3 4 5 6 7 8 9 class Base { public : int m_public {}; protected : int m_protected {}; private : int m_private {}; }; Base \u5728\u8bbf\u95ee\u81ea\u5df1\u7684\u6210\u5458\u65f6\uff0c\u6ca1\u6709\u4efb\u4f55\u9650\u5236\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5916\u90e8\u4ee3\u7801\u53ea\u80fd\u8bbf\u95ee m_public \u3002\u6d3e\u751f\u7c7b\u53ef\u4ee5\u8bbf\u95ee m_public \u548c m_protected \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 class D2 : private Base // note: private inheritance { // \u79c1\u6709\u7ee7\u627f\uff1a // \u7ee7\u627f\u6765\u7684\u516c\u6709\u6210\u5458\u53d8\u4e3a\u79c1\u6709 // \u7ee7\u627f\u6765\u7684\u53d7\u4fdd\u62a4\u6210\u5458\u53d8\u4e3a\u79c1\u6709 // \u7ee7\u627f\u6765\u7684\u516c\u6709\u6210\u5458\u4ecd\u7136\u662f\u4e0d\u53ef\u8bbf\u95ee\u7684 public : int m_public2 {}; protected : int m_protected2 {}; private : int m_private2 {}; }; D2 \u53ef\u4ee5\u4e0d\u53d7\u9650\u5236\u5730\u8bbf\u95ee\u81ea\u5df1\u7684\u6210\u5458\u3002 D2 \u53ef\u4ee5\u8bbf\u95ee Base \u7684 m_public \u548c m_protected \u6210\u5458\uff0c\u4f46\u4e0d\u80fd\u8bbf\u95ee m_private \u6210\u5458\u3002\u56e0\u4e3a D2 \u79c1\u6709\u5730\u7ee7\u627f\u4e86 Base \uff0c\u5f53\u901a\u8fc7 D2 \u8bbf\u95ee\u65f6\uff0c m_public \u548c m_protected \u73b0\u5728\u88ab\u8ba4\u4e3a\u662f\u79c1\u6709\u7684\u3002\u8fd9\u610f\u5473\u7740\u5728\u4f7f\u7528 D2 \u5bf9\u8c61\u65f6\uff0c\u5916\u90e8\u4ee3\u7801\u4e0d\u80fd\u8bbf\u95ee\u8fd9\u4e9b\u53d8\u91cf\uff0c \u4e5f\u4e0d\u80fd\u8bbf\u95ee\u4ece D2 \u6d3e\u751f\u7684\u4efb\u4f55\u7c7b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 class D3 : public D2 { // \u516c\u6709\u7ee7\u627f\uff1a // \u7ee7\u627f\u6765\u7684\u516c\u6709\u6210\u5458\u4ecd\u7136\u516c\u6709 // \u7ee7\u627f\u6765\u7684\u53d7\u4fdd\u62a4\u6210\u5458\u4ecd\u7136\u53d7\u4fdd\u62a4 // \u7ee7\u627f\u6765\u7684\u79c1\u6709\u6210\u5458\u4ecd\u7136\u4e0d\u53ef\u8bbf\u95ee public : int m_public3 {}; protected : int m_protected3 {}; private : int m_private3 {}; }; COPY D3 can access its own members without restriction. D3 can access D2\u2019s m_public2 and m_protected2 members, but not m_private2. Because D3 inherited D2 publicly, m_public2 and m_protected2 keep their access specifiers when accessed through D3. D3 has no access to Base\u2019s m_private, which was already private in Base. Nor does it have access to Base\u2019s m_protected or m_public, both of which became private when D2 inherited them. D3 \u53ef\u4ee5\u4e0d\u53d7\u9650\u5236\u5730\u8bbf\u95ee\u81ea\u5df1\u7684\u6210\u5458\u3002 D3 \u53ef\u4ee5\u8bbf\u95ee D2 \u7684 m_public2 \u548c m_protected2 \u6210\u5458\uff0c\u4f46\u4e0d\u80fd\u8bbf\u95ee m_private2 \u6210\u5458\u3002\u56e0\u4e3a D3 \u516c\u5f00\u7ee7\u627f\u4e86 D2 \uff0c\u6240\u4ee5\u5f53\u901a\u8fc7 D3 \u8bbf\u95ee\u65f6\uff0c m_public2 \u548c m_protected2 \u4fdd\u7559\u4e86\u5b83\u4eec\u7684\u8bbf\u95ee\u4fee\u9970\u7b26\u3002 D3 \u4e0d\u80fd\u8bbf\u95ee Base \u7684 m_private \uff0c\u5b83\u5728 Base \u4e2d\u5df2\u7ecf\u662f\u79c1\u6709\u7684\u3002\u5b83\u4e5f\u4e0d\u80fd\u8bbf\u95ee Base \u7684 m_protected \u6216 m_public \uff0c\u5f53 D2 \u7ee7\u627f\u5b83\u4eec\u65f6\uff0c\u5b83\u4eec\u90fd\u53d8\u6210\u4e86\u79c1\u6709\u3002 \u5c0f\u7ed3 \u8bbf\u95ee\u8bf4\u660e\u7b26\u3001\u7ee7\u627f\u7c7b\u578b\u548c\u6d3e\u751f\u7c7b\u7684\u4ea4\u4e92\u65b9\u5f0f\u4f1a\u5bfc\u81f4\u5f88\u591a\u6df7\u4e71\u3002\u5c3d\u91cf\u628a\u4e8b\u60c5\u5f04\u6e05\u695a: \u9996\u5148\uff0c\u7c7b(\u53ca\u5176\u53cb\u5143)\u603b\u662f\u53ef\u4ee5\u8bbf\u95ee\u81ea\u5df1\u7684\u975e\u7ee7\u627f\u6210\u5458\u3002\u8bbf\u95ee\u8bf4\u660e\u7b26\u53ea\u5f71\u54cd\u5916\u90e8\u548c\u6d3e\u751f\u7c7b\u662f\u5426\u53ef\u4ee5\u8bbf\u95ee\u8fd9\u4e9b\u6210\u5458\u3002 \u5176\u6b21\uff0c\u5f53\u6d3e\u751f\u7c7b\u7ee7\u627f\u6210\u5458\u65f6\uff0c\u8fd9\u4e9b\u6210\u5458\u53ef\u80fd\u4f1a\u66f4\u6539\u6d3e\u751f\u7c7b\u4e2d\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u3002\u8fd9\u4e0d\u4f1a\u5f71\u54cd\u6d3e\u751f\u7c7b\u81ea\u5df1\u7684(\u975e\u7ee7\u627f\u7684)\u6210\u5458(\u5b83\u4eec\u6709\u81ea\u5df1\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26)\u3002\u5b83\u53ea\u5f71\u54cd\u5916\u90e8\u4eba\u5458\u548c\u4ece\u6d3e\u751f\u7c7b\u6d3e\u751f\u7684\u7c7b\u662f\u5426\u53ef\u4ee5\u8bbf\u95ee\u90a3\u4e9b\u7ee7\u627f\u7684\u6210\u5458\u3002 \u4e0b\u9762\u662f\u6240\u6709\u8bbf\u95ee\u8bf4\u660e\u7b26\u548c\u7ee7\u627f\u7c7b\u578b\u7ec4\u5408\u7684\u8868 \u57fa\u7c7b\u4e2d\u7684\u8bbf\u95ee\u4fee\u9970\u7b26 \u516c\u6709\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u4fee\u9970\u7b26 \u79c1\u6709\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u4fee\u9970\u7b26 \u53d7\u4fdd\u62a4\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u4fee\u9970\u7b26 Public Public Private Protected Protected Protected Private Protected Private Inaccessible Inaccessible Inaccessible \u6700\u540e\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5c3d\u7ba1\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ea\u5c55\u793a\u4e86\u4f7f\u7528\u6210\u5458\u53d8\u91cf\u7684\u4f8b\u5b50\uff0c\u4f46\u8fd9\u4e9b\u8bbf\u95ee\u89c4\u5219\u5bf9\u6240\u6709\u6210\u5458\u90fd\u6210\u7acb(\u4f8b\u5982\uff0c\u5728\u7c7b\u5185\u90e8\u58f0\u660e\u7684\u6210\u5458\u51fd\u6570\u548c\u7c7b\u578b)\u3002","title":"17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26"},{"location":"17-5-inheritance-and-access-specifiers/#protected","text":"\u5f53\u914d\u5408\u7ee7\u627f\u7c7b\u5de5\u4f5c\u65f6\uff0c\u95ee\u9898\u53d8\u5f97\u590d\u6742\u4e86\u4e00\u4e9b\u3002 C++ \u63d0\u4f9b\u4e86\u7b2c\u4e09\u79cd\u8bbf\u95ee\u8bf4\u660e\u7b26\uff0c\u5b83\u53ea\u5728\u4f7f\u7528\u7ee7\u627f\u7684\u573a\u5408\u4e2d\u6709\u7528\u9014\u3002 protected \u8bbf\u95ee\u8bf4\u660e\u7b26\u5141\u8bb8\u6210\u5458\u88ab\u7c7b\u672c\u8eab\u3001\u53cb\u5143\u7c7b\u548c\u6d3e\u751f\u7c7b\u8bbf\u95ee\u3002\u4f46\u662f\uff0c \u53d7\u4fdd\u62a4\u6210\u5458 \u4e0d\u80fd\u88ab\u5916\u90e8\u7c7b\u8bbf\u95ee\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 class Base { public : int m_public {}; // \u53ef\u4ee5\u88ab\u4efb\u4f55\u4eba\u8bbf\u95ee protected : int m_protected {}; // \u53ef\u4ee5\u88ab Base\u3001\u53cb\u5143\u7c7b\u548c\u6d3e\u751f\u7c7b\u8bbf\u95ee private : int m_private {}; // \u53ea\u80fd\u88ab Base \u548c\u53cb\u5143\u8bbf\u95ee\uff08\u4e0d\u80fd\u88ab\u6d3e\u751f\u7c7b\u76f4\u63a5\u8bbf\u95ee\uff09 }; class Derived : public Base { public : Derived () { m_public = 1 ; // \u5141\u8bb8: \u53ef\u4ee5\u5728\u6d3e\u751f\u7c7b\u4e2d\u8bbf\u95ee\u57fa\u7c7b\u7684\u516c\u6709\u6210\u5458 m_protected = 2 ; // \u5141\u8bb8: \u53ef\u4ee5\u5728\u6d3e\u751f\u7c7b\u4e2d\u8bbf\u95ee\u57fa\u7c7b\u4e2d\u7684\u53d7\u4fdd\u62a4\u6210\u5458 m_private = 3 ; // \u4e0d\u5141\u8bb8\uff1a\u4e0d\u80fd\u5728\u6d3e\u751f\u7c7b\u4e2d\u8bbf\u95ee\u57fa\u7c7b\u7684\u79c1\u6709\u6210\u5458 } }; int main () { Base base ; base . m_public = 1 ; // \u5141\u8bb8: \u53ef\u4ee5\u4ece\u5916\u90e8\u7c7b\u8bbf\u95ee\u516c\u6709\u6210\u5458 base . m_protected = 2 ; // \u4e0d\u5141\u8bb8: \u4e0d\u80fd\u591f\u4ece\u5916\u90e8\u7c7b\u8bbf\u95ee\u516c\u6709\u6210\u5458 base . m_private = 3 ; // \u4e0d\u5141\u8bb8\uff1a\u4e0d\u80fd\u591f\u4ece\u5916\u90e8\u8bbf\u95ee\u79c1\u6709\u6210\u5458 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u53d7\u4fdd\u62a4\u7684\u57fa\u7c7b\u7684\u6210\u5458 m_protected \u53ef\u4ee5\u76f4\u63a5\u7531\u6d3e\u751f\u7c7b\u8bbf\u95ee\uff0c\u4f46\u4e0d\u80fd\u5728\u5916\u90e8\u88ab\u8bbf\u95ee\u3002","title":"protected \u8bbf\u95ee\u8bf4\u660e\u7b26"},{"location":"17-5-inheritance-and-access-specifiers/#protected_1","text":"\u57fa\u7c7b\u4e2d\u7684 \u53d7\u4fdd\u62a4\u6210\u5458 \u53ef\u4ee5\u76f4\u63a5\u88ab\u6d3e\u751f\u7c7b\u8bbf\u95ee\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u4f60\u4ee5\u540e\u66f4\u6539\u4e86\u53d7\u4fdd\u62a4\u5c5e\u6027\u7684\u4efb\u4f55\u5185\u5bb9(\u7c7b\u578b\u3001\u503c\u7684\u542b\u4e49\u7b49\u7b49)\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u540c\u65f6\u66f4\u6539\u57fa\u7c7b\u548c\u6240\u6709\u6d3e\u751f\u7c7b\u3002 \u56e0\u6b64\uff0c\u53d7\u4fdd\u62a4\u8bbf\u95ee\u8bf4\u660e\u7b26\u9002\u7528\u4e8e\u4f60\u4ece\u81ea\u5df1\u7f16\u5199\u7c7b\u4e2d\u7ee7\u627f\u5e76\u4e14\u6d3e\u751f\u7c7b\u6570\u91cf\u5e76\u4e0d\u591a\u7684\u60c5\u51b5\u3002\u8fd9\u6837\uff0c\u5982\u679c\u4f60\u5bf9\u57fa\u7c7b\u7684\u5b9e\u73b0\u8fdb\u884c\u4e86\u66f4\u6539\uff0c\u5e76\u4e14\u56e0\u6b64\u9700\u8981\u5bf9\u6d3e\u751f\u7c7b\u8fdb\u884c\u66f4\u65b0\uff0c\u90a3\u4e48\u53ef\u4ee5\u81ea\u5df1\u8fdb\u884c\u66f4\u65b0(\u5e76\u4e14\u4e0d\u4f1a\u82b1\u8d39\u592a\u957f\u65f6\u95f4\uff0c\u56e0\u4e3a\u6d3e\u751f\u7c7b\u7684\u6570\u91cf\u662f\u6709\u9650\u7684)\u3002 \u5c06\u6210\u5458\u8bbe\u4e3a\u79c1\u6709\u610f\u5473\u7740\u516c\u5171\u7c7b\u548c\u6d3e\u751f\u7c7b\u4e0d\u80fd\u76f4\u63a5\u5bf9\u57fa\u7c7b\u8fdb\u884c\u66f4\u6539\u3002\u8fd9\u6709\u5229\u4e8e\u5c06\u516c\u5171\u7c7b\u6216\u6d3e\u751f\u7c7b\u4e0e\u5b9e\u73b0\u66f4\u6539\u9694\u79bb\u5f00\u6765\uff0c\u5e76\u786e\u4fdd\u9002\u5f53\u5730\u7ef4\u62a4\u4e0d\u53d8\u91cf\u3002\u4f46\u662f\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u4f60\u7684\u7c7b\u53ef\u80fd\u9700\u8981\u4e00\u4e2a\u66f4\u5927\u7684\u516c\u5f00(\u6216\u53d7\u4fdd\u62a4\u7684)\u63a5\u53e3\u6765\u652f\u6301\u516c\u5171\u6216\u6d3e\u751f\u7c7b\u64cd\u4f5c\u6240\u9700\u7684\u6240\u6709\u529f\u80fd\uff0c\u8be5\u63a5\u53e3\u6709\u5176\u81ea\u8eab\u7684\u6784\u5efa\u3001\u6d4b\u8bd5\u548c\u7ef4\u62a4\u6210\u672c\u3002 \u4e00\u822c\u6765\u8bf4\uff0c\u5982\u679c\u53ef\u4ee5\u7684\u8bdd\uff0c\u6700\u597d\u5c06\u6210\u5458\u8bbe\u4e3a\u79c1\u6709\u7684\uff0c\u53ea\u6709\u51c6\u5907\u521b\u5efa\u6d3e\u751f\u7c7b\u4e14\u6784\u5efa\u548c\u7ef4\u62a4\u5230\u90a3\u4e9b\u79c1\u6709\u6210\u5458\u7684\u63a5\u53e3\u7684\u6210\u672c\u8fc7\u9ad8\u65f6\u624d\u4f7f\u7528\u53d7 protected \u8bbf\u95ee\u8bf4\u660e\u7b26\u3002 \u6269\u5c55\u9605\u8bfb \u4f18\u5148\u4f7f\u7528\u79c1\u6709\u6210\u5458\u800c\u4e0d\u662f\u53d7\u4fdd\u62a4\u6210\u5458\u3002","title":"\u90a3\u4e48\uff0c\u5e94\u8be5\u5728\u4f55\u65f6\u4f7f\u7528protected\u8bbf\u95ee\u8bf4\u660e\u7b26\uff1f"},{"location":"17-5-inheritance-and-access-specifiers/#_1","text":"\u9996\u5148\uff0c\u7ee7\u627f\u7c7b\u578b\u6709\u4e09\u79cd\uff1a \u516c\u5f00\u7ee7\u627f \u3001 \u53d7\u4fdd\u62a4\u7ee7\u627f \u548c \u79c1\u6709\u7ee7\u627f \u3002 \u53ea\u9700\u5728\u7ee7\u627f\u7c7b\u65f6\u6307\u5b9a\u9700\u8981\u7684\u8bbf\u95ee\u7c7b\u578b\u5c31\u53ef\u4ee5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // Inherit from Base publicly class Pub : public Base { }; // Inherit from Base protectedly class Pro : protected Base { }; // Inherit from Base privately class Pri : private Base { }; class Def : Base // Defaults to private inheritance { }; \u5982\u679c\u4e0d\u9009\u62e9\u7ee7\u627f\u7c7b\u578b\uff0cC++\u9ed8\u8ba4\u4e3a\u79c1\u6709\u7ee7\u627f(\u5c31\u50cf\u5982\u679c\u4e0d\u6307\u5b9a\u5176\u4ed6\u7c7b\u578b\uff0c\u6210\u5458\u9ed8\u8ba4\u4e3a\u79c1\u6709\u8bbf\u95ee\u4e00\u6837)\u3002 \u8fd9\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e869\u79cd\u7ec4\u5408\uff1a3\u4e2a \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 (\u516c\u5171\u3001\u79c1\u6709\u548c\u53d7\u4fdd\u62a4)\u548c3\u79cd\u7ee7\u627f\u7c7b\u578b(\u516c\u5171\u3001\u79c1\u6709\u548c\u53d7\u4fdd\u62a4)\u3002 \u90a3\u4e48\u5b83\u4eec\u7684\u533a\u522b\u662f\u4ec0\u4e48\u5462\uff1f\u7b80\u800c\u8a00\u4e4b\uff0c\u5f53\u6210\u5458\u88ab\u7ee7\u627f\u65f6\uff0c\u7ee7\u627f\u6210\u5458\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u53ef\u4ee5\u6839\u636e\u6240\u4f7f\u7528\u7684\u7ee7\u627f\u7c7b\u578b\u66f4\u6539(\u4ec5\u5728\u6d3e\u751f\u7c7b\u4e2d)\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u57fa\u7c7b\u4e2d\u516c\u5171\u7684\u6216\u53d7\u4fdd\u62a4\u7684\u6210\u5458\uff0c\u5728\u6d3e\u751f\u7c7b\u4e2d\u53ef\u80fd\u662f\u5176\u4ed6\u7c7b\u578b\u7684\u3002 \u8fd9\u53ef\u80fd\u770b\u8d77\u6765\u6709\u70b9\u4ee4\u4eba\u56f0\u60d1\uff0c\u4e0d\u8fc7\u4e0d\u8981\u62c5\u5fc3\uff0c\u8fd9\u8282\u8bfe\u5269\u4e0b\u7684\u65f6\u95f4\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e2a\u95ee\u9898\u3002 \u5728\u6211\u4eec\u9010\u6b65\u5b66\u4e60\u8fd9\u4e9b\u4f8b\u5b50\u65f6\uff0c\u8bf7\u8bb0\u4f4f\u4ee5\u4e0b\u89c4\u5219: \u4e00\u4e2a\u7c7b\u603b\u662f\u80fd\u8bbf\u95ee\u5176\u81ea\u5df1\u7684\u6210\u5458\uff08\u975e\u7ee7\u627f\u800c\u6765\u7684\uff09\uff1b \u5916\u90e8\u5728\u8bbf\u95ee\u4e00\u4e2a\u7c7b\u7684\u6210\u5458\u65f6\uff0c\u57fa\u4e8e\u8be5\u6210\u5458\u7684\u8bbf\u95ee\u4fee\u9970\u7b26\u5224\u65ad\u662f\u5426\u80fd\u8bbf\u95ee\uff1b \u6d3e\u751f\u7c7b\u5728\u8bbf\u95ee\u7ee7\u627f\u6765\u7684\u6210\u5458\u65f6\uff0c\u57fa\u4e8e\u5b83\u4ece\u57fa\u7c7b\u7ee7\u627f\u6765\u7684\u8bbf\u95ee\u4fee\u9970\u7b26\u3002\u8fd9\u4e00\u70b9\u4f1a\u6839\u636e\u8bbf\u95ee\u8bf4\u660e\u7b26\u7684\u4e0d\u540c\u548c\u7ee7\u627f\u7c7b\u578b\u7684\u4e0d\u540c\u800c\u6539\u53d8\u3002","title":"\u4e0d\u540c\u7c7b\u578b\u7684\u7ee7\u627f\u4ee5\u53ca\u5b83\u4eec\u5bf9\u6210\u5458\u8bbf\u95ee\u7684\u5f71\u54cd"},{"location":"17-5-inheritance-and-access-specifiers/#_2","text":"\u516c\u5f00\u7ee7\u627f \u662f\u6700\u5e38\u7528\u7684\u7ee7\u627f\u7c7b\u578b\u3002\u4e8b\u5b9e\u4e0a\uff0c\u4f60\u5f88\u5c11\u4f1a\u770b\u5230\u6216\u4f7f\u7528\u5176\u4ed6\u7c7b\u578b\u7684\u7ee7\u627f\uff0c\u56e0\u6b64\u4f60\u7684\u4e3b\u8981\u5173\u6ce8\u70b9\u5e94\u8be5\u653e\u5728\u7406\u89e3\u8fd9\u4e00\u8282\u4e0a\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u516c\u5f00\u7ee7\u627f\u4e5f\u662f\u6700\u5bb9\u6613\u7406\u89e3\u7684\u3002\u5f53\u516c\u5f00\u7ee7\u627f\u57fa\u7c7b\u65f6\uff0c\u7ee7\u627f\u7684 \u516c\u6709\u6210\u5458 \u4fdd\u6301\u516c\u6709\uff0c\u7ee7\u627f\u7684 \u53d7\u4fdd\u62a4\u6210\u5458 \u4fdd\u6301\u53d7\u4fdd\u62a4\u3002\u7ee7\u627f\u7684\u79c1\u6709\u6210\u5458\u7531\u4e8e\u5728\u57fa\u7c7b\u4e2d\u662f\u79c1\u6709\u7684\u800c\u4e0d\u53ef\u8bbf\u95ee\uff0c\u56e0\u6b64\u4ecd\u7136\u4e0d\u53ef\u8bbf\u95ee\u3002 \u57fa\u7c7b\u4e2d\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u516c\u6709\u7ee7\u627f\u540e\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 Public Public Protected Protected Private Inaccessible \u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u6765\u8bf4\u660e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 class Base { public : int m_public {}; protected : int m_protected {}; private : int m_private {}; }; class Pub : public Base // \u6ce8\u610f: \u516c\u5f00\u7ee7\u627f { // \u516c\u6709\u7ee7\u627f\u610f\u5473\u7740: // \u516c\u6709\u7ee7\u627f\u7684\u6210\u5458\u4fdd\u6301\u516c\u6709 (m_public \u88ab\u5f53\u505a public \u7684) // \u516c\u6709\u7ee7\u627f\u6765\u7684\u53d7\u4fdd\u62a4\u6210\u5458\u4ecd\u7136\u662f\u53d7\u4fdd\u62a4\u7684( m_protected \u662f protected \u7684) // \u516c\u6709\u7ee7\u627f\u6765\u7684\u79c1\u6709\u6210\u5458\u662f\u4e0d\u53ef\u8bbf\u95ee\u7684( m_private \u4e0d\u53ef\u8bbf\u95ee) public : Pub () { m_public = 1 ; // okay: m_public \u53ef\u4ee5\u88ab\u8bbf\u95ee m_protected = 2 ; // okay: m_protected \u53ef\u4ee5\u88ab\u8bbf\u95ee m_private = 3 ; // not okay: m_private \u4e0d\u53ef\u4ee5\u88ab\u6d3e\u751f\u7c7b\u8bbf\u95ee } }; int main () { // \u5916\u90e8\u8bbf\u95ee\u65f6\u57fa\u4e8e\u88ab\u8bbf\u95ee\u7c7b\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u5224\u65ad Base base ; base . m_public = 1 ; // \u53ef\u4ee5: m_public \u5728 Base \u4e2d\u662f\u516c\u6709\u7684 base . m_protected = 2 ; //\u4e0d\u53ef\u4ee5 : m_protected \u5728 Base \u4e2d\u662f\u53d7\u4fdd\u62a4\u7684 base . m_private = 3 ; // \u4e0d\u53ef\u4ee5: m_private \u5728 Base \u4e2d\u662f\u79c1\u6709\u7684 Pub pub ; pub . m_public = 1 ; // \u53ef\u4ee5: m_public \u5728 Pub \u4e2d\u662f\u516c\u6709\u7684 pub . m_protected = 2 ; // \u4e0d\u53ef\u4ee5 : m_protected \u5728 Pub \u4e2d\u662f\u53d7\u4fdd\u62a4\u7684 pub . m_private = 3 ; // \u4e0d\u53ef\u4ee5: m_private \u5728 Pub \u4e2d\u662f\u4e0d\u53ef\u4ee5\u8bbf\u95ee\u7684 return 0 ; } \u8fd9\u4e0e\u4e0a\u9762\u6211\u4eec\u4ecb\u7ecd\u53d7\u4fdd\u62a4\u8bbf\u95ee\u8bf4\u660e\u7b26\u7684\u793a\u4f8b\u76f8\u540c\uff0c\u4e0d\u540c\u7684\u662f\uff0c\u6211\u4eec\u4e5f\u5b9e\u4f8b\u5316\u4e86\u6d3e\u751f\u7c7b\uff0c\u53ea\u662f\u4e3a\u4e86\u8868\u660e\u4f7f\u7528\u516c\u6709\u7ee7\u627f\uff0c\u57fa\u7c7b\u548c\u6d3e\u751f\u7c7b\u7684\u5de5\u4f5c\u65b9\u5f0f\u662f\u76f8\u540c\u7684\u3002 \u9664\u975e\u6709\u7279\u5b9a\u7684\u539f\u56e0\uff0c\u5426\u5219\u4f60\u5e94\u8be5\u4f7f\u7528\u516c\u5171\u7ee7\u627f\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u516c\u6709\u7ee7\u627f\uff0c\u9664\u975e\u4f60\u6709\u7279\u6b8a\u7684\u7406\u7531\u4e0d\u8fd9\u6837\u505a\u3002","title":"\u516c\u5f00\u7ee7\u627f"},{"location":"17-5-inheritance-and-access-specifiers/#_3","text":"\u53d7\u4fdd\u62a4\u7684\u7ee7\u627f\u662f\u6700\u4e0d\u5e38\u89c1\u7684\u7ee7\u627f\u65b9\u6cd5\u3002\u9664\u4e86\u5728\u975e\u5e38\u7279\u6b8a\u7684\u60c5\u51b5\u4e0b\uff0c\u5b83\u51e0\u4e4e\u4ece\u4e0d\u88ab\u4f7f\u7528\u3002\u901a\u8fc7\u53d7\u4fdd\u62a4\u7684\u7ee7\u627f\uff0c\u516c\u5171\u6210\u5458\u548c\u53d7\u4fdd\u62a4\u6210\u5458\u6210\u4e3a\u53d7\u4fdd\u62a4\u6210\u5458\uff0c\u800c\u79c1\u6709\u6210\u5458\u4fdd\u6301\u4e0d\u53ef\u8bbf\u95ee\u72b6\u6001\u3002 \u56e0\u4e3a\u8fd9\u79cd\u5f62\u5f0f\u7684\u7ee7\u627f\u975e\u5e38\u7f55\u89c1\uff0c\u6211\u4eec\u5c06\u8df3\u8fc7\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u53ea\u4f7f\u7528\u4e00\u4e2a\u8868\u8fdb\u884c\u603b\u7ed3: \u57fa\u7c7b\u4e2d\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u53d7\u4fdd\u62a4\u7ee7\u627f\u540e\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 Public Protected Protected Protected Private Inaccessible","title":"\u53d7\u4fdd\u62a4\u7ee7\u627f"},{"location":"17-5-inheritance-and-access-specifiers/#_4","text":"\u4f7f\u7528\u79c1\u6709\u7ee7\u627f\uff0c\u57fa\u7c7b\u7684\u6240\u6709\u6210\u5458\u90fd\u88ab\u7ee7\u627f\u4e3a\u79c1\u6709\u3002\u8fd9\u610f\u5473\u7740\u79c1\u6709\u6210\u5458\u662f\u4e0d\u53ef\u8bbf\u95ee\u7684\uff0c\u53d7\u4fdd\u62a4\u7684\u548c\u516c\u5171\u6210\u5458\u4e5f\u53d8\u6210\u79c1\u6709\u3002 \u6ce8\u610f\uff0c\u8fd9\u4e0d\u4f1a\u5f71\u54cd\u6d3e\u751f\u7c7b\u8bbf\u95ee\u4ece\u5176\u7236\u7c7b\u7ee7\u627f\u7684\u6210\u5458\u7684\u65b9\u5f0f\uff01\u5b83\u53ea\u5f71\u54cd\u8bd5\u56fe\u901a\u8fc7\u6d3e\u751f\u7c7b\u8bbf\u95ee\u8fd9\u4e9b\u6210\u5458\u7684\u4ee3\u7801\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 class Base { public : int m_public {}; protected : int m_protected {}; private : int m_private {}; }; class Pri : private Base // note: private inheritance { // Private inheritance means: // Public inherited members become private (so m_public is treated as private) // Protected inherited members become private (so m_protected is treated as private) // Private inherited members stay inaccessible (so m_private is inaccessible) public : Pri () { m_public = 1 ; // okay: m_public is now private in Pri m_protected = 2 ; // okay: m_protected is now private in Pri m_private = 3 ; // not okay: derived classes can't access private members in the base class } }; int main () { // Outside access uses the access specifiers of the class being accessed. // In this case, the access specifiers of base. Base base ; base . m_public = 1 ; // okay: m_public is public in Base base . m_protected = 2 ; // not okay: m_protected is protected in Base base . m_private = 3 ; // not okay: m_private is private in Base Pri pri ; pri . m_public = 1 ; // not okay: m_public is now private in Pri pri . m_protected = 2 ; // not okay: m_protected is now private in Pri pri . m_private = 3 ; // not okay: m_private is inaccessible in Pri return 0 ; } \u603b\u7ed3\u5982\u4e0b\uff1a \u57fa\u7c7b\u4e2d\u7684\u8bbf\u95ee\u4fee\u9970\u7b26 \u79c1\u6709\u7ee7\u627f\u540e\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 Public Private Protected Private Private Inaccessible \u5f53\u6d3e\u751f\u7c7b\u4e0e\u57fa\u7c7b\u6ca1\u6709\u660e\u663e\u7684\u5173\u7cfb\uff0c\u4f46\u5728\u5185\u90e8\u4f7f\u7528\u57fa\u7c7b\u5b9e\u73b0\u65f6\uff0c\u79c1\u6709\u7ee7\u627f\u53ef\u80fd\u5f88\u6709\u7528\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u4e0d\u5e0c\u671b\u57fa\u7c7b\u7684\u516c\u5171\u63a5\u53e3\u901a\u8fc7\u6d3e\u751f\u7c7b\u7684\u5bf9\u8c61\u516c\u5f00(\u5982\u679c\u516c\u5f00\u7ee7\u627f\u5c31\u4f1a\u8fd9\u6837)\u3002 \u4e0d\u8fc7\uff0c\u5728\u5b9e\u8df5\u4e2d\uff0c\u5f88\u5c11\u4f7f\u7528\u79c1\u6709\u7ee7\u627f\u3002","title":"\u79c1\u6709\u7ee7\u627f"},{"location":"17-5-inheritance-and-access-specifiers/#_5","text":"1 2 3 4 5 6 7 8 9 class Base { public : int m_public {}; protected : int m_protected {}; private : int m_private {}; }; Base \u5728\u8bbf\u95ee\u81ea\u5df1\u7684\u6210\u5458\u65f6\uff0c\u6ca1\u6709\u4efb\u4f55\u9650\u5236\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5916\u90e8\u4ee3\u7801\u53ea\u80fd\u8bbf\u95ee m_public \u3002\u6d3e\u751f\u7c7b\u53ef\u4ee5\u8bbf\u95ee m_public \u548c m_protected \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 class D2 : private Base // note: private inheritance { // \u79c1\u6709\u7ee7\u627f\uff1a // \u7ee7\u627f\u6765\u7684\u516c\u6709\u6210\u5458\u53d8\u4e3a\u79c1\u6709 // \u7ee7\u627f\u6765\u7684\u53d7\u4fdd\u62a4\u6210\u5458\u53d8\u4e3a\u79c1\u6709 // \u7ee7\u627f\u6765\u7684\u516c\u6709\u6210\u5458\u4ecd\u7136\u662f\u4e0d\u53ef\u8bbf\u95ee\u7684 public : int m_public2 {}; protected : int m_protected2 {}; private : int m_private2 {}; }; D2 \u53ef\u4ee5\u4e0d\u53d7\u9650\u5236\u5730\u8bbf\u95ee\u81ea\u5df1\u7684\u6210\u5458\u3002 D2 \u53ef\u4ee5\u8bbf\u95ee Base \u7684 m_public \u548c m_protected \u6210\u5458\uff0c\u4f46\u4e0d\u80fd\u8bbf\u95ee m_private \u6210\u5458\u3002\u56e0\u4e3a D2 \u79c1\u6709\u5730\u7ee7\u627f\u4e86 Base \uff0c\u5f53\u901a\u8fc7 D2 \u8bbf\u95ee\u65f6\uff0c m_public \u548c m_protected \u73b0\u5728\u88ab\u8ba4\u4e3a\u662f\u79c1\u6709\u7684\u3002\u8fd9\u610f\u5473\u7740\u5728\u4f7f\u7528 D2 \u5bf9\u8c61\u65f6\uff0c\u5916\u90e8\u4ee3\u7801\u4e0d\u80fd\u8bbf\u95ee\u8fd9\u4e9b\u53d8\u91cf\uff0c \u4e5f\u4e0d\u80fd\u8bbf\u95ee\u4ece D2 \u6d3e\u751f\u7684\u4efb\u4f55\u7c7b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 class D3 : public D2 { // \u516c\u6709\u7ee7\u627f\uff1a // \u7ee7\u627f\u6765\u7684\u516c\u6709\u6210\u5458\u4ecd\u7136\u516c\u6709 // \u7ee7\u627f\u6765\u7684\u53d7\u4fdd\u62a4\u6210\u5458\u4ecd\u7136\u53d7\u4fdd\u62a4 // \u7ee7\u627f\u6765\u7684\u79c1\u6709\u6210\u5458\u4ecd\u7136\u4e0d\u53ef\u8bbf\u95ee public : int m_public3 {}; protected : int m_protected3 {}; private : int m_private3 {}; }; COPY D3 can access its own members without restriction. D3 can access D2\u2019s m_public2 and m_protected2 members, but not m_private2. Because D3 inherited D2 publicly, m_public2 and m_protected2 keep their access specifiers when accessed through D3. D3 has no access to Base\u2019s m_private, which was already private in Base. Nor does it have access to Base\u2019s m_protected or m_public, both of which became private when D2 inherited them. D3 \u53ef\u4ee5\u4e0d\u53d7\u9650\u5236\u5730\u8bbf\u95ee\u81ea\u5df1\u7684\u6210\u5458\u3002 D3 \u53ef\u4ee5\u8bbf\u95ee D2 \u7684 m_public2 \u548c m_protected2 \u6210\u5458\uff0c\u4f46\u4e0d\u80fd\u8bbf\u95ee m_private2 \u6210\u5458\u3002\u56e0\u4e3a D3 \u516c\u5f00\u7ee7\u627f\u4e86 D2 \uff0c\u6240\u4ee5\u5f53\u901a\u8fc7 D3 \u8bbf\u95ee\u65f6\uff0c m_public2 \u548c m_protected2 \u4fdd\u7559\u4e86\u5b83\u4eec\u7684\u8bbf\u95ee\u4fee\u9970\u7b26\u3002 D3 \u4e0d\u80fd\u8bbf\u95ee Base \u7684 m_private \uff0c\u5b83\u5728 Base \u4e2d\u5df2\u7ecf\u662f\u79c1\u6709\u7684\u3002\u5b83\u4e5f\u4e0d\u80fd\u8bbf\u95ee Base \u7684 m_protected \u6216 m_public \uff0c\u5f53 D2 \u7ee7\u627f\u5b83\u4eec\u65f6\uff0c\u5b83\u4eec\u90fd\u53d8\u6210\u4e86\u79c1\u6709\u3002","title":"\u6700\u540e\u4e00\u4e2a\u4f8b\u5b50"},{"location":"17-5-inheritance-and-access-specifiers/#_6","text":"\u8bbf\u95ee\u8bf4\u660e\u7b26\u3001\u7ee7\u627f\u7c7b\u578b\u548c\u6d3e\u751f\u7c7b\u7684\u4ea4\u4e92\u65b9\u5f0f\u4f1a\u5bfc\u81f4\u5f88\u591a\u6df7\u4e71\u3002\u5c3d\u91cf\u628a\u4e8b\u60c5\u5f04\u6e05\u695a: \u9996\u5148\uff0c\u7c7b(\u53ca\u5176\u53cb\u5143)\u603b\u662f\u53ef\u4ee5\u8bbf\u95ee\u81ea\u5df1\u7684\u975e\u7ee7\u627f\u6210\u5458\u3002\u8bbf\u95ee\u8bf4\u660e\u7b26\u53ea\u5f71\u54cd\u5916\u90e8\u548c\u6d3e\u751f\u7c7b\u662f\u5426\u53ef\u4ee5\u8bbf\u95ee\u8fd9\u4e9b\u6210\u5458\u3002 \u5176\u6b21\uff0c\u5f53\u6d3e\u751f\u7c7b\u7ee7\u627f\u6210\u5458\u65f6\uff0c\u8fd9\u4e9b\u6210\u5458\u53ef\u80fd\u4f1a\u66f4\u6539\u6d3e\u751f\u7c7b\u4e2d\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u3002\u8fd9\u4e0d\u4f1a\u5f71\u54cd\u6d3e\u751f\u7c7b\u81ea\u5df1\u7684(\u975e\u7ee7\u627f\u7684)\u6210\u5458(\u5b83\u4eec\u6709\u81ea\u5df1\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26)\u3002\u5b83\u53ea\u5f71\u54cd\u5916\u90e8\u4eba\u5458\u548c\u4ece\u6d3e\u751f\u7c7b\u6d3e\u751f\u7684\u7c7b\u662f\u5426\u53ef\u4ee5\u8bbf\u95ee\u90a3\u4e9b\u7ee7\u627f\u7684\u6210\u5458\u3002 \u4e0b\u9762\u662f\u6240\u6709\u8bbf\u95ee\u8bf4\u660e\u7b26\u548c\u7ee7\u627f\u7c7b\u578b\u7ec4\u5408\u7684\u8868 \u57fa\u7c7b\u4e2d\u7684\u8bbf\u95ee\u4fee\u9970\u7b26 \u516c\u6709\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u4fee\u9970\u7b26 \u79c1\u6709\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u4fee\u9970\u7b26 \u53d7\u4fdd\u62a4\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u4fee\u9970\u7b26 Public Public Private Protected Protected Protected Private Protected Private Inaccessible Inaccessible Inaccessible \u6700\u540e\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5c3d\u7ba1\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ea\u5c55\u793a\u4e86\u4f7f\u7528\u6210\u5458\u53d8\u91cf\u7684\u4f8b\u5b50\uff0c\u4f46\u8fd9\u4e9b\u8bbf\u95ee\u89c4\u5219\u5bf9\u6240\u6709\u6210\u5458\u90fd\u6210\u7acb(\u4f8b\u5982\uff0c\u5728\u7c7b\u5185\u90e8\u58f0\u660e\u7684\u6210\u5458\u51fd\u6570\u548c\u7c7b\u578b)\u3002","title":"\u5c0f\u7ed3"},{"location":"17-6-adding-new-functionality-to-a-derived-class/","tags":["inheritance"],"text":"Key Takeaway - \u5728 17.1 - \u7ee7\u627f\u7b80\u4ecb \u4e2d\u6211\u4eec\u63d0\u5230\uff0c\u4f7f\u7528\u6d3e\u751f\u7c7b\u6700\u5927\u7684\u597d\u5904\u5728\u4e8e\u53ef\u4ee5\u91cd\u7528\u4ee3\u7801\u3002\u4f60\u53ef\u4ee5\u7ee7\u627f\u57fa\u7c7b\u7684\u529f\u80fd\u5e76\u5728\u6d3e\u751f\u7c7b\u4e2d\u6dfb\u52a0\u9700\u8981\u7684\u65b0\u529f\u80fd\u3001\u4fee\u6539\u5df2\u7ecf\u5b58\u5728\u7684\u529f\u80fd\u6216\u8005\u9690\u85cf\u4f60\u4e0d\u9700\u8981\u7684\u529f\u80fd\u3002\u5728\u63a5\u4e0b\u6765\u7684\u51e0\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u505a\u5230\u8fd9\u4e9b\u3002 \u9996\u5148\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include class Base { protected : int m_value {}; public : Base ( int value ) : m_value { value } { } void identify () const { std :: cout << \"I am a Base \\n \" ; } }; \u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7ee7\u627f\u81ea Base \u7684\u6d3e\u751f\u7c7b\u3002\u56e0\u4e3a\u6211\u4eec\u5e0c\u671b\u6d3e\u751f\u7c7b\u80fd\u591f\u5728\u6d3e\u751f\u5bf9\u8c61\u5b9e\u4f8b\u5316\u65f6\u8bbe\u7f6e m_value \u7684\u503c\uff0c\u6240\u4ee5\u9700\u8981\u5728 Derived \u7684 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u4e2d\u8c03\u7528 Base \u7684\u6784\u9020\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 class Derived : public Base { public : Derived ( int value ) : Base { value } { } }; \u5411\u6d3e\u751f\u7c7b\u4e2d\u6dfb\u52a0\u65b0\u7684\u529f\u80fd \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u8bbf\u95ee Base \u7c7b\u7684\u6e90\u4ee3\u7801\uff0c\u6240\u4ee5\u5982\u679c\u9700\u8981\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5411 Base \u6dfb\u52a0\u529f\u80fd\u3002 \u6709\u65f6\u5019\uff0c\u867d\u7136\u57fa\u7c7b\u7684\u4ee3\u7801\u53ef\u4ee5\u8bbf\u95ee\uff0c\u4f46\u6211\u4eec\u4e0d\u60f3\u4fee\u6539\u5b83\u3002\u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u60a8\u521a\u521a\u4ece\u7b2c\u4e09\u65b9\u4f9b\u5e94\u5546\u8d2d\u4e70\u4e86\u4e00\u4e2a\u4ee3\u7801\u5e93\uff0c\u4f46\u9700\u8981\u4e00\u4e9b\u989d\u5916\u7684\u529f\u80fd\u3002\u60a8\u53ef\u4ee5\u6dfb\u52a0\u5230\u539f\u59cb\u4ee3\u7801\u4e2d\uff0c\u4f46\u8fd9\u4e0d\u662f\u6700\u597d\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5982\u679c\u4f9b\u5e94\u5546\u8fdc\u7a0b\u53d1\u5e03\u4e86\u65b0\u7248\u672c\u600e\u4e48\u529e\uff1f\u6dfb\u52a0\u7684\u5185\u5bb9\u8981\u4e48\u88ab\u8986\u76d6\uff0c\u8981\u4e48\u5fc5\u987b\u624b\u52a8\u5c06\u5b83\u4eec\u8fc1\u79fb\u5230\u66f4\u65b0\u4e2d\uff0c\u8fd9\u65e2\u8017\u65f6\u53c8\u6709\u98ce\u9669\u3002 \u6216\u8005\uff0c\u6709\u65f6\u751a\u81f3\u4e0d\u53ef\u80fd\u4fee\u6539\u57fa\u7c7b\u3002\u8003\u8651\u6807\u51c6\u5e93\u4e2d\u7684\u4ee3\u7801\u3002\u6211\u4eec\u65e0\u6cd5\u4fee\u6539\u4f5c\u4e3a\u6807\u51c6\u5e93\u4e00\u90e8\u5206\u7684\u4ee3\u7801\u3002\u4f46\u662f\u6211\u4eec\u80fd\u591f\u4ece\u8fd9\u4e9b\u7c7b\u7ee7\u627f\uff0c\u7136\u540e\u5c06\u6211\u4eec\u81ea\u5df1\u7684\u529f\u80fd\u6dfb\u52a0\u5230\u6211\u4eec\u7684\u6d3e\u751f\u7c7b\u4e2d\u3002\u540c\u6837\u7684\u60c5\u51b5\u4e5f\u53d1\u751f\u5728\u7b2c\u4e09\u65b9\u5e93\u4e2d\uff0c\u5728\u90a3\u91cc\u4f60\u53ef\u4ee5\u8bbf\u95ee\u5934\u6587\u4ef6\uff0c\u4f46\u662f\u4ee3\u7801\u662f\u9884\u7f16\u8bd1\u7684\u3002 \u5728\u4efb\u4f55\u4e00\u79cd\u60c5\u51b5\u4e0b\uff0c\u6700\u597d\u7684\u7b54\u6848\u662f\u521b\u5efa\u4e00\u4e2a\u81ea\u5df1\u7684\u6d3e\u751f\u7c7b\uff0c\u5e76\u5411\u6d3e\u751f\u7c7b\u6dfb\u52a0\u4f60\u60f3\u8981\u7684\u529f\u80fd\u3002 \u57fa\u7c7b\u4e2d\u6ca1\u6709\u5305\u62ec\u7528\u4e8e\u516c\u5171\u4ee3\u7801\u8bbf\u95ee m_value \u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5728\u57fa\u7c7b\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u8bbf\u95ee\u51fd\u6570\u6765\u8865\u6551\u8fd9\u4e2a\u95ee\u9898\u2014\u2014\u4f46\u4e3a\u4e86\u4e3e\u4f8b\uff0c\u6211\u4eec\u5c06\u628a\u5b83\u6dfb\u52a0\u5230\u6d3e\u751f\u7c7b\u4e2d\u3002\u56e0\u4e3a m_value \u5728\u57fa\u7c7b\u4e2d\u88ab\u58f0\u660e\u4e3a\u53d7\u4fdd\u62a4\u7684\uff0c Derived \u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5b83\u3002 \u8981\u5411\u6d3e\u751f\u7c7b\u6dfb\u52a0\u65b0\u529f\u80fd\uff0c\u53ea\u9700\u5728\u6d3e\u751f\u7c7b\u4e2d\u50cf\u6b63\u5e38\u4e00\u6837\u58f0\u660e\u8be5\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 class Derived : public Base { public : Derived ( int value ) : Base { value } { } int getValue () const { return m_value ; } }; \u73b0\u5728 public \u5c06\u80fd\u591f\u5728 Derived \u7c7b\u578b\u7684\u5bf9\u8c61\u4e0a\u8c03\u7528 getValue() \u6765\u8bbf\u95ee m_value \u7684\u503c\u3002 1 2 3 4 5 6 7 int main () { Derived derived { 5 }; std :: cout << \"derived has value \" << derived . getValue () << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 derived has value 5 \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c Base \u5bf9\u8c61\u5e76\u4e0d\u80fd\u8bbf\u95ee Derived \u4e2d\u7684 getValue() \u51fd\u6570\uff0c\u6240\u4ee5\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u4e0d\u80fd\u5de5\u4f5c\u7684\uff1a 1 2 3 4 5 6 7 int main () { Base base { 5 }; std :: cout << \"base has value \" << base . getValue () << '\\n' ; return 0 ; } \u8fd9\u662f\u56e0\u4e3a Base \u7c7b\u4e2d\u5e76\u6ca1\u6709 getValue() \u51fd\u6570\uff0c\u5b83\u5c5e\u4e8e Derived \u7c7b\u3002\u56e0\u4e3a Derived \u662f Base \uff0c\u6240\u4ee5\u5b83\u80fd\u591f\u8bbf\u95ee Base \u7684\u5c5e\u6027\u3002\u4f46\u662f\uff0c Base \u4e0d\u80fd\u8bbf\u95ee Derived \u7684\u4efb\u4f55\u5c5e\u6027\u3002","title":"17.6 - \u4e3a\u6d3e\u751f\u7c7b\u6dfb\u52a0\u65b0\u529f\u80fd"},{"location":"17-6-adding-new-functionality-to-a-derived-class/#_1","text":"\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u8bbf\u95ee Base \u7c7b\u7684\u6e90\u4ee3\u7801\uff0c\u6240\u4ee5\u5982\u679c\u9700\u8981\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5411 Base \u6dfb\u52a0\u529f\u80fd\u3002 \u6709\u65f6\u5019\uff0c\u867d\u7136\u57fa\u7c7b\u7684\u4ee3\u7801\u53ef\u4ee5\u8bbf\u95ee\uff0c\u4f46\u6211\u4eec\u4e0d\u60f3\u4fee\u6539\u5b83\u3002\u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u60a8\u521a\u521a\u4ece\u7b2c\u4e09\u65b9\u4f9b\u5e94\u5546\u8d2d\u4e70\u4e86\u4e00\u4e2a\u4ee3\u7801\u5e93\uff0c\u4f46\u9700\u8981\u4e00\u4e9b\u989d\u5916\u7684\u529f\u80fd\u3002\u60a8\u53ef\u4ee5\u6dfb\u52a0\u5230\u539f\u59cb\u4ee3\u7801\u4e2d\uff0c\u4f46\u8fd9\u4e0d\u662f\u6700\u597d\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5982\u679c\u4f9b\u5e94\u5546\u8fdc\u7a0b\u53d1\u5e03\u4e86\u65b0\u7248\u672c\u600e\u4e48\u529e\uff1f\u6dfb\u52a0\u7684\u5185\u5bb9\u8981\u4e48\u88ab\u8986\u76d6\uff0c\u8981\u4e48\u5fc5\u987b\u624b\u52a8\u5c06\u5b83\u4eec\u8fc1\u79fb\u5230\u66f4\u65b0\u4e2d\uff0c\u8fd9\u65e2\u8017\u65f6\u53c8\u6709\u98ce\u9669\u3002 \u6216\u8005\uff0c\u6709\u65f6\u751a\u81f3\u4e0d\u53ef\u80fd\u4fee\u6539\u57fa\u7c7b\u3002\u8003\u8651\u6807\u51c6\u5e93\u4e2d\u7684\u4ee3\u7801\u3002\u6211\u4eec\u65e0\u6cd5\u4fee\u6539\u4f5c\u4e3a\u6807\u51c6\u5e93\u4e00\u90e8\u5206\u7684\u4ee3\u7801\u3002\u4f46\u662f\u6211\u4eec\u80fd\u591f\u4ece\u8fd9\u4e9b\u7c7b\u7ee7\u627f\uff0c\u7136\u540e\u5c06\u6211\u4eec\u81ea\u5df1\u7684\u529f\u80fd\u6dfb\u52a0\u5230\u6211\u4eec\u7684\u6d3e\u751f\u7c7b\u4e2d\u3002\u540c\u6837\u7684\u60c5\u51b5\u4e5f\u53d1\u751f\u5728\u7b2c\u4e09\u65b9\u5e93\u4e2d\uff0c\u5728\u90a3\u91cc\u4f60\u53ef\u4ee5\u8bbf\u95ee\u5934\u6587\u4ef6\uff0c\u4f46\u662f\u4ee3\u7801\u662f\u9884\u7f16\u8bd1\u7684\u3002 \u5728\u4efb\u4f55\u4e00\u79cd\u60c5\u51b5\u4e0b\uff0c\u6700\u597d\u7684\u7b54\u6848\u662f\u521b\u5efa\u4e00\u4e2a\u81ea\u5df1\u7684\u6d3e\u751f\u7c7b\uff0c\u5e76\u5411\u6d3e\u751f\u7c7b\u6dfb\u52a0\u4f60\u60f3\u8981\u7684\u529f\u80fd\u3002 \u57fa\u7c7b\u4e2d\u6ca1\u6709\u5305\u62ec\u7528\u4e8e\u516c\u5171\u4ee3\u7801\u8bbf\u95ee m_value \u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5728\u57fa\u7c7b\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u8bbf\u95ee\u51fd\u6570\u6765\u8865\u6551\u8fd9\u4e2a\u95ee\u9898\u2014\u2014\u4f46\u4e3a\u4e86\u4e3e\u4f8b\uff0c\u6211\u4eec\u5c06\u628a\u5b83\u6dfb\u52a0\u5230\u6d3e\u751f\u7c7b\u4e2d\u3002\u56e0\u4e3a m_value \u5728\u57fa\u7c7b\u4e2d\u88ab\u58f0\u660e\u4e3a\u53d7\u4fdd\u62a4\u7684\uff0c Derived \u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5b83\u3002 \u8981\u5411\u6d3e\u751f\u7c7b\u6dfb\u52a0\u65b0\u529f\u80fd\uff0c\u53ea\u9700\u5728\u6d3e\u751f\u7c7b\u4e2d\u50cf\u6b63\u5e38\u4e00\u6837\u58f0\u660e\u8be5\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 class Derived : public Base { public : Derived ( int value ) : Base { value } { } int getValue () const { return m_value ; } }; \u73b0\u5728 public \u5c06\u80fd\u591f\u5728 Derived \u7c7b\u578b\u7684\u5bf9\u8c61\u4e0a\u8c03\u7528 getValue() \u6765\u8bbf\u95ee m_value \u7684\u503c\u3002 1 2 3 4 5 6 7 int main () { Derived derived { 5 }; std :: cout << \"derived has value \" << derived . getValue () << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 derived has value 5 \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c Base \u5bf9\u8c61\u5e76\u4e0d\u80fd\u8bbf\u95ee Derived \u4e2d\u7684 getValue() \u51fd\u6570\uff0c\u6240\u4ee5\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u4e0d\u80fd\u5de5\u4f5c\u7684\uff1a 1 2 3 4 5 6 7 int main () { Base base { 5 }; std :: cout << \"base has value \" << base . getValue () << '\\n' ; return 0 ; } \u8fd9\u662f\u56e0\u4e3a Base \u7c7b\u4e2d\u5e76\u6ca1\u6709 getValue() \u51fd\u6570\uff0c\u5b83\u5c5e\u4e8e Derived \u7c7b\u3002\u56e0\u4e3a Derived \u662f Base \uff0c\u6240\u4ee5\u5b83\u80fd\u591f\u8bbf\u95ee Base \u7684\u5c5e\u6027\u3002\u4f46\u662f\uff0c Base \u4e0d\u80fd\u8bbf\u95ee Derived \u7684\u4efb\u4f55\u5c5e\u6027\u3002","title":"\u5411\u6d3e\u751f\u7c7b\u4e2d\u6dfb\u52a0\u65b0\u7684\u529f\u80fd"},{"location":"17-7-calling-inherited-functions-and-overriding-behavior/","tags":["inheritance","overriding"],"text":"Key Takeaway \u57fa\u4e8e\u6d3e\u751f\u7c7b\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u7f16\u8bd1\u5668\u6cbf\u7740\u7ee7\u627f\u94fe\u5411\u4e0a\u67e5\u627e\u5e76\u4f7f\u7528\u627e\u5230\u7684\u7b2c\u4e00\u4e2a \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6d3e\u751f\u7c7b\u7ee7\u627f\u57fa\u7c7b\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u884c\u4e3a\u3002\u5728\u8fd9\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u66f4\u8be6\u7ec6\u5730\u7814\u7a76\u5982\u4f55\u9009\u62e9\u6210\u5458\u51fd\u6570\uff0c\u4ee5\u53ca\u5982\u4f55\u5229\u7528\u5b83\u6765\u6539\u53d8\u6d3e\u751f\u7c7b\u4e2d\u7684\u884c\u4e3a\u3002 \u8c03\u7528\u57fa\u7c7b\u51fd\u6570 \u5f53\u4f7f\u7528\u6d3e\u751f\u7c7b\u5bf9\u8c61\u8c03\u7528\u6210\u5458\u51fd\u6570\u65f6\uff0c\u7f16\u8bd1\u5668\u9996\u5148\u67e5\u770b\u8be5\u6210\u5458\u662f\u5426\u5b58\u5728\u4e8e\u6d3e\u751f\u7c7b\u4e2d\u3002\u5982\u679c\u6ca1\u6709\uff0c\u5b83\u5f00\u59cb\u6cbf\u7740\u7ee7\u627f\u94fe\u5411\u4e0a\u67e5\u627e\uff0c\u5e76\u68c0\u67e5\u6210\u5458\u662f\u5426\u5df2\u7ecf\u5728\u4efb\u4f55\u7236\u7c7b\u4e2d\u5b9a\u4e49\u3002\u5b83\u4f7f\u7528\u5b83\u627e\u5230\u7684\u7b2c\u4e00\u4e2a\u3002 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include class Base { protected : int m_value {}; public : Base ( int value ) : m_value { value } { } void identify () const { std :: cout << \"I am a Base \\n \" ; } }; class Derived : public Base { public : Derived ( int value ) : Base { value } { } }; int main () { Base base { 5 }; base . identify (); Derived derived { 7 }; derived . identify (); return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 I am a Base I am a Base \u5f53\u8c03\u7528 derived.identify() \u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u67e5\u770b identify() \u51fd\u6570\u662f\u5426\u88ab\u5b9a\u4e49\u5728 Derived \u7c7b\u4e2d\u3002\u5b9e\u9645\u4e0a\u6ca1\u6709\u3002\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u7ee7\u7eed\u67e5\u770b\u8be5\u7c7b\u7684\u7236\u7c7b\uff08\u672c\u4f8b\u4e2d\u7684 Base \uff09\u3002 Base \u7c7b\u5b9a\u4e49\u4e86 identify() \u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u4f1a\u4f7f\u7528\u8be5\u51fd\u6570\u3002\u6362\u8a00\u4e4b\uff0c\u7531\u4e8e Derived::identify() \u4e0d\u5b58\u5728\uff0c\u6240\u4ee5\u5b9e\u9645\u8c03\u7528\u7684\u662f Base::identify() \u3002 \u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u57fa\u7c7b\u63d0\u4f9b\u7684\u51fd\u6570\u6ee1\u8db3\u9700\u8981\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u57fa\u7c7b\u7684\u51fd\u6570\u3002 \u91cd\u5b9a\u4e49\u884c\u4e3a\uff08\u51fd\u6570\uff09 \u4e0d\u8fc7\uff0c\u5982\u679c\u6211\u4eec\u5728\u6d3e\u751f\u7c7b\u4e2d\u5b9a\u4e49\u4e86 Derived::identify() \uff0c\u5219\u4f1a\u8c03\u7528\u8fd9\u4e2a\u7248\u672c\u7684\u51fd\u6570\u3002 \u4e5f\u5c31\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6d3e\u751f\u7c7b\u4e2d\u91cd\u5b9a\u4e49\u901a\u8fc7\u91cd\u65b0\u5b9a\u4e49\u51fd\u6570\u6765\u4f7f\u6d3e\u751f\u7c7b\u5177\u6709\u4e0d\u540c\u7684\u884c\u4e3a\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c derived.identify() \u6253\u5370 \u201cI am a Derived\u201d \u7684\u8bdd\u624d\u66f4\u5408\u7406\u3002\u6240\u4ee5\uff0c\u8ba9\u6211\u4eec\u5728\u6d3e\u751f\u7c7b\u4e2d\u4fee\u6539\u51fd\u6570 identify() \uff0c\u4f7f\u5176\u80fd\u591f\u5728\u6211\u4eec\u8c03\u7528 derived.identify() \u65f6\u6253\u5370\u6b63\u786e\u7684\u5185\u5bb9\u3002 \u8981\u4fee\u6539\u57fa\u7c7b\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u5728\u6d3e\u751f\u7c7b\u4e2d\u7684\u5de5\u4f5c\u65b9\u5f0f\uff0c\u53ea\u9700\u91cd\u65b0\u5b9a\u4e49\u6d3e\u751f\u7c7b\u4e2d\u7684\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include class Derived : public Base { public : Derived ( int value ) : Base { value } { } int getValue () const { return m_value ; } // \u5728\u6b64\u5904\u4fee\u6539\u51fd\u6570 void identify () const { std :: cout << \"I am a Derived \\n \" ; } }; \u7c7b\u4f3c\u7684\u4f8b\u5b50\uff0c\u4f46\u662f\u8c03\u7528\u4e86 Derived::identify() \uff1a 1 2 3 4 5 6 7 8 9 10 int main () { Base base { 5 }; base . identify (); Derived derived { 7 }; derived . identify (); return 0 ; } 1 2 I am a Base I am a Derived \u6ce8\u610f\uff0c\u5f53\u5728\u6d3e\u751f\u7c7b\u4e2d\u91cd\u65b0\u5b9a\u4e49\u51fd\u6570\u65f6\uff0c \u6d3e\u751f\u51fd\u6570\u4e0d\u4f1a\u7ee7\u627f\u57fa\u7c7b\u4e2d\u540c\u540d\u51fd\u6570\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u3002\u5b83\u4f7f\u7528\u6d3e\u751f\u7c7b\u4e2d\u5b9a\u4e49\u5b83\u7684\u4efb\u4f55\u8bbf\u95ee\u8bf4\u660e\u7b26 \u3002\u56e0\u6b64\uff0c\u5728\u57fa\u7c7b\u4e2d\u5b9a\u4e49\u4e3a private \u7684\u51fd\u6570\u53ef\u4ee5\u5728\u6d3e\u751f\u7c7b\u4e2d\u91cd\u65b0\u5b9a\u4e49\u4e3a public \uff0c\u53cd\u4e4b\u4ea6\u7136\uff01 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include class Base { private : void print () const { std :: cout << \"Base\" ; } }; class Derived : public Base { public : void print () const { std :: cout << \"Derived \" ; } }; int main () { Derived derived ; derived . print (); // calls derived::print(), which is public return 0 ; } \u6dfb\u52a0\u529f\u80fd \u6709\u65f6\uff0c\u6211\u4eec\u4e0d\u60f3\u5b8c\u5168\u66ff\u6362\u57fa\u7c7b\u7684\u6210\u5458\u51fd\u6570\uff0c\u800c\u662f\u60f3\u5411\u5b83\u6dfb\u52a0\u989d\u5916\u7684\u529f\u80fd\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6ce8\u610f Derived::identify() \u5b8c\u5168\u66ff\u6362\u4e86 Base::identify() \uff01\u8fd9\u53ef\u80fd\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u3002\u53ef\u4ee5\u8ba9\u6d3e\u751f\u51fd\u6570\u8c03\u7528\u540c\u540d\u51fd\u6570\u7684\u57fa\u672c\u7248\u672c(\u4ee5\u4fbf\u91cd\u7528\u4ee3\u7801)\uff0c\u7136\u540e\u5411\u5176\u6dfb\u52a0\u989d\u5916\u7684\u529f\u80fd\u3002 \u8981\u8ba9\u6d3e\u751f\u51fd\u6570\u8c03\u7528\u540c\u540d\u7684\u57fa\u51fd\u6570\uff0c\u53ea\u9700\u6267\u884c\u4e00\u4e2a\u666e\u901a\u51fd\u6570\u8c03\u7528\uff0c\u4f46\u5728\u51fd\u6570\u524d\u9762\u52a0\u4e0a \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 (\u57fa\u7c7b\u7684\u540d\u79f0\u548c\u4e24\u4e2a\u5192\u53f7)\u3002\u4e0b\u9762\u7684\u793a\u4f8b\u91cd\u65b0\u5b9a\u4e49\u4e86 Derived::identify() \uff0c\u5b83\u9996\u5148\u8c03\u7528 Base::identify() \uff0c\u7136\u540e\u6267\u884c\u81ea\u5df1\u7684\u9644\u52a0\u64cd\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include class Derived : public Base { public : Derived ( int value ) : Base { value } { } int getValue () const { return m_value ; } void identify () const { Base :: identify (); // call Base::identify() first std :: cout << \"I am a Derived \\n \" ; // then identify ourselves } }; \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { Base base { 5 }; base . identify (); Derived derived { 7 }; derived . identify (); return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 I am a Base I am a Base I am a Derived \u5f53 derived.identify() \u6267\u884cs\u65f6\uff0c\u5b83\u88ab\u89e3\u6790\u4e3a Derived::identify() \u3002\u4f46\u662f\u5b83\u505a\u7684\u7b2c\u4e00\u4ef6\u4e8b\u662f\u8c03\u7528 Base::identify() \uff0c\u8fdb\u800c\u6253\u5370 \u201cI am a Base\u201d\u3002\u5f53 Base::identify() \u8fd4\u56de\u65f6\uff0c Derived::identify() \u4f1a\u7ee7\u7eed\u6267\u884c\u5e76\u6253\u5370 \u201cI am a Derived\u201d\u3002 \u5f88\u7b80\u5355\u5427\u3002 \u4e3a\u4ec0\u4e48\u6211\u4eec\u9700\u8981\u4f7f\u7528 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u5462\uff1f\u5982\u679c\u50cf\u4e0b\u9762\u8fd9\u6837\u5b9a\u4e49 Derived::identify() \u4f1a\u600e\u6837\u5462\uff1f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include class Derived : public Base { public : Derived ( int value ) : Base { value } { } int getValue () const { return m_value ; } void identify () const { identify (); // Note: no scope resolution! cout << \"I am a Derived\" ; } }; \u4e0d\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u9650\u5b9a\u7b26\u800c\u8c03\u7528 identify() \u7684\u8bdd\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4f1a\u8c03\u7528\u5f53\u524d\u7c7b\u4e2d\u7684 identify() \uff0c\u8fd9\u6837\u8c03\u7528\u7684\u5c31\u662f Derived::identify() \uff0c\u4e8e\u662f Derived::identify() \u4f1a\u81ea\u5df1\u8c03\u7528\u81ea\u5df1\uff0c\u5bfc\u81f4\u6b7b\u5faa\u73af\uff01 \u5728\u5c1d\u8bd5\u8c03\u7528\u57fa\u7c7b\u4e2d\u7684\u53cb\u5143\u51fd\u6570\u65f6\uff0c\u6bd4\u5982\u64cd\u4f5c\u7b26 << \uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u70b9\u9ebb\u70e6\u3002 \u56e0\u4e3a\u57fa\u7c7b\u7684\u53cb\u51fd\u6570\u5b9e\u9645\u4e0a\u4e0d\u662f\u57fa\u7c7b\u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u4f7f\u7528\u8303\u56f4\u89e3\u6790\u9650\u5b9a\u7b26\u4e0d\u8d77\u4f5c\u7528 \u3002\u6211\u4eec\u9700\u8981\u4e00\u79cd\u65b9\u6cd5\u4f7f\u6211\u4eec\u7684\u6d3e\u751f\u7c7b\u4e34\u65f6\u770b\u8d77\u6765\u50cf\u57fa\u7c7b\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u8c03\u7528\u51fd\u6570\u7684\u6b63\u786e\u7248\u672c\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u8fd9\u5f88\u5bb9\u6613\u505a\u5230\uff0c\u53ea\u9700\u4f7f\u7528 static_cast \u8fdb\u884c\u8f6c\u6362\u5373\u53ef\u3002\u8bf7\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include class Base { private : int m_value {}; public : Base ( int value ) : m_value { value } { } friend std :: ostream & operator << ( std :: ostream & out , const Base & b ) { out << \"In Base \\n \" ; out << b . m_value << '\\n' ; return out ; } }; class Derived : public Base { public : Derived ( int value ) : Base { value } { } friend std :: ostream & operator << ( std :: ostream & out , const Derived & d ) { out << \"In Derived \\n \" ; // static_cast Derived to a Base object, so we call the right version of operator<< out << static_cast < const Base &> ( d ); return out ; } }; int main () { Derived derived { 7 }; std :: cout << derived << '\\n' ; return 0 ; } \u56e0\u4e3a Derived \u201d\u662f\u4e00\u4e2a\u201c Base \uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06 Derived \u8f6c\u6362\u4e3a Base \uff0c\u7136\u540e\u5c31\u53ef\u4ee5\u8c03\u7528 Base \u7684 << \u64cd\u4f5c\u7b26\u4e86\u3002 \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 In Derived In Base 7","title":"17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199"},{"location":"17-7-calling-inherited-functions-and-overriding-behavior/#_1","text":"\u5f53\u4f7f\u7528\u6d3e\u751f\u7c7b\u5bf9\u8c61\u8c03\u7528\u6210\u5458\u51fd\u6570\u65f6\uff0c\u7f16\u8bd1\u5668\u9996\u5148\u67e5\u770b\u8be5\u6210\u5458\u662f\u5426\u5b58\u5728\u4e8e\u6d3e\u751f\u7c7b\u4e2d\u3002\u5982\u679c\u6ca1\u6709\uff0c\u5b83\u5f00\u59cb\u6cbf\u7740\u7ee7\u627f\u94fe\u5411\u4e0a\u67e5\u627e\uff0c\u5e76\u68c0\u67e5\u6210\u5458\u662f\u5426\u5df2\u7ecf\u5728\u4efb\u4f55\u7236\u7c7b\u4e2d\u5b9a\u4e49\u3002\u5b83\u4f7f\u7528\u5b83\u627e\u5230\u7684\u7b2c\u4e00\u4e2a\u3002 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include class Base { protected : int m_value {}; public : Base ( int value ) : m_value { value } { } void identify () const { std :: cout << \"I am a Base \\n \" ; } }; class Derived : public Base { public : Derived ( int value ) : Base { value } { } }; int main () { Base base { 5 }; base . identify (); Derived derived { 7 }; derived . identify (); return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 I am a Base I am a Base \u5f53\u8c03\u7528 derived.identify() \u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u67e5\u770b identify() \u51fd\u6570\u662f\u5426\u88ab\u5b9a\u4e49\u5728 Derived \u7c7b\u4e2d\u3002\u5b9e\u9645\u4e0a\u6ca1\u6709\u3002\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u7ee7\u7eed\u67e5\u770b\u8be5\u7c7b\u7684\u7236\u7c7b\uff08\u672c\u4f8b\u4e2d\u7684 Base \uff09\u3002 Base \u7c7b\u5b9a\u4e49\u4e86 identify() \u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u4f1a\u4f7f\u7528\u8be5\u51fd\u6570\u3002\u6362\u8a00\u4e4b\uff0c\u7531\u4e8e Derived::identify() \u4e0d\u5b58\u5728\uff0c\u6240\u4ee5\u5b9e\u9645\u8c03\u7528\u7684\u662f Base::identify() \u3002 \u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u57fa\u7c7b\u63d0\u4f9b\u7684\u51fd\u6570\u6ee1\u8db3\u9700\u8981\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u57fa\u7c7b\u7684\u51fd\u6570\u3002","title":"\u8c03\u7528\u57fa\u7c7b\u51fd\u6570"},{"location":"17-7-calling-inherited-functions-and-overriding-behavior/#_2","text":"\u4e0d\u8fc7\uff0c\u5982\u679c\u6211\u4eec\u5728\u6d3e\u751f\u7c7b\u4e2d\u5b9a\u4e49\u4e86 Derived::identify() \uff0c\u5219\u4f1a\u8c03\u7528\u8fd9\u4e2a\u7248\u672c\u7684\u51fd\u6570\u3002 \u4e5f\u5c31\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6d3e\u751f\u7c7b\u4e2d\u91cd\u5b9a\u4e49\u901a\u8fc7\u91cd\u65b0\u5b9a\u4e49\u51fd\u6570\u6765\u4f7f\u6d3e\u751f\u7c7b\u5177\u6709\u4e0d\u540c\u7684\u884c\u4e3a\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c derived.identify() \u6253\u5370 \u201cI am a Derived\u201d \u7684\u8bdd\u624d\u66f4\u5408\u7406\u3002\u6240\u4ee5\uff0c\u8ba9\u6211\u4eec\u5728\u6d3e\u751f\u7c7b\u4e2d\u4fee\u6539\u51fd\u6570 identify() \uff0c\u4f7f\u5176\u80fd\u591f\u5728\u6211\u4eec\u8c03\u7528 derived.identify() \u65f6\u6253\u5370\u6b63\u786e\u7684\u5185\u5bb9\u3002 \u8981\u4fee\u6539\u57fa\u7c7b\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u5728\u6d3e\u751f\u7c7b\u4e2d\u7684\u5de5\u4f5c\u65b9\u5f0f\uff0c\u53ea\u9700\u91cd\u65b0\u5b9a\u4e49\u6d3e\u751f\u7c7b\u4e2d\u7684\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include class Derived : public Base { public : Derived ( int value ) : Base { value } { } int getValue () const { return m_value ; } // \u5728\u6b64\u5904\u4fee\u6539\u51fd\u6570 void identify () const { std :: cout << \"I am a Derived \\n \" ; } }; \u7c7b\u4f3c\u7684\u4f8b\u5b50\uff0c\u4f46\u662f\u8c03\u7528\u4e86 Derived::identify() \uff1a 1 2 3 4 5 6 7 8 9 10 int main () { Base base { 5 }; base . identify (); Derived derived { 7 }; derived . identify (); return 0 ; } 1 2 I am a Base I am a Derived \u6ce8\u610f\uff0c\u5f53\u5728\u6d3e\u751f\u7c7b\u4e2d\u91cd\u65b0\u5b9a\u4e49\u51fd\u6570\u65f6\uff0c \u6d3e\u751f\u51fd\u6570\u4e0d\u4f1a\u7ee7\u627f\u57fa\u7c7b\u4e2d\u540c\u540d\u51fd\u6570\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u3002\u5b83\u4f7f\u7528\u6d3e\u751f\u7c7b\u4e2d\u5b9a\u4e49\u5b83\u7684\u4efb\u4f55\u8bbf\u95ee\u8bf4\u660e\u7b26 \u3002\u56e0\u6b64\uff0c\u5728\u57fa\u7c7b\u4e2d\u5b9a\u4e49\u4e3a private \u7684\u51fd\u6570\u53ef\u4ee5\u5728\u6d3e\u751f\u7c7b\u4e2d\u91cd\u65b0\u5b9a\u4e49\u4e3a public \uff0c\u53cd\u4e4b\u4ea6\u7136\uff01 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include class Base { private : void print () const { std :: cout << \"Base\" ; } }; class Derived : public Base { public : void print () const { std :: cout << \"Derived \" ; } }; int main () { Derived derived ; derived . print (); // calls derived::print(), which is public return 0 ; }","title":"\u91cd\u5b9a\u4e49\u884c\u4e3a\uff08\u51fd\u6570\uff09"},{"location":"17-7-calling-inherited-functions-and-overriding-behavior/#_3","text":"\u6709\u65f6\uff0c\u6211\u4eec\u4e0d\u60f3\u5b8c\u5168\u66ff\u6362\u57fa\u7c7b\u7684\u6210\u5458\u51fd\u6570\uff0c\u800c\u662f\u60f3\u5411\u5b83\u6dfb\u52a0\u989d\u5916\u7684\u529f\u80fd\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6ce8\u610f Derived::identify() \u5b8c\u5168\u66ff\u6362\u4e86 Base::identify() \uff01\u8fd9\u53ef\u80fd\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u3002\u53ef\u4ee5\u8ba9\u6d3e\u751f\u51fd\u6570\u8c03\u7528\u540c\u540d\u51fd\u6570\u7684\u57fa\u672c\u7248\u672c(\u4ee5\u4fbf\u91cd\u7528\u4ee3\u7801)\uff0c\u7136\u540e\u5411\u5176\u6dfb\u52a0\u989d\u5916\u7684\u529f\u80fd\u3002 \u8981\u8ba9\u6d3e\u751f\u51fd\u6570\u8c03\u7528\u540c\u540d\u7684\u57fa\u51fd\u6570\uff0c\u53ea\u9700\u6267\u884c\u4e00\u4e2a\u666e\u901a\u51fd\u6570\u8c03\u7528\uff0c\u4f46\u5728\u51fd\u6570\u524d\u9762\u52a0\u4e0a \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 (\u57fa\u7c7b\u7684\u540d\u79f0\u548c\u4e24\u4e2a\u5192\u53f7)\u3002\u4e0b\u9762\u7684\u793a\u4f8b\u91cd\u65b0\u5b9a\u4e49\u4e86 Derived::identify() \uff0c\u5b83\u9996\u5148\u8c03\u7528 Base::identify() \uff0c\u7136\u540e\u6267\u884c\u81ea\u5df1\u7684\u9644\u52a0\u64cd\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include class Derived : public Base { public : Derived ( int value ) : Base { value } { } int getValue () const { return m_value ; } void identify () const { Base :: identify (); // call Base::identify() first std :: cout << \"I am a Derived \\n \" ; // then identify ourselves } }; \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { Base base { 5 }; base . identify (); Derived derived { 7 }; derived . identify (); return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 I am a Base I am a Base I am a Derived \u5f53 derived.identify() \u6267\u884cs\u65f6\uff0c\u5b83\u88ab\u89e3\u6790\u4e3a Derived::identify() \u3002\u4f46\u662f\u5b83\u505a\u7684\u7b2c\u4e00\u4ef6\u4e8b\u662f\u8c03\u7528 Base::identify() \uff0c\u8fdb\u800c\u6253\u5370 \u201cI am a Base\u201d\u3002\u5f53 Base::identify() \u8fd4\u56de\u65f6\uff0c Derived::identify() \u4f1a\u7ee7\u7eed\u6267\u884c\u5e76\u6253\u5370 \u201cI am a Derived\u201d\u3002 \u5f88\u7b80\u5355\u5427\u3002 \u4e3a\u4ec0\u4e48\u6211\u4eec\u9700\u8981\u4f7f\u7528 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u5462\uff1f\u5982\u679c\u50cf\u4e0b\u9762\u8fd9\u6837\u5b9a\u4e49 Derived::identify() \u4f1a\u600e\u6837\u5462\uff1f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include class Derived : public Base { public : Derived ( int value ) : Base { value } { } int getValue () const { return m_value ; } void identify () const { identify (); // Note: no scope resolution! cout << \"I am a Derived\" ; } }; \u4e0d\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u9650\u5b9a\u7b26\u800c\u8c03\u7528 identify() \u7684\u8bdd\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4f1a\u8c03\u7528\u5f53\u524d\u7c7b\u4e2d\u7684 identify() \uff0c\u8fd9\u6837\u8c03\u7528\u7684\u5c31\u662f Derived::identify() \uff0c\u4e8e\u662f Derived::identify() \u4f1a\u81ea\u5df1\u8c03\u7528\u81ea\u5df1\uff0c\u5bfc\u81f4\u6b7b\u5faa\u73af\uff01 \u5728\u5c1d\u8bd5\u8c03\u7528\u57fa\u7c7b\u4e2d\u7684\u53cb\u5143\u51fd\u6570\u65f6\uff0c\u6bd4\u5982\u64cd\u4f5c\u7b26 << \uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u70b9\u9ebb\u70e6\u3002 \u56e0\u4e3a\u57fa\u7c7b\u7684\u53cb\u51fd\u6570\u5b9e\u9645\u4e0a\u4e0d\u662f\u57fa\u7c7b\u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u4f7f\u7528\u8303\u56f4\u89e3\u6790\u9650\u5b9a\u7b26\u4e0d\u8d77\u4f5c\u7528 \u3002\u6211\u4eec\u9700\u8981\u4e00\u79cd\u65b9\u6cd5\u4f7f\u6211\u4eec\u7684\u6d3e\u751f\u7c7b\u4e34\u65f6\u770b\u8d77\u6765\u50cf\u57fa\u7c7b\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u8c03\u7528\u51fd\u6570\u7684\u6b63\u786e\u7248\u672c\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u8fd9\u5f88\u5bb9\u6613\u505a\u5230\uff0c\u53ea\u9700\u4f7f\u7528 static_cast \u8fdb\u884c\u8f6c\u6362\u5373\u53ef\u3002\u8bf7\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include class Base { private : int m_value {}; public : Base ( int value ) : m_value { value } { } friend std :: ostream & operator << ( std :: ostream & out , const Base & b ) { out << \"In Base \\n \" ; out << b . m_value << '\\n' ; return out ; } }; class Derived : public Base { public : Derived ( int value ) : Base { value } { } friend std :: ostream & operator << ( std :: ostream & out , const Derived & d ) { out << \"In Derived \\n \" ; // static_cast Derived to a Base object, so we call the right version of operator<< out << static_cast < const Base &> ( d ); return out ; } }; int main () { Derived derived { 7 }; std :: cout << derived << '\\n' ; return 0 ; } \u56e0\u4e3a Derived \u201d\u662f\u4e00\u4e2a\u201c Base \uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06 Derived \u8f6c\u6362\u4e3a Base \uff0c\u7136\u540e\u5c31\u53ef\u4ee5\u8c03\u7528 Base \u7684 << \u64cd\u4f5c\u7b26\u4e86\u3002 \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 In Derived In Base 7","title":"\u6dfb\u52a0\u529f\u80fd"},{"location":"17-8-Hiding-inherited-functionality/","tags":["inheritance","overriding"],"text":"Key Takeaway - \u4fee\u6539\u7ee7\u627f\u6210\u5458\u7684\u8bbf\u95ee\u7ea7\u522b C++\u5141\u8bb8\u6211\u4eec\u5728\u6d3e\u751f\u7c7b\u4e2d\u66f4\u6539\u7ee7\u627f\u6210\u5458\u7684 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u3002\u8fd9\u662f\u901a\u8fc7\u5728\u65b0\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u4e0b\u4f7f\u7528 using\u58f0\u660e\u6765\u6807\u8bc6\u5728\u6d3e\u751f\u7c7b\u4e2d\u66f4\u6539\u5176\u8bbf\u95ee\u6743\u9650\u7684(\u9650\u5b9a\u8303\u56f4\u7684)\u57fa\u7c7b\u6210\u5458\u6765\u5b9e\u73b0\u7684\u3002 \u8003\u8651\u4e0b\u9762\u7684 Base \u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include class Base { private : int m_value {}; public : Base ( int value ) : m_value { value } { } protected : void printValue () const { std :: cout << m_value ; } }; \u56e0\u4e3a Base::printValue() \u7c7b\u88ab\u58f0\u660e\u4e3a \u53d7\u4fdd\u62a4\u6210\u5458 \uff0c\u6240\u4ee5\u5b83\u53ea\u80fd\u88ab Base \u6216\u5b83\u7684\u6d3e\u751f\u7c7b\u8bbf\u95ee\u3002\u800c\u5916\u90e8\u4ee3\u7801\u662f\u4e0d\u80fd\u8bbf\u95ee\u5b83\u7684\u3002 \u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u628a printValue() \u7684\u8bbf\u95ee\u7ea7\u522b\u8c03\u6574\u4e3a \u516c\u6709\u6210\u5458 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class Derived : public Base { public : Derived ( int value ) : Base { value } { } // Base::printValue \u662f\u7ee7\u627f\u6765\u7684\u53d7\u4fdd\u62a4\u6210\u5458\uff0c\u6240\u4ee5\u5916\u90e8\u4ee3\u7801\u4e0d\u80fd\u8bbf\u95ee\u5b83 // \u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 using \u58f0\u660e\u5c06\u5176\u4fee\u6539\u4e3apublic\u7684 using Base :: printValue ; // note: \u6ce8\u610f\u8fd9\u91cc\u6ca1\u6709\u62ec\u53f7 }; \u8fd9\u610f\u5473\u7740\u8fd9\u6bb5\u4ee3\u7801\u73b0\u5728\u53ef\u4ee5\u5de5\u4f5c\u4e86\uff1a 1 2 3 4 5 6 7 8 int main () { Derived derived { 7 }; // printValue public in Derived, so this is okay derived . printValue (); // prints 7 return 0 ; } \u6211\u4eec\u53ea\u80fd\u4fee\u6539\u6d3e\u751f\u7c7b\u4e2d\u80fd\u591f\u8bbf\u95ee\u7684\u57fa\u7c7b\u6210\u5458\u6210\u5458\u7684\u8bbf\u95ee\u4fee\u9970\u7b26\u3002\u56e0\u6b64\uff0c\u79c1\u6709\u7684\u57fa\u6210\u5458\u65e0\u6cd5\u88ab\u4fee\u6539\u4e3a\u53d7\u4fdd\u62a4\u6216\u516c\u6709\uff0c\u56e0\u4e3a\u6d3e\u751f\u7c7b\u4e0d\u80fd\u8bbf\u95ee\u57fa\u7c7b\u7684\u79c1\u6709\u6210\u5458\u3002 \u9690\u85cf\u51fd\u6570 \u5728 C++ \u4e2d\uff0c\u9664\u4e86\u76f4\u63a5\u4fee\u6539\u4ee3\u7801\u4e4b\u5916\uff0c\u8fd8\u6709\u522b\u7684\u529e\u6cd5\u5220\u9664\u6216\u9650\u5236\u57fa\u7c7b\u4e2d\u51fd\u6570\u7684\u4f7f\u7528\u3002\u57fa\u7c7b\u4e2d\u7684\u7684\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u88ab\u9690\u85cf\u8d77\u6765\uff0c\u8fd9\u6837\u5b83\u5c31\u4e0d\u80fd\u5728\u6d3e\u751f\u7c7b\u4e2d\u88ab\u8bbf\u95ee\u3002\u8981\u8fbe\u5230\u8fd9\u4e2a\u76ee\u7684\uff0c\u53ea\u9700\u8981\u4fee\u6539\u8bbf\u95ee\u4fee\u9970\u7b26\u5373\u53ef\u3002 \u4f8b\u5982\uff0c\u53ef\u4ee5\u628a \u516c\u6709\u6210\u5458 \u4fee\u6539\u4e3a \u79c1\u6709\u6210\u5458 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include class Base { public : int m_value {}; }; class Derived : public Base { private : using Base :: m_value ; public : Derived ( int value ) // We can't initialize m_value, since it's a Base member (Base must initialize it) { // But we can assign it a value m_value = value ; } }; int main () { Derived derived { 7 }; // The following won't work because m_value has been redefined as private std :: cout << derived . m_value ; return 0 ; } \u6ce8\u610f\uff0c\u8fd9\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u8bbe\u8ba1\u7cdf\u7cd5\u7684\u57fa\u7c7b\uff0c\u5e76\u5c06\u5176\u6570\u636e\u826f\u597d\u5730\u5c01\u88c5\u5728\u6d3e\u751f\u7c7b\u4e2d\u3002 \u6216\u8005\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u79c1\u6709\u5730\u7ee7\u627f Base \uff0c\u9664\u4e86 \u516c\u5f00\u7ee7\u627f Base \u7684\u6210\u5458\u5e76\u901a\u8fc7\u91cd\u5199\u5176\u8bbf\u95ee\u8bf4\u660e\u7b26\u4f7f m_value \u53d8\u4e3a \u79c1\u6709\u6210\u5458 \u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5 \u901a\u8fc7 \u79c1\u6709\u7ee7\u627f \u7684\u65b9\u5f0f\u7ee7\u627f Base \uff0c\u8fd9\u6837\u4e00\u6765 Base \u7684\u6240\u6709\u6210\u5458\u5728\u4e00\u5f00\u59cb\u5c31\u88ab\u79c1\u6709\u5730\u7ee7\u627f\u3002 \u4f60\u4e5f\u53ef\u4ee5\u5728\u6d3e\u751f\u7c7b\u4e2d\u5c06\u6210\u5458\u51fd\u6570\u6807\u8bb0\u4e3a delete \uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u4e0d\u80fd\u901a\u8fc7\u6d3e\u751f\u5bf9\u8c61\u8c03\u7528: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include class Base { private : int m_value {}; public : Base ( int value ) : m_value { value } { } int getValue () const { return m_value ; } }; class Derived : public Base { public : Derived ( int value ) : Base { value } { } int getValue () = delete ; // mark this function as inaccessible }; int main () { Derived derived { 7 }; // The following won't work because getValue() has been deleted! std :: cout << derived . getValue (); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c getValue() \u51fd\u6570\u88ab\u6807\u8bb0\u4e3a delete \u3002\u8fd9\u610f\u5473\u7740\u8c03\u7528\u6d3e\u751f\u7c7b\u4e2d\u7684\u8be5\u51fd\u6570\u65f6\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\u3002 \u6ce8\u610f\uff0c\u57fa\u7c7b\u4e2d\u7684 getValue() \u51fd\u6570\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528 \u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\u8c03\u7528 Base::getValue() \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int main () { Derived derived { 7 }; // \u53ef\u4ee5\u76f4\u63a5\u8c03\u7528 Base::getValue() std :: cout << derived . Base :: getValue (); // \u5c06\u5b50\u7c7b\u8f6c\u6362\u4e3a\u57fa\u7c7b\u7136\u540e\u8c03\u7528 getValue() \u5c31\u4f1a\u89e3\u6790\u5230 Base::getValue() std :: cout << static_cast < Base &> ( derived ). getValue (); return 0 ; } \u5982\u679c\u4f7f\u7528\u5f3a\u5236\u8f6c\u6362\u65b9\u6cd5\uff0c\u5219\u5f3a\u5236\u8f6c\u6362\u4e3aBase&\u800c\u4e0d\u662fBase\uff0c\u4ee5\u907f\u514d\u751f\u6210 derived \u7684 Base \u90e8\u5206\u7684\u526f\u672c\u3002","title":"17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570"},{"location":"17-8-Hiding-inherited-functionality/#_1","text":"C++\u5141\u8bb8\u6211\u4eec\u5728\u6d3e\u751f\u7c7b\u4e2d\u66f4\u6539\u7ee7\u627f\u6210\u5458\u7684 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u3002\u8fd9\u662f\u901a\u8fc7\u5728\u65b0\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u4e0b\u4f7f\u7528 using\u58f0\u660e\u6765\u6807\u8bc6\u5728\u6d3e\u751f\u7c7b\u4e2d\u66f4\u6539\u5176\u8bbf\u95ee\u6743\u9650\u7684(\u9650\u5b9a\u8303\u56f4\u7684)\u57fa\u7c7b\u6210\u5458\u6765\u5b9e\u73b0\u7684\u3002 \u8003\u8651\u4e0b\u9762\u7684 Base \u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include class Base { private : int m_value {}; public : Base ( int value ) : m_value { value } { } protected : void printValue () const { std :: cout << m_value ; } }; \u56e0\u4e3a Base::printValue() \u7c7b\u88ab\u58f0\u660e\u4e3a \u53d7\u4fdd\u62a4\u6210\u5458 \uff0c\u6240\u4ee5\u5b83\u53ea\u80fd\u88ab Base \u6216\u5b83\u7684\u6d3e\u751f\u7c7b\u8bbf\u95ee\u3002\u800c\u5916\u90e8\u4ee3\u7801\u662f\u4e0d\u80fd\u8bbf\u95ee\u5b83\u7684\u3002 \u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u628a printValue() \u7684\u8bbf\u95ee\u7ea7\u522b\u8c03\u6574\u4e3a \u516c\u6709\u6210\u5458 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class Derived : public Base { public : Derived ( int value ) : Base { value } { } // Base::printValue \u662f\u7ee7\u627f\u6765\u7684\u53d7\u4fdd\u62a4\u6210\u5458\uff0c\u6240\u4ee5\u5916\u90e8\u4ee3\u7801\u4e0d\u80fd\u8bbf\u95ee\u5b83 // \u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 using \u58f0\u660e\u5c06\u5176\u4fee\u6539\u4e3apublic\u7684 using Base :: printValue ; // note: \u6ce8\u610f\u8fd9\u91cc\u6ca1\u6709\u62ec\u53f7 }; \u8fd9\u610f\u5473\u7740\u8fd9\u6bb5\u4ee3\u7801\u73b0\u5728\u53ef\u4ee5\u5de5\u4f5c\u4e86\uff1a 1 2 3 4 5 6 7 8 int main () { Derived derived { 7 }; // printValue public in Derived, so this is okay derived . printValue (); // prints 7 return 0 ; } \u6211\u4eec\u53ea\u80fd\u4fee\u6539\u6d3e\u751f\u7c7b\u4e2d\u80fd\u591f\u8bbf\u95ee\u7684\u57fa\u7c7b\u6210\u5458\u6210\u5458\u7684\u8bbf\u95ee\u4fee\u9970\u7b26\u3002\u56e0\u6b64\uff0c\u79c1\u6709\u7684\u57fa\u6210\u5458\u65e0\u6cd5\u88ab\u4fee\u6539\u4e3a\u53d7\u4fdd\u62a4\u6216\u516c\u6709\uff0c\u56e0\u4e3a\u6d3e\u751f\u7c7b\u4e0d\u80fd\u8bbf\u95ee\u57fa\u7c7b\u7684\u79c1\u6709\u6210\u5458\u3002","title":"\u4fee\u6539\u7ee7\u627f\u6210\u5458\u7684\u8bbf\u95ee\u7ea7\u522b"},{"location":"17-8-Hiding-inherited-functionality/#_2","text":"\u5728 C++ \u4e2d\uff0c\u9664\u4e86\u76f4\u63a5\u4fee\u6539\u4ee3\u7801\u4e4b\u5916\uff0c\u8fd8\u6709\u522b\u7684\u529e\u6cd5\u5220\u9664\u6216\u9650\u5236\u57fa\u7c7b\u4e2d\u51fd\u6570\u7684\u4f7f\u7528\u3002\u57fa\u7c7b\u4e2d\u7684\u7684\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u88ab\u9690\u85cf\u8d77\u6765\uff0c\u8fd9\u6837\u5b83\u5c31\u4e0d\u80fd\u5728\u6d3e\u751f\u7c7b\u4e2d\u88ab\u8bbf\u95ee\u3002\u8981\u8fbe\u5230\u8fd9\u4e2a\u76ee\u7684\uff0c\u53ea\u9700\u8981\u4fee\u6539\u8bbf\u95ee\u4fee\u9970\u7b26\u5373\u53ef\u3002 \u4f8b\u5982\uff0c\u53ef\u4ee5\u628a \u516c\u6709\u6210\u5458 \u4fee\u6539\u4e3a \u79c1\u6709\u6210\u5458 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include class Base { public : int m_value {}; }; class Derived : public Base { private : using Base :: m_value ; public : Derived ( int value ) // We can't initialize m_value, since it's a Base member (Base must initialize it) { // But we can assign it a value m_value = value ; } }; int main () { Derived derived { 7 }; // The following won't work because m_value has been redefined as private std :: cout << derived . m_value ; return 0 ; } \u6ce8\u610f\uff0c\u8fd9\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u8bbe\u8ba1\u7cdf\u7cd5\u7684\u57fa\u7c7b\uff0c\u5e76\u5c06\u5176\u6570\u636e\u826f\u597d\u5730\u5c01\u88c5\u5728\u6d3e\u751f\u7c7b\u4e2d\u3002 \u6216\u8005\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u79c1\u6709\u5730\u7ee7\u627f Base \uff0c\u9664\u4e86 \u516c\u5f00\u7ee7\u627f Base \u7684\u6210\u5458\u5e76\u901a\u8fc7\u91cd\u5199\u5176\u8bbf\u95ee\u8bf4\u660e\u7b26\u4f7f m_value \u53d8\u4e3a \u79c1\u6709\u6210\u5458 \u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5 \u901a\u8fc7 \u79c1\u6709\u7ee7\u627f \u7684\u65b9\u5f0f\u7ee7\u627f Base \uff0c\u8fd9\u6837\u4e00\u6765 Base \u7684\u6240\u6709\u6210\u5458\u5728\u4e00\u5f00\u59cb\u5c31\u88ab\u79c1\u6709\u5730\u7ee7\u627f\u3002 \u4f60\u4e5f\u53ef\u4ee5\u5728\u6d3e\u751f\u7c7b\u4e2d\u5c06\u6210\u5458\u51fd\u6570\u6807\u8bb0\u4e3a delete \uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u4e0d\u80fd\u901a\u8fc7\u6d3e\u751f\u5bf9\u8c61\u8c03\u7528: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include class Base { private : int m_value {}; public : Base ( int value ) : m_value { value } { } int getValue () const { return m_value ; } }; class Derived : public Base { public : Derived ( int value ) : Base { value } { } int getValue () = delete ; // mark this function as inaccessible }; int main () { Derived derived { 7 }; // The following won't work because getValue() has been deleted! std :: cout << derived . getValue (); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c getValue() \u51fd\u6570\u88ab\u6807\u8bb0\u4e3a delete \u3002\u8fd9\u610f\u5473\u7740\u8c03\u7528\u6d3e\u751f\u7c7b\u4e2d\u7684\u8be5\u51fd\u6570\u65f6\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\u3002 \u6ce8\u610f\uff0c\u57fa\u7c7b\u4e2d\u7684 getValue() \u51fd\u6570\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528 \u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\u8c03\u7528 Base::getValue() \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int main () { Derived derived { 7 }; // \u53ef\u4ee5\u76f4\u63a5\u8c03\u7528 Base::getValue() std :: cout << derived . Base :: getValue (); // \u5c06\u5b50\u7c7b\u8f6c\u6362\u4e3a\u57fa\u7c7b\u7136\u540e\u8c03\u7528 getValue() \u5c31\u4f1a\u89e3\u6790\u5230 Base::getValue() std :: cout << static_cast < Base &> ( derived ). getValue (); return 0 ; } \u5982\u679c\u4f7f\u7528\u5f3a\u5236\u8f6c\u6362\u65b9\u6cd5\uff0c\u5219\u5f3a\u5236\u8f6c\u6362\u4e3aBase&\u800c\u4e0d\u662fBase\uff0c\u4ee5\u907f\u514d\u751f\u6210 derived \u7684 Base \u90e8\u5206\u7684\u526f\u672c\u3002","title":"\u9690\u85cf\u51fd\u6570"},{"location":"17-9-multiple-inheritance/","tags":["inheritance","multiple-inheritance"],"text":"Key Takeaway - \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u4ecb\u7ecd\u7684\u6240\u6709\u7ee7\u627f\u793a\u4f8b\u90fd\u662f\u5355\u7ee7\u627f\u2014\u2014\u4e5f\u5c31\u662f\u8bf4\uff0c\u6bcf\u4e2a\u7ee7\u627f\u7684\u7c7b\u90fd\u6709\u4e14\u53ea\u6709\u4e00\u4e2a\u7236\u7c7b\u3002\u4e0d\u8fc7\uff0cC++\u5176\u5b9e\u8fd8\u652f\u6301\u591a\u91cd\u7ee7\u627f\u3002 \u591a\u91cd\u7ee7\u627f \u4f7f\u5f97\u6d3e\u751f\u7c7b\u53ef\u4ee5\u4ece\u591a\u4e2a\u7236\u7c7b\u7ee7\u627f\u6210\u5458\u3002 \u5047\u8bbe\u6211\u4eec\u60f3\u5199\u4e00\u4e2a\u7a0b\u5e8f\u6765\u8bb0\u5f55\u4e00\u7fa4\u8001\u5e08\u7684\u4fe1\u606f\u3002\u8001\u5e08\u201d\u662f\u4e00\u4e2a\u201c\u4eba\u3002\u7136\u800c\uff0c\u6559\u5e08\u4e5f\u662f\u96c7\u5458(\u5982\u679c\u4e3a\u81ea\u5df1\u5de5\u4f5c\uff0c\u4ed6\u4eec\u5c31\u662f\u81ea\u5df1\u7684\u96c7\u4e3b)\u3002\u591a\u91cd\u7ee7\u627f\u53ef\u7528\u4e8e\u521b\u5efa\u4ece Person \u548c Employee \u7ee7\u627f\u5c5e\u6027\u7684 Teacher \u7c7b\u3002\u8981\u4f7f\u7528\u591a\u91cd\u7ee7\u627f\uff0c\u53ea\u9700\u4e00\u6b21\u6307\u5b9a\u6bcf\u4e2a\u57fa\u7c7b(\u5c31\u50cf\u5355\u7ee7\u627f\u4e00\u6837)\uff0c\u7528\u9017\u53f7\u5206\u9694\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 #include #include class Person { private : std :: string m_name ; int m_age {}; public : Person ( std :: string_view name , int age ) : m_name { name }, m_age { age } { } const std :: string & getName () const { return m_name ; } int getAge () const { return m_age ; } }; class Employee { private : std :: string m_employer ; double m_wage {}; public : Employee ( std :: string_view employer , double wage ) : m_employer { employer }, m_wage { wage } { } const std :: string & getEmployer () const { return m_employer ; } double getWage () const { return m_wage ; } }; // Teacher publicly inherits Person and Employee class Teacher : public Person , public Employee { private : int m_teachesGrade {}; public : Teacher ( std :: string_view name , int age , std :: string_view employer , double wage , int teachesGrade ) : Person { name , age }, Employee { employer , wage }, m_teachesGrade { teachesGrade } { } }; int main () { Teacher t { \"Mary\" , 45 , \"Boo\" , 14.3 , 8 }; return 0 ; } Mixins mixin (\u4e5f\u62fc\u4f5c \u201cmix-in\u201d) \u662f\u4e00\u4e2a\u5c0f\u7684\u7c7b\uff0c\u5b83\u53ef\u4ee5\u88ab\u7c7b\u7ee7\u627f\u4ee5\u4fbf\u4e3a\u5176\u6dfb\u52a0\u4e00\u4e9b\u5c5e\u6027\u3002\u5b83\u7684\u540d\u5b57mixin\uff08\u6df7\u5165\uff09\u8bf4\u660e\uff0c\u5b83\u7684\u7528\u9014\u662f\u88ab\u6dfb\u52a0\u5230\u5176\u4ed6\u7c7b\u4e2d\uff0c\u800c\u4e0d\u662f\u5355\u72ec\u88ab\u5b9e\u4f8b\u5316\u3002 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c Box \u548c Label \u7c7b\u90fd\u662f mixin\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f\u5b83\u4eec\u6765\u521b\u5efa\u65b0\u7684 Button class. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 // h/t to reader Waldo for this example #include struct Point2D { int x ; int y ; }; class Box // mixin Box class { public : void setTopLeft ( Point2D point ) { m_topLeft = point ; } void setBottomRight ( Point2D point ) { m_bottomRight = point ; } private : Point2D m_topLeft {}; Point2D m_bottomRight {}; }; class Label // mixin Label class { public : void setText ( const std :: string_view str ) { m_text = str ; } void setFontSize ( int fontSize ) { m_fontSize = fontSize ; } private : std :: string m_text {}; int m_fontSize {}; }; class Button : public Box , public Label {}; int main () { Button button {}; button . Box :: setTopLeft ({ 1 , 1 }); button . Box :: setBottomRight ({ 10 , 10 }); button . Label :: setText ( \"Username: \" ); button . Label :: setFontSize ( 6 ); } COPY \u6269\u5c55\u9605\u8bfb \u56e0\u4e3amixin\u88ab\u8bbe\u8ba1\u4e3a\u5411\u6d3e\u751f\u7c7b\u6dfb\u52a0\u529f\u80fd\uff0c\u800c\u4e0d\u662f\u63d0\u4f9b\u63a5\u53e3\uff0c\u6240\u4ee5mixin\u901a\u5e38\u4e0d\u4f7f\u7528\u865a\u51fd\u6570(\u4e0b\u4e00\u7ae0\u5c06\u8ba8\u8bba)\u3002\u76f8\u53cd\uff0c\u5982\u679c\u9700\u8981\u81ea\u5b9a\u4e49mixin\u7c7b\u4ee5\u7279\u5b9a\u7684\u65b9\u5f0f\u5de5\u4f5c\uff0c\u5219\u901a\u5e38\u4f7f\u7528\u6a21\u677f\u3002\u7531\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0cmixin\u7c7b\u901a\u5e38\u662f\u6a21\u677f\u5316\u7684\u3002 \u4e5f\u8bb8\u4ee4\u4eba\u60ca\u8bb6\u7684\u662f\uff0c\u6d3e\u751f\u7c7b\u53ef\u4ee5\u4f7f\u7528\u6d3e\u751f\u7c7b\u4f5c\u4e3a\u6a21\u677f\u7c7b\u578b\u53c2\u6570\u4ecemixin\u57fa\u7c7b\u7ee7\u627f\u3002\u8fd9\u6837\u7684\u7ee7\u627f\u88ab\u79f0\u4e3a\u5947\u602a\u7684\u5faa\u73af\u6a21\u677f\u6a21\u5f0f(\u7b80\u79f0CRTP)\uff0c\u5b83\u770b\u8d77\u6765\u50cf\u8fd9\u6837: 1 2 3 4 5 6 7 8 9 10 11 12 // The Curiously Recurring Template Pattern (CRTP) template < class T > class Mixin { // Mixin can use template type parameter T to access members of Derived // via (static_cast(this)) }; class Derived : public Mixin < Derived > { }; \u4f7f\u7528 CRTP \u7684\u7b80\u5355\u4f8b\u5b50\u53ef\u4ee5\u770b \u8fd9\u91cc . \u591a\u91cd\u7ee7\u627f\u5e26\u6765\u7684\u95ee\u9898 \u867d\u7136\u591a\u91cd\u7ee7\u627f\u4f3c\u4e4e\u662f\u5355\u7ee7\u627f\u7684\u7b80\u5355\u6269\u5c55\uff0c\u4f46\u591a\u91cd\u7ee7\u627f\u5f15\u5165\u4e86\u8bb8\u591a\u95ee\u9898\uff0c\u8fd9\u4e9b\u95ee\u9898\u53ef\u80fd\u663e\u8457\u589e\u52a0\u7a0b\u5e8f\u7684\u590d\u6742\u6027\uff0c\u5e76\u4f7f\u5176\u6210\u4e3a\u7ef4\u62a4\u7684\u5669\u68a6\u3002\u8ba9\u6211\u4eec\u6765\u770b\u770b\u4e00\u4e9b\u5177\u4f53\u60c5\u51b5\u3002 \u9996\u5148\uff0c\u5f53\u591a\u4e2a\u57fa\u7c7b\u5305\u542b\u5177\u6709\u76f8\u540c\u540d\u79f0\u7684\u51fd\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u4ea7\u751f\u6b67\u4e49\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include class USBDevice { private : long m_id {}; public : USBDevice ( long id ) : m_id { id } { } long getID () const { return m_id ; } }; class NetworkDevice { private : long m_id {}; public : NetworkDevice ( long id ) : m_id { id } { } long getID () const { return m_id ; } }; class WirelessAdapter : public USBDevice , public NetworkDevice { public : WirelessAdapter ( long usbId , long networkId ) : USBDevice { usbId }, NetworkDevice { networkId } { } }; int main () { WirelessAdapter c54G { 5442 , 181742 }; std :: cout << c54G . getID (); // Which getID() do we call? return 0 ; } \u5f53 c54G.getID() \u7f16\u8bd1\u7684\u65f6\u5019\uff0c\u7f16\u8bd1\u5668\u4f1a\u67e5\u770b WirelessAdapter \u4e2d\u662f\u5426\u6709\u540d\u4e3a getID() \u7684\u51fd\u6570\u3002\u6ca1\u6709\u3002\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u67e5\u770b\u7236\u7c7b\u4e2d\u662f\u5426\u5177\u6709\u540d\u4e3a getID() \u7684\u51fd\u6570\u3002\u770b\u5230\u95ee\u9898\u4e86\u5417\uff1f\u95ee\u9898\u662f c54G \u5b9e\u9645\u4e0a\u5305\u542b\u4e24\u4e2a getID() \u51fd\u6570\uff1a\u4e00\u4e2a\u7ee7\u627f\u81ea USBDevice \uff0c \u53e6\u4e00\u4e2a\u7ee7\u627f\u81eaNetworkDevice \u3002\u56e0\u6b64\uff0c\u8fd9\u4e2a\u51fd\u6570\u8c03\u7528\u662f\u6a21\u7cca\u7684\uff0c\u5982\u679c\u8bd5\u56fe\u7f16\u8bd1\u5b83\uff0c\u4f60\u5c06\u6536\u5230\u4e00\u4e2a\u7f16\u8bd1\u5668\u9519\u8bef\u3002 1 2 3 4 5 6 7 int main () { WirelessAdapter c54G { 5442 , 181742 }; std :: cout << c54G . USBDevice :: getID (); return 0 ; } \u867d\u7136\u4e0a\u8ff0\u89e3\u51b3\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u4f46\u662f\u5f53\u60a8\u7684\u7c7b\u7ee7\u627f4\u4e2a\u62166\u4e2a\u57fa\u7c7b(\u8fd9\u4e9b\u57fa\u7c7b\u672c\u8eab\u7ee7\u627f\u5176\u4ed6\u7c7b)\u65f6\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u4e8b\u60c5\u4f1a\u53d8\u5f97\u591a\u4e48\u590d\u6742\u3002\u968f\u7740\u7ee7\u627f\u7684\u7c7b\u8d8a\u6765\u8d8a\u591a\uff0c\u547d\u540d\u51b2\u7a81\u7684\u53ef\u80fd\u6027\u5448\u6307\u6570\u7ea7\u589e\u957f\uff0c\u9700\u8981\u663e\u5f0f\u5730\u89e3\u51b3\u6bcf\u4e00\u4e2a\u547d\u540d\u51b2\u7a81\u3002 \u5176\u6b21\uff0c \u83f1\u5f62\u7ee7\u627f\u95ee\u9898 \u66f4\u52a0\u4e25\u91cd\uff0c\u7b14\u8005\u5c06\u5176\u79f0\u4e3a\u201d\u83f1\u5f62\u672b\u65e5\u201c\u3002\u5f53\u4e00\u4e2a\u7c7b\u4ece\u4e24\u4e2a\u7c7b\u7ee7\u627f\uff0c\u800c\u8fd9\u4e24\u4e2a\u7c7b\u53c8\u7ee7\u627f\u81ea\u4e00\u4e2a\u5171\u540c\u7684\u7c7b\u65f6\uff0c\u5c31\u4f1a\u51fa\u73b0\u83f1\u5f62\u7ee7\u627f\u95ee\u9898\u3002\u5176\u7ee7\u627f\u5c42\u6b21\u7ed3\u6784\u770b\u4e0a\u53bb\u50cf\u4e00\u4e2a\u83f1\u5f62\u3002 \u4f8b\u5982\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class PoweredDevice { }; class Scanner : public PoweredDevice { }; class Printer : public PoweredDevice { }; class Copier : public Scanner , public Printer { }; \u626b\u63cf\u4eea\u548c\u6253\u5370\u673a\u90fd\u662f\u7535\u5668\uff0c\u56e0\u6b64\u5b83\u4eec\u7ee7\u627f PoweredDevice \u3002\u7136\u800c\uff0c\u590d\u5370\u673a\u7ed3\u5408\u4e86\u626b\u63cf\u4eea\u548c\u6253\u5370\u673a\u7684\u529f\u80fd\u3002 \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f1a\u51fa\u73b0\u8bb8\u591a\u95ee\u9898\uff1a\u4f8b\u5982\uff0c\u590d\u5370\u673a\u4e2d\u662f\u5426\u5e94\u8be5\u6709\u4e24\u4efdPoweredDevice\uff0c\u4ee5\u53ca\u5982\u4f55\u89e3\u51b3\u67d0\u4e9b\u7c7b\u578b\u7684\u6a21\u7cca\u5f15\u7528\u3002\u867d\u7136\u8fd9\u4e9b\u95ee\u9898\u4e2d\u7684\u5927\u591a\u6570\u90fd\u53ef\u4ee5\u901a\u8fc7\u663e\u5f0f\u4f5c\u7528\u57df\u6765\u89e3\u51b3\uff0c\u4f46\u4e3a\u4e86\u5904\u7406\u589e\u52a0\u7684\u590d\u6742\u6027\u800c\u5411\u7c7b\u6dfb\u52a0\u7684\u7ef4\u62a4\u5f00\u9500\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5f00\u53d1\u65f6\u95f4\u6025\u5267\u589e\u52a0\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u7ae0\u4e2d\u8ba8\u8bba\u66f4\u591a\u89e3\u51b3\u83f1\u5f62\u7ee7\u627f\u95ee\u9898\u7684\u65b9\u6cd5( 18.8 -- Virtual base classes )\u3002 \u591a\u91cd\u7ee7\u627f\u662f\u5426\u5f0a\u5927\u4e8e\u5229\uff1f \u4e8b\u5b9e\u8bc1\u660e\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u91cd\u7ee7\u627f\u89e3\u51b3\u7684\u5927\u591a\u6570\u95ee\u9898\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5355\u4e00\u7ee7\u627f\u89e3\u51b3\u3002\u8bb8\u591a\u9762\u5411\u5bf9\u8c61\u8bed\u8a00(\u5982\u3002Smalltalk, PHP)\u751a\u81f3\u4e0d\u652f\u6301\u591a\u91cd\u7ee7\u627f\u3002\u8bb8\u591a\u76f8\u5bf9\u73b0\u4ee3\u7684\u8bed\u8a00\uff0c\u5982Java\u548cc#\uff0c\u5c06\u7c7b\u9650\u5236\u4e3a\u666e\u901a\u7c7b\u7684\u5355\u4e00\u7ee7\u627f\uff0c\u4f46\u5141\u8bb8\u63a5\u53e3\u7c7b\u7684\u591a\u91cd\u7ee7\u627f(\u6211\u4eec\u5c06\u5728\u540e\u9762\u8ba8\u8bba)\u3002\u5728\u8fd9\u4e9b\u8bed\u8a00\u4e2d\u7981\u6b62\u591a\u91cd\u7ee7\u627f\u7684\u80cc\u540e\u7684\u9a71\u52a8\u601d\u60f3\u662f\uff0c\u5b83\u53ea\u4f1a\u4f7f\u8bed\u8a00\u8fc7\u4e8e\u590d\u6742\uff0c\u6700\u7ec8\u5bfc\u81f4\u7684\u95ee\u9898\u6bd4\u89e3\u51b3\u7684\u95ee\u9898\u66f4\u591a\u3002 \u8bb8\u591a\u4f5c\u8005\u548c\u6709\u7ecf\u9a8c\u7684\u7a0b\u5e8f\u5458\u8ba4\u4e3a\uff0c\u5e94\u8be5\u4e0d\u60dc\u4e00\u5207\u4ee3\u4ef7\u907f\u514dC++\u4e2d\u7684\u591a\u91cd\u7ee7\u627f\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5e26\u6765\u8bb8\u591a\u6f5c\u5728\u7684\u95ee\u9898\u3002\u7b14\u8005\u5e76\u4e0d\u540c\u610f\u8fd9\u79cd\u89c2\u70b9\uff0c\u56e0\u4e3a\u5728\u67d0\u4e9b\u65f6\u5019\u548c\u60c5\u51b5\u4e0b\uff0c\u591a\u91cd\u7ee7\u627f\u662f\u8fdb\u884c\u7684\u6700\u4f73\u65b9\u5f0f\u3002\u7136\u800c\uff0c\u591a\u91cd\u7ee7\u627f\u5e94\u8be5\u975e\u5e38\u660e\u667a\u5730\u4f7f\u7528\u3002 \u6709\u8da3\u7684\u662f\uff0c\u4f60\u5df2\u7ecf\u5728\u4f7f\u7528\u57fa\u4e8e\u591a\u91cd\u7ee7\u627f\u7f16\u5199\u7684\u7c7b\u800c\u4e0d\u81ea\u77e5\uff0c std:iostream \u5e93\u5bf9\u8c61 std::cin \u548c std::cout \u90fd\u662f\u4f7f\u7528\u591a\u91cd\u7ee7\u627f\u5b9e\u73b0\u7684\uff01 \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u591a\u91cd\u7ee7\u627f\uff0c\u9664\u975e\u5907\u9009\u65b9\u6848\u4f7f\u95ee\u9898\u53d8\u5f97\u66f4\u590d\u6742\u3002","title":"17.9 - \u591a\u91cd\u7ee7\u627f"},{"location":"17-9-multiple-inheritance/#mixins","text":"mixin (\u4e5f\u62fc\u4f5c \u201cmix-in\u201d) \u662f\u4e00\u4e2a\u5c0f\u7684\u7c7b\uff0c\u5b83\u53ef\u4ee5\u88ab\u7c7b\u7ee7\u627f\u4ee5\u4fbf\u4e3a\u5176\u6dfb\u52a0\u4e00\u4e9b\u5c5e\u6027\u3002\u5b83\u7684\u540d\u5b57mixin\uff08\u6df7\u5165\uff09\u8bf4\u660e\uff0c\u5b83\u7684\u7528\u9014\u662f\u88ab\u6dfb\u52a0\u5230\u5176\u4ed6\u7c7b\u4e2d\uff0c\u800c\u4e0d\u662f\u5355\u72ec\u88ab\u5b9e\u4f8b\u5316\u3002 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c Box \u548c Label \u7c7b\u90fd\u662f mixin\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f\u5b83\u4eec\u6765\u521b\u5efa\u65b0\u7684 Button class. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 // h/t to reader Waldo for this example #include struct Point2D { int x ; int y ; }; class Box // mixin Box class { public : void setTopLeft ( Point2D point ) { m_topLeft = point ; } void setBottomRight ( Point2D point ) { m_bottomRight = point ; } private : Point2D m_topLeft {}; Point2D m_bottomRight {}; }; class Label // mixin Label class { public : void setText ( const std :: string_view str ) { m_text = str ; } void setFontSize ( int fontSize ) { m_fontSize = fontSize ; } private : std :: string m_text {}; int m_fontSize {}; }; class Button : public Box , public Label {}; int main () { Button button {}; button . Box :: setTopLeft ({ 1 , 1 }); button . Box :: setBottomRight ({ 10 , 10 }); button . Label :: setText ( \"Username: \" ); button . Label :: setFontSize ( 6 ); } COPY \u6269\u5c55\u9605\u8bfb \u56e0\u4e3amixin\u88ab\u8bbe\u8ba1\u4e3a\u5411\u6d3e\u751f\u7c7b\u6dfb\u52a0\u529f\u80fd\uff0c\u800c\u4e0d\u662f\u63d0\u4f9b\u63a5\u53e3\uff0c\u6240\u4ee5mixin\u901a\u5e38\u4e0d\u4f7f\u7528\u865a\u51fd\u6570(\u4e0b\u4e00\u7ae0\u5c06\u8ba8\u8bba)\u3002\u76f8\u53cd\uff0c\u5982\u679c\u9700\u8981\u81ea\u5b9a\u4e49mixin\u7c7b\u4ee5\u7279\u5b9a\u7684\u65b9\u5f0f\u5de5\u4f5c\uff0c\u5219\u901a\u5e38\u4f7f\u7528\u6a21\u677f\u3002\u7531\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0cmixin\u7c7b\u901a\u5e38\u662f\u6a21\u677f\u5316\u7684\u3002 \u4e5f\u8bb8\u4ee4\u4eba\u60ca\u8bb6\u7684\u662f\uff0c\u6d3e\u751f\u7c7b\u53ef\u4ee5\u4f7f\u7528\u6d3e\u751f\u7c7b\u4f5c\u4e3a\u6a21\u677f\u7c7b\u578b\u53c2\u6570\u4ecemixin\u57fa\u7c7b\u7ee7\u627f\u3002\u8fd9\u6837\u7684\u7ee7\u627f\u88ab\u79f0\u4e3a\u5947\u602a\u7684\u5faa\u73af\u6a21\u677f\u6a21\u5f0f(\u7b80\u79f0CRTP)\uff0c\u5b83\u770b\u8d77\u6765\u50cf\u8fd9\u6837: 1 2 3 4 5 6 7 8 9 10 11 12 // The Curiously Recurring Template Pattern (CRTP) template < class T > class Mixin { // Mixin can use template type parameter T to access members of Derived // via (static_cast(this)) }; class Derived : public Mixin < Derived > { }; \u4f7f\u7528 CRTP \u7684\u7b80\u5355\u4f8b\u5b50\u53ef\u4ee5\u770b \u8fd9\u91cc .","title":"Mixins"},{"location":"17-9-multiple-inheritance/#_1","text":"\u867d\u7136\u591a\u91cd\u7ee7\u627f\u4f3c\u4e4e\u662f\u5355\u7ee7\u627f\u7684\u7b80\u5355\u6269\u5c55\uff0c\u4f46\u591a\u91cd\u7ee7\u627f\u5f15\u5165\u4e86\u8bb8\u591a\u95ee\u9898\uff0c\u8fd9\u4e9b\u95ee\u9898\u53ef\u80fd\u663e\u8457\u589e\u52a0\u7a0b\u5e8f\u7684\u590d\u6742\u6027\uff0c\u5e76\u4f7f\u5176\u6210\u4e3a\u7ef4\u62a4\u7684\u5669\u68a6\u3002\u8ba9\u6211\u4eec\u6765\u770b\u770b\u4e00\u4e9b\u5177\u4f53\u60c5\u51b5\u3002 \u9996\u5148\uff0c\u5f53\u591a\u4e2a\u57fa\u7c7b\u5305\u542b\u5177\u6709\u76f8\u540c\u540d\u79f0\u7684\u51fd\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u4ea7\u751f\u6b67\u4e49\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include class USBDevice { private : long m_id {}; public : USBDevice ( long id ) : m_id { id } { } long getID () const { return m_id ; } }; class NetworkDevice { private : long m_id {}; public : NetworkDevice ( long id ) : m_id { id } { } long getID () const { return m_id ; } }; class WirelessAdapter : public USBDevice , public NetworkDevice { public : WirelessAdapter ( long usbId , long networkId ) : USBDevice { usbId }, NetworkDevice { networkId } { } }; int main () { WirelessAdapter c54G { 5442 , 181742 }; std :: cout << c54G . getID (); // Which getID() do we call? return 0 ; } \u5f53 c54G.getID() \u7f16\u8bd1\u7684\u65f6\u5019\uff0c\u7f16\u8bd1\u5668\u4f1a\u67e5\u770b WirelessAdapter \u4e2d\u662f\u5426\u6709\u540d\u4e3a getID() \u7684\u51fd\u6570\u3002\u6ca1\u6709\u3002\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u67e5\u770b\u7236\u7c7b\u4e2d\u662f\u5426\u5177\u6709\u540d\u4e3a getID() \u7684\u51fd\u6570\u3002\u770b\u5230\u95ee\u9898\u4e86\u5417\uff1f\u95ee\u9898\u662f c54G \u5b9e\u9645\u4e0a\u5305\u542b\u4e24\u4e2a getID() \u51fd\u6570\uff1a\u4e00\u4e2a\u7ee7\u627f\u81ea USBDevice \uff0c \u53e6\u4e00\u4e2a\u7ee7\u627f\u81eaNetworkDevice \u3002\u56e0\u6b64\uff0c\u8fd9\u4e2a\u51fd\u6570\u8c03\u7528\u662f\u6a21\u7cca\u7684\uff0c\u5982\u679c\u8bd5\u56fe\u7f16\u8bd1\u5b83\uff0c\u4f60\u5c06\u6536\u5230\u4e00\u4e2a\u7f16\u8bd1\u5668\u9519\u8bef\u3002 1 2 3 4 5 6 7 int main () { WirelessAdapter c54G { 5442 , 181742 }; std :: cout << c54G . USBDevice :: getID (); return 0 ; } \u867d\u7136\u4e0a\u8ff0\u89e3\u51b3\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u4f46\u662f\u5f53\u60a8\u7684\u7c7b\u7ee7\u627f4\u4e2a\u62166\u4e2a\u57fa\u7c7b(\u8fd9\u4e9b\u57fa\u7c7b\u672c\u8eab\u7ee7\u627f\u5176\u4ed6\u7c7b)\u65f6\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u4e8b\u60c5\u4f1a\u53d8\u5f97\u591a\u4e48\u590d\u6742\u3002\u968f\u7740\u7ee7\u627f\u7684\u7c7b\u8d8a\u6765\u8d8a\u591a\uff0c\u547d\u540d\u51b2\u7a81\u7684\u53ef\u80fd\u6027\u5448\u6307\u6570\u7ea7\u589e\u957f\uff0c\u9700\u8981\u663e\u5f0f\u5730\u89e3\u51b3\u6bcf\u4e00\u4e2a\u547d\u540d\u51b2\u7a81\u3002 \u5176\u6b21\uff0c \u83f1\u5f62\u7ee7\u627f\u95ee\u9898 \u66f4\u52a0\u4e25\u91cd\uff0c\u7b14\u8005\u5c06\u5176\u79f0\u4e3a\u201d\u83f1\u5f62\u672b\u65e5\u201c\u3002\u5f53\u4e00\u4e2a\u7c7b\u4ece\u4e24\u4e2a\u7c7b\u7ee7\u627f\uff0c\u800c\u8fd9\u4e24\u4e2a\u7c7b\u53c8\u7ee7\u627f\u81ea\u4e00\u4e2a\u5171\u540c\u7684\u7c7b\u65f6\uff0c\u5c31\u4f1a\u51fa\u73b0\u83f1\u5f62\u7ee7\u627f\u95ee\u9898\u3002\u5176\u7ee7\u627f\u5c42\u6b21\u7ed3\u6784\u770b\u4e0a\u53bb\u50cf\u4e00\u4e2a\u83f1\u5f62\u3002 \u4f8b\u5982\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class PoweredDevice { }; class Scanner : public PoweredDevice { }; class Printer : public PoweredDevice { }; class Copier : public Scanner , public Printer { }; \u626b\u63cf\u4eea\u548c\u6253\u5370\u673a\u90fd\u662f\u7535\u5668\uff0c\u56e0\u6b64\u5b83\u4eec\u7ee7\u627f PoweredDevice \u3002\u7136\u800c\uff0c\u590d\u5370\u673a\u7ed3\u5408\u4e86\u626b\u63cf\u4eea\u548c\u6253\u5370\u673a\u7684\u529f\u80fd\u3002 \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f1a\u51fa\u73b0\u8bb8\u591a\u95ee\u9898\uff1a\u4f8b\u5982\uff0c\u590d\u5370\u673a\u4e2d\u662f\u5426\u5e94\u8be5\u6709\u4e24\u4efdPoweredDevice\uff0c\u4ee5\u53ca\u5982\u4f55\u89e3\u51b3\u67d0\u4e9b\u7c7b\u578b\u7684\u6a21\u7cca\u5f15\u7528\u3002\u867d\u7136\u8fd9\u4e9b\u95ee\u9898\u4e2d\u7684\u5927\u591a\u6570\u90fd\u53ef\u4ee5\u901a\u8fc7\u663e\u5f0f\u4f5c\u7528\u57df\u6765\u89e3\u51b3\uff0c\u4f46\u4e3a\u4e86\u5904\u7406\u589e\u52a0\u7684\u590d\u6742\u6027\u800c\u5411\u7c7b\u6dfb\u52a0\u7684\u7ef4\u62a4\u5f00\u9500\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5f00\u53d1\u65f6\u95f4\u6025\u5267\u589e\u52a0\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u7ae0\u4e2d\u8ba8\u8bba\u66f4\u591a\u89e3\u51b3\u83f1\u5f62\u7ee7\u627f\u95ee\u9898\u7684\u65b9\u6cd5( 18.8 -- Virtual base classes )\u3002","title":"\u591a\u91cd\u7ee7\u627f\u5e26\u6765\u7684\u95ee\u9898"},{"location":"17-9-multiple-inheritance/#_2","text":"\u4e8b\u5b9e\u8bc1\u660e\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u91cd\u7ee7\u627f\u89e3\u51b3\u7684\u5927\u591a\u6570\u95ee\u9898\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5355\u4e00\u7ee7\u627f\u89e3\u51b3\u3002\u8bb8\u591a\u9762\u5411\u5bf9\u8c61\u8bed\u8a00(\u5982\u3002Smalltalk, PHP)\u751a\u81f3\u4e0d\u652f\u6301\u591a\u91cd\u7ee7\u627f\u3002\u8bb8\u591a\u76f8\u5bf9\u73b0\u4ee3\u7684\u8bed\u8a00\uff0c\u5982Java\u548cc#\uff0c\u5c06\u7c7b\u9650\u5236\u4e3a\u666e\u901a\u7c7b\u7684\u5355\u4e00\u7ee7\u627f\uff0c\u4f46\u5141\u8bb8\u63a5\u53e3\u7c7b\u7684\u591a\u91cd\u7ee7\u627f(\u6211\u4eec\u5c06\u5728\u540e\u9762\u8ba8\u8bba)\u3002\u5728\u8fd9\u4e9b\u8bed\u8a00\u4e2d\u7981\u6b62\u591a\u91cd\u7ee7\u627f\u7684\u80cc\u540e\u7684\u9a71\u52a8\u601d\u60f3\u662f\uff0c\u5b83\u53ea\u4f1a\u4f7f\u8bed\u8a00\u8fc7\u4e8e\u590d\u6742\uff0c\u6700\u7ec8\u5bfc\u81f4\u7684\u95ee\u9898\u6bd4\u89e3\u51b3\u7684\u95ee\u9898\u66f4\u591a\u3002 \u8bb8\u591a\u4f5c\u8005\u548c\u6709\u7ecf\u9a8c\u7684\u7a0b\u5e8f\u5458\u8ba4\u4e3a\uff0c\u5e94\u8be5\u4e0d\u60dc\u4e00\u5207\u4ee3\u4ef7\u907f\u514dC++\u4e2d\u7684\u591a\u91cd\u7ee7\u627f\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5e26\u6765\u8bb8\u591a\u6f5c\u5728\u7684\u95ee\u9898\u3002\u7b14\u8005\u5e76\u4e0d\u540c\u610f\u8fd9\u79cd\u89c2\u70b9\uff0c\u56e0\u4e3a\u5728\u67d0\u4e9b\u65f6\u5019\u548c\u60c5\u51b5\u4e0b\uff0c\u591a\u91cd\u7ee7\u627f\u662f\u8fdb\u884c\u7684\u6700\u4f73\u65b9\u5f0f\u3002\u7136\u800c\uff0c\u591a\u91cd\u7ee7\u627f\u5e94\u8be5\u975e\u5e38\u660e\u667a\u5730\u4f7f\u7528\u3002 \u6709\u8da3\u7684\u662f\uff0c\u4f60\u5df2\u7ecf\u5728\u4f7f\u7528\u57fa\u4e8e\u591a\u91cd\u7ee7\u627f\u7f16\u5199\u7684\u7c7b\u800c\u4e0d\u81ea\u77e5\uff0c std:iostream \u5e93\u5bf9\u8c61 std::cin \u548c std::cout \u90fd\u662f\u4f7f\u7528\u591a\u91cd\u7ee7\u627f\u5b9e\u73b0\u7684\uff01 \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u591a\u91cd\u7ee7\u627f\uff0c\u9664\u975e\u5907\u9009\u65b9\u6848\u4f7f\u95ee\u9898\u53d8\u5f97\u66f4\u590d\u6742\u3002","title":"\u591a\u91cd\u7ee7\u627f\u662f\u5426\u5f0a\u5927\u4e8e\u5229\uff1f"},{"location":"17-x-Chapter-17-comprehensive-quiz/","tags":["summary"],"text":"\u590d\u4e60 17.1 - \u7ee7\u627f\u7b80\u4ecb 17.2 - C++\u7ee7\u627f\u57fa\u7840 \u901a\u8fc7\u7ee7\u627f\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u5728\u4e24\u4e2a\u5bf9\u8c61\u4e4b\u95f4\u5b9e\u73b0\u201dis-a\u201c\uff08\u662f\u4e00\u4e2a\uff09\u7684\u5173\u7cfb\u3002\u88ab\u7ee7\u627f\u7684\u5bf9\u8c61\u79f0\u4e3a\u7236\u7c7b\u6216\u8005\u57fa\u7c7b\uff0c\u6216\u8005\u8d85\u7c7b\u3002\u800c\u7ee7\u627f\u5f97\u5230\u7684\u5bf9\u8c61\u5219\u79f0\u4e3a\u5b50\u7c7b\u3001\u6d3e\u751f\u7c7b\u3002 \u5f53\u4e00\u4e2a\u7c7b\u4ece\u53e6\u4e00\u4e2a\u7c7b\u6d3e\u751f\u51fa\u6765\u65f6\uff0c\u5b83\u4f1a\u83b7\u5f97\u57fa\u7c7b\u7684\u5168\u90e8\u6210\u5458\u3002 17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f 17.4 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u548c\u521d\u59cb\u5316 \u5f53\u6d3e\u751f\u7c7b\u88ab\u6784\u9020\u65f6\uff0c\u5176\u5c5e\u4e8e\u57fa\u7c7b\u7684\u90e8\u5206\u4f1a\u9996\u5148\u88ab\u6784\u5efa\uff0c\u7136\u540e\u6d3e\u751f\u7c7b\u90e8\u5206\u624d\u4f1a\u6784\u5efa\u3002\u66f4\u591a\u5177\u4f53\u4fe1\u606f\u5982\u4e0b\uff1a \u6d3e\u751f\u7c7b\u7684\u5185\u5b58\u662f\u53e6\u5916\u5206\u914d\u7684\uff08\u8db3\u591f\u5b58\u653e\u57fa\u7c7b\u90e8\u5206\u7684\u6570\u636e\u548c\u6d3e\u751f\u7c7b\u90e8\u5206\u7684\u6570\u636e\uff09\uff1b \u8c03\u7528\u6d3e\u751f\u7c7b\u5408\u9002\u7684\u6784\u9020\u51fd\u6570\uff1b \u9996\u5148\u4f1a\u4f7f\u7528\u5408\u9002\u7684\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u6784\u9020\u57fa\u7c7b\u5bf9\u8c61\u3002\u5982\u679c\u57fa\u7c7b\u5bf9\u8c61\u6ca1\u6709\u88ab\u663e\u5f0f\u6307\u5b9a\uff0c\u5219\u8c03\u7528\u7684\u662f\u9ed8\u8ba4\u7684\u6784\u9020\u51fd\u6570\uff1b \u6d3e\u751f\u7c7b\u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u6765\u521d\u59cb\u5316\u5176\u6210\u5458\u53d8\u91cf\uff1b \u6267\u884c\u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\uff1b \u63a7\u5236\u6743\u4ea4\u8fd8\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002 \u6790\u6784\u7684\u987a\u5e8f\u5219\u6b63\u53f7\u76f8\u53cd\uff0c\u5148\u6790\u6784\u6d3e\u751f\u7c7b\u518d\u6790\u6784\u57fa\u7c7b\u3002 17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 C++ \u652f\u63013\u4e2a\u8bbf\u95ee\u63a7\u5236\u8bf4\u660e\u7b26\uff1a \u516c\u6709\u6210\u5458 \u3001 \u79c1\u6709\u6210\u5458 \u548c \u53d7\u4fdd\u62a4\u6210\u5458 \u3002protected \u5173\u952e\u5b57\u4fee\u9970\u7684\u6210\u5458\uff0c\u5141\u8bb8\u8be5\u7c7b\u3001 \u53cb\u5143\u7c7b \u548c\u6d3e\u751f\u7c7b\u8bbf\u95ee\uff0c\u4f46\u662f\u4e0d\u80fd\u88ab\u5176\u4ed6\u7c7b\u8bbf\u95ee\u3002 \u7c7b\u4e5f\u53ef\u4ee5\u901a\u8fc7 \u516c\u5f00\u7ee7\u627f \u3001 \u79c1\u6709\u7ee7\u627f \u6216 \u53d7\u4fdd\u62a4\u7ee7\u627f \u3002\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7c7b\u7684\u7ee7\u627f\u90fd\u662f\u516c\u5f00\u7684\u3002 \u4e0b\u8868\u5217\u4e3e\u4e86\u4e0d\u540c\u8bbf\u95ee\u8bf4\u660e\u7b26\u548c\u7ee7\u627f\u7c7b\u578b\u7684\u7ec4\u5408\uff1a \u57fa\u7c7b\u4e2d\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u516c\u5f00\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u79c1\u6709\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u4fdd\u62a4\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u516c\u5f00 \u516c\u5f00 \u79c1\u6709 \u4fdd\u62a4 \u79c1\u6709 \u4e0d\u53ef\u8bbf\u95ee \u4e0d\u53ef\u8bbf\u95ee \u4e0d\u53ef\u8bbf\u95ee \u4fdd\u62a4 \u4fdd\u62a4 \u79c1\u6709 \u4fdd\u62a4 17.6 - \u4e3a\u6d3e\u751f\u7c7b\u6dfb\u52a0\u65b0\u529f\u80fd 17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570 \u6d3e\u751f\u7c7b\u4e2d\u53ef\u4ee5\u6dfb\u52a0\u65b0\u7684\u51fd\u6570\uff0c\u4fee\u6539\u57fa\u7c7b\u7684\u51fd\u6570\uff0c\u4fee\u6539\u7ee7\u627f\u7684\u6210\u5458\u7684\u8bbf\u95ee\u89c4\u5219\u6216\u8005\u9690\u85cf\u67d0\u4e9b\u51fd\u6570\u3002 17.9 - \u591a\u91cd\u7ee7\u627f \u5f53\u6d3e\u751f\u7c7b\u4ece\u591a\u4e2a\u7236\u7c7b\u8fdb\u884c\u57fa\u7840\u65f6\uff0c\u79f0\u4e3a[[\u591a\u91cd\u7ee7\u627f]]\u3002\u4e00\u822c\u6765\u8bf4\u5e94\u8be5\u907f\u514d\u591a\u91cd\u7ee7\u627f\uff0c\u9664\u975e\u5b83\u80fd\u591f\u6781\u5927\u5730\u964d\u4f4e\u8bbe\u8ba1\u590d\u6742\u5ea6\u3002","title":"17.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7ee7\u627f"},{"location":"17-x-Chapter-17-comprehensive-quiz/#_1","text":"17.1 - \u7ee7\u627f\u7b80\u4ecb 17.2 - C++\u7ee7\u627f\u57fa\u7840 \u901a\u8fc7\u7ee7\u627f\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u5728\u4e24\u4e2a\u5bf9\u8c61\u4e4b\u95f4\u5b9e\u73b0\u201dis-a\u201c\uff08\u662f\u4e00\u4e2a\uff09\u7684\u5173\u7cfb\u3002\u88ab\u7ee7\u627f\u7684\u5bf9\u8c61\u79f0\u4e3a\u7236\u7c7b\u6216\u8005\u57fa\u7c7b\uff0c\u6216\u8005\u8d85\u7c7b\u3002\u800c\u7ee7\u627f\u5f97\u5230\u7684\u5bf9\u8c61\u5219\u79f0\u4e3a\u5b50\u7c7b\u3001\u6d3e\u751f\u7c7b\u3002 \u5f53\u4e00\u4e2a\u7c7b\u4ece\u53e6\u4e00\u4e2a\u7c7b\u6d3e\u751f\u51fa\u6765\u65f6\uff0c\u5b83\u4f1a\u83b7\u5f97\u57fa\u7c7b\u7684\u5168\u90e8\u6210\u5458\u3002 17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f 17.4 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u548c\u521d\u59cb\u5316 \u5f53\u6d3e\u751f\u7c7b\u88ab\u6784\u9020\u65f6\uff0c\u5176\u5c5e\u4e8e\u57fa\u7c7b\u7684\u90e8\u5206\u4f1a\u9996\u5148\u88ab\u6784\u5efa\uff0c\u7136\u540e\u6d3e\u751f\u7c7b\u90e8\u5206\u624d\u4f1a\u6784\u5efa\u3002\u66f4\u591a\u5177\u4f53\u4fe1\u606f\u5982\u4e0b\uff1a \u6d3e\u751f\u7c7b\u7684\u5185\u5b58\u662f\u53e6\u5916\u5206\u914d\u7684\uff08\u8db3\u591f\u5b58\u653e\u57fa\u7c7b\u90e8\u5206\u7684\u6570\u636e\u548c\u6d3e\u751f\u7c7b\u90e8\u5206\u7684\u6570\u636e\uff09\uff1b \u8c03\u7528\u6d3e\u751f\u7c7b\u5408\u9002\u7684\u6784\u9020\u51fd\u6570\uff1b \u9996\u5148\u4f1a\u4f7f\u7528\u5408\u9002\u7684\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u6784\u9020\u57fa\u7c7b\u5bf9\u8c61\u3002\u5982\u679c\u57fa\u7c7b\u5bf9\u8c61\u6ca1\u6709\u88ab\u663e\u5f0f\u6307\u5b9a\uff0c\u5219\u8c03\u7528\u7684\u662f\u9ed8\u8ba4\u7684\u6784\u9020\u51fd\u6570\uff1b \u6d3e\u751f\u7c7b\u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u6765\u521d\u59cb\u5316\u5176\u6210\u5458\u53d8\u91cf\uff1b \u6267\u884c\u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\uff1b \u63a7\u5236\u6743\u4ea4\u8fd8\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002 \u6790\u6784\u7684\u987a\u5e8f\u5219\u6b63\u53f7\u76f8\u53cd\uff0c\u5148\u6790\u6784\u6d3e\u751f\u7c7b\u518d\u6790\u6784\u57fa\u7c7b\u3002 17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 C++ \u652f\u63013\u4e2a\u8bbf\u95ee\u63a7\u5236\u8bf4\u660e\u7b26\uff1a \u516c\u6709\u6210\u5458 \u3001 \u79c1\u6709\u6210\u5458 \u548c \u53d7\u4fdd\u62a4\u6210\u5458 \u3002protected \u5173\u952e\u5b57\u4fee\u9970\u7684\u6210\u5458\uff0c\u5141\u8bb8\u8be5\u7c7b\u3001 \u53cb\u5143\u7c7b \u548c\u6d3e\u751f\u7c7b\u8bbf\u95ee\uff0c\u4f46\u662f\u4e0d\u80fd\u88ab\u5176\u4ed6\u7c7b\u8bbf\u95ee\u3002 \u7c7b\u4e5f\u53ef\u4ee5\u901a\u8fc7 \u516c\u5f00\u7ee7\u627f \u3001 \u79c1\u6709\u7ee7\u627f \u6216 \u53d7\u4fdd\u62a4\u7ee7\u627f \u3002\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7c7b\u7684\u7ee7\u627f\u90fd\u662f\u516c\u5f00\u7684\u3002 \u4e0b\u8868\u5217\u4e3e\u4e86\u4e0d\u540c\u8bbf\u95ee\u8bf4\u660e\u7b26\u548c\u7ee7\u627f\u7c7b\u578b\u7684\u7ec4\u5408\uff1a \u57fa\u7c7b\u4e2d\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u516c\u5f00\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u79c1\u6709\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u4fdd\u62a4\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u516c\u5f00 \u516c\u5f00 \u79c1\u6709 \u4fdd\u62a4 \u79c1\u6709 \u4e0d\u53ef\u8bbf\u95ee \u4e0d\u53ef\u8bbf\u95ee \u4e0d\u53ef\u8bbf\u95ee \u4fdd\u62a4 \u4fdd\u62a4 \u79c1\u6709 \u4fdd\u62a4 17.6 - \u4e3a\u6d3e\u751f\u7c7b\u6dfb\u52a0\u65b0\u529f\u80fd 17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570 \u6d3e\u751f\u7c7b\u4e2d\u53ef\u4ee5\u6dfb\u52a0\u65b0\u7684\u51fd\u6570\uff0c\u4fee\u6539\u57fa\u7c7b\u7684\u51fd\u6570\uff0c\u4fee\u6539\u7ee7\u627f\u7684\u6210\u5458\u7684\u8bbf\u95ee\u89c4\u5219\u6216\u8005\u9690\u85cf\u67d0\u4e9b\u51fd\u6570\u3002 17.9 - \u591a\u91cd\u7ee7\u627f \u5f53\u6d3e\u751f\u7c7b\u4ece\u591a\u4e2a\u7236\u7c7b\u8fdb\u884c\u57fa\u7840\u65f6\uff0c\u79f0\u4e3a[[\u591a\u91cd\u7ee7\u627f]]\u3002\u4e00\u822c\u6765\u8bf4\u5e94\u8be5\u907f\u514d\u591a\u91cd\u7ee7\u627f\uff0c\u9664\u975e\u5b83\u80fd\u591f\u6781\u5927\u5730\u964d\u4f4e\u8bbe\u8ba1\u590d\u6742\u5ea6\u3002","title":"\u590d\u4e60"},{"location":"18-1-pointers-and-references-to-the-base-class-of-derived-objects/","tags":["pointers","references"],"text":"\u5728\u524d\u9762\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u901a\u8fc7 \u7ee7\u627f \u6765\u6d3e\u751f\u65b0\u7684\u7c7b\u3002\u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u7ee7\u627f\u4e2d\u6700\u91cd\u8981\u4e5f\u662f\u6700\u5f3a\u5927\u7684\u4e00\u9762\u2014\u2014\u865a\u51fd\u6570\u3002 \u5728\u6211\u4eec\u8ba8\u8bba\u865a\u51fd\u6570\u4e4b\u524d\uff0c\u5148\u6765\u4e86\u89e3\u4e00\u4e0b\uff0c\u4e3a\u4ec0\u4e48\u865a\u51fd\u6570\u662f\u6709\u7528\u7684\u3002 \u901a\u8fc7 17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f \u7684\u5b66\u4e60\uff0c\u6211\u4eec\u6307\u5b9a\uff0c\u5f53\u521b\u5efa\u4e00\u4e2a\u6d3e\u751f\u7c7b\u65f6\uff0c\u6d3e\u751f\u7c7b\u5b9e\u9645\u4e0a\u662f\u7531\u591a\u4e2a\u90e8\u5206\u7ec4\u6210\u7684\uff1a\u4e00\u90e8\u5206\u5c5e\u4e8e\u5176\u7236\u7c7b\uff0c\u53e6\u4e00\u90e8\u5206\u5c5e\u4e8e\u5b83\u81ea\u5df1\u3002 \u4f8b\u5982\uff0c\u8fd9\u91cc\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include class Base { protected : int m_value {}; public : Base ( int value ) : m_value { value } { } std :: string_view getName () const { return \"Base\" ; } int getValue () const { return m_value ; } }; class Derived : public Base { public : Derived ( int value ) : Base { value } { } std :: string_view getName () const { return \"Derived\" ; } int getValueDoubled () const { return m_value * 2 ; } }; \u5f53\u6211\u4eec\u521b\u5efa Derived \u5bf9\u8c61\u65f6\uff0c\u5b83\u5305\u542b Base \u7684\u90e8\u5206\uff08\u9996\u5148\u6784\u5efa\uff09\u4ee5\u53ca\u5c5e\u4e8e Derived \u7684\u90e8\u5206 (\u968f\u540e\u6784\u5efa)\u3002\u6ce8\u610f\uff0c\u7ee7\u627f\u5173\u7cfb\u9690\u542b\u7684\u662f\u5bf9\u8c61A\u201c\u662f\u4e00\u79cd\u201d\u5bf9\u8c61B\u7684\u542b\u4e49\u3002\u56e0\u4e3a Derived \u662f\u4e00\u4e2a Base \uff0c\u6240\u4ee5 Derived \u5305\u542b Base \u7684\u90e8\u5206\u662f\u5408\u7406\u7684\u3002 \u6307\u9488\uff0c\u5f15\u7528\u548c\u6d3e\u751f\u7c7b \u521b\u5efa\u4e00\u4e2a\u6307\u5411 Derived \u5bf9\u8c61\u7684\u6307\u9488\u548c\u5f15\u7528\u5f88\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { Derived derived { 5 }; std :: cout << \"derived is a \" << derived . getName () << \" and has value \" << derived . getValue () << '\\n' ; Derived & rDerived { derived }; std :: cout << \"rDerived is a \" << rDerived . getName () << \" and has value \" << rDerived . getValue () << '\\n' ; Derived * pDerived { & derived }; std :: cout << \"pDerived is a \" << pDerived -> getName () << \" and has value \" << pDerived -> getValue () << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 derived is a Derived and has value 5 rDerived is a Derived and has value 5 pDerived is a Derived and has value 5 \u4e0d\u8fc7\uff0c\u7531\u4e8e Derived \u4e2d\u5305\u542b\u5c5e\u4e8e Base \u7684\u90e8\u5206\uff0c\u90a3\u4e48\u6211\u4eec\u4e0d\u7981\u8981\u95ee\uff1aC++\u5141\u8bb8\u6211\u4eec\u5c06 Base \u7c7b\u578b\u6307\u9488\u6307\u5411 Derived \u5bf9\u8c61\u5417\uff1f\u7b54\u6848\u662f\u53ef\u4ee5\u7684\uff01 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { Derived derived { 5 }; // These are both legal! Base & rBase { derived }; Base * pBase { & derived }; std :: cout << \"derived is a \" << derived . getName () << \" and has value \" << derived . getValue () << '\\n' ; std :: cout << \"rBase is a \" << rBase . getName () << \" and has value \" << rBase . getValue () << '\\n' ; std :: cout << \"pBase is a \" << pBase -> getName () << \" and has value \" << pBase -> getValue () << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 derived is a Derived and has value 5 rBase is a Base and has value 5 pBase is a Base and has value 5 \u8f93\u51fa\u7ed3\u679c\u53ef\u80fd\u548c\u4f60\u60f3\u7684\u5e76\u4e0d\u4e00\u6837\uff01 \u4e8b\u5b9e\u8bc1\u660e\uff0c\u7531\u4e8e rBase \u548c pBase \u662f Base \u5f15\u7528\u548c\u6307\u9488\uff0c\u5b83\u4eec\u53ea\u80fd\u770b\u5230 Base \u7684\u6210\u5458(\u6216 Base \u7236\u7c7b\u4eec\u7684\u6210\u5458)\u3002\u56e0\u6b64\uff0c\u5373\u4f7f Derived::getName() \u906e\u853d(\u9690\u85cf)\u4e86 Derived \u5bf9\u8c61\u7684 Base::getName() \uff0c Base \u6307\u9488/\u5f15\u7528\u4e5f\u4e0d\u80fd\u770b\u5230 Derived::getName() \u3002\u56e0\u6b64\uff0c\u5b83\u4f1a\u8c03\u7528 Base::getName() \uff0c\u8fd9\u5c31\u662f rBase \u548c pBase \u6253\u5370\u5b83\u4eec\u662f Base \u800c\u4e0d\u662f Derived \u7684\u539f\u56e0\u3002 \u6ce8\u610f\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u4f60\u4e0d\u80fd\u901a\u8fc7 rBase \u6216\u8005 pBase \u6765\u8c03\u7528 Derived::getValueDoubled() \u3002\u56e0\u4e3a\u5b83\u4eec\u770b\u4e0d\u5230\u8be5\u51fd\u6570\u3002 \u8fd9\u662f\u53e6\u4e00\u4e2a\u7a0d\u5fae\u590d\u6742\u4e00\u70b9\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\u4e2d\u4f7f\u7528\u5b83\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 #include #include #include class Animal { protected : std :: string m_name ; // \u5c06\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u8bbe\u7f6e\u4e3a\u53d7\u4fdd\u62a4\u7684\uff0c\u56e0\u4e3a // \u6211\u4eec\u4e0d\u5e0c\u671b\u7528\u6237\u80fd\u591f\u76f4\u63a5\u521b\u5efa Animal \u5bf9\u8c61\uff0c // \u4f46\u662f\u6211\u4eec\u4ecd\u7136\u5e0c\u671b\u80fd\u591f\u4f7f\u7528\u5b83\u6765\u6d3e\u751f\u7c7b\u3002 Animal ( std :: string_view name ) : m_name { name } { } // To prevent slicing (covered later) Animal ( const Animal & ) = default ; Animal & operator = ( const Animal & ) = default ; public : std :: string_view getName () const { return m_name ; } std :: string_view speak () const { return \"???\" ; } }; class Cat : public Animal { public : Cat ( std :: string_view name ) : Animal { name } { } std :: string_view speak () const { return \"Meow\" ; } }; class Dog : public Animal { public : Dog ( std :: string_view name ) : Animal { name } { } std :: string_view speak () const { return \"Woof\" ; } }; int main () { const Cat cat { \"Fred\" }; std :: cout << \"cat is named \" << cat . getName () << \", and it says \" << cat . speak () << '\\n' ; const Dog dog { \"Garbo\" }; std :: cout << \"dog is named \" << dog . getName () << \", and it says \" << dog . speak () << '\\n' ; const Animal * pAnimal { & cat }; std :: cout << \"pAnimal is named \" << pAnimal -> getName () << \", and it says \" << pAnimal -> speak () << '\\n' ; pAnimal = & dog ; std :: cout << \"pAnimal is named \" << pAnimal -> getName () << \", and it says \" << pAnimal -> speak () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c 1 2 3 4 cat is named Fred, and it says Meow dog is named Garbo, and it says Woof pAnimal is named Fred, and it says ??? pAnimal is named Garbo, and it says ??? \u548c\u4e4b\u524d\u7684\u95ee\u9898\u7c7b\u4f3c\uff0c\u56e0\u4e3a pAnimal \u662f\u4e00\u4e2a Animal \u7c7b\u578b\u7684\u6307\u9488\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u770b\u5230 Animal \u7684\u90e8\u5206\u3002\u5176\u7ed3\u679c\u5c31\u662f pAnimal->speak() \u8c03\u7528\u7684\u662f Animal::speak() \u800c\u4e0d\u662f Dog::Speak() \u6216\u8005 Cat::speak() \u3002 \u4f7f\u7528\u6307\u5411\u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 \u73b0\u5728\u4f60\u53ef\u80fd\u4f1a\u8bf4\uff0c\u201c\u4e0a\u9762\u7684\u4f8b\u5b50\u770b\u8d77\u6765\u6709\u70b9\u50bb\u3002\u5f53\u6211\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u6d3e\u751f\u5bf9\u8c61\u65f6\uff0c\u4e3a\u4ec0\u4e48\u8981\u8bbe\u7f6e\u6307\u5411\u6d3e\u751f\u5bf9\u8c61\u57fa\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\u5462?\u201d\u4e8b\u5b9e\u8bc1\u660e\uff0c\u8fd9\u4e48\u505a\u5f88\u6709\u7528\u3002 \u9996\u5148\uff0c\u5047\u8bbe\u4f60\u60f3\u8981\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u6253\u5370\u52a8\u7269\u7684\u540d\u5b57\u548c\u58f0\u97f3\u3002\u5982\u679c\u4e0d\u4f7f\u7528\u6307\u5411\u57fa\u7c7b\u7684\u6307\u9488\uff0c\u4f60\u5c31\u5fc5\u987b\u4f7f\u7528\u91cd\u8f7d\u51fd\u6570\u6765\u7f16\u5199\u5b83\uff0c\u50cf\u8fd9\u6837: 1 2 3 4 5 6 7 8 9 10 //\u51fd\u6570\u91cd\u8f7d\uff08\u7b7e\u540d\u4e0d\u540c\uff09 void report ( const Cat & cat ) { std :: cout << cat . getName () << \" says \" << cat . speak () << '\\n' ; } void report ( const Dog & dog ) { std :: cout << dog . getName () << \" says \" << dog . speak () << '\\n' ; } \u597d\u50cf\u6ca1\u5565\u95ee\u9898\uff0c\u4f46\u60f3\u60f3\u5982\u679c\u6211\u4eec\u4e0d\u6b62\u6709\u4e24\u79cd\u52a8\u7269\u800c\u662f\u670930\u79cd\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002\u4f60\u5fc5\u987b\u7f16\u519930\u4e2a\u51e0\u4e4e\u76f8\u540c\u7684\u51fd\u6570\uff01\u53e6\u5916\uff0c\u5982\u679c\u4f60\u6dfb\u52a0\u4e86\u4e00\u79cd\u65b0\u7684\u52a8\u7269\u7c7b\u578b\uff0c\u4f60\u4e5f\u5fc5\u987b\u4e3a\u5b83\u5199\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u3002\u8003\u8651\u5230\u8fd9\u4e9b\u51fd\u6570\u7684\u552f\u4e00\u533a\u522b\u5c31\u662f\u53c2\u6570\u4e0d\u540c\uff0c\u8fd9\u662f\u65e0\u7591\u662f\u4e00\u79cd\u5de8\u5927\u7684\u6d6a\u8d39\u3002 \u4e0d\u8fc7\uff0c\u56e0\u4e3a Cat \u548c Dog \u90fd\u6d3e\u751f\u81ea Animal \uff0c Cat \u548c Dog \u4e2d\u4e00\u5b9a\u5177\u6709\u5c5e\u4e8e Animal \u7684\u90e8\u5206\u3002\u56e0\u6b64\uff0c\u5982\u679c\u80fd\u50cf\u4e0b\u9762\u8fd9\u4e48\u505a\u5c31\u597d\u4e86\uff1a 1 2 3 4 void report ( const Animal & rAnimal ) { std :: cout << rAnimal . getName () << \" says \" << rAnimal . speak () << '\\n' ; } \u8fd9\u5c06\u5141\u8bb8\u6211\u4eec\u4f20\u5165\u4ece Animal \u6d3e\u751f\u7684\u4efb\u4f55\u7c7b\uff0c\u751a\u81f3\u662f\u5728\u7f16\u5199\u51fd\u6570\u4e4b\u540e\u521b\u5efa\u7684\u7c7b\uff01\u4e0d\u9700\u8981\u4e3a\u6bcf\u4e2a\u6d3e\u751f\u7c7b\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u800c\u662f\u4e0e\u6240\u6709\u6d3e\u751f\u81ea Animal \u7684\u7c7b\u53ef\u4ee5\u5171\u7528\u7684\u4e00\u4e2a\u51fd\u6570\uff01 \u5f53\u7136\uff0c\u8fd9\u4e48\u505a\u6700\u5927\u7684\u95ee\u9898\u5728\u4e8e rAnimal \u662f Animal \u7684\u5f15\u7528\uff0c\u6240\u4ee5 rAnimal.speak() \u4f1a\u8c03\u7528 Animal::speak() \u800c\u4e0d\u662f\u6d3e\u751f\u7c7b\u4e2d\u7684 speak() \u3002 \u5176\u6b21\uff0c\u5047\u8bbe\u4f60\u67093\u53ea\u732b\u548c3\u53ea\u72d7\uff0c\u4f60\u60f3\u628a\u5b83\u4eec\u653e\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\u4ee5\u4fbf\u4e8e\u8bbf\u95ee\u3002\u56e0\u4e3a\u6570\u7ec4\u53ea\u80fd\u4fdd\u5b58\u4e00\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u6ca1\u6709\u6307\u5411\u57fa\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u4f60\u5fc5\u987b\u4e3a\u6bcf\u4e2a\u6d3e\u751f\u7c7b\u578b\u521b\u5efa\u4e0d\u540c\u7684\u6570\u7ec4\uff0c\u50cf\u8fd9\u6837: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include // Cat and Dog from the example above int main () { const auto & cats { std :: to_array < Cat > ({{ \"Fred\" }, { \"Misty\" }, { \"Zeke\" }}) }; const auto & dogs { std :: to_array < Dog > ({{ \"Garbo\" }, { \"Pooky\" }, { \"Truffle\" }}) }; // Before C++20 // const std::array cats{{ { \"Fred\" }, { \"Misty\" }, { \"Zeke\" } }}; // const std::array dogs{{ { \"Garbo\" }, { \"Pooky\" }, { \"Truffle\" } }}; for ( const auto & cat : cats ) { std :: cout << cat . getName () << \" says \" << cat . speak () << '\\n' ; } for ( const auto & dog : dogs ) { std :: cout << dog . getName () << \" says \" << dog . speak () << '\\n' ; } return 0 ; } \u5982\u679c\u670930\u79cd\u4e0d\u540c\u7684\u52a8\u7269\u5462\uff1f\u5c82\u4e0d\u662f\u8981\u5b9a\u4e4930\u4e2a\u6570\u7ec4\uff0c\u6bcf\u4e2a\u52a8\u7269\u90fd\u9700\u8981\u4e00\u4e2a\u6570\u7ec4\uff01 \u4e0d\u8fc7\uff0c\u56e0\u4e3a Cat \u548c Dog \u90fd\u6d3e\u751f\u81ea\u4e8e Animal \uff0c\u6240\u4ee5\u53ef\u4ee5\u8fd9\u6837\u505a: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include #include // Cat and Dog from the example above int main () { const Cat fred { \"Fred\" }; const Cat misty { \"Misty\" }; const Cat zeke { \"Zeke\" }; const Dog garbo { \"Garbo\" }; const Dog pooky { \"Pooky\" }; const Dog truffle { \"Truffle\" }; // Set up an array of pointers to animals, and set those pointers to our Cat and Dog objects // Note: to_array requires C++20 support (and at the time of writing, Visual Studio 2022 still doesn't support it correctly) const auto animals { std :: to_array < const Animal *> ({ & fred , & garbo , & misty , & pooky , & truffle , & zeke }) }; // Before C++20, with the array size being explicitly specified // const std::array animals{ &fred, &garbo, &misty, &pooky, &truffle, &zeke }; for ( const auto animal : animals ) { std :: cout << animal -> getName () << \" says \" << animal -> speak () << '\\n' ; } return 0 ; } \u5c3d\u7ba1\u4e0a\u8ff0\u4ee3\u7801\u53ef\u4ee5\u7f16\u8bd1\u548c\u6267\u884c\uff0c\u4f46\u53ef\u60dc\u7684\u662f\uff0c\u7531\u4e8e\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f Animal \u7c7b\u578b\u7684\u6307\u9488\uff0c\u90a3\u5c31\u610f\u5473\u7740 animal->speak() \u8c03\u7528\u7684\u4f1a\u662f Animal::speak() \u800c\u4e0d\u662f\u6d3e\u751f\u7c7b\u5404\u81ea\u7684 speak() \u51fd\u6570\u3002\u8f93\u51fa\u7ed3\u679c\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 Fred says ??? Garbo says ??? Misty says ??? Pooky says ??? Truffle says ??? Zeke says ??? \u867d\u7136\u8fd9\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8282\u7701\u5927\u91cf\u7684\u65f6\u95f4\u548c\u7cbe\u529b\uff0c\u4f46\u5b83\u4eec\u90fd\u6709\u76f8\u540c\u7684\u95ee\u9898\u3002\u57fa\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\u8c03\u7528\u51fd\u6570\u7684\u57fa\u7c7b\u7248\u672c\u800c\u4e0d\u662f\u6d3e\u751f\u7248\u672c\u3002\u8981\u662f\u6709\u529e\u6cd5\u8ba9\u8fd9\u4e9b\u57fa\u6307\u9488\u8c03\u7528\u51fd\u6570\u7684\u6d3e\u751f\u7248\u672c\u800c\u4e0d\u662f\u57fa\u7248\u672c\u5c31\u597d\u4e86\u2026 \u4f60\u731c\uff0c\u865a\u51fd\u6570\u662f\u505a\u4ec0\u4e48\u7528\u7684\uff1f\uff08\u4e0d\u4f1a\u90a3\u4e48\u5de7\u5427\uff09\u3002","title":"18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528"},{"location":"18-1-pointers-and-references-to-the-base-class-of-derived-objects/#_1","text":"\u521b\u5efa\u4e00\u4e2a\u6307\u5411 Derived \u5bf9\u8c61\u7684\u6307\u9488\u548c\u5f15\u7528\u5f88\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { Derived derived { 5 }; std :: cout << \"derived is a \" << derived . getName () << \" and has value \" << derived . getValue () << '\\n' ; Derived & rDerived { derived }; std :: cout << \"rDerived is a \" << rDerived . getName () << \" and has value \" << rDerived . getValue () << '\\n' ; Derived * pDerived { & derived }; std :: cout << \"pDerived is a \" << pDerived -> getName () << \" and has value \" << pDerived -> getValue () << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 derived is a Derived and has value 5 rDerived is a Derived and has value 5 pDerived is a Derived and has value 5 \u4e0d\u8fc7\uff0c\u7531\u4e8e Derived \u4e2d\u5305\u542b\u5c5e\u4e8e Base \u7684\u90e8\u5206\uff0c\u90a3\u4e48\u6211\u4eec\u4e0d\u7981\u8981\u95ee\uff1aC++\u5141\u8bb8\u6211\u4eec\u5c06 Base \u7c7b\u578b\u6307\u9488\u6307\u5411 Derived \u5bf9\u8c61\u5417\uff1f\u7b54\u6848\u662f\u53ef\u4ee5\u7684\uff01 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { Derived derived { 5 }; // These are both legal! Base & rBase { derived }; Base * pBase { & derived }; std :: cout << \"derived is a \" << derived . getName () << \" and has value \" << derived . getValue () << '\\n' ; std :: cout << \"rBase is a \" << rBase . getName () << \" and has value \" << rBase . getValue () << '\\n' ; std :: cout << \"pBase is a \" << pBase -> getName () << \" and has value \" << pBase -> getValue () << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 derived is a Derived and has value 5 rBase is a Base and has value 5 pBase is a Base and has value 5 \u8f93\u51fa\u7ed3\u679c\u53ef\u80fd\u548c\u4f60\u60f3\u7684\u5e76\u4e0d\u4e00\u6837\uff01 \u4e8b\u5b9e\u8bc1\u660e\uff0c\u7531\u4e8e rBase \u548c pBase \u662f Base \u5f15\u7528\u548c\u6307\u9488\uff0c\u5b83\u4eec\u53ea\u80fd\u770b\u5230 Base \u7684\u6210\u5458(\u6216 Base \u7236\u7c7b\u4eec\u7684\u6210\u5458)\u3002\u56e0\u6b64\uff0c\u5373\u4f7f Derived::getName() \u906e\u853d(\u9690\u85cf)\u4e86 Derived \u5bf9\u8c61\u7684 Base::getName() \uff0c Base \u6307\u9488/\u5f15\u7528\u4e5f\u4e0d\u80fd\u770b\u5230 Derived::getName() \u3002\u56e0\u6b64\uff0c\u5b83\u4f1a\u8c03\u7528 Base::getName() \uff0c\u8fd9\u5c31\u662f rBase \u548c pBase \u6253\u5370\u5b83\u4eec\u662f Base \u800c\u4e0d\u662f Derived \u7684\u539f\u56e0\u3002 \u6ce8\u610f\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u4f60\u4e0d\u80fd\u901a\u8fc7 rBase \u6216\u8005 pBase \u6765\u8c03\u7528 Derived::getValueDoubled() \u3002\u56e0\u4e3a\u5b83\u4eec\u770b\u4e0d\u5230\u8be5\u51fd\u6570\u3002 \u8fd9\u662f\u53e6\u4e00\u4e2a\u7a0d\u5fae\u590d\u6742\u4e00\u70b9\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\u4e2d\u4f7f\u7528\u5b83\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 #include #include #include class Animal { protected : std :: string m_name ; // \u5c06\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u8bbe\u7f6e\u4e3a\u53d7\u4fdd\u62a4\u7684\uff0c\u56e0\u4e3a // \u6211\u4eec\u4e0d\u5e0c\u671b\u7528\u6237\u80fd\u591f\u76f4\u63a5\u521b\u5efa Animal \u5bf9\u8c61\uff0c // \u4f46\u662f\u6211\u4eec\u4ecd\u7136\u5e0c\u671b\u80fd\u591f\u4f7f\u7528\u5b83\u6765\u6d3e\u751f\u7c7b\u3002 Animal ( std :: string_view name ) : m_name { name } { } // To prevent slicing (covered later) Animal ( const Animal & ) = default ; Animal & operator = ( const Animal & ) = default ; public : std :: string_view getName () const { return m_name ; } std :: string_view speak () const { return \"???\" ; } }; class Cat : public Animal { public : Cat ( std :: string_view name ) : Animal { name } { } std :: string_view speak () const { return \"Meow\" ; } }; class Dog : public Animal { public : Dog ( std :: string_view name ) : Animal { name } { } std :: string_view speak () const { return \"Woof\" ; } }; int main () { const Cat cat { \"Fred\" }; std :: cout << \"cat is named \" << cat . getName () << \", and it says \" << cat . speak () << '\\n' ; const Dog dog { \"Garbo\" }; std :: cout << \"dog is named \" << dog . getName () << \", and it says \" << dog . speak () << '\\n' ; const Animal * pAnimal { & cat }; std :: cout << \"pAnimal is named \" << pAnimal -> getName () << \", and it says \" << pAnimal -> speak () << '\\n' ; pAnimal = & dog ; std :: cout << \"pAnimal is named \" << pAnimal -> getName () << \", and it says \" << pAnimal -> speak () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c 1 2 3 4 cat is named Fred, and it says Meow dog is named Garbo, and it says Woof pAnimal is named Fred, and it says ??? pAnimal is named Garbo, and it says ??? \u548c\u4e4b\u524d\u7684\u95ee\u9898\u7c7b\u4f3c\uff0c\u56e0\u4e3a pAnimal \u662f\u4e00\u4e2a Animal \u7c7b\u578b\u7684\u6307\u9488\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u770b\u5230 Animal \u7684\u90e8\u5206\u3002\u5176\u7ed3\u679c\u5c31\u662f pAnimal->speak() \u8c03\u7528\u7684\u662f Animal::speak() \u800c\u4e0d\u662f Dog::Speak() \u6216\u8005 Cat::speak() \u3002","title":"\u6307\u9488\uff0c\u5f15\u7528\u548c\u6d3e\u751f\u7c7b"},{"location":"18-1-pointers-and-references-to-the-base-class-of-derived-objects/#_2","text":"\u73b0\u5728\u4f60\u53ef\u80fd\u4f1a\u8bf4\uff0c\u201c\u4e0a\u9762\u7684\u4f8b\u5b50\u770b\u8d77\u6765\u6709\u70b9\u50bb\u3002\u5f53\u6211\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u6d3e\u751f\u5bf9\u8c61\u65f6\uff0c\u4e3a\u4ec0\u4e48\u8981\u8bbe\u7f6e\u6307\u5411\u6d3e\u751f\u5bf9\u8c61\u57fa\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\u5462?\u201d\u4e8b\u5b9e\u8bc1\u660e\uff0c\u8fd9\u4e48\u505a\u5f88\u6709\u7528\u3002 \u9996\u5148\uff0c\u5047\u8bbe\u4f60\u60f3\u8981\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u6253\u5370\u52a8\u7269\u7684\u540d\u5b57\u548c\u58f0\u97f3\u3002\u5982\u679c\u4e0d\u4f7f\u7528\u6307\u5411\u57fa\u7c7b\u7684\u6307\u9488\uff0c\u4f60\u5c31\u5fc5\u987b\u4f7f\u7528\u91cd\u8f7d\u51fd\u6570\u6765\u7f16\u5199\u5b83\uff0c\u50cf\u8fd9\u6837: 1 2 3 4 5 6 7 8 9 10 //\u51fd\u6570\u91cd\u8f7d\uff08\u7b7e\u540d\u4e0d\u540c\uff09 void report ( const Cat & cat ) { std :: cout << cat . getName () << \" says \" << cat . speak () << '\\n' ; } void report ( const Dog & dog ) { std :: cout << dog . getName () << \" says \" << dog . speak () << '\\n' ; } \u597d\u50cf\u6ca1\u5565\u95ee\u9898\uff0c\u4f46\u60f3\u60f3\u5982\u679c\u6211\u4eec\u4e0d\u6b62\u6709\u4e24\u79cd\u52a8\u7269\u800c\u662f\u670930\u79cd\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002\u4f60\u5fc5\u987b\u7f16\u519930\u4e2a\u51e0\u4e4e\u76f8\u540c\u7684\u51fd\u6570\uff01\u53e6\u5916\uff0c\u5982\u679c\u4f60\u6dfb\u52a0\u4e86\u4e00\u79cd\u65b0\u7684\u52a8\u7269\u7c7b\u578b\uff0c\u4f60\u4e5f\u5fc5\u987b\u4e3a\u5b83\u5199\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u3002\u8003\u8651\u5230\u8fd9\u4e9b\u51fd\u6570\u7684\u552f\u4e00\u533a\u522b\u5c31\u662f\u53c2\u6570\u4e0d\u540c\uff0c\u8fd9\u662f\u65e0\u7591\u662f\u4e00\u79cd\u5de8\u5927\u7684\u6d6a\u8d39\u3002 \u4e0d\u8fc7\uff0c\u56e0\u4e3a Cat \u548c Dog \u90fd\u6d3e\u751f\u81ea Animal \uff0c Cat \u548c Dog \u4e2d\u4e00\u5b9a\u5177\u6709\u5c5e\u4e8e Animal \u7684\u90e8\u5206\u3002\u56e0\u6b64\uff0c\u5982\u679c\u80fd\u50cf\u4e0b\u9762\u8fd9\u4e48\u505a\u5c31\u597d\u4e86\uff1a 1 2 3 4 void report ( const Animal & rAnimal ) { std :: cout << rAnimal . getName () << \" says \" << rAnimal . speak () << '\\n' ; } \u8fd9\u5c06\u5141\u8bb8\u6211\u4eec\u4f20\u5165\u4ece Animal \u6d3e\u751f\u7684\u4efb\u4f55\u7c7b\uff0c\u751a\u81f3\u662f\u5728\u7f16\u5199\u51fd\u6570\u4e4b\u540e\u521b\u5efa\u7684\u7c7b\uff01\u4e0d\u9700\u8981\u4e3a\u6bcf\u4e2a\u6d3e\u751f\u7c7b\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u800c\u662f\u4e0e\u6240\u6709\u6d3e\u751f\u81ea Animal \u7684\u7c7b\u53ef\u4ee5\u5171\u7528\u7684\u4e00\u4e2a\u51fd\u6570\uff01 \u5f53\u7136\uff0c\u8fd9\u4e48\u505a\u6700\u5927\u7684\u95ee\u9898\u5728\u4e8e rAnimal \u662f Animal \u7684\u5f15\u7528\uff0c\u6240\u4ee5 rAnimal.speak() \u4f1a\u8c03\u7528 Animal::speak() \u800c\u4e0d\u662f\u6d3e\u751f\u7c7b\u4e2d\u7684 speak() \u3002 \u5176\u6b21\uff0c\u5047\u8bbe\u4f60\u67093\u53ea\u732b\u548c3\u53ea\u72d7\uff0c\u4f60\u60f3\u628a\u5b83\u4eec\u653e\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\u4ee5\u4fbf\u4e8e\u8bbf\u95ee\u3002\u56e0\u4e3a\u6570\u7ec4\u53ea\u80fd\u4fdd\u5b58\u4e00\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u6ca1\u6709\u6307\u5411\u57fa\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u4f60\u5fc5\u987b\u4e3a\u6bcf\u4e2a\u6d3e\u751f\u7c7b\u578b\u521b\u5efa\u4e0d\u540c\u7684\u6570\u7ec4\uff0c\u50cf\u8fd9\u6837: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include // Cat and Dog from the example above int main () { const auto & cats { std :: to_array < Cat > ({{ \"Fred\" }, { \"Misty\" }, { \"Zeke\" }}) }; const auto & dogs { std :: to_array < Dog > ({{ \"Garbo\" }, { \"Pooky\" }, { \"Truffle\" }}) }; // Before C++20 // const std::array cats{{ { \"Fred\" }, { \"Misty\" }, { \"Zeke\" } }}; // const std::array dogs{{ { \"Garbo\" }, { \"Pooky\" }, { \"Truffle\" } }}; for ( const auto & cat : cats ) { std :: cout << cat . getName () << \" says \" << cat . speak () << '\\n' ; } for ( const auto & dog : dogs ) { std :: cout << dog . getName () << \" says \" << dog . speak () << '\\n' ; } return 0 ; } \u5982\u679c\u670930\u79cd\u4e0d\u540c\u7684\u52a8\u7269\u5462\uff1f\u5c82\u4e0d\u662f\u8981\u5b9a\u4e4930\u4e2a\u6570\u7ec4\uff0c\u6bcf\u4e2a\u52a8\u7269\u90fd\u9700\u8981\u4e00\u4e2a\u6570\u7ec4\uff01 \u4e0d\u8fc7\uff0c\u56e0\u4e3a Cat \u548c Dog \u90fd\u6d3e\u751f\u81ea\u4e8e Animal \uff0c\u6240\u4ee5\u53ef\u4ee5\u8fd9\u6837\u505a: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include #include // Cat and Dog from the example above int main () { const Cat fred { \"Fred\" }; const Cat misty { \"Misty\" }; const Cat zeke { \"Zeke\" }; const Dog garbo { \"Garbo\" }; const Dog pooky { \"Pooky\" }; const Dog truffle { \"Truffle\" }; // Set up an array of pointers to animals, and set those pointers to our Cat and Dog objects // Note: to_array requires C++20 support (and at the time of writing, Visual Studio 2022 still doesn't support it correctly) const auto animals { std :: to_array < const Animal *> ({ & fred , & garbo , & misty , & pooky , & truffle , & zeke }) }; // Before C++20, with the array size being explicitly specified // const std::array animals{ &fred, &garbo, &misty, &pooky, &truffle, &zeke }; for ( const auto animal : animals ) { std :: cout << animal -> getName () << \" says \" << animal -> speak () << '\\n' ; } return 0 ; } \u5c3d\u7ba1\u4e0a\u8ff0\u4ee3\u7801\u53ef\u4ee5\u7f16\u8bd1\u548c\u6267\u884c\uff0c\u4f46\u53ef\u60dc\u7684\u662f\uff0c\u7531\u4e8e\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f Animal \u7c7b\u578b\u7684\u6307\u9488\uff0c\u90a3\u5c31\u610f\u5473\u7740 animal->speak() \u8c03\u7528\u7684\u4f1a\u662f Animal::speak() \u800c\u4e0d\u662f\u6d3e\u751f\u7c7b\u5404\u81ea\u7684 speak() \u51fd\u6570\u3002\u8f93\u51fa\u7ed3\u679c\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 Fred says ??? Garbo says ??? Misty says ??? Pooky says ??? Truffle says ??? Zeke says ??? \u867d\u7136\u8fd9\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8282\u7701\u5927\u91cf\u7684\u65f6\u95f4\u548c\u7cbe\u529b\uff0c\u4f46\u5b83\u4eec\u90fd\u6709\u76f8\u540c\u7684\u95ee\u9898\u3002\u57fa\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\u8c03\u7528\u51fd\u6570\u7684\u57fa\u7c7b\u7248\u672c\u800c\u4e0d\u662f\u6d3e\u751f\u7248\u672c\u3002\u8981\u662f\u6709\u529e\u6cd5\u8ba9\u8fd9\u4e9b\u57fa\u6307\u9488\u8c03\u7528\u51fd\u6570\u7684\u6d3e\u751f\u7248\u672c\u800c\u4e0d\u662f\u57fa\u7248\u672c\u5c31\u597d\u4e86\u2026 \u4f60\u731c\uff0c\u865a\u51fd\u6570\u662f\u505a\u4ec0\u4e48\u7528\u7684\uff1f\uff08\u4e0d\u4f1a\u90a3\u4e48\u5de7\u5427\uff09\u3002","title":"\u4f7f\u7528\u6307\u5411\u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528"},{"location":"18-10-dynamic-casting/","tags":["dynamic casting"],"text":"Key Takeaway \u65e9\u5728 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast \u4e2d\u6211\u4eec\u5c31\u8ba8\u8bba\u8fc7\u7c7b\u578b\u8f6c\u6362\u7684\u8bdd\u9898\uff0c\u5f53\u65f6\u6211\u4eec\u4f7f\u7528 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u2014\u2014 static_cast \u5c06\u53d8\u91cf\u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b\u3002 \u672c\u8282\u8bfe\u6211\u4eec\u4f1a\u8ba8\u8bba\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\u7684\u8f6c\u6362\uff1a \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u2014\u2014 dynamic_cast . dynamic_cast \u7684\u5fc5\u8981\u6027 \u5728\u5904\u7406 \u591a\u6001 \u65f6\u7ecf\u5e38\u4f1a\u9047\u5230\u8fd9\u6837\u7684\u60c5\u51b5\uff1a\u4f60\u6709\u4e00\u4e2a\u6307\u5411\u57fa\u7c7b\u7684\u6307\u9488\uff0c\u4f46\u9700\u8981\u8bbf\u95ee\u5b58\u5728\u4e8e\u6d3e\u751f\u7c7b\u4e2d\u7684\u4e00\u4e9b\u4fe1\u606f\u3002 \u8003\u8651\u4ee5\u4e0b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 #include #include class Base { protected : int m_value {}; public : Base ( int value ) : m_value { value } { } virtual ~ Base () = default ; }; class Derived : public Base { protected : std :: string m_name {}; public : Derived ( int value , const std :: string & name ) : Base { value }, m_name { name } { } const std :: string & getName () const { return m_name ; } }; Base * getObject ( bool returnDerived ) { if ( returnDerived ) return new Derived { 1 , \"Apple\" }; else return new Base { 2 }; } int main () { Base * b { getObject ( true ) }; // \u5728\u8fd9\u91cc\u5982\u4f55\u6253\u5370\u6d3e\u751f\u7c7b\u5bf9\u8c61\u7684\u540d\u5b57\u5462\uff1f\u6b64\u65f6\u53ea\u6709\u4e00\u4e2a\u57fa\u7c7b\u7684\u6307\u9488 delete b ; return 0 ; } \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u51fd\u6570 getObject() \u603b\u662f\u8fd4\u56de\u4e00\u4e2a Base \u6307\u9488\uff0c\u4f46\u8be5\u6307\u9488\u53ef\u4ee5\u6307\u5411 Base \u5bf9\u8c61\u6216 Derived \u5bf9\u8c61\u3002\u5728\u6307\u9488\u6307\u5411\u6d3e\u751f\u5bf9\u8c61\u7684\u60c5\u51b5\u4e0b\uff0c\u5982\u4f55\u8c03\u7528 Derived::getName() ? \u4e00\u79cd\u65b9\u6cd5\u662f\u5411 Base \u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3a getName() \u7684\u865a\u51fd\u6570(\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u7528 Base \u6307\u9488/\u5f15\u7528\u8c03\u7528\u5b83\uff0c\u5e76\u52a8\u6001\u89e3\u6790\u4e3a Derived::getName() )\u3002\u4f46\u662f\u5982\u679c\u4f60\u7528\u4e00\u4e2a\u6307\u5411 Base \u5bf9\u8c61\u7684 Base \u6307\u9488/\u5f15\u7528\u8c03\u7528\u5b83\uff0c\u8fd9\u4e2a\u51fd\u6570\u53c8\u5e94\u8be5\u8fd4\u56de\u4ec0\u4e48\u5462\uff1f\u8fd4\u56de\u4ec0\u4e48\u503c\u90fd\u662f\u6ca1\u6709\u5b9e\u9645\u610f\u4e49\u7684\uff08\u4e5f\u4e0d\u80fd\u4e3a\u7eaf\u865a\u56e0\u4e3a\u57fa\u7c7b\u8981\u80fd\u591f\u88ab\u5b9e\u4f8b\u5316\uff09\u3002\u8fd9\u4e2a\u51fd\u6570\u5bf9\u57fa\u7c7b\u662f\u6ca1\u6709\u610f\u4e49\u7684\uff0c\u53ea\u6709\u6d3e\u751f\u7c7b\u9700\u8981\u8003\u8651\u5b9e\u73b0\u8be5\u51fd\u6570\uff0c\u90a3\u4e48\u6211\u4eec\u4e3a\u4ec0\u4e48\u8981\u7528\u8fd9\u4e2a\u51fd\u6570\u6765\u201c\u6c61\u67d3\u201d\u57fa\u7c7b\u5462\uff1f \u6211\u4eec\u77e5\u9053\uff0cC++ \u5141\u8bb8\u4f60\u5c06 Derived \u6307\u9488\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a Base \u6307\u9488(\u5b9e\u9645\u4e0a\uff0c getObject() \u6b63\u662f\u8fd9\u6837\u505a\u7684)\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u6709\u65f6\u88ab\u79f0\u4e3a \u5411\u4e0a\u8f6c\u6362(upcasting|) \u3002\u90a3\u4e48\uff0c\u662f\u5426\u6709\u4e00\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06 Base \u6307\u9488\u8f6c\u6362\u56de Derived \u7c7b\u6307\u9488\u5462\uff1f\u8fd9\u6837\u7684\u8bdd\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8be5\u6307\u9488\u8c03\u7528 Derived::getName() \uff0c\u800c\u4e0d\u5fc5\u4f9d\u8d56\u865a\u51fd\u6570\u89e3\u6790\u3002 dynamic_cast C++ \u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a dynamic_cast \u7684\u5f3a\u5236\u8f6c\u6362\u64cd\u4f5c\u7b26\uff0c\u53ef\u7528\u4e8e\u6b64\u76ee\u7684\u3002\u5c3d\u7ba1\u52a8\u6001\u5f3a\u5236\u8f6c\u6362\u6709\u4e00\u4e9b\u4e0d\u540c\u7684\u529f\u80fd\uff0c\u4f46\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u52a8\u6001\u5f3a\u5236\u8f6c\u6362\u6700\u5e38\u89c1\u7684\u7528\u9014\u662f\u5c06\u57fa\u7c7b\u6307\u9488\u8f6c\u6362\u4e3a\u6d3e\u751f\u7c7b\u6307\u9488\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a \u5411\u4e0b\u8f6c\u6362\uff08downcasting\uff09 \u3002 \u4f7f\u7528 dynamic_cast \u548c static_cast \u7c7b\u4f3c\u3002\u57fa\u4e8e\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 dynamic_cast \u5c06 Base \u6307\u9488\u8f6c\u6362\u4e3a Derived \u6307\u9488\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int main () { Base * b { getObject ( true ) }; Derived * d { dynamic_cast < Derived *> ( b ) }; // \u4f7f\u7528\u52a8\u6001\u8f6c\u6362\u5c06 Base \u6307\u9488\u8f6c\u6362\u4e3a Derived \u6307\u9488 std :: cout << \"The name of the Derived is: \" << d -> getName () << '\\n' ; delete b ; return 0 ; } \u6253\u5370\uff1a 1 The name of the Derived is: Apple dynamic_cast \u5931\u8d25 \u4e0a\u9762\u7684\u4f8b\u5b50\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\uff0c\u56e0\u4e3a b \u5b9e\u9645\u4e0a\u6307\u5411\u4e00\u4e2a Derived \u5bf9\u8c61\uff0c\u56e0\u6b64\u5c06 b \u8f6c\u6362\u4e3a Derived \u6307\u9488\u662f\u6210\u529f\u7684\u3002 \u7136\u800c\uff0c\u6211\u4eec\u505a\u4e86\u4e00\u4e2a\u76f8\u5f53\u5371\u9669\u7684\u5047\u8bbe\uff1a b \u6307\u5411\u4e00\u4e2a\u6d3e\u751f\u5bf9\u8c61\u3002\u5982\u679c b \u4e0d\u6307\u5411\u6d3e\u751f\u5bf9\u8c61\u5462\uff1f\u8fd9\u5f88\u5bb9\u6613\u901a\u8fc7\u5c06 getObject() \u7684\u53c2\u6570\u4ece true \u66f4\u6539\u4e3a false \u6765\u6d4b\u8bd5\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c getObject() \u5c06\u8fd4\u56de\u4e00\u4e2a Base \u5bf9\u8c61\u7684 Base \u6307\u9488\u3002\u5f53\u6211\u4eec\u5c1d\u8bd5\u5c06 dynamic_cast \u8f6c\u6362\u4e3a\u6d3e\u751f\u7c7b\u578b\u65f6\uff0c\u5b83\u4f1a\u5931\u8d25\uff0c\u56e0\u4e3a\u65e0\u6cd5\u8fdb\u884c\u8f6c\u6362\u3002 \u5982\u679c dynamic_cast \u5931\u8d25\uff0c\u5219\u8f6c\u6362\u7ed3\u679c\u4f1a\u662f\u4e00\u4e2a\u7a7a\u6307\u9488\u3002 \u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u68c0\u67e5\u7a7a\u6307\u9488\u7684\u7ed3\u679c\uff0c\u6240\u4ee5\u6211\u4eec\u8bbf\u95ee d->getName() \uff0c\u5b83\u5c06\u5c1d\u8bd5\u89e3\u5f15\u7528\u7a7a\u6307\u9488\uff0c\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a (\u53ef\u80fd\u662f\u5d29\u6e83)\u3002 \u4e3a\u4e86\u4f7f\u8fd9\u4e2a\u7a0b\u5e8f\u5b89\u5168\uff0c\u9700\u8981\u786e\u4fdd dynamic_cast \u7684\u7ed3\u679c\u5b9e\u9645\u4e0a\u662f\u6210\u529f\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 int main () { Base * b { getObject ( true ) }; Derived * d { dynamic_cast < Derived *> ( b ) }; // use dynamic cast to convert Base pointer into Derived pointer if ( d ) // \u786e\u4fdd d \u662f\u975e\u7a7a std :: cout << \"The name of the Derived is: \" << d -> getName () << '\\n' ; delete b ; return 0 ; } \u6cd5\u5219 \u603b\u662f\u8981\u901a\u8fc7\u68c0\u67e5\u8fd4\u56de\u503c\u662f\u5426\u4e3a\u7a7a\u6307\u9488\u6765\u786e\u4fdd\u52a8\u6001\u8f6c\u6362\u6210\u529f\u3002 \u6ce8\u610f\uff0c\u7531\u4e8e dynamic_cast \u5728\u8fd0\u884c\u65f6\u8fdb\u884c\u4e00\u4e9b\u4e00\u81f4\u6027\u68c0\u67e5(\u4ee5\u786e\u4fdd\u53ef\u4ee5\u8fdb\u884c\u8f6c\u6362)\uff0c\u56e0\u6b64\u4f7f\u7528 dynamic_cast \u786e\u5b9e\u4f1a\u5bfc\u81f4\u6027\u80fd\u635f\u5931\u3002 \u6b64\u5916\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u60c5\u5f62\uff0c\u6b64\u65f6\u4f7f\u7528 dynamic_cast \u8fdb\u884c \u5411\u4e0b\u8f6c\u6362 \u662f\u4e0d\u80fd\u6210\u529f\u7684\uff1a \u53d7\u4fdd\u62a4\u7ee7\u627f \u548c \u79c1\u6709\u7ee7\u627f \u7684\u7c7b\uff1b \u5bf9\u4e8e\u6ca1\u6709\u58f0\u660e\u6216\u7ee7\u627f\u4efb\u4f55\u865a\u51fd\u6570(\u56e0\u6b64\u6ca1\u6709\u865a\u8868)\u7684\u7c7b\uff1b \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6d89\u53ca \u865a\u57fa\u7c7b \u7684\u60c5\u51b5 (\u53c2\u8003 \u8fd9\u91cc \u67e5\u770b\u4e00\u4e9b\u60c5\u51b5\u7684\u793a\u4f8b\uff0c\u4ee5\u53ca\u5982\u4f55\u89e3\u51b3\u5b83\u4eec)\u3002 \u4f7f\u7528 static_cast \u8fdb\u884c\u5411\u4e0b\u8f6c\u6362 \u4e8b\u5b9e\u8bc1\u660e\uff0c\u5411\u4e0b\u8f6c\u6362\u4e5f\u53ef\u4ee5\u4f7f\u7528 static_cast \u5b8c\u6210\u3002 \u4e3b\u8981\u7684\u533a\u522b\u662f static_cast \u4e0d\u8fdb\u884c\u8fd0\u884c\u65f6\u7c7b\u578b\u68c0\u67e5 \uff0c\u4ee5\u786e\u4fdd\u60a8\u6240\u505a\u7684\u5de5\u4f5c\u662f\u6709\u610f\u4e49\u7684\u3002\u8fd9\u4f7f\u5f97\u4f7f\u7528 static_cast \u66f4\u5feb\uff0c\u4f46\u66f4\u5371\u9669\u3002\u5982\u679c\u5c06 Base* \u8f6c\u6362\u4e3a Derived* \uff0c\u5373\u4f7f\u57fa\u7c7b\u6307\u9488\u6ca1\u6709\u6307\u5411\u6d3e\u751f\u7c7b\u5bf9\u8c61\uff0c\u5b83\u4e5f\u4f1a\u201c\u6210\u529f\u201d\u3002\u5f53\u60a8\u5c1d\u8bd5\u8bbf\u95ee\u7ed3\u679c\u6d3e\u751f\u6307\u9488(\u5b9e\u9645\u4e0a\u6307\u5411 Base \u5bf9\u8c61)\u65f6\uff0c\u8fd9\u5c06\u5bfc\u81f4\u672a\u5b9a\u4e49\u7684\u884c\u4e3a\u3002 \u5982\u679c\u4f60\u53ef\u4ee5\u4fdd\u8bc1\u5411\u4e0b\u5f3a\u5236\u8f6c\u6362\u7684\u6307\u9488\u5c06\u6210\u529f\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528 static_cast \u3002\u8fd9\u91cc\u6709\u4e00\u4e2a(\u4e0d\u662f\u5f88\u597d\u7684)\u65b9\u6cd5\uff0c\u901a\u8fc7\u865a\u51fd\u6570\u5224\u65ad\u6240\u6307\u7684\u5bf9\u8c61\u662f\u4e0d\u662f\u6b63\u786e\u7684\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 #include #include // Class identifier enum class ClassID { base , derived // Others can be added here later }; class Base { protected : int m_value {}; public : Base ( int value ) : m_value { value } { } virtual ~ Base () = default ; virtual ClassID getClassID () const { return ClassID :: base ; } }; class Derived : public Base { protected : std :: string m_name {}; public : Derived ( int value , const std :: string & name ) : Base { value }, m_name { name } { } const std :: string & getName () const { return m_name ; } virtual ClassID getClassID () const { return ClassID :: derived ; } }; Base * getObject ( bool bReturnDerived ) { if ( bReturnDerived ) return new Derived { 1 , \"Apple\" }; else return new Base { 2 }; } int main () { Base * b { getObject ( true ) }; if ( b -> getClassID () == ClassID :: derived ) { // We already proved b is pointing to a Derived object, so this should always succeed Derived * d { static_cast < Derived *> ( b ) }; std :: cout << \"The name of the Derived is: \" << d -> getName () << '\\n' ; } delete b ; return 0 ; } \u867d\u7136\u7684\u786e\u6709\u7528\u4f46\u662f\u5b9e\u73b0\u8d77\u6765\u8fd8\u662f\u6709\u70b9\u8d39\u4e8b\u7684(\u8fd8\u8981\u4ed8\u51fa\u8c03\u7528\u865a\u51fd\u6570\u548c\u5904\u7406\u7ed3\u679c\u7684\u4ee3\u4ef7)\uff0c\u90a3\u4e48\u8fd8\u4e0d\u5982\u4f7f\u7528\u76f4\u63a5\u4f7f\u7528 dynamic_cast \u3002 dynamic_cast \u548c\u5f15\u7528 \u5c3d\u7ba1\u4e0a\u9762\u7684\u6240\u6709\u793a\u4f8b\u90fd\u4ee5\u6307\u9488\u7684\u52a8\u6001\u5f3a\u5236\u8f6c\u6362(\u8fd9\u66f4\u5e38\u89c1)\u4e3a\u4f8b\uff0c\u4f46 dynamic_cast \u4e5f\u53ef\u4ee5\u7528\u4e8e\u5f15\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include #include class Base { protected : int m_value ; public : Base ( int value ) : m_value { value } { } virtual ~ Base () = default ; }; class Derived : public Base { protected : std :: string m_name ; public : Derived ( int value , const std :: string & name ) : Base { value }, m_name { name } { } const std :: string & getName () const { return m_name ; } }; int main () { Derived apple { 1 , \"Apple\" }; // create an apple Base & b { apple }; // set base reference to object Derived & d { dynamic_cast < Derived &> ( b ) }; // \u5bf9\u5f15\u7528\u8fdb\u884c\u52a8\u6001\u8f6c\u6362 std :: cout << \"The name of the Derived is: \" << d . getName () << '\\n' ; // \u4f7f\u7528 d \u8bbf\u95ee Derived::getName through d return 0 ; } \u56e0\u4e3aC++\u4e2d\u6ca1\u6709\u201c\u7a7a\u5f15\u7528\u201d\uff0c\u6240\u6709 dynamic_cast \u5728\u5931\u8d25\u65f6\u4e0d\u80fd\u8fd4\u56de\u7a7a\u5f15\u7528\u3002\u6240\u4ee5\uff0c\u5982\u679c\u5f15\u7528\u7684 dynamic_cast \u5931\u8d25\uff0c\u5219\u4f1a\u629b\u51fa std::bad_cast \u7c7b\u578b\u7684\u5f02\u5e38\u3002\u6211\u4eec\u5c06\u5728\u672c\u6559\u7a0b\u540e\u9762\u8ba8\u8bba\u5f02\u5e38\u3002 dynamic_cast vs static_cast \u65b0\u624b\u7a0b\u5e8f\u5458\u6709\u65f6\u4f1a\u5bf9\u4f55\u65f6\u4f7f\u7528 static_cast \u548c dynamic_cast \u611f\u5230\u56f0\u60d1\u3002\u7b54\u6848\u5f88\u7b80\u5355\uff1a\u9664\u975e\u662f\u5411\u4e0b\u7c7b\u578b\u8f6c\u6362\uff08\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cdynamic_cast\u901a\u5e38\u662f\u66f4\u597d\u7684\u9009\u62e9\uff09\uff0c\u5426\u5219\u4e00\u5f8b\u4f7f\u7528 static_cast\u3002\u4f46\u662f\uff0c\u4f60\u8fd8\u5e94\u8be5\u8003\u8651\u5b8c\u5168\u907f\u514d\u5f3a\u5236\u8f6c\u6362\uff0c\u800c\u53ea\u4f7f\u7528\u865a\u51fd\u6570\u3002 \u5411\u4e0b\u8f6c\u6362 vs \u865a\u51fd\u6570 \u6709\u4e9b\u5f00\u53d1\u4eba\u5458\u8ba4\u4e3a dynamic_cast \u662f\u90aa\u6076\u7684\uff0c\u662f\u7c7b\u6ca1\u6709\u88ab\u8bbe\u8ba1\u597d\u7684\u7279\u5f81\u3002\u76f8\u53cd\uff0c\u8fd9\u4e9b\u7a0b\u5e8f\u5458\u8ba4\u4e3a\u5e94\u8be5\u4f7f\u7528\u865a\u51fd\u6570\u3002 \u4e00\u822c\u6765\u8bf4\uff0c \u4f7f\u7528\u865a\u51fd\u6570\u5e94\u8be5\u4f18\u5148\u4e8e\u5411\u4e0b\u8f6c\u6362 \u3002\u7136\u800c\uff0c\u6709\u65f6\u5019\u4f7f\u7528\u5411\u4e0b\u8f6c\u6362\u4f1a\u662f\u66f4\u597d\u7684\u9009\u62e9\uff1a \u5f53\u4f60\u4e0d\u80fd\u4fee\u6539\u57fa\u7c7b\u6765\u6dfb\u52a0\u865a\u51fd\u6570\u65f6(\u4f8b\u5982\uff0c\u56e0\u4e3a\u57fa\u7c7b\u662f\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206) \u5f53\u4f60\u9700\u8981\u8bbf\u95ee\u7279\u5b9a\u4e8e\u6d3e\u751f\u7c7b\u7684\u4e1c\u897f\u65f6(\u4f8b\u5982\uff0c\u4e00\u4e2a\u53ea\u5b58\u5728\u4e8e\u6d3e\u751f\u7c7b\u4e2d\u7684\u8bbf\u95ee\u51fd\u6570) \u5728\u57fa\u7c7b\u4e2d\u6dfb\u52a0\u865a\u51fd\u6570\u662f\u6ca1\u6709\u610f\u4e49\u7684(\u4f8b\u5982\uff0c\u57fa\u7c7b\u6ca1\u6709\u9002\u5f53\u7684\u8fd4\u56de\u503c)\u3002\u5982\u679c\u4e0d\u9700\u8981\u5b9e\u4f8b\u5316\u57fa\u7c7b\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u7eaf\u865a\u51fd\u6570\u3002 \u5bf9 dynamic_cast \u548c RTTI \u7684\u4e00\u4e9b\u8b66\u793a \u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f(RTTI) \u662fC++\u7684\u4e00\u4e2a\u7279\u6027\uff0c\u5b83\u5728\u8fd0\u884c\u65f6\u4f1a\u66b4\u9732\u5bf9\u8c61\u6570\u636e\u7c7b\u578b\u7684\u4fe1\u606f\u3002 dynamic_cast \u5229\u7528\u4e86\u7279\u6027\u3002\u56e0\u4e3aRTTI\u6709\u76f8\u5f53\u5927\u7684\u7a7a\u95f4\u6027\u80fd\u5f00\u9500\uff0c\u4e00\u4e9b\u7f16\u8bd1\u5668\u5141\u8bb8\u4f60\u5173\u95edRTTI\u4f5c\u4e3a\u4e00\u79cd\u4f18\u5316\u65b9\u5f0f\u3002\u4e0d\u7528\u8bf4\uff0c\u5982\u679c\u8fd9\u6837\u505a\uff0c dynamic_cast \u5c06\u4e0d\u80fd\u6b63\u786e\u5de5\u4f5c\u3002","title":"18.10 - \u52a8\u6001\u7c7b\u578b\u8f6c\u6362"},{"location":"18-10-dynamic-casting/#dynamic_cast","text":"\u5728\u5904\u7406 \u591a\u6001 \u65f6\u7ecf\u5e38\u4f1a\u9047\u5230\u8fd9\u6837\u7684\u60c5\u51b5\uff1a\u4f60\u6709\u4e00\u4e2a\u6307\u5411\u57fa\u7c7b\u7684\u6307\u9488\uff0c\u4f46\u9700\u8981\u8bbf\u95ee\u5b58\u5728\u4e8e\u6d3e\u751f\u7c7b\u4e2d\u7684\u4e00\u4e9b\u4fe1\u606f\u3002 \u8003\u8651\u4ee5\u4e0b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 #include #include class Base { protected : int m_value {}; public : Base ( int value ) : m_value { value } { } virtual ~ Base () = default ; }; class Derived : public Base { protected : std :: string m_name {}; public : Derived ( int value , const std :: string & name ) : Base { value }, m_name { name } { } const std :: string & getName () const { return m_name ; } }; Base * getObject ( bool returnDerived ) { if ( returnDerived ) return new Derived { 1 , \"Apple\" }; else return new Base { 2 }; } int main () { Base * b { getObject ( true ) }; // \u5728\u8fd9\u91cc\u5982\u4f55\u6253\u5370\u6d3e\u751f\u7c7b\u5bf9\u8c61\u7684\u540d\u5b57\u5462\uff1f\u6b64\u65f6\u53ea\u6709\u4e00\u4e2a\u57fa\u7c7b\u7684\u6307\u9488 delete b ; return 0 ; } \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u51fd\u6570 getObject() \u603b\u662f\u8fd4\u56de\u4e00\u4e2a Base \u6307\u9488\uff0c\u4f46\u8be5\u6307\u9488\u53ef\u4ee5\u6307\u5411 Base \u5bf9\u8c61\u6216 Derived \u5bf9\u8c61\u3002\u5728\u6307\u9488\u6307\u5411\u6d3e\u751f\u5bf9\u8c61\u7684\u60c5\u51b5\u4e0b\uff0c\u5982\u4f55\u8c03\u7528 Derived::getName() ? \u4e00\u79cd\u65b9\u6cd5\u662f\u5411 Base \u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3a getName() \u7684\u865a\u51fd\u6570(\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u7528 Base \u6307\u9488/\u5f15\u7528\u8c03\u7528\u5b83\uff0c\u5e76\u52a8\u6001\u89e3\u6790\u4e3a Derived::getName() )\u3002\u4f46\u662f\u5982\u679c\u4f60\u7528\u4e00\u4e2a\u6307\u5411 Base \u5bf9\u8c61\u7684 Base \u6307\u9488/\u5f15\u7528\u8c03\u7528\u5b83\uff0c\u8fd9\u4e2a\u51fd\u6570\u53c8\u5e94\u8be5\u8fd4\u56de\u4ec0\u4e48\u5462\uff1f\u8fd4\u56de\u4ec0\u4e48\u503c\u90fd\u662f\u6ca1\u6709\u5b9e\u9645\u610f\u4e49\u7684\uff08\u4e5f\u4e0d\u80fd\u4e3a\u7eaf\u865a\u56e0\u4e3a\u57fa\u7c7b\u8981\u80fd\u591f\u88ab\u5b9e\u4f8b\u5316\uff09\u3002\u8fd9\u4e2a\u51fd\u6570\u5bf9\u57fa\u7c7b\u662f\u6ca1\u6709\u610f\u4e49\u7684\uff0c\u53ea\u6709\u6d3e\u751f\u7c7b\u9700\u8981\u8003\u8651\u5b9e\u73b0\u8be5\u51fd\u6570\uff0c\u90a3\u4e48\u6211\u4eec\u4e3a\u4ec0\u4e48\u8981\u7528\u8fd9\u4e2a\u51fd\u6570\u6765\u201c\u6c61\u67d3\u201d\u57fa\u7c7b\u5462\uff1f \u6211\u4eec\u77e5\u9053\uff0cC++ \u5141\u8bb8\u4f60\u5c06 Derived \u6307\u9488\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a Base \u6307\u9488(\u5b9e\u9645\u4e0a\uff0c getObject() \u6b63\u662f\u8fd9\u6837\u505a\u7684)\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u6709\u65f6\u88ab\u79f0\u4e3a \u5411\u4e0a\u8f6c\u6362(upcasting|) \u3002\u90a3\u4e48\uff0c\u662f\u5426\u6709\u4e00\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06 Base \u6307\u9488\u8f6c\u6362\u56de Derived \u7c7b\u6307\u9488\u5462\uff1f\u8fd9\u6837\u7684\u8bdd\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8be5\u6307\u9488\u8c03\u7528 Derived::getName() \uff0c\u800c\u4e0d\u5fc5\u4f9d\u8d56\u865a\u51fd\u6570\u89e3\u6790\u3002","title":"dynamic_cast \u7684\u5fc5\u8981\u6027"},{"location":"18-10-dynamic-casting/#dynamic_cast_1","text":"C++ \u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a dynamic_cast \u7684\u5f3a\u5236\u8f6c\u6362\u64cd\u4f5c\u7b26\uff0c\u53ef\u7528\u4e8e\u6b64\u76ee\u7684\u3002\u5c3d\u7ba1\u52a8\u6001\u5f3a\u5236\u8f6c\u6362\u6709\u4e00\u4e9b\u4e0d\u540c\u7684\u529f\u80fd\uff0c\u4f46\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u52a8\u6001\u5f3a\u5236\u8f6c\u6362\u6700\u5e38\u89c1\u7684\u7528\u9014\u662f\u5c06\u57fa\u7c7b\u6307\u9488\u8f6c\u6362\u4e3a\u6d3e\u751f\u7c7b\u6307\u9488\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a \u5411\u4e0b\u8f6c\u6362\uff08downcasting\uff09 \u3002 \u4f7f\u7528 dynamic_cast \u548c static_cast \u7c7b\u4f3c\u3002\u57fa\u4e8e\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 dynamic_cast \u5c06 Base \u6307\u9488\u8f6c\u6362\u4e3a Derived \u6307\u9488\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int main () { Base * b { getObject ( true ) }; Derived * d { dynamic_cast < Derived *> ( b ) }; // \u4f7f\u7528\u52a8\u6001\u8f6c\u6362\u5c06 Base \u6307\u9488\u8f6c\u6362\u4e3a Derived \u6307\u9488 std :: cout << \"The name of the Derived is: \" << d -> getName () << '\\n' ; delete b ; return 0 ; } \u6253\u5370\uff1a 1 The name of the Derived is: Apple","title":"dynamic_cast"},{"location":"18-10-dynamic-casting/#dynamic_cast_2","text":"\u4e0a\u9762\u7684\u4f8b\u5b50\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\uff0c\u56e0\u4e3a b \u5b9e\u9645\u4e0a\u6307\u5411\u4e00\u4e2a Derived \u5bf9\u8c61\uff0c\u56e0\u6b64\u5c06 b \u8f6c\u6362\u4e3a Derived \u6307\u9488\u662f\u6210\u529f\u7684\u3002 \u7136\u800c\uff0c\u6211\u4eec\u505a\u4e86\u4e00\u4e2a\u76f8\u5f53\u5371\u9669\u7684\u5047\u8bbe\uff1a b \u6307\u5411\u4e00\u4e2a\u6d3e\u751f\u5bf9\u8c61\u3002\u5982\u679c b \u4e0d\u6307\u5411\u6d3e\u751f\u5bf9\u8c61\u5462\uff1f\u8fd9\u5f88\u5bb9\u6613\u901a\u8fc7\u5c06 getObject() \u7684\u53c2\u6570\u4ece true \u66f4\u6539\u4e3a false \u6765\u6d4b\u8bd5\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c getObject() \u5c06\u8fd4\u56de\u4e00\u4e2a Base \u5bf9\u8c61\u7684 Base \u6307\u9488\u3002\u5f53\u6211\u4eec\u5c1d\u8bd5\u5c06 dynamic_cast \u8f6c\u6362\u4e3a\u6d3e\u751f\u7c7b\u578b\u65f6\uff0c\u5b83\u4f1a\u5931\u8d25\uff0c\u56e0\u4e3a\u65e0\u6cd5\u8fdb\u884c\u8f6c\u6362\u3002 \u5982\u679c dynamic_cast \u5931\u8d25\uff0c\u5219\u8f6c\u6362\u7ed3\u679c\u4f1a\u662f\u4e00\u4e2a\u7a7a\u6307\u9488\u3002 \u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u68c0\u67e5\u7a7a\u6307\u9488\u7684\u7ed3\u679c\uff0c\u6240\u4ee5\u6211\u4eec\u8bbf\u95ee d->getName() \uff0c\u5b83\u5c06\u5c1d\u8bd5\u89e3\u5f15\u7528\u7a7a\u6307\u9488\uff0c\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a (\u53ef\u80fd\u662f\u5d29\u6e83)\u3002 \u4e3a\u4e86\u4f7f\u8fd9\u4e2a\u7a0b\u5e8f\u5b89\u5168\uff0c\u9700\u8981\u786e\u4fdd dynamic_cast \u7684\u7ed3\u679c\u5b9e\u9645\u4e0a\u662f\u6210\u529f\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 int main () { Base * b { getObject ( true ) }; Derived * d { dynamic_cast < Derived *> ( b ) }; // use dynamic cast to convert Base pointer into Derived pointer if ( d ) // \u786e\u4fdd d \u662f\u975e\u7a7a std :: cout << \"The name of the Derived is: \" << d -> getName () << '\\n' ; delete b ; return 0 ; } \u6cd5\u5219 \u603b\u662f\u8981\u901a\u8fc7\u68c0\u67e5\u8fd4\u56de\u503c\u662f\u5426\u4e3a\u7a7a\u6307\u9488\u6765\u786e\u4fdd\u52a8\u6001\u8f6c\u6362\u6210\u529f\u3002 \u6ce8\u610f\uff0c\u7531\u4e8e dynamic_cast \u5728\u8fd0\u884c\u65f6\u8fdb\u884c\u4e00\u4e9b\u4e00\u81f4\u6027\u68c0\u67e5(\u4ee5\u786e\u4fdd\u53ef\u4ee5\u8fdb\u884c\u8f6c\u6362)\uff0c\u56e0\u6b64\u4f7f\u7528 dynamic_cast \u786e\u5b9e\u4f1a\u5bfc\u81f4\u6027\u80fd\u635f\u5931\u3002 \u6b64\u5916\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u60c5\u5f62\uff0c\u6b64\u65f6\u4f7f\u7528 dynamic_cast \u8fdb\u884c \u5411\u4e0b\u8f6c\u6362 \u662f\u4e0d\u80fd\u6210\u529f\u7684\uff1a \u53d7\u4fdd\u62a4\u7ee7\u627f \u548c \u79c1\u6709\u7ee7\u627f \u7684\u7c7b\uff1b \u5bf9\u4e8e\u6ca1\u6709\u58f0\u660e\u6216\u7ee7\u627f\u4efb\u4f55\u865a\u51fd\u6570(\u56e0\u6b64\u6ca1\u6709\u865a\u8868)\u7684\u7c7b\uff1b \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6d89\u53ca \u865a\u57fa\u7c7b \u7684\u60c5\u51b5 (\u53c2\u8003 \u8fd9\u91cc \u67e5\u770b\u4e00\u4e9b\u60c5\u51b5\u7684\u793a\u4f8b\uff0c\u4ee5\u53ca\u5982\u4f55\u89e3\u51b3\u5b83\u4eec)\u3002","title":"dynamic_cast \u5931\u8d25"},{"location":"18-10-dynamic-casting/#static_cast","text":"\u4e8b\u5b9e\u8bc1\u660e\uff0c\u5411\u4e0b\u8f6c\u6362\u4e5f\u53ef\u4ee5\u4f7f\u7528 static_cast \u5b8c\u6210\u3002 \u4e3b\u8981\u7684\u533a\u522b\u662f static_cast \u4e0d\u8fdb\u884c\u8fd0\u884c\u65f6\u7c7b\u578b\u68c0\u67e5 \uff0c\u4ee5\u786e\u4fdd\u60a8\u6240\u505a\u7684\u5de5\u4f5c\u662f\u6709\u610f\u4e49\u7684\u3002\u8fd9\u4f7f\u5f97\u4f7f\u7528 static_cast \u66f4\u5feb\uff0c\u4f46\u66f4\u5371\u9669\u3002\u5982\u679c\u5c06 Base* \u8f6c\u6362\u4e3a Derived* \uff0c\u5373\u4f7f\u57fa\u7c7b\u6307\u9488\u6ca1\u6709\u6307\u5411\u6d3e\u751f\u7c7b\u5bf9\u8c61\uff0c\u5b83\u4e5f\u4f1a\u201c\u6210\u529f\u201d\u3002\u5f53\u60a8\u5c1d\u8bd5\u8bbf\u95ee\u7ed3\u679c\u6d3e\u751f\u6307\u9488(\u5b9e\u9645\u4e0a\u6307\u5411 Base \u5bf9\u8c61)\u65f6\uff0c\u8fd9\u5c06\u5bfc\u81f4\u672a\u5b9a\u4e49\u7684\u884c\u4e3a\u3002 \u5982\u679c\u4f60\u53ef\u4ee5\u4fdd\u8bc1\u5411\u4e0b\u5f3a\u5236\u8f6c\u6362\u7684\u6307\u9488\u5c06\u6210\u529f\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528 static_cast \u3002\u8fd9\u91cc\u6709\u4e00\u4e2a(\u4e0d\u662f\u5f88\u597d\u7684)\u65b9\u6cd5\uff0c\u901a\u8fc7\u865a\u51fd\u6570\u5224\u65ad\u6240\u6307\u7684\u5bf9\u8c61\u662f\u4e0d\u662f\u6b63\u786e\u7684\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 #include #include // Class identifier enum class ClassID { base , derived // Others can be added here later }; class Base { protected : int m_value {}; public : Base ( int value ) : m_value { value } { } virtual ~ Base () = default ; virtual ClassID getClassID () const { return ClassID :: base ; } }; class Derived : public Base { protected : std :: string m_name {}; public : Derived ( int value , const std :: string & name ) : Base { value }, m_name { name } { } const std :: string & getName () const { return m_name ; } virtual ClassID getClassID () const { return ClassID :: derived ; } }; Base * getObject ( bool bReturnDerived ) { if ( bReturnDerived ) return new Derived { 1 , \"Apple\" }; else return new Base { 2 }; } int main () { Base * b { getObject ( true ) }; if ( b -> getClassID () == ClassID :: derived ) { // We already proved b is pointing to a Derived object, so this should always succeed Derived * d { static_cast < Derived *> ( b ) }; std :: cout << \"The name of the Derived is: \" << d -> getName () << '\\n' ; } delete b ; return 0 ; } \u867d\u7136\u7684\u786e\u6709\u7528\u4f46\u662f\u5b9e\u73b0\u8d77\u6765\u8fd8\u662f\u6709\u70b9\u8d39\u4e8b\u7684(\u8fd8\u8981\u4ed8\u51fa\u8c03\u7528\u865a\u51fd\u6570\u548c\u5904\u7406\u7ed3\u679c\u7684\u4ee3\u4ef7)\uff0c\u90a3\u4e48\u8fd8\u4e0d\u5982\u4f7f\u7528\u76f4\u63a5\u4f7f\u7528 dynamic_cast \u3002","title":"\u4f7f\u7528 static_cast\u8fdb\u884c\u5411\u4e0b\u8f6c\u6362"},{"location":"18-10-dynamic-casting/#dynamic_cast_3","text":"\u5c3d\u7ba1\u4e0a\u9762\u7684\u6240\u6709\u793a\u4f8b\u90fd\u4ee5\u6307\u9488\u7684\u52a8\u6001\u5f3a\u5236\u8f6c\u6362(\u8fd9\u66f4\u5e38\u89c1)\u4e3a\u4f8b\uff0c\u4f46 dynamic_cast \u4e5f\u53ef\u4ee5\u7528\u4e8e\u5f15\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include #include class Base { protected : int m_value ; public : Base ( int value ) : m_value { value } { } virtual ~ Base () = default ; }; class Derived : public Base { protected : std :: string m_name ; public : Derived ( int value , const std :: string & name ) : Base { value }, m_name { name } { } const std :: string & getName () const { return m_name ; } }; int main () { Derived apple { 1 , \"Apple\" }; // create an apple Base & b { apple }; // set base reference to object Derived & d { dynamic_cast < Derived &> ( b ) }; // \u5bf9\u5f15\u7528\u8fdb\u884c\u52a8\u6001\u8f6c\u6362 std :: cout << \"The name of the Derived is: \" << d . getName () << '\\n' ; // \u4f7f\u7528 d \u8bbf\u95ee Derived::getName through d return 0 ; } \u56e0\u4e3aC++\u4e2d\u6ca1\u6709\u201c\u7a7a\u5f15\u7528\u201d\uff0c\u6240\u6709 dynamic_cast \u5728\u5931\u8d25\u65f6\u4e0d\u80fd\u8fd4\u56de\u7a7a\u5f15\u7528\u3002\u6240\u4ee5\uff0c\u5982\u679c\u5f15\u7528\u7684 dynamic_cast \u5931\u8d25\uff0c\u5219\u4f1a\u629b\u51fa std::bad_cast \u7c7b\u578b\u7684\u5f02\u5e38\u3002\u6211\u4eec\u5c06\u5728\u672c\u6559\u7a0b\u540e\u9762\u8ba8\u8bba\u5f02\u5e38\u3002","title":"dynamic_cast \u548c\u5f15\u7528"},{"location":"18-10-dynamic-casting/#dynamic_cast-vs-static_cast","text":"\u65b0\u624b\u7a0b\u5e8f\u5458\u6709\u65f6\u4f1a\u5bf9\u4f55\u65f6\u4f7f\u7528 static_cast \u548c dynamic_cast \u611f\u5230\u56f0\u60d1\u3002\u7b54\u6848\u5f88\u7b80\u5355\uff1a\u9664\u975e\u662f\u5411\u4e0b\u7c7b\u578b\u8f6c\u6362\uff08\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cdynamic_cast\u901a\u5e38\u662f\u66f4\u597d\u7684\u9009\u62e9\uff09\uff0c\u5426\u5219\u4e00\u5f8b\u4f7f\u7528 static_cast\u3002\u4f46\u662f\uff0c\u4f60\u8fd8\u5e94\u8be5\u8003\u8651\u5b8c\u5168\u907f\u514d\u5f3a\u5236\u8f6c\u6362\uff0c\u800c\u53ea\u4f7f\u7528\u865a\u51fd\u6570\u3002","title":"dynamic_cast vs static_cast"},{"location":"18-10-dynamic-casting/#vs","text":"\u6709\u4e9b\u5f00\u53d1\u4eba\u5458\u8ba4\u4e3a dynamic_cast \u662f\u90aa\u6076\u7684\uff0c\u662f\u7c7b\u6ca1\u6709\u88ab\u8bbe\u8ba1\u597d\u7684\u7279\u5f81\u3002\u76f8\u53cd\uff0c\u8fd9\u4e9b\u7a0b\u5e8f\u5458\u8ba4\u4e3a\u5e94\u8be5\u4f7f\u7528\u865a\u51fd\u6570\u3002 \u4e00\u822c\u6765\u8bf4\uff0c \u4f7f\u7528\u865a\u51fd\u6570\u5e94\u8be5\u4f18\u5148\u4e8e\u5411\u4e0b\u8f6c\u6362 \u3002\u7136\u800c\uff0c\u6709\u65f6\u5019\u4f7f\u7528\u5411\u4e0b\u8f6c\u6362\u4f1a\u662f\u66f4\u597d\u7684\u9009\u62e9\uff1a \u5f53\u4f60\u4e0d\u80fd\u4fee\u6539\u57fa\u7c7b\u6765\u6dfb\u52a0\u865a\u51fd\u6570\u65f6(\u4f8b\u5982\uff0c\u56e0\u4e3a\u57fa\u7c7b\u662f\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206) \u5f53\u4f60\u9700\u8981\u8bbf\u95ee\u7279\u5b9a\u4e8e\u6d3e\u751f\u7c7b\u7684\u4e1c\u897f\u65f6(\u4f8b\u5982\uff0c\u4e00\u4e2a\u53ea\u5b58\u5728\u4e8e\u6d3e\u751f\u7c7b\u4e2d\u7684\u8bbf\u95ee\u51fd\u6570) \u5728\u57fa\u7c7b\u4e2d\u6dfb\u52a0\u865a\u51fd\u6570\u662f\u6ca1\u6709\u610f\u4e49\u7684(\u4f8b\u5982\uff0c\u57fa\u7c7b\u6ca1\u6709\u9002\u5f53\u7684\u8fd4\u56de\u503c)\u3002\u5982\u679c\u4e0d\u9700\u8981\u5b9e\u4f8b\u5316\u57fa\u7c7b\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u7eaf\u865a\u51fd\u6570\u3002","title":"\u5411\u4e0b\u8f6c\u6362 vs \u865a\u51fd\u6570"},{"location":"18-10-dynamic-casting/#dynamic_cast-rtti","text":"\u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f(RTTI) \u662fC++\u7684\u4e00\u4e2a\u7279\u6027\uff0c\u5b83\u5728\u8fd0\u884c\u65f6\u4f1a\u66b4\u9732\u5bf9\u8c61\u6570\u636e\u7c7b\u578b\u7684\u4fe1\u606f\u3002 dynamic_cast \u5229\u7528\u4e86\u7279\u6027\u3002\u56e0\u4e3aRTTI\u6709\u76f8\u5f53\u5927\u7684\u7a7a\u95f4\u6027\u80fd\u5f00\u9500\uff0c\u4e00\u4e9b\u7f16\u8bd1\u5668\u5141\u8bb8\u4f60\u5173\u95edRTTI\u4f5c\u4e3a\u4e00\u79cd\u4f18\u5316\u65b9\u5f0f\u3002\u4e0d\u7528\u8bf4\uff0c\u5982\u679c\u8fd9\u6837\u505a\uff0c dynamic_cast \u5c06\u4e0d\u80fd\u6b63\u786e\u5de5\u4f5c\u3002","title":"\u5bf9 dynamic_cast \u548c RTTI \u7684\u4e00\u4e9b\u8b66\u793a"},{"location":"18-11-printing-inherited-classes-using-operator%3C%3C/","tags":["inheritance","operator<<"],"text":"Key Takeaway \u53cb\u5143\u51fd\u6570 \u4e0d\u5c5e\u4e8e \u6210\u5458\u51fd\u6570 \uff0c\u56e0\u6b64\u4e0d\u80fd\u662f \u865a\u51fd\u6570 \u4e0d\u540c\u7c7b << \u9700\u8981\u4f20\u5165\u4e0d\u540c\u7c7b\u7684\u5bf9\u8c61\uff0c\u6240\u4ee5\u5373\u4f7f\u80fd\u5b9a\u4e49\u4e3a\u865a\u51fd\u6570\uff0c\u6d3e\u751f\u7c7b\u4e5f\u6ca1\u6cd5\u91cd\u5199\u5b83 \u53cb\u5143\u7684\u8fd0\u7b97\u7b26\u53ef\u4ee5\u5c06\u5b9e\u9645\u5de5\u4f5c\u59d4\u6d3e\u7ed9\u4e00\u4e2a\u666e\u901a\u7684\u6210\u5458\u51fd\u6570\uff08 \u865a\u51fd\u6570 \uff09\uff0c\u800c\u4e14\u65e0\u9700\u5728\u6d3e\u751f\u7c7b\u4e2d\u5b9e\u73b0\u8be5\u8fd0\u7b97\u7b26\uff0c\u53ea\u9700\u8981\u5b9e\u73b0\u8be5\u865a\u51fd\u6570\u7684\u91cd\u5199\u51fd\u6570\u5373\u53ef \u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\u4f7f\u7528\u4e86 \u865a\u51fd\u6570 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include class Base { public : virtual void print () const { std :: cout << \"Base\" ; } }; class Derived : public Base { public : void print () const override { std :: cout << \"Derived\" ; } }; int main () { Derived d {}; Base & b { d }; b . print (); // will call Derived::print() return 0 ; } b.print() \u4f1a\u8c03\u7528 Derived::print() (\u56e0\u4e3a b \u6307\u5411 Derived \u7c7b\u578b\u7684\u5bf9\u8c61 object\uff0c Base::print() \u662f\u4e00\u4e2a\u865a\u51fd\u6570\uff0c\u800c\u4e14 Derived::print() \u662f \u91cd\u5199 \u51fd\u6570)\u3002 \u867d\u7136\u8c03\u7528 print() \u8fd9\u6837\u7684\u6210\u5458\u51fd\u6570\u6765\u6267\u884c\u8f93\u51fa\u662f\u53ef\u4ee5\u7684\uff0c\u4f46\u8fd9\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u4e0d\u80fd\u5f88\u597d\u5730\u4e0e std::cout \u4e00\u8d77\u4f7f\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { Derived d {}; Base & b { d }; std :: cout << \"b is a \" ; b . print (); // \u663e\u5f97\u6742\u4e71\uff0c\u4e0e\u4e00\u822c\u7684\u8f93\u51fa\u8bed\u53e5\u683c\u683c\u4e0d\u5165 std :: cout << '\\n' ; return 0 ; } \u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u5b66\u4e60\u5982\u4f55\u4f7f\u7528 operator<< \u6253\u5370\u7ee7\u627f\u7c7b\u7684\u4fe1\u606f\uff0c\u4f7f\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u98ce\u683c\u6765\u4f7f\u7528\uff1a 1 std :: cout << \"b is a \" << b << '\\n' ; // much better \u6311\u6218 \u9996\u5148\u6211\u4eec\u4f7f\u7528 \u91cd\u8f7d \u7684\u65b9\u5f0f\u5b9e\u73b0 operator<< \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include class Base { public : virtual void print () const { std :: cout << \"Base\" ; } friend std :: ostream & operator << ( std :: ostream & out , const Base & b ) { out << \"Base\" ; return out ; } }; class Derived : public Base { public : void print () const override { std :: cout << \"Derived\" ; } friend std :: ostream & operator << ( std :: ostream & out , const Derived & d ) { out << \"Derived\" ; return out ; } }; int main () { Base b {}; std :: cout << b << '\\n' ; Derived d {}; std :: cout << d << '\\n' ; return 0 ; } \u7531\u4e8e\u4e0d\u9700\u8981 \u865a\u51fd\u6570 \u89e3\u6790\uff0c\u6240\u4ee5\u7a0b\u5e8f\u80fd\u591f\u6b63\u786e\u5730\u5de5\u4f5c\u5e76\u6253\u5370\uff1a 1 2 Base Derived \u518d\u8003\u8651\u4e0b\u9762\u7684 main() \uff1a 1 2 3 4 5 6 7 8 int main () { Derived d {}; Base & bref { d }; std :: cout << bref << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 Base \u663e\u7136\u8f93\u51fa\u7ed3\u679c\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u3002\u5f53\u914d\u5408 Base \u4f7f\u7528\u975e\u865a\u7684 operator<< \u65f6\uff0c std::cout << bref \u4f1a\u8c03\u7528\u57fa\u7c7b\u7684 operator<< \u3002 \u8fd9\u5c31\u662f\u6211\u4eec\u8981\u9762\u4e34\u7684\u6311\u6218\u3002 Operator << \u53ef\u4ee5\u662f\u865a\u51fd\u6570\u5417\uff1f \u5982\u679c\u95ee\u9898\u7684\u539f\u56e0\u5728\u4e8e operator<< \u4e0d\u662f\u865a\u51fd\u6570\uff0c\u90a3\u4e48\u53ef\u4ee5\u628a\u5b83\u8bbe\u4e3a virtual \u5417\uff1f \u4e0d\u884c\uff01\u800c\u4e14\u6709\u5f88\u591a\u539f\u56e0\uff1a \u9996\u5148\uff0c\u53ea\u6709\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u88ab\u865a\u62df\u5316\u2014\u2014\u8fd9\u662f\u6709\u610f\u4e49\u7684\uff0c\u56e0\u4e3a\u53ea\u6709\u7c7b\u53ef\u4ee5\u4ece\u5176\u4ed6\u7c7b\u7ee7\u627f\uff0c\u6ca1\u6709\u529e\u6cd5\u91cd\u5199\u5b58\u5728\u4e8e\u7c7b\u5916\u90e8\u7684\u51fd\u6570(\u53ef\u4ee5\u91cd\u8f7d\u975e\u6210\u5458\u51fd\u6570\uff0c\u4f46\u4e0d\u80fd\u91cd\u5199\u5b83\u4eec)\u3002\u56e0\u4e3a\u6211\u4eec\u901a\u5e38\u5c06\u64cd\u4f5c\u7b26 << \u5b9e\u73b0\u4e3a\u53cb\u5143\uff0c \u800c\u53cb\u5143\u4e0d\u88ab\u89c6\u4e3a\u6210\u5458\u51fd\u6570 \uff0c\u6240\u4ee5\u64cd\u4f5c\u7b26<<\u7684\u53cb\u5143\u7248\u672c\u4e0d\u7b26\u5408\u79f0\u4e3a\u865a\u51fd\u6570\u7684\u6761\u4ef6\u3002(\u8981\u4e86\u89e3\u4e3a\u4ec0\u4e48\u8981\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u5b9e\u73b0\u64cd\u4f5c\u7b26<<\uff0c\u8bf7\u770b 14.5 -\u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 )\u3002 \u5176\u6b21\uff0c\u5373\u4f7f\u6211\u4eec\u53ef\u4ee5\u628a operator<< \u5b9a\u4e49\u4e3a\u865a\u51fd\u6570\uff0c\u4e5f\u5b58\u5728 Base::operator<< \u548c Derived::operator<< \u7684\u51fd\u6570\u5f62\u53c2\u4e0d\u540c\u7684\u95ee\u9898(Base\u7248\u672c\u5c06\u63a5\u53d7Base\u5f62\u53c2\uff0c\u800cDerived\u7248\u672c\u5c06\u63a5\u53d7Derived\u5f62\u53c2)\u3002\u56e0\u6b64\uff0c Derived \u7248\u672c\u4e0d\u4f1a\u88ab\u8ba4\u4e3a\u662f Base \u7248\u672c\u7684 \u91cd\u5199 \uff0c\u56e0\u6b64\u4e0d\u7b26\u5408\u865a\u51fd\u6570\u89e3\u6790\u7684\u6761\u4ef6\u3002 \u90a3\u4e48\u5e94\u8be5\u600e\u4e48\u505a\u5462\uff1f \u89e3\u51b3\u529e\u6cd5 \u7b54\u6848\u975e\u5e38\u7b80\u5355\u3002 \u9996\u5148\uff0c\u5728\u7c7b\u4e2d\u6dfb\u52a0 \u53cb\u5143\u51fd\u6570 operator<< \u3002\u4f46\u662f\uff0c\u4e0d\u8981\u8ba9 operator<< \u81ea\u5df1\u5426\u5219\u5b9e\u9645\u7684\u6253\u5370\uff0c\u800c\u662f\u5c06\u8fd9\u6253\u5370\u5de5\u4f5c\u59d4\u6258\u7ed9\u4e00\u4e2a\u53ef\u4ee5\u88ab\u865a\u5316\u7684\u666e\u901a\u6210\u5458\u51fd\u6570\uff01 \u4ee5\u4e0b\u662f\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include class Base { public : // \u91cd\u8f7d\u7684 operator<< friend std :: ostream & operator << ( std :: ostream & out , const Base & b ) { // \u59d4\u6d3e\u7ed9\u6210\u5458\u51fd\u6570 print() return b . print ( out ); } // \u6211\u4eec\u4f9d\u8d56 print() \u51fd\u6570\u8fdb\u884c\u5b9e\u9645\u7684\u6253\u5370\u5de5\u4f5c // \u56e0\u4e3a print \u662f\u4e00\u4e2a\u666e\u901a\u7684\u6210\u5458\u51fd\u6570\uff0c\u6240\u4ee5\u53ef\u4ee5\u662f\u865a\u51fd\u6570 virtual std :: ostream & print ( std :: ostream & out ) const { out << \"Base\" ; return out ; } }; class Derived : public Base { public : // \u91cd\u5199\u7684 print \u51fd\u6570 std :: ostream & print ( std :: ostream & out ) const override { out << \"Derived\" ; return out ; } //\u4e0d\u9700\u8981\u5b9e\u73b0operator<< }; int main () { Base b {}; std :: cout << b << '\\n' ; Derived d {}; std :: cout << d << '\\n' ; // \u6ce8\u610f\uff0c\u8fd9\u4e00\u884c\u4ee3\u7801\u53ef\u4ee5\u6b63\u786e\u5de5\u4f5c\uff0c\u5373\u4f7f\u6d3e\u751f\u7c7b\u90fd\u6ca1\u6709\u5b9e\u73b0\u8be5\u8fd0\u7b97\u7b26 Base & bref { d }; std :: cout << bref << '\\n' ; return 0 ; } \u4e09\u6761\u8bed\u53e5\u90fd\u80fd\u6b63\u786e\u6267\u884c\uff1a 1 2 3 Base Derived Derived \u8ba9\u6211\u4eec\u66f4\u8be6\u7ec6\u5730\u7814\u7a76\u4e00\u4e0b\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9\u3002 \u9996\u5148\uff0c\u5bf9\u4e8e Base \u7684\u4f8b\u5b50\uff0c\u8c03\u7528 operator<< \u65f6\u4f1a\u8c03\u7528\u865a\u51fd\u6570 print() \u3002\u56e0\u4e3a\u7684 Base \u5f15\u7528\u53c2\u6570\u6307\u5411\u4e00\u4e2a Base \u5bf9\u8c61\uff0c b.print() \u89e3\u6790\u4e3a Base::print() \u5e76\u6267\u884c\u6253\u5370\u3002\u8fd9\u91cc\u6ca1\u4ec0\u4e48\u7279\u522b\u7684\u3002 \u5728\u5bf9\u4e8e Derived \u7684\u4f8b\u5b50\uff0c\u7f16\u8bd1\u5668\u9996\u5148\u67e5\u770b\u662f\u5426\u6709\u63a5\u53d7 Derived \u5bf9\u8c61\u7684 << \u3002\u6ca1\u6709\uff0c\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u5b9a\u4e49\uff0c\u63a5\u4e0b\u6765\uff0c\u7f16\u8bd1\u5668\u67e5\u770b\u662f\u5426\u6709\u63a5\u53d7 Base \u5bf9\u8c61\u7684 << \u3002\u6709\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u5c06 Derived \u5bf9\u8c61\u9690\u5f0f\u4e0a\u8f6c\u6362\u4e3a Base& \u5e76\u8c03\u7528\u51fd\u6570\uff0c\u7136\u540e\u8c03\u7528\u865a print() \uff0c\u89e3\u6790\u4e3a Derived::print() \u3002 \u6ce8\u610f\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u4e3a\u6bcf\u4e2a\u6d3e\u751f\u7c7b\u5b9a\u4e49 operator<< \uff0c\u5904\u7406 Base \u5bf9\u8c61\u7684\u7248\u672c\u5bf9 Base \u53ca\u5176\u6d3e\u751f\u7684\u4efb\u4f55\u7c7b\u53ef\u7528\uff01 \u7b2c\u4e09\u79cd\u60c5\u51b5\u662f\u524d\u4e24\u79cd\u60c5\u51b5\u7684\u6df7\u5408\u3002\u9996\u5148\uff0c\u7f16\u8bd1\u5668\u5c06\u53d8\u91cf bref \u4e0e\u5e26\u6709 Base \u53c2\u6570\u7684 operator<< \u5339\u914d\u3002\u5b83\u8c03\u7528\u4e86\u865a\u51fd\u6570 print() \u3002\u56e0\u4e3a Base \u5f15\u7528\u5b9e\u9645\u4e0a\u662f\u6307\u5411\u4e00\u4e2a Derived \u5bf9\u8c61\uff0c\u6240\u4ee5\u5b83\u89e3\u6790\u4e3a Derived::print() \uff0c\u6b63\u5982\u6211\u4eec\u671f\u671b\u7684\u90a3\u6837\u3002 \u95ee\u9898\u641e\u5b9a\uff01","title":"18.11 - \u4f7f\u7528 << \u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b"},{"location":"18-11-printing-inherited-classes-using-operator%3C%3C/#_1","text":"\u9996\u5148\u6211\u4eec\u4f7f\u7528 \u91cd\u8f7d \u7684\u65b9\u5f0f\u5b9e\u73b0 operator<< \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include class Base { public : virtual void print () const { std :: cout << \"Base\" ; } friend std :: ostream & operator << ( std :: ostream & out , const Base & b ) { out << \"Base\" ; return out ; } }; class Derived : public Base { public : void print () const override { std :: cout << \"Derived\" ; } friend std :: ostream & operator << ( std :: ostream & out , const Derived & d ) { out << \"Derived\" ; return out ; } }; int main () { Base b {}; std :: cout << b << '\\n' ; Derived d {}; std :: cout << d << '\\n' ; return 0 ; } \u7531\u4e8e\u4e0d\u9700\u8981 \u865a\u51fd\u6570 \u89e3\u6790\uff0c\u6240\u4ee5\u7a0b\u5e8f\u80fd\u591f\u6b63\u786e\u5730\u5de5\u4f5c\u5e76\u6253\u5370\uff1a 1 2 Base Derived \u518d\u8003\u8651\u4e0b\u9762\u7684 main() \uff1a 1 2 3 4 5 6 7 8 int main () { Derived d {}; Base & bref { d }; std :: cout << bref << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 Base \u663e\u7136\u8f93\u51fa\u7ed3\u679c\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u3002\u5f53\u914d\u5408 Base \u4f7f\u7528\u975e\u865a\u7684 operator<< \u65f6\uff0c std::cout << bref \u4f1a\u8c03\u7528\u57fa\u7c7b\u7684 operator<< \u3002 \u8fd9\u5c31\u662f\u6211\u4eec\u8981\u9762\u4e34\u7684\u6311\u6218\u3002","title":"\u6311\u6218"},{"location":"18-11-printing-inherited-classes-using-operator%3C%3C/#operator","text":"\u5982\u679c\u95ee\u9898\u7684\u539f\u56e0\u5728\u4e8e operator<< \u4e0d\u662f\u865a\u51fd\u6570\uff0c\u90a3\u4e48\u53ef\u4ee5\u628a\u5b83\u8bbe\u4e3a virtual \u5417\uff1f \u4e0d\u884c\uff01\u800c\u4e14\u6709\u5f88\u591a\u539f\u56e0\uff1a \u9996\u5148\uff0c\u53ea\u6709\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u88ab\u865a\u62df\u5316\u2014\u2014\u8fd9\u662f\u6709\u610f\u4e49\u7684\uff0c\u56e0\u4e3a\u53ea\u6709\u7c7b\u53ef\u4ee5\u4ece\u5176\u4ed6\u7c7b\u7ee7\u627f\uff0c\u6ca1\u6709\u529e\u6cd5\u91cd\u5199\u5b58\u5728\u4e8e\u7c7b\u5916\u90e8\u7684\u51fd\u6570(\u53ef\u4ee5\u91cd\u8f7d\u975e\u6210\u5458\u51fd\u6570\uff0c\u4f46\u4e0d\u80fd\u91cd\u5199\u5b83\u4eec)\u3002\u56e0\u4e3a\u6211\u4eec\u901a\u5e38\u5c06\u64cd\u4f5c\u7b26 << \u5b9e\u73b0\u4e3a\u53cb\u5143\uff0c \u800c\u53cb\u5143\u4e0d\u88ab\u89c6\u4e3a\u6210\u5458\u51fd\u6570 \uff0c\u6240\u4ee5\u64cd\u4f5c\u7b26<<\u7684\u53cb\u5143\u7248\u672c\u4e0d\u7b26\u5408\u79f0\u4e3a\u865a\u51fd\u6570\u7684\u6761\u4ef6\u3002(\u8981\u4e86\u89e3\u4e3a\u4ec0\u4e48\u8981\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u5b9e\u73b0\u64cd\u4f5c\u7b26<<\uff0c\u8bf7\u770b 14.5 -\u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 )\u3002 \u5176\u6b21\uff0c\u5373\u4f7f\u6211\u4eec\u53ef\u4ee5\u628a operator<< \u5b9a\u4e49\u4e3a\u865a\u51fd\u6570\uff0c\u4e5f\u5b58\u5728 Base::operator<< \u548c Derived::operator<< \u7684\u51fd\u6570\u5f62\u53c2\u4e0d\u540c\u7684\u95ee\u9898(Base\u7248\u672c\u5c06\u63a5\u53d7Base\u5f62\u53c2\uff0c\u800cDerived\u7248\u672c\u5c06\u63a5\u53d7Derived\u5f62\u53c2)\u3002\u56e0\u6b64\uff0c Derived \u7248\u672c\u4e0d\u4f1a\u88ab\u8ba4\u4e3a\u662f Base \u7248\u672c\u7684 \u91cd\u5199 \uff0c\u56e0\u6b64\u4e0d\u7b26\u5408\u865a\u51fd\u6570\u89e3\u6790\u7684\u6761\u4ef6\u3002 \u90a3\u4e48\u5e94\u8be5\u600e\u4e48\u505a\u5462\uff1f","title":"Operator << \u53ef\u4ee5\u662f\u865a\u51fd\u6570\u5417\uff1f"},{"location":"18-11-printing-inherited-classes-using-operator%3C%3C/#_2","text":"\u7b54\u6848\u975e\u5e38\u7b80\u5355\u3002 \u9996\u5148\uff0c\u5728\u7c7b\u4e2d\u6dfb\u52a0 \u53cb\u5143\u51fd\u6570 operator<< \u3002\u4f46\u662f\uff0c\u4e0d\u8981\u8ba9 operator<< \u81ea\u5df1\u5426\u5219\u5b9e\u9645\u7684\u6253\u5370\uff0c\u800c\u662f\u5c06\u8fd9\u6253\u5370\u5de5\u4f5c\u59d4\u6258\u7ed9\u4e00\u4e2a\u53ef\u4ee5\u88ab\u865a\u5316\u7684\u666e\u901a\u6210\u5458\u51fd\u6570\uff01 \u4ee5\u4e0b\u662f\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include class Base { public : // \u91cd\u8f7d\u7684 operator<< friend std :: ostream & operator << ( std :: ostream & out , const Base & b ) { // \u59d4\u6d3e\u7ed9\u6210\u5458\u51fd\u6570 print() return b . print ( out ); } // \u6211\u4eec\u4f9d\u8d56 print() \u51fd\u6570\u8fdb\u884c\u5b9e\u9645\u7684\u6253\u5370\u5de5\u4f5c // \u56e0\u4e3a print \u662f\u4e00\u4e2a\u666e\u901a\u7684\u6210\u5458\u51fd\u6570\uff0c\u6240\u4ee5\u53ef\u4ee5\u662f\u865a\u51fd\u6570 virtual std :: ostream & print ( std :: ostream & out ) const { out << \"Base\" ; return out ; } }; class Derived : public Base { public : // \u91cd\u5199\u7684 print \u51fd\u6570 std :: ostream & print ( std :: ostream & out ) const override { out << \"Derived\" ; return out ; } //\u4e0d\u9700\u8981\u5b9e\u73b0operator<< }; int main () { Base b {}; std :: cout << b << '\\n' ; Derived d {}; std :: cout << d << '\\n' ; // \u6ce8\u610f\uff0c\u8fd9\u4e00\u884c\u4ee3\u7801\u53ef\u4ee5\u6b63\u786e\u5de5\u4f5c\uff0c\u5373\u4f7f\u6d3e\u751f\u7c7b\u90fd\u6ca1\u6709\u5b9e\u73b0\u8be5\u8fd0\u7b97\u7b26 Base & bref { d }; std :: cout << bref << '\\n' ; return 0 ; } \u4e09\u6761\u8bed\u53e5\u90fd\u80fd\u6b63\u786e\u6267\u884c\uff1a 1 2 3 Base Derived Derived \u8ba9\u6211\u4eec\u66f4\u8be6\u7ec6\u5730\u7814\u7a76\u4e00\u4e0b\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9\u3002 \u9996\u5148\uff0c\u5bf9\u4e8e Base \u7684\u4f8b\u5b50\uff0c\u8c03\u7528 operator<< \u65f6\u4f1a\u8c03\u7528\u865a\u51fd\u6570 print() \u3002\u56e0\u4e3a\u7684 Base \u5f15\u7528\u53c2\u6570\u6307\u5411\u4e00\u4e2a Base \u5bf9\u8c61\uff0c b.print() \u89e3\u6790\u4e3a Base::print() \u5e76\u6267\u884c\u6253\u5370\u3002\u8fd9\u91cc\u6ca1\u4ec0\u4e48\u7279\u522b\u7684\u3002 \u5728\u5bf9\u4e8e Derived \u7684\u4f8b\u5b50\uff0c\u7f16\u8bd1\u5668\u9996\u5148\u67e5\u770b\u662f\u5426\u6709\u63a5\u53d7 Derived \u5bf9\u8c61\u7684 << \u3002\u6ca1\u6709\uff0c\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u5b9a\u4e49\uff0c\u63a5\u4e0b\u6765\uff0c\u7f16\u8bd1\u5668\u67e5\u770b\u662f\u5426\u6709\u63a5\u53d7 Base \u5bf9\u8c61\u7684 << \u3002\u6709\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u5c06 Derived \u5bf9\u8c61\u9690\u5f0f\u4e0a\u8f6c\u6362\u4e3a Base& \u5e76\u8c03\u7528\u51fd\u6570\uff0c\u7136\u540e\u8c03\u7528\u865a print() \uff0c\u89e3\u6790\u4e3a Derived::print() \u3002 \u6ce8\u610f\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u4e3a\u6bcf\u4e2a\u6d3e\u751f\u7c7b\u5b9a\u4e49 operator<< \uff0c\u5904\u7406 Base \u5bf9\u8c61\u7684\u7248\u672c\u5bf9 Base \u53ca\u5176\u6d3e\u751f\u7684\u4efb\u4f55\u7c7b\u53ef\u7528\uff01 \u7b2c\u4e09\u79cd\u60c5\u51b5\u662f\u524d\u4e24\u79cd\u60c5\u51b5\u7684\u6df7\u5408\u3002\u9996\u5148\uff0c\u7f16\u8bd1\u5668\u5c06\u53d8\u91cf bref \u4e0e\u5e26\u6709 Base \u53c2\u6570\u7684 operator<< \u5339\u914d\u3002\u5b83\u8c03\u7528\u4e86\u865a\u51fd\u6570 print() \u3002\u56e0\u4e3a Base \u5f15\u7528\u5b9e\u9645\u4e0a\u662f\u6307\u5411\u4e00\u4e2a Derived \u5bf9\u8c61\uff0c\u6240\u4ee5\u5b83\u89e3\u6790\u4e3a Derived::print() \uff0c\u6b63\u5982\u6211\u4eec\u671f\u671b\u7684\u90a3\u6837\u3002 \u95ee\u9898\u641e\u5b9a\uff01","title":"\u89e3\u51b3\u529e\u6cd5"},{"location":"18-2-virtual-functions-and-polymorphism/","tags":["virtual functions","polymorphism"],"text":"Key Takeaway \u865a\u51fd\u6570\u5c06\u89e3\u6790\u4e3a\u6700\u540e\u88ab\u6d3e\u751f\u7684\u7248\u672c\uff08\u5728\u539f\u59cb\u5bf9\u8c61\u548c\u5f15\u7528\u5bf9\u8c61\u4e4b\u95f4\u7684\u6d3e\u751f\u94fe\u4e0a\u67e5\u627e\uff09\uff0c\u5373\u4fbf\u4f7f\u7528\u57fa\u7c7b\u6307\u9488\u64cd\u4f5c\u4e5f\u662f\u4e00\u6837\uff08\u4e0d\u7136\u610f\u4e49\u4f55\u5728\uff09 \u9f99\u751f\u9f99\u51e4\u751f\u51e4\uff0c\u865a\u51fd\u6570\u7684\u91cd\u5199\u51fd\u6570\u865a\u51fd\u6570 \u89e3\u6790\u865a\u51fd\u6570\u8c03\u7528\u7684\u65f6\u95f4\u6bd4\u89e3\u6790\u5e38\u89c4\u51fd\u6570\u8c03\u7528\u7684\u65f6\u95f4\u957f\u3002\u6b64\u5916\uff0c\u7f16\u8bd1\u5668\u8fd8\u5fc5\u987b\u4e3a\u6bcf\u4e2a\u5177\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u865a\u51fd\u6570\u7684\u7c7b\u5bf9\u8c61\u5206\u914d\u4e00\u4e2a\u989d\u5916\u7684\u6307\u9488 \u5728 18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u4e00\u4e9b\u53ef\u4ee5\u4f7f\u7528\u57fa\u7c7b\u6307\u9488\u6216\u5f15\u7528\u7b80\u5316\u4ee3\u7801\u7684\u4f8b\u5b50\u3002\u4f46\u662f\uff0c\u5728\u8fd9\u4e9b\u4f8b\u5b50\u4e2d\uff0c\u6700\u5927\u7684\u95ee\u9898\u5c31\u5728\u4e8e\u57fa\u7c7b\u7684\u6307\u9488\u53ea\u80fd\u591f\u8c03\u7528\u57fa\u7c7b\u7248\u672c\u7684\u51fd\u6570\uff0c\u65e0\u6cd5\u8c03\u7528\u6d3e\u751f\u7c7b\u7684\u51fd\u6570\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include class Base { public : std :: string_view getName () const { return \"Base\" ; } }; class Derived : public Base { public : std :: string_view getName () const { return \"Derived\" ; } }; int main () { Derived derived ; Base & rBase { derived }; std :: cout << \"rBase is a \" << rBase . getName () << '\\n' ; return 0 ; } \u6253\u5370\uff1a rBase is a Base \u56e0\u4e3a rBase \u662f Base \u7c7b\u578b\u7684\u5f15\u7528\uff0c\u5b83\u4f1a\u8c03\u7528 Base::getName() \uff0c\u5373\u4fbf\u5b83\u5b9e\u9645\u4e0a\u5f15\u7528\u7684\u662f Derived \u7c7b\u578b\u4e2d\u7684 Base \u90e8\u5206\u3002 \u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528 \u865a\u51fd\u6570 \u6765\u89e3\u51b3\u8be5\u95ee\u9898\u3002 \u865a\u51fd\u6570\u548c\u591a\u6001 \u865a\u51fd\u6570 \u662f\u4e00\u7c7b\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5f53\u5b83\u88ab\u8c03\u7528\u65f6\uff0c\u5b9e\u9645\u89e3\u6790\u5230\u7684\u662f\u6700\u540e\u6d3e\u751f\u7c7b\u4e2d\u5bf9\u5e94\u7248\u672c\u7684\u51fd\u6570\uff08\u9700\u8981\u540c\u65f6\u5b58\u5728\u4e8e\u6d3e\u751f\u7c7b\u548c\u57fa\u7c7b\u4e2d\uff09\u3002\u8fd9\u4e2a\u673a\u5236\u79f0\u4e3a \u591a\u6001 \u3002\u5982\u679c\u6d3e\u751f\u7c7b\u4e2d\u6709\u5177\u6709\u76f8\u540c \u7b7e\u540d (\u51fd\u6570\u540d\u3001\u53c2\u6570\u7c7b\u578b\u3001\u662f\u5426\u4e3aconst\uff09\u5e76\u4e14\u8fd4\u56de\u503c\u7c7b\u578b\u4e00\u6837\u7684\u51fd\u6570\uff0c\u5219\u8fd9\u4e9b\u51fd\u6570\u79f0\u4e3a \u91cd\u5199\u51fd\u6570\uff08override\uff09 \u3002 \u5b9a\u4e49\u865a\u51fd\u6570\u65f6\uff0c\u53ea\u9700\u5728\u51fd\u6570\u58f0\u660e\u524d\u6dfb\u52a0\u201cvirtual\u201d\u5173\u952e\u5b57\u3002 \u4f7f\u7528\u865a\u51fd\u6570\u6539\u5199\u4e0a\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include class Base { public : virtual std :: string_view getName () const { return \"Base\" ; } // note addition of virtual keyword }; class Derived : public Base { public : virtual std :: string_view getName () const { return \"Derived\" ; } }; int main () { Derived derived ; Base & rBase { derived }; std :: cout << \"rBase is a \" << rBase . getName () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 rBase is a Derived \u56e0\u4e3a rBase \u662f Derived \u7c7b\u578b\u5bf9\u8c61\u4e2d Base \u90e8\u5206\u7684\u5f15\u7528\uff0c\u56e0\u6b64\u5f53 rBase.getName() \u88ab\u8c03\u7528\u65f6\uff0c\u5b83\u4f1a\u89e3\u6790\u4e3a Base::getName() \u3002\u4e0d\u8fc7\uff0c\u7531\u4e8e\u8be5\u51fd\u6570\u662f\u865a\u51fd\u6570\uff0c\u6240\u4ee5\u7a0b\u5e8f\u4f1a\u7ee7\u7eed\u6cbf\u7740\u6d3e\u751f\u7684\u65b9\u5411\u67e5\u627e\uff0c\u5982\u679c\u5728 Base \u548c Derived \u4e2d\u5b58\u5728\u8be5\u51fd\u6570\u8fdb\u4e00\u6b65\u6d3e\u751f\u7684\u7248\u672c\uff0c\u5219\u4f1a\u8c03\u7528\u8be5\u51fd\u6570\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u5b9e\u9645\u8c03\u7528\u7684\u662f Derived::getName() \uff01 \u518d\u770b\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include #include class A { public : virtual std :: string_view getName () const { return \"A\" ; } }; class B : public A { public : virtual std :: string_view getName () const { return \"B\" ; } }; class C : public B { public : virtual std :: string_view getName () const { return \"C\" ; } }; class D : public C { public : virtual std :: string_view getName () const { return \"D\" ; } }; int main () { C c ; A & rBase { c }; //C\u7c7b\u578b\u4e2d\u7684A\u57fa\u7c7b\u90e8\u5206 std :: cout << \"rBase is a \" << rBase . getName () << '\\n' ; return 0 ; } \u4f60\u89c9\u5f97\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4f1a\u662f\u4ec0\u4e48\uff1f \u6765\u770b\u770b\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u9996\u5148\uff0c\u5b9e\u4f8b\u5316\u4e00\u4e2aC\u7c7b\u578b\u7684\u5bf9\u8c61\u3002 rBase \u662f\u4e00\u4e2aA\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u5f15\u7528C\u7c7b\u578b\u5bf9\u8c61\u4e2d\u7684A\u7c7b\u578b\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u5f53 rBase.getName() \u88ab\u8c03\u7528\u65f6\uff0c\u5b83\u4f1a\u6c42\u503c\u5f97\u5230 A::getName() \u3002\u4f46\u662f\u7531\u4e8e A::getName() \u662f\u865a\u51fd\u6570\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u8c03\u7528A\u7c7b\u578b\u548cC\u7c7b\u578b\u4e2d\u6700\u540e\u88ab\u6d3e\u751f\u7684\u8be5\u51fd\u6570\u7248\u672c\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u663e\u7136\u662f C::getName() \u3002\u6ce8\u610f\uff0c\u5b83\u4e0d\u4f1a\u89e3\u6790\u5230 D::getName() \uff0c\u56e0\u4e3a\u539f\u59cb\u5bf9\u8c61\u662f C\uff0c\u800c\u4e0d\u662fD\uff0c\u6240\u4ee5D\u7684\u6210\u5458\u4e0d\u5728\u8003\u8651\u8303\u56f4\u5185\u3002 \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 rBase is a C \u66f4\u590d\u6742\u7684\u4f8b\u5b50 \u518d\u6765\u770b\u770b\u6211\u4eec\u5728\u4e0a\u4e00\u8bfe\u4e2d\u4f7f\u7528\u7684\u52a8\u7269\u7684\u4f8b\u5b50\u3002\u4e0b\u9762\u662f\u539f\u59cb\u7684\u4ee3\u7801\uff0c\u4ee5\u53ca\u4e00\u4e9b\u6d4b\u8bd5\u4ee3\u7801: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 #include #include #include class Animal { protected : std :: string m_name ; // We're making this constructor protected because // we don't want people creating Animal objects directly, // but we still want derived classes to be able to use it. Animal ( const std :: string & name ) : m_name { name } { } public : const std :: string & getName () const { return m_name ; } std :: string_view speak () const { return \"???\" ; } }; class Cat : public Animal { public : Cat ( const std :: string & name ) : Animal { name } { } std :: string_view speak () const { return \"Meow\" ; } }; class Dog : public Animal { public : Dog ( const std :: string & name ) : Animal { name } { } std :: string_view speak () const { return \"Woof\" ; } }; void report ( const Animal & animal ) { std :: cout << animal . getName () << \" says \" << animal . speak () << '\\n' ; } int main () { Cat cat { \"Fred\" }; Dog dog { \"Garbo\" }; report ( cat ); report ( dog ); return 0 ; } \u8fd0\u884c\u7ed3\u679c 1 2 Fred says ??? Garbo says ??? \u63a5\u4e0b\u6765\u7684\u4f8b\u5b50\u4e2d\uff0c speak() \u51fd\u6570\u88ab\u5b9a\u4e49\u4e3a\u865a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 #include #include #include class Animal { protected : std :: string m_name ; // We're making this constructor protected because // we don't want people creating Animal objects directly, // but we still want derived classes to be able to use it. Animal ( const std :: string & name ) : m_name { name } { } public : const std :: string & getName () const { return m_name ; } virtual std :: string_view speak () const { return \"???\" ; } //\u865a\u51fd\u6570 }; class Cat : public Animal { public : Cat ( const std :: string & name ) : Animal { name } { } virtual std :: string_view speak () const { return \"Meow\" ; } }; class Dog : public Animal { public : Dog ( const std :: string & name ) : Animal { name } { } virtual std :: string_view speak () const { return \"Woof\" ; } }; void report ( const Animal & animal ) { std :: cout << animal . getName () << \" says \" << animal . speak () << '\\n' ; } int main () { Cat cat { \"Fred\" }; Dog dog { \"Garbo\" }; report ( cat ); report ( dog ); return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Fred says Meow Garbo says Woof \u53ef\u4ee5\u6b63\u786e\u5de5\u4f5c\u4e86\u3002 \u5f53 animal.speak() \u6c42\u804c\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u6ce8\u610f\u5230 Animal::speak() \u662f\u4e00\u4e2a\u865a\u51fd\u6570\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c animal \u5f15\u7528\u7684\u662f Cat \u7c7b\u4e2d\u7684 Animal \u90e8\u5206\u3002\u7a0b\u5e8f\u4f1a\u5728Animal \u548c Cat \u4e4b\u95f4\u6240\u6709\u7684\u6d3e\u751f\u7c7b\u4e2d\u67e5\u770b\u662f\u5426\u6709\u8be5\u51fd\u6570\u8fdb\u4e00\u6b65\u88ab\u6d3e\u751f\u7684\u7248\u672c\u3002\u5728\u672c\u4f8b\u4e2d\uff0c Cat::speak() \u5c31\u4f1a\u88ab\u8c03\u7528\u3002\u5bf9\u4e8e Dog \u4e5f\u7c7b\u4f3c\uff0c\u7a0b\u5e8f\u89e3\u6790\u5230\u7684\u51fd\u6570\u662f Dog::speak() \u3002 \u6ce8\u610f\uff0c\u6211\u4eec\u6ca1\u6709\u5c06 Animal::getName() \u5b9a\u4e49\u4e3a\u865a\u51fd\u6570\u3002\u8fd9\u662f\u56e0\u4e3a getName() \u5e76\u6ca1\u6709\u5728\u4efb\u4f55\u6d3e\u751f\u7c7b\u4e2d\u88ab \u91cd\u5199 \uff0c\u6240\u4ee5\u6ca1\u6709\u5fc5\u8981\u8bbe\u7f6e\u4e3a\u865a\u51fd\u6570\u3002 \u540c\u6837\u5730\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u4e5f\u80fd\u591f\u6309\u7167\u6211\u4eec\u7684\u9700\u8981\u6b63\u5e38\u5de5\u4f5c\u4e86\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 Cat fred { \"Fred\" }; Cat misty { \"Misty\" }; Cat zeke { \"Zeke\" }; Dog garbo { \"Garbo\" }; Dog pooky { \"Pooky\" }; Dog truffle { \"Truffle\" }; // Set up an array of pointers to animals, and set those pointers to our Cat and Dog objects Animal * animals []{ & fred , & garbo , & misty , & pooky , & truffle , & zeke }; for ( const auto * animal : animals ) std :: cout << animal -> getName () << \" says \" << animal -> speak () << '\\n' ; \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 Fred says Meow Garbo says Woof Misty says Meow Pooky says Woof Truffle says Woof Zeke says Meow \u5c3d\u7ba1\u8fd9\u4e24\u4e2a\u4f8b\u5b50\u53ea\u4f7f\u7528\u4e86 Cat \u548c Dog \uff0c\u4f46\u6211\u4eec\u4ece Animal \u6d3e\u751f\u7684\u4efb\u4f55\u5176\u4ed6\u7c7b\u4e5f\u53ef\u4ee5\u4f7f\u7528 report() \u51fd\u6570\u548c Animal \u6570\u7ec4\uff0c\u65e0\u9700\u8fdb\u4e00\u6b65\u4fee\u6539\uff01\u8fd9\u53ef\u80fd\u662f\u865a\u51fd\u6570\u7684\u6700\u5927\u597d\u5904\u2014\u2014\u80fd\u591f\u4ee5\u8fd9\u6837\u4e00\u79cd\u65b9\u5f0f\u6784\u9020\u4ee3\u7801\uff0c\u5373\u65b0\u6d3e\u751f\u7684\u7c7b\u5c06\u81ea\u52a8\u4e0e\u65e7\u4ee3\u7801\u4e00\u8d77\u5de5\u4f5c\uff0c\u800c\u65e0\u9700\u4fee\u6539! \u6ce8\u610f\uff1a\u8981\u4f7f\u7528\u6d3e\u751f\u7c7b\u51fd\u6570\uff0c \u6d3e\u751f\u7c7b\u51fd\u6570\u7684\u7b7e\u540d\u5fc5\u987b\u4e0e\u57fa\u7c7b\u865a\u51fd\u6570\u7684\u7b7e\u540d\u5b8c\u5168\u5339\u914d \uff08\u8fd4\u56de\u503c\u4e0d\u662f\u7b7e\u540d\u7684\u4e00\u90e8\u5206\uff09\u3002\u5982\u679c\u6d3e\u751f\u7c7b\u51fd\u6570\u5177\u6709\u4e0d\u540c\u7684\u5f62\u53c2\u7c7b\u578b\uff0c\u5219\u7a0b\u5e8f\u4ecd\u53ef\u80fd\u7f16\u8bd1\uff0c\u4f46\u865a\u51fd\u6570\u5c06\u4e0d\u80fd\u6309\u9884\u671f\u89e3\u6790\u3002\u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5982\u4f55\u9632\u8303\u8fd9\u79cd\u60c5\u51b5\u3002 \u8fd8\u8981\u6ce8\u610f\uff0c \u5982\u679c\u4e00\u4e2a\u51fd\u6570\u88ab\u6807\u8bb0\u4e3a\u865a\u51fd\u6570\uff0c\u90a3\u4e48\u6240\u6709\u7684\u5339\u914d \u91cd\u5199 \u51fd\u6570\u4e5f\u88ab\u8ba4\u4e3a\u662f\u865a\u51fd\u6570 \uff0c\u5373\u4f7f\u5b83\u4eec\u6ca1\u6709\u88ab\u663e\u5f0f\u5730\u6807\u8bb0\u4e3a\u865a\u51fd\u6570\u3002 \u865a\u51fd\u6570\u7684\u8fd4\u56de\u503c \u6b63\u5e38\u60c5\u51b5\u4e0b\uff0c\u865a\u51fd\u6570\u7684\u51fd\u6570\u7684\u8fd4\u56de\u503c\u548c \u91cd\u5199 \u51fd\u6570\u7684\u8fd4\u56de\u503c\u5fc5\u987b\u8981\u5339\u914d\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 class Base { public : virtual int getValue () const { return 5 ; } }; class Derived : public Base { public : virtual double getValue () const { return 6.78 ; } }; \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c Derived::getValue() is \u5e76\u4e0d\u4f1a\u88ab\u8ba4\u4e3a\u662f\u5bf9 Base::getValue() \u7684\u91cd\u5199\uff0c \u56e0\u6b64\u7f16\u8bd1\u4f1a\u5931\u8d25 \u3002 \u4e0d\u8981\u5728\u6784\u9020\u51fd\u6570\u6216\u6790\u6784\u51fd\u6570\u4e2d\u8c03\u7528\u865a\u51fd\u6570 \u8fd9\u662f\u53e6\u4e00\u4e2a\u7ecf\u5e38\u4f24\u5230\u83dc\u9e1f\u4eec\u7684\u9677\u9631\u662f\u2014\u2014\u4e0d\u5e94\u8be5\u4ece\u6784\u9020\u51fd\u6570\u6216\u6790\u6784\u51fd\u6570\u8c03\u7528\u865a\u51fd\u6570\u3002 \u8bb0\u4f4f\uff0c\u5728\u521b\u5efa\u6d3e\u751f\u7c7b\u65f6\uff0c\u9996\u5148\u6784\u9020\u57fa\u7c7b\u90e8\u5206\u3002\u5982\u679c\u5728 Base \u6784\u9020\u51fd\u6570\u8c03\u7528\u865a\u51fd\u6570\uff0c\u800c\u7c7b\u7684\u6d3e\u751f\u90e8\u5206\u751a\u81f3\u8fd8\u6ca1\u6709\u521b\u5efa\uff0c\u5219\u65e0\u6cd5\u8c03\u7528\u51fd\u6570\u7684\u6d3e\u751f\u7248\u672c\uff0c\u56e0\u4e3a\u6ca1\u6709\u6d3e\u751f\u5bf9\u8c61\u4f9b\u6d3e\u751f\u51fd\u6570\u5904\u7406\u3002\u5728C++\u4e2d\uff0c\u5b83\u4f1a\u8c03\u7528Base\u7248\u672c\u3002\uff08 17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f \uff09 \u6790\u6784\u51fd\u6570\u4e5f\u5b58\u5728\u7c7b\u4f3c\u7684\u95ee\u9898\u3002 \u5982\u679c\u5728\u57fa\u7c7b\u6790\u6784\u51fd\u6570\u4e2d\u8c03\u7528\u865a\u51fd\u6570\uff0c\u5b83\u5c06\u59cb\u7ec8\u89e3\u6790\u4e3a\u8be5\u51fd\u6570\u7684\u57fa\u7c7b\u7248\u672c \uff0c\u56e0\u4e3a\u7c7b\u7684\u6d3e\u751f\u90e8\u5206\u5c06\u5df2\u7ecf\u88ab\u9500\u6bc1\u3002 \u6700\u4f73\u5b9e\u8df5 \u6c38\u8fdc\u4e0d\u8981\u5728\u6784\u9020\u51fd\u6570\u6216\u6790\u6784\u51fd\u6570\u4e2d\u8c03\u7528\u865a\u51fd\u6570 \u865a\u51fd\u6570\u7684\u7f3a\u70b9 \u65e2\u7136\u5927\u591a\u6570\u65f6\u5019\u4f60\u4f1a\u5e0c\u671b\u51fd\u6570\u662f\u865a\u51fd\u6570\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u8ba9\u6240\u6709\u7684\u51fd\u6570\u90fd\u662f\u865a\u7684\u5462\uff1f\u56e0\u4e3a\u8fd9\u4e48\u505a\u6548\u7387\u5f88\u4f4e\u2014\u2014 \u89e3\u6790\u865a\u51fd\u6570\u8c03\u7528\u7684\u65f6\u95f4\u6bd4\u89e3\u6790\u5e38\u89c4\u51fd\u6570\u8c03\u7528\u7684\u65f6\u95f4\u957f\u3002\u6b64\u5916\uff0c\u7f16\u8bd1\u5668\u8fd8\u5fc5\u987b\u4e3a\u6bcf\u4e2a\u5177\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u865a\u51fd\u6570\u7684\u7c7b\u5bf9\u8c61\u5206\u914d\u4e00\u4e2a\u989d\u5916\u7684\u6307\u9488 \u3002\u6211\u4eec\u5c06\u5728\u672c\u7ae0\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e2a\u95ee\u9898\u3002","title":"18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001"},{"location":"18-2-virtual-functions-and-polymorphism/#_1","text":"\u865a\u51fd\u6570 \u662f\u4e00\u7c7b\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5f53\u5b83\u88ab\u8c03\u7528\u65f6\uff0c\u5b9e\u9645\u89e3\u6790\u5230\u7684\u662f\u6700\u540e\u6d3e\u751f\u7c7b\u4e2d\u5bf9\u5e94\u7248\u672c\u7684\u51fd\u6570\uff08\u9700\u8981\u540c\u65f6\u5b58\u5728\u4e8e\u6d3e\u751f\u7c7b\u548c\u57fa\u7c7b\u4e2d\uff09\u3002\u8fd9\u4e2a\u673a\u5236\u79f0\u4e3a \u591a\u6001 \u3002\u5982\u679c\u6d3e\u751f\u7c7b\u4e2d\u6709\u5177\u6709\u76f8\u540c \u7b7e\u540d (\u51fd\u6570\u540d\u3001\u53c2\u6570\u7c7b\u578b\u3001\u662f\u5426\u4e3aconst\uff09\u5e76\u4e14\u8fd4\u56de\u503c\u7c7b\u578b\u4e00\u6837\u7684\u51fd\u6570\uff0c\u5219\u8fd9\u4e9b\u51fd\u6570\u79f0\u4e3a \u91cd\u5199\u51fd\u6570\uff08override\uff09 \u3002 \u5b9a\u4e49\u865a\u51fd\u6570\u65f6\uff0c\u53ea\u9700\u5728\u51fd\u6570\u58f0\u660e\u524d\u6dfb\u52a0\u201cvirtual\u201d\u5173\u952e\u5b57\u3002 \u4f7f\u7528\u865a\u51fd\u6570\u6539\u5199\u4e0a\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include class Base { public : virtual std :: string_view getName () const { return \"Base\" ; } // note addition of virtual keyword }; class Derived : public Base { public : virtual std :: string_view getName () const { return \"Derived\" ; } }; int main () { Derived derived ; Base & rBase { derived }; std :: cout << \"rBase is a \" << rBase . getName () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 rBase is a Derived \u56e0\u4e3a rBase \u662f Derived \u7c7b\u578b\u5bf9\u8c61\u4e2d Base \u90e8\u5206\u7684\u5f15\u7528\uff0c\u56e0\u6b64\u5f53 rBase.getName() \u88ab\u8c03\u7528\u65f6\uff0c\u5b83\u4f1a\u89e3\u6790\u4e3a Base::getName() \u3002\u4e0d\u8fc7\uff0c\u7531\u4e8e\u8be5\u51fd\u6570\u662f\u865a\u51fd\u6570\uff0c\u6240\u4ee5\u7a0b\u5e8f\u4f1a\u7ee7\u7eed\u6cbf\u7740\u6d3e\u751f\u7684\u65b9\u5411\u67e5\u627e\uff0c\u5982\u679c\u5728 Base \u548c Derived \u4e2d\u5b58\u5728\u8be5\u51fd\u6570\u8fdb\u4e00\u6b65\u6d3e\u751f\u7684\u7248\u672c\uff0c\u5219\u4f1a\u8c03\u7528\u8be5\u51fd\u6570\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u5b9e\u9645\u8c03\u7528\u7684\u662f Derived::getName() \uff01 \u518d\u770b\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include #include class A { public : virtual std :: string_view getName () const { return \"A\" ; } }; class B : public A { public : virtual std :: string_view getName () const { return \"B\" ; } }; class C : public B { public : virtual std :: string_view getName () const { return \"C\" ; } }; class D : public C { public : virtual std :: string_view getName () const { return \"D\" ; } }; int main () { C c ; A & rBase { c }; //C\u7c7b\u578b\u4e2d\u7684A\u57fa\u7c7b\u90e8\u5206 std :: cout << \"rBase is a \" << rBase . getName () << '\\n' ; return 0 ; } \u4f60\u89c9\u5f97\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4f1a\u662f\u4ec0\u4e48\uff1f \u6765\u770b\u770b\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u9996\u5148\uff0c\u5b9e\u4f8b\u5316\u4e00\u4e2aC\u7c7b\u578b\u7684\u5bf9\u8c61\u3002 rBase \u662f\u4e00\u4e2aA\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u5f15\u7528C\u7c7b\u578b\u5bf9\u8c61\u4e2d\u7684A\u7c7b\u578b\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u5f53 rBase.getName() \u88ab\u8c03\u7528\u65f6\uff0c\u5b83\u4f1a\u6c42\u503c\u5f97\u5230 A::getName() \u3002\u4f46\u662f\u7531\u4e8e A::getName() \u662f\u865a\u51fd\u6570\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u8c03\u7528A\u7c7b\u578b\u548cC\u7c7b\u578b\u4e2d\u6700\u540e\u88ab\u6d3e\u751f\u7684\u8be5\u51fd\u6570\u7248\u672c\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u663e\u7136\u662f C::getName() \u3002\u6ce8\u610f\uff0c\u5b83\u4e0d\u4f1a\u89e3\u6790\u5230 D::getName() \uff0c\u56e0\u4e3a\u539f\u59cb\u5bf9\u8c61\u662f C\uff0c\u800c\u4e0d\u662fD\uff0c\u6240\u4ee5D\u7684\u6210\u5458\u4e0d\u5728\u8003\u8651\u8303\u56f4\u5185\u3002 \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 rBase is a C","title":"\u865a\u51fd\u6570\u548c\u591a\u6001"},{"location":"18-2-virtual-functions-and-polymorphism/#_2","text":"\u518d\u6765\u770b\u770b\u6211\u4eec\u5728\u4e0a\u4e00\u8bfe\u4e2d\u4f7f\u7528\u7684\u52a8\u7269\u7684\u4f8b\u5b50\u3002\u4e0b\u9762\u662f\u539f\u59cb\u7684\u4ee3\u7801\uff0c\u4ee5\u53ca\u4e00\u4e9b\u6d4b\u8bd5\u4ee3\u7801: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 #include #include #include class Animal { protected : std :: string m_name ; // We're making this constructor protected because // we don't want people creating Animal objects directly, // but we still want derived classes to be able to use it. Animal ( const std :: string & name ) : m_name { name } { } public : const std :: string & getName () const { return m_name ; } std :: string_view speak () const { return \"???\" ; } }; class Cat : public Animal { public : Cat ( const std :: string & name ) : Animal { name } { } std :: string_view speak () const { return \"Meow\" ; } }; class Dog : public Animal { public : Dog ( const std :: string & name ) : Animal { name } { } std :: string_view speak () const { return \"Woof\" ; } }; void report ( const Animal & animal ) { std :: cout << animal . getName () << \" says \" << animal . speak () << '\\n' ; } int main () { Cat cat { \"Fred\" }; Dog dog { \"Garbo\" }; report ( cat ); report ( dog ); return 0 ; } \u8fd0\u884c\u7ed3\u679c 1 2 Fred says ??? Garbo says ??? \u63a5\u4e0b\u6765\u7684\u4f8b\u5b50\u4e2d\uff0c speak() \u51fd\u6570\u88ab\u5b9a\u4e49\u4e3a\u865a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 #include #include #include class Animal { protected : std :: string m_name ; // We're making this constructor protected because // we don't want people creating Animal objects directly, // but we still want derived classes to be able to use it. Animal ( const std :: string & name ) : m_name { name } { } public : const std :: string & getName () const { return m_name ; } virtual std :: string_view speak () const { return \"???\" ; } //\u865a\u51fd\u6570 }; class Cat : public Animal { public : Cat ( const std :: string & name ) : Animal { name } { } virtual std :: string_view speak () const { return \"Meow\" ; } }; class Dog : public Animal { public : Dog ( const std :: string & name ) : Animal { name } { } virtual std :: string_view speak () const { return \"Woof\" ; } }; void report ( const Animal & animal ) { std :: cout << animal . getName () << \" says \" << animal . speak () << '\\n' ; } int main () { Cat cat { \"Fred\" }; Dog dog { \"Garbo\" }; report ( cat ); report ( dog ); return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Fred says Meow Garbo says Woof \u53ef\u4ee5\u6b63\u786e\u5de5\u4f5c\u4e86\u3002 \u5f53 animal.speak() \u6c42\u804c\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u6ce8\u610f\u5230 Animal::speak() \u662f\u4e00\u4e2a\u865a\u51fd\u6570\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c animal \u5f15\u7528\u7684\u662f Cat \u7c7b\u4e2d\u7684 Animal \u90e8\u5206\u3002\u7a0b\u5e8f\u4f1a\u5728Animal \u548c Cat \u4e4b\u95f4\u6240\u6709\u7684\u6d3e\u751f\u7c7b\u4e2d\u67e5\u770b\u662f\u5426\u6709\u8be5\u51fd\u6570\u8fdb\u4e00\u6b65\u88ab\u6d3e\u751f\u7684\u7248\u672c\u3002\u5728\u672c\u4f8b\u4e2d\uff0c Cat::speak() \u5c31\u4f1a\u88ab\u8c03\u7528\u3002\u5bf9\u4e8e Dog \u4e5f\u7c7b\u4f3c\uff0c\u7a0b\u5e8f\u89e3\u6790\u5230\u7684\u51fd\u6570\u662f Dog::speak() \u3002 \u6ce8\u610f\uff0c\u6211\u4eec\u6ca1\u6709\u5c06 Animal::getName() \u5b9a\u4e49\u4e3a\u865a\u51fd\u6570\u3002\u8fd9\u662f\u56e0\u4e3a getName() \u5e76\u6ca1\u6709\u5728\u4efb\u4f55\u6d3e\u751f\u7c7b\u4e2d\u88ab \u91cd\u5199 \uff0c\u6240\u4ee5\u6ca1\u6709\u5fc5\u8981\u8bbe\u7f6e\u4e3a\u865a\u51fd\u6570\u3002 \u540c\u6837\u5730\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u4e5f\u80fd\u591f\u6309\u7167\u6211\u4eec\u7684\u9700\u8981\u6b63\u5e38\u5de5\u4f5c\u4e86\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 Cat fred { \"Fred\" }; Cat misty { \"Misty\" }; Cat zeke { \"Zeke\" }; Dog garbo { \"Garbo\" }; Dog pooky { \"Pooky\" }; Dog truffle { \"Truffle\" }; // Set up an array of pointers to animals, and set those pointers to our Cat and Dog objects Animal * animals []{ & fred , & garbo , & misty , & pooky , & truffle , & zeke }; for ( const auto * animal : animals ) std :: cout << animal -> getName () << \" says \" << animal -> speak () << '\\n' ; \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 Fred says Meow Garbo says Woof Misty says Meow Pooky says Woof Truffle says Woof Zeke says Meow \u5c3d\u7ba1\u8fd9\u4e24\u4e2a\u4f8b\u5b50\u53ea\u4f7f\u7528\u4e86 Cat \u548c Dog \uff0c\u4f46\u6211\u4eec\u4ece Animal \u6d3e\u751f\u7684\u4efb\u4f55\u5176\u4ed6\u7c7b\u4e5f\u53ef\u4ee5\u4f7f\u7528 report() \u51fd\u6570\u548c Animal \u6570\u7ec4\uff0c\u65e0\u9700\u8fdb\u4e00\u6b65\u4fee\u6539\uff01\u8fd9\u53ef\u80fd\u662f\u865a\u51fd\u6570\u7684\u6700\u5927\u597d\u5904\u2014\u2014\u80fd\u591f\u4ee5\u8fd9\u6837\u4e00\u79cd\u65b9\u5f0f\u6784\u9020\u4ee3\u7801\uff0c\u5373\u65b0\u6d3e\u751f\u7684\u7c7b\u5c06\u81ea\u52a8\u4e0e\u65e7\u4ee3\u7801\u4e00\u8d77\u5de5\u4f5c\uff0c\u800c\u65e0\u9700\u4fee\u6539! \u6ce8\u610f\uff1a\u8981\u4f7f\u7528\u6d3e\u751f\u7c7b\u51fd\u6570\uff0c \u6d3e\u751f\u7c7b\u51fd\u6570\u7684\u7b7e\u540d\u5fc5\u987b\u4e0e\u57fa\u7c7b\u865a\u51fd\u6570\u7684\u7b7e\u540d\u5b8c\u5168\u5339\u914d \uff08\u8fd4\u56de\u503c\u4e0d\u662f\u7b7e\u540d\u7684\u4e00\u90e8\u5206\uff09\u3002\u5982\u679c\u6d3e\u751f\u7c7b\u51fd\u6570\u5177\u6709\u4e0d\u540c\u7684\u5f62\u53c2\u7c7b\u578b\uff0c\u5219\u7a0b\u5e8f\u4ecd\u53ef\u80fd\u7f16\u8bd1\uff0c\u4f46\u865a\u51fd\u6570\u5c06\u4e0d\u80fd\u6309\u9884\u671f\u89e3\u6790\u3002\u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5982\u4f55\u9632\u8303\u8fd9\u79cd\u60c5\u51b5\u3002 \u8fd8\u8981\u6ce8\u610f\uff0c \u5982\u679c\u4e00\u4e2a\u51fd\u6570\u88ab\u6807\u8bb0\u4e3a\u865a\u51fd\u6570\uff0c\u90a3\u4e48\u6240\u6709\u7684\u5339\u914d \u91cd\u5199 \u51fd\u6570\u4e5f\u88ab\u8ba4\u4e3a\u662f\u865a\u51fd\u6570 \uff0c\u5373\u4f7f\u5b83\u4eec\u6ca1\u6709\u88ab\u663e\u5f0f\u5730\u6807\u8bb0\u4e3a\u865a\u51fd\u6570\u3002","title":"\u66f4\u590d\u6742\u7684\u4f8b\u5b50"},{"location":"18-2-virtual-functions-and-polymorphism/#_3","text":"\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0c\u865a\u51fd\u6570\u7684\u51fd\u6570\u7684\u8fd4\u56de\u503c\u548c \u91cd\u5199 \u51fd\u6570\u7684\u8fd4\u56de\u503c\u5fc5\u987b\u8981\u5339\u914d\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 class Base { public : virtual int getValue () const { return 5 ; } }; class Derived : public Base { public : virtual double getValue () const { return 6.78 ; } }; \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c Derived::getValue() is \u5e76\u4e0d\u4f1a\u88ab\u8ba4\u4e3a\u662f\u5bf9 Base::getValue() \u7684\u91cd\u5199\uff0c \u56e0\u6b64\u7f16\u8bd1\u4f1a\u5931\u8d25 \u3002","title":"\u865a\u51fd\u6570\u7684\u8fd4\u56de\u503c"},{"location":"18-2-virtual-functions-and-polymorphism/#_4","text":"\u8fd9\u662f\u53e6\u4e00\u4e2a\u7ecf\u5e38\u4f24\u5230\u83dc\u9e1f\u4eec\u7684\u9677\u9631\u662f\u2014\u2014\u4e0d\u5e94\u8be5\u4ece\u6784\u9020\u51fd\u6570\u6216\u6790\u6784\u51fd\u6570\u8c03\u7528\u865a\u51fd\u6570\u3002 \u8bb0\u4f4f\uff0c\u5728\u521b\u5efa\u6d3e\u751f\u7c7b\u65f6\uff0c\u9996\u5148\u6784\u9020\u57fa\u7c7b\u90e8\u5206\u3002\u5982\u679c\u5728 Base \u6784\u9020\u51fd\u6570\u8c03\u7528\u865a\u51fd\u6570\uff0c\u800c\u7c7b\u7684\u6d3e\u751f\u90e8\u5206\u751a\u81f3\u8fd8\u6ca1\u6709\u521b\u5efa\uff0c\u5219\u65e0\u6cd5\u8c03\u7528\u51fd\u6570\u7684\u6d3e\u751f\u7248\u672c\uff0c\u56e0\u4e3a\u6ca1\u6709\u6d3e\u751f\u5bf9\u8c61\u4f9b\u6d3e\u751f\u51fd\u6570\u5904\u7406\u3002\u5728C++\u4e2d\uff0c\u5b83\u4f1a\u8c03\u7528Base\u7248\u672c\u3002\uff08 17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f \uff09 \u6790\u6784\u51fd\u6570\u4e5f\u5b58\u5728\u7c7b\u4f3c\u7684\u95ee\u9898\u3002 \u5982\u679c\u5728\u57fa\u7c7b\u6790\u6784\u51fd\u6570\u4e2d\u8c03\u7528\u865a\u51fd\u6570\uff0c\u5b83\u5c06\u59cb\u7ec8\u89e3\u6790\u4e3a\u8be5\u51fd\u6570\u7684\u57fa\u7c7b\u7248\u672c \uff0c\u56e0\u4e3a\u7c7b\u7684\u6d3e\u751f\u90e8\u5206\u5c06\u5df2\u7ecf\u88ab\u9500\u6bc1\u3002 \u6700\u4f73\u5b9e\u8df5 \u6c38\u8fdc\u4e0d\u8981\u5728\u6784\u9020\u51fd\u6570\u6216\u6790\u6784\u51fd\u6570\u4e2d\u8c03\u7528\u865a\u51fd\u6570","title":"\u4e0d\u8981\u5728\u6784\u9020\u51fd\u6570\u6216\u6790\u6784\u51fd\u6570\u4e2d\u8c03\u7528\u865a\u51fd\u6570"},{"location":"18-2-virtual-functions-and-polymorphism/#_5","text":"\u65e2\u7136\u5927\u591a\u6570\u65f6\u5019\u4f60\u4f1a\u5e0c\u671b\u51fd\u6570\u662f\u865a\u51fd\u6570\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u8ba9\u6240\u6709\u7684\u51fd\u6570\u90fd\u662f\u865a\u7684\u5462\uff1f\u56e0\u4e3a\u8fd9\u4e48\u505a\u6548\u7387\u5f88\u4f4e\u2014\u2014 \u89e3\u6790\u865a\u51fd\u6570\u8c03\u7528\u7684\u65f6\u95f4\u6bd4\u89e3\u6790\u5e38\u89c4\u51fd\u6570\u8c03\u7528\u7684\u65f6\u95f4\u957f\u3002\u6b64\u5916\uff0c\u7f16\u8bd1\u5668\u8fd8\u5fc5\u987b\u4e3a\u6bcf\u4e2a\u5177\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u865a\u51fd\u6570\u7684\u7c7b\u5bf9\u8c61\u5206\u914d\u4e00\u4e2a\u989d\u5916\u7684\u6307\u9488 \u3002\u6211\u4eec\u5c06\u5728\u672c\u7ae0\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e2a\u95ee\u9898\u3002","title":"\u865a\u51fd\u6570\u7684\u7f3a\u70b9"},{"location":"18-3-the-override-and-final-specifiers-and-covariant-return-types/","tags":["override","final","covariant-return-types"],"text":"Key Takeaway \u6d3e\u751f\u7c7b\u865a\u51fd\u6570\u53ea\u6709\u5728\u5176\u7b7e\u540d\u548c\u8fd4\u56de\u7c7b\u578b\u5b8c\u5168\u5339\u914d\u7684\u60c5\u51b5\u4e0b\u624d\u88ab\u8ba4\u4e3a\u662f\u91cd\u5199\uff0c\u4f7f\u7528\u5173\u952e\u5b57 override \u53ef\u4ee5\u9632\u6b62\u9519\u8bef \u5728\u57fa\u7c7b\u7684\u865a\u51fd\u6570\u4e2d\u4f7f\u7528 virtual \u5173\u952e\u5b57\u3002\u5728\u6d3e\u751f\u7c7b\u4e2d\u5bf9\u5e94\u7684\u51fd\u6570\u4e2d\u4f7f\u7528 override \u5173\u952e\u5b57\uff08\u65e0\u9700\u4f7f\u7528 virtual \u5173\u952e\u5b57\uff09\u3002 \u56e0\u4e3a override \u4fee\u9970\u7b26\u610f\u5473\u7740\u8be5\u51fd\u6570\u662f\u865a\u51fd\u6570\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u4f7f\u7528 virtual \u5173\u952e\u5b57\u91cd\u590d\u6807\u8bb0\u5df2\u7ecf\u88ab\u6807\u8bb0\u4e3a override \u7684\u51fd\u6570 \u5982\u679c\u865a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f\u6307\u5411\u76f8\u540c\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u5176\u91cd\u5199\u51fd\u6570\u4e5f\u53ef\u4ee5\u8fd4\u56de\u6307\u5411\u6d3e\u751f\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528\u3002\u8fd9\u4e2a\u673a\u5236\u79f0\u4e3a \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b \u4e3a\u4e86\u89e3\u51b3\u7ee7\u627f\u65b9\u9762\u7684\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\uff0cC++\u63d0\u4f9b\u4e86\u4e24\u4e2a\u7279\u6b8a\u6807\u8bc6\u7b26\uff1a override \u548c final \u3002\u6ce8\u610f\uff0c \u8fd9\u4e9b\u6807\u8bc6\u7b26\u4e0d\u88ab\u8ba4\u4e3a\u662f\u5173\u952e\u5b57 \u2014\u2014\u5b83\u4eec\u662f\u5728\u67d0\u4e9b\u4e0a\u4e0b\u6587\u4e2d\u5177\u6709\u7279\u6b8a\u542b\u4e49\u7684\u666e\u901a\u6807\u8bc6\u7b26\u3002 final \u4f7f\u7528\u5f97\u4e0d\u591a\uff0c\u4f46 override \u662f\u4e00\u4e2a\u5f88\u597d\u7684\u529f\u80fd\uff0c\u5e94\u8be5\u7ecf\u5e38\u4f7f\u7528\u3002\u5728\u8fd9\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u5206\u522b\u4ecb\u7ecd\u4e8c\u8005\uff0c\u4ee5\u53ca\u865a\u51fd\u6570\u91cd\u5199\u8fd4\u56de\u7c7b\u578b\u5fc5\u987b\u5339\u914d\u7684\u89c4\u5219\u7684\u4e00\u4e2a\u4f8b\u5916\u3002 override \u4fee\u9970\u7b26 \u6b63\u5982\u6211\u4eec\u5728\u4e0a\u4e00\u8bfe\u4e2d\u63d0\u5230\u7684\uff0c \u6d3e\u751f\u7c7b\u865a\u51fd\u6570\u53ea\u6709\u5728\u5176\u7b7e\u540d\u548c\u8fd4\u56de\u7c7b\u578b\u5b8c\u5168\u5339\u914d\u7684\u60c5\u51b5\u4e0b\u624d\u88ab\u8ba4\u4e3a\u662f\u91cd\u5199 \u3002\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5bb9\u6613\u88ab\u5ffd\u7565\u7684\u95ee\u9898\uff0c\u5373\u6253\u7b97\u88ab\u91cd\u5199\u7684\u51fd\u6570\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\u88ab\u91cd\u5199\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include class A { public : virtual std :: string_view getName1 ( int x ) { return \"A\" ; } virtual std :: string_view getName2 ( int x ) { return \"A\" ; } }; class B : public A { public : virtual std :: string_view getName1 ( short int x ) { return \"B\" ; } // \u6ce8\u610f: \u53c2\u6570\u662f short int virtual std :: string_view getName2 ( int x ) const { return \"B\" ; } // \u6ce8\u610f: \u51fd\u6570\u662f const }; int main () { B b {}; A & rBase { b }; std :: cout << rBase . getName1 ( 1 ) << '\\n' ; std :: cout << rBase . getName2 ( 2 ) << '\\n' ; return 0 ; } \u56e0\u4e3a rBase \u662fB\u4e2dA\u90e8\u5206\u7684\u5f15\u7528\uff0c\u6240\u4ee5\u8fd9\u91cc\u7684\u672c\u610f\u662f\u4f7f\u7528\u865a\u51fd\u6570\u6765\u8bbf\u95ee B::getName1() \u548c B::getName2() \u3002\u4f46\u662f\uff0c\u7531\u4e8e B::getName1() \u7684\u53c2\u6570\u5e76\u4e0d\u540c\u6240\u4ee5\u6ca1\u6709\u88ab\u770b\u505a\u662f\u5bf9 A::getName1() \u7684\u91cd\u5199\u3002\u66f4\u9690\u853d\u7684\u662f\uff0c\u56e0\u4e3a B::getName2() \u662f const \u800c A::getName2() \u4e0d\u662f\uff0c\u6240\u4ee5 B::getName2() \u4e5f\u4e0d\u4f1a\u88ab\u770b\u505a\u662f A::getName2() \u7684\u91cd\u5199\u3002 \u56e0\u6b64\uff0c\u7a0b\u5e8f\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 A A \u5728\u672c\u4f8b\u4e2d\uff0c\u56e0\u4e3aA\u548cB\u53ea\u662f\u6253\u5370\u4e86\u5b83\u4eec\u5404\u81ea\u7684\u540d\u79f0\uff0c\u6240\u4ee5\u5f88\u5bb9\u6613\u53d1\u73b0\u9519\u8bef\uff0c\u5373\u56e0\u4e3a\u91cd\u5199\u6ca1\u6709\u751f\u6548\u800c\u8c03\u7528\u4e86\u9519\u8bef\u7684\u865a\u51fd\u6570\u3002\u7136\u800c\uff0c\u5728\u66f4\u590d\u6742\u7684\u7a0b\u5e8f\u4e2d\uff0c\u51fd\u6570\u7684\u884c\u4e3a\u6216\u8fd4\u56de\u503c\u6ca1\u6709\u6253\u5370\u51fa\u6765\uff0c\u8fd9\u6837\u7684\u95ee\u9898\u5f88\u96be\u8c03\u8bd5\u3002 \u4e3a\u4e86\u5e2e\u52a9\u89e3\u51b3\u672c\u8be5\u88ab\u91cd\u5199\u4f46\u5374\u6ca1\u6709\u88ab\u91cd\u5199\u7684\u51fd\u6570\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u5c06 override \u4fee\u9970\u7b26\u653e\u7f6e\u5728const\u5c06\u8981\u653e\u7f6e\u7684\u4f4d\u7f6e\uff0c\u4ece\u800c\u5c06 override \u5e94\u7528\u4e8e\u4efb\u4f55\u865a\u51fd\u6570\u3002\u5982\u679c\u57fa\u7c7b\u51fd\u6570\u6ca1\u6709\u91cd\u5199(\u6216\u5e94\u7528\u4e8e\u975e\u865a\u51fd\u6570)\uff0c\u7f16\u8bd1\u5668\u5c06\u4f1a\u9519\u8bef\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include class A { public : virtual std :: string_view getName1 ( int x ) { return \"A\" ; } virtual std :: string_view getName2 ( int x ) { return \"A\" ; } virtual std :: string_view getName3 ( int x ) { return \"A\" ; } }; class B : public A { public : std :: string_view getName1 ( short int x ) override { return \"B\" ; } // compile error, function is not an override std :: string_view getName2 ( int x ) const override { return \"B\" ; } // compile error, function is not an override std :: string_view getName3 ( int x ) override { return \"B\" ; } // okay, function is an override of A::getName3(int) }; int main () { return 0 ; } \u4e0a\u9762\u7684\u7a0b\u5e8f\u4f1a\u62a5\u544a\u4e24\u4e2a\u7f16\u8bd1\u9519\u8bef\uff1a\u7b2c\u4e00\u4e2a\u662f B::getName1() \u76f8\u5173\u9519\u8bef\uff0c\u7b2c\u4e8c\u4e2a\u662f B::getName2() \u76f8\u5173\u9519\u8bef\u3002\u56e0\u5b83\u4eec\u90fd\u6ca1\u6709\u6210\u529f\u5730\u91cd\u5199\u57fa\u7c7b\u4e2d\u7684\u51fd\u6570\u3002 B::getName3() \u91cd\u5199\u6210\u529f\uff0c\u6240\u4ee5\u6ca1\u6709\u95ee\u9898\u3002 \u56e0\u4e3a\u4f7f\u7528 override \u8bf4\u660e\u7b26\u4e0d\u4f1a\u5f71\u54cd\u6027\u80fd\uff0c\u800c\u4e14\u5b83\u6709\u52a9\u4e8e\u786e\u4fdd\u91cd\u5199\u5b9e\u9645\u53d1\u751f\uff0c\u6240\u4ee5\u5e94\u8be5\u4f7f\u7528\u8be5\u4fee\u9970\u7b26\u6807\u8bb0\u6240\u6709\u865a\u51fd\u6570\u3002\u6b64\u5916\uff0c\u56e0\u4e3a override \u4fee\u9970\u7b26\u610f\u5473\u7740\u8be5\u51fd\u6570\u662f\u865a\u51fd\u6570\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u4f7f\u7528 virtual \u5173\u952e\u5b57\u91cd\u590d\u6807\u8bb0\u5df2\u7ecf\u88ab\u6807\u8bb0\u4e3a override \u7684\u51fd\u6570\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u57fa\u7c7b\u7684\u865a\u51fd\u6570\u4e2d\u4f7f\u7528 virtual \u5173\u952e\u5b57\u3002\u5728\u6d3e\u751f\u7c7b\u4e2d\u5bf9\u5e94\u7684\u51fd\u6570\u4e2d\u4f7f\u7528 override \u5173\u952e\u5b57\uff08\u65e0\u9700\u4f7f\u7528 virtual \u5173\u952e\u5b57\uff09\u3002 final \u4fee\u9970\u7b26 \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u4e0d\u5e0c\u671b\u6709\u4eba\u80fd\u591f\u91cd\u5199\u865a\u51fd\u6570\u6216\u4e0d\u5e0c\u671b\u7c7b\u88ab\u7ee7\u627f\u3002 final \u4fee\u9970\u7b26\u53ef\u4ee5\u7528\u6765\u544a\u8bc9\u7f16\u8bd1\u5668\u786e\u4fdd\u8fd9\u4e00\u70b9\u3002\u5982\u679c\u7528\u6237\u8bd5\u56fe\u91cd\u5199\u88ab\u6807\u8bb0\u4e3a final \u7684\u51fd\u6570\u6216\u7ee7\u627f\u5df2\u6307\u5b9a\u4e3a final \u7684\u7c7b\uff0c\u7f16\u8bd1\u5668\u5c06\u7ed9\u51fa\u7f16\u8bd1\u9519\u8bef\u3002 \u5728\u6211\u4eec\u60f3\u8981\u9650\u5236\u7528\u6237\u91cd\u5199\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c final \u4fee\u9970\u7b26\u7528\u5728 override \u4fee\u9970\u7b26\u7684\u76f8\u540c\u4f4d\u7f6e\uff0c\u5982\u4e0b\u6240\u793a: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include class A { public : virtual std :: string_view getName () { return \"A\" ; } }; class B : public A { public : // note use of final specifier on following line -- that makes this function no longer overridable std :: string_view getName () override final { return \"B\" ; } // okay, \u91cd\u5199 A::getName() }; class C : public B { public : std :: string_view getName () override { return \"C\" ; } // \u7f16\u8bd1\u9519\u8bef: \u8bd5\u56fe\u91cd\u5199\u88ab\u6807\u8bb0\u4e3a final \u7684 B::getName() }; \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c B::getName() \u91cd\u5199\u4e86 A::getName() \uff0c\u8fd9\u91cc\u6ca1\u6709\u4efb\u4f55\u95ee\u9898\u3002\u4f46\u662f\u7531\u4e8e B::getName() \u5177\u6709 final \u4fee\u9970\u7b26\uff0c\u6240\u4ee5\u4efb\u4f55\u5bf9\u5b83\u7684\u91cd\u5199\u90fd\u662f\u4e0d\u5141\u8bb8\u7684\uff0c\u4f1a\u88ab\u8ba4\u4e3a\u662f\u9519\u8bef\u3002 C::getName() \u5bf9 B::getName() (\u4e0e\u8fd9\u91cc\u7684 override \u8bf4\u660e\u7b26\u5e76\u4e0d\u76f8\u5173\uff0c\u8fd9\u91cc\u53ea\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\u7f62\u4e86) \u6765\u8bf4\u662f\u91cd\u5199\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002 \u5728\u6211\u4eec\u60f3\u8981\u9632\u6b62\u4ece\u4e00\u4e2a\u7c7b\u7ee7\u627f\u7684\u60c5\u51b5\u4e0b\uff0c final \u4fee\u9970\u7b26\u5e94\u7528\u5728\u7c7b\u540d\u4e4b\u540e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include class A { public : virtual std :: string_view getName () { return \"A\" ; } }; class B final : public A // note use of final specifier here { public : std :: string_view getName () override { return \"B\" ; } }; class C : public B // compile error: cannot inherit from final class { public : std :: string_view getName () override { return \"C\" ; } }; \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7c7bB\u88ab\u58f0\u660e\u4e3afinal\u3002\u56e0\u6b64\uff0c\u5f53C\u8bd5\u56fe\u4eceB\u7ee7\u627f\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u7ed9\u51fa\u4e00\u4e2a\u7f16\u8bd1\u9519\u8bef\u3002 \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b \u6709\u4e00\u79cd\u60c5\u51b5\u4e0b\uff0c\u6d3e\u751f\u7c7b\u4e2d\u91cd\u5199\u7684\u865a\u51fd\u6570\uff0c\u53ef\u4ee5\u548c\u57fa\u7c7b\u4e2d\u7684\u865a\u51fd\u6570\u5177\u6709\u4e0d\u540c\u7684\u8fd4\u56de\u503c\uff0c\u4f46\u4ecd\u7136\u88ab\u8ba4\u4e3a\u662f\u6709\u6548\u7684\u91cd\u5199\u3002 \u5982\u679c\u865a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f\u6307\u5411\u76f8\u540c\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u5176\u91cd\u5199\u51fd\u6570\u4e5f\u53ef\u4ee5\u8fd4\u56de\u6307\u5411\u6d3e\u751f\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528\u3002\u8fd9\u4e2a\u673a\u5236\u79f0\u4e3a \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b \u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include #include class Base { public : // This version of getThis() returns a pointer to a Base class virtual Base * getThis () { std :: cout << \"called Base::getThis() \\n \" ; return this ; } void printType () { std :: cout << \"returned a Base \\n \" ; } }; class Derived : public Base { public : // Normally override functions have to return objects of the same type as the base function // However, because Derived is derived from Base, it's okay to return Derived* instead of Base* Derived * getThis () override { std :: cout << \"called Derived::getThis() \\n \" ; return this ; } void printType () { std :: cout << \"returned a Derived \\n \" ; } }; int main () { Derived d {}; Base * b { & d }; d . getThis () -> printType (); // \u8c03\u7528 Derived::getThis(), \u8fd4\u56de Derived*, \u8c03\u7528 Derived::printType b -> getThis () -> printType (); // \u8c03\u7528 Derived::getThis(), \u8fd4\u56de Base*, \u8c03\u7528 Base::printType return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 4 called Derived::getThis() returned a Derived called Derived::getThis() returned a Base \u5173\u4e8e\u534f\u53d8\u8fd4\u56de\u7c7b\u578b\u6709\u4e00\u4e2a\u6709\u8da3\u7684\u6ce8\u610f\u4e8b\u9879\uff1aC++\u4e0d\u652f\u6301\u52a8\u6001\u7c7b\u578b\uff0c\u56e0\u6b64\u4f60\u603b\u662f\u5f97\u5230\u4e0e\u6240\u8c03\u7528\u51fd\u6570\u7684\u5b9e\u9645\u7248\u672c\u76f8\u5339\u914d\u7684\u7c7b\u8fd4\u56de\u503c\u578b\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u8c03\u7528 d.getThis() \u3002\u56e0\u4e3a d \u662f\u6d3e\u751f\u7684\uff0c\u6240\u4ee5\u8c03\u7528 Derived::getThis() \uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a Derived* \u3002\u7136\u540e\u8fd9\u4e2a Derived* \u88ab\u7528\u6765\u8c03\u7528\u975e\u865a\u51fd\u6570 Derived::printType() \u3002 \u6362\u8a00\u4e4b\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u4f60\u4f7f\u7528 Derived \u7c7b\u578b\u7684\u5bf9\u8c61\u8c03\u7528 getThis() \uff0c\u90a3\u4f60\u5f97\u5230\u7684\u5c31\u662f Derived* \u7c7b\u578b\u7684\u8fd4\u56de\u503c\u3002 \u5982\u679c printType() \u662f\u865a\u51fd\u6570\u7684\u8bdd\u60c5\u51b5\u5219\u4e0d\u540c\uff0c\u6b64\u65f6 b->getThis() ( Base* \u7684\u5bf9\u8c61) \u5c31\u4f1a\u8fdb\u884c\u865a\u51fd\u6570\u89e3\u6790\uff0c\u8c03\u7528 Derived::printType() \u3002 \u534f\u53d8\u8fd4\u56de\u7c7b\u578b\u901a\u5e38\u7528\u4e8e\u8fd4\u56de\u4e00\u4e2a\u6307\u9488\u6216\u5f15\u7528\uff0c\u8be5\u6307\u9488\u6216\u5f15\u7528\u6240\u6307\u7684\u5bf9\u8c61\uff0c\u5305\u542b\u8be5\u6210\u5458\u51fd\u6570(\u4f8b\u5982 Base::getThis() \u8fd4\u56de Base* \uff0c Derived::getThis() \u8fd4\u56de Derived* )\u7684\u60c5\u51b5\u3002\u7136\u800c\uff0c\u5e76\u4e0d\u662f\u5b83\u552f\u4e00\u7684\u7528\u6cd5\uff0c\u5f53 \u91cd\u5199 \u6210\u5458\u51fd\u6570\u6240\u5c5e\u7684\u7c7b\u578b\uff0c\u4e0e\u57fa\u7c7b\u865a\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u5c5e\u4e8e\u6d3e\u751f\u5173\u7cfb\u65f6\uff0c\u90fd\u53ef\u4ee5\u4f7f\u7528\u534f\u53d8\u8fd4\u56de\u7c7b\u578b\u3002","title":"18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b"},{"location":"18-3-the-override-and-final-specifiers-and-covariant-return-types/#override","text":"\u6b63\u5982\u6211\u4eec\u5728\u4e0a\u4e00\u8bfe\u4e2d\u63d0\u5230\u7684\uff0c \u6d3e\u751f\u7c7b\u865a\u51fd\u6570\u53ea\u6709\u5728\u5176\u7b7e\u540d\u548c\u8fd4\u56de\u7c7b\u578b\u5b8c\u5168\u5339\u914d\u7684\u60c5\u51b5\u4e0b\u624d\u88ab\u8ba4\u4e3a\u662f\u91cd\u5199 \u3002\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5bb9\u6613\u88ab\u5ffd\u7565\u7684\u95ee\u9898\uff0c\u5373\u6253\u7b97\u88ab\u91cd\u5199\u7684\u51fd\u6570\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\u88ab\u91cd\u5199\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include class A { public : virtual std :: string_view getName1 ( int x ) { return \"A\" ; } virtual std :: string_view getName2 ( int x ) { return \"A\" ; } }; class B : public A { public : virtual std :: string_view getName1 ( short int x ) { return \"B\" ; } // \u6ce8\u610f: \u53c2\u6570\u662f short int virtual std :: string_view getName2 ( int x ) const { return \"B\" ; } // \u6ce8\u610f: \u51fd\u6570\u662f const }; int main () { B b {}; A & rBase { b }; std :: cout << rBase . getName1 ( 1 ) << '\\n' ; std :: cout << rBase . getName2 ( 2 ) << '\\n' ; return 0 ; } \u56e0\u4e3a rBase \u662fB\u4e2dA\u90e8\u5206\u7684\u5f15\u7528\uff0c\u6240\u4ee5\u8fd9\u91cc\u7684\u672c\u610f\u662f\u4f7f\u7528\u865a\u51fd\u6570\u6765\u8bbf\u95ee B::getName1() \u548c B::getName2() \u3002\u4f46\u662f\uff0c\u7531\u4e8e B::getName1() \u7684\u53c2\u6570\u5e76\u4e0d\u540c\u6240\u4ee5\u6ca1\u6709\u88ab\u770b\u505a\u662f\u5bf9 A::getName1() \u7684\u91cd\u5199\u3002\u66f4\u9690\u853d\u7684\u662f\uff0c\u56e0\u4e3a B::getName2() \u662f const \u800c A::getName2() \u4e0d\u662f\uff0c\u6240\u4ee5 B::getName2() \u4e5f\u4e0d\u4f1a\u88ab\u770b\u505a\u662f A::getName2() \u7684\u91cd\u5199\u3002 \u56e0\u6b64\uff0c\u7a0b\u5e8f\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 A A \u5728\u672c\u4f8b\u4e2d\uff0c\u56e0\u4e3aA\u548cB\u53ea\u662f\u6253\u5370\u4e86\u5b83\u4eec\u5404\u81ea\u7684\u540d\u79f0\uff0c\u6240\u4ee5\u5f88\u5bb9\u6613\u53d1\u73b0\u9519\u8bef\uff0c\u5373\u56e0\u4e3a\u91cd\u5199\u6ca1\u6709\u751f\u6548\u800c\u8c03\u7528\u4e86\u9519\u8bef\u7684\u865a\u51fd\u6570\u3002\u7136\u800c\uff0c\u5728\u66f4\u590d\u6742\u7684\u7a0b\u5e8f\u4e2d\uff0c\u51fd\u6570\u7684\u884c\u4e3a\u6216\u8fd4\u56de\u503c\u6ca1\u6709\u6253\u5370\u51fa\u6765\uff0c\u8fd9\u6837\u7684\u95ee\u9898\u5f88\u96be\u8c03\u8bd5\u3002 \u4e3a\u4e86\u5e2e\u52a9\u89e3\u51b3\u672c\u8be5\u88ab\u91cd\u5199\u4f46\u5374\u6ca1\u6709\u88ab\u91cd\u5199\u7684\u51fd\u6570\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u5c06 override \u4fee\u9970\u7b26\u653e\u7f6e\u5728const\u5c06\u8981\u653e\u7f6e\u7684\u4f4d\u7f6e\uff0c\u4ece\u800c\u5c06 override \u5e94\u7528\u4e8e\u4efb\u4f55\u865a\u51fd\u6570\u3002\u5982\u679c\u57fa\u7c7b\u51fd\u6570\u6ca1\u6709\u91cd\u5199(\u6216\u5e94\u7528\u4e8e\u975e\u865a\u51fd\u6570)\uff0c\u7f16\u8bd1\u5668\u5c06\u4f1a\u9519\u8bef\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include class A { public : virtual std :: string_view getName1 ( int x ) { return \"A\" ; } virtual std :: string_view getName2 ( int x ) { return \"A\" ; } virtual std :: string_view getName3 ( int x ) { return \"A\" ; } }; class B : public A { public : std :: string_view getName1 ( short int x ) override { return \"B\" ; } // compile error, function is not an override std :: string_view getName2 ( int x ) const override { return \"B\" ; } // compile error, function is not an override std :: string_view getName3 ( int x ) override { return \"B\" ; } // okay, function is an override of A::getName3(int) }; int main () { return 0 ; } \u4e0a\u9762\u7684\u7a0b\u5e8f\u4f1a\u62a5\u544a\u4e24\u4e2a\u7f16\u8bd1\u9519\u8bef\uff1a\u7b2c\u4e00\u4e2a\u662f B::getName1() \u76f8\u5173\u9519\u8bef\uff0c\u7b2c\u4e8c\u4e2a\u662f B::getName2() \u76f8\u5173\u9519\u8bef\u3002\u56e0\u5b83\u4eec\u90fd\u6ca1\u6709\u6210\u529f\u5730\u91cd\u5199\u57fa\u7c7b\u4e2d\u7684\u51fd\u6570\u3002 B::getName3() \u91cd\u5199\u6210\u529f\uff0c\u6240\u4ee5\u6ca1\u6709\u95ee\u9898\u3002 \u56e0\u4e3a\u4f7f\u7528 override \u8bf4\u660e\u7b26\u4e0d\u4f1a\u5f71\u54cd\u6027\u80fd\uff0c\u800c\u4e14\u5b83\u6709\u52a9\u4e8e\u786e\u4fdd\u91cd\u5199\u5b9e\u9645\u53d1\u751f\uff0c\u6240\u4ee5\u5e94\u8be5\u4f7f\u7528\u8be5\u4fee\u9970\u7b26\u6807\u8bb0\u6240\u6709\u865a\u51fd\u6570\u3002\u6b64\u5916\uff0c\u56e0\u4e3a override \u4fee\u9970\u7b26\u610f\u5473\u7740\u8be5\u51fd\u6570\u662f\u865a\u51fd\u6570\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u4f7f\u7528 virtual \u5173\u952e\u5b57\u91cd\u590d\u6807\u8bb0\u5df2\u7ecf\u88ab\u6807\u8bb0\u4e3a override \u7684\u51fd\u6570\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u57fa\u7c7b\u7684\u865a\u51fd\u6570\u4e2d\u4f7f\u7528 virtual \u5173\u952e\u5b57\u3002\u5728\u6d3e\u751f\u7c7b\u4e2d\u5bf9\u5e94\u7684\u51fd\u6570\u4e2d\u4f7f\u7528 override \u5173\u952e\u5b57\uff08\u65e0\u9700\u4f7f\u7528 virtual \u5173\u952e\u5b57\uff09\u3002","title":"override \u4fee\u9970\u7b26"},{"location":"18-3-the-override-and-final-specifiers-and-covariant-return-types/#final","text":"\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u4e0d\u5e0c\u671b\u6709\u4eba\u80fd\u591f\u91cd\u5199\u865a\u51fd\u6570\u6216\u4e0d\u5e0c\u671b\u7c7b\u88ab\u7ee7\u627f\u3002 final \u4fee\u9970\u7b26\u53ef\u4ee5\u7528\u6765\u544a\u8bc9\u7f16\u8bd1\u5668\u786e\u4fdd\u8fd9\u4e00\u70b9\u3002\u5982\u679c\u7528\u6237\u8bd5\u56fe\u91cd\u5199\u88ab\u6807\u8bb0\u4e3a final \u7684\u51fd\u6570\u6216\u7ee7\u627f\u5df2\u6307\u5b9a\u4e3a final \u7684\u7c7b\uff0c\u7f16\u8bd1\u5668\u5c06\u7ed9\u51fa\u7f16\u8bd1\u9519\u8bef\u3002 \u5728\u6211\u4eec\u60f3\u8981\u9650\u5236\u7528\u6237\u91cd\u5199\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c final \u4fee\u9970\u7b26\u7528\u5728 override \u4fee\u9970\u7b26\u7684\u76f8\u540c\u4f4d\u7f6e\uff0c\u5982\u4e0b\u6240\u793a: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include class A { public : virtual std :: string_view getName () { return \"A\" ; } }; class B : public A { public : // note use of final specifier on following line -- that makes this function no longer overridable std :: string_view getName () override final { return \"B\" ; } // okay, \u91cd\u5199 A::getName() }; class C : public B { public : std :: string_view getName () override { return \"C\" ; } // \u7f16\u8bd1\u9519\u8bef: \u8bd5\u56fe\u91cd\u5199\u88ab\u6807\u8bb0\u4e3a final \u7684 B::getName() }; \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c B::getName() \u91cd\u5199\u4e86 A::getName() \uff0c\u8fd9\u91cc\u6ca1\u6709\u4efb\u4f55\u95ee\u9898\u3002\u4f46\u662f\u7531\u4e8e B::getName() \u5177\u6709 final \u4fee\u9970\u7b26\uff0c\u6240\u4ee5\u4efb\u4f55\u5bf9\u5b83\u7684\u91cd\u5199\u90fd\u662f\u4e0d\u5141\u8bb8\u7684\uff0c\u4f1a\u88ab\u8ba4\u4e3a\u662f\u9519\u8bef\u3002 C::getName() \u5bf9 B::getName() (\u4e0e\u8fd9\u91cc\u7684 override \u8bf4\u660e\u7b26\u5e76\u4e0d\u76f8\u5173\uff0c\u8fd9\u91cc\u53ea\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\u7f62\u4e86) \u6765\u8bf4\u662f\u91cd\u5199\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002 \u5728\u6211\u4eec\u60f3\u8981\u9632\u6b62\u4ece\u4e00\u4e2a\u7c7b\u7ee7\u627f\u7684\u60c5\u51b5\u4e0b\uff0c final \u4fee\u9970\u7b26\u5e94\u7528\u5728\u7c7b\u540d\u4e4b\u540e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include class A { public : virtual std :: string_view getName () { return \"A\" ; } }; class B final : public A // note use of final specifier here { public : std :: string_view getName () override { return \"B\" ; } }; class C : public B // compile error: cannot inherit from final class { public : std :: string_view getName () override { return \"C\" ; } }; \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7c7bB\u88ab\u58f0\u660e\u4e3afinal\u3002\u56e0\u6b64\uff0c\u5f53C\u8bd5\u56fe\u4eceB\u7ee7\u627f\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u7ed9\u51fa\u4e00\u4e2a\u7f16\u8bd1\u9519\u8bef\u3002","title":"final \u4fee\u9970\u7b26"},{"location":"18-3-the-override-and-final-specifiers-and-covariant-return-types/#_1","text":"\u6709\u4e00\u79cd\u60c5\u51b5\u4e0b\uff0c\u6d3e\u751f\u7c7b\u4e2d\u91cd\u5199\u7684\u865a\u51fd\u6570\uff0c\u53ef\u4ee5\u548c\u57fa\u7c7b\u4e2d\u7684\u865a\u51fd\u6570\u5177\u6709\u4e0d\u540c\u7684\u8fd4\u56de\u503c\uff0c\u4f46\u4ecd\u7136\u88ab\u8ba4\u4e3a\u662f\u6709\u6548\u7684\u91cd\u5199\u3002 \u5982\u679c\u865a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f\u6307\u5411\u76f8\u540c\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u5176\u91cd\u5199\u51fd\u6570\u4e5f\u53ef\u4ee5\u8fd4\u56de\u6307\u5411\u6d3e\u751f\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528\u3002\u8fd9\u4e2a\u673a\u5236\u79f0\u4e3a \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b \u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include #include class Base { public : // This version of getThis() returns a pointer to a Base class virtual Base * getThis () { std :: cout << \"called Base::getThis() \\n \" ; return this ; } void printType () { std :: cout << \"returned a Base \\n \" ; } }; class Derived : public Base { public : // Normally override functions have to return objects of the same type as the base function // However, because Derived is derived from Base, it's okay to return Derived* instead of Base* Derived * getThis () override { std :: cout << \"called Derived::getThis() \\n \" ; return this ; } void printType () { std :: cout << \"returned a Derived \\n \" ; } }; int main () { Derived d {}; Base * b { & d }; d . getThis () -> printType (); // \u8c03\u7528 Derived::getThis(), \u8fd4\u56de Derived*, \u8c03\u7528 Derived::printType b -> getThis () -> printType (); // \u8c03\u7528 Derived::getThis(), \u8fd4\u56de Base*, \u8c03\u7528 Base::printType return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 4 called Derived::getThis() returned a Derived called Derived::getThis() returned a Base \u5173\u4e8e\u534f\u53d8\u8fd4\u56de\u7c7b\u578b\u6709\u4e00\u4e2a\u6709\u8da3\u7684\u6ce8\u610f\u4e8b\u9879\uff1aC++\u4e0d\u652f\u6301\u52a8\u6001\u7c7b\u578b\uff0c\u56e0\u6b64\u4f60\u603b\u662f\u5f97\u5230\u4e0e\u6240\u8c03\u7528\u51fd\u6570\u7684\u5b9e\u9645\u7248\u672c\u76f8\u5339\u914d\u7684\u7c7b\u8fd4\u56de\u503c\u578b\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u8c03\u7528 d.getThis() \u3002\u56e0\u4e3a d \u662f\u6d3e\u751f\u7684\uff0c\u6240\u4ee5\u8c03\u7528 Derived::getThis() \uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a Derived* \u3002\u7136\u540e\u8fd9\u4e2a Derived* \u88ab\u7528\u6765\u8c03\u7528\u975e\u865a\u51fd\u6570 Derived::printType() \u3002 \u6362\u8a00\u4e4b\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u4f60\u4f7f\u7528 Derived \u7c7b\u578b\u7684\u5bf9\u8c61\u8c03\u7528 getThis() \uff0c\u90a3\u4f60\u5f97\u5230\u7684\u5c31\u662f Derived* \u7c7b\u578b\u7684\u8fd4\u56de\u503c\u3002 \u5982\u679c printType() \u662f\u865a\u51fd\u6570\u7684\u8bdd\u60c5\u51b5\u5219\u4e0d\u540c\uff0c\u6b64\u65f6 b->getThis() ( Base* \u7684\u5bf9\u8c61) \u5c31\u4f1a\u8fdb\u884c\u865a\u51fd\u6570\u89e3\u6790\uff0c\u8c03\u7528 Derived::printType() \u3002 \u534f\u53d8\u8fd4\u56de\u7c7b\u578b\u901a\u5e38\u7528\u4e8e\u8fd4\u56de\u4e00\u4e2a\u6307\u9488\u6216\u5f15\u7528\uff0c\u8be5\u6307\u9488\u6216\u5f15\u7528\u6240\u6307\u7684\u5bf9\u8c61\uff0c\u5305\u542b\u8be5\u6210\u5458\u51fd\u6570(\u4f8b\u5982 Base::getThis() \u8fd4\u56de Base* \uff0c Derived::getThis() \u8fd4\u56de Derived* )\u7684\u60c5\u51b5\u3002\u7136\u800c\uff0c\u5e76\u4e0d\u662f\u5b83\u552f\u4e00\u7684\u7528\u6cd5\uff0c\u5f53 \u91cd\u5199 \u6210\u5458\u51fd\u6570\u6240\u5c5e\u7684\u7c7b\u578b\uff0c\u4e0e\u57fa\u7c7b\u865a\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u5c5e\u4e8e\u6d3e\u751f\u5173\u7cfb\u65f6\uff0c\u90fd\u53ef\u4ee5\u4f7f\u7528\u534f\u53d8\u8fd4\u56de\u7c7b\u578b\u3002","title":"\u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b"},{"location":"18-4-virtual-destructors-virtual-assignment-and-overriding-virtualization/","tags":["virtual-destructors","virtual-assignment","overriding-virtualization"],"text":"Key Takeaway \u6790\u6784\u51fd\u6570\u7684\u8c03\u7528\u987a\u5e8f\u548c\u6784\u9020\u51fd\u6570\u76f8\u53cd\uff0c\u800c\u4e14\u662f\u5411\u57fa\u7c7b\u65b9\u5411\u9010\u7ea7\u8c03\u7528\u3002 \u57fa\u7c7b\u7684\u6790\u6784\u51fd\u6570\u5fc5\u987b\u662f \u865a\u51fd\u6570 \u3002\u5982\u679c\u4e0d\u8fd9\u6837\u505a\uff0c\u5f53\u57fa\u4e8e\u6d3e\u751f\u7c7b\u4e2d\u57fa\u7c7b\u90e8\u5206\u7684\u6307\u9488\u6216\u5f15\u7528\u65f6\u9500\u6bc1\u5bf9\u8c61\u65f6\uff0c\u8c03\u7528\u7684\u662f\u57fa\u7c7b\u7684\u6790\u6784\u51fd\u6570\uff0c\u5b50\u7c7b\u6210\u5458\u4e0d\u80fd\u88ab\u6b63\u786e\u9500\u6bc1\u3002 \u6240\u6709\u7684\u6790\u6784\u51fd\u6570\u90fd\u8bbe\u7f6e\u4e3a\u865a\u53ef\u4ee5\u907f\u514d\u4e0a\u8ff0\u95ee\u9898\uff0c\u4f46\u662f\u4f1a\u6709\u6027\u80fd\u635f\u5931\uff08\u65f6\u95f4\u548c\u7a7a\u95f4\uff0c\u6bcf\u4e2a\u7c7b\u5bf9\u8c61\u90fd\u591a\u4e86\u4e00\u4e2a\u6307\u9488\uff09 \u5982\u679c\u4e00\u4e2a\u7c7b\u5141\u8bb8\u88ab\u5176\u4ed6\u7c7b\u7ee7\u627f\uff0c\u786e\u4fdd\u5176\u6790\u6784\u51fd\u6570\u662f\u865a\u51fd\u6570\u3002 \u5982\u679c\u4e00\u4e2a\u7c7b \u4e0d\u5141\u8bb8 \u88ab\u5176\u4ed6\u7c7b\u7ee7\u627f\uff0c\u5c06\u5176\u6807\u8bb0\u4e3a final \u3002\u8fd9\u5c06\u4ece\u6839\u672c\u4e0a\u9632\u6b62\u5176\u4ed6\u7c7b\u5bf9\u5b83\u7684\u7ee7\u627f\uff0c\u800c\u4e0d\u4f1a\u5bf9\u7c7b\u672c\u8eab\u65bd\u52a0\u5176\u4ed6\u9650\u5236 \u865a\u6784\u6790\u6784\u51fd\u6570 \u5c3d\u7ba1 C++ \u53ef\u4ee5\u63d0\u4f9b\u9ed8\u8ba4\u7684\u6784\u9020\u51fd\u6570\uff0c\u4f46\u6211\u4eec\u65f6\u5e38\u4e5f\u4f1a\u60f3\u8981\u63d0\u4f9b\u81ea\u5b9a\u4e49\u7684\u6790\u6784\u51fd\u6570\uff08\u5c24\u5176\u662f\u5f53\u7c7b\u9700\u8981\u91ca\u653e\u5185\u5b58\u7684\u60c5\u51b5\uff09\u3002==\u5f53\u4e00\u4e2a\u7c7b\u6d89\u53ca\u5230 \u7ee7\u627f \u7684\u65f6\u5019\uff0c\u5176\u6790\u6784\u51fd\u6570\u5e94\u8be5\u603b\u662f\u4e3a \u865a\u6790\u6784\u51fd\u6570 \u3002==\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include class Base { public : ~ Base () // note: \u4e0d\u662f\u865a\u51fd\u6570 { std :: cout << \"Calling ~Base() \\n \" ; } }; class Derived : public Base { private : int * m_array ; public : Derived ( int length ) : m_array { new int [ length ] } { } ~ Derived () // note: \u4e0d\u662f virtual (\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u53d1\u51fa\u544a\u8b66) { std :: cout << \"Calling ~Derived() \\n \" ; delete [] m_array ; } }; int main () { Derived * derived { new Derived ( 5 ) }; Base * base { derived }; delete base ; //\u8c03\u7528\u57fa\u7c7b\u7684\u6790\u6784\u51fd\u6570\uff0c\u540c\u65f6\u56e0\u4e3a\u5b83\u4e0d\u662f\u865a\u51fd\u6570\uff0c\u5219\u5b50\u7c7b\u7684\u6790\u6784\u51fd\u6570\u4e0d\u4f1a\u88ab\u8c03\u7528\uff0c\u5b50\u7c7b\u6210\u5458\u65e0\u6cd5\u6790\u6784 return 0 ; } \u6ce8\u610f\uff1a\u5982\u679c\u4f60\u5728\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\u65f6\uff0c\u7f16\u8bd1\u5668\u8b66\u544a\u4f60\u4f7f\u7528\u4e86\u975e\u865a\u7684\u6790\u6784\u51fd\u6570\u3002\u4f60\u9700\u8981\u5173\u95ed\u7f16\u8bd1\u5668\u9009\u9879\uff1a\u5c06\u544a\u8b66\u5f53\u505a\u9519\u8bef\u5bf9\u5f85\u3002 \u56e0\u4e3a base \u662f\u4e00\u4e2a Base \u7c7b\u578b\u7684\u6307\u9488\uff0c\u5219\u5f53 base \u88ab\u5220\u9664\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u67e5\u770b Base \u7684\u6790\u6784\u51fd\u6570\u662f\u5426\u4e3a\u865a\u51fd\u6570\u3002\u5982\u679c\u4e0d\u662f\u7684\u8bdd\uff0c\u5b83\u4f1a\u8ba4\u4e3a\u4f60\u9700\u8981\u8c03\u7528\u7684\u5c31\u662f Base \u7684\u6790\u6784\u51fd\u6570\u3002 \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u80fd\u591f\u8bc1\u660e\u8fd9\u4e00\u70b9\uff1a 1 Calling ~Base() \u4f46\u662f\uff0c\u5b9e\u9645\u4e0a\u6211\u4eec\u5e0c\u671b\u80fd\u591f\u8c03\u7528 Derived \u7684\u6790\u6784\u51fd\u6570\uff08\u8fdb\u800c\u8c03\u7528 Base \u7684\u6790\u6784\u51fd\u6570)\uff0c\u5426\u5219 m_array \u662f\u6ca1\u529e\u6cd5\u88ab\u5220\u9664\u7684\u3002\u4e3a\u6b64\u6211\u4eec\u9700\u8981\u5c06 Base \u7684\u6790\u6784\u51fd\u6570\u8bbe\u7f6e\u4e3a\u865a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include class Base { public : virtual ~ Base () // note: virtual { std :: cout << \"Calling ~Base() \\n \" ; } }; class Derived : public Base { private : int * m_array ; public : Derived ( int length ) : m_array { new int [ length ] } { } virtual ~ Derived () // note: virtual { std :: cout << \"Calling ~Derived() \\n \" ; delete [] m_array ; } }; int main () { Derived * derived { new Derived ( 5 ) }; Base * base { derived }; delete base ; return 0 ; } \u6b64\u65f6\uff0c\u7a0b\u5e8f\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Calling ~Derived() Calling ~Base() \u6cd5\u5219 \u5728\u5904\u7406\u6d89\u53ca\u7ee7\u627f\u7684\u7c7b\u65f6\uff0c\u5176\u6790\u6784\u51fd\u6570\u5fc5\u987b\u663e\u5f0f\u5b9a\u4e49\u4e3a\u865a\u51fd\u6570 \u548c\u666e\u901a\u7684\u865a\u51fd\u6570\u4e00\u6837\uff0c\u5982\u679c\u57fa\u7c7b\u7684\u51fd\u6570\u662f\u865a\u51fd\u6570\uff0c\u5176\u6d3e\u751f\u7c7b\u4e2d\u6240\u6709 \u91cd\u5199 \u51fd\u6570\u90fd\u88ab\u8ba4\u4e3a\u662f\u865a\u51fd\u6570\uff0c\u4e0d\u7ba1\u6709\u4e48\u6709\u6807\u8bb0\u4e3a virtual \u3002\u6240\u4ee5\u6ca1\u6709\u5fc5\u8981\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u7684\u6d3e\u751f\u7c7b\u6790\u6784\u51fd\u6570\u5e76\u5c06\u5176\u6807\u8bb0\u4e3a virtual \u3002 \u6ce8\u610f\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u57fa\u7c7b\u7684\u865a\u6784\u9020\u51fd\u6570\u662f\u7a7a\u7684\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9a\u4e49\uff1a 1 virtual ~ Base () = default ; // generate a virtual default destructor \u865a\u8d4b\u503c \u53ef\u4ee5\u5c06\u8d4b\u503c\u64cd\u4f5c\u7b26\u8bbe\u4e3a virtual \u3002\u7136\u800c\uff0c\u4e0e\u6790\u6784\u51fd\u6570\u7684\u60c5\u51b5(\u865a\u62df\u5316\u603b\u662f\u4e00\u4e2a\u597d\u4e3b\u610f)\u4e0d\u540c\uff0c\u865a\u62df\u5316\u8d4b\u503c\u64cd\u4f5c\u7b26\u4f1a\u5e26\u6765\u4e00\u5927\u5806\u9ebb\u70e6\uff0c\u5e76\u6d89\u53ca\u672c\u6559\u7a0b\u8303\u56f4\u4e4b\u5916\u7684\u4e00\u4e9b\u9ad8\u7ea7\u4e3b\u9898\u3002\u56e0\u6b64\uff0c\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u6211\u4eec\u5efa\u8bae\u4f60\u6682\u65f6\u4e0d\u8981\u4f7f\u7528\u865a\u8d4b\u503c\u3002 \u5ffd\u7565\u865a\u5316 \u6781\u5c11\u6570\u60c5\u51b5\u4e0b\u6211\u4eec\u9700\u8981\u5ffd\u7565\u51fd\u6570\u7684\u865a\u5316\uff0c\u4f8b\u5982\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class Base { public : virtual ~ Base () = default ; virtual const char * getName () const { return \"Base\" ; } }; class Derived : public Base { public : virtual const char * getName () const { return \"Derived\" ; } }; \u53ef\u80fd\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u9700\u8981\u6307\u5411 Derived \u5bf9\u8c61\u7684 Base \u6307\u9488\u80fd\u591f\u8c03\u7528 Base::getName() \u800c\u4e0d\u662f Derived::getName() \u3002\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { Derived derived ; const Base & base { derived }; // Calls Base::getName() instead of the virtualized Derived::getName() std :: cout << base . Base :: getName () << '\\n' ; return 0 ; } \u8fd9\u4e2a\u64cd\u4f5c\u5e76\u4e0d\u5e38\u7528\uff0c\u4f46\u662f\u77e5\u9053\u603b\u6bd4\u4e0d\u77e5\u9053\u597d\u3002 \u5e94\u8be5\u5c06\u6240\u6709\u7684\u6790\u6784\u51fd\u6570\u90fd\u8bbe\u7f6e\u4e3a\u865a\u51fd\u6570\u5417\uff1f \u8fd9\u662f\u65b0\u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u95ee\u7684\u95ee\u9898\u3002\u5982\u4e0a\u9762\u7684\u4f8b\u5b50\u6240\u8ff0\uff0c\u5982\u679c\u57fa\u7c7b\u6790\u6784\u51fd\u6570\u6ca1\u6709\u88ab\u6807\u8bb0\u4e3a\u865a\u51fd\u6570\uff0c\u90a3\u4e48\u5982\u679c\u7a0b\u5e8f\u5458\u7a0d\u540e\u5220\u9664\u6307\u5411\u6d3e\u751f\u5bf9\u8c61\u7684\u57fa\u7c7b\u6307\u9488\uff0c\u5219\u7a0b\u5e8f\u6709\u5185\u5b58\u6cc4\u6f0f\u7684\u98ce\u9669\u3002\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u5c06\u6240\u6709\u6790\u6784\u51fd\u6570\u6807\u8bb0\u4e3a\u865a\u51fd\u6570\u3002\u4f46\u662f\u6211\u4eec\u771f\u7684\u9700\u8981\u8fd9\u4e48\u505a\u5417\uff1f \u8bf4\u201c\u662f\u201d\u5f88\u5bb9\u6613\uff0c\u8fd9\u6837\u4ee5\u540e\u5c31\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55\u7c7b\u4f5c\u4e3a\u57fa\u7c7b\u4e86\u2014\u2014\u4f46\u662f\u8fd9\u6837\u505a\u4f1a\u6709\u6027\u80fd\u635f\u5931(\u5411\u7c7b\u7684\u6bcf\u4e2a\u5b9e\u4f8b\u6dfb\u52a0\u4e00\u4e2a\u865a\u62df\u6307\u9488)\u3002\u6240\u4ee5\u4f60\u5fc5\u987b\u6743\u8861\u8f7b\u91cd\uff0c\u5c24\u5176\u662f\u5b83\u662f\u5426\u7b26\u5408\u4f60\u7684\u610f\u56fe\u3002 \u8457\u540d\u7684C++\u5927\u5e08Herb Sutter\u63d0\u51fa\u4e86\u4e00\u79cd\u80fd\u591f\u907f\u514d\u7531\u975e\u865a\u6790\u6784\u51fd\u6570\u5bfc\u81f4\u7684\u5185\u5b58\u6cc4\u6f0f\u7684\u65b9\u6cd5\uff1a\u201c\u57fa\u7c7b\u6790\u6784\u51fd\u6570\u5e94\u8be5\u662f \u516c\u5171\u7684\u865a\u6790\u6784\u51fd\u6570 \uff0c\u6216\u8005\u662f \u53d7\u4fdd\u62a4\u7684\u975e\u865a\u6790\u6784\u51fd\u6570 \u3002\u201d \u6709\u53d7\u4fdd\u62a4\u6790\u6784\u51fd\u6570\u7684\u7c7b\u4e0d\u80fd\u901a\u8fc7\u6307\u9488\u5220\u9664\uff0c\u56e0\u6b64\uff0c\u5f53\u57fa\u7c7b\u5177\u6709\u975e\u865a\u6790\u6784\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u9632\u6b62\u901a\u8fc7\u57fa\u6307\u9488\u610f\u5916\u5220\u9664\u6d3e\u751f\u7c7b\u3002\u4e0d\u5e78\u7684\u662f\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u57fa\u7c7b\u4e0d\u80fd\u901a\u8fc7\u57fa\u7c7b\u6307\u9488\u5220\u9664\uff0c\u8fd9\u5b9e\u9645\u4e0a\u610f\u5473\u7740\u7c7b\u53ea\u80fd\u7531\u6d3e\u751f\u7c7b\u52a8\u6001\u5206\u914d\u6216\u5220\u9664\u3002\u8fd9\u4e5f\u4f7f\u5f97\u8fd9\u4e9b\u7c7b\u4e0d\u80fd\u4f7f\u7528\u667a\u80fd\u6307\u9488(\u4f8b\u5982 std::unique_ptr \u548c std::shared_ptr )\uff0c\u4ece\u800c\u9650\u5236\u4e86\u8be5\u89c4\u5219\u7684\u6709\u7528\u6027(\u6211\u4eec\u5c06\u5728\u540e\u9762\u7684\u7ae0\u8282\u8ba8\u8bba\u667a\u80fd\u6307\u9488)\u3002\u8fd9\u4e5f\u610f\u5473\u7740\u8fd9\u6837\u7684\u57fa\u7c7b\u4e0d\u80fd\u88ab\u5206\u914d\u5728\u6808\u4e0a\u3002\u4ee3\u4ef7\u6709\u70b9\u5927\uff01 \u65e2\u7136\u5df2\u7ecf\u5728\u8bed\u8a00\u4e2d\u5f15\u5165\u4e86 final \u4fee\u9970\u7b26\uff0c\u6211\u4eec\u7684\u5efa\u8bae\u5982\u4e0b: \u5982\u679c\u4e00\u4e2a\u7c7b\u5141\u8bb8\u88ab\u5176\u4ed6\u7c7b\u7ee7\u627f\uff0c\u786e\u4fdd\u5176\u6790\u6784\u51fd\u6570\u662f\u865a\u51fd\u6570\u3002 \u5982\u679c\u4e00\u4e2a\u7c7b \u4e0d\u5141\u8bb8 \u88ab\u5176\u4ed6\u7c7b\u7ee7\u627f\uff0c\u5c06\u5176\u6807\u8bb0\u4e3a final \u3002\u8fd9\u5c06\u4ece\u6839\u672c\u4e0a\u9632\u6b62\u5176\u4ed6\u7c7b\u5bf9\u5b83\u7684\u7ee7\u627f\uff0c\u800c\u4e0d\u4f1a\u5bf9\u7c7b\u672c\u8eab\u65bd\u52a0\u5176\u4ed6\u9650\u5236","title":"18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316"},{"location":"18-4-virtual-destructors-virtual-assignment-and-overriding-virtualization/#_1","text":"\u5c3d\u7ba1 C++ \u53ef\u4ee5\u63d0\u4f9b\u9ed8\u8ba4\u7684\u6784\u9020\u51fd\u6570\uff0c\u4f46\u6211\u4eec\u65f6\u5e38\u4e5f\u4f1a\u60f3\u8981\u63d0\u4f9b\u81ea\u5b9a\u4e49\u7684\u6790\u6784\u51fd\u6570\uff08\u5c24\u5176\u662f\u5f53\u7c7b\u9700\u8981\u91ca\u653e\u5185\u5b58\u7684\u60c5\u51b5\uff09\u3002==\u5f53\u4e00\u4e2a\u7c7b\u6d89\u53ca\u5230 \u7ee7\u627f \u7684\u65f6\u5019\uff0c\u5176\u6790\u6784\u51fd\u6570\u5e94\u8be5\u603b\u662f\u4e3a \u865a\u6790\u6784\u51fd\u6570 \u3002==\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include class Base { public : ~ Base () // note: \u4e0d\u662f\u865a\u51fd\u6570 { std :: cout << \"Calling ~Base() \\n \" ; } }; class Derived : public Base { private : int * m_array ; public : Derived ( int length ) : m_array { new int [ length ] } { } ~ Derived () // note: \u4e0d\u662f virtual (\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u53d1\u51fa\u544a\u8b66) { std :: cout << \"Calling ~Derived() \\n \" ; delete [] m_array ; } }; int main () { Derived * derived { new Derived ( 5 ) }; Base * base { derived }; delete base ; //\u8c03\u7528\u57fa\u7c7b\u7684\u6790\u6784\u51fd\u6570\uff0c\u540c\u65f6\u56e0\u4e3a\u5b83\u4e0d\u662f\u865a\u51fd\u6570\uff0c\u5219\u5b50\u7c7b\u7684\u6790\u6784\u51fd\u6570\u4e0d\u4f1a\u88ab\u8c03\u7528\uff0c\u5b50\u7c7b\u6210\u5458\u65e0\u6cd5\u6790\u6784 return 0 ; } \u6ce8\u610f\uff1a\u5982\u679c\u4f60\u5728\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\u65f6\uff0c\u7f16\u8bd1\u5668\u8b66\u544a\u4f60\u4f7f\u7528\u4e86\u975e\u865a\u7684\u6790\u6784\u51fd\u6570\u3002\u4f60\u9700\u8981\u5173\u95ed\u7f16\u8bd1\u5668\u9009\u9879\uff1a\u5c06\u544a\u8b66\u5f53\u505a\u9519\u8bef\u5bf9\u5f85\u3002 \u56e0\u4e3a base \u662f\u4e00\u4e2a Base \u7c7b\u578b\u7684\u6307\u9488\uff0c\u5219\u5f53 base \u88ab\u5220\u9664\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u67e5\u770b Base \u7684\u6790\u6784\u51fd\u6570\u662f\u5426\u4e3a\u865a\u51fd\u6570\u3002\u5982\u679c\u4e0d\u662f\u7684\u8bdd\uff0c\u5b83\u4f1a\u8ba4\u4e3a\u4f60\u9700\u8981\u8c03\u7528\u7684\u5c31\u662f Base \u7684\u6790\u6784\u51fd\u6570\u3002 \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u80fd\u591f\u8bc1\u660e\u8fd9\u4e00\u70b9\uff1a 1 Calling ~Base() \u4f46\u662f\uff0c\u5b9e\u9645\u4e0a\u6211\u4eec\u5e0c\u671b\u80fd\u591f\u8c03\u7528 Derived \u7684\u6790\u6784\u51fd\u6570\uff08\u8fdb\u800c\u8c03\u7528 Base \u7684\u6790\u6784\u51fd\u6570)\uff0c\u5426\u5219 m_array \u662f\u6ca1\u529e\u6cd5\u88ab\u5220\u9664\u7684\u3002\u4e3a\u6b64\u6211\u4eec\u9700\u8981\u5c06 Base \u7684\u6790\u6784\u51fd\u6570\u8bbe\u7f6e\u4e3a\u865a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include class Base { public : virtual ~ Base () // note: virtual { std :: cout << \"Calling ~Base() \\n \" ; } }; class Derived : public Base { private : int * m_array ; public : Derived ( int length ) : m_array { new int [ length ] } { } virtual ~ Derived () // note: virtual { std :: cout << \"Calling ~Derived() \\n \" ; delete [] m_array ; } }; int main () { Derived * derived { new Derived ( 5 ) }; Base * base { derived }; delete base ; return 0 ; } \u6b64\u65f6\uff0c\u7a0b\u5e8f\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Calling ~Derived() Calling ~Base() \u6cd5\u5219 \u5728\u5904\u7406\u6d89\u53ca\u7ee7\u627f\u7684\u7c7b\u65f6\uff0c\u5176\u6790\u6784\u51fd\u6570\u5fc5\u987b\u663e\u5f0f\u5b9a\u4e49\u4e3a\u865a\u51fd\u6570 \u548c\u666e\u901a\u7684\u865a\u51fd\u6570\u4e00\u6837\uff0c\u5982\u679c\u57fa\u7c7b\u7684\u51fd\u6570\u662f\u865a\u51fd\u6570\uff0c\u5176\u6d3e\u751f\u7c7b\u4e2d\u6240\u6709 \u91cd\u5199 \u51fd\u6570\u90fd\u88ab\u8ba4\u4e3a\u662f\u865a\u51fd\u6570\uff0c\u4e0d\u7ba1\u6709\u4e48\u6709\u6807\u8bb0\u4e3a virtual \u3002\u6240\u4ee5\u6ca1\u6709\u5fc5\u8981\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u7684\u6d3e\u751f\u7c7b\u6790\u6784\u51fd\u6570\u5e76\u5c06\u5176\u6807\u8bb0\u4e3a virtual \u3002 \u6ce8\u610f\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u57fa\u7c7b\u7684\u865a\u6784\u9020\u51fd\u6570\u662f\u7a7a\u7684\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9a\u4e49\uff1a 1 virtual ~ Base () = default ; // generate a virtual default destructor","title":"\u865a\u6784\u6790\u6784\u51fd\u6570"},{"location":"18-4-virtual-destructors-virtual-assignment-and-overriding-virtualization/#_2","text":"\u53ef\u4ee5\u5c06\u8d4b\u503c\u64cd\u4f5c\u7b26\u8bbe\u4e3a virtual \u3002\u7136\u800c\uff0c\u4e0e\u6790\u6784\u51fd\u6570\u7684\u60c5\u51b5(\u865a\u62df\u5316\u603b\u662f\u4e00\u4e2a\u597d\u4e3b\u610f)\u4e0d\u540c\uff0c\u865a\u62df\u5316\u8d4b\u503c\u64cd\u4f5c\u7b26\u4f1a\u5e26\u6765\u4e00\u5927\u5806\u9ebb\u70e6\uff0c\u5e76\u6d89\u53ca\u672c\u6559\u7a0b\u8303\u56f4\u4e4b\u5916\u7684\u4e00\u4e9b\u9ad8\u7ea7\u4e3b\u9898\u3002\u56e0\u6b64\uff0c\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u6211\u4eec\u5efa\u8bae\u4f60\u6682\u65f6\u4e0d\u8981\u4f7f\u7528\u865a\u8d4b\u503c\u3002","title":"\u865a\u8d4b\u503c"},{"location":"18-4-virtual-destructors-virtual-assignment-and-overriding-virtualization/#_3","text":"\u6781\u5c11\u6570\u60c5\u51b5\u4e0b\u6211\u4eec\u9700\u8981\u5ffd\u7565\u51fd\u6570\u7684\u865a\u5316\uff0c\u4f8b\u5982\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class Base { public : virtual ~ Base () = default ; virtual const char * getName () const { return \"Base\" ; } }; class Derived : public Base { public : virtual const char * getName () const { return \"Derived\" ; } }; \u53ef\u80fd\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u9700\u8981\u6307\u5411 Derived \u5bf9\u8c61\u7684 Base \u6307\u9488\u80fd\u591f\u8c03\u7528 Base::getName() \u800c\u4e0d\u662f Derived::getName() \u3002\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { Derived derived ; const Base & base { derived }; // Calls Base::getName() instead of the virtualized Derived::getName() std :: cout << base . Base :: getName () << '\\n' ; return 0 ; } \u8fd9\u4e2a\u64cd\u4f5c\u5e76\u4e0d\u5e38\u7528\uff0c\u4f46\u662f\u77e5\u9053\u603b\u6bd4\u4e0d\u77e5\u9053\u597d\u3002","title":"\u5ffd\u7565\u865a\u5316"},{"location":"18-4-virtual-destructors-virtual-assignment-and-overriding-virtualization/#_4","text":"\u8fd9\u662f\u65b0\u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u95ee\u7684\u95ee\u9898\u3002\u5982\u4e0a\u9762\u7684\u4f8b\u5b50\u6240\u8ff0\uff0c\u5982\u679c\u57fa\u7c7b\u6790\u6784\u51fd\u6570\u6ca1\u6709\u88ab\u6807\u8bb0\u4e3a\u865a\u51fd\u6570\uff0c\u90a3\u4e48\u5982\u679c\u7a0b\u5e8f\u5458\u7a0d\u540e\u5220\u9664\u6307\u5411\u6d3e\u751f\u5bf9\u8c61\u7684\u57fa\u7c7b\u6307\u9488\uff0c\u5219\u7a0b\u5e8f\u6709\u5185\u5b58\u6cc4\u6f0f\u7684\u98ce\u9669\u3002\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u5c06\u6240\u6709\u6790\u6784\u51fd\u6570\u6807\u8bb0\u4e3a\u865a\u51fd\u6570\u3002\u4f46\u662f\u6211\u4eec\u771f\u7684\u9700\u8981\u8fd9\u4e48\u505a\u5417\uff1f \u8bf4\u201c\u662f\u201d\u5f88\u5bb9\u6613\uff0c\u8fd9\u6837\u4ee5\u540e\u5c31\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55\u7c7b\u4f5c\u4e3a\u57fa\u7c7b\u4e86\u2014\u2014\u4f46\u662f\u8fd9\u6837\u505a\u4f1a\u6709\u6027\u80fd\u635f\u5931(\u5411\u7c7b\u7684\u6bcf\u4e2a\u5b9e\u4f8b\u6dfb\u52a0\u4e00\u4e2a\u865a\u62df\u6307\u9488)\u3002\u6240\u4ee5\u4f60\u5fc5\u987b\u6743\u8861\u8f7b\u91cd\uff0c\u5c24\u5176\u662f\u5b83\u662f\u5426\u7b26\u5408\u4f60\u7684\u610f\u56fe\u3002 \u8457\u540d\u7684C++\u5927\u5e08Herb Sutter\u63d0\u51fa\u4e86\u4e00\u79cd\u80fd\u591f\u907f\u514d\u7531\u975e\u865a\u6790\u6784\u51fd\u6570\u5bfc\u81f4\u7684\u5185\u5b58\u6cc4\u6f0f\u7684\u65b9\u6cd5\uff1a\u201c\u57fa\u7c7b\u6790\u6784\u51fd\u6570\u5e94\u8be5\u662f \u516c\u5171\u7684\u865a\u6790\u6784\u51fd\u6570 \uff0c\u6216\u8005\u662f \u53d7\u4fdd\u62a4\u7684\u975e\u865a\u6790\u6784\u51fd\u6570 \u3002\u201d \u6709\u53d7\u4fdd\u62a4\u6790\u6784\u51fd\u6570\u7684\u7c7b\u4e0d\u80fd\u901a\u8fc7\u6307\u9488\u5220\u9664\uff0c\u56e0\u6b64\uff0c\u5f53\u57fa\u7c7b\u5177\u6709\u975e\u865a\u6790\u6784\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u9632\u6b62\u901a\u8fc7\u57fa\u6307\u9488\u610f\u5916\u5220\u9664\u6d3e\u751f\u7c7b\u3002\u4e0d\u5e78\u7684\u662f\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u57fa\u7c7b\u4e0d\u80fd\u901a\u8fc7\u57fa\u7c7b\u6307\u9488\u5220\u9664\uff0c\u8fd9\u5b9e\u9645\u4e0a\u610f\u5473\u7740\u7c7b\u53ea\u80fd\u7531\u6d3e\u751f\u7c7b\u52a8\u6001\u5206\u914d\u6216\u5220\u9664\u3002\u8fd9\u4e5f\u4f7f\u5f97\u8fd9\u4e9b\u7c7b\u4e0d\u80fd\u4f7f\u7528\u667a\u80fd\u6307\u9488(\u4f8b\u5982 std::unique_ptr \u548c std::shared_ptr )\uff0c\u4ece\u800c\u9650\u5236\u4e86\u8be5\u89c4\u5219\u7684\u6709\u7528\u6027(\u6211\u4eec\u5c06\u5728\u540e\u9762\u7684\u7ae0\u8282\u8ba8\u8bba\u667a\u80fd\u6307\u9488)\u3002\u8fd9\u4e5f\u610f\u5473\u7740\u8fd9\u6837\u7684\u57fa\u7c7b\u4e0d\u80fd\u88ab\u5206\u914d\u5728\u6808\u4e0a\u3002\u4ee3\u4ef7\u6709\u70b9\u5927\uff01 \u65e2\u7136\u5df2\u7ecf\u5728\u8bed\u8a00\u4e2d\u5f15\u5165\u4e86 final \u4fee\u9970\u7b26\uff0c\u6211\u4eec\u7684\u5efa\u8bae\u5982\u4e0b: \u5982\u679c\u4e00\u4e2a\u7c7b\u5141\u8bb8\u88ab\u5176\u4ed6\u7c7b\u7ee7\u627f\uff0c\u786e\u4fdd\u5176\u6790\u6784\u51fd\u6570\u662f\u865a\u51fd\u6570\u3002 \u5982\u679c\u4e00\u4e2a\u7c7b \u4e0d\u5141\u8bb8 \u88ab\u5176\u4ed6\u7c7b\u7ee7\u627f\uff0c\u5c06\u5176\u6807\u8bb0\u4e3a final \u3002\u8fd9\u5c06\u4ece\u6839\u672c\u4e0a\u9632\u6b62\u5176\u4ed6\u7c7b\u5bf9\u5b83\u7684\u7ee7\u627f\uff0c\u800c\u4e0d\u4f1a\u5bf9\u7c7b\u672c\u8eab\u65bd\u52a0\u5176\u4ed6\u9650\u5236","title":"\u5e94\u8be5\u5c06\u6240\u6709\u7684\u6790\u6784\u51fd\u6570\u90fd\u8bbe\u7f6e\u4e3a\u865a\u51fd\u6570\u5417\uff1f"},{"location":"18-5-early-binding-and-late-binding/","tags":["early-binding","late-binding"],"text":"Key Takeaway \u5728\u672c\u8282\u8bfe\u548c\u4e0b\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ed4\u7ec6\u7814\u7a76 \u865a\u51fd\u6570 \u7684\u5b9e\u73b0\u65b9\u5f0f\u3002\u5c3d\u7ba1\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4e8e\u4f60\u9ad8\u6548\u5730\u4f7f\u7528\u865a\u51fd\u6570\u6765\u8bf4\u5e76\u4e0d\u662f\u5fc5\u987b\u7684\uff0c\u4f46\u662f\u5b83\u771f\u7684\u5f88\u6709\u610f\u601d\u3002\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u628a\u8fd9\u4e9b\u5185\u5bb9\u5f53\u505a\u9009\u4fee\u8bfe\u3002 \u5f53C++\u7a0b\u5e8f\u88ab\u6267\u884c\u65f6\uff0c\u5b83\u662f\u6309\u987a\u5e8f\u6267\u884c\u7684\uff0c\u4ece main() \u7684\u9876\u90e8\u5f00\u59cb\u3002\u5f53\u9047\u5230\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u6267\u884c\u70b9\u8df3\u8f6c\u5230\u88ab\u8c03\u7528\u51fd\u6570\u7684\u5f00\u59cb\u3002CPU\u662f\u600e\u4e48\u77e5\u9053\u8981\u8fd9\u4e48\u505a\u7684? \u7f16\u8bd1\u7a0b\u5e8f\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06C++\u7a0b\u5e8f\u4e2d\u7684\u6bcf\u4e2a\u8bed\u53e5\u8f6c\u6362\u4e3a\u4e00\u884c\u6216\u591a\u884c\u673a\u5668\u8bed\u8a00\u3002\u673a\u5668\u8bed\u8a00\u7684\u6bcf\u4e00\u884c\u90fd\u6709\u81ea\u5df1\u552f\u4e00\u7684\u987a\u5e8f\u5730\u5740\u3002\u51fd\u6570\u4e5f\u4e0d\u4f8b\u5916\u2014\u2014\u5f53\u9047\u5230\u4e00\u4e2a\u51fd\u6570\u65f6\uff0c\u5b83\u88ab\u8f6c\u6362\u6210\u673a\u5668\u8bed\u8a00\u5e76\u7ed9\u51fa\u4e0b\u4e00\u4e2a\u53ef\u7528\u5730\u5740\u3002\u56e0\u6b64\uff0c\u6bcf\u4e2a\u51fd\u6570\u6700\u7ec8\u90fd\u6709\u4e00\u4e2a\u552f\u4e00\u7684\u5730\u5740\u3002 \u7ed1\u5b9a \u6307\u5b9a\u5c31\u662f\u5c06 \u6807\u8bc6\u7b26 \uff08\u53d8\u91cf\u540d\u6216\u51fd\u6570\u540d\uff09\u8f6c\u6362\u4e3a\u5730\u5740\u7684\u8fc7\u7a0b\u3002\u867d\u7136\u7ed1\u5b9a\u9002\u7528\u4e8e\u53d8\u91cf\u548c\u51fd\u6570\uff0c\u4f46\u662f\u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u91cd\u70b9\u8ba8\u8bba\u51fd\u6570\u7684\u7ed1\u5b9a\u3002 \u65e9\u7ed1\u5b9a Early binding \u7f16\u8bd1\u5668\u9047\u5230\u7684\u5927\u591a\u6570\u51fd\u6570\u8c03\u7528\u90fd\u662f\u51fd\u6570\u76f4\u63a5\u8c03\u7528\u3002\u76f4\u63a5\u8c03\u7528\u662f\u76f4\u63a5\u8c03\u7528\u51fd\u6570\u7684\u8bed\u53e5\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 #include void printValue ( int value ) { std :: cout << value ; } int main () { printValue ( 5 ); // This is a direct function call return 0 ; } \u76f4\u63a5\u51fd\u6570\u8c03\u7528\u53ef\u4ee5\u4f7f\u7528\u79f0\u4e3a\u65e9\u671f\u7ed1\u5b9a\u7684\u8fc7\u7a0b\u6765\u89e3\u51b3\u3002 \u65e9\u671f\u7ed1\u5b9a (\u4e5f\u79f0\u4e3a\u9759\u6001\u7ed1\u5b9a)\u610f\u5473\u7740\u7f16\u8bd1\u5668(\u6216\u94fe\u63a5\u5668)\u80fd\u591f\u76f4\u63a5\u5c06\u6807\u8bc6\u7b26\u540d(\u5982\u51fd\u6570\u540d\u6216\u53d8\u91cf\u540d)\u4e0e\u673a\u5668\u5730\u5740\u5173\u8054\u8d77\u6765 \u3002\u8bb0\u4f4f\uff0c\u6240\u6709\u51fd\u6570\u90fd\u6709\u552f\u4e00\u7684\u5730\u5740\u3002\u56e0\u6b64\uff0c\u5f53\u7f16\u8bd1\u5668(\u6216\u94fe\u63a5\u5668)\u9047\u5230\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u5b83\u4f1a\u7528\u4e00\u4e2a\u673a\u5668\u8bed\u8a00\u6307\u4ee4\u66ff\u6362\u51fd\u6570\u8c03\u7528\uff0c\u8be5\u6307\u4ee4\u544a\u8bc9CPU\u8df3\u8f6c\u5230\u51fd\u6570\u7684\u5730\u5740\u3002 \u8ba9\u6211\u4eec\u6765\u770b\u770b\u4e00\u4e2a\u4f7f\u7528\u65e9\u671f\u7ed1\u5b9a\u7684\u7b80\u5355\u8ba1\u7b97\u5668\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include int add ( int x , int y ) { return x + y ; } int subtract ( int x , int y ) { return x - y ; } int multiply ( int x , int y ) { return x * y ; } int main () { int x {}; std :: cout << \"Enter a number: \" ; std :: cin >> x ; int y {}; std :: cout << \"Enter another number: \" ; std :: cin >> y ; int op {}; do { std :: cout << \"Enter an operation (0=add, 1=subtract, 2=multiply): \" ; std :: cin >> op ; } while ( op < 0 || op > 2 ); int result {}; switch ( op ) { // call the target function directly using early binding case 0 : result = add ( x , y ); break ; case 1 : result = subtract ( x , y ); break ; case 2 : result = multiply ( x , y ); break ; } std :: cout << \"The answer is: \" << result << '\\n' ; return 0 ; } \u56e0\u4e3a add() \u3001 subtract() \u548c multiply() \u90fd\u662f\u76f4\u63a5\u51fd\u6570\u8c03\u7528\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u4f7f\u7528 \u65e9\u671f\u7ed1\u5b9a \u7684\u65b9\u5f0f\u6765\u89e3\u6790\u5b83\u4eec\u7684\u8c03\u7528\u3002\u7f16\u8bd1\u5668\u4f1a\u5c06 add() \u51fd\u6570\u7684\u8c03\u7528\u66ff\u6362\u4e3a\u4e00\u4e2a\u6307\u4ee4\uff0c\u544a\u8bc9CPU\u5982\u4f55\u8df3\u8f6c\u5230 add() \u51fd\u6570\u7684\u5730\u5740\u3002\u5bf9\u4e8e subtract() \u548c multiply() \u4e5f\u662f\u7c7b\u4f3c\u7684\u9053\u7406\u3002 \u5ef6\u8fdf\u7ed1\u5b9a Late Binding \u5728\u67d0\u4e9b\u7a0b\u5e8f\u4e2d\uff0c\u5728 \u8fd0\u884c\u65f6 (\u7a0b\u5e8f\u8fd0\u884c\u65f6)\u4e4b\u524d\u4e0d\u53ef\u80fd\u77e5\u9053\u5c06\u8c03\u7528\u54ea\u4e2a\u51fd\u6570\u3002\u8fd9\u88ab\u79f0\u4e3a \u540e\u671f\u7ed1\u5b9a (\u6216\u52a8\u6001\u7ed1\u5b9a)\u3002\u5728C++\u4e2d\uff0c\u83b7\u5f97\u540e\u671f\u7ed1\u5b9a\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 \u51fd\u6570\u6307\u9488 \u3002\u7b80\u5355\u56de\u987e\u4e00\u4e0b\u51fd\u6570\u6307\u9488\uff0c\u51fd\u6570\u6307\u9488\u662f\u4e00\u79cd\u6307\u5411\u51fd\u6570\u800c\u4e0d\u662f\u53d8\u91cf\u7684\u6307\u9488\u3002\u51fd\u6570\u6307\u9488\u6240\u6307\u5411\u7684\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u5728\u6307\u9488\u4e0a\u4f7f\u7528\u51fd\u6570\u8c03\u7528\u64cd\u4f5c\u7b26( () )\u6765\u8c03\u7528\u3002 \u4f8b\u5982\uff0c\u4e0b\u9762\u4ee3\u7801\u901a\u8fc7\u51fd\u6570\u6307\u9488\u6765\u8c03\u7528 add() \u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int add ( int x , int y ) { return x + y ; } int main () { // Create a function pointer and make it point to the add function int ( * pFcn )( int , int ) { add }; std :: cout << pFcn ( 5 , 3 ) << '\\n' ; // add 5 + 3 return 0 ; } \u901a\u8fc7\u51fd\u6570\u6307\u9488\u8c03\u7528\u51fd\u6570\u4e5f\u79f0\u4e3a\u95f4\u63a5\u51fd\u6570\u8c03\u7528\u3002\u4e0b\u9762\u7684\u8ba1\u7b97\u5668\u7a0b\u5e8f\u5728\u51fd\u6570\u4e0a\u4e0e\u4e0a\u9762\u7684\u8ba1\u7b97\u5668\u793a\u4f8b\u76f8\u540c\uff0c\u53ea\u4e0d\u8fc7\u5b83\u4f7f\u7528\u51fd\u6570\u6307\u9488\u800c\u4e0d\u662f\u76f4\u63a5\u7684\u51fd\u6570\u8c03\u7528: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 #include int add ( int x , int y ) { return x + y ; } int subtract ( int x , int y ) { return x - y ; } int multiply ( int x , int y ) { return x * y ; } int main () { int x {}; std :: cout << \"Enter a number: \" ; std :: cin >> x ; int y {}; std :: cout << \"Enter another number: \" ; std :: cin >> y ; int op {}; do { std :: cout << \"Enter an operation (0=add, 1=subtract, 2=multiply): \" ; std :: cin >> op ; } while ( op < 0 || op > 2 ); // Create a function pointer named pFcn (yes, the syntax is ugly) int ( * pFcn )( int , int ) { nullptr }; // Set pFcn to point to the function the user chose switch ( op ) { case 0 : pFcn = add ; break ; case 1 : pFcn = subtract ; break ; case 2 : pFcn = multiply ; break ; } // Call the function that pFcn is pointing to with x and y as parameters // This uses late binding std :: cout << \"The answer is: \" << pFcn ( x , y ) << '\\n' ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6ca1\u6709\u76f4\u63a5\u8c03\u7528 add() \u3001 subtract() \u6216 multiply() \uff0c\u800c\u662f\u8ba9\u51fd\u6570\u6307\u9488\u6307\u5411\u9700\u8981\u8c03\u7528\u7684\u51fd\u6570\u3002\u7136\u540e\u518d\u4f7f\u7528\u8be5\u51fd\u6570\u6307\u9488\u8c03\u7528\u51fd\u6570\u3002\u5f88\u663e\u7136\uff0c\u7f16\u8bd1\u5668\u6ca1\u529e\u6cd5\u901a\u8fc7\u65e9\u671f\u7ed1\u5b9a\u6765\u89e3\u6790\u51fd\u6570\u8c03\u7528 pFcn(x, y) \u56e0\u4e3a\u5728\u7f16\u8bd1\u65f6\u5b83\u5e76\u4e0d\u77e5\u9053 pFcn \u4f1a\u6307\u5411\u54ea\u4e2a\u51fd\u6570\u3002 \u540e\u671f\u7ed1\u5b9a\u7684\u6548\u7387\u7565\u4f4e\uff0c\u56e0\u4e3a\u5b83\u6d89\u53ca\u989d\u5916\u7684\u95f4\u63a5\u903b\u8f91\u5c42\u3002\u4f7f\u7528\u65e9\u671f\u7ed1\u5b9a\uff0cCPU\u53ef\u4ee5\u76f4\u63a5\u8df3\u8f6c\u5230\u51fd\u6570\u7684\u5730\u5740\u3002\u4f7f\u7528\u540e\u671f\u7ed1\u5b9a\uff0c\u7a0b\u5e8f\u5fc5\u987b\u8bfb\u53d6\u6307\u9488\u4e2d\u4fdd\u5b58\u7684\u5730\u5740\uff0c\u7136\u540e\u8df3\u8f6c\u5230\u8be5\u5730\u5740\u3002\u8fd9\u9700\u8981\u4e00\u4e2a\u989d\u5916\u7684\u6b65\u9aa4\uff0c\u4f7f\u5b83\u7a0d\u5fae\u6162\u4e00\u4e9b\u3002\u4f46\u662f\uff0c\u540e\u671f\u7ed1\u5b9a\u7684\u4f18\u70b9\u662f\u5b83\u6bd4\u65e9\u671f\u7ed1\u5b9a\u66f4\u7075\u6d3b\uff0c\u56e0\u4e3a\u76f4\u5230\u8fd0\u884c\u65f6\u624d\u9700\u8981\u51b3\u5b9a\u8c03\u7528\u4ec0\u4e48\u51fd\u6570\u3002 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u540e\u671f\u7ed1\u5b9a\u6765\u5b9e\u73b0\u865a\u51fd\u6570\u3002","title":"18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a"},{"location":"18-5-early-binding-and-late-binding/#early-binding","text":"\u7f16\u8bd1\u5668\u9047\u5230\u7684\u5927\u591a\u6570\u51fd\u6570\u8c03\u7528\u90fd\u662f\u51fd\u6570\u76f4\u63a5\u8c03\u7528\u3002\u76f4\u63a5\u8c03\u7528\u662f\u76f4\u63a5\u8c03\u7528\u51fd\u6570\u7684\u8bed\u53e5\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 #include void printValue ( int value ) { std :: cout << value ; } int main () { printValue ( 5 ); // This is a direct function call return 0 ; } \u76f4\u63a5\u51fd\u6570\u8c03\u7528\u53ef\u4ee5\u4f7f\u7528\u79f0\u4e3a\u65e9\u671f\u7ed1\u5b9a\u7684\u8fc7\u7a0b\u6765\u89e3\u51b3\u3002 \u65e9\u671f\u7ed1\u5b9a (\u4e5f\u79f0\u4e3a\u9759\u6001\u7ed1\u5b9a)\u610f\u5473\u7740\u7f16\u8bd1\u5668(\u6216\u94fe\u63a5\u5668)\u80fd\u591f\u76f4\u63a5\u5c06\u6807\u8bc6\u7b26\u540d(\u5982\u51fd\u6570\u540d\u6216\u53d8\u91cf\u540d)\u4e0e\u673a\u5668\u5730\u5740\u5173\u8054\u8d77\u6765 \u3002\u8bb0\u4f4f\uff0c\u6240\u6709\u51fd\u6570\u90fd\u6709\u552f\u4e00\u7684\u5730\u5740\u3002\u56e0\u6b64\uff0c\u5f53\u7f16\u8bd1\u5668(\u6216\u94fe\u63a5\u5668)\u9047\u5230\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u5b83\u4f1a\u7528\u4e00\u4e2a\u673a\u5668\u8bed\u8a00\u6307\u4ee4\u66ff\u6362\u51fd\u6570\u8c03\u7528\uff0c\u8be5\u6307\u4ee4\u544a\u8bc9CPU\u8df3\u8f6c\u5230\u51fd\u6570\u7684\u5730\u5740\u3002 \u8ba9\u6211\u4eec\u6765\u770b\u770b\u4e00\u4e2a\u4f7f\u7528\u65e9\u671f\u7ed1\u5b9a\u7684\u7b80\u5355\u8ba1\u7b97\u5668\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include int add ( int x , int y ) { return x + y ; } int subtract ( int x , int y ) { return x - y ; } int multiply ( int x , int y ) { return x * y ; } int main () { int x {}; std :: cout << \"Enter a number: \" ; std :: cin >> x ; int y {}; std :: cout << \"Enter another number: \" ; std :: cin >> y ; int op {}; do { std :: cout << \"Enter an operation (0=add, 1=subtract, 2=multiply): \" ; std :: cin >> op ; } while ( op < 0 || op > 2 ); int result {}; switch ( op ) { // call the target function directly using early binding case 0 : result = add ( x , y ); break ; case 1 : result = subtract ( x , y ); break ; case 2 : result = multiply ( x , y ); break ; } std :: cout << \"The answer is: \" << result << '\\n' ; return 0 ; } \u56e0\u4e3a add() \u3001 subtract() \u548c multiply() \u90fd\u662f\u76f4\u63a5\u51fd\u6570\u8c03\u7528\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u4f7f\u7528 \u65e9\u671f\u7ed1\u5b9a \u7684\u65b9\u5f0f\u6765\u89e3\u6790\u5b83\u4eec\u7684\u8c03\u7528\u3002\u7f16\u8bd1\u5668\u4f1a\u5c06 add() \u51fd\u6570\u7684\u8c03\u7528\u66ff\u6362\u4e3a\u4e00\u4e2a\u6307\u4ee4\uff0c\u544a\u8bc9CPU\u5982\u4f55\u8df3\u8f6c\u5230 add() \u51fd\u6570\u7684\u5730\u5740\u3002\u5bf9\u4e8e subtract() \u548c multiply() \u4e5f\u662f\u7c7b\u4f3c\u7684\u9053\u7406\u3002","title":"\u65e9\u7ed1\u5b9a Early binding"},{"location":"18-5-early-binding-and-late-binding/#late-binding","text":"\u5728\u67d0\u4e9b\u7a0b\u5e8f\u4e2d\uff0c\u5728 \u8fd0\u884c\u65f6 (\u7a0b\u5e8f\u8fd0\u884c\u65f6)\u4e4b\u524d\u4e0d\u53ef\u80fd\u77e5\u9053\u5c06\u8c03\u7528\u54ea\u4e2a\u51fd\u6570\u3002\u8fd9\u88ab\u79f0\u4e3a \u540e\u671f\u7ed1\u5b9a (\u6216\u52a8\u6001\u7ed1\u5b9a)\u3002\u5728C++\u4e2d\uff0c\u83b7\u5f97\u540e\u671f\u7ed1\u5b9a\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 \u51fd\u6570\u6307\u9488 \u3002\u7b80\u5355\u56de\u987e\u4e00\u4e0b\u51fd\u6570\u6307\u9488\uff0c\u51fd\u6570\u6307\u9488\u662f\u4e00\u79cd\u6307\u5411\u51fd\u6570\u800c\u4e0d\u662f\u53d8\u91cf\u7684\u6307\u9488\u3002\u51fd\u6570\u6307\u9488\u6240\u6307\u5411\u7684\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u5728\u6307\u9488\u4e0a\u4f7f\u7528\u51fd\u6570\u8c03\u7528\u64cd\u4f5c\u7b26( () )\u6765\u8c03\u7528\u3002 \u4f8b\u5982\uff0c\u4e0b\u9762\u4ee3\u7801\u901a\u8fc7\u51fd\u6570\u6307\u9488\u6765\u8c03\u7528 add() \u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int add ( int x , int y ) { return x + y ; } int main () { // Create a function pointer and make it point to the add function int ( * pFcn )( int , int ) { add }; std :: cout << pFcn ( 5 , 3 ) << '\\n' ; // add 5 + 3 return 0 ; } \u901a\u8fc7\u51fd\u6570\u6307\u9488\u8c03\u7528\u51fd\u6570\u4e5f\u79f0\u4e3a\u95f4\u63a5\u51fd\u6570\u8c03\u7528\u3002\u4e0b\u9762\u7684\u8ba1\u7b97\u5668\u7a0b\u5e8f\u5728\u51fd\u6570\u4e0a\u4e0e\u4e0a\u9762\u7684\u8ba1\u7b97\u5668\u793a\u4f8b\u76f8\u540c\uff0c\u53ea\u4e0d\u8fc7\u5b83\u4f7f\u7528\u51fd\u6570\u6307\u9488\u800c\u4e0d\u662f\u76f4\u63a5\u7684\u51fd\u6570\u8c03\u7528: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 #include int add ( int x , int y ) { return x + y ; } int subtract ( int x , int y ) { return x - y ; } int multiply ( int x , int y ) { return x * y ; } int main () { int x {}; std :: cout << \"Enter a number: \" ; std :: cin >> x ; int y {}; std :: cout << \"Enter another number: \" ; std :: cin >> y ; int op {}; do { std :: cout << \"Enter an operation (0=add, 1=subtract, 2=multiply): \" ; std :: cin >> op ; } while ( op < 0 || op > 2 ); // Create a function pointer named pFcn (yes, the syntax is ugly) int ( * pFcn )( int , int ) { nullptr }; // Set pFcn to point to the function the user chose switch ( op ) { case 0 : pFcn = add ; break ; case 1 : pFcn = subtract ; break ; case 2 : pFcn = multiply ; break ; } // Call the function that pFcn is pointing to with x and y as parameters // This uses late binding std :: cout << \"The answer is: \" << pFcn ( x , y ) << '\\n' ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6ca1\u6709\u76f4\u63a5\u8c03\u7528 add() \u3001 subtract() \u6216 multiply() \uff0c\u800c\u662f\u8ba9\u51fd\u6570\u6307\u9488\u6307\u5411\u9700\u8981\u8c03\u7528\u7684\u51fd\u6570\u3002\u7136\u540e\u518d\u4f7f\u7528\u8be5\u51fd\u6570\u6307\u9488\u8c03\u7528\u51fd\u6570\u3002\u5f88\u663e\u7136\uff0c\u7f16\u8bd1\u5668\u6ca1\u529e\u6cd5\u901a\u8fc7\u65e9\u671f\u7ed1\u5b9a\u6765\u89e3\u6790\u51fd\u6570\u8c03\u7528 pFcn(x, y) \u56e0\u4e3a\u5728\u7f16\u8bd1\u65f6\u5b83\u5e76\u4e0d\u77e5\u9053 pFcn \u4f1a\u6307\u5411\u54ea\u4e2a\u51fd\u6570\u3002 \u540e\u671f\u7ed1\u5b9a\u7684\u6548\u7387\u7565\u4f4e\uff0c\u56e0\u4e3a\u5b83\u6d89\u53ca\u989d\u5916\u7684\u95f4\u63a5\u903b\u8f91\u5c42\u3002\u4f7f\u7528\u65e9\u671f\u7ed1\u5b9a\uff0cCPU\u53ef\u4ee5\u76f4\u63a5\u8df3\u8f6c\u5230\u51fd\u6570\u7684\u5730\u5740\u3002\u4f7f\u7528\u540e\u671f\u7ed1\u5b9a\uff0c\u7a0b\u5e8f\u5fc5\u987b\u8bfb\u53d6\u6307\u9488\u4e2d\u4fdd\u5b58\u7684\u5730\u5740\uff0c\u7136\u540e\u8df3\u8f6c\u5230\u8be5\u5730\u5740\u3002\u8fd9\u9700\u8981\u4e00\u4e2a\u989d\u5916\u7684\u6b65\u9aa4\uff0c\u4f7f\u5b83\u7a0d\u5fae\u6162\u4e00\u4e9b\u3002\u4f46\u662f\uff0c\u540e\u671f\u7ed1\u5b9a\u7684\u4f18\u70b9\u662f\u5b83\u6bd4\u65e9\u671f\u7ed1\u5b9a\u66f4\u7075\u6d3b\uff0c\u56e0\u4e3a\u76f4\u5230\u8fd0\u884c\u65f6\u624d\u9700\u8981\u51b3\u5b9a\u8c03\u7528\u4ec0\u4e48\u51fd\u6570\u3002 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u540e\u671f\u7ed1\u5b9a\u6765\u5b9e\u73b0\u865a\u51fd\u6570\u3002","title":"\u5ef6\u8fdf\u7ed1\u5b9a Late Binding"},{"location":"18-6-the-virtual-table/","tags":["virtual-table"],"text":"Key Takeaway \u4e3a\u4e86\u5b9e\u73b0\u865a\u51fd\u6570\uff0cC++ \u4f7f\u7528\u4e86\u4e00\u79cd\u79f0\u4e3a \u865a\u8868 \u7684\u7279\u6b8a\u5f62\u5f0f\u7684 \u540e\u671f\u7ed1\u5b9a \u3002\u865a\u8868\u662f\u4e00\u4e2a\u51fd\u6570\u67e5\u627e\u8868\uff0c\u7528\u4e8e\u4ee5\u52a8\u6001/\u540e\u671f\u7ed1\u5b9a\u65b9\u5f0f\u89e3\u6790\u51fd\u6570\u8c03\u7528\u3002\u865a\u8868\u6709\u65f6\u4e5f\u79f0\u4e3a \u201cvtable\u201d\u3001\u201c\u865a\u51fd\u6570\u8868\u201d\u3001\u201c\u865a\u65b9\u6cd5\u8868\u201d\u6216\u201c\u8c03\u5ea6\u8868\u201d\u3002 \u56e0\u4e3a\u4f7f\u7528\u865a\u51fd\u6570\u5e76\u4e0d\u9700\u8981\u4e86\u89e3\u865a\u8868\u7684\u5de5\u4f5c\u65b9\u5f0f\uff0c\u6240\u4ee5\u53ef\u4ee5\u5c06\u672c\u8282\u89c6\u4e3a\u9009\u4fee \u865a\u8868\u5b9e\u9645\u4e0a\u975e\u5e38\u7b80\u5355\uff0c\u4e0d\u8fc7\u7528\u8bed\u8a00\u63cf\u8ff0\u6709\u70b9\u590d\u6742\u3002\u9996\u5148\uff0c\u6bcf\u4e2a\u4f7f\u7528\u865a\u51fd\u6570\u7684\u7c7b(\u6216\u4ece\u4f7f\u7528\u865a\u51fd\u6570\u7684\u7c7b\u6d3e\u751f\u800c\u6765\u7684\u7c7b)\u90fd\u6709\u81ea\u5df1\u7684\u865a\u8868\u3002\u8fd9\u4e2a\u8868\u53ea\u662f\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u65f6\u8bbe\u7f6e\u7684\u4e00\u4e2a\u9759\u6001\u6570\u7ec4\u3002\u865a\u8868\u4e3a\u7c7b\u7684\u5bf9\u8c61\u8c03\u7528\u7684\u6bcf\u4e2a\u865a\u51fd\u6570\u90fd\u5305\u542b\u4e00\u4e2a\u6761\u76ee\u3002\u8be5\u8868\u4e2d\u7684\u6bcf\u4e2a\u6761\u76ee\u90fd\u53ea\u662f\u4e00\u4e2a\u51fd\u6570\u6307\u9488\uff0c\u6307\u5411\u8be5\u7c7b\u53ef\u8bbf\u95ee\u7684\u6700\u540e\u6d3e\u751f\uff08most-derived\uff09\u7684\u51fd\u6570\u3002 \u5176\u6b21\uff0c\u7f16\u8bd1\u5668\u8fd8\u4f1a\u6dfb\u52a0\u4e00\u4e2a\u9690\u85cf\u7684\u6307\u9488\u4f5c\u4e3a\u57fa\u7c7b\u6210\u5458\uff0c\u79f0\u4e3a *__vptr \u3002 *__vptr \u4f1a\u5728\u7c7b\u5bf9\u8c61\u521b\u5efa\u65f6\u88ab\u81ea\u52a8\u8bbe\u7f6e\uff0c\u4f7f\u5176\u6307\u5411\u8be5\u7c7b\u7684\u865a\u8868\u3002\u548c *this \u6307\u9488\u4e0d\u540c\u7684\u662f\uff0c *this \u6307\u9488\u662f\u4e00\u4e2a\u51fd\u6570\u53c2\u6570\uff0c\u7528\u4e8e\u7f16\u8bd1\u5668\u89e3\u6790\u81ea\u5f15\u7528\u3002 \u800c *__vptr \u662f\u4e00\u4e2a\u5b9e\u9645\u7684\u6307\u9488\u3002\u56e0\u6b64\uff0c\u6240\u6709\u5177\u6709\u8be5\u6307\u9488\u7684\u5bf9\u8c61\u9700\u8981\u591a\u5206\u914d\u4e00\u4e2a\u6307\u9488\u5927\u5c0f\u7684\u5185\u5b58\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c *__vptr \u8fd8\u4f1a\u548c\u5176\u4ed6\u6210\u5458\u4e00\u6837\u88ab\u6d3e\u751f\u7ee7\u627f\uff0c\u8fd9\u4e00\u70b9\u975e\u5e38\u91cd\u8981 \u3002 \u4f46\u5c31\u76ee\u524d\u6765\u8bf4\uff0c\u4f60\u53ef\u80fd\u8fd8\u4e0d\u77e5\u9053\u8fd9\u4e9b\u4e1c\u897f\u662f\u5982\u4f55\u7ec4\u5408\u5728\u4e00\u8d77\u4f7f\u7528\u7684\uff0c\u6240\u4ee5\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class Base { public : virtual void function1 () {}; virtual void function2 () {}; }; class D1 : public Base { public : void function1 () override {}; }; class D2 : public Base { public : void function2 () override {}; }; \u4f8b\u5b50\u4e2d\u6709\u4e09\u4e2a\u7c7b\uff0c\u7f16\u8bd1\u5668\u4f1a\u521b\u5efa\u4e09\u4e2a \u865a\u8868 \uff1a Base \u3001 D1 \u3001 D2 \u5404\u4e00\u4e2a\u3002 \u7f16\u8bd1\u5668\u8fd8\u5411\u4f7f\u7528\u865a\u51fd\u6570\u7684 \u6700\u57fa\u7c7b \u6dfb\u52a0\u4e00\u4e2a\u9690\u85cf\u6307\u9488\u6210\u5458\u3002\u5c3d\u7ba1\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u4f46\u6211\u4eec\u5c06\u628a\u5b83\u653e\u5728\u4e0b\u4e00\u4e2a\u793a\u4f8b\u4e2d\uff0c\u53ea\u662f\u4e3a\u4e86\u663e\u793a\u6dfb\u52a0\u5b83\u7684\u4f4d\u7f6e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Base { public : VirtualTable * __vptr ; //\u81ea\u52a8\u6dfb\u52a0\u7684 virtual void function1 () {}; virtual void function2 () {}; }; class D1 : public Base { public : void function1 () override {}; }; class D2 : public Base { public : void function2 () override {}; }; \u5f53\u4e00\u4e2a\u7c7b\u5bf9\u8c61\u88ab\u521b\u5efa\u65f6\uff0c *__vptr \u88ab\u8bbe\u7f6e\u4e3a\u6307\u5411\u8be5\u7c7b\u7684\u865a\u8868 \u3002\u4f8b\u5982\uff0c\u5f53 Base \u7c7b\u578b\u7684\u5bf9\u8c61\u521b\u5efa\u65f6\uff0c *__vptr \u6307\u5411 Base \u7684\u865a\u8868\u3001\u5f53 D1 \u6216 D2 \u6784\u5efa\u65f6\uff0c *__vptr \u5219\u5206\u522b\u6307\u5411 D1 \u6216 D2 \u7684\u865a\u8868\u3002 \u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u8ba8\u8bba\u4e00\u4e0b\u5982\u4f55\u586b\u5145\u8fd9\u4e9b\u865a\u62df\u8868\u3002\u56e0\u4e3a\u8fd9\u91cc\u53ea\u6709\u4e24\u4e2a\u865a\u51fd\u6570\uff0c \u6bcf\u4e2a\u865a\u8868 \u5c06\u6709\u4e24\u4e2a\u6761\u76ee(\u4e00\u4e2a\u7528\u4e8e function1() \uff0c\u4e00\u4e2a\u7528\u4e8e function2() ))\u3002\u8bf7\u8bb0\u4f4f\uff0c\u5728\u586b\u5199\u8fd9\u4e9b\u865a\u8868\u65f6\uff0c\u6bcf\u4e2a\u6761\u76ee\u90fd\u7528\u8be5\u7c7b\u7c7b\u578b\u7684\u5bf9\u8c61\u53ef\u4ee5\u8c03\u7528\u7684\u6700\u540e\u6d3e\u751f\u7684\u51fd\u6570\u586b\u5199\u3002 Base \u5bf9\u8c61\u7684\u865a\u8868\u5f88\u7b80\u5355\uff0c\u4efb\u4f55 Base \u5bf9\u8c61\u90fd\u53ea\u80fd\u8bbf\u95ee Base \u7684\u6210\u5458\uff0c\u800c\u4e0d\u80fd\u8bbf\u95ee D1 \u6216\u8005 D2 \u3002\u8fd9\u6837\u4e00\u6765\uff0c\u865a\u8868\u4e2d\u6761\u76ee function1 \u6307\u5411 Base::function1() \u5e76\u4e14 function2 \u6307\u5411 Base::function2() . D1 \u7684\u865a\u8868\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\u3002D1 \u7c7b\u578b\u7684\u5bf9\u8c61\u53ef\u4ee5\u8bbf\u95ee D1 \u548c Base \u7684\u6210\u5458\uff0cD1 \u91cd\u5199 \u4e86 function1() \uff0c\u4f7f\u5f97 D1::function1() \u76f8\u8f83\u4e8e Base::function1() \u662f\u66f4\u665a\u88ab\u6d3e\u751f\u7684\u51fd\u6570\u3002\u56e0\u6b64\uff0c function1 \u6307\u5411 D1::function1() \u3002\u56e0\u4e3a D1 \u6ca1\u6709\u91cd\u5199 function2() \uff0c\u6240\u4ee5\u6761\u76ee function2 \u6307\u5411 Base::function2() \u3002 D2 \u7684\u865a\u8868\u7c7b\u4f3c\u4e8e D1 \u7684\u865a\u8868\u3002\u53ea\u4e0d\u8fc7 function1 \u6307\u5411 Base::function1() \u800c function2 \u6307\u5411 D2::function2() \u3002 Here\u2019s a picture of this graphically: \u5c3d\u7ba1\u770b\u4e0a\u53bb\u6709\u4e9b\u590d\u6742\uff0c\u6bcf\u4e2a\u7c7b\u7684 *__vptr \u90fd\u6307\u5411\u8be5\u7c7b\u7684 \u865a\u8868 \uff0c\u865a\u8868\u7684\u6761\u76ee\u6307\u5411\u8be5\u5bf9\u8c61\u80fd\u591f\u8c03\u7528\u7684\u6700\u540e\u88ab\u6d3e\u751f\u7684\u51fd\u6570\u3002 \u8003\u8651\u4e00\u4e0b\u5f53\u6211\u4eec\u521b\u5efa\u4e00\u4e2aD1\u7c7b\u578b\u7684\u5bf9\u8c61\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48: 1 2 3 4 int main () { D1 d1 ; } \u56e0\u4e3a d1 \u662f\u4e00\u4e2a D1 \u5bf9\u8c61\uff0c d1 \u7684 *__vptr \u6307\u5411 D1 \u7684\u865a\u8868\u3002 \u73b0\u5728\uff0c\u521b\u5efa\u4e00\u4e2a\u6307\u5411D1\u7684\u57fa\u7c7b\u6307\u9488\uff1a 1 2 3 4 5 6 7 int main () { D1 d1 ; Base * dPtr = & d1 ; return 0 ; } \u56e0\u4e3a dPtr \u662f\u4e00\u4e2a\u57fa\u7c7b\u6307\u9488\uff0c\u6240\u4ee5\u5b83\u6307\u5411\u7684\u662f d1 \u4e2d\u7684 Base \u90e8\u5206\u3002\u4e0d\u8fc7\uff0c\u7531\u4e8e __vptr \u4f4d\u4e8e Base \u90e8\u5206\uff0c\u6240\u4ee5 dPtr \u662f\u53ef\u4ee5\u8bbf\u95ee\u8be5\u6307\u9488\u7684\u3002\u6700\u7ec8\uff0c \u4e00\u5b9a\u8981\u6ce8\u610f dPtr->__vptr \u6307\u5411\u7684\u662f D1 \u7684\u865a\u8868\uff01 \u56e0\u6b64\uff0c\u5373\u4f7f dPtr \u662f Base \u7c7b\u578b\u6307\u9488\uff0c\u5b83\u4ecd\u7136\u80fd\u591f\u8bbf\u95ee D1\u2019\u7684\u865a\u8868(\u901a\u8fc7 __vptr )\u3002 \u90a3\u4e48\uff0c\u5f53\u6211\u4eec\u5c1d\u8bd5\u8c03\u7528 dPtr->function1() \u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f 1 2 3 4 5 6 7 8 int main () { D1 d1 ; Base * dPtr = & d1 ; dPtr -> function1 (); return 0 ; } \u9996\u5148\uff0c\u7a0b\u5e8f\u8bc6\u522b\u5230 function1() \u662f\u4e00\u4e2a \u865a\u51fd\u6570 \u3002\u5176\u6b21\uff0c\u7a0b\u5e8f\u4f7f\u7528 dPtr->__vptr \u83b7\u53d6 D1 \u7684\u865a\u51fd\u6570\u8868\u3002\u7136\u540e\uff0c\u5b83\u4f1a\u5728\u865a\u8868\u4e2d\u67e5\u627e function1() \u5bf9\u5e94\u7684\u7248\u672c\u2014\u2014\u5373 D1::function1() \u3002\u56e0\u6b64 dPtr->function1() \u4f1a\u89e3\u6790\u4e3a D1::function1() \uff01 \u4f60\u53ef\u80fd\u4f1a\u6709\u8fd9\u6837\u7684\u7591\u95ee\uff1a\u201c\u5982\u679c dPtr \u6307\u5411\u7684\u662f\u4e00\u4e2a Base \u7c7b\u7684\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f D1 \u7c7b\u578b\u7684\u5bf9\u8c61\u4f1a\u600e\u6837\uff1f\u5b83\u4ecd\u7136\u4f1a\u8c03\u7528 D1::function1() \u5417\uff1f\u7b54\u6848\u662f\u5426\u5b9a\u7684\u3002 1 2 3 4 5 6 7 8 int main () { Base b ; Base * bPtr = & b ; //\u6307\u5411\u4e00\u4e2aBase\u5bf9\u8c61 bPtr -> function1 (); return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c b \u88ab\u521b\u5efa\u540e\uff0c __vptr \u5c31\u88ab\u6307\u5411\u4e86 Base \u7684\u865a\u8868\u800c\u4e0d\u662fD1\u7684\u865a\u8868\u3002\u8fd9\u6837\u4e00\u6765\uff0c bPtr->__vptr \u4e5f\u540c\u6837\u6307\u5411 Base \u7684\u865a\u8868\u3002 Base \u865a\u8868\u7684 function1() \u6761\u76ee\u6307\u5411 Base::function1() \u3002\u56e0\u6b64 bPtr->function1() \u89e3\u6790\u5230 Base::function1() \uff0c\u56e0\u4e3a\u5b83\u662f\u80fd\u770b\u5230\u7684\u6700\u540e\u6d3e\u751f\u7684\u51fd\u6570\u3002 \u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u8868\uff0c\u7f16\u8bd1\u5668\u548c\u7a0b\u5e8f\u80fd\u591f\u786e\u4fdd\u51fd\u6570\u8c03\u7528\u89e3\u6790\u4e3a\u9002\u5f53\u7684\u865a\u51fd\u6570\uff0c\u5373\u4f7f\u4f60\u53ea\u4f7f\u7528\u6307\u5411\u57fa\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\uff01 \u8c03\u7528\u865a\u51fd\u6570\u6bd4\u8c03\u7528\u975e\u865a\u51fd\u6570\u8981\u6162\uff0c\u8fd9\u6709\u51e0\u4e2a\u539f\u56e0\uff1a\u9996\u5148\uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528 *__vptr \u6765\u83b7\u5f97\u9002\u5f53\u7684\u865a\u8868\u3002\u5176\u6b21\uff0c\u6211\u4eec\u5fc5\u987b\u5bf9\u865a\u62df\u8868\u5efa\u7acb\u7d22\u5f15\uff0c\u4ee5\u627e\u5230\u8981\u8c03\u7528\u7684\u51fd\u6570\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5fc5\u987b\u6267\u884c3\u4e2a\u64cd\u4f5c\u624d\u80fd\u627e\u5230\u8981\u8c03\u7528\u7684\u51fd\u6570\uff0c\u800c\u666e\u901a\u7684\u95f4\u63a5\u51fd\u6570\u8c03\u7528\u5219\u9700\u8981\u6267\u884c2\u4e2a\u64cd\u4f5c\uff0c\u76f4\u63a5\u51fd\u6570\u8c03\u7528\u5219\u9700\u8981\u6267\u884c1\u4e2a\u64cd\u4f5c\u3002\u7136\u800c\uff0c\u5728\u73b0\u4ee3\u8ba1\u7b97\u673a\u4e2d\uff0c\u8fd9\u589e\u52a0\u7684\u65f6\u95f4\u901a\u5e38\u662f\u76f8\u5f53\u5fae\u4e0d\u8db3\u9053\u7684\u3002 \u53e6\u5916\u9700\u8981\u63d0\u9192\u7684\u662f\uff0c\u4efb\u4f55\u4f7f\u7528\u865a\u51fd\u6570\u7684\u7c7b\u90fd\u6709 *__vptr \uff0c\u56e0\u6b64\u8be5\u7c7b\u7684\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u5c06\u589e\u52a0\u4e00\u4e2a\u6307\u9488\u3002\u865a\u51fd\u6570\u529f\u80fd\u5f3a\u5927\uff0c\u4f46\u5b83\u4eec\u786e\u5b9e\u6709\u6027\u80fd\u4ee3\u4ef7\u3002","title":"18.6 - \u865a\u51fd\u6570\u8868"},{"location":"18-7-pure-virtual-functions-abstract-base-classes-and-%20interface-classes/","tags":["pure-virtual-functions","abstract-base-classes","interface-classes"],"text":"Key Takeaway \u5305\u542b\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\u662f\u62bd\u8c61\u7c7b\uff0c\u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\uff0c\u5176\u6d3e\u751f\u7c7b\u5982\u679c\u5b9e\u73b0\u4e86\u6240\u6709\u7684\u865a\u51fd\u6570\u5219\u53d8\u6210\u6b63\u5e38\u7c7b \u5373\u4f7f\u7eaf\u865a\u51fd\u6570\u6709\u51fd\u6570\u4f53\uff0c\u4e5f\u662f\u7eaf\u865a\u51fd\u6570\uff0c\u800c\u4e14\u5b83\u7684\u5b9a\u4e49\u9700\u8981\u5355\u72ec\u5b9a\u4e49\uff0c\u800c\u4e0d\u80fd\u662f\u58f0\u660e\u65f6\u5b9a\u4e49 \u6709\u5b9e\u73b0\u7684\u7eaf\u865a\u51fd\u6570\u7528\u4e8e\u63d0\u4f9b\u9ed8\u8ba4\u5b9e\u73b0\uff0c\u6d3e\u751f\u7c7b\u5728\u5b9e\u73b0\u7eaf\u865a\u51fd\u6570\u65f6\u4e2d\u53ef\u4ee5\u663e\u5f0f\u5730\u8c03\u7528\u8be5\u7eaf\u865a\u51fd\u6570\u5b9e\u73b0 \u6790\u6784\u51fd\u6570\u53ef\u4ee5\u662f\u7eaf\u865a\u7684\uff0c\u4f46\u5fc5\u987b\u7ed9\u51fa\u4e00\u4e2a\u5b9a\u4e49\uff0c\u4ee5\u4fbf\u5728\u6d3e\u751f\u5bf9\u8c61\u6790\u6784\u65f6\u8c03\u7528\u5b83 \u63a5\u53e3\u7c7b\u4e5f\u6709\u865a\u8868\uff0c\u4f46\u662f\u7eaf\u865a\u51fd\u6570\u7684\u865a\u8868\u6761\u76ee\u8981\u4e48\u662f\u7a7a\u6307\u9488\u7684\u8981\u4e48\u662f\u6253\u5370\u62a5\u9519\u51fd\u6570 \u7eaf\u865a\uff08\u62bd\u8c61\uff09\u51fd\u6570\u548c\u7eaf\u865a\u57fa\u7c7b \u5230\u76ee\u524d\u4f4d\u7f6e\uff0c\u6211\u4eec\u770b\u5230\u7684 \u865a\u51fd\u6570 \u90fd\u662f\u7531\u51fd\u6570\u4f53\uff08\u51fd\u6570\u5b9a\u4e49\uff09\u7684\u3002\u4f46\u662f\uff0cC++ \u5141\u8bb8\u6211\u4eec\u521b\u5efa\u4e00\u79cd\u7279\u6b8a\u7684\u865a\u51fd\u6570\u2014\u2014 \u7eaf\u865a\u51fd\u6570 \uff08\u62bd\u8c61\u51fd\u6570\uff09\uff0c\u5b83\u6ca1\u6709\u51fd\u6570\u4f53\uff01\u7eaf\u865a\u51fd\u6570\u53ea\u662f\u4e00\u4e2a\u5360\u4f4d\u800c\u5df2\uff0c\u5b83\u5fc5\u987b\u5728 \u6d3e\u751f\u7c7b \u4e2d\u91cd\u5b9a\u4e49\u3002 \u521b\u5efa\u7eaf\u865a\u51fd\u6570\uff0c\u4e0d\u9700\u8981\u5b9a\u4e49\u51fd\u6570\u4f53\uff0c\u53ea\u9700\u8981\u5c06\u51fd\u6570\u8d4b\u503c\u4e3a0\u5373\u53ef\u3002 1 2 3 4 5 6 7 8 9 10 11 class Base { public : const char * sayHi () const { return \"Hi\" ; } // \u666e\u901a\u51fd\u6570 virtual const char * getName () const { return \"Base\" ; } // \u666e\u901a\u865a\u51fd\u6570 virtual int getValue () const = 0 ; // \u7eaf\u865a\u51fd\u6570 int doSomething () = 0 ; // \u7f16\u8bd1\u9519\u8bef: \u4e0d\u80fd\u7ed9\u975e\u865a\u51fd\u6570\u8d4b\u503c\u4e3a0 }; \u5f53\u6211\u4eec\u5411\u7c7b\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570\u65f6\uff0c\u5b9e\u9645\u4e0a\u662f\u5728\u8bf4\uff0c\u201c\u8981\u7531\u6d3e\u751f\u7c7b\u6765\u5b9e\u73b0\u8fd9\u4e2a\u51fd\u6570\u201d\u3002 \u4f7f\u7528\u7eaf\u865a\u51fd\u6570\u6709\u4e24\u4e2a\u4e3b\u8981\u540e\u679c\uff1a\u9996\u5148\uff0c\u4efb\u4f55\u5177\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\u90fd\u53d8\u6210\u4e86 \u62bd\u8c61\u7c7b \uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\uff01\u8003\u8651\u4e00\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a Base \u5b9e\u4f8b\u4f1a\u53d1\u751f\u4ec0\u4e48: 1 2 3 4 5 6 7 int main () { Base base ; // We can't instantiate an abstract base class, but for the sake of example, pretend this was allowed base . getValue (); // what would this do? return 0 ; } \u56e0\u4e3a\u6ca1\u6709 getValue() \u7684\u5b9a\u4e49\uff0c\u90a3\u4e48 base.getValue() \u5e94\u8be5\u5982\u4f55\u89e3\u6790\uff1f \u7b2c\u4e8c\uff0c\u4efb\u4f55\u6d3e\u751f\u7c7b\u90fd\u5fc5\u987b\u4e3a\u8fd9\u4e2a\u51fd\u6570\u5b9a\u4e49\u4e00\u4e2a\u4e3b\u4f53\uff0c\u5426\u5219\u6d3e\u751f\u7c7b\u4e5f\u4f1a\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b\u3002 \u4e00\u4e2a\u7eaf\u865a\u51fd\u6570\u7684\u4f8b\u5b50 \u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570\u7684\u4f8b\u5b50\u3002\u5728\u4e0a\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a\u7b80\u5355\u7684 Animal \u57fa\u7c7b\uff0c\u5e76\u4ece\u4e2d\u6d3e\u751f\u4e86 Cat \u548c Dog \u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include class Animal { protected : std :: string m_name ; // We're making this constructor protected because // we don't want people creating Animal objects directly, // but we still want derived classes to be able to use it. Animal ( const std :: string & name ) : m_name { name } { } public : std :: string getName () const { return m_name ; } virtual const char * speak () const { return \"???\" ; } virtual ~ Animal () = default ; //\u9ed8\u8ba4\u6790\u6784\u51fd\u6570\uff08\u865a\u51fd\u6570\uff09 }; class Cat : public Animal { public : Cat ( const std :: string & name ) : Animal { name } { } const char * speak () const override { return \"Meow\" ; } }; class Dog : public Animal { public : Dog ( const std :: string & name ) : Animal { name } { } const char * speak () const override { return \"Woof\" ; } }; \u6211\u4eec\u5c06\u6784\u9020\u51fd\u6570\u5b9a\u4e49\u4e3a \u53d7\u4fdd\u62a4\u6210\u5458 \u4ee5\u9632\u6b62\u5206\u914d Animal \u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u4f46\u662f\uff0c\u4ecd\u7136\u53ef\u4ee5\u521b\u5efa\u4e0d\u91cd\u65b0\u5b9a\u4e49 speak() \u7684\u6d3e\u751f\u7c7b\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include class Cow : public Animal { public : Cow ( const std :: string & name ) : Animal { name } { } // \u6ca1\u6709\u91cd\u65b0\u5b9a\u4e49 speak }; int main () { Cow cow { \"Betsy\" }; std :: cout << cow . getName () << \" says \" << cow . speak () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 Betsy says ??? \u53d1\u751f\u4e86\u4ec0\u4e48\u4e8b?\u6211\u4eec\u5fd8\u8bb0\u91cd\u65b0\u5b9a\u4e49\u51fd\u6570speak()\uff0c\u6240\u4ee5 cow.Speak() \u89e3\u6790\u4e3a Animal.speak() \uff0c\u8fd9\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u4e00\u4e2a\u66f4\u597d\u7684\u529e\u6cd5\u662f\u4f7f\u7528\u7eaf\u865a\u51fd\u6570\uff1a\u6211\u4eec\u901a\u8fc7\u5c06\u6784\u9020\u51fd\u6570\u5b9a\u4e49\u4e3a \u53d7\u4fdd\u62a4\u6210\u5458 \uff0c\u653e\u7f6e\u4eba\u4e3a\u5730\u5206\u914d Animal \u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u4f46\u662f\uff0c\u4ecd\u7136\u53ef\u4ee5\u521b\u5efa\u4e0d\u91cd\u65b0\u5b9a\u4e49 speak() \u7684\u6d3e\u751f\u7c7b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include class Animal // Animal \u662f\u4e00\u4e2a\u62bd\u8c61\u7c7b { protected : std :: string m_name ; public : Animal ( const std :: string & name ) : m_name { name } { } const std :: string & getName () const { return m_name ; } virtual const char * speak () const = 0 ; // \u7eaf\u865a\u51fd\u6570 virtual ~ Animal () = default ; }; \u8fd9\u91cc\u6709\u51e0\u70b9\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0c speak() \u73b0\u5728\u662f\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570\u3002\u8fd9\u610f\u5473\u7740 Animal \u73b0\u5728\u662f\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b\uff0c\u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u5c06\u6784\u9020\u51fd\u6570\u5b9a\u4e49\u4e3a\u53d7\u4fdd\u62a4\u6210\u5458(\u5c3d\u7ba1\u8fd9\u6837\u505a\u6ca1\u6709\u574f\u5904)\u3002\u7b2c\u4e8c\uff0c\u56e0\u4e3a\u6211\u4eec\u7684 Cow \u7c7b\u662f\u4ece Animal \u6d3e\u751f\u7684\uff0c\u4f46\u662f\u6211\u4eec\u6ca1\u6709\u5b9a\u4e49 Cow::speak() \uff0c\u6240\u4ee5 Cow \u4e5f\u662f\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b\u3002\u73b0\u5728\uff0c\u5f53\u6211\u4eec\u8bd5\u56fe\u7f16\u8bd1\u8fd9\u6bb5\u4ee3\u7801\u65f6: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include class Cow : public Animal { public : Cow ( const std :: string & name ) : Animal { name } { } // We forgot to redefine speak }; int main () { Cow cow { \"Betsy\" }; std :: cout << cow . getName () << \" says \" << cow . speak () << '\\n' ; return 0 ; } \u7f16\u8bd1\u5668\u4f1a\u7ed9\u6211\u4eec\u4e00\u4e2a\u8b66\u544a\uff0c\u56e0\u4e3a Cow \u662f\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b\uff0c\u6211\u4eec\u4e0d\u80fd\u521b\u5efa\u62bd\u8c61\u57fa\u7c7b\u7684\u5b9e\u4f8b(\u884c\u53f7\u662f\u9519\u8bef\u7684\uff0c\u56e0\u4e3a\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u7701\u7565\u4e86 Animal \u7c7b): 1 2 3 4 5 ( 33 ) : error C2259: 'Cow' : cannot instantiate abstract class ( 20 ) : note: see declaration of 'Cow' ( 33 ) : note: due to following members: ( 33 ) : note: 'const char *Animal::speak(void) const' : is abstract ( 15 ) : note: see declaration of 'Animal::speak' \u4e0a\u9762\u4fe1\u606f\u8868\u660e\uff0c\u53ea\u6709\u5f53 Cow \u4e3a speak() \u63d0\u4f9b\u4e86\u5b9a\u4e49\uff0c Cow \u624d\u80fd\u591f\u88ab\u5b9e\u4f8b\u5316\u3002 \u8ba9\u6211\u4eec\u7ee7\u7eed\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include class Animal // This Animal is an abstract base class { protected : std :: string m_name ; public : Animal ( const std :: string & name ) : m_name { name } { } const std :: string & getName () const { return m_name ; } virtual const char * speak () const = 0 ; // note that speak is now a pure virtual function virtual ~ Animal () = default ; }; class Cow : public Animal { public : Cow ( const std :: string & name ) : Animal ( name ) { } const char * speak () const override { return \"Moo\" ; } }; int main () { Cow cow { \"Betsy\" }; std :: cout << cow . getName () << \" says \" << cow . speak () << '\\n' ; return 0 ; } \u7f16\u8bd1\u5e76\u8fd0\u884c\u7a0b\u5e8f\uff1a 1 Betsy says Moo \u5f53\u6211\u4eec\u6709\u4e00\u4e2a\u60f3\u8981\u653e\u5728\u57fa\u7c7b\u4e2d\u7684\u51fd\u6570\uff0c\u4f46\u53ea\u6709\u6d3e\u751f\u7c7b\u77e5\u9053\u5b83\u5e94\u8be5\u8fd4\u56de\u4ec0\u4e48\u65f6\uff0c\u7eaf\u865a\u51fd\u6570\u662f\u6709\u7528\u7684 \u3002\u7eaf\u865a\u51fd\u6570\u4f7f\u5f97\u57fa\u7c7b\u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\uff0c\u6d3e\u751f\u7c7b\u5728\u88ab\u5b9e\u4f8b\u5316\u4e4b\u524d\u5fc5\u987b\u5b9a\u4e49\u8fd9\u4e9b\u51fd\u6570\u3002\u8fd9\u6709\u52a9\u4e8e\u786e\u4fdd\u6d3e\u751f\u7c7b\u4e0d\u4f1a\u5fd8\u8bb0\u91cd\u65b0\u5b9a\u4e49\u57fa\u7c7b\u5e0c\u671b\u5b83\u4eec\u91cd\u65b0\u5b9a\u4e49\u7684\u51fd\u6570\u3002 \u5c31\u50cf\u666e\u901a\u865a\u51fd\u6570\u4e00\u6837\uff0c\u7eaf\u865a\u51fd\u6570\u53ef\u4ee5\u4f7f\u7528\u57fa\u7c7b\u7684\u5f15\u7528(\u6216\u6307\u9488)\u6765\u8c03\u7528: 1 2 3 4 5 6 7 8 9 int main () { Cow cow { \"Betsy\" }; Animal & a { cow }; std :: cout << a . speak (); // resolves to Cow::speak(), prints \"Moo\" return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c a.speak() \u901a\u8fc7\u865a\u51fd\u6570\u89e3\u6790\u89e3\u6790\u4e3a Cow::speak() \u3002 \u7531\u4e8e\u5e26\u6709\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\u5177\u6709\u865a\u51fd\u6570\uff0c\u6240\u4ee5\u4e0d\u8981\u5fd8\u8bb0\u5c06\u6790\u6784\u51fd\u6570\u4e5f\u8bbe\u7f6e\u4e3a\u865a\u51fd\u6570\u3002 \u6709\u51fd\u6570\u4f53\uff08\u5b9a\u4e49\uff09\u7684\u7eaf\u865a\u51fd\u6570 \u4e8b\u5b9e\u8bc1\u660e\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u5177\u6709\u51fd\u6570\u4f53\u7684\u7eaf\u865a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include class Animal // This Animal is an abstract base class { protected : std :: string m_name ; public : Animal ( const std :: string & name ) : m_name { name } { } std :: string getName () { return m_name ; } virtual const char * speak () const = 0 ; // = 0 \u610f\u5473\u7740\u8fd9\u662f\u7eaf\u865a\u51fd\u6570 virtual ~ Animal () = default ; }; const char * Animal :: speak () const // \u5373\u4f7f\u8be5\u51fd\u6570\u6709\u5b9a\u4e49 { return \"buzz\" ; } \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c speak() \u4ecd\u7136\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570\u56e0\u4e3a\u201c= 0\u201d(\u5373\u4f7f\u5df2\u7ecf\u7ed9\u4e86\u5b83\u4e00\u4e2a\u5b9a\u4e49)\uff0c Animal \u4ecd\u7136\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b(\u56e0\u6b64\u4e0d\u80fd\u5b9e\u4f8b\u5316)\u3002\u4efb\u4f55\u7ee7\u627f\u81ea Animal \u7684\u7c7b\u90fd\u9700\u8981\u4e3a speak() \u63d0\u4f9b\u81ea\u5df1\u7684\u5b9a\u4e49\uff0c\u5426\u5219\u4e5f\u4f1a\u88ab\u8ba4\u4e3a\u662f\u62bd\u8c61\u57fa\u7c7b\u3002 \u5728\u4e3a\u7eaf\u865a\u51fd\u6570\u63d0\u4f9b\u5b9a\u4e49\u65f6\uff0c\u5fc5\u987b\u5355\u72ec\u63d0\u4f9b\u5b9a\u4e49(\u4e0d\u80fd\u662f \u5185\u8054\u51fd\u6570 )\u3002 \u5c0f\u8d34\u58eb \u5bf9\u4e8e Visual Studio \u7528\u6237\u7684\u63d0\u793a Visual Studio \u9519\u8bef\u5730\u5141\u8bb8\u7eaf\u865a\u51fd\u6570\u5728\u58f0\u660e\u65f6\u5b9a\u4e49\uff1b 1 2 3 4 5 // wrong! virtual const char * speak () const = 0 { return \"buzz\" ; } \u8fd9\u5176\u5b9e\u662f\u9519\u8bef\u7684\uff0c\u4f46\u662f\u65e0\u6cd5\u5173\u95ed\u3002 \u5f53\u60a8\u5e0c\u671b\u57fa\u7c7b\u4e3a\u51fd\u6570\u63d0\u4f9b\u9ed8\u8ba4\u5b9e\u73b0\uff0c\u4f46\u4ecd\u7136\u5f3a\u5236\u4efb\u4f55\u6d3e\u751f\u7c7b\u63d0\u4f9b\u5b83\u4eec\u81ea\u5df1\u7684\u5b9e\u73b0\u65f6\uff0c\u6b64\u8303\u4f8b\u53ef\u80fd\u5f88\u6709\u7528\u3002\u4f46\u662f\uff0c\u5982\u679c\u6d3e\u751f\u7c7b\u5bf9\u57fa\u7c7b\u63d0\u4f9b\u7684\u9ed8\u8ba4\u5b9e\u73b0\u611f\u5230\u6ee1\u610f\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u57fa\u7c7b\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 #include #include class Animal // Animal \u662f\u62bd\u8c61\u57fa\u7c7b { protected : std :: string m_name ; public : Animal ( const std :: string & name ) : m_name ( name ) { } const std :: string & getName () const { return m_name ; } virtual const char * speak () const = 0 ; // speak \u662f\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570 virtual ~ Animal () = default ; }; const char * Animal::speak () const { return \"buzz\" ; // \u9ed8\u8ba4\u5b9e\u73b0 } class Dragonfly : public Animal { public : Dragonfly ( const std :: string & name ) : Animal { name } { } const char * speak () const override // \u63d0\u4f9b\u7eaf\u865a\u51fd\u6570\u5b9a\u4e49\u540e\u8be5\u7c7b\u5df2\u7ecf\u4e0d\u662f\u62bd\u8c61\u7c7b\u4e86 { return Animal :: speak (); // \u4f7f\u7528 Animal \u7684\u9ed8\u8ba4\u5b9e\u73b0 } }; int main () { Dragonfly dfly { \"Sally\" }; std :: cout << dfly . getName () << \" says \" << dfly . speak () << '\\n' ; return 0 ; } \u8f93\u51fa\uff1a 1 Sally says buzz \u8fd9\u4e2a\u529f\u80fd\u5e76\u4e0d\u5e38\u7528\u3002 \u6790\u6784\u51fd\u6570\u53ef\u4ee5\u662f\u7eaf\u865a\u7684\uff0c\u4f46\u5fc5\u987b\u7ed9\u51fa\u4e00\u4e2a\u5b9a\u4e49\uff0c\u4ee5\u4fbf\u5728\u6d3e\u751f\u5bf9\u8c61\u6790\u6784\u65f6\u8c03\u7528\u5b83\u3002 \u63a5\u53e3\u7c7b \u63a5\u53e3\u7c7b \u6ca1\u6709\u6210\u5458\u53d8\u91cf\uff0c\u800c\u4e14 \u6240\u6709 \u51fd\u6570\u90fd\u662f \u7eaf\u865a\u51fd\u6570 \uff01\u6362\u8a00\u4e4b\uff0c\u8fd9\u4e2a\u7c7b\u5c31\u662f\u4e00\u4e2a\u7eaf\u5b9a\u4e49\uff0c\u5e76\u6ca1\u6709\u5b9e\u9645\u5b9e\u73b0\u3002\u5f53\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u51fa\u6d3e\u751f\u7c7b\u5fc5\u987b\u5b9e\u73b0\u7684\u529f\u80fd\u65f6\uff0c\u5e76\u4e14\u5c06\u8fd9\u4e9b\u529f\u80fd\u90fd\u7559\u7ed9\u6d3e\u751f\u7c7b\u5b9e\u73b0\u65f6\uff0c\u4f7f\u7528\u63a5\u53e3\u7c7b\u662f\u5f88\u6709\u7528\u7684\u3002 \u63a5\u53e3\u7c7b\u547d\u540d\u901a\u5e38\u4ee5 I \u5f00\u5934\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u63a5\u53e3\u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 class IErrorLog { public : virtual bool openLog ( const char * filename ) = 0 ; virtual bool closeLog () = 0 ; virtual bool writeError ( const char * errorMessage ) = 0 ; virtual ~ IErrorLog () {} // make a virtual destructor in case we delete an IErrorLog pointer, so the proper derived destructor is called }; \u4efb\u4f55\u4ece IErrorLog \u7ee7\u627f\u7684\u7c7b\u90fd\u5fc5\u987b\u5b9e\u73b0\u8fd9\u4e09\u4e2a\u51fd\u6570\uff0c\u4ee5\u4fbf\u88ab\u80fd\u591f\u88ab\u5b9e\u4f8b\u5316\u3002\u4f60\u53ef\u4ee5\u6d3e\u751f\u4e00\u4e2a\u540d\u4e3a FileErrorLog \u7684\u7c7b\uff0c\u5176\u4e2d openLog() \u6253\u5f00\u78c1\u76d8\u4e0a\u7684\u6587\u4ef6\uff0c closeLog() \u5173\u95ed\u6587\u4ef6\uff0c writeError() \u5c06\u6d88\u606f\u5199\u5165\u6587\u4ef6\u3002\u7136\u540e\u53ef\u6d3e\u751f\u53e6\u4e00\u4e2a\u540d\u4e3a ScreenErrorLog \u7684\u7c7b\uff0c\u5176\u4e2d openLog() \u548c closeLog() \u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\uff0c\u800c writeError() \u5728\u5c4f\u5e55\u4e0a\u7684\u5f39\u51fa\u6d88\u606f\u6846\u4e2d\u6253\u5370\u6d88\u606f\u3002 \u73b0\u5728\uff0c\u5047\u8bbe\u4f60\u9700\u8981\u7f16\u5199\u4e00\u4e9b\u4ee3\u7801\u5e76\u9700\u8981\u4f7f\u7528\u9519\u8bef\u65e5\u5fd7\u529f\u80fd\u3002\u5982\u679c\u4f60\u7f16\u5199\u7684\u4ee3\u7801\u76f4\u63a5\u5305\u4f7f\u7528 FileErrorLog \u6216 ScreenErrorLog \uff0c\u90a3\u4e48\u4f60\u5c06 \u53ea\u80fd \u4f7f\u7528\u8fd9\u79cd\u6307\u5b9a\u7684\u9519\u8bef\u65e5\u5fd7\u7c7b\u578b(\u5728\u4e0d\u4fee\u6539\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b)\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u51fd\u6570\u8feb\u4f7f mySqrt() \u7684\u8c03\u7528\u8005\u4f7f\u7528 FileErrorLog \uff0c\u4f46\u8c03\u7528\u8005\u53ef\u80fd\u5e76\u4e0d\u60f3\u4f7f\u7528\u8fd9\u79cd\u9519\u8bef\u65e5\u5fd7\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // for sqrt() double mySqrt ( double value , FileErrorLog & log ) //\u6ce8\u610f\u53c2\u6570\u4f7f\u7528\u7684\u662fFileErrorLog\u7c7b\u578b { if ( value < 0.0 ) { log . writeError ( \"Tried to take square root of value less than 0\" ); return 0.0 ; } else { return std :: sqrt ( value ); } } \u66f4\u52a0\u7075\u6d3b\u7684\u529e\u6cd5\u662f\u5c06\u8be5\u51fd\u6570\u5b9e\u73b0\u4e3a\u4f7f\u7528 IErrorLog \u7c7b\u578b\u7684\u53c2\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include // for sqrt() double mySqrt ( double value , IErrorLog & log ) { if ( value < 0.0 ) { log . writeError ( \"Tried to take square root of value less than 0\" ); return 0.0 ; } else { return std :: sqrt ( value ); } } \u8fd9\u6837\u4e00\u6765\uff0c\u8c03\u7528\u8005\u53ef\u4ee5\u4f20\u5165\u7b26\u5408 IErrorLog \u63a5\u53e3\u7684\u4efb\u4f55\u7c7b\u3002\u5982\u679c\u5b83\u4eec\u5e0c\u671b\u5c06\u9519\u8bef\u65e5\u5fd7\u5199\u5230\u6587\u4ef6\uff0c\u5219\u53ef\u4ee5\u4f20\u5165 FileErrorLog \u7684\u5b9e\u4f8b\u3002\u5982\u679c\u60f3\u9519\u8bef\u4fe1\u606f\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\uff0c\u5219\u53ef\u4ee5\u4f20\u5165\u4e00\u4e2a ScreenErrorLog \u7684\u5b9e\u4f8b\u3002\u5982\u679c\u4ed6\u4eec\u60f3\u505a\u4e00\u4e9b\u4f60\u751a\u81f3\u6ca1\u6709\u60f3\u8fc7\u7684\u4e8b\u60c5\uff0c\u6bd4\u5982\u5728\u51fa\u73b0\u9519\u8bef\u65f6\u5411\u67d0\u4eba\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\uff0c\u5219\u53ef\u4ee5\u4ece IErrorLog (\u4f8b\u5982EmailErrorLog)\u6d3e\u751f\u4e00\u4e2a\u65b0\u7c7b\u5e76\u4f7f\u7528\u5b83\u7684\u5b9e\u4f8b\uff01\u901a\u8fc7\u4f7f\u7528IErrorLog\uff0c\u60a8\u7684\u51fd\u6570\u53d8\u5f97\u66f4\u52a0\u72ec\u7acb\u548c\u7075\u6d3b\u3002 \u4e0d\u8981\u5fd8\u8bb0\u4e3a\u63a5\u53e3\u7c7b\u5b9a\u4e49\u865a\u6790\u6784\u51fd\u6570\uff0c\u4ee5\u4fbf\u5728 \u5220\u9664\u6307\u5411\u63a5\u53e3\u7684\u6307\u9488 \u65f6\u8c03\u7528\u9002\u5f53\u7684\u6d3e\u751f\u6790\u6784\u51fd\u6570\u3002 \u63a5\u53e3\u7c7b\u975e\u5e38\u5e38\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u6613\u4e8e\u4f7f\u7528\u3001\u6613\u4e8e\u6269\u5c55\u548c\u6613\u4e8e\u7ef4\u62a4\u3002\u4e8b\u5b9e\u4e0a\uff0c\u4e00\u4e9b\u73b0\u4ee3\u8bed\u8a00\uff0c\u5982Java\u548cC#\uff0c\u5df2\u7ecf\u6dfb\u52a0\u4e86 interface \u5173\u952e\u5b57\uff0c\u5141\u8bb8\u7a0b\u5e8f\u5458\u76f4\u63a5\u5b9a\u4e49\u63a5\u53e3\u7c7b\uff0c\u800c\u4e0d\u5fc5\u663e\u5f0f\u5730\u5c06\u6240\u6709\u6210\u5458\u51fd\u6570\u6807\u8bb0\u4e3a\u62bd\u8c61\u3002\u6b64\u5916\uff0c\u5c3d\u7ba1Java(\u7248\u672c8\u4e4b\u524d\u7684\u7248\u672c)\u548cC#\u4e0d\u5141\u8bb8\u5bf9\u666e\u901a\u7c7b\u4f7f\u7528\u591a\u91cd\u7ee7\u627f\uff0c\u4f46\u5b83\u4eec\u5141\u8bb8\u60a8\u6839\u636e\u9700\u8981\u5bf9\u4efb\u610f\u591a\u7684\u63a5\u53e3\u8fdb\u884c\u591a\u91cd\u7ee7\u627f\u3002\u56e0\u4e3a\u63a5\u53e3\u6ca1\u6709\u6570\u636e\u4e5f\u6ca1\u6709\u51fd\u6570\u4f53\uff0c\u6240\u4ee5\u5b83\u4eec\u907f\u514d\u4e86\u8bb8\u591a\u4f20\u7edf\u7684\u591a\u91cd\u7ee7\u627f\u95ee\u9898\uff0c\u540c\u65f6\u4ecd\u7136\u63d0\u4f9b\u4e86\u5f88\u5927\u7684\u7075\u6d3b\u6027\u3002 \u7eaf\u865a\u51fd\u6570\u548c\u865a\u8868 \u62bd\u8c61\u7c7b\u4ecd\u7136\u6709\u865a\u8868\uff0c\u56e0\u4e3a\u5982\u679c\u60a8\u6709\u6307\u5411\u62bd\u8c61\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u8fd9\u4e9b\u865a\u8868\u4ecd\u7136\u6709\u7528 \u3002\u5e26\u6709\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\u7684\u865a\u8868\u9879\u901a\u5e38\u8981\u4e48\u5305\u542b\u7a7a\u6307\u9488\uff0c\u8981\u4e48\u6307\u5411\u6253\u5370\u9519\u8bef\u7684\u6cdb\u578b\u51fd\u6570(\u6709\u65f6\u8be5\u51fd\u6570\u88ab\u547d\u540d\u4e3a __purecall )\u3002","title":"18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b"},{"location":"18-7-pure-virtual-functions-abstract-base-classes-and-%20interface-classes/#_1","text":"\u5230\u76ee\u524d\u4f4d\u7f6e\uff0c\u6211\u4eec\u770b\u5230\u7684 \u865a\u51fd\u6570 \u90fd\u662f\u7531\u51fd\u6570\u4f53\uff08\u51fd\u6570\u5b9a\u4e49\uff09\u7684\u3002\u4f46\u662f\uff0cC++ \u5141\u8bb8\u6211\u4eec\u521b\u5efa\u4e00\u79cd\u7279\u6b8a\u7684\u865a\u51fd\u6570\u2014\u2014 \u7eaf\u865a\u51fd\u6570 \uff08\u62bd\u8c61\u51fd\u6570\uff09\uff0c\u5b83\u6ca1\u6709\u51fd\u6570\u4f53\uff01\u7eaf\u865a\u51fd\u6570\u53ea\u662f\u4e00\u4e2a\u5360\u4f4d\u800c\u5df2\uff0c\u5b83\u5fc5\u987b\u5728 \u6d3e\u751f\u7c7b \u4e2d\u91cd\u5b9a\u4e49\u3002 \u521b\u5efa\u7eaf\u865a\u51fd\u6570\uff0c\u4e0d\u9700\u8981\u5b9a\u4e49\u51fd\u6570\u4f53\uff0c\u53ea\u9700\u8981\u5c06\u51fd\u6570\u8d4b\u503c\u4e3a0\u5373\u53ef\u3002 1 2 3 4 5 6 7 8 9 10 11 class Base { public : const char * sayHi () const { return \"Hi\" ; } // \u666e\u901a\u51fd\u6570 virtual const char * getName () const { return \"Base\" ; } // \u666e\u901a\u865a\u51fd\u6570 virtual int getValue () const = 0 ; // \u7eaf\u865a\u51fd\u6570 int doSomething () = 0 ; // \u7f16\u8bd1\u9519\u8bef: \u4e0d\u80fd\u7ed9\u975e\u865a\u51fd\u6570\u8d4b\u503c\u4e3a0 }; \u5f53\u6211\u4eec\u5411\u7c7b\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570\u65f6\uff0c\u5b9e\u9645\u4e0a\u662f\u5728\u8bf4\uff0c\u201c\u8981\u7531\u6d3e\u751f\u7c7b\u6765\u5b9e\u73b0\u8fd9\u4e2a\u51fd\u6570\u201d\u3002 \u4f7f\u7528\u7eaf\u865a\u51fd\u6570\u6709\u4e24\u4e2a\u4e3b\u8981\u540e\u679c\uff1a\u9996\u5148\uff0c\u4efb\u4f55\u5177\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\u90fd\u53d8\u6210\u4e86 \u62bd\u8c61\u7c7b \uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\uff01\u8003\u8651\u4e00\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a Base \u5b9e\u4f8b\u4f1a\u53d1\u751f\u4ec0\u4e48: 1 2 3 4 5 6 7 int main () { Base base ; // We can't instantiate an abstract base class, but for the sake of example, pretend this was allowed base . getValue (); // what would this do? return 0 ; } \u56e0\u4e3a\u6ca1\u6709 getValue() \u7684\u5b9a\u4e49\uff0c\u90a3\u4e48 base.getValue() \u5e94\u8be5\u5982\u4f55\u89e3\u6790\uff1f \u7b2c\u4e8c\uff0c\u4efb\u4f55\u6d3e\u751f\u7c7b\u90fd\u5fc5\u987b\u4e3a\u8fd9\u4e2a\u51fd\u6570\u5b9a\u4e49\u4e00\u4e2a\u4e3b\u4f53\uff0c\u5426\u5219\u6d3e\u751f\u7c7b\u4e5f\u4f1a\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b\u3002","title":"\u7eaf\u865a\uff08\u62bd\u8c61\uff09\u51fd\u6570\u548c\u7eaf\u865a\u57fa\u7c7b"},{"location":"18-7-pure-virtual-functions-abstract-base-classes-and-%20interface-classes/#_2","text":"\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570\u7684\u4f8b\u5b50\u3002\u5728\u4e0a\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a\u7b80\u5355\u7684 Animal \u57fa\u7c7b\uff0c\u5e76\u4ece\u4e2d\u6d3e\u751f\u4e86 Cat \u548c Dog \u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include class Animal { protected : std :: string m_name ; // We're making this constructor protected because // we don't want people creating Animal objects directly, // but we still want derived classes to be able to use it. Animal ( const std :: string & name ) : m_name { name } { } public : std :: string getName () const { return m_name ; } virtual const char * speak () const { return \"???\" ; } virtual ~ Animal () = default ; //\u9ed8\u8ba4\u6790\u6784\u51fd\u6570\uff08\u865a\u51fd\u6570\uff09 }; class Cat : public Animal { public : Cat ( const std :: string & name ) : Animal { name } { } const char * speak () const override { return \"Meow\" ; } }; class Dog : public Animal { public : Dog ( const std :: string & name ) : Animal { name } { } const char * speak () const override { return \"Woof\" ; } }; \u6211\u4eec\u5c06\u6784\u9020\u51fd\u6570\u5b9a\u4e49\u4e3a \u53d7\u4fdd\u62a4\u6210\u5458 \u4ee5\u9632\u6b62\u5206\u914d Animal \u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u4f46\u662f\uff0c\u4ecd\u7136\u53ef\u4ee5\u521b\u5efa\u4e0d\u91cd\u65b0\u5b9a\u4e49 speak() \u7684\u6d3e\u751f\u7c7b\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include class Cow : public Animal { public : Cow ( const std :: string & name ) : Animal { name } { } // \u6ca1\u6709\u91cd\u65b0\u5b9a\u4e49 speak }; int main () { Cow cow { \"Betsy\" }; std :: cout << cow . getName () << \" says \" << cow . speak () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 Betsy says ??? \u53d1\u751f\u4e86\u4ec0\u4e48\u4e8b?\u6211\u4eec\u5fd8\u8bb0\u91cd\u65b0\u5b9a\u4e49\u51fd\u6570speak()\uff0c\u6240\u4ee5 cow.Speak() \u89e3\u6790\u4e3a Animal.speak() \uff0c\u8fd9\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u4e00\u4e2a\u66f4\u597d\u7684\u529e\u6cd5\u662f\u4f7f\u7528\u7eaf\u865a\u51fd\u6570\uff1a\u6211\u4eec\u901a\u8fc7\u5c06\u6784\u9020\u51fd\u6570\u5b9a\u4e49\u4e3a \u53d7\u4fdd\u62a4\u6210\u5458 \uff0c\u653e\u7f6e\u4eba\u4e3a\u5730\u5206\u914d Animal \u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u4f46\u662f\uff0c\u4ecd\u7136\u53ef\u4ee5\u521b\u5efa\u4e0d\u91cd\u65b0\u5b9a\u4e49 speak() \u7684\u6d3e\u751f\u7c7b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include class Animal // Animal \u662f\u4e00\u4e2a\u62bd\u8c61\u7c7b { protected : std :: string m_name ; public : Animal ( const std :: string & name ) : m_name { name } { } const std :: string & getName () const { return m_name ; } virtual const char * speak () const = 0 ; // \u7eaf\u865a\u51fd\u6570 virtual ~ Animal () = default ; }; \u8fd9\u91cc\u6709\u51e0\u70b9\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0c speak() \u73b0\u5728\u662f\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570\u3002\u8fd9\u610f\u5473\u7740 Animal \u73b0\u5728\u662f\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b\uff0c\u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u5c06\u6784\u9020\u51fd\u6570\u5b9a\u4e49\u4e3a\u53d7\u4fdd\u62a4\u6210\u5458(\u5c3d\u7ba1\u8fd9\u6837\u505a\u6ca1\u6709\u574f\u5904)\u3002\u7b2c\u4e8c\uff0c\u56e0\u4e3a\u6211\u4eec\u7684 Cow \u7c7b\u662f\u4ece Animal \u6d3e\u751f\u7684\uff0c\u4f46\u662f\u6211\u4eec\u6ca1\u6709\u5b9a\u4e49 Cow::speak() \uff0c\u6240\u4ee5 Cow \u4e5f\u662f\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b\u3002\u73b0\u5728\uff0c\u5f53\u6211\u4eec\u8bd5\u56fe\u7f16\u8bd1\u8fd9\u6bb5\u4ee3\u7801\u65f6: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include class Cow : public Animal { public : Cow ( const std :: string & name ) : Animal { name } { } // We forgot to redefine speak }; int main () { Cow cow { \"Betsy\" }; std :: cout << cow . getName () << \" says \" << cow . speak () << '\\n' ; return 0 ; } \u7f16\u8bd1\u5668\u4f1a\u7ed9\u6211\u4eec\u4e00\u4e2a\u8b66\u544a\uff0c\u56e0\u4e3a Cow \u662f\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b\uff0c\u6211\u4eec\u4e0d\u80fd\u521b\u5efa\u62bd\u8c61\u57fa\u7c7b\u7684\u5b9e\u4f8b(\u884c\u53f7\u662f\u9519\u8bef\u7684\uff0c\u56e0\u4e3a\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u7701\u7565\u4e86 Animal \u7c7b): 1 2 3 4 5 ( 33 ) : error C2259: 'Cow' : cannot instantiate abstract class ( 20 ) : note: see declaration of 'Cow' ( 33 ) : note: due to following members: ( 33 ) : note: 'const char *Animal::speak(void) const' : is abstract ( 15 ) : note: see declaration of 'Animal::speak' \u4e0a\u9762\u4fe1\u606f\u8868\u660e\uff0c\u53ea\u6709\u5f53 Cow \u4e3a speak() \u63d0\u4f9b\u4e86\u5b9a\u4e49\uff0c Cow \u624d\u80fd\u591f\u88ab\u5b9e\u4f8b\u5316\u3002 \u8ba9\u6211\u4eec\u7ee7\u7eed\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include class Animal // This Animal is an abstract base class { protected : std :: string m_name ; public : Animal ( const std :: string & name ) : m_name { name } { } const std :: string & getName () const { return m_name ; } virtual const char * speak () const = 0 ; // note that speak is now a pure virtual function virtual ~ Animal () = default ; }; class Cow : public Animal { public : Cow ( const std :: string & name ) : Animal ( name ) { } const char * speak () const override { return \"Moo\" ; } }; int main () { Cow cow { \"Betsy\" }; std :: cout << cow . getName () << \" says \" << cow . speak () << '\\n' ; return 0 ; } \u7f16\u8bd1\u5e76\u8fd0\u884c\u7a0b\u5e8f\uff1a 1 Betsy says Moo \u5f53\u6211\u4eec\u6709\u4e00\u4e2a\u60f3\u8981\u653e\u5728\u57fa\u7c7b\u4e2d\u7684\u51fd\u6570\uff0c\u4f46\u53ea\u6709\u6d3e\u751f\u7c7b\u77e5\u9053\u5b83\u5e94\u8be5\u8fd4\u56de\u4ec0\u4e48\u65f6\uff0c\u7eaf\u865a\u51fd\u6570\u662f\u6709\u7528\u7684 \u3002\u7eaf\u865a\u51fd\u6570\u4f7f\u5f97\u57fa\u7c7b\u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\uff0c\u6d3e\u751f\u7c7b\u5728\u88ab\u5b9e\u4f8b\u5316\u4e4b\u524d\u5fc5\u987b\u5b9a\u4e49\u8fd9\u4e9b\u51fd\u6570\u3002\u8fd9\u6709\u52a9\u4e8e\u786e\u4fdd\u6d3e\u751f\u7c7b\u4e0d\u4f1a\u5fd8\u8bb0\u91cd\u65b0\u5b9a\u4e49\u57fa\u7c7b\u5e0c\u671b\u5b83\u4eec\u91cd\u65b0\u5b9a\u4e49\u7684\u51fd\u6570\u3002 \u5c31\u50cf\u666e\u901a\u865a\u51fd\u6570\u4e00\u6837\uff0c\u7eaf\u865a\u51fd\u6570\u53ef\u4ee5\u4f7f\u7528\u57fa\u7c7b\u7684\u5f15\u7528(\u6216\u6307\u9488)\u6765\u8c03\u7528: 1 2 3 4 5 6 7 8 9 int main () { Cow cow { \"Betsy\" }; Animal & a { cow }; std :: cout << a . speak (); // resolves to Cow::speak(), prints \"Moo\" return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c a.speak() \u901a\u8fc7\u865a\u51fd\u6570\u89e3\u6790\u89e3\u6790\u4e3a Cow::speak() \u3002 \u7531\u4e8e\u5e26\u6709\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\u5177\u6709\u865a\u51fd\u6570\uff0c\u6240\u4ee5\u4e0d\u8981\u5fd8\u8bb0\u5c06\u6790\u6784\u51fd\u6570\u4e5f\u8bbe\u7f6e\u4e3a\u865a\u51fd\u6570\u3002","title":"\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570\u7684\u4f8b\u5b50"},{"location":"18-7-pure-virtual-functions-abstract-base-classes-and-%20interface-classes/#_3","text":"\u4e8b\u5b9e\u8bc1\u660e\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u5177\u6709\u51fd\u6570\u4f53\u7684\u7eaf\u865a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include class Animal // This Animal is an abstract base class { protected : std :: string m_name ; public : Animal ( const std :: string & name ) : m_name { name } { } std :: string getName () { return m_name ; } virtual const char * speak () const = 0 ; // = 0 \u610f\u5473\u7740\u8fd9\u662f\u7eaf\u865a\u51fd\u6570 virtual ~ Animal () = default ; }; const char * Animal :: speak () const // \u5373\u4f7f\u8be5\u51fd\u6570\u6709\u5b9a\u4e49 { return \"buzz\" ; } \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c speak() \u4ecd\u7136\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570\u56e0\u4e3a\u201c= 0\u201d(\u5373\u4f7f\u5df2\u7ecf\u7ed9\u4e86\u5b83\u4e00\u4e2a\u5b9a\u4e49)\uff0c Animal \u4ecd\u7136\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b(\u56e0\u6b64\u4e0d\u80fd\u5b9e\u4f8b\u5316)\u3002\u4efb\u4f55\u7ee7\u627f\u81ea Animal \u7684\u7c7b\u90fd\u9700\u8981\u4e3a speak() \u63d0\u4f9b\u81ea\u5df1\u7684\u5b9a\u4e49\uff0c\u5426\u5219\u4e5f\u4f1a\u88ab\u8ba4\u4e3a\u662f\u62bd\u8c61\u57fa\u7c7b\u3002 \u5728\u4e3a\u7eaf\u865a\u51fd\u6570\u63d0\u4f9b\u5b9a\u4e49\u65f6\uff0c\u5fc5\u987b\u5355\u72ec\u63d0\u4f9b\u5b9a\u4e49(\u4e0d\u80fd\u662f \u5185\u8054\u51fd\u6570 )\u3002 \u5c0f\u8d34\u58eb \u5bf9\u4e8e Visual Studio \u7528\u6237\u7684\u63d0\u793a Visual Studio \u9519\u8bef\u5730\u5141\u8bb8\u7eaf\u865a\u51fd\u6570\u5728\u58f0\u660e\u65f6\u5b9a\u4e49\uff1b 1 2 3 4 5 // wrong! virtual const char * speak () const = 0 { return \"buzz\" ; } \u8fd9\u5176\u5b9e\u662f\u9519\u8bef\u7684\uff0c\u4f46\u662f\u65e0\u6cd5\u5173\u95ed\u3002 \u5f53\u60a8\u5e0c\u671b\u57fa\u7c7b\u4e3a\u51fd\u6570\u63d0\u4f9b\u9ed8\u8ba4\u5b9e\u73b0\uff0c\u4f46\u4ecd\u7136\u5f3a\u5236\u4efb\u4f55\u6d3e\u751f\u7c7b\u63d0\u4f9b\u5b83\u4eec\u81ea\u5df1\u7684\u5b9e\u73b0\u65f6\uff0c\u6b64\u8303\u4f8b\u53ef\u80fd\u5f88\u6709\u7528\u3002\u4f46\u662f\uff0c\u5982\u679c\u6d3e\u751f\u7c7b\u5bf9\u57fa\u7c7b\u63d0\u4f9b\u7684\u9ed8\u8ba4\u5b9e\u73b0\u611f\u5230\u6ee1\u610f\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u57fa\u7c7b\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 #include #include class Animal // Animal \u662f\u62bd\u8c61\u57fa\u7c7b { protected : std :: string m_name ; public : Animal ( const std :: string & name ) : m_name ( name ) { } const std :: string & getName () const { return m_name ; } virtual const char * speak () const = 0 ; // speak \u662f\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570 virtual ~ Animal () = default ; }; const char * Animal::speak () const { return \"buzz\" ; // \u9ed8\u8ba4\u5b9e\u73b0 } class Dragonfly : public Animal { public : Dragonfly ( const std :: string & name ) : Animal { name } { } const char * speak () const override // \u63d0\u4f9b\u7eaf\u865a\u51fd\u6570\u5b9a\u4e49\u540e\u8be5\u7c7b\u5df2\u7ecf\u4e0d\u662f\u62bd\u8c61\u7c7b\u4e86 { return Animal :: speak (); // \u4f7f\u7528 Animal \u7684\u9ed8\u8ba4\u5b9e\u73b0 } }; int main () { Dragonfly dfly { \"Sally\" }; std :: cout << dfly . getName () << \" says \" << dfly . speak () << '\\n' ; return 0 ; } \u8f93\u51fa\uff1a 1 Sally says buzz \u8fd9\u4e2a\u529f\u80fd\u5e76\u4e0d\u5e38\u7528\u3002 \u6790\u6784\u51fd\u6570\u53ef\u4ee5\u662f\u7eaf\u865a\u7684\uff0c\u4f46\u5fc5\u987b\u7ed9\u51fa\u4e00\u4e2a\u5b9a\u4e49\uff0c\u4ee5\u4fbf\u5728\u6d3e\u751f\u5bf9\u8c61\u6790\u6784\u65f6\u8c03\u7528\u5b83\u3002","title":"\u6709\u51fd\u6570\u4f53\uff08\u5b9a\u4e49\uff09\u7684\u7eaf\u865a\u51fd\u6570"},{"location":"18-7-pure-virtual-functions-abstract-base-classes-and-%20interface-classes/#_4","text":"\u63a5\u53e3\u7c7b \u6ca1\u6709\u6210\u5458\u53d8\u91cf\uff0c\u800c\u4e14 \u6240\u6709 \u51fd\u6570\u90fd\u662f \u7eaf\u865a\u51fd\u6570 \uff01\u6362\u8a00\u4e4b\uff0c\u8fd9\u4e2a\u7c7b\u5c31\u662f\u4e00\u4e2a\u7eaf\u5b9a\u4e49\uff0c\u5e76\u6ca1\u6709\u5b9e\u9645\u5b9e\u73b0\u3002\u5f53\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u51fa\u6d3e\u751f\u7c7b\u5fc5\u987b\u5b9e\u73b0\u7684\u529f\u80fd\u65f6\uff0c\u5e76\u4e14\u5c06\u8fd9\u4e9b\u529f\u80fd\u90fd\u7559\u7ed9\u6d3e\u751f\u7c7b\u5b9e\u73b0\u65f6\uff0c\u4f7f\u7528\u63a5\u53e3\u7c7b\u662f\u5f88\u6709\u7528\u7684\u3002 \u63a5\u53e3\u7c7b\u547d\u540d\u901a\u5e38\u4ee5 I \u5f00\u5934\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u63a5\u53e3\u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 class IErrorLog { public : virtual bool openLog ( const char * filename ) = 0 ; virtual bool closeLog () = 0 ; virtual bool writeError ( const char * errorMessage ) = 0 ; virtual ~ IErrorLog () {} // make a virtual destructor in case we delete an IErrorLog pointer, so the proper derived destructor is called }; \u4efb\u4f55\u4ece IErrorLog \u7ee7\u627f\u7684\u7c7b\u90fd\u5fc5\u987b\u5b9e\u73b0\u8fd9\u4e09\u4e2a\u51fd\u6570\uff0c\u4ee5\u4fbf\u88ab\u80fd\u591f\u88ab\u5b9e\u4f8b\u5316\u3002\u4f60\u53ef\u4ee5\u6d3e\u751f\u4e00\u4e2a\u540d\u4e3a FileErrorLog \u7684\u7c7b\uff0c\u5176\u4e2d openLog() \u6253\u5f00\u78c1\u76d8\u4e0a\u7684\u6587\u4ef6\uff0c closeLog() \u5173\u95ed\u6587\u4ef6\uff0c writeError() \u5c06\u6d88\u606f\u5199\u5165\u6587\u4ef6\u3002\u7136\u540e\u53ef\u6d3e\u751f\u53e6\u4e00\u4e2a\u540d\u4e3a ScreenErrorLog \u7684\u7c7b\uff0c\u5176\u4e2d openLog() \u548c closeLog() \u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\uff0c\u800c writeError() \u5728\u5c4f\u5e55\u4e0a\u7684\u5f39\u51fa\u6d88\u606f\u6846\u4e2d\u6253\u5370\u6d88\u606f\u3002 \u73b0\u5728\uff0c\u5047\u8bbe\u4f60\u9700\u8981\u7f16\u5199\u4e00\u4e9b\u4ee3\u7801\u5e76\u9700\u8981\u4f7f\u7528\u9519\u8bef\u65e5\u5fd7\u529f\u80fd\u3002\u5982\u679c\u4f60\u7f16\u5199\u7684\u4ee3\u7801\u76f4\u63a5\u5305\u4f7f\u7528 FileErrorLog \u6216 ScreenErrorLog \uff0c\u90a3\u4e48\u4f60\u5c06 \u53ea\u80fd \u4f7f\u7528\u8fd9\u79cd\u6307\u5b9a\u7684\u9519\u8bef\u65e5\u5fd7\u7c7b\u578b(\u5728\u4e0d\u4fee\u6539\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b)\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u51fd\u6570\u8feb\u4f7f mySqrt() \u7684\u8c03\u7528\u8005\u4f7f\u7528 FileErrorLog \uff0c\u4f46\u8c03\u7528\u8005\u53ef\u80fd\u5e76\u4e0d\u60f3\u4f7f\u7528\u8fd9\u79cd\u9519\u8bef\u65e5\u5fd7\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // for sqrt() double mySqrt ( double value , FileErrorLog & log ) //\u6ce8\u610f\u53c2\u6570\u4f7f\u7528\u7684\u662fFileErrorLog\u7c7b\u578b { if ( value < 0.0 ) { log . writeError ( \"Tried to take square root of value less than 0\" ); return 0.0 ; } else { return std :: sqrt ( value ); } } \u66f4\u52a0\u7075\u6d3b\u7684\u529e\u6cd5\u662f\u5c06\u8be5\u51fd\u6570\u5b9e\u73b0\u4e3a\u4f7f\u7528 IErrorLog \u7c7b\u578b\u7684\u53c2\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include // for sqrt() double mySqrt ( double value , IErrorLog & log ) { if ( value < 0.0 ) { log . writeError ( \"Tried to take square root of value less than 0\" ); return 0.0 ; } else { return std :: sqrt ( value ); } } \u8fd9\u6837\u4e00\u6765\uff0c\u8c03\u7528\u8005\u53ef\u4ee5\u4f20\u5165\u7b26\u5408 IErrorLog \u63a5\u53e3\u7684\u4efb\u4f55\u7c7b\u3002\u5982\u679c\u5b83\u4eec\u5e0c\u671b\u5c06\u9519\u8bef\u65e5\u5fd7\u5199\u5230\u6587\u4ef6\uff0c\u5219\u53ef\u4ee5\u4f20\u5165 FileErrorLog \u7684\u5b9e\u4f8b\u3002\u5982\u679c\u60f3\u9519\u8bef\u4fe1\u606f\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\uff0c\u5219\u53ef\u4ee5\u4f20\u5165\u4e00\u4e2a ScreenErrorLog \u7684\u5b9e\u4f8b\u3002\u5982\u679c\u4ed6\u4eec\u60f3\u505a\u4e00\u4e9b\u4f60\u751a\u81f3\u6ca1\u6709\u60f3\u8fc7\u7684\u4e8b\u60c5\uff0c\u6bd4\u5982\u5728\u51fa\u73b0\u9519\u8bef\u65f6\u5411\u67d0\u4eba\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\uff0c\u5219\u53ef\u4ee5\u4ece IErrorLog (\u4f8b\u5982EmailErrorLog)\u6d3e\u751f\u4e00\u4e2a\u65b0\u7c7b\u5e76\u4f7f\u7528\u5b83\u7684\u5b9e\u4f8b\uff01\u901a\u8fc7\u4f7f\u7528IErrorLog\uff0c\u60a8\u7684\u51fd\u6570\u53d8\u5f97\u66f4\u52a0\u72ec\u7acb\u548c\u7075\u6d3b\u3002 \u4e0d\u8981\u5fd8\u8bb0\u4e3a\u63a5\u53e3\u7c7b\u5b9a\u4e49\u865a\u6790\u6784\u51fd\u6570\uff0c\u4ee5\u4fbf\u5728 \u5220\u9664\u6307\u5411\u63a5\u53e3\u7684\u6307\u9488 \u65f6\u8c03\u7528\u9002\u5f53\u7684\u6d3e\u751f\u6790\u6784\u51fd\u6570\u3002 \u63a5\u53e3\u7c7b\u975e\u5e38\u5e38\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u6613\u4e8e\u4f7f\u7528\u3001\u6613\u4e8e\u6269\u5c55\u548c\u6613\u4e8e\u7ef4\u62a4\u3002\u4e8b\u5b9e\u4e0a\uff0c\u4e00\u4e9b\u73b0\u4ee3\u8bed\u8a00\uff0c\u5982Java\u548cC#\uff0c\u5df2\u7ecf\u6dfb\u52a0\u4e86 interface \u5173\u952e\u5b57\uff0c\u5141\u8bb8\u7a0b\u5e8f\u5458\u76f4\u63a5\u5b9a\u4e49\u63a5\u53e3\u7c7b\uff0c\u800c\u4e0d\u5fc5\u663e\u5f0f\u5730\u5c06\u6240\u6709\u6210\u5458\u51fd\u6570\u6807\u8bb0\u4e3a\u62bd\u8c61\u3002\u6b64\u5916\uff0c\u5c3d\u7ba1Java(\u7248\u672c8\u4e4b\u524d\u7684\u7248\u672c)\u548cC#\u4e0d\u5141\u8bb8\u5bf9\u666e\u901a\u7c7b\u4f7f\u7528\u591a\u91cd\u7ee7\u627f\uff0c\u4f46\u5b83\u4eec\u5141\u8bb8\u60a8\u6839\u636e\u9700\u8981\u5bf9\u4efb\u610f\u591a\u7684\u63a5\u53e3\u8fdb\u884c\u591a\u91cd\u7ee7\u627f\u3002\u56e0\u4e3a\u63a5\u53e3\u6ca1\u6709\u6570\u636e\u4e5f\u6ca1\u6709\u51fd\u6570\u4f53\uff0c\u6240\u4ee5\u5b83\u4eec\u907f\u514d\u4e86\u8bb8\u591a\u4f20\u7edf\u7684\u591a\u91cd\u7ee7\u627f\u95ee\u9898\uff0c\u540c\u65f6\u4ecd\u7136\u63d0\u4f9b\u4e86\u5f88\u5927\u7684\u7075\u6d3b\u6027\u3002","title":"\u63a5\u53e3\u7c7b"},{"location":"18-7-pure-virtual-functions-abstract-base-classes-and-%20interface-classes/#_5","text":"\u62bd\u8c61\u7c7b\u4ecd\u7136\u6709\u865a\u8868\uff0c\u56e0\u4e3a\u5982\u679c\u60a8\u6709\u6307\u5411\u62bd\u8c61\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u8fd9\u4e9b\u865a\u8868\u4ecd\u7136\u6709\u7528 \u3002\u5e26\u6709\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\u7684\u865a\u8868\u9879\u901a\u5e38\u8981\u4e48\u5305\u542b\u7a7a\u6307\u9488\uff0c\u8981\u4e48\u6307\u5411\u6253\u5370\u9519\u8bef\u7684\u6cdb\u578b\u51fd\u6570(\u6709\u65f6\u8be5\u51fd\u6570\u88ab\u547d\u540d\u4e3a __purecall )\u3002","title":"\u7eaf\u865a\u51fd\u6570\u548c\u865a\u8868"},{"location":"18-8-virtual-base-classes/","tags":["virtual-base-classes"],"text":"Key Takeaway \u5728\u4e0a\u4e00\u7ae0 17.9 - \u591a\u91cd\u7ee7\u627f \u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u8c08\u8bba\u4e86 \u83f1\u5f62\u7ee7\u627f\u95ee\u9898 \u3002\u672c\u7ae0\u6211\u4eec\u4f1a\u7ee7\u7eed\u8be5\u8bdd\u9898\u3002 \u6ce8\u610f\uff1a\u672c\u8282\u662f\u9ad8\u7ea7\u4e3b\u9898\uff0c\u53ef\u4ee5\u4f5c\u4e3a\u9009\u4fee\u3002 \u83f1\u5f62\u7ee7\u627f\u95ee\u9898 \u4e0b\u9762\u662f\u6211\u4eec\u4e0a\u4e00\u8bfe\u4e2d\u7684\u4f8b\u5b50(\u591a\u4e86\u4e00\u4e9b\u6784\u9020\u51fd\u6570)\uff0c\u8bf4\u660e\u83f1\u5f62\u7ee7\u627f\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include class PoweredDevice { public : PoweredDevice ( int power ) { std :: cout << \"PoweredDevice: \" << power << '\\n' ; } }; class Scanner : public PoweredDevice { public : Scanner ( int scanner , int power ) : PoweredDevice { power } { std :: cout << \"Scanner: \" << scanner << '\\n' ; } }; class Printer : public PoweredDevice { public : Printer ( int printer , int power ) : PoweredDevice { power } { std :: cout << \"Printer: \" << printer << '\\n' ; } }; class Copier : public Scanner , public Printer { public : Copier ( int scanner , int printer , int power ) : Scanner { scanner , power }, Printer { printer , power } { } }; \u4e5f\u8bb8\u4f60\u4f1a\u8ba4\u4e3a\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u5f97\u5230\u4e0b\u9762\u8fd9\u6837\u7684\u7ee7\u627f\u7ed3\u6784\uff1a \u5982\u679c\u8981\u521b\u5efa\u4e00\u4e2a Copier \u7c7b\u5bf9\u8c61\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u6700\u7ec8\u4f1a\u5f97\u5230 PoweredDevice \u7c7b\u7684\u4e24\u4e2a\u526f\u672c\u2014\u2014\u4e00\u4e2a\u6765\u81ea Printer \uff0c\u4e00\u4e2a\u6765\u81ea Scanner \u3002\u5b83\u6709\u4ee5\u4e0b\u7ed3\u6784: \u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7b80\u77ed\u7684\u793a\u4f8b\u6765\u6f14\u793a\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 int main () { Copier copier { 1 , 2 , 3 }; return 0 ; } 1 2 3 4 PoweredDevice: 3 Scanner: 1 PoweredDevice: 3 Printer: 2 \u770b\u5230\u4e86\u5417\uff1f PoweredDevice \u88ab\u6784\u5efa\u4e86\u4e24\u6b21\u3002 \u867d\u7136\u8fd9\u901a\u5e38\u662f\u671f\u671b\u7684\u7ed3\u679c\uff0c\u4f46\u5728\u6709\u65f6\u4f60\u53ef\u80fd\u53ea\u60f3\u8ba9 Scanner \u548c Printer \u5171\u4eab PoweredDevice \u7684\u4e00\u4e2a\u526f\u672c\u3002 \u865a\u57fa\u7c7b \u8981\u5171\u4eab\u57fa\u7c7b\uff0c\u53ea\u9700\u5728\u6d3e\u751f\u7c7b\u7684\u7ee7\u627f\u5217\u8868\u4e2d\u63d2\u5165\" virtual \"\u5173\u952e\u5b57\u3002\u8fd9\u5c06\u521b\u5efa\u6240\u8c13\u7684 \u865a\u57fa\u7c7b \uff0c\u8fd9\u610f\u5473\u7740\u53ea\u6709\u4e00\u4e2a\u57fa\u7c7b\u5bf9\u8c61\u3002\u57fa\u7c7b\u5bf9\u8c61\u5728\u7ee7\u627f\u6811\u4e2d\u7684\u6240\u6709\u5bf9\u8c61\u4e4b\u95f4\u5171\u4eab\uff0c\u5e76\u4e14\u53ea\u6784\u9020\u4e00\u6b21\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b(\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u6ca1\u6709\u6784\u9020\u51fd\u6570)\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528 virtual \u5173\u952e\u5b57\u521b\u5efa\u5171\u4eab\u57fa\u7c7b: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class PoweredDevice { }; class Scanner : virtual public PoweredDevice { }; class Printer : virtual public PoweredDevice { }; class Copier : public Scanner , public Printer { }; \u73b0\u5728\uff0c\u5f53\u4f60\u521b\u5efa\u4e00\u4e2a Copier \u7c7b\u5bf9\u8c61\u65f6\uff0c\u6bcf\u4e2a Copier \u5c06\u53ea\u83b7\u5f97\u4e00\u4e2a\u7531 Scanner \u548c Printer \u5171\u4eab\u7684 PoweredDevice \u526f\u672c\u3002 \u4f46\u662f\uff0c\u8fd9\u53c8\u5bfc\u81f4\u4e86\u53e6\u4e00\u4e2a\u95ee\u9898\uff1a\u5982\u679c Scanner \u548c Printer \u5171\u4eab\u4e00\u4e2a PoweredDevice \u57fa\u7c7b\uff0c\u90a3\u4e48\u8c01\u8d1f\u8d23\u521b\u5efa\u5b83\u5462\uff1f\u4e8b\u5b9e\u8bc1\u660e\uff0c\u7b54\u6848\u662f Copier \u3002 Copier \u7684\u6784\u9020\u51fd\u6570\u8d1f\u8d23\u521b\u5efa PoweredDevice \u3002\u56e0\u6b64\uff0c Copier \u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u975e\u76f4\u63a5\u7236\u7c7b(non-immediate-parent\u6784\u9020\u51fd\u6570\u4e00\u6b21\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include class PoweredDevice { public : PoweredDevice ( int power ) { std :: cout << \"PoweredDevice: \" << power << '\\n' ; } }; class Scanner : virtual public PoweredDevice // note: PoweredDevice is now a virtual base class { public : Scanner ( int scanner , int power ) : PoweredDevice { power } // this line is required to create Scanner objects, but ignored in this case { std :: cout << \"Scanner: \" << scanner << '\\n' ; } }; class Printer : virtual public PoweredDevice // note: PoweredDevice is now a virtual base class { public : Printer ( int printer , int power ) : PoweredDevice { power } // this line is required to create Printer objects, but ignored in this case { std :: cout << \"Printer: \" << printer << '\\n' ; } }; class Copier : public Scanner , public Printer { public : Copier ( int scanner , int printer , int power ) : PoweredDevice { power }, // PoweredDevice is constructed here Scanner { scanner , power }, Printer { printer , power } { } }; \u518d\u770b\u524d\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 int main () { Copier copier { 1 , 2 , 3 }; return 0 ; } \u7ed3\u679c\uff1a 1 2 3 PoweredDevice: 3 Scanner: 1 Printer: 2 \u5982\u60a8\u6240\u89c1\uff0c PoweredDevice \u53ea\u6784\u9020\u4e00\u6b21\u3002 \u6709\u51e0\u4e2a\u5bb9\u6613\u5ffd\u89c6\u7684\u7ec6\u8282\uff1a \u9996\u5148\uff0c\u865a\u57fa\u7c7b\u603b\u662f\u5728\u975e\u865a\u57fa\u7c7b\u4e4b\u524d\u521b\u5efa\uff0c\u8fd9\u786e\u4fdd\u4e86\u6240\u6709\u57fa\u7c7b\u90fd\u5728\u5b83\u4eec\u7684\u6d3e\u751f\u7c7b\u4e4b\u524d\u521b\u5efa\u3002 \u5176\u6b21\uff0c\u8bf7\u6ce8\u610f Scanner \u548c Printer \u6784\u9020\u51fd\u6570\u4ecd\u7136\u6709\u5bf9 PoweredDevice \u6784\u9020\u51fd\u6570\u7684\u8c03\u7528\u3002\u5728\u521b\u5efa Copier \u7684\u5b9e\u4f8b\u65f6\uff0c\u8fd9\u4e9b\u6784\u9020\u51fd\u6570\u8c03\u7528\u5c06\u88ab\u5ffd\u7565\uff0c\u56e0\u4e3a Copier \u8d1f\u8d23\u521b\u5efa PoweredDevice \uff0c\u800c\u4e0d\u662f Scanner \u6216 Printer \u3002\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u8981\u521b\u5efa Scanner \u6216 Printer \u7684\u5b9e\u4f8b\uff0c\u5c31\u4f1a\u4f7f\u7528\u8fd9\u4e9b\u6784\u9020\u51fd\u6570\u8c03\u7528\uff0c\u5e76\u5e94\u7528\u6b63\u5e38\u7684\u7ee7\u627f\u89c4\u5219\u3002 \u7b2c\u4e09\uff0c\u5982\u679c\u4e00\u4e2a\u7c7b\u7ee7\u627f\u4e86\u4e00\u4e2a\u6216\u591a\u4e2a\u5177\u6709\u865a\u7236\u7c7b\u7684\u7c7b\uff0c\u5219\u6700\u540e\u88ab\u6d3e\u751f\u7684\u7c7b\u4f1a\u8d1f\u8d23\u6784\u9020\u865a\u57fa\u7c7b\u3002\u5728\u672c\u4f8b\u4e2d\uff0c Copier \u7ee7\u627f\u4e86 Printer \u548c Scanner \uff0c\u5b83\u4eec\u90fd\u6709\u4e00\u4e2a PoweredDevice \u865a\u57fa\u7c7b\u3002\u6700\u6700\u540e\u88ab\u6d3e\u751f\u7684\u7c7b\uff0c\u8d1f\u8d23 PoweredDevice \u7684\u521b\u5efa\u3002\u6ce8\u610f\uff0c\u5373\u4f7f\u5728\u5355\u4e00\u7ee7\u627f\u7684\u60c5\u51b5\u4e0b\u4e5f\u662f\u8fd9\u6837\uff1a\u5982\u679c Copier \u4ece Printer \u5355\u72ec\u7ee7\u627f\uff0c\u800c Printer \u4ece PoweredDevice \u865a\u7ee7\u627f\uff0c\u90a3\u4e48 Copier \u4ecd\u7136\u8d1f\u8d23\u521b\u5efa PoweredDevice \u3002 \u7b2c\u56db\uff0c\u6240\u6709\u7ee7\u627f\u865a\u57fa\u7c7b\u7684\u7c7b\u90fd\u5c06\u6709\u4e00\u4e2a\u865a\u8868\uff0c\u5373\u4f7f\u5b83\u4eec\u901a\u5e38\u6ca1\u6709\u865a\u8868\uff0c\u56e0\u6b64\u7c7b\u7684\u5b9e\u4f8b\u5176\u5927\u5c0f\u4f1a\u589e\u52a0\u4e00\u4e2a\u6307\u9488\u3002 \u56e0\u4e3a Scanner \u548c Printer \u5b9e\u9645\u4e0a\u662f\u4ece PoweredDevice \u6d3e\u751f\u7684\uff0c\u6240\u4ee5 Copier \u53ea\u662f\u4e00\u4e2a PoweredDevice \u5b50\u5bf9\u8c61(subobject)\u3002 Scanner \u548c Printer \u90fd\u9700\u8981\u77e5\u9053\u5982\u4f55\u627e\u5230\u5355\u4e2a PoweredDevice \u5b50\u5bf9\u8c61\uff0c\u8fd9\u6837\u5b83\u4eec\u624d\u80fd\u8bbf\u95ee\u5b83\u7684\u6210\u5458(\u56e0\u4e3a\u5b83\u4eec\u6bd5\u7adf\u662f\u4ece\u5b83\u6d3e\u751f\u7684)\u3002\u8fd9\u901a\u5e38\u662f\u901a\u8fc7\u4e00\u4e9b\u865a\u8868\u64cd\u4f5c\u6765\u5b8c\u6210\u7684(\u5b83\u5b9e\u9645\u4e0a\u5b58\u50a8\u4e86\u4ece\u6bcf\u4e2a\u5b50\u7c7b\u5230 PoweredDevice \u5b50\u5bf9\u8c61\u7684\u504f\u79fb\u91cf)\u3002","title":"18.8 - \u865a\u57fa\u7c7b"},{"location":"18-8-virtual-base-classes/#_1","text":"\u4e0b\u9762\u662f\u6211\u4eec\u4e0a\u4e00\u8bfe\u4e2d\u7684\u4f8b\u5b50(\u591a\u4e86\u4e00\u4e9b\u6784\u9020\u51fd\u6570)\uff0c\u8bf4\u660e\u83f1\u5f62\u7ee7\u627f\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include class PoweredDevice { public : PoweredDevice ( int power ) { std :: cout << \"PoweredDevice: \" << power << '\\n' ; } }; class Scanner : public PoweredDevice { public : Scanner ( int scanner , int power ) : PoweredDevice { power } { std :: cout << \"Scanner: \" << scanner << '\\n' ; } }; class Printer : public PoweredDevice { public : Printer ( int printer , int power ) : PoweredDevice { power } { std :: cout << \"Printer: \" << printer << '\\n' ; } }; class Copier : public Scanner , public Printer { public : Copier ( int scanner , int printer , int power ) : Scanner { scanner , power }, Printer { printer , power } { } }; \u4e5f\u8bb8\u4f60\u4f1a\u8ba4\u4e3a\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u5f97\u5230\u4e0b\u9762\u8fd9\u6837\u7684\u7ee7\u627f\u7ed3\u6784\uff1a \u5982\u679c\u8981\u521b\u5efa\u4e00\u4e2a Copier \u7c7b\u5bf9\u8c61\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u6700\u7ec8\u4f1a\u5f97\u5230 PoweredDevice \u7c7b\u7684\u4e24\u4e2a\u526f\u672c\u2014\u2014\u4e00\u4e2a\u6765\u81ea Printer \uff0c\u4e00\u4e2a\u6765\u81ea Scanner \u3002\u5b83\u6709\u4ee5\u4e0b\u7ed3\u6784: \u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7b80\u77ed\u7684\u793a\u4f8b\u6765\u6f14\u793a\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 int main () { Copier copier { 1 , 2 , 3 }; return 0 ; } 1 2 3 4 PoweredDevice: 3 Scanner: 1 PoweredDevice: 3 Printer: 2 \u770b\u5230\u4e86\u5417\uff1f PoweredDevice \u88ab\u6784\u5efa\u4e86\u4e24\u6b21\u3002 \u867d\u7136\u8fd9\u901a\u5e38\u662f\u671f\u671b\u7684\u7ed3\u679c\uff0c\u4f46\u5728\u6709\u65f6\u4f60\u53ef\u80fd\u53ea\u60f3\u8ba9 Scanner \u548c Printer \u5171\u4eab PoweredDevice \u7684\u4e00\u4e2a\u526f\u672c\u3002","title":"\u83f1\u5f62\u7ee7\u627f\u95ee\u9898"},{"location":"18-8-virtual-base-classes/#_2","text":"\u8981\u5171\u4eab\u57fa\u7c7b\uff0c\u53ea\u9700\u5728\u6d3e\u751f\u7c7b\u7684\u7ee7\u627f\u5217\u8868\u4e2d\u63d2\u5165\" virtual \"\u5173\u952e\u5b57\u3002\u8fd9\u5c06\u521b\u5efa\u6240\u8c13\u7684 \u865a\u57fa\u7c7b \uff0c\u8fd9\u610f\u5473\u7740\u53ea\u6709\u4e00\u4e2a\u57fa\u7c7b\u5bf9\u8c61\u3002\u57fa\u7c7b\u5bf9\u8c61\u5728\u7ee7\u627f\u6811\u4e2d\u7684\u6240\u6709\u5bf9\u8c61\u4e4b\u95f4\u5171\u4eab\uff0c\u5e76\u4e14\u53ea\u6784\u9020\u4e00\u6b21\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b(\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u6ca1\u6709\u6784\u9020\u51fd\u6570)\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528 virtual \u5173\u952e\u5b57\u521b\u5efa\u5171\u4eab\u57fa\u7c7b: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class PoweredDevice { }; class Scanner : virtual public PoweredDevice { }; class Printer : virtual public PoweredDevice { }; class Copier : public Scanner , public Printer { }; \u73b0\u5728\uff0c\u5f53\u4f60\u521b\u5efa\u4e00\u4e2a Copier \u7c7b\u5bf9\u8c61\u65f6\uff0c\u6bcf\u4e2a Copier \u5c06\u53ea\u83b7\u5f97\u4e00\u4e2a\u7531 Scanner \u548c Printer \u5171\u4eab\u7684 PoweredDevice \u526f\u672c\u3002 \u4f46\u662f\uff0c\u8fd9\u53c8\u5bfc\u81f4\u4e86\u53e6\u4e00\u4e2a\u95ee\u9898\uff1a\u5982\u679c Scanner \u548c Printer \u5171\u4eab\u4e00\u4e2a PoweredDevice \u57fa\u7c7b\uff0c\u90a3\u4e48\u8c01\u8d1f\u8d23\u521b\u5efa\u5b83\u5462\uff1f\u4e8b\u5b9e\u8bc1\u660e\uff0c\u7b54\u6848\u662f Copier \u3002 Copier \u7684\u6784\u9020\u51fd\u6570\u8d1f\u8d23\u521b\u5efa PoweredDevice \u3002\u56e0\u6b64\uff0c Copier \u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u975e\u76f4\u63a5\u7236\u7c7b(non-immediate-parent\u6784\u9020\u51fd\u6570\u4e00\u6b21\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include class PoweredDevice { public : PoweredDevice ( int power ) { std :: cout << \"PoweredDevice: \" << power << '\\n' ; } }; class Scanner : virtual public PoweredDevice // note: PoweredDevice is now a virtual base class { public : Scanner ( int scanner , int power ) : PoweredDevice { power } // this line is required to create Scanner objects, but ignored in this case { std :: cout << \"Scanner: \" << scanner << '\\n' ; } }; class Printer : virtual public PoweredDevice // note: PoweredDevice is now a virtual base class { public : Printer ( int printer , int power ) : PoweredDevice { power } // this line is required to create Printer objects, but ignored in this case { std :: cout << \"Printer: \" << printer << '\\n' ; } }; class Copier : public Scanner , public Printer { public : Copier ( int scanner , int printer , int power ) : PoweredDevice { power }, // PoweredDevice is constructed here Scanner { scanner , power }, Printer { printer , power } { } }; \u518d\u770b\u524d\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 int main () { Copier copier { 1 , 2 , 3 }; return 0 ; } \u7ed3\u679c\uff1a 1 2 3 PoweredDevice: 3 Scanner: 1 Printer: 2 \u5982\u60a8\u6240\u89c1\uff0c PoweredDevice \u53ea\u6784\u9020\u4e00\u6b21\u3002 \u6709\u51e0\u4e2a\u5bb9\u6613\u5ffd\u89c6\u7684\u7ec6\u8282\uff1a \u9996\u5148\uff0c\u865a\u57fa\u7c7b\u603b\u662f\u5728\u975e\u865a\u57fa\u7c7b\u4e4b\u524d\u521b\u5efa\uff0c\u8fd9\u786e\u4fdd\u4e86\u6240\u6709\u57fa\u7c7b\u90fd\u5728\u5b83\u4eec\u7684\u6d3e\u751f\u7c7b\u4e4b\u524d\u521b\u5efa\u3002 \u5176\u6b21\uff0c\u8bf7\u6ce8\u610f Scanner \u548c Printer \u6784\u9020\u51fd\u6570\u4ecd\u7136\u6709\u5bf9 PoweredDevice \u6784\u9020\u51fd\u6570\u7684\u8c03\u7528\u3002\u5728\u521b\u5efa Copier \u7684\u5b9e\u4f8b\u65f6\uff0c\u8fd9\u4e9b\u6784\u9020\u51fd\u6570\u8c03\u7528\u5c06\u88ab\u5ffd\u7565\uff0c\u56e0\u4e3a Copier \u8d1f\u8d23\u521b\u5efa PoweredDevice \uff0c\u800c\u4e0d\u662f Scanner \u6216 Printer \u3002\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u8981\u521b\u5efa Scanner \u6216 Printer \u7684\u5b9e\u4f8b\uff0c\u5c31\u4f1a\u4f7f\u7528\u8fd9\u4e9b\u6784\u9020\u51fd\u6570\u8c03\u7528\uff0c\u5e76\u5e94\u7528\u6b63\u5e38\u7684\u7ee7\u627f\u89c4\u5219\u3002 \u7b2c\u4e09\uff0c\u5982\u679c\u4e00\u4e2a\u7c7b\u7ee7\u627f\u4e86\u4e00\u4e2a\u6216\u591a\u4e2a\u5177\u6709\u865a\u7236\u7c7b\u7684\u7c7b\uff0c\u5219\u6700\u540e\u88ab\u6d3e\u751f\u7684\u7c7b\u4f1a\u8d1f\u8d23\u6784\u9020\u865a\u57fa\u7c7b\u3002\u5728\u672c\u4f8b\u4e2d\uff0c Copier \u7ee7\u627f\u4e86 Printer \u548c Scanner \uff0c\u5b83\u4eec\u90fd\u6709\u4e00\u4e2a PoweredDevice \u865a\u57fa\u7c7b\u3002\u6700\u6700\u540e\u88ab\u6d3e\u751f\u7684\u7c7b\uff0c\u8d1f\u8d23 PoweredDevice \u7684\u521b\u5efa\u3002\u6ce8\u610f\uff0c\u5373\u4f7f\u5728\u5355\u4e00\u7ee7\u627f\u7684\u60c5\u51b5\u4e0b\u4e5f\u662f\u8fd9\u6837\uff1a\u5982\u679c Copier \u4ece Printer \u5355\u72ec\u7ee7\u627f\uff0c\u800c Printer \u4ece PoweredDevice \u865a\u7ee7\u627f\uff0c\u90a3\u4e48 Copier \u4ecd\u7136\u8d1f\u8d23\u521b\u5efa PoweredDevice \u3002 \u7b2c\u56db\uff0c\u6240\u6709\u7ee7\u627f\u865a\u57fa\u7c7b\u7684\u7c7b\u90fd\u5c06\u6709\u4e00\u4e2a\u865a\u8868\uff0c\u5373\u4f7f\u5b83\u4eec\u901a\u5e38\u6ca1\u6709\u865a\u8868\uff0c\u56e0\u6b64\u7c7b\u7684\u5b9e\u4f8b\u5176\u5927\u5c0f\u4f1a\u589e\u52a0\u4e00\u4e2a\u6307\u9488\u3002 \u56e0\u4e3a Scanner \u548c Printer \u5b9e\u9645\u4e0a\u662f\u4ece PoweredDevice \u6d3e\u751f\u7684\uff0c\u6240\u4ee5 Copier \u53ea\u662f\u4e00\u4e2a PoweredDevice \u5b50\u5bf9\u8c61(subobject)\u3002 Scanner \u548c Printer \u90fd\u9700\u8981\u77e5\u9053\u5982\u4f55\u627e\u5230\u5355\u4e2a PoweredDevice \u5b50\u5bf9\u8c61\uff0c\u8fd9\u6837\u5b83\u4eec\u624d\u80fd\u8bbf\u95ee\u5b83\u7684\u6210\u5458(\u56e0\u4e3a\u5b83\u4eec\u6bd5\u7adf\u662f\u4ece\u5b83\u6d3e\u751f\u7684)\u3002\u8fd9\u901a\u5e38\u662f\u901a\u8fc7\u4e00\u4e9b\u865a\u8868\u64cd\u4f5c\u6765\u5b8c\u6210\u7684(\u5b83\u5b9e\u9645\u4e0a\u5b58\u50a8\u4e86\u4ece\u6bcf\u4e2a\u5b50\u7c7b\u5230 PoweredDevice \u5b50\u5bf9\u8c61\u7684\u504f\u79fb\u91cf)\u3002","title":"\u865a\u57fa\u7c7b"},{"location":"18-9-object-slicing/","tags":["virtual-base-classes"],"text":"Key Takeaway \u6309\u503c\u4f20\u9012 \u5bf9\u8c61\u65f6\uff0c\u5982\u679c\u5f62\u53c2\u662f\u57fa\u7c7b\uff0c\u5b9e\u53c2\u662f\u6d3e\u751f\u7c7b\uff0c\u5219\u4f1a\u88ab\u5207\u7247\uff0c\u5bfc\u81f4\u6d3e\u751f\u7c7b\u4e2d\u865a\u51fd\u6570\u65e0\u6cd5\u8c03\u7528\u3002\u6700\u4fdd\u9669\u7684\u662f \u6309\u5f15\u7528\u4f20\u9012 \uff0c\u6b64\u65f6\u5373\u4f7f\u8f6c\u6362\u6210\u4e86\u57fa\u7c7b\u7684\u5f15\u7528\uff0c\u4e5f\u662f\u4e0d\u59a8\u788d\u865a\u51fd\u6570\u5de5\u4f5c\u3002 \u786e\u4fdd\u51fd\u6570\u5f62\u53c2\u662f\u5f15\u7528(\u6216\u6307\u9488)\uff0c\u5e76\u5728\u6d3e\u751f\u7c7b\u4e2d\u5c3d\u91cf\u907f\u514d\u4efb\u4f55\u5f62\u5f0f\u7684\u503c\u4f20\u9012\u3002 \u8ba9\u6211\u4eec\u56de\u5230\u6211\u4eec\u4e4b\u524d\u7684\u4e00\u4e2a\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include class Base { protected : int m_value {}; public : Base ( int value ) : m_value { value } { } virtual const char * getName () const { return \"Base\" ; } int getValue () const { return m_value ; } }; class Derived : public Base { public : Derived ( int value ) : Base { value } { } const char * getName () const override { return \"Derived\" ; } }; int main () { Derived derived { 5 }; std :: cout << \"derived is a \" << derived . getName () << \" and has value \" << derived . getValue () << '\\n' ; Base & ref { derived }; std :: cout << \"ref is a \" << ref . getName () << \" and has value \" << ref . getValue () << '\\n' ; Base * ptr { & derived }; std :: cout << \"ptr is a \" << ptr -> getName () << \" and has value \" << ptr -> getValue () << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c ref \u5f15\u7528\u548c ptr \u6307\u5411 derived \uff0c\u5b83\u6709\u4e00\u4e2a Base \u90e8\u5206\u548c\u4e00\u4e2a derived \u90e8\u5206\u3002\u56e0\u4e3a ref \u548c ptr \u662f Base \u7c7b\u578b\uff0c\u6240\u4ee5 ref \u548c ptr \u53ea\u80fd\u770b\u5230 derived \u7684 Base \u90e8\u5206\u2014\u2014 derived \u7684 derived \u90e8\u5206\u4ecd\u7136\u5b58\u5728\uff0c\u4f46\u4e0d\u80fd\u901a\u8fc7 ref \u6216 ptr \u770b\u5230\u3002\u4f46\u662f\uff0c\u901a\u8fc7\u4f7f\u7528\u865a\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbf\u95ee\u51fd\u6570\u7684\u6700\u540e\u6d3e\u751f\u7684\u7248\u672c\u3002\u56e0\u6b64\uff0c\u4e0a\u9762\u7684\u7a0b\u5e8f\u8f93\u51fa: 1 2 3 derived is a Derived and has value 5 ref is a Derived and has value 5 ptr is a Derived and has value 5 \u4f46\u662f\uff0c\u5982\u679c Base \u4e0d\u5f15\u7528\u6216\u6307\u9488\u6307\u5411 Derived \u5bf9\u8c61\uff0c\u800c\u662f\u7b80\u5355\u5730\u5c06 Derived \u5bf9\u8c61 \u8d4b\u503c \u7ed9 Base \u5bf9\u8c61\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u60c5\u51b5\u5462? 1 2 3 4 5 6 7 8 int main () { Derived derived { 5 }; Base base { derived }; // what happens here? std :: cout << \"base is a \" << base . getName () << \" and has value \" << base . getValue () << '\\n' ; return 0 ; } derived \u5305\u542b\u4e00\u4e2a Base \u90e8\u5206\u548c Derived \u90e8\u5206\u3002\u5f53\u5c06 Derived \u5bf9\u8c61\u8d4b\u503c\u7ed9 Base \u5bf9\u8c61\u65f6\uff0c\u53ea\u4f1a\u590d\u5236\u6d3e\u751f\u5bf9\u8c61\u4e2d\u7684 Base \u90e8\u5206\uff0c\u800c Derived \u5219\u4e0d\u4f1a\u88ab\u8d4b\u503c\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c base \u63a5\u6536\u4e86 derived \u7684 base \u90e8\u5206\u7684\u526f\u672c\uff0c\u4f46\u4e0d\u4f1a\u83b7\u53d6 derived \u90e8\u5206\u7684\u526f\u672c\uff0c\u4e5f\u5c31\u662f\u8bf4 Derived \u90e8\u5206\u5b9e\u9645\u4e0a\u5df2\u7ecf\u88ab\u201c\u5207\u6389\u4e86\u201d\u3002\u56e0\u6b64\uff0c\u5c06\u6d3e\u751f\u7c7b\u5bf9\u8c61\u8d4b\u503c\u7ed9\u57fa\u7c7b\u5bf9\u8c61\u79f0\u4e3a \u5bf9\u8c61\u5207\u7247 (\u6216\u7b80\u79f0\u5207\u7247)\u3002 \u56e0\u4e3a base \u53d8\u91cf\u6ca1\u6709 Derived \u90e8\u5206\uff0c base.getName() \u53ea\u80fd\u89e3\u6790\u4e3a base::getName() \u3002 \u4e0a\u9762\u7684\u7a0b\u5e8f\u6253\u5370\u51fa\uff1a 1 base is a Base and has value 5 \u4f7f\u7528\u5f97\u5f53\u7684\u8bdd\uff0c\u5bf9\u8c61\u5207\u7247\u662f\u5f88\u6709\u7528\u7684\u3002\u7136\u800c\uff0c\u5982\u679c\u4f7f\u7528\u4e0d\u5f53\uff0c\u5207\u7247\u4f1a\u4ee5\u4e0d\u540c\u7684\u65b9\u5f0f\u5bfc\u81f4\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\u3002\u8ba9\u6211\u4eec\u6765\u770b\u770b\u5176\u4e2d\u7684\u4e00\u4e9b\u6848\u4f8b\u3002 \u5207\u7247\u548c\u51fd\u6570 \u73b0\u5728\uff0c\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u4e0a\u9762\u7684\u4f8b\u5b50\u6709\u70b9\u50bb\u3002\u6bd5\u7adf\uff0c\u4e3a\u4ec0\u4e48\u8981\u8fd9\u6837\u628a derived \u8d4b\u503c\u7ed9 base \u5462\uff1f\u4e00\u822c\u5f88\u5c11\u4f1a\u8fd9\u4e48\u505a\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u51fd\u6570\u6765\u8bf4\uff0c\u5bf9\u8c61\u5207\u7247\u662f\u53ef\u80fd\u5728\u4e0d\u7ecf\u610f\u95f4\u53d1\u751f\u7684\u3002 \u8003\u8651\u4e0b\u9762\u7684\u51fd\u6570\uff1a 1 2 3 4 void printName ( const Base base ) // \u6ce8\u610f: base \u6309\u503c\u4f20\u9012\uff0c\u800c\u4e0d\u662f\u5f15\u7528 { std :: cout << \"I am a \" << base . getName () << '\\n' ; } \u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u51fd\u6570\uff0c\u5e26\u6709\u4e00\u4e2a \u6309\u503c\u4f20\u9012 \u7684 const base \u5bf9\u8c61\u5f62\u53c2\u3002\u5982\u679c\u6211\u4eec\u50cf\u8fd9\u6837\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 int main () { Derived d { 5 }; printName ( d ); // oops, \u6ca1\u6709\u610f\u8bc6\u5230\u5728\u5b9e\u9645\u8c03\u7528\u65f6\u662f\u6309\u503c\u4f20\u9012\u7684 return 0 ; } \u5728\u7f16\u5199\u8fd9\u4e2a\u7a0b\u5e8f\u65f6\uff0c\u60a8\u53ef\u80fd\u6ca1\u6709\u6ce8\u610f\u5230 base \u662f\u4e00\u4e2a\u503c\u5f62\u53c2\uff0c\u800c\u4e0d\u662f\u5f15\u7528\u3002\u56e0\u6b64\uff0c\u5f53 printName(d) \u8c03\u7528\u65f6\uff0c\u867d\u7136\u6211\u4eec\u53ef\u80fd\u671f\u671b base.getName() \u8c03\u7528\u865a\u51fd\u6570 getName() \u5e76\u6253\u5370\u201cI am a Derived\u201d\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u3002\u76f8\u53cd\uff0c Derived \u5bf9\u8c61 d \u88ab\u5207\u7247\uff0c\u53ea\u6709 Base \u90e8\u5206\u88ab\u590d\u5236\u5230 base \u53c2\u6570\u4e2d\u3002\u5f53 base.getName() \u6267\u884c\u65f6\uff0c\u5373\u4f7f\u5bf9 getName() \u51fd\u6570\u8fdb\u884c\u4e86\u865a\u5316\uff0c\u4e5f\u6ca1\u6709\u7c7b\u7684 Derived \u90e8\u5206\u4f9b\u5b83\u89e3\u6790\u3002\u56e0\u6b64\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u8f93\u51fa\uff1a 1 I am a Base \u8fd9\u91cc\u53ef\u4ee5\u660e\u663e\u770b\u5230\u95ee\u9898\uff0c\u4f46\u5982\u679c\u51fd\u6570\u6ca1\u6709\u50cf\u8fd9\u6837\u5b9e\u9645\u6253\u5370\u4efb\u4f55\u4fe1\u606f\uff0c\u90a3\u4e48\u8be5\u95ee\u9898\u4f1a\u975e\u5e38\u96be\u4ee5\u5b9a\u4f4d\u3002 \u5f53\u7136\uff0c \u901a\u8fc7\u5c06\u51fd\u6570\u5f62\u53c2\u4f5c\u4e3a\u5f15\u7528\u800c\u4e0d\u662f\u6309\u503c\u4f20\u9012\uff0c\u5c31\u53ef\u4ee5\u907f\u514d\u5207\u7247(\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u6309\u5f15\u7528\u800c\u4e0d\u662f\u6309\u503c\u4f20\u9012\u7c7b\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\u7684\u53e6\u4e00\u4e2a\u539f\u56e0)\u3002 1 2 3 4 5 6 7 8 9 10 11 12 void printName ( const Base & base ) // note: base now passed by reference { std :: cout << \"I am a \" << base . getName () << '\\n' ; } int main () { Derived d { 5 }; printName ( d ); return 0 ; } COPY This prints: 1 I am a Derived vector \u5207\u7247 \u65b0\u7a0b\u5e8f\u5458\u65f6\u5e38\u5728\u4f7f\u7528 std::vector \u5b9e\u73b0\u591a\u6001\u65f6\u9047\u5230\u95ee\u9898\u3002\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { std :: vector < Base > v {}; v . push_back ( Base { 5 }); // \u6dfb\u52a0\u4e00\u4e2a Base \u5bf9\u8c61\u5230 vector v . push_back ( Derived { 6 }); // \u6dfb\u52a0\u4e00\u4e2a Derived \u5bf9\u8c61\u5230 vector // \u6253\u5370 vector \u4e2d\u7684\u6240\u6709\u5143\u7d20 for ( const auto & element : v ) std :: cout << \"I am a \" << element . getName () << \" with value \" << element . getValue () << '\\n' ; return 0 ; } \u7f16\u8bd1\u8fd0\u884c\u7a0b\u5e8f\uff0c\u8f93\u51fa\uff1a 1 2 I am a Base with value 5 I am a Base with value 6 \u548c\u4e4b\u524d\u7684\u4f8b\u5b50\u7c7b\u4f3c\uff0c\u56e0\u4e3a std::vector \u88ab\u58f0\u660e\u4e3a\u4e86 Base \u7c7b\u578b\u7684\u5bb9\u5668\uff0c\u5f53\u6dfb\u52a0 Derived(6) \u65f6\uff0c\u5b83\u88ab\u5207\u7247\u4e86\u3002 \u4fee\u590d\u8fd9\u4e2a\u95ee\u9898\u6709\u70b9\u9ebb\u70e6\u3002\u8bb8\u591a\u65b0\u7a0b\u5e8f\u5458\u6700\u5148\u60f3\u5230\u521b\u5efa\u4e00\u4e2a\u7c7b\u578b\u4e3a\u57fa\u7c7b\u5f15\u7528\u7684 std::vector \uff0c\u50cf\u8fd9\u6837\uff1a 1 std :: vector < Base &> v {}; \u53ef\u60dc\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u6839\u672c\u5c31\u4e0d\u80fd\u7f16\u8bd1\u3002 std::vector \u4e2d\u7684\u5bf9\u8c61\u662f\u5fc5\u987b\u53ef\u4ee5\u8d4b\u503c\u7684\uff0c\u663e\u7136 \u5de6\u503c\u5f15\u7528 \u4e0d\u6ee1\u8db3\u8981\u6c42\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u552f\u4e00\u529e\u6cd5\u662f\u5c06\u7c7b\u578b\u5b9a\u4e49\u4e3a\u57fa\u7c7b\u7684\u6307\u9488\u7c7b\u578b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include int main () { std :: vector < Base *> v {}; Base b { 5 }; // b \u548c d \u4e0d\u80fd\u662f\u533f\u540d\u5bf9\u8c61\uff08\u6307\u9488\u4e0d\u80fd\u6307\u5411\u533f\u540d\u5bf9\u8c61\uff09 Derived d { 6 }; v . push_back ( & b ); // add a Base object to our vector v . push_back ( & d ); // add a Derived object to our vector // Print out all of the elements in our vector for ( const auto * element : v ) std :: cout << \"I am a \" << element -> getName () << \" with value \" << element -> getValue () << '\\n' ; return 0 ; } \u6253\u5370\uff1a 1 2 I am a Base with value 5 I am a Derived with value 6 \u641e\u5b9a\uff01\u5bf9\u6b64\u6709\u51e0\u70b9\u8fd8\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0cnullptr \u73b0\u5728\u4e5f\u53ef\u4ee5\u88ab\u5b58\u8fdb\u53bb\uff0c\u8fd9\u53ef\u80fd\u662f\u4f60\u60f3\u8981\u7684\u7ed3\u679c\u4e5f\u53ef\u80fd\u4e0d\u662f\u3002\u5176\u6b21\uff0c\u4f60\u73b0\u5728\u5fc5\u987b\u5904\u7406\u6307\u9488\u8bed\u4e49\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5f88\u9ebb\u70e6\u3002\u597d\u7684\u65b9\u9762\u662f\uff0c\u8fd9\u6837\u4e00\u6765\u5c31\u53ef\u4ee5\u4f7f\u7528\u52a8\u6001\u5185\u5b58\u5206\u914d\uff0c\u5982\u679c\u5bf9\u8c61\u9700\u8981\u8d85\u51fa\u4f5c\u7528\u57df\uff0c\u8fd9\u662f\u5f88\u6709\u7528\u7684\u3002 \u53e6\u4e00\u4e2a\u9009\u62e9\u662f\u4f7f\u7528 std::reference_wrapper \uff0c\u5b83\u662f\u4e00\u4e2a\u7c7b\uff0c\u53ef\u6a21\u62df\u51fa\u53ef\u8d4b\u503c\u7684\u5f15\u7528\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include // for std::reference_wrapper #include #include class Base { protected : int m_value {}; public : Base ( int value ) : m_value { value } { } virtual const char * getName () const { return \"Base\" ; } int getValue () const { return m_value ; } }; class Derived : public Base { public : Derived ( int value ) : Base { value } { } const char * getName () const override { return \"Derived\" ; } }; int main () { std :: vector < std :: reference_wrapper < Base >> v {}; // \u5b58\u653e Base \u7684\u53ef\u8d4b\u503c\u5f15\u7528\u7684\u5bb9\u5668 Base b { 5 }; // b and d can't be anonymous objects Derived d { 6 }; v . push_back ( b ); // add a Base object to our vector v . push_back ( d ); // add a Derived object to our vector // Print out all of the elements in our vector // we use .get() to get our element out of the std::reference_wrapper for ( const auto & element : v ) // element has type const std::reference_wrapper& std :: cout << \"I am a \" << element . get (). getName () << \" with value \" << element . get (). getValue () << '\\n' ; return 0 ; } \u7f1d\u5408\u602a\u5bf9\u8c61 Frankenobject \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u770b\u5230\u4e86\u5bf9\u8c61\u5207\u7247\u53ef\u80fd\u5bfc\u81f4\u9519\u8bef\u7684\u60c5\u51b5\uff08\u7531\u4e8e\u6d3e\u751f\u7c7b\u88ab\u5207\u6389\u6240\u5f15\u8d77\uff09\u3002\u73b0\u5728\u8ba9\u6211\u4eec\u6765\u770b\u53e6\u4e00\u79cd\u5371\u9669\u7684\u60c5\u51b5\uff0c\u5176\u4e2d\u6d3e\u751f\u5bf9\u8c61\u4ecd\u7136\u5b58\u5728\uff01 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { Derived d1 { 5 }; Derived d2 { 6 }; Base & b { d2 }; b = d1 ; // \u95ee\u9898\u6240\u5728\uff08\u901a\u8fc7b\u7ed9d2\u8d4b\u503c\u4e3ad1\uff0c\u6ce8\u610f\u8fd9\u91cc\u4e0d\u662f\u7ed9\u5f15\u7528\u91cd\u65b0\u8d4b\u503c\uff09 return 0 ; } \u51fd\u6570\u7684\u524d\u4e09\u884c\u975e\u5e38\u7b80\u5355\u3002\u521b\u5efa\u4e24\u4e2a Derived \u5bf9\u8c61\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u7b2c\u4e8c\u4e2a\u5bf9\u8c61\u7684 Base \u7c7b\u578b\u5f15\u7528\u3002 \u7b2c\u56db\u884c\u662f\u51fa\u9519\u7684\u5730\u65b9\u3002\u56e0\u4e3a b \u6307\u5411 d2 \uff0c\u6211\u4eec\u628a d1 \u8d4b\u503c\u7ed9 b \uff08\u76f8\u5f53\u4e8e d2=d1 \uff09\uff0c\u4f60\u53ef\u80fd\u8ba4\u4e3a\u7ed3\u679c\u662f d1 \u4f1a\u88ab\u590d\u5236\u5230 d2 \u4e2d\u3002\u4f46\u662f b \u662f Base , \u800cC++\u4e3a\u7c7b\u63d0\u4f9b\u7684\u64cd\u4f5c\u7b26=\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4e0d\u662f\u865a\u51fd\u6570\u7684\u3002\u56e0\u6b64\uff0c\u53ea\u6709 d1 \u7684 Base \u90e8\u5206\u88ab\u590d\u5236\u5230 d2 \u4e2d\u3002 \u5c31\u662f d2 \u73b0\u5728\u6709 d1 \u7684\u57fa\u90e8\u5206\u548c d2 \u7684\u6d3e\u751f\u90e8\u5206\u3002\u5728\u8fd9\u4e2a\u7279\u5b9a\u7684\u4f8b\u5b50\u4e2d\uff0c\u8fd9\u4e0d\u662f\u95ee\u9898(\u56e0\u4e3a Derived \u7c7b\u672c\u8eab\u6ca1\u6709\u6570\u636e)\uff0c\u4f46\u662f\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f60\u5b9e\u9645\u521b\u5efa\u4e00\u4e2a \u7f1d\u5408\u602a\u5bf9\u8c61 \u2014\u2014\u7531\u591a\u4e2a\u5bf9\u8c61\u7684\u90e8\u5206\u7ec4\u6210\u3002\u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u6ca1\u6709\u4ec0\u4e48\u7b80\u5355\u7684\u65b9\u6cd5\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u7684\u53d1\u751f(\u53ea\u80fd\u5c3d\u91cf\u907f\u514d\u8fd9\u6837\u7684\u8d4b\u503c)\u3002 \u7ed3\u8bba \u5c3d\u7ba1C++\u652f\u6301\u901a\u8fc7\u5bf9\u8c61\u5207\u7247\u5c06\u6d3e\u751f\u5bf9\u8c61\u5206\u914d\u7ed9\u57fa\u5bf9\u8c61\uff0c\u4f46\u8fd9\u4e48\u505a\u591a\u6570\u60c5\u51b5\u4e0b\u4f1a\u5e26\u6765\u95ee\u9898\uff0c\u56e0\u6b64\u5e94\u8be5\u5c3d\u91cf\u907f\u514d\u5207\u7247\u3002 \u786e\u4fdd\u51fd\u6570\u5f62\u53c2\u662f\u5f15\u7528(\u6216\u6307\u9488)\uff0c\u5e76\u5728\u6d3e\u751f\u7c7b\u4e2d\u5c3d\u91cf\u907f\u514d\u4efb\u4f55\u5f62\u5f0f\u7684\u503c\u4f20\u9012\u3002","title":"18.9 - \u5bf9\u8c61\u5207\u7247"},{"location":"18-9-object-slicing/#_1","text":"\u73b0\u5728\uff0c\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u4e0a\u9762\u7684\u4f8b\u5b50\u6709\u70b9\u50bb\u3002\u6bd5\u7adf\uff0c\u4e3a\u4ec0\u4e48\u8981\u8fd9\u6837\u628a derived \u8d4b\u503c\u7ed9 base \u5462\uff1f\u4e00\u822c\u5f88\u5c11\u4f1a\u8fd9\u4e48\u505a\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u51fd\u6570\u6765\u8bf4\uff0c\u5bf9\u8c61\u5207\u7247\u662f\u53ef\u80fd\u5728\u4e0d\u7ecf\u610f\u95f4\u53d1\u751f\u7684\u3002 \u8003\u8651\u4e0b\u9762\u7684\u51fd\u6570\uff1a 1 2 3 4 void printName ( const Base base ) // \u6ce8\u610f: base \u6309\u503c\u4f20\u9012\uff0c\u800c\u4e0d\u662f\u5f15\u7528 { std :: cout << \"I am a \" << base . getName () << '\\n' ; } \u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u51fd\u6570\uff0c\u5e26\u6709\u4e00\u4e2a \u6309\u503c\u4f20\u9012 \u7684 const base \u5bf9\u8c61\u5f62\u53c2\u3002\u5982\u679c\u6211\u4eec\u50cf\u8fd9\u6837\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 int main () { Derived d { 5 }; printName ( d ); // oops, \u6ca1\u6709\u610f\u8bc6\u5230\u5728\u5b9e\u9645\u8c03\u7528\u65f6\u662f\u6309\u503c\u4f20\u9012\u7684 return 0 ; } \u5728\u7f16\u5199\u8fd9\u4e2a\u7a0b\u5e8f\u65f6\uff0c\u60a8\u53ef\u80fd\u6ca1\u6709\u6ce8\u610f\u5230 base \u662f\u4e00\u4e2a\u503c\u5f62\u53c2\uff0c\u800c\u4e0d\u662f\u5f15\u7528\u3002\u56e0\u6b64\uff0c\u5f53 printName(d) \u8c03\u7528\u65f6\uff0c\u867d\u7136\u6211\u4eec\u53ef\u80fd\u671f\u671b base.getName() \u8c03\u7528\u865a\u51fd\u6570 getName() \u5e76\u6253\u5370\u201cI am a Derived\u201d\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u3002\u76f8\u53cd\uff0c Derived \u5bf9\u8c61 d \u88ab\u5207\u7247\uff0c\u53ea\u6709 Base \u90e8\u5206\u88ab\u590d\u5236\u5230 base \u53c2\u6570\u4e2d\u3002\u5f53 base.getName() \u6267\u884c\u65f6\uff0c\u5373\u4f7f\u5bf9 getName() \u51fd\u6570\u8fdb\u884c\u4e86\u865a\u5316\uff0c\u4e5f\u6ca1\u6709\u7c7b\u7684 Derived \u90e8\u5206\u4f9b\u5b83\u89e3\u6790\u3002\u56e0\u6b64\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u8f93\u51fa\uff1a 1 I am a Base \u8fd9\u91cc\u53ef\u4ee5\u660e\u663e\u770b\u5230\u95ee\u9898\uff0c\u4f46\u5982\u679c\u51fd\u6570\u6ca1\u6709\u50cf\u8fd9\u6837\u5b9e\u9645\u6253\u5370\u4efb\u4f55\u4fe1\u606f\uff0c\u90a3\u4e48\u8be5\u95ee\u9898\u4f1a\u975e\u5e38\u96be\u4ee5\u5b9a\u4f4d\u3002 \u5f53\u7136\uff0c \u901a\u8fc7\u5c06\u51fd\u6570\u5f62\u53c2\u4f5c\u4e3a\u5f15\u7528\u800c\u4e0d\u662f\u6309\u503c\u4f20\u9012\uff0c\u5c31\u53ef\u4ee5\u907f\u514d\u5207\u7247(\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u6309\u5f15\u7528\u800c\u4e0d\u662f\u6309\u503c\u4f20\u9012\u7c7b\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\u7684\u53e6\u4e00\u4e2a\u539f\u56e0)\u3002 1 2 3 4 5 6 7 8 9 10 11 12 void printName ( const Base & base ) // note: base now passed by reference { std :: cout << \"I am a \" << base . getName () << '\\n' ; } int main () { Derived d { 5 }; printName ( d ); return 0 ; } COPY This prints: 1 I am a Derived","title":"\u5207\u7247\u548c\u51fd\u6570"},{"location":"18-9-object-slicing/#vector","text":"\u65b0\u7a0b\u5e8f\u5458\u65f6\u5e38\u5728\u4f7f\u7528 std::vector \u5b9e\u73b0\u591a\u6001\u65f6\u9047\u5230\u95ee\u9898\u3002\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { std :: vector < Base > v {}; v . push_back ( Base { 5 }); // \u6dfb\u52a0\u4e00\u4e2a Base \u5bf9\u8c61\u5230 vector v . push_back ( Derived { 6 }); // \u6dfb\u52a0\u4e00\u4e2a Derived \u5bf9\u8c61\u5230 vector // \u6253\u5370 vector \u4e2d\u7684\u6240\u6709\u5143\u7d20 for ( const auto & element : v ) std :: cout << \"I am a \" << element . getName () << \" with value \" << element . getValue () << '\\n' ; return 0 ; } \u7f16\u8bd1\u8fd0\u884c\u7a0b\u5e8f\uff0c\u8f93\u51fa\uff1a 1 2 I am a Base with value 5 I am a Base with value 6 \u548c\u4e4b\u524d\u7684\u4f8b\u5b50\u7c7b\u4f3c\uff0c\u56e0\u4e3a std::vector \u88ab\u58f0\u660e\u4e3a\u4e86 Base \u7c7b\u578b\u7684\u5bb9\u5668\uff0c\u5f53\u6dfb\u52a0 Derived(6) \u65f6\uff0c\u5b83\u88ab\u5207\u7247\u4e86\u3002 \u4fee\u590d\u8fd9\u4e2a\u95ee\u9898\u6709\u70b9\u9ebb\u70e6\u3002\u8bb8\u591a\u65b0\u7a0b\u5e8f\u5458\u6700\u5148\u60f3\u5230\u521b\u5efa\u4e00\u4e2a\u7c7b\u578b\u4e3a\u57fa\u7c7b\u5f15\u7528\u7684 std::vector \uff0c\u50cf\u8fd9\u6837\uff1a 1 std :: vector < Base &> v {}; \u53ef\u60dc\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u6839\u672c\u5c31\u4e0d\u80fd\u7f16\u8bd1\u3002 std::vector \u4e2d\u7684\u5bf9\u8c61\u662f\u5fc5\u987b\u53ef\u4ee5\u8d4b\u503c\u7684\uff0c\u663e\u7136 \u5de6\u503c\u5f15\u7528 \u4e0d\u6ee1\u8db3\u8981\u6c42\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u552f\u4e00\u529e\u6cd5\u662f\u5c06\u7c7b\u578b\u5b9a\u4e49\u4e3a\u57fa\u7c7b\u7684\u6307\u9488\u7c7b\u578b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include int main () { std :: vector < Base *> v {}; Base b { 5 }; // b \u548c d \u4e0d\u80fd\u662f\u533f\u540d\u5bf9\u8c61\uff08\u6307\u9488\u4e0d\u80fd\u6307\u5411\u533f\u540d\u5bf9\u8c61\uff09 Derived d { 6 }; v . push_back ( & b ); // add a Base object to our vector v . push_back ( & d ); // add a Derived object to our vector // Print out all of the elements in our vector for ( const auto * element : v ) std :: cout << \"I am a \" << element -> getName () << \" with value \" << element -> getValue () << '\\n' ; return 0 ; } \u6253\u5370\uff1a 1 2 I am a Base with value 5 I am a Derived with value 6 \u641e\u5b9a\uff01\u5bf9\u6b64\u6709\u51e0\u70b9\u8fd8\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0cnullptr \u73b0\u5728\u4e5f\u53ef\u4ee5\u88ab\u5b58\u8fdb\u53bb\uff0c\u8fd9\u53ef\u80fd\u662f\u4f60\u60f3\u8981\u7684\u7ed3\u679c\u4e5f\u53ef\u80fd\u4e0d\u662f\u3002\u5176\u6b21\uff0c\u4f60\u73b0\u5728\u5fc5\u987b\u5904\u7406\u6307\u9488\u8bed\u4e49\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5f88\u9ebb\u70e6\u3002\u597d\u7684\u65b9\u9762\u662f\uff0c\u8fd9\u6837\u4e00\u6765\u5c31\u53ef\u4ee5\u4f7f\u7528\u52a8\u6001\u5185\u5b58\u5206\u914d\uff0c\u5982\u679c\u5bf9\u8c61\u9700\u8981\u8d85\u51fa\u4f5c\u7528\u57df\uff0c\u8fd9\u662f\u5f88\u6709\u7528\u7684\u3002 \u53e6\u4e00\u4e2a\u9009\u62e9\u662f\u4f7f\u7528 std::reference_wrapper \uff0c\u5b83\u662f\u4e00\u4e2a\u7c7b\uff0c\u53ef\u6a21\u62df\u51fa\u53ef\u8d4b\u503c\u7684\u5f15\u7528\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include // for std::reference_wrapper #include #include class Base { protected : int m_value {}; public : Base ( int value ) : m_value { value } { } virtual const char * getName () const { return \"Base\" ; } int getValue () const { return m_value ; } }; class Derived : public Base { public : Derived ( int value ) : Base { value } { } const char * getName () const override { return \"Derived\" ; } }; int main () { std :: vector < std :: reference_wrapper < Base >> v {}; // \u5b58\u653e Base \u7684\u53ef\u8d4b\u503c\u5f15\u7528\u7684\u5bb9\u5668 Base b { 5 }; // b and d can't be anonymous objects Derived d { 6 }; v . push_back ( b ); // add a Base object to our vector v . push_back ( d ); // add a Derived object to our vector // Print out all of the elements in our vector // we use .get() to get our element out of the std::reference_wrapper for ( const auto & element : v ) // element has type const std::reference_wrapper& std :: cout << \"I am a \" << element . get (). getName () << \" with value \" << element . get (). getValue () << '\\n' ; return 0 ; }","title":"vector \u5207\u7247"},{"location":"18-9-object-slicing/#frankenobject","text":"\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u770b\u5230\u4e86\u5bf9\u8c61\u5207\u7247\u53ef\u80fd\u5bfc\u81f4\u9519\u8bef\u7684\u60c5\u51b5\uff08\u7531\u4e8e\u6d3e\u751f\u7c7b\u88ab\u5207\u6389\u6240\u5f15\u8d77\uff09\u3002\u73b0\u5728\u8ba9\u6211\u4eec\u6765\u770b\u53e6\u4e00\u79cd\u5371\u9669\u7684\u60c5\u51b5\uff0c\u5176\u4e2d\u6d3e\u751f\u5bf9\u8c61\u4ecd\u7136\u5b58\u5728\uff01 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { Derived d1 { 5 }; Derived d2 { 6 }; Base & b { d2 }; b = d1 ; // \u95ee\u9898\u6240\u5728\uff08\u901a\u8fc7b\u7ed9d2\u8d4b\u503c\u4e3ad1\uff0c\u6ce8\u610f\u8fd9\u91cc\u4e0d\u662f\u7ed9\u5f15\u7528\u91cd\u65b0\u8d4b\u503c\uff09 return 0 ; } \u51fd\u6570\u7684\u524d\u4e09\u884c\u975e\u5e38\u7b80\u5355\u3002\u521b\u5efa\u4e24\u4e2a Derived \u5bf9\u8c61\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u7b2c\u4e8c\u4e2a\u5bf9\u8c61\u7684 Base \u7c7b\u578b\u5f15\u7528\u3002 \u7b2c\u56db\u884c\u662f\u51fa\u9519\u7684\u5730\u65b9\u3002\u56e0\u4e3a b \u6307\u5411 d2 \uff0c\u6211\u4eec\u628a d1 \u8d4b\u503c\u7ed9 b \uff08\u76f8\u5f53\u4e8e d2=d1 \uff09\uff0c\u4f60\u53ef\u80fd\u8ba4\u4e3a\u7ed3\u679c\u662f d1 \u4f1a\u88ab\u590d\u5236\u5230 d2 \u4e2d\u3002\u4f46\u662f b \u662f Base , \u800cC++\u4e3a\u7c7b\u63d0\u4f9b\u7684\u64cd\u4f5c\u7b26=\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4e0d\u662f\u865a\u51fd\u6570\u7684\u3002\u56e0\u6b64\uff0c\u53ea\u6709 d1 \u7684 Base \u90e8\u5206\u88ab\u590d\u5236\u5230 d2 \u4e2d\u3002 \u5c31\u662f d2 \u73b0\u5728\u6709 d1 \u7684\u57fa\u90e8\u5206\u548c d2 \u7684\u6d3e\u751f\u90e8\u5206\u3002\u5728\u8fd9\u4e2a\u7279\u5b9a\u7684\u4f8b\u5b50\u4e2d\uff0c\u8fd9\u4e0d\u662f\u95ee\u9898(\u56e0\u4e3a Derived \u7c7b\u672c\u8eab\u6ca1\u6709\u6570\u636e)\uff0c\u4f46\u662f\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f60\u5b9e\u9645\u521b\u5efa\u4e00\u4e2a \u7f1d\u5408\u602a\u5bf9\u8c61 \u2014\u2014\u7531\u591a\u4e2a\u5bf9\u8c61\u7684\u90e8\u5206\u7ec4\u6210\u3002\u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u6ca1\u6709\u4ec0\u4e48\u7b80\u5355\u7684\u65b9\u6cd5\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u7684\u53d1\u751f(\u53ea\u80fd\u5c3d\u91cf\u907f\u514d\u8fd9\u6837\u7684\u8d4b\u503c)\u3002","title":"\u7f1d\u5408\u602a\u5bf9\u8c61 Frankenobject"},{"location":"18-9-object-slicing/#_2","text":"\u5c3d\u7ba1C++\u652f\u6301\u901a\u8fc7\u5bf9\u8c61\u5207\u7247\u5c06\u6d3e\u751f\u5bf9\u8c61\u5206\u914d\u7ed9\u57fa\u5bf9\u8c61\uff0c\u4f46\u8fd9\u4e48\u505a\u591a\u6570\u60c5\u51b5\u4e0b\u4f1a\u5e26\u6765\u95ee\u9898\uff0c\u56e0\u6b64\u5e94\u8be5\u5c3d\u91cf\u907f\u514d\u5207\u7247\u3002 \u786e\u4fdd\u51fd\u6570\u5f62\u53c2\u662f\u5f15\u7528(\u6216\u6307\u9488)\uff0c\u5e76\u5728\u6d3e\u751f\u7c7b\u4e2d\u5c3d\u91cf\u907f\u514d\u4efb\u4f55\u5f62\u5f0f\u7684\u503c\u4f20\u9012\u3002","title":"\u7ed3\u8bba"},{"location":"18-x-Chapter-18-comprehensive-quiz/","tags":["summary"],"text":"\u590d\u4e60 18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 C++ \u5141\u8bb8\u4f60\u5c06\u57fa\u7c7b\u7c7b\u578b\u7684\u6307\u9488\u6216\u5f15\u7528\u6307\u5411\u6d3e\u751f\u7c7b\u7684\u5bf9\u8c61\u3002\u5f53\u6211\u4eec\u9700\u8981\u7f16\u5199\u4ee3\u7801\u4f7f\u5176\u80fd\u591f\u914d\u5408\u4ece\u540c\u4e00\u4e2a\u57fa\u7c7b\u7ee7\u627f\u7684\u6d3e\u751f\u7c7b\u5de5\u4f5c\u65f6\uff0c\u8fd9\u4e2a\u7279\u6027\u975e\u5e38\u6709\u7528\uff1b 18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001 \u5982\u679c\u6ca1\u6709\u865a\u51fd\u6570\u7684\u5e2e\u52a9\uff0c\u6307\u5411\u6d3e\u751f\u7c7b\u7684\u57fa\u7c7b\u6307\u9488\u548c\u5f15\u7528\u5c31\u53ea\u80fd\u8bbf\u95ee\u57fa\u7c7b\u7684\u6210\u5458\u53d8\u91cf\u548c\u57fa\u7c7b\u4e2d\u5bf9\u5e94\u7684\u51fd\u6570\uff1b \u865a\u51fd\u6570\u662f\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\u7684\u51fd\u6570\uff0c\u5b83\u4f1a\u89e3\u6790\u4e3a\u57fa\u7c7b\u548c\u6d3e\u751f\u7c7b\u4e2d\u5171\u540c\u5b58\u5728\u7684\u6700\u540e\u6d3e\u751f\u7684\u51fd\u6570\uff08\u79f0\u4e3a \u91cd\u8f7d \uff09\u3002\u4e3a\u4e86\u5b9e\u73b0\u91cd\u8f7d\u3002\u6d3e\u751f\u7c7b\u7684\u51fd\u6570\u548c\u57fa\u7c7b\u4e2d\u7684\u865a\u51fd\u6570\u5fc5\u987b\u5177\u6709\u76f8\u540c\u7684\u51fd\u6570\u7b7e\u540d\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u3002\u4e00\u4e2a\u4f8b\u5916\u7684\u60c5\u51b5\u662f \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b \uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u91cd\u8f7d\u51fd\u6570\u7684\u8fd4\u56de\u503c\u53ef\u4ee5\u8fd4\u56de\u6d3e\u751f\u7c7b\u7c7b\u578b\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u800c\u4e0d\u5fc5\u548c\u57fa\u7c7b\u4e2d\u7684\u51fd\u6570\u4e00\u6837\u4e5f\u8fd4\u56de\u57fa\u7c7b\u7c7b\u578b\u7684\u6307\u9488\u548c\u5f15\u7528\uff1b 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b \u5982\u679c\u4e00\u4e2a\u51fd\u6570\u7684\u76ee\u7684\u662f\u88ab\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\u91cd\u8f7d\uff0c\u6211\u4eec\u5e94\u8be5\u4f7f\u7528 override \u6765\u4fee\u9970\u5b83\u4ee5\u4fbf\u786e\u4fdd\u5b83\u88ab\u91cd\u8f7d\uff1b final \u5173\u952e\u5b57\u53ef\u4ee5\u7528\u6765\u6307\u660e\u67d0\u4e2a\u51fd\u6570\u4e0d\u53ef\u4ee5\u88ab\u91cd\u8f7d\u6216\u67d0\u4e2a\u7c7b\u4e0d\u80fd\u88ab\u7ee7\u627f\uff1b 18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316 \u5982\u679c\u4e00\u4e2a\u7c7b\u7684\u529f\u80fd\u662f\u4f5c\u4e3a\u57fa\u7c7b\u4f7f\u7528\uff0c\u90a3\u5e94\u8be5\u5c06\u5176\u6790\u6784\u51fd\u6570\u8bbe\u4e3a\u865a\u51fd\u6570\uff0c\u8fd9\u6837\u624d\u80fd\u786e\u4fdd\u4e00\u4e2a\u6307\u5411\u6d3e\u751f\u7c7b\u7684\u57fa\u7c7b\u7c7b\u578b\u7684\u6307\u9488\u88ab delete \u7684\u65f6\u5019\uff0c\u80fd\u591f\u8c03\u7528\u6b63\u786e\u7684\u6790\u6784\u51fd\u6570\uff08\u5b50\u7c7b\u7684\u6790\u6784\u51fd\u6570\uff09\uff1b \u4f7f\u7528 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u53ef\u4ee5\u89c4\u907f\u865a\u51fd\u6570\u89e3\u6790\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u663e\u5f0f\u5730\u6307\u5b9a\u4f60\u5e0c\u671b\u4f7f\u7528\u54ea\u4e2a\u7c7b\u4e2d\u7684\u51fd\u6570\uff1a\u4f8b\u5982\uff1a base.Base::getName() \uff1b 18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a \u5f53\u7f16\u8bd1\u5668\u89e3\u6790\u5230\u51fd\u6570\u7684\u76f4\u63a5\u8c03\u7528\u65f6\uff0c\u4f1a\u8fdb\u884c \u65e9\u671f\u7ed1\u5b9a \u3002\u7f16\u8bd1\u5668\u6216\u8005\u94fe\u63a5\u5668\u53ef\u4ee5\u76f4\u63a5\u89e3\u6790\u8be5\u51fd\u6570\u8c03\u7528\u3002 \u540e\u671f\u7ed1\u5b9a \u5219\u53d1\u751f\u5728\u901a\u8fc7\u51fd\u6570\u6307\u9488\u8fdb\u884c\u8c03\u7528\u65f6\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5b9e\u9645\u7684\u51fd\u6570\u8c03\u7528\u53ea\u6709\u5728\u8fd0\u884c\u65f6\u624d\u80fd\u786e\u5b9a\u3002\u865a\u51fd\u6570\u4f7f\u7528 \u540e\u671f\u7ed1\u5b9a \u548c\u865a\u51fd\u6570\u8868\u6765\u786e\u5b9a\u5b9e\u9645\u8c03\u7528\u7684\u51fd\u6570\u3002 18.6 - \u865a\u51fd\u6570\u8868 \u4f7f\u7528\u865a\u51fd\u6570\u662f\u6709\u5f00\u9500\u7684\uff1a\u865a\u51fd\u6570\u7684\u8c03\u7528\u66f4\u8017\u65f6\uff0c\u800c\u4e14\u865a\u51fd\u6570\u8868\u4e5f\u4f1a\u4f7f\u6bcf\u4e00\u4e2a\u5305\u542b\u865a\u51fd\u6570\u7684\u5bf9\u8c61\u5176\u5927\u5c0f\u589e\u52a0\u4e00\u4e2a\u6307\u9488\u7684\u5927\u5c0f\u3002 18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b \u865a\u51fd\u6570\u53ef\u4ee5\u88ab\u5b9a\u4e49\u4e3a \u7eaf\u865a\u51fd\u6570 \u6216\u62bd\u8c61\u51fd\u6570\uff0c\u53ea\u9700\u5728\u5176\u51fd\u6570\u539f\u578b\u7684\u7ed3\u5c3e\u6dfb\u52a0\u201d=0\u201c\u5373\u53ef\u3002\u5305\u542b\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\u79f0\u4e3a \u62bd\u8c61\u7c7b \uff0c\u5b83 \u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\u3002\u4e00\u4e2a\u7ee7\u627f\u4e86\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\uff0c\u5fc5\u987b\u5b9e\u73b0\u8fd9\u4e9b\u865a\u51fd\u6570\uff0c\u5426\u5219\u8be5\u7c7b\u4e5f\u662f\u62bd\u8c61\u7c7b\u3002\u7eaf\u865a\u51fd\u6570\u4e5f\u53ef\u4ee5\u6709\u51fd\u6570\u4f53\uff0c\u4f46\u5373\u4fbf\u8fd9\u6837\u5b83\u4ecd\u7136\u662f\u62bd\u8c61\u7684\u3002 \u63a5\u53e3\u7c7b \u662f\u90a3\u4e9b\u6ca1\u6709\u6210\u5458\u53d8\u91cf\uff0c\u4e14\u6240\u6709\u51fd\u6570\u5747\u4e3a \u7eaf\u865a\u51fd\u6570 \u7684\u7c7b\uff0c\u901a\u5e38\u5728\u547d\u540d\u5b83\u4eec\u7684\u65f6\u5019\u4f1a\u4f7f\u7528\u5927\u5199\u5b57\u6bcdI\u5f00\u5934\u3002 18.8 - \u865a\u57fa\u7c7b \u865a\u57fa\u7c7b \u662f\u4e00\u79cd\u57fa\u7c7b\uff0c\u4e0d\u8bba\u5b83\u88ab\u7ee7\u627f\u591a\u5c11\u6b21\uff0c\u5b83\u59cb\u7ec8\u53ea\u4f1a\u88ab\u6784\u9020\u4e00\u6b21\u3002 18.9 - \u5bf9\u8c61\u5207\u7247 \u5f53\u4e00\u4e2a\u6d3e\u751f\u7c7b\u88ab\u8d4b\u503c\u7ed9\u4e00\u4e2a\u57fa\u7c7b\u5bf9\u8c61\u65f6\uff0c\u57fa\u7c7b\u53ea\u4f1a\u5f97\u5230\u4e00\u4efd\u5305\u542b\u4e86\u6d3e\u751f\u7c7b\u4e2d\u57fa\u7c7b\u90e8\u5206\u7684\u62f7\u8d1d\u3002\u8fd9\u4e2a\u73b0\u8c61\u88ab\u79f0\u4e3a \u5bf9\u8c61\u5207\u7247 \u3002 \u6240\u4ee5\u51fd\u6570 \u6309\u503c\u4f20\u9012 \u6d3e\u751f\u5bf9\u8c61\u65f6\u4f1a\u51fa\u73b0\u5207\u7247\uff0c\u5bfc\u81f4\u6d3e\u751f\u7c7b\u865a\u51fd\u6570\u6ca1\u529e\u6cd5\u88ab\u5b9e\u9645\u8c03\u7528 18.10 - \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u53ef\u4ee5\u7528\u6765\u5c06\u4e00\u4e2a\u6307\u5411\u57fa\u7c7b\u5bf9\u8c61\u7684\u6307\u9488\u8f6c\u6362\u4e3a\u6307\u5411\u6d3e\u751f\u7c7b\u5bf9\u8c61\u7684\u6307\u9488\uff0c\u5373 downcasting \u3002\u5982\u679c\u8f6c\u6362\u5931\u8d25\uff0c\u5219\u4f1a\u8fd4\u56de\u4e00\u4e2a\u7a7a\u6307\u9488\u3002 18.11 - \u4f7f\u7528<<\u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b \u4e3a\u6d3e\u751f\u7c7b\u91cd\u8f7d << \u8fd0\u7b97\u7b26\u7684\u6700\u7b80\u5355\u7684\u529e\u6cd5\uff0c\u662f\u91cd\u8f7d\u6700\u57fa\u7840\u7684\u57fa\u7c7b\u7684 << \u8fd0\u7b97\u7b26\uff0c\u7136\u540e\u8c03\u7528\u865a\u6210\u5458\u51fd\u6570\u6765\u8fdb\u884c\u6253\u5370\u3002","title":"18.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u865a\u51fd\u6570"},{"location":"18-x-Chapter-18-comprehensive-quiz/#_1","text":"18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 C++ \u5141\u8bb8\u4f60\u5c06\u57fa\u7c7b\u7c7b\u578b\u7684\u6307\u9488\u6216\u5f15\u7528\u6307\u5411\u6d3e\u751f\u7c7b\u7684\u5bf9\u8c61\u3002\u5f53\u6211\u4eec\u9700\u8981\u7f16\u5199\u4ee3\u7801\u4f7f\u5176\u80fd\u591f\u914d\u5408\u4ece\u540c\u4e00\u4e2a\u57fa\u7c7b\u7ee7\u627f\u7684\u6d3e\u751f\u7c7b\u5de5\u4f5c\u65f6\uff0c\u8fd9\u4e2a\u7279\u6027\u975e\u5e38\u6709\u7528\uff1b 18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001 \u5982\u679c\u6ca1\u6709\u865a\u51fd\u6570\u7684\u5e2e\u52a9\uff0c\u6307\u5411\u6d3e\u751f\u7c7b\u7684\u57fa\u7c7b\u6307\u9488\u548c\u5f15\u7528\u5c31\u53ea\u80fd\u8bbf\u95ee\u57fa\u7c7b\u7684\u6210\u5458\u53d8\u91cf\u548c\u57fa\u7c7b\u4e2d\u5bf9\u5e94\u7684\u51fd\u6570\uff1b \u865a\u51fd\u6570\u662f\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\u7684\u51fd\u6570\uff0c\u5b83\u4f1a\u89e3\u6790\u4e3a\u57fa\u7c7b\u548c\u6d3e\u751f\u7c7b\u4e2d\u5171\u540c\u5b58\u5728\u7684\u6700\u540e\u6d3e\u751f\u7684\u51fd\u6570\uff08\u79f0\u4e3a \u91cd\u8f7d \uff09\u3002\u4e3a\u4e86\u5b9e\u73b0\u91cd\u8f7d\u3002\u6d3e\u751f\u7c7b\u7684\u51fd\u6570\u548c\u57fa\u7c7b\u4e2d\u7684\u865a\u51fd\u6570\u5fc5\u987b\u5177\u6709\u76f8\u540c\u7684\u51fd\u6570\u7b7e\u540d\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u3002\u4e00\u4e2a\u4f8b\u5916\u7684\u60c5\u51b5\u662f \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b \uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u91cd\u8f7d\u51fd\u6570\u7684\u8fd4\u56de\u503c\u53ef\u4ee5\u8fd4\u56de\u6d3e\u751f\u7c7b\u7c7b\u578b\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u800c\u4e0d\u5fc5\u548c\u57fa\u7c7b\u4e2d\u7684\u51fd\u6570\u4e00\u6837\u4e5f\u8fd4\u56de\u57fa\u7c7b\u7c7b\u578b\u7684\u6307\u9488\u548c\u5f15\u7528\uff1b 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b \u5982\u679c\u4e00\u4e2a\u51fd\u6570\u7684\u76ee\u7684\u662f\u88ab\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\u91cd\u8f7d\uff0c\u6211\u4eec\u5e94\u8be5\u4f7f\u7528 override \u6765\u4fee\u9970\u5b83\u4ee5\u4fbf\u786e\u4fdd\u5b83\u88ab\u91cd\u8f7d\uff1b final \u5173\u952e\u5b57\u53ef\u4ee5\u7528\u6765\u6307\u660e\u67d0\u4e2a\u51fd\u6570\u4e0d\u53ef\u4ee5\u88ab\u91cd\u8f7d\u6216\u67d0\u4e2a\u7c7b\u4e0d\u80fd\u88ab\u7ee7\u627f\uff1b 18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316 \u5982\u679c\u4e00\u4e2a\u7c7b\u7684\u529f\u80fd\u662f\u4f5c\u4e3a\u57fa\u7c7b\u4f7f\u7528\uff0c\u90a3\u5e94\u8be5\u5c06\u5176\u6790\u6784\u51fd\u6570\u8bbe\u4e3a\u865a\u51fd\u6570\uff0c\u8fd9\u6837\u624d\u80fd\u786e\u4fdd\u4e00\u4e2a\u6307\u5411\u6d3e\u751f\u7c7b\u7684\u57fa\u7c7b\u7c7b\u578b\u7684\u6307\u9488\u88ab delete \u7684\u65f6\u5019\uff0c\u80fd\u591f\u8c03\u7528\u6b63\u786e\u7684\u6790\u6784\u51fd\u6570\uff08\u5b50\u7c7b\u7684\u6790\u6784\u51fd\u6570\uff09\uff1b \u4f7f\u7528 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u53ef\u4ee5\u89c4\u907f\u865a\u51fd\u6570\u89e3\u6790\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u663e\u5f0f\u5730\u6307\u5b9a\u4f60\u5e0c\u671b\u4f7f\u7528\u54ea\u4e2a\u7c7b\u4e2d\u7684\u51fd\u6570\uff1a\u4f8b\u5982\uff1a base.Base::getName() \uff1b 18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a \u5f53\u7f16\u8bd1\u5668\u89e3\u6790\u5230\u51fd\u6570\u7684\u76f4\u63a5\u8c03\u7528\u65f6\uff0c\u4f1a\u8fdb\u884c \u65e9\u671f\u7ed1\u5b9a \u3002\u7f16\u8bd1\u5668\u6216\u8005\u94fe\u63a5\u5668\u53ef\u4ee5\u76f4\u63a5\u89e3\u6790\u8be5\u51fd\u6570\u8c03\u7528\u3002 \u540e\u671f\u7ed1\u5b9a \u5219\u53d1\u751f\u5728\u901a\u8fc7\u51fd\u6570\u6307\u9488\u8fdb\u884c\u8c03\u7528\u65f6\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5b9e\u9645\u7684\u51fd\u6570\u8c03\u7528\u53ea\u6709\u5728\u8fd0\u884c\u65f6\u624d\u80fd\u786e\u5b9a\u3002\u865a\u51fd\u6570\u4f7f\u7528 \u540e\u671f\u7ed1\u5b9a \u548c\u865a\u51fd\u6570\u8868\u6765\u786e\u5b9a\u5b9e\u9645\u8c03\u7528\u7684\u51fd\u6570\u3002 18.6 - \u865a\u51fd\u6570\u8868 \u4f7f\u7528\u865a\u51fd\u6570\u662f\u6709\u5f00\u9500\u7684\uff1a\u865a\u51fd\u6570\u7684\u8c03\u7528\u66f4\u8017\u65f6\uff0c\u800c\u4e14\u865a\u51fd\u6570\u8868\u4e5f\u4f1a\u4f7f\u6bcf\u4e00\u4e2a\u5305\u542b\u865a\u51fd\u6570\u7684\u5bf9\u8c61\u5176\u5927\u5c0f\u589e\u52a0\u4e00\u4e2a\u6307\u9488\u7684\u5927\u5c0f\u3002 18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b \u865a\u51fd\u6570\u53ef\u4ee5\u88ab\u5b9a\u4e49\u4e3a \u7eaf\u865a\u51fd\u6570 \u6216\u62bd\u8c61\u51fd\u6570\uff0c\u53ea\u9700\u5728\u5176\u51fd\u6570\u539f\u578b\u7684\u7ed3\u5c3e\u6dfb\u52a0\u201d=0\u201c\u5373\u53ef\u3002\u5305\u542b\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\u79f0\u4e3a \u62bd\u8c61\u7c7b \uff0c\u5b83 \u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\u3002\u4e00\u4e2a\u7ee7\u627f\u4e86\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\uff0c\u5fc5\u987b\u5b9e\u73b0\u8fd9\u4e9b\u865a\u51fd\u6570\uff0c\u5426\u5219\u8be5\u7c7b\u4e5f\u662f\u62bd\u8c61\u7c7b\u3002\u7eaf\u865a\u51fd\u6570\u4e5f\u53ef\u4ee5\u6709\u51fd\u6570\u4f53\uff0c\u4f46\u5373\u4fbf\u8fd9\u6837\u5b83\u4ecd\u7136\u662f\u62bd\u8c61\u7684\u3002 \u63a5\u53e3\u7c7b \u662f\u90a3\u4e9b\u6ca1\u6709\u6210\u5458\u53d8\u91cf\uff0c\u4e14\u6240\u6709\u51fd\u6570\u5747\u4e3a \u7eaf\u865a\u51fd\u6570 \u7684\u7c7b\uff0c\u901a\u5e38\u5728\u547d\u540d\u5b83\u4eec\u7684\u65f6\u5019\u4f1a\u4f7f\u7528\u5927\u5199\u5b57\u6bcdI\u5f00\u5934\u3002 18.8 - \u865a\u57fa\u7c7b \u865a\u57fa\u7c7b \u662f\u4e00\u79cd\u57fa\u7c7b\uff0c\u4e0d\u8bba\u5b83\u88ab\u7ee7\u627f\u591a\u5c11\u6b21\uff0c\u5b83\u59cb\u7ec8\u53ea\u4f1a\u88ab\u6784\u9020\u4e00\u6b21\u3002 18.9 - \u5bf9\u8c61\u5207\u7247 \u5f53\u4e00\u4e2a\u6d3e\u751f\u7c7b\u88ab\u8d4b\u503c\u7ed9\u4e00\u4e2a\u57fa\u7c7b\u5bf9\u8c61\u65f6\uff0c\u57fa\u7c7b\u53ea\u4f1a\u5f97\u5230\u4e00\u4efd\u5305\u542b\u4e86\u6d3e\u751f\u7c7b\u4e2d\u57fa\u7c7b\u90e8\u5206\u7684\u62f7\u8d1d\u3002\u8fd9\u4e2a\u73b0\u8c61\u88ab\u79f0\u4e3a \u5bf9\u8c61\u5207\u7247 \u3002 \u6240\u4ee5\u51fd\u6570 \u6309\u503c\u4f20\u9012 \u6d3e\u751f\u5bf9\u8c61\u65f6\u4f1a\u51fa\u73b0\u5207\u7247\uff0c\u5bfc\u81f4\u6d3e\u751f\u7c7b\u865a\u51fd\u6570\u6ca1\u529e\u6cd5\u88ab\u5b9e\u9645\u8c03\u7528 18.10 - \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u53ef\u4ee5\u7528\u6765\u5c06\u4e00\u4e2a\u6307\u5411\u57fa\u7c7b\u5bf9\u8c61\u7684\u6307\u9488\u8f6c\u6362\u4e3a\u6307\u5411\u6d3e\u751f\u7c7b\u5bf9\u8c61\u7684\u6307\u9488\uff0c\u5373 downcasting \u3002\u5982\u679c\u8f6c\u6362\u5931\u8d25\uff0c\u5219\u4f1a\u8fd4\u56de\u4e00\u4e2a\u7a7a\u6307\u9488\u3002 18.11 - \u4f7f\u7528<<\u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b \u4e3a\u6d3e\u751f\u7c7b\u91cd\u8f7d << \u8fd0\u7b97\u7b26\u7684\u6700\u7b80\u5355\u7684\u529e\u6cd5\uff0c\u662f\u91cd\u8f7d\u6700\u57fa\u7840\u7684\u57fa\u7c7b\u7684 << \u8fd0\u7b97\u7b26\uff0c\u7136\u540e\u8c03\u7528\u865a\u6210\u5458\u51fd\u6570\u6765\u8fdb\u884c\u6253\u5370\u3002","title":"\u590d\u4e60"},{"location":"19-x-Chapter-19-comprehensive-quiz/","tags":["summary"],"text":"\u4f7f\u7528[[template|\u6a21\u677f]]\u53ef\u4ee5\u57fa\u4e8e \u5360\u4f4d\u7b26\u7c7b\u578b \u6765\u5b9a\u4e49\u51fd\u6570\u6216\u8005\u7c7b\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5b9a\u4e49\u51fa\u914d\u5408\u4e0d\u540c\u7c7b\u578b\u5de5\u4f5c\uff0c\u4f46\u662f\u7ed3\u6784\u5b8c\u5168\u4e00\u81f4\u7684\u51fd\u6570\u6216\u8005\u7c7b\u3002\u88ab\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u6216\u8005\u7c7b\uff0c\u79f0\u4e3a \u51fd\u6570\u5b9e\u4f8b \u6216 \u7c7b\u5b9e\u4f8b \u6240\u6709\u7684\u6a21\u677f\u51fd\u6570\u6216\u6a21\u677f\u7c7b\uff0c\u90fd\u5fc5\u987b\u4ee5[[template-parameter-declaration|\u6a21\u677f\u53c2\u6570\u58f0\u660e]]\u5f00\u5934\u3002\u5728\u6a21\u677f\u53c2\u6570\u58f0\u660e\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u6307\u5b9a\u6a21\u677f\u7684\u7c7b\u578b\u53c2\u6570\u6216\u8868\u8fbe\u5f0f\u53c2\u6570\u3002 \u6a21\u677f\u7c7b\u578b\u53c2\u6570 \u5176\u5b9e\u5c31\u662f \u5360\u4f4d\u7b26\u7c7b\u578b \uff0c\u901a\u5e38\u4f1a\u5199\u4f5c T \uff0c T1 \uff0c T2 \u6216\u8005\u5176\u4ed6\u5355\u72ec\u5b57\u6bcd\uff08\u4f8b\u5982 S \uff09\u3002 \u8868\u8fbe\u5f0f\u53c2\u6570 \u901a\u5e38\u662f\u6574\u578b\u7c7b\u578b\uff0c\u4f46\u662f\u4e5f\u53ef\u4ee5\u662f\u6307\u9488\u6216\u8005\u5f15\u7528\u7c7b\u578b\uff08\u6307\u5411\u67d0\u4e2a\u51fd\u6570\uff0c\u5bf9\u8c61\u6216\u6210\u5458\u51fd\u6570\uff09\u3002 \u6a21\u677f\u7c7b\u53ca\u5176\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u5e76\u4e0d\u80fd\u50cf\u5176\u4ed6\u4e00\u822c\u7c7b\u90a3\u6837\u8fdb\u884c\u5206\u79bb\u2014\u2014\u4f60\u4e0d\u80fd\u5c06\u7c7b\u7684\u5b9a\u4e49\u653e\u5728\u5934\u6587\u4ef6\u4e2d\u7136\u540e\u5c06\u6210\u5458\u51fd\u6570\u7684\u5b9a\u4e49\u653e\u5728 .cpp \u6587\u4ef6\u4e2d\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6700\u597d\u5c06\u5b83\u4eec\u90fd\u5b9a\u4e49\u5728\u4e00\u4e2a\u5934\u6587\u4ef6\u4e2d\uff0c\u6a21\u677f\u7c7b\u7684\u5b9a\u4e49\u5728\u524d\uff0c\u5176\u6210\u5458\u51fd\u6570\u7684\u5b9a\u4e49\u5728\u540e\u3002 \u6a21\u677f\u7279\u5316 can be used when we want to override the default behavior from the templated function or class for a specific type. If all types are overridden, this is called full specialization. Classes also support partial specialization, where only some of the templated parameters are specialized. Functions can not be partially specialized. C++\u6807\u51c6\u5e93\u4e2d\u7684\u5f88\u591a\u7c7b\u90fd\u4f7f\u7528\u4e86\u6a21\u677f\uff0c\u5305\u62ec std::array \u548c std::vector \u3002\u6a21\u677f\u7ecf\u5e38\u88ab\u7528\u6765\u5b9e\u73b0\u5bb9\u5668\u7c7b\uff0c\u8fd9\u6837\u7684\u5bb9\u5668\u7c7b\u5c31\u53ef\u4ee5\u914d\u5408\u4efb\u610f\u7c7b\u578b\u7684\u5bf9\u8c61\u6765\u4f7f\u7528\u3002","title":"19.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6a21\u677f\u548c\u7c7b"},{"location":"2-1-Introduction-to-functions/","tags":["function"],"text":"Key Takeaway \u5728\u4e0a\u4e00\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u51fd\u6570\u5b9a\u4e49\u4e3a\u4e00\u7ec4\u987a\u5e8f\u6267\u884c\u6307\u4ee4\u7684\u5408\u96c6\u3002\u8fd9\u4e2a\u5b9a\u4e49\u56fa\u7136\u6ca1\u9519\uff0c\u4f46\u662f\u5b83\u5e76\u6ca1\u6709\u8868\u8fbe\u51fa\u51fd\u6570\u7684\u91cd\u8981\u6027\u3002 \u8fd9\u91cc\u8ba9\u6211\u4eec\u91cd\u65b0\u5b9a\u4e49\u51fd\u6570\uff1a\u51fd\u6570\u662f\u4e00\u7ec4\u4e3a\u4e86\u5b8c\u6210\u67d0\u9879\u4efb\u52a1\u800c\u521b\u5efa\u7684\u53ef\u590d\u7528\u7684\u987a\u5e8f\u6307\u4ee4\u96c6\u5408\u3002 \u4f60\u5df2\u7ecf\u77e5\u9053\u4e86\uff0c\u4efb\u4f55\u90fd\u5fc5\u987b\u5305\u542b\u4e00\u4e2a\u540d\u4e3a main \u7684\u51fd\u6570\uff08\u7a0b\u5e8f\u5f00\u59cb\u6267\u884c\u7684\u5730\u65b9\uff09\u3002\u7136\u800c\uff0c\u5982\u679c\u5c06\u7a0b\u5e8f\u7684\u6240\u6709\u4ee3\u7801\u90fd\u585e\u8fdb main \u51fd\u6570\u4e2d\uff0c\u90a3\u4e48\u5b83\u65e0\u7591\u4f1a\u53d8\u5f97\u8d8a\u6765\u8d8a\u957f\uff0c\u4e5f\u8d8a\u6765\u8d8a\u96be\u4ee5\u7ef4\u62a4\u3002\u800c\u51fd\u6570\u5219\u63d0\u4f9b\u4e86\u4e00\u79cd\u5c06\u7a0b\u5e8f\u5206\u5272\u6210\u7b80\u77ed\u7684\u3001\u6a21\u5757\u5316\u7684\u4ee3\u7801\u6bb5\u7684\u65b9\u5f0f\u3002\u8fd9\u79cd\u65b9\u5f0f\u7684\u4ee3\u7801\u66f4\u52a0\u5bb9\u6613\u7ba1\u7406\u3001\u5bb9\u6613\u6d4b\u8bd5\uff0c\u4e5f\u66f4\u5bb9\u6613\u4f7f\u7528\u3002C++ \u6807\u51c6\u5e93\u4e2d\u5305\u542b\u4e86\u975e\u5e38\u591a\u7684\u5df2\u7ecf\u7f16\u5199\u597d\u7684\u51fd\u6570\u53ef\u4f9b\u4f60\u4f7f\u7528\u3002\u4e0d\u8fc7\uff0c\u4f7f\u7528\u81ea\u5df1\u7f16\u5199\u7684\u51fd\u6570\u4e5f\u662f\u975e\u5e38\u5e38\u89c1\u7684\u60c5\u5f62\uff0c\u8fd9\u4e9b\u51fd\u6570\u79f0\u4e3a\u7528\u6237\u81ea\u5b9a\u4e49\u51fd\u6570\u3002 \u6211\u4eec\u53ef\u4ee5\u8003\u8651\u8fd9\u6837\u4e00\u79cd\u751f\u6d3b\u4e2d\u7684\u573a\u666f\uff1a\u5f53\u4f60\u5728\u9605\u8bfb\u7684\u65f6\u5019\uff0c\u7a81\u7136\u60f3\u5230\u8981\u53bb\u6253\u4e00\u4e2a\u7535\u8bdd\u3002\u6b64\u65f6\u4f60\u4f1a\u5c06\u4e66\u7b7e\u5939\u5728\u4e66\u91cc\uff0c\u7136\u540e\u53bb\u6253\u7535\u8bdd\uff0c\u7136\u540e\u5728\u56de\u5230\u5939\u4e86\u4e66\u7b7e\u7684\u5730\u65b9\u7ee7\u7eed\u9605\u8bfb\u3002 C++ \u51fd\u6570\u4e5f\u53ef\u4ee5\u901a\u8fc7\u7c7b\u4f3c\u7684\u65b9\u6cd5\u53bb\u5de5\u4f5c\u3002\u5f53\u9047\u5230\u51fd\u6570\u8c03\u7528\u7684\u65f6\u5019\uff0c\u7a0b\u5e8f\u4f1a\u8fdb\u5165\u51fd\u6570\u5e76\u987a\u5e8f\u5730\u6267\u884c\u5176\u4e2d\u5404\u4e2a\u8bed\u53e5\u3002\u800c\u6240\u8c13\u51fd\u6570\u8c03\u7528\uff0c\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u5b83\u53ef\u4ee5\u544a\u8bc9 CPU \u4e2d\u65ad\u5f53\u524d\u51fd\u6570\u7684\u6267\u884c\u800c\u8f6c\u53bb\u6267\u884c\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\u3002\u6b64\u65f6 CPU \u5c31\u4f1a\u5728\u5f53\u524d\u7684\u6267\u884c\u70b9\u201d\u63d2\u5165\u4e00\u4e2a\u4e66\u7b7e\u201c\uff0c\u7136\u540e\u53bb\u6267\u884c\u53e6\u5916\u7684\u51fd\u6570\u3002\u5f53\u8be5\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\uff0cCPU \u5c31\u4f1a\u8fd4\u56de\u4e4b\u524d\u4e2d\u65ad\u7684\u70b9\u7ee7\u7eed\u6267\u884c\u3002 \u5b9e\u65bd\u51fd\u6570\u8c03\u7528\u7684\u51fd\u6570\uff0c\u79f0\u4e3a \u4e3b\u8c03\u51fd\u6570(caller) \uff0c\u800c\u88ab\u5b83\u8c03\u7528\u7684\u51fd\u6570\u79f0\u4e3a \u88ab\u8c03\u51fd\u6570(callee \u6216 called function) \u3002 \u81ea\u5b9a\u5b9a\u4e49\u51fd\u6570\u6848\u4f8b \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u719f\u6089\u4e00\u4e0b\u5b9a\u4e49\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u3002\u5728\u63a5\u4e0b\u6765\u7684\u51e0\u8282\u8bfe\u4e2d\uff0c\u7528\u6237\u5b9a\u4e49\u51fd\u6570\u90fd\u662f\u5982\u4e0b\u5f62\u5f0f\uff1a 1 2 3 4 return-type identifier() // identifier replaced with the name of your function { // Your code here } \u7b80\u5355\u4ecb\u7ecd\u4e00\u4e0b\u5176\u4e2d\u56db\u4e2a\u4e3b\u8981\u5143\u7d20\uff1a \u5728\u672c\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528 int \uff08\u5bf9\u4e8e main() \u51fd\u6570\uff09\u6216 void \uff08\u5bf9\u4e8e\u5176\u4ed6\u51fd\u6570\uff09\u4f5c\u4e3a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\uff08 return-type \uff09\u3002\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\uff08 2.2 - \u51fd\u6570\u8fd4\u56de\u503c \uff09\u4e2d\u4ecb\u7ecd\u66f4\u591a\u7684\u8fd4\u56de\u503c\u7c7b\u578b\uff1b \u548c\u53d8\u91cf\u4e00\u6837\uff0c\u51fd\u6570\u4e5f\u6709\u51fd\u6570\u540d\u3002 identifier \u5c31\u662f\u4f60\u5b9a\u4e49\u7684\u51fd\u6570\u7684\u51fd\u6570\u540d\uff1b \u6807\u8bc6\u7b26\u540e\u9762\u7684\u62ec\u53f7\u544a\u8bc9\u7f16\u8bd1\u5668\u8fd9\u91cc\u662f\u5728\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff1b \u82b1\u62ec\u53f7\u4ee5\u53ca\u82b1\u62ec\u53f7\u5185\u90e8\u7684\u8bed\u53e5\u79f0\u4e3a \u51fd\u6570\u4f53 \u3002\u8fd9\u91cc\u5c31\u662f\u653e\u7f6e\u51fd\u6570\u8bed\u53e5\u7684\u7684\u5730\u65b9\u3002 \u4e0b\u9762\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5b9a\u4e49\u5e76\u4f7f\u7528\u4e00\u4e2a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // for std::cout // \u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a doPrint() \u7684\u51fd\u6570 void doPrint () // doPrint() \u5728\u672c\u4f8b\u4e2d\u662f\u88ab\u8c03\u51fd\u6570 { std :: cout << \"In doPrint() \\n \" ; } // Definition of function main() int main () { std :: cout << \"Starting main() \\n \" ; doPrint (); // \u4e2d\u65ad main() \u51fd\u6570\u5e76\u8c03\u7528 doPrint()\u3002main() \u51fd\u6570\u662f\u4e3b\u8c03\u51fd\u6570 std :: cout << \"Ending main() \\n \" ; // \u5728 doPrint() \u7ed3\u675f\u540e\u6267\u884c\u8be5\u6307\u4ee4 return 0 ; } \u4e0a\u8ff0\u4ee3\u7801\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 Starting main() In doPrint() Ending main() \u8be5\u7a0b\u5e8f\u4ece main \u51fd\u6570\u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\uff0c\u7b2c\u4e00\u884c\u4f1a\u6253\u5370 Starting main() . main \u51fd\u6570\u7684\u7b2c\u4e8c\u884c\u4f1a\u8c03\u7528 doPrint \u3002\u901a\u8fc7\u5728\u51fd\u6570\u540d\u540e\u9762\u52a0\u4e0a\u62ec\u53f7\u5c31\u53ef\u4ee5\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u4f8b\u5982 doPrint() \u3002\u6ce8\u610f\uff0c\u5982\u679c\u4f60\u5fd8\u8bb0\u4e86\u62ec\u53f7\uff0c\u7a0b\u5e8f\u53ef\u80fd\u65e0\u6cd5\u7f16\u8bd1\uff08\u5982\u679c\u7a0b\u5e8f\u80fd\u591f\u7f16\u8bd1\uff0c\u51fd\u6570\u4e5f\u4e0d\u4f1a\u88ab\u8c03\u7528\uff09\u3002 \u6ce8\u610f \u5728\u8c03\u7528\u51fd\u6570\u7684\u65f6\u5019\uff0c\u4e0d\u8981\u5fd8\u8bb0\u52a0\u4e0a\u62ec\u53f7\u3002 \u56e0\u4e3a\u6267\u884c\u4e86\u51fd\u6570\u8c03\u7528\uff0c main \u51fd\u6570\u4e2d\u8bed\u53e5\u7684\u6267\u884c\u88ab\u6682\u505c\u4e86\uff0c\u6b64\u65f6\u4f1a\u8df3\u8f6c\u5230\u88ab\u8c03\u51fd\u6570 doPrint \u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\u3002 doPrint \u51fd\u6570\u7684\u7b2c\u4e00\u884c\uff08\u4e5f\u662f\u552f\u4e00\u4e00\u884c\uff09\u4f1a\u6253\u5370 In doPrint() \u3002\u5f53\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u7a0b\u5e8f\u4f1a\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570( main \u51fd\u6570)\uff0c\u4ece\u4e4b\u524d\u79bb\u5f00\u7684\u4f4d\u7f6e\u7ee7\u7eed\u6267\u884c\u3002\u56e0\u6b64\uff0c\u4e0b\u4e00\u6761\u8981\u6267\u884c\u7684\u8bed\u53e5\u662f\u6253\u5370 Ending main() \u3002 \u591a\u6b21\u8c03\u7528\u51fd\u6570 \u51fd\u6570\u6700\u6709\u7528\u7684\u529f\u80fd\u83ab\u8fc7\u4e8e\u5b83\u53ef\u4ee5\u88ab\u591a\u6b21\u8c03\u7528\u3002\u4e0b\u9762\u4f8b\u5b50\u5c55\u793a\u4e86\u591a\u6b21\u8c03\u7528\u51fd\u6570\u7684\u573a\u666f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // for std::cout void doPrint () { std :: cout << \"In doPrint() \\n \" ; } // Definition of function main() int main () { std :: cout << \"Starting main() \\n \" ; doPrint (); // doPrint() called for the first time doPrint (); // doPrint() called for the second time std :: cout << \"Ending main() \\n \" ; return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 Starting main() In doPrint() In doPrint() Ending main() \u56e0\u4e3a doPrint \u5728 main \u51fd\u6570\u4e2d\u88ab\u8c03\u7528\u4e86\u4e24\u6b21\uff0c\u56e0\u6b64\u5b83\u4e5f\u4f1a\u6267\u884c\u4e24\u6b21\uff0c\u56e0\u6b64 In doPrint() \u4f1a\u6253\u5370\u4e24\u6b21\uff08\u6bcf\u6b21\u8c03\u7528\u6253\u5370\u4e00\u6b21\uff09\u3002 \u51fd\u6570\u94fe\u5f0f\u8c03\u7528 \u4f60\u5df2\u7ecf\u770b\u5230\u4e86\uff0c main \u51fd\u6570\u53ef\u4ee5\u8c03\u7528\u5176\u4ed6\u51fd\u6570\uff08\u4f8b\u5982 doPrint \uff09\u3002\u4efb\u4f55\u51fd\u6570\u90fd\u53ef\u4ee5\u51fd\u6570\u3002\u5728\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c main \u51fd\u6570\u4f1a\u8c03\u7528\u51fd\u6570 doA \uff0c doA \u51fd\u6570\u53c8\u4f1a\u8c03\u7528 doB \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include // for std::cout void doB () { std :: cout << \"In doB() \\n \" ; } void doA () { std :: cout << \"Starting doA() \\n \" ; doB (); std :: cout << \"Ending doA() \\n \" ; } // Definition of function main() int main () { std :: cout << \"Starting main() \\n \" ; doA (); std :: cout << \"Ending main() \\n \" ; return 0 ; } \u4e0a\u8ff0\u4ee3\u7801\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 Starting main() Starting doA() In doB() Ending doA() Ending main() C++\u4e0d\u652f\u6301\u51fd\u6570\u5d4c\u5957 \u548c\u5176\u4ed6\u8bed\u8a00\u4e0d\u540c\uff0cC++\u5e76\u4e0d\u652f\u6301\u5728\u51fd\u6570\u4e2d\u5b9a\u4e49\u5176\u4ed6\u51fd\u6570\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u4e0d\u5408\u6cd5\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { void foo () // \u9519\u8bef\uff1a\u8be5\u51fd\u6570\u5b9a\u4e49\u4f4d\u4e8e\u5176\u4ed6\u51fd\u6570\u5185\u90e8\u3002 { std :: cout << \"foo! \\n \" ; } foo (); // function call to foo() return 0 ; } \u6b63\u786e\u7684\u65b9\u5f0f\u5e94\u8be5\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include void foo () // no longer inside of main() { std :: cout << \"foo! \\n \" ; } int main () { foo (); return 0 ; } \u9898\u5916\u8bdd \u201cfoo\u201d \u662f\u4e00\u4e2a\u6ca1\u6709\u610f\u4e49\u7684\u5355\u8bcd\uff0c\u5728\u5c55\u793a\u67d0\u4e9b\u6982\u5ff5\u7684\u65f6\u5019\uff0c\u5982\u679c\u540d\u79f0\u4e0d\u91cd\u8981\uff0c\u90a3\u4e48\u5e38\u4f1a\u4f7f\u7528\u5b83\u6765\u4f5c\u4e3a\u5360\u4f4d\u7b26\u3002\u6b64\u7c7b\u5355\u8bcd\u6709\u4e2a\u4e13\u95e8\u7684\u540d\u5b57\u53eb\u505a\uff1a\u4f2a\u53d8\u91cf/ metasyntactic variables (\u4e0d\u8fc7\uff0c\u5728\u65e5\u5e38\u7528\u8bed\u4e2d\u5927\u5bb6\u8fd8\u662f\u559c\u6b22\u79f0\u5176\u4e3a\u201d\u540d\u79f0\u5360\u4f4d\u7b26\u201c\uff0c\u56e0\u4e3a metasyntactic variables\u8fd9\u4e2a\u5355\u8bcd\u771f\u7684\u5f88\u751f\u50fb)\u3002 \u5176\u4ed6C++\u4e2d\u5e38\u89c1\u7684\u4f2a\u53d8\u91cf\u8fd8\u5305\u62ec \u201cbar\u201d\u3001\u201cbaz\u201d\u4ee5\u53ca\u5176\u4ed6\u4ee5\u201coo\u201d\u7ed3\u5c3e\u7684\u4e09\u5b57\u6bcd\u5355\u8bcd\uff0c\u4f8b\u5982\u201cgoo\u201d\u3001\u201cmoo\u201d\u548c\u201cboo\u201d\u3002 \u5982\u679c\u4f60\u5bf9\u5176\u8bcd\u6e90\u611f\u5174\u8da3\uff0c\u53ef\u4ee5\u53bb\u9605\u8bfb RFC 3092 \u3002","title":"2.1 - \u51fd\u6570\u7b80\u4ecb"},{"location":"2-1-Introduction-to-functions/#_1","text":"\u9996\u5148\uff0c\u8ba9\u6211\u4eec\u719f\u6089\u4e00\u4e0b\u5b9a\u4e49\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u3002\u5728\u63a5\u4e0b\u6765\u7684\u51e0\u8282\u8bfe\u4e2d\uff0c\u7528\u6237\u5b9a\u4e49\u51fd\u6570\u90fd\u662f\u5982\u4e0b\u5f62\u5f0f\uff1a 1 2 3 4 return-type identifier() // identifier replaced with the name of your function { // Your code here } \u7b80\u5355\u4ecb\u7ecd\u4e00\u4e0b\u5176\u4e2d\u56db\u4e2a\u4e3b\u8981\u5143\u7d20\uff1a \u5728\u672c\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528 int \uff08\u5bf9\u4e8e main() \u51fd\u6570\uff09\u6216 void \uff08\u5bf9\u4e8e\u5176\u4ed6\u51fd\u6570\uff09\u4f5c\u4e3a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\uff08 return-type \uff09\u3002\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\uff08 2.2 - \u51fd\u6570\u8fd4\u56de\u503c \uff09\u4e2d\u4ecb\u7ecd\u66f4\u591a\u7684\u8fd4\u56de\u503c\u7c7b\u578b\uff1b \u548c\u53d8\u91cf\u4e00\u6837\uff0c\u51fd\u6570\u4e5f\u6709\u51fd\u6570\u540d\u3002 identifier \u5c31\u662f\u4f60\u5b9a\u4e49\u7684\u51fd\u6570\u7684\u51fd\u6570\u540d\uff1b \u6807\u8bc6\u7b26\u540e\u9762\u7684\u62ec\u53f7\u544a\u8bc9\u7f16\u8bd1\u5668\u8fd9\u91cc\u662f\u5728\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff1b \u82b1\u62ec\u53f7\u4ee5\u53ca\u82b1\u62ec\u53f7\u5185\u90e8\u7684\u8bed\u53e5\u79f0\u4e3a \u51fd\u6570\u4f53 \u3002\u8fd9\u91cc\u5c31\u662f\u653e\u7f6e\u51fd\u6570\u8bed\u53e5\u7684\u7684\u5730\u65b9\u3002 \u4e0b\u9762\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5b9a\u4e49\u5e76\u4f7f\u7528\u4e00\u4e2a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // for std::cout // \u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a doPrint() \u7684\u51fd\u6570 void doPrint () // doPrint() \u5728\u672c\u4f8b\u4e2d\u662f\u88ab\u8c03\u51fd\u6570 { std :: cout << \"In doPrint() \\n \" ; } // Definition of function main() int main () { std :: cout << \"Starting main() \\n \" ; doPrint (); // \u4e2d\u65ad main() \u51fd\u6570\u5e76\u8c03\u7528 doPrint()\u3002main() \u51fd\u6570\u662f\u4e3b\u8c03\u51fd\u6570 std :: cout << \"Ending main() \\n \" ; // \u5728 doPrint() \u7ed3\u675f\u540e\u6267\u884c\u8be5\u6307\u4ee4 return 0 ; } \u4e0a\u8ff0\u4ee3\u7801\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 Starting main() In doPrint() Ending main() \u8be5\u7a0b\u5e8f\u4ece main \u51fd\u6570\u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\uff0c\u7b2c\u4e00\u884c\u4f1a\u6253\u5370 Starting main() . main \u51fd\u6570\u7684\u7b2c\u4e8c\u884c\u4f1a\u8c03\u7528 doPrint \u3002\u901a\u8fc7\u5728\u51fd\u6570\u540d\u540e\u9762\u52a0\u4e0a\u62ec\u53f7\u5c31\u53ef\u4ee5\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u4f8b\u5982 doPrint() \u3002\u6ce8\u610f\uff0c\u5982\u679c\u4f60\u5fd8\u8bb0\u4e86\u62ec\u53f7\uff0c\u7a0b\u5e8f\u53ef\u80fd\u65e0\u6cd5\u7f16\u8bd1\uff08\u5982\u679c\u7a0b\u5e8f\u80fd\u591f\u7f16\u8bd1\uff0c\u51fd\u6570\u4e5f\u4e0d\u4f1a\u88ab\u8c03\u7528\uff09\u3002 \u6ce8\u610f \u5728\u8c03\u7528\u51fd\u6570\u7684\u65f6\u5019\uff0c\u4e0d\u8981\u5fd8\u8bb0\u52a0\u4e0a\u62ec\u53f7\u3002 \u56e0\u4e3a\u6267\u884c\u4e86\u51fd\u6570\u8c03\u7528\uff0c main \u51fd\u6570\u4e2d\u8bed\u53e5\u7684\u6267\u884c\u88ab\u6682\u505c\u4e86\uff0c\u6b64\u65f6\u4f1a\u8df3\u8f6c\u5230\u88ab\u8c03\u51fd\u6570 doPrint \u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\u3002 doPrint \u51fd\u6570\u7684\u7b2c\u4e00\u884c\uff08\u4e5f\u662f\u552f\u4e00\u4e00\u884c\uff09\u4f1a\u6253\u5370 In doPrint() \u3002\u5f53\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u7a0b\u5e8f\u4f1a\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570( main \u51fd\u6570)\uff0c\u4ece\u4e4b\u524d\u79bb\u5f00\u7684\u4f4d\u7f6e\u7ee7\u7eed\u6267\u884c\u3002\u56e0\u6b64\uff0c\u4e0b\u4e00\u6761\u8981\u6267\u884c\u7684\u8bed\u53e5\u662f\u6253\u5370 Ending main() \u3002","title":"\u81ea\u5b9a\u5b9a\u4e49\u51fd\u6570\u6848\u4f8b"},{"location":"2-1-Introduction-to-functions/#_2","text":"\u51fd\u6570\u6700\u6709\u7528\u7684\u529f\u80fd\u83ab\u8fc7\u4e8e\u5b83\u53ef\u4ee5\u88ab\u591a\u6b21\u8c03\u7528\u3002\u4e0b\u9762\u4f8b\u5b50\u5c55\u793a\u4e86\u591a\u6b21\u8c03\u7528\u51fd\u6570\u7684\u573a\u666f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // for std::cout void doPrint () { std :: cout << \"In doPrint() \\n \" ; } // Definition of function main() int main () { std :: cout << \"Starting main() \\n \" ; doPrint (); // doPrint() called for the first time doPrint (); // doPrint() called for the second time std :: cout << \"Ending main() \\n \" ; return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 Starting main() In doPrint() In doPrint() Ending main() \u56e0\u4e3a doPrint \u5728 main \u51fd\u6570\u4e2d\u88ab\u8c03\u7528\u4e86\u4e24\u6b21\uff0c\u56e0\u6b64\u5b83\u4e5f\u4f1a\u6267\u884c\u4e24\u6b21\uff0c\u56e0\u6b64 In doPrint() \u4f1a\u6253\u5370\u4e24\u6b21\uff08\u6bcf\u6b21\u8c03\u7528\u6253\u5370\u4e00\u6b21\uff09\u3002","title":"\u591a\u6b21\u8c03\u7528\u51fd\u6570"},{"location":"2-1-Introduction-to-functions/#_3","text":"\u4f60\u5df2\u7ecf\u770b\u5230\u4e86\uff0c main \u51fd\u6570\u53ef\u4ee5\u8c03\u7528\u5176\u4ed6\u51fd\u6570\uff08\u4f8b\u5982 doPrint \uff09\u3002\u4efb\u4f55\u51fd\u6570\u90fd\u53ef\u4ee5\u51fd\u6570\u3002\u5728\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c main \u51fd\u6570\u4f1a\u8c03\u7528\u51fd\u6570 doA \uff0c doA \u51fd\u6570\u53c8\u4f1a\u8c03\u7528 doB \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include // for std::cout void doB () { std :: cout << \"In doB() \\n \" ; } void doA () { std :: cout << \"Starting doA() \\n \" ; doB (); std :: cout << \"Ending doA() \\n \" ; } // Definition of function main() int main () { std :: cout << \"Starting main() \\n \" ; doA (); std :: cout << \"Ending main() \\n \" ; return 0 ; } \u4e0a\u8ff0\u4ee3\u7801\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 Starting main() Starting doA() In doB() Ending doA() Ending main()","title":"\u51fd\u6570\u94fe\u5f0f\u8c03\u7528"},{"location":"2-1-Introduction-to-functions/#c","text":"\u548c\u5176\u4ed6\u8bed\u8a00\u4e0d\u540c\uff0cC++\u5e76\u4e0d\u652f\u6301\u5728\u51fd\u6570\u4e2d\u5b9a\u4e49\u5176\u4ed6\u51fd\u6570\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u4e0d\u5408\u6cd5\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { void foo () // \u9519\u8bef\uff1a\u8be5\u51fd\u6570\u5b9a\u4e49\u4f4d\u4e8e\u5176\u4ed6\u51fd\u6570\u5185\u90e8\u3002 { std :: cout << \"foo! \\n \" ; } foo (); // function call to foo() return 0 ; } \u6b63\u786e\u7684\u65b9\u5f0f\u5e94\u8be5\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include void foo () // no longer inside of main() { std :: cout << \"foo! \\n \" ; } int main () { foo (); return 0 ; } \u9898\u5916\u8bdd \u201cfoo\u201d \u662f\u4e00\u4e2a\u6ca1\u6709\u610f\u4e49\u7684\u5355\u8bcd\uff0c\u5728\u5c55\u793a\u67d0\u4e9b\u6982\u5ff5\u7684\u65f6\u5019\uff0c\u5982\u679c\u540d\u79f0\u4e0d\u91cd\u8981\uff0c\u90a3\u4e48\u5e38\u4f1a\u4f7f\u7528\u5b83\u6765\u4f5c\u4e3a\u5360\u4f4d\u7b26\u3002\u6b64\u7c7b\u5355\u8bcd\u6709\u4e2a\u4e13\u95e8\u7684\u540d\u5b57\u53eb\u505a\uff1a\u4f2a\u53d8\u91cf/ metasyntactic variables (\u4e0d\u8fc7\uff0c\u5728\u65e5\u5e38\u7528\u8bed\u4e2d\u5927\u5bb6\u8fd8\u662f\u559c\u6b22\u79f0\u5176\u4e3a\u201d\u540d\u79f0\u5360\u4f4d\u7b26\u201c\uff0c\u56e0\u4e3a metasyntactic variables\u8fd9\u4e2a\u5355\u8bcd\u771f\u7684\u5f88\u751f\u50fb)\u3002 \u5176\u4ed6C++\u4e2d\u5e38\u89c1\u7684\u4f2a\u53d8\u91cf\u8fd8\u5305\u62ec \u201cbar\u201d\u3001\u201cbaz\u201d\u4ee5\u53ca\u5176\u4ed6\u4ee5\u201coo\u201d\u7ed3\u5c3e\u7684\u4e09\u5b57\u6bcd\u5355\u8bcd\uff0c\u4f8b\u5982\u201cgoo\u201d\u3001\u201cmoo\u201d\u548c\u201cboo\u201d\u3002 \u5982\u679c\u4f60\u5bf9\u5176\u8bcd\u6e90\u611f\u5174\u8da3\uff0c\u53ef\u4ee5\u53bb\u9605\u8bfb RFC 3092 \u3002","title":"C++\u4e0d\u652f\u6301\u51fd\u6570\u5d4c\u5957"},{"location":"2-10-Introduction-to-the-preprocessor/","tags":["preprocessor"],"text":"Key Takeaway \u9884\u5904\u7406\u5668\u53ef\u4ee5\u770b\u505a\u662f\u5355\u72ec\u7684\u7a0b\u5e8f\uff0c\u5b83\u4f7f\u7528\u7684\u9884\u5904\u7406\u6307\u4ee4\u4e5f\u4e0d\u540c\u4e8eC++\u7684\u8bed\u6cd5\u3002 \u51fd\u6570\u7c7b\u578b\u7684\u5b8f\u4e0d\u5b89\u5168\u4e14\u53ef\u4ee5\u88ab\u51fd\u6570\u66ff\u4ee3\uff0c\u56e0\u6b64\u4e0d\u8981\u4f7f\u7528 \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f\uff0c\u5176\u4f5c\u4e3a\u5e38\u91cf\u5b9a\u4e49\u7684\u529f\u80fd\u73b0\u5728\u5df2\u7ecf\u4e0d\u63a8\u8350\u4f7f\u7528\u4e86 #if 0 \u53ef\u4ee5\u7528\u6765\u201c\u6ce8\u91ca\u6389\u201d\u5305\u542b\u591a\u884c\u6ce8\u91ca\u7684\u4ee3\u7801\uff0c\u56e0\u4e3a\u591a\u884c\u6ce8\u91ca\u662f\u4e0d\u80fd\u5d4c\u5957\u7684 \u9884\u5904\u7406\u5668\u6307\u4ee4\u4e4b\u95f4\u4e0d\u4f1a\u4e92\u76f8\u5f71\u54cd\u3002\u56e0\u6b64\u5b9a\u4e49\u67d0\u4e2a\u6807\u8bc6\u7b26\u4e3a\u7a7a\u4e0d\u4f1a\u5bfc\u81f4\u540e\u9762\u7684\u9884\u5904\u7406\u5668\u6307\u4ee4\u88ab\u66ff\u6362\u4e3a\u7a7a \u5b8f\u5b9a\u4e49\u7684\u4f5c\u7528\u57df\u53ea\u5728\u672c\u6587\u4ef6\u4e2d\uff0c\u7531\u4e8e\u9884\u5904\u7406\u5668\u4e0d\u80fd\u7406\u89e3C++\u8bed\u6cd5\uff0c\u56e0\u6b64\u5b8f\u5b9a\u4e49\u662f\u5426\u5728\u51fd\u6570\u5185\u90e8\u5e76\u65e0\u533a\u522b \u7ffb\u8bd1\u53ca\u9884\u5904\u7406\u5668 \u4f60\u53ef\u80fd\u4f1a\u89c9\u5f97\u7f16\u8bd1\u5668\u4f1a\u539f\u5c01\u4e0d\u52a8\u5730\u7f16\u8bd1\u4f60\u5199\u7684\u4ee3\u7801\u3002\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u3002 \u5728\u5f00\u59cb\u7f16\u8bd1\u524d\uff0c\u4ee3\u7801\u4f1a\u7ecf\u8fc7\u4e00\u4e2a\u53eb\u505a \u7ffb\u8bd1(translation) \u7684\u9636\u6bb5\u3002\u5728\u8fd9\u4e2a\u9636\u6bb5\u9700\u8981\u5b8c\u6210\u5f88\u591a\u64cd\u4f5c\uff0c\u624d\u80fd\u4f7f\u4f60\u7684\u4ee3\u7801\u5177\u5907\u88ab\u7f16\u8bd1\u7684\u6761\u4ef6\uff08\u5982\u679c\u6709\u5174\u8da3\u53ef\u4ee5\u5728 \u8fd9\u91cc \u67e5\u770b\u6709\u5173\u7ffb\u8bd1\u9636\u6bb5\u7684\u8be6\u7ec6\u4fe1\u606f\uff09\u3002\u8fdb\u884c\u8fc7\u7ffb\u8bd1\u7684\u4ee3\u7801\u6587\u4ef6\u53eb\u505a\u4e00\u4e2a\u7ffb\u8bd1\u5355\u5143\u3002 \u5728\u7ffb\u8bd1\u9636\u6bb5\u4e2d\u6700\u503c\u5f97\u6211\u4eec\u5173\u6ce8\u7684\u662f \u9884\u5904\u7406\u5668 \uff0c\u5b83\u53ef\u4ee5\u88ab\u770b\u505a\u662f\u4e00\u4e2a\u5bf9\u6bcf\u4e2a\u4ee3\u7801\u6587\u4ef6\u8fdb\u884c\u6587\u672c\u5904\u7406\u7684\u5355\u72ec\u7684\u5de5\u5177\u3002 \u5728\u9884\u5904\u7406\u5668\u5de5\u4f5c\u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u4ece\u5934\u5230\u5c3e\u626b\u63cf\u4ee3\u7801\u6587\u4ef6\uff0c\u67e5\u627e \u9884\u5904\u7406\u5668\u6307\u4ee4 (preprocessor directive) \u3002\u9884\u5904\u7406\u5668\u6307\u4ee4\uff08\u7ecf\u5e38\u7b80\u79f0\u4e3a\u6307\u4ee4\uff09\u662f\u4e00\u4e9b\u4ee5 # \u5f00\u5934\uff0c\u4ee5\u6362\u884c\uff08\u800c\u975e\u5206\u53f7\uff09\u7ed3\u5c3e\u7684\u7b26\u53f7\u3002\u8fd9\u4e9b\u6307\u4ee4\u4f1a\u544a\u8bc9\u9884\u5904\u7406\u5668\u5e94\u8be5\u5bf9\u6587\u672c\u6267\u884c\u54ea\u4e9b\u7279\u5b9a\u7684\u64cd\u4f5c\u3002 \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u9884\u5904\u7406\u5668\u5e76\u4e0d\u7406\u89e3C++\u7684\u8bed\u6cd5\u2014\u2014\u76f8\u53cd\uff0c\u9884\u5904\u7406\u5668\u6307\u4ee4\u6709\u5176\u81ea\u5df1\u7684\u8bed\u6cd5\uff08\u6709\u4e9b\u548cC++\u8bed\u6cd5\u7c7b\u4f3c\uff0c\u6709\u4e9b\u5219\u4e0d\u7136\uff09\u3002 \u9884\u5904\u7406\u5668\u7684\u8f93\u51fa\u4f1a\u7ecf\u5386\u4e00\u4e9b\u5217\u7ffb\u8bd1\u9636\u6bb5\uff0c\u7136\u540e\u624d\u4f1a\u88ab\u7f16\u8bd1\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u9884\u5904\u7406\u5668\u5e76\u4e0d\u4f1a\u4fee\u6539\u539f\u6587\u4ef6\u2014\u2014\u6240\u6709\u7ecf\u9884\u5904\u7406\u5668\u4fee\u6539\u4ea7\u751f\u7684\u53d8\u5316\u90fd\u662f\u4e34\u65f6\u7684\uff08\u7528\u4e8e\u4e00\u6b21\u7f16\u8bd1\uff09 \u672c\u8282\u8bfe\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u6700\u4e3a\u5e38\u89c1\u7684\u9884\u5904\u7406\u5668\u6307\u4ee4\u3002 \u9898\u5916\u8bdd Using \u6307\u4ee4 ( 2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 \u4e2d\u4ecb\u7ecd)\u5e76\u4e0d\u5c5e\u4e8e\u9884\u5904\u7406\u5668\u6307\u4ee4\uff08\u56e0\u6b64\u4e0d\u4f1a\u88ab\u9884\u5904\u7406\u5668\u5904\u7406\uff09\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u6307\u4ee4(directive)\u4e00\u8bcd\u591a\u6307\u9884\u5904\u7406\u5668\u6307\u4ee4\uff0c\u4f46\u4e5f\u4e0d\u662f\u603b\u662f\u8fd9\u6837\u3002 Includes \u6211\u4eec\u5df2\u7ecf\u5728\u5b9e\u9645\u6848\u4f8b\u4e2d\u63a5\u89e6\u8fc7 #include \u6307\u4ee4\u4e86\uff08 #include \uff09\u3002\u5728 #include \u67d0\u4e2a\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u9884\u5904\u7406\u5668\u4f1a\u628a #include \u66ff\u6362\u4e3a\u8be5\u6587\u4ef6\u7684\u5b9e\u9645\u5185\u5bb9\uff0c\u7136\u540e\u9884\u5904\u7406\u5668\u624d\u4f1a\u5bf9\u66ff\u6362\u540e\u7684\u6587\u4ef6\u5185\u5bb9\u8fdb\u884c\u9884\u5904\u7406\u548c\u7f16\u8bd1\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << \"Hello, world!\" ; return 0 ; } \u5f53\u9884\u5904\u7406\u5668\u5904\u7406\u8be5\u7a0b\u5e8f\u65f6\uff0c\u5b83\u4f1a\u628a #include \u66ff\u6362\u4e3a\u7ecf\u8fc7\u9884\u5904\u7406\u7684\u540d\u4e3a\u201ciostream\u201d\u7684\u6587\u4ef6\u7684\u5185\u5bb9\u3002 \u56e0\u4e3a #include \u51e0\u4e4e\u53ea\u7528\u6765\u5305\u542b\u5934\u6587\u4ef6\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u8ba8\u8bba\u5934\u6587\u4ef6\u65f6\u518d\u5bf9\u5176\u8fdb\u884c\u8be6\u7ec6\u7684\u4ecb\u7ecd\u3002\uff08\u53c2\u89c1\uff1a 2.11 - \u5934\u6587\u4ef6 \uff09 \u5b8f\u5b9a\u4e49 #define \u6307\u4ee4\u53ef\u4ee5\u88ab\u7528\u6765\u5b9a\u4e49 \u5b8f(macro) \u3002\u5728C++\u4e2d\uff0c\u5b8f\u6307\u7684\u662f\u4e00\u79cd\u89c4\u5219\uff0c\u8fd9\u79cd\u89c4\u5219\u89c4\u5b9a\u4e86\u4e00\u4e2a\u8f93\u5165\u6587\u672c\u5e94\u8be5\u5982\u4f55\u88ab\u66ff\u6362\u6210\u8f93\u51fa\u6587\u672c\u3002 \u5b8f\u6709\u4e24\u79cd\u57fa\u672c\u7c7b\u578b\uff1a \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f(object-like macros) \u548c \u51fd\u6570\u7c7b\u578b\u7684\u5b8f (function-like macros) \u3002 \u51fd\u6570\u7c7b\u578b\u7684\u5b8f \u548c\u51fd\u6570\u5f88\u50cf\uff0c\u5176\u529f\u80fd\u4e5f\u7c7b\u4f3c\u3002\u6211\u4eec\u5e76\u4e0d\u4f1a\u5728\u8fd9\u91cc\u8be6\u7ec6\u8ba8\u8bba\u5b83\uff0c\u56e0\u4e3a\u5b83\u5e38\u5e38\u88ab\u8ba4\u4e3a\u662f\u4e0d\u5b89\u5168\u7684\uff0c\u51b5\u4e14\u5b83\u80fd\u505a\u7684\u666e\u901a\u51fd\u6570\u4e5f\u80fd\u505a\uff0c \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f \u6709\u4e24\u79cd\u58f0\u660e\u65b9\u5f0f\uff1a 1 2 #define identifier #define identifier substitution_text \u4e0a\u9762\u4e00\u79cd\u65b9\u5f0f\u4e0d\u4f1a\u5bf9\u6587\u672c\u8fdb\u884c\u66ff\u6362\uff0c\u800c\u4e0b\u9762\u4e00\u79cd\u4f1a\u3002\u56e0\u4e3a\u8fd9\u4e9b\u90fd\u5c5e\u4e8e \u9884\u5904\u7406\u5668\u6307\u4ee4 \u800c\u4e0d\u662f\u8bed\u53e5\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u4ee5\u5206\u53f7\u7ed3\u5c3e\uff0c \u6709\u66ff\u6362\u6587\u672c\u7684\u5bf9\u8c61\u7c7b\u578b\u5b8f \u5f53\u9884\u5904\u7406\u5668\u9047\u5230\u8be5\u6307\u4ee4\u65f6\uff0c\u4efb\u4f55\u4f7f\u7528\u8be5\u6807\u8bc6\u7b26\u7684\u5730\u65b9\u90fd\u4f1a\u88ab\u66ff\u6362\u4e3a\u5bf9\u5e94\u7684 \u66ff\u6362\u6587\u672c \u3002\u4e00\u822c\u6765\u8bf4\uff0c\u8fd9\u4e9b\u6807\u8bc6\u7b26\u4f1a\u4ee5\u5168\u5927\u5199\u7684\u5f62\u5f0f\u4f7f\u7528\uff0c\u540c\u65f6\u4f7f\u7528\u4e0b\u5212\u7ebf\u4ee3\u66ff\u7a7a\u683c\u3002 \u8003\u8651\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 #include #define MY_NAME \"Alex\" int main () { std :: cout << \"My name is: \" << MY_NAME ; return 0 ; } \u9884\u5904\u7406\u5668\u4f1a\u628a\u4e0a\u9762\u7684\u4ee3\u7801\u8f6c\u6362\u6210\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 // iostream \u7684\u5185\u5bb9\u4f1a\u88ab\u63d2\u5165\u5230\u8fd9\u91cc int main () { std :: cout << \"My name is: \" << \"Alex\" ; return 0 ; } \u56e0\u6b64\u4e0a\u8ff0\u7a0b\u5e8f\u4f1a\u6253\u5370 My name is: Alex \u3002 \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f\u8fc7\u53bb\u4e5f\u88ab\u7528\u6765\u4f5c\u5b9a\u4e49\u5e38\u91cf\uff08\u4e00\u79cd\u5f00\u9500\u66f4\u5c0f\u7684\u65b9\u5f0f\uff09\u3002\u968f\u7740\u7f16\u8bd1\u5668\u7684\u53d1\u5c55\u548c\u8bed\u8a00\u7684\u66f4\u65b0\uff0c\u6b64\u7c7b\u9700\u6c42\u5728\u73b0\u5982\u4eca\u5df2\u7ecf\u6ca1\u5fc5\u8981\u4e86\u3002\u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f\u5e94\u8be5\u4ec5\u51fa\u73b0\u5728\u5386\u53f2\u9057\u7559\u4ee3\u7801\u4e2d\u624d\u5bf9\u3002 \u6211\u4eec\u5efa\u8bae\u5f7b\u5e95\u653e\u5f03\u4f7f\u7528\u6b64\u7c7b\u5b8f\u5b9a\u4e49\uff0c\u56e0\u4e3a\u6709\u66f4\u597d\u7684\u65b9\u6cd5\u53ef\u4ee5\u7528\u3002\u6211\u4eec\u4f1a\u5728 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \u4e2d\u8fdb\u884c\u4ecb\u7ecd \u65e0\u66ff\u6362\u6587\u672c\u7684\u5bf9\u8c61\u7c7b\u578b\u5b8f \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f\u4e5f\u53ef\u4ee5\u5728\u6ca1\u6709\u66ff\u6362\u6587\u672c\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\uff1a \u4f8b\u5982\uff1a 1 #define USE_YEN \u8fd9\u4e2a\u5b8f\u7684\u4f5c\u7528\u4f60\u5e94\u8be5\u80fd\u591f\u731c\u5230\uff1a\u4efb\u4f55\u9047\u5230\u8be5\u6807\u8bc6\u7b26\u7684\u5730\u65b9\uff0c\u90fd\u4f1a\u88ab\u66ff\u6362\u4e3a\u7a7a\u767d\uff01 \u770b\u4e0a\u53bb\u8fd9\u4e2a\u5b8f\u597d\u50cf\u6ca1\u4ec0\u4e48\u7528\uff0c\u786e\u5b9e\uff0c\u5bf9\u4e8e\u6587\u672c\u66ff\u6362\u6765\u8bf4\u5b83\u786e\u5b9e\u6ca1\u4ec0\u4e48\u7528\u3002\u4e0d\u8fc7\uff0c\u8fd9\u79cd\u7c7b\u578b\u7684\u5b8f\u672c\u6765\u5c31\u4e0d\u662f\u7528\u6765\u8fdb\u884c\u66ff\u6362\u7684\uff0c\u5f88\u5feb\u4f60\u5c31\u4f1a\u770b\u5230\u5b83\u7684\u5b9e\u9645\u7528\u9014\u3002 \u548c\u6709\u66ff\u6362\u6587\u672c\u7684\u5b8f\u4e0d\u540c\uff0c\u8fd9\u79cd\u7c7b\u578b\u7684\u5b8f\u901a\u5e38\u88ab\u8ba4\u4e3a\u662f\u53ef\u4ee5\u4f7f\u7528\u7684\u3002 \u6761\u4ef6\u7f16\u8bd1 \u6761\u4ef6\u7f16\u8bd1\u9884\u5904\u7406\u5668\u6307\u4ee4\u4f7f\u6211\u4eec\u53ef\u4ee5\u63a7\u5236\u5728\u4f55\u79cd\u6761\u4ef6\u4e0b\uff0c\u5bf9\u5e94\u7684\u4ee3\u7801\u9700\u8981\u7f16\u8bd1\u6216\u8005\u4e0d\u9700\u8981\u7f16\u8bd1\u3002\u6761\u4ef6\u7f16\u8bd1\u6307\u4ee4\u7684\u79cd\u7c7b\u6709\u5f88\u591a\uff0c\u8fd9\u91cc\u6211\u4eec\u53ea\u4ecb\u7ecd\u4e09\u79cd\u6700\u4e3a\u5e38\u7528\u7684\uff1a #ifdef \u3001 #ifndef_ \u548c #endif \u3002 #ifdef \u6307\u4ee4\u4f1a\u8ba9\u9884\u5904\u7406\u5668\u68c0\u67e5\u67d0\u4e2a\u6807\u8bc6\u7b26\u662f\u5426\u88ab #define \u8fc7\uff0c\u5982\u679c\u662f\u7684\u8bdd\uff0c #ifdef \u548c #endif \u4e4b\u95f4\u7684\u4ee3\u7801\u5c06\u4f1a\u88ab\u7f16\u8bd1\uff0c\u5426\u5219\u8fd9\u4e9b\u4ee3\u7801\u4f1a\u88ab\u5ffd\u7565\u3002 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #define PRINT_JOE int main () { #ifdef PRINT_JOE std :: cout << \"Joe \\n \" ; // will be compiled since PRINT_JOE is defined #endif #ifdef PRINT_BOB std :: cout << \"Bob \\n \" ; // will be ignored since PRINT_BOB is not defined #endif return 0 ; } \u56e0\u4e3a PRINT_JOE \u88ab #defined \u4e86\uff0c\u56e0\u6b64 std::cout << \"Joe\\n\" \u4f1a\u88ab\u7f16\u8bd1\uff0c\u800c\u56e0\u4e3a PRINT_BOB \u6ca1\u6709\u88ab #defined \uff0c\u6240\u4ee5\u5bf9\u5e94\u7684 std::cout << \"Bob\\n\" \u4f1a\u88ab\u5ffd\u7565\u3002 #ifndef \u662f #ifdef \u7684\u53cd\u4e49\u8bcd\uff0c\u5b83\u63a7\u5236\u9884\u5904\u7406\u5668\u68c0\u67e5\u67d0\u4e2a\u6807\u8bc6\u7b26\u662f\u5426 \u6ca1\u6709 \u88ab #define \u3002 1 2 3 4 5 6 7 8 9 10 #include int main () { #ifndef PRINT_BOB std :: cout << \"Bob \\n \" ; #endif return 0 ; } \u4e0a\u9762\u7684\u7a0b\u5e8f\u4f1a\u6253\u5370 \u201cBob\u201d\uff0c\u56e0\u4e3a PRINT_BOB \u6ca1\u6709\u88ab #define_ \u8fc7\u3002 \u9664\u4e86 #ifdef PRINT_BOB \u548c #ifndef PRINT_BOB \u8fd9\u6837\u7684\u5f62\u5f0f\u4ee5\u5916\uff0c\u4f60\u4e5f\u53ef\u80fd\u4f1a\u9047\u5230 #if defined(PRINT_BOB) \u548c #if !defined(PRINT_BOB) \u8fd9\u6837\u7684\u5f62\u5f0f\u3002\u5b83\u4eec\u7684\u529f\u80fd\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u53ea\u4e0d\u8fc7\u540e\u8005\u770b\u4e0a\u53bb\u548cC++\u7684\u8bed\u6cd5\u98ce\u683c\u66f4\u52a0\u4e00\u81f4\u3002 #if 0 \u8fd8\u6709\u4e00\u4e2a\u5e38\u7528\u7684\u6761\u4ef6\u7f16\u8bd1\u6307\u4ee4\u662f #if 0 \uff0c\u5b83\u53ef\u4ee5\u5c06\u5b83\u5305\u88f9\u8303\u56f4\u5185\u7684\u4ee3\u7801\u6392\u9664\u5728\u7f16\u8bd1\u4e4b\u5916\uff08\u5c31\u597d\u50cf\u8fd9\u4e9b\u4ee3\u7801\u88ab\u6ce8\u91ca\u6389\u4e86\u4e00\u6837\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { std :: cout << \"Joe \\n \" ; #if 0 // \u4ece\u6b64\u884c\u5f00\u59cb\u7684\u4ee3\u7801\u90fd\u4e0d\u7f16\u8bd1 std::cout << \"Bob\\n\"; std::cout << \"Steve\\n\"; #endif // \u76f4\u5230\u8fd9\u4e00\u884c\u4e3a\u6b62 return 0 ; } \u4e0a\u9762\u7684\u7a0b\u5e8f\u53ea\u4f1a\u6253\u5370 \u201cJoe\u201d\uff0c\u56e0\u4e3a\u201cBob\u201d \u548c \u201cSteve\u201d \u5747\u4f4d\u4e8e #if 0 \u5757\u4e2d\uff0c\u6240\u4ee5\u9884\u5904\u7406\u5668\u4f1a\u5c06\u5176\u6392\u9664\u5728\u7f16\u8bd1\u8303\u56f4\u5916\u3002 \u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u65b9\u4fbf\u7684\u201d\u6ce8\u91ca\u6389\u201c\u67d0\u4e9b\u5305\u542b\u4e86\u591a\u884c\u6ce8\u91ca\u7684\u4ee3\u7801\u4e86\uff08\u56e0\u4e3a\u591a\u884c\u6ce8\u91ca\u4e0d\u652f\u6301\u5d4c\u5957\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { std :: cout << \"Joe \\n \" ; #if 0 // Don't compile anything starting here std::cout << \"Bob\\n\"; /* Some * multi-line * comment here */ std::cout << \"Steve\\n\"; #endif // until this point return 0 ; } \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u9884\u5904\u7406\u5668\u6307\u4ee4 \u4f60\u4e5f\u8bb8\u4f1a\u5bf9\u4e0b\u9762\u8fd9\u79cd\u5199\u6cd5\u5fc3\u5b58\u987e\u8651\uff1a 1 2 3 4 #define PRINT_JOE #ifdef PRINT_JOE // ... \u65e2\u7136\u6211\u4eec\u5b9a\u4e49\u4e86 PRINT_JOE \u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u9884\u5904\u7406\u5668\u4e0d\u4f1a\u5c06 #ifdef PRINT_JOE \u4e2d\u7684 PRINT_JOE \u66ff\u6362\u6389\u5462\uff1f \u5b8f\u5b9a\u4e49\u7684\u66ff\u6362\u53ea\u9488\u5bf9\u4e0e\u666e\u901a\u4ee3\u7801\u3002\u5176\u4ed6\u9884\u5904\u7406\u5668\u7684\u6307\u4ee4\u662f\u4e0d\u4f1a\u53d7\u5230\u5f71\u54cd\u7684\uff0c\u56e0\u6b64 #ifdef PRINT_JOE \u4e2d\u7684 PRINT_JOE \u662f\u4f1a\u4fdd\u7559\u7684\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 #define FOO 9 // \u5b8f\u66ff\u6362\u4e3a\u7a7a #ifdef FOO // \u6b64\u5904\u7684 FOO \u4e0d\u4f1a\u88ab\u66ff\u6362\u56e0\u4e3a\u5b83\u5c5e\u4e8e\u9884\u5904\u7406\u5668\u6307\u4ee4 std :: cout << FOO ; // \u6b64\u5904\u7684FOO\u4f1a\u88ab\u66ff\u6362\u56e0\u4e3a\u5b83\u5c5e\u4e8e\u666e\u901a\u4ee3\u7801 #endif \u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\uff0c\u9884\u5904\u7406\u5668\u8f93\u51fa\u7684\u7ed3\u679c\u4e2d\u4e0d\u5305\u542b\u4efb\u4f55\u9884\u5904\u7406\u5668\u6307\u4ee4\u2014\u2014\u5b83\u4eec\u90fd\u4f1a\u5728\u7f16\u8bd1\u524d\u88ab\u5904\u7406\u6389\uff0c\u6bd4\u8f83\u7f16\u8bd1\u5668\u5e76\u4e0d\u77e5\u9053\u8981\u5982\u4f55\u5904\u7406\u8fd9\u4e9b\u6307\u4ee4\u3002 \u5b8f\u5b9a\u4e49\u7684\u4f5c\u7528\u57df \u9884\u5904\u7406\u5668\u6307\u4ee4\u7684\u89e3\u6790\u4f1a\u5728\u7f16\u8bd1\u5668\u5b8c\u6210\uff08\u4ece\u4e0a\u5230\u4e0b\u9010\u6587\u4ef6\u5730\uff09\u3002 \u8003\u8651\u5982\u4e0b\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void foo () { #define MY_NAME \"Alex\" } int main () { std :: cout << \"My name is: \" << MY_NAME ; return 0 ; } \u5c3d\u7ba1\u770b\u4e0a\u53bb #define MY_NAME \u201cAlex\u201d \u50cf\u662f\u88ab\u5b9a\u4e49\u5230\u4e86 foo \u51fd\u6570\u4e2d\uff0c\u4f46\u9884\u5904\u7406\u5668\u5e76\u4e0d\u4f1a\u610f\u8bc6\u5230\u8fd9\u4e00\u70b9\uff0c\u56e0\u4e3a\u5b83\u4e0d\u7406\u89e3\u201d\u51fd\u6570\u201c\u4ee5\u53ca\u5176\u4ed6C++\u7684\u8bed\u6cd5\u6982\u5ff5\u3002\u56e0\u6b64\uff0c\u8fd9\u6bb5\u4ee3\u7801\u7684\u6548\u679c\u597d\u6211\u4eec\u5728\u51fd\u6570\u4e4b\u524d\u6216\u662f\u4e4b\u540e\u5b9a\u4e49 #define MY_NAME \u201cAlex\u201d \u662f\u4e00\u6a21\u4e00\u6837\u7684\u3002\u5904\u4e8e\u53ef\u8bfb\u6027\u7684\u8003\u8651\uff0c\u4e00\u822c #define \u4e0d\u4f1a\u88ab\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8\u3002 \u5f53\u9884\u5904\u7406\u5668\u7684\u5de5\u4f5c\u5b8c\u6210\u540e\uff0c\u6240\u6709\u8fd9\u4e9b #define \u7684\u6807\u8bc6\u7b26\u5c31\u90fd\u88ab\u4e22\u5f03\u4e86\u3002\u56e0\u6b64\u8fd9\u4e9b\u6807\u8bc6\u7b26\u53ea\u6709\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u662f\u6709\u6548\u7684\uff0c\u5b9a\u4e49\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u7684\u5b8f\u4e0d\u4f1a\u5f71\u54cd\u5230\u5176\u4ed6\u6587\u4ef6\u3002 \u8003\u8651\u8fd9\u4e2a\u4f8b\u5b50\uff1a function.cpp: 1 2 3 4 5 6 7 8 9 10 11 #include void doSomething () { #ifdef PRINT std :: cout << \"Printing!\" ; #endif #ifndef PRINT std :: cout << \"Not printing!\" ; #endif } main.cpp: 1 2 3 4 5 6 7 8 9 10 void doSomething (); // forward declaration for function doSomething() #define PRINT int main () { doSomething (); return 0 ; } \u4e0a\u9762\u4ee3\u7801\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 Not printing! \u5c3d\u7ba1 PRINT \u88ab\u5b9a\u4e49\u5728\u4e86 main.cpp \u4e2d\uff0c\u4f46\u662f\u5b83\u4e1d\u6beb\u4e0d\u4f1a\u5f71\u54cd function.cpp \uff08\u5bf9 PRINT \u7684\u5b9a\u4e49\u53ea\u5728 main.cpp \u4e2d\u6709\u6548\uff09\u3002\u6211\u4eec\u4f1a\u5728 2.12 - \u5934\u6587\u4ef6\u91cd\u590d\u5305\u542b\u4fdd\u62a4 \u4e2d\u63a2\u8ba8\u5176\u5f71\u54cd\u3002","title":"2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb"},{"location":"2-10-Introduction-to-the-preprocessor/#_1","text":"\u4f60\u53ef\u80fd\u4f1a\u89c9\u5f97\u7f16\u8bd1\u5668\u4f1a\u539f\u5c01\u4e0d\u52a8\u5730\u7f16\u8bd1\u4f60\u5199\u7684\u4ee3\u7801\u3002\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u3002 \u5728\u5f00\u59cb\u7f16\u8bd1\u524d\uff0c\u4ee3\u7801\u4f1a\u7ecf\u8fc7\u4e00\u4e2a\u53eb\u505a \u7ffb\u8bd1(translation) \u7684\u9636\u6bb5\u3002\u5728\u8fd9\u4e2a\u9636\u6bb5\u9700\u8981\u5b8c\u6210\u5f88\u591a\u64cd\u4f5c\uff0c\u624d\u80fd\u4f7f\u4f60\u7684\u4ee3\u7801\u5177\u5907\u88ab\u7f16\u8bd1\u7684\u6761\u4ef6\uff08\u5982\u679c\u6709\u5174\u8da3\u53ef\u4ee5\u5728 \u8fd9\u91cc \u67e5\u770b\u6709\u5173\u7ffb\u8bd1\u9636\u6bb5\u7684\u8be6\u7ec6\u4fe1\u606f\uff09\u3002\u8fdb\u884c\u8fc7\u7ffb\u8bd1\u7684\u4ee3\u7801\u6587\u4ef6\u53eb\u505a\u4e00\u4e2a\u7ffb\u8bd1\u5355\u5143\u3002 \u5728\u7ffb\u8bd1\u9636\u6bb5\u4e2d\u6700\u503c\u5f97\u6211\u4eec\u5173\u6ce8\u7684\u662f \u9884\u5904\u7406\u5668 \uff0c\u5b83\u53ef\u4ee5\u88ab\u770b\u505a\u662f\u4e00\u4e2a\u5bf9\u6bcf\u4e2a\u4ee3\u7801\u6587\u4ef6\u8fdb\u884c\u6587\u672c\u5904\u7406\u7684\u5355\u72ec\u7684\u5de5\u5177\u3002 \u5728\u9884\u5904\u7406\u5668\u5de5\u4f5c\u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u4ece\u5934\u5230\u5c3e\u626b\u63cf\u4ee3\u7801\u6587\u4ef6\uff0c\u67e5\u627e \u9884\u5904\u7406\u5668\u6307\u4ee4 (preprocessor directive) \u3002\u9884\u5904\u7406\u5668\u6307\u4ee4\uff08\u7ecf\u5e38\u7b80\u79f0\u4e3a\u6307\u4ee4\uff09\u662f\u4e00\u4e9b\u4ee5 # \u5f00\u5934\uff0c\u4ee5\u6362\u884c\uff08\u800c\u975e\u5206\u53f7\uff09\u7ed3\u5c3e\u7684\u7b26\u53f7\u3002\u8fd9\u4e9b\u6307\u4ee4\u4f1a\u544a\u8bc9\u9884\u5904\u7406\u5668\u5e94\u8be5\u5bf9\u6587\u672c\u6267\u884c\u54ea\u4e9b\u7279\u5b9a\u7684\u64cd\u4f5c\u3002 \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u9884\u5904\u7406\u5668\u5e76\u4e0d\u7406\u89e3C++\u7684\u8bed\u6cd5\u2014\u2014\u76f8\u53cd\uff0c\u9884\u5904\u7406\u5668\u6307\u4ee4\u6709\u5176\u81ea\u5df1\u7684\u8bed\u6cd5\uff08\u6709\u4e9b\u548cC++\u8bed\u6cd5\u7c7b\u4f3c\uff0c\u6709\u4e9b\u5219\u4e0d\u7136\uff09\u3002 \u9884\u5904\u7406\u5668\u7684\u8f93\u51fa\u4f1a\u7ecf\u5386\u4e00\u4e9b\u5217\u7ffb\u8bd1\u9636\u6bb5\uff0c\u7136\u540e\u624d\u4f1a\u88ab\u7f16\u8bd1\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u9884\u5904\u7406\u5668\u5e76\u4e0d\u4f1a\u4fee\u6539\u539f\u6587\u4ef6\u2014\u2014\u6240\u6709\u7ecf\u9884\u5904\u7406\u5668\u4fee\u6539\u4ea7\u751f\u7684\u53d8\u5316\u90fd\u662f\u4e34\u65f6\u7684\uff08\u7528\u4e8e\u4e00\u6b21\u7f16\u8bd1\uff09 \u672c\u8282\u8bfe\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u6700\u4e3a\u5e38\u89c1\u7684\u9884\u5904\u7406\u5668\u6307\u4ee4\u3002 \u9898\u5916\u8bdd Using \u6307\u4ee4 ( 2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 \u4e2d\u4ecb\u7ecd)\u5e76\u4e0d\u5c5e\u4e8e\u9884\u5904\u7406\u5668\u6307\u4ee4\uff08\u56e0\u6b64\u4e0d\u4f1a\u88ab\u9884\u5904\u7406\u5668\u5904\u7406\uff09\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u6307\u4ee4(directive)\u4e00\u8bcd\u591a\u6307\u9884\u5904\u7406\u5668\u6307\u4ee4\uff0c\u4f46\u4e5f\u4e0d\u662f\u603b\u662f\u8fd9\u6837\u3002","title":"\u7ffb\u8bd1\u53ca\u9884\u5904\u7406\u5668"},{"location":"2-10-Introduction-to-the-preprocessor/#includes","text":"\u6211\u4eec\u5df2\u7ecf\u5728\u5b9e\u9645\u6848\u4f8b\u4e2d\u63a5\u89e6\u8fc7 #include \u6307\u4ee4\u4e86\uff08 #include \uff09\u3002\u5728 #include \u67d0\u4e2a\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u9884\u5904\u7406\u5668\u4f1a\u628a #include \u66ff\u6362\u4e3a\u8be5\u6587\u4ef6\u7684\u5b9e\u9645\u5185\u5bb9\uff0c\u7136\u540e\u9884\u5904\u7406\u5668\u624d\u4f1a\u5bf9\u66ff\u6362\u540e\u7684\u6587\u4ef6\u5185\u5bb9\u8fdb\u884c\u9884\u5904\u7406\u548c\u7f16\u8bd1\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << \"Hello, world!\" ; return 0 ; } \u5f53\u9884\u5904\u7406\u5668\u5904\u7406\u8be5\u7a0b\u5e8f\u65f6\uff0c\u5b83\u4f1a\u628a #include \u66ff\u6362\u4e3a\u7ecf\u8fc7\u9884\u5904\u7406\u7684\u540d\u4e3a\u201ciostream\u201d\u7684\u6587\u4ef6\u7684\u5185\u5bb9\u3002 \u56e0\u4e3a #include \u51e0\u4e4e\u53ea\u7528\u6765\u5305\u542b\u5934\u6587\u4ef6\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u8ba8\u8bba\u5934\u6587\u4ef6\u65f6\u518d\u5bf9\u5176\u8fdb\u884c\u8be6\u7ec6\u7684\u4ecb\u7ecd\u3002\uff08\u53c2\u89c1\uff1a 2.11 - \u5934\u6587\u4ef6 \uff09","title":"Includes"},{"location":"2-10-Introduction-to-the-preprocessor/#_2","text":"#define \u6307\u4ee4\u53ef\u4ee5\u88ab\u7528\u6765\u5b9a\u4e49 \u5b8f(macro) \u3002\u5728C++\u4e2d\uff0c\u5b8f\u6307\u7684\u662f\u4e00\u79cd\u89c4\u5219\uff0c\u8fd9\u79cd\u89c4\u5219\u89c4\u5b9a\u4e86\u4e00\u4e2a\u8f93\u5165\u6587\u672c\u5e94\u8be5\u5982\u4f55\u88ab\u66ff\u6362\u6210\u8f93\u51fa\u6587\u672c\u3002 \u5b8f\u6709\u4e24\u79cd\u57fa\u672c\u7c7b\u578b\uff1a \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f(object-like macros) \u548c \u51fd\u6570\u7c7b\u578b\u7684\u5b8f (function-like macros) \u3002 \u51fd\u6570\u7c7b\u578b\u7684\u5b8f \u548c\u51fd\u6570\u5f88\u50cf\uff0c\u5176\u529f\u80fd\u4e5f\u7c7b\u4f3c\u3002\u6211\u4eec\u5e76\u4e0d\u4f1a\u5728\u8fd9\u91cc\u8be6\u7ec6\u8ba8\u8bba\u5b83\uff0c\u56e0\u4e3a\u5b83\u5e38\u5e38\u88ab\u8ba4\u4e3a\u662f\u4e0d\u5b89\u5168\u7684\uff0c\u51b5\u4e14\u5b83\u80fd\u505a\u7684\u666e\u901a\u51fd\u6570\u4e5f\u80fd\u505a\uff0c \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f \u6709\u4e24\u79cd\u58f0\u660e\u65b9\u5f0f\uff1a 1 2 #define identifier #define identifier substitution_text \u4e0a\u9762\u4e00\u79cd\u65b9\u5f0f\u4e0d\u4f1a\u5bf9\u6587\u672c\u8fdb\u884c\u66ff\u6362\uff0c\u800c\u4e0b\u9762\u4e00\u79cd\u4f1a\u3002\u56e0\u4e3a\u8fd9\u4e9b\u90fd\u5c5e\u4e8e \u9884\u5904\u7406\u5668\u6307\u4ee4 \u800c\u4e0d\u662f\u8bed\u53e5\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u4ee5\u5206\u53f7\u7ed3\u5c3e\uff0c","title":"\u5b8f\u5b9a\u4e49"},{"location":"2-10-Introduction-to-the-preprocessor/#_3","text":"\u5f53\u9884\u5904\u7406\u5668\u9047\u5230\u8be5\u6307\u4ee4\u65f6\uff0c\u4efb\u4f55\u4f7f\u7528\u8be5\u6807\u8bc6\u7b26\u7684\u5730\u65b9\u90fd\u4f1a\u88ab\u66ff\u6362\u4e3a\u5bf9\u5e94\u7684 \u66ff\u6362\u6587\u672c \u3002\u4e00\u822c\u6765\u8bf4\uff0c\u8fd9\u4e9b\u6807\u8bc6\u7b26\u4f1a\u4ee5\u5168\u5927\u5199\u7684\u5f62\u5f0f\u4f7f\u7528\uff0c\u540c\u65f6\u4f7f\u7528\u4e0b\u5212\u7ebf\u4ee3\u66ff\u7a7a\u683c\u3002 \u8003\u8651\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 #include #define MY_NAME \"Alex\" int main () { std :: cout << \"My name is: \" << MY_NAME ; return 0 ; } \u9884\u5904\u7406\u5668\u4f1a\u628a\u4e0a\u9762\u7684\u4ee3\u7801\u8f6c\u6362\u6210\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 // iostream \u7684\u5185\u5bb9\u4f1a\u88ab\u63d2\u5165\u5230\u8fd9\u91cc int main () { std :: cout << \"My name is: \" << \"Alex\" ; return 0 ; } \u56e0\u6b64\u4e0a\u8ff0\u7a0b\u5e8f\u4f1a\u6253\u5370 My name is: Alex \u3002 \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f\u8fc7\u53bb\u4e5f\u88ab\u7528\u6765\u4f5c\u5b9a\u4e49\u5e38\u91cf\uff08\u4e00\u79cd\u5f00\u9500\u66f4\u5c0f\u7684\u65b9\u5f0f\uff09\u3002\u968f\u7740\u7f16\u8bd1\u5668\u7684\u53d1\u5c55\u548c\u8bed\u8a00\u7684\u66f4\u65b0\uff0c\u6b64\u7c7b\u9700\u6c42\u5728\u73b0\u5982\u4eca\u5df2\u7ecf\u6ca1\u5fc5\u8981\u4e86\u3002\u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f\u5e94\u8be5\u4ec5\u51fa\u73b0\u5728\u5386\u53f2\u9057\u7559\u4ee3\u7801\u4e2d\u624d\u5bf9\u3002 \u6211\u4eec\u5efa\u8bae\u5f7b\u5e95\u653e\u5f03\u4f7f\u7528\u6b64\u7c7b\u5b8f\u5b9a\u4e49\uff0c\u56e0\u4e3a\u6709\u66f4\u597d\u7684\u65b9\u6cd5\u53ef\u4ee5\u7528\u3002\u6211\u4eec\u4f1a\u5728 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \u4e2d\u8fdb\u884c\u4ecb\u7ecd","title":"\u6709\u66ff\u6362\u6587\u672c\u7684\u5bf9\u8c61\u7c7b\u578b\u5b8f"},{"location":"2-10-Introduction-to-the-preprocessor/#_4","text":"\u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f\u4e5f\u53ef\u4ee5\u5728\u6ca1\u6709\u66ff\u6362\u6587\u672c\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\uff1a \u4f8b\u5982\uff1a 1 #define USE_YEN \u8fd9\u4e2a\u5b8f\u7684\u4f5c\u7528\u4f60\u5e94\u8be5\u80fd\u591f\u731c\u5230\uff1a\u4efb\u4f55\u9047\u5230\u8be5\u6807\u8bc6\u7b26\u7684\u5730\u65b9\uff0c\u90fd\u4f1a\u88ab\u66ff\u6362\u4e3a\u7a7a\u767d\uff01 \u770b\u4e0a\u53bb\u8fd9\u4e2a\u5b8f\u597d\u50cf\u6ca1\u4ec0\u4e48\u7528\uff0c\u786e\u5b9e\uff0c\u5bf9\u4e8e\u6587\u672c\u66ff\u6362\u6765\u8bf4\u5b83\u786e\u5b9e\u6ca1\u4ec0\u4e48\u7528\u3002\u4e0d\u8fc7\uff0c\u8fd9\u79cd\u7c7b\u578b\u7684\u5b8f\u672c\u6765\u5c31\u4e0d\u662f\u7528\u6765\u8fdb\u884c\u66ff\u6362\u7684\uff0c\u5f88\u5feb\u4f60\u5c31\u4f1a\u770b\u5230\u5b83\u7684\u5b9e\u9645\u7528\u9014\u3002 \u548c\u6709\u66ff\u6362\u6587\u672c\u7684\u5b8f\u4e0d\u540c\uff0c\u8fd9\u79cd\u7c7b\u578b\u7684\u5b8f\u901a\u5e38\u88ab\u8ba4\u4e3a\u662f\u53ef\u4ee5\u4f7f\u7528\u7684\u3002","title":"\u65e0\u66ff\u6362\u6587\u672c\u7684\u5bf9\u8c61\u7c7b\u578b\u5b8f"},{"location":"2-10-Introduction-to-the-preprocessor/#_5","text":"\u6761\u4ef6\u7f16\u8bd1\u9884\u5904\u7406\u5668\u6307\u4ee4\u4f7f\u6211\u4eec\u53ef\u4ee5\u63a7\u5236\u5728\u4f55\u79cd\u6761\u4ef6\u4e0b\uff0c\u5bf9\u5e94\u7684\u4ee3\u7801\u9700\u8981\u7f16\u8bd1\u6216\u8005\u4e0d\u9700\u8981\u7f16\u8bd1\u3002\u6761\u4ef6\u7f16\u8bd1\u6307\u4ee4\u7684\u79cd\u7c7b\u6709\u5f88\u591a\uff0c\u8fd9\u91cc\u6211\u4eec\u53ea\u4ecb\u7ecd\u4e09\u79cd\u6700\u4e3a\u5e38\u7528\u7684\uff1a #ifdef \u3001 #ifndef_ \u548c #endif \u3002 #ifdef \u6307\u4ee4\u4f1a\u8ba9\u9884\u5904\u7406\u5668\u68c0\u67e5\u67d0\u4e2a\u6807\u8bc6\u7b26\u662f\u5426\u88ab #define \u8fc7\uff0c\u5982\u679c\u662f\u7684\u8bdd\uff0c #ifdef \u548c #endif \u4e4b\u95f4\u7684\u4ee3\u7801\u5c06\u4f1a\u88ab\u7f16\u8bd1\uff0c\u5426\u5219\u8fd9\u4e9b\u4ee3\u7801\u4f1a\u88ab\u5ffd\u7565\u3002 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #define PRINT_JOE int main () { #ifdef PRINT_JOE std :: cout << \"Joe \\n \" ; // will be compiled since PRINT_JOE is defined #endif #ifdef PRINT_BOB std :: cout << \"Bob \\n \" ; // will be ignored since PRINT_BOB is not defined #endif return 0 ; } \u56e0\u4e3a PRINT_JOE \u88ab #defined \u4e86\uff0c\u56e0\u6b64 std::cout << \"Joe\\n\" \u4f1a\u88ab\u7f16\u8bd1\uff0c\u800c\u56e0\u4e3a PRINT_BOB \u6ca1\u6709\u88ab #defined \uff0c\u6240\u4ee5\u5bf9\u5e94\u7684 std::cout << \"Bob\\n\" \u4f1a\u88ab\u5ffd\u7565\u3002 #ifndef \u662f #ifdef \u7684\u53cd\u4e49\u8bcd\uff0c\u5b83\u63a7\u5236\u9884\u5904\u7406\u5668\u68c0\u67e5\u67d0\u4e2a\u6807\u8bc6\u7b26\u662f\u5426 \u6ca1\u6709 \u88ab #define \u3002 1 2 3 4 5 6 7 8 9 10 #include int main () { #ifndef PRINT_BOB std :: cout << \"Bob \\n \" ; #endif return 0 ; } \u4e0a\u9762\u7684\u7a0b\u5e8f\u4f1a\u6253\u5370 \u201cBob\u201d\uff0c\u56e0\u4e3a PRINT_BOB \u6ca1\u6709\u88ab #define_ \u8fc7\u3002 \u9664\u4e86 #ifdef PRINT_BOB \u548c #ifndef PRINT_BOB \u8fd9\u6837\u7684\u5f62\u5f0f\u4ee5\u5916\uff0c\u4f60\u4e5f\u53ef\u80fd\u4f1a\u9047\u5230 #if defined(PRINT_BOB) \u548c #if !defined(PRINT_BOB) \u8fd9\u6837\u7684\u5f62\u5f0f\u3002\u5b83\u4eec\u7684\u529f\u80fd\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u53ea\u4e0d\u8fc7\u540e\u8005\u770b\u4e0a\u53bb\u548cC++\u7684\u8bed\u6cd5\u98ce\u683c\u66f4\u52a0\u4e00\u81f4\u3002","title":"\u6761\u4ef6\u7f16\u8bd1"},{"location":"2-10-Introduction-to-the-preprocessor/#if-0","text":"\u8fd8\u6709\u4e00\u4e2a\u5e38\u7528\u7684\u6761\u4ef6\u7f16\u8bd1\u6307\u4ee4\u662f #if 0 \uff0c\u5b83\u53ef\u4ee5\u5c06\u5b83\u5305\u88f9\u8303\u56f4\u5185\u7684\u4ee3\u7801\u6392\u9664\u5728\u7f16\u8bd1\u4e4b\u5916\uff08\u5c31\u597d\u50cf\u8fd9\u4e9b\u4ee3\u7801\u88ab\u6ce8\u91ca\u6389\u4e86\u4e00\u6837\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { std :: cout << \"Joe \\n \" ; #if 0 // \u4ece\u6b64\u884c\u5f00\u59cb\u7684\u4ee3\u7801\u90fd\u4e0d\u7f16\u8bd1 std::cout << \"Bob\\n\"; std::cout << \"Steve\\n\"; #endif // \u76f4\u5230\u8fd9\u4e00\u884c\u4e3a\u6b62 return 0 ; } \u4e0a\u9762\u7684\u7a0b\u5e8f\u53ea\u4f1a\u6253\u5370 \u201cJoe\u201d\uff0c\u56e0\u4e3a\u201cBob\u201d \u548c \u201cSteve\u201d \u5747\u4f4d\u4e8e #if 0 \u5757\u4e2d\uff0c\u6240\u4ee5\u9884\u5904\u7406\u5668\u4f1a\u5c06\u5176\u6392\u9664\u5728\u7f16\u8bd1\u8303\u56f4\u5916\u3002 \u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u65b9\u4fbf\u7684\u201d\u6ce8\u91ca\u6389\u201c\u67d0\u4e9b\u5305\u542b\u4e86\u591a\u884c\u6ce8\u91ca\u7684\u4ee3\u7801\u4e86\uff08\u56e0\u4e3a\u591a\u884c\u6ce8\u91ca\u4e0d\u652f\u6301\u5d4c\u5957\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { std :: cout << \"Joe \\n \" ; #if 0 // Don't compile anything starting here std::cout << \"Bob\\n\"; /* Some * multi-line * comment here */ std::cout << \"Steve\\n\"; #endif // until this point return 0 ; }","title":"#if 0"},{"location":"2-10-Introduction-to-the-preprocessor/#_6","text":"\u4f60\u4e5f\u8bb8\u4f1a\u5bf9\u4e0b\u9762\u8fd9\u79cd\u5199\u6cd5\u5fc3\u5b58\u987e\u8651\uff1a 1 2 3 4 #define PRINT_JOE #ifdef PRINT_JOE // ... \u65e2\u7136\u6211\u4eec\u5b9a\u4e49\u4e86 PRINT_JOE \u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u9884\u5904\u7406\u5668\u4e0d\u4f1a\u5c06 #ifdef PRINT_JOE \u4e2d\u7684 PRINT_JOE \u66ff\u6362\u6389\u5462\uff1f \u5b8f\u5b9a\u4e49\u7684\u66ff\u6362\u53ea\u9488\u5bf9\u4e0e\u666e\u901a\u4ee3\u7801\u3002\u5176\u4ed6\u9884\u5904\u7406\u5668\u7684\u6307\u4ee4\u662f\u4e0d\u4f1a\u53d7\u5230\u5f71\u54cd\u7684\uff0c\u56e0\u6b64 #ifdef PRINT_JOE \u4e2d\u7684 PRINT_JOE \u662f\u4f1a\u4fdd\u7559\u7684\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 #define FOO 9 // \u5b8f\u66ff\u6362\u4e3a\u7a7a #ifdef FOO // \u6b64\u5904\u7684 FOO \u4e0d\u4f1a\u88ab\u66ff\u6362\u56e0\u4e3a\u5b83\u5c5e\u4e8e\u9884\u5904\u7406\u5668\u6307\u4ee4 std :: cout << FOO ; // \u6b64\u5904\u7684FOO\u4f1a\u88ab\u66ff\u6362\u56e0\u4e3a\u5b83\u5c5e\u4e8e\u666e\u901a\u4ee3\u7801 #endif \u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\uff0c\u9884\u5904\u7406\u5668\u8f93\u51fa\u7684\u7ed3\u679c\u4e2d\u4e0d\u5305\u542b\u4efb\u4f55\u9884\u5904\u7406\u5668\u6307\u4ee4\u2014\u2014\u5b83\u4eec\u90fd\u4f1a\u5728\u7f16\u8bd1\u524d\u88ab\u5904\u7406\u6389\uff0c\u6bd4\u8f83\u7f16\u8bd1\u5668\u5e76\u4e0d\u77e5\u9053\u8981\u5982\u4f55\u5904\u7406\u8fd9\u4e9b\u6307\u4ee4\u3002","title":"\u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u9884\u5904\u7406\u5668\u6307\u4ee4"},{"location":"2-10-Introduction-to-the-preprocessor/#_7","text":"\u9884\u5904\u7406\u5668\u6307\u4ee4\u7684\u89e3\u6790\u4f1a\u5728\u7f16\u8bd1\u5668\u5b8c\u6210\uff08\u4ece\u4e0a\u5230\u4e0b\u9010\u6587\u4ef6\u5730\uff09\u3002 \u8003\u8651\u5982\u4e0b\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void foo () { #define MY_NAME \"Alex\" } int main () { std :: cout << \"My name is: \" << MY_NAME ; return 0 ; } \u5c3d\u7ba1\u770b\u4e0a\u53bb #define MY_NAME \u201cAlex\u201d \u50cf\u662f\u88ab\u5b9a\u4e49\u5230\u4e86 foo \u51fd\u6570\u4e2d\uff0c\u4f46\u9884\u5904\u7406\u5668\u5e76\u4e0d\u4f1a\u610f\u8bc6\u5230\u8fd9\u4e00\u70b9\uff0c\u56e0\u4e3a\u5b83\u4e0d\u7406\u89e3\u201d\u51fd\u6570\u201c\u4ee5\u53ca\u5176\u4ed6C++\u7684\u8bed\u6cd5\u6982\u5ff5\u3002\u56e0\u6b64\uff0c\u8fd9\u6bb5\u4ee3\u7801\u7684\u6548\u679c\u597d\u6211\u4eec\u5728\u51fd\u6570\u4e4b\u524d\u6216\u662f\u4e4b\u540e\u5b9a\u4e49 #define MY_NAME \u201cAlex\u201d \u662f\u4e00\u6a21\u4e00\u6837\u7684\u3002\u5904\u4e8e\u53ef\u8bfb\u6027\u7684\u8003\u8651\uff0c\u4e00\u822c #define \u4e0d\u4f1a\u88ab\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8\u3002 \u5f53\u9884\u5904\u7406\u5668\u7684\u5de5\u4f5c\u5b8c\u6210\u540e\uff0c\u6240\u6709\u8fd9\u4e9b #define \u7684\u6807\u8bc6\u7b26\u5c31\u90fd\u88ab\u4e22\u5f03\u4e86\u3002\u56e0\u6b64\u8fd9\u4e9b\u6807\u8bc6\u7b26\u53ea\u6709\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u662f\u6709\u6548\u7684\uff0c\u5b9a\u4e49\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u7684\u5b8f\u4e0d\u4f1a\u5f71\u54cd\u5230\u5176\u4ed6\u6587\u4ef6\u3002 \u8003\u8651\u8fd9\u4e2a\u4f8b\u5b50\uff1a function.cpp: 1 2 3 4 5 6 7 8 9 10 11 #include void doSomething () { #ifdef PRINT std :: cout << \"Printing!\" ; #endif #ifndef PRINT std :: cout << \"Not printing!\" ; #endif } main.cpp: 1 2 3 4 5 6 7 8 9 10 void doSomething (); // forward declaration for function doSomething() #define PRINT int main () { doSomething (); return 0 ; } \u4e0a\u9762\u4ee3\u7801\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 Not printing! \u5c3d\u7ba1 PRINT \u88ab\u5b9a\u4e49\u5728\u4e86 main.cpp \u4e2d\uff0c\u4f46\u662f\u5b83\u4e1d\u6beb\u4e0d\u4f1a\u5f71\u54cd function.cpp \uff08\u5bf9 PRINT \u7684\u5b9a\u4e49\u53ea\u5728 main.cpp \u4e2d\u6709\u6548\uff09\u3002\u6211\u4eec\u4f1a\u5728 2.12 - \u5934\u6587\u4ef6\u91cd\u590d\u5305\u542b\u4fdd\u62a4 \u4e2d\u63a2\u8ba8\u5176\u5f71\u54cd\u3002","title":"\u5b8f\u5b9a\u4e49\u7684\u4f5c\u7528\u57df"},{"location":"2-11-Header-files/","tags":["header"],"text":"Key Takeaway \u5934\u6587\u4ef6\u4e2d\u4e00\u822c\u4e0d\u5e94\u8be5\u5305\u542b\u51fd\u6570\u548c\u53d8\u91cf\u7684\u5b9a\u4e49\uff0c\u4ee5\u4fbf\u9075\u5faa\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\uff0c\u9664\u975e\u662f\u7b26\u53f7\u5e38\u91cf \u7531\u4e8e 2-12-Header-guards#\u5934\u6587\u4ef6\u9632\u8303\u4e0d\u80fd\u9632\u6b62\u5934\u6587\u4ef6\u88ab\u5305\u542b\u5230\u4e0d\u540c\u7684\u6587\u4ef6 \uff0c\u5934\u6587\u4ef6\u4e2d\u7684\u5b9a\u4e49\u53ef\u80fd\u4f1a\u5bfc\u81f4\u94fe\u63a5\u5668\u53d1\u73b0\u91cd\u5b9a\u4e49\u3002 \u6bcf\u4e2a\u5934\u6587\u4ef6\u90fd\u5e94\u8be5\u6709\u5176\u7279\u5b9a\u7684\u7528\u9014\u5e76\u4e14\u5c3d\u53ef\u80fd\u72ec\u7acb \u4f60\u7f16\u5199\u7684\u4efb\u4f55\u5934\u6587\u4ef6\u90fd\u5e94\u8be5\u80fd\u591f\u72ec\u7acb\u7f16\u8bd1\uff08\u5b83\u5e94\u5f53 #include \u5b83\u6240\u9700\u7684\u5168\u90e8\u4f9d\u8d56\uff09\uff1b \u6e90\u6587\u4ef6\u5e94\u8be5\u5305\u542b\u5176\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\uff08\u5982\u679c\u5b58\u5728\u7684\u8bdd\uff09\uff0c\u53ef\u4ee5\u786e\u4fdd\u5b9a\u4e49\u548c\u58f0\u660e\u4e0d\u5339\u914d\u7684\u95ee\u9898\u5728\u7f16\u8bd1\u65f6\u5c31\u80fd\u88ab\u53d1\u73b0 \u4f7f\u7528\u53cc\u5f15\u53f7\u6765\u5305\u542b\u4f60\u81ea\u5df1\u7f16\u5199\u7684\u5934\u6587\u4ef6\uff0c\u5219\u8be5\u6587\u4ef6\u5fc5\u987b\u4f4d\u4e8e\u5f53\u524d\u76ee\u5f55\u4e2d\u3002\u4f7f\u7528\u5c16\u62ec\u53f7\u5305\u542b\u7f16\u8bd1\u5668\u3001\u64cd\u4f5c\u7cfb\u7edf\u6216\u7b2c\u4e09\u65b9\u63d0\u4f9b\u7684\u5b89\u88c5\u5728\u7cfb\u7edf\u5176\u4ed6\u4f4d\u7f6e\u7684\u5934\u6587\u4ef6\u3002 \u5728\u4f7f\u7528\u6807\u51c6\u5e93\u5934\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u6ca1\u6709 .h \u7248\u672c\u7684\u5934\u6587\u4ef6\u3002\u5bf9\u4e8e\u7528\u6237\u81ea\u5df1\u7f16\u5199\u7684\u5934\u6587\u4ef6\uff0c\u4ecd\u7136\u9700\u8981\u4f7f\u7528 .h \u540e\u7f00\u3002 \u4e0d\u8981 #include .cpp \u6587\u4ef6\u3002 \u5bf9\u4e8e\u7f16\u8bd1\u6240\u9700\u7684\u5934\u6587\u4ef6\uff0c\u6bcf\u4e00\u4e2a\u90fd\u5e94\u8be5\u88ab\u660e\u786e\u5730\u4f7f\u7528 #include \u5305\u542b\u8fdb\u6765\u3002\u4e0d\u8981\u4f9d\u8d56\u88ab\u95f4\u63a5\u5305\u542b\u7684\u5934\u6587\u4ef6\u3002 \u4e3a\u4e86\u6700\u5927\u7a0b\u5ea6\u51cf\u5c11\u5934\u6587\u4ef6\u6ca1\u6709\u6b63\u786e\u5305\u542b\u5f15\u8d77\u7684\u7f16\u8bd1\u5668\u62a5\u9519\uff0c\u8bf7\u6309\u7167\u5982\u4e0b\u987a\u5e8f\u5305\u542b\u5934\u6587\u4ef6\uff1a \u5148\u5305\u542b\u6e90\u6587\u4ef6\u5bf9\u5e94\u7684\u5934\u6587\u4ef6 \u518d\u5305\u542b\u9879\u76ee\u6240\u9700\u7684\u5176\u4ed6\u5934\u6587\u4ef6 \u518d\u5305\u542b\u7b2c\u4e09\u65b9\u5e93\u7684\u5934\u6587\u4ef6 \u518d\u5305\u542b\u6807\u51c6\u5e93\u5934\u6587\u4ef6 \u5934\u6587\u4ef6\u53ca\u5176\u7528\u9014 \u968f\u7740\u7a0b\u5e8f\u8d8a\u6765\u8d8a\u5927\uff08\u4ee5\u53ca\u8d8a\u6765\u8d8a\u591a\u7684\u6587\u4ef6\u88ab\u4f7f\u7528\uff09\uff0c\u4e3a\u6bcf\u4e2a\u88ab\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u521b\u5efa \u524d\u5411\u58f0\u660e \u4f1a\u53d8\u5f97\u975e\u5e38\u9ebb\u70e6\u3002\u5982\u679c\u80fd\u591f\u5c06\u6240\u6709\u7684\u524d\u5411\u58f0\u660e\u90fd\u653e\u5728\u4e00\u4e2a\u6587\u4ef6\u91cc\uff0c\u7136\u540e\u5728\u9700\u8981\u4f7f\u7528\u7684\u65f6\u5019\u5c06\u5176\u5bfc\u5165\uff0c\u5c82\u4e0d\u7f8e\u54c9\uff1f C++ \u4ee3\u7801\u6587\u4ef6(\u6269\u5c55\u540d\u4e3a .cpp ) \u5e76\u4e0d\u662f C++\u9879\u76ee\u4e2d\u552f\u4e00\u5e38\u89c1\u7684\u6587\u4ef6\u7c7b\u578b\u3002\u5934\u6587\u4ef6\u901a\u5e38\u4ee5 .h \u6269\u5c55\u540d\u7ed3\u5c3e\uff0c\u4f46\u662f\u6709\u65f6\u5019\u4f60\u4e5f\u4f1a\u770b\u5230 .hpp \u6269\u5c55\u540d\u7684\u5934\u6587\u4ef6\uff0c\u751a\u81f3\u6709\u4e9b\u90fd\u6ca1\u6709\u6269\u5c55\u540d\u3002 \u8fd9\u7c7b\u5934\u6587\u4ef6\u7684\u4e3b\u8981\u4f5c\u7528\u5c31\u662f\u653e\u7f6e\u4ee3\u7801\u7684\u58f0\u660e\u3002 \u5173\u952e\u4fe1\u606f \u5934\u6587\u4ef6\u4f7f\u6211\u4eec\u53ef\u4ee5\u5c06\u6240\u6709\u7684\u524d\u5411\u58f0\u660e\u90fd\u653e\u5728\u4e00\u4e2a\u6587\u4ef6\u91cc\uff0c\u7136\u540e\u5728\u9700\u8981\u7684\u65f6\u5019\u5c06\u5176\u5bfc\u5165\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u591a\u6587\u4ef6\u7a0b\u5e8f\u4e2d\u907f\u514d\u5f88\u591a\u624b\u5de5\u52b3\u52a8\u3002 \u4f7f\u7528\u6807\u51c6\u5e93\u7684\u5934\u6587\u4ef6 \u8003\u8651\u5982\u4e0b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << \"Hello, world!\" ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u4f7f\u7528 std:: cout \u5c06 \u201cHello, world!\u201d \u6253\u5370\u5728\u63a7\u5236\u53f0\u4e0a\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u7684\u7a0b\u5e8f\u5e76\u6ca1\u6709\u5b9a\u4e49\u8fc7 std:: cout \u51fd\u6570\u5440\uff1f\u7f16\u8bd1\u5668\u5982\u4f55\u5f97\u77e5\u5b83\u7684\u5b9a\u4e49\u7684\u5462\uff1f \u5b9e\u9645\u4e0a std:: cout \u7684\u58f0\u660e\u88ab\u5b9a\u4e49\u5728 \u201ciostream\u201d \u5934\u6587\u4ef6\u4e2d\uff0c\u5f53\u6211\u4eec\u4f7f\u7528 #include \u6307\u4ee4\u7684\u65f6\u5019\uff0c\u5176\u5b9e\u662f\u5728\u544a\u8bc9\u9884\u5904\u7406\u5668\uff0c\u5c06\u8be5\u6587\u4ef6\u7684\u5185\u5bb9\uff08\u5176\u4e2d\u5c31\u5305\u62ec std:: cout \u7684\u524d\u5411\u58f0\u660e\uff09\u5168\u90e8\u62f7\u8d1d\u5230\u6b64\u5904\u3002 \u5173\u952e\u4fe1\u606f \u5f53\u6211\u4eec #include \u4e00\u4e2a\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u8be5\u6587\u4ef6\u7684\u5185\u5bb9\u4f1a\u88ab\u63d2\u5165\u5230\u6b64\u5904\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u4ece\u5176\u4ed6\u6587\u4ef6\u83b7\u53d6\u524d\u5411\u5b9a\u4e49\u3002 \u8003\u8651\u4e00\u4e0b\uff0c\u5982\u679c iostream \u5934\u6587\u4ef6\u4e0d\u5b58\u5728\u4f1a\u600e\u6837\uff1f\u90a3\u4e48\u4f60\u6bcf\u6b21\u4f7f\u7528 std:: cout \u7684\u65f6\u5019\uff0c\u90fd\u5fc5\u987b\u624b\u5de5\u5c06\u6240\u6709\u548c std:: cout \u76f8\u5173\u7684\u58f0\u660e\u90fd\u8f93\u5165\u6216\u62f7\u8d1d\u5230\u6587\u4ef6\u7684\u5f00\u5934\u90e8\u5206\u3002\u8fd9\u6837\u4e0d\u4ec5\u9ebb\u70e6\uff0c\u8fd8\u9700\u8981\u4f7f\u7528\u8005\u77e5\u9053 std:: cout \u5b9e\u73b0\u7684\u7ec6\u8282\uff0c\u8fd9\u5c06\u4f1a\u662f\u975e\u5e38\u975e\u5e38\u5927\u7684\u5de5\u4f5c\u91cf\u3002\u66f4\u7cdf\u7684\u662f\uff0c\u4e00\u65e6\u51fd\u6570\u539f\u578b\u53d1\u751f\u4e86\u6539\u53d8\uff0c\u6211\u4eec\u5fc5\u987b\u624b\u52a8\u66f4\u65b0\u5168\u90e8\u7684\u58f0\u660e\u3002\u6240\u4ee5\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u83ab\u8fc7\u4e8e\u76f4\u63a5\u4f7f\u7528 #include \uff01 \u5bf9\u4e8e\u51fd\u6570\u548c\u53d8\u91cf\u6765\u8bf4\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b83\u4eec\u7684\u5934\u6587\u4ef6\u4e2d\u901a\u5e38\u53ea\u5305\u542b\u58f0\u660e\uff0c\u800c\u4e0d\u5305\u542b\u51fd\u6570\u548c\u53d8\u91cf\u7684\u5b9a\u4e49\uff08\u5426\u5219\u53ef\u80fd\u4f1a\u8fdd\u53cd \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \uff09\u3002 std:: cout \u88ab\u524d\u5411\u5b9a\u4e49\u5728 iostream \u5934\u6587\u4ef6\u4e2d\u5e76\u4f5c\u4e3a C++\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u800c\u6807\u51c6\u5e93\u5219\u4f1a\u5728\u7a0b\u5e8f\u7f16\u8bd1\u65f6\u88ab\u81ea\u52a8\u94fe\u63a5\u3002 \u6700\u4f73\u5b9e\u8df5 \u5934\u6587\u4ef6\u4e2d\u4e00\u822c\u4e0d\u5e94\u8be5\u5305\u542b\u51fd\u6570\u548c\u53d8\u91cf\u7684\u5b9a\u4e49\uff0c\u4ee5\u4fbf\u9075\u5faa\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u3002\u4f8b\u5916\u7684\u662f \u7b26\u53f7\u5e38\u91cf (\u5728 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \u4e2d\u4f1a\u8fdb\u884c\u4ecb\u7ecd)\u3002 \u7f16\u5199\u5934\u6587\u4ef6 \u73b0\u5728\uff0c\u56de\u60f3\u4e00\u4e0b\u4e4b\u524d\u8bfe\u7a0b\u4e2d\u4f7f\u7528\u8fc7\u7684\u7a0b\u5e8f\u3002\u8be5\u7a0b\u5e8f\u5305\u542b\u4e24\u4e2a\u6587\u4ef6 add.cpp \u548c main.cpp \uff1a add.cpp 1 2 3 4 int add ( int x , int y ) { return x + y ; } main.cpp 1 2 3 4 5 6 7 8 9 #include int add ( int x , int y ); // forward declaration using function prototype int main () { std :: cout << \"The sum of 3 and 4 is \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } \uff08\u5982\u679c\u4f60\u60f3\u8981\u4e3a\u8fd9\u4e24\u4e2a\u6587\u4ef6\u91cd\u65b0\u521b\u5efa\u4e00\u4e2a\u9879\u76ee\uff0c\u4e0d\u8981\u5fd8\u8bb0\u628a add.cpp \u6587\u4ef6\u52a0\u5230\u9879\u76ee\u4e2d\uff0c\u8fd9\u6837\u4f60\u624d\u80fd\u7f16\u8bd1\u5b83\uff09\u3002 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u524d\u5411\u58f0\u660e\u4ee5\u4fbf\u8ba9\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1 main.cpp \u65f6\u80fd\u591f\u77e5\u6653 add \u7684\u5b9a\u4e49\u3002\u6b63\u5982\u4e4b\u524d\u4ecb\u7ecd\u7684\u90a3\u6837\uff0c\u5982\u679c\u4e3a\u6bcf\u4e00\u4e2a\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u90fd\u521b\u5efa\u524d\u5411\u58f0\u660e\uff0c\u5c06\u4f1a\u975e\u5e38\u9ebb\u70e6\uff1a \u5176\u5b9e\uff0c\u53ea\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u5934\u6587\u4ef6\u5c31\u53ef\u4ee5\u4e00\u52b3\u6c38\u9038\u89e3\u51b3\u4e0a\u8ff0\u70e6\u607c\u3002\u7f16\u5199\u5934\u6587\u4ef6\u6bd4\u4f60\u60f3\u8c61\u7684\u8981\u7b80\u5355\u7684\u591a\uff0c\u5b83\u53ea\u5305\u542b\u4e24\u90e8\u5206\uff1a \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e \uff0c\u7a0d\u540e\u6211\u4eec\u4f1a\u8be6\u7ec6\u4ecb\u7ecd(\u53c2\u8003\uff1a 2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e )\u3002 \u5934\u6587\u4ef6\u7684\u5b9e\u9645\u5185\u5bb9\uff0c\u5373\u6240\u6709\u6211\u4eec\u5e0c\u671b\u80fd\u591f\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u88ab\u8bbf\u95ee\u7684\u6807\u8bc6\u7b26\u7684\u524d\u5411\u58f0\u660e\u3002 \u5c06\u5934\u6587\u4ef6\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u7684\u65b9\u6cd5\u548c\u6dfb\u52a0\u6e90\u6587\u4ef6\u5dee\u4e0d\u591a\uff08\u53c2\u8003\uff1a 2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f \uff09\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f IDE\uff0c\u53ef\u4ee5\u53c2\u8003\u4e4b\u524d\u8bfe\u7a0b\u4e2d\u63d0\u5230\u7684\u6b65\u9aa4\uff0c\u53ea\u4e0d\u8fc7\u5728\u9009\u62e9 Source \u7684\u65f6\u5019\uff0c\u8981\u6539\u4e3a\u9009\u62e9 Header \u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u547d\u4ee4\u884c\u73af\u5883\uff0c\u5219\u53ea\u9700\u8981\u7528\u7f16\u8f91\u5668\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u5373\u53ef\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u521b\u5efa\u5934\u6587\u4ef6\u7684\u65f6\u5019\u8bf7\u4f7f\u7528 .h \u540e\u7f00\u3002 \u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u901a\u5e38\u662f\u6210\u5bf9\u51fa\u73b0\u7684\uff0c\u5934\u6587\u4ef6\u4f1a\u4e3a\u5bf9\u5e94\u7684\u6e90\u6587\u4ef6\u63d0\u4f9b\u524d\u5411\u58f0\u660e\u3002\u56e0\u4e3a\u5934\u6587\u4ef6\u5305\u542b\u7684\u662f add.cpp \u4e2d\u51fd\u6570\u7684\u58f0\u660e\uff0c\u56e0\u6b64\u5934\u6587\u4ef6\u547d\u540d\u4e3a add.h \u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u6210\u5bf9\u51fa\u73b0\uff08\u4f8b\u5982\uff0cadd.h \u548c add.cpp\uff09\uff0c\u5b83\u4eec\u5e94\u8be5\u662f\u540c\u540d\u4f46\u4e0d\u540c\u540e\u7f00\u7684\u6587\u4ef6\u3002 \u5b8c\u6210\u540e\u7684\u5934\u6587\u4ef6\u5982\u4e0b\uff1a add.h: add.h 1 2 3 4 // 1) \u8fd9\u91cc\u5e94\u8be5\u662f\u5934\u6587\u4ef6\u5305\u542b\u4fdd\u62a4\uff0c\u4f46\u662f\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u53ea\u6709\u4e00\u4e2a\u5934\u6587\u4ef6\uff0c\u4e3a\u4e86\u7b80\u5316\u8fd9\u91cc\u5c31\u7701\u7565\u4e86 // 2) .h \u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u58f0\u660e\u90fd\u5199\u5728\u8fd9\u91cc int add ( int x , int y ); // \u51fd\u6570\u539f\u578b\u2014\u2014\u4e0d\u8981\u5fd8\u8bb0\u5206\u53f7\uff01 \u4e3a\u4e86\u5728 main.cpp \u4e2d\u4f7f\u7528\u8be5\u5934\u6587\u4ef6\uff0c\u6211\u4eec\u9700\u8981 #include (\u8fd9\u91cc\u9700\u8981\u4f7f\u7528\u5f15\u53f7\u800c\u975e\u5c16\u62ec\u53f7)\u3002 main.cpp 1 2 3 4 5 6 7 8 #include \"add.h\" // \u5c06 add.h \u63d2\u5165\u5230\u6b64\u4f4d\u7f6e\u3002\u6ce8\u610f\u4f7f\u7528\u53cc\u5f15\u53f7\u3002 #include int main () { std :: cout << \"The sum of 3 and 4 is \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } add.cpp 1 2 3 4 5 6 #include \"add.h\" // add.h \u7684\u5185\u5bb9\u4f1a\u88ab\u62f7\u8d1d\u5230\u8fd9\u91cc\u3002\u6ce8\u610f\u4f7f\u7528\u53cc\u5f15\u53f7\u3002 int add ( int x , int y ) { return x + y ; } \u5f53\u9884\u5904\u7406\u5668\u5904\u7406\u5230 #include \"add.h\" \u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u628a add.h \u4e2d\u7684\u5185\u5bb9\u90fd\u62f7\u8d1d\u5230\u8fd9\u91cc\u3002\u56e0\u4e3a add.h \u5305\u542b\u4e86\u51fd\u6570 add \u7684\u58f0\u660e\uff0c\u6240\u4ee5\u8be5\u58f0\u660e\u5c31\u4f1a\u88ab\u62f7\u8d1d\u5230\u4e86 main.cpp \u4e2d\u3002\u6700\u7ec8\u7684\u6548\u679c\uff0c\u5c31\u548c\u4e4b\u524d\u76f4\u63a5\u5c06\u58f0\u660e\u5199\u5728 main.cpp \u9876\u90e8\u662f\u4e00\u6837\u7684\u3002 \u73b0\u5728\uff0c\u6211\u4eec\u7684\u7a0b\u5e8f\u5c31\u53ef\u4ee5\u88ab\u6b63\u786e\u7684\u7f16\u8bd1\u548c\u94fe\u63a5\u4e86\u3002 \u6e90\u6587\u4ef6\u9700\u8981\u5305\u542b\u5176\u5bf9\u5e94\u7684\u5934\u6587\u4ef6 C++ \u4e2d\u7684\u6700\u4f73\u5b9e\u8df5\u4e4b\u4e00\u5c31\u662f\u6e90\u6587\u4ef6\u5e94\u8be5\u5305\u542b\u5176\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\uff08\u5982\u679c\u5b58\u5728\u7684\u8bdd\uff09\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c add.cpp \u5e94\u8be5\u5305\u542b add.h \u3002 \u8fd9\u4e48\u505a\u53ef\u4ee5\u4f7f\u5f97\u6709\u4e9b\u95ee\u9898\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u88ab\u53d1\u73b0\uff0c\u800c\u4e0d\u662f\u7559\u5230\u94fe\u63a5\u65f6\u518d\u53d1\u73b0\u3002\u4f8b\u5982\uff1a something.h 1 int something ( int ); // \u58f0\u660e\u7684\u8fd4\u56de\u7c7b\u578b\u662f int something.cpp 1 2 3 4 5 #include \"something.h\" void something ( int ) // \u9519\u8bef: \u9519\u8bef\u7684\u8fd4\u56de\u7c7b\u578b { } \u56e0\u4e3a something.cpp #includes \u4e86 something.h \uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u53d1\u73b0 something() \u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u5339\u914d\u3002\u800c\u5982\u679c something.cpp \u6ca1\u6709 #include something.h \uff0c\u90a3\u4e48\u6211\u4eec\u5fc5\u987b\u8981\u7b49\u5230\u94fe\u63a5\u65f6\uff0c\u8be5\u95ee\u9898\u624d\u4f1a\u88ab\u94fe\u63a5\u5668\u53d1\u73b0\uff0c\u8fd9\u65e0\u7591\u4f1a\u6d6a\u8d39\u65f6\u95f4\u3002\u5176\u4ed6\u4f8b\u5b50\u53ef\u4ee5\u53c2\u8003 \u8fd9\u4e2a\u8bc4\u8bba \u3002 \u6700\u4f73\u5b9e\u8df5 \u6e90\u6587\u4ef6\u9700\u8981\u5305\u542b\u5176\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\uff08\u5982\u679c\u6709\u7684\u8bdd\uff09\u3002 \u9519\u8bef\u6392\u67e5 \u5982\u679c\u7f16\u8bd1\u5668\u62a5\u544a\u4e86 add.h \u65e0\u6cd5\u88ab\u627e\u7684\u9519\u8bef\uff0c\u8bf7\u9996\u5148\u786e\u8ba4\u6587\u4ef6\u540d\u662f\u5426\u4e3a add.h \u3002\u6587\u4ef6\u540d\u53ef\u80fd\u4f1a\u88ab\u9519\u8bef\u5730\u8bbe\u7f6e\u4e3a add (\u65e0\u540e\u7f00) \u6216 add.h.txt \u6216 add.hpp \uff0c\u8fd9\u53ef\u80fd\u53d6\u51b3\u4e8e\u4f60\u662f\u5982\u4f55\u521b\u5efa\u5b83\u4eec\u7684\u3002\u53e6\u5916\uff0c\u4e5f\u8981\u786e\u4fdd\u8be5\u5934\u6587\u4ef6\u548c\u5176\u4ed6\u6587\u4ef6\u4f4d\u4e8e\u76f8\u540c\u7684\u76ee\u5f55\u3002 \u5982\u679c\u94fe\u63a5\u5668\u62a5\u544a\u4e86 add \u51fd\u6570\u672a\u5b9a\u4e49\u7684\u9519\u8bef\uff0c\u8bf7\u786e\u4fdd add.cpp \u88ab\u6dfb\u52a0\u5230\u4e86\u9879\u76ee\u4e2d\uff0c\u8fd9\u6837 add \u51fd\u6570\u624d\u80fd\u591f\u88ab\u6b63\u786e\u94fe\u63a5\u3002 \u5c16\u62ec\u53f7 vs \u53cc\u5f15\u53f7 \u4f60\u53ef\u80fd\u4f1a\u597d\u5947\uff0c\u4e3a\u4ec0\u4e48 iostream \u4f7f\u7528\u7684\u662f\u5c16\u62ec\u53f7\uff0c\u800c add.h \u5c31\u9700\u8981\u4f7f\u7528\u53cc\u5f15\u53f7\u3002\u8fd9\u662f\u56e0\u4e3a\uff0c\u540c\u540d\u7684\u6587\u4ef6\u53ef\u80fd\u4f1a\u5206\u5e03\u5728\u4e0d\u540c\u7684\u76ee\u5f55\u4e2d\u3002\u533a\u5206\u4f7f\u7528\u5c16\u62ec\u53f7\u548c\u53cc\u5f15\u7528\uff0c\u53ef\u4ee5\u544a\u8bc9\u7f16\u8bd1\u5668\u5230\u54ea\u91cc\u5bfb\u627e\u5934\u6587\u4ef6\u3002 \u5f53\u4f7f\u7528\u5c16\u62ec\u53f7\u7684\u65f6\u5019\uff0c\u5176\u5b9e\u662f\u5728\u544a\u8bc9\u9884\u5904\u7406\u5668\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\u5e76\u4e0d\u662f\u6211\u4eec\u7f16\u5199\u7684\u3002\u7f16\u8bd1\u5668\u53ea\u4f1a\u5728 include directories \u6307\u5b9a\u7684\u76ee\u5f55\u4e2d\u641c\u7d22\u3002 include directories \u662f\u9879\u76ee\u3001IDE\u6216\u7f16\u8bd1\u5668\u914d\u7f6e\u7684\u4e00\u90e8\u5206\uff0c\u9ed8\u8ba4\u7684\u8def\u5f84\u662f\u7531\u7f16\u8bd1\u5668\u6216\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684\u3002\u7f16\u8bd1\u5668\u5e76\u4e0d\u4f1a\u5728\u4f60\u7684\u9879\u76ee\u76ee\u5f55\u4e2d\u641c\u7d22\u5bf9\u996e\u7684\u5934\u6587\u4ef6\u3002 \u5f53\u4f7f\u7528\u53cc\u5f15\u53f7\u7684\u65f6\u5019\uff0c\u5176\u5b9e\u662f\u5728\u544a\u8bc9\u9884\u5904\u7406\u5668\u5934\u6587\u4ef6\u662f\u6211\u4eec\u81ea\u5df1\u7f16\u5199\u7684\u3002\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u641c\u7d22\u5f53\u524d\u76ee\u5f55\uff0c\u5982\u679c\u627e\u4e0d\u5230\u6240\u9700\u7684\u5934\u6587\u4ef6\uff0c\u5219\u4f1a\u5728 include directories \u4e2d\u8fdb\u884c\u67e5\u627e\u3002 \u6cd5\u5219 \u4f7f\u7528\u53cc\u5f15\u53f7\u6765\u5305\u542b\u4f60\u81ea\u5df1\u7f16\u5199\u7684\u5934\u6587\u4ef6\uff0c\u5219\u8be5\u6587\u4ef6\u5fc5\u987b\u4f4d\u4e8e\u5f53\u524d\u76ee\u5f55\u4e2d\u3002\u4f7f\u7528\u5c16\u62ec\u53f7\u5305\u542b\u7f16\u8bd1\u5668\u3001\u64cd\u4f5c\u7cfb\u7edf\u6216\u7b2c\u4e09\u65b9\u63d0\u4f9b\u7684\u5b89\u88c5\u5728\u7cfb\u7edf\u5176\u4ed6\u4f4d\u7f6e\u7684\u5934\u6587\u4ef6\u3002 \u4e3a\u4ec0\u4e48 iostream \u6ca1\u6709 .h \u540e\u7f00? \u53e6\u5916\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u662f\uff1a\u201d\u4e3a\u4ec0\u4e48 iostream \uff08\u6216\u8005\u5176\u4ed6\u6807\u51c6\u5e93\u5934\u6587\u4ef6\uff09\u6ca1\u6709 .h \u540e\u7f00\u5462\uff1f\u201c\u3002\u8fd9\u662f\u56e0\u4e3a iostream.h \u548c iostream \u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u5934\u6587\u4ef6\u3002\u8fd9\u6d89\u53ca\u5230\u4e00\u4e9b\u5386\u53f2\u77e5\u8bc6\u3002 \u5728 C++ \u521a\u88ab\u521b\u5efa\u51fa\u6765\u7684\u65f6\u5019\uff0c\u6240\u6709\u7684\u6807\u51c6\u8fd0\u884c\u65f6\u5e93\u90fd\u662f\u4ee5 .h \u7ed3\u5c3e\u7684\u3002\u6700\u521d\u7248\u672c\u7684 cout \u548c cin \u4e5f\u90fd\u88ab\u5b9a\u4e49\u5728 iostream.h \u4e2d\u3002\u5728ANSI\u59d4\u5458\u4f1a\u5bf9\u8bed\u8a00\u8fdb\u884c\u6807\u51c6\u5316\u7684\u65f6\u5019\uff0c\u5b83\u4eec\u51b3\u5b9a\u8981\u5c06\u6807\u51c6\u5e93\u4e2d\u7684\u51fd\u6570\u90fd\u79fb\u5230 std \u547d\u540d\u7a7a\u95f4\u4e2d\u4ee5\u907f\u514d\u5176\u548c\u7528\u6237\u5b9a\u4e49\u7684\u51fd\u6570\u4ea7\u751f\u547d\u540d\u51b2\u7a81\u3002\u4e0d\u8fc7\uff0c\u73b0\u5b9e\u95ee\u9898\u662f\uff0c\u5982\u679c\u6b64\u65f6\u5c06\u8fd9\u4e9b\u51fd\u6570\u90fd\u79fb\u52a8\u5230 std \u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u90a3\u4e48\u4e4b\u524d\u5199\u7684\u4ee3\u7801\u5c31\u90fd\u4e0d\u80fd\u5de5\u4f5c\u4e86\uff01 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ea\u80fd\u4f7f\u7528\u4e00\u7ec4\u65b0\u7684\u5934\u6587\u4ef6\uff0c\u5b83\u4eec\u5177\u6709\u76f8\u540c\u7684\u540d\u5b57\uff0c\u4f46\u6ca1\u6709 .h \u540e\u7f00\u3002\u8fd9\u4e9b\u65b0\u7684\u5934\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\uff0c\u90fd\u5728 std \u4f5c\u7528\u57df\u4e2d\u3002\u8fd9\u6837\uff0c\u4f7f\u7528 #include \u7684\u8001\u4ee3\u7801\u5c31\u4e0d\u9700\u8981\u8fdb\u884c\u4efb\u4f55\u4fee\u6539\u4e86\uff0c\u800c\u65b0\u7f16\u5199\u7684\u4ee3\u7801\u5219\u9700\u8981\u4f7f\u7528 #include \u3002 \u6b64\u5916\uff0c\u4ece C \u8bed\u8a00\u7ee7\u627f\u8fc7\u6765\u7684\u6807\u51c6\u5e93\u5219\u88ab\u6dfb\u52a0\u4e86\u4e00\u4e2a c \u4f5c\u4e3a\u524d\u7f00(\u4f8b\u5982 stdlib.h \u53d8\u6210\u4e86 cstdlib )\u3002\u8fd9\u4e9b\u5e93\u4e2d\u7684\u51fd\u6570\u4e5f\u540c\u6837\u88ab\u79fb\u52a8\u5230\u4e86 std \u6807\u51c6\u5e93\u4e2d\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u4f7f\u7528\u6807\u51c6\u5e93\u5934\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u6ca1\u6709 .h \u7248\u672c\u7684\u5934\u6587\u4ef6\u3002\u5bf9\u4e8e\u7528\u6237\u81ea\u5df1\u7f16\u5199\u7684\u5934\u6587\u4ef6\uff0c\u4ecd\u7136\u9700\u8981\u4f7f\u7528 .h \u540e\u7f00\u3002 include \u5176\u4ed6\u76ee\u5f55\u4e2d\u7684\u5934\u6587\u4ef6 \u53e6\u5916\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\u662f\u5982\u4f55\u5305\u542b\u5176\u4ed6\u76ee\u5f55\u4e2d\u7684\u5934\u6587\u4ef6\u3002 \u4e00\u79cd\u53ef\u884c\uff08\u4f46\u4e0d\u597d\u7684\uff09\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u6765\u6307\u5b9a #include \u7684\u5185\u5bb9\uff0c\u4f8b\u5982\uff1a 1 2 #include \"headers/myHeader.h\" #include \"../moreHeaders/myOtherHeader.h\" \u8fd9\u4e48\u505a\u7a0b\u5e8f\u662f\u53ef\u4ee5\u7f16\u8bd1\u7684\uff08\u5047\u8bbe\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\u786e\u5b9e\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e0b\uff09\uff0c\u4f46\u662f\u5b83\u7684\u7f3a\u70b9\u4e5f\u5f88\u660e\u663e\uff0c\u4f60\u5fc5\u987b\u5728\u4ee3\u7801\u4e2d\u63cf\u8ff0\u5b9e\u9645\u7684\u76ee\u5f55\u7ed3\u6784\u3002\u5982\u679c\u76ee\u5f55\u7ed3\u6784\u66f4\u65b0\u4e86\uff0c\u90a3\u4ee3\u7801\u4e5f\u5c31\u4e0d\u80fd\u5de5\u4f5c\u4e86\u3002 \u66f4\u597d\u7684\u529e\u6cd5\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\u6216\u8005 IDE\u6709\u4e9b\u5934\u6587\u4ef6\u5b58\u653e\u5728\u5176\u4ed6\u4f4d\u7f6e\uff0c\u6b64\u65f6\u5f53\u5b83\u4eec\u65e0\u6cd5\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u88ab\u627e\u5230\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u5230\u6307\u5b9a\u7684\u76ee\u5f55\u4e2d\u67e5\u627e\u3002\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u5728 IDE \u4e2d\u8bbe\u7f6e\u9879\u76ee\u7684 include path \u6216 search directory \u6765\u5b9e\u73b0\u3002 For Visual Studio users \u53f3\u952e\u5355\u51fb\u9879\u76ee\u7684 Solution Explorer \u7136\u540e\u9009\u62e9 Properties \u7136\u540e\u9009\u62e9 VC++ Directories \u9009\u9879\u5361\u3002\u5728\u8fd9\u91cc\u4f60\u53ef\u4ee5\u770b\u5230 Include Directories \u5c06\u4f60\u5e0c\u671b\u7f16\u8bd1\u5668\u641c\u7d22\u7684\u5305\u542b\u4e86\u5934\u6587\u4ef6\u7684\u76ee\u5f55\u586b\u5199\u5230\u8fd9\u91cc\u5373\u53ef\u3002 For Code:: Blocks users \u5728 Code:: Blocks \u4e2d\uff0c\u9009\u62e9 Project \u83dc\u5355\u5e76\u9009\u62e9 Build Options \uff0c\u518d\u9009\u62e9 Search directories \u9009\u9879\u5361\uff0c\u5c06\u4f60\u5e0c\u671b\u7f16\u8bd1\u5668\u641c\u7d22\u7684\u5305\u542b\u4e86\u5934\u6587\u4ef6\u7684\u76ee\u5f55\u586b\u5199\u5230\u8fd9\u91cc\u5373\u53ef\u3002 For GCC/G++ users \u4f7f\u7528 g++ \u662f\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7 -I \u9009\u9879\u6765\u6307\u660e\u5934\u6587\u4ef6\u641c\u7d22\u8def\u5f84\uff1a 1 g ++ - o main - I / source / includes main . cpp \u8fd9\u4e48\u505a\u7684\u597d\u5904\u662f\uff0c\u5982\u679c\u4f60\u6539\u53d8\u4e86\u76ee\u5f55\u7ed3\u6784\uff0c\u90a3\u4e48\u53ea\u9700\u8981\u5728\u8bbe\u7f6e\u91cc\u9762\u4fee\u6539\u8def\u5f84\u5373\u53ef\uff0c\u800c\u4e0d\u5fc5\u5bf9\u4ee3\u7801\u4e2d\u6bcf\u4e00\u5904\u4f7f\u7528\u8be5\u5934\u6587\u4ef6\u7684\u5730\u65b9\u8fdb\u884c\u4fee\u6539\u3002 \u5934\u6587\u4ef6\u4e2d\u53ef\u4ee5\u5305\u542b\u5176\u4ed6\u5934\u6587\u4ef6 \u5934\u6587\u4ef6\u9700\u8981\u4f9d\u8d56\u5176\u4ed6\u5934\u6587\u4ef6\u4e2d\u7684\u5b9a\u4e49\u6216\u58f0\u660e\u4e5f\u662f\u5f88\u5e38\u89c1\u7684\u3002\u56e0\u6b64\uff0c\u5934\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528 #include \u6765\u5305\u542b\u5176\u4ed6\u5934\u6587\u4ef6\u3002 \u5f53\u4f60\u7684\u4ee3\u7801 #includes \u7b2c\u4e00\u4e2a\u5934\u6587\u4ef6\u65f6\uff0c\u4f60\u5176\u5b9e\u4e5f\u5305\u542b\u4e86\u8be5\u5934\u6587\u4ef6\u4e2d\u5305\u542b\u7684\u5176\u4ed6\u5934\u6587\u4ef6\uff08\u4ee5\u53ca\u8fd9\u6240\u6709\u5934\u6587\u4ef6\u4e2d\u5305\u542b\u7684\u5934\u6587\u4ef6\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff09\u3002\u8fd9\u4e9b\u989d\u5916\u7684\u5934\u6587\u4ef6\u6709\u65f6\u5019\u79f0\u4e3a \u95f4\u63a5\u5305\u542b(transitive includes) \uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u88ab\u9690\u5f0f\u5305\u542b\u8fdb\u6765\u7684\uff0c\u6211\u4eec\u660e\u6ca1\u6709\u6307\u660e\u54ea\u4e9b\u9700\u8981\u88ab\u5305\u542b\u3002 \u8fd9\u4e9b\u5934\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u5728\u4f60\u7684\u4ee3\u7801\u4e2d\u662f\u53ef\u7528\u7684\u3002\u4e0d\u8fc7\u4f60\u5e76\u4e0d\u5e94\u8be5\u4f9d\u8d56\u5b83\u4eec\uff0c\u6bd5\u7adf\u5b83\u4eec\u662f\u88ab\u95f4\u63a5\u5305\u542b\u8fdb\u6765\u7684\u3002\u8fd9\u4e9b\u5934\u6587\u4ef6\u53ef\u80fd\u4f1a\u53d8\u5316\uff0c\u4e5f\u53ef\u80fd\u5728\u4e0d\u540c\u7684\u7cfb\u7edf\u4e2d\u5b58\u5728\u5dee\u5f02\uff0c\u8fd9\u65f6\u5019\u4f60\u7684\u4ee3\u7801\u53ef\u80fd\u53ea\u80fd\u5728\u67d0\u5e73\u53f0\u4e0b\u624d\u80fd\u7f16\u8bd1\uff0c\u6216\u8005\u53ef\u80fd\u5728\u5c06\u6765\u65e0\u6cd5\u7f16\u8bd1\u3002\u907f\u514d\u4e0a\u8ff0\u95ee\u9898\u7684\u65b9\u6cd5\u4e5f\u5f88\u7b80\u5355\uff0c\u8bf7\u660e\u786e\u5305\u542b\u4f60\u6240\u9700\u8981\u7684\u5168\u90e8\u5934\u6587\u4ef6\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u4e8e\u7f16\u8bd1\u6240\u9700\u7684\u5934\u6587\u4ef6\uff0c\u6bcf\u4e00\u4e2a\u90fd\u5e94\u8be5\u88ab\u660e\u786e\u5730\u4f7f\u7528 #include \u5305\u542b\u8fdb\u6765\u3002\u4e0d\u8981\u4f9d\u8d56\u88ab\u95f4\u63a5\u5305\u542b\u7684\u5934\u6587\u4ef6\u3002 \u4e0d\u5e78\u7684\u662f\uff0c\u6211\u4eec\u5f88\u96be\u53bb\u7504\u522b\u7a76\u7adf\u54ea\u4e9b\u4ee3\u7801\u6b63\u5728\u4f9d\u8d56\u90a3\u4e9b\u88ab\u95f4\u63a5\u5f15\u5165\u7684\u5934\u6587\u4ef6\u3002 Q: \u6211\u6ca1\u6709 include \uff0c\u4f46\u662f\u7a0b\u5e8f\u4ecd\u7136\u80fd\u6b63\u5e38\u5de5\u4f5c\uff01\u4e3a\u4ec0\u4e48\uff1f \u8fd9\u4e2a\u95ee\u9898\u4e5f\u662f\u5e38\u5907\u95ee\u5230\u7684\u95ee\u9898\u4e4b\u4e00\u3002\u8fd9\u53ef\u80fd\u662f\u56e0\u4e3a\u4f60\u5305\u542b\u4e86\u67d0\u4e2a\u5934\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u6070\u5de7\u8be5\u6587\u4ef6\u4e5f\u5305\u542b\u4e86\u4f60\u6240\u9700\u7684\u90a3\u4e2a\u5934\u6587\u4ef6\uff0c\u6240\u4ee5\u4ee3\u7801\u53ef\u4ee5\u5de5\u4f5c\u3002\u5c3d\u7ba1\u4f60\u7684\u7a0b\u5e8f\u53ef\u4ee5\u7f16\u8bd1\uff0c\u6700\u4f73\u5b9e\u8df5\u544a\u8bc9\u6211\u4eec\uff0c\u8fd9\u79cd\u65b9\u5f0f\u5e76\u4e0d\u53ef\u9760\u3002\u5728\u4f60\u7684\u7535\u8111\u4e0a\u80fd\u7f16\u8bd1\uff0c\u4e0d\u4ee3\u8868\u5728\u5176\u4ed6\u7535\u8111\u4e0a\u4e5f\u80fd\u7f16\u8bd1\u3002 \u5934\u6587\u4ef6 #include \u7684\u987a\u5e8f \u5982\u679c\u4f60\u7684\u5934\u6587\u4ef6\u5185\u5bb9\u6ca1\u95ee\u9898\uff0c\u5e76\u4e14\u4e5f #include \u4e86\u5b83\u4eec\u6240\u4f9d\u8d56\u7684\u5176\u4ed6\u5934\u6587\u4ef6\uff0c\u90a3\u4e48\u5934\u6587\u4ef6\u5305\u542b\u7684\u987a\u5e8f\u5176\u5b9e\u5e76\u4e0d\u91cd\u8981\u3002 \u4e0d\u8fc7\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u5982\u4e0b\u573a\u666f\uff1a\u5934\u6587\u4ef6 A \u9700\u8981\u5934\u6587\u4ef6 B\u4e2d\u7684\u58f0\u660e\uff0c\u4f46\u662f\u5374\u5fd8\u8bb0\u5305\u542b B \u4e86\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u5728\u6e90\u6587\u4ef6\u4e2d\uff0c\u5148\u5305\u542b B \u518d\u5305\u542bA\uff0c\u90a3\u4e48\u7a0b\u5e8f\u662f\u53ef\u4ee5\u7f16\u8bd1\u7684\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u4f1a\u9996\u5148\u7f16\u8bd1\u5230B\u4e2d\u7684\u58f0\u660e\uff0c\u7136\u540e\u624d\u4f1a\u7f16\u8bd1\u5230A\u4e2d\u5bf9B\u6709\u4f9d\u8d56\u7684\u90a3\u4e9b\u4ee3\u7801\u3002 \u53cd\u4e4b\uff0c\u5982\u679c\u6211\u4eec\u5148\u5305\u542b\u5934\u6587\u4ef6 A\uff0c\u518d\u5305\u542b\u5934\u6587\u4ef6 B\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c31\u4f1a\u62a5\u9519\u4e86\uff0c\u56e0\u4e3a\u5b83\u4e0d\u80fd\u5728\u7f16\u8bd1 A \u65f6\u627e\u5230\u5176\u6240\u4f9d\u8d56\u7684 B \u4e2d\u7684\u58f0\u660e\u3002\u5176\u5b9e\u8fd9\u6b63\u662f\u6211\u4eec\u5e0c\u671b\u7684\u7ed3\u679c\uff0c\u56e0\u4e3a\u5b83\u628a\u95ee\u9898\u66b4\u9732\u4e86\u51fa\u6765\uff0c\u6211\u4eec\u4fbf\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u4fee\u590d\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e3a\u4e86\u6700\u5927\u7a0b\u5ea6\u51cf\u5c11\u5934\u6587\u4ef6\u6ca1\u6709\u6b63\u786e\u5305\u542b\u5f15\u8d77\u7684\u7f16\u8bd1\u5668\u62a5\u9519\uff0c\u8bf7\u6309\u7167\u5982\u4e0b\u987a\u5e8f\u5305\u542b\u5934\u6587\u4ef6\uff1a \u5148\u5305\u542b\u6e90\u6587\u4ef6\u5bf9\u5e94\u7684\u5934\u6587\u4ef6 \u518d\u5305\u542b\u9879\u76ee\u6240\u9700\u7684\u5176\u4ed6\u5934\u6587\u4ef6 \u518d\u5305\u542b\u7b2c\u4e09\u65b9\u5e93\u7684\u5934\u6587\u4ef6 \u518d\u5305\u542b\u6807\u51c6\u5e93\u5934\u6587\u4ef6 \u6bcf\u4e00\u7ec4\u5934\u6587\u4ef6\u90fd\u5e94\u8be5\u6309\u7167\u5b57\u6bcd\u8868\u987a\u5e8f\u6392\u5e8f \u8fd9\u6837\uff0c\u4efb\u4f55\u4e00\u4e2a\u4f60\u5b9a\u4e49\u7684\u5934\u6587\u4ef6\u5982\u679c\u6ca1\u6709 #include \u5176\u6240\u9700\u7684\u7b2c\u4e09\u65b9\u5e93\u6216\u6807\u51c6\u5e93\uff0c\u90a3\u4e48\u5f88\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u8fdb\u884c\u4fee\u590d\u4e86\u3002 \u5934\u6587\u4ef6\u6700\u4f73\u5b9e\u8df5 \u5173\u4e8e\u5982\u4f55\u4f7f\u7528\u548c\u521b\u5efa\u5934\u6587\u4ef6\uff0c\u6211\u4eec\u6709\u5982\u4e0b\u5efa\u8bae\uff1a \u59cb\u7ec8\u4f7f\u7528 \u5934\u6587\u4ef6\u91cd\u590d\u5305\u542b\u4fdd\u62a4 \uff1b \u4e0d\u8981\u5728\u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\u53d8\u91cf\u548c\u51fd\u6570 (\u5168\u5c40\u5e38\u91cf\u662f\u4e2a\u4f8b\u5916\u2014\u2014\u7a0d\u540e\u4ecb\u7ecd\uff09\uff1b \u5934\u6587\u4ef6\u548c\u5176\u5bf9\u5e94\u7684\u6e90\u6587\u4ef6\u5e94\u8be5\u5177\u6709\u76f8\u540c\u7684\u6587\u4ef6\u540d\uff08\u4f8b\u5982\uff0c grades.h \u548c grades.cpp \uff0c\u6ce8\u610f\u6269\u5c55\u540d\u662f\u4e0d\u540c\u7684\uff09\uff1b \u6bcf\u4e2a\u5934\u6587\u4ef6\u90fd\u5e94\u8be5\u6709\u5176\u7279\u5b9a\u7684\u7528\u9014\u5e76\u4e14\u5c3d\u53ef\u80fd\u72ec\u7acb\u3002\u4f8b\u5982\uff0c\u4e0eA\u76f8\u5173\u7684\u58f0\u660e\u5e94\u8be5\u653e\u5728 A.h \u4e2d\uff0c\u4e0eB\u76f8\u5173\u7684\u58f0\u660e\u5e94\u8be5\u653e\u5728 B.h \u4e2d\u3002\u8fd9\u6837\u5982\u679c\u4ee5\u540e\u4ec5\u4ec5\u9700\u8981\u4f7f\u7528A\u76f8\u5173\u7684\u529f\u80fd\uff0c\u5219\u65e0\u9700\u5305\u542b B.h \uff0c\u4e5f\u5c31\u4e0d\u4f1a\u6d89\u53ca\u5230\u4efb\u4f55\u4e0eB\u76f8\u5173\u7684\u51fd\u6570\u58f0\u660e\uff1b \u5bf9\u4e8e\u54ea\u4e9b\u51fd\u6570\u5e94\u8be5\u5305\u542b\u54ea\u4e9b\u5934\u6587\u4ef6\u8981\u505a\u5230\u5fc3\u4e2d\u6709\u6570\uff1b \u4f60\u7f16\u5199\u7684\u4efb\u4f55\u5934\u6587\u4ef6\u90fd\u5e94\u8be5\u80fd\u591f\u72ec\u7acb\u7f16\u8bd1\uff08\u5b83\u5e94\u5f53 #include \u5b83\u6240\u9700\u7684\u5168\u90e8\u4f9d\u8d56\uff09\uff1b \u53ea #include \u5fc5\u8981\u7684\u5934\u6587\u4ef6\uff08\u4e0d\u8981\u628a\u80fd\u5305\u542b\u7684\u5168\u90e8\u5305\u542b\u8fdb\u6765\uff09\uff1b \u4e0d\u8981 #include .cpp \u6587\u4ef6\u3002","title":"2.11 - \u5934\u6587\u4ef6"},{"location":"2-11-Header-files/#_1","text":"\u968f\u7740\u7a0b\u5e8f\u8d8a\u6765\u8d8a\u5927\uff08\u4ee5\u53ca\u8d8a\u6765\u8d8a\u591a\u7684\u6587\u4ef6\u88ab\u4f7f\u7528\uff09\uff0c\u4e3a\u6bcf\u4e2a\u88ab\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u521b\u5efa \u524d\u5411\u58f0\u660e \u4f1a\u53d8\u5f97\u975e\u5e38\u9ebb\u70e6\u3002\u5982\u679c\u80fd\u591f\u5c06\u6240\u6709\u7684\u524d\u5411\u58f0\u660e\u90fd\u653e\u5728\u4e00\u4e2a\u6587\u4ef6\u91cc\uff0c\u7136\u540e\u5728\u9700\u8981\u4f7f\u7528\u7684\u65f6\u5019\u5c06\u5176\u5bfc\u5165\uff0c\u5c82\u4e0d\u7f8e\u54c9\uff1f C++ \u4ee3\u7801\u6587\u4ef6(\u6269\u5c55\u540d\u4e3a .cpp ) \u5e76\u4e0d\u662f C++\u9879\u76ee\u4e2d\u552f\u4e00\u5e38\u89c1\u7684\u6587\u4ef6\u7c7b\u578b\u3002\u5934\u6587\u4ef6\u901a\u5e38\u4ee5 .h \u6269\u5c55\u540d\u7ed3\u5c3e\uff0c\u4f46\u662f\u6709\u65f6\u5019\u4f60\u4e5f\u4f1a\u770b\u5230 .hpp \u6269\u5c55\u540d\u7684\u5934\u6587\u4ef6\uff0c\u751a\u81f3\u6709\u4e9b\u90fd\u6ca1\u6709\u6269\u5c55\u540d\u3002 \u8fd9\u7c7b\u5934\u6587\u4ef6\u7684\u4e3b\u8981\u4f5c\u7528\u5c31\u662f\u653e\u7f6e\u4ee3\u7801\u7684\u58f0\u660e\u3002 \u5173\u952e\u4fe1\u606f \u5934\u6587\u4ef6\u4f7f\u6211\u4eec\u53ef\u4ee5\u5c06\u6240\u6709\u7684\u524d\u5411\u58f0\u660e\u90fd\u653e\u5728\u4e00\u4e2a\u6587\u4ef6\u91cc\uff0c\u7136\u540e\u5728\u9700\u8981\u7684\u65f6\u5019\u5c06\u5176\u5bfc\u5165\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u591a\u6587\u4ef6\u7a0b\u5e8f\u4e2d\u907f\u514d\u5f88\u591a\u624b\u5de5\u52b3\u52a8\u3002","title":"\u5934\u6587\u4ef6\u53ca\u5176\u7528\u9014"},{"location":"2-11-Header-files/#_2","text":"\u8003\u8651\u5982\u4e0b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << \"Hello, world!\" ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u4f7f\u7528 std:: cout \u5c06 \u201cHello, world!\u201d \u6253\u5370\u5728\u63a7\u5236\u53f0\u4e0a\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u7684\u7a0b\u5e8f\u5e76\u6ca1\u6709\u5b9a\u4e49\u8fc7 std:: cout \u51fd\u6570\u5440\uff1f\u7f16\u8bd1\u5668\u5982\u4f55\u5f97\u77e5\u5b83\u7684\u5b9a\u4e49\u7684\u5462\uff1f \u5b9e\u9645\u4e0a std:: cout \u7684\u58f0\u660e\u88ab\u5b9a\u4e49\u5728 \u201ciostream\u201d \u5934\u6587\u4ef6\u4e2d\uff0c\u5f53\u6211\u4eec\u4f7f\u7528 #include \u6307\u4ee4\u7684\u65f6\u5019\uff0c\u5176\u5b9e\u662f\u5728\u544a\u8bc9\u9884\u5904\u7406\u5668\uff0c\u5c06\u8be5\u6587\u4ef6\u7684\u5185\u5bb9\uff08\u5176\u4e2d\u5c31\u5305\u62ec std:: cout \u7684\u524d\u5411\u58f0\u660e\uff09\u5168\u90e8\u62f7\u8d1d\u5230\u6b64\u5904\u3002 \u5173\u952e\u4fe1\u606f \u5f53\u6211\u4eec #include \u4e00\u4e2a\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u8be5\u6587\u4ef6\u7684\u5185\u5bb9\u4f1a\u88ab\u63d2\u5165\u5230\u6b64\u5904\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u4ece\u5176\u4ed6\u6587\u4ef6\u83b7\u53d6\u524d\u5411\u5b9a\u4e49\u3002 \u8003\u8651\u4e00\u4e0b\uff0c\u5982\u679c iostream \u5934\u6587\u4ef6\u4e0d\u5b58\u5728\u4f1a\u600e\u6837\uff1f\u90a3\u4e48\u4f60\u6bcf\u6b21\u4f7f\u7528 std:: cout \u7684\u65f6\u5019\uff0c\u90fd\u5fc5\u987b\u624b\u5de5\u5c06\u6240\u6709\u548c std:: cout \u76f8\u5173\u7684\u58f0\u660e\u90fd\u8f93\u5165\u6216\u62f7\u8d1d\u5230\u6587\u4ef6\u7684\u5f00\u5934\u90e8\u5206\u3002\u8fd9\u6837\u4e0d\u4ec5\u9ebb\u70e6\uff0c\u8fd8\u9700\u8981\u4f7f\u7528\u8005\u77e5\u9053 std:: cout \u5b9e\u73b0\u7684\u7ec6\u8282\uff0c\u8fd9\u5c06\u4f1a\u662f\u975e\u5e38\u975e\u5e38\u5927\u7684\u5de5\u4f5c\u91cf\u3002\u66f4\u7cdf\u7684\u662f\uff0c\u4e00\u65e6\u51fd\u6570\u539f\u578b\u53d1\u751f\u4e86\u6539\u53d8\uff0c\u6211\u4eec\u5fc5\u987b\u624b\u52a8\u66f4\u65b0\u5168\u90e8\u7684\u58f0\u660e\u3002\u6240\u4ee5\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u83ab\u8fc7\u4e8e\u76f4\u63a5\u4f7f\u7528 #include \uff01 \u5bf9\u4e8e\u51fd\u6570\u548c\u53d8\u91cf\u6765\u8bf4\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b83\u4eec\u7684\u5934\u6587\u4ef6\u4e2d\u901a\u5e38\u53ea\u5305\u542b\u58f0\u660e\uff0c\u800c\u4e0d\u5305\u542b\u51fd\u6570\u548c\u53d8\u91cf\u7684\u5b9a\u4e49\uff08\u5426\u5219\u53ef\u80fd\u4f1a\u8fdd\u53cd \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \uff09\u3002 std:: cout \u88ab\u524d\u5411\u5b9a\u4e49\u5728 iostream \u5934\u6587\u4ef6\u4e2d\u5e76\u4f5c\u4e3a C++\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u800c\u6807\u51c6\u5e93\u5219\u4f1a\u5728\u7a0b\u5e8f\u7f16\u8bd1\u65f6\u88ab\u81ea\u52a8\u94fe\u63a5\u3002 \u6700\u4f73\u5b9e\u8df5 \u5934\u6587\u4ef6\u4e2d\u4e00\u822c\u4e0d\u5e94\u8be5\u5305\u542b\u51fd\u6570\u548c\u53d8\u91cf\u7684\u5b9a\u4e49\uff0c\u4ee5\u4fbf\u9075\u5faa\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u3002\u4f8b\u5916\u7684\u662f \u7b26\u53f7\u5e38\u91cf (\u5728 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \u4e2d\u4f1a\u8fdb\u884c\u4ecb\u7ecd)\u3002","title":"\u4f7f\u7528\u6807\u51c6\u5e93\u7684\u5934\u6587\u4ef6"},{"location":"2-11-Header-files/#_3","text":"\u73b0\u5728\uff0c\u56de\u60f3\u4e00\u4e0b\u4e4b\u524d\u8bfe\u7a0b\u4e2d\u4f7f\u7528\u8fc7\u7684\u7a0b\u5e8f\u3002\u8be5\u7a0b\u5e8f\u5305\u542b\u4e24\u4e2a\u6587\u4ef6 add.cpp \u548c main.cpp \uff1a add.cpp 1 2 3 4 int add ( int x , int y ) { return x + y ; } main.cpp 1 2 3 4 5 6 7 8 9 #include int add ( int x , int y ); // forward declaration using function prototype int main () { std :: cout << \"The sum of 3 and 4 is \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } \uff08\u5982\u679c\u4f60\u60f3\u8981\u4e3a\u8fd9\u4e24\u4e2a\u6587\u4ef6\u91cd\u65b0\u521b\u5efa\u4e00\u4e2a\u9879\u76ee\uff0c\u4e0d\u8981\u5fd8\u8bb0\u628a add.cpp \u6587\u4ef6\u52a0\u5230\u9879\u76ee\u4e2d\uff0c\u8fd9\u6837\u4f60\u624d\u80fd\u7f16\u8bd1\u5b83\uff09\u3002 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u524d\u5411\u58f0\u660e\u4ee5\u4fbf\u8ba9\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1 main.cpp \u65f6\u80fd\u591f\u77e5\u6653 add \u7684\u5b9a\u4e49\u3002\u6b63\u5982\u4e4b\u524d\u4ecb\u7ecd\u7684\u90a3\u6837\uff0c\u5982\u679c\u4e3a\u6bcf\u4e00\u4e2a\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u90fd\u521b\u5efa\u524d\u5411\u58f0\u660e\uff0c\u5c06\u4f1a\u975e\u5e38\u9ebb\u70e6\uff1a \u5176\u5b9e\uff0c\u53ea\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u5934\u6587\u4ef6\u5c31\u53ef\u4ee5\u4e00\u52b3\u6c38\u9038\u89e3\u51b3\u4e0a\u8ff0\u70e6\u607c\u3002\u7f16\u5199\u5934\u6587\u4ef6\u6bd4\u4f60\u60f3\u8c61\u7684\u8981\u7b80\u5355\u7684\u591a\uff0c\u5b83\u53ea\u5305\u542b\u4e24\u90e8\u5206\uff1a \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e \uff0c\u7a0d\u540e\u6211\u4eec\u4f1a\u8be6\u7ec6\u4ecb\u7ecd(\u53c2\u8003\uff1a 2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e )\u3002 \u5934\u6587\u4ef6\u7684\u5b9e\u9645\u5185\u5bb9\uff0c\u5373\u6240\u6709\u6211\u4eec\u5e0c\u671b\u80fd\u591f\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u88ab\u8bbf\u95ee\u7684\u6807\u8bc6\u7b26\u7684\u524d\u5411\u58f0\u660e\u3002 \u5c06\u5934\u6587\u4ef6\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u7684\u65b9\u6cd5\u548c\u6dfb\u52a0\u6e90\u6587\u4ef6\u5dee\u4e0d\u591a\uff08\u53c2\u8003\uff1a 2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f \uff09\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f IDE\uff0c\u53ef\u4ee5\u53c2\u8003\u4e4b\u524d\u8bfe\u7a0b\u4e2d\u63d0\u5230\u7684\u6b65\u9aa4\uff0c\u53ea\u4e0d\u8fc7\u5728\u9009\u62e9 Source \u7684\u65f6\u5019\uff0c\u8981\u6539\u4e3a\u9009\u62e9 Header \u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u547d\u4ee4\u884c\u73af\u5883\uff0c\u5219\u53ea\u9700\u8981\u7528\u7f16\u8f91\u5668\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u5373\u53ef\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u521b\u5efa\u5934\u6587\u4ef6\u7684\u65f6\u5019\u8bf7\u4f7f\u7528 .h \u540e\u7f00\u3002 \u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u901a\u5e38\u662f\u6210\u5bf9\u51fa\u73b0\u7684\uff0c\u5934\u6587\u4ef6\u4f1a\u4e3a\u5bf9\u5e94\u7684\u6e90\u6587\u4ef6\u63d0\u4f9b\u524d\u5411\u58f0\u660e\u3002\u56e0\u4e3a\u5934\u6587\u4ef6\u5305\u542b\u7684\u662f add.cpp \u4e2d\u51fd\u6570\u7684\u58f0\u660e\uff0c\u56e0\u6b64\u5934\u6587\u4ef6\u547d\u540d\u4e3a add.h \u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u6210\u5bf9\u51fa\u73b0\uff08\u4f8b\u5982\uff0cadd.h \u548c add.cpp\uff09\uff0c\u5b83\u4eec\u5e94\u8be5\u662f\u540c\u540d\u4f46\u4e0d\u540c\u540e\u7f00\u7684\u6587\u4ef6\u3002 \u5b8c\u6210\u540e\u7684\u5934\u6587\u4ef6\u5982\u4e0b\uff1a add.h: add.h 1 2 3 4 // 1) \u8fd9\u91cc\u5e94\u8be5\u662f\u5934\u6587\u4ef6\u5305\u542b\u4fdd\u62a4\uff0c\u4f46\u662f\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u53ea\u6709\u4e00\u4e2a\u5934\u6587\u4ef6\uff0c\u4e3a\u4e86\u7b80\u5316\u8fd9\u91cc\u5c31\u7701\u7565\u4e86 // 2) .h \u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u58f0\u660e\u90fd\u5199\u5728\u8fd9\u91cc int add ( int x , int y ); // \u51fd\u6570\u539f\u578b\u2014\u2014\u4e0d\u8981\u5fd8\u8bb0\u5206\u53f7\uff01 \u4e3a\u4e86\u5728 main.cpp \u4e2d\u4f7f\u7528\u8be5\u5934\u6587\u4ef6\uff0c\u6211\u4eec\u9700\u8981 #include (\u8fd9\u91cc\u9700\u8981\u4f7f\u7528\u5f15\u53f7\u800c\u975e\u5c16\u62ec\u53f7)\u3002 main.cpp 1 2 3 4 5 6 7 8 #include \"add.h\" // \u5c06 add.h \u63d2\u5165\u5230\u6b64\u4f4d\u7f6e\u3002\u6ce8\u610f\u4f7f\u7528\u53cc\u5f15\u53f7\u3002 #include int main () { std :: cout << \"The sum of 3 and 4 is \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } add.cpp 1 2 3 4 5 6 #include \"add.h\" // add.h \u7684\u5185\u5bb9\u4f1a\u88ab\u62f7\u8d1d\u5230\u8fd9\u91cc\u3002\u6ce8\u610f\u4f7f\u7528\u53cc\u5f15\u53f7\u3002 int add ( int x , int y ) { return x + y ; } \u5f53\u9884\u5904\u7406\u5668\u5904\u7406\u5230 #include \"add.h\" \u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u628a add.h \u4e2d\u7684\u5185\u5bb9\u90fd\u62f7\u8d1d\u5230\u8fd9\u91cc\u3002\u56e0\u4e3a add.h \u5305\u542b\u4e86\u51fd\u6570 add \u7684\u58f0\u660e\uff0c\u6240\u4ee5\u8be5\u58f0\u660e\u5c31\u4f1a\u88ab\u62f7\u8d1d\u5230\u4e86 main.cpp \u4e2d\u3002\u6700\u7ec8\u7684\u6548\u679c\uff0c\u5c31\u548c\u4e4b\u524d\u76f4\u63a5\u5c06\u58f0\u660e\u5199\u5728 main.cpp \u9876\u90e8\u662f\u4e00\u6837\u7684\u3002 \u73b0\u5728\uff0c\u6211\u4eec\u7684\u7a0b\u5e8f\u5c31\u53ef\u4ee5\u88ab\u6b63\u786e\u7684\u7f16\u8bd1\u548c\u94fe\u63a5\u4e86\u3002","title":"\u7f16\u5199\u5934\u6587\u4ef6"},{"location":"2-11-Header-files/#_4","text":"C++ \u4e2d\u7684\u6700\u4f73\u5b9e\u8df5\u4e4b\u4e00\u5c31\u662f\u6e90\u6587\u4ef6\u5e94\u8be5\u5305\u542b\u5176\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\uff08\u5982\u679c\u5b58\u5728\u7684\u8bdd\uff09\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c add.cpp \u5e94\u8be5\u5305\u542b add.h \u3002 \u8fd9\u4e48\u505a\u53ef\u4ee5\u4f7f\u5f97\u6709\u4e9b\u95ee\u9898\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u88ab\u53d1\u73b0\uff0c\u800c\u4e0d\u662f\u7559\u5230\u94fe\u63a5\u65f6\u518d\u53d1\u73b0\u3002\u4f8b\u5982\uff1a something.h 1 int something ( int ); // \u58f0\u660e\u7684\u8fd4\u56de\u7c7b\u578b\u662f int something.cpp 1 2 3 4 5 #include \"something.h\" void something ( int ) // \u9519\u8bef: \u9519\u8bef\u7684\u8fd4\u56de\u7c7b\u578b { } \u56e0\u4e3a something.cpp #includes \u4e86 something.h \uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u53d1\u73b0 something() \u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u5339\u914d\u3002\u800c\u5982\u679c something.cpp \u6ca1\u6709 #include something.h \uff0c\u90a3\u4e48\u6211\u4eec\u5fc5\u987b\u8981\u7b49\u5230\u94fe\u63a5\u65f6\uff0c\u8be5\u95ee\u9898\u624d\u4f1a\u88ab\u94fe\u63a5\u5668\u53d1\u73b0\uff0c\u8fd9\u65e0\u7591\u4f1a\u6d6a\u8d39\u65f6\u95f4\u3002\u5176\u4ed6\u4f8b\u5b50\u53ef\u4ee5\u53c2\u8003 \u8fd9\u4e2a\u8bc4\u8bba \u3002 \u6700\u4f73\u5b9e\u8df5 \u6e90\u6587\u4ef6\u9700\u8981\u5305\u542b\u5176\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\uff08\u5982\u679c\u6709\u7684\u8bdd\uff09\u3002","title":"\u6e90\u6587\u4ef6\u9700\u8981\u5305\u542b\u5176\u5bf9\u5e94\u7684\u5934\u6587\u4ef6"},{"location":"2-11-Header-files/#_5","text":"\u5982\u679c\u7f16\u8bd1\u5668\u62a5\u544a\u4e86 add.h \u65e0\u6cd5\u88ab\u627e\u7684\u9519\u8bef\uff0c\u8bf7\u9996\u5148\u786e\u8ba4\u6587\u4ef6\u540d\u662f\u5426\u4e3a add.h \u3002\u6587\u4ef6\u540d\u53ef\u80fd\u4f1a\u88ab\u9519\u8bef\u5730\u8bbe\u7f6e\u4e3a add (\u65e0\u540e\u7f00) \u6216 add.h.txt \u6216 add.hpp \uff0c\u8fd9\u53ef\u80fd\u53d6\u51b3\u4e8e\u4f60\u662f\u5982\u4f55\u521b\u5efa\u5b83\u4eec\u7684\u3002\u53e6\u5916\uff0c\u4e5f\u8981\u786e\u4fdd\u8be5\u5934\u6587\u4ef6\u548c\u5176\u4ed6\u6587\u4ef6\u4f4d\u4e8e\u76f8\u540c\u7684\u76ee\u5f55\u3002 \u5982\u679c\u94fe\u63a5\u5668\u62a5\u544a\u4e86 add \u51fd\u6570\u672a\u5b9a\u4e49\u7684\u9519\u8bef\uff0c\u8bf7\u786e\u4fdd add.cpp \u88ab\u6dfb\u52a0\u5230\u4e86\u9879\u76ee\u4e2d\uff0c\u8fd9\u6837 add \u51fd\u6570\u624d\u80fd\u591f\u88ab\u6b63\u786e\u94fe\u63a5\u3002","title":"\u9519\u8bef\u6392\u67e5"},{"location":"2-11-Header-files/#vs","text":"\u4f60\u53ef\u80fd\u4f1a\u597d\u5947\uff0c\u4e3a\u4ec0\u4e48 iostream \u4f7f\u7528\u7684\u662f\u5c16\u62ec\u53f7\uff0c\u800c add.h \u5c31\u9700\u8981\u4f7f\u7528\u53cc\u5f15\u53f7\u3002\u8fd9\u662f\u56e0\u4e3a\uff0c\u540c\u540d\u7684\u6587\u4ef6\u53ef\u80fd\u4f1a\u5206\u5e03\u5728\u4e0d\u540c\u7684\u76ee\u5f55\u4e2d\u3002\u533a\u5206\u4f7f\u7528\u5c16\u62ec\u53f7\u548c\u53cc\u5f15\u7528\uff0c\u53ef\u4ee5\u544a\u8bc9\u7f16\u8bd1\u5668\u5230\u54ea\u91cc\u5bfb\u627e\u5934\u6587\u4ef6\u3002 \u5f53\u4f7f\u7528\u5c16\u62ec\u53f7\u7684\u65f6\u5019\uff0c\u5176\u5b9e\u662f\u5728\u544a\u8bc9\u9884\u5904\u7406\u5668\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\u5e76\u4e0d\u662f\u6211\u4eec\u7f16\u5199\u7684\u3002\u7f16\u8bd1\u5668\u53ea\u4f1a\u5728 include directories \u6307\u5b9a\u7684\u76ee\u5f55\u4e2d\u641c\u7d22\u3002 include directories \u662f\u9879\u76ee\u3001IDE\u6216\u7f16\u8bd1\u5668\u914d\u7f6e\u7684\u4e00\u90e8\u5206\uff0c\u9ed8\u8ba4\u7684\u8def\u5f84\u662f\u7531\u7f16\u8bd1\u5668\u6216\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684\u3002\u7f16\u8bd1\u5668\u5e76\u4e0d\u4f1a\u5728\u4f60\u7684\u9879\u76ee\u76ee\u5f55\u4e2d\u641c\u7d22\u5bf9\u996e\u7684\u5934\u6587\u4ef6\u3002 \u5f53\u4f7f\u7528\u53cc\u5f15\u53f7\u7684\u65f6\u5019\uff0c\u5176\u5b9e\u662f\u5728\u544a\u8bc9\u9884\u5904\u7406\u5668\u5934\u6587\u4ef6\u662f\u6211\u4eec\u81ea\u5df1\u7f16\u5199\u7684\u3002\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u641c\u7d22\u5f53\u524d\u76ee\u5f55\uff0c\u5982\u679c\u627e\u4e0d\u5230\u6240\u9700\u7684\u5934\u6587\u4ef6\uff0c\u5219\u4f1a\u5728 include directories \u4e2d\u8fdb\u884c\u67e5\u627e\u3002 \u6cd5\u5219 \u4f7f\u7528\u53cc\u5f15\u53f7\u6765\u5305\u542b\u4f60\u81ea\u5df1\u7f16\u5199\u7684\u5934\u6587\u4ef6\uff0c\u5219\u8be5\u6587\u4ef6\u5fc5\u987b\u4f4d\u4e8e\u5f53\u524d\u76ee\u5f55\u4e2d\u3002\u4f7f\u7528\u5c16\u62ec\u53f7\u5305\u542b\u7f16\u8bd1\u5668\u3001\u64cd\u4f5c\u7cfb\u7edf\u6216\u7b2c\u4e09\u65b9\u63d0\u4f9b\u7684\u5b89\u88c5\u5728\u7cfb\u7edf\u5176\u4ed6\u4f4d\u7f6e\u7684\u5934\u6587\u4ef6\u3002","title":"\u5c16\u62ec\u53f7 vs \u53cc\u5f15\u53f7"},{"location":"2-11-Header-files/#iostream-h","text":"\u53e6\u5916\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u662f\uff1a\u201d\u4e3a\u4ec0\u4e48 iostream \uff08\u6216\u8005\u5176\u4ed6\u6807\u51c6\u5e93\u5934\u6587\u4ef6\uff09\u6ca1\u6709 .h \u540e\u7f00\u5462\uff1f\u201c\u3002\u8fd9\u662f\u56e0\u4e3a iostream.h \u548c iostream \u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u5934\u6587\u4ef6\u3002\u8fd9\u6d89\u53ca\u5230\u4e00\u4e9b\u5386\u53f2\u77e5\u8bc6\u3002 \u5728 C++ \u521a\u88ab\u521b\u5efa\u51fa\u6765\u7684\u65f6\u5019\uff0c\u6240\u6709\u7684\u6807\u51c6\u8fd0\u884c\u65f6\u5e93\u90fd\u662f\u4ee5 .h \u7ed3\u5c3e\u7684\u3002\u6700\u521d\u7248\u672c\u7684 cout \u548c cin \u4e5f\u90fd\u88ab\u5b9a\u4e49\u5728 iostream.h \u4e2d\u3002\u5728ANSI\u59d4\u5458\u4f1a\u5bf9\u8bed\u8a00\u8fdb\u884c\u6807\u51c6\u5316\u7684\u65f6\u5019\uff0c\u5b83\u4eec\u51b3\u5b9a\u8981\u5c06\u6807\u51c6\u5e93\u4e2d\u7684\u51fd\u6570\u90fd\u79fb\u5230 std \u547d\u540d\u7a7a\u95f4\u4e2d\u4ee5\u907f\u514d\u5176\u548c\u7528\u6237\u5b9a\u4e49\u7684\u51fd\u6570\u4ea7\u751f\u547d\u540d\u51b2\u7a81\u3002\u4e0d\u8fc7\uff0c\u73b0\u5b9e\u95ee\u9898\u662f\uff0c\u5982\u679c\u6b64\u65f6\u5c06\u8fd9\u4e9b\u51fd\u6570\u90fd\u79fb\u52a8\u5230 std \u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u90a3\u4e48\u4e4b\u524d\u5199\u7684\u4ee3\u7801\u5c31\u90fd\u4e0d\u80fd\u5de5\u4f5c\u4e86\uff01 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ea\u80fd\u4f7f\u7528\u4e00\u7ec4\u65b0\u7684\u5934\u6587\u4ef6\uff0c\u5b83\u4eec\u5177\u6709\u76f8\u540c\u7684\u540d\u5b57\uff0c\u4f46\u6ca1\u6709 .h \u540e\u7f00\u3002\u8fd9\u4e9b\u65b0\u7684\u5934\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\uff0c\u90fd\u5728 std \u4f5c\u7528\u57df\u4e2d\u3002\u8fd9\u6837\uff0c\u4f7f\u7528 #include \u7684\u8001\u4ee3\u7801\u5c31\u4e0d\u9700\u8981\u8fdb\u884c\u4efb\u4f55\u4fee\u6539\u4e86\uff0c\u800c\u65b0\u7f16\u5199\u7684\u4ee3\u7801\u5219\u9700\u8981\u4f7f\u7528 #include \u3002 \u6b64\u5916\uff0c\u4ece C \u8bed\u8a00\u7ee7\u627f\u8fc7\u6765\u7684\u6807\u51c6\u5e93\u5219\u88ab\u6dfb\u52a0\u4e86\u4e00\u4e2a c \u4f5c\u4e3a\u524d\u7f00(\u4f8b\u5982 stdlib.h \u53d8\u6210\u4e86 cstdlib )\u3002\u8fd9\u4e9b\u5e93\u4e2d\u7684\u51fd\u6570\u4e5f\u540c\u6837\u88ab\u79fb\u52a8\u5230\u4e86 std \u6807\u51c6\u5e93\u4e2d\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u4f7f\u7528\u6807\u51c6\u5e93\u5934\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u6ca1\u6709 .h \u7248\u672c\u7684\u5934\u6587\u4ef6\u3002\u5bf9\u4e8e\u7528\u6237\u81ea\u5df1\u7f16\u5199\u7684\u5934\u6587\u4ef6\uff0c\u4ecd\u7136\u9700\u8981\u4f7f\u7528 .h \u540e\u7f00\u3002","title":"\u4e3a\u4ec0\u4e48 iostream \u6ca1\u6709 .h \u540e\u7f00?"},{"location":"2-11-Header-files/#include","text":"\u53e6\u5916\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\u662f\u5982\u4f55\u5305\u542b\u5176\u4ed6\u76ee\u5f55\u4e2d\u7684\u5934\u6587\u4ef6\u3002 \u4e00\u79cd\u53ef\u884c\uff08\u4f46\u4e0d\u597d\u7684\uff09\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u6765\u6307\u5b9a #include \u7684\u5185\u5bb9\uff0c\u4f8b\u5982\uff1a 1 2 #include \"headers/myHeader.h\" #include \"../moreHeaders/myOtherHeader.h\" \u8fd9\u4e48\u505a\u7a0b\u5e8f\u662f\u53ef\u4ee5\u7f16\u8bd1\u7684\uff08\u5047\u8bbe\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\u786e\u5b9e\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e0b\uff09\uff0c\u4f46\u662f\u5b83\u7684\u7f3a\u70b9\u4e5f\u5f88\u660e\u663e\uff0c\u4f60\u5fc5\u987b\u5728\u4ee3\u7801\u4e2d\u63cf\u8ff0\u5b9e\u9645\u7684\u76ee\u5f55\u7ed3\u6784\u3002\u5982\u679c\u76ee\u5f55\u7ed3\u6784\u66f4\u65b0\u4e86\uff0c\u90a3\u4ee3\u7801\u4e5f\u5c31\u4e0d\u80fd\u5de5\u4f5c\u4e86\u3002 \u66f4\u597d\u7684\u529e\u6cd5\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\u6216\u8005 IDE\u6709\u4e9b\u5934\u6587\u4ef6\u5b58\u653e\u5728\u5176\u4ed6\u4f4d\u7f6e\uff0c\u6b64\u65f6\u5f53\u5b83\u4eec\u65e0\u6cd5\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u88ab\u627e\u5230\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u5230\u6307\u5b9a\u7684\u76ee\u5f55\u4e2d\u67e5\u627e\u3002\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u5728 IDE \u4e2d\u8bbe\u7f6e\u9879\u76ee\u7684 include path \u6216 search directory \u6765\u5b9e\u73b0\u3002 For Visual Studio users \u53f3\u952e\u5355\u51fb\u9879\u76ee\u7684 Solution Explorer \u7136\u540e\u9009\u62e9 Properties \u7136\u540e\u9009\u62e9 VC++ Directories \u9009\u9879\u5361\u3002\u5728\u8fd9\u91cc\u4f60\u53ef\u4ee5\u770b\u5230 Include Directories \u5c06\u4f60\u5e0c\u671b\u7f16\u8bd1\u5668\u641c\u7d22\u7684\u5305\u542b\u4e86\u5934\u6587\u4ef6\u7684\u76ee\u5f55\u586b\u5199\u5230\u8fd9\u91cc\u5373\u53ef\u3002 For Code:: Blocks users \u5728 Code:: Blocks \u4e2d\uff0c\u9009\u62e9 Project \u83dc\u5355\u5e76\u9009\u62e9 Build Options \uff0c\u518d\u9009\u62e9 Search directories \u9009\u9879\u5361\uff0c\u5c06\u4f60\u5e0c\u671b\u7f16\u8bd1\u5668\u641c\u7d22\u7684\u5305\u542b\u4e86\u5934\u6587\u4ef6\u7684\u76ee\u5f55\u586b\u5199\u5230\u8fd9\u91cc\u5373\u53ef\u3002 For GCC/G++ users \u4f7f\u7528 g++ \u662f\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7 -I \u9009\u9879\u6765\u6307\u660e\u5934\u6587\u4ef6\u641c\u7d22\u8def\u5f84\uff1a 1 g ++ - o main - I / source / includes main . cpp \u8fd9\u4e48\u505a\u7684\u597d\u5904\u662f\uff0c\u5982\u679c\u4f60\u6539\u53d8\u4e86\u76ee\u5f55\u7ed3\u6784\uff0c\u90a3\u4e48\u53ea\u9700\u8981\u5728\u8bbe\u7f6e\u91cc\u9762\u4fee\u6539\u8def\u5f84\u5373\u53ef\uff0c\u800c\u4e0d\u5fc5\u5bf9\u4ee3\u7801\u4e2d\u6bcf\u4e00\u5904\u4f7f\u7528\u8be5\u5934\u6587\u4ef6\u7684\u5730\u65b9\u8fdb\u884c\u4fee\u6539\u3002","title":"include \u5176\u4ed6\u76ee\u5f55\u4e2d\u7684\u5934\u6587\u4ef6"},{"location":"2-11-Header-files/#_6","text":"\u5934\u6587\u4ef6\u9700\u8981\u4f9d\u8d56\u5176\u4ed6\u5934\u6587\u4ef6\u4e2d\u7684\u5b9a\u4e49\u6216\u58f0\u660e\u4e5f\u662f\u5f88\u5e38\u89c1\u7684\u3002\u56e0\u6b64\uff0c\u5934\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528 #include \u6765\u5305\u542b\u5176\u4ed6\u5934\u6587\u4ef6\u3002 \u5f53\u4f60\u7684\u4ee3\u7801 #includes \u7b2c\u4e00\u4e2a\u5934\u6587\u4ef6\u65f6\uff0c\u4f60\u5176\u5b9e\u4e5f\u5305\u542b\u4e86\u8be5\u5934\u6587\u4ef6\u4e2d\u5305\u542b\u7684\u5176\u4ed6\u5934\u6587\u4ef6\uff08\u4ee5\u53ca\u8fd9\u6240\u6709\u5934\u6587\u4ef6\u4e2d\u5305\u542b\u7684\u5934\u6587\u4ef6\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff09\u3002\u8fd9\u4e9b\u989d\u5916\u7684\u5934\u6587\u4ef6\u6709\u65f6\u5019\u79f0\u4e3a \u95f4\u63a5\u5305\u542b(transitive includes) \uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u88ab\u9690\u5f0f\u5305\u542b\u8fdb\u6765\u7684\uff0c\u6211\u4eec\u660e\u6ca1\u6709\u6307\u660e\u54ea\u4e9b\u9700\u8981\u88ab\u5305\u542b\u3002 \u8fd9\u4e9b\u5934\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u5728\u4f60\u7684\u4ee3\u7801\u4e2d\u662f\u53ef\u7528\u7684\u3002\u4e0d\u8fc7\u4f60\u5e76\u4e0d\u5e94\u8be5\u4f9d\u8d56\u5b83\u4eec\uff0c\u6bd5\u7adf\u5b83\u4eec\u662f\u88ab\u95f4\u63a5\u5305\u542b\u8fdb\u6765\u7684\u3002\u8fd9\u4e9b\u5934\u6587\u4ef6\u53ef\u80fd\u4f1a\u53d8\u5316\uff0c\u4e5f\u53ef\u80fd\u5728\u4e0d\u540c\u7684\u7cfb\u7edf\u4e2d\u5b58\u5728\u5dee\u5f02\uff0c\u8fd9\u65f6\u5019\u4f60\u7684\u4ee3\u7801\u53ef\u80fd\u53ea\u80fd\u5728\u67d0\u5e73\u53f0\u4e0b\u624d\u80fd\u7f16\u8bd1\uff0c\u6216\u8005\u53ef\u80fd\u5728\u5c06\u6765\u65e0\u6cd5\u7f16\u8bd1\u3002\u907f\u514d\u4e0a\u8ff0\u95ee\u9898\u7684\u65b9\u6cd5\u4e5f\u5f88\u7b80\u5355\uff0c\u8bf7\u660e\u786e\u5305\u542b\u4f60\u6240\u9700\u8981\u7684\u5168\u90e8\u5934\u6587\u4ef6\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u4e8e\u7f16\u8bd1\u6240\u9700\u7684\u5934\u6587\u4ef6\uff0c\u6bcf\u4e00\u4e2a\u90fd\u5e94\u8be5\u88ab\u660e\u786e\u5730\u4f7f\u7528 #include \u5305\u542b\u8fdb\u6765\u3002\u4e0d\u8981\u4f9d\u8d56\u88ab\u95f4\u63a5\u5305\u542b\u7684\u5934\u6587\u4ef6\u3002 \u4e0d\u5e78\u7684\u662f\uff0c\u6211\u4eec\u5f88\u96be\u53bb\u7504\u522b\u7a76\u7adf\u54ea\u4e9b\u4ee3\u7801\u6b63\u5728\u4f9d\u8d56\u90a3\u4e9b\u88ab\u95f4\u63a5\u5f15\u5165\u7684\u5934\u6587\u4ef6\u3002 Q: \u6211\u6ca1\u6709 include \uff0c\u4f46\u662f\u7a0b\u5e8f\u4ecd\u7136\u80fd\u6b63\u5e38\u5de5\u4f5c\uff01\u4e3a\u4ec0\u4e48\uff1f \u8fd9\u4e2a\u95ee\u9898\u4e5f\u662f\u5e38\u5907\u95ee\u5230\u7684\u95ee\u9898\u4e4b\u4e00\u3002\u8fd9\u53ef\u80fd\u662f\u56e0\u4e3a\u4f60\u5305\u542b\u4e86\u67d0\u4e2a\u5934\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u6070\u5de7\u8be5\u6587\u4ef6\u4e5f\u5305\u542b\u4e86\u4f60\u6240\u9700\u7684\u90a3\u4e2a\u5934\u6587\u4ef6\uff0c\u6240\u4ee5\u4ee3\u7801\u53ef\u4ee5\u5de5\u4f5c\u3002\u5c3d\u7ba1\u4f60\u7684\u7a0b\u5e8f\u53ef\u4ee5\u7f16\u8bd1\uff0c\u6700\u4f73\u5b9e\u8df5\u544a\u8bc9\u6211\u4eec\uff0c\u8fd9\u79cd\u65b9\u5f0f\u5e76\u4e0d\u53ef\u9760\u3002\u5728\u4f60\u7684\u7535\u8111\u4e0a\u80fd\u7f16\u8bd1\uff0c\u4e0d\u4ee3\u8868\u5728\u5176\u4ed6\u7535\u8111\u4e0a\u4e5f\u80fd\u7f16\u8bd1\u3002","title":"\u5934\u6587\u4ef6\u4e2d\u53ef\u4ee5\u5305\u542b\u5176\u4ed6\u5934\u6587\u4ef6"},{"location":"2-11-Header-files/#include_1","text":"\u5982\u679c\u4f60\u7684\u5934\u6587\u4ef6\u5185\u5bb9\u6ca1\u95ee\u9898\uff0c\u5e76\u4e14\u4e5f #include \u4e86\u5b83\u4eec\u6240\u4f9d\u8d56\u7684\u5176\u4ed6\u5934\u6587\u4ef6\uff0c\u90a3\u4e48\u5934\u6587\u4ef6\u5305\u542b\u7684\u987a\u5e8f\u5176\u5b9e\u5e76\u4e0d\u91cd\u8981\u3002 \u4e0d\u8fc7\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u5982\u4e0b\u573a\u666f\uff1a\u5934\u6587\u4ef6 A \u9700\u8981\u5934\u6587\u4ef6 B\u4e2d\u7684\u58f0\u660e\uff0c\u4f46\u662f\u5374\u5fd8\u8bb0\u5305\u542b B \u4e86\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u5728\u6e90\u6587\u4ef6\u4e2d\uff0c\u5148\u5305\u542b B \u518d\u5305\u542bA\uff0c\u90a3\u4e48\u7a0b\u5e8f\u662f\u53ef\u4ee5\u7f16\u8bd1\u7684\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u4f1a\u9996\u5148\u7f16\u8bd1\u5230B\u4e2d\u7684\u58f0\u660e\uff0c\u7136\u540e\u624d\u4f1a\u7f16\u8bd1\u5230A\u4e2d\u5bf9B\u6709\u4f9d\u8d56\u7684\u90a3\u4e9b\u4ee3\u7801\u3002 \u53cd\u4e4b\uff0c\u5982\u679c\u6211\u4eec\u5148\u5305\u542b\u5934\u6587\u4ef6 A\uff0c\u518d\u5305\u542b\u5934\u6587\u4ef6 B\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c31\u4f1a\u62a5\u9519\u4e86\uff0c\u56e0\u4e3a\u5b83\u4e0d\u80fd\u5728\u7f16\u8bd1 A \u65f6\u627e\u5230\u5176\u6240\u4f9d\u8d56\u7684 B \u4e2d\u7684\u58f0\u660e\u3002\u5176\u5b9e\u8fd9\u6b63\u662f\u6211\u4eec\u5e0c\u671b\u7684\u7ed3\u679c\uff0c\u56e0\u4e3a\u5b83\u628a\u95ee\u9898\u66b4\u9732\u4e86\u51fa\u6765\uff0c\u6211\u4eec\u4fbf\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u4fee\u590d\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e3a\u4e86\u6700\u5927\u7a0b\u5ea6\u51cf\u5c11\u5934\u6587\u4ef6\u6ca1\u6709\u6b63\u786e\u5305\u542b\u5f15\u8d77\u7684\u7f16\u8bd1\u5668\u62a5\u9519\uff0c\u8bf7\u6309\u7167\u5982\u4e0b\u987a\u5e8f\u5305\u542b\u5934\u6587\u4ef6\uff1a \u5148\u5305\u542b\u6e90\u6587\u4ef6\u5bf9\u5e94\u7684\u5934\u6587\u4ef6 \u518d\u5305\u542b\u9879\u76ee\u6240\u9700\u7684\u5176\u4ed6\u5934\u6587\u4ef6 \u518d\u5305\u542b\u7b2c\u4e09\u65b9\u5e93\u7684\u5934\u6587\u4ef6 \u518d\u5305\u542b\u6807\u51c6\u5e93\u5934\u6587\u4ef6 \u6bcf\u4e00\u7ec4\u5934\u6587\u4ef6\u90fd\u5e94\u8be5\u6309\u7167\u5b57\u6bcd\u8868\u987a\u5e8f\u6392\u5e8f \u8fd9\u6837\uff0c\u4efb\u4f55\u4e00\u4e2a\u4f60\u5b9a\u4e49\u7684\u5934\u6587\u4ef6\u5982\u679c\u6ca1\u6709 #include \u5176\u6240\u9700\u7684\u7b2c\u4e09\u65b9\u5e93\u6216\u6807\u51c6\u5e93\uff0c\u90a3\u4e48\u5f88\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u8fdb\u884c\u4fee\u590d\u4e86\u3002","title":"\u5934\u6587\u4ef6 #include \u7684\u987a\u5e8f"},{"location":"2-11-Header-files/#_7","text":"\u5173\u4e8e\u5982\u4f55\u4f7f\u7528\u548c\u521b\u5efa\u5934\u6587\u4ef6\uff0c\u6211\u4eec\u6709\u5982\u4e0b\u5efa\u8bae\uff1a \u59cb\u7ec8\u4f7f\u7528 \u5934\u6587\u4ef6\u91cd\u590d\u5305\u542b\u4fdd\u62a4 \uff1b \u4e0d\u8981\u5728\u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\u53d8\u91cf\u548c\u51fd\u6570 (\u5168\u5c40\u5e38\u91cf\u662f\u4e2a\u4f8b\u5916\u2014\u2014\u7a0d\u540e\u4ecb\u7ecd\uff09\uff1b \u5934\u6587\u4ef6\u548c\u5176\u5bf9\u5e94\u7684\u6e90\u6587\u4ef6\u5e94\u8be5\u5177\u6709\u76f8\u540c\u7684\u6587\u4ef6\u540d\uff08\u4f8b\u5982\uff0c grades.h \u548c grades.cpp \uff0c\u6ce8\u610f\u6269\u5c55\u540d\u662f\u4e0d\u540c\u7684\uff09\uff1b \u6bcf\u4e2a\u5934\u6587\u4ef6\u90fd\u5e94\u8be5\u6709\u5176\u7279\u5b9a\u7684\u7528\u9014\u5e76\u4e14\u5c3d\u53ef\u80fd\u72ec\u7acb\u3002\u4f8b\u5982\uff0c\u4e0eA\u76f8\u5173\u7684\u58f0\u660e\u5e94\u8be5\u653e\u5728 A.h \u4e2d\uff0c\u4e0eB\u76f8\u5173\u7684\u58f0\u660e\u5e94\u8be5\u653e\u5728 B.h \u4e2d\u3002\u8fd9\u6837\u5982\u679c\u4ee5\u540e\u4ec5\u4ec5\u9700\u8981\u4f7f\u7528A\u76f8\u5173\u7684\u529f\u80fd\uff0c\u5219\u65e0\u9700\u5305\u542b B.h \uff0c\u4e5f\u5c31\u4e0d\u4f1a\u6d89\u53ca\u5230\u4efb\u4f55\u4e0eB\u76f8\u5173\u7684\u51fd\u6570\u58f0\u660e\uff1b \u5bf9\u4e8e\u54ea\u4e9b\u51fd\u6570\u5e94\u8be5\u5305\u542b\u54ea\u4e9b\u5934\u6587\u4ef6\u8981\u505a\u5230\u5fc3\u4e2d\u6709\u6570\uff1b \u4f60\u7f16\u5199\u7684\u4efb\u4f55\u5934\u6587\u4ef6\u90fd\u5e94\u8be5\u80fd\u591f\u72ec\u7acb\u7f16\u8bd1\uff08\u5b83\u5e94\u5f53 #include \u5b83\u6240\u9700\u7684\u5168\u90e8\u4f9d\u8d56\uff09\uff1b \u53ea #include \u5fc5\u8981\u7684\u5934\u6587\u4ef6\uff08\u4e0d\u8981\u628a\u80fd\u5305\u542b\u7684\u5168\u90e8\u5305\u542b\u8fdb\u6765\uff09\uff1b \u4e0d\u8981 #include .cpp \u6587\u4ef6\u3002","title":"\u5934\u6587\u4ef6\u6700\u4f73\u5b9e\u8df5"},{"location":"2-12-Header-guards/","tags":["header guards","pragma once"],"text":"Key Takeaway \u7531\u4e8e\u5934\u6587\u4ef6\u9632\u8303\u4e0d\u80fd\u9632\u6b62\u5934\u6587\u4ef6\u88ab\u5305\u542b\u5230\u4e0d\u540c\u7684\u6587\u4ef6\uff0c\u5934\u6587\u4ef6\u4e2d\u7684\u5b9a\u4e49\u53ef\u80fd\u4f1a\u5bfc\u81f4\u94fe\u63a5\u5668\u62a5\u544a\u51fd\u6570\u91cd\u5b9a\u4e49\u3002 \u4e0d\u53ef\u80fd\u5b8c\u5168\u907f\u514d\u5934\u6587\u4ef6\u4e2d\u7684\u5b9a\u4e49\uff0c\u6bd5\u7adf\u7c7b\u662f\u9700\u8981\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u7684\u3002 \u5bf9\u4e8e\u4e0d\u5305\u542b\u5b9a\u4e49\u7684\u5934\u6587\u4ef6\uff0c\u867d\u7136\u6ca1\u5fc5\u8981\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\uff0c\u4f46\u8fd9\u5176\u5b9e\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\uff08\u5c24\u5176\u8003\u8651\u5230\u540e\u9762\u53ef\u80fd\u5bf9\u5934\u6587\u4ef6\u8fdb\u884c\u7684\u6539\u52a8\uff09 \u5904\u4e8e\u517c\u5bb9\u6027\u7684\u8003\u8651\uff0c\u4f18\u5148\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\u800c\u975e #pragma once \u91cd\u590d\u5b9a\u4e49\u95ee\u9898 \u5728 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u53d8\u91cf\u548c\u51fd\u6570\u7684\u6807\u8bc6\u7b26\u53ea\u80fd\u88ab\u5b9a\u4e49\u4e00\u6b21\uff0c\u5373 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219 \u3002\u56e0\u6b64\uff0c\u5982\u679c\u4e00\u4e2a\u7a0b\u5e8f\u5305\u542b\u4e86\u5bf9\u67d0\u4e2a\u6807\u8bc6\u7b26\u7684\u591a\u6b21\u5b9a\u4e49\uff0c\u5c06\u4f1a\u5f15\u8d77\u7f16\u8bd1\u9519\u8bef \uff1a 1 2 3 4 5 6 7 int main () { int x ; // this is a definition for variable x int x ; // compile error: duplicate definition return 0 ; } \u7c7b\u4f3c\u5730\uff0c\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u5728\u7a0b\u5e8f\u4e2d\u88ab\u5b9a\u4e49\u4e86\u591a\u6b21\uff0c\u4e5f\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int foo () // this is a definition for function foo { return 5 ; } int foo () // compile error: duplicate definition { return 5 ; } int main () { std :: cout << foo (); return 0 ; } \u5c3d\u7ba1\u8fd9\u4e9b\u95ee\u9898\u4fee\u590d\u8d77\u6765\u5e76\u4e0d\u96be\uff08\u79fb\u9664\u91cd\u590d\u5b9a\u4e49\u5373\u53ef\uff09\uff0c\u4f46\u662f\u5bf9\u4e8e\u4f7f\u7528\u5934\u6587\u4ef6\u7684\u60c5\u51b5\u6765\u8bf4\uff0c\u5982\u679c\u5934\u6587\u4ef6\u4e2d\u5b58\u5728\u5b9a\u4e49\uff0c\u90a3\u4e48\u5b83\u5f88\u6709\u53ef\u80fd\u4f1a\u88ab\u91cd\u590d\u5730\u5305\u542b\u5230\u4ee3\u7801\u4e2d\u3002\u5c24\u5176\u662f\u4e00\u4e2a\u5934\u6587\u4ef6\u4e2d\u8fd8\u5305\u62ec\u5176\u4ed6\u5934\u6587\u4ef6\u7684\u60c5\u51b5\uff08\u5f88\u5e38\u89c1\uff09\u3002 \u8bf7\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a square.h 1 2 3 4 5 // \u5934\u6587\u4ef6\u91cc\u4e0d\u5e94\u8be5\u5305\u542b\u51fd\u6570\u7684\u5b9a\u4e49\uff0c\u8fd9\u91cc\u53ea\u662f\u4e3a\u4e86\u4e3e\u4f8b int getSquareSides () { return 4 ; } geometry.h 1 #include \"square.h\" main.cpp 1 2 3 4 5 6 7 #include \"square.h\" #include \"geometry.h\" int main () { return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u770b\u4e0a\u53bb\u6ca1\u4ec0\u4e48\u95ee\u9898\uff0c\u4f46\u5b9e\u9645\u4e0a\u5374\u4e0d\u80fd\u7f16\u8bd1\uff01\u539f\u56e0\u5982\u4e0b\uff1a\u9996\u5148\uff0c main.cpp \u6587\u4ef6 #includes \u4e86 square.h \uff0c\u56e0\u6b64\u4f1a\u628a\u51fd\u6570 getSquareSides_into \u7684\u5b9a\u4e49\u62f7\u8d1d\u5230\u4e86 main.cpp \u3002 main.cpp \u6587\u4ef6\u540c\u65f6\u8fd8\u5305\u542b\u4e86 #includes geometry.h \u8fdb\u800c #includes \u4e86 square.h \u3002\u8fd9\u6837 square.h \u7684\u5b9a\u4e49\uff08\u5305\u542b\u4e86 getSquareSides \u7684\u5b9a\u4e49\uff09\u4e5f\u88ab\u62f7\u8d1d\u5230\u4e86 geometry.h \uff0c\u8fdb\u800c\u4e5f\u88ab\u62f7\u8d1d\u5230\u4e86 main.cpp \u3002 \u56e0\u6b64\uff0c\u5728\u89e3\u6790\u4e86\u7684\u5168\u90e8\u7684\u5934\u6587\u4ef6\u540e\uff0c main.cpp \u7684\u5185\u5bb9\u53d8\u6210\u4e86\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int getSquareSides () // from square.h { return 4 ; } int getSquareSides () // from geometry.h (via square.h) { return 4 ; } int main () { return 0 ; } \u5355\u72ec\u770b\u6bcf\u4e2a\u51fd\u6570\u90fd\u6ca1\u6709\u95ee\u9898\uff0c\u4e0d\u8fc7\uff0c\u7531\u4e8e\u6700\u7ec8\u7684\u7ed3\u679c\u7b49\u6548\u4e8e _main.cpp_ \u5305\u542b\u4e86\u4e24\u6b21 square.h \uff0c\u6240\u4ee5\u624d\u9047\u5230\u4e86\u4e0a\u8ff0\u95ee\u9898\u3002 \u5982\u679c geometry.h \u9700\u8981 getSquareSides() \u800c\u4e14 main.cpp \u9700\u8981 geometry.h \u548c square.h \uff0c\u90a3\u4e48\u5e94\u8be5\u5982\u4f55\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\u5462\uff1f \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e \u597d\u6d88\u606f\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 \u5934\u6587\u4ef6\u9632\u8303 header guards \uff08\u6216 include \u9632\u8303\uff09\u6280\u672f\u6765\u907f\u514d\u4e0a\u8ff0\u95ee\u9898\u3002\u5934\u6587\u4ef6\u9632\u8303\u5176\u5b9e\u662f\u4e00\u79cd\u6761\u4ef6\u7f16\u8bd1\uff0c\u5b83\u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 2 3 4 5 6 #ifndef SOME_UNIQUE_NAME_HERE #define SOME_UNIQUE_NAME_HERE // your declarations (and certain types of definitions) here #endif \u5f53\u5934\u6587\u4ef6\u88ab\u5305\u542b\u65f6\uff0c\u9884\u5904\u7406\u5668\u4f1a\u68c0\u67e5 SOME_UNIQUE_NAME_HERE \u662f\u5426\u5df2\u7ecf\u88ab\u5b9a\u4e49\u8fc7\u3002\u5982\u679c\u6ca1\u6709\u8bf4\u660e\u8fd9\u662f\u7b2c\u4e00\u6b21\u5f15\u5165\u8be5\u5934\u6587\u4ef6\uff0c SOME_UNIQUE_NAME_HERE \u80af\u5b9a\u5c1a\u672a\u88ab\u5b9a\u4e49\u3002\u6b64\u65f6\uff0c\u8be5\u5934\u6587\u4ef6\u4f1a #defines SOME_UNIQUE_NAME_HERE \u5e76\u4e14\u5305\u542b\u5934\u6587\u4ef6\u7684\u5185\u5bb9\u3002\u5982\u679c\u8be5\u5934\u6587\u4ef6\u4e4b\u540e\u53c8\u88ab\u5305\u542b\u4e86\uff0c\u90a3\u4e48\u7531\u4e8e SOME_UNIQUE_NAME_HERE \u4e4b\u524d\u5df2\u7ecf\u88ab\u5b9a\u4e49\u8fc7\u4e86\uff0c\u6240\u4ee5\u8be5\u5934\u6587\u4ef6\u4e2d\u5305\u542b\u7684\u5185\u5bb9\u5c06\u4f1a\u88ab\u5ffd\u7565\uff08\u7531\u4e8e #ifndef \u7684\u5b58\u5728\uff09 \u6211\u4eec\u4f7f\u7528\u7684\u6240\u6709\u5934\u6587\u4ef6\uff0c\u90fd\u5e94\u8be5\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e\u3002 SOME_UNIQUE_NAME_HERE \u53ef\u4ee5\u662f\u4efb\u4f55\u540d\u5b57\uff0c\u4f46\u662f\u901a\u5e38\u7684\u60ef\u4f8b\u662f\u4f7f\u7528\u5934\u6587\u4ef6\u7684 \u5168\u540d \u3001\u5168\u90e8\u5927\u5199\u5b57\u6bcd\u5e76\u4f7f\u7528\u4e0b\u5212\u7ebf\u4ee3\u66ff\u7a7a\u683c\u6216\u6807\u70b9\uff0c\u4f8b\u5982 square.h \u7684\u5934\u6587\u4ef6\u9632\u8303\u5982\u4e0b\uff1a square.h 1 2 3 4 5 6 7 8 9 #ifndef SQUARE_H #define SQUARE_H int getSquareSides () { return 4 ; } #endif \u6807\u51c6\u5e93\u4e2d\u4e5f\u4f7f\u7528\u4e86\u5934\u6587\u4ef6\u9632\u8303\u3002\u5982\u679c\u4f60\u53bb\u770b\u770b iostream \u7684\u5934\u6587\u4ef6\uff0c\u4f60\u4f1a\u53d1\u73b0\u5b83\u662f\u8fd9\u6837\u7684\uff1a iostream 1 2 3 4 5 6 #ifndef _IOSTREAM_ #define _IOSTREAM_ // content here #endif \u6269\u5c55\u9605\u8bfb \u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u4f7f\u7528\u4e24\u4e2a\u6765\u81ea\u4e24\u4e2a\u4e0d\u540c\u76ee\u5f55\u7684\u540c\u540d\u5934\u6587\u4ef6\u4e5f\u662f\u53ef\u80fd\u7684(\u4f8b\u5982\uff1a directoryA\\config.h \u548c directoryB\\config.h )\u3002\u5982\u679c\u5934\u6587\u4ef6\u9632\u8303\u53ea\u5229\u7528\u4e86\u6587\u4ef6\u540d\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u5934\u6587\u4ef6\u7684\u5934\u6587\u4ef6\u9632\u8303\u5e94\u8be5\u4e5f\u662f\u4e00\u6837\u7684\uff08\u4f8b\u5982\uff1a CONFIG_H \uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4efb\u4f55\u540c\u65f6\u5305\u542b\u4e86\u4e0a\u8ff0\u4e24\u4e2a\u5934\u6587\u4ef6\u7684\u6587\u4ef6\uff0c\u5176\u5b9e\u5728\u5305\u542b\u7b2c\u4e8c\u4e2a\u7684\u65f6\u5019\u662f\u4e0d\u751f\u6548\u7684\uff0c\u8fd9\u6837\u5c31\u6709\u53ef\u80fd\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 \u6b63\u662f\u7531\u4e8e\u5b58\u5728\u5934\u6587\u4ef6\u9632\u8303\u51b2\u7a81\u7684\u53ef\u80fd\uff0c\u5f88\u591a\u5f00\u53d1\u8005\u5efa\u8bae\u4f7f\u7528\u66f4\u590d\u6742/\u72ec\u7279\u7684\u65b9\u5f0f\u6765\u6784\u5efa\u76f8\u5173\u7684\u5b8f\u3002\u4f8b\u5982\uff1a ___H \u3001 __H \u6216\u8005 __H \u3002 \u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\u66f4\u65b0\u4e4b\u524d\u7684\u4f8b\u5b50 \u518d\u56de\u5230 square.h \u7684\u4f8b\u5b50\uff0c\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u62a4\u6765\u5904\u7406 square.h \u3002\u4e3a\u4e86\u4fdd\u6301\u4e00\u81f4\uff0c\u6211\u4eec\u5728 geometry.h \u4e2d\u4e5f\u6dfb\u52a0\u4e86\u5934\u6587\u4ef6\u5939\u9632\u62a4\u3002 square.h 1 2 3 4 5 6 7 8 9 #ifndef SQUARE_H #define SQUARE_H int getSquareSides () { return 4 ; } #endif geometry.h 1 2 3 4 5 6 #ifndef GEOMETRY_H #define GEOMETRY_H #include \"square.h\" #endif main.cpp 1 2 3 4 5 6 7 #include \"square.h\" #include \"geometry.h\" int main () { return 0 ; } \u5728\u9884\u5904\u7406\u5668\u89e3\u6790\u5168\u90e8\u5934\u6587\u4ef6\u540e\uff0c\u7a0b\u5e8f\u5185\u5bb9\u53d8\u6210\u4e86\u4e0b\u9762\u8fd9\u6837\uff1a main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 // Square.h included from main.cpp #ifndef SQUARE_H // square.h included from main.cpp #define SQUARE_H // SQUARE_H gets defined here // and all this content gets included int getSquareSides () { return 4 ; } #endif // SQUARE_H #ifndef GEOMETRY_H // geometry.h included from main.cpp #define GEOMETRY_H #ifndef SQUARE_H // square.h included from geometry.h, SQUARE_H is already defined from above #define SQUARE_H // so none of this content gets included int getSquareSides () { return 4 ; } #endif // SQUARE_H #endif // GEOMETRY_H int main () { return 0 ; } \u4ece\u4e0a\u9762\u7684\u4f8b\u5b50\u53ef\u4ee5\u770b\u51fa\uff0c\u5f53\u7b2c\u4e8c\u6b21\u5305\u542b square.h \u7684\u65f6\u5019\uff0c\u7531\u4e8e SQUARE_H \u5df2\u7ecf\u88ab\u5b9a\u4e49\u8fc7\u4e86\u3002\u56e0\u6b64 getSquareSides \u51fd\u6570\u53ea\u4f1a\u88ab\u5305\u542b\u4e00\u6b21\u3002 \u5934\u6587\u4ef6\u9632\u8303\u4e0d\u80fd\u9632\u6b62\u5934\u6587\u4ef6\u88ab\u5305\u542b\u5230\u4e0d\u540c\u7684\u6587\u4ef6 \u5934\u6587\u4ef6\u9632\u8303\u7684\u76ee\u6807\u662f\u9632\u6b62\u5934\u6587\u4ef6\u88ab\u591a\u6b21\u5305\u542b\u3002\u4ece\u8bbe\u8ba1\u4e0a\u6765\u8bb2\uff0c\u5b83\u5e76\u4e0d\u80fd\u4fdd\u62a4\u5934\u6587\u4ef6\u88ab\u5305\u542b\u5230\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\uff08\u6bcf\u4e2a\u6587\u4ef6\u53ea\u80fd\u5305\u542b\u4e00\u6b21\uff09\u3002\u8fd9\u53ef\u80fd\u5bfc\u81f4\u96be\u4ee5\u9884\u6599\u7684\u95ee\u9898\u3002 \u8003\u8651\u5982\u4e0b\u60c5\u51b5\uff1a square.h 1 2 3 4 5 6 7 8 9 10 11 #ifndef SQUARE_H #define SQUARE_H int getSquareSides () { return 4 ; } int getSquarePerimeter ( int sideLength ); // getSquarePerimeter \u7684\u524d\u5411\u58f0\u660e #endif square.cpp 1 2 3 4 5 6 #include \"square.h\" // square.h \u5728\u6b64\u5904\u88ab\u5305\u542b\u4e00\u6b21 int getSquarePerimeter ( int sideLength ) { return sideLength * getSquareSides (); } main.cpp 1 2 3 4 5 6 7 8 9 10 #include \"square.h\" // square.h \u5728\u6b64\u5904\u88ab\u5305\u542b\u4e00\u6b21 #include int main () { std :: cout << \"a square has \" << getSquareSides () << \" sides \\n \" ; std :: cout << \"a square of length 5 has perimeter length \" << getSquarePerimeter ( 5 ) << '\\n' ; return 0 ; } \u6ce8\u610f square.h \u88ab\u5305\u542b\u5230\u4e86 main.cpp \u548c square.cpp \u4e2d\u3002\u8fd9\u610f\u5473\u7740 square.h \u7684\u5185\u5bb9\u88ab square.cpp \u548c main.cpp \u5404\u5305\u542b\u4e86\u4e00\u6b21\u3002 \u8ba9\u6211\u4eec\u4ed4\u7ec6\u8bb2\u89e3\u4e00\u4e0b\u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\u3002\u5f53 square.h \u88ab\u5305\u542b\u5230 square.cpp \u65f6 _SQUARE_H_ \u88ab\u5b9a\u4e49\u4e86\uff0c\u5b83\u7684\u5b9a\u4e49\u5230 square.cpp \u6587\u4ef6\u7684\u672b\u5c3e\u4e3a\u6b62\u3002\u8be5\u5b9a\u4e49\u53ef\u4ee5\u907f\u514d square.h \u7684\u5185\u5bb9\u591a\u6b21\u88ab\u5305\u542b\u5230 square.cpp \u4e2d \u3002\u4f46\u662f\uff0c square.cpp \u5904\u7406\u5b8c\u6210\u540e\uff0c SQUARE_H \u5c31\u6ca1\u6709\u5b9a\u4e49\u4e86\u3002\u8fd9\u610f\u5473\u5f53\u9884\u5904\u7406\u5668\u5904\u7406 main.cpp \u65f6\uff0c SQUARE_H \u5c1a\u672a\u5728 main.cpp \u4e2d\u5b9a\u4e49\u3002 \u7531\u4e8e square.cpp \u548c main.cpp \u4e2d\u90fd\u5305\u542b\u4e86 getSquareSides \u51fd\u6570\u7684\u5b9a\u4e49\u3002\u6b64\u65f6\uff0c\u6587\u4ef6\u662f\u53ef\u4ee5\u7f16\u8bd1\u7684\uff0c\u4f46\u662f\u5728\u94fe\u63a5\u65f6\u94fe\u63a5\u5668\u4f1a\u62a5\u544a\u7a0b\u5e8f\u5b58\u5728 getSquareSides \u51fd\u6570\u7684\u91cd\u590d\u5b9a\u4e49\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u6700\u540e\u7684\u65b9\u6cd5\u662f\u628a\u51fd\u6570\u5b9a\u4e49\u653e\u5728\u4e00\u4e2a .cpp \u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u5934\u6587\u4ef6\u4e2d\u5c31\u53ea\u5305\u542b\u51fd\u6570\u7684\u58f0\u660e\uff1a square.h 1 2 3 4 5 6 7 #ifndef SQUARE_H #define SQUARE_H int getSquareSides (); // forward declaration for getSquareSides int getSquarePerimeter ( int sideLength ); // forward declaration for getSquarePerimeter #endif square.cpp 1 2 3 4 5 6 7 8 9 10 11 #include \"square.h\" int getSquareSides () // actual definition for getSquareSides { return 4 ; } int getSquarePerimeter ( int sideLength ) { return sideLength * getSquareSides (); } main.cpp 1 2 3 4 5 6 7 8 9 10 #include \"square.h\" // square.h is also included once here #include int main () { std :: cout << \"a square has \" << getSquareSides () << \"sides \\n \" ; std :: cout << \"a square of length 5 has perimeter length \" << getSquarePerimeter ( 5 ) << '\\n' ; return 0 ; } \u73b0\u5728\uff0c \u7a0b\u5e8f\u5728\u7f16\u8bd1\u7684\u65f6\u5019\uff0c getSquareSides \u51fd\u6570\u53ea\u6709\u4e00\u4e2a\u5b9a\u4e49\u4e86\uff08\u6765\u81ea square.cpp \uff09\uff0c\u56e0\u6b64\u94fe\u63a5\u5668\u4e0d\u4f1a\u518d\u62a5\u9519\u3002 main.cpp \u53ef\u4ee5\u8c03\u7528\u8be5\u51fd\u6570 (\u5373\u4f7f\u5b83\u7684\u5b9a\u4e49\u5728 square.cpp \u4e2d) \uff0c\u56e0\u4e3a\u5b83\u5305\u542b\u4e86 square.h \uff08\u5305\u542b\u8be5\u51fd\u6570\u7684\u58f0\u660e\uff09\uff0c\u94fe\u63a5\u5668\u4f1a\u628a main.cpp \u4e2d\u5bf9 getSquareSides \u51fd\u6570\u7684\u8c03\u7528\u5173\u8054\u5230 square.cpp \u4e2d getSquareSides \u51fd\u6570\u7684\u5b9a\u4e49\u3002 \u4e0d\u628a\u5b9a\u4e49\u653e\u5728\u5934\u6587\u4ef6\u4e2d\u4e0d\u5c31\u884c\u4e86\uff1f \u4e4b\u524d\u6211\u4eec\u8bf4\u8fc7\uff0c\u4e0d\u8981\u628a\u51fd\u6570\u7684\u5b9a\u4e49\u653e\u5728\u5934\u6587\u4ef6\u4e2d\u3002\u6240\u4ee5\u4f60\u53ef\u80fd\u4f1a\u95ee\uff0c\u90a3\u4e3a\u4ec0\u4e48\u8fd8\u8981\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\u5462\uff1f\u6bd5\u7adf\u5b83\u8981\u89e3\u51b3\u7684\u95ee\u9898\u4ece\u4e00\u5f00\u59cb\u5c31\u5e94\u8be5\u907f\u514d\u3002 \u5176\u5b9e\uff0c\u540e\u9762\u6211\u4eec\u4f1a\u5411\u4f60\u5c55\u793a\u4e00\u4e9b \u975e\u51fd\u6570 \u5b9a\u4e49\u88ab\u653e\u7f6e\u5728\u5934\u6587\u4ef6\u4e2d\u7684\u4f8b\u5b50\u3002\u4f8b\u5982\uff0cC++ \u5141\u8bb8\u4f60\u521b\u5efa\u81ea\u5b9a\u4e49\u7c7b\u578b\u3002\u8fd9\u4e9b\u81ea\u5b9a\u4e49\u7c7b\u578b\u901a\u5e38\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u624d\u80fd\u8ba9\u5bf9\u5e94\u7684\u6e90\u6587\u4ef6\u4f7f\u7528\u8fd9\u4e9b\u5b9a\u4e49\u3002\u5982\u679c\u4e0d\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\uff0c\u5219\u5b83\u4eec\u5f88\u53ef\u80fd\u4f1a\u88ab\u591a\u6b21\u62f7\u8d1d\uff0c\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\u3002 \u56e0\u6b64\uff0c\u5373\u4fbf\u6559\u7a0b\u770b\u5230\u8fd9\u91cc\u7684\u65f6\u5019\u4f60\u8fd8\u6ca1\u6709\u5fc5\u8981\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\uff0c\u80fd\u591f\u4ece\u73b0\u5728\u5c31\u517b\u6210\u4e60\u60ef\u8fd8\u662f\u5f88\u597d\u7684\u3002 #pragma once \u73b0\u4ee3\u7f16\u8bd1\u5668\u652f\u6301\u4e00\u79cd\u66f4\u7b80\u5355\u7684\u5904\u7406\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528 #pragma \u6307\u4ee4\u6765\u4ee3\u66ff\u5934\u6587\u4ef6\u9632\u8303\uff1a 1 2 3 #pragma once // your code here #pragma once \u7684\u529f\u80fd\u548c\u5934\u6587\u4ef6\u9632\u8303\u662f\u4e00\u6837\u7684\uff0c\u800c\u4e14\u66f4\u52a0\u7b80\u77ed\u4e14\u4e0d\u6613\u51fa\u9519\u3002\u5728\u5927\u591a\u6570\u7684\u9879\u76ee\u4e2d\uff0c #pragma once \u90fd\u53ef\u4ee5\u5f88\u597d\u7684\u5de5\u4f5c\uff0c\u56e0\u6b64\u5f88\u591a\u5f00\u53d1\u8005\u90fd\u503e\u5411\u4e8e\u4f7f\u7528\u5b83\u800c\u4e0d\u662f\u5934\u6587\u4ef6\u9632\u8303\u3002\u4e0d\u8fc7\uff0c #pragma once \u5e76\u4e0d\u662f C++ \u8bed\u8a00\u7684\u4e00\u90e8\u5206 (\u800c\u4e14\u53ef\u80fd\u6c38\u8fdc\u90fd\u4e0d\u4f1a\u6210\u4e3a C++ \u7684\u4e00\u90e8\u5206\uff0c\u56e0\u4e3a\u6ca1\u529e\u6cd5\u4fdd\u8bc1\u5728\u4efb\u548c\u60c5\u51b5\u4e0b\u5b83\u90fd\u80fd\u6b63\u786e\u5730\u5de5\u4f5c\uff09 \u5904\u4e8e\u517c\u5bb9\u6027\u7684\u8003\u8651\uff0c\u6211\u4eec\u8fd8\u662f\u63a8\u8350\u4f7f\u7528\u4f20\u7edf\u65b9\u5f0f\u2014\u2014\u5934\u6587\u4ef6\u9632\u8303\u3002\u5934\u6587\u4ef6\u9632\u8303\u7528\u8d77\u6765\u4e5f\u4e0d\u590d\u6742\u800c\u4e14\u6240\u6709\u7684\u7f16\u8bd1\u5668\u90fd\u652f\u6301\u3002 \u6700\u4f73\u5b9e\u8df5 \u5904\u4e8e\u517c\u5bb9\u6027\u7684\u8003\u8651\uff0c\u4f18\u5148\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\u800c\u975e #pragma once \u3002 \u5c0f\u7ed3 \u5934\u6587\u4ef6\u9632\u8303\u53ef\u4ee5\u786e\u4fdd\u8be5\u5934\u6587\u4ef6\u7684\u5185\u5bb9\u88ab\u5305\u542b\u5230\u67d0\u4e2a\u6587\u4ef6\u65f6\uff0c\u5176\u5185\u5bb9\u53ea\u88ab\u62f7\u8d1d\u4e00\u6b21\uff0c\u4ee5\u907f\u514d\u91cd\u590d\u5b9a\u4e49\u7684\u95ee\u9898\u3002 \u6ce8\u610f\uff0c\u91cd\u590d\u58f0\u660e\u662f\u53ef\u4ee5\u7684\uff0c\u56e0\u4e3a\u591a\u6b21\u58f0\u660e\u5e76\u4e0d\u4f1a\u5e26\u6765\u4efb\u4f55\u95ee\u9898\u3002\u4e0d\u8fc7\uff0c\u5373\u4f7f\u4f60\u7684\u5934\u6587\u4ef6\u4e2d\u53ea\u5305\u542b\u58f0\u660e\uff0c\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\u4ecd\u7136\u662f\u6700\u4f73\u5b9e\u8df5\u3002 \u6ce8\u610f\uff0c\u5934\u6587\u4ef6\u9632\u8303\u4e0d\u80fd\u907f\u514d\u5934\u6587\u4ef6\u7684\u5185\u5bb9\u88ab\u62f7\u8d1d\u5230\u591a\u4e2a\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\u3002\u4e0d\u8fc7\uff0c\u8fd9\u662f\u597d\u4e8b\uff0c\u56e0\u4e3a\u6211\u4eec\u65f6\u5e38\u9700\u8981\u5c06\u5934\u6587\u4ef6\u5305\u542b\u5230\u591a\u4e2a\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\u3002","title":"2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e"},{"location":"2-12-Header-guards/#_1","text":"\u5728 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u53d8\u91cf\u548c\u51fd\u6570\u7684\u6807\u8bc6\u7b26\u53ea\u80fd\u88ab\u5b9a\u4e49\u4e00\u6b21\uff0c\u5373 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219 \u3002\u56e0\u6b64\uff0c\u5982\u679c\u4e00\u4e2a\u7a0b\u5e8f\u5305\u542b\u4e86\u5bf9\u67d0\u4e2a\u6807\u8bc6\u7b26\u7684\u591a\u6b21\u5b9a\u4e49\uff0c\u5c06\u4f1a\u5f15\u8d77\u7f16\u8bd1\u9519\u8bef \uff1a 1 2 3 4 5 6 7 int main () { int x ; // this is a definition for variable x int x ; // compile error: duplicate definition return 0 ; } \u7c7b\u4f3c\u5730\uff0c\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u5728\u7a0b\u5e8f\u4e2d\u88ab\u5b9a\u4e49\u4e86\u591a\u6b21\uff0c\u4e5f\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int foo () // this is a definition for function foo { return 5 ; } int foo () // compile error: duplicate definition { return 5 ; } int main () { std :: cout << foo (); return 0 ; } \u5c3d\u7ba1\u8fd9\u4e9b\u95ee\u9898\u4fee\u590d\u8d77\u6765\u5e76\u4e0d\u96be\uff08\u79fb\u9664\u91cd\u590d\u5b9a\u4e49\u5373\u53ef\uff09\uff0c\u4f46\u662f\u5bf9\u4e8e\u4f7f\u7528\u5934\u6587\u4ef6\u7684\u60c5\u51b5\u6765\u8bf4\uff0c\u5982\u679c\u5934\u6587\u4ef6\u4e2d\u5b58\u5728\u5b9a\u4e49\uff0c\u90a3\u4e48\u5b83\u5f88\u6709\u53ef\u80fd\u4f1a\u88ab\u91cd\u590d\u5730\u5305\u542b\u5230\u4ee3\u7801\u4e2d\u3002\u5c24\u5176\u662f\u4e00\u4e2a\u5934\u6587\u4ef6\u4e2d\u8fd8\u5305\u62ec\u5176\u4ed6\u5934\u6587\u4ef6\u7684\u60c5\u51b5\uff08\u5f88\u5e38\u89c1\uff09\u3002 \u8bf7\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a square.h 1 2 3 4 5 // \u5934\u6587\u4ef6\u91cc\u4e0d\u5e94\u8be5\u5305\u542b\u51fd\u6570\u7684\u5b9a\u4e49\uff0c\u8fd9\u91cc\u53ea\u662f\u4e3a\u4e86\u4e3e\u4f8b int getSquareSides () { return 4 ; } geometry.h 1 #include \"square.h\" main.cpp 1 2 3 4 5 6 7 #include \"square.h\" #include \"geometry.h\" int main () { return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u770b\u4e0a\u53bb\u6ca1\u4ec0\u4e48\u95ee\u9898\uff0c\u4f46\u5b9e\u9645\u4e0a\u5374\u4e0d\u80fd\u7f16\u8bd1\uff01\u539f\u56e0\u5982\u4e0b\uff1a\u9996\u5148\uff0c main.cpp \u6587\u4ef6 #includes \u4e86 square.h \uff0c\u56e0\u6b64\u4f1a\u628a\u51fd\u6570 getSquareSides_into \u7684\u5b9a\u4e49\u62f7\u8d1d\u5230\u4e86 main.cpp \u3002 main.cpp \u6587\u4ef6\u540c\u65f6\u8fd8\u5305\u542b\u4e86 #includes geometry.h \u8fdb\u800c #includes \u4e86 square.h \u3002\u8fd9\u6837 square.h \u7684\u5b9a\u4e49\uff08\u5305\u542b\u4e86 getSquareSides \u7684\u5b9a\u4e49\uff09\u4e5f\u88ab\u62f7\u8d1d\u5230\u4e86 geometry.h \uff0c\u8fdb\u800c\u4e5f\u88ab\u62f7\u8d1d\u5230\u4e86 main.cpp \u3002 \u56e0\u6b64\uff0c\u5728\u89e3\u6790\u4e86\u7684\u5168\u90e8\u7684\u5934\u6587\u4ef6\u540e\uff0c main.cpp \u7684\u5185\u5bb9\u53d8\u6210\u4e86\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int getSquareSides () // from square.h { return 4 ; } int getSquareSides () // from geometry.h (via square.h) { return 4 ; } int main () { return 0 ; } \u5355\u72ec\u770b\u6bcf\u4e2a\u51fd\u6570\u90fd\u6ca1\u6709\u95ee\u9898\uff0c\u4e0d\u8fc7\uff0c\u7531\u4e8e\u6700\u7ec8\u7684\u7ed3\u679c\u7b49\u6548\u4e8e _main.cpp_ \u5305\u542b\u4e86\u4e24\u6b21 square.h \uff0c\u6240\u4ee5\u624d\u9047\u5230\u4e86\u4e0a\u8ff0\u95ee\u9898\u3002 \u5982\u679c geometry.h \u9700\u8981 getSquareSides() \u800c\u4e14 main.cpp \u9700\u8981 geometry.h \u548c square.h \uff0c\u90a3\u4e48\u5e94\u8be5\u5982\u4f55\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\u5462\uff1f","title":"\u91cd\u590d\u5b9a\u4e49\u95ee\u9898"},{"location":"2-12-Header-guards/#_2","text":"\u597d\u6d88\u606f\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 \u5934\u6587\u4ef6\u9632\u8303 header guards \uff08\u6216 include \u9632\u8303\uff09\u6280\u672f\u6765\u907f\u514d\u4e0a\u8ff0\u95ee\u9898\u3002\u5934\u6587\u4ef6\u9632\u8303\u5176\u5b9e\u662f\u4e00\u79cd\u6761\u4ef6\u7f16\u8bd1\uff0c\u5b83\u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 2 3 4 5 6 #ifndef SOME_UNIQUE_NAME_HERE #define SOME_UNIQUE_NAME_HERE // your declarations (and certain types of definitions) here #endif \u5f53\u5934\u6587\u4ef6\u88ab\u5305\u542b\u65f6\uff0c\u9884\u5904\u7406\u5668\u4f1a\u68c0\u67e5 SOME_UNIQUE_NAME_HERE \u662f\u5426\u5df2\u7ecf\u88ab\u5b9a\u4e49\u8fc7\u3002\u5982\u679c\u6ca1\u6709\u8bf4\u660e\u8fd9\u662f\u7b2c\u4e00\u6b21\u5f15\u5165\u8be5\u5934\u6587\u4ef6\uff0c SOME_UNIQUE_NAME_HERE \u80af\u5b9a\u5c1a\u672a\u88ab\u5b9a\u4e49\u3002\u6b64\u65f6\uff0c\u8be5\u5934\u6587\u4ef6\u4f1a #defines SOME_UNIQUE_NAME_HERE \u5e76\u4e14\u5305\u542b\u5934\u6587\u4ef6\u7684\u5185\u5bb9\u3002\u5982\u679c\u8be5\u5934\u6587\u4ef6\u4e4b\u540e\u53c8\u88ab\u5305\u542b\u4e86\uff0c\u90a3\u4e48\u7531\u4e8e SOME_UNIQUE_NAME_HERE \u4e4b\u524d\u5df2\u7ecf\u88ab\u5b9a\u4e49\u8fc7\u4e86\uff0c\u6240\u4ee5\u8be5\u5934\u6587\u4ef6\u4e2d\u5305\u542b\u7684\u5185\u5bb9\u5c06\u4f1a\u88ab\u5ffd\u7565\uff08\u7531\u4e8e #ifndef \u7684\u5b58\u5728\uff09 \u6211\u4eec\u4f7f\u7528\u7684\u6240\u6709\u5934\u6587\u4ef6\uff0c\u90fd\u5e94\u8be5\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e\u3002 SOME_UNIQUE_NAME_HERE \u53ef\u4ee5\u662f\u4efb\u4f55\u540d\u5b57\uff0c\u4f46\u662f\u901a\u5e38\u7684\u60ef\u4f8b\u662f\u4f7f\u7528\u5934\u6587\u4ef6\u7684 \u5168\u540d \u3001\u5168\u90e8\u5927\u5199\u5b57\u6bcd\u5e76\u4f7f\u7528\u4e0b\u5212\u7ebf\u4ee3\u66ff\u7a7a\u683c\u6216\u6807\u70b9\uff0c\u4f8b\u5982 square.h \u7684\u5934\u6587\u4ef6\u9632\u8303\u5982\u4e0b\uff1a square.h 1 2 3 4 5 6 7 8 9 #ifndef SQUARE_H #define SQUARE_H int getSquareSides () { return 4 ; } #endif \u6807\u51c6\u5e93\u4e2d\u4e5f\u4f7f\u7528\u4e86\u5934\u6587\u4ef6\u9632\u8303\u3002\u5982\u679c\u4f60\u53bb\u770b\u770b iostream \u7684\u5934\u6587\u4ef6\uff0c\u4f60\u4f1a\u53d1\u73b0\u5b83\u662f\u8fd9\u6837\u7684\uff1a iostream 1 2 3 4 5 6 #ifndef _IOSTREAM_ #define _IOSTREAM_ // content here #endif \u6269\u5c55\u9605\u8bfb \u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u4f7f\u7528\u4e24\u4e2a\u6765\u81ea\u4e24\u4e2a\u4e0d\u540c\u76ee\u5f55\u7684\u540c\u540d\u5934\u6587\u4ef6\u4e5f\u662f\u53ef\u80fd\u7684(\u4f8b\u5982\uff1a directoryA\\config.h \u548c directoryB\\config.h )\u3002\u5982\u679c\u5934\u6587\u4ef6\u9632\u8303\u53ea\u5229\u7528\u4e86\u6587\u4ef6\u540d\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u5934\u6587\u4ef6\u7684\u5934\u6587\u4ef6\u9632\u8303\u5e94\u8be5\u4e5f\u662f\u4e00\u6837\u7684\uff08\u4f8b\u5982\uff1a CONFIG_H \uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4efb\u4f55\u540c\u65f6\u5305\u542b\u4e86\u4e0a\u8ff0\u4e24\u4e2a\u5934\u6587\u4ef6\u7684\u6587\u4ef6\uff0c\u5176\u5b9e\u5728\u5305\u542b\u7b2c\u4e8c\u4e2a\u7684\u65f6\u5019\u662f\u4e0d\u751f\u6548\u7684\uff0c\u8fd9\u6837\u5c31\u6709\u53ef\u80fd\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 \u6b63\u662f\u7531\u4e8e\u5b58\u5728\u5934\u6587\u4ef6\u9632\u8303\u51b2\u7a81\u7684\u53ef\u80fd\uff0c\u5f88\u591a\u5f00\u53d1\u8005\u5efa\u8bae\u4f7f\u7528\u66f4\u590d\u6742/\u72ec\u7279\u7684\u65b9\u5f0f\u6765\u6784\u5efa\u76f8\u5173\u7684\u5b8f\u3002\u4f8b\u5982\uff1a ___H \u3001 __H \u6216\u8005 __H \u3002","title":"\u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e"},{"location":"2-12-Header-guards/#_3","text":"\u518d\u56de\u5230 square.h \u7684\u4f8b\u5b50\uff0c\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u62a4\u6765\u5904\u7406 square.h \u3002\u4e3a\u4e86\u4fdd\u6301\u4e00\u81f4\uff0c\u6211\u4eec\u5728 geometry.h \u4e2d\u4e5f\u6dfb\u52a0\u4e86\u5934\u6587\u4ef6\u5939\u9632\u62a4\u3002 square.h 1 2 3 4 5 6 7 8 9 #ifndef SQUARE_H #define SQUARE_H int getSquareSides () { return 4 ; } #endif geometry.h 1 2 3 4 5 6 #ifndef GEOMETRY_H #define GEOMETRY_H #include \"square.h\" #endif main.cpp 1 2 3 4 5 6 7 #include \"square.h\" #include \"geometry.h\" int main () { return 0 ; } \u5728\u9884\u5904\u7406\u5668\u89e3\u6790\u5168\u90e8\u5934\u6587\u4ef6\u540e\uff0c\u7a0b\u5e8f\u5185\u5bb9\u53d8\u6210\u4e86\u4e0b\u9762\u8fd9\u6837\uff1a main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 // Square.h included from main.cpp #ifndef SQUARE_H // square.h included from main.cpp #define SQUARE_H // SQUARE_H gets defined here // and all this content gets included int getSquareSides () { return 4 ; } #endif // SQUARE_H #ifndef GEOMETRY_H // geometry.h included from main.cpp #define GEOMETRY_H #ifndef SQUARE_H // square.h included from geometry.h, SQUARE_H is already defined from above #define SQUARE_H // so none of this content gets included int getSquareSides () { return 4 ; } #endif // SQUARE_H #endif // GEOMETRY_H int main () { return 0 ; } \u4ece\u4e0a\u9762\u7684\u4f8b\u5b50\u53ef\u4ee5\u770b\u51fa\uff0c\u5f53\u7b2c\u4e8c\u6b21\u5305\u542b square.h \u7684\u65f6\u5019\uff0c\u7531\u4e8e SQUARE_H \u5df2\u7ecf\u88ab\u5b9a\u4e49\u8fc7\u4e86\u3002\u56e0\u6b64 getSquareSides \u51fd\u6570\u53ea\u4f1a\u88ab\u5305\u542b\u4e00\u6b21\u3002","title":"\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\u66f4\u65b0\u4e4b\u524d\u7684\u4f8b\u5b50"},{"location":"2-12-Header-guards/#_4","text":"\u5934\u6587\u4ef6\u9632\u8303\u7684\u76ee\u6807\u662f\u9632\u6b62\u5934\u6587\u4ef6\u88ab\u591a\u6b21\u5305\u542b\u3002\u4ece\u8bbe\u8ba1\u4e0a\u6765\u8bb2\uff0c\u5b83\u5e76\u4e0d\u80fd\u4fdd\u62a4\u5934\u6587\u4ef6\u88ab\u5305\u542b\u5230\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\uff08\u6bcf\u4e2a\u6587\u4ef6\u53ea\u80fd\u5305\u542b\u4e00\u6b21\uff09\u3002\u8fd9\u53ef\u80fd\u5bfc\u81f4\u96be\u4ee5\u9884\u6599\u7684\u95ee\u9898\u3002 \u8003\u8651\u5982\u4e0b\u60c5\u51b5\uff1a square.h 1 2 3 4 5 6 7 8 9 10 11 #ifndef SQUARE_H #define SQUARE_H int getSquareSides () { return 4 ; } int getSquarePerimeter ( int sideLength ); // getSquarePerimeter \u7684\u524d\u5411\u58f0\u660e #endif square.cpp 1 2 3 4 5 6 #include \"square.h\" // square.h \u5728\u6b64\u5904\u88ab\u5305\u542b\u4e00\u6b21 int getSquarePerimeter ( int sideLength ) { return sideLength * getSquareSides (); } main.cpp 1 2 3 4 5 6 7 8 9 10 #include \"square.h\" // square.h \u5728\u6b64\u5904\u88ab\u5305\u542b\u4e00\u6b21 #include int main () { std :: cout << \"a square has \" << getSquareSides () << \" sides \\n \" ; std :: cout << \"a square of length 5 has perimeter length \" << getSquarePerimeter ( 5 ) << '\\n' ; return 0 ; } \u6ce8\u610f square.h \u88ab\u5305\u542b\u5230\u4e86 main.cpp \u548c square.cpp \u4e2d\u3002\u8fd9\u610f\u5473\u7740 square.h \u7684\u5185\u5bb9\u88ab square.cpp \u548c main.cpp \u5404\u5305\u542b\u4e86\u4e00\u6b21\u3002 \u8ba9\u6211\u4eec\u4ed4\u7ec6\u8bb2\u89e3\u4e00\u4e0b\u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\u3002\u5f53 square.h \u88ab\u5305\u542b\u5230 square.cpp \u65f6 _SQUARE_H_ \u88ab\u5b9a\u4e49\u4e86\uff0c\u5b83\u7684\u5b9a\u4e49\u5230 square.cpp \u6587\u4ef6\u7684\u672b\u5c3e\u4e3a\u6b62\u3002\u8be5\u5b9a\u4e49\u53ef\u4ee5\u907f\u514d square.h \u7684\u5185\u5bb9\u591a\u6b21\u88ab\u5305\u542b\u5230 square.cpp \u4e2d \u3002\u4f46\u662f\uff0c square.cpp \u5904\u7406\u5b8c\u6210\u540e\uff0c SQUARE_H \u5c31\u6ca1\u6709\u5b9a\u4e49\u4e86\u3002\u8fd9\u610f\u5473\u5f53\u9884\u5904\u7406\u5668\u5904\u7406 main.cpp \u65f6\uff0c SQUARE_H \u5c1a\u672a\u5728 main.cpp \u4e2d\u5b9a\u4e49\u3002 \u7531\u4e8e square.cpp \u548c main.cpp \u4e2d\u90fd\u5305\u542b\u4e86 getSquareSides \u51fd\u6570\u7684\u5b9a\u4e49\u3002\u6b64\u65f6\uff0c\u6587\u4ef6\u662f\u53ef\u4ee5\u7f16\u8bd1\u7684\uff0c\u4f46\u662f\u5728\u94fe\u63a5\u65f6\u94fe\u63a5\u5668\u4f1a\u62a5\u544a\u7a0b\u5e8f\u5b58\u5728 getSquareSides \u51fd\u6570\u7684\u91cd\u590d\u5b9a\u4e49\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u6700\u540e\u7684\u65b9\u6cd5\u662f\u628a\u51fd\u6570\u5b9a\u4e49\u653e\u5728\u4e00\u4e2a .cpp \u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u5934\u6587\u4ef6\u4e2d\u5c31\u53ea\u5305\u542b\u51fd\u6570\u7684\u58f0\u660e\uff1a square.h 1 2 3 4 5 6 7 #ifndef SQUARE_H #define SQUARE_H int getSquareSides (); // forward declaration for getSquareSides int getSquarePerimeter ( int sideLength ); // forward declaration for getSquarePerimeter #endif square.cpp 1 2 3 4 5 6 7 8 9 10 11 #include \"square.h\" int getSquareSides () // actual definition for getSquareSides { return 4 ; } int getSquarePerimeter ( int sideLength ) { return sideLength * getSquareSides (); } main.cpp 1 2 3 4 5 6 7 8 9 10 #include \"square.h\" // square.h is also included once here #include int main () { std :: cout << \"a square has \" << getSquareSides () << \"sides \\n \" ; std :: cout << \"a square of length 5 has perimeter length \" << getSquarePerimeter ( 5 ) << '\\n' ; return 0 ; } \u73b0\u5728\uff0c \u7a0b\u5e8f\u5728\u7f16\u8bd1\u7684\u65f6\u5019\uff0c getSquareSides \u51fd\u6570\u53ea\u6709\u4e00\u4e2a\u5b9a\u4e49\u4e86\uff08\u6765\u81ea square.cpp \uff09\uff0c\u56e0\u6b64\u94fe\u63a5\u5668\u4e0d\u4f1a\u518d\u62a5\u9519\u3002 main.cpp \u53ef\u4ee5\u8c03\u7528\u8be5\u51fd\u6570 (\u5373\u4f7f\u5b83\u7684\u5b9a\u4e49\u5728 square.cpp \u4e2d) \uff0c\u56e0\u4e3a\u5b83\u5305\u542b\u4e86 square.h \uff08\u5305\u542b\u8be5\u51fd\u6570\u7684\u58f0\u660e\uff09\uff0c\u94fe\u63a5\u5668\u4f1a\u628a main.cpp \u4e2d\u5bf9 getSquareSides \u51fd\u6570\u7684\u8c03\u7528\u5173\u8054\u5230 square.cpp \u4e2d getSquareSides \u51fd\u6570\u7684\u5b9a\u4e49\u3002","title":"\u5934\u6587\u4ef6\u9632\u8303\u4e0d\u80fd\u9632\u6b62\u5934\u6587\u4ef6\u88ab\u5305\u542b\u5230\u4e0d\u540c\u7684\u6587\u4ef6"},{"location":"2-12-Header-guards/#_5","text":"\u4e4b\u524d\u6211\u4eec\u8bf4\u8fc7\uff0c\u4e0d\u8981\u628a\u51fd\u6570\u7684\u5b9a\u4e49\u653e\u5728\u5934\u6587\u4ef6\u4e2d\u3002\u6240\u4ee5\u4f60\u53ef\u80fd\u4f1a\u95ee\uff0c\u90a3\u4e3a\u4ec0\u4e48\u8fd8\u8981\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\u5462\uff1f\u6bd5\u7adf\u5b83\u8981\u89e3\u51b3\u7684\u95ee\u9898\u4ece\u4e00\u5f00\u59cb\u5c31\u5e94\u8be5\u907f\u514d\u3002 \u5176\u5b9e\uff0c\u540e\u9762\u6211\u4eec\u4f1a\u5411\u4f60\u5c55\u793a\u4e00\u4e9b \u975e\u51fd\u6570 \u5b9a\u4e49\u88ab\u653e\u7f6e\u5728\u5934\u6587\u4ef6\u4e2d\u7684\u4f8b\u5b50\u3002\u4f8b\u5982\uff0cC++ \u5141\u8bb8\u4f60\u521b\u5efa\u81ea\u5b9a\u4e49\u7c7b\u578b\u3002\u8fd9\u4e9b\u81ea\u5b9a\u4e49\u7c7b\u578b\u901a\u5e38\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u624d\u80fd\u8ba9\u5bf9\u5e94\u7684\u6e90\u6587\u4ef6\u4f7f\u7528\u8fd9\u4e9b\u5b9a\u4e49\u3002\u5982\u679c\u4e0d\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\uff0c\u5219\u5b83\u4eec\u5f88\u53ef\u80fd\u4f1a\u88ab\u591a\u6b21\u62f7\u8d1d\uff0c\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\u3002 \u56e0\u6b64\uff0c\u5373\u4fbf\u6559\u7a0b\u770b\u5230\u8fd9\u91cc\u7684\u65f6\u5019\u4f60\u8fd8\u6ca1\u6709\u5fc5\u8981\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\uff0c\u80fd\u591f\u4ece\u73b0\u5728\u5c31\u517b\u6210\u4e60\u60ef\u8fd8\u662f\u5f88\u597d\u7684\u3002","title":"\u4e0d\u628a\u5b9a\u4e49\u653e\u5728\u5934\u6587\u4ef6\u4e2d\u4e0d\u5c31\u884c\u4e86\uff1f"},{"location":"2-12-Header-guards/#pragma-once","text":"\u73b0\u4ee3\u7f16\u8bd1\u5668\u652f\u6301\u4e00\u79cd\u66f4\u7b80\u5355\u7684\u5904\u7406\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528 #pragma \u6307\u4ee4\u6765\u4ee3\u66ff\u5934\u6587\u4ef6\u9632\u8303\uff1a 1 2 3 #pragma once // your code here #pragma once \u7684\u529f\u80fd\u548c\u5934\u6587\u4ef6\u9632\u8303\u662f\u4e00\u6837\u7684\uff0c\u800c\u4e14\u66f4\u52a0\u7b80\u77ed\u4e14\u4e0d\u6613\u51fa\u9519\u3002\u5728\u5927\u591a\u6570\u7684\u9879\u76ee\u4e2d\uff0c #pragma once \u90fd\u53ef\u4ee5\u5f88\u597d\u7684\u5de5\u4f5c\uff0c\u56e0\u6b64\u5f88\u591a\u5f00\u53d1\u8005\u90fd\u503e\u5411\u4e8e\u4f7f\u7528\u5b83\u800c\u4e0d\u662f\u5934\u6587\u4ef6\u9632\u8303\u3002\u4e0d\u8fc7\uff0c #pragma once \u5e76\u4e0d\u662f C++ \u8bed\u8a00\u7684\u4e00\u90e8\u5206 (\u800c\u4e14\u53ef\u80fd\u6c38\u8fdc\u90fd\u4e0d\u4f1a\u6210\u4e3a C++ \u7684\u4e00\u90e8\u5206\uff0c\u56e0\u4e3a\u6ca1\u529e\u6cd5\u4fdd\u8bc1\u5728\u4efb\u548c\u60c5\u51b5\u4e0b\u5b83\u90fd\u80fd\u6b63\u786e\u5730\u5de5\u4f5c\uff09 \u5904\u4e8e\u517c\u5bb9\u6027\u7684\u8003\u8651\uff0c\u6211\u4eec\u8fd8\u662f\u63a8\u8350\u4f7f\u7528\u4f20\u7edf\u65b9\u5f0f\u2014\u2014\u5934\u6587\u4ef6\u9632\u8303\u3002\u5934\u6587\u4ef6\u9632\u8303\u7528\u8d77\u6765\u4e5f\u4e0d\u590d\u6742\u800c\u4e14\u6240\u6709\u7684\u7f16\u8bd1\u5668\u90fd\u652f\u6301\u3002 \u6700\u4f73\u5b9e\u8df5 \u5904\u4e8e\u517c\u5bb9\u6027\u7684\u8003\u8651\uff0c\u4f18\u5148\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\u800c\u975e #pragma once \u3002","title":"#pragma once"},{"location":"2-12-Header-guards/#_6","text":"\u5934\u6587\u4ef6\u9632\u8303\u53ef\u4ee5\u786e\u4fdd\u8be5\u5934\u6587\u4ef6\u7684\u5185\u5bb9\u88ab\u5305\u542b\u5230\u67d0\u4e2a\u6587\u4ef6\u65f6\uff0c\u5176\u5185\u5bb9\u53ea\u88ab\u62f7\u8d1d\u4e00\u6b21\uff0c\u4ee5\u907f\u514d\u91cd\u590d\u5b9a\u4e49\u7684\u95ee\u9898\u3002 \u6ce8\u610f\uff0c\u91cd\u590d\u58f0\u660e\u662f\u53ef\u4ee5\u7684\uff0c\u56e0\u4e3a\u591a\u6b21\u58f0\u660e\u5e76\u4e0d\u4f1a\u5e26\u6765\u4efb\u4f55\u95ee\u9898\u3002\u4e0d\u8fc7\uff0c\u5373\u4f7f\u4f60\u7684\u5934\u6587\u4ef6\u4e2d\u53ea\u5305\u542b\u58f0\u660e\uff0c\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\u4ecd\u7136\u662f\u6700\u4f73\u5b9e\u8df5\u3002 \u6ce8\u610f\uff0c\u5934\u6587\u4ef6\u9632\u8303\u4e0d\u80fd\u907f\u514d\u5934\u6587\u4ef6\u7684\u5185\u5bb9\u88ab\u62f7\u8d1d\u5230\u591a\u4e2a\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\u3002\u4e0d\u8fc7\uff0c\u8fd9\u662f\u597d\u4e8b\uff0c\u56e0\u4e3a\u6211\u4eec\u65f6\u5e38\u9700\u8981\u5c06\u5934\u6587\u4ef6\u5305\u542b\u5230\u591a\u4e2a\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\u3002","title":"\u5c0f\u7ed3"},{"location":"2-13-How-to-design-your-first-programs/","tags":["desgin"],"text":"Key Takeaway \u5f00\u59cb\u65f6\u4e00\u5207\u4ece\u7b80 \u9010\u6e10\u6dfb\u52a0\u65b0\u7684\u529f\u80fd \u4e00\u6b21\u53ea\u505a\u4e00\u4ef6\u4e8b \u4e00\u8fb9\u5b9e\u73b0\u3001\u4e00\u8fb9\u6d4b\u8bd5 \u4e0d\u8981\u8fc7\u65e9\u5730\u4f18\u5316\u4ee3\u7801 \u5728\u5b66\u4e60\u8fc7\u57fa\u672c\u7684\u7f16\u7a0b\u77e5\u8bc6\u540e\uff0c\u8ba9\u6211\u4eec\u6765\u770b\u770b\u5982\u4f55\u8bbe\u8ba1\u7b2c\u4e00\u4e2a\u7a0b\u5e8f\u3002 \u5f53\u4f60\u5750\u4e0b\u6765\u5f00\u59cb\u7f16\u7a0b\u7684\u65f6\u5019\uff0c\u901a\u5e38\u4f60\u53ea\u6709\u4e00\u4e9b\u5982\u4f55\u7f16\u5199\u8be5\u7a0b\u5e8f\u7684\u57fa\u672c\u7684\u60f3\u6cd5\uff0c\u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u5bf9\u5982\u4f55\u5c06\u60f3\u6cd5\u8f6c\u53d8\u4e3a\u4ee3\u7801\u611f\u5230\u4e0d\u77e5\u6240\u63aa\u3002\u5b9e\u9645\u4e0a\uff0c\u5b8c\u6210\u8fd9\u4ef6\u4e8b\u6240\u9700\u8981\u7684\u6280\u80fd\u4e0e\u4f60\u5728\u751f\u6d3b\u4e2d\u5b66\u5230\u7684\u89e3\u51b3\u95ee\u9898\u7684\u6280\u80fd\u5e76\u65e0\u5dee\u522b\u3002 \u6700\u9700\u8981\u65b0\u624b\u7a0b\u5e8f\u5458\u4eec\u8bb0\u4f4f\u7684\u662f\uff08\u4e5f\u662f\u6700\u96be\u505a\u5230\u7684\u662f\uff09\u5728\u5f00\u59cb \u7f16\u5199 \u4ee3\u7801\u524d\uff0c\u4e00\u5b9a\u8981\u5148 \u8bbe\u8ba1 \u4f60\u7684\u7a0b\u5e8f\u3002\u7f16\u7a0b\u548c\u76d6\u623f\u5b50\u5728\u5f88\u591a\u65b9\u9762\u90fd\u5f88\u76f8\u50cf\u3002\u60f3\u8c61\u4e00\u4e0b\uff0c\u6211\u4eec\u4f1a\u5728\u6ca1\u6709\u56fe\u7eb8\u7684\u60c5\u51b5\u4e0b\u5c31\u5f00\u59cb\u76d6\u623f\u5b50\u5417\uff1f\u5f88\u53ef\u80fd\u4e0d\u4f1a\uff0c\u9664\u975e\u4f60\u662f\u4e2a\u5929\u624d\uff0c\u4e0d\u7136\u4f60\u76d6\u51fa\u6765\u7684\u623f\u5b50\u4e00\u5b9a\u95ee\u9898\u4e0d\u5c11\uff1a\u5899\u662f\u659c\u7684\u3001\u9876\u662f\u6f0f\u7684\u7b49\u7b49\u3002\u540c\u6837\u7684\uff0c\u5982\u679c\u6beb\u65e0\u8ba1\u5212\u5c31\u5f00\u59cb\u7f16\u7a0b\uff0c\u5199\u51fa\u6765\u7684\u4ee3\u7801\u4e5f\u4e00\u5b9a\u662f\u95ee\u9898\u767e\u51fa\uff0c\u7136\u540e\u4f60\u5c31\u9700\u8981\u82b1\u8d39\u5927\u91cf\u7684\u65f6\u95f4\u53bb\u4fee\u590d\u90a3\u4e9b\u4f60\u672c\u6765\u5c31\u53ef\u4ee5\u907f\u514d\u7684\u95ee\u9898\u3002 \u5728\u5f00\u59cb\u524d\u7a0d\u52a0\u8ba1\u5212\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u8282\u7ea6\u65f6\u95f4\uff0c\u8fd8\u80fd\u907f\u514d\u5f88\u591a\u8ba9\u4eba\u611f\u5230\u6cae\u4e27\u7684\u65f6\u523b\u3002 \u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u5411\u4f60\u4ecb\u7ecd\u4e00\u79cd\u5c06\u60f3\u6cd5\u8f6c\u53d8\u4e3a\u7b80\u5355\u7684\u3001\u53ef\u7528\u7684\u7a0b\u5e8f\u7684\u57fa\u672c\u65b9\u6cd5\u3002 \u8bbe\u8ba1\u6b65\u9aa41\uff1a\u5b9a\u76ee\u6807 \u4e3a\u4e86\u7f16\u5199\u4e00\u4e2a\u6210\u529f\u7684\u7a0b\u5e8f\uff0c\u4f60\u5fc5\u987b\u8981\u9996\u5148\u660e\u786e\u4f60\u7684\u76ee\u6807\u662f\u4ec0\u4e48\u3002\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u8be5\u76ee\u6807\u5e94\u8be5\u7528\u4e00\u4e24\u53e5\u8bdd\u5c31\u53ef\u4ee5\u63cf\u8ff0\u3002\u901a\u5e38\u6765\u8bb2\uff0c\u4ece\u7528\u6237\u7684\u89d2\u5ea6\u6765\u63cf\u8ff0\u7f16\u7a0b\u7684\u7ed3\u679c\u662f\u5f88\u6709\u7528\u7684\uff0c\u4f8b\u5982\uff1a \u5141\u8bb8\u7528\u6237\u53ef\u4ee5\u7ba1\u7406\u4e00\u4e9b\u5217\u7684\u59d3\u540d\u5e76\u5c06\u5176\u5173\u8054\u5230\u7535\u8bdd\u53f7\u7801\uff1b \u968f\u673a\u751f\u6210\u770b\u4e0a\u53bb\u5f88\u9177\u7684\u6d1e\u7a74\u573a\u666f\u4ee5\u4f5c\u4e3a\u6e38\u620f\u4e2d\u7684\u5730\u7262\u6765\u4f7f\u7528\uff1b \u751f\u6210\u4e00\u4e2a\u9ad8\u80a1\u606f\u80a1\u7968\u7684\u63a8\u8350\u5217\u8868\uff1b \u5bf9\u4e00\u4e2a\u7403\u4ece\u9ad8\u5854\u6389\u843d\u5230\u5730\u9762\u6240\u9700\u7684\u65f6\u95f4\u8fdb\u884c\u5efa\u6a21\u3002 \u5c3d\u7ba1\u8fd9\u4e00\u6b65\u6240\u505a\u7684\u4e8b\u60c5\u770b\u4e0a\u53bb\u90fd\u662f\u5f88\u663e\u7136\u7684\uff0c\u5b83\u7684\u4f5c\u7528\u5374\u4e0d\u5bb9\u5ffd\u89c6\u3002\u5bf9\u4e8e\u7f16\u7a0b\u6765\u8bf4\uff0c\u6700\u7cdf\u7cd5\u7684\u83ab\u8fc7\u4e8e\u4f60\u5199\u5b8c\u7684\u7a0b\u5e8f\u5374\u4e0d\u662f\u4f60\u60f3\u8981\u7684\uff08\u6216\u8005\u4f60\u8001\u677f\u60f3\u8981\u7684\uff09\uff01 \u8bbe\u8ba1\u6b65\u9aa42\uff1a\u5b9a\u9700\u6c42 \u5c3d\u7ba1\uff0c\u5b9a\u4e49\u95ee\u9898\u6709\u52a9\u4e8e\u4f60\u786e\u5b9a\u4f60\u671f\u671b\u7684\u7ed3\u679c\u662f\u58f0\u660e\uff0c\u4f46\u76ee\u6807\u4ecd\u7136\u662f\u4e0d\u6e05\u695a\u7684\u3002\u63a5\u4e0b\u6765\u4e00\u6b65\u5c31\u662f\u8003\u8651\u5982\u4f55\u8bbe\u8ba1\u9700\u6c42\u3002 \u9700\u6c42\u4e00\u65b9\u9762\u6307\u7684\u662f\u4f60\u80fd\u591f\u4f7f\u7528\u7684\u6709\u9650\u8d44\u6e90\uff08\u4f8b\u5982\uff0c\u9884\u7b97\u3001\u5de5\u671f\u3001\u7a7a\u95f4\u548c\u5185\u5b58\u7b49\u7b49\uff09\uff0c\u53e6\u4e00\u65b9\u9762\u6307\u7684\u662f\u4f60\u7684\u7a0b\u5e8f\u5fc5\u987b\u5177\u5907\u7684\u80fd\u591f\u6ee1\u8db3\u7528\u6237\u9700\u8981\u7684\u80fd\u529b\u3002\u6ce8\u610f\uff0c\u9700\u6c42\u5173\u6ce8\u7684\u4ecd\u7136\u662f \u4ec0\u4e48 \uff0c\u800c\u4e0d\u662f \u5982\u4f55 \u3002 \u4f8b\u5982\uff1a \u7535\u8bdd\u53f7\u7801\u5e94\u8be5\u80fd\u591f\u88ab\u50a8\u5b58\uff0c\u8fd9\u6837\u4ee5\u540e\u53ef\u4ee5\u7528\u6765\u8fdb\u884c\u56de\u62e8\uff1b \u968f\u673a\u7684\u5730\u7262\u5e94\u8be5\u603b\u662f\u5305\u542b\u4e00\u6761\u4ece\u5165\u53e3\u5230\u51fa\u53e3\u7684\u8def\uff1b \u80a1\u7968\u63a8\u8350\u5e94\u8be5\u53c2\u8003\u5386\u53f2\u4ef7\u683c\u4fe1\u606f\uff1b \u7528\u6237\u5e94\u8be5\u80fd\u591f\u8f93\u5165\u9ad8\u5854\u7684\u9ad8\u5ea6\uff1b \u6211\u4eec\u9700\u8981\u5728\u4e03\u5929\u5185\u5b8c\u6210\u53ef\u4f9b\u6d4b\u8bd5\u7684\u7248\u672c\uff1b \u7a0b\u5e8f\u9700\u8981\u572810\u79d2\u5185\u5b8c\u6210\u7528\u6237\u7684\u8bf7\u6c42\uff1b \u7a0b\u5e8f\u5728\u7528\u6237\u4f7f\u7528\u65f6\u5d29\u6e83\u9000\u51fa\u7684\u6982\u7387\u5e94\u8be5\u5c0f\u4e8e 0.1%\u3002 \u4e00\u4e2a\u95ee\u9898\u53ef\u80fd\u4f1a\u6d3e\u751f\u51fa\u5f88\u591a\u9700\u6c42\uff0c\u53ea\u6709\u6240\u6709\u7684\u9700\u6c42\u90fd\u88ab\u6ee1\u8db3\uff0c\u624d\u80fd\u8bf4\u89e3\u51b3\u65b9\u6848\u88ab\u5b9e\u73b0\u4e86\u3002 \u8bbe\u8ba1\u6b65\u9aa43\uff1a\u5b9a\u4e49\u5de5\u5177\u3001\u76ee\u6807\u4ea7\u7269\u4ee5\u53ca\u5907\u4efd\u8ba1\u5212 \u5982\u679c\u4f60\u5df2\u7ecf\u662f\u4e00\u4e2a\u6709\u7ecf\u9a8c\u7684\u7a0b\u5e8f\u5458\u4e86\uff0c\u5728\u8bbe\u8ba1\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u53ef\u80fd\u8fd8\u9700\u8981\u8003\u8651\u5176\u4ed6\u7684\u4e00\u4e9b\u95ee\u9898\uff0c\u4f8b\u5982\uff1a \u5b9a\u4e49\u7a0b\u5e8f\u9700\u8981\u8fd0\u884c\u7684\u5e73\u53f0\u7684\u4f53\u7cfb\u7ed3\u6784\u548c\uff08\u6216\uff09\u64cd\u4f5c\u7cfb\u7edf\uff1b \u786e\u5b9a\u4f60\u9700\u8981\u4f7f\u7528\u54ea\u4e9b\u5de5\u5177\uff1b \u786e\u5b9a\u4f60\u662f\u72ec\u7acb\u5b8c\u6210\u4ee3\u7801\u7f16\u5199\u8fd8\u662f\u4ee5\u56e2\u961f\u5408\u4f5c\u7684\u65b9\u5f0f\u5b8c\u6210\uff1b \u5b9a\u4e49\u6d4b\u8bd5\u3001\u53cd\u9988\u548c\u91ca\u653e\u7b56\u7565\uff1b \u5b9a\u4e49\u5982\u4f55\u5907\u4efd\u4ee3\u7801\u3002 \u4e0d\u8fc7\uff0c\u4f5c\u4e3a\u65b0\u624b\u7a0b\u5e8f\u5458\uff0c\u4e0a\u9762\u51e0\u4e2a\u95ee\u9898\u5176\u5b9e\u90fd\u5f88\u7b80\u5355\uff1a\u4f60\u5c06\u4f1a\u72ec\u7acb\u5730\u5728\u4f60\u81ea\u5df1\u7684\u7535\u8111\u4e0a\u7f16\u5199\u4ee3\u7801\uff0c\u6240\u9700\u7684\u5de5\u5177\u5c31\u662f\u4f60\u4e0b\u8f7d\u5e76\u5b89\u88c5\u4e86\u7684IDE\uff0c\u4f60\u7f16\u5199\u7684\u7a0b\u5e8f\u9664\u4e86\u4f60\u81ea\u5df1\u6ca1\u6709\u5176\u4ed6\u4eba\u4f1a\u4f7f\u7528\u3002 \u5373\u4fbf\u5982\u6b64\uff0c\u5982\u679c\u4f60\u7f16\u5199\u7684\u4ee3\u7801\u4e0d\u662f\u5be5\u5be5\u51e0\u884c\uff0c\u4f60\u8fd8\u662f\u5e94\u8be5\u8003\u8651\u4e00\u4e0b\u5982\u4f55\u5907\u4efd\u4f60\u7684\u4ee3\u7801\u3002\u5c06\u4ee3\u7801\u62f7\u8d1d\u6216\u8005\u538b\u7f29\u540e\u653e\u5230\u7535\u8111\u7684\u5176\u4ed6\u76ee\u5f55\u4e2d\u662f\u4e0d\u591f\u7684\uff08\u5c3d\u7ba1\u804a\u80dc\u4e8e\u65e0\uff09\u3002\u5982\u679c\u4f60\u7684\u7535\u8111\u574f\u4e86\uff0c\u4f60\u7f16\u5199\u7684\u4ee3\u7801\u4e5f\u5c31\u4ed8\u4e4b\u4e1c\u6d41\u4e86\u3002\u4e00\u4e2a\u597d\u7684\u5907\u4efd\u7b56\u7565\uff0c\u5e94\u8be5\u662f\u628a\u4ee3\u7801\u5168\u90e8\u62f7\u8d1d\u4e00\u4efd\u540e\u62f7\u5230\u5176\u4ed6\u7535\u8111\u6216\u5176\u4ed6\u5730\u65b9\u3002\u6709\u5f88\u591a\u529e\u6cd5\u53ef\u4ee5\u5b9e\u73b0\uff0c\u4f8b\u5982\u5c06\u5176\u538b\u7f29\u4e3aZip\u6587\u4ef6\u540e\u53d1\u9001\u5230\u4f60\u81ea\u5df1\u7684\u90ae\u7bb1\uff0c\u62f7\u8d1d\u5230Dropbox\u6216\u8005\u7f51\u76d8\uff0c\u6216\u8005\u901a\u8fc7FTP\u4f20\u5230\u5176\u4ed6\u7535\u8111\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5c40\u57df\u7f51\u62f7\u5230\u5176\u4ed6\u7535\u8111\u4e0a\uff0c\u6216\u8005\u4f7f\u7528\u4f4d\u4e8e\u5176\u4ed6\u7535\u8111\u6216\u4e91\u7aef\u7684\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff08\u4f8b\u5982\uff1aGithub\uff09\u3002\u7248\u672c\u63a7\u5236\u7cfb\u7edf\u9664\u4e86\u5b58\u50a8\u6587\u4ef6\u4ee5\u5916\uff0c\u8fd8\u6709\u5f88\u591a\u5176\u4ed6\u6709\u7528\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u5b83\u53ef\u4ee5\u5c06\u4f60\u7684\u4ee3\u7801\u56de\u9000\u5230\u4e4b\u524d\u7684\u7248\u672c\u3002 \u8bbe\u8ba1\u6b65\u9aa44\uff1a\u5c06\u590d\u6742\u95ee\u9898\u5207\u5206\u4e3a\u591a\u4e2a\u7b80\u5355\u95ee\u9898 \u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\uff0c\u6211\u4eec\u65f6\u5e38\u9700\u8981\u5b8c\u6210\u4e00\u4e9b\u975e\u5e38\u590d\u6742\u7684\u95ee\u9898\u3002\u627e\u5230\u7684\u95ee\u9898\u7684\u89e3\u51b3\u529e\u6cd5\uff0c\u901a\u5e38\u5e76\u4e0d\u5bb9\u6613\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528 \u81ea\u9876\u5411\u4e0b(top down) \u7684\u529e\u6cd5\u6765\u89e3\u51b3\u95ee\u9898\u3002\u4e3a\u4e86\u89e3\u51b3\u4e00\u4e2a\u7279\u522b\u590d\u6742\u7684\u95ee\u9898\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u628a\u8fd9\u4e2a\u95ee\u9898\u5206\u89e3\u4e3a\u591a\u4e2a\u76f8\u5bf9\u6765\u8bf4\u66f4\u5bb9\u6613\u89e3\u51b3\u7684\u5b50\u95ee\u9898\u3002\u5982\u679c\u5b50\u95ee\u9898\u4ecd\u7136\u96be\u4ee5\u89e3\u51b3\uff0c\u5b83\u4eec\u8fd8\u53ef\u4ee5\u88ab\u8fdb\u4e00\u6b65\u5206\u5272\u4e3a\u66f4\u591a\u7684\u5b50\u95ee\u9898\u3002\u901a\u8fc7\u4e0d\u65ad\u5730\u5c06\u590d\u6742\u95ee\u9898\u5206\u89e3\u6210\u7b80\u5355\u7684\u5b50\u95ee\u9898\uff0c\u4f60\u6700\u7ec8\u53ef\u4ee5\u5f97\u5230\u5f88\u591a\u66f4\u5bb9\u6613\u88ab\u5904\u7406\u7684\u5b50\u95ee\u9898\u3002 \u4e3e\u4f8b\u6765\u8bf4\uff0c\u5982\u679c\u6211\u4eec\u9700\u8981\u6253\u626b\u5c4b\u5b50\uff0c\u90a3\u4e48\u5206\u89e3\u4efb\u52a1\u7684\u7ed3\u6784\u53ef\u80fd\u4f1a\u662f\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a \u6253\u626b\u5c4b\u5b50 \u6253\u626b\u5c4b\u5b50\u662f\u4e00\u4e2a\u5f88\u5927\u7684\u4efb\u52a1\uff0c\u8ba9\u6211\u4eec\u5c06\u5176\u5206\u89e3\u4e3a\u591a\u4e2a\u5b50\u4efb\u52a1\uff1a \u6253\u626b\u5c4b\u5b50 \u6e05\u7406\u5730\u6bef \u6253\u626b\u536b\u751f\u95f4 \u6253\u626b\u53a8\u623f \u6bd4\u4e4b\u524d\u5bb9\u6613\u5904\u7406\u4e86\u4e00\u4e9b\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u9010\u9879\u89e3\u51b3\u8fd9\u4e9b\u5b50\u95ee\u9898\u4e86\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u5176\u5b9e\u8fd8\u80fd\u5c06\u8fd9\u4e9b\u4efb\u52a1\u7ee7\u7eed\u5206\u89e3\uff1a \u6253\u626b\u5c4b\u5b50 \u6e05\u7406\u5730\u6bef \u6253\u626b\u536b\u751f\u95f4 \u6e05\u6d17\u9a6c\u6876 \u6e05\u7406\u6c34\u6c60 \u6253\u626b\u53a8\u623f \u6e05\u7406\u7076\u53f0 \u6253\u626b\u7076\u53f0 \u6e05\u6d17\u6c34\u6c60 \u5012\u5783\u573e \u73b0\u5728\uff0c\u6211\u4eec\u9762\u5bf9\u7684\u662f\u4e00\u7cfb\u5217\u5177\u6709\u5c42\u6b21\u7ed3\u6784\u7684\u95ee\u9898\u4e86\uff0c\u800c\u4e14\u5b83\u4eec\u4e2d\u4efb\u4f55\u4e00\u9879\u90fd\u4e0d\u590d\u6742\u3002\u901a\u8fc7\u5b8c\u6210\u8fd9\u4e9b\u76f8\u5bf9\u66f4\u5bb9\u6613\u5904\u7406\u7684\u5b50\u4efb\u52a1\u4e4b\u540e\uff0c\u6211\u4eec\u4e5f\u5c31\u5b8c\u6210\u4e86\u6253\u626b\u5c4b\u5b50\u8fd9\u4e2a\u66f4\u590d\u6742\u7684\u95ee\u9898\u3002 \u53e6\u5916\u4e00\u79cd\u5206\u89e3\u4efb\u52a1\u7684\u65b9\u5f0f\u79f0\u4e3a \u81ea\u5e95\u5411\u4e0a(bottom) \u65b9\u6cd5\u3002\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u65f6\uff0c\u6211\u4eec\u4f1a\u4ece\u4e00\u7cfb\u5217\u7b80\u5355\u7684\u4efb\u52a1\u5f00\u59cb\uff0c\u7136\u540e\u901a\u8fc7\u5408\u5e76\u5206\u7ec4\u7684\u65b9\u5f0f\u6784\u5efa\u4efb\u52a1\u5217\u8868\u3002 \u5927\u591a\u6570\u4eba\u5e73\u65f6\u4e0d\u662f\u4e0a\u5b66\u5c31\u662f\u4e0a\u73ed\uff0c\u90a3\u4e48\u5c31\u4ee5\u6b64\u6765\u4e3e\u4f8b\uff0c\u5047\u8bbe\u6211\u4eec\u8981\u89e3\u51b3\u7684\u662f\u201c\u53bb\u4e0a\u73ed\u201d\u8fd9\u4e2a\u95ee\u9898\uff0c\u5982\u679c\u6709\u4eba\u95ee\u4f60\uff0c\u4f60\u65e9\u4e0a\u51fa\u95e8\u524d\u90fd\u505a\u54ea\u4e9b\u4e8b\u5462\uff1f\u4f60\u7684\u56de\u7b54\u53ef\u80fd\u5305\u542b\u4e0b\u9762\u8fd9\u4e9b\u4efb\u52a1\uff1a \u62ff\u51fa\u8981\u7a7f\u7684\u8863\u670d \u7a7f\u8863 \u5403\u65e9\u996d \u5f00\u8f66\u53bb\u516c\u53f8 \u5237\u7259 \u8d77\u5e8a \u505a\u65e9\u9910 \u4e0a\u8f66 \u6d17\u6fa1 \u4f7f\u7528\u81ea\u5e95\u5411\u4e0a\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u5b83\u4eec\u7684\u76f8\u4f3c\u6027\uff0c\u4e3a\u8fd9\u4e9b\u4efb\u52a1\u521b\u5efa\u4e00\u4e2a\u5c42\u6b21\u7ed3\u6784\uff1a \u4ece\u8d77\u5e8a\u5230\u4e0a\u73ed\u8981\u505a\u7684\u4e8b \u5367\u5ba4\u8981\u505a\u7684\u4e8b \u8d77\u5e8a \u62ff\u8863\u670d \u7a7f\u8863 \u536b\u751f\u95f4\u8981\u505a\u7684\u4e8b \u6d17\u6fa1 \u5237\u7259 \u65e9\u9910\u76f8\u5173 \u51c6\u5907\u9ea6\u7247 \u5403\u9ea6\u7247 \u901a\u52e4\u76f8\u5173 \u4e0a\u8f66 \u5f00\u8f66\u5230\u516c\u53f8 \u5176\u5b9e\u6211\u4eec\u4e0d\u96be\u770b\u51fa\uff0c\u8fd9\u4e9b\u5c42\u6b21\u7ed3\u6784\u5bf9\u4e8e\u7f16\u7a0b\u6765\u8bf4\u662f\u975e\u5e38\u6709\u7528\u7684\uff0c\u56e0\u4e3a\u4e00\u65e6\u4efb\u52a1\u7684\u7ed3\u6784\u6e05\u695a\u4e86\uff0c\u7a0b\u5e8f\u7684\u5927\u4f53\u7ed3\u6784\u5176\u5b9e\u4e5f\u5df2\u7ecf\u6e05\u695a\u4e86\u3002\u6700\u4e0a\u5c42\u7684\u4efb\u52a1\uff08\u6253\u626b\u5c4b\u5b50\u6216\u53bb\u4e0a\u73ed\uff09\u5c31\u53ef\u4ee5\u662f\u7a0b\u5e8f\u7684 main() \u51fd\u6570\uff08\u56e0\u4e3a\u5b83\u662f\u4f60\u8981\u89e3\u51b3\u7684\u4e3b\u8981(main)\u95ee\u9898\uff09\u3002\u800c\u5176\u4ed6\u5b50\u9879\u76ee\u5219\u53ef\u4ee5\u5b9e\u73b0\u4e3a\u4e0d\u540c\u7684\u51fd\u6570\u3002 \u5982\u679c\u5176\u4e2d\u67d0\u4e00\u9879\u4efb\u52a1\uff08\u51fd\u6570\uff09\u5728\u5b9e\u73b0\u65f6\u4ecd\u7136\u8fc7\u4e8e\u56f0\u96be\uff0c\u5219\u53ef\u4ee5\u7ee7\u7eed\u5c06\u5176\u5206\u89e3\u4e3a\u51e0\u4e2a\u5b50\u95ee\u9898\u3002\u6700\u7ec8\u4f60\u5c31\u53ef\u4ee5\u5c06\u7a0b\u5e8f\u5206\u5272\u4e3a\u591a\u4e2a\u5bb9\u6613\u5b9e\u73b0\u7684\u51fd\u6570\u3002 \u8bbe\u8ba1\u6b65\u9aa45\uff1a\u786e\u5b9a\u76f8\u5173\u4e8b\u4ef6\u53d1\u751f\u7684\u987a\u5e8f \u73b0\u5728\uff0c\u7a0b\u5e8f\u7684\u7ed3\u6784\u5df2\u7ecf\u6e05\u695a\u4e86\uff0c\u63a5\u4e0b\u6765\u9700\u8981\u5c06\u8fd9\u4e9b\u4efb\u52a1\u5173\u8054\u5176\u4ed6\u3002\u9996\u5148\uff0c\u6211\u4eec\u8981\u786e\u5b9a\u8fd9\u4e9b\u8981\u4efb\u52a1\u7684\u53d1\u751f\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u4f60\u65e9\u4e0a\u8d77\u6765\uff0c\u5b8c\u6210\u4e0a\u9762\u90a3\u4e9b\u4efb\u52a1\u7684\u987a\u5e8f\u662f\u4ec0\u4e48\uff1f\u5dee\u4e0d\u591a\u5e94\u8be5\u662f\u8fd9\u6837\u7684\u5427\uff1a \u5367\u5ba4\u8981\u505a\u7684\u4e8b \u536b\u751f\u95f4\u8981\u505a\u7684\u4e8b \u65e9\u9910\u76f8\u5173 \u901a\u52e4\u76f8\u5173 \u5982\u679c\u4f60\u8981\u7f16\u5199\u4e00\u4e2a\u8ba1\u7b97\u5668\u7a0b\u5e8f\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u6309\u7167\u4e0b\u9762\u6b65\u9aa4\u6765\u505a\uff1a \u4ece\u7528\u6237\u83b7\u53d6\u7b2c\u4e00\u4e2a\u6570 \u4ece\u7528\u6237\u83b7\u53d6\u9700\u8981\u6267\u884c\u7684\u8fd0\u7b97\u64cd\u4f5c \u4ece\u7528\u6237\u83b7\u53d6\u7b2c\u4e8c\u4e2a\u6570 \u8ba1\u7b97\u7ed3\u679c \u6253\u5370\u7ed3\u679c \u5b8c\u6210\u4e0a\u8ff0\u8bbe\u8ba1\u5de5\u4f5c\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f00\u59cb\u5b9e\u73b0\u4ee3\u7801\u4e86\uff01 \u5b9e\u73b0\u6b65\u9aa41\uff1a\u7f16\u5199main\u51fd\u6570\u6846\u67b6 \u73b0\u5728\u53ef\u4ee5\u5f00\u59cb\u5b9e\u73b0\u4ee3\u7801\u4e86\u3002\u6839\u636e\u4e0a\u9762\u987a\u5e8f\u7ed3\u6784\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u5b8c\u6210 main \u51fd\u6570\u7684\u6846\u67b6\uff0c\u6682\u65f6\u65e0\u9700\u62c5\u5fc3\u8f93\u5165\u8f93\u51fa\u7684\u95ee\u9898\u3002 1 2 3 4 5 6 7 8 9 int main () { // doBedroomThings(); // doBathroomThings(); // doBreakfastThings(); // doTransportationThings(); return 0 ; } \u6216\u8005\u5bf9\u4e8e\u8ba1\u7b97\u5668\u4f8b\u5b50\u6765\u8bf4\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 int main () { // Get first number from user // getUserInput(); // Get mathematical operation from user // getMathematicalOperation(); // Get second number from user // getUserInput(); // Calculate result // calculateResult(); // Print result // printResult(); return 0 ; } \u6ce8\u610f\uff0c\u5982\u679c\u4f60\u4e5f\u4f7f\u7528\u8fd9\u79cd\u201c\u5927\u7eb2\u6846\u67b6\u201d\u7684\u65b9\u6cd5\u6765\u6784\u5efa\u7a0b\u5e8f\uff0c\u8fd9\u91cc\u6d89\u53ca\u7684\u51fd\u6570\u90fd\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\uff0c\u6bd5\u7adf\u5b83\u4eec\u8fd8\u6ca1\u6709\u88ab\u5b9a\u4e49\u3002\u5728\u51fd\u6570\u88ab\u5b9e\u9645\u5b9a\u4e49\u524d\u5148\u5c06\u5176\u6682\u65f6\u5148\u6ce8\u91ca\u6389\u662f\u4e00\u79cd\u65b9\u6cd5\uff08\u4e0a\u9762\u5c55\u793a\u7684\u5c31\u662f\u8fd9\u79cd\u65b9\u6cd5\uff09\u3002\u6b64\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u5bf9\u51fd\u6570\u8fdb\u884c \u6253\u6869(stub) \uff0c\u5373\u4e3a\u7a0b\u5e8f\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u51fd\u6570\u4f53\uff0c\u4ee5\u4fbf\u7a0b\u5e8f\u53ef\u4ee5\u7f16\u8bd1\u901a\u8fc7\u3002 \u5b9e\u73b0\u6b65\u9aa42\uff1a\u5b9e\u73b0\u6bcf\u4e2a\u51fd\u6570 \u5728\u8fd9\u4e00\u6b65\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u51fd\u6570\u90fd\u9700\u8981\u505a\u4e09\u4ef6\u4e8b\uff1a \u5b9a\u4e49\u51fd\u6570\u7684\u539f\u578b\uff08\u8f93\u5165\u8f93\u51fa\uff09\uff1b \u7f16\u5199\u51fd\u6570\uff1b \u6d4b\u8bd5\u51fd\u6570\u3002 \u5982\u679c\u4f60\u7684\u51fd\u6570\u7c92\u5ea6\u8db3\u591f\u7ec6\uff0c\u90a3\u4e48\u6bcf\u4e2a\u51fd\u6570\u90fd\u5e94\u8be5\u662f\u975e\u5e38\u7b80\u5355\u548c\u76f4\u63a5\u7684\u3002\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u5b9e\u73b0\u8d77\u6765\u975e\u5e38\u590d\u6742\uff0c\u90a3\u4e48\u5f88\u53ef\u80fd\u5b83\u8fd8\u9700\u8981\u88ab\u8fdb\u4e00\u6b65\u5206\u5272\u4e3a\u591a\u4e2a\u66f4\u5bb9\u6613\u5b9e\u73b0\u7684\u5b50\u51fd\u6570\uff08 \u4e5f\u53ef\u80fd\u662f\u4f60\u7684\u4efb\u52a1\u987a\u5e8f\u6709\u95ee\u9898\uff0c\u4f60\u5e94\u8be5\u91cd\u65b0\u5ba1\u89c6\u4e00\u4e0b\u5404\u4e2a\u6b65\u9aa4\u7684\u987a\u5e8f \uff09\u3002 \u63a5\u4e0b\u6765\uff0c\u5b9e\u73b0\u8ba1\u7b97\u5668\u7a0b\u5e8f\u4e2d\u7684\u7b2c\u4e00\u4e2a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include // Full implementation of the getUserInput function int getUserInput () { std :: cout << \"Enter an integer \" ; int input {}; std :: cin >> input ; return input ; } int main () { // \u4ece\u7528\u6237\u83b7\u53d6\u7b2c\u4e00\u4e2a\u8f93\u5165 int value { getUserInput () }; // \u6ce8\u610f\uff0c\u8fd9\u91cc\u7684\u4ee3\u7801\u672c\u8eab\u5c31\u5177\u5907\u6d4b\u8bd5\u51fd\u6570\u8f93\u51fa\u7684\u529f\u80fd std :: cout << value ; // \u7528\u4e8e\u786e\u4fdd getUserInput() \u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u7684\u8c03\u8bd5\u4ee3\u7801\uff0c\u7a0d\u540e\u9700\u8981\u79fb\u9664 // Get mathematical operation from user // getMathematicalOperation(); // Get second number from user // getUserInput(); // Calculate result // calculateResult(); // Print result // printResult(); return 0 ; } \u9996\u5148\uff0c\u786e\u5b9a\u51fd\u6570 getUserInput \u4e0d\u9700\u8981\u63a5\u53d7\u4efb\u4f55 \u5b9e\u53c2 \u3002\u540c\u65f6\uff0c\u8be5\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u53cd\u6620\u5230\u51fd\u6570\u539f\u578b\u4e0a\uff0c\u5373\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a int \uff0c\u540c\u65f6\u6ca1\u6709 \u5f62\u53c2 \u3002\u63a5\u4e0b\u6765\uff0c\u7f16\u5199\u51fd\u6570\u4f53\u3002\u51fd\u6570\u4f53\u975e\u5e38\u76f4\u63a5\uff0c\u53ea\u5305\u542b\u56db\u6761\u8bed\u53e5 \u8bed\u53e5 \u3002\u6700\u540e\uff0c\u5728 main \u51fd\u6570\u4e2d\u7f16\u5199\u4e00\u4e9b\u7528\u4e8e\u6d4b\u8bd5\u51fd\u6570 getUserInput \u529f\u80fd\uff08\u5305\u62ec\u5176\u8fd4\u56de\u503c) \u7684\u4e34\u65f6\u4ee3\u7801\uff0c\u786e\u5b9a\u51fd\u6570\u662f\u5426\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u3002 \u591a\u6b21\u8fd0\u884c\u7a0b\u5e8f\u5e76\u4f7f\u7528\u4e0d\u540c\u7684\u8f93\u5165\u503c\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7ed3\u679c\u90fd\u7b26\u5408\u9884\u671f\u3002\u5982\u679c\u53d1\u73b0\u7a0b\u5e8f\u4e0d\u80fd\u6b63\u786e\u5de5\u4f5c\uff0c\u5219\u95ee\u9898\u53ef\u80fd\u5c31\u51fa\u5728\u521a\u624d\u7f16\u5199\u7684\u4ee3\u7801\u4e2d\u3002 \u4e00\u65e6\u4f60\u786e\u5b9a\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\uff0c\u90a3\u4e48\u8fd9\u4e9b\u4e34\u65f6\u6d4b\u8bd5\u4ee3\u7801\u5c31\u53ef\u4ee5\u88ab\u79fb\u9664\u4e86\uff0c\u7136\u540e\u5f00\u59cb\u5b9e\u73b0\u4e0b\u4e00\u4e2a\u51fd\u6570\uff08 getMathematicalOperation \uff09\u3002\u6211\u4eec\u5e76\u4e0d\u4f1a\u5728\u672c\u8282\u8bfe\u4e2d\u5b9e\u73b0\u6240\u6709\u51fd\u6570\uff0c\u56e0\u4e3a\u8fd8\u6709\u4e00\u4e9b\u66f4\u52a0\u91cd\u8981\u7684\u8bdd\u9898\u9700\u8981\u8ba8\u8bba\u3002 \u8bb0\u4f4f\uff1a\u4e0d\u8981\u671f\u671b\u4e00\u6b21\u6027\u5b9e\u73b0\u5168\u90e8\u7684\u4ee3\u7801\u3002\u4e00\u6b65\u4e00\u6b65\u5b8c\u6210\uff0c\u5e76\u4e14\u6bcf\u4e00\u6b65\u90fd\u8981\u8fdb\u884c\u6d4b\u8bd5\u3002 \u5b9e\u73b0\u6b65\u9aa43\uff1a\u6700\u7ec8\u6d4b\u8bd5 \u5f53\u7a0b\u5e8f\u7f16\u5199\u201c\u5b8c\u6210\u201d\u540e\uff0c\u6700\u540e\u4e00\u6b65\u5219\u662f\u5bf9\u5b8c\u6574\u7684\u7a0b\u5e8f\u8fdb\u884c\u6d4b\u8bd5\uff0c\u786e\u5b9a\u5176\u884c\u4e3a\u662f\u5426\u6ee1\u8db3\u9884\u671f\u3002\u5982\u679c\u4e0d\u6ee1\u8db3\uff0c\u5219\u9700\u8981\u8fdb\u884c\u4fee\u590d\u3002 \u7f16\u7a0b\u5fe0\u8a00 \u5f00\u59cb\u65f6\u4e00\u5207\u4ece\u7b80 \uff1a\u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u5bf9\u5176\u8981\u5b9e\u73b0\u7684\u7a0b\u5e8f\u548c\u529f\u80fd\u5177\u6709\u5b8f\u4f1f\u7684\u84dd\u56fe\uff1a\u201c\u6211\u60f3\u8981\u8bbe\u8ba1\u4e00\u6b3e\u5177\u6709\u56fe\u5f62\u754c\u9762\u3001\u914d\u4e50\u548c\u968f\u673a\u5730\u7262\u7684\u89d2\u8272\u626e\u6f14\u6e38\u620f\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u6e38\u620f\u5730\u56fe\u8fd8\u5305\u62ec\u57ce\u9547\uff0c\u4f60\u53ef\u4ee5\u5728\u57ce\u9547\u91cc\u552e\u5356\u4f60\u6536\u96c6\u5230\u7684\u6218\u5229\u54c1\u201d\u3002\u5982\u679c\u4f60\u4ece\u4e00\u5f00\u59cb\u5c31\u8981\u5b8c\u6210\u4e00\u4e2a\u975e\u5e38\u590d\u6742\u7684\u4efb\u52a1\uff0c\u4f60\u53ef\u80fd\u4f1a\u5728\u8fdb\u5ea6\u4e0d\u4f73\u65f6\u611f\u5230\u4e0d\u77e5\u6240\u63aa\u6216\u5fc3\u7070\u610f\u51b7\u3002\u6240\u4ee5\uff0c\u4f60\u5176\u5b9e\u5e94\u8be5\u5c06\u4f60\u5f00\u59cb\u65f6\u7684\u76ee\u6807\u8bbe\u7684\u5c3d\u53ef\u80fd\u7b80\u5355\uff0c\u5373\u4e00\u4e9b\u4f60\u80af\u5b9a\u80fd\u591f\u5b8c\u6210\u7684\u76ee\u6807\u3002\u4f8b\u5982\uff0c\u201c\u6211\u60f3\u8981\u5728\u5c4f\u5e55\u4e0a\u663e\u793a\u4e00\u4e2a2D\u7684\u533a\u57df\u201d\u3002 \u9010\u6e10\u6dfb\u52a0\u65b0\u7684\u529f\u80fd \uff1a\u5f53\u4e0a\u8ff0\u7b80\u5355\u7684\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u8003\u8651\u4e3a\u5176\u589e\u52a0\u65b0\u7684\u529f\u80fd\u4e86\u3002\u4f8b\u5982\uff0c\u5f53\u4e0a\u8ff02D\u533a\u57df\u53ef\u4ee5\u88ab\u6b63\u5e38\u663e\u793a\u5728\u5c4f\u5e55\u4e0a\u540e\uff0c\u4f60\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u80fd\u591f\u79fb\u52a8\u7684\u89d2\u8272\u3002\u5f53\u89d2\u8272\u53ef\u4ee5\u79fb\u52a8\u540e\uff0c\u6dfb\u52a0\u4e00\u4e9b\u5899\u4f53\u9650\u5236\u89d2\u8272\u7684\u79fb\u52a8\u8def\u5f84\u3002\u5899\u4f53\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5229\u7528\u5899\u4f53\u6765\u6784\u5efa\u4e00\u4e2a\u57ce\u9547\u5730\u56fe\u3002\u57ce\u9547\u5b8c\u6210\u540e\uff0c\u6dfb\u52a0\u4e00\u4e2a\u5546\u4ebaNPC\u3002\u901a\u8fc7\u8fd9\u79cd\u5faa\u5e8f\u6e10\u8fdb\u7684\u65b9\u5f0f\u4e3a\u4f60\u7684\u7a0b\u5e8f\u6dfb\u52a0\u65b0\u529f\u80fd\uff0c\u7a0b\u5e8f\u6700\u7ec8\u4f1a\u53d8\u5f97\u8d8a\u6765\u8d8a\u590d\u6742\uff0c\u540c\u65f6\u4e5f\u4e0d\u4f1a\u8ba9\u4f60\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u611f\u5230\u4e0d\u77e5\u6240\u63aa\u3002 \u4e00\u6b21\u53ea\u505a\u4e00\u4ef6\u4e8b \uff1a\u4e0d\u8981\u671f\u671b\u4e00\u6b21\u6027\u5199\u5b8c\u5168\u90e8\u7684\u4ee3\u7801\uff0c\u4e5f\u4e0d\u8981\u628a\u4f60\u7684\u6ce8\u610f\u529b\u5206\u6563\u5728\u591a\u4e2a\u4efb\u52a1\u4e0a\u3002\u4e00\u6b21\u53ea\u505a\u4e00\u4ef6\u4e8b\u3002\u4e00\u4e2a\u6b63\u5e38\u5de5\u4f5c\u7684\u529f\u80fd+\u4e94\u4e2a\u5c1a\u672a\u5b9e\u73b0\u7684\u529f\u80fd\uff0c\u597d\u8fc7\u516d\u4e2a\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u7684\u529f\u80fd\u3002\u6ce8\u610f\u529b\u88ab\u5206\u6563\u7684\u65f6\u5019\uff0c\u4f60\u4f1a\u66f4\u5bb9\u6613\u72af\u9519\uff0c\u4e5f\u66f4\u5bb9\u6613\u5fd8\u8bb0\u91cd\u8981\u7684\u7ec6\u8282\u3002 \u4e00\u8fb9\u5b9e\u73b0\u3001\u4e00\u8fb9\u6d4b\u8bd5 \uff1a\u65b0\u7684\u7a0b\u5e8f\u5458\u5e38\u5e38\u4f1a\u4e00\u6b21\u6027\u5199\u5b8c\u5168\u90e8\u7684\u7a0b\u5e8f\uff0c\u7136\u540e\u624d\u5bf9\u5176\u8fdb\u884c\u7f16\u8bd1\uff0c\u7136\u540e\u5c31\u201c\u6536\u83b7\u201d\u4e86\u975e\u5e38\u591a\u7684\u62a5\u9519\u4fe1\u606f\u3002\u5199\u5b8c\u7684\u4ee3\u7801\u4e0d\u80fd\u7f16\u8bd1\uff0c\u4e0d\u4ec5\u5413\u4eba\uff0c\u800c\u4e14\u8fd8\u96be\u4ee5\u67e5\u627e\u5176\u4ea7\u751f\u9519\u8bef\u7684\u539f\u56e0\u3002\u6b63\u786e\u7684\u505a\u6cd5\u662f\u7f16\u5199\u4e00\u4e9b\u4ee3\u7801\uff0c\u7136\u540e\u5c31\u5bf9\u5176\u8fdb\u884c\u7f16\u8bd1\u548c\u6d4b\u8bd5\u3002\u5982\u679c\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u90a3\u4e48\u4fee\u590d\u8d77\u6765\u4e5f\u975e\u5e38\u7b80\u5355\u3002\u5f53\u4f60\u786e\u5b9a\u8fd9\u4e9b\u4ee3\u7801\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\uff0c\u518d\u91cd\u590d\u4e0a\u8ff0\u64cd\u4f5c\uff0c\u5f00\u59cb\u7f16\u5199\u63a5\u4e0b\u6765\u7684\u4ee3\u7801\u3002\u8fd9\u4e48\u505a\u6216\u8bb8\u4f1a\u201c\u62d6\u7d2f\u201d\u4f60\u7684\u8fdb\u5ea6\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u5f53\u4f60\u5b8c\u6210\u540e \uff0c\u4ee3\u7801\u5e94\u8be5\u662f\u80fd\u591f\u6b63\u786e\u5de5\u4f5c\u7684\uff0c\u8fd9\u597d\u8fc7\u4f60\u5199\u5b8c\u4ee3\u7801\u540e\u518d\u82b1\u8d39\u4e24\u500d\u591a\u7684\u65f6\u95f4\u53bb\u5b9a\u4f4d\u95ee\u9898\u3002 \u4e0d\u8981\u8fc7\u65e9\u5730\u4f18\u5316\u4ee3\u7801 \uff1a\u7b2c\u4e00\u7248\u529f\u80fd\uff08\u6216\u7a0b\u5e8f\uff09\u5f88\u5c11\u662f\u5b8c\u7f8e\u7684\u3002\u800c\u4e14\uff0c\u7a0b\u5e8f\u672c\u8eab\u4e5f\u4f1a\u968f\u7740\u65f6\u95f4\u4e0d\u65ad\u88ab\u66f4\u65b0\uff0c\u6bd5\u7adf\u4f60\u8fd8\u8981\u4e3a\u5176\u6dfb\u52a0\u529f\u80fd\uff0c\u6216\u8005\u91cd\u65b0\u7ec4\u7ec7\u4ee3\u7801\u3002\u5982\u679c\u4f60\u5728\u65e9\u671f\u5c31\u82b1\u8d39\u5f88\u591a\u65f6\u95f4\u53bb\u6253\u78e8\u8fd9\u4e9b\u4ee3\u7801\uff08\u6dfb\u52a0\u5f88\u591a\u6587\u6863\u3001\u786e\u4fdd\u6ee1\u8db3\u6700\u4f73\u5b9e\u8df5\u3001\u4f18\u5316\u7b49\uff09\uff0c\u90a3\u4e48\u6709\u53ef\u80fd\u4f1a\u56e0\u4e3a\u5fc5\u987b\u4fee\u6539\u4ee3\u7801\u800c\u4f7f\u5f97\u8fd9\u4e9b\u52aa\u529b\u90fd\u4ed8\u4e4b\u4e1c\u6d41\u3002\u6b63\u786e\u7684\u505a\u6cd5\u662f\uff0c\u8ba9\u4f60\u7684\u7a0b\u5e8f\u529f\u80fd\u4fdd\u6301\u6700\u7b80\u53ef\u7528\uff0c\u7136\u540e\u7ee7\u7eed\u5176\u4ed6\u7684\u5de5\u4f5c\u3002\u5f53\u4f60\u5bf9\u7a0b\u5e8f\u611f\u5230\u6ee1\u610f\u65f6\uff0c\u518d\u5bf9\u5176\u8fdb\u884c\u4e00\u7cfb\u5217\u7684\u4f18\u5316\u3002\u4e0d\u8fc7\uff0c\u4e0d\u8981\u671f\u671b\u5176\u8fbe\u5230\u5b8c\u7f8e\u7684\u72b6\u6001\uff0c\u5927\u578b\u7a0b\u5e8f\u5f88\u5c11\u662f\u5b8c\u7f8e\u7684\uff0c\u5b83\u603b\u662f\u6709\u8fd9\u6837\u6216\u90a3\u6837\u7684\u95ee\u9898\u9700\u8981\u6211\u4eec\u4f18\u5316\u3002\u505a\u5230\u8db3\u591f\u597d\u5c31\u53ef\u4ee5\u4e86\u3002 \u5927\u591a\u6570\u65b0\u624b\u7a0b\u5e8f\u5458\u5e76\u4e0d\u80fd\u5f88\u597d\u7684\u9075\u5faa\u4e0a\u8ff0\u5fe0\u8a00\uff08\u56e0\u4e3a\u770b\u4e0a\u53bb\u5f88\u9ebb\u70e6\uff0c\u4e5f\u4e0d\u5982\u5199\u4ee3\u7801\u6709\u8da3\uff09\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u5177\u6709\u4e00\u5b9a\u89c4\u6a21\u7684\u4ee3\u7801\u6765\u8bf4\uff0c\u5b8c\u6574\u9075\u5faa\u4e0a\u8ff0\u5efa\u8bae\u7edd\u5bf9\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8282\u7ea6\u5f88\u591a\u65f6\u95f4\uff08\u4ece\u957f\u671f\u6765\u770b\uff09\u3002\u7b80\u5355\u5730\u8fdb\u884c\u4e00\u4e9b\u8bbe\u8ba1\u89c4\u5212\uff0c\u5c31\u53ef\u4ee5\u4e3a\u540e\u671f\u8282\u7ea6\u5927\u91cf\u7684\u8c03\u8bd5\u65f6\u95f4\u3002 \u597d\u6d88\u606f\u662f\uff0c\u4e00\u65e6\u4f60\u4e60\u60ef\u4e86\u8fd9\u4e48\u505a\u4ee5\u540e\uff0c\u5b83\u4fbf\u4f1a\u6162\u6162\u5730\u6210\u4e3a\u4f60\u7684\u4e60\u60ef\u3002\u6700\u7ec8\uff0c\u4f60\u5c06\u80fd\u591f\u4e0d\u5728\u8fdb\u884c\u4efb\u4f55\u9884\u5148\u8bbe\u8ba1\u7684\u60c5\u51b5\u4e0b\uff0c\u5b8c\u6210\u6574\u4e2a\u51fd\u6570\u7684\u7f16\u5199\u3002","title":"2.13 - \u5982\u4f55\u8bbe\u8ba1\u7b2c\u4e00\u4e2a\u7a0b\u5e8f"},{"location":"2-13-How-to-design-your-first-programs/#1","text":"\u4e3a\u4e86\u7f16\u5199\u4e00\u4e2a\u6210\u529f\u7684\u7a0b\u5e8f\uff0c\u4f60\u5fc5\u987b\u8981\u9996\u5148\u660e\u786e\u4f60\u7684\u76ee\u6807\u662f\u4ec0\u4e48\u3002\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u8be5\u76ee\u6807\u5e94\u8be5\u7528\u4e00\u4e24\u53e5\u8bdd\u5c31\u53ef\u4ee5\u63cf\u8ff0\u3002\u901a\u5e38\u6765\u8bb2\uff0c\u4ece\u7528\u6237\u7684\u89d2\u5ea6\u6765\u63cf\u8ff0\u7f16\u7a0b\u7684\u7ed3\u679c\u662f\u5f88\u6709\u7528\u7684\uff0c\u4f8b\u5982\uff1a \u5141\u8bb8\u7528\u6237\u53ef\u4ee5\u7ba1\u7406\u4e00\u4e9b\u5217\u7684\u59d3\u540d\u5e76\u5c06\u5176\u5173\u8054\u5230\u7535\u8bdd\u53f7\u7801\uff1b \u968f\u673a\u751f\u6210\u770b\u4e0a\u53bb\u5f88\u9177\u7684\u6d1e\u7a74\u573a\u666f\u4ee5\u4f5c\u4e3a\u6e38\u620f\u4e2d\u7684\u5730\u7262\u6765\u4f7f\u7528\uff1b \u751f\u6210\u4e00\u4e2a\u9ad8\u80a1\u606f\u80a1\u7968\u7684\u63a8\u8350\u5217\u8868\uff1b \u5bf9\u4e00\u4e2a\u7403\u4ece\u9ad8\u5854\u6389\u843d\u5230\u5730\u9762\u6240\u9700\u7684\u65f6\u95f4\u8fdb\u884c\u5efa\u6a21\u3002 \u5c3d\u7ba1\u8fd9\u4e00\u6b65\u6240\u505a\u7684\u4e8b\u60c5\u770b\u4e0a\u53bb\u90fd\u662f\u5f88\u663e\u7136\u7684\uff0c\u5b83\u7684\u4f5c\u7528\u5374\u4e0d\u5bb9\u5ffd\u89c6\u3002\u5bf9\u4e8e\u7f16\u7a0b\u6765\u8bf4\uff0c\u6700\u7cdf\u7cd5\u7684\u83ab\u8fc7\u4e8e\u4f60\u5199\u5b8c\u7684\u7a0b\u5e8f\u5374\u4e0d\u662f\u4f60\u60f3\u8981\u7684\uff08\u6216\u8005\u4f60\u8001\u677f\u60f3\u8981\u7684\uff09\uff01","title":"\u8bbe\u8ba1\u6b65\u9aa41\uff1a\u5b9a\u76ee\u6807"},{"location":"2-13-How-to-design-your-first-programs/#2","text":"\u5c3d\u7ba1\uff0c\u5b9a\u4e49\u95ee\u9898\u6709\u52a9\u4e8e\u4f60\u786e\u5b9a\u4f60\u671f\u671b\u7684\u7ed3\u679c\u662f\u58f0\u660e\uff0c\u4f46\u76ee\u6807\u4ecd\u7136\u662f\u4e0d\u6e05\u695a\u7684\u3002\u63a5\u4e0b\u6765\u4e00\u6b65\u5c31\u662f\u8003\u8651\u5982\u4f55\u8bbe\u8ba1\u9700\u6c42\u3002 \u9700\u6c42\u4e00\u65b9\u9762\u6307\u7684\u662f\u4f60\u80fd\u591f\u4f7f\u7528\u7684\u6709\u9650\u8d44\u6e90\uff08\u4f8b\u5982\uff0c\u9884\u7b97\u3001\u5de5\u671f\u3001\u7a7a\u95f4\u548c\u5185\u5b58\u7b49\u7b49\uff09\uff0c\u53e6\u4e00\u65b9\u9762\u6307\u7684\u662f\u4f60\u7684\u7a0b\u5e8f\u5fc5\u987b\u5177\u5907\u7684\u80fd\u591f\u6ee1\u8db3\u7528\u6237\u9700\u8981\u7684\u80fd\u529b\u3002\u6ce8\u610f\uff0c\u9700\u6c42\u5173\u6ce8\u7684\u4ecd\u7136\u662f \u4ec0\u4e48 \uff0c\u800c\u4e0d\u662f \u5982\u4f55 \u3002 \u4f8b\u5982\uff1a \u7535\u8bdd\u53f7\u7801\u5e94\u8be5\u80fd\u591f\u88ab\u50a8\u5b58\uff0c\u8fd9\u6837\u4ee5\u540e\u53ef\u4ee5\u7528\u6765\u8fdb\u884c\u56de\u62e8\uff1b \u968f\u673a\u7684\u5730\u7262\u5e94\u8be5\u603b\u662f\u5305\u542b\u4e00\u6761\u4ece\u5165\u53e3\u5230\u51fa\u53e3\u7684\u8def\uff1b \u80a1\u7968\u63a8\u8350\u5e94\u8be5\u53c2\u8003\u5386\u53f2\u4ef7\u683c\u4fe1\u606f\uff1b \u7528\u6237\u5e94\u8be5\u80fd\u591f\u8f93\u5165\u9ad8\u5854\u7684\u9ad8\u5ea6\uff1b \u6211\u4eec\u9700\u8981\u5728\u4e03\u5929\u5185\u5b8c\u6210\u53ef\u4f9b\u6d4b\u8bd5\u7684\u7248\u672c\uff1b \u7a0b\u5e8f\u9700\u8981\u572810\u79d2\u5185\u5b8c\u6210\u7528\u6237\u7684\u8bf7\u6c42\uff1b \u7a0b\u5e8f\u5728\u7528\u6237\u4f7f\u7528\u65f6\u5d29\u6e83\u9000\u51fa\u7684\u6982\u7387\u5e94\u8be5\u5c0f\u4e8e 0.1%\u3002 \u4e00\u4e2a\u95ee\u9898\u53ef\u80fd\u4f1a\u6d3e\u751f\u51fa\u5f88\u591a\u9700\u6c42\uff0c\u53ea\u6709\u6240\u6709\u7684\u9700\u6c42\u90fd\u88ab\u6ee1\u8db3\uff0c\u624d\u80fd\u8bf4\u89e3\u51b3\u65b9\u6848\u88ab\u5b9e\u73b0\u4e86\u3002","title":"\u8bbe\u8ba1\u6b65\u9aa42\uff1a\u5b9a\u9700\u6c42"},{"location":"2-13-How-to-design-your-first-programs/#3","text":"\u5982\u679c\u4f60\u5df2\u7ecf\u662f\u4e00\u4e2a\u6709\u7ecf\u9a8c\u7684\u7a0b\u5e8f\u5458\u4e86\uff0c\u5728\u8bbe\u8ba1\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u53ef\u80fd\u8fd8\u9700\u8981\u8003\u8651\u5176\u4ed6\u7684\u4e00\u4e9b\u95ee\u9898\uff0c\u4f8b\u5982\uff1a \u5b9a\u4e49\u7a0b\u5e8f\u9700\u8981\u8fd0\u884c\u7684\u5e73\u53f0\u7684\u4f53\u7cfb\u7ed3\u6784\u548c\uff08\u6216\uff09\u64cd\u4f5c\u7cfb\u7edf\uff1b \u786e\u5b9a\u4f60\u9700\u8981\u4f7f\u7528\u54ea\u4e9b\u5de5\u5177\uff1b \u786e\u5b9a\u4f60\u662f\u72ec\u7acb\u5b8c\u6210\u4ee3\u7801\u7f16\u5199\u8fd8\u662f\u4ee5\u56e2\u961f\u5408\u4f5c\u7684\u65b9\u5f0f\u5b8c\u6210\uff1b \u5b9a\u4e49\u6d4b\u8bd5\u3001\u53cd\u9988\u548c\u91ca\u653e\u7b56\u7565\uff1b \u5b9a\u4e49\u5982\u4f55\u5907\u4efd\u4ee3\u7801\u3002 \u4e0d\u8fc7\uff0c\u4f5c\u4e3a\u65b0\u624b\u7a0b\u5e8f\u5458\uff0c\u4e0a\u9762\u51e0\u4e2a\u95ee\u9898\u5176\u5b9e\u90fd\u5f88\u7b80\u5355\uff1a\u4f60\u5c06\u4f1a\u72ec\u7acb\u5730\u5728\u4f60\u81ea\u5df1\u7684\u7535\u8111\u4e0a\u7f16\u5199\u4ee3\u7801\uff0c\u6240\u9700\u7684\u5de5\u5177\u5c31\u662f\u4f60\u4e0b\u8f7d\u5e76\u5b89\u88c5\u4e86\u7684IDE\uff0c\u4f60\u7f16\u5199\u7684\u7a0b\u5e8f\u9664\u4e86\u4f60\u81ea\u5df1\u6ca1\u6709\u5176\u4ed6\u4eba\u4f1a\u4f7f\u7528\u3002 \u5373\u4fbf\u5982\u6b64\uff0c\u5982\u679c\u4f60\u7f16\u5199\u7684\u4ee3\u7801\u4e0d\u662f\u5be5\u5be5\u51e0\u884c\uff0c\u4f60\u8fd8\u662f\u5e94\u8be5\u8003\u8651\u4e00\u4e0b\u5982\u4f55\u5907\u4efd\u4f60\u7684\u4ee3\u7801\u3002\u5c06\u4ee3\u7801\u62f7\u8d1d\u6216\u8005\u538b\u7f29\u540e\u653e\u5230\u7535\u8111\u7684\u5176\u4ed6\u76ee\u5f55\u4e2d\u662f\u4e0d\u591f\u7684\uff08\u5c3d\u7ba1\u804a\u80dc\u4e8e\u65e0\uff09\u3002\u5982\u679c\u4f60\u7684\u7535\u8111\u574f\u4e86\uff0c\u4f60\u7f16\u5199\u7684\u4ee3\u7801\u4e5f\u5c31\u4ed8\u4e4b\u4e1c\u6d41\u4e86\u3002\u4e00\u4e2a\u597d\u7684\u5907\u4efd\u7b56\u7565\uff0c\u5e94\u8be5\u662f\u628a\u4ee3\u7801\u5168\u90e8\u62f7\u8d1d\u4e00\u4efd\u540e\u62f7\u5230\u5176\u4ed6\u7535\u8111\u6216\u5176\u4ed6\u5730\u65b9\u3002\u6709\u5f88\u591a\u529e\u6cd5\u53ef\u4ee5\u5b9e\u73b0\uff0c\u4f8b\u5982\u5c06\u5176\u538b\u7f29\u4e3aZip\u6587\u4ef6\u540e\u53d1\u9001\u5230\u4f60\u81ea\u5df1\u7684\u90ae\u7bb1\uff0c\u62f7\u8d1d\u5230Dropbox\u6216\u8005\u7f51\u76d8\uff0c\u6216\u8005\u901a\u8fc7FTP\u4f20\u5230\u5176\u4ed6\u7535\u8111\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5c40\u57df\u7f51\u62f7\u5230\u5176\u4ed6\u7535\u8111\u4e0a\uff0c\u6216\u8005\u4f7f\u7528\u4f4d\u4e8e\u5176\u4ed6\u7535\u8111\u6216\u4e91\u7aef\u7684\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff08\u4f8b\u5982\uff1aGithub\uff09\u3002\u7248\u672c\u63a7\u5236\u7cfb\u7edf\u9664\u4e86\u5b58\u50a8\u6587\u4ef6\u4ee5\u5916\uff0c\u8fd8\u6709\u5f88\u591a\u5176\u4ed6\u6709\u7528\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u5b83\u53ef\u4ee5\u5c06\u4f60\u7684\u4ee3\u7801\u56de\u9000\u5230\u4e4b\u524d\u7684\u7248\u672c\u3002","title":"\u8bbe\u8ba1\u6b65\u9aa43\uff1a\u5b9a\u4e49\u5de5\u5177\u3001\u76ee\u6807\u4ea7\u7269\u4ee5\u53ca\u5907\u4efd\u8ba1\u5212"},{"location":"2-13-How-to-design-your-first-programs/#4","text":"\u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\uff0c\u6211\u4eec\u65f6\u5e38\u9700\u8981\u5b8c\u6210\u4e00\u4e9b\u975e\u5e38\u590d\u6742\u7684\u95ee\u9898\u3002\u627e\u5230\u7684\u95ee\u9898\u7684\u89e3\u51b3\u529e\u6cd5\uff0c\u901a\u5e38\u5e76\u4e0d\u5bb9\u6613\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528 \u81ea\u9876\u5411\u4e0b(top down) \u7684\u529e\u6cd5\u6765\u89e3\u51b3\u95ee\u9898\u3002\u4e3a\u4e86\u89e3\u51b3\u4e00\u4e2a\u7279\u522b\u590d\u6742\u7684\u95ee\u9898\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u628a\u8fd9\u4e2a\u95ee\u9898\u5206\u89e3\u4e3a\u591a\u4e2a\u76f8\u5bf9\u6765\u8bf4\u66f4\u5bb9\u6613\u89e3\u51b3\u7684\u5b50\u95ee\u9898\u3002\u5982\u679c\u5b50\u95ee\u9898\u4ecd\u7136\u96be\u4ee5\u89e3\u51b3\uff0c\u5b83\u4eec\u8fd8\u53ef\u4ee5\u88ab\u8fdb\u4e00\u6b65\u5206\u5272\u4e3a\u66f4\u591a\u7684\u5b50\u95ee\u9898\u3002\u901a\u8fc7\u4e0d\u65ad\u5730\u5c06\u590d\u6742\u95ee\u9898\u5206\u89e3\u6210\u7b80\u5355\u7684\u5b50\u95ee\u9898\uff0c\u4f60\u6700\u7ec8\u53ef\u4ee5\u5f97\u5230\u5f88\u591a\u66f4\u5bb9\u6613\u88ab\u5904\u7406\u7684\u5b50\u95ee\u9898\u3002 \u4e3e\u4f8b\u6765\u8bf4\uff0c\u5982\u679c\u6211\u4eec\u9700\u8981\u6253\u626b\u5c4b\u5b50\uff0c\u90a3\u4e48\u5206\u89e3\u4efb\u52a1\u7684\u7ed3\u6784\u53ef\u80fd\u4f1a\u662f\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a \u6253\u626b\u5c4b\u5b50 \u6253\u626b\u5c4b\u5b50\u662f\u4e00\u4e2a\u5f88\u5927\u7684\u4efb\u52a1\uff0c\u8ba9\u6211\u4eec\u5c06\u5176\u5206\u89e3\u4e3a\u591a\u4e2a\u5b50\u4efb\u52a1\uff1a \u6253\u626b\u5c4b\u5b50 \u6e05\u7406\u5730\u6bef \u6253\u626b\u536b\u751f\u95f4 \u6253\u626b\u53a8\u623f \u6bd4\u4e4b\u524d\u5bb9\u6613\u5904\u7406\u4e86\u4e00\u4e9b\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u9010\u9879\u89e3\u51b3\u8fd9\u4e9b\u5b50\u95ee\u9898\u4e86\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u5176\u5b9e\u8fd8\u80fd\u5c06\u8fd9\u4e9b\u4efb\u52a1\u7ee7\u7eed\u5206\u89e3\uff1a \u6253\u626b\u5c4b\u5b50 \u6e05\u7406\u5730\u6bef \u6253\u626b\u536b\u751f\u95f4 \u6e05\u6d17\u9a6c\u6876 \u6e05\u7406\u6c34\u6c60 \u6253\u626b\u53a8\u623f \u6e05\u7406\u7076\u53f0 \u6253\u626b\u7076\u53f0 \u6e05\u6d17\u6c34\u6c60 \u5012\u5783\u573e \u73b0\u5728\uff0c\u6211\u4eec\u9762\u5bf9\u7684\u662f\u4e00\u7cfb\u5217\u5177\u6709\u5c42\u6b21\u7ed3\u6784\u7684\u95ee\u9898\u4e86\uff0c\u800c\u4e14\u5b83\u4eec\u4e2d\u4efb\u4f55\u4e00\u9879\u90fd\u4e0d\u590d\u6742\u3002\u901a\u8fc7\u5b8c\u6210\u8fd9\u4e9b\u76f8\u5bf9\u66f4\u5bb9\u6613\u5904\u7406\u7684\u5b50\u4efb\u52a1\u4e4b\u540e\uff0c\u6211\u4eec\u4e5f\u5c31\u5b8c\u6210\u4e86\u6253\u626b\u5c4b\u5b50\u8fd9\u4e2a\u66f4\u590d\u6742\u7684\u95ee\u9898\u3002 \u53e6\u5916\u4e00\u79cd\u5206\u89e3\u4efb\u52a1\u7684\u65b9\u5f0f\u79f0\u4e3a \u81ea\u5e95\u5411\u4e0a(bottom) \u65b9\u6cd5\u3002\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u65f6\uff0c\u6211\u4eec\u4f1a\u4ece\u4e00\u7cfb\u5217\u7b80\u5355\u7684\u4efb\u52a1\u5f00\u59cb\uff0c\u7136\u540e\u901a\u8fc7\u5408\u5e76\u5206\u7ec4\u7684\u65b9\u5f0f\u6784\u5efa\u4efb\u52a1\u5217\u8868\u3002 \u5927\u591a\u6570\u4eba\u5e73\u65f6\u4e0d\u662f\u4e0a\u5b66\u5c31\u662f\u4e0a\u73ed\uff0c\u90a3\u4e48\u5c31\u4ee5\u6b64\u6765\u4e3e\u4f8b\uff0c\u5047\u8bbe\u6211\u4eec\u8981\u89e3\u51b3\u7684\u662f\u201c\u53bb\u4e0a\u73ed\u201d\u8fd9\u4e2a\u95ee\u9898\uff0c\u5982\u679c\u6709\u4eba\u95ee\u4f60\uff0c\u4f60\u65e9\u4e0a\u51fa\u95e8\u524d\u90fd\u505a\u54ea\u4e9b\u4e8b\u5462\uff1f\u4f60\u7684\u56de\u7b54\u53ef\u80fd\u5305\u542b\u4e0b\u9762\u8fd9\u4e9b\u4efb\u52a1\uff1a \u62ff\u51fa\u8981\u7a7f\u7684\u8863\u670d \u7a7f\u8863 \u5403\u65e9\u996d \u5f00\u8f66\u53bb\u516c\u53f8 \u5237\u7259 \u8d77\u5e8a \u505a\u65e9\u9910 \u4e0a\u8f66 \u6d17\u6fa1 \u4f7f\u7528\u81ea\u5e95\u5411\u4e0a\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u5b83\u4eec\u7684\u76f8\u4f3c\u6027\uff0c\u4e3a\u8fd9\u4e9b\u4efb\u52a1\u521b\u5efa\u4e00\u4e2a\u5c42\u6b21\u7ed3\u6784\uff1a \u4ece\u8d77\u5e8a\u5230\u4e0a\u73ed\u8981\u505a\u7684\u4e8b \u5367\u5ba4\u8981\u505a\u7684\u4e8b \u8d77\u5e8a \u62ff\u8863\u670d \u7a7f\u8863 \u536b\u751f\u95f4\u8981\u505a\u7684\u4e8b \u6d17\u6fa1 \u5237\u7259 \u65e9\u9910\u76f8\u5173 \u51c6\u5907\u9ea6\u7247 \u5403\u9ea6\u7247 \u901a\u52e4\u76f8\u5173 \u4e0a\u8f66 \u5f00\u8f66\u5230\u516c\u53f8 \u5176\u5b9e\u6211\u4eec\u4e0d\u96be\u770b\u51fa\uff0c\u8fd9\u4e9b\u5c42\u6b21\u7ed3\u6784\u5bf9\u4e8e\u7f16\u7a0b\u6765\u8bf4\u662f\u975e\u5e38\u6709\u7528\u7684\uff0c\u56e0\u4e3a\u4e00\u65e6\u4efb\u52a1\u7684\u7ed3\u6784\u6e05\u695a\u4e86\uff0c\u7a0b\u5e8f\u7684\u5927\u4f53\u7ed3\u6784\u5176\u5b9e\u4e5f\u5df2\u7ecf\u6e05\u695a\u4e86\u3002\u6700\u4e0a\u5c42\u7684\u4efb\u52a1\uff08\u6253\u626b\u5c4b\u5b50\u6216\u53bb\u4e0a\u73ed\uff09\u5c31\u53ef\u4ee5\u662f\u7a0b\u5e8f\u7684 main() \u51fd\u6570\uff08\u56e0\u4e3a\u5b83\u662f\u4f60\u8981\u89e3\u51b3\u7684\u4e3b\u8981(main)\u95ee\u9898\uff09\u3002\u800c\u5176\u4ed6\u5b50\u9879\u76ee\u5219\u53ef\u4ee5\u5b9e\u73b0\u4e3a\u4e0d\u540c\u7684\u51fd\u6570\u3002 \u5982\u679c\u5176\u4e2d\u67d0\u4e00\u9879\u4efb\u52a1\uff08\u51fd\u6570\uff09\u5728\u5b9e\u73b0\u65f6\u4ecd\u7136\u8fc7\u4e8e\u56f0\u96be\uff0c\u5219\u53ef\u4ee5\u7ee7\u7eed\u5c06\u5176\u5206\u89e3\u4e3a\u51e0\u4e2a\u5b50\u95ee\u9898\u3002\u6700\u7ec8\u4f60\u5c31\u53ef\u4ee5\u5c06\u7a0b\u5e8f\u5206\u5272\u4e3a\u591a\u4e2a\u5bb9\u6613\u5b9e\u73b0\u7684\u51fd\u6570\u3002","title":"\u8bbe\u8ba1\u6b65\u9aa44\uff1a\u5c06\u590d\u6742\u95ee\u9898\u5207\u5206\u4e3a\u591a\u4e2a\u7b80\u5355\u95ee\u9898"},{"location":"2-13-How-to-design-your-first-programs/#5","text":"\u73b0\u5728\uff0c\u7a0b\u5e8f\u7684\u7ed3\u6784\u5df2\u7ecf\u6e05\u695a\u4e86\uff0c\u63a5\u4e0b\u6765\u9700\u8981\u5c06\u8fd9\u4e9b\u4efb\u52a1\u5173\u8054\u5176\u4ed6\u3002\u9996\u5148\uff0c\u6211\u4eec\u8981\u786e\u5b9a\u8fd9\u4e9b\u8981\u4efb\u52a1\u7684\u53d1\u751f\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u4f60\u65e9\u4e0a\u8d77\u6765\uff0c\u5b8c\u6210\u4e0a\u9762\u90a3\u4e9b\u4efb\u52a1\u7684\u987a\u5e8f\u662f\u4ec0\u4e48\uff1f\u5dee\u4e0d\u591a\u5e94\u8be5\u662f\u8fd9\u6837\u7684\u5427\uff1a \u5367\u5ba4\u8981\u505a\u7684\u4e8b \u536b\u751f\u95f4\u8981\u505a\u7684\u4e8b \u65e9\u9910\u76f8\u5173 \u901a\u52e4\u76f8\u5173 \u5982\u679c\u4f60\u8981\u7f16\u5199\u4e00\u4e2a\u8ba1\u7b97\u5668\u7a0b\u5e8f\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u6309\u7167\u4e0b\u9762\u6b65\u9aa4\u6765\u505a\uff1a \u4ece\u7528\u6237\u83b7\u53d6\u7b2c\u4e00\u4e2a\u6570 \u4ece\u7528\u6237\u83b7\u53d6\u9700\u8981\u6267\u884c\u7684\u8fd0\u7b97\u64cd\u4f5c \u4ece\u7528\u6237\u83b7\u53d6\u7b2c\u4e8c\u4e2a\u6570 \u8ba1\u7b97\u7ed3\u679c \u6253\u5370\u7ed3\u679c \u5b8c\u6210\u4e0a\u8ff0\u8bbe\u8ba1\u5de5\u4f5c\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f00\u59cb\u5b9e\u73b0\u4ee3\u7801\u4e86\uff01","title":"\u8bbe\u8ba1\u6b65\u9aa45\uff1a\u786e\u5b9a\u76f8\u5173\u4e8b\u4ef6\u53d1\u751f\u7684\u987a\u5e8f"},{"location":"2-13-How-to-design-your-first-programs/#1main","text":"\u73b0\u5728\u53ef\u4ee5\u5f00\u59cb\u5b9e\u73b0\u4ee3\u7801\u4e86\u3002\u6839\u636e\u4e0a\u9762\u987a\u5e8f\u7ed3\u6784\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u5b8c\u6210 main \u51fd\u6570\u7684\u6846\u67b6\uff0c\u6682\u65f6\u65e0\u9700\u62c5\u5fc3\u8f93\u5165\u8f93\u51fa\u7684\u95ee\u9898\u3002 1 2 3 4 5 6 7 8 9 int main () { // doBedroomThings(); // doBathroomThings(); // doBreakfastThings(); // doTransportationThings(); return 0 ; } \u6216\u8005\u5bf9\u4e8e\u8ba1\u7b97\u5668\u4f8b\u5b50\u6765\u8bf4\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 int main () { // Get first number from user // getUserInput(); // Get mathematical operation from user // getMathematicalOperation(); // Get second number from user // getUserInput(); // Calculate result // calculateResult(); // Print result // printResult(); return 0 ; } \u6ce8\u610f\uff0c\u5982\u679c\u4f60\u4e5f\u4f7f\u7528\u8fd9\u79cd\u201c\u5927\u7eb2\u6846\u67b6\u201d\u7684\u65b9\u6cd5\u6765\u6784\u5efa\u7a0b\u5e8f\uff0c\u8fd9\u91cc\u6d89\u53ca\u7684\u51fd\u6570\u90fd\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\uff0c\u6bd5\u7adf\u5b83\u4eec\u8fd8\u6ca1\u6709\u88ab\u5b9a\u4e49\u3002\u5728\u51fd\u6570\u88ab\u5b9e\u9645\u5b9a\u4e49\u524d\u5148\u5c06\u5176\u6682\u65f6\u5148\u6ce8\u91ca\u6389\u662f\u4e00\u79cd\u65b9\u6cd5\uff08\u4e0a\u9762\u5c55\u793a\u7684\u5c31\u662f\u8fd9\u79cd\u65b9\u6cd5\uff09\u3002\u6b64\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u5bf9\u51fd\u6570\u8fdb\u884c \u6253\u6869(stub) \uff0c\u5373\u4e3a\u7a0b\u5e8f\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u51fd\u6570\u4f53\uff0c\u4ee5\u4fbf\u7a0b\u5e8f\u53ef\u4ee5\u7f16\u8bd1\u901a\u8fc7\u3002","title":"\u5b9e\u73b0\u6b65\u9aa41\uff1a\u7f16\u5199main\u51fd\u6570\u6846\u67b6"},{"location":"2-13-How-to-design-your-first-programs/#2_1","text":"\u5728\u8fd9\u4e00\u6b65\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u51fd\u6570\u90fd\u9700\u8981\u505a\u4e09\u4ef6\u4e8b\uff1a \u5b9a\u4e49\u51fd\u6570\u7684\u539f\u578b\uff08\u8f93\u5165\u8f93\u51fa\uff09\uff1b \u7f16\u5199\u51fd\u6570\uff1b \u6d4b\u8bd5\u51fd\u6570\u3002 \u5982\u679c\u4f60\u7684\u51fd\u6570\u7c92\u5ea6\u8db3\u591f\u7ec6\uff0c\u90a3\u4e48\u6bcf\u4e2a\u51fd\u6570\u90fd\u5e94\u8be5\u662f\u975e\u5e38\u7b80\u5355\u548c\u76f4\u63a5\u7684\u3002\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u5b9e\u73b0\u8d77\u6765\u975e\u5e38\u590d\u6742\uff0c\u90a3\u4e48\u5f88\u53ef\u80fd\u5b83\u8fd8\u9700\u8981\u88ab\u8fdb\u4e00\u6b65\u5206\u5272\u4e3a\u591a\u4e2a\u66f4\u5bb9\u6613\u5b9e\u73b0\u7684\u5b50\u51fd\u6570\uff08 \u4e5f\u53ef\u80fd\u662f\u4f60\u7684\u4efb\u52a1\u987a\u5e8f\u6709\u95ee\u9898\uff0c\u4f60\u5e94\u8be5\u91cd\u65b0\u5ba1\u89c6\u4e00\u4e0b\u5404\u4e2a\u6b65\u9aa4\u7684\u987a\u5e8f \uff09\u3002 \u63a5\u4e0b\u6765\uff0c\u5b9e\u73b0\u8ba1\u7b97\u5668\u7a0b\u5e8f\u4e2d\u7684\u7b2c\u4e00\u4e2a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include // Full implementation of the getUserInput function int getUserInput () { std :: cout << \"Enter an integer \" ; int input {}; std :: cin >> input ; return input ; } int main () { // \u4ece\u7528\u6237\u83b7\u53d6\u7b2c\u4e00\u4e2a\u8f93\u5165 int value { getUserInput () }; // \u6ce8\u610f\uff0c\u8fd9\u91cc\u7684\u4ee3\u7801\u672c\u8eab\u5c31\u5177\u5907\u6d4b\u8bd5\u51fd\u6570\u8f93\u51fa\u7684\u529f\u80fd std :: cout << value ; // \u7528\u4e8e\u786e\u4fdd getUserInput() \u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u7684\u8c03\u8bd5\u4ee3\u7801\uff0c\u7a0d\u540e\u9700\u8981\u79fb\u9664 // Get mathematical operation from user // getMathematicalOperation(); // Get second number from user // getUserInput(); // Calculate result // calculateResult(); // Print result // printResult(); return 0 ; } \u9996\u5148\uff0c\u786e\u5b9a\u51fd\u6570 getUserInput \u4e0d\u9700\u8981\u63a5\u53d7\u4efb\u4f55 \u5b9e\u53c2 \u3002\u540c\u65f6\uff0c\u8be5\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u53cd\u6620\u5230\u51fd\u6570\u539f\u578b\u4e0a\uff0c\u5373\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a int \uff0c\u540c\u65f6\u6ca1\u6709 \u5f62\u53c2 \u3002\u63a5\u4e0b\u6765\uff0c\u7f16\u5199\u51fd\u6570\u4f53\u3002\u51fd\u6570\u4f53\u975e\u5e38\u76f4\u63a5\uff0c\u53ea\u5305\u542b\u56db\u6761\u8bed\u53e5 \u8bed\u53e5 \u3002\u6700\u540e\uff0c\u5728 main \u51fd\u6570\u4e2d\u7f16\u5199\u4e00\u4e9b\u7528\u4e8e\u6d4b\u8bd5\u51fd\u6570 getUserInput \u529f\u80fd\uff08\u5305\u62ec\u5176\u8fd4\u56de\u503c) \u7684\u4e34\u65f6\u4ee3\u7801\uff0c\u786e\u5b9a\u51fd\u6570\u662f\u5426\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u3002 \u591a\u6b21\u8fd0\u884c\u7a0b\u5e8f\u5e76\u4f7f\u7528\u4e0d\u540c\u7684\u8f93\u5165\u503c\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7ed3\u679c\u90fd\u7b26\u5408\u9884\u671f\u3002\u5982\u679c\u53d1\u73b0\u7a0b\u5e8f\u4e0d\u80fd\u6b63\u786e\u5de5\u4f5c\uff0c\u5219\u95ee\u9898\u53ef\u80fd\u5c31\u51fa\u5728\u521a\u624d\u7f16\u5199\u7684\u4ee3\u7801\u4e2d\u3002 \u4e00\u65e6\u4f60\u786e\u5b9a\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\uff0c\u90a3\u4e48\u8fd9\u4e9b\u4e34\u65f6\u6d4b\u8bd5\u4ee3\u7801\u5c31\u53ef\u4ee5\u88ab\u79fb\u9664\u4e86\uff0c\u7136\u540e\u5f00\u59cb\u5b9e\u73b0\u4e0b\u4e00\u4e2a\u51fd\u6570\uff08 getMathematicalOperation \uff09\u3002\u6211\u4eec\u5e76\u4e0d\u4f1a\u5728\u672c\u8282\u8bfe\u4e2d\u5b9e\u73b0\u6240\u6709\u51fd\u6570\uff0c\u56e0\u4e3a\u8fd8\u6709\u4e00\u4e9b\u66f4\u52a0\u91cd\u8981\u7684\u8bdd\u9898\u9700\u8981\u8ba8\u8bba\u3002 \u8bb0\u4f4f\uff1a\u4e0d\u8981\u671f\u671b\u4e00\u6b21\u6027\u5b9e\u73b0\u5168\u90e8\u7684\u4ee3\u7801\u3002\u4e00\u6b65\u4e00\u6b65\u5b8c\u6210\uff0c\u5e76\u4e14\u6bcf\u4e00\u6b65\u90fd\u8981\u8fdb\u884c\u6d4b\u8bd5\u3002","title":"\u5b9e\u73b0\u6b65\u9aa42\uff1a\u5b9e\u73b0\u6bcf\u4e2a\u51fd\u6570"},{"location":"2-13-How-to-design-your-first-programs/#3_1","text":"\u5f53\u7a0b\u5e8f\u7f16\u5199\u201c\u5b8c\u6210\u201d\u540e\uff0c\u6700\u540e\u4e00\u6b65\u5219\u662f\u5bf9\u5b8c\u6574\u7684\u7a0b\u5e8f\u8fdb\u884c\u6d4b\u8bd5\uff0c\u786e\u5b9a\u5176\u884c\u4e3a\u662f\u5426\u6ee1\u8db3\u9884\u671f\u3002\u5982\u679c\u4e0d\u6ee1\u8db3\uff0c\u5219\u9700\u8981\u8fdb\u884c\u4fee\u590d\u3002","title":"\u5b9e\u73b0\u6b65\u9aa43\uff1a\u6700\u7ec8\u6d4b\u8bd5"},{"location":"2-13-How-to-design-your-first-programs/#_1","text":"\u5f00\u59cb\u65f6\u4e00\u5207\u4ece\u7b80 \uff1a\u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u5bf9\u5176\u8981\u5b9e\u73b0\u7684\u7a0b\u5e8f\u548c\u529f\u80fd\u5177\u6709\u5b8f\u4f1f\u7684\u84dd\u56fe\uff1a\u201c\u6211\u60f3\u8981\u8bbe\u8ba1\u4e00\u6b3e\u5177\u6709\u56fe\u5f62\u754c\u9762\u3001\u914d\u4e50\u548c\u968f\u673a\u5730\u7262\u7684\u89d2\u8272\u626e\u6f14\u6e38\u620f\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u6e38\u620f\u5730\u56fe\u8fd8\u5305\u62ec\u57ce\u9547\uff0c\u4f60\u53ef\u4ee5\u5728\u57ce\u9547\u91cc\u552e\u5356\u4f60\u6536\u96c6\u5230\u7684\u6218\u5229\u54c1\u201d\u3002\u5982\u679c\u4f60\u4ece\u4e00\u5f00\u59cb\u5c31\u8981\u5b8c\u6210\u4e00\u4e2a\u975e\u5e38\u590d\u6742\u7684\u4efb\u52a1\uff0c\u4f60\u53ef\u80fd\u4f1a\u5728\u8fdb\u5ea6\u4e0d\u4f73\u65f6\u611f\u5230\u4e0d\u77e5\u6240\u63aa\u6216\u5fc3\u7070\u610f\u51b7\u3002\u6240\u4ee5\uff0c\u4f60\u5176\u5b9e\u5e94\u8be5\u5c06\u4f60\u5f00\u59cb\u65f6\u7684\u76ee\u6807\u8bbe\u7684\u5c3d\u53ef\u80fd\u7b80\u5355\uff0c\u5373\u4e00\u4e9b\u4f60\u80af\u5b9a\u80fd\u591f\u5b8c\u6210\u7684\u76ee\u6807\u3002\u4f8b\u5982\uff0c\u201c\u6211\u60f3\u8981\u5728\u5c4f\u5e55\u4e0a\u663e\u793a\u4e00\u4e2a2D\u7684\u533a\u57df\u201d\u3002 \u9010\u6e10\u6dfb\u52a0\u65b0\u7684\u529f\u80fd \uff1a\u5f53\u4e0a\u8ff0\u7b80\u5355\u7684\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u8003\u8651\u4e3a\u5176\u589e\u52a0\u65b0\u7684\u529f\u80fd\u4e86\u3002\u4f8b\u5982\uff0c\u5f53\u4e0a\u8ff02D\u533a\u57df\u53ef\u4ee5\u88ab\u6b63\u5e38\u663e\u793a\u5728\u5c4f\u5e55\u4e0a\u540e\uff0c\u4f60\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u80fd\u591f\u79fb\u52a8\u7684\u89d2\u8272\u3002\u5f53\u89d2\u8272\u53ef\u4ee5\u79fb\u52a8\u540e\uff0c\u6dfb\u52a0\u4e00\u4e9b\u5899\u4f53\u9650\u5236\u89d2\u8272\u7684\u79fb\u52a8\u8def\u5f84\u3002\u5899\u4f53\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5229\u7528\u5899\u4f53\u6765\u6784\u5efa\u4e00\u4e2a\u57ce\u9547\u5730\u56fe\u3002\u57ce\u9547\u5b8c\u6210\u540e\uff0c\u6dfb\u52a0\u4e00\u4e2a\u5546\u4ebaNPC\u3002\u901a\u8fc7\u8fd9\u79cd\u5faa\u5e8f\u6e10\u8fdb\u7684\u65b9\u5f0f\u4e3a\u4f60\u7684\u7a0b\u5e8f\u6dfb\u52a0\u65b0\u529f\u80fd\uff0c\u7a0b\u5e8f\u6700\u7ec8\u4f1a\u53d8\u5f97\u8d8a\u6765\u8d8a\u590d\u6742\uff0c\u540c\u65f6\u4e5f\u4e0d\u4f1a\u8ba9\u4f60\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u611f\u5230\u4e0d\u77e5\u6240\u63aa\u3002 \u4e00\u6b21\u53ea\u505a\u4e00\u4ef6\u4e8b \uff1a\u4e0d\u8981\u671f\u671b\u4e00\u6b21\u6027\u5199\u5b8c\u5168\u90e8\u7684\u4ee3\u7801\uff0c\u4e5f\u4e0d\u8981\u628a\u4f60\u7684\u6ce8\u610f\u529b\u5206\u6563\u5728\u591a\u4e2a\u4efb\u52a1\u4e0a\u3002\u4e00\u6b21\u53ea\u505a\u4e00\u4ef6\u4e8b\u3002\u4e00\u4e2a\u6b63\u5e38\u5de5\u4f5c\u7684\u529f\u80fd+\u4e94\u4e2a\u5c1a\u672a\u5b9e\u73b0\u7684\u529f\u80fd\uff0c\u597d\u8fc7\u516d\u4e2a\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u7684\u529f\u80fd\u3002\u6ce8\u610f\u529b\u88ab\u5206\u6563\u7684\u65f6\u5019\uff0c\u4f60\u4f1a\u66f4\u5bb9\u6613\u72af\u9519\uff0c\u4e5f\u66f4\u5bb9\u6613\u5fd8\u8bb0\u91cd\u8981\u7684\u7ec6\u8282\u3002 \u4e00\u8fb9\u5b9e\u73b0\u3001\u4e00\u8fb9\u6d4b\u8bd5 \uff1a\u65b0\u7684\u7a0b\u5e8f\u5458\u5e38\u5e38\u4f1a\u4e00\u6b21\u6027\u5199\u5b8c\u5168\u90e8\u7684\u7a0b\u5e8f\uff0c\u7136\u540e\u624d\u5bf9\u5176\u8fdb\u884c\u7f16\u8bd1\uff0c\u7136\u540e\u5c31\u201c\u6536\u83b7\u201d\u4e86\u975e\u5e38\u591a\u7684\u62a5\u9519\u4fe1\u606f\u3002\u5199\u5b8c\u7684\u4ee3\u7801\u4e0d\u80fd\u7f16\u8bd1\uff0c\u4e0d\u4ec5\u5413\u4eba\uff0c\u800c\u4e14\u8fd8\u96be\u4ee5\u67e5\u627e\u5176\u4ea7\u751f\u9519\u8bef\u7684\u539f\u56e0\u3002\u6b63\u786e\u7684\u505a\u6cd5\u662f\u7f16\u5199\u4e00\u4e9b\u4ee3\u7801\uff0c\u7136\u540e\u5c31\u5bf9\u5176\u8fdb\u884c\u7f16\u8bd1\u548c\u6d4b\u8bd5\u3002\u5982\u679c\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u90a3\u4e48\u4fee\u590d\u8d77\u6765\u4e5f\u975e\u5e38\u7b80\u5355\u3002\u5f53\u4f60\u786e\u5b9a\u8fd9\u4e9b\u4ee3\u7801\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\uff0c\u518d\u91cd\u590d\u4e0a\u8ff0\u64cd\u4f5c\uff0c\u5f00\u59cb\u7f16\u5199\u63a5\u4e0b\u6765\u7684\u4ee3\u7801\u3002\u8fd9\u4e48\u505a\u6216\u8bb8\u4f1a\u201c\u62d6\u7d2f\u201d\u4f60\u7684\u8fdb\u5ea6\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u5f53\u4f60\u5b8c\u6210\u540e \uff0c\u4ee3\u7801\u5e94\u8be5\u662f\u80fd\u591f\u6b63\u786e\u5de5\u4f5c\u7684\uff0c\u8fd9\u597d\u8fc7\u4f60\u5199\u5b8c\u4ee3\u7801\u540e\u518d\u82b1\u8d39\u4e24\u500d\u591a\u7684\u65f6\u95f4\u53bb\u5b9a\u4f4d\u95ee\u9898\u3002 \u4e0d\u8981\u8fc7\u65e9\u5730\u4f18\u5316\u4ee3\u7801 \uff1a\u7b2c\u4e00\u7248\u529f\u80fd\uff08\u6216\u7a0b\u5e8f\uff09\u5f88\u5c11\u662f\u5b8c\u7f8e\u7684\u3002\u800c\u4e14\uff0c\u7a0b\u5e8f\u672c\u8eab\u4e5f\u4f1a\u968f\u7740\u65f6\u95f4\u4e0d\u65ad\u88ab\u66f4\u65b0\uff0c\u6bd5\u7adf\u4f60\u8fd8\u8981\u4e3a\u5176\u6dfb\u52a0\u529f\u80fd\uff0c\u6216\u8005\u91cd\u65b0\u7ec4\u7ec7\u4ee3\u7801\u3002\u5982\u679c\u4f60\u5728\u65e9\u671f\u5c31\u82b1\u8d39\u5f88\u591a\u65f6\u95f4\u53bb\u6253\u78e8\u8fd9\u4e9b\u4ee3\u7801\uff08\u6dfb\u52a0\u5f88\u591a\u6587\u6863\u3001\u786e\u4fdd\u6ee1\u8db3\u6700\u4f73\u5b9e\u8df5\u3001\u4f18\u5316\u7b49\uff09\uff0c\u90a3\u4e48\u6709\u53ef\u80fd\u4f1a\u56e0\u4e3a\u5fc5\u987b\u4fee\u6539\u4ee3\u7801\u800c\u4f7f\u5f97\u8fd9\u4e9b\u52aa\u529b\u90fd\u4ed8\u4e4b\u4e1c\u6d41\u3002\u6b63\u786e\u7684\u505a\u6cd5\u662f\uff0c\u8ba9\u4f60\u7684\u7a0b\u5e8f\u529f\u80fd\u4fdd\u6301\u6700\u7b80\u53ef\u7528\uff0c\u7136\u540e\u7ee7\u7eed\u5176\u4ed6\u7684\u5de5\u4f5c\u3002\u5f53\u4f60\u5bf9\u7a0b\u5e8f\u611f\u5230\u6ee1\u610f\u65f6\uff0c\u518d\u5bf9\u5176\u8fdb\u884c\u4e00\u7cfb\u5217\u7684\u4f18\u5316\u3002\u4e0d\u8fc7\uff0c\u4e0d\u8981\u671f\u671b\u5176\u8fbe\u5230\u5b8c\u7f8e\u7684\u72b6\u6001\uff0c\u5927\u578b\u7a0b\u5e8f\u5f88\u5c11\u662f\u5b8c\u7f8e\u7684\uff0c\u5b83\u603b\u662f\u6709\u8fd9\u6837\u6216\u90a3\u6837\u7684\u95ee\u9898\u9700\u8981\u6211\u4eec\u4f18\u5316\u3002\u505a\u5230\u8db3\u591f\u597d\u5c31\u53ef\u4ee5\u4e86\u3002 \u5927\u591a\u6570\u65b0\u624b\u7a0b\u5e8f\u5458\u5e76\u4e0d\u80fd\u5f88\u597d\u7684\u9075\u5faa\u4e0a\u8ff0\u5fe0\u8a00\uff08\u56e0\u4e3a\u770b\u4e0a\u53bb\u5f88\u9ebb\u70e6\uff0c\u4e5f\u4e0d\u5982\u5199\u4ee3\u7801\u6709\u8da3\uff09\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u5177\u6709\u4e00\u5b9a\u89c4\u6a21\u7684\u4ee3\u7801\u6765\u8bf4\uff0c\u5b8c\u6574\u9075\u5faa\u4e0a\u8ff0\u5efa\u8bae\u7edd\u5bf9\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8282\u7ea6\u5f88\u591a\u65f6\u95f4\uff08\u4ece\u957f\u671f\u6765\u770b\uff09\u3002\u7b80\u5355\u5730\u8fdb\u884c\u4e00\u4e9b\u8bbe\u8ba1\u89c4\u5212\uff0c\u5c31\u53ef\u4ee5\u4e3a\u540e\u671f\u8282\u7ea6\u5927\u91cf\u7684\u8c03\u8bd5\u65f6\u95f4\u3002 \u597d\u6d88\u606f\u662f\uff0c\u4e00\u65e6\u4f60\u4e60\u60ef\u4e86\u8fd9\u4e48\u505a\u4ee5\u540e\uff0c\u5b83\u4fbf\u4f1a\u6162\u6162\u5730\u6210\u4e3a\u4f60\u7684\u4e60\u60ef\u3002\u6700\u7ec8\uff0c\u4f60\u5c06\u80fd\u591f\u4e0d\u5728\u8fdb\u884c\u4efb\u4f55\u9884\u5148\u8bbe\u8ba1\u7684\u60c5\u51b5\u4e0b\uff0c\u5b8c\u6210\u6574\u4e2a\u51fd\u6570\u7684\u7f16\u5199\u3002","title":"\u7f16\u7a0b\u5fe0\u8a00"},{"location":"2-2-Function-return-values-value-returning%20functions/","tags":["return","undefined behavior","function"],"text":"\u8bf7\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { // get a value from the user std :: cout << \"Enter an integer: \" ; int num {}; std :: cin >> num ; // print the value doubled std :: cout << num << \" doubled is: \" << num * 2 << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u4ece\u6982\u5ff5\u4e0a\u5c06\u5305\u542b\u4e24\u90e8\u5206\uff1a\u9996\u5148\u6211\u4eec\u4ece\u7528\u6237\u83b7\u53d6\u4e00\u4e2a\u8f93\u5165\uff0c\u7136\u540e\u544a\u8bc9\u7528\u6237\u8be5\u8f93\u5165\u7684\u4e24\u500d\u662f\u591a\u5c11\u3002 \u867d\u7136\u8fd9\u4e2a\u7a0b\u5e8f\u5df2\u7ecf\u8db3\u591f\u7b80\u5355\uff0c\u6ca1\u5fc5\u8981\u5c06\u5176\u5212\u5206\u4e3a\u591a\u4e2a\u51fd\u6570\uff0c\u4f46\u5982\u679c\u6211\u4eec\u6267\u610f\u8981\u8fd9\u4e48\u505a\u5462\uff1f\u4ece\u7528\u6237\u83b7\u53d6\u8f93\u5165\u662f\u4e00\u4e2a\u975e\u5e38\u660e\u786e\u7684\u4efb\u52a1\uff0c\u5c06\u5176\u4f5c\u4e3a\u4e00\u4e2a\u51fd\u6570\u5e94\u8be5\u662f\u5f88\u5408\u9002\u7684\u3002 \u56e0\u6b64\uff0c\u7a0b\u5e8f\u88ab\u4fee\u6539\u6210\u4e86\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 // This program doesn't work #include void getValueFromUser () { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; } int main () { getValueFromUser (); // Ask user for input int num {}; // How do we get the value from getValueFromUser() and use it to initialize this variable? std :: cout << num << \" doubled is: \" << num * 2 << '\\n' ; return 0 ; } \u5c3d\u7ba1\u770b\u4e0a\u53bb\u4e0a\u9762\u7684\u5c1d\u8bd5\u6ca1\u4ec0\u4e48\u95ee\u9898\uff0c\u4f46\u5b9e\u9645\u4e0a\u5b83\u5374\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002 \u5f53\u51fd\u6570 getValueFromUser \u88ab\u8c03\u7528\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u8981\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6574\u578b\u6570\uff0c\u8fd9\u7b26\u5408\u6211\u4eec\u7684\u9884\u671f\u3002\u4f46\u662f\u5f53\u51fd\u6570 getValueFromUser \u8c03\u7528\u7ed3\u675f\u5e76\u8fd4\u56de main \u51fd\u6570\u65f6\uff0c\u521a\u624d\u8f93\u5165\u7684\u503c\u5c31\u88ab\u4e22\u5f03\u4e86\u3002\u53d8\u91cf num \u59cb\u7ec8\u90fd\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u4e3a\u7528\u6237\u8f93\u5165\u7684\u503c\uff0c\u56e0\u6b64\u8f93\u51fa\u7ed3\u679c\u59cb\u7ec8\u662f 0 \u3002 \u95ee\u9898\u51fa\u5728 getValueFromUser \u51fd\u6570\u8fd4\u56de\u5230 main \u51fd\u6570\u7684\u65f6\u5019\uff0c\u6211\u4eec\u6ca1\u6709\u5c06\u7528\u6237\u8f93\u5165\u7684\u503c\u4e00\u8d77\u8fd4\u56de\uff0c\u56e0\u6b64 main \u51fd\u6570\u5c31\u65e0\u6cd5\u4f7f\u7528\u8be5\u503c\u3002 \u8fd4\u56de\u503c \u5728\u7f16\u5199\u51fd\u6570\u7684\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u786e\u5b9a\u8be5\u51fd\u6570\u662f\u5426\u5e94\u8be5\u8fd4\u56de\u4e00\u4e2a\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u5982\u679c\u9700\u8981\uff0c\u6709\u4e24\u4ef6\u4e8b\u9700\u8981\u53bb\u505a\u3002 \u9996\u5148\uff0c\u5fc5\u987b\u6307\u5b9a\u51fd\u6570\u8fd4\u56de\u503c\u7684\u7c7b\u578b\uff0c\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u7684\u5b9a\u4e49\u4f4d\u4e8e\u51fd\u6570\u540d\u524d\u9762\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c getValueFromUser \u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a void \uff08\u8868\u793a\u65e0\u9700\u8fd4\u56de\u4efb\u4f55\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\uff09\uff0c\u800c main \u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u5219\u4e3a int \uff08\u5373\u8fd4\u56de\u4e00\u4e2a int \u7c7b\u578b\u7684\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\uff09\u3002\u6ce8\u610f\uff0c\u8fd9\u91cc\u5e76\u4e0d\u662f\u5b9a\u4e49\u8fd4\u56de\u7684\u5177\u4f53\u662f\u4ec0\u4e48\u503c\uff0c\u800c\u4ec5\u662f\u6307\u5b9a\u5176 \u7c7b\u578b \u3002 \u76f8\u5173\u5185\u5bb9 \u8fd4\u56de\u503c\u4e3a void \u7c7b\u578b\u7684\u51fd\u6570\u5c06\u5728\u4e0b\u8282\u8bfe\u4e2d\u4ecb\u7ecd\uff08 2-3-Void-functions-non-value returning functions \uff09 \u5176\u6b21\uff0c\u5728\u9700\u8981\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4e2d\uff0c\u9700\u8981\u901a\u8fc7 return \u8bed\u53e5\u6765\u6307\u5b9a\u8981\u8fd4\u56de\u7684\u5177\u4f53\u503c\u3002\u8be5\u5177\u4f53\u503c\u79f0\u4e3a\u51fd\u6570\u7684 \u8fd4\u56de\u503c \u3002\u5f53 return \u8bed\u53e5\u88ab\u6267\u884c\u65f6\uff0c\u51fd\u6570\u4f1a\u7acb\u5373\u9000\u51fa\uff0c\u540c\u65f6\u5c06\u8fd4\u56de\u503c \u62f7\u8d1d \u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a \u6309\u503c\u8fd4\u56de \u3002 \u4ee5\u4e0b\u9762\u8fd9\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u4e3a\u4f8b\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include // int \u662f\u8fd4\u56de\u503c\u7c7b\u578b // \u8fd4\u56de\u503c\u7c7b\u578b\u4e3a int \u610f\u5473\u7740\u51fd\u6570\u4f1a\u8fd4\u56de\u67d0\u4e2a\u6574\u578b\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\uff08\u5177\u4f53\u662f\u4ec0\u4e48\u503c\u5e76\u4e0d\u4f1a\u5728\u6b64\u5904\u6307\u5b9a\uff09 int returnFive () { // return \u8bed\u53e5\u6307\u5b9a\u4e86\u8981\u8fd4\u56de\u7684\u5177\u4f53\u503c return 5 ; // \u8fd4\u56de 5 \u7ed9\u4e3b\u8c03\u51fd\u6570 } int main () { std :: cout << returnFive () << '\\n' ; // prints 5 std :: cout << returnFive () + 2 << '\\n' ; // prints 7 returnFive (); // \u51fd\u6570\u8fd4\u56de\u4e86 5\uff0c\u4f46\u662f\u8be5\u503c\u88ab\u5ffd\u7565\u4e86\uff0c\u56e0\u4e3a\u6ca1\u6709\u4f7f\u7528\u8be5\u503c\u505a\u4efb\u4f55\u4e8b\u60c5 return 0 ; } \u51fd\u6570\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 5 7 \u4ee3\u7801\u6267\u884c\u4ece main \u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\uff0c\u7b2c\u4e00\u6761\u8bed\u53e5\u5bf9 returnFive \u8fdb\u884c\u4e86\u6c42\u503c\uff0c\u5373\u8c03\u7528\u4e86\u8be5\u51fd\u6570\u3002\u51fd\u6570 returnFive \u8fd4\u56de\u503c\u4e3a 5\uff0c\u8be5\u503c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u540e\uff0c\u901a\u8fc7 std:: cout \u88ab\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002 \u5728\u7b2c\u4e8c\u5904\u51fd\u6570\u8c03\u7528\uff0c\u8bed\u53e5 returnFive \u8fdb\u884c\u4e86\u6c42\u503c\uff0c \u5176\u7ed3\u679c\u5c31\u662f returnFive \u51fd\u6570\u518d\u6b21\u88ab\u8c03\u7528\u3002\u51fd\u6570\u4ecd\u7136\u5c06\u8fd4\u56de\u503c 5 \u8fd4\u56de\u7ed9\u4e86\u4e3b\u8c03\u51fd\u6570\u3002\u8868\u8fbe\u5f0f 5 + 2 \u7684\u6c42\u503c\u7ed3\u679c\u662f 7\uff0c\u968f\u540e\u8be5\u503c\u901a\u8fc7 std:: cout \u88ab\u6253\u5370\u5230\u4e86\u63a7\u5236\u53f0\u3002 \u7b2c\u4e09\u6761\u8bed\u53e5\u518d\u6b21\u8c03\u7528\u4e86 returnFive \u51fd\u6570\uff0c\u5176\u8fd4\u56de\u503c\u4ecd\u7136\u88ab\u8fd4\u56de\u7ed9\u4e86\u4e3b\u8c03\u51fd\u6570\uff0c\u4f46\u662f main \u51fd\u6570\u5e76\u6ca1\u6709\u5bf9\u8be5\u8fd4\u56de\u503c\u8fdb\u884c\u4efb\u4f55\u64cd\u4f5c\uff0c\u56e0\u6b64\u4ec0\u4e48\u4e8b\u60c5\u90fd\u6ca1\u6709\u53d1\u751f\uff08\u8fd4\u56de\u503c\u88ab\u5ffd\u7565\u4e86\uff09\u3002 \u6ce8\u610f\uff1a\u9664\u975e\u4e3b\u8c03\u51fd\u6570\u901a\u8fc7 std:: cout \u5c06\u8fd4\u56de\u503c\u53d1\u9001\u5230\u63a7\u5236\u53f0\u8fdb\u884c\u6253\u5370\uff0c\u5426\u5219\u8be5\u503c\u5e76\u4e0d\u4f1a\u88ab\u6253\u5370\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6700\u540e\u4e00\u6b21\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u4e3b\u8c03\u51fd\u6570\u6ca1\u6709\u5c06\u8fd4\u56de\u503c\u53d1\u9001\u5230 std:: cout \uff0c\u56e0\u6b64\u5176\u7ed3\u679c\u6ca1\u6709\u6253\u5370\u3002 \u5c0f\u8d34\u58eb \u5f53\u88ab\u8c03\u51fd\u6570\u8fd4\u56de\u503c\u662f\uff0c\u4e3b\u8c03\u51fd\u6570\u53ef\u4ee5\u51b3\u5b9a\u662f\u5426\u5728\u8868\u8fbe\u5f0f\u6216\u8005\u8bed\u53e5\u4e2d\u4f7f\u7528\u8be5\u8fd4\u56de\u503c\uff08\u4f8b\u5982\uff1a\u5c06\u5176\u8d4b\u503c\u7ed9\u67d0\u4e2a\u53d8\u91cf\u6216\u5c06\u5176\u53d1\u9001\u5230 std:: cout \uff09\uff0c\u4e5f\u53ef\u4ee5\u5ffd\u7565\u5b83\uff08\u4e0d\u5bf9\u8fd4\u56de\u503c\u505a\u4efb\u4f55\u64cd\u4f5c\uff09\u3002 \u4fee\u6539\u7a0b\u5e8f \u6709\u4e86\u4e0a\u9762\u7684\u77e5\u8bc6\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u8bfe\u7a0b\u5f00\u59cb\u65f6\u7684\u7a0b\u5e8f\u8fdb\u884c\u9002\u5f53\u7684\u4fee\u6539\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int getValueFromUser () // this function now returns an integer value { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; // return the value the user entered back to the caller } int main () { int num { getValueFromUser () }; // \u4f7f\u7528 getValueFromUser() \u7684\u8fd4\u56de\u503c\u5bf9 num \u8fdb\u884c\u521d\u59cb\u5316 std :: cout << num << \" doubled is: \" << num * 2 << '\\n' ; return 0 ; } \u5f53\u4e0a\u8ff0\u7a0b\u5e8f\u5f00\u59cb\u6267\u884c\u65f6\uff0c main \u51fd\u6570\u4e2d\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u4f1a\u521b\u5efa\u4e00\u4e2a int \u7c7b\u578b\u7684\u53d8\u91cf num \u3002\u5f53\u7a0b\u5e8f\u51c6\u5907\u8981\u5bf9 num \u521d\u59cb\u5316\u65f6\uff0c\u5b83\u4f1a\u53d1\u73b0\u6b64\u5904\u6709\u4e00\u4e2a\u51fd\u6570\u8c03\u7528 getValueFromUser() \uff0c\u56e0\u6b64\u5b83\u4f1a\u8df3\u8f6c\u53bb\u6267\u884c\u8be5\u51fd\u6570\u3002\u51fd\u6570 getValueFromUser \u8981\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u503c\uff0c\u7136\u540e\u5b83\u5c06\u8be5\u8f93\u5165\u503c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570( main )\u3002\u8be5\u8fd4\u56de\u503c\u88ab\u7528\u6765\u5bf9\u53d8\u91cf num \u8fdb\u884c\u521d\u59cb\u5316\u3002 \u8bf7\u81ea\u884c\u7f16\u8bd1\u8be5\u51fd\u6570\u5e76\u8fd0\u884c\u5176\u51e0\u6b21\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u80fd\u591f\u6b63\u786e\u8fd0\u884c\u3002 \u8fd4\u56de\u5230\u4e3b\u51fd\u6570 \u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u601d\u7ef4\u5de5\u5177\u6765\u7406\u89e3 main \u51fd\u6570\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u5f53\u7a0b\u5e8f\u6267\u884c\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u53d6\u8c03\u7528 main \u51fd\u6570\u3002\u968f\u540e\u7a0b\u5e8f\u5c31\u4f1a\u8df3\u8f6c\u5230 main \u51fd\u6570\u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\u3002\u51fd\u6570\u4f53\u4e2d\u7684\u8bed\u53e5\u4f1a\u987a\u5e8f\u6267\u884c\u3002\u6700\u7ec8\uff0c main \u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\u503c\uff08\u901a\u5e38\u662f 0\uff09\uff0c\u7136\u540e\u7a0b\u5e8f\u5c31\u7ec8\u6b62\u4e86\u3002 main \u51fd\u6570\u7684\u7684\u8fd4\u56de\u503c\u901a\u5e38\u79f0\u4e3a\u72b6\u6001\u7801\uff08\u6709\u65f6\u4e5f\u79f0\u4e3a\u9000\u51fa\u72b6\u6001\u7801\uff0c\u6781\u5c11\u6570\u60c5\u51b5\u4e0b\u4e5f\u79f0\u4e3a\u8fd4\u56de\u7801\uff09\uff0c\u56e0\u4e3a\u8be5\u8fd4\u56de\u503c\u7528\u6765\u53cd\u6620\u7a0b\u5e8f\u662f\u5426\u6b63\u786e\u6267\u884c\u3002 \u6839\u636e\u5b9a\u4e49\uff0c\u72b6\u6001\u7801\u4e3a 0 \u8868\u793a\u7a0b\u5e8f\u6267\u884c\u6210\u529f\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u7a0b\u5e8f\u6b63\u5e38\u8fd0\u884c\uff0c main \u7684\u8fd4\u56de\u503c\u5e94\u8be5\u4e3a 0 \u3002 \u975e 0 \u7684\u8fd4\u56de\u503c\u901a\u5e38\u7528\u6765\u8868\u793a\u7a0b\u5e8f\u6267\u884c\u5931\u8d25\uff08\u5c3d\u7ba1\u5927\u591a\u6570\u64cd\u4f5c\u7cfb\u7edf\u90fd\u6309\u7167\u4e0a\u8ff0\u7ea6\u5b9a\u6765\u5bf9\u5f85\u8fd4\u56de\u503c\uff0c\u4f46\u662f\u4e25\u683c\u6765\u8bb2\uff0c\u5176\u53ef\u79fb\u690d\u6027\u5e76\u4e0d\u80fd\u5f97\u5230\u4fdd\u8bc1\uff09\u3002 \u6269\u5c55\u9605\u8bfb C++\u6807\u51c6\u4e2d\u503c\u5b9a\u4e49\u4e86\u4e09\u79cd\u8fd4\u56de\u72b6\u6001\u7801\uff1a0\u3001 EXIT_SUCCESS \u548c EXIT_FAILURE \u30020 \u548c EXIT_SUCCESS \u90fd\u8868\u793a\u7a0b\u5e8f\u6267\u884c\u6210\u529f\u3002 EXIT_FAILURE \u5219\u8868\u793a\u7a0b\u5e8f\u6ca1\u6709\u6210\u529f\u6267\u884c\u3002 EXIT_SUCCESS \u548c EXIT_FAILURE \u5728 \u5934\u6587\u4ef6\u4e2d\u58f0\u660e\uff1a 1 2 3 4 5 6 #include // for EXIT_SUCCESS and EXIT_FAILURE int main () { return EXIT_SUCCESS ; } \u5982\u679c\u4f60\u60f3\u5c3d\u53ef\u80fd\u4fdd\u8bc1\u53ef\u79fb\u690d\u6027\uff0c\u8bf7\u4f7f\u7528 0 \u6216\u8005 EXIT_SUCCESS \u8868\u793a\u7a0b\u5e8f\u6210\u529f\u6267\u884c\uff0c\u6216\u8005\u4f7f\u7528 EXIT_FAILURE \u8868\u793a\u7a0b\u5e8f\u6267\u884c\u5931\u8d25\u3002 C++ \u4e0d\u5141\u8bb8\u663e\u5f0f\u8c03\u7528 main \u51fd\u6570\u3002 \u5c31\u76ee\u524d\u800c\u8a00\uff0c\u4f60\u8fd8\u5e94\u8be5\u5c06 main \u51fd\u6570\u5b9a\u4e49\u5728\u6587\u4ef6\u7684\u6700\u4e0b\u65b9\uff0c\u5373\u5b9a\u4e49\u5728\u5176\u4ed6\u6240\u6709\u51fd\u6570\u540e\u9762\u3002 \u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4e0d\u8fd4\u56de\u503c\u5219\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a \u4e00\u4e2a\u4f1a\u8fd4\u56de\u503c\u7684\u51fd\u6570\uff0c\u79f0\u4e3a\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570(value-returning function)\uff0c\u9664\u975e\u8fd4\u56de\u503c\u7c7b\u578b\u662f void \uff0c\u5426\u5219\u90fd\u5c5e\u4e8e\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u3002 \u4e00\u4e2a\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u5fc5\u987b\u8fd4\u56de\u5bf9\u5e94\u7c7b\u578b\u7684\u503c\uff08\u4f7f\u7528 return \u8bed\u53e5\uff09\uff0c\u5426\u5219\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u5728 1-6-Uninitialized-variables-and-undefined-behavior \u4e2d\u8ba8\u8bba\u4e86\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u51fd\u6570\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int getValueFromUser () // this function returns an integer value { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; // \u6ce8\u610f\uff1a\u6ca1\u6709 return \u8bed\u53e5 } int main () { int num { getValueFromUser () }; // initialize num with the return value of getValueFromUser() std :: cout << num << \" doubled is: \" << num * 2 << '\\n' ; return 0 ; } \u5728\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u73b0\u4ee3\u7f16\u8bd1\u5668\u4f1a\u4ea7\u751f\u9519\u8bef\u4fe1\u606f\uff0c\u56e0\u4e3a getValueFromUser \u5b9a\u4e49\u4e86 int \u7c7b\u578b\u7684\u8fd4\u56de\u503c\u4f46\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\u5305\u542b return \u8bed\u53e5\u3002\u7136\u800c\uff0c\u5982\u679c\u7a0b\u5e8f\u51fa\u4e8e\u67d0\u79cd\u539f\u56e0\u7f16\u8bd1\u901a\u8fc7\u4e86\uff0c\u90a3\u4e48\u5728\u8fd0\u884c\u4e0a\u8ff0\u7a0b\u5e8f\u65f6\u5c31\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u56e0\u4e3a getValueFromUser() \u662f\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\uff0c\u4f46\u5e76\u6ca1\u6709\u8fd4\u56de\u4e00\u4e2a\u503c\u3002 \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u4f60\u5fd8\u8bb0\u8fd4\u56de\u503c\uff0c\u7f16\u8bd1\u5668\u662f\u80fd\u591f\u68c0\u6d4b\u5230\u7684\u3002\u4f46\u662f\uff0c\u6709\u4e00\u4e9b\u975e\u5e38\u590d\u6742\u7684\u60c5\u51b5\uff0c\u7f16\u8bd1\u5668\u6709\u53ef\u80fd\u65e0\u6cd5\u786e\u5b9a\u51fd\u6570\u662f\u5426\u8fd4\u56de\u503c\u7684\u3002\u6240\u4ee5\u4f60\u4e0d\u5e94\u8be5\u4f9d\u8d56\u7f16\u8bd1\u5668\u3002 \u6700\u4f73\u5b9e\u8df5 \u786e\u4fdd\u975e void \u7c7b\u578b\u7684\u51fd\u6570\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u90fd\u8fd4\u56de\u503c\u3002 \u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4e0d\u8fd4\u56de\u503c\u5219\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 main \u51fd\u6570\u5982\u679c\u6ca1\u6709\u4f7f\u7528 return \u8bed\u53e5\u5219\u4f1a\u9690\u5f0f\u5730\u8fd4\u56de 0 \u4e0a\u8ff0\u539f\u5219\u6709\u4e00\u4e2a\u7279\u4f8b\uff0c\u90a3\u5c31\u662fmain\u51fd\u6570\u3002main \u51fd\u6570\u5982\u679c\u6ca1\u6709\u4f7f\u7528 return \u8bed\u53e5\u5219\u4f1a\u9690\u5f0f\u5730\u8fd4\u56de 0\u3002\u5373\u4fbf\u5982\u6b64\uff0c\u6700\u4f73\u5b9e\u8df5\u4ecd\u7136\u662f\u663e\u5f0f\u5730\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u8fd9\u6837\u65e2\u80fd\u591f\u8868\u660e\u4f60\u7684\u5b9e\u9645\u610f\u56fe\uff0c\u4e5f\u80fd\u591f\u5728\u5f62\u5f0f\u4e0a\u548c\u5176\u4ed6\u51fd\u6570\u4fdd\u6301\u4e00\u81f4(\u53ef\u4ee5\u9884\u9632\u4f60\u5ffd\u7565\u5176\u4ed6\u51fd\u6570\u7684\u8fd4\u56de\u503c)\u3002 \u51fd\u6570\u53ea\u80fd\u8fd4\u56de\u4e00\u4e2a\u5355\u503c \u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4f9d\u6b21\u53ea\u80fd\u8fd4\u56de\u4e00\u4e2a\u5355\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002 \u6ce8\u610f\uff0c return \u8bed\u53e5\u4e2d\u7684\u503c\u4e0d\u4e00\u5b9a\u8981\u7d20\u5b57\u9762\u91cf\u2014\u2014\u4e5f\u53ef\u4ee5\u662f\u4efb\u4f55\u8868\u8fbe\u5f0f\u7684\u503c\uff0c\u5305\u62ec\u53d8\u91cf\uff0c\u751a\u81f3\u53ef\u4ee5\u662f\u5176\u4ed6\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002 getValueFromUser() \u7684\u4f8b\u5b50\u4e2d\uff0c\u8fd4\u56de\u503c\u662f\u53d8\u91cf input \uff0c\u5176\u4e2d\u5305\u542b\u4e86\u7528\u6237\u8f93\u5165\u7684\u5b9e\u9645\u503c\u3002 \u901a\u8fc7\u4e00\u4e9b\u65b9\u6cd5\u662f\u53ef\u4ee5\u7ed5\u8fc7\u51fd\u6570\u53ea\u80fd\u8fd4\u56de\u5355\u4e00\u503c\u7684\u9650\u5236\u7684\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u51fd\u6570\u7684\u7f16\u5199\u8005\u53ef\u4ee5\u51b3\u5b9a\u8fd4\u56de\u503c\u7684\u542b\u4e49 \u51fd\u6570\u8fd4\u56de\u503c\u7684\u542b\u4e49\u662f\u7531\u51fd\u6570\u7f16\u5199\u8005\u51b3\u5b9a\u7684\u3002\u6709\u4e9b\u51fd\u6570\u4f1a\u5c06\u72b6\u6001\u7801\u4f5c\u4e3a\u8fd4\u56de\u503c\u6765\u8868\u793a\u51fd\u6570\u662f\u5426\u6267\u884c\u6210\u529f\u3002\u6709\u7684\u51fd\u6570\u5219\u5c06\u8ba1\u7b97\u7ed3\u679c\u6216\u5176\u4ed6\u9009\u5b9a\u7684\u503c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u8fd8\u6709\u4e9b\u51fd\u6570\u4ec0\u4e48\u90fd\u4e0d\u8fd4\u56de\uff08\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\u4e2d\u4ecb\u7ecd\u6b64\u7c7b\u51fd\u6570\uff09\u3002 \u56e0\u4e3a\u51fd\u6570\u8fd4\u56de\u503c\u7684\u542b\u4e49\u53ef\u80fd\u662f\u5404\u79cd\u5404\u6837\u7684\uff0c\u56e0\u6b64\u901a\u8fc7\u6ce8\u91ca\u7684\u65b9\u5f0f\u5bf9\u51fd\u6570\u8fd4\u56de\u503c\u7684\u542b\u4e49\u8fdb\u884c\u63cf\u8ff0\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 // \u51fd\u6570\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u503c // \u8fd4\u56de\u503c\u662f\u7528\u6237\u4ece\u952e\u76d8\u8f93\u5165\u7684\u503c\uff0c\u6574\u578b int getValueFromUser () { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; // \u5c06\u7528\u6237\u6570\u7ed3\u679c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570 } \u51fd\u6570\u91cd\u7528 \u63a5\u4e0b\u6765\uff0c\u4ecb\u7ecd\u51fd\u6570\u91cd\u7528\u7684\u5178\u578b\u4f8b\u5b50\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { int x {}; std :: cout << \"Enter an integer: \" ; std :: cin >> x ; int y {}; std :: cout << \"Enter an integer: \" ; std :: cin >> y ; std :: cout << x << \" + \" << y << \" = \" << x + y << '\\n' ; return 0 ; } \u867d\u7136\u8fd9\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\uff0c\u4f46\u662f\u4ee3\u7801\u6709\u4e9b\u5197\u4f59\u3002\u5b9e\u9645\u4e0a\u8fd9\u4e2a\u51fd\u6570\u8fdd\u53cd\u4e86\u597d\u4ee3\u7801\u7684\u6838\u5fc3\u4fe1\u6761\uff1a\u4e0d\u8981\u91cd\u590d\u4f60\u81ea\u5df1\u2014\u2014Don\u2019t Repeat Yourself (\u5e38\u5e38\u7f29\u5199\u4e3aDRY)\u3002 \u4e3a\u4ec0\u4e48\u91cd\u590d\u7684\u4ee3\u7801\u662f\u4e0d\u597d\u7684\uff1f\u5982\u679c\u4f60\u60f3\u8981\u5c06\u6587\u672c \u201cEnter an integer:\u201d \u4fee\u6539\u6210\u5176\u4ed6\u5185\u5bb9\uff0c\u90a3\u4e48\u4f60\u5fc5\u987b\u8981\u4fee\u6539\u4e24\u4e2a\u5730\u65b9\u3002\u5982\u679c\u8981\u521d\u59cb\u5316\u7684\u53d8\u91cf\u670910\u4e2a\u800c\u4e0d\u662f\u4e24\u4e2a\u5462\uff1f\u8fd9\u65e0\u7591\u5c06\u4f1a\u4ea7\u751f\u5927\u91cf\u7684\u5197\u4f59\u4ee3\u7801\uff08\u4f7f\u5f97\u7a0b\u5e8f\u53d8\u5f97\u53c8\u957f\u53c8\u96be\u4ee5\u7406\u89e3\uff09\uff0c\u8fd8\u4e3a\u62fc\u5199\u9519\u8bef\u63d0\u4f9b\u4e86\u6ecb\u751f\u7684\u7a7a\u95f4\u3002 \u4e0b\u9762\uff0c\u4f7f\u7528 getValueFromUser \u51fd\u6570\u6765\u91cd\u6784\u4e0a\u8ff0\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int getValueFromUser () { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; } int main () { int x { getValueFromUser () }; // first call to getValueFromUser int y { getValueFromUser () }; // second call to getValueFromUser std :: cout << x << \" + \" << y << \" = \" << x + y << '\\n' ; return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 Enter an integer: 5 Enter an integer: 7 5 + 7 = 12 \u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\uff0c getValueFromUser \u88ab\u8c03\u7528\u4e86\u91cf\u8bcd\uff0c\u4e00\u6b21\u7528\u4e8e\u521d\u59cb\u5316\u53d8\u91cf x \uff0c\u4e00\u6b21\u7528\u4e8e\u521d\u59cb\u5316\u53d8\u91cf y \u3002\u4f7f\u7528\u8be5\u51fd\u6570\u53ef\u4ee5\u6709\u6548\u5730\u907f\u514d\u91cd\u590d\u4ee3\u7801\uff0c\u4e5f\u51cf\u5c11\u4e86\u72af\u9519\u7684\u6982\u7387\u3002\u53ea\u8981 getValueFromUser \u51fd\u6570\u80fd\u591f\u6b63\u786e\u5de5\u4f5c\uff0c\u90a3\u4e48\u4fbf\u53ef\u4ee5\u591a\u6b21\u8c03\u7528\u5b83\u3002 \u8fd9\u4e5f\u662f\u6a21\u5757\u5316\u7f16\u7a0b\u7684\u7cbe\u9ad3\uff1a\u7f16\u5199\u51fd\u6570\u3001\u6d4b\u8bd5\u51fd\u6570\u4ee5\u786e\u4fdd\u5b83\u80fd\u591f\u6b63\u786e\u5de5\u4f5c\uff0c\u7136\u540e\u4fbf\u53ef\u4ee5\u5728\u63a5\u4e0b\u6765\u7684\u5de5\u4f5c\u4e2d\u53cd\u590d\u4f7f\u7528\u8be5\u51fd\u6570\uff08\u53ea\u8981\u6211\u4eec\u6ca1\u6709\u4fee\u6539\u8fd9\u4e2a\u51fd\u6570\u2014\u2014\u5426\u5219\u5fc5\u987b\u5bf9\u8be5\u51fd\u6570\u91cd\u65b0\u6d4b\u8bd5\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u8bf7\u9075\u5faaDRY\u539f\u5219\uff0c\u5373\u4e0d\u8981\"\u91cd\u590d\u4f60\u81ea\u5df1\u7684\u5de5\u4f5c\"\u3002\u5982\u679c\u6709\u4ef6\u4e8b\u9700\u8981\u505a\u591a\u6b21\uff0c\u8bf7\u8003\u8651\u662f\u5426\u53ef\u4ee5\u4fee\u6539\u4ee3\u7801\uff0c\u5c3d\u53ef\u80fd\u79fb\u9664\u5197\u4f59\u7684\u90e8\u5206\u3002\u53d8\u91cf\u53ef\u4ee5\u7528\u6765\u5b58\u653e\u88ab\u8ba1\u7b97\u51fa\u6765\u4e14\u9700\u8981\u591a\u6b21\u4f7f\u7528\u7684\u6570\u636e\uff08\u8fd9\u6837\u5c31\u4e0d\u9700\u8981\u8fdb\u884c\u91cd\u590d\u8ba1\u7b97\uff09\u3002\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u5b9a\u4e49\u4e00\u7ec4\u53ef\u80fd\u4f1a\u88ab\u591a\u6b21\u6267\u884c\u7684\u6307\u4ee4\u3002\u5faa\u73af\uff08\u540e\u9762\u7684\u7ae0\u8282\u4e2d\u4f1a\u4ecb\u7ecd\uff09\u5219\u53ef\u4ee5\u7528\u6765\u591a\u6b21\u6267\u884c\u8bed\u53e5\u3002 \u9898\u5916\u8bdd DRY \u7684\u53cd\u4e49\u8bcd\u662f WET (\u201cWrite everything twice\u201d\u2014\u2014\u4efb\u4f55\u4ee3\u7801\u90fd\u5199\u4e24\u904d).","title":"2.2 - \u51fd\u6570\u8fd4\u56de\u503c"},{"location":"2-2-Function-return-values-value-returning%20functions/#_1","text":"\u5728\u7f16\u5199\u51fd\u6570\u7684\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u786e\u5b9a\u8be5\u51fd\u6570\u662f\u5426\u5e94\u8be5\u8fd4\u56de\u4e00\u4e2a\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u5982\u679c\u9700\u8981\uff0c\u6709\u4e24\u4ef6\u4e8b\u9700\u8981\u53bb\u505a\u3002 \u9996\u5148\uff0c\u5fc5\u987b\u6307\u5b9a\u51fd\u6570\u8fd4\u56de\u503c\u7684\u7c7b\u578b\uff0c\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u7684\u5b9a\u4e49\u4f4d\u4e8e\u51fd\u6570\u540d\u524d\u9762\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c getValueFromUser \u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a void \uff08\u8868\u793a\u65e0\u9700\u8fd4\u56de\u4efb\u4f55\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\uff09\uff0c\u800c main \u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u5219\u4e3a int \uff08\u5373\u8fd4\u56de\u4e00\u4e2a int \u7c7b\u578b\u7684\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\uff09\u3002\u6ce8\u610f\uff0c\u8fd9\u91cc\u5e76\u4e0d\u662f\u5b9a\u4e49\u8fd4\u56de\u7684\u5177\u4f53\u662f\u4ec0\u4e48\u503c\uff0c\u800c\u4ec5\u662f\u6307\u5b9a\u5176 \u7c7b\u578b \u3002 \u76f8\u5173\u5185\u5bb9 \u8fd4\u56de\u503c\u4e3a void \u7c7b\u578b\u7684\u51fd\u6570\u5c06\u5728\u4e0b\u8282\u8bfe\u4e2d\u4ecb\u7ecd\uff08 2-3-Void-functions-non-value returning functions \uff09 \u5176\u6b21\uff0c\u5728\u9700\u8981\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4e2d\uff0c\u9700\u8981\u901a\u8fc7 return \u8bed\u53e5\u6765\u6307\u5b9a\u8981\u8fd4\u56de\u7684\u5177\u4f53\u503c\u3002\u8be5\u5177\u4f53\u503c\u79f0\u4e3a\u51fd\u6570\u7684 \u8fd4\u56de\u503c \u3002\u5f53 return \u8bed\u53e5\u88ab\u6267\u884c\u65f6\uff0c\u51fd\u6570\u4f1a\u7acb\u5373\u9000\u51fa\uff0c\u540c\u65f6\u5c06\u8fd4\u56de\u503c \u62f7\u8d1d \u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a \u6309\u503c\u8fd4\u56de \u3002 \u4ee5\u4e0b\u9762\u8fd9\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u4e3a\u4f8b\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include // int \u662f\u8fd4\u56de\u503c\u7c7b\u578b // \u8fd4\u56de\u503c\u7c7b\u578b\u4e3a int \u610f\u5473\u7740\u51fd\u6570\u4f1a\u8fd4\u56de\u67d0\u4e2a\u6574\u578b\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\uff08\u5177\u4f53\u662f\u4ec0\u4e48\u503c\u5e76\u4e0d\u4f1a\u5728\u6b64\u5904\u6307\u5b9a\uff09 int returnFive () { // return \u8bed\u53e5\u6307\u5b9a\u4e86\u8981\u8fd4\u56de\u7684\u5177\u4f53\u503c return 5 ; // \u8fd4\u56de 5 \u7ed9\u4e3b\u8c03\u51fd\u6570 } int main () { std :: cout << returnFive () << '\\n' ; // prints 5 std :: cout << returnFive () + 2 << '\\n' ; // prints 7 returnFive (); // \u51fd\u6570\u8fd4\u56de\u4e86 5\uff0c\u4f46\u662f\u8be5\u503c\u88ab\u5ffd\u7565\u4e86\uff0c\u56e0\u4e3a\u6ca1\u6709\u4f7f\u7528\u8be5\u503c\u505a\u4efb\u4f55\u4e8b\u60c5 return 0 ; } \u51fd\u6570\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 5 7 \u4ee3\u7801\u6267\u884c\u4ece main \u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\uff0c\u7b2c\u4e00\u6761\u8bed\u53e5\u5bf9 returnFive \u8fdb\u884c\u4e86\u6c42\u503c\uff0c\u5373\u8c03\u7528\u4e86\u8be5\u51fd\u6570\u3002\u51fd\u6570 returnFive \u8fd4\u56de\u503c\u4e3a 5\uff0c\u8be5\u503c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u540e\uff0c\u901a\u8fc7 std:: cout \u88ab\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002 \u5728\u7b2c\u4e8c\u5904\u51fd\u6570\u8c03\u7528\uff0c\u8bed\u53e5 returnFive \u8fdb\u884c\u4e86\u6c42\u503c\uff0c \u5176\u7ed3\u679c\u5c31\u662f returnFive \u51fd\u6570\u518d\u6b21\u88ab\u8c03\u7528\u3002\u51fd\u6570\u4ecd\u7136\u5c06\u8fd4\u56de\u503c 5 \u8fd4\u56de\u7ed9\u4e86\u4e3b\u8c03\u51fd\u6570\u3002\u8868\u8fbe\u5f0f 5 + 2 \u7684\u6c42\u503c\u7ed3\u679c\u662f 7\uff0c\u968f\u540e\u8be5\u503c\u901a\u8fc7 std:: cout \u88ab\u6253\u5370\u5230\u4e86\u63a7\u5236\u53f0\u3002 \u7b2c\u4e09\u6761\u8bed\u53e5\u518d\u6b21\u8c03\u7528\u4e86 returnFive \u51fd\u6570\uff0c\u5176\u8fd4\u56de\u503c\u4ecd\u7136\u88ab\u8fd4\u56de\u7ed9\u4e86\u4e3b\u8c03\u51fd\u6570\uff0c\u4f46\u662f main \u51fd\u6570\u5e76\u6ca1\u6709\u5bf9\u8be5\u8fd4\u56de\u503c\u8fdb\u884c\u4efb\u4f55\u64cd\u4f5c\uff0c\u56e0\u6b64\u4ec0\u4e48\u4e8b\u60c5\u90fd\u6ca1\u6709\u53d1\u751f\uff08\u8fd4\u56de\u503c\u88ab\u5ffd\u7565\u4e86\uff09\u3002 \u6ce8\u610f\uff1a\u9664\u975e\u4e3b\u8c03\u51fd\u6570\u901a\u8fc7 std:: cout \u5c06\u8fd4\u56de\u503c\u53d1\u9001\u5230\u63a7\u5236\u53f0\u8fdb\u884c\u6253\u5370\uff0c\u5426\u5219\u8be5\u503c\u5e76\u4e0d\u4f1a\u88ab\u6253\u5370\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6700\u540e\u4e00\u6b21\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u4e3b\u8c03\u51fd\u6570\u6ca1\u6709\u5c06\u8fd4\u56de\u503c\u53d1\u9001\u5230 std:: cout \uff0c\u56e0\u6b64\u5176\u7ed3\u679c\u6ca1\u6709\u6253\u5370\u3002 \u5c0f\u8d34\u58eb \u5f53\u88ab\u8c03\u51fd\u6570\u8fd4\u56de\u503c\u662f\uff0c\u4e3b\u8c03\u51fd\u6570\u53ef\u4ee5\u51b3\u5b9a\u662f\u5426\u5728\u8868\u8fbe\u5f0f\u6216\u8005\u8bed\u53e5\u4e2d\u4f7f\u7528\u8be5\u8fd4\u56de\u503c\uff08\u4f8b\u5982\uff1a\u5c06\u5176\u8d4b\u503c\u7ed9\u67d0\u4e2a\u53d8\u91cf\u6216\u5c06\u5176\u53d1\u9001\u5230 std:: cout \uff09\uff0c\u4e5f\u53ef\u4ee5\u5ffd\u7565\u5b83\uff08\u4e0d\u5bf9\u8fd4\u56de\u503c\u505a\u4efb\u4f55\u64cd\u4f5c\uff09\u3002","title":"\u8fd4\u56de\u503c"},{"location":"2-2-Function-return-values-value-returning%20functions/#_2","text":"\u6709\u4e86\u4e0a\u9762\u7684\u77e5\u8bc6\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u8bfe\u7a0b\u5f00\u59cb\u65f6\u7684\u7a0b\u5e8f\u8fdb\u884c\u9002\u5f53\u7684\u4fee\u6539\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int getValueFromUser () // this function now returns an integer value { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; // return the value the user entered back to the caller } int main () { int num { getValueFromUser () }; // \u4f7f\u7528 getValueFromUser() \u7684\u8fd4\u56de\u503c\u5bf9 num \u8fdb\u884c\u521d\u59cb\u5316 std :: cout << num << \" doubled is: \" << num * 2 << '\\n' ; return 0 ; } \u5f53\u4e0a\u8ff0\u7a0b\u5e8f\u5f00\u59cb\u6267\u884c\u65f6\uff0c main \u51fd\u6570\u4e2d\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u4f1a\u521b\u5efa\u4e00\u4e2a int \u7c7b\u578b\u7684\u53d8\u91cf num \u3002\u5f53\u7a0b\u5e8f\u51c6\u5907\u8981\u5bf9 num \u521d\u59cb\u5316\u65f6\uff0c\u5b83\u4f1a\u53d1\u73b0\u6b64\u5904\u6709\u4e00\u4e2a\u51fd\u6570\u8c03\u7528 getValueFromUser() \uff0c\u56e0\u6b64\u5b83\u4f1a\u8df3\u8f6c\u53bb\u6267\u884c\u8be5\u51fd\u6570\u3002\u51fd\u6570 getValueFromUser \u8981\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u503c\uff0c\u7136\u540e\u5b83\u5c06\u8be5\u8f93\u5165\u503c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570( main )\u3002\u8be5\u8fd4\u56de\u503c\u88ab\u7528\u6765\u5bf9\u53d8\u91cf num \u8fdb\u884c\u521d\u59cb\u5316\u3002 \u8bf7\u81ea\u884c\u7f16\u8bd1\u8be5\u51fd\u6570\u5e76\u8fd0\u884c\u5176\u51e0\u6b21\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u80fd\u591f\u6b63\u786e\u8fd0\u884c\u3002","title":"\u4fee\u6539\u7a0b\u5e8f"},{"location":"2-2-Function-return-values-value-returning%20functions/#_3","text":"\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u601d\u7ef4\u5de5\u5177\u6765\u7406\u89e3 main \u51fd\u6570\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u5f53\u7a0b\u5e8f\u6267\u884c\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u53d6\u8c03\u7528 main \u51fd\u6570\u3002\u968f\u540e\u7a0b\u5e8f\u5c31\u4f1a\u8df3\u8f6c\u5230 main \u51fd\u6570\u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\u3002\u51fd\u6570\u4f53\u4e2d\u7684\u8bed\u53e5\u4f1a\u987a\u5e8f\u6267\u884c\u3002\u6700\u7ec8\uff0c main \u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\u503c\uff08\u901a\u5e38\u662f 0\uff09\uff0c\u7136\u540e\u7a0b\u5e8f\u5c31\u7ec8\u6b62\u4e86\u3002 main \u51fd\u6570\u7684\u7684\u8fd4\u56de\u503c\u901a\u5e38\u79f0\u4e3a\u72b6\u6001\u7801\uff08\u6709\u65f6\u4e5f\u79f0\u4e3a\u9000\u51fa\u72b6\u6001\u7801\uff0c\u6781\u5c11\u6570\u60c5\u51b5\u4e0b\u4e5f\u79f0\u4e3a\u8fd4\u56de\u7801\uff09\uff0c\u56e0\u4e3a\u8be5\u8fd4\u56de\u503c\u7528\u6765\u53cd\u6620\u7a0b\u5e8f\u662f\u5426\u6b63\u786e\u6267\u884c\u3002 \u6839\u636e\u5b9a\u4e49\uff0c\u72b6\u6001\u7801\u4e3a 0 \u8868\u793a\u7a0b\u5e8f\u6267\u884c\u6210\u529f\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u7a0b\u5e8f\u6b63\u5e38\u8fd0\u884c\uff0c main \u7684\u8fd4\u56de\u503c\u5e94\u8be5\u4e3a 0 \u3002 \u975e 0 \u7684\u8fd4\u56de\u503c\u901a\u5e38\u7528\u6765\u8868\u793a\u7a0b\u5e8f\u6267\u884c\u5931\u8d25\uff08\u5c3d\u7ba1\u5927\u591a\u6570\u64cd\u4f5c\u7cfb\u7edf\u90fd\u6309\u7167\u4e0a\u8ff0\u7ea6\u5b9a\u6765\u5bf9\u5f85\u8fd4\u56de\u503c\uff0c\u4f46\u662f\u4e25\u683c\u6765\u8bb2\uff0c\u5176\u53ef\u79fb\u690d\u6027\u5e76\u4e0d\u80fd\u5f97\u5230\u4fdd\u8bc1\uff09\u3002 \u6269\u5c55\u9605\u8bfb C++\u6807\u51c6\u4e2d\u503c\u5b9a\u4e49\u4e86\u4e09\u79cd\u8fd4\u56de\u72b6\u6001\u7801\uff1a0\u3001 EXIT_SUCCESS \u548c EXIT_FAILURE \u30020 \u548c EXIT_SUCCESS \u90fd\u8868\u793a\u7a0b\u5e8f\u6267\u884c\u6210\u529f\u3002 EXIT_FAILURE \u5219\u8868\u793a\u7a0b\u5e8f\u6ca1\u6709\u6210\u529f\u6267\u884c\u3002 EXIT_SUCCESS \u548c EXIT_FAILURE \u5728 \u5934\u6587\u4ef6\u4e2d\u58f0\u660e\uff1a 1 2 3 4 5 6 #include // for EXIT_SUCCESS and EXIT_FAILURE int main () { return EXIT_SUCCESS ; } \u5982\u679c\u4f60\u60f3\u5c3d\u53ef\u80fd\u4fdd\u8bc1\u53ef\u79fb\u690d\u6027\uff0c\u8bf7\u4f7f\u7528 0 \u6216\u8005 EXIT_SUCCESS \u8868\u793a\u7a0b\u5e8f\u6210\u529f\u6267\u884c\uff0c\u6216\u8005\u4f7f\u7528 EXIT_FAILURE \u8868\u793a\u7a0b\u5e8f\u6267\u884c\u5931\u8d25\u3002 C++ \u4e0d\u5141\u8bb8\u663e\u5f0f\u8c03\u7528 main \u51fd\u6570\u3002 \u5c31\u76ee\u524d\u800c\u8a00\uff0c\u4f60\u8fd8\u5e94\u8be5\u5c06 main \u51fd\u6570\u5b9a\u4e49\u5728\u6587\u4ef6\u7684\u6700\u4e0b\u65b9\uff0c\u5373\u5b9a\u4e49\u5728\u5176\u4ed6\u6240\u6709\u51fd\u6570\u540e\u9762\u3002","title":"\u8fd4\u56de\u5230\u4e3b\u51fd\u6570"},{"location":"2-2-Function-return-values-value-returning%20functions/#_4","text":"\u4e00\u4e2a\u4f1a\u8fd4\u56de\u503c\u7684\u51fd\u6570\uff0c\u79f0\u4e3a\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570(value-returning function)\uff0c\u9664\u975e\u8fd4\u56de\u503c\u7c7b\u578b\u662f void \uff0c\u5426\u5219\u90fd\u5c5e\u4e8e\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u3002 \u4e00\u4e2a\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u5fc5\u987b\u8fd4\u56de\u5bf9\u5e94\u7c7b\u578b\u7684\u503c\uff08\u4f7f\u7528 return \u8bed\u53e5\uff09\uff0c\u5426\u5219\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u5728 1-6-Uninitialized-variables-and-undefined-behavior \u4e2d\u8ba8\u8bba\u4e86\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u51fd\u6570\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int getValueFromUser () // this function returns an integer value { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; // \u6ce8\u610f\uff1a\u6ca1\u6709 return \u8bed\u53e5 } int main () { int num { getValueFromUser () }; // initialize num with the return value of getValueFromUser() std :: cout << num << \" doubled is: \" << num * 2 << '\\n' ; return 0 ; } \u5728\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u73b0\u4ee3\u7f16\u8bd1\u5668\u4f1a\u4ea7\u751f\u9519\u8bef\u4fe1\u606f\uff0c\u56e0\u4e3a getValueFromUser \u5b9a\u4e49\u4e86 int \u7c7b\u578b\u7684\u8fd4\u56de\u503c\u4f46\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\u5305\u542b return \u8bed\u53e5\u3002\u7136\u800c\uff0c\u5982\u679c\u7a0b\u5e8f\u51fa\u4e8e\u67d0\u79cd\u539f\u56e0\u7f16\u8bd1\u901a\u8fc7\u4e86\uff0c\u90a3\u4e48\u5728\u8fd0\u884c\u4e0a\u8ff0\u7a0b\u5e8f\u65f6\u5c31\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u56e0\u4e3a getValueFromUser() \u662f\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\uff0c\u4f46\u5e76\u6ca1\u6709\u8fd4\u56de\u4e00\u4e2a\u503c\u3002 \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u4f60\u5fd8\u8bb0\u8fd4\u56de\u503c\uff0c\u7f16\u8bd1\u5668\u662f\u80fd\u591f\u68c0\u6d4b\u5230\u7684\u3002\u4f46\u662f\uff0c\u6709\u4e00\u4e9b\u975e\u5e38\u590d\u6742\u7684\u60c5\u51b5\uff0c\u7f16\u8bd1\u5668\u6709\u53ef\u80fd\u65e0\u6cd5\u786e\u5b9a\u51fd\u6570\u662f\u5426\u8fd4\u56de\u503c\u7684\u3002\u6240\u4ee5\u4f60\u4e0d\u5e94\u8be5\u4f9d\u8d56\u7f16\u8bd1\u5668\u3002 \u6700\u4f73\u5b9e\u8df5 \u786e\u4fdd\u975e void \u7c7b\u578b\u7684\u51fd\u6570\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u90fd\u8fd4\u56de\u503c\u3002 \u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4e0d\u8fd4\u56de\u503c\u5219\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u3002","title":"\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4e0d\u8fd4\u56de\u503c\u5219\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a"},{"location":"2-2-Function-return-values-value-returning%20functions/#main-return-0","text":"\u4e0a\u8ff0\u539f\u5219\u6709\u4e00\u4e2a\u7279\u4f8b\uff0c\u90a3\u5c31\u662fmain\u51fd\u6570\u3002main \u51fd\u6570\u5982\u679c\u6ca1\u6709\u4f7f\u7528 return \u8bed\u53e5\u5219\u4f1a\u9690\u5f0f\u5730\u8fd4\u56de 0\u3002\u5373\u4fbf\u5982\u6b64\uff0c\u6700\u4f73\u5b9e\u8df5\u4ecd\u7136\u662f\u663e\u5f0f\u5730\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u8fd9\u6837\u65e2\u80fd\u591f\u8868\u660e\u4f60\u7684\u5b9e\u9645\u610f\u56fe\uff0c\u4e5f\u80fd\u591f\u5728\u5f62\u5f0f\u4e0a\u548c\u5176\u4ed6\u51fd\u6570\u4fdd\u6301\u4e00\u81f4(\u53ef\u4ee5\u9884\u9632\u4f60\u5ffd\u7565\u5176\u4ed6\u51fd\u6570\u7684\u8fd4\u56de\u503c)\u3002","title":"main \u51fd\u6570\u5982\u679c\u6ca1\u6709\u4f7f\u7528 return \u8bed\u53e5\u5219\u4f1a\u9690\u5f0f\u5730\u8fd4\u56de 0"},{"location":"2-2-Function-return-values-value-returning%20functions/#_5","text":"\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4f9d\u6b21\u53ea\u80fd\u8fd4\u56de\u4e00\u4e2a\u5355\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002 \u6ce8\u610f\uff0c return \u8bed\u53e5\u4e2d\u7684\u503c\u4e0d\u4e00\u5b9a\u8981\u7d20\u5b57\u9762\u91cf\u2014\u2014\u4e5f\u53ef\u4ee5\u662f\u4efb\u4f55\u8868\u8fbe\u5f0f\u7684\u503c\uff0c\u5305\u62ec\u53d8\u91cf\uff0c\u751a\u81f3\u53ef\u4ee5\u662f\u5176\u4ed6\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002 getValueFromUser() \u7684\u4f8b\u5b50\u4e2d\uff0c\u8fd4\u56de\u503c\u662f\u53d8\u91cf input \uff0c\u5176\u4e2d\u5305\u542b\u4e86\u7528\u6237\u8f93\u5165\u7684\u5b9e\u9645\u503c\u3002 \u901a\u8fc7\u4e00\u4e9b\u65b9\u6cd5\u662f\u53ef\u4ee5\u7ed5\u8fc7\u51fd\u6570\u53ea\u80fd\u8fd4\u56de\u5355\u4e00\u503c\u7684\u9650\u5236\u7684\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"\u51fd\u6570\u53ea\u80fd\u8fd4\u56de\u4e00\u4e2a\u5355\u503c"},{"location":"2-2-Function-return-values-value-returning%20functions/#_6","text":"\u51fd\u6570\u8fd4\u56de\u503c\u7684\u542b\u4e49\u662f\u7531\u51fd\u6570\u7f16\u5199\u8005\u51b3\u5b9a\u7684\u3002\u6709\u4e9b\u51fd\u6570\u4f1a\u5c06\u72b6\u6001\u7801\u4f5c\u4e3a\u8fd4\u56de\u503c\u6765\u8868\u793a\u51fd\u6570\u662f\u5426\u6267\u884c\u6210\u529f\u3002\u6709\u7684\u51fd\u6570\u5219\u5c06\u8ba1\u7b97\u7ed3\u679c\u6216\u5176\u4ed6\u9009\u5b9a\u7684\u503c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u8fd8\u6709\u4e9b\u51fd\u6570\u4ec0\u4e48\u90fd\u4e0d\u8fd4\u56de\uff08\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\u4e2d\u4ecb\u7ecd\u6b64\u7c7b\u51fd\u6570\uff09\u3002 \u56e0\u4e3a\u51fd\u6570\u8fd4\u56de\u503c\u7684\u542b\u4e49\u53ef\u80fd\u662f\u5404\u79cd\u5404\u6837\u7684\uff0c\u56e0\u6b64\u901a\u8fc7\u6ce8\u91ca\u7684\u65b9\u5f0f\u5bf9\u51fd\u6570\u8fd4\u56de\u503c\u7684\u542b\u4e49\u8fdb\u884c\u63cf\u8ff0\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 // \u51fd\u6570\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u503c // \u8fd4\u56de\u503c\u662f\u7528\u6237\u4ece\u952e\u76d8\u8f93\u5165\u7684\u503c\uff0c\u6574\u578b int getValueFromUser () { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; // \u5c06\u7528\u6237\u6570\u7ed3\u679c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570 }","title":"\u51fd\u6570\u7684\u7f16\u5199\u8005\u53ef\u4ee5\u51b3\u5b9a\u8fd4\u56de\u503c\u7684\u542b\u4e49"},{"location":"2-2-Function-return-values-value-returning%20functions/#_7","text":"\u63a5\u4e0b\u6765\uff0c\u4ecb\u7ecd\u51fd\u6570\u91cd\u7528\u7684\u5178\u578b\u4f8b\u5b50\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { int x {}; std :: cout << \"Enter an integer: \" ; std :: cin >> x ; int y {}; std :: cout << \"Enter an integer: \" ; std :: cin >> y ; std :: cout << x << \" + \" << y << \" = \" << x + y << '\\n' ; return 0 ; } \u867d\u7136\u8fd9\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\uff0c\u4f46\u662f\u4ee3\u7801\u6709\u4e9b\u5197\u4f59\u3002\u5b9e\u9645\u4e0a\u8fd9\u4e2a\u51fd\u6570\u8fdd\u53cd\u4e86\u597d\u4ee3\u7801\u7684\u6838\u5fc3\u4fe1\u6761\uff1a\u4e0d\u8981\u91cd\u590d\u4f60\u81ea\u5df1\u2014\u2014Don\u2019t Repeat Yourself (\u5e38\u5e38\u7f29\u5199\u4e3aDRY)\u3002 \u4e3a\u4ec0\u4e48\u91cd\u590d\u7684\u4ee3\u7801\u662f\u4e0d\u597d\u7684\uff1f\u5982\u679c\u4f60\u60f3\u8981\u5c06\u6587\u672c \u201cEnter an integer:\u201d \u4fee\u6539\u6210\u5176\u4ed6\u5185\u5bb9\uff0c\u90a3\u4e48\u4f60\u5fc5\u987b\u8981\u4fee\u6539\u4e24\u4e2a\u5730\u65b9\u3002\u5982\u679c\u8981\u521d\u59cb\u5316\u7684\u53d8\u91cf\u670910\u4e2a\u800c\u4e0d\u662f\u4e24\u4e2a\u5462\uff1f\u8fd9\u65e0\u7591\u5c06\u4f1a\u4ea7\u751f\u5927\u91cf\u7684\u5197\u4f59\u4ee3\u7801\uff08\u4f7f\u5f97\u7a0b\u5e8f\u53d8\u5f97\u53c8\u957f\u53c8\u96be\u4ee5\u7406\u89e3\uff09\uff0c\u8fd8\u4e3a\u62fc\u5199\u9519\u8bef\u63d0\u4f9b\u4e86\u6ecb\u751f\u7684\u7a7a\u95f4\u3002 \u4e0b\u9762\uff0c\u4f7f\u7528 getValueFromUser \u51fd\u6570\u6765\u91cd\u6784\u4e0a\u8ff0\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int getValueFromUser () { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; } int main () { int x { getValueFromUser () }; // first call to getValueFromUser int y { getValueFromUser () }; // second call to getValueFromUser std :: cout << x << \" + \" << y << \" = \" << x + y << '\\n' ; return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 Enter an integer: 5 Enter an integer: 7 5 + 7 = 12 \u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\uff0c getValueFromUser \u88ab\u8c03\u7528\u4e86\u91cf\u8bcd\uff0c\u4e00\u6b21\u7528\u4e8e\u521d\u59cb\u5316\u53d8\u91cf x \uff0c\u4e00\u6b21\u7528\u4e8e\u521d\u59cb\u5316\u53d8\u91cf y \u3002\u4f7f\u7528\u8be5\u51fd\u6570\u53ef\u4ee5\u6709\u6548\u5730\u907f\u514d\u91cd\u590d\u4ee3\u7801\uff0c\u4e5f\u51cf\u5c11\u4e86\u72af\u9519\u7684\u6982\u7387\u3002\u53ea\u8981 getValueFromUser \u51fd\u6570\u80fd\u591f\u6b63\u786e\u5de5\u4f5c\uff0c\u90a3\u4e48\u4fbf\u53ef\u4ee5\u591a\u6b21\u8c03\u7528\u5b83\u3002 \u8fd9\u4e5f\u662f\u6a21\u5757\u5316\u7f16\u7a0b\u7684\u7cbe\u9ad3\uff1a\u7f16\u5199\u51fd\u6570\u3001\u6d4b\u8bd5\u51fd\u6570\u4ee5\u786e\u4fdd\u5b83\u80fd\u591f\u6b63\u786e\u5de5\u4f5c\uff0c\u7136\u540e\u4fbf\u53ef\u4ee5\u5728\u63a5\u4e0b\u6765\u7684\u5de5\u4f5c\u4e2d\u53cd\u590d\u4f7f\u7528\u8be5\u51fd\u6570\uff08\u53ea\u8981\u6211\u4eec\u6ca1\u6709\u4fee\u6539\u8fd9\u4e2a\u51fd\u6570\u2014\u2014\u5426\u5219\u5fc5\u987b\u5bf9\u8be5\u51fd\u6570\u91cd\u65b0\u6d4b\u8bd5\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u8bf7\u9075\u5faaDRY\u539f\u5219\uff0c\u5373\u4e0d\u8981\"\u91cd\u590d\u4f60\u81ea\u5df1\u7684\u5de5\u4f5c\"\u3002\u5982\u679c\u6709\u4ef6\u4e8b\u9700\u8981\u505a\u591a\u6b21\uff0c\u8bf7\u8003\u8651\u662f\u5426\u53ef\u4ee5\u4fee\u6539\u4ee3\u7801\uff0c\u5c3d\u53ef\u80fd\u79fb\u9664\u5197\u4f59\u7684\u90e8\u5206\u3002\u53d8\u91cf\u53ef\u4ee5\u7528\u6765\u5b58\u653e\u88ab\u8ba1\u7b97\u51fa\u6765\u4e14\u9700\u8981\u591a\u6b21\u4f7f\u7528\u7684\u6570\u636e\uff08\u8fd9\u6837\u5c31\u4e0d\u9700\u8981\u8fdb\u884c\u91cd\u590d\u8ba1\u7b97\uff09\u3002\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u5b9a\u4e49\u4e00\u7ec4\u53ef\u80fd\u4f1a\u88ab\u591a\u6b21\u6267\u884c\u7684\u6307\u4ee4\u3002\u5faa\u73af\uff08\u540e\u9762\u7684\u7ae0\u8282\u4e2d\u4f1a\u4ecb\u7ecd\uff09\u5219\u53ef\u4ee5\u7528\u6765\u591a\u6b21\u6267\u884c\u8bed\u53e5\u3002 \u9898\u5916\u8bdd DRY \u7684\u53cd\u4e49\u8bcd\u662f WET (\u201cWrite everything twice\u201d\u2014\u2014\u4efb\u4f55\u4ee3\u7801\u90fd\u5199\u4e24\u904d).","title":"\u51fd\u6570\u91cd\u7528"},{"location":"2-3-Void-functions-non-value%20returning%20functions/","tags":["void"],"text":"\u5728 2.1 - \u51fd\u6570\u7b80\u4ecb \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u51fd\u6570\u5b9a\u4e49\u7684\u8bed\u6cd5\u5e94\u8be5\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 return-type identifier() // identifier replaced with the name of your function { // Your code here } \u5c3d\u7ba1\u6211\u4eec\u5df2\u7ecf\u89c1\u8fc7\u4e00\u4e9b\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a void \u7684\u51fd\u6570\u7684\u4f8b\u5b50\u4e86\uff0c\u4f46\u662f\u8bfe\u7a0b\u4e2d\u5e76\u672a\u4ecb\u7ecd\u8be5\u7c7b\u578b\u7684\u5177\u4f53\u542b\u4e49\u3002\u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u4e3b\u8981\u7814\u7a76\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a void \u7684\u51fd\u6570\u3002 \u8fd4\u56de\u7a7a\u503c \u88ab\u8c03\u51fd\u6570\u65e0\u9700\u5411\u4e3b\u8c03\u51fd\u6570\u8fd4\u56de\u503c\uff0c\u800c\u4e3a\u4e86\u5c06\u8be5\u4fe1\u606f\u544a\u77e5\u7f16\u8bd1\u5668\uff0c\u5b9a\u4e49\u51fd\u6570\u65f6\u9700\u8981\u4f7f\u7528 void \u4f5c\u4e3a\u8fd4\u56de\u503c\u7c7b\u578b\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // void \u8868\u793a\u8be5\u51fd\u6570\u65e0\u9700\u8fd4\u56de\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570 void printHi () { std :: cout << \"Hi\" << '\\n' ; // \u51fd\u6570\u4e0d\u8fd4\u4f1a\u53ea\uff0c\u56e0\u6b64\u4e5f\u65e0\u9700 return \u8bed\u53e5 } int main () { printHi (); // okay: \u51fd\u6570 printHi() \u88ab\u8c03\u7528\uff0c\u65e0\u8fd4\u56de\u503c return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c printHi \u51fd\u6570\u5177\u6709\u5176\u7279\u5b9a\u7684\u529f\u80fd\uff08\u6253\u5370 \"Hi\"\uff09\uff0c\u4f46\u662f\u5b83\u4e0d\u5fc5\u5c06 \u4efb\u4f55\u503c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u56e0\u6b64\uff0c\u5728\u5b9a\u4e49 printHi \u51fd\u6570\u65f6\u4f7f\u7528\u4e86 void \u4f5c\u4e3a\u8fd4\u56de\u503c\u7c7b\u578b\u3002 \u5f53 main \u51fd\u6570\u8c03\u7528 printHi \u65f6\uff0c printHi \u51fd\u6570\u4f53\u4e2d\u7684\u4ee3\u7801\u4f1a\u88ab\u6267\u884c\uff0c\u201cHi\u201d \u88ab\u6253\u5370\u5728\u5c4f\u5e55\u4e0a\u3002\u968f\u540e\u63a7\u5236\u6743\u4ea4\u8fd8\u7ed9\u4e3b\u8c03\u51fd\u6570\uff0c\u7a0b\u5e8f\u7ee7\u7eed\u5411\u4e0b\u6267\u884c\u3002 \u4e0d\u8fd4\u8fd8\u4efb\u4f55\u503c\u7684\u51fd\u6570\uff0c\u79f0\u4e3a\u65e0\u8fd4\u56de\u503c\u51fd\u6570\uff08\u6216\u7a7a\u51fd\u6570\uff09\u3002 Void \u51fd\u6570\u65e0\u9700 return \u8bed\u53e5 \u7a7a\u51fd\u6570\u4f1a\u5728\u51fd\u6570\u7684\u672b\u5c3e\u81ea\u52a8\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570\uff0c\u65e0\u9700 return \u8bed\u53e5\u3002 \u4e0d\u5e26\u8fd4\u56de\u503c\u7684 return \u8bed\u53e5\u53ef\u4ee5\u88ab\u7528\u5728 void \u51fd\u6570\u4e2d\u2014\u2014\u8be5\u8bed\u53e5\u6267\u884c\u65f6\u4f1a\u5bfc\u81f4\u8be5\u51fd\u6570\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570\uff0c\u8fd9\u548c\u51fd\u6570\u8fd0\u884c\u5230\u7ed3\u5c3e\u65f6\u8fd4\u56de\u7684\u6548\u679c\u662f\u4e00\u6837\u7684\u3002\u56e0\u6b64\uff0c\u5c06\u4e00\u4e2a\u4e0d\u5e26\u8fd4\u56de\u503c\u7684 return \u8bed\u53e5\u653e\u5728\u51fd\u6570\u672b\u5c3e\u662f\u591a\u4f59\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // void means the function does not return a value to the caller void printHi () { std :: cout << \"Hi\" << '\\n' ; return ; // tell compiler to return to the caller -- this is redundant since this will happen anyway! } // function will return to caller here int main () { printHi (); return 0 ; } \u6700\u4f73\u5b9e\u8df5 \u4e0d\u8981\u5728\u4e0d\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u672b\u5c3e\u6dfb\u52a0 return \u8bed\u53e5\u3002 Void \u51fd\u6570\u4e0d\u80fd\u88ab\u7528\u5728\u9700\u8981\u503c\u7684\u8868\u8fbe\u5f0f \u6709\u4e9b\u7c7b\u578b\u7684\u8868\u8fbe\u5f0f\u662f\u9700\u8981\u4e00\u4e2a\u503c\u7684\uff0c\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 #include int main () { std :: cout << 5 ; // ok: 5 is a literal value that we're sending to the console to be printed std :: cout << ; // compile error: no value provided return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u9700\u8981\u6253\u5370\u7684\u5185\u5bb9\u5fc5\u987b\u4f4d\u4e8e std:: cout << \u7684\u53f3\u4fa7\uff0c\u5426\u5219\u7a0b\u5e8f\u5c06\u4f1a\u62a5\u9519\u3002\u56e0\u6b64\uff0c\u7b2c\u4e8c\u5904\u8c03\u7528 std:: cout \u7531\u4e8e\u6ca1\u6709\u53ef\u4ee5\u6253\u5370\u7684\u503c\uff0c\u7f16\u8bd1\u5668\u4f1a\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\u3002 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // void means the function does not return a value to the caller void printHi () { std :: cout << \"Hi\" << '\\n' ; } int main () { printHi (); // okay: function printHi() is called, no value is returned std :: cout << printHi (); // compile error return 0 ; } printHi() \u7b2c\u4e00\u6b21\u88ab\u8c03\u7528\u65f6\uff0c\u5176\u4e0a\u4e0b\u6587\u4e0d\u9700\u8981\u4e00\u4e2a\u503c\u3002\u56e0\u6b64\u8be5\u51fd\u6570\u4e0d\u8fd4\u56de\u503c\u662f\u6ca1\u6709\u4efb\u4f55\u95ee\u9898\u7684\u3002 \u8be5\u51fd\u6570\u7b2c\u4e8c\u6b21\u88ab\u8c03\u7528\u7684\u65f6\u5019\uff0c\u7a0b\u5e8f\u662f\u4e0d\u80fd\u6b63\u786e\u7f16\u8bd1\u7684\u3002\u51fd\u6570 printHi \u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a void \uff0c\u4e5f\u5c31\u662f\u8bf4\u8be5\u51fd\u6570\u4e0d\u5177\u6709\u8fd4\u56de\u503c\u3002\u7136\u800c\uff0c\u8be5\u8bed\u53e5\u5c1d\u8bd5\u5c06 printHi \u51fd\u6570\u7684\u8fd4\u56de\u503c\u8f93\u5165 std:: cout \u5e76\u6253\u5370\u3002 std:: cout \u4e0d\u77e5\u9053\u5982\u4f55\u5904\u7406\u8be5\u60c5\u51b5\uff08\u4ec0\u4e48\u503c\u9700\u8981\u88ab\u6253\u5370\uff1f\uff09\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u4f1a\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\u3002\u4f60\u5fc5\u987b\u628a\u8fd9\u4e00\u884c\u6ce8\u91ca\u6389\u624d\u80fd\u6b63\u5e38\u7f16\u8bd1\u8be5\u7a0b\u5e8f\u3002 \u5c0f\u8d34\u58eb \u6709\u4e9b\u8bed\u53e5\u9700\u8981\u503c\uff0c\u800c\u6709\u4e9b\u8bed\u53e5\u5219\u4e0d\u9700\u8981\u3002 \u5f53\u72ec\u7acb\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u7684\u65f6\u5019\uff08\u4f8b\u5982\u4e0a\u4f8b\u4e2d\u7b2c\u4e00\u6b21\u8c03\u7528 printHi() \u51fd\u6570\u65f6\uff09\uff0c\u6211\u4eec\u5173\u5fc3\u7684\u662f\u51fd\u6570\u672c\u8eab\u7684\u884c\u4e3a\uff08\u529f\u80fd\uff09\u800c\u4e0d\u662f\u5176\u8fd4\u56de\u503c\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6ca1\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4f46\u5ffd\u7565\u5176\u8fd4\u56de\u503c\u3002 \u53cd\u4e4b\uff0c\u5982\u679c\u6211\u4eec\u5728\u4e00\u4e2a\u9700\u8981\u503c\u7684\u4e0a\u4e0b\u6587\u4e2d\uff08\u4f8b\u5982 std:: cout \uff09\u8c03\u7528\u67d0\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u53ea\u80fd\u4f7f\u7528\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // Function that does not return a value void returnNothing () { } // Function that returns a value int returnFive () { return 5 ; } int main () { // When calling a function by itself, no value is required returnNothing (); // ok: we can call a function that does not return a value returnFive (); // ok: we can call a function that returns a value, and ignore that return value // When calling a function in a context that requires a value (like std::cout) std :: cout << returnFive (); // ok: we can call a function that returns a value, and the value will be used std :: cout << returnNothing (); // compile error: we can't call a function that returns void in this context return 0 ; } \u4ece void \u51fd\u6570\u4e2d\u8fd4\u56de\u503c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u62a5\u9519 \u5c1d\u8bd5\u5728\u4e00\u4e2a\u4e0d\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4e2d\u8fd4\u56de\u4e00\u4e2a\u503c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u62a5\u9519\uff1a 1 2 3 4 5 6 void printHi () // This function is non-value returning { std :: cout << \"In printHi()\" << '\\n' ; return 5 ; // compile error: we're trying to return a value } \u63d0\u524d\u8fd4\u56de \u5982\u679c\u4e00\u4e2a return \u8bed\u53e5\u51fa\u73b0\u5728\u6700\u540e\u4e00\u884c\u4ee3\u7801\u4e4b\u524d\uff0c\u5219\u79f0\u4e3a\u63d0\u524d\u8fd4\u56de\u3002\u8be5 return \u8bed\u53e5\u6267\u884c\u65f6\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u76f4\u63a5\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570\uff08\u56e0\u4e3a\u5229\u7528\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\u4f1a\u4ece\u6700\u540e\u4e00\u884c\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570\uff0c\u6240\u4ee5\u79f0\u4e3a\u201c\u63d0\u524d\u201d\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void print () // note: void return type { std :: cout << \"A\" ; return ; // \u51fd\u6570\u5c06\u5728\u8fd9\u884c\u8bed\u53e5\u6267\u884c\u65f6\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570(\u6ce8\u610f\uff1a\u6ca1\u6709\u8fd4\u56de\u503c) std :: cout << \"B\" ; // \u8be5\u884c\u6c38\u8fdc\u4e0d\u4f1a\u88ab\u6267\u884c } int main () { print (); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53 print() \u6267\u884c\u65f6\uff0c\u5b83\u4f1a\u9996\u5148\u6253\u5370 A\uff0c\u7136\u540e\u6267\u884c return \u8bed\u53e5\u5e76\u63a7\u5236\u51fd\u6570\u8fd4\u56de\u5176\u4e3b\u8c03\u51fd\u6570 main\u3002B \u6c38\u8fdc\u90fd\u4e0d\u4f1a\u88ab\u6253\u5370\u56e0\u4e3a\u8be5\u884c\u4ee3\u7801\u4e0d\u4f1a\u6267\u884c\u3002 \u63d0\u524d\u8fd4\u56de\u4e5f\u9002\u7528\u4e8e\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int print () // note: return type of int { std :: cout << \"A\" ; return 5 ; // the function will return to the caller here std :: cout << \"B\" ; // this will never be printed } int main () { std :: cout << print (); // print() returns value 5, which will be print to the console return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 A5 \u9996\u5148, print() \u88ab\u8c03\u7528\u3002 print() \u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u4f1a\u6253\u5370 \u201cA\u201d\u3002\u7136\u540e return \u8bed\u53e5\u88ab\u6267\u884c\uff0c\u8fd4\u56de\u503c 5 \u88ab\u4f20\u9012\u7ed9\u4e3b\u8c03\u51fd\u6570\uff0c\u8be5\u503c\u540c\u6837\u88ab\u6253\u5370\u4e86\u51fa\u6765\u3002 std:: cout << \"B\" \u6c38\u8fdc\u4e0d\u4f1a\u88ab\u6267\u884c\uff0c\u56e0\u4e3a\u5728\u6267\u884c\u5b83\u4e4b\u524d\uff0c\u51fd\u6570\u5df2\u7ecf\u8fd4\u56de\u4e86\u4e3b\u8c03\u51fd\u6570\u3002 \u5386\u53f2\u4e0a\uff0c\u63d0\u524d\u8fd4\u56de\u662f\u4e0d\u53d7\u9f13\u52b1\u7684\u884c\u4e3a\u3002\u4e0d\u8fc7\uff0c\u5728\u73b0\u4ee3\u7f16\u7a0b\u4e2d\uff0c\u5b83\u4eec\u662f\u53ef\u4ee5\u88ab\u63a5\u53d7\u7684\uff0c\u63d0\u524d\u8fd4\u56de\u80fd\u591f\u7b80\u5316\u7a0b\u5e8f\u65f6\uff0c\u6216\u5728\u53d1\u751f\u9519\u8bef\u65f6\u7ec8\u6b62\u51fd\u6570\u540e\u7eed\u7684\u64cd\u4f5c\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 7.10 - break \u548c continue \u4e2d\u8ba8\u8bba\u6709\u5173\u63d0\u524d\u8fd4\u56de\u7684\u4e89\u8bba\u3002","title":"2.3 - void \u51fd\u6570"},{"location":"2-3-Void-functions-non-value%20returning%20functions/#_1","text":"\u88ab\u8c03\u51fd\u6570\u65e0\u9700\u5411\u4e3b\u8c03\u51fd\u6570\u8fd4\u56de\u503c\uff0c\u800c\u4e3a\u4e86\u5c06\u8be5\u4fe1\u606f\u544a\u77e5\u7f16\u8bd1\u5668\uff0c\u5b9a\u4e49\u51fd\u6570\u65f6\u9700\u8981\u4f7f\u7528 void \u4f5c\u4e3a\u8fd4\u56de\u503c\u7c7b\u578b\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // void \u8868\u793a\u8be5\u51fd\u6570\u65e0\u9700\u8fd4\u56de\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570 void printHi () { std :: cout << \"Hi\" << '\\n' ; // \u51fd\u6570\u4e0d\u8fd4\u4f1a\u53ea\uff0c\u56e0\u6b64\u4e5f\u65e0\u9700 return \u8bed\u53e5 } int main () { printHi (); // okay: \u51fd\u6570 printHi() \u88ab\u8c03\u7528\uff0c\u65e0\u8fd4\u56de\u503c return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c printHi \u51fd\u6570\u5177\u6709\u5176\u7279\u5b9a\u7684\u529f\u80fd\uff08\u6253\u5370 \"Hi\"\uff09\uff0c\u4f46\u662f\u5b83\u4e0d\u5fc5\u5c06 \u4efb\u4f55\u503c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u56e0\u6b64\uff0c\u5728\u5b9a\u4e49 printHi \u51fd\u6570\u65f6\u4f7f\u7528\u4e86 void \u4f5c\u4e3a\u8fd4\u56de\u503c\u7c7b\u578b\u3002 \u5f53 main \u51fd\u6570\u8c03\u7528 printHi \u65f6\uff0c printHi \u51fd\u6570\u4f53\u4e2d\u7684\u4ee3\u7801\u4f1a\u88ab\u6267\u884c\uff0c\u201cHi\u201d \u88ab\u6253\u5370\u5728\u5c4f\u5e55\u4e0a\u3002\u968f\u540e\u63a7\u5236\u6743\u4ea4\u8fd8\u7ed9\u4e3b\u8c03\u51fd\u6570\uff0c\u7a0b\u5e8f\u7ee7\u7eed\u5411\u4e0b\u6267\u884c\u3002 \u4e0d\u8fd4\u8fd8\u4efb\u4f55\u503c\u7684\u51fd\u6570\uff0c\u79f0\u4e3a\u65e0\u8fd4\u56de\u503c\u51fd\u6570\uff08\u6216\u7a7a\u51fd\u6570\uff09\u3002","title":"\u8fd4\u56de\u7a7a\u503c"},{"location":"2-3-Void-functions-non-value%20returning%20functions/#void-return","text":"\u7a7a\u51fd\u6570\u4f1a\u5728\u51fd\u6570\u7684\u672b\u5c3e\u81ea\u52a8\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570\uff0c\u65e0\u9700 return \u8bed\u53e5\u3002 \u4e0d\u5e26\u8fd4\u56de\u503c\u7684 return \u8bed\u53e5\u53ef\u4ee5\u88ab\u7528\u5728 void \u51fd\u6570\u4e2d\u2014\u2014\u8be5\u8bed\u53e5\u6267\u884c\u65f6\u4f1a\u5bfc\u81f4\u8be5\u51fd\u6570\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570\uff0c\u8fd9\u548c\u51fd\u6570\u8fd0\u884c\u5230\u7ed3\u5c3e\u65f6\u8fd4\u56de\u7684\u6548\u679c\u662f\u4e00\u6837\u7684\u3002\u56e0\u6b64\uff0c\u5c06\u4e00\u4e2a\u4e0d\u5e26\u8fd4\u56de\u503c\u7684 return \u8bed\u53e5\u653e\u5728\u51fd\u6570\u672b\u5c3e\u662f\u591a\u4f59\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // void means the function does not return a value to the caller void printHi () { std :: cout << \"Hi\" << '\\n' ; return ; // tell compiler to return to the caller -- this is redundant since this will happen anyway! } // function will return to caller here int main () { printHi (); return 0 ; } \u6700\u4f73\u5b9e\u8df5 \u4e0d\u8981\u5728\u4e0d\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u672b\u5c3e\u6dfb\u52a0 return \u8bed\u53e5\u3002","title":"Void \u51fd\u6570\u65e0\u9700 return \u8bed\u53e5"},{"location":"2-3-Void-functions-non-value%20returning%20functions/#void","text":"\u6709\u4e9b\u7c7b\u578b\u7684\u8868\u8fbe\u5f0f\u662f\u9700\u8981\u4e00\u4e2a\u503c\u7684\uff0c\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 #include int main () { std :: cout << 5 ; // ok: 5 is a literal value that we're sending to the console to be printed std :: cout << ; // compile error: no value provided return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u9700\u8981\u6253\u5370\u7684\u5185\u5bb9\u5fc5\u987b\u4f4d\u4e8e std:: cout << \u7684\u53f3\u4fa7\uff0c\u5426\u5219\u7a0b\u5e8f\u5c06\u4f1a\u62a5\u9519\u3002\u56e0\u6b64\uff0c\u7b2c\u4e8c\u5904\u8c03\u7528 std:: cout \u7531\u4e8e\u6ca1\u6709\u53ef\u4ee5\u6253\u5370\u7684\u503c\uff0c\u7f16\u8bd1\u5668\u4f1a\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\u3002 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // void means the function does not return a value to the caller void printHi () { std :: cout << \"Hi\" << '\\n' ; } int main () { printHi (); // okay: function printHi() is called, no value is returned std :: cout << printHi (); // compile error return 0 ; } printHi() \u7b2c\u4e00\u6b21\u88ab\u8c03\u7528\u65f6\uff0c\u5176\u4e0a\u4e0b\u6587\u4e0d\u9700\u8981\u4e00\u4e2a\u503c\u3002\u56e0\u6b64\u8be5\u51fd\u6570\u4e0d\u8fd4\u56de\u503c\u662f\u6ca1\u6709\u4efb\u4f55\u95ee\u9898\u7684\u3002 \u8be5\u51fd\u6570\u7b2c\u4e8c\u6b21\u88ab\u8c03\u7528\u7684\u65f6\u5019\uff0c\u7a0b\u5e8f\u662f\u4e0d\u80fd\u6b63\u786e\u7f16\u8bd1\u7684\u3002\u51fd\u6570 printHi \u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a void \uff0c\u4e5f\u5c31\u662f\u8bf4\u8be5\u51fd\u6570\u4e0d\u5177\u6709\u8fd4\u56de\u503c\u3002\u7136\u800c\uff0c\u8be5\u8bed\u53e5\u5c1d\u8bd5\u5c06 printHi \u51fd\u6570\u7684\u8fd4\u56de\u503c\u8f93\u5165 std:: cout \u5e76\u6253\u5370\u3002 std:: cout \u4e0d\u77e5\u9053\u5982\u4f55\u5904\u7406\u8be5\u60c5\u51b5\uff08\u4ec0\u4e48\u503c\u9700\u8981\u88ab\u6253\u5370\uff1f\uff09\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u4f1a\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\u3002\u4f60\u5fc5\u987b\u628a\u8fd9\u4e00\u884c\u6ce8\u91ca\u6389\u624d\u80fd\u6b63\u5e38\u7f16\u8bd1\u8be5\u7a0b\u5e8f\u3002 \u5c0f\u8d34\u58eb \u6709\u4e9b\u8bed\u53e5\u9700\u8981\u503c\uff0c\u800c\u6709\u4e9b\u8bed\u53e5\u5219\u4e0d\u9700\u8981\u3002 \u5f53\u72ec\u7acb\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u7684\u65f6\u5019\uff08\u4f8b\u5982\u4e0a\u4f8b\u4e2d\u7b2c\u4e00\u6b21\u8c03\u7528 printHi() \u51fd\u6570\u65f6\uff09\uff0c\u6211\u4eec\u5173\u5fc3\u7684\u662f\u51fd\u6570\u672c\u8eab\u7684\u884c\u4e3a\uff08\u529f\u80fd\uff09\u800c\u4e0d\u662f\u5176\u8fd4\u56de\u503c\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6ca1\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4f46\u5ffd\u7565\u5176\u8fd4\u56de\u503c\u3002 \u53cd\u4e4b\uff0c\u5982\u679c\u6211\u4eec\u5728\u4e00\u4e2a\u9700\u8981\u503c\u7684\u4e0a\u4e0b\u6587\u4e2d\uff08\u4f8b\u5982 std:: cout \uff09\u8c03\u7528\u67d0\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u53ea\u80fd\u4f7f\u7528\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // Function that does not return a value void returnNothing () { } // Function that returns a value int returnFive () { return 5 ; } int main () { // When calling a function by itself, no value is required returnNothing (); // ok: we can call a function that does not return a value returnFive (); // ok: we can call a function that returns a value, and ignore that return value // When calling a function in a context that requires a value (like std::cout) std :: cout << returnFive (); // ok: we can call a function that returns a value, and the value will be used std :: cout << returnNothing (); // compile error: we can't call a function that returns void in this context return 0 ; }","title":"Void \u51fd\u6570\u4e0d\u80fd\u88ab\u7528\u5728\u9700\u8981\u503c\u7684\u8868\u8fbe\u5f0f"},{"location":"2-3-Void-functions-non-value%20returning%20functions/#void_1","text":"\u5c1d\u8bd5\u5728\u4e00\u4e2a\u4e0d\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4e2d\u8fd4\u56de\u4e00\u4e2a\u503c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u62a5\u9519\uff1a 1 2 3 4 5 6 void printHi () // This function is non-value returning { std :: cout << \"In printHi()\" << '\\n' ; return 5 ; // compile error: we're trying to return a value }","title":"\u4ece void \u51fd\u6570\u4e2d\u8fd4\u56de\u503c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u62a5\u9519"},{"location":"2-3-Void-functions-non-value%20returning%20functions/#_2","text":"\u5982\u679c\u4e00\u4e2a return \u8bed\u53e5\u51fa\u73b0\u5728\u6700\u540e\u4e00\u884c\u4ee3\u7801\u4e4b\u524d\uff0c\u5219\u79f0\u4e3a\u63d0\u524d\u8fd4\u56de\u3002\u8be5 return \u8bed\u53e5\u6267\u884c\u65f6\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u76f4\u63a5\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570\uff08\u56e0\u4e3a\u5229\u7528\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\u4f1a\u4ece\u6700\u540e\u4e00\u884c\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570\uff0c\u6240\u4ee5\u79f0\u4e3a\u201c\u63d0\u524d\u201d\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void print () // note: void return type { std :: cout << \"A\" ; return ; // \u51fd\u6570\u5c06\u5728\u8fd9\u884c\u8bed\u53e5\u6267\u884c\u65f6\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570(\u6ce8\u610f\uff1a\u6ca1\u6709\u8fd4\u56de\u503c) std :: cout << \"B\" ; // \u8be5\u884c\u6c38\u8fdc\u4e0d\u4f1a\u88ab\u6267\u884c } int main () { print (); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53 print() \u6267\u884c\u65f6\uff0c\u5b83\u4f1a\u9996\u5148\u6253\u5370 A\uff0c\u7136\u540e\u6267\u884c return \u8bed\u53e5\u5e76\u63a7\u5236\u51fd\u6570\u8fd4\u56de\u5176\u4e3b\u8c03\u51fd\u6570 main\u3002B \u6c38\u8fdc\u90fd\u4e0d\u4f1a\u88ab\u6253\u5370\u56e0\u4e3a\u8be5\u884c\u4ee3\u7801\u4e0d\u4f1a\u6267\u884c\u3002 \u63d0\u524d\u8fd4\u56de\u4e5f\u9002\u7528\u4e8e\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int print () // note: return type of int { std :: cout << \"A\" ; return 5 ; // the function will return to the caller here std :: cout << \"B\" ; // this will never be printed } int main () { std :: cout << print (); // print() returns value 5, which will be print to the console return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 A5 \u9996\u5148, print() \u88ab\u8c03\u7528\u3002 print() \u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u4f1a\u6253\u5370 \u201cA\u201d\u3002\u7136\u540e return \u8bed\u53e5\u88ab\u6267\u884c\uff0c\u8fd4\u56de\u503c 5 \u88ab\u4f20\u9012\u7ed9\u4e3b\u8c03\u51fd\u6570\uff0c\u8be5\u503c\u540c\u6837\u88ab\u6253\u5370\u4e86\u51fa\u6765\u3002 std:: cout << \"B\" \u6c38\u8fdc\u4e0d\u4f1a\u88ab\u6267\u884c\uff0c\u56e0\u4e3a\u5728\u6267\u884c\u5b83\u4e4b\u524d\uff0c\u51fd\u6570\u5df2\u7ecf\u8fd4\u56de\u4e86\u4e3b\u8c03\u51fd\u6570\u3002 \u5386\u53f2\u4e0a\uff0c\u63d0\u524d\u8fd4\u56de\u662f\u4e0d\u53d7\u9f13\u52b1\u7684\u884c\u4e3a\u3002\u4e0d\u8fc7\uff0c\u5728\u73b0\u4ee3\u7f16\u7a0b\u4e2d\uff0c\u5b83\u4eec\u662f\u53ef\u4ee5\u88ab\u63a5\u53d7\u7684\uff0c\u63d0\u524d\u8fd4\u56de\u80fd\u591f\u7b80\u5316\u7a0b\u5e8f\u65f6\uff0c\u6216\u5728\u53d1\u751f\u9519\u8bef\u65f6\u7ec8\u6b62\u51fd\u6570\u540e\u7eed\u7684\u64cd\u4f5c\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 7.10 - break \u548c continue \u4e2d\u8ba8\u8bba\u6709\u5173\u63d0\u524d\u8fd4\u56de\u7684\u4e89\u8bba\u3002","title":"\u63d0\u524d\u8fd4\u56de"},{"location":"2-4-Introduction-to-function-parameters-and-arguments/","tags":["parameter","argument"],"text":"\u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4e86\u89e3\u5230\u51fd\u6570\u53ef\u4ee5\u5c06\u503c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\uff0c\u57fa\u4e8e\u6b64\uff0c\u6211\u4eec\u521b\u5efa\u4e86 getValueFromUser \u51fd\u6570\u5e76\u7528\u4e8e\u4e0b\u9762\u8fd9\u6bb5\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int getValueFromUser () { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; } int main () { int num { getValueFromUser () }; std :: cout << num << \" doubled is: \" << num * 2 << '\\n' ; return 0 ; } \u4e0d\u8fc7\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u5c06\u8f93\u51fa\u8bed\u53e5\u4e5f\u5c01\u88c5\u6210\u4e00\u4e2a\u51fd\u6570\u5462\uff1f\u4f60\u53ef\u80fd\u5e0c\u671b\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include int getValueFromUser () { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; } // \u8fd9\u4e2a\u51fd\u6570\u4e0d\u80fd\u7f16\u8bd1 void printDouble () { std :: cout << num << \" doubled is: \" << num * 2 << '\\n' ; } int main () { int num { getValueFromUser () }; printDouble (); return 0 ; } \u8fd9\u6bb5\u7a0b\u5e8f\u5e76\u4e0d\u80fd\u6b63\u786e\u7f16\u8bd1\uff0c\u56e0\u4e3a printDouble \u5e76\u4e0d\u77e5\u9053 num \u662f\u4ec0\u4e48\u3002\u6b64\u65f6\u4f60\u6216\u8bb8\u4f1a\u5728 printDouble() \u4e2d\u5b9a\u4e49 num \uff1a 1 2 3 4 5 void printDouble () { int num {}; // \u6dfb\u52a0\u6b64\u884c std :: cout << num << \" doubled is: \" << num * 2 << '\\n' ; } \u5c3d\u7ba1\u8fd9\u4e48\u505a\u53ef\u4ee5\u907f\u514d\u7f16\u8bd1\u5668\u62a5\u9519\u5e76\u4f7f\u5f97\u7a0b\u5e8f\u6210\u529f\u7f16\u8bd1\uff0c\u4f46\u8be5\u7a0b\u5e8f\u7684\u8fd0\u884c\u7ed3\u679c\u5e76\u4e0d\u6b63\u786e\uff08\u59cb\u7ec8\u4f1a\u6253\u5370\uff1a\u201c0 doubled is: 0\u201d\uff09\u3002\u8fd9\u4e2a\u95ee\u9898\u7684\u6839\u672c\u539f\u56e0\u662f printDouble \u65e0\u6cd5\u8bbf\u95ee\u7528\u6237\u7684\u8f93\u5165\u7ed3\u679c\u3002 \u6211\u4eec\u9700\u8981\u901a\u8fc7\u67d0\u79cd\u65b9\u5f0f\uff0c\u5c06\u53d8\u91cf\u4f20\u9012\u5230 printDouble \u51fd\u6570\u5185\u90e8\uff0c\u4f7f\u5176\u53ef\u4ee5\u5728\u51fd\u6570\u4f53\u5185\u4f7f\u7528\u8be5\u53d8\u91cf\u3002 \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2 \u5f88\u591a\u65f6\u5019\uff0c\u6211\u4eec\u5e0c\u671b\u80fd\u591f\u5728\u8c03\u7528\u51fd\u6570\u65f6\u5c06\u4fe1\u606f\u4e00\u5e76\u4f20\u5165\uff0c\u8fd9\u6837\u8be5\u51fd\u6570\u5c31\u53ef\u4ee5\u57fa\u4e8e\u6b64\u6570\u636e\u8fdb\u884c\u63a5\u4e0b\u6765\u7684\u5de5\u4f5c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u7f16\u5199\u4e00\u4e2a\u8ba1\u7b97\u4e24\u6570\u548c\u7684\u51fd\u6570\uff0c\u90a3\u4e48\u4f60\u5fc5\u987b\u6709\u529e\u6cd5\u544a\u8bc9\u5728\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u544a\u8bc9\u51fd\u6570\u9700\u8981\u76f8\u52a0\u7684\u4e24\u4e2a\u6570\u3002 \u5426\u5219\uff0c\u51fd\u6570\u5982\u4f55\u624d\u80fd\u77e5\u9053\u8981\u5c06\u4ec0\u4e48\u76f8\u52a0\u5462\uff1f\u901a\u8fc7\u51fd\u6570\u7684 \u5f62\u53c2 \u548c \u5b9e\u53c2 \u53ef\u4ee5\u5b8c\u6210\u4e0a\u8ff0\u5de5\u4f5c\u3002 \u51fd\u6570\u7684 \u5f62\u53c2 \u662f\u80fd\u591f\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u7684\u53d8\u91cf\u3002\u5f62\u53c2\u548c\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u51e0\u4e4e\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u9664\u4e86\uff1a\u5f62\u53c2\u603b\u662f\u7531\u4e3b\u8c03\u51fd\u6570\u521d\u59cb\u5316\u5e76\u63d0\u4f9b\u7ed9\u88ab\u8c03\u51fd\u6570\u3002 \u51fd\u6570\u7684\u5f62\u53c2\u662f\u5728\u51fd\u6570\u58f0\u660e\u65f6\u5b9a\u4e49\u7684\uff0c\u5b83\u4eec\u4f4d\u4e8e\u51fd\u6570\u540d\u540e\u7684\u62ec\u53f7\u4e2d\uff0c\u591a\u4e2a\u5f62\u53c2\u5219\u7531\u9017\u53f7\u9694\u5f00\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u5c55\u793a\u4e86\u5305\u542b\u4e0d\u540c\u6570\u91cf\u5f62\u53c2\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 // \u51fd\u6570\u6ca1\u6709\u53c2\u6570 // \u4e0d\u4f9d\u8d56\u4e0e\u4e3b\u8c03\u51fd\u6570\u63d0\u4f9b\u4efb\u4f55\u503c void doPrint () { std :: cout << \"In doPrint() \\n \" ; } // \u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u540d\u4e3a x \u7684\u6574\u578b\u53c2\u6570 // \u4e3b\u8c03\u51fd\u6570\u9700\u8981\u63d0\u4f9b x \u7684\u503c void printValue ( int x ) { std :: cout << x << '\\n' ; } // \u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u4e00\u4e2a\u540d\u4e3a x\uff0c\u4e00\u4e2a\u540d\u4e3a y // \u4e3b\u8c03\u51fd\u6570\u5fc5\u987b\u63d0\u4f9b x \u548c y \u7684\u503c int add ( int x , int y ) { return x + y ; } \u5b9e\u53c2 \u6307\u7684\u662f\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u4e3b\u8c03\u51fd\u6570\u4f20\u9012\u7ed9\u88ab\u8c03\u51fd\u6570\u7684\u503c\uff1a 1 2 3 doPrint (); // \u6ca1\u6709\u5b9e\u53c2 printValue ( 6 ); // 6 \u662f\u4f20\u9012\u7ed9 printValue() \u7684\u5b9e\u53c2 add ( 2 , 3 ); // 2 \u548c 3 \u662f\u4f20\u9012\u7ed9 add() \u7684\u5b9e\u53c2 \u6ce8\u610f\uff0c\u591a\u4e2a\u5b9e\u53c2\u4ecd\u7136\u7531\u9017\u53f7\u8fdb\u884c\u5206\u5272\u3002 \u5f62\u53c2\u548c\u5b9e\u53c2\u662f\u5982\u4f55\u534f\u540c\u5de5\u4f5c\u7684 \u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u6240\u6709\u7684\u5f62\u53c2\u90fd\u88ab\u5f53\u505a\u53d8\u91cf\u521b\u5efa\uff0c\u7136\u540e\u6240\u6709\u7684\u5b9e\u53c2\u90fd\u88ab\u62f7\u8d1d\u5230\u5bf9\u5e94\u7684\u5f62\u53c2\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a \u6309\u503c\u4f20\u9012 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // This function has two integer parameters, one named x, and one named y // The values of x and y are passed in by the caller void printValues ( int x , int y ) { std :: cout << x << '\\n' ; std :: cout << y << '\\n' ; } int main () { printValues ( 6 , 7 ); // This function call has two arguments, 6 and 7 return 0 ; } \u5f53 printValues \u51fd\u6570\u4f7f\u7528\u5b9e\u53c2 6 \u548c 7 \u8fdb\u884c\u8c03\u7528\u65f6\uff0c\u5f62\u53c2 x \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 6\uff0c\u5f62\u53c2 y \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 7\u3002 \u4e0a\u8ff0\u4ee3\u7801\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 6 7 \u6ce8\u610f\uff0c\u5b9e\u53c2\u7684\u4e2a\u6570\u5fc5\u987b\u548c\u5f62\u53c2\u7684\u4e2a\u6570\u4fdd\u6301\u4e00\u81f4\uff0c\u5426\u5219\u7f16\u8bd1\u5668\u5c31\u4f1a\u62a5\u9519\u3002\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u5b9e\u53c2\u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u7684\u8868\u8fbe\u5f0f\uff08\u56e0\u4e3a\u5b9e\u53c2\u672c\u8d28\u4e0a\u53ea\u662f\u5f62\u53c2\u7684\u521d\u59cb\u5316\u503c\uff0c\u800c\u521d\u59cb\u5316\u503c\u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u8868\u8fbe\u5f0f\uff09\u3002 \u4fee\u6b63\u9519\u8bef\u7684\u7a0b\u5e8f \u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u4fee\u6b63\u672c\u8282\u8bfe\u5f00\u5934\u7684\u7a0b\u5e8f\u4e86\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include int getValueFromUser () { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; } void printDouble ( int value ) // \u51fd\u6570\u73b0\u5728\u6709\u4e00\u4e2a\u6574\u578b\u7684\u5f62\u53c2 { std :: cout << value << \" doubled is: \" << value * 2 << '\\n' ; } int main () { int num { getValueFromUser () }; printDouble ( num ); return 0 ; } \u5728\u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\uff0c\u53d8\u91cf num \u9996\u5148\u88ab\u521d\u59cb\u5316\u4e3a\u7528\u6237\u8f93\u5165\u7684\u503c\u3002\u7136\u540e\u51fd\u6570 printDouble \u88ab\u8c03\u7528\uff0c\u5b9e\u53c2 num \u88ab\u62f7\u8d1d\u5230\u5f62\u53c2\u3002\u51fd\u6570 printDouble \u968f\u540e\u5c31\u53ef\u4ee5\u4f7f\u7528\u8be5\u5f62\u53c2\u7684\u503c\u3002 \u4f7f\u7528\u8fd4\u56de\u503c\u4f5c\u4e3a\u5b9e\u53c2 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u53d8\u91cf num \u53ea\u88ab\u4f7f\u7528\u4e86\u4e00\u6b21\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06 getValueFromUser \u7684\u8fd4\u56de\u503c\u4f5c\u4e3a printDouble \u7684\u5b9e\u53c2\uff0c\u5c06\u4e0a\u9762\u7684\u4ee3\u7801\u7a0d\u52a0\u7b80\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int getValueFromUser () { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; } void printDouble ( int value ) { std :: cout << value << \" doubled is: \" << value * 2 << '\\n' ; } int main () { printDouble ( getValueFromUser ()); return 0 ; } \u73b0\u5728\uff0c\u6211\u4eec\u4f7f\u7528 getValueFromUser \u7684\u8fd4\u56de\u503c\u4f5c\u4e3a\u4e86 printDouble \u7684\u5b9e\u53c2\u3002 \u5c3d\u7ba1\u8fd9\u4e2a\u7a0b\u5e8f\u53d8\u5f97\u66f4\u52a0\u7cbe\u7b80\u4e86\uff08\u800c\u4e14\u4e5f\u5f88\u6e05\u695a\u7684\u8868\u660e\u7528\u6237\u7684\u8f93\u5165\u4ec5\u4ec5\u88ab\u7528\u4e8e\u8fdb\u884c\u8ba1\u7b97\uff09\uff0c\u4f46\u662f\u4f60\u4e5f\u8bb8\u4e5f\u4f1a\u51b3\u5b9a\u8fd9\u79cd\u201c\u538b\u7f29\u8bed\u6cd5\u201d\u53ef\u8bfb\u6027\u5e76\u4e0d\u4f1a\u3002\u5982\u679c\u4f60\u89c9\u5f97\u4e4b\u524d\u7684\u7248\u672c\u66f4\u52a0\u6613\u8bfb\uff0c\u90a3\u4e5f\u662f\u53ef\u4ee5\u7684\u3002 \u5f62\u53c2\u548c\u8fd4\u56de\u503c\u662f\u5982\u4f55\u534f\u540c\u5de5\u4f5c\u7684 \u901a\u8fc7\u4e00\u8d77\u4f7f\u7528\u5f62\u53c2\u548c\u8fd4\u56de\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u63a5\u53d7\u67d0\u4e9b\u6570\u636e\uff0c\u5bf9\u6570\u636e\u8fdb\u884c\u8ba1\u7b97\u5e76\u901a\u8fc7\u8fd4\u56de\u503c\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002 \u4e0b\u9762\u7684\u4ee3\u7801\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u51fd\u6570\u8ba1\u7b97\u4e24\u4e2a\u503c\u7684\u548c\u5e76\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // add() takes two integers as parameters, and returns the result of their sum // The values of x and y are determined by the function that calls add() int add ( int x , int y ) { return x + y ; } // main takes no parameters int main () { std :: cout << add ( 4 , 5 ) << '\\n' ; // Arguments 4 and 5 are passed to function add() return 0 ; } \u7a0b\u5e8f\u4ece main \u51fd\u6570\u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\uff0c\u5f53 add(4,5) \u8bed\u53e5\u6c42\u503c\u65f6\uff0c\u51fd\u6570 add \u88ab\u8c03\u7528\uff0c\u5176\u5f62\u53c2 x \u88ab\u521d\u59cb\u5316\u4e3a 4\uff0c\u5f62\u53c2 y \u88ab\u521d\u59cb\u5316\u4e3a 5\u3002 \u51fd\u6570\u7684 return \u8bed\u53e5\u5bf9 x+y \u8fdb\u884c\u4e86\u6c42\u503c\u5e76\u5f97\u5230\u7ed3\u679c 9\uff0c\u8be5\u503c\u968f\u540e\u88ab\u8fd4\u56de\u7ed9 main \u51fd\u6570\u3002\u7136\u540e 9 \u5c31\u88ab\u8f93\u5165 std::cout \u5e76\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002 \u8f93\u51fa\u7ed3\u679c\uff1a 1 9 \u56fe\u89e3\uff1a \u66f4\u591a\u4f8b\u5b50 \u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u66f4\u591a\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include int add ( int x , int y ) { return x + y ; } int multiply ( int z , int w ) { return z * w ; } int main () { std :: cout << add ( 4 , 5 ) << '\\n' ; // within add() x=4, y=5, so x+y=9 std :: cout << add ( 1 + 2 , 3 * 4 ) << '\\n' ; // within add() x=3, y=12, so x+y=15 int a { 5 }; std :: cout << add ( a , a ) << '\\n' ; // evaluates (5 + 5) std :: cout << add ( 1 , multiply ( 2 , 3 )) << '\\n' ; // evaluates 1 + (2 * 3) std :: cout << add ( 1 , add ( 2 , 3 )) << '\\n' ; // evaluates 1 + (2 + 3) return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 9 15 10 7 6 \u7b2c\u4e00\u884c\u8bed\u53e5\u975e\u5e38\u76f4\u767d\u3002 \u7b2c\u4e8c\u884c\u8bed\u53e5\uff0c\u4f5c\u4e3a\u5b9e\u53c2\u7684\u8868\u8fbe\u5f0f\u5728\u4f20\u9012\u524d\u9996\u5148\u8fdb\u884c\u6c42\u503c\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c 1 + 2 \u6c42\u503c\u5f97\u5230 3 \uff0c\u6240\u4ee5 3 \u500d\u62f7\u8d1d\u5230\u5f62\u53c2 x\u3002 3 * 4 \u6c42\u503c\u5f97\u5230 12 \uff0c\u56e0\u6b64 12 \u88ab\u62f7\u8d1d\u5230\u5f62\u53c2 y\u3002 add(3,12) \u7684\u7ed3\u679c\u4e3a 15 \u3002 \u63a5\u4e0b\u6765\u7684\u4e24\u884c\u8bed\u53e5\u4e5f\u6bd4\u8f83\u7b80\u5355\uff1a 1 2 int a { 5 }; std :: cout << add ( a , a ) << '\\n' ; // evaluates (5 + 5) \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c add() \u53d8\u91cf a \u88ab\u62f7\u8d1d\u5230\u5f62\u53c2 x \u548c y\u3002\u7531\u4e8e a \u7684\u503c\u4e3a 5\uff0c\u5219 add(a, a) = add(5,5) \uff0c\u5176\u8ba1\u7b97\u7ed3\u679c\u4e3a 10\u3002 \u63a5\u4e0b\u6765\u662f\u8fd9\u4e00\u7cfb\u5217\u8bed\u53e5\u4e2d\u6700\u590d\u6742\u7684\u4e00\u6761\uff1a 1 std :: cout << add ( 1 , multiply ( 2 , 3 )) << '\\n' ; // evaluates 1 + (2 * 3) \u5f53\u51fd\u6570 add \u6267\u884c\u65f6\uff0c \u7a0b\u5e8f\u5fc5\u987b\u8981\u786e\u5b9a\u5176\u5f62\u53c2 x \u548c y\u3002x \u5f88\u7b80\u5355\uff0c\u56e0\u4e3a\u4f20\u9012\u7ed9\u5b83\u7684\u503c\u5c31\u662f 1\u3002\u800c\u4e3a\u4e86\u83b7\u53d6\u5f62\u53c2 y \u7684\u503c\uff0c\u7a0b\u5e8f\u5fc5\u987b\u8981\u5bf9 multiply(2,3) \u8fdb\u884c\u6c42\u503c\u3002\u4e3a\u6b64\uff0c\u7a0b\u5e8f\u9996\u5148\u8c03\u7528 multiply \u51fd\u6570\u5e76\u5c06 z \u521d\u59cb\u5316\u4e3a 2\uff0cw \u521d\u59cb\u5316\u4e3a 3\uff0c\u56e0\u6b64 multiply(2,3) \u8fd4\u56de\u503c\u4e3a 6\u3002\u8be5\u503c\u968f\u540e\u88ab\u7528\u6765\u521d\u59cb\u5316 add \u51fd\u6570\u7684\u5f62\u53c2 y\u3002 add(1,6) \u7684\u8fd4\u56de\u503c\u4e3a 7\uff0c\u8be5\u503c\u88ab\u4f20\u9012\u7ed9 std:: cout \u7528\u4e8e\u6253\u5370\u3002 \u7b80\u5355\u6765\u8bf4\uff1a add(1, multiply(2,3)) \u6c42\u503c\u5f97\u5230 add(1,6) \uff0c\u8fdb\u800c\u6c42\u503c\u5f97\u5230 7 \u4e0b\u9762\u7684\u8bed\u53e5\u770b\u4e0a\u53bb\u6709\u70b9\u7ed5\uff0c\u56e0\u4e3a add \u51fd\u6570\u7684\u4e00\u4e2a\u5b9e\u53c2\uff0c\u5176\u503c\u6765\u81ea\u4e8e\u53e6\u5916\u4e00\u4e2a add \u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002 1 std :: cout << add ( 1 , add ( 2 , 3 )) << '\\n' ; // evaluates 1 + (2 + 3) \u4e0d\u8fc7\uff0c\u5176\u5b9e\u5b83\u548c\u524d\u4e00\u4e2a\u4f8b\u5b50\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c add(2,3) \u9996\u5148\u6c42\u503c\uff0c\u8fd4\u56de\u503c\u4e3a 5\u3002\u63a5\u4e0b\u6765\uff0c\u5bf9 add(1,5) \u8fdb\u884c\u6c42\u503c\uff0c\u5176\u7ed3\u679c\u4e3a 6\uff0c\u8be5\u503c\u88ab\u4f20\u9012\u7ed9 std::cout \u7528\u4e8e\u6253\u5370\u3002 \u7b80\u5355\u6765\u8bf4\uff1a add(1, add(2,3)) \u6c42\u503c\u5f97\u5230 add(1,5) \uff0c\u7ee7\u7eed\u6c42\u503c\u5f97\u5230 6\u3002 \u7ed3\u8bba \u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\u662f\u51fd\u6570\u80fd\u591f\u88ab\u91cd\u7528\u7684\u5173\u952e\u673a\u5236\uff0c\u56e0\u4e3a\u8be5\u673a\u5236\u53ef\u4ee5\u5141\u8bb8\u6211\u4eec\u5728\u4e0d\u6307\u5b9a\u5177\u4f53\u503c\u7684\u60c5\u51b5\u4e0b\u7f16\u5199\u51fd\u6570\u83b7\u53d6\u6216\u8ba1\u7b97\u6570\u636e\u5e76\u5c06\u8fd4\u56de\u503c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002","title":"2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2"},{"location":"2-4-Introduction-to-function-parameters-and-arguments/#_1","text":"\u5f88\u591a\u65f6\u5019\uff0c\u6211\u4eec\u5e0c\u671b\u80fd\u591f\u5728\u8c03\u7528\u51fd\u6570\u65f6\u5c06\u4fe1\u606f\u4e00\u5e76\u4f20\u5165\uff0c\u8fd9\u6837\u8be5\u51fd\u6570\u5c31\u53ef\u4ee5\u57fa\u4e8e\u6b64\u6570\u636e\u8fdb\u884c\u63a5\u4e0b\u6765\u7684\u5de5\u4f5c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u7f16\u5199\u4e00\u4e2a\u8ba1\u7b97\u4e24\u6570\u548c\u7684\u51fd\u6570\uff0c\u90a3\u4e48\u4f60\u5fc5\u987b\u6709\u529e\u6cd5\u544a\u8bc9\u5728\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u544a\u8bc9\u51fd\u6570\u9700\u8981\u76f8\u52a0\u7684\u4e24\u4e2a\u6570\u3002 \u5426\u5219\uff0c\u51fd\u6570\u5982\u4f55\u624d\u80fd\u77e5\u9053\u8981\u5c06\u4ec0\u4e48\u76f8\u52a0\u5462\uff1f\u901a\u8fc7\u51fd\u6570\u7684 \u5f62\u53c2 \u548c \u5b9e\u53c2 \u53ef\u4ee5\u5b8c\u6210\u4e0a\u8ff0\u5de5\u4f5c\u3002 \u51fd\u6570\u7684 \u5f62\u53c2 \u662f\u80fd\u591f\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u7684\u53d8\u91cf\u3002\u5f62\u53c2\u548c\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u51e0\u4e4e\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u9664\u4e86\uff1a\u5f62\u53c2\u603b\u662f\u7531\u4e3b\u8c03\u51fd\u6570\u521d\u59cb\u5316\u5e76\u63d0\u4f9b\u7ed9\u88ab\u8c03\u51fd\u6570\u3002 \u51fd\u6570\u7684\u5f62\u53c2\u662f\u5728\u51fd\u6570\u58f0\u660e\u65f6\u5b9a\u4e49\u7684\uff0c\u5b83\u4eec\u4f4d\u4e8e\u51fd\u6570\u540d\u540e\u7684\u62ec\u53f7\u4e2d\uff0c\u591a\u4e2a\u5f62\u53c2\u5219\u7531\u9017\u53f7\u9694\u5f00\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u5c55\u793a\u4e86\u5305\u542b\u4e0d\u540c\u6570\u91cf\u5f62\u53c2\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 // \u51fd\u6570\u6ca1\u6709\u53c2\u6570 // \u4e0d\u4f9d\u8d56\u4e0e\u4e3b\u8c03\u51fd\u6570\u63d0\u4f9b\u4efb\u4f55\u503c void doPrint () { std :: cout << \"In doPrint() \\n \" ; } // \u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u540d\u4e3a x \u7684\u6574\u578b\u53c2\u6570 // \u4e3b\u8c03\u51fd\u6570\u9700\u8981\u63d0\u4f9b x \u7684\u503c void printValue ( int x ) { std :: cout << x << '\\n' ; } // \u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u4e00\u4e2a\u540d\u4e3a x\uff0c\u4e00\u4e2a\u540d\u4e3a y // \u4e3b\u8c03\u51fd\u6570\u5fc5\u987b\u63d0\u4f9b x \u548c y \u7684\u503c int add ( int x , int y ) { return x + y ; } \u5b9e\u53c2 \u6307\u7684\u662f\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u4e3b\u8c03\u51fd\u6570\u4f20\u9012\u7ed9\u88ab\u8c03\u51fd\u6570\u7684\u503c\uff1a 1 2 3 doPrint (); // \u6ca1\u6709\u5b9e\u53c2 printValue ( 6 ); // 6 \u662f\u4f20\u9012\u7ed9 printValue() \u7684\u5b9e\u53c2 add ( 2 , 3 ); // 2 \u548c 3 \u662f\u4f20\u9012\u7ed9 add() \u7684\u5b9e\u53c2 \u6ce8\u610f\uff0c\u591a\u4e2a\u5b9e\u53c2\u4ecd\u7136\u7531\u9017\u53f7\u8fdb\u884c\u5206\u5272\u3002","title":"\u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2"},{"location":"2-4-Introduction-to-function-parameters-and-arguments/#_2","text":"\u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u6240\u6709\u7684\u5f62\u53c2\u90fd\u88ab\u5f53\u505a\u53d8\u91cf\u521b\u5efa\uff0c\u7136\u540e\u6240\u6709\u7684\u5b9e\u53c2\u90fd\u88ab\u62f7\u8d1d\u5230\u5bf9\u5e94\u7684\u5f62\u53c2\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a \u6309\u503c\u4f20\u9012 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // This function has two integer parameters, one named x, and one named y // The values of x and y are passed in by the caller void printValues ( int x , int y ) { std :: cout << x << '\\n' ; std :: cout << y << '\\n' ; } int main () { printValues ( 6 , 7 ); // This function call has two arguments, 6 and 7 return 0 ; } \u5f53 printValues \u51fd\u6570\u4f7f\u7528\u5b9e\u53c2 6 \u548c 7 \u8fdb\u884c\u8c03\u7528\u65f6\uff0c\u5f62\u53c2 x \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 6\uff0c\u5f62\u53c2 y \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 7\u3002 \u4e0a\u8ff0\u4ee3\u7801\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 6 7 \u6ce8\u610f\uff0c\u5b9e\u53c2\u7684\u4e2a\u6570\u5fc5\u987b\u548c\u5f62\u53c2\u7684\u4e2a\u6570\u4fdd\u6301\u4e00\u81f4\uff0c\u5426\u5219\u7f16\u8bd1\u5668\u5c31\u4f1a\u62a5\u9519\u3002\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u5b9e\u53c2\u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u7684\u8868\u8fbe\u5f0f\uff08\u56e0\u4e3a\u5b9e\u53c2\u672c\u8d28\u4e0a\u53ea\u662f\u5f62\u53c2\u7684\u521d\u59cb\u5316\u503c\uff0c\u800c\u521d\u59cb\u5316\u503c\u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u8868\u8fbe\u5f0f\uff09\u3002","title":"\u5f62\u53c2\u548c\u5b9e\u53c2\u662f\u5982\u4f55\u534f\u540c\u5de5\u4f5c\u7684"},{"location":"2-4-Introduction-to-function-parameters-and-arguments/#_3","text":"\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u4fee\u6b63\u672c\u8282\u8bfe\u5f00\u5934\u7684\u7a0b\u5e8f\u4e86\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include int getValueFromUser () { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; } void printDouble ( int value ) // \u51fd\u6570\u73b0\u5728\u6709\u4e00\u4e2a\u6574\u578b\u7684\u5f62\u53c2 { std :: cout << value << \" doubled is: \" << value * 2 << '\\n' ; } int main () { int num { getValueFromUser () }; printDouble ( num ); return 0 ; } \u5728\u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\uff0c\u53d8\u91cf num \u9996\u5148\u88ab\u521d\u59cb\u5316\u4e3a\u7528\u6237\u8f93\u5165\u7684\u503c\u3002\u7136\u540e\u51fd\u6570 printDouble \u88ab\u8c03\u7528\uff0c\u5b9e\u53c2 num \u88ab\u62f7\u8d1d\u5230\u5f62\u53c2\u3002\u51fd\u6570 printDouble \u968f\u540e\u5c31\u53ef\u4ee5\u4f7f\u7528\u8be5\u5f62\u53c2\u7684\u503c\u3002","title":"\u4fee\u6b63\u9519\u8bef\u7684\u7a0b\u5e8f"},{"location":"2-4-Introduction-to-function-parameters-and-arguments/#_4","text":"\u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u53d8\u91cf num \u53ea\u88ab\u4f7f\u7528\u4e86\u4e00\u6b21\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06 getValueFromUser \u7684\u8fd4\u56de\u503c\u4f5c\u4e3a printDouble \u7684\u5b9e\u53c2\uff0c\u5c06\u4e0a\u9762\u7684\u4ee3\u7801\u7a0d\u52a0\u7b80\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int getValueFromUser () { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; } void printDouble ( int value ) { std :: cout << value << \" doubled is: \" << value * 2 << '\\n' ; } int main () { printDouble ( getValueFromUser ()); return 0 ; } \u73b0\u5728\uff0c\u6211\u4eec\u4f7f\u7528 getValueFromUser \u7684\u8fd4\u56de\u503c\u4f5c\u4e3a\u4e86 printDouble \u7684\u5b9e\u53c2\u3002 \u5c3d\u7ba1\u8fd9\u4e2a\u7a0b\u5e8f\u53d8\u5f97\u66f4\u52a0\u7cbe\u7b80\u4e86\uff08\u800c\u4e14\u4e5f\u5f88\u6e05\u695a\u7684\u8868\u660e\u7528\u6237\u7684\u8f93\u5165\u4ec5\u4ec5\u88ab\u7528\u4e8e\u8fdb\u884c\u8ba1\u7b97\uff09\uff0c\u4f46\u662f\u4f60\u4e5f\u8bb8\u4e5f\u4f1a\u51b3\u5b9a\u8fd9\u79cd\u201c\u538b\u7f29\u8bed\u6cd5\u201d\u53ef\u8bfb\u6027\u5e76\u4e0d\u4f1a\u3002\u5982\u679c\u4f60\u89c9\u5f97\u4e4b\u524d\u7684\u7248\u672c\u66f4\u52a0\u6613\u8bfb\uff0c\u90a3\u4e5f\u662f\u53ef\u4ee5\u7684\u3002","title":"\u4f7f\u7528\u8fd4\u56de\u503c\u4f5c\u4e3a\u5b9e\u53c2"},{"location":"2-4-Introduction-to-function-parameters-and-arguments/#_5","text":"\u901a\u8fc7\u4e00\u8d77\u4f7f\u7528\u5f62\u53c2\u548c\u8fd4\u56de\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u63a5\u53d7\u67d0\u4e9b\u6570\u636e\uff0c\u5bf9\u6570\u636e\u8fdb\u884c\u8ba1\u7b97\u5e76\u901a\u8fc7\u8fd4\u56de\u503c\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002 \u4e0b\u9762\u7684\u4ee3\u7801\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u51fd\u6570\u8ba1\u7b97\u4e24\u4e2a\u503c\u7684\u548c\u5e76\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // add() takes two integers as parameters, and returns the result of their sum // The values of x and y are determined by the function that calls add() int add ( int x , int y ) { return x + y ; } // main takes no parameters int main () { std :: cout << add ( 4 , 5 ) << '\\n' ; // Arguments 4 and 5 are passed to function add() return 0 ; } \u7a0b\u5e8f\u4ece main \u51fd\u6570\u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\uff0c\u5f53 add(4,5) \u8bed\u53e5\u6c42\u503c\u65f6\uff0c\u51fd\u6570 add \u88ab\u8c03\u7528\uff0c\u5176\u5f62\u53c2 x \u88ab\u521d\u59cb\u5316\u4e3a 4\uff0c\u5f62\u53c2 y \u88ab\u521d\u59cb\u5316\u4e3a 5\u3002 \u51fd\u6570\u7684 return \u8bed\u53e5\u5bf9 x+y \u8fdb\u884c\u4e86\u6c42\u503c\u5e76\u5f97\u5230\u7ed3\u679c 9\uff0c\u8be5\u503c\u968f\u540e\u88ab\u8fd4\u56de\u7ed9 main \u51fd\u6570\u3002\u7136\u540e 9 \u5c31\u88ab\u8f93\u5165 std::cout \u5e76\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002 \u8f93\u51fa\u7ed3\u679c\uff1a 1 9 \u56fe\u89e3\uff1a","title":"\u5f62\u53c2\u548c\u8fd4\u56de\u503c\u662f\u5982\u4f55\u534f\u540c\u5de5\u4f5c\u7684"},{"location":"2-4-Introduction-to-function-parameters-and-arguments/#_6","text":"\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u66f4\u591a\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include int add ( int x , int y ) { return x + y ; } int multiply ( int z , int w ) { return z * w ; } int main () { std :: cout << add ( 4 , 5 ) << '\\n' ; // within add() x=4, y=5, so x+y=9 std :: cout << add ( 1 + 2 , 3 * 4 ) << '\\n' ; // within add() x=3, y=12, so x+y=15 int a { 5 }; std :: cout << add ( a , a ) << '\\n' ; // evaluates (5 + 5) std :: cout << add ( 1 , multiply ( 2 , 3 )) << '\\n' ; // evaluates 1 + (2 * 3) std :: cout << add ( 1 , add ( 2 , 3 )) << '\\n' ; // evaluates 1 + (2 + 3) return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 9 15 10 7 6 \u7b2c\u4e00\u884c\u8bed\u53e5\u975e\u5e38\u76f4\u767d\u3002 \u7b2c\u4e8c\u884c\u8bed\u53e5\uff0c\u4f5c\u4e3a\u5b9e\u53c2\u7684\u8868\u8fbe\u5f0f\u5728\u4f20\u9012\u524d\u9996\u5148\u8fdb\u884c\u6c42\u503c\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c 1 + 2 \u6c42\u503c\u5f97\u5230 3 \uff0c\u6240\u4ee5 3 \u500d\u62f7\u8d1d\u5230\u5f62\u53c2 x\u3002 3 * 4 \u6c42\u503c\u5f97\u5230 12 \uff0c\u56e0\u6b64 12 \u88ab\u62f7\u8d1d\u5230\u5f62\u53c2 y\u3002 add(3,12) \u7684\u7ed3\u679c\u4e3a 15 \u3002 \u63a5\u4e0b\u6765\u7684\u4e24\u884c\u8bed\u53e5\u4e5f\u6bd4\u8f83\u7b80\u5355\uff1a 1 2 int a { 5 }; std :: cout << add ( a , a ) << '\\n' ; // evaluates (5 + 5) \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c add() \u53d8\u91cf a \u88ab\u62f7\u8d1d\u5230\u5f62\u53c2 x \u548c y\u3002\u7531\u4e8e a \u7684\u503c\u4e3a 5\uff0c\u5219 add(a, a) = add(5,5) \uff0c\u5176\u8ba1\u7b97\u7ed3\u679c\u4e3a 10\u3002 \u63a5\u4e0b\u6765\u662f\u8fd9\u4e00\u7cfb\u5217\u8bed\u53e5\u4e2d\u6700\u590d\u6742\u7684\u4e00\u6761\uff1a 1 std :: cout << add ( 1 , multiply ( 2 , 3 )) << '\\n' ; // evaluates 1 + (2 * 3) \u5f53\u51fd\u6570 add \u6267\u884c\u65f6\uff0c \u7a0b\u5e8f\u5fc5\u987b\u8981\u786e\u5b9a\u5176\u5f62\u53c2 x \u548c y\u3002x \u5f88\u7b80\u5355\uff0c\u56e0\u4e3a\u4f20\u9012\u7ed9\u5b83\u7684\u503c\u5c31\u662f 1\u3002\u800c\u4e3a\u4e86\u83b7\u53d6\u5f62\u53c2 y \u7684\u503c\uff0c\u7a0b\u5e8f\u5fc5\u987b\u8981\u5bf9 multiply(2,3) \u8fdb\u884c\u6c42\u503c\u3002\u4e3a\u6b64\uff0c\u7a0b\u5e8f\u9996\u5148\u8c03\u7528 multiply \u51fd\u6570\u5e76\u5c06 z \u521d\u59cb\u5316\u4e3a 2\uff0cw \u521d\u59cb\u5316\u4e3a 3\uff0c\u56e0\u6b64 multiply(2,3) \u8fd4\u56de\u503c\u4e3a 6\u3002\u8be5\u503c\u968f\u540e\u88ab\u7528\u6765\u521d\u59cb\u5316 add \u51fd\u6570\u7684\u5f62\u53c2 y\u3002 add(1,6) \u7684\u8fd4\u56de\u503c\u4e3a 7\uff0c\u8be5\u503c\u88ab\u4f20\u9012\u7ed9 std:: cout \u7528\u4e8e\u6253\u5370\u3002 \u7b80\u5355\u6765\u8bf4\uff1a add(1, multiply(2,3)) \u6c42\u503c\u5f97\u5230 add(1,6) \uff0c\u8fdb\u800c\u6c42\u503c\u5f97\u5230 7 \u4e0b\u9762\u7684\u8bed\u53e5\u770b\u4e0a\u53bb\u6709\u70b9\u7ed5\uff0c\u56e0\u4e3a add \u51fd\u6570\u7684\u4e00\u4e2a\u5b9e\u53c2\uff0c\u5176\u503c\u6765\u81ea\u4e8e\u53e6\u5916\u4e00\u4e2a add \u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002 1 std :: cout << add ( 1 , add ( 2 , 3 )) << '\\n' ; // evaluates 1 + (2 + 3) \u4e0d\u8fc7\uff0c\u5176\u5b9e\u5b83\u548c\u524d\u4e00\u4e2a\u4f8b\u5b50\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c add(2,3) \u9996\u5148\u6c42\u503c\uff0c\u8fd4\u56de\u503c\u4e3a 5\u3002\u63a5\u4e0b\u6765\uff0c\u5bf9 add(1,5) \u8fdb\u884c\u6c42\u503c\uff0c\u5176\u7ed3\u679c\u4e3a 6\uff0c\u8be5\u503c\u88ab\u4f20\u9012\u7ed9 std::cout \u7528\u4e8e\u6253\u5370\u3002 \u7b80\u5355\u6765\u8bf4\uff1a add(1, add(2,3)) \u6c42\u503c\u5f97\u5230 add(1,5) \uff0c\u7ee7\u7eed\u6c42\u503c\u5f97\u5230 6\u3002","title":"\u66f4\u591a\u4f8b\u5b50"},{"location":"2-4-Introduction-to-function-parameters-and-arguments/#_7","text":"\u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\u662f\u51fd\u6570\u80fd\u591f\u88ab\u91cd\u7528\u7684\u5173\u952e\u673a\u5236\uff0c\u56e0\u4e3a\u8be5\u673a\u5236\u53ef\u4ee5\u5141\u8bb8\u6211\u4eec\u5728\u4e0d\u6307\u5b9a\u5177\u4f53\u503c\u7684\u60c5\u51b5\u4e0b\u7f16\u5199\u51fd\u6570\u83b7\u53d6\u6216\u8ba1\u7b97\u6570\u636e\u5e76\u5c06\u8fd4\u56de\u503c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002","title":"\u7ed3\u8bba"},{"location":"2-5-Introduction-to-local-scope/","tags":["scope"],"text":"Key Takeaway \u51fd\u6570\u5f62\u53c2\u4e5f\u662f\u5c40\u90e8\u53d8\u91cf \u53d8\u91cf\u7684\u521b\u5efa\u548c\u9500\u6bc1\u90fd\u662f\u5728\u8fd0\u884c\u65f6\u53d1\u751f\u7684\uff0c\u56e0\u6b64\uff0c\u751f\u547d\u5468\u671f\u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\u5c5e\u6027\u3002\u800c\u4f5c\u7528\u57df\u662f\u4e00\u4e2a\u7f16\u8bd1\u65f6\u5c5e\u6027 \u5bf9\u8c61\u7684\u521b\u5efa\u548c\u521d\u59cb\u5316\u4e0d\u665a\u4e8e\u5176\u5b9a\u4e49\uff0c\u800c\u9500\u6bc1\u5219\u4e0d\u80fd\u65e9\u4e8e\u82b1\u62ec\u53f7\u7684\u7ed3\u5c3e\uff0c\u8fd9\u70b9\u5fc5\u987b\u4fdd\u8bc1 \u51fa\u4e8e\u4f18\u5316\u7684\u76ee\u7684\uff0c\u5bf9\u8c61\u53ef\u4ee5\u88ab\u65e9\u4e00\u70b9\u521b\u5efa\u6216\u665a\u4e00\u70b9\u9500\u6bc1\u3002\u591a\u6570\u65f6\u5019\uff0c\u5c40\u90e8\u53d8\u91cf\u4f1a\u5728\u51fd\u6570\u8fdb\u5165\u65f6\u521b\u5efa\uff0c\u5e76\u4ee5\u76f8\u53cd\u7684\u987a\u5e8f\u5728\u51fd\u6570\u9000\u51fa\u65f6\u9500\u6bc1\u3002 \u8d85\u51fa\u4f5c\u7528\u57df\u548c\u79bb\u5f00\u4f5c\u7528\u57df\u63cf\u8ff0\u7684\u5bf9\u8c61\u4e0d\u540c\uff0c\u524d\u8005\u63cf\u8ff0\u6807\u8bc6\u7b26\uff0c\u540e\u8005\u63cf\u8ff0\u5bf9\u8c61 \u5c3d\u91cf\u5728\u5c40\u90e8\u53d8\u91cf\u7b2c\u4e00\u6b21\u88ab\u4f7f\u7528\u524d\u6700\u8fd1\u7684\u5730\u65b9\u5b9a\u4e49\u8be5\u5c40\u90e8\u53d8\u91cf \u5c40\u90e8\u53d8\u91cf \u51fd\u6570\u7684 \u5f62\u53c2 \u548c\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u4e00\u6837\uff0c\u90fd\u662f \u5c40\u90e8\u53d8\u91cf(local variable) (\u5c40\u90e8\u53d8\u91cf\u662f\u548c \u5168\u5c40\u53d8\u91cf(global variable) \u76f8\u5bf9\u7684\u6982\u5ff5\uff0c\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5168\u5c40\u53d8\u91cf\uff09\u3002 1 2 3 4 5 6 int add ( int x , int y ) // function parameters x and y are local variables { int z { x + y }; // z is a local variable too return z ; } \u672c\u8282\u8bfe\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5c40\u90e8\u53d8\u91cf\u6240\u5177\u6709\u7684\u4e00\u4e9b\u5c5e\u6027\u3002 \u5c40\u90e8\u53d8\u91cf\u751f\u547d\u5468\u671f \u5728 1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf \u4e2d\u6211\u4eec\u8ba8\u8bba\u4e86\u4ee5\u5f62\u5982 int x; \u7684\u65b9\u5f0f\u5b9a\u4e49\u53d8\u91cf\u662f\u5982\u4f55\u5728\u8bed\u53e5\u6267\u884c\u65f6 \u5b9e\u4f8b\u5316 \u53d8\u91cf\u7684\u3002\u51fd\u6570\u7684 \u5f62\u53c2 \u662f\u5728\u8fdb\u5165\u51fd\u6570\u65f6\u88ab\u521b\u5efa\u548c\u521d\u59cb\u5316\u7684\uff0c\u51fd\u6570\u4f53\u5185\u7684\u51fd\u6570\u5219\u662f\u5728\u5b9a\u4e49\u65f6\u521b\u5efa\u5e76\u521d\u59cb\u5316\u7684\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 int add ( int x , int y ) // x \u548c y \u5728\u6b64\u5904\u521b\u5efa\u5e76\u521d\u59cb\u5316 { int z { x + y }; // z \u5728\u6b64\u5904\u88ab\u521b\u5efa\u548c\u521d\u59cb\u5316 return z ; } \u6b64\u65f6\u6211\u4eec\u4e0d\u7ecf\u8981\u95ee\uff0c\u201d\u90a3\u4e48\u5b9e\u4f8b\u5316\u7684\u53d8\u91cf\u662f\u5728\u5408\u9002\u88ab\u9500\u6bc1\u7684\u5462\uff1f\u201c\u3002\u5c40\u90e8\u53d8\u91cf\u662f\u5728 \u5305\u542b\u5176\u5b9a\u4e49\u7684\u4e00\u7ec4\u82b1\u62ec\u53f7\u7684\u7ed3\u5c3e \u65f6\u9500\u6bc1\u7684\uff0c\u5176\u9500\u6bc1\u7684\u987a\u5e8f\u548c\u5b9a\u4e49\u987a\u5e8f\u6b63\u597d\u76f8\u53cd\uff08\u5bf9\u4e8e \u5f62\u53c2 \u6765\u8bf4\uff0c\u5219\u662f\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u9500\u6bc1\uff09\u3002 1 2 3 4 5 6 int add ( int x , int y ) { int z { x + y }; return z ; } // z, y, and x destroyed here \u6b63\u5982\u4eba\u7684\u751f\u547d\u5468\u671f\u88ab\u5b9a\u4e49\u4e3a\u5176\u51fa\u751f\u5230\u6b7b\u4ea1\u7684\u8fd9\u6bb5\u65f6\u95f4\u4e00\u6837\uff0c\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f(lifetime)\u6307\u7684\u662f\u8be5\u53d8\u91cf\u88ab\u521b\u5efa\u5230\u88ab\u9500\u6bc1\u7684\u8fd9\u6bb5\u65f6\u95f4\u3002\u6ce8\u610f\uff0c\u53d8\u91cf\u7684\u521b\u5efa\u548c\u9500\u6bc1\u90fd\u662f\u5728\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u5019\u53d1\u751f\u7684\uff08\u79f0\u4e3a \u8fd0\u884c\u65f6 \uff09\uff0c\u800c\u4e0d\u662f\u5728 \u7f16\u8bd1\u65f6 \u53d1\u751f\u7684\u3002\u56e0\u6b64\uff0c\u751f\u547d\u5468\u671f\u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\u5c5e\u6027\u3002 \u6269\u5c55\u9605\u8bfb \u4e0a\u9762\u5173\u4e8e\u521b\u5efa\u3001\u521d\u59cb\u5316\u548c\u9500\u6bc1\u7684\u89c4\u5219\u662f\u5fc5\u987b\u8981\u6ee1\u8db3\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u521d\u59cb\u5316\u4e0d\u665a\u4e8e\u5176\u5b9a\u4e49\uff0c\u800c\u9500\u6bc1\u5219\u4e0d\u80fd\u65e9\u4e8e\u82b1\u62ec\u53f7\u7684\u7ed3\u5c3e\uff08\u5bf9\u4e8e\u51fd\u6570\u5f62\u53c2\u6765\u8bf4\u5219\u662f\u51fd\u6570\u7ed3\u5c3e\uff09\u3002 \u5b9e\u9645\u4e0a\uff0cC++ \u7684\u89c4\u8303\u7ed9\u4e0e\u7f16\u8bd1\u5668\u4e86\u4e00\u4e9b\u7075\u6d3b\u6027\uff0c\u4f7f\u7f16\u8bd1\u5668\u53ef\u4ee5\u51b3\u5b9a\u4f55\u65f6\u521b\u5efa\u6216\u9500\u6bc1\u5c40\u90e8\u53d8\u91cf\u3002\u51fa\u4e8e\u4f18\u5316\u7684\u76ee\u7684\uff0c\u5bf9\u8c61\u53ef\u4ee5\u88ab\u65e9\u4e00\u70b9\u521b\u5efa\u6216\u665a\u4e00\u70b9\u9500\u6bc1\u3002\u591a\u6570\u65f6\u5019\uff0c\u5c40\u90e8\u53d8\u91cf\u4f1a\u5728\u51fd\u6570\u8fdb\u5165\u65f6\u521b\u5efa\uff0c\u5e76\u4ee5\u76f8\u53cd\u7684\u987a\u5e8f\u5728\u51fd\u6570\u9000\u51fa\u65f6\u9500\u6bc1\u3002\u6211\u4eec\u4f1a\u5728\u5c06\u6765\u7684\u6709\u5173\u51fd\u6570\u8c03\u7528\u6808\u8bfe\u7a0b\u4e2d\u5bf9\u5176\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002 \u4e0b\u9762\u8fd9\u6bb5\u7a0b\u5e8f\u5c55\u793a\u4e86\u6709\u5173\u53d8\u91cf x \u751f\u547d\u5468\u671f\u66f4\u590d\u6742\u7684\u60c5\u51b5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include void doSomething () { std :: cout << \"Hello! \\n \" ; } int main () { int x { 0 }; // x \u7684\u751f\u547d\u5468\u671f\u8d77\u70b9 doSomething (); // \u5728\u51fd\u6570\u8c03\u7528\u671f\u95f4\uff0cx \u4ecd\u7136\u5b58\u5728 return 0 ; } // x \u7684\u751f\u547d\u5468\u671f\u7ec8\u70b9 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cx \u7684\u751f\u547d\u5468\u671f\u4ece\u5b83\u7684\u5b9a\u4e49\u5f00\u59cb\u4e00\u76f4\u5230 main \u51fd\u6570\u7684\u672b\u5c3e\u7ed3\u675f\u3002\u8fd9\u5176\u4e2d\u5305\u542b\u4e86\u51fd\u6570 doSomething \u6267\u884c\u7684\u65f6\u95f4\u3002 \u5c40\u90e8\u4f5c\u7528\u57df \u4e00\u4e2a \u6807\u8bc6\u7b26 \u7684 \u4f5c\u7528\u57df(scope) \u8868\u660e\u4e86\u8be5\u8868\u793a\u7b26\u5728\u6e90\u7801\u4e2d\u80fd\u591f\u88ab\u8bbf\u95ee\u7684\u4f4d\u7f6e\u3002\u5982\u679c\u4e00\u4e2a\u6807\u8bc6\u7b26\u53ef\u4ee5\u88ab\u8bbf\u95ee\uff0c\u5219\u79f0\u5176\u5728\u4f5c\u7528\u57df\u4e2d\u3002\u5f53\u4e00\u4e2a\u6807\u8bc6\u7b26\u4e0d\u80fd\u88ab\u8bbf\u95ee\u65f6\uff0c\u5219\u79f0\u5176\u8d85\u51fa\u4e86\u4f5c\u7528\u57df\uff08Out of scope\uff09\u3002\u4f5c\u7528\u57df\u662f\u4e00\u4e2a \u7f16\u8bd1\u65f6 \u5c5e\u6027\uff0c\u5982\u679c\u5728\u6807\u8bc6\u7b26\u7684\u4f5c\u7528\u57df\u4e4b\u5916\u8bbf\u95ee\u5b83\uff0c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 \u5c40\u90e8\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4ece\u8be5\u53d8\u91cf\u7684\u5b9a\u4e49\u5f00\u59cb\uff0c\u5728\u5305\u542b\u4e86\u8be5\u53d8\u91cf\u5b9a\u4e49\u7684\u4e00\u7ec4\u82b1\u62ec\u53f7\u7684\u7ed3\u5c3e\u7ec8\u6b62\uff08\u5bf9\u4e8e\u51fd\u6570 \u5f62\u53c2 \u5219\u5728\u51fd\u6570\u7ed3\u5c3e\u7ec8\u6b62\uff09\u3002\u8fd9\u786e\u4fdd\u4e86\u53d8\u91cf\u5728\u5b9a\u4e49\u524d\u4e0d\u80fd\u88ab\u4f7f\u7528\uff08\u5373\u4f7f\u7f16\u8bd1\u5668\u9009\u62e9\u5728\u6b64\u4e4b\u524d\u521b\u5efa\u53d8\u91cf\uff09\u3002\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d\u5b9a\u4e49\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u5176\u4f5c\u7528\u57df\u4e0d\u80fd\u5ef6\u4f38\u5230\u5176\u4ed6\u51fd\u6570\u4e2d\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u53d8\u91cf x \u7684\u4f5c\u7528\u57df\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include // x \u4e0d\u5728\u8be5\u51fd\u6570\u7684\u4f5c\u7528\u57df void doSomething () { std :: cout << \"Hello! \\n \" ; } int main () { // x \u6b64\u65f6\u8fd8\u4e0d\u80fd\u88ab\u8bbf\u95ee\uff0c\u56e0\u4e3a\u8fd8\u6ca1\u6709\u8fdb\u5165\u5176\u4f5c\u7528\u57df int x { 0 }; // x \u4ece\u6b64\u65f6\u8fdb\u5165\u4f5c\u7528\u57df\uff0c\u968f\u540e\u53ef\u4ee5\u5728\u8be5\u51fd\u6570\u4e2d\u4f7f\u7528\u5b83 doSomething (); return 0 ; } // x \u4f5c\u7528\u57df\u7ec8\u6b62\u4e0e\u6b64\uff0c\u6b64\u540e\u4e0d\u518d\u80fd\u591f\u88ab\u4f7f\u7528 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u53d8\u91cf x \u4ece\u5176\u5b9a\u4e49\u4f4d\u7f6e\u5f00\u59cb\u8fdb\u5165\u4f5c\u7528\u57df\uff0c\u5728 main \u51fd\u6570\u7ed3\u675f\u65f6\u79bb\u5f00\u5176\u4f5c\u7528\u57df\u3002\u6ce8\u610f\uff0c\u53d8\u91cf x \u7684\u4f5c\u7528\u57df\u4e0d\u4f1a\u5ef6\u4f38\u5230\u51fd\u6570 doSomething() \u4e2d\u3002\u8fd9\u4e5f\u4e00\u4f4d\u7f6e main \u51fd\u6570\u8c03\u7528 doSomething \u548c\u8be5\u8be5\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u6ca1\u6709\u4efb\u4f55\u5173\u7cfb\u3002 \u201c\u8d85\u51fa\u4f5c\u7528\u57df\u201d \u548c \u201c\u79bb\u5f00\u4f5c\u7528\u57df\u201d \u672f\u8bed \u8d85\u51fa\u4f5c\u7528\u57df \u548c \u79bb\u5f00\u4f5c\u7528\u57df \u5bf9\u4e8e\u65b0\u624b\u6765\u8bf4\u53ef\u80fd\u4ee4\u4eba\u56f0\u60d1\u3002 \u6807\u8bc6\u7b26\u8d85\u51fa\u4f5c\u7528\u57df\u8868\u793a\u5176\u4e0d\u80fd\u5728\u4ee3\u7801\u4e2d\u88ab\u8bbf\u95ee\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c \u6807\u8bc6\u7b26(identifier) x \u8fdb\u5165\u4f5c\u7528\u57df\u7684\u65f6\u95f4\u4ece\u5176\u5b9a\u4e49\u5f00\u59cb\u5230 main \u51fd\u6570\u7ed3\u5c3e\u4e3a\u6b62\u3002\u8fd9\u6bb5\u4ee3\u7801\u4ee5\u5916\u7684\u4ee3\u7801\u4e2d\u8bbf\u95ee\u8be5\u6807\u8bc6\u7b26\uff0c\u90fd\u5c5e\u4e8e\u8d85\u51fa\u4f5c\u7528\u57df\u3002 \u672f\u8bed\u79bb\u5f00\u4f5c\u7528\u57df\u63cf\u8ff0\u7684\u5bf9\u8c61\u5219\u4e00\u822c\u662f\u67d0\u4e2a\u5bf9\u8c61\u800c\u4e0d\u662f\u6807\u8bc6\u7b26\u3002\u6211\u4eec\u8bf4\u4e00\u4e2a\u53d8\u91cf\u4f1a\u5728\u5b9e\u4f8b\u5316\u5b83\u4f5c\u7528\u57df\u7684\u7ed3\u5c3e\u5904\uff08\u82b1\u62ec\u53f7\u7ed3\u675f\u5904\uff09\u79bb\u5f00\u4f5c\u7528\u57df\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c \u5bf9\u8c61 x \u4f1a\u5728 main \u51fd\u6570\u7684\u7ed3\u5c3e\u5904\u79bb\u5f00\u4f5c\u7528\u57df\u3002 \u5177\u5907\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u4f1a\u5728\u5176\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\u7ec8\u6b62\uff0c\u5c40\u90e8\u53d8\u91cf\u4f1a\u5728\u6b64\u65f6\u88ab\u9500\u6bc1\u3002 \u6ce8\u610f\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u7c7b\u578b\u7684\u53d8\u91cf\u90fd\u4f1a\u5728\u5176\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\u88ab\u9500\u6bc1\u3002\u6211\u4eec\u4f1a\u5728\u5c06\u6765\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u6b64\u7c7b\u4f8b\u5b50\u3002 \u5176\u4ed6\u4f8b\u5b50 \u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u7a0d\u5fae\u66f4\u590d\u6742\u4e00\u5b9a\u3002\u8bf7\u8bb0\u4f4f\uff0c\u751f\u547d\u5468\u671f\u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\u5c5e\u6027\uff0c\u800c\u4f5c\u7528\u57df\u662f\u7f16\u8bd1\u65f6\u5c5e\u6027\u3002\u867d\u7136\uff0c\u6211\u4eec\u5728\u540c\u4e00\u6bb5\u4ee3\u7801\u4e2d\u8ba8\u8bba\u4e24\u8005\uff0c\u5b83\u4eec\u7684\u51fa\u53d1\u70b9\u662f\u4e0d\u540c\u7684\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int add ( int x , int y ) // x \u548c y \u5728\u6b64\u5904\u88ab\u521b\u5efa\u5e76\u8fdb\u5165\u4f5c\u7528\u57df { // x \u548c y \u4ec5\u5728\u8be5\u51fd\u6570\u4e2d\u53ef\u89c1/\u53ef\u7528 return x + y ; } // y \u548c x \u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1 int main () { int a { 5 }; // a \u5728\u6b64\u5904\u88ab\u521b\u5efa\u3001\u521d\u59cb\u5316\u5e76\u8fdb\u5165\u4f5c\u7528\u57df int b { 6 }; // b \u5728\u6b64\u5904\u88ab\u521b\u5efa\u3001\u521d\u59cb\u5316\u5e76\u8fdb\u5165\u4f5c\u7528\u57df // a \u548c b \u53ea\u5728\u8be5\u51fd\u6570\u4e2d\u53ef\u7528 std :: cout << add ( a , b ) << '\\n' ; // calls function add() with x=5 and y=6 return 0 ; } // b \u548c a \u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1 \u51fd\u6570\u5f62\u53c2 x \u548c y \u5728 add \u51fd\u6570\u88ab\u8c03\u7528\u65f6\u521b\u5efa\uff0c\u4f5c\u4e3a\u53d8\u91cf\uff0c\u5b83\u4eec\u53ea\u5728 add \u51fd\u6570\u4e2d\u53ef\u89c1\u53ef\u7528\uff0c\u5e76\u5728 add \u51fd\u6570\u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u53d8\u91cf a \u548c b \u5728\u51fd\u6570 main \u4e2d\u88ab\u521b\u5efa\uff0c\u53ea\u5728 main \u51fd\u6570\u4e2d\u53ef\u7528\uff0c\u5e76\u4e14\u5728 main \u51fd\u6570\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u3002 \u4e3a\u4e86\u5f3a\u5316\u7406\u89e3\uff0c\u8ba9\u6211\u4eec\u9010\u6b65\u89e3\u6790\u8be5\u51fd\u6570\uff0c\u4e0b\u5217\u64cd\u4f5c\u6309\u987a\u5e8f\u53d1\u751f\uff1a - \u7a0b\u5e8f\u4ece main \u51fd\u6570\u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\uff1b - main \u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf a \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 5\uff1b - main \u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf b \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 6\uff1b - add \u51fd\u6570\u88ab\u8c03\u7528\uff0c\u51fd\u6570\u7684\u5b9e\u53c2\u4e3a 5 \u548c 6\uff1b - add \u7684\u53d8\u91cf x \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 5\uff1b - add \u7684\u53d8\u91cf y \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 6\uff1b - \u8868\u8fbe\u5f0f x + y \u6c42\u503c\u5f97\u5230\u7ed3\u679c 11\uff1b - add \u5c06\u503c 11 \u62f7\u8d1d\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570 main \uff1b - add \u7684 y \u548c x \u88ab\u9500\u6bc1\uff1b - main \u5728\u63a7\u5236\u53f0\u6253\u5370 11\uff1b - main \u8fd4\u56de 0 \u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff1b - main \u7684\u5c40\u90e8\u53d8\u91cf b \u548c a \u88ab\u9500\u6bc1\uff1b \u4ee5\u4e0a\u3002 \u6ce8\u610f\uff0c\u5982\u679c\u51fd\u6570 add \u88ab\u5148\u540e\u4e24\u6b21\u8c03\u7528\uff0c\u90a3\u4e48\u5bf9\u5e94\u7684\u5f62\u53c2 x \u548c y \u4e5f\u88ab\u5148\u540e\u521b\u5efa\u5e76\u9500\u6bc1\u4e24\u6b21\u2014\u2014\u6bcf\u6b21\u8c03\u7528\u5c31\u4f1a\u53d1\u751f\u4e00\u6b21\u521b\u5efa\u548c\u9500\u6bc1\u3002\u5982\u679c\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\u6709\u5927\u91cf\u7684\u51fd\u6570\u8c03\u7528\uff0c\u90a3\u4e48\u53d8\u91cf\u4e5f\u4f1a\u968f\u4e4b\u88ab\u9891\u7e41\u7684\u521b\u5efa\u548c\u9500\u6bc1\u3002 \u51fd\u6570\u7684\u9694\u79bb \u5728\u4e0a\u8ff0\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f88\u660e\u663e\u53ef\u4ee5\u770b\u51fa\uff0c\u53d8\u91cf a \u548c b \u662f\u4e0d\u540c\u4e8e\u53d8\u91cf x \u548c y \u7684\u53d8\u91cf\u3002 \u73b0\u5728\uff0c\u8bf7\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int add ( int x , int y ) // x \u548c y \u5728\u6b64\u5904\u88ab\u521b\u5efa\u5e76\u8fdb\u5165\u4f5c\u7528\u57df { // x \u548c y \u4ec5\u5728\u8be5\u51fd\u6570\u4e2d\u53ef\u89c1/\u53ef\u7528 return x + y ; } // y \u548c x \u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1 int main () { int x { 5 }; // x \u5728\u6b64\u5904\u88ab\u521b\u5efa\u3001\u521d\u59cb\u5316\u5e76\u8fdb\u5165\u4f5c\u7528\u57df int y { 6 }; // y \u5728\u6b64\u5904\u88ab\u521b\u5efa\u3001\u521d\u59cb\u5316\u5e76\u8fdb\u5165\u4f5c\u7528\u57df // x \u548c y \u53ea\u5728\u8be5\u51fd\u6570\u4e2d\u53ef\u7528 std :: cout << add ( x , y ) << '\\n' ; // calls function add() with x=5 and y=6 return 0 ; } // x \u548c y \u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1 \u4e0e\u524d\u4e00\u4e2a\u4f8b\u5b50\u76f8\u6bd4\uff0c\u672c\u4f8b\u53ea\u662f\u5c06 main \u51fd\u6570\u4e2d\u7684\u53d8\u91cf a \u548c b \u4fee\u6539\u6210\u4e86 x \u548c y \u3002\u8fd9\u4e2a\u7a0b\u5e8f\u7684\u7f16\u8bd1\u548c\u8fd0\u884c\u548c\u4e4b\u524d\u7684\u7a0b\u5e8f\u6ca1\u6709\u4efb\u4f55\u533a\u522b\uff0c\u5373\u4f7f main \u51fd\u6570\u548c add \u51fd\u6570\u90fd\u4f7f\u7528\u4e86\u53d8\u91cf\u540d x \u548c y \u3002\u4e3a\u4ec0\u4e48\u8fd9\u6837\u4e5f\u53ef\u4ee5\u5de5\u4f5c\u5462\uff1f \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u8ba4\u8bc6\u5230\u7684\u662f\uff0c\u5373\u4f7f main \u51fd\u6570\u548c add \u51fd\u6570\u90fd\u4f7f\u7528\u4e86\u53d8\u91cf\u540d x \u548c y \uff0c\u4f46\u662f\u8fd9\u4e9b\u53d8\u91cf\u5b9e\u9645\u4e0a\u662f\u5b8c\u5168\u4e0d\u540c\u7684\u3002 main \u51fd\u6570\u4e2d\u7684 x \u548c y \uff0c\u548c add \u51fd\u6570\u4e2d\u7684 x \u548c y \u662f\u6ca1\u6709\u4efb\u4f55\u5173\u7cfb\u7684\u2014\u2014\u5b83\u4eec\u53ea\u662f\u78b0\u5de7\u540c\u540d\u7f62\u4e86\u3002 \u5176\u6b21\uff0c\u5728 main \u51fd\u6570\u4e2d\uff0c \u53d8\u91cf\u540d x \u548c y \u6307\u7684\u662f main \u51fd\u6570\u5c40\u90e8\u4f5c\u7528\u4e2d\u7684\u53d8\u91cf x \u548c y \u3002\u8fd9\u4e9b\u53d8\u91cf\u53ea\u80fd\u591f\u5728 main \u51fd\u6570\u4e2d\u88ab\u770b\u5230\uff08\u88ab\u4f7f\u7528\uff09\u3002\u540c\u6837\u7684\uff0c\u5728 add \u51fd\u6570\u4e2d\uff0c, \u53d8\u91cf\u540d x \u548c y \u6307\u7684\u662f add \u51fd\u6570\u7684\u5f62\u53c2 x \u548c y \uff0c\u5b83\u4eec\u4e5f\u53ea\u80fd\u591f\u5728 add \u4e2d\u88ab\u770b\u5230\u548c\u4f7f\u7528\u3002 \u957f\u8bdd\u77ed\u8bf4\uff0c\u5176\u5b9e add \u548c main \u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u4e0d\u77e5\u9053\u5bf9\u65b9\u4f7f\u7528\u4e86\u4e0e\u81ea\u5df1\u76f8\u540c\u7684\u53d8\u91cf\u540d\u3002\u56e0\u4e3a\uff0c\u8fd9\u4e9b\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u5e76\u6ca1\u6709\u91cd\u53e0\u7684\u90e8\u5206\uff0c\u56e0\u6b64\u5bf9\u4e8e\u7f16\u8bd1\u5668\u6765\u8bf4\uff0c x \u548c y \u5728\u4efb\u4f55\u65f6\u5019\u88ab\u5f15\u7528\uff0c\u5176\u6307\u4ee3\u7684\u5bf9\u8c61\u90fd\u662f\u6e05\u695a\u7684\u3002 \u5173\u952e\u4fe1\u606f \u51fd\u6570\u5f62\u53c2\u6216\u8005\u662f\u5c40\u90e8\u53d8\u91cf\u5728\u58f0\u660e\u7684\u65f6\u5019\u6240\u4f7f\u7528\u7684\u540d\u5b57\uff0c\u53ea\u5728\u51fd\u6570\u4f53\u5185\u90e8\u53ef\u89c1\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u51fd\u6570\u5728\u5b9a\u4e49\u5c40\u90e8\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u4e0d\u8003\u8651\u5176\u4ed6\u51fd\u6570\u662f\u5426\u4f7f\u7528\u4e86\u67d0\u4e9b\u53d8\u91cf\u540d\u3002\u8fd9\u4e00\u7279\u6027\uff0c\u53ef\u4ee5\u5e2e\u52a9\u51fd\u6570\u4fdd\u6301\u72ec\u7acb\u6027\u3002 \u5728\u540e\u7eed\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u4f1a\u63a2\u8ba8\u5173\u4e8e\u5c40\u90e8\u4f5c\u7528\u57df\u7684\u66f4\u591a\u5185\u5bb9\uff0c\u540c\u65f6\u4e5f\u4f1a\u8c08\u5230\u5176\u4ed6\u7c7b\u578b\u7684\u4f5c\u7528\u57df\u3002 \u5728\u4f55\u5904\u5b9a\u4e49\u5c40\u90e8\u53d8\u91cf \u51fd\u6570\u4f53\u5185\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u5176\u5b9a\u4e49\u7684\u4f4d\u7f6e\u5e94\u8be5\u5c3d\u53ef\u80fd\u63a5\u8fd1\u5176\u7b2c\u4e00\u6b21\u88ab\u4f7f\u7528\u7684\u4f4d\u7f6e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; // x \u5728\u6b64\u5904\u5b9a\u4e49 std :: cin >> x ; // \u5728\u6b64\u5904\u4f7f\u7528 std :: cout << \"Enter another integer: \" ; int y {}; // y \u5728\u6b64\u5904\u5b9a\u4e49 std :: cin >> y ; // \u5728\u6b64\u5904\u4f7f\u7528 int sum { x + y }; // sum \u5728\u6b64\u5904\u5b9a\u4e49 std :: cout << \"The sum is: \" << sum << '\\n' ; // \u5728\u6b64\u5904\u4f7f\u7528 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6bcf\u4e2a\u53d8\u91cf\u90fd\u662f\u5728\u5b83\u7b2c\u4e00\u6b21\u88ab\u4f7f\u7528\u65f6\u7684\u524d\u4e00\u884c\u5b9a\u4e49\u7684\u3002\u8fd9\u4e2a\u89c4\u5b9a\u5012\u662f\u65e0\u9700\u4e25\u683c\u9075\u5b88\u2014\u2014\u5982\u679c\u4f60\u5e0c\u671b\u5c06\u7b2c\u4e94\u884c\u548c\u7b2c\u516d\u884c\u7684\u4f4d\u7f6e\u6362\u4e00\u4e0b\u4e5f\u662f\u53ef\u4ee5\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c3d\u91cf\u5728\u5c40\u90e8\u53d8\u91cf\u7b2c\u4e00\u6b21\u88ab\u4f7f\u7528\u524d\u6700\u8fd1\u7684\u5730\u65b9\u5b9a\u4e49\u8be5\u5c40\u90e8\u53d8\u91cf\u3002","title":"2.5 - \u5c40\u90e8\u4f5c\u7528\u57df"},{"location":"2-5-Introduction-to-local-scope/#_1","text":"\u51fd\u6570\u7684 \u5f62\u53c2 \u548c\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u4e00\u6837\uff0c\u90fd\u662f \u5c40\u90e8\u53d8\u91cf(local variable) (\u5c40\u90e8\u53d8\u91cf\u662f\u548c \u5168\u5c40\u53d8\u91cf(global variable) \u76f8\u5bf9\u7684\u6982\u5ff5\uff0c\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5168\u5c40\u53d8\u91cf\uff09\u3002 1 2 3 4 5 6 int add ( int x , int y ) // function parameters x and y are local variables { int z { x + y }; // z is a local variable too return z ; } \u672c\u8282\u8bfe\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5c40\u90e8\u53d8\u91cf\u6240\u5177\u6709\u7684\u4e00\u4e9b\u5c5e\u6027\u3002","title":"\u5c40\u90e8\u53d8\u91cf"},{"location":"2-5-Introduction-to-local-scope/#_2","text":"\u5728 1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf \u4e2d\u6211\u4eec\u8ba8\u8bba\u4e86\u4ee5\u5f62\u5982 int x; \u7684\u65b9\u5f0f\u5b9a\u4e49\u53d8\u91cf\u662f\u5982\u4f55\u5728\u8bed\u53e5\u6267\u884c\u65f6 \u5b9e\u4f8b\u5316 \u53d8\u91cf\u7684\u3002\u51fd\u6570\u7684 \u5f62\u53c2 \u662f\u5728\u8fdb\u5165\u51fd\u6570\u65f6\u88ab\u521b\u5efa\u548c\u521d\u59cb\u5316\u7684\uff0c\u51fd\u6570\u4f53\u5185\u7684\u51fd\u6570\u5219\u662f\u5728\u5b9a\u4e49\u65f6\u521b\u5efa\u5e76\u521d\u59cb\u5316\u7684\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 int add ( int x , int y ) // x \u548c y \u5728\u6b64\u5904\u521b\u5efa\u5e76\u521d\u59cb\u5316 { int z { x + y }; // z \u5728\u6b64\u5904\u88ab\u521b\u5efa\u548c\u521d\u59cb\u5316 return z ; } \u6b64\u65f6\u6211\u4eec\u4e0d\u7ecf\u8981\u95ee\uff0c\u201d\u90a3\u4e48\u5b9e\u4f8b\u5316\u7684\u53d8\u91cf\u662f\u5728\u5408\u9002\u88ab\u9500\u6bc1\u7684\u5462\uff1f\u201c\u3002\u5c40\u90e8\u53d8\u91cf\u662f\u5728 \u5305\u542b\u5176\u5b9a\u4e49\u7684\u4e00\u7ec4\u82b1\u62ec\u53f7\u7684\u7ed3\u5c3e \u65f6\u9500\u6bc1\u7684\uff0c\u5176\u9500\u6bc1\u7684\u987a\u5e8f\u548c\u5b9a\u4e49\u987a\u5e8f\u6b63\u597d\u76f8\u53cd\uff08\u5bf9\u4e8e \u5f62\u53c2 \u6765\u8bf4\uff0c\u5219\u662f\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u9500\u6bc1\uff09\u3002 1 2 3 4 5 6 int add ( int x , int y ) { int z { x + y }; return z ; } // z, y, and x destroyed here \u6b63\u5982\u4eba\u7684\u751f\u547d\u5468\u671f\u88ab\u5b9a\u4e49\u4e3a\u5176\u51fa\u751f\u5230\u6b7b\u4ea1\u7684\u8fd9\u6bb5\u65f6\u95f4\u4e00\u6837\uff0c\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f(lifetime)\u6307\u7684\u662f\u8be5\u53d8\u91cf\u88ab\u521b\u5efa\u5230\u88ab\u9500\u6bc1\u7684\u8fd9\u6bb5\u65f6\u95f4\u3002\u6ce8\u610f\uff0c\u53d8\u91cf\u7684\u521b\u5efa\u548c\u9500\u6bc1\u90fd\u662f\u5728\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u5019\u53d1\u751f\u7684\uff08\u79f0\u4e3a \u8fd0\u884c\u65f6 \uff09\uff0c\u800c\u4e0d\u662f\u5728 \u7f16\u8bd1\u65f6 \u53d1\u751f\u7684\u3002\u56e0\u6b64\uff0c\u751f\u547d\u5468\u671f\u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\u5c5e\u6027\u3002 \u6269\u5c55\u9605\u8bfb \u4e0a\u9762\u5173\u4e8e\u521b\u5efa\u3001\u521d\u59cb\u5316\u548c\u9500\u6bc1\u7684\u89c4\u5219\u662f\u5fc5\u987b\u8981\u6ee1\u8db3\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u521d\u59cb\u5316\u4e0d\u665a\u4e8e\u5176\u5b9a\u4e49\uff0c\u800c\u9500\u6bc1\u5219\u4e0d\u80fd\u65e9\u4e8e\u82b1\u62ec\u53f7\u7684\u7ed3\u5c3e\uff08\u5bf9\u4e8e\u51fd\u6570\u5f62\u53c2\u6765\u8bf4\u5219\u662f\u51fd\u6570\u7ed3\u5c3e\uff09\u3002 \u5b9e\u9645\u4e0a\uff0cC++ \u7684\u89c4\u8303\u7ed9\u4e0e\u7f16\u8bd1\u5668\u4e86\u4e00\u4e9b\u7075\u6d3b\u6027\uff0c\u4f7f\u7f16\u8bd1\u5668\u53ef\u4ee5\u51b3\u5b9a\u4f55\u65f6\u521b\u5efa\u6216\u9500\u6bc1\u5c40\u90e8\u53d8\u91cf\u3002\u51fa\u4e8e\u4f18\u5316\u7684\u76ee\u7684\uff0c\u5bf9\u8c61\u53ef\u4ee5\u88ab\u65e9\u4e00\u70b9\u521b\u5efa\u6216\u665a\u4e00\u70b9\u9500\u6bc1\u3002\u591a\u6570\u65f6\u5019\uff0c\u5c40\u90e8\u53d8\u91cf\u4f1a\u5728\u51fd\u6570\u8fdb\u5165\u65f6\u521b\u5efa\uff0c\u5e76\u4ee5\u76f8\u53cd\u7684\u987a\u5e8f\u5728\u51fd\u6570\u9000\u51fa\u65f6\u9500\u6bc1\u3002\u6211\u4eec\u4f1a\u5728\u5c06\u6765\u7684\u6709\u5173\u51fd\u6570\u8c03\u7528\u6808\u8bfe\u7a0b\u4e2d\u5bf9\u5176\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002 \u4e0b\u9762\u8fd9\u6bb5\u7a0b\u5e8f\u5c55\u793a\u4e86\u6709\u5173\u53d8\u91cf x \u751f\u547d\u5468\u671f\u66f4\u590d\u6742\u7684\u60c5\u51b5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include void doSomething () { std :: cout << \"Hello! \\n \" ; } int main () { int x { 0 }; // x \u7684\u751f\u547d\u5468\u671f\u8d77\u70b9 doSomething (); // \u5728\u51fd\u6570\u8c03\u7528\u671f\u95f4\uff0cx \u4ecd\u7136\u5b58\u5728 return 0 ; } // x \u7684\u751f\u547d\u5468\u671f\u7ec8\u70b9 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cx \u7684\u751f\u547d\u5468\u671f\u4ece\u5b83\u7684\u5b9a\u4e49\u5f00\u59cb\u4e00\u76f4\u5230 main \u51fd\u6570\u7684\u672b\u5c3e\u7ed3\u675f\u3002\u8fd9\u5176\u4e2d\u5305\u542b\u4e86\u51fd\u6570 doSomething \u6267\u884c\u7684\u65f6\u95f4\u3002","title":"\u5c40\u90e8\u53d8\u91cf\u751f\u547d\u5468\u671f"},{"location":"2-5-Introduction-to-local-scope/#_3","text":"\u4e00\u4e2a \u6807\u8bc6\u7b26 \u7684 \u4f5c\u7528\u57df(scope) \u8868\u660e\u4e86\u8be5\u8868\u793a\u7b26\u5728\u6e90\u7801\u4e2d\u80fd\u591f\u88ab\u8bbf\u95ee\u7684\u4f4d\u7f6e\u3002\u5982\u679c\u4e00\u4e2a\u6807\u8bc6\u7b26\u53ef\u4ee5\u88ab\u8bbf\u95ee\uff0c\u5219\u79f0\u5176\u5728\u4f5c\u7528\u57df\u4e2d\u3002\u5f53\u4e00\u4e2a\u6807\u8bc6\u7b26\u4e0d\u80fd\u88ab\u8bbf\u95ee\u65f6\uff0c\u5219\u79f0\u5176\u8d85\u51fa\u4e86\u4f5c\u7528\u57df\uff08Out of scope\uff09\u3002\u4f5c\u7528\u57df\u662f\u4e00\u4e2a \u7f16\u8bd1\u65f6 \u5c5e\u6027\uff0c\u5982\u679c\u5728\u6807\u8bc6\u7b26\u7684\u4f5c\u7528\u57df\u4e4b\u5916\u8bbf\u95ee\u5b83\uff0c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 \u5c40\u90e8\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4ece\u8be5\u53d8\u91cf\u7684\u5b9a\u4e49\u5f00\u59cb\uff0c\u5728\u5305\u542b\u4e86\u8be5\u53d8\u91cf\u5b9a\u4e49\u7684\u4e00\u7ec4\u82b1\u62ec\u53f7\u7684\u7ed3\u5c3e\u7ec8\u6b62\uff08\u5bf9\u4e8e\u51fd\u6570 \u5f62\u53c2 \u5219\u5728\u51fd\u6570\u7ed3\u5c3e\u7ec8\u6b62\uff09\u3002\u8fd9\u786e\u4fdd\u4e86\u53d8\u91cf\u5728\u5b9a\u4e49\u524d\u4e0d\u80fd\u88ab\u4f7f\u7528\uff08\u5373\u4f7f\u7f16\u8bd1\u5668\u9009\u62e9\u5728\u6b64\u4e4b\u524d\u521b\u5efa\u53d8\u91cf\uff09\u3002\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d\u5b9a\u4e49\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u5176\u4f5c\u7528\u57df\u4e0d\u80fd\u5ef6\u4f38\u5230\u5176\u4ed6\u51fd\u6570\u4e2d\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u53d8\u91cf x \u7684\u4f5c\u7528\u57df\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include // x \u4e0d\u5728\u8be5\u51fd\u6570\u7684\u4f5c\u7528\u57df void doSomething () { std :: cout << \"Hello! \\n \" ; } int main () { // x \u6b64\u65f6\u8fd8\u4e0d\u80fd\u88ab\u8bbf\u95ee\uff0c\u56e0\u4e3a\u8fd8\u6ca1\u6709\u8fdb\u5165\u5176\u4f5c\u7528\u57df int x { 0 }; // x \u4ece\u6b64\u65f6\u8fdb\u5165\u4f5c\u7528\u57df\uff0c\u968f\u540e\u53ef\u4ee5\u5728\u8be5\u51fd\u6570\u4e2d\u4f7f\u7528\u5b83 doSomething (); return 0 ; } // x \u4f5c\u7528\u57df\u7ec8\u6b62\u4e0e\u6b64\uff0c\u6b64\u540e\u4e0d\u518d\u80fd\u591f\u88ab\u4f7f\u7528 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u53d8\u91cf x \u4ece\u5176\u5b9a\u4e49\u4f4d\u7f6e\u5f00\u59cb\u8fdb\u5165\u4f5c\u7528\u57df\uff0c\u5728 main \u51fd\u6570\u7ed3\u675f\u65f6\u79bb\u5f00\u5176\u4f5c\u7528\u57df\u3002\u6ce8\u610f\uff0c\u53d8\u91cf x \u7684\u4f5c\u7528\u57df\u4e0d\u4f1a\u5ef6\u4f38\u5230\u51fd\u6570 doSomething() \u4e2d\u3002\u8fd9\u4e5f\u4e00\u4f4d\u7f6e main \u51fd\u6570\u8c03\u7528 doSomething \u548c\u8be5\u8be5\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u6ca1\u6709\u4efb\u4f55\u5173\u7cfb\u3002","title":"\u5c40\u90e8\u4f5c\u7528\u57df"},{"location":"2-5-Introduction-to-local-scope/#_4","text":"\u672f\u8bed \u8d85\u51fa\u4f5c\u7528\u57df \u548c \u79bb\u5f00\u4f5c\u7528\u57df \u5bf9\u4e8e\u65b0\u624b\u6765\u8bf4\u53ef\u80fd\u4ee4\u4eba\u56f0\u60d1\u3002 \u6807\u8bc6\u7b26\u8d85\u51fa\u4f5c\u7528\u57df\u8868\u793a\u5176\u4e0d\u80fd\u5728\u4ee3\u7801\u4e2d\u88ab\u8bbf\u95ee\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c \u6807\u8bc6\u7b26(identifier) x \u8fdb\u5165\u4f5c\u7528\u57df\u7684\u65f6\u95f4\u4ece\u5176\u5b9a\u4e49\u5f00\u59cb\u5230 main \u51fd\u6570\u7ed3\u5c3e\u4e3a\u6b62\u3002\u8fd9\u6bb5\u4ee3\u7801\u4ee5\u5916\u7684\u4ee3\u7801\u4e2d\u8bbf\u95ee\u8be5\u6807\u8bc6\u7b26\uff0c\u90fd\u5c5e\u4e8e\u8d85\u51fa\u4f5c\u7528\u57df\u3002 \u672f\u8bed\u79bb\u5f00\u4f5c\u7528\u57df\u63cf\u8ff0\u7684\u5bf9\u8c61\u5219\u4e00\u822c\u662f\u67d0\u4e2a\u5bf9\u8c61\u800c\u4e0d\u662f\u6807\u8bc6\u7b26\u3002\u6211\u4eec\u8bf4\u4e00\u4e2a\u53d8\u91cf\u4f1a\u5728\u5b9e\u4f8b\u5316\u5b83\u4f5c\u7528\u57df\u7684\u7ed3\u5c3e\u5904\uff08\u82b1\u62ec\u53f7\u7ed3\u675f\u5904\uff09\u79bb\u5f00\u4f5c\u7528\u57df\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c \u5bf9\u8c61 x \u4f1a\u5728 main \u51fd\u6570\u7684\u7ed3\u5c3e\u5904\u79bb\u5f00\u4f5c\u7528\u57df\u3002 \u5177\u5907\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u4f1a\u5728\u5176\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\u7ec8\u6b62\uff0c\u5c40\u90e8\u53d8\u91cf\u4f1a\u5728\u6b64\u65f6\u88ab\u9500\u6bc1\u3002 \u6ce8\u610f\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u7c7b\u578b\u7684\u53d8\u91cf\u90fd\u4f1a\u5728\u5176\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\u88ab\u9500\u6bc1\u3002\u6211\u4eec\u4f1a\u5728\u5c06\u6765\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u6b64\u7c7b\u4f8b\u5b50\u3002","title":"\u201c\u8d85\u51fa\u4f5c\u7528\u57df\u201d \u548c \u201c\u79bb\u5f00\u4f5c\u7528\u57df\u201d"},{"location":"2-5-Introduction-to-local-scope/#_5","text":"\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u7a0d\u5fae\u66f4\u590d\u6742\u4e00\u5b9a\u3002\u8bf7\u8bb0\u4f4f\uff0c\u751f\u547d\u5468\u671f\u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\u5c5e\u6027\uff0c\u800c\u4f5c\u7528\u57df\u662f\u7f16\u8bd1\u65f6\u5c5e\u6027\u3002\u867d\u7136\uff0c\u6211\u4eec\u5728\u540c\u4e00\u6bb5\u4ee3\u7801\u4e2d\u8ba8\u8bba\u4e24\u8005\uff0c\u5b83\u4eec\u7684\u51fa\u53d1\u70b9\u662f\u4e0d\u540c\u7684\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int add ( int x , int y ) // x \u548c y \u5728\u6b64\u5904\u88ab\u521b\u5efa\u5e76\u8fdb\u5165\u4f5c\u7528\u57df { // x \u548c y \u4ec5\u5728\u8be5\u51fd\u6570\u4e2d\u53ef\u89c1/\u53ef\u7528 return x + y ; } // y \u548c x \u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1 int main () { int a { 5 }; // a \u5728\u6b64\u5904\u88ab\u521b\u5efa\u3001\u521d\u59cb\u5316\u5e76\u8fdb\u5165\u4f5c\u7528\u57df int b { 6 }; // b \u5728\u6b64\u5904\u88ab\u521b\u5efa\u3001\u521d\u59cb\u5316\u5e76\u8fdb\u5165\u4f5c\u7528\u57df // a \u548c b \u53ea\u5728\u8be5\u51fd\u6570\u4e2d\u53ef\u7528 std :: cout << add ( a , b ) << '\\n' ; // calls function add() with x=5 and y=6 return 0 ; } // b \u548c a \u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1 \u51fd\u6570\u5f62\u53c2 x \u548c y \u5728 add \u51fd\u6570\u88ab\u8c03\u7528\u65f6\u521b\u5efa\uff0c\u4f5c\u4e3a\u53d8\u91cf\uff0c\u5b83\u4eec\u53ea\u5728 add \u51fd\u6570\u4e2d\u53ef\u89c1\u53ef\u7528\uff0c\u5e76\u5728 add \u51fd\u6570\u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u53d8\u91cf a \u548c b \u5728\u51fd\u6570 main \u4e2d\u88ab\u521b\u5efa\uff0c\u53ea\u5728 main \u51fd\u6570\u4e2d\u53ef\u7528\uff0c\u5e76\u4e14\u5728 main \u51fd\u6570\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u3002 \u4e3a\u4e86\u5f3a\u5316\u7406\u89e3\uff0c\u8ba9\u6211\u4eec\u9010\u6b65\u89e3\u6790\u8be5\u51fd\u6570\uff0c\u4e0b\u5217\u64cd\u4f5c\u6309\u987a\u5e8f\u53d1\u751f\uff1a - \u7a0b\u5e8f\u4ece main \u51fd\u6570\u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\uff1b - main \u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf a \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 5\uff1b - main \u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf b \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 6\uff1b - add \u51fd\u6570\u88ab\u8c03\u7528\uff0c\u51fd\u6570\u7684\u5b9e\u53c2\u4e3a 5 \u548c 6\uff1b - add \u7684\u53d8\u91cf x \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 5\uff1b - add \u7684\u53d8\u91cf y \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 6\uff1b - \u8868\u8fbe\u5f0f x + y \u6c42\u503c\u5f97\u5230\u7ed3\u679c 11\uff1b - add \u5c06\u503c 11 \u62f7\u8d1d\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570 main \uff1b - add \u7684 y \u548c x \u88ab\u9500\u6bc1\uff1b - main \u5728\u63a7\u5236\u53f0\u6253\u5370 11\uff1b - main \u8fd4\u56de 0 \u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff1b - main \u7684\u5c40\u90e8\u53d8\u91cf b \u548c a \u88ab\u9500\u6bc1\uff1b \u4ee5\u4e0a\u3002 \u6ce8\u610f\uff0c\u5982\u679c\u51fd\u6570 add \u88ab\u5148\u540e\u4e24\u6b21\u8c03\u7528\uff0c\u90a3\u4e48\u5bf9\u5e94\u7684\u5f62\u53c2 x \u548c y \u4e5f\u88ab\u5148\u540e\u521b\u5efa\u5e76\u9500\u6bc1\u4e24\u6b21\u2014\u2014\u6bcf\u6b21\u8c03\u7528\u5c31\u4f1a\u53d1\u751f\u4e00\u6b21\u521b\u5efa\u548c\u9500\u6bc1\u3002\u5982\u679c\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\u6709\u5927\u91cf\u7684\u51fd\u6570\u8c03\u7528\uff0c\u90a3\u4e48\u53d8\u91cf\u4e5f\u4f1a\u968f\u4e4b\u88ab\u9891\u7e41\u7684\u521b\u5efa\u548c\u9500\u6bc1\u3002","title":"\u5176\u4ed6\u4f8b\u5b50"},{"location":"2-5-Introduction-to-local-scope/#_6","text":"\u5728\u4e0a\u8ff0\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f88\u660e\u663e\u53ef\u4ee5\u770b\u51fa\uff0c\u53d8\u91cf a \u548c b \u662f\u4e0d\u540c\u4e8e\u53d8\u91cf x \u548c y \u7684\u53d8\u91cf\u3002 \u73b0\u5728\uff0c\u8bf7\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int add ( int x , int y ) // x \u548c y \u5728\u6b64\u5904\u88ab\u521b\u5efa\u5e76\u8fdb\u5165\u4f5c\u7528\u57df { // x \u548c y \u4ec5\u5728\u8be5\u51fd\u6570\u4e2d\u53ef\u89c1/\u53ef\u7528 return x + y ; } // y \u548c x \u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1 int main () { int x { 5 }; // x \u5728\u6b64\u5904\u88ab\u521b\u5efa\u3001\u521d\u59cb\u5316\u5e76\u8fdb\u5165\u4f5c\u7528\u57df int y { 6 }; // y \u5728\u6b64\u5904\u88ab\u521b\u5efa\u3001\u521d\u59cb\u5316\u5e76\u8fdb\u5165\u4f5c\u7528\u57df // x \u548c y \u53ea\u5728\u8be5\u51fd\u6570\u4e2d\u53ef\u7528 std :: cout << add ( x , y ) << '\\n' ; // calls function add() with x=5 and y=6 return 0 ; } // x \u548c y \u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1 \u4e0e\u524d\u4e00\u4e2a\u4f8b\u5b50\u76f8\u6bd4\uff0c\u672c\u4f8b\u53ea\u662f\u5c06 main \u51fd\u6570\u4e2d\u7684\u53d8\u91cf a \u548c b \u4fee\u6539\u6210\u4e86 x \u548c y \u3002\u8fd9\u4e2a\u7a0b\u5e8f\u7684\u7f16\u8bd1\u548c\u8fd0\u884c\u548c\u4e4b\u524d\u7684\u7a0b\u5e8f\u6ca1\u6709\u4efb\u4f55\u533a\u522b\uff0c\u5373\u4f7f main \u51fd\u6570\u548c add \u51fd\u6570\u90fd\u4f7f\u7528\u4e86\u53d8\u91cf\u540d x \u548c y \u3002\u4e3a\u4ec0\u4e48\u8fd9\u6837\u4e5f\u53ef\u4ee5\u5de5\u4f5c\u5462\uff1f \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u8ba4\u8bc6\u5230\u7684\u662f\uff0c\u5373\u4f7f main \u51fd\u6570\u548c add \u51fd\u6570\u90fd\u4f7f\u7528\u4e86\u53d8\u91cf\u540d x \u548c y \uff0c\u4f46\u662f\u8fd9\u4e9b\u53d8\u91cf\u5b9e\u9645\u4e0a\u662f\u5b8c\u5168\u4e0d\u540c\u7684\u3002 main \u51fd\u6570\u4e2d\u7684 x \u548c y \uff0c\u548c add \u51fd\u6570\u4e2d\u7684 x \u548c y \u662f\u6ca1\u6709\u4efb\u4f55\u5173\u7cfb\u7684\u2014\u2014\u5b83\u4eec\u53ea\u662f\u78b0\u5de7\u540c\u540d\u7f62\u4e86\u3002 \u5176\u6b21\uff0c\u5728 main \u51fd\u6570\u4e2d\uff0c \u53d8\u91cf\u540d x \u548c y \u6307\u7684\u662f main \u51fd\u6570\u5c40\u90e8\u4f5c\u7528\u4e2d\u7684\u53d8\u91cf x \u548c y \u3002\u8fd9\u4e9b\u53d8\u91cf\u53ea\u80fd\u591f\u5728 main \u51fd\u6570\u4e2d\u88ab\u770b\u5230\uff08\u88ab\u4f7f\u7528\uff09\u3002\u540c\u6837\u7684\uff0c\u5728 add \u51fd\u6570\u4e2d\uff0c, \u53d8\u91cf\u540d x \u548c y \u6307\u7684\u662f add \u51fd\u6570\u7684\u5f62\u53c2 x \u548c y \uff0c\u5b83\u4eec\u4e5f\u53ea\u80fd\u591f\u5728 add \u4e2d\u88ab\u770b\u5230\u548c\u4f7f\u7528\u3002 \u957f\u8bdd\u77ed\u8bf4\uff0c\u5176\u5b9e add \u548c main \u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u4e0d\u77e5\u9053\u5bf9\u65b9\u4f7f\u7528\u4e86\u4e0e\u81ea\u5df1\u76f8\u540c\u7684\u53d8\u91cf\u540d\u3002\u56e0\u4e3a\uff0c\u8fd9\u4e9b\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u5e76\u6ca1\u6709\u91cd\u53e0\u7684\u90e8\u5206\uff0c\u56e0\u6b64\u5bf9\u4e8e\u7f16\u8bd1\u5668\u6765\u8bf4\uff0c x \u548c y \u5728\u4efb\u4f55\u65f6\u5019\u88ab\u5f15\u7528\uff0c\u5176\u6307\u4ee3\u7684\u5bf9\u8c61\u90fd\u662f\u6e05\u695a\u7684\u3002 \u5173\u952e\u4fe1\u606f \u51fd\u6570\u5f62\u53c2\u6216\u8005\u662f\u5c40\u90e8\u53d8\u91cf\u5728\u58f0\u660e\u7684\u65f6\u5019\u6240\u4f7f\u7528\u7684\u540d\u5b57\uff0c\u53ea\u5728\u51fd\u6570\u4f53\u5185\u90e8\u53ef\u89c1\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u51fd\u6570\u5728\u5b9a\u4e49\u5c40\u90e8\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u4e0d\u8003\u8651\u5176\u4ed6\u51fd\u6570\u662f\u5426\u4f7f\u7528\u4e86\u67d0\u4e9b\u53d8\u91cf\u540d\u3002\u8fd9\u4e00\u7279\u6027\uff0c\u53ef\u4ee5\u5e2e\u52a9\u51fd\u6570\u4fdd\u6301\u72ec\u7acb\u6027\u3002 \u5728\u540e\u7eed\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u4f1a\u63a2\u8ba8\u5173\u4e8e\u5c40\u90e8\u4f5c\u7528\u57df\u7684\u66f4\u591a\u5185\u5bb9\uff0c\u540c\u65f6\u4e5f\u4f1a\u8c08\u5230\u5176\u4ed6\u7c7b\u578b\u7684\u4f5c\u7528\u57df\u3002","title":"\u51fd\u6570\u7684\u9694\u79bb"},{"location":"2-5-Introduction-to-local-scope/#_7","text":"\u51fd\u6570\u4f53\u5185\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u5176\u5b9a\u4e49\u7684\u4f4d\u7f6e\u5e94\u8be5\u5c3d\u53ef\u80fd\u63a5\u8fd1\u5176\u7b2c\u4e00\u6b21\u88ab\u4f7f\u7528\u7684\u4f4d\u7f6e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; // x \u5728\u6b64\u5904\u5b9a\u4e49 std :: cin >> x ; // \u5728\u6b64\u5904\u4f7f\u7528 std :: cout << \"Enter another integer: \" ; int y {}; // y \u5728\u6b64\u5904\u5b9a\u4e49 std :: cin >> y ; // \u5728\u6b64\u5904\u4f7f\u7528 int sum { x + y }; // sum \u5728\u6b64\u5904\u5b9a\u4e49 std :: cout << \"The sum is: \" << sum << '\\n' ; // \u5728\u6b64\u5904\u4f7f\u7528 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6bcf\u4e2a\u53d8\u91cf\u90fd\u662f\u5728\u5b83\u7b2c\u4e00\u6b21\u88ab\u4f7f\u7528\u65f6\u7684\u524d\u4e00\u884c\u5b9a\u4e49\u7684\u3002\u8fd9\u4e2a\u89c4\u5b9a\u5012\u662f\u65e0\u9700\u4e25\u683c\u9075\u5b88\u2014\u2014\u5982\u679c\u4f60\u5e0c\u671b\u5c06\u7b2c\u4e94\u884c\u548c\u7b2c\u516d\u884c\u7684\u4f4d\u7f6e\u6362\u4e00\u4e0b\u4e5f\u662f\u53ef\u4ee5\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c3d\u91cf\u5728\u5c40\u90e8\u53d8\u91cf\u7b2c\u4e00\u6b21\u88ab\u4f7f\u7528\u524d\u6700\u8fd1\u7684\u5730\u65b9\u5b9a\u4e49\u8be5\u5c40\u90e8\u53d8\u91cf\u3002","title":"\u5728\u4f55\u5904\u5b9a\u4e49\u5c40\u90e8\u53d8\u91cf"},{"location":"2-6-Why-functions-are-useful-and-how-to-use-them-effectively/","tags":["function"],"text":"\u901a\u8fc7\u524d\u9762\u7684\u5b66\u4e60\uff0c\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u4e86\u4ec0\u4e48\u662f\u51fd\u6570\u4ee5\u53ca\u51fd\u6570\u7684\u57fa\u672c\u529f\u80fd\uff0c\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u770b\u770b\uff0c\u4e3a\u4ec0\u4e48\u8bf4\u51fd\u6570\u975e\u5e38\u6709\u7528\u3002 \u51fd\u6570\u4e3a\u4ec0\u4e48\u6709\u7528 \u65b0\u624b\u7a0b\u5e8f\u5458\u603b\u4f1a\u95ee\uff1a\u201c\u96be\u9053\u6211\u4eec\u5c31\u4e0d\u80fd\u628a\u6240\u6709\u7684\u4ee3\u7801\u90fd\u585e\u5230 main \u51fd\u6570\u91cc\u9762\u5417\uff1f\u201d \uff0c\u5176\u5b9e\u5bf9\u4e8e\u975e\u5e38\u7b80\u5355\u7684\u7a0b\u5e8f\uff0c\u4f60\u7684\u786e\u53ef\u4ee5\u5c06\u6240\u6709\u7684\u4ee3\u7801\u90fd\u585e\u5230 main \u51fd\u6570\u91cc\u9762\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u4ee3\u7801\u91cf\u6bd4\u8f83\u591a\u6216\u590d\u6742\u5ea6\u8f83\u9ad8\u7684\u7a0b\u5e8f\u6765\u8bf4\uff0c\u4f7f\u7528\u51fd\u6570\u80fd\u591f\u5e26\u6765\u5f88\u591a\u597d\u5904\uff1a \u4fbf\u4e8e\u7ec4\u7ec7\u4ee3\u7801 -- \u968f\u7740\u7a0b\u5e8f\u7684\u590d\u6742\u6027\u8d8a\u6765\u8d8a\u9ad8\uff0c\u5c06\u6240\u6709\u7684\u4ee3\u7801\u90fd\u662f\u585e\u5230 main \u51fd\u6570\u4e2d\u4f1a\u4f7f main \u51fd\u6570\u53d8\u5f97\u975e\u5e38\u590d\u6742\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u51fd\u6570\u770b\u505a\u662f\u4e00\u4e2a\u4e2a\u72ec\u7acb\u4e8e\u4e3b\u7a0b\u5e8f\u7684\u8ff7\u4f60\u7a0b\u5e8f\uff0c\u5728\u7f16\u5199\u5b83\u4eec\u7684\u65f6\u5019\u6211\u4eec\u65e0\u9700\u8003\u8651\u5176\u4ed6\u7a0b\u5e8f\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u4e00\u4e2a\u590d\u6742\u7684\u7a0b\u5e8f\u7b80\u5316\u4e3a\u591a\u4e2a\u66f4\u5c0f\u3001\u66f4\u6613\u7ef4\u62a4\u7684\u6bb5\u843d\uff0c\u4ece\u800c\u964d\u4f4e\u6574\u4e2a\u7a0b\u5e8f\u7684\u590d\u6742\u5ea6\uff1b \u63d0\u9ad8\u53ef\u91cd\u7528\u6027 -- \u5f53\u51fd\u6570\u88ab\u7f16\u5199\u540e\uff0c\u5b83\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u88ab\u591a\u6b21\u8c03\u7528\u3002\u8fd9\u4e48\u505a\u53ef\u4ee5\u907f\u514d\u91cd\u590d\u4ee3\u7801 (\u201cDon\u2019t Repeat Yourself\u201d\u539f\u5219)\u800c\u4e14\u80fd\u591f\u6700\u5927\u7a0b\u5ea6\u4e0a\u51cf\u5c11\u590d\u5236\u7c98\u8d34\u4ee3\u7801\u6240\u53ef\u80fd\u5e26\u6765\u7684\u9519\u8bef\u3002\u51fd\u6570\u8fd8\u53ef\u4ee5\u5728\u7a0b\u5e8f\u95f4\u5171\u4eab\uff0c\u8fd9\u4e48\u505a\u53ef\u4ee5\u907f\u514d\u91cd\u590d\u7f16\u5199\uff08\u6216\u6d4b\u8bd5\uff09\u4ee3\u7801\u3002\uff1b \u4fbf\u4e8e\u6d4b\u8bd5\u4ee3\u7801 -- \u51fd\u6570\u53ef\u4ee5\u51cf\u5c11\u5197\u4f59\u4ee3\u7801\uff0c\u56e0\u6b64\u9996\u5148\u5c31\u53ef\u4ee5\u51cf\u5c11\u9700\u8981\u6d4b\u8bd5\u7684\u4ee3\u7801\u6570\u91cf\u3002\u540c\u65f6\uff0c\u7531\u4e8e\u51fd\u6570\u662f\u81ea\u5305\u542b\u7684\uff0c\u56e0\u6b64\u5f53\u51fd\u6570\u901a\u8fc7\u6d4b\u8bd5\u5e76\u786e\u4fdd\u6b63\u786e\u5de5\u4f5c\u540e\uff0c\u9664\u975e\u5b83\u88ab\u4fee\u6539\u4e86\uff0c\u5426\u5219\u5c06\u65e0\u9700\u5bf9\u5176\u8fdb\u884c\u6d4b\u8bd5\u3002\u8fd9\u4e48\u505a\u663e\u7136\u53ef\u4ee5\u51cf\u5c11\u5f88\u591a\u9700\u8981\u6d4b\u8bd5\u7684\u4ee3\u7801\uff0c\u53d1\u73b0bug\u7684\u96be\u5ea6\u4e5f\u5c31\u968f\u4e4b\u964d\u4f4e\u4e86\uff08\u4e5f\u66f4\u52a0\u5bb9\u6613\u907f\u514dbug\uff09\uff1b \u63d0\u9ad8\u53ef\u6269\u5c55\u6027 -- \u5f53\u6211\u4eec\u9700\u8981\u5bf9\u7a0b\u5e8f\u8fdb\u884c\u6269\u5c55\uff0c\u4ee5\u4f7f\u5176\u80fd\u591f\u5904\u7406\u4e4b\u524d\u4e0d\u80fd\u5904\u7406\u7684\u60c5\u51b5\u65f6\uff0c\u51fd\u6570\u80fd\u591f\u4f7f\u6211\u4eec\u5728\u4e00\u5904\u8fdb\u884c\u6539\u52a8\u5e76\u5c06\u6539\u52a8\u7684\u6548\u679c\u5e94\u7528\u5230\u6240\u6709\u8c03\u7528\u4e86\u8be5\u51fd\u6570\u7684\u5730\u65b9\uff1b \u63d0\u9ad8\u62bd\u8c61\u7a0b\u5ea6 -- \u4e3a\u4e86\u4f7f\u7528\u67d0\u4e2a\u51fd\u6570\uff0c\u4f60\u5fc5\u987b\u77e5\u6653\u5176\u51fd\u6570\u540d\u3001\u8f93\u5165\u3001\u8f93\u51fa\u4ee5\u53ca\u5b83\u6240\u5904\u7684\u4f4d\u7f6e\u3002\u4f46\u662f\uff0c\u4f60\u4e0d\u5fc5\u77e5\u9053\u5b83\u5982\u4f55\u5de5\u4f5c\u6216\u8005\u4efb\u4f55\u4e0e\u4f7f\u7528\u5176\u529f\u80fd\u4e0d\u76f8\u5e72\u7684\u4fe1\u606f\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u5728\u4f7f\u7528\u4ed6\u4eba\u7684\u4ee3\u7801\uff08\u6216\u8005\u4efb\u4f55\u6807\u51c6\u5e93\u4e2d\u7684\u51fd\u6570\uff09\u65f6\uff0c\u4f60\u7684\u8ba4\u77e5\u8d1f\u8f7d\u5c31\u964d\u4f4e\u4e86\u3002 \u5c3d\u7ba1\u770b\u4e0a\u53bb\u4e0d\u50cf\uff0c\u4f46\u5b9e\u9645\u4e0a\u6bcf\u5f53\u4f60\u4f7f\u7528 operator<< \u6216 operator>> \u8fd0\u7b97\u7b26\u65f6\uff0c\u4f60\u5b9e\u9645\u4e0a\u5728\u4f7f\u7528\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u540c\u6837\u5177\u6709\u4e0a\u8ff0\u5168\u90e8\u7684\u4f18\u70b9\u3002 \u9ad8\u6548\u5730\u4f7f\u7528\u51fd\u6570 \u65b0\u624b\u7a0b\u5e8f\u5458\u9047\u5230\u7684\uff08\u9664\u4e86\u5b66\u4e60\u8bed\u8a00\u672c\u8eab\uff09\u6700\u5927\u6311\u6218\u4fbf\u662f\u7406\u89e3\u4f55\u65f6\uff0c\u4ee5\u53ca\u5982\u4f55\u9ad8\u6548\u5730\u4f7f\u7528\u51fd\u6570\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5173\u4e8e\u7f16\u5199\u51fd\u6570\u7684\u6307\u5bfc\u539f\u5219\uff1a \u5728\u7a0b\u5e8f\u4e2d\u91cd\u590d\u51fa\u73b0\u7684\u4e00\u7ec4\u8bed\u53e5\uff0c\u901a\u5e38\u53ef\u4ee5\u88ab\u5c01\u88c5\u6210\u4e00\u4e2a\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u540c\u6837\u7684\u65b9\u5f0f\u591a\u6b21\u4ece\u7528\u6237\u90a3\u91cc\u83b7\u53d6\u8f93\u5165\uff0c\u90a3\u4e48\u8fd9\u4e9b\u91cd\u590d\u4ee3\u7801\u5c31\u5f88\u9002\u5408\u88ab\u5c01\u88c5\u6210\u51fd\u6570\u3002\u5982\u679c\u6211\u4eec\u4ee5\u540c\u6837\u7684\u5f62\u5f0f\u5728\u591a\u5904\u8fdb\u884c\u8f93\u51fa\uff0c\u8fd9\u540c\u6837\u4e5f\u662f\u975e\u5e38\u9002\u5408\u5c01\u88c5\u51fd\u6570\u7684\u60c5\u666f\uff1b \u660e\u786e\u5b9a\u4e49\u4e86\u8f93\u5165\u548c\u8f93\u51fa\u7684\u4ee3\u7801\u975e\u5e38\u9002\u5408\u5c01\u88c5\u6210\u51fd\u6570\uff08\u5c24\u5176\u662f\u5f53\u8fd9\u4e9b\u4ee3\u7801\u975e\u5e38\u590d\u6742\u7684\u65f6\u5019\uff09\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u9700\u8981\u6392\u5e8f\u7684\u5217\u8868\uff0c\u90a3\u4e48\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\u7684\u4ee3\u7801\u5c31\u975e\u5e38\u9002\u5408\u4f5c\u4e3a\u51fd\u6570\uff0c\u65e2\u662f\u8be5\u6392\u5e8f\u53ef\u80fd\u53ea\u8fdb\u884c\u4f9d\u6b21\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8f93\u5165\u662f\u4e00\u4e2a\u672a\u6392\u5e8f\u7684\u5217\u8868\uff0c\u8f93\u51fa\u5219\u662f\u4e00\u4e2a\u6392\u5e8f\u540e\u7684\u5217\u8868\u3002\u8fd8\u6709\u5f88\u597d\u7684\u4f8b\u5b50\u662f\uff0c\u5982\u679c\u4f60\u7f16\u5199\u4e86\u4e00\u4e9b\u6a21\u62df\u63b7\u9ab0\u5b50\u7684\u7a0b\u5e8f\uff0c\u90a3\u4e48\u5b83\u4e5f\u5f88\u9002\u5408\u4f5c\u4e3a\u51fd\u6570\u3002\u5c3d\u7ba1\u5728\u5f53\u524d\u7684\u7a0b\u5e8f\u4e2d\u4f60\u53ef\u80fd\u53ea\u4f1a\u4f7f\u7528\u5b83\u4e00\u6b21\uff0c\u4f46\u662f\u5c06\u6765\u5982\u679c\u4f60\u60f3\u6269\u5c55\u7a0b\u5e8f\u6216\u7f16\u5199\u5176\u4ed6\u7a0b\u5e8f\uff0c\u8be5\u529f\u80fd\u5c31\u53ef\u4ee5\u76f4\u63a5\u88ab\u590d\u7528\uff1b \u4e00\u4e2a\u51fd\u6570\u5e94\u8be5\u53ea\u505a\u4e00\u4ef6\u4e8b\uff1b \u5982\u679c\u4e00\u4e2a\u51fd\u6570\u53d8\u5f97\u53c8\u957f\u3001\u53c8\u590d\u6742\u3001\u6216\u8005\u96be\u4ee5\u7406\u89e3\u4e86\uff0c\u90a3\u4e48\u5b83\u5c31\u5e94\u8be5\u88ab\u518d\u6b21\u62c6\u5206\uff0c\u53d8\u6210\u51e0\u4e2a\u5b50\u51fd\u6570\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a \u91cd\u6784(refactoring) \u3002\u6211\u4eec\u4f1a\u5728 3.10 - \u5728\u95ee\u9898\u53d1\u751f\u524d\u53d1\u73b0\u95ee\u9898 \u4e2d\u4ecb\u7ecd\u91cd\u6784\u7684\u6982\u5ff5\u3002 \u901a\u5e38\uff0c\u5728\u5b66\u4e60C++\u7684\u65f6\u5019\uff0c\u4f60\u7f16\u5199\u7684\u5f88\u591a\u7a0b\u5e8f\u90fd\u6d89\u53ca\u5230\u5982\u4e0b\u4e09\u79cd\u7c7b\u578b\u7684\u5b50\u4efb\u52a1\uff1a \u4ece\u7528\u6237\u83b7\u53d6\u8f93\u5165\uff1b \u57fa\u4e8e\u8f93\u5165\u8fdb\u884c\u8ba1\u7b97\uff1b \u6253\u5370\u8ba1\u7b97\u7684\u7ed3\u679c\u3002 \u5bf9\u4e8e\u77ed\u5c0f\u7684\u7a0b\u5e8f (\u4f8b\u5982\uff0c\u5c0f\u4e8e20\u884c\u4ee3\u7801\u7684\u7a0b\u5e8f\uff09\uff0c\u4e0a\u8ff0\u4e09\u4e2a\u5b50\u4efb\u52a1\u90fd\u53ef\u4ee5\u5728 main \u51fd\u6570\u4e2d\u5b8c\u6210\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u66f4\u957f\uff08\u6216\u8005\u51fa\u4e8e\u7ec3\u4e60\u7684\u76ee\u7684\uff09\uff0c\u4e0a\u8ff0\u4e09\u4e2a\u4efb\u52a1\u90fd\u5f88\u9002\u5408\u5404\u81ea\u4f5c\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u51fd\u6570\u3002 \u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u5c06\u8ba1\u7b97\u7ed3\u679c\u548c\u6253\u5370\u7ed3\u679c\u8fd9\u4e24\u6b65\u5408\u5e76\u4e3a\u4e00\u4e2a\u51fd\u6570\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u8fdd\u53cd\u4e86\u201c\u4e00\u4e2a\u51fd\u6570\u53ea\u505a\u4e00\u4ef6\u4e8b\u201d \u7684\u539f\u5219\u3002\u8d1f\u8d23\u8ba1\u7b97\u7ed3\u679c\u7684\u51fd\u6570\u5e94\u8be5\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\uff0c\u7136\u540e\u8ba9\u4e3b\u8c03\u51fd\u6570\u51b3\u5b9a\u5982\u4f55\u4f7f\u7528\u8be5\u8fd4\u56de\u503c\uff08\u4f8b\u5982\u8c03\u7528\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\u5c06\u8be5\u7ed3\u679c\u6253\u5370\u51fa\u6765\uff09\u3002","title":"2.6 - \u4e3a\u4ec0\u4e48\u51fd\u6570\u6709\u7528\uff0c\u5982\u4f55\u9ad8\u6548\u4f7f\u7528\u51fd\u6570"},{"location":"2-6-Why-functions-are-useful-and-how-to-use-them-effectively/#_1","text":"\u65b0\u624b\u7a0b\u5e8f\u5458\u603b\u4f1a\u95ee\uff1a\u201c\u96be\u9053\u6211\u4eec\u5c31\u4e0d\u80fd\u628a\u6240\u6709\u7684\u4ee3\u7801\u90fd\u585e\u5230 main \u51fd\u6570\u91cc\u9762\u5417\uff1f\u201d \uff0c\u5176\u5b9e\u5bf9\u4e8e\u975e\u5e38\u7b80\u5355\u7684\u7a0b\u5e8f\uff0c\u4f60\u7684\u786e\u53ef\u4ee5\u5c06\u6240\u6709\u7684\u4ee3\u7801\u90fd\u585e\u5230 main \u51fd\u6570\u91cc\u9762\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u4ee3\u7801\u91cf\u6bd4\u8f83\u591a\u6216\u590d\u6742\u5ea6\u8f83\u9ad8\u7684\u7a0b\u5e8f\u6765\u8bf4\uff0c\u4f7f\u7528\u51fd\u6570\u80fd\u591f\u5e26\u6765\u5f88\u591a\u597d\u5904\uff1a \u4fbf\u4e8e\u7ec4\u7ec7\u4ee3\u7801 -- \u968f\u7740\u7a0b\u5e8f\u7684\u590d\u6742\u6027\u8d8a\u6765\u8d8a\u9ad8\uff0c\u5c06\u6240\u6709\u7684\u4ee3\u7801\u90fd\u662f\u585e\u5230 main \u51fd\u6570\u4e2d\u4f1a\u4f7f main \u51fd\u6570\u53d8\u5f97\u975e\u5e38\u590d\u6742\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u51fd\u6570\u770b\u505a\u662f\u4e00\u4e2a\u4e2a\u72ec\u7acb\u4e8e\u4e3b\u7a0b\u5e8f\u7684\u8ff7\u4f60\u7a0b\u5e8f\uff0c\u5728\u7f16\u5199\u5b83\u4eec\u7684\u65f6\u5019\u6211\u4eec\u65e0\u9700\u8003\u8651\u5176\u4ed6\u7a0b\u5e8f\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u4e00\u4e2a\u590d\u6742\u7684\u7a0b\u5e8f\u7b80\u5316\u4e3a\u591a\u4e2a\u66f4\u5c0f\u3001\u66f4\u6613\u7ef4\u62a4\u7684\u6bb5\u843d\uff0c\u4ece\u800c\u964d\u4f4e\u6574\u4e2a\u7a0b\u5e8f\u7684\u590d\u6742\u5ea6\uff1b \u63d0\u9ad8\u53ef\u91cd\u7528\u6027 -- \u5f53\u51fd\u6570\u88ab\u7f16\u5199\u540e\uff0c\u5b83\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u88ab\u591a\u6b21\u8c03\u7528\u3002\u8fd9\u4e48\u505a\u53ef\u4ee5\u907f\u514d\u91cd\u590d\u4ee3\u7801 (\u201cDon\u2019t Repeat Yourself\u201d\u539f\u5219)\u800c\u4e14\u80fd\u591f\u6700\u5927\u7a0b\u5ea6\u4e0a\u51cf\u5c11\u590d\u5236\u7c98\u8d34\u4ee3\u7801\u6240\u53ef\u80fd\u5e26\u6765\u7684\u9519\u8bef\u3002\u51fd\u6570\u8fd8\u53ef\u4ee5\u5728\u7a0b\u5e8f\u95f4\u5171\u4eab\uff0c\u8fd9\u4e48\u505a\u53ef\u4ee5\u907f\u514d\u91cd\u590d\u7f16\u5199\uff08\u6216\u6d4b\u8bd5\uff09\u4ee3\u7801\u3002\uff1b \u4fbf\u4e8e\u6d4b\u8bd5\u4ee3\u7801 -- \u51fd\u6570\u53ef\u4ee5\u51cf\u5c11\u5197\u4f59\u4ee3\u7801\uff0c\u56e0\u6b64\u9996\u5148\u5c31\u53ef\u4ee5\u51cf\u5c11\u9700\u8981\u6d4b\u8bd5\u7684\u4ee3\u7801\u6570\u91cf\u3002\u540c\u65f6\uff0c\u7531\u4e8e\u51fd\u6570\u662f\u81ea\u5305\u542b\u7684\uff0c\u56e0\u6b64\u5f53\u51fd\u6570\u901a\u8fc7\u6d4b\u8bd5\u5e76\u786e\u4fdd\u6b63\u786e\u5de5\u4f5c\u540e\uff0c\u9664\u975e\u5b83\u88ab\u4fee\u6539\u4e86\uff0c\u5426\u5219\u5c06\u65e0\u9700\u5bf9\u5176\u8fdb\u884c\u6d4b\u8bd5\u3002\u8fd9\u4e48\u505a\u663e\u7136\u53ef\u4ee5\u51cf\u5c11\u5f88\u591a\u9700\u8981\u6d4b\u8bd5\u7684\u4ee3\u7801\uff0c\u53d1\u73b0bug\u7684\u96be\u5ea6\u4e5f\u5c31\u968f\u4e4b\u964d\u4f4e\u4e86\uff08\u4e5f\u66f4\u52a0\u5bb9\u6613\u907f\u514dbug\uff09\uff1b \u63d0\u9ad8\u53ef\u6269\u5c55\u6027 -- \u5f53\u6211\u4eec\u9700\u8981\u5bf9\u7a0b\u5e8f\u8fdb\u884c\u6269\u5c55\uff0c\u4ee5\u4f7f\u5176\u80fd\u591f\u5904\u7406\u4e4b\u524d\u4e0d\u80fd\u5904\u7406\u7684\u60c5\u51b5\u65f6\uff0c\u51fd\u6570\u80fd\u591f\u4f7f\u6211\u4eec\u5728\u4e00\u5904\u8fdb\u884c\u6539\u52a8\u5e76\u5c06\u6539\u52a8\u7684\u6548\u679c\u5e94\u7528\u5230\u6240\u6709\u8c03\u7528\u4e86\u8be5\u51fd\u6570\u7684\u5730\u65b9\uff1b \u63d0\u9ad8\u62bd\u8c61\u7a0b\u5ea6 -- \u4e3a\u4e86\u4f7f\u7528\u67d0\u4e2a\u51fd\u6570\uff0c\u4f60\u5fc5\u987b\u77e5\u6653\u5176\u51fd\u6570\u540d\u3001\u8f93\u5165\u3001\u8f93\u51fa\u4ee5\u53ca\u5b83\u6240\u5904\u7684\u4f4d\u7f6e\u3002\u4f46\u662f\uff0c\u4f60\u4e0d\u5fc5\u77e5\u9053\u5b83\u5982\u4f55\u5de5\u4f5c\u6216\u8005\u4efb\u4f55\u4e0e\u4f7f\u7528\u5176\u529f\u80fd\u4e0d\u76f8\u5e72\u7684\u4fe1\u606f\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u5728\u4f7f\u7528\u4ed6\u4eba\u7684\u4ee3\u7801\uff08\u6216\u8005\u4efb\u4f55\u6807\u51c6\u5e93\u4e2d\u7684\u51fd\u6570\uff09\u65f6\uff0c\u4f60\u7684\u8ba4\u77e5\u8d1f\u8f7d\u5c31\u964d\u4f4e\u4e86\u3002 \u5c3d\u7ba1\u770b\u4e0a\u53bb\u4e0d\u50cf\uff0c\u4f46\u5b9e\u9645\u4e0a\u6bcf\u5f53\u4f60\u4f7f\u7528 operator<< \u6216 operator>> \u8fd0\u7b97\u7b26\u65f6\uff0c\u4f60\u5b9e\u9645\u4e0a\u5728\u4f7f\u7528\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u540c\u6837\u5177\u6709\u4e0a\u8ff0\u5168\u90e8\u7684\u4f18\u70b9\u3002","title":"\u51fd\u6570\u4e3a\u4ec0\u4e48\u6709\u7528"},{"location":"2-6-Why-functions-are-useful-and-how-to-use-them-effectively/#_2","text":"\u65b0\u624b\u7a0b\u5e8f\u5458\u9047\u5230\u7684\uff08\u9664\u4e86\u5b66\u4e60\u8bed\u8a00\u672c\u8eab\uff09\u6700\u5927\u6311\u6218\u4fbf\u662f\u7406\u89e3\u4f55\u65f6\uff0c\u4ee5\u53ca\u5982\u4f55\u9ad8\u6548\u5730\u4f7f\u7528\u51fd\u6570\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5173\u4e8e\u7f16\u5199\u51fd\u6570\u7684\u6307\u5bfc\u539f\u5219\uff1a \u5728\u7a0b\u5e8f\u4e2d\u91cd\u590d\u51fa\u73b0\u7684\u4e00\u7ec4\u8bed\u53e5\uff0c\u901a\u5e38\u53ef\u4ee5\u88ab\u5c01\u88c5\u6210\u4e00\u4e2a\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u540c\u6837\u7684\u65b9\u5f0f\u591a\u6b21\u4ece\u7528\u6237\u90a3\u91cc\u83b7\u53d6\u8f93\u5165\uff0c\u90a3\u4e48\u8fd9\u4e9b\u91cd\u590d\u4ee3\u7801\u5c31\u5f88\u9002\u5408\u88ab\u5c01\u88c5\u6210\u51fd\u6570\u3002\u5982\u679c\u6211\u4eec\u4ee5\u540c\u6837\u7684\u5f62\u5f0f\u5728\u591a\u5904\u8fdb\u884c\u8f93\u51fa\uff0c\u8fd9\u540c\u6837\u4e5f\u662f\u975e\u5e38\u9002\u5408\u5c01\u88c5\u51fd\u6570\u7684\u60c5\u666f\uff1b \u660e\u786e\u5b9a\u4e49\u4e86\u8f93\u5165\u548c\u8f93\u51fa\u7684\u4ee3\u7801\u975e\u5e38\u9002\u5408\u5c01\u88c5\u6210\u51fd\u6570\uff08\u5c24\u5176\u662f\u5f53\u8fd9\u4e9b\u4ee3\u7801\u975e\u5e38\u590d\u6742\u7684\u65f6\u5019\uff09\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u9700\u8981\u6392\u5e8f\u7684\u5217\u8868\uff0c\u90a3\u4e48\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\u7684\u4ee3\u7801\u5c31\u975e\u5e38\u9002\u5408\u4f5c\u4e3a\u51fd\u6570\uff0c\u65e2\u662f\u8be5\u6392\u5e8f\u53ef\u80fd\u53ea\u8fdb\u884c\u4f9d\u6b21\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8f93\u5165\u662f\u4e00\u4e2a\u672a\u6392\u5e8f\u7684\u5217\u8868\uff0c\u8f93\u51fa\u5219\u662f\u4e00\u4e2a\u6392\u5e8f\u540e\u7684\u5217\u8868\u3002\u8fd8\u6709\u5f88\u597d\u7684\u4f8b\u5b50\u662f\uff0c\u5982\u679c\u4f60\u7f16\u5199\u4e86\u4e00\u4e9b\u6a21\u62df\u63b7\u9ab0\u5b50\u7684\u7a0b\u5e8f\uff0c\u90a3\u4e48\u5b83\u4e5f\u5f88\u9002\u5408\u4f5c\u4e3a\u51fd\u6570\u3002\u5c3d\u7ba1\u5728\u5f53\u524d\u7684\u7a0b\u5e8f\u4e2d\u4f60\u53ef\u80fd\u53ea\u4f1a\u4f7f\u7528\u5b83\u4e00\u6b21\uff0c\u4f46\u662f\u5c06\u6765\u5982\u679c\u4f60\u60f3\u6269\u5c55\u7a0b\u5e8f\u6216\u7f16\u5199\u5176\u4ed6\u7a0b\u5e8f\uff0c\u8be5\u529f\u80fd\u5c31\u53ef\u4ee5\u76f4\u63a5\u88ab\u590d\u7528\uff1b \u4e00\u4e2a\u51fd\u6570\u5e94\u8be5\u53ea\u505a\u4e00\u4ef6\u4e8b\uff1b \u5982\u679c\u4e00\u4e2a\u51fd\u6570\u53d8\u5f97\u53c8\u957f\u3001\u53c8\u590d\u6742\u3001\u6216\u8005\u96be\u4ee5\u7406\u89e3\u4e86\uff0c\u90a3\u4e48\u5b83\u5c31\u5e94\u8be5\u88ab\u518d\u6b21\u62c6\u5206\uff0c\u53d8\u6210\u51e0\u4e2a\u5b50\u51fd\u6570\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a \u91cd\u6784(refactoring) \u3002\u6211\u4eec\u4f1a\u5728 3.10 - \u5728\u95ee\u9898\u53d1\u751f\u524d\u53d1\u73b0\u95ee\u9898 \u4e2d\u4ecb\u7ecd\u91cd\u6784\u7684\u6982\u5ff5\u3002 \u901a\u5e38\uff0c\u5728\u5b66\u4e60C++\u7684\u65f6\u5019\uff0c\u4f60\u7f16\u5199\u7684\u5f88\u591a\u7a0b\u5e8f\u90fd\u6d89\u53ca\u5230\u5982\u4e0b\u4e09\u79cd\u7c7b\u578b\u7684\u5b50\u4efb\u52a1\uff1a \u4ece\u7528\u6237\u83b7\u53d6\u8f93\u5165\uff1b \u57fa\u4e8e\u8f93\u5165\u8fdb\u884c\u8ba1\u7b97\uff1b \u6253\u5370\u8ba1\u7b97\u7684\u7ed3\u679c\u3002 \u5bf9\u4e8e\u77ed\u5c0f\u7684\u7a0b\u5e8f (\u4f8b\u5982\uff0c\u5c0f\u4e8e20\u884c\u4ee3\u7801\u7684\u7a0b\u5e8f\uff09\uff0c\u4e0a\u8ff0\u4e09\u4e2a\u5b50\u4efb\u52a1\u90fd\u53ef\u4ee5\u5728 main \u51fd\u6570\u4e2d\u5b8c\u6210\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u66f4\u957f\uff08\u6216\u8005\u51fa\u4e8e\u7ec3\u4e60\u7684\u76ee\u7684\uff09\uff0c\u4e0a\u8ff0\u4e09\u4e2a\u4efb\u52a1\u90fd\u5f88\u9002\u5408\u5404\u81ea\u4f5c\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u51fd\u6570\u3002 \u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u5c06\u8ba1\u7b97\u7ed3\u679c\u548c\u6253\u5370\u7ed3\u679c\u8fd9\u4e24\u6b65\u5408\u5e76\u4e3a\u4e00\u4e2a\u51fd\u6570\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u8fdd\u53cd\u4e86\u201c\u4e00\u4e2a\u51fd\u6570\u53ea\u505a\u4e00\u4ef6\u4e8b\u201d \u7684\u539f\u5219\u3002\u8d1f\u8d23\u8ba1\u7b97\u7ed3\u679c\u7684\u51fd\u6570\u5e94\u8be5\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\uff0c\u7136\u540e\u8ba9\u4e3b\u8c03\u51fd\u6570\u51b3\u5b9a\u5982\u4f55\u4f7f\u7528\u8be5\u8fd4\u56de\u503c\uff08\u4f8b\u5982\u8c03\u7528\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\u5c06\u8be5\u7ed3\u679c\u6253\u5370\u51fa\u6765\uff09\u3002","title":"\u9ad8\u6548\u5730\u4f7f\u7528\u51fd\u6570"},{"location":"2-7-Forward-declarations-and-definitions/","tags":["fordward declaration"],"text":"Key Takeaway \u58f0\u660e\u6ee1\u8db3\u7f16\u8bd1\u5668\u7684\u9700\u8981\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728\u5c1a\u672a\u8fdb\u884c\u5177\u4f53\u5b9a\u4e49\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u524d\u5411\u58f0\u660e\u544a\u77e5\u7f16\u8bd1\u5668\u6807\u8bc6\u7b26\u7684\u4fe1\u606f \u5b9a\u4e49\u6ee1\u8db3\u94fe\u63a5\u5668\u7684\u9700\u8981\uff0c\u5426\u5219\u94fe\u63a5\u65f6\u4f1a\u51fa\u9519 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u662f C++\u4e2d\u6700\u8457\u540d\u7684\u89c4\u5219\u4e4b\u4e00\u3002 \u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u4e00\u4e2a\u51fd\u6570\u3001\u53d8\u91cf\u3001\u7c7b\u578b\u6216\u6a21\u677f\u53ea\u80fd\u591f\u88ab\u5b9a\u4e49\u4e00\u6b21\uff1b \u5728\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u4e00\u4e2a\u53d8\u91cf\u6216\u666e\u901a\u51fd\u6570\u53ea\u80fd\u591f\u6709\u4e00\u4e2a\u5b9a\u4e49\uff1b \u7c7b\u578b\u3001\u6a21\u677f\u3001\u5185\u8054\u51fd\u6570\u548c\u5185\u8054\u53d8\u91cf\u53ef\u4ee5\u6709\u4e00\u6837\u7684\u5b9a\u4e49\uff0c\u4f46\u5fc5\u987b\u4f4d\u4e8e\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d \u3002\u7531\u4e8e\u6211\u4eec\u5c1a\u672a\u4ecb\u7ecd\u8fd9\u4e9b\u6982\u5ff5\uff0c\u56e0\u6b64\u76ee\u524d\u65e0\u9700\u8003\u8651\u5b83\u4eec\u3002 \u4e0d\u540c\u6587\u4ef6\u4e2d\u540c\u540d\uff08\u751a\u81f3\u540c\u53c2\u6570\uff09\u7684\u5185\u90e8\u53d8\u91cf\uff08\u51fd\u6570\uff09\u5177\u6709 \u5185\u90e8\u94fe\u63a5 \uff0c\u5b83\u4eec\u662f\u72ec\u7acb\u7684\uff0c\u56e0\u6b64\u4e0d\u4f1a\u8fdd\u53cd\u5355\u4e00\u5b9a\u4e49\u89c4\u5219 \u5c3d\u7ba1\u6240\u6709\u7684\u5b9a\u4e49\u4e5f\u90fd\u662f\u58f0\u660e\uff0c\u4f46\u53cd\u8fc7\u6765\u5374\u4e0d\u662f\u7684\uff1a\u5e76\u4e0d\u662f\u6240\u6709\u7684\u58f0\u660e\u90fd\u662f\u5b9a\u4e49\u3002 \u8003\u8651\u5982\u4e0b\u7b80\u5355\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } int add ( int x , int y ) { return x + y ; } \u6211\u4eec\u671f\u671b\u7684\u7ed3\u679c\u662f\u7a0b\u5e8f\u6709\u5982\u4e0b\u8f93\u51fa\uff1a 1 The sum of 3 and 4 is: 7 \u4f46\u5b9e\u9645\u4e0a\u8fd9\u4e2a\u7a0b\u5e8f\u6839\u672c\u5c31\u4e0d\u80fd\u88ab\u7f16\u8bd1\uff01Visual Studio \u6253\u5370\u7684\u9519\u8bef\u4fe1\u606f\u5982\u4e0b\uff1a 1 add.cpp(5) : error C3861: 'add': identifier not found \u7f16\u8bd1\u5668 \u5728\u7f16\u8bd1\u4e0a\u8ff0\u7a0b\u5e8f\u7684\u65f6\u5019\u4f1a\u987a\u5e8f\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\u7684\u5185\u5bb9\u3002\u5f53\u7f16\u8bd1\u5668\u5f00\u59cb\u8981\u7f16\u8bd1 \u7b2c\u4e94\u884c \u7684 add \u51fd\u6570\u65f6\uff0c\u5b83\u4e0d\u77e5\u9053 add \u662f\u4ec0\u4e48\uff0c\u56e0\u4e3a\u6211\u4eec\u8fd8\u6ca1\u6709\u5b9a\u4e49 add \uff08\u5b83\u662f\u5728\u7b2c\u4e5d\u884c\u5b9a\u4e49\u7684\uff09\uff01\u56e0\u6b64\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u544a\u4e00\u4e2a \u6807\u8bc6\u7b26(identifier) \u672a\u627e\u5230\u7684\u9519\u8bef\uff08identifier not found\uff09\u3002 \u65e7\u7248\u672c\u7684 Visual Studio \u8fd8\u4f1a\u62a5\u544a\u53e6\u5916\u4e00\u4e2a\u9519\u8bef\uff1a 1 add.cpp(9) : error C2365: 'add'; : redefinition; previous definition was 'formerly unknown identifier' \u8fd9\u4e2a\u9519\u8bef\u4fe1\u606f\u53ef\u80fd\u4f1a\u8bef\u5bfc\u6211\u4eec\uff0c\u56e0\u4e3a add \u5c1a\u672a\u88ab\u5b9a\u4e49\uff0c\u53c8\u600e\u4e48\u4f1a\u6709\u91cd\u590d\u5b9a\u4e49(redefinition)\u7684\u95ee\u9898\u5462\uff1f\u629b\u5f00\u8fd9\u4e2a\u95ee\u9898\u4e0d\u8c08\uff0c\u6211\u4eec\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7f16\u8bd1\u5668\u5728\u62a5\u544a\u9519\u8bef\u7684\u65f6\u5019\uff0c\u7684\u786e\u4f1a\u8fde\u5e26\u5730\u62a5\u544a\u4e00\u4e9b\u5197\u4f59\u7684\u6216\u76f8\u5173\u7684\u9519\u8bef\u6216\u544a\u8b66\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u89e3\u51b3\u7f16\u8bd1\u62a5\u9519\u7684\u95ee\u9898\u65f6\uff0c\u603b\u662f\u5148\u89e3\u51b3\u62a5\u544a\u7684\u7b2c\u4e00\u4e2a\u9519\u8bef\u5e76\u91cd\u65b0\u7f16\u8bd1\u3002 \u4e3a\u4e86\u5904\u7406\u6389\u8fd9\u4e2a\u62a5\u9519\u4fe1\u606f\uff0c\u6211\u4eec\u5fc5\u987b\u8981\u89e3\u51b3\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053 add \u662f\u4ec0\u4e48\u8fd9\u4e00\u95ee\u9898\u3002\u901a\u5e38\uff0c\u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u7528\u3002 \u65b9\u6848 1: \u8c03\u6574\u5b9a\u4e49\u987a\u5e8f \u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u5c31\u662f\u5c06 add \u7684\u5b9a\u4e49\u632a\u5230 main \u524d\u9762\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int add ( int x , int y ) { return x + y ; } int main () { std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } \u8fd9\u4e00\u6837\u4e00\u6765\uff0c\u5f53 main \u51fd\u6570\u8c03\u7528 add \u7684\u65f6\u5019\uff0c\u7f16\u8bd1\u5668\u5df2\u7ecf\u77e5\u9053\u4e86 add \u7684\u5b9a\u4e49\u3002\u5bf9\u4e8e\u8fd9\u6837\u4e00\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\u6765\u8bf4\uff0c\u8fd9\u4e48\u505a\u662f\u5f88\u5bb9\u6613\u7684\u3002\u4f46\u662f\u5bf9\u4e8e\u5927\u578b\u7a0b\u5e8f\u6765\u8bf4\uff0c\u641e\u6e05\u695a\u51fd\u6570\u7684\u8c03\u7528\u987a\u5e8f\u5e76\u4f9d\u6b64\u6765\u8c03\u6574\u5176\u5b9a\u4e49\u987a\u5e8f\u662f\u975e\u5e38\u8d39\u529b\u7684\u3002 \u4e0d\u4ec5\u5982\u6b64\uff0c\u5728\u9762\u5bf9\u6709\u4e9b\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u751a\u81f3\u662f\u884c\u4e0d\u901a\u7684\u3002\u4f8b\u5982\uff0c\u67d0\u4e2a\u7a0b\u5e8f\u5305\u542b\u4e24\u4e2a\u51fd\u6570\uff0cA \u548c B\u3002\u5982\u679c\u51fd\u6570 A \u8c03\u7528\u4e86\u51fd\u6570 B\uff0c\u540c\u65f6\u51fd\u6570 B \u53c8\u8c03\u7528\u4e86\u51fd\u6570 A\uff0c\u90a3\u4e48\u6309\u7167\u672c\u65b9\u6cd5\u6765\u505a\u7684\u8bdd\uff0c\u5b9e\u9645\u4e0a\u662f\u65e0\u6cd5\u8c03\u6574\u987a\u5e8f\u4f7f\u5176\u80fd\u591f\u7f16\u8bd1\u7684\u3002\u5982\u679c\u4f60\u5148\u5b9a\u4e49 A\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u62b1\u6028\u627e\u4e0d\u5230 B\uff0c\u5982\u679c\u4f60\u5148\u5b9a\u4e49 B\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u62b1\u6028\u627e\u4e0d\u5230 A\u3002 \u65b9\u6848 2: \u4f7f\u7528\u524d\u5411\u58f0\u660e \u89e3\u51b3\u95ee\u9898\u7684\u65b9\u6848 2\uff0c\u662f\u4f7f\u7528 \u524d\u5411\u58f0\u660e(forward declaration) \u3002 \u4f7f\u7528\u524d\u5411\u58f0\u660e\u53ef\u4ee5\u5728\u5b9e\u9645\u5b9a\u4e49\u8be5\u6807\u8bc6\u7b26\u524d\uff0c\u9884\u5148\u544a\u77e5\u7f16\u8bd1\u5668\u8be5\u6807\u8bc6\u7b26\u662f\u5b58\u5728\u7684\u3002 \u5bf9\u4e8e\u51fd\u6570\u6765\u8bf4\uff0c\u8fd9\u4e48\u505a\u53ef\u4ee5\u5728\u5b9a\u4e49\u51fd\u6570\u4f53\u4e4b\u524d\uff0c\u5148\u544a\u8bc9\u7f16\u8bd1\u5668\u51fd\u6570\u540d\u5b58\u5728\u3002\u56e0\u6b64\uff0c\u5f53\u7f16\u8bd1\u5668\u7f16\u8bd1\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u5b83\u77e5\u9053\u8be5\u6807\u8bc6\u7b26\u8868\u793a\u4e00\u4e2a\u51fd\u6570\uff0c\u6b64\u65f6\u5728\u8fdb\u884c\u51fd\u6570\u8c03\u7528\uff0c\u5e76\u68c0\u67e5\u51fd\u6570\u8c03\u7528\u662f\u5426\u6b63\u786e\uff0c\u5373\u4f7f\u5176\u4e0a\u4e0d\u77e5\u9053\u8be5\u51fd\u6570\u662f\u5982\u4f55\u5b9a\u4e49\u3001\u5728\u54ea\u91cc\u5b9a\u4e49\u7684\u3002 \u7f16\u5199\u524d\u5411\u5347\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u88ab\u79f0\u4e3a \u51fd\u6570\u539f\u578b \u7684\u58f0\u660e\u8bed\u53e5\u3002\u51fd\u6570\u539f\u578b\u5305\u62ec\u4e86\u51fd\u6570\u5934\uff08\u5373\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u3001\u51fd\u6570\u540d\u548c \u5f62\u53c2 \u7c7b\u578b\uff09\u5e76\u4ee5\u5206\u53f7\u7ed3\u5c3e\u3002\u51fd\u6570\u4f53\u5e76\u4e0d\u5c5e\u4e8e\u51fd\u6570\u539f\u578b\u7684\u4e00\u90e8\u5206\u3002 add \u51fd\u6570\u7684\u51fd\u6570\u539f\u578b\u5982\u4e0b\uff1a 1 2 // \u51fd\u6570\u539f\u578b\u5305\u62ec\u8fd4\u56de\u503c\u7c7b\u578b\u3001\u51fd\u6570\u540d\u3001\u5f62\u53c2\u548c\u4e00\u4e2a\u5206\u53f7\u3002\u4e0d\u9700\u8981\u51fd\u6570\u4f53\uff01 int add ( int x , int y ); \u73b0\u5728\uff0c\u4e3a\u4e4b\u524d\u4e0d\u80fd\u7f16\u8bd1\u7684\u7a0b\u5e8f\u6dfb\u52a0 add \u51fd\u6570\u7684\u524d\u5411\u58f0\u660e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int add ( int x , int y ); // \u4f7f\u7528\u51fd\u6570\u539f\u578b\u8fdb\u884c\u524d\u5411\u58f0\u660e int main () { // \u7531\u4e8e\u524d\u5411\u58f0\u660e\u7684\u5b58\u5728\uff0c\u8be5\u884c\u4ee3\u7801\u4e0d\u518d\u62a5\u9519 std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } int add ( int x , int y ) // \u5373\u4f7f\u51fd\u6570\u4f53\u5728\u540e\u9762\u624d\u5b9a\u4e49 { return x + y ; } \u73b0\u5728\uff0c\u5f53\u7f16\u8bd1\u5668\u5230\u8fbe add \u51fd\u6570\u8c03\u7528\u5904\u662f\uff0c\u5b83\u5df2\u7ecf\u77e5\u9053\u4e86 add \u7684\u539f\u578b\uff08\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\uff09\uff0c\u6b64\u65f6\u5b83\u4fbf\u4e0d\u4f1a\u518d\u62a5\u9519\u4e86\u3002 \u5728\u51fd\u6570\u539f\u578b\u4e2d\u5176\u5b9e\u6ca1\u5fc5\u8981\u63d0\u4f9b\u53c2\u6570\u7684\u540d\u5b57\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4f60\u4e5f\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u8fdb\u884c\u524d\u5411\u58f0\u660e\uff1a 1 int add ( int , int ); // valid function prototype \u4e0d\u8fc7\uff0c\u6211\u4eec\u5efa\u8bae\u8fd8\u662f\u4f7f\u7528\u5b9e\u9645\u7684\u53d8\u91cf\u540d\uff0c\u56e0\u4e3a\u8fd9\u6837\u53ef\u4ee5\u5728\u4f60\u770b\u5230\u51fd\u6570\u539f\u578b\u7684\u65f6\u5019\uff0c\u66f4\u597d\u5730\u7406\u89e3\u5176\u542b\u4e49\u3002\u5426\u5219\uff0c\u4f60\u5fc5\u987b\u518d\u6b21\u5bfb\u627e\u5b83\u7684\u5b9a\u4e49\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u5b9a\u4e49\u51fd\u6570\u539f\u578b\u7684\u65f6\u5019\uff0c\u8bf7\u4fdd\u7559\u51fd\u6570\u540d\u3002\u4f60\u53ef\u4ee5\u8d4b\u503c\u7c98\u8d34\u51fd\u6570\u7684\u539f\u578b\u5e76\u5728\u5176\u540e\u9762\u52a0\u4e0a\u4e00\u4e2a\u5206\u53f7\u6765\u8f7b\u677e\u5730\u521b\u5efa\u524d\u5411\u58f0\u660e\u3002 \u5fd8\u8bb0\u5b9a\u4e49\u51fd\u6570\u600e\u4e48\u529e \u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u5e38\u4f1a\u95ee\uff0c\u5982\u679c\u4ec5\u4ec5\u5bf9\u51fd\u6570\u8fdb\u884c\u4e86\u524d\u5411\u58f0\u660e\uff0c\u4f46\u662f\u6ca1\u6709\u5b9e\u9645\u5b9a\u4e49\u8be5\u51fd\u6570\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f \u8fd9\u4e2a\u95ee\u9898\u7684\u7b54\u6848\u662f\uff1a\u5177\u4f53\u60c5\u51b5\u5177\u4f53\u5206\u6790\u3002\u5982\u679c\u51fd\u6570\u5177\u6709\u524d\u5411\u58f0\u660e\uff0c\u4f46\u662f\u8be5\u51fd\u6570\u5e76\u672a\u88ab\u5b9e\u9645\u8c03\u7528\uff0c\u90a3\u4e48\u7a0b\u5e8f\u662f\u53ef\u4ee5\u6b63\u5e38\u7f16\u8bd1\u548c\u8fd0\u884c\u7684\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u51fd\u6570\u88ab\u5b9e\u9645\u8c03\u7528\u4e86\uff0c\u4f46\u7a0b\u5e8f\u5374\u6ca1\u6709\u5b9a\u4e49\u8be5\u51fd\u6570\uff0c\u90a3\u4e48\u53ea\u80fd\u591f\u4fdd\u8bc1\u7f16\u8bd1\u662f\u53ef\u4ee5\u901a\u8fc7\u7684\uff0c\u4f46 \u94fe\u63a5\u5668(linker) \u4f1a\u62a5\u9519\uff0c\u56e0\u4e3a\u5b83\u65e0\u6cd5\u89e3\u6790\u8be5\u51fd\u6570\u8c03\u7528\u3002 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int add ( int x , int y ); // \u4f7f\u7528\u51fd\u6570\u539f\u578b\u8fdb\u884c\u524d\u5411\u58f0\u660e int main () { std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } // \u6ce8\u610f\uff1a\u6ca1\u6709\u5bf9\u51fd\u6570\u8fdb\u884c\u5b9a\u4e49 \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u5bf9 add \u8fdb\u884c\u4e86\u524d\u5411\u58f0\u660e\u5e76\u4e14\u8c03\u7528\u4e86 add \u51fd\u6570\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u5374\u6ca1\u6709\u5bf9\u5176\u8fdb\u884c\u58f0\u660e\u3002\u5982\u679c\u5bf9\u7a0b\u5e8f\u8fdb\u884c\u7f16\u8bd1\uff0cVisual Studio \u4f1a\u62a5\u544a\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 5 Compiling... add.cpp Linking... add.obj : error LNK2001: unresolved external symbol \"int __cdecl add(int,int)\" (?add@@YAHHH@Z) add.exe : fatal error LNK1120: 1 unresolved externals \u53ef\u4ee5\u770b\u5230\uff0c\u7a0b\u5e8f\u662f\u80fd\u591f\u6b63\u786e\u7f16\u8bd1\u7684\uff0c\u4f46\u662f\u5728\u94fe\u63a5\u9636\u6bb5\u5374\u62a5\u9519\u4e86\uff0c\u56e0\u4e3a int add(int, int) \u6ca1\u6709\u88ab\u5b9a\u4e49\u3002 \u5176\u4ed6\u7c7b\u578b\u7684\u524d\u5411\u58f0\u660e \u524d\u5411\u58f0\u660e\u6700\u5e38\u7528\u4e8e\u51fd\u6570\u3002\u4f46\u662f\uff0c\u524d\u5411\u58f0\u660e\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5176\u4ed6\u7684 C++\u6807\u8bc6\u7b26\uff0c\u4f8b\u5982\u53d8\u91cf\u6216\u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u3002\u4e0d\u8fc7\u5b83\u4eec\u7684\u524d\u5411\u58f0\u660e\u65b9\u5f0f\u7a0d\u6709\u4e0d\u540c\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u58f0\u660e vs. \u5b9a\u4e49 \u5728 C++ \u4e2d\uff0c\u4f60\u65f6\u5e38\u4f1a\u542c\u5230 \u58f0\u660e(declaration) \u548c \u5b9a\u4e49(definition) \u8fd9\u4e24\u4e2a\u8bcd\uff0c\u6709\u65f6\u5019\u5b83\u4eec\u662f\u53ef\u4ee5\u4e92\u6362\u7684\u3002\u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u638c\u63e1\u4e86\u8db3\u591f\u7684\u77e5\u8bc6\uff0c\u53ef\u4ee5\u53bb\u7406\u89e3\u5b83\u4eec\u4e4b\u95f4\u7684\u4e0d\u540c\u4e86\u3002 \u5b9a\u4e49(definition) \u5b9e\u9645\u4e0a\u6307\u7684\u662f\u5bf9\u67d0\u4e2a \u6807\u8bc6\u7b26(identifier) \u7684\u5b9e\u73b0\uff08\u5bf9\u4e8e\u51fd\u6570\u6216\u7c7b\u578b\u6765\u8bf4\uff09\u6216\u5b9e\u4f8b\u5316\uff08\u5bf9\u4e8e\u53d8\u91cf\u6765\u8bf4\uff09\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u6709\u5173\u5b9a\u4e49\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 int add ( int x , int y ) // implements function add() { int z { x + y }; // instantiates variable z return z ; } \u5b9a\u4e49\u662f\u4e3a\u4e86\u6ee1\u8db3\u94fe\u63a5\u5668\u7684\u9700\u8981 \u3002\u5982\u679c\u4f60\u4f7f\u7528\u4e86\u672a\u5b9a\u4e49\u7684\u6807\u8bc6\u7b26\uff0c\u94fe\u63a5\u5668\u5c31\u4f1a\u62a5\u9519\u3002 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule,ODR) \u662f C++\u4e2d\u6700\u8457\u540d\u7684\u89c4\u5219\u4e4b\u4e00\u3002 \u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u4e00\u4e2a\u51fd\u6570\u3001\u53d8\u91cf\u3001\u7c7b\u578b\u6216\u6a21\u677f\u53ea\u80fd\u591f\u88ab\u5b9a\u4e49\u4e00\u6b21\uff1b \u5728\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u4e00\u4e2a\u53d8\u91cf\u6216\u666e\u901a\u51fd\u6570\u53ea\u80fd\u591f\u6709\u4e00\u4e2a\u5b9a\u4e49\u3002\u548c\u4e0a\u4e00\u6761\u7684\u533a\u522b\u5728\u4e8e\u4e00\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u7531\u591a\u4e2a\u6587\u4ef6\u7ec4\u6210\uff08\u540e\u9762\u7684\u8bfe\u7a0b\u4f1a\u4ecb\u7ecd\uff09\uff1b \u7c7b\u578b\u3001\u6a21\u677f\u3001\u5185\u8054\u51fd\u6570\u548c\u5185\u8054\u53d8\u91cf \u53ef\u4ee5\u6709\u4e00\u6837\u7684\u5b9a\u4e49 \uff0c\u4f46\u5fc5\u987b\u4f4d\u4e8e\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\u3002\u7531\u4e8e\u6211\u4eec\u5c1a\u672a\u4ecb\u7ecd\u8fd9\u4e9b\u6982\u5ff5\uff0c\u56e0\u6b64\u76ee\u524d\u65e0\u9700\u8003\u8651\u5b83\u4eec\u3002 \u5982\u679c\u8fdd\u53cd\u4e86 ODR \u7684\u7b2c\u4e00\u6761\u89c4\u5219\uff0c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u544a\u91cd\u590d\u5b9a\u4e49\u7684\u9519\u8bef\u3002\u5982\u679c\u8fdd\u53cd\u4e86 ODR \u7684\u7b2c\u4e8c\u6761\u89c4\u5219\uff0c\u5219\u5f88\u53ef\u80fd\u5bfc\u81f4\u94fe\u63a5\u5668\u62a5\u544a\u91cd\u590d\u5b9a\u4e49\u9519\u8bef\u3002\u8fdd\u53cd ODR \u7b2c\u4e09\u6761\u89c4\u5219\u5219\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e0b\u9762\u4f8b\u5b50\u5c55\u793a\u4e86\u8fdd\u53cd\u7b2c\u4e00\u6761\u89c4\u5219\u7684\u60c5\u5f62\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int add ( int x , int y ) { return x + y ; } int add ( int x , int y ) // \u8fdd\u53cd\u4e86ODR\uff0c\u56e0\u4e3a add \u5df2\u7ecf\u88ab\u5b9a\u4e49\u8fc7\u4e86 { return x + y ; } int main () { int x ; int x ; // \u8fdd\u53cd\u4e86ODR\uff0c\u56e0\u4e3a x \u5df2\u7ecf\u88ab\u5b9a\u4e49\u8fc7\u4e86 } \u7531\u4e8e\u4e0a\u8ff0\u7a0b\u5e8f\u8fdd\u53cd\u4e86ODR\u7b2c\u4e00\u6761\u89c4\u5219\uff0cVisual Studio \u7684\u7f16\u8bd1\u5668\u4f1a\u62a5\u544a\u5982\u4e0b\u7f16\u8bd1\u9519\u8bef\uff1a 1 2 3 4 project3.cpp(9): error C2084: function 'int add(int,int)' already has a body project3.cpp(3): note: see previous definition of 'add' project3.cpp(16): error C2086: 'int x': redefinition project3.cpp(15): note: see declaration of 'x' \u6269\u5c55\u9605\u8bfb \u6807\u8bc6\u7b26\u76f8\u540c\u4f46\u53c2\u6570\u4e0d\u540c\u7684\u51fd\u6570\uff0c\u88ab\u770b\u505a\u662f\u4e0d\u540c\u7684\u51fd\u6570\u3002\u6211\u4eec\u4f1a\u5728 8.9 - \u51fd\u6570\u91cd\u8f7d \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u58f0\u660e\u5219\u662f\u4e00\u6761\u8bed\u53e5\uff0c\u5b83\u544a\u8bc9\u7f16\u8bd1\u5668\u67d0\u4e2a\u6807\u8bc6\u7b26\u662f\u5b58\u5728\u7684\uff0c\u5e76\u63d0\u4f9b\u5176\u7c7b\u578b\u4fe1\u606f\u3002\u58f0\u660e\u76f8\u5173\u7684\u4f8b\u5b50\u5982\u4e0b\uff1a 1 2 int add ( int x , int y ); // tells the compiler about a function named \"add\" that takes two int parameters and returns an int. No body! int x ; // tells the compiler about an integer variable named x \u58f0\u660e(declaration) \u7eaf\u7cb9\u4e3a\u4e86\u6ee1\u8db3\u7f16\u8bd1\u5668\u7684\u9700\u8981\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728\u5c1a\u672a\u8fdb\u884c\u5177\u4f53\u5b9a\u4e49\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u524d\u5411\u58f0\u660e\u544a\u77e5\u7f16\u8bd1\u5668\u6807\u8bc6\u7b26\u7684\u4fe1\u606f\u3002 \u5728 C++ \u4e2d\uff0c\u5b9a\u4e49\u4e5f\u5177\u6709\u58f0\u660e\u7684\u529f\u80fd\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728\u4e0a\u9762\u58f0\u660e\u548c\u5b9a\u4e49\u7684\u4f8b\u5b50\u4e2d\u90fd\u51fa\u73b0\u4e86 int x \uff0c\u56e0\u4e3a\u5b83\u65e2\u662f\u58f0\u660e\u4e5f\u662f\u5b9a\u4e49\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5b9a\u4e49\u53ef\u4ee5\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u8981\uff0c\u56e0\u4e3a\u5b83\u65e2\u53ef\u4ee5\u88ab\u7f16\u8bd1\u5668\u4f7f\u7528\uff0c\u4e5f\u53ef\u4ee5\u88ab\u94fe\u63a5\u5668\u4f7f\u7528\u3002\u53ea\u6709\u5f53\u9700\u8981\u5728\u6807\u8bc6\u7b26\u5c1a\u672a\u5b9a\u4e49\u524d\u4f7f\u7528\u5b83\u65f6\uff0c\u624d\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u663e\u5f0f\u7684\u58f0\u660e\u3002 \u5c3d\u7ba1\u6240\u6709\u7684\u5b9a\u4e49\u4e5f\u90fd\u662f\u58f0\u660e\uff0c\u4f46\u53cd\u8fc7\u6765\u5374\u4e0d\u662f\u7684\uff1a\u5e76\u4e0d\u662f\u6240\u6709\u7684\u58f0\u660e\u90fd\u662f\u5b9a\u4e49 \u3002\u4ee5\u51fd\u6570\u539f\u578b\u4e3a\u4f8b\u2014\u2014\u5b83\u53ea\u80fd\u591f\u6ee1\u8db3\u7f16\u8bd1\u5668\u7684\u9700\u8981\uff0c\u4f46\u4e0d\u80fd\u6ee1\u8db3\u94fe\u63a5\u5668\u7684\u9700\u8981\u3002\u5bf9\u4e8e\u90a3\u4e9b\u53ea\u662f\u58f0\u660e\uff0c\u4f46\u662f\u4e0d\u662f\u5b9a\u4e49\u7684\u58f0\u660e\uff0c\u79f0\u4e3a \u7eaf\u58f0\u660e(pure declaration) \u3002 \u5176\u4ed6\u7c7b\u578b\u7684\u7eaf\u58f0\u660e\u5305\u62ec\u53d8\u91cf\u7684\u524d\u5411\u58f0\u660e\u548c\u7c7b\u578b\u58f0\u660e \uff08\u5728\u540e\u7eed\u7684\u7ae0\u8282\u4f1a\u8fdb\u884c\u4ecb\u7ecd\uff0c\u76ee\u524d\u65e0\u9700\u64cd\u5fc3\uff09\u3002 ODR\u539f\u5219\u5e76\u4e0d\u9002\u7528\u4e8e\u7eaf\u58f0\u660e\uff08\u6bd5\u7adfODR\u6307\u7684\u662f \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u800c\u4e0d\u662f \u5355\u4e00\u58f0\u660e\u89c4\u5219 \uff09\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u591a\u6b21\u58f0\u660e\u67d0\u4e2a\u6807\u8bc6\u7b26\uff08\u5c3d\u7ba1\u8fd9\u4e48\u505a\u662f\u591a\u4f59\u7684\uff09\u3002 \u4f5c\u8005\u6ce8 \u5728\u4e00\u822c\u7528\u8bed\u4e2d\uff0c \u58f0\u660e \u4e00\u822c\u6307\u7eaf\u58f0\u660e\uff0c\u800c \u5b9a\u4e49 \u5219\u6307\u7684\u662f\u65e2\u80fd\u505a\u5b9a\u4e49\uff0c\u4e5f\u80fd\u505a\u58f0\u660e\u7684\u5b9a\u4e49\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u79f0 int x; \u4e3a\u5b9a\u4e49\uff0c\u5c3d\u7ba1\u5b83\u65e2\u662f\u5b9a\u4e49\u4e5f\u662f\u58f0\u660e\u3002","title":"2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49"},{"location":"2-7-Forward-declarations-and-definitions/#1","text":"\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u5c31\u662f\u5c06 add \u7684\u5b9a\u4e49\u632a\u5230 main \u524d\u9762\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int add ( int x , int y ) { return x + y ; } int main () { std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } \u8fd9\u4e00\u6837\u4e00\u6765\uff0c\u5f53 main \u51fd\u6570\u8c03\u7528 add \u7684\u65f6\u5019\uff0c\u7f16\u8bd1\u5668\u5df2\u7ecf\u77e5\u9053\u4e86 add \u7684\u5b9a\u4e49\u3002\u5bf9\u4e8e\u8fd9\u6837\u4e00\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\u6765\u8bf4\uff0c\u8fd9\u4e48\u505a\u662f\u5f88\u5bb9\u6613\u7684\u3002\u4f46\u662f\u5bf9\u4e8e\u5927\u578b\u7a0b\u5e8f\u6765\u8bf4\uff0c\u641e\u6e05\u695a\u51fd\u6570\u7684\u8c03\u7528\u987a\u5e8f\u5e76\u4f9d\u6b64\u6765\u8c03\u6574\u5176\u5b9a\u4e49\u987a\u5e8f\u662f\u975e\u5e38\u8d39\u529b\u7684\u3002 \u4e0d\u4ec5\u5982\u6b64\uff0c\u5728\u9762\u5bf9\u6709\u4e9b\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u751a\u81f3\u662f\u884c\u4e0d\u901a\u7684\u3002\u4f8b\u5982\uff0c\u67d0\u4e2a\u7a0b\u5e8f\u5305\u542b\u4e24\u4e2a\u51fd\u6570\uff0cA \u548c B\u3002\u5982\u679c\u51fd\u6570 A \u8c03\u7528\u4e86\u51fd\u6570 B\uff0c\u540c\u65f6\u51fd\u6570 B \u53c8\u8c03\u7528\u4e86\u51fd\u6570 A\uff0c\u90a3\u4e48\u6309\u7167\u672c\u65b9\u6cd5\u6765\u505a\u7684\u8bdd\uff0c\u5b9e\u9645\u4e0a\u662f\u65e0\u6cd5\u8c03\u6574\u987a\u5e8f\u4f7f\u5176\u80fd\u591f\u7f16\u8bd1\u7684\u3002\u5982\u679c\u4f60\u5148\u5b9a\u4e49 A\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u62b1\u6028\u627e\u4e0d\u5230 B\uff0c\u5982\u679c\u4f60\u5148\u5b9a\u4e49 B\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u62b1\u6028\u627e\u4e0d\u5230 A\u3002","title":"\u65b9\u6848 1: \u8c03\u6574\u5b9a\u4e49\u987a\u5e8f"},{"location":"2-7-Forward-declarations-and-definitions/#2","text":"\u89e3\u51b3\u95ee\u9898\u7684\u65b9\u6848 2\uff0c\u662f\u4f7f\u7528 \u524d\u5411\u58f0\u660e(forward declaration) \u3002 \u4f7f\u7528\u524d\u5411\u58f0\u660e\u53ef\u4ee5\u5728\u5b9e\u9645\u5b9a\u4e49\u8be5\u6807\u8bc6\u7b26\u524d\uff0c\u9884\u5148\u544a\u77e5\u7f16\u8bd1\u5668\u8be5\u6807\u8bc6\u7b26\u662f\u5b58\u5728\u7684\u3002 \u5bf9\u4e8e\u51fd\u6570\u6765\u8bf4\uff0c\u8fd9\u4e48\u505a\u53ef\u4ee5\u5728\u5b9a\u4e49\u51fd\u6570\u4f53\u4e4b\u524d\uff0c\u5148\u544a\u8bc9\u7f16\u8bd1\u5668\u51fd\u6570\u540d\u5b58\u5728\u3002\u56e0\u6b64\uff0c\u5f53\u7f16\u8bd1\u5668\u7f16\u8bd1\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u5b83\u77e5\u9053\u8be5\u6807\u8bc6\u7b26\u8868\u793a\u4e00\u4e2a\u51fd\u6570\uff0c\u6b64\u65f6\u5728\u8fdb\u884c\u51fd\u6570\u8c03\u7528\uff0c\u5e76\u68c0\u67e5\u51fd\u6570\u8c03\u7528\u662f\u5426\u6b63\u786e\uff0c\u5373\u4f7f\u5176\u4e0a\u4e0d\u77e5\u9053\u8be5\u51fd\u6570\u662f\u5982\u4f55\u5b9a\u4e49\u3001\u5728\u54ea\u91cc\u5b9a\u4e49\u7684\u3002 \u7f16\u5199\u524d\u5411\u5347\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u88ab\u79f0\u4e3a \u51fd\u6570\u539f\u578b \u7684\u58f0\u660e\u8bed\u53e5\u3002\u51fd\u6570\u539f\u578b\u5305\u62ec\u4e86\u51fd\u6570\u5934\uff08\u5373\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u3001\u51fd\u6570\u540d\u548c \u5f62\u53c2 \u7c7b\u578b\uff09\u5e76\u4ee5\u5206\u53f7\u7ed3\u5c3e\u3002\u51fd\u6570\u4f53\u5e76\u4e0d\u5c5e\u4e8e\u51fd\u6570\u539f\u578b\u7684\u4e00\u90e8\u5206\u3002 add \u51fd\u6570\u7684\u51fd\u6570\u539f\u578b\u5982\u4e0b\uff1a 1 2 // \u51fd\u6570\u539f\u578b\u5305\u62ec\u8fd4\u56de\u503c\u7c7b\u578b\u3001\u51fd\u6570\u540d\u3001\u5f62\u53c2\u548c\u4e00\u4e2a\u5206\u53f7\u3002\u4e0d\u9700\u8981\u51fd\u6570\u4f53\uff01 int add ( int x , int y ); \u73b0\u5728\uff0c\u4e3a\u4e4b\u524d\u4e0d\u80fd\u7f16\u8bd1\u7684\u7a0b\u5e8f\u6dfb\u52a0 add \u51fd\u6570\u7684\u524d\u5411\u58f0\u660e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int add ( int x , int y ); // \u4f7f\u7528\u51fd\u6570\u539f\u578b\u8fdb\u884c\u524d\u5411\u58f0\u660e int main () { // \u7531\u4e8e\u524d\u5411\u58f0\u660e\u7684\u5b58\u5728\uff0c\u8be5\u884c\u4ee3\u7801\u4e0d\u518d\u62a5\u9519 std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } int add ( int x , int y ) // \u5373\u4f7f\u51fd\u6570\u4f53\u5728\u540e\u9762\u624d\u5b9a\u4e49 { return x + y ; } \u73b0\u5728\uff0c\u5f53\u7f16\u8bd1\u5668\u5230\u8fbe add \u51fd\u6570\u8c03\u7528\u5904\u662f\uff0c\u5b83\u5df2\u7ecf\u77e5\u9053\u4e86 add \u7684\u539f\u578b\uff08\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\uff09\uff0c\u6b64\u65f6\u5b83\u4fbf\u4e0d\u4f1a\u518d\u62a5\u9519\u4e86\u3002 \u5728\u51fd\u6570\u539f\u578b\u4e2d\u5176\u5b9e\u6ca1\u5fc5\u8981\u63d0\u4f9b\u53c2\u6570\u7684\u540d\u5b57\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4f60\u4e5f\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u8fdb\u884c\u524d\u5411\u58f0\u660e\uff1a 1 int add ( int , int ); // valid function prototype \u4e0d\u8fc7\uff0c\u6211\u4eec\u5efa\u8bae\u8fd8\u662f\u4f7f\u7528\u5b9e\u9645\u7684\u53d8\u91cf\u540d\uff0c\u56e0\u4e3a\u8fd9\u6837\u53ef\u4ee5\u5728\u4f60\u770b\u5230\u51fd\u6570\u539f\u578b\u7684\u65f6\u5019\uff0c\u66f4\u597d\u5730\u7406\u89e3\u5176\u542b\u4e49\u3002\u5426\u5219\uff0c\u4f60\u5fc5\u987b\u518d\u6b21\u5bfb\u627e\u5b83\u7684\u5b9a\u4e49\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u5b9a\u4e49\u51fd\u6570\u539f\u578b\u7684\u65f6\u5019\uff0c\u8bf7\u4fdd\u7559\u51fd\u6570\u540d\u3002\u4f60\u53ef\u4ee5\u8d4b\u503c\u7c98\u8d34\u51fd\u6570\u7684\u539f\u578b\u5e76\u5728\u5176\u540e\u9762\u52a0\u4e0a\u4e00\u4e2a\u5206\u53f7\u6765\u8f7b\u677e\u5730\u521b\u5efa\u524d\u5411\u58f0\u660e\u3002","title":"\u65b9\u6848 2: \u4f7f\u7528\u524d\u5411\u58f0\u660e"},{"location":"2-7-Forward-declarations-and-definitions/#_1","text":"\u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u5e38\u4f1a\u95ee\uff0c\u5982\u679c\u4ec5\u4ec5\u5bf9\u51fd\u6570\u8fdb\u884c\u4e86\u524d\u5411\u58f0\u660e\uff0c\u4f46\u662f\u6ca1\u6709\u5b9e\u9645\u5b9a\u4e49\u8be5\u51fd\u6570\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f \u8fd9\u4e2a\u95ee\u9898\u7684\u7b54\u6848\u662f\uff1a\u5177\u4f53\u60c5\u51b5\u5177\u4f53\u5206\u6790\u3002\u5982\u679c\u51fd\u6570\u5177\u6709\u524d\u5411\u58f0\u660e\uff0c\u4f46\u662f\u8be5\u51fd\u6570\u5e76\u672a\u88ab\u5b9e\u9645\u8c03\u7528\uff0c\u90a3\u4e48\u7a0b\u5e8f\u662f\u53ef\u4ee5\u6b63\u5e38\u7f16\u8bd1\u548c\u8fd0\u884c\u7684\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u51fd\u6570\u88ab\u5b9e\u9645\u8c03\u7528\u4e86\uff0c\u4f46\u7a0b\u5e8f\u5374\u6ca1\u6709\u5b9a\u4e49\u8be5\u51fd\u6570\uff0c\u90a3\u4e48\u53ea\u80fd\u591f\u4fdd\u8bc1\u7f16\u8bd1\u662f\u53ef\u4ee5\u901a\u8fc7\u7684\uff0c\u4f46 \u94fe\u63a5\u5668(linker) \u4f1a\u62a5\u9519\uff0c\u56e0\u4e3a\u5b83\u65e0\u6cd5\u89e3\u6790\u8be5\u51fd\u6570\u8c03\u7528\u3002 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int add ( int x , int y ); // \u4f7f\u7528\u51fd\u6570\u539f\u578b\u8fdb\u884c\u524d\u5411\u58f0\u660e int main () { std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } // \u6ce8\u610f\uff1a\u6ca1\u6709\u5bf9\u51fd\u6570\u8fdb\u884c\u5b9a\u4e49 \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u5bf9 add \u8fdb\u884c\u4e86\u524d\u5411\u58f0\u660e\u5e76\u4e14\u8c03\u7528\u4e86 add \u51fd\u6570\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u5374\u6ca1\u6709\u5bf9\u5176\u8fdb\u884c\u58f0\u660e\u3002\u5982\u679c\u5bf9\u7a0b\u5e8f\u8fdb\u884c\u7f16\u8bd1\uff0cVisual Studio \u4f1a\u62a5\u544a\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 5 Compiling... add.cpp Linking... add.obj : error LNK2001: unresolved external symbol \"int __cdecl add(int,int)\" (?add@@YAHHH@Z) add.exe : fatal error LNK1120: 1 unresolved externals \u53ef\u4ee5\u770b\u5230\uff0c\u7a0b\u5e8f\u662f\u80fd\u591f\u6b63\u786e\u7f16\u8bd1\u7684\uff0c\u4f46\u662f\u5728\u94fe\u63a5\u9636\u6bb5\u5374\u62a5\u9519\u4e86\uff0c\u56e0\u4e3a int add(int, int) \u6ca1\u6709\u88ab\u5b9a\u4e49\u3002","title":"\u5fd8\u8bb0\u5b9a\u4e49\u51fd\u6570\u600e\u4e48\u529e"},{"location":"2-7-Forward-declarations-and-definitions/#_2","text":"\u524d\u5411\u58f0\u660e\u6700\u5e38\u7528\u4e8e\u51fd\u6570\u3002\u4f46\u662f\uff0c\u524d\u5411\u58f0\u660e\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5176\u4ed6\u7684 C++\u6807\u8bc6\u7b26\uff0c\u4f8b\u5982\u53d8\u91cf\u6216\u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u3002\u4e0d\u8fc7\u5b83\u4eec\u7684\u524d\u5411\u58f0\u660e\u65b9\u5f0f\u7a0d\u6709\u4e0d\u540c\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"\u5176\u4ed6\u7c7b\u578b\u7684\u524d\u5411\u58f0\u660e"},{"location":"2-7-Forward-declarations-and-definitions/#vs","text":"\u5728 C++ \u4e2d\uff0c\u4f60\u65f6\u5e38\u4f1a\u542c\u5230 \u58f0\u660e(declaration) \u548c \u5b9a\u4e49(definition) \u8fd9\u4e24\u4e2a\u8bcd\uff0c\u6709\u65f6\u5019\u5b83\u4eec\u662f\u53ef\u4ee5\u4e92\u6362\u7684\u3002\u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u638c\u63e1\u4e86\u8db3\u591f\u7684\u77e5\u8bc6\uff0c\u53ef\u4ee5\u53bb\u7406\u89e3\u5b83\u4eec\u4e4b\u95f4\u7684\u4e0d\u540c\u4e86\u3002 \u5b9a\u4e49(definition) \u5b9e\u9645\u4e0a\u6307\u7684\u662f\u5bf9\u67d0\u4e2a \u6807\u8bc6\u7b26(identifier) \u7684\u5b9e\u73b0\uff08\u5bf9\u4e8e\u51fd\u6570\u6216\u7c7b\u578b\u6765\u8bf4\uff09\u6216\u5b9e\u4f8b\u5316\uff08\u5bf9\u4e8e\u53d8\u91cf\u6765\u8bf4\uff09\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u6709\u5173\u5b9a\u4e49\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 int add ( int x , int y ) // implements function add() { int z { x + y }; // instantiates variable z return z ; } \u5b9a\u4e49\u662f\u4e3a\u4e86\u6ee1\u8db3\u94fe\u63a5\u5668\u7684\u9700\u8981 \u3002\u5982\u679c\u4f60\u4f7f\u7528\u4e86\u672a\u5b9a\u4e49\u7684\u6807\u8bc6\u7b26\uff0c\u94fe\u63a5\u5668\u5c31\u4f1a\u62a5\u9519\u3002 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule,ODR) \u662f C++\u4e2d\u6700\u8457\u540d\u7684\u89c4\u5219\u4e4b\u4e00\u3002 \u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u4e00\u4e2a\u51fd\u6570\u3001\u53d8\u91cf\u3001\u7c7b\u578b\u6216\u6a21\u677f\u53ea\u80fd\u591f\u88ab\u5b9a\u4e49\u4e00\u6b21\uff1b \u5728\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u4e00\u4e2a\u53d8\u91cf\u6216\u666e\u901a\u51fd\u6570\u53ea\u80fd\u591f\u6709\u4e00\u4e2a\u5b9a\u4e49\u3002\u548c\u4e0a\u4e00\u6761\u7684\u533a\u522b\u5728\u4e8e\u4e00\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u7531\u591a\u4e2a\u6587\u4ef6\u7ec4\u6210\uff08\u540e\u9762\u7684\u8bfe\u7a0b\u4f1a\u4ecb\u7ecd\uff09\uff1b \u7c7b\u578b\u3001\u6a21\u677f\u3001\u5185\u8054\u51fd\u6570\u548c\u5185\u8054\u53d8\u91cf \u53ef\u4ee5\u6709\u4e00\u6837\u7684\u5b9a\u4e49 \uff0c\u4f46\u5fc5\u987b\u4f4d\u4e8e\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\u3002\u7531\u4e8e\u6211\u4eec\u5c1a\u672a\u4ecb\u7ecd\u8fd9\u4e9b\u6982\u5ff5\uff0c\u56e0\u6b64\u76ee\u524d\u65e0\u9700\u8003\u8651\u5b83\u4eec\u3002 \u5982\u679c\u8fdd\u53cd\u4e86 ODR \u7684\u7b2c\u4e00\u6761\u89c4\u5219\uff0c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u544a\u91cd\u590d\u5b9a\u4e49\u7684\u9519\u8bef\u3002\u5982\u679c\u8fdd\u53cd\u4e86 ODR \u7684\u7b2c\u4e8c\u6761\u89c4\u5219\uff0c\u5219\u5f88\u53ef\u80fd\u5bfc\u81f4\u94fe\u63a5\u5668\u62a5\u544a\u91cd\u590d\u5b9a\u4e49\u9519\u8bef\u3002\u8fdd\u53cd ODR \u7b2c\u4e09\u6761\u89c4\u5219\u5219\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e0b\u9762\u4f8b\u5b50\u5c55\u793a\u4e86\u8fdd\u53cd\u7b2c\u4e00\u6761\u89c4\u5219\u7684\u60c5\u5f62\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int add ( int x , int y ) { return x + y ; } int add ( int x , int y ) // \u8fdd\u53cd\u4e86ODR\uff0c\u56e0\u4e3a add \u5df2\u7ecf\u88ab\u5b9a\u4e49\u8fc7\u4e86 { return x + y ; } int main () { int x ; int x ; // \u8fdd\u53cd\u4e86ODR\uff0c\u56e0\u4e3a x \u5df2\u7ecf\u88ab\u5b9a\u4e49\u8fc7\u4e86 } \u7531\u4e8e\u4e0a\u8ff0\u7a0b\u5e8f\u8fdd\u53cd\u4e86ODR\u7b2c\u4e00\u6761\u89c4\u5219\uff0cVisual Studio \u7684\u7f16\u8bd1\u5668\u4f1a\u62a5\u544a\u5982\u4e0b\u7f16\u8bd1\u9519\u8bef\uff1a 1 2 3 4 project3.cpp(9): error C2084: function 'int add(int,int)' already has a body project3.cpp(3): note: see previous definition of 'add' project3.cpp(16): error C2086: 'int x': redefinition project3.cpp(15): note: see declaration of 'x' \u6269\u5c55\u9605\u8bfb \u6807\u8bc6\u7b26\u76f8\u540c\u4f46\u53c2\u6570\u4e0d\u540c\u7684\u51fd\u6570\uff0c\u88ab\u770b\u505a\u662f\u4e0d\u540c\u7684\u51fd\u6570\u3002\u6211\u4eec\u4f1a\u5728 8.9 - \u51fd\u6570\u91cd\u8f7d \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u58f0\u660e\u5219\u662f\u4e00\u6761\u8bed\u53e5\uff0c\u5b83\u544a\u8bc9\u7f16\u8bd1\u5668\u67d0\u4e2a\u6807\u8bc6\u7b26\u662f\u5b58\u5728\u7684\uff0c\u5e76\u63d0\u4f9b\u5176\u7c7b\u578b\u4fe1\u606f\u3002\u58f0\u660e\u76f8\u5173\u7684\u4f8b\u5b50\u5982\u4e0b\uff1a 1 2 int add ( int x , int y ); // tells the compiler about a function named \"add\" that takes two int parameters and returns an int. No body! int x ; // tells the compiler about an integer variable named x \u58f0\u660e(declaration) \u7eaf\u7cb9\u4e3a\u4e86\u6ee1\u8db3\u7f16\u8bd1\u5668\u7684\u9700\u8981\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728\u5c1a\u672a\u8fdb\u884c\u5177\u4f53\u5b9a\u4e49\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u524d\u5411\u58f0\u660e\u544a\u77e5\u7f16\u8bd1\u5668\u6807\u8bc6\u7b26\u7684\u4fe1\u606f\u3002 \u5728 C++ \u4e2d\uff0c\u5b9a\u4e49\u4e5f\u5177\u6709\u58f0\u660e\u7684\u529f\u80fd\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728\u4e0a\u9762\u58f0\u660e\u548c\u5b9a\u4e49\u7684\u4f8b\u5b50\u4e2d\u90fd\u51fa\u73b0\u4e86 int x \uff0c\u56e0\u4e3a\u5b83\u65e2\u662f\u58f0\u660e\u4e5f\u662f\u5b9a\u4e49\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5b9a\u4e49\u53ef\u4ee5\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u8981\uff0c\u56e0\u4e3a\u5b83\u65e2\u53ef\u4ee5\u88ab\u7f16\u8bd1\u5668\u4f7f\u7528\uff0c\u4e5f\u53ef\u4ee5\u88ab\u94fe\u63a5\u5668\u4f7f\u7528\u3002\u53ea\u6709\u5f53\u9700\u8981\u5728\u6807\u8bc6\u7b26\u5c1a\u672a\u5b9a\u4e49\u524d\u4f7f\u7528\u5b83\u65f6\uff0c\u624d\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u663e\u5f0f\u7684\u58f0\u660e\u3002 \u5c3d\u7ba1\u6240\u6709\u7684\u5b9a\u4e49\u4e5f\u90fd\u662f\u58f0\u660e\uff0c\u4f46\u53cd\u8fc7\u6765\u5374\u4e0d\u662f\u7684\uff1a\u5e76\u4e0d\u662f\u6240\u6709\u7684\u58f0\u660e\u90fd\u662f\u5b9a\u4e49 \u3002\u4ee5\u51fd\u6570\u539f\u578b\u4e3a\u4f8b\u2014\u2014\u5b83\u53ea\u80fd\u591f\u6ee1\u8db3\u7f16\u8bd1\u5668\u7684\u9700\u8981\uff0c\u4f46\u4e0d\u80fd\u6ee1\u8db3\u94fe\u63a5\u5668\u7684\u9700\u8981\u3002\u5bf9\u4e8e\u90a3\u4e9b\u53ea\u662f\u58f0\u660e\uff0c\u4f46\u662f\u4e0d\u662f\u5b9a\u4e49\u7684\u58f0\u660e\uff0c\u79f0\u4e3a \u7eaf\u58f0\u660e(pure declaration) \u3002 \u5176\u4ed6\u7c7b\u578b\u7684\u7eaf\u58f0\u660e\u5305\u62ec\u53d8\u91cf\u7684\u524d\u5411\u58f0\u660e\u548c\u7c7b\u578b\u58f0\u660e \uff08\u5728\u540e\u7eed\u7684\u7ae0\u8282\u4f1a\u8fdb\u884c\u4ecb\u7ecd\uff0c\u76ee\u524d\u65e0\u9700\u64cd\u5fc3\uff09\u3002 ODR\u539f\u5219\u5e76\u4e0d\u9002\u7528\u4e8e\u7eaf\u58f0\u660e\uff08\u6bd5\u7adfODR\u6307\u7684\u662f \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u800c\u4e0d\u662f \u5355\u4e00\u58f0\u660e\u89c4\u5219 \uff09\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u591a\u6b21\u58f0\u660e\u67d0\u4e2a\u6807\u8bc6\u7b26\uff08\u5c3d\u7ba1\u8fd9\u4e48\u505a\u662f\u591a\u4f59\u7684\uff09\u3002 \u4f5c\u8005\u6ce8 \u5728\u4e00\u822c\u7528\u8bed\u4e2d\uff0c \u58f0\u660e \u4e00\u822c\u6307\u7eaf\u58f0\u660e\uff0c\u800c \u5b9a\u4e49 \u5219\u6307\u7684\u662f\u65e2\u80fd\u505a\u5b9a\u4e49\uff0c\u4e5f\u80fd\u505a\u58f0\u660e\u7684\u5b9a\u4e49\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u79f0 int x; \u4e3a\u5b9a\u4e49\uff0c\u5c3d\u7ba1\u5b83\u65e2\u662f\u5b9a\u4e49\u4e5f\u662f\u58f0\u660e\u3002","title":"\u58f0\u660e vs. \u5b9a\u4e49"},{"location":"2-8-Programs-with-multiple-code-files/","tags":["program organization"],"text":"\u5728\u9879\u76ee\u4e2d\u6dfb\u52a0\u6587\u4ef6 \u968f\u7740\u7a0b\u5e8f\u89c4\u6a21\u4e0d\u65ad\u589e\u5927\uff0c\u7a0b\u5e8f\u901a\u5e38\u4f1a\u88ab\u5206\u5272\u4e3a\u591a\u4e2a\u6587\u4ef6\u4ee5\u65b9\u4fbf\u7ec4\u7ec7\u7a0b\u5e8f\u6216\u589e\u5f3a\u5176\u53ef\u590d\u7528\u6027\u3002\u4f7f\u7528 IDE \u53ef\u4ee5\u8ba9\u7ec4\u7ec7\u591a\u6587\u4ef6\u9879\u76ee\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\u3002\u6211\u4eec\u5df2\u7ecf\u5b66\u4e60\u4e86\u5982\u4f55\u53bb\u521b\u5efa\u5e76\u7f16\u8bd1\u4e00\u4e2a\u5355\u6587\u4ef6\u9879\u76ee\uff0c\u4e3a\u8fd9\u4e2a\u9879\u76ee\u6dfb\u52a0\u4e00\u4e2a\u6587\u4ef6\u662f\u975e\u5e38\u7b80\u5355\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u4e3a\u9879\u76ee\u6dfb\u52a0\u4ee3\u7801\u6587\u4ef6\u65f6\uff0c\u8bf7\u4f7f\u7528 .cpp \u4f5c\u4e3a\u6269\u5c55\u540d\u3002 For Visual Studio users \u5728 Visual Studio \u4e2d\uff0c\u5728\u89e3\u51b3\u65b9\u6848\u6d4f\u89c8\u5668\u7a97\u53e3(Solution Explorer)\u53f3\u952e\u70b9\u51fb Source Files \u76ee\u5f55\u5e76\u9009\u62e9 Add > New Item\u2026 . \u9009\u62e9 C++ File (.cpp) \u3002\u4e3a\u5176\u6307\u5b9a\u4e00\u4e2a\u6587\u4ef6\u540d\uff0c\u5c31\u53ef\u4ee5\u5c06\u6587\u4ef6\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u3002 \u6ce8\u610f\uff1a\u5982\u679c\u4f60\u662f\u4ece File \u83dc\u5355\u800c\u4e0d\u662f Solution Explorer \u7a97\u53e3\u521b\u5efa\u7684\u6587\u4ef6\uff0c\u90a3\u4e48\u8be5\u6587\u4ef6\u4e0d\u4f1a\u88ab\u81ea\u52a8\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u3002\u56e0\u6b64\u4f60\u5fc5\u987b\u5728\u89e3\u51b3\u65b9\u6848\u6d4f\u89c8\u5668\u7a97\u53e3(Solution Explorer)\u53f3\u952e\u70b9\u51fb Source Files \u76ee\u5f55\u5e76\u9009\u62e9 Add > Existing Item \uff0c\u7136\u540e\u9009\u62e9\u521a\u624d\u521b\u5efa\u7684\u6587\u4ef6\u3002 \u63a5\u4e0b\u6765\uff0c\u5728\u7f16\u8bd1\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u4f60\u4f1a\u53d1\u73b0\u7f16\u8bd1\u5668\u4f1a\u5c06\u8be5\u6587\u4ef6\u5217\u51fa\u6765\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5bf9\u5176\u8fdb\u884c\u7f16\u8bd1\u3002 For Code:: Blocks users \u5728 Code:: Blocks \u4e2d\uff0c\u5728 File \u83dc\u5355\u4e2d\u9009\u62e9 New > File\u2026 \u3002 \u5728 New from template \u5bf9\u8bdd\u6846\u4e2d\uff0c\u9009\u62e9 C/C++ source \u5e76\u70b9\u51fb Go \u3002 \u4f60\u53ef\u80fd\u4f1a\u6765\u5230\u4e00\u4e2a welcome to the C/C++ source file wizard \u5bf9\u8bdd\u6846\u3002\u6b64\u65f6\u70b9\u51fb Next \u3002 \u5728\u5411\u5bfc\u7684\u4e0b\u4e00\u4e2a\u9875\u9762\u4e2d\uff0c\u9009\u62e9 \u201cC++\u201d \u5e76\u70b9\u51fb Next \u3002 \u63a5\u4e0b\u6765\uff0c\u4e3a\u8fd9\u4e2a\u65b0\u6587\u4ef6\u8d77\u4e00\u4e2a\u540d\u5b57\uff08\u4e0d\u8981\u5fd8\u4e86\u4f7f\u7528 .cpp \u4f5c\u4e3a\u540e\u7f00\u540d\uff09\uff0c\u7136\u540e\u70b9\u51fb All \u6309\u94ae\u786e\u4fdd\u6240\u6709\u7684\u6784\u5efa\u76ee\u6807\u90fd\u88ab\u52fe\u9009\u4e86\u3002\u6700\u540e\uff0c\u9009\u62e9 finish \u3002 \u73b0\u5728\uff0c\u5f53\u4f60\u7f16\u8bd1\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u4f60\u5c31\u53ef\u4ee5\u770b\u5230\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u65f6\u5217\u51fa\u4e86\u8be5\u6587\u4ef6\u7684\u540d\u5b57\u3002 For GCC/G++ users \u4f60\u53ef\u4ee5\u4ece\u547d\u4ee4\u884c\u6765\u521b\u5efa\u6587\u4ef6\u5e76\u4e3a\u5176\u6307\u5b9a\u4e00\u4e2a\u6587\u4ef6\u540d\u3002\u5728\u7f16\u8bd1\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u4f60\u5fc5\u987b\u5c06\u76f8\u5173\u7684\u6587\u4ef6\u90fd\u5305\u542b\u5728\u7f16\u8bd1\u547d\u4ee4\u4e2d\u3002 g++ main.cpp add.cpp -o main , \u5176\u4e2d\uff0c main.cpp \u548c add.cpp \u662f\u9700\u8981\u7f16\u8bd1\u7684\u6587\u4ef6\uff0c main \u5219\u662f\u8f93\u51fa\u6587\u4ef6\u7684\u6587\u4ef6\u540d\u3002 \u4e00\u4e2a\u591a\u6587\u4ef6\u7a0b\u5e8f\u7684\u4f8b\u5b50 \u5728 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u4e00\u4e2a\u5355\u6587\u4ef6\u4e0d\u80fd\u7f16\u8bd1\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } int add ( int x , int y ) { return x + y ; } \u5728\u7f16\u8bd1\u5668\u89e3\u6790\u5230\u7b2c\u4e94\u884c\u7684 add \u51fd\u6570\u8c03\u7528\u65f6\uff0c\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053 add \u4e3a\u4f55\u7269\uff0c\u56e0\u4e3a\u6211\u4eec\u8fd8\u6ca1\u6709\u5b9a\u4e49\u5b83\u5440\uff01\u4e4b\u524d\u6211\u4eec\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u65f6\u4f7f\u7528\u4e86\u4e24\u79cd\u65b9\u6848\uff0c\u8981\u4e48\u91cd\u65b0\u8c03\u6574\u51fd\u6570\u5b9a\u4e49\u7684\u987a\u5e8f\uff0c\u8981\u4e48\u4f7f\u7528 \u524d\u5411\u58f0\u660e(forward declaration) \u3002 \u73b0\u5728\uff0c\u6765\u770b\u4e00\u4e2a\u7c7b\u4f3c\u7684\u591a\u6587\u4ef6\u7a0b\u5e8f\uff1a add.cpp: 1 2 3 4 int add ( int x , int y ) { return x + y ; } main.cpp: 1 2 3 4 5 6 7 #include int main () { std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; // compile error return 0 ; } \u4f60\u7684\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5148\u7f16\u8bd1 add.cpp \u6216\u8005\u5148\u7f16\u8bd1 main.cpp \u3002\u4e0d\u8bba\u662f\u90a3\u79cd\u60c5\u51b5\uff0c\u7f16\u8bd1\u5668\u90fd\u4f1a\u62a5\u544a\u548c\u4e4b\u524d\u4e00\u6837\u7684\u7f16\u8bd1\u9519\u8bef\uff1a 1 main.cpp(5) : error C3861: 'add': identifier not found \u95ee\u9898\u7684\u539f\u56e0\u4e5f\u662f\u4e00\u6837\u7684\uff0c\u5728\u7f16\u8bd1\u5668\u89e3\u6790\u5230\u7b2c\u4e94\u884c\u7684 add \u51fd\u6570\u8c03\u7528\u65f6\uff0c\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053 add \u4e3a\u4f55\u7269\u3002 \u8fd8\u8bb0\u5f97\u5417\uff1f\u7f16\u8bd1\u5668\u662f\u5355\u72ec\u7f16\u8bd1\u5404\u4e2a\u6587\u4ef6\u7684\uff0c\u5b83\u5e76\u4e0d\u77e5\u9053\u5176\u4ed6\u4ee3\u7801\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\uff0c\u4e5f\u4e0d\u4f1a\u8bb0\u5f97\u4e4b\u524d\u7f16\u8bd1\u65f6\u9047\u5230\u7684\u4e1c\u897f\u3002\u4e5f\u8bb8\u7f16\u8bd1\u5668\u66fe\u7ecf\u9047\u5230\u8fc7\u51fd\u6570 add \u7684\u5b9a\u4e49(\u5982\u679c\u5b83\u5148\u7f16\u8bd1 add.cpp \u7684\u8bdd)\uff0c\u4f46\u5b83\u5e76\u4e0d\u4f1a\u5c06\u5176\u8bb0\u4e0b\u6765\u3002 \u7f16\u8bd1\u5668\u7684\u8fd9\u79cd\u77ed\u89c6\u548c\u5065\u5fd8\u662f\u6709\u610f\u800c\u4e3a\u4e4b\u7684\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5177\u6709\u76f8\u540c\u540d\u5b57\u7684\u51fd\u6570\u548c\u53d8\u91cf\u4e0d\u4f1a\u53d1\u751f\u51b2\u7a81\u3002\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\u4e2d\u63a2\u8ba8\u547d\u540d\u51b2\u7a81\u7684\u95ee\u9898\u3002 \u89e3\u51b3\u7684\u529e\u6cd5\u4e5f\u662f\u4e00\u6837\u7684\uff1a\u5c06 add \u51fd\u6570\u7684\u5b9a\u4e49\u79fb\u5230 main \u51fd\u6570\u4e4b\u524d\uff0c\u6216\u8005\u4f7f\u7528\u524d\u5411\u58f0\u660e\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u7531\u4e8e add \u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\uff0c\u56e0\u6b64\u8c03\u6574\u5b9a\u4e49\u987a\u5e8f\u662f\u4e0d\u53ef\u4ee5\u7684\u3002 \u56e0\u6b64\u4f7f\u7528\u524d\u5411\u58f0\u660e\u624d\u662f\u66f4\u597d\u7684\u529e\u6cd5\uff1a main.cpp (\u5305\u542b\u524d\u5411\u58f0\u660e): 1 2 3 4 5 6 7 8 9 #include int add ( int x , int y ); // \u8fd9\u884c\u662f\u5fc5\u987b\u7684\uff0c\u8fd9\u6837main.cpp\u624d\u80fd\u591f\u77e5\u9053add()\u51fd\u6570\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e86 int main () { std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } add.cpp (\u4fdd\u6301\u4e0d\u53d8): 1 2 3 4 int add ( int x , int y ) { return x + y ; } \u73b0\u5728\uff0c\u5f53\u7f16\u8bd1\u5668\u7f16\u8bd1 main.cpp \u65f6\uff0c\u5b83\u4f1a\u77e5\u6653\u6807\u8bc6\u7b26 add \u3002\u94fe\u63a5\u5668\u5219\u4f1a\u5c06 main.cpp \u6587\u4ef6\u4e2d\u7684 add \u51fd\u6570\u8c03\u7528\u548c _add.cpp_\u4e2d\u7684\u51fd\u6570\u5b9a\u4e49\u5173\u8054\u8d77\u6765\u3002 \u4f7f\u7528\u8be5\u65b9\u6cd5\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8bbf\u95ee\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u4e86\u3002 \u8bf7\u81ea\u884c\u4f7f\u7528\u524d\u5411\u58f0\u660e\u5e76\u7f16\u8bd1 add.cpp \u548c main.cpp \uff0c\u5982\u679c\u4f60\u9047\u5230\u4e86\u9519\u8bef\uff0c\u8bf7\u6ce8\u610f\u662f\u5426\u5c06 add.cpp \u52a0\u5165\u5230\u4e86\u9879\u76ee\u6216\u7f16\u8bd1\u547d\u4ee4\u4e2d\u3002 \u53d1\u751f\u9519\u8bef\u4e86\uff01 \u5728\u521b\u5efa\u591a\u6587\u4ef6\u7a0b\u5e8f\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5f88\u591a\u4e0d\u540c\u9519\u8bef\u3002\u5982\u679c\u4f60\u5c1d\u8bd5\u4e86\u4e0a\u9762\u7684\u4f8b\u5b50\u5e76\u9047\u5230\u4e86\u4e00\u4e9b\u9519\u8bef\uff0c\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u4fe1\u606f\uff1a \u5982\u679c\u4f60\u9047\u5230\u4e86 main \u4e2d\u6ca1\u6709\u5b9a\u4e49 add \u7684\u95ee\u9898\uff0c\u548c\u53ef\u80fd\u662f\u56e0\u4e3a\u5fd8\u8bb0\u5728 main.cpp \u4e2d\u5bf9 add \u51fd\u6570\u8fdb\u884c\u524d\u5411\u58f0\u660e\u3002 \u5982\u679c\u94fe\u63a5\u5668\u62a5\u544a\u4e86\u65e0\u6cd5\u627e\u5230 add \u5b9a\u4e49\u7684\u95ee\u9898\uff0c\u4f8b\u5982\uff1a 1 unresolved external symbol \"int __cdecl add(int,int)\" (?add@@YAHHH@Z) referenced in function _main \u6700\u5927\u7684\u53ef\u80fd\u6027\u662f add.cpp \u6587\u4ef6\u6ca1\u6709\u88ab\u6b63\u786e\u5730\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u3002\u5728\u7f16\u8bd1\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u770b\u5230\u7f16\u8bd1\u5668\u5217\u51fa\u4e86 main.cpp \u548c add.cpp \u3002\u5982\u679c\u4f60\u503c\u770b\u5230\u4e86 main.cpp \uff0c\u90a3\u4e48 add.cpp \u80af\u5b9a\u6ca1\u6709\u88ab\u7f16\u8bd1\u3002\u5982\u679c\u4f60\u4f7f\u7528 Visual Studio \u6216 Code:: Blocks\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u5728 Solution Explorer/project \u4e2d\u770b\u5230 add.cpp \u3002\u5982\u679c\u4f60\u6ca1\u6709\u770b\u5230\uff0c\u8bf7\u53f3\u952e\u70b9\u51fb\u9879\u76ee\u5e76\u6dfb\u52a0\u6587\u4ef6\uff0c\u7136\u540e\u5c1d\u8bd5\u91cd\u65b0\u7f16\u8bd1\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u547d\u4ee4\u884c\u7684\u65b9\u5f0f\uff0c\u8bf7\u5c06 main.cpp \u548c add.cpp \u90fd\u5305\u542b\u5728\u7f16\u8bd1\u547d\u4ee4\u4e2d\u3002 \u4f60\u662f\u4e0d\u662f\u5c06_add.cpp_ \u6dfb\u52a0\u5230\u4e86\u9519\u8bef\u7684\u9879\u76ee\u4e2d\uff1f \u4e5f\u6709\u53ef\u80fd\u662f\u8be5\u6587\u4ef6\u88ab\u8bbe\u7f6e\u4e3a\u4e0d\u7f16\u8bd1\u94fe\u63a5\u3002\u8bf7\u68c0\u67e5\u6587\u4ef6\u5c5e\u6027\u5e76\u786e\u4fdd\u5c06\u5176\u914d\u7f6e\u4e3a\u7f16\u8bd1/\u94fe\u63a5\u3002\u5728 Code:: Blocks \u4e2d\uff0c\u7f16\u8bd1\u548c\u94fe\u63a5\u5728\u4e24\u4e2a\u5355\u72ec\u7684\u9009\u62e9\u6846\u4e2d\uff0c\u8bf7\u786e\u4fdd\u5b83\u4eec\u90fd\u88ab\u9009\u4e2d\u4e86\u3002\u5bf9\u4e8e Visual Studio \u6765\u8bf4\uff0c\u8bf7\u786e\u4fdd \u201cexclude from build\u201d \u9009\u9879\u88ab\u8bbe\u7f6e\u4e3a no \u6216\u7a7a\u767d\u3002 \u4e0d\u8981\u5728 main.cpp \u4e2d #include \u201cadd.cpp\u201d \u3002\u8fd9\u4e48\u505a\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u5c06 add.cpp \u6587\u4ef6\u7684\u5185\u5bb9\u76f4\u63a5\u63d2\u5165 main.cpp \u800c\u4e0d\u662f\u5c06\u5176\u5f53\u505a\u5355\u72ec\u7684\u6587\u4ef6\u3002 \u5c0f\u7ed3 \u5f53\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u591a\u6587\u4ef6\u7a0b\u5e8f\u65f6\uff0c\u5b83\u53ef\u80fd\u4f1a\u4ee5\u4efb\u610f\u987a\u5e8f\u6765\u7f16\u8bd1\u5404\u4e2a\u6587\u4ef6\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u7f16\u8bd1\u5668\u5bf9\u4e2a\u6587\u4ef6\u7684\u7f16\u8bd1\u8fd8\u662f\u72ec\u7acb\u7684\uff0c\u5b83\u5728\u7f16\u8bd1\u67d0\u6587\u4ef6\u662f\u4e0d\u5177\u5907\u4efb\u4f55\u5176\u4ed6\u6587\u4ef6\u7684\u4fe1\u606f\u3002 \u5728\u5b66\u4e60\u5230\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u540e\uff0c\u6211\u4eec\u4f1a\u5927\u91cf\u4f7f\u7528\u591a\u6587\u4ef6\u6784\u5efa\u7a0b\u5e8f\uff0c\u6240\u4ee5\u4f60\u4ece\u73b0\u5728\u5c31\u5e94\u8be5\u597d\u597d\u638c\u63e1\u8fd9\u79cd\u65b9\u5f0f\u3002 \u63d0\u9192\u4e00\u4e0b\uff1a\u4efb\u4f55\u65b0\u521b\u5efa\u7684\u4ee3\u7801\u6587\u4ef6\u90fd\u5e94\u8be5\u88ab\u52a0\u5165\u5230\u9879\u76ee\u4e2d\uff0c\u8fd9\u6837\u5b83\u624d\u4f1a\u88ab\u7f16\u8bd1\u3002","title":"2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f"},{"location":"2-8-Programs-with-multiple-code-files/#_1","text":"\u968f\u7740\u7a0b\u5e8f\u89c4\u6a21\u4e0d\u65ad\u589e\u5927\uff0c\u7a0b\u5e8f\u901a\u5e38\u4f1a\u88ab\u5206\u5272\u4e3a\u591a\u4e2a\u6587\u4ef6\u4ee5\u65b9\u4fbf\u7ec4\u7ec7\u7a0b\u5e8f\u6216\u589e\u5f3a\u5176\u53ef\u590d\u7528\u6027\u3002\u4f7f\u7528 IDE \u53ef\u4ee5\u8ba9\u7ec4\u7ec7\u591a\u6587\u4ef6\u9879\u76ee\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\u3002\u6211\u4eec\u5df2\u7ecf\u5b66\u4e60\u4e86\u5982\u4f55\u53bb\u521b\u5efa\u5e76\u7f16\u8bd1\u4e00\u4e2a\u5355\u6587\u4ef6\u9879\u76ee\uff0c\u4e3a\u8fd9\u4e2a\u9879\u76ee\u6dfb\u52a0\u4e00\u4e2a\u6587\u4ef6\u662f\u975e\u5e38\u7b80\u5355\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u4e3a\u9879\u76ee\u6dfb\u52a0\u4ee3\u7801\u6587\u4ef6\u65f6\uff0c\u8bf7\u4f7f\u7528 .cpp \u4f5c\u4e3a\u6269\u5c55\u540d\u3002 For Visual Studio users \u5728 Visual Studio \u4e2d\uff0c\u5728\u89e3\u51b3\u65b9\u6848\u6d4f\u89c8\u5668\u7a97\u53e3(Solution Explorer)\u53f3\u952e\u70b9\u51fb Source Files \u76ee\u5f55\u5e76\u9009\u62e9 Add > New Item\u2026 . \u9009\u62e9 C++ File (.cpp) \u3002\u4e3a\u5176\u6307\u5b9a\u4e00\u4e2a\u6587\u4ef6\u540d\uff0c\u5c31\u53ef\u4ee5\u5c06\u6587\u4ef6\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u3002 \u6ce8\u610f\uff1a\u5982\u679c\u4f60\u662f\u4ece File \u83dc\u5355\u800c\u4e0d\u662f Solution Explorer \u7a97\u53e3\u521b\u5efa\u7684\u6587\u4ef6\uff0c\u90a3\u4e48\u8be5\u6587\u4ef6\u4e0d\u4f1a\u88ab\u81ea\u52a8\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u3002\u56e0\u6b64\u4f60\u5fc5\u987b\u5728\u89e3\u51b3\u65b9\u6848\u6d4f\u89c8\u5668\u7a97\u53e3(Solution Explorer)\u53f3\u952e\u70b9\u51fb Source Files \u76ee\u5f55\u5e76\u9009\u62e9 Add > Existing Item \uff0c\u7136\u540e\u9009\u62e9\u521a\u624d\u521b\u5efa\u7684\u6587\u4ef6\u3002 \u63a5\u4e0b\u6765\uff0c\u5728\u7f16\u8bd1\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u4f60\u4f1a\u53d1\u73b0\u7f16\u8bd1\u5668\u4f1a\u5c06\u8be5\u6587\u4ef6\u5217\u51fa\u6765\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5bf9\u5176\u8fdb\u884c\u7f16\u8bd1\u3002 For Code:: Blocks users \u5728 Code:: Blocks \u4e2d\uff0c\u5728 File \u83dc\u5355\u4e2d\u9009\u62e9 New > File\u2026 \u3002 \u5728 New from template \u5bf9\u8bdd\u6846\u4e2d\uff0c\u9009\u62e9 C/C++ source \u5e76\u70b9\u51fb Go \u3002 \u4f60\u53ef\u80fd\u4f1a\u6765\u5230\u4e00\u4e2a welcome to the C/C++ source file wizard \u5bf9\u8bdd\u6846\u3002\u6b64\u65f6\u70b9\u51fb Next \u3002 \u5728\u5411\u5bfc\u7684\u4e0b\u4e00\u4e2a\u9875\u9762\u4e2d\uff0c\u9009\u62e9 \u201cC++\u201d \u5e76\u70b9\u51fb Next \u3002 \u63a5\u4e0b\u6765\uff0c\u4e3a\u8fd9\u4e2a\u65b0\u6587\u4ef6\u8d77\u4e00\u4e2a\u540d\u5b57\uff08\u4e0d\u8981\u5fd8\u4e86\u4f7f\u7528 .cpp \u4f5c\u4e3a\u540e\u7f00\u540d\uff09\uff0c\u7136\u540e\u70b9\u51fb All \u6309\u94ae\u786e\u4fdd\u6240\u6709\u7684\u6784\u5efa\u76ee\u6807\u90fd\u88ab\u52fe\u9009\u4e86\u3002\u6700\u540e\uff0c\u9009\u62e9 finish \u3002 \u73b0\u5728\uff0c\u5f53\u4f60\u7f16\u8bd1\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u4f60\u5c31\u53ef\u4ee5\u770b\u5230\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u65f6\u5217\u51fa\u4e86\u8be5\u6587\u4ef6\u7684\u540d\u5b57\u3002 For GCC/G++ users \u4f60\u53ef\u4ee5\u4ece\u547d\u4ee4\u884c\u6765\u521b\u5efa\u6587\u4ef6\u5e76\u4e3a\u5176\u6307\u5b9a\u4e00\u4e2a\u6587\u4ef6\u540d\u3002\u5728\u7f16\u8bd1\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u4f60\u5fc5\u987b\u5c06\u76f8\u5173\u7684\u6587\u4ef6\u90fd\u5305\u542b\u5728\u7f16\u8bd1\u547d\u4ee4\u4e2d\u3002 g++ main.cpp add.cpp -o main , \u5176\u4e2d\uff0c main.cpp \u548c add.cpp \u662f\u9700\u8981\u7f16\u8bd1\u7684\u6587\u4ef6\uff0c main \u5219\u662f\u8f93\u51fa\u6587\u4ef6\u7684\u6587\u4ef6\u540d\u3002","title":"\u5728\u9879\u76ee\u4e2d\u6dfb\u52a0\u6587\u4ef6"},{"location":"2-8-Programs-with-multiple-code-files/#_2","text":"\u5728 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u4e00\u4e2a\u5355\u6587\u4ef6\u4e0d\u80fd\u7f16\u8bd1\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } int add ( int x , int y ) { return x + y ; } \u5728\u7f16\u8bd1\u5668\u89e3\u6790\u5230\u7b2c\u4e94\u884c\u7684 add \u51fd\u6570\u8c03\u7528\u65f6\uff0c\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053 add \u4e3a\u4f55\u7269\uff0c\u56e0\u4e3a\u6211\u4eec\u8fd8\u6ca1\u6709\u5b9a\u4e49\u5b83\u5440\uff01\u4e4b\u524d\u6211\u4eec\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u65f6\u4f7f\u7528\u4e86\u4e24\u79cd\u65b9\u6848\uff0c\u8981\u4e48\u91cd\u65b0\u8c03\u6574\u51fd\u6570\u5b9a\u4e49\u7684\u987a\u5e8f\uff0c\u8981\u4e48\u4f7f\u7528 \u524d\u5411\u58f0\u660e(forward declaration) \u3002 \u73b0\u5728\uff0c\u6765\u770b\u4e00\u4e2a\u7c7b\u4f3c\u7684\u591a\u6587\u4ef6\u7a0b\u5e8f\uff1a add.cpp: 1 2 3 4 int add ( int x , int y ) { return x + y ; } main.cpp: 1 2 3 4 5 6 7 #include int main () { std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; // compile error return 0 ; } \u4f60\u7684\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5148\u7f16\u8bd1 add.cpp \u6216\u8005\u5148\u7f16\u8bd1 main.cpp \u3002\u4e0d\u8bba\u662f\u90a3\u79cd\u60c5\u51b5\uff0c\u7f16\u8bd1\u5668\u90fd\u4f1a\u62a5\u544a\u548c\u4e4b\u524d\u4e00\u6837\u7684\u7f16\u8bd1\u9519\u8bef\uff1a 1 main.cpp(5) : error C3861: 'add': identifier not found \u95ee\u9898\u7684\u539f\u56e0\u4e5f\u662f\u4e00\u6837\u7684\uff0c\u5728\u7f16\u8bd1\u5668\u89e3\u6790\u5230\u7b2c\u4e94\u884c\u7684 add \u51fd\u6570\u8c03\u7528\u65f6\uff0c\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053 add \u4e3a\u4f55\u7269\u3002 \u8fd8\u8bb0\u5f97\u5417\uff1f\u7f16\u8bd1\u5668\u662f\u5355\u72ec\u7f16\u8bd1\u5404\u4e2a\u6587\u4ef6\u7684\uff0c\u5b83\u5e76\u4e0d\u77e5\u9053\u5176\u4ed6\u4ee3\u7801\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\uff0c\u4e5f\u4e0d\u4f1a\u8bb0\u5f97\u4e4b\u524d\u7f16\u8bd1\u65f6\u9047\u5230\u7684\u4e1c\u897f\u3002\u4e5f\u8bb8\u7f16\u8bd1\u5668\u66fe\u7ecf\u9047\u5230\u8fc7\u51fd\u6570 add \u7684\u5b9a\u4e49(\u5982\u679c\u5b83\u5148\u7f16\u8bd1 add.cpp \u7684\u8bdd)\uff0c\u4f46\u5b83\u5e76\u4e0d\u4f1a\u5c06\u5176\u8bb0\u4e0b\u6765\u3002 \u7f16\u8bd1\u5668\u7684\u8fd9\u79cd\u77ed\u89c6\u548c\u5065\u5fd8\u662f\u6709\u610f\u800c\u4e3a\u4e4b\u7684\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5177\u6709\u76f8\u540c\u540d\u5b57\u7684\u51fd\u6570\u548c\u53d8\u91cf\u4e0d\u4f1a\u53d1\u751f\u51b2\u7a81\u3002\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\u4e2d\u63a2\u8ba8\u547d\u540d\u51b2\u7a81\u7684\u95ee\u9898\u3002 \u89e3\u51b3\u7684\u529e\u6cd5\u4e5f\u662f\u4e00\u6837\u7684\uff1a\u5c06 add \u51fd\u6570\u7684\u5b9a\u4e49\u79fb\u5230 main \u51fd\u6570\u4e4b\u524d\uff0c\u6216\u8005\u4f7f\u7528\u524d\u5411\u58f0\u660e\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u7531\u4e8e add \u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\uff0c\u56e0\u6b64\u8c03\u6574\u5b9a\u4e49\u987a\u5e8f\u662f\u4e0d\u53ef\u4ee5\u7684\u3002 \u56e0\u6b64\u4f7f\u7528\u524d\u5411\u58f0\u660e\u624d\u662f\u66f4\u597d\u7684\u529e\u6cd5\uff1a main.cpp (\u5305\u542b\u524d\u5411\u58f0\u660e): 1 2 3 4 5 6 7 8 9 #include int add ( int x , int y ); // \u8fd9\u884c\u662f\u5fc5\u987b\u7684\uff0c\u8fd9\u6837main.cpp\u624d\u80fd\u591f\u77e5\u9053add()\u51fd\u6570\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e86 int main () { std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } add.cpp (\u4fdd\u6301\u4e0d\u53d8): 1 2 3 4 int add ( int x , int y ) { return x + y ; } \u73b0\u5728\uff0c\u5f53\u7f16\u8bd1\u5668\u7f16\u8bd1 main.cpp \u65f6\uff0c\u5b83\u4f1a\u77e5\u6653\u6807\u8bc6\u7b26 add \u3002\u94fe\u63a5\u5668\u5219\u4f1a\u5c06 main.cpp \u6587\u4ef6\u4e2d\u7684 add \u51fd\u6570\u8c03\u7528\u548c _add.cpp_\u4e2d\u7684\u51fd\u6570\u5b9a\u4e49\u5173\u8054\u8d77\u6765\u3002 \u4f7f\u7528\u8be5\u65b9\u6cd5\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8bbf\u95ee\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u4e86\u3002 \u8bf7\u81ea\u884c\u4f7f\u7528\u524d\u5411\u58f0\u660e\u5e76\u7f16\u8bd1 add.cpp \u548c main.cpp \uff0c\u5982\u679c\u4f60\u9047\u5230\u4e86\u9519\u8bef\uff0c\u8bf7\u6ce8\u610f\u662f\u5426\u5c06 add.cpp \u52a0\u5165\u5230\u4e86\u9879\u76ee\u6216\u7f16\u8bd1\u547d\u4ee4\u4e2d\u3002","title":"\u4e00\u4e2a\u591a\u6587\u4ef6\u7a0b\u5e8f\u7684\u4f8b\u5b50"},{"location":"2-8-Programs-with-multiple-code-files/#_3","text":"\u5728\u521b\u5efa\u591a\u6587\u4ef6\u7a0b\u5e8f\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5f88\u591a\u4e0d\u540c\u9519\u8bef\u3002\u5982\u679c\u4f60\u5c1d\u8bd5\u4e86\u4e0a\u9762\u7684\u4f8b\u5b50\u5e76\u9047\u5230\u4e86\u4e00\u4e9b\u9519\u8bef\uff0c\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u4fe1\u606f\uff1a \u5982\u679c\u4f60\u9047\u5230\u4e86 main \u4e2d\u6ca1\u6709\u5b9a\u4e49 add \u7684\u95ee\u9898\uff0c\u548c\u53ef\u80fd\u662f\u56e0\u4e3a\u5fd8\u8bb0\u5728 main.cpp \u4e2d\u5bf9 add \u51fd\u6570\u8fdb\u884c\u524d\u5411\u58f0\u660e\u3002 \u5982\u679c\u94fe\u63a5\u5668\u62a5\u544a\u4e86\u65e0\u6cd5\u627e\u5230 add \u5b9a\u4e49\u7684\u95ee\u9898\uff0c\u4f8b\u5982\uff1a 1 unresolved external symbol \"int __cdecl add(int,int)\" (?add@@YAHHH@Z) referenced in function _main \u6700\u5927\u7684\u53ef\u80fd\u6027\u662f add.cpp \u6587\u4ef6\u6ca1\u6709\u88ab\u6b63\u786e\u5730\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u3002\u5728\u7f16\u8bd1\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u770b\u5230\u7f16\u8bd1\u5668\u5217\u51fa\u4e86 main.cpp \u548c add.cpp \u3002\u5982\u679c\u4f60\u503c\u770b\u5230\u4e86 main.cpp \uff0c\u90a3\u4e48 add.cpp \u80af\u5b9a\u6ca1\u6709\u88ab\u7f16\u8bd1\u3002\u5982\u679c\u4f60\u4f7f\u7528 Visual Studio \u6216 Code:: Blocks\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u5728 Solution Explorer/project \u4e2d\u770b\u5230 add.cpp \u3002\u5982\u679c\u4f60\u6ca1\u6709\u770b\u5230\uff0c\u8bf7\u53f3\u952e\u70b9\u51fb\u9879\u76ee\u5e76\u6dfb\u52a0\u6587\u4ef6\uff0c\u7136\u540e\u5c1d\u8bd5\u91cd\u65b0\u7f16\u8bd1\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u547d\u4ee4\u884c\u7684\u65b9\u5f0f\uff0c\u8bf7\u5c06 main.cpp \u548c add.cpp \u90fd\u5305\u542b\u5728\u7f16\u8bd1\u547d\u4ee4\u4e2d\u3002 \u4f60\u662f\u4e0d\u662f\u5c06_add.cpp_ \u6dfb\u52a0\u5230\u4e86\u9519\u8bef\u7684\u9879\u76ee\u4e2d\uff1f \u4e5f\u6709\u53ef\u80fd\u662f\u8be5\u6587\u4ef6\u88ab\u8bbe\u7f6e\u4e3a\u4e0d\u7f16\u8bd1\u94fe\u63a5\u3002\u8bf7\u68c0\u67e5\u6587\u4ef6\u5c5e\u6027\u5e76\u786e\u4fdd\u5c06\u5176\u914d\u7f6e\u4e3a\u7f16\u8bd1/\u94fe\u63a5\u3002\u5728 Code:: Blocks \u4e2d\uff0c\u7f16\u8bd1\u548c\u94fe\u63a5\u5728\u4e24\u4e2a\u5355\u72ec\u7684\u9009\u62e9\u6846\u4e2d\uff0c\u8bf7\u786e\u4fdd\u5b83\u4eec\u90fd\u88ab\u9009\u4e2d\u4e86\u3002\u5bf9\u4e8e Visual Studio \u6765\u8bf4\uff0c\u8bf7\u786e\u4fdd \u201cexclude from build\u201d \u9009\u9879\u88ab\u8bbe\u7f6e\u4e3a no \u6216\u7a7a\u767d\u3002 \u4e0d\u8981\u5728 main.cpp \u4e2d #include \u201cadd.cpp\u201d \u3002\u8fd9\u4e48\u505a\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u5c06 add.cpp \u6587\u4ef6\u7684\u5185\u5bb9\u76f4\u63a5\u63d2\u5165 main.cpp \u800c\u4e0d\u662f\u5c06\u5176\u5f53\u505a\u5355\u72ec\u7684\u6587\u4ef6\u3002","title":"\u53d1\u751f\u9519\u8bef\u4e86\uff01"},{"location":"2-8-Programs-with-multiple-code-files/#_4","text":"\u5f53\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u591a\u6587\u4ef6\u7a0b\u5e8f\u65f6\uff0c\u5b83\u53ef\u80fd\u4f1a\u4ee5\u4efb\u610f\u987a\u5e8f\u6765\u7f16\u8bd1\u5404\u4e2a\u6587\u4ef6\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u7f16\u8bd1\u5668\u5bf9\u4e2a\u6587\u4ef6\u7684\u7f16\u8bd1\u8fd8\u662f\u72ec\u7acb\u7684\uff0c\u5b83\u5728\u7f16\u8bd1\u67d0\u6587\u4ef6\u662f\u4e0d\u5177\u5907\u4efb\u4f55\u5176\u4ed6\u6587\u4ef6\u7684\u4fe1\u606f\u3002 \u5728\u5b66\u4e60\u5230\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u540e\uff0c\u6211\u4eec\u4f1a\u5927\u91cf\u4f7f\u7528\u591a\u6587\u4ef6\u6784\u5efa\u7a0b\u5e8f\uff0c\u6240\u4ee5\u4f60\u4ece\u73b0\u5728\u5c31\u5e94\u8be5\u597d\u597d\u638c\u63e1\u8fd9\u79cd\u65b9\u5f0f\u3002 \u63d0\u9192\u4e00\u4e0b\uff1a\u4efb\u4f55\u65b0\u521b\u5efa\u7684\u4ee3\u7801\u6587\u4ef6\u90fd\u5e94\u8be5\u88ab\u52a0\u5165\u5230\u9879\u76ee\u4e2d\uff0c\u8fd9\u6837\u5b83\u624d\u4f1a\u88ab\u7f16\u8bd1\u3002","title":"\u5c0f\u7ed3"},{"location":"2-9-Naming-collisions-and-an-introduction-to-namespaces/","tags":["namespace"],"text":"\u60f3\u8c61\u4e00\u4e0b\uff0c\u4f60\u7b2c\u4e00\u6b21\u5f00\u8f66\u53bb\u670b\u53cb\u5bb6\uff0c\u4ed6\u7ed9\u4f60\u7684\u5730\u5740\u662f\u897f\u7ea2\u5e02\u91d1\u878d\u8857 245 \u53f7\u3002\u4f60\u5728\u53bb\u4e4b\u524d\uff0c\u767e\u5ea6\u5730\u56fe\u4e86\u4e00\u4e0b\u8fd9\u4e2a\u5730\u5740\uff0c\u7ed3\u679c\u53d1\u73b0\u897f\u7ea2\u5e02\u7adf\u7136\u6709\u4e24\u6761\u91d1\u878d\u8857\u3002\u90a3\u4e48\u4f60\u5e94\u8be5\u53bb\u90a3\u6761\u8857\u7684 245 \u53f7\u5462\uff1f\u9664\u975e\u4f60\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u4fe1\u606f\uff08\u4f8b\u5982\u4f60\u5370\u8c61\u4e2d\u4ed6\u7684\u623f\u5b50\u9760\u7740\u4e00\u6761\u6c5f\uff09\uff0c\u4e0d\u7136\u4f60\u53ea\u80fd\u6253\u7535\u8bdd\u7ed9\u670b\u53cb\u95ee\u4e2a\u6e05\u695a\u4e86\u3002\u5728\u73b0\u5b9e\u4e2d\uff0c\u5982\u679c\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\u7684\u8857\u548c\u95e8\u724c\u53f7\u4e0d\u662f\u552f\u4e00\u7684\uff0c\u90a3\u4e48\u65e0\u7591\u4f1a\u975e\u5e38\u4ee4\u4eba\u56f0\u60d1\uff0c\u4e5f\u4f1a\u964d\u4f4e\u57ce\u5e02\u7684\u8fd0\u884c\u6548\u7387\uff08\u4f8b\u5982\u5feb\u9012\u6548\u7387\uff09\u3002 \u7c7b\u4f3c\u7684\uff0cC++\u9700\u8981\u4fdd\u8bc1\u5176\u6240\u6709\u7684\u6807\u8bc6\u7b26\u90fd\u662f\u6ca1\u6709\u6b67\u4e49\u7684\u3002\u5982\u679c\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\u6709\u4e24\u4e2a\u76f8\u540c\u7684\u6807\u8bc6\u7b26\uff0c\u90a3\u4e48 \u7f16\u8bd1\u5668(compiler) \u6216\u8005 \u94fe\u63a5 \u5c06\u65e0\u6cd5\u533a\u5206\u5b83\u4eec\uff0c\u56e0\u6b64\u5fc5\u7136\u4f1a\u62a5\u544a\u9519\u8bef\u3002\u8fd9\u4e2a\u9519\u8bef\u901a\u5e38\u662f\u5173\u4e8e\u547d\u540d\u51b2\u7a81\u7684\u3002 \u547d\u540d\u51b2\u7a81\u5b9e\u4f8b a.cpp: 1 2 3 4 5 6 #include void myFcn ( int x ) { std :: cout << x ; } COPY main.cpp: 1 2 3 4 5 6 7 8 9 10 11 #include void myFcn ( int x ) { std :: cout << 2 * x ; } int main () { return 0 ; } \u5f53\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u6b64\u7a0b\u5e8f\u65f6\uff0c\u5b83\u4f1a\u72ec\u7acb\u5730\u7f16\u8bd1 a.cpp \u548c main.cpp \u4e24\u4e2a\u6587\u4ef6\uff0c\u5e76\u4e14\u4e0d\u4f1a\u6709\u4efb\u4f55\u95ee\u9898\u3002 \u4e0d\u8fc7\uff0c\u5f53\u94fe\u63a5\u5668\u5f00\u59cb\u5de5\u4f5c\u65f6\uff0c\u5b83\u4f1a\u628a a.cpp \u548c main.cpp \u4e2d\u7684\u5b9a\u4e49\u5173\u8054\u8d77\u6765\uff0c\u7136\u540e\u5b83\u5c31\u4f1a\u53d1\u73b0 myFcn \u51fd\u6570\u7684\u5b9a\u4e49\u51b2\u7a81\u4e86\u3002\u968f\u540e\u94fe\u63a5\u5668\u4fbf\u4f1a\u56e0\u4e3a\u62a5\u9519\u800c\u505c\u6b62\u5de5\u4f5c\u3002\u6ce8\u610f\uff0c\u5373\u4f7f myFcn \u59cb\u7ec8\u6ca1\u6709\u88ab\u8c03\u7528\uff0c\u9519\u8bef\u4ecd\u7136\u4f1a\u53d1\u751f\uff01 \u5927\u591a\u6570\u547d\u540d\u51b2\u7a81\u7531\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\u5f15\u8d77\uff1a \u4e00\u4e2a\u51fd\u6570\uff08\u6216\u5c40\u90e8\u53d8\u91cf\uff09\u7684\u4e24\u4e2a\uff08\u6216\u591a\u4e2a\uff09\u5b9a\u4e49\u88ab\u5b9a\u4e49\u5728\u4e86\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\uff0c\u4f46\u88ab\u7f16\u8bd1\u5230\u4e86\u540c\u6837\u7684\u7a0b\u5e8f\u4e2d\u3002\u8fd9\u6837\u4e00\u6765\u5c31\u4f1a\u5bfc\u81f4\u94fe\u63a5\u5668\u62a5\u9519\uff0c\u5982\u4e0a\u9762\u63cf\u8ff0\u7684\u90a3\u6837\uff1b \u4e00\u4e2a\u51fd\u6570\uff08\u6216\u5c40\u90e8\u53d8\u91cf\uff09\u7684\u4e24\u4e2a\uff08\u6216\u591a\u4e2a\uff09\u5b9a\u4e49\u88ab\u5f15\u5165\u4e86\u540c\u6837\u7684\u51fd\u6570\uff08\u901a\u5e38\u662f\u901a\u8fc7 #include \u5b9e\u73b0\u7684\uff09\u3002\u8fd9\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\u3002 \u968f\u7740\u7a0b\u5e8f\u89c4\u6a21\u7684\u589e\u957f\uff0c\u7a0b\u5e8f\u4e2d\u7684\u6807\u8bc6\u7b26\u4e5f\u8d8a\u6765\u8d8a\u591a\uff0c\u53d1\u751f\u547d\u540d\u51b2\u7a81\u7684\u6982\u7387\u4e5f\u968f\u4e4b\u589e\u957f\u3002\u597d\u6d88\u606f\u662f\uff0cC++ \u7a0b\u5e8f\u63d0\u4f9b\u4e86\u5f88\u591a\u7684\u673a\u5236\u6765\u907f\u514d\u547d\u540d\u51b2\u7a81\u3002\u5c40\u90e8 \u4f5c\u7528\u57df(scope) \u53ef\u4ee5\u907f\u514d\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8\u7684\u5c40\u90e8\u53d8\u91cf\u4e0e\u5176\u4ed6\u53d8\u91cf\u53d1\u751f\u547d\u540d\u51b2\u7a81\uff0c\u8fd9\u662f\u5176\u4e2d\u4e00\u79cd\u673a\u5236\u3002\u4f46\u662f\uff0c\u8be5\u673a\u5236\u4e0d\u80fd\u907f\u514d\u51fd\u6570\u540d\u7684\u51b2\u7a81\u3002\u90a3\u4e48\uff0c\u5e94\u8be5\u5982\u4f55\u907f\u514d\u51fd\u6570\u540d\u51b2\u7a81\u5462\uff1f \u547d\u540d\u7a7a\u95f4\u662f\u4ec0\u4e48? \u56de\u60f3\u4e00\u4e0b\u521a\u624d\u6709\u4e2a\u5730\u5740\u7684\u4f8b\u5b50\uff0c\u4e24\u6761\u540c\u540d\u7684\u8857\u4e4b\u6240\u4ee5\u4f1a\u5e26\u6765\u9ebb\u70e6\uff0c\u53ea\u56e0\u4e3a\u5b83\u4eec\u662f\u540c\u4e00\u4e2a\u57ce\u5e02\u4e2d\u7684\u4e24\u6761\u8857\u3002\u5982\u679c\u4f60\u60f3\u8981\u628a\u4fe1\u5bc4\u5230\u897f\u7ea2\u5e02\u7684\u91d1\u878d\u8857 209 \u548c\u8292\u679c\u5e02\u7684\u91d1\u878d\u8857 417\uff0c\u90a3\u4e48\u6839\u672c\u4e0d\u4f1a\u6709\u4efb\u4f55\u95ee\u9898\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u57fa\u4e8e\u57ce\u5e02\u521b\u5efa\u7684\u5206\u7ec4\u53ef\u4ee5\u907f\u514d\u540c\u540d\u8857\u9053\u4ea7\u751f\u7684\u6b67\u4e49\u3002\u6240\u8c13\u7684 \u547d\u540d\u7a7a\u95f4(namespace) \u5c31\u50cf\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u7684\u57ce\u5e02\u4e00\u6837\u3002 \u4e3a\u4e86\u907f\u514d\u6b67\u4e49\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u5728\u547d\u540d\u7a7a\u95f4\u4e2d\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf\u540d\u3002\u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u4e3a\u5176\u4e2d\u5b9a\u4e49\u7684\u540d\u79f0\u63d0\u4f9b\u4e00\u4e2a\u4f5c\u7528\u57df\u8303\u56f4\uff08\u79f0\u4e3a\u547d\u540d\u7a7a\u95f4\u4f5c\u7528\u57df\uff09\uff0c\u7b80\u5355\u6765\u8bf4\uff0c\u5728\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u540d\u79f0\uff0c\u4e0d\u4f1a\u548c\u5176\u4ed6\u4f5c\u7528\u57df\u4e2d\u540c\u540d\u7684\u6807\u8bc6\u7b26\u4ea7\u751f\u51b2\u7a81\u3002 \u5173\u952e\u4fe1\u606f \u5728\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u540d\u79f0\uff0c\u4e0d\u4f1a\u548c\u5176\u4ed6\u4f5c\u7528\u57df\u4e2d\u540c\u540d\u7684\u6807\u8bc6\u7b26\u4ea7\u751f\u51b2\u7a81\u3002 \u5728\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u6240\u6709\u7684\u540d\u79f0\u90fd\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u5426\u5219\u7167\u6837\u4f1a\u4ea7\u751f\u51b2\u7a81\u3002 \u547d\u540d\u7a7a\u95f4\u901a\u5e38\u7528\u4e8e\u5728\u5927\u578b\u9879\u76ee\u4e2d\u5bf9\u53d8\u91cf\u540d\u8fdb\u884c\u5206\u7ec4\uff0c\u4ee5\u6b64\u6765\u786e\u4fdd \u6807\u8bc6\u7b26(identifier) \u4e0d\u4f1a\u5728\u4e0d\u7ecf\u610f\u95f4\u4e0e\u5176\u4ed6\u6807\u8bc6\u7b26\u53d1\u751f\u51b2\u7a81\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5c06\u6240\u6709\u7684\u6570\u5b66\u51fd\u6570\u90fd\u5b9a\u4e49\u5728 math \u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u90a3\u4e48\u5b83\u4eec\u5c31\u4e0d\u4f1a\u548c\u5176\u4ed6\u7a7a\u95f4\u4e2d\u76f8\u540c\u540d\u79f0\u7684\u51fd\u6570\u53d1\u751f\u51b2\u7a81\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u5982\u4f55\u521b\u5efa\u547d\u540d\u7a7a\u95f4\u3002 \u5168\u5c40\u547d\u540d\u7a7a\u95f4 \u5728 C++ \u4e2d\uff0c\u4efb\u4f55\u6ca1\u6709\u88ab\u5b9a\u4e49\u5728\u7c7b\u3001\u51fd\u6570\u6216\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\uff0c\u90fd\u5c5e\u4e8e\u4e00\u4e2a\u9690\u542b\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u79f0\u4e3a\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff08\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a\u5168\u5c40 \u4f5c\u7528\u57df(scope) \uff09\u3002 \u5728\u6587\u7ae0\u5f00\u59cb\u7684\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570 main() \u548c\u4e24\u4e2a\u7248\u672c\u7684 myFcn() \u90fd\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u7684\u547d\u540d\u51b2\u7a81\u4e5f\u662f\u56e0\u4e3a\u4e24\u4e2a myFcn() \u90fd\u5904\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff0c\u4e5f\u5c31\u8fdd\u53cd\u4e86\u547d\u540d\u7a7a\u95f4\u4e2d\u6807\u8bc6\u7b26\u5fc5\u987b\u552f\u4e00\u7684\u89c4\u5219\u3002 \u53ea\u6709\u58f0\u660e\u548c\u5b9a\u4e49\u8bed\u53e5\u53ef\u4ee5\u51fa\u73b0\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d \u3002\u8fd9\u4e5f\u5c31\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u53d8\u91cf\uff0c\u5c3d\u7ba1\u901a\u5e38\u6211\u4eec\u5e94\u8be5\u907f\u514d\u8fd9\u4e48\u505a(\u6211\u4eec\u4f1a\u5728 6.4 - \u5168\u5c40\u53d8\u91cf \u4e00\u8282\u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u5168\u5c40\u53d8\u91cf)\u3002\u4e5f\u540c\u6837\u4e5f\u610f\u5473\u7740\uff0c \u5176\u4ed6\u7c7b\u578b\u7684\u8bed\u53e5(\u4f8b\u5982\u8868\u8fbe\u5f0f)\u4e0d\u80fd\u4f4d\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4(\u5168\u5c40\u53d8\u91cf\u7684\u521d\u59cb\u5316\u662f\u4e00\u4e2a\u4f8b\u5916)\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // handled by preprocessor // \u4ee5\u4e0b\u6240\u6709\u8bed\u53e5\u90fd\u4f4d\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4 void foo (); // \u53ef\u4ee5\u8fd9\u4e48\u505a: \u53ef\u4ee5\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u8fdb\u884c\u51fd\u6570\u7684\u524d\u5411\u58f0\u660e int x ; // \u53ef\u4ee5\u7f16\u8bd1\u4f46\u5f3a\u70c8\u5efa\u8bae\u4e0d\u8981\u8fd9\u4e48\u505a\uff1a\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\u88ab\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d int y { 5 }; // \u53ef\u4ee5\u7f16\u8bd1\u4f46\u4e0d\u63a8\u8350\u8fd9\u4e48\u505a:\u53d8\u91cf\u88ab\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u4e14\u4f7f\u7528\u521d\u59cb\u5316\u503c\u521d\u59cb\u5316 x = 5 ; // \u7f16\u8bd1\u9519\u8bef\uff1a\u53ef\u6267\u884c\u7684\u8bed\u53e5\u4e0d\u80fd\u653e\u7f6e\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d int main () // \u53ef\u4ee5\u8fd9\u4e48\u505a\uff1a\u51fd\u6570\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d { return 0 ; } void goo (); // \u53ef\u4ee5\u8fd9\u4e48\u505a: \u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u5bf9\u5176\u4ed6\u51fd\u6570\u8fdb\u884c\u524d\u5411\u58f0\u660e std \u547d\u540d\u7a7a\u95f4 C++ \u5728\u6700\u521d\u8bbe\u8ba1\u65f6\uff0c\u6240\u6709 C++ \u6807\u51c6\u5e93\u4e2d\u7684\u6807\u8bc6\u7b26\uff08\u5305\u62ec std::cin \u548c std::cout \uff09\u90fd\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u800c\u65e0\u9700\u6dfb\u52a0 std:: \u524d\u7f00\uff08\u90fd\u5c5e\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff09\u3002\u4e0d\u8fc7\uff0c\u8fd9\u6837\u4e5f\u610f\u5473\u7740\u6807\u51c6\u5e93\u4e2d\u7684\u4efb\u4f55\u6807\u8bc6\u7b26\u90fd\u53ef\u80fd\u4f1a\u548c\u5176\u4ed6\u7528\u6237\u6240\u4f7f\u7528\u7684\u6807\u8bc6\u7b26\uff08\u540c\u6837\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff09\u53d1\u751f\u51b2\u7a81\u3002\u672c\u6765\u53ef\u4ee5\u5de5\u4f5c\u7684\u4ee3\u7801\uff0c\u53ef\u80fd\u4f1a\u56e0\u4e3a #include \u4e86\u4e00\u4e2a\u6807\u51c6\u5e93\u4e2d\u7684\u65b0\u6587\u4ef6\u800c\u51fa\u73b0\u547d\u540d\u51b2\u7a81\u3002\u66f4\u574f\u7684\u662f\uff0c\u57fa\u4e8e\u8001\u7248\u672cC++\u53ef\u4ee5\u4f7f\u7528\u7684\u4ee3\u7801\uff0c\u5728\u65b0\u7248\u672c\u4e2d\u53ef\u80fd\u65e0\u6cd5\u4f7f\u7528\uff0c\u56e0\u4e3a\u65b0\u6dfb\u52a0\u5230\u6807\u51c6\u5e93\u4e2d\u7684\u6807\u8bc6\u7b26\u53ef\u80fd\u4f1a\u4e0e\u5df2\u6709\u7684\u4ee3\u7801\u53d1\u751f\u547d\u540d\u51b2\u7a81\u3002\u56e0\u6b64\uff0cC++ \u5c06\u6240\u6709\u6807\u51c6\u5e93\u4e2d\u7684\u6807\u8bc6\u7b26\u90fd\u79fb\u52a8\u5230\u4e86 std \u547d\u540d\u7a7a\u95f4\u4e2d\uff08std\u8868\u793astandard\uff0c\u5373\u6807\u51c6\uff09\u3002 \u5b9e\u9645\u4e0a\uff0c std:: cout \u7684\u540d\u5b57\u5e76\u4e0d\u662f std:: cout \u800c\u662f cout \u3002 std \u53ea\u662f\u5b83\u6240\u5904\u547d\u540d\u7a7a\u95f4\u7684 \u9650\u5b9a\u7b26(qualifier) \u7f62\u4e86\u3002\u56e0\u4e3a\uff0c cout \u5b9a\u4e49\u5728 std \u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u56e0\u6b64\u5b83\u4e0d\u4f1a\u548c\u5176\u4ed6\u4efb\u4f55\u88ab\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u7684 cout \u53d1\u751f\u51b2\u7a81\u3002 \u540c\u6837\u7684\uff0c\u5f53\u9700\u8981\u4f7f\u7528\u5b9a\u4e49\u5728\u67d0\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u65f6\uff08\u4f8b\u5982 std::cout \uff09\uff0c\u4f60\u5fc5\u987b\u544a\u8bc9\u7f16\u8bd1\u5668\u5230\u54ea\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u53bb\u67e5\u627e\u5b83\uff08\u4f8b\u5982 std \uff09\u3002 \u5173\u952e\u4fe1\u606f \u5f53\u9700\u8981\u4f7f\u7528\u5b9a\u4e49\u5728\u67d0\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u65f6\uff08\u4f8b\u5982 std::cout \uff09\uff0c\u4f60\u5fc5\u987b\u544a\u8bc9\u7f16\u8bd1\u5668\u8be5\u6807\u8bc6\u7b26\u88ab\u5b9a\u4e49\u5728\u547d\u540d\u7a7a\u95f4\u4e2d\u3002 \u6709\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5b8c\u6210\u8be5\u64cd\u4f5c\u3002 \u547d\u540d\u7a7a\u95f4\u4fee\u9970\u7b26 std:: \u6700\u76f4\u63a5\u7684\u529e\u6cd5\u5c31\u662f\u901a\u8fc7 std:: \u524d\u7f00\u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u6211\u4eec\u5e0c\u671b\u4f7f\u7528 std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684 cout \uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << \"Hello world!\" ; // when we say cout, we mean the cout defined in the std namespace return 0 ; } :: \u7b26\u53f7\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u8fd0\u7b97\u7b26\uff0c\u79f0\u4e3a \u7a7a\u95f4\u89e3\u6790\u8fd0\u7b97\u7b26(scope resolution operator) \u3002\u8be5\u8fd0\u7b97\u7b26\u5de6\u4fa7\u662f\u547d\u540d\u7a7a\u95f4\u7684\u540d\u5b57\uff0c\u53f3\u4fa7\u5219\u662f\u8be5\u7a7a\u95f4\u4e2d\u5305\u542b\u7684\u6807\u8bc6\u7b26\u3002\u5982\u679c\u5de6\u4fa7\u672a\u6307\u5b9a\u4efb\u4f55\u547d\u540d\u7a7a\u95f4\uff0c\u5219\u9ed8\u8ba4\u4e3a\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u3002 \u56e0\u6b64\uff0c std:: cout \u8868\u793a cout \u88ab\u5b9a\u4e49\u5728\u547d\u540d\u7a7a\u95f4 std \u4e2d\u3002 \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u7528 cout \u662f\u6700\u5b89\u5168\u7684\uff0c\u56e0\u4e3a\u4e0d\u5b58\u5728\u4efb\u4f55\u6b67\u4e49\uff08 cout \u662f std \u4e2d\u552f\u4e00\u7684\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u524d\u7f00\u6765\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u3002 using namespace std \u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u8fd9\u4e48\u505a \u53e6\u5916\u4e00\u79cd\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u4e2d\u6807\u8bc6\u7b26\u7684\u65b9\u5f0f\u662f\u4f7f\u7528 using \u6307\u4ee4\u8bed\u53e5\u3002\u4ee5\u4e0b\u4f8b\u5b50\u57fa\u4e8e\u8be5\u65b9\u6cd5\u4fee\u6539\u4e86\u539f \u201cHello world\u201d \u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 #include using namespace std ; // \u8be5using\u6307\u4ee4\u8bed\u53e5\u53ef\u4ee5\u4f7f\u6211\u4eec\u8bbf\u95ee\u6807\u8bc6\u7b26\u65f6\u65e0\u9700\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u524d\u7f00 int main () { cout << \"Hello world!\" ; return 0 ; } using \u6307\u4ee4\u8bed\u53e5\u53ef\u4ee5\u4f7f\u6211\u4eec\u8bbf\u95ee\u6807\u8bc6\u7b26\u65f6\u65e0\u9700\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u524d\u7f00\u3002\u56e0\u6b64\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53\u7f16\u8bd1\u5668\u9700\u8981\u786e\u5b9a cout \u7684\u5b9a\u4e49\u65f6\uff0c\u5b83\u4f1a\u5339\u914d\u5230 std:: cout \uff0c\u5728\u4f7f\u7528\u4e86 using \u6307\u4ee4\u540e\uff0c\u5c31\u53ef\u4ee5\u4ee5 cout \u8fd9\u79cd\u5f62\u5f0f\u6765\u4f7f\u7528\u8be5\u51fd\u6570\u4e86\u3002 \u5f88\u591a\u6587\u7ae0\u3001\u6559\u7a0b\u3001\u751a\u81f3\u662f\u4e00\u4e9b\u7f16\u8bd1\u5668\u4f1a\u5efa\u8bae\u5728\u7a0b\u5e8f\u7684\u5f00\u5934\u4f7f\u7528 using \u6307\u4ee4\u8bed\u53e5\u3002\u5176\u5b9e\uff0c\u8fd9\u5e76\u4e0d\u662f\u6700\u4f73\u5b9e\u8df5\uff0c\u800c\u4e14\u6781\u4e3a\u4e0d\u63a8\u8350\u4f7f\u7528\u3002 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // \u5bfc\u5165 `std::cout` \u7684\u5b9a\u4e49\u3002 using namespace std ; // \u4f7f\u5f97 std::cout \u80fd\u591f\u4ee5 \"cout\" \u65b9\u5f0f\u4f7f\u7528 int cout () // \u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u6211\u4eec\u7684cout\u51fd\u6570 { return 5 ; } int main () { cout << \"Hello, world!\" ; // \u7f16\u8bd1\u62a5\u9519\uff0c\u8fd9\u91cc\u4f7f\u7528\u7684cout\u662f\u6211\u4eec\u5b9a\u4e49\u7684\uff0c\u8fd8\u662fstd\u5b9a\u4e49\u7684\uff1f return 0 ; } \u4e0a\u9762\u7684\u4ee3\u7801\u662f\u4e0d\u80fd\u591f\u7f16\u8bd1\u7684\uff0c\u56e0\u4e3a\u6b64\u65f6\u7f16\u8bd1\u5668\u65e0\u6cd5\u533a\u5206\u6211\u4eec\u60f3\u8981\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684 cout \u8fd8\u662f std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684 cout \u3002 \u5f53\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u7528 using \u6307\u4ee4\u65f6\uff0c\u6211\u4eec\u5b9a\u4e49\u7684\u4efb\u4f55\u540d\u79f0\u90fd\u53ef\u80fd\u548c std \u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u4efb\u4f55\u540d\u79f0\u53d1\u751f\u51b2\u7a81\u3002\u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u4e5f\u8bb8\u6b64\u65f6\u5e76\u6ca1\u6709\u51b2\u7a81\uff0c\u4f46\u662f\u5728\u65b0\u7248\u672c\u7684C++\u4e2d\uff0c\u7531\u4e8e std \u4e2d\u65b0\u52a0\u5165\u4e86\u5176\u4ed6\u6807\u8bc6\u7b26\uff0c\u5219\u53ef\u80fd\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\u7684\u53d1\u751f\u3002\u8fd9\u4e5f\u662f\u5c06\u6807\u51c6\u5e93\u4e2d\u6240\u6709\u6807\u8bc6\u7b26\u90fd\u79fb\u52a8\u5230\u4e86 std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u521d\u8877\u3002 \u6ce8\u610f \u5e94\u8be5\u5728\u7a0b\u5e8f\u6216\u5934\u6587\u4ef6\u4e2d\u907f\u514d\u4f7f\u7528 using \u6307\u4ee4 (\u4f8b\u5982 using namespace std; )\uff0c\u8fd9\u4e48\u505a\u5c31\u8fdd\u80cc\u4e86\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u7684\u521d\u8877\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 \u4e2d\u4ecb\u7ecd using \u8bed\u53e5\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u3002","title":"2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4"},{"location":"2-9-Naming-collisions-and-an-introduction-to-namespaces/#_1","text":"a.cpp: 1 2 3 4 5 6 #include void myFcn ( int x ) { std :: cout << x ; } COPY main.cpp: 1 2 3 4 5 6 7 8 9 10 11 #include void myFcn ( int x ) { std :: cout << 2 * x ; } int main () { return 0 ; } \u5f53\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u6b64\u7a0b\u5e8f\u65f6\uff0c\u5b83\u4f1a\u72ec\u7acb\u5730\u7f16\u8bd1 a.cpp \u548c main.cpp \u4e24\u4e2a\u6587\u4ef6\uff0c\u5e76\u4e14\u4e0d\u4f1a\u6709\u4efb\u4f55\u95ee\u9898\u3002 \u4e0d\u8fc7\uff0c\u5f53\u94fe\u63a5\u5668\u5f00\u59cb\u5de5\u4f5c\u65f6\uff0c\u5b83\u4f1a\u628a a.cpp \u548c main.cpp \u4e2d\u7684\u5b9a\u4e49\u5173\u8054\u8d77\u6765\uff0c\u7136\u540e\u5b83\u5c31\u4f1a\u53d1\u73b0 myFcn \u51fd\u6570\u7684\u5b9a\u4e49\u51b2\u7a81\u4e86\u3002\u968f\u540e\u94fe\u63a5\u5668\u4fbf\u4f1a\u56e0\u4e3a\u62a5\u9519\u800c\u505c\u6b62\u5de5\u4f5c\u3002\u6ce8\u610f\uff0c\u5373\u4f7f myFcn \u59cb\u7ec8\u6ca1\u6709\u88ab\u8c03\u7528\uff0c\u9519\u8bef\u4ecd\u7136\u4f1a\u53d1\u751f\uff01 \u5927\u591a\u6570\u547d\u540d\u51b2\u7a81\u7531\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\u5f15\u8d77\uff1a \u4e00\u4e2a\u51fd\u6570\uff08\u6216\u5c40\u90e8\u53d8\u91cf\uff09\u7684\u4e24\u4e2a\uff08\u6216\u591a\u4e2a\uff09\u5b9a\u4e49\u88ab\u5b9a\u4e49\u5728\u4e86\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\uff0c\u4f46\u88ab\u7f16\u8bd1\u5230\u4e86\u540c\u6837\u7684\u7a0b\u5e8f\u4e2d\u3002\u8fd9\u6837\u4e00\u6765\u5c31\u4f1a\u5bfc\u81f4\u94fe\u63a5\u5668\u62a5\u9519\uff0c\u5982\u4e0a\u9762\u63cf\u8ff0\u7684\u90a3\u6837\uff1b \u4e00\u4e2a\u51fd\u6570\uff08\u6216\u5c40\u90e8\u53d8\u91cf\uff09\u7684\u4e24\u4e2a\uff08\u6216\u591a\u4e2a\uff09\u5b9a\u4e49\u88ab\u5f15\u5165\u4e86\u540c\u6837\u7684\u51fd\u6570\uff08\u901a\u5e38\u662f\u901a\u8fc7 #include \u5b9e\u73b0\u7684\uff09\u3002\u8fd9\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\u3002 \u968f\u7740\u7a0b\u5e8f\u89c4\u6a21\u7684\u589e\u957f\uff0c\u7a0b\u5e8f\u4e2d\u7684\u6807\u8bc6\u7b26\u4e5f\u8d8a\u6765\u8d8a\u591a\uff0c\u53d1\u751f\u547d\u540d\u51b2\u7a81\u7684\u6982\u7387\u4e5f\u968f\u4e4b\u589e\u957f\u3002\u597d\u6d88\u606f\u662f\uff0cC++ \u7a0b\u5e8f\u63d0\u4f9b\u4e86\u5f88\u591a\u7684\u673a\u5236\u6765\u907f\u514d\u547d\u540d\u51b2\u7a81\u3002\u5c40\u90e8 \u4f5c\u7528\u57df(scope) \u53ef\u4ee5\u907f\u514d\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8\u7684\u5c40\u90e8\u53d8\u91cf\u4e0e\u5176\u4ed6\u53d8\u91cf\u53d1\u751f\u547d\u540d\u51b2\u7a81\uff0c\u8fd9\u662f\u5176\u4e2d\u4e00\u79cd\u673a\u5236\u3002\u4f46\u662f\uff0c\u8be5\u673a\u5236\u4e0d\u80fd\u907f\u514d\u51fd\u6570\u540d\u7684\u51b2\u7a81\u3002\u90a3\u4e48\uff0c\u5e94\u8be5\u5982\u4f55\u907f\u514d\u51fd\u6570\u540d\u51b2\u7a81\u5462\uff1f","title":"\u547d\u540d\u51b2\u7a81\u5b9e\u4f8b"},{"location":"2-9-Naming-collisions-and-an-introduction-to-namespaces/#_2","text":"\u56de\u60f3\u4e00\u4e0b\u521a\u624d\u6709\u4e2a\u5730\u5740\u7684\u4f8b\u5b50\uff0c\u4e24\u6761\u540c\u540d\u7684\u8857\u4e4b\u6240\u4ee5\u4f1a\u5e26\u6765\u9ebb\u70e6\uff0c\u53ea\u56e0\u4e3a\u5b83\u4eec\u662f\u540c\u4e00\u4e2a\u57ce\u5e02\u4e2d\u7684\u4e24\u6761\u8857\u3002\u5982\u679c\u4f60\u60f3\u8981\u628a\u4fe1\u5bc4\u5230\u897f\u7ea2\u5e02\u7684\u91d1\u878d\u8857 209 \u548c\u8292\u679c\u5e02\u7684\u91d1\u878d\u8857 417\uff0c\u90a3\u4e48\u6839\u672c\u4e0d\u4f1a\u6709\u4efb\u4f55\u95ee\u9898\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u57fa\u4e8e\u57ce\u5e02\u521b\u5efa\u7684\u5206\u7ec4\u53ef\u4ee5\u907f\u514d\u540c\u540d\u8857\u9053\u4ea7\u751f\u7684\u6b67\u4e49\u3002\u6240\u8c13\u7684 \u547d\u540d\u7a7a\u95f4(namespace) \u5c31\u50cf\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u7684\u57ce\u5e02\u4e00\u6837\u3002 \u4e3a\u4e86\u907f\u514d\u6b67\u4e49\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u5728\u547d\u540d\u7a7a\u95f4\u4e2d\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf\u540d\u3002\u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u4e3a\u5176\u4e2d\u5b9a\u4e49\u7684\u540d\u79f0\u63d0\u4f9b\u4e00\u4e2a\u4f5c\u7528\u57df\u8303\u56f4\uff08\u79f0\u4e3a\u547d\u540d\u7a7a\u95f4\u4f5c\u7528\u57df\uff09\uff0c\u7b80\u5355\u6765\u8bf4\uff0c\u5728\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u540d\u79f0\uff0c\u4e0d\u4f1a\u548c\u5176\u4ed6\u4f5c\u7528\u57df\u4e2d\u540c\u540d\u7684\u6807\u8bc6\u7b26\u4ea7\u751f\u51b2\u7a81\u3002 \u5173\u952e\u4fe1\u606f \u5728\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u540d\u79f0\uff0c\u4e0d\u4f1a\u548c\u5176\u4ed6\u4f5c\u7528\u57df\u4e2d\u540c\u540d\u7684\u6807\u8bc6\u7b26\u4ea7\u751f\u51b2\u7a81\u3002 \u5728\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u6240\u6709\u7684\u540d\u79f0\u90fd\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u5426\u5219\u7167\u6837\u4f1a\u4ea7\u751f\u51b2\u7a81\u3002 \u547d\u540d\u7a7a\u95f4\u901a\u5e38\u7528\u4e8e\u5728\u5927\u578b\u9879\u76ee\u4e2d\u5bf9\u53d8\u91cf\u540d\u8fdb\u884c\u5206\u7ec4\uff0c\u4ee5\u6b64\u6765\u786e\u4fdd \u6807\u8bc6\u7b26(identifier) \u4e0d\u4f1a\u5728\u4e0d\u7ecf\u610f\u95f4\u4e0e\u5176\u4ed6\u6807\u8bc6\u7b26\u53d1\u751f\u51b2\u7a81\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5c06\u6240\u6709\u7684\u6570\u5b66\u51fd\u6570\u90fd\u5b9a\u4e49\u5728 math \u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u90a3\u4e48\u5b83\u4eec\u5c31\u4e0d\u4f1a\u548c\u5176\u4ed6\u7a7a\u95f4\u4e2d\u76f8\u540c\u540d\u79f0\u7684\u51fd\u6570\u53d1\u751f\u51b2\u7a81\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u5982\u4f55\u521b\u5efa\u547d\u540d\u7a7a\u95f4\u3002","title":"\u547d\u540d\u7a7a\u95f4\u662f\u4ec0\u4e48?"},{"location":"2-9-Naming-collisions-and-an-introduction-to-namespaces/#_3","text":"\u5728 C++ \u4e2d\uff0c\u4efb\u4f55\u6ca1\u6709\u88ab\u5b9a\u4e49\u5728\u7c7b\u3001\u51fd\u6570\u6216\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\uff0c\u90fd\u5c5e\u4e8e\u4e00\u4e2a\u9690\u542b\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u79f0\u4e3a\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff08\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a\u5168\u5c40 \u4f5c\u7528\u57df(scope) \uff09\u3002 \u5728\u6587\u7ae0\u5f00\u59cb\u7684\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570 main() \u548c\u4e24\u4e2a\u7248\u672c\u7684 myFcn() \u90fd\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u7684\u547d\u540d\u51b2\u7a81\u4e5f\u662f\u56e0\u4e3a\u4e24\u4e2a myFcn() \u90fd\u5904\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff0c\u4e5f\u5c31\u8fdd\u53cd\u4e86\u547d\u540d\u7a7a\u95f4\u4e2d\u6807\u8bc6\u7b26\u5fc5\u987b\u552f\u4e00\u7684\u89c4\u5219\u3002 \u53ea\u6709\u58f0\u660e\u548c\u5b9a\u4e49\u8bed\u53e5\u53ef\u4ee5\u51fa\u73b0\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d \u3002\u8fd9\u4e5f\u5c31\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u53d8\u91cf\uff0c\u5c3d\u7ba1\u901a\u5e38\u6211\u4eec\u5e94\u8be5\u907f\u514d\u8fd9\u4e48\u505a(\u6211\u4eec\u4f1a\u5728 6.4 - \u5168\u5c40\u53d8\u91cf \u4e00\u8282\u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u5168\u5c40\u53d8\u91cf)\u3002\u4e5f\u540c\u6837\u4e5f\u610f\u5473\u7740\uff0c \u5176\u4ed6\u7c7b\u578b\u7684\u8bed\u53e5(\u4f8b\u5982\u8868\u8fbe\u5f0f)\u4e0d\u80fd\u4f4d\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4(\u5168\u5c40\u53d8\u91cf\u7684\u521d\u59cb\u5316\u662f\u4e00\u4e2a\u4f8b\u5916)\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // handled by preprocessor // \u4ee5\u4e0b\u6240\u6709\u8bed\u53e5\u90fd\u4f4d\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4 void foo (); // \u53ef\u4ee5\u8fd9\u4e48\u505a: \u53ef\u4ee5\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u8fdb\u884c\u51fd\u6570\u7684\u524d\u5411\u58f0\u660e int x ; // \u53ef\u4ee5\u7f16\u8bd1\u4f46\u5f3a\u70c8\u5efa\u8bae\u4e0d\u8981\u8fd9\u4e48\u505a\uff1a\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\u88ab\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d int y { 5 }; // \u53ef\u4ee5\u7f16\u8bd1\u4f46\u4e0d\u63a8\u8350\u8fd9\u4e48\u505a:\u53d8\u91cf\u88ab\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u4e14\u4f7f\u7528\u521d\u59cb\u5316\u503c\u521d\u59cb\u5316 x = 5 ; // \u7f16\u8bd1\u9519\u8bef\uff1a\u53ef\u6267\u884c\u7684\u8bed\u53e5\u4e0d\u80fd\u653e\u7f6e\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d int main () // \u53ef\u4ee5\u8fd9\u4e48\u505a\uff1a\u51fd\u6570\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d { return 0 ; } void goo (); // \u53ef\u4ee5\u8fd9\u4e48\u505a: \u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u5bf9\u5176\u4ed6\u51fd\u6570\u8fdb\u884c\u524d\u5411\u58f0\u660e","title":"\u5168\u5c40\u547d\u540d\u7a7a\u95f4"},{"location":"2-9-Naming-collisions-and-an-introduction-to-namespaces/#std","text":"C++ \u5728\u6700\u521d\u8bbe\u8ba1\u65f6\uff0c\u6240\u6709 C++ \u6807\u51c6\u5e93\u4e2d\u7684\u6807\u8bc6\u7b26\uff08\u5305\u62ec std::cin \u548c std::cout \uff09\u90fd\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u800c\u65e0\u9700\u6dfb\u52a0 std:: \u524d\u7f00\uff08\u90fd\u5c5e\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff09\u3002\u4e0d\u8fc7\uff0c\u8fd9\u6837\u4e5f\u610f\u5473\u7740\u6807\u51c6\u5e93\u4e2d\u7684\u4efb\u4f55\u6807\u8bc6\u7b26\u90fd\u53ef\u80fd\u4f1a\u548c\u5176\u4ed6\u7528\u6237\u6240\u4f7f\u7528\u7684\u6807\u8bc6\u7b26\uff08\u540c\u6837\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff09\u53d1\u751f\u51b2\u7a81\u3002\u672c\u6765\u53ef\u4ee5\u5de5\u4f5c\u7684\u4ee3\u7801\uff0c\u53ef\u80fd\u4f1a\u56e0\u4e3a #include \u4e86\u4e00\u4e2a\u6807\u51c6\u5e93\u4e2d\u7684\u65b0\u6587\u4ef6\u800c\u51fa\u73b0\u547d\u540d\u51b2\u7a81\u3002\u66f4\u574f\u7684\u662f\uff0c\u57fa\u4e8e\u8001\u7248\u672cC++\u53ef\u4ee5\u4f7f\u7528\u7684\u4ee3\u7801\uff0c\u5728\u65b0\u7248\u672c\u4e2d\u53ef\u80fd\u65e0\u6cd5\u4f7f\u7528\uff0c\u56e0\u4e3a\u65b0\u6dfb\u52a0\u5230\u6807\u51c6\u5e93\u4e2d\u7684\u6807\u8bc6\u7b26\u53ef\u80fd\u4f1a\u4e0e\u5df2\u6709\u7684\u4ee3\u7801\u53d1\u751f\u547d\u540d\u51b2\u7a81\u3002\u56e0\u6b64\uff0cC++ \u5c06\u6240\u6709\u6807\u51c6\u5e93\u4e2d\u7684\u6807\u8bc6\u7b26\u90fd\u79fb\u52a8\u5230\u4e86 std \u547d\u540d\u7a7a\u95f4\u4e2d\uff08std\u8868\u793astandard\uff0c\u5373\u6807\u51c6\uff09\u3002 \u5b9e\u9645\u4e0a\uff0c std:: cout \u7684\u540d\u5b57\u5e76\u4e0d\u662f std:: cout \u800c\u662f cout \u3002 std \u53ea\u662f\u5b83\u6240\u5904\u547d\u540d\u7a7a\u95f4\u7684 \u9650\u5b9a\u7b26(qualifier) \u7f62\u4e86\u3002\u56e0\u4e3a\uff0c cout \u5b9a\u4e49\u5728 std \u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u56e0\u6b64\u5b83\u4e0d\u4f1a\u548c\u5176\u4ed6\u4efb\u4f55\u88ab\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u7684 cout \u53d1\u751f\u51b2\u7a81\u3002 \u540c\u6837\u7684\uff0c\u5f53\u9700\u8981\u4f7f\u7528\u5b9a\u4e49\u5728\u67d0\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u65f6\uff08\u4f8b\u5982 std::cout \uff09\uff0c\u4f60\u5fc5\u987b\u544a\u8bc9\u7f16\u8bd1\u5668\u5230\u54ea\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u53bb\u67e5\u627e\u5b83\uff08\u4f8b\u5982 std \uff09\u3002 \u5173\u952e\u4fe1\u606f \u5f53\u9700\u8981\u4f7f\u7528\u5b9a\u4e49\u5728\u67d0\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u65f6\uff08\u4f8b\u5982 std::cout \uff09\uff0c\u4f60\u5fc5\u987b\u544a\u8bc9\u7f16\u8bd1\u5668\u8be5\u6807\u8bc6\u7b26\u88ab\u5b9a\u4e49\u5728\u547d\u540d\u7a7a\u95f4\u4e2d\u3002 \u6709\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5b8c\u6210\u8be5\u64cd\u4f5c\u3002","title":"std \u547d\u540d\u7a7a\u95f4"},{"location":"2-9-Naming-collisions-and-an-introduction-to-namespaces/#std_1","text":"\u6700\u76f4\u63a5\u7684\u529e\u6cd5\u5c31\u662f\u901a\u8fc7 std:: \u524d\u7f00\u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u6211\u4eec\u5e0c\u671b\u4f7f\u7528 std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684 cout \uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << \"Hello world!\" ; // when we say cout, we mean the cout defined in the std namespace return 0 ; } :: \u7b26\u53f7\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u8fd0\u7b97\u7b26\uff0c\u79f0\u4e3a \u7a7a\u95f4\u89e3\u6790\u8fd0\u7b97\u7b26(scope resolution operator) \u3002\u8be5\u8fd0\u7b97\u7b26\u5de6\u4fa7\u662f\u547d\u540d\u7a7a\u95f4\u7684\u540d\u5b57\uff0c\u53f3\u4fa7\u5219\u662f\u8be5\u7a7a\u95f4\u4e2d\u5305\u542b\u7684\u6807\u8bc6\u7b26\u3002\u5982\u679c\u5de6\u4fa7\u672a\u6307\u5b9a\u4efb\u4f55\u547d\u540d\u7a7a\u95f4\uff0c\u5219\u9ed8\u8ba4\u4e3a\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u3002 \u56e0\u6b64\uff0c std:: cout \u8868\u793a cout \u88ab\u5b9a\u4e49\u5728\u547d\u540d\u7a7a\u95f4 std \u4e2d\u3002 \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u7528 cout \u662f\u6700\u5b89\u5168\u7684\uff0c\u56e0\u4e3a\u4e0d\u5b58\u5728\u4efb\u4f55\u6b67\u4e49\uff08 cout \u662f std \u4e2d\u552f\u4e00\u7684\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u524d\u7f00\u6765\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u3002","title":"\u547d\u540d\u7a7a\u95f4\u4fee\u9970\u7b26 std::"},{"location":"2-9-Naming-collisions-and-an-introduction-to-namespaces/#using-namespace-std","text":"\u53e6\u5916\u4e00\u79cd\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u4e2d\u6807\u8bc6\u7b26\u7684\u65b9\u5f0f\u662f\u4f7f\u7528 using \u6307\u4ee4\u8bed\u53e5\u3002\u4ee5\u4e0b\u4f8b\u5b50\u57fa\u4e8e\u8be5\u65b9\u6cd5\u4fee\u6539\u4e86\u539f \u201cHello world\u201d \u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 #include using namespace std ; // \u8be5using\u6307\u4ee4\u8bed\u53e5\u53ef\u4ee5\u4f7f\u6211\u4eec\u8bbf\u95ee\u6807\u8bc6\u7b26\u65f6\u65e0\u9700\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u524d\u7f00 int main () { cout << \"Hello world!\" ; return 0 ; } using \u6307\u4ee4\u8bed\u53e5\u53ef\u4ee5\u4f7f\u6211\u4eec\u8bbf\u95ee\u6807\u8bc6\u7b26\u65f6\u65e0\u9700\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u524d\u7f00\u3002\u56e0\u6b64\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53\u7f16\u8bd1\u5668\u9700\u8981\u786e\u5b9a cout \u7684\u5b9a\u4e49\u65f6\uff0c\u5b83\u4f1a\u5339\u914d\u5230 std:: cout \uff0c\u5728\u4f7f\u7528\u4e86 using \u6307\u4ee4\u540e\uff0c\u5c31\u53ef\u4ee5\u4ee5 cout \u8fd9\u79cd\u5f62\u5f0f\u6765\u4f7f\u7528\u8be5\u51fd\u6570\u4e86\u3002 \u5f88\u591a\u6587\u7ae0\u3001\u6559\u7a0b\u3001\u751a\u81f3\u662f\u4e00\u4e9b\u7f16\u8bd1\u5668\u4f1a\u5efa\u8bae\u5728\u7a0b\u5e8f\u7684\u5f00\u5934\u4f7f\u7528 using \u6307\u4ee4\u8bed\u53e5\u3002\u5176\u5b9e\uff0c\u8fd9\u5e76\u4e0d\u662f\u6700\u4f73\u5b9e\u8df5\uff0c\u800c\u4e14\u6781\u4e3a\u4e0d\u63a8\u8350\u4f7f\u7528\u3002 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // \u5bfc\u5165 `std::cout` \u7684\u5b9a\u4e49\u3002 using namespace std ; // \u4f7f\u5f97 std::cout \u80fd\u591f\u4ee5 \"cout\" \u65b9\u5f0f\u4f7f\u7528 int cout () // \u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u6211\u4eec\u7684cout\u51fd\u6570 { return 5 ; } int main () { cout << \"Hello, world!\" ; // \u7f16\u8bd1\u62a5\u9519\uff0c\u8fd9\u91cc\u4f7f\u7528\u7684cout\u662f\u6211\u4eec\u5b9a\u4e49\u7684\uff0c\u8fd8\u662fstd\u5b9a\u4e49\u7684\uff1f return 0 ; } \u4e0a\u9762\u7684\u4ee3\u7801\u662f\u4e0d\u80fd\u591f\u7f16\u8bd1\u7684\uff0c\u56e0\u4e3a\u6b64\u65f6\u7f16\u8bd1\u5668\u65e0\u6cd5\u533a\u5206\u6211\u4eec\u60f3\u8981\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684 cout \u8fd8\u662f std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684 cout \u3002 \u5f53\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u7528 using \u6307\u4ee4\u65f6\uff0c\u6211\u4eec\u5b9a\u4e49\u7684\u4efb\u4f55\u540d\u79f0\u90fd\u53ef\u80fd\u548c std \u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u4efb\u4f55\u540d\u79f0\u53d1\u751f\u51b2\u7a81\u3002\u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u4e5f\u8bb8\u6b64\u65f6\u5e76\u6ca1\u6709\u51b2\u7a81\uff0c\u4f46\u662f\u5728\u65b0\u7248\u672c\u7684C++\u4e2d\uff0c\u7531\u4e8e std \u4e2d\u65b0\u52a0\u5165\u4e86\u5176\u4ed6\u6807\u8bc6\u7b26\uff0c\u5219\u53ef\u80fd\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\u7684\u53d1\u751f\u3002\u8fd9\u4e5f\u662f\u5c06\u6807\u51c6\u5e93\u4e2d\u6240\u6709\u6807\u8bc6\u7b26\u90fd\u79fb\u52a8\u5230\u4e86 std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u521d\u8877\u3002 \u6ce8\u610f \u5e94\u8be5\u5728\u7a0b\u5e8f\u6216\u5934\u6587\u4ef6\u4e2d\u907f\u514d\u4f7f\u7528 using \u6307\u4ee4 (\u4f8b\u5982 using namespace std; )\uff0c\u8fd9\u4e48\u505a\u5c31\u8fdd\u80cc\u4e86\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u7684\u521d\u8877\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 \u4e2d\u4ecb\u7ecd using \u8bed\u53e5\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u3002","title":"using namespace std \u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u8fd9\u4e48\u505a"},{"location":"2-x-Chapter-2-summary-and-quiz/","tags":["summary"],"text":"\u7ae0\u8282\u56de\u987e \u51fd\u6570 \u662f\u4e00\u7cfb\u5217\u53ef\u4ee5\u590d\u7528\u7684\u8bed\u53e5\uff0c\u7528\u6765\u5b8c\u6210\u67d0\u4e2a\u7279\u5b9a\u7684\u4efb\u52a1\u3002\u7528\u6237\u81ea\u5df1\u7f16\u5199\u7684\u51fd\u6570\u79f0\u4e3a\u81ea\u5b9a\u4e49\u51fd\u6570\u3002 \u51fd\u6570\u8c03\u7528 \u662f\u4e00\u79cd\u8868\u8fbe\u5f0f\uff0c\u7528\u4e8e\u544a\u8bc9CPU\u53bb\u6267\u884c\u4e00\u4e2a\u51fd\u6570\u3002\u6267\u884c\u51fd\u6570\u8c03\u7528\u7684\u51fd\u6570\uff0c\u79f0\u4e3a \u4e3b\u8c03\u51fd\u6570 \uff0c\u88ab\u8c03\u7528\u7684\u51fd\u6570\u79f0\u4e3a \u88ab\u8c03\u51fd\u6570 \u3002\u5728\u8c03\u7528\u51fd\u6570\u7684\u65f6\u5019\uff0c\u4e0d\u8981\u5fd8\u8bb0\u51fd\u6570\u540d\u540e\u9762\u7684\u62ec\u53f7\u3002 \u51fd\u6570\u5b9a\u4e49\u65f6\u4f7f\u7528\u7684\u82b1\u62ec\u53f7\u548c\u548c\u8bed\u53e5\u79f0\u4e3a \u51fd\u6570\u4f53 \u3002 \u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u79f0\u4e3a \u5e26\u8fd4\u56de\u503c\u7684\u51fd\u6570 \u3002\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u6307\u660e\u4e86\u8fd4\u56de\u503c\u7684\u5177\u4f53\u7c7b\u578b\u3002 return \u8bed\u53e5\u6307\u5b9a\u4e86\u8981\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u7684\u5177\u4f53\u503c\u3002 \u8fd4\u56de\u503c\u662f\u901a\u8fc7 \u62f7\u8d1d \u7684\u65b9\u5f0f\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u7684\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a \u6309\u503c\u8fd4\u56de \u3002\u5982\u679c\u4ee5\u975e\u7a7a\u51fd\u6570\u6ca1\u6709\u80fd\u591f\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a main \u51fd\u6570\u7684\u8fd4\u56de\u503c\u79f0\u4e3a\u7a0b\u5e8f\u7684\u72b6\u6001\u7801\u3002\u7a0b\u5e8f\u901a\u8fc7\u8fd4\u56de\u7684\u72b6\u6001\u7801\u5411\u64cd\u4f5c\u7cfb\u7edf\u8868\u660e\u7a0b\u5e8f\u662f\u5426\u6210\u529f\u6267\u884c\u3002\u8fd4\u56de\u503c\u4e3a 0 \u65f6\u8868\u793a\u6210\u529f\uff0c\u8fd4\u56de\u6b63\u6570\u5219\u8868\u793a\u6267\u884c\u5931\u8d25\u3002 \u8df5\u884c DRY \u7f16\u7a0b\u2014\u2014\u201d\u5373\u4e0d\u8981\u91cd\u590d\u4f60\u505a\u8fc7\u7684\u4e8b\u201c\u3002\u4f7f\u7528\u53d8\u91cf\u548c\u51fd\u6570\u6765\u907f\u514d\u5197\u4f59\u4ee3\u7801\u3002 \u8fd4\u56de\u503c\u7c7b\u578b\u4e3a void \u7684\u51fd\u6570\uff08 2.3 - void \u51fd\u6570 \uff09\uff0c\u4e0d\u9700\u8981\u8fd4\u56de\u4efb\u4f55\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u8fd9\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u79f0\u4e3a void \u51fd\u6570 \u6216\u65e0\u8fd4\u56de\u503c\u7684\u51fd\u6570\u3002Void \u51fd\u6570\u4e0d\u80fd\u88ab\u4f7f\u7528\u5728\u9700\u8981\u8fd4\u56de\u503c\u7684\u573a\u5408\u3002 return \u8bed\u53e5\u5982\u679c\u4e0d\u662f\u51fd\u6570\u7684\u6700\u540e\u4e00\u6761\u8bed\u53e5\uff0c\u5219\u79f0\u4e3a \u63d0\u524d\u8fd4\u56de \u3002 return \u8bed\u53e5\u4f1a\u5bfc\u81f4\u51fd\u6570\u63d0\u524d\u7ec8\u6b62\u5e76\u7acb\u5373\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570\u3002 \u51fd\u6570\u7684 \u5f62\u53c2 \u4e5f\u662f\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\uff0c\u5b83\u7684\u503c\u7531\u4e3b\u8c03\u51fd\u6570\u63d0\u4f9b\u3002\u51fd\u6570\u7684 \u5b9e\u53c2 \u6307\u7684\u662f\u5c31\u662f\u7531\u4e3b\u8c03\u51fd\u6570\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u5177\u4f53\u503c\u3002\u5f53\u5b9e\u53c2\u901a\u8fc7\u62f7\u8d1d\u7684\u65b9\u5f0f\u4f20\u9012\u7ed9\u5f62\u53c2\u65f6\uff0c\u79f0\u4e3a \u6309\u503c\u4f20\u9012(pass by value) \u3002 C++ \u5e76\u6ca1\u6709\u5b9a\u4e49\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u5bf9\u5b9e\u53c2\u7684\u6c42\u503c\u662f\u4ece\u5de6\u5411\u53f3\u8fd8\u662f\u4ece\u53f3\u5411\u5de6\u3002 \u51fd\u6570\u7684\u5f62\u53c2\u548c\u5b9a\u4e49\u5728\u51fd\u6570\u4f53\u5185\u7684\u53d8\u91cf\u79f0\u4e3a\u5c40\u90e8\u53d8\u91cf\u3002\u53d8\u91cf\u5b58\u5728\u7684\u65f6\u95f4\u8303\u56f4\u79f0\u4e3a\u5176\u58f0\u660e\u5468\u671f\u3002\u53d8\u91cf\u5728 \u8fd0\u884c\u65f6 \uff08\u5373\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u5019\uff09\u88ab\u521b\u5efa\u548c\u9500\u6bc1\u3002\u53d8\u91cf\u7684 \u4f5c\u7528\u57df \u51b3\u5b9a\u4e86\u5b83\u5728\u54ea\u91cc\u53ef\u4ee5\u88ab\u8bbf\u95ee\uff0c\u5f53\u53d8\u91cf\u53ef\u4ee5\u88ab\u8bbf\u95ee\u65f6\uff0c\u6211\u4eec\u79f0\u5176\u5728\u4f5c\u7528\u57df\u5185\u3002\u5f53\u5b83\u4e0d\u53ef\u4ee5\u88ab\u8bbf\u95ee\u65f6\uff0c\u6211\u4eec\u79f0\u5176\u4e3a\u4e0d\u5728\u4f5c\u7528\u57df\u3002\u4f5c\u7528\u57df\u662f\u4e00\u4e2a \u7f16\u8bd1\u65f6 \u5c5e\u6027\uff0c\u5373\u5728\u7f16\u8bd1\u65f6\u5c31\u4f1a\u5bf9\u5176\u8fdb\u884c\u68c0\u67e5\u3002 \u7a7a\u767d\u7b26\u6307\u7684\u662f\u54ea\u4e9b\u7528\u6765\u6392\u7248\u7684\u5b57\u7b26\uff0c\u5728C++\u4e2d\uff0c\u5305\u62ec\u7a7a\u683c\u3001\u5236\u8868\u7b26\u548c\u6362\u884c\u3002 \u524d\u5411\u58f0\u660e ( 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 )\u4f7f\u6211\u4eec\u53ef\u4ee5\u5728\u5b9a\u4e49\u6807\u8bc6\u7b26\u4e4b\u524d\uff0c\u544a\u77e5\u7f16\u8bd1\u5668\u8be5\u6807\u8bc6\u7b26\u5b58\u5728\u3002\u7f16\u5199\u51fd\u6570\u7684\u524d\u5411\u58f0\u660e\uff0c\u9700\u8981\u4f7f\u7528\u51fd\u6570\u539f\u578b\uff0c\u5305\u62ec\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u3001\u51fd\u6570\u540d\u548c\u5f62\u53c2\uff0c\u4f46\u4e0d\u9700\u8981\u51fd\u6570\u4f53\u3002 \u5b9a\u4e49 ( 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 )\u5b9e\u9645\u4e0a\u5b9e\u73b0\u4e86\uff08\u5bf9\u4e8e\u51fd\u6570\u548c\u7c7b\u578b\u800c\u8a00\uff09\u6216 \u5b9e\u4f8b\u5316(instantiated) \u4e86\u4e00\u4e2a\u6807\u8bc6\u7b26\u3002 \u58f0\u660e \u5219\u662f\u4e00\u6761\u8bed\u53e5\uff0c\u7528\u4e8e\u544a\u77e5\u7f16\u8bd1\u5668\u6807\u8bc6\u7b26\u662f\u5b58\u5728\u7684\u3002\u5728 C++ \u4e2d\uff0c \u6240\u6709\u7684\u5b9a\u4e49\u90fd\u662f\u58f0\u660e \u3002 \u7eaf\u58f0\u660e \u6307\u7684\u662f\u4e0d\u5177\u6709\u5b9a\u4e49\u529f\u80fd\u7684\u58f0\u660e\uff08\u4f8b\u5982\u51fd\u6570\u539f\u578b\uff09\u3002 \u5927\u591a\u6570\u5177\u6709\u4e00\u5b9a\u89c4\u6a21\u7684\u7a0b\u5e8f\u90fd\u5305\u542b\u591a\u4e2a\u6587\u4ef6\u3002\uff08 2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f \uff09 \u5982\u679c\u4e24\u4e2a\u6807\u8bc6\u7b26\u88ab\u5f15\u5165\u4e86\u540c\u4e00\u4e2a\u7a0b\u5e8f\u4f7f\u5f97\u7f16\u8bd1\u5668\u6216\u94fe\u63a5\u5668\u65e0\u6cd5\u5bf9\u5176\u8fdb\u884c\u533a\u5206\uff0c\u5219\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u6216\u94fe\u63a5\u5668\u62a5\u544a \u547d\u540d\u51b2\u7a81 \u3002 \u547d\u540d\u7a7a\u95f4 \uff08 2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 \uff09\u786e\u4fdd\u6240\u6709\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u90fd\u662f\u72ec\u4e00\u65e0\u4e8c\u7684\u3002 std \u5c31\u5c5e\u4e8e\u547d\u540d\u7a7a\u95f4\u7684\u4e00\u79cd \u9884\u5904\u7406\u5668\uff08 2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb \uff09\u662f\u4e00\u79cd\u7528\u4e8e\u5728\u4ee3\u7801\u7f16\u8bd1\u524d\u5bf9\u6587\u4ef6\u8fdb\u884c\u9884\u5904\u7406\u7684\u7a0b\u5e8f\u3002 \u9884\u5904\u7406\u5668\u6307\u4ee4 \u4ee5 # \u5f00\u5934\uff0c\u4ee5\u6362\u884c\u7ed3\u5c3e\u3002 \u5b8f \u6307\u7684\u662f\u4e00\u6761\u5c06\u8f93\u5165\u6587\u672c\u5982\u4f55\u66ff\u6362\u4e3a\u8f93\u51fa\u6587\u672c\u7684\u89c4\u5219\u3002 \u5934\u6587\u4ef6\uff08 2.11 - \u5934\u6587\u4ef6 \uff09\u6307\u7684\u662f\u5305\u542b\u4ee3\u7801\u58f0\u660e\u7684\u6587\u4ef6\u3002\u5f53\u4f7f\u7528 #include \u6307\u4ee4\u65f6\uff0c\u5b83\u4f1a\u628a\u5934\u6587\u4ef6\u7684\u5185\u5bb9\u66ff\u6362\u5230\u8be5\u6307\u4ee4\u7684\u4f4d\u7f6e\u3002\u5728\u5305\u542b\u5934\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u5c16\u62ec\u53f7\u6765\u5904\u7406\u7cfb\u7edf\u5934\u6587\u4ef6\uff08\u4f8b\u5982C++\u6807\u51c6\u5e93\u7684\u5934\u6587\u4ef6\uff09\uff0c\u4f7f\u7528\u53cc\u5f15\u53f7\u6765\u5904\u7406\u7528\u6237\u81ea\u5b9a\u4e49\u5934\u6587\u4ef6\u3002\u5f53\u4f7f\u7528\u7cfb\u7edf\u63d0\u4f9b\u7684\u5934\u6587\u4ef6\u65f6\uff0c\u4f7f\u7528\u4e0d\u5e26 .h \u540e\u7f00\u7684\u7248\u672c\uff08\u5982\u679c\u5b58\u5728\u7684\u8bdd\uff09 \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e\uff08 2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e ]\uff09\u53ef\u4ee5\u907f\u514d\u5934\u6587\u4ef6\u88ab\u591a\u6b21\u5305\u542b\u5230\u540c\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u4f46\u662f\u4e0d\u4f1a\u907f\u514d\u5b83\u4eec\u88ab\u5305\u542b\u5230\u591a\u4e2a\u6587\u4ef6\u4e2d\uff08\u6bcf\u4e2a\u6587\u4ef6\u53ea\u80fd\u5305\u542b\u4e00\u6b21\uff09\u3002","title":"2.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570\u4e0e\u6587\u4ef6"},{"location":"2-x-Chapter-2-summary-and-quiz/#_1","text":"\u51fd\u6570 \u662f\u4e00\u7cfb\u5217\u53ef\u4ee5\u590d\u7528\u7684\u8bed\u53e5\uff0c\u7528\u6765\u5b8c\u6210\u67d0\u4e2a\u7279\u5b9a\u7684\u4efb\u52a1\u3002\u7528\u6237\u81ea\u5df1\u7f16\u5199\u7684\u51fd\u6570\u79f0\u4e3a\u81ea\u5b9a\u4e49\u51fd\u6570\u3002 \u51fd\u6570\u8c03\u7528 \u662f\u4e00\u79cd\u8868\u8fbe\u5f0f\uff0c\u7528\u4e8e\u544a\u8bc9CPU\u53bb\u6267\u884c\u4e00\u4e2a\u51fd\u6570\u3002\u6267\u884c\u51fd\u6570\u8c03\u7528\u7684\u51fd\u6570\uff0c\u79f0\u4e3a \u4e3b\u8c03\u51fd\u6570 \uff0c\u88ab\u8c03\u7528\u7684\u51fd\u6570\u79f0\u4e3a \u88ab\u8c03\u51fd\u6570 \u3002\u5728\u8c03\u7528\u51fd\u6570\u7684\u65f6\u5019\uff0c\u4e0d\u8981\u5fd8\u8bb0\u51fd\u6570\u540d\u540e\u9762\u7684\u62ec\u53f7\u3002 \u51fd\u6570\u5b9a\u4e49\u65f6\u4f7f\u7528\u7684\u82b1\u62ec\u53f7\u548c\u548c\u8bed\u53e5\u79f0\u4e3a \u51fd\u6570\u4f53 \u3002 \u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u79f0\u4e3a \u5e26\u8fd4\u56de\u503c\u7684\u51fd\u6570 \u3002\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u6307\u660e\u4e86\u8fd4\u56de\u503c\u7684\u5177\u4f53\u7c7b\u578b\u3002 return \u8bed\u53e5\u6307\u5b9a\u4e86\u8981\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u7684\u5177\u4f53\u503c\u3002 \u8fd4\u56de\u503c\u662f\u901a\u8fc7 \u62f7\u8d1d \u7684\u65b9\u5f0f\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u7684\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a \u6309\u503c\u8fd4\u56de \u3002\u5982\u679c\u4ee5\u975e\u7a7a\u51fd\u6570\u6ca1\u6709\u80fd\u591f\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a main \u51fd\u6570\u7684\u8fd4\u56de\u503c\u79f0\u4e3a\u7a0b\u5e8f\u7684\u72b6\u6001\u7801\u3002\u7a0b\u5e8f\u901a\u8fc7\u8fd4\u56de\u7684\u72b6\u6001\u7801\u5411\u64cd\u4f5c\u7cfb\u7edf\u8868\u660e\u7a0b\u5e8f\u662f\u5426\u6210\u529f\u6267\u884c\u3002\u8fd4\u56de\u503c\u4e3a 0 \u65f6\u8868\u793a\u6210\u529f\uff0c\u8fd4\u56de\u6b63\u6570\u5219\u8868\u793a\u6267\u884c\u5931\u8d25\u3002 \u8df5\u884c DRY \u7f16\u7a0b\u2014\u2014\u201d\u5373\u4e0d\u8981\u91cd\u590d\u4f60\u505a\u8fc7\u7684\u4e8b\u201c\u3002\u4f7f\u7528\u53d8\u91cf\u548c\u51fd\u6570\u6765\u907f\u514d\u5197\u4f59\u4ee3\u7801\u3002 \u8fd4\u56de\u503c\u7c7b\u578b\u4e3a void \u7684\u51fd\u6570\uff08 2.3 - void \u51fd\u6570 \uff09\uff0c\u4e0d\u9700\u8981\u8fd4\u56de\u4efb\u4f55\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u8fd9\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u79f0\u4e3a void \u51fd\u6570 \u6216\u65e0\u8fd4\u56de\u503c\u7684\u51fd\u6570\u3002Void \u51fd\u6570\u4e0d\u80fd\u88ab\u4f7f\u7528\u5728\u9700\u8981\u8fd4\u56de\u503c\u7684\u573a\u5408\u3002 return \u8bed\u53e5\u5982\u679c\u4e0d\u662f\u51fd\u6570\u7684\u6700\u540e\u4e00\u6761\u8bed\u53e5\uff0c\u5219\u79f0\u4e3a \u63d0\u524d\u8fd4\u56de \u3002 return \u8bed\u53e5\u4f1a\u5bfc\u81f4\u51fd\u6570\u63d0\u524d\u7ec8\u6b62\u5e76\u7acb\u5373\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570\u3002 \u51fd\u6570\u7684 \u5f62\u53c2 \u4e5f\u662f\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\uff0c\u5b83\u7684\u503c\u7531\u4e3b\u8c03\u51fd\u6570\u63d0\u4f9b\u3002\u51fd\u6570\u7684 \u5b9e\u53c2 \u6307\u7684\u662f\u5c31\u662f\u7531\u4e3b\u8c03\u51fd\u6570\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u5177\u4f53\u503c\u3002\u5f53\u5b9e\u53c2\u901a\u8fc7\u62f7\u8d1d\u7684\u65b9\u5f0f\u4f20\u9012\u7ed9\u5f62\u53c2\u65f6\uff0c\u79f0\u4e3a \u6309\u503c\u4f20\u9012(pass by value) \u3002 C++ \u5e76\u6ca1\u6709\u5b9a\u4e49\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u5bf9\u5b9e\u53c2\u7684\u6c42\u503c\u662f\u4ece\u5de6\u5411\u53f3\u8fd8\u662f\u4ece\u53f3\u5411\u5de6\u3002 \u51fd\u6570\u7684\u5f62\u53c2\u548c\u5b9a\u4e49\u5728\u51fd\u6570\u4f53\u5185\u7684\u53d8\u91cf\u79f0\u4e3a\u5c40\u90e8\u53d8\u91cf\u3002\u53d8\u91cf\u5b58\u5728\u7684\u65f6\u95f4\u8303\u56f4\u79f0\u4e3a\u5176\u58f0\u660e\u5468\u671f\u3002\u53d8\u91cf\u5728 \u8fd0\u884c\u65f6 \uff08\u5373\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u5019\uff09\u88ab\u521b\u5efa\u548c\u9500\u6bc1\u3002\u53d8\u91cf\u7684 \u4f5c\u7528\u57df \u51b3\u5b9a\u4e86\u5b83\u5728\u54ea\u91cc\u53ef\u4ee5\u88ab\u8bbf\u95ee\uff0c\u5f53\u53d8\u91cf\u53ef\u4ee5\u88ab\u8bbf\u95ee\u65f6\uff0c\u6211\u4eec\u79f0\u5176\u5728\u4f5c\u7528\u57df\u5185\u3002\u5f53\u5b83\u4e0d\u53ef\u4ee5\u88ab\u8bbf\u95ee\u65f6\uff0c\u6211\u4eec\u79f0\u5176\u4e3a\u4e0d\u5728\u4f5c\u7528\u57df\u3002\u4f5c\u7528\u57df\u662f\u4e00\u4e2a \u7f16\u8bd1\u65f6 \u5c5e\u6027\uff0c\u5373\u5728\u7f16\u8bd1\u65f6\u5c31\u4f1a\u5bf9\u5176\u8fdb\u884c\u68c0\u67e5\u3002 \u7a7a\u767d\u7b26\u6307\u7684\u662f\u54ea\u4e9b\u7528\u6765\u6392\u7248\u7684\u5b57\u7b26\uff0c\u5728C++\u4e2d\uff0c\u5305\u62ec\u7a7a\u683c\u3001\u5236\u8868\u7b26\u548c\u6362\u884c\u3002 \u524d\u5411\u58f0\u660e ( 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 )\u4f7f\u6211\u4eec\u53ef\u4ee5\u5728\u5b9a\u4e49\u6807\u8bc6\u7b26\u4e4b\u524d\uff0c\u544a\u77e5\u7f16\u8bd1\u5668\u8be5\u6807\u8bc6\u7b26\u5b58\u5728\u3002\u7f16\u5199\u51fd\u6570\u7684\u524d\u5411\u58f0\u660e\uff0c\u9700\u8981\u4f7f\u7528\u51fd\u6570\u539f\u578b\uff0c\u5305\u62ec\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u3001\u51fd\u6570\u540d\u548c\u5f62\u53c2\uff0c\u4f46\u4e0d\u9700\u8981\u51fd\u6570\u4f53\u3002 \u5b9a\u4e49 ( 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 )\u5b9e\u9645\u4e0a\u5b9e\u73b0\u4e86\uff08\u5bf9\u4e8e\u51fd\u6570\u548c\u7c7b\u578b\u800c\u8a00\uff09\u6216 \u5b9e\u4f8b\u5316(instantiated) \u4e86\u4e00\u4e2a\u6807\u8bc6\u7b26\u3002 \u58f0\u660e \u5219\u662f\u4e00\u6761\u8bed\u53e5\uff0c\u7528\u4e8e\u544a\u77e5\u7f16\u8bd1\u5668\u6807\u8bc6\u7b26\u662f\u5b58\u5728\u7684\u3002\u5728 C++ \u4e2d\uff0c \u6240\u6709\u7684\u5b9a\u4e49\u90fd\u662f\u58f0\u660e \u3002 \u7eaf\u58f0\u660e \u6307\u7684\u662f\u4e0d\u5177\u6709\u5b9a\u4e49\u529f\u80fd\u7684\u58f0\u660e\uff08\u4f8b\u5982\u51fd\u6570\u539f\u578b\uff09\u3002 \u5927\u591a\u6570\u5177\u6709\u4e00\u5b9a\u89c4\u6a21\u7684\u7a0b\u5e8f\u90fd\u5305\u542b\u591a\u4e2a\u6587\u4ef6\u3002\uff08 2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f \uff09 \u5982\u679c\u4e24\u4e2a\u6807\u8bc6\u7b26\u88ab\u5f15\u5165\u4e86\u540c\u4e00\u4e2a\u7a0b\u5e8f\u4f7f\u5f97\u7f16\u8bd1\u5668\u6216\u94fe\u63a5\u5668\u65e0\u6cd5\u5bf9\u5176\u8fdb\u884c\u533a\u5206\uff0c\u5219\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u6216\u94fe\u63a5\u5668\u62a5\u544a \u547d\u540d\u51b2\u7a81 \u3002 \u547d\u540d\u7a7a\u95f4 \uff08 2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 \uff09\u786e\u4fdd\u6240\u6709\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u90fd\u662f\u72ec\u4e00\u65e0\u4e8c\u7684\u3002 std \u5c31\u5c5e\u4e8e\u547d\u540d\u7a7a\u95f4\u7684\u4e00\u79cd \u9884\u5904\u7406\u5668\uff08 2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb \uff09\u662f\u4e00\u79cd\u7528\u4e8e\u5728\u4ee3\u7801\u7f16\u8bd1\u524d\u5bf9\u6587\u4ef6\u8fdb\u884c\u9884\u5904\u7406\u7684\u7a0b\u5e8f\u3002 \u9884\u5904\u7406\u5668\u6307\u4ee4 \u4ee5 # \u5f00\u5934\uff0c\u4ee5\u6362\u884c\u7ed3\u5c3e\u3002 \u5b8f \u6307\u7684\u662f\u4e00\u6761\u5c06\u8f93\u5165\u6587\u672c\u5982\u4f55\u66ff\u6362\u4e3a\u8f93\u51fa\u6587\u672c\u7684\u89c4\u5219\u3002 \u5934\u6587\u4ef6\uff08 2.11 - \u5934\u6587\u4ef6 \uff09\u6307\u7684\u662f\u5305\u542b\u4ee3\u7801\u58f0\u660e\u7684\u6587\u4ef6\u3002\u5f53\u4f7f\u7528 #include \u6307\u4ee4\u65f6\uff0c\u5b83\u4f1a\u628a\u5934\u6587\u4ef6\u7684\u5185\u5bb9\u66ff\u6362\u5230\u8be5\u6307\u4ee4\u7684\u4f4d\u7f6e\u3002\u5728\u5305\u542b\u5934\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u5c16\u62ec\u53f7\u6765\u5904\u7406\u7cfb\u7edf\u5934\u6587\u4ef6\uff08\u4f8b\u5982C++\u6807\u51c6\u5e93\u7684\u5934\u6587\u4ef6\uff09\uff0c\u4f7f\u7528\u53cc\u5f15\u53f7\u6765\u5904\u7406\u7528\u6237\u81ea\u5b9a\u4e49\u5934\u6587\u4ef6\u3002\u5f53\u4f7f\u7528\u7cfb\u7edf\u63d0\u4f9b\u7684\u5934\u6587\u4ef6\u65f6\uff0c\u4f7f\u7528\u4e0d\u5e26 .h \u540e\u7f00\u7684\u7248\u672c\uff08\u5982\u679c\u5b58\u5728\u7684\u8bdd\uff09 \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e\uff08 2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e ]\uff09\u53ef\u4ee5\u907f\u514d\u5934\u6587\u4ef6\u88ab\u591a\u6b21\u5305\u542b\u5230\u540c\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u4f46\u662f\u4e0d\u4f1a\u907f\u514d\u5b83\u4eec\u88ab\u5305\u542b\u5230\u591a\u4e2a\u6587\u4ef6\u4e2d\uff08\u6bcf\u4e2a\u6587\u4ef6\u53ea\u80fd\u5305\u542b\u4e00\u6b21\uff09\u3002","title":"\u7ae0\u8282\u56de\u987e"},{"location":"20-x-Chapter-20-comprehensive-quiz/","tags":["summary"],"text":"\u5f02\u5e38\u5904\u7406\u63d0\u4f9b\u4e86\u5c06\u9519\u8bef\u5904\u7406\u6216\u5f02\u5e38\u60c5\u51b5\u5904\u7406\u6d41\u7a0b\u4e0e\u6b63\u5e38\u6d41\u7a0b\u4ee3\u7801\u89e3\u8026\u7684\u4e00\u79cd\u673a\u5236\u3002\u8fd9\u6837\u4e00\u6765\u7a0b\u5e8f\u5458\u5c31\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\uff0c\u66f4\u52a0\u81ea\u5982\u5730\u5904\u7406\u9519\u8bef\uff0c\u540c\u65f6\u8fd8\u53ef\u4ee5\u5728\u5f88\u5927\u7a0b\u5ea6\u4e0a\u7f13\u89e3\u7531\u57fa\u4e8e\u8fd4\u56de\u9519\u8bef\u7801\u7684\u65b9\u5f0f\u5904\u7406\u5f02\u5e38\u6240\u5e26\u6765\u7684\u95ee\u9898\u3002 throw \u8bed\u53e5\u53ef\u4ee5\u7528\u6765\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u3002 Try \u8bed\u53e5\u5757 \u5219\u4f1a\u5173\u6ce8\u5176\u5757\u5185\u4ee3\u7801\u662f\u5426\u629b\u51fa\u5f02\u5e38\u3002\u4e00\u65e6\u6709\u5f02\u5e38\u629b\u51fa\uff0c\u8fd9\u4e9b\u5f02\u5e38\u5c31\u4f1a\u88ab\u91cd\u5b9a\u5411\u5230\u5bf9\u5e94\u7684 catch \u8bed\u53e5\u5757 \u4e2d\uff0c\u4e0d\u540c\u7c7b\u578b\u7684\u8bed\u53e5\u5757\u53ef\u4ee5\u6355\u83b7\u5bf9\u5e94\u7c7b\u578b\u7684\u5f02\u5e38\u5e76\u5bf9\u5176\u8fdb\u884c\u5904\u7406\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5f02\u5e38\u53ea\u8981\u88ab\u6355\u83b7\uff0c\u5c31\u53ef\u4ee5\u8ba4\u4e3a\u662f\u5904\u7406\u4e86\u3002 \u5f02\u5e38\u4f1a\u88ab\u7acb\u5373\u5904\u7406\u2014\u2014\u4e00\u65e6\u5f02\u5e38\u88ab\u629b\u51fa\uff0c\u63a7\u5236\u6d41\u4f1a\u7acb\u5373\u8df3\u8f6c\u5230\u8ddd\u79bb\u8be5Try\u8bed\u53e5\u5757\u6700\u8fd1\uff0c\u4e14\u80fd\u591f\u5904\u7406\u8be5\u5f02\u5e38\u7684catch\u8bed\u53e5\u5757\u3002\u5982\u679c\u629b\u51fa\u5f02\u5e38\u7684\u8bed\u53e5\u4e0d\u4f4d\u4e8e\u4efb\u4f55try\u8bed\u53e5\u5757\u4e2d\uff0c\u5219\u5f02\u5e38\u4f1a\u6cbf\u7740\u51fd\u6570\u8c03\u7528\u6808\u4e0a\u9001\uff0c\u5982\u679c\u5728\u6574\u4e2a\u51fd\u6570\u6808\u4e2d\u90fd\u6ca1\u6709\u80fd\u591f\u627e\u5230\u53ef\u7528\u7684\u5f02\u5e38\u5904\u7406\u903b\u8f91\uff0c\u7a0b\u5e8f\u5c31\u4f1a\u56e0\u4e3a \u5f02\u5e38\u672a\u5904\u7406 \u800c\u505c\u6b62\u8fd0\u884c\u3002 \u629b\u51fa\u7684\u5f02\u5e38\u53ef\u4ee5\u662f\u4efb\u610f\u7c7b\u578b\u7684\uff0c\u5305\u62ec\u7c7b\u3002 catch \u8bed\u53e5\u5757\u53ef\u4ee5\u88ab\u58f0\u660e\u4e3a\u53ea\u6355\u83b7\u67d0\u79cd\u7279\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7701\u7565\u53f7\u6765\u58f0\u660e\u4e3a\u6355\u83b7\u6240\u6709\u7c7b\u578b\u7684\u5f02\u5e38\u3002\u80fd\u591f\u6355\u83b7\u67d0\u79cd\u57fa\u7c7b\u7684 catch \u8bed\u53e5\u5757\uff0c\u4e5f\u53ef\u4ee5\u6355\u83b7\u8be5\u57fa\u7c7b\u5bf9\u5e94\u7684\u6d3e\u751f\u7c7b\u7c7b\u578b\u7684\u5f02\u5e38\u3002\u6807\u51c6\u5e93\u4e2d\u629b\u51fa\u7684\u6240\u6709\u5f02\u5e38\u7c7b\u578b\uff0c\u90fd\u6d3e\u751f\u81ea std::exception \u7c7b\uff08\u4f4d\u4e8e exception \u5934\u6587\u4ef6\u4e2d\uff09\u3002\u56e0\u6b64\uff0c\u6355\u83b7 std::exception \u7684 catch \u7684\u8bed\u53e5\u5757\uff0c\u53ef\u4ee5\u6355\u83b7\u6807\u51c6\u5e93\u4e2d\u7684\u5f02\u5e38\u3002 what() \u6210\u5458\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u786e\u5b9a\u88ab\u629b\u51fa\u7684 std::exception \u662f\u4ec0\u4e48\u3002 \u5728 catch \u8bed\u53e5\u5757\u4e2d\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u629b\u51fa\u65b0\u7684\u9057\u4ea7\u3002\u56e0\u4e3a\u8fd9\u4e2a\u5f02\u5e38\u4e0d\u662f\u5728 try \u8bed\u53e5\u5757\u4e2d\u629b\u51fa\u7684\uff0c\u6240\u4ee5\u5b83\u4e0d\u4f1a\u88ab\u629b\u51fa\u5b83\u7684 catch \u8bed\u53e5\u5757\u6355\u83b7\u3002\u5f02\u5e38\u53ef\u4ee5\u5728 catch \u8bed\u53e5\u5757\u4e2d\u901a\u8fc7 throw \u5173\u952e\u5b57\u5c06\u8be5\u5f02\u5e38\u91cd\u65b0\u629b\u51fa\u3002\u4e0d\u8fc7\uff0c\u4e0d\u8981\u5c06\u6355\u83b7\u7684\u7684\u5f02\u5e38\u53d8\u91cf\u76f4\u63a5\u91cd\u65b0\u629b\u51fa\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4 \u5bf9\u8c61\u5207\u7247 \u3002 \u51fd\u6570\u4e2d\u7684try-block\u8bed\u53e5\u5757\u4f7f\u6211\u4eec\u53ef\u4ee5\u6355\u83b7\u53d1\u751f\u5728\u51fd\u6570\u5185\u90e8\u6216\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u53d1\u751f\u7684\u5f02\u5e38\u3002\u901a\u5e38\u53ea\u4f1a\u5728\u6d3e\u751f\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4e2d\u4f7f\u7528\u3002 \u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\uff0c\u4e5f\u4e0d\u8981\u5728\u6790\u6784\u51fd\u6570\u4e2d\u629b\u51fa\u5f02\u5e38\u3002 \u901a\u8fc7 noexcept \u5f02\u5e38\u6807\u8bc6\u7b26\u53ef\u4ee5\u5c06\u51fd\u6570\u6807\u8bb0\u4e3a\u4e0d\u5f15\u53d1\u5f02\u5e38\u3002 \u5f02\u5e38\u5904\u7406\u662f\u6709\u5f00\u9500\u7684\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u5f02\u5e38\u7684\u4ee3\u7801\u4f1a\u7a0d\u6162\u4e00\u4e9b\uff0c\u800c\u4e14\u5904\u7406\u5f02\u5e38\u7684\u5f00\u9500\u975e\u5e38\u9ad8\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5e94\u8be5\u53ea\u5728\u9700\u8981\u5904\u7406\u5f02\u5e38\u7684\u573a\u5408\u4e0b\u4f7f\u7528\u5f02\u5e38\uff0c\u800c\u4e0d\u8981\u5728\u4e00\u822c\u7684\u9519\u8bef\u5904\u7406\u65f6\u4f7f\u7528\u5f02\u5e38\uff08\u4f8b\u5982\u5904\u7406\u975e\u6cd5\u8f93\u5165\uff09\u3002","title":"20.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f02\u5e38"},{"location":"20.4/","text":"","title":"20.4"},{"location":"21-1-the-standard-library/","tags":["stl"],"text":"Key Takeaway \u606d\u559c\u4f60\uff01\u4f60\u5df2\u7ecf\u5b8c\u6210\u4e86\u6559\u7a0b\u7684\u4e3b\u8981\u90e8\u5206\uff01\u5728\u524d\u9762\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86C++\u8bed\u8a00\u7684\u8bb8\u591a\u4e3b\u8981\u7279\u6027(\u5305\u62ecC++11/14/17\u6269\u5c55\u7684\u4e00\u4e9b\u7279\u6027)\u3002 \u90a3\u4e48\u4f60\u80af\u5b9a\u8981\u95ee\u4e86\uff0c\u201c\u63a5\u4e0b\u6765\u8981\u600e\u4e48\u505a\u5462\uff1f\u201d \u4f60\u53ef\u80fd\u5df2\u7ecf\u6ce8\u610f\u5230\u4e86\uff0c\u5927\u91cf\u7684\u7a0b\u5e8f\u90fd\u5728\u53cd\u590d\u4f7f\u7528\u76f8\u540c\u7684\u6982\u5ff5\uff1a\u5faa\u73af\u3001\u5b57\u7b26\u4e32\u3001\u6570\u7ec4\u3001\u6392\u5e8f\u7b49\u2026\u2026\u4f60\u53ef\u80fd\u8fd8\u6ce8\u610f\u5230\uff0c\u4f7f\u7528\u975e\u7c7b\u7248\u672c\u7684\u5bb9\u5668\u548c\u5e38\u7528\u7b97\u6cd5\u7f16\u5199\u7684\u7a0b\u5e8f\u5f88\u5bb9\u6613\u51fa\u9519\u3002\u597d\u6d88\u606f\u662f\uff0cC++\u9644\u5e26\u4e86\u4e00\u4e2a\u5e93\uff0c\u5176\u4e2d\u63d0\u4f9b\u4e86\u5f88\u591a\u53ef\u91cd\u7528\u7684\u7c7b\uff0c\u4f60\u53ef\u4ee5\u5229\u7528\u5b83\u4eec\u6784\u5efa\u7a0b\u5e8f\u3002\u8fd9\u4e2a\u5e93\u88ab\u79f0\u4e3aC++\u6807\u51c6\u5e93\u3002 \u6807\u51c6\u5e93 \u6807\u51c6\u5e93\u5305\u542b\u4e00\u7ec4\u7c7b\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u6a21\u677f\u5bb9\u5668\u3001\u7b97\u6cd5\u548c\u8fed\u4ee3\u5668\u3002\u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a\u5e38\u7528\u7684\u7c7b\u6216\u7b97\u6cd5\uff0c\u6807\u51c6\u5e93\u4e2d\u5f88\u53ef\u80fd\u5df2\u7ecf\u63d0\u4f9b\u4e86\u5b83\u3002\u4f7f\u7528\u6807\u51c6\u5e93\u7684\u4f18\u70b9\u662f\u4f60\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8fd9\u4e9b\u7c7b\uff0c\u800c\u4e0d\u5fc5\u81ea\u5df1\u7f16\u5199\u548c\u8c03\u8bd5\u7c7b\u3002\u800c\u4e14\u6807\u51c6\u5e93\u4e2d\u7684\u7c7b\u90fd\u975e\u5e38\u9ad8\u6548\u3002\u7f3a\u70b9\u662f\u6807\u51c6\u5e93\u5f88\u590d\u6742\uff0c\u800c\u4e14\u770b\u8d77\u6765\u53ef\u80fd\u6709\u70b9\u5413\u4eba\uff0c\u56e0\u4e3a\u6240\u6709\u4e1c\u897f\u90fd\u662f\u6a21\u677f\u5316\u7684\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u4f60\u53ef\u4ee5\u5148\u5c06\u6807\u51c6\u5e93\u5206\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u53ea\u4f7f\u7528\u4f60\u9700\u8981\u7684\u90e8\u5206\uff0c\u800c\u5ffd\u7565\u5176\u4ed6\u90e8\u5206\uff0c\u76f4\u5230\u4f60\u9700\u8981\u4f7f\u7528\u5b83\u4eec\u7684\u65f6\u5019\u518d\u53bb\u4ed4\u7ec6\u7814\u7a76\u3002 \u5728\u63a5\u4e0b\u6765\u7684\u51e0\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f1a\u5bbd\u6cdb\u5730\u4e86\u89e3\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u5bb9\u5668\u3001\u7b97\u6cd5\u548c\u8fed\u4ee3\u5668\u7684\u7c7b\u578b\u3002\u7136\u540e\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u6df1\u5165\u7814\u7a76\u4e00\u4e9b\u5177\u4f53\u7684\u7c7b\u3002","title":"21.1 - \u6807\u51c6\u5e93"},{"location":"21-1-the-standard-library/#_1","text":"\u6807\u51c6\u5e93\u5305\u542b\u4e00\u7ec4\u7c7b\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u6a21\u677f\u5bb9\u5668\u3001\u7b97\u6cd5\u548c\u8fed\u4ee3\u5668\u3002\u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a\u5e38\u7528\u7684\u7c7b\u6216\u7b97\u6cd5\uff0c\u6807\u51c6\u5e93\u4e2d\u5f88\u53ef\u80fd\u5df2\u7ecf\u63d0\u4f9b\u4e86\u5b83\u3002\u4f7f\u7528\u6807\u51c6\u5e93\u7684\u4f18\u70b9\u662f\u4f60\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8fd9\u4e9b\u7c7b\uff0c\u800c\u4e0d\u5fc5\u81ea\u5df1\u7f16\u5199\u548c\u8c03\u8bd5\u7c7b\u3002\u800c\u4e14\u6807\u51c6\u5e93\u4e2d\u7684\u7c7b\u90fd\u975e\u5e38\u9ad8\u6548\u3002\u7f3a\u70b9\u662f\u6807\u51c6\u5e93\u5f88\u590d\u6742\uff0c\u800c\u4e14\u770b\u8d77\u6765\u53ef\u80fd\u6709\u70b9\u5413\u4eba\uff0c\u56e0\u4e3a\u6240\u6709\u4e1c\u897f\u90fd\u662f\u6a21\u677f\u5316\u7684\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u4f60\u53ef\u4ee5\u5148\u5c06\u6807\u51c6\u5e93\u5206\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u53ea\u4f7f\u7528\u4f60\u9700\u8981\u7684\u90e8\u5206\uff0c\u800c\u5ffd\u7565\u5176\u4ed6\u90e8\u5206\uff0c\u76f4\u5230\u4f60\u9700\u8981\u4f7f\u7528\u5b83\u4eec\u7684\u65f6\u5019\u518d\u53bb\u4ed4\u7ec6\u7814\u7a76\u3002 \u5728\u63a5\u4e0b\u6765\u7684\u51e0\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f1a\u5bbd\u6cdb\u5730\u4e86\u89e3\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u5bb9\u5668\u3001\u7b97\u6cd5\u548c\u8fed\u4ee3\u5668\u7684\u7c7b\u578b\u3002\u7136\u540e\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u6df1\u5165\u7814\u7a76\u4e00\u4e9b\u5177\u4f53\u7684\u7c7b\u3002","title":"\u6807\u51c6\u5e93"},{"location":"21-2-STL-containers-overview/","tags":["stl","containers"],"text":"Key Takeaway STL\u5e93\u4e2d\u6700\u6709\u7528\u7684\u529f\u80fd\u8981\u7b97\u662fSTL\u5bb9\u5668\u7c7b\u4e86\u3002\u5982\u679c\u4f60\u9700\u8981\u5feb\u901f\u590d\u4e60\u4e00\u4e0b\u4ec0\u4e48\u662f\u5bb9\u5668\u7c7b\uff0c\u8bf7\u53c2\u8003 16.6 - \u5bb9\u5668\u7c7b \u3002 STL \u63d0\u4f9b\u4e86\u5f88\u591a\u4e0d\u540c\u573a\u5408\u4f7f\u7528\u7684 \u5bb9\u5668\u7c7b \u3002\u901a\u5e38\uff0c\u8fd9\u4e9b\u5bb9\u5668\u7c7b\u53ef\u4ee5\u5206\u4e3a\u4e09\u4e2a\u5927\u7c7b\uff1a \u987a\u5e8f\u5bb9\u5668 \uff0c \u5173\u8054\u5bb9\u5668 \u548c \u5bb9\u5668\u9002\u914d\u5668 \u3002 \u987a\u5e8f\u5bb9\u5668 \u987a\u5e8f\u5bb9\u5668\u662f\u6307\u90a3\u4e9b\u5728\u5bb9\u5668\u4e2d\u6309\u987a\u5e8f\u5b58\u653e\u5143\u7d20\u7684\u5bb9\u5668\u7c7b\u3002\u987a\u5e8f\u5bb9\u5668\u7684\u4e00\u5927\u7279\u70b9\u5c31\u662f\u4f60\u53ef\u4ee5\u51b3\u5b9a\u5728\u54ea\u4e2a\u4f4d\u7f6e\u63d2\u5165\u5143\u7d20\u3002\u6700\u5e38\u89c1\u7684\u987a\u5e8f\u5bb9\u5668\u7684\u4f8b\u5b50\u5c31\u662f\u6570\u7ec4\uff1a\u5982\u679c\u4f60\u9700\u8981\u5728\u6570\u7ec4\u7684\u67d0\u4e2a\u4f4d\u7f6e\u63d2\u5165\u5143\u7d20\uff0c\u90a3\u4e48\u8be5\u5143\u7d20\u4e00\u5b9a\u4f1a\u88ab\u63d2\u5165\u5230\u8fd9\u4e2a\u4f4d\u7f6e\u3002 \u5bf9\u4e8eC++11\u6765\u8bf4\uff0cSTL\u5305\u542b\u4e866\u79cd\u987a\u5e8f\u5bb9\u5668\uff1a std::vector std::deque std::array std::list std::forward_list std::basic_string \u5982\u679c\u4f60\u5b66\u8fc7\u7269\u7406\u7684\u8bdd\uff0c\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3avector\u8868\u793a\u7684\u5177\u6709\u5927\u5c0f\u548c\u65b9\u5411\u7684\u5411\u91cf\u3002\u4e0d\u8fc7\uff0cSTL\u4e2d\u7684 vector \u53ef\u4e0d\u662f\u5411\u91cf\uff0c\u5b83\u662f\u4e00\u4e2a\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u5927\u5c0f\u7684\u52a8\u6001\u6570\u7ec4\u3002vector \u5bb9\u5668\u7c7b\u652f\u6301\u901a\u8fc7\u4e0b\u6807\u8fd0\u7b97\u7b26\u8fdb\u884c\u968f\u673a\u8bbf\u95ee\uff0c\u4e5f\u652f\u6301\u50cf\u5c3e\u90e8\u5feb\u901f\u63d2\u5165\u5143\u7d20\u3002 \u4e0b\u9762\u7684\u4ee3\u7801\u5411 vector \u4e2d\u63d2\u5165\u4e86 6 \u4e2a\u6570\u5b57\uff0c\u5e76\u4e14\u4f7f\u7528\u4e0b\u6807\u8fd0\u7b97\u7b26\u4f9d\u6b21\u8bbf\u95ee\u5e76\u6253\u5370\u5143\u7d20\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: vector < int > vect ; for ( int count = 0 ; count < 6 ; ++ count ) vect . push_back ( 10 - count ); // insert at end of array for ( int index = 0 ; index < vect . size (); ++ index ) std :: cout << vect [ index ] << ' ' ; std :: cout << '\\n' ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 10 9 8 7 6 5 deque \u7c7b\uff08\u8bfb\u4f5cdeck\uff09\u662f\u4e00\u4e2a\u53cc\u7aef\u961f\u5217\u5bb9\u5668\u7c7b\uff0c\u5b83\u4f7f\u7528\u52a8\u6001\u6570\u7ec4\u5b9e\u73b0\uff0c\u652f\u6301\u5411\u524d\u9762\u6216\u8005\u540e\u9762\u63d2\u5165\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: deque < int > deq ; for ( int count = 0 ; count < 3 ; ++ count ) { deq . push_back ( count ); // insert at end of array deq . push_front ( 10 - count ); // insert at front of array } for ( int index = 0 ; index < deq . size (); ++ index ) std :: cout << deq [ index ] << ' ' ; std :: cout << '\\n' ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 8 9 10 0 1 2 list \u662f\u4e00\u79cd\u7279\u6b8a\u7684\u987a\u5e8f\u5bb9\u5668\uff0c\u79f0\u4e3a\u53cc\u5411\u94fe\u8868\u3002\u5bb9\u5668\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u901a\u8fc7\u6307\u9488\u6307\u5411\u524d\u4e00\u4e2a\u5143\u7d20\u548c\u540e\u4e00\u4e2a\u5143\u7d20\u3002\u94fe\u8868\u53ea\u63d0\u4f9b\u4e86\u4ece\u7b2c\u4e00\u4e2a\u5143\u7d20\u6216\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u8bbf\u95ee\u5bb9\u5668\u7684\u65b9\u6cd5\u2014\u2014\u5373\u4e0d\u652f\u6301\u5143\u7d20\u7684\u968f\u673a\u8bbf\u95ee\u3002\u5982\u679c\u4f60\u9700\u8981\u8bbf\u95ee\u4e2d\u95f4\u4f4d\u7f6e\u7684\u5143\u7d20\uff0c\u4f60\u5fc5\u987b\u904d\u5386\u94fe\u8868\u4ee5\u627e\u5230\u4f60\u9700\u8981\u8bbf\u95ee\u7684\u5143\u7d20\u3002\u94fe\u8868\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u652f\u6301\u5feb\u901f\u63d2\u5165\u5143\u7d20\uff08\u5982\u679c\u4f60\u77e5\u9053\u8981\u5411\u54ea\u91cc\u63d2\u5165\u7684\u8bdd\uff09\u3002\u901a\u5e38\u6211\u4eec\u4f1a\u4f7f\u7528\u8fed\u4ee3\u5668\u904d\u5386\u94fe\u8868\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u94fe\u8868\u548c\u8fed\u4ee3\u5668\u3002 \u5c3d\u7ba1 STL \u4e2d\u7684 string (\u548c wstring )\u901a\u5e38\u4e0d\u88ab\u770b\u505a\u662f\u4e00\u79cd\u987a\u5e8f\u5bb9\u5668\uff0c\u4f46\u4ece\u672c\u8d28\u4e0a\u8bb2\u5b83\u662f\u7684\u3002\u4f60\u53ef\u4ee5\u5c06\u5b83\u770b\u505a\u662f\u5143\u7d20\u7c7b\u578b\u4e3achar\u6216wchar\u7684vector\u3002 \u5173\u8054\u5bb9\u5668 \u5173\u8054\u5bb9\u5668\u4f1a\u5728\u65b0\u5143\u7d20\u63d2\u5165\u65f6\u81ea\u52a8\u6392\u5e8f\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5173\u8054\u5bb9\u5668\u4f1a\u4f7f\u7528 < \u5bf9\u5143\u7d20\u8fdb\u884c\u6bd4\u8f83\u3002 set \u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u653e\u552f\u4e00\u5143\u7d20\u7684\u5bb9\u5668\uff0c\u91cd\u590d\u7684\u5143\u7d20\u662f\u4e0d\u88ab\u5141\u8bb8\u7684\u3002\u5143\u7d20\u4f1a\u6309\u7167\u5b83\u4eec\u7684\u503c\u8fdb\u884c\u6392\u5e8f\uff1b multiset \u662f\u4e00\u79cd\u5141\u8bb8\u91cd\u590d\u5143\u7d20\u7684\u96c6\u5408\uff1b map (\u4e5f\u79f0\u4e3a\u5173\u8054\u6570\u7ec4) \u662f\u4e00\u4e2a\u5305\u542b\u5143\u7d20\u5bf9\u7684\u96c6\u5408\uff0c\u79f0\u4e3a\u952e\u503c\u5bf9\u3002\u5176\u4e2d\u952e\u7528\u6765\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u548c\u7d22\u5f15\uff0c\u800c\u4e14\u952e\u5fc5\u987b\u662f\u552f\u4e00\u7684\u3002\u5176\u4e2d\u503c\u662f\u5b9e\u9645\u6570\u636e\uff1b multimap (\u4e5f\u79f0\u4e3a\u5b57\u5178)\u662f\u4e00\u79cd\u6620\u5c04\uff0c\u5b83\u5141\u8bb8\u91cd\u590d\u7684\u952e\u3002\u5728\u5b9e\u9645\u751f\u6d3b\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u79cd multimaps \uff1a\u952e\u662f\u8981\u67e5\u7684\u8bcd\uff0c\u800c\u503c\u5219\u662f\u8bcd\u7684\u610f\u601d\u3002\u6240\u6709\u7684\u952e\u5747\u662f\u6309\u5347\u5e8f\u6392\u5217\u7684\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u952e\u6765\u67e5\u503c\u3002\u6709\u4e9b\u8bcd\u6709\u591a\u4e2a\u542b\u4e49\uff0c\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5b57\u5178\u662f\u4e00\u4e2a multimap \u800c\u4e0d\u662f map \u3002 \u5bb9\u5668\u9002\u914d\u5668 \u5bb9\u5668\u9002\u914d\u5668\u662f\u7528\u4e8e\u7279\u5b9a\u7528\u9014\u7684\u7279\u6b8a\u9884\u5b9a\u4e49\u5bb9\u5668\u3002\u5bb9\u5668\u9002\u914d\u5668\u7684\u6709\u8da3\u4e4b\u5904\u5728\u4e8e\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u5b83\u4eec\u4f7f\u7528\u7684 \u987a\u5e8f\u5bb9\u5668 \u3002 stack \u5bb9\u5668\u652f\u6301\u5143\u7d20\u7684 LIFO \uff08\u540e\u5165\u5148\u51fa\uff09\u3002\u5143\u7d20\u4ece\u5bb9\u5668\u7684\u672b\u7aef\u88ab\u538b\u5165(push)\u6216\u5f39\u51fa(pop)\u3002 stack \u9ed8\u8ba4\u4f7f\u7528 deque \u4f5c\u4e3a\u987a\u5e8f\u5bb9\u5668\uff08\u770b\u4e0a\u53bb\u591a\u5c11\u6709\u70b9\u5947\u602a\uff0c\u6bd5\u7adf\u611f\u89c9 vector \u66f4\u5408\u9002\uff09\uff0c\u4f46\u662f\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 vector \u6216\u94fe\u8868\uff1b queue \u5bb9\u5668\u652f\u6301\u5143\u7d20\u7684 FIFO\uff08\u5148\u5165\u5148\u51fa\uff09\u3002\u5143\u7d20\u4ece\u5bb9\u5668\u7684\u5c3e\u90e8\u63d2\u5165\uff0c\u7136\u540e\u4ece\u5934\u90e8\u5f39\u51fa\u3002 queue \u9ed8\u8ba4\u4f7f\u7528 deque \u4f5c\u4e3a\u987a\u5e8f\u5bb9\u5668\uff0c\u4f46\u662f\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u94fe\u8868\uff1b priority queue \u662f\u4e00\u79cd\u59cb\u7ec8\u4fdd\u6301\u5143\u7d20\u4e3a\u6392\u5e8f\u72b6\u6001\uff08\u901a\u8fc7 < \u8fd0\u7b97\u7b26\uff09\u7684\u961f\u5217\u3002\u5f53\u5143\u7d20\u88ab\u538b\u5165\u4f18\u5148\u7ea7\u961f\u5217\u65f6\uff0c\u5143\u7d20\u4f1a\u5728\u961f\u5217\u4e2d\u8fdb\u884c\u6392\u5e8f\u3002\u5220\u9664\u65f6\uff0c\u5219\u4ece\u961f\u9996\u79fb\u9664\u5143\u7d20\uff08\u5f53\u524d\u4f18\u5148\u7ea7\u6700\u9ad8\u7684\u5143\u7d20\uff09\u3002","title":"21.2 - STL\u5bb9\u5668"},{"location":"21-2-STL-containers-overview/#_1","text":"\u987a\u5e8f\u5bb9\u5668\u662f\u6307\u90a3\u4e9b\u5728\u5bb9\u5668\u4e2d\u6309\u987a\u5e8f\u5b58\u653e\u5143\u7d20\u7684\u5bb9\u5668\u7c7b\u3002\u987a\u5e8f\u5bb9\u5668\u7684\u4e00\u5927\u7279\u70b9\u5c31\u662f\u4f60\u53ef\u4ee5\u51b3\u5b9a\u5728\u54ea\u4e2a\u4f4d\u7f6e\u63d2\u5165\u5143\u7d20\u3002\u6700\u5e38\u89c1\u7684\u987a\u5e8f\u5bb9\u5668\u7684\u4f8b\u5b50\u5c31\u662f\u6570\u7ec4\uff1a\u5982\u679c\u4f60\u9700\u8981\u5728\u6570\u7ec4\u7684\u67d0\u4e2a\u4f4d\u7f6e\u63d2\u5165\u5143\u7d20\uff0c\u90a3\u4e48\u8be5\u5143\u7d20\u4e00\u5b9a\u4f1a\u88ab\u63d2\u5165\u5230\u8fd9\u4e2a\u4f4d\u7f6e\u3002 \u5bf9\u4e8eC++11\u6765\u8bf4\uff0cSTL\u5305\u542b\u4e866\u79cd\u987a\u5e8f\u5bb9\u5668\uff1a std::vector std::deque std::array std::list std::forward_list std::basic_string \u5982\u679c\u4f60\u5b66\u8fc7\u7269\u7406\u7684\u8bdd\uff0c\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3avector\u8868\u793a\u7684\u5177\u6709\u5927\u5c0f\u548c\u65b9\u5411\u7684\u5411\u91cf\u3002\u4e0d\u8fc7\uff0cSTL\u4e2d\u7684 vector \u53ef\u4e0d\u662f\u5411\u91cf\uff0c\u5b83\u662f\u4e00\u4e2a\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u5927\u5c0f\u7684\u52a8\u6001\u6570\u7ec4\u3002vector \u5bb9\u5668\u7c7b\u652f\u6301\u901a\u8fc7\u4e0b\u6807\u8fd0\u7b97\u7b26\u8fdb\u884c\u968f\u673a\u8bbf\u95ee\uff0c\u4e5f\u652f\u6301\u50cf\u5c3e\u90e8\u5feb\u901f\u63d2\u5165\u5143\u7d20\u3002 \u4e0b\u9762\u7684\u4ee3\u7801\u5411 vector \u4e2d\u63d2\u5165\u4e86 6 \u4e2a\u6570\u5b57\uff0c\u5e76\u4e14\u4f7f\u7528\u4e0b\u6807\u8fd0\u7b97\u7b26\u4f9d\u6b21\u8bbf\u95ee\u5e76\u6253\u5370\u5143\u7d20\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: vector < int > vect ; for ( int count = 0 ; count < 6 ; ++ count ) vect . push_back ( 10 - count ); // insert at end of array for ( int index = 0 ; index < vect . size (); ++ index ) std :: cout << vect [ index ] << ' ' ; std :: cout << '\\n' ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 10 9 8 7 6 5 deque \u7c7b\uff08\u8bfb\u4f5cdeck\uff09\u662f\u4e00\u4e2a\u53cc\u7aef\u961f\u5217\u5bb9\u5668\u7c7b\uff0c\u5b83\u4f7f\u7528\u52a8\u6001\u6570\u7ec4\u5b9e\u73b0\uff0c\u652f\u6301\u5411\u524d\u9762\u6216\u8005\u540e\u9762\u63d2\u5165\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: deque < int > deq ; for ( int count = 0 ; count < 3 ; ++ count ) { deq . push_back ( count ); // insert at end of array deq . push_front ( 10 - count ); // insert at front of array } for ( int index = 0 ; index < deq . size (); ++ index ) std :: cout << deq [ index ] << ' ' ; std :: cout << '\\n' ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 8 9 10 0 1 2 list \u662f\u4e00\u79cd\u7279\u6b8a\u7684\u987a\u5e8f\u5bb9\u5668\uff0c\u79f0\u4e3a\u53cc\u5411\u94fe\u8868\u3002\u5bb9\u5668\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u901a\u8fc7\u6307\u9488\u6307\u5411\u524d\u4e00\u4e2a\u5143\u7d20\u548c\u540e\u4e00\u4e2a\u5143\u7d20\u3002\u94fe\u8868\u53ea\u63d0\u4f9b\u4e86\u4ece\u7b2c\u4e00\u4e2a\u5143\u7d20\u6216\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u8bbf\u95ee\u5bb9\u5668\u7684\u65b9\u6cd5\u2014\u2014\u5373\u4e0d\u652f\u6301\u5143\u7d20\u7684\u968f\u673a\u8bbf\u95ee\u3002\u5982\u679c\u4f60\u9700\u8981\u8bbf\u95ee\u4e2d\u95f4\u4f4d\u7f6e\u7684\u5143\u7d20\uff0c\u4f60\u5fc5\u987b\u904d\u5386\u94fe\u8868\u4ee5\u627e\u5230\u4f60\u9700\u8981\u8bbf\u95ee\u7684\u5143\u7d20\u3002\u94fe\u8868\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u652f\u6301\u5feb\u901f\u63d2\u5165\u5143\u7d20\uff08\u5982\u679c\u4f60\u77e5\u9053\u8981\u5411\u54ea\u91cc\u63d2\u5165\u7684\u8bdd\uff09\u3002\u901a\u5e38\u6211\u4eec\u4f1a\u4f7f\u7528\u8fed\u4ee3\u5668\u904d\u5386\u94fe\u8868\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u94fe\u8868\u548c\u8fed\u4ee3\u5668\u3002 \u5c3d\u7ba1 STL \u4e2d\u7684 string (\u548c wstring )\u901a\u5e38\u4e0d\u88ab\u770b\u505a\u662f\u4e00\u79cd\u987a\u5e8f\u5bb9\u5668\uff0c\u4f46\u4ece\u672c\u8d28\u4e0a\u8bb2\u5b83\u662f\u7684\u3002\u4f60\u53ef\u4ee5\u5c06\u5b83\u770b\u505a\u662f\u5143\u7d20\u7c7b\u578b\u4e3achar\u6216wchar\u7684vector\u3002","title":"\u987a\u5e8f\u5bb9\u5668"},{"location":"21-2-STL-containers-overview/#_2","text":"\u5173\u8054\u5bb9\u5668\u4f1a\u5728\u65b0\u5143\u7d20\u63d2\u5165\u65f6\u81ea\u52a8\u6392\u5e8f\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5173\u8054\u5bb9\u5668\u4f1a\u4f7f\u7528 < \u5bf9\u5143\u7d20\u8fdb\u884c\u6bd4\u8f83\u3002 set \u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u653e\u552f\u4e00\u5143\u7d20\u7684\u5bb9\u5668\uff0c\u91cd\u590d\u7684\u5143\u7d20\u662f\u4e0d\u88ab\u5141\u8bb8\u7684\u3002\u5143\u7d20\u4f1a\u6309\u7167\u5b83\u4eec\u7684\u503c\u8fdb\u884c\u6392\u5e8f\uff1b multiset \u662f\u4e00\u79cd\u5141\u8bb8\u91cd\u590d\u5143\u7d20\u7684\u96c6\u5408\uff1b map (\u4e5f\u79f0\u4e3a\u5173\u8054\u6570\u7ec4) \u662f\u4e00\u4e2a\u5305\u542b\u5143\u7d20\u5bf9\u7684\u96c6\u5408\uff0c\u79f0\u4e3a\u952e\u503c\u5bf9\u3002\u5176\u4e2d\u952e\u7528\u6765\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u548c\u7d22\u5f15\uff0c\u800c\u4e14\u952e\u5fc5\u987b\u662f\u552f\u4e00\u7684\u3002\u5176\u4e2d\u503c\u662f\u5b9e\u9645\u6570\u636e\uff1b multimap (\u4e5f\u79f0\u4e3a\u5b57\u5178)\u662f\u4e00\u79cd\u6620\u5c04\uff0c\u5b83\u5141\u8bb8\u91cd\u590d\u7684\u952e\u3002\u5728\u5b9e\u9645\u751f\u6d3b\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u79cd multimaps \uff1a\u952e\u662f\u8981\u67e5\u7684\u8bcd\uff0c\u800c\u503c\u5219\u662f\u8bcd\u7684\u610f\u601d\u3002\u6240\u6709\u7684\u952e\u5747\u662f\u6309\u5347\u5e8f\u6392\u5217\u7684\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u952e\u6765\u67e5\u503c\u3002\u6709\u4e9b\u8bcd\u6709\u591a\u4e2a\u542b\u4e49\uff0c\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5b57\u5178\u662f\u4e00\u4e2a multimap \u800c\u4e0d\u662f map \u3002","title":"\u5173\u8054\u5bb9\u5668"},{"location":"21-2-STL-containers-overview/#_3","text":"\u5bb9\u5668\u9002\u914d\u5668\u662f\u7528\u4e8e\u7279\u5b9a\u7528\u9014\u7684\u7279\u6b8a\u9884\u5b9a\u4e49\u5bb9\u5668\u3002\u5bb9\u5668\u9002\u914d\u5668\u7684\u6709\u8da3\u4e4b\u5904\u5728\u4e8e\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u5b83\u4eec\u4f7f\u7528\u7684 \u987a\u5e8f\u5bb9\u5668 \u3002 stack \u5bb9\u5668\u652f\u6301\u5143\u7d20\u7684 LIFO \uff08\u540e\u5165\u5148\u51fa\uff09\u3002\u5143\u7d20\u4ece\u5bb9\u5668\u7684\u672b\u7aef\u88ab\u538b\u5165(push)\u6216\u5f39\u51fa(pop)\u3002 stack \u9ed8\u8ba4\u4f7f\u7528 deque \u4f5c\u4e3a\u987a\u5e8f\u5bb9\u5668\uff08\u770b\u4e0a\u53bb\u591a\u5c11\u6709\u70b9\u5947\u602a\uff0c\u6bd5\u7adf\u611f\u89c9 vector \u66f4\u5408\u9002\uff09\uff0c\u4f46\u662f\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 vector \u6216\u94fe\u8868\uff1b queue \u5bb9\u5668\u652f\u6301\u5143\u7d20\u7684 FIFO\uff08\u5148\u5165\u5148\u51fa\uff09\u3002\u5143\u7d20\u4ece\u5bb9\u5668\u7684\u5c3e\u90e8\u63d2\u5165\uff0c\u7136\u540e\u4ece\u5934\u90e8\u5f39\u51fa\u3002 queue \u9ed8\u8ba4\u4f7f\u7528 deque \u4f5c\u4e3a\u987a\u5e8f\u5bb9\u5668\uff0c\u4f46\u662f\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u94fe\u8868\uff1b priority queue \u662f\u4e00\u79cd\u59cb\u7ec8\u4fdd\u6301\u5143\u7d20\u4e3a\u6392\u5e8f\u72b6\u6001\uff08\u901a\u8fc7 < \u8fd0\u7b97\u7b26\uff09\u7684\u961f\u5217\u3002\u5f53\u5143\u7d20\u88ab\u538b\u5165\u4f18\u5148\u7ea7\u961f\u5217\u65f6\uff0c\u5143\u7d20\u4f1a\u5728\u961f\u5217\u4e2d\u8fdb\u884c\u6392\u5e8f\u3002\u5220\u9664\u65f6\uff0c\u5219\u4ece\u961f\u9996\u79fb\u9664\u5143\u7d20\uff08\u5f53\u524d\u4f18\u5148\u7ea7\u6700\u9ad8\u7684\u5143\u7d20\uff09\u3002","title":"\u5bb9\u5668\u9002\u914d\u5668"},{"location":"21-3-STL-iterators-overview/","tags":["stl","containers"],"text":"Key Takeaway \u8fed\u4ee3\u5668 \u662f\u4e00\u4e2a\u53ef\u4ee5\u904d\u5386 \u5bb9\u5668\u7c7b \u7684\u5bf9\u8c61\uff0c\u4f7f\u7528\u5b83\u904d\u5386\u5bb9\u5668\u65f6\uff0c\u4f7f\u7528\u8005\u65e0\u9700\u77e5\u9053\u5bb9\u5668\u7684\u5b9e\u73b0\u7ec6\u8282\u3002\u5bf9\u4e8e\u5f88\u591a\u7c7b\u6765\u8bf4\uff08\u5c24\u5176\u662f\u94fe\u8868\u6216\u5173\u8054\u5bb9\u5668\uff09\uff0c\u8fed\u4ee3\u5668\u662f\u662f\u8bbf\u95ee\u5176\u5143\u7d20\u7684\u9996\u9009\u65b9\u5f0f\u3002 \u8fed\u4ee3\u5668\u53ef\u4ee5\u88ab\u5f62\u8c61\u5730\u770b\u505a\u662f\u4e00\u4e2a\u6307\u5411\u5bb9\u5668\u5143\u7d20\u7684\u6307\u9488\uff0c\u540c\u65f6\u5b83\u5177\u6709\u4e00\u7cfb\u5217\u88ab\u91cd\u8f7d\u7684\u8fd0\u7b97\u7b26\uff1a Operator* -- \u8fed\u4ee3\u5668\u7684 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \uff0c\u5b83\u53ef\u4ee5\u8fd4\u56de\u8fed\u4ee3\u5668\u5f53\u524d\u6240\u6307\u7684\u5143\u7d20\uff1b Operator++ -- \u5c06\u8fed\u4ee3\u5668\u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002\u5f88\u591a\u8fed\u4ee3\u5668\u4e5f\u63d0\u4f9b\u4e86 Operator-- \u7528\u4e8e\u5c06\u8fed\u4ee3\u5668\u79fb\u52a8\u5230\u4e0a\u4e00\u4e2a\u5143\u7d20\uff1b Operator== \u548c Operator!= -- \u57fa\u672c\u7684\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff0c\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u8fed\u4ee3\u5668\u6240\u6307\u7684\u5143\u7d20\u662f\u5426\u76f8\u540c\u3002\u5982\u679c\u9700\u8981\u6bd4\u8f83\u4e24\u4e2a\u8fed\u4ee3\u5668\u6240\u6307\u5143\u7d20\u7684\u503c\u662f\u5426\u76f8\u540c\uff0c\u5219\u9700\u8981\u5148\u5bf9\u8fed\u4ee3\u5668\u89e3\u5f15\u7528\uff0c\u7136\u540e\u5728\u6bd4\u8f83\uff1b Operator= -- \u4e3a\u8fed\u4ee3\u5668\u8d4b\u503c\u4e00\u4e2a\u65b0\u7684\u4f4d\u7f6e\uff08\u4e00\u822c\u6765\u8bf4\u662f\u5bb9\u5668\u7684\u8d77\u70b9\u6216\u672b\u5c3e\u5143\u7d20\uff09\u3002\u5982\u679c\u9700\u8981\u7ed9\u8fed\u4ee3\u5668\u6240\u6307\u7684\u5143\u7d20\u8d4b\u503c\uff0c\u5219\u9700\u8981\u5148\u5bf9\u8fed\u4ee3\u5668\u89e3\u5f15\u7528\u7136\u540e\u518d\u8d4b\u503c\u3002 \u6bcf\u4e2a\u5bb9\u5668\u90fd\u63d0\u4f9b\u4e86\u56db\u4e2a\u57fa\u672c\u7684\u6210\u5458\u51fd\u6570\uff0c\u7528\u4e8e\u914d\u5408\u8d4b\u503c\u53f7\u4f7f\u7528\uff1a begin() \uff1a\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u5bb9\u5668\u8d77\u59cb\u5143\u7d20\u7684\u8fed\u4ee3\u5668\uff1b end() \uff1a\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u5bb9\u5668\u672b\u5c3e\u7684\u8fed\u4ee3\u5668\uff08\u5176\u524d\u4e00\u4e2a\u5143\u7d20\u4e3a\u5bb9\u5668\u4e2d\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff09\uff1b cbegin() \uff1a\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u5bb9\u5668\u8d77\u59cb\u5143\u7d20\u7684const\u8fed\u4ee3\u5668\uff1b cend() \uff1a\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u5bb9\u5668\u672b\u5c3e\u7684const\u8fed\u4ee3\u5668\uff08\u5176\u524d\u4e00\u4e2a\u5143\u7d20\u4e3a\u5bb9\u5668\u4e2d\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff09\u3002 \u867d\u7136 end() \u6307\u5411\u7684\u4e0d\u662f\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u800c\u662f\u5176\u540e\u9762\u4e00\u4e2a\u4f4d\u7f6e\u6709\u70b9\u5947\u602a\uff0c\u4f46\u5176\u5b9e\u8fd9\u4e48\u505a\u53ef\u4ee5\u7b80\u5316\u5faa\u73af\uff1a\u8fed\u4ee3\u53ef\u4ee5\u4e00\u76f4\u8fdb\u884c\uff0c\u76f4\u5230\u8fed\u4ee3\u5668\u5230\u8fbe end() \uff0c\u6b64\u65f6\u6211\u4eec\u5c31\u77e5\u9053\u904d\u5386\u7ed3\u675f\u4e86\u3002 \u6700\u540e\uff0c\u6240\u6709\u7684\u5bb9\u5668\u90fd\u63d0\u4f9b\u4e86\uff08\u81f3\u5c11\uff09\u4e24\u79cd\u7c7b\u578b\u7684\u8fed\u4ee3\u5668\uff1a container::iterator \uff1a\u53ef\u4ee5\u8bfb\u5199\u7684\u8fed\u4ee3\u5668 container::const_iterator \uff1a \u53ea\u8bfb\u8fed\u4ee3\u5668 \u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u770b\u51e0\u4e2a\u4f7f\u7528\u8fed\u4ee3\u5668\u7684\u5b9e\u4f8b\u5427\u3002 \u904d\u5386 vector 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include int main () { std :: vector < int > vect ; for ( int count = 0 ; count < 6 ; ++ count ) vect . push_back ( count ); std :: vector < int >:: const_iterator it ; // \u58f0\u660e\u4e00\u4e2a\u53ea\u8bfb\u8fed\u4ee3\u5668 it = vect . cbegin (); // \u5c06\u8fed\u4ee3\u5668\u8d4b\u503c\u4e3a vector \u7684\u8d77\u70b9 while ( it != vect . cend ()) // \u5982\u679c it \u8fd8\u6ca1\u6709\u5230\u8fbe\u5bb9\u5668\u7684\u672b\u5c3e { std :: cout << * it << ' ' ; // \u6253\u5370 it \u6240\u6307\u7684\u5143\u7d20 ++ it ; // \u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5143\u7d20 } std :: cout << '\\n' ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 0 1 2 3 4 5 \u904d\u5386\u5217\u8868 \u904d\u5386\u5217\u8868\u4e5f\u662f\u7c7b\u4f3c\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include int main () { std :: list < int > li ; for ( int count = 0 ; count < 6 ; ++ count ) li . push_back ( count ); std :: list < int >:: const_iterator it ; // declare an iterator it = li . cbegin (); // assign it to the start of the list while ( it != li . cend ()) // while it hasn't reach the end { std :: cout << * it << ' ' ; // print the value of the element it points to ++ it ; // and iterate to the next element } std :: cout << '\\n' ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 0 1 2 3 4 5 \u53ef\u4ee5\u770b\u5230\uff0c\u5c3d\u7ba1 vector \u548c\u94fe\u8868\u7684\u5177\u4f53\u5b9e\u73b0\u622a\u7136\u4e0d\u540c\uff0c\u4f46\u662f\u904d\u5386\u5b83\u4eec\u7684\u4ee3\u7801\u5374\u51e0\u4e4e\u5b8c\u5168\u4e00\u6837\uff01 \u904d\u5386\u96c6\u5408 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u5305\u542b6\u4e2a\u6570\u7684\u96c6\u5408\uff0c\u7136\u540e\u518d\u4f7f\u7528\u8fed\u4ee3\u5668\u4f9d\u6b21\u6253\u5370\u5b83\u4eec\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include int main () { std :: set < int > myset ; myset . insert ( 7 ); myset . insert ( 2 ); myset . insert ( -6 ); myset . insert ( 8 ); myset . insert ( 1 ); myset . insert ( -4 ); std :: set < int >:: const_iterator it ; // declare an iterator it = myset . cbegin (); // assign it to the start of the set while ( it != myset . cend ()) // while it hasn't reach the end { std :: cout << * it << ' ' ; // print the value of the element it points to ++ it ; // and iterate to the next element } std :: cout << '\\n' ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 -6 -4 1 2 7 8 \u4ece\u8fd9\u4e2a\u4f8b\u5b50\u53ef\u4ee5\u770b\u51fa\uff0c\u867d\u7136\u586b\u5145\u96c6\u5408\u7684\u65b9\u6cd5\u548c\u586b\u5145vector\u4ee5\u53ca\u94fe\u8868\u5b8c\u5168\u4e0d\u540c\uff0c\u4f46\u662f\u4f7f\u7528\u8fed\u4ee3\u5668\u904d\u5386\u5b83\u4eec\u7684\u65b9\u6cd5\u5374\u662f\u4e00\u81f4\u7684\u3002 \u904d\u5386\u6620\u5c04 \u904d\u5386\u6620\u5c04\u7a0d\u5fae\u590d\u6742\u4e9b\u3002 Maps \u548c multimaps \u9700\u8981\u586b\u5145\u5143\u7d20\u5bf9(\u5b9a\u4e49\u4e3a std::pair )\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 make_pair() \u51fd\u6570\u5f88\u65b9\u4fbf\u7684\u521b\u5efa\u5143\u7d20\u5bf9\u3002 std::pair \u5141\u8bb8\u6211\u4eec\u901a\u8fc7 first \u548c second \u4e24\u4e2a\u6210\u5458\u53d8\u91cf\u8bbf\u95ee\u5143\u7d20\u5bf9\u3002 first \u662f\u952e\uff0c second \u662f\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include #include int main () { std :: map < int , std :: string > mymap ; mymap . insert ( std :: make_pair ( 4 , \"apple\" )); mymap . insert ( std :: make_pair ( 2 , \"orange\" )); mymap . insert ( std :: make_pair ( 1 , \"banana\" )); mymap . insert ( std :: make_pair ( 3 , \"grapes\" )); mymap . insert ( std :: make_pair ( 6 , \"mango\" )); mymap . insert ( std :: make_pair ( 5 , \"peach\" )); auto it { mymap . cbegin () }; // declare a const iterator and assign to start of vector while ( it != mymap . cend ()) // while it hasn't reach the end { std :: cout << it -> first << '=' << it -> second << ' ' ; // the value of the element it points to ++ it ; // and iterate to the next element } std :: cout << '\\n' ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 1=banana 2=orange 3=grapes 4=apple 5=peach 6=mango \u53ef\u4ee5\u770b\u5230\uff0c\u8fed\u4ee3\u5668\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u4f9d\u6b21\u904d\u5386\u5bb9\u5668\u4e2d\u7684\u5404\u4e2a\u5143\u7d20\uff0c\u6211\u4eec\u5b8c\u5168\u65e0\u9700\u64cd\u5fc3 map \u7a76\u7adf\u662f\u5982\u4f55\u5b58\u50a8\u8fd9\u4e9b\u5143\u7d20\u7684\uff01 \u5c0f\u7ed3 \u8fed\u4ee3\u5668\u63d0\u4f9b\u4e86\u4e00\u79cd\u65e0\u9700\u77e5\u9053\u5b9e\u73b0\u7ec6\u8282\u5c31\u53ef\u4ee5\u8f7b\u677e\u904d\u5386 \u5bb9\u5668\u7c7b \u7684\u65b9\u6cd5\u3002\u7ed3\u5408STL\u7b97\u6cd5\u548c\u5bb9\u5668\u7c7b\u7684\u6210\u5458\u51fd\u6570\uff0c\u8fed\u4ee3\u5668\u4f1a\u662f\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\u3002\u5728\u4e0b\u8282\u8bfe\u4e2d\uff0c\u4f60\u5c06\u4f1a\u770b\u5230\u5982\u4f55\u4f7f\u7528\u8fed\u4ee3\u5668\u5411\u94fe\u8868\u63d2\u5165\u5143\u7d20\uff08\u94fe\u8868\u6ca1\u6709\u63d0\u4f9b\u4e0b\u6807\u8fd0\u7b97\u7b26\u6240\u4ee5\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u5143\u7d20\uff09\u3002 \u8fd8\u6709\u4e00\u4ef6\u4e8b\u503c\u5f97\u6ce8\u610f\uff1a\u8fed\u4ee3\u5668\u5fc5\u987b\u88ab\u5b9e\u73b0\u4e3a\u7c7b\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u77e5\u9053\u7c7b\u7684\u5b9e\u73b0\u7ec6\u8282\u3002\u56e0\u6b64\uff0c\u8fed\u4ee3\u5668\u603b\u662f\u548c\u7279\u5b9a\u7684\u5bb9\u5668\u7c7b\u7ed1\u5b9a\u7684\u3002","title":"21.3 - STL\u8fed\u4ee3\u5668"},{"location":"21-3-STL-iterators-overview/#vector","text":"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include int main () { std :: vector < int > vect ; for ( int count = 0 ; count < 6 ; ++ count ) vect . push_back ( count ); std :: vector < int >:: const_iterator it ; // \u58f0\u660e\u4e00\u4e2a\u53ea\u8bfb\u8fed\u4ee3\u5668 it = vect . cbegin (); // \u5c06\u8fed\u4ee3\u5668\u8d4b\u503c\u4e3a vector \u7684\u8d77\u70b9 while ( it != vect . cend ()) // \u5982\u679c it \u8fd8\u6ca1\u6709\u5230\u8fbe\u5bb9\u5668\u7684\u672b\u5c3e { std :: cout << * it << ' ' ; // \u6253\u5370 it \u6240\u6307\u7684\u5143\u7d20 ++ it ; // \u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5143\u7d20 } std :: cout << '\\n' ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 0 1 2 3 4 5","title":"\u904d\u5386 vector"},{"location":"21-3-STL-iterators-overview/#_1","text":"\u904d\u5386\u5217\u8868\u4e5f\u662f\u7c7b\u4f3c\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include int main () { std :: list < int > li ; for ( int count = 0 ; count < 6 ; ++ count ) li . push_back ( count ); std :: list < int >:: const_iterator it ; // declare an iterator it = li . cbegin (); // assign it to the start of the list while ( it != li . cend ()) // while it hasn't reach the end { std :: cout << * it << ' ' ; // print the value of the element it points to ++ it ; // and iterate to the next element } std :: cout << '\\n' ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 0 1 2 3 4 5 \u53ef\u4ee5\u770b\u5230\uff0c\u5c3d\u7ba1 vector \u548c\u94fe\u8868\u7684\u5177\u4f53\u5b9e\u73b0\u622a\u7136\u4e0d\u540c\uff0c\u4f46\u662f\u904d\u5386\u5b83\u4eec\u7684\u4ee3\u7801\u5374\u51e0\u4e4e\u5b8c\u5168\u4e00\u6837\uff01","title":"\u904d\u5386\u5217\u8868"},{"location":"21-3-STL-iterators-overview/#_2","text":"\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u5305\u542b6\u4e2a\u6570\u7684\u96c6\u5408\uff0c\u7136\u540e\u518d\u4f7f\u7528\u8fed\u4ee3\u5668\u4f9d\u6b21\u6253\u5370\u5b83\u4eec\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include int main () { std :: set < int > myset ; myset . insert ( 7 ); myset . insert ( 2 ); myset . insert ( -6 ); myset . insert ( 8 ); myset . insert ( 1 ); myset . insert ( -4 ); std :: set < int >:: const_iterator it ; // declare an iterator it = myset . cbegin (); // assign it to the start of the set while ( it != myset . cend ()) // while it hasn't reach the end { std :: cout << * it << ' ' ; // print the value of the element it points to ++ it ; // and iterate to the next element } std :: cout << '\\n' ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 -6 -4 1 2 7 8 \u4ece\u8fd9\u4e2a\u4f8b\u5b50\u53ef\u4ee5\u770b\u51fa\uff0c\u867d\u7136\u586b\u5145\u96c6\u5408\u7684\u65b9\u6cd5\u548c\u586b\u5145vector\u4ee5\u53ca\u94fe\u8868\u5b8c\u5168\u4e0d\u540c\uff0c\u4f46\u662f\u4f7f\u7528\u8fed\u4ee3\u5668\u904d\u5386\u5b83\u4eec\u7684\u65b9\u6cd5\u5374\u662f\u4e00\u81f4\u7684\u3002","title":"\u904d\u5386\u96c6\u5408"},{"location":"21-3-STL-iterators-overview/#_3","text":"\u904d\u5386\u6620\u5c04\u7a0d\u5fae\u590d\u6742\u4e9b\u3002 Maps \u548c multimaps \u9700\u8981\u586b\u5145\u5143\u7d20\u5bf9(\u5b9a\u4e49\u4e3a std::pair )\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 make_pair() \u51fd\u6570\u5f88\u65b9\u4fbf\u7684\u521b\u5efa\u5143\u7d20\u5bf9\u3002 std::pair \u5141\u8bb8\u6211\u4eec\u901a\u8fc7 first \u548c second \u4e24\u4e2a\u6210\u5458\u53d8\u91cf\u8bbf\u95ee\u5143\u7d20\u5bf9\u3002 first \u662f\u952e\uff0c second \u662f\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include #include int main () { std :: map < int , std :: string > mymap ; mymap . insert ( std :: make_pair ( 4 , \"apple\" )); mymap . insert ( std :: make_pair ( 2 , \"orange\" )); mymap . insert ( std :: make_pair ( 1 , \"banana\" )); mymap . insert ( std :: make_pair ( 3 , \"grapes\" )); mymap . insert ( std :: make_pair ( 6 , \"mango\" )); mymap . insert ( std :: make_pair ( 5 , \"peach\" )); auto it { mymap . cbegin () }; // declare a const iterator and assign to start of vector while ( it != mymap . cend ()) // while it hasn't reach the end { std :: cout << it -> first << '=' << it -> second << ' ' ; // the value of the element it points to ++ it ; // and iterate to the next element } std :: cout << '\\n' ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 1=banana 2=orange 3=grapes 4=apple 5=peach 6=mango \u53ef\u4ee5\u770b\u5230\uff0c\u8fed\u4ee3\u5668\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u4f9d\u6b21\u904d\u5386\u5bb9\u5668\u4e2d\u7684\u5404\u4e2a\u5143\u7d20\uff0c\u6211\u4eec\u5b8c\u5168\u65e0\u9700\u64cd\u5fc3 map \u7a76\u7adf\u662f\u5982\u4f55\u5b58\u50a8\u8fd9\u4e9b\u5143\u7d20\u7684\uff01","title":"\u904d\u5386\u6620\u5c04"},{"location":"21-3-STL-iterators-overview/#_4","text":"\u8fed\u4ee3\u5668\u63d0\u4f9b\u4e86\u4e00\u79cd\u65e0\u9700\u77e5\u9053\u5b9e\u73b0\u7ec6\u8282\u5c31\u53ef\u4ee5\u8f7b\u677e\u904d\u5386 \u5bb9\u5668\u7c7b \u7684\u65b9\u6cd5\u3002\u7ed3\u5408STL\u7b97\u6cd5\u548c\u5bb9\u5668\u7c7b\u7684\u6210\u5458\u51fd\u6570\uff0c\u8fed\u4ee3\u5668\u4f1a\u662f\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\u3002\u5728\u4e0b\u8282\u8bfe\u4e2d\uff0c\u4f60\u5c06\u4f1a\u770b\u5230\u5982\u4f55\u4f7f\u7528\u8fed\u4ee3\u5668\u5411\u94fe\u8868\u63d2\u5165\u5143\u7d20\uff08\u94fe\u8868\u6ca1\u6709\u63d0\u4f9b\u4e0b\u6807\u8fd0\u7b97\u7b26\u6240\u4ee5\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u5143\u7d20\uff09\u3002 \u8fd8\u6709\u4e00\u4ef6\u4e8b\u503c\u5f97\u6ce8\u610f\uff1a\u8fed\u4ee3\u5668\u5fc5\u987b\u88ab\u5b9e\u73b0\u4e3a\u7c7b\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u77e5\u9053\u7c7b\u7684\u5b9e\u73b0\u7ec6\u8282\u3002\u56e0\u6b64\uff0c\u8fed\u4ee3\u5668\u603b\u662f\u548c\u7279\u5b9a\u7684\u5bb9\u5668\u7c7b\u7ed1\u5b9a\u7684\u3002","title":"\u5c0f\u7ed3"},{"location":"21-4-STL-algorithms-overview/","tags":["stl","containers"],"text":"Key Takeaway \u7b97\u6cd5\u90fd\u662f\u914d\u5408\u8fed\u4ee3\u5668\u4f7f\u7528\u7684\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u4e00\u822c\u4e5f\u662f\u8fed\u4ee3\u5668 \u9664\u4e86 \u5bb9\u5668\u7c7b \u548c \u8fed\u4ee3\u5668 \uff0cSTL\u8fd8\u63d0\u4f9b\u4e86\u5f88\u591a \u6cdb\u578b\u7b97\u6cd5 \u6765\u914d\u5408\u5bb9\u5668\u7c7b\u4e2d\u7684\u5143\u7d20\u4f7f\u7528\uff0c\u5bf9\u5b83\u4eec\u8fdb\u884c\u641c\u7d22\u3001\u6392\u5e8f\u3001\u63d2\u5165\u3001\u91cd\u6392\u3001\u5220\u9664\u548c\u62f7\u8d1d\u3002 \u6ce8\u610f\uff0c\u8fd9\u4e9b\u7b97\u6cd5\u90fd\u88ab\u5b9e\u73b0\u4e3a\u4f7f\u7528\u8fed\u4ee3\u5668\u8fdb\u884c\u64cd\u4f5c\u7684\u51fd\u6570\u3002\u8fd9\u610f\u5473\u7740\u6bcf\u4e2a\u7b97\u6cd5\u90fd\u53ea\u9700\u8981\u5b9e\u73b0\u4e00\u6b21\uff0c\u5c31\u53ef\u4ee5\u914d\u5408\u6240\u6709\u63d0\u4f9b\u8fed\u4ee3\u5668\u7684\u5bb9\u5668\u4f7f\u7528\uff08\u5305\u62ec\u4f60\u81ea\u5b9a\u4e49\u7684\u7c7b\uff09\u3002\u5c3d\u7ba1\u8fd9\u4e9b\u7b97\u6cd5\u975e\u5e38\u5f3a\u5927\u800c\u4e14\u53ef\u4ee5\u65b9\u4fbf\u5730\u7f16\u5199\u590d\u6742\u7684\u4ee3\u7801\u903b\u8f91\uff0c\u4f46\u662f\u5b83\u4e5f\u6709\u7f3a\u70b9\uff1a\u67d0\u4e9b\u7b97\u6cd5\u548c\u5bb9\u5668\u7c7b\u578b\u7684\u7ec4\u5408\u53ef\u80fd\u65e0\u6cd5\u5de5\u4f5c\uff0c\u6709\u4e9b\u53ef\u80fd\u5bfc\u81f4\u6b7b\u5faa\u73af\uff0c\u6216\u8005\u6027\u80fd\u6781\u5dee\u3002\u6240\u4ee5\u5728\u4f7f\u7528\u65f6\u9700\u8981\u4f60\u81ea\u5df1\u8003\u8651\u6e05\u695a\u5e76\u627f\u62c5\u76f8\u5e94\u7684\u98ce\u9669\u3002 STL \u63d0\u4f9b\u4e86\u975e\u5e38\u591a\u7684\u7b97\u6cd5\u2014\u2014\u6211\u4eec\u53ea\u4f1a\u5728\u6b64\u4ecb\u7ecd\u4e00\u4e9b\u975e\u5e38\u5e38\u89c1\u7684\u3002\u5269\u4e0b\u7684\u7b97\u6cd5\u4f1a\u5728STL\u7b97\u6cd5\u7ae0\u8282\u4e2d\u8fdb\u884c\u4ecb\u7ecd \u4f7f\u7528STL\u7b97\u6cd5\uff0c\u53ea\u9700\u8981\u6dfb\u52a0algorithm\u5934\u6587\u4ef6\u5373\u53ef\u3002 min_element \u548c max_element std::min_element \u548c std::max_element \u7b97\u6cd5\u7528\u4e8e\u67e5\u627e\u5bb9\u5668 \u5bb9\u5668\u7c7b \u4e2d\u7684\u6700\u5927\u5143\u7d20\u548c\u6700\u5c0f\u5143\u7d20\u3002 std::iota \u53ef\u4ee5\u751f\u6210\u4e00\u7ec4\u8fde\u7eed\u7684\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // std::min_element and std::max_element #include #include #include // std::iota int main () { std :: list < int > li ( 6 ); // \u4f7f\u7528\u6570\u5b57\u586b\u5145 li \uff0c\u4ece0\u5f00\u59cb std :: iota ( li . begin (), li . end (), 0 ); std :: cout << * std :: min_element ( li . begin (), li . end ()) << ' ' << * std :: max_element ( li . begin (), li . end ()) << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 0 5 find \u548c list::insert \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c std::find() \u7b97\u6cd5\u7528\u4e8e\u5728\u94fe\u8868\u4e2d\u8fdb\u884c\u67e5\u627e\uff0c\u7136\u540e\u4f7f\u7528 list::insert() \u51fd\u6570\u5411\u94fe\u8868\u4e2d\u7684\u8be5\u4f4d\u7f6e\u63d2\u5165\u4e00\u4e2a\u65b0\u7684\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include #include #include int main () { std :: list < int > li ( 6 ); std :: iota ( li . begin (), li . end (), 0 ); // \u5728\u94fe\u8868\u4e2d\u627e\u5230 3 auto it { std :: find ( li . begin (), li . end (), 3 ) }; // \u5c06 8 \u63d2\u5165\u5230 3 \u7684\u524d\u9762 li . insert ( it , 8 ); for ( int i : li ) // \u57fa\u4e8e\u8fed\u4ee3\u5668\u7684for\u5faa\u73af std :: cout << i << ' ' ; std :: cout << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 0 1 2 8 3 4 5 \u5982\u679c\u7b97\u6cd5\u6ca1\u6709\u627e\u5230\u76ee\u6807\u503c\uff0c\u5b83\u4f1a\u8fd4\u56deend\u8fed\u4ee3\u5668\u3002\u5982\u679c\u6211\u4eec\u4e0d\u786e\u5b9a3\u662f\u5426\u5728 li \u4e2d\uff0c\u5219\u6700\u597d\u5148\u901a\u8fc7 std::find \u67e5\u627e\u5e76\u901a\u8fc7\u8fd4\u56de\u7684\u8fed\u4ee3\u5668\u786e\u8ba4\u662f\u5426\u627e\u5230\uff0c\u7136\u540e\u518d\u4f7f\u7528\u8be5\u8fed\u4ee3\u5668\u3002 1 2 3 4 5 6 7 8 if ( it == li . end ()) { std :: cout << \"3 was not found \\n \" ; } else { // ... } sort \u548c reverse \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f1a\u9996\u5148\u5bf9vector\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u518d\u5c06\u5176\u9006\u5e8f\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include #include #include int main () { std :: vector < int > vect { 7 , -3 , 6 , 2 , -5 , 0 , 4 }; // \u6392\u5e8f std :: sort ( vect . begin (), vect . end ()); for ( int i : vect ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; // \u9006\u5e8f std :: reverse ( vect . begin (), vect . end ()); for ( int i : vect ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 2 -5 -3 0 2 4 6 7 7 6 4 2 0 -3 -5 \u6211\u4eec\u4e5f\u53ef\u4ee5\u5c06\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u6bd4\u8f83\u51fd\u6570\u4f5c\u4e3a std::sort \u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u4f20\u5165\u3002\u5728 \u5934\u6587\u4ef6\u4e2d\u4e5f\u63d0\u4f9b\u4e86\u4e0d\u5c11\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7684\u6b64\u7c7b\u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u5c06 std::greater \u4f20\u5165 std::sort \uff0c\u7136\u540e\u4e0d\u8981\u4f7f\u7528 std::reverse \u3002\u6b64\u65f6 vector \u4e5f\u662f\u4ece\u5927\u5230\u5c0f\u6392\u5e8f\u7684\u3002 \u6ce8\u610f std::sort() \u4e0d\u80fd\u914d\u5408\u94fe\u8868\u4f7f\u7528\uff0c\u94fe\u8868\u63d0\u4f9b\u4e86\u81ea\u5df1\u7684 sort() \u6210\u5458\u51fd\u6570\uff0c\u5b83\u7684\u6548\u7387\u8981\u6bd4\u6cdb\u578b\u7684\u6392\u5e8f\u9ad8\u7684\u591a\u3002 \u5c0f\u7ed3 \u5c3d\u7ba1\u672c\u6587\u53ea\u662f\u7b80\u5355\u5730\u4ecb\u7ecd\u4e86STL\u4e2d\u63d0\u4f9b\u7684\u4e00\u4e9b\u7b97\u6cd5\uff0c\u5b83\u8db3\u4ee5\u5411\u4f60\u8bc1\u660e\u914d\u5408\u8fed\u4ee3\u5668\u548c\u5bb9\u5668\u4f7f\u7528\u8fd9\u4e9b\u7b97\u6cd5\u662f\u591a\u4e48\u7684\u7b80\u5355\u3002\u8981\u60f3\u8bb2\u5b8c\u5269\u4e0b\u7684\u7b97\u6cd5\uff0c\u90a3\u5f97\u9700\u8981\u4e00\u6574\u4e2a\u7ae0\u8282\u624d\u884c\u5462\uff01","title":"21.4 - STL\u7b97\u6cd5"},{"location":"21-4-STL-algorithms-overview/#min_element-max_element","text":"std::min_element \u548c std::max_element \u7b97\u6cd5\u7528\u4e8e\u67e5\u627e\u5bb9\u5668 \u5bb9\u5668\u7c7b \u4e2d\u7684\u6700\u5927\u5143\u7d20\u548c\u6700\u5c0f\u5143\u7d20\u3002 std::iota \u53ef\u4ee5\u751f\u6210\u4e00\u7ec4\u8fde\u7eed\u7684\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // std::min_element and std::max_element #include #include #include // std::iota int main () { std :: list < int > li ( 6 ); // \u4f7f\u7528\u6570\u5b57\u586b\u5145 li \uff0c\u4ece0\u5f00\u59cb std :: iota ( li . begin (), li . end (), 0 ); std :: cout << * std :: min_element ( li . begin (), li . end ()) << ' ' << * std :: max_element ( li . begin (), li . end ()) << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 0 5","title":"min_element \u548c max_element"},{"location":"21-4-STL-algorithms-overview/#find-listinsert","text":"\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c std::find() \u7b97\u6cd5\u7528\u4e8e\u5728\u94fe\u8868\u4e2d\u8fdb\u884c\u67e5\u627e\uff0c\u7136\u540e\u4f7f\u7528 list::insert() \u51fd\u6570\u5411\u94fe\u8868\u4e2d\u7684\u8be5\u4f4d\u7f6e\u63d2\u5165\u4e00\u4e2a\u65b0\u7684\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include #include #include int main () { std :: list < int > li ( 6 ); std :: iota ( li . begin (), li . end (), 0 ); // \u5728\u94fe\u8868\u4e2d\u627e\u5230 3 auto it { std :: find ( li . begin (), li . end (), 3 ) }; // \u5c06 8 \u63d2\u5165\u5230 3 \u7684\u524d\u9762 li . insert ( it , 8 ); for ( int i : li ) // \u57fa\u4e8e\u8fed\u4ee3\u5668\u7684for\u5faa\u73af std :: cout << i << ' ' ; std :: cout << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 0 1 2 8 3 4 5 \u5982\u679c\u7b97\u6cd5\u6ca1\u6709\u627e\u5230\u76ee\u6807\u503c\uff0c\u5b83\u4f1a\u8fd4\u56deend\u8fed\u4ee3\u5668\u3002\u5982\u679c\u6211\u4eec\u4e0d\u786e\u5b9a3\u662f\u5426\u5728 li \u4e2d\uff0c\u5219\u6700\u597d\u5148\u901a\u8fc7 std::find \u67e5\u627e\u5e76\u901a\u8fc7\u8fd4\u56de\u7684\u8fed\u4ee3\u5668\u786e\u8ba4\u662f\u5426\u627e\u5230\uff0c\u7136\u540e\u518d\u4f7f\u7528\u8be5\u8fed\u4ee3\u5668\u3002 1 2 3 4 5 6 7 8 if ( it == li . end ()) { std :: cout << \"3 was not found \\n \" ; } else { // ... }","title":"find \u548c list::insert"},{"location":"21-4-STL-algorithms-overview/#sort-reverse","text":"\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f1a\u9996\u5148\u5bf9vector\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u518d\u5c06\u5176\u9006\u5e8f\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include #include #include int main () { std :: vector < int > vect { 7 , -3 , 6 , 2 , -5 , 0 , 4 }; // \u6392\u5e8f std :: sort ( vect . begin (), vect . end ()); for ( int i : vect ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; // \u9006\u5e8f std :: reverse ( vect . begin (), vect . end ()); for ( int i : vect ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 2 -5 -3 0 2 4 6 7 7 6 4 2 0 -3 -5 \u6211\u4eec\u4e5f\u53ef\u4ee5\u5c06\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u6bd4\u8f83\u51fd\u6570\u4f5c\u4e3a std::sort \u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u4f20\u5165\u3002\u5728 \u5934\u6587\u4ef6\u4e2d\u4e5f\u63d0\u4f9b\u4e86\u4e0d\u5c11\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7684\u6b64\u7c7b\u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u5c06 std::greater \u4f20\u5165 std::sort \uff0c\u7136\u540e\u4e0d\u8981\u4f7f\u7528 std::reverse \u3002\u6b64\u65f6 vector \u4e5f\u662f\u4ece\u5927\u5230\u5c0f\u6392\u5e8f\u7684\u3002 \u6ce8\u610f std::sort() \u4e0d\u80fd\u914d\u5408\u94fe\u8868\u4f7f\u7528\uff0c\u94fe\u8868\u63d0\u4f9b\u4e86\u81ea\u5df1\u7684 sort() \u6210\u5458\u51fd\u6570\uff0c\u5b83\u7684\u6548\u7387\u8981\u6bd4\u6cdb\u578b\u7684\u6392\u5e8f\u9ad8\u7684\u591a\u3002","title":"sort \u548c reverse"},{"location":"21-4-STL-algorithms-overview/#_1","text":"\u5c3d\u7ba1\u672c\u6587\u53ea\u662f\u7b80\u5355\u5730\u4ecb\u7ecd\u4e86STL\u4e2d\u63d0\u4f9b\u7684\u4e00\u4e9b\u7b97\u6cd5\uff0c\u5b83\u8db3\u4ee5\u5411\u4f60\u8bc1\u660e\u914d\u5408\u8fed\u4ee3\u5668\u548c\u5bb9\u5668\u4f7f\u7528\u8fd9\u4e9b\u7b97\u6cd5\u662f\u591a\u4e48\u7684\u7b80\u5355\u3002\u8981\u60f3\u8bb2\u5b8c\u5269\u4e0b\u7684\u7b97\u6cd5\uff0c\u90a3\u5f97\u9700\u8981\u4e00\u6574\u4e2a\u7ae0\u8282\u624d\u884c\u5462\uff01","title":"\u5c0f\u7ed3"},{"location":"22-1-std-string-and-std-wstring/","tags":["string","wstring"],"text":"Key Takeaway C++\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u5f88\u591a\u6709\u7528\u7684\u7c7b\uff0c\u4f46\u662f\u5176\u4e2d\u6700\u6709\u7528\u7684\uff0c\u53ef\u80fd\u975e std::string \u83ab\u5c5e\u4e86\u3002 std::string (\u548c std::wstring ) \u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b\uff0c\u63d0\u4f9b\u4e86\u5b57\u7b26\u4e32\u76f8\u5173\u7684\u591a\u79cd\u64cd\u4f5c\uff0c\u5305\u62ec\u8d4b\u503c\u3001\u6bd4\u8f83\u548c\u4fee\u6539\uff0c\u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u4f1a\u4e00\u8d77\u6df1\u5165\u5b66\u4e60\u5b57\u7b26\u4e32\u7c7b\u3002 \u6ce8\u610f\uff1a\u6211\u4eec\u5c06C\u8bed\u8a00\u4e2d\u7684\u5b57\u7b26\u4e32\u79f0\u4e3aC\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u800c\u5c06 std::string (\u548c std::wstring \u79f0\u4e3a\u5b57\u7b26\u4e32)\u3002 \u4f5c\u8005\u6ce8 \u6ce8\u610f\uff0c\u672c\u7ae0\u8282\u7684\u5185\u5bb9\u7a0d\u5fae\u6709\u4e9b\u8fc7\u65f6\uff0c\u800c\u4e14\u5f88\u53ef\u80fd\u4f1a\u5728\u5c06\u6765\u8fdb\u884c\u66f4\u65b0\u3002\u4f60\u53ef\u4ee5\u6d4f\u89c8\u4e00\u4e0b\u672c\u7ae0\u8282\uff0c\u91cd\u70b9\u5173\u6ce8\u6709\u5173\u5b57\u7b26\u4e32\u7684\u6838\u5fc3\u601d\u60f3\u548c\u4f8b\u5b50\u3002\u4f46\u5982\u679c\u662f\u4f5c\u4e3a\u53c2\u8003\u6587\u6863\uff0c\u4e13\u95e8\u7684\u53c2\u8003\u7f51\u7ad9(\u4f8b\u5982 cppreference ) \u4f1a\u4f7f\u66f4\u597d\u7684\u9009\u62e9\uff0c\u5728\u90a3\u91cc\u4f60\u53ef\u4ee5\u627e\u5230\u66f4\u65b0\u3001\u66f4\u51c6\u786e\u7684\u4fe1\u606f\u3002 \u8bbe\u8ba1\u5b57\u7b26\u4e32\u7c7b\u7684\u521d\u8877 \u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86 C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 \uff0c\u5b83\u7684\u672c\u8d28\u5c31\u662f\u4e00\u4e2a\u5b57\u7b26\u6570\u7ec4\uff0c\u5b58\u653e\u4e86\u7ec4\u6210\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u3002\u5982\u679c\u4f60\u66fe\u7ecf\u4f7f\u7528\u8fc7C\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u4f60\u80af\u5b9a\u4e5f\u4f1a\u89c9\u5f97\u5b83\u7528\u8d77\u6765\u5f88\u56f0\u96be\u3001\u5bb9\u6613\u51fa\u9519\u800c\u4e14\u4e0d\u5bb9\u6613\u8c03\u8bd5\u3002 C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u6709\u5f88\u591a\u7f3a\u9677\uff0c\u8fd9\u4e3b\u8981\u662f\u56e0\u4e3a\u4f60\u5fc5\u987b\u81ea\u5df1\u7ba1\u7406\u5b83\u7684\u5185\u5b58\u3002\u6bd4\u65b9\u8bf4\uff0c\u5982\u679c\u4f60\u9700\u8981\u5c06\u5b57\u7b26\u4e32\"hello\"\u8d4b\u503c\u5230\u4e00\u4e2a\u7f13\u5b58\u4e2d\uff0c\u5219\u4f60\u5fc5\u987b\u9996\u5148\u5206\u914d\u4e00\u4e2a\u957f\u5ea6\u6b63\u786e\u7684\u7f13\u5b58\uff1a 1 char * strHello { new char [ 7 ] }; \u4e0d\u8981\u5fd8\u8bb0\u5728\u8ba1\u7b97\u957f\u5ea6\u65f6\u591a\u52a0\u4e00\u4e2a\u7ed3\u675f\u7b26\u7684\u957f\u5ea6\uff01 \u7136\u540e\u4f60\u9700\u8981\u5c06\u503c\u62f7\u8d1d\u5230\u8fd9\u5757\u7f13\u5b58\u4e2d\uff1a 1 strcpy ( strHello , \"hello!\" ); \u53ea\u6709\u5f53\u4f60\u8ba1\u7b97\u7684\u7f13\u5b58\u957f\u5ea6\u6b63\u786e\u65f6\uff0c\u624d\u4e0d\u4f1a\u51fa\u73b0\u7f13\u51b2\u533a\u6ea2\u51fa\u3002 \u5f53\u7136\uff0c\u56e0\u4e3a\u5b57\u7b26\u4e32\u662f\u52a8\u6001\u5206\u914d\u7684\uff0c\u6240\u4ee5\u4f60\u8fd8\u5fc5\u987b\u8bb0\u5f97\u5728\u4f7f\u7528\u5b8c\u6210\u540e\u6b63\u786e\u5730\u91ca\u653e\u5b83\uff1a 1 delete [] strHello ; \u4e0d\u8981\u5fd8\u8bb0\u4f7f\u7528\u6570\u7ec4delete\u800c\u4e0d\u662f\u666e\u901adelete\u3002 \u6b64\u5916\uff0cC\u8bed\u8a00\u63d0\u4f9b\u7684\u8bb8\u591a\u7528\u4e8e\u5904\u7406\u6570\u5b57\u7684\u64cd\u4f5c\u7b26\uff0c\u4f8b\u5982\u8d4b\u503c\u548c\u6bd4\u8f83\uff0c\u6839\u672c\u65e0\u6cd5\u7528\u4e8e\u5904\u7406C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002\u6709\u65f6\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u770b\u4f3c\u6709\u6548\uff0c\u4f46\u5b9e\u9645\u4e0a\u4f1a\u4ea7\u751f\u4e0d\u6b63\u786e\u7684\u7ed3\u679c\u2014\u2014\u4f8b\u5982\uff0c\u4f7f\u7528 == \u6bd4\u8f83\u4e24\u4e2aC\u98ce\u683c\u5b57\u7b26\u4e32\u65f6\uff0c\u5b9e\u9645\u4e0a\u6bd4\u8f83\u7684\u662f\u4e24\u4e2a\u6307\u9488\uff0c\u800c\u975e\u5b57\u7b26\u4e32\u672c\u8eab\u3002\u4f7f\u7528 operator= \u5c06\u4e00\u4e2aC\u98ce\u683c\u5b57\u7b26\u4e32\u8d4b\u503c\u7ed9\u53e6\u4e00\u4e2aC\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u4e4d\u4e00\u770b\u4f3c\u4e4e\u53ef\u884c\uff0c\u4f46\u5b9e\u9645\u4e0a\u662f\u5728\u8fdb\u884c\u6307\u9488\u7684 \u6d45\u62f7\u8d1d \uff0c\u8fd9\u901a\u5e38\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c\u3002\u8fd9\u7c7b\u4e8b\u60c5\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\uff0c\u800c\u4e14\u5f88\u96be\u53d1\u73b0\u548c\u8c03\u8bd5\uff01 \u5f52\u6839\u7ed3\u5e95\uff0c\u4f7f\u7528C\u98ce\u683c\u5b57\u7b26\u4e32\u9700\u8981\u8bb0\u4f4f\u8bb8\u591a\u5173\u4e8e\u5b89\u5168/\u4e0d\u5b89\u5168\u7684\u89c4\u5219\uff0c\u8bb0\u4f4f\u4e00\u5806\u5177\u6709\u6709\u8da3\u540d\u79f0\u7684\u51fd\u6570\uff0c\u5982 strcat() \u548c strcmp() \uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u76f4\u89c2\u7684\u64cd\u4f5c\u7b26\uff0c\u800c\u4e14\u8fd8\u9700\u8981\u8fdb\u884c\u5927\u91cf\u7684\u624b\u52a8\u5185\u5b58\u7ba1\u7406\u3002 \u5e78\u8fd0\u7684\u662f\uff0cC++\u548c\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u5904\u7406\u5b57\u7b26\u4e32\u7684\u66f4\u597d\u7684\u65b9\u6cd5\uff1a std::string \u548c std::wstring \u7c7b\u3002\u901a\u8fc7\u4f7f\u7528\u6784\u9020\u51fd\u6570\u3001\u6790\u6784\u51fd\u6570\u548c\u64cd\u4f5c\u7b26\u91cd\u8f7d\u7b49C++\u6982\u5ff5\uff0c std::string \u4f7f\u6211\u4eec\u53ef\u4ee5\u4ee5\u4e00\u79cd\u76f4\u89c2\u548c\u5b89\u5168\u7684\u65b9\u5f0f\u521b\u5efa\u548c\u64cd\u4f5c\u5b57\u7b26\u4e32\uff01\u4e0d\u518d\u9700\u8981\u5185\u5b58\u7ba1\u7406\uff0c\u4e0d\u518d\u9700\u8981\u5947\u602a\u7684\u51fd\u6570\u540d\uff0c\u4e5f\u5927\u5927\u964d\u4f4e\u4e86\u53d1\u751f\u95ee\u9898\u7684\u53ef\u80fd\u6027\u3002 \u5b57\u7b26\u4e32\u7b80\u4ecb \u6240\u6709\u6807\u51c6\u5e93\u5b57\u7b26\u4e32\u51fd\u6570\u90fd\u4f4d\u4e8e\u5934\u6587\u4ef6\u4e2d\uff0c\u4f7f\u7528\u65f6\u53ea\u9700\u8981\u5305\u542b\u8be5\u5934\u6587\u4ef6\u5373\u53ef\uff1a 1 #include \u5728\u5b57\u7b26\u4e32\u5934\u6587\u4ef6\u4e2d\u5b9e\u9645\u4e0a\u67093\u4e2a\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\u7c7b\u3002\u7b2c\u4e00\u4e2a\u662f\u540d\u4e3a basic_string \u7684\u6a21\u677f\u57fa\u7c7b\uff1a 1 2 3 4 5 namespace std { template < class charT , class traits = char_traits < charT > , class Allocator = allocator < charT > > class basic_string ; } \u6211\u4eec\u5e76\u4e0d\u4f1a\u76f4\u63a5\u4f7f\u7528\u8fd9\u4e2a\u7c7b\uff0c\u6240\u4ee5\u6682\u65f6\u4e0d\u8981\u62c5\u5fc3 trait \u6216 Allocator \u8fd9\u4e9b\u4e71\u4e03\u516b\u7cdf\u7684\u60c5\u51b5\u3002\u5728\u51e0\u4e4e\u6240\u6709\u53ef\u4ee5\u60f3\u8c61\u5230\u7684\u60c5\u51b5\u4e0b\uff0c\u9ed8\u8ba4\u503c\u5c31\u8db3\u591f\u4e86\u3002 \u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u4e24\u79cd\u7c7b\u578b\u7684 basic_string \uff1a 1 2 3 4 5 namespace std { typedef basic_string < char > string ; typedef basic_string < wchar_t > wstring ; } \u8fd9\u662f\u4f60\u5c06\u5b9e\u9645\u4f7f\u7528\u7684\u4e24\u4e2a\u7c7b\u3002 string \u7528\u4e8e\u6807\u51c6\u7684ASCII\u548cutf-8\u5b57\u7b26\u4e32\u3002 wstring \u7528\u4e8e\u5bbd\u5b57\u7b26/unicode (utf-16)\u5b57\u7b26\u4e32\u3002\u4f46\u662f\u5e76\u6ca1\u6709\u7528\u4e8eutf-32\u5b57\u7b26\u4e32\u7684\u5185\u7f6e\u7c7b(\u4e0d\u8fc7\uff0c\u5982\u679c\u9700\u8981\u7684\u8bdd\uff0c\u60a8\u5e94\u8be5\u80fd\u591f\u4ece basic_string \u81ea\u5df1\u6269\u5c55)\u3002 \u5c3d\u7ba1\u4f60\u4f1a\u76f4\u63a5\u4f7f\u7528\u7684\u662f std::string \u548c std::wstring \uff0c\u4f46\u6240\u6709\u7684\u5b57\u7b26\u4e32\u529f\u80fd\u90fd\u662f\u5728 basic_string \u7c7b\u4e2d\u5b9e\u73b0\u7684\u3002 string \u548c wstring \u80fd\u591f\u76f4\u63a5\u8bbf\u95ee\u8fd9\u4e9b\u529f\u80fd\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u6a21\u677f\u5316\u7684\u3002\u56e0\u6b64\uff0c\u6240\u63d0\u4f9b\u7684\u6240\u6709\u51fd\u6570\u90fd\u9002\u7528\u4e8e string \u548c wstring \u3002\u4f46\u662f\uff0c\u7531\u4e8e basic_string \u662f\u4e00\u4e2a\u6a21\u677f\u5316\u7c7b\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u5f53\u4f60\u5bf9 string \u6216 wstring \u8fdb\u884c\u8bed\u6cd5\u9519\u8bef\u64cd\u4f5c\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u4ea7\u751f\u53ef\u6015\u7684\u6a21\u677f\u9519\u8bef\u3002\u4e0d\u8981\u88ab\u8fd9\u4e9b\u9519\u8bef\u5413\u5012\uff1a\u5b83\u4eec\u53ea\u662f\u770b\u8d77\u6765\u6bd4\u8f83\u5413\u4eba\u7f62\u4e86\u3002 \u8fd9\u662f string \u7c7b\u4e2d\u6240\u6709\u51fd\u6570\u7684\u5217\u8868\u3002\u8fd9\u4e9b\u51fd\u6570\u4e2d\u7684\u5927\u591a\u6570\u90fd\u6709\u591a\u79cd\u53d8\u79cd\u7528\u4e8e\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u8f93\u5165\uff0c\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\u66f4\u6df1\u5165\u5730\u8ba8\u5b83\u4eec\u7684\u5177\u4f53\u7528\u6cd5\u3002 \u6784\u5efa\u548c\u6790\u6784 Function Effect (constructor) \u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u62f7\u8d1d (destructor) \u9500\u6bc1\u5b57\u7b26\u4e32 \u957f\u5ea6\u548c\u5bb9\u91cf Function Effect capacity() \u5728\u4e0d\u91cd\u65b0\u5206\u914d\u5185\u5b58\u60c5\u51b5\u4e0b\uff0c\u80fd\u591f\u5b58\u653e\u7684\u6700\u591a\u7684\u5b57\u7b26\u4e2a\u6570 empty() \u8fd4\u56de\u5e03\u5c14\u503c\u8868\u793a\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u7a7a\u5b57\u7b26\u4e32 length(), size() \u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u4e2a\u6570 max_size() \u8fd4\u56de\u53ef\u4ee5\u5206\u914d\u7684\u6700\u957f\u7684\u5b57\u7b26\u4e32\u5927\u5c0f reserve() \u6269\u5927\u6216\u7f29\u5c0f\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf \u5143\u7d20\u8bbf\u95ee Function Effect [] , at() \u4f7f\u7528\u7d22\u5f15\u8bbf\u95ee\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26 \u4fee\u6539\u5b57\u7b26\u4e32 Function Effect =, assign() \u5b57\u7b26\u4e32\u8d4b\u503c +=, append(), push_back() \u5b57\u7b26\u4e32\u672b\u5c3e\u6dfb\u52a0 insert() \u5728\u4efb\u610f\u7d22\u5f15\u4f4d\u7f6e\u63d2\u5165 clear() \u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u7b26 erase() \u6e05\u9664\u5b57\u7b26 replace() \u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u4e00\u90e8\u5206 resize() \u8c03\u6574\u5b57\u7b26\u4e32\u5927\u5c0f(\u5728\u672b\u5c3e\u622a\u65ad\u6216\u6dfb\u52a0) swap() \u4ea4\u6362\u4e24\u4e2a\u5b57\u7b26\u4e32 \u8f93\u5165\u8f93\u51fa Function Effect >> , getline() \u4ece\u8f93\u5165\u6d41\u4e2d\u83b7\u53d6\u8f93\u5165 << \u5c06\u5b57\u7b26\u4e32\u8f93\u51fa\u5230\u8f93\u51fa\u6d41 c_str() \u8fd4\u56de\u4ee5\u7a7a\u5b57\u7b26\u7ed3\u5c3e\u7684C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 copy() \u62f7\u8d1d\u5b57\u7b26\u4e32\u5185\u5bb9\u5230\u5b57\u7b26\u6570\u7ec4(\u4e0d\u5305\u542b\u7ed3\u675f\u7b26) data() \u7c7b\u4f3c c_str() \u3002\u975econst\u91cd\u8f7d\uff0c\u5141\u8bb8\u5199\u5165\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u3002 \u5b57\u7b26\u4e32\u6bd4\u8f83 Function Effect == , != \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\uff08\u8fd4\u56de\u5e03\u5c14\u503c\uff09 < , <= , > , >= \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u5927\u5c0f\u5173\u7cfb\uff08\u8fd4\u56de\u5e03\u5c14\u503c\uff09 compare() \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u76f8\u7b49(\u8fd4\u56de -1, 0, \u6216 1) \u5b50\u4e32\u548c\u62fc\u63a5 Function Effect + \u8fde\u63a5\u4e24\u4e2a\u5b57\u7b26\u4e32 substr() \u8fd4\u56de\u4e00\u4e2a\u5b50\u4e32 \u641c\u7d22 Function Effect find() \u641c\u7d22\u5b57\u7b26\u3001\u5b50\u4e32\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u4f4d\u7f6e find_first_of() \u627e\u5230\u5b57\u7b26\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u4f4d\u7f6e find_first_not_of() \u627e\u5230\u7b2c\u4e00\u4e2a\u4e0d\u5728\u5b57\u7b26\u96c6\u4e2d\u7684\u5b57\u7b26\u51fa\u73b0\u7684\u4f4d\u7f6e find_last_of() \u627e\u5230\u5b57\u7b26\u96c6\u4e2d\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u51fa\u73b0\u7684\u7d22\u5f15\u4f4d\u7f6e find_last_not_of() \u627e\u5230\u6700\u540e\u4e00\u4e2a\u4e0d\u5728\u5b57\u7b26\u96c6\u4e2d\u7684\u5b57\u7b26\u51fa\u73b0\u7684\u4f4d\u7f6e rfind() \u641c\u7d22\u5b57\u7b26\u3001\u5b50\u4e32\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u4f4d\u7f6e \u8fed\u4ee3\u5668\u548c\u5185\u5b58\u5206\u914d Function Effect begin() , end() \u6b63\u5411\u8fed\u4ee3\u5668\uff0c\u5f00\u5934/\u7ed3\u5c3e get_allocator() \u8fd4\u56de\u5206\u914d\u5668 rbegin() , rend() \u53cd\u5411\u8fed\u4ee3\u5668\uff0c\u5f00\u5934/\u7ed3\u5c3e \u867d\u7136\u6807\u51c6\u5e93\u5b57\u7b26\u4e32\u7c7b\u63d0\u4f9b\u4e86\u5f88\u591a\u529f\u80fd\uff0c\u4f46\u6709\u4e00\u4e9b\u660e\u663e\u7684\u9057\u6f0f\uff1a \u5bf9\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u652f\u6301\uff1b \u4ece\u6570\u5b57\u521b\u5efa\u5b57\u7b26\u4e32\u7684\u6784\u9020\u51fd\u6570\uff1b \u5927\u5c0f\u5199\u5b57\u6bcd\u8f6c\u6362\u51fd\u6570\uff1b \u5ffd\u7565\u5927\u5c0f\u5199\u7684\u5b57\u7b26\u4e32\u6bd4\u8f83\u65b9\u6cd5\uff1b \u5b57\u7b26\u4e32\u5206\u8bcd(Tokenization)\u540e\u5b58\u5165\u6570\u7ec4\uff1b \u83b7\u53d6\u5b57\u7b26\u4e32\u5de6\u534a\u90e8\u5206\u6216\u53f3\u534a\u90e8\u5206\u7684\u51fd\u6570\uff1b \u7a7a\u767d\u79fb\u9664\u51fd\u6570(trimming)\uff1b sprintf \u98ce\u683c\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u51fd\u6570\uff1b utf-8 \u548c utf-16 \u8f6c\u6362\u3002 \u5bf9\u4e8e\u5176\u4e2d\u7684\u5927\u591a\u6570\u529f\u80fd\uff0c\u4f60\u5fc5\u987b\u7f16\u5199\u81ea\u5df1\u7684\u51fd\u6570\uff0c\u6216\u8005\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aC\u98ce\u683c\u7684\u5b57\u7b26\u4e32(\u4f7f\u7528 c_str() )\uff0c\u5e76\u4f7f\u7528\u63d0\u4f9b\u6b64\u529f\u80fd\u7684C\u51fd\u6570\u3002 \u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u66f4\u6df1\u5165\u5730\u7814\u7a76\u5b57\u7b26\u4e32\u7c7b\u7684\u5404\u79cd\u529f\u80fd\u3002\u5c3d\u7ba1\u6211\u4eec\u5c06\u5728\u793a\u4f8b\u4e2d\u4f7f\u7528 string \uff0c\u4f46\u6240\u6709\u5185\u5bb9\u90fd\u540c\u6837\u9002\u7528\u4e8e wstring \u3002","title":"22.1 - std::string \u548c std::wstring"},{"location":"22-1-std-string-and-std-wstring/#_1","text":"\u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86 C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 \uff0c\u5b83\u7684\u672c\u8d28\u5c31\u662f\u4e00\u4e2a\u5b57\u7b26\u6570\u7ec4\uff0c\u5b58\u653e\u4e86\u7ec4\u6210\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u3002\u5982\u679c\u4f60\u66fe\u7ecf\u4f7f\u7528\u8fc7C\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u4f60\u80af\u5b9a\u4e5f\u4f1a\u89c9\u5f97\u5b83\u7528\u8d77\u6765\u5f88\u56f0\u96be\u3001\u5bb9\u6613\u51fa\u9519\u800c\u4e14\u4e0d\u5bb9\u6613\u8c03\u8bd5\u3002 C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u6709\u5f88\u591a\u7f3a\u9677\uff0c\u8fd9\u4e3b\u8981\u662f\u56e0\u4e3a\u4f60\u5fc5\u987b\u81ea\u5df1\u7ba1\u7406\u5b83\u7684\u5185\u5b58\u3002\u6bd4\u65b9\u8bf4\uff0c\u5982\u679c\u4f60\u9700\u8981\u5c06\u5b57\u7b26\u4e32\"hello\"\u8d4b\u503c\u5230\u4e00\u4e2a\u7f13\u5b58\u4e2d\uff0c\u5219\u4f60\u5fc5\u987b\u9996\u5148\u5206\u914d\u4e00\u4e2a\u957f\u5ea6\u6b63\u786e\u7684\u7f13\u5b58\uff1a 1 char * strHello { new char [ 7 ] }; \u4e0d\u8981\u5fd8\u8bb0\u5728\u8ba1\u7b97\u957f\u5ea6\u65f6\u591a\u52a0\u4e00\u4e2a\u7ed3\u675f\u7b26\u7684\u957f\u5ea6\uff01 \u7136\u540e\u4f60\u9700\u8981\u5c06\u503c\u62f7\u8d1d\u5230\u8fd9\u5757\u7f13\u5b58\u4e2d\uff1a 1 strcpy ( strHello , \"hello!\" ); \u53ea\u6709\u5f53\u4f60\u8ba1\u7b97\u7684\u7f13\u5b58\u957f\u5ea6\u6b63\u786e\u65f6\uff0c\u624d\u4e0d\u4f1a\u51fa\u73b0\u7f13\u51b2\u533a\u6ea2\u51fa\u3002 \u5f53\u7136\uff0c\u56e0\u4e3a\u5b57\u7b26\u4e32\u662f\u52a8\u6001\u5206\u914d\u7684\uff0c\u6240\u4ee5\u4f60\u8fd8\u5fc5\u987b\u8bb0\u5f97\u5728\u4f7f\u7528\u5b8c\u6210\u540e\u6b63\u786e\u5730\u91ca\u653e\u5b83\uff1a 1 delete [] strHello ; \u4e0d\u8981\u5fd8\u8bb0\u4f7f\u7528\u6570\u7ec4delete\u800c\u4e0d\u662f\u666e\u901adelete\u3002 \u6b64\u5916\uff0cC\u8bed\u8a00\u63d0\u4f9b\u7684\u8bb8\u591a\u7528\u4e8e\u5904\u7406\u6570\u5b57\u7684\u64cd\u4f5c\u7b26\uff0c\u4f8b\u5982\u8d4b\u503c\u548c\u6bd4\u8f83\uff0c\u6839\u672c\u65e0\u6cd5\u7528\u4e8e\u5904\u7406C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002\u6709\u65f6\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u770b\u4f3c\u6709\u6548\uff0c\u4f46\u5b9e\u9645\u4e0a\u4f1a\u4ea7\u751f\u4e0d\u6b63\u786e\u7684\u7ed3\u679c\u2014\u2014\u4f8b\u5982\uff0c\u4f7f\u7528 == \u6bd4\u8f83\u4e24\u4e2aC\u98ce\u683c\u5b57\u7b26\u4e32\u65f6\uff0c\u5b9e\u9645\u4e0a\u6bd4\u8f83\u7684\u662f\u4e24\u4e2a\u6307\u9488\uff0c\u800c\u975e\u5b57\u7b26\u4e32\u672c\u8eab\u3002\u4f7f\u7528 operator= \u5c06\u4e00\u4e2aC\u98ce\u683c\u5b57\u7b26\u4e32\u8d4b\u503c\u7ed9\u53e6\u4e00\u4e2aC\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u4e4d\u4e00\u770b\u4f3c\u4e4e\u53ef\u884c\uff0c\u4f46\u5b9e\u9645\u4e0a\u662f\u5728\u8fdb\u884c\u6307\u9488\u7684 \u6d45\u62f7\u8d1d \uff0c\u8fd9\u901a\u5e38\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c\u3002\u8fd9\u7c7b\u4e8b\u60c5\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\uff0c\u800c\u4e14\u5f88\u96be\u53d1\u73b0\u548c\u8c03\u8bd5\uff01 \u5f52\u6839\u7ed3\u5e95\uff0c\u4f7f\u7528C\u98ce\u683c\u5b57\u7b26\u4e32\u9700\u8981\u8bb0\u4f4f\u8bb8\u591a\u5173\u4e8e\u5b89\u5168/\u4e0d\u5b89\u5168\u7684\u89c4\u5219\uff0c\u8bb0\u4f4f\u4e00\u5806\u5177\u6709\u6709\u8da3\u540d\u79f0\u7684\u51fd\u6570\uff0c\u5982 strcat() \u548c strcmp() \uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u76f4\u89c2\u7684\u64cd\u4f5c\u7b26\uff0c\u800c\u4e14\u8fd8\u9700\u8981\u8fdb\u884c\u5927\u91cf\u7684\u624b\u52a8\u5185\u5b58\u7ba1\u7406\u3002 \u5e78\u8fd0\u7684\u662f\uff0cC++\u548c\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u5904\u7406\u5b57\u7b26\u4e32\u7684\u66f4\u597d\u7684\u65b9\u6cd5\uff1a std::string \u548c std::wstring \u7c7b\u3002\u901a\u8fc7\u4f7f\u7528\u6784\u9020\u51fd\u6570\u3001\u6790\u6784\u51fd\u6570\u548c\u64cd\u4f5c\u7b26\u91cd\u8f7d\u7b49C++\u6982\u5ff5\uff0c std::string \u4f7f\u6211\u4eec\u53ef\u4ee5\u4ee5\u4e00\u79cd\u76f4\u89c2\u548c\u5b89\u5168\u7684\u65b9\u5f0f\u521b\u5efa\u548c\u64cd\u4f5c\u5b57\u7b26\u4e32\uff01\u4e0d\u518d\u9700\u8981\u5185\u5b58\u7ba1\u7406\uff0c\u4e0d\u518d\u9700\u8981\u5947\u602a\u7684\u51fd\u6570\u540d\uff0c\u4e5f\u5927\u5927\u964d\u4f4e\u4e86\u53d1\u751f\u95ee\u9898\u7684\u53ef\u80fd\u6027\u3002","title":"\u8bbe\u8ba1\u5b57\u7b26\u4e32\u7c7b\u7684\u521d\u8877"},{"location":"22-1-std-string-and-std-wstring/#_2","text":"\u6240\u6709\u6807\u51c6\u5e93\u5b57\u7b26\u4e32\u51fd\u6570\u90fd\u4f4d\u4e8e\u5934\u6587\u4ef6\u4e2d\uff0c\u4f7f\u7528\u65f6\u53ea\u9700\u8981\u5305\u542b\u8be5\u5934\u6587\u4ef6\u5373\u53ef\uff1a 1 #include \u5728\u5b57\u7b26\u4e32\u5934\u6587\u4ef6\u4e2d\u5b9e\u9645\u4e0a\u67093\u4e2a\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\u7c7b\u3002\u7b2c\u4e00\u4e2a\u662f\u540d\u4e3a basic_string \u7684\u6a21\u677f\u57fa\u7c7b\uff1a 1 2 3 4 5 namespace std { template < class charT , class traits = char_traits < charT > , class Allocator = allocator < charT > > class basic_string ; } \u6211\u4eec\u5e76\u4e0d\u4f1a\u76f4\u63a5\u4f7f\u7528\u8fd9\u4e2a\u7c7b\uff0c\u6240\u4ee5\u6682\u65f6\u4e0d\u8981\u62c5\u5fc3 trait \u6216 Allocator \u8fd9\u4e9b\u4e71\u4e03\u516b\u7cdf\u7684\u60c5\u51b5\u3002\u5728\u51e0\u4e4e\u6240\u6709\u53ef\u4ee5\u60f3\u8c61\u5230\u7684\u60c5\u51b5\u4e0b\uff0c\u9ed8\u8ba4\u503c\u5c31\u8db3\u591f\u4e86\u3002 \u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u4e24\u79cd\u7c7b\u578b\u7684 basic_string \uff1a 1 2 3 4 5 namespace std { typedef basic_string < char > string ; typedef basic_string < wchar_t > wstring ; } \u8fd9\u662f\u4f60\u5c06\u5b9e\u9645\u4f7f\u7528\u7684\u4e24\u4e2a\u7c7b\u3002 string \u7528\u4e8e\u6807\u51c6\u7684ASCII\u548cutf-8\u5b57\u7b26\u4e32\u3002 wstring \u7528\u4e8e\u5bbd\u5b57\u7b26/unicode (utf-16)\u5b57\u7b26\u4e32\u3002\u4f46\u662f\u5e76\u6ca1\u6709\u7528\u4e8eutf-32\u5b57\u7b26\u4e32\u7684\u5185\u7f6e\u7c7b(\u4e0d\u8fc7\uff0c\u5982\u679c\u9700\u8981\u7684\u8bdd\uff0c\u60a8\u5e94\u8be5\u80fd\u591f\u4ece basic_string \u81ea\u5df1\u6269\u5c55)\u3002 \u5c3d\u7ba1\u4f60\u4f1a\u76f4\u63a5\u4f7f\u7528\u7684\u662f std::string \u548c std::wstring \uff0c\u4f46\u6240\u6709\u7684\u5b57\u7b26\u4e32\u529f\u80fd\u90fd\u662f\u5728 basic_string \u7c7b\u4e2d\u5b9e\u73b0\u7684\u3002 string \u548c wstring \u80fd\u591f\u76f4\u63a5\u8bbf\u95ee\u8fd9\u4e9b\u529f\u80fd\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u6a21\u677f\u5316\u7684\u3002\u56e0\u6b64\uff0c\u6240\u63d0\u4f9b\u7684\u6240\u6709\u51fd\u6570\u90fd\u9002\u7528\u4e8e string \u548c wstring \u3002\u4f46\u662f\uff0c\u7531\u4e8e basic_string \u662f\u4e00\u4e2a\u6a21\u677f\u5316\u7c7b\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u5f53\u4f60\u5bf9 string \u6216 wstring \u8fdb\u884c\u8bed\u6cd5\u9519\u8bef\u64cd\u4f5c\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u4ea7\u751f\u53ef\u6015\u7684\u6a21\u677f\u9519\u8bef\u3002\u4e0d\u8981\u88ab\u8fd9\u4e9b\u9519\u8bef\u5413\u5012\uff1a\u5b83\u4eec\u53ea\u662f\u770b\u8d77\u6765\u6bd4\u8f83\u5413\u4eba\u7f62\u4e86\u3002 \u8fd9\u662f string \u7c7b\u4e2d\u6240\u6709\u51fd\u6570\u7684\u5217\u8868\u3002\u8fd9\u4e9b\u51fd\u6570\u4e2d\u7684\u5927\u591a\u6570\u90fd\u6709\u591a\u79cd\u53d8\u79cd\u7528\u4e8e\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u8f93\u5165\uff0c\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\u66f4\u6df1\u5165\u5730\u8ba8\u5b83\u4eec\u7684\u5177\u4f53\u7528\u6cd5\u3002","title":"\u5b57\u7b26\u4e32\u7b80\u4ecb"},{"location":"22-1-std-string-and-std-wstring/#_3","text":"Function Effect (constructor) \u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u62f7\u8d1d (destructor) \u9500\u6bc1\u5b57\u7b26\u4e32","title":"\u6784\u5efa\u548c\u6790\u6784"},{"location":"22-1-std-string-and-std-wstring/#_4","text":"Function Effect capacity() \u5728\u4e0d\u91cd\u65b0\u5206\u914d\u5185\u5b58\u60c5\u51b5\u4e0b\uff0c\u80fd\u591f\u5b58\u653e\u7684\u6700\u591a\u7684\u5b57\u7b26\u4e2a\u6570 empty() \u8fd4\u56de\u5e03\u5c14\u503c\u8868\u793a\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u7a7a\u5b57\u7b26\u4e32 length(), size() \u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u4e2a\u6570 max_size() \u8fd4\u56de\u53ef\u4ee5\u5206\u914d\u7684\u6700\u957f\u7684\u5b57\u7b26\u4e32\u5927\u5c0f reserve() \u6269\u5927\u6216\u7f29\u5c0f\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf","title":"\u957f\u5ea6\u548c\u5bb9\u91cf"},{"location":"22-1-std-string-and-std-wstring/#_5","text":"Function Effect [] , at() \u4f7f\u7528\u7d22\u5f15\u8bbf\u95ee\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26","title":"\u5143\u7d20\u8bbf\u95ee"},{"location":"22-1-std-string-and-std-wstring/#_6","text":"Function Effect =, assign() \u5b57\u7b26\u4e32\u8d4b\u503c +=, append(), push_back() \u5b57\u7b26\u4e32\u672b\u5c3e\u6dfb\u52a0 insert() \u5728\u4efb\u610f\u7d22\u5f15\u4f4d\u7f6e\u63d2\u5165 clear() \u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u7b26 erase() \u6e05\u9664\u5b57\u7b26 replace() \u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u4e00\u90e8\u5206 resize() \u8c03\u6574\u5b57\u7b26\u4e32\u5927\u5c0f(\u5728\u672b\u5c3e\u622a\u65ad\u6216\u6dfb\u52a0) swap() \u4ea4\u6362\u4e24\u4e2a\u5b57\u7b26\u4e32","title":"\u4fee\u6539\u5b57\u7b26\u4e32"},{"location":"22-1-std-string-and-std-wstring/#_7","text":"Function Effect >> , getline() \u4ece\u8f93\u5165\u6d41\u4e2d\u83b7\u53d6\u8f93\u5165 << \u5c06\u5b57\u7b26\u4e32\u8f93\u51fa\u5230\u8f93\u51fa\u6d41 c_str() \u8fd4\u56de\u4ee5\u7a7a\u5b57\u7b26\u7ed3\u5c3e\u7684C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 copy() \u62f7\u8d1d\u5b57\u7b26\u4e32\u5185\u5bb9\u5230\u5b57\u7b26\u6570\u7ec4(\u4e0d\u5305\u542b\u7ed3\u675f\u7b26) data() \u7c7b\u4f3c c_str() \u3002\u975econst\u91cd\u8f7d\uff0c\u5141\u8bb8\u5199\u5165\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u3002","title":"\u8f93\u5165\u8f93\u51fa"},{"location":"22-1-std-string-and-std-wstring/#_8","text":"Function Effect == , != \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\uff08\u8fd4\u56de\u5e03\u5c14\u503c\uff09 < , <= , > , >= \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u5927\u5c0f\u5173\u7cfb\uff08\u8fd4\u56de\u5e03\u5c14\u503c\uff09 compare() \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u76f8\u7b49(\u8fd4\u56de -1, 0, \u6216 1)","title":"\u5b57\u7b26\u4e32\u6bd4\u8f83"},{"location":"22-1-std-string-and-std-wstring/#_9","text":"Function Effect + \u8fde\u63a5\u4e24\u4e2a\u5b57\u7b26\u4e32 substr() \u8fd4\u56de\u4e00\u4e2a\u5b50\u4e32","title":"\u5b50\u4e32\u548c\u62fc\u63a5"},{"location":"22-1-std-string-and-std-wstring/#_10","text":"Function Effect find() \u641c\u7d22\u5b57\u7b26\u3001\u5b50\u4e32\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u4f4d\u7f6e find_first_of() \u627e\u5230\u5b57\u7b26\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u4f4d\u7f6e find_first_not_of() \u627e\u5230\u7b2c\u4e00\u4e2a\u4e0d\u5728\u5b57\u7b26\u96c6\u4e2d\u7684\u5b57\u7b26\u51fa\u73b0\u7684\u4f4d\u7f6e find_last_of() \u627e\u5230\u5b57\u7b26\u96c6\u4e2d\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u51fa\u73b0\u7684\u7d22\u5f15\u4f4d\u7f6e find_last_not_of() \u627e\u5230\u6700\u540e\u4e00\u4e2a\u4e0d\u5728\u5b57\u7b26\u96c6\u4e2d\u7684\u5b57\u7b26\u51fa\u73b0\u7684\u4f4d\u7f6e rfind() \u641c\u7d22\u5b57\u7b26\u3001\u5b50\u4e32\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u4f4d\u7f6e","title":"\u641c\u7d22"},{"location":"22-1-std-string-and-std-wstring/#_11","text":"Function Effect begin() , end() \u6b63\u5411\u8fed\u4ee3\u5668\uff0c\u5f00\u5934/\u7ed3\u5c3e get_allocator() \u8fd4\u56de\u5206\u914d\u5668 rbegin() , rend() \u53cd\u5411\u8fed\u4ee3\u5668\uff0c\u5f00\u5934/\u7ed3\u5c3e \u867d\u7136\u6807\u51c6\u5e93\u5b57\u7b26\u4e32\u7c7b\u63d0\u4f9b\u4e86\u5f88\u591a\u529f\u80fd\uff0c\u4f46\u6709\u4e00\u4e9b\u660e\u663e\u7684\u9057\u6f0f\uff1a \u5bf9\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u652f\u6301\uff1b \u4ece\u6570\u5b57\u521b\u5efa\u5b57\u7b26\u4e32\u7684\u6784\u9020\u51fd\u6570\uff1b \u5927\u5c0f\u5199\u5b57\u6bcd\u8f6c\u6362\u51fd\u6570\uff1b \u5ffd\u7565\u5927\u5c0f\u5199\u7684\u5b57\u7b26\u4e32\u6bd4\u8f83\u65b9\u6cd5\uff1b \u5b57\u7b26\u4e32\u5206\u8bcd(Tokenization)\u540e\u5b58\u5165\u6570\u7ec4\uff1b \u83b7\u53d6\u5b57\u7b26\u4e32\u5de6\u534a\u90e8\u5206\u6216\u53f3\u534a\u90e8\u5206\u7684\u51fd\u6570\uff1b \u7a7a\u767d\u79fb\u9664\u51fd\u6570(trimming)\uff1b sprintf \u98ce\u683c\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u51fd\u6570\uff1b utf-8 \u548c utf-16 \u8f6c\u6362\u3002 \u5bf9\u4e8e\u5176\u4e2d\u7684\u5927\u591a\u6570\u529f\u80fd\uff0c\u4f60\u5fc5\u987b\u7f16\u5199\u81ea\u5df1\u7684\u51fd\u6570\uff0c\u6216\u8005\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aC\u98ce\u683c\u7684\u5b57\u7b26\u4e32(\u4f7f\u7528 c_str() )\uff0c\u5e76\u4f7f\u7528\u63d0\u4f9b\u6b64\u529f\u80fd\u7684C\u51fd\u6570\u3002 \u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u66f4\u6df1\u5165\u5730\u7814\u7a76\u5b57\u7b26\u4e32\u7c7b\u7684\u5404\u79cd\u529f\u80fd\u3002\u5c3d\u7ba1\u6211\u4eec\u5c06\u5728\u793a\u4f8b\u4e2d\u4f7f\u7528 string \uff0c\u4f46\u6240\u6709\u5185\u5bb9\u90fd\u540c\u6837\u9002\u7528\u4e8e wstring \u3002","title":"\u8fed\u4ee3\u5668\u548c\u5185\u5b58\u5206\u914d"},{"location":"22-2-std-string-construction-and-destruction/","tags":["string"],"text":"Key Takeaway \u5728\u8fd9\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u770b\u770b\u5982\u4f55\u6784\u9020 std::string \u7684\u5bf9\u8c61\uff0c\u4ee5\u53ca\u5982\u4f55\u4ece\u6570\u5b57\u521b\u5efa\u5b57\u7b26\u4e32\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002 \u5b57\u7b26\u4e32\u6784\u9020 \u5b57\u7b26\u4e32\u7c7b\u6709\u8bb8\u591a\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u5b57\u7b26\u4e32\u7684\u6784\u9020\u51fd\u6570\u3002\u6211\u4eec\u5c06\u5728\u8fd9\u91cc\u9010\u4e00\u4e86\u89e3\u5b83\u4eec\u3002 \u6ce8\u610f\uff1a string::size_type \u89e3\u6790\u4e3a size_t \uff0c\u5b83\u4e0e sizeof \u64cd\u4f5c\u7b26\u8fd4\u56de\u7684\u65e0\u7b26\u53f7\u6574\u578b\u76f8\u540c\u3002 size_t \u7684\u5b9e\u9645\u5927\u5c0f\u53d6\u51b3\u4e8e\u73af\u5883\u3002\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u5c06\u5176\u770b\u505a\u65e0\u7b26\u53f7\u6574\u578b\u3002 string::string() \u5b57\u7b26\u4e32\u7c7b\u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u2014\u2014\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\u3002 \u4f8b\u5b50\uff1a 1 2 std :: string sSource ; std :: cout << sSource ; \u8f93\u51fa\uff1a 1 string::string(const string& strString) \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002\u8be5\u6784\u9020\u51fd\u6570\u57fa\u4e8e\u4e00\u4e2a\u5b57\u7b26\u4e32 strString \u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u3002 \u4f8b\u5b50\uff1a 1 2 3 std :: string sSource { \"my string\" }; std :: string sOutput { sSource }; std :: cout << sOutput ; \u8f93\u51fa\uff1a 1 my string string::string(const string& strString, size_type unIndex) string::string(const string& strString, size_type unIndex, size_type unLength) \u8be5\u6784\u9020\u51fd\u6570\u57fa\u4e8e strString \u5b57\u7b26\u4e32\u4e2d\u6700\u591a unLength \u4e2a\u5b57\u7b26\uff08\u4ece unIndex \u5f00\u59cb\uff09\u6784\u9020\u65b0\u7684\u5b57\u7b26\u4e32\u3002\u5982\u679c\u9047\u5230\u7ed3\u675f\u7b26\uff0c\u5219\u505c\u6b62\u62f7\u8d1d\uff0c\u5373\u4f7f\u8fd8\u6ca1\u6709\u5230 unLength \u4e2a\u5b57\u7b26\uff1b \u5982\u679c\u6ca1\u6709 unLength \u4e2a\u5b57\u7b26\uff0c\u5219\u4f1a\u4ece unIndex \u5f00\u59cb\u62f7\u8d1d\u5168\u90e8\u5b57\u7b26\uff1b \u5982\u679c unIndex \u6bd4\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u8fd8\u957f\uff0c\u5219\u6216\u629b\u51fa out_of_range \u5f02\u5e38\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 std :: string sSource { \"my string\" }; std :: string sOutput { sSource , 3 }; std :: cout << sOutput << '\\n' ; std :: string sOutput2 ( sSource , 3 , 4 ); std :: cout << sOutput2 << '\\n' ; \u8f93\u51fa\uff1a 1 2 string stri string::string(const char* szCString) \u8be5\u6784\u9020\u51fd\u6570\u4f1a\u57fa\u4e8e\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 szCString \u6784\u9020\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\uff0c\u4f46\u662f\u4e0d\u4f1a\u5305\u542b\u7ed3\u675f\u7b26\uff1b \u5982\u679c\u7ed3\u679c\u4f1a\u8d85\u8fc7\u5b57\u7b26\u4e32\u7684\u6700\u5927\u957f\u5ea6\uff0c\u5219\u4f1a\u629b\u51fa length_error \u5f02\u5e38\uff1b \u8b66\u544a\u26a0\ufe0f \uff1a szCString \u4e0d\u80fd\u662f NULL\u3002 \u4f8b\u5b50\uff1a 1 2 3 const char * szSource { \"my string\" }; std :: string sOutput { szSource }; std :: cout << sOutput << '\\n' ; \u8f93\u51fa\uff1a 1 my string string::string(const char* szCString, size_type unLength) This constructor creates a new string from the first unLength chars from the C-style string szCString. If the resulting size exceeds the maximum string length, the length_error exception will be thrown. Warning: For this function only, NULLs are not treated as end-of-string characters in szCString! This means it is possible to read off the end of your string if unLength is too big. Be careful not to overflow your string buffer! \u4f8b\u5b50\uff1a 1 2 3 const char * szSource { \"my string\" }; std :: string sOutput ( szSource , 4 ); std :: cout << sOutput << '\\n' ; \u8f93\u51fa\uff1a 1 my s string::string(size_type nNum, char chChar) \u8be5\u6784\u9020\u51fd\u6570\u57fa\u4e8e\u67d0\u4e2a\u5b57\u7b26\uff0c\u5c06\u5176\u91cd\u590d chChar \u6b21\u521b\u5efa\u51fa\u65b0\u7684\u5b57\u7b26\u4e32\uff1b \u5982\u679c\u7ed3\u679c\u7684\u957f\u5ea6\u8d85\u8fc7\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u5219\u629b\u51fa length_error \u5f02\u5e38\u3002 \u4f8b\u5b50\uff1a 1 2 std :: string sOutput ( 4 , 'Q' ); std :: cout << sOutput << '\\n' ; \u8f93\u51fa\uff1a 1 QQQQ template string::string(InputIterator itBeg, InputIterator itEnd) \u8be5\u6784\u9020\u51fd\u6570\u57fa\u4e8e\u4e00\u4e2a\u8303\u56f4 [itBeg, itEnd) \u7684\u5b57\u7b26\u4e32\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\uff1b \u5982\u679c\u7ed3\u679c\u7684\u957f\u5ea6\u8d85\u8fc7\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u5219\u629b\u51fa length_error \u5f02\u5e38\u3002 \u6ca1\u6709\u793a\u4f8b\u4ee3\u7801\u3002\u5b83\u592a\u6666\u6da9\u4e86\uff0c\u4f60\u53ef\u80fd\u6c38\u8fdc\u4e0d\u4f1a\u7528\u5b83\u3002 \u5b57\u7b26\u4e32\u6790\u6784 string::~string() \u6790\u6784\u51fd\u6570\uff0c\u7528\u4e8e\u9500\u6bc1\u5b57\u7b26\u4e32\u5e76\u91ca\u653e\u5185\u5b58 \u6ca1\u6709\u793a\u4f8b\u4ee3\u7801\u3002\u4f60\u4e0d\u4f1a\u663e\u793a\u5730\u8c03\u7528\u5b83\u3002 \u4ece\u6570\u5b57\u6784\u5efa\u5b57\u7b26\u4e32 std::string \u7c7b\u9057\u6f0f\u4e86\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u529f\u80fd\uff0c\u5373\u4ece\u6570\u5b57\u521b\u5efa\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\uff1a 1 std :: string sFour { 4 }; \u8f93\u51fa\u7ed3\u679c\uff1a 1 c:vcprojectstest2test2test.cpp(10) : error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it)' : cannot convert parameter 1 from 'int' to 'std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it' \u8fd8\u8bb0\u5f97\u6211\u8bf4\u8fc7\u5b57\u7b26\u4e32\u7c7b\u4f1a\u4ea7\u751f\u53ef\u6015\u7684\u62a5\u9519\u4fe1\u606f\u5417\uff1f\u5c31\u662f\u50cf\u4e0a\u9762\u8fd9\u6837\u3002\u8fd9\u91cc\u7684\u6709\u7528\u7684\u4fe1\u606f\u662f\uff1a 1 cannot convert parameter 1 from 'int' to 'std::basic_string \u6362\u53e5\u8bdd\u8bf4\uff0c\u5b83\u8bd5\u56fe\u5c06int\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u4f46\u5931\u8d25\u4e86\u3002 \u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 std::ostringstream \u7c7b\u3002 std::ostringstream \u53ef\u4ee5\u63a5\u53d7\u6765\u81ea\u5404\u79cd\u6765\u6e90\u7684\u8f93\u5165\uff0c\u5305\u62ec\u5b57\u7b26\u3001\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u7b49\u2026\u5b83\u8fd8\u80fd\u591f\u8f93\u51fa\u5b57\u7b26\u4e32(\u901a\u8fc7 \u63d0\u53d6\u8fd0\u7b97\u7b26 \uff0c\u6216\u901a\u8fc7 str() \u51fd\u6570)\u3002\u6709\u5173 std::ostringstream \u7684\u66f4\u591a\u4fe1\u606f\uff0c\u53c2\u8003 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b \u3002 \u4ece\u4e0d\u540c\u7c7b\u578b\u7684\u8f93\u5165\u521b\u5efa\u5b57\u7b26\u4e32\u7684\u4f8b\u5b50\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include #include #include template < typename T > inline std :: string ToString ( T tX ) { std :: ostringstream oStream ; oStream << tX ; return oStream . str (); } \u4e0b\u9762\u662f\u4e00\u4e9b\u6d4b\u8bd5\u5b83\u7684\u793a\u4f8b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 int main () { std :: string sFour { ToString ( 4 ) }; std :: string sSixPointSeven { ToString ( 6.7 ) }; std :: string sA { ToString ( 'A' ) }; std :: cout << sFour << '\\n' ; std :: cout << sSixPointSeven << '\\n' ; std :: cout << sA << '\\n' ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 6.7 A \u6ce8\u610f\uff0c\u8fd9\u91cc\u6211\u4eec\u5ffd\u7565\u4e86\u9519\u8bef\u68c0\u67e5\u3002\u5c06 tX \u63d2\u5165 oStream \u4e2d\u53ef\u80fd\u4f1a\u5931\u8d25\uff0c\u5982\u679c\u8f6c\u6362\u5931\u8d25\u5219\u5e94\u8be5\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u3002 \u76f8\u5173\u5185\u5bb9 \u6807\u51c6\u5e93\u8fd8\u5305\u542b\u4e00\u4e2a\u540d\u4e3a std::to_string() \u7684\u51fd\u6570\uff0c\u53ef\u7528\u4e8e\u5c06\u5b57\u7b26\u548c\u6570\u5b57\u8f6c\u6362\u4e3a std::string \u3002\u867d\u7136\u8fd9\u662f\u57fa\u672c\u573a\u5408\u4e2d\u66f4\u7b80\u5355\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u4f46 std::to_string \u7684\u8f93\u51fa\u53ef\u80fd\u4e0e\u4e0a\u9762 std::cout \u6216 out ToString() \u51fd\u6570\u7684\u8f93\u51fa\u4e0d\u540c\u3002\u5176\u4e2d\u4e00\u4e9b\u5dee\u5f02\u88ab\u8bb0\u5f55 \u8fd9\u91cc \u3002 \u4ece\u5b57\u7b26\u4e32\u521b\u5efa\u6570\u5b57 \u548c\u4e0a\u9762\u7684\u89e3\u51b3\u65b9\u6848\u7c7b\u4f3c\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include #include template < typename T > inline bool FromString ( const std :: string & sString , T & tX ) { std :: istringstream iStream ( sString ); return ! ( iStream >> tX ). fail (); // \u5c06\u503c\u63d0\u53d6\u5230 tX \u5e76\u8fd4\u56de\u662f\u5426\u6210\u529f } \u4e0b\u9762\u662f\u4e00\u4e9b\u6d4b\u8bd5\u5b83\u7684\u793a\u4f8b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 int main () { double dX ; if ( FromString ( \"3.4\" , dX )) std :: cout << dX << '\\n' ; if ( FromString ( \"ABC\" , dX )) std :: cout << dX << '\\n' ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 3.4 \u6ce8\u610f\uff0c\u7b2c\u4e8c\u6b21\u8f6c\u6362\u5931\u8d25\u5e76\u8fd4\u56de\u4e86 false \u3002","title":"22.2 - std-string \u7684\u6784\u9020\u548c\u6790\u6784"},{"location":"22-2-std-string-construction-and-destruction/#_1","text":"\u5b57\u7b26\u4e32\u7c7b\u6709\u8bb8\u591a\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u5b57\u7b26\u4e32\u7684\u6784\u9020\u51fd\u6570\u3002\u6211\u4eec\u5c06\u5728\u8fd9\u91cc\u9010\u4e00\u4e86\u89e3\u5b83\u4eec\u3002 \u6ce8\u610f\uff1a string::size_type \u89e3\u6790\u4e3a size_t \uff0c\u5b83\u4e0e sizeof \u64cd\u4f5c\u7b26\u8fd4\u56de\u7684\u65e0\u7b26\u53f7\u6574\u578b\u76f8\u540c\u3002 size_t \u7684\u5b9e\u9645\u5927\u5c0f\u53d6\u51b3\u4e8e\u73af\u5883\u3002\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u5c06\u5176\u770b\u505a\u65e0\u7b26\u53f7\u6574\u578b\u3002 string::string() \u5b57\u7b26\u4e32\u7c7b\u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u2014\u2014\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\u3002 \u4f8b\u5b50\uff1a 1 2 std :: string sSource ; std :: cout << sSource ; \u8f93\u51fa\uff1a 1 string::string(const string& strString) \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002\u8be5\u6784\u9020\u51fd\u6570\u57fa\u4e8e\u4e00\u4e2a\u5b57\u7b26\u4e32 strString \u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u3002 \u4f8b\u5b50\uff1a 1 2 3 std :: string sSource { \"my string\" }; std :: string sOutput { sSource }; std :: cout << sOutput ; \u8f93\u51fa\uff1a 1 my string string::string(const string& strString, size_type unIndex) string::string(const string& strString, size_type unIndex, size_type unLength) \u8be5\u6784\u9020\u51fd\u6570\u57fa\u4e8e strString \u5b57\u7b26\u4e32\u4e2d\u6700\u591a unLength \u4e2a\u5b57\u7b26\uff08\u4ece unIndex \u5f00\u59cb\uff09\u6784\u9020\u65b0\u7684\u5b57\u7b26\u4e32\u3002\u5982\u679c\u9047\u5230\u7ed3\u675f\u7b26\uff0c\u5219\u505c\u6b62\u62f7\u8d1d\uff0c\u5373\u4f7f\u8fd8\u6ca1\u6709\u5230 unLength \u4e2a\u5b57\u7b26\uff1b \u5982\u679c\u6ca1\u6709 unLength \u4e2a\u5b57\u7b26\uff0c\u5219\u4f1a\u4ece unIndex \u5f00\u59cb\u62f7\u8d1d\u5168\u90e8\u5b57\u7b26\uff1b \u5982\u679c unIndex \u6bd4\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u8fd8\u957f\uff0c\u5219\u6216\u629b\u51fa out_of_range \u5f02\u5e38\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 std :: string sSource { \"my string\" }; std :: string sOutput { sSource , 3 }; std :: cout << sOutput << '\\n' ; std :: string sOutput2 ( sSource , 3 , 4 ); std :: cout << sOutput2 << '\\n' ; \u8f93\u51fa\uff1a 1 2 string stri string::string(const char* szCString) \u8be5\u6784\u9020\u51fd\u6570\u4f1a\u57fa\u4e8e\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 szCString \u6784\u9020\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\uff0c\u4f46\u662f\u4e0d\u4f1a\u5305\u542b\u7ed3\u675f\u7b26\uff1b \u5982\u679c\u7ed3\u679c\u4f1a\u8d85\u8fc7\u5b57\u7b26\u4e32\u7684\u6700\u5927\u957f\u5ea6\uff0c\u5219\u4f1a\u629b\u51fa length_error \u5f02\u5e38\uff1b \u8b66\u544a\u26a0\ufe0f \uff1a szCString \u4e0d\u80fd\u662f NULL\u3002 \u4f8b\u5b50\uff1a 1 2 3 const char * szSource { \"my string\" }; std :: string sOutput { szSource }; std :: cout << sOutput << '\\n' ; \u8f93\u51fa\uff1a 1 my string string::string(const char* szCString, size_type unLength) This constructor creates a new string from the first unLength chars from the C-style string szCString. If the resulting size exceeds the maximum string length, the length_error exception will be thrown. Warning: For this function only, NULLs are not treated as end-of-string characters in szCString! This means it is possible to read off the end of your string if unLength is too big. Be careful not to overflow your string buffer! \u4f8b\u5b50\uff1a 1 2 3 const char * szSource { \"my string\" }; std :: string sOutput ( szSource , 4 ); std :: cout << sOutput << '\\n' ; \u8f93\u51fa\uff1a 1 my s string::string(size_type nNum, char chChar) \u8be5\u6784\u9020\u51fd\u6570\u57fa\u4e8e\u67d0\u4e2a\u5b57\u7b26\uff0c\u5c06\u5176\u91cd\u590d chChar \u6b21\u521b\u5efa\u51fa\u65b0\u7684\u5b57\u7b26\u4e32\uff1b \u5982\u679c\u7ed3\u679c\u7684\u957f\u5ea6\u8d85\u8fc7\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u5219\u629b\u51fa length_error \u5f02\u5e38\u3002 \u4f8b\u5b50\uff1a 1 2 std :: string sOutput ( 4 , 'Q' ); std :: cout << sOutput << '\\n' ; \u8f93\u51fa\uff1a 1 QQQQ template string::string(InputIterator itBeg, InputIterator itEnd) \u8be5\u6784\u9020\u51fd\u6570\u57fa\u4e8e\u4e00\u4e2a\u8303\u56f4 [itBeg, itEnd) \u7684\u5b57\u7b26\u4e32\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\uff1b \u5982\u679c\u7ed3\u679c\u7684\u957f\u5ea6\u8d85\u8fc7\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u5219\u629b\u51fa length_error \u5f02\u5e38\u3002 \u6ca1\u6709\u793a\u4f8b\u4ee3\u7801\u3002\u5b83\u592a\u6666\u6da9\u4e86\uff0c\u4f60\u53ef\u80fd\u6c38\u8fdc\u4e0d\u4f1a\u7528\u5b83\u3002","title":"\u5b57\u7b26\u4e32\u6784\u9020"},{"location":"22-2-std-string-construction-and-destruction/#_2","text":"string::~string() \u6790\u6784\u51fd\u6570\uff0c\u7528\u4e8e\u9500\u6bc1\u5b57\u7b26\u4e32\u5e76\u91ca\u653e\u5185\u5b58 \u6ca1\u6709\u793a\u4f8b\u4ee3\u7801\u3002\u4f60\u4e0d\u4f1a\u663e\u793a\u5730\u8c03\u7528\u5b83\u3002","title":"\u5b57\u7b26\u4e32\u6790\u6784"},{"location":"22-2-std-string-construction-and-destruction/#_3","text":"std::string \u7c7b\u9057\u6f0f\u4e86\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u529f\u80fd\uff0c\u5373\u4ece\u6570\u5b57\u521b\u5efa\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\uff1a 1 std :: string sFour { 4 }; \u8f93\u51fa\u7ed3\u679c\uff1a 1 c:vcprojectstest2test2test.cpp(10) : error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it)' : cannot convert parameter 1 from 'int' to 'std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it' \u8fd8\u8bb0\u5f97\u6211\u8bf4\u8fc7\u5b57\u7b26\u4e32\u7c7b\u4f1a\u4ea7\u751f\u53ef\u6015\u7684\u62a5\u9519\u4fe1\u606f\u5417\uff1f\u5c31\u662f\u50cf\u4e0a\u9762\u8fd9\u6837\u3002\u8fd9\u91cc\u7684\u6709\u7528\u7684\u4fe1\u606f\u662f\uff1a 1 cannot convert parameter 1 from 'int' to 'std::basic_string \u6362\u53e5\u8bdd\u8bf4\uff0c\u5b83\u8bd5\u56fe\u5c06int\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u4f46\u5931\u8d25\u4e86\u3002 \u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 std::ostringstream \u7c7b\u3002 std::ostringstream \u53ef\u4ee5\u63a5\u53d7\u6765\u81ea\u5404\u79cd\u6765\u6e90\u7684\u8f93\u5165\uff0c\u5305\u62ec\u5b57\u7b26\u3001\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u7b49\u2026\u5b83\u8fd8\u80fd\u591f\u8f93\u51fa\u5b57\u7b26\u4e32(\u901a\u8fc7 \u63d0\u53d6\u8fd0\u7b97\u7b26 \uff0c\u6216\u901a\u8fc7 str() \u51fd\u6570)\u3002\u6709\u5173 std::ostringstream \u7684\u66f4\u591a\u4fe1\u606f\uff0c\u53c2\u8003 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b \u3002 \u4ece\u4e0d\u540c\u7c7b\u578b\u7684\u8f93\u5165\u521b\u5efa\u5b57\u7b26\u4e32\u7684\u4f8b\u5b50\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include #include #include template < typename T > inline std :: string ToString ( T tX ) { std :: ostringstream oStream ; oStream << tX ; return oStream . str (); } \u4e0b\u9762\u662f\u4e00\u4e9b\u6d4b\u8bd5\u5b83\u7684\u793a\u4f8b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 int main () { std :: string sFour { ToString ( 4 ) }; std :: string sSixPointSeven { ToString ( 6.7 ) }; std :: string sA { ToString ( 'A' ) }; std :: cout << sFour << '\\n' ; std :: cout << sSixPointSeven << '\\n' ; std :: cout << sA << '\\n' ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 6.7 A \u6ce8\u610f\uff0c\u8fd9\u91cc\u6211\u4eec\u5ffd\u7565\u4e86\u9519\u8bef\u68c0\u67e5\u3002\u5c06 tX \u63d2\u5165 oStream \u4e2d\u53ef\u80fd\u4f1a\u5931\u8d25\uff0c\u5982\u679c\u8f6c\u6362\u5931\u8d25\u5219\u5e94\u8be5\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u3002 \u76f8\u5173\u5185\u5bb9 \u6807\u51c6\u5e93\u8fd8\u5305\u542b\u4e00\u4e2a\u540d\u4e3a std::to_string() \u7684\u51fd\u6570\uff0c\u53ef\u7528\u4e8e\u5c06\u5b57\u7b26\u548c\u6570\u5b57\u8f6c\u6362\u4e3a std::string \u3002\u867d\u7136\u8fd9\u662f\u57fa\u672c\u573a\u5408\u4e2d\u66f4\u7b80\u5355\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u4f46 std::to_string \u7684\u8f93\u51fa\u53ef\u80fd\u4e0e\u4e0a\u9762 std::cout \u6216 out ToString() \u51fd\u6570\u7684\u8f93\u51fa\u4e0d\u540c\u3002\u5176\u4e2d\u4e00\u4e9b\u5dee\u5f02\u88ab\u8bb0\u5f55 \u8fd9\u91cc \u3002","title":"\u4ece\u6570\u5b57\u6784\u5efa\u5b57\u7b26\u4e32"},{"location":"22-2-std-string-construction-and-destruction/#_4","text":"\u548c\u4e0a\u9762\u7684\u89e3\u51b3\u65b9\u6848\u7c7b\u4f3c\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include #include template < typename T > inline bool FromString ( const std :: string & sString , T & tX ) { std :: istringstream iStream ( sString ); return ! ( iStream >> tX ). fail (); // \u5c06\u503c\u63d0\u53d6\u5230 tX \u5e76\u8fd4\u56de\u662f\u5426\u6210\u529f } \u4e0b\u9762\u662f\u4e00\u4e9b\u6d4b\u8bd5\u5b83\u7684\u793a\u4f8b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 int main () { double dX ; if ( FromString ( \"3.4\" , dX )) std :: cout << dX << '\\n' ; if ( FromString ( \"ABC\" , dX )) std :: cout << dX << '\\n' ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 3.4 \u6ce8\u610f\uff0c\u7b2c\u4e8c\u6b21\u8f6c\u6362\u5931\u8d25\u5e76\u8fd4\u56de\u4e86 false \u3002","title":"\u4ece\u5b57\u7b26\u4e32\u521b\u5efa\u6570\u5b57"},{"location":"22-3-std-string-length-and-capacity/","tags":["string"],"text":"Key Takeaway \u4e00\u65e6\u521b\u5efa\u4e86\u5b57\u7b26\u4e32\uff0c\u77e5\u9053\u5b83\u4eec\u7684\u957f\u5ea6\u901a\u5e38\u662f\u5f88\u6709\u7528\u7684\u3002\u6211\u4eec\u8fd8\u5c06\u8ba8\u8bba\u5c06 std::string \u8f6c\u6362\u56deC\u98ce\u683c\u5b57\u7b26\u4e32\u7684\u5404\u79cd\u65b9\u6cd5\uff0c\u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u5728\u5c06\u5176\u7528\u5728\u671f\u671b char* \u7c7b\u578b\u5b57\u7b26\u4e32\u7684\u51fd\u6570\u4e2d\u4f7f\u7528\u5b83\u4eec\u3002 \u5b57\u7b26\u4e32\u7684\u957f\u5ea6 \u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u975e\u5e38\u7b80\u5355\u2014\u2014\u5373\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u4e2a\u6570\u3002\u6709\u4e24\u4e2a\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u786e\u5b9a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff1a size_type string::length() const size_type string::size() const \u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u53ef\u4ee5\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u4e2a\u6570\uff0c\u5305\u62ec\u7ed3\u675f\u7b26\u3002 \u4f8b\u5b50\uff1a 1 2 std :: string s { \"012345678\" }; std :: cout << s . length () << '\\n' ; \u8f93\u51fa\uff1a 1 9 \u5c3d\u7ba1\u4f7f\u7528 length() \u5224\u65ad\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u6709\u5b57\u7b26\u4e5f\u662f\u53ef\u884c\u7684\uff0c\u4f46\u662f\u6700\u4f73\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 empty() \u51fd\u6570\uff1a bool string::empty() const \u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u6ca1\u6709\u5b57\u7b26\uff0c\u5219\u8fd4\u56de true \uff0c\u5426\u5219\u8fd4\u56de false \u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string string1 { \"Not Empty\" }; std :: cout << ( string1 . empty () ? \"true\" : \"false\" ) << '\\n' ; std :: string string2 ; // empty std :: cout << ( string2 . empty () ? \"true\" : \"false\" ) << '\\n' ; \u8f93\u51fa\uff1a 1 2 false true \u8fd8\u6709\u4e00\u4e2a\u4e0e\u5927\u5c0f\u76f8\u5173\u7684\u51fd\u6570\uff0c\u867d\u7136\u53ef\u80fd\u6c38\u8fdc\u90fd\u7528\u4e0d\u5230\uff0c\u4f46\u4e3a\u4e86\u5b8c\u6574\uff0c\u6211\u4eec\u51b3\u5b9a\u8fd8\u662f\u5c06\u5176\u5217\u5728\u8fd9\u91cc\uff1a size_type string::max_size() const \u8fd4\u56de\u5b57\u7b26\u4e32\u5141\u8bb8\u7684\u6700\u5927\u5b57\u7b26\u6570\uff1b \u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u548c\u7cfb\u7edf\u67b6\u6784\u7684\u4e0d\u540c\uff0c\u8be5\u503c\u4f1a\u6709\u6240\u4e0d\u540c\u3002 \u4f8b\u5b50\uff1a 1 2 std :: string s { \"MyString\" }; std :: cout << s . max_size () << '\\n' ; \u8f93\u51fa\uff1a 1 4294967294 \u5b57\u7b26\u4e32\u7684\u5bb9\u91cf \u5b57\u7b26\u4e32\u7684\u5bb9\u91cf\u53cd\u6620\u4e86\u8be5\u5b57\u7b26\u4e32\u5206\u914d\u591a\u5c11\u5185\u5b58\u6765\u4fdd\u5b58\u5176\u5185\u5bb9\u3002\u8be5\u503c\u7528\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u4e0d\u5305\u62ec\u7ec8\u6b62\u7b26\u3002\u4f8b\u5982\uff0c\u5bb9\u91cf\u4e3a8\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u5bb9\u7eb38\u4e2a\u5b57\u7b26\u3002 size_type string::capacity() const \u8fd4\u56de\u5b57\u7b26\u4e32\u5728\u4e0d\u91cd\u65b0\u5206\u914d\u7684\u60c5\u51b5\u4e0b\u53ef\u4ee5\u5bb9\u7eb3\u7684\u5b57\u7b26\u6570\u3002 \u4f8b\u5b50\uff1a 1 2 3 std :: string s { \"01234567\" }; std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; \u8f93\u51fa\uff1a 1 2 Length: 8 Capacity: 15 \u6ce8\u610f\uff0c\u8be5\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf\u5927\u4e8e\u5176\u957f\u5ea6\uff01\u867d\u7136\u6211\u4eec\u7684\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f8\uff0c\u4f46\u8be5\u5b57\u7b26\u4e32\u5b9e\u9645\u4e0a\u5206\u914d\u4e86\u8db3\u591f\u5b58\u653e15\u4e2a\u5b57\u7b26\u7684\u5185\u5b58\uff01\u4e3a\u4ec0\u4e48\u8981\u8fd9\u6837\u505a? \u8fd9\u91cc\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u7528\u6237\u5e0c\u671b\u5728\u5b57\u7b26\u4e32\u4e2d\u653e\u5165\u7684\u5b57\u7b26\u591a\u4e8e\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf\uff0c\u5219\u5b57\u7b26\u4e32\u9700\u8981\u91cd\u65b0\u5206\u914d\u66f4\u5927\u7684\u5bb9\u91cf\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u548c\u5bb9\u91cf\u90fd\u4e3a8\uff0c\u90a3\u4e48\u5411\u5b57\u7b26\u4e32\u4e2d\u6dfb\u52a0\u4efb\u4f55\u5b57\u7b26\u90fd\u5c06\u5f3a\u5236\u91cd\u65b0\u5206\u914d\u3002\u901a\u8fc7\u4f7f\u5bb9\u91cf\u5927\u4e8e\u5b9e\u9645\u5b57\u7b26\u4e32\uff0c\u8fd9\u4e3a\u7528\u6237\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7f13\u51b2\u7a7a\u95f4\uff0c\u4ee5\u4fbf\u5728\u9700\u8981\u8fdb\u884c\u91cd\u65b0\u5206\u914d\u4e4b\u524d\u80fd\u591f\u5b58\u653e\u4e00\u4e9b\u989d\u5916\u7684\u5b57\u7b26\u3002 \u4e8b\u5b9e\u8bc1\u660e\uff0c\u518d\u5206\u914d\u6709\u4ee5\u4e0b\u7f3a\u9677\uff1a \u9996\u5148\uff0c\u91cd\u65b0\u5206\u914d\u5b57\u7b26\u4e32\u5f00\u9500\u8f83\u5927\u3002\u9996\u5148\uff0c\u5fc5\u987b\u5206\u914d\u65b0\u7684\u5185\u5b58\u3002\u7136\u540e\u5fc5\u987b\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u590d\u5236\u5230\u65b0\u7684\u5185\u5b58\u4e2d\u3002\u5982\u679c\u5b57\u7b26\u4e32\u5f88\u5927\uff0c\u8fd9\u53ef\u80fd\u9700\u8981\u5f88\u957f\u65f6\u95f4\u3002\u6700\u540e\uff0c\u65e7\u7684\u5185\u5b58\u5fc5\u987b\u88ab\u91ca\u653e\u3002\u5982\u679c\u4f60\u6b63\u5728\u8fdb\u884c\u591a\u6b21\u91cd\u65b0\u5206\u914d\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4f1a\u6781\u5927\u5730\u62d6\u7d2f\u7a0b\u5e8f\u6027\u80fd\u3002 \u5176\u6b21\uff0c \u6bcf\u5f53\u4e00\u4e2a\u5b57\u7b26\u4e32\u88ab\u91cd\u65b0\u5206\u914d\u65f6\uff0c\u8be5\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u5c06\u653e\u7f6e\u5230\u4e00\u4e2a\u65b0\u7684\u5185\u5b58\u5730\u5740\u3002\u8fd9\u610f\u5473\u7740\u5bf9\u5b57\u7b26\u4e32\u7684\u6240\u6709\u5f15\u7528\u3001\u6307\u9488\u548c\u8fed\u4ee3\u5668\u90fd\u4f1a\u65e0\u6548\uff01 \u6ce8\u610f\uff0c\u5206\u914d\u7ed9\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf\u5e76\u4e0d\u603b\u662f\u5927\u4e8e\u957f\u5ea6\u3002\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 std :: string s { \"0123456789abcde\" }; std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 Length: 15 Capacity: 15 \uff08\u5b9e\u9645\u7ed3\u679c\u6839\u636e\u7f16\u8bd1\u5668\u7684\u4e0d\u540c\u4e5f\u53ef\u80fd\u4f1a\u4e0d\u540c\uff09 \u63a5\u4e0b\u6765\uff0c\u5728\u5b57\u7b26\u4e32\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5b57\u7b26\u5e76\u89c2\u5bdf\u5176\u5bb9\u91cf\u7684\u53d8\u5316: 1 2 3 4 5 6 7 8 std :: string s ( \"0123456789abcde\" ); std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; // Now add a new character s += \"f\" ; std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 Length: 15 Capacity: 15 Length: 16 Capacity: 31 void string::reserve() void string::reserve(size_type unSize) \u6b64\u51fd\u6570\u7684\u7b2c\u4e8c\u79cd\u5f62\u5f0f\u5c06\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf\u8bbe\u7f6e\u4e3a\u81f3\u5c11 unSize (\u53ef\u4ee5\u66f4\u5927)\u3002\u6ce8\u610f\uff0c\u8fd9\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u91cd\u65b0\u5206\u914d\u3002 \u5982\u679c\u8c03\u7528\u51fd\u6570\u7684\u7b2c\u4e00\u79cd\u5f62\u5f0f\uff0c\u6216\u8005\u8c03\u7528\u7b2c\u4e8c\u4e2a\u5473\u9053\u65f6 unSize \u5c0f\u4e8e\u5f53\u524d\u5bb9\u91cf\uff0c\u51fd\u6570\u5c06\u5c1d\u8bd5\u7f29\u5c0f\u5bb9\u91cf\u4ee5\u5339\u914d\u957f\u5ea6\u3002\u6839\u636e\u6267\u884c\u60c5\u51b5\uff0c\u53ef\u80fd\u4f1a\u5ffd\u7565\u8fd9\u4e2a\u7f29\u5c0f\u5bb9\u91cf\u7684\u8981\u6c42\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 std :: string s { \"01234567\" }; std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; s . reserve ( 200 ); std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; s . reserve (); std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; \u8f93\u51fa\uff1a 1 2 3 4 5 6 Length: 8 Capacity: 15 Length: 8 Capacity: 207 Length: 8 Capacity: 207 \u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u4e24\u4e2a\u6709\u8da3\u7684\u73b0\u8c61\u3002\u9996\u5148\uff0c\u867d\u7136\u6211\u4eec\u8981\u6c42\u7684\u5bb9\u91cf\u662f200\uff0c\u4f46\u5b9e\u9645\u4e0a\u6211\u4eec\u5f97\u5230\u7684\u5bb9\u91cf\u662f207\u3002\u5bb9\u91cf\u603b\u662f\u4fdd\u8bc1\u81f3\u5c11\u4e0e\u4f60\u7684\u8bf7\u6c42\u4e00\u6837\u5927\uff0c\u4f46\u4e5f\u53ef\u80fd\u66f4\u5927\u3002\u7136\u540e\u6211\u4eec\u8bf7\u6c42\u5bb9\u91cf\u66f4\u6539\u4ee5\u9002\u5e94\u5b57\u7b26\u4e32\u3002\u8be5\u8bf7\u6c42\u88ab\u5ffd\u7565\uff0c\u5bb9\u91cf\u5e76\u6ca1\u6709\u6539\u53d8\u3002 \u5982\u679c\u4f60\u63d0\u524d\u77e5\u9053\u4f60\u5c06\u901a\u8fc7\u5927\u91cf\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u6765\u6784\u9020\u4e00\u4e2a\u5f88\u957f\u5b57\u7b26\u4e32\uff08\u8fd9\u4f1a\u589e\u52a0\u5b57\u7b26\u4e32\u7684\u5927\u5c0f\uff09\uff0c\u5219\u5e94\u8be5\u5728\u5f00\u59cb\u65f6\u5c31\u9884\u7559\u8db3\u591f\u7684\u5bb9\u91cf\u6765\u907f\u514d\u91cd\u65b0\u5206\u914d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include #include // for rand() and srand() #include // for time() int main () { std :: srand ( std :: time ( nullptr )); // seed random number generator std :: string s {}; // length 0 s . reserve ( 64 ); // reserve 64 characters // Fill string up with random lower case characters for ( int count { 0 }; count < 64 ; ++ count ) s += 'a' + std :: rand () % 26 ; std :: cout << s ; } \u8fd9\u4e2a\u7a0b\u5e8f\u7684\u7ed3\u679c\u6bcf\u6b21\u90fd\u4f1a\u6539\u53d8\uff0c\u5176\u4e2d\u4e00\u6b21\u7684\u7ed3\u679c\u4e3a\uff1a 1 wzpzujwuaokbakgijqdawvzjqlgcipiiuuxhyfkdppxpyycvytvyxwqsbtielxpy \u4e3a\u4e86\u907f\u514d\u91cd\u65b0\u5206\u914d\uff0c\u8fd9\u91cc\u6211\u4eec\u5148\u8bbe\u7f6e\u4e00\u6b21\u5bb9\u91cf\uff0c\u7136\u540e\u586b\u5145\u5b57\u7b26\u4e32\u3002\u5f53\u901a\u8fc7\u8fde\u63a5\u6784\u9020\u957f\u5b57\u7b26\u4e32\u65f6\uff0c\u5176\u6027\u80fd\u5dee\u5f02\u662f\u975e\u5e38\u660e\u663e\u7684\u3002","title":"22.3 - std-string \u7684\u957f\u5ea6\u548c\u5bb9\u91cf"},{"location":"22-3-std-string-length-and-capacity/#_1","text":"\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u975e\u5e38\u7b80\u5355\u2014\u2014\u5373\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u4e2a\u6570\u3002\u6709\u4e24\u4e2a\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u786e\u5b9a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff1a size_type string::length() const size_type string::size() const \u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u53ef\u4ee5\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u4e2a\u6570\uff0c\u5305\u62ec\u7ed3\u675f\u7b26\u3002 \u4f8b\u5b50\uff1a 1 2 std :: string s { \"012345678\" }; std :: cout << s . length () << '\\n' ; \u8f93\u51fa\uff1a 1 9 \u5c3d\u7ba1\u4f7f\u7528 length() \u5224\u65ad\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u6709\u5b57\u7b26\u4e5f\u662f\u53ef\u884c\u7684\uff0c\u4f46\u662f\u6700\u4f73\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 empty() \u51fd\u6570\uff1a bool string::empty() const \u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u6ca1\u6709\u5b57\u7b26\uff0c\u5219\u8fd4\u56de true \uff0c\u5426\u5219\u8fd4\u56de false \u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string string1 { \"Not Empty\" }; std :: cout << ( string1 . empty () ? \"true\" : \"false\" ) << '\\n' ; std :: string string2 ; // empty std :: cout << ( string2 . empty () ? \"true\" : \"false\" ) << '\\n' ; \u8f93\u51fa\uff1a 1 2 false true \u8fd8\u6709\u4e00\u4e2a\u4e0e\u5927\u5c0f\u76f8\u5173\u7684\u51fd\u6570\uff0c\u867d\u7136\u53ef\u80fd\u6c38\u8fdc\u90fd\u7528\u4e0d\u5230\uff0c\u4f46\u4e3a\u4e86\u5b8c\u6574\uff0c\u6211\u4eec\u51b3\u5b9a\u8fd8\u662f\u5c06\u5176\u5217\u5728\u8fd9\u91cc\uff1a size_type string::max_size() const \u8fd4\u56de\u5b57\u7b26\u4e32\u5141\u8bb8\u7684\u6700\u5927\u5b57\u7b26\u6570\uff1b \u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u548c\u7cfb\u7edf\u67b6\u6784\u7684\u4e0d\u540c\uff0c\u8be5\u503c\u4f1a\u6709\u6240\u4e0d\u540c\u3002 \u4f8b\u5b50\uff1a 1 2 std :: string s { \"MyString\" }; std :: cout << s . max_size () << '\\n' ; \u8f93\u51fa\uff1a 1 4294967294","title":"\u5b57\u7b26\u4e32\u7684\u957f\u5ea6"},{"location":"22-3-std-string-length-and-capacity/#_2","text":"\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf\u53cd\u6620\u4e86\u8be5\u5b57\u7b26\u4e32\u5206\u914d\u591a\u5c11\u5185\u5b58\u6765\u4fdd\u5b58\u5176\u5185\u5bb9\u3002\u8be5\u503c\u7528\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u4e0d\u5305\u62ec\u7ec8\u6b62\u7b26\u3002\u4f8b\u5982\uff0c\u5bb9\u91cf\u4e3a8\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u5bb9\u7eb38\u4e2a\u5b57\u7b26\u3002 size_type string::capacity() const \u8fd4\u56de\u5b57\u7b26\u4e32\u5728\u4e0d\u91cd\u65b0\u5206\u914d\u7684\u60c5\u51b5\u4e0b\u53ef\u4ee5\u5bb9\u7eb3\u7684\u5b57\u7b26\u6570\u3002 \u4f8b\u5b50\uff1a 1 2 3 std :: string s { \"01234567\" }; std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; \u8f93\u51fa\uff1a 1 2 Length: 8 Capacity: 15 \u6ce8\u610f\uff0c\u8be5\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf\u5927\u4e8e\u5176\u957f\u5ea6\uff01\u867d\u7136\u6211\u4eec\u7684\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f8\uff0c\u4f46\u8be5\u5b57\u7b26\u4e32\u5b9e\u9645\u4e0a\u5206\u914d\u4e86\u8db3\u591f\u5b58\u653e15\u4e2a\u5b57\u7b26\u7684\u5185\u5b58\uff01\u4e3a\u4ec0\u4e48\u8981\u8fd9\u6837\u505a? \u8fd9\u91cc\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u7528\u6237\u5e0c\u671b\u5728\u5b57\u7b26\u4e32\u4e2d\u653e\u5165\u7684\u5b57\u7b26\u591a\u4e8e\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf\uff0c\u5219\u5b57\u7b26\u4e32\u9700\u8981\u91cd\u65b0\u5206\u914d\u66f4\u5927\u7684\u5bb9\u91cf\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u548c\u5bb9\u91cf\u90fd\u4e3a8\uff0c\u90a3\u4e48\u5411\u5b57\u7b26\u4e32\u4e2d\u6dfb\u52a0\u4efb\u4f55\u5b57\u7b26\u90fd\u5c06\u5f3a\u5236\u91cd\u65b0\u5206\u914d\u3002\u901a\u8fc7\u4f7f\u5bb9\u91cf\u5927\u4e8e\u5b9e\u9645\u5b57\u7b26\u4e32\uff0c\u8fd9\u4e3a\u7528\u6237\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7f13\u51b2\u7a7a\u95f4\uff0c\u4ee5\u4fbf\u5728\u9700\u8981\u8fdb\u884c\u91cd\u65b0\u5206\u914d\u4e4b\u524d\u80fd\u591f\u5b58\u653e\u4e00\u4e9b\u989d\u5916\u7684\u5b57\u7b26\u3002 \u4e8b\u5b9e\u8bc1\u660e\uff0c\u518d\u5206\u914d\u6709\u4ee5\u4e0b\u7f3a\u9677\uff1a \u9996\u5148\uff0c\u91cd\u65b0\u5206\u914d\u5b57\u7b26\u4e32\u5f00\u9500\u8f83\u5927\u3002\u9996\u5148\uff0c\u5fc5\u987b\u5206\u914d\u65b0\u7684\u5185\u5b58\u3002\u7136\u540e\u5fc5\u987b\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u590d\u5236\u5230\u65b0\u7684\u5185\u5b58\u4e2d\u3002\u5982\u679c\u5b57\u7b26\u4e32\u5f88\u5927\uff0c\u8fd9\u53ef\u80fd\u9700\u8981\u5f88\u957f\u65f6\u95f4\u3002\u6700\u540e\uff0c\u65e7\u7684\u5185\u5b58\u5fc5\u987b\u88ab\u91ca\u653e\u3002\u5982\u679c\u4f60\u6b63\u5728\u8fdb\u884c\u591a\u6b21\u91cd\u65b0\u5206\u914d\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4f1a\u6781\u5927\u5730\u62d6\u7d2f\u7a0b\u5e8f\u6027\u80fd\u3002 \u5176\u6b21\uff0c \u6bcf\u5f53\u4e00\u4e2a\u5b57\u7b26\u4e32\u88ab\u91cd\u65b0\u5206\u914d\u65f6\uff0c\u8be5\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u5c06\u653e\u7f6e\u5230\u4e00\u4e2a\u65b0\u7684\u5185\u5b58\u5730\u5740\u3002\u8fd9\u610f\u5473\u7740\u5bf9\u5b57\u7b26\u4e32\u7684\u6240\u6709\u5f15\u7528\u3001\u6307\u9488\u548c\u8fed\u4ee3\u5668\u90fd\u4f1a\u65e0\u6548\uff01 \u6ce8\u610f\uff0c\u5206\u914d\u7ed9\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf\u5e76\u4e0d\u603b\u662f\u5927\u4e8e\u957f\u5ea6\u3002\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 std :: string s { \"0123456789abcde\" }; std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 Length: 15 Capacity: 15 \uff08\u5b9e\u9645\u7ed3\u679c\u6839\u636e\u7f16\u8bd1\u5668\u7684\u4e0d\u540c\u4e5f\u53ef\u80fd\u4f1a\u4e0d\u540c\uff09 \u63a5\u4e0b\u6765\uff0c\u5728\u5b57\u7b26\u4e32\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5b57\u7b26\u5e76\u89c2\u5bdf\u5176\u5bb9\u91cf\u7684\u53d8\u5316: 1 2 3 4 5 6 7 8 std :: string s ( \"0123456789abcde\" ); std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; // Now add a new character s += \"f\" ; std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 Length: 15 Capacity: 15 Length: 16 Capacity: 31 void string::reserve() void string::reserve(size_type unSize) \u6b64\u51fd\u6570\u7684\u7b2c\u4e8c\u79cd\u5f62\u5f0f\u5c06\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf\u8bbe\u7f6e\u4e3a\u81f3\u5c11 unSize (\u53ef\u4ee5\u66f4\u5927)\u3002\u6ce8\u610f\uff0c\u8fd9\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u91cd\u65b0\u5206\u914d\u3002 \u5982\u679c\u8c03\u7528\u51fd\u6570\u7684\u7b2c\u4e00\u79cd\u5f62\u5f0f\uff0c\u6216\u8005\u8c03\u7528\u7b2c\u4e8c\u4e2a\u5473\u9053\u65f6 unSize \u5c0f\u4e8e\u5f53\u524d\u5bb9\u91cf\uff0c\u51fd\u6570\u5c06\u5c1d\u8bd5\u7f29\u5c0f\u5bb9\u91cf\u4ee5\u5339\u914d\u957f\u5ea6\u3002\u6839\u636e\u6267\u884c\u60c5\u51b5\uff0c\u53ef\u80fd\u4f1a\u5ffd\u7565\u8fd9\u4e2a\u7f29\u5c0f\u5bb9\u91cf\u7684\u8981\u6c42\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 std :: string s { \"01234567\" }; std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; s . reserve ( 200 ); std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; s . reserve (); std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; \u8f93\u51fa\uff1a 1 2 3 4 5 6 Length: 8 Capacity: 15 Length: 8 Capacity: 207 Length: 8 Capacity: 207 \u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u4e24\u4e2a\u6709\u8da3\u7684\u73b0\u8c61\u3002\u9996\u5148\uff0c\u867d\u7136\u6211\u4eec\u8981\u6c42\u7684\u5bb9\u91cf\u662f200\uff0c\u4f46\u5b9e\u9645\u4e0a\u6211\u4eec\u5f97\u5230\u7684\u5bb9\u91cf\u662f207\u3002\u5bb9\u91cf\u603b\u662f\u4fdd\u8bc1\u81f3\u5c11\u4e0e\u4f60\u7684\u8bf7\u6c42\u4e00\u6837\u5927\uff0c\u4f46\u4e5f\u53ef\u80fd\u66f4\u5927\u3002\u7136\u540e\u6211\u4eec\u8bf7\u6c42\u5bb9\u91cf\u66f4\u6539\u4ee5\u9002\u5e94\u5b57\u7b26\u4e32\u3002\u8be5\u8bf7\u6c42\u88ab\u5ffd\u7565\uff0c\u5bb9\u91cf\u5e76\u6ca1\u6709\u6539\u53d8\u3002 \u5982\u679c\u4f60\u63d0\u524d\u77e5\u9053\u4f60\u5c06\u901a\u8fc7\u5927\u91cf\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u6765\u6784\u9020\u4e00\u4e2a\u5f88\u957f\u5b57\u7b26\u4e32\uff08\u8fd9\u4f1a\u589e\u52a0\u5b57\u7b26\u4e32\u7684\u5927\u5c0f\uff09\uff0c\u5219\u5e94\u8be5\u5728\u5f00\u59cb\u65f6\u5c31\u9884\u7559\u8db3\u591f\u7684\u5bb9\u91cf\u6765\u907f\u514d\u91cd\u65b0\u5206\u914d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include #include // for rand() and srand() #include // for time() int main () { std :: srand ( std :: time ( nullptr )); // seed random number generator std :: string s {}; // length 0 s . reserve ( 64 ); // reserve 64 characters // Fill string up with random lower case characters for ( int count { 0 }; count < 64 ; ++ count ) s += 'a' + std :: rand () % 26 ; std :: cout << s ; } \u8fd9\u4e2a\u7a0b\u5e8f\u7684\u7ed3\u679c\u6bcf\u6b21\u90fd\u4f1a\u6539\u53d8\uff0c\u5176\u4e2d\u4e00\u6b21\u7684\u7ed3\u679c\u4e3a\uff1a 1 wzpzujwuaokbakgijqdawvzjqlgcipiiuuxhyfkdppxpyycvytvyxwqsbtielxpy \u4e3a\u4e86\u907f\u514d\u91cd\u65b0\u5206\u914d\uff0c\u8fd9\u91cc\u6211\u4eec\u5148\u8bbe\u7f6e\u4e00\u6b21\u5bb9\u91cf\uff0c\u7136\u540e\u586b\u5145\u5b57\u7b26\u4e32\u3002\u5f53\u901a\u8fc7\u8fde\u63a5\u6784\u9020\u957f\u5b57\u7b26\u4e32\u65f6\uff0c\u5176\u6027\u80fd\u5dee\u5f02\u662f\u975e\u5e38\u660e\u663e\u7684\u3002","title":"\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf"},{"location":"22-4-std-string-character-access-and-conversion-to-C-style-arrays/","tags":["string"],"text":"Key Takeaway \u8bbf\u95ee\u5b57\u7b26 \u8bbf\u95ee\u5b57\u7b26\u4e5f\u6709\u4e24\u4e2a\u6781\u5176\u7c7b\u4f3c\u7684\u65b9\u6cd5\u3002\u6700\u7b80\u5355\u4e5f\u662f\u6700\u5757\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 \uff1a char& string::operator[] (size_type nIndex) const char& string::operator[] (size_type nIndex) const \u51fd\u6570\u8fd4\u56de\u4f4d\u4e8e\u7d22\u5f15 nIndex \u5904\u7684\u5b57\u7b26\uff1b \u4f20\u5165\u975e\u6cd5\u7d22\u5f15\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \uff1b \u56e0\u4e3a\u8fd4\u56de\u7c7b\u578b\u662f char& \u6240\u4ee5\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u4fee\u6539\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sSource { \"abcdefg\" }; std :: cout << sSource [ 5 ] << '\\n' ; sSource [ 5 ] = 'X' ; std :: cout << sSource << '\\n' ; \u8f93\u51fa\uff1a 1 2 f abcdeXg \u6b64\u5916\uff0c\u8fd8\u6709\u4e00\u4e2a\u975e\u64cd\u4f5c\u7b26\u7684\u7248\u672c\u3002\u8be5\u7248\u672c\u7684\u51fd\u6570\u901f\u5ea6\u4f1a\u6162\u4e00\u4e9b\uff0c\u56e0\u4e3a\u5b83==\u4f1a\u68c0\u67e5\u6570\u7ec4\u4e0b\u6807\u662f\u5426\u5408\u6cd5==\u3002\u5982\u679c\u4f60\u4e0d\u786e\u5b9a\u7d22\u5f15 nIndex \u662f\u5426\u4e00\u5b9a\u6709\u6548\uff0c\u5219\u6700\u597d\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u6765\u8bbf\u95ee\uff1a char& string::at (size_type nIndex) const char& string::at (size_type nIndex) const \u51fd\u6570\u8fd4\u56de\u4f4d\u4e8e\u7d22\u5f15 nIndex \u5904\u7684\u5b57\u7b26\uff1b \u4f20\u5165\u975e\u6cd5\u7d22\u5f15\u4f1a\u5bfc\u81f4 out_of_range \u5f02\u5e38\uff1b \u56e0\u4e3a\u8fd4\u56de\u7c7b\u578b\u662f char& \u6240\u4ee5\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u4fee\u6539\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sSource { \"abcdefg\" }; std :: cout << sSource . at ( 5 ) << '\\n' ; sSource . at ( 5 ) = 'X' ; std :: cout << sSource << '\\n' ; \u8f93\u51fa 1 2 f abcdeXg \u8f6c\u6362\u4e3aC\u98ce\u683c\u6570\u7ec4 \u5f88\u591a\u51fd\u6570\uff08\u5305\u62ec\u6240\u6709C\u8bed\u8a00\u51fd\u6570\uff09\u671f\u671b\u7684\u8f93\u5165\u662fC\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u800c\u4e0d\u662f std::string \u3002\u56e0\u6b64\uff0c std::string \u63d0\u4f9b\u4e86\u4e09\u79cd\u5c06 std::string \u8f6c\u6362\u4e3aC\u8bed\u8a00\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff1a const char* string::c_str () const \u5c06\u5b57\u7b26\u4e32\u5185\u5bb9\u4f5c\u4e3aC\u98ce\u683c\u5b57\u7b26\u4e32\u5e38\u91cf\u8fd4\u56de\uff1b \u7ed3\u5c3e\u4f1a\u6dfb\u52a0\u7ed3\u675f\u7b26\uff1b \u8be5C\u98ce\u683c\u5b57\u7b26\u4e32\u5f52 std::string \u6240\u6709\u4e14\u4e0d\u5e94\u8be5\u88ab\u624b\u52a8\u91ca\u653e\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 #include std :: string sSource { \"abcdefg\" }; std :: cout << std :: strlen ( sSource . c_str ()); \u8f93\u51fa\uff1a 1 7 const char* string::data () const \u5c06\u5b57\u7b26\u4e32\u5185\u5bb9\u4f5c\u4e3aC\u98ce\u683c\u5b57\u7b26\u4e32\u5e38\u91cf\u8fd4\u56de\uff1b \u7ed3\u5c3e\u4f1a\u6dfb\u52a0\u7ed3\u675f\u7b26\uff1b\u8be5\u51fd\u6570\u884c\u4e3a\u4e0e c_str() \u662f\u4e00\u6837\u7684\uff1b \u8be5C\u98ce\u683c\u5b57\u7b26\u4e32\u5f52 std::string \u6240\u6709\u4e14\u4e0d\u5e94\u8be5\u88ab\u624b\u52a8\u91ca\u653e\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 #include std :: string sSource { \"abcdefg\" }; const char * szString { \"abcdefg\" }; // memcmp compares the first n characters of two C-style strings and returns 0 if they are equal if ( std :: memcmp ( sSource . data (), szString , sSource . length ()) == 0 ) std :: cout << \"The strings are equal\" ; else std :: cout << \"The strings are not equal\" ; \u8f93\u51fa\uff1a 1 The strings are equal size_type string::copy(char* szBuf, size_type nLength, size_type nIndex = 0) const \u4ece\u5b57\u7b26\u4e32\u4e2d\u62f7\u8d1d\u81f3\u591a nLength \u4e2a\u5b57\u7b26\u5230 szBuf \uff0c\u4ece\u7d22\u5f15 nIndex \u5f00\u59cb\uff1b \u8fd4\u56de\u6210\u529f\u62f7\u8d1d\u7684\u5b57\u7b26\u7684\u4e2a\u6570\uff1b \u5e76\u4e0d\u4f1a\u6dfb\u52a0\u7ed3\u675f\u7b26 \u3002\u6761\u7528\u5728\u5fc5\u987b\u4fdd\u8bc1 szBuf \u88ab\u521d\u59cb\u5316\u4e3a\u7a7a\u6216\u57fa\u4e8e\u8fd4\u56de\u7684\u957f\u5ea6\u4e3a\u5b57\u7b26\u4e32\u6dfb\u52a0\u7ed3\u675f\u7b26\uff1b \u8c03\u7528\u8005\u9700\u8981\u786e\u4fdd szBuf \u4e0d\u6ea2\u51fa\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 std :: string sSource { \"sphinx of black quartz, judge my vow\" }; char szBuf [ 20 ]; int nLength { static_cast < int > ( sSource . copy ( szBuf , 5 , 10 )) }; szBuf [ nLength ] = '\\0' ; // Make sure we terminate the string in the buffer std :: cout << szBuf << '\\n' ; \u8f93\u51fa\uff1a 1 black \u8fd9\u4e2a\u51fd\u6570\u5e94\u8be5\u5c3d\u53ef\u80fd\u907f\u514d\uff0c\u56e0\u4e3a\u5176\u76f8\u5bf9\u6bd4\u8f83\u5371\u9669\u7684(\u56e0\u4e3a\u9700\u8981\u7531\u8c03\u7528\u8005\u63d0\u4f9b\u7ed3\u675f\u7b26\u5e76\u907f\u514d\u7f13\u51b2\u533a\u6ea2\u51fa)\u3002","title":"22.4 - std-string \u5b57\u7b26\u8bbf\u95ee\u548cC\u5b57\u7b26\u6570\u7ec4\u8f6c\u6362"},{"location":"22-4-std-string-character-access-and-conversion-to-C-style-arrays/#_1","text":"\u8bbf\u95ee\u5b57\u7b26\u4e5f\u6709\u4e24\u4e2a\u6781\u5176\u7c7b\u4f3c\u7684\u65b9\u6cd5\u3002\u6700\u7b80\u5355\u4e5f\u662f\u6700\u5757\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 \uff1a char& string::operator[] (size_type nIndex) const char& string::operator[] (size_type nIndex) const \u51fd\u6570\u8fd4\u56de\u4f4d\u4e8e\u7d22\u5f15 nIndex \u5904\u7684\u5b57\u7b26\uff1b \u4f20\u5165\u975e\u6cd5\u7d22\u5f15\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \uff1b \u56e0\u4e3a\u8fd4\u56de\u7c7b\u578b\u662f char& \u6240\u4ee5\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u4fee\u6539\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sSource { \"abcdefg\" }; std :: cout << sSource [ 5 ] << '\\n' ; sSource [ 5 ] = 'X' ; std :: cout << sSource << '\\n' ; \u8f93\u51fa\uff1a 1 2 f abcdeXg \u6b64\u5916\uff0c\u8fd8\u6709\u4e00\u4e2a\u975e\u64cd\u4f5c\u7b26\u7684\u7248\u672c\u3002\u8be5\u7248\u672c\u7684\u51fd\u6570\u901f\u5ea6\u4f1a\u6162\u4e00\u4e9b\uff0c\u56e0\u4e3a\u5b83==\u4f1a\u68c0\u67e5\u6570\u7ec4\u4e0b\u6807\u662f\u5426\u5408\u6cd5==\u3002\u5982\u679c\u4f60\u4e0d\u786e\u5b9a\u7d22\u5f15 nIndex \u662f\u5426\u4e00\u5b9a\u6709\u6548\uff0c\u5219\u6700\u597d\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u6765\u8bbf\u95ee\uff1a char& string::at (size_type nIndex) const char& string::at (size_type nIndex) const \u51fd\u6570\u8fd4\u56de\u4f4d\u4e8e\u7d22\u5f15 nIndex \u5904\u7684\u5b57\u7b26\uff1b \u4f20\u5165\u975e\u6cd5\u7d22\u5f15\u4f1a\u5bfc\u81f4 out_of_range \u5f02\u5e38\uff1b \u56e0\u4e3a\u8fd4\u56de\u7c7b\u578b\u662f char& \u6240\u4ee5\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u4fee\u6539\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sSource { \"abcdefg\" }; std :: cout << sSource . at ( 5 ) << '\\n' ; sSource . at ( 5 ) = 'X' ; std :: cout << sSource << '\\n' ; \u8f93\u51fa 1 2 f abcdeXg","title":"\u8bbf\u95ee\u5b57\u7b26"},{"location":"22-4-std-string-character-access-and-conversion-to-C-style-arrays/#c","text":"\u5f88\u591a\u51fd\u6570\uff08\u5305\u62ec\u6240\u6709C\u8bed\u8a00\u51fd\u6570\uff09\u671f\u671b\u7684\u8f93\u5165\u662fC\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u800c\u4e0d\u662f std::string \u3002\u56e0\u6b64\uff0c std::string \u63d0\u4f9b\u4e86\u4e09\u79cd\u5c06 std::string \u8f6c\u6362\u4e3aC\u8bed\u8a00\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff1a const char* string::c_str () const \u5c06\u5b57\u7b26\u4e32\u5185\u5bb9\u4f5c\u4e3aC\u98ce\u683c\u5b57\u7b26\u4e32\u5e38\u91cf\u8fd4\u56de\uff1b \u7ed3\u5c3e\u4f1a\u6dfb\u52a0\u7ed3\u675f\u7b26\uff1b \u8be5C\u98ce\u683c\u5b57\u7b26\u4e32\u5f52 std::string \u6240\u6709\u4e14\u4e0d\u5e94\u8be5\u88ab\u624b\u52a8\u91ca\u653e\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 #include std :: string sSource { \"abcdefg\" }; std :: cout << std :: strlen ( sSource . c_str ()); \u8f93\u51fa\uff1a 1 7 const char* string::data () const \u5c06\u5b57\u7b26\u4e32\u5185\u5bb9\u4f5c\u4e3aC\u98ce\u683c\u5b57\u7b26\u4e32\u5e38\u91cf\u8fd4\u56de\uff1b \u7ed3\u5c3e\u4f1a\u6dfb\u52a0\u7ed3\u675f\u7b26\uff1b\u8be5\u51fd\u6570\u884c\u4e3a\u4e0e c_str() \u662f\u4e00\u6837\u7684\uff1b \u8be5C\u98ce\u683c\u5b57\u7b26\u4e32\u5f52 std::string \u6240\u6709\u4e14\u4e0d\u5e94\u8be5\u88ab\u624b\u52a8\u91ca\u653e\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 #include std :: string sSource { \"abcdefg\" }; const char * szString { \"abcdefg\" }; // memcmp compares the first n characters of two C-style strings and returns 0 if they are equal if ( std :: memcmp ( sSource . data (), szString , sSource . length ()) == 0 ) std :: cout << \"The strings are equal\" ; else std :: cout << \"The strings are not equal\" ; \u8f93\u51fa\uff1a 1 The strings are equal size_type string::copy(char* szBuf, size_type nLength, size_type nIndex = 0) const \u4ece\u5b57\u7b26\u4e32\u4e2d\u62f7\u8d1d\u81f3\u591a nLength \u4e2a\u5b57\u7b26\u5230 szBuf \uff0c\u4ece\u7d22\u5f15 nIndex \u5f00\u59cb\uff1b \u8fd4\u56de\u6210\u529f\u62f7\u8d1d\u7684\u5b57\u7b26\u7684\u4e2a\u6570\uff1b \u5e76\u4e0d\u4f1a\u6dfb\u52a0\u7ed3\u675f\u7b26 \u3002\u6761\u7528\u5728\u5fc5\u987b\u4fdd\u8bc1 szBuf \u88ab\u521d\u59cb\u5316\u4e3a\u7a7a\u6216\u57fa\u4e8e\u8fd4\u56de\u7684\u957f\u5ea6\u4e3a\u5b57\u7b26\u4e32\u6dfb\u52a0\u7ed3\u675f\u7b26\uff1b \u8c03\u7528\u8005\u9700\u8981\u786e\u4fdd szBuf \u4e0d\u6ea2\u51fa\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 std :: string sSource { \"sphinx of black quartz, judge my vow\" }; char szBuf [ 20 ]; int nLength { static_cast < int > ( sSource . copy ( szBuf , 5 , 10 )) }; szBuf [ nLength ] = '\\0' ; // Make sure we terminate the string in the buffer std :: cout << szBuf << '\\n' ; \u8f93\u51fa\uff1a 1 black \u8fd9\u4e2a\u51fd\u6570\u5e94\u8be5\u5c3d\u53ef\u80fd\u907f\u514d\uff0c\u56e0\u4e3a\u5176\u76f8\u5bf9\u6bd4\u8f83\u5371\u9669\u7684(\u56e0\u4e3a\u9700\u8981\u7531\u8c03\u7528\u8005\u63d0\u4f9b\u7ed3\u675f\u7b26\u5e76\u907f\u514d\u7f13\u51b2\u533a\u6ea2\u51fa)\u3002","title":"\u8f6c\u6362\u4e3aC\u98ce\u683c\u6570\u7ec4"},{"location":"22-5-std-string-assignment-and-swapping/","tags":["string"],"text":"Key Takeaway String \u8d4b\u503c \u4e3a\u5b57\u7b26\u4e32\u8d4b\u503c\u7684\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26 \uff0c\u6b64\u5916\uff0c assign() \u51fd\u6570\u4e5f\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u7684\u529f\u80fd\u3002 string& string::operator= (const string& str) string& string::assign (const string& str) string& string::operator= (const char* str) string& string::assign (const char* str) string& string::operator= (char c) \u8fd9\u4e9b\u51fd\u6570\u90fd\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u8d4b\u503c\uff1b \u51fd\u6570\u8fd4\u56de *this \uff0c\u6240\u4ee5\u53ef\u4ee5\u8fdb\u884c\u94fe\u5f0f\u8c03\u7528\uff1b \u6ce8\u610f\uff0c assign() \u51fd\u6570\u7684\u5404\u4e2a\u7248\u672c\u90fd\u4e0d\u80fd\u63a5\u6536\u4e00\u4e2a\u5355\u72ec\u5b57\u7b26\u4f5c\u4e3a\u53c2\u6570\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 std :: string sString ; // Assign a string value sString = std :: string ( \"One\" ); std :: cout << sString << '\\n' ; const std :: string sTwo ( \"Two\" ); sString . assign ( sTwo ); std :: cout << sString << '\\n' ; // Assign a C-style string sString = \"Three\" ; std :: cout << sString << '\\n' ; sString . assign ( \"Four\" ); std :: cout << sString << '\\n' ; // Assign a char sString = '5' ; std :: cout << sString << '\\n' ; // Chain assignment std :: string sOther ; sString = sOther = \"Six\" ; std :: cout << sString << ' ' << sOther << '\\n' ; \u8f93\u51fa\uff1a 1 2 3 4 5 6 One Two Three Four 5 Six Six assign() \u6210\u5458\u51fd\u6570\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u7248\u672c\uff1a string& string::assign (const string& str, size_type index, size_type len) \u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u5b50\u4e32\u8d4b\u503c\u7ed9\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4ece index \u5f00\u59cb\uff0c\u957f\u5ea6\u4e3a len \u5982\u679c\u7d22\u5f15 index \u8d8a\u754c\uff0c\u5219\u629b\u51fa out_of_range \u5f02\u5e38\uff1b \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 const std :: string sSource ( \"abcdefg\" ); std :: string sDest ; sDest . assign ( sSource , 2 , 4 ); // assign a substring of source from index 2 of length 4 std :: cout << sDest << '\\n' ; \u8f93\u51fa\uff1a 1 cdef string& string::assign (const char* chars, size_type len) \u5c06C\u98ce\u683c\u5b57\u7b26\u4e32\u6570\u7ec4\u4e2d\u7684 len \u4e2a\u5b57\u7b26\u8d4b\u503c\u7ed9\u5b57\u7b26\u4e32\uff1b \u5982\u679c\u8981\u62f7\u8d1d\u7684\u957f\u5ea6\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\uff0c\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sDest ; sDest . assign ( \"abcdefg\" , 4 ); std :: cout << sDest << '\\n' ; \u8f93\u51fa\uff1a 1 abcd \u8fd9\u4e2a\u51fd\u6570\u5b58\u5728\u6f5c\u5728\u98ce\u9669\uff0c\u4e0d\u5efa\u8bae\u4f7f\u7528\u3002 string& string::assign (size_type len, char c) \u5c06\u5b57\u7b26c\u91cd\u590d len \u6b21\u8d4b\u503c\u7ed9\u5b57\u7b26\u4e32\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\uff0c\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sDest ; sDest . assign ( 4 , 'g' ); std :: cout << sDest << '\\n' ; \u8f93\u51fa\uff1a 1 gggg \u4ea4\u6362\u5b57\u7b26\u4e32 \u5982\u679c\u4f60\u6709\u4e24\u4e2a\u5b57\u7b26\u4e32\u5e76\u5e0c\u671b\u4ea4\u6362\u5b83\u4eec\u7684\u503c\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528\u4e24\u4e2a\u540d\u4e3a swap() \u7684\u51fd\u6570\u3002 void string::swap (string& str) void swap (string& str1, string& str2) \u4e24\u4e2a\u51fd\u6570\u90fd\u53ef\u4ee5\u4ea4\u6362\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u503c\u3002\u6210\u5458\u51fd\u6570\u4f1a\u4ea4\u6362 *this \u548c str \uff0c\u5168\u5c40\u51fd\u6570\u5219\u4ea4\u6362 str1 \u548c str2 \uff1b \u8fd9\u4e9b\u51fd\u6570\u662f\u9ad8\u6548\u7684\uff0c\u5e94\u8be5\u7528\u6765\u4ee3\u66ff\u8d4b\u503c\u6765\u6267\u884c\u5b57\u7b26\u4e32\u4ea4\u6362\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 std :: string sStr1 ( \"red\" ); std :: string sStr2 ( \"blue\" ); std :: cout << sStr1 << ' ' << sStr2 << '\\n' ; swap ( sStr1 , sStr2 ); std :: cout << sStr1 << ' ' << sStr2 << '\\n' ; sStr1 . swap ( sStr2 ); std :: cout << sStr1 << ' ' << sStr2 << '\\n' ; \u8f93\u51fa\uff1a 1 2 3 red blue blue red red blue","title":"22.5 - std-string \u8d4b\u503c\u548c\u4ea4\u6362"},{"location":"22-5-std-string-assignment-and-swapping/#string","text":"\u4e3a\u5b57\u7b26\u4e32\u8d4b\u503c\u7684\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26 \uff0c\u6b64\u5916\uff0c assign() \u51fd\u6570\u4e5f\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u7684\u529f\u80fd\u3002 string& string::operator= (const string& str) string& string::assign (const string& str) string& string::operator= (const char* str) string& string::assign (const char* str) string& string::operator= (char c) \u8fd9\u4e9b\u51fd\u6570\u90fd\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u8d4b\u503c\uff1b \u51fd\u6570\u8fd4\u56de *this \uff0c\u6240\u4ee5\u53ef\u4ee5\u8fdb\u884c\u94fe\u5f0f\u8c03\u7528\uff1b \u6ce8\u610f\uff0c assign() \u51fd\u6570\u7684\u5404\u4e2a\u7248\u672c\u90fd\u4e0d\u80fd\u63a5\u6536\u4e00\u4e2a\u5355\u72ec\u5b57\u7b26\u4f5c\u4e3a\u53c2\u6570\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 std :: string sString ; // Assign a string value sString = std :: string ( \"One\" ); std :: cout << sString << '\\n' ; const std :: string sTwo ( \"Two\" ); sString . assign ( sTwo ); std :: cout << sString << '\\n' ; // Assign a C-style string sString = \"Three\" ; std :: cout << sString << '\\n' ; sString . assign ( \"Four\" ); std :: cout << sString << '\\n' ; // Assign a char sString = '5' ; std :: cout << sString << '\\n' ; // Chain assignment std :: string sOther ; sString = sOther = \"Six\" ; std :: cout << sString << ' ' << sOther << '\\n' ; \u8f93\u51fa\uff1a 1 2 3 4 5 6 One Two Three Four 5 Six Six assign() \u6210\u5458\u51fd\u6570\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u7248\u672c\uff1a string& string::assign (const string& str, size_type index, size_type len) \u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u5b50\u4e32\u8d4b\u503c\u7ed9\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4ece index \u5f00\u59cb\uff0c\u957f\u5ea6\u4e3a len \u5982\u679c\u7d22\u5f15 index \u8d8a\u754c\uff0c\u5219\u629b\u51fa out_of_range \u5f02\u5e38\uff1b \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 const std :: string sSource ( \"abcdefg\" ); std :: string sDest ; sDest . assign ( sSource , 2 , 4 ); // assign a substring of source from index 2 of length 4 std :: cout << sDest << '\\n' ; \u8f93\u51fa\uff1a 1 cdef string& string::assign (const char* chars, size_type len) \u5c06C\u98ce\u683c\u5b57\u7b26\u4e32\u6570\u7ec4\u4e2d\u7684 len \u4e2a\u5b57\u7b26\u8d4b\u503c\u7ed9\u5b57\u7b26\u4e32\uff1b \u5982\u679c\u8981\u62f7\u8d1d\u7684\u957f\u5ea6\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\uff0c\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sDest ; sDest . assign ( \"abcdefg\" , 4 ); std :: cout << sDest << '\\n' ; \u8f93\u51fa\uff1a 1 abcd \u8fd9\u4e2a\u51fd\u6570\u5b58\u5728\u6f5c\u5728\u98ce\u9669\uff0c\u4e0d\u5efa\u8bae\u4f7f\u7528\u3002 string& string::assign (size_type len, char c) \u5c06\u5b57\u7b26c\u91cd\u590d len \u6b21\u8d4b\u503c\u7ed9\u5b57\u7b26\u4e32\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\uff0c\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sDest ; sDest . assign ( 4 , 'g' ); std :: cout << sDest << '\\n' ; \u8f93\u51fa\uff1a 1 gggg","title":"String \u8d4b\u503c"},{"location":"22-5-std-string-assignment-and-swapping/#_1","text":"\u5982\u679c\u4f60\u6709\u4e24\u4e2a\u5b57\u7b26\u4e32\u5e76\u5e0c\u671b\u4ea4\u6362\u5b83\u4eec\u7684\u503c\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528\u4e24\u4e2a\u540d\u4e3a swap() \u7684\u51fd\u6570\u3002 void string::swap (string& str) void swap (string& str1, string& str2) \u4e24\u4e2a\u51fd\u6570\u90fd\u53ef\u4ee5\u4ea4\u6362\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u503c\u3002\u6210\u5458\u51fd\u6570\u4f1a\u4ea4\u6362 *this \u548c str \uff0c\u5168\u5c40\u51fd\u6570\u5219\u4ea4\u6362 str1 \u548c str2 \uff1b \u8fd9\u4e9b\u51fd\u6570\u662f\u9ad8\u6548\u7684\uff0c\u5e94\u8be5\u7528\u6765\u4ee3\u66ff\u8d4b\u503c\u6765\u6267\u884c\u5b57\u7b26\u4e32\u4ea4\u6362\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 std :: string sStr1 ( \"red\" ); std :: string sStr2 ( \"blue\" ); std :: cout << sStr1 << ' ' << sStr2 << '\\n' ; swap ( sStr1 , sStr2 ); std :: cout << sStr1 << ' ' << sStr2 << '\\n' ; sStr1 . swap ( sStr2 ); std :: cout << sStr1 << ' ' << sStr2 << '\\n' ; \u8f93\u51fa\uff1a 1 2 3 red blue blue red red blue","title":"\u4ea4\u6362\u5b57\u7b26\u4e32"},{"location":"22-6-std-string-appending/","tags":["string"],"text":"Key Takeaway \u8ffd\u52a0\u5b57\u7b26\u4e32 \u5728\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u8ffd\u52a0\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528 operator+= \u6216 append() \u6216 push_back() \u3002 string& string::operator+= (const string& str) string& string::append (const string& str) \u8fd9\u4e9b\u51fd\u6570\u90fd\u53ef\u4ee5\u5c06 str \u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\u3002 Sample code: 1 2 3 4 5 6 7 8 std :: string sString { \"one\" }; sString += std :: string { \" two\" }; std :: string sThree { \" three\" }; sString . append ( sThree ); std :: cout << sString << '\\n' ; Output: 1 one two three append() \u51fd\u6570\u4e5f\u6709\u51e0\u79cd\uff0c\u53ef\u4ee5\u7528\u4e8e\u6dfb\u52a0\u4e00\u4e2a\u5b50\u4e32\uff1a string& string::append (const string& str, size_type index, size_type num) \u8be5\u51fd\u6570\u5c06 str \u4e2d\u4ece\u7d22\u5f15 index \u5f00\u59cb\u7684 num \u4e2a\u5b57\u7b26\u4e32\u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\u672b\u5c3e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u629b\u51fa out_of_range \uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 std :: string sString { \"one \" }; const std :: string sTemp { \"twothreefour\" }; sString . append ( sTemp , 3 , 5 ); // append substring of sTemp starting at index 3 of length 5 std :: cout << sString << '\\n' ; \u8f93\u51fa\uff1a 1 one three Operator+= \u548c append() \u4e5f\u63d0\u4f9b\u4e86\u914d\u5408C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u5de5\u4f5c\u7684\u7248\u672c\uff1a string& string::operator+= (const char* str) string& string::append (const char* str) \u8fd9\u4e9b\u51fd\u6570\u90fd\u53ef\u4ee5\u5c06 str \u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b str \u4e0d\u80fd\u662f NULL\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 std :: string sString { \"one\" }; sString += \" two\" ; sString . append ( \" three\" ); std :: cout << sString << '\\n' ; \u8f93\u51fa\uff1a 1 one two three append() \u8fd8\u6709\u53e6\u5916\u4e00\u4e2a\u914d\u5408C\u8bed\u8a00\u5b57\u7b26\u4e32\u5de5\u4f5c\u7684\u7248\u672c\uff1a string& string::append (const char* str, size_type len) \u5c06 str \u7684\u524d len \u4e2a\u5b57\u7b26\u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f1a\u5ffd\u7565\u7279\u6b8a\u5b57\u7b26(\u5305\u62ec \u201d )\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sString { \"one \" }; sString . append ( \"threefour\" , 5 ); std :: cout << sString << '\\n' ; \u8f93\u51fa\uff1a 1 one three \u8be5\u51fd\u6570\u4e0d\u5b89\u5168\uff0c\u4e0d\u63a8\u8350\u4f7f\u7528\u3002 \u8fd8\u6709\u53e6\u5916\u4e00\u4e9b\u53ef\u4ee5\u8ffd\u52a0 \u5b57\u7b26 \u7684\u51fd\u6570\uff0c\u6ce8\u610f\uff0c\u51fd\u6570\u540d\u662f push_back() \u800c\u4e0d\u662f append() \uff01 string& string::operator+= (char c) void string::push_back (char c) \u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u53ef\u4ee5\u5c06\u5b57\u7b26c\u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\uff1b operator += \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f8b\u5b50\uff1a 1 2 3 4 5 std :: string sString { \"one\" }; sString += ' ' ; sString . push_back ( '2' ); std :: cout << sString << '\\n' ; \u8f93\u51fa\uff1a 1 one 2 \u5982\u679c\u4f60\u597d\u5947\u4e3a\u4ec0\u4e48\u8fd9\u4e2a\u51fd\u6570\u7684\u540d\u5b57\u662f push_back() \u800c\u4e0d\u662f append() \u3002\u8fd9\u91cc\u662f\u9075\u5faa\u4e86 \u6808 \u64cd\u4f5c\u7684\u547d\u540d\u4e60\u60ef\uff0c push_back() \u7528\u4e8e\u5c06\u4e00\u4e2a\u5355\u4e2a\u5185\u5bb9\u6dfb\u52a0\u5230\u6808\u9876\u3002\u5982\u679c\u6211\u4eec\u5c06\u5b57\u7b26\u4e32\u770b\u505a\u662f\u4e00\u4e2a\u5b57\u7b26\u7ec4\u6210\u7684\u6808\uff0c\u5219\u4f7f\u7528 push_back() \u5c06\u4e00\u4e2a\u5b57\u7b26\u6dfb\u52a0\u5230\u6808\u9876\u662f\u5f88\u7b26\u5408\u76f4\u89c9\u7684\u3002\u4f46\u662f\uff0c\u6ca1\u6709\u63d0\u4f9b append() \u51fd\u6570\u5728\u6211\u770b\u6765\u5728\u4e00\u81f4\u6027\u65b9\u9762\u662f\u4e00\u79cd\u7f3a\u5931\uff01 append() \u51fd\u6570\u6709\u4e00\u4e2a\u7248\u672c\u53ef\u4ee5\u6dfb\u52a0\u591a\u4e2a\u5b57\u7b26\u4e32\uff1a string& string::append (size_type num, char c) \u5c06\u5b57\u7b26c\u91cd\u590d num \u6b21\u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\u4e2d\uff1b \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sString { \"aaa\" }; sString . append ( 4 , 'b' ); std :: cout << sString << '\\n' ; \u8f93\u51fa\uff1a 1 aaabbbb \u6700\u540e\u4e00\u79cd\u7248\u672c\u7684 append() \u53ef\u4ee5\u914d\u5408 \u8fed\u4ee3\u5668 \u5de5\u4f5c\uff1a string& string::append (InputIterator start, InputIterator end) \u5c06 [start, end) \u8303\u56f4\u5185\u7684\u5b57\u7b26\u8ffd\u52a0\u5230\u5b57\u7b26\u4e32 (\u5de6\u95ed\u53f3\u5f00\u533a\u95f4)\uff1b \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b","title":"22.6 - std-string \u8ffd\u52a0"},{"location":"22-6-std-string-appending/#_1","text":"\u5728\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u8ffd\u52a0\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528 operator+= \u6216 append() \u6216 push_back() \u3002 string& string::operator+= (const string& str) string& string::append (const string& str) \u8fd9\u4e9b\u51fd\u6570\u90fd\u53ef\u4ee5\u5c06 str \u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\u3002 Sample code: 1 2 3 4 5 6 7 8 std :: string sString { \"one\" }; sString += std :: string { \" two\" }; std :: string sThree { \" three\" }; sString . append ( sThree ); std :: cout << sString << '\\n' ; Output: 1 one two three append() \u51fd\u6570\u4e5f\u6709\u51e0\u79cd\uff0c\u53ef\u4ee5\u7528\u4e8e\u6dfb\u52a0\u4e00\u4e2a\u5b50\u4e32\uff1a string& string::append (const string& str, size_type index, size_type num) \u8be5\u51fd\u6570\u5c06 str \u4e2d\u4ece\u7d22\u5f15 index \u5f00\u59cb\u7684 num \u4e2a\u5b57\u7b26\u4e32\u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\u672b\u5c3e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u629b\u51fa out_of_range \uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 std :: string sString { \"one \" }; const std :: string sTemp { \"twothreefour\" }; sString . append ( sTemp , 3 , 5 ); // append substring of sTemp starting at index 3 of length 5 std :: cout << sString << '\\n' ; \u8f93\u51fa\uff1a 1 one three Operator+= \u548c append() \u4e5f\u63d0\u4f9b\u4e86\u914d\u5408C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u5de5\u4f5c\u7684\u7248\u672c\uff1a string& string::operator+= (const char* str) string& string::append (const char* str) \u8fd9\u4e9b\u51fd\u6570\u90fd\u53ef\u4ee5\u5c06 str \u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b str \u4e0d\u80fd\u662f NULL\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 std :: string sString { \"one\" }; sString += \" two\" ; sString . append ( \" three\" ); std :: cout << sString << '\\n' ; \u8f93\u51fa\uff1a 1 one two three append() \u8fd8\u6709\u53e6\u5916\u4e00\u4e2a\u914d\u5408C\u8bed\u8a00\u5b57\u7b26\u4e32\u5de5\u4f5c\u7684\u7248\u672c\uff1a string& string::append (const char* str, size_type len) \u5c06 str \u7684\u524d len \u4e2a\u5b57\u7b26\u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f1a\u5ffd\u7565\u7279\u6b8a\u5b57\u7b26(\u5305\u62ec \u201d )\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sString { \"one \" }; sString . append ( \"threefour\" , 5 ); std :: cout << sString << '\\n' ; \u8f93\u51fa\uff1a 1 one three \u8be5\u51fd\u6570\u4e0d\u5b89\u5168\uff0c\u4e0d\u63a8\u8350\u4f7f\u7528\u3002 \u8fd8\u6709\u53e6\u5916\u4e00\u4e9b\u53ef\u4ee5\u8ffd\u52a0 \u5b57\u7b26 \u7684\u51fd\u6570\uff0c\u6ce8\u610f\uff0c\u51fd\u6570\u540d\u662f push_back() \u800c\u4e0d\u662f append() \uff01 string& string::operator+= (char c) void string::push_back (char c) \u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u53ef\u4ee5\u5c06\u5b57\u7b26c\u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\uff1b operator += \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f8b\u5b50\uff1a 1 2 3 4 5 std :: string sString { \"one\" }; sString += ' ' ; sString . push_back ( '2' ); std :: cout << sString << '\\n' ; \u8f93\u51fa\uff1a 1 one 2 \u5982\u679c\u4f60\u597d\u5947\u4e3a\u4ec0\u4e48\u8fd9\u4e2a\u51fd\u6570\u7684\u540d\u5b57\u662f push_back() \u800c\u4e0d\u662f append() \u3002\u8fd9\u91cc\u662f\u9075\u5faa\u4e86 \u6808 \u64cd\u4f5c\u7684\u547d\u540d\u4e60\u60ef\uff0c push_back() \u7528\u4e8e\u5c06\u4e00\u4e2a\u5355\u4e2a\u5185\u5bb9\u6dfb\u52a0\u5230\u6808\u9876\u3002\u5982\u679c\u6211\u4eec\u5c06\u5b57\u7b26\u4e32\u770b\u505a\u662f\u4e00\u4e2a\u5b57\u7b26\u7ec4\u6210\u7684\u6808\uff0c\u5219\u4f7f\u7528 push_back() \u5c06\u4e00\u4e2a\u5b57\u7b26\u6dfb\u52a0\u5230\u6808\u9876\u662f\u5f88\u7b26\u5408\u76f4\u89c9\u7684\u3002\u4f46\u662f\uff0c\u6ca1\u6709\u63d0\u4f9b append() \u51fd\u6570\u5728\u6211\u770b\u6765\u5728\u4e00\u81f4\u6027\u65b9\u9762\u662f\u4e00\u79cd\u7f3a\u5931\uff01 append() \u51fd\u6570\u6709\u4e00\u4e2a\u7248\u672c\u53ef\u4ee5\u6dfb\u52a0\u591a\u4e2a\u5b57\u7b26\u4e32\uff1a string& string::append (size_type num, char c) \u5c06\u5b57\u7b26c\u91cd\u590d num \u6b21\u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\u4e2d\uff1b \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sString { \"aaa\" }; sString . append ( 4 , 'b' ); std :: cout << sString << '\\n' ; \u8f93\u51fa\uff1a 1 aaabbbb \u6700\u540e\u4e00\u79cd\u7248\u672c\u7684 append() \u53ef\u4ee5\u914d\u5408 \u8fed\u4ee3\u5668 \u5de5\u4f5c\uff1a string& string::append (InputIterator start, InputIterator end) \u5c06 [start, end) \u8303\u56f4\u5185\u7684\u5b57\u7b26\u8ffd\u52a0\u5230\u5b57\u7b26\u4e32 (\u5de6\u95ed\u53f3\u5f00\u533a\u95f4)\uff1b \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b","title":"\u8ffd\u52a0\u5b57\u7b26\u4e32"},{"location":"22-7-std-string-inserting/","tags":["string"],"text":"Key Takeaway \u63d2\u5165 \u5411\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u5b57\u7b26\u53ef\u4ee5\u901a\u8fc7 insert() \u51fd\u6570\u5b8c\u6210\u3002 string& string::insert (size_type index, const string& str) string& string::insert (size_type index, const char* str) \u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u5c06 str \u4e2d\u7684\u5b57\u7b26\uff0c\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u7d22\u5f15 index \u4f4d\u7f6e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u4f1a\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7d22\u5f15\u65e0\u6548\uff0c\u5219\u4f1a\u629b\u51fa out_of_range \u5f02\u5e38\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u51fa\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u5bf9\u4e8e\u63a5\u53d7C\u98ce\u683c\u5b57\u7b26\u4e32\u7248\u672c\u7684\u51fd\u6570\uff0c str \u4e0d\u80fd\u662fNULL\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 string sString ( \"aaaa\" ); cout << sString << endl ; sString . insert ( 2 , string ( \"bbbb\" )); cout << sString << endl ; sString . insert ( 4 , \"cccc\" ); cout << sString << endl ; \u8f93\u51fa\uff1a 1 2 3 aaaa aabbbbaa aabbccccbbaa \u8fd8\u6709\u4e00\u4e2a\u7279\u522b\u5413\u4eba\u7684 insert() \u7248\u672c\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b50\u4e32\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u7684 index \u4f4d\u7f6e\uff1a string& string::insert (size_type index, const string& str, size_type startindex, size_type num) \u8be5\u51fd\u6570\u4f1a\u5c06 str \u4e2d\u4ece startindex \u5f00\u59cb\u7684 num \u4e2a\u5b57\u7b26\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u7684 index \u4f4d\u7f6e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u4f1a\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c index \u6216\u8005 startindex \uff0c\u8d8a\u754c\uff0c\u5219\u629b\u51fa out_of_range \u5f02\u5e38\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u51fa\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f8b\u5b50\uff1a 1 2 3 4 5 string sString ( \"aaaa\" ); const string sInsert ( \"01234567\" ); sString . insert ( 2 , sInsert , 3 , 4 ); // insert substring of sInsert from index [3,7) into sString at index 2 cout << sString << endl ; \u8f93\u51fa\uff1a 1 aa3456aa \u8fd8\u6709\u4e00\u4e2a\u7248\u672c\u7684 insert() \u53ef\u4ee5\u5c06C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u7684\u524d\u9762\u4e00\u90e8\u5206\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\uff1a string& string::insert(size_type index, const char* str, size_type len) \u5c06 str \u4e2d\u7684\u524d len \u4e2a\u5b57\u7b26\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d index \u4f4d\u7f6e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u4f1a\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7d22\u5f15\u65e0\u6548\uff0c\u5219\u629b\u51fa out_of_range \u5f02\u5e38\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u51fa\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f1a\u5ffd\u7565\u7279\u6b8a\u5b57\u7b26 (\u4f8b\u5982 \u201d ) \u4f8b\u5b50\uff1a 1 2 3 4 string sString ( \"aaaa\" ); sString . insert ( 2 , \"bcdef\" , 3 ); cout << sString << endl ; \u8f93\u51fa\uff1a 1 aabcdaa \u8fd8\u6709\u4e00\u4e2a\u7248\u672c\u7684 insert() \u53ef\u4ee5\u63d2\u5165\u67d0\u4e2a\u5b57\u7b26\u82e5\u5e72\u6b21\uff1a string& string::insert(size_type index, size_type num, char c) \u5c06\u5b57\u7b26 c \u5728\u5b57\u7b26\u4e32 index \u5904\u63d2\u5165 num \u6b21\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u4f1a\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7d22\u5f15\u65e0\u6548\uff0c\u5219\u629b\u51fa out_of_range \u5f02\u5e38\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u51fa\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f8b\u5b50\uff1a 1 2 3 4 string sString ( \"aaaa\" ); sString . insert ( 2 , 4 , 'c' ); cout << sString << endl ; \u8f93\u51fa\uff1a 1 aaccccaa \u6700\u540e\uff0c\u8fd8\u6709\u4e09\u4e2a\u7248\u672c\u7684 insert() \u7528\u4e8e\u914d\u5408 \u8fed\u4ee3\u5668 \u4f7f\u7528\uff1a void insert(iterator it, size_type num, char c) iterator string::insert(iterator it, char c) void string::insert(iterator it, InputIterator begin, InputIterator end) \u7b2c\u4e00\u4e2a\u51fd\u6570\u5c06\u5b57\u7b26 c \u5728\u8fed\u4ee3\u5668 it \u524d\u63d2\u5165 num \u6b21\uff1b \u7b2c\u4e8c\u4e2a\u51fd\u6570\u5c06\u5b57\u7b26c\u63d2\u5165\u5230\u8fed\u4ee3\u5668 it \u524d\u5e76\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u8be5\u5b57\u7b26\u7684\u8fed\u4ee3\u5668\uff1b \u7b2c\u4e09\u4e2a\u51fd\u6570\u5c06 [begin,end) \u8303\u56f4\u5185\u7684\u5b57\u7b26\u63d2\u5165\u5230\u8fed\u4ee3\u5668 it \u524d\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u51fa\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b","title":"22.7 - std-string \u63d2\u5165"},{"location":"22-7-std-string-inserting/#_1","text":"\u5411\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u5b57\u7b26\u53ef\u4ee5\u901a\u8fc7 insert() \u51fd\u6570\u5b8c\u6210\u3002 string& string::insert (size_type index, const string& str) string& string::insert (size_type index, const char* str) \u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u5c06 str \u4e2d\u7684\u5b57\u7b26\uff0c\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u7d22\u5f15 index \u4f4d\u7f6e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u4f1a\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7d22\u5f15\u65e0\u6548\uff0c\u5219\u4f1a\u629b\u51fa out_of_range \u5f02\u5e38\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u51fa\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u5bf9\u4e8e\u63a5\u53d7C\u98ce\u683c\u5b57\u7b26\u4e32\u7248\u672c\u7684\u51fd\u6570\uff0c str \u4e0d\u80fd\u662fNULL\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 string sString ( \"aaaa\" ); cout << sString << endl ; sString . insert ( 2 , string ( \"bbbb\" )); cout << sString << endl ; sString . insert ( 4 , \"cccc\" ); cout << sString << endl ; \u8f93\u51fa\uff1a 1 2 3 aaaa aabbbbaa aabbccccbbaa \u8fd8\u6709\u4e00\u4e2a\u7279\u522b\u5413\u4eba\u7684 insert() \u7248\u672c\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b50\u4e32\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u7684 index \u4f4d\u7f6e\uff1a string& string::insert (size_type index, const string& str, size_type startindex, size_type num) \u8be5\u51fd\u6570\u4f1a\u5c06 str \u4e2d\u4ece startindex \u5f00\u59cb\u7684 num \u4e2a\u5b57\u7b26\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u7684 index \u4f4d\u7f6e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u4f1a\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c index \u6216\u8005 startindex \uff0c\u8d8a\u754c\uff0c\u5219\u629b\u51fa out_of_range \u5f02\u5e38\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u51fa\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f8b\u5b50\uff1a 1 2 3 4 5 string sString ( \"aaaa\" ); const string sInsert ( \"01234567\" ); sString . insert ( 2 , sInsert , 3 , 4 ); // insert substring of sInsert from index [3,7) into sString at index 2 cout << sString << endl ; \u8f93\u51fa\uff1a 1 aa3456aa \u8fd8\u6709\u4e00\u4e2a\u7248\u672c\u7684 insert() \u53ef\u4ee5\u5c06C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u7684\u524d\u9762\u4e00\u90e8\u5206\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\uff1a string& string::insert(size_type index, const char* str, size_type len) \u5c06 str \u4e2d\u7684\u524d len \u4e2a\u5b57\u7b26\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d index \u4f4d\u7f6e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u4f1a\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7d22\u5f15\u65e0\u6548\uff0c\u5219\u629b\u51fa out_of_range \u5f02\u5e38\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u51fa\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f1a\u5ffd\u7565\u7279\u6b8a\u5b57\u7b26 (\u4f8b\u5982 \u201d ) \u4f8b\u5b50\uff1a 1 2 3 4 string sString ( \"aaaa\" ); sString . insert ( 2 , \"bcdef\" , 3 ); cout << sString << endl ; \u8f93\u51fa\uff1a 1 aabcdaa \u8fd8\u6709\u4e00\u4e2a\u7248\u672c\u7684 insert() \u53ef\u4ee5\u63d2\u5165\u67d0\u4e2a\u5b57\u7b26\u82e5\u5e72\u6b21\uff1a string& string::insert(size_type index, size_type num, char c) \u5c06\u5b57\u7b26 c \u5728\u5b57\u7b26\u4e32 index \u5904\u63d2\u5165 num \u6b21\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u4f1a\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7d22\u5f15\u65e0\u6548\uff0c\u5219\u629b\u51fa out_of_range \u5f02\u5e38\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u51fa\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f8b\u5b50\uff1a 1 2 3 4 string sString ( \"aaaa\" ); sString . insert ( 2 , 4 , 'c' ); cout << sString << endl ; \u8f93\u51fa\uff1a 1 aaccccaa \u6700\u540e\uff0c\u8fd8\u6709\u4e09\u4e2a\u7248\u672c\u7684 insert() \u7528\u4e8e\u914d\u5408 \u8fed\u4ee3\u5668 \u4f7f\u7528\uff1a void insert(iterator it, size_type num, char c) iterator string::insert(iterator it, char c) void string::insert(iterator it, InputIterator begin, InputIterator end) \u7b2c\u4e00\u4e2a\u51fd\u6570\u5c06\u5b57\u7b26 c \u5728\u8fed\u4ee3\u5668 it \u524d\u63d2\u5165 num \u6b21\uff1b \u7b2c\u4e8c\u4e2a\u51fd\u6570\u5c06\u5b57\u7b26c\u63d2\u5165\u5230\u8fed\u4ee3\u5668 it \u524d\u5e76\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u8be5\u5b57\u7b26\u7684\u8fed\u4ee3\u5668\uff1b \u7b2c\u4e09\u4e2a\u51fd\u6570\u5c06 [begin,end) \u8303\u56f4\u5185\u7684\u5b57\u7b26\u63d2\u5165\u5230\u8fed\u4ee3\u5668 it \u524d\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u51fa\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b","title":"\u63d2\u5165"},{"location":"23-1-Input-and-output-IO-streams/","tags":["io"],"text":"Key Takeaway \u8f93\u5165\u8f93\u51fa\u5e76\u6ca1\u6709\u88ab\u5b9a\u4e49\u4e3aC++\u8bed\u8a00\u7684\u6838\u5fc3\u529f\u80fd\uff0c\u5b83\u662f\u901a\u8fc7C++\u7684\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\uff08\u56e0\u6b64\u4f4d\u4e8estd\u547d\u540d\u7a7a\u95f4\u4e2d\uff09\u3002\u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5728\u4ee3\u7801\u4e2d\u5305\u542b\u4e86 iostream \u5e93\u7684\u5934\u6587\u4ef6\u5e76\u4f7f\u7528\u4e86 cin \u548c cout \u5bf9\u8c61\u8fdb\u884c\u7b80\u5355\u7684\u8f93\u5165\u8f93\u51fa\u3002\u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u8fdb\u4e00\u6b65\u7814\u7a76\u5176\u7ec6\u8282\u3002 iostream \u5e93 \u5f53\u6211\u4eec\u5305\u542b iostream \u5934\u6587\u4ef6\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u8bbf\u95ee\u63d0\u4f9b\u8f93\u5165\u8f93\u51fa\u529f\u80fd\u7684\u5168\u90e8\u7c7b\u4e86\uff08\u5305\u62ec\u4e00\u4e2a\u79f0\u4e3a iostream \u7684\u7c7b\uff09\u3002\u6240\u6709\u975e\u6587\u4ef6IO\u7684\u7c7b\u7684\u7ee7\u627f\u56fe\u53ef\u4ee5\u5728 \u8fd9\u91cc \u627e\u5230\u3002 \u5173\u4e8e\u8fd9\u4e2a\u5c42\u6b21\u7ed3\u6784\uff0c\u60a8\u53ef\u80fd\u6ce8\u610f\u5230\u7684\u7b2c\u4e00\u4ef6\u4e8b\u662f\u5b83\u4f7f\u7528\u4e86\u591a\u91cd\u7ee7\u627f(\u8fd9\u662f\u6211\u4eec\u591a\u6b21\u63d0\u9192\u4f60\u8981\u907f\u514d\u7684\u4e8b\u60c5)\u3002\u4f46\u662f\uff0c iostream \u5e93\u7ecf\u8fc7\u4e86\u5e7f\u6cdb\u7684\u8bbe\u8ba1\u548c\u6d4b\u8bd5\uff0c\u4ee5\u907f\u514d\u4efb\u4f55\u5178\u578b\u7684\u591a\u91cd\u7ee7\u627f\u95ee\u9898\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u653e\u5fc3\u5927\u80c6\u5730\u4f7f\u7528\u5b83\u3002 \u6d41\uff08Streams\uff09 \u4f60\u53ef\u80fd\u6ce8\u610f\u5230\u7684\u7b2c\u4e8c\u4ef6\u4e8b\u662f\u201c\u6d41(stream)\u201d\u8fd9\u4e2a\u8bcd\u88ab\u4f7f\u7528\u5f97\u975e\u5e38\u9891\u7e41\u3002\u5728\u6700\u57fa\u672c\u7684\u60c5\u51b5\u4e0b\uff0cC++\u4e2d\u7684I/O\u662f\u901a\u8fc7\u6d41\u5b9e\u73b0\u7684\u3002\u62bd\u8c61\u5730\u8bf4\uff0c\u4e00\u4e2a \u6d41 \u53ea\u662f\u4e00\u4e2a\u53ef\u4ee5\u6309\u987a\u5e8f\u8bbf\u95ee\u7684\u5b57\u8282\u5e8f\u5217\u3002\u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\uff0c\u6d41\u53ef\u80fd\u4ea7\u751f\u6216\u6d88\u8017\u65e0\u9650\u6570\u91cf\u7684\u6570\u636e\u3002 \u901a\u5e38\u60c5\u51b5\u4e0b\u6211\u4eec\u9700\u8981\u5904\u7406\u4e24\u79cd\u7c7b\u578b\u7684\u6d41\u3002 \u8f93\u5165\u6d41 \u7528\u6765\u4fdd\u5b58\u4ece\u6570\u636e\u751f\u4ea7\u8005\u5f97\u5230\u7684\u8f93\u5165\uff0c\u4f8b\u5982\u952e\u76d8\u3001\u6587\u4ef6\u6216\u7f51\u7edc\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u6309\u4e0b\u952e\u76d8\u4e0a\u7684\u6309\u952e\u6765\u4ea7\u751f\u8f93\u51fa\uff08\u5373\u4fbf\u7a0b\u5e8f\u6ca1\u6709\u8981\u6c42\u4efb\u4f55\u8f93\u5165\uff09\uff0c\u8be5\u8f93\u5165\u6570\u636e\u5e76\u6ca1\u6709\u88ab\u76f4\u63a5\u5ffd\u7565\uff0c\u800c\u662f\u88ab\u9001\u5165\u4e86\u8f93\u5165\u6d41\u4e2d\uff0c\u5b83\u53ea\u662f\u5728\u90a3\u91cc\u7b49\u5f85\u7a0b\u5e8f\u53bb\u8bfb\u53d6\u5b83\u3002 \u53e6\u4e00\u65b9\u9762\uff0c \u8f93\u51fa\u6d41 \u7528\u4e8e\u4fdd\u5b58\u6570\u636e\u6d88\u8d39\u8005\u9700\u8981\u7684\u6570\u636e\uff0c\u4f8b\u5982\u663e\u793a\u5668\u3001\u6587\u4ef6\u6216\u6253\u5370\u673a\u3002\u5f53\u5411\u8f93\u51fa\u8bbe\u5907\u5199\u6570\u636e\u65f6\uff0c\u8be5\u8bbe\u5907\u53ef\u80fd\u5e76\u6ca1\u6709\u51c6\u5907\u597d\u63a5\u6536\u8be5\u6570\u636e\u2014\u2014\u4f8b\u5982\uff1a\u5f53\u6570\u636e\u88ab\u5199\u5230\u8f93\u51fa\u6d41\u65f6\uff0c\u6253\u5370\u673a\u7684\u53ef\u80fd\u8fd8\u5728\u51c6\u5907\u3002\u6570\u636e\u5c31\u4f1a\u5728\u8f93\u51fa\u6d41\u4e2d\u7b49\u5f85\u6253\u5370\u673a\u53bb\u6d88\u8d39\u5b83\u3002 \u6709\u4e9b\u8bbe\u5907\uff0c\u5982\u6587\u4ef6\u548c\u7f51\u7edc\uff0c\u53ef\u4ee5\u540c\u65f6\u4f5c\u4e3a\u8f93\u5165\u548c\u8f93\u51fa\u6e90\u3002 \u6d41\u7684\u597d\u5904\u662f\uff0c\u7a0b\u5e8f\u5458\u53ea\u9700\u5b66\u4e60\u5982\u4f55\u4e0e\u6d41\u4ea4\u4e92\uff0c\u4ee5\u4fbf\u5411\u8bb8\u591a\u4e0d\u540c\u7c7b\u578b\u7684\u8bbe\u5907\u8bfb\u5199\u6570\u636e\u3002\u5173\u4e8e\u6d41\u5982\u4f55\u4e0e\u5b83\u4eec\u6240\u8fde\u63a5\u7684\u5b9e\u9645\u8bbe\u5907\u8fdb\u884c\u63a5\u53e3\u7684\u7ec6\u8282\u7531\u73af\u5883\u6216\u64cd\u4f5c\u7cfb\u7edf\u51b3\u5b9a\u3002 C++\u4e2d\u7684 Input/output \u5c3d\u7ba1 ios \u7c7b\u901a\u5e38\u6d3e\u751f\u81ea ios_base \uff0c\u4f46 ios \u901a\u5e38\u662f\u4f60\u4f1a\u76f4\u63a5\u4f7f\u7528\u7684\u6700\u4e0a\u5c42\u7684\u57fa\u7c7b\u3002 ios \u7c7b\u5b9a\u4e49\u4e86\u4e00\u5806\u5bf9\u8f93\u5165\u548c\u8f93\u51fa\u6d41\u90fd\u901a\u7528\u7684\u4e1c\u897f\u3002\u6211\u4eec\u5c06\u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u5185\u5bb9\u3002 istream \u7c7b\u662f\u5904\u7406\u8f93\u5165\u6d41\u65f6\u9700\u8981\u7528\u5230\u7684\u4e3b\u8981\u7c7b\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 \u63d0\u53d6\u8fd0\u7b97\u7b26>> \u4ece\u6d41\u4e2d\u63d0\u53d6\u503c\u3002\u5f53\u7528\u6237\u6309\u952e\u65f6\uff0c\u5bf9\u5e94\u7684\u6570\u636e\u4f1a\u88ab\u653e\u7f6e\u5728\u8f93\u5165\u6d41\u4e2d\uff0c\u6b64\u65f6\u4f60\u7684\u7a0b\u5e8f\u53ef\u4ee5\u4ece\u6d41\u4e2d\u63d0\u53d6\u8be5\u503c\u4ee5\u4fbf\u4f7f\u7528\u8be5\u6570\u636e\u3002 ostream \u7c7b\u5219\u662f\u5904\u7406\u8f93\u51fa\u6d41\u7684\u4e3b\u8981\u7c7b\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 \u63d2\u5165\u8fd0\u7b97\u7b26<< \u6d41\u4e2d\u63d2\u5165\u6570\u636e\u3002\u6211\u4eec\u9700\u8981\u5c06\u6570\u636e\u63d2\u5165\u6d41\u4e2d\uff0c\u8fd9\u6837\u6570\u636e\u6d88\u8d39\u8005\uff08\u4f8b\u5982\u663e\u793a\u5668\uff09\u624d\u80fd\u8bbf\u95ee\u8be5\u6570\u636e\u3002 iostream \u7c7b\u53ef\u4ee5\u5904\u7406\u8f93\u5165\u548c\u8f93\u51fa\uff0c\u5b9e\u73b0\u53cc\u5411 I/O. C++ \u4e2d\u7684\u6807\u51c6\u6d41 \u6807\u51c6\u6d41 \u662f\u7cfb\u7edf\u73af\u5883\u63d0\u4f9b\u7ed9\u7a0b\u5e8f\u7684\u9884\u5148\u8fde\u63a5\u597d\u7684\u6d41\u3002C++\u6709\u56db\u4e2a\u9884\u5b9a\u4e49\u7684\u6807\u51c6\u6d41\u5bf9\u8c61\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5b83\u4eec\uff0c\u5176\u4e2d\u524d\u4e09\u4e2a\u4f60\u4e4b\u524d\u5df2\u7ecf\u89c1\u8fc7\u4e86\uff1a cin -- \u7ed1\u5b9a\u5230\u6807\u51c6\u8f93\u5165\u6d41(\u4f8b\u5982\u952e\u76d8)\u7684 istream \u5bf9\u8c61\uff1b cout -- \u7ed1\u5b9a\u5230\u6807\u51c6\u8f93\u51fa\u6d41(\u901a\u5e38\u662f\u663e\u793a\u5668)\u7684 ostream \u5bf9\u8c61 \uff1b cerr -- \u7ed1\u5b9a\u5230\u6807\u51c6\u9519\u8bef\u6d41(\u901a\u5e38\u662f\u663e\u793a\u5668)\u7684 ostream \u5bf9\u8c61\uff0c\u63d0\u4f9b\u65e0\u7f13\u51b2\u8f93\u51fa\uff1b clog -- \u7ed1\u5b9a\u5230\u6807\u51c6\u9519\u8bef\u6d41(\u901a\u5e38\u662f\u663e\u793a\u5668)\u7684 ostream \u5bf9\u8c61\uff0c\u63d0\u4f9b\u7f13\u51b2\u8f93\u51fa\u3002 \u65e0\u7f13\u51b2\u8f93\u51fa\u901a\u5e38\u7acb\u5373\u5904\u7406\uff0c\u800c\u7f13\u51b2\u8f93\u51fa\u901a\u5e38\u4f5c\u4e3a\u5757\u5b58\u50a8\u548c\u5199\u5165\u3002\u56e0\u4e3aclog\u4e0d\u7ecf\u5e38\u4f7f\u7528\uff0c\u6240\u4ee5\u5b83\u7ecf\u5e38\u4ece\u6807\u51c6\u6d41\u5217\u8868\u4e2d\u88ab\u7701\u7565\u3002 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u66f4\u8be6\u7ec6\u5730\u4e86\u89e3\u66f4\u591a\u4e0eI/O\u76f8\u5173\u7684\u529f\u80fd\u3002","title":"23.1 - \u8f93\u5165\u8f93\u51fa\u6d41"},{"location":"23-1-Input-and-output-IO-streams/#iostream","text":"\u5f53\u6211\u4eec\u5305\u542b iostream \u5934\u6587\u4ef6\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u8bbf\u95ee\u63d0\u4f9b\u8f93\u5165\u8f93\u51fa\u529f\u80fd\u7684\u5168\u90e8\u7c7b\u4e86\uff08\u5305\u62ec\u4e00\u4e2a\u79f0\u4e3a iostream \u7684\u7c7b\uff09\u3002\u6240\u6709\u975e\u6587\u4ef6IO\u7684\u7c7b\u7684\u7ee7\u627f\u56fe\u53ef\u4ee5\u5728 \u8fd9\u91cc \u627e\u5230\u3002 \u5173\u4e8e\u8fd9\u4e2a\u5c42\u6b21\u7ed3\u6784\uff0c\u60a8\u53ef\u80fd\u6ce8\u610f\u5230\u7684\u7b2c\u4e00\u4ef6\u4e8b\u662f\u5b83\u4f7f\u7528\u4e86\u591a\u91cd\u7ee7\u627f(\u8fd9\u662f\u6211\u4eec\u591a\u6b21\u63d0\u9192\u4f60\u8981\u907f\u514d\u7684\u4e8b\u60c5)\u3002\u4f46\u662f\uff0c iostream \u5e93\u7ecf\u8fc7\u4e86\u5e7f\u6cdb\u7684\u8bbe\u8ba1\u548c\u6d4b\u8bd5\uff0c\u4ee5\u907f\u514d\u4efb\u4f55\u5178\u578b\u7684\u591a\u91cd\u7ee7\u627f\u95ee\u9898\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u653e\u5fc3\u5927\u80c6\u5730\u4f7f\u7528\u5b83\u3002","title":"iostream \u5e93"},{"location":"23-1-Input-and-output-IO-streams/#streams","text":"\u4f60\u53ef\u80fd\u6ce8\u610f\u5230\u7684\u7b2c\u4e8c\u4ef6\u4e8b\u662f\u201c\u6d41(stream)\u201d\u8fd9\u4e2a\u8bcd\u88ab\u4f7f\u7528\u5f97\u975e\u5e38\u9891\u7e41\u3002\u5728\u6700\u57fa\u672c\u7684\u60c5\u51b5\u4e0b\uff0cC++\u4e2d\u7684I/O\u662f\u901a\u8fc7\u6d41\u5b9e\u73b0\u7684\u3002\u62bd\u8c61\u5730\u8bf4\uff0c\u4e00\u4e2a \u6d41 \u53ea\u662f\u4e00\u4e2a\u53ef\u4ee5\u6309\u987a\u5e8f\u8bbf\u95ee\u7684\u5b57\u8282\u5e8f\u5217\u3002\u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\uff0c\u6d41\u53ef\u80fd\u4ea7\u751f\u6216\u6d88\u8017\u65e0\u9650\u6570\u91cf\u7684\u6570\u636e\u3002 \u901a\u5e38\u60c5\u51b5\u4e0b\u6211\u4eec\u9700\u8981\u5904\u7406\u4e24\u79cd\u7c7b\u578b\u7684\u6d41\u3002 \u8f93\u5165\u6d41 \u7528\u6765\u4fdd\u5b58\u4ece\u6570\u636e\u751f\u4ea7\u8005\u5f97\u5230\u7684\u8f93\u5165\uff0c\u4f8b\u5982\u952e\u76d8\u3001\u6587\u4ef6\u6216\u7f51\u7edc\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u6309\u4e0b\u952e\u76d8\u4e0a\u7684\u6309\u952e\u6765\u4ea7\u751f\u8f93\u51fa\uff08\u5373\u4fbf\u7a0b\u5e8f\u6ca1\u6709\u8981\u6c42\u4efb\u4f55\u8f93\u5165\uff09\uff0c\u8be5\u8f93\u5165\u6570\u636e\u5e76\u6ca1\u6709\u88ab\u76f4\u63a5\u5ffd\u7565\uff0c\u800c\u662f\u88ab\u9001\u5165\u4e86\u8f93\u5165\u6d41\u4e2d\uff0c\u5b83\u53ea\u662f\u5728\u90a3\u91cc\u7b49\u5f85\u7a0b\u5e8f\u53bb\u8bfb\u53d6\u5b83\u3002 \u53e6\u4e00\u65b9\u9762\uff0c \u8f93\u51fa\u6d41 \u7528\u4e8e\u4fdd\u5b58\u6570\u636e\u6d88\u8d39\u8005\u9700\u8981\u7684\u6570\u636e\uff0c\u4f8b\u5982\u663e\u793a\u5668\u3001\u6587\u4ef6\u6216\u6253\u5370\u673a\u3002\u5f53\u5411\u8f93\u51fa\u8bbe\u5907\u5199\u6570\u636e\u65f6\uff0c\u8be5\u8bbe\u5907\u53ef\u80fd\u5e76\u6ca1\u6709\u51c6\u5907\u597d\u63a5\u6536\u8be5\u6570\u636e\u2014\u2014\u4f8b\u5982\uff1a\u5f53\u6570\u636e\u88ab\u5199\u5230\u8f93\u51fa\u6d41\u65f6\uff0c\u6253\u5370\u673a\u7684\u53ef\u80fd\u8fd8\u5728\u51c6\u5907\u3002\u6570\u636e\u5c31\u4f1a\u5728\u8f93\u51fa\u6d41\u4e2d\u7b49\u5f85\u6253\u5370\u673a\u53bb\u6d88\u8d39\u5b83\u3002 \u6709\u4e9b\u8bbe\u5907\uff0c\u5982\u6587\u4ef6\u548c\u7f51\u7edc\uff0c\u53ef\u4ee5\u540c\u65f6\u4f5c\u4e3a\u8f93\u5165\u548c\u8f93\u51fa\u6e90\u3002 \u6d41\u7684\u597d\u5904\u662f\uff0c\u7a0b\u5e8f\u5458\u53ea\u9700\u5b66\u4e60\u5982\u4f55\u4e0e\u6d41\u4ea4\u4e92\uff0c\u4ee5\u4fbf\u5411\u8bb8\u591a\u4e0d\u540c\u7c7b\u578b\u7684\u8bbe\u5907\u8bfb\u5199\u6570\u636e\u3002\u5173\u4e8e\u6d41\u5982\u4f55\u4e0e\u5b83\u4eec\u6240\u8fde\u63a5\u7684\u5b9e\u9645\u8bbe\u5907\u8fdb\u884c\u63a5\u53e3\u7684\u7ec6\u8282\u7531\u73af\u5883\u6216\u64cd\u4f5c\u7cfb\u7edf\u51b3\u5b9a\u3002","title":"\u6d41\uff08Streams\uff09"},{"location":"23-1-Input-and-output-IO-streams/#c-inputoutput","text":"\u5c3d\u7ba1 ios \u7c7b\u901a\u5e38\u6d3e\u751f\u81ea ios_base \uff0c\u4f46 ios \u901a\u5e38\u662f\u4f60\u4f1a\u76f4\u63a5\u4f7f\u7528\u7684\u6700\u4e0a\u5c42\u7684\u57fa\u7c7b\u3002 ios \u7c7b\u5b9a\u4e49\u4e86\u4e00\u5806\u5bf9\u8f93\u5165\u548c\u8f93\u51fa\u6d41\u90fd\u901a\u7528\u7684\u4e1c\u897f\u3002\u6211\u4eec\u5c06\u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u5185\u5bb9\u3002 istream \u7c7b\u662f\u5904\u7406\u8f93\u5165\u6d41\u65f6\u9700\u8981\u7528\u5230\u7684\u4e3b\u8981\u7c7b\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 \u63d0\u53d6\u8fd0\u7b97\u7b26>> \u4ece\u6d41\u4e2d\u63d0\u53d6\u503c\u3002\u5f53\u7528\u6237\u6309\u952e\u65f6\uff0c\u5bf9\u5e94\u7684\u6570\u636e\u4f1a\u88ab\u653e\u7f6e\u5728\u8f93\u5165\u6d41\u4e2d\uff0c\u6b64\u65f6\u4f60\u7684\u7a0b\u5e8f\u53ef\u4ee5\u4ece\u6d41\u4e2d\u63d0\u53d6\u8be5\u503c\u4ee5\u4fbf\u4f7f\u7528\u8be5\u6570\u636e\u3002 ostream \u7c7b\u5219\u662f\u5904\u7406\u8f93\u51fa\u6d41\u7684\u4e3b\u8981\u7c7b\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 \u63d2\u5165\u8fd0\u7b97\u7b26<< \u6d41\u4e2d\u63d2\u5165\u6570\u636e\u3002\u6211\u4eec\u9700\u8981\u5c06\u6570\u636e\u63d2\u5165\u6d41\u4e2d\uff0c\u8fd9\u6837\u6570\u636e\u6d88\u8d39\u8005\uff08\u4f8b\u5982\u663e\u793a\u5668\uff09\u624d\u80fd\u8bbf\u95ee\u8be5\u6570\u636e\u3002 iostream \u7c7b\u53ef\u4ee5\u5904\u7406\u8f93\u5165\u548c\u8f93\u51fa\uff0c\u5b9e\u73b0\u53cc\u5411 I/O.","title":"C++\u4e2d\u7684 Input/output"},{"location":"23-1-Input-and-output-IO-streams/#c","text":"\u6807\u51c6\u6d41 \u662f\u7cfb\u7edf\u73af\u5883\u63d0\u4f9b\u7ed9\u7a0b\u5e8f\u7684\u9884\u5148\u8fde\u63a5\u597d\u7684\u6d41\u3002C++\u6709\u56db\u4e2a\u9884\u5b9a\u4e49\u7684\u6807\u51c6\u6d41\u5bf9\u8c61\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5b83\u4eec\uff0c\u5176\u4e2d\u524d\u4e09\u4e2a\u4f60\u4e4b\u524d\u5df2\u7ecf\u89c1\u8fc7\u4e86\uff1a cin -- \u7ed1\u5b9a\u5230\u6807\u51c6\u8f93\u5165\u6d41(\u4f8b\u5982\u952e\u76d8)\u7684 istream \u5bf9\u8c61\uff1b cout -- \u7ed1\u5b9a\u5230\u6807\u51c6\u8f93\u51fa\u6d41(\u901a\u5e38\u662f\u663e\u793a\u5668)\u7684 ostream \u5bf9\u8c61 \uff1b cerr -- \u7ed1\u5b9a\u5230\u6807\u51c6\u9519\u8bef\u6d41(\u901a\u5e38\u662f\u663e\u793a\u5668)\u7684 ostream \u5bf9\u8c61\uff0c\u63d0\u4f9b\u65e0\u7f13\u51b2\u8f93\u51fa\uff1b clog -- \u7ed1\u5b9a\u5230\u6807\u51c6\u9519\u8bef\u6d41(\u901a\u5e38\u662f\u663e\u793a\u5668)\u7684 ostream \u5bf9\u8c61\uff0c\u63d0\u4f9b\u7f13\u51b2\u8f93\u51fa\u3002 \u65e0\u7f13\u51b2\u8f93\u51fa\u901a\u5e38\u7acb\u5373\u5904\u7406\uff0c\u800c\u7f13\u51b2\u8f93\u51fa\u901a\u5e38\u4f5c\u4e3a\u5757\u5b58\u50a8\u548c\u5199\u5165\u3002\u56e0\u4e3aclog\u4e0d\u7ecf\u5e38\u4f7f\u7528\uff0c\u6240\u4ee5\u5b83\u7ecf\u5e38\u4ece\u6807\u51c6\u6d41\u5217\u8868\u4e2d\u88ab\u7701\u7565\u3002 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u66f4\u8be6\u7ec6\u5730\u4e86\u89e3\u66f4\u591a\u4e0eI/O\u76f8\u5173\u7684\u529f\u80fd\u3002","title":"C++ \u4e2d\u7684\u6807\u51c6\u6d41"},{"location":"23-2-Input-with-istream/","tags":["io"],"text":"Key Takeaway \u63d0\u53d6\u8fd0\u7b97\u7b26 \u4f1a\u5ffd\u7565\u7a7a\u767d\u7b26 std::get() \u53ef\u4ee5\u83b7\u53d6\u5b57\u7b26\u6216\u83b7\u53d6\u5b57\u7b26\u4e32\uff08\u6307\u5b9a\u6700\u5927\u5b57\u7b26\u4e2a\u6570\uff09\uff0c\u4f46\u662f\u5b83\u4e0d\u8bfb\u53d6\u6362\u884c\u7b26 getline() \uff0c\u5b83\u7684\u5de5\u4f5c\u65b9\u5f0f\u548c get() \u5b8c\u5168\u4e00\u81f4\u4f46\u662f\u4f1a\u8bfb\u53d6\u6362\u884c\u7b26\uff01 getline() \u7684\u4e00\u4e2a\u7279\u6b8a\u7248\u672c\u5b58\u5728\u4e8e istream \u7c7b\u4e4b\u5916\uff0c\u7528\u4e8e\u8bfb\u5165 std::string \u7c7b\u578b\u7684\u53d8\u91cf\u3002\u8fd9\u4e2a\u7279\u6b8a\u7248\u672c\u4e0d\u662f ostream \u6216 istream \u7684\u6210\u5458\uff0c\u5305\u542b\u5728 string \u5934\u6587\u4ef6\u4e2d\u3002 iostream \u5e93\u662f\u5f88\u590d\u6742\u7684\u2014\u2014\u6240\u4ee5\u672c\u6559\u7a0b\u4e0d\u53ef\u80fd\u6db5\u76d6\u5b83\u7684\u65b9\u65b9\u9762\u9762\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u4f1a\u5411\u4f60\u4ecb\u7ecd\u5176\u4e2d\u6700\u5e38\u7528\u7684\u90a3\u4e9b\u529f\u80fd\u3002\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba\u8f93\u5165\u5e93 istream \u7684\u5404\u9879\u529f\u80fd\u3002 \u63d0\u53d6\u8fd0\u7b97\u7b26 >> \u5728\u4e4b\u524d\u7684\u5f88\u591a\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u5728\u4f7f\u7528 \u63d0\u53d6\u8fd0\u7b97\u7b26 \u4ece\u8f93\u5165\u6d41\u4e2d\u83b7\u53d6\u6570\u636e\u4e86\u3002C++\u4e3a\u6240\u6709\u7684\u5185\u5efa\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u4e86\u9884\u5b9a\u4e49\u7684\u63d0\u53d6\u8fd0\u7b97\u7b26\uff0c\u540c\u65f6\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 \u4e3a\u6211\u4eec\u81ea\u5df1\u7684\u7c7b\u63d0\u4f9b\u63d0\u53d6\u8fd0\u7b97\u7b26\u3002 \u8bfb\u53d6\u5b57\u7b26\u4e32\u65f6\uff0c\u63d0\u53d6\u64cd\u4f5c\u7b26\u7684\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\u662f\u5982\u4f55\u9632\u6b62\u8f93\u5165\u6ea2\u51fa\u7f13\u51b2\u533a\u3002\u4e3e\u4e2a\u4f8b\u5b50: 1 2 char buf [ 10 ]; std :: cin >> buf ; \u5982\u679c\u7528\u6237\u8f93\u516518\u4e2a\u5b57\u7b26\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u6b64\u65f6\u7f13\u51b2\u533a\u4f1a\u6ea2\u51fa\u5e76\u53d1\u751f\u4e0d\u597d\u7684\u4e8b\u60c5\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u6211\u4eec\u4e0d\u5e94\u8be5\u5047\u8bbe\u7528\u6237\u4f1a\u8f93\u5165\u591a\u5c11\u5b57\u7b26\u3002 \u5904\u7406\u8fd9\u4e2a\u95ee\u9898\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528manipulators\u3002manipulator\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u7528\u4e8e\u5728\u5e94\u7528\u63d0\u53d6(>>)\u6216\u63d2\u5165(setw(\u4f4d\u4e8e iomanip \u5934\u6587\u4ef6)\u65f6\u4fee\u6539\u6d41\uff0c\u53ef\u4ee5\u7528\u4e8e\u9650\u5236\u4ece\u6d41\u4e2d\u8bfb\u5165\u7684\u5b57\u7b26\u6570\u91cf\u3002\u8981\u4f7f\u7528 setw() \uff0c\u53ea\u9700\u63d0\u4f9b\u8981\u8bfb\u53d6\u7684\u6700\u5927\u5b57\u7b26\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u50cf\u8fd9\u6837\u5c06\u5176\u63d2\u5165\u5230\u8f93\u5165\u8bed\u53e5\u4e2d: 1 2 3 #include char buf [ 10 ]; std :: cin >> std :: setw ( 10 ) >> buf ; \u8fd9\u4e2a\u7a0b\u5e8f\u53ea\u4f1a\u4ece\u6d41\u4e2d\u8bfb\u53d69\u4e2a\u5b57\u7b26\uff08\u4e3a\u7ed3\u675f\u7b26\u9884\u7559\u4e00\u4e2a\u5b57\u7b26\uff09\u3002\u4efb\u4f55\u5176\u4ed6\u7684\u5b57\u7b26\u90fd\u4f1a\u88ab\u7559\u5728\u6d41\u4e2d\u7b49\u5f85\u4e0b\u4e00\u6b21\u63d0\u53d6\u3002 \u63d0\u53d6\u64cd\u4f5c\u548c\u7a7a\u767d\u5b57\u7b26 \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u63d0\u53d6\u64cd\u4f5c\u7b26\u4f1a\u8df3\u8fc7\u7a7a\u767d\u5b57\u7b26\uff08\u7a7a\u683c\u3001tab\u548c\u7a7a\u884c\uff09\u3002 \u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 int main () { char ch ; while ( std :: cin >> ch ) std :: cout << ch ; return 0 ; } \u7528\u6237\u8f93\u5165\uff1a 1 Hello my name is Alex \u7531\u4e8e \u63d0\u53d6\u8fd0\u7b97\u7b26 \u4f1a\u8df3\u8fc7\u7a7a\u683c\u548c\u6362\u884c\uff0c\u6240\u4ee5\u8bfb\u53d6\u7684\u7ed3\u679c\u662f\uff1a 1 HellomynameisAlex \u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u4f60\u5e0c\u671b\u83b7\u5f97\u7528\u6237\u8f93\u5165\u5e76\u4fdd\u7559\u7a7a\u683c\u3002\u4e3a\u6b64\uff0cistream\u7c7b\u63d0\u4f9b\u4e86\u8bb8\u591a\u53ef\u7528\u4e8e\u6b64\u76ee\u7684\u7684\u51fd\u6570\u3002 \u5176\u4e2d\u6700\u6709\u7528\u7684\u662f get() \u51fd\u6570\uff0c\u5b83\u53ea\u662f\u4ece\u8f93\u5165\u6d41\u4e2d\u83b7\u53d6\u4e00\u4e2a\u5b57\u7b26\u3002\u4e0b\u9762\u662f\u4e0a\u9762\u4f7f\u7528 get() \u7684\u76f8\u540c\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 int main () { char ch ; while ( std :: cin . get ( ch )) std :: cout << ch ; return 0 ; } \u7528\u6237\u8f93\u5165\uff1a 1 Hello my name is Alex \u8f93\u51fa\u4e3a\uff1a 1 Hello my name is Alex std::get() \u8fd8\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32\u7248\u672c\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\u4f5c\u4e3a\u8bfb\u53d6\u5b57\u7b26\u4e2a\u6570\u7684\u6700\u5927\u503c\uff1a 1 2 3 4 5 6 7 8 int main () { char strBuf [ 11 ]; std :: cin . get ( strBuf , 11 ); std :: cout << strBuf << '\\n' ; return 0 ; } \u5982\u679c\u8f93\u5165\u4e3a\uff1a 1 Hello my name is Alex \u5219\u8f93\u51fa\u4e3a\uff1a 1 Hello my n \u6ce8\u610f\uff0c\u6211\u4eec\u53ea\u8bfb\u53d6\u4e86\u524d10\u4e2a\u5b57\u7b26(\u5fc5\u987b\u4e3a\u7ec8\u6b62\u7b26\u7559\u4e0b\u4e00\u4e2a\u5b57\u7b26)\u3002\u5176\u4f59\u7684\u5b57\u7b26\u7559\u5728\u8f93\u5165\u6d41\u4e2d\u3002 \u5173\u4e8e get() \u9700\u8981\u6ce8\u610f\u7684\u4e00\u4ef6\u91cd\u8981\u4e8b\u60c5\u662f\uff0c\u5b83\u4e0d\u8bfb\u53d6\u6362\u884c\u7b26\uff01\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c: 1 2 3 4 5 6 7 8 9 10 11 12 int main () { char strBuf [ 11 ]; // Read up to 10 characters std :: cin . get ( strBuf , 11 ); std :: cout << strBuf << '\\n' ; // Read up to 10 more characters std :: cin . get ( strBuf , 11 ); std :: cout << strBuf << '\\n' ; return 0 ; } \u5982\u679c\u8f93\u5165\u4e3a\uff1a 1 Hello! \u5219\u7a0b\u5e8f\u8f93\u51fa\u4e3a\uff1a 1 Hello! \u7136\u540e\u7a0b\u5e8f\u5c31\u7ed3\u675f\u4e86\uff01 \u4e3a\u4ec0\u4e48\u5b83\u4e0d\u8981\u6211\u518d\u8f93\u516510\u4e2a\u5b57\u7b26\uff1f\u8fd9\u662f\u56e0\u4e3a\u7b2c\u4e00\u4e2a get() \u8bfb\u53d6\u5230\u6362\u884c\u7136\u540e\u5c31\u7ed3\u675f\u4e86\u3002\u800c\u7b2c\u4e8c\u4e2a get() \u5219\u4f1a\u7ee7\u7eed\u8bfb\u53d6 cin \u6d41\uff0c\u6b64\u65f6\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u662f\u6362\u884c\uff0c\u6240\u4ee5\u5b83\u4f1a\u7acb\u5373\u505c\u6b62\u3002 \u56e0\u6b64\uff0c C++\u63d0\u4f9b\u4e86 getline() \uff0c\u5b83\u7684\u5de5\u4f5c\u65b9\u5f0f\u548c get() \u5b8c\u5168\u4e00\u81f4\u4f46\u662f\u4f1a\u8bfb\u53d6\u6362\u884c\u7b26\uff01 1 2 3 4 5 6 7 8 9 10 11 12 int main () { char strBuf [ 11 ]; // Read up to 10 characters std :: cin . getline ( strBuf , 11 ); std :: cout << strBuf << '\\n' ; // Read up to 10 more characters std :: cin . getline ( strBuf , 11 ); std :: cout << strBuf << '\\n' ; return 0 ; } \u5373\u4f7f\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u4e2d\u6709\u6362\u884c\u7b26\uff0c\u8fd9\u6bb5\u4ee3\u7801\u4e5f\u4f1a\u6309\u7167\u4f60\u7684\u671f\u671b\u6267\u884c\u3002 \u5982\u679c\u4f60\u9700\u8981\u77e5\u9053\u6700\u540e\u4e00\u6b21\u8c03\u7528 getline() \u63d0\u53d6\u4e86\u591a\u5c11\u5b57\u7b26\uff0c\u4f7f\u7528 gcount() : 1 2 3 4 5 6 7 8 9 int main () { char strBuf [ 100 ]; std :: cin . getline ( strBuf , 100 ); std :: cout << strBuf << '\\n' ; std :: cout << std :: cin . gcount () << \" characters were read\" << '\\n' ; return 0 ; } \u7528\u4e8e std::string \u7684 getline() getline() \u7684\u4e00\u4e2a\u7279\u6b8a\u7248\u672c\u5b58\u5728\u4e8e istream \u7c7b\u4e4b\u5916\uff0c\u7528\u4e8e\u8bfb\u5165 std::string \u7c7b\u578b\u7684\u53d8\u91cf\u3002\u8fd9\u4e2a\u7279\u6b8a\u7248\u672c\u4e0d\u662f ostream \u6216 istream \u7684\u6210\u5458\uff0c\u5305\u542b\u5728 string \u5934\u6587\u4ef6\u4e2d\u3002\u4e0b\u9762\u662f\u5b83\u7684\u7528\u6cd5\u793a\u4f8b: 1 2 3 4 5 6 7 8 9 10 11 #include #include int main () { std :: string strBuf ; std :: getline ( std :: cin , strBuf ); std :: cout << strBuf << '\\n' ; return 0 ; } \u4e00\u4e9b\u66f4\u6709\u7528\u7684 istream \u51fd\u6570 \u8fd8\u6709\u4e00\u4e9b\u66f4\u6709\u7528\u7684\u8f93\u5165\u51fd\u6570\u4f60\u53ef\u80fd\u4f1a\u7528\u5230\uff1a ignore() \uff1a\u5ffd\u7565\u6d41\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\uff1b ignore(int nCount) \uff1a\u5ffd\u7565\u6d41\u4e2d\u7684\u524d nCount \u4e2a\u5b57\u7b26\uff1b peek() \uff1a\u5141\u8bb8\u4f60\u4ece\u6d41\u4e2d\u8bfb\u53d6\u5b57\u7b26\u800c\u4e0d\u5c06\u5176\u4ece\u6d41\u4e2d\u5220\u9664\u3002 unget() \uff1a\u8fd4\u56de\u8bfb\u56de\u6d41\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\uff0c\u4ee5\u4fbf\u4e0b\u4e00\u6b21\u8c03\u7528\u53ef\u4ee5\u518d\u6b21\u8bfb\u53d6\u5b83\u3002 putback(char ch) \uff1a\u5141\u8bb8\u4f60\u5c06\u6240\u9009\u7684\u5b57\u7b26\u653e\u56de\u6d41\u4e2d\uff0c\u4ee5\u4fbf\u4e0b\u4e00\u4e2a\u8c03\u7528\u8bfb\u53d6\u3002\u8fd4\u56de\u8bfb\u56de\u6d41\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\uff0c\u4ee5\u4fbf\u4e0b\u4e00\u6b21\u8c03\u7528\u53ef\u4ee5\u518d\u6b21\u8bfb\u53d6\u5b83\u3002 istream \u5305\u542b\u8bb8\u591a\u5176\u4ed6\u51fd\u6570\u548c\u4e0a\u9762\u63d0\u5230\u7684\u51fd\u6570\u7684\u53d8\u4f53\uff0c\u5b83\u4eec\u53ef\u80fd\u6709\u7528\uff0c\u8fd9\u53d6\u51b3\u4e8e\u4f60\u9700\u8981\u505a\u4ec0\u4e48\u3002\u7136\u800c\uff0c\u8fd9\u4e9b\u8bae\u9898\u66f4\u9002\u5408\u653e\u5728\u4e13\u6ce8\u4e8e\u6807\u51c6\u5e93\u7684\u6559\u7a0b\u6216\u4e66\u4e2d(\u4f8b\u5982Nicolai M. Josuttis\u7684 \u201cc++\u6807\u51c6\u5e93\u201d )\u3002","title":"23.2 - \u4f7f\u7528istream\u5904\u7406\u8f93\u5165"},{"location":"23-2-Input-with-istream/#_1","text":"\u5728\u4e4b\u524d\u7684\u5f88\u591a\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u5728\u4f7f\u7528 \u63d0\u53d6\u8fd0\u7b97\u7b26 \u4ece\u8f93\u5165\u6d41\u4e2d\u83b7\u53d6\u6570\u636e\u4e86\u3002C++\u4e3a\u6240\u6709\u7684\u5185\u5efa\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u4e86\u9884\u5b9a\u4e49\u7684\u63d0\u53d6\u8fd0\u7b97\u7b26\uff0c\u540c\u65f6\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 \u4e3a\u6211\u4eec\u81ea\u5df1\u7684\u7c7b\u63d0\u4f9b\u63d0\u53d6\u8fd0\u7b97\u7b26\u3002 \u8bfb\u53d6\u5b57\u7b26\u4e32\u65f6\uff0c\u63d0\u53d6\u64cd\u4f5c\u7b26\u7684\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\u662f\u5982\u4f55\u9632\u6b62\u8f93\u5165\u6ea2\u51fa\u7f13\u51b2\u533a\u3002\u4e3e\u4e2a\u4f8b\u5b50: 1 2 char buf [ 10 ]; std :: cin >> buf ; \u5982\u679c\u7528\u6237\u8f93\u516518\u4e2a\u5b57\u7b26\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u6b64\u65f6\u7f13\u51b2\u533a\u4f1a\u6ea2\u51fa\u5e76\u53d1\u751f\u4e0d\u597d\u7684\u4e8b\u60c5\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u6211\u4eec\u4e0d\u5e94\u8be5\u5047\u8bbe\u7528\u6237\u4f1a\u8f93\u5165\u591a\u5c11\u5b57\u7b26\u3002 \u5904\u7406\u8fd9\u4e2a\u95ee\u9898\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528manipulators\u3002manipulator\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u7528\u4e8e\u5728\u5e94\u7528\u63d0\u53d6(>>)\u6216\u63d2\u5165(setw(\u4f4d\u4e8e iomanip \u5934\u6587\u4ef6)\u65f6\u4fee\u6539\u6d41\uff0c\u53ef\u4ee5\u7528\u4e8e\u9650\u5236\u4ece\u6d41\u4e2d\u8bfb\u5165\u7684\u5b57\u7b26\u6570\u91cf\u3002\u8981\u4f7f\u7528 setw() \uff0c\u53ea\u9700\u63d0\u4f9b\u8981\u8bfb\u53d6\u7684\u6700\u5927\u5b57\u7b26\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u50cf\u8fd9\u6837\u5c06\u5176\u63d2\u5165\u5230\u8f93\u5165\u8bed\u53e5\u4e2d: 1 2 3 #include char buf [ 10 ]; std :: cin >> std :: setw ( 10 ) >> buf ; \u8fd9\u4e2a\u7a0b\u5e8f\u53ea\u4f1a\u4ece\u6d41\u4e2d\u8bfb\u53d69\u4e2a\u5b57\u7b26\uff08\u4e3a\u7ed3\u675f\u7b26\u9884\u7559\u4e00\u4e2a\u5b57\u7b26\uff09\u3002\u4efb\u4f55\u5176\u4ed6\u7684\u5b57\u7b26\u90fd\u4f1a\u88ab\u7559\u5728\u6d41\u4e2d\u7b49\u5f85\u4e0b\u4e00\u6b21\u63d0\u53d6\u3002","title":"\u63d0\u53d6\u8fd0\u7b97\u7b26 >>"},{"location":"23-2-Input-with-istream/#_2","text":"\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u63d0\u53d6\u64cd\u4f5c\u7b26\u4f1a\u8df3\u8fc7\u7a7a\u767d\u5b57\u7b26\uff08\u7a7a\u683c\u3001tab\u548c\u7a7a\u884c\uff09\u3002 \u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 int main () { char ch ; while ( std :: cin >> ch ) std :: cout << ch ; return 0 ; } \u7528\u6237\u8f93\u5165\uff1a 1 Hello my name is Alex \u7531\u4e8e \u63d0\u53d6\u8fd0\u7b97\u7b26 \u4f1a\u8df3\u8fc7\u7a7a\u683c\u548c\u6362\u884c\uff0c\u6240\u4ee5\u8bfb\u53d6\u7684\u7ed3\u679c\u662f\uff1a 1 HellomynameisAlex \u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u4f60\u5e0c\u671b\u83b7\u5f97\u7528\u6237\u8f93\u5165\u5e76\u4fdd\u7559\u7a7a\u683c\u3002\u4e3a\u6b64\uff0cistream\u7c7b\u63d0\u4f9b\u4e86\u8bb8\u591a\u53ef\u7528\u4e8e\u6b64\u76ee\u7684\u7684\u51fd\u6570\u3002 \u5176\u4e2d\u6700\u6709\u7528\u7684\u662f get() \u51fd\u6570\uff0c\u5b83\u53ea\u662f\u4ece\u8f93\u5165\u6d41\u4e2d\u83b7\u53d6\u4e00\u4e2a\u5b57\u7b26\u3002\u4e0b\u9762\u662f\u4e0a\u9762\u4f7f\u7528 get() \u7684\u76f8\u540c\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 int main () { char ch ; while ( std :: cin . get ( ch )) std :: cout << ch ; return 0 ; } \u7528\u6237\u8f93\u5165\uff1a 1 Hello my name is Alex \u8f93\u51fa\u4e3a\uff1a 1 Hello my name is Alex std::get() \u8fd8\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32\u7248\u672c\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\u4f5c\u4e3a\u8bfb\u53d6\u5b57\u7b26\u4e2a\u6570\u7684\u6700\u5927\u503c\uff1a 1 2 3 4 5 6 7 8 int main () { char strBuf [ 11 ]; std :: cin . get ( strBuf , 11 ); std :: cout << strBuf << '\\n' ; return 0 ; } \u5982\u679c\u8f93\u5165\u4e3a\uff1a 1 Hello my name is Alex \u5219\u8f93\u51fa\u4e3a\uff1a 1 Hello my n \u6ce8\u610f\uff0c\u6211\u4eec\u53ea\u8bfb\u53d6\u4e86\u524d10\u4e2a\u5b57\u7b26(\u5fc5\u987b\u4e3a\u7ec8\u6b62\u7b26\u7559\u4e0b\u4e00\u4e2a\u5b57\u7b26)\u3002\u5176\u4f59\u7684\u5b57\u7b26\u7559\u5728\u8f93\u5165\u6d41\u4e2d\u3002 \u5173\u4e8e get() \u9700\u8981\u6ce8\u610f\u7684\u4e00\u4ef6\u91cd\u8981\u4e8b\u60c5\u662f\uff0c\u5b83\u4e0d\u8bfb\u53d6\u6362\u884c\u7b26\uff01\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c: 1 2 3 4 5 6 7 8 9 10 11 12 int main () { char strBuf [ 11 ]; // Read up to 10 characters std :: cin . get ( strBuf , 11 ); std :: cout << strBuf << '\\n' ; // Read up to 10 more characters std :: cin . get ( strBuf , 11 ); std :: cout << strBuf << '\\n' ; return 0 ; } \u5982\u679c\u8f93\u5165\u4e3a\uff1a 1 Hello! \u5219\u7a0b\u5e8f\u8f93\u51fa\u4e3a\uff1a 1 Hello! \u7136\u540e\u7a0b\u5e8f\u5c31\u7ed3\u675f\u4e86\uff01 \u4e3a\u4ec0\u4e48\u5b83\u4e0d\u8981\u6211\u518d\u8f93\u516510\u4e2a\u5b57\u7b26\uff1f\u8fd9\u662f\u56e0\u4e3a\u7b2c\u4e00\u4e2a get() \u8bfb\u53d6\u5230\u6362\u884c\u7136\u540e\u5c31\u7ed3\u675f\u4e86\u3002\u800c\u7b2c\u4e8c\u4e2a get() \u5219\u4f1a\u7ee7\u7eed\u8bfb\u53d6 cin \u6d41\uff0c\u6b64\u65f6\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u662f\u6362\u884c\uff0c\u6240\u4ee5\u5b83\u4f1a\u7acb\u5373\u505c\u6b62\u3002 \u56e0\u6b64\uff0c C++\u63d0\u4f9b\u4e86 getline() \uff0c\u5b83\u7684\u5de5\u4f5c\u65b9\u5f0f\u548c get() \u5b8c\u5168\u4e00\u81f4\u4f46\u662f\u4f1a\u8bfb\u53d6\u6362\u884c\u7b26\uff01 1 2 3 4 5 6 7 8 9 10 11 12 int main () { char strBuf [ 11 ]; // Read up to 10 characters std :: cin . getline ( strBuf , 11 ); std :: cout << strBuf << '\\n' ; // Read up to 10 more characters std :: cin . getline ( strBuf , 11 ); std :: cout << strBuf << '\\n' ; return 0 ; } \u5373\u4f7f\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u4e2d\u6709\u6362\u884c\u7b26\uff0c\u8fd9\u6bb5\u4ee3\u7801\u4e5f\u4f1a\u6309\u7167\u4f60\u7684\u671f\u671b\u6267\u884c\u3002 \u5982\u679c\u4f60\u9700\u8981\u77e5\u9053\u6700\u540e\u4e00\u6b21\u8c03\u7528 getline() \u63d0\u53d6\u4e86\u591a\u5c11\u5b57\u7b26\uff0c\u4f7f\u7528 gcount() : 1 2 3 4 5 6 7 8 9 int main () { char strBuf [ 100 ]; std :: cin . getline ( strBuf , 100 ); std :: cout << strBuf << '\\n' ; std :: cout << std :: cin . gcount () << \" characters were read\" << '\\n' ; return 0 ; }","title":"\u63d0\u53d6\u64cd\u4f5c\u548c\u7a7a\u767d\u5b57\u7b26"},{"location":"23-2-Input-with-istream/#stdstring-getline","text":"getline() \u7684\u4e00\u4e2a\u7279\u6b8a\u7248\u672c\u5b58\u5728\u4e8e istream \u7c7b\u4e4b\u5916\uff0c\u7528\u4e8e\u8bfb\u5165 std::string \u7c7b\u578b\u7684\u53d8\u91cf\u3002\u8fd9\u4e2a\u7279\u6b8a\u7248\u672c\u4e0d\u662f ostream \u6216 istream \u7684\u6210\u5458\uff0c\u5305\u542b\u5728 string \u5934\u6587\u4ef6\u4e2d\u3002\u4e0b\u9762\u662f\u5b83\u7684\u7528\u6cd5\u793a\u4f8b: 1 2 3 4 5 6 7 8 9 10 11 #include #include int main () { std :: string strBuf ; std :: getline ( std :: cin , strBuf ); std :: cout << strBuf << '\\n' ; return 0 ; }","title":"\u7528\u4e8estd::string \u7684 getline()"},{"location":"23-2-Input-with-istream/#istream","text":"\u8fd8\u6709\u4e00\u4e9b\u66f4\u6709\u7528\u7684\u8f93\u5165\u51fd\u6570\u4f60\u53ef\u80fd\u4f1a\u7528\u5230\uff1a ignore() \uff1a\u5ffd\u7565\u6d41\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\uff1b ignore(int nCount) \uff1a\u5ffd\u7565\u6d41\u4e2d\u7684\u524d nCount \u4e2a\u5b57\u7b26\uff1b peek() \uff1a\u5141\u8bb8\u4f60\u4ece\u6d41\u4e2d\u8bfb\u53d6\u5b57\u7b26\u800c\u4e0d\u5c06\u5176\u4ece\u6d41\u4e2d\u5220\u9664\u3002 unget() \uff1a\u8fd4\u56de\u8bfb\u56de\u6d41\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\uff0c\u4ee5\u4fbf\u4e0b\u4e00\u6b21\u8c03\u7528\u53ef\u4ee5\u518d\u6b21\u8bfb\u53d6\u5b83\u3002 putback(char ch) \uff1a\u5141\u8bb8\u4f60\u5c06\u6240\u9009\u7684\u5b57\u7b26\u653e\u56de\u6d41\u4e2d\uff0c\u4ee5\u4fbf\u4e0b\u4e00\u4e2a\u8c03\u7528\u8bfb\u53d6\u3002\u8fd4\u56de\u8bfb\u56de\u6d41\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\uff0c\u4ee5\u4fbf\u4e0b\u4e00\u6b21\u8c03\u7528\u53ef\u4ee5\u518d\u6b21\u8bfb\u53d6\u5b83\u3002 istream \u5305\u542b\u8bb8\u591a\u5176\u4ed6\u51fd\u6570\u548c\u4e0a\u9762\u63d0\u5230\u7684\u51fd\u6570\u7684\u53d8\u4f53\uff0c\u5b83\u4eec\u53ef\u80fd\u6709\u7528\uff0c\u8fd9\u53d6\u51b3\u4e8e\u4f60\u9700\u8981\u505a\u4ec0\u4e48\u3002\u7136\u800c\uff0c\u8fd9\u4e9b\u8bae\u9898\u66f4\u9002\u5408\u653e\u5728\u4e13\u6ce8\u4e8e\u6807\u51c6\u5e93\u7684\u6559\u7a0b\u6216\u4e66\u4e2d(\u4f8b\u5982Nicolai M. Josuttis\u7684 \u201cc++\u6807\u51c6\u5e93\u201d )\u3002","title":"\u4e00\u4e9b\u66f4\u6709\u7528\u7684istream \u51fd\u6570"},{"location":"23-3-output-with-ostream-and-ios/","tags":["io"],"text":"Key Takeaway \u66f4\u6539\u683c\u5f0f\u5316\u7684\u65b9\u5f0f\u6709\u4e24\u79cd\uff0c\u6807\u8bb0\u548cmanipulators\u3002\u6807\u8bb0\u4f60\u53ef\u4ee5\u5c06\u5176\u770b\u505a\u662f\u53ef\u4ee5\u5f00\u5173\u7684\u5e03\u5c14\u91cf\u3002\u800cmanipulators\u5219\u662f\u653e\u7f6e\u5728\u6d41\u4e2d\uff0c\u53ef\u4ee5\u4fee\u6539\u8f93\u5165\u8f93\u51fa\u7684\u5bf9\u8c61 \u3002 setf() \u548c unsetf() \u5206\u522b\u7528\u4e8e\u8bbe\u7f6e\u548c\u5173\u95ed\u6807\u8bb0 \u8bb8\u591a\u6807\u5fd7\u5c5e\u4e8e\u7ec4\uff0c\u79f0\u4e3a\u683c\u5f0f\u7ec4\u3002 \u683c\u5f0f\u7ec4 \u662f\u4e00\u7ec4\u6267\u884c\u7c7b\u4f3c(\u6709\u65f6\u4e92\u65a5)\u683c\u5f0f\u5316\u9009\u9879\u7684\u6807\u5fd7\uff0c\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528\u4e24\u4e2a\u53c2\u6570\u7248\u672c\u7684 setf() \uff0c\u5e76\u901a\u8fc7\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u660e\u7ec4\u3002\u6b64\u65f6\u7ec4\u5185\u5176\u4ed6\u6807\u8bb0\u4f1a\u88ab\u81ea\u52a8\u5173\u95ed\u3002 \u672c\u8282\u8bfe\u6211\u4eec\u5c06\u4ecb\u7ecd iostream \u7684\u8f93\u51fa\u7c7b ostream \u3002 \u63d2\u5165\u8fd0\u7b97\u7b26 \u63d2\u5165\u8fd0\u7b97\u7b26 \u7528\u4e8e\u5411\u8f93\u51fa\u6d41\u63d2\u5165\u6570\u636e\u3002C++\u4e3a\u6240\u6709\u7684\u5185\u5efa\u6570\u636e\u7ed3\u6784\u90fd\u9884\u5b9a\u4e49\u4e86\u63d2\u5165\u8fd0\u7b97\u7b26\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7 \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 \u4e3a\u81ea\u5b9a\u4e49\u7684\u7c7b\u63d0\u4f9b\u63d2\u5165\u8fd0\u7b97\u7b26\u3002 \u5728 23.1 - \u8f93\u5165\u8f93\u51fa\u6d41 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c istream \u548c ostream \u90fd\u662f\u4ece ios \u7c7b\u6d3e\u751f\u6765\u7684\u3002 ios (\u548c ios_base ) \u7c7b\u7684\u4e00\u4e2a\u529f\u80fd\u5c31\u662f\u63a7\u5236\u8f93\u51fa\u7684\u683c\u5f0f\u3002 \u683c\u5f0f\u5316 \u66f4\u6539\u683c\u5f0f\u5316\u7684\u65b9\u5f0f\u6709\u4e24\u79cd\uff0c\u6807\u8bb0\u548cmanipulators\u3002\u6807\u8bb0\u4f60\u53ef\u4ee5\u5c06\u5176\u770b\u505a\u662f\u53ef\u4ee5\u5f00\u5173\u7684\u5e03\u5c14\u91cf\u3002\u800cmanipulators\u5219\u662f\u653e\u7f6e\u5728\u6d41\u4e2d\uff0c\u53ef\u4ee5\u4fee\u6539\u8f93\u5165\u8f93\u51fa\u7684\u5bf9\u8c61 \u3002 ==\u8bbe\u7f6e\u4e00\u4e2a\u6807\u8bb0\u53ef\u4ee5\u4f7f\u7528 setf() \u51fd\u6570\u5e76\u63d0\u4f9b\u5408\u9002\u7684\u6807\u8bb0\u4f5c\u4e3a\u53c2\u6570\u3002==\u4f8b\u5982\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0bC++\u5e76\u4e0d\u4f1a\u5728\u6b63\u6570\u524d\u52a0\u4e00\u4e2a+\u53f7\u3002\u4f46\u662f\uff0c\u4f7f\u7528 std::ios::showpos \u6807\u8bb0\u5c31\u53ef\u4ee5\u4fee\u6539\u8be5\u884c\u4e3a\uff1a 1 2 std :: cout . setf ( std :: ios :: showpos ); // \u6253\u5f00std::ios::showpos \u6807\u8bb0 std :: cout << 27 << '\\n' ; \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 +27 \u6211\u4eec\u4e5f\u53ef\u4ee5\u5229\u7528 \u6309\u4f4d\u6216 \u64cd\u4f5c\u4e00\u6b21\u6027\u6253\u5f00\u591a\u4e2a ios \u6807\u8bb0 \uff1a 1 2 std :: cout . setf ( std :: ios :: showpos | std :: ios :: uppercase ); // \u6253\u5f00 std::ios::showpos \u548c std::ios::uppercase \u6807\u8bb0 std :: cout << 1234567.89f << '\\n' ; \u8f93\u51fa\u7ed3\u679c\uff1a 1 +1.23457E+06 \u5173\u95ed\u6807\u8bb0\u4e5f\u5f88\u7b80\u5355\uff0c\u4f7f\u7528 unsetf() \u51fd\u6570\u5373\u53ef\uff1a 1 2 3 4 std :: cout . setf ( std :: ios :: showpos ); // turn on the std::ios::showpos flag std :: cout << 27 << '\\n' ; std :: cout . unsetf ( std :: ios :: showpos ); // turn off the std::ios::showpos flag std :: cout << 28 << '\\n' ; \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 +27 28 \u5728\u4f7f\u7528 setf() \u65f6\u8fd8\u6709\u4e00\u4e2a\u9700\u8981\u63d0\u53ca\u7684\u5c0f\u6280\u5de7\u3002 \u8bb8\u591a\u6807\u5fd7\u5c5e\u4e8e\u7ec4\uff0c\u79f0\u4e3a\u683c\u5f0f\u7ec4\u3002 \u683c\u5f0f\u7ec4 \u662f\u4e00\u7ec4\u6267\u884c\u7c7b\u4f3c(\u6709\u65f6\u4e92\u65a5)\u683c\u5f0f\u5316\u9009\u9879\u7684\u6807\u5fd7 \u3002\u4f8b\u5982\uff0c\u540d\u4e3a\u201cbasefield\u201d\u7684\u683c\u5f0f\u7ec4\u5305\u542b\u6807\u5fd7\u201coct\u201d\u3001\u201cdec\u201d\u548c\u201chex\u201d\uff0c\u5b83\u4eec\u63a7\u5236\u6574\u6570\u503c\u7684\u57fa\u6570\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u8bbe\u7f6e\u4e86\u201cdec\u201d\u6807\u5fd7\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u8fd9\u6837\u505a: 1 2 std :: cout . setf ( std :: ios :: hex ); // \u5c1d\u8bd5\u542f\u52a8\u5341\u516d\u8fdb\u5236\u8f93\u51fa std :: cout << 27 << '\\n' ; \u5219\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 27 \u6ca1\u6709\u6548\u679c\uff01\u8fd9\u662f\u56e0\u4e3a setf() \u53ea\u80fd\u6253\u5f00\u6807\u8bb0\u2014\u2014\u4f46\u662f\u5b83\u6ca1\u6709\u667a\u80fd\u5230\u61c2\u5f97\u53bb\u5173\u95ed\u4e92\u65a5\u7684\u6807\u8bb0\u3002\u56e0\u6b64\uff0c\u5f53 std::ios::hex \u5f00\u542f\u65f6\uff0c std::ios::dec \u4ecd\u7136\u662f\u5f00\u542f\u72b6\u6001\uff0c\u7531\u4e8e\u5b83\u4f18\u5148\u7ea7\u66f4\u9ad8\uff0c\u6240\u4ee5\u4ecd\u7136\u662f\u6309\u7167\u5341\u8fdb\u5236\u8f93\u51fa\u7684\u3002\u6709\u4e24\u4e2a\u529e\u6cd5\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002 \u7b2c\u4e00\u79cd\u65b9\u6cd5\u662f\u5173\u95ed std::ios::dec \u5e76\u5f00\u542f std::hex \uff1a 1 2 3 std :: cout . unsetf ( std :: ios :: dec ); // turn off decimal output std :: cout . setf ( std :: ios :: hex ); // turn on hexadecimal output std :: cout << 27 << '\\n' ; \u8f93\u51fa\u7ed3\u679c\u7b26\u5408\u9884\u671f\uff1a 1 1b \u7b2c\u4e8c\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u53e6\u4e00\u4e2a\u7248\u672c\u7684 setf() \uff0c\u5b83\u6709\u4e24\u4e2a\u5f62\u53c2\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u660e\u6807\u8bb0\u6240\u5c5e\u7684 \u683c\u5f0f\u7ec4 \u3002\u5f53\u4f7f\u7528\u8fd9\u4e2a\u7248\u672c\u7684 setf() \u65f6\uff0c\u540c\u7ec4\u7684\u5176\u4ed6\u6807\u8bb0\u90fd\u4f1a\u88ab\u81ea\u52a8\u5173\u95ed\uff0c\u53ea\u6709\u6211\u4eec\u8bbe\u7f6e\u7684\u6807\u8bb0\u4f1a\u88ab\u6253\u5f00\u3002\u4f8b\u5982\uff1a 1 2 3 // Turn on std::ios::hex as the only std::ios::basefield flag std :: cout . setf ( std :: ios :: hex , std :: ios :: basefield ); std :: cout << 27 << '\\n' ; \u8f93\u51fa\u7ed3\u679c\u4ecd\u7136\u7b26\u5408\u9884\u671f\uff1a 1 1b \u4f7f\u7528 setf() \u548c unsetf() \u662f\u6709\u70b9\u522b\u626d\u7684\uff0c\u56e0\u6b64C++\u63d0\u4f9b\u4e86\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u6765\u6539\u53d8\u683c\u5f0f\u5316\u9009\u9879\uff1amanipulators\u3002manipulators \u6700\u8d5e\u7684\u5730\u65b9\u5728\u4e8e\u5b83\u8db3\u591f\u667a\u80fd\uff0c\u5b83\u80fd\u591f\u81ea\u52a8\u5f00\u5173\u76f8\u5173\u8054\u7684\u6807\u8bb0\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86 manipulators \u6539\u53d8\u8ba1\u6570\u8fdb\u5236\uff1a 1 2 3 std :: cout << std :: hex << 27 << '\\n' ; // print 27 in hex std :: cout << 28 << '\\n' ; // \u4ecd\u7136\u662f\u5341\u516d\u8fdb\u5236\uff0c\u4e0d\u662f\u4e00\u6b21\u6027\u7684 std :: cout << std :: dec << 29 << '\\n' ; // back to decimal \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 3 1b 1c 29 \u901a\u5e38\uff0c\u4f7f\u7528manipulators\u6bd4\u8bbe\u7f6e\u548c\u53d6\u6d88\u8bbe\u7f6e\u6807\u8bb0\u8981\u5bb9\u6613\u5f97\u591a\u3002\u8bb8\u591a\u6807\u8bb0\u90fd\u53ef\u4ee5\u901a\u8fc7\u6807\u5fd7 \u548c manipulators\u6765\u5b9e\u73b0(\u4f8b\u5982\u66f4\u6539\u57fa\u6570)\uff0c\u7136\u800c\uff0c\u53e6\u5916\u4e00\u4e9b\u6807\u8bb0\u5219\u53ea\u80fd\u901a\u8fc7\u6807\u8bb0\u6216manipulators\u5b9e\u73b0\uff0c\u56e0\u6b64\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u8fd9\u4e24\u79cd\u65b9\u6cd5\u975e\u5e38\u91cd\u8981\u3002 \u6709\u7528\u7684\u6807\u8bb0 \u8fd9\u91cc\u6211\u4eec\u5217\u4e3e\u4e00\u4e9b\u5f88\u6709\u7528\u7684\u6807\u8bb0\u3001manipulators \u4ee5\u53ca\u6210\u5458\u51fd\u6570\u3002 \u6807\u8bb0\u4f4d\u4e8e std::ios \u7c7b\u4e2d\uff0cmanipulators \u5219\u4f4d\u4e8estd \u547d\u540d\u7a7a\u95f4 \u603b\uff0c\u800c\u6210\u5458\u51fd\u6570\u5219\u4f4d\u4e8e std::ostream \u7c7b\u4e2d\u3002 \u7ec4\u6807 \u8bb0 \u542b\u4e49 std::ios::boolalpha \u6253\u5f00\u8be5\u6807\u8bb0\u540e\uff0c\u5e03\u5c14\u7c7b\u578b\u4f1a\u6253\u5370 \u201ctrue\u201d \u6216 \u201cfalse\u201d\u3002\u5982\u679c\u5173\u95ed\uff0c\u5219\u59270\u62161 Manipulator \u542b\u4e49 std::boolalpha \u5e03\u5c14\u7c7b\u578b\u4f1a\u6253\u5370 \u201ctrue\u201d or \u201cfalse\u201d std::noboolalpha \u5e03\u5c14\u7c7b\u578b\u4f1a\u6253\u5370 0 or 1 (default) \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 std :: cout << true << ' ' << false << '\\n' ; std :: cout . setf ( std :: ios :: boolalpha ); std :: cout << true << ' ' << false << '\\n' ; std :: cout << std :: noboolalpha << true << ' ' << false << '\\n' ; std :: cout << std :: boolalpha << true << ' ' << false << '\\n' ; \u7ed3\u679c\uff1a 1 2 3 4 1 0 true false 1 0 true false \u7ec4 \u6807\u8bb0 \u542b\u4e49 std::ios::showpos \u8bbe\u7f6e\u540e\uff0c\u6b63\u6570\u524d\u9762\u4f1a\u6dfb\u52a0+\u53f7 Manipulator \u542b\u4e49 std::showpos \u6b63\u6570\u524d\u9762\u4f1a\u6dfb\u52a0+\u53f7 std::noshowpos \u6b63\u6570\u524d\u9762\u4e0d\u6dfb\u52a0+\u53f7 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 std :: cout << 5 << '\\n' ; std :: cout . setf ( std :: ios :: showpos ); std :: cout << 5 << '\\n' ; std :: cout << std :: noshowpos << 5 << '\\n' ; std :: cout << std :: showpos << 5 << '\\n' ; \u7ed3\u679c\uff1a 1 2 3 4 5 +5 5 +5 \u7ec4 \u6807\u8bb0 \u542b\u4e49 std::ios::uppercase If set, uses upper case letters Manipulator \u542b\u4e49 std::uppercase \u4f7f\u7528\u5927\u5199\u5b57\u6bcd std::nouppercase \u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 std :: cout << 12345678.9 << '\\n' ; std :: cout . setf ( std :: ios :: uppercase ); std :: cout << 12345678.9 << '\\n' ; std :: cout << std :: nouppercase << 12345678.9 << '\\n' ; std :: cout << std :: uppercase << 12345678.9 << '\\n' ; \u7ed3\u679c\uff1a 1 2 3 4 1.23457e+007 1.23457E+007 1.23457e+007 1.23457E+007 \u7ec4 \u6807\u8bb0 \u542b\u4e49 std::ios::basefield std::ios::dec \u6309\u7167\u5341\u8fdb\u5236\u6253\u5370\uff08\u9ed8\u8ba4\u7684\uff09 std::ios::basefield std::ios::hex \u6309\u7167\u5341\u516d\u8fdb\u5236\u6253\u5370 std::ios::basefield std::ios::oct \u6309\u7167\u516b\u8fdb\u5236\u6253\u5370 std::ios::basefield (none) \u6839\u636e\u524d\u7f00\u5b57\u6bcd\u6253\u5370 Manipulator Meaning std::dec \u6309\u7167\u5341\u8fdb\u5236\u6253\u5370 std::hex \u6309\u7167\u5341\u516d\u8fdb\u5236\u6253\u5370 std::oct \u6309\u7167\u516b\u8fdb\u5236\u6253\u5370 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 std :: cout << 27 << '\\n' ; std :: cout . setf ( std :: ios :: dec , std :: ios :: basefield ); std :: cout << 27 << '\\n' ; std :: cout . setf ( std :: ios :: oct , std :: ios :: basefield ); std :: cout << 27 << '\\n' ; std :: cout . setf ( std :: ios :: hex , std :: ios :: basefield ); std :: cout << 27 << '\\n' ; std :: cout << std :: dec << 27 << '\\n' ; std :: cout << std :: oct << 27 << '\\n' ; std :: cout << std :: hex << 27 << '\\n' ; \u7ed3\u679c\uff1a 1 2 3 4 5 6 7 27 27 33 1b 27 33 1b \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u770b\u5230\u901a\u8fc7\u6807\u5fd7\u548c\u901a\u8fc7\u64cd\u4f5c\u7b26\u8bbe\u7f6e\u683c\u5f0f\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u5728\u4ee5\u540e\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u64cd\u7eb5\u7b26\uff0c\u9664\u975e\u5b83\u4eec\u4e0d\u53ef\u7528\u3002 \u7cbe\u5ea6\u3001\u8ba1\u6570\u6cd5\u548c\u5c0f\u6570\u70b9 \u4f7f\u7528 manipulators (\u6216\u8005\u6807\u8bb0) \u4e5f\u53ef\u4ee5\u6539\u53d8\u6570\u503c\u6253\u5370\u65f6\u7684\u7cbe\u5ea6\u548c\u8981\u5c55\u793a\u7684\u5c0f\u6570\u70b9\u4f4d\u6570\u3002\u8fd9\u4e9b\u683c\u5f0f\u5316\u9009\u9879\u7684\u7ec4\u5408\u6bd4\u8f83\u590d\u6742\uff0c\u8ba9\u6211\u4eec\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\uff1a |\u7ec4 |\u6807\u8bb0 |\u542b\u4e49| |:--|:--|:--|:--| | | std::ios::floatfield | std::ios::fixed |\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528\u5341\u8fdb\u5236\u8bb0\u6570\u6cd5 | | std::ios::floatfield | std::ios::scientific |\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528\u79d1\u5b66\u8bb0\u6570\u6cd5 | | std::ios::floatfield |(none) |\u4f4d\u6570\u8f83\u5c11\u65f6\u4f7f\u7528\u56fa\u5b9a\u8ba1\u6570\u6cd5\uff0c\u4f4d\u6570\u591a\u65f6\u4f7f\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5 | | std::ios::floatfield | std::ios::showpoint |\u5bf9\u4e8e\u6d6e\u70b9\u503c\uff0c\u59cb\u7ec8\u663e\u793a\u5c0f\u6570\u70b9\u548c\u672b\u5c3e0 Manipulator \u542b\u4e49 std::fixed \u4f7f\u7528\u5341\u8fdb\u5236\u8bb0\u6570\u6cd5 std::scientific \u4f7f\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5 std::showpoint \u5bf9\u4e8e\u6d6e\u70b9\u503c\uff0c\u59cb\u7ec8\u663e\u793a\u5c0f\u6570\u70b9\u548c\u672b\u5c3e0 std::noshowpoint \u5bf9\u4e8e\u6d6e\u70b9\u503c\uff0c\u4e0d\u663e\u793a\u5c0f\u6570\u70b9\u548c\u672b\u5c3e0 std::setprecision(int) \u4e3a\u6d6e\u70b9\u6570\u8bbe\u7f6e\u7cbe\u5ea6(\u5b9a\u4e49\u5728 iomanip \u5934\u6587\u4ef6\u4e2d) \u6210\u5458\u51fd\u6570 \u542b\u4e49 std::ios_base::precision() \u8fd4\u56de\u5f53\u524d\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6 std::ios_base::precision(int) \u8bbe\u7f6e\u6d6e\u70b9\u6570\u7cbe\u5ea6\u5e76\u8fd4\u56de\u4e4b\u524d\u7684\u7cbe\u5ea6 \u5982\u679c\u4f7f\u7528\u56fa\u5b9a\u8bb0\u6570\u6cd5\u6216\u79d1\u5b66\u8bb0\u6570\u6cd5\uff0c\u5219\u7cbe\u5ea6\u51b3\u5b9a\u5206\u6570\u4e2d\u663e\u793a\u7684\u5c0f\u6570\u70b9\u540e\u591a\u5c11\u4f4d\u3002\u6ce8\u610f\uff0c\u5982\u679c\u7cbe\u5ea6\u5c0f\u4e8e\u6709\u6548\u4f4d\u6570\uff0c\u5219\u6570\u5b57\u5c06\u56db\u820d\u4e94\u5165\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 std :: cout << std :: fixed << '\\n' ; std :: cout << std :: setprecision ( 3 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 4 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 5 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 6 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 7 ) << 123.456 << '\\n' ; std :: cout << std :: scientific << '\\n' ; std :: cout << std :: setprecision ( 3 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 4 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 5 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 6 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 7 ) << 123.456 << '\\n' ; \u6253\u5370\uff1a 1 2 3 4 5 6 7 8 9 10 11 123.456 123.4560 123.45600 123.456000 123.4560000 1.235e+002 1.2346e+002 1.23456e+002 1.234560e+002 1.2345600e+002 \u5982\u679c\u65e2\u4e0d\u4f7f\u7528\u56fa\u5b9a\u8ba1\u6570\u6cd5\u4e5f\u4e0d\u4f7f\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5\uff0c\u5219\u7cbe\u5ea6\u51b3\u5b9a\u4e86\u5e94\u8be5\u663e\u793a\u591a\u5c11\u6709\u6548\u6570\u5b57\u3002\u540c\u6837\uff0c\u5982\u679c\u7cbe\u5ea6\u5c0f\u4e8e\u6709\u6548\u4f4d\u6570\uff0c\u5219\u6570\u5b57\u5c06\u56db\u820d\u4e94\u5165\u3002 1 2 3 4 5 std :: cout << std :: setprecision ( 3 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 4 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 5 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 6 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 7 ) << 123.456 << '\\n' ; \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 4 5 123 123.5 123.46 123.456 123.456 \u4f7f\u7528 showpoint manipulator \u6216\u6807\u8bb0\uff0c\u53ef\u4ee5\u6253\u5370\u5c0f\u6570\u70b9\u548c\u672b\u5c3e0\u3002 1 2 3 4 5 6 std :: cout << std :: showpoint << '\\n' ; std :: cout << std :: setprecision ( 3 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 4 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 5 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 6 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 7 ) << 123.456 << '\\n' ; \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 123. 123.5 123.46 123.456 123.4560 \u4e0b\u9762\u662f\u4e00\u4e2a\u6709\u66f4\u591a\u4f8b\u5b50\u7684\u6c47\u603b\u8868: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Option Precision 12345.0 0.12345 Normal 3 1.23e+004 0.123 4 1.235e+004 0.1235 5 12345 0.12345 6 12345 0.12345 Showpoint 3 1.23e+004 0.123 4 1.235e+004 0.1235 5 12345. 0.12345 6 12345.0 0.123450 Fixed 3 12345.000 0.123 4 12345.0000 0.1235 5 12345.00000 0.12345 6 12345.000000 0.123450 Scientific 3 1.235e+004 1.235e-001 4 1.2345e+004 1.2345e-001 5 1.23450e+004 1.23450e-001 6 1.234500e+004 1.234500e-001 \u5bbd\u5ea6\u3001\u586b\u5145\u5b57\u7b26\u548c\u5bf9\u9f50 \u901a\u5e38\uff0c\u5f53\u6211\u4eec\u6253\u5370\u6570\u5b57\u65f6\u662f\u4e0d\u8003\u8651\u5176\u5468\u56f4\u7684\u7a7a\u95f4\u7684\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u7684\u786e\u53ef\u4ee5\u8ba9\u6570\u5b57\u5411\u5de6\u6216\u5411\u53f3\u5bf9\u5176\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5fc5\u987b\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u5b57\u6bb5\u5bbd\u5ea6\u2014\u2014\u5373\u8f93\u51fa\u7a7a\u95f4\u7684\u5927\u5c0f\u3002\u5982\u679c\u5b9e\u9645\u6253\u5370\u7684\u6570\u5b57\u5c0f\u4e8e\u5b57\u6bb5\u5bbd\u5ea6\uff0c\u5b83\u5c06\u88ab\u5de6\u5bf9\u9f50\u6216\u53f3\u5bf9\u9f50(\u6839\u636e\u6307\u5b9a)\u3002\u5982\u679c\u5b9e\u9645\u6570\u5b57\u5927\u4e8e\u5b57\u6bb5\u5bbd\u5ea6\uff0c\u5b83\u5e76\u4e0d\u4f1a\u88ab\u622a\u65ad\u2014\u2014\u800c\u662f\u4f1a\u6ea2\u51fa\u3002 Group Flag Meaning std::ios::adjustfield std::ios::internal Left-justifies the sign of the number, and right-justifies the value std::ios::adjustfield std::ios::left Left-justifies the sign and value std::ios::adjustfield std::ios::right Right-justifies the sign and value (default) Manipulator Meaning std::internal Left-justifies the sign of the number, and right-justifies the value std::left Left-justifies the sign and value std::right Right-justifies the sign and value std::setfill(char) Sets the parameter as the fill character (defined in the iomanip header) std::setw(int) Sets the field width for input and output to the parameter (defined in the iomanip header) Member function Meaning std::basic_ostream::fill() Returns the current fill character std::basic_ostream::fill(char) Sets the fill character and returns the old fill character std::ios_base::width() Returns the current field width std::ios_base::width(int) Sets the current field width and returns old field width \u4e3a\u4e86\u4f7f\u7528\u8fd9\u4e9b\u683c\u5f0f\u5316\u64cd\u4f5c\uff0c\u6211\u4eec\u5fc5\u987b\u5148\u8bbe\u7f6e\u5bbd\u5ea6\u3002\u8bbe\u7f6e\u5bbd\u5ea6\u53ef\u4ee5\u901a\u8fc7 width(int) \u6210\u5458\u51fd\u6570\u6765\u5b8c\u6210\uff0c\u6216\u8005\u901a\u8fc7 setw() manipulator\u3002\u6ce8\u610f\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u53f3\u5bf9\u9f50\u7684\u3002 1 2 3 4 5 std :: cout << -12345 << '\\n' ; // print default value with no field width std :: cout << std :: setw ( 10 ) << -12345 << '\\n' ; // print default with field width std :: cout << std :: setw ( 10 ) << std :: left << -12345 << '\\n' ; // print left justified std :: cout << std :: setw ( 10 ) << std :: right << -12345 << '\\n' ; // print right justified std :: cout << std :: setw ( 10 ) << std :: internal << -12345 << '\\n' ; // print internally justified \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 2 3 4 5 -12345 -12345 -12345 -12345 - 12345 \u6ce8\u610f\uff0c setw() \u548c width() \u53ea\u5bf9\u4e0b\u4e00\u4e2a\u8f93\u51fa\u8bed\u53e5\u6709\u6548\uff0c\u548c\u5176\u4ed6\u7684\u6807\u8bb0\u6216manipulators\u4e0d\u540c\uff0c\u5b83\u4eec\u4e0d\u662f\u6301\u4e45\u5316\u7684\u3002 \u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u8bbe\u7f6e\u4e00\u4e2a\u586b\u5145\u5b57\u7b26\u5e76\u8fdb\u884c\u7c7b\u4f3c\u7684\u64cd\u4f5c\uff1a 1 2 3 4 5 6 std :: cout . fill ( '*' ); std :: cout << -12345 << '\\n' ; // print default value with no field width std :: cout << std :: setw ( 10 ) << -12345 << '\\n' ; // print default with field width std :: cout << std :: setw ( 10 ) << std :: left << -12345 << '\\n' ; // print left justified std :: cout << std :: setw ( 10 ) << std :: right << -12345 << '\\n' ; // print right justified std :: cout << std :: setw ( 10 ) << std :: internal << -12345 << '\\n' ; // print internally justified \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 5 -12345 ****-12345 -12345**** ****-12345 -****12345 \u6ce8\u610f\uff0c\u4e4b\u524d\u7684\u7a7a\u683c\u88ab\u586b\u5145\u5b57\u7b26\u6240\u66ff\u4ee3\u4e86\u3002 ostream \u7c7b\u548c iostream \u5e93\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u6709\u7528\u7684\u8f93\u51fa\u51fd\u6570\u3001\u6807\u8bb0\u548c manipulators\u3002\u548c istream \u7c7b\u4e00\u6837\uff0c\u8fd9\u4e9b\u8bae\u9898\u66f4\u9002\u5408\u653e\u5728\u4e13\u6ce8\u4e8e\u6807\u51c6\u5e93\u7684\u6559\u7a0b\u6216\u4e66\u4e2d(\u4f8b\u5982Nicolai M. Josuttis\u7684 \u201cc++\u6807\u51c6\u5e93\u201d )\u3002","title":"23.3 - \u4f7f\u7528ostream\u548cios\u5904\u7406\u8f93\u51fa"},{"location":"23-3-output-with-ostream-and-ios/#_1","text":"\u63d2\u5165\u8fd0\u7b97\u7b26 \u7528\u4e8e\u5411\u8f93\u51fa\u6d41\u63d2\u5165\u6570\u636e\u3002C++\u4e3a\u6240\u6709\u7684\u5185\u5efa\u6570\u636e\u7ed3\u6784\u90fd\u9884\u5b9a\u4e49\u4e86\u63d2\u5165\u8fd0\u7b97\u7b26\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7 \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 \u4e3a\u81ea\u5b9a\u4e49\u7684\u7c7b\u63d0\u4f9b\u63d2\u5165\u8fd0\u7b97\u7b26\u3002 \u5728 23.1 - \u8f93\u5165\u8f93\u51fa\u6d41 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c istream \u548c ostream \u90fd\u662f\u4ece ios \u7c7b\u6d3e\u751f\u6765\u7684\u3002 ios (\u548c ios_base ) \u7c7b\u7684\u4e00\u4e2a\u529f\u80fd\u5c31\u662f\u63a7\u5236\u8f93\u51fa\u7684\u683c\u5f0f\u3002","title":"\u63d2\u5165\u8fd0\u7b97\u7b26"},{"location":"23-3-output-with-ostream-and-ios/#_2","text":"\u66f4\u6539\u683c\u5f0f\u5316\u7684\u65b9\u5f0f\u6709\u4e24\u79cd\uff0c\u6807\u8bb0\u548cmanipulators\u3002\u6807\u8bb0\u4f60\u53ef\u4ee5\u5c06\u5176\u770b\u505a\u662f\u53ef\u4ee5\u5f00\u5173\u7684\u5e03\u5c14\u91cf\u3002\u800cmanipulators\u5219\u662f\u653e\u7f6e\u5728\u6d41\u4e2d\uff0c\u53ef\u4ee5\u4fee\u6539\u8f93\u5165\u8f93\u51fa\u7684\u5bf9\u8c61 \u3002 ==\u8bbe\u7f6e\u4e00\u4e2a\u6807\u8bb0\u53ef\u4ee5\u4f7f\u7528 setf() \u51fd\u6570\u5e76\u63d0\u4f9b\u5408\u9002\u7684\u6807\u8bb0\u4f5c\u4e3a\u53c2\u6570\u3002==\u4f8b\u5982\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0bC++\u5e76\u4e0d\u4f1a\u5728\u6b63\u6570\u524d\u52a0\u4e00\u4e2a+\u53f7\u3002\u4f46\u662f\uff0c\u4f7f\u7528 std::ios::showpos \u6807\u8bb0\u5c31\u53ef\u4ee5\u4fee\u6539\u8be5\u884c\u4e3a\uff1a 1 2 std :: cout . setf ( std :: ios :: showpos ); // \u6253\u5f00std::ios::showpos \u6807\u8bb0 std :: cout << 27 << '\\n' ; \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 +27 \u6211\u4eec\u4e5f\u53ef\u4ee5\u5229\u7528 \u6309\u4f4d\u6216 \u64cd\u4f5c\u4e00\u6b21\u6027\u6253\u5f00\u591a\u4e2a ios \u6807\u8bb0 \uff1a 1 2 std :: cout . setf ( std :: ios :: showpos | std :: ios :: uppercase ); // \u6253\u5f00 std::ios::showpos \u548c std::ios::uppercase \u6807\u8bb0 std :: cout << 1234567.89f << '\\n' ; \u8f93\u51fa\u7ed3\u679c\uff1a 1 +1.23457E+06 \u5173\u95ed\u6807\u8bb0\u4e5f\u5f88\u7b80\u5355\uff0c\u4f7f\u7528 unsetf() \u51fd\u6570\u5373\u53ef\uff1a 1 2 3 4 std :: cout . setf ( std :: ios :: showpos ); // turn on the std::ios::showpos flag std :: cout << 27 << '\\n' ; std :: cout . unsetf ( std :: ios :: showpos ); // turn off the std::ios::showpos flag std :: cout << 28 << '\\n' ; \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 +27 28 \u5728\u4f7f\u7528 setf() \u65f6\u8fd8\u6709\u4e00\u4e2a\u9700\u8981\u63d0\u53ca\u7684\u5c0f\u6280\u5de7\u3002 \u8bb8\u591a\u6807\u5fd7\u5c5e\u4e8e\u7ec4\uff0c\u79f0\u4e3a\u683c\u5f0f\u7ec4\u3002 \u683c\u5f0f\u7ec4 \u662f\u4e00\u7ec4\u6267\u884c\u7c7b\u4f3c(\u6709\u65f6\u4e92\u65a5)\u683c\u5f0f\u5316\u9009\u9879\u7684\u6807\u5fd7 \u3002\u4f8b\u5982\uff0c\u540d\u4e3a\u201cbasefield\u201d\u7684\u683c\u5f0f\u7ec4\u5305\u542b\u6807\u5fd7\u201coct\u201d\u3001\u201cdec\u201d\u548c\u201chex\u201d\uff0c\u5b83\u4eec\u63a7\u5236\u6574\u6570\u503c\u7684\u57fa\u6570\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u8bbe\u7f6e\u4e86\u201cdec\u201d\u6807\u5fd7\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u8fd9\u6837\u505a: 1 2 std :: cout . setf ( std :: ios :: hex ); // \u5c1d\u8bd5\u542f\u52a8\u5341\u516d\u8fdb\u5236\u8f93\u51fa std :: cout << 27 << '\\n' ; \u5219\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 27 \u6ca1\u6709\u6548\u679c\uff01\u8fd9\u662f\u56e0\u4e3a setf() \u53ea\u80fd\u6253\u5f00\u6807\u8bb0\u2014\u2014\u4f46\u662f\u5b83\u6ca1\u6709\u667a\u80fd\u5230\u61c2\u5f97\u53bb\u5173\u95ed\u4e92\u65a5\u7684\u6807\u8bb0\u3002\u56e0\u6b64\uff0c\u5f53 std::ios::hex \u5f00\u542f\u65f6\uff0c std::ios::dec \u4ecd\u7136\u662f\u5f00\u542f\u72b6\u6001\uff0c\u7531\u4e8e\u5b83\u4f18\u5148\u7ea7\u66f4\u9ad8\uff0c\u6240\u4ee5\u4ecd\u7136\u662f\u6309\u7167\u5341\u8fdb\u5236\u8f93\u51fa\u7684\u3002\u6709\u4e24\u4e2a\u529e\u6cd5\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002 \u7b2c\u4e00\u79cd\u65b9\u6cd5\u662f\u5173\u95ed std::ios::dec \u5e76\u5f00\u542f std::hex \uff1a 1 2 3 std :: cout . unsetf ( std :: ios :: dec ); // turn off decimal output std :: cout . setf ( std :: ios :: hex ); // turn on hexadecimal output std :: cout << 27 << '\\n' ; \u8f93\u51fa\u7ed3\u679c\u7b26\u5408\u9884\u671f\uff1a 1 1b \u7b2c\u4e8c\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u53e6\u4e00\u4e2a\u7248\u672c\u7684 setf() \uff0c\u5b83\u6709\u4e24\u4e2a\u5f62\u53c2\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u660e\u6807\u8bb0\u6240\u5c5e\u7684 \u683c\u5f0f\u7ec4 \u3002\u5f53\u4f7f\u7528\u8fd9\u4e2a\u7248\u672c\u7684 setf() \u65f6\uff0c\u540c\u7ec4\u7684\u5176\u4ed6\u6807\u8bb0\u90fd\u4f1a\u88ab\u81ea\u52a8\u5173\u95ed\uff0c\u53ea\u6709\u6211\u4eec\u8bbe\u7f6e\u7684\u6807\u8bb0\u4f1a\u88ab\u6253\u5f00\u3002\u4f8b\u5982\uff1a 1 2 3 // Turn on std::ios::hex as the only std::ios::basefield flag std :: cout . setf ( std :: ios :: hex , std :: ios :: basefield ); std :: cout << 27 << '\\n' ; \u8f93\u51fa\u7ed3\u679c\u4ecd\u7136\u7b26\u5408\u9884\u671f\uff1a 1 1b \u4f7f\u7528 setf() \u548c unsetf() \u662f\u6709\u70b9\u522b\u626d\u7684\uff0c\u56e0\u6b64C++\u63d0\u4f9b\u4e86\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u6765\u6539\u53d8\u683c\u5f0f\u5316\u9009\u9879\uff1amanipulators\u3002manipulators \u6700\u8d5e\u7684\u5730\u65b9\u5728\u4e8e\u5b83\u8db3\u591f\u667a\u80fd\uff0c\u5b83\u80fd\u591f\u81ea\u52a8\u5f00\u5173\u76f8\u5173\u8054\u7684\u6807\u8bb0\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86 manipulators \u6539\u53d8\u8ba1\u6570\u8fdb\u5236\uff1a 1 2 3 std :: cout << std :: hex << 27 << '\\n' ; // print 27 in hex std :: cout << 28 << '\\n' ; // \u4ecd\u7136\u662f\u5341\u516d\u8fdb\u5236\uff0c\u4e0d\u662f\u4e00\u6b21\u6027\u7684 std :: cout << std :: dec << 29 << '\\n' ; // back to decimal \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 3 1b 1c 29 \u901a\u5e38\uff0c\u4f7f\u7528manipulators\u6bd4\u8bbe\u7f6e\u548c\u53d6\u6d88\u8bbe\u7f6e\u6807\u8bb0\u8981\u5bb9\u6613\u5f97\u591a\u3002\u8bb8\u591a\u6807\u8bb0\u90fd\u53ef\u4ee5\u901a\u8fc7\u6807\u5fd7 \u548c manipulators\u6765\u5b9e\u73b0(\u4f8b\u5982\u66f4\u6539\u57fa\u6570)\uff0c\u7136\u800c\uff0c\u53e6\u5916\u4e00\u4e9b\u6807\u8bb0\u5219\u53ea\u80fd\u901a\u8fc7\u6807\u8bb0\u6216manipulators\u5b9e\u73b0\uff0c\u56e0\u6b64\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u8fd9\u4e24\u79cd\u65b9\u6cd5\u975e\u5e38\u91cd\u8981\u3002","title":"\u683c\u5f0f\u5316"},{"location":"23-3-output-with-ostream-and-ios/#_3","text":"\u8fd9\u91cc\u6211\u4eec\u5217\u4e3e\u4e00\u4e9b\u5f88\u6709\u7528\u7684\u6807\u8bb0\u3001manipulators \u4ee5\u53ca\u6210\u5458\u51fd\u6570\u3002 \u6807\u8bb0\u4f4d\u4e8e std::ios \u7c7b\u4e2d\uff0cmanipulators \u5219\u4f4d\u4e8estd \u547d\u540d\u7a7a\u95f4 \u603b\uff0c\u800c\u6210\u5458\u51fd\u6570\u5219\u4f4d\u4e8e std::ostream \u7c7b\u4e2d\u3002 \u7ec4\u6807 \u8bb0 \u542b\u4e49 std::ios::boolalpha \u6253\u5f00\u8be5\u6807\u8bb0\u540e\uff0c\u5e03\u5c14\u7c7b\u578b\u4f1a\u6253\u5370 \u201ctrue\u201d \u6216 \u201cfalse\u201d\u3002\u5982\u679c\u5173\u95ed\uff0c\u5219\u59270\u62161 Manipulator \u542b\u4e49 std::boolalpha \u5e03\u5c14\u7c7b\u578b\u4f1a\u6253\u5370 \u201ctrue\u201d or \u201cfalse\u201d std::noboolalpha \u5e03\u5c14\u7c7b\u578b\u4f1a\u6253\u5370 0 or 1 (default) \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 std :: cout << true << ' ' << false << '\\n' ; std :: cout . setf ( std :: ios :: boolalpha ); std :: cout << true << ' ' << false << '\\n' ; std :: cout << std :: noboolalpha << true << ' ' << false << '\\n' ; std :: cout << std :: boolalpha << true << ' ' << false << '\\n' ; \u7ed3\u679c\uff1a 1 2 3 4 1 0 true false 1 0 true false \u7ec4 \u6807\u8bb0 \u542b\u4e49 std::ios::showpos \u8bbe\u7f6e\u540e\uff0c\u6b63\u6570\u524d\u9762\u4f1a\u6dfb\u52a0+\u53f7 Manipulator \u542b\u4e49 std::showpos \u6b63\u6570\u524d\u9762\u4f1a\u6dfb\u52a0+\u53f7 std::noshowpos \u6b63\u6570\u524d\u9762\u4e0d\u6dfb\u52a0+\u53f7 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 std :: cout << 5 << '\\n' ; std :: cout . setf ( std :: ios :: showpos ); std :: cout << 5 << '\\n' ; std :: cout << std :: noshowpos << 5 << '\\n' ; std :: cout << std :: showpos << 5 << '\\n' ; \u7ed3\u679c\uff1a 1 2 3 4 5 +5 5 +5 \u7ec4 \u6807\u8bb0 \u542b\u4e49 std::ios::uppercase If set, uses upper case letters Manipulator \u542b\u4e49 std::uppercase \u4f7f\u7528\u5927\u5199\u5b57\u6bcd std::nouppercase \u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 std :: cout << 12345678.9 << '\\n' ; std :: cout . setf ( std :: ios :: uppercase ); std :: cout << 12345678.9 << '\\n' ; std :: cout << std :: nouppercase << 12345678.9 << '\\n' ; std :: cout << std :: uppercase << 12345678.9 << '\\n' ; \u7ed3\u679c\uff1a 1 2 3 4 1.23457e+007 1.23457E+007 1.23457e+007 1.23457E+007 \u7ec4 \u6807\u8bb0 \u542b\u4e49 std::ios::basefield std::ios::dec \u6309\u7167\u5341\u8fdb\u5236\u6253\u5370\uff08\u9ed8\u8ba4\u7684\uff09 std::ios::basefield std::ios::hex \u6309\u7167\u5341\u516d\u8fdb\u5236\u6253\u5370 std::ios::basefield std::ios::oct \u6309\u7167\u516b\u8fdb\u5236\u6253\u5370 std::ios::basefield (none) \u6839\u636e\u524d\u7f00\u5b57\u6bcd\u6253\u5370 Manipulator Meaning std::dec \u6309\u7167\u5341\u8fdb\u5236\u6253\u5370 std::hex \u6309\u7167\u5341\u516d\u8fdb\u5236\u6253\u5370 std::oct \u6309\u7167\u516b\u8fdb\u5236\u6253\u5370 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 std :: cout << 27 << '\\n' ; std :: cout . setf ( std :: ios :: dec , std :: ios :: basefield ); std :: cout << 27 << '\\n' ; std :: cout . setf ( std :: ios :: oct , std :: ios :: basefield ); std :: cout << 27 << '\\n' ; std :: cout . setf ( std :: ios :: hex , std :: ios :: basefield ); std :: cout << 27 << '\\n' ; std :: cout << std :: dec << 27 << '\\n' ; std :: cout << std :: oct << 27 << '\\n' ; std :: cout << std :: hex << 27 << '\\n' ; \u7ed3\u679c\uff1a 1 2 3 4 5 6 7 27 27 33 1b 27 33 1b \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u770b\u5230\u901a\u8fc7\u6807\u5fd7\u548c\u901a\u8fc7\u64cd\u4f5c\u7b26\u8bbe\u7f6e\u683c\u5f0f\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u5728\u4ee5\u540e\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u64cd\u7eb5\u7b26\uff0c\u9664\u975e\u5b83\u4eec\u4e0d\u53ef\u7528\u3002","title":"\u6709\u7528\u7684\u6807\u8bb0"},{"location":"23-3-output-with-ostream-and-ios/#_4","text":"\u4f7f\u7528 manipulators (\u6216\u8005\u6807\u8bb0) \u4e5f\u53ef\u4ee5\u6539\u53d8\u6570\u503c\u6253\u5370\u65f6\u7684\u7cbe\u5ea6\u548c\u8981\u5c55\u793a\u7684\u5c0f\u6570\u70b9\u4f4d\u6570\u3002\u8fd9\u4e9b\u683c\u5f0f\u5316\u9009\u9879\u7684\u7ec4\u5408\u6bd4\u8f83\u590d\u6742\uff0c\u8ba9\u6211\u4eec\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\uff1a |\u7ec4 |\u6807\u8bb0 |\u542b\u4e49| |:--|:--|:--|:--| | | std::ios::floatfield | std::ios::fixed |\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528\u5341\u8fdb\u5236\u8bb0\u6570\u6cd5 | | std::ios::floatfield | std::ios::scientific |\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528\u79d1\u5b66\u8bb0\u6570\u6cd5 | | std::ios::floatfield |(none) |\u4f4d\u6570\u8f83\u5c11\u65f6\u4f7f\u7528\u56fa\u5b9a\u8ba1\u6570\u6cd5\uff0c\u4f4d\u6570\u591a\u65f6\u4f7f\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5 | | std::ios::floatfield | std::ios::showpoint |\u5bf9\u4e8e\u6d6e\u70b9\u503c\uff0c\u59cb\u7ec8\u663e\u793a\u5c0f\u6570\u70b9\u548c\u672b\u5c3e0 Manipulator \u542b\u4e49 std::fixed \u4f7f\u7528\u5341\u8fdb\u5236\u8bb0\u6570\u6cd5 std::scientific \u4f7f\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5 std::showpoint \u5bf9\u4e8e\u6d6e\u70b9\u503c\uff0c\u59cb\u7ec8\u663e\u793a\u5c0f\u6570\u70b9\u548c\u672b\u5c3e0 std::noshowpoint \u5bf9\u4e8e\u6d6e\u70b9\u503c\uff0c\u4e0d\u663e\u793a\u5c0f\u6570\u70b9\u548c\u672b\u5c3e0 std::setprecision(int) \u4e3a\u6d6e\u70b9\u6570\u8bbe\u7f6e\u7cbe\u5ea6(\u5b9a\u4e49\u5728 iomanip \u5934\u6587\u4ef6\u4e2d) \u6210\u5458\u51fd\u6570 \u542b\u4e49 std::ios_base::precision() \u8fd4\u56de\u5f53\u524d\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6 std::ios_base::precision(int) \u8bbe\u7f6e\u6d6e\u70b9\u6570\u7cbe\u5ea6\u5e76\u8fd4\u56de\u4e4b\u524d\u7684\u7cbe\u5ea6 \u5982\u679c\u4f7f\u7528\u56fa\u5b9a\u8bb0\u6570\u6cd5\u6216\u79d1\u5b66\u8bb0\u6570\u6cd5\uff0c\u5219\u7cbe\u5ea6\u51b3\u5b9a\u5206\u6570\u4e2d\u663e\u793a\u7684\u5c0f\u6570\u70b9\u540e\u591a\u5c11\u4f4d\u3002\u6ce8\u610f\uff0c\u5982\u679c\u7cbe\u5ea6\u5c0f\u4e8e\u6709\u6548\u4f4d\u6570\uff0c\u5219\u6570\u5b57\u5c06\u56db\u820d\u4e94\u5165\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 std :: cout << std :: fixed << '\\n' ; std :: cout << std :: setprecision ( 3 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 4 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 5 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 6 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 7 ) << 123.456 << '\\n' ; std :: cout << std :: scientific << '\\n' ; std :: cout << std :: setprecision ( 3 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 4 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 5 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 6 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 7 ) << 123.456 << '\\n' ; \u6253\u5370\uff1a 1 2 3 4 5 6 7 8 9 10 11 123.456 123.4560 123.45600 123.456000 123.4560000 1.235e+002 1.2346e+002 1.23456e+002 1.234560e+002 1.2345600e+002 \u5982\u679c\u65e2\u4e0d\u4f7f\u7528\u56fa\u5b9a\u8ba1\u6570\u6cd5\u4e5f\u4e0d\u4f7f\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5\uff0c\u5219\u7cbe\u5ea6\u51b3\u5b9a\u4e86\u5e94\u8be5\u663e\u793a\u591a\u5c11\u6709\u6548\u6570\u5b57\u3002\u540c\u6837\uff0c\u5982\u679c\u7cbe\u5ea6\u5c0f\u4e8e\u6709\u6548\u4f4d\u6570\uff0c\u5219\u6570\u5b57\u5c06\u56db\u820d\u4e94\u5165\u3002 1 2 3 4 5 std :: cout << std :: setprecision ( 3 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 4 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 5 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 6 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 7 ) << 123.456 << '\\n' ; \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 4 5 123 123.5 123.46 123.456 123.456 \u4f7f\u7528 showpoint manipulator \u6216\u6807\u8bb0\uff0c\u53ef\u4ee5\u6253\u5370\u5c0f\u6570\u70b9\u548c\u672b\u5c3e0\u3002 1 2 3 4 5 6 std :: cout << std :: showpoint << '\\n' ; std :: cout << std :: setprecision ( 3 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 4 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 5 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 6 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 7 ) << 123.456 << '\\n' ; \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 123. 123.5 123.46 123.456 123.4560 \u4e0b\u9762\u662f\u4e00\u4e2a\u6709\u66f4\u591a\u4f8b\u5b50\u7684\u6c47\u603b\u8868: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Option Precision 12345.0 0.12345 Normal 3 1.23e+004 0.123 4 1.235e+004 0.1235 5 12345 0.12345 6 12345 0.12345 Showpoint 3 1.23e+004 0.123 4 1.235e+004 0.1235 5 12345. 0.12345 6 12345.0 0.123450 Fixed 3 12345.000 0.123 4 12345.0000 0.1235 5 12345.00000 0.12345 6 12345.000000 0.123450 Scientific 3 1.235e+004 1.235e-001 4 1.2345e+004 1.2345e-001 5 1.23450e+004 1.23450e-001 6 1.234500e+004 1.234500e-001","title":"\u7cbe\u5ea6\u3001\u8ba1\u6570\u6cd5\u548c\u5c0f\u6570\u70b9"},{"location":"23-3-output-with-ostream-and-ios/#_5","text":"\u901a\u5e38\uff0c\u5f53\u6211\u4eec\u6253\u5370\u6570\u5b57\u65f6\u662f\u4e0d\u8003\u8651\u5176\u5468\u56f4\u7684\u7a7a\u95f4\u7684\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u7684\u786e\u53ef\u4ee5\u8ba9\u6570\u5b57\u5411\u5de6\u6216\u5411\u53f3\u5bf9\u5176\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5fc5\u987b\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u5b57\u6bb5\u5bbd\u5ea6\u2014\u2014\u5373\u8f93\u51fa\u7a7a\u95f4\u7684\u5927\u5c0f\u3002\u5982\u679c\u5b9e\u9645\u6253\u5370\u7684\u6570\u5b57\u5c0f\u4e8e\u5b57\u6bb5\u5bbd\u5ea6\uff0c\u5b83\u5c06\u88ab\u5de6\u5bf9\u9f50\u6216\u53f3\u5bf9\u9f50(\u6839\u636e\u6307\u5b9a)\u3002\u5982\u679c\u5b9e\u9645\u6570\u5b57\u5927\u4e8e\u5b57\u6bb5\u5bbd\u5ea6\uff0c\u5b83\u5e76\u4e0d\u4f1a\u88ab\u622a\u65ad\u2014\u2014\u800c\u662f\u4f1a\u6ea2\u51fa\u3002 Group Flag Meaning std::ios::adjustfield std::ios::internal Left-justifies the sign of the number, and right-justifies the value std::ios::adjustfield std::ios::left Left-justifies the sign and value std::ios::adjustfield std::ios::right Right-justifies the sign and value (default) Manipulator Meaning std::internal Left-justifies the sign of the number, and right-justifies the value std::left Left-justifies the sign and value std::right Right-justifies the sign and value std::setfill(char) Sets the parameter as the fill character (defined in the iomanip header) std::setw(int) Sets the field width for input and output to the parameter (defined in the iomanip header) Member function Meaning std::basic_ostream::fill() Returns the current fill character std::basic_ostream::fill(char) Sets the fill character and returns the old fill character std::ios_base::width() Returns the current field width std::ios_base::width(int) Sets the current field width and returns old field width \u4e3a\u4e86\u4f7f\u7528\u8fd9\u4e9b\u683c\u5f0f\u5316\u64cd\u4f5c\uff0c\u6211\u4eec\u5fc5\u987b\u5148\u8bbe\u7f6e\u5bbd\u5ea6\u3002\u8bbe\u7f6e\u5bbd\u5ea6\u53ef\u4ee5\u901a\u8fc7 width(int) \u6210\u5458\u51fd\u6570\u6765\u5b8c\u6210\uff0c\u6216\u8005\u901a\u8fc7 setw() manipulator\u3002\u6ce8\u610f\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u53f3\u5bf9\u9f50\u7684\u3002 1 2 3 4 5 std :: cout << -12345 << '\\n' ; // print default value with no field width std :: cout << std :: setw ( 10 ) << -12345 << '\\n' ; // print default with field width std :: cout << std :: setw ( 10 ) << std :: left << -12345 << '\\n' ; // print left justified std :: cout << std :: setw ( 10 ) << std :: right << -12345 << '\\n' ; // print right justified std :: cout << std :: setw ( 10 ) << std :: internal << -12345 << '\\n' ; // print internally justified \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 2 3 4 5 -12345 -12345 -12345 -12345 - 12345 \u6ce8\u610f\uff0c setw() \u548c width() \u53ea\u5bf9\u4e0b\u4e00\u4e2a\u8f93\u51fa\u8bed\u53e5\u6709\u6548\uff0c\u548c\u5176\u4ed6\u7684\u6807\u8bb0\u6216manipulators\u4e0d\u540c\uff0c\u5b83\u4eec\u4e0d\u662f\u6301\u4e45\u5316\u7684\u3002 \u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u8bbe\u7f6e\u4e00\u4e2a\u586b\u5145\u5b57\u7b26\u5e76\u8fdb\u884c\u7c7b\u4f3c\u7684\u64cd\u4f5c\uff1a 1 2 3 4 5 6 std :: cout . fill ( '*' ); std :: cout << -12345 << '\\n' ; // print default value with no field width std :: cout << std :: setw ( 10 ) << -12345 << '\\n' ; // print default with field width std :: cout << std :: setw ( 10 ) << std :: left << -12345 << '\\n' ; // print left justified std :: cout << std :: setw ( 10 ) << std :: right << -12345 << '\\n' ; // print right justified std :: cout << std :: setw ( 10 ) << std :: internal << -12345 << '\\n' ; // print internally justified \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 5 -12345 ****-12345 -12345**** ****-12345 -****12345 \u6ce8\u610f\uff0c\u4e4b\u524d\u7684\u7a7a\u683c\u88ab\u586b\u5145\u5b57\u7b26\u6240\u66ff\u4ee3\u4e86\u3002 ostream \u7c7b\u548c iostream \u5e93\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u6709\u7528\u7684\u8f93\u51fa\u51fd\u6570\u3001\u6807\u8bb0\u548c manipulators\u3002\u548c istream \u7c7b\u4e00\u6837\uff0c\u8fd9\u4e9b\u8bae\u9898\u66f4\u9002\u5408\u653e\u5728\u4e13\u6ce8\u4e8e\u6807\u51c6\u5e93\u7684\u6559\u7a0b\u6216\u4e66\u4e2d(\u4f8b\u5982Nicolai M. Josuttis\u7684 \u201cc++\u6807\u51c6\u5e93\u201d )\u3002","title":"\u5bbd\u5ea6\u3001\u586b\u5145\u5b57\u7b26\u548c\u5bf9\u9f50"},{"location":"23-4-stream-classes-for-strings/","tags":["io","string"],"text":"Key Takeaway \u4f7f\u7528 \u63d0\u53d6\u8fd0\u7b97\u7b26 \u4ece\u6d41\u4e2d\u63d0\u53d6\u201c\u6570\u5b57\u683c\u5f0f\u201d\u7684\u5b57\u7b26\u4e32\u5230\u6570\u503c\u7c7b\u578b\uff0c\u5373\u53ef\u5b8c\u6210\u5b57\u7b26\u4e32\u5230\u6570\u503c\u7684\u8f6c\u6362 \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u4f60\u770b\u5230\u7684\u6240\u6709I/O\u793a\u4f8b\u90fd\u662f\u4ece cout \u5199\u5165\u6216\u4ece cin \u8bfb\u53d6\u7684\u3002\u4f46\u662f\uff0c\u8fd8\u6709\u53e6\u4e00\u7ec4\u79f0\u4e3a\u5b57\u7b26\u4e32\u6d41\u7c7b\u7684\u7c7b\uff0c\u5b83\u5141\u8bb8\u4f60\u4f7f\u7528\u719f\u6089\u7684 \u63d2\u5165\u8fd0\u7b97\u7b26 \u548c \u63d0\u53d6\u8fd0\u7b97\u7b26 \u6765\u5904\u7406\u5b57\u7b26\u4e32\u3002\u50cf istream \u548c ostream \u4e00\u6837\uff0c\u5b57\u7b26\u4e32\u6d41\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7f13\u51b2\u533a\u6765\u4fdd\u5b58\u6570\u636e\u3002\u7136\u800c\uff0c\u4e0e cin \u548c cout \u4e0d\u540c\u7684\u662f\uff0c\u8fd9\u4e9b\u6d41\u4e0d\u8fde\u63a5\u5230I/O\u901a\u9053(\u5982\u952e\u76d8\u3001\u663e\u793a\u5668\u7b49)\u3002\u5b57\u7b26\u4e32\u6d41\u7684\u4e3b\u8981\u7528\u9014\u4e4b\u4e00\u662f\u7f13\u51b2\u8f93\u51fa\u4ee5\u4fbf\u7a0d\u540e\u663e\u793a\uff0c\u6216\u8005\u9010\u884c\u5904\u7406\u8f93\u5165\u3002 \u5b57\u7b26\u4e32\u6709\u516d\u4e2a\u6d41\u7c7b: \u5176\u4e2d istringstream (\u6d3e\u751f\u81ea istream )\u3001 ostringstream (\u6d3e\u751f\u81ea ostream )\u548c stringstream (\u6d3e\u751f\u81ea iostream )\u7528\u4e8e\u8bfb\u5199\u666e\u901a\u5b57\u7b26\u5bbd\u5ea6\u7684\u5b57\u7b26\u4e32\u3002 Wistringstream \u3001 wostringstream \u548c wstringstream \u7528\u4e8e\u8bfb\u5199\u5bbd\u5b57\u7b26\u4e32\u3002\u8981\u4f7f\u7528 stringstreams \uff0c\u4f60\u9700\u8981 #include sstream \u5934\u6587\u4ef6\u3002 \u6709\u4e24\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u5411 stringstream \u8f93\u5165\u6570\u636e\uff1a \u4f7f\u7528 \u63d2\u5165\u8fd0\u7b97\u7b26 \uff1a 1 2 std :: stringstream os ; os << \"en garde! \\n \" ; // insert \"en garde!\" into the stringstream \u4f7f\u7528 str(string) \u51fd\u6570\u8bbe\u7f6e\u5b57\u7b26\u4e32\u5230\u7f13\u51b2\uff1a 1 2 std :: stringstream os ; os . str ( \"en garde!\" ); // set the stringstream buffer to \"en garde!\" \u7c7b\u4f3c\u7684\uff0c\u4ece stringstream \u8bfb\u53d6\u4e5f\u6709\u4e24\u4e2a\u65b9\u6cd5\uff1a \u4f7f\u7528 str() \u51fd\u6570\u83b7\u53d6\u7f13\u51b2\u4e2d\u7684\u5185\u5bb9\uff1a 1 2 3 std :: stringstream os ; os << \"12345 67.89 \\n \" ; std :: cout << os . str (); \u6253\u5370\uff1a 1 12345 67.89 \u4f7f\u7528 \u63d0\u53d6\u8fd0\u7b97\u7b26 \uff1a 1 2 3 4 5 6 7 8 9 10 11 std :: stringstream os ; os << \"12345 67.89\" ; // insert a string of numbers into the stream std :: string strValue ; os >> strValue ; std :: string strValue2 ; os >> strValue2 ; // print the numbers separated by a dash std :: cout << strValue << \" - \" << strValue2 << '\\n' ; \u7a0b\u5e8f\u6253\u5370\uff1a 1 12345 - 67.89 \u6ce8\u610f\uff0c\u63d0\u53d6\u8fd0\u7b97\u7b26\u4f1a\u904d\u5386\u5b57\u7b26\u4e32\uff0c\u6bcf\u4e2a>>\u4f1a\u8fd4\u56de\u6d41\u4e2d\u4e0b\u4e00\u4e2a\u53ef\u4ee5\u63d0\u53d6\u7684\u5b57\u7b26\u4e32\u3002\u53e6\u4e00\u65b9\u9762\uff0c str() \u53ef\u4ee5\u8fd4\u56de\u6574\u4e2a\u6d41\u4e2d\u7684\u6570\u636e\uff0c\u5373\u4f7f\u6211\u4eec\u5df2\u7ecf\u4f7f\u7528 >> \u4ece\u4e2d\u8fdb\u884c\u4e86\u63d0\u53d6\u3002 \u8f6c\u6362\u5b57\u7b26\u4e32\u548c\u6570\u5b57 \u56e0\u4e3a\u63d2\u5165\u548c\u63d0\u53d6\u64cd\u4f5c\u7b26\u77e5\u9053\u5982\u4f55\u5904\u7406\u6240\u6709\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002 \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u5982\u4f55\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32: 1 2 3 4 5 6 7 8 9 10 std :: stringstream os ; int nValue { 12345 }; double dValue { 67.89 }; os << nValue << ' ' << dValue ; std :: string strValue1 , strValue2 ; os >> strValue1 >> strValue2 ; std :: cout << strValue1 << ' ' << strValue2 << '\\n' ; \u8f93\u51fa\u7ed3\u679c\uff1a 1 12345 67.89 \u73b0\u5728\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\uff1a 1 2 3 4 5 6 7 8 std :: stringstream os ; os << \"12345 67.89\" ; // insert a string of numbers into the stream int nValue ; double dValue ; os >> nValue >> dValue ; std :: cout << nValue << ' ' << dValue << '\\n' ; \u8f93\u51fa\u7ed3\u679c\uff1a 1 12345 67.89 \u6e05\u7406 stringstream \u4e3a\u518d\u6b21\u4f7f\u7528\u505a\u51c6\u5907 \u6e05\u7a7a stringstream \u7f13\u51b2\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff1a \u4f7f\u7528 str() \u8bbe\u7f6e\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u7a7a\u5b57\u7b26\u4e32\uff1a 1 2 3 4 5 6 7 std :: stringstream os ; os << \"Hello \" ; os . str ( \"\" ); // erase the buffer os << \"World!\" ; std :: cout << os . str (); \u4f7f\u7528 str() \u548c\u7a7a\u7684 std::string \u5bf9\u8c61\u5c06\u5176\u8bbe\u7f6e\u4e3a\u7a7a: 1 2 3 4 5 6 7 std :: stringstream os ; os << \"Hello \" ; os . str ( std :: string {}); // \u6e05\u7a7a os << \"World!\" ; std :: cout << os . str (); \u4e24\u4e2a\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u662f\u76f8\u540c\u7684\uff1a 1 World! \u6e05\u7406 stringstream \u65f6\uff0c\u5e94\u8be5\u540c\u65f6\u8c03\u7528 clear() \u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 std :: stringstream os ; os << \"Hello \" ; os . str ( \"\" ); // \u6e05\u7a7a buffer os . clear (); // \u91cd\u7f6e\u9519\u8bef\u6807\u8bb0 os << \"World!\" ; std :: cout << os . str (); clear() \u91cd\u7f6e\u53ef\u80fd\u5df2\u8bbe\u7f6e\u7684\u4efb\u4f55\u9519\u8bef\u6807\u5fd7\uff0c\u5e76\u5c06\u6d41\u8fd4\u56de\u5230ok\u72b6\u6001\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u66f4\u591a\u5730\u8ba8\u8bba\u6d41\u72b6\u6001\u548c\u9519\u8bef\u6807\u5fd7\u3002","title":"23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b"},{"location":"23-4-stream-classes-for-strings/#_1","text":"\u56e0\u4e3a\u63d2\u5165\u548c\u63d0\u53d6\u64cd\u4f5c\u7b26\u77e5\u9053\u5982\u4f55\u5904\u7406\u6240\u6709\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002 \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u5982\u4f55\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32: 1 2 3 4 5 6 7 8 9 10 std :: stringstream os ; int nValue { 12345 }; double dValue { 67.89 }; os << nValue << ' ' << dValue ; std :: string strValue1 , strValue2 ; os >> strValue1 >> strValue2 ; std :: cout << strValue1 << ' ' << strValue2 << '\\n' ; \u8f93\u51fa\u7ed3\u679c\uff1a 1 12345 67.89 \u73b0\u5728\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\uff1a 1 2 3 4 5 6 7 8 std :: stringstream os ; os << \"12345 67.89\" ; // insert a string of numbers into the stream int nValue ; double dValue ; os >> nValue >> dValue ; std :: cout << nValue << ' ' << dValue << '\\n' ; \u8f93\u51fa\u7ed3\u679c\uff1a 1 12345 67.89","title":"\u8f6c\u6362\u5b57\u7b26\u4e32\u548c\u6570\u5b57"},{"location":"23-4-stream-classes-for-strings/#stringstream","text":"\u6e05\u7a7a stringstream \u7f13\u51b2\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff1a \u4f7f\u7528 str() \u8bbe\u7f6e\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u7a7a\u5b57\u7b26\u4e32\uff1a 1 2 3 4 5 6 7 std :: stringstream os ; os << \"Hello \" ; os . str ( \"\" ); // erase the buffer os << \"World!\" ; std :: cout << os . str (); \u4f7f\u7528 str() \u548c\u7a7a\u7684 std::string \u5bf9\u8c61\u5c06\u5176\u8bbe\u7f6e\u4e3a\u7a7a: 1 2 3 4 5 6 7 std :: stringstream os ; os << \"Hello \" ; os . str ( std :: string {}); // \u6e05\u7a7a os << \"World!\" ; std :: cout << os . str (); \u4e24\u4e2a\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u662f\u76f8\u540c\u7684\uff1a 1 World! \u6e05\u7406 stringstream \u65f6\uff0c\u5e94\u8be5\u540c\u65f6\u8c03\u7528 clear() \u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 std :: stringstream os ; os << \"Hello \" ; os . str ( \"\" ); // \u6e05\u7a7a buffer os . clear (); // \u91cd\u7f6e\u9519\u8bef\u6807\u8bb0 os << \"World!\" ; std :: cout << os . str (); clear() \u91cd\u7f6e\u53ef\u80fd\u5df2\u8bbe\u7f6e\u7684\u4efb\u4f55\u9519\u8bef\u6807\u5fd7\uff0c\u5e76\u5c06\u6d41\u8fd4\u56de\u5230ok\u72b6\u6001\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u66f4\u591a\u5730\u8ba8\u8bba\u6d41\u72b6\u6001\u548c\u9519\u8bef\u6807\u5fd7\u3002","title":"\u6e05\u7406 stringstream \u4e3a\u518d\u6b21\u4f7f\u7528\u505a\u51c6\u5907"},{"location":"23-5-stream-states-and-input-validation/","tags":["io"],"text":"Key Takeaway \u6d41\u7684\u72b6\u6001 ios_base \u7c7b\u5305\u542b\u4e86\u591a\u4e2a\u4e0d\u540c\u7684\u72b6\u6001\u6807\u8bb0\uff0c\u5b83\u4eec\u53ef\u4ee5\u7528\u6765\u8868\u793a\u6d41\u7684\u4e0d\u540c\u72b6\u6001\uff1a \u6807\u8bb0 \u542b\u4e49 goodbit \u4e00\u5207\u6b63\u5e38 badbit \u53d1\u751f\u4e86\u67d0\u79cd\u4e25\u91cd\u9519\u8bef(\u4f8b\u5982\uff1a\u7a0b\u5e8f\u8bfb\u53d6\u8d85\u8fc7\u4e86\u6587\u4ef6\u672b\u5c3e) eofbit \u6d41\u5230\u8fbe\u6587\u4ef6\u672b\u5c3e failbit \u53d1\u751f\u975e\u81f4\u547d\u9519\u8bef(\u4f8b\u5982\uff0c\u7a0b\u5e8f\u671f\u671b\u6574\u578b\u65f6\u7528\u6237\u8f93\u5165\u4e86\u5b57\u7b26) \u5c3d\u7ba1\u8fd9\u4e9b\u6807\u8bb0\u5b9a\u4e49\u5728 ios_base \u4e2d\uff0c\u4f46\u662f\u56e0\u4e3a ios \u6d3e\u751f\u81ea ios_base \u800c\u4e14 ios \u5199\u8d77\u6765\u66f4\u7b80\u5355\uff0c\u6240\u4ee5\u6211\u4eec\u901a\u5e38\u4f1a\u901a\u8fc7 ios \u8bbf\u95ee\u8fd9\u4e9b\u6807\u8bb0(\u4f8b\u5982\uff1a as std::ios::failbit ). ios \u540c\u6837\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6210\u5458\u51fd\u6570\uff0c\u7528\u4e8e\u8bbf\u95ee\u8fd9\u4e9b\u72b6\u6001\uff1a \u6210\u5458\u51fd\u6570 \u542b\u4e49 good() \u5982\u679c\u8bbe\u7f6e\u4e86 goodbit \u5219\u8fd4\u56de true (\u6d41\u72b6\u6001\u6b63\u5e38) bad() \u5982\u679c\u8bbe\u7f6e\u4e86 badbit \u5219\u8fd4\u56de true (\u53d1\u751f\u4e25\u91cd\u9519\u8bef) eof() \u5982\u679c\u8bbe\u7f6e\u4e86 eofbit \u5219\u8fd4\u56de true (\u6d41\u5230\u8fbe\u6587\u4ef6\u672b\u5c3e) fail() \u5982\u679c\u8bbe\u7f6e\u4e86 failbit \u5219\u8fd4\u56de true (\u53d1\u751f\u975e\u81f4\u547d\u9519\u8bef) clear() \u6e05\u7a7a\u6240\u6709\u6807\u8bb0\u5e76\u5c06\u6d41\u72b6\u6001\u8bbe\u7f6e\u4e3a goodbit clear(state) \u6e05\u7a7a\u6240\u6709\u6807\u8bb0\u5e76\u5c06\u6d41\u72b6\u6001\u8bbe\u7f6e\u4e3a\u53c2\u6570 state \u8868\u793a\u7684\u72b6\u6001 rdstate() \u8fd4\u56de\u5f53\u524d\u8bbe\u7f6e\u7684\u6807\u8bb0 setstate(state) \u5c06\u6d41\u72b6\u6001\u8bbe\u7f6e\u4e3a\u53c2\u6570 state \u8868\u793a\u7684\u72b6\u6001 \u5b9e\u8df5\u4e2d\u6700\u5e38\u9047\u5230\u7684\u6807\u8bb0\u662f failbit \uff0c\u5f53\u7528\u6237\u8f93\u5165\u975e\u6cd5\u6570\u636e\u65f6\u5219\u8be5\u6807\u8bb0\u5c31\u4f1a\u88ab\u8bbe\u7f6e\u3002\u4f8b\u5982\uff1a 1 2 3 std :: cout << \"Enter your age: \" ; int age {}; std :: cin >> age ; \u7a0b\u5e8f\u671f\u671b\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6574\u578b\u3002\u4f46\u662f\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u662f\u4e00\u4e2a\u975e\u6570\u503c\u6570\u636e\uff0c\u4f8b\u5982 \u201cAlex\u201d\uff0c cin \u5c31\u65e0\u6cd5\u63d0\u53d6\u4efb\u4f55\u5e74\u9f84\u4fe1\u606f\uff0c\u6b64\u65f6 failbit \u4f1a\u88ab\u8bbe\u7f6e \u3002 \u5f53\u53d1\u751f\u9519\u8bef\u65f6\uff0c\u6d41\u4f1a\u88ab\u8bbe\u7f6e\u4e3a\u9664 goodbit \u4ee5\u5916\u7684\u72b6\u6001\uff0c\u540e\u7eed\u5bf9\u6d41\u7684\u64cd\u4f5c\u4e5f\u4f1a\u88ab\u5ffd\u7565\u3002\u6b64\u65f6\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528 clear() \u51fd\u6570\u6765\u91cd\u7f6e\u72b6\u6001\u3002 \u8f93\u5165\u9a8c\u8bc1 \u8f93\u5165\u9a8c\u8bc1 \u662f\u68c0\u67e5\u7528\u6237\u8f93\u5165\u662f\u5426\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u7684\u8fc7\u7a0b\u3002\u8f93\u5165\u9a8c\u8bc1\u901a\u5e38\u53ef\u4ee5\u5206\u4e3a\u4e24\u79cd\u7c7b\u578b\uff1a\u5b57\u7b26\u4e32\u548c\u6570\u5b57\u3002 \u5bf9\u4e8e\u5b57\u7b26\u4e32\u9a8c\u8bc1\uff0c\u6240\u6709\u7528\u6237\u8f93\u5165\u90fd\u4f1a\u88ab\u4f5c\u4e3a\u5b57\u7b26\u4e32\u63a5\u53d7\uff0c\u7136\u540e\u6839\u636e\u5176\u683c\u5f0f\u662f\u5426\u6ee1\u8db3\u8981\u6c42\u6765\u5224\u65ad\u5e94\u8be5\u63a5\u53d7\u6216\u62d2\u7edd\u8be5\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u8981\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u7535\u8bdd\u53f7\u7801\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u786e\u4fdd\u5176\u8f93\u5165\u7684\u6570\u636e\u670910\u4f4d\u6570\u5b57\u3002\u5728\u5927\u591a\u6570\u8bed\u8a00\u4e2d(\u7279\u522b\u662f\u50cfPerl\u548cPHP\u8fd9\u6837\u7684\u811a\u672c\u8bed\u8a00)\uff0c\u8fd9\u662f\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u5b8c\u6210\u7684\u3002C++\u6807\u51c6\u5e93\u4e5f\u6709\u4e00\u4e2a \u6b63\u5219\u8868\u8fbe\u5f0f\u5e93 \u3002\u56e0\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\u6bd4\u624b\u52a8\u5b57\u7b26\u4e32\u9a8c\u8bc1\u6162\uff0c\u6240\u4ee5\u53ea\u6709\u5728\u6027\u80fd(\u7f16\u8bd1\u65f6\u548c\u8fd0\u884c\u65f6)\u8981\u6c42\u4e0d\u9ad8\u6216\u8005\u624b\u52a8\u9a8c\u8bc1\u592a\u9ebb\u70e6\u7684\u60c5\u51b5\u4e0b\u624d\u5e94\u8be5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002 \u5bf9\u4e8e\u6570\u503c\u9a8c\u8bc1\uff0c\u6211\u4eec\u901a\u5e38\u5173\u5fc3\u7684\u662f\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\u662f\u5426\u5728\u7279\u5b9a\u7684\u8303\u56f4\u5185(\u4f8b\u5982\uff0c\u57280\u523020\u4e4b\u95f4)\u3002\u7136\u800c\uff0c\u4e0e\u5b57\u7b26\u4e32\u9a8c\u8bc1\u4e0d\u540c\u7684\u662f\uff0c\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u5b8c\u5168\u4e0d\u662f\u6570\u5b57\u7684\u4e1c\u897f\u2014\u2014\u6211\u4eec\u4e5f\u9700\u8981\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\u3002 \u4e3a\u6b64\uff0cC++\u63d0\u4f9b\u4e86\u8bb8\u591a\u6709\u7528\u7684\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u6765\u786e\u5b9a\u7279\u5b9a\u7684\u5b57\u7b26\u662f\u6570\u5b57\u8fd8\u662f\u5b57\u6bcd\u3002\u4ee5\u4e0b\u51fd\u6570\u4f4d\u4e8e cctype \u5934\u6587\u4ef6\u4e2d \u51fd\u6570 \u542b\u4e49 std::isalnum(int) \u5982\u679c\u53c2\u6570\u662f\u5b57\u6bcd\u6216\u6570\u5b57\u5219\u8fd4\u56de\u975e\u96f6\u503c std::isalpha(int) \u5982\u679c\u53c2\u6570\u662f\u5b57\u6bcd\u5219\u8fd4\u56de\u975e\u96f6\u503c std::iscntrl(int) \u5982\u679c\u53c2\u6570\u662f\u63a7\u5236\u5b57\u7b26\u5219\u8fd4\u56de\u975e\u96f6\u503c std::isdigit(int) \u5982\u679c\u53c2\u6570\u662f\u6570\u5b57\u5219\u8fd4\u56de\u975e\u96f6\u503c std::isgraph(int) \u5982\u679c\u53c2\u6570\u662f\u975e\u7a7a\u767d\u53ef\u6253\u5370\u5b57\u7b26\u5219\u8fd4\u56de\u975e\u96f6\u503c std::isprint(int) \u5982\u679c\u53c2\u6570\u662f\u53ef\u6253\u5370\u5b57\u7b26\uff08\u5305\u62ec\u975e\u7a7a\u767d\uff09\u5219\u8fd4\u56de\u975e\u96f6\u503c std::ispunct(int) \u5982\u679c\u53c2\u6570\u662f\u4e0d\u662f\u5b57\u6bcd\u3001\u6570\u5b57\u6216\u7a7a\u767d\u7b26\u8fd4\u56de\u975e\u96f6\u503c std::isspace(int) \u5982\u679c\u53c2\u6570\u662f\u7a7a\u767d\u7b26\u5219\u8fd4\u56de\u975e\u96f6\u503c std::isxdigit(int) \u5982\u679c\u53c2\u6570\u662f\u5341\u516d\u8fdb\u5236\u5219\u8fd4\u56de\u975e\u96f6\u503c \u5b57\u7b26\u4e32\u9a8c\u8bc1 \u8ba9\u6211\u4eec\u505a\u4e00\u4e2a\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u9a8c\u8bc1\u6848\u4f8b\uff0c\u8981\u6c42\u7528\u6237\u8f93\u5165\u4ed6\u4eec\u7684\u540d\u5b57\u3002\u9a8c\u8bc1\u6807\u51c6\u662f\u7528\u6237\u53ea\u80fd\u8f93\u5165\u5b57\u6bcd\u5b57\u7b26\u6216\u7a7a\u683c\u3002\u5982\u679c\u9047\u5230\u5176\u4ed6\u60c5\u51b5\uff0c\u5219\u5c06\u62d2\u7edd\u8f93\u5165\u3002 \u5bf9\u4e8e\u4e0d\u5b9a\u957f\u5ea6\u7684\u8f93\u5165\uff0c\u9a8c\u8bc1\u5b57\u7b26\u4e32\u7684\u6700\u4f73\u65b9\u6cd5(\u9664\u4e86\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5e93\u4e4b\u5916)\u662f\u9010\u4e2a\u904d\u5386\u5b57\u7b26\u4e32\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u786e\u4fdd\u5b83\u6ee1\u8db3\u9a8c\u8bc1\u6807\u51c6\u3002\u8fd9\u6b63\u662f\u6211\u4eec\u5728\u8fd9\u91cc\u8981\u505a\u7684\uff0c\u6216\u8005\u901a\u8fc7 std::all_of \u5b8c\u6210\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include // std::all_of #include // std::isalpha, std::isspace #include #include #include #include bool isValidName ( std :: string_view name ) { return std :: ranges :: all_of ( name , []( char ch ) { return ( std :: isalpha ( ch ) || std :: isspace ( ch )); }); // Before C++20, without ranges // return std::all_of(name.begin(), name.end(), [](char ch) { // return (std::isalpha(ch) || std::isspace(ch)); // }); } int main () { std :: string name {}; do { std :: cout << \"Enter your name: \" ; std :: getline ( std :: cin , name ); // \u8bfb\u53d6\u6574\u884c\u8f93\u5165\uff0c\u5305\u62ec\u7a7a\u683c } while ( ! isValidName ( name )); std :: cout << \"Hello \" << name << \"! \\n \" ; } \u6ce8\u610f\uff0c\u8fd9\u6bb5\u4ee3\u7801\u5e76\u4e0d\u5b8c\u7f8e\uff1a\u7528\u6237\u53ef\u4ee5\u8bf4\u4ed6\u4eec\u7684\u540d\u5b57\u662f\u201casf w jweo s di we ao\u201d\u6216\u5176\u4ed6\u4e00\u4e9b\u4e71\u4e03\u516b\u7cdf\u7684\u4e1c\u897f\uff0c\u751a\u81f3\u66f4\u7cdf\uff0c\u53ea\u662f\u4e00\u5806\u7a7a\u683c\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6539\u8fdb\u9a8c\u8bc1\u6807\u51c6\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f7f\u5176\u53ea\u63a5\u53d7\u81f3\u5c11\u5305\u542b\u4e00\u4e2a\u5b57\u7b26\u548c\u6700\u591a\u4e00\u4e2a\u7a7a\u683c\u7684\u5b57\u7b26\u4e32\u3002 \u4f5c\u8005\u6ce8 \u8bfb\u8005\u201cWaldo\u201d\u63d0\u4f9b\u4e86\u4e00\u4e2aC++20\u89e3\u51b3\u65b9\u6848(\u4f7f\u7528std::ranges)\u6765\u89e3\u51b3\u8fd9\u4e9b\u7f3a\u70b9\uff0c\u53c2\u8003 \u8fd9\u91cc \u518d\u770b\u53e6\u4e00\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u8981\u6c42\u7528\u6237\u8f93\u5165\u4ed6\u4eec\u7684\u7535\u8bdd\u53f7\u7801\u3002\u867d\u7136\u7528\u6237\u540d\u7684\u957f\u5ea6\u662f\u53ef\u53d8\u7684\uff0c\u4f46\u6bcf\u4e2a\u5b57\u7b26\u7684\u9a8c\u8bc1\u6807\u51c6\u90fd\u662f\u76f8\u540c\u7684\u3002\u800c\u7535\u8bdd\u53f7\u7801\u7684\u957f\u5ea6\u867d\u7136\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u9a8c\u8bc1\u6807\u51c6\u5219\u6839\u636e\u5b57\u7b26\u7684\u4f4d\u7f6e\u800c\u4e0d\u540c\u3002\u56e0\u6b64\u9700\u8981\u91c7\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u9a8c\u8bc1\u7535\u8bdd\u53f7\u7801\u8f93\u5165\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u6839\u636e\u9884\u5148\u786e\u5b9a\u7684\u6a21\u677f\u68c0\u67e5\u7528\u6237\u7684\u8f93\u5165\u662f\u5426\u5339\u914d\u3002\u6a21\u677f\u7684\u5de5\u4f5c\u65b9\u5f0f\u5982\u4e0b: # \u4f1a\u5339\u914d\u4efb\u4f55\u8f93\u5165\u7684\u6570\u5b57\uff1b @ \u4f1a\u5339\u914d\u4efb\u4f55\u8f93\u5165\u7684\u82f1\u6587\u5b57\u6bcd\uff1b _ \u4f1a\u5339\u914d\u4efb\u4f55\u8f93\u5165\u7684\u7a7a\u767d\uff1b ? \u5339\u914d\u6240\u6709\u3002 \u9664\u6b64\u4e4b\u5916\uff0c\u7528\u6237\u8f93\u5165\u7684\u5176\u4ed6\u5b57\u7b26\u5fc5\u987b\u7cbe\u786e\u5339\u914d\u3002 \u56e0\u6b64\uff0c\u5982\u679c\u6a21\u677f\u4e3a \u201c(###) ###-####\u201d\uff0c\u5219\u8868\u793a\u7528\u6237\u9700\u8981\u8f93\u5165 \u2018 ( \u2018\uff0c\u4e09\u4e2a\u6570\u5b57\uff0c \u2018 ) \u2019 \uff0c\u7a7a\u683c\uff0c\u4e09\u4e2a\u6570\u5b57\uff0c\u77ed\u6a2a\u7ebf\uff0c\u56db\u4e2a\u6570\u5b57\u3002\u5bf9\u4e8e\u4efb\u4f55\u4e0d\u6ee1\u8db3\u4e0a\u8ff0\u8981\u6c42\u7684\u8f93\u5165\uff0c\u90fd\u4f1a\u88ab\u7a0b\u5e8f\u62d2\u7edd\u3002 \u4ee3\u7801\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 #include // std::equal #include // std::isdigit, std::isspace, std::isalpha #include #include #include #include #include bool inputMatches ( std :: string_view input , std :: string_view pattern ) { if ( input . length () != pattern . length ()) { return false ; } // This table defines all special symbols that can match a range of user input // Each symbol is mapped to a function that determines whether the input is valid for that symbol static const std :: map < char , int ( * )( int ) > validators { { '#' , & std :: isdigit }, { '_' , & std :: isspace }, { '@' , & std :: isalpha }, { '?' , []( int ) { return 1 ; } } }; // Before C++20, use // return std::equal(input.begin(), input.end(), pattern.begin(), [](char ch, char mask) -> bool { // ... return std :: ranges :: equal ( input , pattern , []( char ch , char mask ) -> bool { if ( auto found { validators . find ( mask ) }; found != validators . end ()) { // \u627e\u5230\u53ef\u5339\u914d\u7684\u6a21\u5f0f\uff0c\u8c03\u7528\u5bf9\u5e94\u7684\u6821\u9a8c\u51fd\u6570 return ( * found -> second )( ch ); } else { // \u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u6a21\u5f0f\uff0c\u6b64\u65f6\u8981\u6c42\u5b57\u7b26\u7cbe\u786e\u5339\u914d return ( ch == mask ); } }); } int main () { std :: string phoneNumber {}; do { std :: cout << \"Enter a phone number (###) ###-####: \" ; std :: getline ( std :: cin , phoneNumber ); } while ( ! inputMatches ( phoneNumber , \"(###) ###-####\" )); std :: cout << \"You entered: \" << phoneNumber << '\\n' ; } \u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u5f3a\u5236\u7528\u6237\u6309\u7167\u6211\u4eec\u8981\u6c42\u7684\u683c\u5f0f\u8f93\u5165\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u4ecd\u7136\u6709\u5c40\u9650\u6027\uff1a\u5982\u679c # \u3001 @ \u3001 _ \u548c ? \u4e5f\u662f\u5408\u6cd5\u7684\u7528\u6237\u8f93\u5165\uff0c\u5219\u8be5\u51fd\u6570\u5c06\u65e0\u6cd5\u5de5\u4f5c\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u7b26\u53f7\u5df2\u7ecf\u88ab\u8d4b\u4e88\u4e86\u7279\u6b8a\u7684\u542b\u4e49\u3002\u6b64\u5916\uff0c\u4e0d\u540c\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u8be5\u6a21\u677f\u4e2d\u6ca1\u6709\u7b26\u53f7\u53ef\u4ee5\u8868\u793a\u201c\u8f93\u5165\u4e00\u7ec4\u4e0d\u5b9a\u957f\u7684\u5b57\u7b26\u201d\u3002\u56e0\u6b64\uff0c\u8be5\u6a21\u677f\u4e0d\u80fd\u88ab\u7528\u6765\u786e\u4fdd\u7528\u6237\u8f93\u5165\u4e24\u4e2a\u5355\u8bcd\uff08\u7a7a\u683c\u9694\u5f00\uff09\uff0c\u56e0\u4e3a\u5b83\u4e0d\u80fd\u5904\u7406\u53d8\u957f\u7684\u8f93\u5165\u3002\u5bf9\u4e8e\u8fd9\u79cd\u7c7b\u578b\u7684\u8f93\u5165\uff0c\u975e\u6a21\u677f\u7684\u65b9\u6cd5\u66f4\u52a0\u5408\u9002 \u3002 \u6570\u503c\u9a8c\u8bc1 \u5728\u5904\u7406\u6570\u503c\u578b\u8f93\u5165\u65f6\uff0c\u6700\u5e38\u89c1\u7684\u505a\u6cd5\u662f\u4f7f\u7528 \u63d0\u53d6\u8fd0\u7b97\u7b26 \u5c06\u8f93\u5165\u63d0\u53d6\u5230\u4e00\u4e2a\u6570\u503c\u7c7b\u578b\u7684\u53d8\u91cf\u4e2d \uff08\u53c2\u89c1\uff1a 23-4-stream-classes-for-strings#\u8f6c\u6362\u5b57\u7b26\u4e32\u548c\u6570\u5b57 \uff09\u3002\u901a\u8fc7\u68c0\u67e5\u6d41\u72b6\u6001\u6807\u8bb0 failbit \uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u77e5\u9053\u7528\u6237\u7684\u8f93\u5165\u662f\u5426\u662f\u4e00\u4e2a\u6570\u5b57\u3002 \u8bf7\u770b\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include int main () { int age {}; while ( true ) { std :: cout << \"Enter your age: \" ; std :: cin >> age ; if ( std :: cin . fail ()) // \u65e0\u6cd5\u63d0\u53d6 { std :: cin . clear (); // \u91cd\u7f6e\u72b6\u6001\u4e3a goodbit\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u8c03\u7528 ignore() std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // \u4ece\u6d41\u4e2d\u5ffd\u7565\u975e\u6cd5\u8f93\u5165 continue ; // \u7ee7\u7eed } if ( age <= 0 ) // \u786e\u4fdd\u8f93\u5165\u7684\u5e74\u9f84\u662f\u6b63\u6570 continue ; break ; } std :: cout << \"You entered: \" << age << '\\n' ; } \u5982\u679c\u7528\u6237\u8f93\u5165\u4e86\u4e00\u4e2a\u6574\u578b\u6570\uff0c\u5219\u63d0\u53d6\u53ef\u4ee5\u6210\u529f\u3002 std::cin.fail() \u7684\u7ed3\u679c\u4e3a false \uff0c\u4e0d\u8fdb\u5165if\u8bed\u53e5\u3002\u5047\u8bbe\u7528\u6237\u8f93\u5165\u7684\u662f\u6b63\u6570\u7684\u8bdd\uff0c\u5219\u63a5\u4e0b\u6765\u4f1a\u8d70\u5230 break \u8bed\u53e5\uff0c\u9000\u51fa\u5faa\u73af\u3002 \u5982\u679c\uff0c\u7528\u6237\u8f93\u5165\u7684\u4e0d\u662f\u6574\u6570\u800c\u662f\u5b57\u6bcd\uff0c\u5219\u63d0\u53d6\u4f1a\u5931\u8d25\u3002\u6b64\u65f6 std::cin.fail() \u8fd4\u56de true \uff0c\u6240\u4ee5\u4f1a\u8d70\u5230if\u8bed\u53e5\u4e2d\u3002\u5728\u8bed\u53e5\u5757\u7684\u6700\u540e\uff0c\u4f1a\u9047\u5230 continue \uff0c\u7ee7\u7eed\u6267\u884cwhile\u5faa\u73af\uff0c\u8981\u6c42\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002 \u4f46\u662f\uff0c\u8fd8\u6709\u4e00\u79cd\u60c5\u51b5\u6211\u4eec\u8fd8\u6ca1\u6709\u6d4b\u8bd5\u2014\u2014\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u5f00\u5934\u662f\u6570\u5b57\uff0c\u4f46\u662f\u540e\u9762\u662f\u5b57\u6bcd(\u4f8b\u5982 \u201c34abcd56\u201d)\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f00\u5934\u662f\u6570\u5b57(34)\uff0c\u5b83\u4f1a\u88ab\u63d0\u53d6\u6210\u5e74\u9f84\uff0c\u5269\u4e0b\u7684\u5b57\u7b26\u4e32\u5219\u4f1a\u6d41\u5728\u8f93\u5165\u6d41\u4e2d\uff0c failbit \u5e76\u4e0d\u4f1a\u88ab\u8bbe\u7f6e\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u4e24\u4e2a\u95ee\u9898\uff1a \u5982\u679c\u4f60\u5c06\u5b83\u4f5c\u4e3a\u5408\u6cd5\u8f93\u5165\uff0c\u5219\u8f93\u5165\u6d41\u4e2d\u6b8b\u7559\u4e86\u5783\u573e\u4fe1\u606f\uff1b \u5982\u679c\u4f60\u4e0d\u8ba4\u4e3a\u8fd9\u662f\u5408\u6cd5\u8f93\u5165\uff0c\u4f46\u662f\u5b83\u5e76\u4e0d\u4f1a\u88ab\u62d2\u7edd\uff08\u800c\u4e14\u4f60\u7684\u6d41\u4e2d\u8fd8\u6709\u5783\u573e\u4fe1\u606f\uff09\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u65b9\u6cd5\u4e5f\u5f88\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include #include int main () { int age {}; while ( true ) { std :: cout << \"Enter your age: \" ; std :: cin >> age ; if ( std :: cin . fail ()) // no extraction took place { std :: cin . clear (); // reset the state bits back to goodbit so we can use ignore() std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // \u4ece\u6d41\u4e2d\u6e05\u9664\u9519\u8bef\u7684\u8f93\u5165 continue ; // try again } std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // \u4ece\u8f93\u5165\u6d41\u4e2d\u5254\u9664\u591a\u4f59\u7684\u8f93\u5165 if ( age <= 0 ) // make sure age is positive continue ; break ; } std :: cout << \"You entered: \" << age << '\\n' ; } \u5982\u679c\u4f60\u4e0d\u5e0c\u671b\u8fd9\u6837\u7684\u8f93\u5165\u662f\u6709\u6548\u7684\uff0c\u90a3\u8fd8\u9700\u8981\u505a\u4e00\u4e9b\u989d\u5916\u7684\u5de5\u4f5c\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u524d\u9762\u7684\u89e3\u51b3\u65b9\u6848\u5df2\u7ecf\u5b8c\u6210\u4e86\u4e00\u534a\u7684\u5de5\u4f5c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 gcount() \u51fd\u6570\u6765\u786e\u5b9a\u6709\u591a\u5c11\u5b57\u7b26\u88ab\u5ffd\u7565\u4e86\u3002\u5982\u679c\u8f93\u5165\u6709\u6548\uff0c\u5219 gcount() \u5e94\u8be5\u8fd4\u56de1(\u88ab\u4e22\u5f03\u7684\u6362\u884c\u7b26)\u3002\u5982\u679c\u8fd4\u56de\u503c\u5927\u4e8e1\uff0c\u8bf4\u660e\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u6ca1\u6709\u88ab\u6b63\u786e\u63d0\u53d6\uff0c\u6b64\u65f6\u5e94\u8be5\u8981\u6c42\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002\u8fd9\u91cc\u6709\u4e00\u4e2a\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include #include int main () { int age {}; while ( true ) { std :: cout << \"Enter your age: \" ; std :: cin >> age ; if ( std :: cin . fail ()) // no extraction took place { std :: cin . clear (); // reset the state bits back to goodbit so we can use ignore() std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // clear out the bad input from the stream continue ; // try again } std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // clear out any additional input from the stream if ( std :: cin . gcount () > 1 ) // if we cleared out more than one additional character { continue ; // we'll consider this input to be invalid } if ( age <= 0 ) // make sure age is positive { continue ; } break ; } std :: cout << \"You entered: \" << age << '\\n' ; } \u6570\u503c\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fdb\u884c\u9a8c\u8bc1 \u4e3a\u4e86\u5f97\u5230\u4e00\u4e2a\u503c\uff0c\u7adf\u7136\u8981\u50cf\u4e0a\u9762\u90a3\u6837\u8fd9\u4e48\u591a\u7684\u64cd\u4f5c\uff01\u5904\u7406\u6570\u5b57\u8f93\u5165\u7684\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u5c06\u5176\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8bfb\u5165\uff0c\u7136\u540e\u5c1d\u8bd5\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u5b57\u7c7b\u578b\u3002\u4e0b\u9762\u7684\u7a0b\u5e8f\u4f7f\u7528\u4e86\u8fd9\u79cd\u65b9\u6cd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include // std::from_chars #include #include #include #include std :: optional < int > extractAge ( std :: string_view age ) { int result {}; auto end { age . data () + age . length () }; // \u5c1d\u8bd5\u4eceage\u4e2d\u63d0\u53d6\u4e00\u4e2a\u6574\u6570 if ( std :: from_chars ( age . data (), end , result ). ptr != end ) { return {}; } if ( result <= 0 ) // \u786e\u4fdd\u5e74\u9f84\u662f\u6b63\u6570 { return {}; } return result ; } int main () { int age {}; while ( true ) { std :: cout << \"Enter your age: \" ; std :: string strAge {}; std :: getline ( std :: cin >> std :: ws , strAge ); if ( auto extracted { extractAge ( strAge ) }) { age = * extracted ; break ; } } std :: cout << \"You entered: \" << age << '\\n' ; } COPY \u4e0e\u76f4\u63a5\u63d0\u53d6\u6570\u5b57\u76f8\u6bd4\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7684\u5de5\u4f5c\u91cf\u662f\u591a\u8fd8\u662f\u5c11\u53d6\u51b3\u4e8e\u9a8c\u8bc1\u53c2\u6570\u548c\u9650\u5236\u3002 \u5982\u4f60\u6240\u89c1\uff0c\u5728C++\u4e2d\u8fdb\u884c\u8f93\u5165\u9a8c\u8bc1\u9700\u8981\u5927\u91cf\u7684\u5de5\u4f5c\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u8bb8\u591a\u8fd9\u6837\u7684\u4efb\u52a1(\u4f8b\u5982\uff0c\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fdb\u884c\u6570\u503c\u9a8c\u8bc1)\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u8f6c\u6362\u4e3a\u53ef\u4ee5\u5728\u5404\u79cd\u60c5\u51b5\u4e0b\u91cd\u7528\u7684\u51fd\u6570\u3002","title":"23.5 - \u6d41\u72b6\u6001\u548c\u8f93\u5165\u9a8c\u8bc1"},{"location":"23-5-stream-states-and-input-validation/#_1","text":"ios_base \u7c7b\u5305\u542b\u4e86\u591a\u4e2a\u4e0d\u540c\u7684\u72b6\u6001\u6807\u8bb0\uff0c\u5b83\u4eec\u53ef\u4ee5\u7528\u6765\u8868\u793a\u6d41\u7684\u4e0d\u540c\u72b6\u6001\uff1a \u6807\u8bb0 \u542b\u4e49 goodbit \u4e00\u5207\u6b63\u5e38 badbit \u53d1\u751f\u4e86\u67d0\u79cd\u4e25\u91cd\u9519\u8bef(\u4f8b\u5982\uff1a\u7a0b\u5e8f\u8bfb\u53d6\u8d85\u8fc7\u4e86\u6587\u4ef6\u672b\u5c3e) eofbit \u6d41\u5230\u8fbe\u6587\u4ef6\u672b\u5c3e failbit \u53d1\u751f\u975e\u81f4\u547d\u9519\u8bef(\u4f8b\u5982\uff0c\u7a0b\u5e8f\u671f\u671b\u6574\u578b\u65f6\u7528\u6237\u8f93\u5165\u4e86\u5b57\u7b26) \u5c3d\u7ba1\u8fd9\u4e9b\u6807\u8bb0\u5b9a\u4e49\u5728 ios_base \u4e2d\uff0c\u4f46\u662f\u56e0\u4e3a ios \u6d3e\u751f\u81ea ios_base \u800c\u4e14 ios \u5199\u8d77\u6765\u66f4\u7b80\u5355\uff0c\u6240\u4ee5\u6211\u4eec\u901a\u5e38\u4f1a\u901a\u8fc7 ios \u8bbf\u95ee\u8fd9\u4e9b\u6807\u8bb0(\u4f8b\u5982\uff1a as std::ios::failbit ). ios \u540c\u6837\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6210\u5458\u51fd\u6570\uff0c\u7528\u4e8e\u8bbf\u95ee\u8fd9\u4e9b\u72b6\u6001\uff1a \u6210\u5458\u51fd\u6570 \u542b\u4e49 good() \u5982\u679c\u8bbe\u7f6e\u4e86 goodbit \u5219\u8fd4\u56de true (\u6d41\u72b6\u6001\u6b63\u5e38) bad() \u5982\u679c\u8bbe\u7f6e\u4e86 badbit \u5219\u8fd4\u56de true (\u53d1\u751f\u4e25\u91cd\u9519\u8bef) eof() \u5982\u679c\u8bbe\u7f6e\u4e86 eofbit \u5219\u8fd4\u56de true (\u6d41\u5230\u8fbe\u6587\u4ef6\u672b\u5c3e) fail() \u5982\u679c\u8bbe\u7f6e\u4e86 failbit \u5219\u8fd4\u56de true (\u53d1\u751f\u975e\u81f4\u547d\u9519\u8bef) clear() \u6e05\u7a7a\u6240\u6709\u6807\u8bb0\u5e76\u5c06\u6d41\u72b6\u6001\u8bbe\u7f6e\u4e3a goodbit clear(state) \u6e05\u7a7a\u6240\u6709\u6807\u8bb0\u5e76\u5c06\u6d41\u72b6\u6001\u8bbe\u7f6e\u4e3a\u53c2\u6570 state \u8868\u793a\u7684\u72b6\u6001 rdstate() \u8fd4\u56de\u5f53\u524d\u8bbe\u7f6e\u7684\u6807\u8bb0 setstate(state) \u5c06\u6d41\u72b6\u6001\u8bbe\u7f6e\u4e3a\u53c2\u6570 state \u8868\u793a\u7684\u72b6\u6001 \u5b9e\u8df5\u4e2d\u6700\u5e38\u9047\u5230\u7684\u6807\u8bb0\u662f failbit \uff0c\u5f53\u7528\u6237\u8f93\u5165\u975e\u6cd5\u6570\u636e\u65f6\u5219\u8be5\u6807\u8bb0\u5c31\u4f1a\u88ab\u8bbe\u7f6e\u3002\u4f8b\u5982\uff1a 1 2 3 std :: cout << \"Enter your age: \" ; int age {}; std :: cin >> age ; \u7a0b\u5e8f\u671f\u671b\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6574\u578b\u3002\u4f46\u662f\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u662f\u4e00\u4e2a\u975e\u6570\u503c\u6570\u636e\uff0c\u4f8b\u5982 \u201cAlex\u201d\uff0c cin \u5c31\u65e0\u6cd5\u63d0\u53d6\u4efb\u4f55\u5e74\u9f84\u4fe1\u606f\uff0c\u6b64\u65f6 failbit \u4f1a\u88ab\u8bbe\u7f6e \u3002 \u5f53\u53d1\u751f\u9519\u8bef\u65f6\uff0c\u6d41\u4f1a\u88ab\u8bbe\u7f6e\u4e3a\u9664 goodbit \u4ee5\u5916\u7684\u72b6\u6001\uff0c\u540e\u7eed\u5bf9\u6d41\u7684\u64cd\u4f5c\u4e5f\u4f1a\u88ab\u5ffd\u7565\u3002\u6b64\u65f6\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528 clear() \u51fd\u6570\u6765\u91cd\u7f6e\u72b6\u6001\u3002","title":"\u6d41\u7684\u72b6\u6001"},{"location":"23-5-stream-states-and-input-validation/#_2","text":"\u8f93\u5165\u9a8c\u8bc1 \u662f\u68c0\u67e5\u7528\u6237\u8f93\u5165\u662f\u5426\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u7684\u8fc7\u7a0b\u3002\u8f93\u5165\u9a8c\u8bc1\u901a\u5e38\u53ef\u4ee5\u5206\u4e3a\u4e24\u79cd\u7c7b\u578b\uff1a\u5b57\u7b26\u4e32\u548c\u6570\u5b57\u3002 \u5bf9\u4e8e\u5b57\u7b26\u4e32\u9a8c\u8bc1\uff0c\u6240\u6709\u7528\u6237\u8f93\u5165\u90fd\u4f1a\u88ab\u4f5c\u4e3a\u5b57\u7b26\u4e32\u63a5\u53d7\uff0c\u7136\u540e\u6839\u636e\u5176\u683c\u5f0f\u662f\u5426\u6ee1\u8db3\u8981\u6c42\u6765\u5224\u65ad\u5e94\u8be5\u63a5\u53d7\u6216\u62d2\u7edd\u8be5\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u8981\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u7535\u8bdd\u53f7\u7801\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u786e\u4fdd\u5176\u8f93\u5165\u7684\u6570\u636e\u670910\u4f4d\u6570\u5b57\u3002\u5728\u5927\u591a\u6570\u8bed\u8a00\u4e2d(\u7279\u522b\u662f\u50cfPerl\u548cPHP\u8fd9\u6837\u7684\u811a\u672c\u8bed\u8a00)\uff0c\u8fd9\u662f\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u5b8c\u6210\u7684\u3002C++\u6807\u51c6\u5e93\u4e5f\u6709\u4e00\u4e2a \u6b63\u5219\u8868\u8fbe\u5f0f\u5e93 \u3002\u56e0\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\u6bd4\u624b\u52a8\u5b57\u7b26\u4e32\u9a8c\u8bc1\u6162\uff0c\u6240\u4ee5\u53ea\u6709\u5728\u6027\u80fd(\u7f16\u8bd1\u65f6\u548c\u8fd0\u884c\u65f6)\u8981\u6c42\u4e0d\u9ad8\u6216\u8005\u624b\u52a8\u9a8c\u8bc1\u592a\u9ebb\u70e6\u7684\u60c5\u51b5\u4e0b\u624d\u5e94\u8be5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002 \u5bf9\u4e8e\u6570\u503c\u9a8c\u8bc1\uff0c\u6211\u4eec\u901a\u5e38\u5173\u5fc3\u7684\u662f\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\u662f\u5426\u5728\u7279\u5b9a\u7684\u8303\u56f4\u5185(\u4f8b\u5982\uff0c\u57280\u523020\u4e4b\u95f4)\u3002\u7136\u800c\uff0c\u4e0e\u5b57\u7b26\u4e32\u9a8c\u8bc1\u4e0d\u540c\u7684\u662f\uff0c\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u5b8c\u5168\u4e0d\u662f\u6570\u5b57\u7684\u4e1c\u897f\u2014\u2014\u6211\u4eec\u4e5f\u9700\u8981\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\u3002 \u4e3a\u6b64\uff0cC++\u63d0\u4f9b\u4e86\u8bb8\u591a\u6709\u7528\u7684\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u6765\u786e\u5b9a\u7279\u5b9a\u7684\u5b57\u7b26\u662f\u6570\u5b57\u8fd8\u662f\u5b57\u6bcd\u3002\u4ee5\u4e0b\u51fd\u6570\u4f4d\u4e8e cctype \u5934\u6587\u4ef6\u4e2d \u51fd\u6570 \u542b\u4e49 std::isalnum(int) \u5982\u679c\u53c2\u6570\u662f\u5b57\u6bcd\u6216\u6570\u5b57\u5219\u8fd4\u56de\u975e\u96f6\u503c std::isalpha(int) \u5982\u679c\u53c2\u6570\u662f\u5b57\u6bcd\u5219\u8fd4\u56de\u975e\u96f6\u503c std::iscntrl(int) \u5982\u679c\u53c2\u6570\u662f\u63a7\u5236\u5b57\u7b26\u5219\u8fd4\u56de\u975e\u96f6\u503c std::isdigit(int) \u5982\u679c\u53c2\u6570\u662f\u6570\u5b57\u5219\u8fd4\u56de\u975e\u96f6\u503c std::isgraph(int) \u5982\u679c\u53c2\u6570\u662f\u975e\u7a7a\u767d\u53ef\u6253\u5370\u5b57\u7b26\u5219\u8fd4\u56de\u975e\u96f6\u503c std::isprint(int) \u5982\u679c\u53c2\u6570\u662f\u53ef\u6253\u5370\u5b57\u7b26\uff08\u5305\u62ec\u975e\u7a7a\u767d\uff09\u5219\u8fd4\u56de\u975e\u96f6\u503c std::ispunct(int) \u5982\u679c\u53c2\u6570\u662f\u4e0d\u662f\u5b57\u6bcd\u3001\u6570\u5b57\u6216\u7a7a\u767d\u7b26\u8fd4\u56de\u975e\u96f6\u503c std::isspace(int) \u5982\u679c\u53c2\u6570\u662f\u7a7a\u767d\u7b26\u5219\u8fd4\u56de\u975e\u96f6\u503c std::isxdigit(int) \u5982\u679c\u53c2\u6570\u662f\u5341\u516d\u8fdb\u5236\u5219\u8fd4\u56de\u975e\u96f6\u503c","title":"\u8f93\u5165\u9a8c\u8bc1"},{"location":"23-5-stream-states-and-input-validation/#_3","text":"\u8ba9\u6211\u4eec\u505a\u4e00\u4e2a\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u9a8c\u8bc1\u6848\u4f8b\uff0c\u8981\u6c42\u7528\u6237\u8f93\u5165\u4ed6\u4eec\u7684\u540d\u5b57\u3002\u9a8c\u8bc1\u6807\u51c6\u662f\u7528\u6237\u53ea\u80fd\u8f93\u5165\u5b57\u6bcd\u5b57\u7b26\u6216\u7a7a\u683c\u3002\u5982\u679c\u9047\u5230\u5176\u4ed6\u60c5\u51b5\uff0c\u5219\u5c06\u62d2\u7edd\u8f93\u5165\u3002 \u5bf9\u4e8e\u4e0d\u5b9a\u957f\u5ea6\u7684\u8f93\u5165\uff0c\u9a8c\u8bc1\u5b57\u7b26\u4e32\u7684\u6700\u4f73\u65b9\u6cd5(\u9664\u4e86\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5e93\u4e4b\u5916)\u662f\u9010\u4e2a\u904d\u5386\u5b57\u7b26\u4e32\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u786e\u4fdd\u5b83\u6ee1\u8db3\u9a8c\u8bc1\u6807\u51c6\u3002\u8fd9\u6b63\u662f\u6211\u4eec\u5728\u8fd9\u91cc\u8981\u505a\u7684\uff0c\u6216\u8005\u901a\u8fc7 std::all_of \u5b8c\u6210\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include // std::all_of #include // std::isalpha, std::isspace #include #include #include #include bool isValidName ( std :: string_view name ) { return std :: ranges :: all_of ( name , []( char ch ) { return ( std :: isalpha ( ch ) || std :: isspace ( ch )); }); // Before C++20, without ranges // return std::all_of(name.begin(), name.end(), [](char ch) { // return (std::isalpha(ch) || std::isspace(ch)); // }); } int main () { std :: string name {}; do { std :: cout << \"Enter your name: \" ; std :: getline ( std :: cin , name ); // \u8bfb\u53d6\u6574\u884c\u8f93\u5165\uff0c\u5305\u62ec\u7a7a\u683c } while ( ! isValidName ( name )); std :: cout << \"Hello \" << name << \"! \\n \" ; } \u6ce8\u610f\uff0c\u8fd9\u6bb5\u4ee3\u7801\u5e76\u4e0d\u5b8c\u7f8e\uff1a\u7528\u6237\u53ef\u4ee5\u8bf4\u4ed6\u4eec\u7684\u540d\u5b57\u662f\u201casf w jweo s di we ao\u201d\u6216\u5176\u4ed6\u4e00\u4e9b\u4e71\u4e03\u516b\u7cdf\u7684\u4e1c\u897f\uff0c\u751a\u81f3\u66f4\u7cdf\uff0c\u53ea\u662f\u4e00\u5806\u7a7a\u683c\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6539\u8fdb\u9a8c\u8bc1\u6807\u51c6\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f7f\u5176\u53ea\u63a5\u53d7\u81f3\u5c11\u5305\u542b\u4e00\u4e2a\u5b57\u7b26\u548c\u6700\u591a\u4e00\u4e2a\u7a7a\u683c\u7684\u5b57\u7b26\u4e32\u3002 \u4f5c\u8005\u6ce8 \u8bfb\u8005\u201cWaldo\u201d\u63d0\u4f9b\u4e86\u4e00\u4e2aC++20\u89e3\u51b3\u65b9\u6848(\u4f7f\u7528std::ranges)\u6765\u89e3\u51b3\u8fd9\u4e9b\u7f3a\u70b9\uff0c\u53c2\u8003 \u8fd9\u91cc \u518d\u770b\u53e6\u4e00\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u8981\u6c42\u7528\u6237\u8f93\u5165\u4ed6\u4eec\u7684\u7535\u8bdd\u53f7\u7801\u3002\u867d\u7136\u7528\u6237\u540d\u7684\u957f\u5ea6\u662f\u53ef\u53d8\u7684\uff0c\u4f46\u6bcf\u4e2a\u5b57\u7b26\u7684\u9a8c\u8bc1\u6807\u51c6\u90fd\u662f\u76f8\u540c\u7684\u3002\u800c\u7535\u8bdd\u53f7\u7801\u7684\u957f\u5ea6\u867d\u7136\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u9a8c\u8bc1\u6807\u51c6\u5219\u6839\u636e\u5b57\u7b26\u7684\u4f4d\u7f6e\u800c\u4e0d\u540c\u3002\u56e0\u6b64\u9700\u8981\u91c7\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u9a8c\u8bc1\u7535\u8bdd\u53f7\u7801\u8f93\u5165\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u6839\u636e\u9884\u5148\u786e\u5b9a\u7684\u6a21\u677f\u68c0\u67e5\u7528\u6237\u7684\u8f93\u5165\u662f\u5426\u5339\u914d\u3002\u6a21\u677f\u7684\u5de5\u4f5c\u65b9\u5f0f\u5982\u4e0b: # \u4f1a\u5339\u914d\u4efb\u4f55\u8f93\u5165\u7684\u6570\u5b57\uff1b @ \u4f1a\u5339\u914d\u4efb\u4f55\u8f93\u5165\u7684\u82f1\u6587\u5b57\u6bcd\uff1b _ \u4f1a\u5339\u914d\u4efb\u4f55\u8f93\u5165\u7684\u7a7a\u767d\uff1b ? \u5339\u914d\u6240\u6709\u3002 \u9664\u6b64\u4e4b\u5916\uff0c\u7528\u6237\u8f93\u5165\u7684\u5176\u4ed6\u5b57\u7b26\u5fc5\u987b\u7cbe\u786e\u5339\u914d\u3002 \u56e0\u6b64\uff0c\u5982\u679c\u6a21\u677f\u4e3a \u201c(###) ###-####\u201d\uff0c\u5219\u8868\u793a\u7528\u6237\u9700\u8981\u8f93\u5165 \u2018 ( \u2018\uff0c\u4e09\u4e2a\u6570\u5b57\uff0c \u2018 ) \u2019 \uff0c\u7a7a\u683c\uff0c\u4e09\u4e2a\u6570\u5b57\uff0c\u77ed\u6a2a\u7ebf\uff0c\u56db\u4e2a\u6570\u5b57\u3002\u5bf9\u4e8e\u4efb\u4f55\u4e0d\u6ee1\u8db3\u4e0a\u8ff0\u8981\u6c42\u7684\u8f93\u5165\uff0c\u90fd\u4f1a\u88ab\u7a0b\u5e8f\u62d2\u7edd\u3002 \u4ee3\u7801\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 #include // std::equal #include // std::isdigit, std::isspace, std::isalpha #include #include #include #include #include bool inputMatches ( std :: string_view input , std :: string_view pattern ) { if ( input . length () != pattern . length ()) { return false ; } // This table defines all special symbols that can match a range of user input // Each symbol is mapped to a function that determines whether the input is valid for that symbol static const std :: map < char , int ( * )( int ) > validators { { '#' , & std :: isdigit }, { '_' , & std :: isspace }, { '@' , & std :: isalpha }, { '?' , []( int ) { return 1 ; } } }; // Before C++20, use // return std::equal(input.begin(), input.end(), pattern.begin(), [](char ch, char mask) -> bool { // ... return std :: ranges :: equal ( input , pattern , []( char ch , char mask ) -> bool { if ( auto found { validators . find ( mask ) }; found != validators . end ()) { // \u627e\u5230\u53ef\u5339\u914d\u7684\u6a21\u5f0f\uff0c\u8c03\u7528\u5bf9\u5e94\u7684\u6821\u9a8c\u51fd\u6570 return ( * found -> second )( ch ); } else { // \u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u6a21\u5f0f\uff0c\u6b64\u65f6\u8981\u6c42\u5b57\u7b26\u7cbe\u786e\u5339\u914d return ( ch == mask ); } }); } int main () { std :: string phoneNumber {}; do { std :: cout << \"Enter a phone number (###) ###-####: \" ; std :: getline ( std :: cin , phoneNumber ); } while ( ! inputMatches ( phoneNumber , \"(###) ###-####\" )); std :: cout << \"You entered: \" << phoneNumber << '\\n' ; } \u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u5f3a\u5236\u7528\u6237\u6309\u7167\u6211\u4eec\u8981\u6c42\u7684\u683c\u5f0f\u8f93\u5165\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u4ecd\u7136\u6709\u5c40\u9650\u6027\uff1a\u5982\u679c # \u3001 @ \u3001 _ \u548c ? \u4e5f\u662f\u5408\u6cd5\u7684\u7528\u6237\u8f93\u5165\uff0c\u5219\u8be5\u51fd\u6570\u5c06\u65e0\u6cd5\u5de5\u4f5c\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u7b26\u53f7\u5df2\u7ecf\u88ab\u8d4b\u4e88\u4e86\u7279\u6b8a\u7684\u542b\u4e49\u3002\u6b64\u5916\uff0c\u4e0d\u540c\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u8be5\u6a21\u677f\u4e2d\u6ca1\u6709\u7b26\u53f7\u53ef\u4ee5\u8868\u793a\u201c\u8f93\u5165\u4e00\u7ec4\u4e0d\u5b9a\u957f\u7684\u5b57\u7b26\u201d\u3002\u56e0\u6b64\uff0c\u8be5\u6a21\u677f\u4e0d\u80fd\u88ab\u7528\u6765\u786e\u4fdd\u7528\u6237\u8f93\u5165\u4e24\u4e2a\u5355\u8bcd\uff08\u7a7a\u683c\u9694\u5f00\uff09\uff0c\u56e0\u4e3a\u5b83\u4e0d\u80fd\u5904\u7406\u53d8\u957f\u7684\u8f93\u5165\u3002\u5bf9\u4e8e\u8fd9\u79cd\u7c7b\u578b\u7684\u8f93\u5165\uff0c\u975e\u6a21\u677f\u7684\u65b9\u6cd5\u66f4\u52a0\u5408\u9002 \u3002","title":"\u5b57\u7b26\u4e32\u9a8c\u8bc1"},{"location":"23-5-stream-states-and-input-validation/#_4","text":"\u5728\u5904\u7406\u6570\u503c\u578b\u8f93\u5165\u65f6\uff0c\u6700\u5e38\u89c1\u7684\u505a\u6cd5\u662f\u4f7f\u7528 \u63d0\u53d6\u8fd0\u7b97\u7b26 \u5c06\u8f93\u5165\u63d0\u53d6\u5230\u4e00\u4e2a\u6570\u503c\u7c7b\u578b\u7684\u53d8\u91cf\u4e2d \uff08\u53c2\u89c1\uff1a 23-4-stream-classes-for-strings#\u8f6c\u6362\u5b57\u7b26\u4e32\u548c\u6570\u5b57 \uff09\u3002\u901a\u8fc7\u68c0\u67e5\u6d41\u72b6\u6001\u6807\u8bb0 failbit \uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u77e5\u9053\u7528\u6237\u7684\u8f93\u5165\u662f\u5426\u662f\u4e00\u4e2a\u6570\u5b57\u3002 \u8bf7\u770b\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include int main () { int age {}; while ( true ) { std :: cout << \"Enter your age: \" ; std :: cin >> age ; if ( std :: cin . fail ()) // \u65e0\u6cd5\u63d0\u53d6 { std :: cin . clear (); // \u91cd\u7f6e\u72b6\u6001\u4e3a goodbit\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u8c03\u7528 ignore() std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // \u4ece\u6d41\u4e2d\u5ffd\u7565\u975e\u6cd5\u8f93\u5165 continue ; // \u7ee7\u7eed } if ( age <= 0 ) // \u786e\u4fdd\u8f93\u5165\u7684\u5e74\u9f84\u662f\u6b63\u6570 continue ; break ; } std :: cout << \"You entered: \" << age << '\\n' ; } \u5982\u679c\u7528\u6237\u8f93\u5165\u4e86\u4e00\u4e2a\u6574\u578b\u6570\uff0c\u5219\u63d0\u53d6\u53ef\u4ee5\u6210\u529f\u3002 std::cin.fail() \u7684\u7ed3\u679c\u4e3a false \uff0c\u4e0d\u8fdb\u5165if\u8bed\u53e5\u3002\u5047\u8bbe\u7528\u6237\u8f93\u5165\u7684\u662f\u6b63\u6570\u7684\u8bdd\uff0c\u5219\u63a5\u4e0b\u6765\u4f1a\u8d70\u5230 break \u8bed\u53e5\uff0c\u9000\u51fa\u5faa\u73af\u3002 \u5982\u679c\uff0c\u7528\u6237\u8f93\u5165\u7684\u4e0d\u662f\u6574\u6570\u800c\u662f\u5b57\u6bcd\uff0c\u5219\u63d0\u53d6\u4f1a\u5931\u8d25\u3002\u6b64\u65f6 std::cin.fail() \u8fd4\u56de true \uff0c\u6240\u4ee5\u4f1a\u8d70\u5230if\u8bed\u53e5\u4e2d\u3002\u5728\u8bed\u53e5\u5757\u7684\u6700\u540e\uff0c\u4f1a\u9047\u5230 continue \uff0c\u7ee7\u7eed\u6267\u884cwhile\u5faa\u73af\uff0c\u8981\u6c42\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002 \u4f46\u662f\uff0c\u8fd8\u6709\u4e00\u79cd\u60c5\u51b5\u6211\u4eec\u8fd8\u6ca1\u6709\u6d4b\u8bd5\u2014\u2014\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u5f00\u5934\u662f\u6570\u5b57\uff0c\u4f46\u662f\u540e\u9762\u662f\u5b57\u6bcd(\u4f8b\u5982 \u201c34abcd56\u201d)\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f00\u5934\u662f\u6570\u5b57(34)\uff0c\u5b83\u4f1a\u88ab\u63d0\u53d6\u6210\u5e74\u9f84\uff0c\u5269\u4e0b\u7684\u5b57\u7b26\u4e32\u5219\u4f1a\u6d41\u5728\u8f93\u5165\u6d41\u4e2d\uff0c failbit \u5e76\u4e0d\u4f1a\u88ab\u8bbe\u7f6e\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u4e24\u4e2a\u95ee\u9898\uff1a \u5982\u679c\u4f60\u5c06\u5b83\u4f5c\u4e3a\u5408\u6cd5\u8f93\u5165\uff0c\u5219\u8f93\u5165\u6d41\u4e2d\u6b8b\u7559\u4e86\u5783\u573e\u4fe1\u606f\uff1b \u5982\u679c\u4f60\u4e0d\u8ba4\u4e3a\u8fd9\u662f\u5408\u6cd5\u8f93\u5165\uff0c\u4f46\u662f\u5b83\u5e76\u4e0d\u4f1a\u88ab\u62d2\u7edd\uff08\u800c\u4e14\u4f60\u7684\u6d41\u4e2d\u8fd8\u6709\u5783\u573e\u4fe1\u606f\uff09\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u65b9\u6cd5\u4e5f\u5f88\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include #include int main () { int age {}; while ( true ) { std :: cout << \"Enter your age: \" ; std :: cin >> age ; if ( std :: cin . fail ()) // no extraction took place { std :: cin . clear (); // reset the state bits back to goodbit so we can use ignore() std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // \u4ece\u6d41\u4e2d\u6e05\u9664\u9519\u8bef\u7684\u8f93\u5165 continue ; // try again } std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // \u4ece\u8f93\u5165\u6d41\u4e2d\u5254\u9664\u591a\u4f59\u7684\u8f93\u5165 if ( age <= 0 ) // make sure age is positive continue ; break ; } std :: cout << \"You entered: \" << age << '\\n' ; } \u5982\u679c\u4f60\u4e0d\u5e0c\u671b\u8fd9\u6837\u7684\u8f93\u5165\u662f\u6709\u6548\u7684\uff0c\u90a3\u8fd8\u9700\u8981\u505a\u4e00\u4e9b\u989d\u5916\u7684\u5de5\u4f5c\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u524d\u9762\u7684\u89e3\u51b3\u65b9\u6848\u5df2\u7ecf\u5b8c\u6210\u4e86\u4e00\u534a\u7684\u5de5\u4f5c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 gcount() \u51fd\u6570\u6765\u786e\u5b9a\u6709\u591a\u5c11\u5b57\u7b26\u88ab\u5ffd\u7565\u4e86\u3002\u5982\u679c\u8f93\u5165\u6709\u6548\uff0c\u5219 gcount() \u5e94\u8be5\u8fd4\u56de1(\u88ab\u4e22\u5f03\u7684\u6362\u884c\u7b26)\u3002\u5982\u679c\u8fd4\u56de\u503c\u5927\u4e8e1\uff0c\u8bf4\u660e\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u6ca1\u6709\u88ab\u6b63\u786e\u63d0\u53d6\uff0c\u6b64\u65f6\u5e94\u8be5\u8981\u6c42\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002\u8fd9\u91cc\u6709\u4e00\u4e2a\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include #include int main () { int age {}; while ( true ) { std :: cout << \"Enter your age: \" ; std :: cin >> age ; if ( std :: cin . fail ()) // no extraction took place { std :: cin . clear (); // reset the state bits back to goodbit so we can use ignore() std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // clear out the bad input from the stream continue ; // try again } std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // clear out any additional input from the stream if ( std :: cin . gcount () > 1 ) // if we cleared out more than one additional character { continue ; // we'll consider this input to be invalid } if ( age <= 0 ) // make sure age is positive { continue ; } break ; } std :: cout << \"You entered: \" << age << '\\n' ; }","title":"\u6570\u503c\u9a8c\u8bc1"},{"location":"23-5-stream-states-and-input-validation/#_5","text":"\u4e3a\u4e86\u5f97\u5230\u4e00\u4e2a\u503c\uff0c\u7adf\u7136\u8981\u50cf\u4e0a\u9762\u90a3\u6837\u8fd9\u4e48\u591a\u7684\u64cd\u4f5c\uff01\u5904\u7406\u6570\u5b57\u8f93\u5165\u7684\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u5c06\u5176\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8bfb\u5165\uff0c\u7136\u540e\u5c1d\u8bd5\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u5b57\u7c7b\u578b\u3002\u4e0b\u9762\u7684\u7a0b\u5e8f\u4f7f\u7528\u4e86\u8fd9\u79cd\u65b9\u6cd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include // std::from_chars #include #include #include #include std :: optional < int > extractAge ( std :: string_view age ) { int result {}; auto end { age . data () + age . length () }; // \u5c1d\u8bd5\u4eceage\u4e2d\u63d0\u53d6\u4e00\u4e2a\u6574\u6570 if ( std :: from_chars ( age . data (), end , result ). ptr != end ) { return {}; } if ( result <= 0 ) // \u786e\u4fdd\u5e74\u9f84\u662f\u6b63\u6570 { return {}; } return result ; } int main () { int age {}; while ( true ) { std :: cout << \"Enter your age: \" ; std :: string strAge {}; std :: getline ( std :: cin >> std :: ws , strAge ); if ( auto extracted { extractAge ( strAge ) }) { age = * extracted ; break ; } } std :: cout << \"You entered: \" << age << '\\n' ; } COPY \u4e0e\u76f4\u63a5\u63d0\u53d6\u6570\u5b57\u76f8\u6bd4\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7684\u5de5\u4f5c\u91cf\u662f\u591a\u8fd8\u662f\u5c11\u53d6\u51b3\u4e8e\u9a8c\u8bc1\u53c2\u6570\u548c\u9650\u5236\u3002 \u5982\u4f60\u6240\u89c1\uff0c\u5728C++\u4e2d\u8fdb\u884c\u8f93\u5165\u9a8c\u8bc1\u9700\u8981\u5927\u91cf\u7684\u5de5\u4f5c\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u8bb8\u591a\u8fd9\u6837\u7684\u4efb\u52a1(\u4f8b\u5982\uff0c\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fdb\u884c\u6570\u503c\u9a8c\u8bc1)\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u8f6c\u6362\u4e3a\u53ef\u4ee5\u5728\u5404\u79cd\u60c5\u51b5\u4e0b\u91cd\u7528\u7684\u51fd\u6570\u3002","title":"\u6570\u503c\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fdb\u884c\u9a8c\u8bc1"},{"location":"23-6-basic-file-IO/","tags":["io","file"],"text":"Key Takeaway \u57fa\u672c\u7684\u6587\u4ef6IO\u7c7b\u6709\u4e09\u4e2a\uff1a ifstream (\u6d3e\u751f\u81ea istream )\u3001 ofstream (\u6d3e\u751f\u81ea ostream ) \u548c fstream (\u6d3e\u751f\u81ea iostream ) C++\u4e2d\u7684\u6587\u4ef6I/O\u548c\u666e\u901a I/O \u975e\u5e38\u7c7b\u4f3c\u3002\u57fa\u672c\u7684\u6587\u4ef6IO\u7c7b\u6709\u4e09\u4e2a\uff1a ifstream (\u6d3e\u751f\u81ea istream )\u3001 ofstream (\u6d3e\u751f\u81ea ostream ) \u548c fstream (\u6d3e\u751f\u81ea iostream )\u3002\u8fd9\u4e9b\u7c7b\u53ef\u4ee5\u5206\u522b\u7528\u4e8e\u6587\u4ef6\u8f93\u5165\u3001\u6587\u4ef6\u8f93\u51fa\u548c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa\u3002\u4f7f\u7528\u8fd9\u4e9b\u6587\u4ef6IO\u7c7b\u65f6\uff0c\u9700\u8981\u5305\u542b fstream \u5934\u6587\u4ef6\u3002 \u548c cout \u3001 cin \u3001 cerr \u4ee5\u53ca clog \u8fd9\u4e9b\u5df2\u7ecf\u8fde\u63a5\u597d\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7684\u6d41\u4e0d\u540c\uff0c\u6587\u4ef6\u6d41\u5fc5\u987b\u7531\u7a0b\u5e8f\u5458\u624b\u52a8\u8bbe\u7f6e\u3002\u4e0d\u8fc7\uff0c\u64cd\u4f5c\u5f88\u7b80\u5355\uff1a\u8981\u6253\u5f00\u4e00\u4e2a\u7528\u4e8e\u8bfb\u5199\u7684\u6587\u4ef6\u65f6\uff0c\u53ea\u9700\u8981\u5b9e\u4f8b\u5316\u4e00\u4e2a\u9700\u8981\u7684\u6587\u4ef6IO\u7c7b\uff08\u5c06\u6587\u4ef6\u540d\u4f5c\u4e3a\u53c2\u6570\uff09\u3002\u7136\u540e\u4f7f\u7528 \u63d0\u53d6\u8fd0\u7b97\u7b26 \u6216 \u63d2\u5165\u8fd0\u7b97\u7b26 \u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u6216\u5411\u6587\u4ef6\u5199\u5165\u6570\u636e\u3002\u5b8c\u6210\u64cd\u4f5c\u540e\uff0c \u6709\u51e0\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5173\u95ed\u8be5\u6587\u4ef6\uff1a\u663e\u5f0f\u5730\u8c03\u7528 close() \u51fd\u6570\u6216\u8005\u8ba9\u6587\u4ef6IO\u53d8\u91cf \u79bb\u5f00\u4f5c\u7528\u57df \uff08 \u6790\u6784\u51fd\u6570 \u4f1a\u5e2e\u4f60\u5173\u95ed\u6587\u4ef6\uff09\u3002 \u6587\u4ef6\u8f93\u51fa \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86 ofstream \u7c7b\u8fdb\u884c\u6587\u4ef6\u8f93\u51fa\u3002\u975e\u5e38\u7b80\u5355\u76f4\u63a5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include int main () { // ofstream \u7528\u4e8e\u5199\u6587\u4ef6 // \u521b\u5efa\u4e00\u4e2a\u540d\u4e3a Sample.txt \u7684\u6587\u4ef6 std :: ofstream outf { \"Sample.txt\" }; // \u5982\u679c\u65e0\u6cd5\u6253\u5f00\u8f93\u51fa\u6587\u4ef6\u6d41 if ( ! outf ) { // \u6253\u5370\u9519\u8bef\u5e76\u9000\u51fa std :: cerr << \"Uh oh, Sample.txt could not be opened for writing! \\n \" ; return 1 ; } // \u5411\u6587\u4ef6\u5199\u5165\u4e24\u884c\u6587\u5b57 outf << \"This is line 1 \\n \" ; outf << \"This is line 2 \\n \" ; return 0 ; // outf \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0cofstream\u6790\u6784\u51fd\u6570\u4f1a\u5173\u95ed\u6587\u4ef6 } \u6b64\u65f6\u6253\u5f00\u9879\u76ee\u76ee\u5f55\uff0c\u5e94\u8be5\u4f1a\u770b\u5230\u4e00\u4e2a\u540d\u4e3a Sample.txt \u7684\u6587\u4ef6\u3002\u5982\u679c\u7528\u6587\u672c\u7f16\u8f91\u5668\u6253\u5f00\u5b83\uff0c\u53ef\u4ee5\u770b\u5230\u5b83\u786e\u5b9e\u5305\u542b\u4e86\u521a\u624d\u5199\u5165\u6587\u4ef6\u7684\u4e24\u884c\u6587\u672c\u3002 \u6ce8\u610f\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 put() \u51fd\u6570\u5411\u6587\u4ef6\u5199\u5165\u5355\u4e2a\u5b57\u7b26\u3002 \u6587\u4ef6\u8f93\u5165 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f1a\u6253\u5f00\u4e0a\u4e00\u4e2a\u4f8b\u5b50\u4e2d\u65b0\u521b\u5efa\u7684\u6587\u4ef6\u5e76\u8bfb\u53d6\u5176\u5185\u5bb9\u3002\u6ce8\u610f\uff0c\u5728\u5230\u8fbe\u6587\u4ef6\u672b\u5c3e\uff08EOF\uff09\u65f6\uff0c ifstream \u4f1a\u8fd4\u56de0\u3002\u6211\u4eec\u9700\u8981\u57fa\u4e8e\u8fd9\u4e2a\u7279\u6027\u6765\u5224\u65ad\u8bfb\u53d6\u591a\u5c11\u5185\u5bb9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include #include int main () { // ifstream \u7528\u4e8e\u8bfb\u53d6\u6587\u4ef6 // \u4ece Sample.txt \u8bfb\u53d6\u5185\u5bb9 std :: ifstream inf { \"Sample.txt\" }; // \u5982\u679c\u65e0\u6cd5\u6253\u5f00\u8f93\u5165\u6587\u4ef6\u6d41 if ( ! inf ) { // \u6253\u5370\u9519\u8bef\u5e76\u9000\u51fa std :: cerr << \"Uh oh, Sample.txt could not be opened for reading! \\n \" ; return 1 ; } // \u5982\u679c\u4ecd\u7136\u6709\u5185\u5bb9\u53ef\u4ee5\u8bfb\u53d6 while ( inf ) { // \u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5b57\u7b26\u4e32\u5e76\u6253\u5370 std :: string strInput ; inf >> strInput ; std :: cout << strInput << '\\n' ; } return 0 ; // \u5f53 inf \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0cifstream \u7684\u6790\u6784\u51fd\u6570\u4f1a\u8d1f\u8d23\u5173\u95ed\u6587\u4ef6 } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 5 6 7 8 This is line 1 This is line 2 \u989d\u3002\u3002\u8fd9\u597d\u50cf\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c\u3002\u8fd8\u8bb0\u5f97\u5417\uff1f \u63d0\u53d6\u8fd0\u7b97\u7b26 \u4f1a\u88ab\u7a7a\u683c\u5206\u5272\uff0c\u6240\u4ee5\u4e3a\u4e86\u8bfb\u53d6\u4e00\u6574\u884c\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528 getline() \u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include #include #include int main () { // ifstream \u7528\u4e8e\u8bfb\u53d6\u6587\u4ef6 // \u4ece Sample.txt \u8bfb\u53d6\u5185\u5bb9 std :: ifstream inf { \"Sample.txt\" }; // \u5982\u679c\u65e0\u6cd5\u6253\u5f00\u8f93\u5165\u6587\u4ef6\u6d41 if ( ! inf ) { // \u6253\u5370\u9519\u8bef\u5e76\u9000\u51fa std :: cerr << \"Uh oh, Sample.txt could not be opened for reading! \\n \" ; return 1 ; } // \u5982\u679c\u4ecd\u7136\u6709\u5185\u5bb9\u53ef\u4ee5\u8bfb\u53d6 while ( inf ) { // \u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5b57\u7b26\u4e32\u5e76\u6253\u5370 std :: string strInput ; std :: getline ( inf , strInput ); //\u4f7f\u7528 getline \u8bfb\u53d6\u4e00\u6574\u884c std :: cout << strInput << '\\n' ; } return 0 ; // \u5f53 inf \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0cifstream \u7684\u6790\u6784\u51fd\u6570\u4f1a\u8d1f\u8d23\u5173\u95ed\u6587\u4ef6 } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 This is line 1 This is line 2 \u5e26\u7f13\u51b2\u7684\u8f93\u51fa C++\u4e2d\u7684\u8f93\u51fa\u53ef\u4ee5\u88ab\u7f13\u51b2\u3002\u8fd9\u610f\u5473\u7740\u8f93\u51fa\u5230\u6587\u4ef6\u6d41\u7684\u4efb\u4f55\u5185\u5bb9\u90fd\u4e0d\u4f1a\u7acb\u5373\u5199\u5165\u78c1\u76d8\u3002\u8fd9\u6837\u4e00\u6765\u5c31\u53ef\u4ee5\u5c06\u51e0\u4e2a\u8f93\u51fa\u64cd\u4f5c\u5408\u5e76\u5904\u7406\u3002\u8fd9\u6837\u505a\u4e3b\u8981\u662f\u51fa\u4e8e\u6027\u80fd\u539f\u56e0\uff0c\u5f53\u7f13\u51b2\u533a\u88ab\u5199\u5165\u78c1\u76d8\u65f6\uff0c\u8fd9\u88ab\u79f0\u4e3a \u5237\u65b0\u7f13\u51b2\u533a \u3002 \u89e6\u53d1\u7f13\u51b2\u533a\u5237\u65b0\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u5173\u95ed\u6587\u4ef6\u2014\u2014\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u5c06\u88ab\u5237\u65b0\u5230\u78c1\u76d8\uff0c\u7136\u540e\u5173\u95ed\u6587\u4ef6\u3002 \u7f13\u51b2\u901a\u5e38\u4e0d\u662f\u95ee\u9898\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5b83\u53ef\u80fd\u4f1a\u7ed9\u7c97\u5fc3\u7684\u4eba\u5e26\u6765\u9ebb\u70e6\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e3b\u8981\u7684\u7f6a\u9b41\u7978\u9996\u662f\u7f13\u51b2\u533a\u4e2d\u6709\u6570\u636e\uff0c\u7136\u540e\u7a0b\u5e8f\u7acb\u5373\u7ec8\u6b62(\u901a\u8fc7\u5d29\u6e83\u6216\u8c03\u7528 exit() )\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e0d\u4f1a\u6267\u884c\u6587\u4ef6\u6d41\u7c7b\u7684\u6790\u6784\u51fd\u6570\uff0c\u8fd9\u610f\u5473\u7740\u6587\u4ef6\u6c38\u8fdc\u4e0d\u4f1a\u5173\u95ed\uff0c\u6240\u4ee5\u7f13\u51b2\u533a\u6c38\u8fdc\u4e0d\u4f1a\u5237\u65b0\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f13\u51b2\u533a\u4e2d\u7684\u6570\u636e\u5c06\u4e0d\u4f1a\u88ab\u5199\u5165\u78c1\u76d8\uff0c\u5e76\u4e14\u6c38\u8fdc\u4e22\u5931\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u5728\u8c03\u7528 exit() \u4e4b\u524d\u663e\u5f0f\u5173\u95ed\u6240\u6709\u6253\u5f00\u7684\u6587\u4ef6\u603b\u662f\u4e00\u4e2a\u597d\u4e3b\u610f \u7f13\u51b2\u533a\u53ef\u4ee5\u901a\u8fc7 ostream::flush() \u51fd\u6570\u624b\u52a8\u5237\u65b0\uff0c\u6216\u8005\u5411\u8f93\u51fa\u6d41\u53d1\u9001 std::flush \u4e5f\u53ef\u4ee5\u3002\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u53ef\u4ee5\u7528\u4e8e\u786e\u4fdd\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u7acb\u5373\u5199\u5165\u78c1\u76d8\uff0c\u4ee5\u9632\u7a0b\u5e8f\u7a81\u7136\u5d29\u6e83\u3002 \u53e6\u5916\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c std::endl; \u4f1a\u5237\u65b0\u8f93\u51fa\u6d41\u3002\u56e0\u6b64\uff0c\u8fc7\u5ea6\u4f7f\u7528 std::endl (\u5bfc\u81f4\u4e0d\u5fc5\u8981\u7684\u7f13\u51b2\u533a\u5237\u65b0)\u53ef\u80fd\u4f1a\u5728\u6267\u884c\u7f13\u5b58I/O\u65f6\u4ea7\u751f\u6027\u80fd\u5f71\u54cd\uff0c\u56e0\u4e3a\u5237\u65b0\u64cd\u4f5c\u5f00\u9500\u5f88\u5927(\u4f8b\u5982\u5199\u5165\u6587\u4ef6)\u3002\u7531\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u6ce8\u91cd\u6027\u80fd\u7684\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u4f7f\u7528 \\n \u800c\u4e0d\u662f std::endl \u5728\u8f93\u51fa\u6d41\u4e2d\u63d2\u5165\u6362\u884c\u7b26\uff0c\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u7f13\u51b2\u533a\u5237\u65b0\u3002 \u6587\u4ef6\u6a21\u5f0f \u5982\u679c\u6211\u4eec\u8bd5\u56fe\u5411\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u6587\u4ef6\u5199\u5165\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u518d\u6b21\u8fd0\u884c\u8f93\u51fa\u793a\u4f8b\u53ef\u4ee5\u770b\u5230\uff0c\u6bcf\u6b21\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u539f\u59cb\u6587\u4ef6\u90fd\u88ab\u5b8c\u5168\u8986\u76d6\u3002\u5982\u679c\u6211\u4eec\u60f3\u5728\u6587\u4ef6\u7684\u672b\u5c3e\u8ffd\u52a0\u66f4\u591a\u7684\u6570\u636e\u5e94\u8be5\u600e\u4e48\u64cd\u4f5c\u5462\uff1f\u5b9e\u9645\u4e0a\uff0c\u6587\u4ef6\u6d41\u6784\u9020\u51fd\u6570\u63a5\u53d7\u7b2c\u4e8c\u4e2a\u53ef\u9009\u53c2\u6570\uff0c\u8be5\u53c2\u6570\u53ef\u4ee5\u6307\u5b9a\u5e94\u8be5\u5982\u4f55\u6253\u5f00\u6587\u4ef6\u3002\u8fd9\u4e2a\u53c2\u6570\u88ab\u79f0\u4e3a\u6a21\u5f0f(mode)\uff0c\u5b83\u63a5\u53d7\u7684\u6709\u6548\u6807\u5fd7\u5b58\u5728\u4e8e ios \u7c7b\u4e2d\u3002 Ios file mode Meaning app \u4f7f\u7528\u8ffd\u52a0\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6 ate \u5728\u8bfb\u5199\u524d\u79fb\u52a8\u5230\u6587\u4ef6\u672b\u5c3e binary \u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6(\u800c\u975e\u6587\u672c\u6a21\u5f0f) in \u4ee5\u8bfb\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6( ifstream \u7684\u9ed8\u8ba4\u6a21\u5f0f) out \u4ee5\u5199\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6( ofstream \u7684\u9ed8\u8ba4\u6a21\u5f0f) trunc \u5982\u679c\u6587\u4ef6\u5b58\u5728\u5219\u6e05\u6389\u5176\u5185\u5bb9 \u4f7f\u7528 \u6309\u4f4d\u6216 \u8fd0\u7b97\u7b26\u53ef\u4ee5\u540c\u65f6\u8bbe\u7f6e\u8bfb\u4e2a\u6807\u8bb0\u3002 ifstream \u9ed8\u8ba4\u4f7f\u7528\u7684\u662f std::ios::in \u6587\u4ef6\u6a21\u5f0f\u3002\u800c ofstream \u9ed8\u8ba4\u4f7f\u7528\u4e86 std::ios::out \u6587\u4ef6\u6a21\u5f0f\u3002 fstream \u9ed8\u8ba4\u4f7f\u7528\u4e86 std::ios::in | std::ios::out \u6587\u4ef6\u6a21\u5f0f\uff0c\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u5bf9\u8be5\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u3002 \u5c0f\u8d34\u58eb \u5982\u679c\u4f7f\u7528\u4e86 std::ios::in \u4f46\u6587\u4ef6\u5e76\u4e0d\u5b58\u5728\uff0c\u5219 fstream \u4f1a\u5931\u8d25\u3002\u6240\u4ee5\u5982\u679c\u4f60\u5e0c\u671b\u4f7f\u7528 fstream \u521b\u5efa\u4ee5\u65b0\u6587\u4ef6\uff0c\u8bf7\u786e\u4fdd\u53ea\u4f7f\u7528 std::ios::out \u6a21\u5f0f\u3002 \u8ba9\u6211\u4eec\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5411\u4e4b\u524d\u521b\u5efa\u7684 Sample.txt \u6587\u4ef6\u8ffd\u52a0\u4e24\u884c\u5185\u5bb9: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include int main () { // \u4f7f\u7528 ios:app \u6807\u8bb0\u544a\u77e5 ofstream \u8ffd\u52a0\u5185\u5bb9\u800c\u4e0d\u662f\u8986\u5199 // \u6211\u4eec\u4e0d\u9700\u8981\u6dfb\u52a0 std::ios::out // \u56e0\u4e3a ofstream \u9ed8\u8ba4\u662f std::ios::out std :: ofstream outf { \"Sample.txt\" , std :: ios :: app }; // \u5982\u679c\u4e0d\u80fd\u6253\u5f00\u6587\u4ef6 if ( ! outf ) { // \u6253\u5370\u9519\u8bef\u5e76\u9000\u51fa std :: cerr << \"Uh oh, Sample.txt could not be opened for writing! \\n \" ; return 1 ; } outf << \"This is line 3 \\n \" ; outf << \"This is line 4 \\n \" ; return 0 ; // \u5f53 inf \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0cifstream \u7684\u6790\u6784\u51fd\u6570\u4f1a\u8d1f\u8d23\u5173\u95ed\u6587\u4ef6 } \u73b0\u5728\uff0c\u5982\u679c\u6211\u4eec\u770b\u4e00\u4e0b Sample .txt (\u4f7f\u7528\u4e0a\u9762\u7684\u4e00\u4e2a\u793a\u4f8b\u7a0b\u5e8f\u6253\u5370\u5b83\u7684\u5185\u5bb9\uff0c\u6216\u5728\u6587\u672c\u7f16\u8f91\u5668\u4e2d\u52a0\u8f7d\u5b83)\uff0c\u53ef\u4ee5\u770b\u5230\u4ee5\u4e0b\u5185\u5bb9: 1 2 3 4 This is line 1 This is line 2 This is line 3 This is line 4 \u4f7f\u7528 open() \u6253\u5f00\u6587\u4ef6 \u5c31\u50cf\u53ef\u4ee5\u4f7f\u7528 close() \u663e\u5f0f\u5730\u5173\u95ed\u6587\u4ef6\u6d41\u4e00\u6837\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 open() \u663e\u5f0f\u5730\u6253\u5f00\u6587\u4ef6\u6d41\u3002 open() \u7684\u5de5\u4f5c\u539f\u7406\u4e0e\u6587\u4ef6\u6d41\u6784\u9020\u51fd\u6570\u7c7b\u4f3c\u2014\u2014\u5b83\u63a5\u53d7\u4e00\u4e2a\u6587\u4ef6\u540d\u548c\u4e00\u4e2a\u53ef\u9009\u7684\u6587\u4ef6\u6a21\u5f0f\u3002 \u4f8b\u5982: 1 2 3 4 5 6 7 8 9 std :: ofstream outf { \"Sample.txt\" }; outf << \"This is line 1 \\n \" ; outf << \"This is line 2 \\n \" outf . close (); // explicitly close the file // Oops, we forgot something outf . open ( \"Sample.txt\" , std :: ios :: app ); outf << \"This is line 3 \\n \" ; outf . close (); \u66f4\u591a\u5173\u4e8e open() \u51fd\u6570\u7684\u4fe1\u606f\u53ef\u4ee5\u5728 \u8fd9\u91cc \u627e\u5230\u3002","title":"23.6 - \u57fa\u672c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa"},{"location":"23-6-basic-file-IO/#_1","text":"\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86 ofstream \u7c7b\u8fdb\u884c\u6587\u4ef6\u8f93\u51fa\u3002\u975e\u5e38\u7b80\u5355\u76f4\u63a5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include int main () { // ofstream \u7528\u4e8e\u5199\u6587\u4ef6 // \u521b\u5efa\u4e00\u4e2a\u540d\u4e3a Sample.txt \u7684\u6587\u4ef6 std :: ofstream outf { \"Sample.txt\" }; // \u5982\u679c\u65e0\u6cd5\u6253\u5f00\u8f93\u51fa\u6587\u4ef6\u6d41 if ( ! outf ) { // \u6253\u5370\u9519\u8bef\u5e76\u9000\u51fa std :: cerr << \"Uh oh, Sample.txt could not be opened for writing! \\n \" ; return 1 ; } // \u5411\u6587\u4ef6\u5199\u5165\u4e24\u884c\u6587\u5b57 outf << \"This is line 1 \\n \" ; outf << \"This is line 2 \\n \" ; return 0 ; // outf \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0cofstream\u6790\u6784\u51fd\u6570\u4f1a\u5173\u95ed\u6587\u4ef6 } \u6b64\u65f6\u6253\u5f00\u9879\u76ee\u76ee\u5f55\uff0c\u5e94\u8be5\u4f1a\u770b\u5230\u4e00\u4e2a\u540d\u4e3a Sample.txt \u7684\u6587\u4ef6\u3002\u5982\u679c\u7528\u6587\u672c\u7f16\u8f91\u5668\u6253\u5f00\u5b83\uff0c\u53ef\u4ee5\u770b\u5230\u5b83\u786e\u5b9e\u5305\u542b\u4e86\u521a\u624d\u5199\u5165\u6587\u4ef6\u7684\u4e24\u884c\u6587\u672c\u3002 \u6ce8\u610f\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 put() \u51fd\u6570\u5411\u6587\u4ef6\u5199\u5165\u5355\u4e2a\u5b57\u7b26\u3002","title":"\u6587\u4ef6\u8f93\u51fa"},{"location":"23-6-basic-file-IO/#_2","text":"\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f1a\u6253\u5f00\u4e0a\u4e00\u4e2a\u4f8b\u5b50\u4e2d\u65b0\u521b\u5efa\u7684\u6587\u4ef6\u5e76\u8bfb\u53d6\u5176\u5185\u5bb9\u3002\u6ce8\u610f\uff0c\u5728\u5230\u8fbe\u6587\u4ef6\u672b\u5c3e\uff08EOF\uff09\u65f6\uff0c ifstream \u4f1a\u8fd4\u56de0\u3002\u6211\u4eec\u9700\u8981\u57fa\u4e8e\u8fd9\u4e2a\u7279\u6027\u6765\u5224\u65ad\u8bfb\u53d6\u591a\u5c11\u5185\u5bb9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include #include int main () { // ifstream \u7528\u4e8e\u8bfb\u53d6\u6587\u4ef6 // \u4ece Sample.txt \u8bfb\u53d6\u5185\u5bb9 std :: ifstream inf { \"Sample.txt\" }; // \u5982\u679c\u65e0\u6cd5\u6253\u5f00\u8f93\u5165\u6587\u4ef6\u6d41 if ( ! inf ) { // \u6253\u5370\u9519\u8bef\u5e76\u9000\u51fa std :: cerr << \"Uh oh, Sample.txt could not be opened for reading! \\n \" ; return 1 ; } // \u5982\u679c\u4ecd\u7136\u6709\u5185\u5bb9\u53ef\u4ee5\u8bfb\u53d6 while ( inf ) { // \u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5b57\u7b26\u4e32\u5e76\u6253\u5370 std :: string strInput ; inf >> strInput ; std :: cout << strInput << '\\n' ; } return 0 ; // \u5f53 inf \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0cifstream \u7684\u6790\u6784\u51fd\u6570\u4f1a\u8d1f\u8d23\u5173\u95ed\u6587\u4ef6 } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 5 6 7 8 This is line 1 This is line 2 \u989d\u3002\u3002\u8fd9\u597d\u50cf\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c\u3002\u8fd8\u8bb0\u5f97\u5417\uff1f \u63d0\u53d6\u8fd0\u7b97\u7b26 \u4f1a\u88ab\u7a7a\u683c\u5206\u5272\uff0c\u6240\u4ee5\u4e3a\u4e86\u8bfb\u53d6\u4e00\u6574\u884c\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528 getline() \u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include #include #include int main () { // ifstream \u7528\u4e8e\u8bfb\u53d6\u6587\u4ef6 // \u4ece Sample.txt \u8bfb\u53d6\u5185\u5bb9 std :: ifstream inf { \"Sample.txt\" }; // \u5982\u679c\u65e0\u6cd5\u6253\u5f00\u8f93\u5165\u6587\u4ef6\u6d41 if ( ! inf ) { // \u6253\u5370\u9519\u8bef\u5e76\u9000\u51fa std :: cerr << \"Uh oh, Sample.txt could not be opened for reading! \\n \" ; return 1 ; } // \u5982\u679c\u4ecd\u7136\u6709\u5185\u5bb9\u53ef\u4ee5\u8bfb\u53d6 while ( inf ) { // \u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5b57\u7b26\u4e32\u5e76\u6253\u5370 std :: string strInput ; std :: getline ( inf , strInput ); //\u4f7f\u7528 getline \u8bfb\u53d6\u4e00\u6574\u884c std :: cout << strInput << '\\n' ; } return 0 ; // \u5f53 inf \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0cifstream \u7684\u6790\u6784\u51fd\u6570\u4f1a\u8d1f\u8d23\u5173\u95ed\u6587\u4ef6 } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 This is line 1 This is line 2","title":"\u6587\u4ef6\u8f93\u5165"},{"location":"23-6-basic-file-IO/#_3","text":"C++\u4e2d\u7684\u8f93\u51fa\u53ef\u4ee5\u88ab\u7f13\u51b2\u3002\u8fd9\u610f\u5473\u7740\u8f93\u51fa\u5230\u6587\u4ef6\u6d41\u7684\u4efb\u4f55\u5185\u5bb9\u90fd\u4e0d\u4f1a\u7acb\u5373\u5199\u5165\u78c1\u76d8\u3002\u8fd9\u6837\u4e00\u6765\u5c31\u53ef\u4ee5\u5c06\u51e0\u4e2a\u8f93\u51fa\u64cd\u4f5c\u5408\u5e76\u5904\u7406\u3002\u8fd9\u6837\u505a\u4e3b\u8981\u662f\u51fa\u4e8e\u6027\u80fd\u539f\u56e0\uff0c\u5f53\u7f13\u51b2\u533a\u88ab\u5199\u5165\u78c1\u76d8\u65f6\uff0c\u8fd9\u88ab\u79f0\u4e3a \u5237\u65b0\u7f13\u51b2\u533a \u3002 \u89e6\u53d1\u7f13\u51b2\u533a\u5237\u65b0\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u5173\u95ed\u6587\u4ef6\u2014\u2014\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u5c06\u88ab\u5237\u65b0\u5230\u78c1\u76d8\uff0c\u7136\u540e\u5173\u95ed\u6587\u4ef6\u3002 \u7f13\u51b2\u901a\u5e38\u4e0d\u662f\u95ee\u9898\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5b83\u53ef\u80fd\u4f1a\u7ed9\u7c97\u5fc3\u7684\u4eba\u5e26\u6765\u9ebb\u70e6\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e3b\u8981\u7684\u7f6a\u9b41\u7978\u9996\u662f\u7f13\u51b2\u533a\u4e2d\u6709\u6570\u636e\uff0c\u7136\u540e\u7a0b\u5e8f\u7acb\u5373\u7ec8\u6b62(\u901a\u8fc7\u5d29\u6e83\u6216\u8c03\u7528 exit() )\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e0d\u4f1a\u6267\u884c\u6587\u4ef6\u6d41\u7c7b\u7684\u6790\u6784\u51fd\u6570\uff0c\u8fd9\u610f\u5473\u7740\u6587\u4ef6\u6c38\u8fdc\u4e0d\u4f1a\u5173\u95ed\uff0c\u6240\u4ee5\u7f13\u51b2\u533a\u6c38\u8fdc\u4e0d\u4f1a\u5237\u65b0\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f13\u51b2\u533a\u4e2d\u7684\u6570\u636e\u5c06\u4e0d\u4f1a\u88ab\u5199\u5165\u78c1\u76d8\uff0c\u5e76\u4e14\u6c38\u8fdc\u4e22\u5931\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u5728\u8c03\u7528 exit() \u4e4b\u524d\u663e\u5f0f\u5173\u95ed\u6240\u6709\u6253\u5f00\u7684\u6587\u4ef6\u603b\u662f\u4e00\u4e2a\u597d\u4e3b\u610f \u7f13\u51b2\u533a\u53ef\u4ee5\u901a\u8fc7 ostream::flush() \u51fd\u6570\u624b\u52a8\u5237\u65b0\uff0c\u6216\u8005\u5411\u8f93\u51fa\u6d41\u53d1\u9001 std::flush \u4e5f\u53ef\u4ee5\u3002\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u53ef\u4ee5\u7528\u4e8e\u786e\u4fdd\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u7acb\u5373\u5199\u5165\u78c1\u76d8\uff0c\u4ee5\u9632\u7a0b\u5e8f\u7a81\u7136\u5d29\u6e83\u3002 \u53e6\u5916\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c std::endl; \u4f1a\u5237\u65b0\u8f93\u51fa\u6d41\u3002\u56e0\u6b64\uff0c\u8fc7\u5ea6\u4f7f\u7528 std::endl (\u5bfc\u81f4\u4e0d\u5fc5\u8981\u7684\u7f13\u51b2\u533a\u5237\u65b0)\u53ef\u80fd\u4f1a\u5728\u6267\u884c\u7f13\u5b58I/O\u65f6\u4ea7\u751f\u6027\u80fd\u5f71\u54cd\uff0c\u56e0\u4e3a\u5237\u65b0\u64cd\u4f5c\u5f00\u9500\u5f88\u5927(\u4f8b\u5982\u5199\u5165\u6587\u4ef6)\u3002\u7531\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u6ce8\u91cd\u6027\u80fd\u7684\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u4f7f\u7528 \\n \u800c\u4e0d\u662f std::endl \u5728\u8f93\u51fa\u6d41\u4e2d\u63d2\u5165\u6362\u884c\u7b26\uff0c\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u7f13\u51b2\u533a\u5237\u65b0\u3002","title":"\u5e26\u7f13\u51b2\u7684\u8f93\u51fa"},{"location":"23-6-basic-file-IO/#_4","text":"\u5982\u679c\u6211\u4eec\u8bd5\u56fe\u5411\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u6587\u4ef6\u5199\u5165\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u518d\u6b21\u8fd0\u884c\u8f93\u51fa\u793a\u4f8b\u53ef\u4ee5\u770b\u5230\uff0c\u6bcf\u6b21\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u539f\u59cb\u6587\u4ef6\u90fd\u88ab\u5b8c\u5168\u8986\u76d6\u3002\u5982\u679c\u6211\u4eec\u60f3\u5728\u6587\u4ef6\u7684\u672b\u5c3e\u8ffd\u52a0\u66f4\u591a\u7684\u6570\u636e\u5e94\u8be5\u600e\u4e48\u64cd\u4f5c\u5462\uff1f\u5b9e\u9645\u4e0a\uff0c\u6587\u4ef6\u6d41\u6784\u9020\u51fd\u6570\u63a5\u53d7\u7b2c\u4e8c\u4e2a\u53ef\u9009\u53c2\u6570\uff0c\u8be5\u53c2\u6570\u53ef\u4ee5\u6307\u5b9a\u5e94\u8be5\u5982\u4f55\u6253\u5f00\u6587\u4ef6\u3002\u8fd9\u4e2a\u53c2\u6570\u88ab\u79f0\u4e3a\u6a21\u5f0f(mode)\uff0c\u5b83\u63a5\u53d7\u7684\u6709\u6548\u6807\u5fd7\u5b58\u5728\u4e8e ios \u7c7b\u4e2d\u3002 Ios file mode Meaning app \u4f7f\u7528\u8ffd\u52a0\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6 ate \u5728\u8bfb\u5199\u524d\u79fb\u52a8\u5230\u6587\u4ef6\u672b\u5c3e binary \u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6(\u800c\u975e\u6587\u672c\u6a21\u5f0f) in \u4ee5\u8bfb\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6( ifstream \u7684\u9ed8\u8ba4\u6a21\u5f0f) out \u4ee5\u5199\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6( ofstream \u7684\u9ed8\u8ba4\u6a21\u5f0f) trunc \u5982\u679c\u6587\u4ef6\u5b58\u5728\u5219\u6e05\u6389\u5176\u5185\u5bb9 \u4f7f\u7528 \u6309\u4f4d\u6216 \u8fd0\u7b97\u7b26\u53ef\u4ee5\u540c\u65f6\u8bbe\u7f6e\u8bfb\u4e2a\u6807\u8bb0\u3002 ifstream \u9ed8\u8ba4\u4f7f\u7528\u7684\u662f std::ios::in \u6587\u4ef6\u6a21\u5f0f\u3002\u800c ofstream \u9ed8\u8ba4\u4f7f\u7528\u4e86 std::ios::out \u6587\u4ef6\u6a21\u5f0f\u3002 fstream \u9ed8\u8ba4\u4f7f\u7528\u4e86 std::ios::in | std::ios::out \u6587\u4ef6\u6a21\u5f0f\uff0c\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u5bf9\u8be5\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u3002 \u5c0f\u8d34\u58eb \u5982\u679c\u4f7f\u7528\u4e86 std::ios::in \u4f46\u6587\u4ef6\u5e76\u4e0d\u5b58\u5728\uff0c\u5219 fstream \u4f1a\u5931\u8d25\u3002\u6240\u4ee5\u5982\u679c\u4f60\u5e0c\u671b\u4f7f\u7528 fstream \u521b\u5efa\u4ee5\u65b0\u6587\u4ef6\uff0c\u8bf7\u786e\u4fdd\u53ea\u4f7f\u7528 std::ios::out \u6a21\u5f0f\u3002 \u8ba9\u6211\u4eec\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5411\u4e4b\u524d\u521b\u5efa\u7684 Sample.txt \u6587\u4ef6\u8ffd\u52a0\u4e24\u884c\u5185\u5bb9: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include int main () { // \u4f7f\u7528 ios:app \u6807\u8bb0\u544a\u77e5 ofstream \u8ffd\u52a0\u5185\u5bb9\u800c\u4e0d\u662f\u8986\u5199 // \u6211\u4eec\u4e0d\u9700\u8981\u6dfb\u52a0 std::ios::out // \u56e0\u4e3a ofstream \u9ed8\u8ba4\u662f std::ios::out std :: ofstream outf { \"Sample.txt\" , std :: ios :: app }; // \u5982\u679c\u4e0d\u80fd\u6253\u5f00\u6587\u4ef6 if ( ! outf ) { // \u6253\u5370\u9519\u8bef\u5e76\u9000\u51fa std :: cerr << \"Uh oh, Sample.txt could not be opened for writing! \\n \" ; return 1 ; } outf << \"This is line 3 \\n \" ; outf << \"This is line 4 \\n \" ; return 0 ; // \u5f53 inf \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0cifstream \u7684\u6790\u6784\u51fd\u6570\u4f1a\u8d1f\u8d23\u5173\u95ed\u6587\u4ef6 } \u73b0\u5728\uff0c\u5982\u679c\u6211\u4eec\u770b\u4e00\u4e0b Sample .txt (\u4f7f\u7528\u4e0a\u9762\u7684\u4e00\u4e2a\u793a\u4f8b\u7a0b\u5e8f\u6253\u5370\u5b83\u7684\u5185\u5bb9\uff0c\u6216\u5728\u6587\u672c\u7f16\u8f91\u5668\u4e2d\u52a0\u8f7d\u5b83)\uff0c\u53ef\u4ee5\u770b\u5230\u4ee5\u4e0b\u5185\u5bb9: 1 2 3 4 This is line 1 This is line 2 This is line 3 This is line 4","title":"\u6587\u4ef6\u6a21\u5f0f"},{"location":"23-6-basic-file-IO/#open","text":"\u5c31\u50cf\u53ef\u4ee5\u4f7f\u7528 close() \u663e\u5f0f\u5730\u5173\u95ed\u6587\u4ef6\u6d41\u4e00\u6837\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 open() \u663e\u5f0f\u5730\u6253\u5f00\u6587\u4ef6\u6d41\u3002 open() \u7684\u5de5\u4f5c\u539f\u7406\u4e0e\u6587\u4ef6\u6d41\u6784\u9020\u51fd\u6570\u7c7b\u4f3c\u2014\u2014\u5b83\u63a5\u53d7\u4e00\u4e2a\u6587\u4ef6\u540d\u548c\u4e00\u4e2a\u53ef\u9009\u7684\u6587\u4ef6\u6a21\u5f0f\u3002 \u4f8b\u5982: 1 2 3 4 5 6 7 8 9 std :: ofstream outf { \"Sample.txt\" }; outf << \"This is line 1 \\n \" ; outf << \"This is line 2 \\n \" outf . close (); // explicitly close the file // Oops, we forgot something outf . open ( \"Sample.txt\" , std :: ios :: app ); outf << \"This is line 3 \\n \" ; outf . close (); \u66f4\u591a\u5173\u4e8e open() \u51fd\u6570\u7684\u4fe1\u606f\u53ef\u4ee5\u5728 \u8fd9\u91cc \u627e\u5230\u3002","title":"\u4f7f\u7528 open()\u6253\u5f00\u6587\u4ef6"},{"location":"23-7-random-file-IO/","tags":["io"],"text":"Key Takeaway \u6587\u4ef6\u6307\u9488 \u6bcf\u4e2a\u6587\u4ef6\u6d41\u7c7b\u90fd\u5305\u542b\u4e00\u4e2a\u6587\u4ef6\u6307\u9488\uff0c\u7528\u4e8e\u8ddf\u8e2a\u6587\u4ef6\u5185\u7684\u5f53\u524d\u8bfb/\u5199\u4f4d\u7f6e\u3002\u5f53\u5bf9\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u65f6\uff0c\u8bfb/\u5199\u64cd\u4f5c\u53d1\u751f\u5728\u6587\u4ef6\u6307\u9488\u7684\u5f53\u524d\u4f4d\u7f6e\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5f53\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u65f6\uff0c\u6587\u4ef6\u6307\u9488\u88ab\u8bbe\u7f6e\u4e3a\u6587\u4ef6\u7684\u5f00\u5934\u3002\u4f46\u662f\uff0c\u5982\u679c\u4ee5\u8ffd\u52a0\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u5219\u6587\u4ef6\u6307\u9488\u5c06\u79fb\u52a8\u5230\u6587\u4ef6\u7684\u672b\u5c3e\uff0c\u56e0\u6b64\u5199\u5165\u64cd\u4f5c\u4e0d\u4f1a\u8986\u76d6\u6587\u4ef6\u7684\u4efb\u4f55\u5f53\u524d\u5185\u5bb9\u3002 \u4f7f\u7528 seekg() \u548c seekp() \u8fdb\u884c\u968f\u673a\u6587\u4ef6\u8bbf\u95ee \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u6240\u5b8c\u6210\u7684\u6587\u4ef6\u8bbf\u95ee\u90fd\u662f\u987a\u5e8f\u7684\u2014\u2014\u5373\u6309\u987a\u5e8f\u8bfb\u53d6\u6216\u5199\u5165\u4e86\u6587\u4ef6\u5185\u5bb9\u3002\u4f46\u662f\uff0c\u5176\u5b9e\u4e5f\u53ef\u4ee5\u8fdb\u884c\u968f\u673a\u6587\u4ef6\u8bbf\u95ee\u2014\u2014\u5373\u8df3\u5230\u6587\u4ef6\u4e2d\u7684\u67d0\u4e2a\u4f4d\u7f6e\u518d\u8bfb\u53d6\u5176\u5185\u5bb9\u3002\u5f53\u4f60\u5e0c\u671b\u4ece\u5305\u542b\u5927\u91cf\u8bb0\u5f55\u7684\u6587\u4ef6\u4e2d\u68c0\u7d22\u7279\u5b9a\u7684\u8bb0\u5f55\u65f6\uff0c\u8fd9\u662f\u5f88\u6709\u7528\u7684\u3002\u56e0\u4e3a\u4f60\u53ef\u4ee5\u76f4\u63a5\u8df3\u5230\u60f3\u8981\u68c0\u7d22\u7684\u8bb0\u5f55\uff0c\u800c\u4e0d\u5fc5\u8bfb\u53d6\u6240\u6709\u7684\u8bb0\u5f55\u5e76\u4ece\u4e2d\u627e\u5230\u4f60\u60f3\u8981\u7684\u8bb0\u5f55\u3002 \u901a\u8fc7\u4f7f\u7528 seekg() \u51fd\u6570(\u7528\u4e8e\u8f93\u5165)\u548c seekp() \u51fd\u6570(\u7528\u4e8e\u8f93\u51fa)\u64cd\u4f5c\u6587\u4ef6\u6307\u9488\u53ef\u4ee5\u5b9e\u73b0\u6587\u4ef6\u968f\u673a\u8bbf\u95ee\u3002\u5982\u679c\u4f60\u60f3\u77e5\u9053\uff0c g \u4ee3\u8868\"get\" \uff0c p \u4ee3\u8868\"put\"\u3002\u5bf9\u4e8e\u67d0\u4e9b\u7c7b\u578b\u7684\u6d41\uff0c seekg() (\u6539\u53d8\u8bfb\u4f4d\u7f6e)\u548c seekp() (\u6539\u53d8\u5199\u4f4d\u7f6e)\u72ec\u7acb\u64cd\u4f5c\u2014\u2014\u7136\u800c\uff0c\u5bf9\u4e8e\u6587\u4ef6\u6d41\uff0c\u8bfb\u548c\u5199\u4f4d\u7f6e\u603b\u662f\u76f8\u540c\u7684\uff0c\u56e0\u6b64 seekg \u548c seekp \u53ef\u4ee5\u4e92\u6362\u4f7f\u7528\u3002 seekg() \u548c seekp() \u51fd\u6570\u6709\u4e24\u4e2a \u5f62\u53c2 \u3002\u7b2c\u4e00\u4e2a\u5f62\u53c2\u662f\u6587\u4ef6\u6307\u9488\u9700\u8981\u7f16\u8bd1\u7684\u5b57\u8282\u6570\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5219\u662f ios \u6807\u8bb0\uff0c\u7528\u4e8e\u6307\u5b9a\u4ece\u54ea\u91cc\u504f\u79fb\uff08\u504f\u79fb\u57fa\u51c6\u70b9\uff09\u3002 ios \u641c\u7d22\u6807\u8bb0 \u542b\u4e49 beg \u76f8\u5bf9\u4e8e\u6587\u4ef6\u5f00\u5934\u8fdb\u884c\u504f\u79fb (\u9ed8\u8ba4) cur \u76f8\u5bf9\u4e8e\u5f53\u524d\u4f4d\u7f6e\u8fdb\u884c\u504f\u79fb end \u76f8\u5bf9\u4e8e\u6587\u4ef6\u7ed3\u5c3e\u8fdb\u884c\u504f\u79fb \u6b63\u504f\u79fb\u91cf\u610f\u5473\u7740\u5c06\u6587\u4ef6\u6307\u9488\u5411\u6587\u4ef6\u7684\u672b\u5c3e\u79fb\u52a8\uff0c\u800c\u8d1f\u504f\u79fb\u91cf\u610f\u5473\u7740\u5c06\u6587\u4ef6\u6307\u9488\u5411\u6587\u4ef6\u5f00\u5934\u79fb\u52a8\u3002 \u4e0b\u9762\u662f\u4e00\u4e9b\u4f8b\u5b50\uff1a 1 2 3 4 5 inf . seekg ( 14 , std :: ios :: cur ); // \u5411\u524d\u79fb\u52a814\u5b57\u8282 inf . seekg ( -18 , std :: ios :: cur ); // \u5411\u540e\u79fb\u52a818\u5b57\u8282 inf . seekg ( 22 , std :: ios :: beg ); // \u79fb\u52a8\u5230\u6587\u4ef6\u7684\u7b2c22\u5b57\u8282 inf . seekg ( 24 ); // \u79fb\u52a8\u5230\u6587\u4ef6\u7684\u7b2c22\u5b57\u8282 inf . seekg ( -28 , std :: ios :: end ); // \u79fb\u52a8\u5230\u6587\u4ef6\u7ed3\u675f\u524d\u7684\u7b2c28\u5b57\u8282 \u79fb\u52a8\u5230\u6587\u4ef6\u7684\u5f00\u5934\u6216\u7ed3\u5c3e\u5f88\u5bb9\u6613\uff1a 1 2 inf . seekg ( 0 , std :: ios :: beg ); // \u79fb\u52a8\u5230\u6587\u4ef6\u7684\u5f00\u5934 inf . seekg ( 0 , std :: ios :: end ); // \u79fb\u52a8\u5230\u6587\u4ef6\u7684\u7ed3\u5c3e \u4f7f\u7528 seekg() \u548c\u4e0a\u4e00\u8bfe\u4e2d\u7684\u521b\u5efa\u7684\u8f93\u5165\u6587\u4ef6\u505a\u4e00\u4e2a\u4f8b\u5b50\u3002\u8f93\u5165\u6587\u4ef6\u7684\u5185\u5bb9\u5982\u4e0b\uff1a 1 2 3 4 This is line 1 This is line 2 This is line 3 This is line 4 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include #include #include int main () { std :: ifstream inf { \"Sample.txt\" }; // If we couldn't open the input file stream for reading if ( ! inf ) { // Print an error and exit std :: cerr << \"Uh oh, Sample.txt could not be opened for reading! \\n \" ; return 1 ; } std :: string strData ; inf . seekg ( 5 ); // move to 5th character // Get the rest of the line and print it, moving to line 2 std :: getline ( inf , strData ); std :: cout << strData << '\\n' ; inf . seekg ( 8 , std :: ios :: cur ); // move 8 more bytes into file // Get rest of the line and print it std :: getline ( inf , strData ); std :: cout << strData << '\\n' ; inf . seekg ( -14 , std :: ios :: end ); // move 14 bytes before end of file // Get rest of the line and print it std :: getline ( inf , strData ); std :: cout << strData << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 is line 1 line 2 This is line 4 \u6ce8\u610f\uff1a\u5f53\u4e0e\u6587\u672c\u6587\u4ef6\u4e00\u8d77\u4f7f\u7528\u65f6\uff0c\u4e00\u4e9b\u7f16\u8bd1\u5668\u5bf9 seekg() \u548c seekp() \u7684\u5b9e\u73b0\u6709bug(\u7531\u4e8e\u7f13\u51b2\u7684\u5173\u7cfb)\u3002\u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u662f\u5176\u4e2d\u4e4b\u4e00(\u5982\u679c\u4f60\u7684\u8f93\u51fa\u7ed3\u679c\u548c\u4e0a\u9762\u4e0d\u540c\uff0c\u5219\u8bf4\u660e\u6709\u6b64\u7c7b\u95ee\u9898)\uff0c\u6b64\u65f6\u4f60\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6: 1 std :: ifstream inf ( \"Sample.txt\" , std :: ifstream :: binary ); \u53e6\u5916\u4e24\u4e2a\u6709\u7528\u7684\u51fd\u6570\u662f tellg() \u548c tellp() \uff0c\u5b83\u4eec\u8fd4\u56de\u6587\u4ef6\u6307\u9488\u7684\u7edd\u5bf9\u4f4d\u7f6e\u3002\u8fd9\u53ef\u4ee5\u7528\u6765\u786e\u5b9a\u6587\u4ef6\u7684\u5927\u5c0f: 1 2 3 std :: ifstream inf ( \"Sample.txt\" ); inf . seekg ( 0 , std :: ios :: end ); // move to end of file std :: cout << inf . tellg (); \u6253\u5370\u7ed3\u679c\uff1a 1 64 \u8fd9\u5c31\u662f Sample.txt \u7684\u5b57\u8282\u957f\u5ea6(\u5047\u8bbe\u5728\u6700\u540e\u4e00\u884c\u4e4b\u540e\u6709\u4e00\u4e2a\u56de\u8f66)\u3002 \u4f7f\u7528 fstream \u540c\u65f6\u8fdb\u884c\u6587\u4ef6\u7684\u8bfb\u5199 fstream \u7c7b\u53ef\u4ee5\u540c\u65f6\u8bfb\u5199\u6587\u4ef6\uff01\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b83\u4e0d\u80fd\u5728\u8bfb\u53d6\u548c\u5199\u5165\u4e4b\u95f4\u968f\u610f\u5207\u6362\u3002\u4e00\u65e6\u8fdb\u884c\u4e86\u8bfb\u6216\u5199\u64cd\u4f5c\uff0c\u5728\u4e24\u8005\u4e4b\u95f4\u5207\u6362\u7684\u552f\u4e00\u65b9\u6cd5\u662f\u6267\u884c\u4fee\u6539\u6587\u4ef6\u6307\u9488\u4f4d\u7f6e\u7684\u64cd\u4f5c(\u4f8b\u5982 seek )\u3002\u5982\u679c\u4f60\u4e0d\u60f3\u79fb\u52a8\u6587\u4ef6\u6307\u9488(\u56e0\u4e3a\u5b83\u5df2\u7ecf\u5728\u9700\u8981\u7684\u4f4d\u7f6e\u4e86)\uff0c\u4f60\u53ef\u4ee5\u5c06\u6307\u9488\u8c03\u6574\u5230\u5f53\u524d\u4f4d\u7f6e\uff1a 1 2 // assume iofile is an object of type fstream iofile . seekg ( iofile . tellg (), std :: ios :: beg ); // \u6307\u9488\u79fb\u52a8\u5230\u5f53\u524d\u4f4d\u7f6e \u5982\u679c\u4f60\u4e0d\u8fd9\u6837\u505a\uff0c\u4efb\u4f55\u5947\u602a\u7684\u4e8b\u60c5\u90fd\u53ef\u80fd\u53d1\u751f\u3002 (\u6ce8\u610f\uff1a\u5c3d\u7ba1\u770b\u8d77\u6765 iofile.seekg(0, std::ios::cur) \u4e5f\u80fd\u8d77\u5230\u76f8\u4f3c\u7684\u4f5c\u7528\u3002\u4f46\u662f\u5b9e\u9645\u4e0a\u6709\u4e9b\u7f16\u8bd1\u5668\u4f1a\u5c06\u5176\u4f18\u5316\u6389)\u3002 \u8fd8\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u548c ifstream \u4e0d\u540c\uff0c fstream \u4e0d\u80fd\u901a\u8fc7 while (inf) \u6765\u5224\u65ad\u662f\u5426\u8fbe\u5230\u6587\u4ef6\u672b\u5c3e\u3002 \u63a5\u4e0b\u6765\u4f7f\u7528 fstream \u6f14\u793a\u4e00\u4e0b\u6587\u4ef6\u8f93\u5165\u8f93\u51fa\u3002\u4e0b\u9762\u7a0b\u5e8f\u4f1a\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u8bfb\u53d6\u5176\u5185\u5bb9\uff0c\u7136\u540e\u5c06\u5176\u4e2d\u6240\u6709\u7684\u5143\u97f3\u5b57\u6bcd\u66ff\u6362\u4e3a\u2018 # \u2019\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 #include #include #include int main () { // Note we have to specify both in and out because we're using fstream std :: fstream iofile { \"Sample.txt\" , std :: ios :: in | std :: ios :: out }; // If we couldn't open iofile, print an error if ( ! iofile ) { // Print an error and exit std :: cerr << \"Uh oh, Sample.txt could not be opened! \\n \" ; return 1 ; } char chChar {}; // we're going to do this character by character // While there's still data to process while ( iofile . get ( chChar )) { switch ( chChar ) { // If we find a vowel case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : case 'A' : case 'E' : case 'I' : case 'O' : case 'U' : // Back up one character iofile . seekg ( -1 , std :: ios :: cur ); // Because we did a seek, we can now safely do a write, so // let's write a # over the vowel iofile << '#' ; // Now we want to go back to read mode so the next call // to get() will perform correctly. We'll seekg() to the current // location because we don't want to move the file pointer. iofile . seekg ( iofile . tellg (), std :: ios :: beg ); std :: cout << iofile . rdbuf (); break ; } } return 0 ; } \u8fd0\u884c\u7a0b\u5e8f\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 Th#s #s l#n# 1 Th#s #s l#n# 2 Th#s #s l#n# 3 Th#s #s l#n# 4 \u5176\u4ed6\u6709\u7528\u7684\u6587\u4ef6\u51fd\u6570 \u8981\u5220\u9664\u6587\u4ef6\uff0c\u53ea\u9700\u4f7f\u7528 remove() \u51fd\u6570\u3002 \u6b64\u5916\uff0c\u5982\u679c\u6d41\u662f\u6253\u5f00\u72b6\u6001\uff0c is_open() \u51fd\u6570\u4f1a\u8fd4\u56de true \uff0c\u5426\u5219\u8fd4\u56de false \u3002 \u5173\u4e8e\u5c06\u6307\u9488\u5199\u5165\u786c\u76d8\u7684\u8b66\u544a\u26a0\ufe0f \u867d\u7136\u5c06\u53d8\u91cf\u5199\u5230\u6587\u4ef6\u662f\u5f88\u5bb9\u6613\u505a\u5230\u7684\uff0c\u4f46\u5728\u5904\u7406\u6307\u9488\u65f6\uff0c\u60c5\u51b5\u5c31\u53d8\u5f97\u66f4\u52a0\u590d\u6742\u4e86\u3002\u8bb0\u4f4f\uff0c\u6307\u9488\u53ea\u662f\u4fdd\u5b58\u5b83\u6240\u6307\u5411\u7684\u53d8\u91cf\u7684\u5730\u5740\u3002\u5c3d\u7ba1\u53ef\u4ee5\u5c06\u5730\u5740\u8bfb\u5199\u5230\u78c1\u76d8\uff0c\u4f46\u8fd9\u6837\u505a\u662f\u975e\u5e38\u5371\u9669\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u53d8\u91cf\u7684\u5730\u5740\u5728\u6bcf\u6b21\u6267\u884c\u65f6\u53ef\u80fd\u4e0d\u540c\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u5f53\u4f60\u5c06\u8be5\u5730\u5740\u5199\u5165\u78c1\u76d8\u65f6\uff0c\u53d8\u91cf\u53ef\u80fd\u4f4d\u4e8e\u5730\u5740 0x0012FF7C \uff0c\u4f46\u5f53\u4f60\u518d\u6b21\u8bfb\u53d6\u8be5\u5730\u5740\u65f6\uff0c\u5b83\u53ef\u80fd\u4e0d\u518d\u4f4d\u4e8e\u8be5\u5730\u5740\u4e86\uff01 \u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3a nValue \u7684\u6574\u6570\uff0c\u4f4d\u4e8e\u5730\u5740 0x0012FF7C \u3002\u4f60\u7ed9 nValue \u8d4b\u503c5\u3002\u540c\u65f6\uff0c\u58f0\u660e\u4e86\u4e00\u4e2a\u540d\u4e3a *pnValue \u7684\u6307\u9488\uff0c \u5b83\u6307\u5411nValue \u3002 pnValue \u4fdd\u5b58 nValue \u7684\u5730\u5740 0x0012FF7C \u3002\u6b64\u65f6\uff0c\u4f60\u5e0c\u671b\u5c06\u8fd9\u4e9b\u53d8\u91cf\u4fdd\u5b58\u5230\u6587\u4ef6\u4ee5\u5907\u5c06\u6765\u4f7f\u7528\uff0c\u56e0\u6b64\u4f60\u5c06\u503c5\u548c\u5730\u5740 0x0012FF7C \u5199\u5165\u78c1\u76d8\u3002 \u51e0\u5468\u540e\uff0c\u518d\u6b21\u8fd0\u884c\u8be5\u7a0b\u5e8f\u5e76\u4ece\u78c1\u76d8\u8bfb\u53d6\u8fd9\u4e9b\u503c\u3002\u5c06\u503c5\u8bfb\u5165\u53e6\u4e00\u4e2a\u540d\u4e3a nValue \u7684\u53d8\u91cf\uff0c\u8be5\u53d8\u91cf\u4f4d\u4e8e 0x0012FF78 \u3002\u4f60\u5c06\u5730\u5740 0x0012FF7C \u8bfb\u5165\u540d\u4e3a *pnValue \u7684\u6307\u9488\u4e2d\u3002\u56e0\u4e3a pnValue \u73b0\u5728\u6307\u5411 0x0012FF7C \uff0c\u800c nValue \u4f4d\u4e8e 0x0012FF78 \uff0c pnValue \u4e0d\u518d\u6307\u5411 nValue \uff0c\u8bd5\u56fe\u8bbf\u95ee pnValue \u5c06\u4f1a\u5e26\u6765\u9ebb\u70e6\u3002 \u6ce8\u610f \u4e0d\u8981\u5411\u6587\u4ef6\u5199\u5165\u5185\u5b58\u5730\u5740\u3002\u5f53\u4f60\u4ece\u78c1\u76d8\u8bfb\u56de\u8fd9\u4e9b\u503c\u65f6\uff0c\u6700\u521d\u4f4d\u4e8e\u8fd9\u4e9b\u5730\u5740\u7684\u53d8\u91cf\u53ef\u80fd\u4f4d\u4e8e\u4e0d\u540c\u7684\u5730\u5740\uff0c\u8fd9\u4e9b\u5730\u5740\u662f\u65e0\u6548\u7684\u3002","title":"23.7 - \u968f\u673a\u6587\u4ef6\u8f93\u5165\u8f93\u51fa"},{"location":"23-7-random-file-IO/#_1","text":"\u6bcf\u4e2a\u6587\u4ef6\u6d41\u7c7b\u90fd\u5305\u542b\u4e00\u4e2a\u6587\u4ef6\u6307\u9488\uff0c\u7528\u4e8e\u8ddf\u8e2a\u6587\u4ef6\u5185\u7684\u5f53\u524d\u8bfb/\u5199\u4f4d\u7f6e\u3002\u5f53\u5bf9\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u65f6\uff0c\u8bfb/\u5199\u64cd\u4f5c\u53d1\u751f\u5728\u6587\u4ef6\u6307\u9488\u7684\u5f53\u524d\u4f4d\u7f6e\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5f53\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u65f6\uff0c\u6587\u4ef6\u6307\u9488\u88ab\u8bbe\u7f6e\u4e3a\u6587\u4ef6\u7684\u5f00\u5934\u3002\u4f46\u662f\uff0c\u5982\u679c\u4ee5\u8ffd\u52a0\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u5219\u6587\u4ef6\u6307\u9488\u5c06\u79fb\u52a8\u5230\u6587\u4ef6\u7684\u672b\u5c3e\uff0c\u56e0\u6b64\u5199\u5165\u64cd\u4f5c\u4e0d\u4f1a\u8986\u76d6\u6587\u4ef6\u7684\u4efb\u4f55\u5f53\u524d\u5185\u5bb9\u3002","title":"\u6587\u4ef6\u6307\u9488"},{"location":"23-7-random-file-IO/#seekg-seekp","text":"\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u6240\u5b8c\u6210\u7684\u6587\u4ef6\u8bbf\u95ee\u90fd\u662f\u987a\u5e8f\u7684\u2014\u2014\u5373\u6309\u987a\u5e8f\u8bfb\u53d6\u6216\u5199\u5165\u4e86\u6587\u4ef6\u5185\u5bb9\u3002\u4f46\u662f\uff0c\u5176\u5b9e\u4e5f\u53ef\u4ee5\u8fdb\u884c\u968f\u673a\u6587\u4ef6\u8bbf\u95ee\u2014\u2014\u5373\u8df3\u5230\u6587\u4ef6\u4e2d\u7684\u67d0\u4e2a\u4f4d\u7f6e\u518d\u8bfb\u53d6\u5176\u5185\u5bb9\u3002\u5f53\u4f60\u5e0c\u671b\u4ece\u5305\u542b\u5927\u91cf\u8bb0\u5f55\u7684\u6587\u4ef6\u4e2d\u68c0\u7d22\u7279\u5b9a\u7684\u8bb0\u5f55\u65f6\uff0c\u8fd9\u662f\u5f88\u6709\u7528\u7684\u3002\u56e0\u4e3a\u4f60\u53ef\u4ee5\u76f4\u63a5\u8df3\u5230\u60f3\u8981\u68c0\u7d22\u7684\u8bb0\u5f55\uff0c\u800c\u4e0d\u5fc5\u8bfb\u53d6\u6240\u6709\u7684\u8bb0\u5f55\u5e76\u4ece\u4e2d\u627e\u5230\u4f60\u60f3\u8981\u7684\u8bb0\u5f55\u3002 \u901a\u8fc7\u4f7f\u7528 seekg() \u51fd\u6570(\u7528\u4e8e\u8f93\u5165)\u548c seekp() \u51fd\u6570(\u7528\u4e8e\u8f93\u51fa)\u64cd\u4f5c\u6587\u4ef6\u6307\u9488\u53ef\u4ee5\u5b9e\u73b0\u6587\u4ef6\u968f\u673a\u8bbf\u95ee\u3002\u5982\u679c\u4f60\u60f3\u77e5\u9053\uff0c g \u4ee3\u8868\"get\" \uff0c p \u4ee3\u8868\"put\"\u3002\u5bf9\u4e8e\u67d0\u4e9b\u7c7b\u578b\u7684\u6d41\uff0c seekg() (\u6539\u53d8\u8bfb\u4f4d\u7f6e)\u548c seekp() (\u6539\u53d8\u5199\u4f4d\u7f6e)\u72ec\u7acb\u64cd\u4f5c\u2014\u2014\u7136\u800c\uff0c\u5bf9\u4e8e\u6587\u4ef6\u6d41\uff0c\u8bfb\u548c\u5199\u4f4d\u7f6e\u603b\u662f\u76f8\u540c\u7684\uff0c\u56e0\u6b64 seekg \u548c seekp \u53ef\u4ee5\u4e92\u6362\u4f7f\u7528\u3002 seekg() \u548c seekp() \u51fd\u6570\u6709\u4e24\u4e2a \u5f62\u53c2 \u3002\u7b2c\u4e00\u4e2a\u5f62\u53c2\u662f\u6587\u4ef6\u6307\u9488\u9700\u8981\u7f16\u8bd1\u7684\u5b57\u8282\u6570\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5219\u662f ios \u6807\u8bb0\uff0c\u7528\u4e8e\u6307\u5b9a\u4ece\u54ea\u91cc\u504f\u79fb\uff08\u504f\u79fb\u57fa\u51c6\u70b9\uff09\u3002 ios \u641c\u7d22\u6807\u8bb0 \u542b\u4e49 beg \u76f8\u5bf9\u4e8e\u6587\u4ef6\u5f00\u5934\u8fdb\u884c\u504f\u79fb (\u9ed8\u8ba4) cur \u76f8\u5bf9\u4e8e\u5f53\u524d\u4f4d\u7f6e\u8fdb\u884c\u504f\u79fb end \u76f8\u5bf9\u4e8e\u6587\u4ef6\u7ed3\u5c3e\u8fdb\u884c\u504f\u79fb \u6b63\u504f\u79fb\u91cf\u610f\u5473\u7740\u5c06\u6587\u4ef6\u6307\u9488\u5411\u6587\u4ef6\u7684\u672b\u5c3e\u79fb\u52a8\uff0c\u800c\u8d1f\u504f\u79fb\u91cf\u610f\u5473\u7740\u5c06\u6587\u4ef6\u6307\u9488\u5411\u6587\u4ef6\u5f00\u5934\u79fb\u52a8\u3002 \u4e0b\u9762\u662f\u4e00\u4e9b\u4f8b\u5b50\uff1a 1 2 3 4 5 inf . seekg ( 14 , std :: ios :: cur ); // \u5411\u524d\u79fb\u52a814\u5b57\u8282 inf . seekg ( -18 , std :: ios :: cur ); // \u5411\u540e\u79fb\u52a818\u5b57\u8282 inf . seekg ( 22 , std :: ios :: beg ); // \u79fb\u52a8\u5230\u6587\u4ef6\u7684\u7b2c22\u5b57\u8282 inf . seekg ( 24 ); // \u79fb\u52a8\u5230\u6587\u4ef6\u7684\u7b2c22\u5b57\u8282 inf . seekg ( -28 , std :: ios :: end ); // \u79fb\u52a8\u5230\u6587\u4ef6\u7ed3\u675f\u524d\u7684\u7b2c28\u5b57\u8282 \u79fb\u52a8\u5230\u6587\u4ef6\u7684\u5f00\u5934\u6216\u7ed3\u5c3e\u5f88\u5bb9\u6613\uff1a 1 2 inf . seekg ( 0 , std :: ios :: beg ); // \u79fb\u52a8\u5230\u6587\u4ef6\u7684\u5f00\u5934 inf . seekg ( 0 , std :: ios :: end ); // \u79fb\u52a8\u5230\u6587\u4ef6\u7684\u7ed3\u5c3e \u4f7f\u7528 seekg() \u548c\u4e0a\u4e00\u8bfe\u4e2d\u7684\u521b\u5efa\u7684\u8f93\u5165\u6587\u4ef6\u505a\u4e00\u4e2a\u4f8b\u5b50\u3002\u8f93\u5165\u6587\u4ef6\u7684\u5185\u5bb9\u5982\u4e0b\uff1a 1 2 3 4 This is line 1 This is line 2 This is line 3 This is line 4 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include #include #include int main () { std :: ifstream inf { \"Sample.txt\" }; // If we couldn't open the input file stream for reading if ( ! inf ) { // Print an error and exit std :: cerr << \"Uh oh, Sample.txt could not be opened for reading! \\n \" ; return 1 ; } std :: string strData ; inf . seekg ( 5 ); // move to 5th character // Get the rest of the line and print it, moving to line 2 std :: getline ( inf , strData ); std :: cout << strData << '\\n' ; inf . seekg ( 8 , std :: ios :: cur ); // move 8 more bytes into file // Get rest of the line and print it std :: getline ( inf , strData ); std :: cout << strData << '\\n' ; inf . seekg ( -14 , std :: ios :: end ); // move 14 bytes before end of file // Get rest of the line and print it std :: getline ( inf , strData ); std :: cout << strData << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 is line 1 line 2 This is line 4 \u6ce8\u610f\uff1a\u5f53\u4e0e\u6587\u672c\u6587\u4ef6\u4e00\u8d77\u4f7f\u7528\u65f6\uff0c\u4e00\u4e9b\u7f16\u8bd1\u5668\u5bf9 seekg() \u548c seekp() \u7684\u5b9e\u73b0\u6709bug(\u7531\u4e8e\u7f13\u51b2\u7684\u5173\u7cfb)\u3002\u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u662f\u5176\u4e2d\u4e4b\u4e00(\u5982\u679c\u4f60\u7684\u8f93\u51fa\u7ed3\u679c\u548c\u4e0a\u9762\u4e0d\u540c\uff0c\u5219\u8bf4\u660e\u6709\u6b64\u7c7b\u95ee\u9898)\uff0c\u6b64\u65f6\u4f60\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6: 1 std :: ifstream inf ( \"Sample.txt\" , std :: ifstream :: binary ); \u53e6\u5916\u4e24\u4e2a\u6709\u7528\u7684\u51fd\u6570\u662f tellg() \u548c tellp() \uff0c\u5b83\u4eec\u8fd4\u56de\u6587\u4ef6\u6307\u9488\u7684\u7edd\u5bf9\u4f4d\u7f6e\u3002\u8fd9\u53ef\u4ee5\u7528\u6765\u786e\u5b9a\u6587\u4ef6\u7684\u5927\u5c0f: 1 2 3 std :: ifstream inf ( \"Sample.txt\" ); inf . seekg ( 0 , std :: ios :: end ); // move to end of file std :: cout << inf . tellg (); \u6253\u5370\u7ed3\u679c\uff1a 1 64 \u8fd9\u5c31\u662f Sample.txt \u7684\u5b57\u8282\u957f\u5ea6(\u5047\u8bbe\u5728\u6700\u540e\u4e00\u884c\u4e4b\u540e\u6709\u4e00\u4e2a\u56de\u8f66)\u3002","title":"\u4f7f\u7528 seekg() \u548c seekp() \u8fdb\u884c\u968f\u673a\u6587\u4ef6\u8bbf\u95ee"},{"location":"23-7-random-file-IO/#fstream","text":"fstream \u7c7b\u53ef\u4ee5\u540c\u65f6\u8bfb\u5199\u6587\u4ef6\uff01\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b83\u4e0d\u80fd\u5728\u8bfb\u53d6\u548c\u5199\u5165\u4e4b\u95f4\u968f\u610f\u5207\u6362\u3002\u4e00\u65e6\u8fdb\u884c\u4e86\u8bfb\u6216\u5199\u64cd\u4f5c\uff0c\u5728\u4e24\u8005\u4e4b\u95f4\u5207\u6362\u7684\u552f\u4e00\u65b9\u6cd5\u662f\u6267\u884c\u4fee\u6539\u6587\u4ef6\u6307\u9488\u4f4d\u7f6e\u7684\u64cd\u4f5c(\u4f8b\u5982 seek )\u3002\u5982\u679c\u4f60\u4e0d\u60f3\u79fb\u52a8\u6587\u4ef6\u6307\u9488(\u56e0\u4e3a\u5b83\u5df2\u7ecf\u5728\u9700\u8981\u7684\u4f4d\u7f6e\u4e86)\uff0c\u4f60\u53ef\u4ee5\u5c06\u6307\u9488\u8c03\u6574\u5230\u5f53\u524d\u4f4d\u7f6e\uff1a 1 2 // assume iofile is an object of type fstream iofile . seekg ( iofile . tellg (), std :: ios :: beg ); // \u6307\u9488\u79fb\u52a8\u5230\u5f53\u524d\u4f4d\u7f6e \u5982\u679c\u4f60\u4e0d\u8fd9\u6837\u505a\uff0c\u4efb\u4f55\u5947\u602a\u7684\u4e8b\u60c5\u90fd\u53ef\u80fd\u53d1\u751f\u3002 (\u6ce8\u610f\uff1a\u5c3d\u7ba1\u770b\u8d77\u6765 iofile.seekg(0, std::ios::cur) \u4e5f\u80fd\u8d77\u5230\u76f8\u4f3c\u7684\u4f5c\u7528\u3002\u4f46\u662f\u5b9e\u9645\u4e0a\u6709\u4e9b\u7f16\u8bd1\u5668\u4f1a\u5c06\u5176\u4f18\u5316\u6389)\u3002 \u8fd8\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u548c ifstream \u4e0d\u540c\uff0c fstream \u4e0d\u80fd\u901a\u8fc7 while (inf) \u6765\u5224\u65ad\u662f\u5426\u8fbe\u5230\u6587\u4ef6\u672b\u5c3e\u3002 \u63a5\u4e0b\u6765\u4f7f\u7528 fstream \u6f14\u793a\u4e00\u4e0b\u6587\u4ef6\u8f93\u5165\u8f93\u51fa\u3002\u4e0b\u9762\u7a0b\u5e8f\u4f1a\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u8bfb\u53d6\u5176\u5185\u5bb9\uff0c\u7136\u540e\u5c06\u5176\u4e2d\u6240\u6709\u7684\u5143\u97f3\u5b57\u6bcd\u66ff\u6362\u4e3a\u2018 # \u2019\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 #include #include #include int main () { // Note we have to specify both in and out because we're using fstream std :: fstream iofile { \"Sample.txt\" , std :: ios :: in | std :: ios :: out }; // If we couldn't open iofile, print an error if ( ! iofile ) { // Print an error and exit std :: cerr << \"Uh oh, Sample.txt could not be opened! \\n \" ; return 1 ; } char chChar {}; // we're going to do this character by character // While there's still data to process while ( iofile . get ( chChar )) { switch ( chChar ) { // If we find a vowel case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : case 'A' : case 'E' : case 'I' : case 'O' : case 'U' : // Back up one character iofile . seekg ( -1 , std :: ios :: cur ); // Because we did a seek, we can now safely do a write, so // let's write a # over the vowel iofile << '#' ; // Now we want to go back to read mode so the next call // to get() will perform correctly. We'll seekg() to the current // location because we don't want to move the file pointer. iofile . seekg ( iofile . tellg (), std :: ios :: beg ); std :: cout << iofile . rdbuf (); break ; } } return 0 ; } \u8fd0\u884c\u7a0b\u5e8f\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 Th#s #s l#n# 1 Th#s #s l#n# 2 Th#s #s l#n# 3 Th#s #s l#n# 4","title":"\u4f7f\u7528fstream\u540c\u65f6\u8fdb\u884c\u6587\u4ef6\u7684\u8bfb\u5199"},{"location":"23-7-random-file-IO/#_2","text":"\u8981\u5220\u9664\u6587\u4ef6\uff0c\u53ea\u9700\u4f7f\u7528 remove() \u51fd\u6570\u3002 \u6b64\u5916\uff0c\u5982\u679c\u6d41\u662f\u6253\u5f00\u72b6\u6001\uff0c is_open() \u51fd\u6570\u4f1a\u8fd4\u56de true \uff0c\u5426\u5219\u8fd4\u56de false \u3002","title":"\u5176\u4ed6\u6709\u7528\u7684\u6587\u4ef6\u51fd\u6570"},{"location":"23-7-random-file-IO/#_3","text":"\u867d\u7136\u5c06\u53d8\u91cf\u5199\u5230\u6587\u4ef6\u662f\u5f88\u5bb9\u6613\u505a\u5230\u7684\uff0c\u4f46\u5728\u5904\u7406\u6307\u9488\u65f6\uff0c\u60c5\u51b5\u5c31\u53d8\u5f97\u66f4\u52a0\u590d\u6742\u4e86\u3002\u8bb0\u4f4f\uff0c\u6307\u9488\u53ea\u662f\u4fdd\u5b58\u5b83\u6240\u6307\u5411\u7684\u53d8\u91cf\u7684\u5730\u5740\u3002\u5c3d\u7ba1\u53ef\u4ee5\u5c06\u5730\u5740\u8bfb\u5199\u5230\u78c1\u76d8\uff0c\u4f46\u8fd9\u6837\u505a\u662f\u975e\u5e38\u5371\u9669\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u53d8\u91cf\u7684\u5730\u5740\u5728\u6bcf\u6b21\u6267\u884c\u65f6\u53ef\u80fd\u4e0d\u540c\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u5f53\u4f60\u5c06\u8be5\u5730\u5740\u5199\u5165\u78c1\u76d8\u65f6\uff0c\u53d8\u91cf\u53ef\u80fd\u4f4d\u4e8e\u5730\u5740 0x0012FF7C \uff0c\u4f46\u5f53\u4f60\u518d\u6b21\u8bfb\u53d6\u8be5\u5730\u5740\u65f6\uff0c\u5b83\u53ef\u80fd\u4e0d\u518d\u4f4d\u4e8e\u8be5\u5730\u5740\u4e86\uff01 \u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3a nValue \u7684\u6574\u6570\uff0c\u4f4d\u4e8e\u5730\u5740 0x0012FF7C \u3002\u4f60\u7ed9 nValue \u8d4b\u503c5\u3002\u540c\u65f6\uff0c\u58f0\u660e\u4e86\u4e00\u4e2a\u540d\u4e3a *pnValue \u7684\u6307\u9488\uff0c \u5b83\u6307\u5411nValue \u3002 pnValue \u4fdd\u5b58 nValue \u7684\u5730\u5740 0x0012FF7C \u3002\u6b64\u65f6\uff0c\u4f60\u5e0c\u671b\u5c06\u8fd9\u4e9b\u53d8\u91cf\u4fdd\u5b58\u5230\u6587\u4ef6\u4ee5\u5907\u5c06\u6765\u4f7f\u7528\uff0c\u56e0\u6b64\u4f60\u5c06\u503c5\u548c\u5730\u5740 0x0012FF7C \u5199\u5165\u78c1\u76d8\u3002 \u51e0\u5468\u540e\uff0c\u518d\u6b21\u8fd0\u884c\u8be5\u7a0b\u5e8f\u5e76\u4ece\u78c1\u76d8\u8bfb\u53d6\u8fd9\u4e9b\u503c\u3002\u5c06\u503c5\u8bfb\u5165\u53e6\u4e00\u4e2a\u540d\u4e3a nValue \u7684\u53d8\u91cf\uff0c\u8be5\u53d8\u91cf\u4f4d\u4e8e 0x0012FF78 \u3002\u4f60\u5c06\u5730\u5740 0x0012FF7C \u8bfb\u5165\u540d\u4e3a *pnValue \u7684\u6307\u9488\u4e2d\u3002\u56e0\u4e3a pnValue \u73b0\u5728\u6307\u5411 0x0012FF7C \uff0c\u800c nValue \u4f4d\u4e8e 0x0012FF78 \uff0c pnValue \u4e0d\u518d\u6307\u5411 nValue \uff0c\u8bd5\u56fe\u8bbf\u95ee pnValue \u5c06\u4f1a\u5e26\u6765\u9ebb\u70e6\u3002 \u6ce8\u610f \u4e0d\u8981\u5411\u6587\u4ef6\u5199\u5165\u5185\u5b58\u5730\u5740\u3002\u5f53\u4f60\u4ece\u78c1\u76d8\u8bfb\u56de\u8fd9\u4e9b\u503c\u65f6\uff0c\u6700\u521d\u4f4d\u4e8e\u8fd9\u4e9b\u5730\u5740\u7684\u53d8\u91cf\u53ef\u80fd\u4f4d\u4e8e\u4e0d\u540c\u7684\u5730\u5740\uff0c\u8fd9\u4e9b\u5730\u5740\u662f\u65e0\u6548\u7684\u3002","title":"\u5173\u4e8e\u5c06\u6307\u9488\u5199\u5165\u786c\u76d8\u7684\u8b66\u544a\u26a0\ufe0f"},{"location":"3-1-Syntax-and-semantic-errors/","tags":["Syntax"],"text":"\u8f6f\u4ef6\u9519\u8bef\u662f\u666e\u904d\u5b58\u5728\u7684\u3002\u5236\u9020\u5b83\u4eec\u5f88\u5bb9\u6613\uff0c\u4f46\u8981\u627e\u5230\u5b83\u4eec\u5374\u5f88\u96be\u3002\u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8C++\u7a0b\u5e8f\u95ee\u9898\u5b9a\u4f4d\u4e0e\u62cd\u9519\u7684\u76f8\u5173\u4e3b\u9898\uff0c\u5305\u62ec\u5b66\u4e60\u5982\u4f55\u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\uff0c\u5b83\u662fIDE\u7684\u4e00\u90e8\u5206\u3002 \u5c3d\u7ba1\u8c03\u8bd5\u5de5\u5177\u548c\u6280\u672f\u5e76\u4e0d\u662fC++\u6807\u51c6\u7684\u4e00\u90e8\u5206\uff0c\u4f46\u5b66\u4f1a\u5728\u6240\u7f16\u5199\u7684\u7a0b\u5e8f\u4e2d\u53d1\u73b0\u5e76\u5220\u9664\u9519\u8bef\u662f\u6210\u4e3a\u4e00\u540d\u6210\u529f\u7a0b\u5e8f\u5458\u6781\u4e3a\u91cd\u8981\u7684\u4e00\u90e8\u5206\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5c06\u82b1\u4e00\u70b9\u65f6\u95f4\u8ba8\u8bba\u8fd9\u4e9b\u4e3b\u9898\uff0c\u4ee5\u4fbf\u968f\u7740\u4f60\u7f16\u5199\u7684\u7a0b\u5e8f\u53d8\u5f97\u66f4\u52a0\u590d\u6742\uff0c\u4f60\u8bca\u65ad\u548c\u7ea0\u6b63\u95ee\u9898\u7684\u80fd\u529b\u4e5f\u4f1a\u4ee5\u5339\u914d\u7684\u901f\u5ea6\u63d0\u9ad8\u3002 \u5982\u679c\u4f60\u6709\u7528\u53e6\u4e00\u79cd\u7f16\u8bd1\u7f16\u7a0b\u8bed\u8a00\u8c03\u8bd5\u7a0b\u5e8f\u7684\u7ecf\u9a8c\uff0c\u90a3\u4e48\u5e94\u8be5\u5df2\u7ecf\u719f\u6089\u5176\u4e2d\u7684\u5927\u90e8\u5206\u5185\u5bb9\u3002 \u8bed\u6cd5\u9519\u8bef\u548c\u8bed\u4e49\u9519\u8bef \u7f16\u7a0b\u5e76\u4e0d\u7b80\u5355\uff0c\u66f4\u4f55\u51b5 C++\u662f\u4e00\u79cd\u6709\u70b9\u53e4\u602a\u7684\u8bed\u8a00\u3002\u628a\u8fd9\u4e24\u8005\u653e\u5728\u4e00\u8d77\uff0c\u5c31\u4f1a\u6709\u5f88\u591a\u51fa\u9519\u7684\u65b9\u5f0f\u3002\u9519\u8bef\u901a\u5e38\u5206\u4e3a\u4e24\u7c7b\uff1a\u8bed\u6cd5\u9519\u8bef\u548c\u8bed\u4e49\u9519\u8bef(\u903b\u8f91\u9519\u8bef)\u3002 \u5f53\u7f16\u5199\u7684\u8bed\u53e5\u5c5e\u4e8eC++\u65e0\u6548\u65e0\u6548\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f \u8bed\u6cd5\u9519\u8bef \u3002\u8fd9\u5305\u62ec\u7f3a\u5c11\u5206\u53f7\u3001\u4f7f\u7528\u672a\u58f0\u660e\u7684\u53d8\u91cf\u3001\u62ec\u53f7\u6216\u5927\u62ec\u53f7\u4e0d\u5339\u914d\u7b49\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u7a0b\u5e8f\u5305\u542b\u76f8\u5f53\u591a\u7684\u8bed\u6cd5\u9519\u8bef\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout < \"Hi there\" ; << x ; // invalid operator (<), extraneous semicolon, undeclared variable (x) return 0 // missing semicolon at end of statement } \u5e78\u8fd0\u7684\u662f\uff0c\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u6355\u83b7\u8bed\u6cd5\u9519\u8bef\u5e76\u751f\u6210\u8b66\u544a\u6216\u9519\u8bef\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u8bc6\u522b\u548c\u4fee\u590d\u8fd9\u4e9b\u95ee\u9898\u3002\u7136\u540e\u5c31\u662f\u91cd\u65b0\u7f16\u8bd1\u76f4\u5230\u6d88\u9664\u6240\u6709\u9519\u8bef\u7684\u95ee\u9898\u4e86\u3002 \u4e00\u65e6\u4f60\u7684\u7a0b\u5e8f\u7f16\u8bd1\u6b63\u786e\uff0c\u8ba9\u5b83\u5b9e\u9645\u4ea7\u751f\u4f60\u60f3\u8981\u7684\u7ed3\u679c\u53ef\u80fd\u662f\u68d8\u624b\u7684\u3002\u5f53\u8bed\u53e5\u5728\u8bed\u6cd5\u4e0a\u662f\u6709\u6548\u7684\uff0c\u4f46\u5e76\u6ca1\u6709\u6309\u7167\u7a0b\u5e8f\u5458\u7684\u610f\u56fe\u6267\u884c\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f \u8bed\u4e49\u9519\u8bef \u3002 \u6709\u65f6\u8fd9\u5c06\u5bfc\u81f4\u4f60\u7684\u7a0b\u5e8f\u5d29\u6e83\uff0c\u4f8b\u5982\u96640\u7684\u60c5\u51b5\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { int a { 10 }; int b { 0 }; std :: cout << a << \" / \" << b << \" = \" << a / b ; // division by 0 is undefined return 0 ; } \u4f46\u66f4\u591a\u65f6\u5019\u8bed\u4e49\u9519\u8bef\u53ea\u4f1a\u5bfc\u81f4\u7ed3\u679c\u9519\u8bef\u6216\u884c\u4e3a\u9519\u8bef\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { int x ; std :: cout << x ; // Use of uninitialized variable leads to undefined result return 0 ; } \u6216\u8005 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int add ( int x , int y ) { return x - y ; // function is supposed to add, but it doesn't } int main () { std :: cout << add ( 5 , 3 ); // should produce 8, but produces 2 return 0 ; } \u4ea6\u6216\u8005 1 2 3 4 5 6 7 8 #include int main () { return 0 ; // function returns here std :: cout << \"Hello, world!\" ; // so this never executes } \u73b0\u4ee3\u7f16\u8bd1\u5668\u5728\u68c0\u6d4b\u67d0\u4e9b\u7c7b\u578b\u7684\u5e38\u89c1\u8bed\u4e49\u9519\u8bef(\u4f8b\u5982\u4f7f\u7528\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf)\u65b9\u9762\u505a\u5f97\u8d8a\u6765\u8d8a\u597d\u3002\u7136\u800c\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u5c06\u65e0\u6cd5\u6355\u83b7\u5927\u591a\u6570\u8fd9\u7c7b\u95ee\u9898\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u88ab\u8bbe\u8ba1\u4e3a\u5f3a\u5236\u8bed\u6cd5\uff0c\u800c\u4e0d\u662f\u5f3a\u5236\u8bed\u4e49\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u9519\u8bef\u5f88\u5bb9\u6613\u53d1\u73b0\u3002\u4f46\u662f\u5728\u5927\u591a\u6570\u91cd\u8981\u7684\u7a0b\u5e8f\u4e2d\uff0c\u901a\u8fc7\u76ee\u6d4b\u4ee3\u7801\u4e0d\u5bb9\u6613\u53d1\u73b0\u8bed\u4e49\u9519\u8bef\u3002\u8fd9\u5c31\u662f\u8c03\u8bd5\u6280\u672f\u6d3e\u4e0a\u7528\u573a\u7684\u5730\u65b9\u3002","title":"3.1 - \u8bed\u6cd5\u548c\u8bed\u4e49\u9519\u8bef"},{"location":"3-1-Syntax-and-semantic-errors/#_1","text":"\u7f16\u7a0b\u5e76\u4e0d\u7b80\u5355\uff0c\u66f4\u4f55\u51b5 C++\u662f\u4e00\u79cd\u6709\u70b9\u53e4\u602a\u7684\u8bed\u8a00\u3002\u628a\u8fd9\u4e24\u8005\u653e\u5728\u4e00\u8d77\uff0c\u5c31\u4f1a\u6709\u5f88\u591a\u51fa\u9519\u7684\u65b9\u5f0f\u3002\u9519\u8bef\u901a\u5e38\u5206\u4e3a\u4e24\u7c7b\uff1a\u8bed\u6cd5\u9519\u8bef\u548c\u8bed\u4e49\u9519\u8bef(\u903b\u8f91\u9519\u8bef)\u3002 \u5f53\u7f16\u5199\u7684\u8bed\u53e5\u5c5e\u4e8eC++\u65e0\u6548\u65e0\u6548\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f \u8bed\u6cd5\u9519\u8bef \u3002\u8fd9\u5305\u62ec\u7f3a\u5c11\u5206\u53f7\u3001\u4f7f\u7528\u672a\u58f0\u660e\u7684\u53d8\u91cf\u3001\u62ec\u53f7\u6216\u5927\u62ec\u53f7\u4e0d\u5339\u914d\u7b49\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u7a0b\u5e8f\u5305\u542b\u76f8\u5f53\u591a\u7684\u8bed\u6cd5\u9519\u8bef\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout < \"Hi there\" ; << x ; // invalid operator (<), extraneous semicolon, undeclared variable (x) return 0 // missing semicolon at end of statement } \u5e78\u8fd0\u7684\u662f\uff0c\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u6355\u83b7\u8bed\u6cd5\u9519\u8bef\u5e76\u751f\u6210\u8b66\u544a\u6216\u9519\u8bef\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u8bc6\u522b\u548c\u4fee\u590d\u8fd9\u4e9b\u95ee\u9898\u3002\u7136\u540e\u5c31\u662f\u91cd\u65b0\u7f16\u8bd1\u76f4\u5230\u6d88\u9664\u6240\u6709\u9519\u8bef\u7684\u95ee\u9898\u4e86\u3002 \u4e00\u65e6\u4f60\u7684\u7a0b\u5e8f\u7f16\u8bd1\u6b63\u786e\uff0c\u8ba9\u5b83\u5b9e\u9645\u4ea7\u751f\u4f60\u60f3\u8981\u7684\u7ed3\u679c\u53ef\u80fd\u662f\u68d8\u624b\u7684\u3002\u5f53\u8bed\u53e5\u5728\u8bed\u6cd5\u4e0a\u662f\u6709\u6548\u7684\uff0c\u4f46\u5e76\u6ca1\u6709\u6309\u7167\u7a0b\u5e8f\u5458\u7684\u610f\u56fe\u6267\u884c\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f \u8bed\u4e49\u9519\u8bef \u3002 \u6709\u65f6\u8fd9\u5c06\u5bfc\u81f4\u4f60\u7684\u7a0b\u5e8f\u5d29\u6e83\uff0c\u4f8b\u5982\u96640\u7684\u60c5\u51b5\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { int a { 10 }; int b { 0 }; std :: cout << a << \" / \" << b << \" = \" << a / b ; // division by 0 is undefined return 0 ; } \u4f46\u66f4\u591a\u65f6\u5019\u8bed\u4e49\u9519\u8bef\u53ea\u4f1a\u5bfc\u81f4\u7ed3\u679c\u9519\u8bef\u6216\u884c\u4e3a\u9519\u8bef\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { int x ; std :: cout << x ; // Use of uninitialized variable leads to undefined result return 0 ; } \u6216\u8005 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int add ( int x , int y ) { return x - y ; // function is supposed to add, but it doesn't } int main () { std :: cout << add ( 5 , 3 ); // should produce 8, but produces 2 return 0 ; } \u4ea6\u6216\u8005 1 2 3 4 5 6 7 8 #include int main () { return 0 ; // function returns here std :: cout << \"Hello, world!\" ; // so this never executes } \u73b0\u4ee3\u7f16\u8bd1\u5668\u5728\u68c0\u6d4b\u67d0\u4e9b\u7c7b\u578b\u7684\u5e38\u89c1\u8bed\u4e49\u9519\u8bef(\u4f8b\u5982\u4f7f\u7528\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf)\u65b9\u9762\u505a\u5f97\u8d8a\u6765\u8d8a\u597d\u3002\u7136\u800c\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u5c06\u65e0\u6cd5\u6355\u83b7\u5927\u591a\u6570\u8fd9\u7c7b\u95ee\u9898\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u88ab\u8bbe\u8ba1\u4e3a\u5f3a\u5236\u8bed\u6cd5\uff0c\u800c\u4e0d\u662f\u5f3a\u5236\u8bed\u4e49\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u9519\u8bef\u5f88\u5bb9\u6613\u53d1\u73b0\u3002\u4f46\u662f\u5728\u5927\u591a\u6570\u91cd\u8981\u7684\u7a0b\u5e8f\u4e2d\uff0c\u901a\u8fc7\u76ee\u6d4b\u4ee3\u7801\u4e0d\u5bb9\u6613\u53d1\u73b0\u8bed\u4e49\u9519\u8bef\u3002\u8fd9\u5c31\u662f\u8c03\u8bd5\u6280\u672f\u6d3e\u4e0a\u7528\u573a\u7684\u5730\u65b9\u3002","title":"\u8bed\u6cd5\u9519\u8bef\u548c\u8bed\u4e49\u9519\u8bef"},{"location":"3-10-Finding-issues-before-they-become-problems/","text":"When you make a semantic error, that error may or may not be immediately noticeable when you run your program. An issue may lurk undetected in your code for a long time before newly introduced code or changed circumstances cause it to manifest as a program malfunction. The longer an error sits in the code base before it is found, the more likely it is to be hard to find, and something that may have been easy to fix originally turns into a debugging adventure that eats up time and energy. So what can we do about that? \u4e0d\u8981\u72af\u9519 Well, the best thing is to not make errors in the first place. Here\u2019s an incomplete list of things that can help avoid making errors: Follow best practices Don\u2019t program when tired Understand where the common pitfalls are in a language (all those things we warn you not to do) Keep your programs simple Don\u2019t let your functions get too long Prefer using the standard library to writing your own code, when possible Comment your code liberally \u4ee3\u7801\u91cd\u6784 As you add new capabilities to your programs (\u201cbehavioral changes\u201d), you will find that some of your functions grow in length. As functions get longer, they get both more complex and harder to understand. One way to address this is to break a single long function into multiple shorter functions. This process of making structural changes to your code without changing its behavior (typically in order to make your program more organized, modular, or performant) is called refactoring. So how long is too long for a function? A function that takes up one vertical screen worth of code is generally regarded as far too long -- if you have to scroll to read the whole function, the function\u2019s comprehensibility drops significantly. But the shorter the better -- functions less than ten lines are good. Functions that are less than five lines are even better. \u5173\u952e\u4fe1\u606f When making changes to your code, make behavioral changes OR structural changes, and then retest for correctness. Making behavioral and structural changes at the same time tends to lead to more errors as well as errors that are harder to find. \u9632\u5fa1\u5f0f\u7f16\u7a0b\u7b80\u4ecb Errors can be not only of your own making (e.g. incorrect logic), but also occur when your users use the application in a way that you did not anticipate. For example, if you ask the user to enter an integer, and they enter a letter instead, how does your program behave in such a case? Unless you anticipated this, and added some error handling for this case, probably not very well. Defensive programming is a practice whereby the programmer tries to anticipate all of the ways the software could be misused, either by end-users, or by other developers (including the programmer themselves) using the code. These misuses can often be detected and then mitigated (e.g. by asking a user who entered bad input to try again). We\u2019ll explore topics related to error handling in future lessons. Finding errors fast Since not making errors is difficult in large programs, the next best thing is to catch errors you do make quickly. The best way to do this is to program a little bit at a time, and then test your code and make sure it works. However, there are a few other techniques we can also use. \u6d4b\u8bd5\u51fd\u6570\u7b80\u4ecb One common way to help uncover issues with your program is to write testing functions to \u201cexercise\u201d the code you\u2019ve written. Here\u2019s a primitive attempt, more for illustrative purposes than anything: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int add ( int x , int y ) { return x + y ; } void testadd () { std :: cout << \"This function should print: 2 0 0 -2 \\n \" ; std :: cout << add ( 1 , 1 ) << ' ' ; std :: cout << add ( -1 , 1 ) << ' ' ; std :: cout << add ( 1 , -1 ) << ' ' ; std :: cout << add ( -1 , -1 ) << ' ' ; } int main () { testadd (); return 0 ; } COPY The testadd() function tests the add() function by calling it with different values. If all the values match our expectations, then we can be reasonably confident the function works. Even better, we can keep this function around, and run it any time we change function add to ensure we haven\u2019t accidentally broken it. This is a primitive form of unit testing, which is a software testing method by which small units of source code are tested to determine whether they are correct. As with logging frameworks, there are many 3rd party unit testing frameworks that can be used. It\u2019s also possible to write your own, though we\u2019ll need more language features at our disposal to do the topic justice. We\u2019ll come back to some of this in a future lesson. An introduction to constraints Constraints-based techniques involve the addition of some extra code (that can be compiled out in a non-debug build, if desired) to check that some set of assumptions or expectations are not violated. For example, if we were writing a function to calculate the factorial of a number, which expects a non-negative argument, the function could check to make sure the caller passed in a non-negative number before proceeding. If the caller passes in a negative number, then the function could immediately error out rather than producing some indeterminate result, helping ensure the problem is caught immediately. One common method of doing this is via assert and static_assert , which we cover in lesson 7.17 -- Assert and static_assert . Shotgunning for general issues Programmers tend to make certain kinds of common mistakes, and some of those mistakes can be discovered by programs trained to look for them. These programs, generally known as static analysis tools (sometimes informally called linters ) are programs that analyze your code to identify specific semantic issues (in this context, _static_means that these tools analyze the source code). The issues found by static analysis tools may or may not be the cause of any particular problem you are having, but may help point out fragile areas of code or issues that can be problematic in certain circumstances. You already have one static analysis tool at your disposal -- your compiler! In addition to ensuring your program is syntactically correct, most modern C++ compilers will do some light static analysis to identify some common problems. For example, many compilers will warn you if you try to use a variable that has not been initialized. If you haven\u2019t already, turning up your compiler warning and error levels (see lesson 0.11 -- Configuring your compiler: Warning and error levels ) can help surface these. Many static analysis tools exist , some of which can identify over 300 types of programming errors. And on large programs, this can surface tens or hundreds of potential issues. On our small academic programs, use of a static analysis tool is optional (but may help you find areas where your code is non-compliant with best practices). On larger non-academic programs, use of a static analysis tool is highly recommended. \u6700\u4f73\u5b9e\u8df5 Use a static analysis tool on your programs to help find areas where your code is non-compliant with best practices. For Visual Studio users Visual Studio 2019 comes with a built-in static analysis tool. You can access it via Build > Run Code Analysis on Solution (Alt+F11) .","title":"3.10 - \u5728\u95ee\u9898\u53d1\u751f\u524d\u53d1\u73b0\u95ee\u9898"},{"location":"3-10-Finding-issues-before-they-become-problems/#_1","text":"Well, the best thing is to not make errors in the first place. Here\u2019s an incomplete list of things that can help avoid making errors: Follow best practices Don\u2019t program when tired Understand where the common pitfalls are in a language (all those things we warn you not to do) Keep your programs simple Don\u2019t let your functions get too long Prefer using the standard library to writing your own code, when possible Comment your code liberally","title":"\u4e0d\u8981\u72af\u9519"},{"location":"3-10-Finding-issues-before-they-become-problems/#_2","text":"As you add new capabilities to your programs (\u201cbehavioral changes\u201d), you will find that some of your functions grow in length. As functions get longer, they get both more complex and harder to understand. One way to address this is to break a single long function into multiple shorter functions. This process of making structural changes to your code without changing its behavior (typically in order to make your program more organized, modular, or performant) is called refactoring. So how long is too long for a function? A function that takes up one vertical screen worth of code is generally regarded as far too long -- if you have to scroll to read the whole function, the function\u2019s comprehensibility drops significantly. But the shorter the better -- functions less than ten lines are good. Functions that are less than five lines are even better. \u5173\u952e\u4fe1\u606f When making changes to your code, make behavioral changes OR structural changes, and then retest for correctness. Making behavioral and structural changes at the same time tends to lead to more errors as well as errors that are harder to find.","title":"\u4ee3\u7801\u91cd\u6784"},{"location":"3-10-Finding-issues-before-they-become-problems/#_3","text":"Errors can be not only of your own making (e.g. incorrect logic), but also occur when your users use the application in a way that you did not anticipate. For example, if you ask the user to enter an integer, and they enter a letter instead, how does your program behave in such a case? Unless you anticipated this, and added some error handling for this case, probably not very well. Defensive programming is a practice whereby the programmer tries to anticipate all of the ways the software could be misused, either by end-users, or by other developers (including the programmer themselves) using the code. These misuses can often be detected and then mitigated (e.g. by asking a user who entered bad input to try again). We\u2019ll explore topics related to error handling in future lessons. Finding errors fast Since not making errors is difficult in large programs, the next best thing is to catch errors you do make quickly. The best way to do this is to program a little bit at a time, and then test your code and make sure it works. However, there are a few other techniques we can also use.","title":"\u9632\u5fa1\u5f0f\u7f16\u7a0b\u7b80\u4ecb"},{"location":"3-10-Finding-issues-before-they-become-problems/#_4","text":"One common way to help uncover issues with your program is to write testing functions to \u201cexercise\u201d the code you\u2019ve written. Here\u2019s a primitive attempt, more for illustrative purposes than anything: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int add ( int x , int y ) { return x + y ; } void testadd () { std :: cout << \"This function should print: 2 0 0 -2 \\n \" ; std :: cout << add ( 1 , 1 ) << ' ' ; std :: cout << add ( -1 , 1 ) << ' ' ; std :: cout << add ( 1 , -1 ) << ' ' ; std :: cout << add ( -1 , -1 ) << ' ' ; } int main () { testadd (); return 0 ; } COPY The testadd() function tests the add() function by calling it with different values. If all the values match our expectations, then we can be reasonably confident the function works. Even better, we can keep this function around, and run it any time we change function add to ensure we haven\u2019t accidentally broken it. This is a primitive form of unit testing, which is a software testing method by which small units of source code are tested to determine whether they are correct. As with logging frameworks, there are many 3rd party unit testing frameworks that can be used. It\u2019s also possible to write your own, though we\u2019ll need more language features at our disposal to do the topic justice. We\u2019ll come back to some of this in a future lesson.","title":"\u6d4b\u8bd5\u51fd\u6570\u7b80\u4ecb"},{"location":"3-10-Finding-issues-before-they-become-problems/#an-introduction-to-constraints","text":"Constraints-based techniques involve the addition of some extra code (that can be compiled out in a non-debug build, if desired) to check that some set of assumptions or expectations are not violated. For example, if we were writing a function to calculate the factorial of a number, which expects a non-negative argument, the function could check to make sure the caller passed in a non-negative number before proceeding. If the caller passes in a negative number, then the function could immediately error out rather than producing some indeterminate result, helping ensure the problem is caught immediately. One common method of doing this is via assert and static_assert , which we cover in lesson 7.17 -- Assert and static_assert .","title":"An introduction to constraints"},{"location":"3-10-Finding-issues-before-they-become-problems/#shotgunning-for-general-issues","text":"Programmers tend to make certain kinds of common mistakes, and some of those mistakes can be discovered by programs trained to look for them. These programs, generally known as static analysis tools (sometimes informally called linters ) are programs that analyze your code to identify specific semantic issues (in this context, _static_means that these tools analyze the source code). The issues found by static analysis tools may or may not be the cause of any particular problem you are having, but may help point out fragile areas of code or issues that can be problematic in certain circumstances. You already have one static analysis tool at your disposal -- your compiler! In addition to ensuring your program is syntactically correct, most modern C++ compilers will do some light static analysis to identify some common problems. For example, many compilers will warn you if you try to use a variable that has not been initialized. If you haven\u2019t already, turning up your compiler warning and error levels (see lesson 0.11 -- Configuring your compiler: Warning and error levels ) can help surface these. Many static analysis tools exist , some of which can identify over 300 types of programming errors. And on large programs, this can surface tens or hundreds of potential issues. On our small academic programs, use of a static analysis tool is optional (but may help you find areas where your code is non-compliant with best practices). On larger non-academic programs, use of a static analysis tool is highly recommended. \u6700\u4f73\u5b9e\u8df5 Use a static analysis tool on your programs to help find areas where your code is non-compliant with best practices. For Visual Studio users Visual Studio 2019 comes with a built-in static analysis tool. You can access it via Build > Run Code Analysis on Solution (Alt+F11) .","title":"Shotgunning for general issues"},{"location":"3-2-The-debugging-process/","tags":["debugging"],"text":"\u5047\u8bbe\u4f60\u7f16\u5199\u4e86\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5b83\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u2014\u2014\u4ee3\u7801\u7f16\u8bd1\u6ca1\u95ee\u9898\uff0c\u4f46\u662f\u8fd0\u884c\u7684\u7ed3\u679c\u4e0d\u6b63\u786e\u3002\u90a3\u4e48\u4f60\u4e00\u5b9a\u5728\u4ec0\u4e48\u5730\u65b9\u5199\u4e86\u6709\u8bed\u4e49\u9519\u8bef\u7684\u4ee3\u7801\u3002\u6b64\u65f6\u5e94\u8be5\u5982\u4f55\u5b9a\u4f4d\u95ee\u9898\u7684\u539f\u56e0\u5462\uff1f\u5982\u679c\u4f60\u4e00\u76f4\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\uff0c\u7f16\u5199\u5c11\u91cf\u4ee3\u7801\u5e76\u8fdb\u884c\u6d4b\u8bd5\uff0c\u90a3\u4e48\u53ef\u80fd\u5f88\u6e05\u695a\u9519\u8bef\u4f1a\u53d1\u751f\u5728\u54ea\u91cc\uff0c\u5426\u5219\u4f60\u53ef\u80fd\u5f88\u96be\u4e00\u4e0b\u627e\u5230\u95ee\u9898\u7684\u539f\u56e0\u3002 \u6240\u6709\u7684bug\u90fd\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u524d\u63d0\u2014\u2014\u5373\u4f60\u4ee5\u4e3a\u7684\u5e76\u4e0d\u662f\u4f60\u4ee5\u4e3a\u7684\u3002\u5b9e\u9645\u4e0a\uff0c\u627e\u51fa\u9519\u8bef\u5728\u54ea\u91cc\u662f\u5f88\u6709\u6311\u6218\u6027\u7684\u3002\u5728\u8fd9\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u6982\u8ff0\u8c03\u8bd5\u7a0b\u5e8f\u7684\u4e00\u822c\u8fc7\u7a0b\u3002 \u56e0\u4e3a\u6211\u4eec\u8fd8\u6ca1\u6709\u6d89\u53ca\u5230\u90a3\u4e48\u591a\u7684C++\u4e3b\u9898\uff0c\u672c\u7ae0\u4e2d\u7684\u793a\u4f8b\u7a0b\u5e8f\u5c06\u662f\u76f8\u5f53\u57fa\u7840\u7684\u3002\u8fd9\u53ef\u80fd\u4f1a\u8ba9\u6211\u4eec\u5728\u8fd9\u91cc\u5c55\u793a\u7684\u4e00\u4e9b\u6280\u672f\u663e\u5f97\u591a\u4f59\u3002\u4f46\u662f\uff0c\u8bf7\u8bb0\u4f4f\uff0c\u8fd9\u4e9b\u6280\u672f\u662f\u4e3a\u66f4\u5927\u3001\u66f4\u590d\u6742\u7684\u7a0b\u5e8f\u800c\u8bbe\u8ba1\u7684\uff0c\u5728\u8fd9\u6837\u7684\u4ee3\u7801\u4e2d(\u8fd9\u662f\u4f60\u6700\u9700\u8981\u5b83\u4eec\u7684\u5730\u65b9)\u4f1a\u66f4\u6709\u7528\u3002 \u4ee3\u7801\u6392\u9519\u7684\u4e00\u822c\u65b9\u6cd5 \u4e00\u65e6\u786e\u5b9a\u4e86\u95ee\u9898\uff0c\u4ee3\u7801\u6392\u9519\u901a\u5e38\u5305\u62ec\u4e94\u4e2a\u6b65\u9aa4: \u627e\u51fa\u95ee\u9898\u7684\u6839\u672c\u539f\u56e0(\u901a\u5e38\u662f\u65e0\u6cd5\u5de5\u4f5c\u7684\u4ee3\u7801\u884c)\uff1b \u786e\u4fdd\u4f60\u4e86\u89e3\u95ee\u9898\u53d1\u751f\u7684\u539f\u56e0\uff1b \u51b3\u5b9a\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1b \u4fee\u590d\u5f15\u8d77\u95ee\u9898\u7684\u95ee\u9898\uff1b \u91cd\u65b0\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u95ee\u9898\u5df2\u88ab\u4fee\u590d\uff0c\u4e14\u6ca1\u6709\u51fa\u73b0\u65b0\u95ee\u9898\u3002 \u8ba9\u6211\u4eec\u6253\u4e00\u4e2a\u73b0\u5b9e\u751f\u6d3b\u7684\u6bd4\u55bb\u3002\u5047\u8bbe\u6709\u4e00\u5929\u665a\u4e0a\uff0c\u4f60\u4ece\u51b0\u7bb1\u91cc\u7684\u5236\u51b0\u673a\u91cc\u53d6\u51b0\uff0c\u628a\u676f\u5b50\u653e\u5728\u51fa\u53e3\u5904\uff0c\u6309\u4e0b\u6309\u94ae\u540e\u58f0\u660e\u90fd\u6ca1\u6709\u51fa\u6765\u3002\u54e6\u54e6\uff0c\u51fa\u95ee\u9898\u4e86\uff01\u6b64\u65f6\u4f60\u4f1a\u600e\u4e48\u505a\uff1f\u4f60\u53ef\u80fd\u4f1a\u5f00\u59cb\u68c0\u67e5\u53ca\u5176\uff0c\u770b\u770b\u662f\u5426\u53ef\u4ee5\u786e\u5b9a\u95ee\u9898\u7684\u6839\u672c\u539f\u56e0\u3002 \u627e\u5230\u95ee\u9898\u7684\u6839\u56e0 \uff1a \u65e2\u7136\u4f60\u542c\u5230\u4e86\u5236\u51b0\u673a\u8bd5\u56fe\u9001\u51b0\u7684\u58f0\u97f3\uff0c\u90a3\u5f88\u53ef\u80fd\u4e0d\u662f\u9001\u51b0\u88c5\u7f6e\u672c\u8eab\u3002\u6240\u4ee5\u4f60\u6253\u5f00\u51b0\u7bb1\uff0c\u68c0\u67e5\u51b0\u76d8\u3002\u6ca1\u6709\u51b0\u3002\u8fd9\u662f\u95ee\u9898\u7684\u6839\u672c\u539f\u56e0\u5417\uff1f\u4e0d\uff0c\u8fd9\u662f\u4e00\u79cd\u73b0\u8c61\u3002\u5728\u8fdb\u4e00\u6b65\u68c0\u67e5\u4e4b\u540e\uff0c\u4f60\u786e\u5b9a\u5236\u51b0\u673a\u4f3c\u4e4e\u6ca1\u6709\u5728\u5236\u51b0\u3002\u662f\u5236\u51b0\u673a\u51fa\u4e86\u95ee\u9898\u8fd8\u662f\u522b\u7684\u4ec0\u4e48\u95ee\u9898\uff1f\u51b0\u7bb1\u4ecd\u7136\u662f\u51b7\u7684\uff0c\u6c34\u7ba1\u6ca1\u6709\u5835\u585e\uff0c\u5176\u4ed6\u4e00\u5207\u4f3c\u4e4e\u90fd\u5728\u5de5\u4f5c\uff0c\u6240\u4ee5\u4f60\u5f97\u51fa\u7ed3\u8bba\uff0c\u6839\u672c\u539f\u56e0\u662f\u5236\u51b0\u673a\u574f\u4e86\u3002 \u7406\u89e3\u95ee\u9898 \uff1a\u5728\u672c\u4f8b\u4e2d\uff0c\u8fd9\u5f88\u7b80\u5355\u3002\u574f\u4e86\u7684\u5236\u51b0\u673a\u5236\u4e0d\u51fa\u51b0\u3002 \u786e\u5b9a\u4fee\u590d\u65b9\u6cd5 \uff1a \u6b64\u65f6\uff0c\u4f60\u6709\u51e0\u4e2a\u89e3\u51b3\u65b9\u6848\uff1a\u4e00\u662f\u53ef\u4ee5\u7ed5\u8fc7\u8fd9\u4e2a\u95ee\u9898(\u4ece\u5546\u5e97\u8d2d\u4e70\u888b\u88c5\u51b0)\u3002\u6216\u8005\u4f60\u53ef\u4ee5\u8bd5\u7740\u8fdb\u4e00\u6b65\u8bca\u65ad\u5236\u51b0\u673a\uff0c\u770b\u770b\u662f\u5426\u6709\u96f6\u4ef6\u9700\u8981\u4fee\u590d\u3002\u4e09\u662f\u4f60\u53ef\u4ee5\u4e70\u4e00\u4e2a\u65b0\u7684\u5236\u51b0\u673a\uff0c\u628a\u5b83\u5b89\u88c5\u5728\u73b0\u6709\u7684\u90a3\u4e2a\u5730\u65b9\u3002\u4f60\u51b3\u5b9a\u4e70\u4e00\u4e2a\u65b0\u7684\u5236\u51b0\u673a\u3002 \u4fee\u590d\u95ee\u9898 \uff1a\u5236\u51b0\u673a\u4e00\u5230\uff0c\u4f60\u5c31\u628a\u5b83\u88c5\u4e0a\u3002 \u6d4b\u8bd5 \uff1a \u5728\u91cd\u65b0\u63a5\u901a\u7535\u6e90\u5e76\u7b49\u5f85\u4e00\u591c\u4e4b\u540e\uff0c\u4f60\u7684\u65b0\u5236\u51b0\u673a\u5f00\u59cb\u5236\u51b0\u3002\u6ca1\u6709\u53d1\u73b0\u65b0\u7684\u95ee\u9898\u3002 \u73b0\u5728\u8ba9\u6211\u4eec\u628a\u8fd9\u4e2a\u8fc7\u7a0b\u5e94\u7528\u5230\u4e0a\u8282\u8bfe\u7684\u7b80\u5355\u7a0b\u5e8f\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include // Adds two numbers int add ( int x , int y ) { return x - y ; // function is supposed to add, but it doesn't } int main () { std :: cout << add ( 5 , 3 ) << '\\n' ; // should produce 8, but produces 2 return 0 ; } \u8fd9\u6bb5\u4ee3\u7801\u7684\u552f\u4e00\u4f18\u70b9\u5c31\u662f\u9519\u7684\u5f88\u660e\u663e\uff1a\u9519\u8bef\u7684\u7b54\u6848\u901a\u8fc7\u7b2c11\u884c\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\u3002\u8fd9\u4e3a\u6211\u4eec\u7684\u8c03\u67e5\u63d0\u4f9b\u4e86\u4e00\u4e2a\u8d77\u70b9\u3002 \u627e\u5230\u95ee\u9898\u7684\u6839\u56e0 \uff1a\u5728\u7b2c11\u884c\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4f20\u9012\u7684\u53c2\u6570(5\u548c3)\u662f\u5b57\u9762\u91cf\uff0c\u56e0\u6b64\u6ca1\u6709\u51fa\u9519\u7684\u7a7a\u95f4\u3002\u7531\u4e8e\u51fd\u6570 add \u7684\u8f93\u5165\u662f\u6b63\u786e\u7684\uff0c\u4f46\u8f93\u51fa\u662f\u9519\u8bef\u7684\uff0c\u5f88\u660e\u663e\u51fd\u6570 add \u4e00\u5b9a\u8ba1\u7b97\u51fa\u4e86\u9519\u8bef\u7684\u503c\u3002\u51fd\u6570 add \u4e2d\u552f\u4e00\u7684\u8bed\u53e5\u662f return \u8bed\u53e5\uff0c\u5b83\u4e00\u5b9a\u662f\u7f6a\u9b41\u7978\u9996\u3002\u65b9\u5411\u6ca1\u6709\u9519\uff0c\u73b0\u5728\u6211\u4eec\u77e5\u9053\u6ce8\u610f\u529b\u5e94\u8be5\u96c6\u4e2d\u5728\u54ea\u91cc\uff0c\u968f\u540e\u4fbf\u53ef\u4ee5\u53d1\u73b0\u8fd9\u91cc\u5728\u505a\u51cf\u6cd5\u800c\u4e0d\u662f\u52a0\u6cd5\uff0c\u57fa\u672c\u4e0a\u4e00\u773c\u5c31\u53ef\u4ee5\u770b\u51fa\u6765\uff01 \u7406\u89e3\u95ee\u9898 \uff1a\u5728\u8fd9\u91cc\u6307\u9488\uff0c\u5f88\u663e\u7136\u4e3a\u4ec0\u4e48\u4f1a\u5f97\u5230\u9519\u8bef\u7684\u503c\u2014\u2014\u56e0\u4e3a\u4f7f\u7528\u4e86\u9519\u8bef\u7684\u64cd\u4f5c\u7b26\u3002 \u786e\u5b9a\u4fee\u590d\u65b9\u6cd5 \uff1a\u5c06\u51cf\u53f7\u6539\u6210\u52a0\u53f7\u5373\u53ef\u3002 \u4fee\u590d\u95ee\u9898 \uff1a\u5728\u7a0b\u5e8f\u4e2d\u5c06\u51cf\u53f7\u6539\u4e3a\u52a0\u53f7\uff0c\u786e\u4fdd\u7a0b\u5e8f\u53ef\u4ee5\u7f16\u8bd1\u3002 \u6d4b\u8bd5 \uff1a\u5728\u5b9e\u73b0\u66f4\u6539\u4e4b\u540e\uff0c\u91cd\u65b0\u8fd0\u884c\u7a0b\u5e8f\uff0c\u786e\u5b9a\u5f97\u5230\u4e86\u6b63\u786e\u503c\uff1a8\u3002\u5bf9\u4e8e\u8fd9\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\uff0c\u8fd9\u5c31\u662f\u6240\u6709\u9700\u8981\u7684\u6d4b\u8bd5\u3002 \u8fd9\u4e2a\u793a\u4f8b\u5f88\u7b80\u5355\uff0c\u4f46\u8bf4\u660e\u4e86\u5bf9\u4efb\u4f55\u7a0b\u5e8f\u8fdb\u884c\u6392\u9519(debug)\u65f6\u5e94\u8be5\u7ecf\u5386\u7684\u57fa\u672c\u8fc7\u7a0b\u3002","title":"3.2 - \u4ee3\u7801\u8c03\u8bd5\u6b65\u9aa4"},{"location":"3-2-The-debugging-process/#_1","text":"\u4e00\u65e6\u786e\u5b9a\u4e86\u95ee\u9898\uff0c\u4ee3\u7801\u6392\u9519\u901a\u5e38\u5305\u62ec\u4e94\u4e2a\u6b65\u9aa4: \u627e\u51fa\u95ee\u9898\u7684\u6839\u672c\u539f\u56e0(\u901a\u5e38\u662f\u65e0\u6cd5\u5de5\u4f5c\u7684\u4ee3\u7801\u884c)\uff1b \u786e\u4fdd\u4f60\u4e86\u89e3\u95ee\u9898\u53d1\u751f\u7684\u539f\u56e0\uff1b \u51b3\u5b9a\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1b \u4fee\u590d\u5f15\u8d77\u95ee\u9898\u7684\u95ee\u9898\uff1b \u91cd\u65b0\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u95ee\u9898\u5df2\u88ab\u4fee\u590d\uff0c\u4e14\u6ca1\u6709\u51fa\u73b0\u65b0\u95ee\u9898\u3002 \u8ba9\u6211\u4eec\u6253\u4e00\u4e2a\u73b0\u5b9e\u751f\u6d3b\u7684\u6bd4\u55bb\u3002\u5047\u8bbe\u6709\u4e00\u5929\u665a\u4e0a\uff0c\u4f60\u4ece\u51b0\u7bb1\u91cc\u7684\u5236\u51b0\u673a\u91cc\u53d6\u51b0\uff0c\u628a\u676f\u5b50\u653e\u5728\u51fa\u53e3\u5904\uff0c\u6309\u4e0b\u6309\u94ae\u540e\u58f0\u660e\u90fd\u6ca1\u6709\u51fa\u6765\u3002\u54e6\u54e6\uff0c\u51fa\u95ee\u9898\u4e86\uff01\u6b64\u65f6\u4f60\u4f1a\u600e\u4e48\u505a\uff1f\u4f60\u53ef\u80fd\u4f1a\u5f00\u59cb\u68c0\u67e5\u53ca\u5176\uff0c\u770b\u770b\u662f\u5426\u53ef\u4ee5\u786e\u5b9a\u95ee\u9898\u7684\u6839\u672c\u539f\u56e0\u3002 \u627e\u5230\u95ee\u9898\u7684\u6839\u56e0 \uff1a \u65e2\u7136\u4f60\u542c\u5230\u4e86\u5236\u51b0\u673a\u8bd5\u56fe\u9001\u51b0\u7684\u58f0\u97f3\uff0c\u90a3\u5f88\u53ef\u80fd\u4e0d\u662f\u9001\u51b0\u88c5\u7f6e\u672c\u8eab\u3002\u6240\u4ee5\u4f60\u6253\u5f00\u51b0\u7bb1\uff0c\u68c0\u67e5\u51b0\u76d8\u3002\u6ca1\u6709\u51b0\u3002\u8fd9\u662f\u95ee\u9898\u7684\u6839\u672c\u539f\u56e0\u5417\uff1f\u4e0d\uff0c\u8fd9\u662f\u4e00\u79cd\u73b0\u8c61\u3002\u5728\u8fdb\u4e00\u6b65\u68c0\u67e5\u4e4b\u540e\uff0c\u4f60\u786e\u5b9a\u5236\u51b0\u673a\u4f3c\u4e4e\u6ca1\u6709\u5728\u5236\u51b0\u3002\u662f\u5236\u51b0\u673a\u51fa\u4e86\u95ee\u9898\u8fd8\u662f\u522b\u7684\u4ec0\u4e48\u95ee\u9898\uff1f\u51b0\u7bb1\u4ecd\u7136\u662f\u51b7\u7684\uff0c\u6c34\u7ba1\u6ca1\u6709\u5835\u585e\uff0c\u5176\u4ed6\u4e00\u5207\u4f3c\u4e4e\u90fd\u5728\u5de5\u4f5c\uff0c\u6240\u4ee5\u4f60\u5f97\u51fa\u7ed3\u8bba\uff0c\u6839\u672c\u539f\u56e0\u662f\u5236\u51b0\u673a\u574f\u4e86\u3002 \u7406\u89e3\u95ee\u9898 \uff1a\u5728\u672c\u4f8b\u4e2d\uff0c\u8fd9\u5f88\u7b80\u5355\u3002\u574f\u4e86\u7684\u5236\u51b0\u673a\u5236\u4e0d\u51fa\u51b0\u3002 \u786e\u5b9a\u4fee\u590d\u65b9\u6cd5 \uff1a \u6b64\u65f6\uff0c\u4f60\u6709\u51e0\u4e2a\u89e3\u51b3\u65b9\u6848\uff1a\u4e00\u662f\u53ef\u4ee5\u7ed5\u8fc7\u8fd9\u4e2a\u95ee\u9898(\u4ece\u5546\u5e97\u8d2d\u4e70\u888b\u88c5\u51b0)\u3002\u6216\u8005\u4f60\u53ef\u4ee5\u8bd5\u7740\u8fdb\u4e00\u6b65\u8bca\u65ad\u5236\u51b0\u673a\uff0c\u770b\u770b\u662f\u5426\u6709\u96f6\u4ef6\u9700\u8981\u4fee\u590d\u3002\u4e09\u662f\u4f60\u53ef\u4ee5\u4e70\u4e00\u4e2a\u65b0\u7684\u5236\u51b0\u673a\uff0c\u628a\u5b83\u5b89\u88c5\u5728\u73b0\u6709\u7684\u90a3\u4e2a\u5730\u65b9\u3002\u4f60\u51b3\u5b9a\u4e70\u4e00\u4e2a\u65b0\u7684\u5236\u51b0\u673a\u3002 \u4fee\u590d\u95ee\u9898 \uff1a\u5236\u51b0\u673a\u4e00\u5230\uff0c\u4f60\u5c31\u628a\u5b83\u88c5\u4e0a\u3002 \u6d4b\u8bd5 \uff1a \u5728\u91cd\u65b0\u63a5\u901a\u7535\u6e90\u5e76\u7b49\u5f85\u4e00\u591c\u4e4b\u540e\uff0c\u4f60\u7684\u65b0\u5236\u51b0\u673a\u5f00\u59cb\u5236\u51b0\u3002\u6ca1\u6709\u53d1\u73b0\u65b0\u7684\u95ee\u9898\u3002 \u73b0\u5728\u8ba9\u6211\u4eec\u628a\u8fd9\u4e2a\u8fc7\u7a0b\u5e94\u7528\u5230\u4e0a\u8282\u8bfe\u7684\u7b80\u5355\u7a0b\u5e8f\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include // Adds two numbers int add ( int x , int y ) { return x - y ; // function is supposed to add, but it doesn't } int main () { std :: cout << add ( 5 , 3 ) << '\\n' ; // should produce 8, but produces 2 return 0 ; } \u8fd9\u6bb5\u4ee3\u7801\u7684\u552f\u4e00\u4f18\u70b9\u5c31\u662f\u9519\u7684\u5f88\u660e\u663e\uff1a\u9519\u8bef\u7684\u7b54\u6848\u901a\u8fc7\u7b2c11\u884c\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\u3002\u8fd9\u4e3a\u6211\u4eec\u7684\u8c03\u67e5\u63d0\u4f9b\u4e86\u4e00\u4e2a\u8d77\u70b9\u3002 \u627e\u5230\u95ee\u9898\u7684\u6839\u56e0 \uff1a\u5728\u7b2c11\u884c\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4f20\u9012\u7684\u53c2\u6570(5\u548c3)\u662f\u5b57\u9762\u91cf\uff0c\u56e0\u6b64\u6ca1\u6709\u51fa\u9519\u7684\u7a7a\u95f4\u3002\u7531\u4e8e\u51fd\u6570 add \u7684\u8f93\u5165\u662f\u6b63\u786e\u7684\uff0c\u4f46\u8f93\u51fa\u662f\u9519\u8bef\u7684\uff0c\u5f88\u660e\u663e\u51fd\u6570 add \u4e00\u5b9a\u8ba1\u7b97\u51fa\u4e86\u9519\u8bef\u7684\u503c\u3002\u51fd\u6570 add \u4e2d\u552f\u4e00\u7684\u8bed\u53e5\u662f return \u8bed\u53e5\uff0c\u5b83\u4e00\u5b9a\u662f\u7f6a\u9b41\u7978\u9996\u3002\u65b9\u5411\u6ca1\u6709\u9519\uff0c\u73b0\u5728\u6211\u4eec\u77e5\u9053\u6ce8\u610f\u529b\u5e94\u8be5\u96c6\u4e2d\u5728\u54ea\u91cc\uff0c\u968f\u540e\u4fbf\u53ef\u4ee5\u53d1\u73b0\u8fd9\u91cc\u5728\u505a\u51cf\u6cd5\u800c\u4e0d\u662f\u52a0\u6cd5\uff0c\u57fa\u672c\u4e0a\u4e00\u773c\u5c31\u53ef\u4ee5\u770b\u51fa\u6765\uff01 \u7406\u89e3\u95ee\u9898 \uff1a\u5728\u8fd9\u91cc\u6307\u9488\uff0c\u5f88\u663e\u7136\u4e3a\u4ec0\u4e48\u4f1a\u5f97\u5230\u9519\u8bef\u7684\u503c\u2014\u2014\u56e0\u4e3a\u4f7f\u7528\u4e86\u9519\u8bef\u7684\u64cd\u4f5c\u7b26\u3002 \u786e\u5b9a\u4fee\u590d\u65b9\u6cd5 \uff1a\u5c06\u51cf\u53f7\u6539\u6210\u52a0\u53f7\u5373\u53ef\u3002 \u4fee\u590d\u95ee\u9898 \uff1a\u5728\u7a0b\u5e8f\u4e2d\u5c06\u51cf\u53f7\u6539\u4e3a\u52a0\u53f7\uff0c\u786e\u4fdd\u7a0b\u5e8f\u53ef\u4ee5\u7f16\u8bd1\u3002 \u6d4b\u8bd5 \uff1a\u5728\u5b9e\u73b0\u66f4\u6539\u4e4b\u540e\uff0c\u91cd\u65b0\u8fd0\u884c\u7a0b\u5e8f\uff0c\u786e\u5b9a\u5f97\u5230\u4e86\u6b63\u786e\u503c\uff1a8\u3002\u5bf9\u4e8e\u8fd9\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\uff0c\u8fd9\u5c31\u662f\u6240\u6709\u9700\u8981\u7684\u6d4b\u8bd5\u3002 \u8fd9\u4e2a\u793a\u4f8b\u5f88\u7b80\u5355\uff0c\u4f46\u8bf4\u660e\u4e86\u5bf9\u4efb\u4f55\u7a0b\u5e8f\u8fdb\u884c\u6392\u9519(debug)\u65f6\u5e94\u8be5\u7ecf\u5386\u7684\u57fa\u672c\u8fc7\u7a0b\u3002","title":"\u4ee3\u7801\u6392\u9519\u7684\u4e00\u822c\u65b9\u6cd5"},{"location":"3-3-A-strategy-for-debugging/","tags":["debugging"],"text":"\u5728\u8c03\u8bd5\u7a0b\u5e8f\u65f6\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f60\u7684\u7edd\u5927\u591a\u6570\u65f6\u95f4\u5c06\u82b1\u5728\u8bd5\u56fe\u627e\u5230\u9519\u8bef\u7684\u5b9e\u9645\u4f4d\u7f6e\u4e0a\u3002\u4e00\u65e6\u53d1\u73b0\u95ee\u9898\uff0c\u5269\u4e0b\u7684\u6b65\u9aa4(\u4fee\u590d\u95ee\u9898\u548c\u9a8c\u8bc1\u95ee\u9898\u5df2\u4fee\u590d)\u76f8\u6bd4\u8d77\u6765\u901a\u5e38\u662f\u5fae\u4e0d\u8db3\u9053\u7684\u3002 \u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u5f00\u59cb\u63a2\u7d22\u5982\u4f55\u67e5\u627e\u9519\u8bef\u3002 \u901a\u8fc7\u4ee3\u7801\u68c0\u89c6\u627e\u5230\u95ee\u9898 \u5047\u8bbe\u60a8\u6ce8\u610f\u5230\u4e00\u4e2a\u95ee\u9898\uff0c\u60a8\u60f3\u8981\u8ffd\u8e2a\u8fd9\u4e2a\u7279\u5b9a\u95ee\u9898\u7684\u539f\u56e0\u3002\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b(\u7279\u522b\u662f\u5728\u8f83\u5c0f\u7684\u7a0b\u5e8f\u4e2d)\uff0c\u6211\u4eec\u53ef\u4ee5\u5feb\u901f\u5730\u5b9a\u4f4d\u95ee\u9898\u6240\u5728\u7684\u4f4d\u7f6e\u3002 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\u7247\u6bb5\uff1a 1 2 3 4 5 6 7 8 int main () { getNames (); // ask user to enter a bunch of names sortNames (); // sort them in alphabetical order printNames (); // print the sorted list of names return 0 ; } \u5982\u679c\u4f60\u671f\u671b\u8fd9\u4e2a\u7a0b\u5e8f\u6309\u5b57\u6bcd\u987a\u5e8f\u6253\u5370\u540d\u79f0\uff0c\u4f46\u5b83\u6253\u5370\u7684\u7ed3\u679c\u5374\u662f\u76f8\u53cd\u7684\uff0c\u90a3\u4e48\u95ee\u9898\u53ef\u80fd\u51fa\u5728 sortNames \u51fd\u6570\u4e0a\u3002\u5bf9\u4e8e\u8fd9\u79cd\u53ef\u4ee5\u5c06\u95ee\u9898\u7f29\u5c0f\u5230\u7279\u5b9a\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u53ea\u9700\u67e5\u770b\u4ee3\u7801\u5c31\u53ef\u4ee5\u53d1\u73b0\u95ee\u9898\u3002 \u7136\u800c\uff0c\u968f\u7740\u7a0b\u5e8f\u53d8\u5f97\u8d8a\u6765\u8d8a\u590d\u6742\uff0c\u901a\u8fc7\u4ee3\u7801\u68c0\u67e5\u53d1\u73b0\u95ee\u9898\u4e5f\u53d8\u5f97\u8d8a\u6765\u8d8a\u590d\u6742\u3002 \u9996\u5148\uff0c\u6709\u5f88\u591a\u4ee3\u7801\u8981\u770b\u3002\u5728\u4e00\u4e2a\u6570\u5343\u884c\u957f\u7684\u7a0b\u5e8f\u4e2d\uff0c\u9010\u884c\u8d70\u8bfb\u4ee3\u7801\u4f1a\u82b1\u8d39\u5f88\u957f\u65f6\u95f4(\u800c\u4e14\u975e\u5e38\u65e0\u804a)\u3002\u5176\u6b21\uff0c\u4ee3\u7801\u672c\u8eab\u5f80\u5f80\u66f4\u590d\u6742\uff0c\u5f88\u591a\u5730\u65b9\u90fd\u53ef\u80fd\u51fa\u9519\u3002\u7b2c\u4e09\uff0c\u4ee3\u7801\u7684\u884c\u4e3a\u53ef\u80fd\u4e0d\u80fd\u63d0\u9ad8\u592a\u591a\u7ebf\u7d22\u3002\u5982\u679c\u4f60\u7f16\u5199\u4e86\u4e00\u4e2a\u8f93\u51fa\u80a1\u7968\u63a8\u8350\u7684\u7a0b\u5e8f\uff0c\u800c\u5b83\u5b9e\u9645\u4e0a\u4ec0\u4e48\u4e5f\u6ca1\u6709\u8f93\u51fa\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u80fd\u5c31\u4e0d\u77e5\u9053\u4ece\u54ea\u91cc\u5f00\u59cb\u67e5\u627e\u95ee\u9898\u4e86\u3002 \u6700\u540e\uff0c\u9519\u8bef\u53ef\u80fd\u662f\u7531\u9519\u8bef\u7684\u5047\u8bbe\u5f15\u8d77\u7684\u3002\u51e0\u4e4e\u4e0d\u53ef\u80fd\u4ece\u89c6\u89c9\u4e0a\u53d1\u73b0\u7531\u9519\u8bef\u5047\u8bbe\u5f15\u8d77\u7684\u9519\u8bef\uff0c\u56e0\u4e3a\u5728\u68c0\u67e5\u4ee3\u7801\u65f6\uff0c\u6211\u4eec\u5f80\u5f80\u503e\u5411\u4e8e\u4f1a\u505a\u51fa\u540c\u6837\u7684\u9519\u8bef\u5047\u8bbe\uff0c\u5bfc\u81f4\u6ca1\u6709\u6ce8\u610f\u5230\u9519\u8bef\u3002\u6240\u4ee5\uff0c\u5982\u679c\u4e00\u4e2a\u7a0b\u5e8f\u65e0\u6cd5\u901a\u8fc7\u4ee3\u7801\u68c0\u89c6\u53d1\u73b0\u95ee\u9898\uff0c\u90a3\u5e94\u8be5\u5982\u4f55\u5b9a\u4f4d\u5462\uff1f \u901a\u8fc7\u8fd0\u884c\u7a0b\u5e8f\u627e\u5230\u95ee\u9898 \u6240\u5e78\uff0c\u5982\u679c\u4e0d\u80fd\u901a\u8fc7\u4ee3\u7801\u68c0\u89c6\u53d1\u73b0\u95ee\u9898\uff0c\u8fd8\u53ef\u4ee5\u91c7\u7528\u53e6\u4e00\u79cd\u65b9\u6cd5\uff1a\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u89c2\u5bdf\u5b83\u7684\u884c\u4e3a\uff0c\u5e76\u5c1d\u8bd5\u4ece\u4e2d\u8bca\u65ad\u95ee\u9898\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6982\u62ec\u4e3a: \u627e\u5230\u590d\u73b0\u95ee\u9898\u7684\u6761\u4ef6\uff1b \u8fd0\u884c\u7a0b\u5e8f\uff0c\u83b7\u53d6\u4fe1\u606f\uff0c\u9010\u6b65\u903c\u8fd1\"\u771f\u50cf\"\uff1b \u91cd\u590d\u4e0a\u8ff0\u6b65\u9aa4\u76f4\u5230\u627e\u5230\u95ee\u9898\u6240\u5728\u3002 \u5728\u672c\u7ae0\u7684\u5176\u4f59\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5b9e\u65bd\u8fd9\u79cd\u65b9\u6cd5\u7684\u6280\u672f\u3002 \u590d\u73b0\u95ee\u9898 \u53d1\u73b0\u95ee\u9898\u7684\u7b2c\u4e00\u6b65\u4e5f\u662f\u6700\u91cd\u8981\u7684\u4e00\u6b65\u662f\u80fd\u591f\u91cd\u73b0\u95ee\u9898\uff0c\u539f\u56e0\u5f88\u7b80\u5355\uff1a\u5982\u679c\u4f60\u4e0d\u80fd\u518d\u6b21\u89c2\u5bdf\u5230\u95ee\u9898\u7684\u8bdd\uff0c\u8981\u5982\u4f55\u624d\u80fd\u5b9a\u4f4d\u5b83\u5462\uff1f \u56de\u5230\u5236\u51b0\u673a\u7684\u7c7b\u6bd4\u2014\u2014\u5047\u8bbe\u6709\u4e00\u5929\u4f60\u7684\u670b\u53cb\u544a\u8bc9\u4f60\uff0c\u4f60\u7684\u5236\u51b0\u673a\u574f\u4e86\u3002\u4f60\u53bb\u68c0\u67e5\u7684\u65f6\u5019\u53d1\u73b0\u5b83\u5de5\u4f5c\u5f97\u5f88\u597d\u3002\u4f60\u5e94\u8be5\u5982\u4f55\u8bca\u65ad\u8fd9\u4e2a\u95ee\u9898\uff1f\u8fd9\u4e2a\u65f6\u5019\u5c31\u6bd4\u8f83\u68d8\u624b\u4e86\u3002\u7136\u800c\uff0c\u5982\u679c\u4f60\u80fd\u591f\u518d\u6b21\u8ba9\u5236\u51b0\u673a\u8fdb\u5165\u5230\u4e0d\u80fd\u5de5\u4f5c\u7684\u72b6\u6001\uff0c\u90a3\u4e48\u624d\u53ef\u4ee5\u5f00\u59cb\u5b9a\u4f4d\u95ee\u9898\u3002 \u5982\u679c\u4e00\u4e2a\u8f6f\u4ef6bug\u5f88\u660e\u663e\u7684(\u4f8b\u5982\uff0c\u7a0b\u5e8f\u5728\u6bcf\u6b21\u8fd0\u884c\u65f6\u90fd\u5728\u76f8\u540c\u7684\u5730\u65b9\u5d29\u6e83)\uff0c\u90a3\u4e48\u91cd\u73b0\u8fd9\u4e2a\u95ee\u9898\u5c31\u76f8\u5f53\u7b80\u5355\u4e86\u3002\u7136\u800c\uff0c\u590d\u73b0\u95ee\u9898\u6709\u65f6\u5019\u662f\u5f88\u56f0\u96be\u7684\u3002\u4f8b\u5982\u8be5\u95ee\u9898\u53ef\u80fd\u53ea\u53d1\u751f\u5728\u67d0\u4e9b\u8bbe\u5907\u4e0a\uff0c\u6216\u53ea\u5728\u7279\u5b9a\u60c5\u51b5\u4e0b(\u4f8b\u5982\uff0c\u5f53\u7528\u6237\u8f93\u5165\u67d0\u4e9b\u8f93\u5165\u65f6)\u624d\u51fa\u73b0\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8bbe\u8ba1\u4e00\u7ec4\u590d\u73b0\u95ee\u9898\u7684\u6b65\u9aa4\u4f1a\u5f88\u6709\u5e2e\u52a9\u3002\u590d\u73b0\u6b65\u9aa4\u662f\u4e00\u4e2a\u6e05\u6670\u4e14\u7cbe\u786e\u7684\u5206\u6b65\u6267\u884c\u5217\u8868\uff0c\u4f60\u53ef\u4ee5\u9075\u5faa\u8fd9\u4e9b\u6b65\u9aa4\u4f7f\u95ee\u9898\u4ee5\u66f4\u5927\u7684\u6982\u7387\u518d\u6b21\u51fa\u73b0\u3002\u6211\u4eec\u7684\u76ee\u6807\u662f\u5c31\u80fd\u591f\u5c3d\u53ef\u80fd\u5730\u4f7f\u95ee\u9898\u518d\u6b21\u51fa\u73b0\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u53cd\u590d\u8fd0\u884c\u7a0b\u5e8f\uff0c\u5e76\u67e5\u627e\u7ebf\u7d22\u6765\u786e\u5b9a\u662f\u4ec0\u4e48\u539f\u56e0\u5bfc\u81f4\u4e86\u95ee\u9898\u3002\u5982\u679c\u95ee\u9898\u53ef\u4ee5100%\u7684\u91cd\u73b0\uff0c\u90a3\u81ea\u7136\u662f\u6700\u7406\u60f3\u7684\u72b6\u6001\uff0c\u4f46\u4f4e\u4e8e100%\u901a\u5e38\u4e5f\u662f\u53ef\u4ee5\u7684\u3002\u4e00\u4e2a\u53ea\u670950%\u51e0\u7387\u89e6\u53d1\u7684\u95ee\u9898\uff0c\u610f\u5473\u7740\u6211\u4eec\u9700\u8981\u82b1\u8d39\u4e24\u500d\u7684\u65f6\u95f4\u6765\u8bca\u65ad\u95ee\u9898\uff0c\u56e0\u4e3a\u6709\u4e00\u534a\u7684\u65f6\u95f4\u7a0b\u5e8f\u4e0d\u4f1a\u663e\u793a\u95ee\u9898\uff0c\u56e0\u6b64\u4e0d\u4f1a\u63d0\u4f9b\u4efb\u4f55\u6709\u7528\u7684\u8bca\u65ad\u4fe1\u606f\u3002 \u5173\u6ce8\u95ee\u9898 \u4e00\u65e6\u6211\u4eec\u80fd\u591f\u6bd4\u8f83\u7a33\u5b9a\u5730\u590d\u73b0\u95ee\u9898\uff0c\u63a5\u4e0b\u6765\u5c31\u662f\u627e\u5230\u5bfc\u81f4\u95ee\u9898\u7684\u4ee3\u7801\u3002\u6839\u636e\u95ee\u9898\u7684\u6027\u8d28\u4e0d\u540c\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u53ef\u80fd\u5f88\u7b80\u5355\uff0c\u4e5f\u53ef\u80fd\u5f88\u56f0\u96be\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u6211\u4eec\u4e0d\u592a\u6e05\u695a\u95ee\u9898\u5230\u5e95\u5728\u54ea\u91cc\u3002\u6211\u4eec\u600e\u4e48\u627e\u5230\u5b83? \u8fd9\u91cc\u53ef\u4ee5\u505a\u4e2a\u7c7b\u6bd4\u3002\u6211\u4eec\u6765\u73a9\u4e2a\u731c\u6570\u5b57\u6e38\u620f\uff0c\u4f60\u9700\u8981\u731c\u4e00\u4e2a1\u523010\u4e4b\u95f4\u7684\u6570\u5b57\uff0c\u6bcf\u731c\u4e00\u6b21\uff0c\u6211\u5c31\u4f1a\u544a\u8bc9\u4f60\u662f\u592a\u9ad8\u3001\u592a\u4f4e\u8fd8\u662f\u6b63\u786e\uff1a 1 2 3 4 5 6 7 8 You: 5 Me: Too low You: 8 Me: Too high You: 6 Me: Too low You: 7 Me: Correct \u5728\u4e0a\u9762\u7684\u6e38\u620f\u4e2d\uff0c\u4f60\u4e0d\u9700\u8981\u731c\u6bcf\u4e00\u4e2a\u6570\u5b57\u6765\u627e\u5230\u7b54\u6848\u3002\u901a\u8fc7\u731c\u6d4b\u548c\u8003\u8651\u4ece\u6bcf\u6b21\u731c\u6d4b\u4e2d\u5b66\u5230\u7684\u4fe1\u606f\u7684\u8fc7\u7a0b\uff0c\u4f60\u53ef\u80fd\u53ea\u9700\u51e0\u6b21\u731c\u6d4b\u5c31\u53ef\u4ee5\u201c\u9501\u5b9a\u201d\u6b63\u786e\u7684\u6570\u5b57(\u5982\u679c\u4f60\u4f7f\u7528\u6700\u4f18\u7b56\u7565\uff0c\u90a3\u4e48\u603b\u662f\u53ef\u4ee5\u57284\u6b21\u6216\u66f4\u5c11\u7684\u731c\u6d4b\u4e2d\u627e\u5230\u76ee\u6807\u6570\u5b57)\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u6765\u8c03\u8bd5\u7a0b\u5e8f\u3002\u5728\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u4e0d\u77e5\u9053bug\u5728\u54ea\u91cc\u3002\u7136\u800c\uff0c\u6211\u4eec\u77e5\u9053\u95ee\u9898\u4e00\u5b9a\u662f\u5728\u7a0b\u5e8f\u5f00\u59cb\u5230\u7a0b\u5e8f\u663e\u793a\u51fa\u6211\u4eec\u53ef\u4ee5\u89c2\u5bdf\u5230\u7684\u7b2c\u4e00\u4e2a\u9519\u8bef\u75c7\u72b6\u4e4b\u95f4\u6267\u884c\u7684\u4ee3\u7801\u4e2d\u7684\u67d0\u4e2a\u5730\u65b9\u3002\u8fd9\u81f3\u5c11\u6392\u9664\u4e86\u5728\u51fa\u73b0\u7b2c\u4e00\u4e2a\u53ef\u89c2\u5bdf\u5230\u7684\u75c7\u72b6\u540e\u6267\u884c\u7684\u7a0b\u5e8f\u90e8\u5206\u3002\u4f46\u8fd9\u4ecd\u7136\u53ef\u80fd\u7559\u4e0b\u5927\u91cf\u7684\u4ee3\u7801\u9700\u8981\u8986\u76d6\u3002\u4e3a\u4e86\u8bca\u65ad\u95ee\u9898\uff0c\u6211\u4eec\u5c06\u5bf9\u95ee\u9898\u7684\u4f4d\u7f6e\u8fdb\u884c\u4e00\u4e9b\u6709\u6839\u636e\u7684\u731c\u6d4b\uff0c\u76ee\u7684\u662f\u5feb\u901f\u627e\u5230\u95ee\u9898\u6240\u5728\u3002 \u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u65e0\u8bba\u662f\u4ec0\u4e48\u539f\u56e0\u5bfc\u81f4\u6211\u4eec\u6ce8\u610f\u5230\u95ee\u9898\uff0c\u90fd\u4f1a\u7ed9\u6211\u4eec\u4e00\u4e2a\u63a5\u8fd1\u5b9e\u9645\u95ee\u9898\u6240\u5728\u7684\u521d\u59cb\u731c\u6d4b\u3002\u4f8b\u5982\uff0c\u5982\u679c\u7a0b\u5e8f\u5728\u5e94\u8be5\u5199\u5165\u6570\u636e\u7684\u65f6\u5019\u6ca1\u6709\u5199\u5165\u6587\u4ef6\uff0c\u90a3\u4e48\u95ee\u9898\u53ef\u80fd\u51fa\u5728\u5904\u7406\u5199\u5165\u6587\u4ef6\u7684\u4ee3\u7801\u7684\u67d0\u4e2a\u5730\u65b9(\u5e9f\u8bdd!)\u7136\u540e\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u6e38\u620f\u7b56\u7565\u6765\u5c1d\u8bd5\u627e\u51fa\u771f\u6b63\u7684\u95ee\u9898\u6240\u5728\u3002 \u4f8b\u5982\uff1a \u5982\u679c\u5728\u7a0b\u5e8f\u7684\u67d0\u4e2a\u65f6\u523b\uff0c\u6211\u4eec\u53ef\u4ee5\u8bc1\u660e\u95ee\u9898\u8fd8\u6ca1\u6709\u53d1\u751f\uff0c\u8fd9\u5c31\u7c7b\u4f3c\u4e8e\u6536\u5230\u4e00\u4e2a\u201c\u4f4e\u4e86\u201d\u7684\u7ed3\u679c\u2014\u2014\u6211\u4eec\u77e5\u9053\u95ee\u9898\u4e00\u5b9a\u662f\u5728\u7a0b\u5e8f\u540e\u9762\u7684\u67d0\u4e2a\u5730\u65b9\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u7684\u7a0b\u5e8f\u6bcf\u6b21\u90fd\u5728\u540c\u4e00\u4e2a\u5730\u65b9\u5d29\u6e83\uff0c\u5e76\u4e14\u6211\u4eec\u53ef\u4ee5\u8bc1\u660e\u7a0b\u5e8f\u6ca1\u6709\u5728\u7a0b\u5e8f\u6267\u884c\u7684\u67d0\u4e2a\u7279\u5b9a\u70b9\u5d29\u6e83\uff0c\u90a3\u4e48\u5d29\u6e83\u4e00\u5b9a\u53d1\u751f\u5728\u4ee3\u7801\u7684\u540e\u9762\u3002 \u5982\u679c\u5728\u7a0b\u5e8f\u7684\u67d0\u4e2a\u70b9\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u89c2\u5bdf\u5230\u4e0e\u95ee\u9898\u76f8\u5173\u7684\u4e0d\u6b63\u786e\u884c\u4e3a\uff0c\u90a3\u4e48\u8fd9\u5c31\u7c7b\u4f3c\u4e8e\u63a5\u6536\u5230\u4e00\u4e2a\u201c\u9ad8\u4e86\u201d\u7684\u7ed3\u679c\uff0c\u6211\u4eec\u77e5\u9053\u95ee\u9898\u4e00\u5b9a\u662f\u5728\u7a0b\u5e8f\u7684\u65e9\u671f\u67d0\u4e2a\u5730\u65b9\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4e00\u4e2a\u7a0b\u5e8f\u8f93\u51fa\u67d0\u4e2a\u53d8\u91cf x \u7684\u503c\u3002\u4f60\u5e0c\u671b\u5b83\u8f93\u51fa 2 \uff0c\u4f46\u662f\u5b83\u8f93\u51fa\u4e868\u3002\u53d8\u91cf x \u7684\u503c\u4e00\u5b9a\u662f\u9519\u8bef\u7684\u3002\u5982\u679c\u5728\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u7684\u67d0\u4e2a\u65f6\u523b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u53d8\u91cf x \u5df2\u7ecf\u6709\u503c 8 \uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u77e5\u9053\u95ee\u9898\u4e00\u5b9a\u5728\u8fd9\u4e2a\u65f6\u523b\u4e4b\u524d\u5c31\u5df2\u7ecf\u53d1\u751f\u4e86\u3002 \u201c\u9ad8\u4e86\u4f4e\u4e86\u201d\u7684\u7c7b\u6bd4\u5e76\u4e0d\u5b8c\u7f8e\u2014\u2014\u6211\u4eec\u6709\u65f6\u4e5f\u53ef\u4ee5\u4ece\u8003\u8651\u4e2d\u5220\u9664\u4ee3\u7801\u7684\u6574\u4e2a\u90e8\u5206\uff0c\u800c\u4e0d\u83b7\u5f97\u4efb\u4f55\u5173\u4e8e\u5b9e\u9645\u95ee\u9898\u662f\u5728\u8be5\u70b9\u4e4b\u524d\u8fd8\u662f\u4e4b\u540e\u7684\u4fe1\u606f\u3002 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u5c55\u793a\u8fd9\u4e09\u79cd\u60c5\u51b5\u7684\u4f8b\u5b50\u3002 \u6700\u7ec8\uff0c\u6709\u4e86\u8db3\u591f\u7684\u7ebf\u7d22\uff0c\u518d\u914d\u5408\u4f7f\u7528\u5408\u9002\u7684\u6280\u672f\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u627e\u5230\u5f15\u8d77\u95ee\u9898\u7684\u786e\u5207\u4f4d\u7f6e\uff01\u5982\u679c\u6211\u4eec\u662f\u56e0\u4e3a\u67d0\u4e9b\u9519\u8bef\u5047\u8bbe\u800c\u9020\u6210\u4e86\u95ee\u9898\uff0c\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u4e5f\u53ef\u4ee5\u88ab\u53d1\u73b0\u3002\u5f53\u4f60\u6392\u9664\u4e86\u6240\u6709\u5176\u4ed6\u56e0\u7d20\uff0c\u5269\u4e0b\u7684\u4e00\u5b9a\u662f\u5bfc\u81f4\u95ee\u9898\u7684\u552f\u4e00\u56e0\u7d20\u3002\u90a3\u5c31\u53ea\u9700\u8981\u7406\u89e3\u4e3a\u4ec0\u4e48\u4e86\u3002 \u4f7f\u7528\u4ec0\u4e48\u731c\u6d4b\u7b56\u7565\u53d6\u51b3\u4e8e\u4f60\u81ea\u5df1\u2014\u2014\u731c\u6d4b\u7b56\u7565\u5e94\u8be5\u6839\u636ebug\u7684\u6027\u8d28\uff0c\u56e0\u5730\u5236\u5b9c\u5730\u4f7f\u7528\u3002\u6240\u4ee5\u4f60\u53ef\u80fd\u9700\u8981\u5c1d\u8bd5\u8bb8\u591a\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u7f29\u5c0f\u95ee\u9898\u7684\u8303\u56f4\u3002\u5f53\u79ef\u7d2f\u4e86\u8db3\u591f\u591a\u8c03\u8bd5\u95ee\u9898\u7684\u7ecf\u9a8c\u65f6\uff0c\u4f60\u7684\u76f4\u89c9\u4e5f\u4f1a\u53d8\u5f97\u8d8a\u6765\u8d8a\u51c6\u3002 \u90a3\u4e48\u6211\u4eec\u5982\u4f55\u201c\u731c\u6d4b\u201d\u5462\uff1f\u6709\u5f88\u591a\u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u7ae0\u4ece\u4e00\u4e9b\u7b80\u5355\u7684\u65b9\u6cd5\u5f00\u59cb\uff0c\u7136\u540e\u6211\u4eec\u5c06\u5728\u8fd9\u4e9b\u65b9\u6cd5\u7684\u57fa\u7840\u4e0a\u7ee7\u7eed\u53d1\u5c55\uff0c\u5e76\u5728\u4ee5\u540e\u7684\u7ae0\u8282\u4e2d\u63a2\u8ba8\u5176\u4ed6\u65b9\u6cd5\u3002","title":"3.3 - \u4ee3\u7801\u8c03\u8bd5\u7b56\u7565"},{"location":"3-3-A-strategy-for-debugging/#_1","text":"\u5047\u8bbe\u60a8\u6ce8\u610f\u5230\u4e00\u4e2a\u95ee\u9898\uff0c\u60a8\u60f3\u8981\u8ffd\u8e2a\u8fd9\u4e2a\u7279\u5b9a\u95ee\u9898\u7684\u539f\u56e0\u3002\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b(\u7279\u522b\u662f\u5728\u8f83\u5c0f\u7684\u7a0b\u5e8f\u4e2d)\uff0c\u6211\u4eec\u53ef\u4ee5\u5feb\u901f\u5730\u5b9a\u4f4d\u95ee\u9898\u6240\u5728\u7684\u4f4d\u7f6e\u3002 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\u7247\u6bb5\uff1a 1 2 3 4 5 6 7 8 int main () { getNames (); // ask user to enter a bunch of names sortNames (); // sort them in alphabetical order printNames (); // print the sorted list of names return 0 ; } \u5982\u679c\u4f60\u671f\u671b\u8fd9\u4e2a\u7a0b\u5e8f\u6309\u5b57\u6bcd\u987a\u5e8f\u6253\u5370\u540d\u79f0\uff0c\u4f46\u5b83\u6253\u5370\u7684\u7ed3\u679c\u5374\u662f\u76f8\u53cd\u7684\uff0c\u90a3\u4e48\u95ee\u9898\u53ef\u80fd\u51fa\u5728 sortNames \u51fd\u6570\u4e0a\u3002\u5bf9\u4e8e\u8fd9\u79cd\u53ef\u4ee5\u5c06\u95ee\u9898\u7f29\u5c0f\u5230\u7279\u5b9a\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u53ea\u9700\u67e5\u770b\u4ee3\u7801\u5c31\u53ef\u4ee5\u53d1\u73b0\u95ee\u9898\u3002 \u7136\u800c\uff0c\u968f\u7740\u7a0b\u5e8f\u53d8\u5f97\u8d8a\u6765\u8d8a\u590d\u6742\uff0c\u901a\u8fc7\u4ee3\u7801\u68c0\u67e5\u53d1\u73b0\u95ee\u9898\u4e5f\u53d8\u5f97\u8d8a\u6765\u8d8a\u590d\u6742\u3002 \u9996\u5148\uff0c\u6709\u5f88\u591a\u4ee3\u7801\u8981\u770b\u3002\u5728\u4e00\u4e2a\u6570\u5343\u884c\u957f\u7684\u7a0b\u5e8f\u4e2d\uff0c\u9010\u884c\u8d70\u8bfb\u4ee3\u7801\u4f1a\u82b1\u8d39\u5f88\u957f\u65f6\u95f4(\u800c\u4e14\u975e\u5e38\u65e0\u804a)\u3002\u5176\u6b21\uff0c\u4ee3\u7801\u672c\u8eab\u5f80\u5f80\u66f4\u590d\u6742\uff0c\u5f88\u591a\u5730\u65b9\u90fd\u53ef\u80fd\u51fa\u9519\u3002\u7b2c\u4e09\uff0c\u4ee3\u7801\u7684\u884c\u4e3a\u53ef\u80fd\u4e0d\u80fd\u63d0\u9ad8\u592a\u591a\u7ebf\u7d22\u3002\u5982\u679c\u4f60\u7f16\u5199\u4e86\u4e00\u4e2a\u8f93\u51fa\u80a1\u7968\u63a8\u8350\u7684\u7a0b\u5e8f\uff0c\u800c\u5b83\u5b9e\u9645\u4e0a\u4ec0\u4e48\u4e5f\u6ca1\u6709\u8f93\u51fa\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u80fd\u5c31\u4e0d\u77e5\u9053\u4ece\u54ea\u91cc\u5f00\u59cb\u67e5\u627e\u95ee\u9898\u4e86\u3002 \u6700\u540e\uff0c\u9519\u8bef\u53ef\u80fd\u662f\u7531\u9519\u8bef\u7684\u5047\u8bbe\u5f15\u8d77\u7684\u3002\u51e0\u4e4e\u4e0d\u53ef\u80fd\u4ece\u89c6\u89c9\u4e0a\u53d1\u73b0\u7531\u9519\u8bef\u5047\u8bbe\u5f15\u8d77\u7684\u9519\u8bef\uff0c\u56e0\u4e3a\u5728\u68c0\u67e5\u4ee3\u7801\u65f6\uff0c\u6211\u4eec\u5f80\u5f80\u503e\u5411\u4e8e\u4f1a\u505a\u51fa\u540c\u6837\u7684\u9519\u8bef\u5047\u8bbe\uff0c\u5bfc\u81f4\u6ca1\u6709\u6ce8\u610f\u5230\u9519\u8bef\u3002\u6240\u4ee5\uff0c\u5982\u679c\u4e00\u4e2a\u7a0b\u5e8f\u65e0\u6cd5\u901a\u8fc7\u4ee3\u7801\u68c0\u89c6\u53d1\u73b0\u95ee\u9898\uff0c\u90a3\u5e94\u8be5\u5982\u4f55\u5b9a\u4f4d\u5462\uff1f","title":"\u901a\u8fc7\u4ee3\u7801\u68c0\u89c6\u627e\u5230\u95ee\u9898"},{"location":"3-3-A-strategy-for-debugging/#_2","text":"\u6240\u5e78\uff0c\u5982\u679c\u4e0d\u80fd\u901a\u8fc7\u4ee3\u7801\u68c0\u89c6\u53d1\u73b0\u95ee\u9898\uff0c\u8fd8\u53ef\u4ee5\u91c7\u7528\u53e6\u4e00\u79cd\u65b9\u6cd5\uff1a\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u89c2\u5bdf\u5b83\u7684\u884c\u4e3a\uff0c\u5e76\u5c1d\u8bd5\u4ece\u4e2d\u8bca\u65ad\u95ee\u9898\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6982\u62ec\u4e3a: \u627e\u5230\u590d\u73b0\u95ee\u9898\u7684\u6761\u4ef6\uff1b \u8fd0\u884c\u7a0b\u5e8f\uff0c\u83b7\u53d6\u4fe1\u606f\uff0c\u9010\u6b65\u903c\u8fd1\"\u771f\u50cf\"\uff1b \u91cd\u590d\u4e0a\u8ff0\u6b65\u9aa4\u76f4\u5230\u627e\u5230\u95ee\u9898\u6240\u5728\u3002 \u5728\u672c\u7ae0\u7684\u5176\u4f59\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5b9e\u65bd\u8fd9\u79cd\u65b9\u6cd5\u7684\u6280\u672f\u3002","title":"\u901a\u8fc7\u8fd0\u884c\u7a0b\u5e8f\u627e\u5230\u95ee\u9898"},{"location":"3-3-A-strategy-for-debugging/#_3","text":"\u53d1\u73b0\u95ee\u9898\u7684\u7b2c\u4e00\u6b65\u4e5f\u662f\u6700\u91cd\u8981\u7684\u4e00\u6b65\u662f\u80fd\u591f\u91cd\u73b0\u95ee\u9898\uff0c\u539f\u56e0\u5f88\u7b80\u5355\uff1a\u5982\u679c\u4f60\u4e0d\u80fd\u518d\u6b21\u89c2\u5bdf\u5230\u95ee\u9898\u7684\u8bdd\uff0c\u8981\u5982\u4f55\u624d\u80fd\u5b9a\u4f4d\u5b83\u5462\uff1f \u56de\u5230\u5236\u51b0\u673a\u7684\u7c7b\u6bd4\u2014\u2014\u5047\u8bbe\u6709\u4e00\u5929\u4f60\u7684\u670b\u53cb\u544a\u8bc9\u4f60\uff0c\u4f60\u7684\u5236\u51b0\u673a\u574f\u4e86\u3002\u4f60\u53bb\u68c0\u67e5\u7684\u65f6\u5019\u53d1\u73b0\u5b83\u5de5\u4f5c\u5f97\u5f88\u597d\u3002\u4f60\u5e94\u8be5\u5982\u4f55\u8bca\u65ad\u8fd9\u4e2a\u95ee\u9898\uff1f\u8fd9\u4e2a\u65f6\u5019\u5c31\u6bd4\u8f83\u68d8\u624b\u4e86\u3002\u7136\u800c\uff0c\u5982\u679c\u4f60\u80fd\u591f\u518d\u6b21\u8ba9\u5236\u51b0\u673a\u8fdb\u5165\u5230\u4e0d\u80fd\u5de5\u4f5c\u7684\u72b6\u6001\uff0c\u90a3\u4e48\u624d\u53ef\u4ee5\u5f00\u59cb\u5b9a\u4f4d\u95ee\u9898\u3002 \u5982\u679c\u4e00\u4e2a\u8f6f\u4ef6bug\u5f88\u660e\u663e\u7684(\u4f8b\u5982\uff0c\u7a0b\u5e8f\u5728\u6bcf\u6b21\u8fd0\u884c\u65f6\u90fd\u5728\u76f8\u540c\u7684\u5730\u65b9\u5d29\u6e83)\uff0c\u90a3\u4e48\u91cd\u73b0\u8fd9\u4e2a\u95ee\u9898\u5c31\u76f8\u5f53\u7b80\u5355\u4e86\u3002\u7136\u800c\uff0c\u590d\u73b0\u95ee\u9898\u6709\u65f6\u5019\u662f\u5f88\u56f0\u96be\u7684\u3002\u4f8b\u5982\u8be5\u95ee\u9898\u53ef\u80fd\u53ea\u53d1\u751f\u5728\u67d0\u4e9b\u8bbe\u5907\u4e0a\uff0c\u6216\u53ea\u5728\u7279\u5b9a\u60c5\u51b5\u4e0b(\u4f8b\u5982\uff0c\u5f53\u7528\u6237\u8f93\u5165\u67d0\u4e9b\u8f93\u5165\u65f6)\u624d\u51fa\u73b0\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8bbe\u8ba1\u4e00\u7ec4\u590d\u73b0\u95ee\u9898\u7684\u6b65\u9aa4\u4f1a\u5f88\u6709\u5e2e\u52a9\u3002\u590d\u73b0\u6b65\u9aa4\u662f\u4e00\u4e2a\u6e05\u6670\u4e14\u7cbe\u786e\u7684\u5206\u6b65\u6267\u884c\u5217\u8868\uff0c\u4f60\u53ef\u4ee5\u9075\u5faa\u8fd9\u4e9b\u6b65\u9aa4\u4f7f\u95ee\u9898\u4ee5\u66f4\u5927\u7684\u6982\u7387\u518d\u6b21\u51fa\u73b0\u3002\u6211\u4eec\u7684\u76ee\u6807\u662f\u5c31\u80fd\u591f\u5c3d\u53ef\u80fd\u5730\u4f7f\u95ee\u9898\u518d\u6b21\u51fa\u73b0\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u53cd\u590d\u8fd0\u884c\u7a0b\u5e8f\uff0c\u5e76\u67e5\u627e\u7ebf\u7d22\u6765\u786e\u5b9a\u662f\u4ec0\u4e48\u539f\u56e0\u5bfc\u81f4\u4e86\u95ee\u9898\u3002\u5982\u679c\u95ee\u9898\u53ef\u4ee5100%\u7684\u91cd\u73b0\uff0c\u90a3\u81ea\u7136\u662f\u6700\u7406\u60f3\u7684\u72b6\u6001\uff0c\u4f46\u4f4e\u4e8e100%\u901a\u5e38\u4e5f\u662f\u53ef\u4ee5\u7684\u3002\u4e00\u4e2a\u53ea\u670950%\u51e0\u7387\u89e6\u53d1\u7684\u95ee\u9898\uff0c\u610f\u5473\u7740\u6211\u4eec\u9700\u8981\u82b1\u8d39\u4e24\u500d\u7684\u65f6\u95f4\u6765\u8bca\u65ad\u95ee\u9898\uff0c\u56e0\u4e3a\u6709\u4e00\u534a\u7684\u65f6\u95f4\u7a0b\u5e8f\u4e0d\u4f1a\u663e\u793a\u95ee\u9898\uff0c\u56e0\u6b64\u4e0d\u4f1a\u63d0\u4f9b\u4efb\u4f55\u6709\u7528\u7684\u8bca\u65ad\u4fe1\u606f\u3002","title":"\u590d\u73b0\u95ee\u9898"},{"location":"3-3-A-strategy-for-debugging/#_4","text":"\u4e00\u65e6\u6211\u4eec\u80fd\u591f\u6bd4\u8f83\u7a33\u5b9a\u5730\u590d\u73b0\u95ee\u9898\uff0c\u63a5\u4e0b\u6765\u5c31\u662f\u627e\u5230\u5bfc\u81f4\u95ee\u9898\u7684\u4ee3\u7801\u3002\u6839\u636e\u95ee\u9898\u7684\u6027\u8d28\u4e0d\u540c\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u53ef\u80fd\u5f88\u7b80\u5355\uff0c\u4e5f\u53ef\u80fd\u5f88\u56f0\u96be\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u6211\u4eec\u4e0d\u592a\u6e05\u695a\u95ee\u9898\u5230\u5e95\u5728\u54ea\u91cc\u3002\u6211\u4eec\u600e\u4e48\u627e\u5230\u5b83? \u8fd9\u91cc\u53ef\u4ee5\u505a\u4e2a\u7c7b\u6bd4\u3002\u6211\u4eec\u6765\u73a9\u4e2a\u731c\u6570\u5b57\u6e38\u620f\uff0c\u4f60\u9700\u8981\u731c\u4e00\u4e2a1\u523010\u4e4b\u95f4\u7684\u6570\u5b57\uff0c\u6bcf\u731c\u4e00\u6b21\uff0c\u6211\u5c31\u4f1a\u544a\u8bc9\u4f60\u662f\u592a\u9ad8\u3001\u592a\u4f4e\u8fd8\u662f\u6b63\u786e\uff1a 1 2 3 4 5 6 7 8 You: 5 Me: Too low You: 8 Me: Too high You: 6 Me: Too low You: 7 Me: Correct \u5728\u4e0a\u9762\u7684\u6e38\u620f\u4e2d\uff0c\u4f60\u4e0d\u9700\u8981\u731c\u6bcf\u4e00\u4e2a\u6570\u5b57\u6765\u627e\u5230\u7b54\u6848\u3002\u901a\u8fc7\u731c\u6d4b\u548c\u8003\u8651\u4ece\u6bcf\u6b21\u731c\u6d4b\u4e2d\u5b66\u5230\u7684\u4fe1\u606f\u7684\u8fc7\u7a0b\uff0c\u4f60\u53ef\u80fd\u53ea\u9700\u51e0\u6b21\u731c\u6d4b\u5c31\u53ef\u4ee5\u201c\u9501\u5b9a\u201d\u6b63\u786e\u7684\u6570\u5b57(\u5982\u679c\u4f60\u4f7f\u7528\u6700\u4f18\u7b56\u7565\uff0c\u90a3\u4e48\u603b\u662f\u53ef\u4ee5\u57284\u6b21\u6216\u66f4\u5c11\u7684\u731c\u6d4b\u4e2d\u627e\u5230\u76ee\u6807\u6570\u5b57)\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u6765\u8c03\u8bd5\u7a0b\u5e8f\u3002\u5728\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u4e0d\u77e5\u9053bug\u5728\u54ea\u91cc\u3002\u7136\u800c\uff0c\u6211\u4eec\u77e5\u9053\u95ee\u9898\u4e00\u5b9a\u662f\u5728\u7a0b\u5e8f\u5f00\u59cb\u5230\u7a0b\u5e8f\u663e\u793a\u51fa\u6211\u4eec\u53ef\u4ee5\u89c2\u5bdf\u5230\u7684\u7b2c\u4e00\u4e2a\u9519\u8bef\u75c7\u72b6\u4e4b\u95f4\u6267\u884c\u7684\u4ee3\u7801\u4e2d\u7684\u67d0\u4e2a\u5730\u65b9\u3002\u8fd9\u81f3\u5c11\u6392\u9664\u4e86\u5728\u51fa\u73b0\u7b2c\u4e00\u4e2a\u53ef\u89c2\u5bdf\u5230\u7684\u75c7\u72b6\u540e\u6267\u884c\u7684\u7a0b\u5e8f\u90e8\u5206\u3002\u4f46\u8fd9\u4ecd\u7136\u53ef\u80fd\u7559\u4e0b\u5927\u91cf\u7684\u4ee3\u7801\u9700\u8981\u8986\u76d6\u3002\u4e3a\u4e86\u8bca\u65ad\u95ee\u9898\uff0c\u6211\u4eec\u5c06\u5bf9\u95ee\u9898\u7684\u4f4d\u7f6e\u8fdb\u884c\u4e00\u4e9b\u6709\u6839\u636e\u7684\u731c\u6d4b\uff0c\u76ee\u7684\u662f\u5feb\u901f\u627e\u5230\u95ee\u9898\u6240\u5728\u3002 \u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u65e0\u8bba\u662f\u4ec0\u4e48\u539f\u56e0\u5bfc\u81f4\u6211\u4eec\u6ce8\u610f\u5230\u95ee\u9898\uff0c\u90fd\u4f1a\u7ed9\u6211\u4eec\u4e00\u4e2a\u63a5\u8fd1\u5b9e\u9645\u95ee\u9898\u6240\u5728\u7684\u521d\u59cb\u731c\u6d4b\u3002\u4f8b\u5982\uff0c\u5982\u679c\u7a0b\u5e8f\u5728\u5e94\u8be5\u5199\u5165\u6570\u636e\u7684\u65f6\u5019\u6ca1\u6709\u5199\u5165\u6587\u4ef6\uff0c\u90a3\u4e48\u95ee\u9898\u53ef\u80fd\u51fa\u5728\u5904\u7406\u5199\u5165\u6587\u4ef6\u7684\u4ee3\u7801\u7684\u67d0\u4e2a\u5730\u65b9(\u5e9f\u8bdd!)\u7136\u540e\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u6e38\u620f\u7b56\u7565\u6765\u5c1d\u8bd5\u627e\u51fa\u771f\u6b63\u7684\u95ee\u9898\u6240\u5728\u3002 \u4f8b\u5982\uff1a \u5982\u679c\u5728\u7a0b\u5e8f\u7684\u67d0\u4e2a\u65f6\u523b\uff0c\u6211\u4eec\u53ef\u4ee5\u8bc1\u660e\u95ee\u9898\u8fd8\u6ca1\u6709\u53d1\u751f\uff0c\u8fd9\u5c31\u7c7b\u4f3c\u4e8e\u6536\u5230\u4e00\u4e2a\u201c\u4f4e\u4e86\u201d\u7684\u7ed3\u679c\u2014\u2014\u6211\u4eec\u77e5\u9053\u95ee\u9898\u4e00\u5b9a\u662f\u5728\u7a0b\u5e8f\u540e\u9762\u7684\u67d0\u4e2a\u5730\u65b9\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u7684\u7a0b\u5e8f\u6bcf\u6b21\u90fd\u5728\u540c\u4e00\u4e2a\u5730\u65b9\u5d29\u6e83\uff0c\u5e76\u4e14\u6211\u4eec\u53ef\u4ee5\u8bc1\u660e\u7a0b\u5e8f\u6ca1\u6709\u5728\u7a0b\u5e8f\u6267\u884c\u7684\u67d0\u4e2a\u7279\u5b9a\u70b9\u5d29\u6e83\uff0c\u90a3\u4e48\u5d29\u6e83\u4e00\u5b9a\u53d1\u751f\u5728\u4ee3\u7801\u7684\u540e\u9762\u3002 \u5982\u679c\u5728\u7a0b\u5e8f\u7684\u67d0\u4e2a\u70b9\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u89c2\u5bdf\u5230\u4e0e\u95ee\u9898\u76f8\u5173\u7684\u4e0d\u6b63\u786e\u884c\u4e3a\uff0c\u90a3\u4e48\u8fd9\u5c31\u7c7b\u4f3c\u4e8e\u63a5\u6536\u5230\u4e00\u4e2a\u201c\u9ad8\u4e86\u201d\u7684\u7ed3\u679c\uff0c\u6211\u4eec\u77e5\u9053\u95ee\u9898\u4e00\u5b9a\u662f\u5728\u7a0b\u5e8f\u7684\u65e9\u671f\u67d0\u4e2a\u5730\u65b9\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4e00\u4e2a\u7a0b\u5e8f\u8f93\u51fa\u67d0\u4e2a\u53d8\u91cf x \u7684\u503c\u3002\u4f60\u5e0c\u671b\u5b83\u8f93\u51fa 2 \uff0c\u4f46\u662f\u5b83\u8f93\u51fa\u4e868\u3002\u53d8\u91cf x \u7684\u503c\u4e00\u5b9a\u662f\u9519\u8bef\u7684\u3002\u5982\u679c\u5728\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u7684\u67d0\u4e2a\u65f6\u523b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u53d8\u91cf x \u5df2\u7ecf\u6709\u503c 8 \uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u77e5\u9053\u95ee\u9898\u4e00\u5b9a\u5728\u8fd9\u4e2a\u65f6\u523b\u4e4b\u524d\u5c31\u5df2\u7ecf\u53d1\u751f\u4e86\u3002 \u201c\u9ad8\u4e86\u4f4e\u4e86\u201d\u7684\u7c7b\u6bd4\u5e76\u4e0d\u5b8c\u7f8e\u2014\u2014\u6211\u4eec\u6709\u65f6\u4e5f\u53ef\u4ee5\u4ece\u8003\u8651\u4e2d\u5220\u9664\u4ee3\u7801\u7684\u6574\u4e2a\u90e8\u5206\uff0c\u800c\u4e0d\u83b7\u5f97\u4efb\u4f55\u5173\u4e8e\u5b9e\u9645\u95ee\u9898\u662f\u5728\u8be5\u70b9\u4e4b\u524d\u8fd8\u662f\u4e4b\u540e\u7684\u4fe1\u606f\u3002 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u5c55\u793a\u8fd9\u4e09\u79cd\u60c5\u51b5\u7684\u4f8b\u5b50\u3002 \u6700\u7ec8\uff0c\u6709\u4e86\u8db3\u591f\u7684\u7ebf\u7d22\uff0c\u518d\u914d\u5408\u4f7f\u7528\u5408\u9002\u7684\u6280\u672f\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u627e\u5230\u5f15\u8d77\u95ee\u9898\u7684\u786e\u5207\u4f4d\u7f6e\uff01\u5982\u679c\u6211\u4eec\u662f\u56e0\u4e3a\u67d0\u4e9b\u9519\u8bef\u5047\u8bbe\u800c\u9020\u6210\u4e86\u95ee\u9898\uff0c\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u4e5f\u53ef\u4ee5\u88ab\u53d1\u73b0\u3002\u5f53\u4f60\u6392\u9664\u4e86\u6240\u6709\u5176\u4ed6\u56e0\u7d20\uff0c\u5269\u4e0b\u7684\u4e00\u5b9a\u662f\u5bfc\u81f4\u95ee\u9898\u7684\u552f\u4e00\u56e0\u7d20\u3002\u90a3\u5c31\u53ea\u9700\u8981\u7406\u89e3\u4e3a\u4ec0\u4e48\u4e86\u3002 \u4f7f\u7528\u4ec0\u4e48\u731c\u6d4b\u7b56\u7565\u53d6\u51b3\u4e8e\u4f60\u81ea\u5df1\u2014\u2014\u731c\u6d4b\u7b56\u7565\u5e94\u8be5\u6839\u636ebug\u7684\u6027\u8d28\uff0c\u56e0\u5730\u5236\u5b9c\u5730\u4f7f\u7528\u3002\u6240\u4ee5\u4f60\u53ef\u80fd\u9700\u8981\u5c1d\u8bd5\u8bb8\u591a\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u7f29\u5c0f\u95ee\u9898\u7684\u8303\u56f4\u3002\u5f53\u79ef\u7d2f\u4e86\u8db3\u591f\u591a\u8c03\u8bd5\u95ee\u9898\u7684\u7ecf\u9a8c\u65f6\uff0c\u4f60\u7684\u76f4\u89c9\u4e5f\u4f1a\u53d8\u5f97\u8d8a\u6765\u8d8a\u51c6\u3002 \u90a3\u4e48\u6211\u4eec\u5982\u4f55\u201c\u731c\u6d4b\u201d\u5462\uff1f\u6709\u5f88\u591a\u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u7ae0\u4ece\u4e00\u4e9b\u7b80\u5355\u7684\u65b9\u6cd5\u5f00\u59cb\uff0c\u7136\u540e\u6211\u4eec\u5c06\u5728\u8fd9\u4e9b\u65b9\u6cd5\u7684\u57fa\u7840\u4e0a\u7ee7\u7eed\u53d1\u5c55\uff0c\u5e76\u5728\u4ee5\u540e\u7684\u7ae0\u8282\u4e2d\u63a2\u8ba8\u5176\u4ed6\u65b9\u6cd5\u3002","title":"\u5173\u6ce8\u95ee\u9898"},{"location":"3-4-Basic-debugging-tactics/","tags":["debugging"],"text":"\u5728\u4e0a\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u63a2\u7d22\u4e86\u4e00\u79cd\u901a\u8fc7\u8fd0\u884c\u7a0b\u5e8f\u548c\u4f7f\u7528\u731c\u6d4b\u6765\u67e5\u627e\u95ee\u9898\u6240\u5728\u7684\u7b56\u7565\u3002\u672c\u8282\u8bfe\u6211\u4eec\u5c06\u63a2\u7d22\u4e00\u4e9b\u7528\u4e8e\u731c\u6d4b\u548c\u6536\u96c6\u4fe1\u606f\u4ee5\u5e2e\u52a9\u53d1\u73b0\u95ee\u9898\u7684\u57fa\u672c\u7b56\u7565\u3002 Debug \u6280\u672f 1\uff1a\u6ce8\u91ca\u6389\u4ee3\u7801 \u5148\u4ece\u4e00\u4e2a\u7b80\u5355\u7684\u95ee\u9898\u5f00\u59cb\u3002\u5982\u679c\u7a0b\u5e8f\u8868\u73b0\u51fa\u9519\u8bef\u7684\u884c\u4e3a\uff0c\u6211\u4eec\u53ef\u4ee5\u6ce8\u91ca\u6389\u4e00\u4e9b\u4ee3\u7801\uff0c\u770b\u770b\u95ee\u9898\u662f\u5426\u4ecd\u7136\u5b58\u5728\u3002\u5982\u679c\u95ee\u9898\u4ecd\u7136\u5b58\u5728\uff0c\u5219\u6ce8\u91ca\u6389\u7684\u4ee3\u7801\u80af\u5b9a\u6ca1\u6709\u95ee\u9898\u3002 \u8003\u8651\u4ee5\u4e0b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 int main () { getNames (); // ask user to enter a bunch of names doMaintenance (); // do some random stuff sortNames (); // sort them in alphabetical order printNames (); // print the sorted list of names return 0 ; } \u5047\u8bbe\u8fd9\u4e2a\u7a0b\u5e8f\u5e94\u8be5\u6309\u5b57\u6bcd\u987a\u5e8f\u6253\u5370\u7528\u6237\u8f93\u5165\u7684\u540d\u5b57\uff0c\u4f46\u5b83\u5374\u6309\u76f8\u53cd\u7684\u5b57\u6bcd\u987a\u5e8f\u6253\u5370\u3002\u95ee\u9898\u5728\u54ea\u91cc? getNames \u662f\u5426\u8f93\u5165\u4e86\u9519\u8bef\u7684\u540d\u79f0\uff1f sortNames \u662f\u5426\u5c06\u5b83\u4eec\u53cd\u5411\u6392\u5e8f\uff1f\u8fd8\u662f printNames \u6253\u5370\u7684\u987a\u5e8f\u4e0d\u5bf9\uff1f\u4ee5\u4e0a\u4efb\u4f55\u4e00\u79cd\u731c\u6d4b\u90fd\u6709\u53ef\u80fd\uff0c\u4f46\u662f domainmaintenance() \u591a\u534a\u4e0e\u95ee\u9898\u65e0\u5173\uff0c\u6240\u4ee5\u53ef\u4ee5\u5148\u5c06\u5176\u6ce8\u91ca\u6389\u3002 1 2 3 4 5 6 7 8 9 int main () { getNames (); // ask user to enter a bunch of names // doMaintenance(); // do some random stuff sortNames (); // sort them in alphabetical order printNames (); // print the sorted list of names return 0 ; } \u5982\u679c\u95ee\u9898\u6d88\u5931\u4e86\uff0c\u90a3\u4e48 domainance \u53cd\u5012\u662f\u6709\u95ee\u9898\u7684\uff0c\u5230\u65f6\u5019\u518d\u6ce8\u610f\u529b\u96c6\u4e2d\u5728\u90a3\u91cc\u3002 \u4e0d\u8fc7\uff0c\u95ee\u9898\u5f88\u53ef\u80fd\u8fd8\u4f1a\u5b58\u5728\uff0c\u56e0\u6b64 doMaintenance \u4e0d\u662f\u9519\u8bef\u7684\u6839\u6e90\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u641c\u7d22\u4e2d\u6392\u9664\u6574\u4e2a\u51fd\u6570\u3002\u8fd9\u5e76\u4e0d\u80fd\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u5b9e\u9645\u95ee\u9898\u662f\u53d1\u751f\u5728 doMaintenance \u8c03\u7528\u4e4b\u524d\u8fd8\u662f\u4e4b\u540e\uff0c\u4f46\u7ec8\u5f52\u662f\u7f29\u5c0f\u4e86\u8303\u56f4\u3002 \u4e0d\u8981\u5fd8\u8bb0\u6ce8\u91ca\u6389\u4e86\u54ea\u4e9b\u51fd\u6570\uff0c\u4ee5\u4fbf\u4ee5\u540e\u53ef\u4ee5\u53d6\u6d88\u6ce8\u91ca! Debug \u6280\u672f 2\uff1a\u9a8c\u8bc1\u4ee3\u7801\u6d41\u7a0b \u590d\u6742\u7a0b\u5e8f\u4e2d\u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u662f\u7a0b\u5e8f\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u7684\u6b21\u6570\u592a\u591a\u6216\u8fc7\u5c11(\u5305\u62ec\u6839\u672c\u4e0d\u8c03\u7528)\u3002 \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5c06\u8bed\u53e5\u653e\u5728\u51fd\u6570\u7684\u9876\u90e8\u4ee5\u6253\u5370\u51fd\u6570\u540d\u4f1a\u5f88\u6709\u5e2e\u52a9\u3002\u8fd9\u6837\uff0c\u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u5c31\u53ef\u4ee5\u6253\u5370\u51fa\u8c03\u7528\u4e86\u54ea\u4e9b\u51fd\u6570\u3002 \u5c0f\u8d34\u58eb \u5f53\u6253\u5370\u7528\u4e8e\u8c03\u8bd5\u76ee\u7684\u7684\u4fe1\u606f\u65f6\uff0c\u4f7f\u7528 std::cerr \u800c\u4e0d\u662f std::cout \u3002\u8fd9\u6837\u505a\u7684\u4e00\u4e2a\u539f\u56e0\u662f std::cout \u53ef\u80fd\u4f1a\u88ab\u7f13\u51b2\uff0c\u8fd9\u610f\u5473\u7740\u5728\u8981\u6c42 std::cout \u6253\u5370\u4fe1\u606f\u548c\u5b83\u5b9e\u9645\u6253\u5370\u4fe1\u606f\u4e4b\u95f4\u53ef\u80fd\u4f1a\u6709\u4e00\u4e2a\u65f6\u95f4\u5dee\u3002\u5982\u679c\u4f7f\u7528 std::cout \u8fdb\u884c\u6253\u5370\u7136\u540e\u7a0b\u5e8f\u7acb\u5373\u5d29\u6e83\uff0c\u90a3\u4e48 std::cout \u53ef\u80fd\u8fd8\u6ca1\u6709\u6765\u5f97\u53ca\u6253\u5370\u3002\u8fd9\u53ef\u80fd\u4f1a\u8bef\u5bfc\u4f60\u3002\u53e6\u4e00\u65b9\u9762\uff0c std::cerr \u662f\u65e0\u7f13\u51b2\u7684\uff0c\u8fd9\u610f\u5473\u7740\u53d1\u9001\u7ed9\u5b83\u7684\u4efb\u4f55\u5185\u5bb9\u90fd\u5c06\u7acb\u5373\u6253\u5370\u3002\u8fd9\u6709\u52a9\u4e8e\u786e\u4fdd\u6240\u6709\u8c03\u8bd5\u8f93\u51fa\u5c3d\u5feb\u51fa\u73b0(\u4ee5\u4e00\u4e9b\u6027\u80fd\u4e3a\u4ee3\u4ef7\uff0c\u6211\u4eec\u5728\u8c03\u8bd5\u65f6\u901a\u5e38\u4e0d\u5173\u7cfb\u6027\u80fd)\u3002 \u4f7f\u7528 std::cerr \u4e5f\u53ef\u4ee5\u8868\u660e\u8be5\u6253\u5370\u4fe1\u606f\u548c\u9519\u8bef\u6d41\u7a0b\u6709\u5173\u3002 \u8003\u8651\u4ee5\u4e0b\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int getValue () { return 4 ; } int main () { std :: cout << getValue ; return 0 ; } \u4f60\u5fc5\u987b\u5173\u95ed \u201c\u5c06\u8b66\u544a\u5f53\u505a\u9519\u8bef\u5904\u7406\u201d \u7684\u7f16\u8bd1\u9009\u9879\uff0c\u624d\u80fd\u7f16\u8bd1\u8be5\u4ee3\u7801\u3002 \u867d\u7136\u6211\u4eec\u671f\u671b\u8fd9\u4e2a\u7a0b\u5e8f\u6253\u5370\u503c 4\uff0c\u4f46\u5b83\u5b9e\u9645\u6253\u5370\u4e86 1 \u3002 \u5728 Visual Studio (\u6216\u8005\u5176\u4ed6\u7f16\u8bd1\u5668)\u4e0a\uff0c\u4e5f\u53ef\u80fd\u6253\u5370\uff1a 1 00101424 \u6dfb\u52a0\u4e00\u4e9b\u8c03\u8bd5\u8bed\u53e5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int getValue () { std :: cerr << \"getValue() called \\n \" ; return 4 ; } int main () { std :: cerr << \"main() called \\n \" ; std :: cout << getValue ; return 0 ; } \u5c0f\u8d34\u58eb \u5728\u6dfb\u52a0\u4e34\u65f6\u8c03\u8bd5\u8bed\u53e5\u65f6\uff0c\u4e0d\u7f29\u8fdb\u4f1a\u5f88\u6709\u5e2e\u52a9\u3002\u8fd9\u6837\u5728\u4e0d\u9700\u8981\u65f6\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u627e\u5230\u5b83\u4eec\u5e76\u5220\u9664\u3002 \u73b0\u5728\uff0c\u51fd\u6570\u6267\u884c\u65f6\u4f1a\u6253\u5370\u5bf9\u5e94\u7684\u51fd\u6570\u540d\uff1a 1 2 main () called 1 \u53ef\u4ee5\u770b\u5230 getValue \u6839\u672c\u6ca1\u88ab\u8c03\u7528\u8fc7\u3002\u90a3\u4e48\u80af\u5b9a\u662f\u8c03\u7528\u8be5\u51fd\u6570\u7684\u4ee3\u7801\u51fa\u4e86\u95ee\u9898\u3002\u4ed4\u7ec6\u770b\u770b\u90a3\u884c\u4ee3\u7801\uff1a 1 std :: cout << getValue ; \u54e6\uff0c\u770b\uff0c\u6211\u4eec\u5fd8\u8bb0\u4e86\u51fd\u6570\u8c03\u7528\u7684\u62ec\u53f7\u3002\u5b83\u5e94\u8be5\u662f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int getValue () { std :: cerr << \"getValue() called \\n \" ; return 4 ; } int main () { std :: cerr << \"main() called \\n \" ; std :: cout << getValue (); // added parenthesis here return 0 ; } \u8fd9\u6837\u4e00\u6765\u5c31\u6ca1\u95ee\u9898\u4e86\uff1a 1 2 3 main() called getValue() called 4 \u6b64\u65f6\u53ef\u4ee5\u5220\u9664\u4e34\u65f6\u8c03\u8bd5\u8bed\u53e5\u4e86\u3002 Debug \u6280\u672f 3\uff1a \u6253\u5370\u503c \u5bf9\u4e8e\u67d0\u4e9b\u7c7b\u578b\u7684\u9519\u8bef\uff0c\u7a0b\u5e8f\u53ef\u80fd\u8ba1\u7b97\u6216\u4f20\u9012\u4e86\u9519\u8bef\u7684\u503c\u3002 \u6211\u4eec\u8fd8\u53ef\u4ee5\u8f93\u51fa\u53d8\u91cf(\u5305\u62ec\u53c2\u6570)\u6216\u8868\u8fbe\u5f0f\u7684\u503c\uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u662f\u6b63\u786e\u7684\u3002 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff0c\u5b83\u5e94\u8be5\u628a\u4e24\u4e2a\u6570\u5b57\u76f8\u52a0\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include int add ( int x , int y ) { return x + y ; } void printResult ( int z ) { std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { int x { getUserInput () }; int y { getUserInput () }; std :: cout << x << \" + \" << y << '\\n' ; int z { add ( x , 5 ) }; printResult ( z ); return 0 ; } \u8f93\u51fa\u7ed3\u679c\u4e0d\u6b63\u786e\uff1a 1 2 3 4 Enter a number: 4 Enter a number: 3 4 + 3 The answer is: 9 \u7ed3\u679c\u4e0d\u5bf9\uff0c\u4f46\u4f60\u80fd\u53d1\u73b0\u9519\u8bef\u5417\uff1f\u5373\u4f7f\u5728\u8fd9\u4e2a\u7b80\u77ed\u7684\u7a0b\u5e8f\u4e2d\uff0c\u95ee\u9898\u4e5f\u5f88\u96be\u88ab\u53d1\u73b0\u3002\u63a5\u4e0b\u6765\u6dfb\u52a0\u4e00\u4e9b\u4ee3\u7801\u6765\u6253\u5370\u53d8\u91cf\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include int add ( int x , int y ) { return x + y ; } void printResult ( int z ) { std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { int x { getUserInput () }; std :: cerr << \"main::x = \" << x << '\\n' ; int y { getUserInput () }; std :: cerr << \"main::y = \" << y << '\\n' ; std :: cout << x << \" + \" << y << '\\n' ; int z { add ( x , 5 ) }; std :: cerr << \"main::z = \" << z << '\\n' ; printResult ( z ); return 0 ; } \u8f93\u51fa\uff1a 1 2 3 4 5 6 7 Enter a number: 4 main::x = 4 Enter a number: 3 main::y = 3 4 + 3 main::z = 9 The answer is: 9 \u53d8\u91cf x \u548c y \u7684\u503c\u90fd\u662f\u6b63\u786e\u7684\uff0c\u4f46\u662f z \u7684\u503c\u4e0d\u6b63\u786e\u3002\u6240\u4ee5\u95ee\u9898\u80af\u5b9a\u5728\u8fd9\u9644\u8fd1\uff0c\u8fd9\u6837\u4e00\u6765 add \u51fd\u6570\u5c31\u975e\u5e38\u53ef\u7591\u4e86\u3002 \u4fee\u6539\u4e00\u4e0b add \u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include int add ( int x , int y ) { std :: cerr << \"add() called (x=\" << x << \", y=\" << y << \") \\n \" ; return x + y ; } void printResult ( int z ) { std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { int x { getUserInput () }; std :: cerr << \"main::x = \" << x << '\\n' ; int y { getUserInput () }; std :: cerr << \"main::y = \" << y << '\\n' ; std :: cout << x << \" + \" << y << '\\n' ; int z { add ( x , 5 ) }; std :: cerr << \"main::z = \" << z << '\\n' ; printResult ( z ); return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 5 6 7 Enter a number: 4 main::x = 4 Enter a number: 3 main::y = 3 add() called (x=4, y=5) main::z = 9 The answer is: 9 \u53d8\u91cf y \u7684\u503c\u662f3\uff0c\u4f46\u662f\u6211\u4eec\u7684\u51fd\u6570 add \u7684\u53c2\u6570 y \u7684\u503c\u662f 5 \u3002\u6211\u4eec\u80af\u5b9a\u4f20\u9012\u4e86\u9519\u8bef\u7684\u53c2\u6570\u3002\u679c\u7136\uff1a 1 int z { add ( x , 5 ) }; \u627e\u5230\u4e86\uff01\u5728\u8c03\u7528 add \u51fd\u6570\u7684\u65f6\u5019\u6211\u4eec\u4f20\u9012\u4e86\u5b57\u9762\u91cf 5 \u800c\u4e0d\u662f\u53d8\u91cf y \u3002\u4fee\u6539\u8fd9\u4e2a\u95ee\u9898\u53ea\u9700\u8981\u628a\u8fd9\u91cc\u7684\u5b9e\u53c2\u6539\u6389\u5c31\u53ef\u4ee5\u4e86\u3002 \u518d\u770b\u4e00\u4e2a\u4f8b\u5b50\uff1a \u8fd9\u4e2a\u7a0b\u5e8f\u4e0e\u524d\u9762\u7684\u7a0b\u5e8f\u975e\u5e38\u76f8\u4f3c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include int add ( int x , int y ) { return x + y ; } void printResult ( int z ) { std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return -- x ; } int main () { int x { getUserInput () }; int y { getUserInput () }; int z { add ( x , y ) }; printResult ( z ); return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 Enter a number: 4 Enter a number: 3 The answer is: 5 \u7ed3\u679c\u4e0d\u5bf9\uff0c\u4ec0\u4e48\u539f\u56e0\u5462\uff1f \u8ba9\u6211\u4eec\u5bf9\u8fd9\u6bb5\u4ee3\u7801\u8fdb\u884c\u4e00\u4e9b\u8c03\u8bd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include int add ( int x , int y ) { std :: cerr << \"add() called (x=\" << x << \", y=\" << y << \") \\n \" ; return x + y ; } void printResult ( int z ) { std :: cerr << \"printResult() called (z=\" << z << \") \\n \" ; std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cerr << \"getUserInput() called \\n \" ; std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return -- x ; } int main () { std :: cerr << \"main() called \\n \" ; int x { getUserInput () }; std :: cerr << \"main::x = \" << x << '\\n' ; int y { getUserInput () }; std :: cerr << \"main::y = \" << y << '\\n' ; int z { add ( x , y ) }; std :: cerr << \"main::z = \" << z << '\\n' ; printResult ( z ); return 0 ; } \u73b0\u5728\u8ba9\u6211\u4eec\u7528\u76f8\u540c\u7684\u8f93\u5165\u518d\u6b21\u8fd0\u884c\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 main() called getUserInput() called Enter a number: 4 main::x = 3 getUserInput() called Enter a number: 3 main::y = 2 add() called (x=3, y=2) main::z = 5 printResult() called (z=5) The answer is: 5 \u8fd9\u6837\u4e00\u6765\u5c31\u53ef\u4ee5\u770b\u51fa\u95ee\u9898\u4e86\uff1a\u7528\u6237\u8f93\u5165\u7684\u503c\u662f 4 \uff0c\u4f46\u662f main \u51fd\u6570\u4e2d x \u5f97\u5230\u7684\u503c\u5374\u662f 3 \u3002\u5728\u7528\u6237\u8f93\u5165\u7684\u4f4d\u7f6e\u548c\u7ed9 x \u8d4b\u503c\u4e4b\u95f4\u7684\u4ee3\u7801\u80af\u5b9a\u51fa\u4e86\u95ee\u9898\u3002\u5728\u51fd\u6570 getUserInput \u4e2d\u6dfb\u52a0\u4e00\u4e9b\u8c03\u8bd5\u4ee3\u7801\u6765\uff0c\u786e\u5b9a\u7a0b\u5e8f\u662f\u5426\u4ece\u7528\u6237\u90a3\u91cc\u83b7\u5f97\u4e86\u6b63\u786e\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include int add ( int x , int y ) { std :: cerr << \"add() called (x=\" << x << \", y=\" << y << \") \\n \" ; return x + y ; } void printResult ( int z ) { std :: cerr << \"printResult() called (z=\" << z << \") \\n \" ; std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cerr << \"getUserInput() called \\n \" ; std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; std :: cerr << \"getUserInput::x = \" << x << '\\n' ; // \u6dfb\u52a0\u8c03\u8bd5\u4fe1\u606f return -- x ; } int main () { std :: cerr << \"main() called \\n \" ; int x { getUserInput () }; std :: cerr << \"main::x = \" << x << '\\n' ; int y { getUserInput () }; std :: cerr << \"main::y = \" << y << '\\n' ; int z { add ( x , y ) }; std :: cerr << \"main::z = \" << z << '\\n' ; printResult ( z ); return 0 ; } \u8f93\u51fa\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 main() called getUserInput() called Enter a number: 4 getUserInput::x = 4 main::x = 3 getUserInput() called Enter a number: 3 getUserInput::x = 3 main::y = 2 add() called (x=3, y=2) main::z = 5 printResult() called (z=5) The answer is: 5 \u6dfb\u52a0\u8fd9\u884c\u8c03\u8bd5\u4ee3\u7801\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u7528\u6237\u8f93\u5165\u88ab\u6b63\u786e\u5730\u63a5\u6536\u5230 getUserInput() \u7684\u53d8\u91cf x \u4e2d\u3002\u4f46\u662f\u53d8\u91cf x \u5374\u5f97\u5230\u4e86\u9519\u8bef\u7684\u503c\u3002\u95ee\u9898\u80af\u5b9a\u5728\u8fd9\u4e24\u70b9\u4e4b\u95f4\u3002\u552f\u4e00\u7684\u7f6a\u9b41\u7978\u9996\u662f\u51fd\u6570 getUserInput() \u7684\u8fd4\u56de\u503c\uff1a 1 return -- x ; \u55ef\uff0c\u8fd9\u5f88\u5947\u602a\u3002 x \u524d\u9762\u7684\u201c -- \u201d\u7b26\u53f7\u662f\u4ec0\u4e48\uff1f\u7531\u4e8e\u6211\u4eec\u7684\u6559\u7a0b\u8fd8\u6ca1\u6709\u8bb2\u5230\u8fd9\u4e00\u70b9\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u5b83\u7684\u610f\u601d\uff0c\u4e5f\u4e0d\u7528\u62c5\u5fc3\u3002\u4f46\u662f\uff0c\u5373\u4f7f\u4e0d\u77e5\u9053\u5b83\u662f\u4ec0\u4e48\u610f\u601d\uff0c\u901a\u8fc7\u8c03\u8bd5\u5de5\u4f5c\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5408\u7406\u5730\u786e\u5b9a\u8fd9\u4e00\u884c\u6709\u95ee\u9898\u2014\u2014\u56e0\u6b64\uff0c\u5f88\u53ef\u80fd\u662f\u8fd9\u4e2a\u201c -- \u201d\u7b26\u53f7\u5bfc\u81f4\u4e86\u95ee\u9898\u3002 \u56e0\u4e3a\u6211\u4eec\u5e0c\u671b getUserInput \u53ea\u8fd4\u56de x \u7684\u503c\uff0c\u6240\u4ee5\u5e72\u8106\u628a -- \u5220\u4e86\u8bd5\u8bd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include int add ( int x , int y ) { std :: cerr << \"add() called (x=\" << x << \", y=\" << y << \") \\n \" ; return x + y ; } void printResult ( int z ) { std :: cerr << \"printResult() called (z=\" << z << \") \\n \" ; std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cerr << \"getUserInput() called \\n \" ; std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; std :: cerr << \"getUserInput::x = \" << x << '\\n' ; return x ; // removed -- before x } int main () { std :: cerr << \"main() called \\n \" ; int x { getUserInput () }; std :: cerr << \"main::x = \" << x << '\\n' ; int y { getUserInput () }; std :: cerr << \"main::y = \" << y << '\\n' ; int z { add ( x , y ) }; std :: cerr << \"main::z = \" << z << '\\n' ; printResult ( z ); return 0 ; } \u8f93\u51fa\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 main() called getUserInput() called Enter a number: 4 getUserInput::x = 4 main::x = 4 getUserInput() called Enter a number: 3 getUserInput::x = 3 main::y = 3 add() called (x=4, y=3) main::z = 7 printResult() called (z=7) The answer is: 7 \u7a0b\u5e8f\u73b0\u5728\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\uff0c\u867d\u7136\u6211\u4eec\u4e0d\u77e5\u9053 -- \u662f\u5e72\u561b\u7684\uff0c\u4f46\u662f\u8fd9\u5e76\u4e0d\u5f71\u54cd\u6211\u4eec\u5b9a\u4f4d\u548c\u4fee\u590d\u8fd9\u4e2a\u95ee\u9898\u3002 \u4e3a\u4ec0\u4e48\u4f7f\u7528\u6253\u5370\u8bed\u53e5\u6765\u8c03\u8bd5\u7a0b\u5e8f\u5e76\u4e0d\u662f\u4e0a\u7b56 \u867d\u7136\u4e3a\u8bca\u65ad\u76ee\u7684\u5411\u7a0b\u5e8f\u6dfb\u52a0\u8c03\u8bd5\u8bed\u53e5\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u57fa\u672c\u6280\u672f\uff0c\u4e5f\u5f88\u6709\u7528(\u7279\u522b\u662f\u5f53\u8c03\u8bd5\u5668\u7531\u4e8e\u67d0\u4e9b\u539f\u56e0\u4e0d\u53ef\u7528\u65f6)\uff0c\u4f46\u7531\u4e8e\u4ee5\u4e0b\u51e0\u4e2a\u539f\u56e0\uff0c\u5b83\u5e76\u4e0d\u662f\u5f88\u597d: \u8c03\u8bd5\u8bed\u53e5\u4f7f\u4ee3\u7801\u53d8\u5f97\u6df7\u4e71\uff1b \u8c03\u8bd5\u8bed\u53e5\u4f7f\u7a0b\u5e8f\u7684\u8f93\u51fa\u53d8\u5f97\u6df7\u4e71\uff1b \u8c03\u8bd5\u8bed\u53e5\u5728\u4f7f\u7528\u5b8c\u4e4b\u540e\u5fc5\u987b\u5220\u9664\uff0c\u8fd9\u4f7f\u5f97\u5b83\u4eec\u4e0d\u53ef\u91cd\u7528\uff1b \u8c03\u8bd5\u8bed\u53e5\u9700\u8981\u4fee\u6539\u4ee3\u7801\u6765\u6dfb\u52a0\u548c\u5220\u9664\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5f15\u5165\u65b0\u7684\u9519\u8bef\u3002 \u4e0b\u8282\u8bfe\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u4e00\u79cd\u66f4\u597d\u7684\u529e\u6cd5\u6765\u8c03\u8bd5\u7a0b\u5e8f\u3002","title":"3.4 \u2014 \u57fa\u672c\u4ee3\u7801\u8c03\u8bd5\u6280\u672f"},{"location":"3-4-Basic-debugging-tactics/#debug-1","text":"\u5148\u4ece\u4e00\u4e2a\u7b80\u5355\u7684\u95ee\u9898\u5f00\u59cb\u3002\u5982\u679c\u7a0b\u5e8f\u8868\u73b0\u51fa\u9519\u8bef\u7684\u884c\u4e3a\uff0c\u6211\u4eec\u53ef\u4ee5\u6ce8\u91ca\u6389\u4e00\u4e9b\u4ee3\u7801\uff0c\u770b\u770b\u95ee\u9898\u662f\u5426\u4ecd\u7136\u5b58\u5728\u3002\u5982\u679c\u95ee\u9898\u4ecd\u7136\u5b58\u5728\uff0c\u5219\u6ce8\u91ca\u6389\u7684\u4ee3\u7801\u80af\u5b9a\u6ca1\u6709\u95ee\u9898\u3002 \u8003\u8651\u4ee5\u4e0b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 int main () { getNames (); // ask user to enter a bunch of names doMaintenance (); // do some random stuff sortNames (); // sort them in alphabetical order printNames (); // print the sorted list of names return 0 ; } \u5047\u8bbe\u8fd9\u4e2a\u7a0b\u5e8f\u5e94\u8be5\u6309\u5b57\u6bcd\u987a\u5e8f\u6253\u5370\u7528\u6237\u8f93\u5165\u7684\u540d\u5b57\uff0c\u4f46\u5b83\u5374\u6309\u76f8\u53cd\u7684\u5b57\u6bcd\u987a\u5e8f\u6253\u5370\u3002\u95ee\u9898\u5728\u54ea\u91cc? getNames \u662f\u5426\u8f93\u5165\u4e86\u9519\u8bef\u7684\u540d\u79f0\uff1f sortNames \u662f\u5426\u5c06\u5b83\u4eec\u53cd\u5411\u6392\u5e8f\uff1f\u8fd8\u662f printNames \u6253\u5370\u7684\u987a\u5e8f\u4e0d\u5bf9\uff1f\u4ee5\u4e0a\u4efb\u4f55\u4e00\u79cd\u731c\u6d4b\u90fd\u6709\u53ef\u80fd\uff0c\u4f46\u662f domainmaintenance() \u591a\u534a\u4e0e\u95ee\u9898\u65e0\u5173\uff0c\u6240\u4ee5\u53ef\u4ee5\u5148\u5c06\u5176\u6ce8\u91ca\u6389\u3002 1 2 3 4 5 6 7 8 9 int main () { getNames (); // ask user to enter a bunch of names // doMaintenance(); // do some random stuff sortNames (); // sort them in alphabetical order printNames (); // print the sorted list of names return 0 ; } \u5982\u679c\u95ee\u9898\u6d88\u5931\u4e86\uff0c\u90a3\u4e48 domainance \u53cd\u5012\u662f\u6709\u95ee\u9898\u7684\uff0c\u5230\u65f6\u5019\u518d\u6ce8\u610f\u529b\u96c6\u4e2d\u5728\u90a3\u91cc\u3002 \u4e0d\u8fc7\uff0c\u95ee\u9898\u5f88\u53ef\u80fd\u8fd8\u4f1a\u5b58\u5728\uff0c\u56e0\u6b64 doMaintenance \u4e0d\u662f\u9519\u8bef\u7684\u6839\u6e90\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u641c\u7d22\u4e2d\u6392\u9664\u6574\u4e2a\u51fd\u6570\u3002\u8fd9\u5e76\u4e0d\u80fd\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u5b9e\u9645\u95ee\u9898\u662f\u53d1\u751f\u5728 doMaintenance \u8c03\u7528\u4e4b\u524d\u8fd8\u662f\u4e4b\u540e\uff0c\u4f46\u7ec8\u5f52\u662f\u7f29\u5c0f\u4e86\u8303\u56f4\u3002 \u4e0d\u8981\u5fd8\u8bb0\u6ce8\u91ca\u6389\u4e86\u54ea\u4e9b\u51fd\u6570\uff0c\u4ee5\u4fbf\u4ee5\u540e\u53ef\u4ee5\u53d6\u6d88\u6ce8\u91ca!","title":"Debug \u6280\u672f 1\uff1a\u6ce8\u91ca\u6389\u4ee3\u7801"},{"location":"3-4-Basic-debugging-tactics/#debug-2","text":"\u590d\u6742\u7a0b\u5e8f\u4e2d\u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u662f\u7a0b\u5e8f\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u7684\u6b21\u6570\u592a\u591a\u6216\u8fc7\u5c11(\u5305\u62ec\u6839\u672c\u4e0d\u8c03\u7528)\u3002 \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5c06\u8bed\u53e5\u653e\u5728\u51fd\u6570\u7684\u9876\u90e8\u4ee5\u6253\u5370\u51fd\u6570\u540d\u4f1a\u5f88\u6709\u5e2e\u52a9\u3002\u8fd9\u6837\uff0c\u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u5c31\u53ef\u4ee5\u6253\u5370\u51fa\u8c03\u7528\u4e86\u54ea\u4e9b\u51fd\u6570\u3002 \u5c0f\u8d34\u58eb \u5f53\u6253\u5370\u7528\u4e8e\u8c03\u8bd5\u76ee\u7684\u7684\u4fe1\u606f\u65f6\uff0c\u4f7f\u7528 std::cerr \u800c\u4e0d\u662f std::cout \u3002\u8fd9\u6837\u505a\u7684\u4e00\u4e2a\u539f\u56e0\u662f std::cout \u53ef\u80fd\u4f1a\u88ab\u7f13\u51b2\uff0c\u8fd9\u610f\u5473\u7740\u5728\u8981\u6c42 std::cout \u6253\u5370\u4fe1\u606f\u548c\u5b83\u5b9e\u9645\u6253\u5370\u4fe1\u606f\u4e4b\u95f4\u53ef\u80fd\u4f1a\u6709\u4e00\u4e2a\u65f6\u95f4\u5dee\u3002\u5982\u679c\u4f7f\u7528 std::cout \u8fdb\u884c\u6253\u5370\u7136\u540e\u7a0b\u5e8f\u7acb\u5373\u5d29\u6e83\uff0c\u90a3\u4e48 std::cout \u53ef\u80fd\u8fd8\u6ca1\u6709\u6765\u5f97\u53ca\u6253\u5370\u3002\u8fd9\u53ef\u80fd\u4f1a\u8bef\u5bfc\u4f60\u3002\u53e6\u4e00\u65b9\u9762\uff0c std::cerr \u662f\u65e0\u7f13\u51b2\u7684\uff0c\u8fd9\u610f\u5473\u7740\u53d1\u9001\u7ed9\u5b83\u7684\u4efb\u4f55\u5185\u5bb9\u90fd\u5c06\u7acb\u5373\u6253\u5370\u3002\u8fd9\u6709\u52a9\u4e8e\u786e\u4fdd\u6240\u6709\u8c03\u8bd5\u8f93\u51fa\u5c3d\u5feb\u51fa\u73b0(\u4ee5\u4e00\u4e9b\u6027\u80fd\u4e3a\u4ee3\u4ef7\uff0c\u6211\u4eec\u5728\u8c03\u8bd5\u65f6\u901a\u5e38\u4e0d\u5173\u7cfb\u6027\u80fd)\u3002 \u4f7f\u7528 std::cerr \u4e5f\u53ef\u4ee5\u8868\u660e\u8be5\u6253\u5370\u4fe1\u606f\u548c\u9519\u8bef\u6d41\u7a0b\u6709\u5173\u3002 \u8003\u8651\u4ee5\u4e0b\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int getValue () { return 4 ; } int main () { std :: cout << getValue ; return 0 ; } \u4f60\u5fc5\u987b\u5173\u95ed \u201c\u5c06\u8b66\u544a\u5f53\u505a\u9519\u8bef\u5904\u7406\u201d \u7684\u7f16\u8bd1\u9009\u9879\uff0c\u624d\u80fd\u7f16\u8bd1\u8be5\u4ee3\u7801\u3002 \u867d\u7136\u6211\u4eec\u671f\u671b\u8fd9\u4e2a\u7a0b\u5e8f\u6253\u5370\u503c 4\uff0c\u4f46\u5b83\u5b9e\u9645\u6253\u5370\u4e86 1 \u3002 \u5728 Visual Studio (\u6216\u8005\u5176\u4ed6\u7f16\u8bd1\u5668)\u4e0a\uff0c\u4e5f\u53ef\u80fd\u6253\u5370\uff1a 1 00101424 \u6dfb\u52a0\u4e00\u4e9b\u8c03\u8bd5\u8bed\u53e5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int getValue () { std :: cerr << \"getValue() called \\n \" ; return 4 ; } int main () { std :: cerr << \"main() called \\n \" ; std :: cout << getValue ; return 0 ; } \u5c0f\u8d34\u58eb \u5728\u6dfb\u52a0\u4e34\u65f6\u8c03\u8bd5\u8bed\u53e5\u65f6\uff0c\u4e0d\u7f29\u8fdb\u4f1a\u5f88\u6709\u5e2e\u52a9\u3002\u8fd9\u6837\u5728\u4e0d\u9700\u8981\u65f6\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u627e\u5230\u5b83\u4eec\u5e76\u5220\u9664\u3002 \u73b0\u5728\uff0c\u51fd\u6570\u6267\u884c\u65f6\u4f1a\u6253\u5370\u5bf9\u5e94\u7684\u51fd\u6570\u540d\uff1a 1 2 main () called 1 \u53ef\u4ee5\u770b\u5230 getValue \u6839\u672c\u6ca1\u88ab\u8c03\u7528\u8fc7\u3002\u90a3\u4e48\u80af\u5b9a\u662f\u8c03\u7528\u8be5\u51fd\u6570\u7684\u4ee3\u7801\u51fa\u4e86\u95ee\u9898\u3002\u4ed4\u7ec6\u770b\u770b\u90a3\u884c\u4ee3\u7801\uff1a 1 std :: cout << getValue ; \u54e6\uff0c\u770b\uff0c\u6211\u4eec\u5fd8\u8bb0\u4e86\u51fd\u6570\u8c03\u7528\u7684\u62ec\u53f7\u3002\u5b83\u5e94\u8be5\u662f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int getValue () { std :: cerr << \"getValue() called \\n \" ; return 4 ; } int main () { std :: cerr << \"main() called \\n \" ; std :: cout << getValue (); // added parenthesis here return 0 ; } \u8fd9\u6837\u4e00\u6765\u5c31\u6ca1\u95ee\u9898\u4e86\uff1a 1 2 3 main() called getValue() called 4 \u6b64\u65f6\u53ef\u4ee5\u5220\u9664\u4e34\u65f6\u8c03\u8bd5\u8bed\u53e5\u4e86\u3002","title":"Debug \u6280\u672f 2\uff1a\u9a8c\u8bc1\u4ee3\u7801\u6d41\u7a0b"},{"location":"3-4-Basic-debugging-tactics/#debug-3","text":"\u5bf9\u4e8e\u67d0\u4e9b\u7c7b\u578b\u7684\u9519\u8bef\uff0c\u7a0b\u5e8f\u53ef\u80fd\u8ba1\u7b97\u6216\u4f20\u9012\u4e86\u9519\u8bef\u7684\u503c\u3002 \u6211\u4eec\u8fd8\u53ef\u4ee5\u8f93\u51fa\u53d8\u91cf(\u5305\u62ec\u53c2\u6570)\u6216\u8868\u8fbe\u5f0f\u7684\u503c\uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u662f\u6b63\u786e\u7684\u3002 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff0c\u5b83\u5e94\u8be5\u628a\u4e24\u4e2a\u6570\u5b57\u76f8\u52a0\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include int add ( int x , int y ) { return x + y ; } void printResult ( int z ) { std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { int x { getUserInput () }; int y { getUserInput () }; std :: cout << x << \" + \" << y << '\\n' ; int z { add ( x , 5 ) }; printResult ( z ); return 0 ; } \u8f93\u51fa\u7ed3\u679c\u4e0d\u6b63\u786e\uff1a 1 2 3 4 Enter a number: 4 Enter a number: 3 4 + 3 The answer is: 9 \u7ed3\u679c\u4e0d\u5bf9\uff0c\u4f46\u4f60\u80fd\u53d1\u73b0\u9519\u8bef\u5417\uff1f\u5373\u4f7f\u5728\u8fd9\u4e2a\u7b80\u77ed\u7684\u7a0b\u5e8f\u4e2d\uff0c\u95ee\u9898\u4e5f\u5f88\u96be\u88ab\u53d1\u73b0\u3002\u63a5\u4e0b\u6765\u6dfb\u52a0\u4e00\u4e9b\u4ee3\u7801\u6765\u6253\u5370\u53d8\u91cf\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include int add ( int x , int y ) { return x + y ; } void printResult ( int z ) { std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { int x { getUserInput () }; std :: cerr << \"main::x = \" << x << '\\n' ; int y { getUserInput () }; std :: cerr << \"main::y = \" << y << '\\n' ; std :: cout << x << \" + \" << y << '\\n' ; int z { add ( x , 5 ) }; std :: cerr << \"main::z = \" << z << '\\n' ; printResult ( z ); return 0 ; } \u8f93\u51fa\uff1a 1 2 3 4 5 6 7 Enter a number: 4 main::x = 4 Enter a number: 3 main::y = 3 4 + 3 main::z = 9 The answer is: 9 \u53d8\u91cf x \u548c y \u7684\u503c\u90fd\u662f\u6b63\u786e\u7684\uff0c\u4f46\u662f z \u7684\u503c\u4e0d\u6b63\u786e\u3002\u6240\u4ee5\u95ee\u9898\u80af\u5b9a\u5728\u8fd9\u9644\u8fd1\uff0c\u8fd9\u6837\u4e00\u6765 add \u51fd\u6570\u5c31\u975e\u5e38\u53ef\u7591\u4e86\u3002 \u4fee\u6539\u4e00\u4e0b add \u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include int add ( int x , int y ) { std :: cerr << \"add() called (x=\" << x << \", y=\" << y << \") \\n \" ; return x + y ; } void printResult ( int z ) { std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { int x { getUserInput () }; std :: cerr << \"main::x = \" << x << '\\n' ; int y { getUserInput () }; std :: cerr << \"main::y = \" << y << '\\n' ; std :: cout << x << \" + \" << y << '\\n' ; int z { add ( x , 5 ) }; std :: cerr << \"main::z = \" << z << '\\n' ; printResult ( z ); return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 5 6 7 Enter a number: 4 main::x = 4 Enter a number: 3 main::y = 3 add() called (x=4, y=5) main::z = 9 The answer is: 9 \u53d8\u91cf y \u7684\u503c\u662f3\uff0c\u4f46\u662f\u6211\u4eec\u7684\u51fd\u6570 add \u7684\u53c2\u6570 y \u7684\u503c\u662f 5 \u3002\u6211\u4eec\u80af\u5b9a\u4f20\u9012\u4e86\u9519\u8bef\u7684\u53c2\u6570\u3002\u679c\u7136\uff1a 1 int z { add ( x , 5 ) }; \u627e\u5230\u4e86\uff01\u5728\u8c03\u7528 add \u51fd\u6570\u7684\u65f6\u5019\u6211\u4eec\u4f20\u9012\u4e86\u5b57\u9762\u91cf 5 \u800c\u4e0d\u662f\u53d8\u91cf y \u3002\u4fee\u6539\u8fd9\u4e2a\u95ee\u9898\u53ea\u9700\u8981\u628a\u8fd9\u91cc\u7684\u5b9e\u53c2\u6539\u6389\u5c31\u53ef\u4ee5\u4e86\u3002 \u518d\u770b\u4e00\u4e2a\u4f8b\u5b50\uff1a \u8fd9\u4e2a\u7a0b\u5e8f\u4e0e\u524d\u9762\u7684\u7a0b\u5e8f\u975e\u5e38\u76f8\u4f3c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include int add ( int x , int y ) { return x + y ; } void printResult ( int z ) { std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return -- x ; } int main () { int x { getUserInput () }; int y { getUserInput () }; int z { add ( x , y ) }; printResult ( z ); return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 Enter a number: 4 Enter a number: 3 The answer is: 5 \u7ed3\u679c\u4e0d\u5bf9\uff0c\u4ec0\u4e48\u539f\u56e0\u5462\uff1f \u8ba9\u6211\u4eec\u5bf9\u8fd9\u6bb5\u4ee3\u7801\u8fdb\u884c\u4e00\u4e9b\u8c03\u8bd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include int add ( int x , int y ) { std :: cerr << \"add() called (x=\" << x << \", y=\" << y << \") \\n \" ; return x + y ; } void printResult ( int z ) { std :: cerr << \"printResult() called (z=\" << z << \") \\n \" ; std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cerr << \"getUserInput() called \\n \" ; std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return -- x ; } int main () { std :: cerr << \"main() called \\n \" ; int x { getUserInput () }; std :: cerr << \"main::x = \" << x << '\\n' ; int y { getUserInput () }; std :: cerr << \"main::y = \" << y << '\\n' ; int z { add ( x , y ) }; std :: cerr << \"main::z = \" << z << '\\n' ; printResult ( z ); return 0 ; } \u73b0\u5728\u8ba9\u6211\u4eec\u7528\u76f8\u540c\u7684\u8f93\u5165\u518d\u6b21\u8fd0\u884c\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 main() called getUserInput() called Enter a number: 4 main::x = 3 getUserInput() called Enter a number: 3 main::y = 2 add() called (x=3, y=2) main::z = 5 printResult() called (z=5) The answer is: 5 \u8fd9\u6837\u4e00\u6765\u5c31\u53ef\u4ee5\u770b\u51fa\u95ee\u9898\u4e86\uff1a\u7528\u6237\u8f93\u5165\u7684\u503c\u662f 4 \uff0c\u4f46\u662f main \u51fd\u6570\u4e2d x \u5f97\u5230\u7684\u503c\u5374\u662f 3 \u3002\u5728\u7528\u6237\u8f93\u5165\u7684\u4f4d\u7f6e\u548c\u7ed9 x \u8d4b\u503c\u4e4b\u95f4\u7684\u4ee3\u7801\u80af\u5b9a\u51fa\u4e86\u95ee\u9898\u3002\u5728\u51fd\u6570 getUserInput \u4e2d\u6dfb\u52a0\u4e00\u4e9b\u8c03\u8bd5\u4ee3\u7801\u6765\uff0c\u786e\u5b9a\u7a0b\u5e8f\u662f\u5426\u4ece\u7528\u6237\u90a3\u91cc\u83b7\u5f97\u4e86\u6b63\u786e\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include int add ( int x , int y ) { std :: cerr << \"add() called (x=\" << x << \", y=\" << y << \") \\n \" ; return x + y ; } void printResult ( int z ) { std :: cerr << \"printResult() called (z=\" << z << \") \\n \" ; std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cerr << \"getUserInput() called \\n \" ; std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; std :: cerr << \"getUserInput::x = \" << x << '\\n' ; // \u6dfb\u52a0\u8c03\u8bd5\u4fe1\u606f return -- x ; } int main () { std :: cerr << \"main() called \\n \" ; int x { getUserInput () }; std :: cerr << \"main::x = \" << x << '\\n' ; int y { getUserInput () }; std :: cerr << \"main::y = \" << y << '\\n' ; int z { add ( x , y ) }; std :: cerr << \"main::z = \" << z << '\\n' ; printResult ( z ); return 0 ; } \u8f93\u51fa\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 main() called getUserInput() called Enter a number: 4 getUserInput::x = 4 main::x = 3 getUserInput() called Enter a number: 3 getUserInput::x = 3 main::y = 2 add() called (x=3, y=2) main::z = 5 printResult() called (z=5) The answer is: 5 \u6dfb\u52a0\u8fd9\u884c\u8c03\u8bd5\u4ee3\u7801\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u7528\u6237\u8f93\u5165\u88ab\u6b63\u786e\u5730\u63a5\u6536\u5230 getUserInput() \u7684\u53d8\u91cf x \u4e2d\u3002\u4f46\u662f\u53d8\u91cf x \u5374\u5f97\u5230\u4e86\u9519\u8bef\u7684\u503c\u3002\u95ee\u9898\u80af\u5b9a\u5728\u8fd9\u4e24\u70b9\u4e4b\u95f4\u3002\u552f\u4e00\u7684\u7f6a\u9b41\u7978\u9996\u662f\u51fd\u6570 getUserInput() \u7684\u8fd4\u56de\u503c\uff1a 1 return -- x ; \u55ef\uff0c\u8fd9\u5f88\u5947\u602a\u3002 x \u524d\u9762\u7684\u201c -- \u201d\u7b26\u53f7\u662f\u4ec0\u4e48\uff1f\u7531\u4e8e\u6211\u4eec\u7684\u6559\u7a0b\u8fd8\u6ca1\u6709\u8bb2\u5230\u8fd9\u4e00\u70b9\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u5b83\u7684\u610f\u601d\uff0c\u4e5f\u4e0d\u7528\u62c5\u5fc3\u3002\u4f46\u662f\uff0c\u5373\u4f7f\u4e0d\u77e5\u9053\u5b83\u662f\u4ec0\u4e48\u610f\u601d\uff0c\u901a\u8fc7\u8c03\u8bd5\u5de5\u4f5c\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5408\u7406\u5730\u786e\u5b9a\u8fd9\u4e00\u884c\u6709\u95ee\u9898\u2014\u2014\u56e0\u6b64\uff0c\u5f88\u53ef\u80fd\u662f\u8fd9\u4e2a\u201c -- \u201d\u7b26\u53f7\u5bfc\u81f4\u4e86\u95ee\u9898\u3002 \u56e0\u4e3a\u6211\u4eec\u5e0c\u671b getUserInput \u53ea\u8fd4\u56de x \u7684\u503c\uff0c\u6240\u4ee5\u5e72\u8106\u628a -- \u5220\u4e86\u8bd5\u8bd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include int add ( int x , int y ) { std :: cerr << \"add() called (x=\" << x << \", y=\" << y << \") \\n \" ; return x + y ; } void printResult ( int z ) { std :: cerr << \"printResult() called (z=\" << z << \") \\n \" ; std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cerr << \"getUserInput() called \\n \" ; std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; std :: cerr << \"getUserInput::x = \" << x << '\\n' ; return x ; // removed -- before x } int main () { std :: cerr << \"main() called \\n \" ; int x { getUserInput () }; std :: cerr << \"main::x = \" << x << '\\n' ; int y { getUserInput () }; std :: cerr << \"main::y = \" << y << '\\n' ; int z { add ( x , y ) }; std :: cerr << \"main::z = \" << z << '\\n' ; printResult ( z ); return 0 ; } \u8f93\u51fa\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 main() called getUserInput() called Enter a number: 4 getUserInput::x = 4 main::x = 4 getUserInput() called Enter a number: 3 getUserInput::x = 3 main::y = 3 add() called (x=4, y=3) main::z = 7 printResult() called (z=7) The answer is: 7 \u7a0b\u5e8f\u73b0\u5728\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\uff0c\u867d\u7136\u6211\u4eec\u4e0d\u77e5\u9053 -- \u662f\u5e72\u561b\u7684\uff0c\u4f46\u662f\u8fd9\u5e76\u4e0d\u5f71\u54cd\u6211\u4eec\u5b9a\u4f4d\u548c\u4fee\u590d\u8fd9\u4e2a\u95ee\u9898\u3002","title":"Debug \u6280\u672f 3\uff1a \u6253\u5370\u503c"},{"location":"3-4-Basic-debugging-tactics/#_1","text":"\u867d\u7136\u4e3a\u8bca\u65ad\u76ee\u7684\u5411\u7a0b\u5e8f\u6dfb\u52a0\u8c03\u8bd5\u8bed\u53e5\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u57fa\u672c\u6280\u672f\uff0c\u4e5f\u5f88\u6709\u7528(\u7279\u522b\u662f\u5f53\u8c03\u8bd5\u5668\u7531\u4e8e\u67d0\u4e9b\u539f\u56e0\u4e0d\u53ef\u7528\u65f6)\uff0c\u4f46\u7531\u4e8e\u4ee5\u4e0b\u51e0\u4e2a\u539f\u56e0\uff0c\u5b83\u5e76\u4e0d\u662f\u5f88\u597d: \u8c03\u8bd5\u8bed\u53e5\u4f7f\u4ee3\u7801\u53d8\u5f97\u6df7\u4e71\uff1b \u8c03\u8bd5\u8bed\u53e5\u4f7f\u7a0b\u5e8f\u7684\u8f93\u51fa\u53d8\u5f97\u6df7\u4e71\uff1b \u8c03\u8bd5\u8bed\u53e5\u5728\u4f7f\u7528\u5b8c\u4e4b\u540e\u5fc5\u987b\u5220\u9664\uff0c\u8fd9\u4f7f\u5f97\u5b83\u4eec\u4e0d\u53ef\u91cd\u7528\uff1b \u8c03\u8bd5\u8bed\u53e5\u9700\u8981\u4fee\u6539\u4ee3\u7801\u6765\u6dfb\u52a0\u548c\u5220\u9664\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5f15\u5165\u65b0\u7684\u9519\u8bef\u3002 \u4e0b\u8282\u8bfe\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u4e00\u79cd\u66f4\u597d\u7684\u529e\u6cd5\u6765\u8c03\u8bd5\u7a0b\u5e8f\u3002","title":"\u4e3a\u4ec0\u4e48\u4f7f\u7528\u6253\u5370\u8bed\u53e5\u6765\u8c03\u8bd5\u7a0b\u5e8f\u5e76\u4e0d\u662f\u4e0a\u7b56"},{"location":"3-5-More-debugging-tactics/","tags":["debugging"],"text":"\u5728\u4e0a\u8282\u8bfe ( 3.4 \u2014 \u57fa\u672c\u4ee3\u7801\u8c03\u8bd5\u6280\u672f ) \u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u624b\u52a8\u8c03\u8bd5\u4ee3\u7801\u3002\u5728\u8fd9\u8282\u8bfe\u4e2d\u6211\u4eec\u63d0\u4f9b\u4e00\u79cd\u65b0\u7684\u65b9\u5f0f\uff0c\u5b83\u53ef\u4ee5\u907f\u514d\u8c03\u8bd5\u8bed\u53e5\u5e26\u6765\u7684\u95ee\u9898\uff1a \u8c03\u8bd5\u8bed\u53e5\u4f7f\u4ee3\u7801\u53d8\u5f97\u6df7\u4e71\uff1b \u8c03\u8bd5\u8bed\u53e5\u4f7f\u7a0b\u5e8f\u7684\u8f93\u51fa\u53d8\u5f97\u6df7\u4e71\uff1b \u8c03\u8bd5\u8bed\u53e5\u5728\u4f7f\u7528\u5b8c\u4e4b\u540e\u5fc5\u987b\u5220\u9664\uff0c\u8fd9\u4f7f\u5f97\u5b83\u4eec\u4e0d\u53ef\u91cd\u7528\uff1b \u8c03\u8bd5\u8bed\u53e5\u9700\u8981\u4fee\u6539\u4ee3\u7801\u6765\u6dfb\u52a0\u548c\u5220\u9664\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5f15\u5165\u65b0\u7684\u9519\u8bef\u3002 \u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u7d22\u8fd9\u6837\u505a\u7684\u4e00\u4e9b\u57fa\u672c\u6280\u5de7\u3002 \u4e3a\u8c03\u8bd5\u4ee3\u7801\u6dfb\u52a0\u6267\u884c\u6761\u4ef6 \u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int getUserInput () { std :: cerr << \"getUserInput() called \\n \" ; std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { std :: cerr << \"main() called \\n \" ; int x { getUserInput () }; std :: cout << \"You entered: \" << x ; return 0 ; } \u5f53\u5b8c\u6210\u95ee\u9898\u5b9a\u4f4d\u540e\uff0c \u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\u7684\u8c03\u8bd5\u8bed\u53e5\u90fd\u9700\u8981\u88ab\u5220\u6389\u6216\u8005\u6ce8\u91ca\u6389\u3002\u4e07\u4e00\u5c06\u6765\u4f60\u8fd8\u9700\u8981\u5728\u8fd9\u91cc\u5b9a\u4f4d\u95ee\u9898\uff0c\u90a3\u4e48\u8fd8\u5f97\u628a\u5b83\u4eec\u52a0\u56de\u6765\u6216\u8005\u653e\u5f00\u5b83\u4eec\u7684\u6ce8\u91ca\u3002 \u4e00\u79cd\u66f4\u5bb9\u6613\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u7981\u7528\u548c\u542f\u7528\u8c03\u8bd5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u9884\u5904\u7406\u5668\u6307\u4ee4\u4f7f\u8c03\u8bd5\u8bed\u53e5\u5177\u6709\u6761\u4ef6: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #define ENABLE_DEBUG // comment out to disable debugging int getUserInput () { #ifdef ENABLE_DEBUG std :: cerr << \"getUserInput() called \\n \" ; #endif std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { #ifdef ENABLE_DEBUG std :: cerr << \"main() called \\n \" ; #endif int x { getUserInput () }; std :: cout << \"You entered: \" << x ; return 0 ; } \u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u53ea\u9700\u8981\u6ce8\u91ca\u6216\u8005\u53d6\u6d88\u5bf9 #define ENABLE_DEBUG \u7684\u6ce8\u91ca\uff0c\u5373\u53ef\u91cd\u7528\u4e4b\u524d\u5168\u90e8\u7684\u8c03\u8bd5\u8bed\u53e5\uff0c\u800c\u4e0d\u9700\u8981\u4f9d\u6b21\u5bf9\u5176\u8fdb\u884c\u6dfb\u52a0\u548c\u5220\u9664\u3002\u5982\u679c\u4f60\u7684\u9879\u76ee\u4e2d\u5305\u542b\u591a\u4e2a\u6587\u4ef6\uff0c\u5219\u53ef\u4ee5\u628a #define ENABLE_DEBUG \u5b9a\u4e49\u5230\u5934\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u88ab\u5176\u4ed6\u6587\u4ef6\u5305\u542b\uff0c\u8fd9\u6837\u4e00\u6765\u5728\u4e00\u5904\u5bf9\u6b64\u5b8f\u5b9a\u4e49\u8fdb\u884c\u6ce8\u91ca\u5c31\u53ef\u4ee5\u5c06\u6548\u679c\u5e94\u7528\u5230\u5168\u90e8\u4ee3\u7801\u6587\u4ef6\u3002 \u8fd9\u89e3\u51b3\u4e86\u5fc5\u987b\u5220\u9664\u8c03\u8bd5\u8bed\u53e5\u7684\u95ee\u9898\u53ca\u5176\u6f5c\u5728\u7684\u98ce\u9669\uff0c\u4f46\u4ee3\u4ef7\u662f\u4ee3\u7801\u4f1a\u66f4\u52a0\u6df7\u4e71\u3002\u53e6\u4e00\u4e2a\u7f3a\u70b9\u662f\uff0c\u5982\u679c\u4f60\u72af\u4e86\u4e00\u4e2a\u62fc\u5199\u9519\u8bef(\u4f8b\u5982\u62fc\u9519\u4e86\u201cDEBUG '\u201d)\u6216\u5fd8\u8bb0\u5c06\u5934\u6587\u4ef6\u5305\u542b\u5230\u4e00\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\uff0c\u8be5\u6587\u4ef6\u7684\u90e8\u5206\u6216\u5168\u90e8\u8c03\u8bd5\u53ef\u80fd\u65e0\u6cd5\u542f\u7528\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u8fd9\u6bd4\u65e0\u6761\u4ef6\u7684\u7248\u672c\u66f4\u597d\uff0c\u4f46\u4ecd\u6709\u6539\u8fdb\u7684\u7a7a\u95f4\u3002 \u4f7f\u7528\u65e5\u5fd7 \u9664\u4e86\u901a\u8fc7\u9884\u5904\u7406\u5668\u8fdb\u884c\u6761\u4ef6\u5316\u8c03\u8bd5\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5c06\u8c03\u8bd5\u4fe1\u606f\u53d1\u9001\u5230\u65e5\u5fd7\u6587\u4ef6\u3002\u65e5\u5fd7\u6587\u4ef6\u662f\u8bb0\u5f55\u8f6f\u4ef6\u4e2d\u53d1\u751f\u7684\u4e8b\u4ef6\u7684\u6587\u4ef6(\u901a\u5e38\u5b58\u50a8\u5728\u78c1\u76d8\u4e0a)\u3002\u5c06\u4fe1\u606f\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\u7684\u8fc7\u7a0b\u79f0\u4e3a\u65e5\u5fd7\u8bb0\u5f55\u3002\u5927\u591a\u6570\u5e94\u7528\u7a0b\u5e8f\u548c\u64cd\u4f5c\u7cfb\u7edf\u90fd\u4f1a\u7f16\u5199\u65e5\u5fd7\u6587\u4ef6\uff0c\u7528\u4e8e\u5e2e\u52a9\u8bca\u65ad\u53d1\u751f\u7684\u95ee\u9898\u3002 \u56e0\u4e3a\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\u7684\u4fe1\u606f\u4e0e\u7a0b\u5e8f\u7684\u8f93\u51fa\u662f\u5206\u5f00\u7684\uff0c\u6240\u4ee5\u53ef\u4ee5\u907f\u514d\u6df7\u5408\u6b63\u5e38\u8f93\u51fa\u548c\u8c03\u8bd5\u8f93\u51fa\u6240\u9020\u6210\u7684\u6df7\u4e71\u3002\u65e5\u5fd7\u6587\u4ef6\u4e5f\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u53d1\u9001\u7ed9\u5176\u4ed6\u4eba\u8fdb\u884c\u8bca\u65ad\u2014\u2014\u56e0\u6b64\uff0c\u5982\u679c\u8f6f\u4ef6\u5ba2\u6237\u9047\u5230\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u8981\u6c42\u4ed6\u4eec\u5c06\u65e5\u5fd7\u6587\u4ef6\u53d1\u8fc7\u6765\uff0c\u8fd9\u53ef\u80fd\u6709\u52a9\u4e8e\u4e3a\u4f60\u63d0\u4f9b\u95ee\u9898\u7684\u7ebf\u7d22\u3002 \u867d\u7136\u4f60\u53ef\u4ee5\u81ea\u5df1\u7f16\u5199\u4ee3\u7801\u6765\u521b\u5efa\u65e5\u5fd7\u6587\u4ef6\u5e76\u5411\u5b83\u4eec\u53d1\u9001\u8f93\u51fa\uff0c\u4f46\u6211\u4eec\u8fd8\u662f\u63a8\u8350\u4f7f\u7528\u73b0\u6709\u7684\u8bb8\u591a\u7b2c\u4e09\u65b9\u65e5\u5fd7\u5de5\u5177\uff0c\u5177\u4f53\u7528\u54ea\u4e00\u4e2a\u53d6\u51b3\u4e8e\u4f60\u81ea\u5df1\u3002 \u6211\u4eec\u4f1a\u4f7f\u7528 plog \u65e5\u5fd7\u8bb0\u5f55\u5668\u8f93\u51fa\u5230\u65e5\u5fd7\u8bb0\u5f55\u5668\u7684\u6837\u5b50\u3002plog\u662f\u4e00\u4e2a\u7eaf\u5934\u6587\u4ef6\u5b9e\u73b0\u7684\u5e93\uff0c\u56e0\u6b64\u5f88\u5bb9\u6613\u5728\u4efb\u4f55\u9700\u8981\u5b83\u7684\u5730\u65b9\u5305\u542b\u5b83\uff0c\u800c\u4e14\u5b83\u662f\u8f7b\u91cf\u7ea7\u7684\uff0c\u6613\u4e8e\u4f7f\u7528\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include // Step 1: include the logger headers #include int getUserInput () { PLOGD << \"getUserInput() called\" ; // PLOGD is defined by the plog library std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { plog :: init ( plog :: debug , \"Logfile.txt\" ); // Step 2: initialize the logger PLOGD << \"main() called\" ; // Step 3: Output to the log as if you were writing to the console int x { getUserInput () }; std :: cout << \"You entered: \" << x ; return 0 ; } \u4ee3\u7801\u8f93\u51fa\u7684\u65e5\u5fd7\u5982\u4e0b\u6240\u793a(\u5728 Logfile.txt \u6587\u4ef6\u4e2d): 1 2 2018-12-26 20:03:33.295 DEBUG [4752] [main@14] main() called 2018-12-26 20:03:33.296 DEBUG [4752] [getUserInput@4] getUserInput() called \u5982\u4f55\u5305\u542b\u3001\u521d\u59cb\u5316\u548c\u4f7f\u7528\u8bb0\u5f55\u5668\u53d6\u51b3\u4e8e\u4f60\u9009\u62e9\u7684\u65e5\u5fd7\u5de5\u5177\u3002 \u6ce8\u610f\uff0c\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u4e0d\u9700\u8981\u6761\u4ef6\u7f16\u8bd1\u6307\u4ee4\uff0c\u56e0\u4e3a\u5927\u591a\u6570\u8bb0\u5f55\u5668\u90fd\u6709\u4e00\u79cd\u65b9\u6cd5\u6765\u51cf\u5c11/\u6d88\u9664\u5bf9\u65e5\u5fd7\u7684\u5199\u5165\u8f93\u51fa\u3002\u8fd9\u4f7f\u5f97\u4ee3\u7801\u66f4\u5bb9\u6613\u9605\u8bfb\uff0c\u56e0\u4e3a\u6761\u4ef6\u7f16\u8bd1\u884c\u589e\u52a0\u4e86\u5f88\u591a\u6df7\u4e71\u3002\u4f7f\u7528 plog \uff0c\u53ef\u4ee5\u901a\u8fc7\u66f4\u6539 init \u8bed\u53e5\u6765\u4e34\u65f6\u7981\u7528\u65e5\u5fd7\u8bb0\u5f55: 1 plog :: init ( plog :: none , \"Logfile.txt\" ); // plog::none eliminates writing of most messages, essentially turning logging off \u6211\u4eec\u4e0d\u4f1a\u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\u4f7f\u7528plog\uff0c\u6240\u4ee5\u4f60\u4e0d\u9700\u8981\u62c5\u5fc3\u5b66\u4e60\u5b83\u3002 \u9898\u5916\u8bdd \u5982\u679c\u4f60\u60f3\u81ea\u5df1\u7f16\u8bd1\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u6216\u8005\u5728\u4f60\u81ea\u5df1\u7684\u9879\u76ee\u4e2d\u4f7f\u7528plog\uff0c\u53ef\u4ee5\u6309\u7167\u8fd9\u4e9b\u8bf4\u660e\u6765\u5b89\u88c5\u5b83: \u9996\u5148\uff0c\u83b7\u53d6\u6700\u65b0\u7248\u672c\u7684 plog: - \u8bbf\u95ee plog \u7684 Github \u4ed3\u5e93\uff1b - \u70b9\u51fb\u53f3\u4e0a\u89d2\u7684\u7eff\u8272Code\u6309\u94ae\uff0c\u7136\u540e\u9009\u62e9\u201cDownload zip\u201d\u3002 \u63a5\u4e0b\u6765\uff0c\u5728\u7535\u8111\u4e0a\u89e3\u538b\u538b\u7f29\u5305\uff08\u5047\u8bbe\u662f somewhere \uff09\u3002 \u6700\u540e\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u9700\u8981\u4f7f\u7528\u5b83\u7684\u9879\u76ee\uff0c\u5728IDE\u7684 include directory \u8bbe\u7f6e somewhere\\plog-master\\include\\ \u3002\u5982\u679c\u4f60\u4f7f\u7528 Visual Studio \u53ef\u4ee5\u53c2\u8003\uff1a A.2 - \u5728visual studio\u4e2d\u4f7f\u7528\u5e93 \uff0c\u5982\u679c\u662fCode::Blocks\u5219\u53c2\u8003 A.3 - \u5728Code Blocks\u4e2d\u4f7f\u7528\u5e93 \u3002","title":"3.5 - \u66f4\u591a\u4ee3\u7801\u8c03\u8bd5\u6280\u672f"},{"location":"3-5-More-debugging-tactics/#_1","text":"\u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int getUserInput () { std :: cerr << \"getUserInput() called \\n \" ; std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { std :: cerr << \"main() called \\n \" ; int x { getUserInput () }; std :: cout << \"You entered: \" << x ; return 0 ; } \u5f53\u5b8c\u6210\u95ee\u9898\u5b9a\u4f4d\u540e\uff0c \u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\u7684\u8c03\u8bd5\u8bed\u53e5\u90fd\u9700\u8981\u88ab\u5220\u6389\u6216\u8005\u6ce8\u91ca\u6389\u3002\u4e07\u4e00\u5c06\u6765\u4f60\u8fd8\u9700\u8981\u5728\u8fd9\u91cc\u5b9a\u4f4d\u95ee\u9898\uff0c\u90a3\u4e48\u8fd8\u5f97\u628a\u5b83\u4eec\u52a0\u56de\u6765\u6216\u8005\u653e\u5f00\u5b83\u4eec\u7684\u6ce8\u91ca\u3002 \u4e00\u79cd\u66f4\u5bb9\u6613\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u7981\u7528\u548c\u542f\u7528\u8c03\u8bd5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u9884\u5904\u7406\u5668\u6307\u4ee4\u4f7f\u8c03\u8bd5\u8bed\u53e5\u5177\u6709\u6761\u4ef6: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #define ENABLE_DEBUG // comment out to disable debugging int getUserInput () { #ifdef ENABLE_DEBUG std :: cerr << \"getUserInput() called \\n \" ; #endif std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { #ifdef ENABLE_DEBUG std :: cerr << \"main() called \\n \" ; #endif int x { getUserInput () }; std :: cout << \"You entered: \" << x ; return 0 ; } \u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u53ea\u9700\u8981\u6ce8\u91ca\u6216\u8005\u53d6\u6d88\u5bf9 #define ENABLE_DEBUG \u7684\u6ce8\u91ca\uff0c\u5373\u53ef\u91cd\u7528\u4e4b\u524d\u5168\u90e8\u7684\u8c03\u8bd5\u8bed\u53e5\uff0c\u800c\u4e0d\u9700\u8981\u4f9d\u6b21\u5bf9\u5176\u8fdb\u884c\u6dfb\u52a0\u548c\u5220\u9664\u3002\u5982\u679c\u4f60\u7684\u9879\u76ee\u4e2d\u5305\u542b\u591a\u4e2a\u6587\u4ef6\uff0c\u5219\u53ef\u4ee5\u628a #define ENABLE_DEBUG \u5b9a\u4e49\u5230\u5934\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u88ab\u5176\u4ed6\u6587\u4ef6\u5305\u542b\uff0c\u8fd9\u6837\u4e00\u6765\u5728\u4e00\u5904\u5bf9\u6b64\u5b8f\u5b9a\u4e49\u8fdb\u884c\u6ce8\u91ca\u5c31\u53ef\u4ee5\u5c06\u6548\u679c\u5e94\u7528\u5230\u5168\u90e8\u4ee3\u7801\u6587\u4ef6\u3002 \u8fd9\u89e3\u51b3\u4e86\u5fc5\u987b\u5220\u9664\u8c03\u8bd5\u8bed\u53e5\u7684\u95ee\u9898\u53ca\u5176\u6f5c\u5728\u7684\u98ce\u9669\uff0c\u4f46\u4ee3\u4ef7\u662f\u4ee3\u7801\u4f1a\u66f4\u52a0\u6df7\u4e71\u3002\u53e6\u4e00\u4e2a\u7f3a\u70b9\u662f\uff0c\u5982\u679c\u4f60\u72af\u4e86\u4e00\u4e2a\u62fc\u5199\u9519\u8bef(\u4f8b\u5982\u62fc\u9519\u4e86\u201cDEBUG '\u201d)\u6216\u5fd8\u8bb0\u5c06\u5934\u6587\u4ef6\u5305\u542b\u5230\u4e00\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\uff0c\u8be5\u6587\u4ef6\u7684\u90e8\u5206\u6216\u5168\u90e8\u8c03\u8bd5\u53ef\u80fd\u65e0\u6cd5\u542f\u7528\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u8fd9\u6bd4\u65e0\u6761\u4ef6\u7684\u7248\u672c\u66f4\u597d\uff0c\u4f46\u4ecd\u6709\u6539\u8fdb\u7684\u7a7a\u95f4\u3002","title":"\u4e3a\u8c03\u8bd5\u4ee3\u7801\u6dfb\u52a0\u6267\u884c\u6761\u4ef6"},{"location":"3-5-More-debugging-tactics/#_2","text":"\u9664\u4e86\u901a\u8fc7\u9884\u5904\u7406\u5668\u8fdb\u884c\u6761\u4ef6\u5316\u8c03\u8bd5\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5c06\u8c03\u8bd5\u4fe1\u606f\u53d1\u9001\u5230\u65e5\u5fd7\u6587\u4ef6\u3002\u65e5\u5fd7\u6587\u4ef6\u662f\u8bb0\u5f55\u8f6f\u4ef6\u4e2d\u53d1\u751f\u7684\u4e8b\u4ef6\u7684\u6587\u4ef6(\u901a\u5e38\u5b58\u50a8\u5728\u78c1\u76d8\u4e0a)\u3002\u5c06\u4fe1\u606f\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\u7684\u8fc7\u7a0b\u79f0\u4e3a\u65e5\u5fd7\u8bb0\u5f55\u3002\u5927\u591a\u6570\u5e94\u7528\u7a0b\u5e8f\u548c\u64cd\u4f5c\u7cfb\u7edf\u90fd\u4f1a\u7f16\u5199\u65e5\u5fd7\u6587\u4ef6\uff0c\u7528\u4e8e\u5e2e\u52a9\u8bca\u65ad\u53d1\u751f\u7684\u95ee\u9898\u3002 \u56e0\u4e3a\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\u7684\u4fe1\u606f\u4e0e\u7a0b\u5e8f\u7684\u8f93\u51fa\u662f\u5206\u5f00\u7684\uff0c\u6240\u4ee5\u53ef\u4ee5\u907f\u514d\u6df7\u5408\u6b63\u5e38\u8f93\u51fa\u548c\u8c03\u8bd5\u8f93\u51fa\u6240\u9020\u6210\u7684\u6df7\u4e71\u3002\u65e5\u5fd7\u6587\u4ef6\u4e5f\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u53d1\u9001\u7ed9\u5176\u4ed6\u4eba\u8fdb\u884c\u8bca\u65ad\u2014\u2014\u56e0\u6b64\uff0c\u5982\u679c\u8f6f\u4ef6\u5ba2\u6237\u9047\u5230\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u8981\u6c42\u4ed6\u4eec\u5c06\u65e5\u5fd7\u6587\u4ef6\u53d1\u8fc7\u6765\uff0c\u8fd9\u53ef\u80fd\u6709\u52a9\u4e8e\u4e3a\u4f60\u63d0\u4f9b\u95ee\u9898\u7684\u7ebf\u7d22\u3002 \u867d\u7136\u4f60\u53ef\u4ee5\u81ea\u5df1\u7f16\u5199\u4ee3\u7801\u6765\u521b\u5efa\u65e5\u5fd7\u6587\u4ef6\u5e76\u5411\u5b83\u4eec\u53d1\u9001\u8f93\u51fa\uff0c\u4f46\u6211\u4eec\u8fd8\u662f\u63a8\u8350\u4f7f\u7528\u73b0\u6709\u7684\u8bb8\u591a\u7b2c\u4e09\u65b9\u65e5\u5fd7\u5de5\u5177\uff0c\u5177\u4f53\u7528\u54ea\u4e00\u4e2a\u53d6\u51b3\u4e8e\u4f60\u81ea\u5df1\u3002 \u6211\u4eec\u4f1a\u4f7f\u7528 plog \u65e5\u5fd7\u8bb0\u5f55\u5668\u8f93\u51fa\u5230\u65e5\u5fd7\u8bb0\u5f55\u5668\u7684\u6837\u5b50\u3002plog\u662f\u4e00\u4e2a\u7eaf\u5934\u6587\u4ef6\u5b9e\u73b0\u7684\u5e93\uff0c\u56e0\u6b64\u5f88\u5bb9\u6613\u5728\u4efb\u4f55\u9700\u8981\u5b83\u7684\u5730\u65b9\u5305\u542b\u5b83\uff0c\u800c\u4e14\u5b83\u662f\u8f7b\u91cf\u7ea7\u7684\uff0c\u6613\u4e8e\u4f7f\u7528\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include // Step 1: include the logger headers #include int getUserInput () { PLOGD << \"getUserInput() called\" ; // PLOGD is defined by the plog library std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { plog :: init ( plog :: debug , \"Logfile.txt\" ); // Step 2: initialize the logger PLOGD << \"main() called\" ; // Step 3: Output to the log as if you were writing to the console int x { getUserInput () }; std :: cout << \"You entered: \" << x ; return 0 ; } \u4ee3\u7801\u8f93\u51fa\u7684\u65e5\u5fd7\u5982\u4e0b\u6240\u793a(\u5728 Logfile.txt \u6587\u4ef6\u4e2d): 1 2 2018-12-26 20:03:33.295 DEBUG [4752] [main@14] main() called 2018-12-26 20:03:33.296 DEBUG [4752] [getUserInput@4] getUserInput() called \u5982\u4f55\u5305\u542b\u3001\u521d\u59cb\u5316\u548c\u4f7f\u7528\u8bb0\u5f55\u5668\u53d6\u51b3\u4e8e\u4f60\u9009\u62e9\u7684\u65e5\u5fd7\u5de5\u5177\u3002 \u6ce8\u610f\uff0c\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u4e0d\u9700\u8981\u6761\u4ef6\u7f16\u8bd1\u6307\u4ee4\uff0c\u56e0\u4e3a\u5927\u591a\u6570\u8bb0\u5f55\u5668\u90fd\u6709\u4e00\u79cd\u65b9\u6cd5\u6765\u51cf\u5c11/\u6d88\u9664\u5bf9\u65e5\u5fd7\u7684\u5199\u5165\u8f93\u51fa\u3002\u8fd9\u4f7f\u5f97\u4ee3\u7801\u66f4\u5bb9\u6613\u9605\u8bfb\uff0c\u56e0\u4e3a\u6761\u4ef6\u7f16\u8bd1\u884c\u589e\u52a0\u4e86\u5f88\u591a\u6df7\u4e71\u3002\u4f7f\u7528 plog \uff0c\u53ef\u4ee5\u901a\u8fc7\u66f4\u6539 init \u8bed\u53e5\u6765\u4e34\u65f6\u7981\u7528\u65e5\u5fd7\u8bb0\u5f55: 1 plog :: init ( plog :: none , \"Logfile.txt\" ); // plog::none eliminates writing of most messages, essentially turning logging off \u6211\u4eec\u4e0d\u4f1a\u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\u4f7f\u7528plog\uff0c\u6240\u4ee5\u4f60\u4e0d\u9700\u8981\u62c5\u5fc3\u5b66\u4e60\u5b83\u3002 \u9898\u5916\u8bdd \u5982\u679c\u4f60\u60f3\u81ea\u5df1\u7f16\u8bd1\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u6216\u8005\u5728\u4f60\u81ea\u5df1\u7684\u9879\u76ee\u4e2d\u4f7f\u7528plog\uff0c\u53ef\u4ee5\u6309\u7167\u8fd9\u4e9b\u8bf4\u660e\u6765\u5b89\u88c5\u5b83: \u9996\u5148\uff0c\u83b7\u53d6\u6700\u65b0\u7248\u672c\u7684 plog: - \u8bbf\u95ee plog \u7684 Github \u4ed3\u5e93\uff1b - \u70b9\u51fb\u53f3\u4e0a\u89d2\u7684\u7eff\u8272Code\u6309\u94ae\uff0c\u7136\u540e\u9009\u62e9\u201cDownload zip\u201d\u3002 \u63a5\u4e0b\u6765\uff0c\u5728\u7535\u8111\u4e0a\u89e3\u538b\u538b\u7f29\u5305\uff08\u5047\u8bbe\u662f somewhere \uff09\u3002 \u6700\u540e\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u9700\u8981\u4f7f\u7528\u5b83\u7684\u9879\u76ee\uff0c\u5728IDE\u7684 include directory \u8bbe\u7f6e somewhere\\plog-master\\include\\ \u3002\u5982\u679c\u4f60\u4f7f\u7528 Visual Studio \u53ef\u4ee5\u53c2\u8003\uff1a A.2 - \u5728visual studio\u4e2d\u4f7f\u7528\u5e93 \uff0c\u5982\u679c\u662fCode::Blocks\u5219\u53c2\u8003 A.3 - \u5728Code Blocks\u4e2d\u4f7f\u7528\u5e93 \u3002","title":"\u4f7f\u7528\u65e5\u5fd7"},{"location":"3-6-Using-an-integrated-debugger-Stepping/","tags":["debugger"],"text":"\u5f53\u4f60\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u4ece main \u51fd\u6570\u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\uff0c\u7136\u540e\u9010\u6761\u6267\u884c\uff0c\u76f4\u5230\u7a0b\u5e8f\u7ed3\u675f\u3002\u5728\u7a0b\u5e8f\u8fd0\u884c\u7684\u4efb\u4f55\u65f6\u95f4\u70b9\uff0c\u7a0b\u5e8f\u90fd\u5728\u8ddf\u8e2a\u5f88\u591a\u4e8b\u60c5\uff1a\u4f60\u6b63\u5728\u4f7f\u7528\u7684\u53d8\u91cf\u7684\u503c\uff0c\u8c03\u7528\u4e86\u54ea\u4e9b\u51fd\u6570(\u8fd9\u6837\u5f53\u8fd9\u4e9b\u51fd\u6570\u8fd4\u56de\u65f6\uff0c\u7a0b\u5e8f\u5c31\u77e5\u9053\u8981\u8fd4\u56de\u5230\u54ea\u91cc)\uff0c\u4ee5\u53ca\u7a0b\u5e8f\u4e2d\u7684\u5f53\u524d\u6267\u884c\u70b9(\u8fd9\u6837\u5b83\u5c31\u77e5\u9053\u4e0b\u4e00\u6b65\u8981\u6267\u884c\u54ea\u6761\u8bed\u53e5)\u3002\u6240\u6709\u8fd9\u4e9b\u88ab\u8ddf\u8e2a\u7684\u4fe1\u606f\u90fd\u88ab\u79f0\u4e3a\u7a0b\u5e8f\u72b6\u6001(\u6216\u7b80\u79f0\u4e3a state )\u3002 \u5728\u524d\u9762\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u7684\u8c03\u8bd5\u65b9\u6cd5\u90fd\u9700\u8981\u5bf9\u4ee3\u7801\u8fdb\u884c\u4fee\u6539\uff0c\u4f8b\u5982\u6dfb\u52a0\u6253\u5370\u8bed\u53e5\u6216\u4f7f\u7528\u65e5\u5fd7\u5de5\u5177\u3002\u8fd9\u4e9b\u662f\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u68c0\u67e5\u7a0b\u5e8f\u72b6\u6001\u7684\u7b80\u5355\u65b9\u6cd5\u3002\u5c3d\u7ba1\u5982\u679c\u4f7f\u7528\u5f97\u5f53\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u662f\u6709\u6548\u7684\uff0c\u4f46\u4e5f\u6709\u7f3a\u70b9\uff1a\u5b83\u4eec\u9700\u8981\u4fee\u6539\u4ee3\u7801\uff0c\u8fd9\u9700\u8981\u65f6\u95f4\uff0c\u5e76\u53ef\u80fd\u5f15\u5165\u65b0\u7684\u9519\u8bef\uff0c\u5e76\u4e14\u5b83\u4eec\u4f1a\u4f7f\u4ee3\u7801\u53d8\u5f97\u6df7\u4e71\uff0c\u4f7f\u73b0\u6709\u7684\u4ee3\u7801\u66f4\u96be\u7406\u89e3\u3002 \u6211\u4eec\u4e4b\u6240\u4ee5\u8fd9\u4e48\u505a\uff0c\u662f\u56e0\u4e3a\u6211\u4eec\u57fa\u4e8e\u4e00\u4e2a\u5047\u8bbe\u2014\u2014\u5373\u4ee3\u7801\u4e00\u65e6\u5f00\u59cb\u8fd0\u884c\uff0c\u7a0b\u5e8f\u5c31\u4f1a\u8fd0\u884c\u5230\u7ed3\u675f(\u53ea\u4f1a\u5728\u63a5\u53d7\u8f93\u5165\u65f6\u6682\u505c)\uff0c\u6211\u4eec\u6ca1\u6709\u673a\u4f1a\u5728\u4efb\u4f55\u6211\u4eec\u60f3\u8981\u7684\u65f6\u5019\u8fdb\u884c\u5e72\u9884\u548c\u68c0\u67e5\u7a0b\u5e8f\u7684\u7ed3\u679c\u3002 \u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u80fd\u591f\u6d88\u9664\u8fd9\u79cd\u5047\u8bbe\u5462\uff1f\u5e78\u8fd0\u7684\u662f\uff0c\u5927\u591a\u6570\u73b0\u4ee3IDE\u90fd\u63d0\u4f9b\u4e86\u4e00\u79cd\u79f0\u4e3a\u8c03\u8bd5\u5668\u7684\u96c6\u6210\u5de5\u5177\uff0c\u8be5\u5de5\u5177\u6b63\u662f\u4e3a\u6b64\u800c\u8bbe\u8ba1\u7684\u3002 \u8c03\u8bd5\u5668 \u8c03\u8bd5\u5668\u662f\u4e00\u79cd\u8ba1\u7b97\u673a\u7a0b\u5e8f\uff0c\u5b83\u5141\u8bb8\u7a0b\u5e8f\u5458\u63a7\u5236\u53e6\u4e00\u4e2a\u7a0b\u5e8f\u7684\u6267\u884c\u65b9\u5f0f\uff0c\u5e76\u5728\u8be5\u7a0b\u5e8f\u8fd0\u884c\u65f6\u68c0\u67e5\u8be5\u7a0b\u5e8f\u7684\u72b6\u6001\u3002\u4f8b\u5982\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u4f7f\u7528\u8c03\u8bd5\u5668\u9010\u884c\u6267\u884c\u7a0b\u5e8f\uff0c\u5728\u6574\u4e2a\u8fc7\u7a0b\u4e2d\u68c0\u67e5\u53d8\u91cf\u7684\u503c\u3002\u901a\u8fc7\u5c06\u53d8\u91cf\u7684\u5b9e\u9645\u503c\u4e0e\u9884\u671f\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u6216\u8005\u89c2\u5bdf\u6574\u4e2a\u4ee3\u7801\u7684\u6267\u884c\u8def\u5f84\uff0c\u8c03\u8bd5\u5668\u53ef\u4ee5\u6781\u5927\u5730\u5e2e\u52a9\u8ddf\u8e2a\u8bed\u4e49(\u903b\u8f91)\u9519\u8bef\u3002 \u8c03\u8bd5\u5668\u6709\u4e24\u5927\u6838\u5fc3\u529f\u80fd\uff1a\u7cbe\u786e\u63a7\u5236\u7a0b\u5e8f\u6267\u884c\u7684\u80fd\u529b\uff0c\u4ee5\u53ca\u67e5\u770b(\u548c\u4fee\u6539\uff0c\u5982\u679c\u9700\u8981\u7684\u8bdd)\u7a0b\u5e8f\u72b6\u6001\u7684\u80fd\u529b\u3002 \u65e9\u671f\u7684\u8c03\u8bd5\u5668\uff0c\u5982 gdb \uff0c\u662f\u5177\u6709\u547d\u4ee4\u884c\u63a5\u53e3\u7684\u72ec\u7acb\u7a0b\u5e8f\uff0c\u7a0b\u5e8f\u5458\u5fc5\u987b\u952e\u5165\u795e\u79d8\u7684\u547d\u4ee4\u624d\u80fd\u4f7f\u5b83\u4eec\u5de5\u4f5c\u3002\u540e\u6765\u7684\u8c03\u8bd5\u5668(\u5982Borland\u7684[turbo debugger]\u7684\u65e9\u671f\u7248\u672c(https://en.wikipedia.org/wiki/Turbo_Debugger))\u4ecd\u7136\u662f\u72ec\u7acb\u7684\uff0c\u4f46\u6709\u81ea\u5df1\u7684\u201c\u56fe\u5f62\u5316\u201d\u524d\u7aef\uff0c\u80fd\u591f\u4f7f\u5176\u66f4\u52a0\u6613\u7528\u3002\u76ee\u524d\u5927\u591a\u6570\u73b0\u4ee3IDE\u90fd\u6709\u4e00\u4e2a\u96c6\u6210\u7684\u8c03\u8bd5\u5668\u2014\u2014\u4e5f\u5c31\u662f\u8bf4\uff0c\u8c03\u8bd5\u5668\u4f7f\u7528\u4e0e\u4ee3\u7801\u7f16\u8f91\u5668\u76f8\u540c\u7684\u63a5\u53e3\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u4f7f\u7528\u7f16\u5199\u4ee3\u7801\u65f6\u4f7f\u7528\u7684\u76f8\u540c\u73af\u5883\u8fdb\u884c\u8c03\u8bd5(\u800c\u4e0d\u5fc5\u5207\u6362\u7a0b\u5e8f)\u3002 \u51e0\u4e4e\u6240\u6709\u7684\u73b0\u4ee3\u8c03\u8bd5\u5668\u90fd\u5305\u542b\u76f8\u540c\u7684\u57fa\u672c\u529f\u80fd\u6807\u51c6\u96c6\u2014\u2014\u7136\u800c\uff0c\u5728\u8bbf\u95ee\u8fd9\u4e9b\u529f\u80fd\u7684\u83dc\u5355\u5982\u4f55\u4f7f\u7528\u65b9\u9762\u51e0\u4e4e\u6ca1\u6709\u4e00\u81f4\u6027\uff0c\u952e\u76d8\u5feb\u6377\u952e\u7684\u4e00\u81f4\u6027\u751a\u81f3\u66f4\u4f4e\u3002\u5c3d\u7ba1\u6211\u4eec\u7684\u793a\u4f8b\u5c06\u4f7f\u7528Microsoft Visual Studio\u7684\u622a\u56fe(\u6211\u4eec\u4e5f\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728Code::Blocks\u4e2d\u5b8c\u6210\u6240\u6709\u64cd\u4f5c)\uff0c\u4f46\u65e0\u8bba\u4f60\u4f7f\u7528\u7684\u662f\u54ea\u79cdIDE\uff0c\u90fd\u5e94\u8be5\u4e0d\u96be\u5f04\u6e05\u695a\u5982\u4f55\u8bbf\u95ee\u6211\u4eec\u8ba8\u8bba\u7684\u6bcf\u4e2a\u529f\u80fd\u3002 \u5c0f\u8d34\u58eb \u8c03\u8bd5\u5668\u952e\u76d8\u5feb\u6377\u952e\u53ea\u6709\u5728IDE/\u96c6\u6210\u8c03\u8bd5\u5668\u662f\u6d3b\u52a8\u7a97\u53e3\u65f6\u624d\u6709\u6548\u3002 \u672c\u7ae0\u7684\u5269\u4f59\u90e8\u5206\u5c06\u5b66\u4e60\u5982\u4f55\u4f7f\u7528\u8c03\u8bd5\u5668\u3002 \u5c0f\u8d34\u58eb \u4e0d\u8981\u5ffd\u89c6\u5b66\u4e60\u4f7f\u7528\u8c03\u8bd5\u5668\u3002\u968f\u7740\u7a0b\u5e8f\u53d8\u5f97\u8d8a\u6765\u8d8a\u590d\u6742\uff0c\u4e0e\u67e5\u627e\u548c\u4fee\u590d\u95ee\u9898\u6240\u8282\u7701\u7684\u65f6\u95f4\u76f8\u6bd4\uff0c\u5b66\u4e60\u6709\u6548\u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u6240\u82b1\u8d39\u7684\u65f6\u95f4\u5c31\u663e\u5f97\u5fae\u4e0d\u8db3\u9053\u4e86\u3002 \u6ce8\u610f Before proceeding with this lesson (and subsequent lessons related to using a debugger), make sure your project is compiled using a debug build configuration (see 0.9 -- Configuring your compiler: Build configurations for more information). If you\u2019re compiling your project using a release configuration instead, the functionality of the debugger may not work correctly (e.g. when you try to step into your program, it will just run the program instead). For Code::Blocks users If you\u2019re using Code::Blocks, your debugger may or may not be set up correctly. Let\u2019s check. First, go to Settings menu > Debugger\u2026 . Next, open the GDB/CDB debugger tree on the left, and choose Default . A dialog should open that looks something like this: If you see a big red bar where the \u201cExecutable path\u201d should be, then you need to locate your debugger. To do so, click the \u2026 button to the right of the Executable path_field. Next, find the \u201cgdb32.exe\u201d file on your system -- mine was in _C:\\Program Files (x86)\\CodeBlocks\\MinGW\\bin\\gdb32.exe . Then click OK . For Code::Blocks users There have been reports that the Code::Blocks integrated debugger (GDB) can have issues recognizing some file paths that contain spaces or non-English characters in them. If the debugger appears to be malfunctioning as you go through these lessons, that could be a reason why. \u5355\u6b65\u8c03\u8bd5 We\u2019re going to start our exploration of the debugger by first examining some of the debugging tools that allow us to control the way a program executes. Stepping is the name for a set of related debugger features that let us execute (step through) our code statement by statement. There are a number of related stepping commands that we\u2019ll cover in turn. Step into The step into command executes the next statement in the normal execution path of the program, and then pauses execution of the program so we can examine the program\u2019s state using the debugger. If the statement being executed contains a function call, step into causes the program to jump to the top of the function being called, where it will pause. Let\u2019s take a look at a very simple program: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void printValue ( int value ) { std :: cout << value << '\\n' ; } int main () { printValue ( 5 ); return 0 ; } Let\u2019s debug this program using the step into command. First, locate and then execute the step into debug command once. For Visual Studio users In Visual Studio, the step into command can be accessed via Debug menu > Step Into , or by pressing the F11 shortcut key. For Code::Blocks users In Code::Blocks, the step into command can be accessed via Debug menu > Step into , or by pressing the Shift-F7 For other compilers If using a different IDE, you\u2019ll likely find the step into command under a Debug or Run menu. When your program isn\u2019t running and you execute the first debug command, you may see quite a few things happen: The program will recompile if needed. The program will begin to run. Because our application is a console program, a console output window should open. It will be empty because we haven\u2019t output anything yet. Your IDE may open some diagnostic windows, which may have names such as \u201cDiagnostic Tools\u201d, \u201cCall Stack\u201d, and \u201cWatch\u201d. We\u2019ll cover what some of these are later -- for now you can ignore them. Because we did a step into , you should now see some kind of marker appear to the left of the opening brace of function main (line 9). In Visual Studio, this marker is a yellow arrow (Code::Blocks uses a yellow triangle). If you are using a different IDE, you should see something that serves the same purpose. This arrow marker indicates that the line being pointed to will be executed next. In this case, the debugger is telling us that the next line to be executed is the opening brace of function main (line 9). Choose step into (using the appropriate command for your IDE, listed above) to execute the opening brace, and the arrow will move to the next statement (line 10). This means the next line that will be executed is the call to function printValue . Choose step into again. Because this statement contains a function call to printValue , we step into the function, and the arrow will move to the top of the body of printValue (line 4). Choose step into again to execute the opening brace of function printValue , which will advance the arrow to line 5. Choose step into yet again, which will execute the statement std::cout << value and move the arrow to line 6. \u6ce8\u610f Because operator<< is implemented as a function, your IDE may step into the implementation of operator<< instead. If this happens, you\u2019ll see your IDE open a new code file, and the arrow marker will move to the top of a function named operator<< (this is part of the standard library). Close the code file that just opened, then find and execute step out debug command (instructions are below under the \u201cstep out\u201d section, if you need help). Now because std::cout << value has executed, we should see the value 5 appear in the console window. \u5c0f\u8d34\u58eb In a prior lesson, we mentioned that std::cout is buffered, which means there may be a delay between when you ask std::cout to print a value, and when it actually does. Because of this, you may not see the value 5 appear at this point. To ensure that all output from std::cout is output immediately, you can add the following statement to the top of your main() function: 1 std :: cout << std :: unitbuf ; // enable automatic flushing for std::cout (for debugging) COPY Choose step into again to execute the closing brace of function printValue . At this point, printValue has finished executing and control is returned to main . You will note that the arrow is again pointing to printValue ! While you might think that the debugger intends to call printValue again, in actuality the debugger is just letting you know that it is returning from the function call. Choose step into three more times. At this point, we have executed all the lines in our program, so we are done. Some debuggers will terminate the debugging session automatically at this point, others may not. If your debugger does not, you may need to find a \u201cStop Debugging\u201d command in your menus (in Visual Studio, this is under Debug > Stop Debugging ). Note that Stop Debugging can be used at any point in the debugging process to end the debugging session. Congratulations, you\u2019ve now stepped through a program and watched every line execute! Step over Like step into , The step over command executes the next statement in the normal execution path of the program. However, whereas step into will enter function calls and execute them line by line, step over will execute an entire function without stopping and return control to you after the function has been executed. For Visual Studio users In Visual Studio, the step over command can be accessed via Debug menu > Step Over , or by pressing the F10 shortcut key. For Code::Blocks users In Code::Blocks, the step over command is called Next line instead, and can be accessed via Debug menu > Next line , or by pressing the F7 shortcut key. Let\u2019s take a look at an example where we step over the function call to printValue : 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void printValue ( int value ) { std :: cout << value << '\\n' ; } int main () { printValue ( 5 ); return 0 ; } First, use step into on your program until the execution marker is on line 10: Now, choose step over . The debugger will execute the function (which prints the value 5 in the console output window) and then return control to you on the next statement (line 12). The step over command provides a convenient way to skip functions when you are sure they already work or are not interested in debugging them right now. Step out Unlike the other two stepping commands, Step out does not just execute the next line of code. Instead, it executes all remaining code in the function currently being executed, and then returns control to you when the function has returned. For Visual Studio users In Visual Studio, the step out command can be accessed via Debug menu > Step Out , or by pressing the Shift-F11 shortcut combo. For Code::Blocks users In Code::Blocks, the step out command can be accessed via Debug menu > Step out , or by pressing the ctrl-F7 shortcut combo. Let\u2019s take a look at an example of this using the same program as above: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void printValue ( int value ) { std :: cout << value << '\\n' ; } int main () { printValue ( 5 ); return 0 ; } Step into the program until you are inside function printValue , with the execution marker on line 4. Then choose step out . You will notice the value 5 appears in the output window, and the debugger returns control to you after the function has terminated (on line 10). This command is most useful when you\u2019ve accidentally stepped into a function that you don\u2019t want to debug. A step too far When stepping through a program, you can normally only step forward. It\u2019s very easy to accidentally step past (overstep) the place you wanted to examine. If you step past your intended destination, the usual thing to do is stop debugging and restart debugging again, being a little more careful not to pass your target this time. Step back Some debuggers (such as Visual Studio Enterprise Edition and GDB 7.0) have introduced a stepping capability generally referred to as step back or reverse debugging . The goal of a step back is to rewind the last step, so you can return the program to a prior state. This can be useful if you overstep, or if you want to re-examine a statement that just executed. Implementing step back requires a great deal of sophistication on the part of the debugger (because it has to keep track of a separate program state for each step). Because of the complexity, this capability isn\u2019t standardized yet, and varies by debugger. As of the time of writing (Jan 2019), neither Visual Studio Community edition nor the latest version of Code::Blocks support this capability. Hopefully at some point in the future, it will trickle down into these products and be available for wider use.","title":"3.6 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u5355\u6b65\u8c03\u8bd5"},{"location":"3-6-Using-an-integrated-debugger-Stepping/#_1","text":"\u8c03\u8bd5\u5668\u662f\u4e00\u79cd\u8ba1\u7b97\u673a\u7a0b\u5e8f\uff0c\u5b83\u5141\u8bb8\u7a0b\u5e8f\u5458\u63a7\u5236\u53e6\u4e00\u4e2a\u7a0b\u5e8f\u7684\u6267\u884c\u65b9\u5f0f\uff0c\u5e76\u5728\u8be5\u7a0b\u5e8f\u8fd0\u884c\u65f6\u68c0\u67e5\u8be5\u7a0b\u5e8f\u7684\u72b6\u6001\u3002\u4f8b\u5982\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u4f7f\u7528\u8c03\u8bd5\u5668\u9010\u884c\u6267\u884c\u7a0b\u5e8f\uff0c\u5728\u6574\u4e2a\u8fc7\u7a0b\u4e2d\u68c0\u67e5\u53d8\u91cf\u7684\u503c\u3002\u901a\u8fc7\u5c06\u53d8\u91cf\u7684\u5b9e\u9645\u503c\u4e0e\u9884\u671f\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u6216\u8005\u89c2\u5bdf\u6574\u4e2a\u4ee3\u7801\u7684\u6267\u884c\u8def\u5f84\uff0c\u8c03\u8bd5\u5668\u53ef\u4ee5\u6781\u5927\u5730\u5e2e\u52a9\u8ddf\u8e2a\u8bed\u4e49(\u903b\u8f91)\u9519\u8bef\u3002 \u8c03\u8bd5\u5668\u6709\u4e24\u5927\u6838\u5fc3\u529f\u80fd\uff1a\u7cbe\u786e\u63a7\u5236\u7a0b\u5e8f\u6267\u884c\u7684\u80fd\u529b\uff0c\u4ee5\u53ca\u67e5\u770b(\u548c\u4fee\u6539\uff0c\u5982\u679c\u9700\u8981\u7684\u8bdd)\u7a0b\u5e8f\u72b6\u6001\u7684\u80fd\u529b\u3002 \u65e9\u671f\u7684\u8c03\u8bd5\u5668\uff0c\u5982 gdb \uff0c\u662f\u5177\u6709\u547d\u4ee4\u884c\u63a5\u53e3\u7684\u72ec\u7acb\u7a0b\u5e8f\uff0c\u7a0b\u5e8f\u5458\u5fc5\u987b\u952e\u5165\u795e\u79d8\u7684\u547d\u4ee4\u624d\u80fd\u4f7f\u5b83\u4eec\u5de5\u4f5c\u3002\u540e\u6765\u7684\u8c03\u8bd5\u5668(\u5982Borland\u7684[turbo debugger]\u7684\u65e9\u671f\u7248\u672c(https://en.wikipedia.org/wiki/Turbo_Debugger))\u4ecd\u7136\u662f\u72ec\u7acb\u7684\uff0c\u4f46\u6709\u81ea\u5df1\u7684\u201c\u56fe\u5f62\u5316\u201d\u524d\u7aef\uff0c\u80fd\u591f\u4f7f\u5176\u66f4\u52a0\u6613\u7528\u3002\u76ee\u524d\u5927\u591a\u6570\u73b0\u4ee3IDE\u90fd\u6709\u4e00\u4e2a\u96c6\u6210\u7684\u8c03\u8bd5\u5668\u2014\u2014\u4e5f\u5c31\u662f\u8bf4\uff0c\u8c03\u8bd5\u5668\u4f7f\u7528\u4e0e\u4ee3\u7801\u7f16\u8f91\u5668\u76f8\u540c\u7684\u63a5\u53e3\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u4f7f\u7528\u7f16\u5199\u4ee3\u7801\u65f6\u4f7f\u7528\u7684\u76f8\u540c\u73af\u5883\u8fdb\u884c\u8c03\u8bd5(\u800c\u4e0d\u5fc5\u5207\u6362\u7a0b\u5e8f)\u3002 \u51e0\u4e4e\u6240\u6709\u7684\u73b0\u4ee3\u8c03\u8bd5\u5668\u90fd\u5305\u542b\u76f8\u540c\u7684\u57fa\u672c\u529f\u80fd\u6807\u51c6\u96c6\u2014\u2014\u7136\u800c\uff0c\u5728\u8bbf\u95ee\u8fd9\u4e9b\u529f\u80fd\u7684\u83dc\u5355\u5982\u4f55\u4f7f\u7528\u65b9\u9762\u51e0\u4e4e\u6ca1\u6709\u4e00\u81f4\u6027\uff0c\u952e\u76d8\u5feb\u6377\u952e\u7684\u4e00\u81f4\u6027\u751a\u81f3\u66f4\u4f4e\u3002\u5c3d\u7ba1\u6211\u4eec\u7684\u793a\u4f8b\u5c06\u4f7f\u7528Microsoft Visual Studio\u7684\u622a\u56fe(\u6211\u4eec\u4e5f\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728Code::Blocks\u4e2d\u5b8c\u6210\u6240\u6709\u64cd\u4f5c)\uff0c\u4f46\u65e0\u8bba\u4f60\u4f7f\u7528\u7684\u662f\u54ea\u79cdIDE\uff0c\u90fd\u5e94\u8be5\u4e0d\u96be\u5f04\u6e05\u695a\u5982\u4f55\u8bbf\u95ee\u6211\u4eec\u8ba8\u8bba\u7684\u6bcf\u4e2a\u529f\u80fd\u3002 \u5c0f\u8d34\u58eb \u8c03\u8bd5\u5668\u952e\u76d8\u5feb\u6377\u952e\u53ea\u6709\u5728IDE/\u96c6\u6210\u8c03\u8bd5\u5668\u662f\u6d3b\u52a8\u7a97\u53e3\u65f6\u624d\u6709\u6548\u3002 \u672c\u7ae0\u7684\u5269\u4f59\u90e8\u5206\u5c06\u5b66\u4e60\u5982\u4f55\u4f7f\u7528\u8c03\u8bd5\u5668\u3002 \u5c0f\u8d34\u58eb \u4e0d\u8981\u5ffd\u89c6\u5b66\u4e60\u4f7f\u7528\u8c03\u8bd5\u5668\u3002\u968f\u7740\u7a0b\u5e8f\u53d8\u5f97\u8d8a\u6765\u8d8a\u590d\u6742\uff0c\u4e0e\u67e5\u627e\u548c\u4fee\u590d\u95ee\u9898\u6240\u8282\u7701\u7684\u65f6\u95f4\u76f8\u6bd4\uff0c\u5b66\u4e60\u6709\u6548\u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u6240\u82b1\u8d39\u7684\u65f6\u95f4\u5c31\u663e\u5f97\u5fae\u4e0d\u8db3\u9053\u4e86\u3002 \u6ce8\u610f Before proceeding with this lesson (and subsequent lessons related to using a debugger), make sure your project is compiled using a debug build configuration (see 0.9 -- Configuring your compiler: Build configurations for more information). If you\u2019re compiling your project using a release configuration instead, the functionality of the debugger may not work correctly (e.g. when you try to step into your program, it will just run the program instead). For Code::Blocks users If you\u2019re using Code::Blocks, your debugger may or may not be set up correctly. Let\u2019s check. First, go to Settings menu > Debugger\u2026 . Next, open the GDB/CDB debugger tree on the left, and choose Default . A dialog should open that looks something like this: If you see a big red bar where the \u201cExecutable path\u201d should be, then you need to locate your debugger. To do so, click the \u2026 button to the right of the Executable path_field. Next, find the \u201cgdb32.exe\u201d file on your system -- mine was in _C:\\Program Files (x86)\\CodeBlocks\\MinGW\\bin\\gdb32.exe . Then click OK . For Code::Blocks users There have been reports that the Code::Blocks integrated debugger (GDB) can have issues recognizing some file paths that contain spaces or non-English characters in them. If the debugger appears to be malfunctioning as you go through these lessons, that could be a reason why.","title":"\u8c03\u8bd5\u5668"},{"location":"3-6-Using-an-integrated-debugger-Stepping/#_2","text":"We\u2019re going to start our exploration of the debugger by first examining some of the debugging tools that allow us to control the way a program executes. Stepping is the name for a set of related debugger features that let us execute (step through) our code statement by statement. There are a number of related stepping commands that we\u2019ll cover in turn.","title":"\u5355\u6b65\u8c03\u8bd5"},{"location":"3-6-Using-an-integrated-debugger-Stepping/#step-into","text":"The step into command executes the next statement in the normal execution path of the program, and then pauses execution of the program so we can examine the program\u2019s state using the debugger. If the statement being executed contains a function call, step into causes the program to jump to the top of the function being called, where it will pause. Let\u2019s take a look at a very simple program: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void printValue ( int value ) { std :: cout << value << '\\n' ; } int main () { printValue ( 5 ); return 0 ; } Let\u2019s debug this program using the step into command. First, locate and then execute the step into debug command once. For Visual Studio users In Visual Studio, the step into command can be accessed via Debug menu > Step Into , or by pressing the F11 shortcut key. For Code::Blocks users In Code::Blocks, the step into command can be accessed via Debug menu > Step into , or by pressing the Shift-F7 For other compilers If using a different IDE, you\u2019ll likely find the step into command under a Debug or Run menu. When your program isn\u2019t running and you execute the first debug command, you may see quite a few things happen: The program will recompile if needed. The program will begin to run. Because our application is a console program, a console output window should open. It will be empty because we haven\u2019t output anything yet. Your IDE may open some diagnostic windows, which may have names such as \u201cDiagnostic Tools\u201d, \u201cCall Stack\u201d, and \u201cWatch\u201d. We\u2019ll cover what some of these are later -- for now you can ignore them. Because we did a step into , you should now see some kind of marker appear to the left of the opening brace of function main (line 9). In Visual Studio, this marker is a yellow arrow (Code::Blocks uses a yellow triangle). If you are using a different IDE, you should see something that serves the same purpose. This arrow marker indicates that the line being pointed to will be executed next. In this case, the debugger is telling us that the next line to be executed is the opening brace of function main (line 9). Choose step into (using the appropriate command for your IDE, listed above) to execute the opening brace, and the arrow will move to the next statement (line 10). This means the next line that will be executed is the call to function printValue . Choose step into again. Because this statement contains a function call to printValue , we step into the function, and the arrow will move to the top of the body of printValue (line 4). Choose step into again to execute the opening brace of function printValue , which will advance the arrow to line 5. Choose step into yet again, which will execute the statement std::cout << value and move the arrow to line 6. \u6ce8\u610f Because operator<< is implemented as a function, your IDE may step into the implementation of operator<< instead. If this happens, you\u2019ll see your IDE open a new code file, and the arrow marker will move to the top of a function named operator<< (this is part of the standard library). Close the code file that just opened, then find and execute step out debug command (instructions are below under the \u201cstep out\u201d section, if you need help). Now because std::cout << value has executed, we should see the value 5 appear in the console window. \u5c0f\u8d34\u58eb In a prior lesson, we mentioned that std::cout is buffered, which means there may be a delay between when you ask std::cout to print a value, and when it actually does. Because of this, you may not see the value 5 appear at this point. To ensure that all output from std::cout is output immediately, you can add the following statement to the top of your main() function: 1 std :: cout << std :: unitbuf ; // enable automatic flushing for std::cout (for debugging) COPY Choose step into again to execute the closing brace of function printValue . At this point, printValue has finished executing and control is returned to main . You will note that the arrow is again pointing to printValue ! While you might think that the debugger intends to call printValue again, in actuality the debugger is just letting you know that it is returning from the function call. Choose step into three more times. At this point, we have executed all the lines in our program, so we are done. Some debuggers will terminate the debugging session automatically at this point, others may not. If your debugger does not, you may need to find a \u201cStop Debugging\u201d command in your menus (in Visual Studio, this is under Debug > Stop Debugging ). Note that Stop Debugging can be used at any point in the debugging process to end the debugging session. Congratulations, you\u2019ve now stepped through a program and watched every line execute!","title":"Step into"},{"location":"3-6-Using-an-integrated-debugger-Stepping/#step-over","text":"Like step into , The step over command executes the next statement in the normal execution path of the program. However, whereas step into will enter function calls and execute them line by line, step over will execute an entire function without stopping and return control to you after the function has been executed. For Visual Studio users In Visual Studio, the step over command can be accessed via Debug menu > Step Over , or by pressing the F10 shortcut key. For Code::Blocks users In Code::Blocks, the step over command is called Next line instead, and can be accessed via Debug menu > Next line , or by pressing the F7 shortcut key. Let\u2019s take a look at an example where we step over the function call to printValue : 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void printValue ( int value ) { std :: cout << value << '\\n' ; } int main () { printValue ( 5 ); return 0 ; } First, use step into on your program until the execution marker is on line 10: Now, choose step over . The debugger will execute the function (which prints the value 5 in the console output window) and then return control to you on the next statement (line 12). The step over command provides a convenient way to skip functions when you are sure they already work or are not interested in debugging them right now.","title":"Step over"},{"location":"3-6-Using-an-integrated-debugger-Stepping/#step-out","text":"Unlike the other two stepping commands, Step out does not just execute the next line of code. Instead, it executes all remaining code in the function currently being executed, and then returns control to you when the function has returned. For Visual Studio users In Visual Studio, the step out command can be accessed via Debug menu > Step Out , or by pressing the Shift-F11 shortcut combo. For Code::Blocks users In Code::Blocks, the step out command can be accessed via Debug menu > Step out , or by pressing the ctrl-F7 shortcut combo. Let\u2019s take a look at an example of this using the same program as above: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void printValue ( int value ) { std :: cout << value << '\\n' ; } int main () { printValue ( 5 ); return 0 ; } Step into the program until you are inside function printValue , with the execution marker on line 4. Then choose step out . You will notice the value 5 appears in the output window, and the debugger returns control to you after the function has terminated (on line 10). This command is most useful when you\u2019ve accidentally stepped into a function that you don\u2019t want to debug.","title":"Step out"},{"location":"3-6-Using-an-integrated-debugger-Stepping/#a-step-too-far","text":"When stepping through a program, you can normally only step forward. It\u2019s very easy to accidentally step past (overstep) the place you wanted to examine. If you step past your intended destination, the usual thing to do is stop debugging and restart debugging again, being a little more careful not to pass your target this time.","title":"A step too far"},{"location":"3-6-Using-an-integrated-debugger-Stepping/#step-back","text":"Some debuggers (such as Visual Studio Enterprise Edition and GDB 7.0) have introduced a stepping capability generally referred to as step back or reverse debugging . The goal of a step back is to rewind the last step, so you can return the program to a prior state. This can be useful if you overstep, or if you want to re-examine a statement that just executed. Implementing step back requires a great deal of sophistication on the part of the debugger (because it has to keep track of a separate program state for each step). Because of the complexity, this capability isn\u2019t standardized yet, and varies by debugger. As of the time of writing (Jan 2019), neither Visual Studio Community edition nor the latest version of Code::Blocks support this capability. Hopefully at some point in the future, it will trickle down into these products and be available for wider use.","title":"Step back"},{"location":"3-7-Using-an-integrated-debugger-Running-and-breakpoints/","tags":["debugger"],"text":"While stepping (covered in lesson 3.6 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u5355\u6b65\u8c03\u8bd5 ) is useful for examining each individual line of your code in isolation, in a large program, it can take a long time to step through your code to even get to the point where you want to examine in more detail. Fortunately, modern debuggers provide more tools to help us efficiently debug our programs. In this lesson, we\u2019ll look at some of the debugger features that let us more quickly navigate our code. Run to cursor The first useful command is commonly called Run to cursor . This Run to cursor command executes the program until execution reaches the statement selected by your cursor. Then it returns control to you so you can debug starting at that point. This makes for an efficient way to start debugging at a particular point in your code, or if already debugging, to move straight to some place you want to examine further. For Visual Studio users In Visual Studio, the run to cursor command can be accessed by right clicking a statement in your code and choosing Run to Cursor from the context menu, or by pressing the ctrl-F10 keyboard combo. For Code::Blocks users In Code::Blocks, the run to cursor command can be accessed by right clicking a statement in your code and choosing either Run to cursor from the context menu or Debug menu > Run to cursor , or by pressing the F4 shortcut key. Let\u2019s try it using the same program we\u2019ve been using: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void printValue ( int value ) { std :: cout << value ; } int main () { printValue ( 5 ); return 0 ; } COPY Simply right click anywhere on line 5, then choose \u201cRun to cursor\u201d. You will notice the program starts running, and the execution marker moves to the line you just selected. Your program has executed up to this point and is now waiting for your further debugging commands. From here, you can step through your program, run to cursor to a different location, etc\u2026 If you run to cursor to a location that doesn\u2019t execute, run to cursor will simply run your program until termination. Continue Once you\u2019re in the middle of a debugging session, you may want to just run the program from that point forward. The easiest way to do this is to use the _continue_command. The continue debug command simply continues running the program as per normal, either until the program terminates, or until something triggers control to return back to you again (such as a breakpoint, which we\u2019ll cover later in this lesson). For Visual Studio users In Visual Studio, the continue command can be accessed while already debugging a program via Debug menu > Continue , or by pressing the F5 shortcut key. For Code::Blocks users In Code::Blocks, the continue command can be accessed while already debugging a program via Debug menu > Start / Continue , or by pressing the F8 shortcut key. Let\u2019s test out the continue command. If your execution marker isn\u2019t already on line 5, run to cursor to line 5. Then choose continue from this point. Your program will finish executing and then terminate. Start The continue command has a twin brother named start . The start command performs the same action as continue , just starting from the beginning of the program. It can only be invoked when not already in a debug session. For Visual Studio users In Visual Studio, the start command can be accessed while not debugging a program via Debug menu > Start Debugging , or by pressing the F5 shortcut key. For Code::Blocks users In Code::Blocks, the start command can be accessed while not debugging a program via Debug menu > Start / Continue , or by pressing the F8 shortcut key. If you use the start command on the above sample program, it will run all the way through without interruption. While this may seem unremarkable, that\u2019s only because we haven\u2019t told the debugger to interrupt the program. We\u2019ll put this command to better use in the next section. Breakpoints The last topic we are going to talk about in this section is breakpoints. A breakpoint is a special marker that tells the debugger to stop execution of the program at the breakpoint when running in debug mode. For Visual Studio users In Visual Studio, you can set or remove a breakpoint via Debug menu > Toggle Breakpoint , or by right clicking on a statement and choosing Toggle Breakpoint from the context menu, or by pressing the F9 shortcut key, or by clicking to the left of the line number (in the light grey area). For Code::Blocks users In Code::Blocks, you can set or remove a breakpoint via Debug menu > Toggle breakpoint , or by right clicking on a statement and choosing Toggle breakpoint from the context menu, or by pressing the F5 shortcut key, or by clicking to the right of the line number. When you set a breakpoint, you will see a new type of icon appear. Visual Studio uses a red circle, Code::Blocks uses a red octagon (like a stop sign): Go ahead and set a breakpoint on the line 5, as shown in the image above. Now, choose the Start command to have the debugger run your code, and let\u2019s see the breakpoint in action. You will notice that instead of running all the way to the end of the program, the debugger stops at the breakpoint (with the execution marker sitting on top of the breakpoint icon): It\u2019s just as if you\u2019d run to cursor to this point. Breakpoints have a couple of advantages over run to cursor . First, a breakpoint will cause the debugger to return control to you every time they are encountered (unlike run to cursor , which only runs to the cursor once each time it is invoked). Second, you can set a breakpoint and it will persist until you remove it, whereas with run to cursor you have to locate the spot you want to run to each time you invoke the command. Note that breakpoints placed on lines that are not in the path of execution will not cause the debugger to halt execution of the code. Let\u2019s take a look at a slightly modified program that better illustrates the difference between breakpoints and run to cursor : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include void printValue ( int value ) { std :: cout << value ; } int main () { printValue ( 5 ); printValue ( 6 ); printValue ( 7 ); return 0 ; } COPY First, start a new debugging session and then do a run to cursor to line 5. Now choose continue . The program will continue to the end (it won\u2019t stop on line 5 again, even though line 5 is executed twice more). Next, place a breakpoint on line 5, then choose start . The program will stop on line 5. Now choose continue . The program will stop on line 5 a second time. Choose continue_again, and it will stop a third time. One more _continue , and the program will terminate. You can see that the breakpoint caused the program to stop as many times as that line was executed. Set next statement There\u2019s one more debugging command that\u2019s used fairly uncommonly, but is still at least worth knowing about, even if you won\u2019t use it very often. The set next statementcommand allows us to change the point of execution to some other statement (sometimes informally called jumping ). This can be used to jump the point of execution forwards and skip some code that would otherwise execute, or backwards and have something that already executed run again. For Visual Studio users In Visual Studio, you can jump the point of execution by right clicking on a statement and choosing Set next statement from the context menu, or by pressing the Ctrl-Shift-F10 shortcut combination. This option is contextual and only occurs while already debugging a program. For Code::Blocks users In Code::Blocks, you can jump the point of execution via Debug menu > Set next statement , or by right clicking on a statement and choosing Set next statement from the context menu. Code::Blocks doesn\u2019t have a keyboard shortcut for this command. Let\u2019s see jumping forwards in action: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include void printValue ( int value ) { std :: cout << value ; } int main () { printValue ( 5 ); printValue ( 6 ); printValue ( 7 ); return 0 ; } COPY First, run to cursor to line 11. At this point, you should see the value of 5 in the console output window. Now, right click on line 12, and choose set next statement . This causes line 11 to be skipped and not execute. Then choose continue to finish executing your program. The output of your program should look like this: 57 We can see that printValue(6) was skipped. This functionality can be useful in several contexts. In our exploration of basic debugging techniques, we discussed commenting out a function as a way to determine whether that function had a role in causing an issue. This requires modifying our code, and remembering to uncomment the function later. In the debugger, there\u2019s no direct way to skip a function, so if you decide you want to do this, using set next statement to jump over a function call is the easiest way to do so. Jumping backwards can also be useful if we want to watch a function that just executed run again, so we can see what it is doing. With the same code above, run to cursor to line 12. Then set next statement on line 11, and continue . The program\u2019s output should be: 5667 Warning The set next statement command will change the point of execution, but will not otherwise change the program state. Your variables will retain whatever values they had before the jump. As a result, jumping may cause your program to produce different values, results, or behaviors than it would otherwise. Use this capability judiciously (especially jumping backwards). Warning You should not use set next statement to change the point of execution to a different function. This will result in undefined behavior, and likely a crash. Conclusion You now learned the major ways that you can use an integrated debugger to watch and control how your program executes. While these commands can be useful for diagnosing code flow issues (e.g. to determine if certain functions are or aren\u2019t being called), they are only a portion of the benefit that the integrated debugger brings to the table. In the next lesson, we\u2019ll start exploring additional ways to examine your program\u2019s state, for which you\u2019ll need these commands as a prerequisite. Let\u2019s go!","title":"3.7 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8fd0\u884c\u548c\u65ad\u70b9"},{"location":"3-7-Using-an-integrated-debugger-Running-and-breakpoints/#run-to-cursor","text":"The first useful command is commonly called Run to cursor . This Run to cursor command executes the program until execution reaches the statement selected by your cursor. Then it returns control to you so you can debug starting at that point. This makes for an efficient way to start debugging at a particular point in your code, or if already debugging, to move straight to some place you want to examine further. For Visual Studio users In Visual Studio, the run to cursor command can be accessed by right clicking a statement in your code and choosing Run to Cursor from the context menu, or by pressing the ctrl-F10 keyboard combo. For Code::Blocks users In Code::Blocks, the run to cursor command can be accessed by right clicking a statement in your code and choosing either Run to cursor from the context menu or Debug menu > Run to cursor , or by pressing the F4 shortcut key. Let\u2019s try it using the same program we\u2019ve been using: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void printValue ( int value ) { std :: cout << value ; } int main () { printValue ( 5 ); return 0 ; } COPY Simply right click anywhere on line 5, then choose \u201cRun to cursor\u201d. You will notice the program starts running, and the execution marker moves to the line you just selected. Your program has executed up to this point and is now waiting for your further debugging commands. From here, you can step through your program, run to cursor to a different location, etc\u2026 If you run to cursor to a location that doesn\u2019t execute, run to cursor will simply run your program until termination. Continue Once you\u2019re in the middle of a debugging session, you may want to just run the program from that point forward. The easiest way to do this is to use the _continue_command. The continue debug command simply continues running the program as per normal, either until the program terminates, or until something triggers control to return back to you again (such as a breakpoint, which we\u2019ll cover later in this lesson). For Visual Studio users In Visual Studio, the continue command can be accessed while already debugging a program via Debug menu > Continue , or by pressing the F5 shortcut key. For Code::Blocks users In Code::Blocks, the continue command can be accessed while already debugging a program via Debug menu > Start / Continue , or by pressing the F8 shortcut key. Let\u2019s test out the continue command. If your execution marker isn\u2019t already on line 5, run to cursor to line 5. Then choose continue from this point. Your program will finish executing and then terminate. Start The continue command has a twin brother named start . The start command performs the same action as continue , just starting from the beginning of the program. It can only be invoked when not already in a debug session. For Visual Studio users In Visual Studio, the start command can be accessed while not debugging a program via Debug menu > Start Debugging , or by pressing the F5 shortcut key. For Code::Blocks users In Code::Blocks, the start command can be accessed while not debugging a program via Debug menu > Start / Continue , or by pressing the F8 shortcut key. If you use the start command on the above sample program, it will run all the way through without interruption. While this may seem unremarkable, that\u2019s only because we haven\u2019t told the debugger to interrupt the program. We\u2019ll put this command to better use in the next section. Breakpoints The last topic we are going to talk about in this section is breakpoints. A breakpoint is a special marker that tells the debugger to stop execution of the program at the breakpoint when running in debug mode. For Visual Studio users In Visual Studio, you can set or remove a breakpoint via Debug menu > Toggle Breakpoint , or by right clicking on a statement and choosing Toggle Breakpoint from the context menu, or by pressing the F9 shortcut key, or by clicking to the left of the line number (in the light grey area). For Code::Blocks users In Code::Blocks, you can set or remove a breakpoint via Debug menu > Toggle breakpoint , or by right clicking on a statement and choosing Toggle breakpoint from the context menu, or by pressing the F5 shortcut key, or by clicking to the right of the line number. When you set a breakpoint, you will see a new type of icon appear. Visual Studio uses a red circle, Code::Blocks uses a red octagon (like a stop sign): Go ahead and set a breakpoint on the line 5, as shown in the image above. Now, choose the Start command to have the debugger run your code, and let\u2019s see the breakpoint in action. You will notice that instead of running all the way to the end of the program, the debugger stops at the breakpoint (with the execution marker sitting on top of the breakpoint icon): It\u2019s just as if you\u2019d run to cursor to this point. Breakpoints have a couple of advantages over run to cursor . First, a breakpoint will cause the debugger to return control to you every time they are encountered (unlike run to cursor , which only runs to the cursor once each time it is invoked). Second, you can set a breakpoint and it will persist until you remove it, whereas with run to cursor you have to locate the spot you want to run to each time you invoke the command. Note that breakpoints placed on lines that are not in the path of execution will not cause the debugger to halt execution of the code. Let\u2019s take a look at a slightly modified program that better illustrates the difference between breakpoints and run to cursor : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include void printValue ( int value ) { std :: cout << value ; } int main () { printValue ( 5 ); printValue ( 6 ); printValue ( 7 ); return 0 ; } COPY First, start a new debugging session and then do a run to cursor to line 5. Now choose continue . The program will continue to the end (it won\u2019t stop on line 5 again, even though line 5 is executed twice more). Next, place a breakpoint on line 5, then choose start . The program will stop on line 5. Now choose continue . The program will stop on line 5 a second time. Choose continue_again, and it will stop a third time. One more _continue , and the program will terminate. You can see that the breakpoint caused the program to stop as many times as that line was executed. Set next statement There\u2019s one more debugging command that\u2019s used fairly uncommonly, but is still at least worth knowing about, even if you won\u2019t use it very often. The set next statementcommand allows us to change the point of execution to some other statement (sometimes informally called jumping ). This can be used to jump the point of execution forwards and skip some code that would otherwise execute, or backwards and have something that already executed run again. For Visual Studio users In Visual Studio, you can jump the point of execution by right clicking on a statement and choosing Set next statement from the context menu, or by pressing the Ctrl-Shift-F10 shortcut combination. This option is contextual and only occurs while already debugging a program. For Code::Blocks users In Code::Blocks, you can jump the point of execution via Debug menu > Set next statement , or by right clicking on a statement and choosing Set next statement from the context menu. Code::Blocks doesn\u2019t have a keyboard shortcut for this command. Let\u2019s see jumping forwards in action: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include void printValue ( int value ) { std :: cout << value ; } int main () { printValue ( 5 ); printValue ( 6 ); printValue ( 7 ); return 0 ; } COPY First, run to cursor to line 11. At this point, you should see the value of 5 in the console output window. Now, right click on line 12, and choose set next statement . This causes line 11 to be skipped and not execute. Then choose continue to finish executing your program. The output of your program should look like this: 57 We can see that printValue(6) was skipped. This functionality can be useful in several contexts. In our exploration of basic debugging techniques, we discussed commenting out a function as a way to determine whether that function had a role in causing an issue. This requires modifying our code, and remembering to uncomment the function later. In the debugger, there\u2019s no direct way to skip a function, so if you decide you want to do this, using set next statement to jump over a function call is the easiest way to do so. Jumping backwards can also be useful if we want to watch a function that just executed run again, so we can see what it is doing. With the same code above, run to cursor to line 12. Then set next statement on line 11, and continue . The program\u2019s output should be: 5667 Warning The set next statement command will change the point of execution, but will not otherwise change the program state. Your variables will retain whatever values they had before the jump. As a result, jumping may cause your program to produce different values, results, or behaviors than it would otherwise. Use this capability judiciously (especially jumping backwards). Warning You should not use set next statement to change the point of execution to a different function. This will result in undefined behavior, and likely a crash. Conclusion You now learned the major ways that you can use an integrated debugger to watch and control how your program executes. While these commands can be useful for diagnosing code flow issues (e.g. to determine if certain functions are or aren\u2019t being called), they are only a portion of the benefit that the integrated debugger brings to the table. In the next lesson, we\u2019ll start exploring additional ways to examine your program\u2019s state, for which you\u2019ll need these commands as a prerequisite. Let\u2019s go!","title":"Run to cursor"},{"location":"3-8-Using-an-integrated-debugger-Watching-variables/","tags":["debugger"],"text":"In the previous lessons ( 3.6 -- Using an integrated debugger: Stepping and 3.7 -- Using an integrated debugger: Running and breakpoints ), you learned how to use the debugger to watch the path of execution through your program. However, stepping through a program is only half of what makes the debugger useful. The debugger also lets you examine the value of variables as you step through your code, all without having to modify your code. As per previous lessons, our examples here will use Visual Studio -- if you are using a different IDE/debugger, the commands may have slightly different names or be located in different locations. Warning In case you are returning, make sure your project is compiled using a debug build configuration (see 0.9 -- Configuring your compiler: Build configurations for more information). If you\u2019re compiling your project using a release configuration instead, the functionality of the debugger may not work correctly. Watching variables Watching a variable is the process of inspecting the value of a variable while the program is executing in debug mode. Most debuggers provide several ways to do this. Let\u2019s take a look at a sample program: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int x { 1 }; std :: cout << x << ' ' ; x = x + 2 ; std :: cout << x << ' ' ; x = x + 3 ; std :: cout << x << ' ' ; return 0 ; } COPY This is a pretty straightforward sample program -- it prints the numbers 1, 3, and 6. First, run to cursor to line 6. At this point, the variable x has already been created and initialized with the value 1, so when we examine the value of x, we should expect to see the value 1. The easiest way to examine the value of a simple variable like x is to hover your mouse over the variable x. Some modern debuggers support this method of inspecting simple variables, and it is the most straightforward way to do so. For Code::Blocks users If you\u2019re using Code::Blocks, this option is (inexplicably) off by default. Let\u2019s turn it on. First, go to Settings menu > Debugger\u2026 . Then under the GDB/CDB debugger node , select the Default profile. Finally, check the box labeled Evaluate expression under cursor . Hover your mouse cursor over variable x on line 6, and you should see something like this: Note that you can hover over any variable x, not just the one on the current line. For example, if we hover over the x on line 12, we\u2019ll see the same value: If you\u2019re using Visual Studio, you can also use QuickWatch. Highlight the variable name x with your mouse, and then choose \u201cQuickWatch\u201d from the right-click menu. This will pull up a subwindow containing the current value of the variable: Go ahead and close QuickWatch if you opened it. Now let\u2019s watch this variable change as we step through the program. Either choose step over twice, or run to cursor to line 9. The variable x should now have value 3 . Inspect it and make sure that it does! The watch window Using the mouse hover or QuickWatch methods to inspect variables is fine if you want to know the value of a variable at a particular point in time, but it\u2019s not particularly well suited to watching the value of a variable change as you run the code because you continually have to rehover/reselect the variable. In order to address this issue, all modern integrated debuggers provide another feature, called a watch window. The watch window is a window where you can add variables you would like to continually inspect, and these variables will be updated as you step through your program. The watch window may already be on your screen when you enter debug mode, but if it is not, you can bring it up through your IDE\u2019s window commands (these are typically found in a View or Debug menu). For Visual Studio users In Visual Studio, the watch menu can be found at Debug menu > Windows > Watch > Watch 1 . Do note that you have to be in debug mode for this option to be enabled, so step into your program first. Where this window appears (docked left, right, or bottom) may vary. You can change where it is docked by dragging the Watch 1 tab to a different side of the application window. For Code::Blocks users In Code::Blocks, the watch menu can be found at Debug menu > Debugging windows > Watches . This window will likely appear as a separate window. You can dock it into your main window by dragging it over. You should now see something like this: The watches window may or may not contain anything in it already. There are typically two different ways to add variables to the watch window: Pull up the watch window, and type in the name of the variable you would like to watch in the leftmost column of the watch window. In the code window, right click on the variable you\u2019d like to watch, and choose Add Watch (Visual Studio) or Watch x (replace x with the variable\u2019s name) (Code::Blocks). If you\u2019re not already in a debugging session with the execution marker on line 9 of your program, start a new debugging session and run to cursor to line 9. Now, go ahead and add the variable \u201cx\u201d to your watch list. You should now see this: Now step over twice, or run to cursor to line 12, and you should see the value of x change from 3 to 6 . Variables that go out of scope (e.g. a local variable inside a function that has already returned to the caller) will stay in your watch window, but will generally either be marked as \u201cnot available\u201d, or may show the last known value but grayed out. If the variable returns to scope (e.g. the function is called again), its value will begin showing again. Therefore, it\u2019s fine to leave variables in the watch window, even if they\u2019re out of scope. Using watches is the best way to watch the value of a variable change over time as you step through your program. The watch window can evaluate expressions too The watch window will also allow you to evaluate simple expressions. If you haven\u2019t already, run to cursor to line 12. Then try entering x + 2 into the watch window and see what happens (it should evaluate to 8). You can also highlight an expression in your code and then inspect the value of that expression via hover or by adding it to the watch window via the right-click context menu. Warning Identifiers in watched expressions will evaluate to their current values. If you want to know what value an expression in your code is actually evaluating to, run to cursor to it first, so that all identifiers have the correct values. Local watches Because inspecting the value of local variables inside a function is common while debugging, many debuggers will offer some way to quickly watch the value of all local variables in scope. For Visual Studio users In Visual Studio, you can see the value of all local variables in the Locals window, which can be found at Debug menu > Windows > Locals . Note that you have to be in a debug session to activate this window. For Code::Blocks users In Code::Blocks, this is integrated into the Watch window, under the Locals node. If you don\u2019t see any, there either aren\u2019t any, or you need to uncollapse the node. If you\u2019re just looking to watch the value of a local variable, check the locals window first. It should already be there.","title":"3.8 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u76d1\u63a7\u53d8\u91cf"},{"location":"3-9-Using-an-integrated-debugger-The-call-stack/","tags":["debugger","call stack"],"text":"Modern debuggers contain one more debugging information window that can be very useful in debugging your program, and that is the call stack window. When your program calls a function, you already know that it bookmarks the current location, makes the function call, and then returns. How does it know where to return to? The answer is that it keeps track in the call stack. The call stack is a list of all the active functions that have been called to get to the current point of execution. The call stack includes an entry for each function called, as well as which line of code will be returned to when the function returns. Whenever a new function is called, that function is added to the top of the call stack. When the current function returns to the caller, it is removed from the top of the call stack, and control returns to the function just below it. The call stack window is a debugger window that shows the current call stack. If you don\u2019t see the call stack window, you will need to tell the IDE to show it. For Visual Studio users In Visual Studio, the call stack window can be found via Debug menu > Windows > Call Stack . Note that you have to be in a debug session to activate this window. For Code::Blocks users In Code::Blocks, the call stack window can be found via Debug menu > Debugging windows > Call stack . Let\u2019s take a look at the call stack using a sample program: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include void a () { std :: cout << \"a() called \\n \" ; } void b () { std :: cout << \"b() called \\n \" ; a (); } int main () { a (); b (); return 0 ; } COPY Put breakpoints on lines 5 and 10 of this program, and then start debugging mode. Because function a is called first, the breakpoint on line 5 will be hit first. At this point, you should see something like this: Note that your line numbers may be slightly different (off by 1), and that instead of [External Code] you may see a bunch of other crazily named functions. These differences are insubstantial. What\u2019s relevant here is the top two lines. From the bottom up, we can see that function main was called first, and then that function a was called next. The line 5 next to function a shows us where the current point of execution is (which matches the execution marker in the code window). The line 17 on the second line indicates the line that will be returned to when control returns to function main . Tip The line numbers after the function names show the next line to be executed in each function. Since the top entry on the call stack represents the currently executing function, the line number here shows the next line that will execute when execution resumes. The remaining entries in the call stack represent functions that will be returned to at some point, so the line number for these represent the next statement that will execute after the function is returned to. Now, choose the continue debug command to advance execution to the next breakpoint, which will be on line 10. The call stack should update to reflect the new situation: You\u2019ll notice that function b is now the top line of the call stack, reflecting the fact that function b is the function that is actively being executed. Note that function a is no longer represented on the call stack. This is because function a was removed from the call stack when it returned. Choose the continue debug command one more time, and we\u2019ll hit the breakpoint on line 5 again (because function b calls function a ). The call stack will look like this: There are now three functions on the call stack: (from bottom to top) main , which called function b , which called function a . The call stack is useful in conjunction with breakpoints, when your breakpoint is hit and you want to know what functions were called to get to that specific point in the code. Conclusion Congratulations, you now know the basics of using an integrated debugger! Using stepping, breakpoints, watches, and the call stack window, you now have the fundamentals to be able to debug almost any problem. Like many things, becoming good at using a debugger takes some practice and some trial and error. But again, we\u2019ll reiterate the point that the time devoted to learning how to use an integrated debugger effectively will be repaid many times over in time saved debugging your programs!","title":"3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808"},{"location":"3-x-Chapter-3-summary-and-quiz/","tags":["summary"],"text":"3.1 - \u8bed\u6cd5\u548c\u8bed\u4e49\u9519\u8bef \u5982\u679c\u4f60\u7684\u8bed\u53e5\u4e0d\u7b26\u5408 C++ \u7684\u8bed\u6cd5\uff0c\u5219\u4f1a\u4ea7\u751f \u8bed\u6cd5\u9519\u8bef \u3002\u7f16\u8bd1\u5668\u6216\u6355\u83b7\u8bed\u6cd5\u9519\u8bef\u3002 \u5f53\u8bed\u53e5\u7684\u8bed\u6cd5\u6b63\u786e\uff0c\u4f46\u662f\u5176\u884c\u4e3a\u5e76\u4e0d\u7b26\u5408\u7a0b\u5e8f\u5458\u7684\u672c\u610f\uff0c\u6b64\u65f6\u79f0\u4e3a \u8bed\u4e49\u9519\u8bef \u3002 3.2 - \u4ee3\u7801\u8c03\u8bd5\u6b65\u9aa4 \u5b9a\u4f4d\u5e76\u4fee\u590d\u7a0b\u5e8f\u9519\u8bef\u7684\u8fc7\u7a0b\uff0c\u79f0\u4e3adebugging\u3002 \u53ef\u4ee5\u4f7f\u7528\u4e94\u6b65\u6cd5\u6765\u5b9a\u4f4d\u5e76\u4fee\u590d\u7a0b\u5e8f\u9519\u8bef\uff1a \u5b9a\u4f4d\u6839\u56e0\uff1b \u7406\u89e3\u95ee\u9898\uff1b \u786e\u5b9a\u4fee\u590d\u65b9\u5f0f\uff1b \u4fee\u590d\u95ee\u9898\uff1b \u91cd\u65b0\u6d4b\u8bd5 debug\u8fc7\u7a0b\u4e2d\u6700\u56f0\u96be\u7684\u90e8\u5206\u5c31\u662f\u627e\u5230\u95ee\u9898\u6240\u5728\u3002 \u9759\u6001\u5206\u6790\u5de5\u5177\u53ef\u4ee5\u7528\u6765\u5bf9\u4ee3\u7801\u8fdb\u884c\u5206\u6790\uff0c\u67e5\u627e\u53ef\u80fd\u5bfc\u81f4\u95ee\u9898\u7684\u8bed\u4e49\u9519\u8bef\u3002 debug\u8fc7\u7a0b\u4e2d\uff0c\u6700\u91cd\u8981\u7684\u4e00\u6b65\u5c31\u662f\u8981\u80fd\u591f\u7a33\u5b9a\u5730\u590d\u73b0\u95ee\u9898\u3002 \u4ee5\u4e0b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9a\u4f4d\u95ee\u9898\uff1a \u6ce8\u91ca\u6389\u4ee3\u7801\uff1b \u4f7f\u7528\u8f93\u51fa\u8bed\u53e5\u6765\u9a8c\u8bc1\u4ee3\u7801\u6d41\u7a0b\uff1b \u6253\u5370\u503c\u3002 \u5f53\u4f7f\u7528\u6253\u5370\u8bed\u53e5\u65f6\uff0c\u63a8\u8350\u4f7f\u7528 std::cerr \u6765\u4ee3\u66ff std::cout \uff0c\u4e0d\u8fc7\u6700\u597d\u907f\u514d\u4f9d\u8d56\u6253\u5370\u8bed\u53e5\u6765\u5b9a\u4f4d\u95ee\u9898\u3002 \u65e5\u5fd7\u6587\u4ef6\u8bb0\u5f55\u4e86\u7a0b\u5e8f\u4e2d\u7684\u5404\u4e2a\u4e8b\u4ef6\u3002\u5c06\u4fe1\u606f\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\u7684\u8fc7\u7a0b\uff0c\u79f0\u4e3a\u5199\u65e5\u5fd7\uff08logging\uff09\u3002 \u5728\u4fdd\u8bc1\u4ee3\u7801\u884c\u4e3a\u4e0d\u53d8\u7684\u524d\u63d0\u4e0b\uff0c\u4fee\u6539\u4ee3\u7801\u7ed3\u6784\uff0c\u79f0\u4e3a \u91cd\u6784 \u3002\u91cd\u6784\u7ecf\u5e38\u88ab\u7528\u6765\u4fee\u6539\u4ee3\u7801\u4f7f\u5176\u66f4\u52a0\u6709\u6761\u7406\u3001\u6a21\u5757\u5316\u6216\u8005\u4f7f\u5176\u6027\u80fd\u66f4\u597d\u3002 \u5355\u5143\u6d4b\u8bd5\u662f\u4e00\u79cd\u8f6f\u4ef6\u6d4b\u8bd5\u65b9\u6cd5\uff0c\u5b83\u5bf9\u4e00\u4e2a\u4e2a\u5355\u5143\u7684\u6e90\u7801\u8fdb\u884c\u6d4b\u8bd5\u6765\u5224\u65ad\u8f6f\u4ef6\u4ee3\u7801\u662f\u5426\u6b63\u786e\u3002 \u9632\u5fa1\u5f0f\u7f16\u7a0b\u601d\u60f3\u8981\u6c42\u7a0b\u5e8f\u5458\u9884\u6d4b\u8f6f\u4ef6\u53ef\u80fd\u906d\u9047\u7684\u7684\u5404\u79cd\u8bef\u7528\u3002\u8fd9\u4e9b\u8bef\u7528\u901a\u5e38\u662f\u53ef\u4ee5\u88ab\u68c0\u6d4b\u548c\u9884\u9632\u7684\u3002 \u5bf9\u7a0b\u5e8f\u8fd0\u884c\u8fdb\u884c\u8ffd\u8e2a\u7684\u5404\u9879\u4fe1\u606f\uff08\u53d8\u91cf\u7684\u503c\u3001\u88ab\u8c03\u7528\u7684\u51fd\u6570\u3001\u5f53\u524d\u6267\u884c\u70b9\uff09\u90fd\u662f\u7a0b\u5e8f\u72b6\u6001\u7684\u7b2c\u4e00\u90e8\u5206\u3002 3.6 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u5355\u6b65\u8c03\u8bd5 3.7 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8fd0\u884c\u548c\u65ad\u70b9 3.8 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u76d1\u63a7\u53d8\u91cf 3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808 \u8c03\u8bd5\u5668\uff08debugger\uff09\u53ef\u4ee5\u5e2e\u52a9\u7a0b\u5e8f\u5458\u63a7\u5236\u7a0b\u5e8f\u7684\u8fd0\u4ee5\u53ca\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u67e5\u770b\u7a0b\u5e8f\u72b6\u6001\u3002\u96c6\u6210\u8c03\u8bd5\u5668\u6307\u7684\u662f\u54ea\u4e9b\u96c6\u6210\u5728\u4ee3\u7801\u7f16\u8f91\u5668\u4e2d\u7684\u8c03\u8bd5\u5668\u3002 - \u5355\u6b65\u8c03\u8bd5\u662f\u8c03\u8bd5\u5668\u7684\u4e00\u4e2a\u529f\u80fd\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u9010\u8bed\u53e5\u5730\u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8c03\u8bd5\uff1b - \u5355\u6b65\u8fdb\u5165\u7a0b\u5e8f\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u8fd0\u884c\u4e0b\u4e00\u4e2a\u6761\u8bed\u53e5\u7136\u540e\u505c\u6b62\u3002\u5982\u679c\u8be5\u8bed\u53e5\u4e2d\u5305\u542b\u51fd\u6570\u8c03\u7528\uff0c\u5355\u6b65\u8fdb\u5165\uff08step into\uff09\u53ef\u4ee5\u8df3\u8f6c\u5230\u8be5\u51fd\u6570\u7684\u9876\u90e8\uff1b - \u5355\u6b65\u8df3\u8fc7\u4f1a\u6267\u884c\u4e0b\u4e00\u6761\u8bed\u53e5\uff0c\u7136\u540e\u505c\u6b62\u6267\u884c\u3002\u5982\u679c\u8be5\u8bed\u53e5\u5305\u542b\u51fd\u6570\u8c03\u7528\uff0c\u5219\u5355\u6b65\u8df3\u8fc7\u4f1a\u6267\u884c\u8be5\u51fd\u6570\u5e76\u5c06\u63a7\u5236\u6743\u4ea4\u8fd8\u7ed9\u4f60\uff1b - \u5355\u6b65\u8df3\u51fa\u4f1a\u6267\u884c\u51fd\u6570\u4e2d\u5269\u4e0b\u7684\u8bed\u53e5\u7136\u540e\u5728\u51fd\u6570\u8fd4\u56de\u540e\u5c06\u63a7\u5236\u6743\u4ea4\u7ed9\u4f60\uff1b - \u8fd0\u884c\u5230\u5149\u6807\u51fa\u4f1a\u6301\u7eed\u6267\u884c\u4ee3\u7801\u76f4\u5230\u5230\u8fbe\u4f60\u9f20\u6807\u6240\u9009\u5904\uff1b - \u7ee7\u7eed\u6267\u884c\uff0c\u4f1a\u6301\u7eed\u6267\u884c\u7a0b\u5e8f\u4ee3\u7801\u76f4\u5230\u9047\u5230\u4e0b\u4e00\u4e2a\u65ad\u70b9\uff1b - \u542f\u52a8\u6267\u884c\u548c\u7ee7\u7eed\u6267\u884c\u7c7b\u4f3c\uff0c\u4e0d\u8fc7\u5b83\u4f1a\u4ece\u7a0b\u5e8f\u7684\u5f00\u59cb\u5904\u8fdb\u884c\u6267\u884c\uff1b - \u65ad\u70b9 \u662f\u6807\u8bb0\u8c03\u8bd5\u5668\u5e94\u8be5\u505c\u6b62\u6267\u884c\u7a0b\u5e8f\u7684\u4f4d\u7f6e\uff1b - \u76d1\u89c6\u53d8\u91cf\u5141\u8bb8\u6211\u4eec\u5728\u7a0b\u5e8f\u8fd0\u884c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u67e5\u770b\u53d8\u91cf\u7684\u503c\u3002\u76d1\u89c6\u7a97\u53e3\u4e2d\u53ef\u4ee5\u770b\u5230\u88ab\u76d1\u89c6\u7684\u53d8\u91cf\u548c\u8868\u8fbe\u5f0f\uff1b - \u8c03\u7528\u6808\u662f\u4e00\u4e2a\u5305\u542b\u4e86\u5f53\u524d\u6d3b\u52a8\u51fd\u6570\u7684\u5217\u8868\u3002\u8c03\u7528\u6808\u7a97\u53e3\u4e2d\u4f1a\u663e\u793a\u7a0b\u5e8f\u7684\u8c03\u7528\u6808\u3002","title":"3.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4ee3\u7801\u8c03\u8bd5"},{"location":"4-1-Introduction-to-fundamental-data-types/","tags":["data type","C++11"],"text":"Key Takeaway \u53d8\u91cf\u53ea\u662f\u4e00\u6bb5\u53ef\u4ee5\u5b58\u653e\u4fe1\u606f\u7684\u5185\u5b58\u7684\u540d\u79f0 \u4f7f\u7528 \u6570\u636e\u7c7b\u578b \uff08\u901a\u5e38\u7b80\u79f0\u4e3a\u7c7b\u578b\uff09\u544a\u8bc9\u8ba1\u7b97\u673a\u5982\u4f55\u5c06\u5185\u5b58\u4e2d\u7684\u6570\u636e\u89e3\u6790\u4e3a\u6709\u610f\u4e49\u7684\u503c \u4e00\u4e2abyte\u901a\u5e38\u7b49\u4e8e8\u4e2abit \u201c\u6574\u6570\u578b\u7684\u7c7b\u578b\u201d\uff0c\u5305\u62ec\u5e03\u5c14\u7c7b\u578b\u3001\u5b57\u7b26\u3001\u6574\u578b\u548c\u679a\u4e3e \u5b57\u7b26\u4e32\u4e0d\u5c5e\u4e8e C++ \u7684\u57fa\u672c\u7c7b\u578b\uff0c\u5b83\u5c5e\u4e8e\u590d\u5408\u7c7b\u578b\uff0c\u7531 std::string \u5b9a\u4e49 Bits\u3001bytes \u548c\u5185\u5b58\u5730\u5740 \u5728 1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u4e8b\u5b9e\u4e0a\u53d8\u91cf\u53ea\u662f\u4e00\u6bb5\u53ef\u4ee5\u5b58\u653e\u4fe1\u606f\u7684\u5185\u5b58\u7684\u540d\u79f0\u3002\u7b80\u5355\u56de\u5fc6\u4e00\u4e0b\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u4f7f\u7528\u8ba1\u7b97\u673a\u63d0\u4f9b\u7684 \u968f\u673a\u8bbf\u95ee\u5185\u5b58(RAM) \u3002\u5f53\u53d8\u91cf\u88ab\u5b9a\u4e49\u7684\u65f6\u5019\uff0c\u4e00\u5757\u5185\u5b58\u88ab\u5173\u8054\u5230\u4e86\u8be5\u53d8\u91cf\u3002 \u5185\u5b58\u7684\u6700\u5c0f\u5355\u5143\u662f\u4e00\u4e2a\u4e8c\u8fdb\u5236\u4f4d\uff08\u4e5f\u79f0\u4e3a \u4f4d \uff09\uff0c\u5b83\u53ef\u4ee5\u5b58\u50a8 0 \u6216 1.\u4f60\u53ef\u4ee5\u628a\u4e00\u4e2a\u6bd4\u7279\u770b\u6210\u662f\u4e00\u4e2a\u706f\u6ce1\u7684\u5f00\u5173\u2014\u2014\u5b83\u53ea\u6709\u5f00\uff081\uff09\u548c\u5173\uff080\uff09\u4e24\u79cd\u72b6\u6001\uff0c\u6ca1\u6709\u5176\u4ed6\u4efb\u4f55\u4e2d\u95f4\u72b6\u6001\u3002\u5982\u679c\u4f60\u53bb\u89c2\u5bdf\u4e00\u6bb5\u5185\u5b58\u5185\u5bb9\u7684\u8bdd\uff0c\u4f60\u603b\u80fd\u770b\u5230 \u2026011010100101010\u2026 \u6216\u5176\u4ed6\u7c7b\u4f3c\u7ec4\u5408\u3002 \u6211\u4eec\u5c06\u5185\u5b58\u5206\u5272\u6210\u4e00\u7cfb\u5217\u8fde\u7eed\u7684\u5355\u5143\uff0c\u79f0\u4e3a\u5185\u5b58\u5730\u5740\u3002\u7c7b\u4f3c\u4e8e\u8857\u9053\u5730\u5740\u53ef\u4ee5\u7528\u6765\u67e5\u627e\u8857\u9053\u4e0a\u67d0\u5e62\u5efa\u7b51\uff0c\u901a\u8fc7\u5185\u5b58\u5730\u5740\u6211\u4eec\u4e5f\u53ef\u4ee5\u627e\u5230\u7279\u5b9a\u5730\u5740\u4e2d\u5b58\u653e\u7684\u5185\u5bb9\u3002 \u4e0d\u591f\u6709\u4ef6\u4e8b\u53ef\u80fd\u51fa\u4e4e\u4f60\u7684\u610f\u6599\uff0c\u5bf9\u4e8e\u73b0\u4ee3\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u6765\u8bf4\uff0c\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684bit\u5e76\u4e0d\u90fd\u5177\u6709\u4e00\u4e2a\u552f\u4e00\u7684\u5730\u5740\u3002\u8fd9\u662f\u56e0\u4e3a\u5185\u5b58\u5730\u5740\u7684\u6570\u91cf\u662f\u6709\u9650\u7684\uff0c\u800c\u4e14\u4e5f\u5e76\u4e0d\u9700\u8981\u4e3a\u6bcf\u4e2abit\u90fd\u6307\u5b9a\u4e00\u4e2a\u552f\u4e00\u7684\u5730\u5740\u3002\u5b9e\u9645\u4e0a\uff0c\u6bcf\u4e2a\u5185\u5b58\u5730\u5740\u8868\u793a\u4e00\u4e2a \u5b57\u8282 \u7684\u5730\u5740\u3002\u4e00\u4e2a\u5b57\u8282\u8868\u793a\u4e00\u7ec4\u88ab\u4f5c\u4e3a\u4e00\u4e2a\u5355\u5143\u64cd\u4f5c\u7684\u6bd4\u7279\u3002\u4e00\u822c\u6765\u8bb2\uff08\u73b0\u4ee3\u8ba1\u7b97\u673a\uff09\u4e00\u4e2a\u5b57\u8282\u5305\u542b8\u4e2a\u8fde\u7eed\u7684 bit\u3002 \u5173\u952e\u4fe1\u606f \u5728 C++\uff0c\u6211\u4eec\u901a\u5e38\u4ee5 byte \u4e3a\u5355\u4f4d\u64cd\u4f5c\u6570\u636e\u3002 \u4e0b\u56fe\u663e\u793a\u4e86\u4e00\u4e9b\u5185\u5b58\u5730\u5740\u53ca\u5176\u5185\u90e8\u5b58\u653e\u7684\u6570\u636e\uff08byte\u4e3a\u5355\u4f4d\uff09\uff1a \u9898\u5916\u8bdd \u5728\u4e00\u4e9b\u8001\u7684\u6216\u8005\u975e\u6807\u7684\u8ba1\u7b97\u673a\u4e0a\uff0c\u4e00\u4e2a\u5b57\u8282\u53ef\u80fd\u8868\u793a\u4e0d\u540c\u957f\u5ea6\u7684\u6bd4\u7279\uff081 \u5230 48 \u6bd4\u7279\u90fd\u6709\u53ef\u80fd\uff09\u2014\u2014 \u4e0d\u8fc7\uff0c\u4f60\u901a\u5e38\u4e0d\u9700\u8981\u62c5\u5fc3\u8fd9\u4e2a\u95ee\u9898\uff0c\u56e0\u4e3a\u73b0\u4ee3\u8ba1\u7b97\u673a\u7684\u4e8b\u5b9e\u6807\u51c6\u5c318\u6bd4\u7279\uff0c\u6211\u4eec\u53ef\u4ee5\u5047\u8bbe1\u4e2a\u5b57\u8282\u5c31\u662f8\u4e2a\u6bd4\u7279\u3002 \u6570\u636e\u7c7b\u578b \u7531\u4e8e\u8ba1\u7b97\u673a\u4e0a\u6240\u6709\u7684\u6570\u636e\u90fd\u53ea\u662f\u4e00\u4e9b\u8fde\u7eed\u7684\u6bd4\u7279\uff0c\u6211\u4eec\u5fc5\u987b\u8981\u4f7f\u7528 \u6570\u636e\u7c7b\u578b \uff08\u901a\u5e38\u7b80\u79f0\u4e3a\u7c7b\u578b\uff09\u544a\u8bc9\u8ba1\u7b97\u673a\u5982\u4f55\u5c06\u5185\u5b58\u4e2d\u7684\u6570\u636e\u89e3\u6790\u4e3a\u6709\u610f\u4e49\u7684\u503c\u3002\u6211\u4eec\u5df2\u7ecf\u5b66\u4e60\u4e86\u4e00\u79cd\u6570\u636e\u7c7b\u578b\uff1a\u6574\u578b\u3002\u5f53\u4e00\u4e2a\u6574\u578b\u53d8\u91cf\u88ab\u5b9a\u4e49\u65f6\uff0c\u6211\u4eec\u544a\u8bc9\u8ba1\u7b97\u673a\u201d\u8be5\u53d8\u91cf\u4f7f\u7528\u7684\u8fd9\u4e00\u6bb5\u5185\u5b58\u9700\u8981\u88ab\u89e3\u6790\u4e3a\u4e00\u4e2a\u6574\u578b\u503c\u201c\u3002 \u5f53\u4e3a\u4e00\u4e2a\u5bf9\u8c61\u8fdb\u884c\u8d4b\u503c\u65f6\uff0c\u7f16\u8bd1\u5668\u548cCPU\u4f1a\u5c06\u8fd9\u4e2a\u503c\u7f16\u7801\u6210\u4e00\u7cfb\u5217\u8fde\u7eed\u7684 bit\u7136\u540e\u5b58\u653e\u5230\u5185\u5b58\u4e2d\uff08\u6ce8\u610f\uff1a\u5185\u5b58\u53ea\u80fd\u5b58\u653e\u6bd4\u7279\uff09\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5c06\u4e00\u4e2a\u6574\u578b\u7684\u5bf9\u8c61\u8d4b\u503c\u4e3a65\uff0c\u5219\u8be5\u503c\u4f1a\u88ab\u8f6c\u6362\u4e3a 0100 0001 \u540e\u5b58\u653e\u5728\u5185\u5b58\u5173\u8054\u7684\u5730\u5740\u4e2d\u3002 \u53cd\u8fc7\u6765\uff0c\u5f53\u5bf9\u8c61\u88ab\u6c42\u503c\u65f6\uff0c\u8fd9\u4e9b\u8fde\u7eed\u7684 bit \u4f1a\u88ab\u91cd\u65b0\u7ec4\u7ec7\u6210\u539f\u6765\u7684\u503c\uff0c\u5373 0100 0001 \u4f1a\u88ab\u8f6c\u6362\u4e3a 65\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u7f16\u8bd1\u5668\u548cCPU\u4f1a\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u4e0a\u8ff0\u5de5\u4f5c\uff0c\u6211\u4eec\u901a\u5e38\u4e0d\u9700\u8981\u62c5\u5fc3\u8fd9\u4e9b\u503c\u662f\u5982\u4f55\u88ab\u8f6c\u6362\u7684\u3002 \u6211\u4eec\u7684\u5de5\u4f5c\u4ec5\u4ec5\u662f\u4e3a\u53d8\u91cf\u6307\u5b9a\u4e00\u4e2a\u6211\u4eec\u9700\u8981\u7684\u503c\u3002 \u57fa\u672c\u6570\u636e\u7c7b\u578b C++ \u5177\u6709\u5f88\u591a\u5185\u7f6e\u7684\u6570\u636e\u7c7b\u578b\uff0c\u79f0\u4e3a\u57fa\u7840\u6570\u636e\u7c7b\u578b\uff0c\u4e5f\u7ecf\u5e38\u88ab\u79f0\u4e3a\u57fa\u672c\u7c7b\u578b\u3001\u539f\u59cb\u7c7b\u578b\u6216\u5185\u7f6e\u7c7b\u578b\u3002 \u4e0b\u8868\u662f\u4e00\u4e9b\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u6709\u4e9b\u53ef\u80fd\u4f60\u5df2\u7ecf\u89c1\u8fc7\u4e86\uff1a \u7c7b\u578b \u5206\u7c7b \u542b\u4e49 \u4f8b\u5b50 float Floating Point a number with a fractional part 3.14159 double Floating Point a number with a fractional part 3.14159 long double Floating Point a number with a fractional part 3.14159 bool Integral (Boolean) true or false true char Integral (Character) a single character of text \u2018c\u2019 wchar_t Integral (Character) a single character of text \u2018c\u2019 char8_t (C++20) Integral (Character) a single character of text \u2018c\u2019 char16_t (C++11) Integral (Character) a single character of text \u2018c\u2019 char32_t (C++11) Integral (Character) a single character of text \u2018c\u2019 short Integral (Integer) positive and negative whole numbers, including 0 64 int Integral (Integer) positive and negative whole numbers, including 0 64 long Integral (Integer) positive and negative whole numbers, including 0 64 long long (C++11) Integral (Integer) positive and negative whole numbers, including 0 64 std::nullptr_t (C++11) Null Pointer a null pointer nullptr void Void no type n/a \u672c\u7ae0\u5c06\u76f8\u7ee7\u4ecb\u7ecd\u8fd9\u4e9b\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff08\u9664\u4e86 std::nullptr_t \uff0c\u6211\u4eec\u4f1a\u5728\u8ba8\u8bba\u6307\u9488\u65f6\u8ba8\u8bba\u5b83\uff09\u3002C++ \u4e5f\u652f\u6301\u5f88\u591a\u590d\u6742\u7c7b\u578b\uff0c\u79f0\u4e3a \u590d\u5408\u7c7b\u578b \uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u7ae0\u8282\u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u4f5c\u8005\u6ce8 \u672f\u8bed\u6574\u578b\uff08interger\uff09\u548c\u6574\u6570\u7684\uff08integral\uff09\u7c7b\u4f3c\uff0c\u4f46\u662f\u610f\u601d\u5e76\u4e0d\u5b8c\u5168\u76f8\u540c\u3002 \u6574\u578b \u6307\u7684\u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u653e\u975e\u5c0f\u6570\u503c\u7684\u7279\u5b9a\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u6574\u6570\u30010\u6216\u8005\u8d1f\u6574\u6570\u3002\u800c \u6574\u6570\u578b\u7684 \u5219\u8868\u793a\u201c\u50cf\u6574\u578b\u201d\u3002\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6574\u578b\u7684\u4e00\u8bcd\u88ab\u7ec4\u6210\u5176\u4ed6\u672f\u8bed\uff0c\u4f8b\u5982\u201c\u6574\u6570\u578b\u7684\u7c7b\u578b\u201d\uff0c\u5305\u62ec\u5e03\u5c14\u7c7b\u578b\u3001\u5b57\u7b26\u548c\u6574\u578b\uff08\u8fd8\u5305\u62ec\u679a\u4e3e\u2014\u2014\u5728\u7b2c\u4e5d\u7ae0\u4f1a\u8fdb\u884c\u4ecb\u7ecd\uff09\u3002\u201c\u6574\u6570\u578b\u7684\u7c7b\u578b\u201d\u4e4b\u6240\u4ee5\u8fd9\u4e48\u547d\u540d\uff0c\u662f\u56e0\u4e3a\u8fd9\u4e9b\u7c7b\u578b\u7684\u6570\u636e\u5728\u5185\u5b58\u4e2d\u662f\u4ee5\u6574\u578b\u6570\u5b58\u653e\u7684\uff0c\u5373\u4f7f\u5b83\u4eec\u7684\u884c\u4e3a\u53ef\u80fd\u4e0d\u540c\uff08\u7a0d\u540e\u6211\u4eec\u4f1a\u5728\u8ba8\u8bba\u5b57\u7b26\u7c7b\u578b\u65f6\u4e86\u89e3\u5176\u5dee\u5f02\uff09\u3002 \u4f5c\u8005\u6ce8 \u5927\u591a\u6570\u73b0\u4ee3\u7f16\u7a0b\u8bed\u8a00\u90fd\u5305\u542b\u57fa\u672c\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\uff08\u5b57\u7b26\u4e32\u6307\u7684\u662f\u5305\u542b\u4e00\u7cfb\u5217\u5b57\u7b26\u7684\u6570\u636e\uff0c\u901a\u5e38\u7528\u6765\u8868\u793a\u6587\u672c\uff09\u3002\u5728C++\u4e2d\uff0c\u5b57\u7b26\u4e32\u5e76\u4e0d\u5c5e\u4e8e\u57fa\u672c\u7c7b\u578b\uff08\u5b83\u5c5e\u4e8e\u590d\u5408\u7c7b\u578b\uff09\u3002\u4f46\u662f\u56e0\u4e3a\u5b57\u7b26\u4e32\u7684\u4f7f\u7528\u65b9\u6cd5\u5f88\u76f4\u63a5\uff0c\u800c\u4e14\u4e5f\u975e\u5e38\u6709\u7528\uff0c\u56e0\u6b64\u5728\u672c\u7ae0\u4e2d\u6211\u4eec\u4e5f\u4f1a\u4ecb\u7ecd\u5b57\u7b26\u4e32\u7c7b\u578b\uff08 4.13 - std::string \u7b80\u4ecb \uff09\u3002 The _t suffix \u5f88\u591a\u5728\u65b0\u7248\u672c C++ \u4e2d\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982 std::nullptr_t \uff09\u90fd\u5305\u542b\u4e00\u4e2a _t \u540e\u7f00\u3002\u8fd9\u4e2a\u540e\u7f00\u8868\u793a type\uff08\u7c7b\u578b\uff09\uff0c\u8fd9\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7528\u4e8e\u73b0\u4ee3\u7c7b\u578b\u7684\u547d\u540d\u65b9\u5f0f\u3002 \u5982\u679c\u4f60\u9047\u5230\u4e86\u5e26\u6709 _t \u540e\u7f00\u7684\u6807\u8bc6\u7b26\uff0c\u5b83\u5f88\u53ef\u80fd\u662f\u4e00\u4e2a\u7c7b\u578b\uff0c\u4f46\u662f\u4e5f\u6709\u5f88\u591a\u7c7b\u578b\u662f\u4e0d\u5305\u542b\u8be5\u540e\u7f00\u7684\uff0c\u6240\u4ee5\u8be5\u547d\u540d\u89c4\u5219\u5e76\u4e0d\u662f\u88ab\u4e00\u8d2f\u9075\u5b88\u7684\u3002","title":"4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb"},{"location":"4-1-Introduction-to-fundamental-data-types/#bitsbytes","text":"\u5728 1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u4e8b\u5b9e\u4e0a\u53d8\u91cf\u53ea\u662f\u4e00\u6bb5\u53ef\u4ee5\u5b58\u653e\u4fe1\u606f\u7684\u5185\u5b58\u7684\u540d\u79f0\u3002\u7b80\u5355\u56de\u5fc6\u4e00\u4e0b\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u4f7f\u7528\u8ba1\u7b97\u673a\u63d0\u4f9b\u7684 \u968f\u673a\u8bbf\u95ee\u5185\u5b58(RAM) \u3002\u5f53\u53d8\u91cf\u88ab\u5b9a\u4e49\u7684\u65f6\u5019\uff0c\u4e00\u5757\u5185\u5b58\u88ab\u5173\u8054\u5230\u4e86\u8be5\u53d8\u91cf\u3002 \u5185\u5b58\u7684\u6700\u5c0f\u5355\u5143\u662f\u4e00\u4e2a\u4e8c\u8fdb\u5236\u4f4d\uff08\u4e5f\u79f0\u4e3a \u4f4d \uff09\uff0c\u5b83\u53ef\u4ee5\u5b58\u50a8 0 \u6216 1.\u4f60\u53ef\u4ee5\u628a\u4e00\u4e2a\u6bd4\u7279\u770b\u6210\u662f\u4e00\u4e2a\u706f\u6ce1\u7684\u5f00\u5173\u2014\u2014\u5b83\u53ea\u6709\u5f00\uff081\uff09\u548c\u5173\uff080\uff09\u4e24\u79cd\u72b6\u6001\uff0c\u6ca1\u6709\u5176\u4ed6\u4efb\u4f55\u4e2d\u95f4\u72b6\u6001\u3002\u5982\u679c\u4f60\u53bb\u89c2\u5bdf\u4e00\u6bb5\u5185\u5b58\u5185\u5bb9\u7684\u8bdd\uff0c\u4f60\u603b\u80fd\u770b\u5230 \u2026011010100101010\u2026 \u6216\u5176\u4ed6\u7c7b\u4f3c\u7ec4\u5408\u3002 \u6211\u4eec\u5c06\u5185\u5b58\u5206\u5272\u6210\u4e00\u7cfb\u5217\u8fde\u7eed\u7684\u5355\u5143\uff0c\u79f0\u4e3a\u5185\u5b58\u5730\u5740\u3002\u7c7b\u4f3c\u4e8e\u8857\u9053\u5730\u5740\u53ef\u4ee5\u7528\u6765\u67e5\u627e\u8857\u9053\u4e0a\u67d0\u5e62\u5efa\u7b51\uff0c\u901a\u8fc7\u5185\u5b58\u5730\u5740\u6211\u4eec\u4e5f\u53ef\u4ee5\u627e\u5230\u7279\u5b9a\u5730\u5740\u4e2d\u5b58\u653e\u7684\u5185\u5bb9\u3002 \u4e0d\u591f\u6709\u4ef6\u4e8b\u53ef\u80fd\u51fa\u4e4e\u4f60\u7684\u610f\u6599\uff0c\u5bf9\u4e8e\u73b0\u4ee3\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u6765\u8bf4\uff0c\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684bit\u5e76\u4e0d\u90fd\u5177\u6709\u4e00\u4e2a\u552f\u4e00\u7684\u5730\u5740\u3002\u8fd9\u662f\u56e0\u4e3a\u5185\u5b58\u5730\u5740\u7684\u6570\u91cf\u662f\u6709\u9650\u7684\uff0c\u800c\u4e14\u4e5f\u5e76\u4e0d\u9700\u8981\u4e3a\u6bcf\u4e2abit\u90fd\u6307\u5b9a\u4e00\u4e2a\u552f\u4e00\u7684\u5730\u5740\u3002\u5b9e\u9645\u4e0a\uff0c\u6bcf\u4e2a\u5185\u5b58\u5730\u5740\u8868\u793a\u4e00\u4e2a \u5b57\u8282 \u7684\u5730\u5740\u3002\u4e00\u4e2a\u5b57\u8282\u8868\u793a\u4e00\u7ec4\u88ab\u4f5c\u4e3a\u4e00\u4e2a\u5355\u5143\u64cd\u4f5c\u7684\u6bd4\u7279\u3002\u4e00\u822c\u6765\u8bb2\uff08\u73b0\u4ee3\u8ba1\u7b97\u673a\uff09\u4e00\u4e2a\u5b57\u8282\u5305\u542b8\u4e2a\u8fde\u7eed\u7684 bit\u3002 \u5173\u952e\u4fe1\u606f \u5728 C++\uff0c\u6211\u4eec\u901a\u5e38\u4ee5 byte \u4e3a\u5355\u4f4d\u64cd\u4f5c\u6570\u636e\u3002 \u4e0b\u56fe\u663e\u793a\u4e86\u4e00\u4e9b\u5185\u5b58\u5730\u5740\u53ca\u5176\u5185\u90e8\u5b58\u653e\u7684\u6570\u636e\uff08byte\u4e3a\u5355\u4f4d\uff09\uff1a \u9898\u5916\u8bdd \u5728\u4e00\u4e9b\u8001\u7684\u6216\u8005\u975e\u6807\u7684\u8ba1\u7b97\u673a\u4e0a\uff0c\u4e00\u4e2a\u5b57\u8282\u53ef\u80fd\u8868\u793a\u4e0d\u540c\u957f\u5ea6\u7684\u6bd4\u7279\uff081 \u5230 48 \u6bd4\u7279\u90fd\u6709\u53ef\u80fd\uff09\u2014\u2014 \u4e0d\u8fc7\uff0c\u4f60\u901a\u5e38\u4e0d\u9700\u8981\u62c5\u5fc3\u8fd9\u4e2a\u95ee\u9898\uff0c\u56e0\u4e3a\u73b0\u4ee3\u8ba1\u7b97\u673a\u7684\u4e8b\u5b9e\u6807\u51c6\u5c318\u6bd4\u7279\uff0c\u6211\u4eec\u53ef\u4ee5\u5047\u8bbe1\u4e2a\u5b57\u8282\u5c31\u662f8\u4e2a\u6bd4\u7279\u3002 \u6570\u636e\u7c7b\u578b \u7531\u4e8e\u8ba1\u7b97\u673a\u4e0a\u6240\u6709\u7684\u6570\u636e\u90fd\u53ea\u662f\u4e00\u4e9b\u8fde\u7eed\u7684\u6bd4\u7279\uff0c\u6211\u4eec\u5fc5\u987b\u8981\u4f7f\u7528 \u6570\u636e\u7c7b\u578b \uff08\u901a\u5e38\u7b80\u79f0\u4e3a\u7c7b\u578b\uff09\u544a\u8bc9\u8ba1\u7b97\u673a\u5982\u4f55\u5c06\u5185\u5b58\u4e2d\u7684\u6570\u636e\u89e3\u6790\u4e3a\u6709\u610f\u4e49\u7684\u503c\u3002\u6211\u4eec\u5df2\u7ecf\u5b66\u4e60\u4e86\u4e00\u79cd\u6570\u636e\u7c7b\u578b\uff1a\u6574\u578b\u3002\u5f53\u4e00\u4e2a\u6574\u578b\u53d8\u91cf\u88ab\u5b9a\u4e49\u65f6\uff0c\u6211\u4eec\u544a\u8bc9\u8ba1\u7b97\u673a\u201d\u8be5\u53d8\u91cf\u4f7f\u7528\u7684\u8fd9\u4e00\u6bb5\u5185\u5b58\u9700\u8981\u88ab\u89e3\u6790\u4e3a\u4e00\u4e2a\u6574\u578b\u503c\u201c\u3002 \u5f53\u4e3a\u4e00\u4e2a\u5bf9\u8c61\u8fdb\u884c\u8d4b\u503c\u65f6\uff0c\u7f16\u8bd1\u5668\u548cCPU\u4f1a\u5c06\u8fd9\u4e2a\u503c\u7f16\u7801\u6210\u4e00\u7cfb\u5217\u8fde\u7eed\u7684 bit\u7136\u540e\u5b58\u653e\u5230\u5185\u5b58\u4e2d\uff08\u6ce8\u610f\uff1a\u5185\u5b58\u53ea\u80fd\u5b58\u653e\u6bd4\u7279\uff09\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5c06\u4e00\u4e2a\u6574\u578b\u7684\u5bf9\u8c61\u8d4b\u503c\u4e3a65\uff0c\u5219\u8be5\u503c\u4f1a\u88ab\u8f6c\u6362\u4e3a 0100 0001 \u540e\u5b58\u653e\u5728\u5185\u5b58\u5173\u8054\u7684\u5730\u5740\u4e2d\u3002 \u53cd\u8fc7\u6765\uff0c\u5f53\u5bf9\u8c61\u88ab\u6c42\u503c\u65f6\uff0c\u8fd9\u4e9b\u8fde\u7eed\u7684 bit \u4f1a\u88ab\u91cd\u65b0\u7ec4\u7ec7\u6210\u539f\u6765\u7684\u503c\uff0c\u5373 0100 0001 \u4f1a\u88ab\u8f6c\u6362\u4e3a 65\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u7f16\u8bd1\u5668\u548cCPU\u4f1a\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u4e0a\u8ff0\u5de5\u4f5c\uff0c\u6211\u4eec\u901a\u5e38\u4e0d\u9700\u8981\u62c5\u5fc3\u8fd9\u4e9b\u503c\u662f\u5982\u4f55\u88ab\u8f6c\u6362\u7684\u3002 \u6211\u4eec\u7684\u5de5\u4f5c\u4ec5\u4ec5\u662f\u4e3a\u53d8\u91cf\u6307\u5b9a\u4e00\u4e2a\u6211\u4eec\u9700\u8981\u7684\u503c\u3002","title":"Bits\u3001bytes \u548c\u5185\u5b58\u5730\u5740"},{"location":"4-1-Introduction-to-fundamental-data-types/#_1","text":"C++ \u5177\u6709\u5f88\u591a\u5185\u7f6e\u7684\u6570\u636e\u7c7b\u578b\uff0c\u79f0\u4e3a\u57fa\u7840\u6570\u636e\u7c7b\u578b\uff0c\u4e5f\u7ecf\u5e38\u88ab\u79f0\u4e3a\u57fa\u672c\u7c7b\u578b\u3001\u539f\u59cb\u7c7b\u578b\u6216\u5185\u7f6e\u7c7b\u578b\u3002 \u4e0b\u8868\u662f\u4e00\u4e9b\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u6709\u4e9b\u53ef\u80fd\u4f60\u5df2\u7ecf\u89c1\u8fc7\u4e86\uff1a \u7c7b\u578b \u5206\u7c7b \u542b\u4e49 \u4f8b\u5b50 float Floating Point a number with a fractional part 3.14159 double Floating Point a number with a fractional part 3.14159 long double Floating Point a number with a fractional part 3.14159 bool Integral (Boolean) true or false true char Integral (Character) a single character of text \u2018c\u2019 wchar_t Integral (Character) a single character of text \u2018c\u2019 char8_t (C++20) Integral (Character) a single character of text \u2018c\u2019 char16_t (C++11) Integral (Character) a single character of text \u2018c\u2019 char32_t (C++11) Integral (Character) a single character of text \u2018c\u2019 short Integral (Integer) positive and negative whole numbers, including 0 64 int Integral (Integer) positive and negative whole numbers, including 0 64 long Integral (Integer) positive and negative whole numbers, including 0 64 long long (C++11) Integral (Integer) positive and negative whole numbers, including 0 64 std::nullptr_t (C++11) Null Pointer a null pointer nullptr void Void no type n/a \u672c\u7ae0\u5c06\u76f8\u7ee7\u4ecb\u7ecd\u8fd9\u4e9b\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff08\u9664\u4e86 std::nullptr_t \uff0c\u6211\u4eec\u4f1a\u5728\u8ba8\u8bba\u6307\u9488\u65f6\u8ba8\u8bba\u5b83\uff09\u3002C++ \u4e5f\u652f\u6301\u5f88\u591a\u590d\u6742\u7c7b\u578b\uff0c\u79f0\u4e3a \u590d\u5408\u7c7b\u578b \uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u7ae0\u8282\u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u4f5c\u8005\u6ce8 \u672f\u8bed\u6574\u578b\uff08interger\uff09\u548c\u6574\u6570\u7684\uff08integral\uff09\u7c7b\u4f3c\uff0c\u4f46\u662f\u610f\u601d\u5e76\u4e0d\u5b8c\u5168\u76f8\u540c\u3002 \u6574\u578b \u6307\u7684\u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u653e\u975e\u5c0f\u6570\u503c\u7684\u7279\u5b9a\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u6574\u6570\u30010\u6216\u8005\u8d1f\u6574\u6570\u3002\u800c \u6574\u6570\u578b\u7684 \u5219\u8868\u793a\u201c\u50cf\u6574\u578b\u201d\u3002\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6574\u578b\u7684\u4e00\u8bcd\u88ab\u7ec4\u6210\u5176\u4ed6\u672f\u8bed\uff0c\u4f8b\u5982\u201c\u6574\u6570\u578b\u7684\u7c7b\u578b\u201d\uff0c\u5305\u62ec\u5e03\u5c14\u7c7b\u578b\u3001\u5b57\u7b26\u548c\u6574\u578b\uff08\u8fd8\u5305\u62ec\u679a\u4e3e\u2014\u2014\u5728\u7b2c\u4e5d\u7ae0\u4f1a\u8fdb\u884c\u4ecb\u7ecd\uff09\u3002\u201c\u6574\u6570\u578b\u7684\u7c7b\u578b\u201d\u4e4b\u6240\u4ee5\u8fd9\u4e48\u547d\u540d\uff0c\u662f\u56e0\u4e3a\u8fd9\u4e9b\u7c7b\u578b\u7684\u6570\u636e\u5728\u5185\u5b58\u4e2d\u662f\u4ee5\u6574\u578b\u6570\u5b58\u653e\u7684\uff0c\u5373\u4f7f\u5b83\u4eec\u7684\u884c\u4e3a\u53ef\u80fd\u4e0d\u540c\uff08\u7a0d\u540e\u6211\u4eec\u4f1a\u5728\u8ba8\u8bba\u5b57\u7b26\u7c7b\u578b\u65f6\u4e86\u89e3\u5176\u5dee\u5f02\uff09\u3002 \u4f5c\u8005\u6ce8 \u5927\u591a\u6570\u73b0\u4ee3\u7f16\u7a0b\u8bed\u8a00\u90fd\u5305\u542b\u57fa\u672c\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\uff08\u5b57\u7b26\u4e32\u6307\u7684\u662f\u5305\u542b\u4e00\u7cfb\u5217\u5b57\u7b26\u7684\u6570\u636e\uff0c\u901a\u5e38\u7528\u6765\u8868\u793a\u6587\u672c\uff09\u3002\u5728C++\u4e2d\uff0c\u5b57\u7b26\u4e32\u5e76\u4e0d\u5c5e\u4e8e\u57fa\u672c\u7c7b\u578b\uff08\u5b83\u5c5e\u4e8e\u590d\u5408\u7c7b\u578b\uff09\u3002\u4f46\u662f\u56e0\u4e3a\u5b57\u7b26\u4e32\u7684\u4f7f\u7528\u65b9\u6cd5\u5f88\u76f4\u63a5\uff0c\u800c\u4e14\u4e5f\u975e\u5e38\u6709\u7528\uff0c\u56e0\u6b64\u5728\u672c\u7ae0\u4e2d\u6211\u4eec\u4e5f\u4f1a\u4ecb\u7ecd\u5b57\u7b26\u4e32\u7c7b\u578b\uff08 4.13 - std::string \u7b80\u4ecb \uff09\u3002","title":"\u57fa\u672c\u6570\u636e\u7c7b\u578b"},{"location":"4-1-Introduction-to-fundamental-data-types/#the-_t-suffix","text":"\u5f88\u591a\u5728\u65b0\u7248\u672c C++ \u4e2d\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982 std::nullptr_t \uff09\u90fd\u5305\u542b\u4e00\u4e2a _t \u540e\u7f00\u3002\u8fd9\u4e2a\u540e\u7f00\u8868\u793a type\uff08\u7c7b\u578b\uff09\uff0c\u8fd9\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7528\u4e8e\u73b0\u4ee3\u7c7b\u578b\u7684\u547d\u540d\u65b9\u5f0f\u3002 \u5982\u679c\u4f60\u9047\u5230\u4e86\u5e26\u6709 _t \u540e\u7f00\u7684\u6807\u8bc6\u7b26\uff0c\u5b83\u5f88\u53ef\u80fd\u662f\u4e00\u4e2a\u7c7b\u578b\uff0c\u4f46\u662f\u4e5f\u6709\u5f88\u591a\u7c7b\u578b\u662f\u4e0d\u5305\u542b\u8be5\u540e\u7f00\u7684\uff0c\u6240\u4ee5\u8be5\u547d\u540d\u89c4\u5219\u5e76\u4e0d\u662f\u88ab\u4e00\u8d2f\u9075\u5b88\u7684\u3002","title":"The _t suffix"},{"location":"4-10-Introduction-to-if-statements/","tags":["if","boolean"],"text":"Key Takeaway \u975e\u5e03\u5c14\u503c\u5728\u4f5c\u4e3a\u6761\u4ef6\u8868\u8fbe\u5f0f\u4f7f\u7528\u65f6\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff0c\u975e0\u503c\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c true \uff0c0\u503c\u5219\u88ab\u8f6c\u6362\u4e3a false \u60f3\u8c61\u4e00\u4e0b\uff0c\u5982\u679c\u4f60\u6b63\u8981\u53bb\u8d85\u5e02\uff0c\u7136\u540e\u4f60\u7684\u5ba4\u53cb\u5bf9\u4f60\u8bf4\uff1a\u201d\u5982\u679c\u6709\u8349\u8393\u7684\u8bdd\uff0c\u5c31\u4e70\u4e00\u70b9\u201c\u3002\u8fd9\u662f\u4e00\u4e2a\u6761\u4ef6\u8bed\u53e5\uff0c\u5c31\u662f\u8bf4\u4f60\u505a\u67d0\u4e2a\u52a8\u4f5c\uff08\"\u4e70\u4e1c\u897f\"\uff09\u7684\u6761\u4ef6\u662f\u67d0\u4e2a\u6761\u4ef6\uff08\u6709\u8349\u8393\u5356\uff09\u4e3a\u771f\u3002 \u8fd9\u79cd\u6761\u4ef6\u5728\u7f16\u7a0b\u4e2d\u975e\u5e38\u5e38\u89c1\uff0c\u6761\u4ef6\u8bed\u53e5\u4f7f\u6211\u4eec\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u67d0\u79cd\u6761\u4ef6\u884c\u4e3a\u3002C++ \u4e2d\u6700\u7b80\u5355\u7684\u6761\u4ef6\u8bed\u53e5\u662f if \u8bed\u53e5\u3002 if \u8bed\u53e5\u4f7f\u6211\u4eec\u53ef\u4ee5\u5728\u67d0\u4e2a\u6761\u4ef6\u4e3a\u771f\u65f6\u624d\u6267\u884c\u4e00\u884c\uff08\u6216\u591a\u884c\uff09\u4ee3\u7801\u3002 \u6700\u7b80\u5355\u7684 if \u8bed\u53e5\u5f62\u5f0f\u5982\u4e0b\uff1a 1 if (condition) true_statement; \u4e0b\u9762\u8fd9\u79cd\u5f62\u5f0f\u53ef\u8bfb\u6027\u66f4\u597d\uff1a 1 2 if (condition) true_statement; \u6240\u8c13\u201c\u6761\u4ef6\u201d (\u4e5f\u79f0\u4e3a\u6761\u4ef6\u8868\u8fbe\u5f0f)\u7684\u6c42\u503c\u7ed3\u679c\u662f\u4e00\u4e2a\u5e03\u5c14\u503c\u3002 \u5982\u679c if \u8bed\u53e5\u8bed\u53e5\u7684\u6c42\u503c\u7ed3\u679c\u4e3a\u5e03\u5c14\u503c true \uff0c\u5219 true_statement \u5c31\u4f1a\u6267\u884c\u3002\u53cd\u4e4b\uff0c\u5982\u56fe\u6761\u4ef6\u6c42\u503c\u7684\u7ed3\u679c\u4e3a\u5e03\u5c14\u503c false \uff0c\u5219\u8bed\u53e5 true_statement \u4f1a\u88ab\u8df3\u8fc7\u3002 \u4e00\u4e2a\u4f7f\u7528\u4e86 if \u8bed\u53e5\u7684\u4f8b\u7a0b\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; if ( x == 0 ) std :: cout << \"The value is zero \\n \" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Enter an integer: 0 The value is zero \u6211\u4eec\u4ed4\u7ec6\u5206\u6790\u4e00\u4e0b\u8fd9\u4e2a\u7a0b\u5e8f\u3002 \u9996\u5148\uff0c\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6574\u578b\u3002\u7136\u540e\u6761\u4ef6 x == 0 \u8fdb\u884c\u6c42\u503c\u3002 \u76f8\u7b49\u6bd4\u8f83\u8fd0\u7b97\u7b26 ( == ) \u88ab\u7528\u6765\u6d4b\u8bd5\u4e24\u4e2a\u503c\u662f\u5426\u76f8\u7b49\u3002\u5982\u679c\u4e24\u6570\u76f8\u7b49\uff0c\u5219\u7b49\u6bd4\u8f83\u8fd0\u7b97\u7b26 ( == ) \u8fd4\u56de true \uff0c\u5426\u5219\u8fd4\u56de false \u3002\u56e0\u4e3a x \u7684\u503c\u4e3a0\uff0c\u800c\u4e14 0 == 0 \u4e3a\u771f\uff0c\u6240\u4ee5\u8868\u8fbe\u5f0f\u6c42\u503c\u7684\u7ed3\u679c\u4e3a true \u3002 \u56e0\u4e3a\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u6c42\u503c\u7ed3\u679c\u4e3a true \uff0c\u5219\u63a5\u4e0b\u6765\u7684\u8bed\u53e5\u4f1a\u88ab\u6267\u884c\uff0c\u6253\u5370 The value is zero \u3002 \u518d\u6b21\u8fd0\u884c\u7a0b\u5e8f\uff1a 1 Enter an integer: 5 \u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c x == 0 \u6c42\u503c\u7ed3\u679c\u4e3a false \u3002\u56e0\u6b64\u63a5\u4e0b\u6765\u7684\u8bed\u53e5\u4f1a\u88ab\u8df3\u8fc7\uff0c\u7136\u540e\u7a0b\u5e8f\u5c31\u7ed3\u675f\u4e86\uff0c\u4e0d\u4f1a\u6253\u5370\u4efb\u4f55\u6587\u672c\u3002 \u6ce8\u610f If \u8bed\u53e5\u53ea\u4f1a\u6709\u6761\u4ef6\u5730\u6267\u884c\u4e00\u6761\u8bed\u53e5\u3002\u6211\u4eec\u4f1a\u5728 7.2 - if \u8bed\u53e5\u548c\u8bed\u53e5\u5757 \u4e2d\u4ecb\u7ecd\u5982\u4f55\u5728\u6761\u4ef6\u4e3a\u771f\u65f6\u6267\u884c\u591a\u6761\u8bed\u53e5\u3002 If-else \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u544a\u8bc9\u7528\u6237\uff0c\u4ed6\u4eec\u8f93\u5165\u7684\u503c\u662f\u5426\u4e3a\u975e0\u5462\uff1f \u7a0b\u5e8f\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u5199\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; if ( x == 0 ) std :: cout << \"The value is zero \\n \" ; if ( x != 0 ) std :: cout << \"The value is non-zero \\n \" ; return 0 ; } \u6216\u8005\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; bool zero { ( x == 0 ) }; if ( zero ) std :: cout << \"The value is zero \\n \" ; if ( ! zero ) std :: cout << \"The value is non-zero \\n \" ; return 0 ; } \u8fd9\u4e24\u4e2a\u7a0b\u5e8f\u5176\u5b9e\u6ca1\u5fc5\u8981\u8fd9\u4e48\u590d\u6742\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u53e6\u4e00\u79cd\u5f62\u5f0f\u7684 if \u8bed\u53e5\u2014\u2014 if-else \u8bed\u53e5\u3002 if-else \u8bed\u53e5\u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 2 3 4 if (condition) true_statement; else false_statement; \u5982\u679c condition \u6c42\u503c\u7ed3\u679c\u4e3a true \uff0c\u5219 true_statement \u4f1a\u6267\u884c\uff0c\u5426\u5219 false_statement \u5c06\u4f1a\u6267\u884c\u3002 \u8ba9\u6211\u4eec\u4f7f\u7528 if-else \u6765\u4fee\u6539\u4e00\u4e0b\u4e0a\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; if ( x == 0 ) std :: cout << \"The value is zero \\n \" ; else std :: cout << \"The value is non-zero \\n \" ; return 0 ; } \u73b0\u5728\uff0c\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Enter an integer: 0 The value is zero 1 2 Enter an integer: 5 The value is non-zero \u94fe\u5f0f if \u8bed\u53e5 \u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u8fde\u7eed\u68c0\u67e5\u4e00\u4e9b\u6761\u4ef6\u662f\u5426\u4e3a\u771f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u94fe\u5f0f\u7684 if \u8bed\u53e5\u6765\u5b8c\u6210\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; if ( x > 0 ) std :: cout << \"The value is positive \\n \" ; else if ( x < 0 ) std :: cout << \"The value is negative \\n \" ; else std :: cout << \"The value is zero \\n \" ; return 0 ; } \u8fd9\u91cc\u7684\u5c0f\u4e8e\u53f7( < ) \u88ab\u7528\u6765\u6d4b\u8bd5\u4e00\u4e2a\u503c\u662f\u5426\u5c0f\u4e8e\u53e6\u4e00\u4e2a\u503c\u3002\u7c7b\u4f3c\u5730\uff0c\u5927\u4e8e\u53f7 ( > )\u5219\u7528\u6765\u6d4b\u8bd5\u4e00\u4e2a\u503c\u662f\u5426\u5927\u4e8e\u53e6\u4e00\u4e2a\u3002\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u8fd4\u56de\u7684\u90fd\u662f\u5e03\u5c14\u503c\u3002 \u591a\u6b21\u8fd0\u884c\u7a0b\u5e8f\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Enter an integer: 4 The value is positive 1 2 Enter an integer: -3 The value is negative 1 2 Enter an integer: 0 The value is zero \u6ce8\u610f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4efb\u610f\u6b21\u94fe\u5f0f_if_ \u8bed\u53e5\u5b8c\u6210\u4f60\u6240\u9700\u8981\u8fdb\u884c\u7684\u6761\u4ef6\u5224\u65ad\u3002 \u5e03\u5c14\u7c7b\u578b\u8fd4\u56de\u503c\u548c if \u8bed\u53e5 \u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff08 4.9 - \u5e03\u5c14\u503c \uff09\uff0c\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a\u8fd4\u56de\u5e03\u5c14\u503c\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // \u5982\u679c x \u548c y \u76f8\u7b49\u5219\u8fd4\u56de true \u5426\u5219\u8fd4\u56de false bool isEqual ( int x , int y ) { return ( x == y ); // \u5982\u679c x \u548c y \u76f8\u7b49\uff0coperator== \u5219\u8fd4\u56de true \u5426\u5219\u8fd4\u56de false } int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another integer: \" ; int y {}; std :: cin >> y ; std :: cout << std :: boolalpha ; // print bools as true or false std :: cout << x << \" and \" << y << \" are equal? \" ; std :: cout << isEqual ( x , y ); // will return true or false return 0 ; } \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 if \u8bed\u53e5\u5bf9\u4e0a\u9762\u7684\u4ee3\u7801\u8fdb\u884c\u6539\u5199\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // returns true if x and y are equal, false otherwise bool isEqual ( int x , int y ) { return ( x == y ); // operator== returns true if x equals y, and false otherwise } int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another integer: \" ; int y {}; std :: cin >> y ; if ( isEqual ( x , y )) std :: cout << x << \" and \" << y << \" are equal \\n \" ; else std :: cout << x << \" and \" << y << \" are not equal \\n \" ; return 0 ; } \u4e24\u6b21\u8fd0\u884c\u4e0a\u8ff0\u7a0b\u5e8f\uff1a 1 2 3 Enter an integer: 5 Enter another integer: 5 5 and 5 are equal 1 2 3 Enter an integer: 6 Enter another integer: 4 6 and 4 are not equal \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u5c31\u662f isEqual \u51fd\u6570\uff0c\u5b83\u7684\u8fd4\u56de\u503c\u662f\u5e03\u5c14\u7c7b\u578b\u7684\u3002 \u975e\u5e03\u5c14\u7c7b\u578b\u6761\u4ef6 \u5728\u4e0a\u8ff0\u6240\u6709\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u7684\u6761\u4ef6\u8981\u4e48\u662f\u5e03\u5c14\u503c\uff08true \u6216 false\uff09\uff0c\u8981\u4e48\u662f\u8fd4\u56de\u5e03\u5c14\u503c\u7684\u51fd\u6570\u3002\u90a3\u5982\u679c\u6211\u4eec\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u6c42\u503c\u7684\u7ed3\u679c\u4e0d\u662f\u5e03\u5c14\u7c7b\u578b\u5462\uff1f \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6761\u4ef6\u8868\u8fbe\u5f0f\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff1a\u975e0\u503c\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c true \uff0c0\u503c\u5219\u88ab\u8f6c\u6362\u4e3a false \u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { if ( 4 ) // \u6ca1\u6709\u5b9e\u9645\u610f\u4e49\uff0c\u4ec5\u4f9b\u4e3e\u4f8b\u4f7f\u7528 std :: cout << \"hi\" ; else std :: cout << \"bye\" ; return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u4f1a\u6253\u5370 \u201chi\u201d\uff0c\u56e0\u4e3a4\u662f\u4e00\u4e2a\u975e0\u503c\uff0c\u6240\u4ee5\u88ab\u8f6c\u6362\u6210\u4e86\u5e03\u5c14\u503c true \uff0c\u4f7f\u5f97\u5bf9\u5e94\u7684\u8bed\u53e5\u80fd\u591f\u88ab\u6267\u884c\u3002 \u6211\u4eec\u4f1a\u5728 7.2 - if \u8bed\u53e5\u548c\u8bed\u53e5\u5757 \u4e2d\u66f4\u8be6\u7ec6\u5730\u4ecb\u7ecd if \u8bed\u53e5\u3002","title":"4.10 - if \u8bed\u53e5\u7b80\u4ecb"},{"location":"4-10-Introduction-to-if-statements/#if-else","text":"\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u544a\u8bc9\u7528\u6237\uff0c\u4ed6\u4eec\u8f93\u5165\u7684\u503c\u662f\u5426\u4e3a\u975e0\u5462\uff1f \u7a0b\u5e8f\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u5199\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; if ( x == 0 ) std :: cout << \"The value is zero \\n \" ; if ( x != 0 ) std :: cout << \"The value is non-zero \\n \" ; return 0 ; } \u6216\u8005\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; bool zero { ( x == 0 ) }; if ( zero ) std :: cout << \"The value is zero \\n \" ; if ( ! zero ) std :: cout << \"The value is non-zero \\n \" ; return 0 ; } \u8fd9\u4e24\u4e2a\u7a0b\u5e8f\u5176\u5b9e\u6ca1\u5fc5\u8981\u8fd9\u4e48\u590d\u6742\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u53e6\u4e00\u79cd\u5f62\u5f0f\u7684 if \u8bed\u53e5\u2014\u2014 if-else \u8bed\u53e5\u3002 if-else \u8bed\u53e5\u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 2 3 4 if (condition) true_statement; else false_statement; \u5982\u679c condition \u6c42\u503c\u7ed3\u679c\u4e3a true \uff0c\u5219 true_statement \u4f1a\u6267\u884c\uff0c\u5426\u5219 false_statement \u5c06\u4f1a\u6267\u884c\u3002 \u8ba9\u6211\u4eec\u4f7f\u7528 if-else \u6765\u4fee\u6539\u4e00\u4e0b\u4e0a\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; if ( x == 0 ) std :: cout << \"The value is zero \\n \" ; else std :: cout << \"The value is non-zero \\n \" ; return 0 ; } \u73b0\u5728\uff0c\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Enter an integer: 0 The value is zero 1 2 Enter an integer: 5 The value is non-zero","title":"If-else"},{"location":"4-10-Introduction-to-if-statements/#if","text":"\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u8fde\u7eed\u68c0\u67e5\u4e00\u4e9b\u6761\u4ef6\u662f\u5426\u4e3a\u771f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u94fe\u5f0f\u7684 if \u8bed\u53e5\u6765\u5b8c\u6210\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; if ( x > 0 ) std :: cout << \"The value is positive \\n \" ; else if ( x < 0 ) std :: cout << \"The value is negative \\n \" ; else std :: cout << \"The value is zero \\n \" ; return 0 ; } \u8fd9\u91cc\u7684\u5c0f\u4e8e\u53f7( < ) \u88ab\u7528\u6765\u6d4b\u8bd5\u4e00\u4e2a\u503c\u662f\u5426\u5c0f\u4e8e\u53e6\u4e00\u4e2a\u503c\u3002\u7c7b\u4f3c\u5730\uff0c\u5927\u4e8e\u53f7 ( > )\u5219\u7528\u6765\u6d4b\u8bd5\u4e00\u4e2a\u503c\u662f\u5426\u5927\u4e8e\u53e6\u4e00\u4e2a\u3002\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u8fd4\u56de\u7684\u90fd\u662f\u5e03\u5c14\u503c\u3002 \u591a\u6b21\u8fd0\u884c\u7a0b\u5e8f\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Enter an integer: 4 The value is positive 1 2 Enter an integer: -3 The value is negative 1 2 Enter an integer: 0 The value is zero \u6ce8\u610f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4efb\u610f\u6b21\u94fe\u5f0f_if_ \u8bed\u53e5\u5b8c\u6210\u4f60\u6240\u9700\u8981\u8fdb\u884c\u7684\u6761\u4ef6\u5224\u65ad\u3002","title":"\u94fe\u5f0f if \u8bed\u53e5"},{"location":"4-10-Introduction-to-if-statements/#if_1","text":"\u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff08 4.9 - \u5e03\u5c14\u503c \uff09\uff0c\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a\u8fd4\u56de\u5e03\u5c14\u503c\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // \u5982\u679c x \u548c y \u76f8\u7b49\u5219\u8fd4\u56de true \u5426\u5219\u8fd4\u56de false bool isEqual ( int x , int y ) { return ( x == y ); // \u5982\u679c x \u548c y \u76f8\u7b49\uff0coperator== \u5219\u8fd4\u56de true \u5426\u5219\u8fd4\u56de false } int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another integer: \" ; int y {}; std :: cin >> y ; std :: cout << std :: boolalpha ; // print bools as true or false std :: cout << x << \" and \" << y << \" are equal? \" ; std :: cout << isEqual ( x , y ); // will return true or false return 0 ; } \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 if \u8bed\u53e5\u5bf9\u4e0a\u9762\u7684\u4ee3\u7801\u8fdb\u884c\u6539\u5199\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // returns true if x and y are equal, false otherwise bool isEqual ( int x , int y ) { return ( x == y ); // operator== returns true if x equals y, and false otherwise } int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another integer: \" ; int y {}; std :: cin >> y ; if ( isEqual ( x , y )) std :: cout << x << \" and \" << y << \" are equal \\n \" ; else std :: cout << x << \" and \" << y << \" are not equal \\n \" ; return 0 ; } \u4e24\u6b21\u8fd0\u884c\u4e0a\u8ff0\u7a0b\u5e8f\uff1a 1 2 3 Enter an integer: 5 Enter another integer: 5 5 and 5 are equal 1 2 3 Enter an integer: 6 Enter another integer: 4 6 and 4 are not equal \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u5c31\u662f isEqual \u51fd\u6570\uff0c\u5b83\u7684\u8fd4\u56de\u503c\u662f\u5e03\u5c14\u7c7b\u578b\u7684\u3002","title":"\u5e03\u5c14\u7c7b\u578b\u8fd4\u56de\u503c\u548c if \u8bed\u53e5"},{"location":"4-10-Introduction-to-if-statements/#_1","text":"\u5728\u4e0a\u8ff0\u6240\u6709\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u7684\u6761\u4ef6\u8981\u4e48\u662f\u5e03\u5c14\u503c\uff08true \u6216 false\uff09\uff0c\u8981\u4e48\u662f\u8fd4\u56de\u5e03\u5c14\u503c\u7684\u51fd\u6570\u3002\u90a3\u5982\u679c\u6211\u4eec\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u6c42\u503c\u7684\u7ed3\u679c\u4e0d\u662f\u5e03\u5c14\u7c7b\u578b\u5462\uff1f \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6761\u4ef6\u8868\u8fbe\u5f0f\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff1a\u975e0\u503c\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c true \uff0c0\u503c\u5219\u88ab\u8f6c\u6362\u4e3a false \u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { if ( 4 ) // \u6ca1\u6709\u5b9e\u9645\u610f\u4e49\uff0c\u4ec5\u4f9b\u4e3e\u4f8b\u4f7f\u7528 std :: cout << \"hi\" ; else std :: cout << \"bye\" ; return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u4f1a\u6253\u5370 \u201chi\u201d\uff0c\u56e0\u4e3a4\u662f\u4e00\u4e2a\u975e0\u503c\uff0c\u6240\u4ee5\u88ab\u8f6c\u6362\u6210\u4e86\u5e03\u5c14\u503c true \uff0c\u4f7f\u5f97\u5bf9\u5e94\u7684\u8bed\u53e5\u80fd\u591f\u88ab\u6267\u884c\u3002 \u6211\u4eec\u4f1a\u5728 7.2 - if \u8bed\u53e5\u548c\u8bed\u53e5\u5757 \u4e2d\u66f4\u8be6\u7ec6\u5730\u4ecb\u7ecd if \u8bed\u53e5\u3002","title":"\u975e\u5e03\u5c14\u7c7b\u578b\u6761\u4ef6"},{"location":"4-11-Chars/","tags":["char","C++11"],"text":"Key Takeaway \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u4ecb\u7ecd\u7684\u57fa\u7840\u6570\u636e\u7c7b\u578b\u90fd\u662f\u7528\u6765\u4fdd\u5b58\u6570\u5b57\u7684\uff08\u6574\u578b\u6216\u8005\u6d6e\u70b9\u6570\uff09\u6216\u8005\u5e03\u5c14\u503c\u7684\u3002\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u60f3\u5b58\u653e\u5b57\u6bcd\u548c\u6807\u70b9\u600e\u4e48\u529e\uff1f 1 2 3 4 5 6 7 8 9 10 11 #include int main () { std :: cout << \"Would you like a burrito? (y/n)\" ; // We want the user to enter a 'y' or 'n' character // How do we do this? return 0 ; } char \u7c7b\u578b\u662f\u7528\u6765\u4fdd\u5b58 \u5b57\u7b26(character) \u3002\u5b57\u7b26\u53ef\u4ee5\u662f\u4e00\u4e2a\u5b57\u6bcd\u3001\u6570\u5b57\u3001\u7b26\u53f7\u6216\u7a7a\u767d\u3002 char\u7c7b\u578b\u4e5f\u5c5e\u4e8e\u6574\u578b\u7c7b\u578b\uff0c\u4e5f\u5c31\u662f\u8bf4\u5b83\u5e95\u5c42\u5b58\u50a8\u7684\u8fd8\u662f\u6574\u578b\u6570\u3002\u548c\u5e03\u5c14\u503c\u5c060\u89e3\u6790\u4e3afalse\u7c7b\u4f3c\uff0c\u5c06\u975e\u96f6\u503c\u89e3\u6790\u4e3atrue\u7c7b\u4f3c\uff0cchar \u7c7b\u578b\u5c06\u5b58\u50a8\u7684\u6574\u578b\u89e3\u6790\u4e3a ASCII \u5b57\u7b26\u3002 ASCII \u5373\u7f8e\u56fd\u4fe1\u606f\u4ea4\u6362\u6807\u51c6\u4ee3\u7801\u662f\uff0c\u5b83\u662f\u4e00\u79cd\u5c06\u82f1\u6587\u5b57\u6bcd\uff08\u4ee5\u53ca\u5c11\u6570\u5176\u4ed6\u7b26\u53f7\uff09\u8868\u793a\u4e3a0\u5230127\u6574\u578b\u6570\u5b57\u7684\u65b9\u6cd5\u3002\u4f8b\u5982ASCII\u4e2d97\u8868\u793a\u5b57\u6bcd a \u3002 \u5b57\u7b26\u5e38\u91cf\u901a\u5e38\u653e\u7f6e\u5728\u5355\u5f15\u53f7\u4e2d\uff08\u4f8b\u5982, 'g' , '1' , ' ' \uff09\u3002 \u5b8c\u6574\u7684 ASCII \u5b57\u6bcd\u8868\u5982\u4e0b\uff1a ASCII \u7801 \u7b26\u53f7 ASCII \u7801 \u7b26\u53f7 ASCII \u7801 \u7b26\u53f7 ASCII \u7801 \u7b26\u53f7 0 NUL (null) 32 (space) 64 @ 96 ` 1 SOH (start of header) 33 ! 65 A 97 a 2 STX (start of text) 34 \u201d 66 B 98 b 3 ETX (end of text) 35 # 67 C 99 c 4 EOT (end of transmission) 36 $ 68 D 100 d 5 ENQ (enquiry) 37 % 69 E 101 e 6 ACK (acknowledge) 38 & 70 F 102 f 7 BEL (bell) 39 \u2019 71 G 103 g 8 BS (backspace) 40 ( 72 H 104 h 9 HT (horizontal tab) 41 ) 73 I 105 i 10 LF (line feed/new line) 42 * 74 J 106 j 11 VT (vertical tab) 43 + 75 K 107 k 12 FF (form feed / new page) 44 , 76 L 108 l 13 CR (carriage return) 45 - 77 M 109 m 14 SO (shift out) 46 . 78 N 110 n 15 SI (shift in) 47 / 79 O 111 o 16 DLE (data link escape) 48 0 80 P 112 p 17 DC1 (data control 1) 49 1 81 Q 113 q 18 DC2 (data control 2) 50 2 82 R 114 r 19 DC3 (data control 3) 51 3 83 S 115 s 20 DC4 (data control 4) 52 4 84 T 116 t 21 NAK (negative acknowledge) 53 5 85 U 117 u 22 SYN (synchronous idle) 54 6 86 V 118 v 23 ETB (end of transmission block) 55 7 87 W 119 w 24 CAN (cancel) 56 8 88 X 120 x 25 EM (end of medium) 57 9 89 Y 121 y 26 SUB (substitute) 58 : 90 Z 122 z 27 ESC (escape) 59 ; 91 [ 123 { 28 FS (file separator) 60 < 92 \\ 124 29 GS (group separator) 61 = 93 ] 125 } 30 RS (record separator) 62 > 94 ^ 126 ~ 31 US (unit separator) 63 ? 95 _ 127 DEL (\u5220\u9664) ASCII \u7801 0-31 \u4e3a\u4e0d\u53ef\u6253\u5370\u5b57\u7b26\uff0c\u5b83\u4eec\u5927\u591a\u88ab\u7528\u6765\u6392\u7248\u6216\u63a7\u5236\u6253\u5370\u673a\uff0c\u5176\u4e2d\u5f88\u591a\u73b0\u5728\u5df2\u7ecf\u4e0d\u518d\u4f7f\u7528\u4e86\u3002\u5982\u679c\u4f60\u5c1d\u8bd5\u5c06\u5b83\u4eec\u6253\u5370\u51fa\u6765\u7684\u8bdd\uff0c\u6253\u5370\u7684\u7ed3\u679c\u53ef\u80fd\u548c\u4f60\u4f7f\u7528\u7684\u7cfb\u7edf\u76f8\u540c\uff08\u4f60\u53ef\u80fd\u4f1a\u5f97\u5230\u4e00\u4e9b\u50cfemoji\u7684\u5b57\u7b26\uff09\u3002 ASCII \u7801 32-127 \u4e3a\u53ef\u6253\u5370\u5b57\u7b26\uff0c\u5b83\u4eec\u901a\u5e38\u8868\u793a\u5b57\u6bcd\u3001\u6570\u5b57\u5b57\u7b26\u548c\u6807\u70b9\uff0c\u591a\u88ab\u7528\u6765\u8868\u793a\u57fa\u672c\u7684\u82f1\u6587\u6587\u672c\u3002 \u521d\u59cb\u5316\u5b57\u7b26 \u4f7f\u7528\u5b57\u7b26\u5b57\u9762\u91cf\u53ef\u4ee5\u521d\u59cb\u5316\u5b57\u7b26\u53d8\u91cf\uff1a 1 char ch2 { 'a' }; // \u4f7f\u7528 'a' \u6765\u521d\u59cb\u5316(\u88ab\u5b58\u50a8\u4e3a 97) (\u63a8\u8350) \u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6574\u578b\u6570\u6765\u521d\u59cb\u5316\u5b57\u7b26\u53d8\u91cf\uff0c\u4f46\u662f\u5e94\u8be5\u5c3d\u91cf\u907f\u514d\u8fd9\u4e48\u505a\u3002 1 char ch1 { 97 }; // \u4f7f\u7528 97 \u6765\u521d\u59cb\u5316('a') (\u4e0d\u63a8\u8350) \u6ce8\u610f \u6ce8\u610f\u4e0d\u8981\u5c06\u5b57\u7b26\u6570\u5b57\u548c\u6574\u578b\u6570\u5b57\u641e\u6df7\uff0c\u4e0b\u9762\u4e24\u79cd\u521d\u59cb\u5316\u5e76\u4e0d\u76f8\u540c\uff1a 1 2 char ch { 5 }; // \u4f7f\u7528 5 \u521d\u59cb\u5316(\u4fdd\u5b58\u4e3a\u6574\u578b 5) char ch { '5' }; // \u4f7f\u7528 '5' \u521d\u59cb\u5316 (\u4fdd\u5b58\u4e3a\u6574\u578b 53) \u53ea\u6709\u5f53\u9700\u8981\u5c06\u6570\u5b57\u663e\u793a\u6210\u6587\u672c\u7684\u65f6\u5019\uff0c\u624d\u4f7f\u7528\u5b57\u7b26\u6570\u5b57\u800c\u4e0d\u662f\u6574\u578b\u6570\u5b57\u3002\u6574\u578b\u6570\u5b57\u7528\u4e8e\u6570\u5b66\u8fd0\u7b97\u3002 \u6253\u5370\u5b57\u7b26 \u5f53\u4f7f\u7528 std::cout \u6253\u5370\u5b57\u7b26\u7684\u65f6\u5019 std::cout \u4f1a\u628a\u5b57\u7b26\u53d8\u91cf\u6253\u5370\u6210ASCII\u5b57\u7b26\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { char ch1 { 'a' }; // (preferred) std :: cout << ch1 ; // cout prints character 'a' char ch2 { 98 }; // code point for 'b' (not preferred) std :: cout << ch2 ; // cout prints a character ('b') return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1aab \u5b57\u7b26\u5b57\u9762\u91cf\u4e5f\u53ef\u4ee5\u76f4\u63a5\u6253\u5370\uff1a 1 cout << 'c' ; \u8f93\u51fa\u7ed3\u679c\uff1ac \u8f93\u5165\u5b57\u7b26 \u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\u4f1a\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\uff0c\u7136\u540e\u5c06\u5b57\u7b26\u6253\u5370\u51fa\u6765\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { std :: cout << \"Input a keyboard character: \" ; char ch {}; std :: cin >> ch ; std :: cout << \"You entered: \" << ch << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 2 Input a keyboard character: q You entered q \u6ce8\u610f\uff0c std::cin \u5141\u8bb8\u4f60\u8f93\u5165\u591a\u4e2a\u5b57\u7b26\u3002\u4e0d\u8fc7\u53d8\u91cf ch \u53ea\u80fd\u63a5\u6536\u4e00\u4e2a\u5b57\u7b26\uff0c\u56e0\u6b64\u53ea\u6709\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4f1a\u88ab\u5b58\u653e\u5230\u53d8\u91cf\u4e2d\uff0c\u5269\u4e0b\u5b57\u7b26\u4f1a\u88ab\u7559\u5728 std::cin \u7684\u7f13\u51b2\u4e2d\uff0c\u53ef\u4ee5\u88ab\u63a5\u4e0b\u6765\u518d\u6b21\u8c03\u7528 std::cin \u65f6\u4f7f\u7528\u3002 \u4ee5\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e3a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { std :: cout << \"Input a keyboard character: \" ; // \u5047\u8bbe\u7528\u6237\u8f93\u5165\u4e86 \"abcd\" (\u5e26\u53cc\u5f15\u53f7) char ch {}; std :: cin >> ch ; // ch = 'a', \"bcd\" \u88ab\u7559\u5728\u4e86\u7f13\u51b2\u4e2d std :: cout << \"You entered: \" << ch << '\\n' ; // \u6ce8\u610f\uff0c\u4e0b\u9762\u7684ch\u6ca1\u6709\u8981\u6c42\u7528\u6237\u518d\u6b21\u8f93\u5165\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u4ece\u7f13\u51b2\u4e2d\u83b7\u53d6\u8f93\u5165 std :: cin >> ch ; // ch = 'b', \"cd\" \u88ab\u6d41\u5728\u7f13\u51b2\u4e2d std :: cout << \"You entered: \" << ch << '\\n' ; return 0 ; } 1 2 3 Input a keyboard character: abcd You entered: a You entered: b \u5982\u679c\u4f60\u60f3\u4e00\u6b21\u6027\u8bfb\u5165\u591a\u4e2a\u5b57\u7b26\uff08\u4f8b\u5982\uff0c\u540d\u5b57\u3001\u5355\u8bcd\u6216\u8005\u53e5\u5b50\uff09\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528\u5b57\u7b26\u4e32\u800c\u4e0d\u662f\u5b57\u7b26\u3002\u5b57\u7b26\u4e32\u662f\u4e00\u4e2a\u5b57\u7b26\u5e8f\u5217\uff08\u56e0\u6b64\u5b57\u7b26\u4e32\u53ef\u4ee5\u4fdd\u5b58\u591a\u4e2a\u7b26\u53f7\uff09\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 4.17 - std::string \u7b80\u4ecb \u4e2d\u8ba8\u8bba\u5173\u4e8e\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u3002 \u5b57\u7b26\u7c7b\u578b\u7684\u5927\u5c0f\u3001\u8303\u56f4\u548c\u9ed8\u8ba4\u7b26\u53f7 C++ \u5c06\u5b57\u7b26\u7c7b\u578b\u7684\u5927\u5c0f\u5b9a\u4e49\u4e3a\u56fa\u5b9a\u76841\u5b57\u8282\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5b57\u7b26\u53ef\u4ee5\u662f\u6709\u7b26\u53f7\u6216\u8005\u65e0\u7b26\u53f7\u7684\uff08\u591a\u6570\u60c5\u51b5\u4e3a\u6709\u7b26\u53f7\uff09\u3002\u5982\u679c\u4f60\u4f7f\u7528\u5b57\u7b26\u53d8\u91cf\u6765\u4fdd\u5b58ASCII\u5b57\u7b26\u7684\u8bdd\uff0c\u4f60\u4e0d\u9700\u8981\u6307\u5b9a\u7b26\u53f7\uff0c\u56e0\u4e3a\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u7684\u5b57\u7b26\u53d8\u91cf\u90fd\u53ef\u4ee5\u4fdd\u5b580-127\u5185\u7684\u503c\u3002 \u5982\u679c\u4f60\u4f7f\u7528\u5b57\u7b26\u53d8\u91cf\u6765\u4fdd\u5b58\u4e00\u4e9b\u6bd4\u8f83\u5c0f\u7684\u6574\u578b\u503c\uff08\u5982\u975e\u4f18\u5316\u7a7a\u95f4\u8fd9\u6837\u7684\u7279\u522b\u9700\u8981\uff0c\u8bf7\u4e0d\u8981\u8fd9\u4e48\u505a\uff09\uff0c\u4f60\u5e94\u8be5\u6307\u5b9a\u5b83\u662f\u6709\u7b26\u53f7\u8fd8\u662f\u65e0\u7b26\u53f7\u7684\u3002\u6709\u7b26\u53f7\u7684\u5b57\u7b26\u53ef\u4ee5\u4fdd\u5b58\u7684\u6574\u6570\u8303\u56f4\u4e3a-128 \u5230 127\u3002\u65e0\u7b26\u53f7\u5b57\u7b26\u5219\u53ef\u4ee5\u4fdd\u5b58 0 \u5230 255 \u8303\u56f4\u5185\u7684\u6574\u6570\u3002 \u8f6c\u8bd1\u5e8f\u5217 C++ \u4e2d\u7684\u67d0\u4e9b\u5b57\u7b26\u662f\u5177\u6709\u7279\u6b8a\u542b\u4e49\u7684\uff0c\u8fd9\u4e9b\u5b57\u7b26\u79f0\u4e3a\u8f6c\u8bd1\u5e8f\u5217\uff08escape sequences\uff09\u3002\u8f6c\u8bd1\u5e8f\u5217\u4ee5 \\ \uff08\u53cd\u659c\u7ebf\uff09\u5f00\u5934\uff0c\u7d27\u63a5\u7740\u662f\u4e00\u4e2a\u5b57\u6bcd\u6216\u6570\u5b57\u3002 \u4f60\u53ef\u80fd\u5df2\u7ecf\u89c1\u8fc7\u5728\u5e38\u9047\u5230\u7684\u8f6c\u4e49\u5e8f\u5217 '\\n' \uff0c\u5b83\u4f1a\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u4e00\u4e2a\u6362\u884c\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << \"First line \\n Second line \\n \" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 First line Second line \u53e6\u5916\u4e00\u4e2a\u5e38\u7528\u7684\u8f6c\u8bd1\u5e8f\u5217\u662f '\\t' \uff0c\u8868\u793a\u4e00\u4e2a\u6c34\u5e73\u5236\u8868\u7b26\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << \"First part \\t Second part\" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u4e3a 1 First part Second part \u5176\u4ed6\u4e00\u4e9b\u503c\u5f97\u6ce8\u610f\u7684\u8f6c\u8bd1\u5e8f\u5217\u6709\uff1a \\' \uff1a\u6253\u5370\u5355\u5f15\u53f7 \\\" \uff1a\u6253\u5370\u53cc\u5f15\u53f7 \\\\ \u6253\u5370\u53cd\u659c\u7ebf \u4e0b\u8868\u5c55\u793a\u4e86\u5168\u90e8\u7684\u8f6c\u4e49\u5e8f\u5217 \u540d\u79f0 \u7b26\u53f7 \u542b\u4e49 Alert \\a Makes an alert, such as a beep Backspace \\b Moves the cursor back one space Formfeed \\f Moves the cursor to next logical page Newline \\n Moves cursor to next line Carriage return \\r Moves cursor to beginning of line Horizontal tab \\t Prints a horizontal tab Vertical tab \\v Prints a vertical tab Single quote \\\u2019 Prints a single quote Double quote \\\u201d Prints a double quote Backslash \\ Prints a backslash. Question mark \\? Prints a question mark. No longer relevant. You can use question marks unescaped. Octal number (number) Translates into char represented by octal Hex number \\x(number) Translates into char represented by hex number \u4e3e\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { std :: cout << \" \\\" This is quoted text \\\"\\n \" ; std :: cout << \"This string contains a single backslash \\\\\\n \" ; std :: cout << \"6F in hex is char ' \\x6F ' \\n \" ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 \"This is quoted text\" This string contains a single backslash \\ 6F in hex is char 'o' Newline ( \\n ) vs. std::endl \u5728 1.5 - iostream \u7b80\u4ecb\uff1a cout\uff0ccin \u548c endl \u4e2d\u5df2\u7ecf\u4ecb\u7ecd\u4e86\u3002 \u5b57\u7b26\u653e\u5728\u5355\u5f15\u53f7\u548c\u53cc\u5f15\u53f7\u4e2d\u6709\u4ec0\u4e48\u533a\u522b \u5355\u5b57\u7b26\u603b\u662f\u653e\u5728\u5355\u5f15\u53f7\u4e2d (\u4f8b\u5982 \u2018a\u2019, \u2018+\u2019, \u20185\u2019)\u3002\u5b57\u7b26\u53ea\u80fd\u8868\u793a\u4e00\u4e2a\u7b26\u53f7\uff08\u4f8b\u5982\uff0c\u5b57\u6bcd\u3001\u52a0\u53f7\u548c\u6570\u5b575\uff09\uff0c\u4e0b\u9762\u8fd9\u6837\u7684\u5199\u6cd5\u662f\u4e0d\u5bf9\u7684\uff1a 1 char ch { ' 56 ' }; // char \u53ea\u80fd\u4fdd\u6301\u4e00\u4e2a\u7b26\u53f7 \u53cc\u5f15\u53f7\u4e2d\u7684\u6587\u672c\u4f1a\u88ab\u770b\u505a\u662f\u5b57\u7b26\u4e32\uff08\u4f8b\u5982 \u201cHello, world!\u201d\uff09\u3002 \u4f60\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff1a 1 std :: cout << \"Hello, world!\" ; // \"Hello, world!\" is a string literal \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u53ea\u6709\u4e00\u4e2a\u5b57\u7b26\uff0c\u8bf7\u653e\u5728\u5355\u5f15\u53f7\u4e2d (\u4f8b\u5982 \u2018t\u2019 \u6216 \u2018\\n\u2019, \u800c\u4e0d\u662f \u201ct\u201d \u6216 \u201c\\n\u201d)\u3002\u4ee5\u4fbf\u7f16\u8bd1\u5668\u8fdb\u884c\u4f18\u5316\u3002 \u8fd8\u6709\u5176\u4ed6\u5b57\u7b26\u7c7b\u578b\u5417\uff1f wchar_t, char16_t \u548c char32_t? wchar_t \u5728\u51e0\u4e4e\u4efb\u4f55\u573a\u5408\u4e0b\u90fd\u5e94\u8be5\u907f\u514d\u4f7f\u7528 (\u5904\u7406\u5728\u4f7f\u7528 Windows API \u65f6)\u3002\u5b83\u7684\u5927\u5c0f\u662f\u5b9e\u73b0\u65f6\u5b9a\u4e49\u7684\uff0c\u975e\u5e38\u4e0d\u53ef\u9760\u800c\u4e14\u51e0\u4e4e\u5df2\u7ecf\u5e9f\u5f03\u4e86\u3002 \u9898\u5916\u8bdd \u672f\u8bed\u201c\u5e9f\u5f03\u201d\u6307\u7684\u662f\uff0c\u4ecd\u7136\u652f\u6301\uff0c\u4f46\u662f\u4e0d\u518d\u63a8\u8350\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u5df2\u7ecf\u88ab\u5176\u4ed6\u66f4\u597d\u7684\u540c\u7c7b\u66ff\u6362\u4e86\uff0c\u540c\u65f6\u5b83\u4e5f\u662f\u4e0d\u5b89\u5168\u7684\u3002 \u548c ASCII \u5c06\u6574\u578b 0-127 \u6620\u5c04\u5230\u82f1\u6587\u5b57\u6bcd\u4e00\u6837\uff0c\u4e5f\u6709\u5176\u4ed6\u5b57\u7b26\u7f16\u7801\u6807\u51c6\u53ef\u4ee5\u5c06\u6574\u578b\u6620\u5c04\u5230\u5176\u4ed6\u8bed\u8a00\u7684\u5b57\u7b26\u3002\u9664\u4e86 ASCII \u4ee5\u5916\u6700\u8457\u540d\u7684\u5b57\u6bcd\u7f16\u7801\u662f Unicode\uff0c\u5b83\u5c06 144,000 \u4e2a\u6574\u578b\u6570\u6620\u5c04\u5230\u4e0d\u540c\u8bed\u8a00\u7684\u5b57\u7b26\u3002\u56e0\u4e3a Unicode \u7684\u7f16\u7801\u70b9\u975e\u5e38\u591a\uff0c\u6240\u4ee5\u4e00\u4e2a Unicode \u7f16\u7801\u70b9\u9700\u898132\u4f4d\u624d\u80fd\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff08\u79f0\u4e3a UTF-32\uff09\u3002\u4e0d\u8fc7\uff0cUnicode \u5b57\u7b26\u4e5f\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a 16 \u4f4d\u6216 8\u4f4d\u5b57\u7b26 (\u5206\u522b\u79f0\u4e3a UTF-16 \u548c UTF-8)\u3002 char16_t \u548c char32_t \u5728 C++11 \u4e2d\u88ab\u5f15\u5165\uff0c\u7528\u4e8e\u652f\u6301 16 \u4f4d\u548c 32 \u4f4d Unicode \u5b57\u7b26\u3002 char8_t \u5728 C++20 \u4e2d\u88ab\u5f15\u5165\u3002 \u4f60\u4e0d\u9700\u8981\u4f7f\u7528 char8_t \u3001 char16_t \u6216 char32_t \uff0c\u89e6\u53d1\u4f60\u5e0c\u671b\u7a0b\u5e8f\u517c\u5bb9 Unicode\u3002Unicode \u548c\u672c\u5730\u5316\u4e00\u822c\u6765\u8bf4\u5df2\u7ecf\u8d85\u51fa\u4e86\u672c\u6559\u7a0b\u7684\u8303\u56f4\uff0c\u6240\u4ee5\u6211\u4eec\u5e76\u4e0d\u4f1a\u6d89\u53ca\u76f8\u5173\u5185\u5bb9\u3002 \u540c\u65f6\uff0c\u5f53\u4f60\u4f7f\u7528\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u7684\u65f6\u5019\uff0c\u5e94\u8be5\u53ea\u4f7f\u7528ASCII\u5b57\u7b26\u3002\u4f7f\u7528\u5176\u4ed6\u5b57\u7b26\u96c6\u7684\u5b57\u7b26\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5b57\u7b26\u663e\u793a\u9519\u8bef\u3002","title":"4.11 - \u5b57\u7b26"},{"location":"4-11-Chars/#_1","text":"\u4f7f\u7528\u5b57\u7b26\u5b57\u9762\u91cf\u53ef\u4ee5\u521d\u59cb\u5316\u5b57\u7b26\u53d8\u91cf\uff1a 1 char ch2 { 'a' }; // \u4f7f\u7528 'a' \u6765\u521d\u59cb\u5316(\u88ab\u5b58\u50a8\u4e3a 97) (\u63a8\u8350) \u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6574\u578b\u6570\u6765\u521d\u59cb\u5316\u5b57\u7b26\u53d8\u91cf\uff0c\u4f46\u662f\u5e94\u8be5\u5c3d\u91cf\u907f\u514d\u8fd9\u4e48\u505a\u3002 1 char ch1 { 97 }; // \u4f7f\u7528 97 \u6765\u521d\u59cb\u5316('a') (\u4e0d\u63a8\u8350) \u6ce8\u610f \u6ce8\u610f\u4e0d\u8981\u5c06\u5b57\u7b26\u6570\u5b57\u548c\u6574\u578b\u6570\u5b57\u641e\u6df7\uff0c\u4e0b\u9762\u4e24\u79cd\u521d\u59cb\u5316\u5e76\u4e0d\u76f8\u540c\uff1a 1 2 char ch { 5 }; // \u4f7f\u7528 5 \u521d\u59cb\u5316(\u4fdd\u5b58\u4e3a\u6574\u578b 5) char ch { '5' }; // \u4f7f\u7528 '5' \u521d\u59cb\u5316 (\u4fdd\u5b58\u4e3a\u6574\u578b 53) \u53ea\u6709\u5f53\u9700\u8981\u5c06\u6570\u5b57\u663e\u793a\u6210\u6587\u672c\u7684\u65f6\u5019\uff0c\u624d\u4f7f\u7528\u5b57\u7b26\u6570\u5b57\u800c\u4e0d\u662f\u6574\u578b\u6570\u5b57\u3002\u6574\u578b\u6570\u5b57\u7528\u4e8e\u6570\u5b66\u8fd0\u7b97\u3002","title":"\u521d\u59cb\u5316\u5b57\u7b26"},{"location":"4-11-Chars/#_2","text":"\u5f53\u4f7f\u7528 std::cout \u6253\u5370\u5b57\u7b26\u7684\u65f6\u5019 std::cout \u4f1a\u628a\u5b57\u7b26\u53d8\u91cf\u6253\u5370\u6210ASCII\u5b57\u7b26\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { char ch1 { 'a' }; // (preferred) std :: cout << ch1 ; // cout prints character 'a' char ch2 { 98 }; // code point for 'b' (not preferred) std :: cout << ch2 ; // cout prints a character ('b') return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1aab \u5b57\u7b26\u5b57\u9762\u91cf\u4e5f\u53ef\u4ee5\u76f4\u63a5\u6253\u5370\uff1a 1 cout << 'c' ; \u8f93\u51fa\u7ed3\u679c\uff1ac","title":"\u6253\u5370\u5b57\u7b26"},{"location":"4-11-Chars/#_3","text":"\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\u4f1a\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\uff0c\u7136\u540e\u5c06\u5b57\u7b26\u6253\u5370\u51fa\u6765\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { std :: cout << \"Input a keyboard character: \" ; char ch {}; std :: cin >> ch ; std :: cout << \"You entered: \" << ch << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 2 Input a keyboard character: q You entered q \u6ce8\u610f\uff0c std::cin \u5141\u8bb8\u4f60\u8f93\u5165\u591a\u4e2a\u5b57\u7b26\u3002\u4e0d\u8fc7\u53d8\u91cf ch \u53ea\u80fd\u63a5\u6536\u4e00\u4e2a\u5b57\u7b26\uff0c\u56e0\u6b64\u53ea\u6709\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4f1a\u88ab\u5b58\u653e\u5230\u53d8\u91cf\u4e2d\uff0c\u5269\u4e0b\u5b57\u7b26\u4f1a\u88ab\u7559\u5728 std::cin \u7684\u7f13\u51b2\u4e2d\uff0c\u53ef\u4ee5\u88ab\u63a5\u4e0b\u6765\u518d\u6b21\u8c03\u7528 std::cin \u65f6\u4f7f\u7528\u3002 \u4ee5\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e3a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { std :: cout << \"Input a keyboard character: \" ; // \u5047\u8bbe\u7528\u6237\u8f93\u5165\u4e86 \"abcd\" (\u5e26\u53cc\u5f15\u53f7) char ch {}; std :: cin >> ch ; // ch = 'a', \"bcd\" \u88ab\u7559\u5728\u4e86\u7f13\u51b2\u4e2d std :: cout << \"You entered: \" << ch << '\\n' ; // \u6ce8\u610f\uff0c\u4e0b\u9762\u7684ch\u6ca1\u6709\u8981\u6c42\u7528\u6237\u518d\u6b21\u8f93\u5165\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u4ece\u7f13\u51b2\u4e2d\u83b7\u53d6\u8f93\u5165 std :: cin >> ch ; // ch = 'b', \"cd\" \u88ab\u6d41\u5728\u7f13\u51b2\u4e2d std :: cout << \"You entered: \" << ch << '\\n' ; return 0 ; } 1 2 3 Input a keyboard character: abcd You entered: a You entered: b \u5982\u679c\u4f60\u60f3\u4e00\u6b21\u6027\u8bfb\u5165\u591a\u4e2a\u5b57\u7b26\uff08\u4f8b\u5982\uff0c\u540d\u5b57\u3001\u5355\u8bcd\u6216\u8005\u53e5\u5b50\uff09\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528\u5b57\u7b26\u4e32\u800c\u4e0d\u662f\u5b57\u7b26\u3002\u5b57\u7b26\u4e32\u662f\u4e00\u4e2a\u5b57\u7b26\u5e8f\u5217\uff08\u56e0\u6b64\u5b57\u7b26\u4e32\u53ef\u4ee5\u4fdd\u5b58\u591a\u4e2a\u7b26\u53f7\uff09\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 4.17 - std::string \u7b80\u4ecb \u4e2d\u8ba8\u8bba\u5173\u4e8e\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u3002","title":"\u8f93\u5165\u5b57\u7b26"},{"location":"4-11-Chars/#_4","text":"C++ \u5c06\u5b57\u7b26\u7c7b\u578b\u7684\u5927\u5c0f\u5b9a\u4e49\u4e3a\u56fa\u5b9a\u76841\u5b57\u8282\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5b57\u7b26\u53ef\u4ee5\u662f\u6709\u7b26\u53f7\u6216\u8005\u65e0\u7b26\u53f7\u7684\uff08\u591a\u6570\u60c5\u51b5\u4e3a\u6709\u7b26\u53f7\uff09\u3002\u5982\u679c\u4f60\u4f7f\u7528\u5b57\u7b26\u53d8\u91cf\u6765\u4fdd\u5b58ASCII\u5b57\u7b26\u7684\u8bdd\uff0c\u4f60\u4e0d\u9700\u8981\u6307\u5b9a\u7b26\u53f7\uff0c\u56e0\u4e3a\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u7684\u5b57\u7b26\u53d8\u91cf\u90fd\u53ef\u4ee5\u4fdd\u5b580-127\u5185\u7684\u503c\u3002 \u5982\u679c\u4f60\u4f7f\u7528\u5b57\u7b26\u53d8\u91cf\u6765\u4fdd\u5b58\u4e00\u4e9b\u6bd4\u8f83\u5c0f\u7684\u6574\u578b\u503c\uff08\u5982\u975e\u4f18\u5316\u7a7a\u95f4\u8fd9\u6837\u7684\u7279\u522b\u9700\u8981\uff0c\u8bf7\u4e0d\u8981\u8fd9\u4e48\u505a\uff09\uff0c\u4f60\u5e94\u8be5\u6307\u5b9a\u5b83\u662f\u6709\u7b26\u53f7\u8fd8\u662f\u65e0\u7b26\u53f7\u7684\u3002\u6709\u7b26\u53f7\u7684\u5b57\u7b26\u53ef\u4ee5\u4fdd\u5b58\u7684\u6574\u6570\u8303\u56f4\u4e3a-128 \u5230 127\u3002\u65e0\u7b26\u53f7\u5b57\u7b26\u5219\u53ef\u4ee5\u4fdd\u5b58 0 \u5230 255 \u8303\u56f4\u5185\u7684\u6574\u6570\u3002","title":"\u5b57\u7b26\u7c7b\u578b\u7684\u5927\u5c0f\u3001\u8303\u56f4\u548c\u9ed8\u8ba4\u7b26\u53f7"},{"location":"4-11-Chars/#_5","text":"C++ \u4e2d\u7684\u67d0\u4e9b\u5b57\u7b26\u662f\u5177\u6709\u7279\u6b8a\u542b\u4e49\u7684\uff0c\u8fd9\u4e9b\u5b57\u7b26\u79f0\u4e3a\u8f6c\u8bd1\u5e8f\u5217\uff08escape sequences\uff09\u3002\u8f6c\u8bd1\u5e8f\u5217\u4ee5 \\ \uff08\u53cd\u659c\u7ebf\uff09\u5f00\u5934\uff0c\u7d27\u63a5\u7740\u662f\u4e00\u4e2a\u5b57\u6bcd\u6216\u6570\u5b57\u3002 \u4f60\u53ef\u80fd\u5df2\u7ecf\u89c1\u8fc7\u5728\u5e38\u9047\u5230\u7684\u8f6c\u4e49\u5e8f\u5217 '\\n' \uff0c\u5b83\u4f1a\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u4e00\u4e2a\u6362\u884c\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << \"First line \\n Second line \\n \" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 First line Second line \u53e6\u5916\u4e00\u4e2a\u5e38\u7528\u7684\u8f6c\u8bd1\u5e8f\u5217\u662f '\\t' \uff0c\u8868\u793a\u4e00\u4e2a\u6c34\u5e73\u5236\u8868\u7b26\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << \"First part \\t Second part\" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u4e3a 1 First part Second part \u5176\u4ed6\u4e00\u4e9b\u503c\u5f97\u6ce8\u610f\u7684\u8f6c\u8bd1\u5e8f\u5217\u6709\uff1a \\' \uff1a\u6253\u5370\u5355\u5f15\u53f7 \\\" \uff1a\u6253\u5370\u53cc\u5f15\u53f7 \\\\ \u6253\u5370\u53cd\u659c\u7ebf \u4e0b\u8868\u5c55\u793a\u4e86\u5168\u90e8\u7684\u8f6c\u4e49\u5e8f\u5217 \u540d\u79f0 \u7b26\u53f7 \u542b\u4e49 Alert \\a Makes an alert, such as a beep Backspace \\b Moves the cursor back one space Formfeed \\f Moves the cursor to next logical page Newline \\n Moves cursor to next line Carriage return \\r Moves cursor to beginning of line Horizontal tab \\t Prints a horizontal tab Vertical tab \\v Prints a vertical tab Single quote \\\u2019 Prints a single quote Double quote \\\u201d Prints a double quote Backslash \\ Prints a backslash. Question mark \\? Prints a question mark. No longer relevant. You can use question marks unescaped. Octal number (number) Translates into char represented by octal Hex number \\x(number) Translates into char represented by hex number \u4e3e\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { std :: cout << \" \\\" This is quoted text \\\"\\n \" ; std :: cout << \"This string contains a single backslash \\\\\\n \" ; std :: cout << \"6F in hex is char ' \\x6F ' \\n \" ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 \"This is quoted text\" This string contains a single backslash \\ 6F in hex is char 'o'","title":"\u8f6c\u8bd1\u5e8f\u5217"},{"location":"4-11-Chars/#newline-n-vs-stdendl","text":"\u5728 1.5 - iostream \u7b80\u4ecb\uff1a cout\uff0ccin \u548c endl \u4e2d\u5df2\u7ecf\u4ecb\u7ecd\u4e86\u3002","title":"Newline (\\n) vs. std::endl"},{"location":"4-11-Chars/#_6","text":"\u5355\u5b57\u7b26\u603b\u662f\u653e\u5728\u5355\u5f15\u53f7\u4e2d (\u4f8b\u5982 \u2018a\u2019, \u2018+\u2019, \u20185\u2019)\u3002\u5b57\u7b26\u53ea\u80fd\u8868\u793a\u4e00\u4e2a\u7b26\u53f7\uff08\u4f8b\u5982\uff0c\u5b57\u6bcd\u3001\u52a0\u53f7\u548c\u6570\u5b575\uff09\uff0c\u4e0b\u9762\u8fd9\u6837\u7684\u5199\u6cd5\u662f\u4e0d\u5bf9\u7684\uff1a 1 char ch { ' 56 ' }; // char \u53ea\u80fd\u4fdd\u6301\u4e00\u4e2a\u7b26\u53f7 \u53cc\u5f15\u53f7\u4e2d\u7684\u6587\u672c\u4f1a\u88ab\u770b\u505a\u662f\u5b57\u7b26\u4e32\uff08\u4f8b\u5982 \u201cHello, world!\u201d\uff09\u3002 \u4f60\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff1a 1 std :: cout << \"Hello, world!\" ; // \"Hello, world!\" is a string literal \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u53ea\u6709\u4e00\u4e2a\u5b57\u7b26\uff0c\u8bf7\u653e\u5728\u5355\u5f15\u53f7\u4e2d (\u4f8b\u5982 \u2018t\u2019 \u6216 \u2018\\n\u2019, \u800c\u4e0d\u662f \u201ct\u201d \u6216 \u201c\\n\u201d)\u3002\u4ee5\u4fbf\u7f16\u8bd1\u5668\u8fdb\u884c\u4f18\u5316\u3002","title":"\u5b57\u7b26\u653e\u5728\u5355\u5f15\u53f7\u548c\u53cc\u5f15\u53f7\u4e2d\u6709\u4ec0\u4e48\u533a\u522b"},{"location":"4-11-Chars/#wchar_t-char16_t-char32_t","text":"wchar_t \u5728\u51e0\u4e4e\u4efb\u4f55\u573a\u5408\u4e0b\u90fd\u5e94\u8be5\u907f\u514d\u4f7f\u7528 (\u5904\u7406\u5728\u4f7f\u7528 Windows API \u65f6)\u3002\u5b83\u7684\u5927\u5c0f\u662f\u5b9e\u73b0\u65f6\u5b9a\u4e49\u7684\uff0c\u975e\u5e38\u4e0d\u53ef\u9760\u800c\u4e14\u51e0\u4e4e\u5df2\u7ecf\u5e9f\u5f03\u4e86\u3002 \u9898\u5916\u8bdd \u672f\u8bed\u201c\u5e9f\u5f03\u201d\u6307\u7684\u662f\uff0c\u4ecd\u7136\u652f\u6301\uff0c\u4f46\u662f\u4e0d\u518d\u63a8\u8350\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u5df2\u7ecf\u88ab\u5176\u4ed6\u66f4\u597d\u7684\u540c\u7c7b\u66ff\u6362\u4e86\uff0c\u540c\u65f6\u5b83\u4e5f\u662f\u4e0d\u5b89\u5168\u7684\u3002 \u548c ASCII \u5c06\u6574\u578b 0-127 \u6620\u5c04\u5230\u82f1\u6587\u5b57\u6bcd\u4e00\u6837\uff0c\u4e5f\u6709\u5176\u4ed6\u5b57\u7b26\u7f16\u7801\u6807\u51c6\u53ef\u4ee5\u5c06\u6574\u578b\u6620\u5c04\u5230\u5176\u4ed6\u8bed\u8a00\u7684\u5b57\u7b26\u3002\u9664\u4e86 ASCII \u4ee5\u5916\u6700\u8457\u540d\u7684\u5b57\u6bcd\u7f16\u7801\u662f Unicode\uff0c\u5b83\u5c06 144,000 \u4e2a\u6574\u578b\u6570\u6620\u5c04\u5230\u4e0d\u540c\u8bed\u8a00\u7684\u5b57\u7b26\u3002\u56e0\u4e3a Unicode \u7684\u7f16\u7801\u70b9\u975e\u5e38\u591a\uff0c\u6240\u4ee5\u4e00\u4e2a Unicode \u7f16\u7801\u70b9\u9700\u898132\u4f4d\u624d\u80fd\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff08\u79f0\u4e3a UTF-32\uff09\u3002\u4e0d\u8fc7\uff0cUnicode \u5b57\u7b26\u4e5f\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a 16 \u4f4d\u6216 8\u4f4d\u5b57\u7b26 (\u5206\u522b\u79f0\u4e3a UTF-16 \u548c UTF-8)\u3002 char16_t \u548c char32_t \u5728 C++11 \u4e2d\u88ab\u5f15\u5165\uff0c\u7528\u4e8e\u652f\u6301 16 \u4f4d\u548c 32 \u4f4d Unicode \u5b57\u7b26\u3002 char8_t \u5728 C++20 \u4e2d\u88ab\u5f15\u5165\u3002 \u4f60\u4e0d\u9700\u8981\u4f7f\u7528 char8_t \u3001 char16_t \u6216 char32_t \uff0c\u89e6\u53d1\u4f60\u5e0c\u671b\u7a0b\u5e8f\u517c\u5bb9 Unicode\u3002Unicode \u548c\u672c\u5730\u5316\u4e00\u822c\u6765\u8bf4\u5df2\u7ecf\u8d85\u51fa\u4e86\u672c\u6559\u7a0b\u7684\u8303\u56f4\uff0c\u6240\u4ee5\u6211\u4eec\u5e76\u4e0d\u4f1a\u6d89\u53ca\u76f8\u5173\u5185\u5bb9\u3002 \u540c\u65f6\uff0c\u5f53\u4f60\u4f7f\u7528\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u7684\u65f6\u5019\uff0c\u5e94\u8be5\u53ea\u4f7f\u7528ASCII\u5b57\u7b26\u3002\u4f7f\u7528\u5176\u4ed6\u5b57\u7b26\u96c6\u7684\u5b57\u7b26\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5b57\u7b26\u663e\u793a\u9519\u8bef\u3002","title":"\u8fd8\u6709\u5176\u4ed6\u5b57\u7b26\u7c7b\u578b\u5417\uff1f wchar_t, char16_t \u548c char32_t?"},{"location":"4-12-Introduction-to-type-conversion-and-static_cast/","tags":["type conversion","static_cast"],"text":"Key Takeaway \u7f16\u8bd1\u5668\u4f1a\u5bf9\u57fa\u672c\u6570\u636e\u7c7b\u578b\u8fdb\u884c\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\uff0c\u4f46\u5bfc\u81f4\u4fe1\u606f\u4e22\u5931\u7684\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u4f1a\u5f15\u53d1\u544a\u8b66\u6216\u9519\u8bef\uff08\u62ec\u53f7\u521d\u59cb\u5316\u65f6\uff09 static_cast(expression) \u8fdb\u884c\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u6bcf\u5f53\u4f60\u770b\u5230\u6709\u5c16\u62ec\u53f7( <> )\u5b58\u5728\u7684 C++ \u8bed\u6cd5\uff08\u9664\u4e86\u9884\u5904\u7406\u5668\u6307\u4ee4\uff09\u65f6\uff0c\u5728\u4e24\u4e2a\u5c16\u62ec\u53f7\u4e2d\u95f4\u7684\u5185\u5bb9\u5f88\u53ef\u80fd\u662f\u4e00\u4e2a\u7c7b\u578b\u540d\u3002\u8fd9\u662fC++\u4e2d\u5904\u7406\u9700\u8981\u53ef\u53c2\u6570\u5316\u7c7b\u578b\u65f6\u7684\u65b9\u6cd5\u3002 static_cast \u8fd0\u7b97\u7b26\u4e0d\u4f1a\u505a\u4efb\u4f55\u8303\u56f4\u68c0\u67e5\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u5728\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u65b0\u7c7b\u578b\u5e76\u4e0d\u80fd\u5305\u542b\u8be5\u503c\uff0c\u5219\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u5927\u591a\u6570\u7f16\u8bd1\u5668\u5c06 std::int8_t \u548c std::uint8_t (\u4ee5\u53ca\u5bf9\u5e94\u7684\u901f\u5ea6\u4f18\u5148\u3001\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u7c7b\u578b) \u5206\u522b\u5f53\u505a signed char \u548c unsigned char \u5904\u7406\u3002 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u8003\u8651\u5982\u4e0b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void print ( double x ) // print takes an double parameter { std :: cout << x ; } int main () { print ( 5 ); // what happens when we pass an int value? return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c print() \u51fd\u6570\u7684 \u5f62\u53c2 \u4e3a double \u7c7b\u578b\uff0c\u4e3b\u8c03\u51fd\u6570\u5c06 5 \u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u4f46\u6b64\u5904 5 \u7684\u7c7b\u578b\u662f\u6574\u578b\u3002\u8fd9\u6837\u505a\u6709\u4ec0\u4e48\u540e\u679c\u5417\uff1f \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0cC++ \u4f1a\u5f88\u4e50\u4e8e\u5c06\u4e00\u79cd\u57fa\u672c\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u57fa\u672c\u7c7b\u578b\uff0c\u8fd9\u4e2a\u8f6c\u6362\u8fc7\u7a0b\u79f0\u4e3a \u7c7b\u578b\u8f6c\u6362(type conversion) \u3002\u56e0\u6b64\uff0c \u5b9e\u53c2 5 \u4f1a\u88ab\u8f6c\u6362\u4e3a double \u7c7b\u578b\u7684\u503c 5.0 \u5e76\u4e14\u88ab\u62f7\u8d1d\u7ed9\u5f62\u53c2 x \u3002\u4e8e\u662f print() \u5c31\u5c06\u8be5\u503c\u6253\u5370\u4e86\u51fa\u6765\uff1a 1 5 \u5f53\u7f16\u8bd1\u5668\u5e2e\u52a9\u6211\u4eec\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u7684\u65f6\u5019\uff0c\u6211\u4eec\u79f0\u5176\u4e3a \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u3002 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u544a\u8b66 \u5c3d\u7ba1\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u5df2\u7ecf\u8db3\u591f\u6ee1\u8db3\u5927\u591a\u6570\u7684\u7c7b\u578b\u8f6c\u6362\u9700\u8981\uff0c\u4f46\u662f\u4ecd\u7136\u6709\u4e9b\u60c5\u51b5\u662f\u65e0\u6cd5\u6ee1\u8db3\u7684\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void print ( int x ) // print \u51fd\u6570\u73b0\u5728\u63a5\u6536\u4e00\u4e2a\u6574\u578b\u7684\u53c2\u6570 { std :: cout << x ; } int main () { print ( 5.5 ); // \u8b66\u544a\uff1a\u6211\u4eec\u4f20\u5165\u4e86\u4e00\u4e2a double \u7c7b\u578b\u7684\u503c\u3002 return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u5c06 print() \u7684\u5f62\u53c2\u6539\u6210\u4e86 int \u7c7b\u578b\uff0c\u800c\u8c03\u7528 print() \u7684\u65f6\u5019\u5374\u4f20\u5165\u4e86\u4e00\u4e2a double \u7c7b\u578b\u7684\u503c 5.5 \u3002\u548c\u6700\u521d\u7684\u4f8b\u5b50\u7684\u60c5\u51b5\u7c7b\u4f3c\uff0c\u7f16\u8bd1\u5668\u4f1a\u5bf9\u5176\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff0c\u5c06 double \u503c 5.5 \u8f6c\u6362\u4e3a int \u7c7b\u578b\uff0c\u4ee5\u4fbf\u80fd\u591f\u88ab\u4f20\u5165 print() \u3002 \u4f46\u662f\u548c\u6700\u521d\u7684\u4f8b\u5b50\u76f8\u6bd4\uff0c\u5f53\u8fd9\u4e2a\u7a0b\u5e8f\u88ab\u7f16\u8bd1\u7684\u65f6\u5019\uff0c\u7f16\u8bd1\u5668\u4f1a\u4ea7\u751f\u5173\u4e8e\u6570\u636e\u4e22\u5931\u7684\u544a\u8b66\u3002\u5982\u679c\u4f60\u6253\u5f00\u4e86\u201c\u5c06\u8b66\u544a\u5f53\u505a\u9519\u8bef\u5904\u7406\u201d\u7684\u8bdd\uff0c\u4f60\u7684\u7f16\u8bd1\u5668\u5c31\u4f1a\u505c\u6b62\u7f16\u8bd1\u8fc7\u7a0b\u3002 \u5c0f\u8d34\u58eb \u4f60\u9700\u8981\u6682\u65f6\u5173\u95ed \u201c\u5c06\u8b66\u544a\u5f53\u505a\u9519\u8bef\u5904\u7406\u201d \u529f\u80fd\u624d\u80fd\u7f16\u8bd1\u8be5\u4f8b\u7a0b\uff0c\u8bf7\u53c2\u8003 0.11 -- Configuring your compiler: Warning and error levels for more information about this setting. \u7f16\u8bd1\u7a0b\u5e8f\u5e76\u8fd0\u884c\uff0c\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 5 \u6ce8\u610f\uff0c\u5c3d\u7ba1\u6211\u4eec\u4f20\u5165\u7684\u503c\u662f 5.5\uff0c\u4f46\u6253\u5370\u7ed3\u679c\u5374\u662f 5\u3002\u56e0\u4e3a\u6574\u578b\u53d8\u91cf\u4e0d\u80fd\u4fdd\u5b58\u5c0f\u6570\u90e8\u5206\uff0c\u6240\u4ee5 double \u7c7b\u578b\u7684\u503c 5.5 \u5c31\u88ab\u8f6c\u6362\u6210\u4e86\u4e00\u4e2a int\uff0c\u5c0f\u6570\u90e8\u5206\u76f4\u63a5\u88ab\u4e22\u5f03\u4e86\uff0c\u53ea\u6709\u6574\u6570\u90e8\u5206\u88ab\u4fdd\u7559\u4e86\u4e0b\u6765\u3002 \u56e0\u4e3a\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u6210\u6574\u578b\u503c\u4f1a\u5bfc\u81f4\u6d88\u5931\u90e8\u5206\u88ab\u4e22\u5f03\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u9700\u8981\u5728\u8fdb\u884c\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u7684\u65f6\u5019\u5411\u6211\u4eec\u53d1\u51fa\u544a\u8b66\u3002\u5373\u4f7f\u6211\u4eec\u4f20\u5165\u7684\u6d6e\u70b9\u6570\u672c\u8eab\u6ca1\u6709\u5c0f\u6570\u90e8\u5206\uff0c\u7f16\u8bd1\u5668\u4ecd\u7136\u4f1a\u53d1\u51fa\u544a\u8b66\uff0c\u4f8b\u5982 5.0 \u88ab\u8f6c\u6362\u6210\u6574\u578b\u65f6\u6ca1\u6709\u4e22\u5931\u5c0f\u6570\uff0c\u4f46\u662f\u7f16\u8bd1\u5668\u4ecd\u7136\u4f1a\u63d0\u9192\u6211\u4eec\u8fd9\u79cd\u8f6c\u6362\u662f\u4e0d\u5b89\u5168\u7684\u3002 \u5173\u952e\u4fe1\u606f \u6709\u4e9b\u7c7b\u578b\u8f6c\u6362\u603b\u662f\u5b89\u5168\u7684\uff08\u4f8b\u5982 int \u5230 double \uff09\uff0c\u800c\u53e6\u5916\u4e00\u4e9b\u7c7b\u578b\u5728\u8f6c\u6362\u65f6\u4f1a\u5bfc\u81f4\u503c\u7684\u53d8\u5316\uff08\u4f8b\u5982 double \u5230 int \uff09\u3002\u4e0d\u5b89\u5168\u7684\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u901a\u5e38\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u544a\u8b66\u6216\u62a5\u9519\uff08\u5728\u62ec\u53f7\u521d\u59cb\u5316\u65f6\uff09\u3002 \u8fd9\u4e5f\u662f\u63a8\u8350\u4f7f\u7528 \u62ec\u53f7\u521d\u59cb\u5316 \u7684\u4e3b\u8981\u539f\u56e0\u4e4b\u4e00\u3002\u62ec\u53f7\u521d\u59cb\u5316\u80fd\u591f\u907f\u514d\u521d\u59cb\u5316\u503c\u56e0\u4e3a\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u800c\u4e22\u5931\u4fe1\u606f\u3002 1 2 3 4 5 6 7 int main () { double d { 5 }; // okay: int to double is safe int x { 5.5 }; // error: double to int not safe return 0 ; } \u76f8\u5173\u5185\u5bb9 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u662f\u4e00\u4e2a\u5185\u5bb9\u975e\u5e38\u4e30\u5bcc\u7684\u8bdd\u9898\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u5bf9\u5176\u8fdb\u884c\u6df1\u5165\u89e3\u6790\uff08 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \uff09\u3002 \u4f7f\u7528 static_cast \u8fd0\u7b97\u7b26\u8fdb\u884c\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u5728\u56de\u5230\u7684 print() \u4f8b\u5b50\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u4e3b\u52a8\u5c06\u4e00\u4e2a double \u7c7b\u578b\u7684\u503c\u8f6c\u6362\u6210 int \u7c7b\u578b\u540e\u4f20\u5165\u51fd\u6570\u5462\uff1f\uff08\u5373\u4f7f\u77e5\u9053\u8fd9\u4e48\u505a\u4f1a\u4e22\u5931\u5c0f\u6570\u90e8\u5206\uff09\u3002\u5173\u95ed\u7f16\u8bd1\u5668\u7684\u201c\u5c06\u8b66\u544a\u5f53\u505a\u9519\u8bef\u5904\u7406\u201d\u529f\u80fd\u5e76\u4e0d\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\uff0c\u56e0\u5b83\u53ef\u80fd\u4f1a\u9020\u6210\u5176\u4ed6\u672c\u5e94\u8be5\u53d1\u73b0\u7684\u95ee\u9898\u88ab\u5ffd\u7565\u3002 C++ \u8fd8\u652f\u6301\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\u8f6c\u6362\uff0c\u79f0\u4e3a \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u3002\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u5141\u8bb8\u6211\u4eec\u660e\u786e\u5730\u544a\u8bc9\u7f16\u8bd1\u5668\u5c06\u4e00\u79cd\u7c7b\u578b\u7684\u503c\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\uff0c\u7a0b\u5e8f\u5458\u4f1a\u4e3a\u6b64\u64cd\u4f5c\u8d1f\u8d23\uff08\u5373\u5982\u679c\u8f6c\u6362\u5bfc\u81f4\u4e86\u4fe1\u606f\u7684\u4e22\u5931\uff0c\u7b97\u6211\u4eec\u7684\u9519\uff09\u3002 \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528 static_cast \u64cd\u4f5c\u7b26\u8fdb\u884c\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u3002 static cast \u7684\u8bed\u6cd5\u770b\u4e0a\u53bb\u6709\u4e9b\u53e4\u602a\uff1a 1 static_cast(expression) static_cast \u5c06\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u6570\uff0c\u7136\u540e\u8fd4\u56de\u7531 new_type \u6307\u5b9a\u7684\u65b0\u7c7b\u578b\u7684\u503c(\u4f8b\u5982 int\u3001bool\u3001char\u3001double)\u3002 \u5173\u952e\u4fe1\u606f \u6bcf\u5f53\u4f60\u770b\u5230\u6709\u5c16\u62ec\u53f7( <> )\u5b58\u5728\u7684 C++ \u8bed\u6cd5\uff08\u9664\u4e86\u9884\u5904\u7406\u5668\u6307\u4ee4\uff09\u65f6\uff0c\u5728\u4e24\u4e2a\u5c16\u62ec\u53f7\u4e2d\u95f4\u7684\u5185\u5bb9\u5f88\u53ef\u80fd\u662f\u4e00\u4e2a\u7c7b\u578b\u540d\u3002\u8fd9\u662f C++ \u4e2d\u5904\u7406\u9700\u8981\u53ef\u53c2\u6570\u5316\u7c7b\u578b\u65f6\u7684\u65b9\u6cd5\u3002 \u4f7f\u7528 static_cast \u4fee\u6539\u4e4b\u524d\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void print ( int x ) { std :: cout << x ; } int main () { print ( static_cast < int > ( 5.5 ) ); // explicitly convert double value 5.5 to an int return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u663e\u5f0f\u5730\u8981\u6c42\u5c06 double \u7c7b\u578b\u7684\u503c 5.5 \u8f6c\u6362\u4e3a int \u7c7b\u578b\u503c\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4e0d\u4f1a\u4ea7\u751f\u544a\u8b66\u4fe1\u606f\uff08\u65e0\u9700\u5173\u95ed \u201c\u5c06\u8b66\u544a\u5f53\u505a\u9519\u8bef\u5904\u7406\u201d \u529f\u80fd\uff09\u3002 \u76f8\u5173\u5185\u5bb9 C++ \u8fd8\u652f\u6301\u5176\u4ed6\u7c7b\u578b\u7684\u8f6c\u6362\u3002\u6211\u4eec\u4f1a\u5728 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd \u4f7f\u7528 static_cast \u5c06 char \u8f6c\u6362\u4e3a int \u5728 4.11 - \u5b57\u7b26 \u4e2d\uff0c\u6211\u4eec\u770b\u5230\u4f7f\u7528 std::cout \u6253\u5370\u5b57\u7b26\u7c7b\u578b\u65f6\uff0c\u5c4f\u5e55\u4e0a\u4f1a\u6253\u5370\u4e00\u4e2a\u5b57\u7b26\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char ch { 97 }; // 97 is ASCII code for 'a' std :: cout << ch << '\\n' ; return 0 ; } This prints: 1 a \u5982\u679c\u6211\u4eec\u60f3\u8981\u6253\u5370\u5176\u5bf9\u5e94\u7684\u6574\u578b\u503c\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06 char \u8f6c\u6362\u4e3a int \uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char ch { 97 }; // 97 \u662f ASCII \u7801\u4e2d\u7684 'a' std :: cout << ch << \" has value \" << static_cast < int > ( ch ) << '\\n' ; // \u5c06 ch \u6253\u5370\u4e3a int return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 a has value 97 \u8bf7\u6ce8\u610f static_cast \u7684\u5f62\u53c2\u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\u3002\u5f53\u4f20\u5165\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u53d8\u91cf\u9996\u5148\u4f1a\u8fdb\u884c\u6c42\u503c\u5e76\u5f97\u5230\u5177\u4f53\u7684\u503c\uff0c\u7136\u540e\u8be5\u503c\u4f1a\u88ab\u8f6c\u6362\u6210\u65b0\u7684\u7c7b\u578b\u3002\u4f5c\u4e3a\u5b9e\u53c2\u7684\u53d8\u91cf\u672c\u8eab\u5e76\u4e0d\u4f1a\u53d7\u5230\u5f71\u54cd\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c ch \u4ecd\u7136\u662f\u5b57\u7b26\u7c7b\u578b\uff0c\u5b83\u4fdd\u5b58\u7684\u503c\u4e5f\u662f\u4ecd\u7136\u662f\u539f\u6765\u7684\u503c\u3002 \u5c06\u65e0\u7b26\u53f7\u6570\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u6570 \u5c06\u65e0\u7b26\u53f7\u7c7b\u578b\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u7c7b\u578b\uff0c\u540c\u6837\u53ef\u4ee5\u4f7f\u7528 static_cast \u8fd0\u7b97\u7b26\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { unsigned int u { 5u }; // 5u \u8868\u793a 5 \u662f\u65e0\u7b26\u53f7\u6574\u578b int s { static_cast < int > ( u ) }; // \u8fd4\u56de\u503c\u7c7b\u578b\u4e3a int std :: cout << s ; return 0 ; } static_cast \u8fd0\u7b97\u7b26\u4e0d\u4f1a\u505a\u4efb\u4f55\u8303\u56f4\u68c0\u67e5\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u5728\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u65b0\u7c7b\u578b\u5e76\u4e0d\u80fd\u5305\u542b\u8be5\u503c\uff0c\u5219\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u56e0\u6b64\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c unsigned int \u4fdd\u5b58\u7684\u503c\u6bd4 int \u80fd\u591f\u4fdd\u5b58\u7684\u6700\u5927\u503c\u8fd8\u5927\uff0c\u5219\u5c06 unsigned int \u8f6c\u6362\u4e3a int \u65f6\u4f1a\u4ea7\u751f\u65e0\u6cd5\u9884\u8ba1\u7684\u7ed3\u679c\u3002 \u6ce8\u610f \u5982\u679c\u88ab\u8f6c\u6362\u7684\u503c\u4e0d\u80fd\u5339\u914d\u65b0\u7c7b\u578b\u7684\u8303\u56f4\uff0c\u5219 static_cast \u8fd0\u7b97\u7b26\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 std::int8_t \u548c std::uint8_t \u884c\u4e3a\u66f4\u50cf chars \u800c\u4e0d\u662f\u6574\u578b \u6b63\u5982 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t \u4e2d\u4ecb\u7ecd\u7684\u90a3\u6837\uff0c\u5927\u591a\u6570\u7f16\u8bd1\u5668\u5c06 std::int8_t \u548c std::uint8_t (\u4ee5\u53ca\u5bf9\u5e94\u7684\u901f\u5ea6\u4f18\u5148\u3001\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u7c7b\u578b) \u5206\u522b\u5f53\u505a signed char \u548c unsigned char \u5904\u7406\u3002\u73b0\u5728\uff0c\u65e2\u7136\u6211\u4eec\u5df2\u7ecf\u4ecb\u7ecd\u4e86\u4ec0\u4e48\u662f\u5b57\u7b26\uff0c\u63a5\u4e0b\u6765\u5c31\u53ef\u4ee5\u8c08\u8c08\uff0c\u7f16\u8bd1\u5668\u8fd9\u6837\u505a\u4f1a\u5e26\u6765\u4ec0\u4e48\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { std :: int8_t myint { 65 }; // initialize myint with value 65 std :: cout << myint ; // you're probably expecting this to print 65 return 0 ; } \u56e0\u4e3a std::int8_t \u81ea\u79f0\u662f\u4e00\u79cd\u6574\u578b\uff0c\u6240\u4ee5\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u4e0a\u9762\u7684\u7a0b\u5e8f\u4f1a\u6253\u5370 65\u3002\u4e0d\u8fc7\uff0c\u5728\u5927\u591a\u6570\u7cfb\u7edf\u4e0a\uff0c\u4e0a\u9762\u7684\u7a0b\u5e8f\u6253\u5370\u7684\u5176\u5b9e\u662f A \uff08 myint \u88ab\u5f53\u505a\u4e86 signed char \uff09\u5904\u7406\u3002\u4f46\u662f\uff0c\u8fd9\u4e5f\u4e0d\u662f\u7edd\u5bf9\u7684\uff08\u6709\u4e9b\u7cfb\u7edf\u786e\u5b9e\u4f1a\u6253\u5370 65\uff09\u3002 \u5982\u679c\u4f60\u60f3\u8981\u786e\u4fdd std::int8_t \u6216 std::uint8_t \u5bf9\u8c61\u90fd\u88ab\u5f53\u505a\u6574\u578b\u5904\u7406\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { std :: int8_t myint { 65 }; std :: cout << static_cast < int > ( myint ); // \u603b\u662f\u6253\u5370 65 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c std::int8_t \u88ab\u5f53\u505a\u6574\u578b\u5904\u7406\u4e86\uff0c\u4f46\u662f\u5982\u679c\u4ece\u63a7\u5236\u53f0\u83b7\u53d6\u8f93\u5165\uff0c\u4ecd\u7136\u4f1a\u5e26\u6765\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include int main () { std :: cout << \"Enter a number between 0 and 255: \" ; std :: int8_t myint {}; std :: cin >> myint ; std :: cout << \"You entered: \" << static_cast < int > ( myint ); return 0 ; } \u8fd0\u884c\u7a0b\u5e8f\uff1a 1 2 Enter a number between 0 and 255: 35 You entered: 51 \u8fd9\u4e2a\u95ee\u9898\u662f\u8fd9\u6837\u7684\uff0c\u5f53 std::int8_t \u88ab\u770b\u505a\u5b57\u7b26\u65f6\uff0c\u8f93\u5165\u51fd\u6570\u4f1a\u5c06\u8f93\u5165\u7684\u5185\u5bb9\u770b\u505a\u662f\u4e00\u7cfb\u5217\u7684\u5b57\u7b26\u800c\u4e0d\u662f\u6574\u578b\u3002\u5f53\u4f60\u8f93\u516535\u4e4b\u540e\uff0c\u4f60\u5b9e\u9645\u4e0a\u6570\u4e86 '3' \u548c '5' \u4e24\u4e2a\u5b57\u7b26\u3002\u56e0\u4e3a\u5b57\u7b26\u7c7b\u578b\u53ea\u80fd\u4fdd\u5b58\u4e00\u4e2a\u5b57\u7b26\uff0c\u56e0\u6b64\u53ea '3' \u88ab\u4fdd\u5b58\u5230\u4e86\u53d8\u91cf\u4e2d\uff08 '5' \u88ab\u7559\u5728\u4e86\u8f93\u5165\u6d41\u4e2d\u4ee5\u4fbf\u540e\u7eed\u7684\u4f7f\u7528\uff09\u3002\u56e0\u4e3a\u5b57\u7b26 '3' \u7684 ASCII \u7801\u662f 51\uff0c\u6240\u4ee551\u5c31\u88ab\u5b58\u653e\u5230\u4e86 myint \u4e2d\uff0c\u6700\u7ec8\u4e5f\u5c31\u88ab\u6253\u5370\u4e86\u51fa\u6765\u3002 \u4e0e\u4e4b\u76f8\u5bf9\u7684\uff0c\u5176\u4ed6\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u5728\u8f93\u5165\u548c\u8f93\u51fa\u65f6\u603b\u662f\u4f1a\u88ab\u5f53\u505a\u6574\u578b\u503c\u5904\u7406\u3002","title":"4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast"},{"location":"4-12-Introduction-to-type-conversion-and-static_cast/#_1","text":"\u8003\u8651\u5982\u4e0b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void print ( double x ) // print takes an double parameter { std :: cout << x ; } int main () { print ( 5 ); // what happens when we pass an int value? return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c print() \u51fd\u6570\u7684 \u5f62\u53c2 \u4e3a double \u7c7b\u578b\uff0c\u4e3b\u8c03\u51fd\u6570\u5c06 5 \u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u4f46\u6b64\u5904 5 \u7684\u7c7b\u578b\u662f\u6574\u578b\u3002\u8fd9\u6837\u505a\u6709\u4ec0\u4e48\u540e\u679c\u5417\uff1f \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0cC++ \u4f1a\u5f88\u4e50\u4e8e\u5c06\u4e00\u79cd\u57fa\u672c\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u57fa\u672c\u7c7b\u578b\uff0c\u8fd9\u4e2a\u8f6c\u6362\u8fc7\u7a0b\u79f0\u4e3a \u7c7b\u578b\u8f6c\u6362(type conversion) \u3002\u56e0\u6b64\uff0c \u5b9e\u53c2 5 \u4f1a\u88ab\u8f6c\u6362\u4e3a double \u7c7b\u578b\u7684\u503c 5.0 \u5e76\u4e14\u88ab\u62f7\u8d1d\u7ed9\u5f62\u53c2 x \u3002\u4e8e\u662f print() \u5c31\u5c06\u8be5\u503c\u6253\u5370\u4e86\u51fa\u6765\uff1a 1 5 \u5f53\u7f16\u8bd1\u5668\u5e2e\u52a9\u6211\u4eec\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u7684\u65f6\u5019\uff0c\u6211\u4eec\u79f0\u5176\u4e3a \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u3002","title":"\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362"},{"location":"4-12-Introduction-to-type-conversion-and-static_cast/#_2","text":"\u5c3d\u7ba1\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u5df2\u7ecf\u8db3\u591f\u6ee1\u8db3\u5927\u591a\u6570\u7684\u7c7b\u578b\u8f6c\u6362\u9700\u8981\uff0c\u4f46\u662f\u4ecd\u7136\u6709\u4e9b\u60c5\u51b5\u662f\u65e0\u6cd5\u6ee1\u8db3\u7684\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void print ( int x ) // print \u51fd\u6570\u73b0\u5728\u63a5\u6536\u4e00\u4e2a\u6574\u578b\u7684\u53c2\u6570 { std :: cout << x ; } int main () { print ( 5.5 ); // \u8b66\u544a\uff1a\u6211\u4eec\u4f20\u5165\u4e86\u4e00\u4e2a double \u7c7b\u578b\u7684\u503c\u3002 return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u5c06 print() \u7684\u5f62\u53c2\u6539\u6210\u4e86 int \u7c7b\u578b\uff0c\u800c\u8c03\u7528 print() \u7684\u65f6\u5019\u5374\u4f20\u5165\u4e86\u4e00\u4e2a double \u7c7b\u578b\u7684\u503c 5.5 \u3002\u548c\u6700\u521d\u7684\u4f8b\u5b50\u7684\u60c5\u51b5\u7c7b\u4f3c\uff0c\u7f16\u8bd1\u5668\u4f1a\u5bf9\u5176\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff0c\u5c06 double \u503c 5.5 \u8f6c\u6362\u4e3a int \u7c7b\u578b\uff0c\u4ee5\u4fbf\u80fd\u591f\u88ab\u4f20\u5165 print() \u3002 \u4f46\u662f\u548c\u6700\u521d\u7684\u4f8b\u5b50\u76f8\u6bd4\uff0c\u5f53\u8fd9\u4e2a\u7a0b\u5e8f\u88ab\u7f16\u8bd1\u7684\u65f6\u5019\uff0c\u7f16\u8bd1\u5668\u4f1a\u4ea7\u751f\u5173\u4e8e\u6570\u636e\u4e22\u5931\u7684\u544a\u8b66\u3002\u5982\u679c\u4f60\u6253\u5f00\u4e86\u201c\u5c06\u8b66\u544a\u5f53\u505a\u9519\u8bef\u5904\u7406\u201d\u7684\u8bdd\uff0c\u4f60\u7684\u7f16\u8bd1\u5668\u5c31\u4f1a\u505c\u6b62\u7f16\u8bd1\u8fc7\u7a0b\u3002 \u5c0f\u8d34\u58eb \u4f60\u9700\u8981\u6682\u65f6\u5173\u95ed \u201c\u5c06\u8b66\u544a\u5f53\u505a\u9519\u8bef\u5904\u7406\u201d \u529f\u80fd\u624d\u80fd\u7f16\u8bd1\u8be5\u4f8b\u7a0b\uff0c\u8bf7\u53c2\u8003 0.11 -- Configuring your compiler: Warning and error levels for more information about this setting. \u7f16\u8bd1\u7a0b\u5e8f\u5e76\u8fd0\u884c\uff0c\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 5 \u6ce8\u610f\uff0c\u5c3d\u7ba1\u6211\u4eec\u4f20\u5165\u7684\u503c\u662f 5.5\uff0c\u4f46\u6253\u5370\u7ed3\u679c\u5374\u662f 5\u3002\u56e0\u4e3a\u6574\u578b\u53d8\u91cf\u4e0d\u80fd\u4fdd\u5b58\u5c0f\u6570\u90e8\u5206\uff0c\u6240\u4ee5 double \u7c7b\u578b\u7684\u503c 5.5 \u5c31\u88ab\u8f6c\u6362\u6210\u4e86\u4e00\u4e2a int\uff0c\u5c0f\u6570\u90e8\u5206\u76f4\u63a5\u88ab\u4e22\u5f03\u4e86\uff0c\u53ea\u6709\u6574\u6570\u90e8\u5206\u88ab\u4fdd\u7559\u4e86\u4e0b\u6765\u3002 \u56e0\u4e3a\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u6210\u6574\u578b\u503c\u4f1a\u5bfc\u81f4\u6d88\u5931\u90e8\u5206\u88ab\u4e22\u5f03\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u9700\u8981\u5728\u8fdb\u884c\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u7684\u65f6\u5019\u5411\u6211\u4eec\u53d1\u51fa\u544a\u8b66\u3002\u5373\u4f7f\u6211\u4eec\u4f20\u5165\u7684\u6d6e\u70b9\u6570\u672c\u8eab\u6ca1\u6709\u5c0f\u6570\u90e8\u5206\uff0c\u7f16\u8bd1\u5668\u4ecd\u7136\u4f1a\u53d1\u51fa\u544a\u8b66\uff0c\u4f8b\u5982 5.0 \u88ab\u8f6c\u6362\u6210\u6574\u578b\u65f6\u6ca1\u6709\u4e22\u5931\u5c0f\u6570\uff0c\u4f46\u662f\u7f16\u8bd1\u5668\u4ecd\u7136\u4f1a\u63d0\u9192\u6211\u4eec\u8fd9\u79cd\u8f6c\u6362\u662f\u4e0d\u5b89\u5168\u7684\u3002 \u5173\u952e\u4fe1\u606f \u6709\u4e9b\u7c7b\u578b\u8f6c\u6362\u603b\u662f\u5b89\u5168\u7684\uff08\u4f8b\u5982 int \u5230 double \uff09\uff0c\u800c\u53e6\u5916\u4e00\u4e9b\u7c7b\u578b\u5728\u8f6c\u6362\u65f6\u4f1a\u5bfc\u81f4\u503c\u7684\u53d8\u5316\uff08\u4f8b\u5982 double \u5230 int \uff09\u3002\u4e0d\u5b89\u5168\u7684\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u901a\u5e38\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u544a\u8b66\u6216\u62a5\u9519\uff08\u5728\u62ec\u53f7\u521d\u59cb\u5316\u65f6\uff09\u3002 \u8fd9\u4e5f\u662f\u63a8\u8350\u4f7f\u7528 \u62ec\u53f7\u521d\u59cb\u5316 \u7684\u4e3b\u8981\u539f\u56e0\u4e4b\u4e00\u3002\u62ec\u53f7\u521d\u59cb\u5316\u80fd\u591f\u907f\u514d\u521d\u59cb\u5316\u503c\u56e0\u4e3a\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u800c\u4e22\u5931\u4fe1\u606f\u3002 1 2 3 4 5 6 7 int main () { double d { 5 }; // okay: int to double is safe int x { 5.5 }; // error: double to int not safe return 0 ; } \u76f8\u5173\u5185\u5bb9 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u662f\u4e00\u4e2a\u5185\u5bb9\u975e\u5e38\u4e30\u5bcc\u7684\u8bdd\u9898\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u5bf9\u5176\u8fdb\u884c\u6df1\u5165\u89e3\u6790\uff08 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \uff09\u3002","title":"\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u544a\u8b66"},{"location":"4-12-Introduction-to-type-conversion-and-static_cast/#static_cast","text":"\u5728\u56de\u5230\u7684 print() \u4f8b\u5b50\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u4e3b\u52a8\u5c06\u4e00\u4e2a double \u7c7b\u578b\u7684\u503c\u8f6c\u6362\u6210 int \u7c7b\u578b\u540e\u4f20\u5165\u51fd\u6570\u5462\uff1f\uff08\u5373\u4f7f\u77e5\u9053\u8fd9\u4e48\u505a\u4f1a\u4e22\u5931\u5c0f\u6570\u90e8\u5206\uff09\u3002\u5173\u95ed\u7f16\u8bd1\u5668\u7684\u201c\u5c06\u8b66\u544a\u5f53\u505a\u9519\u8bef\u5904\u7406\u201d\u529f\u80fd\u5e76\u4e0d\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\uff0c\u56e0\u5b83\u53ef\u80fd\u4f1a\u9020\u6210\u5176\u4ed6\u672c\u5e94\u8be5\u53d1\u73b0\u7684\u95ee\u9898\u88ab\u5ffd\u7565\u3002 C++ \u8fd8\u652f\u6301\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\u8f6c\u6362\uff0c\u79f0\u4e3a \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u3002\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u5141\u8bb8\u6211\u4eec\u660e\u786e\u5730\u544a\u8bc9\u7f16\u8bd1\u5668\u5c06\u4e00\u79cd\u7c7b\u578b\u7684\u503c\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\uff0c\u7a0b\u5e8f\u5458\u4f1a\u4e3a\u6b64\u64cd\u4f5c\u8d1f\u8d23\uff08\u5373\u5982\u679c\u8f6c\u6362\u5bfc\u81f4\u4e86\u4fe1\u606f\u7684\u4e22\u5931\uff0c\u7b97\u6211\u4eec\u7684\u9519\uff09\u3002 \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528 static_cast \u64cd\u4f5c\u7b26\u8fdb\u884c\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u3002 static cast \u7684\u8bed\u6cd5\u770b\u4e0a\u53bb\u6709\u4e9b\u53e4\u602a\uff1a 1 static_cast(expression) static_cast \u5c06\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u6570\uff0c\u7136\u540e\u8fd4\u56de\u7531 new_type \u6307\u5b9a\u7684\u65b0\u7c7b\u578b\u7684\u503c(\u4f8b\u5982 int\u3001bool\u3001char\u3001double)\u3002 \u5173\u952e\u4fe1\u606f \u6bcf\u5f53\u4f60\u770b\u5230\u6709\u5c16\u62ec\u53f7( <> )\u5b58\u5728\u7684 C++ \u8bed\u6cd5\uff08\u9664\u4e86\u9884\u5904\u7406\u5668\u6307\u4ee4\uff09\u65f6\uff0c\u5728\u4e24\u4e2a\u5c16\u62ec\u53f7\u4e2d\u95f4\u7684\u5185\u5bb9\u5f88\u53ef\u80fd\u662f\u4e00\u4e2a\u7c7b\u578b\u540d\u3002\u8fd9\u662f C++ \u4e2d\u5904\u7406\u9700\u8981\u53ef\u53c2\u6570\u5316\u7c7b\u578b\u65f6\u7684\u65b9\u6cd5\u3002 \u4f7f\u7528 static_cast \u4fee\u6539\u4e4b\u524d\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void print ( int x ) { std :: cout << x ; } int main () { print ( static_cast < int > ( 5.5 ) ); // explicitly convert double value 5.5 to an int return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u663e\u5f0f\u5730\u8981\u6c42\u5c06 double \u7c7b\u578b\u7684\u503c 5.5 \u8f6c\u6362\u4e3a int \u7c7b\u578b\u503c\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4e0d\u4f1a\u4ea7\u751f\u544a\u8b66\u4fe1\u606f\uff08\u65e0\u9700\u5173\u95ed \u201c\u5c06\u8b66\u544a\u5f53\u505a\u9519\u8bef\u5904\u7406\u201d \u529f\u80fd\uff09\u3002 \u76f8\u5173\u5185\u5bb9 C++ \u8fd8\u652f\u6301\u5176\u4ed6\u7c7b\u578b\u7684\u8f6c\u6362\u3002\u6211\u4eec\u4f1a\u5728 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd","title":"\u4f7f\u7528 static_cast \u8fd0\u7b97\u7b26\u8fdb\u884c\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362"},{"location":"4-12-Introduction-to-type-conversion-and-static_cast/#static_cast-char-int","text":"\u5728 4.11 - \u5b57\u7b26 \u4e2d\uff0c\u6211\u4eec\u770b\u5230\u4f7f\u7528 std::cout \u6253\u5370\u5b57\u7b26\u7c7b\u578b\u65f6\uff0c\u5c4f\u5e55\u4e0a\u4f1a\u6253\u5370\u4e00\u4e2a\u5b57\u7b26\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char ch { 97 }; // 97 is ASCII code for 'a' std :: cout << ch << '\\n' ; return 0 ; } This prints: 1 a \u5982\u679c\u6211\u4eec\u60f3\u8981\u6253\u5370\u5176\u5bf9\u5e94\u7684\u6574\u578b\u503c\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06 char \u8f6c\u6362\u4e3a int \uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char ch { 97 }; // 97 \u662f ASCII \u7801\u4e2d\u7684 'a' std :: cout << ch << \" has value \" << static_cast < int > ( ch ) << '\\n' ; // \u5c06 ch \u6253\u5370\u4e3a int return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 a has value 97 \u8bf7\u6ce8\u610f static_cast \u7684\u5f62\u53c2\u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\u3002\u5f53\u4f20\u5165\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u53d8\u91cf\u9996\u5148\u4f1a\u8fdb\u884c\u6c42\u503c\u5e76\u5f97\u5230\u5177\u4f53\u7684\u503c\uff0c\u7136\u540e\u8be5\u503c\u4f1a\u88ab\u8f6c\u6362\u6210\u65b0\u7684\u7c7b\u578b\u3002\u4f5c\u4e3a\u5b9e\u53c2\u7684\u53d8\u91cf\u672c\u8eab\u5e76\u4e0d\u4f1a\u53d7\u5230\u5f71\u54cd\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c ch \u4ecd\u7136\u662f\u5b57\u7b26\u7c7b\u578b\uff0c\u5b83\u4fdd\u5b58\u7684\u503c\u4e5f\u662f\u4ecd\u7136\u662f\u539f\u6765\u7684\u503c\u3002","title":"\u4f7f\u7528 static_cast \u5c06 char \u8f6c\u6362\u4e3a int"},{"location":"4-12-Introduction-to-type-conversion-and-static_cast/#_3","text":"\u5c06\u65e0\u7b26\u53f7\u7c7b\u578b\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u7c7b\u578b\uff0c\u540c\u6837\u53ef\u4ee5\u4f7f\u7528 static_cast \u8fd0\u7b97\u7b26\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { unsigned int u { 5u }; // 5u \u8868\u793a 5 \u662f\u65e0\u7b26\u53f7\u6574\u578b int s { static_cast < int > ( u ) }; // \u8fd4\u56de\u503c\u7c7b\u578b\u4e3a int std :: cout << s ; return 0 ; } static_cast \u8fd0\u7b97\u7b26\u4e0d\u4f1a\u505a\u4efb\u4f55\u8303\u56f4\u68c0\u67e5\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u5728\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u65b0\u7c7b\u578b\u5e76\u4e0d\u80fd\u5305\u542b\u8be5\u503c\uff0c\u5219\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u56e0\u6b64\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c unsigned int \u4fdd\u5b58\u7684\u503c\u6bd4 int \u80fd\u591f\u4fdd\u5b58\u7684\u6700\u5927\u503c\u8fd8\u5927\uff0c\u5219\u5c06 unsigned int \u8f6c\u6362\u4e3a int \u65f6\u4f1a\u4ea7\u751f\u65e0\u6cd5\u9884\u8ba1\u7684\u7ed3\u679c\u3002 \u6ce8\u610f \u5982\u679c\u88ab\u8f6c\u6362\u7684\u503c\u4e0d\u80fd\u5339\u914d\u65b0\u7c7b\u578b\u7684\u8303\u56f4\uff0c\u5219 static_cast \u8fd0\u7b97\u7b26\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u3002","title":"\u5c06\u65e0\u7b26\u53f7\u6570\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u6570"},{"location":"4-12-Introduction-to-type-conversion-and-static_cast/#stdint8_t-stduint8_t-chars","text":"\u6b63\u5982 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t \u4e2d\u4ecb\u7ecd\u7684\u90a3\u6837\uff0c\u5927\u591a\u6570\u7f16\u8bd1\u5668\u5c06 std::int8_t \u548c std::uint8_t (\u4ee5\u53ca\u5bf9\u5e94\u7684\u901f\u5ea6\u4f18\u5148\u3001\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u7c7b\u578b) \u5206\u522b\u5f53\u505a signed char \u548c unsigned char \u5904\u7406\u3002\u73b0\u5728\uff0c\u65e2\u7136\u6211\u4eec\u5df2\u7ecf\u4ecb\u7ecd\u4e86\u4ec0\u4e48\u662f\u5b57\u7b26\uff0c\u63a5\u4e0b\u6765\u5c31\u53ef\u4ee5\u8c08\u8c08\uff0c\u7f16\u8bd1\u5668\u8fd9\u6837\u505a\u4f1a\u5e26\u6765\u4ec0\u4e48\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { std :: int8_t myint { 65 }; // initialize myint with value 65 std :: cout << myint ; // you're probably expecting this to print 65 return 0 ; } \u56e0\u4e3a std::int8_t \u81ea\u79f0\u662f\u4e00\u79cd\u6574\u578b\uff0c\u6240\u4ee5\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u4e0a\u9762\u7684\u7a0b\u5e8f\u4f1a\u6253\u5370 65\u3002\u4e0d\u8fc7\uff0c\u5728\u5927\u591a\u6570\u7cfb\u7edf\u4e0a\uff0c\u4e0a\u9762\u7684\u7a0b\u5e8f\u6253\u5370\u7684\u5176\u5b9e\u662f A \uff08 myint \u88ab\u5f53\u505a\u4e86 signed char \uff09\u5904\u7406\u3002\u4f46\u662f\uff0c\u8fd9\u4e5f\u4e0d\u662f\u7edd\u5bf9\u7684\uff08\u6709\u4e9b\u7cfb\u7edf\u786e\u5b9e\u4f1a\u6253\u5370 65\uff09\u3002 \u5982\u679c\u4f60\u60f3\u8981\u786e\u4fdd std::int8_t \u6216 std::uint8_t \u5bf9\u8c61\u90fd\u88ab\u5f53\u505a\u6574\u578b\u5904\u7406\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { std :: int8_t myint { 65 }; std :: cout << static_cast < int > ( myint ); // \u603b\u662f\u6253\u5370 65 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c std::int8_t \u88ab\u5f53\u505a\u6574\u578b\u5904\u7406\u4e86\uff0c\u4f46\u662f\u5982\u679c\u4ece\u63a7\u5236\u53f0\u83b7\u53d6\u8f93\u5165\uff0c\u4ecd\u7136\u4f1a\u5e26\u6765\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include int main () { std :: cout << \"Enter a number between 0 and 255: \" ; std :: int8_t myint {}; std :: cin >> myint ; std :: cout << \"You entered: \" << static_cast < int > ( myint ); return 0 ; } \u8fd0\u884c\u7a0b\u5e8f\uff1a 1 2 Enter a number between 0 and 255: 35 You entered: 51 \u8fd9\u4e2a\u95ee\u9898\u662f\u8fd9\u6837\u7684\uff0c\u5f53 std::int8_t \u88ab\u770b\u505a\u5b57\u7b26\u65f6\uff0c\u8f93\u5165\u51fd\u6570\u4f1a\u5c06\u8f93\u5165\u7684\u5185\u5bb9\u770b\u505a\u662f\u4e00\u7cfb\u5217\u7684\u5b57\u7b26\u800c\u4e0d\u662f\u6574\u578b\u3002\u5f53\u4f60\u8f93\u516535\u4e4b\u540e\uff0c\u4f60\u5b9e\u9645\u4e0a\u6570\u4e86 '3' \u548c '5' \u4e24\u4e2a\u5b57\u7b26\u3002\u56e0\u4e3a\u5b57\u7b26\u7c7b\u578b\u53ea\u80fd\u4fdd\u5b58\u4e00\u4e2a\u5b57\u7b26\uff0c\u56e0\u6b64\u53ea '3' \u88ab\u4fdd\u5b58\u5230\u4e86\u53d8\u91cf\u4e2d\uff08 '5' \u88ab\u7559\u5728\u4e86\u8f93\u5165\u6d41\u4e2d\u4ee5\u4fbf\u540e\u7eed\u7684\u4f7f\u7528\uff09\u3002\u56e0\u4e3a\u5b57\u7b26 '3' \u7684 ASCII \u7801\u662f 51\uff0c\u6240\u4ee551\u5c31\u88ab\u5b58\u653e\u5230\u4e86 myint \u4e2d\uff0c\u6700\u7ec8\u4e5f\u5c31\u88ab\u6253\u5370\u4e86\u51fa\u6765\u3002 \u4e0e\u4e4b\u76f8\u5bf9\u7684\uff0c\u5176\u4ed6\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u5728\u8f93\u5165\u548c\u8f93\u51fa\u65f6\u603b\u662f\u4f1a\u88ab\u5f53\u505a\u6574\u578b\u503c\u5904\u7406\u3002","title":"std::int8_t \u548c std::uint8_t \u884c\u4e3a\u66f4\u50cf chars \u800c\u4e0d\u662f\u6574\u578b"},{"location":"4-13-Const-variables-and-symbolic-constants/","tags":["const"],"text":"Key Takeaway const \u53d8\u91cf\u5fc5\u987b\u521d\u59cb\u5316\uff0c\u4e14\u521d\u59cb\u5316\u4e4b\u540e\u503c\u4e0d\u53ef\u4ee5\u6539\u53d8 \u51fd\u6570\u53c2\u6570\u53ef\u4ee5\u4e3a const \uff0c\u4f46\u662f\u4f20\u503c\u7684\u60c5\u51b5\u4e0b\u4e0d\u9700\u8981\u7528 \u51fd\u6570\u7684\u8fd4\u56de\u503c\u53ef\u4ee5\u4e3a const \uff0c\u4f46\u662f\u4e00\u822c\u4e0d\u8981\u7528\uff0c\u6ca1\u6709\u610f\u4e49\u800c\u4e14\u4f1a\u5f71\u54cd\u6027\u80fd \u907f\u514d\u5c06\u5bf9\u8c61\u5f62\u5f0f\u7684\u9884\u5904\u7406\u5668\u5b8f\u7528\u4e8e\u7b26\u53f7\u5e38\u91cf \u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u5171\u4eab\u7b26\u53f7\u5e38\u91cf\uff0c\u53ef\u4ee5 \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf \u5728\u7f16\u7a0b\u4e2d\uff0c\u5e38\u91cf\uff08constant\uff09\u6307\u7684\u662f\u4e0d\u4f1a\u6539\u53d8\u7684\u503c\u3002C++\u652f\u6301\u51e0\u79cd\u7c7b\u578b\u7684\u5e38\u91cf\uff1a const \u53d8\u91cf\uff08\u53c2\u89c1\uff1a 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr \uff09\u548c \u5b57\u9762\u91cf \uff08\u53c2\u89c1\uff1a 4.15 - \u5b57\u9762\u91cf \uff09 const \u53d8\u91cf \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u770b\u5230\u7684\u6240\u6709\u53d8\u91cf\u90fd\u662f\u975e\u5e38\u91cf\u2014\u2014\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4eec\u7684\u503c\u53ef\u4ee5\u5728\u4efb\u4f55\u65f6\u5019\u66f4\u6539(\u901a\u5e38\u901a\u8fc7\u8d4b\u503c)\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 int main () { int x { 4 }; // x is a non-constant variable x = 5 ; // change value of x to 5 using assignment operator return 0 ; } \u4e0d\u8fc7\uff0c\u6709\u65f6\u5019\u9700\u8981\u5c06\u53d8\u91cf\u5b9a\u4e49\u4e3a\u4e0d\u80fd\u6539\u53d8\u3002\u4f8b\u5982\uff0c\u5730\u7403\u7684\u5f15\u529b\u662f 9.8 meters/second^2 \uff0c\u8fd9\u4e2a\u503c\u4e0d\u592a\u53ef\u80fd\u4f1a\u968f\u65f6\u6539\u53d8\uff08\u5982\u679c\u771f\u7684\u4f1a\u968f\u65f6\u6539\u53d8\u7684\u8bdd\uff0c\u4f60\u5e94\u8be5\u62c5\u5fc3\u7684\u5c31\u4e0d\u662f C++\u4e86\uff09\u3002\u5c06\u8be5\u503c\u5b9a\u4e49\u4e3a\u5e38\u91cf\u6709\u52a9\u4e8e\u786e\u4fdd\u8be5\u503c\u4e0d\u4f1a\u610f\u5916\u66f4\u6539\u3002\u5e38\u91cf\u8fd8\u6709\u5176\u4ed6\u597d\u5904\uff0c\u6211\u4eec\u7a0d\u540e\u5c06\u63a2\u8ba8\u3002 \u503c\u4e0d\u80fd\u4fee\u6539\u7684\u53d8\u91cf\u79f0\u4e3a\u5e38\u6570\u53d8\u91cf\u3002 const \u5173\u952e\u5b57 \u5c06\u53d8\u91cf\u5b9a\u4e49\u4e3a\u5e38\u91cf\uff0c\u53ea\u9700\u8981\u5728\u7c7b\u578b\u524d\u9762\u6216\u540e\u9762\u6dfb\u52a0 const \u5173\u952e\u5b57\uff0c\u4f8b\u5982\uff1a 1 2 const double gravity { 9.8 }; // preferred use of const before type int const sidesInSquare { 4 }; // \"east const\" style, okay but not preferred \u5c3d\u7ba1 C++ \u5141\u8bb8\u4f60\u5728\u7c7b\u578b\u524d\u9762\u6216\u8005\u540e\u9762\u6dfb\u52a0 const \u5173\u952e\u5b57\uff0c\u6211\u4eec\u8fd8\u662f\u63a8\u8350\u4f60\u628a\u5b83\u653e\u5728\u7c7b\u578b\u524d\u9762\uff0c\u8fd9\u6837\u770b\u4e0a\u53bb\u66f4\u50cf\u662f\u6b63\u5e38\u7684\u82f1\u8bed\u8bed\u6cd5\uff08\u4f8b\u5982\uff1a\u201ca green ball\u201d \u800c\u4e0d\u662f \u201ca ball green\u201d\uff09\u3002 \u9898\u5916\u8bdd \u57fa\u4e8e\u7f16\u8bd1\u5668\u89e3\u6790\u590d\u6742\u58f0\u660e\u7684\u65b9\u5f0f\uff0c\u4e00\u4e9b\u5f00\u53d1\u4eba\u5458\u66f4\u559c\u6b22\u5c06 const \u653e\u5728\u7c7b\u578b\u4e4b\u540e(\u66f4\u4e00\u81f4)\u3002\u8fd9\u79cd\u98ce\u683c\u88ab\u79f0\u4e3a\u201ceast const\u201d\u3002\u867d\u7136\u8fd9\u79cd\u98ce\u683c\u6709\u4e00\u4e9b\u62e5\u62a4\u8005(\u4e5f\u6709\u4e00\u4e9b\u5408\u7406\u7684\u89c2\u70b9)\uff0c\u4f46\u5b83\u5e76\u6ca1\u6709\u5927\u53d7\u6b22\u8fce\u3002 \u6700\u4f73\u5b9e\u8df5 \u628a const \u653e\u7f6e\u5728\u7c7b\u578b\u524d\uff08\u66f4\u7b26\u5408\u4e60\u60ef\u7684\u505a\u6cd5\uff09\u3002 Const \u53d8\u91cf\u5fc5\u987b\u521d\u59cb\u5316 const \u53d8\u91cf \u5fc5\u987b \u5728\u5b9a\u4e49\u65f6\u521d\u59cb\u5316\uff0c\u6b64\u540e\u4f60\u4e5f\u4e0d\u80fd\u901a\u8fc7\u8d4b\u503c\u6765\u6539\u53d8\u5b83\uff1a 1 2 3 4 5 6 7 int main () { const double gravity ; // error: const variables must be initialized gravity = 9.9 ; // error: const variables can not be changed return 0 ; } \u6ce8\u610f\uff0cconst \u53d8\u91cf\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u7c7b\u578b\u7684\u53d8\u91cf\u521d\u59cb\u5316\uff08\u5305\u62ec\u975e const \u7c7b\u578b\u7684\u53d8\u91cf\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << \"Enter your age: \" ; int age {}; std :: cin >> age ; const int constAge { age }; // initialize const variable using non-const value age = 5 ; // ok: age is non-const, so we can change its value constAge = 6 ; // error: constAge is const, so we cannot change its value return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u7528\u975e const \u53d8\u91cf age \u521d\u59cb\u5316 const \u53d8\u91cf constAge \u3002\u56e0\u4e3a age \u4ecd\u7136\u662f\u975e const \u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u6539\u53d8\u5b83\u7684\u503c\u3002\u4f46\u662f\uff0c\u7531\u4e8e constAge \u662f const \uff0c\u6211\u4eec\u4e0d\u80fd\u5728\u521d\u59cb\u5316\u540e\u66f4\u6539\u5b83\u7684\u503c\u3002 const \u53d8\u91cf\u547d\u540d const \u53d8\u91cf\u7684\u547d\u540d\u6709\u5f88\u591a\u4e0d\u540c\u7684\u4e60\u60ef\u3002 \u4ece C \u8bed\u8a00\u8fc7\u6765\u7684\u7a0b\u5e8f\u5458\u559c\u6b22\u4f7f\u7528\u4e0b\u5212\u7ebf\u3001\u5168\u5927\u5199\u5b57\u6bcd\u7684\u65b9\u5f0f\u547d\u540d const \u53d8\u91cf \uff08\u4f8b\u5982 EARTH_GRAVITY \uff09\uff0c\u800c C++ \u4e2d\u5219\u591a\u4f7f\u7528\u5927\u5c0f\u5199\u4ea4\u66ff\u7684\u65b9\u5f0f\uff0c\u540c\u65f6\u6dfb\u52a0 k \u4f5c\u4e3a\u524d\u7f00(\u4f8b\u5982 kEarthGravity )\u3002 \u4e0d\u8fc7\uff0c\u7531\u4e8e const \u53d8\u91cf\u7684\u884c\u4e3a\u548c\u666e\u901a\u53d8\u91cf\u6ca1\u4ec0\u4e48\u533a\u522b\uff08\u9664\u4e86\u4e0d\u80fd\u8d4b\u503c\u4ee5\u5916\uff09\uff0c\u6240\u4ee5\u6ca1\u5fc5\u8981\u4e13\u95e8\u4e3a\u5b83\u4f7f\u7528\u4e00\u79cd\u7279\u6b8a\u5f62\u5f0f\u7684\u547d\u540d\u65b9\u5f0f\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5efa\u8bae\u4f7f\u7528\u548c\u975e const \u7c7b\u578b\u4e00\u6837\u7684\u53d8\u91cf\u540d\uff08\u4f8b\u5982 earthGravity \uff09\u3002 Const \u51fd\u6570\u5f62\u53c2 \u51fd\u6570\u7684 \u5f62\u53c2 \u4e5f\u53ef\u4ee5\u901a\u8fc7 const \u5173\u952e\u5b57\u5b9a\u4e49\u4e3a\u5e38\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include void printInt ( const int x ) { std :: cout << x ; } int main () { printInt ( 5 ); // 5 will be used as the initializer for x printInt ( 6 ); // 6 will be used as the initializer for x return 0 ; } \u6ce8\u610f\uff0c\u6211\u4eec\u6ca1\u6709\u4e3a const \u5f62\u53c2 x \u63d0\u4f9b\u663e\u5f0f\u7684\u521d\u59cb\u5316\u503c\u2014\u2014\u51fd\u6570\u8c03\u7528\u4e2d\u7684\u5b9e\u53c2\u503c\u5c06\u88ab\u7528\u4f5c x \u7684\u521d\u59cb\u5316\u5f0f\u3002 \u5c06\u51fd\u6570\u5f62\u53c2\u8bbe\u4e3a\u5e38\u91cf\u53ef\u4ee5\u5f97\u5230\u7f16\u8bd1\u5668\u7684\u5e2e\u52a9\uff0c\u4ee5\u786e\u4fdd\u5f62\u53c2\u7684\u503c\u4e0d\u4f1a\u5728\u51fd\u6570\u5185\u90e8\u88ab\u66f4\u6539\u3002\u7136\u800c\uff0c\u5f53\u5b9e\u53c2\u901a\u8fc7 \u6309\u503c\u4f20\u9012 \u65f6\uff0c\u6211\u4eec\u901a\u5e38\u4e0d\u5173\u5fc3\u51fd\u6570\u662f\u5426\u6539\u53d8\u4e86\u5f62\u53c2\u7684\u503c(\u56e0\u4e3a\u5b83\u53ea\u662f\u4e00\u4e2a\u526f\u672c\uff0c\u65e0\u8bba\u5982\u4f55\u90fd\u4f1a\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u9500\u6bc1)\u3002\u7531\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u6211\u4eec\u901a\u5e38\u4e0d\u4f7f\u7528 const \u5f62\u53c2\u6765\u4f20\u9012\u503c(\u56e0\u4e3a\u5b83\u4f1a\u7ed9\u6211\u4eec\u7684\u4ee3\u7801\u589e\u52a0\u6df7\u4e71\uff0c\u800c\u4e0d\u4f1a\u63d0\u4f9b\u592a\u591a\u7684\u5b9e\u9645\u4ef7\u503c)\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728 \u6309\u503c\u4f20\u9012 \u65f6\u4e0d\u8981\u4f7f\u7528 const \u3002 \u5728\u672c\u7cfb\u5217\u6559\u7a0b\u7684\u540e\u9762\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5411\u51fd\u6570\u4f20\u9012\u53c2\u6570\u7684\u53e6\u5916\u4e24\u79cd\u65b9\u6cd5\uff1a \u4f20\u5f15\u7528 \u548c \u4f20\u5730\u5740 \u3002\u5728\u4f7f\u7528\u8fd9\u4e24\u79cd\u65b9\u6cd5\u65f6\uff0c\u6b63\u786e\u4f7f\u7528 const \u975e\u5e38\u91cd\u8981\u3002 Const \u7c7b\u578b\u8fd4\u56de\u503c \u51fd\u6570\u7684\u8fd4\u56de\u503c\u540c\u6837\u53ef\u4ee5\u662f const \u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include const int getValue () { return 5 ; } int main () { std :: cout << getValue (); return 0 ; } \u7136\u800c\uff0c\u7531\u4e8e\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u526f\u672c\uff0c\u5c06\u5176\u8bbe\u4e3a const \u6ca1\u6709\u4ec0\u4e48\u610f\u4e49\u3002\u8fd4\u56de const \u503c\u8fd8\u53ef\u80fd\u59a8\u788d\u67d0\u4e9b\u7c7b\u578b\u7684\u7f16\u8bd1\u5668\u4f18\u5316\uff0c\u4ece\u800c\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e0d\u8981\u4f7f\u7528 const \u7c7b\u578b\u7684\u8fd4\u56de\u503c\u3002 \u4ec0\u4e48\u662f\u7b26\u53f7\u5e38\u91cf\uff1f \u7b26\u53f7\u5e38\u6570\u662f\u8d4b\u4e88\u5e38\u6570\u503c\u7684\u540d\u79f0\u3002\u5e38\u91cf\u53d8\u91cf\u662f\u7b26\u53f7\u5e38\u91cf\u7684\u4e00\u79cd\uff0c\u56e0\u4e3a\u53d8\u91cf\u6709\u4e00\u4e2a\u540d\u79f0(\u5b83\u7684\u6807\u8bc6\u7b26)\u548c\u4e00\u4e2a\u5e38\u91cf\u503c\u3002 \u5728 2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86 \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f \u6709\u4e24\u79cd\u5f62\u5f0f\u2014\u2014\u4e00\u79cd\u7528\u4e8e\u66ff\u6362\uff0c\u4e00\u79cd\u4e0d\u7528\u4e8e\u66ff\u6362\u3002\u8fd9\u91cc\u6211\u4eec\u4f1a\u8ba8\u8bba\u4e00\u4e0b\u7528\u4e8e\u66ff\u6362\u7684\u5b8f\uff0c\u5b83\u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 `#define` identifier substitution_text \u6bcf\u5f53 \u9884\u5904\u7406\u5668 \u9047\u5230\u8be5\u6307\u4ee4\u65f6\uff0c\u540e\u7eed\u6240\u6709 identifier \u90fd\u4f1a\u88ab\u66ff\u6362\u4e3a substitution_text \u3002\u8fd9\u91cc\u7684 identifier \u901a\u5e38\u4f1a\u4f7f\u7528\u5168\u5927\u5199\u5f62\u5f0f\u5e76\u4f7f\u7528\u4e0b\u5212\u7ebf\u4ee3\u66ff\u7a7a\u683c\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 #include #define MAX_STUDENTS_PER_CLASS 30 int main () { std :: cout << \"The class has \" << MAX_STUDENTS_PER_CLASS << \" students. \\n \" ; return 0 ; } \u5f53\u4f60\u7f16\u8bd1\u4ee3\u7801\u65f6\uff0c\u9884\u5904\u7406\u5668\u5c31\u4f1a\u628a MAX_STUDENTS_PER_CLASS \u66ff\u6362\u4e3a\u5b57\u9762\u91cf 30\uff0c\u7136\u540e\u88ab\u7f16\u8bd1\u5230\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\u3002 \u56e0\u4e3a\u5b8f\u662f\u6709\u540d\u5b57\u7684\uff0c\u800c\u4e14\u5b83\u7684\u66ff\u6362\u6587\u672c\u662f\u4e00\u4e2a\u5e38\u91cf\uff0c\u6240\u4ee5\u5b83\u4e5f\u5c5e\u4e8e\u7b26\u53f7\u5e38\u91cf\u7684\u4e00\u79cd\u3002 \u907f\u514d\u5c06\u5bf9\u8c61\u5f62\u5f0f\u7684\u9884\u5904\u7406\u5668\u5b8f\u7528\u4e8e\u7b26\u53f7\u5e38\u91cf \u6240\u4ee5\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u7528 #define \u5b9a\u4e49\u7b26\u53f7\u5e38\u91cf\u5462\uff1f\u8fd9\u91cc\u6709\uff08\u81f3\u5c11\uff09\u4e09\u4e2a\u4e3b\u8981\u95ee\u9898\u3002 \u9996\u5148\uff0c\u56e0\u4e3a\u5b8f\u7684\u89e3\u6790\u662f\u9884\u5904\u7406\u5668\u8d1f\u8d23\u7684\uff0c\u6240\u4ee5\u6240\u6709\u7684\u66ff\u6362\u90fd\u53d1\u751f\u5728\u7f16\u8bd1\u4e4b\u524d\u3002\u5f53\u4f60\u8c03\u8bd5\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u4f60\u65e0\u6cd5\u770b\u5230\u5b9e\u9645\u7684\u503c\uff08\u4f8b\u5982 30\uff09\uff0c\u800c\u53ea\u80fd\u770b\u5230\u8be5\u7b26\u53f7\u5e38\u91cf\u7684\u540d\u5b57(\u4f8b\u5982 MAX_STUDENTS_PER_CLASS )\u3002\u800c\u4e14\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u5b8f\u5b9a\u4e49\u5e76\u4e0d\u662f\u53d8\u91cf\uff0c\u6240\u4ee5\u518d\u8c03\u8bd5\u5668\u4e2d\u4f60\u6ca1\u6cd5\u5bf9\u5176\u503c\u8fdb\u884c\u76d1\u63a7\u3002 \u5982\u679c\u4f60\u60f3\u8981\u6307\u5b9a MAX_STUDENTS_PER_CLASS \u89e3\u6790\u540e\u7684\u503c\u662f\u591a\u5c11\uff0c\u4f60\u5fc5\u987b\u53d6\u627e\u5230 MAX_STUDENTS_PER_CLASS \u7684\u5b9a\u4e49\u624d\u884c(\u8be5\u5b9a\u4e49\u8fd8\u53ef\u80fd\u662f\u5728\u522b\u7684\u6587\u4ef6\u4e2d)\u3002\u8fd9\u6837\u5c31\u4f1a\u4f7f\u4f60\u7684\u7a0b\u5e8f\u96be\u4ee5\u8c03\u8bd5\u3002 \u53e6\u5916\uff0c\u5b8f\u548c\u666e\u901a\u4ee3\u7801\u53ef\u80fd\u4f1a\u4ea7\u751f\u547d\u540d\u51b2\u7a81\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 #include \"someheader.h\" #include int main () { int beta { 5 }; std :: cout << beta ; return 0 ; } \u5982\u679c someheader.h \u6070\u597d #define \u4e86\u4e00\u4e2a\u540d\u4e3a beta \u7684\u5b8f\uff0c\u90a3\u4e48\u8fd9\u4e2a\u7a0b\u5e8f\u5c31\u65e0\u6cd5\u7f16\u8bd1\uff0c\u56e0\u4e3a\u9884\u5904\u7406\u5668\u4f1a\u628a int \u53d8\u91cf\u7684\u540d\u5b57\u66ff\u6362\u6389\u3002\u901a\u5e38\uff0c\u4f7f\u7528\u5168\u5927\u5199\u7684\u5b8f\u540d\u53ef\u4ee5\u907f\u514d\u6b64\u7c7b\u95ee\u9898\uff0c\u4f46\u5e76\u65e0\u6cd5\u5b8c\u5168\u675c\u7edd\u3002 \u7b2c\u4e09\uff0c\u5b8f\u5e76\u4e0d\u9075\u5faa\u6b63\u5e38\u7684\u4f5c\u7528\u57df\u89c4\u5219\uff0c\u8fd9\u610f\u5473\u7740\u5b9a\u6781\u5c11\u6570\u60c5\u51b5\u4e0b\uff0c\u5b9a\u4e49\u5728\u51fd\u6570\u67d0\u90e8\u5206\u7684\u5b8f\u53ef\u80fd\u4f1a\u548c\u5176\u4ed6\u90e8\u5206\u7684\u4ee3\u7801\u53d1\u751f\u51b2\u7a81\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u5e38\u91cf\u800c\u4e0d\u662f\u5b8f\u66ff\u6362\u6765\u521b\u5efa\u5e38\u6570\u53d8\u91cf\u3002 \u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u5171\u7528\u7b26\u53f7\u5e38\u91cf \u5728\u5f88\u591a\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u6709\u4e9b\u7b26\u53f7\u5e38\u91cf\u9700\u8981\u88ab\u6240\u6709\u7684\u4ee3\u7801\u4f7f\u7528\uff08\u800c\u4e0d\u4ec5\u4ec5\u662f\u88ab\u5c40\u90e8\u7684\u4ee3\u7801\u4f7f\u7528\uff09\u3002\u8fd9\u4e9b\u53d8\u91cf\u53ef\u80fd\u662f\u7269\u7406\u5e38\u91cf\u6216\u6570\u5b66\u5e38\u91cf\uff08\u4f8b\u5982 \u03c0 \u6216\u963f\u4f0f\u4f3d\u5fb7\u7f57\u5e38\u6570\uff09\uff0c\u6216\u8005\u662f\u67d0\u4e2a\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u7684\u53c2\u6570\uff08\u4f8b\u5982\u6469\u64e6\u7cfb\u6570\u6216\u5f15\u529b\u7cfb\u6570\uff09\u3002\u4e0e\u5176\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u5404\u5b9a\u4e49\u4e00\u904d\u8fd9\u4e9b\u53d8\u91cf\uff0c\u4e0d\u5982\u5c06\u5b83\u4eec\u96c6\u4e2d\u5b9a\u4e49\u5728\u4e00\u4e2a\u5730\u65b9\u7136\u540e\u6309\u9700\u4f7f\u7528\u3002\u8fd9\u6837\uff0c\u4e07\u4e00\u4f60\u9700\u8981\u4fee\u6539\u5b83\u4eec\u7684\u503c\uff0c\u4f60\u53ea\u9700\u8981\u5728\u4e00\u5904\u4fee\u6539\u5373\u53ef\u3002 \u5728 C++ \u4e2d\u6709\u5f88\u591a\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u4e0a\u8ff0\u9700\u6c42\uff0c\u6211\u4eec\u4f1a\u5728 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf \u4e2d\u8fdb\u884c\u8be6\u7ec6\u7684\u4ecb\u7ecd\u3002","title":"4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf"},{"location":"4-13-Const-variables-and-symbolic-constants/#const","text":"\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u770b\u5230\u7684\u6240\u6709\u53d8\u91cf\u90fd\u662f\u975e\u5e38\u91cf\u2014\u2014\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4eec\u7684\u503c\u53ef\u4ee5\u5728\u4efb\u4f55\u65f6\u5019\u66f4\u6539(\u901a\u5e38\u901a\u8fc7\u8d4b\u503c)\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 int main () { int x { 4 }; // x is a non-constant variable x = 5 ; // change value of x to 5 using assignment operator return 0 ; } \u4e0d\u8fc7\uff0c\u6709\u65f6\u5019\u9700\u8981\u5c06\u53d8\u91cf\u5b9a\u4e49\u4e3a\u4e0d\u80fd\u6539\u53d8\u3002\u4f8b\u5982\uff0c\u5730\u7403\u7684\u5f15\u529b\u662f 9.8 meters/second^2 \uff0c\u8fd9\u4e2a\u503c\u4e0d\u592a\u53ef\u80fd\u4f1a\u968f\u65f6\u6539\u53d8\uff08\u5982\u679c\u771f\u7684\u4f1a\u968f\u65f6\u6539\u53d8\u7684\u8bdd\uff0c\u4f60\u5e94\u8be5\u62c5\u5fc3\u7684\u5c31\u4e0d\u662f C++\u4e86\uff09\u3002\u5c06\u8be5\u503c\u5b9a\u4e49\u4e3a\u5e38\u91cf\u6709\u52a9\u4e8e\u786e\u4fdd\u8be5\u503c\u4e0d\u4f1a\u610f\u5916\u66f4\u6539\u3002\u5e38\u91cf\u8fd8\u6709\u5176\u4ed6\u597d\u5904\uff0c\u6211\u4eec\u7a0d\u540e\u5c06\u63a2\u8ba8\u3002 \u503c\u4e0d\u80fd\u4fee\u6539\u7684\u53d8\u91cf\u79f0\u4e3a\u5e38\u6570\u53d8\u91cf\u3002","title":"const \u53d8\u91cf"},{"location":"4-13-Const-variables-and-symbolic-constants/#const_1","text":"\u5c06\u53d8\u91cf\u5b9a\u4e49\u4e3a\u5e38\u91cf\uff0c\u53ea\u9700\u8981\u5728\u7c7b\u578b\u524d\u9762\u6216\u540e\u9762\u6dfb\u52a0 const \u5173\u952e\u5b57\uff0c\u4f8b\u5982\uff1a 1 2 const double gravity { 9.8 }; // preferred use of const before type int const sidesInSquare { 4 }; // \"east const\" style, okay but not preferred \u5c3d\u7ba1 C++ \u5141\u8bb8\u4f60\u5728\u7c7b\u578b\u524d\u9762\u6216\u8005\u540e\u9762\u6dfb\u52a0 const \u5173\u952e\u5b57\uff0c\u6211\u4eec\u8fd8\u662f\u63a8\u8350\u4f60\u628a\u5b83\u653e\u5728\u7c7b\u578b\u524d\u9762\uff0c\u8fd9\u6837\u770b\u4e0a\u53bb\u66f4\u50cf\u662f\u6b63\u5e38\u7684\u82f1\u8bed\u8bed\u6cd5\uff08\u4f8b\u5982\uff1a\u201ca green ball\u201d \u800c\u4e0d\u662f \u201ca ball green\u201d\uff09\u3002 \u9898\u5916\u8bdd \u57fa\u4e8e\u7f16\u8bd1\u5668\u89e3\u6790\u590d\u6742\u58f0\u660e\u7684\u65b9\u5f0f\uff0c\u4e00\u4e9b\u5f00\u53d1\u4eba\u5458\u66f4\u559c\u6b22\u5c06 const \u653e\u5728\u7c7b\u578b\u4e4b\u540e(\u66f4\u4e00\u81f4)\u3002\u8fd9\u79cd\u98ce\u683c\u88ab\u79f0\u4e3a\u201ceast const\u201d\u3002\u867d\u7136\u8fd9\u79cd\u98ce\u683c\u6709\u4e00\u4e9b\u62e5\u62a4\u8005(\u4e5f\u6709\u4e00\u4e9b\u5408\u7406\u7684\u89c2\u70b9)\uff0c\u4f46\u5b83\u5e76\u6ca1\u6709\u5927\u53d7\u6b22\u8fce\u3002 \u6700\u4f73\u5b9e\u8df5 \u628a const \u653e\u7f6e\u5728\u7c7b\u578b\u524d\uff08\u66f4\u7b26\u5408\u4e60\u60ef\u7684\u505a\u6cd5\uff09\u3002","title":"const \u5173\u952e\u5b57"},{"location":"4-13-Const-variables-and-symbolic-constants/#const_2","text":"const \u53d8\u91cf \u5fc5\u987b \u5728\u5b9a\u4e49\u65f6\u521d\u59cb\u5316\uff0c\u6b64\u540e\u4f60\u4e5f\u4e0d\u80fd\u901a\u8fc7\u8d4b\u503c\u6765\u6539\u53d8\u5b83\uff1a 1 2 3 4 5 6 7 int main () { const double gravity ; // error: const variables must be initialized gravity = 9.9 ; // error: const variables can not be changed return 0 ; } \u6ce8\u610f\uff0cconst \u53d8\u91cf\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u7c7b\u578b\u7684\u53d8\u91cf\u521d\u59cb\u5316\uff08\u5305\u62ec\u975e const \u7c7b\u578b\u7684\u53d8\u91cf\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << \"Enter your age: \" ; int age {}; std :: cin >> age ; const int constAge { age }; // initialize const variable using non-const value age = 5 ; // ok: age is non-const, so we can change its value constAge = 6 ; // error: constAge is const, so we cannot change its value return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u7528\u975e const \u53d8\u91cf age \u521d\u59cb\u5316 const \u53d8\u91cf constAge \u3002\u56e0\u4e3a age \u4ecd\u7136\u662f\u975e const \u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u6539\u53d8\u5b83\u7684\u503c\u3002\u4f46\u662f\uff0c\u7531\u4e8e constAge \u662f const \uff0c\u6211\u4eec\u4e0d\u80fd\u5728\u521d\u59cb\u5316\u540e\u66f4\u6539\u5b83\u7684\u503c\u3002","title":"Const \u53d8\u91cf\u5fc5\u987b\u521d\u59cb\u5316"},{"location":"4-13-Const-variables-and-symbolic-constants/#const_3","text":"const \u53d8\u91cf\u7684\u547d\u540d\u6709\u5f88\u591a\u4e0d\u540c\u7684\u4e60\u60ef\u3002 \u4ece C \u8bed\u8a00\u8fc7\u6765\u7684\u7a0b\u5e8f\u5458\u559c\u6b22\u4f7f\u7528\u4e0b\u5212\u7ebf\u3001\u5168\u5927\u5199\u5b57\u6bcd\u7684\u65b9\u5f0f\u547d\u540d const \u53d8\u91cf \uff08\u4f8b\u5982 EARTH_GRAVITY \uff09\uff0c\u800c C++ \u4e2d\u5219\u591a\u4f7f\u7528\u5927\u5c0f\u5199\u4ea4\u66ff\u7684\u65b9\u5f0f\uff0c\u540c\u65f6\u6dfb\u52a0 k \u4f5c\u4e3a\u524d\u7f00(\u4f8b\u5982 kEarthGravity )\u3002 \u4e0d\u8fc7\uff0c\u7531\u4e8e const \u53d8\u91cf\u7684\u884c\u4e3a\u548c\u666e\u901a\u53d8\u91cf\u6ca1\u4ec0\u4e48\u533a\u522b\uff08\u9664\u4e86\u4e0d\u80fd\u8d4b\u503c\u4ee5\u5916\uff09\uff0c\u6240\u4ee5\u6ca1\u5fc5\u8981\u4e13\u95e8\u4e3a\u5b83\u4f7f\u7528\u4e00\u79cd\u7279\u6b8a\u5f62\u5f0f\u7684\u547d\u540d\u65b9\u5f0f\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5efa\u8bae\u4f7f\u7528\u548c\u975e const \u7c7b\u578b\u4e00\u6837\u7684\u53d8\u91cf\u540d\uff08\u4f8b\u5982 earthGravity \uff09\u3002","title":"const \u53d8\u91cf\u547d\u540d"},{"location":"4-13-Const-variables-and-symbolic-constants/#const_4","text":"\u51fd\u6570\u7684 \u5f62\u53c2 \u4e5f\u53ef\u4ee5\u901a\u8fc7 const \u5173\u952e\u5b57\u5b9a\u4e49\u4e3a\u5e38\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include void printInt ( const int x ) { std :: cout << x ; } int main () { printInt ( 5 ); // 5 will be used as the initializer for x printInt ( 6 ); // 6 will be used as the initializer for x return 0 ; } \u6ce8\u610f\uff0c\u6211\u4eec\u6ca1\u6709\u4e3a const \u5f62\u53c2 x \u63d0\u4f9b\u663e\u5f0f\u7684\u521d\u59cb\u5316\u503c\u2014\u2014\u51fd\u6570\u8c03\u7528\u4e2d\u7684\u5b9e\u53c2\u503c\u5c06\u88ab\u7528\u4f5c x \u7684\u521d\u59cb\u5316\u5f0f\u3002 \u5c06\u51fd\u6570\u5f62\u53c2\u8bbe\u4e3a\u5e38\u91cf\u53ef\u4ee5\u5f97\u5230\u7f16\u8bd1\u5668\u7684\u5e2e\u52a9\uff0c\u4ee5\u786e\u4fdd\u5f62\u53c2\u7684\u503c\u4e0d\u4f1a\u5728\u51fd\u6570\u5185\u90e8\u88ab\u66f4\u6539\u3002\u7136\u800c\uff0c\u5f53\u5b9e\u53c2\u901a\u8fc7 \u6309\u503c\u4f20\u9012 \u65f6\uff0c\u6211\u4eec\u901a\u5e38\u4e0d\u5173\u5fc3\u51fd\u6570\u662f\u5426\u6539\u53d8\u4e86\u5f62\u53c2\u7684\u503c(\u56e0\u4e3a\u5b83\u53ea\u662f\u4e00\u4e2a\u526f\u672c\uff0c\u65e0\u8bba\u5982\u4f55\u90fd\u4f1a\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u9500\u6bc1)\u3002\u7531\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u6211\u4eec\u901a\u5e38\u4e0d\u4f7f\u7528 const \u5f62\u53c2\u6765\u4f20\u9012\u503c(\u56e0\u4e3a\u5b83\u4f1a\u7ed9\u6211\u4eec\u7684\u4ee3\u7801\u589e\u52a0\u6df7\u4e71\uff0c\u800c\u4e0d\u4f1a\u63d0\u4f9b\u592a\u591a\u7684\u5b9e\u9645\u4ef7\u503c)\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728 \u6309\u503c\u4f20\u9012 \u65f6\u4e0d\u8981\u4f7f\u7528 const \u3002 \u5728\u672c\u7cfb\u5217\u6559\u7a0b\u7684\u540e\u9762\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5411\u51fd\u6570\u4f20\u9012\u53c2\u6570\u7684\u53e6\u5916\u4e24\u79cd\u65b9\u6cd5\uff1a \u4f20\u5f15\u7528 \u548c \u4f20\u5730\u5740 \u3002\u5728\u4f7f\u7528\u8fd9\u4e24\u79cd\u65b9\u6cd5\u65f6\uff0c\u6b63\u786e\u4f7f\u7528 const \u975e\u5e38\u91cd\u8981\u3002","title":"Const \u51fd\u6570\u5f62\u53c2"},{"location":"4-13-Const-variables-and-symbolic-constants/#const_5","text":"\u51fd\u6570\u7684\u8fd4\u56de\u503c\u540c\u6837\u53ef\u4ee5\u662f const \u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include const int getValue () { return 5 ; } int main () { std :: cout << getValue (); return 0 ; } \u7136\u800c\uff0c\u7531\u4e8e\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u526f\u672c\uff0c\u5c06\u5176\u8bbe\u4e3a const \u6ca1\u6709\u4ec0\u4e48\u610f\u4e49\u3002\u8fd4\u56de const \u503c\u8fd8\u53ef\u80fd\u59a8\u788d\u67d0\u4e9b\u7c7b\u578b\u7684\u7f16\u8bd1\u5668\u4f18\u5316\uff0c\u4ece\u800c\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e0d\u8981\u4f7f\u7528 const \u7c7b\u578b\u7684\u8fd4\u56de\u503c\u3002","title":"Const \u7c7b\u578b\u8fd4\u56de\u503c"},{"location":"4-13-Const-variables-and-symbolic-constants/#_1","text":"\u7b26\u53f7\u5e38\u6570\u662f\u8d4b\u4e88\u5e38\u6570\u503c\u7684\u540d\u79f0\u3002\u5e38\u91cf\u53d8\u91cf\u662f\u7b26\u53f7\u5e38\u91cf\u7684\u4e00\u79cd\uff0c\u56e0\u4e3a\u53d8\u91cf\u6709\u4e00\u4e2a\u540d\u79f0(\u5b83\u7684\u6807\u8bc6\u7b26)\u548c\u4e00\u4e2a\u5e38\u91cf\u503c\u3002 \u5728 2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86 \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f \u6709\u4e24\u79cd\u5f62\u5f0f\u2014\u2014\u4e00\u79cd\u7528\u4e8e\u66ff\u6362\uff0c\u4e00\u79cd\u4e0d\u7528\u4e8e\u66ff\u6362\u3002\u8fd9\u91cc\u6211\u4eec\u4f1a\u8ba8\u8bba\u4e00\u4e0b\u7528\u4e8e\u66ff\u6362\u7684\u5b8f\uff0c\u5b83\u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 `#define` identifier substitution_text \u6bcf\u5f53 \u9884\u5904\u7406\u5668 \u9047\u5230\u8be5\u6307\u4ee4\u65f6\uff0c\u540e\u7eed\u6240\u6709 identifier \u90fd\u4f1a\u88ab\u66ff\u6362\u4e3a substitution_text \u3002\u8fd9\u91cc\u7684 identifier \u901a\u5e38\u4f1a\u4f7f\u7528\u5168\u5927\u5199\u5f62\u5f0f\u5e76\u4f7f\u7528\u4e0b\u5212\u7ebf\u4ee3\u66ff\u7a7a\u683c\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 #include #define MAX_STUDENTS_PER_CLASS 30 int main () { std :: cout << \"The class has \" << MAX_STUDENTS_PER_CLASS << \" students. \\n \" ; return 0 ; } \u5f53\u4f60\u7f16\u8bd1\u4ee3\u7801\u65f6\uff0c\u9884\u5904\u7406\u5668\u5c31\u4f1a\u628a MAX_STUDENTS_PER_CLASS \u66ff\u6362\u4e3a\u5b57\u9762\u91cf 30\uff0c\u7136\u540e\u88ab\u7f16\u8bd1\u5230\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\u3002 \u56e0\u4e3a\u5b8f\u662f\u6709\u540d\u5b57\u7684\uff0c\u800c\u4e14\u5b83\u7684\u66ff\u6362\u6587\u672c\u662f\u4e00\u4e2a\u5e38\u91cf\uff0c\u6240\u4ee5\u5b83\u4e5f\u5c5e\u4e8e\u7b26\u53f7\u5e38\u91cf\u7684\u4e00\u79cd\u3002","title":"\u4ec0\u4e48\u662f\u7b26\u53f7\u5e38\u91cf\uff1f"},{"location":"4-13-Const-variables-and-symbolic-constants/#_2","text":"\u6240\u4ee5\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u7528 #define \u5b9a\u4e49\u7b26\u53f7\u5e38\u91cf\u5462\uff1f\u8fd9\u91cc\u6709\uff08\u81f3\u5c11\uff09\u4e09\u4e2a\u4e3b\u8981\u95ee\u9898\u3002 \u9996\u5148\uff0c\u56e0\u4e3a\u5b8f\u7684\u89e3\u6790\u662f\u9884\u5904\u7406\u5668\u8d1f\u8d23\u7684\uff0c\u6240\u4ee5\u6240\u6709\u7684\u66ff\u6362\u90fd\u53d1\u751f\u5728\u7f16\u8bd1\u4e4b\u524d\u3002\u5f53\u4f60\u8c03\u8bd5\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u4f60\u65e0\u6cd5\u770b\u5230\u5b9e\u9645\u7684\u503c\uff08\u4f8b\u5982 30\uff09\uff0c\u800c\u53ea\u80fd\u770b\u5230\u8be5\u7b26\u53f7\u5e38\u91cf\u7684\u540d\u5b57(\u4f8b\u5982 MAX_STUDENTS_PER_CLASS )\u3002\u800c\u4e14\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u5b8f\u5b9a\u4e49\u5e76\u4e0d\u662f\u53d8\u91cf\uff0c\u6240\u4ee5\u518d\u8c03\u8bd5\u5668\u4e2d\u4f60\u6ca1\u6cd5\u5bf9\u5176\u503c\u8fdb\u884c\u76d1\u63a7\u3002 \u5982\u679c\u4f60\u60f3\u8981\u6307\u5b9a MAX_STUDENTS_PER_CLASS \u89e3\u6790\u540e\u7684\u503c\u662f\u591a\u5c11\uff0c\u4f60\u5fc5\u987b\u53d6\u627e\u5230 MAX_STUDENTS_PER_CLASS \u7684\u5b9a\u4e49\u624d\u884c(\u8be5\u5b9a\u4e49\u8fd8\u53ef\u80fd\u662f\u5728\u522b\u7684\u6587\u4ef6\u4e2d)\u3002\u8fd9\u6837\u5c31\u4f1a\u4f7f\u4f60\u7684\u7a0b\u5e8f\u96be\u4ee5\u8c03\u8bd5\u3002 \u53e6\u5916\uff0c\u5b8f\u548c\u666e\u901a\u4ee3\u7801\u53ef\u80fd\u4f1a\u4ea7\u751f\u547d\u540d\u51b2\u7a81\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 #include \"someheader.h\" #include int main () { int beta { 5 }; std :: cout << beta ; return 0 ; } \u5982\u679c someheader.h \u6070\u597d #define \u4e86\u4e00\u4e2a\u540d\u4e3a beta \u7684\u5b8f\uff0c\u90a3\u4e48\u8fd9\u4e2a\u7a0b\u5e8f\u5c31\u65e0\u6cd5\u7f16\u8bd1\uff0c\u56e0\u4e3a\u9884\u5904\u7406\u5668\u4f1a\u628a int \u53d8\u91cf\u7684\u540d\u5b57\u66ff\u6362\u6389\u3002\u901a\u5e38\uff0c\u4f7f\u7528\u5168\u5927\u5199\u7684\u5b8f\u540d\u53ef\u4ee5\u907f\u514d\u6b64\u7c7b\u95ee\u9898\uff0c\u4f46\u5e76\u65e0\u6cd5\u5b8c\u5168\u675c\u7edd\u3002 \u7b2c\u4e09\uff0c\u5b8f\u5e76\u4e0d\u9075\u5faa\u6b63\u5e38\u7684\u4f5c\u7528\u57df\u89c4\u5219\uff0c\u8fd9\u610f\u5473\u7740\u5b9a\u6781\u5c11\u6570\u60c5\u51b5\u4e0b\uff0c\u5b9a\u4e49\u5728\u51fd\u6570\u67d0\u90e8\u5206\u7684\u5b8f\u53ef\u80fd\u4f1a\u548c\u5176\u4ed6\u90e8\u5206\u7684\u4ee3\u7801\u53d1\u751f\u51b2\u7a81\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u5e38\u91cf\u800c\u4e0d\u662f\u5b8f\u66ff\u6362\u6765\u521b\u5efa\u5e38\u6570\u53d8\u91cf\u3002","title":"\u907f\u514d\u5c06\u5bf9\u8c61\u5f62\u5f0f\u7684\u9884\u5904\u7406\u5668\u5b8f\u7528\u4e8e\u7b26\u53f7\u5e38\u91cf"},{"location":"4-13-Const-variables-and-symbolic-constants/#_3","text":"\u5728\u5f88\u591a\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u6709\u4e9b\u7b26\u53f7\u5e38\u91cf\u9700\u8981\u88ab\u6240\u6709\u7684\u4ee3\u7801\u4f7f\u7528\uff08\u800c\u4e0d\u4ec5\u4ec5\u662f\u88ab\u5c40\u90e8\u7684\u4ee3\u7801\u4f7f\u7528\uff09\u3002\u8fd9\u4e9b\u53d8\u91cf\u53ef\u80fd\u662f\u7269\u7406\u5e38\u91cf\u6216\u6570\u5b66\u5e38\u91cf\uff08\u4f8b\u5982 \u03c0 \u6216\u963f\u4f0f\u4f3d\u5fb7\u7f57\u5e38\u6570\uff09\uff0c\u6216\u8005\u662f\u67d0\u4e2a\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u7684\u53c2\u6570\uff08\u4f8b\u5982\u6469\u64e6\u7cfb\u6570\u6216\u5f15\u529b\u7cfb\u6570\uff09\u3002\u4e0e\u5176\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u5404\u5b9a\u4e49\u4e00\u904d\u8fd9\u4e9b\u53d8\u91cf\uff0c\u4e0d\u5982\u5c06\u5b83\u4eec\u96c6\u4e2d\u5b9a\u4e49\u5728\u4e00\u4e2a\u5730\u65b9\u7136\u540e\u6309\u9700\u4f7f\u7528\u3002\u8fd9\u6837\uff0c\u4e07\u4e00\u4f60\u9700\u8981\u4fee\u6539\u5b83\u4eec\u7684\u503c\uff0c\u4f60\u53ea\u9700\u8981\u5728\u4e00\u5904\u4fee\u6539\u5373\u53ef\u3002 \u5728 C++ \u4e2d\u6709\u5f88\u591a\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u4e0a\u8ff0\u9700\u6c42\uff0c\u6211\u4eec\u4f1a\u5728 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf \u4e2d\u8fdb\u884c\u8be6\u7ec6\u7684\u4ecb\u7ecd\u3002","title":"\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u5171\u7528\u7b26\u53f7\u5e38\u91cf"},{"location":"4-14-Compile-time-constants-constant-expressions-and-constexpr/","tags":["const","constexpr"],"text":"Key Takeaway \u8003\u8651\u4e0b\u9762\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << 3 + 4 ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 7 \u4e0d\u8fc7\uff0c\u8fd9\u91cc\u5305\u542b\u4e86\u4e00\u4e2a\u4e0d\u6613\u88ab\u53d1\u73b0\u7684\u4f18\u5316\u70b9\u3002 \u5982\u679c\u5bf9\u4e0a\u8ff0\u6587\u4ef6\u539f\u5c01\u4e0d\u52a8\u5730\u8fdb\u884c\u7f16\u8bd1\uff0c\u7f16\u8bd1\u5668\u5c06\u751f\u6210\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u5e76\u5728\u8fd0\u884c\u65f6(\u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6)\u8ba1\u7b97 3 + 4 \u7684\u7ed3\u679c\u3002\u5982\u679c\u7a0b\u5e8f\u88ab\u6267\u884c 100 \u4e07\u6b21\uff0c3 + 4 \u5c06\u88ab\u8ba1\u7b97 100 \u4e07\u6b21\uff0c7 \u7684\u7ed3\u679c\u503c\u5c06\u4ea7\u751f 100 \u4e07\u6b21\u3002\u4f46\u662f\u8bf7\u6ce8\u610f\uff0c3 + 4 \u7684\u7ed3\u679c\u6c38\u8fdc\u4e0d\u4f1a\u6539\u53d8\u2014\u2014\u5b83\u603b\u662f 7\u3002\u56e0\u6b64\uff0c\u6bcf\u6b21\u7a0b\u5e8f\u8fd0\u884c\u65f6\u91cd\u65b0\u8ba1\u7b97 3 + 4 \u662f\u4e00\u79cd\u6d6a\u8d39\u3002 \u5e38\u91cf\u8868\u8fbe\u5f0f \u5e38\u91cf\u8868\u8fbe\u5f0f\u662f\u53ef\u4ee5\u7531\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\u8868\u8fbe\u5f0f\u3002\u8981\u6210\u4e3a\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u8868\u8fbe\u5f0f\u4e2d\u7684\u6240\u6709\u503c\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u5df2\u77e5(\u8c03\u7528\u7684\u6240\u6709\u64cd\u4f5c\u7b26\u548c\u51fd\u6570\u5fc5\u987b\u652f\u6301\u7f16\u8bd1\u65f6\u6c42\u503c)\u3002 \u5f53\u7f16\u8bd1\u5668\u9047\u5230\u5e38\u91cf\u8868\u8fbe\u5f0f\u65f6\uff0c\u5b83\u5c06\u7528\u8be5\u5e38\u91cf\u8868\u8fbe\u5f0f\u7684\u6c42\u503c\u7ed3\u679c\u66ff\u6362\u8be5\u5e38\u91cf\u8868\u8fbe\u5f0f\u3002 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u8868\u8fbe\u5f0f 3 + 4 \u662f\u4e00\u4e2a\u5e38\u91cf\u8868\u8fbe\u5f0f\u3002\u56e0\u6b64\uff0c\u5f53\u8fd9\u4e2a\u7a0b\u5e8f\u88ab\u7f16\u8bd1\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u8ba1\u7b97\u5e38\u6570\u8868\u8fbe\u5f0f 3 + 4 \uff0c\u7136\u540e\u5c06\u5e38\u6570\u8868\u8fbe\u5f0f 3 + 4 \u66ff\u6362\u4e3a\u7ed3\u679c\u503c 7 \u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u7f16\u8bd1\u5668\u5b9e\u9645\u4e0a\u7f16\u8bd1\u7684\u662f\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << 7 ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u4ea7\u751f\u76f8\u540c\u7684\u8f93\u51fa( 7 )\uff0c\u4f46\u6700\u7ec8\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4e0d\u518d\u9700\u8981\u5728\u8fd0\u884c\u65f6\u82b1\u8d39 CPU \u5468\u671f\u8ba1\u7b97 3 + 4 ! \u6ce8\u610f\uff0c\u66ff\u6362\u540e\u7684\u8868\u8fbe\u5f0f std:: cout << 7 \u4e0d\u662f\u4e00\u4e2a\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u56e0\u4e3a\u6211\u4eec\u7684\u7a0b\u5e8f\u4e0d\u80fd\u5728\u7f16\u8bd1\u65f6\u5c06\u503c\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002\u6240\u4ee5\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4f1a\u5728\u8fd0\u884c\u65f6\u6c42\u503c\u3002 \u5173\u952e\u4fe1\u606f \u5728\u7f16\u8bd1\u65f6\u5bf9\u5e38\u91cf\u8868\u8fbe\u5f0f\u6c42\u503c\u4f1a\u4f7f\u7f16\u8bd1\u82b1\u8d39\u66f4\u957f\u7684\u65f6\u95f4(\u56e0\u4e3a\u7f16\u8bd1\u5668\u5fc5\u987b\u505a\u66f4\u591a\u7684\u5de5\u4f5c)\uff0c\u4f46\u662f\u8fd9\u6837\u7684\u8868\u8fbe\u5f0f\u53ea\u9700\u8981\u6c42\u503c\u4e00\u6b21(\u800c\u4e0d\u662f\u6bcf\u6b21\u7a0b\u5e8f\u8fd0\u884c\u65f6)\u3002\u5f97\u5230\u7684\u53ef\u6267\u884c\u6587\u4ef6\u901f\u5ea6\u66f4\u5feb\uff0c\u4f7f\u7528\u7684\u5185\u5b58\u66f4\u5c11\u3002 \u7f16\u8bd1\u65f6\u5e38\u6570 \u7f16\u8bd1\u65f6\u5e38\u6570\u662f\u4e00\u4e2a\u5728\u7f16\u8bd1\u65f6\u5176\u503c\u5df2\u77e5\u7684\u5e38\u6570\u3002\u5b57\u9762\u91cf(\u4f8b\u5982 1 \uff0c 2.3 \u548c \"Hello, world!\" )\u662f\u4e00\u79cd\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 \u90a3\u4e48 const \u53d8\u91cf\u5462\uff1f const \u53d8\u91cf\u53ef\u80fd\u662f\u4e5f\u53ef\u80fd\u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 \u7f16\u8bd1\u65f6\u5e38\u91cf( const ) \u5982\u679c const \u53d8\u91cf\u7684\u521d\u59cb\u5316\u503c\u662f\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u90a3\u4e48\u5b83\u5c31\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 \u8003\u8651\u4e00\u4e2a\u4e0e\u4e0a\u9762\u7c7b\u4f3c\u7684\u4f7f\u7528 const \u53d8\u91cf\u7684\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 #include int main () { const int x { 3 }; // x \u662f\u7f16\u8bd1\u65f6\u5e38\u91cf const int y { 4 }; // y \u662f\u7f16\u8bd1\u65f6\u5e38\u91cf std :: cout << x + y ; // x + y \u662f\u7f16\u8bd1\u65f6\u5e38\u91cf return 0 ; } \u56e0\u4e3a x \u548c y \u7684\u521d\u59cb\u5316\u503c\u662f\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u6240\u4ee5 x \u548c y \u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u8fd9\u610f\u5473\u7740 x + y \u4e5f\u662f\u4e00\u4e2a\u5e38\u6570\u8868\u8fbe\u5f0f\u3002\u56e0\u6b64\uff0c\u5f53\u7f16\u8bd1\u5668\u7f16\u8bd1\u8fd9\u4e2a\u7a0b\u5e8f\u65f6\uff0c\u5b83\u53ef\u4ee5\u8ba1\u7b97 x + y \u7684\u503c\uff0c\u5e76\u5c06\u5e38\u91cf\u8868\u8fbe\u5f0f\u66ff\u6362\u4e3a\u7ed3\u679c\u5b57\u9762\u503c 7 \u3002 \u8bf7\u6ce8\u610f\uff0c\u7f16\u8bd1\u65f6 const \u7684\u521d\u59cb\u5316\u5f0f\u53ef\u4ee5\u662f\u4efb\u4f55\u5e38\u91cf\u8868\u8fbe\u5f0f\u3002\u4ee5\u4e0b\u4e24\u4e2a\u90fd\u662f\u7f16\u8bd1\u65f6\u7684 const \u53d8\u91cf: 1 2 const int z { 1 + 2 }; const int w { z * 2 }; \u7f16\u8bd1\u65f6\u7684 const \u53d8\u91cf\u901a\u5e38\u88ab\u7528\u4f5c\u7b26\u53f7\u5e38\u91cf: 1 const double gravity { 9.8 }; \u7f16\u8bd1\u65f6\u5e38\u91cf\u4f7f\u7f16\u8bd1\u5668\u80fd\u591f\u6267\u884c\u975e\u7f16\u8bd1\u65f6\u5e38\u91cf\u65e0\u6cd5\u63d0\u4f9b\u7684\u4f18\u5316\u3002\u4f8b\u5982\uff0c\u6bcf\u5f53\u4f7f\u7528 gravity \u65f6\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u7b80\u5355\u5730\u7528\u53cc\u7cbe\u5ea6\u5b57\u9762\u503c 9.8 \u66ff\u6362\u6807\u8bc6\u7b26 gravity \uff0c\u8fd9\u5c31\u907f\u514d\u4e86\u5fc5\u987b\u4ece\u5185\u5b58\u4e2d\u67d0\u5904\u83b7\u53d6\u503c\u3002 \u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u65f6\u5e38\u91cf\u5c06\u88ab \u4f18\u5316\u6389 \u3002\u5728\u65e0\u6cd5\u5b9e\u73b0\u7684\u60c5\u51b5\u4e0b(\u6216\u5f53\u4f18\u5316\u5173\u95ed\u65f6)\uff0c\u53d8\u91cf\u4ecd\u7136\u4f1a\u5728\u8fd0\u884c\u65f6\u521b\u5efa(\u548c\u521d\u59cb\u5316)\u3002 \u8fd0\u884c\u65f6\u5e38\u91cf( const ) \u4efb\u4f55\u7528\u975e\u5e38\u91cf\u8868\u8fbe\u5f0f\u521d\u59cb\u5316\u7684 const \u53d8\u91cf\u90fd\u662f\u8fd0\u884c\u65f6\u5e38\u91cf\u3002\u8fd0\u884c\u65f6\u5e38\u91cf\u662f\u5728\u8fd0\u884c\u65f6\u624d\u77e5\u9053\u5176\u521d\u59cb\u5316\u503c\u7684\u5e38\u91cf\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u6f14\u793a\u4e86\u8fd0\u884c\u65f6\u5e38\u91cf\u7684\u7528\u6cd5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int getNumber () { std :: cout << \"Enter a number: \" ; int y {}; std :: cin >> y ; return y ; } int main () { const int x { 3 }; // x is a compile time constant std :: cout << \"Enter a number: \" ; const int y { getNumber () }; // y is a runtime constant std :: cout << x + y ; // x + y is a runtime expression return 0 ; } \u5373\u4f7f y \u662f const \uff0c\u5176\u521d\u59cb\u5316\u503c( getNumber() \u7684\u8fd4\u56de\u503c)\u8981\u5230\u8fd0\u884c\u65f6\u624d\u77e5\u9053\u3002\u56e0\u6b64\uff0c y \u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\u5e38\u91cf\uff0c\u800c\u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u56e0\u6b64\uff0c\u8868\u8fbe\u5f0f x + y \u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\u8868\u8fbe\u5f0f\u3002 constexpr \u5173\u952e\u5b57 \u5f53\u58f0\u660e const \u53d8\u91cf\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u9690\u5f0f\u5730\u8ddf\u8e2a\u5b83\u662f\u8fd0\u884c\u65f6\u5e38\u91cf\u8fd8\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u9664\u4e86\u4f18\u5316\u76ee\u7684\u4e4b\u5916\uff0c\u662f\u8fd0\u884c\u65f6\u5e38\u91cf\u8fd8\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u5e76\u4e0d\u91cd\u8981\uff0c\u4f46\u4e5f\u6709\u4e00\u4e9b\u5947\u602a\u7684\u60c5\u51b5\u4e0b\uff0cC++\u9700\u8981\u7f16\u8bd1\u65f6\u5e38\u91cf\u800c\u4e0d\u662f\u8fd0\u884c\u65f6\u5e38\u91cf(\u6211\u4eec\u5c06\u5728\u7a0d\u540e\u4ecb\u7ecd\u8fd9\u4e9b\u4e3b\u9898\u65f6\u8ba8\u8bba\u8fd9\u4e9b\u60c5\u51b5)\u3002 \u56e0\u4e3a\u7f16\u8bd1\u65f6\u5e38\u91cf\u901a\u5e38\u5141\u8bb8\u66f4\u597d\u7684\u4f18\u5316(\u5e76\u4e14\u51e0\u4e4e\u6ca1\u6709\u7f3a\u70b9)\uff0c\u6240\u4ee5\u6211\u4eec\u901a\u5e38\u5e0c\u671b\u5c3d\u53ef\u80fd\u4f7f\u7528\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 \u5f53\u4f7f\u7528 const \u65f6\uff0c\u53d8\u91cf\u6700\u7ec8\u53ef\u80fd\u662f\u7f16\u8bd1\u65f6\u7684 const \u6216\u8fd0\u884c\u65f6\u7684 const \uff0c\u8fd9\u53d6\u51b3\u4e8e\u521d\u59cb\u5316\u5f0f\u662f\u5426\u662f\u7f16\u8bd1\u65f6\u8868\u8fbe\u5f0f\u3002\u56e0\u4e3a\u4e24\u8005\u7684\u5b9a\u4e49\u770b\u8d77\u6765\u5b8c\u5168\u76f8\u540c\uff0c\u6240\u4ee5\u6211\u4eec\u6700\u7ec8\u5f97\u5230\u7684\u53ef\u80fd\u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\u5e38\u91cf\uff0c\u800c\u6211\u4eec\u539f\u4ee5\u4e3a\u5f97\u5230\u7684\u662f\u4e00\u4e2a\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u5728\u524d\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f88\u96be\u533a\u5206 y \u662f\u7f16\u8bd1\u65f6\u7684 const \u8fd8\u662f\u8fd0\u884c\u65f6\u7684 const \u2014\u2014\u6211\u4eec\u5fc5\u987b\u67e5\u770b getNumber() \u7684\u8fd4\u56de\u503c\u6765\u786e\u5b9a\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u7f16\u8bd1\u5668\u7684\u5e2e\u52a9\uff0c\u4ee5\u786e\u4fdd\u5728\u9884\u671f\u7684\u5730\u65b9\u5f97\u5230\u7f16\u8bd1\u65f6\u7684 const \u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5728\u53d8\u91cf\u58f0\u660e\u4e2d\u4f7f\u7528 constexpr \u800c\u4e0d\u662f const \u3002 constexpr (\u201c\u5e38\u91cf\u8868\u8fbe\u5f0f\u201d\u7684\u7f29\u5199)\u53d8\u91cf\u53ea\u80fd\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u5982\u679c constexpr \u53d8\u91cf\u7684\u521d\u59cb\u5316\u503c\u4e0d\u662f\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u7f16\u8bd1\u5668\u5c06\u51fa\u9519\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { constexpr double gravity { 9.8 }; // ok: 9.8 is a constant expression constexpr int sum { 4 + 5 }; // ok: 4 + 5 is a constant expression constexpr int something { sum }; // ok: sum is a constant expression std :: cout << \"Enter your age: \" ; int age {}; std :: cin >> age ; constexpr int myAge { age }; // compile error: age is not a constant expression return 0 ; } \u6700\u4f73\u5b9e\u8df5 \u4efb\u4f55\u5728\u521d\u59cb\u5316\u540e\u503c\u5c31\u4e0d\u80fd\u88ab\u6539\u53d8\uff0c\u4e14\u521d\u59cb\u5316\u503c\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u786e\u5b9a\u7684\u53d8\u91cf\uff0c\u90fd\u5fc5\u987b\u58f0\u660e\u4e3a constexpr \u3002 \u4efb\u4f55\u5728\u521d\u59cb\u5316\u540e\u503c\u5c31\u4e0d\u80fd\u88ab\u6539\u53d8\uff0c\u4f46\u662f\u521d\u59cb\u5316\u503c\u4e0d\u80fd\u5728\u7f16\u8bd1\u65f6\u786e\u5b9a\u7684\u53d8\u91cf\uff0c\u90fd\u5e94\u8be5\u58f0\u660e\u4e3a const \u3002 Although function parameters can be const , function parameters cannot be constexpr . \u76f8\u5173\u5185\u5bb9 C++ \u4e5f\u652f\u6301\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\u51fd\u6570(\u56e0\u6b64\u53ef\u4ee5\u5728\u5e38\u91cf\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528)\u2014\u2014\u6211\u4eec\u4f1a\u5728 6.14 - Constexpr \u548c consteval \u51fd\u6570 \u4e2d\u8ba8\u8bba\u8fd9\u4e9b\u51fd\u6570\u3002","title":"4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr"},{"location":"4-14-Compile-time-constants-constant-expressions-and-constexpr/#_1","text":"\u5e38\u91cf\u8868\u8fbe\u5f0f\u662f\u53ef\u4ee5\u7531\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\u8868\u8fbe\u5f0f\u3002\u8981\u6210\u4e3a\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u8868\u8fbe\u5f0f\u4e2d\u7684\u6240\u6709\u503c\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u5df2\u77e5(\u8c03\u7528\u7684\u6240\u6709\u64cd\u4f5c\u7b26\u548c\u51fd\u6570\u5fc5\u987b\u652f\u6301\u7f16\u8bd1\u65f6\u6c42\u503c)\u3002 \u5f53\u7f16\u8bd1\u5668\u9047\u5230\u5e38\u91cf\u8868\u8fbe\u5f0f\u65f6\uff0c\u5b83\u5c06\u7528\u8be5\u5e38\u91cf\u8868\u8fbe\u5f0f\u7684\u6c42\u503c\u7ed3\u679c\u66ff\u6362\u8be5\u5e38\u91cf\u8868\u8fbe\u5f0f\u3002 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u8868\u8fbe\u5f0f 3 + 4 \u662f\u4e00\u4e2a\u5e38\u91cf\u8868\u8fbe\u5f0f\u3002\u56e0\u6b64\uff0c\u5f53\u8fd9\u4e2a\u7a0b\u5e8f\u88ab\u7f16\u8bd1\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u8ba1\u7b97\u5e38\u6570\u8868\u8fbe\u5f0f 3 + 4 \uff0c\u7136\u540e\u5c06\u5e38\u6570\u8868\u8fbe\u5f0f 3 + 4 \u66ff\u6362\u4e3a\u7ed3\u679c\u503c 7 \u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u7f16\u8bd1\u5668\u5b9e\u9645\u4e0a\u7f16\u8bd1\u7684\u662f\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << 7 ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u4ea7\u751f\u76f8\u540c\u7684\u8f93\u51fa( 7 )\uff0c\u4f46\u6700\u7ec8\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4e0d\u518d\u9700\u8981\u5728\u8fd0\u884c\u65f6\u82b1\u8d39 CPU \u5468\u671f\u8ba1\u7b97 3 + 4 ! \u6ce8\u610f\uff0c\u66ff\u6362\u540e\u7684\u8868\u8fbe\u5f0f std:: cout << 7 \u4e0d\u662f\u4e00\u4e2a\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u56e0\u4e3a\u6211\u4eec\u7684\u7a0b\u5e8f\u4e0d\u80fd\u5728\u7f16\u8bd1\u65f6\u5c06\u503c\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002\u6240\u4ee5\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4f1a\u5728\u8fd0\u884c\u65f6\u6c42\u503c\u3002 \u5173\u952e\u4fe1\u606f \u5728\u7f16\u8bd1\u65f6\u5bf9\u5e38\u91cf\u8868\u8fbe\u5f0f\u6c42\u503c\u4f1a\u4f7f\u7f16\u8bd1\u82b1\u8d39\u66f4\u957f\u7684\u65f6\u95f4(\u56e0\u4e3a\u7f16\u8bd1\u5668\u5fc5\u987b\u505a\u66f4\u591a\u7684\u5de5\u4f5c)\uff0c\u4f46\u662f\u8fd9\u6837\u7684\u8868\u8fbe\u5f0f\u53ea\u9700\u8981\u6c42\u503c\u4e00\u6b21(\u800c\u4e0d\u662f\u6bcf\u6b21\u7a0b\u5e8f\u8fd0\u884c\u65f6)\u3002\u5f97\u5230\u7684\u53ef\u6267\u884c\u6587\u4ef6\u901f\u5ea6\u66f4\u5feb\uff0c\u4f7f\u7528\u7684\u5185\u5b58\u66f4\u5c11\u3002","title":"\u5e38\u91cf\u8868\u8fbe\u5f0f"},{"location":"4-14-Compile-time-constants-constant-expressions-and-constexpr/#_2","text":"\u7f16\u8bd1\u65f6\u5e38\u6570\u662f\u4e00\u4e2a\u5728\u7f16\u8bd1\u65f6\u5176\u503c\u5df2\u77e5\u7684\u5e38\u6570\u3002\u5b57\u9762\u91cf(\u4f8b\u5982 1 \uff0c 2.3 \u548c \"Hello, world!\" )\u662f\u4e00\u79cd\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 \u90a3\u4e48 const \u53d8\u91cf\u5462\uff1f const \u53d8\u91cf\u53ef\u80fd\u662f\u4e5f\u53ef\u80fd\u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002","title":"\u7f16\u8bd1\u65f6\u5e38\u6570"},{"location":"4-14-Compile-time-constants-constant-expressions-and-constexpr/#const","text":"\u5982\u679c const \u53d8\u91cf\u7684\u521d\u59cb\u5316\u503c\u662f\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u90a3\u4e48\u5b83\u5c31\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 \u8003\u8651\u4e00\u4e2a\u4e0e\u4e0a\u9762\u7c7b\u4f3c\u7684\u4f7f\u7528 const \u53d8\u91cf\u7684\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 #include int main () { const int x { 3 }; // x \u662f\u7f16\u8bd1\u65f6\u5e38\u91cf const int y { 4 }; // y \u662f\u7f16\u8bd1\u65f6\u5e38\u91cf std :: cout << x + y ; // x + y \u662f\u7f16\u8bd1\u65f6\u5e38\u91cf return 0 ; } \u56e0\u4e3a x \u548c y \u7684\u521d\u59cb\u5316\u503c\u662f\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u6240\u4ee5 x \u548c y \u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u8fd9\u610f\u5473\u7740 x + y \u4e5f\u662f\u4e00\u4e2a\u5e38\u6570\u8868\u8fbe\u5f0f\u3002\u56e0\u6b64\uff0c\u5f53\u7f16\u8bd1\u5668\u7f16\u8bd1\u8fd9\u4e2a\u7a0b\u5e8f\u65f6\uff0c\u5b83\u53ef\u4ee5\u8ba1\u7b97 x + y \u7684\u503c\uff0c\u5e76\u5c06\u5e38\u91cf\u8868\u8fbe\u5f0f\u66ff\u6362\u4e3a\u7ed3\u679c\u5b57\u9762\u503c 7 \u3002 \u8bf7\u6ce8\u610f\uff0c\u7f16\u8bd1\u65f6 const \u7684\u521d\u59cb\u5316\u5f0f\u53ef\u4ee5\u662f\u4efb\u4f55\u5e38\u91cf\u8868\u8fbe\u5f0f\u3002\u4ee5\u4e0b\u4e24\u4e2a\u90fd\u662f\u7f16\u8bd1\u65f6\u7684 const \u53d8\u91cf: 1 2 const int z { 1 + 2 }; const int w { z * 2 }; \u7f16\u8bd1\u65f6\u7684 const \u53d8\u91cf\u901a\u5e38\u88ab\u7528\u4f5c\u7b26\u53f7\u5e38\u91cf: 1 const double gravity { 9.8 }; \u7f16\u8bd1\u65f6\u5e38\u91cf\u4f7f\u7f16\u8bd1\u5668\u80fd\u591f\u6267\u884c\u975e\u7f16\u8bd1\u65f6\u5e38\u91cf\u65e0\u6cd5\u63d0\u4f9b\u7684\u4f18\u5316\u3002\u4f8b\u5982\uff0c\u6bcf\u5f53\u4f7f\u7528 gravity \u65f6\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u7b80\u5355\u5730\u7528\u53cc\u7cbe\u5ea6\u5b57\u9762\u503c 9.8 \u66ff\u6362\u6807\u8bc6\u7b26 gravity \uff0c\u8fd9\u5c31\u907f\u514d\u4e86\u5fc5\u987b\u4ece\u5185\u5b58\u4e2d\u67d0\u5904\u83b7\u53d6\u503c\u3002 \u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u65f6\u5e38\u91cf\u5c06\u88ab \u4f18\u5316\u6389 \u3002\u5728\u65e0\u6cd5\u5b9e\u73b0\u7684\u60c5\u51b5\u4e0b(\u6216\u5f53\u4f18\u5316\u5173\u95ed\u65f6)\uff0c\u53d8\u91cf\u4ecd\u7136\u4f1a\u5728\u8fd0\u884c\u65f6\u521b\u5efa(\u548c\u521d\u59cb\u5316)\u3002","title":"\u7f16\u8bd1\u65f6\u5e38\u91cf(const)"},{"location":"4-14-Compile-time-constants-constant-expressions-and-constexpr/#const_1","text":"\u4efb\u4f55\u7528\u975e\u5e38\u91cf\u8868\u8fbe\u5f0f\u521d\u59cb\u5316\u7684 const \u53d8\u91cf\u90fd\u662f\u8fd0\u884c\u65f6\u5e38\u91cf\u3002\u8fd0\u884c\u65f6\u5e38\u91cf\u662f\u5728\u8fd0\u884c\u65f6\u624d\u77e5\u9053\u5176\u521d\u59cb\u5316\u503c\u7684\u5e38\u91cf\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u6f14\u793a\u4e86\u8fd0\u884c\u65f6\u5e38\u91cf\u7684\u7528\u6cd5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int getNumber () { std :: cout << \"Enter a number: \" ; int y {}; std :: cin >> y ; return y ; } int main () { const int x { 3 }; // x is a compile time constant std :: cout << \"Enter a number: \" ; const int y { getNumber () }; // y is a runtime constant std :: cout << x + y ; // x + y is a runtime expression return 0 ; } \u5373\u4f7f y \u662f const \uff0c\u5176\u521d\u59cb\u5316\u503c( getNumber() \u7684\u8fd4\u56de\u503c)\u8981\u5230\u8fd0\u884c\u65f6\u624d\u77e5\u9053\u3002\u56e0\u6b64\uff0c y \u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\u5e38\u91cf\uff0c\u800c\u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u56e0\u6b64\uff0c\u8868\u8fbe\u5f0f x + y \u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\u8868\u8fbe\u5f0f\u3002","title":"\u8fd0\u884c\u65f6\u5e38\u91cf(const)"},{"location":"4-14-Compile-time-constants-constant-expressions-and-constexpr/#constexpr","text":"\u5f53\u58f0\u660e const \u53d8\u91cf\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u9690\u5f0f\u5730\u8ddf\u8e2a\u5b83\u662f\u8fd0\u884c\u65f6\u5e38\u91cf\u8fd8\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u9664\u4e86\u4f18\u5316\u76ee\u7684\u4e4b\u5916\uff0c\u662f\u8fd0\u884c\u65f6\u5e38\u91cf\u8fd8\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u5e76\u4e0d\u91cd\u8981\uff0c\u4f46\u4e5f\u6709\u4e00\u4e9b\u5947\u602a\u7684\u60c5\u51b5\u4e0b\uff0cC++\u9700\u8981\u7f16\u8bd1\u65f6\u5e38\u91cf\u800c\u4e0d\u662f\u8fd0\u884c\u65f6\u5e38\u91cf(\u6211\u4eec\u5c06\u5728\u7a0d\u540e\u4ecb\u7ecd\u8fd9\u4e9b\u4e3b\u9898\u65f6\u8ba8\u8bba\u8fd9\u4e9b\u60c5\u51b5)\u3002 \u56e0\u4e3a\u7f16\u8bd1\u65f6\u5e38\u91cf\u901a\u5e38\u5141\u8bb8\u66f4\u597d\u7684\u4f18\u5316(\u5e76\u4e14\u51e0\u4e4e\u6ca1\u6709\u7f3a\u70b9)\uff0c\u6240\u4ee5\u6211\u4eec\u901a\u5e38\u5e0c\u671b\u5c3d\u53ef\u80fd\u4f7f\u7528\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 \u5f53\u4f7f\u7528 const \u65f6\uff0c\u53d8\u91cf\u6700\u7ec8\u53ef\u80fd\u662f\u7f16\u8bd1\u65f6\u7684 const \u6216\u8fd0\u884c\u65f6\u7684 const \uff0c\u8fd9\u53d6\u51b3\u4e8e\u521d\u59cb\u5316\u5f0f\u662f\u5426\u662f\u7f16\u8bd1\u65f6\u8868\u8fbe\u5f0f\u3002\u56e0\u4e3a\u4e24\u8005\u7684\u5b9a\u4e49\u770b\u8d77\u6765\u5b8c\u5168\u76f8\u540c\uff0c\u6240\u4ee5\u6211\u4eec\u6700\u7ec8\u5f97\u5230\u7684\u53ef\u80fd\u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\u5e38\u91cf\uff0c\u800c\u6211\u4eec\u539f\u4ee5\u4e3a\u5f97\u5230\u7684\u662f\u4e00\u4e2a\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u5728\u524d\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f88\u96be\u533a\u5206 y \u662f\u7f16\u8bd1\u65f6\u7684 const \u8fd8\u662f\u8fd0\u884c\u65f6\u7684 const \u2014\u2014\u6211\u4eec\u5fc5\u987b\u67e5\u770b getNumber() \u7684\u8fd4\u56de\u503c\u6765\u786e\u5b9a\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u7f16\u8bd1\u5668\u7684\u5e2e\u52a9\uff0c\u4ee5\u786e\u4fdd\u5728\u9884\u671f\u7684\u5730\u65b9\u5f97\u5230\u7f16\u8bd1\u65f6\u7684 const \u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5728\u53d8\u91cf\u58f0\u660e\u4e2d\u4f7f\u7528 constexpr \u800c\u4e0d\u662f const \u3002 constexpr (\u201c\u5e38\u91cf\u8868\u8fbe\u5f0f\u201d\u7684\u7f29\u5199)\u53d8\u91cf\u53ea\u80fd\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u5982\u679c constexpr \u53d8\u91cf\u7684\u521d\u59cb\u5316\u503c\u4e0d\u662f\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u7f16\u8bd1\u5668\u5c06\u51fa\u9519\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { constexpr double gravity { 9.8 }; // ok: 9.8 is a constant expression constexpr int sum { 4 + 5 }; // ok: 4 + 5 is a constant expression constexpr int something { sum }; // ok: sum is a constant expression std :: cout << \"Enter your age: \" ; int age {}; std :: cin >> age ; constexpr int myAge { age }; // compile error: age is not a constant expression return 0 ; } \u6700\u4f73\u5b9e\u8df5 \u4efb\u4f55\u5728\u521d\u59cb\u5316\u540e\u503c\u5c31\u4e0d\u80fd\u88ab\u6539\u53d8\uff0c\u4e14\u521d\u59cb\u5316\u503c\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u786e\u5b9a\u7684\u53d8\u91cf\uff0c\u90fd\u5fc5\u987b\u58f0\u660e\u4e3a constexpr \u3002 \u4efb\u4f55\u5728\u521d\u59cb\u5316\u540e\u503c\u5c31\u4e0d\u80fd\u88ab\u6539\u53d8\uff0c\u4f46\u662f\u521d\u59cb\u5316\u503c\u4e0d\u80fd\u5728\u7f16\u8bd1\u65f6\u786e\u5b9a\u7684\u53d8\u91cf\uff0c\u90fd\u5e94\u8be5\u58f0\u660e\u4e3a const \u3002 Although function parameters can be const , function parameters cannot be constexpr . \u76f8\u5173\u5185\u5bb9 C++ \u4e5f\u652f\u6301\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\u51fd\u6570(\u56e0\u6b64\u53ef\u4ee5\u5728\u5e38\u91cf\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528)\u2014\u2014\u6211\u4eec\u4f1a\u5728 6.14 - Constexpr \u548c consteval \u51fd\u6570 \u4e2d\u8ba8\u8bba\u8fd9\u4e9b\u51fd\u6570\u3002","title":"constexpr\u00a0\u5173\u952e\u5b57"},{"location":"4-15-Literals/","tags":["literal","constants","C++14"],"text":"Key Takeaway \u5e38\u91cf\u6709\u4e24\u79cd\uff0c\u5b57\u9762\u91cf\u5e38\u91cf\uff08\u7b80\u79f0\u5b57\u9762\u91cf\uff09\u548c\u7b26\u53f7\u5e38\u91cf \u5b57\u9762\u91cf\u7684\u7c7b\u578b\u5177\u6709\u9ed8\u8ba4\u503c\uff0c\u53ef\u4ee5\u901a\u8fc7\u540e\u7f00\u4fee\u6539 \u5c0f\u6570\u5b57\u9762\u91cf\u7684\u9ed8\u8ba4\u7c7b\u578b\u4e3adouble\u800c\u4e0d\u662ffloat C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u9ed8\u8ba4\u7c7b\u578b\u4e3a\u5b57\u7b26\u6570\u7ec4 std::string \u548c std::string_view \u7c7b\u578b\u7684\u5b57\u9762\u91cf\u53ef\u4ee5\u901a\u8fc7 s \u548c sv \u540e\u7f00\u6307\u5b9a\uff0c\u5728\u7c7b\u578b\u63a8\u65ad\u65f6\u6bd4\u8f83\u6709\u7528\uff0c\u5176\u4ed6\u60c5\u51b5\u4e00\u822c\u4e0d\u9700\u8981 \u58f0\u660e\u6d6e\u70b9\u6570\u5b57\u9762\u91cf\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a 3.14159 \u6216 1.9e10 \u53ea\u6709\u5f53\u4e00\u4e2a\u6570\u5b57\uff08\u4e5f\u53ef\u80fd\u662f\u5176\u4ed6\u7c7b\u578b\uff09\u542b\u4e49\u4e0d\u660e\u786e\u6216\u591a\u6b21\u88ab\u4f7f\u7528\u65f6\u624d\u88ab\u770b\u505a\u662f\u9b54\u672f\u6570\u5b57\u3002\u6709\u4e9b\u573a\u5408\u4e0b\u7684\u5b57\u9762\u91cf\uff0c\u5c24\u5176\u662f\u53ea\u4f7f\u7528\u4e00\u6b21\u7684\u5b57\u9762\u91cf\uff0c\u53ef\u4ee5\u4e0d\u88ab\u770b\u505a\u662f\u9b54\u672f\u6570\u5b57 \u4f7f\u7528\u5e38\u91cf\u6765\u907f\u514d\u9b54\u672f\u6570\u5b57 \u5b57\u9762\u91cf\u5e38\u91cf\uff08\u901a\u5e38\u79f0\u4e3a \u5b57\u9762\u91cf \uff09 \u6307\u7684\u662f\u76f4\u63a5\u5d4c\u5165\u5230\u4ee3\u7801\u4e2d\u7684\u672a\u547d\u540d\u7684\u503c\uff0c\u4f8b\u5982\uff1a 1 2 3 return 5 ; // 5 \u662f\u4e00\u4e2a\u6574\u578b\u5b57\u9762\u91cf bool myNameIsAlex { true }; // true \u662f\u4e00\u4e2a\u5e03\u5c14\u5b57\u9762\u91cf std :: cout << 3.4 ; // 3.4 \u662f\u4e00\u4e2a double \u5b57\u9762\u91cf \u5b57\u9762\u91cf\u901a\u5e38\u88ab\u79f0\u4e3a \u5b57\u9762\u91cf\u5e38\u91cf \uff0c\u56e0\u4e3a\u4f60\u4e0d\u80fd\u52a8\u6001\u5730\u6539\u53d8\u5b83\u4eec\u7684\u503c \u5b57\u9762\u91cf\u7684\u7c7b\u578b \u548c\u5176\u4ed6\u5bf9\u8c61\u8bf6\u65b0\u4e00\u6837\uff0c\u6240\u6709\u7684\u5b57\u9762\u91cf\u4e5f\u6709\u5176\u7c7b\u578b\u3002\u5b57\u9762\u91cf\u7684\u7c7b\u578b\u7531\u5176\u503c\u548c\u5b57\u9762\u91cf\u672c\u8eab\u7684\u683c\u5f0f\u63a8\u5b9a\u3002 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff1a \u5b57\u9762\u91cf\u503c \u4f8b\u5b50 \u9ed8\u8ba4\u7c7b\u578b integral value 5, 0, -3 int boolean value true, false bool floating point value 3.4, -2.2 double (\u4e0d\u662f float)! char value \u2018a\u2019 char C-style string \u201cHello, world!\u201d const char[14] \u5b57\u9762\u91cf\u540e\u7f00 \u5982\u679c\u9ed8\u8ba4\u7c7b\u578b\u4e0d\u662f\u4f60\u60f3\u8981\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u9762\u91cf\u7684\u540e\u7f00\u6539\u53d8\u5176\u7c7b\u578b\uff1a \u6570\u636e\u7c7b\u578b \u540e\u7f00 \u542b\u4e49 int u or U unsigned int int l or L long int ul, uL, Ul, UL, lu, lU, Lu, or LU unsigned long int ll or LL long long int ull, uLL, Ull, ULL, llu, llU, LLu, or LLU unsigned long long double f or F float double l or L long double \u540e\u7f00\u662f\u533a\u5206\u5927\u5c0f\u5199\u7684\uff0c\u4f46\u662f\u7531\u4e8e\u5c0f\u5199\u5b57\u6bcd L \u770b\u8d77\u6765\u5f88\u50cf 1 \uff0c\u6240\u4ee5\u5f00\u53d1\u8005\u901a\u5e38\u559c\u6b22\u7528\u5927\u5199\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 4.17 - std::string \u7b80\u4ecb \u548c 4.18 - std::string_view \u7b80\u4ecb \u4e2d\u8ba8\u8bba\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7684\u540e\u7f00\u3002 \u6574\u578b\u5b57\u9762\u91cf \u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u4f60\u4e0d\u9700\u8981\u4e3a\u6574\u578b\u6307\u5b9a\u540e\u7f00\uff0c\u4f46\u662f\u5982\u679c\u8981\u505a\u7684\u8bdd\uff0c\u53ef\u4ee5\u53c2\u8003\u4e0b\u9762\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { std :: cout << 5 ; // 5 (no suffix) is type int (by default) std :: cout << 5L ; // 5L is type long return 0 ; } u (\u6216\u8005 \u2018U\u2019) \u662f\u4e00\u4e2a\u7279\u4f8b\uff0c\u901a\u5e38\u7528\u6765\u6807\u8bb0\u65e0\u7b26\u53f7\u6574\u578b\u5b57\u9762\u91cf\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { unsigned int x { 5u }; // 5u is type unsigned int std :: cout << x ; return 0 ; } \u6d6e\u70b9\u6570\u5b57\u9762\u91cf \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6d6e\u70b9\u6570\u5b57\u9762\u91cf\u7684\u7c7b\u578b\u4e3a double \uff0c\u5982\u679c\u5e0c\u671b\u4f7f\u7528 float \u7c7b\u578b\uff0c\u5219\u9700\u8981\u6307\u5b9a f \u6216 F \u540e\u7f00\uff1a 1 2 std :: cout << 5.0 ; // 5.0 (no suffix) is type double (by default) std :: cout << 5.0f ; // 5.0f is type float \u65b0\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u5947\u602a\u4e3a\u4ec0\u4e48\u4e0b\u9762\u7684\u4ee3\u7801\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u544a\u8b66\uff1a 1 float f { 4.1 }; // warning: 4.1 \u662f double \u7c7b\u578b\u7684\u5b57\u9762\u91cf\u800c\u4e0d\u662f float \u7c7b\u578b \u56e0\u4e3a\uff0c4.1 \u6ca1\u6709\u540e\u7f00\u6240\u4ee5\u662f double \u7c7b\u578b\u7684\u5b57\u9762\u91cf\uff0c\u800c\u4e0d\u662f float \u7c7b\u578b\u3002\u5f53C++\u5b9a\u4e49\u5b57\u9762\u91cf\u7684\u7c7b\u578b\u65f6\uff0c\u5b83\u4e0d\u5728\u4e4e\u8be5\u5b57\u9762\u91cf\u7684\u7528\u9014\uff08\u4f8b\u5982\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u662f\u7528\u4e8e\u521d\u59cb\u5316\u4e00\u4e2a float \u53d8\u91cf\uff09\u3002\u56e0\u6b64\uff0c4.1 \u5fc5\u987b\u88ab\u8f6c\u6362\u4e3a double \u7c7b\u578b\u624d\u53ef\u4ee5\u88ab\u8d4b\u503c\u7ed9\u53d8\u91cf f \uff0c\u800c\u8fd9\u4e48\u505a\u662f\u4f1a\u5bfc\u81f4\u7cbe\u5ea6\u4e22\u5931\u7684\u3002 \u5728C++\u4e2d\uff0c\u53ea\u8981\u5b57\u9762\u91cf\u7684\u610f\u601d\u662f\u6e05\u6670\u7684\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u653e\u5fc3\u4f7f\u7528\u3002\u4f8b\u5982\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4f1a\u4f7f\u7528\u5b57\u9762\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u6216\u8d4b\u503c\u3001\u6570\u5b66\u8fd0\u7b97\u6216\u76f4\u63a5\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\u3002 \u5b57\u7b26\u4e32\u5b57\u9762\u91cf \u5728 4.11 - \u5b57\u7b26 \u4e2d\u6211\u4eec\u5c06\u5b57\u7b26\u4e32\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u5b57\u7b26\u5e8f\u5217\u7684\u96c6\u5408\u3002\u5728 C++ \u4e2d\u4e5f\u652f\u6301\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff1a 1 2 std :: cout << \"Hello, world!\" ; // \"Hello, world!\" is a C-style string literal std :: cout << \"Hello,\" \" world!\" ; // C++ \u4f1a\u94fe\u63a5\u5b57\u7b26\u4e32\u5b57\u9762\u91cf \u51fa\u4e8e\u67d0\u4e9b\u5386\u53f2\u539f\u56e0\uff0cC++ \u5904\u7406\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u6709\u4e9b\u7279\u522b\u3002\u76ee\u524d\u6765\u8bb2\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u6765\u4f5c\u4e3a\u6253\u5370\u6587\u672c\u6216\u7528\u4e8e\u521d\u59cb\u5316 std::string \u3002 \u6269\u5c55\u9605\u8bfb C++ \u540c\u6837\u4e5f\u652f\u6301 std::string \u548c std::string_view \u5b57\u9762\u91cf\u3002\u867d\u7136\u5728\u5f88\u591a\u65f6\u5019\u5e76\u4e0d\u9700\u8981\u4f7f\u7528\u5b83\u4eec\uff0c\u4f46\u662f\u5728\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u7684\u65f6\u5019\uff0c\u5b83\u4eec\u8fd8\u662f\u633a\u6709\u7528\u7684\u3002\u7c7b\u578b\u63a8\u65ad\u53ef\u4ee5\u53d1\u751f\u5728\u4f7f\u7528 auto \u5173\u952e\u5b57\uff08 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad \uff09\u6216\u662f\u5728\u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65ad\u65f6\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include #include // for std::string #include // for std::string_view int main () { using namespace std :: literals ; // easiest way to access the s and sv suffixes std :: cout << \"foo \\n \" ; // no suffix is a C-style string literal std :: cout << \"goo \\n \" s ; // s \u540e\u7f00\u8868\u793a std::string \u5b57\u9762\u91cf std :: cout << \"moo \\n \" sv ; // sv \u540e\u7f00\u8868\u793a a std::string_view \u5b57\u9762\u91cf return 0 ; }; \u6b64\u5904\u4f7f\u7528 using \u6765\u5f15\u5165\u6574\u4e2a\u547d\u540d\u7a7a\u95f4\u662f\u4e00\u4e2a\u88ab\u5141\u8bb8\u7684\u7279\u4f8b\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u6df1\u5165\u8ba8\u8bba\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u3002 \u6d6e\u70b9\u6570\u5b57\u9762\u91cf\u7684\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a \u58f0\u660e\u6d6e\u70b9\u6570\u5b57\u9762\u91cf\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a 1 2 double pi { 3.14159 }; // 3.14159 \u662f double \u7c7b\u578b\u5b57\u9762\u91cf double avogadro { 6.02e23 }; // 6.02 x 10^23 \u662f double \u7c7b\u578b\uff0c\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a \u5bf9\u4e8e\u79d1\u5b66\u8ba1\u6570\u6cd5\u6765\u8bb2\uff0c\u6307\u6570\u90e8\u5206\u4e5f\u53ef\u4ee5\u662f\u8d1f\u503c\uff1a 1 double electron { 1.6e-19 }; // charge on an electron is 1.6 x 10^-19 \u9b54\u672f\u6570\u5b57 \u9b54\u672f\u6570\u5b57\u6307\u7684\u662f\u542b\u4e49\u4e0d\u6e05\u6216\u591a\u6b21\u4f7f\u7528\u7684\u5b57\u9762\u91cf\uff08\u901a\u5e38\u662f\u6570\u5b57\uff09\u3002 \u4e0b\u9762\u4ee3\u7801\u5c55\u793a\u4e86\u4e00\u79cd\u542b\u4e49\u4e0d\u6e05\u7684\u9b54\u672f\u6570\u5b57\uff1a 1 constexpr int maxStudentsPerSchool { numClassrooms * 30 }; \u8fd9\u91cc\u7684 30 \u5230\u5e95\u662f\u4ec0\u4e48\u542b\u4e49\uff1f\u5c3d\u7ba1\u6709\u65f6\u5019\u4f60\u53ef\u80fd\u53ef\u4ee5\u731c\u5230\u5b83\u7684\u542b\u4e49\uff0c\u4f8b\u5982\u8fd9\u91cc\u6307\u7684\u662f\u6bcf\u4e2a\u6559\u5ba4\u6700\u591a\u7684\u5b66\u751f\u6570\uff0c\u4f46\u662f\u8fd9\u4e2a\u610f\u4e49\u5e76\u4e0d\u660e\u663e\u3002\u5bf9\u4e8e\u66f4\u52a0\u590d\u6742\u7684\u7a0b\u5e8f\u6765\u8bf4\uff0c\u63a8\u6d4b\u67d0\u4e2a\u786c\u7f16\u7801\u7684\u6570\u5b57\u662f\u5f88\u96be\u7684\uff0c\u9664\u975e\u6709\u6ce8\u91ca\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7b26\u53f7\u5e38\u91cf\u6765\u907f\u514d\u8fd9\u79cd\u542b\u4e49\u4e0d\u6e05\u7684\u9b54\u672f\u6570\u5b57\uff1a 1 2 constexpr int maxStudentsPerClass { 30 }; // now obvious what 30 is constexpr int maxStudentsPerSchool { numClassrooms * maxStudentsPerClass }; \u4f7f\u7528\u9b54\u672f\u6570\u5b57\u901a\u5e38\u88ab\u8ba4\u4e3a\u662f\u4e00\u79cd\u4e0d\u597d\u7684\u7f16\u7801\u4e60\u60ef\uff0c\u5b83\u4eec\u4e0d\u4ec5\u6ca1\u6709\u63d0\u4f9b\u5173\u4e8e\u5176\u7528\u9014\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u800c\u4e14\u8fd8\u7559\u4e0b\u4e86\u4e00\u4e2a\u9690\u60a3\uff08\u4e07\u4e00\u9700\u8981\u4fee\u6539\u5219\u9700\u8981\u5728\u591a\u5904\u4fee\u6539\uff09\u3002\u5047\u8bbe\uff0c\u5b66\u6821\u8d2d\u4e70\u4e86\u4e00\u4e9b\u65b0\u7684\u8bfe\u684c\uff0c\u73b0\u5728\u4e00\u4e2a\u6559\u5ba4\u80fd\u591f\u5bb9\u7eb3 35 \u4eba\u4e86\uff0c\u90a3\u4e48\u6211\u4eec\u7684\u7a0b\u5e8f\u4e5f\u5fc5\u987b\u53cd\u6620\u8fd9\u4e00\u60c5\u51b5\u3002\u8003\u8651\u5982\u4e0b\u4ee3\u7801\uff1a 1 2 constexpr int maxStudents { numClassrooms * 30 }; setMax ( 30 ); \u4e3a\u4e86\u4fee\u6539\u7a0b\u5e8f\u9002\u5e94\u65b0\u7684\u60c5\u51b5\uff0c\u6211\u4eec\u5fc5\u987b\u628a\u5e38\u91cf 30 \u4fee\u6539\u4e3a 35\uff0c\u4f46\u662f setMax() \u600e\u4e48\u529e\uff1f\u5b83\u7684\u53c2\u6570 30 \u548c\u5176\u4ed6 30 \u662f\u4e00\u4e2a\u610f\u601d\u5417\uff1f\u5e94\u8be5\u66f4\u65b0\u8fd8\u662f\u5e94\u8be5\u4fdd\u7559\u554a\uff0c\u641e\u4e0d\u597d\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u51fa\u95ee\u9898\u7684\u3002\u5982\u4f60\u8fdb\u884c\u5168\u5c40\u7684\u67e5\u627e\u548c\u66ff\u6362\uff0c\u90a3\u4e48\u4f60\u53ef\u80fd\u4f1a\u66ff\u6362\u6389\u672c\u4e0d\u5e94\u8be5\u88ab\u66f4\u65b0\u7684 setMax() \u4e2d\u7684 30\u3002\u6240\u4ee5\u4f60\u53ef\u80fd\u9700\u8981\u9010\u4e2a\u68c0\u67e5\u6bcf\u4e00\u4e2a 30\uff0c\u786e\u4fdd\u5b83\u5e94\u8be5\u88ab\u66ff\u6362\u624d\u6267\u884c\u64cd\u4f5c\u3002\u8fd9\u5c31\u592a\u8d39\u65f6\u95f4\uff08\u800c\u4e14\u8fd8\u5bb9\u6613\u51fa\u9519\uff09\u3002 \u4e0b\u9762\u7684\u4ee3\u7801 (\u4f7f\u7528\u7b26\u53f7\u5e38\u91cf) \u5219\u66f4\u52a0\u6e05\u6670\uff0c\u660e\u663e\u53ef\u4ee5\u770b\u51fa\u4e24\u4e2a 30 \u662f\u4e0d\u662f\u4e00\u56de\u4e8b\uff1a 1 2 3 4 5 constexpr int maxStudentsPerClass { 30 }; // now obvious what 30 is constexpr int totalStudents { numClassrooms * maxStudentsPerClass }; constexpr int maxNameLength { 30 }; setMax ( maxNameLength ); // now obvious this 30 is used in a different context \u9b54\u672f\u6570\u5b57\u5e76\u4e0d\u603b\u662f\u6570\u5b57\u2014\u2014\u4e5f\u53ef\u80fd\u662f\u5b57\u7b26\u4e32\u6216\u5176\u4ed6\u7c7b\u578b\u3002 \u6ce8\u610f\uff0c\u53ea\u4f7f\u7528\u4e00\u6b21\uff0c\u4e14\u542b\u4e49\u660e\u786e\u7684 \u5b57\u9762\u91cf \u901a\u5e38\u4e0d\u88ab\u8ba4\u4e3a\u662f\u9b54\u672f\u6570\u5b57\u3002\u50cf-1\u30010\u30010.0 \u548c 1\u8fd9\u6837\u7684\u503c\u5c31\u7ecf\u5e38\u88ab\u7528\u5728\u542b\u4e49\u975e\u5e38\u660e\u786e\u7684\u60c5\u5883\u4e2d\uff1a 1 2 int idGenerator { 0 }; // fine: we're starting our id generator with value 0 idGenerator = idGenerator + 1 ; // fine: we're just incrementing our generator \u5176\u4ed6\u503c\u5728\u67d0\u4e9b\u8bed\u5883\u4e0b\u542b\u4e49\u4e5f\u662f\u660e\u663e\u7684 (\u56e0\u6b64\u4e5f\u53ef\u4ee5\u4e0d\u88ab\u770b\u505a\u662f\u9b54\u672f\u6570\u5b57)\uff1a 1 int kmtoM ( int km ) { return km * 1000 ; } // \u53ef\u4ee5\u8fd9\u4e48\u505a: \u5f88\u663e\u7136\u8fd9\u91cc1000\u662fkm\u548cm\u4e4b\u95f4\u7684\u8f6c\u6362\u7cfb\u6570 \u6700\u4f73\u5b9e\u8df5 \u5728\u4ee3\u7801\u4e2d\u907f\u514d\u9b54\u672f\u6570\u5b57 (\u4f7f\u7528\u5e38\u91cf\u6765\u4ee3\u66ff)\u3002","title":"4.15 - \u5b57\u9762\u91cf"},{"location":"4-15-Literals/#_1","text":"\u548c\u5176\u4ed6\u5bf9\u8c61\u8bf6\u65b0\u4e00\u6837\uff0c\u6240\u6709\u7684\u5b57\u9762\u91cf\u4e5f\u6709\u5176\u7c7b\u578b\u3002\u5b57\u9762\u91cf\u7684\u7c7b\u578b\u7531\u5176\u503c\u548c\u5b57\u9762\u91cf\u672c\u8eab\u7684\u683c\u5f0f\u63a8\u5b9a\u3002 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff1a \u5b57\u9762\u91cf\u503c \u4f8b\u5b50 \u9ed8\u8ba4\u7c7b\u578b integral value 5, 0, -3 int boolean value true, false bool floating point value 3.4, -2.2 double (\u4e0d\u662f float)! char value \u2018a\u2019 char C-style string \u201cHello, world!\u201d const char[14]","title":"\u5b57\u9762\u91cf\u7684\u7c7b\u578b"},{"location":"4-15-Literals/#_2","text":"\u5982\u679c\u9ed8\u8ba4\u7c7b\u578b\u4e0d\u662f\u4f60\u60f3\u8981\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u9762\u91cf\u7684\u540e\u7f00\u6539\u53d8\u5176\u7c7b\u578b\uff1a \u6570\u636e\u7c7b\u578b \u540e\u7f00 \u542b\u4e49 int u or U unsigned int int l or L long int ul, uL, Ul, UL, lu, lU, Lu, or LU unsigned long int ll or LL long long int ull, uLL, Ull, ULL, llu, llU, LLu, or LLU unsigned long long double f or F float double l or L long double \u540e\u7f00\u662f\u533a\u5206\u5927\u5c0f\u5199\u7684\uff0c\u4f46\u662f\u7531\u4e8e\u5c0f\u5199\u5b57\u6bcd L \u770b\u8d77\u6765\u5f88\u50cf 1 \uff0c\u6240\u4ee5\u5f00\u53d1\u8005\u901a\u5e38\u559c\u6b22\u7528\u5927\u5199\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 4.17 - std::string \u7b80\u4ecb \u548c 4.18 - std::string_view \u7b80\u4ecb \u4e2d\u8ba8\u8bba\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7684\u540e\u7f00\u3002","title":"\u5b57\u9762\u91cf\u540e\u7f00"},{"location":"4-15-Literals/#_3","text":"\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u4f60\u4e0d\u9700\u8981\u4e3a\u6574\u578b\u6307\u5b9a\u540e\u7f00\uff0c\u4f46\u662f\u5982\u679c\u8981\u505a\u7684\u8bdd\uff0c\u53ef\u4ee5\u53c2\u8003\u4e0b\u9762\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { std :: cout << 5 ; // 5 (no suffix) is type int (by default) std :: cout << 5L ; // 5L is type long return 0 ; } u (\u6216\u8005 \u2018U\u2019) \u662f\u4e00\u4e2a\u7279\u4f8b\uff0c\u901a\u5e38\u7528\u6765\u6807\u8bb0\u65e0\u7b26\u53f7\u6574\u578b\u5b57\u9762\u91cf\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { unsigned int x { 5u }; // 5u is type unsigned int std :: cout << x ; return 0 ; }","title":"\u6574\u578b\u5b57\u9762\u91cf"},{"location":"4-15-Literals/#_4","text":"\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6d6e\u70b9\u6570\u5b57\u9762\u91cf\u7684\u7c7b\u578b\u4e3a double \uff0c\u5982\u679c\u5e0c\u671b\u4f7f\u7528 float \u7c7b\u578b\uff0c\u5219\u9700\u8981\u6307\u5b9a f \u6216 F \u540e\u7f00\uff1a 1 2 std :: cout << 5.0 ; // 5.0 (no suffix) is type double (by default) std :: cout << 5.0f ; // 5.0f is type float \u65b0\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u5947\u602a\u4e3a\u4ec0\u4e48\u4e0b\u9762\u7684\u4ee3\u7801\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u544a\u8b66\uff1a 1 float f { 4.1 }; // warning: 4.1 \u662f double \u7c7b\u578b\u7684\u5b57\u9762\u91cf\u800c\u4e0d\u662f float \u7c7b\u578b \u56e0\u4e3a\uff0c4.1 \u6ca1\u6709\u540e\u7f00\u6240\u4ee5\u662f double \u7c7b\u578b\u7684\u5b57\u9762\u91cf\uff0c\u800c\u4e0d\u662f float \u7c7b\u578b\u3002\u5f53C++\u5b9a\u4e49\u5b57\u9762\u91cf\u7684\u7c7b\u578b\u65f6\uff0c\u5b83\u4e0d\u5728\u4e4e\u8be5\u5b57\u9762\u91cf\u7684\u7528\u9014\uff08\u4f8b\u5982\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u662f\u7528\u4e8e\u521d\u59cb\u5316\u4e00\u4e2a float \u53d8\u91cf\uff09\u3002\u56e0\u6b64\uff0c4.1 \u5fc5\u987b\u88ab\u8f6c\u6362\u4e3a double \u7c7b\u578b\u624d\u53ef\u4ee5\u88ab\u8d4b\u503c\u7ed9\u53d8\u91cf f \uff0c\u800c\u8fd9\u4e48\u505a\u662f\u4f1a\u5bfc\u81f4\u7cbe\u5ea6\u4e22\u5931\u7684\u3002 \u5728C++\u4e2d\uff0c\u53ea\u8981\u5b57\u9762\u91cf\u7684\u610f\u601d\u662f\u6e05\u6670\u7684\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u653e\u5fc3\u4f7f\u7528\u3002\u4f8b\u5982\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4f1a\u4f7f\u7528\u5b57\u9762\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u6216\u8d4b\u503c\u3001\u6570\u5b66\u8fd0\u7b97\u6216\u76f4\u63a5\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\u3002","title":"\u6d6e\u70b9\u6570\u5b57\u9762\u91cf"},{"location":"4-15-Literals/#_5","text":"\u5728 4.11 - \u5b57\u7b26 \u4e2d\u6211\u4eec\u5c06\u5b57\u7b26\u4e32\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u5b57\u7b26\u5e8f\u5217\u7684\u96c6\u5408\u3002\u5728 C++ \u4e2d\u4e5f\u652f\u6301\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff1a 1 2 std :: cout << \"Hello, world!\" ; // \"Hello, world!\" is a C-style string literal std :: cout << \"Hello,\" \" world!\" ; // C++ \u4f1a\u94fe\u63a5\u5b57\u7b26\u4e32\u5b57\u9762\u91cf \u51fa\u4e8e\u67d0\u4e9b\u5386\u53f2\u539f\u56e0\uff0cC++ \u5904\u7406\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u6709\u4e9b\u7279\u522b\u3002\u76ee\u524d\u6765\u8bb2\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u6765\u4f5c\u4e3a\u6253\u5370\u6587\u672c\u6216\u7528\u4e8e\u521d\u59cb\u5316 std::string \u3002 \u6269\u5c55\u9605\u8bfb C++ \u540c\u6837\u4e5f\u652f\u6301 std::string \u548c std::string_view \u5b57\u9762\u91cf\u3002\u867d\u7136\u5728\u5f88\u591a\u65f6\u5019\u5e76\u4e0d\u9700\u8981\u4f7f\u7528\u5b83\u4eec\uff0c\u4f46\u662f\u5728\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u7684\u65f6\u5019\uff0c\u5b83\u4eec\u8fd8\u662f\u633a\u6709\u7528\u7684\u3002\u7c7b\u578b\u63a8\u65ad\u53ef\u4ee5\u53d1\u751f\u5728\u4f7f\u7528 auto \u5173\u952e\u5b57\uff08 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad \uff09\u6216\u662f\u5728\u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65ad\u65f6\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include #include // for std::string #include // for std::string_view int main () { using namespace std :: literals ; // easiest way to access the s and sv suffixes std :: cout << \"foo \\n \" ; // no suffix is a C-style string literal std :: cout << \"goo \\n \" s ; // s \u540e\u7f00\u8868\u793a std::string \u5b57\u9762\u91cf std :: cout << \"moo \\n \" sv ; // sv \u540e\u7f00\u8868\u793a a std::string_view \u5b57\u9762\u91cf return 0 ; }; \u6b64\u5904\u4f7f\u7528 using \u6765\u5f15\u5165\u6574\u4e2a\u547d\u540d\u7a7a\u95f4\u662f\u4e00\u4e2a\u88ab\u5141\u8bb8\u7684\u7279\u4f8b\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u6df1\u5165\u8ba8\u8bba\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u3002","title":"\u5b57\u7b26\u4e32\u5b57\u9762\u91cf"},{"location":"4-15-Literals/#_6","text":"\u58f0\u660e\u6d6e\u70b9\u6570\u5b57\u9762\u91cf\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a 1 2 double pi { 3.14159 }; // 3.14159 \u662f double \u7c7b\u578b\u5b57\u9762\u91cf double avogadro { 6.02e23 }; // 6.02 x 10^23 \u662f double \u7c7b\u578b\uff0c\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a \u5bf9\u4e8e\u79d1\u5b66\u8ba1\u6570\u6cd5\u6765\u8bb2\uff0c\u6307\u6570\u90e8\u5206\u4e5f\u53ef\u4ee5\u662f\u8d1f\u503c\uff1a 1 double electron { 1.6e-19 }; // charge on an electron is 1.6 x 10^-19","title":"\u6d6e\u70b9\u6570\u5b57\u9762\u91cf\u7684\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a"},{"location":"4-15-Literals/#_7","text":"\u9b54\u672f\u6570\u5b57\u6307\u7684\u662f\u542b\u4e49\u4e0d\u6e05\u6216\u591a\u6b21\u4f7f\u7528\u7684\u5b57\u9762\u91cf\uff08\u901a\u5e38\u662f\u6570\u5b57\uff09\u3002 \u4e0b\u9762\u4ee3\u7801\u5c55\u793a\u4e86\u4e00\u79cd\u542b\u4e49\u4e0d\u6e05\u7684\u9b54\u672f\u6570\u5b57\uff1a 1 constexpr int maxStudentsPerSchool { numClassrooms * 30 }; \u8fd9\u91cc\u7684 30 \u5230\u5e95\u662f\u4ec0\u4e48\u542b\u4e49\uff1f\u5c3d\u7ba1\u6709\u65f6\u5019\u4f60\u53ef\u80fd\u53ef\u4ee5\u731c\u5230\u5b83\u7684\u542b\u4e49\uff0c\u4f8b\u5982\u8fd9\u91cc\u6307\u7684\u662f\u6bcf\u4e2a\u6559\u5ba4\u6700\u591a\u7684\u5b66\u751f\u6570\uff0c\u4f46\u662f\u8fd9\u4e2a\u610f\u4e49\u5e76\u4e0d\u660e\u663e\u3002\u5bf9\u4e8e\u66f4\u52a0\u590d\u6742\u7684\u7a0b\u5e8f\u6765\u8bf4\uff0c\u63a8\u6d4b\u67d0\u4e2a\u786c\u7f16\u7801\u7684\u6570\u5b57\u662f\u5f88\u96be\u7684\uff0c\u9664\u975e\u6709\u6ce8\u91ca\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7b26\u53f7\u5e38\u91cf\u6765\u907f\u514d\u8fd9\u79cd\u542b\u4e49\u4e0d\u6e05\u7684\u9b54\u672f\u6570\u5b57\uff1a 1 2 constexpr int maxStudentsPerClass { 30 }; // now obvious what 30 is constexpr int maxStudentsPerSchool { numClassrooms * maxStudentsPerClass }; \u4f7f\u7528\u9b54\u672f\u6570\u5b57\u901a\u5e38\u88ab\u8ba4\u4e3a\u662f\u4e00\u79cd\u4e0d\u597d\u7684\u7f16\u7801\u4e60\u60ef\uff0c\u5b83\u4eec\u4e0d\u4ec5\u6ca1\u6709\u63d0\u4f9b\u5173\u4e8e\u5176\u7528\u9014\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u800c\u4e14\u8fd8\u7559\u4e0b\u4e86\u4e00\u4e2a\u9690\u60a3\uff08\u4e07\u4e00\u9700\u8981\u4fee\u6539\u5219\u9700\u8981\u5728\u591a\u5904\u4fee\u6539\uff09\u3002\u5047\u8bbe\uff0c\u5b66\u6821\u8d2d\u4e70\u4e86\u4e00\u4e9b\u65b0\u7684\u8bfe\u684c\uff0c\u73b0\u5728\u4e00\u4e2a\u6559\u5ba4\u80fd\u591f\u5bb9\u7eb3 35 \u4eba\u4e86\uff0c\u90a3\u4e48\u6211\u4eec\u7684\u7a0b\u5e8f\u4e5f\u5fc5\u987b\u53cd\u6620\u8fd9\u4e00\u60c5\u51b5\u3002\u8003\u8651\u5982\u4e0b\u4ee3\u7801\uff1a 1 2 constexpr int maxStudents { numClassrooms * 30 }; setMax ( 30 ); \u4e3a\u4e86\u4fee\u6539\u7a0b\u5e8f\u9002\u5e94\u65b0\u7684\u60c5\u51b5\uff0c\u6211\u4eec\u5fc5\u987b\u628a\u5e38\u91cf 30 \u4fee\u6539\u4e3a 35\uff0c\u4f46\u662f setMax() \u600e\u4e48\u529e\uff1f\u5b83\u7684\u53c2\u6570 30 \u548c\u5176\u4ed6 30 \u662f\u4e00\u4e2a\u610f\u601d\u5417\uff1f\u5e94\u8be5\u66f4\u65b0\u8fd8\u662f\u5e94\u8be5\u4fdd\u7559\u554a\uff0c\u641e\u4e0d\u597d\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u51fa\u95ee\u9898\u7684\u3002\u5982\u4f60\u8fdb\u884c\u5168\u5c40\u7684\u67e5\u627e\u548c\u66ff\u6362\uff0c\u90a3\u4e48\u4f60\u53ef\u80fd\u4f1a\u66ff\u6362\u6389\u672c\u4e0d\u5e94\u8be5\u88ab\u66f4\u65b0\u7684 setMax() \u4e2d\u7684 30\u3002\u6240\u4ee5\u4f60\u53ef\u80fd\u9700\u8981\u9010\u4e2a\u68c0\u67e5\u6bcf\u4e00\u4e2a 30\uff0c\u786e\u4fdd\u5b83\u5e94\u8be5\u88ab\u66ff\u6362\u624d\u6267\u884c\u64cd\u4f5c\u3002\u8fd9\u5c31\u592a\u8d39\u65f6\u95f4\uff08\u800c\u4e14\u8fd8\u5bb9\u6613\u51fa\u9519\uff09\u3002 \u4e0b\u9762\u7684\u4ee3\u7801 (\u4f7f\u7528\u7b26\u53f7\u5e38\u91cf) \u5219\u66f4\u52a0\u6e05\u6670\uff0c\u660e\u663e\u53ef\u4ee5\u770b\u51fa\u4e24\u4e2a 30 \u662f\u4e0d\u662f\u4e00\u56de\u4e8b\uff1a 1 2 3 4 5 constexpr int maxStudentsPerClass { 30 }; // now obvious what 30 is constexpr int totalStudents { numClassrooms * maxStudentsPerClass }; constexpr int maxNameLength { 30 }; setMax ( maxNameLength ); // now obvious this 30 is used in a different context \u9b54\u672f\u6570\u5b57\u5e76\u4e0d\u603b\u662f\u6570\u5b57\u2014\u2014\u4e5f\u53ef\u80fd\u662f\u5b57\u7b26\u4e32\u6216\u5176\u4ed6\u7c7b\u578b\u3002 \u6ce8\u610f\uff0c\u53ea\u4f7f\u7528\u4e00\u6b21\uff0c\u4e14\u542b\u4e49\u660e\u786e\u7684 \u5b57\u9762\u91cf \u901a\u5e38\u4e0d\u88ab\u8ba4\u4e3a\u662f\u9b54\u672f\u6570\u5b57\u3002\u50cf-1\u30010\u30010.0 \u548c 1\u8fd9\u6837\u7684\u503c\u5c31\u7ecf\u5e38\u88ab\u7528\u5728\u542b\u4e49\u975e\u5e38\u660e\u786e\u7684\u60c5\u5883\u4e2d\uff1a 1 2 int idGenerator { 0 }; // fine: we're starting our id generator with value 0 idGenerator = idGenerator + 1 ; // fine: we're just incrementing our generator \u5176\u4ed6\u503c\u5728\u67d0\u4e9b\u8bed\u5883\u4e0b\u542b\u4e49\u4e5f\u662f\u660e\u663e\u7684 (\u56e0\u6b64\u4e5f\u53ef\u4ee5\u4e0d\u88ab\u770b\u505a\u662f\u9b54\u672f\u6570\u5b57)\uff1a 1 int kmtoM ( int km ) { return km * 1000 ; } // \u53ef\u4ee5\u8fd9\u4e48\u505a: \u5f88\u663e\u7136\u8fd9\u91cc1000\u662fkm\u548cm\u4e4b\u95f4\u7684\u8f6c\u6362\u7cfb\u6570 \u6700\u4f73\u5b9e\u8df5 \u5728\u4ee3\u7801\u4e2d\u907f\u514d\u9b54\u672f\u6570\u5b57 (\u4f7f\u7528\u5e38\u91cf\u6765\u4ee3\u66ff)\u3002","title":"\u9b54\u672f\u6570\u5b57"},{"location":"4-16-Numeral-systems-decimal-binary-hexadecimal-and-octal/","tags":["Numeral systems"],"text":"Key Takeaway C++14\u624d\u652f\u6301\u4e86\u4f7f\u7528 0b \u521b\u5efa\u4e8c\u8fdb\u5236\u5b57\u9762\u91cf\uff0c\u540c\u65f6\u53ef\u4ee5\u7528\u5355\u5f15\u53f7\u4f5c\u4e3a\u4f4d\u7684\u5206\u9694\u7b26\uff0c\u4f46\u662f\u5206\u9694\u7b26\u4e0d\u80fd\u653e\u5728\u7b2c\u4e00\u4f4d\u524d\u9762 \u4f7f\u7528 std::dec \u3001 std::oct \u548c std::hex \u53ef\u4ee5\u8f7b\u677e\u6253\u5370\u5341\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u548c\u5341\u516d\u8fdb\u5236\u3002\u4f46\u4e0d\u80fd\u76f4\u63a5\u6253\u5370\u4e8c\u8fdb\u5236\u3002 std::cout << std::hex << x << '\\n'; // hexadecimal \u4f7f\u7528 std::bitset \uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a std::bitset \u53d8\u91cf\uff0c\u5e76\u4e14\u544a\u8bc9 std::bitset \u6709\u591a\u5c11\u4f4d\u9700\u8981\u5b58\u50a8\u3002\u9700\u8981\u5305\u542b \u5934\u6587\u4ef6 \u4f5c\u8005\u6ce8 \u672c\u8282\u8bfe\u4e3a\u9009\u5b66\u3002 \u5728\u5c06\u6765\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4f1a\u4f7f\u7528\u5341\u516d\u8fdb\u5236\u6570\uff0c\u6240\u4ee5\u4f60\u81f3\u5c11\u9700\u8981\u638c\u63e1\u5341\u516d\u8fdb\u5236\u3002 \u5728\u65e5\u5e38\u751f\u6d3b\u4e2d\uff0c\u6211\u4eec\u4e60\u60ef\u4e86\u6570\u5b57\u53ef\u4ee5\u6709 0, 1, 2, 3, 4, 5, 6, 7, 8, \u6216 9\u3002\u5341\u8fdb\u5236\u4e5f\u79f0\u4e3a\u201cbase 10\u201d\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u670910\u79cd\u4e0d\u540c\u7684\u6570\u5b57(0 \u5230 9)\u3002\u5728\u8fd9\u4e2a\u7cfb\u7edf\u91cc\uff0c\u6211\u4eec\u4f1a\u6309\u7167 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, \u2026 \u7684\u65b9\u5f0f\u6765\u8ba1\u6570\uff0c\u800c\u5728 C++ \u4e2d\uff0c\u6570\u5b57\u4e5f\u662f\u9ed8\u8ba4\u5341\u8fdb\u5236\u8868\u793a\u6cd5\u3002 1 int x { 12 }; // 12 is assumed to be a decimal number \u5bf9\u4e8e\u4e8c\u8fdb\u5236\u6765\u8bf4\uff0c\u6570\u5b57\u53ea\u6709 0 \u548c 1 \u4e24\u79cd\u53ef\u80fd\uff0c\u6240\u4ee5\u624d\u53eb\u505a 2 \uff08\u201cbase 2\u201d\uff09\u3002\u4e8c\u8fdb\u5236\u8ba1\u6570\u65f6\u901a\u5e38\u662f\u8fd9\u6837\u7684\uff1a0, 1, 10, 11, 100, 101, 110, 111, \u2026 \u5f53\u7136\uff0c\u8ba1\u7b97\u673a\u6709\u65f6\u5019\u4e5f\u4f7f\u7528\u5176\u4ed6\u7684\u8fdb\u5236\uff0c\u4f8b\u5982\u516b\u8fdb\u5236\u548c\u5341\u516d\u8fdb\u5236\u3002 8\u8fdb\u5236\u548c16\u8fdb\u5236\u5b57\u9762\u91cf \u516b\u8fdb\u5236\u7684\u6570\u5b57\u67098\u79cd\u53ef\u80fd: 0, 1, 2, 3, 4, 5, 6, \u548c 7\u3002\u8ba1\u6570\u65b9\u5f0f\u7c7b\u4f3c\u4e8e 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, \u2026 \uff08\u6ce8\u610f\uff0c\u6ca1\u67098\u548c9\uff0c\u6240\u4ee5\u6211\u4eec\u4ece7\u8df3\u5230\u4e8610\uff08\u4e00\u96f6\uff09\uff09 |\u5341\u8fdb\u5236 |0 |1 |2 |3 |4 |5 |6 |7 |8 |9 |10 |11| |---|---|---|---|---|---|---|---|---|---|---|---| |\u516b\u8fdb\u5236 |0 |1 |2 |3 |4 |5 |6 |7 |10 |11 |12 |13| \u5982\u9700\u4f7f\u7528\u516b\u8fdb\u5236\uff0c\u5219\u9700\u8981\u5728\u6570\u5b57\u524d\u52a0\u4e00\u4e2a 0 \uff08\u96f6\uff09\u4f5c\u4e3a\u524d\u7f00\u3002 1 2 3 4 5 6 7 8 #include int main () { int x { 012 }; // 0 before the number means this is octal std :: cout << x ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 10 \u4e3a\u4ec0\u4e48\u6253\u5370\u7684\u662f 10 \u800c\u4e0d\u662f 12\uff1f\u56e0\u4e3a\u6253\u5370\u65f6\u4f7f\u7528\u7684\u662f\u5341\u8fdb\u5236\uff0c\u800c\u516b\u8fdb\u5236\u768412\u7b49\u4e8e10\u8fdb\u5236\u768410\u3002 \u516b\u8fdb\u5236\u5f88\u96be\u7528\uff0c\u6211\u5efa\u8bae\u4f60\u907f\u514d\u4f7f\u7528\u5b83\u3002 \u5341\u516d\u8fdb\u5236\u670916\u4e2a\u6570\uff1a0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10, 11, 12, \u2026 \u5341\u8fdb\u5236 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 \u5341\u516d\u8fdb\u5236 0 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 \u5982\u9700\u4f7f\u7528\u5341\u516d\u8fdb\u5236\uff0c\u5219\u9700\u8981\u5728\u6570\u5b57\u524d\u52a0\u4e00\u4e2a 0x \uff08\u96f6x\uff09\u4f5c\u4e3a\u524d\u7f00\u3002 1 2 3 4 5 6 7 8 #include int main () { int x { 0xF }; // 0x before the number means this is hexadecimal std :: cout << x ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 15 \u56e0\u4e3a16\u8fdb\u5236\u4e2d\u6bcf\u4e00\u4f4d\u6570\u5b57\u90fd\u670916\u79cd\u53ef\u80fd\uff0c\u6240\u4ee5\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u7684\u6570\u521a\u597d\u5360\u7528\u4e864\u4e2a\u4f4d\u3002\u56e0\u6b64\uff0c\u4e00\u5bf9\u5341\u516d\u8fdb\u5236\u6570\u521a\u597d\u53ef\u4ee5\u8868\u793a\u4e00\u4e2a\u5b57\u8282\uff088\u4e2a\u4f4d\uff09 \u3002 \u4e00\u4e2a32\u4f4d\u7684\u6574\u578b\u8868\u793a\u6210\u4e8c\u8fdb\u5236\u662f\u8fd9\u4e2a\u6837\u5b50\u7684\uff1a0011 1010 0111 1111 1001 1000 0010 0110\u3002\u8fd9\u4e2a\u4e8c\u8fdb\u5236\u6570\u975e\u5e38\u957f\u800c\u4e14\u90fd\u662f\u91cd\u590d\u7684\u6570\u5b57\uff0c\u8bfb\u8d77\u6765\u5c31\u5f88\u56f0\u96be\u3002\u800c\u5982\u679c\u5199\u6210\u5341\u516d\u8fdb\u5236\uff0c\u5219\u53d8\u6210\u4e86\uff1a3A7F 9826\u3002\u8fd9\u4e5f\u4f7f\u5f97\u5341\u516d\u8fdb\u5236\u6570\u6210\u4e3a\u4e86\u4e00\u79cd\u975e\u5e38\u9002\u5408\u7cbe\u7b80\u5730\u8868\u793a\u5185\u5b58\u4e2d\u503c\u7684\u65b9\u5f0f\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u901a\u5e38\u90fd\u4f1a\u4f7f\u7528\u5341\u516d\u8fdb\u5236\u6570\u6765\u8868\u793a\u5185\u5b58\u5730\u5740\u6216\u5185\u5b58\u4e2d\u7684\u539f\u59cb\u6570\u636e\u3002 \u5728 C++14\u4e4b\u524d\uff0c\u662f\u6ca1\u6709\u529e\u6cd5\u76f4\u63a5\u8d4b\u503c\u4e8c\u8fdb\u5236\u5b57\u9762\u91cf\u7684\uff0c\u4e0d\u8fc7\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5341\u516d\u8fdb\u5236\u503c\u6765\u66f2\u7ebf\u6551\u56fd\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { int bin {}; // assume 16-bit ints bin = 0x0001 ; // assign binary 0000 0000 0000 0001 to the variable bin = 0x0002 ; // assign binary 0000 0000 0000 0010 to the variable bin = 0x0004 ; // assign binary 0000 0000 0000 0100 to the variable bin = 0x0008 ; // assign binary 0000 0000 0000 1000 to the variable bin = 0x0010 ; // assign binary 0000 0000 0001 0000 to the variable bin = 0x0020 ; // assign binary 0000 0000 0010 0000 to the variable bin = 0x0040 ; // assign binary 0000 0000 0100 0000 to the variable bin = 0x0080 ; // assign binary 0000 0000 1000 0000 to the variable bin = 0x00FF ; // assign binary 0000 0000 1111 1111 to the variable bin = 0x00B3 ; // assign binary 0000 0000 1011 0011 to the variable bin = 0xF770 ; // assign binary 1111 0111 0111 0000 to the variable return 0 ; } \u5728 C++14 \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 0b \u524d\u7f00\u6765\u521b\u5efa\u4e8c\u8fdb\u5236\u5b57\u9762\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int bin {}; // assume 16-bit ints bin = 0b1 ; // assign binary 0000 0000 0000 0001 to the variable bin = 0b11 ; // assign binary 0000 0000 0000 0011 to the variable bin = 0b1010 ; // assign binary 0000 0000 0000 1010 to the variable bin = 0b11110000 ; // assign binary 0000 0000 1111 0000 to the variable return 0 ; } \u56e0\u4e3a\u4e8c\u8fdb\u5236\u5b57\u9762\u91cf\u4e00\u65e6\u957f\u4e86\u5c31\u5f88\u96be\u9605\u8bfb\uff0cC++14\u5f15\u5165\u4e86\u5355\u5f15\u53f7 ' \u4f5c\u4e3a\u5206\u9694\u7b26\u3002 1 2 3 4 5 6 7 8 9 #include int main () { int bin { 0b1011'0010 }; // assign binary 1011 0010 to the variable long value { 2'132'673'462 }; // much easier to read than 2132673462 return 0 ; } \u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u4e0d\u652f\u6301 C++14\uff0c\u90a3\u4e48\u4f60\u4f7f\u7528\u4e0a\u8ff0\u8868\u793a\u6cd5\u7684\u65f6\u5019\u5c06\u4f1a\u62a5\u9519\u3002 \u6b64\u5916\uff0c\u8bf7\u6ce8\u610f\u5206\u9694\u7b26\u4e0d\u80fd\u653e\u5728\u7b2c\u4e00\u4f4d\u524d\u9762\uff1a 1 int bin { 0 b ' 1011'0010 }; // error: ' used before first digit of value \u5341\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u6253\u5370 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cC++ \u4f1a\u6309\u7167\u5341\u8fdb\u5236\u6765\u6253\u5370\u6570\u503c\u3002\u4e0d\u8fc7\u4f60\u53ef\u4ee5\u544a\u8bc9\u5b83\u6253\u5370\u5176\u4ed6\u683c\u5f0f\u3002\u4f7f\u7528 std::dec \u3001 std::oct \u548c std::hex \u53ef\u4ee5\u8f7b\u677e\u6253\u5370\u5341\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u548c\u5341\u516d\u8fdb\u5236\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 12 }; std :: cout << x << '\\n' ; // decimal (by default) std :: cout << std :: hex << x << '\\n' ; // hexadecimal std :: cout << x << '\\n' ; // now hexadecimal std :: cout << std :: oct << x << '\\n' ; // octal std :: cout << std :: dec << x << '\\n' ; // return to decimal std :: cout << x << '\\n' ; // decimal return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 3 4 5 6 12 c c 14 12 12 \u4e8c\u8fdb\u5236\u6253\u5370 \u6253\u5370\u4e8c\u8fdb\u5236\u6709\u70b9\u590d\u6742\uff0c\u56e0\u4e3a std::cout \u672c\u8eab\u5e76\u4e0d\u652f\u6301\u6253\u5370\u4e8c\u8fdb\u5236\u3002\u5e78\u597d\uff0cC++\u6807\u51c6\u5e93\u91cc\u9762\u6709\u4e00\u4e2a\u53eb\u505a std::bitset \u7684\u7c7b\u578b\u53ef\u4ee5\u4e3a\u6211\u6240\u7528 (\u9700\u8981 \u5934\u6587\u4ef6)\u3002 \u4f7f\u7528 std::bitset \uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a std::bitset \u53d8\u91cf\uff0c\u5e76\u4e14\u544a\u8bc9 std::bitset \u6709\u591a\u5c11\u4f4d\u9700\u8981\u5b58\u50a8\u3002\u8fd9\u4e2a\u4f4d\u6570\u5fc5\u987b\u662f\u4e00\u4e2a\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 std::bitset \u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u65e0\u7b26\u53f7\u5b57\u9762\u91cf\u6765\u521d\u59cb\u5316\uff08\u4efb\u4f55\u683c\u5f0f\u90fd\u53ef\u4ee5\uff0c\u5341\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u6216\u8005\u4e8c\u8fdb\u5236\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // for std::bitset #include int main () { // std::bitset<8> \u8868\u793a\u8981\u5b58\u50a88\u4e2a\u4f4d std :: bitset < 8 > bin1 { 0b1100'0101 }; // binary literal for binary 1100 0101 std :: bitset < 8 > bin2 { 0xC5 }; // hexadecimal literal for binary 1100 0101 std :: cout << bin1 << ' ' << bin2 << '\\n' ; std :: cout << std :: bitset < 4 > { 0b1010 } << '\\n' ; // create a temporary std::bitset and print it return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 11000101 11000101 1010 \u4e0a\u9762\u4ee3\u7801\u4e2d\u7684\u8fd9\u4e00\u884c\uff1a 1 std :: cout << std :: bitset < 4 > { 0b1010 } << '\\n' ; // create a temporary std::bitset and print it \u521b\u5efa\u4e86\u4e00\u4e2a\u4e34\u65f6\u76844\u4e2a\u4f4d\u7684 std::bitset \u5bf9\u8c61\uff0c\u7136\u540e\u7528\u4e8c\u8fdb\u5236 0b1010 \u5bf9\u5176\u8fdb\u884c\u4e86\u521d\u59cb\u5316\u5e76\u6253\u5370\u4e3a\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0c\u968f\u540e\u5c31\u4e22\u5f03\u4e86\u8fd9\u4e2a\u4e34\u65f6\u7684\u5bf9\u8c61\u3002","title":"4.16 - \u6570\u503c\u7cfb\u7edf\uff08\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236\uff09"},{"location":"4-16-Numeral-systems-decimal-binary-hexadecimal-and-octal/#816","text":"\u516b\u8fdb\u5236\u7684\u6570\u5b57\u67098\u79cd\u53ef\u80fd: 0, 1, 2, 3, 4, 5, 6, \u548c 7\u3002\u8ba1\u6570\u65b9\u5f0f\u7c7b\u4f3c\u4e8e 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, \u2026 \uff08\u6ce8\u610f\uff0c\u6ca1\u67098\u548c9\uff0c\u6240\u4ee5\u6211\u4eec\u4ece7\u8df3\u5230\u4e8610\uff08\u4e00\u96f6\uff09\uff09 |\u5341\u8fdb\u5236 |0 |1 |2 |3 |4 |5 |6 |7 |8 |9 |10 |11| |---|---|---|---|---|---|---|---|---|---|---|---| |\u516b\u8fdb\u5236 |0 |1 |2 |3 |4 |5 |6 |7 |10 |11 |12 |13| \u5982\u9700\u4f7f\u7528\u516b\u8fdb\u5236\uff0c\u5219\u9700\u8981\u5728\u6570\u5b57\u524d\u52a0\u4e00\u4e2a 0 \uff08\u96f6\uff09\u4f5c\u4e3a\u524d\u7f00\u3002 1 2 3 4 5 6 7 8 #include int main () { int x { 012 }; // 0 before the number means this is octal std :: cout << x ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 10 \u4e3a\u4ec0\u4e48\u6253\u5370\u7684\u662f 10 \u800c\u4e0d\u662f 12\uff1f\u56e0\u4e3a\u6253\u5370\u65f6\u4f7f\u7528\u7684\u662f\u5341\u8fdb\u5236\uff0c\u800c\u516b\u8fdb\u5236\u768412\u7b49\u4e8e10\u8fdb\u5236\u768410\u3002 \u516b\u8fdb\u5236\u5f88\u96be\u7528\uff0c\u6211\u5efa\u8bae\u4f60\u907f\u514d\u4f7f\u7528\u5b83\u3002 \u5341\u516d\u8fdb\u5236\u670916\u4e2a\u6570\uff1a0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10, 11, 12, \u2026 \u5341\u8fdb\u5236 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 \u5341\u516d\u8fdb\u5236 0 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 \u5982\u9700\u4f7f\u7528\u5341\u516d\u8fdb\u5236\uff0c\u5219\u9700\u8981\u5728\u6570\u5b57\u524d\u52a0\u4e00\u4e2a 0x \uff08\u96f6x\uff09\u4f5c\u4e3a\u524d\u7f00\u3002 1 2 3 4 5 6 7 8 #include int main () { int x { 0xF }; // 0x before the number means this is hexadecimal std :: cout << x ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 15 \u56e0\u4e3a16\u8fdb\u5236\u4e2d\u6bcf\u4e00\u4f4d\u6570\u5b57\u90fd\u670916\u79cd\u53ef\u80fd\uff0c\u6240\u4ee5\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u7684\u6570\u521a\u597d\u5360\u7528\u4e864\u4e2a\u4f4d\u3002\u56e0\u6b64\uff0c\u4e00\u5bf9\u5341\u516d\u8fdb\u5236\u6570\u521a\u597d\u53ef\u4ee5\u8868\u793a\u4e00\u4e2a\u5b57\u8282\uff088\u4e2a\u4f4d\uff09 \u3002 \u4e00\u4e2a32\u4f4d\u7684\u6574\u578b\u8868\u793a\u6210\u4e8c\u8fdb\u5236\u662f\u8fd9\u4e2a\u6837\u5b50\u7684\uff1a0011 1010 0111 1111 1001 1000 0010 0110\u3002\u8fd9\u4e2a\u4e8c\u8fdb\u5236\u6570\u975e\u5e38\u957f\u800c\u4e14\u90fd\u662f\u91cd\u590d\u7684\u6570\u5b57\uff0c\u8bfb\u8d77\u6765\u5c31\u5f88\u56f0\u96be\u3002\u800c\u5982\u679c\u5199\u6210\u5341\u516d\u8fdb\u5236\uff0c\u5219\u53d8\u6210\u4e86\uff1a3A7F 9826\u3002\u8fd9\u4e5f\u4f7f\u5f97\u5341\u516d\u8fdb\u5236\u6570\u6210\u4e3a\u4e86\u4e00\u79cd\u975e\u5e38\u9002\u5408\u7cbe\u7b80\u5730\u8868\u793a\u5185\u5b58\u4e2d\u503c\u7684\u65b9\u5f0f\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u901a\u5e38\u90fd\u4f1a\u4f7f\u7528\u5341\u516d\u8fdb\u5236\u6570\u6765\u8868\u793a\u5185\u5b58\u5730\u5740\u6216\u5185\u5b58\u4e2d\u7684\u539f\u59cb\u6570\u636e\u3002 \u5728 C++14\u4e4b\u524d\uff0c\u662f\u6ca1\u6709\u529e\u6cd5\u76f4\u63a5\u8d4b\u503c\u4e8c\u8fdb\u5236\u5b57\u9762\u91cf\u7684\uff0c\u4e0d\u8fc7\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5341\u516d\u8fdb\u5236\u503c\u6765\u66f2\u7ebf\u6551\u56fd\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { int bin {}; // assume 16-bit ints bin = 0x0001 ; // assign binary 0000 0000 0000 0001 to the variable bin = 0x0002 ; // assign binary 0000 0000 0000 0010 to the variable bin = 0x0004 ; // assign binary 0000 0000 0000 0100 to the variable bin = 0x0008 ; // assign binary 0000 0000 0000 1000 to the variable bin = 0x0010 ; // assign binary 0000 0000 0001 0000 to the variable bin = 0x0020 ; // assign binary 0000 0000 0010 0000 to the variable bin = 0x0040 ; // assign binary 0000 0000 0100 0000 to the variable bin = 0x0080 ; // assign binary 0000 0000 1000 0000 to the variable bin = 0x00FF ; // assign binary 0000 0000 1111 1111 to the variable bin = 0x00B3 ; // assign binary 0000 0000 1011 0011 to the variable bin = 0xF770 ; // assign binary 1111 0111 0111 0000 to the variable return 0 ; } \u5728 C++14 \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 0b \u524d\u7f00\u6765\u521b\u5efa\u4e8c\u8fdb\u5236\u5b57\u9762\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int bin {}; // assume 16-bit ints bin = 0b1 ; // assign binary 0000 0000 0000 0001 to the variable bin = 0b11 ; // assign binary 0000 0000 0000 0011 to the variable bin = 0b1010 ; // assign binary 0000 0000 0000 1010 to the variable bin = 0b11110000 ; // assign binary 0000 0000 1111 0000 to the variable return 0 ; } \u56e0\u4e3a\u4e8c\u8fdb\u5236\u5b57\u9762\u91cf\u4e00\u65e6\u957f\u4e86\u5c31\u5f88\u96be\u9605\u8bfb\uff0cC++14\u5f15\u5165\u4e86\u5355\u5f15\u53f7 ' \u4f5c\u4e3a\u5206\u9694\u7b26\u3002 1 2 3 4 5 6 7 8 9 #include int main () { int bin { 0b1011'0010 }; // assign binary 1011 0010 to the variable long value { 2'132'673'462 }; // much easier to read than 2132673462 return 0 ; } \u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u4e0d\u652f\u6301 C++14\uff0c\u90a3\u4e48\u4f60\u4f7f\u7528\u4e0a\u8ff0\u8868\u793a\u6cd5\u7684\u65f6\u5019\u5c06\u4f1a\u62a5\u9519\u3002 \u6b64\u5916\uff0c\u8bf7\u6ce8\u610f\u5206\u9694\u7b26\u4e0d\u80fd\u653e\u5728\u7b2c\u4e00\u4f4d\u524d\u9762\uff1a 1 int bin { 0 b ' 1011'0010 }; // error: ' used before first digit of value","title":"8\u8fdb\u5236\u548c16\u8fdb\u5236\u5b57\u9762\u91cf"},{"location":"4-16-Numeral-systems-decimal-binary-hexadecimal-and-octal/#_1","text":"\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cC++ \u4f1a\u6309\u7167\u5341\u8fdb\u5236\u6765\u6253\u5370\u6570\u503c\u3002\u4e0d\u8fc7\u4f60\u53ef\u4ee5\u544a\u8bc9\u5b83\u6253\u5370\u5176\u4ed6\u683c\u5f0f\u3002\u4f7f\u7528 std::dec \u3001 std::oct \u548c std::hex \u53ef\u4ee5\u8f7b\u677e\u6253\u5370\u5341\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u548c\u5341\u516d\u8fdb\u5236\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 12 }; std :: cout << x << '\\n' ; // decimal (by default) std :: cout << std :: hex << x << '\\n' ; // hexadecimal std :: cout << x << '\\n' ; // now hexadecimal std :: cout << std :: oct << x << '\\n' ; // octal std :: cout << std :: dec << x << '\\n' ; // return to decimal std :: cout << x << '\\n' ; // decimal return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 3 4 5 6 12 c c 14 12 12","title":"\u5341\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u6253\u5370"},{"location":"4-16-Numeral-systems-decimal-binary-hexadecimal-and-octal/#_2","text":"\u6253\u5370\u4e8c\u8fdb\u5236\u6709\u70b9\u590d\u6742\uff0c\u56e0\u4e3a std::cout \u672c\u8eab\u5e76\u4e0d\u652f\u6301\u6253\u5370\u4e8c\u8fdb\u5236\u3002\u5e78\u597d\uff0cC++\u6807\u51c6\u5e93\u91cc\u9762\u6709\u4e00\u4e2a\u53eb\u505a std::bitset \u7684\u7c7b\u578b\u53ef\u4ee5\u4e3a\u6211\u6240\u7528 (\u9700\u8981 \u5934\u6587\u4ef6)\u3002 \u4f7f\u7528 std::bitset \uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a std::bitset \u53d8\u91cf\uff0c\u5e76\u4e14\u544a\u8bc9 std::bitset \u6709\u591a\u5c11\u4f4d\u9700\u8981\u5b58\u50a8\u3002\u8fd9\u4e2a\u4f4d\u6570\u5fc5\u987b\u662f\u4e00\u4e2a\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 std::bitset \u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u65e0\u7b26\u53f7\u5b57\u9762\u91cf\u6765\u521d\u59cb\u5316\uff08\u4efb\u4f55\u683c\u5f0f\u90fd\u53ef\u4ee5\uff0c\u5341\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u6216\u8005\u4e8c\u8fdb\u5236\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // for std::bitset #include int main () { // std::bitset<8> \u8868\u793a\u8981\u5b58\u50a88\u4e2a\u4f4d std :: bitset < 8 > bin1 { 0b1100'0101 }; // binary literal for binary 1100 0101 std :: bitset < 8 > bin2 { 0xC5 }; // hexadecimal literal for binary 1100 0101 std :: cout << bin1 << ' ' << bin2 << '\\n' ; std :: cout << std :: bitset < 4 > { 0b1010 } << '\\n' ; // create a temporary std::bitset and print it return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 11000101 11000101 1010 \u4e0a\u9762\u4ee3\u7801\u4e2d\u7684\u8fd9\u4e00\u884c\uff1a 1 std :: cout << std :: bitset < 4 > { 0b1010 } << '\\n' ; // create a temporary std::bitset and print it \u521b\u5efa\u4e86\u4e00\u4e2a\u4e34\u65f6\u76844\u4e2a\u4f4d\u7684 std::bitset \u5bf9\u8c61\uff0c\u7136\u540e\u7528\u4e8c\u8fdb\u5236 0b1010 \u5bf9\u5176\u8fdb\u884c\u4e86\u521d\u59cb\u5316\u5e76\u6253\u5370\u4e3a\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0c\u968f\u540e\u5c31\u4e22\u5f03\u4e86\u8fd9\u4e2a\u4e34\u65f6\u7684\u5bf9\u8c61\u3002","title":"\u4e8c\u8fdb\u5236\u6253\u5370"},{"location":"4-17-an-introduction-to-std-string/","tags":["string","input-manipulator"],"text":"Key Takeaway std::string \u4e0d\u80fd\u63a5\u6536\u7a7a\u683c\uff0c\u4e3a\u4e86\u8f93\u5165\u4e00\u6574\u884c\u6587\u672c\uff0c\u6700\u597d\u662f\u4f7f\u7528 std::getline() \u51fd\u6570 \u4f7f\u7528\u8f93\u5165\u6216\u8f93\u51fa\u64cd\u7eb5\u5668\uff0c\u53ef\u4ee5\u4fee\u6539 std::cin \u6216 std::cout \u7684\u884c\u4e3a\uff0c std::ws \u8f93\u5165\u64cd\u7eb5\u5668\u544a\u8bc9 std::cin \u8981\u5ffd\u7565\u4efb\u4f55\u524d\u7f6e\u7a7a\u767d \u8f93\u5165\u5b57\u7b26\u4e32\u65f6\uff0c\u6572\u51fb\u56de\u8f66\u4f1a\u5bfc\u81f4\u8f93\u5165\u6d41\u4e2d\u586b\u5165 \\n \uff0c\u5b83\u53ef\u80fd\u88ab\u63d0\u53d6\uff0c\u4e5f\u53ef\u80fd\u88ab\u7559\u5728\u6d41\u4e2d\uff0c\u8fd9\u53d6\u51b3\u4e8e\u63a5\u6536\u5b83\u7684\u53d8\u91cf\u662f\u4ec0\u4e48\u7c7b\u578b\u3002\u5982\u679c\u7559\u5728\u6d41\u4e2d\u5219\u4f1a\u5bfc\u81f4 std::getline \u8ba4\u4e3a\u8f93\u5165\u5df2\u7ecf\u5b8c\u6210 \u4f7f\u7528 std::string \u7684\u6210\u5458\u51fd\u6570 std::string::length() \u83b7\u53d6\u5b57\u7b26\u4e32\u957f\u5ea6\uff0c\u5b83\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a size_t \uff08\u65e0\u7b26\u53f7\u6574\u578b\uff09 \u4f60\u7f16\u5199\u7684\u7b2c\u4e00\u4e2a C++ \u7a0b\u5e8f\u53ef\u80fd\u662f\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << \"Hello, world! \\n \" ; return 0 ; } \u90a3\u4e48\uff0c\u201cHello, world!\u201d \u7a76\u7adf\u662f\u4ec0\u4e48\u5462\uff1f\u201cHello, world!\u201d \u662f\u4e00\u7ec4\u5b57\u7b26\u7684\u96c6\u5408\uff0c\u79f0\u4e3a\u5b57\u7b26\u4e32\u3002\u5728 C++ \u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u7b26\u4e32\u6765\u8868\u793a\u6587\u672c\uff0c\u4f8b\u5982\u540d\u5b57\u3001\u5730\u5740\u3001\u5355\u8bcd\u548c\u53e5\u5b50\u3002\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff08\u4f8b\u5982 \u201cHello, world!\\n\u201d\uff09\u88ab\u653e\u5728\u53cc\u5f15\u53f7\u4e2d\u95f4\uff0c\u4ee5\u8868\u660e\u5b83\u4eec\u662f\u5b57\u7b26\u4e32\u3002 \u56e0\u4e3a\u5b57\u7b26\u4e32\u5728\u7a0b\u5e8f\u4e2d\u5f88\u5e38\u89c1\uff0c\u6240\u4ee5\u5927\u591a\u6570\u73b0\u4ee3\u7f16\u7a0b\u8bed\u8a00\u90fd\u5305\u542b\u57fa\u7840\u5b57\u7b26\u4e32\u6570\u636e\u7c7b\u578b\u3002\u4e0d\u8fc7\uff0c\u5728 C++ \u4e2d\uff0c\u5b57\u7b26\u4e32\u5e76\u4e0d\u662f\u57fa\u7840\u6570\u636e\u7c7b\u578b\uff08\u5b83\u88ab\u79f0\u4e3a \u590d\u5408\u7c7b\u578b \uff0c\u5e76\u88ab\u5b9a\u4e49\u5728 C++ \u7684\u6807\u51c6\u5e93\u4e2d\uff0c\u800c\u4e0d\u662f\u4f5c\u4e3aC++\u6838\u5fc3\u7684\u4e00\u90e8\u5206\uff09\u3002\u4f46\u662f\uff0c\u5b57\u7b26\u4e32\u5f88\u7b80\u5355\u4e5f\u5f88\u6709\u7528\uff0c\u6240\u4ee5\u6211\u4eec\u51c6\u5907\u63d0\u65e9\u4ecb\u7ecd\u5b83\u800c\u4e0d\u662f\u7b49\u5230\u7b2c\u4e5d\u7ae0\u4ecb\u7ecd\u590d\u5408\u7c7b\u578b\u65f6\u518d\u4ecb\u7ecd\u5b83\u3002 std::string \u7b80\u4ecb \u4e3a\u4e86\u5728 C++ \u4e2d\u4f7f\u7528\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u9996\u5148\u5e94\u5f53 #include \u5934\u6587\u4ef6\u4ee5\u4fbf\u83b7\u53d6 std::string \u7684\u58f0\u660e\u3002\u8fd9\u4e00\u6b65\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5b9a\u4e49 std::string \u7c7b\u578b\u7684\u53d8\u91cf\u4e86\u3002 1 2 3 #include // allows use of std::string std :: string myName {}; // empty string \u548c\u5176\u4ed6\u666e\u901a\u53d8\u91cf\u4e00\u6837\uff0c\u4f60\u53ef\u4ee5\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u521d\u59cb\u5316\u548c\u8d4b\u503c\uff1a 1 2 std :: string myName { \"Alex\" }; // initialize myName with string literal \"Alex\" myName = \"John\" ; // assign variable myName the string literal \"John\" \u6ce8\u610f\uff0c\u5b57\u7b26\u4e32\u4e5f\u53ef\u4ee5\u8868\u793a\u6570\u5b57\uff1a 1 std :: string myID { \"45\" }; // \"45\" \u4e0d\u540c\u4e8e\u6574\u578b 45! \u5728\u5b57\u7b26\u4e32\u4e2d\uff0c\u6570\u5b57\u88ab\u5f53\u505a\u6587\u672c\u5904\u7406\uff0c\u56e0\u6b64\u5b83\u4eec\u4e0d\u80fd\u88ab\u5f53\u505a\u6570\u5b57\u6765\u4f7f\u7528\uff08\u4f8b\u5982\uff0c\u4f60\u4e0d\u80fd\u5c06\u5b83\u4eec\u76f8\u4e58\uff09\u3002C++\u5e76\u4e0d\u4f1a\u5c06\u5b57\u7b26\u4e32\u6570\u5b57\u81ea\u52a8\u8f6c\u6362\u4e3a\u6574\u578b\u6216\u6d6e\u70b9\u578b\u7684\u503c\u3002 \u5b57\u7b26\u4e32\u8f93\u51fa \u5b57\u7b26\u4e32\u53ef\u4ee5\u4f7f\u7528 std::cout \u8f93\u51fa\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { std :: string myName { \"Alex\" }; std :: cout << \"My name is: \" << myName << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 My name is: Alex \u5982\u679c\u662f\u7a7a\u5b57\u7b26\u4e32\uff0c\u5219\u4ec0\u4e48\u4e5f\u4e0d\u4f1a\u6253\u5370\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { std :: string empty { }; std :: cout << '[' << empty << ']' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 [] \u4f7f\u7528 std::cin \u8f93\u5165\u5b57\u7b26\u4e32 \u4f7f\u7528 std::cin \u8f93\u5165\u5b57\u7b26\u4e32\u53ef\u80fd\u4f1a\u6709\u4e00\u4e9b\u51fa\u4eba\u610f\u6599\u7684\u5730\u65b9\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: cout << \"Enter your full name: \" ; std :: string name {}; std :: cin >> name ; // \u53ef\u80fd\u5e76\u4e0d\u80fd\u5982\u613f\u5de5\u4f5c\uff0c\u56e0\u4e3a std::cin \u662f\u7a7a\u683c\u5206\u5272\u7684 std :: cout << \"Enter your age: \" ; std :: string age {}; std :: cin >> age ; std :: cout << \"Your name is \" << name << \" and your age is \" << age << '\\n' ; return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Enter your full name: John Doe Enter your age: Your name is John and your age is Doe \u5b8c\u5168\u4e0d\u5bf9\u561b\uff01\u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\uff1f\u5b9e\u9645\u4e0a\uff0c\u5728\u4f7f\u7528 operator>> \u63d0\u53d6\u5b57\u7b26\u4e32\u5230 cin \u7684\u65f6\u5019\uff0c operator>> \u53ea\u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u7a7a\u683c\u524d\u7684\u5b57\u7b26\u3002\u5176\u4ed6\u7684\u5b57\u7b26\u90fd\u4f1a\u88ab\u7559\u5728 std::cin \u4e2d\u4f9b\u4e0b\u4e00\u6b21\u63d0\u53d6\u3002 \u56e0\u6b64\u5f53\u6211\u4eec\u7528 operator>> \u63d0\u53d6\u5b57\u7b26\u4e32\u5230 name \u65f6\uff0c\u53ea\u6709 \"John\" \u88ab\u63d0\u53d6\u4e86\uff0c\u800c \" Doe\" \u5219\u88ab\u7559\u5728\u4e86 std::cin \u7684\u7f13\u51b2\u4e2d\u3002\u968f\u540e\u5728\u4f7f\u7528 operator>> \u83b7\u53d6 age \u7684\u65f6\u5019\uff0c\u5b83\u5c31\u4f1a\u63d0\u53d6 \"Doe\" \u800c\u4e0d\u662f\u5e74\u9f84\u3002\u7136\u540e\u7a0b\u5e8f\u5c31\u622a\u6b62\u4e86\u3002 \u4f7f\u7528 std::getline() \u8f93\u5165\u6587\u672c \u4e3a\u4e86\u8f93\u5165\u4e00\u6574\u884c\u6587\u672c\uff0c\u6700\u597d\u662f\u4f7f\u7528 std::getline() \u51fd\u6570\u3002 std::getline() \u53ef\u4ee5\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f std::cin \uff0c\u7b2c\u4e8c\u4e2a\u5219\u662f\u5b57\u7b26\u4e32\u53d8\u91cf\u3002 \u76f8\u540c\u7684\u7a0b\u5e8f\uff0c\u4f7f\u7528 std::getline() \u6765\u8fdb\u884c\u8f93\u5165\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // For std::string and std::getline #include int main () { std :: cout << \"Enter your full name: \" ; std :: string name {}; std :: getline ( std :: cin >> std :: ws , name ); // read a full line of text into name std :: cout << \"Enter your age: \" ; std :: string age {}; std :: getline ( std :: cin >> std :: ws , age ); // read a full line of text into age std :: cout << \"Your name is \" << name << \" and your age is \" << age << '\\n' ; return 0 ; } \u73b0\u5728\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\uff1a 1 2 3 Enter your full name: John Doe Enter your age: 23 Your name is John Doe and your age is 23 std::ws \u53c8\u662f\u4ec0\u4e48\uff1f \u5728 4.8 - \u6d6e\u70b9\u6570 \u4e2d\u6211\u4eec\u8ba8\u8bba\u4e86 \u8f93\u51fa\u64cd\u7eb5\u5668(output manipulators) \uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u6539\u53d8\u8f93\u51fa\u7684\u65b9\u5f0f\u3002\u5f53\u65f6\uff0c\u6211\u4eec\u4f7f\u7528\u8f93\u51fa\u64cd\u7eb5\u51fd\u6570 std::setprecision() \u4fee\u6539\u4e86 std::cout \u7684\u8f93\u51fa\u7cbe\u5ea6\u3002 C++ \u5f53\u7136\u4e5f\u63d0\u4f9b\u4e86 \u8f93\u5165\u64cd\u7eb5\u5668(input manipulators) \uff0c\u5b83\u53ef\u4ee5\u4fee\u6539\u8f93\u5165\u7684\u65b9\u5f0f\u3002 std::ws \u8f93\u5165\u64cd\u7eb5\u5668\u544a\u8bc9 std::cin \u8981\u5ffd\u7565\u4efb\u4f55\u524d\u7f6e\u7a7a\u767d\u3002 \u901a\u8fc7\u4e0b\u9762\u7684\u7a0b\u5e8f\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u5b83\u6709\u4ec0\u4e48\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: cout << \"Pick 1 or 2: \" ; int choice {}; std :: cin >> choice ; std :: cout << \"Now enter your name: \" ; std :: string name {}; std :: getline ( std :: cin , name ); // \u6ce8\u610f\uff1a\u6ca1\u6709\u4f7f\u7528 std::ws std :: cout << \"Hello, \" << name << \", you picked \" << choice << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Pick 1 or 2: 2 Now enter your name: Hello, , you picked 2 \u7a0b\u5e8f\u9996\u5148\u63d0\u9192\u4f60\u8f93\u5165 1 \u6216\u8005 2\uff0c\u7136\u540e\u7b49\u5f85\u4f60\u7684\u8f93\u5165\u3002\u5230\u76ee\u524d\u4e3a\u6b62\u90fd\u6ca1\u6709\u4ec0\u4e48\u95ee\u9898\u3002\u7136\u540e\u7a0b\u5e8f\u53c8\u8ba9\u4f60\u8f93\u5165\u59d3\u540d\uff0c\u4f46\u662f\u5b83\u5e76\u6ca1\u6709\u7b49\u5f85\u4f60\u8f93\u5165\u5c31\u6253\u5370\u4e86\"Hello\"\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5c31\u9000\u51fa\u4e86\u3002 \u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\uff1f \u5b9e\u9645\u4e0a\uff0c\u5f53\u4f60\u4f7f\u7528 operator>> \u83b7\u53d6\u8f93\u5165\u7684\u65f6\u5019\uff0c std::cin \u5e76\u4e0d\u662f\u4ec5\u4ec5\u6355\u83b7\u503c\uff0c\u5b83\u4e5f\u6355\u83b7\u6362\u884c\u7b26 \\n \uff08\u5f53\u4f60\u6572\u4e0b\u56de\u8f66\u7684\u65f6\u5019\uff09\u3002\u6240\u4ee5\uff0c\u5728\u4f60\u8f93\u5165 2 \u5e76\u6572\u51fb\u56de\u8f66\u540e\uff0c std::cin \u5f97\u5230\u7684\u8f93\u5165\u5b57\u7b26\u4e32\u5176\u5b9e\u662f \"2\\n\" \u3002\u968f\u540e\uff0c2\u88ab\u63d0\u53d6\u5230\u4e86 choice \uff0c\u800c\u6362\u884c\u7b26\u5c31\u88ab\u7559\u5230\u4e86\u7f13\u51b2\u4e2d\u3002\u7136\u540e\uff0c\u5f53 std::getline() \u5f00\u59cb\u8bfb\u53d6\u59d3\u540d\u65f6\uff0c\u5b83\u53d1\u73b0 \"\\n\" \u5df2\u7ecf\u5728\u8f93\u5165\u6d41\u4e2d\u4e86\uff0c\u6240\u4ee5\u5b83\u8f93\u5165\u5728\u4e4b\u524d\u80af\u5b9a\u5df2\u7ecf\u5b8c\u6210\uff08\u8f93\u5165\u7684\u662f\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\uff09\uff01\u8fd9\u5f53\u7136\u5e76\u4e0d\u662f\u6211\u4eec\u5e0c\u671b\u7684\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 std::ws \u4fee\u6539\u8be5\u7a0b\u5e8f\uff0c\u8fd9\u6837 std::getline() \u5c31\u4f1a\u5ffd\u7565\u4efb\u4f55\u524d\u7f6e\u7684\u7a7a\u767d\u5b57\u7b26\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: cout << \"Pick 1 or 2: \" ; int choice {}; std :: cin >> choice ; std :: cout << \"Now enter your name: \" ; std :: string name {}; std :: getline ( std :: cin >> std :: ws , name ); // note: added std::ws here std :: cout << \"Hello, \" << name << \", you picked \" << choice << '\\n' ; return 0 ; } \u73b0\u5728\uff0c \u7a0b\u5e8f\u5c31\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\u3002 1 2 3 Pick 1 or 2: 2 Now enter your name: Alex Hello, Alex, you picked 2 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f7f\u7528 std::getline \u6765\u8bfb\u53d6\u5b57\u7b26\u4e32\uff0c\u8bf7\u4f7f\u7528 std::ws \u5ffd\u7565\u524d\u7f6e\u7684\u7a7a\u767d \u5173\u952e\u4fe1\u606f \u4f7f\u7528\u63d0\u53d6\u64cd\u4f5c\u7b26( >> ) \u548c std::cin \u6765\u5ffd\u7565\u524d\u7f6e\u7a7a\u767d\u3002 \u9664\u975e\u4f7f\u7528 std::ws \u6307\u660e\uff0c\u5426\u5219 std::getline \u5e76\u4e0d\u4f1a\u5ffd\u7565\u524d\u7f6e\u7a7a\u767d\u3002 \u5b57\u7b26\u4e32\u957f\u5ea6 \u5982\u679c\u60f3\u8981\u77e5\u9053 std::string \u5305\u542b\u591a\u5c11\u5b57\u7b26\uff0c\u5219\u53ef\u4ee5\u67e5\u8be2 std::string \u7684\u957f\u5ea6\uff0c\u5176\u8bed\u6cd5\u53ef\u80fd\u548c\u4e4b\u524d\u6211\u4eec\u63a5\u89e6\u5230\u7684\u4e0d\u592a\u4e00\u6837\uff0c\u4f46\u662f\u975e\u5e38\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 #include #include int main () { std :: string myName { \"Alex\" }; std :: cout << myName << \" has \" << myName . length () << \" characters \\n \" ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 Alex has 4 characters \u6ce8\u610f\uff0c\u5e76\u4e0d\u662f\u901a\u8fc7 length(myName) \u8fd9\u79cd\u5f62\u5f0f\u6765\u67e5\u8be2\uff0c\u800c\u662f myName.length() \u3002\u56e0\u4e3a length() \u51fd\u6570\u5e76\u4e0d\u662f\u4e00\u4e2a\u666e\u901a\u7684\u3001\u72ec\u7acb\u7684\u51fd\u6570\u2014\u2014\u5b83\u662f\u4e00\u7c7b\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u5d4c\u5165\u5728 std::string \uff0c\u79f0\u4e3a \u6210\u5458\u51fd\u6570(member function) \u3002\u56e0\u4e3a length() \u5b58\u5728\u4e8e std::string \u5185\u90e8\uff0c\u56e0\u6b64\u5728\u6587\u6863\u4e2d\u6709\u65f6\u4f1a\u5199\u4f5c std::string::length \u3002 \u6211\u4eec\u4f1a\u5728\u7a0d\u540e\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u548c\u7f16\u5199\u6210\u5458\u51fd\u6570\u3002 \u540c\u65f6\uff0c\u8fd8\u8981\u6ce8\u610f\u5230 std::string::length() \u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u65e0\u7b26\u53f7\u6574\u578b\u6570\uff08\u5f88\u53ef\u80fd\u662f size_t \uff09\u3002\u5982\u679c\u4f60\u5e0c\u671b\u5c06\u5b83\u8d4b\u503c\u7ed9\u4e00\u4e2a int \u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528 static_cast \uff0c\u8fd9\u6837\u624d\u80fd\u907f\u514d signed/unsigned \u8f6c\u6362\u5bfc\u81f4\u7684\u7f16\u8bd1\u5668\u544a\u8b66\uff1a 1 int length = static_cast < int > ( myName . length ()); In C++20, you can also use the std::ssize() function to get the length of a std::string as a signed integer: 1 2 3 4 5 6 7 8 9 10 #include #include int main () { std :: string name { \"Alex\" }; std :: cout << name << \" has \" << std :: ssize ( name ) << \" characters \\n \" ; return 0 ; } COPY std::string is expensive to initialize and copy Whenever a std::string is initialized, a copy of the string used to initialize it is made. And whenever a std::string is passed by value to a std::string parameter, another copy is made. These copies are expensive, and should be avoided if possible. Best practice Do not pass std::string by value, as making copies of std::string is expensive. Prefer std::string_view parameters. We\u2019ll discuss this topic (and std::string_view ) further in lesson 4.18 - std::string_view \u7b80\u4ecb Literals for std::string Double-quoted string literals (like \u201cHello, world!\u201d) are C-style strings by default (and thus, have a strange type). We can create string literals with type std::string by using a s suffix after the double-quoted string literal. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include #include // for std::string #include // for std::string_view int main () { using namespace std :: literals ; // easiest way to access the s and sv suffixes std :: cout << \"foo \\n \" ; // no suffix is a C-style string literal std :: cout << \"goo \\n \" s ; // s suffix is a std::string literal std :: cout << \"moo \\n \" sv ; // sv suffix is a std::string_view literal return 0 ; }; \u5c0f\u8d34\u58eb The \u201cs\u201d suffix lives in the namespace std::literals::string_literals . The easiest way to access the literal suffixes is via using directive using namespace std::literals . We discuss using directives in lesson 6.12 -- Using declarations and using directives . This is one of the exception cases where using an entire namespace is okay. You probably won\u2019t need to use std::string literals very often (as it\u2019s fine to initialize a std::string object with a C-style string literal), but we\u2019ll see a few cases in future lessons where using std::string literals instead of C-style string literals makes things easier. Constexpr strings If you try to define a constexpr std::string , your compiler will probably generate an error: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include using namespace std :: literals ; int main () { constexpr std :: string name { \"Alex\" s }; // compile error std :: cout << \"My name is: \" << name ; return 0 ; } This happens because constexpr std::string isn\u2019t supported in C++17 or earlier, and only has minimal support in C++20. If you need constexpr strings, use std::string_view instead (discussed in lesson 4.18 -- Introduction to std::string_view . \u7ed3\u8bba std::string \u5f88\u590d\u6742\uff0c\u800c\u4e14\u4f7f\u7528\u4e86\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u5230\u7684\u591a\u79cd\u8bed\u8a00\u7279\u6027\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u5bf9\u4e8e\u5b8c\u6210\u7b80\u5355\u7684\u4efb\u52a1\u6765\u8bf4\u6765\u8bf4\uff0c\u4f60\u5e76\u4e0d\u9700\u8981\u5b8c\u5168\u7406\u89e3\u5b83\uff0c\u6bd4\u5982\u57fa\u672c\u7684\u8f93\u5165\u8f93\u51fa\u3002\u5efa\u8bae\u4f60\u4ece\u73b0\u5728\u5f00\u59cb\u5c31\u53ef\u4ee5\u591a\u5c1d\u8bd5\u5e76\u63a2\u7d22z\u5b57\u7b26\u4e32\u7684\u4f7f\u7528\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u5185\u5bb9\u4e2d\u4ecb\u7ecdstring\u7684\u5176\u4ed6\u529f\u80fd\u3002","title":"4.17 - std::string \u7b80\u4ecb"},{"location":"4-17-an-introduction-to-std-string/#stdstring","text":"\u4e3a\u4e86\u5728 C++ \u4e2d\u4f7f\u7528\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u9996\u5148\u5e94\u5f53 #include \u5934\u6587\u4ef6\u4ee5\u4fbf\u83b7\u53d6 std::string \u7684\u58f0\u660e\u3002\u8fd9\u4e00\u6b65\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5b9a\u4e49 std::string \u7c7b\u578b\u7684\u53d8\u91cf\u4e86\u3002 1 2 3 #include // allows use of std::string std :: string myName {}; // empty string \u548c\u5176\u4ed6\u666e\u901a\u53d8\u91cf\u4e00\u6837\uff0c\u4f60\u53ef\u4ee5\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u521d\u59cb\u5316\u548c\u8d4b\u503c\uff1a 1 2 std :: string myName { \"Alex\" }; // initialize myName with string literal \"Alex\" myName = \"John\" ; // assign variable myName the string literal \"John\" \u6ce8\u610f\uff0c\u5b57\u7b26\u4e32\u4e5f\u53ef\u4ee5\u8868\u793a\u6570\u5b57\uff1a 1 std :: string myID { \"45\" }; // \"45\" \u4e0d\u540c\u4e8e\u6574\u578b 45! \u5728\u5b57\u7b26\u4e32\u4e2d\uff0c\u6570\u5b57\u88ab\u5f53\u505a\u6587\u672c\u5904\u7406\uff0c\u56e0\u6b64\u5b83\u4eec\u4e0d\u80fd\u88ab\u5f53\u505a\u6570\u5b57\u6765\u4f7f\u7528\uff08\u4f8b\u5982\uff0c\u4f60\u4e0d\u80fd\u5c06\u5b83\u4eec\u76f8\u4e58\uff09\u3002C++\u5e76\u4e0d\u4f1a\u5c06\u5b57\u7b26\u4e32\u6570\u5b57\u81ea\u52a8\u8f6c\u6362\u4e3a\u6574\u578b\u6216\u6d6e\u70b9\u578b\u7684\u503c\u3002","title":"std::string \u7b80\u4ecb"},{"location":"4-17-an-introduction-to-std-string/#_1","text":"\u5b57\u7b26\u4e32\u53ef\u4ee5\u4f7f\u7528 std::cout \u8f93\u51fa\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { std :: string myName { \"Alex\" }; std :: cout << \"My name is: \" << myName << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 My name is: Alex \u5982\u679c\u662f\u7a7a\u5b57\u7b26\u4e32\uff0c\u5219\u4ec0\u4e48\u4e5f\u4e0d\u4f1a\u6253\u5370\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { std :: string empty { }; std :: cout << '[' << empty << ']' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 []","title":"\u5b57\u7b26\u4e32\u8f93\u51fa"},{"location":"4-17-an-introduction-to-std-string/#stdcin","text":"\u4f7f\u7528 std::cin \u8f93\u5165\u5b57\u7b26\u4e32\u53ef\u80fd\u4f1a\u6709\u4e00\u4e9b\u51fa\u4eba\u610f\u6599\u7684\u5730\u65b9\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: cout << \"Enter your full name: \" ; std :: string name {}; std :: cin >> name ; // \u53ef\u80fd\u5e76\u4e0d\u80fd\u5982\u613f\u5de5\u4f5c\uff0c\u56e0\u4e3a std::cin \u662f\u7a7a\u683c\u5206\u5272\u7684 std :: cout << \"Enter your age: \" ; std :: string age {}; std :: cin >> age ; std :: cout << \"Your name is \" << name << \" and your age is \" << age << '\\n' ; return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Enter your full name: John Doe Enter your age: Your name is John and your age is Doe \u5b8c\u5168\u4e0d\u5bf9\u561b\uff01\u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\uff1f\u5b9e\u9645\u4e0a\uff0c\u5728\u4f7f\u7528 operator>> \u63d0\u53d6\u5b57\u7b26\u4e32\u5230 cin \u7684\u65f6\u5019\uff0c operator>> \u53ea\u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u7a7a\u683c\u524d\u7684\u5b57\u7b26\u3002\u5176\u4ed6\u7684\u5b57\u7b26\u90fd\u4f1a\u88ab\u7559\u5728 std::cin \u4e2d\u4f9b\u4e0b\u4e00\u6b21\u63d0\u53d6\u3002 \u56e0\u6b64\u5f53\u6211\u4eec\u7528 operator>> \u63d0\u53d6\u5b57\u7b26\u4e32\u5230 name \u65f6\uff0c\u53ea\u6709 \"John\" \u88ab\u63d0\u53d6\u4e86\uff0c\u800c \" Doe\" \u5219\u88ab\u7559\u5728\u4e86 std::cin \u7684\u7f13\u51b2\u4e2d\u3002\u968f\u540e\u5728\u4f7f\u7528 operator>> \u83b7\u53d6 age \u7684\u65f6\u5019\uff0c\u5b83\u5c31\u4f1a\u63d0\u53d6 \"Doe\" \u800c\u4e0d\u662f\u5e74\u9f84\u3002\u7136\u540e\u7a0b\u5e8f\u5c31\u622a\u6b62\u4e86\u3002","title":"\u4f7f\u7528 std::cin \u8f93\u5165\u5b57\u7b26\u4e32"},{"location":"4-17-an-introduction-to-std-string/#stdgetline","text":"\u4e3a\u4e86\u8f93\u5165\u4e00\u6574\u884c\u6587\u672c\uff0c\u6700\u597d\u662f\u4f7f\u7528 std::getline() \u51fd\u6570\u3002 std::getline() \u53ef\u4ee5\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f std::cin \uff0c\u7b2c\u4e8c\u4e2a\u5219\u662f\u5b57\u7b26\u4e32\u53d8\u91cf\u3002 \u76f8\u540c\u7684\u7a0b\u5e8f\uff0c\u4f7f\u7528 std::getline() \u6765\u8fdb\u884c\u8f93\u5165\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // For std::string and std::getline #include int main () { std :: cout << \"Enter your full name: \" ; std :: string name {}; std :: getline ( std :: cin >> std :: ws , name ); // read a full line of text into name std :: cout << \"Enter your age: \" ; std :: string age {}; std :: getline ( std :: cin >> std :: ws , age ); // read a full line of text into age std :: cout << \"Your name is \" << name << \" and your age is \" << age << '\\n' ; return 0 ; } \u73b0\u5728\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\uff1a 1 2 3 Enter your full name: John Doe Enter your age: 23 Your name is John Doe and your age is 23","title":"\u4f7f\u7528 std::getline() \u8f93\u5165\u6587\u672c"},{"location":"4-17-an-introduction-to-std-string/#stdws","text":"\u5728 4.8 - \u6d6e\u70b9\u6570 \u4e2d\u6211\u4eec\u8ba8\u8bba\u4e86 \u8f93\u51fa\u64cd\u7eb5\u5668(output manipulators) \uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u6539\u53d8\u8f93\u51fa\u7684\u65b9\u5f0f\u3002\u5f53\u65f6\uff0c\u6211\u4eec\u4f7f\u7528\u8f93\u51fa\u64cd\u7eb5\u51fd\u6570 std::setprecision() \u4fee\u6539\u4e86 std::cout \u7684\u8f93\u51fa\u7cbe\u5ea6\u3002 C++ \u5f53\u7136\u4e5f\u63d0\u4f9b\u4e86 \u8f93\u5165\u64cd\u7eb5\u5668(input manipulators) \uff0c\u5b83\u53ef\u4ee5\u4fee\u6539\u8f93\u5165\u7684\u65b9\u5f0f\u3002 std::ws \u8f93\u5165\u64cd\u7eb5\u5668\u544a\u8bc9 std::cin \u8981\u5ffd\u7565\u4efb\u4f55\u524d\u7f6e\u7a7a\u767d\u3002 \u901a\u8fc7\u4e0b\u9762\u7684\u7a0b\u5e8f\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u5b83\u6709\u4ec0\u4e48\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: cout << \"Pick 1 or 2: \" ; int choice {}; std :: cin >> choice ; std :: cout << \"Now enter your name: \" ; std :: string name {}; std :: getline ( std :: cin , name ); // \u6ce8\u610f\uff1a\u6ca1\u6709\u4f7f\u7528 std::ws std :: cout << \"Hello, \" << name << \", you picked \" << choice << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Pick 1 or 2: 2 Now enter your name: Hello, , you picked 2 \u7a0b\u5e8f\u9996\u5148\u63d0\u9192\u4f60\u8f93\u5165 1 \u6216\u8005 2\uff0c\u7136\u540e\u7b49\u5f85\u4f60\u7684\u8f93\u5165\u3002\u5230\u76ee\u524d\u4e3a\u6b62\u90fd\u6ca1\u6709\u4ec0\u4e48\u95ee\u9898\u3002\u7136\u540e\u7a0b\u5e8f\u53c8\u8ba9\u4f60\u8f93\u5165\u59d3\u540d\uff0c\u4f46\u662f\u5b83\u5e76\u6ca1\u6709\u7b49\u5f85\u4f60\u8f93\u5165\u5c31\u6253\u5370\u4e86\"Hello\"\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5c31\u9000\u51fa\u4e86\u3002 \u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\uff1f \u5b9e\u9645\u4e0a\uff0c\u5f53\u4f60\u4f7f\u7528 operator>> \u83b7\u53d6\u8f93\u5165\u7684\u65f6\u5019\uff0c std::cin \u5e76\u4e0d\u662f\u4ec5\u4ec5\u6355\u83b7\u503c\uff0c\u5b83\u4e5f\u6355\u83b7\u6362\u884c\u7b26 \\n \uff08\u5f53\u4f60\u6572\u4e0b\u56de\u8f66\u7684\u65f6\u5019\uff09\u3002\u6240\u4ee5\uff0c\u5728\u4f60\u8f93\u5165 2 \u5e76\u6572\u51fb\u56de\u8f66\u540e\uff0c std::cin \u5f97\u5230\u7684\u8f93\u5165\u5b57\u7b26\u4e32\u5176\u5b9e\u662f \"2\\n\" \u3002\u968f\u540e\uff0c2\u88ab\u63d0\u53d6\u5230\u4e86 choice \uff0c\u800c\u6362\u884c\u7b26\u5c31\u88ab\u7559\u5230\u4e86\u7f13\u51b2\u4e2d\u3002\u7136\u540e\uff0c\u5f53 std::getline() \u5f00\u59cb\u8bfb\u53d6\u59d3\u540d\u65f6\uff0c\u5b83\u53d1\u73b0 \"\\n\" \u5df2\u7ecf\u5728\u8f93\u5165\u6d41\u4e2d\u4e86\uff0c\u6240\u4ee5\u5b83\u8f93\u5165\u5728\u4e4b\u524d\u80af\u5b9a\u5df2\u7ecf\u5b8c\u6210\uff08\u8f93\u5165\u7684\u662f\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\uff09\uff01\u8fd9\u5f53\u7136\u5e76\u4e0d\u662f\u6211\u4eec\u5e0c\u671b\u7684\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 std::ws \u4fee\u6539\u8be5\u7a0b\u5e8f\uff0c\u8fd9\u6837 std::getline() \u5c31\u4f1a\u5ffd\u7565\u4efb\u4f55\u524d\u7f6e\u7684\u7a7a\u767d\u5b57\u7b26\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: cout << \"Pick 1 or 2: \" ; int choice {}; std :: cin >> choice ; std :: cout << \"Now enter your name: \" ; std :: string name {}; std :: getline ( std :: cin >> std :: ws , name ); // note: added std::ws here std :: cout << \"Hello, \" << name << \", you picked \" << choice << '\\n' ; return 0 ; } \u73b0\u5728\uff0c \u7a0b\u5e8f\u5c31\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\u3002 1 2 3 Pick 1 or 2: 2 Now enter your name: Alex Hello, Alex, you picked 2 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f7f\u7528 std::getline \u6765\u8bfb\u53d6\u5b57\u7b26\u4e32\uff0c\u8bf7\u4f7f\u7528 std::ws \u5ffd\u7565\u524d\u7f6e\u7684\u7a7a\u767d \u5173\u952e\u4fe1\u606f \u4f7f\u7528\u63d0\u53d6\u64cd\u4f5c\u7b26( >> ) \u548c std::cin \u6765\u5ffd\u7565\u524d\u7f6e\u7a7a\u767d\u3002 \u9664\u975e\u4f7f\u7528 std::ws \u6307\u660e\uff0c\u5426\u5219 std::getline \u5e76\u4e0d\u4f1a\u5ffd\u7565\u524d\u7f6e\u7a7a\u767d\u3002","title":"std::ws \u53c8\u662f\u4ec0\u4e48\uff1f"},{"location":"4-17-an-introduction-to-std-string/#_2","text":"\u5982\u679c\u60f3\u8981\u77e5\u9053 std::string \u5305\u542b\u591a\u5c11\u5b57\u7b26\uff0c\u5219\u53ef\u4ee5\u67e5\u8be2 std::string \u7684\u957f\u5ea6\uff0c\u5176\u8bed\u6cd5\u53ef\u80fd\u548c\u4e4b\u524d\u6211\u4eec\u63a5\u89e6\u5230\u7684\u4e0d\u592a\u4e00\u6837\uff0c\u4f46\u662f\u975e\u5e38\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 #include #include int main () { std :: string myName { \"Alex\" }; std :: cout << myName << \" has \" << myName . length () << \" characters \\n \" ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 Alex has 4 characters \u6ce8\u610f\uff0c\u5e76\u4e0d\u662f\u901a\u8fc7 length(myName) \u8fd9\u79cd\u5f62\u5f0f\u6765\u67e5\u8be2\uff0c\u800c\u662f myName.length() \u3002\u56e0\u4e3a length() \u51fd\u6570\u5e76\u4e0d\u662f\u4e00\u4e2a\u666e\u901a\u7684\u3001\u72ec\u7acb\u7684\u51fd\u6570\u2014\u2014\u5b83\u662f\u4e00\u7c7b\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u5d4c\u5165\u5728 std::string \uff0c\u79f0\u4e3a \u6210\u5458\u51fd\u6570(member function) \u3002\u56e0\u4e3a length() \u5b58\u5728\u4e8e std::string \u5185\u90e8\uff0c\u56e0\u6b64\u5728\u6587\u6863\u4e2d\u6709\u65f6\u4f1a\u5199\u4f5c std::string::length \u3002 \u6211\u4eec\u4f1a\u5728\u7a0d\u540e\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u548c\u7f16\u5199\u6210\u5458\u51fd\u6570\u3002 \u540c\u65f6\uff0c\u8fd8\u8981\u6ce8\u610f\u5230 std::string::length() \u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u65e0\u7b26\u53f7\u6574\u578b\u6570\uff08\u5f88\u53ef\u80fd\u662f size_t \uff09\u3002\u5982\u679c\u4f60\u5e0c\u671b\u5c06\u5b83\u8d4b\u503c\u7ed9\u4e00\u4e2a int \u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528 static_cast \uff0c\u8fd9\u6837\u624d\u80fd\u907f\u514d signed/unsigned \u8f6c\u6362\u5bfc\u81f4\u7684\u7f16\u8bd1\u5668\u544a\u8b66\uff1a 1 int length = static_cast < int > ( myName . length ()); In C++20, you can also use the std::ssize() function to get the length of a std::string as a signed integer: 1 2 3 4 5 6 7 8 9 10 #include #include int main () { std :: string name { \"Alex\" }; std :: cout << name << \" has \" << std :: ssize ( name ) << \" characters \\n \" ; return 0 ; } COPY std::string is expensive to initialize and copy Whenever a std::string is initialized, a copy of the string used to initialize it is made. And whenever a std::string is passed by value to a std::string parameter, another copy is made. These copies are expensive, and should be avoided if possible. Best practice Do not pass std::string by value, as making copies of std::string is expensive. Prefer std::string_view parameters. We\u2019ll discuss this topic (and std::string_view ) further in lesson 4.18 - std::string_view \u7b80\u4ecb","title":"\u5b57\u7b26\u4e32\u957f\u5ea6"},{"location":"4-17-an-introduction-to-std-string/#literals-for-stdstring","text":"Double-quoted string literals (like \u201cHello, world!\u201d) are C-style strings by default (and thus, have a strange type). We can create string literals with type std::string by using a s suffix after the double-quoted string literal. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include #include // for std::string #include // for std::string_view int main () { using namespace std :: literals ; // easiest way to access the s and sv suffixes std :: cout << \"foo \\n \" ; // no suffix is a C-style string literal std :: cout << \"goo \\n \" s ; // s suffix is a std::string literal std :: cout << \"moo \\n \" sv ; // sv suffix is a std::string_view literal return 0 ; }; \u5c0f\u8d34\u58eb The \u201cs\u201d suffix lives in the namespace std::literals::string_literals . The easiest way to access the literal suffixes is via using directive using namespace std::literals . We discuss using directives in lesson 6.12 -- Using declarations and using directives . This is one of the exception cases where using an entire namespace is okay. You probably won\u2019t need to use std::string literals very often (as it\u2019s fine to initialize a std::string object with a C-style string literal), but we\u2019ll see a few cases in future lessons where using std::string literals instead of C-style string literals makes things easier.","title":"Literals for\u00a0std::string"},{"location":"4-17-an-introduction-to-std-string/#constexpr-strings","text":"If you try to define a constexpr std::string , your compiler will probably generate an error: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include using namespace std :: literals ; int main () { constexpr std :: string name { \"Alex\" s }; // compile error std :: cout << \"My name is: \" << name ; return 0 ; } This happens because constexpr std::string isn\u2019t supported in C++17 or earlier, and only has minimal support in C++20. If you need constexpr strings, use std::string_view instead (discussed in lesson 4.18 -- Introduction to std::string_view .","title":"Constexpr strings"},{"location":"4-17-an-introduction-to-std-string/#_3","text":"std::string \u5f88\u590d\u6742\uff0c\u800c\u4e14\u4f7f\u7528\u4e86\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u5230\u7684\u591a\u79cd\u8bed\u8a00\u7279\u6027\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u5bf9\u4e8e\u5b8c\u6210\u7b80\u5355\u7684\u4efb\u52a1\u6765\u8bf4\u6765\u8bf4\uff0c\u4f60\u5e76\u4e0d\u9700\u8981\u5b8c\u5168\u7406\u89e3\u5b83\uff0c\u6bd4\u5982\u57fa\u672c\u7684\u8f93\u5165\u8f93\u51fa\u3002\u5efa\u8bae\u4f60\u4ece\u73b0\u5728\u5f00\u59cb\u5c31\u53ef\u4ee5\u591a\u5c1d\u8bd5\u5e76\u63a2\u7d22z\u5b57\u7b26\u4e32\u7684\u4f7f\u7528\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u5185\u5bb9\u4e2d\u4ecb\u7ecdstring\u7684\u5176\u4ed6\u529f\u80fd\u3002","title":"\u7ed3\u8bba"},{"location":"4-18-Introduction-to-std-string_view/","tags":["string_view","C++17"],"text":"Key Takeaway \u5f53\u9700\u8981\u4f7f\u7528\u53ea\u8bfb\u5b57\u7b26\u4e32\u65f6\uff0c\u4f7f\u7528 std:: string_view \u800c\u4e0d\u662f std:: string \uff0c\u5c24\u5176\u662f\u51fd\u6570\u5f62\u53c2\u3002 std:: string_view \u4e0d\u80fd\u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a std:: string \u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06 std:: string_view \u8f6c\u6362\u4e3a std:: string \u3002 std:: string_view \u53ef\u4ee5\u901a\u8fc7 std:: string \u521d\u59cb\u5316 \u521b\u5efa std:: string_view \u7c7b\u578b\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff0c\u5219\u9700\u8981\u5728\u53cc\u5f15\u53f7\u5b57\u7b26\u4e32\u540e\u9762\u6dfb\u52a0 sv \u540e\u7f00\u3002\u5982\u679c\u4e0d\u6dfb\u52a0\u540e\u7f00\u5219\u9ed8\u8ba4\u662f C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { int x { 5 }; std :: cout << x << '\\n' ; return 0 ; } \u5f53\u5bf9 x \u8fdb\u884c\u5b9a\u4e49\u65f6\uff0c\u521d\u59cb\u5316\u503c 5 \u4f1a\u88ab\u62f7\u8d1d\u5230 int x \u7684\u5185\u5b58\u5730\u5740\u3002\u5bf9\u4e8e\u57fa\u7840\u6570\u636e\u7c7b\u578b\u6765\u8bf4\uff0c\u521d\u59cb\u5316\uff08\u6216\u8005\u62f7\u8d1d\uff09\u4e00\u4e2a\u53d8\u91cf\u662f\u5f88\u5feb\u7684\u3002 \u5728\u770b\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { std :: string s { \"Hello, world!\" }; std :: cout << s << '\\n' ; return 0 ; } \u5f53 s \u521d\u59cb\u5316\u65f6\uff0cC \u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32 \u5b57\u9762\u91cf \"Hello, world!\" \u4f1a\u88ab\u62f7\u8d1d\u5230 std:: string s \u7684\u5185\u5b58\u5730\u5740\u3002\u548c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e0d\u540c\u7684\u662f\uff0c\u521d\u59cb\u5316\u6216\u8005\u62f7\u8d1d std:: string \u662f\u8f83\u6162\u7684\u3002 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u5c06 s \u7684\u503c\u6253\u5370\u5230\u63a7\u5236\u53f0\uff0c\u7136\u540e\u9500\u6bc1 s \u3002\u6211\u4eec\u590d\u5236\u4e86\u4e00\u4efd\u201cHello, world!\u201d\uff0c\u53ea\u662f\u4e3a\u4e86\u6253\u5370\uff0c\u7136\u540e\u5c31\u9500\u6bc1\u4e86\u90a3\u4e2a\u526f\u672c\uff0c\u76f8\u5f53\u4f4e\u6548\u3002 \u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u4e5f\u7c7b\u4f3c\u7684\u60c5\u51b5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include void printString ( std :: string str ) { std :: cout << str '\\n' ; } int main () { std :: string s { \"Hello, world!\" }; printString ( s ); return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cC\u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u201dHello world!\u201c \u88ab\u590d\u5236\u4e86\u4e24\u6b21\u3002\u7b2c\u4e00\u6b21\u662f\u5728 main() \u51fd\u6570\u4e2d\u5bf9 s \u521d\u59cb\u5316\u7684\u65f6\u5019\uff0c\u53e6\u5916\u4e00\u6b21\u662f\u5728 printString() \u4e2d\u521d\u59cb\u5316\u53c2\u6570 str \u7684\u65f6\u5019\u3002\u4e3a\u4e86\u6253\u5370\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u9700\u8981\u8fdb\u884c\u5f88\u591a\u6b21\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\uff01 std:: string_view \uff08C++17\uff09 \u4e3a\u4e86\u89e3\u51b3 std:: string \u521d\u59cb\u5316\u6216\u62f7\u8d1d\u5f00\u9500\u6bd4\u8f83\u5927\u7684\u95ee\u9898\uff0cC++17 \u5f15\u5165\u4e86 std:: string_view (\u5b58\u5728\u4e8e \u5934\u6587\u4ef6\u4e2d)\u3002 std:: string_view \u4e3a\u5df2\u6709\u7684\u5b57\u7b26\u4e32(C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u3001 std:: string \u6216\u8005\u5b57\u7b26\u6570\u7ec4) \u63d0\u4f9b\u4e00\u79cd \u53ea\u8bfb \u7684\u8bbf\u95ee\u65b9\u5f0f\uff0c\u800c\u65e0\u9700\u521b\u5efa\u4e00\u4efd\u62f7\u8d1d\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u548c\u4e4b\u524d\u7684\u7a0b\u5e8f\u6548\u679c\u662f\u5b8c\u5168\u4e00\u81f4\u7684\uff0c\u53ea\u4e0d\u8fc7\u6211\u4eec\u4f7f\u7528 std:: string_view \u66ff\u6362\u4e86 std:: string \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include #include void printSV ( std :: string_view str ) // now a std::string_view { std :: cout << str '\\n' ; } int main () { std :: string_view s { \"Hello, world!\" }; // now a std::string_view printSV ( s ); return 0 ; } \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u548c\u4e4b\u524d\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u4f46\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u5e76\u4e0d\u4f1a\u521b\u5efa \u201cHello, world!\u201d \u7684\u526f\u672c\u3002 std:: string_view s \u901a\u8fc7 C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 \"Hello, world!\" \u8fdb\u884c\u521d\u59cb\u5316\u3002\u53d8\u91cf s \u4e3a \u201cHello, world!\u201d \u63d0\u4f9b\u4e86\u53ea\u8bfb\u7684\u8bbf\u95ee\u65b9\u5f0f\uff0c\u65e0\u9700\u521b\u5efa\u526f\u672c\u3002\u5f53\u6211\u4eec\u5c06\u53d8\u91cf s \u4f20\u9012\u7ed9 printSV() \u65f6\uff0c\u51fd\u6570\u7684\u5f62\u53c2 str \u88ab\u521d\u59cb\u5316\u4e3a s \u3002\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 str \u8bbf\u95ee\u5b57\u7b26\u4e32 \u201cHello, world!\u201d\uff0c\u800c\u4e0d\u9700\u8981\u521b\u5efa\u8be5\u5b57\u7b26\u4e32\u7684\u62f7\u8d1d\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u9700\u8981\u4f7f\u7528\u53ea\u8bfb\u5b57\u7b26\u4e32\u65f6\uff0c\u4f7f\u7528 std:: string_view \u800c\u4e0d\u662f std:: string \uff0c\u5c24\u5176\u662f\u51fd\u6570\u5f62\u53c2\u3002 constexpr std:: string_view \u548c std:: string \u4e0d\u540c\u7684\u662f\uff0c std:: string_view \u5b8c\u5168\u652f\u6301 constexpr \uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { constexpr std :: string_view s { \"Hello, world!\" }; std :: cout << s << '\\n' ; // s \u4f1a\u5728\u7f16\u8bd1\u65f6\u88ab\u66ff\u6362\u4e3a \"Hello, world!\" return 0 ; } \u5c06 std:: string \u8f6c\u6362\u4e3a std:: string_view std:: string_view \u53ef\u4ee5\u901a\u8fc7 std:: string \u7c7b\u578b\u7684\u521d\u59cb\u5316\u503c\u521b\u5efa\uff0c\u6b64\u65f6 std:: string \u4f1a\u88ab \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u4e3a std:: string_view \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include #include void printSV ( std :: string_view str ) { std :: cout << str << '\\n' ; } int main () { std :: string s { \"Hello, world\" }; std :: string_view sv { s }; // Initialize a std::string_view from a std::string std :: cout << sv ; printSV ( s ); // implicitly convert a std::string to std::string_view return 0 ; } \u5c06 std:: string_view \u8f6c\u6362\u4e3a std:: string \u56e0\u4e3a std:: string \u4f1a\u62f7\u8d1d\u5b83\u7684\u521d\u59cb\u5316\u503c\uff0c\u6240\u4ee5 C++\u4e0d\u5141\u8bb8\u5c06 std:: string_view \u9690\u5f0f\u8f6c\u6362\u4e3a std:: string \u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u53ef\u4ee5\u663e\u793a\u5730\u4f7f\u7528 std:: string_view \u4f5c\u4e3a\u521d\u59cb\u5316\u503c\u5e76\u521b\u5efa std:: string \u3002\u6216\u8005\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06 std:: string_view \u8f6c\u6362\u4e3a std:: string \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include #include void printString ( std :: string str ) { std :: cout << str << '\\n' ; } int main () { std :: string_view sv { \"balloon\" }; std :: string str { sv }; // okay, \u53ef\u4ee5\u4f7f\u7528std::string_view\u521d\u59cb\u5316\u503c\u521b\u5efastd::string // printString(sv); // \u7f16\u8bd1\u9519\u8bef: std::string_view \u4e0d\u80fd\u9690\u5f0f\u8f6c\u6362\u4e3astd::string print ( static_cast < std :: string > ( sv )); // okay,\u53ef\u4ee5\u663e\u793a\u5730\u5c06 std::string_view \u8f6c\u6362\u4e3a std::string return 0 ; } std:: string_view \u5b57\u9762\u91cf \u53cc\u5f15\u53f7\u5185\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u901a\u5e38\u9ed8\u8ba4\u662f C \u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002\u5982\u679c\u5e0c\u671b\u521b\u5efa std:: string_view \u7c7b\u578b\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff0c\u5219\u9700\u8981\u5728\u53cc\u5f15\u53f7\u5b57\u7b26\u4e32\u540e\u9762\u6dfb\u52a0 sv \u540e\u7f00\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include #include // for std::string #include // for std::string_view int main () { using namespace std :: literals ; // easiest way to access the s and sv suffixes std :: cout << \"foo \\n \" ; // no suffix is a C-style string literal std :: cout << \"goo \\n \" s ; // s suffix is a std::string literal std :: cout << \"moo \\n \" sv ; // sv suffix is a std::string_view literal return 0 ; }; \u5c0f\u8d34\u58eb sv \u540e\u7f00\u4f4d\u4e8e\u547d\u540d\u7a7a\u95f4 std:: literals:: string_view_literals \u4e2d\u3002\u8bbf\u95ee\u5b57\u9762\u91cf\u540e\u7f00\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u6307\u4ee4 using namespace std:: literals \u3002\u6211\u4eec\u5728\u8bfe\u7a0b 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 \u4e2d\u8ba8\u8bba\u4e86 using \u6307\u4ee4\u3002\u8fd9\u662f using \u6574\u4e2a\u547d\u540d\u7a7a\u95f4\u662f\u53ef\u4ee5\u7684\u4f8b\u5916\u60c5\u51b5\u4e4b\u4e00\u3002 \u4e0d\u8981\u8fd4\u56de std:: string_view \u7c7b\u578b\u7684\u503c \u4ece\u51fd\u6570\u8fd4\u56de std:: string_view \u7c7b\u578b\u7684\u503c\u53ef\u4e0d\u662f\u4e2a\u597d\u4e3b\u610f\uff0c\u6211\u4eec\u4f1a\u5728 11.7 - std:: string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 \u4e2d\u8ba8\u8bba\u3002\u76ee\u524d\u4f60\u53ea\u9700\u8981\u8bb0\u4f4f\u4e0d\u8981\u8fd9\u4e48\u505a\u5c31\u884c\u3002","title":"4.18 - std::string_view \u7b80\u4ecb"},{"location":"4-18-Introduction-to-std-string_view/#std-string_viewc17","text":"\u4e3a\u4e86\u89e3\u51b3 std:: string \u521d\u59cb\u5316\u6216\u62f7\u8d1d\u5f00\u9500\u6bd4\u8f83\u5927\u7684\u95ee\u9898\uff0cC++17 \u5f15\u5165\u4e86 std:: string_view (\u5b58\u5728\u4e8e \u5934\u6587\u4ef6\u4e2d)\u3002 std:: string_view \u4e3a\u5df2\u6709\u7684\u5b57\u7b26\u4e32(C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u3001 std:: string \u6216\u8005\u5b57\u7b26\u6570\u7ec4) \u63d0\u4f9b\u4e00\u79cd \u53ea\u8bfb \u7684\u8bbf\u95ee\u65b9\u5f0f\uff0c\u800c\u65e0\u9700\u521b\u5efa\u4e00\u4efd\u62f7\u8d1d\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u548c\u4e4b\u524d\u7684\u7a0b\u5e8f\u6548\u679c\u662f\u5b8c\u5168\u4e00\u81f4\u7684\uff0c\u53ea\u4e0d\u8fc7\u6211\u4eec\u4f7f\u7528 std:: string_view \u66ff\u6362\u4e86 std:: string \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include #include void printSV ( std :: string_view str ) // now a std::string_view { std :: cout << str '\\n' ; } int main () { std :: string_view s { \"Hello, world!\" }; // now a std::string_view printSV ( s ); return 0 ; } \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u548c\u4e4b\u524d\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u4f46\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u5e76\u4e0d\u4f1a\u521b\u5efa \u201cHello, world!\u201d \u7684\u526f\u672c\u3002 std:: string_view s \u901a\u8fc7 C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 \"Hello, world!\" \u8fdb\u884c\u521d\u59cb\u5316\u3002\u53d8\u91cf s \u4e3a \u201cHello, world!\u201d \u63d0\u4f9b\u4e86\u53ea\u8bfb\u7684\u8bbf\u95ee\u65b9\u5f0f\uff0c\u65e0\u9700\u521b\u5efa\u526f\u672c\u3002\u5f53\u6211\u4eec\u5c06\u53d8\u91cf s \u4f20\u9012\u7ed9 printSV() \u65f6\uff0c\u51fd\u6570\u7684\u5f62\u53c2 str \u88ab\u521d\u59cb\u5316\u4e3a s \u3002\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 str \u8bbf\u95ee\u5b57\u7b26\u4e32 \u201cHello, world!\u201d\uff0c\u800c\u4e0d\u9700\u8981\u521b\u5efa\u8be5\u5b57\u7b26\u4e32\u7684\u62f7\u8d1d\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u9700\u8981\u4f7f\u7528\u53ea\u8bfb\u5b57\u7b26\u4e32\u65f6\uff0c\u4f7f\u7528 std:: string_view \u800c\u4e0d\u662f std:: string \uff0c\u5c24\u5176\u662f\u51fd\u6570\u5f62\u53c2\u3002","title":"std:: string_view\uff08C++17\uff09"},{"location":"4-18-Introduction-to-std-string_view/#constexpr-std-string_view","text":"\u548c std:: string \u4e0d\u540c\u7684\u662f\uff0c std:: string_view \u5b8c\u5168\u652f\u6301 constexpr \uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { constexpr std :: string_view s { \"Hello, world!\" }; std :: cout << s << '\\n' ; // s \u4f1a\u5728\u7f16\u8bd1\u65f6\u88ab\u66ff\u6362\u4e3a \"Hello, world!\" return 0 ; }","title":"constexpr std:: string_view"},{"location":"4-18-Introduction-to-std-string_view/#std-string-std-string_view","text":"std:: string_view \u53ef\u4ee5\u901a\u8fc7 std:: string \u7c7b\u578b\u7684\u521d\u59cb\u5316\u503c\u521b\u5efa\uff0c\u6b64\u65f6 std:: string \u4f1a\u88ab \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u4e3a std:: string_view \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include #include void printSV ( std :: string_view str ) { std :: cout << str << '\\n' ; } int main () { std :: string s { \"Hello, world\" }; std :: string_view sv { s }; // Initialize a std::string_view from a std::string std :: cout << sv ; printSV ( s ); // implicitly convert a std::string to std::string_view return 0 ; }","title":"\u5c06\u00a0std:: string\u00a0\u8f6c\u6362\u4e3a\u00a0std:: string_view"},{"location":"4-18-Introduction-to-std-string_view/#std-string_view-std-string","text":"\u56e0\u4e3a std:: string \u4f1a\u62f7\u8d1d\u5b83\u7684\u521d\u59cb\u5316\u503c\uff0c\u6240\u4ee5 C++\u4e0d\u5141\u8bb8\u5c06 std:: string_view \u9690\u5f0f\u8f6c\u6362\u4e3a std:: string \u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u53ef\u4ee5\u663e\u793a\u5730\u4f7f\u7528 std:: string_view \u4f5c\u4e3a\u521d\u59cb\u5316\u503c\u5e76\u521b\u5efa std:: string \u3002\u6216\u8005\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06 std:: string_view \u8f6c\u6362\u4e3a std:: string \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include #include void printString ( std :: string str ) { std :: cout << str << '\\n' ; } int main () { std :: string_view sv { \"balloon\" }; std :: string str { sv }; // okay, \u53ef\u4ee5\u4f7f\u7528std::string_view\u521d\u59cb\u5316\u503c\u521b\u5efastd::string // printString(sv); // \u7f16\u8bd1\u9519\u8bef: std::string_view \u4e0d\u80fd\u9690\u5f0f\u8f6c\u6362\u4e3astd::string print ( static_cast < std :: string > ( sv )); // okay,\u53ef\u4ee5\u663e\u793a\u5730\u5c06 std::string_view \u8f6c\u6362\u4e3a std::string return 0 ; }","title":"\u5c06 std:: string_view\u00a0\u8f6c\u6362\u4e3a std:: string"},{"location":"4-18-Introduction-to-std-string_view/#std-string_view","text":"\u53cc\u5f15\u53f7\u5185\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u901a\u5e38\u9ed8\u8ba4\u662f C \u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002\u5982\u679c\u5e0c\u671b\u521b\u5efa std:: string_view \u7c7b\u578b\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff0c\u5219\u9700\u8981\u5728\u53cc\u5f15\u53f7\u5b57\u7b26\u4e32\u540e\u9762\u6dfb\u52a0 sv \u540e\u7f00\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include #include // for std::string #include // for std::string_view int main () { using namespace std :: literals ; // easiest way to access the s and sv suffixes std :: cout << \"foo \\n \" ; // no suffix is a C-style string literal std :: cout << \"goo \\n \" s ; // s suffix is a std::string literal std :: cout << \"moo \\n \" sv ; // sv suffix is a std::string_view literal return 0 ; }; \u5c0f\u8d34\u58eb sv \u540e\u7f00\u4f4d\u4e8e\u547d\u540d\u7a7a\u95f4 std:: literals:: string_view_literals \u4e2d\u3002\u8bbf\u95ee\u5b57\u9762\u91cf\u540e\u7f00\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u6307\u4ee4 using namespace std:: literals \u3002\u6211\u4eec\u5728\u8bfe\u7a0b 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 \u4e2d\u8ba8\u8bba\u4e86 using \u6307\u4ee4\u3002\u8fd9\u662f using \u6574\u4e2a\u547d\u540d\u7a7a\u95f4\u662f\u53ef\u4ee5\u7684\u4f8b\u5916\u60c5\u51b5\u4e4b\u4e00\u3002","title":"std:: string_view \u5b57\u9762\u91cf"},{"location":"4-18-Introduction-to-std-string_view/#std-string_view_1","text":"\u4ece\u51fd\u6570\u8fd4\u56de std:: string_view \u7c7b\u578b\u7684\u503c\u53ef\u4e0d\u662f\u4e2a\u597d\u4e3b\u610f\uff0c\u6211\u4eec\u4f1a\u5728 11.7 - std:: string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 \u4e2d\u8ba8\u8bba\u3002\u76ee\u524d\u4f60\u53ea\u9700\u8981\u8bb0\u4f4f\u4e0d\u8981\u8fd9\u4e48\u505a\u5c31\u884c\u3002","title":"\u4e0d\u8981\u8fd4\u56de std:: string_view \u7c7b\u578b\u7684\u503c"},{"location":"4-2-Void/","tags":["void"],"text":"Void \u662f\u6700\u5bb9\u6613\u4ecb\u7ecd\u7684\u6570\u636e\u7c7b\u578b\u3002\u57fa\u672c\u4e0a\uff0cvoid \u610f\u5473\u7740\u201c\u6ca1\u6709\u7c7b\u578b\u201d\uff01 \u56e0\u6b64\uff0c\u53d8\u91cf\u4e0d\u80fd\u88ab\u5b9a\u4e49\u4e3a void \u7c7b\u578b\uff1a 1 void value ; // won't work, variables can't be defined with a void type \u4f46 Void \u53ef\u4ee5\u88ab\u7528\u5728\u5176\u4ed6\u5730\u65b9\u3002 \u4e0d\u8fd4\u56de\u503c\u7684\u51fd\u6570 \u6700\u5e38\u89c1\u7684\u7528\u5230 void \u7684\u5730\u65b9\u662f\u6307\u793a\u51fd\u6570\u6ca1\u6709\u8fd4\u56de\u503c\uff1a 1 2 3 4 5 void writeValue ( int x ) // void here means no return value { std :: cout << \"The value of x is: \" << x << '\\n' ; // no return statement, because this function doesn't return a value } \u5982\u679c\u4f60\u5728\u8fd9\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u4e2d\u4f7f\u7528\u4e86 return \u8bed\u53e5\uff0c\u5219\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\uff1a 1 2 3 4 void noReturn ( int x ) // void here means no return value { return 5 ; // error } \u5728 Visual Studio 2017 \u4e0a\uff0c\u62a5\u9519\u4fe1\u606f\u5982\u4e0b\uff1a 1 error C2562: 'noReturn': 'void' function returning a value \u5f03\u7528: \u4e0d\u63a5\u53d7\u4ea7\u751f\u7684\u51fd\u6570 \u5728 C \u8bed\u8a00\u4e2d\uff0cvoid \u8fd8\u88ab\u7528\u6765\u8868\u660e\u51fd\u6570\u4e0d\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\uff1a 1 2 3 4 5 6 int getValue ( void ) // void here means no parameters { int x {}; std :: cin >> x ; return x ; } \u5c3d\u7ba1\u4e0a\u8ff0\u7528\u6cd5\u5728 C++ \u4e2d\u4ecd\u7136\u53ef\u7528\uff08\u5411\u540e\u517c\u5bb9\uff09\uff0c\u8fd9\u79cd\u65b9\u5f0f\u5728 C++ \u4e2d\u88ab\u8ba4\u4e3a\u662f\u5df2\u7ecf\u5f03\u7528\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u8fd9\u79cd\u65b9\u5f0f\u662f\u7b49\u4ef7\u7684\uff0c\u4e5f\u662f C++ \u6240\u63a8\u8350\u7684\uff1a 1 2 3 4 5 6 int getValue () // \u7a7a\u7684\u51fd\u6570\u5f62\u53c2\u5c31\u9690\u542b\u4e0d\u63a5\u53d7\u53c2\u6570\u7684\u542b\u4e49 { int x {}; std :: cin >> x ; return x ; } \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u7a7a\u7684\u5f62\u53c2\u5217\u8868\u800c\u975e void \u6765\u8868\u793a\u51fd\u6570\u4e0d\u63a5\u53d7\u53c2\u6570 void \u7684\u5176\u4ed6\u7528\u6cd5 void \u5173\u952e\u5b57\u5728 C++ \u4e2d\u7684\u7b2c\u4e09\u79cd\u7528\u6cd5\uff08\u66f4\u9ad8\u7ea7\u7684\u7528\u6cd5\uff09\u6211\u4eec\u4f1a\u5728 11.4 - void \u6307\u9488 \u4e2d\u8fdb\u884c\u4ecb\u7ecd . \u7531\u4e8e\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u6307\u9488\u7684\u76f8\u5173\u6982\u5ff5\uff0c\u6240\u4ee5\u6682\u65f6\u4e0d\u9700\u8981\u5173\u7cfb\u7a7a\u6307\u9488\u7684\u7528\u6cd5\u3002 \u7ee7\u7eed\u5f80\u4e0b\u770b\u5427\uff01","title":"4.2 - Void"},{"location":"4-2-Void/#_1","text":"\u6700\u5e38\u89c1\u7684\u7528\u5230 void \u7684\u5730\u65b9\u662f\u6307\u793a\u51fd\u6570\u6ca1\u6709\u8fd4\u56de\u503c\uff1a 1 2 3 4 5 void writeValue ( int x ) // void here means no return value { std :: cout << \"The value of x is: \" << x << '\\n' ; // no return statement, because this function doesn't return a value } \u5982\u679c\u4f60\u5728\u8fd9\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u4e2d\u4f7f\u7528\u4e86 return \u8bed\u53e5\uff0c\u5219\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\uff1a 1 2 3 4 void noReturn ( int x ) // void here means no return value { return 5 ; // error } \u5728 Visual Studio 2017 \u4e0a\uff0c\u62a5\u9519\u4fe1\u606f\u5982\u4e0b\uff1a 1 error C2562: 'noReturn': 'void' function returning a value","title":"\u4e0d\u8fd4\u56de\u503c\u7684\u51fd\u6570"},{"location":"4-2-Void/#_2","text":"\u5728 C \u8bed\u8a00\u4e2d\uff0cvoid \u8fd8\u88ab\u7528\u6765\u8868\u660e\u51fd\u6570\u4e0d\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\uff1a 1 2 3 4 5 6 int getValue ( void ) // void here means no parameters { int x {}; std :: cin >> x ; return x ; } \u5c3d\u7ba1\u4e0a\u8ff0\u7528\u6cd5\u5728 C++ \u4e2d\u4ecd\u7136\u53ef\u7528\uff08\u5411\u540e\u517c\u5bb9\uff09\uff0c\u8fd9\u79cd\u65b9\u5f0f\u5728 C++ \u4e2d\u88ab\u8ba4\u4e3a\u662f\u5df2\u7ecf\u5f03\u7528\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u8fd9\u79cd\u65b9\u5f0f\u662f\u7b49\u4ef7\u7684\uff0c\u4e5f\u662f C++ \u6240\u63a8\u8350\u7684\uff1a 1 2 3 4 5 6 int getValue () // \u7a7a\u7684\u51fd\u6570\u5f62\u53c2\u5c31\u9690\u542b\u4e0d\u63a5\u53d7\u53c2\u6570\u7684\u542b\u4e49 { int x {}; std :: cin >> x ; return x ; } \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u7a7a\u7684\u5f62\u53c2\u5217\u8868\u800c\u975e void \u6765\u8868\u793a\u51fd\u6570\u4e0d\u63a5\u53d7\u53c2\u6570","title":"\u5f03\u7528: \u4e0d\u63a5\u53d7\u4ea7\u751f\u7684\u51fd\u6570"},{"location":"4-2-Void/#void","text":"void \u5173\u952e\u5b57\u5728 C++ \u4e2d\u7684\u7b2c\u4e09\u79cd\u7528\u6cd5\uff08\u66f4\u9ad8\u7ea7\u7684\u7528\u6cd5\uff09\u6211\u4eec\u4f1a\u5728 11.4 - void \u6307\u9488 \u4e2d\u8fdb\u884c\u4ecb\u7ecd . \u7531\u4e8e\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u6307\u9488\u7684\u76f8\u5173\u6982\u5ff5\uff0c\u6240\u4ee5\u6682\u65f6\u4e0d\u9700\u8981\u5173\u7cfb\u7a7a\u6307\u9488\u7684\u7528\u6cd5\u3002 \u7ee7\u7eed\u5f80\u4e0b\u770b\u5427\uff01","title":"void \u7684\u5176\u4ed6\u7528\u6cd5"},{"location":"4-3-Object-sizes-and-the-sizeof-operator/","tags":["sizeof"],"text":"Key Takeaway C++ \u53ea\u4fdd\u8bc1\u4e86\u6bcf\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\u5360\u7528\u7a7a\u95f4\u7684\u6700\u5c0f\u503c\uff0c\u5177\u4f53\u5927\u5c0f\u548c\u7f16\u8bd1\u5668\u53ca\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u76f8\u5173 CPU \u6309\u7167\u67d0\u4e2a\u957f\u5ea6\u6765\u5904\u7406\u6570\u636e\uff0c\u53ea\u6709\u5f53\u6570\u636e\u7c7b\u578b\u7684\u5927\u5c0f\u7b49\u4e8e\u8be5\u957f\u5ea6\u65f6\u624d\u4f1a\u66f4\u5feb\uff0c\u5e76\u4e0d\u4e00\u5b9a\u5360\u7528\u5185\u5b58\u5c0f\u7684\u6570\u636e\u7ed3\u6784\u66f4\u5feb \u5bf9\u8c61\u7684\u5927\u5c0f \u6b63\u5982\u5728 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb \u4e2d\u4ecb\u7ecd\u7684\u90a3\u6837\uff0c\u73b0\u4ee3\u8ba1\u7b97\u673a\u4ee5 \u5b57\u8282(byte) \u4e3a\u5355\u4f4d\u4f7f\u7528\u5185\u5b58\uff0c\u6bcf\u4e2a\u5b57\u8282\u7684\u5185\u5b58\u90fd\u6709\u5176\u552f\u4e00\u5bf9\u5e94\u7684\u5730\u5740\u3002\u4ece\u8fd9\u4e2a\u89d2\u5ea6\u6765\u8bf4\uff0c\u5185\u5b58\u53ef\u4ee5\u88ab\u60f3\u8c61\u6210\u4e00\u7cfb\u5217\u7684\u5c0f\u50a8\u85cf\u95f4\u6216\u4fe1\u7bb1\uff0c\u4f60\u53ef\u4ee5\u4ece\u4e2d\u5b58\u653e\u6216\u8bfb\u53d6\u4fe1\u606f\uff0c\u800c\u53d8\u91cf\u540d\u5219\u7528\u6765\u5bf9\u8fd9\u4e9b\u50a8\u85cf\u95f4\u6216\u4fe1\u7bb1\u8fdb\u884c\u8bbf\u95ee\u3002 \u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u6bd4\u55bb\u5e76\u4e0d\u5b8c\u5168\u51c6\u786e\u2014\u2014\u5f88\u591a\u5bf9\u8c61\u5b9e\u9645\u4e0a\u5360\u636e\u7684\u5185\u5b58\u8981\u5927\u4e8e1\u4e2a\u5b57\u8282\u3002\u4e00\u4e2a\u5bf9\u8c61\u53ef\u80fd\u4f1a\u4f7f\u75282\u30014\u30018\u6216\u8005\u66f4\u591a\u8fde\u7eed\u7684\u5185\u5b58\u5730\u5740\u3002\u800c\u5bf9\u8c61\u4f7f\u7528\u591a\u5927\u7684\u5185\u5b58\uff0c\u53d6\u51b3\u4e8e\u5bf9\u8c61\u7684\u6570\u636e\u7c7b\u578b\u3002 \u56e0\u4e3a\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u53d8\u91cf\u540d\u6765\u8bbf\u95ee\u5185\u5b58\uff08\u800c\u975e\u76f4\u63a5\u8bbf\u95ee\u5185\u5b58\uff09\uff0c\u7f16\u8bd1\u5668\u5e2e\u6211\u4eec\u9690\u85cf\u4e86\u4e0a\u8ff0\u7ec6\u8282\uff0c\u5373\u4e00\u4e2a\u5bf9\u8c61\u5360\u7528\u591a\u5c11\u5185\u5b58\u3002\u5f53\u4f7f\u7528\u53d8\u91cf x \u8bbf\u95ee\u5176\u5bf9\u5e94\u7684\u5185\u5b58\u65f6\uff0c\u7f16\u8bd1\u5668\u6307\u5b9a\u5e94\u8be5\u83b7\u53d6\u591a\u5c11\u5b57\u8282\u7684\u6570\u636e\uff08\u57fa\u4e8e x \u7684\u6570\u636e\u7c7b\u578b\uff09\uff0c\u5b83\u4e3a\u6211\u4eec\u5b8c\u6210\u4e86\u4e0a\u8ff0\u5de5\u4f5c\u3002 \u5373\u4f7f\u8fd9\u4e5f\uff0c\u5f88\u591a\u65f6\u5019\u77e5\u9053\u5bf9\u8c61\u5360\u7528\u591a\u5c11\u5185\u5b58\u662f\u6709\u7528\u7684\u3002 \u9996\u5148\uff0c\u5bf9\u8c61\u5360\u7528\u7684\u5185\u5b58\u51b3\u5b9a\u4e86\u8be5\u5bf9\u8c61\u80fd\u591f\u4fdd\u5b58\u591a\u5c11\u4fe1\u606f\u3002 \u4e00\u4e2a\u6bd4\u7279\u53ef\u4ee5\u4fdd\u5b58\u4e24\u4e2a\u53ef\u80fd\u7684\u503c\uff0c\u53730\u62161\uff1a bit 0 0 1 bit 0 bit 1 0 0 1 1 1 0 1 1 bit 0 bit 1 bit 2 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 \u6982\u62ec\u6765\u8bf4\uff0c\u5177\u6709 n \u4e2a \u4f4d \u5927\u5c0f\u7684\u5bf9\u8c61\uff08n\u4e3a\u6b63\u6574\u6570\uff09\u53ef\u4ee5\u4fdd\u5b58 \\(2^n\\) \u4e2d\u4e0d\u540c\u7684\u503c\uff082\u7684n\u6b21\u65b9\uff0c\u4e00\u822c\u4e5f\u5199\u4f5c 2^n \uff09\u3002\u56e0\u6b64\uff0c\u5bf9\u4e8e\u4e00\u4e2a8\u4e2a\u6bd4\u7279\u7684\u5b57\u8282\u6765\u8bf4\uff0c\u5927\u5c0f\u4f4d1\u5b57\u8282\u7684\u5bf9\u8c61\u53ef\u4ee5\u4fdd\u5b58 \\(2^8\\) (256)\u4e2d\u53ef\u80fd\u7684\u503c\u3002\u4f7f\u75282\u4e2a\u5b57\u8282\u7684\u5bf9\u8c61\u53ef\u4ee5\u4fdd\u5b58 \\(2^{16}\\) (65536)\u79cd\u4e0d\u540c\u7684\u503c\uff01 \u56e0\u6b64\uff0c\u5bf9\u8c61\u7684\u5927\u5c0f\u51b3\u5b9a\u4e86\u5b83\u80fd\u4fdd\u5b58\u7684\u4e0d\u540c\u503c\u7684\u4e0a\u9650\u2014\u2014\u4f7f\u7528\u7684\u5185\u5b58\u8d8a\u591a\uff0c\u80fd\u591f\u5b58\u653e\u7684\u4e0d\u540c\u503c\u4e5f\u5c31\u8d8a\u591a\u3002\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u5bf9\u6574\u578b\u8fdb\u884c\u66f4\u8be6\u7ec6\u5730\u4ecb\u7ecd\u3002 \u5176\u6b21\uff0c\u8ba1\u7b97\u673a\u7684\u5185\u5b58\u662f\u6709\u9650\u7684\u3002\u5f53\u6211\u4eec\u5b9a\u4e49\u5bf9\u8c61\u7684\u65f6\u5019\uff0c\u4e00\u5c0f\u5757\u7a7a\u95f2\u7684\u5185\u5b58\u5c31\u88ab\u5360\u7528\u4e86\uff08\u53ea\u8981\u8be5\u5bf9\u8c61\u8fd8\u5b58\u5728\uff09\u3002\u56e0\u4e3a\u73b0\u4ee3\u8ba1\u7b97\u673a\u7684\u5185\u5b58\u76f8\u5bf9\u6765\u8bf4\u6bd4\u8f83\u591a\uff0c\u6240\u4ee5\u5360\u7528\u4e00\u5c0f\u5757\u5185\u5b58\u5f71\u54cd\u4e0d\u5927\u3002\u4f46\u662f\uff0c\u5f53\u7a0b\u5e8f\u4e2d\u5177\u6709\u5927\u91cf\u5bf9\u8c61\u6216\u8005\u6570\u636e\u65f6\uff08\u4f8b\u5982\uff0c\u4e00\u4e2a\u6b63\u5728\u6e32\u67d3\u51e0\u767e\u4e07\u4e2a\u5730\u7262\u7684\u5927\u578b\u6e38\u620f\uff09\uff0c\u5bf9\u8c61\u5360\u75281\u4e2a\u5b57\u8282\u7684\u5927\u5c0f\u8fd8\u662f8\u4e2a\u5b57\u8282\u5c31\u4f1a\u9020\u6210\u5f88\u5927\u7684\u5dee\u522b\u3002 \u5173\u952e\u4fe1\u606f \u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u8fc7\u5206\u5173\u6ce8\u4f18\u5316\u4ed6\u4eec\u7684\u4ee3\u7801\u5e76\u4f7f\u5176\u7a0b\u5e8f\u80fd\u591f\u4f7f\u7528\u5c3d\u53ef\u80fd\u5c11\u7684\u5185\u5b58\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u5176\u5b9e\u5f71\u54cd\u4e0d\u5927\u3002\u4f60\u7684\u6ce8\u610f\u529b\u5e94\u8be5\u653e\u5728\u7f16\u5199\u6613\u4e8e\u7ef4\u62a4\u7684\u4ee3\u7801\uff0c\u53ea\u6709\u5f53\u4f18\u5316\u53ef\u4ee5\u5e26\u6765\u663e\u8457\u6536\u76ca\u7684\u65f6\u5019\u518d\u53bb\u505a\u3002 \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7684\u5927\u5c0f \u63a5\u4e0b\u6765\u7684\u95ee\u9898\u5f88\u663e\u7136\uff1a\u201d\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7a76\u7adf\u5360\u7528\u591a\u5c11\u5185\u5b58\uff1f\u201c\u3002\u7b54\u6848\u53ef\u80fd\u4f1a\u4ee4\u4f60\u5403\u60ca\uff0c\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u5360\u7528\u7684\u5185\u5b58\u548c\u7f16\u8bd1\u5668\u4ee5\u53ca\uff08\u6216\uff09\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u6709\u5173\u3002 C++ \u53ea\u4fdd\u8bc1\u4e86\u6bcf\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\u6700\u5c0f\u7684\u5c3a\u5bf8\uff1a \u5206\u7c7b \u7c7b\u578b \u6700\u5c0f\u5c3a\u5bf8 \u5907\u6ce8 boolean bool 1 byte character char 1 byte \u603b\u662f1\u4e2a\u5b57\u8282 character wchat_t 1 byte character char16_t 2 byte character char32_t 4 byte integer short 2 byte integer int 2 byte integer long 4 byte integer long long 8 byte floating point float 4 byte floating point double 8 byte floating point long double 8 byte \u4e0d\u7136\uff0c\u8fd9\u4e9b\u53d8\u91cf\u7684\u5b9e\u9645\u5927\u5c0f\u548c\u4f60\u7684\u673a\u5668\u76f8\u5173\uff08\u4f8b\u5982 int \u901a\u5e38\u5c31\u662f4\u4e2a\u5b57\u8282\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u5904\u4e8e\u517c\u5bb9\u6027\u8003\u8651\uff0c\u4f60\u4e0d\u80fd\u591f\u5047\u8bbe\u53d8\u91cf\u7684\u5b9e\u9645\u5927\u5c0f\u4f1a\u5927\u4e8e\u5176\u6700\u5c0f\u503c\u3002 sizeof \u8fd0\u7b97\u7b26 \u4e3a\u4e86\u786e\u5b9a\u6570\u636e\u7c7b\u578b\u5728\u5177\u4f53\u673a\u5668\u4e0a\u5360\u7528\u7684\u5185\u5b58\u5927\u5c0f\uff0cC++ \u63d0\u4f9b\u4e86 size \u8fd0\u7b97\u7b26\u3002\u8be5\u8fd0\u7b97\u7b26\u662f\u4e00\u4e2a\u4e00\u5143\u8fd0\u7b97\u7b26\uff0c\u5b83\u4f7f\u7528\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\u6216\u53d8\u91cf\u4f5c\u4e3a \u64cd\u4f5c\u6570(operands) \uff0c\u8ba1\u7b97\u5f97\u5230\u8be5\u6570\u636e\u7c7b\u578b\u6216\u53d8\u91cf\u5360\u7528\u5185\u5b58\u7684\u5927\u5c0f\uff08\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\uff09\u3002\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u7f16\u8bd1\u548c\u8fd0\u884c\u4e0b\u9762\u7684\u7a0b\u5e8f\u4ee5\u4fbf\u786e\u5b9a\u5728\u4f60\u7684\u7535\u8111\u4e0a\uff0c\u5404\u4e2a\u6570\u636e\u7c7b\u578b\u90fd\u5360\u7528\u591a\u5c11\u5185\u5b58\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { std :: cout << \"bool: \\t\\t \" << sizeof ( bool ) << \" bytes \\n \" ; std :: cout << \"char: \\t\\t \" << sizeof ( char ) << \" bytes \\n \" ; std :: cout << \"wchar_t: \\t \" << sizeof ( wchar_t ) << \" bytes \\n \" ; std :: cout << \"char16_t: \\t \" << sizeof ( char16_t ) << \" bytes \\n \" ; std :: cout << \"char32_t: \\t \" << sizeof ( char32_t ) << \" bytes \\n \" ; std :: cout << \"short: \\t\\t \" << sizeof ( short ) << \" bytes \\n \" ; std :: cout << \"int: \\t\\t \" << sizeof ( int ) << \" bytes \\n \" ; std :: cout << \"long: \\t\\t \" << sizeof ( long ) << \" bytes \\n \" ; std :: cout << \"long long: \\t \" << sizeof ( long long ) << \" bytes \\n \" ; std :: cout << \"float: \\t\\t \" << sizeof ( float ) << \" bytes \\n \" ; std :: cout << \"double: \\t\\t \" << sizeof ( double ) << \" bytes \\n \" ; std :: cout << \"long double: \\t \" << sizeof ( long double ) << \" bytes \\n \" ; return 0 ; } \u4f5c\u8005\u4f7f\u7528\u7684\u662f 64 \u4f4d\u673a\u5668\uff0c\u901a\u8fc7 Visual Studio \u7f16\u8bd1\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u8f93\u51fa\u7684\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 bool: 1 bytes char: 1 bytes wchar_t: 2 bytes char16_t: 2 bytes char32_t: 4 bytes short: 2 bytes int: 4 bytes long: 4 bytes long long: 8 bytes float: 4 bytes double: 8 bytes long double: 8 bytes \u4f60\u5f97\u5230\u7684\u7ed3\u679c\u53ef\u80fd\u4f1a\u548c\u6b64\u5904\u4e0d\u4e00\u6837\uff0c\u8fd9\u53d6\u51b3\u4e8e\u662f\u5426\u4f7f\u7528\u4e86\u4e0d\u540c\u7684\u8ba1\u7b97\u673a\u6216\u7f16\u8bd1\u5668\u3002\u6ce8\u610f\uff1a\u4f60\u4e0d\u80fd\u4f7f\u7528 sizeof \u8ba1\u7b97 void \u7684\u5927\u5c0f\uff0c\u56e0\u4e3a\u5b83\u6ca1\u6709\u5927\u5c0f\uff08\u8fd9\u4e48\u505a\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\uff09\u3002 \u6269\u5c55\u9605\u8bfb \u4e5f\u8bb8\u4f60\u6709\u5174\u8da3\u77e5\u9053\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u7684 \\t \u6709\u4ec0\u4e48\u4f5c\u7528\u3002\u8fd9\u662f\u4e00\u4e2a\u7279\u6b8a\u7b26\u53f7\uff0c\u7528\u4e8e\u63d2\u5165\u4e00\u4e2a\u5236\u8868\u7b26\uff08\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b83\u5c06\u8f93\u51fa\u7ed3\u679c\u6309\u5217\u5bf9\u5176\uff09\u3002\u6211\u4eec\u4f1a\u5728 4.11 - \u5b57\u7b26 \u4e2d\u4ecb\u7ecd \\t \u548c\u5176\u4ed6\u7684\u7279\u6b8a\u5b57\u7b26\u3002 \u4f60\u4e5f\u53ef\u4ee5\u5bf9\u53d8\u91cf\u540d\u4f7f\u7528 sizeof \u3002 1 2 3 4 5 6 7 8 9 #include int main () { int x {}; std :: cout << \"x is \" << sizeof ( x ) << \" bytes \\n \" ; return 0 ; } 1 x is 4 bytes \u8bd1\u8005\u6ce8 \u5bf9\u6307\u9488\u4f7f\u7528 sizeof \u7684\u7ed3\u679c\u662f\u56fa\u5b9a\u7684\uff0c\u4e0d\u53d6\u51b3\u4e8e\u6307\u9488\u6307\u5411\u4ec0\u4e48\u7c7b\u578b\u7684\u6570\u636e\u3002\u901a\u5e38\u4e3a4\u6216\u80058\u4e2a\u5b57\u8282\u3002 \u57fa\u672c\u6570\u636e\u7ed3\u6784\u7684\u6027\u80fd \u5728\u73b0\u4ee3\u8ba1\u7b97\u673a\u4e0a\uff0c\u57fa\u7840\u6570\u636e\u7c7b\u578b\u7684\u5bf9\u8c61\u6027\u80fd\u901a\u5e38\u90fd\u975e\u5e38\u597d\uff0c\u56e0\u6b64\u8fd9\u4e9b\u53d8\u91cf\u7684\u6027\u80fd\u95ee\u9898\u901a\u5e38\u4e0d\u9700\u8981\u6211\u4eec\u7279\u522b\u8003\u8651\u3002 \u9898\u5916\u8bdd \u4f60\u53ef\u80fd\u4f1a\u60f3\u5f53\u7136\u5730\u8ba4\u4e3a\u4f7f\u7528\u5185\u5b58\u5c11\u7684\u6570\u636e\u7ed3\u6784\u901f\u5ea6\u4e5f\u4f1a\u66f4\u5feb\uff0c\u5b9e\u9645\u4e0a\u5e76\u4e0d\u603b\u662f\u8fd9\u6837\u3002CPU \u901a\u5e38\u4f1a\u5bf9\u6570\u636e\u7684\u6d41\u7a0b\u8fdb\u884c\u4f18\u5316\uff0c\u6309\u7167\u67d0\u4e2a\u957f\u5ea6\u6765\u5904\u7406\u6570\u636e\uff08\u4f8b\u598232\u4f4d\uff09\uff0c\u5982\u679c\u6570\u636e\u7c7b\u578b\u7684\u5927\u5c0f\u6070\u597d\u548c\u8be5\u957f\u5ea6\u76f8\u540c\uff0c\u5219\u4f1a\u66f4\u5feb\u4e00\u4e9b\u3002\u5728\u8fd9\u6837\u7684\u8ba1\u7b97\u673a\u4e0a\uff0c32\u4f4d\u7684 int \u4f1a\u6bd4 16\u4e3a\u7684 int \u66f4\u5feb\u3002","title":"4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26"},{"location":"4-3-Object-sizes-and-the-sizeof-operator/#_1","text":"\u6b63\u5982\u5728 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb \u4e2d\u4ecb\u7ecd\u7684\u90a3\u6837\uff0c\u73b0\u4ee3\u8ba1\u7b97\u673a\u4ee5 \u5b57\u8282(byte) \u4e3a\u5355\u4f4d\u4f7f\u7528\u5185\u5b58\uff0c\u6bcf\u4e2a\u5b57\u8282\u7684\u5185\u5b58\u90fd\u6709\u5176\u552f\u4e00\u5bf9\u5e94\u7684\u5730\u5740\u3002\u4ece\u8fd9\u4e2a\u89d2\u5ea6\u6765\u8bf4\uff0c\u5185\u5b58\u53ef\u4ee5\u88ab\u60f3\u8c61\u6210\u4e00\u7cfb\u5217\u7684\u5c0f\u50a8\u85cf\u95f4\u6216\u4fe1\u7bb1\uff0c\u4f60\u53ef\u4ee5\u4ece\u4e2d\u5b58\u653e\u6216\u8bfb\u53d6\u4fe1\u606f\uff0c\u800c\u53d8\u91cf\u540d\u5219\u7528\u6765\u5bf9\u8fd9\u4e9b\u50a8\u85cf\u95f4\u6216\u4fe1\u7bb1\u8fdb\u884c\u8bbf\u95ee\u3002 \u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u6bd4\u55bb\u5e76\u4e0d\u5b8c\u5168\u51c6\u786e\u2014\u2014\u5f88\u591a\u5bf9\u8c61\u5b9e\u9645\u4e0a\u5360\u636e\u7684\u5185\u5b58\u8981\u5927\u4e8e1\u4e2a\u5b57\u8282\u3002\u4e00\u4e2a\u5bf9\u8c61\u53ef\u80fd\u4f1a\u4f7f\u75282\u30014\u30018\u6216\u8005\u66f4\u591a\u8fde\u7eed\u7684\u5185\u5b58\u5730\u5740\u3002\u800c\u5bf9\u8c61\u4f7f\u7528\u591a\u5927\u7684\u5185\u5b58\uff0c\u53d6\u51b3\u4e8e\u5bf9\u8c61\u7684\u6570\u636e\u7c7b\u578b\u3002 \u56e0\u4e3a\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u53d8\u91cf\u540d\u6765\u8bbf\u95ee\u5185\u5b58\uff08\u800c\u975e\u76f4\u63a5\u8bbf\u95ee\u5185\u5b58\uff09\uff0c\u7f16\u8bd1\u5668\u5e2e\u6211\u4eec\u9690\u85cf\u4e86\u4e0a\u8ff0\u7ec6\u8282\uff0c\u5373\u4e00\u4e2a\u5bf9\u8c61\u5360\u7528\u591a\u5c11\u5185\u5b58\u3002\u5f53\u4f7f\u7528\u53d8\u91cf x \u8bbf\u95ee\u5176\u5bf9\u5e94\u7684\u5185\u5b58\u65f6\uff0c\u7f16\u8bd1\u5668\u6307\u5b9a\u5e94\u8be5\u83b7\u53d6\u591a\u5c11\u5b57\u8282\u7684\u6570\u636e\uff08\u57fa\u4e8e x \u7684\u6570\u636e\u7c7b\u578b\uff09\uff0c\u5b83\u4e3a\u6211\u4eec\u5b8c\u6210\u4e86\u4e0a\u8ff0\u5de5\u4f5c\u3002 \u5373\u4f7f\u8fd9\u4e5f\uff0c\u5f88\u591a\u65f6\u5019\u77e5\u9053\u5bf9\u8c61\u5360\u7528\u591a\u5c11\u5185\u5b58\u662f\u6709\u7528\u7684\u3002 \u9996\u5148\uff0c\u5bf9\u8c61\u5360\u7528\u7684\u5185\u5b58\u51b3\u5b9a\u4e86\u8be5\u5bf9\u8c61\u80fd\u591f\u4fdd\u5b58\u591a\u5c11\u4fe1\u606f\u3002 \u4e00\u4e2a\u6bd4\u7279\u53ef\u4ee5\u4fdd\u5b58\u4e24\u4e2a\u53ef\u80fd\u7684\u503c\uff0c\u53730\u62161\uff1a bit 0 0 1 bit 0 bit 1 0 0 1 1 1 0 1 1 bit 0 bit 1 bit 2 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 \u6982\u62ec\u6765\u8bf4\uff0c\u5177\u6709 n \u4e2a \u4f4d \u5927\u5c0f\u7684\u5bf9\u8c61\uff08n\u4e3a\u6b63\u6574\u6570\uff09\u53ef\u4ee5\u4fdd\u5b58 \\(2^n\\) \u4e2d\u4e0d\u540c\u7684\u503c\uff082\u7684n\u6b21\u65b9\uff0c\u4e00\u822c\u4e5f\u5199\u4f5c 2^n \uff09\u3002\u56e0\u6b64\uff0c\u5bf9\u4e8e\u4e00\u4e2a8\u4e2a\u6bd4\u7279\u7684\u5b57\u8282\u6765\u8bf4\uff0c\u5927\u5c0f\u4f4d1\u5b57\u8282\u7684\u5bf9\u8c61\u53ef\u4ee5\u4fdd\u5b58 \\(2^8\\) (256)\u4e2d\u53ef\u80fd\u7684\u503c\u3002\u4f7f\u75282\u4e2a\u5b57\u8282\u7684\u5bf9\u8c61\u53ef\u4ee5\u4fdd\u5b58 \\(2^{16}\\) (65536)\u79cd\u4e0d\u540c\u7684\u503c\uff01 \u56e0\u6b64\uff0c\u5bf9\u8c61\u7684\u5927\u5c0f\u51b3\u5b9a\u4e86\u5b83\u80fd\u4fdd\u5b58\u7684\u4e0d\u540c\u503c\u7684\u4e0a\u9650\u2014\u2014\u4f7f\u7528\u7684\u5185\u5b58\u8d8a\u591a\uff0c\u80fd\u591f\u5b58\u653e\u7684\u4e0d\u540c\u503c\u4e5f\u5c31\u8d8a\u591a\u3002\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u5bf9\u6574\u578b\u8fdb\u884c\u66f4\u8be6\u7ec6\u5730\u4ecb\u7ecd\u3002 \u5176\u6b21\uff0c\u8ba1\u7b97\u673a\u7684\u5185\u5b58\u662f\u6709\u9650\u7684\u3002\u5f53\u6211\u4eec\u5b9a\u4e49\u5bf9\u8c61\u7684\u65f6\u5019\uff0c\u4e00\u5c0f\u5757\u7a7a\u95f2\u7684\u5185\u5b58\u5c31\u88ab\u5360\u7528\u4e86\uff08\u53ea\u8981\u8be5\u5bf9\u8c61\u8fd8\u5b58\u5728\uff09\u3002\u56e0\u4e3a\u73b0\u4ee3\u8ba1\u7b97\u673a\u7684\u5185\u5b58\u76f8\u5bf9\u6765\u8bf4\u6bd4\u8f83\u591a\uff0c\u6240\u4ee5\u5360\u7528\u4e00\u5c0f\u5757\u5185\u5b58\u5f71\u54cd\u4e0d\u5927\u3002\u4f46\u662f\uff0c\u5f53\u7a0b\u5e8f\u4e2d\u5177\u6709\u5927\u91cf\u5bf9\u8c61\u6216\u8005\u6570\u636e\u65f6\uff08\u4f8b\u5982\uff0c\u4e00\u4e2a\u6b63\u5728\u6e32\u67d3\u51e0\u767e\u4e07\u4e2a\u5730\u7262\u7684\u5927\u578b\u6e38\u620f\uff09\uff0c\u5bf9\u8c61\u5360\u75281\u4e2a\u5b57\u8282\u7684\u5927\u5c0f\u8fd8\u662f8\u4e2a\u5b57\u8282\u5c31\u4f1a\u9020\u6210\u5f88\u5927\u7684\u5dee\u522b\u3002 \u5173\u952e\u4fe1\u606f \u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u8fc7\u5206\u5173\u6ce8\u4f18\u5316\u4ed6\u4eec\u7684\u4ee3\u7801\u5e76\u4f7f\u5176\u7a0b\u5e8f\u80fd\u591f\u4f7f\u7528\u5c3d\u53ef\u80fd\u5c11\u7684\u5185\u5b58\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u5176\u5b9e\u5f71\u54cd\u4e0d\u5927\u3002\u4f60\u7684\u6ce8\u610f\u529b\u5e94\u8be5\u653e\u5728\u7f16\u5199\u6613\u4e8e\u7ef4\u62a4\u7684\u4ee3\u7801\uff0c\u53ea\u6709\u5f53\u4f18\u5316\u53ef\u4ee5\u5e26\u6765\u663e\u8457\u6536\u76ca\u7684\u65f6\u5019\u518d\u53bb\u505a\u3002","title":"\u5bf9\u8c61\u7684\u5927\u5c0f"},{"location":"4-3-Object-sizes-and-the-sizeof-operator/#_2","text":"\u63a5\u4e0b\u6765\u7684\u95ee\u9898\u5f88\u663e\u7136\uff1a\u201d\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7a76\u7adf\u5360\u7528\u591a\u5c11\u5185\u5b58\uff1f\u201c\u3002\u7b54\u6848\u53ef\u80fd\u4f1a\u4ee4\u4f60\u5403\u60ca\uff0c\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u5360\u7528\u7684\u5185\u5b58\u548c\u7f16\u8bd1\u5668\u4ee5\u53ca\uff08\u6216\uff09\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u6709\u5173\u3002 C++ \u53ea\u4fdd\u8bc1\u4e86\u6bcf\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\u6700\u5c0f\u7684\u5c3a\u5bf8\uff1a \u5206\u7c7b \u7c7b\u578b \u6700\u5c0f\u5c3a\u5bf8 \u5907\u6ce8 boolean bool 1 byte character char 1 byte \u603b\u662f1\u4e2a\u5b57\u8282 character wchat_t 1 byte character char16_t 2 byte character char32_t 4 byte integer short 2 byte integer int 2 byte integer long 4 byte integer long long 8 byte floating point float 4 byte floating point double 8 byte floating point long double 8 byte \u4e0d\u7136\uff0c\u8fd9\u4e9b\u53d8\u91cf\u7684\u5b9e\u9645\u5927\u5c0f\u548c\u4f60\u7684\u673a\u5668\u76f8\u5173\uff08\u4f8b\u5982 int \u901a\u5e38\u5c31\u662f4\u4e2a\u5b57\u8282\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u5904\u4e8e\u517c\u5bb9\u6027\u8003\u8651\uff0c\u4f60\u4e0d\u80fd\u591f\u5047\u8bbe\u53d8\u91cf\u7684\u5b9e\u9645\u5927\u5c0f\u4f1a\u5927\u4e8e\u5176\u6700\u5c0f\u503c\u3002","title":"\u57fa\u7840\u6570\u636e\u7c7b\u578b\u7684\u5927\u5c0f"},{"location":"4-3-Object-sizes-and-the-sizeof-operator/#sizeof","text":"\u4e3a\u4e86\u786e\u5b9a\u6570\u636e\u7c7b\u578b\u5728\u5177\u4f53\u673a\u5668\u4e0a\u5360\u7528\u7684\u5185\u5b58\u5927\u5c0f\uff0cC++ \u63d0\u4f9b\u4e86 size \u8fd0\u7b97\u7b26\u3002\u8be5\u8fd0\u7b97\u7b26\u662f\u4e00\u4e2a\u4e00\u5143\u8fd0\u7b97\u7b26\uff0c\u5b83\u4f7f\u7528\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\u6216\u53d8\u91cf\u4f5c\u4e3a \u64cd\u4f5c\u6570(operands) \uff0c\u8ba1\u7b97\u5f97\u5230\u8be5\u6570\u636e\u7c7b\u578b\u6216\u53d8\u91cf\u5360\u7528\u5185\u5b58\u7684\u5927\u5c0f\uff08\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\uff09\u3002\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u7f16\u8bd1\u548c\u8fd0\u884c\u4e0b\u9762\u7684\u7a0b\u5e8f\u4ee5\u4fbf\u786e\u5b9a\u5728\u4f60\u7684\u7535\u8111\u4e0a\uff0c\u5404\u4e2a\u6570\u636e\u7c7b\u578b\u90fd\u5360\u7528\u591a\u5c11\u5185\u5b58\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { std :: cout << \"bool: \\t\\t \" << sizeof ( bool ) << \" bytes \\n \" ; std :: cout << \"char: \\t\\t \" << sizeof ( char ) << \" bytes \\n \" ; std :: cout << \"wchar_t: \\t \" << sizeof ( wchar_t ) << \" bytes \\n \" ; std :: cout << \"char16_t: \\t \" << sizeof ( char16_t ) << \" bytes \\n \" ; std :: cout << \"char32_t: \\t \" << sizeof ( char32_t ) << \" bytes \\n \" ; std :: cout << \"short: \\t\\t \" << sizeof ( short ) << \" bytes \\n \" ; std :: cout << \"int: \\t\\t \" << sizeof ( int ) << \" bytes \\n \" ; std :: cout << \"long: \\t\\t \" << sizeof ( long ) << \" bytes \\n \" ; std :: cout << \"long long: \\t \" << sizeof ( long long ) << \" bytes \\n \" ; std :: cout << \"float: \\t\\t \" << sizeof ( float ) << \" bytes \\n \" ; std :: cout << \"double: \\t\\t \" << sizeof ( double ) << \" bytes \\n \" ; std :: cout << \"long double: \\t \" << sizeof ( long double ) << \" bytes \\n \" ; return 0 ; } \u4f5c\u8005\u4f7f\u7528\u7684\u662f 64 \u4f4d\u673a\u5668\uff0c\u901a\u8fc7 Visual Studio \u7f16\u8bd1\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u8f93\u51fa\u7684\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 bool: 1 bytes char: 1 bytes wchar_t: 2 bytes char16_t: 2 bytes char32_t: 4 bytes short: 2 bytes int: 4 bytes long: 4 bytes long long: 8 bytes float: 4 bytes double: 8 bytes long double: 8 bytes \u4f60\u5f97\u5230\u7684\u7ed3\u679c\u53ef\u80fd\u4f1a\u548c\u6b64\u5904\u4e0d\u4e00\u6837\uff0c\u8fd9\u53d6\u51b3\u4e8e\u662f\u5426\u4f7f\u7528\u4e86\u4e0d\u540c\u7684\u8ba1\u7b97\u673a\u6216\u7f16\u8bd1\u5668\u3002\u6ce8\u610f\uff1a\u4f60\u4e0d\u80fd\u4f7f\u7528 sizeof \u8ba1\u7b97 void \u7684\u5927\u5c0f\uff0c\u56e0\u4e3a\u5b83\u6ca1\u6709\u5927\u5c0f\uff08\u8fd9\u4e48\u505a\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\uff09\u3002 \u6269\u5c55\u9605\u8bfb \u4e5f\u8bb8\u4f60\u6709\u5174\u8da3\u77e5\u9053\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u7684 \\t \u6709\u4ec0\u4e48\u4f5c\u7528\u3002\u8fd9\u662f\u4e00\u4e2a\u7279\u6b8a\u7b26\u53f7\uff0c\u7528\u4e8e\u63d2\u5165\u4e00\u4e2a\u5236\u8868\u7b26\uff08\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b83\u5c06\u8f93\u51fa\u7ed3\u679c\u6309\u5217\u5bf9\u5176\uff09\u3002\u6211\u4eec\u4f1a\u5728 4.11 - \u5b57\u7b26 \u4e2d\u4ecb\u7ecd \\t \u548c\u5176\u4ed6\u7684\u7279\u6b8a\u5b57\u7b26\u3002 \u4f60\u4e5f\u53ef\u4ee5\u5bf9\u53d8\u91cf\u540d\u4f7f\u7528 sizeof \u3002 1 2 3 4 5 6 7 8 9 #include int main () { int x {}; std :: cout << \"x is \" << sizeof ( x ) << \" bytes \\n \" ; return 0 ; } 1 x is 4 bytes \u8bd1\u8005\u6ce8 \u5bf9\u6307\u9488\u4f7f\u7528 sizeof \u7684\u7ed3\u679c\u662f\u56fa\u5b9a\u7684\uff0c\u4e0d\u53d6\u51b3\u4e8e\u6307\u9488\u6307\u5411\u4ec0\u4e48\u7c7b\u578b\u7684\u6570\u636e\u3002\u901a\u5e38\u4e3a4\u6216\u80058\u4e2a\u5b57\u8282\u3002","title":"sizeof \u8fd0\u7b97\u7b26"},{"location":"4-3-Object-sizes-and-the-sizeof-operator/#_3","text":"\u5728\u73b0\u4ee3\u8ba1\u7b97\u673a\u4e0a\uff0c\u57fa\u7840\u6570\u636e\u7c7b\u578b\u7684\u5bf9\u8c61\u6027\u80fd\u901a\u5e38\u90fd\u975e\u5e38\u597d\uff0c\u56e0\u6b64\u8fd9\u4e9b\u53d8\u91cf\u7684\u6027\u80fd\u95ee\u9898\u901a\u5e38\u4e0d\u9700\u8981\u6211\u4eec\u7279\u522b\u8003\u8651\u3002 \u9898\u5916\u8bdd \u4f60\u53ef\u80fd\u4f1a\u60f3\u5f53\u7136\u5730\u8ba4\u4e3a\u4f7f\u7528\u5185\u5b58\u5c11\u7684\u6570\u636e\u7ed3\u6784\u901f\u5ea6\u4e5f\u4f1a\u66f4\u5feb\uff0c\u5b9e\u9645\u4e0a\u5e76\u4e0d\u603b\u662f\u8fd9\u6837\u3002CPU \u901a\u5e38\u4f1a\u5bf9\u6570\u636e\u7684\u6d41\u7a0b\u8fdb\u884c\u4f18\u5316\uff0c\u6309\u7167\u67d0\u4e2a\u957f\u5ea6\u6765\u5904\u7406\u6570\u636e\uff08\u4f8b\u598232\u4f4d\uff09\uff0c\u5982\u679c\u6570\u636e\u7c7b\u578b\u7684\u5927\u5c0f\u6070\u597d\u548c\u8be5\u957f\u5ea6\u76f8\u540c\uff0c\u5219\u4f1a\u66f4\u5feb\u4e00\u4e9b\u3002\u5728\u8fd9\u6837\u7684\u8ba1\u7b97\u673a\u4e0a\uff0c32\u4f4d\u7684 int \u4f1a\u6bd4 16\u4e3a\u7684 int \u66f4\u5feb\u3002","title":"\u57fa\u672c\u6570\u636e\u7ed3\u6784\u7684\u6027\u80fd"},{"location":"4-4-Signed-integers/","tags":["data type","integer","undefined behavior"],"text":"Key Takeaway n \u4f4d\u6709\u7b26\u53f7\u6574\u578b\u7684\u8303\u56f4\u4e3a \\(-(2^{n-1})\\) \u5230 \\(2^{n-1}-1\\) \u3002 \u6709\u7b26\u53f7\u6574\u578b\u7684\u6ea2\u51fa\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u6574\u578b\u662f\u4e00\u79cd\u6574\u6570\u7c7b\u578b\uff0c\u5b83\u53ef\u4ee5\u8868\u793a\u6b63\u6574\u6570\u548c\u8d1f\u6574\u6570\uff0c\u5f53\u7136\u4e5f\u5305\u62ec 0\uff08\u4f8b\u5982\uff1a-2\u3001-1\u30010\u30011\u30012\uff09\u3002C++ \u4e2d\u6709 4 \u4e2d\u57fa\u7840\u6574\u578b\uff1a Type Minimum Size Note short 16 bits int 16 bits \u73b0\u4ee3\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u4e0b\u901a\u5e38\u4e3a 32 \u4f4d long 32 bits long long 64 bits \u8fd9\u4e9b\u7c7b\u578b\u7684\u4e3b\u8981\u5dee\u5f02\u662f\u5b83\u4eec\u7684\u5927\u5c0f\u4e0d\u540c\u2014\u2014\u8d8a\u5927\u7684\u6574\u578b\u53ef\u4ee5\u8868\u793a\u8d8a\u5927\u7684\u6570\u3002 \u63d0\u9192 C++ \u53ea\u4fdd\u8bc1\u6574\u578b\u5360\u7528\u7a7a\u95f4\u7684\u6700\u5c0f\u503c\uff0c\u800c\u4e0d\u4fdd\u8bc1\u5176\u5177\u4f53\u503c\u3002\u8bf7\u53c2\u8003 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 \u6765\u786e\u5b9a\u6bcf\u79cd\u7c7b\u578b\u5728\u4f60\u673a\u5668\u4e0a\u7684\u5177\u4f53\u5927\u5c0f\u3002 \u6709\u7b26\u53f7\u6574\u578b \u5728\u65e5\u5e38\u751f\u6d3b\u4e2d\uff0c\u5982\u679c\u6211\u4eec\u8981\u8868\u793a\u8d1f\u6574\u6570\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528\u8d1f\u53f7\u3002\u4f8b\u5982\uff0c -3 \u8868\u793a \u201c\u8d1f\u4e09\u201d\u3002\u6211\u4eec\u8fd8\u4f1a\u4f7f\u7528+3 \u6765\u8868\u793a\u201d\u6b63\u4e09\u201c\uff08\u5c3d\u7ba1\u901a\u5e38\u6b63\u53f7\u4f1a\u88ab\u7701\u7565\uff09\u3002\u6570\u5b57\u7684\u8fd9\u79cd\u201c\u6b63\u8d1f\u96f6\u201d\u5c5e\u6027\uff0c\u79f0\u4e3a\u6570\u5b57\u7684\u8d1f\u53f7\u3002 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6574\u578b\u662f\u6709\u7b26\u53f7\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\u6570\u5b57\u7684\u7b26\u53f7\u662f\u6570\u5b57\u672c\u8eab\u7684\u4e00\u90e8\u5206\uff08\u4f7f\u7528\u4e00\u4e2a\u4f4d\u8fdb\u884c\u5b58\u50a8\uff0c\u79f0\u4e3a\u7b26\u53f7\u4f4d\uff09\u3002\u56e0\u6b64\uff0c\u6709\u7b26\u53f7\u6574\u578b\u53ef\u4ee5\u8868\u793a\u6b63\u6570\u8d1f\u6570\u548c 0\u3002 \u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u4e13\u6ce8\u4e8e\u4ecb\u7ecd\u6709\u7b26\u53f7\u6574\u578b(signed)\u3002\u65e0\u7b26\u53f7(unsigned)\u6574\u578b\uff08\u53ea\u80fd\u4fdd\u5b58\u975e\u8d1f\u503c\uff09\u5219\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u8ba8\u8bba\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u5728 O.4 -- Converting between binary and decimal \u4e2d\u8ba8\u8bba\u4e86\u4e8c\u8fdb\u5236\u8868\u793a\u6cd5\u65f6\u7b26\u53f7\u4f4d\u5982\u4f55\u4f7f\u7528\u3002 \u5b9a\u4e49\u6709\u7b26\u53f7\u6574\u578b \u5b9a\u4e49\u6709\u7b26\u53f7\u6574\u578b\u65f6\uff0c\u63a8\u8350\u7684\u65b9\u5f0f\u5982\u4e0b\uff1a 1 2 3 4 short s ; int i ; long l ; long long ll ; \u6240\u6709\u7684\u6574\u578b(\u9664\u4e86 int )\u4ee5\u5916\uff0c\u90fd\u53ef\u4ee5\u5e26\u4e0a\u4e00\u4e2a\u53ef\u9009\u7684 int \u540e\u7f00\uff1a 1 2 3 short int si ; //short int \u4e2d\u7684 int \u5373\u53ef\u9009\u540e\u7f00 long int li ; long long int lli ; \u4e0d\u8fc7\uff0c\u5e76\u4e0d\u63a8\u8350\u4f7f\u7528\u8fd9\u4e9b\u540e\u7f00\u3002\u4f7f\u7528\u8fd9\u4e9b\u540e\u7f00\u4e0d\u4ec5\u9700\u8981\u6253\u66f4\u591a\u5b57\uff0c\u800c\u4e14\u6dfb\u52a0 int \u540e\u7f00\u540e\uff0c\u4e0d\u5bb9\u6613\u5c06\u8fd9\u4e9b\u7c7b\u578b\u548c int \u7c7b\u578b\u7684\u53d8\u91cf\u533a\u5206\u5f00\u6765\uff0c\u800c\u4e14\u5982\u679c\u4e0d\u7ecf\u610f\u5fd8\u8bb0\u4e86 short \u6216\u8005 long \u4fee\u9970\u7b26\u7684\u8bdd\u5219\u53ef\u80fd\u4f1a\u5f15\u5165\u95ee\u9898\u3002 \u6574\u578b\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u53ef\u9009\u7684 singed \u5173\u952e\u5b57\uff0c\u901a\u5e38\u4f1a\u5c06\u5176\u653e\u7f6e\u5728\u7c7b\u578b\u540d\u524d\uff1a 1 2 3 4 signed short ss ; signed int si ; signed long sl ; signed long long sll ; \u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u5173\u952e\u5b57\u4e5f\u4e0d\u63a8\u8350\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u662f\u591a\u4f59\u7684\uff0c\u6574\u578b\u9ed8\u8ba4\u5c31\u662f\u6709\u7b26\u53f7\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u6700\u7cbe\u7b80\u7684\u5199\u6cd5\uff0c\u4e0d\u8981\u4f7f\u7528 int \u540e\u7f00\u6216 signed \u524d\u7f00\u3002 \u6709\u7b26\u53f7\u6574\u578b\u7684\u8303\u56f4 \u6b63\u5982\u4e4b\u524d\u4ecb\u7ecd\u7684\u90a3\u6837\uff0c\u5728\u5185\u5b58\u4e2d\u5360\u7528 n \u4f4d\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u4fdd\u5b58 \\(2^n\\) \u4e2a\u53ef\u80fd\u7684\u503c\u3002\u4f46\u662f\uff0c\u5177\u4f53\u662f\u54ea\u4e9b\u503c\u5462\uff1f\u6211\u4eec\u79f0\u4e00\u79cd\u7c7b\u578b\u53ef\u4ee5\u8868\u793a\u7684\u503c\u4e3a\u5176\u8303\u56f4\u3002\u800c\u6574\u578b\u7684\u8303\u56f4\u6709\u4e24\u4e2a\u56e0\u7d20\u51b3\u5b9a\uff1a\u5927\u5c0f\uff08\u591a\u5c11\u4f4d\uff09\uff0c\u662f\u5426\u6709\u7b26\u53f7\u3002 \u6839\u636e\u5b9a\u4e49\uff0c8 \u4f4d\u6709\u7b26\u53f7\u6574\u578b\u53ef\u4ee5\u8868\u793a\u7684\u8303\u56f4\u662f\uff1a-128 \u5230 127\u3002\u4e5f\u5c31\u662f\u8bf4\u4e00\u4e2a\u6709\u7b26\u53f7\u6574\u578b\u53ef\u4ee5\u5b89\u5168\u5730\u8868\u793a -128 \u5230 127 (\u5305\u62ec) \u4e2d\u7684\u4efb\u610f\u6574\u6570\u3002 \u9898\u5916\u8bdd \u6570\u5b66\u65f6\u95f4\uff1a8 \u4f4d\u6574\u578b\u6709 8 \u4e2a\u4f4d\uff0c2 \u7684 8 \u6b21\u65b9\u662f 256\uff0c\u6240\u4ee5\u5b83\u4e00\u5171\u53ef\u4ee5\u4fdd\u5b58 256 \u4e2a\u53ef\u80fd\u7684\u503c\u3002\u5728 -128 \u5230 127 \u4e2d\u4e00\u5171\u6709 256 \u4e2a\u53ef\u80fd\u7684\u6574\u6570\u503c\u3002 \u4e0b\u8868\u5c55\u793a\u4e86\u4e0d\u540c\u6709\u7b26\u53f7\u6574\u578b\u53ef\u4ee5\u8868\u793a\u7684\u8303\u56f4\uff1a \u5927\u5c0f/\u7c7b\u578b \u8303\u56f4 8 bit signed -128 \u5230 127 16 bit signed -32,768 \u5230 32,767 32 bit signed -2,147,483,648 \u5230 2,147,483,647 64 bit signed -9,223,372,036,854,775,808 \u5230 9,223,372,036,854,775,807 \u5bf9\u4e8e\u559c\u6b22\u6570\u5b66\u8868\u793a\u6cd5\u7684\u4eba\u6765\u8bf4\uff1a n \u4f4d\u6709\u7b26\u53f7\u6574\u578b\u7684\u8303\u56f4\u4e3a \\(-(2^{n-1})\\) \u5230 \\(2^{n-1}-1\\) \u3002 \u5bf9\u4e8e\u4e0d\u559c\u6b22\u6570\u5b66\u7684\u4eba\u6765\u8bf4\uff0c\u53c2\u8003\u4e0a\u8868\u5c31\u53ef\u4ee5\u4e86\u3002 \u6574\u6570\u6ea2\u51fa \u5982\u679c\u6211\u4eec\u5c1d\u8bd5\u628a 280 \u8d4b\u503c\u7ed9\u4e00\u4e2a 8 \u4f4d\u6709\u7b26\u53f7\u6574\u578b\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f\u8fd9\u4e2a\u6570\u5b57\u5df2\u7ecf\u8d85\u51fa\u4e86 8 \u4f4d\u6709\u7b26\u53f7\u6574\u578b\u6570\u80fd\u591f\u8868\u793a\u7684\u8303\u56f4\u3002280 \u9700\u8981 9 \u4e2a\u4f4d\uff08\u52a0\u4e00\u4e2a\u7b26\u53f7\u4f4d\uff09\u624d\u80fd\u8868\u793a\uff0c\u4f46\u662f\u4e00\u4e2a8\u4f4d\u6574\u578b\u53ea\u67097\u4e2a\u4f4d\uff08\u52a0\u4e00\u4e2a\u7b26\u53f7\u4f4d\uff09\u3002 \u5f53\u9700\u8981\u5b58\u50a8\u7684\u503c\u7684\u8303\u56f4\u8d85\u8fc7\u4e86\u53d8\u91cf\u53ef\u4ee5\u8868\u793a\u7684\u8303\u56f4\uff0c\u5c31\u4f1a\u53d1\u751f\u6574\u6570\u6ea2\u51fa(\u901a\u5e38\u7b80\u79f0\u4e3a\u6ea2\u51fa) \u3002\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u8868\u793a\u7684\u6570\uff0c\u9700\u8981\u6bd4\u8be5\u53d8\u91cf\u80fd\u591f\u4f7f\u7528\u7684\u66f4\u591a\u7684\u4f4d\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53d8\u91cf\u5c06\u6ca1\u6709\u8db3\u591f\u7684\u5185\u5b58\u7a7a\u95f4\u53ef\u4ee5\u7528\u6765\u5b58\u653e\u6570\u636e\u3002 \u5bf9\u4e8e\u6709\u7b26\u53f7\u6574\u578b\u6765\u8bf4\uff0c\u7a76\u7adf\u54ea\u4e2a\u4f4d\u7684\u6570\u636e\u4f1a\u88ab\u4e22\u5f03\u5e76\u6ca1\u6709\u660e\u786e\u7684\u5b9a\u4e49\uff0c\u56e0\u6b64\u6709\u7b26\u53f7\u6574\u578b\u7684\u6ea2\u51fa\u662f\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 \u6ce8\u610f \u6709\u7b26\u53f7\u6574\u578b\u7684\u6ea2\u51fa\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a \u901a\u5e38\u6765\u8bf4\uff0c\u6ea2\u51fa\u4f1a\u5bfc\u81f4\u4fe1\u606f\u7684\u4e22\u5931\uff0c\u8fd9\u662f\u6211\u4eec\u6700\u4e0d\u5e0c\u671b\u53d1\u751f\u7684\u662f\u3002\u5982\u679c\u4f60\u6000\u7591\u5bf9\u8c61\u9700\u8981\u4fdd\u5b58\u7684\u503c\u6709\u53ef\u80fd\u4f1a\u8d85\u8fc7\u5b83\u80fd\u591f\u8868\u793a\u7684\u8303\u56f4\uff0c\u8bf7\u4f7f\u7528\u8303\u56f4\u66f4\u5927\u7684\u7c7b\u578b\uff01 \u6574\u578b\u9664\u6cd5 \u5f53\u5bf9\u4e24\u4e2a\u6574\u578b\u8fdb\u884c\u9664\u6cd5\u8fd0\u7b97\u65f6\uff0c \u5982\u679c\u5546\u662f\u6574\u6570\uff0c\u5219\u7ed3\u679c\u5e94\u8be5\u5982\u4f60\u6240\u6599\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << 20 / 4 ; return 0 ; } \u8ba1\u7b97\u7ed3\u679c\u4e3a 5\u3002 \u4f46\u662f\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u8fd9\u4e2a\u5546\u662f\u5c0f\u6570\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << 8 / 5 ; return 0 ; } \u8ba1\u7b97\u7ed3\u679c\u4e3a 1\u3002 \u5f53 C++\u5bf9\u4e24\u4e2a\u6574\u578b\u505a\u9664\u6cd5\u65f6\uff08\u79f0\u4e3a\u6574\u6570\u9664\u6cd5\uff09\uff0c\u5b83\u53ea\u80fd\u4ea7\u751f\u6574\u6570\u7ed3\u679c\uff0c\u56e0\u4e3a\u6574\u578b\u4e0d\u80fd\u4fdd\u5b58\u5c0f\u6570\u503c\uff0c\u7ed3\u679c\u4e2d\u7684\u5c0f\u6570\u90e8\u5206\u4f1a\u88ab \u4e22\u5f03 \uff08\u800c\u4e0d\u662f\u56db\u820d\u4e94\u5165\uff09\u3002 \u4ed4\u7ec6\u770b\u770b\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c8 / 5 \u5e94\u8be5\u7b49\u4e8e1.6\uff0c\u4f46\u662f\u5c0f\u6570\u90e8\u5206(0.6)\u4f1a\u88ab\u4e22\u5f03\uff0c\u6240\u4ee5\u7ed3\u679c\u4e3a1\u3002 \u540c\u6837\u7684\uff0c-8 / 5 \u7684\u7ed3\u679c\u4e3a -1\u3002 \u6ce8\u610f \u5728\u4f7f\u7528\u6574\u6570\u9664\u6cd5\u65f6\u8981\u5c0f\u5fc3\uff0c\u56e0\u4e3a\u5546\u4e2d\u7684\u5c0f\u6570\u90e8\u5206\u4f1a\u88ab\u4e22\u5f03\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u8fd9\u6070\u597d\u662f\u4f60\u671f\u671b\u7684\uff0c\u90a3\u4e48\u53ef\u4ee5\u653e\u5fc3\u4f7f\u7528\u3002 \u5982\u679c\u4f60\u5e0c\u671b\u4fdd\u7559\u5c0f\u6570\u90e8\u5206\uff0c\u8bf7\u53c2\u8003 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 \u4e2d\u4ecb\u7ecd\u7684\u65b9\u6cd5\u3002","title":"4.4 - \u6709\u7b26\u53f7\u6574\u578b"},{"location":"4-4-Signed-integers/#_1","text":"\u5728\u65e5\u5e38\u751f\u6d3b\u4e2d\uff0c\u5982\u679c\u6211\u4eec\u8981\u8868\u793a\u8d1f\u6574\u6570\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528\u8d1f\u53f7\u3002\u4f8b\u5982\uff0c -3 \u8868\u793a \u201c\u8d1f\u4e09\u201d\u3002\u6211\u4eec\u8fd8\u4f1a\u4f7f\u7528+3 \u6765\u8868\u793a\u201d\u6b63\u4e09\u201c\uff08\u5c3d\u7ba1\u901a\u5e38\u6b63\u53f7\u4f1a\u88ab\u7701\u7565\uff09\u3002\u6570\u5b57\u7684\u8fd9\u79cd\u201c\u6b63\u8d1f\u96f6\u201d\u5c5e\u6027\uff0c\u79f0\u4e3a\u6570\u5b57\u7684\u8d1f\u53f7\u3002 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6574\u578b\u662f\u6709\u7b26\u53f7\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\u6570\u5b57\u7684\u7b26\u53f7\u662f\u6570\u5b57\u672c\u8eab\u7684\u4e00\u90e8\u5206\uff08\u4f7f\u7528\u4e00\u4e2a\u4f4d\u8fdb\u884c\u5b58\u50a8\uff0c\u79f0\u4e3a\u7b26\u53f7\u4f4d\uff09\u3002\u56e0\u6b64\uff0c\u6709\u7b26\u53f7\u6574\u578b\u53ef\u4ee5\u8868\u793a\u6b63\u6570\u8d1f\u6570\u548c 0\u3002 \u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u4e13\u6ce8\u4e8e\u4ecb\u7ecd\u6709\u7b26\u53f7\u6574\u578b(signed)\u3002\u65e0\u7b26\u53f7(unsigned)\u6574\u578b\uff08\u53ea\u80fd\u4fdd\u5b58\u975e\u8d1f\u503c\uff09\u5219\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u8ba8\u8bba\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u5728 O.4 -- Converting between binary and decimal \u4e2d\u8ba8\u8bba\u4e86\u4e8c\u8fdb\u5236\u8868\u793a\u6cd5\u65f6\u7b26\u53f7\u4f4d\u5982\u4f55\u4f7f\u7528\u3002","title":"\u6709\u7b26\u53f7\u6574\u578b"},{"location":"4-4-Signed-integers/#_2","text":"\u5b9a\u4e49\u6709\u7b26\u53f7\u6574\u578b\u65f6\uff0c\u63a8\u8350\u7684\u65b9\u5f0f\u5982\u4e0b\uff1a 1 2 3 4 short s ; int i ; long l ; long long ll ; \u6240\u6709\u7684\u6574\u578b(\u9664\u4e86 int )\u4ee5\u5916\uff0c\u90fd\u53ef\u4ee5\u5e26\u4e0a\u4e00\u4e2a\u53ef\u9009\u7684 int \u540e\u7f00\uff1a 1 2 3 short int si ; //short int \u4e2d\u7684 int \u5373\u53ef\u9009\u540e\u7f00 long int li ; long long int lli ; \u4e0d\u8fc7\uff0c\u5e76\u4e0d\u63a8\u8350\u4f7f\u7528\u8fd9\u4e9b\u540e\u7f00\u3002\u4f7f\u7528\u8fd9\u4e9b\u540e\u7f00\u4e0d\u4ec5\u9700\u8981\u6253\u66f4\u591a\u5b57\uff0c\u800c\u4e14\u6dfb\u52a0 int \u540e\u7f00\u540e\uff0c\u4e0d\u5bb9\u6613\u5c06\u8fd9\u4e9b\u7c7b\u578b\u548c int \u7c7b\u578b\u7684\u53d8\u91cf\u533a\u5206\u5f00\u6765\uff0c\u800c\u4e14\u5982\u679c\u4e0d\u7ecf\u610f\u5fd8\u8bb0\u4e86 short \u6216\u8005 long \u4fee\u9970\u7b26\u7684\u8bdd\u5219\u53ef\u80fd\u4f1a\u5f15\u5165\u95ee\u9898\u3002 \u6574\u578b\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u53ef\u9009\u7684 singed \u5173\u952e\u5b57\uff0c\u901a\u5e38\u4f1a\u5c06\u5176\u653e\u7f6e\u5728\u7c7b\u578b\u540d\u524d\uff1a 1 2 3 4 signed short ss ; signed int si ; signed long sl ; signed long long sll ; \u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u5173\u952e\u5b57\u4e5f\u4e0d\u63a8\u8350\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u662f\u591a\u4f59\u7684\uff0c\u6574\u578b\u9ed8\u8ba4\u5c31\u662f\u6709\u7b26\u53f7\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u6700\u7cbe\u7b80\u7684\u5199\u6cd5\uff0c\u4e0d\u8981\u4f7f\u7528 int \u540e\u7f00\u6216 signed \u524d\u7f00\u3002","title":"\u5b9a\u4e49\u6709\u7b26\u53f7\u6574\u578b"},{"location":"4-4-Signed-integers/#_3","text":"\u6b63\u5982\u4e4b\u524d\u4ecb\u7ecd\u7684\u90a3\u6837\uff0c\u5728\u5185\u5b58\u4e2d\u5360\u7528 n \u4f4d\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u4fdd\u5b58 \\(2^n\\) \u4e2a\u53ef\u80fd\u7684\u503c\u3002\u4f46\u662f\uff0c\u5177\u4f53\u662f\u54ea\u4e9b\u503c\u5462\uff1f\u6211\u4eec\u79f0\u4e00\u79cd\u7c7b\u578b\u53ef\u4ee5\u8868\u793a\u7684\u503c\u4e3a\u5176\u8303\u56f4\u3002\u800c\u6574\u578b\u7684\u8303\u56f4\u6709\u4e24\u4e2a\u56e0\u7d20\u51b3\u5b9a\uff1a\u5927\u5c0f\uff08\u591a\u5c11\u4f4d\uff09\uff0c\u662f\u5426\u6709\u7b26\u53f7\u3002 \u6839\u636e\u5b9a\u4e49\uff0c8 \u4f4d\u6709\u7b26\u53f7\u6574\u578b\u53ef\u4ee5\u8868\u793a\u7684\u8303\u56f4\u662f\uff1a-128 \u5230 127\u3002\u4e5f\u5c31\u662f\u8bf4\u4e00\u4e2a\u6709\u7b26\u53f7\u6574\u578b\u53ef\u4ee5\u5b89\u5168\u5730\u8868\u793a -128 \u5230 127 (\u5305\u62ec) \u4e2d\u7684\u4efb\u610f\u6574\u6570\u3002 \u9898\u5916\u8bdd \u6570\u5b66\u65f6\u95f4\uff1a8 \u4f4d\u6574\u578b\u6709 8 \u4e2a\u4f4d\uff0c2 \u7684 8 \u6b21\u65b9\u662f 256\uff0c\u6240\u4ee5\u5b83\u4e00\u5171\u53ef\u4ee5\u4fdd\u5b58 256 \u4e2a\u53ef\u80fd\u7684\u503c\u3002\u5728 -128 \u5230 127 \u4e2d\u4e00\u5171\u6709 256 \u4e2a\u53ef\u80fd\u7684\u6574\u6570\u503c\u3002 \u4e0b\u8868\u5c55\u793a\u4e86\u4e0d\u540c\u6709\u7b26\u53f7\u6574\u578b\u53ef\u4ee5\u8868\u793a\u7684\u8303\u56f4\uff1a \u5927\u5c0f/\u7c7b\u578b \u8303\u56f4 8 bit signed -128 \u5230 127 16 bit signed -32,768 \u5230 32,767 32 bit signed -2,147,483,648 \u5230 2,147,483,647 64 bit signed -9,223,372,036,854,775,808 \u5230 9,223,372,036,854,775,807 \u5bf9\u4e8e\u559c\u6b22\u6570\u5b66\u8868\u793a\u6cd5\u7684\u4eba\u6765\u8bf4\uff1a n \u4f4d\u6709\u7b26\u53f7\u6574\u578b\u7684\u8303\u56f4\u4e3a \\(-(2^{n-1})\\) \u5230 \\(2^{n-1}-1\\) \u3002 \u5bf9\u4e8e\u4e0d\u559c\u6b22\u6570\u5b66\u7684\u4eba\u6765\u8bf4\uff0c\u53c2\u8003\u4e0a\u8868\u5c31\u53ef\u4ee5\u4e86\u3002","title":"\u6709\u7b26\u53f7\u6574\u578b\u7684\u8303\u56f4"},{"location":"4-4-Signed-integers/#_4","text":"\u5982\u679c\u6211\u4eec\u5c1d\u8bd5\u628a 280 \u8d4b\u503c\u7ed9\u4e00\u4e2a 8 \u4f4d\u6709\u7b26\u53f7\u6574\u578b\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f\u8fd9\u4e2a\u6570\u5b57\u5df2\u7ecf\u8d85\u51fa\u4e86 8 \u4f4d\u6709\u7b26\u53f7\u6574\u578b\u6570\u80fd\u591f\u8868\u793a\u7684\u8303\u56f4\u3002280 \u9700\u8981 9 \u4e2a\u4f4d\uff08\u52a0\u4e00\u4e2a\u7b26\u53f7\u4f4d\uff09\u624d\u80fd\u8868\u793a\uff0c\u4f46\u662f\u4e00\u4e2a8\u4f4d\u6574\u578b\u53ea\u67097\u4e2a\u4f4d\uff08\u52a0\u4e00\u4e2a\u7b26\u53f7\u4f4d\uff09\u3002 \u5f53\u9700\u8981\u5b58\u50a8\u7684\u503c\u7684\u8303\u56f4\u8d85\u8fc7\u4e86\u53d8\u91cf\u53ef\u4ee5\u8868\u793a\u7684\u8303\u56f4\uff0c\u5c31\u4f1a\u53d1\u751f\u6574\u6570\u6ea2\u51fa(\u901a\u5e38\u7b80\u79f0\u4e3a\u6ea2\u51fa) \u3002\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u8868\u793a\u7684\u6570\uff0c\u9700\u8981\u6bd4\u8be5\u53d8\u91cf\u80fd\u591f\u4f7f\u7528\u7684\u66f4\u591a\u7684\u4f4d\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53d8\u91cf\u5c06\u6ca1\u6709\u8db3\u591f\u7684\u5185\u5b58\u7a7a\u95f4\u53ef\u4ee5\u7528\u6765\u5b58\u653e\u6570\u636e\u3002 \u5bf9\u4e8e\u6709\u7b26\u53f7\u6574\u578b\u6765\u8bf4\uff0c\u7a76\u7adf\u54ea\u4e2a\u4f4d\u7684\u6570\u636e\u4f1a\u88ab\u4e22\u5f03\u5e76\u6ca1\u6709\u660e\u786e\u7684\u5b9a\u4e49\uff0c\u56e0\u6b64\u6709\u7b26\u53f7\u6574\u578b\u7684\u6ea2\u51fa\u662f\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 \u6ce8\u610f \u6709\u7b26\u53f7\u6574\u578b\u7684\u6ea2\u51fa\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a \u901a\u5e38\u6765\u8bf4\uff0c\u6ea2\u51fa\u4f1a\u5bfc\u81f4\u4fe1\u606f\u7684\u4e22\u5931\uff0c\u8fd9\u662f\u6211\u4eec\u6700\u4e0d\u5e0c\u671b\u53d1\u751f\u7684\u662f\u3002\u5982\u679c\u4f60\u6000\u7591\u5bf9\u8c61\u9700\u8981\u4fdd\u5b58\u7684\u503c\u6709\u53ef\u80fd\u4f1a\u8d85\u8fc7\u5b83\u80fd\u591f\u8868\u793a\u7684\u8303\u56f4\uff0c\u8bf7\u4f7f\u7528\u8303\u56f4\u66f4\u5927\u7684\u7c7b\u578b\uff01","title":"\u6574\u6570\u6ea2\u51fa"},{"location":"4-4-Signed-integers/#_5","text":"\u5f53\u5bf9\u4e24\u4e2a\u6574\u578b\u8fdb\u884c\u9664\u6cd5\u8fd0\u7b97\u65f6\uff0c \u5982\u679c\u5546\u662f\u6574\u6570\uff0c\u5219\u7ed3\u679c\u5e94\u8be5\u5982\u4f60\u6240\u6599\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << 20 / 4 ; return 0 ; } \u8ba1\u7b97\u7ed3\u679c\u4e3a 5\u3002 \u4f46\u662f\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u8fd9\u4e2a\u5546\u662f\u5c0f\u6570\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << 8 / 5 ; return 0 ; } \u8ba1\u7b97\u7ed3\u679c\u4e3a 1\u3002 \u5f53 C++\u5bf9\u4e24\u4e2a\u6574\u578b\u505a\u9664\u6cd5\u65f6\uff08\u79f0\u4e3a\u6574\u6570\u9664\u6cd5\uff09\uff0c\u5b83\u53ea\u80fd\u4ea7\u751f\u6574\u6570\u7ed3\u679c\uff0c\u56e0\u4e3a\u6574\u578b\u4e0d\u80fd\u4fdd\u5b58\u5c0f\u6570\u503c\uff0c\u7ed3\u679c\u4e2d\u7684\u5c0f\u6570\u90e8\u5206\u4f1a\u88ab \u4e22\u5f03 \uff08\u800c\u4e0d\u662f\u56db\u820d\u4e94\u5165\uff09\u3002 \u4ed4\u7ec6\u770b\u770b\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c8 / 5 \u5e94\u8be5\u7b49\u4e8e1.6\uff0c\u4f46\u662f\u5c0f\u6570\u90e8\u5206(0.6)\u4f1a\u88ab\u4e22\u5f03\uff0c\u6240\u4ee5\u7ed3\u679c\u4e3a1\u3002 \u540c\u6837\u7684\uff0c-8 / 5 \u7684\u7ed3\u679c\u4e3a -1\u3002 \u6ce8\u610f \u5728\u4f7f\u7528\u6574\u6570\u9664\u6cd5\u65f6\u8981\u5c0f\u5fc3\uff0c\u56e0\u4e3a\u5546\u4e2d\u7684\u5c0f\u6570\u90e8\u5206\u4f1a\u88ab\u4e22\u5f03\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u8fd9\u6070\u597d\u662f\u4f60\u671f\u671b\u7684\uff0c\u90a3\u4e48\u53ef\u4ee5\u653e\u5fc3\u4f7f\u7528\u3002 \u5982\u679c\u4f60\u5e0c\u671b\u4fdd\u7559\u5c0f\u6570\u90e8\u5206\uff0c\u8bf7\u53c2\u8003 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 \u4e2d\u4ecb\u7ecd\u7684\u65b9\u6cd5\u3002","title":"\u6574\u578b\u9664\u6cd5"},{"location":"4-5-Unsigned-integers-and-why-to-avoid-them/","tags":["data type","integer","overflow"],"text":"Key Takeaway \u65e0\u7b26\u53f7\u6574\u578b\u8868\u793a\u8d85\u8fc7\u8303\u56f4\u7684\u6570\u65f6\uff0c\u5b9e\u9645\u5b58\u50a8\u7684\u503c\u4e3a\u539f\u503c\u9664\u4ee5\u8303\u56f4\u6700\u5927\u503c\u52a0 1 \u540e\u7684\u4f59\u6570\u3002\u4f8b\u5982\uff0c value mod 2^32 \u3002\u79f0\u4e3a\u65e0\u7b26\u53f7\u7684\u53cd\u8f6c\u3002 \u65e0\u7b26\u53f7\u4f1a\u7ffb\u8f6c\uff0c\u6709\u7b26\u53f7\u4f1a\u6ea2\u51fa \u65e0\u7b26\u53f7\u53cd\u8f6c\u53ef\u4ee5\u54110\u53cd\u8f6c\u4e5f\u53ef\u4ee5\u5411\u6700\u5927\u503c\u53cd\u8f6c \u65e0\u7b26\u53f7\u6574\u578b\u8868\u793a\u8d85\u8fc7\u8303\u56f4\u7684\u6570\u65f6\u4e0d\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \uff0c\u5b83\u7684\u884c\u4e3a\u662f\u786e\u5b9a\u7684\uff0c\u56e0\u6b64 C++ \u6807\u51c6\u660e\u786e\u8bf4\u660e\u65e0\u7b26\u53f7\u6574\u578b\u4e0d\u4f1a\"\u6ea2\u51fa\" \u6df7\u7528\u65e0\u7b26\u53f7\u548c\u6709\u7b26\u53f7\u7c7b\u578b\u65f6\u53ef\u80fd\u4ea7\u751f\u95ee\u9898\uff0c\u56e0\u4e3a\u6709\u7b26\u53f7\u6570\u4f1a\u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a\u65e0\u7b26\u53f7\u6570\uff0c\u6b64\u65f6\u53ef\u80fd\u53d1\u751f\u53cd\u8f6c\u3002 \u5c3d\u91cf\u4f7f\u7528\u6709\u7b26\u53f7\u6570\u4fdd\u5b58\u6570\u91cf\u503c\u548c\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\uff0c\u65e0\u7b26\u53f7+\u6709\u7b26\u53f7\u4f1a\u53d1\u751f\u8f6c\u6362\u5bfc\u81f4\u6709\u8d1f\u7684\u7b26\u53f7\u6570\u6ea2\u51fa\uff08 8.4 - \u7b97\u672f\u8f6c\u6362 \uff09 \u8fdb\u884c\u4f4d\u8fd0\u7b97\u7684\u65f6\u5019\u53ef\u4ee5\u4f7f\u7528\u65e0\u7b26\u53f7\u6574\u578b \u65e0\u7b26\u53f7\u6574\u578b \u5728\u4e0a\u8282\u8bfe ( 4.4 - \u6709\u7b26\u53f7\u6574\u578b ) \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u6709\u7b26\u53f7\u6574\u578b\uff0c\u8fd9\u4e9b\u5f81\u4fe1\u53ef\u4ee5\u4fdd\u5b58\u6b63\u8d1f\u6574\u6570\u548c 0\u3002 C++ \u8fd8\u652f\u6301\u65e0\u7b26\u53f7\u6574\u578b\u3002\u65e0\u7b26\u53f7\u6574\u578b\u53ea\u80fd\u8868\u793a\u975e\u7b26\u6574\u6570\u3002 \u5b9a\u4e49\u65e0\u7b26\u53f7\u6574\u578b \u5b9a\u4e49\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u9700\u8981\u4f7f\u7528 unsigned \u5173\u952e\u5b57\u3002\u901a\u5e38\u6765\u8bb2\uff0c\u8be5\u5173\u952e\u5b57\u5e94\u8be5\u5199\u5728\u7c7b\u578b\u524d\uff1a 1 2 3 4 unsigned short us ; unsigned int ui ; unsigned long ul ; unsigned long long ull ; \u65e0\u7b26\u53f7\u6574\u578b\u7684\u8303\u56f4 1 \u5b57\u8282(8 \u4f4d)\u65e0\u7b26\u53f7\u6574\u578b\u7684\u8303\u56f4\u4e3a 0 \u548c 255\uff0c\u800c 1 \u5b57\u8282\u6709\u7b26\u53f7\u6574\u578b\u7684\u8303\u56f4\u662f -128 \u5230 127\u3002\u5b83\u4eec\u90fd\u80fd\u591f\u5b58\u653e 256 \u4e2a\u4e0d\u540c\u7684\u503c\uff0c\u4f46\u662f\u6709\u7b26\u53f7\u6574\u578b\u7684\u4e00\u534a\u8303\u56f4\u88ab\u7528\u6765\u8868\u793a\u8d1f\u6570\uff0c\u800c\u65e0\u7b26\u53f7\u6574\u578b\u5219\u5168\u90e8\u7528\u6765\u5b58\u653e\u6574\u6570\uff0c\u56e0\u6b64\u80fd\u591f\u8868\u793a\u7684\u6b63\u6570\u8303\u56f4\u662f\u6709\u7b26\u53f7\u6574\u578b\u7684\u4e24\u500d\u3002 \u4e0b\u8868\u5c55\u793a\u4e86\u4e0d\u540c\u6709\u7b26\u53f7\u6574\u578b\u53ef\u4ee5\u8868\u793a\u7684\u8303\u56f4\uff1a \u5927\u5c0f/\u7c7b\u578b \u8303\u56f4 1 byte unsigned 0 \u5230 255 2 byte unsigned 0 \u5230 65,535 4 byte unsigned 0 \u5230 4,294,967,295 8 byte unsigned 0 \u5230 18,446,744,073,709,551,615 \u4e00\u4e2a n \u4e3a\u7684\u65e0\u7b26\u53f7\u6574\u578b\u53ef\u8868\u793a\u7684\u8303\u56f4\u4e3a 0 \u5230 \\((2^n)-1\\) \u3002 \u5982\u679c\u4e0d\u9700\u8981\u8868\u793a\u8d1f\u6570\uff0c\u90a3\u4e48\u65e0\u7b26\u53f7\u6574\u578b\u66f4\u9002\u5408\u5728\u5185\u5b58\u6bd4\u8f83\u5c0f\u7684\u7f51\u7edc\u548c\u7cfb\u7edf\u4e0a\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5728\u4e0d\u5360\u7528\u66f4\u591a\u5185\u5b58\u7684\u524d\u63d0\u4e0b\u4fdd\u5b58\u66f4\u591a\u7684\u6b63\u6574\u6570\u3002 \u5206\u6e05\u6709\u7b26\u53f7(signed) \u548c\u65e0\u7b26\u53f7(unsigned) \u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u7ecf\u5e38\u628a\u6709\u7b26\u53f7(signed) \u548c\u65e0\u7b26\u53f7(unsigned)\u8fd9\u4e24\u4e2a\u672f\u8bed\u641e\u6df7\u3002\u4ee5\u4e0b\u4ecb\u7ecd\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u8bb0\u5fc6\u5b83\u4eec\u7684\u4e0d\u540c\uff1a\u4e3a\u4e86\u533a\u5206\u8d1f\u6570\u548c\u6b63\u6570\uff0c\u6211\u4eec\u9700\u8981\u8d1f\u53f7\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u7b26\u53f7\uff0c\u6211\u4eec\u5047\u5b9a\u8be5\u6570\u4e3a\u6b63\u6570\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u5177\u6709\u7b26\u53f7\u7684\u6574\u578b\uff08\u6709\u7b26\u53f7\u6574\u578b\uff09\u53ef\u4ee5\u533a\u5206\u6b63\u8d1f\u3002\u800c\u4e0d\u5177\u6709\u7b26\u53f7\u7684\u6574\u578b\uff08\u6709\u7b26\u53f7\u6574\u578b\uff09\u5219\u5168\u90e8\u4e3a\u6b63\u6570\u3002 \u65e0\u7b26\u53f7\u6574\u578b\u6ea2\u51fa What happens if we try to store the number 280 (which requires 9 bits to represent) in a 1-byte (8-bit) unsigned integer? The answer is overflow. \u5982\u679c\u6211\u4eec\u5c1d\u8bd5\u628a 280 \uff08\u9700\u8981 9 \u4e2a\u4f4d\u6765\u8868\u793a\uff09\u8d4b\u503c\u7ed9\u4e00\u4e2a 8 \u4f4d\u65e0\u7b26\u53f7\u6574\u578b\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f\u7b54\u6848\u662f\u2014\u2014\u6ea2\u51fa\u3002 \u4f5c\u8005\u6ce8 \u5947\u602a\u7684\u662f\uff0cC++\u6807\u51c6\u4e2d\u660e\u786e\u5199\u9053\uff0c\u65e0\u7b26\u53f7\u64cd\u4f5c\u6570\u53c2\u4e0e\u7684\u8fd0\u7b97\u6c38\u8fdc\u4e0d\u4f1a\u6ea2\u51fa\u3002\u8fd9\u548c\u6211\u4eec\u7684\u7f16\u7a0b\u5e38\u8bc6\uff08\u65e0\u7b26\u53f7\u548c\u6709\u7b26\u53f7\u6574\u578b\u90fd\u4f1a\u6ea2\u51fa\uff09\u662f\u77db\u76fe\u7684\u3002( \u53c2\u8003\u6587\u732e )\u3002\u7531\u4e8e\u5927\u591a\u6570\u7684\u7a0b\u5e8f\u5458\u90fd\u4f1a\u8003\u8651\u8be5\u6ea2\u51fa\uff0c\u6211\u4eec\u8fd9\u91cc\u4e5f\u6cbf\u7528\u4e86\u8be5\u8bf4\u6cd5\uff0c\u5c3d\u7ba1\u548cC++\u6807\u51c6\u6709\u6240\u77db\u76fe\u3002 \u5f53\u65e0\u7b26\u53f7\u503c\u8d85\u8fc7\u8303\u56f4\u65f6\uff0c\u5b83\u4f1a\u88ab\u9664\u4ee5\u8be5\u7c7b\u578b\u7684\u6700\u5927\u503c\u52a0 1\uff0c\u4e14\u53ea\u4fdd\u7559\u4f59\u6570\u3002 280 \u4e0d\u5728 0 \u5230 255 \u7684\u8303\u56f4\u5185\u3002\u8be5\u8303\u56f4\u7684\u6700\u5927\u503c+1 \u662f 256\uff0c\u6240\u4ee5 280 \u4f1a\u9664\u4ee5 256\uff0c\u4f59\u6570\u4e3a 24\u3002\u6700\u7ec8 24 \u4f1a\u88ab\u5b58\u653e\u5230\u5185\u5b58\u4e2d\u3002 \u8fd9\u4e2a\u95ee\u9898\u8fd8\u53ef\u4ee5\u8fd9\u6837\u7406\u89e3\u3002\u6bd4\u6700\u5927\u503c\u8fd8\u5927\u7684\u6570\u5728\u5b58\u653e\u65f6\u4f1a\u88ab\u201cwraps around\u201d (\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a\u201cmodulo wrapping\u201d)\uff0c\u5373\u4ece 0 \u5f00\u59cb\u91cd\u65b0\u8ba1\u7b97\u3002255 \u5728 1 \u5b57\u8282\u6574\u578b\u7684\u8868\u793a\u8303\u56f4\u5185\uff0c\u6240\u4ee5 255 \u6ca1\u95ee\u9898\u3002\u800c 256 \u5219\u8d85\u8fc7\u4e86\u8303\u56f4\uff0c\u56e0\u6b64\u5b83\u4f1a\u88ab\u5f52\u4e3a 0\u3002257 \u5219\u4f1a\u5f52\u4e3a 1\u3002280 \u5219\u5f52\u4e3a 24. \u8fd9\u91cc\u662f\u4e00\u4e2a 2 \u5b57\u8282\u65e0\u7b26\u53f7\u77ed\u6574\u578b\uff08 unsigned short \uff09 \u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { unsigned short x { 65535 }; // largest 16-bit unsigned value possible std :: cout << \"x was: \" << x << '\\n' ; x = 65536 ; // 65536 is out of our range, so we get wrap-around std :: cout << \"x is now: \" << x << '\\n' ; x = 65537 ; // 65537 is out of our range, so we get wrap-around std :: cout << \"x is now: \" << x << '\\n' ; return 0 ; } \u8bd1\u8005\u6ce8 \u65e0\u7b26\u53f7\u4e0d\u4f1a\u6ea2\u51fa\uff0c\u7684\u786e\u662f C++\u6807\u51c6\uff0c\u4e0d\u6ea2\u51fa\u8fd9\u4e2a\u8bf4\u6cd5\u4e5f\u662f\u5408\u7406\u7684\uff0c\u56e0\u4e3a\u65e0\u7b26\u53f7\u5728\u9047\u5230\u8d85\u8fc7\u8868\u793a\u8303\u56f4\u7684\u60c5\u51b5\u65f6\u5e76\u4e0d\u4f1a\u8868\u73b0\u51fa \u672a\u5b9a\u4e49\u884c\u4e3a \uff0c\u7ed3\u679c\u4e5f\u4e0d\u662f\u7b80\u5355\u7684\u4e22\u5f03\uff0c\u800c\u662f\u5bf9\u539f\u503c\u8fdb\u884c\u6c42\u6a21\u8fd0\u7b97\u3002\u82f1\u6587\u53eb warp around\uff0c\u5373\u4ece 0 \u5f00\u59cb\u91cd\u65b0\u8ba1\u7b97\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e 32 \u4f4d\u65e0\u7b26\u53f7\uff0c\u8d85\u51fa\u8303\u56f4\u65f6\u7ed3\u679c\u7b49\u4e8e value mod 2^32 \u4f60\u89c9\u5f97\u7a0b\u5e8f\u7684\u7ed3\u679c\u4f1a\u662f\u4ec0\u4e48\u5462\uff1f (\u6ce8\u610f\uff0c\u5982\u679c\u4f60\u7f16\u8bd1\u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8bdd\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u4ea7\u751f\u5173\u4e8e\u6ea2\u51fa\u6216\u622a\u65ad\u7684\u544a\u8b66\u2014\u2014\u4f60\u9700\u8981\u5173\u95ed\u201d\u5c06\u544a\u8b66\u770b\u505a\u9519\u8bef\u201c\u529f\u80fd\uff0c\u624d\u80fd\u8fd0\u884c\u8be5\u7a0b\u5e8f\uff09 1 2 3 x was: 65535 x is now: 0 x is now: 1 \u5411\u53e6\u4e00\u4e2a\u65b9\u5411 wrap \u4e5f\u662f\u53ef\u4ee5\u7684\uff0c0 \u53ef\u4ee5\u88ab 2 \u5b57\u8282\u5927\u5c0f\u7684\u65e0\u7b26\u53f7\u6574\u578b\u8868\u793a\uff0c\u4f46\u662f-1 \u4e0d\u53ef\u4ee5\uff0c\u56e0\u6b64\u5b83\u4f1a\u88ab wrap \u5230\u6700\u5927\u503c\uff0c\u7ed3\u679c\u4e3a 65535\u3002-2 \u7684\u7ed3\u679c\u662f 65534\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { unsigned short x { 0 }; // smallest 2-byte unsigned value possible std :: cout << \"x was: \" << x << '\\n' ; x = -1 ; // -1 is out of our range, so we get wrap-around std :: cout << \"x is now: \" << x << '\\n' ; x = -2 ; // -2 is out of our range, so we get wrap-around std :: cout << \"x is now: \" << x << '\\n' ; return 0 ; } 1 2 3 x was: 0 x is now: 65535 x is now: 65534 \u4e0a\u8ff0\u4ee3\u7801\u4f1a\u5f15\u8d77\u67d0\u4e9b\u7f16\u8bd1\u5668\u4ea7\u751f\u544a\u8b66\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u68c0\u6d4b\u5230\u6574\u578b\u5b57\u9762\u91cf\u8d85\u8fc7\u4e86\u8868\u793a\u5b83\u7684\u7c7b\u578b\u7684\u8303\u56f4\u3002\u5982\u679c\u4f60\u5e0c\u671b\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\uff0c\u8bf7\u6682\u65f6\u5173\u95ed\u201d\u5c06\u544a\u8b66\u770b\u505a\u9519\u8bef\u201c\u529f\u80fd\u3002 \u9898\u5916\u8bdd \u5f88\u591a\u7535\u5b50\u6e38\u620f\u53f2\u4e0a\u7684\u8457\u540d\u7684 bug \u90fd\u548c\u65e0\u7b26\u53f7\u6570\u53cd\u8f6c\u6709\u5173\u3002\u8857\u673a\u6e38\u620f\u300a\u5927\u91d1\u521a\u300b\u4e2d\uff0c\u4f60\u4e0d\u53ef\u80fd\u901a\u8fc722\u5173\uff0c\u56e0\u4e3a\u6ea2\u51fabug\u5bfc\u81f4\u7528\u6237\u5728\u8fd9\u4e00\u5173\u4e2d\u4e0d\u53ef\u80fd\u6709\u8db3\u591f\u7684\u65f6\u95f4\u3002 \u7535\u8111\u6e38\u620f\u300a\u6587\u660e\u300b\u4e2d\uff0c\u7518\u5730\u901a\u5e38\u662f\u7b2c\u4e00\u4e2a\u4f7f\u7528\u6838\u6b66\u5668\u7684\uff0c\u8fd9\u548c\u4ed6\u672c\u5e94\u8be5\u6709\u7684\u4fdd\u5b88\u7684\u5c5e\u6027\u975e\u5e38\u77db\u76fe\u3002\u73a9\u5bb6\u8ba4\u4e3a\u8fd9\u662f\u7531\u4e8e\u7518\u5730\u7684\u653b\u51fb\u6027\u88ab\u521d\u59cb\u5316\u62101\uff0c\u4f46\u662f\u5982\u679c\u4ed6\u8d70\u6c11\u4e3b\u8def\u7ebf\u7684\u8bdd\uff0c\u5176\u653b\u51fb\u6027\u4f1a-2\u3002\u8fdb\u800c\u5bfc\u81f4\u4e86\u6570\u503c\u88ab\u53cd\u8f6c\u6210\u4e86255\uff0c\u4f7f\u5176\u53d8\u5f97\u975e\u5e38\u597d\u6597\u3002\u4e0d\u8fc7\uff0c\u6700\u8fd1\u5e2d\u5fb7\u6885\u5c14\u5bf9\u6b64\u8fdb\u884c\u4e86\u6f84\u6e05\uff0c\u4ed6\u8bf4\u8fd9\u5e76\u4e0d\u662f\u5f15\u8d77\u4e0a\u8ff0\u95ee\u9898\u7684\u539f\u56e0\u3002 \u6709\u5173\u65e0\u7b26\u53f7\u6574\u578b\u7684\u4e89\u8bba \u5f88\u591a\u7a0b\u5e8f\u5458 (\u4ee5\u53ca\u4e00\u4e9b\u5927\u5382\uff0c\u4f8b\u5982\u8c37\u6b4c\uff09\u8ba4\u4e3a\u7a0b\u5e8f\u4e5f\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u65e0\u7b26\u53f7\u6574\u578b\u3002 \u8fd9\u4e3b\u8981\u662f\u56e0\u4e3a\u65e0\u7b26\u53f7\u6574\u578b\u6709\u4e24\u79cd\u884c\u4e3a\u53ef\u80fd\u4f1a\u5bfc\u81f4\u95ee\u9898\u3002 \u9996\u5148\uff0c\u5c06\u4e24\u4e2a\u65e0\u7b26\u53f7\u6574\u578b\u76f8\u51cf\uff0c\u4f8b\u59823-5\u7b49\u4e8e-2\uff0c\u4f46\u662f-2\u4e0d\u80fd\u88ab\u65e0\u7b26\u53f7\u6574\u578b\u8868\u793a\u3002 1 2 3 4 5 6 7 8 9 10 #include int main () { unsigned int x { 3 }; unsigned int y { 5 }; std :: cout << x - y << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\uff0c\u5c3d\u7ba1\u4e0a\u9762\u7684\u4ee3\u7801\u770b\u4e0a\u53bb\u4eba\u755c\u65e0\u5bb3\uff0c\u5b9e\u9645\u4e0a\u7ed3\u679c\u5374\u4ee4\u4eba\u5403\u60ca\uff1a 1 4294967294 \u8fd9\u662f\u56e0\u4e3a -2 \u88ab\u53cd\u8f6c\u5230\u4e86\u4e00\u4e2a\u63a5\u8fd1 4\u81ea\u5df1\u6574\u578b\u6700\u5927\u503c\u7684\u6570\u3002\u53e6\u5916\u4e00\u4e2a\u53ef\u80fd\u4ea7\u751f\u53cd\u8f6c\u7684\u60c5\u51b5\u662f\uff0c\u5bf9\u65e0\u7b26\u53f7\u6574\u578b\u4f7f\u7528\u9012\u51cf\u8fd0\u7b97\u7b26( -- )\u3002\u90a3\u4e48\u5f53\u53d8\u91cf\u5faa\u73af\u9012\u51cf\u65f6\uff0c\u6700\u7ec8\u53ef\u80fd\u4f1a\u53d1\u751f\u4e8e\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\u540c\u6837\u7684\u95ee\u9898\u3002 . \u6b64\u5916\uff0c\u5f53\u5c06\u6709\u7b26\u53f7\u6574\u578b\u548c\u65e0\u7b26\u53f7\u6574\u578b\u6df7\u7528\u65f6\uff0c\u4e5f\u53ef\u80fd\u4ea7\u751f\u610f\u5916\u3002\u5728C++\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u65f6\uff08\u4f8b\u5982\u7b97\u6570\u6216\u6bd4\u8f83\uff09\uff0c\u5982\u679c\u4e24\u4e2a\u64cd\u4f5c\u6570\u5206\u522b\u4e3a\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u90a3\u4e48\u6709\u7b26\u53f7\u7684\u4e00\u65b9\u4f1a\u88ab\u8f6c\u6362\u4e3a\u65e0\u7b26\u53f7\u3002\u7136\u540e\uff0c\u56e0\u4e3a\u65e0\u7b26\u53f7\u6574\u578b\u4e0d\u53ef\u4ee5\u4fdd\u5b58\u8d1f\u6570\uff0c\u8fd9\u5c31\u6709\u53ef\u80fd\u9020\u6210\u6570\u636e\u4e22\u5931\u7684\u60c5\u51b5\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { signed int s { -1 }; unsigned int u { 1 }; if ( s < u ) // -1 \u88ab\u9690\u5f0f\u8f6c\u6362\u6210\u4e86 4294967295, \u800c 4294967295 < 1 \u662f\u9519\u8bef\u7684 std :: cout << \"-1 is less than 1 \\n \" ; else std :: cout << \"1 is less than -1 \\n \" ; // \u8fd9\u53e5\u8bed\u53e5\u4f1a\u88ab\u6267\u884c return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u7f16\u8bd1\uff0c\u770b\u4e0a\u53bb\u4e5f\u7b26\u5408\u903b\u8f91\u3002\u4f46\u662f\u5b83\u6253\u5370\u7684\u7ed3\u679c\u5374\u662f\u9519\u8bef\u7684\u3002\u5c3d\u7ba1\u7f16\u8bd1\u5668\u5728\u68c0\u67e5\u5230\u6709\u7b26\u53f7/\u65e0\u7b26\u53f7\u4e0d\u5339\u914d\u65f6\uff0c\u4f1a\u53d1\u51fa\u4e00\u4e2a\u8b66\u544a\u3002\u4f46\u662f\u7531\u4e8e\u8fd8\u6709\u5f88\u591a\u5176\u4ed6\u60c5\u51b5\u4e0b\u7f16\u8bd1\u5668\u4e5f\u4f1a\u62a5\u544a\u5b8c\u5168\u4e00\u6837\u7684\u8b66\u544a\uff08\u4f8b\u5982\u4e24\u4e2a\u6570\u90fd\u662f\u6b63\u6570\u65f6\uff09\uff0c\u8fd9\u4f1a\u8ba9\u6211\u4eec\u5f88\u96be\u5224\u65ad\u771f\u6b63\u7684\u95ee\u9898\u5728\u54ea\u91cc\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 4.10 - if \u8bed\u53e5 \u4e2d\u4ecb\u7ecd if \u8bed\u53e5\u3002 \u6b64\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u53ef\u80fd\u6709\u95ee\u9898\u7684\u60c5\u51b5\uff0c\u662f\u6839\u672c\u65e0\u6cd5\u88ab\u68c0\u67e5\u5230\u7684\u3002\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 void doSomething ( unsigned int x ) { // Run some code x times std :: cout << \"x is \" << x << '\\n' ; } int main () { doSomething ( -1 ); return 0 ; } doSomething() \u51fd\u6570\u7684\u4f5c\u8005\u671f\u671b\u7528\u6237\u53ea\u4f20\u9012\u6b63\u6570\u7ed9\u8be5\u51fd\u6570\u3002\u4f46\u662f\u8c03\u7528\u8005\u72af\u4e86\u4e00\u4e2a\u9519\u8bef\uff0c\u5b83\u4f20\u4e86-1\u3002\u8fd9\u662f\u4e2a\u5f88\u663e\u7136\u7684\u9519\u8bef\uff0c\u4f46\u662f\u4eba\u4eec\u4ecd\u7136\u96be\u514d\u4f1a\u72af\u3002\u90a3\u4e48\u6b64\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f \u5b9e\u53c2 -1 \u88ab\u9690\u5f0f\u8f6c\u6362\u6210\u4e86\u4e00\u4e2a\u65e0\u7b26\u53f7 \u5f62\u53c2 \u3002-1 \u4e0d\u5728\u65e0\u7b26\u53f7\u6574\u578b\u7684\u8868\u793a\u8303\u56f4\uff0c\u56e0\u6b64\u5fc5\u7136\u88ab\u53cd\u8f6c\u6210\u4e00\u4e2a\u975e\u5e38\u5927\u7684\u6570(\u53ef\u80fd\u662f 4294967295)\u3002\u7136\u540e\u4f60\u7684\u7a0b\u5e8f\u5c31\u4f1a\u51fa\u73b0\u95ee\u9898\u3002 \u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u5e76\u6ca1\u6709\u4e00\u79cd\u65b9\u6cd5\u80fd\u591f\u907f\u514d\u4e0a\u8ff0\u95ee\u9898\u7684\u53d1\u751f\u3002C++ \u53ef\u4ee5\u81ea\u7531\u5730\u8f6c\u6362\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u6570\uff0c\u4f46\u662f\u5b83\u5e76\u4e0d\u4f1a\u5728\u8f6c\u6362\u65f6\u8fdb\u884c\u4efb\u4f55\u7684\u8303\u56f4\u68c0\u67e5\u4ee5\u786e\u4fdd\u4e0d\u53d1\u751f\u6ea2\u51fa\u3002 \u4e0a\u8ff0\u8fd9\u4e9b\u95ee\u9898\u90fd\u662f\u7ecf\u5e38\u4f1a\u9047\u5230\u7684\uff0c\u5b83\u4eec\u4ea7\u751f\u610f\u6599\u4e4b\u5916\u7684\u884c\u4e3a\uff0c\u5e76\u4e14\u96be\u4ee5\u88ab\u53d1\u73b0\u3002\u5373\u4f7f\u4f7f\u7528\u4e00\u4e9b\u88ab\u4e13\u7528\u4e8e\u67e5\u627e\u6b64\u7c7b\u95ee\u9898\u7684\u5de5\u5177\u6709\u5f88\u96be\u53d1\u73b0\u3002 \u56e0\u6b64\uff0c\u4e00\u4e9b\u6709\u4e89\u8bae\u7684\u6700\u4f73\u5b9e\u8df5\u8ba4\u4e3a\uff0c\u6211\u4eec\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u65e0\u7b26\u53f7\u7c7b\u578b\uff0c\u9664\u975e\u786e\u5b9e\u6709\u7279\u6b8a\u7684\u9700\u6c42\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c3d\u91cf\u4f7f\u7528\u6709\u7b26\u53f7\u6570\u6765\u5b58\u653e\u6570\u91cf\u503c\uff08\u5c3d\u7ba1\u6570\u91cf\u53ef\u80fd\u90fd\u662f\u975e\u8d1f\u7684\uff09\u548c\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u3002\u907f\u514d\u6df7\u7528\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u7c7b\u578b\u3002 \u76f8\u5173\u5185\u5bb9 \u652f\u6301\u4e0a\u8ff0\u5efa\u8bae\u7684\u4e00\u4e9b\u6750\u6599(\u540c\u65f6\u4e5f\u9a73\u65a5\u4e86\u4e00\u4e9b\u5e38\u89c1\u7684\u53cd\u5bf9\u610f\u89c1\uff09\uff1a Interactive C++ panel (see 12:12-13:08,42:40-45:26, and 1:02:50-1:03:15) Subscripts and sizes should be signed Unsigned integers from the libtorrent blog \u90a3\u4e48\u4ec0\u4e48\u65f6\u5019\u624d\u4f7f\u7528\u65e0\u7b26\u53f7\u6570\u5462\uff1f \u8fd8\u662f\u6709\u4e00\u4e9b\u573a\u5408\u662f\u9700\u8981\u6216\u8005\u53ef\u4ee5\u4f7f\u7528\u65e0\u7b26\u53f7\u6570\u7684\u3002 \u9996\u5148\uff0c\u65e0\u7b26\u53f7\u6570\u5728\u8fdb\u884c\u4f4d\u8fd0\u7b97\u65f6\u662f\u63a8\u8350\u4f7f\u7528\u7684\uff08\u5728\u7ae0\u8282O\u4e2d\u4ecb\u7ecd\uff09\u3002\u5728\u4e00\u4e9b\u786e\u5b9e\u9700\u8981\u53cd\u8f6c\u884c\u4e3a\u7684\u573a\u5408\uff0c\u65e0\u7b26\u53f7\u6570\u4e5f\u662f\u6709\u7528\u7684\uff08\u4f8b\u5982\u4e00\u4e9b\u52a0\u5bc6\u7b97\u6cd5\u548c\u968f\u673a\u6570\u751f\u6210\uff09\u3002 \u5176\u6b21\uff0c\u5728\u6709\u4e9b\u65f6\u5019\u65e0\u7b26\u53f7\u6570\u662f\u65e0\u6cd5\u907f\u514d\u7684\uff08\u5f88\u591a\u4e0e\u6570\u7ec4\u8fdb\u884c\u7d22\u5f15\u6709\u5173\uff09\u3002\u6211\u4eec\u4f1a\u5728\u6709\u5173\u6570\u7ec4\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002\u5728\u8fd9\u4e9b\u60c5\u51b5\u4e0b\uff0c\u65e0\u7b26\u53f7\u6570\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u6570\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast \u4e2d\u4ecb\u7ecd\u5982\u4f55\u5c06\u65e0\u7b26\u53f7\u6570\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u6570\u3002 \u540c\u65f6\u8bf7\u6ce8\u610f\uff0c\u5982\u679c\u4f60\u5728\u5f00\u53d1\u5d4c\u5165\u5f0f\u7cfb\u7edf\uff08\u4f8b\u5982 Arduino\uff09\u6216\u8005\u5176\u4ed6\u5904\u7406\u5668/\u5185\u5b58\u6bd4\u8f83\u5403\u7d27\u7684\u73af\u5883\uff0c\u51fa\u4e8e\u6027\u80fd\u7684\u539f\u56e0\u800c\u4f7f\u7528\u65e0\u7b26\u53f7\u6570\u662f\u975e\u5e38\u5e38\u89c1\u7684\uff0c\u540c\u65f6\u4e5f\u662f\u53ef\u4ee5\u88ab\u63a5\u53d7\u7684\uff08\u5728\u6709\u4e9b\u573a\u666f\u4e0b\u4e5f\u662f\u65e0\u6cd5\u907f\u514d\u7684\uff09\u3002","title":"4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83"},{"location":"4-5-Unsigned-integers-and-why-to-avoid-them/#_1","text":"\u5728\u4e0a\u8282\u8bfe ( 4.4 - \u6709\u7b26\u53f7\u6574\u578b ) \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u6709\u7b26\u53f7\u6574\u578b\uff0c\u8fd9\u4e9b\u5f81\u4fe1\u53ef\u4ee5\u4fdd\u5b58\u6b63\u8d1f\u6574\u6570\u548c 0\u3002 C++ \u8fd8\u652f\u6301\u65e0\u7b26\u53f7\u6574\u578b\u3002\u65e0\u7b26\u53f7\u6574\u578b\u53ea\u80fd\u8868\u793a\u975e\u7b26\u6574\u6570\u3002","title":"\u65e0\u7b26\u53f7\u6574\u578b"},{"location":"4-5-Unsigned-integers-and-why-to-avoid-them/#_2","text":"\u5b9a\u4e49\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u9700\u8981\u4f7f\u7528 unsigned \u5173\u952e\u5b57\u3002\u901a\u5e38\u6765\u8bb2\uff0c\u8be5\u5173\u952e\u5b57\u5e94\u8be5\u5199\u5728\u7c7b\u578b\u524d\uff1a 1 2 3 4 unsigned short us ; unsigned int ui ; unsigned long ul ; unsigned long long ull ;","title":"\u5b9a\u4e49\u65e0\u7b26\u53f7\u6574\u578b"},{"location":"4-5-Unsigned-integers-and-why-to-avoid-them/#_3","text":"1 \u5b57\u8282(8 \u4f4d)\u65e0\u7b26\u53f7\u6574\u578b\u7684\u8303\u56f4\u4e3a 0 \u548c 255\uff0c\u800c 1 \u5b57\u8282\u6709\u7b26\u53f7\u6574\u578b\u7684\u8303\u56f4\u662f -128 \u5230 127\u3002\u5b83\u4eec\u90fd\u80fd\u591f\u5b58\u653e 256 \u4e2a\u4e0d\u540c\u7684\u503c\uff0c\u4f46\u662f\u6709\u7b26\u53f7\u6574\u578b\u7684\u4e00\u534a\u8303\u56f4\u88ab\u7528\u6765\u8868\u793a\u8d1f\u6570\uff0c\u800c\u65e0\u7b26\u53f7\u6574\u578b\u5219\u5168\u90e8\u7528\u6765\u5b58\u653e\u6574\u6570\uff0c\u56e0\u6b64\u80fd\u591f\u8868\u793a\u7684\u6b63\u6570\u8303\u56f4\u662f\u6709\u7b26\u53f7\u6574\u578b\u7684\u4e24\u500d\u3002 \u4e0b\u8868\u5c55\u793a\u4e86\u4e0d\u540c\u6709\u7b26\u53f7\u6574\u578b\u53ef\u4ee5\u8868\u793a\u7684\u8303\u56f4\uff1a \u5927\u5c0f/\u7c7b\u578b \u8303\u56f4 1 byte unsigned 0 \u5230 255 2 byte unsigned 0 \u5230 65,535 4 byte unsigned 0 \u5230 4,294,967,295 8 byte unsigned 0 \u5230 18,446,744,073,709,551,615 \u4e00\u4e2a n \u4e3a\u7684\u65e0\u7b26\u53f7\u6574\u578b\u53ef\u8868\u793a\u7684\u8303\u56f4\u4e3a 0 \u5230 \\((2^n)-1\\) \u3002 \u5982\u679c\u4e0d\u9700\u8981\u8868\u793a\u8d1f\u6570\uff0c\u90a3\u4e48\u65e0\u7b26\u53f7\u6574\u578b\u66f4\u9002\u5408\u5728\u5185\u5b58\u6bd4\u8f83\u5c0f\u7684\u7f51\u7edc\u548c\u7cfb\u7edf\u4e0a\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5728\u4e0d\u5360\u7528\u66f4\u591a\u5185\u5b58\u7684\u524d\u63d0\u4e0b\u4fdd\u5b58\u66f4\u591a\u7684\u6b63\u6574\u6570\u3002","title":"\u65e0\u7b26\u53f7\u6574\u578b\u7684\u8303\u56f4"},{"location":"4-5-Unsigned-integers-and-why-to-avoid-them/#signed-unsigned","text":"\u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u7ecf\u5e38\u628a\u6709\u7b26\u53f7(signed) \u548c\u65e0\u7b26\u53f7(unsigned)\u8fd9\u4e24\u4e2a\u672f\u8bed\u641e\u6df7\u3002\u4ee5\u4e0b\u4ecb\u7ecd\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u8bb0\u5fc6\u5b83\u4eec\u7684\u4e0d\u540c\uff1a\u4e3a\u4e86\u533a\u5206\u8d1f\u6570\u548c\u6b63\u6570\uff0c\u6211\u4eec\u9700\u8981\u8d1f\u53f7\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u7b26\u53f7\uff0c\u6211\u4eec\u5047\u5b9a\u8be5\u6570\u4e3a\u6b63\u6570\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u5177\u6709\u7b26\u53f7\u7684\u6574\u578b\uff08\u6709\u7b26\u53f7\u6574\u578b\uff09\u53ef\u4ee5\u533a\u5206\u6b63\u8d1f\u3002\u800c\u4e0d\u5177\u6709\u7b26\u53f7\u7684\u6574\u578b\uff08\u6709\u7b26\u53f7\u6574\u578b\uff09\u5219\u5168\u90e8\u4e3a\u6b63\u6570\u3002","title":"\u5206\u6e05\u6709\u7b26\u53f7(signed) \u548c\u65e0\u7b26\u53f7(unsigned)"},{"location":"4-5-Unsigned-integers-and-why-to-avoid-them/#_4","text":"What happens if we try to store the number 280 (which requires 9 bits to represent) in a 1-byte (8-bit) unsigned integer? The answer is overflow. \u5982\u679c\u6211\u4eec\u5c1d\u8bd5\u628a 280 \uff08\u9700\u8981 9 \u4e2a\u4f4d\u6765\u8868\u793a\uff09\u8d4b\u503c\u7ed9\u4e00\u4e2a 8 \u4f4d\u65e0\u7b26\u53f7\u6574\u578b\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f\u7b54\u6848\u662f\u2014\u2014\u6ea2\u51fa\u3002 \u4f5c\u8005\u6ce8 \u5947\u602a\u7684\u662f\uff0cC++\u6807\u51c6\u4e2d\u660e\u786e\u5199\u9053\uff0c\u65e0\u7b26\u53f7\u64cd\u4f5c\u6570\u53c2\u4e0e\u7684\u8fd0\u7b97\u6c38\u8fdc\u4e0d\u4f1a\u6ea2\u51fa\u3002\u8fd9\u548c\u6211\u4eec\u7684\u7f16\u7a0b\u5e38\u8bc6\uff08\u65e0\u7b26\u53f7\u548c\u6709\u7b26\u53f7\u6574\u578b\u90fd\u4f1a\u6ea2\u51fa\uff09\u662f\u77db\u76fe\u7684\u3002( \u53c2\u8003\u6587\u732e )\u3002\u7531\u4e8e\u5927\u591a\u6570\u7684\u7a0b\u5e8f\u5458\u90fd\u4f1a\u8003\u8651\u8be5\u6ea2\u51fa\uff0c\u6211\u4eec\u8fd9\u91cc\u4e5f\u6cbf\u7528\u4e86\u8be5\u8bf4\u6cd5\uff0c\u5c3d\u7ba1\u548cC++\u6807\u51c6\u6709\u6240\u77db\u76fe\u3002 \u5f53\u65e0\u7b26\u53f7\u503c\u8d85\u8fc7\u8303\u56f4\u65f6\uff0c\u5b83\u4f1a\u88ab\u9664\u4ee5\u8be5\u7c7b\u578b\u7684\u6700\u5927\u503c\u52a0 1\uff0c\u4e14\u53ea\u4fdd\u7559\u4f59\u6570\u3002 280 \u4e0d\u5728 0 \u5230 255 \u7684\u8303\u56f4\u5185\u3002\u8be5\u8303\u56f4\u7684\u6700\u5927\u503c+1 \u662f 256\uff0c\u6240\u4ee5 280 \u4f1a\u9664\u4ee5 256\uff0c\u4f59\u6570\u4e3a 24\u3002\u6700\u7ec8 24 \u4f1a\u88ab\u5b58\u653e\u5230\u5185\u5b58\u4e2d\u3002 \u8fd9\u4e2a\u95ee\u9898\u8fd8\u53ef\u4ee5\u8fd9\u6837\u7406\u89e3\u3002\u6bd4\u6700\u5927\u503c\u8fd8\u5927\u7684\u6570\u5728\u5b58\u653e\u65f6\u4f1a\u88ab\u201cwraps around\u201d (\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a\u201cmodulo wrapping\u201d)\uff0c\u5373\u4ece 0 \u5f00\u59cb\u91cd\u65b0\u8ba1\u7b97\u3002255 \u5728 1 \u5b57\u8282\u6574\u578b\u7684\u8868\u793a\u8303\u56f4\u5185\uff0c\u6240\u4ee5 255 \u6ca1\u95ee\u9898\u3002\u800c 256 \u5219\u8d85\u8fc7\u4e86\u8303\u56f4\uff0c\u56e0\u6b64\u5b83\u4f1a\u88ab\u5f52\u4e3a 0\u3002257 \u5219\u4f1a\u5f52\u4e3a 1\u3002280 \u5219\u5f52\u4e3a 24. \u8fd9\u91cc\u662f\u4e00\u4e2a 2 \u5b57\u8282\u65e0\u7b26\u53f7\u77ed\u6574\u578b\uff08 unsigned short \uff09 \u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { unsigned short x { 65535 }; // largest 16-bit unsigned value possible std :: cout << \"x was: \" << x << '\\n' ; x = 65536 ; // 65536 is out of our range, so we get wrap-around std :: cout << \"x is now: \" << x << '\\n' ; x = 65537 ; // 65537 is out of our range, so we get wrap-around std :: cout << \"x is now: \" << x << '\\n' ; return 0 ; } \u8bd1\u8005\u6ce8 \u65e0\u7b26\u53f7\u4e0d\u4f1a\u6ea2\u51fa\uff0c\u7684\u786e\u662f C++\u6807\u51c6\uff0c\u4e0d\u6ea2\u51fa\u8fd9\u4e2a\u8bf4\u6cd5\u4e5f\u662f\u5408\u7406\u7684\uff0c\u56e0\u4e3a\u65e0\u7b26\u53f7\u5728\u9047\u5230\u8d85\u8fc7\u8868\u793a\u8303\u56f4\u7684\u60c5\u51b5\u65f6\u5e76\u4e0d\u4f1a\u8868\u73b0\u51fa \u672a\u5b9a\u4e49\u884c\u4e3a \uff0c\u7ed3\u679c\u4e5f\u4e0d\u662f\u7b80\u5355\u7684\u4e22\u5f03\uff0c\u800c\u662f\u5bf9\u539f\u503c\u8fdb\u884c\u6c42\u6a21\u8fd0\u7b97\u3002\u82f1\u6587\u53eb warp around\uff0c\u5373\u4ece 0 \u5f00\u59cb\u91cd\u65b0\u8ba1\u7b97\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e 32 \u4f4d\u65e0\u7b26\u53f7\uff0c\u8d85\u51fa\u8303\u56f4\u65f6\u7ed3\u679c\u7b49\u4e8e value mod 2^32 \u4f60\u89c9\u5f97\u7a0b\u5e8f\u7684\u7ed3\u679c\u4f1a\u662f\u4ec0\u4e48\u5462\uff1f (\u6ce8\u610f\uff0c\u5982\u679c\u4f60\u7f16\u8bd1\u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8bdd\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u4ea7\u751f\u5173\u4e8e\u6ea2\u51fa\u6216\u622a\u65ad\u7684\u544a\u8b66\u2014\u2014\u4f60\u9700\u8981\u5173\u95ed\u201d\u5c06\u544a\u8b66\u770b\u505a\u9519\u8bef\u201c\u529f\u80fd\uff0c\u624d\u80fd\u8fd0\u884c\u8be5\u7a0b\u5e8f\uff09 1 2 3 x was: 65535 x is now: 0 x is now: 1 \u5411\u53e6\u4e00\u4e2a\u65b9\u5411 wrap \u4e5f\u662f\u53ef\u4ee5\u7684\uff0c0 \u53ef\u4ee5\u88ab 2 \u5b57\u8282\u5927\u5c0f\u7684\u65e0\u7b26\u53f7\u6574\u578b\u8868\u793a\uff0c\u4f46\u662f-1 \u4e0d\u53ef\u4ee5\uff0c\u56e0\u6b64\u5b83\u4f1a\u88ab wrap \u5230\u6700\u5927\u503c\uff0c\u7ed3\u679c\u4e3a 65535\u3002-2 \u7684\u7ed3\u679c\u662f 65534\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { unsigned short x { 0 }; // smallest 2-byte unsigned value possible std :: cout << \"x was: \" << x << '\\n' ; x = -1 ; // -1 is out of our range, so we get wrap-around std :: cout << \"x is now: \" << x << '\\n' ; x = -2 ; // -2 is out of our range, so we get wrap-around std :: cout << \"x is now: \" << x << '\\n' ; return 0 ; } 1 2 3 x was: 0 x is now: 65535 x is now: 65534 \u4e0a\u8ff0\u4ee3\u7801\u4f1a\u5f15\u8d77\u67d0\u4e9b\u7f16\u8bd1\u5668\u4ea7\u751f\u544a\u8b66\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u68c0\u6d4b\u5230\u6574\u578b\u5b57\u9762\u91cf\u8d85\u8fc7\u4e86\u8868\u793a\u5b83\u7684\u7c7b\u578b\u7684\u8303\u56f4\u3002\u5982\u679c\u4f60\u5e0c\u671b\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\uff0c\u8bf7\u6682\u65f6\u5173\u95ed\u201d\u5c06\u544a\u8b66\u770b\u505a\u9519\u8bef\u201c\u529f\u80fd\u3002 \u9898\u5916\u8bdd \u5f88\u591a\u7535\u5b50\u6e38\u620f\u53f2\u4e0a\u7684\u8457\u540d\u7684 bug \u90fd\u548c\u65e0\u7b26\u53f7\u6570\u53cd\u8f6c\u6709\u5173\u3002\u8857\u673a\u6e38\u620f\u300a\u5927\u91d1\u521a\u300b\u4e2d\uff0c\u4f60\u4e0d\u53ef\u80fd\u901a\u8fc722\u5173\uff0c\u56e0\u4e3a\u6ea2\u51fabug\u5bfc\u81f4\u7528\u6237\u5728\u8fd9\u4e00\u5173\u4e2d\u4e0d\u53ef\u80fd\u6709\u8db3\u591f\u7684\u65f6\u95f4\u3002 \u7535\u8111\u6e38\u620f\u300a\u6587\u660e\u300b\u4e2d\uff0c\u7518\u5730\u901a\u5e38\u662f\u7b2c\u4e00\u4e2a\u4f7f\u7528\u6838\u6b66\u5668\u7684\uff0c\u8fd9\u548c\u4ed6\u672c\u5e94\u8be5\u6709\u7684\u4fdd\u5b88\u7684\u5c5e\u6027\u975e\u5e38\u77db\u76fe\u3002\u73a9\u5bb6\u8ba4\u4e3a\u8fd9\u662f\u7531\u4e8e\u7518\u5730\u7684\u653b\u51fb\u6027\u88ab\u521d\u59cb\u5316\u62101\uff0c\u4f46\u662f\u5982\u679c\u4ed6\u8d70\u6c11\u4e3b\u8def\u7ebf\u7684\u8bdd\uff0c\u5176\u653b\u51fb\u6027\u4f1a-2\u3002\u8fdb\u800c\u5bfc\u81f4\u4e86\u6570\u503c\u88ab\u53cd\u8f6c\u6210\u4e86255\uff0c\u4f7f\u5176\u53d8\u5f97\u975e\u5e38\u597d\u6597\u3002\u4e0d\u8fc7\uff0c\u6700\u8fd1\u5e2d\u5fb7\u6885\u5c14\u5bf9\u6b64\u8fdb\u884c\u4e86\u6f84\u6e05\uff0c\u4ed6\u8bf4\u8fd9\u5e76\u4e0d\u662f\u5f15\u8d77\u4e0a\u8ff0\u95ee\u9898\u7684\u539f\u56e0\u3002","title":"\u65e0\u7b26\u53f7\u6574\u578b\u6ea2\u51fa"},{"location":"4-5-Unsigned-integers-and-why-to-avoid-them/#_5","text":"\u5f88\u591a\u7a0b\u5e8f\u5458 (\u4ee5\u53ca\u4e00\u4e9b\u5927\u5382\uff0c\u4f8b\u5982\u8c37\u6b4c\uff09\u8ba4\u4e3a\u7a0b\u5e8f\u4e5f\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u65e0\u7b26\u53f7\u6574\u578b\u3002 \u8fd9\u4e3b\u8981\u662f\u56e0\u4e3a\u65e0\u7b26\u53f7\u6574\u578b\u6709\u4e24\u79cd\u884c\u4e3a\u53ef\u80fd\u4f1a\u5bfc\u81f4\u95ee\u9898\u3002 \u9996\u5148\uff0c\u5c06\u4e24\u4e2a\u65e0\u7b26\u53f7\u6574\u578b\u76f8\u51cf\uff0c\u4f8b\u59823-5\u7b49\u4e8e-2\uff0c\u4f46\u662f-2\u4e0d\u80fd\u88ab\u65e0\u7b26\u53f7\u6574\u578b\u8868\u793a\u3002 1 2 3 4 5 6 7 8 9 10 #include int main () { unsigned int x { 3 }; unsigned int y { 5 }; std :: cout << x - y << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\uff0c\u5c3d\u7ba1\u4e0a\u9762\u7684\u4ee3\u7801\u770b\u4e0a\u53bb\u4eba\u755c\u65e0\u5bb3\uff0c\u5b9e\u9645\u4e0a\u7ed3\u679c\u5374\u4ee4\u4eba\u5403\u60ca\uff1a 1 4294967294 \u8fd9\u662f\u56e0\u4e3a -2 \u88ab\u53cd\u8f6c\u5230\u4e86\u4e00\u4e2a\u63a5\u8fd1 4\u81ea\u5df1\u6574\u578b\u6700\u5927\u503c\u7684\u6570\u3002\u53e6\u5916\u4e00\u4e2a\u53ef\u80fd\u4ea7\u751f\u53cd\u8f6c\u7684\u60c5\u51b5\u662f\uff0c\u5bf9\u65e0\u7b26\u53f7\u6574\u578b\u4f7f\u7528\u9012\u51cf\u8fd0\u7b97\u7b26( -- )\u3002\u90a3\u4e48\u5f53\u53d8\u91cf\u5faa\u73af\u9012\u51cf\u65f6\uff0c\u6700\u7ec8\u53ef\u80fd\u4f1a\u53d1\u751f\u4e8e\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\u540c\u6837\u7684\u95ee\u9898\u3002 . \u6b64\u5916\uff0c\u5f53\u5c06\u6709\u7b26\u53f7\u6574\u578b\u548c\u65e0\u7b26\u53f7\u6574\u578b\u6df7\u7528\u65f6\uff0c\u4e5f\u53ef\u80fd\u4ea7\u751f\u610f\u5916\u3002\u5728C++\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u65f6\uff08\u4f8b\u5982\u7b97\u6570\u6216\u6bd4\u8f83\uff09\uff0c\u5982\u679c\u4e24\u4e2a\u64cd\u4f5c\u6570\u5206\u522b\u4e3a\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u90a3\u4e48\u6709\u7b26\u53f7\u7684\u4e00\u65b9\u4f1a\u88ab\u8f6c\u6362\u4e3a\u65e0\u7b26\u53f7\u3002\u7136\u540e\uff0c\u56e0\u4e3a\u65e0\u7b26\u53f7\u6574\u578b\u4e0d\u53ef\u4ee5\u4fdd\u5b58\u8d1f\u6570\uff0c\u8fd9\u5c31\u6709\u53ef\u80fd\u9020\u6210\u6570\u636e\u4e22\u5931\u7684\u60c5\u51b5\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { signed int s { -1 }; unsigned int u { 1 }; if ( s < u ) // -1 \u88ab\u9690\u5f0f\u8f6c\u6362\u6210\u4e86 4294967295, \u800c 4294967295 < 1 \u662f\u9519\u8bef\u7684 std :: cout << \"-1 is less than 1 \\n \" ; else std :: cout << \"1 is less than -1 \\n \" ; // \u8fd9\u53e5\u8bed\u53e5\u4f1a\u88ab\u6267\u884c return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u7f16\u8bd1\uff0c\u770b\u4e0a\u53bb\u4e5f\u7b26\u5408\u903b\u8f91\u3002\u4f46\u662f\u5b83\u6253\u5370\u7684\u7ed3\u679c\u5374\u662f\u9519\u8bef\u7684\u3002\u5c3d\u7ba1\u7f16\u8bd1\u5668\u5728\u68c0\u67e5\u5230\u6709\u7b26\u53f7/\u65e0\u7b26\u53f7\u4e0d\u5339\u914d\u65f6\uff0c\u4f1a\u53d1\u51fa\u4e00\u4e2a\u8b66\u544a\u3002\u4f46\u662f\u7531\u4e8e\u8fd8\u6709\u5f88\u591a\u5176\u4ed6\u60c5\u51b5\u4e0b\u7f16\u8bd1\u5668\u4e5f\u4f1a\u62a5\u544a\u5b8c\u5168\u4e00\u6837\u7684\u8b66\u544a\uff08\u4f8b\u5982\u4e24\u4e2a\u6570\u90fd\u662f\u6b63\u6570\u65f6\uff09\uff0c\u8fd9\u4f1a\u8ba9\u6211\u4eec\u5f88\u96be\u5224\u65ad\u771f\u6b63\u7684\u95ee\u9898\u5728\u54ea\u91cc\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 4.10 - if \u8bed\u53e5 \u4e2d\u4ecb\u7ecd if \u8bed\u53e5\u3002 \u6b64\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u53ef\u80fd\u6709\u95ee\u9898\u7684\u60c5\u51b5\uff0c\u662f\u6839\u672c\u65e0\u6cd5\u88ab\u68c0\u67e5\u5230\u7684\u3002\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 void doSomething ( unsigned int x ) { // Run some code x times std :: cout << \"x is \" << x << '\\n' ; } int main () { doSomething ( -1 ); return 0 ; } doSomething() \u51fd\u6570\u7684\u4f5c\u8005\u671f\u671b\u7528\u6237\u53ea\u4f20\u9012\u6b63\u6570\u7ed9\u8be5\u51fd\u6570\u3002\u4f46\u662f\u8c03\u7528\u8005\u72af\u4e86\u4e00\u4e2a\u9519\u8bef\uff0c\u5b83\u4f20\u4e86-1\u3002\u8fd9\u662f\u4e2a\u5f88\u663e\u7136\u7684\u9519\u8bef\uff0c\u4f46\u662f\u4eba\u4eec\u4ecd\u7136\u96be\u514d\u4f1a\u72af\u3002\u90a3\u4e48\u6b64\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f \u5b9e\u53c2 -1 \u88ab\u9690\u5f0f\u8f6c\u6362\u6210\u4e86\u4e00\u4e2a\u65e0\u7b26\u53f7 \u5f62\u53c2 \u3002-1 \u4e0d\u5728\u65e0\u7b26\u53f7\u6574\u578b\u7684\u8868\u793a\u8303\u56f4\uff0c\u56e0\u6b64\u5fc5\u7136\u88ab\u53cd\u8f6c\u6210\u4e00\u4e2a\u975e\u5e38\u5927\u7684\u6570(\u53ef\u80fd\u662f 4294967295)\u3002\u7136\u540e\u4f60\u7684\u7a0b\u5e8f\u5c31\u4f1a\u51fa\u73b0\u95ee\u9898\u3002 \u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u5e76\u6ca1\u6709\u4e00\u79cd\u65b9\u6cd5\u80fd\u591f\u907f\u514d\u4e0a\u8ff0\u95ee\u9898\u7684\u53d1\u751f\u3002C++ \u53ef\u4ee5\u81ea\u7531\u5730\u8f6c\u6362\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u6570\uff0c\u4f46\u662f\u5b83\u5e76\u4e0d\u4f1a\u5728\u8f6c\u6362\u65f6\u8fdb\u884c\u4efb\u4f55\u7684\u8303\u56f4\u68c0\u67e5\u4ee5\u786e\u4fdd\u4e0d\u53d1\u751f\u6ea2\u51fa\u3002 \u4e0a\u8ff0\u8fd9\u4e9b\u95ee\u9898\u90fd\u662f\u7ecf\u5e38\u4f1a\u9047\u5230\u7684\uff0c\u5b83\u4eec\u4ea7\u751f\u610f\u6599\u4e4b\u5916\u7684\u884c\u4e3a\uff0c\u5e76\u4e14\u96be\u4ee5\u88ab\u53d1\u73b0\u3002\u5373\u4f7f\u4f7f\u7528\u4e00\u4e9b\u88ab\u4e13\u7528\u4e8e\u67e5\u627e\u6b64\u7c7b\u95ee\u9898\u7684\u5de5\u5177\u6709\u5f88\u96be\u53d1\u73b0\u3002 \u56e0\u6b64\uff0c\u4e00\u4e9b\u6709\u4e89\u8bae\u7684\u6700\u4f73\u5b9e\u8df5\u8ba4\u4e3a\uff0c\u6211\u4eec\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u65e0\u7b26\u53f7\u7c7b\u578b\uff0c\u9664\u975e\u786e\u5b9e\u6709\u7279\u6b8a\u7684\u9700\u6c42\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c3d\u91cf\u4f7f\u7528\u6709\u7b26\u53f7\u6570\u6765\u5b58\u653e\u6570\u91cf\u503c\uff08\u5c3d\u7ba1\u6570\u91cf\u53ef\u80fd\u90fd\u662f\u975e\u8d1f\u7684\uff09\u548c\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u3002\u907f\u514d\u6df7\u7528\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u7c7b\u578b\u3002 \u76f8\u5173\u5185\u5bb9 \u652f\u6301\u4e0a\u8ff0\u5efa\u8bae\u7684\u4e00\u4e9b\u6750\u6599(\u540c\u65f6\u4e5f\u9a73\u65a5\u4e86\u4e00\u4e9b\u5e38\u89c1\u7684\u53cd\u5bf9\u610f\u89c1\uff09\uff1a Interactive C++ panel (see 12:12-13:08,42:40-45:26, and 1:02:50-1:03:15) Subscripts and sizes should be signed Unsigned integers from the libtorrent blog","title":"\u6709\u5173\u65e0\u7b26\u53f7\u6574\u578b\u7684\u4e89\u8bba"},{"location":"4-5-Unsigned-integers-and-why-to-avoid-them/#_6","text":"\u8fd8\u662f\u6709\u4e00\u4e9b\u573a\u5408\u662f\u9700\u8981\u6216\u8005\u53ef\u4ee5\u4f7f\u7528\u65e0\u7b26\u53f7\u6570\u7684\u3002 \u9996\u5148\uff0c\u65e0\u7b26\u53f7\u6570\u5728\u8fdb\u884c\u4f4d\u8fd0\u7b97\u65f6\u662f\u63a8\u8350\u4f7f\u7528\u7684\uff08\u5728\u7ae0\u8282O\u4e2d\u4ecb\u7ecd\uff09\u3002\u5728\u4e00\u4e9b\u786e\u5b9e\u9700\u8981\u53cd\u8f6c\u884c\u4e3a\u7684\u573a\u5408\uff0c\u65e0\u7b26\u53f7\u6570\u4e5f\u662f\u6709\u7528\u7684\uff08\u4f8b\u5982\u4e00\u4e9b\u52a0\u5bc6\u7b97\u6cd5\u548c\u968f\u673a\u6570\u751f\u6210\uff09\u3002 \u5176\u6b21\uff0c\u5728\u6709\u4e9b\u65f6\u5019\u65e0\u7b26\u53f7\u6570\u662f\u65e0\u6cd5\u907f\u514d\u7684\uff08\u5f88\u591a\u4e0e\u6570\u7ec4\u8fdb\u884c\u7d22\u5f15\u6709\u5173\uff09\u3002\u6211\u4eec\u4f1a\u5728\u6709\u5173\u6570\u7ec4\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002\u5728\u8fd9\u4e9b\u60c5\u51b5\u4e0b\uff0c\u65e0\u7b26\u53f7\u6570\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u6570\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast \u4e2d\u4ecb\u7ecd\u5982\u4f55\u5c06\u65e0\u7b26\u53f7\u6570\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u6570\u3002 \u540c\u65f6\u8bf7\u6ce8\u610f\uff0c\u5982\u679c\u4f60\u5728\u5f00\u53d1\u5d4c\u5165\u5f0f\u7cfb\u7edf\uff08\u4f8b\u5982 Arduino\uff09\u6216\u8005\u5176\u4ed6\u5904\u7406\u5668/\u5185\u5b58\u6bd4\u8f83\u5403\u7d27\u7684\u73af\u5883\uff0c\u51fa\u4e8e\u6027\u80fd\u7684\u539f\u56e0\u800c\u4f7f\u7528\u65e0\u7b26\u53f7\u6570\u662f\u975e\u5e38\u5e38\u89c1\u7684\uff0c\u540c\u65f6\u4e5f\u662f\u53ef\u4ee5\u88ab\u63a5\u53d7\u7684\uff08\u5728\u6709\u4e9b\u573a\u666f\u4e0b\u4e5f\u662f\u65e0\u6cd5\u907f\u514d\u7684\uff09\u3002","title":"\u90a3\u4e48\u4ec0\u4e48\u65f6\u5019\u624d\u4f7f\u7528\u65e0\u7b26\u53f7\u6570\u5462\uff1f"},{"location":"4-6-Fixed-width-integers-and-size_t/","tags":["data type","fixed-width","integer","size_t","C++11"],"text":"Key Takeaway \u4eceC\u8bed\u8a00\u7ee7\u627f\u800c\u6765\u7684\u6574\u578b\u5927\u5c0f\u662f\u4e0d\u56fa\u5b9a\u7684\uff0c\u6700\u5c0f\u662f2\u5b57\u8282\uff0c\u4f46\u662f\u66f4\u591a\u65f6\u5019\u662f4\u5b57\u8282\u3002 C99\u548cC++11\u63d0\u4f9b\u4e86\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\uff0c\u901a\u8fc7\u5934\u6587\u4ef6 \u53ef\u4ee5\u4f7f\u7528\u3002\u7f3a\u70b9\u662f\u5e76\u975e\u6240\u6709\u673a\u5668\u4e0a\u90fd\u6709\u5b9a\u4e49\u4e14\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd C++\u63d0\u4f9b\u4e86\u901f\u5ea6\u4f18\u5148\u548c\u5927\u5c0f\u4f18\u5148\u7684\u4e24\u7c7b\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b \u907f\u514d\u4f7f\u75288\u4f4d\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u3002\u5982\u679c\u4e00\u5b9a\u8981\u4f7f\u7528\uff0c\u8bf7\u6ce8\u610f\u5b83\u4eec\u7684\u884c\u4e3a\u66f4\u7c7b\u4f3c\u4e8e\u5b57\u7b26\u3002 \u6574\u578b\u4f7f\u7528\u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u6574\u578b\u7684\u5927\u5c0f\u4e0d\u91cd\u8981\uff08\u4f8b\u5982\u5b58\u653e\u7684\u6570\u603b\u662f\u57282\u5b57\u8282\u6709\u7b26\u53f7\u6574\u578b\u8303\u56f4\u5185\uff09\uff0c\u63a8\u8350\u4f7f\u7528 int \u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u8981\u6c42\u7528\u6237\u8f93\u5165\u5e74\u9f84\uff0c\u6216\u8005\u8fdb\u884c10\u4ee5\u5185\u7684\u8ba1\u6570\uff08\u6b64\u65f62\u5b57\u8282\u8303\u56f4\u603b\u662f\u591f\u7528\u7684\uff09\u3002\u90a3\u4e48\u4f7f\u7528 int \u5c31\u8db3\u591f\u5e94\u5bf9\u7edd\u5927\u591a\u6570\u573a\u666f\u4e86\uff1b \u5982\u679c\u9700\u8981\u5b58\u50a8\u6570\u91cf\u503c\uff0c\u4e14\u8303\u56f4\u5fc5\u987b\u660e\u786e\u65f6\uff0c\u63a8\u8350\u4f7f\u7528 std::int#_t \uff1b \u5982\u679c\u9700\u8981\u8fdb\u884c\u4f4d\u8fd0\u7b97\u6216\u60f3\u8981\u5229\u7528\u65e0\u7b26\u53f7\u6570\u7684\u7ffb\u8f6c\u7279\u6027\u65f6\uff0c\u4f7f\u7528 std::uint#_t \u3002 \u907f\u514d\uff1a \u4f7f\u7528\u65e0\u7b26\u53f7\u7c7b\u578b\u4fdd\u5b58\u6570\u91cf\u503c\uff1b \u4f7f\u7528 8 \u4e3a\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\uff1b \u4f7f\u7528\u901f\u5ea6\u6216\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u7c7b\u578b\uff1b \u4efb\u4f55\u4e0e\u7279\u5b9a\u7f16\u8bd1\u5668\u76f8\u5173\u7684\u56fa\u5b9a\u6574\u578b\u7c7b\u578b\u2014\u2014\u4f8b\u5982 Visual Studio \u5b9a\u4e49\u7684 __int8 \uff0c __int16 \u7b49\u3002 std::size_t \u662f\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u5176\u5bbd\u5ea6\u901a\u5e38\u7b49\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\u4f4d\u6570\uff0c\u540c\u65f6\u5176\u80fd\u591f\u8868\u793a\u7684\u6700\u5927\u503c\u4e5f\u662f\u7cfb\u7edf\u80fd\u591f\u521b\u5efa\u5bf9\u8c61\u5927\u5c0f\u7684\u7406\u8bba\u4e0a\u9650 \u4e0a\u9762\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u6574\u578b\uff0c\u5e76\u4e14\u63d0\u5230 C++ \u53ea\u4fdd\u8bc1\u6574\u578b\u503c\u5360\u7528\u7a7a\u95f4\u7684\u6700\u5c0f\u503c\u2014\u2014\u4f46\u662f\u5b83\u5360\u7528\u7684\u7a7a\u95f4\u53ef\u4ee5\u66f4\u5927\uff0c\u8fd9\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u7cfb\u7edf\u3002 \u4e3a\u4ec0\u4e48\u6574\u578b\u7684\u5927\u5c0f\u4e0d\u662f\u56fa\u5b9a\u7684\uff1f \u7b80\u800c\u8a00\u4e4b\uff0c\u8fd9\u8fd8\u662f\u4ece C \u8bed\u8a00\u7ee7\u627f\u800c\u6765\u7684\u3002\u5728\u8ba1\u7b97\u673a\u975e\u5e38\u6162\u7684\u5e74\u4ee3\uff0c\u6027\u80fd\u662f\u6700\u9700\u8981\u88ab\u8003\u8651\u7684\u95ee\u9898\u3002C \u8bed\u8a00\u9009\u62e9\u6545\u610f\u4e0d\u53bb\u56fa\u5b9a\u6574\u578b\u7684\u5927\u5c0f\uff0c\u4ee5\u4fbf\u7f16\u8bd1\u5668\u7684\u5b9e\u73b0\u8005\u53ef\u4ee5\u4e3a\u76ee\u6807\u8ba1\u7b97\u673a\u9009\u62e9\u6700\u5408\u9002\u7684\u6574\u578b\u5927\u5c0f\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002 \u5f88\u8ba8\u538c\u662f\u5427\uff1f \u4ece\u73b0\u4ee3\u6807\u51c6\u7684\u89d2\u5ea6\u6765\u8bb2\uff0c\u662f\u7684\u3002\u4f5c\u4e3a\u7a0b\u5e8f\u5458\u8fd8\u8981\u5904\u7406\u7c7b\u578b\u8303\u56f4\u4e0d\u786e\u5b9a\u7684\u60c5\u51b5\u786e\u5b9e\u6709\u70b9\u8352\u5510\u3002 \u8003\u8651 int \u7c7b\u578b\uff0c\u5176\u5360\u7528\u5185\u5b58\u6700\u5c0f\u4e3a 2 \u5b57\u8282\uff0c\u4f46\u662f\u5728\u73b0\u4ee3\u8ba1\u7b97\u673a\u4e0a\u5374\u7ecf\u5e38\u4e3a 4 \u5b57\u8282\u3002\u5982\u679c\u4f60\u5047\u8bbe int \u662f 4 \u4e2a\u5b57\u8282\uff08\u786e\u5b9e 4 \u5b57\u8282\u60c5\u51b5\u6700\u6709\u53ef\u80fd\uff09\uff0c\u90a3\u4e48\u4f60\u7684\u7a0b\u5e8f\u5728 int \u5b9e\u9645\u53ea\u5360\u4e24\u4e2a\u5b57\u8282\u7684\u8ba1\u7b97\u673a\u4e0a\u5c31\u4f1a\u4ea7\u751f\u5f02\u5e38\u7684\u884c\u4e3a\uff08\u56e0\u4e3a\u4f60\u5f88\u53ef\u80fd\u628a\u4e00\u4e2a\u9700\u8981 4 \u5b57\u8282\u624d\u80fd\u5b58\u653e\u7684\u503c\u5b58\u653e\u5230\u4e86\u5b9e\u9645\u53ea\u6709 2 \u4e2a\u5b57\u8282\u7684\u53d8\u91cf\u4e2d\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u6ea2\u51fa\u6216 \u672a\u5b9a\u4e49\u884c\u4e3a \uff09\u3002\u800c\u5982\u679c\u4f60\u5047\u8bbe int \u53ea\u6709\u4e24\u4e2a\u5b57\u8282\u4ee5\u4fbf\u83b7\u5f97\u6700\u597d\u7684\u517c\u5bb9\u6027\uff0c\u90a3\u4e48\u5728 4 \u5b57\u8282\u7cfb\u7edf\u4e0a\u65e0\u7591\u4f1a\u6d6a\u8d39\u4e24\u4e2a\u5b57\u8282\uff0c\u8fd9\u4f1a\u4f7f\u5f97\u4f60\u7684\u5185\u5b58\u4f7f\u7528\u6210\u500d\u589e\u52a0\uff01 \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cC99 \u5b9a\u4e49\u4e86\u4e00\u7ec4\u56fa\u5b9a\u5bbd\u5ea6\u6574\u5f62\uff08\u4f4d\u4e8e stdint.h \uff09\u6765\u786e\u4fdd\u6574\u578b\u5728\u4e0d\u540c\u7684\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u4e0b\u90fd\u5177\u6709\u76f8\u540c\u7684\u5927\u5c0f\u3002 \u540d\u79f0 \u7c7b\u578b \u8303\u56f4 \u5907\u6ce8 std::int8_t 1 byte signed -128 \u5230 127 \u5728\u5927\u591a\u6570\u7cfb\u7edf\u4e0a\u88ab\u770b\u505a\u6709\u7b26\u53f7\u5b57\u7b26\u5904\u7406\uff0c\u89c1\u4e0b\u9762\u5907\u6ce8\u3002 std::uint8_t 1 byte unsigned 0 \u5230 255 \u5728\u5927\u591a\u6570\u7cfb\u7edf\u4e0a\u88ab\u770b\u505a\u65e0\u7b26\u53f7\u5b57\u7b26\u5904\u7406\uff0c\u89c1\u4e0b\u9762\u5907\u6ce8 std::int16_t 2 byte signed -32,768 \u5230 32,767 std::uint16_t 2 byte unsigned 0 \u5230 65,535 std::int32_t 4 byte signed -2,147,483,648 \u5230 2,147,483,647 std::uint32_t 4 byte unsigned 0 \u5230 4,294,967,295 std::int64_t 8 byte signed -9,223,372,036,854,775,808 \u5230 9,223,372,036,854,775,807 std::uint64_t 8 byte unsigned 0 \u5230 18,446,744,073,709,551,615 C++ \u5728 C++11 \u4e2d\u5b98\u65b9\u5438\u7eb3\u4e86\u8fd9\u4e9b\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u3002\u901a\u8fc7\u5305\u542b \u5934\u6587\u4ef6\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\uff0c\u5b83\u4eec\u88ab\u5b9a\u4e49\u5728 std \u547d\u540d\u7a7a\u95f4\u4e2d\u3002 \u8bf7\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 #include // for fixed-width integers #include int main () { std :: int16_t i { 5 }; std :: cout << i ; return 0 ; } \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u901a\u5e38\u6765\u8bb2\u6709\u4e24\u4e2a\u7f3a\u9677\u3002 \u9996\u5148\uff0c\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u4e0d\u80fd\u4fdd\u8bc1\u5728\u6240\u6709\u7684\u4f53\u7cfb\u7ed3\u6784\u4e2d\u90fd\u88ab\u5b9a\u4e49\u4e86\u3002\u5b83\u53ea\u5b58\u5728\u4e8e, They only exist on systems where there are fundamental types matching their widths and following a certain binary representation.\u5982\u679c\u4f53\u7cfb\u7ed3\u6784\u4e0d\u652f\u6301\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\uff0c\u5219\u4f60\u7684\u7a0b\u5e8f\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\u3002\u4e0d\u8fc7\uff0c\u5927\u591a\u6570\u7684\u73b0\u4ee3\u4f53\u7cfb\u7ed3\u6784\u90fd\u4ee5 8/16/32/64-\u4f4d\u7684\u53d8\u91cf\u4e3a\u6807\u51c6\uff0c\u56e0\u6b64\u9664\u975e\u4f60\u9700\u8981\u5c06\u7a0b\u5e8f\u79fb\u690d\u5230\u67d0\u4e2a\u6781\u5176\u7279\u6b8a\u7684\u5927\u578b\u673a\u6216\u5d4c\u5165\u5f0f\u7cfb\u7edf\u4e0a\uff0c\u5426\u5219\u4e00\u822c\u6ca1\u6709\u95ee\u9898\u3002 \u5176\u6b21\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u4e86\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\uff0c\u5b83\u7684\u6027\u80fd\u76f8\u8f83\u4e8e\u4f7f\u7528\u66f4\u5bbd\u7684\u7c7b\u578b\u53ef\u80fd\u6216\u7a0d\u5dee\uff08\u5728\u540c\u6837\u7684\u4f53\u7cfb\u7ed3\u6784\u4e0b\uff09\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a\u5927\u5c0f\u786e\u5b9a\u4e3a 32 \u4f4d\u7684\u6574\u578b\uff0c\u4f60\u53ef\u80fd\u4f1a\u4f7f\u7528 std:: int32_t \uff0c\u4f46\u662f\u4f60\u7684 CPU \u53ef\u80fd\u5728\u5904\u7406 64 \u4f4d\u6574\u5f62\u65f6\u66f4\u5feb\u3002\u4e0d\u8fc7\uff0c\u5373\u4fbf CPU \u80fd\u591f\u66f4\u5feb\u7684\u5904\u7406\u67d0\u79cd\u7ed9\u5b9a\u7684\u7c7b\u578b\uff0c\u4e5f\u4e0d\u4e00\u5b9a\u610f\u5473\u7740\u7a0b\u5e8f\u7684\u8fd0\u884c\u901f\u5ea6\u66f4\u5feb\u2014\u2014\u73b0\u4ee3\u7a0b\u5e8f\u66f4\u591a\u53d7\u9650\u4e8e\u5185\u5b58\u4f7f\u7528\u800c\u4e0d\u662f CPU \u5904\u7406\u901f\u5ea6\u3002\u4f7f\u7528\u66f4\u5927\u7684\u5185\u5b58\u8db3\u8ff9(memory footprint)\u5e26\u6765\u7684\u6027\u80fd\u635f\u5931\u53ef\u80fd\u4f1a\u8d85\u8fc7 CPU \u5bf9\u5176\u8fdb\u884c\u7684\u52a0\u901f\u3002\u4e0d\u7ecf\u5b9e\u9645\u6d4b\u8bd5\u662f\u5f88\u96be\u8fdb\u884c\u5bf9\u6bd4\u7684\u3002 \u901f\u5ea6\u4f18\u5148\u6574\u578b\u548c\u5c3a\u5bf8\u4f18\u5148\u6574\u578b \u4e3a\u4e86\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\uff0cC++\u8fd8\u5b9a\u4e49\u4e86\u53e6\u5916\u4e24\u7ec4\u6574\u578b\uff0c\u5e76\u786e\u4fdd\u5b83\u4eec\u603b\u662f\u5177\u6709\u5b9a\u4e49\u3002 \u901f\u5ea6\u4f18\u5148\u7684\u7c7b\u7c7b\u578b( std::int_fast#_t \u548c std::uint_fast#_t ) \u63d0\u4f9b\u4e86\u6700\u5feb\u7684\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u540c\u65f6\u5176\u5bbd\u5ea6\u6700\u5c0f\u4e3a # \u4f4d ( # = 8\u300116\u300132 \u6216 64)\u3002\u4f8b\u5982\uff0c std::int_fast32_t \u53ef\u4ee5\u5b9a\u4e49\u5bbd\u5ea6\u6700\u5c0f\u4e3a 32 \u4f4d\u4e14\u901f\u5ea6\u6700\u5feb\u7684\u6709\u7b26\u53f7\u6574\u578b\u3002 \u5c3a\u5bf8\u4f18\u5148\u7684\u7c7b\u578b( std::int_least#_t \u548c std::uint_least#_t ) \u63d0\u4f9b\u4e86\u6700\u5c0f\u7684\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u5176\u5bbd\u5ea6\u4e3a # \u4f4d ( # = 8\u300116\u300132 \u6216 64)\u3002\u4f8b\u5982\uff0c std::uint_least32_t \u53ef\u4ee5\u5b9a\u4e49\u5bbd\u5ea6\u81f3\u5c11\u4e3a 32 \u4f4d\u7684\u6700\u5c0f\u7684\u65e0\u7b26\u53f7\u6574\u578b\u3002 \u4ee5\u4f5c\u8005\u4f7f\u7528\u7684 Visual Studio (32-bit \u63a7\u5236\u53f0\u7a0b\u5e8f)\u4e3a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // for fixed-width integers #include int main () { std :: cout << \"least 8: \" << sizeof ( std :: int_least8_t ) * 8 << \" bits \\n \" ; std :: cout << \"least 16: \" << sizeof ( std :: int_least16_t ) * 8 << \" bits \\n \" ; std :: cout << \"least 32: \" << sizeof ( std :: int_least32_t ) * 8 << \" bits \\n \" ; std :: cout << '\\n' ; std :: cout << \"fast 8: \" << sizeof ( std :: int_fast8_t ) * 8 << \" bits \\n \" ; std :: cout << \"fast 16: \" << sizeof ( std :: int_fast16_t ) * 8 << \" bits \\n \" ; std :: cout << \"fast 32: \" << sizeof ( std :: int_fast32_t ) * 8 << \" bits \\n \" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 least 8: 8 bits least 16: 16 bits least 32: 32 bits fast 8: 8 bits fast 16: 32 bits fast 32: 32 bits \u53ef\u4ee5\u770b\u51fa std::int_least16_t \u662f 16 \u4f4d\u7684\uff0c\u4f46\u662f std::int_fast16_t \u5b9e\u9645\u4e0a\u5374\u662f 32 \u4f4d\u3002\u8fd9\u662f\u56e0\u4e3a\u5728\u4f5c\u8005\u7684\u673a\u5668\u4e0a\uff0c32 \u4f4d\u6574\u578b\u7684\u5904\u7406\u901f\u5ea6\u8981\u6bd4 16 \u4f4d\u5757\u3002 \u7136\u800c\uff0c\u8fd9\u4e24\u79cd\u7c7b\u578b\u4e5f\u6709\u5176\u7f3a\u70b9\uff1a\u9996\u5148\uff0c\u4f7f\u7528\u5b83\u4eec\u7684\u7a0b\u5e8f\u5458\u5b9e\u9645\u4e0a\u5e76\u4e0d\u591a\uff0c\u4e0d\u719f\u6089\u7684\u60c5\u51b5\u4f7f\u7528\u5b83\u4eec\u4f1a\u9020\u6210\u9519\u8bef\u3002\u53e6\u5916\uff0c\u901f\u5ea6\u4f18\u5148\u7684\u7c7b\u578b\u540c\u6837\u4e5f\u4f1a\u5e26\u6765\u548c 4 \u6bd4\u7279\u5bbd\u6574\u578b\u4e00\u6837\u7684\u5185\u5b58\u6d6a\u8d39\u95ee\u9898\u3002\u66f4\u4e25\u91cd\u7684\u662f\uff0c\u56e0\u4e3a\u8fd9\u4e24\u79cd\u6574\u578b\u7c7b\u578b\u7684\u5927\u5c0f\u662f\u53d8\u5316\u7684\uff0c\u90a3\u4e48\u53ef\u80fd\u4f1a\u9020\u6210\u4f60\u7684\u7a0b\u5e8f\u5728\u4e0d\u540c\u4f53\u7cfb\u7ed3\u6784\u7684\u7535\u8111\u4e0a\u884c\u4e3a\u6709\u6240\u4e0d\u540c\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include // for fixed-width integers #include int main () { std :: uint_fast16_t sometype { 0 }; -- sometype ; // \u6545\u610f\u5229\u7528\u65e0\u7b26\u53f7\u6574\u578b\u53cd\u8f6c\u7684\u7279\u6027 std :: cout << sometype ; return 0 ; } \u4e0a\u8ff0\u4ee3\u7801\u7684\u8f93\u51fa\u7ed3\u679c\u53d6\u51b3\u4e8e std::uint_fast16_t \u662f 16 \u4f4d\u300132 \u4f4d\u8fd8\u662f 64 \u4f4d\u3002 \u9664\u975e\u80fd\u5728\u5bf9\u5e94\u7684\u5e73\u53f0\u4e0a\u8fdb\u884c\u6d4b\u8bd5\uff0c\u5426\u5219\u6211\u4eec\u5f88\u96be\u9884\u77e5\u7a0b\u5e8f\u5728\u4ec0\u4e48\u5e73\u53f0\u4e0a\u8fd0\u884c\u65f6\u4f1a\u51fa\u95ee\u9898\uff0c\u800c\u5bf9\u4e8e\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u6211\u4eec\u80fd\u591f\u4f7f\u7528\u7684\u5e73\u53f0\u79cd\u7c7b\u5e76\u4e0d\u4f1a\u5f88\u591a\u3002 std:: int8_t \u548c std:: uint8_t \u7684\u884c\u4e3a\u53ef\u80fd\u66f4\u50cf\u5b57\u7b26\u800c\u975e\u6574\u578b \u7531\u4e8e\u5bf9C++\u6807\u51c6\u7814\u7a76\u4e0d\u591f\u4ed4\u7ec6\uff0c\u5f88\u591a\u7f16\u8bd1\u5668\u5b9a\u4e49\u548c\u5bf9\u5f85 std::int8_t \u548c std::uint8_t \u7c7b\u578b(\u4ee5\u53ca\u5176\u5bf9\u5e94\u7684\u901f\u5ea6\u6216\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u7c7b\u578b) \u7684\u65b9\u5f0f\u548c signed char \u4e0e unsigned char \u662f\u5b8c\u5168\u4e00\u6837\u7684\u3002\u8fd9\u4e5f\u5c31\u610f\u5473\u77408\u4f4d\u7684\u7c7b\u578b\u53ef\u80fd\uff08\u4e5f\u53ef\u80fd\u4e0d\uff09\u4e0e\u5176\u4ed6\u56fa\u5b9a\u5bbd\u5ea6\u7c7b\u578b\u5177\u6709\u4e0d\u540c\u7684\u884c\u4e3a\uff0c\u8fd9\u6709\u53ef\u80fd\u9020\u6210\u9519\u8bef\u3002\u5177\u4f53\u7684\u884c\u4e3a\u548c\u7cfb\u7edf\u76f8\u5173\uff0c\u6240\u4ee5\u5728\u67d0\u4e2a\u5e73\u53f0\u4e0a\u6b63\u5e38\u5de5\u4f5c\u7684\u7a0b\u5e8f\u5728\u5176\u4ed6\u5e73\u53f0\u4e0a\u53ef\u80fd\u65e0\u6cd5\u5de5\u4f5c\u6216\u7f16\u8bd1\u3002 \u6211\u4eec\u4f1a\u5728 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast \u4e2d\u4e3e\u4f8b\u8bf4\u660e\u3002 \u4e3a\u4e86\u4fdd\u6301\u4e00\u81f4\u6027\uff0c\u6700\u597d\u80fd\u591f\u907f\u514d\u4f7f\u7528 _ std::int8_t \u548c std::uint8_t (\u4ee5\u53ca\u5176\u76f8\u5173\u7684\u901f\u5ea6\u6216\u5927\u5c0f\u4f18\u5148\u7c7b\u578b) \u3002\u53ef\u4ee5\u4f7f\u7528 std::int16_t \u6216 std::uint16_t \u4ee3\u66ff\u3002 \u6ce8\u610f \u907f\u514d\u4f7f\u75288\u4f4d\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u3002\u5982\u679c\u4e00\u5b9a\u8981\u4f7f\u7528\uff0c\u8bf7\u6ce8\u610f\u5b83\u4eec\u7684\u884c\u4e3a\u66f4\u7c7b\u4f3c\u4e8e\u5b57\u7b26\u3002 \u6574\u578b\u7c7b\u578b\u9009\u62e9\u7684\u6700\u4f73\u5b9e\u8df5 \u7531\u4e8e\u5404\u79cd\u57fa\u7840\u6574\u578b\u7c7b\u578b\u90fd\u5b58\u5728\u5404\u81ea\u7684\u4f18\u7f3a\u70b9\uff0c\u5982\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u7c7b\u578b\uff0c\u901f\u5ea6/\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u7c7b\u578b\u4ee5\u53ca\u6709\u7b26\u53f7\u65e0\u7b26\u53f7\u6574\u578b\u3002\u6211\u4eec\u9700\u8981\u5728\u4f7f\u7528\u65f6\u53c2\u8003\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u3002 \u6211\u4eec\u7684\u5b97\u65e8\u662f\uff0c\u6b63\u786e\u6027\u4f18\u4e8e\u901f\u5ea6\uff0c\u7f16\u8bd1\u65f6\u53d1\u73b0\u9519\u8bef\u4f18\u4e8e\u8fd0\u884c\u65f6\u53d1\u73b0\u9519\u8bef\u2014\u2014\u57fa\u4e8e\u8fd9\u4e2a\u6307\u5bfc\u539f\u5219\uff0c\u6211\u4eec\u5efa\u8bae\u907f\u514d\u4f7f\u7528\u901f\u5ea6/\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u7c7b\u578b\uff0c\u4f7f\u7528\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u7c7b\u578b\u3002\u5982\u679c\u5c06\u6765\u53d1\u73b0\u67d0\u4e2a\u5e73\u53f0\u4e0a\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u4e0d\u80fd\u7f16\u8bd1\uff0c\u90a3\u65f6\u4f60\u53ef\u4ee5\u518d\u51b3\u5b9a\u662f\u5426\u8981\u4fee\u6539\u4f60\u7684\u7a0b\u5e8f\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u6574\u578b\u7684\u5927\u5c0f\u4e0d\u91cd\u8981\uff08\u4f8b\u5982\u5b58\u653e\u7684\u6570\u603b\u662f\u57282\u5b57\u8282\u6709\u7b26\u53f7\u6574\u578b\u8303\u56f4\u5185\uff09\uff0c\u63a8\u8350\u4f7f\u7528 int \u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u8981\u6c42\u7528\u6237\u8f93\u5165\u5e74\u9f84\uff0c\u6216\u8005\u8fdb\u884c10\u4ee5\u5185\u7684\u8ba1\u6570\uff08\u6b64\u65f62\u5b57\u8282\u8303\u56f4\u603b\u662f\u591f\u7528\u7684\uff09\u3002\u90a3\u4e48\u4f7f\u7528 int \u5c31\u8db3\u591f\u5e94\u5bf9\u7edd\u5927\u591a\u6570\u573a\u666f\u4e86\uff1b \u5982\u679c\u9700\u8981\u5b58\u50a8\u6570\u91cf\u503c\uff0c\u4e14\u8303\u56f4\u5fc5\u987b\u660e\u786e\u65f6\uff0c\u63a8\u8350\u4f7f\u7528 std::int#_t \uff1b \u5982\u679c\u9700\u8981\u8fdb\u884c\u4f4d\u8fd0\u7b97\u6216\u60f3\u8981\u5229\u7528\u65e0\u7b26\u53f7\u6570\u7684\u7ffb\u8f6c\u7279\u6027\u65f6\uff0c\u4f7f\u7528 std::uint#_t \u3002 \u5c3d\u91cf\u907f\u514d\uff1a \u4f7f\u7528\u65e0\u7b26\u53f7\u7c7b\u578b\u4fdd\u5b58\u6570\u91cf\u503c\uff1b \u4f7f\u7528 8 \u4e3a\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\uff1b \u4f7f\u7528\u901f\u5ea6\u6216\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u7c7b\u578b\uff1b \u4efb\u4f55\u4e0e\u7279\u5b9a\u7f16\u8bd1\u5668\u76f8\u5173\u7684\u56fa\u5b9a\u6574\u578b\u7c7b\u578b\u2014\u2014\u4f8b\u5982 Visual Studio \u5b9a\u4e49\u7684 __int8 \uff0c __int16 \u7b49\u3002 std:: size_t \u662f\u4ec0\u4e48\uff1f \u8003\u8651\u5982\u4e0b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << sizeof ( int ) << '\\n' ; return 0 ; } \u5728\u4f5c\u8005\u7684\u673a\u5668\u4e0a\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 4 \u5f88\u7b80\u5355\u6ca1\u9519\u5427\uff1f\u6211\u4eec\u77e5\u9053 sizeof \u8fd0\u7b97\u7b26\u80af\u5b9a\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\u6570\uff0c\u4f46\u662f\u8fd9\u4e2a\u6574\u578b\u6570\u7a76\u7adf\u662f\u4ec0\u4e48\u7c7b\u578b\u5462\uff1f\u662f int \uff1f\u8fd8\u662f short \uff1f\u3002 \u5b9e\u9645\u4e0a sizeof \uff08\u4ee5\u53ca\u5176\u4ed6\u8fd4\u56de\u957f\u5ea6\u6216\u5927\u5c0f\u7684\u51fd\u6570\uff09\u8fd4\u56de\u7684\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u4e3a std::size_t \u3002 std::size_t \u88ab\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u5b83\u901a\u5e38\u88ab\u7528\u6765\u8868\u793a\u5bf9\u8c61\u7684\u5927\u5c0f\u6216\u957f\u5ea6\u3002 \u6709\u8da3\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 sizeof \u8fd0\u7b97\u7b26\uff08\u8fd4\u56de\u7c7b\u578b\u4e3a std:: size_t \uff09\u8fd4\u56de std:: size_t \u672c\u8eab\u7684\u957f\u5ea6\uff1a 1 2 3 4 5 6 7 8 9 #include // std::size_t #include int main () { std :: cout << sizeof ( std :: size_t ) << '\\n' ; return 0 ; } \u572832\u4f4d\uff084\u5b57\u8282\uff09\u7684\u63a7\u5236\u53f0\u7a0b\u5e8f\u4e2d\uff0c\u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 4 \u548c\u6574\u578b\u7684\u5927\u5c0f\u53ef\u4ee5\u6539\u53d8\u4e00\u6837\uff0c std::size_t \u540c\u6837\u53ef\u80fd\u4f1a\u6539\u53d8\u3002 std::size_t \u53ea\u4fdd\u8bc1\u5b83\u4e3a\u65e0\u7b26\u53f7\uff0c\u5e76\u4e14\u6700\u5c0f\u4e3a16\u4f4d\uff0c\u4f46\u662f\u5728\u5927\u591a\u6570\u7cfb\u7edf\u4e2d\uff0c\u5b83\u7684\u5b9e\u9645\u5927\u5c0f\u901a\u5e38\u7b49\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684[[address-width|\u5730\u5740\u5bbd\u5ea6(address-width)]]\u3002\u5373\u5bf9\u4e8e32\u4f4d\u5e94\u7528\u7a0b\u5e8f\uff0c std::size_t \u901a\u5e38\u4e3a 32 \u4f4d\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u800c\u5bf9\u4e8e 64 \u4f4d\u7a0b\u5e8f\uff0c size_t \u901a\u5e38\u4e3a64\u4f4d\u65e0\u7b26\u53f7\u6574\u578b\u3002 size_t \u88ab\u5b9a\u4e49\u4e3a\u8981\u8db3\u591f\u5927\u80fd\u4ee5\u4fbf\u80fd\u8868\u793a\u5bf9\u5e94\u7cfb\u7edf\u80fd\u591f\u521b\u5efa\u7684\u6700\u5927\u7684\u5bf9\u8c61\uff08\u4ee5\u5b57\u8282\u8ba1\uff09\u7684\u5c3a\u5bf8\u3002\u4f8b\u5982\uff0c\u5982\u679c std::size_t \u662f4\u4e2a\u5b57\u8282\u5bbd\uff0c\u5219\u8be5\u7cfb\u7edf\u80fd\u591f\u521b\u5efa\u7684\u6700\u5927\u7684\u5bf9\u8c61\u4e0d\u53ef\u80fd\u8d85\u8fc7 4,294,967,295 \u5b57\u8282\uff0c\u56e0\u4e3a\u8fd9\u662f4\u5b57\u8282\u65e0\u7b26\u53f7\u6574\u578b\u80fd\u591f\u5b58\u653e\u7684\u6700\u5927\u503c\u3002\u4e0d\u8fc7\uff0c\u8fd9\u53ea\u662f\u5bf9\u8c61\u5c3a\u5bf8\u7684\u7406\u8bba\u4e0a\u9650\uff0c\u5b9e\u9645\u4e0a\u9650\u53d6\u51b3\u4e8e\u4f60\u4f7f\u7528\u7684\u7f16\u8bd1\u5668\u3002 \u4ece\u5b9a\u4e49\u4e0a\u770b\uff0c\u4efb\u4f55\u5c3a\u5bf8\u5927\u4e8e size_t \u80fd\u591f\u8868\u793a\u7684\u503c\u7684\u5bf9\u8c61\uff0c\u90fd\u88ab\u770b\u505a\u662f\u9519\u8bef\u7684\uff08\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\uff09\uff0c\u56e0\u4e3a\u6b64\u65f6 size_t \u5df2\u7ecf\u4e0d\u80fd\u591f\u6b63\u786e\u8868\u793a\u8be5\u5bf9\u8c61\u7684\u5927\u5c0f\uff08\u4f1a\u53d1\u751f\u65e0\u7b26\u53f7\u6570\u7ffb\u8f6c\uff09\u3002 \u9898\u5916\u8bdd \u6709\u4e9b\u7f16\u8bd1\u5668\u5c06\u6700\u5927\u53ef\u521b\u5efa\u5bf9\u8c61\u7684\u5927\u5c0f\u9650\u5236\u4e3a std::size_t \u80fd\u591f\u8868\u793a\u7684\u6700\u5927\u503c(\u6709\u5174\u8da3\u53ef\u4ee5\u9605\u8bfb \u8fd9\u7bc7\u6587\u7ae0 ). \u5b9e\u9645\u4e0a\uff0c\u6700\u5927\u53ef\u521b\u5efa\u5bf9\u8c61\u7684\u5927\u5c0f\u53ef\u80fd\u4f1a\u5c0f\u4e8e\uff08\u8fdc\u5c0f\u4e8e\uff09\u8fd9\u4e2a\u503c\uff0c\u5b83\u53d6\u51b3\u4e8e\u4f60\u7684\u8ba1\u7b97\u673a\u6709\u591a\u5c11\u53ef\u7528\u7684\u8fde\u7eed\u5185\u5b58\u3002","title":"4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t"},{"location":"4-6-Fixed-width-integers-and-size_t/#_1","text":"\u7b80\u800c\u8a00\u4e4b\uff0c\u8fd9\u8fd8\u662f\u4ece C \u8bed\u8a00\u7ee7\u627f\u800c\u6765\u7684\u3002\u5728\u8ba1\u7b97\u673a\u975e\u5e38\u6162\u7684\u5e74\u4ee3\uff0c\u6027\u80fd\u662f\u6700\u9700\u8981\u88ab\u8003\u8651\u7684\u95ee\u9898\u3002C \u8bed\u8a00\u9009\u62e9\u6545\u610f\u4e0d\u53bb\u56fa\u5b9a\u6574\u578b\u7684\u5927\u5c0f\uff0c\u4ee5\u4fbf\u7f16\u8bd1\u5668\u7684\u5b9e\u73b0\u8005\u53ef\u4ee5\u4e3a\u76ee\u6807\u8ba1\u7b97\u673a\u9009\u62e9\u6700\u5408\u9002\u7684\u6574\u578b\u5927\u5c0f\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002","title":"\u4e3a\u4ec0\u4e48\u6574\u578b\u7684\u5927\u5c0f\u4e0d\u662f\u56fa\u5b9a\u7684\uff1f"},{"location":"4-6-Fixed-width-integers-and-size_t/#_2","text":"\u4ece\u73b0\u4ee3\u6807\u51c6\u7684\u89d2\u5ea6\u6765\u8bb2\uff0c\u662f\u7684\u3002\u4f5c\u4e3a\u7a0b\u5e8f\u5458\u8fd8\u8981\u5904\u7406\u7c7b\u578b\u8303\u56f4\u4e0d\u786e\u5b9a\u7684\u60c5\u51b5\u786e\u5b9e\u6709\u70b9\u8352\u5510\u3002 \u8003\u8651 int \u7c7b\u578b\uff0c\u5176\u5360\u7528\u5185\u5b58\u6700\u5c0f\u4e3a 2 \u5b57\u8282\uff0c\u4f46\u662f\u5728\u73b0\u4ee3\u8ba1\u7b97\u673a\u4e0a\u5374\u7ecf\u5e38\u4e3a 4 \u5b57\u8282\u3002\u5982\u679c\u4f60\u5047\u8bbe int \u662f 4 \u4e2a\u5b57\u8282\uff08\u786e\u5b9e 4 \u5b57\u8282\u60c5\u51b5\u6700\u6709\u53ef\u80fd\uff09\uff0c\u90a3\u4e48\u4f60\u7684\u7a0b\u5e8f\u5728 int \u5b9e\u9645\u53ea\u5360\u4e24\u4e2a\u5b57\u8282\u7684\u8ba1\u7b97\u673a\u4e0a\u5c31\u4f1a\u4ea7\u751f\u5f02\u5e38\u7684\u884c\u4e3a\uff08\u56e0\u4e3a\u4f60\u5f88\u53ef\u80fd\u628a\u4e00\u4e2a\u9700\u8981 4 \u5b57\u8282\u624d\u80fd\u5b58\u653e\u7684\u503c\u5b58\u653e\u5230\u4e86\u5b9e\u9645\u53ea\u6709 2 \u4e2a\u5b57\u8282\u7684\u53d8\u91cf\u4e2d\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u6ea2\u51fa\u6216 \u672a\u5b9a\u4e49\u884c\u4e3a \uff09\u3002\u800c\u5982\u679c\u4f60\u5047\u8bbe int \u53ea\u6709\u4e24\u4e2a\u5b57\u8282\u4ee5\u4fbf\u83b7\u5f97\u6700\u597d\u7684\u517c\u5bb9\u6027\uff0c\u90a3\u4e48\u5728 4 \u5b57\u8282\u7cfb\u7edf\u4e0a\u65e0\u7591\u4f1a\u6d6a\u8d39\u4e24\u4e2a\u5b57\u8282\uff0c\u8fd9\u4f1a\u4f7f\u5f97\u4f60\u7684\u5185\u5b58\u4f7f\u7528\u6210\u500d\u589e\u52a0\uff01","title":"\u5f88\u8ba8\u538c\u662f\u5427\uff1f"},{"location":"4-6-Fixed-width-integers-and-size_t/#_3","text":"\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cC99 \u5b9a\u4e49\u4e86\u4e00\u7ec4\u56fa\u5b9a\u5bbd\u5ea6\u6574\u5f62\uff08\u4f4d\u4e8e stdint.h \uff09\u6765\u786e\u4fdd\u6574\u578b\u5728\u4e0d\u540c\u7684\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u4e0b\u90fd\u5177\u6709\u76f8\u540c\u7684\u5927\u5c0f\u3002 \u540d\u79f0 \u7c7b\u578b \u8303\u56f4 \u5907\u6ce8 std::int8_t 1 byte signed -128 \u5230 127 \u5728\u5927\u591a\u6570\u7cfb\u7edf\u4e0a\u88ab\u770b\u505a\u6709\u7b26\u53f7\u5b57\u7b26\u5904\u7406\uff0c\u89c1\u4e0b\u9762\u5907\u6ce8\u3002 std::uint8_t 1 byte unsigned 0 \u5230 255 \u5728\u5927\u591a\u6570\u7cfb\u7edf\u4e0a\u88ab\u770b\u505a\u65e0\u7b26\u53f7\u5b57\u7b26\u5904\u7406\uff0c\u89c1\u4e0b\u9762\u5907\u6ce8 std::int16_t 2 byte signed -32,768 \u5230 32,767 std::uint16_t 2 byte unsigned 0 \u5230 65,535 std::int32_t 4 byte signed -2,147,483,648 \u5230 2,147,483,647 std::uint32_t 4 byte unsigned 0 \u5230 4,294,967,295 std::int64_t 8 byte signed -9,223,372,036,854,775,808 \u5230 9,223,372,036,854,775,807 std::uint64_t 8 byte unsigned 0 \u5230 18,446,744,073,709,551,615 C++ \u5728 C++11 \u4e2d\u5b98\u65b9\u5438\u7eb3\u4e86\u8fd9\u4e9b\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u3002\u901a\u8fc7\u5305\u542b \u5934\u6587\u4ef6\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\uff0c\u5b83\u4eec\u88ab\u5b9a\u4e49\u5728 std \u547d\u540d\u7a7a\u95f4\u4e2d\u3002 \u8bf7\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 #include // for fixed-width integers #include int main () { std :: int16_t i { 5 }; std :: cout << i ; return 0 ; } \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u901a\u5e38\u6765\u8bb2\u6709\u4e24\u4e2a\u7f3a\u9677\u3002 \u9996\u5148\uff0c\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u4e0d\u80fd\u4fdd\u8bc1\u5728\u6240\u6709\u7684\u4f53\u7cfb\u7ed3\u6784\u4e2d\u90fd\u88ab\u5b9a\u4e49\u4e86\u3002\u5b83\u53ea\u5b58\u5728\u4e8e, They only exist on systems where there are fundamental types matching their widths and following a certain binary representation.\u5982\u679c\u4f53\u7cfb\u7ed3\u6784\u4e0d\u652f\u6301\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\uff0c\u5219\u4f60\u7684\u7a0b\u5e8f\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\u3002\u4e0d\u8fc7\uff0c\u5927\u591a\u6570\u7684\u73b0\u4ee3\u4f53\u7cfb\u7ed3\u6784\u90fd\u4ee5 8/16/32/64-\u4f4d\u7684\u53d8\u91cf\u4e3a\u6807\u51c6\uff0c\u56e0\u6b64\u9664\u975e\u4f60\u9700\u8981\u5c06\u7a0b\u5e8f\u79fb\u690d\u5230\u67d0\u4e2a\u6781\u5176\u7279\u6b8a\u7684\u5927\u578b\u673a\u6216\u5d4c\u5165\u5f0f\u7cfb\u7edf\u4e0a\uff0c\u5426\u5219\u4e00\u822c\u6ca1\u6709\u95ee\u9898\u3002 \u5176\u6b21\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u4e86\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\uff0c\u5b83\u7684\u6027\u80fd\u76f8\u8f83\u4e8e\u4f7f\u7528\u66f4\u5bbd\u7684\u7c7b\u578b\u53ef\u80fd\u6216\u7a0d\u5dee\uff08\u5728\u540c\u6837\u7684\u4f53\u7cfb\u7ed3\u6784\u4e0b\uff09\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a\u5927\u5c0f\u786e\u5b9a\u4e3a 32 \u4f4d\u7684\u6574\u578b\uff0c\u4f60\u53ef\u80fd\u4f1a\u4f7f\u7528 std:: int32_t \uff0c\u4f46\u662f\u4f60\u7684 CPU \u53ef\u80fd\u5728\u5904\u7406 64 \u4f4d\u6574\u5f62\u65f6\u66f4\u5feb\u3002\u4e0d\u8fc7\uff0c\u5373\u4fbf CPU \u80fd\u591f\u66f4\u5feb\u7684\u5904\u7406\u67d0\u79cd\u7ed9\u5b9a\u7684\u7c7b\u578b\uff0c\u4e5f\u4e0d\u4e00\u5b9a\u610f\u5473\u7740\u7a0b\u5e8f\u7684\u8fd0\u884c\u901f\u5ea6\u66f4\u5feb\u2014\u2014\u73b0\u4ee3\u7a0b\u5e8f\u66f4\u591a\u53d7\u9650\u4e8e\u5185\u5b58\u4f7f\u7528\u800c\u4e0d\u662f CPU \u5904\u7406\u901f\u5ea6\u3002\u4f7f\u7528\u66f4\u5927\u7684\u5185\u5b58\u8db3\u8ff9(memory footprint)\u5e26\u6765\u7684\u6027\u80fd\u635f\u5931\u53ef\u80fd\u4f1a\u8d85\u8fc7 CPU \u5bf9\u5176\u8fdb\u884c\u7684\u52a0\u901f\u3002\u4e0d\u7ecf\u5b9e\u9645\u6d4b\u8bd5\u662f\u5f88\u96be\u8fdb\u884c\u5bf9\u6bd4\u7684\u3002","title":"\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b"},{"location":"4-6-Fixed-width-integers-and-size_t/#_4","text":"\u4e3a\u4e86\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\uff0cC++\u8fd8\u5b9a\u4e49\u4e86\u53e6\u5916\u4e24\u7ec4\u6574\u578b\uff0c\u5e76\u786e\u4fdd\u5b83\u4eec\u603b\u662f\u5177\u6709\u5b9a\u4e49\u3002 \u901f\u5ea6\u4f18\u5148\u7684\u7c7b\u7c7b\u578b( std::int_fast#_t \u548c std::uint_fast#_t ) \u63d0\u4f9b\u4e86\u6700\u5feb\u7684\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u540c\u65f6\u5176\u5bbd\u5ea6\u6700\u5c0f\u4e3a # \u4f4d ( # = 8\u300116\u300132 \u6216 64)\u3002\u4f8b\u5982\uff0c std::int_fast32_t \u53ef\u4ee5\u5b9a\u4e49\u5bbd\u5ea6\u6700\u5c0f\u4e3a 32 \u4f4d\u4e14\u901f\u5ea6\u6700\u5feb\u7684\u6709\u7b26\u53f7\u6574\u578b\u3002 \u5c3a\u5bf8\u4f18\u5148\u7684\u7c7b\u578b( std::int_least#_t \u548c std::uint_least#_t ) \u63d0\u4f9b\u4e86\u6700\u5c0f\u7684\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u5176\u5bbd\u5ea6\u4e3a # \u4f4d ( # = 8\u300116\u300132 \u6216 64)\u3002\u4f8b\u5982\uff0c std::uint_least32_t \u53ef\u4ee5\u5b9a\u4e49\u5bbd\u5ea6\u81f3\u5c11\u4e3a 32 \u4f4d\u7684\u6700\u5c0f\u7684\u65e0\u7b26\u53f7\u6574\u578b\u3002 \u4ee5\u4f5c\u8005\u4f7f\u7528\u7684 Visual Studio (32-bit \u63a7\u5236\u53f0\u7a0b\u5e8f)\u4e3a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // for fixed-width integers #include int main () { std :: cout << \"least 8: \" << sizeof ( std :: int_least8_t ) * 8 << \" bits \\n \" ; std :: cout << \"least 16: \" << sizeof ( std :: int_least16_t ) * 8 << \" bits \\n \" ; std :: cout << \"least 32: \" << sizeof ( std :: int_least32_t ) * 8 << \" bits \\n \" ; std :: cout << '\\n' ; std :: cout << \"fast 8: \" << sizeof ( std :: int_fast8_t ) * 8 << \" bits \\n \" ; std :: cout << \"fast 16: \" << sizeof ( std :: int_fast16_t ) * 8 << \" bits \\n \" ; std :: cout << \"fast 32: \" << sizeof ( std :: int_fast32_t ) * 8 << \" bits \\n \" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 least 8: 8 bits least 16: 16 bits least 32: 32 bits fast 8: 8 bits fast 16: 32 bits fast 32: 32 bits \u53ef\u4ee5\u770b\u51fa std::int_least16_t \u662f 16 \u4f4d\u7684\uff0c\u4f46\u662f std::int_fast16_t \u5b9e\u9645\u4e0a\u5374\u662f 32 \u4f4d\u3002\u8fd9\u662f\u56e0\u4e3a\u5728\u4f5c\u8005\u7684\u673a\u5668\u4e0a\uff0c32 \u4f4d\u6574\u578b\u7684\u5904\u7406\u901f\u5ea6\u8981\u6bd4 16 \u4f4d\u5757\u3002 \u7136\u800c\uff0c\u8fd9\u4e24\u79cd\u7c7b\u578b\u4e5f\u6709\u5176\u7f3a\u70b9\uff1a\u9996\u5148\uff0c\u4f7f\u7528\u5b83\u4eec\u7684\u7a0b\u5e8f\u5458\u5b9e\u9645\u4e0a\u5e76\u4e0d\u591a\uff0c\u4e0d\u719f\u6089\u7684\u60c5\u51b5\u4f7f\u7528\u5b83\u4eec\u4f1a\u9020\u6210\u9519\u8bef\u3002\u53e6\u5916\uff0c\u901f\u5ea6\u4f18\u5148\u7684\u7c7b\u578b\u540c\u6837\u4e5f\u4f1a\u5e26\u6765\u548c 4 \u6bd4\u7279\u5bbd\u6574\u578b\u4e00\u6837\u7684\u5185\u5b58\u6d6a\u8d39\u95ee\u9898\u3002\u66f4\u4e25\u91cd\u7684\u662f\uff0c\u56e0\u4e3a\u8fd9\u4e24\u79cd\u6574\u578b\u7c7b\u578b\u7684\u5927\u5c0f\u662f\u53d8\u5316\u7684\uff0c\u90a3\u4e48\u53ef\u80fd\u4f1a\u9020\u6210\u4f60\u7684\u7a0b\u5e8f\u5728\u4e0d\u540c\u4f53\u7cfb\u7ed3\u6784\u7684\u7535\u8111\u4e0a\u884c\u4e3a\u6709\u6240\u4e0d\u540c\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include // for fixed-width integers #include int main () { std :: uint_fast16_t sometype { 0 }; -- sometype ; // \u6545\u610f\u5229\u7528\u65e0\u7b26\u53f7\u6574\u578b\u53cd\u8f6c\u7684\u7279\u6027 std :: cout << sometype ; return 0 ; } \u4e0a\u8ff0\u4ee3\u7801\u7684\u8f93\u51fa\u7ed3\u679c\u53d6\u51b3\u4e8e std::uint_fast16_t \u662f 16 \u4f4d\u300132 \u4f4d\u8fd8\u662f 64 \u4f4d\u3002 \u9664\u975e\u80fd\u5728\u5bf9\u5e94\u7684\u5e73\u53f0\u4e0a\u8fdb\u884c\u6d4b\u8bd5\uff0c\u5426\u5219\u6211\u4eec\u5f88\u96be\u9884\u77e5\u7a0b\u5e8f\u5728\u4ec0\u4e48\u5e73\u53f0\u4e0a\u8fd0\u884c\u65f6\u4f1a\u51fa\u95ee\u9898\uff0c\u800c\u5bf9\u4e8e\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u6211\u4eec\u80fd\u591f\u4f7f\u7528\u7684\u5e73\u53f0\u79cd\u7c7b\u5e76\u4e0d\u4f1a\u5f88\u591a\u3002","title":"\u901f\u5ea6\u4f18\u5148\u6574\u578b\u548c\u5c3a\u5bf8\u4f18\u5148\u6574\u578b"},{"location":"4-6-Fixed-width-integers-and-size_t/#std-int8_t-std-uint8_t","text":"\u7531\u4e8e\u5bf9C++\u6807\u51c6\u7814\u7a76\u4e0d\u591f\u4ed4\u7ec6\uff0c\u5f88\u591a\u7f16\u8bd1\u5668\u5b9a\u4e49\u548c\u5bf9\u5f85 std::int8_t \u548c std::uint8_t \u7c7b\u578b(\u4ee5\u53ca\u5176\u5bf9\u5e94\u7684\u901f\u5ea6\u6216\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u7c7b\u578b) \u7684\u65b9\u5f0f\u548c signed char \u4e0e unsigned char \u662f\u5b8c\u5168\u4e00\u6837\u7684\u3002\u8fd9\u4e5f\u5c31\u610f\u5473\u77408\u4f4d\u7684\u7c7b\u578b\u53ef\u80fd\uff08\u4e5f\u53ef\u80fd\u4e0d\uff09\u4e0e\u5176\u4ed6\u56fa\u5b9a\u5bbd\u5ea6\u7c7b\u578b\u5177\u6709\u4e0d\u540c\u7684\u884c\u4e3a\uff0c\u8fd9\u6709\u53ef\u80fd\u9020\u6210\u9519\u8bef\u3002\u5177\u4f53\u7684\u884c\u4e3a\u548c\u7cfb\u7edf\u76f8\u5173\uff0c\u6240\u4ee5\u5728\u67d0\u4e2a\u5e73\u53f0\u4e0a\u6b63\u5e38\u5de5\u4f5c\u7684\u7a0b\u5e8f\u5728\u5176\u4ed6\u5e73\u53f0\u4e0a\u53ef\u80fd\u65e0\u6cd5\u5de5\u4f5c\u6216\u7f16\u8bd1\u3002 \u6211\u4eec\u4f1a\u5728 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast \u4e2d\u4e3e\u4f8b\u8bf4\u660e\u3002 \u4e3a\u4e86\u4fdd\u6301\u4e00\u81f4\u6027\uff0c\u6700\u597d\u80fd\u591f\u907f\u514d\u4f7f\u7528 _ std::int8_t \u548c std::uint8_t (\u4ee5\u53ca\u5176\u76f8\u5173\u7684\u901f\u5ea6\u6216\u5927\u5c0f\u4f18\u5148\u7c7b\u578b) \u3002\u53ef\u4ee5\u4f7f\u7528 std::int16_t \u6216 std::uint16_t \u4ee3\u66ff\u3002 \u6ce8\u610f \u907f\u514d\u4f7f\u75288\u4f4d\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u3002\u5982\u679c\u4e00\u5b9a\u8981\u4f7f\u7528\uff0c\u8bf7\u6ce8\u610f\u5b83\u4eec\u7684\u884c\u4e3a\u66f4\u7c7b\u4f3c\u4e8e\u5b57\u7b26\u3002","title":"std:: int8_t \u548c std:: uint8_t \u7684\u884c\u4e3a\u53ef\u80fd\u66f4\u50cf\u5b57\u7b26\u800c\u975e\u6574\u578b"},{"location":"4-6-Fixed-width-integers-and-size_t/#_5","text":"\u7531\u4e8e\u5404\u79cd\u57fa\u7840\u6574\u578b\u7c7b\u578b\u90fd\u5b58\u5728\u5404\u81ea\u7684\u4f18\u7f3a\u70b9\uff0c\u5982\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u7c7b\u578b\uff0c\u901f\u5ea6/\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u7c7b\u578b\u4ee5\u53ca\u6709\u7b26\u53f7\u65e0\u7b26\u53f7\u6574\u578b\u3002\u6211\u4eec\u9700\u8981\u5728\u4f7f\u7528\u65f6\u53c2\u8003\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u3002 \u6211\u4eec\u7684\u5b97\u65e8\u662f\uff0c\u6b63\u786e\u6027\u4f18\u4e8e\u901f\u5ea6\uff0c\u7f16\u8bd1\u65f6\u53d1\u73b0\u9519\u8bef\u4f18\u4e8e\u8fd0\u884c\u65f6\u53d1\u73b0\u9519\u8bef\u2014\u2014\u57fa\u4e8e\u8fd9\u4e2a\u6307\u5bfc\u539f\u5219\uff0c\u6211\u4eec\u5efa\u8bae\u907f\u514d\u4f7f\u7528\u901f\u5ea6/\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u7c7b\u578b\uff0c\u4f7f\u7528\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u7c7b\u578b\u3002\u5982\u679c\u5c06\u6765\u53d1\u73b0\u67d0\u4e2a\u5e73\u53f0\u4e0a\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u4e0d\u80fd\u7f16\u8bd1\uff0c\u90a3\u65f6\u4f60\u53ef\u4ee5\u518d\u51b3\u5b9a\u662f\u5426\u8981\u4fee\u6539\u4f60\u7684\u7a0b\u5e8f\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u6574\u578b\u7684\u5927\u5c0f\u4e0d\u91cd\u8981\uff08\u4f8b\u5982\u5b58\u653e\u7684\u6570\u603b\u662f\u57282\u5b57\u8282\u6709\u7b26\u53f7\u6574\u578b\u8303\u56f4\u5185\uff09\uff0c\u63a8\u8350\u4f7f\u7528 int \u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u8981\u6c42\u7528\u6237\u8f93\u5165\u5e74\u9f84\uff0c\u6216\u8005\u8fdb\u884c10\u4ee5\u5185\u7684\u8ba1\u6570\uff08\u6b64\u65f62\u5b57\u8282\u8303\u56f4\u603b\u662f\u591f\u7528\u7684\uff09\u3002\u90a3\u4e48\u4f7f\u7528 int \u5c31\u8db3\u591f\u5e94\u5bf9\u7edd\u5927\u591a\u6570\u573a\u666f\u4e86\uff1b \u5982\u679c\u9700\u8981\u5b58\u50a8\u6570\u91cf\u503c\uff0c\u4e14\u8303\u56f4\u5fc5\u987b\u660e\u786e\u65f6\uff0c\u63a8\u8350\u4f7f\u7528 std::int#_t \uff1b \u5982\u679c\u9700\u8981\u8fdb\u884c\u4f4d\u8fd0\u7b97\u6216\u60f3\u8981\u5229\u7528\u65e0\u7b26\u53f7\u6570\u7684\u7ffb\u8f6c\u7279\u6027\u65f6\uff0c\u4f7f\u7528 std::uint#_t \u3002 \u5c3d\u91cf\u907f\u514d\uff1a \u4f7f\u7528\u65e0\u7b26\u53f7\u7c7b\u578b\u4fdd\u5b58\u6570\u91cf\u503c\uff1b \u4f7f\u7528 8 \u4e3a\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\uff1b \u4f7f\u7528\u901f\u5ea6\u6216\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u7c7b\u578b\uff1b \u4efb\u4f55\u4e0e\u7279\u5b9a\u7f16\u8bd1\u5668\u76f8\u5173\u7684\u56fa\u5b9a\u6574\u578b\u7c7b\u578b\u2014\u2014\u4f8b\u5982 Visual Studio \u5b9a\u4e49\u7684 __int8 \uff0c __int16 \u7b49\u3002","title":"\u6574\u578b\u7c7b\u578b\u9009\u62e9\u7684\u6700\u4f73\u5b9e\u8df5"},{"location":"4-6-Fixed-width-integers-and-size_t/#std-size_t","text":"\u8003\u8651\u5982\u4e0b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << sizeof ( int ) << '\\n' ; return 0 ; } \u5728\u4f5c\u8005\u7684\u673a\u5668\u4e0a\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 4 \u5f88\u7b80\u5355\u6ca1\u9519\u5427\uff1f\u6211\u4eec\u77e5\u9053 sizeof \u8fd0\u7b97\u7b26\u80af\u5b9a\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\u6570\uff0c\u4f46\u662f\u8fd9\u4e2a\u6574\u578b\u6570\u7a76\u7adf\u662f\u4ec0\u4e48\u7c7b\u578b\u5462\uff1f\u662f int \uff1f\u8fd8\u662f short \uff1f\u3002 \u5b9e\u9645\u4e0a sizeof \uff08\u4ee5\u53ca\u5176\u4ed6\u8fd4\u56de\u957f\u5ea6\u6216\u5927\u5c0f\u7684\u51fd\u6570\uff09\u8fd4\u56de\u7684\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u4e3a std::size_t \u3002 std::size_t \u88ab\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u5b83\u901a\u5e38\u88ab\u7528\u6765\u8868\u793a\u5bf9\u8c61\u7684\u5927\u5c0f\u6216\u957f\u5ea6\u3002 \u6709\u8da3\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 sizeof \u8fd0\u7b97\u7b26\uff08\u8fd4\u56de\u7c7b\u578b\u4e3a std:: size_t \uff09\u8fd4\u56de std:: size_t \u672c\u8eab\u7684\u957f\u5ea6\uff1a 1 2 3 4 5 6 7 8 9 #include // std::size_t #include int main () { std :: cout << sizeof ( std :: size_t ) << '\\n' ; return 0 ; } \u572832\u4f4d\uff084\u5b57\u8282\uff09\u7684\u63a7\u5236\u53f0\u7a0b\u5e8f\u4e2d\uff0c\u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 4 \u548c\u6574\u578b\u7684\u5927\u5c0f\u53ef\u4ee5\u6539\u53d8\u4e00\u6837\uff0c std::size_t \u540c\u6837\u53ef\u80fd\u4f1a\u6539\u53d8\u3002 std::size_t \u53ea\u4fdd\u8bc1\u5b83\u4e3a\u65e0\u7b26\u53f7\uff0c\u5e76\u4e14\u6700\u5c0f\u4e3a16\u4f4d\uff0c\u4f46\u662f\u5728\u5927\u591a\u6570\u7cfb\u7edf\u4e2d\uff0c\u5b83\u7684\u5b9e\u9645\u5927\u5c0f\u901a\u5e38\u7b49\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684[[address-width|\u5730\u5740\u5bbd\u5ea6(address-width)]]\u3002\u5373\u5bf9\u4e8e32\u4f4d\u5e94\u7528\u7a0b\u5e8f\uff0c std::size_t \u901a\u5e38\u4e3a 32 \u4f4d\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u800c\u5bf9\u4e8e 64 \u4f4d\u7a0b\u5e8f\uff0c size_t \u901a\u5e38\u4e3a64\u4f4d\u65e0\u7b26\u53f7\u6574\u578b\u3002 size_t \u88ab\u5b9a\u4e49\u4e3a\u8981\u8db3\u591f\u5927\u80fd\u4ee5\u4fbf\u80fd\u8868\u793a\u5bf9\u5e94\u7cfb\u7edf\u80fd\u591f\u521b\u5efa\u7684\u6700\u5927\u7684\u5bf9\u8c61\uff08\u4ee5\u5b57\u8282\u8ba1\uff09\u7684\u5c3a\u5bf8\u3002\u4f8b\u5982\uff0c\u5982\u679c std::size_t \u662f4\u4e2a\u5b57\u8282\u5bbd\uff0c\u5219\u8be5\u7cfb\u7edf\u80fd\u591f\u521b\u5efa\u7684\u6700\u5927\u7684\u5bf9\u8c61\u4e0d\u53ef\u80fd\u8d85\u8fc7 4,294,967,295 \u5b57\u8282\uff0c\u56e0\u4e3a\u8fd9\u662f4\u5b57\u8282\u65e0\u7b26\u53f7\u6574\u578b\u80fd\u591f\u5b58\u653e\u7684\u6700\u5927\u503c\u3002\u4e0d\u8fc7\uff0c\u8fd9\u53ea\u662f\u5bf9\u8c61\u5c3a\u5bf8\u7684\u7406\u8bba\u4e0a\u9650\uff0c\u5b9e\u9645\u4e0a\u9650\u53d6\u51b3\u4e8e\u4f60\u4f7f\u7528\u7684\u7f16\u8bd1\u5668\u3002 \u4ece\u5b9a\u4e49\u4e0a\u770b\uff0c\u4efb\u4f55\u5c3a\u5bf8\u5927\u4e8e size_t \u80fd\u591f\u8868\u793a\u7684\u503c\u7684\u5bf9\u8c61\uff0c\u90fd\u88ab\u770b\u505a\u662f\u9519\u8bef\u7684\uff08\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\uff09\uff0c\u56e0\u4e3a\u6b64\u65f6 size_t \u5df2\u7ecf\u4e0d\u80fd\u591f\u6b63\u786e\u8868\u793a\u8be5\u5bf9\u8c61\u7684\u5927\u5c0f\uff08\u4f1a\u53d1\u751f\u65e0\u7b26\u53f7\u6570\u7ffb\u8f6c\uff09\u3002 \u9898\u5916\u8bdd \u6709\u4e9b\u7f16\u8bd1\u5668\u5c06\u6700\u5927\u53ef\u521b\u5efa\u5bf9\u8c61\u7684\u5927\u5c0f\u9650\u5236\u4e3a std::size_t \u80fd\u591f\u8868\u793a\u7684\u6700\u5927\u503c(\u6709\u5174\u8da3\u53ef\u4ee5\u9605\u8bfb \u8fd9\u7bc7\u6587\u7ae0 ). \u5b9e\u9645\u4e0a\uff0c\u6700\u5927\u53ef\u521b\u5efa\u5bf9\u8c61\u7684\u5927\u5c0f\u53ef\u80fd\u4f1a\u5c0f\u4e8e\uff08\u8fdc\u5c0f\u4e8e\uff09\u8fd9\u4e2a\u503c\uff0c\u5b83\u53d6\u51b3\u4e8e\u4f60\u7684\u8ba1\u7b97\u673a\u6709\u591a\u5c11\u53ef\u7528\u7684\u8fde\u7eed\u5185\u5b58\u3002","title":"std:: size_t \u662f\u4ec0\u4e48\uff1f"},{"location":"4-7-Introduction-to-scientific-notation/","tags":["data type","scientific notation"],"text":"\u5728\u8c08\u8bba\u4e0b\u4e00\u4e2a\u8bdd\u9898\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u6765\u8ba8\u8bba\u4e00\u4e0b\u79d1\u5b66\u8ba1\u6570\u6cd5\u3002 \u79d1\u5b66\u8ba1\u6570\u6cd5 \u662f\u4e00\u79cd\u5c06\u5927\u6570\u8868\u793a\u4e3a\u7cbe\u7b80\u683c\u5f0f\u7684\u975e\u5e38\u6709\u7528\u7684\u65b9\u6cd5\u3002\u5c3d\u7ba1\u79d1\u5b66\u8ba1\u6570\u6cd5\u7b2c\u4e00\u773c\u770b\u4e0a\u53bb\u4f1a\u6709\u4e9b\u964c\u751f\uff0c\u7406\u89e3\u79d1\u5b66\u8ba1\u6570\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u6d6e\u70b9\u6570\u7684\u5de5\u4f5c\u539f\u7406\u53ca\u5176\u9650\u5236\u3002 \u79d1\u5b66\u8ba1\u6570\u6cd5\u7684\u8868\u793a\u5f62\u5f0f\u4e3a\uff1a \\(\u6709\u6548\u6570\u5b57 * 10^{\u6307\u6570}\\) \u3002\u4f8b\u5982 1.2 x 10\u2074 \u4e2d\uff0c 1.2 \u662f\u6709\u6548\u6570\u5b57\u800c4\u662f\u6307\u6570\u3002\u56e0\u4e3a10\u7684\u56db\u6b21\u65b9\u7b49\u4e8e10,000\uff0c\u6240\u4ee5 1.2 x 10\u2074 \u7b49\u4e8e12,000\u3002 \u6309\u7167\u60ef\u4f8b\uff0c\u79d1\u5b66\u8ba1\u6570\u6cd5\u7684\u6709\u6548\u6570\u5b57\u90e8\u5206\uff0c\u5c0f\u6570\u70b9\u524d\u5199\u4e00\u4e2a\u975e\u96f6\u6570\u5b57\uff0c\u5176\u4ed6\u975e\u96f6\u6570\u5b57\u5219\u5199\u5230\u5c0f\u6570\u70b9\u540e\u3002 \u4f8b\u5982\uff0c\u4ee5\u5341\u8fdb\u5236\u8868\u793a\u6cd5\u8868\u793a\u5730\u7403\u7684\u8d28\u91cf\u65f6\uff0c\u6211\u4eec\u4f1a\u5199\u6210 5973600000000000000000000 kg \u3002\u597d\u957f\u4e00\u4e32\u6570\u5b57\uff01\uff08\u800c\u4e14\u5927\u5230\u4e0d\u80fd\u88ab8\u5b57\u8282\u6574\u578b\u6240\u8868\u793a\uff09\u3002\u540c\u65f6\uff0c\u8bfb\u8d77\u6765\u4e5f\u5f88\u56f0\u96be\uff08\u5230\u5e95\u662f19\u4e2a0\u8fd8\u662f20\u4e2a0\uff1f\uff09\u3002\u5373\u4fbf\u4ee5\u9017\u53f7\u6765\u5206\u5272\u6210 5,973,600,000,000,000,000,000,000\uff0c\u770b\u4e0a\u53bb\u4ecd\u7136\u975e\u5e38\u5403\u529b\u3002 \u5982\u679c\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a\uff0c\u5219\u53ef\u4ee5\u5199\u4f5c 5.9736 x 10\u00b2\u2074 kg \uff0c\u770b\u4e0a\u53bb\u5c31\u975e\u5e38\u7b80\u6d01\u4e86\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u79d1\u5b66\u8ba1\u6570\u6cd5\u8fd8\u6709\u5229\u4e8e\u5bf9\u4e24\u4e2a\u975e\u5e38\u5927\u6216\u975e\u5e38\u5c0f\u7684\u503c\u6bd4\u8f83\u5176\u6570\u91cf\u7ea7\uff0c\u56e0\u4e3a\u53ea\u9700\u8981\u6bd4\u8f83\u6307\u6570\u90e8\u5206\u5373\u53ef\u3002 \u56e0\u4e3a\u5728C++\u4e2d\uff0c\u8f93\u5165\u548c\u663e\u793a\u6307\u6570\u8bb0\u53f7\u4f1a\u6bd4\u8f83\u56f0\u96be\uff0c\u6240\u4ee5\u6211\u4eec\u4f7f\u7528\u5b57\u6bcd e \uff08\u6709\u65f6\u4e5f\u4f7f\u7528\u5927\u5199\u5b57\u6bcd E \uff09\u6765\u8868\u793a10\u7684\u6b21\u65b9\u3002\u4f8b\u5982 1.2 x 10\u2074 \u4f1a\u5199\u4f5c 1.2e4 \uff0c\u800c 5.9736 x 10\u00b2\u2074 \u5219\u5199\u4f5c 5.9736e24 \u3002 \u5bf9\u4e8e\u5c0f\u4e8e1\u7684\u6570\uff0c\u6307\u6570\u90e8\u5206\u5219\u662f\u8d1f\u6570\uff0c\u4f8b\u5982 5e-2 \u7b49\u4ef7\u4e8e 5 * 10\u207b\u00b2 \uff0c\u5373 5 / 10\u00b2 \u6216 0.05 \u3002\u7535\u5b50\u7684\u8d28\u91cf\u53ef\u4ee5\u8868\u793a\u4e3a 9.1093822e-31 kg \u3002 \u5982\u4f55\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u79d1\u5b66\u8ba1\u6570\u6cd5 \u4f7f\u7528\u4e0b\u9762\u6b65\u9aa4\uff1a \u6307\u6570\u90e8\u5206\u4ece0\u5f00\u59cb\u3002 \u79fb\u52a8\u5c0f\u6570\u70b9\uff0c\u4f7f\u5176\u5de6\u8fb9\u53ea\u6709\u4e00\u4f4d\u6570\u3002 \u6bcf\u6b21\u5411\u5de6\u79fb\u52a8\uff0c\u6307\u6570+1 \u6bcf\u6b21\u5411\u53f3\u79fb\u52a8\uff0c\u6307\u6570-1 \u5220\u9664\u6709\u6548\u6570\u5b57\u5de6\u8fb9\u76840 \u5982\u679c\u539f\u6570\u5b57\u6ca1\u6709\u5c0f\u6570\u90e8\u5206\uff0c\u5220\u9664\u6709\u6548\u6570\u5b57\u672b\u5c3e\u76840\uff0c\u5982\u679c\u6ca1\u6709\u7279\u522b\u6307\u660e\uff0c\u6211\u4eec\u8ba4\u4e3a\u8fd9\u4e9b0\u6ca1\u6709\u610f\u4e49\u3002 \u4e00\u4e9b\u4f8b\u5b50\uff1a 1 2 3 4 \u8d77\u59cb\u503c: 42030 \u5c0f\u6570\u70b9\u5de6\u79fb4\u4f4d\uff1a4.2030e4 \u6709\u6548\u6570\u5b57\u5de6\u4fa7\u6ca1\u6709\u9700\u8981\u5220\u9664\u76840\uff1a4.2030e4 \u5220\u9664\u6709\u6548\u6570\u5b57\u672b\u5c3e\u76840\uff1a4.203e4 (4 \u4f4d\u6709\u6548\u6570\u5b57) 1 2 3 4 \u8d77\u59cb\u503c: 0.0078900 \u5c0f\u6570\u70b9\u53f3\u79fb3\u4f4d: 0007.8900e-3 \u5220\u9664\u6709\u6548\u6570\u5b57\u5de6\u4fa7\u76840: 7.8900e-3 \u4e0d\u9700\u8981\u5220\u9664\u6709\u6548\u6570\u5b57\u672b\u5c3e\u76840: 7.8900e-3 (5 \u4f4d\u6709\u6548\u6570\u5b57) 1 2 3 4 \u8d77\u59cb\u503c: 600.410 \u5c0f\u6570\u70b9\u5de6\u79fb2\u4f4d: 6.00410e2 \u6709\u6548\u6570\u5b57\u5de6\u4fa7\u6ca1\u6709\u9700\u8981\u5220\u9664\u76840: 6.00410e2 \u4e0d\u9700\u8981\u5220\u9664\u6709\u6548\u6570\u5b57\u672b\u5c3e\u76840: 6.00410e2 (6 \u4f4d\u6709\u6548\u6570\u5b57) \u91cd\u70b9\u7406\u89e3\uff1a\u6709\u6548\u6570\u5b57\u90e8\u5206\u7684\u4f4d\u6570 (\u2018e\u2019\u524d\u9762\u7684\u90e8\u5206) \u79f0\u4e3a\u6709\u6548\u6570\u5b57 \u3002\u6709\u6548\u6570\u5b57\u51b3\u5b9a\u4e86\u8fd9\u4e2a\u6570\u7684 \u7cbe\u5ea6 \uff0c\u6709\u6548\u6570\u5b57\u4f4d\u6570\u8d8a\u591a\u8bf4\u660e\u7cbe\u5ea6\u8d8a\u9ad8\u3002 \u7cbe\u5ea6\u4ee5\u53ca\u5c0f\u6570\u70b9\u540e\u76840 \u60f3\u8c61\u4e00\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u5c31\u540c\u4e00\u4e2a\u82f9\u679c\u7684\u91cd\u91cf\u8be2\u95ee\u4e24\u4e2a\u5b9e\u9a8c\u5ba4\u52a9\u624b\uff0c\u4e00\u4e2a\u56de\u7b54\u82f9\u679c\u91cd\u4e3a87\u514b\uff0c\u53e6\u4e00\u4e2a\u56de\u7b54\u4e3a87.00\u514b\uff0c\u5047\u8bbe\u5b83\u4eec\u7684\u7ed3\u679c\u90fd\u662f\u5bf9\u7684\uff0c\u5bf9\u4e8e\u524d\u4e00\u4e2a\u56de\u7b54\u6765\u8bf4\uff0c\u82f9\u679c\u7684\u771f\u5b9e\u91cd\u91cf\u53ef\u80fd\u4f4d\u4e8e86.50 \u548c 87.49 \u514b\u4e4b\u95f4\u3002\u4e5f\u8bb8\u4ed6\u4f7f\u7528\u7684\u5929\u5e73\u7cbe\u5ea6\u53ea\u5230\u514b\uff0c\u4e5f\u8bb8\u662f\u4ed6\u8fdb\u884c\u4e86\u56db\u820d\u4e94\u5165\u3002\u800c\u5bf9\u4e8e\u540e\u4e00\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u5176\u7cbe\u5ea6\u66f4\u52a0\u81ea\u4fe1(\u91cd\u91cf\u5728 86.9950 \u548c 87.0049 \u514b\u4e4b\u95f4\uff0c\u53d8\u5316\u533a\u95f4\u66f4\u5c0f\u4e86)\u3002 \u6240\u4ee5\uff0c\u5728\u6807\u51c6\u7684\u79d1\u5b66\u8ba1\u6570\u6cd5\u4e2d\uff0c\u6211\u4eec\u503e\u5411\u4e8e\u4fdd\u7559\u5c0f\u6570\u70b9\u540e\u9762\u5c3e\u90e8\u76840\uff0c\u56e0\u4e3a\u8fd9\u4e9b0\u53ef\u4ee5\u6709\u6548\u5730\u8868\u660e\u8be5\u6570\u503c\u7684\u7cbe\u5ea6\u3002 \u4e0d\u8fc7\uff0c\u5728C++\u4e2d\uff0c87 \u548c 87.000 \u5e76\u6ca1\u6709\u4efb\u4f55\u533a\u522b\uff0c\u7f16\u8bd1\u5668\u5b58\u50a8\u4e24\u6570\u7684\u7ed3\u679c\u4e5f\u662f\u5b8c\u5168\u4e00\u6837\u7684\u3002\u4ece\u6280\u672f\u4e0a\u8bb2\uff0c\u4f7f\u7528\u4e24\u79cd\u5199\u6cd5\u6ca1\u6709\u4efb\u4f55\u533a\u522b\uff08\u5f53\u7136\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u4ee3\u7801\u5373\u6587\u6863\u7684\u8868\u793a\u65b9\u5f0f\uff0c\u4e5f\u8bb8\u4f1a\u6709\u51fa\u4e8e\u79d1\u5b66\u4e0a\u7684\u7406\u7531\uff09\u3002 \u4ecb\u7ecd\u5b8c\u79d1\u5b66\u8ba1\u6570\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb\u4ecb\u7ecd\u6d6e\u70b9\u6570\u4e86\u3002","title":"4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5"},{"location":"4-7-Introduction-to-scientific-notation/#_1","text":"\u4f7f\u7528\u4e0b\u9762\u6b65\u9aa4\uff1a \u6307\u6570\u90e8\u5206\u4ece0\u5f00\u59cb\u3002 \u79fb\u52a8\u5c0f\u6570\u70b9\uff0c\u4f7f\u5176\u5de6\u8fb9\u53ea\u6709\u4e00\u4f4d\u6570\u3002 \u6bcf\u6b21\u5411\u5de6\u79fb\u52a8\uff0c\u6307\u6570+1 \u6bcf\u6b21\u5411\u53f3\u79fb\u52a8\uff0c\u6307\u6570-1 \u5220\u9664\u6709\u6548\u6570\u5b57\u5de6\u8fb9\u76840 \u5982\u679c\u539f\u6570\u5b57\u6ca1\u6709\u5c0f\u6570\u90e8\u5206\uff0c\u5220\u9664\u6709\u6548\u6570\u5b57\u672b\u5c3e\u76840\uff0c\u5982\u679c\u6ca1\u6709\u7279\u522b\u6307\u660e\uff0c\u6211\u4eec\u8ba4\u4e3a\u8fd9\u4e9b0\u6ca1\u6709\u610f\u4e49\u3002 \u4e00\u4e9b\u4f8b\u5b50\uff1a 1 2 3 4 \u8d77\u59cb\u503c: 42030 \u5c0f\u6570\u70b9\u5de6\u79fb4\u4f4d\uff1a4.2030e4 \u6709\u6548\u6570\u5b57\u5de6\u4fa7\u6ca1\u6709\u9700\u8981\u5220\u9664\u76840\uff1a4.2030e4 \u5220\u9664\u6709\u6548\u6570\u5b57\u672b\u5c3e\u76840\uff1a4.203e4 (4 \u4f4d\u6709\u6548\u6570\u5b57) 1 2 3 4 \u8d77\u59cb\u503c: 0.0078900 \u5c0f\u6570\u70b9\u53f3\u79fb3\u4f4d: 0007.8900e-3 \u5220\u9664\u6709\u6548\u6570\u5b57\u5de6\u4fa7\u76840: 7.8900e-3 \u4e0d\u9700\u8981\u5220\u9664\u6709\u6548\u6570\u5b57\u672b\u5c3e\u76840: 7.8900e-3 (5 \u4f4d\u6709\u6548\u6570\u5b57) 1 2 3 4 \u8d77\u59cb\u503c: 600.410 \u5c0f\u6570\u70b9\u5de6\u79fb2\u4f4d: 6.00410e2 \u6709\u6548\u6570\u5b57\u5de6\u4fa7\u6ca1\u6709\u9700\u8981\u5220\u9664\u76840: 6.00410e2 \u4e0d\u9700\u8981\u5220\u9664\u6709\u6548\u6570\u5b57\u672b\u5c3e\u76840: 6.00410e2 (6 \u4f4d\u6709\u6548\u6570\u5b57) \u91cd\u70b9\u7406\u89e3\uff1a\u6709\u6548\u6570\u5b57\u90e8\u5206\u7684\u4f4d\u6570 (\u2018e\u2019\u524d\u9762\u7684\u90e8\u5206) \u79f0\u4e3a\u6709\u6548\u6570\u5b57 \u3002\u6709\u6548\u6570\u5b57\u51b3\u5b9a\u4e86\u8fd9\u4e2a\u6570\u7684 \u7cbe\u5ea6 \uff0c\u6709\u6548\u6570\u5b57\u4f4d\u6570\u8d8a\u591a\u8bf4\u660e\u7cbe\u5ea6\u8d8a\u9ad8\u3002","title":"\u5982\u4f55\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u79d1\u5b66\u8ba1\u6570\u6cd5"},{"location":"4-7-Introduction-to-scientific-notation/#0","text":"\u60f3\u8c61\u4e00\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u5c31\u540c\u4e00\u4e2a\u82f9\u679c\u7684\u91cd\u91cf\u8be2\u95ee\u4e24\u4e2a\u5b9e\u9a8c\u5ba4\u52a9\u624b\uff0c\u4e00\u4e2a\u56de\u7b54\u82f9\u679c\u91cd\u4e3a87\u514b\uff0c\u53e6\u4e00\u4e2a\u56de\u7b54\u4e3a87.00\u514b\uff0c\u5047\u8bbe\u5b83\u4eec\u7684\u7ed3\u679c\u90fd\u662f\u5bf9\u7684\uff0c\u5bf9\u4e8e\u524d\u4e00\u4e2a\u56de\u7b54\u6765\u8bf4\uff0c\u82f9\u679c\u7684\u771f\u5b9e\u91cd\u91cf\u53ef\u80fd\u4f4d\u4e8e86.50 \u548c 87.49 \u514b\u4e4b\u95f4\u3002\u4e5f\u8bb8\u4ed6\u4f7f\u7528\u7684\u5929\u5e73\u7cbe\u5ea6\u53ea\u5230\u514b\uff0c\u4e5f\u8bb8\u662f\u4ed6\u8fdb\u884c\u4e86\u56db\u820d\u4e94\u5165\u3002\u800c\u5bf9\u4e8e\u540e\u4e00\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u5176\u7cbe\u5ea6\u66f4\u52a0\u81ea\u4fe1(\u91cd\u91cf\u5728 86.9950 \u548c 87.0049 \u514b\u4e4b\u95f4\uff0c\u53d8\u5316\u533a\u95f4\u66f4\u5c0f\u4e86)\u3002 \u6240\u4ee5\uff0c\u5728\u6807\u51c6\u7684\u79d1\u5b66\u8ba1\u6570\u6cd5\u4e2d\uff0c\u6211\u4eec\u503e\u5411\u4e8e\u4fdd\u7559\u5c0f\u6570\u70b9\u540e\u9762\u5c3e\u90e8\u76840\uff0c\u56e0\u4e3a\u8fd9\u4e9b0\u53ef\u4ee5\u6709\u6548\u5730\u8868\u660e\u8be5\u6570\u503c\u7684\u7cbe\u5ea6\u3002 \u4e0d\u8fc7\uff0c\u5728C++\u4e2d\uff0c87 \u548c 87.000 \u5e76\u6ca1\u6709\u4efb\u4f55\u533a\u522b\uff0c\u7f16\u8bd1\u5668\u5b58\u50a8\u4e24\u6570\u7684\u7ed3\u679c\u4e5f\u662f\u5b8c\u5168\u4e00\u6837\u7684\u3002\u4ece\u6280\u672f\u4e0a\u8bb2\uff0c\u4f7f\u7528\u4e24\u79cd\u5199\u6cd5\u6ca1\u6709\u4efb\u4f55\u533a\u522b\uff08\u5f53\u7136\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u4ee3\u7801\u5373\u6587\u6863\u7684\u8868\u793a\u65b9\u5f0f\uff0c\u4e5f\u8bb8\u4f1a\u6709\u51fa\u4e8e\u79d1\u5b66\u4e0a\u7684\u7406\u7531\uff09\u3002 \u4ecb\u7ecd\u5b8c\u79d1\u5b66\u8ba1\u6570\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb\u4ecb\u7ecd\u6d6e\u70b9\u6570\u4e86\u3002","title":"\u7cbe\u5ea6\u4ee5\u53ca\u5c0f\u6570\u70b9\u540e\u76840"},{"location":"4-8-Floating-point-numbers/","tags":["data type","float"],"text":"Key Takeaway \u6d6e\u70b9\u6570\u6570\u636e\u7c7b\u578b\u6709\u4e09\u79cd\uff1a float (4 bytes), double (8 byte) \u548c long double (8\u300112\u300116byte) \u6d6e\u70b9\u6570\u6570\u636e\u7c7b\u578b\u59cb\u7ec8\u662f\u6709\u7b26\u53f7\u7684 \u5728\u4f7f\u7528\u6d6e\u70b9\u6570\u5b57\u9762\u91cf\u65f6\uff0c\u8bf7\u59cb\u7ec8\u4fdd\u7559\u4e00\u4f4d\u5c0f\u6570 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\u6d6e\u70b9\u6570\u5b57\u9762\u91cf\u4e3a double \u3002\u4f7f\u7528 f \u540e\u7f00\u53ef\u4ee5\u6807\u6ce8\u8be5\u5b57\u9762\u91cf\u4e3a float \u8bf7\u786e\u4fdd\u4f60\u4f7f\u7528\u7684\u5b57\u9762\u91cf\u548c\u5b83\u8d4b\u503c\u7684\u7c7b\u578b\u662f\u5339\u914d\u7684\u3002\u5426\u5219\u4f1a\u53d1\u751f\u4e0d\u5fc5\u8981\u7684\u8f6c\u6362\uff0c\u5bfc\u81f4\u7cbe\u5ea6\u4e22\u5931 \u5982\u679c\u5185\u5b58\u7a7a\u95f4\u5141\u8bb8\uff0c\u5c3d\u91cf\u4f7f\u7528 double \u800c\u4e0d\u662f float\uff0c\u56e0\u4e3a float \u7684\u7cbe\u5ea6\u66f4\u4f4e\uff0c\u4e5f\u5c31\u6709\u53ef\u80fd\u9020\u6210\u6570\u503c\u4e0d\u51c6\u786e\u7684\u95ee\u9898 \u820d\u5165\u8bef\u5dee\u59cb\u7ec8\u5b58\u5728\uff0c\u5b83\u662f\u89c4\u5219\u7684\u4e00\u90e8\u5206\uff0c\u6c38\u8fdc\u4e0d\u80fd\u5047\u5b9a\u6d6e\u70b9\u6570\u662f\u7cbe\u786e\u7684 \u6574\u578b\u53ef\u4ee5\u5f88\u597d\u5730\u8868\u793a\u6574\u6570\uff0c\u4f46\u662f\u5f88\u591a\u65f6\u5019\u6211\u4eec\u9700\u8981\u4fdd\u5b58\u975e\u5e38\u5927\u7684\u6570\uff0c\u6216\u8005\u5c0f\u6570\u3002 \u6d6e\u70b9\u6570 \u7c7b\u578b\u7684\u53d8\u91cf\u53ef\u4ee5\u7528\u6765\u5b58\u653e\u5b9e\u6570\uff0c\u4f8b\u59824320.0\u3001-3.33 \u6216 0.01226\u3002\u6d6e\u70b9\u6570\u540d\u5b57\u4e2d\u7684 \u6d6e\u70b9 \u4e8c\u5b57\uff0c\u5f62\u8c61\u5730\u8bf4\u660e\u4e86\u5c0f\u6570\u70b9\u53ef\u4ee5\u6d6e\u52a8\uff1b\u4e5f\u5c31\u662f\u8bf4\u5b83\u53ef\u4ee5\u652f\u6301\u5c0f\u6570\u70b9\u524d\u548c\u5c0f\u6570\u70b9\u540e\u5177\u6709\u4e0d\u540c\u4f4d\u7684\u6570\u5b57\u3002 \u6d6e\u70b9\u6570\u6570\u636e\u7c7b\u578b\u6709\u4e09\u79cd\uff1a float , double \u548c long double \u3002\u548c\u6574\u578b\u4e00\u6837 C++ \u5e76\u6ca1\u6709\u5b9a\u4e49\u8fd9\u4e09\u79cd\u7c7b\u578b\u7684\u5177\u4f53\u957f\u5ea6\uff08\u53ea\u786e\u4fdd\u5176\u6700\u5c0f\u503c\uff09\u3002\u5728\u73b0\u4ee3\u8ba1\u7b97\u673a\u4e0a\uff0c\u6d6e\u70b9\u6570\u7684\u8868\u793a\u65b9\u6cd5\u51e0\u4e4e\u603b\u662f\u9075\u5faa IEEE 754 \u4e8c\u8fdb\u5236\u683c\u5f0f\u3002\u5728\u8fd9\u79cd\u683c\u5f0f\u4e0b\uff0c float \u4e3a4\u4e2a\u5b57\u8282\uff0c double \u5219\u662f 8 \u4e2a\u5b57\u8282\uff0c\u800c long double \u53ef\u4ee5\u548c double \u76f8\u540c\uff088\u5b57\u8282\uff09\uff0c\u4e5f\u53ef\u80fd\u4e3a80\u4f4d\uff08\u901a\u5e38\u4f1a\u8865\u4f4d\u523012\u5b57\u8282\uff09\u6216\u800516\u5b57\u8282\u3002 \u6d6e\u70b9\u6570\u6570\u636e\u7c7b\u578b\u59cb\u7ec8\u662f\u6709\u7b26\u53f7\u7684\uff08\u53ef\u4ee5\u4fdd\u5b58\u6b63\u8d1f\u6570\uff09\u3002 Category Type Minimum Size Typical Size floating point float 4 bytes 4 bytes double 8 bytes 8 bytes long double 8 bytes 8, 12, or 16 bytes \u4e0b\u9762\u662f\u4e00\u4e9b\u6d6e\u70b9\u6570\u7684\u4f8b\u5b50\uff1a 1 2 3 float fValue ; double dValue ; long double ldValue ; \u5728\u4f7f\u7528\u6d6e\u70b9\u6570 \u5b57\u9762\u91cf \u65f6\uff0c\u8bf7\u59cb\u7ec8\u4fdd\u7559\u4e00\u4f4d\u5c0f\u6570\uff08\u5373\u4f7f\u8fd9\u4e00\u4f4d\u662f0\uff09\u3002\u8fd9\u53ef\u4ee5\u5e2e\u52a9\u7f16\u8bd1\u5668\u660e\u786e\u8be5\u6570\u503c\u4e3a\u6d6e\u70b9\u6570\u800c\u975e\u6574\u578b\u6570\u3002 1 2 3 int x { 5 }; // 5 \u8868\u793a\u6574\u578b\u6570 double y { 5.0 }; // 5.0 \u662f\u6d6e\u70b9\u6570\u5b57\u9762\u91cf (\u6ca1\u6709\u540e\u7f00\u7684\u60c5\u51b5\u4e0b\u9ed8\u8ba4\u4e3a double) float z { 5.0f }; // 5.0 \u662f\u6d6e\u70b9\u6570\u5b57\u9762\u91cf\uff0cf \u540e\u7f00\u8868\u793a float \u6ce8\u610f\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u6d6e\u70b9\u6570\u5b57\u9762\u91cf\u4e3a double \u3002\u4f7f\u7528 f \u540e\u7f00\u53ef\u4ee5\u6807\u6ce8\u8be5\u5b57\u9762\u91cf\u4e3a float \u6700\u4f73\u5b9e\u8df5 \u8bf7\u786e\u4fdd\u4f60\u4f7f\u7528\u7684\u5b57\u9762\u91cf\u548c\u5b83\u8d4b\u503c\u7684\u7c7b\u578b\u662f\u5339\u914d\u7684\u3002\u5426\u5219\u4f1a\u53d1\u751f\u4e0d\u5fc5\u8981\u7684\u8f6c\u6362\uff0c\u5bfc\u81f4\u7cbe\u5ea6\u4e22\u5931\u3002 \u6ce8\u610f \u8bf7\u786e\u4fdd\u5728\u5e94\u5f53\u4f7f\u7528\u6d6e\u70b9\u6570\u5b57\u9762\u91cf\u65f6\uff0c\u4e0d\u8981\u8bef\u7528\u6574\u578b\u5b57\u9762\u91cf\u3002\u8fd9\u53ef\u80fd\u4f1a\u53d1\u751f\u5bf9\u6d6e\u70b9\u7c7b\u578b\u5bf9\u8c61\u521d\u59cb\u5316\u3001\u8d4b\u503c\u3001\u6570\u5b66\u8fd0\u7b97\u4ee5\u53ca\u8c03\u7528\u8fd4\u56de\u503c\u5e94\u5f53\u4e3a\u6d6e\u70b9\u6570\u7684\u51fd\u6570\u65f6\u3002 \u6253\u5370\u6d6e\u70b9\u6570 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { std :: cout << 5.0 << '\\n' ; std :: cout << 6.7f << '\\n' ; std :: cout << 9876543.21 << '\\n' ; return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u53ef\u80fd\u4f1a\u8ba9\u4f60\u611f\u5230\u610f\u5916\uff1a 1 2 3 5 6.7 9.87654e+06 \u5728\u7b2c\u4e00\u6761\u6253\u5370\u4e2d\uff0c std::cout \u6253\u5370 5\uff0c\u5373\u4f7f\u6211\u4eec\u8f93\u5165\u7684\u662f5.0\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b std::cout \u4e0d\u4f1a\u8f93\u5165\u5c0f\u6570\u90e8\u5206\u76840\u3002 \u5728\u7b2c\u4e8c\u6761\u6253\u5370\u4e2d\uff0c\u7ed3\u679c\u548c\u6211\u4eec\u671f\u671b\u7684\u662f\u4e00\u6837\u3002 \u5728\u7b2c\u4e09\u6761\u6253\u5370\u4e2d\uff0c\u6253\u5370\u7ed3\u679c\u4e3a\u79d1\u5b66\u8ba1\u6570\u6cd5\uff08\u5982\u679c\u4f60\u9700\u8981\u590d\u4e60\u4e00\u4e0b\u79d1\u5b66\u8ba1\u6570\u6cd5\u53ef\u4ee5\u53c2\u8003 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 \uff09\u3002 \u6d6e\u70b9\u6570\u7684\u8303\u56f4 \u5047\u5b9a\u4f7f\u7528 IEEE 754 \u8868\u793a\u6cd5\uff1a \u5927\u5c0f \u8303\u56f4 \u7cbe\u5ea6 4 bytes \u00b11.18 x 10-38 \u5230 \u00b13.4 x 1038 6-9 \u4f4d\u6709\u6548\u6570\u5b57\uff0c\u4e00\u822c\u4e3a7 8 bytes \u00b12.23 x 10-308 \u5230 \u00b11.80 x 10308 15-18 \u4f4d\u6709\u6548\u6570\u5b57\uff0c\u4e00\u822c\u4e3a16 80-bits (typically uses 12 \u6216 16 bytes) \u00b13.36 x 10-4932 \u5230 \u00b11.18 x 104932 18-21 \u4f4d\u6709\u6548\u6570\u5b57 16 bytes \u00b13.36 x 10-4932 \u5230 \u00b11.18 x 104932 33-36 \u4f4d\u6709\u6548\u6570\u5b57 80\u4f4d\u7684\u6d6e\u70b9\u6570\u7c7b\u578b\u53ef\u4ee5\u770b\u505a\u662f\u5386\u53f2\u9057\u7559\u95ee\u9898\u3002\u5728\u73b0\u4ee3\u5904\u7406\u5668\u4e0a\uff0c\u5b83\u901a\u5e38\u88ab\u5b9e\u73b0\u4e3a12\u5b57\u8282\u621616\u5b57\u8282\uff08\u5bf9\u4e8e\u5904\u7406\u5668\u6765\u8bf4\u662f\u66f4\u52a0\u65b9\u4fbf\u7684\u957f\u5ea6\uff09\u3002 \u4f60\u53ef\u80fd\u4f1a\u5947\u602a\u4e3a\u4ec0\u4e4880\u4f4d\u7684\u6d6e\u70b9\u6570\u7c7b\u578b\u548c16\u5b57\u8282\u7684\u6d6e\u70b9\u6570\u7c7b\u578b\u5177\u6709\u76f8\u540c\u7684\u8303\u56f4\u3002\u8fd9\u662f\u56e0\u4e3a\u5b83\u4eec\u4e2d\u4e13\u95e8\u7528\u6765\u8868\u793a\u6307\u6570\u7684\u4f4d\u662f\u76f8\u540c\u7684\u2014\u2014\u4e0d\u8fc7\uff0c16\u5b57\u8282\u7684\u6d6e\u70b9\u6570\u53ef\u4ee5\u8868\u793a\u66f4\u591a\u7684\u6709\u6548\u6570\u5b57\u3002 \u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6 \u8003\u8651\u4ee5\u4e0b\u5206\u6570 1/3\u3002\u5b83\u7684\u5341\u8fdb\u5236\u8868\u793a\u6cd5\u4e3a 0.33333333333333\u2026 \u65e0\u9650\u5faa\u73af3\u3002\u5982\u679c\u4f60\u5728\u7eb8\u4e0a\u4e00\u76f4\u5199\u7684\u8bdd\uff0c\u5199\u7740\u5199\u7740\u4f60\u5c31\u53d7\u4e0d\u4e86\u4e86\u3002\u6700\u7ec8\u4f60\u5199\u4e0b\u6765\u7684\u53ef\u80fd\u662f 0.3333333333\u2026. (\u65e0\u9650\u5faa\u73af3)\uff0c\u4f46\u7edd\u5bf9\u4e0d\u53ef\u80fd\u662f\u5168\u90e8\u7684\u503c\u3002 \u5bf9\u4e8e\u8ba1\u7b97\u673a\u6765\u8bf4\uff0c\u65e0\u9650\u957f\u5ea6\u7684\u6570\u5b57\u9700\u8981\u65e0\u9650\u5927\u7684\u5185\u5b58\u624d\u80fd\u5b58\u50a8\uff0c\u800c\u901a\u5e38\u6211\u4eec\u53ea\u80fd\u4f7f\u75284\u5b57\u8282\u62168\u5b57\u8282\u7684\u7a7a\u95f4\u3002\u6709\u9650\u7684\u5185\u5b58\u5c31\u610f\u5473\u7740\u6d6e\u70b9\u6570\u53ea\u80fd\u5b58\u653e\u7279\u5b9a\u957f\u5ea6\u7684\u6709\u6548\u6570\u5b57\u2014\u2014\u5176\u4ed6\u5269\u4f59\u90e8\u5206\u5c31\u88ab\u4e22\u5f03\u4e86\u3002\u6700\u7ec8\u88ab\u5b58\u50a8\u4e0b\u6765\u7684\u6570\u5b57\u662f\u6211\u4eec\u80fd\u591f\u63a5\u53d7\u7684\u503c\uff0c\u800c\u4e0d\u662f\u5b9e\u9645\u503c\u3002 \u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u88ab\u5b9a\u4e49\u4e3a\uff1a\u5728\u4e0d\u635f\u5931\u4fe1\u606f\u7684\u60c5\u51b5\u4e0b\u80fd\u591f\u8868\u793a\u7684\u6700\u591a\u7684\u6709\u6548\u6570\u5b57\u4f4d\u6570\u3002 \u5728\u8f93\u51fa\u6d6e\u70b9\u6570\u65f6\uff0c std::cout \u7684\u9ed8\u8ba4\u7cbe\u5ea6\u4e3a6\u2014\u2014\u4e5f\u5c31\u8bf4\u5b83\u5047\u8bbe\u6240\u6709\u6d6e\u70b9\u6570\u90fd\u53ea\u67096\u4f4d\u6709\u6548\u6570\u5b57\uff0c\u8d85\u8fc7\u7684\u90e8\u5206\u90fd\u4f1a\u88ab\u622a\u65ad\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u5bf9 std::cout \u622a\u65ad\u52306\u4f4d\u6709\u6548\u6570\u5b57\u8fdb\u884c\u4e86\u6f14\u793a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { std :: cout << 9.87654321f << '\\n' ; std :: cout << 987.654321f << '\\n' ; std :: cout << 987654.321f << '\\n' ; std :: cout << 9876543.21f << '\\n' ; std :: cout << 0.0000987654321f << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 3 4 5 9.87654 987.654 987654 9.87654e+006 9.87654e-005 \u6211\u4eec\u6ce8\u610f\u5230\uff0c\u8f93\u51fa\u7ed3\u679c\u4e2d\u7684\u6570\u90fd\u53ea\u67096\u4f4d\u6709\u6548\u6570\u5b57\u3002 \u540c\u65f6\u6211\u4eec\u6ce8\u610f\u5230\uff0c std::cout \u6709\u4e9b\u60c5\u51b5\u4e0b\u4f1a\u81ea\u52a8\u6539\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5\u3002\u6839\u636e\u7f16\u8bd1\u5668\u7684\u4e0d\u540c\uff0c\u6307\u6570\u90e8\u5206\u4f1a\u88ab\u88650\u5230\u6700\u5c0f\u4f4d\u6570\u3002\u4e0d\u8981\u62c5\u5fc3\uff0c9.87654e+006 \u548c 9.87654e6 \u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u53ea\u662f\u8865\u4e86\u4e00\u4e9b0\u7f62\u4e86\u3002\u6307\u6570\u7684\u6700\u5c0f\u4f4d\u6570\u6839\u7f16\u8bd1\u5668\u6709\u5173\uff0cVisual Studio \u662f 3\uff0c\u5176\u4ed6\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u4f7f\u7528C99\u7684\u6807\u51c6\u53732\u3002 \u6d6e\u70b9\u6570\u7cbe\u5ea6\u53d6\u51b3\u4e8e\u6d6e\u70b9\u6570\u7c7b\u578b\u7684\u5927\u5c0f\uff08float\u7684\u7cbe\u5ea6\u5c0f\u4e8edouble\uff09\u4ee5\u53ca\u88ab\u5b58\u50a8\u7684\u5177\u4f53\u503c\uff08\u6709\u4e9b\u503c\u7684\u7cbe\u5ea6\u672c\u8eab\u5c31\u6bd4\u5176\u4ed6\u503c\u9ad8\uff09\u3002float \u7c7b\u578b\u7684\u7cbe\u5ea6\u901a\u5e38\u4f4d\u4e8e 6 \u5230 9\u4e4b\u95f4\uff0c\u591a\u6570\u5177\u6709\u81f3\u5c117\u4f4d\u6709\u6548\u6570\u5b57\u3002Double \u7c7b\u578b\u7684\u7cbe\u5ea6\u901a\u5e38\u4e3a 15 \u5230 18\u4e4b\u95f4\uff0c\u591a\u6570\u5177\u6709\u81f3\u5c1116\u4f4d\u6709\u6548\u6570\u5b57\u3002Long double \u7c7b\u578b\u7684\u7cbe\u5ea6\u6700\u5c0f\u4e3a15\u300118\u621633\u4f4d\u6709\u6548\u6570\u5b57\uff0c\u53d6\u51b3\u4e8e\u5b83\u5360\u7528\u591a\u5c11\u5b57\u8282\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u540d\u4e3a std::setprecision() \u7684 \u8f93\u51fa\u64cd\u7eb5\u5668(Output manipulators) \u51fd\u6570\u6765\u6307\u5b9a std::cout \u7684\u7cbe\u5ea6\u3002\u8f93\u51fa\u64cd\u7eb5\u5668\u53ef\u4ee5\u4fee\u6539\u6570\u636e\u8f93\u51fa\u7684\u65b9\u5f0f\uff0c\u5b83\u4eec\u88ab\u5b9a\u4e49\u5728 iomanip \u5934\u6587\u4ef6\u4e2d. 1 2 3 4 5 6 7 8 9 10 11 #include #include // for output manipulator std::setprecision() int main () { std :: cout << std :: setprecision ( 16 ); // \u663e\u793a16\u4f4d\u7cbe\u5ea6 std :: cout << 3.33333333333333333333333333333333333333f << '\\n' ; // f \u540e\u7f00\u8868\u793a float std :: cout << 3.33333333333333333333333333333333333333 << '\\n' ; // \u6ca1\u6709\u540e\u7f00\u9ed8\u8ba4 double return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3.333333253860474 3.333333333333334 \u901a\u8fc7 std::setprecision() \u8bbe\u7f6e\u7cbe\u5ea6\u540e\uff0c\u4e0a\u8ff0\u6570\u5b57\u90fd\u88ab\u6253\u5370\u4e3a16\u4f4d\u7cbe\u5ea6\u3002\u4f46\u662f\uff0c\u5982\u4f60\u6240\u89c1\uff0c\u5176\u7cbe\u5ea6\u5e76\u6ca1\u6709\u8fbe\u523016\u4f4d\u3002\u800c\u4e14\u7531\u4e8e float \u7684\u7cbe\u5ea6\u6bd4 double \u8fd8\u4f4e\uff0c float \u7c7b\u578b\u7684\u503c\u88ab\u6253\u5370\u51fa\u6765\u540e\u8bef\u5dee\u4e5f\u66f4\u5927\u3002 \u7cbe\u5ea6\u95ee\u9898\u4e0d\u4ec5\u4ec5\u5f71\u54cd\u5c0f\u6570\uff0c\u5b83\u8fd8\u4f1a\u5f71\u54cd\u5177\u6709\u5f88\u591a\u6709\u6548\u6570\u5b57\u4f4d\u7684\u6570\u3002\u8ba9\u6211\u4eec\u8003\u8651\u5982\u4e0b\u5927\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include // for std::setprecision() #include int main () { float f { 123456789.0f }; // f \u6709 10 \u4f4d\u6709\u6548\u6570\u5b57 std :: cout << std :: setprecision ( 9 ); // \u663e\u793a f \u7684 9 \u4f4d\u6709\u6548\u6570\u5b57\u3002 std :: cout << f << '\\n' ; return 0 ; } \u8f93\u51fa 1 123456792 123456792 \u6bd4 123456789 \u8981\u5927\u3002123456789.0 \u5177\u6709 10 \u4f4d\u6709\u6548\u6570\u5b57\uff0c\u4f46\u662f float \u901a\u5e38\u53ea\u67097\u4f4d\u7cbe\u5ea6\uff08\u7ed3\u679c123456792\u6b63\u53f7\u67097\u4f4d\u6709\u6548\u6570\u5b57\uff09\u3002\u7cbe\u5ea6\u4e22\u5931\u4e86\uff01\u7cbe\u5ea6\u4e22\u5931\u4e3b\u8981\u662f\u56e0\u4e3a\u6570\u6ca1\u6709\u88ab\u7cbe\u786e\u7684\u5b58\u50a8\uff0c\u6211\u4eec\u79f0\u5176\u4e3a \u820d\u5165\u8bef\u5dee(rounding error) \u3002 \u56e0\u6b64\uff0c\u5728\u4f7f\u7528\u7684\u6d6e\u70b9\u6570\u7cbe\u5ea6\u6bd4\u5bf9\u5e94\u6d6e\u70b9\u7c7b\u578b\u53d8\u91cf\u80fd\u591f\u8868\u793a\u7684\u7cbe\u5ea6\u66f4\u9ad8\u65f6\uff0c\u8981\u591a\u52a0\u6ce8\u610f\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u5185\u5b58\u7a7a\u95f4\u5141\u8bb8\uff0c\u5c3d\u91cf\u4f7f\u7528 double \u800c\u4e0d\u662f float\uff0c\u56e0\u4e3a float \u7684\u7cbe\u5ea6\u66f4\u4f4e\uff0c\u4e5f\u5c31\u6709\u53ef\u80fd\u9020\u6210\u6570\u503c\u4e0d\u51c6\u786e\u7684\u95ee\u9898\u3002 \u820d\u5165\u8bef\u5dee\u7684\u5b58\u5728\u589e\u52a0\u4e86\u6d6e\u70b9\u6570\u6bd4\u8f83\u7684\u96be\u5ea6 \u6d6e\u70b9\u6570\u4f7f\u7528\u8d77\u6765\u66f4\u52a0\u56f0\u96be\uff0c\u8fd9\u662f\u56e0\u4e3a\u5b83\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u6cd5\uff08\u6570\u636e\u5b58\u50a8\u5f62\u5f0f\uff09\u548c\u5341\u8fdb\u5236\u8868\u793a\u6cd5\uff08\u6211\u4eec\u7684\u7406\u89e3\u65b9\u5f0f\uff09\u4e4b\u95f4\u7684\u5173\u7cfb\u5e76\u4e0d\u660e\u663e\u3002\u5bf9\u4e8e\u5206\u6570 1/10 \u6765\u8bf4\uff0c\u5341\u8fdb\u5236\u8868\u793a\u6cd5\u5f88\u5bb9\u6613\u5c06\u5176\u8868\u793a\u4e3a 0.1\uff0c\u800c\u6211\u4eec\u4e5f\u4e60\u60ef\u6027\u7684\u8ba4\u4e3a0.1\u5f88\u5bb9\u6613\u8868\u793a\uff0c\u6bd5\u7adf\u5b83\u53ea\u6709\u4e00\u4f4d\u6709\u6548\u6570\u5b57\u3002\u5b9e\u9645\u4e0a\uff0c\u5bf9\u4e8e\u4e8c\u8fdb\u5236\u6765\u8bf4\uff0c0.1\u9700\u8981\u8868\u793a\u4e3a\u4e00\u4e2a\u65e0\u7a77\u5e8f\u5217\uff1a0.00011001100110011\u2026 \u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u628a0.1\u8d4b\u503c\u7ed9\u6d6e\u70b9\u6570\u65f6\uff0c\u5b9e\u9645\u4e0a\u4f1a\u51fa\u73b0\u7cbe\u5ea6\u95ee\u9898\u3002 \u900f\u8fc7\u4e0b\u9762\u7a0b\u5e8f\u4e2d\u4f60\u53ef\u4ee5\u770b\u5230\u5176\u5f71\u54cd\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include // for std::setprecision() #include int main () { double d { 0.1 }; std :: cout << d << '\\n' ; // use default cout precision of 6 std :: cout << std :: setprecision ( 17 ); std :: cout << d << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 0.1 0.10000000000000001 \u7b2c\u4e00\u884c\uff0c std::cout \u6253\u5370\u4e86 0.1\uff0c\u5982\u6211\u4eec\u6240\u6599\u3002 \u7b2c\u4e8c\u884c\uff0c\u6211\u4eec\u8ba9 std::cout \u663e\u793a17\u4f4d\u7cbe\u5ea6\uff0c\u53ef\u4ee5\u770b\u5230\uff0c d \u7684\u6253\u5370\u7ed3\u679c\u5e76\u4e0d\u662f0.1\u3002\u8fd9\u662f\u56e0\u4e3a double \u5fc5\u987b\u5bf9\u6570\u636e\u8fdb\u884c\u622a\u65ad\uff0c\u6bd5\u7adf\u5185\u5b58\u4e0d\u662f\u65e0\u9650\u7684\u3002\u7ed3\u679c\u5c31\u662f\u6570\u636e\u7684\u7cbe\u5ea6\u7f16\u7a0b\u4e8616\u4f4d(double\u4fdd\u8bc1\u7684)\uff0c\u4f46\u662f\u5176\u503c\u5374\u4e0d\u662f0.1\u3002\u820d\u5165\u8bef\u5dee\u53ef\u80fd\u4f1a\u5f97\u5230\u4e00\u4e2a\u7a0d\u5927\u4e00\u70b9\u6216\u7a0d\u5c0f\u4e00\u70b9\u7684\u503c\uff0c\u8fd9\u53d6\u51b3\u4e8e\u622a\u65ad\u53d1\u751f\u5728\u54ea\u91cc\u3002 \u820d\u5165\u8bef\u5dee\u4f1a\u5e26\u6765\u975e\u9884\u671f\u7684\u7ed3\u679c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // for std::setprecision() #include int main () { std :: cout << std :: setprecision ( 17 ); double d1 { 1.0 }; std :: cout << d1 << '\\n' ; double d2 { 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 }; // should equal 1.0 std :: cout << d2 << '\\n' ; return 0 ; } 1 2 1 0.99999999999999989 \u5c3d\u7ba1\u6211\u4eec\u671f\u671b d1 \u7684\u503c\u5e94\u8be5\u7b49\u4e8e d2 \uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u6211\u4eec\u4e5f\u770b\u5230\u4e86\u5b83\u4eec\u5e76\u4e0d\u76f8\u7b49\u3002\u5982\u679c\u6211\u4eec\u5728\u7a0b\u5e8f\u4e2d\u5bf9 d1 \u548c d2 \u8fdb\u884c\u6bd4\u8f83\uff0c\u90a3\u4e48\u7ed3\u679c\u53ef\u80fd\u5c31\u4f1a\u51fa\u4eba\u610f\u6599\u3002\u56e0\u4e3a\u6d6e\u70b9\u6570\u7684\u8fd9\u79cd\u4e0d\u7cbe\u786e\u7684\u7279\u6027\uff0c\u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\u901a\u5e38\u4f1a\u4ea7\u751f\u95ee\u9898\u2014\u2014\u6211\u4eec\u4f1a\u5728 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 \u4e2d\u8fdb\u884c\u8be6\u7ec6\u5730\u8ba8\u8bba\u3002 \u5173\u4e8e\u820d\u5165\u8bef\u5dee\u7684\u6700\u540e\u4e00\u70b9\u53ee\u5631\uff1a\u6570\u5b66\u8fd0\u7b97 (\u4f8b\u5982\u52a0\u6cd5\u548c\u4e58\u6cd5) \u4f1a\u5bfc\u81f4\u820d\u5165\u8bef\u5dee\u7684\u79ef\u7d2f\u3002\u6240\u4ee5\u5373\u4f7f 0.1 \u53ea\u5728\u7b2c 17\u4f4d\u6709\u6548\u6570\u5b57\u65f6\u5b58\u5728\u8bef\u5dee\uff0c\u5f53\u6211\u4eec\u5c06\u662f\u4e2a0.1\u76f8\u52a0\u65f6\uff0c\u820d\u5165\u8bef\u5dee\u5c31\u51fa\u73b0\u5728\u4e86\u7b2c16\u4f4d\u3002\u4e0d\u65ad\u5730\u8ba1\u7b97\u4f1a\u5bfc\u81f4\u8fd9\u4e9b\u8bef\u5dee\u88ab\u4e0d\u65ad\u5730\u653e\u5927\u3002 \u5173\u952e\u4fe1\u606f \u5f53\u6570\u636e\u4e0d\u80fd\u88ab\u7cbe\u786e\u7684\u5b58\u50a8\u65f6\uff0c\u4f1a\u51fa\u73b0\u820d\u5165\u8bef\u5dee\u3002\u5373\u4f7f\u662f\u975e\u5e38\u7b80\u5355\u7684\u6570\u5b57\u4e5f\u5b58\u5728\u8be5\u95ee\u9898\uff08\u4f8b\u5982 0.1\uff09\u3002\u56e0\u6b64\uff0c\u820d\u5165\u8bef\u5dee\u603b\u662f\u65e0\u65f6\u65e0\u523b\u5730\u53d1\u751f\u3002\u6444\u5165\u8bef\u5dee\u5e76\u4e0d\u662f\u5f02\u5e38\uff0c\u5b83\u662f\u89c4\u5219\u7684\u4e00\u90e8\u5206\u3002\u6c38\u8fdc\u4e0d\u8981\u5047\u8bbe\u4f60\u7684\u6d6e\u70b9\u6570\u662f\u7cbe\u786e\u7684\u3002 \u8fd9\u6761\u89c4\u5219\u7684\u7ed3\u679c\u5c31\u662f\u5728\u4f7f\u7528\u6d6e\u70b9\u6570\u5904\u7406\u91d1\u878d\u6216\u8d27\u5e01\u6570\u636e\u65f6\uff0c\u4e00\u5b9a\u8981\u683c\u5916\u8b66\u60d5\u3002 NaN \u548c Inf \u8fd8\u6709\u4e24\u7c7b\u7279\u6b8a\u7684\u6d6e\u70b9\u6570\u3002\u4e00\u4e2a\u662f Inf \uff0c\u8868\u793a\u65e0\u7a77\u3002 Inf \u53ef\u4ee5\u662f\u6b63\u4e5f\u53ef\u4ee5\u662f\u8d1f\u3002\u7b2c\u4e8c\u4e2a\u662f NaN \uff0c\u8868\u793a\u201d\u975e\u6570\u5b57\u201c\u3002 NaN \u7684\u79cd\u7c7b\u6709\u5f88\u591a\uff08\u6211\u4eec\u4e0d\u4f1a\u5728\u6b64\u8ba8\u8bba\uff09\u3002 NaN \u548c Inf \u53ea\u6709\u5728\u7f16\u8bd1\u5668\u4f7f\u7528\u7279\u5b9a\u683c\u5f0f(IEEE 754) \u7684\u6d6e\u70b9\u6570\u65f6\u624d\u53ef\u4ee5\u4f7f\u7528\u3002\u5f53\u4f7f\u7528\u5176\u4ed6\u683c\u5f0f\u7684\u65f6\u5019\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u5c31\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e0b\u9762\u7684\u4f8b\u7a0b\u5c55\u793a\u4e86\u4e0a\u8ff0\u4e09\u79cd\u6d6e\u70b9\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { double zero { 0.0 }; double posinf { 5.0 / zero }; // \u6b63\u65e0\u7a77 std :: cout << posinf << '\\n' ; double neginf { -5.0 / zero }; // \u8d1f\u65e0\u7a77 std :: cout << neginf << '\\n' ; double nan { zero / zero }; // \u975e\u6570\u5b57 (\u6570\u5b66\u4e0a\u65e0\u6548) std :: cout << nan << '\\n' ; return 0 ; } \u5728 Windows \u4e0a\u7684 Visual Studio 2008 \u8fd0\u884c\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 1.#INF -1.#INF 1.#IND INF \u8868\u793a\u65e0\u7a77\uff0c\u800c IND \u8868\u793a\u4e0d\u786e\u5b9a\uff08indeterminate\uff09\u3002\u6ce8\u610f\uff0c Inf \u548c NaN \u7684\u6253\u5370\u7ed3\u679c\u548c\u5e73\u53f0\u662f\u76f8\u5173\u7684\uff0c\u5728\u4f60\u7684\u7535\u8111\u4e0a\u7ed3\u679c\u53ef\u80fd\u6709\u6240\u4e0d\u540c\u3002 \u6700\u4f73\u5b9e\u8df5 \u8981\u5f7b\u5e95\u907f\u514d\u96640\uff0c\u5373\u4f7f\u4f60\u7684\u7684\u8ba1\u7b97\u673a\u652f\u6301\u8fd9\u79cd\u7528\u6cd5\u3002 \u7ed3\u8bba \u603b\u7ed3\u4e00\u4e0b\uff0c\u5173\u4e8e\u6d6e\u70b9\u6570\u4f60\u9700\u8981\u8bb0\u4f4f\u4e0b\u9762\u4e24\u4ef6\u4e8b\uff1a \u6d6e\u70b9\u6570\u5728\u8868\u793a\u5f88\u5927\u6216\u5f88\u5c0f\u7684\u6570\u65f6\u975e\u5e38\u6709\u7528\uff0c\u5f53\u7136\u4e5f\u9002\u5408\u4fdd\u5b58\u6709\u5c0f\u6570\u90e8\u5206\u7684\u6570\u3002 \u6d6e\u70b9\u6570\u901a\u5e38\u4f1a\u6709\u5fae\u5c0f\u7684\u820d\u5165\u8bef\u5dee\u95ee\u9898\uff0c\u5373\u4fbf\u4e00\u4e2a\u6570\u7684\u6709\u6548\u6570\u5b57\u6bd4\u7cbe\u5ea6\u80fd\u591f\u8868\u793a\u7684\u6709\u6548\u6570\u5b57\u5c11\uff0c\u4e5f\u5b58\u5728\u8be5\u95ee\u9898\u3002\u5f88\u591a\u65f6\u5019\u8fd9\u4e9b\u95ee\u9898\u4e0d\u4f1a\u88ab\u6211\u4eec\u6ce8\u610f\u5230\uff0c\u56e0\u4e3a\u6444\u5165\u8bef\u5dee\u5f88\u5c0f\uff0c\u800c\u4e14\u8f93\u51fa\u7ed3\u679c\u4f1a\u88ab\u622a\u65ad\uff0c\u4f46\u662f\uff0c\u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\u65f6\uff0c\u820d\u5165\u8bef\u5dee\u7684\u5b58\u5728\u53ef\u80fd\u4f1a\u5e26\u6765\u610f\u5916\u7684\u7ed3\u679c\u3002 \u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u65f6\u4e5f\u53ef\u80fd\u9020\u6210\u820d\u5165\u8bef\u5dee\u7684\u4e0d\u65ad\u589e\u5927\u3002","title":"4.8 - \u6d6e\u70b9\u6570"},{"location":"4-8-Floating-point-numbers/#_1","text":"\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { std :: cout << 5.0 << '\\n' ; std :: cout << 6.7f << '\\n' ; std :: cout << 9876543.21 << '\\n' ; return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u53ef\u80fd\u4f1a\u8ba9\u4f60\u611f\u5230\u610f\u5916\uff1a 1 2 3 5 6.7 9.87654e+06 \u5728\u7b2c\u4e00\u6761\u6253\u5370\u4e2d\uff0c std::cout \u6253\u5370 5\uff0c\u5373\u4f7f\u6211\u4eec\u8f93\u5165\u7684\u662f5.0\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b std::cout \u4e0d\u4f1a\u8f93\u5165\u5c0f\u6570\u90e8\u5206\u76840\u3002 \u5728\u7b2c\u4e8c\u6761\u6253\u5370\u4e2d\uff0c\u7ed3\u679c\u548c\u6211\u4eec\u671f\u671b\u7684\u662f\u4e00\u6837\u3002 \u5728\u7b2c\u4e09\u6761\u6253\u5370\u4e2d\uff0c\u6253\u5370\u7ed3\u679c\u4e3a\u79d1\u5b66\u8ba1\u6570\u6cd5\uff08\u5982\u679c\u4f60\u9700\u8981\u590d\u4e60\u4e00\u4e0b\u79d1\u5b66\u8ba1\u6570\u6cd5\u53ef\u4ee5\u53c2\u8003 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 \uff09\u3002","title":"\u6253\u5370\u6d6e\u70b9\u6570"},{"location":"4-8-Floating-point-numbers/#_2","text":"\u5047\u5b9a\u4f7f\u7528 IEEE 754 \u8868\u793a\u6cd5\uff1a \u5927\u5c0f \u8303\u56f4 \u7cbe\u5ea6 4 bytes \u00b11.18 x 10-38 \u5230 \u00b13.4 x 1038 6-9 \u4f4d\u6709\u6548\u6570\u5b57\uff0c\u4e00\u822c\u4e3a7 8 bytes \u00b12.23 x 10-308 \u5230 \u00b11.80 x 10308 15-18 \u4f4d\u6709\u6548\u6570\u5b57\uff0c\u4e00\u822c\u4e3a16 80-bits (typically uses 12 \u6216 16 bytes) \u00b13.36 x 10-4932 \u5230 \u00b11.18 x 104932 18-21 \u4f4d\u6709\u6548\u6570\u5b57 16 bytes \u00b13.36 x 10-4932 \u5230 \u00b11.18 x 104932 33-36 \u4f4d\u6709\u6548\u6570\u5b57 80\u4f4d\u7684\u6d6e\u70b9\u6570\u7c7b\u578b\u53ef\u4ee5\u770b\u505a\u662f\u5386\u53f2\u9057\u7559\u95ee\u9898\u3002\u5728\u73b0\u4ee3\u5904\u7406\u5668\u4e0a\uff0c\u5b83\u901a\u5e38\u88ab\u5b9e\u73b0\u4e3a12\u5b57\u8282\u621616\u5b57\u8282\uff08\u5bf9\u4e8e\u5904\u7406\u5668\u6765\u8bf4\u662f\u66f4\u52a0\u65b9\u4fbf\u7684\u957f\u5ea6\uff09\u3002 \u4f60\u53ef\u80fd\u4f1a\u5947\u602a\u4e3a\u4ec0\u4e4880\u4f4d\u7684\u6d6e\u70b9\u6570\u7c7b\u578b\u548c16\u5b57\u8282\u7684\u6d6e\u70b9\u6570\u7c7b\u578b\u5177\u6709\u76f8\u540c\u7684\u8303\u56f4\u3002\u8fd9\u662f\u56e0\u4e3a\u5b83\u4eec\u4e2d\u4e13\u95e8\u7528\u6765\u8868\u793a\u6307\u6570\u7684\u4f4d\u662f\u76f8\u540c\u7684\u2014\u2014\u4e0d\u8fc7\uff0c16\u5b57\u8282\u7684\u6d6e\u70b9\u6570\u53ef\u4ee5\u8868\u793a\u66f4\u591a\u7684\u6709\u6548\u6570\u5b57\u3002","title":"\u6d6e\u70b9\u6570\u7684\u8303\u56f4"},{"location":"4-8-Floating-point-numbers/#_3","text":"\u8003\u8651\u4ee5\u4e0b\u5206\u6570 1/3\u3002\u5b83\u7684\u5341\u8fdb\u5236\u8868\u793a\u6cd5\u4e3a 0.33333333333333\u2026 \u65e0\u9650\u5faa\u73af3\u3002\u5982\u679c\u4f60\u5728\u7eb8\u4e0a\u4e00\u76f4\u5199\u7684\u8bdd\uff0c\u5199\u7740\u5199\u7740\u4f60\u5c31\u53d7\u4e0d\u4e86\u4e86\u3002\u6700\u7ec8\u4f60\u5199\u4e0b\u6765\u7684\u53ef\u80fd\u662f 0.3333333333\u2026. (\u65e0\u9650\u5faa\u73af3)\uff0c\u4f46\u7edd\u5bf9\u4e0d\u53ef\u80fd\u662f\u5168\u90e8\u7684\u503c\u3002 \u5bf9\u4e8e\u8ba1\u7b97\u673a\u6765\u8bf4\uff0c\u65e0\u9650\u957f\u5ea6\u7684\u6570\u5b57\u9700\u8981\u65e0\u9650\u5927\u7684\u5185\u5b58\u624d\u80fd\u5b58\u50a8\uff0c\u800c\u901a\u5e38\u6211\u4eec\u53ea\u80fd\u4f7f\u75284\u5b57\u8282\u62168\u5b57\u8282\u7684\u7a7a\u95f4\u3002\u6709\u9650\u7684\u5185\u5b58\u5c31\u610f\u5473\u7740\u6d6e\u70b9\u6570\u53ea\u80fd\u5b58\u653e\u7279\u5b9a\u957f\u5ea6\u7684\u6709\u6548\u6570\u5b57\u2014\u2014\u5176\u4ed6\u5269\u4f59\u90e8\u5206\u5c31\u88ab\u4e22\u5f03\u4e86\u3002\u6700\u7ec8\u88ab\u5b58\u50a8\u4e0b\u6765\u7684\u6570\u5b57\u662f\u6211\u4eec\u80fd\u591f\u63a5\u53d7\u7684\u503c\uff0c\u800c\u4e0d\u662f\u5b9e\u9645\u503c\u3002 \u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u88ab\u5b9a\u4e49\u4e3a\uff1a\u5728\u4e0d\u635f\u5931\u4fe1\u606f\u7684\u60c5\u51b5\u4e0b\u80fd\u591f\u8868\u793a\u7684\u6700\u591a\u7684\u6709\u6548\u6570\u5b57\u4f4d\u6570\u3002 \u5728\u8f93\u51fa\u6d6e\u70b9\u6570\u65f6\uff0c std::cout \u7684\u9ed8\u8ba4\u7cbe\u5ea6\u4e3a6\u2014\u2014\u4e5f\u5c31\u8bf4\u5b83\u5047\u8bbe\u6240\u6709\u6d6e\u70b9\u6570\u90fd\u53ea\u67096\u4f4d\u6709\u6548\u6570\u5b57\uff0c\u8d85\u8fc7\u7684\u90e8\u5206\u90fd\u4f1a\u88ab\u622a\u65ad\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u5bf9 std::cout \u622a\u65ad\u52306\u4f4d\u6709\u6548\u6570\u5b57\u8fdb\u884c\u4e86\u6f14\u793a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { std :: cout << 9.87654321f << '\\n' ; std :: cout << 987.654321f << '\\n' ; std :: cout << 987654.321f << '\\n' ; std :: cout << 9876543.21f << '\\n' ; std :: cout << 0.0000987654321f << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 3 4 5 9.87654 987.654 987654 9.87654e+006 9.87654e-005 \u6211\u4eec\u6ce8\u610f\u5230\uff0c\u8f93\u51fa\u7ed3\u679c\u4e2d\u7684\u6570\u90fd\u53ea\u67096\u4f4d\u6709\u6548\u6570\u5b57\u3002 \u540c\u65f6\u6211\u4eec\u6ce8\u610f\u5230\uff0c std::cout \u6709\u4e9b\u60c5\u51b5\u4e0b\u4f1a\u81ea\u52a8\u6539\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5\u3002\u6839\u636e\u7f16\u8bd1\u5668\u7684\u4e0d\u540c\uff0c\u6307\u6570\u90e8\u5206\u4f1a\u88ab\u88650\u5230\u6700\u5c0f\u4f4d\u6570\u3002\u4e0d\u8981\u62c5\u5fc3\uff0c9.87654e+006 \u548c 9.87654e6 \u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u53ea\u662f\u8865\u4e86\u4e00\u4e9b0\u7f62\u4e86\u3002\u6307\u6570\u7684\u6700\u5c0f\u4f4d\u6570\u6839\u7f16\u8bd1\u5668\u6709\u5173\uff0cVisual Studio \u662f 3\uff0c\u5176\u4ed6\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u4f7f\u7528C99\u7684\u6807\u51c6\u53732\u3002 \u6d6e\u70b9\u6570\u7cbe\u5ea6\u53d6\u51b3\u4e8e\u6d6e\u70b9\u6570\u7c7b\u578b\u7684\u5927\u5c0f\uff08float\u7684\u7cbe\u5ea6\u5c0f\u4e8edouble\uff09\u4ee5\u53ca\u88ab\u5b58\u50a8\u7684\u5177\u4f53\u503c\uff08\u6709\u4e9b\u503c\u7684\u7cbe\u5ea6\u672c\u8eab\u5c31\u6bd4\u5176\u4ed6\u503c\u9ad8\uff09\u3002float \u7c7b\u578b\u7684\u7cbe\u5ea6\u901a\u5e38\u4f4d\u4e8e 6 \u5230 9\u4e4b\u95f4\uff0c\u591a\u6570\u5177\u6709\u81f3\u5c117\u4f4d\u6709\u6548\u6570\u5b57\u3002Double \u7c7b\u578b\u7684\u7cbe\u5ea6\u901a\u5e38\u4e3a 15 \u5230 18\u4e4b\u95f4\uff0c\u591a\u6570\u5177\u6709\u81f3\u5c1116\u4f4d\u6709\u6548\u6570\u5b57\u3002Long double \u7c7b\u578b\u7684\u7cbe\u5ea6\u6700\u5c0f\u4e3a15\u300118\u621633\u4f4d\u6709\u6548\u6570\u5b57\uff0c\u53d6\u51b3\u4e8e\u5b83\u5360\u7528\u591a\u5c11\u5b57\u8282\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u540d\u4e3a std::setprecision() \u7684 \u8f93\u51fa\u64cd\u7eb5\u5668(Output manipulators) \u51fd\u6570\u6765\u6307\u5b9a std::cout \u7684\u7cbe\u5ea6\u3002\u8f93\u51fa\u64cd\u7eb5\u5668\u53ef\u4ee5\u4fee\u6539\u6570\u636e\u8f93\u51fa\u7684\u65b9\u5f0f\uff0c\u5b83\u4eec\u88ab\u5b9a\u4e49\u5728 iomanip \u5934\u6587\u4ef6\u4e2d. 1 2 3 4 5 6 7 8 9 10 11 #include #include // for output manipulator std::setprecision() int main () { std :: cout << std :: setprecision ( 16 ); // \u663e\u793a16\u4f4d\u7cbe\u5ea6 std :: cout << 3.33333333333333333333333333333333333333f << '\\n' ; // f \u540e\u7f00\u8868\u793a float std :: cout << 3.33333333333333333333333333333333333333 << '\\n' ; // \u6ca1\u6709\u540e\u7f00\u9ed8\u8ba4 double return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3.333333253860474 3.333333333333334 \u901a\u8fc7 std::setprecision() \u8bbe\u7f6e\u7cbe\u5ea6\u540e\uff0c\u4e0a\u8ff0\u6570\u5b57\u90fd\u88ab\u6253\u5370\u4e3a16\u4f4d\u7cbe\u5ea6\u3002\u4f46\u662f\uff0c\u5982\u4f60\u6240\u89c1\uff0c\u5176\u7cbe\u5ea6\u5e76\u6ca1\u6709\u8fbe\u523016\u4f4d\u3002\u800c\u4e14\u7531\u4e8e float \u7684\u7cbe\u5ea6\u6bd4 double \u8fd8\u4f4e\uff0c float \u7c7b\u578b\u7684\u503c\u88ab\u6253\u5370\u51fa\u6765\u540e\u8bef\u5dee\u4e5f\u66f4\u5927\u3002 \u7cbe\u5ea6\u95ee\u9898\u4e0d\u4ec5\u4ec5\u5f71\u54cd\u5c0f\u6570\uff0c\u5b83\u8fd8\u4f1a\u5f71\u54cd\u5177\u6709\u5f88\u591a\u6709\u6548\u6570\u5b57\u4f4d\u7684\u6570\u3002\u8ba9\u6211\u4eec\u8003\u8651\u5982\u4e0b\u5927\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include // for std::setprecision() #include int main () { float f { 123456789.0f }; // f \u6709 10 \u4f4d\u6709\u6548\u6570\u5b57 std :: cout << std :: setprecision ( 9 ); // \u663e\u793a f \u7684 9 \u4f4d\u6709\u6548\u6570\u5b57\u3002 std :: cout << f << '\\n' ; return 0 ; } \u8f93\u51fa 1 123456792 123456792 \u6bd4 123456789 \u8981\u5927\u3002123456789.0 \u5177\u6709 10 \u4f4d\u6709\u6548\u6570\u5b57\uff0c\u4f46\u662f float \u901a\u5e38\u53ea\u67097\u4f4d\u7cbe\u5ea6\uff08\u7ed3\u679c123456792\u6b63\u53f7\u67097\u4f4d\u6709\u6548\u6570\u5b57\uff09\u3002\u7cbe\u5ea6\u4e22\u5931\u4e86\uff01\u7cbe\u5ea6\u4e22\u5931\u4e3b\u8981\u662f\u56e0\u4e3a\u6570\u6ca1\u6709\u88ab\u7cbe\u786e\u7684\u5b58\u50a8\uff0c\u6211\u4eec\u79f0\u5176\u4e3a \u820d\u5165\u8bef\u5dee(rounding error) \u3002 \u56e0\u6b64\uff0c\u5728\u4f7f\u7528\u7684\u6d6e\u70b9\u6570\u7cbe\u5ea6\u6bd4\u5bf9\u5e94\u6d6e\u70b9\u7c7b\u578b\u53d8\u91cf\u80fd\u591f\u8868\u793a\u7684\u7cbe\u5ea6\u66f4\u9ad8\u65f6\uff0c\u8981\u591a\u52a0\u6ce8\u610f\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u5185\u5b58\u7a7a\u95f4\u5141\u8bb8\uff0c\u5c3d\u91cf\u4f7f\u7528 double \u800c\u4e0d\u662f float\uff0c\u56e0\u4e3a float \u7684\u7cbe\u5ea6\u66f4\u4f4e\uff0c\u4e5f\u5c31\u6709\u53ef\u80fd\u9020\u6210\u6570\u503c\u4e0d\u51c6\u786e\u7684\u95ee\u9898\u3002","title":"\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6"},{"location":"4-8-Floating-point-numbers/#_4","text":"\u6d6e\u70b9\u6570\u4f7f\u7528\u8d77\u6765\u66f4\u52a0\u56f0\u96be\uff0c\u8fd9\u662f\u56e0\u4e3a\u5b83\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u6cd5\uff08\u6570\u636e\u5b58\u50a8\u5f62\u5f0f\uff09\u548c\u5341\u8fdb\u5236\u8868\u793a\u6cd5\uff08\u6211\u4eec\u7684\u7406\u89e3\u65b9\u5f0f\uff09\u4e4b\u95f4\u7684\u5173\u7cfb\u5e76\u4e0d\u660e\u663e\u3002\u5bf9\u4e8e\u5206\u6570 1/10 \u6765\u8bf4\uff0c\u5341\u8fdb\u5236\u8868\u793a\u6cd5\u5f88\u5bb9\u6613\u5c06\u5176\u8868\u793a\u4e3a 0.1\uff0c\u800c\u6211\u4eec\u4e5f\u4e60\u60ef\u6027\u7684\u8ba4\u4e3a0.1\u5f88\u5bb9\u6613\u8868\u793a\uff0c\u6bd5\u7adf\u5b83\u53ea\u6709\u4e00\u4f4d\u6709\u6548\u6570\u5b57\u3002\u5b9e\u9645\u4e0a\uff0c\u5bf9\u4e8e\u4e8c\u8fdb\u5236\u6765\u8bf4\uff0c0.1\u9700\u8981\u8868\u793a\u4e3a\u4e00\u4e2a\u65e0\u7a77\u5e8f\u5217\uff1a0.00011001100110011\u2026 \u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u628a0.1\u8d4b\u503c\u7ed9\u6d6e\u70b9\u6570\u65f6\uff0c\u5b9e\u9645\u4e0a\u4f1a\u51fa\u73b0\u7cbe\u5ea6\u95ee\u9898\u3002 \u900f\u8fc7\u4e0b\u9762\u7a0b\u5e8f\u4e2d\u4f60\u53ef\u4ee5\u770b\u5230\u5176\u5f71\u54cd\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include // for std::setprecision() #include int main () { double d { 0.1 }; std :: cout << d << '\\n' ; // use default cout precision of 6 std :: cout << std :: setprecision ( 17 ); std :: cout << d << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 0.1 0.10000000000000001 \u7b2c\u4e00\u884c\uff0c std::cout \u6253\u5370\u4e86 0.1\uff0c\u5982\u6211\u4eec\u6240\u6599\u3002 \u7b2c\u4e8c\u884c\uff0c\u6211\u4eec\u8ba9 std::cout \u663e\u793a17\u4f4d\u7cbe\u5ea6\uff0c\u53ef\u4ee5\u770b\u5230\uff0c d \u7684\u6253\u5370\u7ed3\u679c\u5e76\u4e0d\u662f0.1\u3002\u8fd9\u662f\u56e0\u4e3a double \u5fc5\u987b\u5bf9\u6570\u636e\u8fdb\u884c\u622a\u65ad\uff0c\u6bd5\u7adf\u5185\u5b58\u4e0d\u662f\u65e0\u9650\u7684\u3002\u7ed3\u679c\u5c31\u662f\u6570\u636e\u7684\u7cbe\u5ea6\u7f16\u7a0b\u4e8616\u4f4d(double\u4fdd\u8bc1\u7684)\uff0c\u4f46\u662f\u5176\u503c\u5374\u4e0d\u662f0.1\u3002\u820d\u5165\u8bef\u5dee\u53ef\u80fd\u4f1a\u5f97\u5230\u4e00\u4e2a\u7a0d\u5927\u4e00\u70b9\u6216\u7a0d\u5c0f\u4e00\u70b9\u7684\u503c\uff0c\u8fd9\u53d6\u51b3\u4e8e\u622a\u65ad\u53d1\u751f\u5728\u54ea\u91cc\u3002 \u820d\u5165\u8bef\u5dee\u4f1a\u5e26\u6765\u975e\u9884\u671f\u7684\u7ed3\u679c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // for std::setprecision() #include int main () { std :: cout << std :: setprecision ( 17 ); double d1 { 1.0 }; std :: cout << d1 << '\\n' ; double d2 { 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 }; // should equal 1.0 std :: cout << d2 << '\\n' ; return 0 ; } 1 2 1 0.99999999999999989 \u5c3d\u7ba1\u6211\u4eec\u671f\u671b d1 \u7684\u503c\u5e94\u8be5\u7b49\u4e8e d2 \uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u6211\u4eec\u4e5f\u770b\u5230\u4e86\u5b83\u4eec\u5e76\u4e0d\u76f8\u7b49\u3002\u5982\u679c\u6211\u4eec\u5728\u7a0b\u5e8f\u4e2d\u5bf9 d1 \u548c d2 \u8fdb\u884c\u6bd4\u8f83\uff0c\u90a3\u4e48\u7ed3\u679c\u53ef\u80fd\u5c31\u4f1a\u51fa\u4eba\u610f\u6599\u3002\u56e0\u4e3a\u6d6e\u70b9\u6570\u7684\u8fd9\u79cd\u4e0d\u7cbe\u786e\u7684\u7279\u6027\uff0c\u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\u901a\u5e38\u4f1a\u4ea7\u751f\u95ee\u9898\u2014\u2014\u6211\u4eec\u4f1a\u5728 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 \u4e2d\u8fdb\u884c\u8be6\u7ec6\u5730\u8ba8\u8bba\u3002 \u5173\u4e8e\u820d\u5165\u8bef\u5dee\u7684\u6700\u540e\u4e00\u70b9\u53ee\u5631\uff1a\u6570\u5b66\u8fd0\u7b97 (\u4f8b\u5982\u52a0\u6cd5\u548c\u4e58\u6cd5) \u4f1a\u5bfc\u81f4\u820d\u5165\u8bef\u5dee\u7684\u79ef\u7d2f\u3002\u6240\u4ee5\u5373\u4f7f 0.1 \u53ea\u5728\u7b2c 17\u4f4d\u6709\u6548\u6570\u5b57\u65f6\u5b58\u5728\u8bef\u5dee\uff0c\u5f53\u6211\u4eec\u5c06\u662f\u4e2a0.1\u76f8\u52a0\u65f6\uff0c\u820d\u5165\u8bef\u5dee\u5c31\u51fa\u73b0\u5728\u4e86\u7b2c16\u4f4d\u3002\u4e0d\u65ad\u5730\u8ba1\u7b97\u4f1a\u5bfc\u81f4\u8fd9\u4e9b\u8bef\u5dee\u88ab\u4e0d\u65ad\u5730\u653e\u5927\u3002 \u5173\u952e\u4fe1\u606f \u5f53\u6570\u636e\u4e0d\u80fd\u88ab\u7cbe\u786e\u7684\u5b58\u50a8\u65f6\uff0c\u4f1a\u51fa\u73b0\u820d\u5165\u8bef\u5dee\u3002\u5373\u4f7f\u662f\u975e\u5e38\u7b80\u5355\u7684\u6570\u5b57\u4e5f\u5b58\u5728\u8be5\u95ee\u9898\uff08\u4f8b\u5982 0.1\uff09\u3002\u56e0\u6b64\uff0c\u820d\u5165\u8bef\u5dee\u603b\u662f\u65e0\u65f6\u65e0\u523b\u5730\u53d1\u751f\u3002\u6444\u5165\u8bef\u5dee\u5e76\u4e0d\u662f\u5f02\u5e38\uff0c\u5b83\u662f\u89c4\u5219\u7684\u4e00\u90e8\u5206\u3002\u6c38\u8fdc\u4e0d\u8981\u5047\u8bbe\u4f60\u7684\u6d6e\u70b9\u6570\u662f\u7cbe\u786e\u7684\u3002 \u8fd9\u6761\u89c4\u5219\u7684\u7ed3\u679c\u5c31\u662f\u5728\u4f7f\u7528\u6d6e\u70b9\u6570\u5904\u7406\u91d1\u878d\u6216\u8d27\u5e01\u6570\u636e\u65f6\uff0c\u4e00\u5b9a\u8981\u683c\u5916\u8b66\u60d5\u3002","title":"\u820d\u5165\u8bef\u5dee\u7684\u5b58\u5728\u589e\u52a0\u4e86\u6d6e\u70b9\u6570\u6bd4\u8f83\u7684\u96be\u5ea6"},{"location":"4-8-Floating-point-numbers/#nan-inf","text":"\u8fd8\u6709\u4e24\u7c7b\u7279\u6b8a\u7684\u6d6e\u70b9\u6570\u3002\u4e00\u4e2a\u662f Inf \uff0c\u8868\u793a\u65e0\u7a77\u3002 Inf \u53ef\u4ee5\u662f\u6b63\u4e5f\u53ef\u4ee5\u662f\u8d1f\u3002\u7b2c\u4e8c\u4e2a\u662f NaN \uff0c\u8868\u793a\u201d\u975e\u6570\u5b57\u201c\u3002 NaN \u7684\u79cd\u7c7b\u6709\u5f88\u591a\uff08\u6211\u4eec\u4e0d\u4f1a\u5728\u6b64\u8ba8\u8bba\uff09\u3002 NaN \u548c Inf \u53ea\u6709\u5728\u7f16\u8bd1\u5668\u4f7f\u7528\u7279\u5b9a\u683c\u5f0f(IEEE 754) \u7684\u6d6e\u70b9\u6570\u65f6\u624d\u53ef\u4ee5\u4f7f\u7528\u3002\u5f53\u4f7f\u7528\u5176\u4ed6\u683c\u5f0f\u7684\u65f6\u5019\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u5c31\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e0b\u9762\u7684\u4f8b\u7a0b\u5c55\u793a\u4e86\u4e0a\u8ff0\u4e09\u79cd\u6d6e\u70b9\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { double zero { 0.0 }; double posinf { 5.0 / zero }; // \u6b63\u65e0\u7a77 std :: cout << posinf << '\\n' ; double neginf { -5.0 / zero }; // \u8d1f\u65e0\u7a77 std :: cout << neginf << '\\n' ; double nan { zero / zero }; // \u975e\u6570\u5b57 (\u6570\u5b66\u4e0a\u65e0\u6548) std :: cout << nan << '\\n' ; return 0 ; } \u5728 Windows \u4e0a\u7684 Visual Studio 2008 \u8fd0\u884c\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 1.#INF -1.#INF 1.#IND INF \u8868\u793a\u65e0\u7a77\uff0c\u800c IND \u8868\u793a\u4e0d\u786e\u5b9a\uff08indeterminate\uff09\u3002\u6ce8\u610f\uff0c Inf \u548c NaN \u7684\u6253\u5370\u7ed3\u679c\u548c\u5e73\u53f0\u662f\u76f8\u5173\u7684\uff0c\u5728\u4f60\u7684\u7535\u8111\u4e0a\u7ed3\u679c\u53ef\u80fd\u6709\u6240\u4e0d\u540c\u3002 \u6700\u4f73\u5b9e\u8df5 \u8981\u5f7b\u5e95\u907f\u514d\u96640\uff0c\u5373\u4f7f\u4f60\u7684\u7684\u8ba1\u7b97\u673a\u652f\u6301\u8fd9\u79cd\u7528\u6cd5\u3002","title":"NaN \u548c Inf"},{"location":"4-8-Floating-point-numbers/#_5","text":"\u603b\u7ed3\u4e00\u4e0b\uff0c\u5173\u4e8e\u6d6e\u70b9\u6570\u4f60\u9700\u8981\u8bb0\u4f4f\u4e0b\u9762\u4e24\u4ef6\u4e8b\uff1a \u6d6e\u70b9\u6570\u5728\u8868\u793a\u5f88\u5927\u6216\u5f88\u5c0f\u7684\u6570\u65f6\u975e\u5e38\u6709\u7528\uff0c\u5f53\u7136\u4e5f\u9002\u5408\u4fdd\u5b58\u6709\u5c0f\u6570\u90e8\u5206\u7684\u6570\u3002 \u6d6e\u70b9\u6570\u901a\u5e38\u4f1a\u6709\u5fae\u5c0f\u7684\u820d\u5165\u8bef\u5dee\u95ee\u9898\uff0c\u5373\u4fbf\u4e00\u4e2a\u6570\u7684\u6709\u6548\u6570\u5b57\u6bd4\u7cbe\u5ea6\u80fd\u591f\u8868\u793a\u7684\u6709\u6548\u6570\u5b57\u5c11\uff0c\u4e5f\u5b58\u5728\u8be5\u95ee\u9898\u3002\u5f88\u591a\u65f6\u5019\u8fd9\u4e9b\u95ee\u9898\u4e0d\u4f1a\u88ab\u6211\u4eec\u6ce8\u610f\u5230\uff0c\u56e0\u4e3a\u6444\u5165\u8bef\u5dee\u5f88\u5c0f\uff0c\u800c\u4e14\u8f93\u51fa\u7ed3\u679c\u4f1a\u88ab\u622a\u65ad\uff0c\u4f46\u662f\uff0c\u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\u65f6\uff0c\u820d\u5165\u8bef\u5dee\u7684\u5b58\u5728\u53ef\u80fd\u4f1a\u5e26\u6765\u610f\u5916\u7684\u7ed3\u679c\u3002 \u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u65f6\u4e5f\u53ef\u80fd\u9020\u6210\u820d\u5165\u8bef\u5dee\u7684\u4e0d\u65ad\u589e\u5927\u3002","title":"\u7ed3\u8bba"},{"location":"4-9-Boolean-values/","tags":["data type","bool"],"text":"Key Takeaway \u4e0d\u80fd\u7528\u62ec\u53f7\u521d\u59cb\u5316\u548c\u4e00\u4e2a\u6574\u578b\u6570\u6765\u521d\u59cb\u5316\u5e03\u5c14\u53d8\u91cf\uff0c\u5176\u4ed6\u60c5\u51b5\u662f\u53ef\u4ee5\u8f6c\u6362\u7684 std::cin \u4e0d\u80fd\u63a5\u6536 true \u6216\u8005 false \uff0c\u9664\u975e\u4f7f\u7528 std::cout << std::boolalpha; \u5728\u73b0\u5b9e\u4e2d\uff0c\u6709\u4e9b\u95ee\u9898\u7684\u7b54\u6848\u53ea\u6709\u201d\u662f\u201c\u6216\u201d\u4e0d\u662f\u201c\u4e24\u79cd\u3002\u201d\u82f9\u679c\u662f\u6c34\u679c\u5417\uff1f\u201c\uff0c\u662f\u7684\u3002\u201d\u4f60\u7231\u5403\u7b0b\u5417\uff1f\u201c\u4e0d\u3002 \u73b0\u5728\uff0c\u6211\u4eec\u8003\u8651\u4e00\u4e2a\u7c7b\u4f3c\u7684\u8bed\u53e5\uff0c\u5b83\u53ea\u80fd\u7528 true \u6216 false \u6765\u56de\u7b54\uff1a\u201d\u82f9\u679c\u662f\u6c34\u679c\u201c\uff0c true \u3002\u201d\u6211\u559c\u6b22\u5403\u7b0b\u201c\uff0c false \u3002 \u50cf\u8fd9\u79cd\u53ea\u6709\u4e24\u79cd\u53ef\u80fd\u7b54\u6848\u7684\u8bed\u53e5\uff08yes/true \u6216 no/false\uff09\u5f88\u5e38\u89c1\uff0c\u5f88\u591a\u7f16\u7a0b\u8bed\u8a00\u90fd\u6709\u7528\u4e8e\u5904\u7406\u6b64\u7c7b\u8f93\u51fa\u7ed3\u679c\u7684\u7279\u6b8a\u7c7b\u578b\u3002\u5b83\u4eec\u79f0\u4e3a\u5e03\u5c14\u7c7b\u578b\uff08\u6ce8\u610f\uff1aBoolean\u5728\u82f1\u8bed\u4e2d\u662f\u5927\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684\uff0c\u56e0\u4e3a\u5b83\u7684\u53d1\u660e\u4eba\u662f\u4e54\u6cbb\u5e03\u5c14\uff08George Boole\uff09\uff09\u3002 \u5e03\u5c14\u53d8\u91cf \u5e03\u5c14\u53d8\u91cf\u53ea\u6709\u4e24\u79cd\u53ef\u80fd\u7684\u503c\uff1a true \u548c false \u3002 \u58f0\u660e\u5e03\u5c14\u7c7b\u578b\u7684\u53d8\u91cf\u9700\u8981\u4f7f\u7528\u5173\u952e\u5b57 bool \u3002 1 bool b ; \u521d\u59cb\u5316\u5e03\u5c14\u7c7b\u578b\u7684\u53d8\u91cf\u6216\u4e3a\u5176\u8d4b\u503c\uff0c\u9700\u8981\u4f7f\u7528 true \u6216 false \u3002 1 2 3 4 bool b1 { true }; bool b2 { false }; b1 = false ; bool b3 {}; // \u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a false \u548c\u4e00\u5143\u8d1f\u53f7\u8fd0\u7b97\u7b26( - )\u53ef\u4ee5\u88ab\u7528\u6765\u5c06\u4e00\u4e2a\u6b63\u6570\u53d8\u6210\u8d1f\u6570\u4e00\u6837\uff0c\u4f7f\u7528\u903b\u8f91 \u975e \u8fd0\u7b97\u7b26( ! )\u53ef\u4ee5\u5c06\u5e03\u5c14\u503c true \u53d8\u6210 false \u6216\u8005\u628a false \u53d8\u6210 true \uff1a 1 2 bool b1 { ! true }; // b1 \u88ab\u521d\u59cb\u5316\u4e3a false bool b2 { ! false }; // b2 \u88ab\u521d\u59cb\u5316\u4e3a true \u5e03\u5c14\u503c\u901a\u5e38\u5e76\u4e0d\u4f1a\u4ee5 \u201ctrue\u201d \u6216 \u201cfalse\u201d\u6765\u5b58\u50a8\u3002\u5b9e\u9645\u4e0a\u5b83\u662f\u4ee5\u6574\u578b\u6765\u5b58\u50a8\u7684\uff1a true \u4f1a\u53d8\u62101\uff0c false \u4f1a\u53d8\u62100\u3002\u7c7b\u4f3c\u5730\uff0c\u5f53\u5e03\u5c14\u7c7b\u578b\u6c42\u503c\u65f6\uff0c\u6c42\u503c\u7684\u5b9e\u9645\u7ed3\u679c\u4e5f\u5e76\u4e0d\u662f\u201ctrue\u201d \u6216 \u201cfalse\u201d\u3002\u901a\u5e38\u5b83\u4eec\u4f1a\u6c42\u503c\u6210_0_ (false) \u6216 1 (true)\u3002\u56e0\u4e3a\u5e03\u5c14\u7c7b\u578b\u5b9e\u9645\u4e0a\u662f\u4ee5\u6574\u578b\u5b58\u50a8\u7684\uff0c\u56e0\u6b64\u5b83\u5c5e\u4e8e\u4e00\u79cd\u6574\u5f62\u7684\u7c7b\u578b(integral type)\u3002 \u6253\u5370\u5e03\u5c14\u53d8\u91cf \u5f53\u4f7f\u7528 std::cout \u6253\u5370\u5e03\u5c14\u53d8\u91cf\u65f6\uff0c std::cout \u4f1a\u5c06 false \u6253\u5370\u6210 0\uff0c\u800c\u628a true \u6253\u5370\u62101\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { std :: cout << true << '\\n' ; // true evaluates to 1 std :: cout << ! true << '\\n' ; // !true evaluates to 0 bool b { false }; std :: cout << b << '\\n' ; // b is false, which evaluates to 0 std :: cout << ! b << '\\n' ; // !b is true, which evaluates to 1 return 0 ; } Outputs: 1 2 3 4 1 0 0 1 \u5982\u679c\u4f60\u5e0c\u671b std::cout \u6253\u5370 \u201ctrue\u201d \u6216 \u201cfalse\u201d \u800c\u4e0d\u662f 0 \u6216 1\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 std::boolalpha \u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { std :: cout << true << '\\n' ; std :: cout << false << '\\n' ; std :: cout << std :: boolalpha ; // print bools as true or false std :: cout << true << '\\n' ; std :: cout << false << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 4 1 0 true false \u4f60\u53ef\u4ee5\u4f7f\u7528 std::noboolalpha \u6765\u5173\u95ed\u6b64\u529f\u80fd\u3002 \u6574\u578b\u8f6c\u6362\u4e3a\u5e03\u5c14\u7c7b\u578b \u4f60\u4e0d\u80fd\u7528\u62ec\u53f7\u521d\u59cb\u5316\u548c\u4e00\u4e2a\u6574\u578b\u6570\u6765\u521d\u59cb\u5316\u5e03\u5c14\u53d8\u91cf\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { bool b { 4 }; // \u9519\u8bef\uff1a: \u4e0d\u5141\u8bb8\u7f29\u7a84\u8f6c\u6362 std :: cout << b ; return 0 ; } (\u6ce8\u610f\uff1a\u6709\u4e9b\u7248\u672c\u7684g++\u5e76\u4e0d\u4f1a\u6709\u4e0a\u8ff0\u9650\u5236\uff09 \u4e0d\u8fc7\uff0c\u5728\u5176\u4ed6\u573a\u666f\u4e0b\u6574\u578b\u662f\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\u7684\u3002\u6574\u578b\u503c0\u4f1a\u88ab\u8f6c\u6362\u4e3a false \uff0c\u800c\u5176\u4ed6\u6574\u578b\u4f1a\u88ab\u8f6c\u6362\u4e3a true \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << std :: boolalpha ; // print bools as true or false bool b1 = 4 ; // \u62f7\u8d1d\u521d\u59cb\u5316\u5141\u8bb8\u9690\u5f0f\u8f6c\u6362 std :: cout << b1 << '\\n' ; bool b2 = 0 ; // \u62f7\u8d1d\u521d\u59cb\u5316\u5141\u8bb8\u9690\u5f0f\u8f6c\u6362 std :: cout << b2 << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 true false \u6ce8\u610f\uff1a bool b1 = 4; \u53ef\u4ee5\u80fd\u4f1a\u4ea7\u751f\u8b66\u544a\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f60\u9700\u8981\u5173\u95ed\u7f16\u8bd1\u7684\u201d\u5c06\u544a\u8b66\u5f53\u505a\u9519\u8bef\u5904\u7406\u201c\u529f\u80fd\uff0c\u8fd9\u6837\u4f60\u731c\u5462\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\u3002 \u8f93\u5165\u5e03\u5c14\u503c \u4f7f\u7528 std::cin \u6765\u8f93\u5165\u5e03\u5c14\u7c7b\u578b\u5e38\u5e38\u4f1a\u8ba9\u65b0\u624b\u7a0b\u5e8f\u5458\u6293\u72c2\u3002 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { bool b {}; // \u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a false std :: cout << \"Enter a boolean value: \" ; std :: cin >> b ; std :: cout << \"You entered: \" << b << '\\n' ; return 0 ; } 1 2 Enter a Boolean value: true You entered: 0 \u4ec0\u4e48\uff01\u600e\u4e48\u4f1a\u8fd9\u6837\uff1f \u5b9e\u9645\u4e0a\uff0c std::cin \u53ea\u80fd\u591f\u63a5\u6536\u4e24\u79cd\u5e03\u5c14\u7c7b\u578b\u8f93\u5165\uff1a0\u548c1\uff08\u800c\u4e0d\u662f true \u6216 false \uff09\u3002\u4efb\u4f55\u5176\u4ed6\u7684\u8f93\u5165\u90fd\u4f1a\u5bfc\u81f4 std::cin \u51fa\u9519\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u8f93\u5165\u4e86true\uff0c\u56e0\u6b64\u5b9e\u9645\u4e0a std::cin \u51fa\u9519\u4e86\uff0c\u8f93\u5165\u51fa\u9519\u540e\uff0c\u63a5\u53d7\u8f93\u5165\u7684\u53d8\u91cf\u4f1a\u88ab\u6e05\u96f6\uff0c\u56e0\u6b64 b \u5c31\u88ab\u8d4b\u503c\u4e3a false \u3002\u56e0\u6b64\uff0c\u5f53 std::cout \u6253\u5370 b \u7684\u65f6\u5019\uff0c\u4f1a\u6253\u53700\u3002 \u4e3a\u4e86\u8ba9 std::cin \u80fd\u591f\u63a5\u53d7 \u201cfalse\u201d \u548c \u201ctrue\u201d \u4f5c\u4e3a\u8f93\u5165\uff0c std::boolalpha \u5fc5\u987b\u88ab\u6253\u5f00\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { bool b {}; std :: cout << \"Enter a boolean value: \" ; // \u5141\u8bb8\u7528\u6237\u8f93\u5165 'true' \u6216 'false' \u4f5c\u4e3a\u5e03\u5c14\u503c std :: cin >> std :: boolalpha ; std :: cin >> b ; std :: cout << \"You entered: \" << b << '\\n' ; return 0 ; } \u4e0d\u8fc7\uff0c\u5f53 std::boolalpha \u6253\u5361\u540e\uff0c\u201c0\u201d \u548c \u201c1\u201d \u5c31\u4e0d\u4f1a\u518d\u88ab\u770b\u51fa\u5e03\u5c14\u7c7b\u578b\u4e86\u3002 \u5e03\u5c14\u7c7b\u578b\u7684\u8fd4\u56de\u503c \u5e03\u5c14\u503c\u5e38\u5907\u7528\u4f5c\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u7528\u6765\u68c0\u67e5\u67d0\u4ef6\u4e8b(\u7269)\u662f\u5426\u4e3atrue\u3002\u8fd9\u79cd\u51fd\u6570\u547d\u540d\u65f6\u901a\u5e38\u4ee5 is \u5f00\u5934\uff08\u4f8b\u5982 isEqual \uff09\u6216 has \uff08\u4f8b\u5982 hasCommonDivisor \uff09\u3002 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u548c\u4e0a\u9762\u6709\u4e9b\u7c7b\u4f3c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // \u5982\u679c x \u548c y \u76f8\u7b49\u5219\u8fd4\u56de true \u5426\u5219\u8fd4\u56de false bool isEqual ( int x , int y ) { return ( x == y ); // \u5982\u679c x \u548c y \u76f8\u7b49\uff0coperator== \u5219\u8fd4\u56de true \u5426\u5219\u8fd4\u56de false } int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another integer: \" ; int y {}; std :: cin >> y ; std :: cout << std :: boolalpha ; // print bools as true or false std :: cout << x << \" and \" << y << \" are equal? \" ; std :: cout << isEqual ( x , y ); // will return true or false return 0 ; } \u4e24\u6b21\u8fd0\u884c\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 Enter an integer: 5 Enter another integer: 5 5 and 5 are equal? true 1 2 3 Enter an integer: 6 Enter another integer: 4 6 and 4 are equal? false \u539f\u7406\u662f\u4ec0\u4e48\u5462\uff1f\u9996\u5148\u6211\u4eec\u4ece\u8f93\u5165\u83b7\u53d6 x \u548c y \u7684\u503c\u3002\u63a5\u4e0b\u6765\uff0c\u8868\u8fbe\u5f0f isEqual(x, y) \u4f1a\u8fdb\u884c\u6c42\u503c\u3002\u5728\u7b2c\u4e00\u6b21\u8fd0\u884c\u65f6\uff0c\u6267\u884c\u7684\u51fd\u6570\u76f8\u5f53\u4e8e isEqual(5, 5) \u3002\u5728\u51fd\u6570\u4e2d\uff0c 5 == 5 \u88ab\u6c42\u503c\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f true \u3002\u968f\u540e true \u88ab\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u5e76\u901a\u8fc7 std::cout \u88ab\u6253\u5370\u51fa\u6765\u3002\u5728\u7b2c\u4e8c\u6b21\u8fd0\u884c\u65f6\uff0c\u8c03\u7528\u7684\u51fd\u6570\u76f8\u5f53\u4e8e isEqual(6, 4) \uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u4e3a false \u3002 \u4f7f\u7528\u5e03\u5c14\u503c\u9700\u8981\u719f\u6089\u4e00\u4e0b\uff0c\u4e0d\u8fc7\u5f53\u4f60\u4e60\u60ef\u4e4b\u540e\uff0c\u4f1a\u975e\u5e38\u559c\u6b22\u5b83\u8868\u73b0\u51fa\u6765\u7684\u7b80\u6d01\uff01\u5e03\u5c14\u503c\u4e5f\u662f\u7f16\u7a0b\u4e2d\u975e\u5e38\u5927\u7684\u4e00\u90e8\u5206\u2014\u2014\u4f60\u4f7f\u7528\u5e03\u5c14\u503c\u7684\u60c5\u51b5\u6bd4\u5176\u4ed6\u57fa\u672c\u7c7b\u578b\u52a0\u8d77\u6765\u90fd\u591a\u3002 \u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\u4e2d\u7ee7\u7eed\u8ba8\u8bba\u5e03\u5c14\u503c\u3002","title":"4.9 - \u5e03\u5c14\u503c"},{"location":"4-9-Boolean-values/#_1","text":"\u5e03\u5c14\u53d8\u91cf\u53ea\u6709\u4e24\u79cd\u53ef\u80fd\u7684\u503c\uff1a true \u548c false \u3002 \u58f0\u660e\u5e03\u5c14\u7c7b\u578b\u7684\u53d8\u91cf\u9700\u8981\u4f7f\u7528\u5173\u952e\u5b57 bool \u3002 1 bool b ; \u521d\u59cb\u5316\u5e03\u5c14\u7c7b\u578b\u7684\u53d8\u91cf\u6216\u4e3a\u5176\u8d4b\u503c\uff0c\u9700\u8981\u4f7f\u7528 true \u6216 false \u3002 1 2 3 4 bool b1 { true }; bool b2 { false }; b1 = false ; bool b3 {}; // \u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a false \u548c\u4e00\u5143\u8d1f\u53f7\u8fd0\u7b97\u7b26( - )\u53ef\u4ee5\u88ab\u7528\u6765\u5c06\u4e00\u4e2a\u6b63\u6570\u53d8\u6210\u8d1f\u6570\u4e00\u6837\uff0c\u4f7f\u7528\u903b\u8f91 \u975e \u8fd0\u7b97\u7b26( ! )\u53ef\u4ee5\u5c06\u5e03\u5c14\u503c true \u53d8\u6210 false \u6216\u8005\u628a false \u53d8\u6210 true \uff1a 1 2 bool b1 { ! true }; // b1 \u88ab\u521d\u59cb\u5316\u4e3a false bool b2 { ! false }; // b2 \u88ab\u521d\u59cb\u5316\u4e3a true \u5e03\u5c14\u503c\u901a\u5e38\u5e76\u4e0d\u4f1a\u4ee5 \u201ctrue\u201d \u6216 \u201cfalse\u201d\u6765\u5b58\u50a8\u3002\u5b9e\u9645\u4e0a\u5b83\u662f\u4ee5\u6574\u578b\u6765\u5b58\u50a8\u7684\uff1a true \u4f1a\u53d8\u62101\uff0c false \u4f1a\u53d8\u62100\u3002\u7c7b\u4f3c\u5730\uff0c\u5f53\u5e03\u5c14\u7c7b\u578b\u6c42\u503c\u65f6\uff0c\u6c42\u503c\u7684\u5b9e\u9645\u7ed3\u679c\u4e5f\u5e76\u4e0d\u662f\u201ctrue\u201d \u6216 \u201cfalse\u201d\u3002\u901a\u5e38\u5b83\u4eec\u4f1a\u6c42\u503c\u6210_0_ (false) \u6216 1 (true)\u3002\u56e0\u4e3a\u5e03\u5c14\u7c7b\u578b\u5b9e\u9645\u4e0a\u662f\u4ee5\u6574\u578b\u5b58\u50a8\u7684\uff0c\u56e0\u6b64\u5b83\u5c5e\u4e8e\u4e00\u79cd\u6574\u5f62\u7684\u7c7b\u578b(integral type)\u3002","title":"\u5e03\u5c14\u53d8\u91cf"},{"location":"4-9-Boolean-values/#_2","text":"\u5f53\u4f7f\u7528 std::cout \u6253\u5370\u5e03\u5c14\u53d8\u91cf\u65f6\uff0c std::cout \u4f1a\u5c06 false \u6253\u5370\u6210 0\uff0c\u800c\u628a true \u6253\u5370\u62101\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { std :: cout << true << '\\n' ; // true evaluates to 1 std :: cout << ! true << '\\n' ; // !true evaluates to 0 bool b { false }; std :: cout << b << '\\n' ; // b is false, which evaluates to 0 std :: cout << ! b << '\\n' ; // !b is true, which evaluates to 1 return 0 ; } Outputs: 1 2 3 4 1 0 0 1 \u5982\u679c\u4f60\u5e0c\u671b std::cout \u6253\u5370 \u201ctrue\u201d \u6216 \u201cfalse\u201d \u800c\u4e0d\u662f 0 \u6216 1\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 std::boolalpha \u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { std :: cout << true << '\\n' ; std :: cout << false << '\\n' ; std :: cout << std :: boolalpha ; // print bools as true or false std :: cout << true << '\\n' ; std :: cout << false << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 4 1 0 true false \u4f60\u53ef\u4ee5\u4f7f\u7528 std::noboolalpha \u6765\u5173\u95ed\u6b64\u529f\u80fd\u3002","title":"\u6253\u5370\u5e03\u5c14\u53d8\u91cf"},{"location":"4-9-Boolean-values/#_3","text":"\u4f60\u4e0d\u80fd\u7528\u62ec\u53f7\u521d\u59cb\u5316\u548c\u4e00\u4e2a\u6574\u578b\u6570\u6765\u521d\u59cb\u5316\u5e03\u5c14\u53d8\u91cf\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { bool b { 4 }; // \u9519\u8bef\uff1a: \u4e0d\u5141\u8bb8\u7f29\u7a84\u8f6c\u6362 std :: cout << b ; return 0 ; } (\u6ce8\u610f\uff1a\u6709\u4e9b\u7248\u672c\u7684g++\u5e76\u4e0d\u4f1a\u6709\u4e0a\u8ff0\u9650\u5236\uff09 \u4e0d\u8fc7\uff0c\u5728\u5176\u4ed6\u573a\u666f\u4e0b\u6574\u578b\u662f\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\u7684\u3002\u6574\u578b\u503c0\u4f1a\u88ab\u8f6c\u6362\u4e3a false \uff0c\u800c\u5176\u4ed6\u6574\u578b\u4f1a\u88ab\u8f6c\u6362\u4e3a true \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << std :: boolalpha ; // print bools as true or false bool b1 = 4 ; // \u62f7\u8d1d\u521d\u59cb\u5316\u5141\u8bb8\u9690\u5f0f\u8f6c\u6362 std :: cout << b1 << '\\n' ; bool b2 = 0 ; // \u62f7\u8d1d\u521d\u59cb\u5316\u5141\u8bb8\u9690\u5f0f\u8f6c\u6362 std :: cout << b2 << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 true false \u6ce8\u610f\uff1a bool b1 = 4; \u53ef\u4ee5\u80fd\u4f1a\u4ea7\u751f\u8b66\u544a\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f60\u9700\u8981\u5173\u95ed\u7f16\u8bd1\u7684\u201d\u5c06\u544a\u8b66\u5f53\u505a\u9519\u8bef\u5904\u7406\u201c\u529f\u80fd\uff0c\u8fd9\u6837\u4f60\u731c\u5462\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\u3002","title":"\u6574\u578b\u8f6c\u6362\u4e3a\u5e03\u5c14\u7c7b\u578b"},{"location":"4-9-Boolean-values/#_4","text":"\u4f7f\u7528 std::cin \u6765\u8f93\u5165\u5e03\u5c14\u7c7b\u578b\u5e38\u5e38\u4f1a\u8ba9\u65b0\u624b\u7a0b\u5e8f\u5458\u6293\u72c2\u3002 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { bool b {}; // \u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a false std :: cout << \"Enter a boolean value: \" ; std :: cin >> b ; std :: cout << \"You entered: \" << b << '\\n' ; return 0 ; } 1 2 Enter a Boolean value: true You entered: 0 \u4ec0\u4e48\uff01\u600e\u4e48\u4f1a\u8fd9\u6837\uff1f \u5b9e\u9645\u4e0a\uff0c std::cin \u53ea\u80fd\u591f\u63a5\u6536\u4e24\u79cd\u5e03\u5c14\u7c7b\u578b\u8f93\u5165\uff1a0\u548c1\uff08\u800c\u4e0d\u662f true \u6216 false \uff09\u3002\u4efb\u4f55\u5176\u4ed6\u7684\u8f93\u5165\u90fd\u4f1a\u5bfc\u81f4 std::cin \u51fa\u9519\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u8f93\u5165\u4e86true\uff0c\u56e0\u6b64\u5b9e\u9645\u4e0a std::cin \u51fa\u9519\u4e86\uff0c\u8f93\u5165\u51fa\u9519\u540e\uff0c\u63a5\u53d7\u8f93\u5165\u7684\u53d8\u91cf\u4f1a\u88ab\u6e05\u96f6\uff0c\u56e0\u6b64 b \u5c31\u88ab\u8d4b\u503c\u4e3a false \u3002\u56e0\u6b64\uff0c\u5f53 std::cout \u6253\u5370 b \u7684\u65f6\u5019\uff0c\u4f1a\u6253\u53700\u3002 \u4e3a\u4e86\u8ba9 std::cin \u80fd\u591f\u63a5\u53d7 \u201cfalse\u201d \u548c \u201ctrue\u201d \u4f5c\u4e3a\u8f93\u5165\uff0c std::boolalpha \u5fc5\u987b\u88ab\u6253\u5f00\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { bool b {}; std :: cout << \"Enter a boolean value: \" ; // \u5141\u8bb8\u7528\u6237\u8f93\u5165 'true' \u6216 'false' \u4f5c\u4e3a\u5e03\u5c14\u503c std :: cin >> std :: boolalpha ; std :: cin >> b ; std :: cout << \"You entered: \" << b << '\\n' ; return 0 ; } \u4e0d\u8fc7\uff0c\u5f53 std::boolalpha \u6253\u5361\u540e\uff0c\u201c0\u201d \u548c \u201c1\u201d \u5c31\u4e0d\u4f1a\u518d\u88ab\u770b\u51fa\u5e03\u5c14\u7c7b\u578b\u4e86\u3002","title":"\u8f93\u5165\u5e03\u5c14\u503c"},{"location":"4-9-Boolean-values/#_5","text":"\u5e03\u5c14\u503c\u5e38\u5907\u7528\u4f5c\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u7528\u6765\u68c0\u67e5\u67d0\u4ef6\u4e8b(\u7269)\u662f\u5426\u4e3atrue\u3002\u8fd9\u79cd\u51fd\u6570\u547d\u540d\u65f6\u901a\u5e38\u4ee5 is \u5f00\u5934\uff08\u4f8b\u5982 isEqual \uff09\u6216 has \uff08\u4f8b\u5982 hasCommonDivisor \uff09\u3002 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u548c\u4e0a\u9762\u6709\u4e9b\u7c7b\u4f3c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // \u5982\u679c x \u548c y \u76f8\u7b49\u5219\u8fd4\u56de true \u5426\u5219\u8fd4\u56de false bool isEqual ( int x , int y ) { return ( x == y ); // \u5982\u679c x \u548c y \u76f8\u7b49\uff0coperator== \u5219\u8fd4\u56de true \u5426\u5219\u8fd4\u56de false } int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another integer: \" ; int y {}; std :: cin >> y ; std :: cout << std :: boolalpha ; // print bools as true or false std :: cout << x << \" and \" << y << \" are equal? \" ; std :: cout << isEqual ( x , y ); // will return true or false return 0 ; } \u4e24\u6b21\u8fd0\u884c\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 Enter an integer: 5 Enter another integer: 5 5 and 5 are equal? true 1 2 3 Enter an integer: 6 Enter another integer: 4 6 and 4 are equal? false \u539f\u7406\u662f\u4ec0\u4e48\u5462\uff1f\u9996\u5148\u6211\u4eec\u4ece\u8f93\u5165\u83b7\u53d6 x \u548c y \u7684\u503c\u3002\u63a5\u4e0b\u6765\uff0c\u8868\u8fbe\u5f0f isEqual(x, y) \u4f1a\u8fdb\u884c\u6c42\u503c\u3002\u5728\u7b2c\u4e00\u6b21\u8fd0\u884c\u65f6\uff0c\u6267\u884c\u7684\u51fd\u6570\u76f8\u5f53\u4e8e isEqual(5, 5) \u3002\u5728\u51fd\u6570\u4e2d\uff0c 5 == 5 \u88ab\u6c42\u503c\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f true \u3002\u968f\u540e true \u88ab\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u5e76\u901a\u8fc7 std::cout \u88ab\u6253\u5370\u51fa\u6765\u3002\u5728\u7b2c\u4e8c\u6b21\u8fd0\u884c\u65f6\uff0c\u8c03\u7528\u7684\u51fd\u6570\u76f8\u5f53\u4e8e isEqual(6, 4) \uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u4e3a false \u3002 \u4f7f\u7528\u5e03\u5c14\u503c\u9700\u8981\u719f\u6089\u4e00\u4e0b\uff0c\u4e0d\u8fc7\u5f53\u4f60\u4e60\u60ef\u4e4b\u540e\uff0c\u4f1a\u975e\u5e38\u559c\u6b22\u5b83\u8868\u73b0\u51fa\u6765\u7684\u7b80\u6d01\uff01\u5e03\u5c14\u503c\u4e5f\u662f\u7f16\u7a0b\u4e2d\u975e\u5e38\u5927\u7684\u4e00\u90e8\u5206\u2014\u2014\u4f60\u4f7f\u7528\u5e03\u5c14\u503c\u7684\u60c5\u51b5\u6bd4\u5176\u4ed6\u57fa\u672c\u7c7b\u578b\u52a0\u8d77\u6765\u90fd\u591a\u3002 \u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\u4e2d\u7ee7\u7eed\u8ba8\u8bba\u5e03\u5c14\u503c\u3002","title":"\u5e03\u5c14\u7c7b\u578b\u7684\u8fd4\u56de\u503c"},{"location":"4-x-Chapter-4-summary-and-quiz/","tags":["summary"],"text":"\u7ae0\u8282\u56de\u987e 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb \u5185\u5b58\u7684\u6700\u5c0f\u5355\u5143\u79f0\u4e3a \u4f4d \u3002\u5185\u5b58\u53ef\u5bfb\u627e\u7684\u6700\u5c0f\u5355\u4f4d\u662f \u5b57\u8282 \uff0c\u73b0\u4ee3\u6807\u51c6\u4e00\u4e2a\u5b57\u8282\u7b49\u4e8e8\u4e2a\u4f4d\u3002 \u4e00\u79cd \u6570\u636e\u7c7b\u578b \u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u4f55\u5c06\u5185\u5b58\u4e2d\u7684\u6570\u636e\u89e3\u6790\u6210\u6709\u610f\u4e49\u7684\u503c\u3002 C++ \u652f\u6301\u591a\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6d6e\u70b9\u6570\u3001\u6574\u578b\u3001\u5e03\u5c14\u7c7b\u578b\u3001\u5b57\u7b26\u3001null\u6307\u9488\u548c void\u3002 4.2 - Void Void \u7528\u4e8e\u8868\u793a\u65e0\u7c7b\u578b\u3002\u5176\u6700\u4e3b\u8981\u7684\u4f5c\u7528\u662f\u8868\u660e\u51fd\u6570\u65e0\u8fd4\u56de\u503c\u3002 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 \u4e0d\u540c\u6570\u636e\u7c7b\u578b\u5360\u636e\u7684\u5185\u5b58\u5927\u5c0f\u4e5f\u4e0d\u540c\uff0c\u540c\u65f6\u4e5f\u4f1a\u6536\u5230\u673a\u5668\u4f53\u7cfb\u7ed3\u6784\u7684\u5f71\u54cd\u3002\u53c2\u8003 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 \u4e2d\u7684\u8868\u683c\u4e86\u89e3\u6bcf\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\u5360\u7528\u7684\u7a7a\u95f4\u3002 sizeof \u8fd0\u7b97\u7b26\u53ef\u4ee5\u8fd4\u56de\u67d0\u4e2a\u7c7b\u578b\u6240\u5360\u7528\u7684\u5b57\u8282\u6570\u3002 4.4 - \u6709\u7b26\u53f7\u6574\u578b \u6709\u7b26\u53f7\u6574\u578b \u7528\u4e8e\u4fdd\u5b58\u6b63\u6570\u548c\u8d1f\u6570\uff0c\u4e5f\u5305\u62ec0\u3002\u4e00\u4e2a\u7279\u5b9a\u6570\u636e\u7c7b\u578b\u80fd\u591f\u4fdd\u5b58\u7684\u503c\uff0c\u79f0\u4e3a\u8be5\u7c7b\u578b\u7684\u8303\u56f4\u3002\u5f53\u4f7f\u7528\u6574\u578b\u65f6\uff0c\u4e00\u5b9a\u8981\u6ce8\u610f\u6ea2\u51fa\u548c\u6574\u578b\u96640\u95ee\u9898\u3002 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 \u65e0\u7b26\u53f7\u6574\u578b \u53ea\u80fd\u4fdd\u5b58\u6b63\u6574\u6570\uff0c\u901a\u5e38\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u5b83\uff0c\u9664\u975e\u662f\u7528\u4e8e\u4f4d\u8fd0\u7b97\u3002 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b \u662f\u5177\u6709\u786e\u5b9a\u5927\u5c0f\u7684\u6574\u578b\uff0c\u4f46\u662f\u5e76\u4e0d\u662f\u6240\u6709\u7684\u4f53\u7cfb\u7ed3\u6784\u90fd\u652f\u6301\u3002\u901f\u5ea6\u4f18\u5148\u548c\u5927\u5c0f\u4f18\u5148\u7684\u6574\u578b\u662f\u4fdd\u8bc1\u67d0\u5176\u5177\u6709\u67d0\u5927\u5c0f\u524d\u63d0\u4e0b\u7684\u6700\u5feb\u7684\u6216\u6700\u5c0f\u7684\u6574\u578b\u3002 std::int8_t \u548c std::uint8_t \u4e00\u822c\u4e0d\u8981\u4f7f\u7528\uff0c\u5b83\u4eec\u7684\u884c\u4e3a\u66f4\u50cf\u5b57\u7b26\u800c\u4e0d\u662f\u6574\u578b\u3002 size_t \u662f\u4e00\u79cd\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u5b83\u7528\u4e8e\u8868\u793a\u5bf9\u8c61\u7684\u5927\u5c0f\u6216\u957f\u5ea6\u3002 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 \u79d1\u5b66\u8ba1\u6570\u6cd5\u662f\u4e00\u79cd\u8868\u793a\u5927\u6570\u7684\u7b80\u5355\u8bb0\u6cd5\u3002C++ \u652f\u6301\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a\u6d6e\u70b9\u6570\u3002\u79d1\u5b66\u8ba1\u6570\u6cd5\u4e2d\uff0ce\u524d\u9762\u7684\u90e8\u5206\u79f0\u4e3a\u6709\u6548\u6570\u5b57\u3002 4.8 - \u6d6e\u70b9\u6570 \u6d6e\u70b9\u6570 \u7c7b\u578b\u7528\u4e8e\u4fdd\u5b58\u5b9e\u6570\uff08\u5305\u62ec\u54ea\u4e9b\u6709\u5c0f\u6570\u90e8\u5206\u7684\u6570\uff09\u3002\u6570\u5b57\u7684\u7cbe\u5ea6\u6307\u7684\u662f\u5728\u4e0d\u4e22\u5931\u7cbe\u5ea6\u7684\u524d\u63d0\u4e0b\uff0c\u53ef\u4ee5\u8868\u793a\u591a\u5c11\u4e2a\u6709\u6548\u6570\u5b57\u3002 \u820d\u5165\u8bef\u5dee \u51fa\u73b0\u5728\u8fc7\u591a\u7684\u6709\u6548\u6570\u5b57\u88ab\u5b58\u653e\u5728\u65e0\u6cd5\u4fdd\u5b58\u8fd9\u4e48\u591a\u6709\u6548\u6570\u5b57\u7684\u6d6e\u70b9\u6570\u4e2d\u3002\u820d\u5165\u8bef\u5dee\u4e00\u81f4\u5b58\u5728\uff0c\u5373\u4f7f\u662f\u50cf 0.1 \u8fd9\u6837\u7b80\u5355\u7684\u6570\u3002\u56e0\u6b64\uff0c\u4f60\u4e0d\u80fd\u5c06\u6d6e\u70b9\u6570\u8fdb\u884c\u76f4\u63a5\u6bd4\u8f83\u3002 4.9 - \u5e03\u5c14\u503c \u5e03\u5c14 \u7c7b\u578b\u7528\u4e8e\u5b58\u653e true \u548c false \u3002 4.10 - if \u8bed\u53e5\u7b80\u4ecb If \u8bed\u53e5 4.10 - if \u8bed\u53e5\u7b80\u4ecb \u5141\u8bb8\u6211\u4eec\u6ee1\u8db3\u67d0\u79cd\u6761\u4ef6\u65f6\u624d\u6267\u884c\u4e00\u6761\u6216\u591a\u6761\u8bed\u53e5\u3002if \u8bed\u53e5\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u4f1a\u88ab\u89e3\u6790\u4e3a\u5e03\u5c14\u503c\u3002 4.11 - \u5b57\u7b26 \u5b57\u7b26 \u5b58\u653e\u7684\u503c\u4f1a\u88ab\u89e3\u6790\u4e3a ASCII \u5b57\u6bcd\u3002\u5f53\u4f7f\u7528\u5b57\u7b26\u65f6\uff0c\u5c0f\u5fc3\u4e0d\u8981\u5c06 ASCII \u7801\u503c\u548c\u6570\u5b57\u641e\u6df7\u3002\u5982\u679c\u9700\u8981\u628a\u5b57\u7b26\u5f53\u6574\u578b\u6253\u5370\u5904\u7406\uff0c\u9700\u8981\u4f7f\u7528 static_cast \u3002 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast \u5c16\u62ec\u53f7\u5728 C++ \u4e2d\u901a\u5e38\u7528\u6765\u8868\u793a\u53c2\u6570\u5316\u7c7b\u578b\u3002\u5728 static_cast \u4e2d\uff0c\u5c16\u62ec\u53f7\u7684\u5185\u5bb9\u6307\u5b9a\u4e86\u76ee\u6807\u7c7b\u578b\u3002(\u4f8b\u5982 static_cast(x) \u4f1a\u628a x \u8f6c\u6362\u4e3a int )\u3002 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \u5e38\u91cf\u6307\u7684\u662f\u503c\u4e0d\u4f1a\u6539\u53d8\u7684\u53d8\u91cf\u3002C++ \u652f\u6301\u4e24\u79cd\u5e38\u91cf\uff1a\u5b57\u9762\u91cf\u5e38\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \u7b26\u53f7\u5e38\u91cf \u6307\u7684\u662f\u7ed9\u4e0e\u5e38\u91cf\u7684\u540d\u5b57\u3002\u5e38\u91cf\u662f\u7b26\u53f7\u53d8\u91cf\u7684\u4e00\u79cd\uff0c\u4f7f\u7528\u66ff\u6362\u6587\u672c\u7684\u7c7b\u5bf9\u8c61\u5b8f\u4e5f\u662f\u7b26\u53f7\u5e38\u91cf\uff1b 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr \u5e38\u91cf\u8868\u8fbe\u5f0f \u662f\u4e00\u4e2a\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\u8868\u8fbe\u5f0f\u3002 \u7f16\u8bd1\u65f6\u5e38\u91cf \u662f\u5176\u503c\u5728\u7f16\u8bd1\u65f6\u5df2\u77e5\u7684\u5e38\u91cf\u3002 \u8fd0\u884c\u65f6\u5e38\u91cf \u662f\u5728\u8fd0\u884c\u65f6\u4e4b\u524d\u4e0d\u77e5\u9053\u5176\u521d\u59cb\u5316\u503c\u7684\u5e38\u91cf\u3002constexprvariable\u5fc5\u987b\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 4.15 - \u5b57\u9762\u91cf \u5b57\u9762\u91cf \u662f\u76f4\u63a5\u5d4c\u5165\u5230\u4ee3\u7801\u4e2d\u7684\u5e38\u91cf\u3002\u5b57\u9762\u91cf\u662f\u6709\u7c7b\u578b\u7684\uff0c\u800c\u5b57\u9762\u91cf\u540e\u7f00\u53ef\u4ee5\u7528\u6765\u4fee\u6539\u5b57\u9762\u91cf\u7684\u9ed8\u8ba4\u7c7b\u578b\u3002 \u9b54\u9b3c\u6570\u5b57\u6307\u7684\u662f\u542b\u4e49\u4e0d\u660e\u786e\u6216\u662f\u7a0d\u540e\u4f1a\u88ab\u4fee\u6539\u7684\u5b57\u9762\u91cf\uff08\u901a\u5e38\u662f\u6570\u5b57\uff09\uff0c\u4e0d\u8981\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u9b54\u672f\u6570\u5b57\uff0c\u5e94\u8be5\u7528\u7b26\u53f7\u5e38\u91cf\u4ee3\u66ff\u5b83\u3002 4.16 - \u6570\u503c\u7cfb\u7edf\uff08\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236\uff09 \u5728\u65e5\u5e38\u751f\u6d3b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5341\u8fdb\u5236\u6570\uff0c\u5b83\u670910\u4f4d\u6570\u5b57\u3002\u8ba1\u7b97\u673a\u4f7f\u7528\u4e8c\u8fdb\u5236\uff0c\u5b83\u53ea\u67092\u4f4d\u6570\u5b57\u3002C++\u8fd8\u652f\u6301\u516b\u8fdb\u5236(\u4ee58\u4e3a\u57fa\u6570)\u548c\u5341\u516d\u8fdb\u5236(\u4ee516\u4e3a\u57fa\u6570)\u3002\u8fd9\u4e9b\u90fd\u662f\u6570\u5b57\u7cfb\u7edf\u7684\u4f8b\u5b50\uff0c\u662f\u7528\u6765\u8868\u793a\u6570\u5b57\u7684\u7b26\u53f7(\u6570\u5b57)\u7684\u96c6\u5408\u3002 4.17 - std::string \u7b80\u4ecb 4.18 - std::string_view \u7b80\u4ecb \u5b57\u7b26\u4e32\u662f\u7528\u4e8e\u8868\u793a\u6587\u672c(\u5982\u540d\u79f0\u3001\u5355\u8bcd\u548c\u53e5\u5b50)\u7684\u987a\u5e8f\u5b57\u7b26\u7684\u96c6\u5408\u3002\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u603b\u662f\u653e\u5728\u53cc\u5f15\u53f7\u4e4b\u95f4\u3002C++\u4e2d\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u662fC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u5b83\u7684\u7c7b\u578b\u6bd4\u8f83\u5947\u602a\uff0c\u5f88\u96be\u5904\u7406\u3002 std::string \u63d0\u4f9b\u4e86\u7b80\u4fbf\u3001\u5b89\u5168\u7684\u5904\u7406\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u3002\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u603b\u662f\u653e\u5728\u53cc\u5f15\u53f7\u4e2d\u3002\u4f7f\u7528 std::string \u9700\u8981\u5305\u542b \u5934\u6587\u4ef6\uff1b std::string_view \u4e3a\u5df2\u7ecf\u5b58\u5728\u7684\u5b57\u7b26\u4e32\uff08C\u98ce\u683c\u5b57\u7b26\u4e32\u3001 std::string \u6216\u5b57\u7b26\u6570\u7ec4\uff09\u63d0\u4f9b\u4e86\u4e00\u79cd\u53ea\u8bfb\u7684\u8bbf\u95ee\u65b9\u5f0f\uff0c\u800c\u4e0d\u9700\u521b\u5efa\u62f7\u8d1d\u3002","title":"4.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u57fa\u672c\u6570\u636e\u7c7b\u578b"},{"location":"4-x-Chapter-4-summary-and-quiz/#_1","text":"4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb \u5185\u5b58\u7684\u6700\u5c0f\u5355\u5143\u79f0\u4e3a \u4f4d \u3002\u5185\u5b58\u53ef\u5bfb\u627e\u7684\u6700\u5c0f\u5355\u4f4d\u662f \u5b57\u8282 \uff0c\u73b0\u4ee3\u6807\u51c6\u4e00\u4e2a\u5b57\u8282\u7b49\u4e8e8\u4e2a\u4f4d\u3002 \u4e00\u79cd \u6570\u636e\u7c7b\u578b \u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u4f55\u5c06\u5185\u5b58\u4e2d\u7684\u6570\u636e\u89e3\u6790\u6210\u6709\u610f\u4e49\u7684\u503c\u3002 C++ \u652f\u6301\u591a\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6d6e\u70b9\u6570\u3001\u6574\u578b\u3001\u5e03\u5c14\u7c7b\u578b\u3001\u5b57\u7b26\u3001null\u6307\u9488\u548c void\u3002 4.2 - Void Void \u7528\u4e8e\u8868\u793a\u65e0\u7c7b\u578b\u3002\u5176\u6700\u4e3b\u8981\u7684\u4f5c\u7528\u662f\u8868\u660e\u51fd\u6570\u65e0\u8fd4\u56de\u503c\u3002 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 \u4e0d\u540c\u6570\u636e\u7c7b\u578b\u5360\u636e\u7684\u5185\u5b58\u5927\u5c0f\u4e5f\u4e0d\u540c\uff0c\u540c\u65f6\u4e5f\u4f1a\u6536\u5230\u673a\u5668\u4f53\u7cfb\u7ed3\u6784\u7684\u5f71\u54cd\u3002\u53c2\u8003 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 \u4e2d\u7684\u8868\u683c\u4e86\u89e3\u6bcf\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\u5360\u7528\u7684\u7a7a\u95f4\u3002 sizeof \u8fd0\u7b97\u7b26\u53ef\u4ee5\u8fd4\u56de\u67d0\u4e2a\u7c7b\u578b\u6240\u5360\u7528\u7684\u5b57\u8282\u6570\u3002 4.4 - \u6709\u7b26\u53f7\u6574\u578b \u6709\u7b26\u53f7\u6574\u578b \u7528\u4e8e\u4fdd\u5b58\u6b63\u6570\u548c\u8d1f\u6570\uff0c\u4e5f\u5305\u62ec0\u3002\u4e00\u4e2a\u7279\u5b9a\u6570\u636e\u7c7b\u578b\u80fd\u591f\u4fdd\u5b58\u7684\u503c\uff0c\u79f0\u4e3a\u8be5\u7c7b\u578b\u7684\u8303\u56f4\u3002\u5f53\u4f7f\u7528\u6574\u578b\u65f6\uff0c\u4e00\u5b9a\u8981\u6ce8\u610f\u6ea2\u51fa\u548c\u6574\u578b\u96640\u95ee\u9898\u3002 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 \u65e0\u7b26\u53f7\u6574\u578b \u53ea\u80fd\u4fdd\u5b58\u6b63\u6574\u6570\uff0c\u901a\u5e38\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u5b83\uff0c\u9664\u975e\u662f\u7528\u4e8e\u4f4d\u8fd0\u7b97\u3002 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b \u662f\u5177\u6709\u786e\u5b9a\u5927\u5c0f\u7684\u6574\u578b\uff0c\u4f46\u662f\u5e76\u4e0d\u662f\u6240\u6709\u7684\u4f53\u7cfb\u7ed3\u6784\u90fd\u652f\u6301\u3002\u901f\u5ea6\u4f18\u5148\u548c\u5927\u5c0f\u4f18\u5148\u7684\u6574\u578b\u662f\u4fdd\u8bc1\u67d0\u5176\u5177\u6709\u67d0\u5927\u5c0f\u524d\u63d0\u4e0b\u7684\u6700\u5feb\u7684\u6216\u6700\u5c0f\u7684\u6574\u578b\u3002 std::int8_t \u548c std::uint8_t \u4e00\u822c\u4e0d\u8981\u4f7f\u7528\uff0c\u5b83\u4eec\u7684\u884c\u4e3a\u66f4\u50cf\u5b57\u7b26\u800c\u4e0d\u662f\u6574\u578b\u3002 size_t \u662f\u4e00\u79cd\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u5b83\u7528\u4e8e\u8868\u793a\u5bf9\u8c61\u7684\u5927\u5c0f\u6216\u957f\u5ea6\u3002 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 \u79d1\u5b66\u8ba1\u6570\u6cd5\u662f\u4e00\u79cd\u8868\u793a\u5927\u6570\u7684\u7b80\u5355\u8bb0\u6cd5\u3002C++ \u652f\u6301\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a\u6d6e\u70b9\u6570\u3002\u79d1\u5b66\u8ba1\u6570\u6cd5\u4e2d\uff0ce\u524d\u9762\u7684\u90e8\u5206\u79f0\u4e3a\u6709\u6548\u6570\u5b57\u3002 4.8 - \u6d6e\u70b9\u6570 \u6d6e\u70b9\u6570 \u7c7b\u578b\u7528\u4e8e\u4fdd\u5b58\u5b9e\u6570\uff08\u5305\u62ec\u54ea\u4e9b\u6709\u5c0f\u6570\u90e8\u5206\u7684\u6570\uff09\u3002\u6570\u5b57\u7684\u7cbe\u5ea6\u6307\u7684\u662f\u5728\u4e0d\u4e22\u5931\u7cbe\u5ea6\u7684\u524d\u63d0\u4e0b\uff0c\u53ef\u4ee5\u8868\u793a\u591a\u5c11\u4e2a\u6709\u6548\u6570\u5b57\u3002 \u820d\u5165\u8bef\u5dee \u51fa\u73b0\u5728\u8fc7\u591a\u7684\u6709\u6548\u6570\u5b57\u88ab\u5b58\u653e\u5728\u65e0\u6cd5\u4fdd\u5b58\u8fd9\u4e48\u591a\u6709\u6548\u6570\u5b57\u7684\u6d6e\u70b9\u6570\u4e2d\u3002\u820d\u5165\u8bef\u5dee\u4e00\u81f4\u5b58\u5728\uff0c\u5373\u4f7f\u662f\u50cf 0.1 \u8fd9\u6837\u7b80\u5355\u7684\u6570\u3002\u56e0\u6b64\uff0c\u4f60\u4e0d\u80fd\u5c06\u6d6e\u70b9\u6570\u8fdb\u884c\u76f4\u63a5\u6bd4\u8f83\u3002 4.9 - \u5e03\u5c14\u503c \u5e03\u5c14 \u7c7b\u578b\u7528\u4e8e\u5b58\u653e true \u548c false \u3002 4.10 - if \u8bed\u53e5\u7b80\u4ecb If \u8bed\u53e5 4.10 - if \u8bed\u53e5\u7b80\u4ecb \u5141\u8bb8\u6211\u4eec\u6ee1\u8db3\u67d0\u79cd\u6761\u4ef6\u65f6\u624d\u6267\u884c\u4e00\u6761\u6216\u591a\u6761\u8bed\u53e5\u3002if \u8bed\u53e5\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u4f1a\u88ab\u89e3\u6790\u4e3a\u5e03\u5c14\u503c\u3002 4.11 - \u5b57\u7b26 \u5b57\u7b26 \u5b58\u653e\u7684\u503c\u4f1a\u88ab\u89e3\u6790\u4e3a ASCII \u5b57\u6bcd\u3002\u5f53\u4f7f\u7528\u5b57\u7b26\u65f6\uff0c\u5c0f\u5fc3\u4e0d\u8981\u5c06 ASCII \u7801\u503c\u548c\u6570\u5b57\u641e\u6df7\u3002\u5982\u679c\u9700\u8981\u628a\u5b57\u7b26\u5f53\u6574\u578b\u6253\u5370\u5904\u7406\uff0c\u9700\u8981\u4f7f\u7528 static_cast \u3002 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast \u5c16\u62ec\u53f7\u5728 C++ \u4e2d\u901a\u5e38\u7528\u6765\u8868\u793a\u53c2\u6570\u5316\u7c7b\u578b\u3002\u5728 static_cast \u4e2d\uff0c\u5c16\u62ec\u53f7\u7684\u5185\u5bb9\u6307\u5b9a\u4e86\u76ee\u6807\u7c7b\u578b\u3002(\u4f8b\u5982 static_cast(x) \u4f1a\u628a x \u8f6c\u6362\u4e3a int )\u3002 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \u5e38\u91cf\u6307\u7684\u662f\u503c\u4e0d\u4f1a\u6539\u53d8\u7684\u53d8\u91cf\u3002C++ \u652f\u6301\u4e24\u79cd\u5e38\u91cf\uff1a\u5b57\u9762\u91cf\u5e38\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \u7b26\u53f7\u5e38\u91cf \u6307\u7684\u662f\u7ed9\u4e0e\u5e38\u91cf\u7684\u540d\u5b57\u3002\u5e38\u91cf\u662f\u7b26\u53f7\u53d8\u91cf\u7684\u4e00\u79cd\uff0c\u4f7f\u7528\u66ff\u6362\u6587\u672c\u7684\u7c7b\u5bf9\u8c61\u5b8f\u4e5f\u662f\u7b26\u53f7\u5e38\u91cf\uff1b 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr \u5e38\u91cf\u8868\u8fbe\u5f0f \u662f\u4e00\u4e2a\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\u8868\u8fbe\u5f0f\u3002 \u7f16\u8bd1\u65f6\u5e38\u91cf \u662f\u5176\u503c\u5728\u7f16\u8bd1\u65f6\u5df2\u77e5\u7684\u5e38\u91cf\u3002 \u8fd0\u884c\u65f6\u5e38\u91cf \u662f\u5728\u8fd0\u884c\u65f6\u4e4b\u524d\u4e0d\u77e5\u9053\u5176\u521d\u59cb\u5316\u503c\u7684\u5e38\u91cf\u3002constexprvariable\u5fc5\u987b\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 4.15 - \u5b57\u9762\u91cf \u5b57\u9762\u91cf \u662f\u76f4\u63a5\u5d4c\u5165\u5230\u4ee3\u7801\u4e2d\u7684\u5e38\u91cf\u3002\u5b57\u9762\u91cf\u662f\u6709\u7c7b\u578b\u7684\uff0c\u800c\u5b57\u9762\u91cf\u540e\u7f00\u53ef\u4ee5\u7528\u6765\u4fee\u6539\u5b57\u9762\u91cf\u7684\u9ed8\u8ba4\u7c7b\u578b\u3002 \u9b54\u9b3c\u6570\u5b57\u6307\u7684\u662f\u542b\u4e49\u4e0d\u660e\u786e\u6216\u662f\u7a0d\u540e\u4f1a\u88ab\u4fee\u6539\u7684\u5b57\u9762\u91cf\uff08\u901a\u5e38\u662f\u6570\u5b57\uff09\uff0c\u4e0d\u8981\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u9b54\u672f\u6570\u5b57\uff0c\u5e94\u8be5\u7528\u7b26\u53f7\u5e38\u91cf\u4ee3\u66ff\u5b83\u3002 4.16 - \u6570\u503c\u7cfb\u7edf\uff08\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236\uff09 \u5728\u65e5\u5e38\u751f\u6d3b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5341\u8fdb\u5236\u6570\uff0c\u5b83\u670910\u4f4d\u6570\u5b57\u3002\u8ba1\u7b97\u673a\u4f7f\u7528\u4e8c\u8fdb\u5236\uff0c\u5b83\u53ea\u67092\u4f4d\u6570\u5b57\u3002C++\u8fd8\u652f\u6301\u516b\u8fdb\u5236(\u4ee58\u4e3a\u57fa\u6570)\u548c\u5341\u516d\u8fdb\u5236(\u4ee516\u4e3a\u57fa\u6570)\u3002\u8fd9\u4e9b\u90fd\u662f\u6570\u5b57\u7cfb\u7edf\u7684\u4f8b\u5b50\uff0c\u662f\u7528\u6765\u8868\u793a\u6570\u5b57\u7684\u7b26\u53f7(\u6570\u5b57)\u7684\u96c6\u5408\u3002 4.17 - std::string \u7b80\u4ecb 4.18 - std::string_view \u7b80\u4ecb \u5b57\u7b26\u4e32\u662f\u7528\u4e8e\u8868\u793a\u6587\u672c(\u5982\u540d\u79f0\u3001\u5355\u8bcd\u548c\u53e5\u5b50)\u7684\u987a\u5e8f\u5b57\u7b26\u7684\u96c6\u5408\u3002\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u603b\u662f\u653e\u5728\u53cc\u5f15\u53f7\u4e4b\u95f4\u3002C++\u4e2d\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u662fC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u5b83\u7684\u7c7b\u578b\u6bd4\u8f83\u5947\u602a\uff0c\u5f88\u96be\u5904\u7406\u3002 std::string \u63d0\u4f9b\u4e86\u7b80\u4fbf\u3001\u5b89\u5168\u7684\u5904\u7406\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u3002\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u603b\u662f\u653e\u5728\u53cc\u5f15\u53f7\u4e2d\u3002\u4f7f\u7528 std::string \u9700\u8981\u5305\u542b \u5934\u6587\u4ef6\uff1b std::string_view \u4e3a\u5df2\u7ecf\u5b58\u5728\u7684\u5b57\u7b26\u4e32\uff08C\u98ce\u683c\u5b57\u7b26\u4e32\u3001 std::string \u6216\u5b57\u7b26\u6570\u7ec4\uff09\u63d0\u4f9b\u4e86\u4e00\u79cd\u53ea\u8bfb\u7684\u8bbf\u95ee\u65b9\u5f0f\uff0c\u800c\u4e0d\u9700\u521b\u5efa\u62f7\u8d1d\u3002","title":"\u7ae0\u8282\u56de\u987e"},{"location":"5-1-Operator-precedence-and-associativity/","tags":["operator"],"text":"Key Takeaway \u4f7f\u7528\u62ec\u53f7\u6765\u6e05\u695a\u5730\u8868\u660e\u590d\u6742\u8868\u8fbe\u5f0f\u5e94\u8be5\u5982\u4f55\u6c42\u503c(\u5c3d\u7ba1\u4ece\u6280\u672f\u4e0a\u6765\u8bb2\u8fd9\u662f\u591a\u4f59\u7684)\u3002 \u5982\u679c\u8868\u8fbe\u5f0f\u4e2d\u53ea\u6709\u4e00\u4e2a\u8d4b\u503c\u8fd0\u7b97\u7b26\u5b83\u53f3\u8fb9\u7684\u6574\u4e2a\u8868\u8fbe\u5f0f\u6ca1\u5fc5\u8981\u653e\u5728\u62ec\u53f7\u91cc\u3002 \u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u7b26\u5408\u8868\u8fbe\u5f0f\u4e2d\u64cd\u4f5c\u6570\u53ef\u4ee5\u4ee5\u4efb\u610f\u987a\u5e8f\u6c42\u503c\uff0c\u5305\u62ec\u51fd\u6570\u8c03\u7528\u548c\u53c2\u6570\u6c42\u503c\u3002 \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b\u89c4\u5b9a\u4ee5\u5916\u7684\u60c5\u51b5\uff0c\u90fd\u53ea\u80fd\u5047\u8bbe\u4f1a\u4ee5\u4efb\u610f\u987a\u5e8f\u6c42\u503c\u3002\u786e\u4fdd\u4f60\u7f16\u5199\u7684\u8868\u8fbe\u5f0f\u4e0d\u4f9d\u8d56\u4e8e\u8fd9\u4e9b\u90e8\u5206\u7684\u6c42\u503c\u987a\u5e8f\u3002 \u7ae0\u8282\u7b80\u4ecb \u672c\u7ae0\u7684\u5185\u5bb9\u5efa\u7acb\u5728 1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 \u7684\u57fa\u7840\u4e0a\u3002\u9996\u5148\u8ba9\u6211\u4eec\u6765\u590d\u4e60\u4e00\u4e0b\u5b66\u8fc7\u7684\u5185\u5bb9\uff1a \u6570\u5b66\u4e2d\u7684\u8fd0\u7b97\u6307\u7684\u662f\u4e00\u4e2a\u8ba1\u7b97\u64cd\u4f5c\uff0c\u5b83\u6d89\u53ca\u52300\u4e2a\u6216\u591a\u4e2a\u8f93\u5165\u503c\uff08\u79f0\u4e3a \u64cd\u4f5c\u6570 \uff09\uff0c\u5e76\u80fd\u591f\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u503c\uff08\u79f0\u4e3a\u8f93\u51fa\u503c\uff09\u3002\u8fd9\u4e2a\u7279\u5b9a\u7684\u8ba1\u7b97\u64cd\u4f5c\uff0c\u901a\u8fc7\u67d0\u79cd\u7ed3\u6784\uff08\u901a\u5e38\u662f\u4e00\u4e2a\u6216\u4e00\u5bf9\u7b26\u53f7\uff09\u6765\u6307\u5b9a\uff0c\u8fd9\u79cd\u7ed3\u6784\u79f0\u4e4b\u4e3a \u8fd0\u7b97\u7b26(operator) \u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u90fd\u77e5\u9053 2 + 3 = 5 \u3002\u5728\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4e2d\uff0c2\u548c3\u79f0\u4e4b\u4e3a\u8fd0\u7b97\u7b26\uff0c\u800c\u7b26\u53f7 + \u5219\u79f0\u4e3a\u8fd0\u7b97\u7b26\uff0c\u8be5\u8fd0\u7b97\u7b26\u544a\u8bc9\u6211\u4eec\u6b64\u65f6\u5c06\u5bf9\u4e24\u4e2a\u64cd\u4f5c\u6570\u6267\u884c\u4e00\u4e2a\u52a0\u6cd5\u64cd\u4f5c\uff0c\u5e76\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u503c 5\u3002 \u5728\u672c\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba\u6709\u5173\u8fd0\u7b97\u7b26\u7684\u8bdd\u9898\uff0c\u540c\u65f6\u4ecb\u7ecd\u5f88\u591a C++ \u652f\u6301\u7684\u8fd0\u7b97\u7b26\u3002 \u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7 \u8003\u8651\u4e00\u4e0b\u8fd9\u4e2a\u7a0d\u5fae\u6709\u70b9\u590d\u6742\u7684\u8868\u8fbe\u5f0f 4 + 2 * 3 \u3002\u4e00\u4e2a\u542b\u6709\u591a\u79cd\u8fd0\u7b97\u7b26\u7684\u8868\u8fbe\u5f0f\u79f0\u4e3a \u590d\u5408\u8868\u8fbe\u5f0f \uff0c\u4e3a\u4e86\u5bf9\u4e00\u4e2a\u590d\u5408\u8868\u8fbe\u5f0f\u6c42\u503c\uff0c\u6211\u4eec\u5fc5\u987b\u77e5\u9053\u6bcf\u4e2a\u8fd0\u7b97\u7b26\u7684\u542b\u4e49\uff0c\u4ee5\u53ca\u8fd0\u7b97\u7b26\u7684\u6267\u884c\u987a\u5e8f\u3002\u590d\u6742\u8868\u8fbe\u5f0f\u6c42\u503c\u65f6\u8fd0\u7b97\u7b26\u7684\u6267\u884c\u987a\u5e8f\u6709\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u51b3\u5b9a\u3002\u57fa\u4e8e\u5bf9\u666e\u901a\u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u7684\u7406\u89e3\uff08\u4e58\u6cd5\u4f18\u5148\u7ea7\u9ad8\u4e8e\u52a0\u6cd5\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u77e5\u9053\u4e0a\u8ff0\u8868\u8fbe\u5f0f\u5e94\u8be5\u6309\u7167 4 + (2 * 3) \u6c42\u503c\u5e76\u5f97\u5230\u7ed3\u679c 10\u3002 \u5728 C++ \u4e2d\uff0c\u5f53\u7f16\u8bd1\u5668\u9047\u5230\u8868\u8fbe\u5f0f\u65f6\uff0c\u5b83\u540c\u6837\u9700\u8981\u5206\u6790\u8868\u8fbe\u5f0f\u5e76\u786e\u5b9a\u5982\u4f55\u6c42\u503c\u3002\u4e3a\u4e86\u5e2e\u52a9\u7f16\u8bd1\u5668\u5224\u65ad\uff0c\u6240\u6709\u7684\u8fd0\u7b97\u7b26\u90fd\u88ab\u6307\u5b9a\u4e86\u4f18\u5148\u7ea7\u3002\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u8d8a\u9ad8\uff0c\u8d8a\u5148\u6c42\u503c\u3002 \u4f60\u53ef\u4ee5\u4ece\u4e0b\u9762\u7684\u8868\u4e2d\u770b\u5230\uff0c\u4e58\u6cd5\u548c\u9664\u6cd5\uff08\u4f18\u5148\u7ea7 5\uff09\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e\u52a0\u6cd5\u548c\u51cf\u6cd5\uff08\u4f18\u5148\u7ea7 6\uff09\u56e0\u6b64 4 + 2 * 3 \u4f1a\u88ab\u770b\u505a 4 + (2 * 3) \u56e0\u4e3a\u4e58\u6cd5\u7684\u4f18\u5148\u7ea7\u66f4\u9ad8\u3002 \u8fd0\u7b97\u7b26\u7684\u7ed3\u5408\u5f8b \u5982\u679c\u4e24\u4e2a\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u6070\u597d\u76f8\u7b49\u4f1a\u600e\u6837\uff1f\u4f8b\u5982\uff0c\u5728 3 * 4 / 2 \u4e2d\uff0c\u4e58\u6cd5\u548c\u9664\u6cd5\u7684\u4f18\u5148\u7ea7\u5c31\u662f\u4e00\u6837\u7684\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u7f16\u8bd1\u5668\u4ec5\u9760\u4f18\u5148\u7ea7\u662f\u4e0d\u80fd\u5224\u65ad\u5982\u4f55\u6c42\u503c\u7684\u3002 \u5982\u679c\u4e24\u4e2a\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u76f8\u540c\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u53c2\u8003\u8fd0\u7b97\u7b26\u7684\u7ed3\u5408\u5f8b\u4ee5\u5224\u65ad\u8be5\u8fd0\u7b97\u7b26\u7684\u6c42\u503c\u987a\u5e8f\u662f\u4ece\u5de6\u5411\u53f3\u8fd8\u662f\u4ece\u53f3\u5411\u5de6\u3002\u4f18\u5148\u7ea7 5 \u7684\u8fd0\u7b97\u7b26\uff0c\u7ed3\u5408\u5f8b\u4e3a\u4ece\u5de6\u5411\u53f3\uff0c\u56e0\u6b64\u8868\u8fbe\u5f0f\u88ab\u770b\u505a (3 * 4) / 2 = 6 \u3002 \u8fd0\u7b97\u7b26\u8868 \u4e0b\u8868\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u4f5c\u4e3a\u53c2\u8003\u624b\u518c\u6765\u4f7f\u7528\uff08\u5982\u679c\u4f60\u5c06\u6765\u9047\u5230\u6709\u5173\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b\u76f8\u5173\u7684\u95ee\u9898\u65f6\u53ef\u4ee5\u8fc7\u6765\u67e5\u8be2\uff09\u3002 \u6ce8\u610f\uff1a \u4f18\u5148\u7ea71\u662f\u6700\u9ad8\u7684\u4f18\u5148\u7ea7\uff0c17 \u4e3a\u6700\u4f4e\u3002\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u8d8a\u9ad8\uff0c\u8d8a\u5148\u6c42\u503c\uff1b L->R \u8868\u793a\u4ece\u5de6\u5411\u53f3\uff1b R->L \u8868\u793a\u4ece\u53f3\u5411\u5de6\uff1b \u4f60\u80af\u5b9a\u5df2\u7ecf\u8ba4\u8bc6\u4e0a\u8868\u4e2d\u7684\u67d0\u4e9b\u64cd\u4f5c\u7b26\u4e86\uff0c\u4f8b\u5982 + , - , * , / , () \u548c sizeof \u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u7684\u57fa\u7840\u7684\u8bdd\uff0c\u5176\u4e2d\u5927\u591a\u662f\u64cd\u4f5c\u7b26\u4f60\u5e94\u8be5\u8fd8\u4e0d\u80fd\u7406\u89e3\u5176\u542b\u4e49\u3002\u8fd9\u662f\u6b63\u5e38\u7684\uff0c\u6211\u4eec\u672c\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u5b83\u4eec\u4e2d\u7684\u5927\u591a\u662f\uff0c\u5269\u4e0b\u7684\u4e00\u4e9b\u5219\u4f1a\u5728\u9047\u5230\u65f6\u518d\u4ecb\u7ecd\u3002 Q: \u4e3a\u4ec0\u4e48\u6ca1\u6709\u6307\u6570\u8fd0\u7b97\u7b26? C++ \u5e76\u4e0d\u652f\u6301\u6307\u6570\u8fd0\u7b97\u7b26\uff08 operator^ \u5728 C++ \u4e2d\u6709\u5176\u4ed6\u7684\u542b\u4e49 \uff09\u3002\u6211\u4eec\u4f1a\u5728 5.3 - \u6c42\u6a21\u548c\u6307\u6570\u8fd0\u7b97 \u4e2d\u4ecb\u7ecd\u6307\u6570\u8fd0\u7b97\u3002 \u52a0\u62ec\u53f7 \u5728\u6570\u5b66\u8fd0\u7b97\u4e2d\uff0c\u6211\u4eec\u90fd\u77e5\u9053\u52a0\u62ec\u53f7\u53ef\u4ee5\u6539\u53d8\u8fd0\u7b97\u7b26\u6267\u884c\u7684\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u77e5\u9053 4 + 2 * 3 \u4f1a\u6309\u7167 4 + (2 * 3) \u6c42\u503c\u3002\u4f46\u662f\u5982\u679c\u6211\u5e0c\u671b\u6309\u7167 (4 + 2) * 3 \u6c42\u503c\u5462\uff1f\u4f60\u5c31\u53ef\u4ee5\u5411\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4e00\u6837\uff0c\u4f7f\u7528\u62ec\u53f7\u660e\u786e\u8868\u793a\u4f60\u5e0c\u671b\u7684\u6c42\u503c\u65b9\u5f0f\u3002C++ \u4e2d\u4f60\u4e5f\u53ef\u4ee5\u8fd9\u6837\u505a\uff0c\u56e0\u4e3a\u62ec\u53f7\u5177\u6709\u6700\u9ad8\u7684\u4f18\u5148\u7ea7\uff0c\u6240\u4ee5\u62ec\u53f7\u603b\u662f\u5148\u4e8e\u5b83\u5185\u90e8\u7684\u4efb\u4f55\u7b26\u53f7\u8fdb\u884c\u6c42\u503c\u3002 \u518d\u8003\u8651\u4e0b\u9762\u7684\u8868\u8fbe\u5f0f x && y || z \u3002\u5b83\u5e94\u8be5\u6309\u7167 (x && y) || z \u6c42\u503c\u8fd8\u662f\u6309\u7167 x && (y || z) \u6c42\u503c\u5462\uff1f\u4f60\u53ef\u4ee5\u67e5\u770b\u4e0a\u8868\uff0c\u53d1\u73b0 && \u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e || \u3002\u4f46\u662f\u8fd0\u7b97\u7b26\u6bd5\u7adf\u592a\u591a\u4e86\uff0c\u4f60\u4e5f\u4e0d\u53ef\u80fd\u90fd\u8bb0\u4f4f\u3002 \u4e3a\u4e86\u51cf\u5c11\u9519\u8bef\uff0c\u540c\u65f6\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff08\u51cf\u5c11\u67e5\u8868\uff09\uff0c\u5bf9\u4e8e\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u4f7f\u7528\u62ec\u53f7\u660e\u786e\u4f60\u7684\u76ee\u7684\u662f\u6700\u597d\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u62ec\u53f7\u6765\u6e05\u695a\u5730\u8868\u660e\u590d\u6742\u8868\u8fbe\u5f0f\u5e94\u8be5\u5982\u4f55\u6c42\u503c(\u5c3d\u7ba1\u4ece\u6280\u672f\u4e0a\u6765\u8bb2\u8fd9\u662f\u591a\u4f59\u7684)\u3002 \u4e0a\u9762\u6240\u8bf4\u7684\u6700\u4f73\u5b9e\u8df5\u6709\u4e00\u4e2a\u4f8b\u5916\uff1a\u5bf9\u4e8e\u8d4b\u503c\u8fd0\u7b97\u7b26\u6765\u8bf4\uff0c\u5982\u679c\u8868\u8fbe\u5f0f\u4e2d\u53ea\u6709\u4e00\u4e2a\u8d4b\u503c\u8fd0\u7b97\u7b26\u5b83\u53f3\u8fb9\u7684\u6574\u4e2a\u8868\u8fbe\u5f0f\u6ca1\u5fc5\u8981\u653e\u5728\u62ec\u53f7\u91cc\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 x = ( y + z + w ); // \u4e0d\u9700\u8981 x = y + z + w ; // \u8fd9\u6837\u5c31\u53ef\u4ee5 x = (( y || z ) && w ); // \u4e0d\u9700\u8981 x = ( y || z ) && w ; // \u8fd9\u6837\u5c31\u53ef\u4ee5 x = ( y *= z ); // \u6709\u591a\u4e2a\u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u60c5\u51b5\u4e0b\uff0c\u52a0\u62ec\u53f7\u4ecd\u7136\u5f88\u6709\u7528 \u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u662f\u5012\u6570\u7b2c\u4e8c\u9ad8\u7684\uff08\u9017\u53f7\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u6700\u4f4e\uff0c\u800c\u4e14\u4e5f\u4e0d\u5e38\u7528\uff09\u3002\u56e0\u6b64\uff0c\u53ea\u8981\u8868\u8fbe\u5f0f\u4e2d\u53ea\u6709\u4e00\u4e2a\u8d4b\u503c\u8fd0\u7b97\u7b26\uff08\u800c\u4e14\u6ca1\u6709\u9017\u53f7\u8fd0\u7b97\u7b26\uff09\uff0c\u90a3\u4e48\u53ef\u4ee5\u786e\u5b9a\u8d4b\u503c\u53f7\u53f3\u8fb9\u7684\u8868\u8fbe\u5f0f\u4f1a\u5728\u8d4b\u503c\u524d\u5b8c\u5168\u6c42\u503c\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u8868\u8fbe\u5f0f\u4e2d\u53ea\u6709\u4e00\u4e2a\u8d4b\u503c\u8fd0\u7b97\u7b26\u5b83\u53f3\u8fb9\u7684\u6574\u4e2a\u8868\u8fbe\u5f0f\u6ca1\u5fc5\u8981\u653e\u5728\u62ec\u53f7\u91cc\u3002 \u8868\u8fbe\u5f0f\u6c42\u503c\u987a\u5e8f\u548c\u51fd\u6570\u53c2\u6570\u5904\u7406\u987a\u5e8f\u5927\u591a\u662f\u672a\u6307\u660e\u7684 \u8003\u8651\u4e0b\u9762\u7684\u8868\u8fbe\u5f0f\uff1a 1 a + b * c \u57fa\u4e8e\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b\uff0c\u6211\u4eec\u6307\u5b9a\u4e0a\u9762\u7684\u8868\u8fbe\u5f0f\u4f1a\u6309\u7167\u4e0b\u9762\u7684\u65b9\u5f0f\u6c42\u503c\uff1a 1 a + ( b * c ) \u5982\u679c a=1 \uff0c b=2 \uff0c c=3 \uff0c\u5219\u6574\u4e2a\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u4e3a 7\u3002 \u4f46\u662f\uff0c\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b\u53ea\u8868\u660e\u8fd0\u7b97\u7b26\u4e4b\u95f4\u7684\u6c42\u503c\u987a\u5e8f\uff0c\u4f46\u5e76\u6ca1\u6709\u8bf4\u660e\u8868\u8fbe\u5f0f\u5176\u4ed6\u90e8\u5206\u7684\u6c42\u503c\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u4e0a\u9762\u4f8b\u5b50\u4e2d a,b,c\u7684\u6c42\u503c\u987a\u5e8f\u662f\u4ec0\u4e48\u5462\uff1f \u4ee4\u4eba\u5403\u60ca\u7684\u662f\uff0c\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u590d\u6742\u8868\u8fbe\u5f0f\u4e2d\u5404\u90e8\u5206\u7684\u6c42\u503c\u987a\u5e8f\u662f\u6ca1\u6709\u660e\u786e\u89c4\u5b9a\u7684\uff08\u5305\u62ec\u51fd\u6570\u8c03\u7528\u548c\u53c2\u6570\u6c42\u503c\uff09\u3002\u56e0\u6b64\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u81ea\u7531\u9009\u62e9\u5b83\u8ba4\u4e3a\u6700\u4f18\u7684\u6c42\u503c\u987a\u5e8f\u3002 \u6ce8\u610f \u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u7b26\u5408\u8868\u8fbe\u5f0f\u4e2d\u64cd\u4f5c\u6570\u53ef\u4ee5\u4ee5\u4efb\u610f\u987a\u5e8f\u6c42\u503c\uff0c\u5305\u62ec\u51fd\u6570\u8c03\u7528\u548c\u53c2\u6570\u6c42\u503c\u3002 \u5bf9\u4e8e\u5927\u591a\u6570\u8868\u8fbe\u5f0f\uff0c\u8fd9\u90fd\u4e0d\u662f\u4ec0\u4e48\u5927\u4e8b\u3002\u4ee5\u4e0a\u9762\u7684\u8868\u8fbe\u5f0f\u4e3a\u4f8b\uff0c\u51e0\u4e2a\u53d8\u91cf\u54ea\u4e2a\u5148\u6c42\u503c\u90fd\u662f\u53ef\u4ee5\u7684\uff0c\u7ed3\u679c\u4e5f\u603b\u662f\u7b49\u4e8e 7\u3002 \u4f46\u662f\uff0c\u786e\u5b9e\u6709\u4e9b\u8868\u8fbe\u5f0f\u662f\u6709\u53ef\u80fd\u53d7\u5f71\u54cd\u7684\u3002\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u5b83\u5176\u4e2d\u5305\u542b\u7684\u9519\u8bef\u662f\u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u90fd\u4f1a\u72af\u7684\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int getValue () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; return x ; } int main () { std :: cout << getValue () + ( getValue () * getValue ()); // a + (b * c) return 0 ; } \u8fd0\u884c\u8be5\u7a0b\u5e8f\u5e76\u4f9d\u6b21\u8f93\u5165 1\u30012 \u548c 3\u7684\u8bdd\uff0c\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u8f93\u51fa\u7ed3\u679c\u5e94\u8be5\u662f 7\u3002\u4f46\u662f\uff0c\u8fd9\u662f\u5efa\u7acb\u5728\u8c03\u7528 getValue() \u4f1a\u4ece\u5de6\u5411\u53f3\u6c42\u503c\u7684\u5047\u8bbe\u4e0a\u7684\u3002\u5b9e\u9645\u4e0a\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u9009\u62e9\u4e0d\u540c\u7684\u6c42\u503c\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u7f16\u8bd1\u5668\u91c7\u7528\u4ece\u53f3\u5411\u5de6\u7684\u987a\u5e8f\u6c42\u503c\uff0c\u5219\u76f8\u540c\u7684\u8f93\u5165\u5f97\u5230\u7684\u7ed3\u679c\u5c31\u662f 5 \uff08 3+(2*1) \uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b\u89c4\u5b9a\u4ee5\u5916\u7684\u60c5\u51b5\uff0c\u90fd\u53ea\u80fd\u5047\u8bbe\u4f1a\u4ee5\u4efb\u610f\u987a\u5e8f\u6c42\u503c\u3002\u786e\u4fdd\u4f60\u7f16\u5199\u7684\u8868\u8fbe\u5f0f\u4e0d\u4f9d\u8d56\u4e8e\u8fd9\u4e9b\u90e8\u5206\u7684\u6c42\u503c\u987a\u5e8f\u3002 \u4e0a\u9762\u7684\u7a0b\u5e8f\u53ef\u4ee5\u7a0d\u52a0\u4fee\u6539\u53d8\u6210\u6ca1\u6709\u6b67\u4e49\u7684\u5f62\u5f0f\uff0c\u5373\u5c06\u51fd\u6570\u8c03\u7528\u4f5c\u4e3a\u5355\u72ec\u7684\u8bed\u53e5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int getValue () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; return x ; } int main () { int a { getValue () }; // will execute first int b { getValue () }; // will execute second int c { getValue () }; // will execute third std :: cout << a + ( b * c ); // order of eval doesn't matter now return 0 ; } \u76f8\u5173\u5185\u5bb9 \u8fd8\u6709\u4e00\u4e9b\u60c5\u51b5\u4e5f\u4f1a\u9020\u6210\u4e0a\u8ff0\u6c42\u503c\u987a\u5e8f\u95ee\u9898\uff0c\u6211\u4eec\u4f1a\u5728 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"5.1 - \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b"},{"location":"5-1-Operator-precedence-and-associativity/#_1","text":"\u672c\u7ae0\u7684\u5185\u5bb9\u5efa\u7acb\u5728 1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 \u7684\u57fa\u7840\u4e0a\u3002\u9996\u5148\u8ba9\u6211\u4eec\u6765\u590d\u4e60\u4e00\u4e0b\u5b66\u8fc7\u7684\u5185\u5bb9\uff1a \u6570\u5b66\u4e2d\u7684\u8fd0\u7b97\u6307\u7684\u662f\u4e00\u4e2a\u8ba1\u7b97\u64cd\u4f5c\uff0c\u5b83\u6d89\u53ca\u52300\u4e2a\u6216\u591a\u4e2a\u8f93\u5165\u503c\uff08\u79f0\u4e3a \u64cd\u4f5c\u6570 \uff09\uff0c\u5e76\u80fd\u591f\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u503c\uff08\u79f0\u4e3a\u8f93\u51fa\u503c\uff09\u3002\u8fd9\u4e2a\u7279\u5b9a\u7684\u8ba1\u7b97\u64cd\u4f5c\uff0c\u901a\u8fc7\u67d0\u79cd\u7ed3\u6784\uff08\u901a\u5e38\u662f\u4e00\u4e2a\u6216\u4e00\u5bf9\u7b26\u53f7\uff09\u6765\u6307\u5b9a\uff0c\u8fd9\u79cd\u7ed3\u6784\u79f0\u4e4b\u4e3a \u8fd0\u7b97\u7b26(operator) \u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u90fd\u77e5\u9053 2 + 3 = 5 \u3002\u5728\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4e2d\uff0c2\u548c3\u79f0\u4e4b\u4e3a\u8fd0\u7b97\u7b26\uff0c\u800c\u7b26\u53f7 + \u5219\u79f0\u4e3a\u8fd0\u7b97\u7b26\uff0c\u8be5\u8fd0\u7b97\u7b26\u544a\u8bc9\u6211\u4eec\u6b64\u65f6\u5c06\u5bf9\u4e24\u4e2a\u64cd\u4f5c\u6570\u6267\u884c\u4e00\u4e2a\u52a0\u6cd5\u64cd\u4f5c\uff0c\u5e76\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u503c 5\u3002 \u5728\u672c\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba\u6709\u5173\u8fd0\u7b97\u7b26\u7684\u8bdd\u9898\uff0c\u540c\u65f6\u4ecb\u7ecd\u5f88\u591a C++ \u652f\u6301\u7684\u8fd0\u7b97\u7b26\u3002","title":"\u7ae0\u8282\u7b80\u4ecb"},{"location":"5-1-Operator-precedence-and-associativity/#_2","text":"\u8003\u8651\u4e00\u4e0b\u8fd9\u4e2a\u7a0d\u5fae\u6709\u70b9\u590d\u6742\u7684\u8868\u8fbe\u5f0f 4 + 2 * 3 \u3002\u4e00\u4e2a\u542b\u6709\u591a\u79cd\u8fd0\u7b97\u7b26\u7684\u8868\u8fbe\u5f0f\u79f0\u4e3a \u590d\u5408\u8868\u8fbe\u5f0f \uff0c\u4e3a\u4e86\u5bf9\u4e00\u4e2a\u590d\u5408\u8868\u8fbe\u5f0f\u6c42\u503c\uff0c\u6211\u4eec\u5fc5\u987b\u77e5\u9053\u6bcf\u4e2a\u8fd0\u7b97\u7b26\u7684\u542b\u4e49\uff0c\u4ee5\u53ca\u8fd0\u7b97\u7b26\u7684\u6267\u884c\u987a\u5e8f\u3002\u590d\u6742\u8868\u8fbe\u5f0f\u6c42\u503c\u65f6\u8fd0\u7b97\u7b26\u7684\u6267\u884c\u987a\u5e8f\u6709\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u51b3\u5b9a\u3002\u57fa\u4e8e\u5bf9\u666e\u901a\u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u7684\u7406\u89e3\uff08\u4e58\u6cd5\u4f18\u5148\u7ea7\u9ad8\u4e8e\u52a0\u6cd5\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u77e5\u9053\u4e0a\u8ff0\u8868\u8fbe\u5f0f\u5e94\u8be5\u6309\u7167 4 + (2 * 3) \u6c42\u503c\u5e76\u5f97\u5230\u7ed3\u679c 10\u3002 \u5728 C++ \u4e2d\uff0c\u5f53\u7f16\u8bd1\u5668\u9047\u5230\u8868\u8fbe\u5f0f\u65f6\uff0c\u5b83\u540c\u6837\u9700\u8981\u5206\u6790\u8868\u8fbe\u5f0f\u5e76\u786e\u5b9a\u5982\u4f55\u6c42\u503c\u3002\u4e3a\u4e86\u5e2e\u52a9\u7f16\u8bd1\u5668\u5224\u65ad\uff0c\u6240\u6709\u7684\u8fd0\u7b97\u7b26\u90fd\u88ab\u6307\u5b9a\u4e86\u4f18\u5148\u7ea7\u3002\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u8d8a\u9ad8\uff0c\u8d8a\u5148\u6c42\u503c\u3002 \u4f60\u53ef\u4ee5\u4ece\u4e0b\u9762\u7684\u8868\u4e2d\u770b\u5230\uff0c\u4e58\u6cd5\u548c\u9664\u6cd5\uff08\u4f18\u5148\u7ea7 5\uff09\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e\u52a0\u6cd5\u548c\u51cf\u6cd5\uff08\u4f18\u5148\u7ea7 6\uff09\u56e0\u6b64 4 + 2 * 3 \u4f1a\u88ab\u770b\u505a 4 + (2 * 3) \u56e0\u4e3a\u4e58\u6cd5\u7684\u4f18\u5148\u7ea7\u66f4\u9ad8\u3002","title":"\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7"},{"location":"5-1-Operator-precedence-and-associativity/#_3","text":"\u5982\u679c\u4e24\u4e2a\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u6070\u597d\u76f8\u7b49\u4f1a\u600e\u6837\uff1f\u4f8b\u5982\uff0c\u5728 3 * 4 / 2 \u4e2d\uff0c\u4e58\u6cd5\u548c\u9664\u6cd5\u7684\u4f18\u5148\u7ea7\u5c31\u662f\u4e00\u6837\u7684\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u7f16\u8bd1\u5668\u4ec5\u9760\u4f18\u5148\u7ea7\u662f\u4e0d\u80fd\u5224\u65ad\u5982\u4f55\u6c42\u503c\u7684\u3002 \u5982\u679c\u4e24\u4e2a\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u76f8\u540c\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u53c2\u8003\u8fd0\u7b97\u7b26\u7684\u7ed3\u5408\u5f8b\u4ee5\u5224\u65ad\u8be5\u8fd0\u7b97\u7b26\u7684\u6c42\u503c\u987a\u5e8f\u662f\u4ece\u5de6\u5411\u53f3\u8fd8\u662f\u4ece\u53f3\u5411\u5de6\u3002\u4f18\u5148\u7ea7 5 \u7684\u8fd0\u7b97\u7b26\uff0c\u7ed3\u5408\u5f8b\u4e3a\u4ece\u5de6\u5411\u53f3\uff0c\u56e0\u6b64\u8868\u8fbe\u5f0f\u88ab\u770b\u505a (3 * 4) / 2 = 6 \u3002","title":"\u8fd0\u7b97\u7b26\u7684\u7ed3\u5408\u5f8b"},{"location":"5-1-Operator-precedence-and-associativity/#_4","text":"\u4e0b\u8868\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u4f5c\u4e3a\u53c2\u8003\u624b\u518c\u6765\u4f7f\u7528\uff08\u5982\u679c\u4f60\u5c06\u6765\u9047\u5230\u6709\u5173\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b\u76f8\u5173\u7684\u95ee\u9898\u65f6\u53ef\u4ee5\u8fc7\u6765\u67e5\u8be2\uff09\u3002 \u6ce8\u610f\uff1a \u4f18\u5148\u7ea71\u662f\u6700\u9ad8\u7684\u4f18\u5148\u7ea7\uff0c17 \u4e3a\u6700\u4f4e\u3002\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u8d8a\u9ad8\uff0c\u8d8a\u5148\u6c42\u503c\uff1b L->R \u8868\u793a\u4ece\u5de6\u5411\u53f3\uff1b R->L \u8868\u793a\u4ece\u53f3\u5411\u5de6\uff1b \u4f60\u80af\u5b9a\u5df2\u7ecf\u8ba4\u8bc6\u4e0a\u8868\u4e2d\u7684\u67d0\u4e9b\u64cd\u4f5c\u7b26\u4e86\uff0c\u4f8b\u5982 + , - , * , / , () \u548c sizeof \u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u7684\u57fa\u7840\u7684\u8bdd\uff0c\u5176\u4e2d\u5927\u591a\u662f\u64cd\u4f5c\u7b26\u4f60\u5e94\u8be5\u8fd8\u4e0d\u80fd\u7406\u89e3\u5176\u542b\u4e49\u3002\u8fd9\u662f\u6b63\u5e38\u7684\uff0c\u6211\u4eec\u672c\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u5b83\u4eec\u4e2d\u7684\u5927\u591a\u662f\uff0c\u5269\u4e0b\u7684\u4e00\u4e9b\u5219\u4f1a\u5728\u9047\u5230\u65f6\u518d\u4ecb\u7ecd\u3002 Q: \u4e3a\u4ec0\u4e48\u6ca1\u6709\u6307\u6570\u8fd0\u7b97\u7b26? C++ \u5e76\u4e0d\u652f\u6301\u6307\u6570\u8fd0\u7b97\u7b26\uff08 operator^ \u5728 C++ \u4e2d\u6709\u5176\u4ed6\u7684\u542b\u4e49 \uff09\u3002\u6211\u4eec\u4f1a\u5728 5.3 - \u6c42\u6a21\u548c\u6307\u6570\u8fd0\u7b97 \u4e2d\u4ecb\u7ecd\u6307\u6570\u8fd0\u7b97\u3002","title":"\u8fd0\u7b97\u7b26\u8868"},{"location":"5-1-Operator-precedence-and-associativity/#_5","text":"\u5728\u6570\u5b66\u8fd0\u7b97\u4e2d\uff0c\u6211\u4eec\u90fd\u77e5\u9053\u52a0\u62ec\u53f7\u53ef\u4ee5\u6539\u53d8\u8fd0\u7b97\u7b26\u6267\u884c\u7684\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u77e5\u9053 4 + 2 * 3 \u4f1a\u6309\u7167 4 + (2 * 3) \u6c42\u503c\u3002\u4f46\u662f\u5982\u679c\u6211\u5e0c\u671b\u6309\u7167 (4 + 2) * 3 \u6c42\u503c\u5462\uff1f\u4f60\u5c31\u53ef\u4ee5\u5411\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4e00\u6837\uff0c\u4f7f\u7528\u62ec\u53f7\u660e\u786e\u8868\u793a\u4f60\u5e0c\u671b\u7684\u6c42\u503c\u65b9\u5f0f\u3002C++ \u4e2d\u4f60\u4e5f\u53ef\u4ee5\u8fd9\u6837\u505a\uff0c\u56e0\u4e3a\u62ec\u53f7\u5177\u6709\u6700\u9ad8\u7684\u4f18\u5148\u7ea7\uff0c\u6240\u4ee5\u62ec\u53f7\u603b\u662f\u5148\u4e8e\u5b83\u5185\u90e8\u7684\u4efb\u4f55\u7b26\u53f7\u8fdb\u884c\u6c42\u503c\u3002 \u518d\u8003\u8651\u4e0b\u9762\u7684\u8868\u8fbe\u5f0f x && y || z \u3002\u5b83\u5e94\u8be5\u6309\u7167 (x && y) || z \u6c42\u503c\u8fd8\u662f\u6309\u7167 x && (y || z) \u6c42\u503c\u5462\uff1f\u4f60\u53ef\u4ee5\u67e5\u770b\u4e0a\u8868\uff0c\u53d1\u73b0 && \u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e || \u3002\u4f46\u662f\u8fd0\u7b97\u7b26\u6bd5\u7adf\u592a\u591a\u4e86\uff0c\u4f60\u4e5f\u4e0d\u53ef\u80fd\u90fd\u8bb0\u4f4f\u3002 \u4e3a\u4e86\u51cf\u5c11\u9519\u8bef\uff0c\u540c\u65f6\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff08\u51cf\u5c11\u67e5\u8868\uff09\uff0c\u5bf9\u4e8e\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u4f7f\u7528\u62ec\u53f7\u660e\u786e\u4f60\u7684\u76ee\u7684\u662f\u6700\u597d\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u62ec\u53f7\u6765\u6e05\u695a\u5730\u8868\u660e\u590d\u6742\u8868\u8fbe\u5f0f\u5e94\u8be5\u5982\u4f55\u6c42\u503c(\u5c3d\u7ba1\u4ece\u6280\u672f\u4e0a\u6765\u8bb2\u8fd9\u662f\u591a\u4f59\u7684)\u3002 \u4e0a\u9762\u6240\u8bf4\u7684\u6700\u4f73\u5b9e\u8df5\u6709\u4e00\u4e2a\u4f8b\u5916\uff1a\u5bf9\u4e8e\u8d4b\u503c\u8fd0\u7b97\u7b26\u6765\u8bf4\uff0c\u5982\u679c\u8868\u8fbe\u5f0f\u4e2d\u53ea\u6709\u4e00\u4e2a\u8d4b\u503c\u8fd0\u7b97\u7b26\u5b83\u53f3\u8fb9\u7684\u6574\u4e2a\u8868\u8fbe\u5f0f\u6ca1\u5fc5\u8981\u653e\u5728\u62ec\u53f7\u91cc\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 x = ( y + z + w ); // \u4e0d\u9700\u8981 x = y + z + w ; // \u8fd9\u6837\u5c31\u53ef\u4ee5 x = (( y || z ) && w ); // \u4e0d\u9700\u8981 x = ( y || z ) && w ; // \u8fd9\u6837\u5c31\u53ef\u4ee5 x = ( y *= z ); // \u6709\u591a\u4e2a\u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u60c5\u51b5\u4e0b\uff0c\u52a0\u62ec\u53f7\u4ecd\u7136\u5f88\u6709\u7528 \u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u662f\u5012\u6570\u7b2c\u4e8c\u9ad8\u7684\uff08\u9017\u53f7\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u6700\u4f4e\uff0c\u800c\u4e14\u4e5f\u4e0d\u5e38\u7528\uff09\u3002\u56e0\u6b64\uff0c\u53ea\u8981\u8868\u8fbe\u5f0f\u4e2d\u53ea\u6709\u4e00\u4e2a\u8d4b\u503c\u8fd0\u7b97\u7b26\uff08\u800c\u4e14\u6ca1\u6709\u9017\u53f7\u8fd0\u7b97\u7b26\uff09\uff0c\u90a3\u4e48\u53ef\u4ee5\u786e\u5b9a\u8d4b\u503c\u53f7\u53f3\u8fb9\u7684\u8868\u8fbe\u5f0f\u4f1a\u5728\u8d4b\u503c\u524d\u5b8c\u5168\u6c42\u503c\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u8868\u8fbe\u5f0f\u4e2d\u53ea\u6709\u4e00\u4e2a\u8d4b\u503c\u8fd0\u7b97\u7b26\u5b83\u53f3\u8fb9\u7684\u6574\u4e2a\u8868\u8fbe\u5f0f\u6ca1\u5fc5\u8981\u653e\u5728\u62ec\u53f7\u91cc\u3002","title":"\u52a0\u62ec\u53f7"},{"location":"5-1-Operator-precedence-and-associativity/#_6","text":"\u8003\u8651\u4e0b\u9762\u7684\u8868\u8fbe\u5f0f\uff1a 1 a + b * c \u57fa\u4e8e\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b\uff0c\u6211\u4eec\u6307\u5b9a\u4e0a\u9762\u7684\u8868\u8fbe\u5f0f\u4f1a\u6309\u7167\u4e0b\u9762\u7684\u65b9\u5f0f\u6c42\u503c\uff1a 1 a + ( b * c ) \u5982\u679c a=1 \uff0c b=2 \uff0c c=3 \uff0c\u5219\u6574\u4e2a\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u4e3a 7\u3002 \u4f46\u662f\uff0c\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b\u53ea\u8868\u660e\u8fd0\u7b97\u7b26\u4e4b\u95f4\u7684\u6c42\u503c\u987a\u5e8f\uff0c\u4f46\u5e76\u6ca1\u6709\u8bf4\u660e\u8868\u8fbe\u5f0f\u5176\u4ed6\u90e8\u5206\u7684\u6c42\u503c\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u4e0a\u9762\u4f8b\u5b50\u4e2d a,b,c\u7684\u6c42\u503c\u987a\u5e8f\u662f\u4ec0\u4e48\u5462\uff1f \u4ee4\u4eba\u5403\u60ca\u7684\u662f\uff0c\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u590d\u6742\u8868\u8fbe\u5f0f\u4e2d\u5404\u90e8\u5206\u7684\u6c42\u503c\u987a\u5e8f\u662f\u6ca1\u6709\u660e\u786e\u89c4\u5b9a\u7684\uff08\u5305\u62ec\u51fd\u6570\u8c03\u7528\u548c\u53c2\u6570\u6c42\u503c\uff09\u3002\u56e0\u6b64\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u81ea\u7531\u9009\u62e9\u5b83\u8ba4\u4e3a\u6700\u4f18\u7684\u6c42\u503c\u987a\u5e8f\u3002 \u6ce8\u610f \u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u7b26\u5408\u8868\u8fbe\u5f0f\u4e2d\u64cd\u4f5c\u6570\u53ef\u4ee5\u4ee5\u4efb\u610f\u987a\u5e8f\u6c42\u503c\uff0c\u5305\u62ec\u51fd\u6570\u8c03\u7528\u548c\u53c2\u6570\u6c42\u503c\u3002 \u5bf9\u4e8e\u5927\u591a\u6570\u8868\u8fbe\u5f0f\uff0c\u8fd9\u90fd\u4e0d\u662f\u4ec0\u4e48\u5927\u4e8b\u3002\u4ee5\u4e0a\u9762\u7684\u8868\u8fbe\u5f0f\u4e3a\u4f8b\uff0c\u51e0\u4e2a\u53d8\u91cf\u54ea\u4e2a\u5148\u6c42\u503c\u90fd\u662f\u53ef\u4ee5\u7684\uff0c\u7ed3\u679c\u4e5f\u603b\u662f\u7b49\u4e8e 7\u3002 \u4f46\u662f\uff0c\u786e\u5b9e\u6709\u4e9b\u8868\u8fbe\u5f0f\u662f\u6709\u53ef\u80fd\u53d7\u5f71\u54cd\u7684\u3002\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u5b83\u5176\u4e2d\u5305\u542b\u7684\u9519\u8bef\u662f\u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u90fd\u4f1a\u72af\u7684\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int getValue () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; return x ; } int main () { std :: cout << getValue () + ( getValue () * getValue ()); // a + (b * c) return 0 ; } \u8fd0\u884c\u8be5\u7a0b\u5e8f\u5e76\u4f9d\u6b21\u8f93\u5165 1\u30012 \u548c 3\u7684\u8bdd\uff0c\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u8f93\u51fa\u7ed3\u679c\u5e94\u8be5\u662f 7\u3002\u4f46\u662f\uff0c\u8fd9\u662f\u5efa\u7acb\u5728\u8c03\u7528 getValue() \u4f1a\u4ece\u5de6\u5411\u53f3\u6c42\u503c\u7684\u5047\u8bbe\u4e0a\u7684\u3002\u5b9e\u9645\u4e0a\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u9009\u62e9\u4e0d\u540c\u7684\u6c42\u503c\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u7f16\u8bd1\u5668\u91c7\u7528\u4ece\u53f3\u5411\u5de6\u7684\u987a\u5e8f\u6c42\u503c\uff0c\u5219\u76f8\u540c\u7684\u8f93\u5165\u5f97\u5230\u7684\u7ed3\u679c\u5c31\u662f 5 \uff08 3+(2*1) \uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b\u89c4\u5b9a\u4ee5\u5916\u7684\u60c5\u51b5\uff0c\u90fd\u53ea\u80fd\u5047\u8bbe\u4f1a\u4ee5\u4efb\u610f\u987a\u5e8f\u6c42\u503c\u3002\u786e\u4fdd\u4f60\u7f16\u5199\u7684\u8868\u8fbe\u5f0f\u4e0d\u4f9d\u8d56\u4e8e\u8fd9\u4e9b\u90e8\u5206\u7684\u6c42\u503c\u987a\u5e8f\u3002 \u4e0a\u9762\u7684\u7a0b\u5e8f\u53ef\u4ee5\u7a0d\u52a0\u4fee\u6539\u53d8\u6210\u6ca1\u6709\u6b67\u4e49\u7684\u5f62\u5f0f\uff0c\u5373\u5c06\u51fd\u6570\u8c03\u7528\u4f5c\u4e3a\u5355\u72ec\u7684\u8bed\u53e5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int getValue () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; return x ; } int main () { int a { getValue () }; // will execute first int b { getValue () }; // will execute second int c { getValue () }; // will execute third std :: cout << a + ( b * c ); // order of eval doesn't matter now return 0 ; } \u76f8\u5173\u5185\u5bb9 \u8fd8\u6709\u4e00\u4e9b\u60c5\u51b5\u4e5f\u4f1a\u9020\u6210\u4e0a\u8ff0\u6c42\u503c\u987a\u5e8f\u95ee\u9898\uff0c\u6211\u4eec\u4f1a\u5728 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"\u8868\u8fbe\u5f0f\u6c42\u503c\u987a\u5e8f\u548c\u51fd\u6570\u53c2\u6570\u5904\u7406\u987a\u5e8f\u5927\u591a\u662f\u672a\u6307\u660e\u7684"},{"location":"5-2-Arithmetic-operators/","tags":["operator"],"text":"Key Takeaway \u4f7f\u7528 static_cast \u5bf9\u6574\u578b\u6570\u8fdb\u884c\u6d6e\u70b9\u9664\u6cd5 std::cout << \"double / int = \" << static_cast(x) / y << '\\n'; \u9664 0 (\u6216 0.0) \u5f88\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4f60\u7684\u7a0b\u5e8f\u5d29\u6e83 \u4e00\u5143\u6570\u5b66\u8fd0\u7b97\u7b26 \u4e00\u5143\u8fd0\u7b97\u7b26\u6709\u4e24\u79cd\uff0c\u6b63\u53f7( + )\u548c\u8d1f\u53f7( - )\u3002\u63d0\u9192\u4e00\u4e0b\uff0c\u4e00\u5143\u8fd0\u7b97\u7b26\u53ea\u9700\u8981\u4e00\u4e2a \u64cd\u4f5c\u6570 \u3002 \u8fd0\u7b97\u7b26 \u7b26\u53f7 \u683c\u5f0f \u64cd\u4f5c Unary plus + +x x\u7684\u503c Unary minus - -x x\u7684\u76f8\u53cd\u6570 \u4e00\u5143\u7b26\u53f7\u8fd0\u7b97\u7b26\u8fd4\u56de\u64cd\u4f5c\u6570\u4e58\u4ee5-1\u4e4b\u540e\u7684\u7ed3\u679c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c x = 5\uff0c\u5219 -x \u662f -5\u3002 \u4e00\u5143\u6b63\u53f7\u8fd0\u7b97\u7b26\u8fd4\u56de\u64cd\u4f5c\u6570\u672c\u8eab\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c +5 \u662f 5 \u5219 +x \u662f x\u3002\u4e00\u822c\u6765\u8bf4\u4f60\u4e0d\u9700\u8981\u4f7f\u7528\u5b83\uff0c\u56e0\u4e3a\u662f\u591a\u4f59\u7684\u3002\u53ea\u6709\u4e3a\u4e86\u548c\u4e00\u5143\u8d1f\u53f7\u8fd0\u7b97\u7b26\u5f62\u6210\u5bf9\u79f0\u7684\u60c5\u51b5\u4e0b\u624d\u4f1a\u4f7f\u7528\u5b83\u3002 \u8fd9\u4e9b\u64cd\u4f5c\u7b26\u5e94\u5f53\u653e\u7f6e\u5728\u7d27\u90bb\u7740\u64cd\u4f5c\u6570\u524d\u9762\u7684\u4f4d\u7f6e\uff08\u4f8b\u5982\u5e94\u8be5\u5199\u4f5c -x \u800c\u4e0d\u662f - x \uff09\u3002 \u4e0d\u8981\u628a\u4e00\u5143\u8d1f\u53f7\u8fd0\u7b97\u7b26\u548c\u51cf\u53f7\u641e\u6df7\u4e86\uff0c\u5c3d\u7ba1\u5b83\u4eec\u7684\u7b26\u53f7\u7684\u786e\u662f\u540c\u4e00\u4e2a\u3002\u4f8b\u5982\uff0c\u5728\u8868\u8fbe\u5f0f x = 5 - -3; \u4e2d\uff0c\u7b2c\u4e00\u4e2a - \u662f\u51cf\u53f7\uff0c\u7b2c\u4e8c\u4e2a - \u5219\u662f\u8d1f\u53f7\u3002 \u4e8c\u5143\u6570\u5b66\u8fd0\u7b97\u7b26 \u4e8c\u5143\u6570\u5b66\u8fd0\u7b97\u7b26\u6709 5 \u4e2a\uff0c\u5b83\u4eec\u9700\u8981\u4e24\u4e2a\u64cd\u4f5c\u6570\u3002 \u8fd0\u7b97\u7b26 \u7b26\u53f7 \u5f62\u5f0f \u64cd\u4f5c Addition + x + y x \u52a0 y Subtraction - x - y x \u51cf y Multiplication * x * y x \u4e58 y Division / x / y x \u9664 y Modulus (Remainder) % x % y x \u9664 y \u7684\u4f59\u6570 \u52a0\u51cf\u4e58\u548c\u65e5\u5e38\u751f\u6d3b\u4e2d\u7684\u7528\u6cd5\u662f\u4e00\u6837\u7684\u3002 \u9664\u6cd5\u548c\u6c42\u6a21\uff08\u6c42\u4f59\u6570\uff09\u5219\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u989d\u5916\u7684\u8bf4\u660e\u3002\u6211\u4eec\u4f1a\u5728\u672c\u8282\u8bfe\u4ecb\u7ecd\u9664\u6cd5\uff0c\u4e0b\u8282\u8bfe\u4f1a\u4ecb\u7ecd\u6c42\u4f59\u3002 \u6574\u6570\u9664\u6cd5\u548c\u6d6e\u70b9\u6570\u9664\u6cd5 \u53ef\u4ee5\u7b80\u5355\u5730\u8ba4\u4e3a\u9664\u6cd5\u8fd0\u7b97\u7b26\u6709\u4e24\u79cd\u201d\u6a21\u5f0f\u201c\u3002 \u5982\u679c\u4efb\u4e00(\u6216\u5168\u90e8)\u7684\u64cd\u4f5c\u6570\u662f\u6d6e\u70b9\u503c\uff0c\u5219\u9664\u6cd5\u64cd\u4f5c\u6570\u4f1a\u6267\u884c \u6d6e\u70b9\u9664\u6cd5 \u3002\u6d6e\u70b9\u6570\u9664\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6d6e\u70b9\u503c\uff0c\u5c0f\u6570\u90e8\u5206\u4f1a\u88ab\u4fdd\u7559\u3002\u4f8b\u5982 7.0 / 4 = 1.75 \u3001 7 / 4.0 = 1.75 \u548c 7.0 / 4.0 = 1.75 \u3002\u548c\u5176\u4ed6\u6d6e\u70b9\u8fd0\u7b97\u4e00\u6837\uff0c \u820d\u5165\u8bef\u5dee \u53ef\u80fd\u4f1a\u53d1\u751f\u3002 \u5982\u679c\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u662f\u6574\u578b\uff0c\u5219\u9664\u6cd5\u8fd0\u7b97\u7b26\u4f1a\u6267\u884c\u6574\u578b\u9664\u6cd5\u3002\u6574\u578b\u9664\u6cd5\u4f1a\u4e22\u5f03\u7ed3\u679c\u7684\u5c0f\u6570\u90e8\u5206\u5e76\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\u6570\u3002\u4f8b\u5982 7 / 4 = 1 \uff0c\u56e0\u4e3a\u5c0f\u6570\u90e8\u5206\u88ab\u4e22\u5f03\u4e86\u3002\u7c7b\u4f3c\u7684 -7 / 4 = -1 \u56e0\u4e3a\u5c0f\u6570\u90e8\u5206\u88ab\u4e22\u5f03\u4e86\u3002 \u4f7f\u7528 static_cast \u5bf9\u6574\u578b\u6570\u8fdb\u884c\u6d6e\u70b9\u9664\u6cd5 \u5982\u679c\u6709\u4e24\u4e2a\u6574\u578b\u6570\uff0c\u6211\u5e0c\u671b\u4e0d\u8981\u4e22\u5931\u5b83\u4eec\u505a\u9664\u6cd5\u540e\u7684\u5c0f\u6570\u90e8\u5206\uff0c\u90a3\u4e48\u5e94\u8be5\u600e\u4e48\u505a\u5462\uff1f \u5728 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast \u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 static_cast<> \u5c06\u5b57\u7b26\u8f6c\u6362\u6210\u6574\u578b\u4ee5\u4fbf\u6253\u5370\u65f6\u80fd\u591f\u5c06\u5176\u6253\u5370\u4e3a\u4e00\u4e2a\u6574\u6570\u3002 \u8fd9\u91cc\uff0c\u6211\u4eec\u540c\u6837\u53ef\u4ee5\u7528 static_cast<> \u5c06\u4e00\u4e2a\u6574\u578b\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u4ee5\u4fbf\u4f7f\u7528\u6d6e\u70b9\u6570\u9664\u6cd5\u3002\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 7 }; int y { 4 }; std :: cout << \"int / int = \" << x / y << '\\n' ; std :: cout << \"double / int = \" << static_cast < double > ( x ) / y << '\\n' ; std :: cout << \"int / double = \" << x / static_cast < double > ( y ) << '\\n' ; std :: cout << \"double / double = \" << static_cast < double > ( x ) / static_cast < double > ( y ) << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 int / int = 1 double / int = 1.75 int / double = 1.75 double / double = 1.75 \u4ece\u7ed3\u679c\u6765\u770b\uff0c\u53ea\u8981\u6709\u4efb\u4f55\u4e00\u4e2a\u64cd\u4f5c\u6570\u662f\u6d6e\u70b9\u6570\uff0c\u5c31\u4f1a\u6267\u884c\u6d6e\u70b9\u9664\u6cd5\uff0c\u800c\u4e0d\u662f\u6574\u6570\u9664\u6cd5\u3002 \u9664 0 \u9519\u8bef \u9664 0 (\u6216 0.0) \u5f88\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4f60\u7684\u7a0b\u5e8f\u5d29\u6e83\uff0c\u56e0\u4e3a\u8be5\u8fd0\u7b97\u7684\u7ed3\u679c\u662f\u6ca1\u6709\u6570\u5b66\u5b9a\u4e49\u7684\u3002 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { std :: cout << \"Enter a divisor: \" ; int x {}; std :: cin >> x ; std :: cout << \"12 / \" << x << \" = \" << 12 / x << '\\n' ; return 0 ; } \u5982\u679c\u4f60\u8fd0\u884c\u4e0a\u8ff0\u7a0b\u5e8f\u5e76\u8f93\u5165 0\uff0c\u4f60\u7684\u7a0b\u5e8f\u4f1a\u5d29\u6e83\u6216\u5f02\u5e38\u7ec8\u6b62\u3002\u4f60\u53ef\u4ee5\u4eb2\u81ea\u4f53\u9a8c\u4e00\u4e0b\uff0c\u8fd9\u4e0d\u4f1a\u5bf9\u4f60\u7535\u8111\u6709\u4ec0\u4e48\u5f71\u54cd\u7684\u3002 \u7b97\u6570\u8d4b\u503c\u8fd0\u7b97\u7b26 \u8fd0\u7b97\u7b26 \u7b26\u53f7 \u683c\u5f0f \u64cd\u4f5c Assignment = x = y y \u8d4b\u503c\u7ed9 x Addition assignment += x += y \u628a y \u52a0\u5230 x \u4e0a Subtraction assignment -= x -= y \u4ece x \u4e2d\u51cf\u53bb y Multiplication assignment *= x *= y \u628a x \u4e58\u4e0a y Division assignment /= x /= y \u628a x \u9664 y Modulus assignment %= x %= y \u628a x / y \u7684\u4f59\u6570\u8d4b\u503c\u7ed9 x \u5b66\u5230\u8fd9\u91cc\uff0c\u5982\u679c\u4f60\u9700\u8981\u7ed9\u4e00\u4e2a\u53d8\u91cf\u52a0\u4e0a 4\uff0c\u4f60\u5f88\u53ef\u80fd\u4f1a\u8fd9\u6837\u505a\uff1a 1 x = x + 4 ; // add 4 to existing value of x \u8fd9\u4e48\u505a\u662f\u6ca1\u95ee\u9898\u7684\uff0c\u5c31\u662f\u6709\u4e9b\u5570\u55e6\uff0c\u5b83\u9700\u8981\u4e24\u4e2a\u8fd0\u7b97\u7b26\u624d\u80fd\u5b8c\u6210\uff08 operator+ \u548c operator= \uff09\u3002 \u56e0\u4e3a x = x + 4 \u8fd9\u6837\u7684\u8868\u8fbe\u5f0f\u592a\u5e38\u89c1\u4e86\uff0c\u6240\u4ee5 C++ \u63d0\u4f9b\u4e86\u4e94\u79cd\u540c\u6837\u529f\u80fd\u4f46\u66f4\u65b9\u4fbf\u7684\u8fd0\u7b97\u7b26\u3002 x = x + 4 \u53ef\u4ee5\u66ff\u6362\u4e3a x += 4 \u3002\u800c\u5bf9\u4e8e x = x * y \uff0c\u4f60\u53ef\u4ee5\u5199\u6210 x *= y \u3002 \u56e0\u6b64\uff0c\u4e0a\u8ff0\u4ee3\u7801\u53ef\u4ee5\u53d8\u6210\u4e0b\u9762\u7684\u5f62\u5f0f\uff1a 1 x += 4 ; // add 4 to existing value of x","title":"5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26"},{"location":"5-2-Arithmetic-operators/#_1","text":"\u4e00\u5143\u8fd0\u7b97\u7b26\u6709\u4e24\u79cd\uff0c\u6b63\u53f7( + )\u548c\u8d1f\u53f7( - )\u3002\u63d0\u9192\u4e00\u4e0b\uff0c\u4e00\u5143\u8fd0\u7b97\u7b26\u53ea\u9700\u8981\u4e00\u4e2a \u64cd\u4f5c\u6570 \u3002 \u8fd0\u7b97\u7b26 \u7b26\u53f7 \u683c\u5f0f \u64cd\u4f5c Unary plus + +x x\u7684\u503c Unary minus - -x x\u7684\u76f8\u53cd\u6570 \u4e00\u5143\u7b26\u53f7\u8fd0\u7b97\u7b26\u8fd4\u56de\u64cd\u4f5c\u6570\u4e58\u4ee5-1\u4e4b\u540e\u7684\u7ed3\u679c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c x = 5\uff0c\u5219 -x \u662f -5\u3002 \u4e00\u5143\u6b63\u53f7\u8fd0\u7b97\u7b26\u8fd4\u56de\u64cd\u4f5c\u6570\u672c\u8eab\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c +5 \u662f 5 \u5219 +x \u662f x\u3002\u4e00\u822c\u6765\u8bf4\u4f60\u4e0d\u9700\u8981\u4f7f\u7528\u5b83\uff0c\u56e0\u4e3a\u662f\u591a\u4f59\u7684\u3002\u53ea\u6709\u4e3a\u4e86\u548c\u4e00\u5143\u8d1f\u53f7\u8fd0\u7b97\u7b26\u5f62\u6210\u5bf9\u79f0\u7684\u60c5\u51b5\u4e0b\u624d\u4f1a\u4f7f\u7528\u5b83\u3002 \u8fd9\u4e9b\u64cd\u4f5c\u7b26\u5e94\u5f53\u653e\u7f6e\u5728\u7d27\u90bb\u7740\u64cd\u4f5c\u6570\u524d\u9762\u7684\u4f4d\u7f6e\uff08\u4f8b\u5982\u5e94\u8be5\u5199\u4f5c -x \u800c\u4e0d\u662f - x \uff09\u3002 \u4e0d\u8981\u628a\u4e00\u5143\u8d1f\u53f7\u8fd0\u7b97\u7b26\u548c\u51cf\u53f7\u641e\u6df7\u4e86\uff0c\u5c3d\u7ba1\u5b83\u4eec\u7684\u7b26\u53f7\u7684\u786e\u662f\u540c\u4e00\u4e2a\u3002\u4f8b\u5982\uff0c\u5728\u8868\u8fbe\u5f0f x = 5 - -3; \u4e2d\uff0c\u7b2c\u4e00\u4e2a - \u662f\u51cf\u53f7\uff0c\u7b2c\u4e8c\u4e2a - \u5219\u662f\u8d1f\u53f7\u3002","title":"\u4e00\u5143\u6570\u5b66\u8fd0\u7b97\u7b26"},{"location":"5-2-Arithmetic-operators/#_2","text":"\u4e8c\u5143\u6570\u5b66\u8fd0\u7b97\u7b26\u6709 5 \u4e2a\uff0c\u5b83\u4eec\u9700\u8981\u4e24\u4e2a\u64cd\u4f5c\u6570\u3002 \u8fd0\u7b97\u7b26 \u7b26\u53f7 \u5f62\u5f0f \u64cd\u4f5c Addition + x + y x \u52a0 y Subtraction - x - y x \u51cf y Multiplication * x * y x \u4e58 y Division / x / y x \u9664 y Modulus (Remainder) % x % y x \u9664 y \u7684\u4f59\u6570 \u52a0\u51cf\u4e58\u548c\u65e5\u5e38\u751f\u6d3b\u4e2d\u7684\u7528\u6cd5\u662f\u4e00\u6837\u7684\u3002 \u9664\u6cd5\u548c\u6c42\u6a21\uff08\u6c42\u4f59\u6570\uff09\u5219\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u989d\u5916\u7684\u8bf4\u660e\u3002\u6211\u4eec\u4f1a\u5728\u672c\u8282\u8bfe\u4ecb\u7ecd\u9664\u6cd5\uff0c\u4e0b\u8282\u8bfe\u4f1a\u4ecb\u7ecd\u6c42\u4f59\u3002","title":"\u4e8c\u5143\u6570\u5b66\u8fd0\u7b97\u7b26"},{"location":"5-2-Arithmetic-operators/#_3","text":"\u53ef\u4ee5\u7b80\u5355\u5730\u8ba4\u4e3a\u9664\u6cd5\u8fd0\u7b97\u7b26\u6709\u4e24\u79cd\u201d\u6a21\u5f0f\u201c\u3002 \u5982\u679c\u4efb\u4e00(\u6216\u5168\u90e8)\u7684\u64cd\u4f5c\u6570\u662f\u6d6e\u70b9\u503c\uff0c\u5219\u9664\u6cd5\u64cd\u4f5c\u6570\u4f1a\u6267\u884c \u6d6e\u70b9\u9664\u6cd5 \u3002\u6d6e\u70b9\u6570\u9664\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6d6e\u70b9\u503c\uff0c\u5c0f\u6570\u90e8\u5206\u4f1a\u88ab\u4fdd\u7559\u3002\u4f8b\u5982 7.0 / 4 = 1.75 \u3001 7 / 4.0 = 1.75 \u548c 7.0 / 4.0 = 1.75 \u3002\u548c\u5176\u4ed6\u6d6e\u70b9\u8fd0\u7b97\u4e00\u6837\uff0c \u820d\u5165\u8bef\u5dee \u53ef\u80fd\u4f1a\u53d1\u751f\u3002 \u5982\u679c\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u662f\u6574\u578b\uff0c\u5219\u9664\u6cd5\u8fd0\u7b97\u7b26\u4f1a\u6267\u884c\u6574\u578b\u9664\u6cd5\u3002\u6574\u578b\u9664\u6cd5\u4f1a\u4e22\u5f03\u7ed3\u679c\u7684\u5c0f\u6570\u90e8\u5206\u5e76\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\u6570\u3002\u4f8b\u5982 7 / 4 = 1 \uff0c\u56e0\u4e3a\u5c0f\u6570\u90e8\u5206\u88ab\u4e22\u5f03\u4e86\u3002\u7c7b\u4f3c\u7684 -7 / 4 = -1 \u56e0\u4e3a\u5c0f\u6570\u90e8\u5206\u88ab\u4e22\u5f03\u4e86\u3002","title":"\u6574\u6570\u9664\u6cd5\u548c\u6d6e\u70b9\u6570\u9664\u6cd5"},{"location":"5-2-Arithmetic-operators/#static_cast","text":"\u5982\u679c\u6709\u4e24\u4e2a\u6574\u578b\u6570\uff0c\u6211\u5e0c\u671b\u4e0d\u8981\u4e22\u5931\u5b83\u4eec\u505a\u9664\u6cd5\u540e\u7684\u5c0f\u6570\u90e8\u5206\uff0c\u90a3\u4e48\u5e94\u8be5\u600e\u4e48\u505a\u5462\uff1f \u5728 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast \u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 static_cast<> \u5c06\u5b57\u7b26\u8f6c\u6362\u6210\u6574\u578b\u4ee5\u4fbf\u6253\u5370\u65f6\u80fd\u591f\u5c06\u5176\u6253\u5370\u4e3a\u4e00\u4e2a\u6574\u6570\u3002 \u8fd9\u91cc\uff0c\u6211\u4eec\u540c\u6837\u53ef\u4ee5\u7528 static_cast<> \u5c06\u4e00\u4e2a\u6574\u578b\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u4ee5\u4fbf\u4f7f\u7528\u6d6e\u70b9\u6570\u9664\u6cd5\u3002\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 7 }; int y { 4 }; std :: cout << \"int / int = \" << x / y << '\\n' ; std :: cout << \"double / int = \" << static_cast < double > ( x ) / y << '\\n' ; std :: cout << \"int / double = \" << x / static_cast < double > ( y ) << '\\n' ; std :: cout << \"double / double = \" << static_cast < double > ( x ) / static_cast < double > ( y ) << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 int / int = 1 double / int = 1.75 int / double = 1.75 double / double = 1.75 \u4ece\u7ed3\u679c\u6765\u770b\uff0c\u53ea\u8981\u6709\u4efb\u4f55\u4e00\u4e2a\u64cd\u4f5c\u6570\u662f\u6d6e\u70b9\u6570\uff0c\u5c31\u4f1a\u6267\u884c\u6d6e\u70b9\u9664\u6cd5\uff0c\u800c\u4e0d\u662f\u6574\u6570\u9664\u6cd5\u3002","title":"\u4f7f\u7528 static_cast \u5bf9\u6574\u578b\u6570\u8fdb\u884c\u6d6e\u70b9\u9664\u6cd5"},{"location":"5-2-Arithmetic-operators/#0","text":"\u9664 0 (\u6216 0.0) \u5f88\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4f60\u7684\u7a0b\u5e8f\u5d29\u6e83\uff0c\u56e0\u4e3a\u8be5\u8fd0\u7b97\u7684\u7ed3\u679c\u662f\u6ca1\u6709\u6570\u5b66\u5b9a\u4e49\u7684\u3002 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { std :: cout << \"Enter a divisor: \" ; int x {}; std :: cin >> x ; std :: cout << \"12 / \" << x << \" = \" << 12 / x << '\\n' ; return 0 ; } \u5982\u679c\u4f60\u8fd0\u884c\u4e0a\u8ff0\u7a0b\u5e8f\u5e76\u8f93\u5165 0\uff0c\u4f60\u7684\u7a0b\u5e8f\u4f1a\u5d29\u6e83\u6216\u5f02\u5e38\u7ec8\u6b62\u3002\u4f60\u53ef\u4ee5\u4eb2\u81ea\u4f53\u9a8c\u4e00\u4e0b\uff0c\u8fd9\u4e0d\u4f1a\u5bf9\u4f60\u7535\u8111\u6709\u4ec0\u4e48\u5f71\u54cd\u7684\u3002","title":"\u9664 0 \u9519\u8bef"},{"location":"5-2-Arithmetic-operators/#_4","text":"\u8fd0\u7b97\u7b26 \u7b26\u53f7 \u683c\u5f0f \u64cd\u4f5c Assignment = x = y y \u8d4b\u503c\u7ed9 x Addition assignment += x += y \u628a y \u52a0\u5230 x \u4e0a Subtraction assignment -= x -= y \u4ece x \u4e2d\u51cf\u53bb y Multiplication assignment *= x *= y \u628a x \u4e58\u4e0a y Division assignment /= x /= y \u628a x \u9664 y Modulus assignment %= x %= y \u628a x / y \u7684\u4f59\u6570\u8d4b\u503c\u7ed9 x \u5b66\u5230\u8fd9\u91cc\uff0c\u5982\u679c\u4f60\u9700\u8981\u7ed9\u4e00\u4e2a\u53d8\u91cf\u52a0\u4e0a 4\uff0c\u4f60\u5f88\u53ef\u80fd\u4f1a\u8fd9\u6837\u505a\uff1a 1 x = x + 4 ; // add 4 to existing value of x \u8fd9\u4e48\u505a\u662f\u6ca1\u95ee\u9898\u7684\uff0c\u5c31\u662f\u6709\u4e9b\u5570\u55e6\uff0c\u5b83\u9700\u8981\u4e24\u4e2a\u8fd0\u7b97\u7b26\u624d\u80fd\u5b8c\u6210\uff08 operator+ \u548c operator= \uff09\u3002 \u56e0\u4e3a x = x + 4 \u8fd9\u6837\u7684\u8868\u8fbe\u5f0f\u592a\u5e38\u89c1\u4e86\uff0c\u6240\u4ee5 C++ \u63d0\u4f9b\u4e86\u4e94\u79cd\u540c\u6837\u529f\u80fd\u4f46\u66f4\u65b9\u4fbf\u7684\u8fd0\u7b97\u7b26\u3002 x = x + 4 \u53ef\u4ee5\u66ff\u6362\u4e3a x += 4 \u3002\u800c\u5bf9\u4e8e x = x * y \uff0c\u4f60\u53ef\u4ee5\u5199\u6210 x *= y \u3002 \u56e0\u6b64\uff0c\u4e0a\u8ff0\u4ee3\u7801\u53ef\u4ee5\u53d8\u6210\u4e0b\u9762\u7684\u5f62\u5f0f\uff1a 1 x += 4 ; // add 4 to existing value of x","title":"\u7b97\u6570\u8d4b\u503c\u8fd0\u7b97\u7b26"},{"location":"5-3-Modulus-and-Exponentiation/","tags":["operator"],"text":"Key Takeaway \u5728\u9700\u8981\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u80fd\u88ab\u53e6\u5916\u4e00\u4e2a\u6570\u6574\u9664\uff08\u9664\u6cd5\u7ed3\u679c\u6ca1\u6709\u4f59\u6570\uff09\u65f6\u6c42\u6a21\u8fd0\u7b97\u975e\u5e38\u6709\uff1a\u5982\u679c x%y \u7ed3\u679c\u4e3a0\uff0c\u8bf4\u660e x \u53ef\u4ee5\u88ab y \u6574\u9664\u3002 \u8d1f\u6570\u4e5f\u53ef\u4ee5\u6c42\u6a21\u3002 x % y \u7684\u7ed3\u679c\u603b\u662f\u548c x \u7684\u7b26\u53f7\u4e00\u6837\u3002 C++ \u4e0d\u652f\u6301\u6307\u6570\u8fd0\u7b97\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528 \u4e2d\u7684 pow \u51fd\u6570 \u7f16\u5199\u6574\u578b\u6307\u6570\u8fd0\u7b97\u51fd\u6570\u53ef\u4ee5\u4f7f\u7528\u5e73\u65b9\u6c42\u5e42\u6cd5 \u6c42\u6a21\u8fd0\u7b97\u7b26 \u6c42\u6a21\u8fd0\u7b97\u7b26\uff08\u6709\u65f6\u5019\u4e5f\u88ab\u53eb\u505a\u6c42\u4f59\u8fd0\u7b97\u7b26\uff09\u53ef\u4ee5\u8fd4\u56de\u6574\u6570\u9664\u6cd5\u7ed3\u679c\u7684\u4f59\u6570\u3002\u4f8b\u5982 7 / 4 = 1 \u4f59 3\u3002\u56e0\u6b64 7 % 4 = 3\u3002\u53c8\u6bd4\u5982\uff0c25 / 7 = 3 \u4f59 4\uff0c\u56e0\u6b64 25 % 7 = 4\u3002\u6c42\u6a21\u8fd0\u7b97\u53ea\u80fd\u7528\u4e8e\u6574\u6570\u64cd\u4f5c\u6570\u3002 \u5728\u9700\u8981\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u80fd\u88ab\u53e6\u5916\u4e00\u4e2a\u6570\u6574\u9664\uff08\u9664\u6cd5\u7ed3\u679c\u6ca1\u6709\u4f59\u6570\uff09\u65f6\u6c42\u6a21\u8fd0\u7b97\u975e\u5e38\u6709\uff1a\u5982\u679c x%y \u7ed3\u679c\u4e3a0\uff0c\u8bf4\u660e x \u53ef\u4ee5\u88ab y \u6574\u9664\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another integer: \" ; int y {}; std :: cin >> y ; std :: cout << \"The remainder is: \" << x % y << '\\n' ; if (( x % y ) == 0 ) std :: cout << x << \" is evenly divisible by \" << y << '\\n' ; else std :: cout << x << \" is not evenly divisible by \" << y << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 Enter an integer: 6 Enter another integer: 3 The remainder is: 0 6 is evenly divisible by 3 1 2 3 4 Enter an integer: 6 Enter another integer: 4 The remainder is: 2 6 is not evenly divisible by 4 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5b9e\u65f6\u5982\u679c\u7b2c\u4e8c\u4e2a\u6570\u6bd4\u7b2c\u4e00\u4e2a\u6570\u5927\u4f1a\u600e\u4e48\u6837\uff1a 1 2 3 4 Enter an integer: 2 Enter another integer: 4 The remainder is: 2 2 is not evenly divisible by 4 \u4f59\u6570\u662f 2 \uff0c\u4e4d\u4e00\u770b\u539f\u56e0\u53ef\u80fd\u4e0d\u662f\u5f88\u660e\u663e\uff0c\u4f46\u5176\u5b9e\u5f88\u7b80\u5355\uff1a2 / 4 \u662f 0 (\u6574\u6570\u9664\u6cd5) \u4f59\u6570\u4e3a 2\u3002\u4efb\u4f55\u65f6\u5019\u53ea\u8981\u7b2c\u4e8c\u4e2a\u6570\u5927\u4e8e\u7b2c\u4e00\u4e2a\uff0c\u5219\u7b2c\u4e00\u4e2a\u6570\u4e3a\u4f59\u6570\u3002 \u8d1f\u6570\u6c42\u6a21 \u8d1f\u6570\u4e5f\u53ef\u4ee5\u6c42\u6a21\u3002 x % y \u7684\u7ed3\u679c\u603b\u662f\u548c x \u7684\u7b26\u53f7\u4e00\u6837\u3002 \u6267\u884c\u4e0a\u8ff0\u7a0b\u5e8f\uff1a 1 2 3 4 Enter an integer: -6 Enter another integer: 4 The remainder is: -2 -6 is not evenly divisible by 4 1 2 3 4 Enter an integer: 6 Enter another integer: -4 The remainder is: 2 6 is not evenly divisible by -4 \u5728\u4e0a\u9762\u4e24\u79cd\u4f8b\u5b50\u4e2d\u53ef\u4ee5\u770b\u5230\u4f59\u6570\u7684\u7b26\u53f7\u603b\u662f\u548c\u7b2c\u4e00\u4e2a\u64cd\u4f5c\u7b26\u4e00\u6837\u3002 \u6307\u6570\u8fd0\u7b97\u7b26\u53bb\u54ea\u4e86? \u4f60\u53ef\u80fd\u5df2\u7ecf\u6ce8\u610f\u5230\u4e86 ^ \u8fd0\u7b97\u7b26(\u901a\u5e38\u7528\u4e8e\u8868\u793a\u6307\u6570\u8fd0\u7b97)\u5728 C++ \u4e2d\u662f\u5f02\u6216\u8fd0\u7b97\u7b26 (\u5728 O.3 -- Bit manipulation with bitwise operators and bit masks \u4e2d\u4ecb\u7ecd)\u3002C++ \u5e76\u6ca1\u6709\u6307\u6570\u8fd0\u7b97\u7b26\u3002 \u5728 C++ \u4e2d\u8fdb\u884c\u6307\u6570\u8fd0\u7b97\u9700\u8981 #include \u5934\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528 pow() \u51fd\u6570\uff1a 1 2 3 #include double x { std :: pow ( 3.0 , 4.0 ) }; // 3 to the 4th power \u6ce8\u610f\uff0c\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u90fd\u662f double\u3002\u800c\u7531\u4e8e\u6d6e\u70b9\u6570\u5b58\u5728 \u820d\u5165\u8bef\u5dee \uff0c\u6240\u4ee5 pow() \u7684\u7ed3\u679c\u53ef\u80fd\u5e76\u4e0d\u7cbe\u786e\uff08\u5373\u4f7f\u4f60\u4f20\u5165\u7684\u662f\u6574\u6570\uff09\u3002 \u5982\u679c\u4f60\u60f3\u8fdb\u884c\u6574\u6570\u6307\u6570\u8fd0\u7b97\uff0c\u4f60\u6700\u597d\u81ea\u5df1\u5199\u4e00\u4e2a\u51fd\u6570\u3002\u4e0b\u9762\u8fd9\u4e2a\u51fd\u6570\u5c31\u5b9e\u73b0\u4e86\u6574\u578b\u7684\u6307\u6570\u8fd0\u7b97\uff08\u4f7f\u7528\u4e86\u4e0d\u592a\u76f4\u89c2\u7684 \u5e73\u65b9\u6c42\u5e42 \u7b97\u6cd5\u4ee5\u63d0\u9ad8\u6548\u7387\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include // for std::int64_t #include // for assert // note: exp must be non-negative std :: int64_t powint ( int base , int exp ) { assert ( exp > 0 && \"powint: exp parameter has negative value\" ); std :: int64_t result { 1 }; while ( exp ) { if ( exp & 1 ) result *= base ; exp >>= 1 ; base *= base ; } return result ; } int main () { std :: cout << powint ( 7 , 12 ); // 7 to the 12th power return 0 ; } \u8bd1\u8005\u6ce8 \u6c42x\u7684n\u6b21\u65b9\uff0c\u6700\u76f4\u63a5\u7684\u7b97\u6cd5\u662f\u628ax\u4e58\u4ee5x\u6267\u884cn\u6b21\uff0c\u4f46\u662f\u8fd9\u6837\u5f88\u6162\u3002 \u4f7f\u7528\u5e73\u65b9\u6c42\u5e42\u7684\u65b9\u6cd5\u53ef\u4ee5\u6781\u5927\u63d0\u9ad8\u901f\u5ea6\uff0c\u5b83\u7684\u539f\u7406\u5982\u4e0b\uff1a \u5047\u8bbe\u6c42x\u768416\u6b21\u65b9\uff0c\u5176\u5b9e\u6ca1\u5fc5\u8981\u628ax\u4e58\u4ee5x\u6267\u884c16\u6b21 \u66f4\u5feb\u7684\u65b9\u6cd5\u662f\u628ax\u5e73\u65b9\uff0c\u7136\u540e\u518d\u5e73\u65b9\uff0c\u518d\u5e73\u65b9\uff0c\u518d\u5e73\u65b9\u3002\u5373 \\(((((x^2)^2)^2)^2)\\) \uff0c\u56e0\u4e3a\u6211\u4eec\u77e5\u9053\u6c42\u6307\u6570\u7684\u6307\u6570\u5b9e\u9645\u4e0a\u662f\u628a\u4e24\u4e2a\u6307\u6570\u76f8\u4e58\u3002\u6240\u4ee5 \\(((((x^2)^2)^2)^2)=x^{16}\\) \u5982\u679c\u8f6c\u6362\u4e3a\u66f4\u4e00\u822c\u7684\u5f62\u5f0f\uff0cx\u7684n\u6b21\u65b9\u5176\u5b9e\u5c31\u662f\u770bn\u8f6c\u6362\u62102\u8fdb\u5236\u4e4b\u540e\uff0c\u91cc\u9762\u5305\u542b\u591a\u5c11\u4e2a1\u3002\u5047\u8bben = 37\uff0c\u5219\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u662f 100101 \u3002 \\(x^{37} = x^1 * x^{100} * x^{10000}\\) , \u5373 \\(x^{37} = x^1 * x^{4} * x^{32}\\) \u6240\u4ee5\uff0c\u6211\u4eec\u53ea\u8981\u628a\u6307\u6570\u5148\u8f6c\u53d8\u6210\u4e8c\u8fdb\u5236\uff0c\u7136\u540e\u4e00\u4f4d\u4e00\u4f4d\u7684\u770b\uff0c\u5982\u679c\u9047\u52301\uff0c\u5219\u770b\u8be5\u4f4d\u662f\u7b2c\u51e0\u4f4d\uff0c\u7136\u540e\u6c422\u7684\u51e0\u6b21\u65b9\uff0c\u5047\u8bbe\u7ed3\u679c\u4e3an\uff0c\u5219\u518d\u6b21\u6c42x\u7684n\u6b21\u65b9\uff0c\u6700\u540e\u628a\u6240\u6709\u7684\u7ed3\u679c\u4e58\u8d77\u6765\u3002 \u4e0d\u8fc7\u6309\u7167\u4e0a\u9762\u8ba1\u7b97\u6bcf\u4e2a\u90e8\u5206\u7684\u65f6\u5019\u8fd8\u662f\u5f88\u9ebb\u70e6\uff0c\u5b9e\u9645\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a \u4e0d\u8bba\u662f1\u8fd8\u662f0\uff0c\u90fd\u628ax\u81ea\u8eab\u6210\u4ee5\u81ea\u8eab \u5982\u679c\u9047\u52301\uff0c\u5219\u628ax\u7684\u7ed3\u679c\u4e58\u5230result\u4e0a \u5982\u679c\u9047\u52300\uff0c\u8df3\u8fc7 \u6700\u540e\u7684\u7ed3\u679c\u76f8\u5f53\u4e8e\u6bcf\u5904\u7406\u6307\u6570\u7684\u4e00\u4e2a\u4f4d\uff0cx\u90fd\u5e73\u65b9\u4e00\u6b21\u3002\u7136\u540e\u53ea\u6709a\u4f4d\u4e0a\u4e3a1\u65f6\uff0cx\u7684a\u6b21\u65b9\u624d\u88ab\u4e58\u5230\u7ed3\u679c\u4e0a\uff0c\u8fd9\u6837\u5c31\u5f88\u81ea\u7136\uff0c\u4e0d\u7528\u518d\u7279\u610f\u53bb\u8ba1\u7b97\u5f53\u524d\u662f\u7b2c\u51e0\u4f4d\u4e86\u3002 \u89c6\u9891\u8bb2\u89e3 \u7ed3\u679c\u4e3a\uff1a 1 13841287201 \u5982\u679c\u4f60\u4e0d\u7406\u89e3\u8fd9\u4e2a\u51fd\u6570\u7684\u539f\u7406\u4e5f\u4e0d\u8981\u7d27 \u2014\u2014 \u4f7f\u7528\u5b83\u5e76\u4e0d\u9700\u8981\u4f60\u4e86\u89e3\u5b83\u7684\u539f\u7406\u3002 \u6ce8\u610f \u5927\u90e8\u5206\u60c5\u51b5\u4e0b\uff0c\u6574\u578b\u7684\u6307\u6570\u8fd0\u7b97\u90fd\u53ef\u80fd\u4f1a\u6ea2\u51fa\u6574\u578b\u3002\u8fd9\u5f88\u53ef\u80fd\u662f\u8be5\u51fd\u6570\u6ca1\u6709\u88ab\u5305\u542b\u5230\u6807\u51c6\u5e93\u4e2d\u7684\u539f\u56e0\u3002","title":"5.3 - \u6c42\u6a21\u548c\u6307\u6570\u8fd0\u7b97"},{"location":"5-3-Modulus-and-Exponentiation/#_1","text":"\u6c42\u6a21\u8fd0\u7b97\u7b26\uff08\u6709\u65f6\u5019\u4e5f\u88ab\u53eb\u505a\u6c42\u4f59\u8fd0\u7b97\u7b26\uff09\u53ef\u4ee5\u8fd4\u56de\u6574\u6570\u9664\u6cd5\u7ed3\u679c\u7684\u4f59\u6570\u3002\u4f8b\u5982 7 / 4 = 1 \u4f59 3\u3002\u56e0\u6b64 7 % 4 = 3\u3002\u53c8\u6bd4\u5982\uff0c25 / 7 = 3 \u4f59 4\uff0c\u56e0\u6b64 25 % 7 = 4\u3002\u6c42\u6a21\u8fd0\u7b97\u53ea\u80fd\u7528\u4e8e\u6574\u6570\u64cd\u4f5c\u6570\u3002 \u5728\u9700\u8981\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u80fd\u88ab\u53e6\u5916\u4e00\u4e2a\u6570\u6574\u9664\uff08\u9664\u6cd5\u7ed3\u679c\u6ca1\u6709\u4f59\u6570\uff09\u65f6\u6c42\u6a21\u8fd0\u7b97\u975e\u5e38\u6709\uff1a\u5982\u679c x%y \u7ed3\u679c\u4e3a0\uff0c\u8bf4\u660e x \u53ef\u4ee5\u88ab y \u6574\u9664\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another integer: \" ; int y {}; std :: cin >> y ; std :: cout << \"The remainder is: \" << x % y << '\\n' ; if (( x % y ) == 0 ) std :: cout << x << \" is evenly divisible by \" << y << '\\n' ; else std :: cout << x << \" is not evenly divisible by \" << y << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 Enter an integer: 6 Enter another integer: 3 The remainder is: 0 6 is evenly divisible by 3 1 2 3 4 Enter an integer: 6 Enter another integer: 4 The remainder is: 2 6 is not evenly divisible by 4 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5b9e\u65f6\u5982\u679c\u7b2c\u4e8c\u4e2a\u6570\u6bd4\u7b2c\u4e00\u4e2a\u6570\u5927\u4f1a\u600e\u4e48\u6837\uff1a 1 2 3 4 Enter an integer: 2 Enter another integer: 4 The remainder is: 2 2 is not evenly divisible by 4 \u4f59\u6570\u662f 2 \uff0c\u4e4d\u4e00\u770b\u539f\u56e0\u53ef\u80fd\u4e0d\u662f\u5f88\u660e\u663e\uff0c\u4f46\u5176\u5b9e\u5f88\u7b80\u5355\uff1a2 / 4 \u662f 0 (\u6574\u6570\u9664\u6cd5) \u4f59\u6570\u4e3a 2\u3002\u4efb\u4f55\u65f6\u5019\u53ea\u8981\u7b2c\u4e8c\u4e2a\u6570\u5927\u4e8e\u7b2c\u4e00\u4e2a\uff0c\u5219\u7b2c\u4e00\u4e2a\u6570\u4e3a\u4f59\u6570\u3002","title":"\u6c42\u6a21\u8fd0\u7b97\u7b26"},{"location":"5-3-Modulus-and-Exponentiation/#_2","text":"\u8d1f\u6570\u4e5f\u53ef\u4ee5\u6c42\u6a21\u3002 x % y \u7684\u7ed3\u679c\u603b\u662f\u548c x \u7684\u7b26\u53f7\u4e00\u6837\u3002 \u6267\u884c\u4e0a\u8ff0\u7a0b\u5e8f\uff1a 1 2 3 4 Enter an integer: -6 Enter another integer: 4 The remainder is: -2 -6 is not evenly divisible by 4 1 2 3 4 Enter an integer: 6 Enter another integer: -4 The remainder is: 2 6 is not evenly divisible by -4 \u5728\u4e0a\u9762\u4e24\u79cd\u4f8b\u5b50\u4e2d\u53ef\u4ee5\u770b\u5230\u4f59\u6570\u7684\u7b26\u53f7\u603b\u662f\u548c\u7b2c\u4e00\u4e2a\u64cd\u4f5c\u7b26\u4e00\u6837\u3002","title":"\u8d1f\u6570\u6c42\u6a21"},{"location":"5-3-Modulus-and-Exponentiation/#_3","text":"\u4f60\u53ef\u80fd\u5df2\u7ecf\u6ce8\u610f\u5230\u4e86 ^ \u8fd0\u7b97\u7b26(\u901a\u5e38\u7528\u4e8e\u8868\u793a\u6307\u6570\u8fd0\u7b97)\u5728 C++ \u4e2d\u662f\u5f02\u6216\u8fd0\u7b97\u7b26 (\u5728 O.3 -- Bit manipulation with bitwise operators and bit masks \u4e2d\u4ecb\u7ecd)\u3002C++ \u5e76\u6ca1\u6709\u6307\u6570\u8fd0\u7b97\u7b26\u3002 \u5728 C++ \u4e2d\u8fdb\u884c\u6307\u6570\u8fd0\u7b97\u9700\u8981 #include \u5934\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528 pow() \u51fd\u6570\uff1a 1 2 3 #include double x { std :: pow ( 3.0 , 4.0 ) }; // 3 to the 4th power \u6ce8\u610f\uff0c\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u90fd\u662f double\u3002\u800c\u7531\u4e8e\u6d6e\u70b9\u6570\u5b58\u5728 \u820d\u5165\u8bef\u5dee \uff0c\u6240\u4ee5 pow() \u7684\u7ed3\u679c\u53ef\u80fd\u5e76\u4e0d\u7cbe\u786e\uff08\u5373\u4f7f\u4f60\u4f20\u5165\u7684\u662f\u6574\u6570\uff09\u3002 \u5982\u679c\u4f60\u60f3\u8fdb\u884c\u6574\u6570\u6307\u6570\u8fd0\u7b97\uff0c\u4f60\u6700\u597d\u81ea\u5df1\u5199\u4e00\u4e2a\u51fd\u6570\u3002\u4e0b\u9762\u8fd9\u4e2a\u51fd\u6570\u5c31\u5b9e\u73b0\u4e86\u6574\u578b\u7684\u6307\u6570\u8fd0\u7b97\uff08\u4f7f\u7528\u4e86\u4e0d\u592a\u76f4\u89c2\u7684 \u5e73\u65b9\u6c42\u5e42 \u7b97\u6cd5\u4ee5\u63d0\u9ad8\u6548\u7387\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include // for std::int64_t #include // for assert // note: exp must be non-negative std :: int64_t powint ( int base , int exp ) { assert ( exp > 0 && \"powint: exp parameter has negative value\" ); std :: int64_t result { 1 }; while ( exp ) { if ( exp & 1 ) result *= base ; exp >>= 1 ; base *= base ; } return result ; } int main () { std :: cout << powint ( 7 , 12 ); // 7 to the 12th power return 0 ; } \u8bd1\u8005\u6ce8 \u6c42x\u7684n\u6b21\u65b9\uff0c\u6700\u76f4\u63a5\u7684\u7b97\u6cd5\u662f\u628ax\u4e58\u4ee5x\u6267\u884cn\u6b21\uff0c\u4f46\u662f\u8fd9\u6837\u5f88\u6162\u3002 \u4f7f\u7528\u5e73\u65b9\u6c42\u5e42\u7684\u65b9\u6cd5\u53ef\u4ee5\u6781\u5927\u63d0\u9ad8\u901f\u5ea6\uff0c\u5b83\u7684\u539f\u7406\u5982\u4e0b\uff1a \u5047\u8bbe\u6c42x\u768416\u6b21\u65b9\uff0c\u5176\u5b9e\u6ca1\u5fc5\u8981\u628ax\u4e58\u4ee5x\u6267\u884c16\u6b21 \u66f4\u5feb\u7684\u65b9\u6cd5\u662f\u628ax\u5e73\u65b9\uff0c\u7136\u540e\u518d\u5e73\u65b9\uff0c\u518d\u5e73\u65b9\uff0c\u518d\u5e73\u65b9\u3002\u5373 \\(((((x^2)^2)^2)^2)\\) \uff0c\u56e0\u4e3a\u6211\u4eec\u77e5\u9053\u6c42\u6307\u6570\u7684\u6307\u6570\u5b9e\u9645\u4e0a\u662f\u628a\u4e24\u4e2a\u6307\u6570\u76f8\u4e58\u3002\u6240\u4ee5 \\(((((x^2)^2)^2)^2)=x^{16}\\) \u5982\u679c\u8f6c\u6362\u4e3a\u66f4\u4e00\u822c\u7684\u5f62\u5f0f\uff0cx\u7684n\u6b21\u65b9\u5176\u5b9e\u5c31\u662f\u770bn\u8f6c\u6362\u62102\u8fdb\u5236\u4e4b\u540e\uff0c\u91cc\u9762\u5305\u542b\u591a\u5c11\u4e2a1\u3002\u5047\u8bben = 37\uff0c\u5219\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u662f 100101 \u3002 \\(x^{37} = x^1 * x^{100} * x^{10000}\\) , \u5373 \\(x^{37} = x^1 * x^{4} * x^{32}\\) \u6240\u4ee5\uff0c\u6211\u4eec\u53ea\u8981\u628a\u6307\u6570\u5148\u8f6c\u53d8\u6210\u4e8c\u8fdb\u5236\uff0c\u7136\u540e\u4e00\u4f4d\u4e00\u4f4d\u7684\u770b\uff0c\u5982\u679c\u9047\u52301\uff0c\u5219\u770b\u8be5\u4f4d\u662f\u7b2c\u51e0\u4f4d\uff0c\u7136\u540e\u6c422\u7684\u51e0\u6b21\u65b9\uff0c\u5047\u8bbe\u7ed3\u679c\u4e3an\uff0c\u5219\u518d\u6b21\u6c42x\u7684n\u6b21\u65b9\uff0c\u6700\u540e\u628a\u6240\u6709\u7684\u7ed3\u679c\u4e58\u8d77\u6765\u3002 \u4e0d\u8fc7\u6309\u7167\u4e0a\u9762\u8ba1\u7b97\u6bcf\u4e2a\u90e8\u5206\u7684\u65f6\u5019\u8fd8\u662f\u5f88\u9ebb\u70e6\uff0c\u5b9e\u9645\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a \u4e0d\u8bba\u662f1\u8fd8\u662f0\uff0c\u90fd\u628ax\u81ea\u8eab\u6210\u4ee5\u81ea\u8eab \u5982\u679c\u9047\u52301\uff0c\u5219\u628ax\u7684\u7ed3\u679c\u4e58\u5230result\u4e0a \u5982\u679c\u9047\u52300\uff0c\u8df3\u8fc7 \u6700\u540e\u7684\u7ed3\u679c\u76f8\u5f53\u4e8e\u6bcf\u5904\u7406\u6307\u6570\u7684\u4e00\u4e2a\u4f4d\uff0cx\u90fd\u5e73\u65b9\u4e00\u6b21\u3002\u7136\u540e\u53ea\u6709a\u4f4d\u4e0a\u4e3a1\u65f6\uff0cx\u7684a\u6b21\u65b9\u624d\u88ab\u4e58\u5230\u7ed3\u679c\u4e0a\uff0c\u8fd9\u6837\u5c31\u5f88\u81ea\u7136\uff0c\u4e0d\u7528\u518d\u7279\u610f\u53bb\u8ba1\u7b97\u5f53\u524d\u662f\u7b2c\u51e0\u4f4d\u4e86\u3002 \u89c6\u9891\u8bb2\u89e3 \u7ed3\u679c\u4e3a\uff1a 1 13841287201 \u5982\u679c\u4f60\u4e0d\u7406\u89e3\u8fd9\u4e2a\u51fd\u6570\u7684\u539f\u7406\u4e5f\u4e0d\u8981\u7d27 \u2014\u2014 \u4f7f\u7528\u5b83\u5e76\u4e0d\u9700\u8981\u4f60\u4e86\u89e3\u5b83\u7684\u539f\u7406\u3002 \u6ce8\u610f \u5927\u90e8\u5206\u60c5\u51b5\u4e0b\uff0c\u6574\u578b\u7684\u6307\u6570\u8fd0\u7b97\u90fd\u53ef\u80fd\u4f1a\u6ea2\u51fa\u6574\u578b\u3002\u8fd9\u5f88\u53ef\u80fd\u662f\u8be5\u51fd\u6570\u6ca1\u6709\u88ab\u5305\u542b\u5230\u6807\u51c6\u5e93\u4e2d\u7684\u539f\u56e0\u3002","title":"\u6307\u6570\u8fd0\u7b97\u7b26\u53bb\u54ea\u4e86?"},{"location":"5-4-Increment-decrement-operators-and-side-effects/","tags":["operator"],"text":"Key Takeaway \u540e\u7f00\u7248\u672c\u7684\u8fd0\u7b97\u7b26\u591a\u4e86\u5f88\u591a\u6b65\u9aa4\uff08\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u526f\u672c\uff09\uff0c\u56e0\u6b64\u5b83\u7684\u6027\u80fd\u4e0d\u5982\u524d\u7f00\u7248\u672c \u5f3a\u70c8\u63a8\u8350\u4f7f\u7528\u524d\u7f00\u7248\u672c\u7684\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26\uff0c\u56e0\u4e3a\u5b83\u4eec\u6027\u80fd\u66f4\u597d\uff0c\u800c\u4e14\u4e5f\u4e0d\u5bb9\u6613\u5e26\u6765\u95ee\u9898 \u56e0\u4e3a C++ \u6807\u51c6\u6ca1\u6709\u89c4\u5b9a\u51fd\u6570\u53c2\u6570\u7684\u6c42\u503c\u987a\u5e8f\u3002\u6240\u4ee5 add(x, ++x) \u8fd9\u6837\u7684\u51fd\u6570\u8c03\u7528\u7ed3\u679c\u662f\u4e0d\u786e\u5b9a\u7684\uff0c\u56e0\u4e3a\u51fd\u6570 add \u7684\u5176\u4e2d\u4e00\u4e2a\u53c2\u6570\u5177\u6709\u526f\u4f5c\u7528\u3002 C++ \u6ca1\u6709\u5b9a\u4e49\u51fd\u6570\u53c2\u6570\u6216\u8fd0\u7b97\u7b26\u64cd\u4f5c\u6570\u7684\u6c42\u503c\u987a\u5e8f\u3002 \u53d8\u91cf\u7684\u81ea\u589e\u81ea\u51cf \u5c06\u4e00\u4e2a\u53d8\u91cf\u9012\u589e\uff08\u52a01\uff09\u548c\u9012\u51cf\uff08\u51cf1\uff09\u90fd\u662f\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u56e0\u6b64\u5b83\u4eec\u6709\u4e13\u95e8\u7684\u8fd0\u7b97\u7b26\u3002 \u8fd0\u7b97\u7b26 \u7b26\u53f7 \u5f62\u5f0f \u64cd\u4f5c Prefix increment (pre-increment) ++ ++x Increment x, then return x Prefix decrement (pre-decrement) \u2013\u2013 \u2013\u2013x Decrement x, then return x Postfix increment (post-increment) ++ x++ Copy x, then increment x, then return the copy Postfix decrement (post-decrement) \u2013\u2013 x\u2013\u2013 Copy x, then decrement x, then return the copy \u6ce8\u610f\uff0c\u6bcf\u79cd\u64cd\u4f5c\u7b26\u90fd\u6709\u4e24\u4e2a\u7248\u672c\uff0c\u4e00\u4e2a\u662f\u524d\u7f00\u5f62\u5f0f\uff08\u8fd0\u7b97\u7b26\u5728\u64cd\u4f5c\u7b26\u524d\u9762\uff09\uff0c\u4e00\u4e2a\u662f\u540e\u7f00\u5f62\u5f0f\uff08\u8fd0\u7b97\u7b26\u5728\u64cd\u4f5c\u7b26\u540e\u9762\uff09\u3002 \u524d\u7f00\u9012\u589e/\u9012\u51cf\u64cd\u4f5c\u7b26\u975e\u5e38\u7b80\u5355\uff0c\u5373\u64cd\u4f5c\u6570\u5148\u9012\u589e\u6216\u9012\u51cf\uff0c\u7136\u540e\u8868\u8fbe\u5f0f\u518d\u6c42\u503c\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { int x { 5 }; int y = ++ x ; // x is incremented to 6, x is evaluated to the value 6, and 6 is assigned to y std :: cout << x << ' ' << y ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 6 6 \u540e\u7f00\u81ea\u589e/\u81ea\u51cf\u7a0d\u5fae\u6709\u70b9\u590d\u6742\u3002\u9996\u5148\uff0c\u5b83\u62f7\u8d1d\u4e86\u4e00\u4efd\u64cd\u4f5c\u6570\uff0c\u7136\u540e\u5bf9\u64cd\u4f5c\u6570\uff08\u800c\u4e0d\u662f\u5b83\u7684\u62f7\u8d1d\uff09\u8fdb\u884c\u9012\u589e\u6216\u9012\u51cf\u3002\u6700\u540e\u5bf9\u62f7\u8d1d\u7684\u503c\uff08\u800c\u975e\u539f\u59cb\u503c\uff09\u8fdb\u884c\u6c42\u503c\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { int x { 5 }; int y = x ++ ; // x \u81ea\u589e\u4e3a6\uff0c\u62f7\u8d1d\u7684\u6700\u521d\u7684\u503c\u4e3a5,5\u88ab\u8d4b\u503c\u7ed9\u4e86y std :: cout << x << ' ' << y ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 6 5 \u8ba9\u6211\u4eec\u4ecb\u7ecd\u4e00\u4e0b\u7b2c\u516d\u884c\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a x \u7684\u4e34\u65f6\u62f7\u8d1d\uff0c\u503c\u548cx\u4e00\u6837\uff085\uff09\u3002\u968f\u540e\uff0c\u8fd9\u4e2a\u62f7\u8d1d\u88ab\u8fd4\u56de\u7136\u540e\u8d4b\u503c\u7ed9 y \u3002\u7136\u540e\uff0c\u8fd9\u4e2a\u4e34\u65f6\u7684\u62f7\u8d1d\u5c31\u88ab\u4e22\u5f03\u4e86\u3002 \u56e0\u6b64\uff0c y \u6700\u7ec8\u7684\u503c\u5c31\u662f 5\uff08\u9012\u589e\u524d\u7684\u503c\uff09\uff0c\u7136\u540e x \u7684\u503c\u53d8\u6210\u4e86 6\uff08\u9012\u589e\u540e\u7684\u503c\uff09\u3002 \u6ce8\u610f\uff0c\u540e\u7f00\u7248\u672c\u7684\u8fd0\u7b97\u7b26\u591a\u4e86\u5f88\u591a\u6b65\u9aa4\uff0c\u56e0\u6b64\u5b83\u7684\u6027\u80fd\u4e0d\u5982\u524d\u7f00\u7248\u672c\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u6f14\u793a\u4e86\u524d\u7f00\u9012\u589e\u9012\u51cf\u548c\u540e\u7f00\u9012\u589e\u9012\u51cf\u7684\u4e0d\u540c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 5 }; int y { 5 }; std :: cout << x << ' ' << y << '\\n' ; std :: cout << ++ x << ' ' << -- y << '\\n' ; // prefix std :: cout << x << ' ' << y << '\\n' ; std :: cout << x ++ << ' ' << y -- << '\\n' ; // postfix std :: cout << x << ' ' << y << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 5 5 6 4 6 4 6 4 7 3 \u5728\u7b2c\u516b\u884c\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u524d\u7f00\u9012\u589e\u9012\u51cf\u3002\u56e0\u6b64\u8fd9\u4e00\u884c\u4e2d x \u548c y \u5148\u9012\u589e\uff0c\u518d\u6253\u5370\uff0c\u56e0\u6b64\u5b83\u4eec\u66f4\u65b0\u540e\u7684\u503c\u88ab\u6253\u5370\u4e86\u51fa\u6765\u3002 \u5728\u7b2c\u5341\u884c\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u7684\u662f\u540e\u7f00\u9012\u589e\u9012\u51cf\u3002\u56e0\u6b64\u8fd9\u4e00\u884c\u4e2d\uff0cx \u548c y \u5148\u88ab\u62f7\u8d1d\uff0c\u7136\u540e\u6253\u5370\u3002\u6240\u4ee5\u6211\u4eec\u6ca1\u6709\u770b\u5230\u9012\u589e\u540e\u7684\u7ed3\u679c\u88ab\u6253\u5370\u51fa\u6765\u3002\u9012\u589e\u7684\u7ed3\u679c\u53ea\u6709\u518d\u4e0b\u4e00\u6b21\u6253\u5370\u65f6\u624d\u80fd\u6709\u6240\u4f53\u73b0\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f3a\u70c8\u63a8\u8350\u4f7f\u7528\u524d\u7f00\u7248\u672c\u7684\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26\uff0c\u56e0\u4e3a\u5b83\u4eec\u6027\u80fd\u66f4\u597d\uff0c\u800c\u4e14\u4e5f\u4e0d\u5bb9\u6613\u5e26\u6765\u95ee\u9898\u3002 \u526f\u4f5c\u7528 \u5982\u679c\u6211\u4eec\u8bf4\u4e00\u4e2a\u51fd\u6570\u6216\u8868\u8fbe\u5f0f\u5177\u6709\u526f\u4f5c\u7528\uff0c\u5219\u610f\u5473\u7740\u5b83\u6267\u884c\u540e\u5e26\u6765\u4e86\u67d0\u4e9b\u5f71\u54cd\u4e14\u8fd9\u4e9b\u5f71\u54cd\u5728\u51fd\u6570\u6216\u8868\u8fbe\u5f0f\u7684\u751f\u547d\u5468\u671f\u7ed3\u675f\u540e\u8fd8\u5b58\u5728\u3002 \u526f\u4f5c\u7528\u7684\u5178\u578b\u4f8b\u5b50\u5305\u62ec\u5bf9\u8c61\u7684\u503c\u88ab\u6539\u53d8\u3001\u8fdb\u884c\u4e86\u8f93\u5165\u8f93\u51fa\u6216\u8005\u66f4\u65b0\u4e86\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08\u4f8b\u5982\u6253\u5f00\u6216\u5173\u95ed\u67d0\u4e2a\u6309\u94ae\uff09\u3002 \u5f88\u591a\u65f6\u5019\uff0c\u526f\u4f5c\u7528\u662f\u6709\u7528\u7684\uff1a 1 2 3 x = 5 ; // the assignment operator modifies the state of x ++ x ; // operator++ modifies the state of x std :: cout << x ; // operator<< modifies the state of the console \u4e0a\u9762\u8d4b\u503c\u53f7\u7684\u526f\u4f5c\u7528\u662f\u6c38\u4e45\u5730\u6539\u53d8\u4e86 x \u7684\u503c\uff0c\u5373\u4f7f\u5728\u8d4b\u503c\u8bed\u53e5\u7ed3\u675f\u6267\u884c\u540e\uff0c x \u7684\u503c\u4e5f\u8fd8\u662f 5\u3002\u540c\u6837\u7684\uff0c ++ \u8fd0\u7b97\u7b26\u4e5f\u6709\u526f\u4f5c\u7528\uff0c ++x \u6267\u884c\u540e x \u7684\u503c\u5c31\u9012\u589e\u4e86\u3002\u6253\u5370 x \u540c\u6837\u6709\u526f\u4f5c\u7528\uff0c\u5b83\u6539\u53d8\u4e86\u63a7\u5236\u53f0\u7684\u72b6\u6001\uff0c\u56e0\u4e3a\u4f60\u53ef\u4ee5\u770b\u5230 x \u7684\u503c\u88ab\u6253\u5370\u5230\u4e86\u63a7\u5236\u53f0\u3002 \u4e0d\u8fc7\uff0c\u526f\u4f5c\u7528\u6709\u65f6\u5019\u4e5f\u4f1a\u5e26\u6765\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int add ( int x , int y ) { return x + y ; } int main () { int x { 5 }; int value { add ( x , ++ x ) }; // \u7a76\u7adf\u662f 5 + 6 \u8fd8\u662f 6 + 6? // \u8fd9\u53d6\u51b3\u4e8e\u4f60\u7684\u7f16\u8bd1\u5668\u5982\u4f55\u9009\u62e9\u51fd\u6570\u53c2\u6570\u7684\u6c42\u503c\u987a\u5e8f std :: cout << value << '\\n' ; // value \u53ef\u80fd\u662f 11 \u6216\u8005 12\uff0c\u53d6\u51b3\u4e8e\u7f16\u8bd1\u5668 return 0 ; } \u56e0\u4e3a C++ \u6807\u51c6\u6ca1\u6709\u89c4\u5b9a\u51fd\u6570\u53c2\u6570\u7684\u6c42\u503c\u987a\u5e8f\u3002\u5982\u679c\u5de6\u8fb9\u7684\u53c2\u6570\u5148\u6c42\u503c\uff0c\u5219\u5b9e\u9645\u8c03\u7528\u4e3a add(5,6) \uff0c\u7ed3\u679c\u4e3a 11\u3002\u5982\u679c\u53f3\u8fb9\u7684\u53c2\u6570\u5148\u6c42\u503c\uff0c\u5219\u5b9e\u9645\u8c03\u7528\u4e3a add(6,6) \uff0c\u7ed3\u679c\u4e3a 12\u3002\u9020\u6210\u8fd9\u4e2a\u95ee\u9898\u7684\u539f\u56e0\uff0c\u662f\u56e0\u4e3a\u51fd\u6570 add \u7684\u5176\u4e2d\u4e00\u4e2a\u53c2\u6570\u5177\u6709\u526f\u4f5c\u7528\u3002 \u9898\u5916\u8bdd C++ \u6807\u51c6\u6545\u610f\u6ca1\u6709\u5b9a\u4e49\u8fd9\u4e9b\u4e8b\u60c5\uff0c\u8fd9\u6837\u7f16\u8bd1\u5668\u53ef\u4ee5\u6839\u636e\u8ba1\u7b97\u673a\u7684\u4f53\u7cfb\u7ed3\u6784\u9009\u62e9\u6700\u81ea\u7136\uff08\u4e00\u822c\u4e5f\u662f\u6700\u9ad8\u6548\uff09\u7684\u65b9\u5f0f\u6765\u5904\u7406\u3002 C++ \u6807\u51c6\u6ca1\u6709\u89c4\u5b9a\u6c42\u503c\u987a\u5e8f\u7684\u573a\u666f\u8fd8\u6709\u4e00\u4e9b\uff08\u4f8b\u5982\u64cd\u4f5c\u6570\u7684\u6c42\u503c\u987a\u5e8f\uff09\uff0c\u6240\u4ee5\u4e0d\u540c\u7684\u7f16\u8bd1\u5668\u884c\u4e3a\u4e5f\u53ef\u80fd\u662f\u4e0d\u4e00\u6837\u7684\u3002\u5373\u4f7fC++\u6807\u51c6\u80fd\u591f\u660e\u786e\u8fd9\u4e9b\u95ee\u9898\uff0c\u7531\u4e8e\u5386\u53f2\u539f\u56e0\uff0c\u8fd9\u91cc\u4e5f\u662f\u7f16\u8bd1\u5668bug\u591a\u53d1\u4e4b\u5730\u3002\u901a\u5e38\uff0c\u8fd9\u4e9b\u95ee\u9898\u662f\u53ef\u4ee5\u907f\u514d\u7684\uff0c\u53ea\u8981\u4f60\u907f\u514d\u4e0d\u8981\u8ba9\u526f\u4f5c\u7528\u591a\u6b21\u4f5c\u7528\u5728\u4e00\u4e2a\u53d8\u91cf\u4e0a\u3002 \u6ce8\u610f C++ \u6ca1\u6709\u5b9a\u4e49\u51fd\u6570\u53c2\u6570\u6216\u8fd0\u7b97\u7b26\u64cd\u4f5c\u6570\u7684\u6c42\u503c\u987a\u5e8f\u3002 \u6ce8\u610f \u5728\u4e00\u4e2a\u8bed\u53e5\u4e2d\uff0c\u4e0d\u8981\u8ba9\u526f\u4f5c\u7528\u591a\u6b21\u4f5c\u7528\u5728\u4e00\u4e2a\u53d8\u91cf\u4e0a\u3002\u5982\u679c\u8fd9\u4e48\u505a\u7684\u8bdd\uff0c\u7ed3\u679c\u53ef\u80fd\u662f\u4e0d\u786e\u5b9a\u7684\u3002","title":"5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528"},{"location":"5-4-Increment-decrement-operators-and-side-effects/#_1","text":"\u5c06\u4e00\u4e2a\u53d8\u91cf\u9012\u589e\uff08\u52a01\uff09\u548c\u9012\u51cf\uff08\u51cf1\uff09\u90fd\u662f\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u56e0\u6b64\u5b83\u4eec\u6709\u4e13\u95e8\u7684\u8fd0\u7b97\u7b26\u3002 \u8fd0\u7b97\u7b26 \u7b26\u53f7 \u5f62\u5f0f \u64cd\u4f5c Prefix increment (pre-increment) ++ ++x Increment x, then return x Prefix decrement (pre-decrement) \u2013\u2013 \u2013\u2013x Decrement x, then return x Postfix increment (post-increment) ++ x++ Copy x, then increment x, then return the copy Postfix decrement (post-decrement) \u2013\u2013 x\u2013\u2013 Copy x, then decrement x, then return the copy \u6ce8\u610f\uff0c\u6bcf\u79cd\u64cd\u4f5c\u7b26\u90fd\u6709\u4e24\u4e2a\u7248\u672c\uff0c\u4e00\u4e2a\u662f\u524d\u7f00\u5f62\u5f0f\uff08\u8fd0\u7b97\u7b26\u5728\u64cd\u4f5c\u7b26\u524d\u9762\uff09\uff0c\u4e00\u4e2a\u662f\u540e\u7f00\u5f62\u5f0f\uff08\u8fd0\u7b97\u7b26\u5728\u64cd\u4f5c\u7b26\u540e\u9762\uff09\u3002 \u524d\u7f00\u9012\u589e/\u9012\u51cf\u64cd\u4f5c\u7b26\u975e\u5e38\u7b80\u5355\uff0c\u5373\u64cd\u4f5c\u6570\u5148\u9012\u589e\u6216\u9012\u51cf\uff0c\u7136\u540e\u8868\u8fbe\u5f0f\u518d\u6c42\u503c\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { int x { 5 }; int y = ++ x ; // x is incremented to 6, x is evaluated to the value 6, and 6 is assigned to y std :: cout << x << ' ' << y ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 6 6 \u540e\u7f00\u81ea\u589e/\u81ea\u51cf\u7a0d\u5fae\u6709\u70b9\u590d\u6742\u3002\u9996\u5148\uff0c\u5b83\u62f7\u8d1d\u4e86\u4e00\u4efd\u64cd\u4f5c\u6570\uff0c\u7136\u540e\u5bf9\u64cd\u4f5c\u6570\uff08\u800c\u4e0d\u662f\u5b83\u7684\u62f7\u8d1d\uff09\u8fdb\u884c\u9012\u589e\u6216\u9012\u51cf\u3002\u6700\u540e\u5bf9\u62f7\u8d1d\u7684\u503c\uff08\u800c\u975e\u539f\u59cb\u503c\uff09\u8fdb\u884c\u6c42\u503c\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { int x { 5 }; int y = x ++ ; // x \u81ea\u589e\u4e3a6\uff0c\u62f7\u8d1d\u7684\u6700\u521d\u7684\u503c\u4e3a5,5\u88ab\u8d4b\u503c\u7ed9\u4e86y std :: cout << x << ' ' << y ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 6 5 \u8ba9\u6211\u4eec\u4ecb\u7ecd\u4e00\u4e0b\u7b2c\u516d\u884c\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a x \u7684\u4e34\u65f6\u62f7\u8d1d\uff0c\u503c\u548cx\u4e00\u6837\uff085\uff09\u3002\u968f\u540e\uff0c\u8fd9\u4e2a\u62f7\u8d1d\u88ab\u8fd4\u56de\u7136\u540e\u8d4b\u503c\u7ed9 y \u3002\u7136\u540e\uff0c\u8fd9\u4e2a\u4e34\u65f6\u7684\u62f7\u8d1d\u5c31\u88ab\u4e22\u5f03\u4e86\u3002 \u56e0\u6b64\uff0c y \u6700\u7ec8\u7684\u503c\u5c31\u662f 5\uff08\u9012\u589e\u524d\u7684\u503c\uff09\uff0c\u7136\u540e x \u7684\u503c\u53d8\u6210\u4e86 6\uff08\u9012\u589e\u540e\u7684\u503c\uff09\u3002 \u6ce8\u610f\uff0c\u540e\u7f00\u7248\u672c\u7684\u8fd0\u7b97\u7b26\u591a\u4e86\u5f88\u591a\u6b65\u9aa4\uff0c\u56e0\u6b64\u5b83\u7684\u6027\u80fd\u4e0d\u5982\u524d\u7f00\u7248\u672c\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u6f14\u793a\u4e86\u524d\u7f00\u9012\u589e\u9012\u51cf\u548c\u540e\u7f00\u9012\u589e\u9012\u51cf\u7684\u4e0d\u540c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 5 }; int y { 5 }; std :: cout << x << ' ' << y << '\\n' ; std :: cout << ++ x << ' ' << -- y << '\\n' ; // prefix std :: cout << x << ' ' << y << '\\n' ; std :: cout << x ++ << ' ' << y -- << '\\n' ; // postfix std :: cout << x << ' ' << y << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 5 5 6 4 6 4 6 4 7 3 \u5728\u7b2c\u516b\u884c\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u524d\u7f00\u9012\u589e\u9012\u51cf\u3002\u56e0\u6b64\u8fd9\u4e00\u884c\u4e2d x \u548c y \u5148\u9012\u589e\uff0c\u518d\u6253\u5370\uff0c\u56e0\u6b64\u5b83\u4eec\u66f4\u65b0\u540e\u7684\u503c\u88ab\u6253\u5370\u4e86\u51fa\u6765\u3002 \u5728\u7b2c\u5341\u884c\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u7684\u662f\u540e\u7f00\u9012\u589e\u9012\u51cf\u3002\u56e0\u6b64\u8fd9\u4e00\u884c\u4e2d\uff0cx \u548c y \u5148\u88ab\u62f7\u8d1d\uff0c\u7136\u540e\u6253\u5370\u3002\u6240\u4ee5\u6211\u4eec\u6ca1\u6709\u770b\u5230\u9012\u589e\u540e\u7684\u7ed3\u679c\u88ab\u6253\u5370\u51fa\u6765\u3002\u9012\u589e\u7684\u7ed3\u679c\u53ea\u6709\u518d\u4e0b\u4e00\u6b21\u6253\u5370\u65f6\u624d\u80fd\u6709\u6240\u4f53\u73b0\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f3a\u70c8\u63a8\u8350\u4f7f\u7528\u524d\u7f00\u7248\u672c\u7684\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26\uff0c\u56e0\u4e3a\u5b83\u4eec\u6027\u80fd\u66f4\u597d\uff0c\u800c\u4e14\u4e5f\u4e0d\u5bb9\u6613\u5e26\u6765\u95ee\u9898\u3002","title":"\u53d8\u91cf\u7684\u81ea\u589e\u81ea\u51cf"},{"location":"5-4-Increment-decrement-operators-and-side-effects/#_2","text":"\u5982\u679c\u6211\u4eec\u8bf4\u4e00\u4e2a\u51fd\u6570\u6216\u8868\u8fbe\u5f0f\u5177\u6709\u526f\u4f5c\u7528\uff0c\u5219\u610f\u5473\u7740\u5b83\u6267\u884c\u540e\u5e26\u6765\u4e86\u67d0\u4e9b\u5f71\u54cd\u4e14\u8fd9\u4e9b\u5f71\u54cd\u5728\u51fd\u6570\u6216\u8868\u8fbe\u5f0f\u7684\u751f\u547d\u5468\u671f\u7ed3\u675f\u540e\u8fd8\u5b58\u5728\u3002 \u526f\u4f5c\u7528\u7684\u5178\u578b\u4f8b\u5b50\u5305\u62ec\u5bf9\u8c61\u7684\u503c\u88ab\u6539\u53d8\u3001\u8fdb\u884c\u4e86\u8f93\u5165\u8f93\u51fa\u6216\u8005\u66f4\u65b0\u4e86\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08\u4f8b\u5982\u6253\u5f00\u6216\u5173\u95ed\u67d0\u4e2a\u6309\u94ae\uff09\u3002 \u5f88\u591a\u65f6\u5019\uff0c\u526f\u4f5c\u7528\u662f\u6709\u7528\u7684\uff1a 1 2 3 x = 5 ; // the assignment operator modifies the state of x ++ x ; // operator++ modifies the state of x std :: cout << x ; // operator<< modifies the state of the console \u4e0a\u9762\u8d4b\u503c\u53f7\u7684\u526f\u4f5c\u7528\u662f\u6c38\u4e45\u5730\u6539\u53d8\u4e86 x \u7684\u503c\uff0c\u5373\u4f7f\u5728\u8d4b\u503c\u8bed\u53e5\u7ed3\u675f\u6267\u884c\u540e\uff0c x \u7684\u503c\u4e5f\u8fd8\u662f 5\u3002\u540c\u6837\u7684\uff0c ++ \u8fd0\u7b97\u7b26\u4e5f\u6709\u526f\u4f5c\u7528\uff0c ++x \u6267\u884c\u540e x \u7684\u503c\u5c31\u9012\u589e\u4e86\u3002\u6253\u5370 x \u540c\u6837\u6709\u526f\u4f5c\u7528\uff0c\u5b83\u6539\u53d8\u4e86\u63a7\u5236\u53f0\u7684\u72b6\u6001\uff0c\u56e0\u4e3a\u4f60\u53ef\u4ee5\u770b\u5230 x \u7684\u503c\u88ab\u6253\u5370\u5230\u4e86\u63a7\u5236\u53f0\u3002 \u4e0d\u8fc7\uff0c\u526f\u4f5c\u7528\u6709\u65f6\u5019\u4e5f\u4f1a\u5e26\u6765\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int add ( int x , int y ) { return x + y ; } int main () { int x { 5 }; int value { add ( x , ++ x ) }; // \u7a76\u7adf\u662f 5 + 6 \u8fd8\u662f 6 + 6? // \u8fd9\u53d6\u51b3\u4e8e\u4f60\u7684\u7f16\u8bd1\u5668\u5982\u4f55\u9009\u62e9\u51fd\u6570\u53c2\u6570\u7684\u6c42\u503c\u987a\u5e8f std :: cout << value << '\\n' ; // value \u53ef\u80fd\u662f 11 \u6216\u8005 12\uff0c\u53d6\u51b3\u4e8e\u7f16\u8bd1\u5668 return 0 ; } \u56e0\u4e3a C++ \u6807\u51c6\u6ca1\u6709\u89c4\u5b9a\u51fd\u6570\u53c2\u6570\u7684\u6c42\u503c\u987a\u5e8f\u3002\u5982\u679c\u5de6\u8fb9\u7684\u53c2\u6570\u5148\u6c42\u503c\uff0c\u5219\u5b9e\u9645\u8c03\u7528\u4e3a add(5,6) \uff0c\u7ed3\u679c\u4e3a 11\u3002\u5982\u679c\u53f3\u8fb9\u7684\u53c2\u6570\u5148\u6c42\u503c\uff0c\u5219\u5b9e\u9645\u8c03\u7528\u4e3a add(6,6) \uff0c\u7ed3\u679c\u4e3a 12\u3002\u9020\u6210\u8fd9\u4e2a\u95ee\u9898\u7684\u539f\u56e0\uff0c\u662f\u56e0\u4e3a\u51fd\u6570 add \u7684\u5176\u4e2d\u4e00\u4e2a\u53c2\u6570\u5177\u6709\u526f\u4f5c\u7528\u3002 \u9898\u5916\u8bdd C++ \u6807\u51c6\u6545\u610f\u6ca1\u6709\u5b9a\u4e49\u8fd9\u4e9b\u4e8b\u60c5\uff0c\u8fd9\u6837\u7f16\u8bd1\u5668\u53ef\u4ee5\u6839\u636e\u8ba1\u7b97\u673a\u7684\u4f53\u7cfb\u7ed3\u6784\u9009\u62e9\u6700\u81ea\u7136\uff08\u4e00\u822c\u4e5f\u662f\u6700\u9ad8\u6548\uff09\u7684\u65b9\u5f0f\u6765\u5904\u7406\u3002 C++ \u6807\u51c6\u6ca1\u6709\u89c4\u5b9a\u6c42\u503c\u987a\u5e8f\u7684\u573a\u666f\u8fd8\u6709\u4e00\u4e9b\uff08\u4f8b\u5982\u64cd\u4f5c\u6570\u7684\u6c42\u503c\u987a\u5e8f\uff09\uff0c\u6240\u4ee5\u4e0d\u540c\u7684\u7f16\u8bd1\u5668\u884c\u4e3a\u4e5f\u53ef\u80fd\u662f\u4e0d\u4e00\u6837\u7684\u3002\u5373\u4f7fC++\u6807\u51c6\u80fd\u591f\u660e\u786e\u8fd9\u4e9b\u95ee\u9898\uff0c\u7531\u4e8e\u5386\u53f2\u539f\u56e0\uff0c\u8fd9\u91cc\u4e5f\u662f\u7f16\u8bd1\u5668bug\u591a\u53d1\u4e4b\u5730\u3002\u901a\u5e38\uff0c\u8fd9\u4e9b\u95ee\u9898\u662f\u53ef\u4ee5\u907f\u514d\u7684\uff0c\u53ea\u8981\u4f60\u907f\u514d\u4e0d\u8981\u8ba9\u526f\u4f5c\u7528\u591a\u6b21\u4f5c\u7528\u5728\u4e00\u4e2a\u53d8\u91cf\u4e0a\u3002 \u6ce8\u610f C++ \u6ca1\u6709\u5b9a\u4e49\u51fd\u6570\u53c2\u6570\u6216\u8fd0\u7b97\u7b26\u64cd\u4f5c\u6570\u7684\u6c42\u503c\u987a\u5e8f\u3002 \u6ce8\u610f \u5728\u4e00\u4e2a\u8bed\u53e5\u4e2d\uff0c\u4e0d\u8981\u8ba9\u526f\u4f5c\u7528\u591a\u6b21\u4f5c\u7528\u5728\u4e00\u4e2a\u53d8\u91cf\u4e0a\u3002\u5982\u679c\u8fd9\u4e48\u505a\u7684\u8bdd\uff0c\u7ed3\u679c\u53ef\u80fd\u662f\u4e0d\u786e\u5b9a\u7684\u3002","title":"\u526f\u4f5c\u7528"},{"location":"5-5-Comma-and-conditional-operators/","tags":["operator"],"text":"Key Takeaway \u9017\u53f7\u8868\u8fbe\u5f0f\u9996\u5148\u5bf9\u5b83\u5de6\u4fa7\u7684\u64cd\u4f5c\u6570\u6c42\u503c\uff0c\u7136\u540e\u5bf9\u53f3\u4fa7\u64cd\u4f5c\u6570\u6c42\u503c\uff0c\u7136\u540e\u8fd4\u56de\u53f3\u4fa7\u64cd\u4f5c\u6570\u7684\u7ed3\u679c\u3002 \u907f\u514d\u4f7f\u7528\u9017\u53f7\u8fd0\u7b97\u7b26\uff0c\u9664\u975e\u662f\u5728 for \u5faa\u73af\u4e2d\u3002 \u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u6574\u4e2a\u8868\u8fbe\u5f0f\u90fd\u8981\u653e\u5728\u62ec\u53f7\u4e2d\uff0c\u6b64\u5916\uff0c\u6761\u4ef6\u90e8\u5206\u4e5f\u8981\u653e\u5728\u62ec\u53f7\u4e2d\u3002 \u6761\u4ef6\u8fd0\u7b97\u7b26\u53ef\u4ee5\u4f5c\u4e3a\u8868\u8fbe\u5f0f\u6c42\u503c\uff0c\u8fd9\u6837\u4fbf\u53ef\u4ee5\u7528\u4e8e\u521d\u59cb\u5316\u3001\u8d4b\u503c\u548c\u4f20\u53c2 if/else \u8bed\u53e5\u5757\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u5728 if/else \u7ed3\u675f\u540e\u5c31\u9500\u6bc1\u4e86 \u6761\u4ef6\u8bed\u53e5\u4e2d\u7684\u4e24\u4e2a\u8868\u8fbe\u5f0f\u7c7b\u578b\u9700\u8981\u5339\u914d\u6216\u8005\u53ef\u4ee5\u8f6c\u53d8\u4e3a\u5171\u540c\u7684\u7c7b\u578b\uff0c\u5426\u5219\u65e0\u6cd5\u7f16\u8bd1\uff0c\u5fc5\u987b\u662f if/else \u5b8c\u6210\u3002 \u4ec5\u5728\u9762\u5bf9\u7b80\u5355\u6761\u4ef6\u4e14\u4f60\u4f7f\u7528\u5176\u7ed3\u679c\uff0c\u540c\u65f6\u80fd\u591f\u589e\u5f3a\u53ef\u8bfb\u6027\u7684\u6761\u4ef6\u4e0b\uff0c\u4f7f\u7528\u6761\u4ef6\u8fd0\u7b97\u7b26 \u9017\u53f7\u8fd0\u7b97\u7b26 \u8fd0\u7b97\u7b26 \u7b26\u53f7 \u5f62\u5f0f \u64cd\u4f5c \u9017\u53f7\u8fd0\u7b97\u7b26 , x, y \u5148\u5bf9 x \u6c42\u503c\uff0c\u518d\u5bf9 y \u6c42\u503c\uff0c\u7136\u540e\u8fd4\u56de y \u6c42\u503c\u7ed3\u679c \u9017\u53f7\u8fd0\u7b97\u7b26 ( , ) \u5141\u8bb8\u4f60\u5728\u5141\u8bb8\u5355\u72ec\u8868\u8fbe\u5f0f\u7684\u5730\u65b9\uff0c\u5bf9\u591a\u4e2a\u8868\u8fbe\u5f0f\u6c42\u503c\u3002\u9017\u53f7\u8868\u8fbe\u5f0f\u9996\u5148\u5bf9\u5b83\u5de6\u4fa7\u7684\u64cd\u4f5c\u6570\u6c42\u503c\uff0c\u7136\u540e\u5bf9\u53f3\u4fa7\u64cd\u4f5c\u6570\u6c42\u503c\uff0c\u7136\u540e\u8fd4\u56de\u53f3\u4fa7\u64cd\u4f5c\u6570\u7684\u7ed3\u679c\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { int x { 1 }; int y { 2 }; std :: cout << ( ++ x , ++ y ); // \u5bf9 x \u548c y \u9012\u589e\uff0c \u5bf9\u53f3\u4fa7\u64cd\u4f5c\u6570\u6c42\u503c return 0 ; } \u9996\u5148\uff0c\u9017\u53f7\u8fd0\u7b97\u7b26\u5de6\u4fa7\u64cd\u4f5c\u6570\u6c42\u503c\uff0c\u5c06 x \u9012\u589e\u4e3a 2\u3002\u7136\u540e\u9017\u53f7\u8fd0\u7b97\u7b26\u53f3\u4fa7\u64cd\u4f5c\u6570\u6c42\u503c\uff0c\u5c06 y \u9012\u589e\u4e3a 3\u3002\u7136\u540e\u9017\u53f7\u8868\u8fbe\u5f0f\u8fd4\u56de\u7684\u662f\u53f3\u4fa7\u6c42\u503c\u7684\u7ed3\u679c\uff083\uff09\uff0c\u5e76\u6253\u5370\u5728\u5c4f\u5e55\u4e0a\u3002 \u6ce8\u610f\uff0c\u9017\u53f7\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u662f\u6700\u4f4e\u7684\uff0c\u751a\u81f3\u6bd4\u8d4b\u503c\u8868\u8fbe\u5f0f\u8fd8\u4f4e\u3002\u56e0\u6b64\uff0c\u4e0b\u9762\u4e24\u884c\u4ee3\u7801\u7684\u6548\u679c\u662f\u4e0d\u540c\u7684\u3002 1 2 z = ( a , b ); // \u5148\u5bf9 (a, b) \u6c42\u503c\u5f97\u5230 b \u7684\u7ed3\u679c\uff0c\u7136\u540e\u8d4b\u503c\u7ed9 z z = a , b ; // \u7b49\u4ef7\u4e8e \"(z = a), b\"\uff0c\u6240\u4ee5 z \u9996\u5148\u88ab\u8d4b\u503c\u4e3a a\uff0c\u7136\u540e b \u6c42\u503c\uff0c\u7ed3\u679c\u4e22\u5f03\u3002 \u8fd9\u4e5f\u4f7f\u5f97\u9017\u53f7\u8fd0\u7b97\u7b26\u7528\u8d77\u6765\u5b58\u5728\u5371\u9669\u3002 \u51e0\u4e4e\u4efb\u4f55\u4f7f\u7528\u9017\u53f7\u8fd0\u7b97\u7b26\u7f16\u5199\u7684\u8bed\u53e5\u90fd\u53ef\u4ee5\u7528\u591a\u4e2a\u5355\u72ec\u7684\u8bed\u53e5\u6765\u66ff\u6362\u3002\u4f8b\u5982\uff0c\u4e0a\u8ff0\u4ee3\u7801\u53ef\u4ee5\u6539\u5199\u6210\u4e0b\u9762\u7684\u5f62\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int x { 1 }; int y { 2 }; ++ x ; std :: cout << ++ y ; return 0 ; } \u5927\u591a\u6570\u7a0b\u5e8f\u5458\u51e0\u4e4e\u4ece\u6765\u4e0d\u4f1a\u4f7f\u7528\u9017\u53f7\u8fd0\u7b97\u7b26\uff0c\u53ea\u6709\u5728 for \u5faa\u73af\u4e2d\u5b58\u5728\u4f8b\u5916\uff0c\u6211\u4eec\u4f1a\u5728 7.9 - for \u8bed\u53e5 \u4e2d\u8be6\u7ec6\u8ba8\u8bba\uff1a \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u4f7f\u7528\u9017\u53f7\u8fd0\u7b97\u7b26\uff0c\u9664\u975e\u662f\u5728 for \u5faa\u73af\u4e2d\u3002 \u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26 \u5728 C++\uff0c\u9017\u53f7\u5e38\u7528\u4f5c\u5206\u9694\u7b26\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u548c\u9017\u53f7\u8fd0\u7b97\u7b26\u5e76\u6ca1\u6709\u5173\u7cfb\uff0c\u4e5f\u4e0d\u5177\u5907\u9017\u53f7\u8fd0\u7b97\u7b26\u7684\u529f\u80fd\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 void foo ( int x , int y ) // \u5728\u51fd\u6570\u4e2d\u9017\u53f7\u7528\u4e8e\u5206\u5272\u53c2\u6570 { add ( x , y ); // Comma used to separate arguments in function call constexpr int z { 3 }, w { 5 }; // \u9017\u53f7\u7528\u4e8e\u5206\u5272\u540c\u4e00\u884c\u4e2d\u5b9a\u4e49\u7684\u591a\u4e2a\u53d8\u91cf\uff08\u4e0d\u8981\u8fd9\u4e48\u505a\uff09 } \u4e0d\u9700\u8981\u907f\u514d\u4f7f\u7528\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\uff08\u9664\u975e\u662f\u58f0\u660e\u591a\u4e2a\u53d8\u91cf\u65f6\uff0c\u4e0d\u8981\u8fd9\u4e48\u505a\uff09\u3002 \u6761\u4ef6\u8fd0\u7b97\u7b26 \u8fd0\u7b97\u7b26 \u7b26\u53f7 \u5f62\u5f0f \u64cd\u4f5c \u6761\u4ef6\u8fd0\u7b97\u7b26 ?: c ? x : y \u5982\u679c c \u975e\u96f6\uff08\u771f\uff09\u5219\u5bf9 x \u5176\u4e2d\uff0c\u5426\u5219\u5bf9 y \u6c42\u503c \u6761\u4ef6\u8fd0\u7b97\u7b26 ( ?: ) (\u6709\u65f6\u4e5f\u79f0\u4e3a \u201d\u7b97\u6570 if \u201c\u8fd0\u7b97\u7b26) \u662f\u4e00\u4e2a\u4e09\u5143\u8fd0\u7b97\u7b26\uff08\u6709\u4e09\u4e2a\u64cd\u4f5c\u6570\uff09\u3002\u56e0\u4e3a\u5b83\u662f C++ \u4e2d\u552f\u4e00\u7684\u4e09\u5143\u8fd0\u7b97\u7b26\uff0c\u56e0\u6b64\u6709\u4f7f\u7528\u4e5f\u79f0\u5b83\u4e3a\u201d\u90a3\u4e2a\u4e09\u5143\u8fd0\u7b97\u7b26\u201c\u3002 ?: \u8fd0\u7b97\u7b26\u63d0\u4f9b\u4e86\u4e00\u79cd\u5feb\u6377\u7684\u6761\u4ef6\u8bed\u53e5\u529f\u80fd\u3002\u5982\u679c\u4f60\u5bf9\u6761\u4ef6\u8bed\u53e5\u8fd8\u6709\u7591\u95ee\uff0c\u8bf7\u53c2\u8003 4.10 - if \u8bed\u53e5\u7b80\u4ecb \u3002 if/else \u8bed\u53e5\u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 2 3 4 if ( condition ) statement1 ; else statement2 ; \u5982\u679c condition \u6c42\u503c\u7ed3\u679c\u4e3a true \uff0c\u5219 statement1 \u4f1a\u88ab\u6267\u884c\uff0c\u5426\u5219\u6267\u884c statement2 \u3002 ?: \u8fd0\u7b97\u7b26\u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 (condition) ? expression1 : expression2; \u5982\u679c condition \u6c42\u503c\u7ed3\u679c\u4e3a true \uff0c\u5219 statement1 \u4f1a\u88ab\u6267\u884c\uff0c\u5426\u5219\u6267\u884c statement2 \u3002\u6ce8\u610f\uff0c\u8fd9\u91cc\u7684 expression2 \u662f\u5fc5\u987b\u8981\u6709\u7684\u3002 \u4e0b\u9762\u8fd9\u4e2a if/else \u8bed\u53e5\uff1a 1 2 3 4 if ( x > y ) larger = x ; else larger = y ; \u53ef\u4ee5\u6539\u5199\u6210\u5982\u4e0b\u5f62\u5f0f\uff1a 1 larger = ( x > y ) ? x : y ; \u8fd9\u6837\u53ef\u4ee5\u5728\u4e0d\u727a\u7272\u53ef\u8bfb\u6027\u7684\u524d\u63d0\u4e0b\uff0c\u8ba9\u4ee3\u7801\u53d8\u5f97\u66f4\u52a0\u7d27\u51d1\u3002 \u4e3a\u6761\u4ef6\u8fd0\u7b97\u7b26\u6dfb\u52a0\u62ec\u53f7 \u6839\u636e\u60ef\u4f8b\uff0c\u6211\u4eec\u4e00\u822c\u4f1a\u628a\u6761\u4ef6\u8fd0\u7b97\u7b26\u7684 \u6761\u4ef6 \u90e8\u5206\u653e\u5728\u62ec\u53f7\u91cc\uff0c\u8fd9\u6837\u4e0d\u4ec5\u53ef\u8bfb\u6027\u66f4\u597d\uff0c\u800c\u4e14\u53ef\u4ee5\u786e\u4fdd\u8fd0\u7b97\u4f18\u5148\u7ea7\u662f\u6b63\u786e\u7684\u3002\u5176\u4ed6\u64cd\u4f5c\u6570\u7684\u6c42\u503c\u4f18\u5148\u7ea7\uff0c\u52a0\u4e0d\u52a0\u62ec\u53f7\u90fd\u662f\u4e00\u6837\u7684\uff0c\u6240\u4ee5\u5c31\u6ca1\u5fc5\u8981\u663e\u793a\u5730\u6dfb\u52a0\u62ec\u53f7\u4e86\u3002 \u6ce8\u610f\uff0c ?: \u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u975e\u5e38\u4f4e\u3002\u9664\u975e\u662f\u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u91cc\uff0c\u4e0d\u7136\u6574\u4e2a\u6761\u4ef6\u8fd0\u7b97\u7b26\u8868\u8fbe\u5f0f\u4e5f\u8981\u653e\u5728\u62ec\u53f7\u4e2d\u3002 \u4f8b\u5982\uff0c\u4e3a\u4e86\u6253\u5370 x \u548c y \u4e2d\u8f83\u5927\u7684\u503c\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 2 3 4 if ( x > y ) std :: cout << x ; else std :: cout << y ; \u6216\u8005\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8fd0\u7b97\u7b26\uff1a 1 std :: cout << (( x > y ) ? x : y ); \u8ba9\u6211\u4eec\u6d4b\u8bd5\u4e00\u4e0b\uff0c\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c\u5982\u679c\u4e0d\u628a\u6761\u4ef6\u8fd0\u7b97\u7b26\u8868\u8fbe\u5f0f\u653e\u5728\u4e00\u4e2a\u62ec\u53f7\u4e2d\u4f1a\u600e\u6837\u3002 \u56e0 << operator \u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e ?: \u8fd0\u7b97\u7b26\uff0c\u56e0\u6b64\u4e0b\u9762\u8bed\u53e5\uff1a 1 std :: cout << ( x > y ) ? x : y ; \u7b49\u4ef7\u4e8e\uff1a 1 ( std :: cout << ( x > y )) ? x : y ; \u56e0\u6b64\uff0c\u5982\u679c x>y \u5219\u7ed3\u679c\u4f1a\u6253\u5370 1(true)\uff0c\u5426\u5219\u6253\u53700\u3002 \u6700\u4f73\u5b9e\u8df5 \u603b\u662f\u628a\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u6761\u4ef6\u90e8\u5206\u653e\u5728\u62ec\u53f7\u91cc\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u8003\u8651\u628a\u6574\u4e2a\u90e8\u5206\u90fd\u653e\u5728\u62ec\u53f7\u91cc\u3002 \u6761\u4ef6\u8fd0\u7b97\u7b26\u4f5c\u4e3a\u8868\u8fbe\u5f0f\u6c42\u503c \u56e0\u4e3a\u6761\u4ef6\u8fd0\u7b97\u7b26\u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u8bed\u53e5\uff0c\u6240\u4ee5\u6761\u4ef6\u8fd0\u7b97\u7b26\u53ef\u4ee5\u88ab\u7528\u5728\u65e0\u6cd5\u4f7f\u7528 if/else \u7684\u5730\u65b9\u3002 \u4f8b\u5982\uff0c\u53ef\u4ee5\u7528\u5b83\u6765\u521d\u59cb\u5316\u4e00\u4e2a\u5e38\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { constexpr bool inBigClassroom { false }; constexpr int classSize { inBigClassroom ? 30 : 20 }; std :: cout << \"The class size is: \" << classSize << '\\n' ; return 0 ; } \u4f7f\u7528 if/else \u5e76\u4e0d\u80fd\u7528\u4e8e\u8fd9\u79cd\u5e38\u89c1\uff0c\u4f60\u53ef\u80fd\u4f1a\u89c9\u5f97\u4e0b\u9762\u8fd9\u79cd\u65b9\u6cd5\u662f\u53ef\u884c\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { constexpr bool inBigClassroom { false }; if ( inBigClassroom ) constexpr int classSize { 30 }; else constexpr int classSize { 20 }; std :: cout << \"The class size is: \" << classSize ; return 0 ; } \u5b9e\u9645\u4e0a\uff0c\u4e0a\u8ff0\u4ee3\u7801\u662f\u4e0d\u80fd\u7f16\u8bd1\u7684\uff0c\u4f60\u4f1a\u88ab\u544a\u77e5 claseSize \u6ca1\u6709\u5b9a\u4e49\u3002\u6b63\u5982\u5b9a\u4e49\u5728\u51fd\u6570\u4e2d\u7684\u53d8\u91cf\u4f1a\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u9500\u6bc1\u4e00\u6837\uff0c\u5b9a\u4e49\u5728 if/else \u8bed\u53e5\u5757\u4e2d\u7684\u53d8\u91cf\u4e5f\u4f1a\u5728 if/else \u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u6253\u5370 classSize \u65f6\uff0c\u8be5\u53d8\u91cf\u5b9e\u9645\u4e0a\u5df2\u7ecf\u88ab\u9500\u6bc1\u4e86\u3002 \u8bd1\u8005\u6ce8 \u5982\u679c\u4f60\u6ca1\u6709\u4e00\u773c\u770b\u51fa\u4e0a\u9762\u7684\u95ee\u9898\uff0c\u53ef\u80fd\u548c\u5b83\u7684\u8868\u73b0\u5f62\u5f0f\u6709\u5173\uff0c\u6211\u4eec\u90fd\u77e5\u9053\uff0c\u901a\u8fc7\u5927\u62ec\u53f7\u53ef\u4ee5\u9650\u5236\u53d8\u91cf\u7684\u4f5c\u7528\u57df\uff0c\u5982\u679c\u4fee\u6539\u4e3a\u4e0b\u9762\u8fd9\u6837\u7684\u5f62\u5f0f\uff0c\u4f60\u5e94\u8be5\u5c31\u80fd\u591f\u770b\u51fa\u4e86\uff1a 1 2 3 4 5 6 7 8 if ( inBigClassroom ) { constexpr int classSize { 30 }; } else { constexpr int classSize { 20 }; } \u53c2\u8003 6.1 - \u590d\u5408\u8bed\u53e5\uff08\u8bed\u53e5\u5757\uff09 \u548c 6.3 - \u5c40\u90e8\u53d8\u91cf \u56e0\u6b64\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u4f7f\u7528 if/else \u5b8c\u6210\u548c\u6761\u4ef6\u8868\u8fbe\u5f0f\u4e00\u6837\u7684\u6548\u679c\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u8fd9\u6837\u505a\uff1b 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int getClassSize ( bool inBigClassroom ) { if ( inBigClassroom ) return 30 ; return 20 ; } int main () { const int classSize { getClassSize ( false ) }; std :: cout << \"The class size is: \" << classSize ; return 0 ; } \u8fd9\u79cd\u65b9\u6cd5\u662f\u53ef\u884c\u7684\u56e0\u4e3a\u5b83\u6ca1\u6709\u5728 if/else \u4e2d\u5b9a\u4e49\u53d8\u91cf\uff0c\u6211\u4eec\u662f\u628a\u503c\u8fd4\u56de\u7ed9\u4e86\u4e3b\u8c03\u51fd\u6570\uff0c\u7136\u540e\u8be5\u503c\u88ab\u7528\u4e8e\u521d\u59cb\u5316\u3002 \u4f46\u8fd9\u4e48\u505a\u663e\u7136\u9700\u8981\u5f88\u591a\u989d\u5916\u7684\u6b65\u9aa4\u3002 \u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u5fc5\u987b\u5339\u914d\u6216\u8005\u53ef\u8f6c\u6362\u4e3a\u5339\u914d\u7684\u7c7b\u578b \u4e3a\u4e86\u7b26\u5408 C++ \u7c7b\u578b\u68c0\u67e5\u7684\u8981\u6c42\uff0c\u6761\u4ef6\u8bed\u53e5\u4e2d\u7684\u4e24\u4e2a\u8868\u8fbe\u5f0f\u7c7b\u578b\u9700\u8981\u5339\u914d\u6216\u8005\u53ef\u4ee5\u8f6c\u53d8\u4e3a\u5171\u540c\u7684\u7c7b\u578b\u3002 \u6269\u5c55\u9605\u8bfb \u5f53\u7c7b\u578b\u4e0d\u5339\u914d\u65f6\uff0c\u4f7f\u7528\u7684\u8f6c\u6362\u89c4\u5219\u975e\u5e38\u590d\u6742\uff0c\u4f60\u53ef\u4ee5\u5728 \u8fd9\u91cc \u627e\u5230\u8be6\u7ec6\u4fe1\u606f \u4e5f\u8bb8\u4f60\u89c9\u5f97\u4e0b\u9762\u8fd9\u6837\u505a\u662f\u53ef\u884c\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { constexpr int x { 5 }; std :: cout << ( x != 5 ? x : \"x is 5\" ); // \u4e0d\u80fd\u7f16\u8bd1 return 0 ; } \u5176\u5b9e\u4e0a\u9762\u7684\u4ee3\u7801\u5e76\u4e0d\u80fd\u7f16\u8bd1\u3002\u56e0\u4e3a\u5176\u4e2d\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7c7b\u578b\u662f\u6574\u578b\uff0c\u800c\u53e6\u5916\u4e00\u4e2a\u662f\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u3002\u7f16\u8bd1\u5668\u4e0d\u80fd\u591f\u786e\u5b9a\u8fd9\u4e9b\u7c7b\u578b\u516c\u5171\u7684\u7c7b\u578b\u662f\u4ec0\u4e48\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f60\u5fc5\u987b\u4f7f\u7528 if/else \u8bed\u53e5\u3002 \u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u4f7f\u7528\u6761\u4ef6\u64cd\u4f5c\u7b26\uff1f \u6761\u4ef6\u8868\u8fbe\u5f0f\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u52a0\u7cbe\u7b80\u7684\u7c7b\u4f3c if/else \u8bed\u53e5\u7684\u529f\u80fd\u3002\u5b83\u5728\u9700\u8981\u8fdb\u884c\u6761\u4ef6\u521d\u59cb\u5316\uff08\u6216\u8d4b\u503c\uff09\u6216\u4f20\u53c2\u65f6\u5c24\u4e3a\u6709\u6548\u3002 \u8bf7\u4e0d\u8981\u5c1d\u8bd5\u5229\u7528\u5b83\u6765\u4ee3\u66ff\u590d\u6742\u7684 if/else \u8bed\u53e5\uff0c\u5426\u5219\u5f88\u5feb\u4f1a\u53d8\u5f97\u590d\u6742\u7684\u96be\u4ee5\u7ef4\u62a4\u3002 \u6700\u4f73\u5b9e\u8df5 \u4ec5\u5728\u9762\u5bf9\u7b80\u5355\u6761\u4ef6\u4e14\u4f60\u4f7f\u7528\u5176\u7ed3\u679c\uff0c\u540c\u65f6\u80fd\u591f\u589e\u5f3a\u53ef\u8bfb\u6027\u7684\u6761\u4ef6\u4e0b\uff0c\u4f7f\u7528\u6761\u4ef6\u8fd0\u7b97\u7b26","title":"5.5 - \u9017\u53f7\u548c\u6761\u4ef6\u8fd0\u7b97\u7b26"},{"location":"5-5-Comma-and-conditional-operators/#_1","text":"\u8fd0\u7b97\u7b26 \u7b26\u53f7 \u5f62\u5f0f \u64cd\u4f5c \u9017\u53f7\u8fd0\u7b97\u7b26 , x, y \u5148\u5bf9 x \u6c42\u503c\uff0c\u518d\u5bf9 y \u6c42\u503c\uff0c\u7136\u540e\u8fd4\u56de y \u6c42\u503c\u7ed3\u679c \u9017\u53f7\u8fd0\u7b97\u7b26 ( , ) \u5141\u8bb8\u4f60\u5728\u5141\u8bb8\u5355\u72ec\u8868\u8fbe\u5f0f\u7684\u5730\u65b9\uff0c\u5bf9\u591a\u4e2a\u8868\u8fbe\u5f0f\u6c42\u503c\u3002\u9017\u53f7\u8868\u8fbe\u5f0f\u9996\u5148\u5bf9\u5b83\u5de6\u4fa7\u7684\u64cd\u4f5c\u6570\u6c42\u503c\uff0c\u7136\u540e\u5bf9\u53f3\u4fa7\u64cd\u4f5c\u6570\u6c42\u503c\uff0c\u7136\u540e\u8fd4\u56de\u53f3\u4fa7\u64cd\u4f5c\u6570\u7684\u7ed3\u679c\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { int x { 1 }; int y { 2 }; std :: cout << ( ++ x , ++ y ); // \u5bf9 x \u548c y \u9012\u589e\uff0c \u5bf9\u53f3\u4fa7\u64cd\u4f5c\u6570\u6c42\u503c return 0 ; } \u9996\u5148\uff0c\u9017\u53f7\u8fd0\u7b97\u7b26\u5de6\u4fa7\u64cd\u4f5c\u6570\u6c42\u503c\uff0c\u5c06 x \u9012\u589e\u4e3a 2\u3002\u7136\u540e\u9017\u53f7\u8fd0\u7b97\u7b26\u53f3\u4fa7\u64cd\u4f5c\u6570\u6c42\u503c\uff0c\u5c06 y \u9012\u589e\u4e3a 3\u3002\u7136\u540e\u9017\u53f7\u8868\u8fbe\u5f0f\u8fd4\u56de\u7684\u662f\u53f3\u4fa7\u6c42\u503c\u7684\u7ed3\u679c\uff083\uff09\uff0c\u5e76\u6253\u5370\u5728\u5c4f\u5e55\u4e0a\u3002 \u6ce8\u610f\uff0c\u9017\u53f7\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u662f\u6700\u4f4e\u7684\uff0c\u751a\u81f3\u6bd4\u8d4b\u503c\u8868\u8fbe\u5f0f\u8fd8\u4f4e\u3002\u56e0\u6b64\uff0c\u4e0b\u9762\u4e24\u884c\u4ee3\u7801\u7684\u6548\u679c\u662f\u4e0d\u540c\u7684\u3002 1 2 z = ( a , b ); // \u5148\u5bf9 (a, b) \u6c42\u503c\u5f97\u5230 b \u7684\u7ed3\u679c\uff0c\u7136\u540e\u8d4b\u503c\u7ed9 z z = a , b ; // \u7b49\u4ef7\u4e8e \"(z = a), b\"\uff0c\u6240\u4ee5 z \u9996\u5148\u88ab\u8d4b\u503c\u4e3a a\uff0c\u7136\u540e b \u6c42\u503c\uff0c\u7ed3\u679c\u4e22\u5f03\u3002 \u8fd9\u4e5f\u4f7f\u5f97\u9017\u53f7\u8fd0\u7b97\u7b26\u7528\u8d77\u6765\u5b58\u5728\u5371\u9669\u3002 \u51e0\u4e4e\u4efb\u4f55\u4f7f\u7528\u9017\u53f7\u8fd0\u7b97\u7b26\u7f16\u5199\u7684\u8bed\u53e5\u90fd\u53ef\u4ee5\u7528\u591a\u4e2a\u5355\u72ec\u7684\u8bed\u53e5\u6765\u66ff\u6362\u3002\u4f8b\u5982\uff0c\u4e0a\u8ff0\u4ee3\u7801\u53ef\u4ee5\u6539\u5199\u6210\u4e0b\u9762\u7684\u5f62\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int x { 1 }; int y { 2 }; ++ x ; std :: cout << ++ y ; return 0 ; } \u5927\u591a\u6570\u7a0b\u5e8f\u5458\u51e0\u4e4e\u4ece\u6765\u4e0d\u4f1a\u4f7f\u7528\u9017\u53f7\u8fd0\u7b97\u7b26\uff0c\u53ea\u6709\u5728 for \u5faa\u73af\u4e2d\u5b58\u5728\u4f8b\u5916\uff0c\u6211\u4eec\u4f1a\u5728 7.9 - for \u8bed\u53e5 \u4e2d\u8be6\u7ec6\u8ba8\u8bba\uff1a \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u4f7f\u7528\u9017\u53f7\u8fd0\u7b97\u7b26\uff0c\u9664\u975e\u662f\u5728 for \u5faa\u73af\u4e2d\u3002","title":"\u9017\u53f7\u8fd0\u7b97\u7b26"},{"location":"5-5-Comma-and-conditional-operators/#_2","text":"\u5728 C++\uff0c\u9017\u53f7\u5e38\u7528\u4f5c\u5206\u9694\u7b26\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u548c\u9017\u53f7\u8fd0\u7b97\u7b26\u5e76\u6ca1\u6709\u5173\u7cfb\uff0c\u4e5f\u4e0d\u5177\u5907\u9017\u53f7\u8fd0\u7b97\u7b26\u7684\u529f\u80fd\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 void foo ( int x , int y ) // \u5728\u51fd\u6570\u4e2d\u9017\u53f7\u7528\u4e8e\u5206\u5272\u53c2\u6570 { add ( x , y ); // Comma used to separate arguments in function call constexpr int z { 3 }, w { 5 }; // \u9017\u53f7\u7528\u4e8e\u5206\u5272\u540c\u4e00\u884c\u4e2d\u5b9a\u4e49\u7684\u591a\u4e2a\u53d8\u91cf\uff08\u4e0d\u8981\u8fd9\u4e48\u505a\uff09 } \u4e0d\u9700\u8981\u907f\u514d\u4f7f\u7528\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\uff08\u9664\u975e\u662f\u58f0\u660e\u591a\u4e2a\u53d8\u91cf\u65f6\uff0c\u4e0d\u8981\u8fd9\u4e48\u505a\uff09\u3002","title":"\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26"},{"location":"5-5-Comma-and-conditional-operators/#_3","text":"\u8fd0\u7b97\u7b26 \u7b26\u53f7 \u5f62\u5f0f \u64cd\u4f5c \u6761\u4ef6\u8fd0\u7b97\u7b26 ?: c ? x : y \u5982\u679c c \u975e\u96f6\uff08\u771f\uff09\u5219\u5bf9 x \u5176\u4e2d\uff0c\u5426\u5219\u5bf9 y \u6c42\u503c \u6761\u4ef6\u8fd0\u7b97\u7b26 ( ?: ) (\u6709\u65f6\u4e5f\u79f0\u4e3a \u201d\u7b97\u6570 if \u201c\u8fd0\u7b97\u7b26) \u662f\u4e00\u4e2a\u4e09\u5143\u8fd0\u7b97\u7b26\uff08\u6709\u4e09\u4e2a\u64cd\u4f5c\u6570\uff09\u3002\u56e0\u4e3a\u5b83\u662f C++ \u4e2d\u552f\u4e00\u7684\u4e09\u5143\u8fd0\u7b97\u7b26\uff0c\u56e0\u6b64\u6709\u4f7f\u7528\u4e5f\u79f0\u5b83\u4e3a\u201d\u90a3\u4e2a\u4e09\u5143\u8fd0\u7b97\u7b26\u201c\u3002 ?: \u8fd0\u7b97\u7b26\u63d0\u4f9b\u4e86\u4e00\u79cd\u5feb\u6377\u7684\u6761\u4ef6\u8bed\u53e5\u529f\u80fd\u3002\u5982\u679c\u4f60\u5bf9\u6761\u4ef6\u8bed\u53e5\u8fd8\u6709\u7591\u95ee\uff0c\u8bf7\u53c2\u8003 4.10 - if \u8bed\u53e5\u7b80\u4ecb \u3002 if/else \u8bed\u53e5\u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 2 3 4 if ( condition ) statement1 ; else statement2 ; \u5982\u679c condition \u6c42\u503c\u7ed3\u679c\u4e3a true \uff0c\u5219 statement1 \u4f1a\u88ab\u6267\u884c\uff0c\u5426\u5219\u6267\u884c statement2 \u3002 ?: \u8fd0\u7b97\u7b26\u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 (condition) ? expression1 : expression2; \u5982\u679c condition \u6c42\u503c\u7ed3\u679c\u4e3a true \uff0c\u5219 statement1 \u4f1a\u88ab\u6267\u884c\uff0c\u5426\u5219\u6267\u884c statement2 \u3002\u6ce8\u610f\uff0c\u8fd9\u91cc\u7684 expression2 \u662f\u5fc5\u987b\u8981\u6709\u7684\u3002 \u4e0b\u9762\u8fd9\u4e2a if/else \u8bed\u53e5\uff1a 1 2 3 4 if ( x > y ) larger = x ; else larger = y ; \u53ef\u4ee5\u6539\u5199\u6210\u5982\u4e0b\u5f62\u5f0f\uff1a 1 larger = ( x > y ) ? x : y ; \u8fd9\u6837\u53ef\u4ee5\u5728\u4e0d\u727a\u7272\u53ef\u8bfb\u6027\u7684\u524d\u63d0\u4e0b\uff0c\u8ba9\u4ee3\u7801\u53d8\u5f97\u66f4\u52a0\u7d27\u51d1\u3002","title":"\u6761\u4ef6\u8fd0\u7b97\u7b26"},{"location":"5-5-Comma-and-conditional-operators/#_4","text":"\u6839\u636e\u60ef\u4f8b\uff0c\u6211\u4eec\u4e00\u822c\u4f1a\u628a\u6761\u4ef6\u8fd0\u7b97\u7b26\u7684 \u6761\u4ef6 \u90e8\u5206\u653e\u5728\u62ec\u53f7\u91cc\uff0c\u8fd9\u6837\u4e0d\u4ec5\u53ef\u8bfb\u6027\u66f4\u597d\uff0c\u800c\u4e14\u53ef\u4ee5\u786e\u4fdd\u8fd0\u7b97\u4f18\u5148\u7ea7\u662f\u6b63\u786e\u7684\u3002\u5176\u4ed6\u64cd\u4f5c\u6570\u7684\u6c42\u503c\u4f18\u5148\u7ea7\uff0c\u52a0\u4e0d\u52a0\u62ec\u53f7\u90fd\u662f\u4e00\u6837\u7684\uff0c\u6240\u4ee5\u5c31\u6ca1\u5fc5\u8981\u663e\u793a\u5730\u6dfb\u52a0\u62ec\u53f7\u4e86\u3002 \u6ce8\u610f\uff0c ?: \u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u975e\u5e38\u4f4e\u3002\u9664\u975e\u662f\u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u91cc\uff0c\u4e0d\u7136\u6574\u4e2a\u6761\u4ef6\u8fd0\u7b97\u7b26\u8868\u8fbe\u5f0f\u4e5f\u8981\u653e\u5728\u62ec\u53f7\u4e2d\u3002 \u4f8b\u5982\uff0c\u4e3a\u4e86\u6253\u5370 x \u548c y \u4e2d\u8f83\u5927\u7684\u503c\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 2 3 4 if ( x > y ) std :: cout << x ; else std :: cout << y ; \u6216\u8005\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8fd0\u7b97\u7b26\uff1a 1 std :: cout << (( x > y ) ? x : y ); \u8ba9\u6211\u4eec\u6d4b\u8bd5\u4e00\u4e0b\uff0c\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c\u5982\u679c\u4e0d\u628a\u6761\u4ef6\u8fd0\u7b97\u7b26\u8868\u8fbe\u5f0f\u653e\u5728\u4e00\u4e2a\u62ec\u53f7\u4e2d\u4f1a\u600e\u6837\u3002 \u56e0 << operator \u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e ?: \u8fd0\u7b97\u7b26\uff0c\u56e0\u6b64\u4e0b\u9762\u8bed\u53e5\uff1a 1 std :: cout << ( x > y ) ? x : y ; \u7b49\u4ef7\u4e8e\uff1a 1 ( std :: cout << ( x > y )) ? x : y ; \u56e0\u6b64\uff0c\u5982\u679c x>y \u5219\u7ed3\u679c\u4f1a\u6253\u5370 1(true)\uff0c\u5426\u5219\u6253\u53700\u3002 \u6700\u4f73\u5b9e\u8df5 \u603b\u662f\u628a\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u6761\u4ef6\u90e8\u5206\u653e\u5728\u62ec\u53f7\u91cc\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u8003\u8651\u628a\u6574\u4e2a\u90e8\u5206\u90fd\u653e\u5728\u62ec\u53f7\u91cc\u3002","title":"\u4e3a\u6761\u4ef6\u8fd0\u7b97\u7b26\u6dfb\u52a0\u62ec\u53f7"},{"location":"5-5-Comma-and-conditional-operators/#_5","text":"\u56e0\u4e3a\u6761\u4ef6\u8fd0\u7b97\u7b26\u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u8bed\u53e5\uff0c\u6240\u4ee5\u6761\u4ef6\u8fd0\u7b97\u7b26\u53ef\u4ee5\u88ab\u7528\u5728\u65e0\u6cd5\u4f7f\u7528 if/else \u7684\u5730\u65b9\u3002 \u4f8b\u5982\uff0c\u53ef\u4ee5\u7528\u5b83\u6765\u521d\u59cb\u5316\u4e00\u4e2a\u5e38\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { constexpr bool inBigClassroom { false }; constexpr int classSize { inBigClassroom ? 30 : 20 }; std :: cout << \"The class size is: \" << classSize << '\\n' ; return 0 ; } \u4f7f\u7528 if/else \u5e76\u4e0d\u80fd\u7528\u4e8e\u8fd9\u79cd\u5e38\u89c1\uff0c\u4f60\u53ef\u80fd\u4f1a\u89c9\u5f97\u4e0b\u9762\u8fd9\u79cd\u65b9\u6cd5\u662f\u53ef\u884c\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { constexpr bool inBigClassroom { false }; if ( inBigClassroom ) constexpr int classSize { 30 }; else constexpr int classSize { 20 }; std :: cout << \"The class size is: \" << classSize ; return 0 ; } \u5b9e\u9645\u4e0a\uff0c\u4e0a\u8ff0\u4ee3\u7801\u662f\u4e0d\u80fd\u7f16\u8bd1\u7684\uff0c\u4f60\u4f1a\u88ab\u544a\u77e5 claseSize \u6ca1\u6709\u5b9a\u4e49\u3002\u6b63\u5982\u5b9a\u4e49\u5728\u51fd\u6570\u4e2d\u7684\u53d8\u91cf\u4f1a\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u9500\u6bc1\u4e00\u6837\uff0c\u5b9a\u4e49\u5728 if/else \u8bed\u53e5\u5757\u4e2d\u7684\u53d8\u91cf\u4e5f\u4f1a\u5728 if/else \u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u6253\u5370 classSize \u65f6\uff0c\u8be5\u53d8\u91cf\u5b9e\u9645\u4e0a\u5df2\u7ecf\u88ab\u9500\u6bc1\u4e86\u3002 \u8bd1\u8005\u6ce8 \u5982\u679c\u4f60\u6ca1\u6709\u4e00\u773c\u770b\u51fa\u4e0a\u9762\u7684\u95ee\u9898\uff0c\u53ef\u80fd\u548c\u5b83\u7684\u8868\u73b0\u5f62\u5f0f\u6709\u5173\uff0c\u6211\u4eec\u90fd\u77e5\u9053\uff0c\u901a\u8fc7\u5927\u62ec\u53f7\u53ef\u4ee5\u9650\u5236\u53d8\u91cf\u7684\u4f5c\u7528\u57df\uff0c\u5982\u679c\u4fee\u6539\u4e3a\u4e0b\u9762\u8fd9\u6837\u7684\u5f62\u5f0f\uff0c\u4f60\u5e94\u8be5\u5c31\u80fd\u591f\u770b\u51fa\u4e86\uff1a 1 2 3 4 5 6 7 8 if ( inBigClassroom ) { constexpr int classSize { 30 }; } else { constexpr int classSize { 20 }; } \u53c2\u8003 6.1 - \u590d\u5408\u8bed\u53e5\uff08\u8bed\u53e5\u5757\uff09 \u548c 6.3 - \u5c40\u90e8\u53d8\u91cf \u56e0\u6b64\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u4f7f\u7528 if/else \u5b8c\u6210\u548c\u6761\u4ef6\u8868\u8fbe\u5f0f\u4e00\u6837\u7684\u6548\u679c\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u8fd9\u6837\u505a\uff1b 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int getClassSize ( bool inBigClassroom ) { if ( inBigClassroom ) return 30 ; return 20 ; } int main () { const int classSize { getClassSize ( false ) }; std :: cout << \"The class size is: \" << classSize ; return 0 ; } \u8fd9\u79cd\u65b9\u6cd5\u662f\u53ef\u884c\u7684\u56e0\u4e3a\u5b83\u6ca1\u6709\u5728 if/else \u4e2d\u5b9a\u4e49\u53d8\u91cf\uff0c\u6211\u4eec\u662f\u628a\u503c\u8fd4\u56de\u7ed9\u4e86\u4e3b\u8c03\u51fd\u6570\uff0c\u7136\u540e\u8be5\u503c\u88ab\u7528\u4e8e\u521d\u59cb\u5316\u3002 \u4f46\u8fd9\u4e48\u505a\u663e\u7136\u9700\u8981\u5f88\u591a\u989d\u5916\u7684\u6b65\u9aa4\u3002","title":"\u6761\u4ef6\u8fd0\u7b97\u7b26\u4f5c\u4e3a\u8868\u8fbe\u5f0f\u6c42\u503c"},{"location":"5-5-Comma-and-conditional-operators/#_6","text":"\u4e3a\u4e86\u7b26\u5408 C++ \u7c7b\u578b\u68c0\u67e5\u7684\u8981\u6c42\uff0c\u6761\u4ef6\u8bed\u53e5\u4e2d\u7684\u4e24\u4e2a\u8868\u8fbe\u5f0f\u7c7b\u578b\u9700\u8981\u5339\u914d\u6216\u8005\u53ef\u4ee5\u8f6c\u53d8\u4e3a\u5171\u540c\u7684\u7c7b\u578b\u3002 \u6269\u5c55\u9605\u8bfb \u5f53\u7c7b\u578b\u4e0d\u5339\u914d\u65f6\uff0c\u4f7f\u7528\u7684\u8f6c\u6362\u89c4\u5219\u975e\u5e38\u590d\u6742\uff0c\u4f60\u53ef\u4ee5\u5728 \u8fd9\u91cc \u627e\u5230\u8be6\u7ec6\u4fe1\u606f \u4e5f\u8bb8\u4f60\u89c9\u5f97\u4e0b\u9762\u8fd9\u6837\u505a\u662f\u53ef\u884c\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { constexpr int x { 5 }; std :: cout << ( x != 5 ? x : \"x is 5\" ); // \u4e0d\u80fd\u7f16\u8bd1 return 0 ; } \u5176\u5b9e\u4e0a\u9762\u7684\u4ee3\u7801\u5e76\u4e0d\u80fd\u7f16\u8bd1\u3002\u56e0\u4e3a\u5176\u4e2d\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7c7b\u578b\u662f\u6574\u578b\uff0c\u800c\u53e6\u5916\u4e00\u4e2a\u662f\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u3002\u7f16\u8bd1\u5668\u4e0d\u80fd\u591f\u786e\u5b9a\u8fd9\u4e9b\u7c7b\u578b\u516c\u5171\u7684\u7c7b\u578b\u662f\u4ec0\u4e48\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f60\u5fc5\u987b\u4f7f\u7528 if/else \u8bed\u53e5\u3002","title":"\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u5fc5\u987b\u5339\u914d\u6216\u8005\u53ef\u8f6c\u6362\u4e3a\u5339\u914d\u7684\u7c7b\u578b"},{"location":"5-5-Comma-and-conditional-operators/#_7","text":"\u6761\u4ef6\u8868\u8fbe\u5f0f\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u52a0\u7cbe\u7b80\u7684\u7c7b\u4f3c if/else \u8bed\u53e5\u7684\u529f\u80fd\u3002\u5b83\u5728\u9700\u8981\u8fdb\u884c\u6761\u4ef6\u521d\u59cb\u5316\uff08\u6216\u8d4b\u503c\uff09\u6216\u4f20\u53c2\u65f6\u5c24\u4e3a\u6709\u6548\u3002 \u8bf7\u4e0d\u8981\u5c1d\u8bd5\u5229\u7528\u5b83\u6765\u4ee3\u66ff\u590d\u6742\u7684 if/else \u8bed\u53e5\uff0c\u5426\u5219\u5f88\u5feb\u4f1a\u53d8\u5f97\u590d\u6742\u7684\u96be\u4ee5\u7ef4\u62a4\u3002 \u6700\u4f73\u5b9e\u8df5 \u4ec5\u5728\u9762\u5bf9\u7b80\u5355\u6761\u4ef6\u4e14\u4f60\u4f7f\u7528\u5176\u7ed3\u679c\uff0c\u540c\u65f6\u80fd\u591f\u589e\u5f3a\u53ef\u8bfb\u6027\u7684\u6761\u4ef6\u4e0b\uff0c\u4f7f\u7528\u6761\u4ef6\u8fd0\u7b97\u7b26","title":"\u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u4f7f\u7528\u6761\u4ef6\u64cd\u4f5c\u7b26\uff1f"},{"location":"5-6-Relational-operators-and-floating-point-comparisons/","tags":["operator","float point"],"text":"Key Takeaway \u8bf7\u907f\u514d\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528 operator== \u548c operator!= \u3002 \u6bd4\u8f83\u8fd0\u7b97\u7b26\u53ef\u4ee5\u5e94\u7528\u4e8e\u6d6e\u70b9\u6570 \u5173\u7cfb\u8fd0\u7b97\u7b26\u6307\u7684\u662f\u90a3\u4e9b\u5141\u8bb8\u6211\u4eec\u6bd4\u8f83\u4e24\u4e2a\u6570\u7684\u8fd0\u7b97\u7b26\uff0c\u8fd9\u7c7b\u8fd0\u7b97\u7b26\u6709 6 \u4e2a\uff1a \u8fd0\u7b97\u7b26 \u7b26\u53f7 \u5f62\u5f0f \u64cd\u4f5c \u5927\u4e8e > x > y \u5982\u679c x \u5927\u4e8e y\uff0c\u5219\u8fd4\u56de true\uff0c\u5426\u5219\u4e3a false \u5c0f\u4e8e < x < y \u5982\u679c x \u5c0f\u4e8e y\uff0c\u5219\u8fd4\u56de true\uff0c\u5426\u5219\u4e3a false \u5927\u4e8e\u7b49\u4e8e >= x >= y \u5982\u679c x \u5927\u4e8e\u7b49\u4e8e y\uff0c\u5219\u8fd4\u56de true\uff0c\u5426\u5219\u4e3a false \u5c0f\u4e8e\u7b49\u4e8e <= x <= y \u5982\u679c x \u5c0f\u4e8e\u7b49\u4e8e y\uff0c\u5219\u8fd4\u56de true\uff0c\u5426\u5219\u4e3a false \u76f8\u7b49 == x == y \u5982\u679c x \u76f8\u7b49 y\uff0c\u5219\u8fd4\u56de true\uff0c\u5426\u5219\u4e3a false \u4e0d\u7b49 != x != y \u5982\u679c x \u4e0d\u7b49 y\uff0c\u5219\u8fd4\u56de true\uff0c\u5426\u5219\u4e3a false \u8fd9\u91cc\u9762\u5927\u591a\u6570\u8fd0\u7b97\u7b26\u5982\u4f55\u5de5\u4f5c\uff0c\u76f8\u6bd4\u4f60\u5df2\u7ecf\u89c1\u8bc6\u8fc7\u4e86\uff0c\u800c\u4e14\u90fd\u975e\u5e38\u7b26\u5408\u5e38\u8bc6\u3002\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u7684\u6c42\u503c\u7ed3\u679c\u53ea\u4f1a\u662f true (1) \u6216 false (0) \u4e24\u79cd\u3002 \u4e0b\u9762\u4f8b\u7a0b\u5c55\u793a\u4e86\u5b83\u4eec\u7684\u4f7f\u7528\u65b9\u6cd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another integer: \" ; int y {}; std :: cin >> y ; if ( x == y ) std :: cout << x << \" equals \" << y << '\\n' ; if ( x != y ) std :: cout << x << \" does not equal \" << y << '\\n' ; if ( x > y ) std :: cout << x << \" is greater than \" << y << '\\n' ; if ( x < y ) std :: cout << x << \" is less than \" << y << '\\n' ; if ( x >= y ) std :: cout << x << \" is greater than or equal to \" << y << '\\n' ; if ( x <= y ) std :: cout << x << \" is less than or equal to \" << y << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 Enter an integer: 4 Enter another integer: 5 4 does not equal 5 4 is less than 5 4 is less than or equal to 5 \u5728\u5bf9\u6574\u578b\u8fdb\u884c\u6bd4\u8f83\u7684\u65f6\u5019\uff0c\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u90fd\u975e\u5e38\u7b80\u5355\u3002 \u5e03\u5c14\u6761\u4ef6\u503c \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c if \u8bed\u53e5\u6216 \u6761\u4ef6\u8fd0\u7b97\u7b26 \uff08\u4ee5\u53ca\u5176\u4ed6\u4e00\u4e9b\u7279\u6b8a\u573a\u666f\u4e0b\uff09\u4e2d\u7684\u6761\u4ef6\u6c42\u503c\u7ed3\u679c\u603b\u662f\u5e03\u5c14\u7c7b\u578b\u7684\u503c\u3002 \u5f88\u591a\u7a0b\u5e8f\u5458\u4f1a\u4f7f\u7528\u4e0b\u9762\u7684\u5199\u6cd5\uff1a 1 if ( b1 == true ) ... \u4f46\u662f\u8fd9\u79cd\u5199\u6cd5\u6709\u4e9b\u5197\u4f59\uff0c\u56e0\u4e3a == true \u5e76\u6ca1\u6709\u4e3a\u8fd9\u4e2a\u6761\u4ef6\u6dfb\u52a0\u4efb\u4f55\u503c\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u8fd9\u4e48\u5199\uff1a 1 if ( b1 ) ... \u540c\u6837\u7684\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 if ( b1 == false ) ... \u6700\u597d\u5199\u6210\u8fd9\u6837\uff1a 1 if ( ! b1 ) ... \u6700\u4f73\u5b9e\u8df5 \u4e0d\u8981\u4e3a\u6761\u4ef6\u6dfb\u52a0\u6ca1\u5fc5\u8981\u7684 == \u6216 != \uff0c\u5982\u679c\u6ca1\u6709\u5176\u4ed6\u7684\u503c\u5219\u4f1a\u8ba9\u53ef\u8bfb\u6027\u53d8\u5f97\u66f4\u5dee \u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\u53ef\u80fd\u4f1a\u5e26\u6765\u95ee\u9898 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { double d1 { 100.0 - 99.99 }; // \u5e94\u8be5\u7b49\u4e8e 0.01 double d2 { 10.0 - 9.99 }; // \u5e94\u8be5\u7b49\u4e8e 0.01 if ( d1 == d2 ) std :: cout << \"d1 == d2\" << '\\n' ; else if ( d1 > d2 ) std :: cout << \"d1 > d2\" << '\\n' ; else if ( d1 < d2 ) std :: cout << \"d1 < d2\" << '\\n' ; return 0 ; } \u53d8\u91cf d1 \u548c d2 \u5e94\u8be5\u90fd\u7b49\u4e8e 0.01 \u3002\u4f46\u662f\u4f60\u5bf9\u5b83\u4eec\u6bd4\u8f83\u7684\u8bdd\uff0c\u5c06\u4f1a\u4ea7\u751f\u4ee4\u4eba\u610f\u5916\u7684\u7ed3\u679c\uff1a 1 d1 > d2 \u5982\u679c\u4f60\u4f7f\u7528\u8c03\u8bd5\u5668\u6765\u67e5\u770b\u4e24\u4e2a\u53d8\u91cf\uff0c\u5219 d1 = 0.0100000000000005116 \u548c d2 = 0.0099999999999997868 \u3002\u8fd9\u4e24\u4e2a\u503c\u90fd\u63a5\u8fd1\u4e8e 0.01\uff0c\u4f46\u662f d1 \u6bd4 0.1 \u5927\uff0cd2 \u5219\u6bd4 0.1 \u5c0f\u3002 \u5982\u679c\u4f60\u9700\u8981\u5f88\u9ad8\u7684\u7cbe\u5ea6\uff0c\u5219\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528\u4e0a\u8ff0\u6bd4\u8f83\u8fd0\u7b97\u7b26\u662f\u5f88\u5371\u9669\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u6d6e\u70b9\u6570\u5e76\u4e0d\u7cbe\u786e\uff0c\u5f88\u5c0f\u7684 \u820d\u5165\u8bef\u5dee \u90fd\u6709\u53ef\u80fd\u9020\u6210\u4e0a\u8ff0\u610f\u5916\u60c5\u51b5\u3002 \u6211\u4eec\u5728 4.8 - \u6d6e\u70b9\u6570 \u4e2d\u4ecb\u7ecd\u4e86\u820d\u5165\u8bef\u5dee\u7684\u95ee\u9898\u3002 \u5f53\u5927\u4e8e\u5c0f\u4e8e\u53f7( < , <= , > \u548c >= ) \u6bcf\u7528\u5728\u6d6e\u70b9\u6570\u6bd4\u8f83\u65f6\uff0c\u901a\u5e38\u662f\u53ef\u4ee5\u5f97\u5230\u6b63\u786e\u7ed3\u679c\u7684\uff08\u9664\u975e\u4e24\u4e2a\u6570\u975e\u5e38\u63a5\u8fd1\uff09\u3002\u56e0\u6b64\uff0c\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528\u4f7f\u7528\u6b64\u7c7b\u6bd4\u8f83\u8fd0\u7b97\u7b26\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\uff0c\u53ea\u6709\u5f53\u4e24\u4e2a\u6570\u975e\u5e38\u63a5\u8fd1\u7684\u65f6\u5019\u624d\u6709\u53ef\u80fd\u5f97\u5230\u9519\u8bef\u7684\u7ed3\u679c\u3002 \u4f8b\u5982\uff0c\u8003\u8651\u6211\u4eec\u5728\u8bbe\u8ba1\u4e00\u4e2a\u6e38\u620f\uff08\u6bd4\u65b9\u8bf4\u300a\u592a\u7a7a\u4fb5\u7565\u8005\u300b\uff09\uff0c\u6b64\u65f6\u4f60\u9700\u8981\u5224\u65ad\u4e24\u4e2a\u7269\u4f53\u662f\u5426\u4f1a\u76f8\u4ea4\uff08\u6bd4\u5982\u8bf4\u5bfc\u5f39\u548c\u5916\u661f\u4eba\uff09\u3002\u5f53\u4e24\u4e2a\u7269\u4f53\u76f8\u8ddd\u751a\u8fdc\u65f6\uff0c\u8fd9\u4e9b\u6bd4\u8f83\u8fd0\u7b97\u7b26\u53ef\u4ee5\u8fd4\u56de\u6b63\u786e\u7684\u7ed3\u679c\u3002\u5f53\u4e24\u4e2a\u7269\u4f53\u5df2\u7ecf\u975e\u5e38\u63a5\u8fd1\u65f6\uff0c\u90a3\u4f60\u5176\u5b9e\u5df2\u7ecf\u5f97\u5230\u7ed3\u679c\u4e86\uff0c\u5373\u4f7f\u6bd4\u8f83\u7684\u7ed3\u8bba\u662f\u9519\u8bef\u7684\uff0c\u4f60\u53ef\u80fd\u4e5f\u4e0d\u4f1a\u6ce8\u610f\u5230\uff08\u770b\u4e0a\u53bb\u662f\u5728\u975e\u5e38\u8fd1\u7684\u4e3e\u4f8b\u547d\u4e2d\u6216\u4e22\u5931\u4e86\uff09\uff0c\u4e5f\u4e0d\u4f1a\u5bf9\u4f60\u7684\u6e38\u620f\u9020\u6210\u4e25\u91cd\u7684\u5f71\u54cd\u3002 \u6d6e\u70b9\u503c\u76f8\u7b49 \u76f8\u7b49\u8fd0\u7b97\u7b26( == \u548c != ) \u7684\u9ebb\u70e6\u5c31\u6bd4\u8f83\u5927\u3002\u5bf9\u4e8e\u8fd0\u7b97\u7b26 operator== \uff0c\u5b83\u53ea\u6709\u5728\u4e24\u4e2a\u64cd\u4f5c\u6570\u5b8c\u5168\u76f8\u7b49\u65f6\u624d\u8fd4\u56de true \u3002\u56e0\u4e3a\u5f88\u5c0f\u7684\u820d\u5165\u8bef\u5dee\u5c31\u4f1a\u4f7f\u4e24\u4e2a\u6d6e\u70b9\u6570\u4e0d\u7b49\uff0c\u6240\u4ee5 operator== \u6709\u975e\u5e38\u5927\u7684\u53ef\u80fd\u5728\u4f60\u8ba4\u4e3a\u5e94\u8be5\u8fd4\u56de true \u7684\u65f6\u5019\u8fd4\u56de false \u3002 Operator!= \u4e5f\u6709\u7c7b\u4f3c\u7684\u95ee\u9898\u3002 \u56e0\u6b64\uff0c\u8bf7\u907f\u514d\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528\u8fd9\u4e24\u79cd\u8fd0\u7b97\u7b26\u3002 \u6ce8\u610f \u8bf7\u907f\u514d\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528 operator== \u548c operator!= \u3002 \u6bd4\u8f83\u6d6e\u70b9\u6570 \uff08\u6269\u5c55\u9605\u8bfb\uff09 \u90a3\u4e48\uff0c\u5bf9\u4e24\u4e2a\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\u7684\u5408\u7406\u65b9\u6cd5\u662f\u4ec0\u4e48\u5462\uff1f \u5224\u65ad\u6d6e\u70b9\u6570\u662f\u5426\u76f8\u7b49\u7684\u5e38\u7528\u65b9\u6cd5\uff0c\u662f\u4f7f\u7528\u4e00\u4e2a\u51fd\u6570\u6765\u5224\u65ad\u4e24\u79cd\u662f\u4e0d\u662f \u975e\u5e38\u63a5\u8fd1 \u3002\u53ea\u8981\u975e\u5e38\u63a5\u8fd1\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u79f0\u5176 \u201d\u76f8\u7b49\u201c \u3002\u8868\u793a\u201d\u975e\u5e38\u63a5\u8fd1\u201c\u7684\u4f20\u7edf\u65b9\u6cd5\u662f\u4f7f\u7528epsilon\u3002\u5b83\u901a\u5e38\u4f1a\u88ab\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u975e\u5e38\u5c0f\u7684\u6b63\u6570\uff08\u4f8b\u5982\uff0c 0.00000001 \u6709\u65f6\u5019\u5199\u4f5c 1e-8 \uff09\u3002 \u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u4f7f\u7528\u81ea\u5df1\u5b9a\u4e49\u7684\u201d\u975e\u5e38\u63a5\u8fd1\u201c\u51fd\u6570\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 #include // for std::abs() // epsilon is an absolute value bool approximatelyEqualAbs ( double a , double b , double absEpsilon ) { // if the distance between a and b is less than absEpsilon, then a and b are \"close enough\" return std :: abs ( a - b ) <= absEpsilon ; } std::abs() \u662f \u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u5b83\u8fd4\u56de\u7684\u662f\u5176 \u5b9e\u53c2 \u7684\u7edd\u5bf9\u503c\u3002 std::abs(a - b) <= absEpsilon \u4f1a\u68c0\u67e5 a \u548c b \u7684\u5dee\u7684\u7edd\u5bf9\u503c\u662f\u5426\u5c0f\u4e8e\u4f20\u5165\u7684\u4efb\u4f55\u8868\u793a\u5f88\u63a5\u8fd1\u503c\u7684 \"epsilon\"\u3002\u5982\u679c a \u548c b \u8db3\u591f\u63a5\u8fd1\uff0c\u5219\u51fd\u6570\u4f1a\u8fd4\u56de true\uff0c\u8868\u793a\u5b83\u4eec\u76f8\u7b49\uff0c\u5426\u5219\u5219\u8fd4\u56de false\u3002 \u5c3d\u7ba1\u8fd9\u4e2a\u51fd\u6570\u786e\u5b9e\u53ef\u7528\uff0c\u4f46\u5e76\u4e0d\u662f\u6700\u4f73\u7684\u89e3\u51b3\u65b9\u6848\u3002 0.00001 \u5bf9\u4e8e1.0\u5de6\u53f3\u7684\u8f93\u5165\u662f\u5408\u9002\u7684\uff0c\u4f46\u662f\u5bf9\u4e8e 0.0000001 \u5de6\u53f3\u7684\u6570\u5c31\u592a\u5927\u4e86\uff0c\u800c\u5bf9\u4e8e 10000 \u8fd9\u6837\u7684\u8f93\u5165\u53c8\u592a\u5c0f\u4e86\u3002 \u9898\u5916\u8bdd \u5982\u679c\u6211\u4eec\u8bf4\uff0c\u5f53\u4e24\u4e2a\u6570\u7684\u5dee\u8ddd\u5c0f\u4e8e 0.00001 \u65f6\uff0c\u5b83\u4eec\u5c31\u80fd\u88ab\u770b\u505a\u662f\u76f8\u540c\u7684\u6570\uff0c\u90a3\u4e48\uff1a 1 \u548c 1.0001 \u662f\u4e0d\u540c\u7684\uff0c\u4f46\u662f 1 \u548c 1.00001 \u5219\u662f\u76f8\u540c\u7684\uff0c\u8fd9\u5e76\u4e0d\u5408\u7406\uff1b 0.0000001 \u548c 0.00001 \u662f\u76f8\u540c\u7684\uff0c\u8fd9\u770b\u8d77\u6765\u5e76\u4e0d\u5408\u7406\uff0c\u6bd4\u8f83\u5b83\u4eec\u5dee\u4e86\u4e00\u4e2a\u6570\u91cf\u7ea7\uff1b 10000 \u548c 10000.00001 \u5219\u662f\u4e0d\u540c\u7684\uff0c\u8fd9\u770b\u8d77\u6765\u4e5f\u5e76\u4e0d\u5408\u7406\uff0c\u8003\u8651\u5230\u8fd9\u4e24\u4e2a\u6570\u7684\u6570\u91cf\u7ea7\uff0c\u6709\u8fd9\u4e48\u5c0f\u7684\u5dee\u5f02\u88ab\u770b\u505a\u662f\u4e0d\u540c\u7684\u4e24\u4e2a\u6570\u663e\u7136\u4e0d\u5408\u7406\u3002 \u8fd9\u4e5f\u610f\u5473\u7740\uff0c\u6bcf\u6b21\u8c03\u7528\u8be5\u51fd\u6570\u7684\u65f6\u5019\uff0c\u6211\u4eec\u90fd\u5fc5\u987b\u4e3a\u8f93\u5165\u7684\u503c\u6311\u9009\u4e00\u4e2a\u5408\u9002\u7684 epsilon \u503c\u3002\u65e2\u7136\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u4e86 epsilon \u7684\u503c\u9700\u8981\u6839\u636e\u8f93\u5165\u6570\u636e\u7684\u6570\u91cf\u7ea7\u53d8\u5316\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u5e94\u8be5\u901a\u8fc7\u4fee\u6539\u51fd\u6570\u6765\u4f7f\u5176\u81ea\u52a8\u4e3a\u6211\u4eec\u5b8c\u6210\u8fd9\u4e2a\u5de5\u4f5c\u3002 Donald Knuth \u662f\u4e00\u4e2a\u8457\u540d\u7684\u8ba1\u7b97\u673a\u79d1\u5b66\u5bb6\uff0c\u4ed6\u5728\u4ed6\u7684\u8457\u4f5c \u201c\u8ba1\u7b97\u673a\u7a0b\u5e8f\u8bbe\u8ba1\u827a\u672f\u7b2c\u4e8c\u5377\uff1a\u534a\u6570\u503c\u7b97\u6cd5(Addison-Wesley, 1969)\u201c \u4e2d\u4ecb\u7ecd\u4e86\u8fd9\u6837\u7684\u65b9\u6cd5\uff1a 1 2 3 4 5 6 7 8 #include // std::max #include // std::abs // return true if the difference between a and b is within epsilon percent of the larger of a and b bool approximatelyEqualRel ( double a , double b , double relEpsilon ) { return ( std :: abs ( a - b ) <= ( std :: max ( std :: abs ( a ), std :: abs ( b )) * relEpsilon )); } \u548c\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e00\u4e2a\u7edd\u5bf9\u7684 epsilon \u503c\u76f8\u6bd4\uff0c\u8fd9\u91cc\u7684 epsilon \u662f\u4e00\u4e2a\u4e0e a \u6216 b \u6570\u91cf\u7ea7\u6709\u5173\u7684\u76f8\u5bf9\u503c\u3002 \u8ba9\u6211\u4eec\u4ed4\u7ec6\u5206\u6790\u4e00\u4e0b\u8fd9\u4e2a\u770b\u4e0a\u53bb\u975e\u5e38\u590d\u6742\u7684\u51fd\u6570\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u5728\u4e0d\u7b49\u53f7 <= operator \u7684\u5de6\u8fb9 std::abs(a - b) \u4e3a\u6211\u4eec\u6c42\u51fa\u4e86 a \u548c b \u4e4b\u95f4\u7684\u5dee\u5f02\uff08\u4ee5\u4e00\u4e2a\u6b63\u6570\u6765\u8868\u793a\uff09\u3002 \u5728\u4e0d\u7b49\u53f7 <= operator \u7684\u53f3\u8fb9\uff0c\u6211\u4eec\u9700\u8981\u8ba1\u7b97\u90a3\u4e2a\u6240\u8c13\u201d\u8db3\u591f\u63a5\u8fd1\u201c\u7684\u503c\u7684\u6700\u5927\u503c\u3002\u4e3a\u6b64\uff0c\u8fd9\u4e2a\u7b97\u6cd5\u9009\u53d6\u4e86 a \u548c b \u4e2d\u8f83\u5927\u7684\u4e00\u4e2a\u503c\uff08\u4f5c\u4e3a\u6570\u91cf\u7ea7\u7684\u4e00\u4e2a\u8fd1\u4f3c\u6307\u6807\uff09\uff0c\u7136\u540e\u5c06\u5176\u4e58\u4ee5 relEpsilon \u3002\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c relEpsilon \u8868\u793a\u7684\u662f\u4e00\u4e2a\u767e\u5206\u6bd4\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u79f0\u201d\u8db3\u591f\u63a5\u8fd1\u201c\u610f\u5473\u7740 a \u548c b \u7684\u5dee\u5f02\u5728 1% \u4ee5\u5185\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u5c06 relEpsilon \u8bbe\u4e3a 0.01 (1% = 1/100 = 0.01)\uff0c relEpsilon \u7684\u503c\u53ef\u4ee5\u9009\u53d6\u6700\u9002\u5408\u6211\u4eec\u7684\u503c (\u4f8b\u5982 epsilon \u4e3a 0.002 \u8868\u793a\u4e24\u8005\u5dee\u5f02\u5728 0.2% \u4ee5\u5185)\u3002 \u5982\u679c\u8981\u5224\u65ad\u662f\u5426\u4e0d\u76f8\u7b49 ( != ) \uff0c\u4e5f\u53ef\u4ee5\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u7136\u540e\u5728\u524d\u9762\u6dfb\u52a0\u903b\u8f91\u975e ( ! ) \u8fd0\u7b97\u7b26\uff0c\u5bf9\u7ed3\u679c\u53d6\u53cd\uff1a 1 2 if ( ! approximatelyEqualRel ( a , b , 0.001 )) std :: cout << a << \" is not equal to \" << b << '\\n' ; \u6ce8\u610f\uff0c\u5c3d\u7ba1 approximatelyEqual() \u51fd\u6570\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u90fd\u53ef\u4ee5\u6b63\u786e\u5de5\u4f5c\uff0c\u5b83\u4ecd\u7136\u4e0d\u662f\u5b8c\u7f8e\u7684\uff0c\u5c24\u5176\u662f\u5f53\u4e24\u6570\u63a5\u8fd10\u65f6\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include #include // return true if the difference between a and b is within epsilon percent of the larger of a and b bool approximatelyEqualRel ( double a , double b , double relEpsilon ) { return ( std :: abs ( a - b ) <= ( std :: max ( std :: abs ( a ), std :: abs ( b )) * relEpsilon )); } int main () { // a \u975e\u5e38\u63a5\u8fd1 1.0\uff0c\u4f46\u662f\u7531\u4e8e\u820d\u5165\u8bef\u5dee\u7684\u5b58\u5728\uff0c\u5b83\u7a0d\u5fae\u5c0f\u4e8e 1.0 double a { 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 }; // \u9996\u5148, \u8ba9\u6211\u4eec\u628a a \u548c 0.1 \u76f8\u6bd4 std :: cout << approximatelyEqualRel ( a , 1.0 , 1e-8 ) << '\\n' ; // \u7136\u540e, \u8ba9\u6211\u4eec\u628a a - 0.1 (\u51e0\u4e4e\u4e3a 0.0) \u548c 0.0 \u76f8\u6bd4 std :: cout << approximatelyEqualRel ( a -1.0 , 0.0 , 1e-8 ) << '\\n' ; } \u51fa\u4eba\u610f\u6599\u7684\u662f\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 1 0 \u7b2c\u4e8c\u4e2a\u51fd\u6570\u8c03\u7528\u5e76\u6ca1\u6709\u7b26\u5408\u6211\u4eec\u7684\u9884\u671f\uff0c\u5f53\u6570\u636e\u63a5\u8fd1 0 \u65f6\u5c31\u4f1a\u51fa\u95ee\u9898\u3002 \u907f\u514d\u51fa\u73b0\u8be5\u95ee\u9898\u7684\u65b9\u5f0f\uff0c\u662f\u540c\u65f6\u4f7f\u7528\u4e00\u4e2a\u7edd\u5bf9 epsilon (\u50cf\u7b2c\u4e00\u4e2a\u4f8b\u5b50\u90a3\u6837) \u548c\u4e00\u4e2a\u76f8\u5bf9 epsilon (\u50cf Knuth \u7684\u4f8b\u5b50\u90a3\u6837)\uff1a 1 2 3 4 5 6 7 8 9 10 11 // return true if the difference between a and b is less than absEpsilon, or within relEpsilon percent of the larger of a and b bool approximatelyEqualAbsRel ( double a , double b , double absEpsilon , double relEpsilon ) { // Check if the numbers are really close -- needed when comparing numbers near zero. double diff { std :: abs ( a - b ) }; if ( diff <= absEpsilon ) return true ; // Otherwise fall back to Knuth's algorithm return ( diff <= ( std :: max ( std :: abs ( a ), std :: abs ( b )) * relEpsilon )); } \u5728\u8fd9\u4e2a\u7b97\u6cd5\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u68c0\u67e5 a \u548c b \u662f\u5426\u8db3\u591f\u63a5\u8fd1\uff0c\u7528\u4e8e\u5904\u7406 a \u548c b \u90fd\u975e\u5e38\u63a5\u8fd1 0 \u7684\u60c5\u51b5\u3002 absEpsilon \u53c2\u6570\u5e94\u8be5\u88ab\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u975e\u5e38\u5c0f\u7684\u6570\uff08\u4f8b\u5982 1e-12 \uff09\uff0c\u5982\u679c\u8fd9\u4e00\u6b65\u7684\u5224\u65ad\u6ca1\u6709\u6210\u529f\uff0c\u5219\u4f7f\u7528 Knuth \u7684\u7b97\u6cd5\uff0c\u4f7f\u7528\u76f8\u5bf9\u7684 epsilon\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u7528\u4e8e\u5bf9\u4e24\u79cd\u65b9\u6cd5\u8fdb\u884c\u6d4b\u8bd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include #include // return true if the difference between a and b is within epsilon percent of the larger of a and b bool approximatelyEqualRel ( double a , double b , double relEpsilon ) { return ( std :: abs ( a - b ) <= ( std :: max ( std :: abs ( a ), std :: abs ( b )) * relEpsilon )); } bool approximatelyEqualAbsRel ( double a , double b , double absEpsilon , double relEpsilon ) { // Check if the numbers are really close -- needed when comparing numbers near zero. double diff { std :: abs ( a - b ) }; if ( diff <= absEpsilon ) return true ; // Otherwise fall back to Knuth's algorithm return ( diff <= ( std :: max ( std :: abs ( a ), std :: abs ( b )) * relEpsilon )); } int main () { // a is really close to 1.0, but has rounding errors double a { 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 }; std :: cout << approximatelyEqualRel ( a , 1.0 , 1e-8 ) << '\\n' ; // compare \"almost 1.0\" to 1.0 std :: cout << approximatelyEqualRel ( a -1.0 , 0.0 , 1e-8 ) << '\\n' ; // compare \"almost 0.0\" to 0.0 std :: cout << approximatelyEqualAbsRel ( a , 1.0 , 1e-12 , 1e-8 ) << '\\n' ; // compare \"almost 1.0\" to 1.0 std :: cout << approximatelyEqualAbsRel ( a -1.0 , 0.0 , 1e-12 , 1e-8 ) << '\\n' ; // compare \"almost 0.0\" to 0.0 } 1 2 3 4 1 0 1 1 \u53ef\u4ee5\u770b\u5230 approximatelyEqualAbsRel() \u53ef\u4ee5\u6b63\u786e\u5730\u5904\u7406\u6570\u503c\u5f88\u5c0f\u7684\u60c5\u51b5\u3002 \u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\u662f\u4e00\u4e2a\u5f88\u590d\u6742\u7684\u95ee\u9898\uff0c\u800c\u4e14\u6ca1\u6709\u4e07\u80fd\u7684\u7b97\u6cd5\u53ef\u4ee5\u5e94\u5bf9\u6240\u6709\u7684\u60c5\u5f62\u3002\u4e0d\u8fc7\uff0c approximatelyEqualAbsRel() \u914d\u5408 1e-12 \u7684 absEpsilon \u4ee5\u53ca 1e-8 \u7684 relEpsilon \u5df2\u7ecf\u8db3\u591f\u5e94\u5bf9\u5927\u591a\u6570\u60c5\u51b5\u4e86\u3002","title":"5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83"},{"location":"5-6-Relational-operators-and-floating-point-comparisons/#_1","text":"\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c if \u8bed\u53e5\u6216 \u6761\u4ef6\u8fd0\u7b97\u7b26 \uff08\u4ee5\u53ca\u5176\u4ed6\u4e00\u4e9b\u7279\u6b8a\u573a\u666f\u4e0b\uff09\u4e2d\u7684\u6761\u4ef6\u6c42\u503c\u7ed3\u679c\u603b\u662f\u5e03\u5c14\u7c7b\u578b\u7684\u503c\u3002 \u5f88\u591a\u7a0b\u5e8f\u5458\u4f1a\u4f7f\u7528\u4e0b\u9762\u7684\u5199\u6cd5\uff1a 1 if ( b1 == true ) ... \u4f46\u662f\u8fd9\u79cd\u5199\u6cd5\u6709\u4e9b\u5197\u4f59\uff0c\u56e0\u4e3a == true \u5e76\u6ca1\u6709\u4e3a\u8fd9\u4e2a\u6761\u4ef6\u6dfb\u52a0\u4efb\u4f55\u503c\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u8fd9\u4e48\u5199\uff1a 1 if ( b1 ) ... \u540c\u6837\u7684\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 if ( b1 == false ) ... \u6700\u597d\u5199\u6210\u8fd9\u6837\uff1a 1 if ( ! b1 ) ... \u6700\u4f73\u5b9e\u8df5 \u4e0d\u8981\u4e3a\u6761\u4ef6\u6dfb\u52a0\u6ca1\u5fc5\u8981\u7684 == \u6216 != \uff0c\u5982\u679c\u6ca1\u6709\u5176\u4ed6\u7684\u503c\u5219\u4f1a\u8ba9\u53ef\u8bfb\u6027\u53d8\u5f97\u66f4\u5dee","title":"\u5e03\u5c14\u6761\u4ef6\u503c"},{"location":"5-6-Relational-operators-and-floating-point-comparisons/#_2","text":"\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { double d1 { 100.0 - 99.99 }; // \u5e94\u8be5\u7b49\u4e8e 0.01 double d2 { 10.0 - 9.99 }; // \u5e94\u8be5\u7b49\u4e8e 0.01 if ( d1 == d2 ) std :: cout << \"d1 == d2\" << '\\n' ; else if ( d1 > d2 ) std :: cout << \"d1 > d2\" << '\\n' ; else if ( d1 < d2 ) std :: cout << \"d1 < d2\" << '\\n' ; return 0 ; } \u53d8\u91cf d1 \u548c d2 \u5e94\u8be5\u90fd\u7b49\u4e8e 0.01 \u3002\u4f46\u662f\u4f60\u5bf9\u5b83\u4eec\u6bd4\u8f83\u7684\u8bdd\uff0c\u5c06\u4f1a\u4ea7\u751f\u4ee4\u4eba\u610f\u5916\u7684\u7ed3\u679c\uff1a 1 d1 > d2 \u5982\u679c\u4f60\u4f7f\u7528\u8c03\u8bd5\u5668\u6765\u67e5\u770b\u4e24\u4e2a\u53d8\u91cf\uff0c\u5219 d1 = 0.0100000000000005116 \u548c d2 = 0.0099999999999997868 \u3002\u8fd9\u4e24\u4e2a\u503c\u90fd\u63a5\u8fd1\u4e8e 0.01\uff0c\u4f46\u662f d1 \u6bd4 0.1 \u5927\uff0cd2 \u5219\u6bd4 0.1 \u5c0f\u3002 \u5982\u679c\u4f60\u9700\u8981\u5f88\u9ad8\u7684\u7cbe\u5ea6\uff0c\u5219\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528\u4e0a\u8ff0\u6bd4\u8f83\u8fd0\u7b97\u7b26\u662f\u5f88\u5371\u9669\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u6d6e\u70b9\u6570\u5e76\u4e0d\u7cbe\u786e\uff0c\u5f88\u5c0f\u7684 \u820d\u5165\u8bef\u5dee \u90fd\u6709\u53ef\u80fd\u9020\u6210\u4e0a\u8ff0\u610f\u5916\u60c5\u51b5\u3002 \u6211\u4eec\u5728 4.8 - \u6d6e\u70b9\u6570 \u4e2d\u4ecb\u7ecd\u4e86\u820d\u5165\u8bef\u5dee\u7684\u95ee\u9898\u3002 \u5f53\u5927\u4e8e\u5c0f\u4e8e\u53f7( < , <= , > \u548c >= ) \u6bcf\u7528\u5728\u6d6e\u70b9\u6570\u6bd4\u8f83\u65f6\uff0c\u901a\u5e38\u662f\u53ef\u4ee5\u5f97\u5230\u6b63\u786e\u7ed3\u679c\u7684\uff08\u9664\u975e\u4e24\u4e2a\u6570\u975e\u5e38\u63a5\u8fd1\uff09\u3002\u56e0\u6b64\uff0c\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528\u4f7f\u7528\u6b64\u7c7b\u6bd4\u8f83\u8fd0\u7b97\u7b26\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\uff0c\u53ea\u6709\u5f53\u4e24\u4e2a\u6570\u975e\u5e38\u63a5\u8fd1\u7684\u65f6\u5019\u624d\u6709\u53ef\u80fd\u5f97\u5230\u9519\u8bef\u7684\u7ed3\u679c\u3002 \u4f8b\u5982\uff0c\u8003\u8651\u6211\u4eec\u5728\u8bbe\u8ba1\u4e00\u4e2a\u6e38\u620f\uff08\u6bd4\u65b9\u8bf4\u300a\u592a\u7a7a\u4fb5\u7565\u8005\u300b\uff09\uff0c\u6b64\u65f6\u4f60\u9700\u8981\u5224\u65ad\u4e24\u4e2a\u7269\u4f53\u662f\u5426\u4f1a\u76f8\u4ea4\uff08\u6bd4\u5982\u8bf4\u5bfc\u5f39\u548c\u5916\u661f\u4eba\uff09\u3002\u5f53\u4e24\u4e2a\u7269\u4f53\u76f8\u8ddd\u751a\u8fdc\u65f6\uff0c\u8fd9\u4e9b\u6bd4\u8f83\u8fd0\u7b97\u7b26\u53ef\u4ee5\u8fd4\u56de\u6b63\u786e\u7684\u7ed3\u679c\u3002\u5f53\u4e24\u4e2a\u7269\u4f53\u5df2\u7ecf\u975e\u5e38\u63a5\u8fd1\u65f6\uff0c\u90a3\u4f60\u5176\u5b9e\u5df2\u7ecf\u5f97\u5230\u7ed3\u679c\u4e86\uff0c\u5373\u4f7f\u6bd4\u8f83\u7684\u7ed3\u8bba\u662f\u9519\u8bef\u7684\uff0c\u4f60\u53ef\u80fd\u4e5f\u4e0d\u4f1a\u6ce8\u610f\u5230\uff08\u770b\u4e0a\u53bb\u662f\u5728\u975e\u5e38\u8fd1\u7684\u4e3e\u4f8b\u547d\u4e2d\u6216\u4e22\u5931\u4e86\uff09\uff0c\u4e5f\u4e0d\u4f1a\u5bf9\u4f60\u7684\u6e38\u620f\u9020\u6210\u4e25\u91cd\u7684\u5f71\u54cd\u3002","title":"\u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\u53ef\u80fd\u4f1a\u5e26\u6765\u95ee\u9898"},{"location":"5-6-Relational-operators-and-floating-point-comparisons/#_3","text":"\u76f8\u7b49\u8fd0\u7b97\u7b26( == \u548c != ) \u7684\u9ebb\u70e6\u5c31\u6bd4\u8f83\u5927\u3002\u5bf9\u4e8e\u8fd0\u7b97\u7b26 operator== \uff0c\u5b83\u53ea\u6709\u5728\u4e24\u4e2a\u64cd\u4f5c\u6570\u5b8c\u5168\u76f8\u7b49\u65f6\u624d\u8fd4\u56de true \u3002\u56e0\u4e3a\u5f88\u5c0f\u7684\u820d\u5165\u8bef\u5dee\u5c31\u4f1a\u4f7f\u4e24\u4e2a\u6d6e\u70b9\u6570\u4e0d\u7b49\uff0c\u6240\u4ee5 operator== \u6709\u975e\u5e38\u5927\u7684\u53ef\u80fd\u5728\u4f60\u8ba4\u4e3a\u5e94\u8be5\u8fd4\u56de true \u7684\u65f6\u5019\u8fd4\u56de false \u3002 Operator!= \u4e5f\u6709\u7c7b\u4f3c\u7684\u95ee\u9898\u3002 \u56e0\u6b64\uff0c\u8bf7\u907f\u514d\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528\u8fd9\u4e24\u79cd\u8fd0\u7b97\u7b26\u3002 \u6ce8\u610f \u8bf7\u907f\u514d\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528 operator== \u548c operator!= \u3002","title":"\u6d6e\u70b9\u503c\u76f8\u7b49"},{"location":"5-6-Relational-operators-and-floating-point-comparisons/#_4","text":"\u90a3\u4e48\uff0c\u5bf9\u4e24\u4e2a\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\u7684\u5408\u7406\u65b9\u6cd5\u662f\u4ec0\u4e48\u5462\uff1f \u5224\u65ad\u6d6e\u70b9\u6570\u662f\u5426\u76f8\u7b49\u7684\u5e38\u7528\u65b9\u6cd5\uff0c\u662f\u4f7f\u7528\u4e00\u4e2a\u51fd\u6570\u6765\u5224\u65ad\u4e24\u79cd\u662f\u4e0d\u662f \u975e\u5e38\u63a5\u8fd1 \u3002\u53ea\u8981\u975e\u5e38\u63a5\u8fd1\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u79f0\u5176 \u201d\u76f8\u7b49\u201c \u3002\u8868\u793a\u201d\u975e\u5e38\u63a5\u8fd1\u201c\u7684\u4f20\u7edf\u65b9\u6cd5\u662f\u4f7f\u7528epsilon\u3002\u5b83\u901a\u5e38\u4f1a\u88ab\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u975e\u5e38\u5c0f\u7684\u6b63\u6570\uff08\u4f8b\u5982\uff0c 0.00000001 \u6709\u65f6\u5019\u5199\u4f5c 1e-8 \uff09\u3002 \u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u4f7f\u7528\u81ea\u5df1\u5b9a\u4e49\u7684\u201d\u975e\u5e38\u63a5\u8fd1\u201c\u51fd\u6570\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 #include // for std::abs() // epsilon is an absolute value bool approximatelyEqualAbs ( double a , double b , double absEpsilon ) { // if the distance between a and b is less than absEpsilon, then a and b are \"close enough\" return std :: abs ( a - b ) <= absEpsilon ; } std::abs() \u662f \u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u5b83\u8fd4\u56de\u7684\u662f\u5176 \u5b9e\u53c2 \u7684\u7edd\u5bf9\u503c\u3002 std::abs(a - b) <= absEpsilon \u4f1a\u68c0\u67e5 a \u548c b \u7684\u5dee\u7684\u7edd\u5bf9\u503c\u662f\u5426\u5c0f\u4e8e\u4f20\u5165\u7684\u4efb\u4f55\u8868\u793a\u5f88\u63a5\u8fd1\u503c\u7684 \"epsilon\"\u3002\u5982\u679c a \u548c b \u8db3\u591f\u63a5\u8fd1\uff0c\u5219\u51fd\u6570\u4f1a\u8fd4\u56de true\uff0c\u8868\u793a\u5b83\u4eec\u76f8\u7b49\uff0c\u5426\u5219\u5219\u8fd4\u56de false\u3002 \u5c3d\u7ba1\u8fd9\u4e2a\u51fd\u6570\u786e\u5b9e\u53ef\u7528\uff0c\u4f46\u5e76\u4e0d\u662f\u6700\u4f73\u7684\u89e3\u51b3\u65b9\u6848\u3002 0.00001 \u5bf9\u4e8e1.0\u5de6\u53f3\u7684\u8f93\u5165\u662f\u5408\u9002\u7684\uff0c\u4f46\u662f\u5bf9\u4e8e 0.0000001 \u5de6\u53f3\u7684\u6570\u5c31\u592a\u5927\u4e86\uff0c\u800c\u5bf9\u4e8e 10000 \u8fd9\u6837\u7684\u8f93\u5165\u53c8\u592a\u5c0f\u4e86\u3002 \u9898\u5916\u8bdd \u5982\u679c\u6211\u4eec\u8bf4\uff0c\u5f53\u4e24\u4e2a\u6570\u7684\u5dee\u8ddd\u5c0f\u4e8e 0.00001 \u65f6\uff0c\u5b83\u4eec\u5c31\u80fd\u88ab\u770b\u505a\u662f\u76f8\u540c\u7684\u6570\uff0c\u90a3\u4e48\uff1a 1 \u548c 1.0001 \u662f\u4e0d\u540c\u7684\uff0c\u4f46\u662f 1 \u548c 1.00001 \u5219\u662f\u76f8\u540c\u7684\uff0c\u8fd9\u5e76\u4e0d\u5408\u7406\uff1b 0.0000001 \u548c 0.00001 \u662f\u76f8\u540c\u7684\uff0c\u8fd9\u770b\u8d77\u6765\u5e76\u4e0d\u5408\u7406\uff0c\u6bd4\u8f83\u5b83\u4eec\u5dee\u4e86\u4e00\u4e2a\u6570\u91cf\u7ea7\uff1b 10000 \u548c 10000.00001 \u5219\u662f\u4e0d\u540c\u7684\uff0c\u8fd9\u770b\u8d77\u6765\u4e5f\u5e76\u4e0d\u5408\u7406\uff0c\u8003\u8651\u5230\u8fd9\u4e24\u4e2a\u6570\u7684\u6570\u91cf\u7ea7\uff0c\u6709\u8fd9\u4e48\u5c0f\u7684\u5dee\u5f02\u88ab\u770b\u505a\u662f\u4e0d\u540c\u7684\u4e24\u4e2a\u6570\u663e\u7136\u4e0d\u5408\u7406\u3002 \u8fd9\u4e5f\u610f\u5473\u7740\uff0c\u6bcf\u6b21\u8c03\u7528\u8be5\u51fd\u6570\u7684\u65f6\u5019\uff0c\u6211\u4eec\u90fd\u5fc5\u987b\u4e3a\u8f93\u5165\u7684\u503c\u6311\u9009\u4e00\u4e2a\u5408\u9002\u7684 epsilon \u503c\u3002\u65e2\u7136\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u4e86 epsilon \u7684\u503c\u9700\u8981\u6839\u636e\u8f93\u5165\u6570\u636e\u7684\u6570\u91cf\u7ea7\u53d8\u5316\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u5e94\u8be5\u901a\u8fc7\u4fee\u6539\u51fd\u6570\u6765\u4f7f\u5176\u81ea\u52a8\u4e3a\u6211\u4eec\u5b8c\u6210\u8fd9\u4e2a\u5de5\u4f5c\u3002 Donald Knuth \u662f\u4e00\u4e2a\u8457\u540d\u7684\u8ba1\u7b97\u673a\u79d1\u5b66\u5bb6\uff0c\u4ed6\u5728\u4ed6\u7684\u8457\u4f5c \u201c\u8ba1\u7b97\u673a\u7a0b\u5e8f\u8bbe\u8ba1\u827a\u672f\u7b2c\u4e8c\u5377\uff1a\u534a\u6570\u503c\u7b97\u6cd5(Addison-Wesley, 1969)\u201c \u4e2d\u4ecb\u7ecd\u4e86\u8fd9\u6837\u7684\u65b9\u6cd5\uff1a 1 2 3 4 5 6 7 8 #include // std::max #include // std::abs // return true if the difference between a and b is within epsilon percent of the larger of a and b bool approximatelyEqualRel ( double a , double b , double relEpsilon ) { return ( std :: abs ( a - b ) <= ( std :: max ( std :: abs ( a ), std :: abs ( b )) * relEpsilon )); } \u548c\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e00\u4e2a\u7edd\u5bf9\u7684 epsilon \u503c\u76f8\u6bd4\uff0c\u8fd9\u91cc\u7684 epsilon \u662f\u4e00\u4e2a\u4e0e a \u6216 b \u6570\u91cf\u7ea7\u6709\u5173\u7684\u76f8\u5bf9\u503c\u3002 \u8ba9\u6211\u4eec\u4ed4\u7ec6\u5206\u6790\u4e00\u4e0b\u8fd9\u4e2a\u770b\u4e0a\u53bb\u975e\u5e38\u590d\u6742\u7684\u51fd\u6570\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u5728\u4e0d\u7b49\u53f7 <= operator \u7684\u5de6\u8fb9 std::abs(a - b) \u4e3a\u6211\u4eec\u6c42\u51fa\u4e86 a \u548c b \u4e4b\u95f4\u7684\u5dee\u5f02\uff08\u4ee5\u4e00\u4e2a\u6b63\u6570\u6765\u8868\u793a\uff09\u3002 \u5728\u4e0d\u7b49\u53f7 <= operator \u7684\u53f3\u8fb9\uff0c\u6211\u4eec\u9700\u8981\u8ba1\u7b97\u90a3\u4e2a\u6240\u8c13\u201d\u8db3\u591f\u63a5\u8fd1\u201c\u7684\u503c\u7684\u6700\u5927\u503c\u3002\u4e3a\u6b64\uff0c\u8fd9\u4e2a\u7b97\u6cd5\u9009\u53d6\u4e86 a \u548c b \u4e2d\u8f83\u5927\u7684\u4e00\u4e2a\u503c\uff08\u4f5c\u4e3a\u6570\u91cf\u7ea7\u7684\u4e00\u4e2a\u8fd1\u4f3c\u6307\u6807\uff09\uff0c\u7136\u540e\u5c06\u5176\u4e58\u4ee5 relEpsilon \u3002\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c relEpsilon \u8868\u793a\u7684\u662f\u4e00\u4e2a\u767e\u5206\u6bd4\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u79f0\u201d\u8db3\u591f\u63a5\u8fd1\u201c\u610f\u5473\u7740 a \u548c b \u7684\u5dee\u5f02\u5728 1% \u4ee5\u5185\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u5c06 relEpsilon \u8bbe\u4e3a 0.01 (1% = 1/100 = 0.01)\uff0c relEpsilon \u7684\u503c\u53ef\u4ee5\u9009\u53d6\u6700\u9002\u5408\u6211\u4eec\u7684\u503c (\u4f8b\u5982 epsilon \u4e3a 0.002 \u8868\u793a\u4e24\u8005\u5dee\u5f02\u5728 0.2% \u4ee5\u5185)\u3002 \u5982\u679c\u8981\u5224\u65ad\u662f\u5426\u4e0d\u76f8\u7b49 ( != ) \uff0c\u4e5f\u53ef\u4ee5\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u7136\u540e\u5728\u524d\u9762\u6dfb\u52a0\u903b\u8f91\u975e ( ! ) \u8fd0\u7b97\u7b26\uff0c\u5bf9\u7ed3\u679c\u53d6\u53cd\uff1a 1 2 if ( ! approximatelyEqualRel ( a , b , 0.001 )) std :: cout << a << \" is not equal to \" << b << '\\n' ; \u6ce8\u610f\uff0c\u5c3d\u7ba1 approximatelyEqual() \u51fd\u6570\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u90fd\u53ef\u4ee5\u6b63\u786e\u5de5\u4f5c\uff0c\u5b83\u4ecd\u7136\u4e0d\u662f\u5b8c\u7f8e\u7684\uff0c\u5c24\u5176\u662f\u5f53\u4e24\u6570\u63a5\u8fd10\u65f6\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include #include // return true if the difference between a and b is within epsilon percent of the larger of a and b bool approximatelyEqualRel ( double a , double b , double relEpsilon ) { return ( std :: abs ( a - b ) <= ( std :: max ( std :: abs ( a ), std :: abs ( b )) * relEpsilon )); } int main () { // a \u975e\u5e38\u63a5\u8fd1 1.0\uff0c\u4f46\u662f\u7531\u4e8e\u820d\u5165\u8bef\u5dee\u7684\u5b58\u5728\uff0c\u5b83\u7a0d\u5fae\u5c0f\u4e8e 1.0 double a { 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 }; // \u9996\u5148, \u8ba9\u6211\u4eec\u628a a \u548c 0.1 \u76f8\u6bd4 std :: cout << approximatelyEqualRel ( a , 1.0 , 1e-8 ) << '\\n' ; // \u7136\u540e, \u8ba9\u6211\u4eec\u628a a - 0.1 (\u51e0\u4e4e\u4e3a 0.0) \u548c 0.0 \u76f8\u6bd4 std :: cout << approximatelyEqualRel ( a -1.0 , 0.0 , 1e-8 ) << '\\n' ; } \u51fa\u4eba\u610f\u6599\u7684\u662f\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 1 0 \u7b2c\u4e8c\u4e2a\u51fd\u6570\u8c03\u7528\u5e76\u6ca1\u6709\u7b26\u5408\u6211\u4eec\u7684\u9884\u671f\uff0c\u5f53\u6570\u636e\u63a5\u8fd1 0 \u65f6\u5c31\u4f1a\u51fa\u95ee\u9898\u3002 \u907f\u514d\u51fa\u73b0\u8be5\u95ee\u9898\u7684\u65b9\u5f0f\uff0c\u662f\u540c\u65f6\u4f7f\u7528\u4e00\u4e2a\u7edd\u5bf9 epsilon (\u50cf\u7b2c\u4e00\u4e2a\u4f8b\u5b50\u90a3\u6837) \u548c\u4e00\u4e2a\u76f8\u5bf9 epsilon (\u50cf Knuth \u7684\u4f8b\u5b50\u90a3\u6837)\uff1a 1 2 3 4 5 6 7 8 9 10 11 // return true if the difference between a and b is less than absEpsilon, or within relEpsilon percent of the larger of a and b bool approximatelyEqualAbsRel ( double a , double b , double absEpsilon , double relEpsilon ) { // Check if the numbers are really close -- needed when comparing numbers near zero. double diff { std :: abs ( a - b ) }; if ( diff <= absEpsilon ) return true ; // Otherwise fall back to Knuth's algorithm return ( diff <= ( std :: max ( std :: abs ( a ), std :: abs ( b )) * relEpsilon )); } \u5728\u8fd9\u4e2a\u7b97\u6cd5\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u68c0\u67e5 a \u548c b \u662f\u5426\u8db3\u591f\u63a5\u8fd1\uff0c\u7528\u4e8e\u5904\u7406 a \u548c b \u90fd\u975e\u5e38\u63a5\u8fd1 0 \u7684\u60c5\u51b5\u3002 absEpsilon \u53c2\u6570\u5e94\u8be5\u88ab\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u975e\u5e38\u5c0f\u7684\u6570\uff08\u4f8b\u5982 1e-12 \uff09\uff0c\u5982\u679c\u8fd9\u4e00\u6b65\u7684\u5224\u65ad\u6ca1\u6709\u6210\u529f\uff0c\u5219\u4f7f\u7528 Knuth \u7684\u7b97\u6cd5\uff0c\u4f7f\u7528\u76f8\u5bf9\u7684 epsilon\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u7528\u4e8e\u5bf9\u4e24\u79cd\u65b9\u6cd5\u8fdb\u884c\u6d4b\u8bd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include #include // return true if the difference between a and b is within epsilon percent of the larger of a and b bool approximatelyEqualRel ( double a , double b , double relEpsilon ) { return ( std :: abs ( a - b ) <= ( std :: max ( std :: abs ( a ), std :: abs ( b )) * relEpsilon )); } bool approximatelyEqualAbsRel ( double a , double b , double absEpsilon , double relEpsilon ) { // Check if the numbers are really close -- needed when comparing numbers near zero. double diff { std :: abs ( a - b ) }; if ( diff <= absEpsilon ) return true ; // Otherwise fall back to Knuth's algorithm return ( diff <= ( std :: max ( std :: abs ( a ), std :: abs ( b )) * relEpsilon )); } int main () { // a is really close to 1.0, but has rounding errors double a { 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 }; std :: cout << approximatelyEqualRel ( a , 1.0 , 1e-8 ) << '\\n' ; // compare \"almost 1.0\" to 1.0 std :: cout << approximatelyEqualRel ( a -1.0 , 0.0 , 1e-8 ) << '\\n' ; // compare \"almost 0.0\" to 0.0 std :: cout << approximatelyEqualAbsRel ( a , 1.0 , 1e-12 , 1e-8 ) << '\\n' ; // compare \"almost 1.0\" to 1.0 std :: cout << approximatelyEqualAbsRel ( a -1.0 , 0.0 , 1e-12 , 1e-8 ) << '\\n' ; // compare \"almost 0.0\" to 0.0 } 1 2 3 4 1 0 1 1 \u53ef\u4ee5\u770b\u5230 approximatelyEqualAbsRel() \u53ef\u4ee5\u6b63\u786e\u5730\u5904\u7406\u6570\u503c\u5f88\u5c0f\u7684\u60c5\u51b5\u3002 \u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\u662f\u4e00\u4e2a\u5f88\u590d\u6742\u7684\u95ee\u9898\uff0c\u800c\u4e14\u6ca1\u6709\u4e07\u80fd\u7684\u7b97\u6cd5\u53ef\u4ee5\u5e94\u5bf9\u6240\u6709\u7684\u60c5\u5f62\u3002\u4e0d\u8fc7\uff0c approximatelyEqualAbsRel() \u914d\u5408 1e-12 \u7684 absEpsilon \u4ee5\u53ca 1e-8 \u7684 relEpsilon \u5df2\u7ecf\u8db3\u591f\u5e94\u5bf9\u5927\u591a\u6570\u60c5\u51b5\u4e86\u3002","title":"\u6bd4\u8f83\u6d6e\u70b9\u6570 \uff08\u6269\u5c55\u9605\u8bfb\uff09"},{"location":"5-7-Logical-operators/","tags":["operator"],"text":"Key Takeaway \u903b\u8f91\u975e\u7684\u4f18\u5148\u7ea7\u975e\u5e38\u9ad8\uff0c\u5982\u679c\u4f7f\u7528\u5b83\u7684\u76ee\u7684\u662f\u5bf9\u6574\u4e2a\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u53d6\u53cd\uff0c\u5219\u8981\u5bf9\u6574\u4e2a\u8868\u8fbe\u5f0f\u52a0\u62ec\u53f7 \u533a\u5206 \u903b\u8f91\u4e0e( && ) \u548c \u6309\u4f4d\u4e0e( & ) \uff0c \u903b\u8f91\u975e( || ) \u548c \u6309\u4f4d\u975e( | ) \u77ed\u8def\u6c42\u503c\u53ef\u80fd\u4f1a\u5bfc\u81f4 \u903b\u8f91\u4e0e \u6216 \u903b\u8f91\u975e \u4e0d\u5bf9\u7b2c\u4e8c\u4e2a\u8868\u8fbe\u5f0f\u6c42\u503c\u3002\u6240\u4ee5\u8bf7\u907f\u514d\u5c06\u5177\u6709\u526f\u4f5c\u7528\u7684\u8fd0\u7b97\u7b26\u548c\u903b\u8f91\u4e0e\u6216\u903b\u8f91\u975e\u4e00\u8d77\u4f7f\u7528\u3002 \u903b\u8f91\u6216\u548c\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u7684\u4e24\u4e2a\u64cd\u4f5c\u6570\uff0c\u6c42\u503c\u987a\u5e8f\u662f\u56fa\u5b9a\u7684\uff0c\u56e0\u4e3a\u6807\u51c6\u91cc\u5bf9\u5176\u8fdb\u884c\u4e86\u8bf4\u660e\uff0c\u5de6\u8fb9\u7684\u64cd\u4f5c\u6570\u603b\u662f\u5148\u6c42\u503c\u3002 \u53ea\u6709 C++ \u81ea\u5e26\u7684\u8fd9\u4e9b\u903b\u8f91\u8fd0\u7b97\u7b26\u4f1a\u6267\u884c\u77ed\u8def\u6c42\u503c\uff0c\u5982\u679c\u4f60\u81ea\u5df1\u91cd\u8f7d\u4e86\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u4ee5\u4fbf\u5c06\u5176\u5e94\u7528\u4e8e\u4f60\u81ea\u5df1\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u8fd9\u4e9b\u91cd\u8f7d\u540e\u7684\u8fd0\u7b97\u7b26\u662f\u4e0d\u5177\u5907\u77ed\u8def\u6c42\u503c\u529f\u80fd\u7684\u3002 \u903b\u8f91\u4e0e\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e\u903b\u8f91\u6216\u7684\u4f18\u5148\u7ea7\uff0c\u5982\u679c\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u91cc\u540c\u65f6\u4f7f\u7528\u903b\u8f91\u4e0e\u548c\u903b\u8f91\u6216\uff0c\u6700\u597d\u4f7f\u7528\u62ec\u53f7\u4e3a\u5176\u660e\u786e\u6307\u5b9a\u4f18\u5148\u7ea7\u3002 \u5fb7\u6469\u6839\u5b9a\u5f8b !(x && y) \u7b49\u4ef7\u4e8e !x || !y !(x || y) \u7b49\u4ef7\u4e8e !x && !y C++ \u6ca1\u6709\u5f02\u6216\u8fd0\u7b97\u7b26\u3002\u53ef\u4ee5\u4f7f\u7528\u4e0d\u7b49\u53f7( != )\u6765\u6a21\u62df\u903b\u8f91\u5f02\u6216\uff08\u76f8\u540c\u4e3a\u771f\uff09 C++ \u4e2d\u7684\u5f88\u591a\u64cd\u4f5c\u7b26 (\u4f8b\u5982 operator || ) \u90fd\u53ef\u4ee5\u4f7f\u7528\u540d\u5b57\u6765\u4ee3\u66ff\u7b26\u53f7\u5417\uff0c\u56e0\u4e3a\u5e76\u4e0d\u662f\u6240\u6709\u7684\u952e\u76d8\u90fd\u652f\u6301\u8f93\u5165\u7b26\u53f7\u3002 \u5c3d\u7ba1\u6761\u4ef6\uff08\u6bd4\u8f83\uff09\u8fd0\u7b97\u7b26\u53ef\u4ee5\u88ab\u7528\u6765\u6d4b\u8bd5\u4e00\u4e2a\u7279\u5b9a\u7684\u8868\u8fbe\u5f0f\u662f\u5426\u4e3a\u771f\uff0c\u4f46\u5b83\u4eec\u4e00\u6b21\u53ea\u80fd\u5bf9\u4e00\u4e2a\u6761\u4ef6\u8fdb\u884c\u6d4b\u8bd5\u3002\u5f88\u591a\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u77e5\u9053\u591a\u4e2a\u6761\u4ef6\u662f\u5426\u540c\u65f6\u4e3a\u771f\u3002\u4f8b\u5982\uff0c\u4e3a\u4e86\u786e\u5b9a\u5f69\u7968\u662f\u5426\u4e2d\u5956\uff0c\u6211\u4eec\u5fc5\u987b\u5c06\u8d2d\u4e70\u7684\u6bcf\u4e00\u4e2a\u6570\u5b57\u548c\u4e2d\u5956\u53f7\u7801\u7684\u5404\u4e2a\u6570\u5b57\u9010\u4e00\u6bd4\u8f83\u3002\u5bf9\u4e8e\u67096\u4e2a\u6570\u7684\u5f69\u7968\uff0c\u8fd9\u5c31\u9700\u89816\u6b21\u6bd4\u8f83\uff0c\u53ea\u6709\u5f53\u5b83\u4eec\u7684\u7ed3\u679c\u5168\u90e8\u90fd\u662f\u771f\u7684\u65f6\u5019\uff0c\u624d\u80fd\u770b\u505a\u4e2d\u5956\u3002\u8fd8\u6709\u4e00\u4e9b\u60c5\u51b5\uff0c\u6211\u4eec\u9700\u8981\u77e5\u9053\u591a\u4e2a\u6761\u4ef6\u4e2d\u662f\u5426\u6709\u4e00\u4e2a\u4e3a\u771f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eca\u5929\u751f\u75c5\u4e86\uff0c\u6216\u8005\u592a\u7d2f\u4e86\uff0c\u53c8\u6216\u8005\u4e2d\u4e86\u5f69\u7968\uff0c\u8fd9\u4e09\u4e2a\u6761\u4ef6\u53ea\u8981\u6709\u4e00\u6761\u4e3a\u771f\u6211\u5c31\u4f1a\u8003\u8651\u7fd8\u73ed\u3002 \u903b\u8f91\u8fd0\u7b97\u7b26\u5c31\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u8fd9\u6837\u7684\u80fd\u529b\u3002 C++ \u63d0\u4f9b\u4e86\u4e09\u79cd\u903b\u8f91\u8fd0\u7b97\u7b26\uff1a \u8fd0\u7b97\u7b26 \u7b26\u53f7 \u5f62\u5f0f \u64cd\u4f5c \u903b\u8f91\u975e NOT ! !x \u5982\u679c x \u4e3a\u771f\u5219\u4e3a false\uff0c\u5982\u679c x \u4e3a\u5047\u5219\u4e3a true \u903b\u8f91\u4e0e AND && x && y \u5982\u679c x \u548c y \u90fd\u662f\u771f\uff0c\u5219\u4e3a true\uff0c\u5426\u5219\u4e3a false \u903b\u8f91\u6216 OR || x || y \u5982\u679c x \u6216 y \u5176\u4e2d\u4e00\u4e2a\u4f4d\u771f\uff0c\u5219\u4e3a\u771f\uff0c\u5426\u5219\u4e3a\u5047 \u903b\u8f91\u975e\uff08NOT\uff09 \u5728 4.9 - \u5e03\u5c14\u503c \u4e00\u8282\u8bfe\u4e2d\u4f60\u5df2\u7ecf\u9047\u5230\u903b\u8f91\u975e\u8fd9\u4e2a\u4e00\u5143\u8fd0\u7b97\u7b26\uff1a \u64cd\u4f5c\u7b26 \u7ed3\u679c true false false true \u5982\u679c\u903b\u8f91\u975e\u7684\u64cd\u4f5c\u6570\u6c42\u503c\u4e3a true \uff0c\u5219\u903b\u8f91\u975e\u6c42\u503c\u4e3a false \u3002\u5982\u679c\u903b\u8f91\u975e\u7684\u64cd\u4f5c\u6570\u6c42\u503c\u4e3a false \uff0c\u903b\u8f91\u975e\u7684\u6c42\u503c\u4e3a true \u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u903b\u8f91\u975e\u53ef\u4ee5\u5c06\u5e03\u5c14\u503c\u53cd\u8f6c\uff0c\u771f\u53d8\u6210\u5047\uff0c\u5047\u53d8\u6210\u771f\u3002 \u903b\u8f91\u975e\u7ecf\u5e38\u88ab\u7528\u5728\u6761\u4ef6\u8868\u8fbe\u5f0f\u4e2d\uff1a 1 2 3 4 5 bool tooLarge { x > 100 }; // tooLarge is true if x > 100 if ( ! tooLarge ) // do something with x else // print an error \u6709\u4ef6\u4e8b\u60c5\u9700\u8981\u6ce8\u610f\uff0c\u90a3\u5c31\u903b\u8f91\u975e\u7684\u4f18\u5148\u7ea7\u975e\u5e38\u9ad8\u3002\u65b0\u624b\u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u72af\u8fd9\u6837\u7684\u9519\u8bef\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 5 }; int y { 7 }; if ( ! x > y ) std :: cout << x << \" is not greater than \" << y << '\\n' ; else std :: cout << x << \" is greater than \" << y << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 5 is greater than 7 \u4f46\u662f x \u5e76\u4e0d\u6bd4 7 \u5927\uff0c\u8fd9\u7ed3\u679c\u662f\u600e\u4e48\u5f97\u5230\u7684\uff1f\u8fd9\u662f\u56e0\u4e3a\u903b\u8f91\u975e\u7684\u4f18\u5148\u7ea7\u6bd4\u5927\u4e8e\u53f7\u7684\u4f18\u5148\u7ea7\u9ad8\uff0c\u56e0\u6b64 ! x > y \u5b9e\u9645\u4e0a\u76f8\u5f53\u4e8e (!x) > y \u3002\u56e0\u4e3a x \u662f 5\uff0c !x \u6c42\u503c\u7ed3\u679c\u4e3a 0 \uff0c\u800c 0 > y \u662f\u5047\uff0c\u6240\u4ee5\u6267\u884c\u7684\u662f else \u7684\u8bed\u53e5\u3002 \u6b63\u786e\u7684\u505a\u6cd5\u5e94\u8be5\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 5 }; int y { 7 }; if ( ! ( x > y )) std :: cout << x << \" is not greater than \" << y << '\\n' ; else std :: cout << x << \" is greater than \" << y << '\\n' ; return 0 ; } \u8fd9\u6837 x > y \u4f1a\u9996\u5148\u8fdb\u884c\u6c42\u503c\uff0c\u7136\u540e\u518d\u901a\u8fc7\u903b\u8f91\u975e\u5c06\u5e03\u5c14\u7ed3\u679c\u53cd\u8f6c\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u903b\u8f91\u975e\u8981\u5bf9\u5176\u4ed6\u8fd0\u7b97\u7b26\u7684\u7ed3\u679c\u8fdb\u884c\u64cd\u4f5c\uff0c\u90a3\u4e48\u5176\u4ed6\u8fd0\u7b97\u7b26\u548c\u5b83\u4eec\u7684\u64cd\u4f5c\u6570\u5e94\u8be5\u653e\u5728\u62ec\u53f7\u4e2d\u3002 \u903b\u8f91\u975e\u7684\u7b80\u5355\u7528\u6cd5\uff0c\u4f8b\u5982 if (!value) \u5e76\u4e0d\u9700\u8981\u62ec\u53f7\uff0c\u56e0\u4e3a\u8fd9\u65f6\u4e0d\u4f1a\u53d7\u5230\u4f18\u5148\u7ea7\u7684\u5f71\u54cd\u3002 \u903b\u8f91\u6216\uff08OR\uff09 \u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u88ab\u7528\u6765\u6d4b\u8bd5\u4e24\u4e2a\u6761\u4ef6\u4e2d\u662f\u5426\u6709\u4e3a\u771f\u7684\u3002\u5982\u679c\u5de6\u64cd\u4f5c\u6570\u6216\u53f3\u64cd\u4f5c\u6570\u4e3a true \uff0c\u6216\u8005\u4e24\u4e2a\u90fd\u662f true \uff0c\u5219\u903b\u8f91\u6216\u8868\u8fbe\u5f0f\u4f1a\u8fd4\u56de true \uff0c\u5426\u5219\u8fd4\u56de false \u3002 \u5de6\u64cd\u4f5c\u6570 \u53f3\u64cd\u4f5c\u6570 \u7ed3\u679c false false false false true true true false true true true true \u4f8b\u5982\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { std :: cout << \"Enter a number: \" ; int value {}; std :: cin >> value ; if ( value == 0 || value == 1 ) std :: cout << \"You picked 0 or 1 \\n \" ; else std :: cout << \"You did not pick 0 or 1 \\n \" ; return 0 ; } \u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f7f\u7528\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u6765\u5224\u65ad\u505a\u64cd\u4f5c\u6570 ( value == 0 ) \u6216\u8005\u53f3\u64cd\u4f5c\u6570 ( value == 1 ) \u662f\u5426\u4e3a\u771f\u3002\u5982\u679c\u5176\u4e00\uff08\u6216\u4e24\u4e2a\uff09\u5168\u90e8\u4e3a\u771f\uff0c\u5219\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u7684\u6c42\u503c\u7ed3\u679c\u4e3a\u771f\uff0cif \u8bed\u53e5\u5c31\u4f1a\u88ab\u6267\u884c\u3002\u5982\u679c\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u662f\u5047\uff0c\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u7684\u7ed3\u679c\u5c31\u662f\u5047\uff0c\u90a3\u4e48 else \u8bed\u53e5\u5c31\u4f1a\u6267\u884c\u3002 \u4f60\u53ef\u4ee5\u5c06\u591a\u4e2a\u903b\u8f91\u6216\u8868\u8fbe\u5f0f\u8fde\u63a5\u8d77\u6765\uff1a 1 2 if ( value == 0 || value == 1 || value == 2 || value == 3 ) std :: cout << \"You picked 0, 1, 2, or 3 \\n \" ; \u65b0\u624b\u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u628a\u903b\u8f91\u6216\u8fd0\u7b97\u7b26 ( || ) \u548c\u6309\u4f4d\u6216\u8fd0\u7b97\u7b26\u641e\u6df7 ( | ) (\u7a0d\u540e\u4f1a\u4ecb\u7ecd)\u3002 \u5373\u4f7f\u5b83\u4eec\u7684\u540d\u5b57\u4e2d\u90fd\u6709\u4e00\u4e2a\u6216\uff0c\u5176\u529f\u80fd\u786e\u662f\u4e0d\u540c\u7684\u3002\u5c06\u5b83\u4eec\u6df7\u4e3a\u4e00\u8c08\u4f1a\u5e26\u6765\u9519\u8bef\u7684\u7ed3\u679c\u3002 \u903b\u8f91\u4e0e\uff08AND) \u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u88ab\u7528\u4f5c\u6d4b\u8bd5\u4e24\u4e2a\u64cd\u4f5c\u6570\u662f\u5426\u5747\u4e3a\u771f\u3002\u5982\u679c\u5747\u4e3a\u771f\uff0c\u5219\u903b\u8f91\u4e0e\u4f1a\u8fd4\u56de true \uff0c\u5426\u5219\u8fd4\u56de\u503c\u4e3a false \u3002 \u5de6\u64cd\u4f5c\u6570 \u53f3\u64cd\u4f5c\u6570 \u7ed3\u679c false false false false true false true false false true true true \u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u8981\u5224\u65ad x \u662f\u5426\u5728 10 \u5230 20 \u4e4b\u95f4\uff1a\u6211\u4eec\u5fc5\u987b\u77e5\u9053 x \u662f\u5426\u5927\u4e8e 10 \uff0c \u5e76\u4e14 \u662f\u5426\u5c0f\u4e8e 20\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { std :: cout << \"Enter a number: \" ; int value {}; std :: cin >> value ; if ( value > 10 && value < 20 ) std :: cout << \"Your value is between 10 and 20 \\n \" ; else std :: cout << \"Your value is not between 10 and 20 \\n \" ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u68c0\u6d4b\u5de6\u4fa7\u7684\u6761\u4ef6 (value > 10) \u548c \u53f3\u4fa7\u7684\u6761\u4ef6 (value < 20) \u662f\u5426\u90fd\u662f true \u3002\u5982\u679c\u90fd\u662f\u771f\uff0c\u5219\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u7684\u6c42\u503c\u7ed3\u679c\u4e3a true \uff0c\u6267\u884c if \u8bed\u53e5\u3002\u5982\u679c\u5de6\u53f3\u4e24\u4e2a\u6761\u4ef6\u90fd\u662f\u5047\uff0c\u5219\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u7684\u6c42\u503c\u7ed3\u679c\u4e3a false \uff0c\u6267\u884c else \u8bed\u53e5\u3002 \u548c\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u4e00\u6837\uff0c\u4f60\u53ef\u4ee5\u628a\u591a\u4e2a\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u8fde\u63a5\u8d77\u6765\uff1a 1 2 3 4 if ( value > 10 && value < 20 && value != 16 ) // do something else // do something else \u5982\u679c\u4e0a\u8ff0\u6240\u6709\u6761\u4ef6\u90fd\u662f\u771f\uff0c\u5219\u6267\u884c if \u8bed\u53e5\u3002\u5982\u679c\u4efb\u4f55\u4e00\u4e2a\u6761\u4ef6\u4e3a\u5047\uff0c\u5219\u6267\u884c else \u8bed\u53e5\u3002 \u65b0\u624b\u7a0b\u5e8f\u5458\u4e0d\u4ec5\u5bb9\u6613\u628a \u903b\u8f91\u6216 \u548c \u6309\u4f4d\u6216 \u641e\u6df7\uff0c\u4e5f\u4f1a\u628a\u903b\u8f91\u4e0e ( && ) \u548c\u6309\u4f4d\u4e0e ( & )\u641e\u6df7\u3002 \u77ed\u8def\u6c42\u503c \u4e3a\u4e86\u8ba9\u903b\u8f91\u4e0e\u8fd4\u56de true \uff0c\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u9700\u8981\u6c42\u503c\u4e3a\u771f\u3002\u5982\u679c\u7b2c\u4e00\u4e2a\u64cd\u4f5c\u6570\u6c42\u503c\u4e3a\u5047\uff0c\u4e0d\u8bba\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u6570\u6c42\u503c\u7ed3\u679c\u5982\u4f55\uff0c\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u4e00\u5b9a\u6c42\u503c\u4e3a false \u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u4f1a\u7acb\u5373\u8fd4\u56de false \uff0c\u800c\u4e0d\u4f1a\u518d\u5bf9\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u6570\u6c42\u503c\u3002\u8fd9\u4e2a\u65b9\u6cd5\u79f0\u4e3a\u77ed\u8def\u6c42\u503c\uff0c\u8fd9\u4e48\u505a\u53ef\u4ee5\u4f18\u5316\u7a0b\u5e8f\u6027\u80fd\u3002 \u7c7b\u4f3c\u5730\uff0c\u5982\u679c\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u7684\u7b2c\u4e00\u4e2a\u64cd\u4f5c\u6570\u6c42\u503c\u7ed3\u679c\u4e3a\u771f\uff0c\u5219\u5b83\u4f1a\u76f4\u63a5\u8fd4\u56de true \uff0c\u800c\u4e0d\u5fc5\u5bf9\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u6570\u8fdb\u884c\u6c42\u503c\u3002 \u77ed\u8def\u6c42\u503c\u8fd8\u5411\u6211\u4eec\u5c55\u793a\u4e86\uff0c\u4e3a\u4ec0\u4e48\u6709\u526f\u4f5c\u7528\u7684\u8fd0\u7b97\u7b26\uff0c\u4e0d\u5e94\u8be5\u653e\u5230\u590d\u5408\u8868\u8fbe\u5f0f\u4e2d\u3002\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 if ( x == 1 && ++ y == 2 ) // do something \u5982\u679c x \u4e0d\u7b49\u4e8e 1\uff0c\u90a3\u4e48\u6574\u4e2a\u6761\u4ef6\u4e00\u5b9a\u4f1a\u8fd4\u56de false \u3002\u6240\u4ee5 ++y \u6839\u672c\u4e0d\u4f1a\u88ab\u6c42\u503c\u3002\u56e0\u6b64\uff0c\u53ea\u6709\u5f53 x \u7b49\u4e8e 1 \u65f6\uff0c y \u624d\u4f1a\u9012\u589e\uff0c\u4e5f\u8bb8\u8fd9\u5e76\u4e0d\u662f\u4f60\u9700\u8981\u7684\uff01 \u6ce8\u610f \u77ed\u8def\u6c42\u503c\u53ef\u80fd\u4f1a\u5bfc\u81f4 \u903b\u8f91\u4e0e \u6216 \u903b\u8f91\u975e \u4e0d\u5bf9\u7b2c\u4e8c\u4e2a\u8868\u8fbe\u5f0f\u6c42\u503c\u3002\u6240\u4ee5\u8bf7\u907f\u514d\u5c06\u5177\u6709\u526f\u4f5c\u7528\u7684\u8fd0\u7b97\u7b26\u548c\u903b\u8f91\u4e0e\u6216\u903b\u8f91\u975e\u4e00\u8d77\u4f7f\u7528\u3002 \u5173\u952e\u4fe1\u606f \u903b\u8f91\u6216\u548c\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u7684\u4e24\u4e2a\u64cd\u4f5c\u6570\uff0c\u6c42\u503c\u987a\u5e8f\u662f\u56fa\u5b9a\u7684\uff0c\u56e0\u4e3a\u6807\u51c6\u91cc\u5bf9\u5176\u8fdb\u884c\u4e86\u8bf4\u660e\uff0c\u5de6\u8fb9\u7684\u64cd\u4f5c\u6570\u603b\u662f\u5148\u6c42\u503c\u3002\uff08\u53c2\u8003\uff1a 5-1-Operator-precedence-and-associativity#\u8868\u8fbe\u5f0f\u6c42\u503c\u987a\u5e8f\u548c\u51fd\u6570\u53c2\u6570\u5904\u7406\u987a\u5e8f\u5927\u591a\u662f\u672a\u6307\u660e\u7684 \uff09 \u6269\u5c55\u9605\u8bfb \u53ea\u6709 C++ \u81ea\u5e26\u7684\u8fd9\u4e9b\u903b\u8f91\u8fd0\u7b97\u7b26\u4f1a\u6267\u884c\u77ed\u8def\u6c42\u503c\uff0c\u5982\u679c\u4f60\u81ea\u5df1\u91cd\u8f7d\u4e86\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u4ee5\u4fbf\u5c06\u5176\u5e94\u7528\u4e8e\u4f60\u81ea\u5df1\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u8fd9\u4e9b\u91cd\u8f7d\u540e\u7684\u8fd0\u7b97\u7b26\u662f\u4e0d\u5177\u5907\u77ed\u8def\u6c42\u503c\u529f\u80fd\u7684\u3002 \u6df7\u5408 AND \u548c OR \u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u91cc\u9762\u6df7\u5408\u4f7f\u7528\u903b\u8f91\u4e0e\u548c\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u901a\u5e38\u662f\u65e0\u6cd5\u907f\u514d\u7684\uff0c\u4f46\u662f\u8fd9\u6837\u7684\u7528\u6cd5\u4e5f\u5b58\u5728\u4e00\u5b9a\u7684\u5371\u9669\u3002 \u5f88\u591a\u7a0b\u5e8f\u5458\u4f1a\u5047\u5b9a\u903b\u8f91\u4e0e\u548c\u903b\u8f91\u6216\u5177\u6709\u76f8\u540c\u7684\u4f18\u5148\u7ea7\uff08\u6216\u5fd8\u8bb0\u4e86\u5b83\u4eec\u7684\u4f18\u5148\u7ea7\u662f\u4e0d\u540c\u7684\uff09\uff0c\u5c31\u50cf\u52a0/\u51cf\u6216\u8005\u4e58/\u9664\u90a3\u6837\u3002\u4f46\u662f\uff0c\u903b\u8f91\u4e0e\u7684\u4f18\u5148\u7ea7\u5176\u5b9e\u6bd4\u903b\u8f91\u6216\u8981\u9ad8\uff0c\u56e0\u6b64\u903b\u8f91\u4e0e\u4f1a\u5148\u4e8e\u903b\u8f91\u6216\u6c42\u503c\uff08\u9664\u975e\u6709\u62ec\u53f7\u4fee\u6539\u5176\u4f18\u5148\u7ea7\uff09\u3002 \u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u5199\u51fa\u8fd9\u6837\u7684\u8868\u8fbe\u5f0f\uff1a value1 || value2 && value3 \u3002\u56e0\u4e3a\u903b\u8f91\u4e0e\u7684\u4f18\u5148\u7ea7\u66f4\u9ad8\uff0c\u6240\u4ee5\u4e0a\u8ff0\u8868\u8fbe\u5f0f\u7b49\u4ef7\u4e8e value1 || (value2 && value3) \u800c\u4e0d\u662f (value1 || value2) && value3 \u3002\u5982\u679c\u8fd9\u6b63\u662f\u4f60\u60f3\u8981\u7684\uff0c\u90a3\u5c31\u597d\u4e86\u3002\u4f46\u662f\u5982\u679c\u4f60\u5047\u8bbe\u4e0a\u9762\u7684\u8868\u8fbe\u5f0f\u662f\u6309\u7167\u4ece\u5de6\u5411\u53f3\u7684\u987a\u5e8f\u6c42\u503c\uff0c\u90a3\u5f97\u5230\u7684\u7ed3\u679c\u80af\u5b9a\u51fa\u4f60\u6240\u6599\uff01 \u5982\u679c\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u91cc\u540c\u65f6\u4f7f\u7528\u903b\u8f91\u4e0e\u548c\u903b\u8f91\u6216\uff0c\u6700\u597d\u4f7f\u7528\u62ec\u53f7\u4e3a\u5176\u660e\u786e\u6307\u5b9a\u4f18\u5148\u7ea7\u3002\u8fd9\u6837\u4e0d\u4ec5\u53ef\u4ee5\u907f\u514d\u7531\u4f18\u5148\u7ea7\u5f15\u8d77\u7684\u987a\u5e8f\uff0c\u8fd8\u80fd\u591f\u589e\u5f3a\u53ef\u8bfb\u6027\u5e76\u6e05\u6670\u5730\u8868\u660e\u4f60\u7684\u76ee\u7684\u3002\u4f8b\u5982\uff0c value1 && value2 || value3 && value4 \u6700\u597d\u6539\u5199\u4e3a (value1 && value2) || (value3 && value4) . \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u91cc\u540c\u65f6\u4f7f\u7528\u903b\u8f91\u4e0e\u548c\u903b\u8f91\u6216\uff0c\u6700\u597d\u4f7f\u7528\u62ec\u53f7\u4e3a\u5176\u660e\u786e\u6307\u5b9a\u4f18\u5148\u7ea7\u3002 \u5fb7\u6469\u6839\u5b9a\u5f8b\uff08De Morgan\u2018s law\uff09 \u5f88\u591a\u7a0b\u5e8f\u5458\u4f1a\u8ba4\u4e3a !(x && y) \u7b49\u4ef7\u4e8e !x && !y \u3002\u4e0d\u5e78\u7684\u662f\uff0c\u903b\u8f91\u975e\u8fd0\u7b97\u7b26\u5e76\u4e0d\u80fd\u50cf\u8fd9\u6837\u8fdb\u884c\u5206\u914d\u3002 \u6839\u636e \u5fb7\u6469\u6839\u5b9a\u5f8b \uff0c\u903b\u8f91\u975e\u7684\u5206\u914d\u5f8b\u5e94\u8be5\u662f\u4e0b\u9762\u8fd9\u79cd\u65b9\u5f0f\uff1a !(x && y) \u7b49\u4ef7\u4e8e !x || !y !(x || y) \u7b49\u4ef7\u4e8e !x && !y \u6362\u8a00\u4e4b\uff0c\u5f53\u4f60\u5206\u914d\u903b\u8f91\u975e\u8fd0\u7b97\u7b26\u65f6\uff0c\u5fc5\u987b\u5c06\u903b\u8f91\u4e0e\u66ff\u6362\u4e3a\u903b\u8f91\u6216\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002 \u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5fb7\u6469\u6839\u5b9a\u5f8b\u5bf9\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u8fdb\u884c\u7b80\u5316\u3002 \u6269\u5c55\u9605\u8bfb \u6211\u4eec\u53ef\u4ee5\u8bc1\u660e\u5fb7\u6469\u6839\u5b9a\u5f8b\u7684\u7b2c\u4e00\u90e8\u5206\u662f\u6b63\u786e\u7684\uff0c\u5373\u5bf9\u4e8e\u4efb\u4f55\u53ef\u80fd\u7684 x \u548c y \uff0c !(x && y) \u7b49\u4e8e !x || !y \u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u79f0\u4e3a \u771f\u503c\u8868 \u7684\u6570\u5b66\u6982\u5ff5\u3002 x y !x !y !(x && y) !x || !y false false true true true true false true true false true true true false false true true true true true false false false false \u5728\u8868\u4e2d\uff0c\u7b2c\u4e00\u5217\u548c\u7b2c\u4e8c\u5217\u5206\u522b\u8868\u793a x \u548c y \u3002\u6bcf\u4e00\u884c\u5219\u8868\u793a\u4e00\u7ec4\u53ef\u80fd\u7684 x \u548c y \u7ec4\u5408\uff0c4\u884c\u5c31\u8db3\u4ee5\u8868\u793a\u6240\u6709\u7684\u53ef\u80fd\u3002 \u5269\u4e0b\u7684\u5217\u8868\u793a\u57fa\u4e8e\u521d\u59cb x \u548c y \u6784\u9020\u7684\u8868\u8fbe\u5f0f\u3002\u7b2c\u4e09\u5217\u548c\u7b2c\u56db\u5217\u5206\u522b\u8ba1\u7b97 !x \u548c !y \u3002\u7b2c\u4e94\u5217\u8ba1\u7b97 !(x && y) \u3002\u7b2c\u516d\u5217\u5219\u8ba1\u7b97 !x || !y \u3002 \u53ef\u4ee5\u6ce8\u610f\u5230\uff0c\u7b2c\u4e94\u5217\u548c\u7b2c\u516d\u5217\u7684\u503c\u603b\u662f\u76f8\u540c\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u4e8e\u4efb\u610f x \u548c y \uff0c !(x && y) \u7b49\u4e8e !x || !y \uff0c\u8fd9\u4e5f\u6b63\u662f\u6211\u4eec\u8981\u8bc1\u660e\u7684\u3002 \u540c\u6837\u7684\uff0c\u6211\u4eec\u8fd8\u80fd\u591f\u8bc1\u660e\u5fb7\u6469\u6839\u5b9a\u5f8b\u7684\u7b2c\u4e8c\u90e8\u5206\u4e5f\u662f\u6b63\u786e\u7684\uff1a x y !x !y !(x || y) !x && !y false false true true true true false true true false false false true false false true false false true true false false false false \u540c\u6837\u7684\uff0c\u5bf9\u4e8e\u4efb\u610f x \u548c y \uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230 !(x || y) \u7b49\u4e8e !x && !y \u3002 \u903b\u8f91\u5f02\u6216\u8fd0\u7b97\u7b26\u5728\u54ea\u91cc\uff1f \u6709\u4e9b\u8bed\u8a00\u63d0\u4f9b\u4e86\u903b\u8f91\u5f02\u6216\uff08XOR\uff09\u8fd0\u7b97\u7b26\uff0c\u7528\u6765\u6d4b\u8bd5 \u662f\u5426\u6709\u5947\u6570\u4e2a\u6761\u4ef6\u4e3a\u771f \u3002 \u5de6\u64cd\u4f5c\u6570 \u53f3\u64cd\u4f5c\u6570 \u7ed3\u679c false false false false true true true false true true true false C++ \u5e76\u6ca1\u6709\u63d0\u4f9b\u903b\u8f91\u5f02\u6216\u8fd0\u7b97\u7b26\u3002\u548c \u903b\u8f91\u6216 \u6216\u8005 \u903b\u8f91\u4e0e \u4e0d\u540c\uff0c \u903b\u8f91\u5f02\u6216 \u5e76\u6ca1\u6709\u77ed\u8def\u6c42\u503c\u7279\u6027\u3002\u56e0\u6b64\uff0c\u4f7f\u7528\u903b\u8f91\u6216\u6216\u8005\u903b\u8f91\u4e0e\u6765\u6784\u5efa\u4e00\u4e2a\u903b\u8f91\u5f02\u6216\u662f\u5f88\u56f0\u96be\u7684\u3002\u4f46\u662f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0d\u7b49\u53f7( != )\u6765\u6a21\u62df\u903b\u8f91\u5f02\u6216\uff1a 1 if ( a != b ) ... // a XOR b, \u5047\u8bbe a \u548c b \u90fd\u662f\u5e03\u5c14\u503c \u4e5f\u53ef\u4ee5\u5bf9\u591a\u4e2a\u64cd\u4f5c\u6570\u6765\u4f7f\u7528\uff1a 1 if ( a != b != c != d ) ... // a XOR b XOR c XOR d, \u5047\u8bbe a, b, c, \u548c d \u90fd\u662f\u5e03\u5c14\u503c \u6ce8\u610f\uff0c\u4e0a\u9762\u7684\u65b9\u6cd5\u53ea\u6709\u5f53\u64cd\u4f5c\u6570\u90fd\u662f\u5e03\u5c14\u7c7b\u578b\u65f6\u624d\u6709\u6548 (\u4e0d\u80fd\u662f\u6574\u578b)\u3002\u5982\u679c\u4f60\u60f3\u5c06\u903b\u8f91\u5f02\u6216\u5e94\u7528\u4e8e\u975e\u5e03\u5c14\u7c7b\u578b\u7684\u64cd\u4f5c\u6570\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06\u5b83\u8f6c\u6362\u4e3a\u5e03\u5c14\u7c7b\u578b\u3002 1 if ( static_cast < bool > ( a ) != static_cast < bool > ( b ) != static_cast < bool > ( c ) != static_cast < bool > ( d )) ... // a XOR b XOR c XOR d, for any type that can be converted to bool \u8fd0\u7b97\u7b26\u7684\u5176\u4ed6\u8868\u793a\u5f62\u5f0f C++ \u4e2d\u7684\u5f88\u591a\u64cd\u4f5c\u7b26 (\u4f8b\u5982 operator || ) \u90fd\u6709\u53ef\u4ee5\u4ee3\u66ff\u7b26\u53f7\u8868\u793a\u6cd5\u7684\u5173\u952e\u5b57\u3002\u51fa\u4e8e\u5386\u53f2\u539f\u56e0\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u7684\u952e\u76d8\u6216\u8005\u6807\u51c6\u652f\u6301\u8f93\u5165\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u7684\u7b26\u53f7\u3002\u56e0\u6b64C++\u63d0\u4f9b\u4e86\u4e00\u7ec4\u5173\u952e\u5b57\u7528\u4e8e\u4ee3\u66ff\u7b26\u53f7\u3002\u4f8b\u5982\uff0c || \u4e5f\u53ef\u4ee5\u7528 or \u6765\u66ff\u4ee3\u3002 \u5b8c\u6574\u7684\u5217\u8868\u53ef\u4ee5\u53c2\u8003 \u8fd9\u91cc \uff0c\u5c24\u5176\u53ef\u4ee5\u6ce8\u610f\u4e00\u4e0b\u4e0b\u9762\u4e09\u79cd\uff1a \u8fd0\u7b97\u7b26 \u5173\u952e\u5b57\u66ff\u4ee3\u540d && and || or ! not \u56e0\u6b64\u4e0b\u9762\u4e24\u884c\u4ee3\u7801\u662f\u7b49\u4ef7\u7684\uff1a 1 2 std :: cout << ! a && ( b || c ); std :: cout << not a and ( b or c ); \u5c3d\u7ba1\u8fd9\u4e9b\u5173\u952e\u5b57\u770b\u8d77\u6765\u53ef\u8bfb\u6027\u66f4\u597d\uff0c\u4f46\u662f\u5927\u591a\u6570\u6709\u7ecf\u9a8c\u7684 C++ \u7a0b\u5e8f\u5458\u8fd8\u662f\u559c\u6b22\u4f7f\u7528\u7b26\u53f7\u800c\u4e0d\u662f\u5173\u952e\u5b57\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u8fd8\u662f\u5efa\u8bae\u4f60\u5b66\u4e60\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u7684\u7b26\u53f7\u8868\u793a\u6cd5\uff0c\u8fd9\u4e5f\u662f\u4ee3\u7801\u4e2d\u6700\u5e38\u89c1\u7684\u5f62\u5f0f\u3002","title":"5.7 - \u903b\u8f91\u8fd0\u7b97\u7b26"},{"location":"5-7-Logical-operators/#not","text":"\u5728 4.9 - \u5e03\u5c14\u503c \u4e00\u8282\u8bfe\u4e2d\u4f60\u5df2\u7ecf\u9047\u5230\u903b\u8f91\u975e\u8fd9\u4e2a\u4e00\u5143\u8fd0\u7b97\u7b26\uff1a \u64cd\u4f5c\u7b26 \u7ed3\u679c true false false true \u5982\u679c\u903b\u8f91\u975e\u7684\u64cd\u4f5c\u6570\u6c42\u503c\u4e3a true \uff0c\u5219\u903b\u8f91\u975e\u6c42\u503c\u4e3a false \u3002\u5982\u679c\u903b\u8f91\u975e\u7684\u64cd\u4f5c\u6570\u6c42\u503c\u4e3a false \uff0c\u903b\u8f91\u975e\u7684\u6c42\u503c\u4e3a true \u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u903b\u8f91\u975e\u53ef\u4ee5\u5c06\u5e03\u5c14\u503c\u53cd\u8f6c\uff0c\u771f\u53d8\u6210\u5047\uff0c\u5047\u53d8\u6210\u771f\u3002 \u903b\u8f91\u975e\u7ecf\u5e38\u88ab\u7528\u5728\u6761\u4ef6\u8868\u8fbe\u5f0f\u4e2d\uff1a 1 2 3 4 5 bool tooLarge { x > 100 }; // tooLarge is true if x > 100 if ( ! tooLarge ) // do something with x else // print an error \u6709\u4ef6\u4e8b\u60c5\u9700\u8981\u6ce8\u610f\uff0c\u90a3\u5c31\u903b\u8f91\u975e\u7684\u4f18\u5148\u7ea7\u975e\u5e38\u9ad8\u3002\u65b0\u624b\u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u72af\u8fd9\u6837\u7684\u9519\u8bef\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 5 }; int y { 7 }; if ( ! x > y ) std :: cout << x << \" is not greater than \" << y << '\\n' ; else std :: cout << x << \" is greater than \" << y << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 5 is greater than 7 \u4f46\u662f x \u5e76\u4e0d\u6bd4 7 \u5927\uff0c\u8fd9\u7ed3\u679c\u662f\u600e\u4e48\u5f97\u5230\u7684\uff1f\u8fd9\u662f\u56e0\u4e3a\u903b\u8f91\u975e\u7684\u4f18\u5148\u7ea7\u6bd4\u5927\u4e8e\u53f7\u7684\u4f18\u5148\u7ea7\u9ad8\uff0c\u56e0\u6b64 ! x > y \u5b9e\u9645\u4e0a\u76f8\u5f53\u4e8e (!x) > y \u3002\u56e0\u4e3a x \u662f 5\uff0c !x \u6c42\u503c\u7ed3\u679c\u4e3a 0 \uff0c\u800c 0 > y \u662f\u5047\uff0c\u6240\u4ee5\u6267\u884c\u7684\u662f else \u7684\u8bed\u53e5\u3002 \u6b63\u786e\u7684\u505a\u6cd5\u5e94\u8be5\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 5 }; int y { 7 }; if ( ! ( x > y )) std :: cout << x << \" is not greater than \" << y << '\\n' ; else std :: cout << x << \" is greater than \" << y << '\\n' ; return 0 ; } \u8fd9\u6837 x > y \u4f1a\u9996\u5148\u8fdb\u884c\u6c42\u503c\uff0c\u7136\u540e\u518d\u901a\u8fc7\u903b\u8f91\u975e\u5c06\u5e03\u5c14\u7ed3\u679c\u53cd\u8f6c\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u903b\u8f91\u975e\u8981\u5bf9\u5176\u4ed6\u8fd0\u7b97\u7b26\u7684\u7ed3\u679c\u8fdb\u884c\u64cd\u4f5c\uff0c\u90a3\u4e48\u5176\u4ed6\u8fd0\u7b97\u7b26\u548c\u5b83\u4eec\u7684\u64cd\u4f5c\u6570\u5e94\u8be5\u653e\u5728\u62ec\u53f7\u4e2d\u3002 \u903b\u8f91\u975e\u7684\u7b80\u5355\u7528\u6cd5\uff0c\u4f8b\u5982 if (!value) \u5e76\u4e0d\u9700\u8981\u62ec\u53f7\uff0c\u56e0\u4e3a\u8fd9\u65f6\u4e0d\u4f1a\u53d7\u5230\u4f18\u5148\u7ea7\u7684\u5f71\u54cd\u3002","title":"\u903b\u8f91\u975e\uff08NOT\uff09"},{"location":"5-7-Logical-operators/#or","text":"\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u88ab\u7528\u6765\u6d4b\u8bd5\u4e24\u4e2a\u6761\u4ef6\u4e2d\u662f\u5426\u6709\u4e3a\u771f\u7684\u3002\u5982\u679c\u5de6\u64cd\u4f5c\u6570\u6216\u53f3\u64cd\u4f5c\u6570\u4e3a true \uff0c\u6216\u8005\u4e24\u4e2a\u90fd\u662f true \uff0c\u5219\u903b\u8f91\u6216\u8868\u8fbe\u5f0f\u4f1a\u8fd4\u56de true \uff0c\u5426\u5219\u8fd4\u56de false \u3002 \u5de6\u64cd\u4f5c\u6570 \u53f3\u64cd\u4f5c\u6570 \u7ed3\u679c false false false false true true true false true true true true \u4f8b\u5982\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { std :: cout << \"Enter a number: \" ; int value {}; std :: cin >> value ; if ( value == 0 || value == 1 ) std :: cout << \"You picked 0 or 1 \\n \" ; else std :: cout << \"You did not pick 0 or 1 \\n \" ; return 0 ; } \u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f7f\u7528\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u6765\u5224\u65ad\u505a\u64cd\u4f5c\u6570 ( value == 0 ) \u6216\u8005\u53f3\u64cd\u4f5c\u6570 ( value == 1 ) \u662f\u5426\u4e3a\u771f\u3002\u5982\u679c\u5176\u4e00\uff08\u6216\u4e24\u4e2a\uff09\u5168\u90e8\u4e3a\u771f\uff0c\u5219\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u7684\u6c42\u503c\u7ed3\u679c\u4e3a\u771f\uff0cif \u8bed\u53e5\u5c31\u4f1a\u88ab\u6267\u884c\u3002\u5982\u679c\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u662f\u5047\uff0c\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u7684\u7ed3\u679c\u5c31\u662f\u5047\uff0c\u90a3\u4e48 else \u8bed\u53e5\u5c31\u4f1a\u6267\u884c\u3002 \u4f60\u53ef\u4ee5\u5c06\u591a\u4e2a\u903b\u8f91\u6216\u8868\u8fbe\u5f0f\u8fde\u63a5\u8d77\u6765\uff1a 1 2 if ( value == 0 || value == 1 || value == 2 || value == 3 ) std :: cout << \"You picked 0, 1, 2, or 3 \\n \" ; \u65b0\u624b\u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u628a\u903b\u8f91\u6216\u8fd0\u7b97\u7b26 ( || ) \u548c\u6309\u4f4d\u6216\u8fd0\u7b97\u7b26\u641e\u6df7 ( | ) (\u7a0d\u540e\u4f1a\u4ecb\u7ecd)\u3002 \u5373\u4f7f\u5b83\u4eec\u7684\u540d\u5b57\u4e2d\u90fd\u6709\u4e00\u4e2a\u6216\uff0c\u5176\u529f\u80fd\u786e\u662f\u4e0d\u540c\u7684\u3002\u5c06\u5b83\u4eec\u6df7\u4e3a\u4e00\u8c08\u4f1a\u5e26\u6765\u9519\u8bef\u7684\u7ed3\u679c\u3002","title":"\u903b\u8f91\u6216\uff08OR\uff09"},{"location":"5-7-Logical-operators/#and","text":"\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u88ab\u7528\u4f5c\u6d4b\u8bd5\u4e24\u4e2a\u64cd\u4f5c\u6570\u662f\u5426\u5747\u4e3a\u771f\u3002\u5982\u679c\u5747\u4e3a\u771f\uff0c\u5219\u903b\u8f91\u4e0e\u4f1a\u8fd4\u56de true \uff0c\u5426\u5219\u8fd4\u56de\u503c\u4e3a false \u3002 \u5de6\u64cd\u4f5c\u6570 \u53f3\u64cd\u4f5c\u6570 \u7ed3\u679c false false false false true false true false false true true true \u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u8981\u5224\u65ad x \u662f\u5426\u5728 10 \u5230 20 \u4e4b\u95f4\uff1a\u6211\u4eec\u5fc5\u987b\u77e5\u9053 x \u662f\u5426\u5927\u4e8e 10 \uff0c \u5e76\u4e14 \u662f\u5426\u5c0f\u4e8e 20\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { std :: cout << \"Enter a number: \" ; int value {}; std :: cin >> value ; if ( value > 10 && value < 20 ) std :: cout << \"Your value is between 10 and 20 \\n \" ; else std :: cout << \"Your value is not between 10 and 20 \\n \" ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u68c0\u6d4b\u5de6\u4fa7\u7684\u6761\u4ef6 (value > 10) \u548c \u53f3\u4fa7\u7684\u6761\u4ef6 (value < 20) \u662f\u5426\u90fd\u662f true \u3002\u5982\u679c\u90fd\u662f\u771f\uff0c\u5219\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u7684\u6c42\u503c\u7ed3\u679c\u4e3a true \uff0c\u6267\u884c if \u8bed\u53e5\u3002\u5982\u679c\u5de6\u53f3\u4e24\u4e2a\u6761\u4ef6\u90fd\u662f\u5047\uff0c\u5219\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u7684\u6c42\u503c\u7ed3\u679c\u4e3a false \uff0c\u6267\u884c else \u8bed\u53e5\u3002 \u548c\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u4e00\u6837\uff0c\u4f60\u53ef\u4ee5\u628a\u591a\u4e2a\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u8fde\u63a5\u8d77\u6765\uff1a 1 2 3 4 if ( value > 10 && value < 20 && value != 16 ) // do something else // do something else \u5982\u679c\u4e0a\u8ff0\u6240\u6709\u6761\u4ef6\u90fd\u662f\u771f\uff0c\u5219\u6267\u884c if \u8bed\u53e5\u3002\u5982\u679c\u4efb\u4f55\u4e00\u4e2a\u6761\u4ef6\u4e3a\u5047\uff0c\u5219\u6267\u884c else \u8bed\u53e5\u3002 \u65b0\u624b\u7a0b\u5e8f\u5458\u4e0d\u4ec5\u5bb9\u6613\u628a \u903b\u8f91\u6216 \u548c \u6309\u4f4d\u6216 \u641e\u6df7\uff0c\u4e5f\u4f1a\u628a\u903b\u8f91\u4e0e ( && ) \u548c\u6309\u4f4d\u4e0e ( & )\u641e\u6df7\u3002","title":"\u903b\u8f91\u4e0e\uff08AND)"},{"location":"5-7-Logical-operators/#_1","text":"\u4e3a\u4e86\u8ba9\u903b\u8f91\u4e0e\u8fd4\u56de true \uff0c\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u9700\u8981\u6c42\u503c\u4e3a\u771f\u3002\u5982\u679c\u7b2c\u4e00\u4e2a\u64cd\u4f5c\u6570\u6c42\u503c\u4e3a\u5047\uff0c\u4e0d\u8bba\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u6570\u6c42\u503c\u7ed3\u679c\u5982\u4f55\uff0c\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u4e00\u5b9a\u6c42\u503c\u4e3a false \u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u4f1a\u7acb\u5373\u8fd4\u56de false \uff0c\u800c\u4e0d\u4f1a\u518d\u5bf9\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u6570\u6c42\u503c\u3002\u8fd9\u4e2a\u65b9\u6cd5\u79f0\u4e3a\u77ed\u8def\u6c42\u503c\uff0c\u8fd9\u4e48\u505a\u53ef\u4ee5\u4f18\u5316\u7a0b\u5e8f\u6027\u80fd\u3002 \u7c7b\u4f3c\u5730\uff0c\u5982\u679c\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u7684\u7b2c\u4e00\u4e2a\u64cd\u4f5c\u6570\u6c42\u503c\u7ed3\u679c\u4e3a\u771f\uff0c\u5219\u5b83\u4f1a\u76f4\u63a5\u8fd4\u56de true \uff0c\u800c\u4e0d\u5fc5\u5bf9\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u6570\u8fdb\u884c\u6c42\u503c\u3002 \u77ed\u8def\u6c42\u503c\u8fd8\u5411\u6211\u4eec\u5c55\u793a\u4e86\uff0c\u4e3a\u4ec0\u4e48\u6709\u526f\u4f5c\u7528\u7684\u8fd0\u7b97\u7b26\uff0c\u4e0d\u5e94\u8be5\u653e\u5230\u590d\u5408\u8868\u8fbe\u5f0f\u4e2d\u3002\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 if ( x == 1 && ++ y == 2 ) // do something \u5982\u679c x \u4e0d\u7b49\u4e8e 1\uff0c\u90a3\u4e48\u6574\u4e2a\u6761\u4ef6\u4e00\u5b9a\u4f1a\u8fd4\u56de false \u3002\u6240\u4ee5 ++y \u6839\u672c\u4e0d\u4f1a\u88ab\u6c42\u503c\u3002\u56e0\u6b64\uff0c\u53ea\u6709\u5f53 x \u7b49\u4e8e 1 \u65f6\uff0c y \u624d\u4f1a\u9012\u589e\uff0c\u4e5f\u8bb8\u8fd9\u5e76\u4e0d\u662f\u4f60\u9700\u8981\u7684\uff01 \u6ce8\u610f \u77ed\u8def\u6c42\u503c\u53ef\u80fd\u4f1a\u5bfc\u81f4 \u903b\u8f91\u4e0e \u6216 \u903b\u8f91\u975e \u4e0d\u5bf9\u7b2c\u4e8c\u4e2a\u8868\u8fbe\u5f0f\u6c42\u503c\u3002\u6240\u4ee5\u8bf7\u907f\u514d\u5c06\u5177\u6709\u526f\u4f5c\u7528\u7684\u8fd0\u7b97\u7b26\u548c\u903b\u8f91\u4e0e\u6216\u903b\u8f91\u975e\u4e00\u8d77\u4f7f\u7528\u3002 \u5173\u952e\u4fe1\u606f \u903b\u8f91\u6216\u548c\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u7684\u4e24\u4e2a\u64cd\u4f5c\u6570\uff0c\u6c42\u503c\u987a\u5e8f\u662f\u56fa\u5b9a\u7684\uff0c\u56e0\u4e3a\u6807\u51c6\u91cc\u5bf9\u5176\u8fdb\u884c\u4e86\u8bf4\u660e\uff0c\u5de6\u8fb9\u7684\u64cd\u4f5c\u6570\u603b\u662f\u5148\u6c42\u503c\u3002\uff08\u53c2\u8003\uff1a 5-1-Operator-precedence-and-associativity#\u8868\u8fbe\u5f0f\u6c42\u503c\u987a\u5e8f\u548c\u51fd\u6570\u53c2\u6570\u5904\u7406\u987a\u5e8f\u5927\u591a\u662f\u672a\u6307\u660e\u7684 \uff09 \u6269\u5c55\u9605\u8bfb \u53ea\u6709 C++ \u81ea\u5e26\u7684\u8fd9\u4e9b\u903b\u8f91\u8fd0\u7b97\u7b26\u4f1a\u6267\u884c\u77ed\u8def\u6c42\u503c\uff0c\u5982\u679c\u4f60\u81ea\u5df1\u91cd\u8f7d\u4e86\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u4ee5\u4fbf\u5c06\u5176\u5e94\u7528\u4e8e\u4f60\u81ea\u5df1\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u8fd9\u4e9b\u91cd\u8f7d\u540e\u7684\u8fd0\u7b97\u7b26\u662f\u4e0d\u5177\u5907\u77ed\u8def\u6c42\u503c\u529f\u80fd\u7684\u3002","title":"\u77ed\u8def\u6c42\u503c"},{"location":"5-7-Logical-operators/#and-or","text":"\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u91cc\u9762\u6df7\u5408\u4f7f\u7528\u903b\u8f91\u4e0e\u548c\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u901a\u5e38\u662f\u65e0\u6cd5\u907f\u514d\u7684\uff0c\u4f46\u662f\u8fd9\u6837\u7684\u7528\u6cd5\u4e5f\u5b58\u5728\u4e00\u5b9a\u7684\u5371\u9669\u3002 \u5f88\u591a\u7a0b\u5e8f\u5458\u4f1a\u5047\u5b9a\u903b\u8f91\u4e0e\u548c\u903b\u8f91\u6216\u5177\u6709\u76f8\u540c\u7684\u4f18\u5148\u7ea7\uff08\u6216\u5fd8\u8bb0\u4e86\u5b83\u4eec\u7684\u4f18\u5148\u7ea7\u662f\u4e0d\u540c\u7684\uff09\uff0c\u5c31\u50cf\u52a0/\u51cf\u6216\u8005\u4e58/\u9664\u90a3\u6837\u3002\u4f46\u662f\uff0c\u903b\u8f91\u4e0e\u7684\u4f18\u5148\u7ea7\u5176\u5b9e\u6bd4\u903b\u8f91\u6216\u8981\u9ad8\uff0c\u56e0\u6b64\u903b\u8f91\u4e0e\u4f1a\u5148\u4e8e\u903b\u8f91\u6216\u6c42\u503c\uff08\u9664\u975e\u6709\u62ec\u53f7\u4fee\u6539\u5176\u4f18\u5148\u7ea7\uff09\u3002 \u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u5199\u51fa\u8fd9\u6837\u7684\u8868\u8fbe\u5f0f\uff1a value1 || value2 && value3 \u3002\u56e0\u4e3a\u903b\u8f91\u4e0e\u7684\u4f18\u5148\u7ea7\u66f4\u9ad8\uff0c\u6240\u4ee5\u4e0a\u8ff0\u8868\u8fbe\u5f0f\u7b49\u4ef7\u4e8e value1 || (value2 && value3) \u800c\u4e0d\u662f (value1 || value2) && value3 \u3002\u5982\u679c\u8fd9\u6b63\u662f\u4f60\u60f3\u8981\u7684\uff0c\u90a3\u5c31\u597d\u4e86\u3002\u4f46\u662f\u5982\u679c\u4f60\u5047\u8bbe\u4e0a\u9762\u7684\u8868\u8fbe\u5f0f\u662f\u6309\u7167\u4ece\u5de6\u5411\u53f3\u7684\u987a\u5e8f\u6c42\u503c\uff0c\u90a3\u5f97\u5230\u7684\u7ed3\u679c\u80af\u5b9a\u51fa\u4f60\u6240\u6599\uff01 \u5982\u679c\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u91cc\u540c\u65f6\u4f7f\u7528\u903b\u8f91\u4e0e\u548c\u903b\u8f91\u6216\uff0c\u6700\u597d\u4f7f\u7528\u62ec\u53f7\u4e3a\u5176\u660e\u786e\u6307\u5b9a\u4f18\u5148\u7ea7\u3002\u8fd9\u6837\u4e0d\u4ec5\u53ef\u4ee5\u907f\u514d\u7531\u4f18\u5148\u7ea7\u5f15\u8d77\u7684\u987a\u5e8f\uff0c\u8fd8\u80fd\u591f\u589e\u5f3a\u53ef\u8bfb\u6027\u5e76\u6e05\u6670\u5730\u8868\u660e\u4f60\u7684\u76ee\u7684\u3002\u4f8b\u5982\uff0c value1 && value2 || value3 && value4 \u6700\u597d\u6539\u5199\u4e3a (value1 && value2) || (value3 && value4) . \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u91cc\u540c\u65f6\u4f7f\u7528\u903b\u8f91\u4e0e\u548c\u903b\u8f91\u6216\uff0c\u6700\u597d\u4f7f\u7528\u62ec\u53f7\u4e3a\u5176\u660e\u786e\u6307\u5b9a\u4f18\u5148\u7ea7\u3002","title":"\u6df7\u5408 AND \u548c OR"},{"location":"5-7-Logical-operators/#de-morgans-law","text":"\u5f88\u591a\u7a0b\u5e8f\u5458\u4f1a\u8ba4\u4e3a !(x && y) \u7b49\u4ef7\u4e8e !x && !y \u3002\u4e0d\u5e78\u7684\u662f\uff0c\u903b\u8f91\u975e\u8fd0\u7b97\u7b26\u5e76\u4e0d\u80fd\u50cf\u8fd9\u6837\u8fdb\u884c\u5206\u914d\u3002 \u6839\u636e \u5fb7\u6469\u6839\u5b9a\u5f8b \uff0c\u903b\u8f91\u975e\u7684\u5206\u914d\u5f8b\u5e94\u8be5\u662f\u4e0b\u9762\u8fd9\u79cd\u65b9\u5f0f\uff1a !(x && y) \u7b49\u4ef7\u4e8e !x || !y !(x || y) \u7b49\u4ef7\u4e8e !x && !y \u6362\u8a00\u4e4b\uff0c\u5f53\u4f60\u5206\u914d\u903b\u8f91\u975e\u8fd0\u7b97\u7b26\u65f6\uff0c\u5fc5\u987b\u5c06\u903b\u8f91\u4e0e\u66ff\u6362\u4e3a\u903b\u8f91\u6216\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002 \u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5fb7\u6469\u6839\u5b9a\u5f8b\u5bf9\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u8fdb\u884c\u7b80\u5316\u3002 \u6269\u5c55\u9605\u8bfb \u6211\u4eec\u53ef\u4ee5\u8bc1\u660e\u5fb7\u6469\u6839\u5b9a\u5f8b\u7684\u7b2c\u4e00\u90e8\u5206\u662f\u6b63\u786e\u7684\uff0c\u5373\u5bf9\u4e8e\u4efb\u4f55\u53ef\u80fd\u7684 x \u548c y \uff0c !(x && y) \u7b49\u4e8e !x || !y \u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u79f0\u4e3a \u771f\u503c\u8868 \u7684\u6570\u5b66\u6982\u5ff5\u3002 x y !x !y !(x && y) !x || !y false false true true true true false true true false true true true false false true true true true true false false false false \u5728\u8868\u4e2d\uff0c\u7b2c\u4e00\u5217\u548c\u7b2c\u4e8c\u5217\u5206\u522b\u8868\u793a x \u548c y \u3002\u6bcf\u4e00\u884c\u5219\u8868\u793a\u4e00\u7ec4\u53ef\u80fd\u7684 x \u548c y \u7ec4\u5408\uff0c4\u884c\u5c31\u8db3\u4ee5\u8868\u793a\u6240\u6709\u7684\u53ef\u80fd\u3002 \u5269\u4e0b\u7684\u5217\u8868\u793a\u57fa\u4e8e\u521d\u59cb x \u548c y \u6784\u9020\u7684\u8868\u8fbe\u5f0f\u3002\u7b2c\u4e09\u5217\u548c\u7b2c\u56db\u5217\u5206\u522b\u8ba1\u7b97 !x \u548c !y \u3002\u7b2c\u4e94\u5217\u8ba1\u7b97 !(x && y) \u3002\u7b2c\u516d\u5217\u5219\u8ba1\u7b97 !x || !y \u3002 \u53ef\u4ee5\u6ce8\u610f\u5230\uff0c\u7b2c\u4e94\u5217\u548c\u7b2c\u516d\u5217\u7684\u503c\u603b\u662f\u76f8\u540c\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u4e8e\u4efb\u610f x \u548c y \uff0c !(x && y) \u7b49\u4e8e !x || !y \uff0c\u8fd9\u4e5f\u6b63\u662f\u6211\u4eec\u8981\u8bc1\u660e\u7684\u3002 \u540c\u6837\u7684\uff0c\u6211\u4eec\u8fd8\u80fd\u591f\u8bc1\u660e\u5fb7\u6469\u6839\u5b9a\u5f8b\u7684\u7b2c\u4e8c\u90e8\u5206\u4e5f\u662f\u6b63\u786e\u7684\uff1a x y !x !y !(x || y) !x && !y false false true true true true false true true false false false true false false true false false true true false false false false \u540c\u6837\u7684\uff0c\u5bf9\u4e8e\u4efb\u610f x \u548c y \uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230 !(x || y) \u7b49\u4e8e !x && !y \u3002","title":"\u5fb7\u6469\u6839\u5b9a\u5f8b\uff08De Morgan\u2018s law\uff09"},{"location":"5-7-Logical-operators/#_2","text":"\u6709\u4e9b\u8bed\u8a00\u63d0\u4f9b\u4e86\u903b\u8f91\u5f02\u6216\uff08XOR\uff09\u8fd0\u7b97\u7b26\uff0c\u7528\u6765\u6d4b\u8bd5 \u662f\u5426\u6709\u5947\u6570\u4e2a\u6761\u4ef6\u4e3a\u771f \u3002 \u5de6\u64cd\u4f5c\u6570 \u53f3\u64cd\u4f5c\u6570 \u7ed3\u679c false false false false true true true false true true true false C++ \u5e76\u6ca1\u6709\u63d0\u4f9b\u903b\u8f91\u5f02\u6216\u8fd0\u7b97\u7b26\u3002\u548c \u903b\u8f91\u6216 \u6216\u8005 \u903b\u8f91\u4e0e \u4e0d\u540c\uff0c \u903b\u8f91\u5f02\u6216 \u5e76\u6ca1\u6709\u77ed\u8def\u6c42\u503c\u7279\u6027\u3002\u56e0\u6b64\uff0c\u4f7f\u7528\u903b\u8f91\u6216\u6216\u8005\u903b\u8f91\u4e0e\u6765\u6784\u5efa\u4e00\u4e2a\u903b\u8f91\u5f02\u6216\u662f\u5f88\u56f0\u96be\u7684\u3002\u4f46\u662f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0d\u7b49\u53f7( != )\u6765\u6a21\u62df\u903b\u8f91\u5f02\u6216\uff1a 1 if ( a != b ) ... // a XOR b, \u5047\u8bbe a \u548c b \u90fd\u662f\u5e03\u5c14\u503c \u4e5f\u53ef\u4ee5\u5bf9\u591a\u4e2a\u64cd\u4f5c\u6570\u6765\u4f7f\u7528\uff1a 1 if ( a != b != c != d ) ... // a XOR b XOR c XOR d, \u5047\u8bbe a, b, c, \u548c d \u90fd\u662f\u5e03\u5c14\u503c \u6ce8\u610f\uff0c\u4e0a\u9762\u7684\u65b9\u6cd5\u53ea\u6709\u5f53\u64cd\u4f5c\u6570\u90fd\u662f\u5e03\u5c14\u7c7b\u578b\u65f6\u624d\u6709\u6548 (\u4e0d\u80fd\u662f\u6574\u578b)\u3002\u5982\u679c\u4f60\u60f3\u5c06\u903b\u8f91\u5f02\u6216\u5e94\u7528\u4e8e\u975e\u5e03\u5c14\u7c7b\u578b\u7684\u64cd\u4f5c\u6570\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06\u5b83\u8f6c\u6362\u4e3a\u5e03\u5c14\u7c7b\u578b\u3002 1 if ( static_cast < bool > ( a ) != static_cast < bool > ( b ) != static_cast < bool > ( c ) != static_cast < bool > ( d )) ... // a XOR b XOR c XOR d, for any type that can be converted to bool","title":"\u903b\u8f91\u5f02\u6216\u8fd0\u7b97\u7b26\u5728\u54ea\u91cc\uff1f"},{"location":"5-7-Logical-operators/#_3","text":"C++ \u4e2d\u7684\u5f88\u591a\u64cd\u4f5c\u7b26 (\u4f8b\u5982 operator || ) \u90fd\u6709\u53ef\u4ee5\u4ee3\u66ff\u7b26\u53f7\u8868\u793a\u6cd5\u7684\u5173\u952e\u5b57\u3002\u51fa\u4e8e\u5386\u53f2\u539f\u56e0\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u7684\u952e\u76d8\u6216\u8005\u6807\u51c6\u652f\u6301\u8f93\u5165\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u7684\u7b26\u53f7\u3002\u56e0\u6b64C++\u63d0\u4f9b\u4e86\u4e00\u7ec4\u5173\u952e\u5b57\u7528\u4e8e\u4ee3\u66ff\u7b26\u53f7\u3002\u4f8b\u5982\uff0c || \u4e5f\u53ef\u4ee5\u7528 or \u6765\u66ff\u4ee3\u3002 \u5b8c\u6574\u7684\u5217\u8868\u53ef\u4ee5\u53c2\u8003 \u8fd9\u91cc \uff0c\u5c24\u5176\u53ef\u4ee5\u6ce8\u610f\u4e00\u4e0b\u4e0b\u9762\u4e09\u79cd\uff1a \u8fd0\u7b97\u7b26 \u5173\u952e\u5b57\u66ff\u4ee3\u540d && and || or ! not \u56e0\u6b64\u4e0b\u9762\u4e24\u884c\u4ee3\u7801\u662f\u7b49\u4ef7\u7684\uff1a 1 2 std :: cout << ! a && ( b || c ); std :: cout << not a and ( b or c ); \u5c3d\u7ba1\u8fd9\u4e9b\u5173\u952e\u5b57\u770b\u8d77\u6765\u53ef\u8bfb\u6027\u66f4\u597d\uff0c\u4f46\u662f\u5927\u591a\u6570\u6709\u7ecf\u9a8c\u7684 C++ \u7a0b\u5e8f\u5458\u8fd8\u662f\u559c\u6b22\u4f7f\u7528\u7b26\u53f7\u800c\u4e0d\u662f\u5173\u952e\u5b57\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u8fd8\u662f\u5efa\u8bae\u4f60\u5b66\u4e60\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u7684\u7b26\u53f7\u8868\u793a\u6cd5\uff0c\u8fd9\u4e5f\u662f\u4ee3\u7801\u4e2d\u6700\u5e38\u89c1\u7684\u5f62\u5f0f\u3002","title":"\u8fd0\u7b97\u7b26\u7684\u5176\u4ed6\u8868\u793a\u5f62\u5f0f"},{"location":"5-x-Chapter-5-summary-and-quiz/","tags":["summary"],"text":"\u7ae0\u8282\u56de\u987e \u4f7f\u7528\u62ec\u53f7\u660e\u786e\u6307\u5b9a\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\uff0c\u907f\u514d\u6b67\u4e49\u3002\uff08 5.1 - \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b \uff09 \u7b97\u6570\u8fd0\u7b97\u7b26\u7684\u884c\u4e3a\u548c\u666e\u901a\u7684\u6570\u5b66\u8fd0\u7b97\u7b26\u662f\u4e00\u6837\u7684\u3002\uff08 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 \uff09\u6c42\u6a21\u8fd0\u7b97\u7b26( % )\u8fd4\u56de\u6b63\u6570\u76f8\u9664\u7684\u4f59\u6570\uff08 5.3 - \u6c42\u6a21\u548c\u6307\u6570\u8fd0\u7b97 \uff09 \u81ea\u589e\u81ea\u51cf\u64cd\u4f5c\u7b26\u53ef\u4ee5\u5bf9\u4e00\u4e2a\u53d8\u91cf\u8fdb\u884c\u9012\u589e\u548c\u9012\u51cf\u64cd\u4f5c\u3002\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u81ea\u589e\u81ea\u51cf\u64cd\u4f5c\u7b26\u7684\u540e\u7f00\u5f62\u5f0f\uff08 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 \uff09 \u6ce8\u610f\u64cd\u4f5c\u7b26\u7684\u526f\u4f5c\u7528\uff0c\u5c24\u5176\u662f\u5728\u51fd\u6570\u53c2\u6570\u6c42\u503c\u65f6\u3002\u5728\u4e00\u4e2a\u8bed\u53e5\u4e2d\uff0c\u4e0d\u8981\u8ba9\u526f\u4f5c\u7528\u4e24\u6b21\u4f5c\u7528\u4e8e\u4e00\u4e2a\u53d8\u91cf 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 \uff09 \u9017\u53f7\u8fd0\u7b97\u7b26\u53ef\u4ee5\u5c06\u591a\u4e2a\u8bed\u53e5\u5408\u5e76\u4e3a\u4e00\u4e2a\u3002\u4f46\u662f\u591a\u4e2a\u8bed\u53e5\u901a\u5e38\u5206\u5f00\u5199\u4f1a\u66f4\u597d\u3002\uff08 5.5 - \u9017\u53f7\u548c\u6761\u4ef6\u8fd0\u7b97\u7b26 \uff09 \u6761\u4ef6\u8fd0\u7b97\u7b26\u53ef\u4ee5\u6784\u5efa\u4e00\u4e2a \u7cbe\u7b80\u7248 \u7684 if \u8bed\u53e5\uff0c\u4f46\u662f\u5e76\u4e0d\u8981\u628a\u5b83\u5f53\u505a if \u8bed\u53e5\u7684\u4ee3\u66ff\u3002\u53ea\u6709\u5f53\u4f60\u5173\u7cfb\u5b83\u7684\u7ed3\u679c\u65f6\u518d\u4f7f\u7528\uff08 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 \uff09 \u5173\u7cfb\u8fd0\u7b97\u7b26\u53ef\u4ee5\u88ab\u7528\u6765\u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\uff0c\u4f46\u662f\u8981\u5c0f\u5fc3\u5bf9\u6d6e\u70b9\u6570\u5e94\u7528 \u7b49\u4e8e \u548c \u4e0d\u7b49\u4e8e \uff08 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 \uff09 \u4f7f\u7528\u903b\u8f91\u8fd0\u7b97\u7b26\u6211\u4eec\u53ef\u4ee5\u6784\u5efa\u590d\u5408\u6761\u4ef6\u8bed\u53e5\uff08 5.7 - \u903b\u8f91\u8fd0\u7b97\u7b26 \uff09","title":"5.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26"},{"location":"5-x-Chapter-5-summary-and-quiz/#_1","text":"\u4f7f\u7528\u62ec\u53f7\u660e\u786e\u6307\u5b9a\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\uff0c\u907f\u514d\u6b67\u4e49\u3002\uff08 5.1 - \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b \uff09 \u7b97\u6570\u8fd0\u7b97\u7b26\u7684\u884c\u4e3a\u548c\u666e\u901a\u7684\u6570\u5b66\u8fd0\u7b97\u7b26\u662f\u4e00\u6837\u7684\u3002\uff08 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 \uff09\u6c42\u6a21\u8fd0\u7b97\u7b26( % )\u8fd4\u56de\u6b63\u6570\u76f8\u9664\u7684\u4f59\u6570\uff08 5.3 - \u6c42\u6a21\u548c\u6307\u6570\u8fd0\u7b97 \uff09 \u81ea\u589e\u81ea\u51cf\u64cd\u4f5c\u7b26\u53ef\u4ee5\u5bf9\u4e00\u4e2a\u53d8\u91cf\u8fdb\u884c\u9012\u589e\u548c\u9012\u51cf\u64cd\u4f5c\u3002\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u81ea\u589e\u81ea\u51cf\u64cd\u4f5c\u7b26\u7684\u540e\u7f00\u5f62\u5f0f\uff08 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 \uff09 \u6ce8\u610f\u64cd\u4f5c\u7b26\u7684\u526f\u4f5c\u7528\uff0c\u5c24\u5176\u662f\u5728\u51fd\u6570\u53c2\u6570\u6c42\u503c\u65f6\u3002\u5728\u4e00\u4e2a\u8bed\u53e5\u4e2d\uff0c\u4e0d\u8981\u8ba9\u526f\u4f5c\u7528\u4e24\u6b21\u4f5c\u7528\u4e8e\u4e00\u4e2a\u53d8\u91cf 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 \uff09 \u9017\u53f7\u8fd0\u7b97\u7b26\u53ef\u4ee5\u5c06\u591a\u4e2a\u8bed\u53e5\u5408\u5e76\u4e3a\u4e00\u4e2a\u3002\u4f46\u662f\u591a\u4e2a\u8bed\u53e5\u901a\u5e38\u5206\u5f00\u5199\u4f1a\u66f4\u597d\u3002\uff08 5.5 - \u9017\u53f7\u548c\u6761\u4ef6\u8fd0\u7b97\u7b26 \uff09 \u6761\u4ef6\u8fd0\u7b97\u7b26\u53ef\u4ee5\u6784\u5efa\u4e00\u4e2a \u7cbe\u7b80\u7248 \u7684 if \u8bed\u53e5\uff0c\u4f46\u662f\u5e76\u4e0d\u8981\u628a\u5b83\u5f53\u505a if \u8bed\u53e5\u7684\u4ee3\u66ff\u3002\u53ea\u6709\u5f53\u4f60\u5173\u7cfb\u5b83\u7684\u7ed3\u679c\u65f6\u518d\u4f7f\u7528\uff08 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 \uff09 \u5173\u7cfb\u8fd0\u7b97\u7b26\u53ef\u4ee5\u88ab\u7528\u6765\u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\uff0c\u4f46\u662f\u8981\u5c0f\u5fc3\u5bf9\u6d6e\u70b9\u6570\u5e94\u7528 \u7b49\u4e8e \u548c \u4e0d\u7b49\u4e8e \uff08 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 \uff09 \u4f7f\u7528\u903b\u8f91\u8fd0\u7b97\u7b26\u6211\u4eec\u53ef\u4ee5\u6784\u5efa\u590d\u5408\u6761\u4ef6\u8bed\u53e5\uff08 5.7 - \u903b\u8f91\u8fd0\u7b97\u7b26 \uff09","title":"\u7ae0\u8282\u56de\u987e"},{"location":"6-1-Compound-statements-blocks/","tags":["statement"],"text":"Key Takeaway \u51fd\u6570\u7684\u5d4c\u5957\u5c42\u6570\u4e0d\u8981\u8d85\u8fc73\u5c42\u3002\u5982\u679c\u4f60\u7684\u51fd\u6570\u5d4c\u5957\u8fc7\u9ad8\uff0c\u8bf7\u8003\u8651\u5c06\u5176\u5206\u89e3\u4e3a\u591a\u4e2a\u5b50\u51fd\u6570 \u590d\u5408\u8bed\u53e5\uff08\u4e5f\u79f0\u4e3a\u8bed\u5757\uff0c\u53e5\u5757\uff09\u6307\u7684\u662f\u5305\u542b \u96f6\u4e2a \u6216\u591a\u4e2a\u8bed\u53e5\u7684\u7ec4\u5408\u3002\u540c\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c06\u5b83\u4eec\u770b\u505a\u662f\u4e00\u4e2a\u5355\u72ec\u7684\u8bed\u53e5\u3002 \u8bed\u53e5\u5757\u4ee5 { \u5f00\u5934\uff0c\u4ee5 } \u7ed3\u5c3e\uff0c\u9700\u8981\u6267\u884c\u7684\u8bed\u53e5\u653e\u5728\u4e2d\u95f4\u3002\u4efb\u4f55\u80fd\u591f\u4f7f\u7528\u5355\u72ec\u8bed\u53e5\u7684\u5730\u65b9\uff0c\u4e5f\u90fd\u53ef\u4ee5\u4f7f\u7528\u8bed\u53e5\u5757\u3002\u5728\u8bed\u53e5\u5757\u7684\u672b\u5c3e\u4e0d\u9700\u8981\u6dfb\u52a0\u5206\u53f7\u3002 \u5728\u5b66\u4e60\u51fd\u6570\u7684\u65f6\u5019\u6211\u4eec\u5df2\u7ecf\u89c1\u8fc7\u8fd9\u6837\u7684\u7ed3\u6784\u4e86\uff0c\u51fd\u6570\u4f53\u5c31\u662f\u4e00\u4e2a\u5757\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int add ( int x , int y ) { // start block return x + y ; } // end block (no semicolon) int main () { // start block // multiple statements int value {}; // this is initialization, not a block add ( 3 , 4 ); return 0 ; } // end block (no semicolon) \u8bed\u53e5\u5757\u5d4c\u5957 \u5c3d\u7ba1\u51fd\u6570\u4f53\u5185\u4e0d\u80fd\u5d4c\u5165\u5176\u4ed6\u51fd\u6570\uff0c\u4f46\u662f\u8bed\u53e5\u5757\u4e2d\u662f\u53ef\u4ee5\u5d4c\u5165\u5176\u4ed6\u8bed\u53e5\u5757\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 int add ( int x , int y ) { // block return x + y ; } // end block int main () { // outer block // multiple statements int value {}; { // inner/nested block add ( 3 , 4 ); } // end inner/nested block return 0 ; } // end outer block \u5f53\u8bed\u53e5\u5757\u5d4c\u5957\u4e4b\u540e\uff0c\u5305\u542b\u5176\u4ed6\u8bed\u53e5\u5757\u7684\u8bed\u53e5\u5757\u79f0\u4e3a \u5916\u5c42\u8bed\u53e5\u5757 \uff0c\u88ab\u5305\u542b\u7684\u8bed\u53e5\u5757\u79f0\u4e3a \u5185\u5c42\u8bed\u53e5\u5757 \u6216\u8005\u5d4c\u5957\u8bed\u53e5\u5757\u3002 \u4f7f\u7528\u8bed\u53e5\u5757\u6309\u6761\u4ef6\u6267\u884c\u591a\u6761\u8bed\u53e5 \u8bed\u53e5\u5757\u6700\u5e38\u7528\u7684\u573a\u666f\u662f\u914d\u5408 if \u8bed\u53e5\u6765\u4f7f\u7528\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cif \u8bed\u53e5\u5f53\u6761\u4ef6\u6c42\u503c\u4e3a\u771f\u65f6\uff0c\u4f1a\u6267\u884c\u4e00\u6761\u8bed\u53e5\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8bed\u53e5\u5757\u6765\u66ff\u6362\u5355\u56fe\u8bed\u53e5\uff0c\u8fd9\u6837 if \u5c31\u80fd\u591f\u5728\u6c42\u503c\u4e3a\u771f\u65f6\uff0c\u6267\u884c\u591a\u6761\u8bed\u53e5\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int main () { // start of outer block std :: cout << \"Enter an integer: \" ; int value {}; std :: cin >> value ; if ( value >= 0 ) { // start of nested block std :: cout << value << \" is a positive integer (or zero) \\n \" ; std :: cout << \"Double this number is \" << value * 2 << '\\n' ; } // end of nested block else { // start of another nested block std :: cout << value << \" is a negative integer \\n \" ; std :: cout << \"The positive of this number is \" << - value << '\\n' ; } // end of another nested block return 0 ; } // end of outer block \u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u662f 3\uff1a 1 2 3 Enter an integer: 3 3 is a positive integer (or zero) Double this number is 6 \u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u662f 4\uff1a 1 2 3 Enter an integer: -4 -4 is a negative integer The positive of this number is 4 \u8bed\u53e5\u5757\u5d4c\u5957\u7684\u5c42\u6570 \u5728\u8bed\u53e5\u5757\u4e2d\u5d4c\u5957\u8bed\u53e5\u5757\uff0c\u518d\u5d4c\u5957\u8bed\u53e5\u5757\u4e5f\u662f\u53ef\u4ee5\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 int main () { // block 1, nesting level 1 std :: cout << \"Enter an integer: \" ; int value {}; std :: cin >> value ; if ( value > 0 ) { // block 2, nesting level 2 if (( value % 2 ) == 0 ) { // block 3, nesting level 3 std :: cout << value << \" is positive and even \\n \" ; } else { // block 4, also nesting level 3 std :: cout << value << \" is positive and odd \\n \" ; } } return 0 ; } \u51fd\u6570\u7684\u5d4c\u5957\u5c42\u6570 (\u4e5f\u79f0\u4e3a\u5d4c\u5957\u6df1\u5ea6) \u6307\u7684\u662f\u51fd\u6570\u4e2d\u5d4c\u5957\u8bed\u53e5\u5757\u7684\u6700\u5927\u503c\uff08\u5305\u62ec\u6700\u5916\u5c42\u8bed\u53e5\u5757\uff09\u3002\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u4e00\u5171\u67094\u4e2a\u8bed\u53e5\u5757\uff0c\u4f46\u662f\u5d4c\u5957\u5c42\u6570\u4e3a 3\uff0c\u56e0\u4e3a\u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\u4efb\u610f\u4f4d\u7f6e\u4f60\u4e0d\u53ef\u80fd\u5904\u5728\u5927\u4e8e3\u5c42\u7684\u5d4c\u5957\u4e2d\u3002 C++ \u6807\u51c6\u5199\u9053\uff0cC++\u7f16\u8bd1\u5668\u9700\u8981\u652f\u6301 256 \u5c42\u5d4c\u5957\u2014\u2014\u4f46\u662f\u5e76\u4e0d\u662f\u6240\u6709\u7f16\u8bd1\u5668\u90fd\u8fd9\u4e48\u505a\u4e86\uff08\u4f8b\u5982\uff0c\u622a\u6b62\u5230\u672c\u6587\u64b0\u5199\u65f6\uff0cVisual Studio \u652f\u6301\u7684\u5d4c\u5957\u5c42\u6570\u5728100\u5230110\u4e4b\u95f4\uff09\u3002 \u4e0d\u7ba1 C++ \u5728\u6280\u672f\u4e0a\u652f\u6301\u591a\u5c11\u5c42\u5d4c\u5957\uff0c\u5728\u6211\u4eec\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u5d4c\u5957\u5c42\u6570\u6700\u597d\u4e0d\u8981\u8d85\u8fc73\u3002\u5c31\u50cf\u7279\u522b\u957f\u7684\u51fd\u6570\u5e94\u8be5\u91cd\u6784\uff08\u91cd\u5199\u4e3a\u66f4\u5c0f\u7684\u51fd\u6570\uff09\u4e00\u6837\uff0c\u8fc7\u5ea6\u5d4c\u5957\u7684\u51fd\u6570\u96be\u4ee5\u9605\u8bfb\uff0c\u5d4c\u5957\u6700\u4e25\u91cd\u7684\u8bed\u53e5\u5757\u4e5f\u5e94\u8be5\u88ab\u91cd\u6784\u4e3a\u5355\u72ec\u7684\u51fd\u6570\u3002 \u6700\u4f73\u5b9e\u8df5 \u51fd\u6570\u7684\u5d4c\u5957\u5c42\u6570\u4e0d\u8981\u8d85\u8fc73\u5c42\u3002\u5982\u679c\u4f60\u7684\u51fd\u6570\u5d4c\u5957\u8fc7\u9ad8\uff0c\u8bf7\u8003\u8651\u5c06\u5176\u5206\u89e3\u4e3a\u591a\u4e2a\u5b50\u51fd\u6570","title":"6.1 - \u590d\u5408\u8bed\u53e5\uff08\u8bed\u53e5\u5757\uff09"},{"location":"6-1-Compound-statements-blocks/#_1","text":"\u5c3d\u7ba1\u51fd\u6570\u4f53\u5185\u4e0d\u80fd\u5d4c\u5165\u5176\u4ed6\u51fd\u6570\uff0c\u4f46\u662f\u8bed\u53e5\u5757\u4e2d\u662f\u53ef\u4ee5\u5d4c\u5165\u5176\u4ed6\u8bed\u53e5\u5757\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 int add ( int x , int y ) { // block return x + y ; } // end block int main () { // outer block // multiple statements int value {}; { // inner/nested block add ( 3 , 4 ); } // end inner/nested block return 0 ; } // end outer block \u5f53\u8bed\u53e5\u5757\u5d4c\u5957\u4e4b\u540e\uff0c\u5305\u542b\u5176\u4ed6\u8bed\u53e5\u5757\u7684\u8bed\u53e5\u5757\u79f0\u4e3a \u5916\u5c42\u8bed\u53e5\u5757 \uff0c\u88ab\u5305\u542b\u7684\u8bed\u53e5\u5757\u79f0\u4e3a \u5185\u5c42\u8bed\u53e5\u5757 \u6216\u8005\u5d4c\u5957\u8bed\u53e5\u5757\u3002","title":"\u8bed\u53e5\u5757\u5d4c\u5957"},{"location":"6-1-Compound-statements-blocks/#_2","text":"\u8bed\u53e5\u5757\u6700\u5e38\u7528\u7684\u573a\u666f\u662f\u914d\u5408 if \u8bed\u53e5\u6765\u4f7f\u7528\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cif \u8bed\u53e5\u5f53\u6761\u4ef6\u6c42\u503c\u4e3a\u771f\u65f6\uff0c\u4f1a\u6267\u884c\u4e00\u6761\u8bed\u53e5\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8bed\u53e5\u5757\u6765\u66ff\u6362\u5355\u56fe\u8bed\u53e5\uff0c\u8fd9\u6837 if \u5c31\u80fd\u591f\u5728\u6c42\u503c\u4e3a\u771f\u65f6\uff0c\u6267\u884c\u591a\u6761\u8bed\u53e5\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int main () { // start of outer block std :: cout << \"Enter an integer: \" ; int value {}; std :: cin >> value ; if ( value >= 0 ) { // start of nested block std :: cout << value << \" is a positive integer (or zero) \\n \" ; std :: cout << \"Double this number is \" << value * 2 << '\\n' ; } // end of nested block else { // start of another nested block std :: cout << value << \" is a negative integer \\n \" ; std :: cout << \"The positive of this number is \" << - value << '\\n' ; } // end of another nested block return 0 ; } // end of outer block \u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u662f 3\uff1a 1 2 3 Enter an integer: 3 3 is a positive integer (or zero) Double this number is 6 \u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u662f 4\uff1a 1 2 3 Enter an integer: -4 -4 is a negative integer The positive of this number is 4","title":"\u4f7f\u7528\u8bed\u53e5\u5757\u6309\u6761\u4ef6\u6267\u884c\u591a\u6761\u8bed\u53e5"},{"location":"6-1-Compound-statements-blocks/#_3","text":"\u5728\u8bed\u53e5\u5757\u4e2d\u5d4c\u5957\u8bed\u53e5\u5757\uff0c\u518d\u5d4c\u5957\u8bed\u53e5\u5757\u4e5f\u662f\u53ef\u4ee5\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 int main () { // block 1, nesting level 1 std :: cout << \"Enter an integer: \" ; int value {}; std :: cin >> value ; if ( value > 0 ) { // block 2, nesting level 2 if (( value % 2 ) == 0 ) { // block 3, nesting level 3 std :: cout << value << \" is positive and even \\n \" ; } else { // block 4, also nesting level 3 std :: cout << value << \" is positive and odd \\n \" ; } } return 0 ; } \u51fd\u6570\u7684\u5d4c\u5957\u5c42\u6570 (\u4e5f\u79f0\u4e3a\u5d4c\u5957\u6df1\u5ea6) \u6307\u7684\u662f\u51fd\u6570\u4e2d\u5d4c\u5957\u8bed\u53e5\u5757\u7684\u6700\u5927\u503c\uff08\u5305\u62ec\u6700\u5916\u5c42\u8bed\u53e5\u5757\uff09\u3002\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u4e00\u5171\u67094\u4e2a\u8bed\u53e5\u5757\uff0c\u4f46\u662f\u5d4c\u5957\u5c42\u6570\u4e3a 3\uff0c\u56e0\u4e3a\u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\u4efb\u610f\u4f4d\u7f6e\u4f60\u4e0d\u53ef\u80fd\u5904\u5728\u5927\u4e8e3\u5c42\u7684\u5d4c\u5957\u4e2d\u3002 C++ \u6807\u51c6\u5199\u9053\uff0cC++\u7f16\u8bd1\u5668\u9700\u8981\u652f\u6301 256 \u5c42\u5d4c\u5957\u2014\u2014\u4f46\u662f\u5e76\u4e0d\u662f\u6240\u6709\u7f16\u8bd1\u5668\u90fd\u8fd9\u4e48\u505a\u4e86\uff08\u4f8b\u5982\uff0c\u622a\u6b62\u5230\u672c\u6587\u64b0\u5199\u65f6\uff0cVisual Studio \u652f\u6301\u7684\u5d4c\u5957\u5c42\u6570\u5728100\u5230110\u4e4b\u95f4\uff09\u3002 \u4e0d\u7ba1 C++ \u5728\u6280\u672f\u4e0a\u652f\u6301\u591a\u5c11\u5c42\u5d4c\u5957\uff0c\u5728\u6211\u4eec\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u5d4c\u5957\u5c42\u6570\u6700\u597d\u4e0d\u8981\u8d85\u8fc73\u3002\u5c31\u50cf\u7279\u522b\u957f\u7684\u51fd\u6570\u5e94\u8be5\u91cd\u6784\uff08\u91cd\u5199\u4e3a\u66f4\u5c0f\u7684\u51fd\u6570\uff09\u4e00\u6837\uff0c\u8fc7\u5ea6\u5d4c\u5957\u7684\u51fd\u6570\u96be\u4ee5\u9605\u8bfb\uff0c\u5d4c\u5957\u6700\u4e25\u91cd\u7684\u8bed\u53e5\u5757\u4e5f\u5e94\u8be5\u88ab\u91cd\u6784\u4e3a\u5355\u72ec\u7684\u51fd\u6570\u3002 \u6700\u4f73\u5b9e\u8df5 \u51fd\u6570\u7684\u5d4c\u5957\u5c42\u6570\u4e0d\u8981\u8d85\u8fc73\u5c42\u3002\u5982\u679c\u4f60\u7684\u51fd\u6570\u5d4c\u5957\u8fc7\u9ad8\uff0c\u8bf7\u8003\u8651\u5c06\u5176\u5206\u89e3\u4e3a\u591a\u4e2a\u5b50\u51fd\u6570","title":"\u8bed\u53e5\u5757\u5d4c\u5957\u7684\u5c42\u6570"},{"location":"6-10-Static-local-variables/","tags":["static","local variable"],"text":"Key Takeaway \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\uff0c\u4f1a\u5728\u53d8\u91cf\u5b9a\u4e49\u65f6\u521b\u5efa\u53d8\u91cf\uff0c\u9000\u51fa\u8bed\u53e5\u5757\u65f6\u9500\u6bc1\u53d8\u91cf \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\uff0c\u4f1a\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u521b\u5efa\u53d8\u91cf\uff0c\u5e76\u4e14\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u9500\u6bc1\u3002 \u5177\u6709\u521d\u503c0\u6216\u8005constexpr\u7c7b\u578b\u521d\u59cb\u5316\u503c\u7684\u9759\u6001\u53d8\u91cf\uff0c\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u5c31\u4f1a\u88ab\u521d\u59cb\u5316\u3002\u800c\u5177\u6709\u975econstexpr\u7c7b\u578b\u521d\u59cb\u5316\u503c\u7684\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u5219\u4f1a\u5728\u7b2c\u4e00\u6b21\u88ab\u5b9a\u4e49\u65f6\u8fdb\u884c\u521d\u59cb\u5316(\u5728\u540e\u7eed\u7684\u51fd\u6570\u8c03\u7528\u4e2d\uff0c\u8be5\u53d8\u91cf\u4e0d\u4f1a\u88ab\u91cd\u65b0\u521d\u59cb\u5316\uff09\u3002 \u9759\u6001\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u4e00\u6837\u90fd\u53ef\u4ee5\u6301\u7eed\u5230\u7a0b\u5e8f\u7ed3\u675f\u524d\uff0c\u4f46\u662f\u5b83\u7684\u53ef\u89c1\u8303\u56f4\u66f4\u5c0f\uff0c\u4e5f\u5c31\u66f4\u5b89\u5168\u3002 \u5bf9\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u3002\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u53ea\u4f1a\u5728\u7b2c\u4e00\u6b21\u6267\u884c\u65f6\u88ab\u521d\u59cb\u5316\uff0c\u540e\u9762\u4e0d\u4f1a\u5bf9\u5176\u8fdb\u884c\u91cd\u590d\u5730\u521d\u59cb\u5316\u3002 \u5f53\u53d8\u91cf\u521b\u5efa\u6216\u8005\u521d\u59cb\u5316\u5f00\u9500\u5f88\u5927\u65f6\uff0c\u53ef\u4ee5\u5c06\u5176\u5b9a\u4e49\u4e3a\u9759\u6001const\u5c40\u90e8\u53d8\u91cf \u9664\u975e\u53d8\u91cf\u6c38\u8fdc\u4e0d\u9700\u8981\u88ab\u91cd\u7f6e\uff0c\u5426\u5219\u8981\u907f\u514d\u4f7f\u7528\u9759\u6001\u5c40\u90e8\u53d8\u91cf static \u662f C++ \u4e2d\u6700\u4ee4\u4eba\u56f0\u60d1\u7684\u672f\u8bed\u4e4b\u4e00\uff0c\u8fd9\u4e3b\u8981\u662f\u56e0\u4e3a static \u5728\u4e0d\u540c\u8bed\u5883\u4e0b\u7684\u542b\u4e49\u4e5f\u662f\u4e0d\u540c\u7684\u3002 \u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff08 6.4 - \u5168\u5c40\u53d8\u91cf \uff09\uff0c\u6211\u4eec\u4ecb\u7ecd\u8fc7\u5168\u5c40\u53d8\u91cf\u5177\u6709 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u4e5f\u5c31\u662f\u8bf4\u5168\u5c40\u53d8\u91cf\u4f1a\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u88ab\u521b\u5efa\uff0c\u5e76\u4e14\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u3002 \u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86 static \u5173\u952e\u5b57\u53ef\u4ee5\u4f7f\u5168\u5c40\u53d8\u91cf\u5177\u6709 \u5185\u90e8\u94fe\u63a5 \u5c5e\u6027\uff0c\u4e5f\u5c31\u662f\u8be5\u5168\u5c40\u53d8\u91cf\u53ea\u80fd\u5728\u5b9a\u4e49\u5b83\u7684\u6587\u4ef6\u4e2d\u4f7f\u7528\u3002 \u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba static \u5bf9\u5c40\u90e8\u53d8\u91cf\u7684\u5f71\u54cd\u3002 \u9759\u6001\u5c40\u90e8\u53d8\u91cf \u5728 2.5 - \u5c40\u90e8\u4f5c\u7528\u57df \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u8fc7\u5c40\u90e8\u53d8\u91cf\u9ed8\u8ba4\u5177\u6709 \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4f1a\u5728\u5b9a\u4e49\u65f6\u88ab\u521b\u5efa\uff0c\u5728\u8bed\u53e5\u5757\u9000\u51fa\u65f6\u88ab\u9500\u6bc1\u3002 \u5bf9\u5c40\u90e8\u53d8\u91cf\u4f7f\u7528 static \u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u5c06\u5c40\u90e8\u53d8\u91cf\u7684 \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u4ece \u81ea\u52a8 \u4fee\u6539\u4e3a \u9759\u6001 \u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8fd9\u79cd\u5c40\u90e8\u53d8\u91cf\u4f1a\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u88ab\u521b\u5efa\uff0c\u7136\u540e\u5728\u7a0b\u5e8f\u7ed3\u675f\u540e\u88ab\u9500\u6bc1\uff08\u5c31\u50cf\u5168\u5c40\u53d8\u91cf\u4e00\u6837\uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u9759\u6001\u53d8\u91cf\u53ef\u4ee5\u5728\u79bb\u5f00\u4f5c\u7528\u57df\u540e\u7ee7\u7eed\u4fdd\u6301\u5b83\u7684\u503c\u3002 \u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u5c31\u53ef\u4ee5\u5f88\u597d\u7684\u5c55\u793a \u81ea\u52a8\u6301\u7eed\u65f6\u95f4 \u548c \u9759\u6001\u6301\u7eed\u65f6\u95f4 \u7684\u4e0d\u540c\u3002 \u81ea\u52a8\u6301\u7eed\u65f6\u95f4 \uff08\u9ed8\u8ba4\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void incrementAndPrint () { int value { 1 }; // automatic duration by default ++ value ; std :: cout << value << '\\n' ; } // value is destroyed here int main () { incrementAndPrint (); incrementAndPrint (); incrementAndPrint (); return 0 ; } \u6bcf\u6b21\u8c03\u7528 incrementAndPrint() \u7684\u65f6\u5019\uff0c\u53d8\u91cf value \u90fd\u4f1a\u88ab\u521b\u5efa\u5e76\u4e14\u8d4b\u503c\u4e3a 1\u3002 incrementAndPrint() \u4f1a\u628a value \u9012\u589e\u4e3a 2\uff0c\u7136\u540e\u5c06 2 \u6253\u5370\u51fa\u6765\uff0c\u5f53 incrementAndPrint() \u51fd\u6570\u9000\u51fa\u540e\uff0c\u53d8\u91cf\u5c31\u4f1a\u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1\u3002\u6240\u4ee5\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 2 2 2 \u63a5\u4e0b\u6765\uff0c\u8003\u8651 static \u7248\u672c\u7684\u7a0b\u5e8f\u3002\u8fd9\u4e24\u4e2a\u7248\u672c\u7a0b\u5e8f\u7684\u552f\u4e00\u533a\u522b\u5c31\u662f\u63a5\u4e0b\u6765\u6211\u4eec\u4f7f\u7528 static \u5173\u952e\u5b57\u5c06\u81ea\u52a8\u6301\u7eed\u65f6\u95f4\u4fee\u6539\u4e3a\u9759\u6001\u6301\u7eed\u65f6\u95f4\u3002 \u9759\u6001\u6301\u7eed\u65f6\u95f4 \uff08\u4f7f\u7528 static \u5173\u952e\u5b57\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void incrementAndPrint () { static int s_value { 1 }; // static duration via static keyword. This initializer is only executed once. ++ s_value ; std :: cout << s_value << '\\n' ; } // s_value is not destroyed here, but becomes inaccessible because it goes out of scope int main () { incrementAndPrint (); incrementAndPrint (); incrementAndPrint (); return 0 ; } \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u56e0\u4e3a s_value \u88ab\u5b9a\u4e49\u4e3a static \u7c7b\u578b\uff0c\u56e0\u6b64\u5b83\u4f1a\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u88ab\u521b\u5efa\u3002 \u5177\u6709\u521d\u503c0\u6216\u8005constexpr\u7c7b\u578b\u521d\u59cb\u5316\u503c\u7684\u9759\u6001\u53d8\u91cf\uff0c\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u5c31\u4f1a\u88ab\u521d\u59cb\u5316\u3002\u800c\u5177\u6709\u975econstexpr\u7c7b\u578b\u521d\u59cb\u5316\u503c\u7684\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u5219\u4f1a\u5728\u7b2c\u4e00\u6b21\u88ab\u5b9a\u4e49\u65f6\u8fdb\u884c\u521d\u59cb\u5316(\u5728\u540e\u7eed\u7684\u51fd\u6570\u8c03\u7528\u4e2d\uff0c\u8be5\u53d8\u91cf\u4e0d\u4f1a\u88ab\u91cd\u65b0\u521d\u59cb\u5316\uff09\u3002\u56e0\u4e3a s_value \u5177\u6709 constexpr \u521d\u59cb\u5316\u503c 1 \uff0c\u6240\u4ee5\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6 s_value \u5c31\u4f1a\u88ab\u521d\u59cb\u5316\u3002 \u5f53\u51fd\u6570\u7ed3\u675f\u65f6\uff0c s_value \u4f1a \u8d85\u51fa\u4f5c\u7528\u57df \uff0c\u4f46\u662f\u5b83\u4e0d\u4f1a\u88ab\u9500\u6bc1\u3002\u6bcf\u6b21 incrementAndPrint() \u88ab\u8c03\u7528\u65f6\uff0c s_value \u7684\u503c\u4ecd\u7136\u4e3a\u4e4b\u524d\u7684\u503c\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u7a0b\u5e8f\u7684\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 2 3 4 \u7c7b\u4f3c \u201cg_\u201d \u901a\u5e38\u4f5c\u4e3a\u5168\u5c40\u53d8\u91cf\u7684\u524d\u7f00\uff0c\u201cs_\u201d \u5e38\u88ab\u7528\u6765\u4f5c\u4e3a\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u7684\u524d\u7f00\u3002 \u9759\u6001\u5c40\u90e8\u53d8\u91cf\u6700\u5e38\u89c1\u7684\u7528\u6cd5\u662f\u6700\u4e3a\u4e00\u4e2a\u552f\u4e00ID\u751f\u6210\u5668\u3002\u60f3\u8c61\u4e00\u4e0b\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\u5305\u542b\u4e86\u5f88\u591a\u7c7b\u4f3c\u7684\u5bf9\u8c61\uff08\u4f8b\u5982\uff0c\u5728\u67d0\u4e2a\u6e38\u620f\u4e2d\u4f60\u88ab\u5f88\u591a\u50f5\u5c38\u653b\u51fb\uff0c\u6216\u8005\u4f60\u5e0c\u671b\u6a21\u62df\u5f88\u591a\u4e09\u89d2\u5f62\uff09\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f60\u51e0\u4e4e\u4e0d\u53ef\u80fd\u5b9a\u4f4d\u662f\u54ea\u4e2a\u5bf9\u8c61\u51fa\u95ee\u9898\u4e86\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u521b\u5efa\u4e00\u4e2a\u552f\u4e00\u7684\u6807\u8bc6\u7b26\uff0c\u5219\u53ef\u4ee5\u5728\u65e5\u540edebug\u65f6\u8f7b\u677e\u5730\u5c06\u5b83\u4eec\u533a\u522b\u5f00\u6765\u3002 \u4f7f\u7528\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u53ef\u4ee5\u5f88\u8f7b\u677e\u5730\u521b\u5efa\u552f\u4e00ID\uff1a 1 2 3 4 5 int generateID () { static int s_itemID { 0 }; return s_itemID ++ ; // makes copy of s_itemID, increments the real s_itemID, then returns the value in the copy } \u5f53\u51fd\u6570\u7b2c\u4e00\u6b21\u88ab\u8c03\u7528\u65f6\uff0c\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u88ab\u521d\u59cb\u5316\u4e3a0\u3002\u7b2c\u4e8c\u6b21\u8c03\u7528\u65f6\uff0c\u8be5\u53d8\u91cf\u9012\u589e\u4e3a\u4e86 1\u3002\u6bcf\u6b21\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u7684\u503c\u90fd\u4f1a\u6bd4\u4e4b\u524d\u4e00\u6b21\u8c03\u7528\u59271\u3002\u4f60\u53ef\u4ee5\u628a\u8fd9\u4e9b\u503c\u4f5c\u4e3a\u552f\u4e00ID\u8d4b\u503c\u7ed9\u5bf9\u8c61\u3002\u56e0\u4e3a s_itemID \u662f\u5c40\u90e8\u53d8\u91cf\uff0c\u6240\u4ee5\u5b83\u5e76\u4e0d\u4f1a\u88ab\u5176\u4ed6\u51fd\u6570\u201c\u7be1\u6539\u201d\u3002 \u9759\u6001\u53d8\u91cf\u76f8\u5bf9\u4e8e\u5168\u5c40\u53d8\u91cf\u5177\u6709\u4e00\u4e9b\u4f18\u52bf\uff08\u5b83\u4eec\u90fd\u4e0d\u4f1a\u5728\u7a0b\u5e8f\u7ed3\u675f\u524d\u88ab\u9500\u6bc1\uff09\uff0c\u56e0\u4e3a\u5b83\u53ea\u5728\u5b9a\u4e49\u5b83\u7684\u5757\u4e2d\u53ef\u89c1\u3002\u8fd9\u6837\u4e00\u6765\u5b83\u5c31\u4f1a\u66f4\u52a0\u5b89\u5168\uff08\u5f53\u4f60\u9700\u8981\u9891\u7e41\u4fee\u6539\u5b83\u7684\u503c\u7684\u65f6\u5019\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u3002\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u53ea\u4f1a\u5728\u7b2c\u4e00\u6b21\u6267\u884c\u65f6\u88ab\u521d\u59cb\u5316\uff0c\u540e\u9762\u4e0d\u4f1a\u5bf9\u5176\u8fdb\u884c\u91cd\u590d\u5730\u521d\u59cb\u5316\u3002 \u9759\u6001\u5c40\u90e8\u5e38\u91cf \u9759\u6001\u5c40\u90e8\u53d8\u91cf\u4e5f\u53ef\u4ee5\u88ab\u5b9a\u4e49\u4e3a\u5e38\u91cf\u3002\u9759\u6001\u5c40\u90e8\u5e38\u91cf\u7684\u5178\u578b\u4f7f\u7528\u573a\u666f\u662f\u5f53\u4f60\u7684\u51fd\u6570\u9700\u8981\u4e00\u4e2a\u5e38\u91cf\u503c\uff0c\u4f46\u662f\u521b\u5efa\u6216\u521d\u59cb\u5316\u8be5\u5e38\u91cf\u7684\u5f00\u9500\u975e\u5e38\u5927\uff08\u4f8b\u5982\u8be5\u503c\u662f\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u7684\uff09\u3002\u5982\u679c\u4f60\u4f7f\u7528\u4e00\u4e2a\u666e\u901a\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u90a3\u4e48\u8fd9\u4e2a\u53d8\u91cf\u5c31\u4f1a\u5728\u6bcf\u6b21\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u521d\u59cb\u5316\u3002\u800c\u4f7f\u7528const\u7c7b\u578b\u7684\u9759\u6001\u5c40\u90e8\u53d8\u91cf\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728\u7b2c\u4e00\u6b21\u8c03\u7528\u51fd\u6570\u65f6\u521d\u59cb\u5316\u5b83\uff0c\u7136\u540e\u5728\u540e\u7eed\u7684\u8c03\u7528\u4e2d\u5bf9\u5176\u8fdb\u884c\u91cd\u7528\u3002 \u4e0d\u8981\u4f7f\u7528\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u6765\u6539\u53d8\u6d41\u7a0b \u8003\u8651\u5982\u4e0b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include int getInteger () { static bool s_isFirstCall { true }; if ( s_isFirstCall ) { std :: cout << \"Enter an integer: \" ; s_isFirstCall = false ; } else { std :: cout << \"Enter another integer: \" ; } int i {}; std :: cin >> i ; return i ; } int main () { int a { getInteger () }; int b { getInteger () }; std :: cout << a << \" + \" << b << \" = \" << ( a + b ) << '\\n' ; return 0 ; } \u8f93\u51fa\u5982\u4e0b\uff1a 1 2 3 Enter an integer: 5 Enter another integer: 9 5 + 9 = 14 \u8fd9\u6bb5\u4ee3\u7801\u7684\u786e\u80fd\u591f\u6309\u7167\u9884\u671f\u5de5\u4f5c\uff0c\u4f46\u662f\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528\u7684\u662f\u4e00\u4e2a\u9759\u6001\u5c40\u90e8\u53d8\u91cf\uff0c\u8fd9\u4f1a\u4f7f\u5f97\u7a0b\u5e8f\u96be\u4ee5\u7406\u89e3\u3002\u5982\u679c\u6709\u4eba\u5728\u4e0d\u77e5\u9053 getInteger() \u51fd\u6570\u7684\u5b9e\u73b0\u7ec6\u8282\u65f6\uff0c\u9605\u8bfb main \u51fd\u6570\u7684\u4ee3\u7801\uff0c\u5b83\u5b8c\u5168\u4e0d\u4f1a\u8ba4\u4e3a\u4e24\u6b21\u8c03\u7528 getInteger() \u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u6548\u679c\u3002\u4f46\u662f\u4e24\u6b21\u8c03\u7528\u8be5\u51fd\u6570\u7684\u786e\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u8f93\u51fa\uff0c\u8fd9\u5c31\u597d\u8ba9\u4eba\u611f\u5230\u975e\u5e38\u56f0\u60d1\uff0c 1 \u5047\u8bbe\u4f60\u7684\u5fae\u6ce2\u7089\u4e0a\u6709\u4e00\u4e2a+1\u6309\u94ae\uff0c\u5f53\u4f60\u6309\u4e0b\u5b83\u7684\u65f6\u5019\u5c31\u4f1a\u5ef6\u957f\u4e00\u5206\u949f\u52a0\u70ed\u65f6\u95f4\u3002\u4f60\u51c6\u5907\u52a0\u70ed\u4e00\u4e9b\u5403\u7684\uff0c\u8bbe\u5b9a\u597d\u65f6\u95f4\uff0c\u7136\u540e\u7b49\u7740\u51fa\u9505\u3002\u8fd9\u671f\u95f4\u4f60\u53ef\u4ee5\u770b\u770b\u7a97\u5916\u7684\u732b\u54aa\uff0c\u6d88\u78e8\u6d88\u78e8\u65f6\u95f4\u3002\u7136\u540e\u5fae\u6ce2\u7089\u54cd\u4e86\uff0c\u4f60\u628a\u996d\u62ff\u51fa\u6765\u957f\u4e86\u4e00\u53e3\uff0c\u53d1\u73b0\u8fd8\u6709\u70b9\u51c9\u3002\u6ca1\u5173\u7cfb\uff0c\u4f60\u53c8\u628a\u996d\u653e\u4e86\u56de\u53bb\u7136\u540e\u6309\u4e86\u4e00\u4e0b+1\u6309\u94ae\uff0c\u51c6\u5907\u518d\u70ed\u4e00\u5206\u949f\u3002\u53ef\u662f\u8fd9\u4e00\u6b21\uff0c\u5fae\u6ce2\u7089\u53ea\u52a0\u70ed\u4e86\u4e00\u79d2\u949f\uff0c\u800c\u4e0d\u662f\u4e00\u5206\u949f\u3002\u8fd9\u79cd\u60c5\u51b5\u5c31\u50cf\u6211\u4eec\u603b\u8bf4\u7684\u90a3\u6837\u201c\u6211\u4eec\u4ec0\u4e48\u4e5f\u6ca1\u6539\uff0c\u7a0b\u5e8f\u5c31\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u4e86\u201d\u6216\u662f\u201c\u4e0a\u6b21\u7528\u8fd8\u597d\u597d\u7684\u5462\u201d\u3002\u6211\u4eec\u603b\u662f\u671f\u671b\uff0c\u5f53\u91cd\u590d\u4e00\u4e2a\u52a8\u4f5c\u65f6\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u4e5f\u5e94\u8be5\u548c\u4e4b\u524d\u662f\u4e00\u6837\u7684\uff0c\u540c\u6837\u7684\u9053\u7406\u4e5f\u9002\u7528\u4e8e\u51fd\u6570\u3002 \u5047\u8bbe\u6211\u4eec\u60f3\u8981\u4e3a\u8ba1\u7b97\u5668\u6dfb\u52a0\u51cf\u6cd5\u529f\u80fd\uff0c\u671f\u671b\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 Addition Enter an integer: 5 Enter another integer: 9 5 + 9 = 14 Subtraction Enter an integer: 12 Enter another integer: 3 12 - 3 = 9 \u6b64\u65f6\u4f60\u53ef\u80fd\u4f1a\u8003\u8651\u4f7f\u7528 getInteger() \u518d\u8bfb\u53d6\u4e24\u4e2a\u6574\u6570\uff0c\u5c31\u50cf\u505a\u52a0\u6cd5\u65f6\u4e00\u6837\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 int main () { std :: cout << \"Addition \\n \" ; int a { getInteger () }; int b { getInteger () }; std :: cout << a << \" + \" << b << \" = \" << ( a + b ) << '\\n' ; std :: cout << \"Subtraction \\n \" ; int c { getInteger () }; int d { getInteger () }; std :: cout << c << \" - \" << d << \" = \" << ( c - d ) << '\\n' ; return 0 ; } \u4f46\u662f\uff0c\u8fd9\u6837\u5e76\u6ca1\u6709\u7528\uff0c\u8f93\u51fa\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 Addition Enter an integer: 5 Enter another integer: 9 5 + 9 = 14 Subtraction Enter another integer: 12 Enter another integer: 3 12 - 3 = 9 (\u8f93\u51fa\u7684\u8fd8\u662f \u201cEnter another integer\u201d \u800c\u4e0d\u662f \u201cEnter an integer\u201d) getInteger() \u662f\u4e0d\u53ef\u91cd\u7528\u7684\uff0c\u56e0\u5b83\u5177\u6709\u67d0\u79cd\u5185\u90e8\u72b6\u6001\uff08\u9759\u6001\u5c40\u90e8\u53d8\u91cf s_isFirstCall \uff09\uff0c\u800c\u8be5\u72b6\u6001\u5e76\u4e0d\u80fd\u591f\u4ece\u5916\u90e8\u88ab\u91cd\u7f6e\u3002 s_isFirstCall \u5e76\u4e0d\u662f\u4e00\u4e2a\u9700\u8981\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u4fdd\u6301\u552f\u4e00\u7684\u53d8\u91cf\uff0c\u5c3d\u7ba1\u4e0a\u9762\u7b2c\u4e00\u4e2a\u7a0b\u5e8f\u80fd\u591f\u6b63\u786e\u8fd0\u884c\uff0c\u4f46\u662f\u9759\u6001\u53d8\u91cf\u963b\u788d\u4e86\u6211\u4eec\u5bf9\u51fd\u6570\u7684\u91cd\u7528\u3002 \u5b9e\u73b0 getInteger \u7684\u66f4\u597d\u7684\u65b9\u6cd5\u662f\u5c06 s_isFirstCall \u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u3002\u8fd9\u6837\u4e3b\u8c03\u51fd\u6570\u5c31\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u6253\u5370\u7684\u5185\u5bb9\u3002 \u9759\u6001\u5c40\u90e8\u53d8\u91cf\u9002\u7528\u4e8e\u8be5\u53d8\u91cf\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\uff08\u6216\u53ef\u9884\u89c1\u7684\u672a\u6765\uff09\u9700\u8981\u4fdd\u6301\u552f\u4e00\u6027\u4e14\u65e0\u9700\u5bf9\u5176\u8fdb\u884c\u91cd\u7f6e\u7684\u60c5\u51b5\u3002 \u6700\u4f73\u5b9e\u8df5 \u9664\u975e\u53d8\u91cf\u6c38\u8fdc\u4e0d\u9700\u8981\u88ab\u91cd\u7f6e\uff0c\u5426\u5219\u8981\u907f\u514d\u4f7f\u7528\u9759\u6001\u5c40\u90e8\u53d8\u91cf","title":"6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf"},{"location":"6-10-Static-local-variables/#_1","text":"\u5728 2.5 - \u5c40\u90e8\u4f5c\u7528\u57df \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u8fc7\u5c40\u90e8\u53d8\u91cf\u9ed8\u8ba4\u5177\u6709 \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4f1a\u5728\u5b9a\u4e49\u65f6\u88ab\u521b\u5efa\uff0c\u5728\u8bed\u53e5\u5757\u9000\u51fa\u65f6\u88ab\u9500\u6bc1\u3002 \u5bf9\u5c40\u90e8\u53d8\u91cf\u4f7f\u7528 static \u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u5c06\u5c40\u90e8\u53d8\u91cf\u7684 \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u4ece \u81ea\u52a8 \u4fee\u6539\u4e3a \u9759\u6001 \u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8fd9\u79cd\u5c40\u90e8\u53d8\u91cf\u4f1a\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u88ab\u521b\u5efa\uff0c\u7136\u540e\u5728\u7a0b\u5e8f\u7ed3\u675f\u540e\u88ab\u9500\u6bc1\uff08\u5c31\u50cf\u5168\u5c40\u53d8\u91cf\u4e00\u6837\uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u9759\u6001\u53d8\u91cf\u53ef\u4ee5\u5728\u79bb\u5f00\u4f5c\u7528\u57df\u540e\u7ee7\u7eed\u4fdd\u6301\u5b83\u7684\u503c\u3002 \u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u5c31\u53ef\u4ee5\u5f88\u597d\u7684\u5c55\u793a \u81ea\u52a8\u6301\u7eed\u65f6\u95f4 \u548c \u9759\u6001\u6301\u7eed\u65f6\u95f4 \u7684\u4e0d\u540c\u3002 \u81ea\u52a8\u6301\u7eed\u65f6\u95f4 \uff08\u9ed8\u8ba4\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void incrementAndPrint () { int value { 1 }; // automatic duration by default ++ value ; std :: cout << value << '\\n' ; } // value is destroyed here int main () { incrementAndPrint (); incrementAndPrint (); incrementAndPrint (); return 0 ; } \u6bcf\u6b21\u8c03\u7528 incrementAndPrint() \u7684\u65f6\u5019\uff0c\u53d8\u91cf value \u90fd\u4f1a\u88ab\u521b\u5efa\u5e76\u4e14\u8d4b\u503c\u4e3a 1\u3002 incrementAndPrint() \u4f1a\u628a value \u9012\u589e\u4e3a 2\uff0c\u7136\u540e\u5c06 2 \u6253\u5370\u51fa\u6765\uff0c\u5f53 incrementAndPrint() \u51fd\u6570\u9000\u51fa\u540e\uff0c\u53d8\u91cf\u5c31\u4f1a\u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1\u3002\u6240\u4ee5\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 2 2 2 \u63a5\u4e0b\u6765\uff0c\u8003\u8651 static \u7248\u672c\u7684\u7a0b\u5e8f\u3002\u8fd9\u4e24\u4e2a\u7248\u672c\u7a0b\u5e8f\u7684\u552f\u4e00\u533a\u522b\u5c31\u662f\u63a5\u4e0b\u6765\u6211\u4eec\u4f7f\u7528 static \u5173\u952e\u5b57\u5c06\u81ea\u52a8\u6301\u7eed\u65f6\u95f4\u4fee\u6539\u4e3a\u9759\u6001\u6301\u7eed\u65f6\u95f4\u3002 \u9759\u6001\u6301\u7eed\u65f6\u95f4 \uff08\u4f7f\u7528 static \u5173\u952e\u5b57\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void incrementAndPrint () { static int s_value { 1 }; // static duration via static keyword. This initializer is only executed once. ++ s_value ; std :: cout << s_value << '\\n' ; } // s_value is not destroyed here, but becomes inaccessible because it goes out of scope int main () { incrementAndPrint (); incrementAndPrint (); incrementAndPrint (); return 0 ; } \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u56e0\u4e3a s_value \u88ab\u5b9a\u4e49\u4e3a static \u7c7b\u578b\uff0c\u56e0\u6b64\u5b83\u4f1a\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u88ab\u521b\u5efa\u3002 \u5177\u6709\u521d\u503c0\u6216\u8005constexpr\u7c7b\u578b\u521d\u59cb\u5316\u503c\u7684\u9759\u6001\u53d8\u91cf\uff0c\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u5c31\u4f1a\u88ab\u521d\u59cb\u5316\u3002\u800c\u5177\u6709\u975econstexpr\u7c7b\u578b\u521d\u59cb\u5316\u503c\u7684\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u5219\u4f1a\u5728\u7b2c\u4e00\u6b21\u88ab\u5b9a\u4e49\u65f6\u8fdb\u884c\u521d\u59cb\u5316(\u5728\u540e\u7eed\u7684\u51fd\u6570\u8c03\u7528\u4e2d\uff0c\u8be5\u53d8\u91cf\u4e0d\u4f1a\u88ab\u91cd\u65b0\u521d\u59cb\u5316\uff09\u3002\u56e0\u4e3a s_value \u5177\u6709 constexpr \u521d\u59cb\u5316\u503c 1 \uff0c\u6240\u4ee5\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6 s_value \u5c31\u4f1a\u88ab\u521d\u59cb\u5316\u3002 \u5f53\u51fd\u6570\u7ed3\u675f\u65f6\uff0c s_value \u4f1a \u8d85\u51fa\u4f5c\u7528\u57df \uff0c\u4f46\u662f\u5b83\u4e0d\u4f1a\u88ab\u9500\u6bc1\u3002\u6bcf\u6b21 incrementAndPrint() \u88ab\u8c03\u7528\u65f6\uff0c s_value \u7684\u503c\u4ecd\u7136\u4e3a\u4e4b\u524d\u7684\u503c\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u7a0b\u5e8f\u7684\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 2 3 4 \u7c7b\u4f3c \u201cg_\u201d \u901a\u5e38\u4f5c\u4e3a\u5168\u5c40\u53d8\u91cf\u7684\u524d\u7f00\uff0c\u201cs_\u201d \u5e38\u88ab\u7528\u6765\u4f5c\u4e3a\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u7684\u524d\u7f00\u3002 \u9759\u6001\u5c40\u90e8\u53d8\u91cf\u6700\u5e38\u89c1\u7684\u7528\u6cd5\u662f\u6700\u4e3a\u4e00\u4e2a\u552f\u4e00ID\u751f\u6210\u5668\u3002\u60f3\u8c61\u4e00\u4e0b\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\u5305\u542b\u4e86\u5f88\u591a\u7c7b\u4f3c\u7684\u5bf9\u8c61\uff08\u4f8b\u5982\uff0c\u5728\u67d0\u4e2a\u6e38\u620f\u4e2d\u4f60\u88ab\u5f88\u591a\u50f5\u5c38\u653b\u51fb\uff0c\u6216\u8005\u4f60\u5e0c\u671b\u6a21\u62df\u5f88\u591a\u4e09\u89d2\u5f62\uff09\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f60\u51e0\u4e4e\u4e0d\u53ef\u80fd\u5b9a\u4f4d\u662f\u54ea\u4e2a\u5bf9\u8c61\u51fa\u95ee\u9898\u4e86\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u521b\u5efa\u4e00\u4e2a\u552f\u4e00\u7684\u6807\u8bc6\u7b26\uff0c\u5219\u53ef\u4ee5\u5728\u65e5\u540edebug\u65f6\u8f7b\u677e\u5730\u5c06\u5b83\u4eec\u533a\u522b\u5f00\u6765\u3002 \u4f7f\u7528\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u53ef\u4ee5\u5f88\u8f7b\u677e\u5730\u521b\u5efa\u552f\u4e00ID\uff1a 1 2 3 4 5 int generateID () { static int s_itemID { 0 }; return s_itemID ++ ; // makes copy of s_itemID, increments the real s_itemID, then returns the value in the copy } \u5f53\u51fd\u6570\u7b2c\u4e00\u6b21\u88ab\u8c03\u7528\u65f6\uff0c\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u88ab\u521d\u59cb\u5316\u4e3a0\u3002\u7b2c\u4e8c\u6b21\u8c03\u7528\u65f6\uff0c\u8be5\u53d8\u91cf\u9012\u589e\u4e3a\u4e86 1\u3002\u6bcf\u6b21\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u7684\u503c\u90fd\u4f1a\u6bd4\u4e4b\u524d\u4e00\u6b21\u8c03\u7528\u59271\u3002\u4f60\u53ef\u4ee5\u628a\u8fd9\u4e9b\u503c\u4f5c\u4e3a\u552f\u4e00ID\u8d4b\u503c\u7ed9\u5bf9\u8c61\u3002\u56e0\u4e3a s_itemID \u662f\u5c40\u90e8\u53d8\u91cf\uff0c\u6240\u4ee5\u5b83\u5e76\u4e0d\u4f1a\u88ab\u5176\u4ed6\u51fd\u6570\u201c\u7be1\u6539\u201d\u3002 \u9759\u6001\u53d8\u91cf\u76f8\u5bf9\u4e8e\u5168\u5c40\u53d8\u91cf\u5177\u6709\u4e00\u4e9b\u4f18\u52bf\uff08\u5b83\u4eec\u90fd\u4e0d\u4f1a\u5728\u7a0b\u5e8f\u7ed3\u675f\u524d\u88ab\u9500\u6bc1\uff09\uff0c\u56e0\u4e3a\u5b83\u53ea\u5728\u5b9a\u4e49\u5b83\u7684\u5757\u4e2d\u53ef\u89c1\u3002\u8fd9\u6837\u4e00\u6765\u5b83\u5c31\u4f1a\u66f4\u52a0\u5b89\u5168\uff08\u5f53\u4f60\u9700\u8981\u9891\u7e41\u4fee\u6539\u5b83\u7684\u503c\u7684\u65f6\u5019\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u3002\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u53ea\u4f1a\u5728\u7b2c\u4e00\u6b21\u6267\u884c\u65f6\u88ab\u521d\u59cb\u5316\uff0c\u540e\u9762\u4e0d\u4f1a\u5bf9\u5176\u8fdb\u884c\u91cd\u590d\u5730\u521d\u59cb\u5316\u3002","title":"\u9759\u6001\u5c40\u90e8\u53d8\u91cf"},{"location":"6-10-Static-local-variables/#_2","text":"\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u4e5f\u53ef\u4ee5\u88ab\u5b9a\u4e49\u4e3a\u5e38\u91cf\u3002\u9759\u6001\u5c40\u90e8\u5e38\u91cf\u7684\u5178\u578b\u4f7f\u7528\u573a\u666f\u662f\u5f53\u4f60\u7684\u51fd\u6570\u9700\u8981\u4e00\u4e2a\u5e38\u91cf\u503c\uff0c\u4f46\u662f\u521b\u5efa\u6216\u521d\u59cb\u5316\u8be5\u5e38\u91cf\u7684\u5f00\u9500\u975e\u5e38\u5927\uff08\u4f8b\u5982\u8be5\u503c\u662f\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u7684\uff09\u3002\u5982\u679c\u4f60\u4f7f\u7528\u4e00\u4e2a\u666e\u901a\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u90a3\u4e48\u8fd9\u4e2a\u53d8\u91cf\u5c31\u4f1a\u5728\u6bcf\u6b21\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u521d\u59cb\u5316\u3002\u800c\u4f7f\u7528const\u7c7b\u578b\u7684\u9759\u6001\u5c40\u90e8\u53d8\u91cf\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728\u7b2c\u4e00\u6b21\u8c03\u7528\u51fd\u6570\u65f6\u521d\u59cb\u5316\u5b83\uff0c\u7136\u540e\u5728\u540e\u7eed\u7684\u8c03\u7528\u4e2d\u5bf9\u5176\u8fdb\u884c\u91cd\u7528\u3002","title":"\u9759\u6001\u5c40\u90e8\u5e38\u91cf"},{"location":"6-10-Static-local-variables/#_3","text":"\u8003\u8651\u5982\u4e0b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include int getInteger () { static bool s_isFirstCall { true }; if ( s_isFirstCall ) { std :: cout << \"Enter an integer: \" ; s_isFirstCall = false ; } else { std :: cout << \"Enter another integer: \" ; } int i {}; std :: cin >> i ; return i ; } int main () { int a { getInteger () }; int b { getInteger () }; std :: cout << a << \" + \" << b << \" = \" << ( a + b ) << '\\n' ; return 0 ; } \u8f93\u51fa\u5982\u4e0b\uff1a 1 2 3 Enter an integer: 5 Enter another integer: 9 5 + 9 = 14 \u8fd9\u6bb5\u4ee3\u7801\u7684\u786e\u80fd\u591f\u6309\u7167\u9884\u671f\u5de5\u4f5c\uff0c\u4f46\u662f\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528\u7684\u662f\u4e00\u4e2a\u9759\u6001\u5c40\u90e8\u53d8\u91cf\uff0c\u8fd9\u4f1a\u4f7f\u5f97\u7a0b\u5e8f\u96be\u4ee5\u7406\u89e3\u3002\u5982\u679c\u6709\u4eba\u5728\u4e0d\u77e5\u9053 getInteger() \u51fd\u6570\u7684\u5b9e\u73b0\u7ec6\u8282\u65f6\uff0c\u9605\u8bfb main \u51fd\u6570\u7684\u4ee3\u7801\uff0c\u5b83\u5b8c\u5168\u4e0d\u4f1a\u8ba4\u4e3a\u4e24\u6b21\u8c03\u7528 getInteger() \u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u6548\u679c\u3002\u4f46\u662f\u4e24\u6b21\u8c03\u7528\u8be5\u51fd\u6570\u7684\u786e\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u8f93\u51fa\uff0c\u8fd9\u5c31\u597d\u8ba9\u4eba\u611f\u5230\u975e\u5e38\u56f0\u60d1\uff0c 1 \u5047\u8bbe\u4f60\u7684\u5fae\u6ce2\u7089\u4e0a\u6709\u4e00\u4e2a+1\u6309\u94ae\uff0c\u5f53\u4f60\u6309\u4e0b\u5b83\u7684\u65f6\u5019\u5c31\u4f1a\u5ef6\u957f\u4e00\u5206\u949f\u52a0\u70ed\u65f6\u95f4\u3002\u4f60\u51c6\u5907\u52a0\u70ed\u4e00\u4e9b\u5403\u7684\uff0c\u8bbe\u5b9a\u597d\u65f6\u95f4\uff0c\u7136\u540e\u7b49\u7740\u51fa\u9505\u3002\u8fd9\u671f\u95f4\u4f60\u53ef\u4ee5\u770b\u770b\u7a97\u5916\u7684\u732b\u54aa\uff0c\u6d88\u78e8\u6d88\u78e8\u65f6\u95f4\u3002\u7136\u540e\u5fae\u6ce2\u7089\u54cd\u4e86\uff0c\u4f60\u628a\u996d\u62ff\u51fa\u6765\u957f\u4e86\u4e00\u53e3\uff0c\u53d1\u73b0\u8fd8\u6709\u70b9\u51c9\u3002\u6ca1\u5173\u7cfb\uff0c\u4f60\u53c8\u628a\u996d\u653e\u4e86\u56de\u53bb\u7136\u540e\u6309\u4e86\u4e00\u4e0b+1\u6309\u94ae\uff0c\u51c6\u5907\u518d\u70ed\u4e00\u5206\u949f\u3002\u53ef\u662f\u8fd9\u4e00\u6b21\uff0c\u5fae\u6ce2\u7089\u53ea\u52a0\u70ed\u4e86\u4e00\u79d2\u949f\uff0c\u800c\u4e0d\u662f\u4e00\u5206\u949f\u3002\u8fd9\u79cd\u60c5\u51b5\u5c31\u50cf\u6211\u4eec\u603b\u8bf4\u7684\u90a3\u6837\u201c\u6211\u4eec\u4ec0\u4e48\u4e5f\u6ca1\u6539\uff0c\u7a0b\u5e8f\u5c31\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u4e86\u201d\u6216\u662f\u201c\u4e0a\u6b21\u7528\u8fd8\u597d\u597d\u7684\u5462\u201d\u3002\u6211\u4eec\u603b\u662f\u671f\u671b\uff0c\u5f53\u91cd\u590d\u4e00\u4e2a\u52a8\u4f5c\u65f6\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u4e5f\u5e94\u8be5\u548c\u4e4b\u524d\u662f\u4e00\u6837\u7684\uff0c\u540c\u6837\u7684\u9053\u7406\u4e5f\u9002\u7528\u4e8e\u51fd\u6570\u3002 \u5047\u8bbe\u6211\u4eec\u60f3\u8981\u4e3a\u8ba1\u7b97\u5668\u6dfb\u52a0\u51cf\u6cd5\u529f\u80fd\uff0c\u671f\u671b\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 Addition Enter an integer: 5 Enter another integer: 9 5 + 9 = 14 Subtraction Enter an integer: 12 Enter another integer: 3 12 - 3 = 9 \u6b64\u65f6\u4f60\u53ef\u80fd\u4f1a\u8003\u8651\u4f7f\u7528 getInteger() \u518d\u8bfb\u53d6\u4e24\u4e2a\u6574\u6570\uff0c\u5c31\u50cf\u505a\u52a0\u6cd5\u65f6\u4e00\u6837\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 int main () { std :: cout << \"Addition \\n \" ; int a { getInteger () }; int b { getInteger () }; std :: cout << a << \" + \" << b << \" = \" << ( a + b ) << '\\n' ; std :: cout << \"Subtraction \\n \" ; int c { getInteger () }; int d { getInteger () }; std :: cout << c << \" - \" << d << \" = \" << ( c - d ) << '\\n' ; return 0 ; } \u4f46\u662f\uff0c\u8fd9\u6837\u5e76\u6ca1\u6709\u7528\uff0c\u8f93\u51fa\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 Addition Enter an integer: 5 Enter another integer: 9 5 + 9 = 14 Subtraction Enter another integer: 12 Enter another integer: 3 12 - 3 = 9 (\u8f93\u51fa\u7684\u8fd8\u662f \u201cEnter another integer\u201d \u800c\u4e0d\u662f \u201cEnter an integer\u201d) getInteger() \u662f\u4e0d\u53ef\u91cd\u7528\u7684\uff0c\u56e0\u5b83\u5177\u6709\u67d0\u79cd\u5185\u90e8\u72b6\u6001\uff08\u9759\u6001\u5c40\u90e8\u53d8\u91cf s_isFirstCall \uff09\uff0c\u800c\u8be5\u72b6\u6001\u5e76\u4e0d\u80fd\u591f\u4ece\u5916\u90e8\u88ab\u91cd\u7f6e\u3002 s_isFirstCall \u5e76\u4e0d\u662f\u4e00\u4e2a\u9700\u8981\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u4fdd\u6301\u552f\u4e00\u7684\u53d8\u91cf\uff0c\u5c3d\u7ba1\u4e0a\u9762\u7b2c\u4e00\u4e2a\u7a0b\u5e8f\u80fd\u591f\u6b63\u786e\u8fd0\u884c\uff0c\u4f46\u662f\u9759\u6001\u53d8\u91cf\u963b\u788d\u4e86\u6211\u4eec\u5bf9\u51fd\u6570\u7684\u91cd\u7528\u3002 \u5b9e\u73b0 getInteger \u7684\u66f4\u597d\u7684\u65b9\u6cd5\u662f\u5c06 s_isFirstCall \u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u3002\u8fd9\u6837\u4e3b\u8c03\u51fd\u6570\u5c31\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u6253\u5370\u7684\u5185\u5bb9\u3002 \u9759\u6001\u5c40\u90e8\u53d8\u91cf\u9002\u7528\u4e8e\u8be5\u53d8\u91cf\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\uff08\u6216\u53ef\u9884\u89c1\u7684\u672a\u6765\uff09\u9700\u8981\u4fdd\u6301\u552f\u4e00\u6027\u4e14\u65e0\u9700\u5bf9\u5176\u8fdb\u884c\u91cd\u7f6e\u7684\u60c5\u51b5\u3002 \u6700\u4f73\u5b9e\u8df5 \u9664\u975e\u53d8\u91cf\u6c38\u8fdc\u4e0d\u9700\u8981\u88ab\u91cd\u7f6e\uff0c\u5426\u5219\u8981\u907f\u514d\u4f7f\u7528\u9759\u6001\u5c40\u90e8\u53d8\u91cf","title":"\u4e0d\u8981\u4f7f\u7528\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u6765\u6539\u53d8\u6d41\u7a0b"},{"location":"6-11-Scope-duration-and-linkage-summary/","tags":["linkage","scope","C++11"],"text":"\u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5\u8fd9\u4e9b\u6982\u5ff5\u5f88\u7ed5\u4eba\uff0c\u6240\u4ee5\u6211\u4eec\u5229\u7528\u8fd9\u8282\u8bfe\u5bf9\u5b83\u4eec\u8fdb\u884c\u4e00\u6b21\u6574\u7406\u548c\u603b\u7ed3\u3002\u6709\u4e9b\u5185\u5bb9\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\uff0c\u4f46\u662f\u6211\u4eec\u4ecd\u7136\u5c06\u5176\u7f57\u5217\u5728\u8fd9\u91cc\uff0c\u5728\u540e\u7eed\u5b66\u4e60\u65f6\u53ef\u4ee5\u8fdb\u884c\u53c2\u8003\u3002 \u4f5c\u7528\u57df\u5c0f\u7ed3 \u53d8\u91cf\u6807\u8bc6\u7b26\u7684 \u4f5c\u7528\u57df \u51b3\u5b9a\u4e86\u6807\u8bc6\u7b26\u5728\u54ea\u91cc\u53ef\u4ee5\u88ab\u8bbf\u95ee\u3002 \u5177\u6709 \u5757\uff08\u5c40\u90e8\uff09\u4f5c\u7528\u57df \u7684\u6807\u8bc6\u7b26\uff0c\u53ea\u80fd\u591f\u5728\u58f0\u660e\u5b83\u4eec\u7684\u5757\uff08\u53ca\u5176\u5d4c\u5957\u5757\uff09\u4e2d\u8bbf\u95ee\uff0c\u8fd9\u4e9b\u53d8\u91cf\u5305\u62ec\uff1a \u5c40\u90e8\u53d8\u91cf \u51fd\u6570\u5f62\u53c2 \u5b9a\u4e49\u5728\u5757\u4e2d\u7684\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u578b\uff08\u5305\u62ec\u679a\u4e3e\u548c\u7c7b\uff09 \u5177\u6709 \u6587\u4ef6\uff08\u5168\u5c40\uff09\u4f5c\u7528\u57df \u7684\u53d8\u91cf\u548c\u51fd\u6570\u4ece\u58f0\u660e\u4f4d\u7f6e\u5f00\u59cb\uff0c\u76f4\u5230\u6587\u4ef6\u7ed3\u5c3e\u90fd\u53ef\u4ee5\u88ab\u8bbf\u95ee\uff0c \u5305\u62ec\uff1a \u5168\u5c40\u53d8\u91cf \u51fd\u6570 \u5b9a\u4e49\u5728\u5168\u5c40\u4f5c\u7528\u57df\u6216\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u578b \uff08\u5305\u62ec\u679a\u4e3e\u548c\u7c7b\uff09 \u6301\u7eed\u65f6\u95f4\u5c0f\u7ed3 \u53d8\u91cf\u7684\u6301\u7eed\u65f6\u95f4\u51b3\u5b9a\u4e86\u5b83\u5408\u9002\u88ab\u521b\u5efa\u548c\u9500\u6bc1\u3002 \u5177\u6709 \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u7684\u53d8\u91cf\uff0c\u5728\u5b9a\u4e49\u65f6\u88ab\u521b\u5efa\uff0c\u800c\u4e14\u5728\u79bb\u5f00\u8bed\u53e5\u5757\u65f6\u88ab\u9500\u6bc1\uff0c\u5305\u62ec\uff1a \u5c40\u90e8\u53d8\u91cf \u51fd\u6570\u5f62\u53c2 \u5177\u6709 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u7684\u53d8\u91cf\uff0c\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u521b\u5efa\uff0c\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u9500\u6bc1\uff0c\u5305\u62ec\uff1a \u5168\u5c40\u53d8\u91cf \u9759\u6001\u5c40\u90e8\u53d8\u91cf \u5177\u6709 \u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u65f6\u95f4\u7684\u53d8\u91cf\u5728\u53ef\u4ee5\u6839\u636e\u7a0b\u5e8f\u5458\u7684\u9700\u8981\u521b\u5efa\u548c\u9500\u6bc1\uff0c\u5305\u62ec\uff1a \u52a8\u6001\u5206\u914d\u7684\u53d8\u91cf \u94fe\u63a5\u5c0f\u7ed3 \u6807\u8bc6\u7b26\u7684\u94fe\u63a5\u5c5e\u6027\uff0c\u51b3\u5b9a\u4e86\u6807\u8bc6\u7b26\u7684\u591a\u4e2a\u58f0\u660e\u662f\u5426\u88ab\u770b\u505a\u540c\u4e00\u4e2a\u8868\u793a\u7b26\u3002 \u4e0d\u5177\u5907\u94fe\u63a5\u5c5e\u6027 \u7684\u6807\u8bc6\u7b26\uff0c\u610f\u5473\u7740\u8be5\u6807\u8bc6\u7b26\u53ea\u8868\u793a\u5b83\u81ea\u5df1\uff0c\u5305\u62ec\uff1a \u5c40\u90e8\u53d8\u91cf \u5b9a\u4e49\u5728\u5757\u4e2d\u7684\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u578b\uff08\u5305\u62ec\u679a\u4e3e\u548c\u7c7b\uff09 \u5177\u5907\u5185\u90e8\u94fe\u63a5\u5c5e\u6027 **\u7684\u6807\u8bc6\u7b26\u53ef\u4ee5\u5728\u6587\u4ef6\u5185\u90e8\u7684\u4efb\u4f55\u4f4d\u7f6e\u88ab\u8bbf\u95ee\uff0c\u5305\u62ec\uff1a \u9759\u6001\u5168\u5c40\u53d8\u91cf(\u521d\u59cb\u5316\u6216\u672a\u521d\u59cb\u5316\u7684) \u9759\u6001( static )\u51fd\u6570 const \u5168\u5c40\u53d8\u91cf \u5b9a\u4e49\u5728\u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u51fd\u6570 \u5b9a\u4e49\u5728\u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u578b\uff08\u5305\u62ec\u679a\u4e3e\u548c\u7c7b\uff09 \u5177\u5907\u5916\u90e8\u94fe\u63a5\u5c5e\u6027 \u7684\u6807\u8bc6\u7b26\u53ef\u4ee5\u5728\u5b9a\u4e49\u5b83\u7684\u6587\u4ef6\u4e2d\u8bbf\u95ee\uff0c\u4e5f\u53ef\u4ee5\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\uff08\u901a\u8fc7 \u524d\u5411\u58f0\u660e \uff09\u8bbf\u95ee\uff0c\u5305\u62ec\uff1a \u51fd\u6570 \u975e\u5e38\u91cf\u5168\u5c40\u53d8\u91cf(\u521d\u59cb\u5316\u6216\u672a\u521d\u59cb\u5316\u7684) \u5916\u90e8\u5e38\u91cf\u5168\u5c40\u53d8\u91cf \u5185\u8054\u5e38\u91cf\u5168\u5c40\u53d8\u91cf \u5b9a\u4e49\u5728\u547d\u540d\u7a7a\u95f4\u6216\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u7684\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u578b \uff08\u5305\u62ec\u679a\u4e3e\u548c\u7c7b\uff09 \u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u6807\u8bc6\u53f7\u5982\u679c\u5728\u591a\u4e2acpp\u6587\u4ef6\u4e2d\u88ab\u7f16\u8bd1\uff0c\u5219\u4f1a\u5bfc\u81f4\u94fe\u63a5\u5668\u62a5\u9519\uff08\u56e0\u4e3a\u8fdd\u53cd\u4e86 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \uff09\u3002\u4e0d\u8fc7\uff0c\u4e5f\u6709\u4f8b\u5916\u7684\u60c5\u51b5\uff08\u5bf9\u4e8e\u7c7b\u578b\u3001\u76ee\u6807\u3001\u5185\u8054\u51fd\u6570\u548c\u5185\u8054\u53d8\u91cf\uff09\u2014\u2014\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u76f8\u5173\u5185\u5bb9\u3002 \u540c\u65f6\uff0c\u6ce8\u610f \u51fd\u6570\u9ed8\u8ba4\u5177\u6709\u5916\u90e8\u94fe\u63a5 \uff0c\u5fc5\u987b\u8981\u4f7f\u7528 static \u624d\u80fd\u4f7f\u5176\u5177\u6709\u5185\u90e8\u94fe\u63a5\u3002 \u53d8\u91cf\u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5\u5c0f\u7ed3 \u56e0\u4e3a\u53d8\u91cf\u5177\u6709\u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5\u5c5e\u6027\uff0c\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u8868\u683c\u5bf9\u5176\u8fdb\u884c\u603b\u7ed3\uff1a \u7c7b\u578b \u4f8b\u5b50 \u4f5c\u7528\u57df \u6301\u7eed\u65f6\u95f4 \u94fe\u63a5\u5c5e\u6027 \u5907\u6ce8 \u5c40\u90e8\u53d8\u91cf int x; \u5757 \u81ea\u52a8 \u65e0 \u9759\u6001\u5c40\u90e8\u53d8\u91cf static int s_x; \u5757 \u9759\u6001 \u65e0 \u52a8\u6001\u53d8\u91cf int *x { new int{} }; \u5757 \u52a8\u6001 \u65e0 \u51fd\u6570\u5f62\u53c2 void foo(int x) \u5757 \u81ea\u52a8 \u65e0 \u5916\u90e8\u975e\u5e38\u91cf\u5168\u5c40\u53d8\u91cf int g_x; \u6587\u4ef6 \u9759\u6001 \u5916\u90e8 \u521d\u59cb\u5316\u6216\u672a\u521d\u59cb\u5316 \u5185\u90e8\u975e\u5e38\u91cf\u5168\u5c40\u53d8\u91cf static int g_x; \u6587\u4ef6 \u9759\u6001 \u5185\u90e8 \u521d\u59cb\u5316\u6216\u672a\u521d\u59cb\u5316 \u5185\u90e8\u5e38\u91cf\u5168\u5c40\u53d8\u91cf constexpr int g_x { 1 }; \u6587\u4ef6 \u9759\u6001 \u5185\u90e8 \u5fc5\u987b\u521d\u59cb\u5316 \u5916\u90e8\u5e38\u91cf\u5168\u5c40\u53d8\u91cf extern const int g_x { 1 }; \u6587\u4ef6 \u9759\u6001 \u5916\u90e8 \u5fc5\u987b\u521d\u59cb\u5316 \u5185\u8054\u5e38\u91cf\u5168\u5c40\u53d8\u91cf(C++17) inline constexpr int g_x { 1 }; \u6587\u4ef6 \u9759\u6001 \u5916\u90e8 \u5fc5\u987b\u521d\u59cb\u5316 \u524d\u5411\u58f0\u660e\u5c0f\u7ed3 \u4f7f\u7528\u524d\u5411\u58f0\u660e\u53ef\u4ee5\u4ece\u5176\u4ed6\u6587\u4ef6\u4e2d\u8bbf\u95ee\u4e00\u4e2a\u51fd\u6570\u6216\u8005\u53d8\u91cf\u3002\u58f0\u660e\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4e0e\u5f80\u5e38\u4e00\u6837(\u5168\u5c40\u53d8\u91cf\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\uff0c\u5c40\u90e8\u53d8\u91cf\u5177\u6709\u5757\u4f5c\u7528\u57df)\u3002 \u7c7b\u578b \u4f8b\u5b50 \u5907\u6ce8 \u51fd\u6570\u524d\u5411\u58f0\u660e void foo(int x); \u53ea\u9700\u8981\u51fd\u6570\u539f\u578b\uff0c\u4e0d\u80fd\u6709\u51fd\u6570\u4f53 \u975e\u5e38\u91cf\u7684\u53d8\u91cf\u524d\u5411\u58f0\u660e extern int g_x; \u5fc5\u987b\u672a\u521d\u59cb\u5316 \u5e38\u91cf\u7684\u524d\u5411\u58f0\u660e extern const int g_x; \u5fc5\u987b\u672a\u521d\u59cb\u5316 Constexpr \u53d8\u91cf\u524d\u5411\u58f0\u660e extern constexpr int g_x; \u4e0d\u5141\u8bb8\uff0c constexpr \u4e0d\u80fd\u524d\u5411\u58f0\u660e \u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26\u5230\u5e95\u662f\u4ec0\u4e48\uff1f \u5f53 static \u548c extern \u5173\u952e\u5b57\uff08\u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26\uff09\u4f5c\u4e3a\u53d8\u91cf\u58f0\u660e\u7684\u4e00\u90e8\u5206\u65f6\uff0c\u5b83\u4eec\u53ef\u4ee5\u8bbe\u5b9a\u6807\u8bc6\u7b26\u7684 \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u548c\u94fe\u63a5\u5c5e\u6027\u3002 C++ \u652f\u6301 4 \u79cd\u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26\uff1a \u8bf4\u660e\u7b26 \u542b\u4e49 \u5907\u6ce8 extern static (\u6216 thread_local) \u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u548c\u5916\u90e8\u94fe\u63a5 static static (\u6216 thread_local) \u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u548c\u5185\u90e8\u94fe\u63a5 thread_local thread \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 mutable \u5373\u4f7f\u7c7b\u662fconst\u7c7b\u578b\u7684\uff0c\u53d8\u91cf\u4e5f\u5141\u8bb8\u4fee\u6539 auto \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u5728 C++11 \u4e2d\u5df2\u7ecf\u5f03\u7528 register \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u5e76\u4e14\u63d0\u793a\u7f16\u8bd1\u5668\u5c06\u5176\u5b58\u653e\u5728\u5bc4\u5b58\u5668\u4e2d \u5728 C++17 \u4e2d\u5df2\u7ecf\u5f03\u7528 \u672f\u8bed \u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26 \u901a\u5e38\u53ea\u4f1a\u5728\u6b63\u5f0f\u7684\u6587\u6863\u4e2d\u4f7f\u7528\u3002","title":"6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3"},{"location":"6-11-Scope-duration-and-linkage-summary/#_1","text":"\u53d8\u91cf\u6807\u8bc6\u7b26\u7684 \u4f5c\u7528\u57df \u51b3\u5b9a\u4e86\u6807\u8bc6\u7b26\u5728\u54ea\u91cc\u53ef\u4ee5\u88ab\u8bbf\u95ee\u3002 \u5177\u6709 \u5757\uff08\u5c40\u90e8\uff09\u4f5c\u7528\u57df \u7684\u6807\u8bc6\u7b26\uff0c\u53ea\u80fd\u591f\u5728\u58f0\u660e\u5b83\u4eec\u7684\u5757\uff08\u53ca\u5176\u5d4c\u5957\u5757\uff09\u4e2d\u8bbf\u95ee\uff0c\u8fd9\u4e9b\u53d8\u91cf\u5305\u62ec\uff1a \u5c40\u90e8\u53d8\u91cf \u51fd\u6570\u5f62\u53c2 \u5b9a\u4e49\u5728\u5757\u4e2d\u7684\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u578b\uff08\u5305\u62ec\u679a\u4e3e\u548c\u7c7b\uff09 \u5177\u6709 \u6587\u4ef6\uff08\u5168\u5c40\uff09\u4f5c\u7528\u57df \u7684\u53d8\u91cf\u548c\u51fd\u6570\u4ece\u58f0\u660e\u4f4d\u7f6e\u5f00\u59cb\uff0c\u76f4\u5230\u6587\u4ef6\u7ed3\u5c3e\u90fd\u53ef\u4ee5\u88ab\u8bbf\u95ee\uff0c \u5305\u62ec\uff1a \u5168\u5c40\u53d8\u91cf \u51fd\u6570 \u5b9a\u4e49\u5728\u5168\u5c40\u4f5c\u7528\u57df\u6216\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u578b \uff08\u5305\u62ec\u679a\u4e3e\u548c\u7c7b\uff09","title":"\u4f5c\u7528\u57df\u5c0f\u7ed3"},{"location":"6-11-Scope-duration-and-linkage-summary/#_2","text":"\u53d8\u91cf\u7684\u6301\u7eed\u65f6\u95f4\u51b3\u5b9a\u4e86\u5b83\u5408\u9002\u88ab\u521b\u5efa\u548c\u9500\u6bc1\u3002 \u5177\u6709 \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u7684\u53d8\u91cf\uff0c\u5728\u5b9a\u4e49\u65f6\u88ab\u521b\u5efa\uff0c\u800c\u4e14\u5728\u79bb\u5f00\u8bed\u53e5\u5757\u65f6\u88ab\u9500\u6bc1\uff0c\u5305\u62ec\uff1a \u5c40\u90e8\u53d8\u91cf \u51fd\u6570\u5f62\u53c2 \u5177\u6709 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u7684\u53d8\u91cf\uff0c\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u521b\u5efa\uff0c\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u9500\u6bc1\uff0c\u5305\u62ec\uff1a \u5168\u5c40\u53d8\u91cf \u9759\u6001\u5c40\u90e8\u53d8\u91cf \u5177\u6709 \u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u65f6\u95f4\u7684\u53d8\u91cf\u5728\u53ef\u4ee5\u6839\u636e\u7a0b\u5e8f\u5458\u7684\u9700\u8981\u521b\u5efa\u548c\u9500\u6bc1\uff0c\u5305\u62ec\uff1a \u52a8\u6001\u5206\u914d\u7684\u53d8\u91cf","title":"\u6301\u7eed\u65f6\u95f4\u5c0f\u7ed3"},{"location":"6-11-Scope-duration-and-linkage-summary/#_3","text":"\u6807\u8bc6\u7b26\u7684\u94fe\u63a5\u5c5e\u6027\uff0c\u51b3\u5b9a\u4e86\u6807\u8bc6\u7b26\u7684\u591a\u4e2a\u58f0\u660e\u662f\u5426\u88ab\u770b\u505a\u540c\u4e00\u4e2a\u8868\u793a\u7b26\u3002 \u4e0d\u5177\u5907\u94fe\u63a5\u5c5e\u6027 \u7684\u6807\u8bc6\u7b26\uff0c\u610f\u5473\u7740\u8be5\u6807\u8bc6\u7b26\u53ea\u8868\u793a\u5b83\u81ea\u5df1\uff0c\u5305\u62ec\uff1a \u5c40\u90e8\u53d8\u91cf \u5b9a\u4e49\u5728\u5757\u4e2d\u7684\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u578b\uff08\u5305\u62ec\u679a\u4e3e\u548c\u7c7b\uff09 \u5177\u5907\u5185\u90e8\u94fe\u63a5\u5c5e\u6027 **\u7684\u6807\u8bc6\u7b26\u53ef\u4ee5\u5728\u6587\u4ef6\u5185\u90e8\u7684\u4efb\u4f55\u4f4d\u7f6e\u88ab\u8bbf\u95ee\uff0c\u5305\u62ec\uff1a \u9759\u6001\u5168\u5c40\u53d8\u91cf(\u521d\u59cb\u5316\u6216\u672a\u521d\u59cb\u5316\u7684) \u9759\u6001( static )\u51fd\u6570 const \u5168\u5c40\u53d8\u91cf \u5b9a\u4e49\u5728\u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u51fd\u6570 \u5b9a\u4e49\u5728\u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u578b\uff08\u5305\u62ec\u679a\u4e3e\u548c\u7c7b\uff09 \u5177\u5907\u5916\u90e8\u94fe\u63a5\u5c5e\u6027 \u7684\u6807\u8bc6\u7b26\u53ef\u4ee5\u5728\u5b9a\u4e49\u5b83\u7684\u6587\u4ef6\u4e2d\u8bbf\u95ee\uff0c\u4e5f\u53ef\u4ee5\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\uff08\u901a\u8fc7 \u524d\u5411\u58f0\u660e \uff09\u8bbf\u95ee\uff0c\u5305\u62ec\uff1a \u51fd\u6570 \u975e\u5e38\u91cf\u5168\u5c40\u53d8\u91cf(\u521d\u59cb\u5316\u6216\u672a\u521d\u59cb\u5316\u7684) \u5916\u90e8\u5e38\u91cf\u5168\u5c40\u53d8\u91cf \u5185\u8054\u5e38\u91cf\u5168\u5c40\u53d8\u91cf \u5b9a\u4e49\u5728\u547d\u540d\u7a7a\u95f4\u6216\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u7684\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u578b \uff08\u5305\u62ec\u679a\u4e3e\u548c\u7c7b\uff09 \u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u6807\u8bc6\u53f7\u5982\u679c\u5728\u591a\u4e2acpp\u6587\u4ef6\u4e2d\u88ab\u7f16\u8bd1\uff0c\u5219\u4f1a\u5bfc\u81f4\u94fe\u63a5\u5668\u62a5\u9519\uff08\u56e0\u4e3a\u8fdd\u53cd\u4e86 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \uff09\u3002\u4e0d\u8fc7\uff0c\u4e5f\u6709\u4f8b\u5916\u7684\u60c5\u51b5\uff08\u5bf9\u4e8e\u7c7b\u578b\u3001\u76ee\u6807\u3001\u5185\u8054\u51fd\u6570\u548c\u5185\u8054\u53d8\u91cf\uff09\u2014\u2014\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u76f8\u5173\u5185\u5bb9\u3002 \u540c\u65f6\uff0c\u6ce8\u610f \u51fd\u6570\u9ed8\u8ba4\u5177\u6709\u5916\u90e8\u94fe\u63a5 \uff0c\u5fc5\u987b\u8981\u4f7f\u7528 static \u624d\u80fd\u4f7f\u5176\u5177\u6709\u5185\u90e8\u94fe\u63a5\u3002","title":"\u94fe\u63a5\u5c0f\u7ed3"},{"location":"6-11-Scope-duration-and-linkage-summary/#_4","text":"\u56e0\u4e3a\u53d8\u91cf\u5177\u6709\u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5\u5c5e\u6027\uff0c\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u8868\u683c\u5bf9\u5176\u8fdb\u884c\u603b\u7ed3\uff1a \u7c7b\u578b \u4f8b\u5b50 \u4f5c\u7528\u57df \u6301\u7eed\u65f6\u95f4 \u94fe\u63a5\u5c5e\u6027 \u5907\u6ce8 \u5c40\u90e8\u53d8\u91cf int x; \u5757 \u81ea\u52a8 \u65e0 \u9759\u6001\u5c40\u90e8\u53d8\u91cf static int s_x; \u5757 \u9759\u6001 \u65e0 \u52a8\u6001\u53d8\u91cf int *x { new int{} }; \u5757 \u52a8\u6001 \u65e0 \u51fd\u6570\u5f62\u53c2 void foo(int x) \u5757 \u81ea\u52a8 \u65e0 \u5916\u90e8\u975e\u5e38\u91cf\u5168\u5c40\u53d8\u91cf int g_x; \u6587\u4ef6 \u9759\u6001 \u5916\u90e8 \u521d\u59cb\u5316\u6216\u672a\u521d\u59cb\u5316 \u5185\u90e8\u975e\u5e38\u91cf\u5168\u5c40\u53d8\u91cf static int g_x; \u6587\u4ef6 \u9759\u6001 \u5185\u90e8 \u521d\u59cb\u5316\u6216\u672a\u521d\u59cb\u5316 \u5185\u90e8\u5e38\u91cf\u5168\u5c40\u53d8\u91cf constexpr int g_x { 1 }; \u6587\u4ef6 \u9759\u6001 \u5185\u90e8 \u5fc5\u987b\u521d\u59cb\u5316 \u5916\u90e8\u5e38\u91cf\u5168\u5c40\u53d8\u91cf extern const int g_x { 1 }; \u6587\u4ef6 \u9759\u6001 \u5916\u90e8 \u5fc5\u987b\u521d\u59cb\u5316 \u5185\u8054\u5e38\u91cf\u5168\u5c40\u53d8\u91cf(C++17) inline constexpr int g_x { 1 }; \u6587\u4ef6 \u9759\u6001 \u5916\u90e8 \u5fc5\u987b\u521d\u59cb\u5316","title":"\u53d8\u91cf\u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5\u5c0f\u7ed3"},{"location":"6-11-Scope-duration-and-linkage-summary/#_5","text":"\u4f7f\u7528\u524d\u5411\u58f0\u660e\u53ef\u4ee5\u4ece\u5176\u4ed6\u6587\u4ef6\u4e2d\u8bbf\u95ee\u4e00\u4e2a\u51fd\u6570\u6216\u8005\u53d8\u91cf\u3002\u58f0\u660e\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4e0e\u5f80\u5e38\u4e00\u6837(\u5168\u5c40\u53d8\u91cf\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\uff0c\u5c40\u90e8\u53d8\u91cf\u5177\u6709\u5757\u4f5c\u7528\u57df)\u3002 \u7c7b\u578b \u4f8b\u5b50 \u5907\u6ce8 \u51fd\u6570\u524d\u5411\u58f0\u660e void foo(int x); \u53ea\u9700\u8981\u51fd\u6570\u539f\u578b\uff0c\u4e0d\u80fd\u6709\u51fd\u6570\u4f53 \u975e\u5e38\u91cf\u7684\u53d8\u91cf\u524d\u5411\u58f0\u660e extern int g_x; \u5fc5\u987b\u672a\u521d\u59cb\u5316 \u5e38\u91cf\u7684\u524d\u5411\u58f0\u660e extern const int g_x; \u5fc5\u987b\u672a\u521d\u59cb\u5316 Constexpr \u53d8\u91cf\u524d\u5411\u58f0\u660e extern constexpr int g_x; \u4e0d\u5141\u8bb8\uff0c constexpr \u4e0d\u80fd\u524d\u5411\u58f0\u660e","title":"\u524d\u5411\u58f0\u660e\u5c0f\u7ed3"},{"location":"6-11-Scope-duration-and-linkage-summary/#_6","text":"\u5f53 static \u548c extern \u5173\u952e\u5b57\uff08\u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26\uff09\u4f5c\u4e3a\u53d8\u91cf\u58f0\u660e\u7684\u4e00\u90e8\u5206\u65f6\uff0c\u5b83\u4eec\u53ef\u4ee5\u8bbe\u5b9a\u6807\u8bc6\u7b26\u7684 \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u548c\u94fe\u63a5\u5c5e\u6027\u3002 C++ \u652f\u6301 4 \u79cd\u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26\uff1a \u8bf4\u660e\u7b26 \u542b\u4e49 \u5907\u6ce8 extern static (\u6216 thread_local) \u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u548c\u5916\u90e8\u94fe\u63a5 static static (\u6216 thread_local) \u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u548c\u5185\u90e8\u94fe\u63a5 thread_local thread \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 mutable \u5373\u4f7f\u7c7b\u662fconst\u7c7b\u578b\u7684\uff0c\u53d8\u91cf\u4e5f\u5141\u8bb8\u4fee\u6539 auto \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u5728 C++11 \u4e2d\u5df2\u7ecf\u5f03\u7528 register \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u5e76\u4e14\u63d0\u793a\u7f16\u8bd1\u5668\u5c06\u5176\u5b58\u653e\u5728\u5bc4\u5b58\u5668\u4e2d \u5728 C++17 \u4e2d\u5df2\u7ecf\u5f03\u7528 \u672f\u8bed \u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26 \u901a\u5e38\u53ea\u4f1a\u5728\u6b63\u5f0f\u7684\u6587\u6863\u4e2d\u4f7f\u7528\u3002","title":"\u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26\u5230\u5e95\u662f\u4ec0\u4e48\uff1f"},{"location":"6-12-Using-declarations-and-using%20directives/","tags":["using"],"text":"Key Takeaway \u51fd\u6570\u5185\u90e8\u4f7f\u7528 using \u58f0\u660e\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\uff0c\u540c\u65f6\u4e5f\u662f\u5b89\u5168\u7684 \u660e\u786e\u6307\u5b9a\u4f5c\u7528\u57df\u597d\u8fc7 using \u8bed\u53e5\u3002\u5c3d\u53ef\u80fd\u5730\u907f\u514d\u4f7f\u7528 using \u6307\u4ee4\u3002\u5728\u8bed\u53e5\u5757\u4e2d\u4f7f\u7528 using \u58f0\u660e\u901a\u5e38\u662f\u53ef\u4ee5\u7684\u3002 \u4f60\u53ef\u80fd\u5728\u5f88\u591a\u6559\u6750\u6216\u8005\u6559\u7a0b\u4e2d\u90fd\u770b\u8fc7\u8fd9\u6837\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 #include using namespace std ; int main () { cout << \"Hello world!\" ; return 0 ; } \u6709\u4e00\u4e9b\u53e4\u65e9\u7684 IDE \u751a\u81f3\u4f1a\u81ea\u52a8\u4e3a\u4f60\u65b0\u5efa\u8fd9\u6837\u7684 C++ \u9879\u76ee\uff08\u4ee5\u4fbf\u4f60\u53ef\u4ee5\u5feb\u901f\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\uff09\u3002 \u5982\u679c\u4f60\u770b\u5230\u8fd9\u6837\u7684\u4ee3\u7801\uff0c\u5343\u4e07\u8981\u539f\u8c05\u8fd9\u4e9b\u8bfe\u672c\u3001\u6559\u7a0b\u6216\u662f\u7f16\u8bd1\u5668\uff0c\u5b83\u4eec\u5f88\u53ef\u80fd\u5df2\u7ecf\u8fc7\u65f6\u4e86\u3002\u4e3a\u4ec0\u4e48\uff1f\u4e0a\u5b8c\u8fd9\u8282\u8bfe\u4f60\u5c31\u77e5\u9053\u4e86\u3002 \u5386\u53f2\u8bfe \u5728 C++ \u5f00\u59cb\u652f\u6301\u547d\u540d\u7a7a\u95f4\u4e4b\u524d\uff0c\u6240\u6709\u73b0\u5728\u4f4d\u4e8e std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\uff0c\u5f7c\u65f6\u90fd\u4f4d\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u8fd9\u4f7f\u5f97\u7528\u6237\u7684\u547d\u540d\u5f88\u5bb9\u6613\u548c\u6807\u51c6\u5e93\u4e2d\u7684\u540d\u79f0\u53d1\u751f\u51b2\u7a81\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u5728\u67d0\u4e2a C++\u7248\u672c\u4e0b\u53ef\u4ee5\u8fd0\u884c\u7684\u7a0b\u5e8f\uff0c\u5f53\u4f7f\u7528\u4e86\u65b0\u7248\u672c\u7684 C++ \u65f6\u4e5f\u5f88\u53ef\u80fd\u65e0\u6cd5\u5de5\u4f5c\u3002 \u547d\u540d\u7a7a\u95f4\u7684\u6807\u51c6\u5316\u662f\u5728 1995 \u5e74\u5b8c\u6210\u7684\uff0c\u4ece\u90a3\u65f6\u8d77\uff0c\u6240\u6709\u6807\u51c6\u5e93\u4e2d\u63d0\u4f9b\u7684\u529f\u80fd\uff0c\u90fd\u88ab\u79fb\u52a8\u5230\u4e86 std \u547d\u540d\u7a7a\u95f4\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4e0d\u5177\u6709 std:: \u524d\u7f00\u7684\u4ee3\u7801\u5c31\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u4e86\u3002 \u7ef4\u62a4\u8fc7\u5927\u578b\u9879\u76ee\u7684\u4eba\u90fd\u77e5\u9053\uff0c\u4efb\u4f55\u4e00\u70b9\u6539\u52a8\u90fd\u6709\u53ef\u80fd\u7834\u574f\u6574\u4e2a\u7a0b\u5e8f\uff08\u4e0d\u8bba\u6539\u52a8\u662f\u591a\u4e48\u7684\u4e0d\u8d77\u773c\uff09\u3002\u56e0\u6b64\uff0c\u4e3a\u6240\u6709\u76f8\u5173\u7684\u6807\u8bc6\u7b26\u6dfb\u52a0 std:: \u524d\u7f00\u7684\u98ce\u9669\u975e\u5e38\u5927\u3002\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u63d0\u4f9b\u4e00\u79cd\u66f4\u52a0\u6709\u6548\u7684\u65b9\u5f0f\u3002 \u65f6\u95f4\u4e00\u6643\u5230\u4e86\u4eca\u5929\u2014\u2014\u5982\u679c\u4f60\u5927\u91cf\u4f7f\u7528\u4e86\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u529f\u80fd\uff0c\u4e3a\u6bcf\u4e00\u4e2a\u76f8\u5173\u7684\u6807\u8bc6\u7b26\u6dfb\u52a0 std:: \u4e5f\u662f\u4e00\u79cd\u975e\u5e38\u91cd\u590d\u7684\u5de5\u4f5c\uff0c\u800c\u4e14\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4f1a\u8ba9\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u53d8\u5dee\u3002 \u4e3a\u6b64\uff0cC++ \u4e3a\u8fd9\u4e9b\u95ee\u9898\u63d0\u4f9b\u4e86\u4e00\u79cd\u89e3\u51b3\u65b9\u6cd5\uff0c\u5373 using \u8bed\u53e5\u3002 \u5728\u7ee7\u7eed\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u6765\u4ecb\u7ecd\u4e24\u4e2a\u672f\u8bed \u9650\u5b9a\u540d\u548c\u975e\u9650\u5b9a\u540d \u4e00\u4e2a\u53d8\u91cf\u540d\uff08\u51fd\u6570\u540d\uff09\u53ef\u4ee5\u662f\u9650\u5b9a\u540d\uff08qualified\uff09\u6216\u8005\u975e\u9650\u5b9a\u540d\uff08unqualified\uff09\u3002 \u5305\u542b\u4e86\u4f5c\u7528\u57df\u4fe1\u606f\u7684\u540d\u5b57\uff0c\u79f0\u4e3a\u9650\u5b9a\u540d\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 ( :: )\u53ef\u4ee5\u521b\u5efa\u9650\u5b9a\u540d\u3002\u4f8b\u5982\uff1a 1 2 std :: cout // identifier cout is qualified by namespace std :: foo // identifier foo is qualified by the global namespace \u6269\u5c55\u9605\u8bfb \u7ed3\u5408\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u548c\u7c7b\u540d\u4e5f\u53ef\u4ee5\u521b\u5efa\u9650\u5b9a\u540d\uff0c\u6216\u8005\u5bf9\u7c7b\u5bf9\u8c61\u4f7f\u7528\u6210\u5458\u9009\u62e9\u64cd\u4f5c\u7b26\uff08 . \u6216 -> \uff09\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 class C ; // some class C :: s_member ; // s_member is qualified by class C obj . x ; // x is qualified by class object obj ptr -> y ; // y is qualified by pointer to class object ptr \u800c\u4e0d\u5177\u6709\u4f5c\u7528\u57df\u9650\u5b9a\u7b26\u7684\u53d8\u91cf\u540d\uff0c\u5219\u4e3a\u975e\u9650\u5b9a\u540d\u3002\u4f8b\u5982 cout \u548c x \u90fd\u662f\u975e\u9650\u5b9a\u540d\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e0d\u5305\u542b\u4efb\u4f55\u7684\u4f5c\u7528\u57df\u4fe1\u606f\u3002 Using \u58f0\u660e \u51cf\u5c11\u91cd\u590d\u8f93\u5165 std:: \u7684\u4e00\u4e2a\u65b9\u6cd5\u662f\u4f7f\u7528 using \u58f0\u660e\u8bed\u53e5\u3002using \u58f0\u660e\u8bed\u53e5\u4f7f\u6211\u4eec\u53ef\u4ee5\u5c06\u975e\u9650\u5b9a\u540d\u4f5c\u4e3a\u9650\u5b9a\u540d\u7684\u522b\u540d\u6765\u4f7f\u7528\u3002 \u4e0b\u9762\u8fd9\u4e2a\u7ecf\u5178\u7684 Hello World \u7a0b\u5e8f\uff0c\u5728\u7b2c\u4e94\u884c\u4f7f\u7528\u4e86 using \u58f0\u660e\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { using std :: cout ; // this using declaration tells the compiler that cout should resolve to std::cout cout << \"Hello world!\" ; // so no std:: prefix is needed here! return 0 ; } // the using declaration expires here using std:: cout; \u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u6211\u4eec\u8981\u4f7f\u7528 std namespace \u4e2d\u7684 cout \uff0c\u6240\u4ee5\u5728\u4efb\u4f55\u4f7f\u7528 cout \u7684\u5730\u65b9\uff0c\u7f16\u8bd1\u5668\u90fd\u4f1a\u5047\u5b9a\u5b83\u7b49\u4ef7\u4e8e std:: cout \u3002\u5982\u679c\u5728\u9047\u5230 std:: cout \u548c\u5176\u4ed6 cout \u77db\u76fe\u7684\u5730\u65b9\uff0c\u5219\u4f18\u5148\u8ba4\u4e3a\u662f std:: cout \u3002\u56e0\u6b64\uff0c\u5728\u7b2c\u516d\u884c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 cout \u800c\u65e0\u9700\u4f7f\u7528 std:: cout \u3002 \u4ece\u8fd9\u4e2a\u5c0f\u4f8b\u5b50\u6211\u4eec\u5f88\u96be\u770b\u51fa using \u8bed\u53e5\u80fd\u591f\u5e26\u6765\u7684\u6536\u76ca\uff0c\u4f46\u662f\u5f53\u4f60\u5728\u51fd\u6570\u4e2d\u5927\u91cf\u4f7f\u7528 cout \u7684\u65f6\u5019\uff0c using \u58f0\u660e\u53ef\u4ee5\u8ba9\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u53d8\u5f97\u66f4\u597d\u3002\u6ce8\u610f\uff0c\u5bf9\u4e8e\u4e0d\u540c\u7684\u53d8\u91cf\u540d\uff0c\u9700\u8981\u5355\u72ec\u7684 using \u58f0\u660e\uff08\u4f8b\u5982 std:: cout \u662f\u4e00\u4e2a\uff0c std:: cin \u662f\u53e6\u4e00\u4e2a\uff09\u3002 \u5c3d\u7ba1\u8fd9\u79cd\u65b9\u6cd5\u6ca1\u6709\u4f7f\u7528 std:: \u524d\u7f00\u90a3\u4e48\u660e\u786e\uff0c\u4f46\u901a\u5e38\u4e5f\u88ab\u8ba4\u4e3a\u662f\u4e00\u79cd\u5b89\u5168\u7684\u3001\u53ef\u63a5\u53d7\u7684\u65b9\u6cd5\uff08\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\uff09\u3002 Using \u6307\u4ee4 \u53e6\u5916\u4e00\u79cd\u7b80\u5316\u65b9\u5f0f\u662f\u4f7f\u7528 using \u6307\u4ee4\uff0c\u5b83\u53ef\u4ee5\u5c06\u8be5\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u90fd\u5bfc\u5165\u5230\u4f7f\u7528\u4e86\u8be5 using \u6307\u4ee4\u7684\u4f5c\u7528\u57df\u3002 \u6269\u5c55\u9605\u8bfb \u4ece\u6280\u672f\u89d2\u5ea6\u6765\u8bb2\uff0c\u4f7f\u7528 using \u6307\u4ee4\u5176\u5b9e\u5e76\u6ca1\u6709\u5c06\u6807\u8bc6\u7b26\u5bfc\u5165\u5230\u5f53\u524d\u7684\u4f5c\u7528\u57df\u2014\u2014\u5b9e\u9645\u4e0a\u5b83\u4eec\u88ab\u5bfc\u5165\u5230\u5916\u5c42\u4f5c\u7528\u57df\uff08\u5173\u4e8e\u5916\u5c42\u4f5c\u7528\u57df\u7684\u8be6\u7ec6\u4fe1\u606f\u53ef\u4ee5\u53c2\u8003 \u8fd9\u91cc \uff09\u3002\u4e0d\u8fc7\uff0c\u5728\u5916\u5c42\u4f5c\u7528\u57df\u4e2d\u5e76\u4e0d\u80fd\u8bbf\u95ee\u8fd9\u4e9b\u53d8\u91cf\u540d\uff0c\u5b83\u4eec\u53ea\u80fd\u5728\u4f7f\u7528\u4e86 using \u6307\u4ee4\u7684\u4f5c\u7528\u57df\u4e2d\u8bbf\u95ee\u3002 \u4ece\u5b9e\u9645\u6548\u679c\u6765\u770b\uff0c\u4f7f\u7528 using \u6307\u4ee4\u7684\u884c\u4e3a\u770b\u4e0a\u53bb\u5c31\u50cf\u662f\u53d8\u91cf\u540d\u88ab\u5bfc\u5165\u5230\u4e86\u5f53\u524d\u4f5c\u7528\u57df\u3002\u4e3a\u4e86\u4e0d\u628a\u4e8b\u60c5\u641e\u590d\u6742\uff0c\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4f1a\u6cbf\u7528\u8fd9\u79cd\u7b80\u5316\u7684\u8bf4\u6cd5\uff0c\u5373\u53d8\u91cf\u540d\u88ab \u5bfc\u5165 \u5230\u4e86\u5f53\u524d\u4f5c\u7528\u57df\u3002 \u518d\u56de\u8fc7\u5934\u770b Hello World \u7a0b\u5e8f\uff0c\u7b2c\u4e94\u884c\u4f7f\u7528\u4e86 using \u6307\u4ee4\uff1a 1 2 3 4 5 6 7 8 #include int main () { using namespace std ; // this using directive tells the compiler to import all names from namespace std into the current namespace without qualification cout << \"Hello world!\" ; // so no std:: prefix is needed here return 0 ; } using namespace std; \u544a\u8bc9\u7f16\u8bd1\u5668\u5c06 std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684 \u5168\u90e8 \u6807\u8bc6\u7b26\u5bfc\u5165\u5230\u5f53\u524d\u4f5c\u7528\u57df\uff08\u4f8b\u5982 main() \u51fd\u6570\uff09\u3002\u5f53\u6211\u4eec\u4f7f\u7528\u975e\u9650\u5b9a\u6807\u8bc6\u7b26 cout \u7684\u65f6\u5019\uff0c\u5b83\u5c31\u4f1a\u88ab\u89e3\u6790\u4e3a\u5bfc\u5165\u7684 std:: cout \u3002 Using \u6307\u4ee4\u662f\u4e3a\u4e86\u90a3\u4e9b\u5728\u547d\u540d\u7a7a\u95f4\u53d1\u660e\u524d\u5c31\u5b58\u5728\u7684\u9879\u76ee\u800c\u8bbe\u8ba1\u7684\u3002\u5728\u8fd9\u79cd\u53e4\u8001\u7684\u9879\u76ee\u4e2d\uff0c\u6709\u4e86 using \u6307\u4ee4\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u4e0d\u5fc5\u9010\u4e00\u5730\u5c06\u975e\u9650\u5b9a\u540d\u4fee\u6539\u4e3a\u9650\u5b9a\u540d\uff08\u8fd9\u4e48\u505a\u98ce\u9669\u975e\u5e38\u5927\uff09\uff0c\u53ea\u9700\u5c06 using namespace std; \u653e\u7f6e\u5728\u6587\u4ef6\u7684\u5f00\u59cb\u5904\uff0c\u90a3\u4e48\u6240\u6709\u7684\u5df2\u7ecf\u88ab\u79fb\u52a8\u5230\u4e86 std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\uff0c\u4ecd\u7136\u53ef\u4ee5\u4ee5\u975e\u9650\u5b9a\u540d\u7684\u65b9\u5f0f\u6765\u4f7f\u7528\u3002 Using \u6307\u4ee4\u5e26\u6765\u7684\u95ee\u9898 (\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528 \u201cusing namespace std;\u201d) \u5728\u73b0\u4ee3 C++ \u4e2d\uff0c\u4f7f\u7528 using \u6307\u4ee4\u901a\u5e38\u662f\u5f0a\u5927\u4e8e\u5229\u7684\uff08\u9664\u4e86\u80fd\u5c11\u6253\u5b57\u4ee5\u5916\uff09\u3002\u56e0\u4e3a\u4f7f\u7528 using \u6307\u4ee4\u5bfc\u5165\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6240\u6709\u6807\u8bc6\u7b26\u4f1a\u6781\u5927\u5730\u63d0\u9ad8\u547d\u540d\u51b2\u7a81\u7684\u53ef\u80fd\uff08\u5c24\u5176\u662f\u5bfc\u5165 std \uff09\u3002 \u901a\u8fc7\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230 using \u6307\u4ee4\u53ef\u80fd\u5e26\u6765\u7684\u4e8c\u4e49\u6027\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include namespace a { int x { 10 }; } namespace b { int x { 20 }; } int main () { using namespace a ; using namespace b ; std :: cout << x << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u51b3\u5b9a\u8fd9\u91cc\u7684 main \u4e2d\u7684 x \u5230\u5e95\u6307\u7684\u662f a:: x \u8fd8\u662f b:: x \u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u4f1a\u56e0\u4e3a\u201c\u6b67\u4e49\u7b26\u53f7\u201d\u9519\u8bef\u800c\u65e0\u6cd5\u7f16\u8bd1\u3002\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u529e\u6cd5\u662f\u79fb\u9664\u5176\u4e2d\u4e00\u4e2a using \u8bed\u53e5\uff0c\u4f7f\u7528 using \u58f0\u660e\uff0c\u6216\u8005\u76f4\u63a5\u4f7f\u7528\u663e\u5f0f\u7684\u4f5c\u7528\u57df\u4fee\u9970\u7b26( a:: \u6216 b:: )\u3002 \u518d\u770b\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // imports the declaration of std::cout int cout () // declares our own \"cout\" function { return 5 ; } int main () { using namespace std ; // makes std::cout accessible as \"cout\" cout << \"Hello, world!\" ; // uh oh! Which cout do we want here? The one in the std namespace or the one we defined above? return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u786e\u5b9a cout \u5230\u5e95\u6307\u7684\u662f std:: cout \u8fd8\u662f\u6211\u4eec\u5b9a\u4e49\u7684 cout \u51fd\u6570\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u4ecd\u7136\u4f1a\u56e0\u4e3a\u201c\u6b67\u4e49\u7b26\u53f7\u201d\u9519\u8bef\u800c\u65e0\u6cd5\u7f16\u8bd1\u3002\u5982\u679c\u6211\u4eec\u4f7f\u7528\u524d\u7f00 std:: \uff0c\u50cf\u8fd9\u6837\uff1a 1 std :: cout << \"Hello, world!\" ; // tell the compiler we mean std::cout \u6216\u8005\u4f7f\u7528 using \u58f0\u660e\u800c\u4e0d\u662f using \u6307\u4ee4\uff1a 1 2 using std :: cout ; // tell the compiler that cout means std::cout cout << \"Hello, world!\" ; // so this means std::cout \u7a0b\u5e8f\u5c06\u53ef\u4ee5\u987a\u5229\u5b8c\u6210\u7f16\u8bd1\u3002 \u867d\u7136\u4f60\u5f88\u53ef\u80fd\u5e76\u4e0d\u4f1a\u5b9a\u4e49\u4e00\u4e2a\u53eb\u505a cout \u7684\u51fd\u6570\uff0c\u4f46\u662f std \u547d\u540d\u7a7a\u95f4\u4e2d\u6709\u6210\u767e\u4e0a\u5343\u7684\u6807\u8bc6\u7b26\u53ef\u80fd\u4f1a\u4e0e\u4f60\u5b9a\u4e49\u7684\u6807\u8bc6\u7b26\u4ea7\u751f\u51b2\u7a81\uff0c\u6bd4\u65b9\u8bf4\u201ccount\u201d, \u201cmin\u201d, \u201cmax\u201d, \u201csearch\u201d, \u201csort\u201d \u7b49\u7b49\u3002 \u800c\u4e14\uff0c\u5373\u4f7f using \u6307\u4ee4\u4eca\u5929\u6ca1\u6709\u5e26\u6765\u547d\u540d\u51b2\u7a81\uff0c\u4e5f\u96be\u4fdd\u4e0d\u5728\u672a\u6765\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u7684\u4ee3\u7801\u4f7f\u7528 using \u5bfc\u5165\u4e86\u67d0\u4e2a\u547d\u540d\u7a7a\u95f4\uff0c\u7136\u540e\u67d0\u5929\u8be5\u5e93\u66f4\u65b0\u4e86\uff0c\u6dfb\u52a0\u4e86\u4e00\u4e9b\u65b0\u7684\u53d8\u91cf\u540d\uff0c\u90a3\u6ca1\u51c6\u5c31\u4f1a\u548c\u4f60\u7684\u4ee3\u7801\u4ea7\u751f\u51b2\u7a81\u54af\u3002 \u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u9634\u9669\u7684\u95ee\u9898\u4e5f\u53ef\u80fd\u53d1\u751f\u3002\u5f53\u5e93\u66f4\u65b0\u4e4b\u540e\uff0c\u5b83\u5b9a\u4e49\u7684\u4e00\u4e9b\u65b0\u7684\u51fd\u6570\uff0c\u53ef\u80fd\u4e0d\u4ec5\u51fd\u6570\u540d\u548c\u6211\u4eec\u5b9a\u4e49\u7684\u51fd\u6570\u6709\u51b2\u7a81\uff0c\u751a\u81f3\u5b83\u66f4\u7b26\u5408\u67d0\u79cd\u67d0\u5904\u7684\u51fd\u6570\u8c03\u7528\u65b9\u5f0f\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c31\u53ef\u80fd\u4f1a\u4f7f\u7528\u8be5\u51fd\u6570\u6765\u66ff\u6362\u6211\u4eec\u7684\u51fd\u6570\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u7684\u884c\u4e3a\u5c06\u4f1a\u88ab\u6539\u53d8\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a ```cpp title=foolib.h\"\" namespace foo { // pretend there is some useful code that we use here } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ```cpp title=\"main.cpp\" #include #include // a third-party library, thus angled brackets used int someFcn(double) { return 1; } int main() { using namespace foo; // Because we're lazy and want to access foo:: qualified names without typing the foo:: prefix std::cout << someFcn(0); // \u5b57\u9762\u91cf\u5e94\u5f53\u4f7f\u7528 0.0 \u4f46\u88ab\u8bef\u7528\u4e3a 0\uff0c\u8fd9\u662f\u4e00\u4e2a\u5f88\u5bb9\u6613\u72af\u7684\u9519\u8bef\u3002 return 0; } \u7a0b\u5e8f\u8fd0\u884c\u540e\u4f1a\u6253\u5370 1 \u3002 \u73b0\u5728\uff0c\u66f4\u65b0 foolib\uff08\u4f7f\u7528\u66f4\u65b0\u7684 foolib.h \uff09\uff0c\u7a0b\u5e8f\u7f16\u7a0b\u4e0b\u9762\u8fd9\u6837\uff1a foolib.h 1 2 3 4 5 6 7 8 9 10 namespace foo { // newly introduced function int someFcn ( int ) { return 2 ; } // pretend there is some useful code that we use here } main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int someFcn ( double ) { return 1 ; } int main () { using namespace foo ; // Because we're lazy and want to access foo:: qualified names without typing the foo:: prefix std :: cout << someFcn ( 0 ); // \u5b57\u9762\u91cf\u5e94\u5f53\u4f7f\u7528 0.0 \u4f46\u88ab\u8bef\u7528\u4e3a 0\uff0c\u8fd9\u662f\u4e00\u4e2a\u5f88\u5bb9\u6613\u72af\u7684\u9519\u8bef\u3002 return 0 ; } main.cpp \u6587\u4ef6\u5b8c\u5168\u6ca1\u6709\u6539\u53d8\uff0c\u4f46\u662f\u7a0b\u5e8f\u5374\u8f93\u51fa\u4e86 2\uff01 \u5f53\u7f16\u8bd1\u5668\u9047\u5230\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u5b83\u9700\u8981\u786e\u5b9a\u4e0e\u51fd\u6570\u8c03\u7528\u6700\u5339\u914d\u7684\u51fd\u6570\u5b9a\u4e49\u662f\u4ec0\u4e48\u3002\u5f53\u6709\u591a\u4e2a\u5b9a\u4e49\u53ef\u4f9b\u7f16\u8bd1\u5668\u9009\u62e9\u65f6\uff0c\u5b83\u4e00\u5b9a\u4f1a\u9009\u62e9\u65e0\u9700\u53c2\u6570\u8f6c\u6362\u7684\u90a3\u4e00\u4e2a\u3002\u56e0\u4e3a 0 \u662f\u4e00\u4e2a\u6574\u578b\uff0cC++ \u4e00\u5b9a\u4f1a\u9009\u62e9\u66f4\u52a0\u5339\u914d\u7684\u65b0\u5b9a\u4e49\u7684 someFcn(int) (\u65e0\u9700\u8f6c\u6362) \u800c\u4e0d\u662f someFcn(double) (\u9700\u8981\u5c06 int \u8f6c\u6362\u4e3a double)\u3002\u8fd9\u6837\u5c31\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u884c\u4e3a\u88ab\u610f\u5916\u5730\u6539\u53d8\u3002 \u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u4f7f\u7528\u7684\u662f using \u58f0\u660e\u6216\u8005\u4f5c\u7528\u57df\u4fee\u9970\u7b26\u7684\u8bdd\uff0c\u5c31\u5b8c\u5168\u4e0d\u4f1a\u53d1\u751f\u8fd9\u7c7b\u95ee\u9898\u3002 \u6700\u540e\uff0c\u5bf9\u4e8e\u7a0b\u5e8f\u7684\u8bfb\u8005\u6765\u8bf4\uff0c\u5982\u679c\u4e0d\u4f7f\u7528\u4f5c\u7528\u57df\u4fee\u9970\u7b26\u7684\u8bdd\uff0c\u8bfb\u8005\u5c06\u96be\u786e\u5b9a\u67d0\u4e2a\u51fd\u6570\u5230\u5e95\u662f\u81ea\u5b9a\u4e49\u7684\uff0c\u8fd8\u662f\u5c5e\u4e8e\u67d0\u4e2a\u5e93\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f7f\u7528 using \u6307\u4ee4\uff1a 1 2 3 4 5 6 using namespace ns ; int main () { foo (); // is this foo a user-defined function, or part of the ns library? } \u8fd9\u79cd\u60c5\u51b5\u4e0b\u5c31\u5f88\u96be\u786e\u5b9a foo() \u6307\u7684\u662f ns:: foo() \u8fd8\u662f\u7528\u6237\u81ea\u5b9a\u4e49\u7684 foo() \u51fd\u6570\u3002\u5728\u4f7f\u7528\u73b0\u4ee3 IDE \u65f6\uff0c\u5982\u679c\u5c06\u9f20\u6807\u60ac\u6d6e\u5728\u53d8\u91cf\u540d\u4e0a\u65f6\uff0cIDE \u4e00\u822c\u90fd\u53ef\u4ee5\u5e2e\u6211\u4eec\u786e\u5b9a\u6807\u8bc6\u7b26\u5230\u5e95\u6307\u7684\u662f\u4ec0\u4e48\uff0c\u4f46\u662f\u8fd9\u6837\u663e\u7136\u5f88\u9ebb\u70e6\uff0c\u6bd5\u7adf\u6bcf\u4e2a\u53d8\u91cf\u540d\u90fd\u60ac\u6d6e\u9f20\u6807\u770b\u4e00\u4e0b\u662f\u5f88\u9ebb\u70e6\u7684\u3002 \u5982\u679c\u4e0d\u662f\u4f7f\u7528 using \u6307\u4ee4\uff0c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u5c31\u4f1a\u597d\u5f88\u591a\uff1a 1 2 3 4 5 int main () { ns :: foo (); // clearly part of the ns library foo (); // likely a user-defined function } \u5728\u8fd9\u4e2a\u7248\u672c\u4e2d\uff0c ns:: foo() \u663e\u7136\u662f\u8c03\u7528\u5e93\u51fd\u6570\u3002\u800c foo() \u5219\u662f\u8c03\u7528\u7528\u6237\u81ea\u5b9a\u4e49\u51fd\u6570\u3002 Using \u58f0\u660e\u548c\u6307\u4ee4\u7684\u4f5c\u7528\u57df \u5982\u679c\u5728\u8bed\u53e5\u5757\u4e2d\u4f7f\u7528 using \u58f0\u660e\u6216 using \u6307\u4ee4\uff0c\u90a3\u5bf9\u5e94\u7684\u53d8\u91cf\u540d\u53ea\u5728\u8be5\u4f5c\u7528\u57df\u4e2d\u6709\u6548\u3002\u8fd9\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u7279\u6027\uff0c\u56e0\u4e3a\u5b83\u5c06\u547d\u540d\u51b2\u7a81\u7684\u98ce\u9669\u9650\u5b9a\u5728\u4e86\u8bed\u53e5\u5757\u4e2d\u3002 \u5982\u679c\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u4f7f\u7528 using \u58f0\u660e\u6216 using \u6307\u4ee4\uff0c\u53d8\u91cf\u540d\u5c06\u5f71\u54cd\u5230\u6574\u4e2a\u6587\u4ef6\uff08\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\uff09\u3002 \u53d6\u6d88\u6216\u8005\u66ff\u6362 using \u8bed\u53e5 \u4e00\u65e6\u4f7f\u7528\u4e86 using \u8bed\u53e5\uff0c\u5728\u76f8\u540c\u7684\u4f5c\u7528\u57df\u4e2d\u5c06\u6ca1\u6709\u529e\u6cd5\u5bf9\u5176\u8fdb\u884c\u53d6\u6d88\u6216\u66ff\u6362\u3002 1 2 3 4 5 6 7 8 9 int main () { using namespace foo ; // \u6ca1\u6709\u529e\u6cd5\u53d6\u6d88 \"using namespace foo\" \u7684\u6548\u679c // \u4e5f\u6ca1\u6709\u529e\u6cd5\u4f7f\u7528\u5176\u4ed6 using \u8bed\u53e5\u66ff\u6362 return 0 ; } // using namespace foo ends here \u5982\u679c\u5e0c\u671b\u9650\u5236 using \u8bed\u53e5\u7684\u4f5c\u7528\u8303\u56f4\uff0c\u53ef\u4ee5\u5c06\u5b83\u653e\u5728\u4e00\u4e2a\u8bed\u53e5\u5757\u4e2d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int main () { { using namespace foo ; // calls to foo:: stuff here } // using namespace foo expires { using namespace Goo ; // calls to Goo:: stuff here } // using namespace Goo expires return 0 ; } \u5f53\u7136\uff0c\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u53ef\u4ee5\u4e00\u52b3\u6c38\u9038\u5730\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u3002 using \u8bed\u53e5\u7684\u6700\u4f73\u5b9e\u8df5 \u9664\u975e\u9047\u5230\u975e\u5e38\u7279\u6b8a\u7684\u60c5\u51b5\uff0c\u8bf7\u907f\u514d\u4f7f\u7528 using \u6307\u4ee4 (\u5c24\u5176\u662f using namespace std; )\u3002\u5728\u8bed\u53e5\u5757\u4e2d\u4f7f\u7528 using \u58f0\u660e\u901a\u5e38\u88ab\u8ba4\u4e3a\u662f\u5b89\u5168\u7684\u3002\u7edd\u5bf9\u4e0d\u8981\u5728\u5934\u6587\u4ef6\u7684\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u4f7f\u7528\u5b83\u4eec\uff0c\u5728\u6e90\u6587\u4ef6\u7684\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u6709\u9650\u5730\u4f7f\u7528\u5b83\u4eec\u3002 \u6700\u4f73\u5b9e\u8df5 \u660e\u786e\u6307\u5b9a\u4f5c\u7528\u57df\u597d\u8fc7 using \u8bed\u53e5\u3002\u5c3d\u53ef\u80fd\u5730\u907f\u514d\u4f7f\u7528 using \u6307\u4ee4\u3002\u5728\u8bed\u53e5\u5757\u4e2d\u4f7f\u7528 using \u58f0\u660e\u901a\u5e38\u662f\u53ef\u4ee5\u7684\u3002 \u76f8\u5173\u5185\u5bb9 using \u5173\u952e\u5b57\u8fd8\u53ef\u4ee5\u7528\u6765\u5b9a\u4e49\u7c7b\u578b\u522b\u540d\uff0c\u6211\u4eec\u4f1a\u5728 8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d \u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u7c7b\u578b\u522b\u540d\u3002","title":"6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4"},{"location":"6-12-Using-declarations-and-using%20directives/#_1","text":"\u5728 C++ \u5f00\u59cb\u652f\u6301\u547d\u540d\u7a7a\u95f4\u4e4b\u524d\uff0c\u6240\u6709\u73b0\u5728\u4f4d\u4e8e std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\uff0c\u5f7c\u65f6\u90fd\u4f4d\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u8fd9\u4f7f\u5f97\u7528\u6237\u7684\u547d\u540d\u5f88\u5bb9\u6613\u548c\u6807\u51c6\u5e93\u4e2d\u7684\u540d\u79f0\u53d1\u751f\u51b2\u7a81\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u5728\u67d0\u4e2a C++\u7248\u672c\u4e0b\u53ef\u4ee5\u8fd0\u884c\u7684\u7a0b\u5e8f\uff0c\u5f53\u4f7f\u7528\u4e86\u65b0\u7248\u672c\u7684 C++ \u65f6\u4e5f\u5f88\u53ef\u80fd\u65e0\u6cd5\u5de5\u4f5c\u3002 \u547d\u540d\u7a7a\u95f4\u7684\u6807\u51c6\u5316\u662f\u5728 1995 \u5e74\u5b8c\u6210\u7684\uff0c\u4ece\u90a3\u65f6\u8d77\uff0c\u6240\u6709\u6807\u51c6\u5e93\u4e2d\u63d0\u4f9b\u7684\u529f\u80fd\uff0c\u90fd\u88ab\u79fb\u52a8\u5230\u4e86 std \u547d\u540d\u7a7a\u95f4\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4e0d\u5177\u6709 std:: \u524d\u7f00\u7684\u4ee3\u7801\u5c31\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u4e86\u3002 \u7ef4\u62a4\u8fc7\u5927\u578b\u9879\u76ee\u7684\u4eba\u90fd\u77e5\u9053\uff0c\u4efb\u4f55\u4e00\u70b9\u6539\u52a8\u90fd\u6709\u53ef\u80fd\u7834\u574f\u6574\u4e2a\u7a0b\u5e8f\uff08\u4e0d\u8bba\u6539\u52a8\u662f\u591a\u4e48\u7684\u4e0d\u8d77\u773c\uff09\u3002\u56e0\u6b64\uff0c\u4e3a\u6240\u6709\u76f8\u5173\u7684\u6807\u8bc6\u7b26\u6dfb\u52a0 std:: \u524d\u7f00\u7684\u98ce\u9669\u975e\u5e38\u5927\u3002\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u63d0\u4f9b\u4e00\u79cd\u66f4\u52a0\u6709\u6548\u7684\u65b9\u5f0f\u3002 \u65f6\u95f4\u4e00\u6643\u5230\u4e86\u4eca\u5929\u2014\u2014\u5982\u679c\u4f60\u5927\u91cf\u4f7f\u7528\u4e86\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u529f\u80fd\uff0c\u4e3a\u6bcf\u4e00\u4e2a\u76f8\u5173\u7684\u6807\u8bc6\u7b26\u6dfb\u52a0 std:: \u4e5f\u662f\u4e00\u79cd\u975e\u5e38\u91cd\u590d\u7684\u5de5\u4f5c\uff0c\u800c\u4e14\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4f1a\u8ba9\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u53d8\u5dee\u3002 \u4e3a\u6b64\uff0cC++ \u4e3a\u8fd9\u4e9b\u95ee\u9898\u63d0\u4f9b\u4e86\u4e00\u79cd\u89e3\u51b3\u65b9\u6cd5\uff0c\u5373 using \u8bed\u53e5\u3002 \u5728\u7ee7\u7eed\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u6765\u4ecb\u7ecd\u4e24\u4e2a\u672f\u8bed","title":"\u5386\u53f2\u8bfe"},{"location":"6-12-Using-declarations-and-using%20directives/#_2","text":"\u4e00\u4e2a\u53d8\u91cf\u540d\uff08\u51fd\u6570\u540d\uff09\u53ef\u4ee5\u662f\u9650\u5b9a\u540d\uff08qualified\uff09\u6216\u8005\u975e\u9650\u5b9a\u540d\uff08unqualified\uff09\u3002 \u5305\u542b\u4e86\u4f5c\u7528\u57df\u4fe1\u606f\u7684\u540d\u5b57\uff0c\u79f0\u4e3a\u9650\u5b9a\u540d\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 ( :: )\u53ef\u4ee5\u521b\u5efa\u9650\u5b9a\u540d\u3002\u4f8b\u5982\uff1a 1 2 std :: cout // identifier cout is qualified by namespace std :: foo // identifier foo is qualified by the global namespace \u6269\u5c55\u9605\u8bfb \u7ed3\u5408\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u548c\u7c7b\u540d\u4e5f\u53ef\u4ee5\u521b\u5efa\u9650\u5b9a\u540d\uff0c\u6216\u8005\u5bf9\u7c7b\u5bf9\u8c61\u4f7f\u7528\u6210\u5458\u9009\u62e9\u64cd\u4f5c\u7b26\uff08 . \u6216 -> \uff09\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 class C ; // some class C :: s_member ; // s_member is qualified by class C obj . x ; // x is qualified by class object obj ptr -> y ; // y is qualified by pointer to class object ptr \u800c\u4e0d\u5177\u6709\u4f5c\u7528\u57df\u9650\u5b9a\u7b26\u7684\u53d8\u91cf\u540d\uff0c\u5219\u4e3a\u975e\u9650\u5b9a\u540d\u3002\u4f8b\u5982 cout \u548c x \u90fd\u662f\u975e\u9650\u5b9a\u540d\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e0d\u5305\u542b\u4efb\u4f55\u7684\u4f5c\u7528\u57df\u4fe1\u606f\u3002","title":"\u9650\u5b9a\u540d\u548c\u975e\u9650\u5b9a\u540d"},{"location":"6-12-Using-declarations-and-using%20directives/#using","text":"\u51cf\u5c11\u91cd\u590d\u8f93\u5165 std:: \u7684\u4e00\u4e2a\u65b9\u6cd5\u662f\u4f7f\u7528 using \u58f0\u660e\u8bed\u53e5\u3002using \u58f0\u660e\u8bed\u53e5\u4f7f\u6211\u4eec\u53ef\u4ee5\u5c06\u975e\u9650\u5b9a\u540d\u4f5c\u4e3a\u9650\u5b9a\u540d\u7684\u522b\u540d\u6765\u4f7f\u7528\u3002 \u4e0b\u9762\u8fd9\u4e2a\u7ecf\u5178\u7684 Hello World \u7a0b\u5e8f\uff0c\u5728\u7b2c\u4e94\u884c\u4f7f\u7528\u4e86 using \u58f0\u660e\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { using std :: cout ; // this using declaration tells the compiler that cout should resolve to std::cout cout << \"Hello world!\" ; // so no std:: prefix is needed here! return 0 ; } // the using declaration expires here using std:: cout; \u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u6211\u4eec\u8981\u4f7f\u7528 std namespace \u4e2d\u7684 cout \uff0c\u6240\u4ee5\u5728\u4efb\u4f55\u4f7f\u7528 cout \u7684\u5730\u65b9\uff0c\u7f16\u8bd1\u5668\u90fd\u4f1a\u5047\u5b9a\u5b83\u7b49\u4ef7\u4e8e std:: cout \u3002\u5982\u679c\u5728\u9047\u5230 std:: cout \u548c\u5176\u4ed6 cout \u77db\u76fe\u7684\u5730\u65b9\uff0c\u5219\u4f18\u5148\u8ba4\u4e3a\u662f std:: cout \u3002\u56e0\u6b64\uff0c\u5728\u7b2c\u516d\u884c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 cout \u800c\u65e0\u9700\u4f7f\u7528 std:: cout \u3002 \u4ece\u8fd9\u4e2a\u5c0f\u4f8b\u5b50\u6211\u4eec\u5f88\u96be\u770b\u51fa using \u8bed\u53e5\u80fd\u591f\u5e26\u6765\u7684\u6536\u76ca\uff0c\u4f46\u662f\u5f53\u4f60\u5728\u51fd\u6570\u4e2d\u5927\u91cf\u4f7f\u7528 cout \u7684\u65f6\u5019\uff0c using \u58f0\u660e\u53ef\u4ee5\u8ba9\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u53d8\u5f97\u66f4\u597d\u3002\u6ce8\u610f\uff0c\u5bf9\u4e8e\u4e0d\u540c\u7684\u53d8\u91cf\u540d\uff0c\u9700\u8981\u5355\u72ec\u7684 using \u58f0\u660e\uff08\u4f8b\u5982 std:: cout \u662f\u4e00\u4e2a\uff0c std:: cin \u662f\u53e6\u4e00\u4e2a\uff09\u3002 \u5c3d\u7ba1\u8fd9\u79cd\u65b9\u6cd5\u6ca1\u6709\u4f7f\u7528 std:: \u524d\u7f00\u90a3\u4e48\u660e\u786e\uff0c\u4f46\u901a\u5e38\u4e5f\u88ab\u8ba4\u4e3a\u662f\u4e00\u79cd\u5b89\u5168\u7684\u3001\u53ef\u63a5\u53d7\u7684\u65b9\u6cd5\uff08\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\uff09\u3002","title":"Using \u58f0\u660e"},{"location":"6-12-Using-declarations-and-using%20directives/#using_1","text":"\u53e6\u5916\u4e00\u79cd\u7b80\u5316\u65b9\u5f0f\u662f\u4f7f\u7528 using \u6307\u4ee4\uff0c\u5b83\u53ef\u4ee5\u5c06\u8be5\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u90fd\u5bfc\u5165\u5230\u4f7f\u7528\u4e86\u8be5 using \u6307\u4ee4\u7684\u4f5c\u7528\u57df\u3002 \u6269\u5c55\u9605\u8bfb \u4ece\u6280\u672f\u89d2\u5ea6\u6765\u8bb2\uff0c\u4f7f\u7528 using \u6307\u4ee4\u5176\u5b9e\u5e76\u6ca1\u6709\u5c06\u6807\u8bc6\u7b26\u5bfc\u5165\u5230\u5f53\u524d\u7684\u4f5c\u7528\u57df\u2014\u2014\u5b9e\u9645\u4e0a\u5b83\u4eec\u88ab\u5bfc\u5165\u5230\u5916\u5c42\u4f5c\u7528\u57df\uff08\u5173\u4e8e\u5916\u5c42\u4f5c\u7528\u57df\u7684\u8be6\u7ec6\u4fe1\u606f\u53ef\u4ee5\u53c2\u8003 \u8fd9\u91cc \uff09\u3002\u4e0d\u8fc7\uff0c\u5728\u5916\u5c42\u4f5c\u7528\u57df\u4e2d\u5e76\u4e0d\u80fd\u8bbf\u95ee\u8fd9\u4e9b\u53d8\u91cf\u540d\uff0c\u5b83\u4eec\u53ea\u80fd\u5728\u4f7f\u7528\u4e86 using \u6307\u4ee4\u7684\u4f5c\u7528\u57df\u4e2d\u8bbf\u95ee\u3002 \u4ece\u5b9e\u9645\u6548\u679c\u6765\u770b\uff0c\u4f7f\u7528 using \u6307\u4ee4\u7684\u884c\u4e3a\u770b\u4e0a\u53bb\u5c31\u50cf\u662f\u53d8\u91cf\u540d\u88ab\u5bfc\u5165\u5230\u4e86\u5f53\u524d\u4f5c\u7528\u57df\u3002\u4e3a\u4e86\u4e0d\u628a\u4e8b\u60c5\u641e\u590d\u6742\uff0c\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4f1a\u6cbf\u7528\u8fd9\u79cd\u7b80\u5316\u7684\u8bf4\u6cd5\uff0c\u5373\u53d8\u91cf\u540d\u88ab \u5bfc\u5165 \u5230\u4e86\u5f53\u524d\u4f5c\u7528\u57df\u3002 \u518d\u56de\u8fc7\u5934\u770b Hello World \u7a0b\u5e8f\uff0c\u7b2c\u4e94\u884c\u4f7f\u7528\u4e86 using \u6307\u4ee4\uff1a 1 2 3 4 5 6 7 8 #include int main () { using namespace std ; // this using directive tells the compiler to import all names from namespace std into the current namespace without qualification cout << \"Hello world!\" ; // so no std:: prefix is needed here return 0 ; } using namespace std; \u544a\u8bc9\u7f16\u8bd1\u5668\u5c06 std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684 \u5168\u90e8 \u6807\u8bc6\u7b26\u5bfc\u5165\u5230\u5f53\u524d\u4f5c\u7528\u57df\uff08\u4f8b\u5982 main() \u51fd\u6570\uff09\u3002\u5f53\u6211\u4eec\u4f7f\u7528\u975e\u9650\u5b9a\u6807\u8bc6\u7b26 cout \u7684\u65f6\u5019\uff0c\u5b83\u5c31\u4f1a\u88ab\u89e3\u6790\u4e3a\u5bfc\u5165\u7684 std:: cout \u3002 Using \u6307\u4ee4\u662f\u4e3a\u4e86\u90a3\u4e9b\u5728\u547d\u540d\u7a7a\u95f4\u53d1\u660e\u524d\u5c31\u5b58\u5728\u7684\u9879\u76ee\u800c\u8bbe\u8ba1\u7684\u3002\u5728\u8fd9\u79cd\u53e4\u8001\u7684\u9879\u76ee\u4e2d\uff0c\u6709\u4e86 using \u6307\u4ee4\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u4e0d\u5fc5\u9010\u4e00\u5730\u5c06\u975e\u9650\u5b9a\u540d\u4fee\u6539\u4e3a\u9650\u5b9a\u540d\uff08\u8fd9\u4e48\u505a\u98ce\u9669\u975e\u5e38\u5927\uff09\uff0c\u53ea\u9700\u5c06 using namespace std; \u653e\u7f6e\u5728\u6587\u4ef6\u7684\u5f00\u59cb\u5904\uff0c\u90a3\u4e48\u6240\u6709\u7684\u5df2\u7ecf\u88ab\u79fb\u52a8\u5230\u4e86 std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\uff0c\u4ecd\u7136\u53ef\u4ee5\u4ee5\u975e\u9650\u5b9a\u540d\u7684\u65b9\u5f0f\u6765\u4f7f\u7528\u3002","title":"Using \u6307\u4ee4"},{"location":"6-12-Using-declarations-and-using%20directives/#using-using-namespace-std","text":"\u5728\u73b0\u4ee3 C++ \u4e2d\uff0c\u4f7f\u7528 using \u6307\u4ee4\u901a\u5e38\u662f\u5f0a\u5927\u4e8e\u5229\u7684\uff08\u9664\u4e86\u80fd\u5c11\u6253\u5b57\u4ee5\u5916\uff09\u3002\u56e0\u4e3a\u4f7f\u7528 using \u6307\u4ee4\u5bfc\u5165\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6240\u6709\u6807\u8bc6\u7b26\u4f1a\u6781\u5927\u5730\u63d0\u9ad8\u547d\u540d\u51b2\u7a81\u7684\u53ef\u80fd\uff08\u5c24\u5176\u662f\u5bfc\u5165 std \uff09\u3002 \u901a\u8fc7\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230 using \u6307\u4ee4\u53ef\u80fd\u5e26\u6765\u7684\u4e8c\u4e49\u6027\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include namespace a { int x { 10 }; } namespace b { int x { 20 }; } int main () { using namespace a ; using namespace b ; std :: cout << x << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u51b3\u5b9a\u8fd9\u91cc\u7684 main \u4e2d\u7684 x \u5230\u5e95\u6307\u7684\u662f a:: x \u8fd8\u662f b:: x \u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u4f1a\u56e0\u4e3a\u201c\u6b67\u4e49\u7b26\u53f7\u201d\u9519\u8bef\u800c\u65e0\u6cd5\u7f16\u8bd1\u3002\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u529e\u6cd5\u662f\u79fb\u9664\u5176\u4e2d\u4e00\u4e2a using \u8bed\u53e5\uff0c\u4f7f\u7528 using \u58f0\u660e\uff0c\u6216\u8005\u76f4\u63a5\u4f7f\u7528\u663e\u5f0f\u7684\u4f5c\u7528\u57df\u4fee\u9970\u7b26( a:: \u6216 b:: )\u3002 \u518d\u770b\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // imports the declaration of std::cout int cout () // declares our own \"cout\" function { return 5 ; } int main () { using namespace std ; // makes std::cout accessible as \"cout\" cout << \"Hello, world!\" ; // uh oh! Which cout do we want here? The one in the std namespace or the one we defined above? return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u786e\u5b9a cout \u5230\u5e95\u6307\u7684\u662f std:: cout \u8fd8\u662f\u6211\u4eec\u5b9a\u4e49\u7684 cout \u51fd\u6570\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u4ecd\u7136\u4f1a\u56e0\u4e3a\u201c\u6b67\u4e49\u7b26\u53f7\u201d\u9519\u8bef\u800c\u65e0\u6cd5\u7f16\u8bd1\u3002\u5982\u679c\u6211\u4eec\u4f7f\u7528\u524d\u7f00 std:: \uff0c\u50cf\u8fd9\u6837\uff1a 1 std :: cout << \"Hello, world!\" ; // tell the compiler we mean std::cout \u6216\u8005\u4f7f\u7528 using \u58f0\u660e\u800c\u4e0d\u662f using \u6307\u4ee4\uff1a 1 2 using std :: cout ; // tell the compiler that cout means std::cout cout << \"Hello, world!\" ; // so this means std::cout \u7a0b\u5e8f\u5c06\u53ef\u4ee5\u987a\u5229\u5b8c\u6210\u7f16\u8bd1\u3002 \u867d\u7136\u4f60\u5f88\u53ef\u80fd\u5e76\u4e0d\u4f1a\u5b9a\u4e49\u4e00\u4e2a\u53eb\u505a cout \u7684\u51fd\u6570\uff0c\u4f46\u662f std \u547d\u540d\u7a7a\u95f4\u4e2d\u6709\u6210\u767e\u4e0a\u5343\u7684\u6807\u8bc6\u7b26\u53ef\u80fd\u4f1a\u4e0e\u4f60\u5b9a\u4e49\u7684\u6807\u8bc6\u7b26\u4ea7\u751f\u51b2\u7a81\uff0c\u6bd4\u65b9\u8bf4\u201ccount\u201d, \u201cmin\u201d, \u201cmax\u201d, \u201csearch\u201d, \u201csort\u201d \u7b49\u7b49\u3002 \u800c\u4e14\uff0c\u5373\u4f7f using \u6307\u4ee4\u4eca\u5929\u6ca1\u6709\u5e26\u6765\u547d\u540d\u51b2\u7a81\uff0c\u4e5f\u96be\u4fdd\u4e0d\u5728\u672a\u6765\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u7684\u4ee3\u7801\u4f7f\u7528 using \u5bfc\u5165\u4e86\u67d0\u4e2a\u547d\u540d\u7a7a\u95f4\uff0c\u7136\u540e\u67d0\u5929\u8be5\u5e93\u66f4\u65b0\u4e86\uff0c\u6dfb\u52a0\u4e86\u4e00\u4e9b\u65b0\u7684\u53d8\u91cf\u540d\uff0c\u90a3\u6ca1\u51c6\u5c31\u4f1a\u548c\u4f60\u7684\u4ee3\u7801\u4ea7\u751f\u51b2\u7a81\u54af\u3002 \u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u9634\u9669\u7684\u95ee\u9898\u4e5f\u53ef\u80fd\u53d1\u751f\u3002\u5f53\u5e93\u66f4\u65b0\u4e4b\u540e\uff0c\u5b83\u5b9a\u4e49\u7684\u4e00\u4e9b\u65b0\u7684\u51fd\u6570\uff0c\u53ef\u80fd\u4e0d\u4ec5\u51fd\u6570\u540d\u548c\u6211\u4eec\u5b9a\u4e49\u7684\u51fd\u6570\u6709\u51b2\u7a81\uff0c\u751a\u81f3\u5b83\u66f4\u7b26\u5408\u67d0\u79cd\u67d0\u5904\u7684\u51fd\u6570\u8c03\u7528\u65b9\u5f0f\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c31\u53ef\u80fd\u4f1a\u4f7f\u7528\u8be5\u51fd\u6570\u6765\u66ff\u6362\u6211\u4eec\u7684\u51fd\u6570\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u7684\u884c\u4e3a\u5c06\u4f1a\u88ab\u6539\u53d8\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a ```cpp title=foolib.h\"\" namespace foo { // pretend there is some useful code that we use here } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ```cpp title=\"main.cpp\" #include #include // a third-party library, thus angled brackets used int someFcn(double) { return 1; } int main() { using namespace foo; // Because we're lazy and want to access foo:: qualified names without typing the foo:: prefix std::cout << someFcn(0); // \u5b57\u9762\u91cf\u5e94\u5f53\u4f7f\u7528 0.0 \u4f46\u88ab\u8bef\u7528\u4e3a 0\uff0c\u8fd9\u662f\u4e00\u4e2a\u5f88\u5bb9\u6613\u72af\u7684\u9519\u8bef\u3002 return 0; } \u7a0b\u5e8f\u8fd0\u884c\u540e\u4f1a\u6253\u5370 1 \u3002 \u73b0\u5728\uff0c\u66f4\u65b0 foolib\uff08\u4f7f\u7528\u66f4\u65b0\u7684 foolib.h \uff09\uff0c\u7a0b\u5e8f\u7f16\u7a0b\u4e0b\u9762\u8fd9\u6837\uff1a foolib.h 1 2 3 4 5 6 7 8 9 10 namespace foo { // newly introduced function int someFcn ( int ) { return 2 ; } // pretend there is some useful code that we use here } main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int someFcn ( double ) { return 1 ; } int main () { using namespace foo ; // Because we're lazy and want to access foo:: qualified names without typing the foo:: prefix std :: cout << someFcn ( 0 ); // \u5b57\u9762\u91cf\u5e94\u5f53\u4f7f\u7528 0.0 \u4f46\u88ab\u8bef\u7528\u4e3a 0\uff0c\u8fd9\u662f\u4e00\u4e2a\u5f88\u5bb9\u6613\u72af\u7684\u9519\u8bef\u3002 return 0 ; } main.cpp \u6587\u4ef6\u5b8c\u5168\u6ca1\u6709\u6539\u53d8\uff0c\u4f46\u662f\u7a0b\u5e8f\u5374\u8f93\u51fa\u4e86 2\uff01 \u5f53\u7f16\u8bd1\u5668\u9047\u5230\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u5b83\u9700\u8981\u786e\u5b9a\u4e0e\u51fd\u6570\u8c03\u7528\u6700\u5339\u914d\u7684\u51fd\u6570\u5b9a\u4e49\u662f\u4ec0\u4e48\u3002\u5f53\u6709\u591a\u4e2a\u5b9a\u4e49\u53ef\u4f9b\u7f16\u8bd1\u5668\u9009\u62e9\u65f6\uff0c\u5b83\u4e00\u5b9a\u4f1a\u9009\u62e9\u65e0\u9700\u53c2\u6570\u8f6c\u6362\u7684\u90a3\u4e00\u4e2a\u3002\u56e0\u4e3a 0 \u662f\u4e00\u4e2a\u6574\u578b\uff0cC++ \u4e00\u5b9a\u4f1a\u9009\u62e9\u66f4\u52a0\u5339\u914d\u7684\u65b0\u5b9a\u4e49\u7684 someFcn(int) (\u65e0\u9700\u8f6c\u6362) \u800c\u4e0d\u662f someFcn(double) (\u9700\u8981\u5c06 int \u8f6c\u6362\u4e3a double)\u3002\u8fd9\u6837\u5c31\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u884c\u4e3a\u88ab\u610f\u5916\u5730\u6539\u53d8\u3002 \u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u4f7f\u7528\u7684\u662f using \u58f0\u660e\u6216\u8005\u4f5c\u7528\u57df\u4fee\u9970\u7b26\u7684\u8bdd\uff0c\u5c31\u5b8c\u5168\u4e0d\u4f1a\u53d1\u751f\u8fd9\u7c7b\u95ee\u9898\u3002 \u6700\u540e\uff0c\u5bf9\u4e8e\u7a0b\u5e8f\u7684\u8bfb\u8005\u6765\u8bf4\uff0c\u5982\u679c\u4e0d\u4f7f\u7528\u4f5c\u7528\u57df\u4fee\u9970\u7b26\u7684\u8bdd\uff0c\u8bfb\u8005\u5c06\u96be\u786e\u5b9a\u67d0\u4e2a\u51fd\u6570\u5230\u5e95\u662f\u81ea\u5b9a\u4e49\u7684\uff0c\u8fd8\u662f\u5c5e\u4e8e\u67d0\u4e2a\u5e93\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f7f\u7528 using \u6307\u4ee4\uff1a 1 2 3 4 5 6 using namespace ns ; int main () { foo (); // is this foo a user-defined function, or part of the ns library? } \u8fd9\u79cd\u60c5\u51b5\u4e0b\u5c31\u5f88\u96be\u786e\u5b9a foo() \u6307\u7684\u662f ns:: foo() \u8fd8\u662f\u7528\u6237\u81ea\u5b9a\u4e49\u7684 foo() \u51fd\u6570\u3002\u5728\u4f7f\u7528\u73b0\u4ee3 IDE \u65f6\uff0c\u5982\u679c\u5c06\u9f20\u6807\u60ac\u6d6e\u5728\u53d8\u91cf\u540d\u4e0a\u65f6\uff0cIDE \u4e00\u822c\u90fd\u53ef\u4ee5\u5e2e\u6211\u4eec\u786e\u5b9a\u6807\u8bc6\u7b26\u5230\u5e95\u6307\u7684\u662f\u4ec0\u4e48\uff0c\u4f46\u662f\u8fd9\u6837\u663e\u7136\u5f88\u9ebb\u70e6\uff0c\u6bd5\u7adf\u6bcf\u4e2a\u53d8\u91cf\u540d\u90fd\u60ac\u6d6e\u9f20\u6807\u770b\u4e00\u4e0b\u662f\u5f88\u9ebb\u70e6\u7684\u3002 \u5982\u679c\u4e0d\u662f\u4f7f\u7528 using \u6307\u4ee4\uff0c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u5c31\u4f1a\u597d\u5f88\u591a\uff1a 1 2 3 4 5 int main () { ns :: foo (); // clearly part of the ns library foo (); // likely a user-defined function } \u5728\u8fd9\u4e2a\u7248\u672c\u4e2d\uff0c ns:: foo() \u663e\u7136\u662f\u8c03\u7528\u5e93\u51fd\u6570\u3002\u800c foo() \u5219\u662f\u8c03\u7528\u7528\u6237\u81ea\u5b9a\u4e49\u51fd\u6570\u3002","title":"Using \u6307\u4ee4\u5e26\u6765\u7684\u95ee\u9898 (\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528 \u201cusing namespace std;\u201d)"},{"location":"6-12-Using-declarations-and-using%20directives/#using_2","text":"\u5982\u679c\u5728\u8bed\u53e5\u5757\u4e2d\u4f7f\u7528 using \u58f0\u660e\u6216 using \u6307\u4ee4\uff0c\u90a3\u5bf9\u5e94\u7684\u53d8\u91cf\u540d\u53ea\u5728\u8be5\u4f5c\u7528\u57df\u4e2d\u6709\u6548\u3002\u8fd9\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u7279\u6027\uff0c\u56e0\u4e3a\u5b83\u5c06\u547d\u540d\u51b2\u7a81\u7684\u98ce\u9669\u9650\u5b9a\u5728\u4e86\u8bed\u53e5\u5757\u4e2d\u3002 \u5982\u679c\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u4f7f\u7528 using \u58f0\u660e\u6216 using \u6307\u4ee4\uff0c\u53d8\u91cf\u540d\u5c06\u5f71\u54cd\u5230\u6574\u4e2a\u6587\u4ef6\uff08\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\uff09\u3002","title":"Using \u58f0\u660e\u548c\u6307\u4ee4\u7684\u4f5c\u7528\u57df"},{"location":"6-12-Using-declarations-and-using%20directives/#using_3","text":"\u4e00\u65e6\u4f7f\u7528\u4e86 using \u8bed\u53e5\uff0c\u5728\u76f8\u540c\u7684\u4f5c\u7528\u57df\u4e2d\u5c06\u6ca1\u6709\u529e\u6cd5\u5bf9\u5176\u8fdb\u884c\u53d6\u6d88\u6216\u66ff\u6362\u3002 1 2 3 4 5 6 7 8 9 int main () { using namespace foo ; // \u6ca1\u6709\u529e\u6cd5\u53d6\u6d88 \"using namespace foo\" \u7684\u6548\u679c // \u4e5f\u6ca1\u6709\u529e\u6cd5\u4f7f\u7528\u5176\u4ed6 using \u8bed\u53e5\u66ff\u6362 return 0 ; } // using namespace foo ends here \u5982\u679c\u5e0c\u671b\u9650\u5236 using \u8bed\u53e5\u7684\u4f5c\u7528\u8303\u56f4\uff0c\u53ef\u4ee5\u5c06\u5b83\u653e\u5728\u4e00\u4e2a\u8bed\u53e5\u5757\u4e2d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int main () { { using namespace foo ; // calls to foo:: stuff here } // using namespace foo expires { using namespace Goo ; // calls to Goo:: stuff here } // using namespace Goo expires return 0 ; } \u5f53\u7136\uff0c\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u53ef\u4ee5\u4e00\u52b3\u6c38\u9038\u5730\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u3002","title":"\u53d6\u6d88\u6216\u8005\u66ff\u6362 using \u8bed\u53e5"},{"location":"6-12-Using-declarations-and-using%20directives/#using_4","text":"\u9664\u975e\u9047\u5230\u975e\u5e38\u7279\u6b8a\u7684\u60c5\u51b5\uff0c\u8bf7\u907f\u514d\u4f7f\u7528 using \u6307\u4ee4 (\u5c24\u5176\u662f using namespace std; )\u3002\u5728\u8bed\u53e5\u5757\u4e2d\u4f7f\u7528 using \u58f0\u660e\u901a\u5e38\u88ab\u8ba4\u4e3a\u662f\u5b89\u5168\u7684\u3002\u7edd\u5bf9\u4e0d\u8981\u5728\u5934\u6587\u4ef6\u7684\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u4f7f\u7528\u5b83\u4eec\uff0c\u5728\u6e90\u6587\u4ef6\u7684\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u6709\u9650\u5730\u4f7f\u7528\u5b83\u4eec\u3002 \u6700\u4f73\u5b9e\u8df5 \u660e\u786e\u6307\u5b9a\u4f5c\u7528\u57df\u597d\u8fc7 using \u8bed\u53e5\u3002\u5c3d\u53ef\u80fd\u5730\u907f\u514d\u4f7f\u7528 using \u6307\u4ee4\u3002\u5728\u8bed\u53e5\u5757\u4e2d\u4f7f\u7528 using \u58f0\u660e\u901a\u5e38\u662f\u53ef\u4ee5\u7684\u3002 \u76f8\u5173\u5185\u5bb9 using \u5173\u952e\u5b57\u8fd8\u53ef\u4ee5\u7528\u6765\u5b9a\u4e49\u7c7b\u578b\u522b\u540d\uff0c\u6211\u4eec\u4f1a\u5728 8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d \u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u7c7b\u578b\u522b\u540d\u3002","title":"using \u8bed\u53e5\u7684\u6700\u4f73\u5b9e\u8df5"},{"location":"6-13-Inline-functions/","tags":["inline"],"text":"Key Takeaway \u73b0\u4ee3\u7f16\u8bd1\u5668\u53ef\u4ee5\u51b3\u5b9a\u51fd\u6570\u662f\u5426\u5e94\u5f53\u88ab\u5c55\u5f00\u3002 \u6709\u4e9b\u51fd\u6570\u662f\u9690\u5f0f\u7684\u5185\u8054\u51fd\u6570\uff0c\u5305\u62ec\uff1a \u5b9a\u4e49\u5728\u7c7b\u3001\u7ed3\u6784\u4f53\u6216\u8054\u5408\u4f53\u4e2d\u7684\u51fd\u6570\uff1b Constexpr / consteval \u51fd\u6570 6.14 - Constexpr \u548c consteval \u51fd\u6570 inline \u5173\u952e\u5b57\u662f\u4e00\u79cd \u63d0\u793a \uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u5ffd\u7565\u5b83\u3002\u540c\u65f6\u7f16\u8bd1\u5668\u4e5f\u53ef\u4ee5\u5c55\u5f00\u6ca1\u6709\u6807\u8bb0 inline \u7684\u51fd\u6570\u3002 \u73b0\u4ee3 inline \u5173\u952e\u5b57\u7684\u542b\u4e49\u662f \u5141\u8bb8\u591a\u5904\u5b9a\u4e49 \u5185\u8054\u51fd\u6570\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff1a\u4e3a\u4e86\u80fd\u591f\u5c55\u5f00\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u5fc5\u987b\u80fd\u591f\u5728\u51fd\u6570\u8c03\u7528\u5904\u770b\u5230\u51fd\u6570\u7684\u5b9a\u4e49\u3002\u56e0\u6b64\uff0c\u5185\u8054\u51fd\u6570\u901a\u5e38\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u5b83\u4eec\u5c31\u53ef\u4ee5\u88ab\u5305\u542b\u5230\u4f7f\u7528\u5b83\u4eec\u7684\u6e90\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u7f16\u8bd1\u5668\u770b\u5230\u51fd\u6570\u7684\u5b8c\u6574\u5b9a\u4e49\u3002 \u6709\u4e9b\u65f6\u5019\u6211\u4eec\u9700\u8981\u7f16\u5199\u4ee3\u7801\u5b8c\u6210\u4e00\u4e9b\u72ec\u7acb\u7684\u4efb\u52a1\uff0c\u4f8b\u5982\u4ece\u7528\u6237\u8bfb\u53d6\u8f93\u5165\u3001\u8f93\u51fa\u5230\u6587\u4ef6\u6216\u8005\u8ba1\u7b97\u7279\u5b9a\u7684\u503c\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u6709\u4e24\u79cd\u9009\u62e9\uff1a \u5c06\u4ee3\u7801\u4f5c\u4e3a\u5df2\u6709\u51fd\u6570\u7684\u4e00\u90e8\u5206\uff08\u79f0\u4e3a\u5d4c\u5165\uff09\uff1b \u521b\u5efa\u4e00\u4e2a\u5355\u72ec\u7684\u51fd\u6570\uff08\u6216\u8005\u5b50\u51fd\u6570\uff09\u5b8c\u6210\u8be5\u4efb\u52a1\u3002 \u5c06\u8fd9\u4e9b\u4ee3\u7801\u4f5c\u4e3a\u51fd\u6570\u80fd\u591f\u5e26\u6765\u5f88\u591a\u6f5c\u5728\u7684\u597d\u5904\uff0c\u56e0\u4e3a\u51fd\u6570\u4e2d\u7684\u4ee3\u7801\uff1a \u53ef\u8bfb\u6027\u66f4\u597d\uff0c\u66f4\u5bb9\u6613\u7406\u89e3\uff1b \u53ef\u7528\u6027\u66f4\u597d\uff0c\u53ef\u4ee5\u5728\u4e0d\u4e86\u89e3\u51fd\u6570\u5b9e\u73b0\u7ec6\u8282\u7684\u524d\u63d0\u4e0b\u8c03\u7528\u51fd\u6570\uff1b \u66f4\u5bb9\u6613\u66f4\u65b0\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u4ee3\u7801\u53ef\u4ee5\u5728\u4e00\u5904\u4fee\u6539\u5904\u5904\u66f4\u65b0\uff1b \u53ef\u590d\u7528\u6027\u66f4\u597d\uff0c\u56e0\u4e3a\u51fd\u6570\u5929\u751f\u662f\u6a21\u5757\u5316\u7684\u3002 \u4e0d\u8fc7\uff0c\u51fd\u6570\u8c03\u7528\u4e5f\u662f\u4e5f\u6709\u4e0d\u597d\u7684\u5730\u65b9\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5e26\u6765\u989d\u5916\u7684\u6027\u80fd\u5f00\u9500\u3002\u4f8b\u5982\u4e0b\u5217\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int min ( int x , int y ) { return ( x < y ) ? x : y ; } int main () { std :: cout << min ( 5 , 6 ) << '\\n' ; std :: cout << min ( 3 , 2 ) << '\\n' ; return 0 ; } \u5f53 min() \u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0cCPU\u5fc5\u987b\u4fdd\u5b58\u5f53\u524d\u6307\u4ee4\u7684\u5730\u5740\uff08\u4ee5\u4fbf\u7a0d\u540e\u8fd4\u56de\uff09\u4ee5\u53ca\u4e00\u4e9bCPU\u5bc4\u5b58\u5668\u7684\u503c\uff08\u7528\u4e8e\u8fd4\u56de\u65f6\u6062\u590d\u4e0a\u4e0b\u6587\uff09\u3002\u7136\u540e \u5b9e\u53c2 x \u548c y \u5fc5\u987b\u88ab\u5b9e\u4f8b\u5316\u548c\u521d\u59cb\u5316\u3002\u7136\u540e\u8df3\u8f6c\u5230 min() \u51fd\u6570\u6267\u884c\u3002\u5f53\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u7a0b\u5e8f\u53c8\u4f1a\u8df3\u8f6c\u56de\u5230\u4e3b\u8c03\u51fd\u6570\uff0c\u540c\u65f6\u8fd4\u56de\u503c\u88ab\u62f7\u8d1d\u8fd4\u56de\u3002\u6362\u8a00\u4e4b\uff0c\u6bcf\u6b21\u51fd\u6570\u8c03\u7528\u90fd\u4f1a\u4ea7\u751f\u5f88\u5927\u7684\u5f00\u9500\u3002 \u5bf9\u4e8e\u90a3\u4e9b\u590d\u6742\u7684\u51fd\u6570\u6765\u8bf4\uff0c\u8fd9\u4e9b\u5f00\u9500\u76f8\u5bf9\u4e8e\u51fd\u6570\u6267\u884c\u5b9e\u9645\u6765\u8bb2\u53ef\u80fd\u662f\u5fae\u4e0d\u8db3\u9053\u7684\u3002\u4e0d\u8fc7\uff0c\u5bf9\u4e8e\u5f88\u591a\u5c0f\u51fd\u6570\uff08\u4f8b\u5982\u8fd9\u91cc\u7684 min() \uff09\u3002\u51fd\u6570\u8c03\u7528\u7684\u5f00\u9500\u751a\u81f3\u53ef\u80fd\u6bd4\u51fd\u6570\u5185\u90e8\u4ee3\u7801\u6267\u884c\u8017\u65f6\u8fd8\u8981\u9ad8\u3002\u5982\u679c\u8fd9\u4e9b\u51fd\u6570\u88ab\u9891\u7e41\u8c03\u7528\uff0c\u90a3\u4e48\u548c\u4e0d\u4f7f\u7528\u4ee3\u7801\u6765\u6bd4\u6027\u80fd\u5dee\u5f02\u4f1a\u975e\u5e38\u5927\u3002 \u5185\u8054\u5c55\u5f00 \u597d\u5728\uff0cC++\u7f16\u8bd1\u5668\u6709\u529e\u6cd5\u514b\u670d\u8fd9\u79cd\u5f00\u9500\uff1a\u5185\u8054\u5c55\u5f00\u53ef\u4ee5\u5c06\u51fd\u6570\u8c03\u7528\u6839\u636e\u5176\u5b9a\u4e49\u76f4\u63a5\u66ff\u6362\u4e3a\u5bf9\u5e94\u7684\u4ee3\u7801\u3002 \u4f8b\u5982\uff0c\u5982\u679c\u7f16\u8bd1\u5668\u80fd\u591f\u5c06\u4e0a\u9762\u7684 min() \u51fd\u6570\u5c55\u5f00\u7684\u8bdd\uff0c\u5c06\u4f1a\u5f97\u5230\u5982\u4e0b\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << (( 5 < 6 ) ? 5 : 6 ) << '\\n' ; std :: cout << (( 3 < 2 ) ? 3 : 2 ) << '\\n' ; return 0 ; } \u6ce8\u610f\uff0c min() \u51fd\u6570\u4e2d\u7684\u51fd\u6570\u8c03\u7528\u88ab\u5176\u51fd\u6570\u4f53\u4ee3\u7801\u66ff\u6362\uff08\u5b9e\u53c2\u6570\u503c\u66ff\u6362\u4e86\u5f62\u53c2\uff09\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u907f\u514d\u51fd\u6570\u8c03\u7528\u5e26\u6765\u7684\u989d\u5916\u5f00\u9500\u3002 \u5185\u8054\u4ee3\u7801\u7684\u6027\u80fd \u9664\u4e86\u80fd\u591f\u907f\u514d\u51fd\u6570\u8c03\u7528\u7684\u5f00\u9500\uff0c\u5185\u8054\u5c55\u5f00\u8fd8\u53ef\u4ee5\u4f7f\u7f16\u8bd1\u5668\u66f4\u52a0\u6709\u6548\u5730\u4f18\u5316\u4ee3\u7801\u2014\u2014\u4f8b\u5982\uff0c\u56e0\u4e3a\u8868\u8fbe\u5f0f ((5 < 6) ? 5 : 6) \u73b0\u5728\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c31\u53ef\u4ee5\u5c06 main() \u51fd\u6570\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u4f18\u5316\u4e3a std::cout << 5 << '\\n'; \u3002 \u4e0d\u8fc7\uff0c\u5185\u8054\u5c55\u5f00\u4e5f\u6709\u5b83\u7684\u5f00\u9500\uff1a\u5982\u679c\u5c55\u5f00\u51fd\u6570\u4f53\u6240\u9700\u7684\u6307\u4ee4\u6bd4\u51fd\u6570\u8c03\u7528\u672c\u8eab\u8fd8\u591a\uff0c\u90a3\u4e48\u6bcf\u4e00\u5904\u5185\u8054\u5c55\u5f00\u5c06\u4f1a\u5bfc\u81f4\u53ef\u6267\u884c\u6587\u4ef6\u7684\u81a8\u80c0\u3002\u53ef\u6267\u884c\u7a0b\u5e8f\u8d8a\u5927\uff0c\u901f\u5ea6\u4e5f\u53ef\u80fd\u4f1a\u8d8a\u5dee\uff08\u7531\u4e8e\u4e0d\u80fd\u5339\u914d\u7f13\u5b58\u7684\u5927\u5c0f\uff09\u3002 \u786e\u5b9a\u51fd\u6570\u662f\u5426\u5e94\u8be5\u88ab\u5b9a\u4e49\u4e3a\u5185\u8054\uff08\u79fb\u9664\u51fd\u6570\u8c03\u7528\u5f00\u9500\u5e26\u6765\u7684\u597d\u5904\u5927\u4e8e\u53ef\u6267\u884c\u6587\u4ef6\u5927\u5c0f\u589e\u957f\u5e26\u6765\u7684\u574f\u5904\uff09\u7684\u4f9d\u636e\u5e76\u4e0d\u660e\u663e\u3002\u5185\u8054\u5c55\u5f00\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u7684\u63d0\u5347\u3001\u6027\u80fd\u7684\u4e0b\u964d\u6216\u8005\u5bf9\u6027\u80fd\u5b8c\u5168\u6ca1\u6709\u5f71\u54cd\uff0c\u8fd9\u53d6\u51b3\u4e8e\u51fd\u6570\u8c03\u7528\u7684\u5f00\u9500\uff0c\u51fd\u6570\u5927\u5c0f\u548c\u7f16\u8bd1\u5668\u80fd\u591f\u6267\u884c\u54ea\u4e9b\u4f18\u5316\u3002 \u5185\u8054\u5c55\u5f00\u6700\u9002\u5408\u7b80\u5355\u3001\u5c0f\u7684\u51fd\u6570\uff08\u4f8b\u5982\u53ea\u6709\u51e0\u884c\u8bed\u53e5\u7684\u51fd\u6570\uff09\uff0c\u7279\u522b\u662f\u4f1a\u88ab\u591a\u6b21\u8c03\u7528\u7684\u51fd\u6570\uff08\u4f8b\u5982\u5faa\u73af\u4e2d\u7684\u51fd\u6570\uff09\u3002 \u5185\u8054\u4ee3\u7801\u7684\u5c55\u5f00\u65f6\u673a \u6240\u6709\u7684\u51fd\u6570\u90fd\u53ef\u4ee5\u5f52\u4e3a\u4ee5\u4e0b\u4e09\u7c7b\uff1a \u5fc5\u987b\u88ab\u5c55\u5f00\uff1b \u53ef\u80fd\u4f1a\u88ab\u5c55\u5f00(\u5f88\u591a\u51fd\u6570\u90fd\u5c5e\u4e8e\u8fd9\u4e00\u7c7b). \u4e0d\u80fd\u88ab\u5c55\u5f00\u3002 \u80fd\u591f\u88ab\u5c55\u5f00\u7684\u51fd\u6570\u79f0\u4e3a\u5185\u8054\u51fd\u6570\u3002 \u5927\u591a\u6570\u51fd\u6570\u90fd\u5c5e\u4e8e\u7b2c\u4e8c\u7c7b\uff1a\u201c\u53ef\u80fd\u4f1a\u88ab\u5c55\u5f00\u201d\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u51fd\u6570\u6765\u8bf4\uff0c\u5982\u679c\u5c55\u5f00\u80fd\u591f\u5e26\u6765\u597d\u5904\uff0c\u90a3\u4e48\u5c31\u53ef\u80fd\u88ab\u5c55\u5f00\u3002\u5bf9\u4e8e\u8fd9\u4e00\u7c7b\u51fd\u6570\uff0c\u73b0\u4ee3\u7f16\u8bd1\u5668\u53ef\u4ee5\u5206\u6790\u6bcf\u4e2a\u51fd\u6570\u548c\u6bcf\u5904\u51fd\u6570\u8c03\u7528\u6765\u786e\u5b9a\u5bf9\u5e94\u7684\u51fd\u6570\u662f\u4e0d\u662f\u88ab\u5185\u8054\u5c55\u5f00\u540e\u7684\u6536\u76ca\u66f4\u9ad8\u3002\u7f16\u8bd1\u5668\u6700\u7ec8\u4f1a\u51b3\u5b9a\u662f\u5426\u5c06\u5b83\u4eec\u4e2d\u4e00\u4e9b\u3001\u6216\u8005\u5168\u90e8\u5c55\u5f00\u3002 \u5c0f\u8d34\u58eb \u73b0\u4ee3\u7f16\u8bd1\u5668\u53ef\u4ee5\u51b3\u5b9a\u51fd\u6570\u662f\u5426\u5e94\u5f53\u88ab\u5c55\u5f00\u3002 \u6269\u5c55\u9605\u8bfb \u6709\u4e9b\u51fd\u6570\u662f\u9690\u5f0f\u7684\u5185\u8054\u51fd\u6570\uff0c\u5305\u62ec\uff1a - \u5b9a\u4e49\u5728\u7c7b\u3001\u7ed3\u6784\u4f53\u6216\u8054\u5408\u4f53\u4e2d\u7684\u51fd\u6570\uff1b - Constexpr / consteval \u51fd\u6570 6.14 - Constexpr \u548c consteval \u51fd\u6570 \u5386\u53f2\u4e0a\u7684 inline \u5173\u952e\u5b57 \u56de\u987e C++ \u7684\u5386\u53f2\uff0c\u7f16\u8bd1\u5668\u66fe\u7ecf\u8981\u4e48\u4e0d\u5177\u5907\u5224\u65ad\u51fd\u6570\u5c55\u5f00\u662f\u5426\u80fd\u591f\u5e26\u6765\u597d\u5904\u7684\u80fd\u529b\uff0c\u8981\u4e48\u4e0d\u80fd\u591f\u5f88\u597d\u7684\u6267\u884c\u4e0a\u8ff0\u5224\u65ad\u3002\u56e0\u6b64\uff0cC++\u7279\u610f\u63d0\u4f9b\u4e86 inline \u5173\u952e\u5b57\uff0c\u7528\u4e8e \u63d0\u793a \u7f16\u8bd1\u5668\u8be5\u51fd\u6570\u5e94\u8be5\u88ab\u5185\u8054\u5c55\u5f00\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include inline int min ( int x , int y ) // hint to the compiler that it should do inline expansion of this function { return ( x < y ) ? x : y ; } int main () { std :: cout << min ( 5 , 6 ) << '\\n' ; std :: cout << min ( 3 , 2 ) << '\\n' ; return 0 ; } \u8fd9\u5c31\u662f\u6240\u8c13\u201c\u5185\u8054\u51fd\u6570\u201d\u7684\u6765\u5386\uff08\u56e0\u4e3a\u8fd9\u4e9b\u51fd\u6570\u7684\u58f0\u660e\u8bed\u6cd5\u4e2d\u5305\u542b inline \u5173\u952e\u5b57\uff09\u3002 \u4f46\u662f\uff0c\u73b0\u4ee3 C++ \u4e2d\uff0c inline \u5173\u952e\u5b57\u5df2\u7ecf\u4e0d\u518d\u88ab\u7528\u4e8e\u544a\u8bc9\u7f16\u8bd1\u5668\u5c55\u5f00\u5bf9\u5e94\u7684\u51fd\u6570\uff0c\u8fd9\u4e48\u505a\u7684\u539f\u56e0\u6709\u5f88\u591a\uff1a \u4f7f\u7528 inline \u8981\u6c42\u5185\u8054\u5c55\u5f00\u662f\u4e00\u79cd\u8fc7\u65e9\u4f18\u5316\u884c\u4e3a\uff0c\u6ee5\u7528 inline \u6709\u53cd\u5012\u4f1a\u4f24\u5bb3\u7a0b\u5e8f\u6027\u80fd\uff1b inline \u5173\u952e\u5b57\u53ea\u662f\u4e00\u4e2a \u63d0\u793a \u2014\u2014\u7f16\u8bd1\u5668\u5b8c\u5168\u53ef\u4ee5\u5ffd\u7565\u5b83\uff0c\u5c24\u5176\u662f\u5f53\u4f60\u8981\u6c42\u7f16\u8bd1\u5668\u5185\u8054\u5c55\u5f00\u4e00\u4e2a\u5f88\u5927\u7684\u51fd\u6570\u65f6\u3002\u540c\u65f6\uff0c\u7f16\u8bd1\u5668\u4e5f\u53ef\u4ee5\u5c55\u5f00\u90a3\u4e9b\u5e76\u6ca1\u6709\u88ab\u6807\u8bb0\u4e3a inline \u7684\u51fd\u6570\u3002 inline \u5173\u952e\u5b57\u7684\u5b9a\u4e49\u5904\u4e8e\u4e00\u79cd\u4e0d\u5408\u9002\u7684\u7c92\u5ea6\u3002\u6211\u4eec\u5728\u51fd\u6570\u58f0\u660e\u4e2d\u4f7f\u7528\u8be5\u5173\u952e\u5b57\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u51fd\u6570\u7684\u5c55\u5f00\u662f\u57fa\u4e8e\u6bcf\u4e2a\u51fd\u6570\u8c03\u7528\u7684\u3002\u5bf9\u4e8e\u4e00\u4e2a\u51fd\u6570\u7684\u5404\u5904\u8c03\u7528\uff0c\u5b83\u4eec\u4e2d\u7684\u4e00\u90e8\u5206\u53ef\u80fd\u88ab\u5c55\u5f00\uff0c\u800c\u53e6\u4e00\u4e9b\u5219\u4e0d\u88ab\u5c55\u5f00\uff0c\u7136\u800c\u8fd9\u4e2a\u5173\u952e\u5b57\u5374\u4e0d\u80fd\u5bf9\u6b64\u4ea7\u751f\u5f71\u54cd\u3002 \u73b0\u4ee3\u7f16\u8bd1\u5668\u901a\u5e38\u5f88\u64c5\u957f\u51b3\u5b9a\u90a3\u4e9b\u51fd\u6570\u5e94\u8be5\u88ab\u4f5c\u4e3a\u5185\u8054\u51fd\u6570\u2014\u2014\u5b83\u7684\u8fd9\u79cd\u80fd\u529b\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u90fd\u6bd4\u4eba\u7c7b\u5f3a\u3002 \u56e0\u6b64\uff0c\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u5ffd\u7565\u4f60\u4e3a\u51fd\u6570\u6dfb\u52a0\u7684 inline \u6807\u8bb0\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e0d\u8981\u4f7f\u7528 inline \u5173\u952e\u5b57\u8981\u6c42\u5185\u8054\u5c55\u5f00\u51fd\u6570\u3002 \u73b0\u4ee3 inline \u5173\u952e\u5b57 \u5728\u4e4b\u524d\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u5934\u6587\u4ef6\u4e2d\u4e0d\u5e94\u8be5\u653e\u7f6e\u51fd\u6570\u7684\u5b9e\u73b0\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u5934\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u4f1a\u88ab\u5305\u542b\u5230\u591a\u4e2a.cpp\u6587\u4ef6\u4e2d\uff0c\u90a3\u4e48\u8fd9\u4e9b\u51fd\u6570\u7684\u5b9a\u4e49\u4e5f\u5c31\u4f1a\u88ab\u590d\u5236\u591a\u4efd\u3002\u56e0\u6b64\uff0c\u8fd9\u4e9b\u6587\u4ef6\u7f16\u8bd1\u540e\uff0c\u94fe\u63a5\u5668\u5c31\u4f1a\u62a5\u9519\uff0c\u56e0\u4e3a\u51fd\u6570\u7684\u5b9a\u4e49\u8fdd\u53cd\u4e86 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u3002 \u5728 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\uff0c\u5728\u73b0\u4ee3 C++ \u4e2d\uff0c inline \u6982\u5ff5\u53d1\u5c55\u51fa\u4e86\u65b0\u7684\u542b\u4e49\uff1a\u5141\u8bb8\u591a\u91cd\u5b9a\u4e49\u3002\u5bf9\u4e8e\u51fd\u6570\u548c\u53d8\u91cf\u90fd\u662f\u9002\u7528\u7684\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u5c06\u51fd\u6570\u6807\u8bb0\u4e3a\u5185\u8054\u540e\uff0c\u8be5\u51fd\u6570\u5c31\u53ef\u4ee5\u88ab\u591a\u6b21\u5b9a\u4e49\u4e86\uff08\u5728\u4e0d\u540c\u7684\u51fd\u6570\u4e2d\uff09\uff0c\u53ea\u8981\u8fd9\u4e9b\u5b9a\u4e49\u662f\u4e00\u81f4\u7684\u5373\u53ef\u3002 \u4e3a\u4e86\u80fd\u591f\u5c55\u5f00\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u5fc5\u987b\u80fd\u591f\u5728\u51fd\u6570\u8c03\u7528\u5904\u770b\u5230\u51fd\u6570\u7684\u5b9a\u4e49\u3002\u56e0\u6b64\uff0c\u5185\u8054\u51fd\u6570\u901a\u5e38\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u5b83\u4eec\u5c31\u53ef\u4ee5\u88ab\u5305\u542b\u5230\u4f7f\u7528\u5b83\u4eec\u7684\u6e90\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u7f16\u8bd1\u5668\u770b\u5230\u51fd\u6570\u7684\u5b8c\u6574\u5b9a\u4e49\u3002 \u5173\u952e\u4fe1\u606f \u7f16\u8bd1\u5668\u5fc5\u987b\u80fd\u591f\u5728\u5185\u8054\u51fd\u6570\u8c03\u7528\u5904\u770b\u5230\u51fd\u6570\u7684\u5b8c\u6574\u5b9a\u4e49\u3002 \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4e0d\u9700\u8981\u5c06\u51fd\u6570\u5b9a\u4e49\u4e3a\u5185\u8054\uff0c\u4f46\u662f\u6211\u4eec\u5728\u5c06\u6765\u7684\u8bfe\u7a0b\u4e2d\u4f1a\u770b\u5230\u5b83\u6709\u7528\u7684\u573a\u666f\u3002 \u6700\u4f73\u5b9e\u8df5 \u9664\u975e\u6709\u7279\u6b8a\u7684\u3001\u4ee4\u4eba\u4fe1\u670d\u7684\u7406\u7531\uff0c\u5426\u5219\u4e0d\u8981\u4f7f\u7528 inline \u5173\u952e\u5b57\u3002","title":"6.13 - \u5185\u8054\u51fd\u6570"},{"location":"6-13-Inline-functions/#_1","text":"\u597d\u5728\uff0cC++\u7f16\u8bd1\u5668\u6709\u529e\u6cd5\u514b\u670d\u8fd9\u79cd\u5f00\u9500\uff1a\u5185\u8054\u5c55\u5f00\u53ef\u4ee5\u5c06\u51fd\u6570\u8c03\u7528\u6839\u636e\u5176\u5b9a\u4e49\u76f4\u63a5\u66ff\u6362\u4e3a\u5bf9\u5e94\u7684\u4ee3\u7801\u3002 \u4f8b\u5982\uff0c\u5982\u679c\u7f16\u8bd1\u5668\u80fd\u591f\u5c06\u4e0a\u9762\u7684 min() \u51fd\u6570\u5c55\u5f00\u7684\u8bdd\uff0c\u5c06\u4f1a\u5f97\u5230\u5982\u4e0b\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << (( 5 < 6 ) ? 5 : 6 ) << '\\n' ; std :: cout << (( 3 < 2 ) ? 3 : 2 ) << '\\n' ; return 0 ; } \u6ce8\u610f\uff0c min() \u51fd\u6570\u4e2d\u7684\u51fd\u6570\u8c03\u7528\u88ab\u5176\u51fd\u6570\u4f53\u4ee3\u7801\u66ff\u6362\uff08\u5b9e\u53c2\u6570\u503c\u66ff\u6362\u4e86\u5f62\u53c2\uff09\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u907f\u514d\u51fd\u6570\u8c03\u7528\u5e26\u6765\u7684\u989d\u5916\u5f00\u9500\u3002","title":"\u5185\u8054\u5c55\u5f00"},{"location":"6-13-Inline-functions/#_2","text":"\u9664\u4e86\u80fd\u591f\u907f\u514d\u51fd\u6570\u8c03\u7528\u7684\u5f00\u9500\uff0c\u5185\u8054\u5c55\u5f00\u8fd8\u53ef\u4ee5\u4f7f\u7f16\u8bd1\u5668\u66f4\u52a0\u6709\u6548\u5730\u4f18\u5316\u4ee3\u7801\u2014\u2014\u4f8b\u5982\uff0c\u56e0\u4e3a\u8868\u8fbe\u5f0f ((5 < 6) ? 5 : 6) \u73b0\u5728\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c31\u53ef\u4ee5\u5c06 main() \u51fd\u6570\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u4f18\u5316\u4e3a std::cout << 5 << '\\n'; \u3002 \u4e0d\u8fc7\uff0c\u5185\u8054\u5c55\u5f00\u4e5f\u6709\u5b83\u7684\u5f00\u9500\uff1a\u5982\u679c\u5c55\u5f00\u51fd\u6570\u4f53\u6240\u9700\u7684\u6307\u4ee4\u6bd4\u51fd\u6570\u8c03\u7528\u672c\u8eab\u8fd8\u591a\uff0c\u90a3\u4e48\u6bcf\u4e00\u5904\u5185\u8054\u5c55\u5f00\u5c06\u4f1a\u5bfc\u81f4\u53ef\u6267\u884c\u6587\u4ef6\u7684\u81a8\u80c0\u3002\u53ef\u6267\u884c\u7a0b\u5e8f\u8d8a\u5927\uff0c\u901f\u5ea6\u4e5f\u53ef\u80fd\u4f1a\u8d8a\u5dee\uff08\u7531\u4e8e\u4e0d\u80fd\u5339\u914d\u7f13\u5b58\u7684\u5927\u5c0f\uff09\u3002 \u786e\u5b9a\u51fd\u6570\u662f\u5426\u5e94\u8be5\u88ab\u5b9a\u4e49\u4e3a\u5185\u8054\uff08\u79fb\u9664\u51fd\u6570\u8c03\u7528\u5f00\u9500\u5e26\u6765\u7684\u597d\u5904\u5927\u4e8e\u53ef\u6267\u884c\u6587\u4ef6\u5927\u5c0f\u589e\u957f\u5e26\u6765\u7684\u574f\u5904\uff09\u7684\u4f9d\u636e\u5e76\u4e0d\u660e\u663e\u3002\u5185\u8054\u5c55\u5f00\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u7684\u63d0\u5347\u3001\u6027\u80fd\u7684\u4e0b\u964d\u6216\u8005\u5bf9\u6027\u80fd\u5b8c\u5168\u6ca1\u6709\u5f71\u54cd\uff0c\u8fd9\u53d6\u51b3\u4e8e\u51fd\u6570\u8c03\u7528\u7684\u5f00\u9500\uff0c\u51fd\u6570\u5927\u5c0f\u548c\u7f16\u8bd1\u5668\u80fd\u591f\u6267\u884c\u54ea\u4e9b\u4f18\u5316\u3002 \u5185\u8054\u5c55\u5f00\u6700\u9002\u5408\u7b80\u5355\u3001\u5c0f\u7684\u51fd\u6570\uff08\u4f8b\u5982\u53ea\u6709\u51e0\u884c\u8bed\u53e5\u7684\u51fd\u6570\uff09\uff0c\u7279\u522b\u662f\u4f1a\u88ab\u591a\u6b21\u8c03\u7528\u7684\u51fd\u6570\uff08\u4f8b\u5982\u5faa\u73af\u4e2d\u7684\u51fd\u6570\uff09\u3002","title":"\u5185\u8054\u4ee3\u7801\u7684\u6027\u80fd"},{"location":"6-13-Inline-functions/#_3","text":"\u6240\u6709\u7684\u51fd\u6570\u90fd\u53ef\u4ee5\u5f52\u4e3a\u4ee5\u4e0b\u4e09\u7c7b\uff1a \u5fc5\u987b\u88ab\u5c55\u5f00\uff1b \u53ef\u80fd\u4f1a\u88ab\u5c55\u5f00(\u5f88\u591a\u51fd\u6570\u90fd\u5c5e\u4e8e\u8fd9\u4e00\u7c7b). \u4e0d\u80fd\u88ab\u5c55\u5f00\u3002 \u80fd\u591f\u88ab\u5c55\u5f00\u7684\u51fd\u6570\u79f0\u4e3a\u5185\u8054\u51fd\u6570\u3002 \u5927\u591a\u6570\u51fd\u6570\u90fd\u5c5e\u4e8e\u7b2c\u4e8c\u7c7b\uff1a\u201c\u53ef\u80fd\u4f1a\u88ab\u5c55\u5f00\u201d\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u51fd\u6570\u6765\u8bf4\uff0c\u5982\u679c\u5c55\u5f00\u80fd\u591f\u5e26\u6765\u597d\u5904\uff0c\u90a3\u4e48\u5c31\u53ef\u80fd\u88ab\u5c55\u5f00\u3002\u5bf9\u4e8e\u8fd9\u4e00\u7c7b\u51fd\u6570\uff0c\u73b0\u4ee3\u7f16\u8bd1\u5668\u53ef\u4ee5\u5206\u6790\u6bcf\u4e2a\u51fd\u6570\u548c\u6bcf\u5904\u51fd\u6570\u8c03\u7528\u6765\u786e\u5b9a\u5bf9\u5e94\u7684\u51fd\u6570\u662f\u4e0d\u662f\u88ab\u5185\u8054\u5c55\u5f00\u540e\u7684\u6536\u76ca\u66f4\u9ad8\u3002\u7f16\u8bd1\u5668\u6700\u7ec8\u4f1a\u51b3\u5b9a\u662f\u5426\u5c06\u5b83\u4eec\u4e2d\u4e00\u4e9b\u3001\u6216\u8005\u5168\u90e8\u5c55\u5f00\u3002 \u5c0f\u8d34\u58eb \u73b0\u4ee3\u7f16\u8bd1\u5668\u53ef\u4ee5\u51b3\u5b9a\u51fd\u6570\u662f\u5426\u5e94\u5f53\u88ab\u5c55\u5f00\u3002 \u6269\u5c55\u9605\u8bfb \u6709\u4e9b\u51fd\u6570\u662f\u9690\u5f0f\u7684\u5185\u8054\u51fd\u6570\uff0c\u5305\u62ec\uff1a - \u5b9a\u4e49\u5728\u7c7b\u3001\u7ed3\u6784\u4f53\u6216\u8054\u5408\u4f53\u4e2d\u7684\u51fd\u6570\uff1b - Constexpr / consteval \u51fd\u6570 6.14 - Constexpr \u548c consteval \u51fd\u6570","title":"\u5185\u8054\u4ee3\u7801\u7684\u5c55\u5f00\u65f6\u673a"},{"location":"6-13-Inline-functions/#inline","text":"\u56de\u987e C++ \u7684\u5386\u53f2\uff0c\u7f16\u8bd1\u5668\u66fe\u7ecf\u8981\u4e48\u4e0d\u5177\u5907\u5224\u65ad\u51fd\u6570\u5c55\u5f00\u662f\u5426\u80fd\u591f\u5e26\u6765\u597d\u5904\u7684\u80fd\u529b\uff0c\u8981\u4e48\u4e0d\u80fd\u591f\u5f88\u597d\u7684\u6267\u884c\u4e0a\u8ff0\u5224\u65ad\u3002\u56e0\u6b64\uff0cC++\u7279\u610f\u63d0\u4f9b\u4e86 inline \u5173\u952e\u5b57\uff0c\u7528\u4e8e \u63d0\u793a \u7f16\u8bd1\u5668\u8be5\u51fd\u6570\u5e94\u8be5\u88ab\u5185\u8054\u5c55\u5f00\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include inline int min ( int x , int y ) // hint to the compiler that it should do inline expansion of this function { return ( x < y ) ? x : y ; } int main () { std :: cout << min ( 5 , 6 ) << '\\n' ; std :: cout << min ( 3 , 2 ) << '\\n' ; return 0 ; } \u8fd9\u5c31\u662f\u6240\u8c13\u201c\u5185\u8054\u51fd\u6570\u201d\u7684\u6765\u5386\uff08\u56e0\u4e3a\u8fd9\u4e9b\u51fd\u6570\u7684\u58f0\u660e\u8bed\u6cd5\u4e2d\u5305\u542b inline \u5173\u952e\u5b57\uff09\u3002 \u4f46\u662f\uff0c\u73b0\u4ee3 C++ \u4e2d\uff0c inline \u5173\u952e\u5b57\u5df2\u7ecf\u4e0d\u518d\u88ab\u7528\u4e8e\u544a\u8bc9\u7f16\u8bd1\u5668\u5c55\u5f00\u5bf9\u5e94\u7684\u51fd\u6570\uff0c\u8fd9\u4e48\u505a\u7684\u539f\u56e0\u6709\u5f88\u591a\uff1a \u4f7f\u7528 inline \u8981\u6c42\u5185\u8054\u5c55\u5f00\u662f\u4e00\u79cd\u8fc7\u65e9\u4f18\u5316\u884c\u4e3a\uff0c\u6ee5\u7528 inline \u6709\u53cd\u5012\u4f1a\u4f24\u5bb3\u7a0b\u5e8f\u6027\u80fd\uff1b inline \u5173\u952e\u5b57\u53ea\u662f\u4e00\u4e2a \u63d0\u793a \u2014\u2014\u7f16\u8bd1\u5668\u5b8c\u5168\u53ef\u4ee5\u5ffd\u7565\u5b83\uff0c\u5c24\u5176\u662f\u5f53\u4f60\u8981\u6c42\u7f16\u8bd1\u5668\u5185\u8054\u5c55\u5f00\u4e00\u4e2a\u5f88\u5927\u7684\u51fd\u6570\u65f6\u3002\u540c\u65f6\uff0c\u7f16\u8bd1\u5668\u4e5f\u53ef\u4ee5\u5c55\u5f00\u90a3\u4e9b\u5e76\u6ca1\u6709\u88ab\u6807\u8bb0\u4e3a inline \u7684\u51fd\u6570\u3002 inline \u5173\u952e\u5b57\u7684\u5b9a\u4e49\u5904\u4e8e\u4e00\u79cd\u4e0d\u5408\u9002\u7684\u7c92\u5ea6\u3002\u6211\u4eec\u5728\u51fd\u6570\u58f0\u660e\u4e2d\u4f7f\u7528\u8be5\u5173\u952e\u5b57\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u51fd\u6570\u7684\u5c55\u5f00\u662f\u57fa\u4e8e\u6bcf\u4e2a\u51fd\u6570\u8c03\u7528\u7684\u3002\u5bf9\u4e8e\u4e00\u4e2a\u51fd\u6570\u7684\u5404\u5904\u8c03\u7528\uff0c\u5b83\u4eec\u4e2d\u7684\u4e00\u90e8\u5206\u53ef\u80fd\u88ab\u5c55\u5f00\uff0c\u800c\u53e6\u4e00\u4e9b\u5219\u4e0d\u88ab\u5c55\u5f00\uff0c\u7136\u800c\u8fd9\u4e2a\u5173\u952e\u5b57\u5374\u4e0d\u80fd\u5bf9\u6b64\u4ea7\u751f\u5f71\u54cd\u3002 \u73b0\u4ee3\u7f16\u8bd1\u5668\u901a\u5e38\u5f88\u64c5\u957f\u51b3\u5b9a\u90a3\u4e9b\u51fd\u6570\u5e94\u8be5\u88ab\u4f5c\u4e3a\u5185\u8054\u51fd\u6570\u2014\u2014\u5b83\u7684\u8fd9\u79cd\u80fd\u529b\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u90fd\u6bd4\u4eba\u7c7b\u5f3a\u3002 \u56e0\u6b64\uff0c\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u5ffd\u7565\u4f60\u4e3a\u51fd\u6570\u6dfb\u52a0\u7684 inline \u6807\u8bb0\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e0d\u8981\u4f7f\u7528 inline \u5173\u952e\u5b57\u8981\u6c42\u5185\u8054\u5c55\u5f00\u51fd\u6570\u3002","title":"\u5386\u53f2\u4e0a\u7684 inline \u5173\u952e\u5b57"},{"location":"6-13-Inline-functions/#inline_1","text":"\u5728\u4e4b\u524d\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u5934\u6587\u4ef6\u4e2d\u4e0d\u5e94\u8be5\u653e\u7f6e\u51fd\u6570\u7684\u5b9e\u73b0\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u5934\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u4f1a\u88ab\u5305\u542b\u5230\u591a\u4e2a.cpp\u6587\u4ef6\u4e2d\uff0c\u90a3\u4e48\u8fd9\u4e9b\u51fd\u6570\u7684\u5b9a\u4e49\u4e5f\u5c31\u4f1a\u88ab\u590d\u5236\u591a\u4efd\u3002\u56e0\u6b64\uff0c\u8fd9\u4e9b\u6587\u4ef6\u7f16\u8bd1\u540e\uff0c\u94fe\u63a5\u5668\u5c31\u4f1a\u62a5\u9519\uff0c\u56e0\u4e3a\u51fd\u6570\u7684\u5b9a\u4e49\u8fdd\u53cd\u4e86 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u3002 \u5728 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\uff0c\u5728\u73b0\u4ee3 C++ \u4e2d\uff0c inline \u6982\u5ff5\u53d1\u5c55\u51fa\u4e86\u65b0\u7684\u542b\u4e49\uff1a\u5141\u8bb8\u591a\u91cd\u5b9a\u4e49\u3002\u5bf9\u4e8e\u51fd\u6570\u548c\u53d8\u91cf\u90fd\u662f\u9002\u7528\u7684\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u5c06\u51fd\u6570\u6807\u8bb0\u4e3a\u5185\u8054\u540e\uff0c\u8be5\u51fd\u6570\u5c31\u53ef\u4ee5\u88ab\u591a\u6b21\u5b9a\u4e49\u4e86\uff08\u5728\u4e0d\u540c\u7684\u51fd\u6570\u4e2d\uff09\uff0c\u53ea\u8981\u8fd9\u4e9b\u5b9a\u4e49\u662f\u4e00\u81f4\u7684\u5373\u53ef\u3002 \u4e3a\u4e86\u80fd\u591f\u5c55\u5f00\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u5fc5\u987b\u80fd\u591f\u5728\u51fd\u6570\u8c03\u7528\u5904\u770b\u5230\u51fd\u6570\u7684\u5b9a\u4e49\u3002\u56e0\u6b64\uff0c\u5185\u8054\u51fd\u6570\u901a\u5e38\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u5b83\u4eec\u5c31\u53ef\u4ee5\u88ab\u5305\u542b\u5230\u4f7f\u7528\u5b83\u4eec\u7684\u6e90\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u7f16\u8bd1\u5668\u770b\u5230\u51fd\u6570\u7684\u5b8c\u6574\u5b9a\u4e49\u3002 \u5173\u952e\u4fe1\u606f \u7f16\u8bd1\u5668\u5fc5\u987b\u80fd\u591f\u5728\u5185\u8054\u51fd\u6570\u8c03\u7528\u5904\u770b\u5230\u51fd\u6570\u7684\u5b8c\u6574\u5b9a\u4e49\u3002 \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4e0d\u9700\u8981\u5c06\u51fd\u6570\u5b9a\u4e49\u4e3a\u5185\u8054\uff0c\u4f46\u662f\u6211\u4eec\u5728\u5c06\u6765\u7684\u8bfe\u7a0b\u4e2d\u4f1a\u770b\u5230\u5b83\u6709\u7528\u7684\u573a\u666f\u3002 \u6700\u4f73\u5b9e\u8df5 \u9664\u975e\u6709\u7279\u6b8a\u7684\u3001\u4ee4\u4eba\u4fe1\u670d\u7684\u7406\u7531\uff0c\u5426\u5219\u4e0d\u8981\u4f7f\u7528 inline \u5173\u952e\u5b57\u3002","title":"\u73b0\u4ee3 inline \u5173\u952e\u5b57"},{"location":"6-14-Constexpr-and-consteval-functions/","tags":["constexpr","consteval","C++20"],"text":"Key Takeaway \u4e3a\u4e86\u80fd\u591f\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u51fd\u6570\u5fc5\u987b\u5177\u6709 constexpr \u7c7b\u578b\u7684\u8fd4\u56de\u503c\uff0c\u540c\u65f6\u4e0d\u80fd\u591f\u8c03\u7528\u4efb\u4f55\u975econstexpr\u51fd\u6570\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u4e5f\u5fc5\u987b\u4f7f\u7528 constexpr \u5b9e\u53c2 (\u4f8b\u5982 constexpr \u53d8\u91cf\u6216\u5b57\u9762\u91cf)\u3002 constexpr \u51fd\u6570\u5177\u6709\u9690\u542b\u7684\u5185\u8054\u5c5e\u6027\uff0c\u8fd9\u4f7f\u5f97\u5b83\u4eec\u80fd\u591f\u514d\u4e8e\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u7684\u9650\u5236\u3002\u56e0\u4e3a\u8fd0\u884c\u65f6\u6c42\u503c\u5fc5\u987b\u80fd\u591f\u8ba9\u7f16\u8bd1\u5668\u5728\u51fd\u6570\u8c03\u7528\u5904\u770b\u5230\u51fd\u6570\u5b8c\u6574\u5b9a\u4e49\u3002\u6240\u4ee5\u5b83\u4eec\u5e94\u8be5\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u4e14\u88ab\u5305\u542b\u5230\u4f7f\u7528\u5b83\u4eec\u7684\u6e90\u6587\u4ef6\u3002 \u6839\u636e C++ \u6807\u51c6\uff0c\u5982\u679c constexpr \u51fd\u6570 \u6709\u8d44\u683c \u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c \u800c\u4e14\u5b83\u7684\u8fd4\u56de\u503c\u88ab\u7528\u5728\u5e38\u6570\u8868\u8fbe\u5f0f\u4e2d\uff0c\u90a3\u4e48\u5c31 \u5fc5\u987b \u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\u3002 \u4f7f\u7528\u6ee1\u8db3\u6761\u4ef6\u7684\u51fd\u6570\u8fd4\u56de\u503c\u521d\u59cb\u5316\u4e00\u4e2a constexpr \u53d8\u91cf\u53ef\u4ee5\u786e\u4fdd\u51fd\u6570\u5728\u7f16\u8bd1\u65f6\u6c42\u503c C++20 \u5f15\u5165\u4e86 consteval \u5173\u952e\u5b57\uff0c\u5b83\u53ef\u4ee5\u6307\u5b9a\u51fd\u6570 \u5fc5\u987b \u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u5426\u5219\u5c06\u4f1a\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002\u8fd9\u79cd\u51fd\u6570\u88ab\u79f0\u4e3a \u5373\u65f6\u51fd\u6570(immediate functions) \u3002 \u4f7f\u7528 consteval \u53ef\u4ee5\u8ba9 constexpr \u51fd\u6570\u5728\u7f16\u8bd1\u65f6\u6267\u884c \u5728 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86 constexpr \u5173\u952e\u5b57\uff0c\u5b9e\u7528\u5b83\u53ef\u4ee5\u521b\u5efa \u7f16\u8bd1\u65f6 \u7684 \u7b26\u53f7\u5e38\u91cf \u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86\u5e38\u91cf\u8868\u8fbe\u5f0f\u2014\u2014\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\u8868\u8fbe\u5f0f\u3002 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { constexpr int x { 5 }; constexpr int y { 6 }; std :: cout << ( x > y ? x : y ) << \" is greater!\" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 6 is greater! \u56e0\u4e3a x \u548c y \u662f constexpr \uff0c\u7f16\u8bd1\u5668\u4f1a\u5728\u7f16\u8bd1\u65f6\u5bf9 (x > y ? x : y) \u8fdb\u884c\u6c42\u503c\uff0c\u4f7f\u7528 6 \u6765\u66ff\u6362\u8be5\u8868\u8fbe\u5f0f\u3002\u56e0\u4e3a\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u65e0\u9700\u5728\u8fd0\u884c\u65f6\u6c42\u503c\uff0c\u7a0b\u5e8f\u663e\u7136\u4f1a\u8fd0\u884c\u5730\u66f4\u5feb\u3002 \u4e0d\u8fc7\uff0c\u5728\u6253\u5370\u8bed\u53e5\u4e2d\u4f7f\u7528\u8fd9\u6837\u4e00\u4e2a\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u5e76\u4e0d\u662f\u7406\u60f3\u7684\u65b9\u5f0f\u2014\u2014\u6700\u597d\u662f\u5c06\u8be5\u8868\u8fbe\u5f0f\u5c01\u88c5\u6210\u4e00\u4e2a\u51fd\u6570\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u5c31\u662f\u8fd9\u6837\u505a\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int greater ( int x , int y ) { return ( x > y ? x : y ); // \u8868\u8fbe\u5f0f\u5728\u6b64 } int main () { constexpr int x { 5 }; constexpr int y { 6 }; std :: cout << greater ( x , y ) << \" is greater!\" ; // \u8fd0\u884c\u65f6\u6c42\u503c return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u7684\u8fd0\u884c\u7ed3\u679c\u548c\u524d\u4e00\u4e2a\u7a0b\u5e8f\u5e76\u6ca1\u6709\u533a\u522b\u3002\u4f46\u662f\u5b83\u7684\u7f3a\u70b9\u662f\uff0c\u5c06\u8868\u8fbe\u5f0f\u5c01\u88c5\u6210\u51fd\u6570\u540e\uff0c greater(x, y) \u51fd\u6570\u5fc5\u987b\u5728\u8fd0\u884c\u65f6\u88ab\u8c03\u7528\u3002\u4f7f\u7528\u51fd\u6570\u4ee3\u66ff\u8868\u8fbe\u5f0f\u4e4b\u540e\uff0c\u6211\u4eec\u5931\u53bb\u4e86\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\u80fd\u529b\uff08\u4f24\u5bb3\u6027\u80fd\uff09\u3002 \u90a3\u4e48\u5e94\u8be5\u5982\u4f55\u89e3\u51b3\u8be5\u95ee\u9898\u5462\uff1f Constexpr \u51fd\u6570\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u6c42\u503c constexpr \u51fd\u6570\u7684\u8fd4\u56de\u503c\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u4e3a\u4e86\u5c06\u51fd\u6570\u5b9a\u4e49\u4e3a constexpr \u7c7b\u578b\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5728\u8fd4\u56de\u503c\u7c7b\u578b\u524d\u6dfb\u52a0 constexpr \u5173\u952e\u5b57\u5373\u53ef\u3002\u8bf7\u89c1\u4e0b\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include constexpr int greater ( int x , int y ) // now a constexpr function { return ( x > y ? x : y ); } int main () { constexpr int x { 5 }; constexpr int y { 6 }; // We'll explain why we use variable g here later in the lesson constexpr int g { greater ( x , y ) }; // will be evaluated at compile-time std :: cout << g << \" is greater!\" ; return 0 ; } \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4ecd\u7136\u662f\u4e00\u6837\u7684\uff0c \u4f46\u662f greater() \u51fd\u6570\u4f1a\u5728\u7f16\u8bd1\u65f6\u8fdb\u884c\u6c42\u503c\uff01 \u4e3a\u4e86\u80fd\u591f\u6709\u8d44\u683c\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u51fd\u6570\u5fc5\u987b\u5177\u6709 constexpr \u7c7b\u578b\u7684\u8fd4\u56de\u503c\uff0c\u540c\u65f6\u4e0d\u80fd\u591f\u8c03\u7528\u4efb\u4f55\u975econstexpr\u51fd\u6570\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u4e5f\u5fc5\u987b\u4f7f\u7528 constexpr \u5b9e\u53c2 (\u4f8b\u5982 constexpr \u53d8\u91cf\u6216\u5b57\u9762\u91cf)\u3002 \u4f5c\u8005\u6ce8 \u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u6587\u7ae0\u4e2d\u4f7f\u7528\u201c\u6709\u8d44\u683c\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u201d\u8fd9\u4e2a\u672f\u8bed\u3002 \u6269\u5c55\u9605\u8bfb \u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u8981\u6c42\u89c1 \u8fd9\u91cc . greater() \u51fd\u6570\u80fd\u591f\u6ee1\u8db3\u4e0a\u8ff0\u8981\u6c42\uff0c\u6240\u4ee5\u5b83\u6709\u8d44\u683c\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528 constexpr \u8fd4\u56de\u7c7b\u578b\u5fc5\u987b\u8fd4\u56de\u4e00\u4e2a\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 Constexpr \u51fd\u6570\u5177\u6709\u9690\u542b\u5185\u8054\u5c5e\u6027 \u56e0\u4e3a constexpr \u51fd\u6570\u53ef\u80fd\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u5fc5\u987b\u80fd\u591f\u5728\u8c03\u7528\u8be5\u51fd\u6570\u7684\u5730\u65b9\u770b\u5230\u8be5\u51fd\u6570\u7684\u5b8c\u6574\u5b9a\u4e49\u3002 \u8fd9\u4e5f\u610f\u5473\u7740 constexpr \u51fd\u6570\u5982\u679c\u591a\u4e2a\u6587\u4ef6\u4e2d\u88ab\u8c03\u7528\uff0c\u90a3\u4e48\u5b83\u7684\u5b9a\u4e49\u4e5f\u5fc5\u987b\u6bcf\u4e2a\u6587\u4ef6\u4e00\u4efd\u2014\u2014\u8fd9\u5c31\u53ef\u80fd\u4f1a\u8fdd\u53cd \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\uff0c constexpr \u51fd\u6570\u5177\u6709\u9690\u542b\u7684\u5185\u8054\u5c5e\u6027\uff0c\u8fd9\u4f7f\u5f97\u5b83\u4eec\u80fd\u591f\u514d\u4e8e\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u7684\u9650\u5236\u3002 \u56e0\u6b64\uff0c constexpr \u51fd\u6570\u4e5f\u901a\u5e38\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u56e0\u6b64\u5b83\u4eec\u53ef\u4ee5\u88ab #included \u5230\u4efb\u4f55 .cpp \u6587\u4ef6\u4ee5\u63d0\u4f9b\u51fd\u6570\u7684\u5b8c\u6574\u5b9a\u4e49\u3002 Constexpr \u51fd\u6570\u4e5f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6c42\u503c \u5177\u6709 constexpr \u8fd4\u56de\u503c\u7c7b\u578b\u7684\u51fd\u6570\u4e5f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6c42\u503c\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5b83\u4f1a\u8fd4\u56de\u975e constexpr \u7684\u7ed3\u679c\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include constexpr int greater ( int x , int y ) { return ( x > y ? x : y ); } int main () { int x { 5 }; // \u975e constexpr int y { 6 }; // \u975e constexpr std :: cout << greater ( x , y ) << \" is greater!\" ; // will be evaluated at runtime return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u53c2\u6570 x \u548c y \u5e76\u975e constexpr \u7c7b\u578b\uff0c\u6240\u4ee5\u51fd\u6570\u5c31\u4e0d\u80fd\u5728\u7f16\u8bd1\u65f6\u88ab\u6c42\u503c\u3002\u4e0d\u8fc7\uff0c\u51fd\u6570\u8fd8\u662f\u53ef\u4ee5\u5728 \u8fd0\u884c\u65f6 \u6c42\u503c\u7684\uff0c\u53ea\u4e0d\u8fc7\u8fd4\u56de\u7684\u503c\u662f\u975e constexpr \u7c7b\u578b\u7684 int \u3002 \u5173\u952e\u4fe1\u606f \u4f7f\u51fd\u6570\u5177\u6709 constexpr \u8fd4\u56de\u503c\u7c7b\u578b\u5373\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u4e5f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6c42\u503c\uff0c \u56e0\u6b64\u8fd9\u6837\u7684\u51fd\u6570\u53ef\u4ee5\u6ee1\u8db3\u4e24\u79cd\u573a\u666f\u3002\u5982\u679c\u4e0d\u662f\u8fd9\u6837\u7684\u8bdd\uff0c\u4f60\u5c31\u5fc5\u987b\u5b9a\u4e49\u4e24\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\uff08constexpr \u7248\u672c\u548c\u975e constexpr \u7248\u672c\uff09\uff0c\u4f46\u662f\u7531\u4e8e\u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u80fd\u7528\u6765\u533a\u522b\u51fd\u6570\u91cd\u8f7d\uff0c\u6240\u4ee5\u4f60\u5c31\u4e0d\u5f97\u4e0d\u4e3a\u5b83\u8d77\u4e00\u4e2a\u4e0d\u540c\u7684\u540d\u5b57\u3002 \u90a3\u4e48\uff0cconstexpr \u51fd\u6570\u4ec0\u4e48\u65f6\u5019\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff1f \u4f60\u53ef\u80fd\u4f1a\u89c9\u5f97\uff0c constexpr \u51fd\u6570\u5982\u679c\u53ef\u80fd\u7684\u8bdd\u603b\u662f\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u8fd9\u6837\u3002 \u6839\u636e C++ \u6807\u51c6\uff0c\u5982\u679c constexpr \u51fd\u6570\u6709\u8d44\u683c\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c \u800c\u4e14\u5b83\u7684\u8fd4\u56de\u503c\u88ab\u7528\u5728\u5e38\u6570\u8868\u8fbe\u5f0f\u4e2d\uff0c\u90a3\u4e48\u5c31 \u5fc5\u987b \u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u5219\u53ef\u4ee5\u81ea\u7531\u9009\u62e9\u662f\u5728\u7f16\u8bd1\u65f6\u8fd8\u662f\u8fd0\u884c\u65f6\u6c42\u503c\u3002 \u8ba9\u6211\u4eec\u901a\u8fc7\u4e0b\u9762\u7684\u4f8b\u5b50\u8fdb\u884c\u66f4\u8fdb\u4e00\u6b65\u7684\u89e3\u91ca\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include constexpr int greater ( int x , int y ) { return ( x > y ? x : y ); } int main () { constexpr int g { greater ( 5 , 6 ) }; // case 1: \u5728\u7f16\u8bd1\u65f6\u6c42\u503c std :: cout << g << \"is greater!\" ; int x { 5 }; // not constexpr std :: cout << greater ( x , 6 ) << \" is greater!\" ; // case 2: \u5728\u8fd0\u884c\u65f6\u6c42\u503c std :: cout << greater ( 5 , 6 ) << \" is greater!\" ; // case 3: \u5728\u7f16\u8bd1\u65f6\u6216\u8005\u8fd0\u884c\u65f6\u6c42\u503c\u90fd\u53ef\u80fd return 0 ; } \u5bf9\u4e8e case 1\uff0c\u6211\u4eec\u901a\u8fc7 constexpr \u53c2\u6570\u8c03\u7528\u4e86 greater() \u51fd\u6570\uff0c\u56e0\u6b64\u5b83\u6709\u8d44\u683c\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u540c\u65f6\uff0c constexpr \u53d8\u91cf g \u7684\u521d\u59cb\u5316\u503c\u5fc5\u987b\u662f constexpr \uff0c\u6240\u4ee5\u51fd\u6570\u7684\u8fd4\u56de\u503c\u88ab\u7528\u5728\u5e38\u91cf\u8868\u8fbe\u5f0f\u4e2d\u3002\u56e0\u6b64 greater() \u5fc5\u987b\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\u3002 \u5728 case 2 \u4e2d\uff0c\u6211\u4eec\u8c03\u7528 greater() \u65f6\u6240\u7528\u7684\u5176\u4e2d\u4e00\u4e2a\u53c2\u6570\u662f\u975e constexpr \u7c7b\u578b\u7684\u3002\u56e0\u6b64 greater() \u5e76\u4e0d\u80fd\u591f\u5728\u7f16\u8bd1\u65f6\u88ab\u6c42\u503c\uff0c\u53ea\u80fd\u5728\u8fd0\u884c\u65f6\u6c42\u503c\u3002 case 3 \u5c31\u6709\u610f\u601d\u4e86\u3002 greater() \u5728\u8c03\u7528\u65f6\u4f7f\u7528\u4e86 constexpr \u53c2\u6570\uff0c\u6240\u4ee5\u5b83\u662f\u6709\u8d44\u683c\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\u3002\u4f46\u662f\u5b83\u7684\u8fd4\u56de\u503c\u5e76\u6ca1\u6709\u7528\u5728\u9700\u8981\u5e38\u6570\u8868\u8fbe\u5f0f\u7684\u5730\u65b9\uff08 << \u603b\u662f\u5728\u8fd0\u884c\u65f6\u6c42\u503c\uff09\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u53ef\u4ee5\u81ea\u884c\u51b3\u5b9a greater() \u51fd\u6570\u662f\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u51fd\u6570\u8fd0\u884c\u65f6\uff01 \u6ce8\u610f\uff0c\u4f60\u7684\u7f16\u8bd1\u5668\u4f18\u5316\u7b49\u7ea7\u8bbe\u7f6e\u53ef\u80fd\u4f1a\u5f71\u54cd\u5230\u5b83\u5bf9\u51fd\u6570\u5e94\u8be5\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u8fd8\u662f\u8fd0\u884c\u65f6\u6c42\u503c\u7684\u51b3\u5b9a\u3002\u8fd9\u4e5f\u610f\u5473\u7740\u7f16\u8bd1\u5668\u5728 debug \u548c release \u6784\u5efa\u65f6\u53ef\u80fd\u4f1a\u505a\u51fa\u4e0d\u540c\u7684\u51b3\u5b9a\uff08\u56e0\u4e3adebug\u6a21\u5f0f\u4e0b\u901a\u5e38\u4f1a\u5173\u95ed\u4f18\u5316\uff09\u3002 \u5173\u952e\u4fe1\u606f constexpr \u51fd\u6570\u662f\u6709\u8d44\u683c\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\uff0c\u800c\u4e14\u5b83\u53ea\u6709\u5728\u8fd4\u56de\u503c\u88ab\u7528\u5728\u9700\u8981\u5e38\u91cf\u8868\u8fbe\u5f0f\u7684\u5730\u65b9\u65f6\uff0c\u5b83\u624d\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u5176\u4ed6\u60c5\u51b5\u4e0b\u662f\u5426\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u662f\u4e0d\u80fd\u4fdd\u8bc1\u7684\u3002 \u56e0\u6b64\uff0c constexpr \u51fd\u6570\u6700\u597d\u88ab\u770b\u505a\u662f\u201c\u53ef\u4ee5\u88ab\u7528\u5728\u5e38\u91cf\u8868\u8fbe\u5f0f\u201d\u800c\u4e0d\u662f\u201c\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u201d\u3002 \u786e\u5b9a constexpr \u51fd\u6570\u8c03\u7528\u662f\u5426\u662f\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684 \u5728 C++20 \u4ee5\u524d\uff0c\u5e76\u6ca1\u6709\u6807\u51c6\u7684\u8bed\u8a00\u5de5\u5177\u53ef\u4ee5\u5b8c\u6210\u8be5\u4efb\u52a1\u3002 \u5728 C++20 \u4e2d std::is_constant_evaluated() (\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d)\u4f1a\u8fd4\u56de\u4e00\u4e2a bool \u7c7b\u578b\u7684\u7ed3\u679c\u6307\u793a\u5f53\u524d\u51fd\u6570\u8c03\u7528\u662f\u5426\u7684\u5e38\u91cf\u4e0a\u4e0b\u6587\u4e2d\u6267\u884c\u7684\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u6761\u4ef6\u8bed\u53e5\u6765\u4f7f\u5f97\u51fd\u6570\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u548c\u8fd0\u884c\u65f6\u6c42\u503c\u65f6\u8868\u73b0\u51fa\u4e0d\u540c\u7684\u884c\u4e3a\u3002 1 2 3 4 5 6 7 8 #include // for std::is_constant_evaluated constexpr int someFunction () { if ( std :: is_constant_evaluated ()) // if compile-time evaluation // do something else // runtime evaluation // do something else } \u4f7f\u7528\u5f97\u5f53\u7684\u8bdd\uff0c\u4f60\u53ef\u4ee5\u4f60\u53ef\u4ee5\u8ba9\u51fd\u6570\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u65f6\u4ea7\u751f\u53ef\u4ee5\u88ab\u89c2\u5bdf\u5230\u7684\u4e0d\u540c\u884c\u4e3a\uff08\u4f8b\u5982\u8fd4\u56de\u4e00\u4e2a\u7279\u6b8a\u7684\u503c\uff09\uff0c\u7136\u540e\u4f60\u53ef\u4ee5\u901a\u8fc7\u8be5\u503c\u6765\u5224\u65ad\u51fd\u6570\u662f\u5728\u4f55\u65f6\u6c42\u503c\u7684\uff0c \u5f3a\u5236 constexpr \u51fd\u6570\u5728\u7f16\u8bd1\u65f6\u6c42\u503c \u6ca1\u6709\u529e\u6cd5\u53bb\u544a\u8bc9\u7f16\u8bd1\u5668 constexpr \u51fd\u6570\u5e94\u8be5\u5728\u6761\u4ef6\u5141\u8bb8\u7684\u60c5\u51b5\u4e0b\u5c3d\u53ef\u80fd\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff08\u5373\u4f7f\u8fd4\u56de\u503c\u88ab\u7528\u5728\u975e\u5e38\u6570\u8868\u8fbe\u5f0f\u65f6\uff09\u3002 \u4e0d\u8fc7\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8ba9\u51fd\u6570\u8fd4\u56de\u503c\u4f7f\u7528\u5728\u5e38\u6570\u8868\u8fbe\u5f0f\u4e2d\u4ee5\u786e\u4fdd\u5177\u5907\u6761\u4ef6\u7684\u51fd\u6570\u80fd\u591f\u786e\u4fdd\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u5f53\u7136\uff0c\u8fd9\u662f\u9488\u5bf9\u6bcf\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u800c\u8a00\u7684\u3002 \u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u8fd4\u56de\u503c\u6765\u521d\u59cb\u5316\u4e00\u4e2a constexpr \u53d8\u91cf\uff08\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728\u4e4b\u524d\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u4f7f\u7528\u4e86 \u2018g\u2019 \u8fd9\u4e2a\u53d8\u91cf\uff09\u3002\u53ef\u60dc\u7684\u662f\uff0c\u8fd9\u9700\u8981\u4e3a\u7a0b\u5e8f\u5f15\u5165\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\u800c\u4e14\u8fd9\u4e2a\u53d8\u91cf\u53ea\u662f\u4e3a\u4e86\u786e\u4fdd\u51fd\u6570\u662f\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\uff0c\u8fd9\u6837\u975e\u5e38\u4e0d\u4f18\u96c5\u800c\u4e14\u4e5f\u4f1a\u964d\u4f4e\u7a0b\u5e8f\u7684\u53ef\u8bfb\u6027\u3002 \u6269\u5c55\u9605\u8bfb \u4e3a\u4e86\u907f\u514d\u6bcf\u6b21\u90fd\u9700\u8981\u5f15\u5165\u4e00\u4e2a\u65b0\u7684 constexpr \u53d8\u91cf\u624d\u80fd\u786e\u4fdd\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u5f88\u591a\u4eba\u5c1d\u8bd5\u4e86\u4e0d\u5c11\u89e3\u51b3\u529e\u6cd5\u3002\u53c2\u89c1 \u8fd9\u91cc \u548c \u8fd9\u91cc \u3002 \u4e0d\u8fc7\uff0cC++20 \u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u529e\u6cd5\uff0c\u9a6c\u4e0a\u6211\u4eec\u5c31\u80fd\u770b\u5230\u3002 Consteval (C++20) C++20 \u5f15\u5165\u4e86 consteval \u5173\u952e\u5b57\uff0c\u5b83\u53ef\u4ee5\u6307\u5b9a\u51fd\u6570 \u5fc5\u987b \u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u5426\u5219\u5c06\u4f1a\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002\u8fd9\u79cd\u51fd\u6570\u88ab\u79f0\u4e3a \u5373\u65f6\u51fd\u6570(immediate functions) \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include consteval int greater ( int x , int y ) // \u51fd\u6570\u73b0\u5728\u662f consteval \u7c7b\u578b { return ( x > y ? x : y ); } int main () { constexpr int g { greater ( 5 , 6 ) }; // ok: will evaluate at compile-time std :: cout << greater ( 5 , 6 ) << \" is greater!\" ; // ok: will evaluate at compile-time int x { 5 }; // not constexpr std :: cout << greater ( x , 6 ) << \" is greater!\" ; // \u9519\u8bef: consteval \u51fd\u6570\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u6c42\u503c return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u524d\u9762\u4e24\u5904 greater() \u51fd\u6570\u8c03\u7528\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u800c greater(x, 6) \u5e76\u4e0d\u80fd\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u6240\u4ee5\u4f1a\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002 \u548c constexpr \u51fd\u6570\u4e00\u6837\uff0c consteval \u4e5f\u662f\u9690\u5f0f\u5185\u8054\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f60\u6709\u51fd\u6570\u51fa\u4e8e\u67d0\u4e9b\u539f\u56e0\uff08\u4f8b\u5982\u6027\u80fd\uff09\u800c\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 consteval \u3002 \u4f7f\u7528 consteval \u8ba9 constexpr \u5728\u7f16\u8bd1\u65f6\u6267\u884c(C++20) consteval \u51fd\u6570\u662f\u4e0d\u80fd\u591f\u5728\u8fd0\u884c\u65f6\u6c42\u503c\u7684\uff0c\u8fd9\u4e5f\u4f7f\u5176\u4e0d\u5982 constexpr \u51fd\u6570\u90a3\u6837\u7075\u6d3b\uff08\u65e2\u80fd\u591f\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u4e5f\u80fd\u591f\u5728\u8fd0\u884c\u65f6\u6c42\u503c\uff09\u3002\u6240\u4ee5\uff0c\u5982\u679c\u80fd\u591f\u6709\u529e\u6cd5\u5f3a\u5236 constexpr \u51fd\u6570\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff08\u5373\u4f7f\u5b83\u7684\u8fd4\u56de\u503c\u6ca1\u6709\u88ab\u7528\u5728\u5e38\u6570\u8868\u8fbe\u5f0f\u4e2d\uff09\uff0c\u4e5f\u662f\u975e\u5e38\u6709\u7528\u7684\u3002\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u8ba9\u51fd\u6570\u5c3d\u53ef\u80fd\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u5982\u679c\u51fd\u6570\u4e0d\u5177\u5907 \u8d44\u683c \uff0c\u5219\u5728\u8fd0\u884c\u65f6\u6c42\u503c\u3002 Consteval \u51fd\u6570\u4e3a\u4e0a\u9762\u7684\u60f3\u6cd5\u63d0\u4f9b\u4e86\u5b9e\u73b0\u7684\u53ef\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7b80\u6d01\u7684\u5e2e\u52a9\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include // Uses abbreviated function template (C++20) and `auto` return type to make this function work with any type of value // See 'related content' box below for more info (you don't need to know how these work to use this function) consteval auto compileTime ( auto value ) { return value ; } constexpr int greater ( int x , int y ) // \u51fd\u6570\u662f constexpr \u7c7b\u578b\u7684 { return ( x > y ? x : y ); } int main () { std :: cout << greater ( 5 , 6 ); // \u53ef\u80fd\u4f1a\uff0c\u4e5f\u53ef\u80fd\u4e0d\u4f1a\u5728\u7f16\u8bd1\u65f6\u6267\u884c std :: cout << compileTime ( greater ( 5 , 6 )); // \u4f1a\u5728\u7f16\u8bd1\u65f6\u6267\u884c int x { 5 }; std :: cout << greater ( x , 6 ); // \u4ecd\u7136\u53ef\u4ee5\u8c03\u7528 constexpr \u7248\u672c\u4f7f\u5176\u5728\u8fd0\u884c\u65f6\u6267\u884c return 0 ; } consteval \u51fd\u6570\u9700\u8981\u5e38\u91cf\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u53c2\u6570\u2014\u2014\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u5c06 constexpr \u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5b83\uff0c\u5219 constexpr \u51fd\u6570\u5c31\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002 consteval \u51fd\u6570\u53ea\u662f\u628a\u53c2\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c\u8fd4\u56de\u4e86\uff0c\u56e0\u6b64\u8c03\u7528\u8005\u8fd8\u662f\u53ef\u4ee5\u6b63\u5e38\u4f7f\u7528\u8be5\u503c\u7684\u3002 \u6ce8\u610f\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u51fd\u6570\u662f \u503c\u8fd4\u56de \uff0c\u867d\u7136\u5728\u8fd0\u884c\u65f6\u503c\u8fd4\u56de\u4f1a\u5f71\u54cd\u6027\u80fd\uff08\u5982\u679c\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u5c5e\u4e8e\u62f7\u8d1d\u5f00\u9500\u6bd4\u8f83\u5927\u7684\u90a3\u79cd\uff0c\u4f8b\u5982 std::string \uff09\uff0c\u5728\u7f16\u8bd1\u65f6\u5176\u5b9e\u4e0d\u5b58\u5728\u8be5\u95ee\u9898\uff0c\u56e0\u4e3a\u5168\u90e8\u8c03\u7528 consteval \u51fd\u6570\u7684\u5730\u65b9\u90fd\u4f1a\u88ab\u66ff\u6362\u4e3a\u8ba1\u7b97\u540e\u7684\u503c\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad \u4e2d\u4ecb\u7ecd auto \u7c7b\u578b\u7684\u8fd4\u56de\u503c\uff1b \u6211\u4eec\u4f1a\u5728 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f \u4e2d\u4ecb\u7ecd\u7b80\u5199\u51fd\u6570\u6a21\u677f\uff08 auto \u5f62\u53c2 \uff09","title":"6.14 - Constexpr \u548c consteval \u51fd\u6570"},{"location":"6-14-Constexpr-and-consteval-functions/#constexpr","text":"constexpr \u51fd\u6570\u7684\u8fd4\u56de\u503c\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u4e3a\u4e86\u5c06\u51fd\u6570\u5b9a\u4e49\u4e3a constexpr \u7c7b\u578b\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5728\u8fd4\u56de\u503c\u7c7b\u578b\u524d\u6dfb\u52a0 constexpr \u5173\u952e\u5b57\u5373\u53ef\u3002\u8bf7\u89c1\u4e0b\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include constexpr int greater ( int x , int y ) // now a constexpr function { return ( x > y ? x : y ); } int main () { constexpr int x { 5 }; constexpr int y { 6 }; // We'll explain why we use variable g here later in the lesson constexpr int g { greater ( x , y ) }; // will be evaluated at compile-time std :: cout << g << \" is greater!\" ; return 0 ; } \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4ecd\u7136\u662f\u4e00\u6837\u7684\uff0c \u4f46\u662f greater() \u51fd\u6570\u4f1a\u5728\u7f16\u8bd1\u65f6\u8fdb\u884c\u6c42\u503c\uff01 \u4e3a\u4e86\u80fd\u591f\u6709\u8d44\u683c\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u51fd\u6570\u5fc5\u987b\u5177\u6709 constexpr \u7c7b\u578b\u7684\u8fd4\u56de\u503c\uff0c\u540c\u65f6\u4e0d\u80fd\u591f\u8c03\u7528\u4efb\u4f55\u975econstexpr\u51fd\u6570\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u4e5f\u5fc5\u987b\u4f7f\u7528 constexpr \u5b9e\u53c2 (\u4f8b\u5982 constexpr \u53d8\u91cf\u6216\u5b57\u9762\u91cf)\u3002 \u4f5c\u8005\u6ce8 \u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u6587\u7ae0\u4e2d\u4f7f\u7528\u201c\u6709\u8d44\u683c\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u201d\u8fd9\u4e2a\u672f\u8bed\u3002 \u6269\u5c55\u9605\u8bfb \u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u8981\u6c42\u89c1 \u8fd9\u91cc . greater() \u51fd\u6570\u80fd\u591f\u6ee1\u8db3\u4e0a\u8ff0\u8981\u6c42\uff0c\u6240\u4ee5\u5b83\u6709\u8d44\u683c\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528 constexpr \u8fd4\u56de\u7c7b\u578b\u5fc5\u987b\u8fd4\u56de\u4e00\u4e2a\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002","title":"Constexpr \u51fd\u6570\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u6c42\u503c"},{"location":"6-14-Constexpr-and-consteval-functions/#constexpr_1","text":"\u56e0\u4e3a constexpr \u51fd\u6570\u53ef\u80fd\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u5fc5\u987b\u80fd\u591f\u5728\u8c03\u7528\u8be5\u51fd\u6570\u7684\u5730\u65b9\u770b\u5230\u8be5\u51fd\u6570\u7684\u5b8c\u6574\u5b9a\u4e49\u3002 \u8fd9\u4e5f\u610f\u5473\u7740 constexpr \u51fd\u6570\u5982\u679c\u591a\u4e2a\u6587\u4ef6\u4e2d\u88ab\u8c03\u7528\uff0c\u90a3\u4e48\u5b83\u7684\u5b9a\u4e49\u4e5f\u5fc5\u987b\u6bcf\u4e2a\u6587\u4ef6\u4e00\u4efd\u2014\u2014\u8fd9\u5c31\u53ef\u80fd\u4f1a\u8fdd\u53cd \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\uff0c constexpr \u51fd\u6570\u5177\u6709\u9690\u542b\u7684\u5185\u8054\u5c5e\u6027\uff0c\u8fd9\u4f7f\u5f97\u5b83\u4eec\u80fd\u591f\u514d\u4e8e\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u7684\u9650\u5236\u3002 \u56e0\u6b64\uff0c constexpr \u51fd\u6570\u4e5f\u901a\u5e38\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u56e0\u6b64\u5b83\u4eec\u53ef\u4ee5\u88ab #included \u5230\u4efb\u4f55 .cpp \u6587\u4ef6\u4ee5\u63d0\u4f9b\u51fd\u6570\u7684\u5b8c\u6574\u5b9a\u4e49\u3002","title":"Constexpr \u51fd\u6570\u5177\u6709\u9690\u542b\u5185\u8054\u5c5e\u6027"},{"location":"6-14-Constexpr-and-consteval-functions/#constexpr_2","text":"\u5177\u6709 constexpr \u8fd4\u56de\u503c\u7c7b\u578b\u7684\u51fd\u6570\u4e5f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6c42\u503c\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5b83\u4f1a\u8fd4\u56de\u975e constexpr \u7684\u7ed3\u679c\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include constexpr int greater ( int x , int y ) { return ( x > y ? x : y ); } int main () { int x { 5 }; // \u975e constexpr int y { 6 }; // \u975e constexpr std :: cout << greater ( x , y ) << \" is greater!\" ; // will be evaluated at runtime return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u53c2\u6570 x \u548c y \u5e76\u975e constexpr \u7c7b\u578b\uff0c\u6240\u4ee5\u51fd\u6570\u5c31\u4e0d\u80fd\u5728\u7f16\u8bd1\u65f6\u88ab\u6c42\u503c\u3002\u4e0d\u8fc7\uff0c\u51fd\u6570\u8fd8\u662f\u53ef\u4ee5\u5728 \u8fd0\u884c\u65f6 \u6c42\u503c\u7684\uff0c\u53ea\u4e0d\u8fc7\u8fd4\u56de\u7684\u503c\u662f\u975e constexpr \u7c7b\u578b\u7684 int \u3002 \u5173\u952e\u4fe1\u606f \u4f7f\u51fd\u6570\u5177\u6709 constexpr \u8fd4\u56de\u503c\u7c7b\u578b\u5373\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u4e5f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6c42\u503c\uff0c \u56e0\u6b64\u8fd9\u6837\u7684\u51fd\u6570\u53ef\u4ee5\u6ee1\u8db3\u4e24\u79cd\u573a\u666f\u3002\u5982\u679c\u4e0d\u662f\u8fd9\u6837\u7684\u8bdd\uff0c\u4f60\u5c31\u5fc5\u987b\u5b9a\u4e49\u4e24\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\uff08constexpr \u7248\u672c\u548c\u975e constexpr \u7248\u672c\uff09\uff0c\u4f46\u662f\u7531\u4e8e\u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u80fd\u7528\u6765\u533a\u522b\u51fd\u6570\u91cd\u8f7d\uff0c\u6240\u4ee5\u4f60\u5c31\u4e0d\u5f97\u4e0d\u4e3a\u5b83\u8d77\u4e00\u4e2a\u4e0d\u540c\u7684\u540d\u5b57\u3002","title":"Constexpr \u51fd\u6570\u4e5f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6c42\u503c"},{"location":"6-14-Constexpr-and-consteval-functions/#constexpr_3","text":"\u4f60\u53ef\u80fd\u4f1a\u89c9\u5f97\uff0c constexpr \u51fd\u6570\u5982\u679c\u53ef\u80fd\u7684\u8bdd\u603b\u662f\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u8fd9\u6837\u3002 \u6839\u636e C++ \u6807\u51c6\uff0c\u5982\u679c constexpr \u51fd\u6570\u6709\u8d44\u683c\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c \u800c\u4e14\u5b83\u7684\u8fd4\u56de\u503c\u88ab\u7528\u5728\u5e38\u6570\u8868\u8fbe\u5f0f\u4e2d\uff0c\u90a3\u4e48\u5c31 \u5fc5\u987b \u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u5219\u53ef\u4ee5\u81ea\u7531\u9009\u62e9\u662f\u5728\u7f16\u8bd1\u65f6\u8fd8\u662f\u8fd0\u884c\u65f6\u6c42\u503c\u3002 \u8ba9\u6211\u4eec\u901a\u8fc7\u4e0b\u9762\u7684\u4f8b\u5b50\u8fdb\u884c\u66f4\u8fdb\u4e00\u6b65\u7684\u89e3\u91ca\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include constexpr int greater ( int x , int y ) { return ( x > y ? x : y ); } int main () { constexpr int g { greater ( 5 , 6 ) }; // case 1: \u5728\u7f16\u8bd1\u65f6\u6c42\u503c std :: cout << g << \"is greater!\" ; int x { 5 }; // not constexpr std :: cout << greater ( x , 6 ) << \" is greater!\" ; // case 2: \u5728\u8fd0\u884c\u65f6\u6c42\u503c std :: cout << greater ( 5 , 6 ) << \" is greater!\" ; // case 3: \u5728\u7f16\u8bd1\u65f6\u6216\u8005\u8fd0\u884c\u65f6\u6c42\u503c\u90fd\u53ef\u80fd return 0 ; } \u5bf9\u4e8e case 1\uff0c\u6211\u4eec\u901a\u8fc7 constexpr \u53c2\u6570\u8c03\u7528\u4e86 greater() \u51fd\u6570\uff0c\u56e0\u6b64\u5b83\u6709\u8d44\u683c\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u540c\u65f6\uff0c constexpr \u53d8\u91cf g \u7684\u521d\u59cb\u5316\u503c\u5fc5\u987b\u662f constexpr \uff0c\u6240\u4ee5\u51fd\u6570\u7684\u8fd4\u56de\u503c\u88ab\u7528\u5728\u5e38\u91cf\u8868\u8fbe\u5f0f\u4e2d\u3002\u56e0\u6b64 greater() \u5fc5\u987b\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\u3002 \u5728 case 2 \u4e2d\uff0c\u6211\u4eec\u8c03\u7528 greater() \u65f6\u6240\u7528\u7684\u5176\u4e2d\u4e00\u4e2a\u53c2\u6570\u662f\u975e constexpr \u7c7b\u578b\u7684\u3002\u56e0\u6b64 greater() \u5e76\u4e0d\u80fd\u591f\u5728\u7f16\u8bd1\u65f6\u88ab\u6c42\u503c\uff0c\u53ea\u80fd\u5728\u8fd0\u884c\u65f6\u6c42\u503c\u3002 case 3 \u5c31\u6709\u610f\u601d\u4e86\u3002 greater() \u5728\u8c03\u7528\u65f6\u4f7f\u7528\u4e86 constexpr \u53c2\u6570\uff0c\u6240\u4ee5\u5b83\u662f\u6709\u8d44\u683c\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\u3002\u4f46\u662f\u5b83\u7684\u8fd4\u56de\u503c\u5e76\u6ca1\u6709\u7528\u5728\u9700\u8981\u5e38\u6570\u8868\u8fbe\u5f0f\u7684\u5730\u65b9\uff08 << \u603b\u662f\u5728\u8fd0\u884c\u65f6\u6c42\u503c\uff09\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u53ef\u4ee5\u81ea\u884c\u51b3\u5b9a greater() \u51fd\u6570\u662f\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u51fd\u6570\u8fd0\u884c\u65f6\uff01 \u6ce8\u610f\uff0c\u4f60\u7684\u7f16\u8bd1\u5668\u4f18\u5316\u7b49\u7ea7\u8bbe\u7f6e\u53ef\u80fd\u4f1a\u5f71\u54cd\u5230\u5b83\u5bf9\u51fd\u6570\u5e94\u8be5\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u8fd8\u662f\u8fd0\u884c\u65f6\u6c42\u503c\u7684\u51b3\u5b9a\u3002\u8fd9\u4e5f\u610f\u5473\u7740\u7f16\u8bd1\u5668\u5728 debug \u548c release \u6784\u5efa\u65f6\u53ef\u80fd\u4f1a\u505a\u51fa\u4e0d\u540c\u7684\u51b3\u5b9a\uff08\u56e0\u4e3adebug\u6a21\u5f0f\u4e0b\u901a\u5e38\u4f1a\u5173\u95ed\u4f18\u5316\uff09\u3002 \u5173\u952e\u4fe1\u606f constexpr \u51fd\u6570\u662f\u6709\u8d44\u683c\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\uff0c\u800c\u4e14\u5b83\u53ea\u6709\u5728\u8fd4\u56de\u503c\u88ab\u7528\u5728\u9700\u8981\u5e38\u91cf\u8868\u8fbe\u5f0f\u7684\u5730\u65b9\u65f6\uff0c\u5b83\u624d\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u5176\u4ed6\u60c5\u51b5\u4e0b\u662f\u5426\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u662f\u4e0d\u80fd\u4fdd\u8bc1\u7684\u3002 \u56e0\u6b64\uff0c constexpr \u51fd\u6570\u6700\u597d\u88ab\u770b\u505a\u662f\u201c\u53ef\u4ee5\u88ab\u7528\u5728\u5e38\u91cf\u8868\u8fbe\u5f0f\u201d\u800c\u4e0d\u662f\u201c\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u201d\u3002","title":"\u90a3\u4e48\uff0cconstexpr \u51fd\u6570\u4ec0\u4e48\u65f6\u5019\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff1f"},{"location":"6-14-Constexpr-and-consteval-functions/#constexpr_4","text":"\u5728 C++20 \u4ee5\u524d\uff0c\u5e76\u6ca1\u6709\u6807\u51c6\u7684\u8bed\u8a00\u5de5\u5177\u53ef\u4ee5\u5b8c\u6210\u8be5\u4efb\u52a1\u3002 \u5728 C++20 \u4e2d std::is_constant_evaluated() (\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d)\u4f1a\u8fd4\u56de\u4e00\u4e2a bool \u7c7b\u578b\u7684\u7ed3\u679c\u6307\u793a\u5f53\u524d\u51fd\u6570\u8c03\u7528\u662f\u5426\u7684\u5e38\u91cf\u4e0a\u4e0b\u6587\u4e2d\u6267\u884c\u7684\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u6761\u4ef6\u8bed\u53e5\u6765\u4f7f\u5f97\u51fd\u6570\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u548c\u8fd0\u884c\u65f6\u6c42\u503c\u65f6\u8868\u73b0\u51fa\u4e0d\u540c\u7684\u884c\u4e3a\u3002 1 2 3 4 5 6 7 8 #include // for std::is_constant_evaluated constexpr int someFunction () { if ( std :: is_constant_evaluated ()) // if compile-time evaluation // do something else // runtime evaluation // do something else } \u4f7f\u7528\u5f97\u5f53\u7684\u8bdd\uff0c\u4f60\u53ef\u4ee5\u4f60\u53ef\u4ee5\u8ba9\u51fd\u6570\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u65f6\u4ea7\u751f\u53ef\u4ee5\u88ab\u89c2\u5bdf\u5230\u7684\u4e0d\u540c\u884c\u4e3a\uff08\u4f8b\u5982\u8fd4\u56de\u4e00\u4e2a\u7279\u6b8a\u7684\u503c\uff09\uff0c\u7136\u540e\u4f60\u53ef\u4ee5\u901a\u8fc7\u8be5\u503c\u6765\u5224\u65ad\u51fd\u6570\u662f\u5728\u4f55\u65f6\u6c42\u503c\u7684\uff0c","title":"\u786e\u5b9a constexpr \u51fd\u6570\u8c03\u7528\u662f\u5426\u662f\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684"},{"location":"6-14-Constexpr-and-consteval-functions/#constexpr_5","text":"\u6ca1\u6709\u529e\u6cd5\u53bb\u544a\u8bc9\u7f16\u8bd1\u5668 constexpr \u51fd\u6570\u5e94\u8be5\u5728\u6761\u4ef6\u5141\u8bb8\u7684\u60c5\u51b5\u4e0b\u5c3d\u53ef\u80fd\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff08\u5373\u4f7f\u8fd4\u56de\u503c\u88ab\u7528\u5728\u975e\u5e38\u6570\u8868\u8fbe\u5f0f\u65f6\uff09\u3002 \u4e0d\u8fc7\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8ba9\u51fd\u6570\u8fd4\u56de\u503c\u4f7f\u7528\u5728\u5e38\u6570\u8868\u8fbe\u5f0f\u4e2d\u4ee5\u786e\u4fdd\u5177\u5907\u6761\u4ef6\u7684\u51fd\u6570\u80fd\u591f\u786e\u4fdd\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u5f53\u7136\uff0c\u8fd9\u662f\u9488\u5bf9\u6bcf\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u800c\u8a00\u7684\u3002 \u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u8fd4\u56de\u503c\u6765\u521d\u59cb\u5316\u4e00\u4e2a constexpr \u53d8\u91cf\uff08\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728\u4e4b\u524d\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u4f7f\u7528\u4e86 \u2018g\u2019 \u8fd9\u4e2a\u53d8\u91cf\uff09\u3002\u53ef\u60dc\u7684\u662f\uff0c\u8fd9\u9700\u8981\u4e3a\u7a0b\u5e8f\u5f15\u5165\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\u800c\u4e14\u8fd9\u4e2a\u53d8\u91cf\u53ea\u662f\u4e3a\u4e86\u786e\u4fdd\u51fd\u6570\u662f\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\uff0c\u8fd9\u6837\u975e\u5e38\u4e0d\u4f18\u96c5\u800c\u4e14\u4e5f\u4f1a\u964d\u4f4e\u7a0b\u5e8f\u7684\u53ef\u8bfb\u6027\u3002 \u6269\u5c55\u9605\u8bfb \u4e3a\u4e86\u907f\u514d\u6bcf\u6b21\u90fd\u9700\u8981\u5f15\u5165\u4e00\u4e2a\u65b0\u7684 constexpr \u53d8\u91cf\u624d\u80fd\u786e\u4fdd\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u5f88\u591a\u4eba\u5c1d\u8bd5\u4e86\u4e0d\u5c11\u89e3\u51b3\u529e\u6cd5\u3002\u53c2\u89c1 \u8fd9\u91cc \u548c \u8fd9\u91cc \u3002 \u4e0d\u8fc7\uff0cC++20 \u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u529e\u6cd5\uff0c\u9a6c\u4e0a\u6211\u4eec\u5c31\u80fd\u770b\u5230\u3002","title":"\u5f3a\u5236 constexpr \u51fd\u6570\u5728\u7f16\u8bd1\u65f6\u6c42\u503c"},{"location":"6-14-Constexpr-and-consteval-functions/#consteval-c20","text":"C++20 \u5f15\u5165\u4e86 consteval \u5173\u952e\u5b57\uff0c\u5b83\u53ef\u4ee5\u6307\u5b9a\u51fd\u6570 \u5fc5\u987b \u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u5426\u5219\u5c06\u4f1a\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002\u8fd9\u79cd\u51fd\u6570\u88ab\u79f0\u4e3a \u5373\u65f6\u51fd\u6570(immediate functions) \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include consteval int greater ( int x , int y ) // \u51fd\u6570\u73b0\u5728\u662f consteval \u7c7b\u578b { return ( x > y ? x : y ); } int main () { constexpr int g { greater ( 5 , 6 ) }; // ok: will evaluate at compile-time std :: cout << greater ( 5 , 6 ) << \" is greater!\" ; // ok: will evaluate at compile-time int x { 5 }; // not constexpr std :: cout << greater ( x , 6 ) << \" is greater!\" ; // \u9519\u8bef: consteval \u51fd\u6570\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u6c42\u503c return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u524d\u9762\u4e24\u5904 greater() \u51fd\u6570\u8c03\u7528\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u800c greater(x, 6) \u5e76\u4e0d\u80fd\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u6240\u4ee5\u4f1a\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002 \u548c constexpr \u51fd\u6570\u4e00\u6837\uff0c consteval \u4e5f\u662f\u9690\u5f0f\u5185\u8054\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f60\u6709\u51fd\u6570\u51fa\u4e8e\u67d0\u4e9b\u539f\u56e0\uff08\u4f8b\u5982\u6027\u80fd\uff09\u800c\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 consteval \u3002","title":"Consteval\u00a0(C++20)"},{"location":"6-14-Constexpr-and-consteval-functions/#consteval-constexpr-c20","text":"consteval \u51fd\u6570\u662f\u4e0d\u80fd\u591f\u5728\u8fd0\u884c\u65f6\u6c42\u503c\u7684\uff0c\u8fd9\u4e5f\u4f7f\u5176\u4e0d\u5982 constexpr \u51fd\u6570\u90a3\u6837\u7075\u6d3b\uff08\u65e2\u80fd\u591f\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u4e5f\u80fd\u591f\u5728\u8fd0\u884c\u65f6\u6c42\u503c\uff09\u3002\u6240\u4ee5\uff0c\u5982\u679c\u80fd\u591f\u6709\u529e\u6cd5\u5f3a\u5236 constexpr \u51fd\u6570\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff08\u5373\u4f7f\u5b83\u7684\u8fd4\u56de\u503c\u6ca1\u6709\u88ab\u7528\u5728\u5e38\u6570\u8868\u8fbe\u5f0f\u4e2d\uff09\uff0c\u4e5f\u662f\u975e\u5e38\u6709\u7528\u7684\u3002\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u8ba9\u51fd\u6570\u5c3d\u53ef\u80fd\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u5982\u679c\u51fd\u6570\u4e0d\u5177\u5907 \u8d44\u683c \uff0c\u5219\u5728\u8fd0\u884c\u65f6\u6c42\u503c\u3002 Consteval \u51fd\u6570\u4e3a\u4e0a\u9762\u7684\u60f3\u6cd5\u63d0\u4f9b\u4e86\u5b9e\u73b0\u7684\u53ef\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7b80\u6d01\u7684\u5e2e\u52a9\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include // Uses abbreviated function template (C++20) and `auto` return type to make this function work with any type of value // See 'related content' box below for more info (you don't need to know how these work to use this function) consteval auto compileTime ( auto value ) { return value ; } constexpr int greater ( int x , int y ) // \u51fd\u6570\u662f constexpr \u7c7b\u578b\u7684 { return ( x > y ? x : y ); } int main () { std :: cout << greater ( 5 , 6 ); // \u53ef\u80fd\u4f1a\uff0c\u4e5f\u53ef\u80fd\u4e0d\u4f1a\u5728\u7f16\u8bd1\u65f6\u6267\u884c std :: cout << compileTime ( greater ( 5 , 6 )); // \u4f1a\u5728\u7f16\u8bd1\u65f6\u6267\u884c int x { 5 }; std :: cout << greater ( x , 6 ); // \u4ecd\u7136\u53ef\u4ee5\u8c03\u7528 constexpr \u7248\u672c\u4f7f\u5176\u5728\u8fd0\u884c\u65f6\u6267\u884c return 0 ; } consteval \u51fd\u6570\u9700\u8981\u5e38\u91cf\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u53c2\u6570\u2014\u2014\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u5c06 constexpr \u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5b83\uff0c\u5219 constexpr \u51fd\u6570\u5c31\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002 consteval \u51fd\u6570\u53ea\u662f\u628a\u53c2\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c\u8fd4\u56de\u4e86\uff0c\u56e0\u6b64\u8c03\u7528\u8005\u8fd8\u662f\u53ef\u4ee5\u6b63\u5e38\u4f7f\u7528\u8be5\u503c\u7684\u3002 \u6ce8\u610f\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u51fd\u6570\u662f \u503c\u8fd4\u56de \uff0c\u867d\u7136\u5728\u8fd0\u884c\u65f6\u503c\u8fd4\u56de\u4f1a\u5f71\u54cd\u6027\u80fd\uff08\u5982\u679c\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u5c5e\u4e8e\u62f7\u8d1d\u5f00\u9500\u6bd4\u8f83\u5927\u7684\u90a3\u79cd\uff0c\u4f8b\u5982 std::string \uff09\uff0c\u5728\u7f16\u8bd1\u65f6\u5176\u5b9e\u4e0d\u5b58\u5728\u8be5\u95ee\u9898\uff0c\u56e0\u4e3a\u5168\u90e8\u8c03\u7528 consteval \u51fd\u6570\u7684\u5730\u65b9\u90fd\u4f1a\u88ab\u66ff\u6362\u4e3a\u8ba1\u7b97\u540e\u7684\u503c\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad \u4e2d\u4ecb\u7ecd auto \u7c7b\u578b\u7684\u8fd4\u56de\u503c\uff1b \u6211\u4eec\u4f1a\u5728 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f \u4e2d\u4ecb\u7ecd\u7b80\u5199\u51fd\u6570\u6a21\u677f\uff08 auto \u5f62\u53c2 \uff09","title":"\u4f7f\u7528 consteval \u8ba9 constexpr \u5728\u7f16\u8bd1\u65f6\u6267\u884c(C++20)"},{"location":"6-15-Unnamed-and-inline-namespaces/","tags":["inline namespace","unmanned namespace"],"text":"Key Takeaway \u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u5185\u5bb9\u53ef\u4ee5\u5728\u7236\u547d\u540d\u7a7a\u95f4\u4e2d\u8bbf\u95ee\uff0c\u4f46\u662f\u5b83\u4eec\u5177\u6709\u5185\u90e8\u94fe\u63a5\uff0c\u53ea\u80fd\u5728\u8be5\u6587\u4ef6\u4e2d\u8bbf\u95ee \u5bf9\u4e8e\u51fd\u6570\u6765\u8bf4\uff0c\u628a\u5b83\u4eec\u5b9a\u4e49\u5728\u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6548\u679c\u548c\u628a\u5b83\u4eec\u5b9a\u4e49\u4e3a static \u7c7b\u578b\u7684\u6548\u679c\u662f\u5b8c\u5168\u4e00\u6837\u7684 \u5185\u8054\u547d\u540d\u7a7a\u95f4\u5e38\u7528\u6765\u5bf9\u7279\u5b9a\u7684\u5185\u5bb9\u8fdb\u884c\u7248\u672c\u7ba1\u7406\u3002\u548c\u533f\u540d\u547d\u540d\u7a7a\u95f4\u7c7b\u4f3c\uff0c\u5b9a\u4e49\u5728\u5185\u8054\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u5185\u5bb9\u90fd\u5c5e\u4e8e\u5176\u7236\u547d\u540d\u7a7a\u95f4\u3002\u4f46\u5185\u8054\u547d\u540d\u7a7a\u95f4\u5e76\u4e0d\u4f1a\u7ed9\u5176\u5185\u90e8\u5b9a\u4e49\u7684\u5185\u5bb9\u8d4b\u4e88 \u5185\u90e8\u94fe\u63a5 \u3002 C++ \u652f\u6301\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u53ea\u8981\u6709\u4e24\u79cd\u503c\u5f97\u6211\u4eec\u53bb\u4e86\u89e3\u3002\u672c\u8bfe\u7a0b\u4e0d\u4f1a\u5efa\u7acb\u5728\u8fd9\u4e9b\u57fa\u7840\u4e0a\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u628a\u8fd9\u8282\u8bfe\u5f53\u505a\u9009\u4fee\u3002 \u533f\u540d\u547d\u540d\u7a7a\u95f4 \u672a\u547d\u540d\u547d\u540d\u7a7a\u95f4\uff08\u4e5f\u53eb\u533f\u540d\u547d\u540d\u7a7a\u95f4\uff09\u662f\u4e00\u79cd\u6ca1\u6709\u540d\u5b57\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include namespace // \u533f\u540d\u547d\u540d\u7a7a\u95f4 { void doSomething () // \u53ea\u80fd\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u88ab\u8bbf\u95ee { std :: cout << \"v1 \\n \" ; } } int main () { doSomething (); // \u4e0d\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u524d\u7f00\u8c03\u7528 doSomething() return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 v1 \u6240\u6709\u5728 \u533f\u540d\u547d\u540d\u7a7a\u95f4 \u4e2d\u5b9a\u4e49\u7684\u5185\u5bb9\uff0c\u90fd\u88ab\u770b\u505a\u662f\u8be5\u547d\u540d\u7a7a\u95f4\u7684\u7236\u547d\u540d\u7a7a\u95f4\u7684\u4e00\u90e8\u5206\u3002\u6240\u4ee5\u5373\u4f7f doSomething \u88ab\u5b9a\u4e49\u5728\u4e00\u4e2a\u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u8fd9\u4e2a\u51fd\u6570\u4e5f\u662f\u53ef\u4ee5\u5728\u5b83\u7684\u7236\u547d\u540d\u7a7a\u95f4\uff08\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u662f\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff09\u4e2d\u88ab\u8bbf\u95ee\u7684\uff0c\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728 main \u51fd\u6570\u4e2d\u53ef\u4ee5\u4e0d\u4f7f\u7528\u4efb\u4f55\u9650\u5b9a\u7b26\u5c31\u8bbf\u95ee doSomething \u3002 \u8fd9\u6837\u4e00\u6765\uff0c\u597d\u50cf\u533f\u540d\u547d\u540d\u7a7a\u95f4\u5e76\u6ca1\u6709\u4ec0\u4e48\u7528\u3002\u4f46\u662f\u533f\u540d\u547d\u540d\u7a7a\u95f4\u8fd8\u6709\u4e00\u4e2a\u6548\u679c\uff0c\u5c31\u662f\u5c06\u5176\u5185\u90e8\u7684\u6807\u8bc6\u7b26\u770b\u505a\u662f\u5b83\u7684\u5185\u90e8\u94fe\u63a5\uff0c\u8fd9\u610f\u5473\u7740\u8be5\u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u5185\u5bb9\u4e0d\u80fd\u5728\u8be5\u6587\u4ef6\u4ee5\u5916\u7684\u5730\u65b9\u88ab\u8bbf\u95ee\u3002 \u5bf9\u4e8e\u51fd\u6570\u6765\u8bf4\uff0c\u628a\u5b83\u4eec\u5b9a\u4e49\u5728\u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6548\u679c\u548c\u628a\u5b83\u4eec\u5b9a\u4e49\u4e3a static \u7c7b\u578b\u7684\u6548\u679c\u662f\u5b8c\u5168\u4e00\u6837\u7684\u3002\u4e0b\u9762\u7684\u7a0b\u5e8f\u548c\u4e0a\u9762\u7684\u4f8b\u7a0b\u6548\u679c\u662f\u4e00\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include static void doSomething () // \u53ea\u80fd\u5728\u8be5\u6587\u4ef6\u4e2d\u8bbf\u95ee { std :: cout << \"v1 \\n \" ; } int main () { doSomething (); // \u4e0d\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u524d\u7f00\u8c03\u7528 doSomething() return 0 ; } \u5728\u6709\u5f88\u591a\u5185\u5bb9\u9700\u8981\u88ab\u9650\u5b9a\u5728\u7ed9\u5b9a\u6587\u4ef6\u4e2d\u65f6\uff0c\u533f\u540d\u547d\u540d\u7a7a\u95f4\u5c31\u5f88\u6709\u7528\u3002\u6bd5\u7adf\u548c\u5c06\u5b83\u4eec\u9010\u4e2a\u5b9a\u4e49\u4e3a static \u76f8\u6bd4\uff0c\u4f7f\u7528\u533f\u540d\u547d\u540d\u7a7a\u95f4\u8981\u65b9\u4fbf\u7684\u591a\u3002\u533f\u540d\u547d\u540d\u7a7a\u95f4\u8fd8\u53ef\u4ee5\u628a \u7528\u6237\u5b9a\u4e49\u7c7b\u578b \uff08\u7a0d\u540e\u4f1a\u4ecb\u7ecd\uff09\u9650\u5b9a\u5728\u6587\u4ef6\u4e2d\uff0c\u8fd9\u4e2a\u95ee\u9898\u6ca1\u6709\u5176\u4ed6\u53ef\u7528\u7684\u65b9\u6cd5\u3002 \u5185\u8054\u547d\u540d\u7a7a\u95f4 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void doSomething () { std :: cout << \"v1 \\n \" ; } int main () { doSomething (); return 0 ; } \u6253\u5370\uff1a 1 v1 \u5f88\u7b80\u5355\uff0c\u5bf9\u5427\uff1f \u4f46\u662f\uff0c\u5047\u8bbe\u4f60\u60f3\u8981\u5347\u7ea7\u4e00\u4e0b doSomething \u51fd\u6570\u5e76\u6539\u53d8\u5b83\u7684\u884c\u4e3a\u3002\u4f46\u662f\u8fd9\u6837\u7684\u8bdd\uff0c\u4ecd\u7136\u4f7f\u7528\u8001\u7248\u672c doSomething \u51fd\u6570\u7684\u4ee3\u7801\u5c31\u4f1a\u906d\u5230\u7834\u574f\u3002\u6709\u4ec0\u4e48\u529e\u6cd5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5462\uff1f \u4e00\u79cd\u529e\u6cd5\u662f\u91cd\u65b0\u5b9a\u4e49\u4e00\u4e2a\u540d\u5b57\u4e0d\u540c\u7684\u65b0\u7248\u672c\u7684\u51fd\u6570\u3002\u4f46\u662f\uff0c\u968f\u7740\u4f60\u4e00\u6b21\u6b21\u5730\u4fee\u6539\u51fd\u6570\uff0c\u6700\u7ec8\u7684\u4e0b\u573a\u5c31\u662f\u4f60\u4f1a\u62e5\u6709\u4e00\u5806\u540d\u5b57\u51e0\u4e4e\u76f8\u540c\u7684\u51fd\u6570\uff08 doSomething , doSomething_v2 , doSomething_v3 \u7b49\u7b49\uff09\u3002 \u53e6\u5916\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 \u5185\u8054\u547d\u540d\u7a7a\u95f4 \u3002\u5185\u8054\u547d\u540d\u7a7a\u95f4\u5e38\u7528\u6765\u5bf9\u7279\u5b9a\u7684\u5185\u5bb9\u8fdb\u884c\u7248\u672c\u7ba1\u7406\u3002\u548c\u533f\u540d\u547d\u540d\u7a7a\u95f4\u7c7b\u4f3c\uff0c\u5b9a\u4e49\u5728\u5185\u8054\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u5185\u5bb9\u90fd\u5c5e\u4e8e\u5176\u7236\u547d\u540d\u7a7a\u95f4\u3002\u4e0d\u8fc7\uff0c\u5185\u8054\u547d\u540d\u7a7a\u95f4\u5e76\u4e0d\u4f1a\u7ed9\u5176\u5185\u90e8\u5b9a\u4e49\u7684\u5185\u5bb9\u8d4b\u4e88 \u5185\u90e8\u94fe\u63a5 \u3002 \u5b9a\u4e49\u5185\u8054\u547d\u540d\u7a7a\u95f4\uff0c\u9700\u8981\u4f7f\u7528 inline \u5173\u952e\u5b57\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include inline namespace v1 // \u58f0\u660e\u5185\u8054\u547d\u540d\u7a7a\u95f4 v1 { void doSomething () { std :: cout << \"v1 \\n \" ; } } namespace v2 // \u58f0\u660e\u5185\u8054\u547d\u540d\u7a7a\u95f4 v2 { void doSomething () { std :: cout << \"v2 \\n \" ; } } int main () { v1 :: doSomething (); // calls the v1 version of doSomething() v2 :: doSomething (); // calls the v2 version of doSomething() doSomething (); // calls the inline version of doSomething() (which is v1) return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 v1 v2 v1 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u8c03\u7528 doSomething \u4f1a\u4f7f\u7528 v1 \u7248\u672c\u7684(\u5185\u8054\u7248\u672c)\u7684 doSomething \u3002\u800c\u4efb\u4f55\u60f3\u8981\u4f7f\u7528\u65b0\u7248\u672c\u7684\u5730\u65b9\uff0c\u53ef\u4ee5\u663e\u5f0f\u5730\u8c03\u7528 v2::dosomething() \u3002\u8fd9\u6837\u6211\u4eec\u4e0d\u4ec5\u53ef\u4ee5\u4f7f\u7528\u65b0\u7248\u672c\u7684\u51fd\u6570\uff0c\u540c\u65f6\u4e5f\u80fd\u591f\u4fdd\u5b58\u8001\u7248\u672c\u3002 \u6216\u8005\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u63a8\u5e7f\u65b0\u7248\u672c\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include namespace v1 // \u58f0\u660e\u5185\u8054\u547d\u540d\u7a7a\u95f4 v1 { void doSomething () { std :: cout << \"v1 \\n \" ; } } inline namespace v2 // \u58f0\u660e\u5185\u8054\u547d\u540d\u7a7a\u95f4 v2 { void doSomething () { std :: cout << \"v2 \\n \" ; } } int main () { v1 :: doSomething (); // calls the v1 version of doSomething() v2 :: doSomething (); // calls the v2 version of doSomething() doSomething (); // calls the inline version of doSomething() (which is v2) return 0 ; } \u6253\u5370\uff1a 1 2 3 v1 v2 v2 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u8c03\u7528 doSomething \u7684\u5730\u65b9\u90fd\u4f1a\u4f7f\u7528 v2 \u7248\u672c\u7684\u51fd\u6570\uff08\u66f4\u65b0\u66f4\u597d\u7684\u7248\u672c\uff09\u3002\u800c\u90a3\u4e9b\u613f\u610f\u4f7f\u7528\u8001\u7248\u672c\u51fd\u6570\u7684\u4eba\uff0c\u4e5f\u53ef\u4ee5\u663e\u5f0f\u5730\u8c03\u7528 v1::doSomething() \u6765\u4f7f\u7528\u65e7\u7248\u672c\u7684\u51fd\u6570\u3002\u8fd9\u4e5f\u610f\u5473\u7740\u9700\u8981\u4f7f\u7528 v1::doSomething \u7248\u672c\u7684\u5730\u65b9\u5fc5\u987b\u8981\u5c06 doSomething \u8fdb\u884c\u66ff\u6362\u4e3a v1::doSomething \u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u547d\u540d\u5f97\u5f53\u7684\u8bdd\uff0c\u4e5f\u5e76\u4e0d\u4f1a\u5e26\u6765\u4ec0\u4e48\u5927\u9ebb\u70e6\u3002","title":"6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4"},{"location":"6-15-Unnamed-and-inline-namespaces/#_1","text":"\u672a\u547d\u540d\u547d\u540d\u7a7a\u95f4\uff08\u4e5f\u53eb\u533f\u540d\u547d\u540d\u7a7a\u95f4\uff09\u662f\u4e00\u79cd\u6ca1\u6709\u540d\u5b57\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include namespace // \u533f\u540d\u547d\u540d\u7a7a\u95f4 { void doSomething () // \u53ea\u80fd\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u88ab\u8bbf\u95ee { std :: cout << \"v1 \\n \" ; } } int main () { doSomething (); // \u4e0d\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u524d\u7f00\u8c03\u7528 doSomething() return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 v1 \u6240\u6709\u5728 \u533f\u540d\u547d\u540d\u7a7a\u95f4 \u4e2d\u5b9a\u4e49\u7684\u5185\u5bb9\uff0c\u90fd\u88ab\u770b\u505a\u662f\u8be5\u547d\u540d\u7a7a\u95f4\u7684\u7236\u547d\u540d\u7a7a\u95f4\u7684\u4e00\u90e8\u5206\u3002\u6240\u4ee5\u5373\u4f7f doSomething \u88ab\u5b9a\u4e49\u5728\u4e00\u4e2a\u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u8fd9\u4e2a\u51fd\u6570\u4e5f\u662f\u53ef\u4ee5\u5728\u5b83\u7684\u7236\u547d\u540d\u7a7a\u95f4\uff08\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u662f\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff09\u4e2d\u88ab\u8bbf\u95ee\u7684\uff0c\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728 main \u51fd\u6570\u4e2d\u53ef\u4ee5\u4e0d\u4f7f\u7528\u4efb\u4f55\u9650\u5b9a\u7b26\u5c31\u8bbf\u95ee doSomething \u3002 \u8fd9\u6837\u4e00\u6765\uff0c\u597d\u50cf\u533f\u540d\u547d\u540d\u7a7a\u95f4\u5e76\u6ca1\u6709\u4ec0\u4e48\u7528\u3002\u4f46\u662f\u533f\u540d\u547d\u540d\u7a7a\u95f4\u8fd8\u6709\u4e00\u4e2a\u6548\u679c\uff0c\u5c31\u662f\u5c06\u5176\u5185\u90e8\u7684\u6807\u8bc6\u7b26\u770b\u505a\u662f\u5b83\u7684\u5185\u90e8\u94fe\u63a5\uff0c\u8fd9\u610f\u5473\u7740\u8be5\u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u5185\u5bb9\u4e0d\u80fd\u5728\u8be5\u6587\u4ef6\u4ee5\u5916\u7684\u5730\u65b9\u88ab\u8bbf\u95ee\u3002 \u5bf9\u4e8e\u51fd\u6570\u6765\u8bf4\uff0c\u628a\u5b83\u4eec\u5b9a\u4e49\u5728\u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6548\u679c\u548c\u628a\u5b83\u4eec\u5b9a\u4e49\u4e3a static \u7c7b\u578b\u7684\u6548\u679c\u662f\u5b8c\u5168\u4e00\u6837\u7684\u3002\u4e0b\u9762\u7684\u7a0b\u5e8f\u548c\u4e0a\u9762\u7684\u4f8b\u7a0b\u6548\u679c\u662f\u4e00\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include static void doSomething () // \u53ea\u80fd\u5728\u8be5\u6587\u4ef6\u4e2d\u8bbf\u95ee { std :: cout << \"v1 \\n \" ; } int main () { doSomething (); // \u4e0d\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u524d\u7f00\u8c03\u7528 doSomething() return 0 ; } \u5728\u6709\u5f88\u591a\u5185\u5bb9\u9700\u8981\u88ab\u9650\u5b9a\u5728\u7ed9\u5b9a\u6587\u4ef6\u4e2d\u65f6\uff0c\u533f\u540d\u547d\u540d\u7a7a\u95f4\u5c31\u5f88\u6709\u7528\u3002\u6bd5\u7adf\u548c\u5c06\u5b83\u4eec\u9010\u4e2a\u5b9a\u4e49\u4e3a static \u76f8\u6bd4\uff0c\u4f7f\u7528\u533f\u540d\u547d\u540d\u7a7a\u95f4\u8981\u65b9\u4fbf\u7684\u591a\u3002\u533f\u540d\u547d\u540d\u7a7a\u95f4\u8fd8\u53ef\u4ee5\u628a \u7528\u6237\u5b9a\u4e49\u7c7b\u578b \uff08\u7a0d\u540e\u4f1a\u4ecb\u7ecd\uff09\u9650\u5b9a\u5728\u6587\u4ef6\u4e2d\uff0c\u8fd9\u4e2a\u95ee\u9898\u6ca1\u6709\u5176\u4ed6\u53ef\u7528\u7684\u65b9\u6cd5\u3002","title":"\u533f\u540d\u547d\u540d\u7a7a\u95f4"},{"location":"6-15-Unnamed-and-inline-namespaces/#_2","text":"\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void doSomething () { std :: cout << \"v1 \\n \" ; } int main () { doSomething (); return 0 ; } \u6253\u5370\uff1a 1 v1 \u5f88\u7b80\u5355\uff0c\u5bf9\u5427\uff1f \u4f46\u662f\uff0c\u5047\u8bbe\u4f60\u60f3\u8981\u5347\u7ea7\u4e00\u4e0b doSomething \u51fd\u6570\u5e76\u6539\u53d8\u5b83\u7684\u884c\u4e3a\u3002\u4f46\u662f\u8fd9\u6837\u7684\u8bdd\uff0c\u4ecd\u7136\u4f7f\u7528\u8001\u7248\u672c doSomething \u51fd\u6570\u7684\u4ee3\u7801\u5c31\u4f1a\u906d\u5230\u7834\u574f\u3002\u6709\u4ec0\u4e48\u529e\u6cd5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5462\uff1f \u4e00\u79cd\u529e\u6cd5\u662f\u91cd\u65b0\u5b9a\u4e49\u4e00\u4e2a\u540d\u5b57\u4e0d\u540c\u7684\u65b0\u7248\u672c\u7684\u51fd\u6570\u3002\u4f46\u662f\uff0c\u968f\u7740\u4f60\u4e00\u6b21\u6b21\u5730\u4fee\u6539\u51fd\u6570\uff0c\u6700\u7ec8\u7684\u4e0b\u573a\u5c31\u662f\u4f60\u4f1a\u62e5\u6709\u4e00\u5806\u540d\u5b57\u51e0\u4e4e\u76f8\u540c\u7684\u51fd\u6570\uff08 doSomething , doSomething_v2 , doSomething_v3 \u7b49\u7b49\uff09\u3002 \u53e6\u5916\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 \u5185\u8054\u547d\u540d\u7a7a\u95f4 \u3002\u5185\u8054\u547d\u540d\u7a7a\u95f4\u5e38\u7528\u6765\u5bf9\u7279\u5b9a\u7684\u5185\u5bb9\u8fdb\u884c\u7248\u672c\u7ba1\u7406\u3002\u548c\u533f\u540d\u547d\u540d\u7a7a\u95f4\u7c7b\u4f3c\uff0c\u5b9a\u4e49\u5728\u5185\u8054\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u5185\u5bb9\u90fd\u5c5e\u4e8e\u5176\u7236\u547d\u540d\u7a7a\u95f4\u3002\u4e0d\u8fc7\uff0c\u5185\u8054\u547d\u540d\u7a7a\u95f4\u5e76\u4e0d\u4f1a\u7ed9\u5176\u5185\u90e8\u5b9a\u4e49\u7684\u5185\u5bb9\u8d4b\u4e88 \u5185\u90e8\u94fe\u63a5 \u3002 \u5b9a\u4e49\u5185\u8054\u547d\u540d\u7a7a\u95f4\uff0c\u9700\u8981\u4f7f\u7528 inline \u5173\u952e\u5b57\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include inline namespace v1 // \u58f0\u660e\u5185\u8054\u547d\u540d\u7a7a\u95f4 v1 { void doSomething () { std :: cout << \"v1 \\n \" ; } } namespace v2 // \u58f0\u660e\u5185\u8054\u547d\u540d\u7a7a\u95f4 v2 { void doSomething () { std :: cout << \"v2 \\n \" ; } } int main () { v1 :: doSomething (); // calls the v1 version of doSomething() v2 :: doSomething (); // calls the v2 version of doSomething() doSomething (); // calls the inline version of doSomething() (which is v1) return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 v1 v2 v1 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u8c03\u7528 doSomething \u4f1a\u4f7f\u7528 v1 \u7248\u672c\u7684(\u5185\u8054\u7248\u672c)\u7684 doSomething \u3002\u800c\u4efb\u4f55\u60f3\u8981\u4f7f\u7528\u65b0\u7248\u672c\u7684\u5730\u65b9\uff0c\u53ef\u4ee5\u663e\u5f0f\u5730\u8c03\u7528 v2::dosomething() \u3002\u8fd9\u6837\u6211\u4eec\u4e0d\u4ec5\u53ef\u4ee5\u4f7f\u7528\u65b0\u7248\u672c\u7684\u51fd\u6570\uff0c\u540c\u65f6\u4e5f\u80fd\u591f\u4fdd\u5b58\u8001\u7248\u672c\u3002 \u6216\u8005\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u63a8\u5e7f\u65b0\u7248\u672c\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include namespace v1 // \u58f0\u660e\u5185\u8054\u547d\u540d\u7a7a\u95f4 v1 { void doSomething () { std :: cout << \"v1 \\n \" ; } } inline namespace v2 // \u58f0\u660e\u5185\u8054\u547d\u540d\u7a7a\u95f4 v2 { void doSomething () { std :: cout << \"v2 \\n \" ; } } int main () { v1 :: doSomething (); // calls the v1 version of doSomething() v2 :: doSomething (); // calls the v2 version of doSomething() doSomething (); // calls the inline version of doSomething() (which is v2) return 0 ; } \u6253\u5370\uff1a 1 2 3 v1 v2 v2 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u8c03\u7528 doSomething \u7684\u5730\u65b9\u90fd\u4f1a\u4f7f\u7528 v2 \u7248\u672c\u7684\u51fd\u6570\uff08\u66f4\u65b0\u66f4\u597d\u7684\u7248\u672c\uff09\u3002\u800c\u90a3\u4e9b\u613f\u610f\u4f7f\u7528\u8001\u7248\u672c\u51fd\u6570\u7684\u4eba\uff0c\u4e5f\u53ef\u4ee5\u663e\u5f0f\u5730\u8c03\u7528 v1::doSomething() \u6765\u4f7f\u7528\u65e7\u7248\u672c\u7684\u51fd\u6570\u3002\u8fd9\u4e5f\u610f\u5473\u7740\u9700\u8981\u4f7f\u7528 v1::doSomething \u7248\u672c\u7684\u5730\u65b9\u5fc5\u987b\u8981\u5c06 doSomething \u8fdb\u884c\u66ff\u6362\u4e3a v1::doSomething \u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u547d\u540d\u5f97\u5f53\u7684\u8bdd\uff0c\u4e5f\u5e76\u4e0d\u4f1a\u5e26\u6765\u4ec0\u4e48\u5927\u9ebb\u70e6\u3002","title":"\u5185\u8054\u547d\u540d\u7a7a\u95f4"},{"location":"6-2-User-defined-namespaces-and-the-scope-resolution-operator/","tags":["namespace","namespace aliases","scope resolution operator","C++17"],"text":"Key Takeaway \u4e0d\u6307\u5b9a\u547d\u540d\u7a7a\u95f4\u7684\u524d\u63d0\u4e0b\uff0c\u7f16\u8bd1\u5668\u9996\u5148\u5728\u5f53\u524d\u547d\u540d\u7a7a\u95f4\u4e2d\u67e5\u627e\uff0c\u7136\u540e\u6309\u7167\u5305\u542b\u5173\u7cfb\u9010\u5c42\u67e5\u627e\uff0c\u76f4\u5230\u627e\u5230\u5168\u5c40\u547d\u540d\u7a7a\u95f4 \u4e0d\u5e26\u524d\u7f00\u7684\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u8868\u793a\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u67e5\u627e\uff08 ::print() \uff09 \u5c06\u547d\u540d\u7a7a\u95f4\u7684\u58f0\u660e\u5206\u6210\u591a\u4e2a\u90e8\u5206\u653e\u5728\u4e0d\u540c\u7684\u4f4d\u7f6e\u662f\u5408\u6cd5\u7684\uff08\u591a\u4e2a\u6587\u4ef6\u3001\u76f8\u540c\u6587\u4ef6\u7684\u4e0d\u540c\u4f4d\u7f6e\uff09\u3002\u6240\u6709\u8fd9\u4e9b\u58f0\u660e\u90fd\u88ab\u8ba4\u4e3a\u662f\u5bf9\u5e94\u547d\u540d\u7a7a\u95f4\u58f0\u660e\u7684\u4e00\u90e8\u5206\u3002 \u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u5d4c\u5957\uff0c\u4f46\u662f\u5d4c\u5957\u540e\u5f15\u7528\u5185\u90e8\u7684\u51fd\u6570\u5199\u8d77\u6765\u5f88\u9ebb\u70e6\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u522b\u540d\u4e3a\u5176\u521b\u5efa\u4e00\u4e2a\u7b80\u77ed\u7684\u3001\u4e34\u65f6\u7684\u540d\u5b57 C++17 \u8fd8\u652f\u6301 namespace foo::goo \u8fd9\u79cd\u65b9\u5f0f\u6765\u521b\u5efa\u5d4c\u5957\u7684\u547d\u540d\u7a7a\u95f4 \u547d\u540d\u7a7a\u95f4\u522b\u540d\u6700\u597d\u7684\u4e00\u70b9\u662f\uff1a\u5982\u679c\u4f60\u9700\u8981\u5c06 foo::goo \u4e2d\u7684\u51fd\u6570\u79fb\u52a8\u5230\u53e6\u5916\u7684\u5730\u65b9\uff0c\u90a3\u4e48\u4f60\u53ea\u9700\u8981\u66f4\u65b0\u4e00\u4e0b\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684 active \u522b\u540d\u4f7f\u5176\u6307\u5411\u65b0\u7684\u547d\u540d\u7a7a\u95f4\u5373\u53ef\uff0c\u800c\u4e0d\u5fc5\u67e5\u627e\u66ff\u6362\u6240\u6709\u7684 foo::goo . C++ \u7684\u547d\u540d\u7a7a\u95f4\u4e0d\u662f\u4e3a\u4e86\u50cf C# \u6216\u8005 Java \u90a3\u6837\u521b\u5efa\u4e00\u4e2a\u5c42\u6b21\u7ed3\u6784\uff0c\u5b83\u53ea\u662f\u4e00\u79cd\u907f\u514d\u547d\u540d\u51b2\u7a81\u7684\u673a\u5236\uff0c\u56e0\u6b64\u4e0d\u8981\u8fc7\u5ea6\u5d4c\u5957\u3002 \u81ea\u5df1\u7f16\u5199\u7684\u5e93\uff0c\u5982\u679c\u63d0\u4f9b\u7ed9\u4ed6\u4eba\u4f7f\u7528\uff0c\u8bf7\u653e\u5728\u81ea\u5b9a\u4e49\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u547d\u540d\u51b2\u7a81\uff0c\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u7f16\u8f91\u5668\u7684\u4ee3\u7801\u8865\u5168\u548c\u63d0\u793a\u8fdb\u884c\u5de5\u4f5c \u5728 2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4\u3002\u63d0\u9192\u4e00\u4e0b\uff0c\u547d\u540d\u51b2\u7a81\u53d1\u751f\u5728\u76f8\u540c\u4f5c\u7528\u57df\u4e2d\u6709\u4e24\u4e2a\u76f8\u540c\u540d\u5b57\u7684\u65f6\u5019\uff0c\u8fd9\u65f6\u7f16\u8bd1\u5668\u5c06\u4e0d\u80fd\u591f\u533a\u5206\u5b83\u4eec\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u6216\u8005\u94fe\u63a5\u5668\u4f1a\u62a5\u544a\u9519\u8bef\u4fe1\u606f\u3002\u5f53\u7a0b\u5e8f\u8d8a\u53d8\u8d8a\u5927\u7684\u65f6\u5019\uff0c\u6807\u8bc6\u7b26\u7684\u6570\u91cf\u4e5f\u4f1a\u7ebf\u6027\u589e\u957f\uff0c\u8fd9\u4f1a\u4f7f\u5f97\u547d\u540d\u51b2\u7a81\u7684\u53ef\u80fd\u6027\u6210\u6307\u6570\u500d\u589e\u957f\u3002 \u8ba9\u6211\u4eec\u518d\u56de\u987e\u4e00\u4e0b\u547d\u540d\u51b2\u7a81\u7684\u4f8b\u5b50\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c foo.cpp \u548c goo.cpp \u4e2d\u5305\u542b\u4e86\u529f\u80fd\u4e0d\u540c\uff0c\u4f46\u51fd\u6570\u540d\u5b8c\u5168\u548c\u53c2\u6570\u5b8c\u5168\u76f8\u540c\u7684\u4e24\u4e2a\u51fd\u6570\u3002 foo.cpp 1 2 3 4 5 // This doSomething() adds the value of its parameters int doSomething ( int x , int y ) { return x + y ; } goo.cpp 1 2 3 4 5 // This doSomething() subtracts the value of its parameters int doSomething ( int x , int y ) { return x - y ; } main.cpp 1 2 3 4 5 6 7 8 9 #include int doSomething ( int x , int y ); // forward declaration for doSomething int main () { std :: cout << doSomething ( 4 , 3 ) << '\\n' ; // which doSomething will we get? return 0 ; } \u5982\u679c\u7a0b\u5e8f\u4e2d\u53ea\u5305\u542b foo.cpp \u6216\u8005 goo.cpp (\u4f46\u4e0d\u662f\u4e24\u4e2a\u90fd\u6709)\uff0c\u7a0b\u5e8f\u5c31\u53ef\u4ee5\u987a\u5229\u5730\u7f16\u8bd1\u548c\u8fd0\u884c\u3002\u4e0d\u8fc7\uff0c\u5f53\u628a\u5b83\u4eec\u4e24\u4e2a\u90fd\u7f16\u8bd1\u5230\u540c\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\u65f6\uff0c\u6211\u4eec\u5c31\u5411\u540c\u4e00\u4e2a\u4f5c\u7528\u57df\uff08\u5168\u5c40\u4f5c\u7528\u57df\uff09\u4e2d\u5f15\u5165\u4e86\u4e24\u4e2a\u540d\u5b57\u548c\u53c2\u6570\u90fd\u5b8c\u5168\u76f8\u540c\u7684\u51fd\u6570\uff0c\u8fd9\u5c31\u4f1a\u4ee3\u7801\u547d\u540d\u51b2\u7a81\uff0c\u9020\u6210\u7684\u540e\u679c\u5c31\u662f\u94fe\u63a5\u5668\u62a5\u9519\uff1a 1 goo.cpp:3: multiple definition of `doSomething(int, int)'; foo.cpp:3: first defined here \u6ce8\u610f\uff0c\u8fd9\u4e2a\u95ee\u9898\u5728\u4e8e \u91cd\u590d\u5b9a\u4e49 \uff0c\u6240\u4ee5\u65e0\u5173\u662f\u5426 doSomething \u88ab\u8c03\u7528\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u662f\u5bf9\u5176\u4e2d\u4e00\u4e2a\u51fd\u6570\u91cd\u547d\u540d\uff0c\u8fd9\u6837\u547d\u540d\u5c31\u4e0d\u4f1a\u51b2\u7a81\u4e86\u3002\u4f46\u662f\uff0c\u8fd9\u4e48\u505a\u5c31\u610f\u5473\u7740\u6240\u6709\u8c03\u7528\u8be5\u51fd\u6570\u7684\u5730\u65b9\u7684\u4ee3\u7801\u4e5f\u8981\u5bf9\u5e94\u5730\u4fee\u6539\uff0c\u8fd9\u4e0d\u4ec5\u662f\u4e00\u9879\u5927\u5de5\u7a0b\uff0c\u4e5f\u975e\u5e38\u5bb9\u6613\u72af\u9519\u3002\u53e6\u4e00\u4e2a\u907f\u514d\u547d\u540d\u51b2\u7a81\u7684\u529e\u6cd5\uff0c\u662f\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u547d\u540d\u7a7a\u95f4\u3002\u4e5f\u6b63\u662f\u8fd9\u6837\uff0c\u6807\u51c6\u5e93\u7684\u51fd\u6570\u90fd\u88ab\u79fb\u52a8\u5230\u4e86 std \u547d\u540d\u7a7a\u95f4\u4e2d\u3002 \u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4 C++ \u5141\u8bb8\u6211\u4eec\u901a\u8fc7 namespace \u5173\u952e\u5b57\u521b\u5efa\u4f60\u81ea\u5df1\u7684\u547d\u540d\u7a7a\u95f4\u3002\u7528\u6237\u4e3a\u4e86\u81ea\u5df1\u7684\u58f0\u660e\u521b\u5efa\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u79f0\u4e3a \u7528\u6237\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4 \u3002C++ \u63d0\u4f9b\u7684\u547d\u540d\u7a7a\u95f4\uff08\u4f8b\u5982 global \u548c std \uff09\u5e76\u6ca1\u6709\u8003\u8651\u7528\u6237\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u3002 \u547d\u540d\u7a7a\u95f4\u7684\u6807\u8bc6\u7b26\u901a\u5e38\u4e0d\u662f\u5927\u5199\u5f62\u5f0f \u6211\u4eec\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u91cd\u65b0\u7f16\u5199\u4e86\u4e0a\u9762\u7684\u51e0\u4e2a\u7a0b\u5e8f\uff1a foo.cpp 1 2 3 4 5 6 7 8 namespace foo // define a namespace named foo { // This doSomething() belongs to namespace foo int doSomething ( int x , int y ) { return x + y ; } } goo.cpp 1 2 3 4 5 6 7 8 namespace goo // define a namespace named goo { // This doSomething() belongs to namespace goo int doSomething ( int x , int y ) { return x - y ; } } \u73b0\u5728 foo.cpp \u4e2d\u7684 doSomething() \u4f4d\u4e8e foo \u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u800c goo.cpp \u4e2d\u7684 doSomething() \u5219\u4f4d\u4e8e goo \u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u91cd\u65b0\u7f16\u8bd1\u7a0b\u5e8f\uff0c\u770b\u770b\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002 main.cpp 1 2 3 4 5 6 7 int doSomething ( int x , int y ); // forward declaration for doSomething int main () { std :: cout << doSomething ( 4 , 3 ) << '\\n' ; // which doSomething will we get? return 0 ; } \u7ed3\u679c\u5c31\u662f\u6211\u4eec\u5f97\u5230\u4e86\u53e6\u5916\u7684\u9519\u8bef\u4fe1\u606f\uff01 1 ConsoleApplication1.obj : error LNK2019: unresolved external symbol \"int __cdecl doSomething(int,int)\" (?doSomething@@YAHHH@Z) referenced in function _main \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7f16\u8bd1\u6ca1\u6709\u95ee\u9898(\u56e0\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86 \u524d\u5411\u58f0\u660e )\uff0c\u4f46\u662f\u94fe\u63a5\u5668\u5e76\u4e0d\u80fd\u5728 global \u4f5c\u7528\u57df\u4e2d\u627e\u5230 doSomething \u7684\u5b9a\u4e49\u3002\u8fd9\u662f\u56e0\u4e3a\u8fd9\u4e24\u4e2a\u7248\u672c\u7684 doSomething \u90fd\u5df2\u7ecf\u4e0d\u5728 global \u4f5c\u7528\u57df\u4e2d\u4e86\u3002 \u544a\u8bc9\u7f16\u8bd1\u5668\u4f7f\u7528\u54ea\u4e2a\u7248\u672c doSomething() \u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff0c\u4e00\u79cd\u662f \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \uff0c\u4e00\u79cd\u662fusing\u8bed\u53e5\uff08\u53c2\u89c1: 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 \uff09\u3002 \u5728\u63a5\u4e0b\u6765\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8fd8\u662f\u56de\u5230\u5355\u6587\u4ef6\u7684\u573a\u666f\uff0c\u8fd9\u6837\u770b\u8d77\u6765\u66f4\u52a0\u6e05\u6670\u3002 \u901a\u8fc7\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26( :: )\u8bbf\u95ee\u547d\u540d\u7a7a\u95f4 \u544a\u8bc9\u7f16\u8bd1\u5668\u5728\u54ea\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u67e5\u627e\u51fd\u6570\u7684\u6700\u597d\u7684\u529e\u6cd5\u5c31\u662f\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26( :: )\u3002\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u544a\u8bc9\u7f16\u8bd1\u5668\u8be5\u8fd0\u7b97\u7b26\u5de6\u9762\u7684\u64cd\u4f5c\u6570\u662f\u5b83\u5e94\u8be5\u67e5\u627e\u7684\u547d\u540d\u7a7a\u95f4\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u67e5\u627e\u7f16\u8bd1\u5668\u67e5\u627e foo \u547d\u540d\u7a7a\u95f4\u7684 doSomething() \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include namespace foo // define a namespace named foo { // This doSomething() belongs to namespace foo int doSomething ( int x , int y ) { return x + y ; } } namespace goo // define a namespace named goo { // This doSomething() belongs to namespace goo int doSomething ( int x , int y ) { return x - y ; } } int main () { std :: cout << foo :: doSomething ( 4 , 3 ) << '\\n' ; // use the doSomething() that exists in namespace foo return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 7 \u5982\u679c\u4f60\u5e0c\u671b\u4f7f\u7528 goo \u4e2d\u7684 doSomething() \uff0c\u5219\u5e94\u8be5\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include namespace foo // define a namespace named foo { // This doSomething() belongs to namespace foo int doSomething ( int x , int y ) { return x + y ; } } namespace goo // define a namespace named goo { // This doSomething() belongs to namespace goo int doSomething ( int x , int y ) { return x - y ; } } int main () { std :: cout << goo :: doSomething ( 4 , 3 ) << '\\n' ; // use the doSomething() that exists in namespace goo return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 1 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u975e\u5e38\u597d\u7528\uff0c\u5b83\u5141\u8bb8\u6211\u4eec \u663e\u5f0f\u5730 \u6307\u5b9a\u4e00\u4e2a\u9700\u8981\u67e5\u627e\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u4e0d\u5b58\u5728\u4efb\u4f55\u6a21\u7cca\u7684\u542b\u4e49\u3002\u6211\u4eec\u751a\u81f3\u53ef\u4ee5\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include namespace foo // define a namespace named foo { // This doSomething() belongs to namespace foo int doSomething ( int x , int y ) { return x + y ; } } namespace goo // define a namespace named goo { // This doSomething() belongs to namespace goo int doSomething ( int x , int y ) { return x - y ; } } int main () { std :: cout << foo :: doSomething ( 4 , 3 ) << '\\n' ; // use the doSomething() that exists in namespace foo std :: cout << goo :: doSomething ( 4 , 3 ) << '\\n' ; // use the doSomething() that exists in namespace goo return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 7 1 \u4f7f\u7528\u6ca1\u6709\u524d\u7f00\u7684\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u4e5f\u53ef\u4ee5\u7528\u5728\u4e00\u4e2a\u6807\u8bc6\u7b26\u524d\u9762\uff0c\u4f46\u4e0d\u6307\u5b9a\u547d\u540d\u7a7a\u95f4\uff08\u4f8b\u5982 ::doSomething \uff09\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u4f1a\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u67e5\u627e\u6807\u8bc6\u7b26\uff08 doSomething \uff09 \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include void print () // this print lives in the global namespace { std :: cout << \" there \\n \" ; } namespace foo { void print () // this print lives in the foo namespace { std :: cout << \"Hello\" ; } } int main () { foo :: print (); // call print() in foo namespace :: print (); // call print() in global namespace (same as just calling print() in this case) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c ::print() \u7684\u6548\u679c\u548c print() \uff08\u4e0d\u8fdb\u884c\u4f5c\u7528\u57df\u89e3\u6790\uff09\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u6240\u4ee5\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u662f\u591a\u4f59\u7684\u3002\u4f46\u662f\uff0c\u5728\u63a5\u4e0b\u6765\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u80fd\u591f\u770b\u5230\u8fd9\u79cd\u65e0\u524d\u7f00\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u7684\u4f5c\u7528\u3002 \u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u89e3\u6790 \u5f53\u4e00\u4e2a\u6807\u8bc6\u7b26\u4f4d\u4e8e\u67d0\u4e2a\u547d\u540d\u7a7a\u95f4\u65f6\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\u4f5c\u7528\u57df\u89e3\u6790\uff0c\u5219\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u5728\u76f8\u540c\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\u8fdb\u884c\u67e5\u627e\u3002\u5982\u679c\u6ca1\u6709\u627e\u5230\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u5728\u6309\u7167\u5305\u542b\u5173\u7cfb\uff0c\u5728\u547d\u540d\u7a7a\u95f4\u5e8f\u5217\u4e2d\u4f9d\u6b21\u67e5\u627e\uff0c\u6700\u540e\u624d\u4f1a\u67e5\u627e global \u547d\u540d\u7a7a\u95f4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include void print () // this print lives in the global namespace { std :: cout << \" there \\n \" ; } namespace foo { void print () // this print lives in the foo namespace { std :: cout << \"Hello\" ; } void printHelloThere () { print (); // calls print() in foo namespace :: print (); // calls print() in global namespace } } int main () { foo :: printHelloThere (); return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 Hello there \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u8c03\u7528 print() \u51fd\u6570\u65f6\u5e76\u6ca1\u6709\u6307\u5b9a\u9700\u8981\u89e3\u6790\u7684\u4f5c\u7528\u57df\u3002\u56e0\u6b64\u5728 foo \u547d\u540d\u7a7a\u95f4\u4e2d\u8c03\u7528 print() \u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u67e5\u627e foo::print() \u7684\u5b9a\u4e49\u3002\u56e0\u4e3a\u8be5\u5b9a\u4e49\u5b58\u5728\uff0c\u6240\u4ee5 foo::print() \u88ab\u6267\u884c\u3002 \u5982\u679c foo::print() \u5e76\u6ca1\u6709\u88ab\u627e\u5230\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c31\u5fc5\u987b\u67e5\u627e\u5305\u542b\u8be5\u4f5c\u7528\u57df\u7684\u4f5c\u7528\u57df\uff08\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u662f global \u4f5c\u7528\u57df\uff09\u4ee5\u5c1d\u8bd5\u627e\u5230 print() \u3002 \u6ce8\u610f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0d\u5e26\u524d\u7f00\u7684\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\uff08 ::print() \uff09\u660e\u786e\u6307\u5b9a\u7f16\u8bd1\u5668\u4f7f\u7528 global \u547d\u540d\u7a7a\u95f4\u4e2d\u7684 print() \u3002 \u547d\u540d\u7a7a\u95f4\u5b9a\u4e49\u4e3a\u591a\u4e2a\u5757 \u5c06\u547d\u540d\u7a7a\u95f4\u7684\u58f0\u660e\u5206\u6210\u591a\u4e2a\u90e8\u5206\u653e\u5728\u4e0d\u540c\u7684\u4f4d\u7f6e\u662f\u5408\u6cd5\u7684\uff08\u591a\u4e2a\u6587\u4ef6\u3001\u76f8\u540c\u6587\u4ef6\u7684\u4e0d\u540c\u4f4d\u7f6e\uff09\u3002\u6240\u6709\u8fd9\u4e9b\u58f0\u660e\u90fd\u88ab\u8ba4\u4e3a\u662f\u5bf9\u5e94\u547d\u540d\u7a7a\u95f4\u58f0\u660e\u7684\u4e00\u90e8\u5206\u3002 circle.h 1 2 3 4 5 6 7 8 9 #ifndef CIRCLE_H #define CIRCLE_H namespace basicMath { constexpr double pi { 3.14 }; } #endif growth.h 1 2 3 4 5 6 7 8 9 10 #ifndef GROWTH_H #define GROWTH_H namespace basicMath { // the constant e is also part of namespace basicMath constexpr double e { 2.7 }; } #endif main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 #include \"circle.h\" // for basicMath::pi #include \"growth.h\" // for basicMath::e #include int main () { std :: cout << basicMath :: pi << '\\n' ; std :: cout << basicMath :: e << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u6211\u4eec\u6240\u613f\uff1a 1 2 3.14 2.7 \u6807\u51c6\u5e93\u5927\u91cf\u4f7f\u7528\u4e86\u8fd9\u4e00\u7279\u6027\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u6807\u51c6\u5e93\u7684\u5934\u6587\u4ef6\u90fd\u5c06\u5b83\u58f0\u660e\u7684\u5185\u5bb9\u653e\u5728 namespace std \u5757\u4e2d\u3002\u5426\u5219\uff0c\u5168\u90e8\u7684\u6807\u51c6\u5e93\u5c06\u5fc5\u987b\u5b9a\u4e49\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u6587\u4ef6\u4e2d\u4e86\u3002 \u6ce8\u610f\uff0c\u501f\u52a9\u8fd9\u4e2a\u529f\u80fd\uff0c\u4f60\u5b9e\u9645\u4e0a\u53ef\u4ee5\u628a\u81ea\u5b9a\u4e49\u7684\u51fd\u6570\u4e5f\u653e\u5230 std \u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u4e0d\u8fc7\u8fd9\u4e48\u505a\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u7684\u53d1\u751f\uff0c\u56e0\u4e3a std \u547d\u540d\u7a7a\u95f4\u5177\u6709\u7279\u6b8a\u7684\u89c4\u5219\u4ee5\u4fbf\u7981\u6b62\u7528\u6237\u53bb\u6269\u5c55\u6807\u51c6\u5e93\u7684\u4ee3\u7801\u3002 \u6ce8\u610f \u4e0d\u8981\u628a\u81ea\u5b9a\u4e49\u7684\u529f\u80fd\u5b9a\u4e49\u5728 std \u547d\u540d\u7a7a\u95f4\u4e2d\u3002 \u5982\u679c\u4f60\u9700\u8981\u628a\u4ee3\u7801\u5206\u6563\u5230\u591a\u4e2a\u6587\u4ef6\u65f6\uff0c\u4f60\u5fc5\u987b\u5728\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u4e2d\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\uff1a add.h 1 2 3 4 5 6 7 8 9 10 #ifndef ADD_H #define ADD_H namespace basicMath { // function add() is part of namespace basicMath int add ( int x , int y ); } #endif add.cpp 1 2 3 4 5 6 7 8 9 10 #include \"add.h\" namespace basicMath { // define the function add() int add ( int x , int y ) { return x + y ; } } main.cpp 1 2 3 4 5 6 7 8 9 10 #include \"add.h\" // for basicMath::add() #include int main () { std :: cout << basicMath :: add ( 4 , 3 ) << '\\n' ; return 0 ; } \u5982\u679c\u4f60\u5728\u6e90\u6587\u4ef6\u4e2d\u7701\u7565\u4e86\u547d\u540d\u7a7a\u95f4\uff0c\u7f16\u8bd1\u5668\u5c06\u65e0\u6cd5\u627e\u5230 basicMath::add \uff0c\u56e0\u4e3a\u6e90\u6587\u4ef6\u53ea\u5b9a\u4e49\u4e86 add (\u5168\u5c40\u547d\u540d\u7a7a\u95f4)\u3002\u5982\u679c\u8be5\u547d\u540d\u7a7a\u95f4\u5728\u5934\u6587\u4ef6\u4e2d\u88ab\u7701\u7565\uff0c\u5219\u201cmain.cpp\u201d \u5c31\u65e0\u6cd5\u4f7f\u7528 basicMath::add \uff0c\u56e0\u4e3a\u5b83\u53ea\u80fd\u770b\u5230 add \u7684\u58f0\u660e (\u5168\u5c40\u547d\u540d\u7a7a\u95f4)\u3002 \u547d\u540d\u7a7a\u95f4\u5d4c\u5957 \u547d\u540d\u7a7a\u95f4\u4e5f\u53ef\u4ee5\u5d4c\u5957\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include namespace foo { namespace goo // goo is a namespace inside the foo namespace { int add ( int x , int y ) { return x + y ; } } } int main () { std :: cout << foo :: goo :: add ( 1 , 2 ) << '\\n' ; return 0 ; } \u6ce8\u610f\uff0c\u56e0\u4e3a\u547d\u540d\u7a7a\u95f4 goo \u5b9a\u4e49\u5728\u547d\u540d\u7a7a\u95f4 foo \u4e2d\uff0c\u6240\u4ee5\u8981\u8bbf\u95ee add \u5fc5\u987b\u50cf\u8fd9\u6837 foo::goo::add \u3002 \u4ece C++17\u5f00\u59cb\uff0c\u5d4c\u5957\u547d\u540d\u7a7a\u95f4\u4e5f\u53ef\u4ee5\u8fd9\u6837\u58f0\u660e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include namespace foo :: goo // goo is a namespace inside the foo namespace (C++17 style) { int add ( int x , int y ) { return x + y ; } } int main () { std :: cout << foo :: goo :: add ( 1 , 2 ) << '\\n' ; return 0 ; } \u547d\u540d\u7a7a\u95f4\u522b\u540d \u5728C++\u4e2d\u5b8c\u6574\u8f93\u5165\u5d4c\u5957\u547d\u540d\u7a7a\u95f4\u7684\u6807\u8bc6\u7b26\u662f\u975e\u5e38\u75db\u82e6\u7684\u4e00\u4ef6\u4e8b\uff0c\u6240\u4ee5C++\u5141\u8bb8\u4f60\u4e3a\u547d\u540d\u7a7a\u95f4\u521b\u5efa \u522b\u540d \uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u522b\u540d\u4e3a\u4e00\u4e2a\u975e\u5e38\u957f\u7684\u547d\u540d\u7a7a\u95f4\u5e8f\u5217\u521b\u5efa\u4e00\u4e2a\u7b80\u77ed\u7684\u3001\u4e34\u65f6\u7684\u540d\u79f0\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include namespace foo :: goo { int add ( int x , int y ) { return x + y ; } } int main () { namespace active = foo :: goo ; // active now refers to foo::goo std :: cout << active :: add ( 1 , 2 ) << '\\n' ; // This is really foo::goo::add() return 0 ; } // The active alias ends here \u547d\u540d\u7a7a\u95f4\u522b\u540d\u6700\u597d\u7684\u4e00\u70b9\u662f\uff1a\u5982\u679c\u4f60\u9700\u8981\u5c06 foo::goo \u4e2d\u7684\u51fd\u6570\u79fb\u52a8\u5230\u53e6\u5916\u7684\u5730\u65b9\uff0c\u90a3\u4e48\u4f60\u53ea\u9700\u8981\u66f4\u65b0\u4e00\u4e0b\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684 active \u522b\u540d\u4f7f\u5176\u6307\u5411\u65b0\u7684\u547d\u540d\u7a7a\u95f4\u5373\u53ef\uff0c\u800c\u4e0d\u5fc5\u67e5\u627e\u66ff\u6362\u6240\u6709\u7684 foo::goo . 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include namespace foo :: goo { } namespace v2 { int add ( int x , int y ) { return x + y ; } } int main () { namespace active = v2 ; // active now refers to v2 std :: cout << active :: add ( 1 , 2 ) << '\\n' ; // We don't have to change this return 0 ; } \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0cC++\u7684\u547d\u540d\u7a7a\u95f4\u88ab\u8bbe\u8ba1\u51fa\u6765\u7684\u76ee\u7684\u662f\u4f5c\u4e3a\u4e00\u79cd\u653e\u7f6e\u547d\u540d\u51b2\u7a81\u7684\u673a\u5236\uff0c\u4f46\u5b83\u5e76\u4e0d\u662f\u4e3a\u4e86\u5b9e\u73b0\u4e00\u79cd\u4fe1\u606f\u7684\u5c42\u6b21\u5173\u7cfb\u3002\u8fd9\u4e00\u70b9\u6211\u4eec\u53ef\u4ee5\u4ece\u6807\u51c6\u5e93\u4e2d\u770b\u51fa\uff1a\u51e0\u4e4e\u5168\u90e8\u7684\u6807\u51c6\u5e93\u90fd\u4f4d\u4e8e std:: \u547d\u540d\u7a7a\u95f4\u4e2d\uff08\u5c11\u6570\u65b0\u7684\u529f\u80fd\u653e\u7f6e\u5728\u5d4c\u5957\u547d\u540d\u7a7a\u95f4\u4e2d\uff09\u3002\u5728\u8fd9\u4e00\u70b9\u4e0a\uff0c\u5176\u4ed6\u7684\u4e00\u4e9b\u8bed\u8a00\uff08\u4f8b\u5982C#\uff09\u548c C++ \u662f\u622a\u7136\u4e0d\u540c\u7684\u3002 \u603b\u7684\u6765\u8bf4\uff0c\u5e94\u5f53\u5c3d\u91cf\u907f\u514d\u547d\u540d\u7a7a\u95f4\u7684\u5d4c\u5957\u3002 \u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u4f7f\u7528\u547d\u540d\u7a7a\u95f4 \u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u88ab\u7528\u6765\u5206\u5272\u4e0e\u5f53\u524d\u5e94\u7528\u7a0b\u5e8f\u5f3a\u76f8\u5173\u7684\u4ee3\u7801\u548c\u90a3\u4e9b\u53ef\u80fd\u5728\u65e5\u540e\u88ab\u91cd\u7528\u7684\u4ee3\u7801\uff08\u4f8b\u5982\u6570\u5b66\u51fd\u6570\uff09\u3002\u4f8b\u5982\uff0c\u7269\u7406\u548c\u6570\u5b66\u51fd\u6570\u53ef\u4ee5\u5b9a\u4e49\u5728\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d(\u4f8b\u5982 math:: )\u3002\u8bed\u8a00\u548c\u672c\u5730\u5316\u7684\u51fd\u6570\u5219\u53ef\u4ee5\u5b9a\u4e49\u5728\u53e6\u5916\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\uff08\u4f8b\u5982 lang:: \uff09 \u3002 \u5982\u679c\u4f60\u7f16\u5199\u7684\u662f\u5e93\u51fd\u6570\u6216\u7528\u4e8e\u63d0\u4f9b\u7ed9\u4ed6\u4eba\u4f7f\u7528\u7684\u4ee3\u7801\uff0c\u8bf7\u59cb\u7ec8\u628a\u4f60\u7684\u51fd\u6570\u5b9a\u4e49\u5728\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u5982\u679c\u4f60\u4e0d\u9075\u5faa\u8fd9\u9879\u6700\u4f73\u5b9e\u8df5\uff0c\u5373\u4f60\u7684\u4ee3\u7801\u6ca1\u6709\u58f0\u660e\u5728\u4e00\u4e2a\u7279\u5b9a\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u90a3\u4e48\u5f88\u6709\u53ef\u80fd\u4f1a\u9020\u6210\u547d\u540d\u51b2\u7a81\u3002\u5c06\u4f60\u7684\u4ee3\u7801\u653e\u5728\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u53e6\u5916\u4e00\u4e2a\u597d\u5904\u662f\uff0c\u4f60\u7684\u7528\u6237\u53ef\u4ee5\u501f\u52a9\u7f16\u8f91\u5668\u7684\u81ea\u52a8\u8865\u5168\u548c\u63d0\u793a\u529f\u80fd\u770b\u5230\u4f60\u5e93\u4e2d\u7684\u7684\u5185\u5bb9\u3002","title":"6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26"},{"location":"6-2-User-defined-namespaces-and-the-scope-resolution-operator/#_1","text":"C++ \u5141\u8bb8\u6211\u4eec\u901a\u8fc7 namespace \u5173\u952e\u5b57\u521b\u5efa\u4f60\u81ea\u5df1\u7684\u547d\u540d\u7a7a\u95f4\u3002\u7528\u6237\u4e3a\u4e86\u81ea\u5df1\u7684\u58f0\u660e\u521b\u5efa\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u79f0\u4e3a \u7528\u6237\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4 \u3002C++ \u63d0\u4f9b\u7684\u547d\u540d\u7a7a\u95f4\uff08\u4f8b\u5982 global \u548c std \uff09\u5e76\u6ca1\u6709\u8003\u8651\u7528\u6237\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u3002","title":"\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4"},{"location":"6-2-User-defined-namespaces-and-the-scope-resolution-operator/#_2","text":"\u6211\u4eec\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u91cd\u65b0\u7f16\u5199\u4e86\u4e0a\u9762\u7684\u51e0\u4e2a\u7a0b\u5e8f\uff1a foo.cpp 1 2 3 4 5 6 7 8 namespace foo // define a namespace named foo { // This doSomething() belongs to namespace foo int doSomething ( int x , int y ) { return x + y ; } } goo.cpp 1 2 3 4 5 6 7 8 namespace goo // define a namespace named goo { // This doSomething() belongs to namespace goo int doSomething ( int x , int y ) { return x - y ; } } \u73b0\u5728 foo.cpp \u4e2d\u7684 doSomething() \u4f4d\u4e8e foo \u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u800c goo.cpp \u4e2d\u7684 doSomething() \u5219\u4f4d\u4e8e goo \u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u91cd\u65b0\u7f16\u8bd1\u7a0b\u5e8f\uff0c\u770b\u770b\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002 main.cpp 1 2 3 4 5 6 7 int doSomething ( int x , int y ); // forward declaration for doSomething int main () { std :: cout << doSomething ( 4 , 3 ) << '\\n' ; // which doSomething will we get? return 0 ; } \u7ed3\u679c\u5c31\u662f\u6211\u4eec\u5f97\u5230\u4e86\u53e6\u5916\u7684\u9519\u8bef\u4fe1\u606f\uff01 1 ConsoleApplication1.obj : error LNK2019: unresolved external symbol \"int __cdecl doSomething(int,int)\" (?doSomething@@YAHHH@Z) referenced in function _main \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7f16\u8bd1\u6ca1\u6709\u95ee\u9898(\u56e0\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86 \u524d\u5411\u58f0\u660e )\uff0c\u4f46\u662f\u94fe\u63a5\u5668\u5e76\u4e0d\u80fd\u5728 global \u4f5c\u7528\u57df\u4e2d\u627e\u5230 doSomething \u7684\u5b9a\u4e49\u3002\u8fd9\u662f\u56e0\u4e3a\u8fd9\u4e24\u4e2a\u7248\u672c\u7684 doSomething \u90fd\u5df2\u7ecf\u4e0d\u5728 global \u4f5c\u7528\u57df\u4e2d\u4e86\u3002 \u544a\u8bc9\u7f16\u8bd1\u5668\u4f7f\u7528\u54ea\u4e2a\u7248\u672c doSomething() \u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff0c\u4e00\u79cd\u662f \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \uff0c\u4e00\u79cd\u662fusing\u8bed\u53e5\uff08\u53c2\u89c1: 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 \uff09\u3002 \u5728\u63a5\u4e0b\u6765\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8fd8\u662f\u56de\u5230\u5355\u6587\u4ef6\u7684\u573a\u666f\uff0c\u8fd9\u6837\u770b\u8d77\u6765\u66f4\u52a0\u6e05\u6670\u3002","title":"\u547d\u540d\u7a7a\u95f4\u7684\u6807\u8bc6\u7b26\u901a\u5e38\u4e0d\u662f\u5927\u5199\u5f62\u5f0f"},{"location":"6-2-User-defined-namespaces-and-the-scope-resolution-operator/#_3","text":"\u544a\u8bc9\u7f16\u8bd1\u5668\u5728\u54ea\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u67e5\u627e\u51fd\u6570\u7684\u6700\u597d\u7684\u529e\u6cd5\u5c31\u662f\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26( :: )\u3002\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u544a\u8bc9\u7f16\u8bd1\u5668\u8be5\u8fd0\u7b97\u7b26\u5de6\u9762\u7684\u64cd\u4f5c\u6570\u662f\u5b83\u5e94\u8be5\u67e5\u627e\u7684\u547d\u540d\u7a7a\u95f4\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u67e5\u627e\u7f16\u8bd1\u5668\u67e5\u627e foo \u547d\u540d\u7a7a\u95f4\u7684 doSomething() \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include namespace foo // define a namespace named foo { // This doSomething() belongs to namespace foo int doSomething ( int x , int y ) { return x + y ; } } namespace goo // define a namespace named goo { // This doSomething() belongs to namespace goo int doSomething ( int x , int y ) { return x - y ; } } int main () { std :: cout << foo :: doSomething ( 4 , 3 ) << '\\n' ; // use the doSomething() that exists in namespace foo return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 7 \u5982\u679c\u4f60\u5e0c\u671b\u4f7f\u7528 goo \u4e2d\u7684 doSomething() \uff0c\u5219\u5e94\u8be5\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include namespace foo // define a namespace named foo { // This doSomething() belongs to namespace foo int doSomething ( int x , int y ) { return x + y ; } } namespace goo // define a namespace named goo { // This doSomething() belongs to namespace goo int doSomething ( int x , int y ) { return x - y ; } } int main () { std :: cout << goo :: doSomething ( 4 , 3 ) << '\\n' ; // use the doSomething() that exists in namespace goo return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 1 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u975e\u5e38\u597d\u7528\uff0c\u5b83\u5141\u8bb8\u6211\u4eec \u663e\u5f0f\u5730 \u6307\u5b9a\u4e00\u4e2a\u9700\u8981\u67e5\u627e\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u4e0d\u5b58\u5728\u4efb\u4f55\u6a21\u7cca\u7684\u542b\u4e49\u3002\u6211\u4eec\u751a\u81f3\u53ef\u4ee5\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include namespace foo // define a namespace named foo { // This doSomething() belongs to namespace foo int doSomething ( int x , int y ) { return x + y ; } } namespace goo // define a namespace named goo { // This doSomething() belongs to namespace goo int doSomething ( int x , int y ) { return x - y ; } } int main () { std :: cout << foo :: doSomething ( 4 , 3 ) << '\\n' ; // use the doSomething() that exists in namespace foo std :: cout << goo :: doSomething ( 4 , 3 ) << '\\n' ; // use the doSomething() that exists in namespace goo return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 7 1","title":"\u901a\u8fc7\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26(::)\u8bbf\u95ee\u547d\u540d\u7a7a\u95f4"},{"location":"6-2-User-defined-namespaces-and-the-scope-resolution-operator/#_4","text":"\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u4e5f\u53ef\u4ee5\u7528\u5728\u4e00\u4e2a\u6807\u8bc6\u7b26\u524d\u9762\uff0c\u4f46\u4e0d\u6307\u5b9a\u547d\u540d\u7a7a\u95f4\uff08\u4f8b\u5982 ::doSomething \uff09\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u4f1a\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u67e5\u627e\u6807\u8bc6\u7b26\uff08 doSomething \uff09 \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include void print () // this print lives in the global namespace { std :: cout << \" there \\n \" ; } namespace foo { void print () // this print lives in the foo namespace { std :: cout << \"Hello\" ; } } int main () { foo :: print (); // call print() in foo namespace :: print (); // call print() in global namespace (same as just calling print() in this case) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c ::print() \u7684\u6548\u679c\u548c print() \uff08\u4e0d\u8fdb\u884c\u4f5c\u7528\u57df\u89e3\u6790\uff09\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u6240\u4ee5\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u662f\u591a\u4f59\u7684\u3002\u4f46\u662f\uff0c\u5728\u63a5\u4e0b\u6765\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u80fd\u591f\u770b\u5230\u8fd9\u79cd\u65e0\u524d\u7f00\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u7684\u4f5c\u7528\u3002","title":"\u4f7f\u7528\u6ca1\u6709\u524d\u7f00\u7684\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26"},{"location":"6-2-User-defined-namespaces-and-the-scope-resolution-operator/#_5","text":"\u5f53\u4e00\u4e2a\u6807\u8bc6\u7b26\u4f4d\u4e8e\u67d0\u4e2a\u547d\u540d\u7a7a\u95f4\u65f6\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\u4f5c\u7528\u57df\u89e3\u6790\uff0c\u5219\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u5728\u76f8\u540c\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\u8fdb\u884c\u67e5\u627e\u3002\u5982\u679c\u6ca1\u6709\u627e\u5230\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u5728\u6309\u7167\u5305\u542b\u5173\u7cfb\uff0c\u5728\u547d\u540d\u7a7a\u95f4\u5e8f\u5217\u4e2d\u4f9d\u6b21\u67e5\u627e\uff0c\u6700\u540e\u624d\u4f1a\u67e5\u627e global \u547d\u540d\u7a7a\u95f4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include void print () // this print lives in the global namespace { std :: cout << \" there \\n \" ; } namespace foo { void print () // this print lives in the foo namespace { std :: cout << \"Hello\" ; } void printHelloThere () { print (); // calls print() in foo namespace :: print (); // calls print() in global namespace } } int main () { foo :: printHelloThere (); return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 Hello there \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u8c03\u7528 print() \u51fd\u6570\u65f6\u5e76\u6ca1\u6709\u6307\u5b9a\u9700\u8981\u89e3\u6790\u7684\u4f5c\u7528\u57df\u3002\u56e0\u6b64\u5728 foo \u547d\u540d\u7a7a\u95f4\u4e2d\u8c03\u7528 print() \u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u67e5\u627e foo::print() \u7684\u5b9a\u4e49\u3002\u56e0\u4e3a\u8be5\u5b9a\u4e49\u5b58\u5728\uff0c\u6240\u4ee5 foo::print() \u88ab\u6267\u884c\u3002 \u5982\u679c foo::print() \u5e76\u6ca1\u6709\u88ab\u627e\u5230\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c31\u5fc5\u987b\u67e5\u627e\u5305\u542b\u8be5\u4f5c\u7528\u57df\u7684\u4f5c\u7528\u57df\uff08\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u662f global \u4f5c\u7528\u57df\uff09\u4ee5\u5c1d\u8bd5\u627e\u5230 print() \u3002 \u6ce8\u610f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0d\u5e26\u524d\u7f00\u7684\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\uff08 ::print() \uff09\u660e\u786e\u6307\u5b9a\u7f16\u8bd1\u5668\u4f7f\u7528 global \u547d\u540d\u7a7a\u95f4\u4e2d\u7684 print() \u3002","title":"\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u89e3\u6790"},{"location":"6-2-User-defined-namespaces-and-the-scope-resolution-operator/#_6","text":"\u5c06\u547d\u540d\u7a7a\u95f4\u7684\u58f0\u660e\u5206\u6210\u591a\u4e2a\u90e8\u5206\u653e\u5728\u4e0d\u540c\u7684\u4f4d\u7f6e\u662f\u5408\u6cd5\u7684\uff08\u591a\u4e2a\u6587\u4ef6\u3001\u76f8\u540c\u6587\u4ef6\u7684\u4e0d\u540c\u4f4d\u7f6e\uff09\u3002\u6240\u6709\u8fd9\u4e9b\u58f0\u660e\u90fd\u88ab\u8ba4\u4e3a\u662f\u5bf9\u5e94\u547d\u540d\u7a7a\u95f4\u58f0\u660e\u7684\u4e00\u90e8\u5206\u3002 circle.h 1 2 3 4 5 6 7 8 9 #ifndef CIRCLE_H #define CIRCLE_H namespace basicMath { constexpr double pi { 3.14 }; } #endif growth.h 1 2 3 4 5 6 7 8 9 10 #ifndef GROWTH_H #define GROWTH_H namespace basicMath { // the constant e is also part of namespace basicMath constexpr double e { 2.7 }; } #endif main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 #include \"circle.h\" // for basicMath::pi #include \"growth.h\" // for basicMath::e #include int main () { std :: cout << basicMath :: pi << '\\n' ; std :: cout << basicMath :: e << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u6211\u4eec\u6240\u613f\uff1a 1 2 3.14 2.7 \u6807\u51c6\u5e93\u5927\u91cf\u4f7f\u7528\u4e86\u8fd9\u4e00\u7279\u6027\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u6807\u51c6\u5e93\u7684\u5934\u6587\u4ef6\u90fd\u5c06\u5b83\u58f0\u660e\u7684\u5185\u5bb9\u653e\u5728 namespace std \u5757\u4e2d\u3002\u5426\u5219\uff0c\u5168\u90e8\u7684\u6807\u51c6\u5e93\u5c06\u5fc5\u987b\u5b9a\u4e49\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u6587\u4ef6\u4e2d\u4e86\u3002 \u6ce8\u610f\uff0c\u501f\u52a9\u8fd9\u4e2a\u529f\u80fd\uff0c\u4f60\u5b9e\u9645\u4e0a\u53ef\u4ee5\u628a\u81ea\u5b9a\u4e49\u7684\u51fd\u6570\u4e5f\u653e\u5230 std \u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u4e0d\u8fc7\u8fd9\u4e48\u505a\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u7684\u53d1\u751f\uff0c\u56e0\u4e3a std \u547d\u540d\u7a7a\u95f4\u5177\u6709\u7279\u6b8a\u7684\u89c4\u5219\u4ee5\u4fbf\u7981\u6b62\u7528\u6237\u53bb\u6269\u5c55\u6807\u51c6\u5e93\u7684\u4ee3\u7801\u3002 \u6ce8\u610f \u4e0d\u8981\u628a\u81ea\u5b9a\u4e49\u7684\u529f\u80fd\u5b9a\u4e49\u5728 std \u547d\u540d\u7a7a\u95f4\u4e2d\u3002 \u5982\u679c\u4f60\u9700\u8981\u628a\u4ee3\u7801\u5206\u6563\u5230\u591a\u4e2a\u6587\u4ef6\u65f6\uff0c\u4f60\u5fc5\u987b\u5728\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u4e2d\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\uff1a add.h 1 2 3 4 5 6 7 8 9 10 #ifndef ADD_H #define ADD_H namespace basicMath { // function add() is part of namespace basicMath int add ( int x , int y ); } #endif add.cpp 1 2 3 4 5 6 7 8 9 10 #include \"add.h\" namespace basicMath { // define the function add() int add ( int x , int y ) { return x + y ; } } main.cpp 1 2 3 4 5 6 7 8 9 10 #include \"add.h\" // for basicMath::add() #include int main () { std :: cout << basicMath :: add ( 4 , 3 ) << '\\n' ; return 0 ; } \u5982\u679c\u4f60\u5728\u6e90\u6587\u4ef6\u4e2d\u7701\u7565\u4e86\u547d\u540d\u7a7a\u95f4\uff0c\u7f16\u8bd1\u5668\u5c06\u65e0\u6cd5\u627e\u5230 basicMath::add \uff0c\u56e0\u4e3a\u6e90\u6587\u4ef6\u53ea\u5b9a\u4e49\u4e86 add (\u5168\u5c40\u547d\u540d\u7a7a\u95f4)\u3002\u5982\u679c\u8be5\u547d\u540d\u7a7a\u95f4\u5728\u5934\u6587\u4ef6\u4e2d\u88ab\u7701\u7565\uff0c\u5219\u201cmain.cpp\u201d \u5c31\u65e0\u6cd5\u4f7f\u7528 basicMath::add \uff0c\u56e0\u4e3a\u5b83\u53ea\u80fd\u770b\u5230 add \u7684\u58f0\u660e (\u5168\u5c40\u547d\u540d\u7a7a\u95f4)\u3002","title":"\u547d\u540d\u7a7a\u95f4\u5b9a\u4e49\u4e3a\u591a\u4e2a\u5757"},{"location":"6-2-User-defined-namespaces-and-the-scope-resolution-operator/#_7","text":"\u547d\u540d\u7a7a\u95f4\u4e5f\u53ef\u4ee5\u5d4c\u5957\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include namespace foo { namespace goo // goo is a namespace inside the foo namespace { int add ( int x , int y ) { return x + y ; } } } int main () { std :: cout << foo :: goo :: add ( 1 , 2 ) << '\\n' ; return 0 ; } \u6ce8\u610f\uff0c\u56e0\u4e3a\u547d\u540d\u7a7a\u95f4 goo \u5b9a\u4e49\u5728\u547d\u540d\u7a7a\u95f4 foo \u4e2d\uff0c\u6240\u4ee5\u8981\u8bbf\u95ee add \u5fc5\u987b\u50cf\u8fd9\u6837 foo::goo::add \u3002 \u4ece C++17\u5f00\u59cb\uff0c\u5d4c\u5957\u547d\u540d\u7a7a\u95f4\u4e5f\u53ef\u4ee5\u8fd9\u6837\u58f0\u660e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include namespace foo :: goo // goo is a namespace inside the foo namespace (C++17 style) { int add ( int x , int y ) { return x + y ; } } int main () { std :: cout << foo :: goo :: add ( 1 , 2 ) << '\\n' ; return 0 ; }","title":"\u547d\u540d\u7a7a\u95f4\u5d4c\u5957"},{"location":"6-2-User-defined-namespaces-and-the-scope-resolution-operator/#_8","text":"\u5728C++\u4e2d\u5b8c\u6574\u8f93\u5165\u5d4c\u5957\u547d\u540d\u7a7a\u95f4\u7684\u6807\u8bc6\u7b26\u662f\u975e\u5e38\u75db\u82e6\u7684\u4e00\u4ef6\u4e8b\uff0c\u6240\u4ee5C++\u5141\u8bb8\u4f60\u4e3a\u547d\u540d\u7a7a\u95f4\u521b\u5efa \u522b\u540d \uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u522b\u540d\u4e3a\u4e00\u4e2a\u975e\u5e38\u957f\u7684\u547d\u540d\u7a7a\u95f4\u5e8f\u5217\u521b\u5efa\u4e00\u4e2a\u7b80\u77ed\u7684\u3001\u4e34\u65f6\u7684\u540d\u79f0\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include namespace foo :: goo { int add ( int x , int y ) { return x + y ; } } int main () { namespace active = foo :: goo ; // active now refers to foo::goo std :: cout << active :: add ( 1 , 2 ) << '\\n' ; // This is really foo::goo::add() return 0 ; } // The active alias ends here \u547d\u540d\u7a7a\u95f4\u522b\u540d\u6700\u597d\u7684\u4e00\u70b9\u662f\uff1a\u5982\u679c\u4f60\u9700\u8981\u5c06 foo::goo \u4e2d\u7684\u51fd\u6570\u79fb\u52a8\u5230\u53e6\u5916\u7684\u5730\u65b9\uff0c\u90a3\u4e48\u4f60\u53ea\u9700\u8981\u66f4\u65b0\u4e00\u4e0b\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684 active \u522b\u540d\u4f7f\u5176\u6307\u5411\u65b0\u7684\u547d\u540d\u7a7a\u95f4\u5373\u53ef\uff0c\u800c\u4e0d\u5fc5\u67e5\u627e\u66ff\u6362\u6240\u6709\u7684 foo::goo . 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include namespace foo :: goo { } namespace v2 { int add ( int x , int y ) { return x + y ; } } int main () { namespace active = v2 ; // active now refers to v2 std :: cout << active :: add ( 1 , 2 ) << '\\n' ; // We don't have to change this return 0 ; } \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0cC++\u7684\u547d\u540d\u7a7a\u95f4\u88ab\u8bbe\u8ba1\u51fa\u6765\u7684\u76ee\u7684\u662f\u4f5c\u4e3a\u4e00\u79cd\u653e\u7f6e\u547d\u540d\u51b2\u7a81\u7684\u673a\u5236\uff0c\u4f46\u5b83\u5e76\u4e0d\u662f\u4e3a\u4e86\u5b9e\u73b0\u4e00\u79cd\u4fe1\u606f\u7684\u5c42\u6b21\u5173\u7cfb\u3002\u8fd9\u4e00\u70b9\u6211\u4eec\u53ef\u4ee5\u4ece\u6807\u51c6\u5e93\u4e2d\u770b\u51fa\uff1a\u51e0\u4e4e\u5168\u90e8\u7684\u6807\u51c6\u5e93\u90fd\u4f4d\u4e8e std:: \u547d\u540d\u7a7a\u95f4\u4e2d\uff08\u5c11\u6570\u65b0\u7684\u529f\u80fd\u653e\u7f6e\u5728\u5d4c\u5957\u547d\u540d\u7a7a\u95f4\u4e2d\uff09\u3002\u5728\u8fd9\u4e00\u70b9\u4e0a\uff0c\u5176\u4ed6\u7684\u4e00\u4e9b\u8bed\u8a00\uff08\u4f8b\u5982C#\uff09\u548c C++ \u662f\u622a\u7136\u4e0d\u540c\u7684\u3002 \u603b\u7684\u6765\u8bf4\uff0c\u5e94\u5f53\u5c3d\u91cf\u907f\u514d\u547d\u540d\u7a7a\u95f4\u7684\u5d4c\u5957\u3002","title":"\u547d\u540d\u7a7a\u95f4\u522b\u540d"},{"location":"6-2-User-defined-namespaces-and-the-scope-resolution-operator/#_9","text":"\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u88ab\u7528\u6765\u5206\u5272\u4e0e\u5f53\u524d\u5e94\u7528\u7a0b\u5e8f\u5f3a\u76f8\u5173\u7684\u4ee3\u7801\u548c\u90a3\u4e9b\u53ef\u80fd\u5728\u65e5\u540e\u88ab\u91cd\u7528\u7684\u4ee3\u7801\uff08\u4f8b\u5982\u6570\u5b66\u51fd\u6570\uff09\u3002\u4f8b\u5982\uff0c\u7269\u7406\u548c\u6570\u5b66\u51fd\u6570\u53ef\u4ee5\u5b9a\u4e49\u5728\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d(\u4f8b\u5982 math:: )\u3002\u8bed\u8a00\u548c\u672c\u5730\u5316\u7684\u51fd\u6570\u5219\u53ef\u4ee5\u5b9a\u4e49\u5728\u53e6\u5916\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\uff08\u4f8b\u5982 lang:: \uff09 \u3002 \u5982\u679c\u4f60\u7f16\u5199\u7684\u662f\u5e93\u51fd\u6570\u6216\u7528\u4e8e\u63d0\u4f9b\u7ed9\u4ed6\u4eba\u4f7f\u7528\u7684\u4ee3\u7801\uff0c\u8bf7\u59cb\u7ec8\u628a\u4f60\u7684\u51fd\u6570\u5b9a\u4e49\u5728\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u5982\u679c\u4f60\u4e0d\u9075\u5faa\u8fd9\u9879\u6700\u4f73\u5b9e\u8df5\uff0c\u5373\u4f60\u7684\u4ee3\u7801\u6ca1\u6709\u58f0\u660e\u5728\u4e00\u4e2a\u7279\u5b9a\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u90a3\u4e48\u5f88\u6709\u53ef\u80fd\u4f1a\u9020\u6210\u547d\u540d\u51b2\u7a81\u3002\u5c06\u4f60\u7684\u4ee3\u7801\u653e\u5728\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u53e6\u5916\u4e00\u4e2a\u597d\u5904\u662f\uff0c\u4f60\u7684\u7528\u6237\u53ef\u4ee5\u501f\u52a9\u7f16\u8f91\u5668\u7684\u81ea\u52a8\u8865\u5168\u548c\u63d0\u793a\u529f\u80fd\u770b\u5230\u4f60\u5e93\u4e2d\u7684\u7684\u5185\u5bb9\u3002","title":"\u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u4f7f\u7528\u547d\u540d\u7a7a\u95f4"},{"location":"6-3-Local-variables/","tags":["local variables","storage duration","linkage"],"text":"Key Takeaway \u51fd\u6570\u53c2\u6570\u4e5f\u662f\u5c40\u90e8\u53d8\u91cf \u6807\u8bc6\u7b26\u6709\u591a\u79cd\u5c5e\u6027\uff1a\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u3001\u751f\u547d\u5468\u671f\u3001\u94fe\u63a5 \u5c40\u90e8\u53d8\u91cf\u7684\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u548c\u5b83\u7684\u751f\u547d\u5468\u671f\u662f\u4e00\u6837\u7684\uff0c\u5b83\u5177\u6709\u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\uff0c\u6240\u4ee5\u4e5f\u79f0\u4e3a \u81ea\u52a8\u53d8\u91cf \u5c40\u90e8\u53d8\u91cf\u7684\u94fe\u63a5\u5c5e\u6027\u4e3a\u65e0\u94fe\u63a5\uff0c\u6bcf\u4e2a\u58f0\u660e\u90fd\u6307\u4ee3\u7684\u662f\u4e00\u4e2a\u552f\u4e00\u7684\u5bf9\u8c61 \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u6027\u770b\u4e0a\u53bb\u6709\u70b9\u7c7b\u4f3c\u3002\u5176\u5b9e\u4e0d\u7136\uff0c\u4f5c\u7528\u57df\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5355\u72ec\u7684\u58f0\u660e\u5728\u54ea\u91cc\u53ef\u4ee5\u88ab\u8bbf\u95ee\uff0c\u800c\u94fe\u63a5\u6027\u5b9a\u4e49\u4e86\u591a\u4e2a\u58f0\u660e\u662f\u5426\u6307\u4ee3\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002 \u53d8\u91cf\u5e94\u8be5\u88ab\u5b9a\u4e49\u5728\u5c3d\u53ef\u80fd\u5c0f\u7684\u4f5c\u7528\u57df\u4e2d \u4f7f\u7528\u4e00\u4e2a\u4e13\u95e8\u7684\u8bed\u53e5\u5757\u6765\u9650\u5236\u4e00\u4e2a\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u662f\u6ca1\u5fc5\u8981\u7684\uff0c\u4e0d\u8981\u8fd9\u6837\u505a\u3002\u5982\u679c\u4e00\u5b9a\u6709\u5fc5\u8981\uff0c\u6700\u597d\u8fd8\u662f\u63d0\u70bc\u6210\u4e00\u4e2a\u5355\u72ec\u7684\u51fd\u6570 \u5728 2.5 - \u5c40\u90e8\u4f5c\u7528\u57df \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5c40\u90e8\u53d8\u91cf\u2014\u2014\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8\u7684\u53d8\u91cf\uff08\u5305\u62ec\u51fd\u6570\u53c2\u6570\uff09\u3002 C++\u4e2d\u5e76\u6ca1\u6709\u4e00\u4e2a\u4e13\u95e8\u5c5e\u6027\u80fd\u591f\u5c06\u53d8\u91cf\u5b9a\u4e49\u4e3a\u5c40\u90e8\u53d8\u91cf\u3002\u5c40\u90e8\u53d8\u91cf\u533a\u522b\u4e8e\u5176\u4ed6\u53d8\u91cf\u7684\u5c5e\u6027\u6709\u5f88\u591a\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u8fd9\u4e9b\u4e0d\u540c\u7684\u5c5e\u6027\u3002 \u5728 2.5 - \u5c40\u90e8\u4f5c\u7528\u57df \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86 \u4f5c\u7528\u57df \u7684\u6982\u5ff5\u3002\u4e00\u4e2a \u6807\u8bc6\u7b26 \u7684\u4f5c\u7528\u57df\u6307\u7684\u662f\u5b83\u53ef\u4ee5\u5728\u6e90\u4ee3\u7801\u4e2d\u88ab\u8bbf\u95ee\u7684\u8303\u56f4\u3002\u5f53\u4e00\u4e2a\u6807\u8bc6\u7b26\u53ef\u4ee5\u88ab\u8bbf\u95ee\u65f6\uff0c\u6211\u4eec\u79f0\u5176\u4e3a \u5728\u4f5c\u7528\u57df\u4e2d \u3002\u800c\u5f53\u4e00\u4e2a\u6807\u8bc6\u7b26\u4e0d\u53ef\u4ee5\u88ab\u8bbf\u95ee\u65f6\uff0c\u6211\u4eec\u79f0\u5176 \u8d85\u51fa\u4f5c\u7528\u57df \u3002\u4f5c\u7528\u57df\u662f\u4e00\u4e2a \u7f16\u8bd1\u65f6 \u5c5e\u6027\uff0c\u5f53\u6807\u8bc6\u7b26\u8d85\u51fa\u4f5c\u7528\u57df\u65f6\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 \u5c40\u90e8\u53d8\u91cf\u7684\u5757\u4f5c\u7528\u57df \u5c40\u90e8\u53d8\u91cf\u5177\u6709 \u5757\u4f5c\u7528\u57df \uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4eec\u7684\u4f5c\u7528\u57df\u88ab\u9650\u5b9a\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u4ee3\u7801\u5757\u4e2d\u3002 \u76f8\u5173\u5185\u5bb9 \u5982\u679c\u4f60\u5fd8\u8bb0\u4e86\u4ec0\u4e48\u662f\u4ee3\u7801\u5757\uff0c\u8bf7\u53c2\u8003 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u3002 1 2 3 4 5 6 7 int main () { int i { 5 }; // i enters scope here double d { 4.0 }; // d enters scope here return 0 ; } // i and d go out of scope here \u5c3d\u7ba1\u51fd\u6570\u7684 \u5f62\u53c2 \u5e76\u6ca1\u6709\u88ab\u5b9a\u4e49\u5728\u51fd\u6570\u4f53\u5185\uff0c\u901a\u5e38\u51fd\u6570\u4ecd\u5c06\u5176\u770b\u505a\u662f\u51fd\u6570\u4f53\u4ee3\u7801\u5757\u7684\u4e00\u90e8\u5206\u3002 1 2 3 4 5 6 7 int max ( int x , int y ) // x and y enter scope here { // assign the greater of x or y to max int max { ( x > y ) ? x : y }; // max enters scope here return max ; } // x, y, and max leave scope here \u4f5c\u7528\u57df\u4e2d\u7684\u6240\u6709\u53d8\u91cf\u5fc5\u987b\u5177\u5907\u4e0d\u540c\u7684\u53d8\u91cf\u540d \u5728\u7ed9\u5b9a\u4f5c\u7528\u57df\u4e2d\uff0c\u53d8\u91cf\u7684\u540d\u5b57\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u5426\u5219\u5728\u5f15\u7528\u53d8\u91cf\u7684\u65f6\u5019\u5c31\u4f1a\u51fa\u73b0\u6b67\u4e49\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 void someFunction ( int x ) { int x {}; // compilation failure due to name collision with function parameter } int main () { return 0 ; } \u7531\u4e8e x \u88ab\u5b9a\u4e49\u5728\u51fd\u6570\u4f53\u5185\uff0c\u4e14\u540c\u65f6\u51fd\u6570\u7684\u5f62\u53c2\u4e5f\u662f x \uff0c\u8fd9\u5c31\u9020\u6210\u4e86\u5757\u4f5c\u7528\u57df\u4e2d\u51fa\u73b0\u4e86\u540c\u540d\u7684\u53d8\u91cf\uff0c\u56e0\u6b64\u7a0b\u5e8f\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\u3002 \u5c40\u90e8\u53d8\u91cf\u7684\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u662f\u81ea\u52a8\u7684 \u4e00\u4e2a\u53d8\u91cf\u7684 \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff08\u4e5f\u79f0\u4e3a\u6301\u7eed\u65f6\u95f4\uff09\u51b3\u5b9a\u4e86\u8be5\u53d8\u91cf\u88ab\u521b\u5efa\u548c\u9500\u6bc1\u5e94\u8be5\u9075\u5faa\u7684\u89c4\u5219\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u53d8\u91cf\u7684\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u6709\u5b83\u7684\u751f\u547d\u5468\u671f\u76f4\u63a5\u51b3\u5b9a\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u5728 2.5 - \u5c40\u90e8\u4f5c\u7528\u57df \u4e2d\u63a2\u8ba8\u4e86\u751f\u547d\u5468\u671f\u3002 \u5c40\u90e8\u53d8\u91cf\u5177\u6709\u81ea\u52a8\u7684\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\uff0c\u610f\u5473\u7740\u5b83\u4f1a\u5728\u5b9a\u4e49\u65f6\u88ab\u521b\u5efa\u5e76\u4e14\u5728\u8bed\u53e5\u5757\u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 int main () { int i { 5 }; // i created and initialized here double d { 4.0 }; // d created and initialized here return 0 ; } // i and d are destroyed here \u6b63\u56e0\u4e3a\u6b64\uff0c\u5c40\u90e8\u53d8\u91cf\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a\u81ea\u52a8\u53d8\u91cf\u3002 \u5d4c\u5957\u8bed\u53e5\u5757\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf \u5c40\u90e8\u53d8\u91cf\u53ef\u4ee5\u4f4d\u4e8e\u5d4c\u5957\u7684\u5757\u4e2d\u3002\u8fd9\u79cd\u573a\u666f\u548c\u51fd\u6570\u4f53\u5185\u7684\u5c40\u90e8\u53d8\u91cf\u6ca1\u6709\u4efb\u4f55\u533a\u522b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int main () // outer block { int x { 5 }; // x enters scope and is created here { // nested block int y { 7 }; // y enters scope and is created here } // y goes out of scope and is destroyed here // y can not be used here because it is out of scope in this block return 0 ; } // x goes out of scope and is destroyed here \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf y \u88ab\u5b9a\u4e49\u5728\u4e00\u4e2a\u5d4c\u5957\u7684\u5757\u4e2d\uff0c\u5b83\u7684\u4f5c\u7528\u57df\u8303\u56f4\u4ece\u5b9a\u4e49\u5f00\u59cb\u5230\u5d4c\u5957\u5757\u7ed3\u675f\u65f6\u7ed3\u675f\uff0c\u5b83\u7684\u751f\u547d\u5468\u671f\u4e5f\u662f\u4e00\u6837\u7684\u3002\u56e0\u6b64\u53d8\u91cf y \u7684\u4f5c\u7528\u57df\u88ab\u9650\u5236\u5728\u4e86\u5757\u7684\u5185\u90e8\uff0c\u6240\u4ee5\u5728\u5bf9\u5e94\u4ee3\u7801\u5757\u7684\u5916\u90e8\u662f\u4e0d\u80fd\u591f\u8bbf\u95ee\u8be5\u53d8\u91cf\u3002 \u9700\u8981\u6ce8\u610f\u7684\uff0c\u5d4c\u5957\u5757\u88ab\u770b\u505a\u662f\u5b83\u5916\u5c42\u5757\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u5b9a\u4e49\u5728\u5916\u5c42\u5757\u4e2d\u7684\u53d8\u91cf\u53ef\u4ee5\u5728\u5d4c\u5957\u5757\u4e2d\u8bbf\u95ee\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { // outer block int x { 5 }; // x enters scope and is created here { // nested block int y { 7 }; // y enters scope and is created here // x and y are both in scope here std :: cout << x << \" + \" << y << \" = \" << x + y << '\\n' ; } // y goes out of scope and is destroyed here // y can not be used here because it is out of scope in this block return 0 ; } // x goes out of scope and is destroyed here \u5c40\u90e8\u53d8\u91cf\u7684\u94fe\u63a5\u6027 \u6807\u8bc6\u7b26\u8fd8\u6709\u53e6\u5916\u7684\u4e00\u4e2a\u5c5e\u6027\u2014\u2014 \u94fe\u63a5\u6027(linkage) \u3002\u6807\u8bc6\u7b26\u7684\u94fe\u63a5\u6027\u51b3\u5b9a\u4e86\u76f8\u540c\u540d\u5b57\u7684\u58f0\u660e\u662f\u5426\u6307\u4ee3\u7684\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002 \u5c40\u90e8\u53d8\u91cf\u6ca1\u6709\u94fe\u63a5\uff08 no linkage \uff09\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u6bcf\u4e2a\u58f0\u660e\u90fd\u6307\u4ee3\u7684\u662f\u4e00\u4e2a\u552f\u4e00\u7684\u5bf9\u8c61\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { int x { 2 }; // \u5c40\u90e8\u53d8\u91cf\uff0c\u6ca1\u6709\u94fe\u63a5\u6027 { int x { 3 }; // \u8fd9\u91cc\u7684 x \u8868\u793a\u7684\u662f\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61 } return 0 ; } \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u6027\u770b\u4e0a\u53bb\u6709\u70b9\u7c7b\u4f3c\u3002\u5176\u5b9e\u4e0d\u7136\uff0c\u4f5c\u7528\u57df\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5355\u72ec\u7684\u58f0\u660e\u5728\u54ea\u91cc\u53ef\u4ee5\u88ab\u8bbf\u95ee\uff0c\u800c\u94fe\u63a5\u6027\u5b9a\u4e49\u4e86\u591a\u4e2a\u58f0\u660e\u662f\u5426\u6307\u4ee3\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 6.5 - \u53d8\u91cf\u906e\u853d \u4e2d\u8ba8\u8bba\u5177\u4e00\u4e2a\u53d8\u91cf\u4e0e\u5176\u5d4c\u5957\u8bed\u53e5\u5757\u4e2d\u7684\u540c\u540d\u53d8\u91cf\u4e4b\u95f4\u6709\u4ec0\u4e48\u5f71\u54cd\u3002 \u5c40\u90e8\u53d8\u91cf\u7684\u94fe\u63a5\u6027\u6ca1\u4ec0\u4e48\u597d\u8bb2\u7684\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u5bf9\u94fe\u63a5\u6027\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002 \u53d8\u91cf\u5e94\u8be5\u88ab\u5b9a\u4e49\u5728\u5c3d\u53ef\u80fd\u5c0f\u7684\u4f5c\u7528\u57df\u4e2d \u5982\u679c\u53d8\u91cf\u53ea\u5728\u67d0\u4e2a\u5d4c\u5957\u5757\u4e2d\u4f7f\u7528\uff0c\u90a3\u4e48\u5b83\u5c31\u5e94\u8be5\u88ab\u5b9a\u4e49\u5728\u5d4c\u5957\u5757\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { // do not define y here { // y is only used inside this block, so define it here int y { 5 }; std :: cout << y << '\\n' ; } // otherwise y could still be used here, where it's not needed return 0 ; } \u901a\u8fc7\u9650\u5236\u53d8\u91cf\u7684\u4f5c\u7528\u57df\uff0c\u4f60\u53ef\u4ee5\u51cf\u5c0f\u7a0b\u5e8f\u7684\u590d\u6742\u5ea6\uff0c\u56e0\u4e3a\u8fd9\u6837\u53ef\u4ee5\u51cf\u5c11\u6d3b\u52a8\u53d8\u91cf\u7684\u6570\u91cf\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u8fd9\u6837\u8fd8\u80fd\u66f4\u597d\u7684\u770b\u51fa\u53d8\u91cf\u5728\u54ea\u91cc\u88ab\u4f7f\u7528\u4e86\uff08\u6216\u6ca1\u88ab\u4f7f\u7528\uff09\u3002\u5b9a\u4e49\u5728\u4e00\u4e2a\u5757\u4e2d\u7684\u53d8\u91cf\u53ea\u80fd\u591f\u5728\u8fd9\u4e2a\u5757\uff08\u53ca\u5176\u5d4c\u5957\u5757\uff09\u4e2d\u88ab\u4f7f\u7528\u3002\u8fd9\u4f7f\u5f97\u7a0b\u5e8f\u4e5f\u66f4\u5bb9\u6613\u7406\u89e3\u3002 \u5982\u679c\u5916\u5c42\u5757\u9700\u8981\u4f7f\u7528\u53d8\u91cf\uff0c\u90a3\u4e48\u5b83\u9700\u8981\u5728\u5916\u5c42\u5757\u4e2d\u58f0\u660e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { int y { 5 }; // \u56e0\u4e3a\u5916\u5c42\u5757\u9700\u8981\u4f7f\u7528 y\uff0c\u5219 y \u8981\u88ab\u5b9a\u4e49\u5728\u5916\u5c42 { int x {}; std :: cin >> x ; // if we declared y here, immediately before its actual first use... if ( x == 4 ) y = 4 ; } // ... it would be destroyed here std :: cout << y ; // and we need y to exist here return 0 ; } \u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\u8fd8\u5c55\u793a\u4e86\u4e00\u4e2a\u4f8b\u5916\u60c5\u51b5\uff0c\u5c31\u662f\u53d8\u91cf\u5728\u5b83\u88ab\u4f7f\u7528\u524d\uff0c\u65e9\u65e9\u5730\u5c31\u58f0\u660e\u4e86\uff08\u53c2\u89c1\uff1a 2-5-Introduction-to-local-scope#\u5728\u4f55\u5904\u5b9a\u4e49\u5c40\u90e8\u53d8\u91cf \uff09 \u65b0\u624b\u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u8003\u8651\u662f\u5426\u6709\u5fc5\u8981\u6545\u610f\u521b\u5efa\u4e00\u4e2a\u5d4c\u5957\u5757\u6765\u9650\u5236\u53d8\u91cf\u7684\u4f5c\u7528\u57df\uff08\u5f3a\u5236\u5c06\u5176\u53ca\u65e9\u5730 \u8d85\u51fa\u4f5c\u7528\u57df \u6216\u9500\u6bc1\uff09\u3002\u8fd9\u4e48\u505a\u7684\u786e\u53ef\u4ee5\u4f7f\u53d8\u91cf\u66f4\u7b80\u5355\uff0c\u4f46\u662f\u51fd\u6570\u603b\u4f53\u4e0a\u6765\u8bb2\u53d8\u5f97\u66f4\u957f\u3001\u590d\u6742\u5ea6\u4e5f\u53d8\u9ad8\u4e86\u3002\u8fd9\u4e48\u505a\u901a\u5e38\u6765\u8bf4\u662f\u4e0d\u5212\u7b97\u7684\u3002\u5982\u679c\u7684\u786e\u9700\u8981\u6545\u610f\u521b\u5efa\u4e00\u4e2a\u8bed\u53e5\u5757\u6765\u9650\u5236\u53d8\u91cf\u7684\u4f5c\u7528\u57df\uff0c\u901a\u5e38\u6700\u597d\u7684\u529e\u6cd5\u8fd8\u662f\u628a\u5b83\u653e\u5230\u4e00\u4e2a\u5355\u72ec\u7684\u51fd\u6570\u4e2d\u3002 \u6700\u4f73\u5b9e\u8df5 \u53d8\u91cf\u5e94\u8be5\u88ab\u5b9a\u4e49\u5728\u5c3d\u53ef\u80fd\u5c0f\u7684\u4f5c\u7528\u57df\u4e2d\u3002\u4f7f\u7528\u4e00\u4e2a\u4e13\u95e8\u7684\u8bed\u53e5\u5757\u6765\u9650\u5236\u4e00\u4e2a\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u662f\u6ca1\u5fc5\u8981\u7684\uff0c\u4e0d\u8981\u8fd9\u6837\u505a\u3002","title":"6.3 - \u5c40\u90e8\u53d8\u91cf"},{"location":"6-3-Local-variables/#_1","text":"\u5c40\u90e8\u53d8\u91cf\u5177\u6709 \u5757\u4f5c\u7528\u57df \uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4eec\u7684\u4f5c\u7528\u57df\u88ab\u9650\u5b9a\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u4ee3\u7801\u5757\u4e2d\u3002 \u76f8\u5173\u5185\u5bb9 \u5982\u679c\u4f60\u5fd8\u8bb0\u4e86\u4ec0\u4e48\u662f\u4ee3\u7801\u5757\uff0c\u8bf7\u53c2\u8003 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u3002 1 2 3 4 5 6 7 int main () { int i { 5 }; // i enters scope here double d { 4.0 }; // d enters scope here return 0 ; } // i and d go out of scope here \u5c3d\u7ba1\u51fd\u6570\u7684 \u5f62\u53c2 \u5e76\u6ca1\u6709\u88ab\u5b9a\u4e49\u5728\u51fd\u6570\u4f53\u5185\uff0c\u901a\u5e38\u51fd\u6570\u4ecd\u5c06\u5176\u770b\u505a\u662f\u51fd\u6570\u4f53\u4ee3\u7801\u5757\u7684\u4e00\u90e8\u5206\u3002 1 2 3 4 5 6 7 int max ( int x , int y ) // x and y enter scope here { // assign the greater of x or y to max int max { ( x > y ) ? x : y }; // max enters scope here return max ; } // x, y, and max leave scope here","title":"\u5c40\u90e8\u53d8\u91cf\u7684\u5757\u4f5c\u7528\u57df"},{"location":"6-3-Local-variables/#_2","text":"\u5728\u7ed9\u5b9a\u4f5c\u7528\u57df\u4e2d\uff0c\u53d8\u91cf\u7684\u540d\u5b57\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u5426\u5219\u5728\u5f15\u7528\u53d8\u91cf\u7684\u65f6\u5019\u5c31\u4f1a\u51fa\u73b0\u6b67\u4e49\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 void someFunction ( int x ) { int x {}; // compilation failure due to name collision with function parameter } int main () { return 0 ; } \u7531\u4e8e x \u88ab\u5b9a\u4e49\u5728\u51fd\u6570\u4f53\u5185\uff0c\u4e14\u540c\u65f6\u51fd\u6570\u7684\u5f62\u53c2\u4e5f\u662f x \uff0c\u8fd9\u5c31\u9020\u6210\u4e86\u5757\u4f5c\u7528\u57df\u4e2d\u51fa\u73b0\u4e86\u540c\u540d\u7684\u53d8\u91cf\uff0c\u56e0\u6b64\u7a0b\u5e8f\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\u3002","title":"\u4f5c\u7528\u57df\u4e2d\u7684\u6240\u6709\u53d8\u91cf\u5fc5\u987b\u5177\u5907\u4e0d\u540c\u7684\u53d8\u91cf\u540d"},{"location":"6-3-Local-variables/#_3","text":"\u4e00\u4e2a\u53d8\u91cf\u7684 \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff08\u4e5f\u79f0\u4e3a\u6301\u7eed\u65f6\u95f4\uff09\u51b3\u5b9a\u4e86\u8be5\u53d8\u91cf\u88ab\u521b\u5efa\u548c\u9500\u6bc1\u5e94\u8be5\u9075\u5faa\u7684\u89c4\u5219\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u53d8\u91cf\u7684\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u6709\u5b83\u7684\u751f\u547d\u5468\u671f\u76f4\u63a5\u51b3\u5b9a\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u5728 2.5 - \u5c40\u90e8\u4f5c\u7528\u57df \u4e2d\u63a2\u8ba8\u4e86\u751f\u547d\u5468\u671f\u3002 \u5c40\u90e8\u53d8\u91cf\u5177\u6709\u81ea\u52a8\u7684\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\uff0c\u610f\u5473\u7740\u5b83\u4f1a\u5728\u5b9a\u4e49\u65f6\u88ab\u521b\u5efa\u5e76\u4e14\u5728\u8bed\u53e5\u5757\u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 int main () { int i { 5 }; // i created and initialized here double d { 4.0 }; // d created and initialized here return 0 ; } // i and d are destroyed here \u6b63\u56e0\u4e3a\u6b64\uff0c\u5c40\u90e8\u53d8\u91cf\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a\u81ea\u52a8\u53d8\u91cf\u3002","title":"\u5c40\u90e8\u53d8\u91cf\u7684\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u662f\u81ea\u52a8\u7684"},{"location":"6-3-Local-variables/#_4","text":"\u5c40\u90e8\u53d8\u91cf\u53ef\u4ee5\u4f4d\u4e8e\u5d4c\u5957\u7684\u5757\u4e2d\u3002\u8fd9\u79cd\u573a\u666f\u548c\u51fd\u6570\u4f53\u5185\u7684\u5c40\u90e8\u53d8\u91cf\u6ca1\u6709\u4efb\u4f55\u533a\u522b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int main () // outer block { int x { 5 }; // x enters scope and is created here { // nested block int y { 7 }; // y enters scope and is created here } // y goes out of scope and is destroyed here // y can not be used here because it is out of scope in this block return 0 ; } // x goes out of scope and is destroyed here \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf y \u88ab\u5b9a\u4e49\u5728\u4e00\u4e2a\u5d4c\u5957\u7684\u5757\u4e2d\uff0c\u5b83\u7684\u4f5c\u7528\u57df\u8303\u56f4\u4ece\u5b9a\u4e49\u5f00\u59cb\u5230\u5d4c\u5957\u5757\u7ed3\u675f\u65f6\u7ed3\u675f\uff0c\u5b83\u7684\u751f\u547d\u5468\u671f\u4e5f\u662f\u4e00\u6837\u7684\u3002\u56e0\u6b64\u53d8\u91cf y \u7684\u4f5c\u7528\u57df\u88ab\u9650\u5236\u5728\u4e86\u5757\u7684\u5185\u90e8\uff0c\u6240\u4ee5\u5728\u5bf9\u5e94\u4ee3\u7801\u5757\u7684\u5916\u90e8\u662f\u4e0d\u80fd\u591f\u8bbf\u95ee\u8be5\u53d8\u91cf\u3002 \u9700\u8981\u6ce8\u610f\u7684\uff0c\u5d4c\u5957\u5757\u88ab\u770b\u505a\u662f\u5b83\u5916\u5c42\u5757\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u5b9a\u4e49\u5728\u5916\u5c42\u5757\u4e2d\u7684\u53d8\u91cf\u53ef\u4ee5\u5728\u5d4c\u5957\u5757\u4e2d\u8bbf\u95ee\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { // outer block int x { 5 }; // x enters scope and is created here { // nested block int y { 7 }; // y enters scope and is created here // x and y are both in scope here std :: cout << x << \" + \" << y << \" = \" << x + y << '\\n' ; } // y goes out of scope and is destroyed here // y can not be used here because it is out of scope in this block return 0 ; } // x goes out of scope and is destroyed here","title":"\u5d4c\u5957\u8bed\u53e5\u5757\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf"},{"location":"6-3-Local-variables/#_5","text":"\u6807\u8bc6\u7b26\u8fd8\u6709\u53e6\u5916\u7684\u4e00\u4e2a\u5c5e\u6027\u2014\u2014 \u94fe\u63a5\u6027(linkage) \u3002\u6807\u8bc6\u7b26\u7684\u94fe\u63a5\u6027\u51b3\u5b9a\u4e86\u76f8\u540c\u540d\u5b57\u7684\u58f0\u660e\u662f\u5426\u6307\u4ee3\u7684\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002 \u5c40\u90e8\u53d8\u91cf\u6ca1\u6709\u94fe\u63a5\uff08 no linkage \uff09\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u6bcf\u4e2a\u58f0\u660e\u90fd\u6307\u4ee3\u7684\u662f\u4e00\u4e2a\u552f\u4e00\u7684\u5bf9\u8c61\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { int x { 2 }; // \u5c40\u90e8\u53d8\u91cf\uff0c\u6ca1\u6709\u94fe\u63a5\u6027 { int x { 3 }; // \u8fd9\u91cc\u7684 x \u8868\u793a\u7684\u662f\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61 } return 0 ; } \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u6027\u770b\u4e0a\u53bb\u6709\u70b9\u7c7b\u4f3c\u3002\u5176\u5b9e\u4e0d\u7136\uff0c\u4f5c\u7528\u57df\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5355\u72ec\u7684\u58f0\u660e\u5728\u54ea\u91cc\u53ef\u4ee5\u88ab\u8bbf\u95ee\uff0c\u800c\u94fe\u63a5\u6027\u5b9a\u4e49\u4e86\u591a\u4e2a\u58f0\u660e\u662f\u5426\u6307\u4ee3\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 6.5 - \u53d8\u91cf\u906e\u853d \u4e2d\u8ba8\u8bba\u5177\u4e00\u4e2a\u53d8\u91cf\u4e0e\u5176\u5d4c\u5957\u8bed\u53e5\u5757\u4e2d\u7684\u540c\u540d\u53d8\u91cf\u4e4b\u95f4\u6709\u4ec0\u4e48\u5f71\u54cd\u3002 \u5c40\u90e8\u53d8\u91cf\u7684\u94fe\u63a5\u6027\u6ca1\u4ec0\u4e48\u597d\u8bb2\u7684\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u5bf9\u94fe\u63a5\u6027\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002","title":"\u5c40\u90e8\u53d8\u91cf\u7684\u94fe\u63a5\u6027"},{"location":"6-3-Local-variables/#_6","text":"\u5982\u679c\u53d8\u91cf\u53ea\u5728\u67d0\u4e2a\u5d4c\u5957\u5757\u4e2d\u4f7f\u7528\uff0c\u90a3\u4e48\u5b83\u5c31\u5e94\u8be5\u88ab\u5b9a\u4e49\u5728\u5d4c\u5957\u5757\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { // do not define y here { // y is only used inside this block, so define it here int y { 5 }; std :: cout << y << '\\n' ; } // otherwise y could still be used here, where it's not needed return 0 ; } \u901a\u8fc7\u9650\u5236\u53d8\u91cf\u7684\u4f5c\u7528\u57df\uff0c\u4f60\u53ef\u4ee5\u51cf\u5c0f\u7a0b\u5e8f\u7684\u590d\u6742\u5ea6\uff0c\u56e0\u4e3a\u8fd9\u6837\u53ef\u4ee5\u51cf\u5c11\u6d3b\u52a8\u53d8\u91cf\u7684\u6570\u91cf\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u8fd9\u6837\u8fd8\u80fd\u66f4\u597d\u7684\u770b\u51fa\u53d8\u91cf\u5728\u54ea\u91cc\u88ab\u4f7f\u7528\u4e86\uff08\u6216\u6ca1\u88ab\u4f7f\u7528\uff09\u3002\u5b9a\u4e49\u5728\u4e00\u4e2a\u5757\u4e2d\u7684\u53d8\u91cf\u53ea\u80fd\u591f\u5728\u8fd9\u4e2a\u5757\uff08\u53ca\u5176\u5d4c\u5957\u5757\uff09\u4e2d\u88ab\u4f7f\u7528\u3002\u8fd9\u4f7f\u5f97\u7a0b\u5e8f\u4e5f\u66f4\u5bb9\u6613\u7406\u89e3\u3002 \u5982\u679c\u5916\u5c42\u5757\u9700\u8981\u4f7f\u7528\u53d8\u91cf\uff0c\u90a3\u4e48\u5b83\u9700\u8981\u5728\u5916\u5c42\u5757\u4e2d\u58f0\u660e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { int y { 5 }; // \u56e0\u4e3a\u5916\u5c42\u5757\u9700\u8981\u4f7f\u7528 y\uff0c\u5219 y \u8981\u88ab\u5b9a\u4e49\u5728\u5916\u5c42 { int x {}; std :: cin >> x ; // if we declared y here, immediately before its actual first use... if ( x == 4 ) y = 4 ; } // ... it would be destroyed here std :: cout << y ; // and we need y to exist here return 0 ; } \u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\u8fd8\u5c55\u793a\u4e86\u4e00\u4e2a\u4f8b\u5916\u60c5\u51b5\uff0c\u5c31\u662f\u53d8\u91cf\u5728\u5b83\u88ab\u4f7f\u7528\u524d\uff0c\u65e9\u65e9\u5730\u5c31\u58f0\u660e\u4e86\uff08\u53c2\u89c1\uff1a 2-5-Introduction-to-local-scope#\u5728\u4f55\u5904\u5b9a\u4e49\u5c40\u90e8\u53d8\u91cf \uff09 \u65b0\u624b\u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u8003\u8651\u662f\u5426\u6709\u5fc5\u8981\u6545\u610f\u521b\u5efa\u4e00\u4e2a\u5d4c\u5957\u5757\u6765\u9650\u5236\u53d8\u91cf\u7684\u4f5c\u7528\u57df\uff08\u5f3a\u5236\u5c06\u5176\u53ca\u65e9\u5730 \u8d85\u51fa\u4f5c\u7528\u57df \u6216\u9500\u6bc1\uff09\u3002\u8fd9\u4e48\u505a\u7684\u786e\u53ef\u4ee5\u4f7f\u53d8\u91cf\u66f4\u7b80\u5355\uff0c\u4f46\u662f\u51fd\u6570\u603b\u4f53\u4e0a\u6765\u8bb2\u53d8\u5f97\u66f4\u957f\u3001\u590d\u6742\u5ea6\u4e5f\u53d8\u9ad8\u4e86\u3002\u8fd9\u4e48\u505a\u901a\u5e38\u6765\u8bf4\u662f\u4e0d\u5212\u7b97\u7684\u3002\u5982\u679c\u7684\u786e\u9700\u8981\u6545\u610f\u521b\u5efa\u4e00\u4e2a\u8bed\u53e5\u5757\u6765\u9650\u5236\u53d8\u91cf\u7684\u4f5c\u7528\u57df\uff0c\u901a\u5e38\u6700\u597d\u7684\u529e\u6cd5\u8fd8\u662f\u628a\u5b83\u653e\u5230\u4e00\u4e2a\u5355\u72ec\u7684\u51fd\u6570\u4e2d\u3002 \u6700\u4f73\u5b9e\u8df5 \u53d8\u91cf\u5e94\u8be5\u88ab\u5b9a\u4e49\u5728\u5c3d\u53ef\u80fd\u5c0f\u7684\u4f5c\u7528\u57df\u4e2d\u3002\u4f7f\u7528\u4e00\u4e2a\u4e13\u95e8\u7684\u8bed\u53e5\u5757\u6765\u9650\u5236\u4e00\u4e2a\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u662f\u6ca1\u5fc5\u8981\u7684\uff0c\u4e0d\u8981\u8fd9\u6837\u505a\u3002","title":"\u53d8\u91cf\u5e94\u8be5\u88ab\u5b9a\u4e49\u5728\u5c3d\u53ef\u80fd\u5c0f\u7684\u4f5c\u7528\u57df\u4e2d"},{"location":"6-4-Introduction-to-global-variables/","tags":["global","storage duration"],"text":"Key Takeaway \u8003\u8651\u4e3a\u5168\u5c40\u53d8\u91cf\u6dfb\u52a0 \u201cg\u201d or \u201cg_\u201d \u524d\u7f00\u4ee5\u533a\u522b\u4e8e\u5176\u4ed6\u53d8\u91cf\u3002 \u5168\u5c40\u53d8\u91cf\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\u548c\u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u5168\u5c40\u53d8\u91cf\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u88ab\u521b\u5efa\uff0c\u5e76\u4e14\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u2014\u2014\u5373 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u5177\u6709\u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u7684\u53d8\u91cf\u79f0\u4e3a \u9759\u6001\u53d8\u91cf(static variables) \u3002 \u9759\u6001\u53d8\u91cf\u5177\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c 0 \u5728 6.3 - \u5c40\u90e8\u53d8\u91cf \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5c40\u90e8\u53d8\u91cf\u3002\u5c40\u90e8\u53d8\u91cf\u662f\u5b9a\u4e49\u5728\u51fd\u6570\u4e2d\uff08\u6216\u51fd\u6570\u53c2\u6570\uff09\u7684\u53d8\u91cf\u3002\u5c40\u90e8\u53d8\u91cf\u5177\u6709\u5757\u4f5c\u7528\u57df\uff08\u53ea\u5728\u5b9a\u4e49\u5b83\u7684\u5757\u4e2d\u53ef\u89c1\uff09\uff0c\u540c\u65f6\u5177\u6709\u81ea\u52a8\u6301\u7eed\u65f6\u95f4\uff08\u5728\u5b9a\u4e49\u65f6\u521b\u5efa\uff0c\u5728\u5757\u7ed3\u675f\u65f6\u9500\u6bc1\uff09\u3002 C++ \u4e2d\uff0c\u53d8\u91cf\u4e5f\u53ef\u4ee5\u88ab\u58f0\u660e\u5728\u51fd\u6570 \u5916\u9762 \u3002\u8fd9\u4e9b\u53d8\u91cf\u79f0\u4e3a\u5168\u5c40\u53d8\u91cf\u3002 \u5168\u5c40\u53d8\u91cf\u7684\u58f0\u660e\u548c\u547d\u540d \u6309\u7167\u60ef\u4f8b\uff0c\u5168\u5c40\u53d8\u91cf\u4f1a\u88ab\u5b9a\u4e49\u5728\u6587\u4ef6\u7684\u5f00\u5934\uff0c\u7d27\u63a5\u7740\u5934\u6587\u4ef6\u5305\u542b\u7684\u540e\u9762\uff0c\u4f46\u662f\u5728\u6240\u6709\u4ee3\u7801\u7684\u524d\u9762\u3002\u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include // Variables declared outside of a function are global variables int g_x {}; // global variable g_x void doSomething () { // global variables can be seen and used everywhere in the file g_x = 3 ; std :: cout << g_x << '\\n' ; } int main () { doSomething (); std :: cout << g_x << '\\n' ; // global variables can be seen and used everywhere in the file g_x = 5 ; std :: cout << g_x << '\\n' ; return 0 ; } // g_x goes out of scope here \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b 1 2 3 3 3 5 \u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u5f88\u591a\u7a0b\u5e8f\u5458\u559c\u6b22\u4e3a\u5168\u5c40\u53d8\u91cf\u6dfb\u52a0\u524d\u7f00 \u201cg\u201d \u6216 \u201cg_\u201d \u4ee5\u8868\u660e\u5b83\u662f\u5168\u5c40\u7684\uff08global\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u8003\u8651\u4e3a\u5168\u5c40\u53d8\u91cf\u6dfb\u52a0 \u201cg\u201d or \u201cg_\u201d \u524d\u7f00\u4ee5\u533a\u522b\u4e8e\u5176\u4ed6\u53d8\u91cf\u3002 \u5168\u5c40\u53d8\u91cf\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\u548c\u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u5168\u5c40\u53d8\u91cf\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\uff08\u6709\u65f6\u5019\u4e5f\u975e\u6b63\u5f0f\u5730\u79f0\u4e3a\u5168\u5c40\u4f5c\u7528\u57df\u6216\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4f5c\u7528\u57df\uff09\uff0c\u4e5f\u5c31\u662f\u8bf4\u8fd9\u7c7b\u53d8\u91cf\u4ece\u58f0\u660e\u5b83\u7684\u6587\u4ef6\u5f00\u5934\uff0c\u5230\u6587\u4ef6\u7ed3\u5c3e\u90fd\u662f\u53ef\u89c1\u7684\u3002\u4e00\u65e6\u58f0\u660e\u4e4b\u540e\uff0c\u5168\u5c40\u53d8\u91cf\u5c31\u53ef\u4ee5\u5728\u6587\u4ef6\u7684\u4efb\u4f55\u5730\u65b9\u4f7f\u7528\uff01\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5168\u5c40\u53d8\u91cf g_x \u65e2\u53ef\u4ee5\u5728 doSomething() \u4e2d\u4f7f\u7528\uff0c\u4e5f\u53ef\u4ee5\u5728 main() \u4e2d\u4f7f\u7528\u3002 \u56e0\u4e3a\u5b83\u4eec\u88ab\u5b9a\u4e49\u5728\u51fd\u6570\u7684\u5916\u9762\uff0c\u5168\u5c40\u53d8\u91cf\u901a\u5e38\u88ab\u8ba4\u4e3a\u662f\u5c5e\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u3002 \u5168\u5c40\u53d8\u91cf\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u88ab\u521b\u5efa\uff0c\u5e76\u4e14\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u2014\u2014\u5373 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u5177\u6709\u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u7684\u53d8\u91cf\u79f0\u4e3a \u9759\u6001\u53d8\u91cf(static variables) \u3002 \u5c40\u90e8\u53d8\u91cf\u9ed8\u8ba4\u662f\u672a\u521d\u59cb\u5316\u7684\uff0c\u9759\u6001\u53d8\u91cf\u5219\u4e0d\u540c\uff0c\u5b83\u5177\u6709\u9ed8\u8ba4\u7684\u521d\u59cb\u503c 0 \u3002 \u5168\u5c40\u53d8\u91cf\u521d\u59cb\u5316 \u975e\u5e38\u91cf\u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u4e5f\u53ef\u4ee5\u8fdb\u884c\u521d\u59cb\u5316\uff08\u53ef\u9009\uff09\u3002 1 2 3 int g_x ; // no explicit initializer (zero-initialized by default) int g_y {}; // zero initialized int g_z { 1 }; // initialized with value \u5e38\u91cf\u5168\u5c40\u53d8\u91cf \u548c\u5c40\u90e8\u53d8\u91cf\u4e00\u6837\uff0c\u5168\u5c40\u53d8\u91cf\u4e5f\u53ef\u4ee5\u662f\u5e38\u91cf\u3002\u548c\u6240\u6709\u7684\u5e38\u91cf\u4e00\u6837\uff0c\u5e38\u91cf\u5168\u5c40\u53d8\u91cf\u5fc5\u987b\u88ab\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include const int g_x ; // \u9519\u8bef\uff1aconst \u5e38\u91cf\u5fc5\u987b\u521d\u59cb\u5316 constexpr int g_w ; // \u9519\u8bef\uff1aconstexpr \u5e38\u91cf\u5fc5\u987b\u521d\u59cb\u5316 const int g_y { 1 }; // const global variable g_y, initialized with a value constexpr int g_z { 2 }; // constexpr global variable g_z, initialized with a value void doSomething () { // global variables can be seen and used everywhere in the file std :: cout << g_y << '\\n' ; std :: cout << g_z << '\\n' ; } int main () { doSomething (); // global variables can be seen and used everywhere in the file std :: cout << g_y << '\\n' ; std :: cout << g_z << '\\n' ; return 0 ; } // g_y and g_z goes out of scope here \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf \u8be6\u7ec6\u4ecb\u7ecd\u5e38\u91cf\u5168\u5c40\u53d8\u91cf\u3002 \u5173\u4e8e\u975e\u5e38\u91cf\u7c7b\u578b\u5168\u5c40\u53d8\u91cf\u7684\u63d0\u9192 \u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u5c1d\u8bd5\u5b9a\u4e49\u5f88\u591a\u5168\u5c40\u53d8\u91cf\uff0c\u56e0\u4e3a\u8fd9\u6837\u4ed6\u4eec\u5c31\u53ef\u4ee5\u4e0d\u9700\u8981\u4f20\u53c2\u5c31\u80fd\u591f\u4f7f\u7528\u8fd9\u4e9b\u53d8\u91cf\u3002\u4e0d\u8fc7\uff0c\u901a\u5e38\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\uff01\u6211\u4eec\u4f1a\u5728 6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c \u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002 \u5c0f\u7ed3 1 2 3 4 5 6 7 8 9 10 11 12 // \u975e\u5e38\u91cf\u5168\u5c40\u53d8\u91cf int g_x ; // \u5b9a\u4e49\u672a\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\uff08\u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a0\uff09 int g_x {}; // \u663e\u793a\u5730\u5b9a\u4e49\u521d\u59cb\u5316\u4e3a0\u7684\u5168\u5c40\u53d8\u91cf int g_x { 1 }; // \u663e\u793a\u5730\u521d\u59cb\u5316\u5168\u5c40\u53d8\u91cf // Const \u5168\u5c40\u53d8\u91cf const int g_y ; // \u9519\u8bef\uff1aconst \u53d8\u91cf\u5fc5\u987b\u521d\u59cb\u5316 const int g_y { 2 }; // \u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u5168\u5c40\u5e38\u91cf // Constexpr \u5168\u5c40\u53d8\u91cf constexpr int g_y ; // \u9519\u8bef: constexpr \u53d8\u91cf\u5fc5\u987b\u521d\u59cb\u5316 constexpr int g_y { 3 }; // \u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u5168\u5c40 const \u53d8\u91cf","title":"6.4 - \u5168\u5c40\u53d8\u91cf"},{"location":"6-4-Introduction-to-global-variables/#_1","text":"\u6309\u7167\u60ef\u4f8b\uff0c\u5168\u5c40\u53d8\u91cf\u4f1a\u88ab\u5b9a\u4e49\u5728\u6587\u4ef6\u7684\u5f00\u5934\uff0c\u7d27\u63a5\u7740\u5934\u6587\u4ef6\u5305\u542b\u7684\u540e\u9762\uff0c\u4f46\u662f\u5728\u6240\u6709\u4ee3\u7801\u7684\u524d\u9762\u3002\u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include // Variables declared outside of a function are global variables int g_x {}; // global variable g_x void doSomething () { // global variables can be seen and used everywhere in the file g_x = 3 ; std :: cout << g_x << '\\n' ; } int main () { doSomething (); std :: cout << g_x << '\\n' ; // global variables can be seen and used everywhere in the file g_x = 5 ; std :: cout << g_x << '\\n' ; return 0 ; } // g_x goes out of scope here \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b 1 2 3 3 3 5 \u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u5f88\u591a\u7a0b\u5e8f\u5458\u559c\u6b22\u4e3a\u5168\u5c40\u53d8\u91cf\u6dfb\u52a0\u524d\u7f00 \u201cg\u201d \u6216 \u201cg_\u201d \u4ee5\u8868\u660e\u5b83\u662f\u5168\u5c40\u7684\uff08global\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u8003\u8651\u4e3a\u5168\u5c40\u53d8\u91cf\u6dfb\u52a0 \u201cg\u201d or \u201cg_\u201d \u524d\u7f00\u4ee5\u533a\u522b\u4e8e\u5176\u4ed6\u53d8\u91cf\u3002","title":"\u5168\u5c40\u53d8\u91cf\u7684\u58f0\u660e\u548c\u547d\u540d"},{"location":"6-4-Introduction-to-global-variables/#_2","text":"\u5168\u5c40\u53d8\u91cf\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\uff08\u6709\u65f6\u5019\u4e5f\u975e\u6b63\u5f0f\u5730\u79f0\u4e3a\u5168\u5c40\u4f5c\u7528\u57df\u6216\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4f5c\u7528\u57df\uff09\uff0c\u4e5f\u5c31\u662f\u8bf4\u8fd9\u7c7b\u53d8\u91cf\u4ece\u58f0\u660e\u5b83\u7684\u6587\u4ef6\u5f00\u5934\uff0c\u5230\u6587\u4ef6\u7ed3\u5c3e\u90fd\u662f\u53ef\u89c1\u7684\u3002\u4e00\u65e6\u58f0\u660e\u4e4b\u540e\uff0c\u5168\u5c40\u53d8\u91cf\u5c31\u53ef\u4ee5\u5728\u6587\u4ef6\u7684\u4efb\u4f55\u5730\u65b9\u4f7f\u7528\uff01\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5168\u5c40\u53d8\u91cf g_x \u65e2\u53ef\u4ee5\u5728 doSomething() \u4e2d\u4f7f\u7528\uff0c\u4e5f\u53ef\u4ee5\u5728 main() \u4e2d\u4f7f\u7528\u3002 \u56e0\u4e3a\u5b83\u4eec\u88ab\u5b9a\u4e49\u5728\u51fd\u6570\u7684\u5916\u9762\uff0c\u5168\u5c40\u53d8\u91cf\u901a\u5e38\u88ab\u8ba4\u4e3a\u662f\u5c5e\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u3002 \u5168\u5c40\u53d8\u91cf\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u88ab\u521b\u5efa\uff0c\u5e76\u4e14\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u2014\u2014\u5373 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u5177\u6709\u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u7684\u53d8\u91cf\u79f0\u4e3a \u9759\u6001\u53d8\u91cf(static variables) \u3002 \u5c40\u90e8\u53d8\u91cf\u9ed8\u8ba4\u662f\u672a\u521d\u59cb\u5316\u7684\uff0c\u9759\u6001\u53d8\u91cf\u5219\u4e0d\u540c\uff0c\u5b83\u5177\u6709\u9ed8\u8ba4\u7684\u521d\u59cb\u503c 0 \u3002","title":"\u5168\u5c40\u53d8\u91cf\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\u548c\u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4"},{"location":"6-4-Introduction-to-global-variables/#_3","text":"\u975e\u5e38\u91cf\u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u4e5f\u53ef\u4ee5\u8fdb\u884c\u521d\u59cb\u5316\uff08\u53ef\u9009\uff09\u3002 1 2 3 int g_x ; // no explicit initializer (zero-initialized by default) int g_y {}; // zero initialized int g_z { 1 }; // initialized with value","title":"\u5168\u5c40\u53d8\u91cf\u521d\u59cb\u5316"},{"location":"6-4-Introduction-to-global-variables/#_4","text":"\u548c\u5c40\u90e8\u53d8\u91cf\u4e00\u6837\uff0c\u5168\u5c40\u53d8\u91cf\u4e5f\u53ef\u4ee5\u662f\u5e38\u91cf\u3002\u548c\u6240\u6709\u7684\u5e38\u91cf\u4e00\u6837\uff0c\u5e38\u91cf\u5168\u5c40\u53d8\u91cf\u5fc5\u987b\u88ab\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include const int g_x ; // \u9519\u8bef\uff1aconst \u5e38\u91cf\u5fc5\u987b\u521d\u59cb\u5316 constexpr int g_w ; // \u9519\u8bef\uff1aconstexpr \u5e38\u91cf\u5fc5\u987b\u521d\u59cb\u5316 const int g_y { 1 }; // const global variable g_y, initialized with a value constexpr int g_z { 2 }; // constexpr global variable g_z, initialized with a value void doSomething () { // global variables can be seen and used everywhere in the file std :: cout << g_y << '\\n' ; std :: cout << g_z << '\\n' ; } int main () { doSomething (); // global variables can be seen and used everywhere in the file std :: cout << g_y << '\\n' ; std :: cout << g_z << '\\n' ; return 0 ; } // g_y and g_z goes out of scope here \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf \u8be6\u7ec6\u4ecb\u7ecd\u5e38\u91cf\u5168\u5c40\u53d8\u91cf\u3002","title":"\u5e38\u91cf\u5168\u5c40\u53d8\u91cf"},{"location":"6-4-Introduction-to-global-variables/#_5","text":"\u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u5c1d\u8bd5\u5b9a\u4e49\u5f88\u591a\u5168\u5c40\u53d8\u91cf\uff0c\u56e0\u4e3a\u8fd9\u6837\u4ed6\u4eec\u5c31\u53ef\u4ee5\u4e0d\u9700\u8981\u4f20\u53c2\u5c31\u80fd\u591f\u4f7f\u7528\u8fd9\u4e9b\u53d8\u91cf\u3002\u4e0d\u8fc7\uff0c\u901a\u5e38\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\uff01\u6211\u4eec\u4f1a\u5728 6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c \u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002","title":"\u5173\u4e8e\u975e\u5e38\u91cf\u7c7b\u578b\u5168\u5c40\u53d8\u91cf\u7684\u63d0\u9192"},{"location":"6-4-Introduction-to-global-variables/#_6","text":"1 2 3 4 5 6 7 8 9 10 11 12 // \u975e\u5e38\u91cf\u5168\u5c40\u53d8\u91cf int g_x ; // \u5b9a\u4e49\u672a\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\uff08\u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a0\uff09 int g_x {}; // \u663e\u793a\u5730\u5b9a\u4e49\u521d\u59cb\u5316\u4e3a0\u7684\u5168\u5c40\u53d8\u91cf int g_x { 1 }; // \u663e\u793a\u5730\u521d\u59cb\u5316\u5168\u5c40\u53d8\u91cf // Const \u5168\u5c40\u53d8\u91cf const int g_y ; // \u9519\u8bef\uff1aconst \u53d8\u91cf\u5fc5\u987b\u521d\u59cb\u5316 const int g_y { 2 }; // \u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u5168\u5c40\u5e38\u91cf // Constexpr \u5168\u5c40\u53d8\u91cf constexpr int g_y ; // \u9519\u8bef: constexpr \u53d8\u91cf\u5fc5\u987b\u521d\u59cb\u5316 constexpr int g_y { 3 }; // \u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u5168\u5c40 const \u53d8\u91cf","title":"\u5c0f\u7ed3"},{"location":"6-5-Variable-shadowing-name-hiding/","tags":["variable shadowing"],"text":"Key Takeaway \u5185\u5916\u4ee3\u7801\u5757\u4e2d\u5982\u679c\u6709\u540c\u540d\u7684\u53d8\u91cf\uff0c\u90a3\u4e48\u5728\u4e24\u4e2a\u53d8\u91cf\u90fd\u6709\u6548\u7684\u4f5c\u7528\u57df\u4e2d\uff0c\u5185\u5c42\u5b9a\u4e49\u7684\u53d8\u91cf\u4f1a\u4e34\u65f6\u5c4f\u853d\u5916\u5c42\u5b9a\u4e49\u7684\u53d8\u91cf \u5728\u5185\u5c42\u5d4c\u5957\u5757\u4e2d\uff0c\u662f\u6ca1\u6709\u529e\u6cd5\u76f4\u63a5\u8bbf\u95ee\u5df2\u7ecf\u88ab\u4e34\u65f6\u5c4f\u853d\u7684\u5916\u5c42\u53d8\u91cf\u7684\u3002 \u5f53\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u91cd\u540d\u65f6\uff0c\u5b83\u4e5f\u4f1a\u5728\u5c40\u90e8\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4e2d\u5956\u5168\u5c40\u53d8\u91cf\u4e34\u65f6\u5c4f\u853d,\u4e0d\u8fc7\uff0c\u56e0\u4e3a\u5168\u5c40\u53d8\u91cf\u5c5e\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u7684\u4e00\u90e8\u5206\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6ca1\u6709\u524d\u7f00\u7684 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 ( :: )\u544a\u8bc9\u7f16\u8bd1\u5668\u53bb\u67e5\u627e\u5168\u5c40\u53d8\u91cf\u800c\u4e0d\u662f\u5c40\u90e8\u53d8\u91cf\u3002 \u5168\u5c40\u53d8\u91cf\u540d\u5177\u6709\u201cg_\u201d \u524d\u7f00\u7684\u65f6\u5019\uff0c\u4e0d\u5bb9\u6613\u53d1\u751f\u53d8\u91cf\u906e\u853d \u907f\u514d\u53d8\u91cf\u906e\u853d \u5404\u4e2a\u5757\u5b9a\u4e49\u4e86\u81ea\u5df1\u7684\u4f5c\u7528\u57df\u3002\u6240\u4ee5\u5982\u679c\u6211\u4eec\u5728\u4e00\u4e2a\u5d4c\u5957\u5757\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\uff0c\u800c\u5728\u5176\u5916\u5c42\u5757\u4e2d\uff0c\u6709\u4e00\u4e2a\u540c\u540d\u7684\u53d8\u91cf\uff0c\u6b64\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u5f53\u4e24\u4e2a\u53d8\u91cf\u90fd\u5728\u4f5c\u7528\u57df\u4e2d\u65f6\uff0c\u5d4c\u5957\u5757\u4e2d\u7684\u53d8\u91cf\u4f1a\u5c06\u5916\u5c42\u5757\u4e2d\u7684\u540c\u540d\u53d8\u91cf\u9690\u85cf\u8d77\u6765\uff0c\u79f0\u4e3a\u53d8\u91cf\u9690\u85cf\u6216\u53d8\u91cf\u906e\u853d\uff08shadowing\uff09\u3002 \u5c40\u90e8\u53d8\u91cf\u7684\u906e\u853d 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include int main () { // \u5916\u5c42\u5757 int apples { 5 }; // \u5916\u5c42\u5757\u4e2d\u7684 apples { // \u5d4c\u5957\u5757 // \u6b64\u5904\u7684 apples \u6307\u7684\u662f\u5916\u5c42\u7684 apples std :: cout << apples << '\\n' ; // \u6253\u5370\u5916\u5c42\u7684 apples \u7684\u503c int apples { 0 }; // \u5728\u5d4c\u5957\u5757\u4e2d\u5b9a\u4e49 apples // \u4ece\u6b64\uff0capples \u6307\u7684\u662f\u5d4c\u5957\u5757\u4e2d\u7684 apples // \u5916\u5c42\u7684 apples \u6682\u65f6\u88ab\u5c4f\u853d\u4e86 apples = 10 ; // \u5c06 10 \u8d4b\u503c\u7ed9\u5d4c\u5957\u5757\u4e2d\u7684 apples std :: cout << apples << '\\n' ; // \u6253\u5370\u5d4c\u5957\u5757\u7684 apples \u7684\u503c } // \u5d4c\u5957\u5757\u7684 apples \u9500\u6bc1 std :: cout << apples << '\\n' ; // \u6253\u5370\u5916\u5c42\u7684 apples \u7684\u503c return 0 ; } // \u5916\u5c42\u5757\u7684 apples \u9500\u6bc1 \u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 5 10 5 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5728\u5916\u5c42\u5757\u4e2d\u58f0\u660e\u4e86\u4e00\u4e2a\u540d\u4e3a apples \u7684\u53d8\u91cf\u3002\u8fd9\u4e2a\u53d8\u91cf\u5728\u5d4c\u5957\u5757\u4e2d\u4e5f\u662f\u53ef\u89c1\u7684\uff0c\u53ef\u4ee5\u770b\u5230\u5b83\u7684\u503c\u80fd\u591f\u6b63\u786e\u6253\u5370\u51fa\u6765(5)\u3002 \u7136\u540e\u6211\u4eec\u5728\u5d4c\u5957\u5757\u4e2d\u58f0\u660e\u4e86\u4e00\u4e2a\u4e0d\u540c\u7684\u53d8\u91cf\uff08\u4f46\u540d\u5b57\u4ecd\u7136\u662f apples \uff09\u3002\u4ece\u8fd9\u65f6\u5f00\u59cb\u5230\u5d4c\u5957\u5757\u7684\u7ed3\u5c3e\uff0c apples \u6307\u7684\u5c31\u662f\u5d4c\u5957\u5c42\u7684 apples \u800c\u4e0d\u662f\u5916\u5c42\u7684 apples \u3002 \u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u5c06 10 \u8d4b\u503c\u7ed9 apples \u65f6\uff0c\u6211\u4eec\u8d4b\u503c\u7684\u662f\u5185\u5c42\u5757\u7684 apples \u3002\u7136\u540e\u6253\u5370 apples \uff0c\u5f97\u5230\u7ed3\u679c10\uff0c\u7b49\u5230\u5185\u5c42\u5757\u7ed3\u675f\u65f6\uff0c apples \u5c31\u9500\u6bc1\u4e86\u3002\u4f46\u662f\uff0c\u5916\u5c42\u5757\u7684 apples \u5e76\u4e0d\u4f1a\u53d7\u5f71\u54cd\uff0c\u6240\u4ee5\u6211\u4eec\u5728\u5916\u5c42\u5757\u4e2d\u4ecd\u7136\u53ef\u4ee5\u6253\u5370 apples ( 5 )\u3002 \u6ce8\u610f\uff0c\u5982\u679c\u6211\u4eec\u6ca1\u6709\u5728\u5185\u5c42\u5757\u4e2d\u5b9a\u4e49 apples \uff0c\u90a3\u4e48\u5185\u5c42\u5757\u4e2d\u7684 apples \u6307\u7684\u4ecd\u7136\u662f\u5916\u5c42\u7684 apples \u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5bf9 apples \u8d4b\u503c10\uff0c\u5219\u63a5\u6536\u8be5\u503c\u7684\u4e5f\u662f\u5916\u5c42\u5b9a\u4e49\u7684 apples \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int main () { // outer block int apples { 5 }; // here's the outer block apples { // nested block // apples refers to outer block apples here std :: cout << apples << '\\n' ; // print value of outer block apples // no inner block apples defined in this example apples = 10 ; // this applies to outer block apples std :: cout << apples << '\\n' ; // print value of outer block apples } // outer block apples retains its value even after we leave the nested block std :: cout << apples << '\\n' ; // prints value of outer block apples return 0 ; } // outer block apples destroyed \u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 5 10 10 \u5728\u5185\u5c42\u5d4c\u5957\u5757\u4e2d\uff0c\u662f\u6ca1\u6709\u529e\u6cd5\u76f4\u63a5\u8bbf\u95ee\u5df2\u7ecf\u88ab\u4e34\u65f6\u5c4f\u853d\u7684\u5916\u5c42\u53d8\u91cf\u7684\u3002 \u5168\u5c40\u53d8\u91cf\u7684\u906e\u853d \u5f53\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u91cd\u540d\u65f6\uff0c\u5b83\u4e5f\u4f1a\u5728\u5c40\u90e8\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4e2d\u5956\u5168\u5c40\u53d8\u91cf\u4e34\u65f6\u5c4f\u853d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int value { 5 }; // global variable void foo () { std :: cout << \"global variable value: \" << value << '\\n' ; // value is not shadowed here, so this refers to the global value } int main () { int value { 7 }; // hides the global variable value until the end of this block ++ value ; // increments local value, not global value std :: cout << \"local variable value: \" << value << '\\n' ; foo (); return 0 ; } // local value is destroyed \u6253\u5370\u7ed3\u679c\uff1a 1 2 local variable value: 8 global variable value: 5 \u4e0d\u8fc7\uff0c\u56e0\u4e3a\u5168\u5c40\u53d8\u91cf\u5c5e\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u7684\u4e00\u90e8\u5206\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6ca1\u6709\u524d\u7f00\u7684 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 ( :: )\u544a\u8bc9\u7f16\u8bd1\u5668\u53bb\u67e5\u627e\u5168\u5c40\u53d8\u91cf\u800c\u4e0d\u662f\u5c40\u90e8\u53d8\u91cf\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int value { 5 }; // global variable int main () { int value { 7 }; // hides the global variable value ++ value ; // increments local value, not global value -- ( :: value ); // decrements global value, not local value (parenthesis added for readability) std :: cout << \"local variable value: \" << value << '\\n' ; std :: cout << \"global variable value: \" << :: value << '\\n' ; return 0 ; } // local value is destroyed \u6253\u5370\u7ed3\u679c\uff1a 1 2 local variable value: 8 global variable value: 4 \u907f\u514d\u53d8\u91cf\u906e\u853d \u901a\u5e38\u60c5\u51b5\u4e0b\u5e94\u8be5\u907f\u514d\u53d8\u91cf\u906e\u853d\uff0c \u56e0\u4e3a\u5b83\u5bb9\u6613\u5f15\u8d77\u53d8\u91cf\u8bef\u7528\u7684\u9519\u8bef\u3002\u5f88\u591a\u7f16\u8bd1\u5668\u4f1a\u5728\u68c0\u6d4b\u5230\u53d8\u91cf\u906e\u853d\u65f6\u53d1\u51fa\u544a\u8b66\u3002 \u4e5f\u6b63\u662f\u5904\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u6211\u4eec\u63a8\u8350\u907f\u514d\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u7684\u906e\u853d\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u4f60\u7684\u5168\u5c40\u53d8\u91cf\u540d\u5177\u6709\u201cg_\u201d \u524d\u7f00\u7684\u8bdd\uff0c\u8fd9\u901a\u5e38\u662f\u5f88\u5bb9\u6613\u907f\u514d\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u53d8\u91cf\u906e\u853d","title":"6.5 - \u53d8\u91cf\u906e\u853d"},{"location":"6-5-Variable-shadowing-name-hiding/#_1","text":"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include int main () { // \u5916\u5c42\u5757 int apples { 5 }; // \u5916\u5c42\u5757\u4e2d\u7684 apples { // \u5d4c\u5957\u5757 // \u6b64\u5904\u7684 apples \u6307\u7684\u662f\u5916\u5c42\u7684 apples std :: cout << apples << '\\n' ; // \u6253\u5370\u5916\u5c42\u7684 apples \u7684\u503c int apples { 0 }; // \u5728\u5d4c\u5957\u5757\u4e2d\u5b9a\u4e49 apples // \u4ece\u6b64\uff0capples \u6307\u7684\u662f\u5d4c\u5957\u5757\u4e2d\u7684 apples // \u5916\u5c42\u7684 apples \u6682\u65f6\u88ab\u5c4f\u853d\u4e86 apples = 10 ; // \u5c06 10 \u8d4b\u503c\u7ed9\u5d4c\u5957\u5757\u4e2d\u7684 apples std :: cout << apples << '\\n' ; // \u6253\u5370\u5d4c\u5957\u5757\u7684 apples \u7684\u503c } // \u5d4c\u5957\u5757\u7684 apples \u9500\u6bc1 std :: cout << apples << '\\n' ; // \u6253\u5370\u5916\u5c42\u7684 apples \u7684\u503c return 0 ; } // \u5916\u5c42\u5757\u7684 apples \u9500\u6bc1 \u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 5 10 5 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5728\u5916\u5c42\u5757\u4e2d\u58f0\u660e\u4e86\u4e00\u4e2a\u540d\u4e3a apples \u7684\u53d8\u91cf\u3002\u8fd9\u4e2a\u53d8\u91cf\u5728\u5d4c\u5957\u5757\u4e2d\u4e5f\u662f\u53ef\u89c1\u7684\uff0c\u53ef\u4ee5\u770b\u5230\u5b83\u7684\u503c\u80fd\u591f\u6b63\u786e\u6253\u5370\u51fa\u6765(5)\u3002 \u7136\u540e\u6211\u4eec\u5728\u5d4c\u5957\u5757\u4e2d\u58f0\u660e\u4e86\u4e00\u4e2a\u4e0d\u540c\u7684\u53d8\u91cf\uff08\u4f46\u540d\u5b57\u4ecd\u7136\u662f apples \uff09\u3002\u4ece\u8fd9\u65f6\u5f00\u59cb\u5230\u5d4c\u5957\u5757\u7684\u7ed3\u5c3e\uff0c apples \u6307\u7684\u5c31\u662f\u5d4c\u5957\u5c42\u7684 apples \u800c\u4e0d\u662f\u5916\u5c42\u7684 apples \u3002 \u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u5c06 10 \u8d4b\u503c\u7ed9 apples \u65f6\uff0c\u6211\u4eec\u8d4b\u503c\u7684\u662f\u5185\u5c42\u5757\u7684 apples \u3002\u7136\u540e\u6253\u5370 apples \uff0c\u5f97\u5230\u7ed3\u679c10\uff0c\u7b49\u5230\u5185\u5c42\u5757\u7ed3\u675f\u65f6\uff0c apples \u5c31\u9500\u6bc1\u4e86\u3002\u4f46\u662f\uff0c\u5916\u5c42\u5757\u7684 apples \u5e76\u4e0d\u4f1a\u53d7\u5f71\u54cd\uff0c\u6240\u4ee5\u6211\u4eec\u5728\u5916\u5c42\u5757\u4e2d\u4ecd\u7136\u53ef\u4ee5\u6253\u5370 apples ( 5 )\u3002 \u6ce8\u610f\uff0c\u5982\u679c\u6211\u4eec\u6ca1\u6709\u5728\u5185\u5c42\u5757\u4e2d\u5b9a\u4e49 apples \uff0c\u90a3\u4e48\u5185\u5c42\u5757\u4e2d\u7684 apples \u6307\u7684\u4ecd\u7136\u662f\u5916\u5c42\u7684 apples \u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5bf9 apples \u8d4b\u503c10\uff0c\u5219\u63a5\u6536\u8be5\u503c\u7684\u4e5f\u662f\u5916\u5c42\u5b9a\u4e49\u7684 apples \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int main () { // outer block int apples { 5 }; // here's the outer block apples { // nested block // apples refers to outer block apples here std :: cout << apples << '\\n' ; // print value of outer block apples // no inner block apples defined in this example apples = 10 ; // this applies to outer block apples std :: cout << apples << '\\n' ; // print value of outer block apples } // outer block apples retains its value even after we leave the nested block std :: cout << apples << '\\n' ; // prints value of outer block apples return 0 ; } // outer block apples destroyed \u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 5 10 10 \u5728\u5185\u5c42\u5d4c\u5957\u5757\u4e2d\uff0c\u662f\u6ca1\u6709\u529e\u6cd5\u76f4\u63a5\u8bbf\u95ee\u5df2\u7ecf\u88ab\u4e34\u65f6\u5c4f\u853d\u7684\u5916\u5c42\u53d8\u91cf\u7684\u3002","title":"\u5c40\u90e8\u53d8\u91cf\u7684\u906e\u853d"},{"location":"6-5-Variable-shadowing-name-hiding/#_2","text":"\u5f53\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u91cd\u540d\u65f6\uff0c\u5b83\u4e5f\u4f1a\u5728\u5c40\u90e8\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4e2d\u5956\u5168\u5c40\u53d8\u91cf\u4e34\u65f6\u5c4f\u853d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int value { 5 }; // global variable void foo () { std :: cout << \"global variable value: \" << value << '\\n' ; // value is not shadowed here, so this refers to the global value } int main () { int value { 7 }; // hides the global variable value until the end of this block ++ value ; // increments local value, not global value std :: cout << \"local variable value: \" << value << '\\n' ; foo (); return 0 ; } // local value is destroyed \u6253\u5370\u7ed3\u679c\uff1a 1 2 local variable value: 8 global variable value: 5 \u4e0d\u8fc7\uff0c\u56e0\u4e3a\u5168\u5c40\u53d8\u91cf\u5c5e\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u7684\u4e00\u90e8\u5206\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6ca1\u6709\u524d\u7f00\u7684 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 ( :: )\u544a\u8bc9\u7f16\u8bd1\u5668\u53bb\u67e5\u627e\u5168\u5c40\u53d8\u91cf\u800c\u4e0d\u662f\u5c40\u90e8\u53d8\u91cf\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int value { 5 }; // global variable int main () { int value { 7 }; // hides the global variable value ++ value ; // increments local value, not global value -- ( :: value ); // decrements global value, not local value (parenthesis added for readability) std :: cout << \"local variable value: \" << value << '\\n' ; std :: cout << \"global variable value: \" << :: value << '\\n' ; return 0 ; } // local value is destroyed \u6253\u5370\u7ed3\u679c\uff1a 1 2 local variable value: 8 global variable value: 4","title":"\u5168\u5c40\u53d8\u91cf\u7684\u906e\u853d"},{"location":"6-5-Variable-shadowing-name-hiding/#_3","text":"\u901a\u5e38\u60c5\u51b5\u4e0b\u5e94\u8be5\u907f\u514d\u53d8\u91cf\u906e\u853d\uff0c \u56e0\u4e3a\u5b83\u5bb9\u6613\u5f15\u8d77\u53d8\u91cf\u8bef\u7528\u7684\u9519\u8bef\u3002\u5f88\u591a\u7f16\u8bd1\u5668\u4f1a\u5728\u68c0\u6d4b\u5230\u53d8\u91cf\u906e\u853d\u65f6\u53d1\u51fa\u544a\u8b66\u3002 \u4e5f\u6b63\u662f\u5904\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u6211\u4eec\u63a8\u8350\u907f\u514d\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u7684\u906e\u853d\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u4f60\u7684\u5168\u5c40\u53d8\u91cf\u540d\u5177\u6709\u201cg_\u201d \u524d\u7f00\u7684\u8bdd\uff0c\u8fd9\u901a\u5e38\u662f\u5f88\u5bb9\u6613\u907f\u514d\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u53d8\u91cf\u906e\u853d","title":"\u907f\u514d\u53d8\u91cf\u906e\u853d"},{"location":"6-6-Internal-linkage/","tags":["linkage"],"text":"Key Takeaway \u5168\u5c40\u53d8\u91cf\u548c\u51fd\u6570\u7684\u8868\u793a\u7b26\uff0c\u94fe\u63a5\u5c5e\u6027\u4e3a \u5185\u90e8\u94fe\u63a5(internal linkage) \u6216 \u5185\u90e8\u94fe\u63a5(external linkage) \u5177\u6709\u5185\u90e8\u94fe\u63a5\u5c5e\u6027\u7684\u6807\u8bc6\u7b26\uff0c\u5728\u4e00\u4e2a\u6587\u4ef6\u5185\u53ef\u89c1\u53ef\u7528\uff0c \u4f46\u662f\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u4e0d\u53ef\u8bbf\u95ee static \u53ef\u4ee5\u5c06\u5168\u5c40\u53d8\u91cf\u58f0\u660e\u4e3a \u5185\u90e8\u53d8\u91cf \uff0c Const \u548c constexpr \u7684\u5168\u5c40\u53d8\u91cf\u5219\u9ed8\u8ba4\u5177\u6709\u5185\u90e8\u94fe\u63a5\u5c5e\u6027 \u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26\u53ef\u4ee5\u540c\u65f6\u8bbe\u7f6e\u53d8\u91cf\u7684\u94fe\u63a5\u5c5e\u6027\u548c\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\uff08\u4f46\u4e0d\u662f\u5176\u4f5c\u7528\u57df\uff09\uff0c\u6700\u5e38\u7528\u7684\u6709 static , extern , and mutable \u3002 \u5b9a\u4e49\u5728\u4e0d\u540c\u6587\u4ef6\u4e2d\u7684\u5185\u90e8\u53d8\u91cf\u662f\u72ec\u7acb\u7684\u5b9e\u4f53\uff0c\u56e0\u6b64\u4e0d\u8fdd\u53cd \u5355\u4e00\u5b9a\u4e49\u89c4\u5219 \u94fe\u63a5\u662f\u6807\u8bc6\u7b26\u7684\u5c5e\u6027\uff08\u800c\u4e0d\u662f\u53d8\u91cf\u7684\u5c5e\u6027\uff09 \u51fd\u6570\u6807\u8bc6\u7b26\u9ed8\u8ba4\u662f\u5916\u90e8\u94fe\u63a5\uff0c\u53ef\u4ee5\u901a\u8fc7 static \u5173\u952e\u5b57\u4fee\u6539\u4e3a\u5185\u90e8\u94fe\u63a5\uff0c\u6b64\u65f6\u51fd\u6570\u53ea\u80fd\u5728\u6587\u4ef6\u5185\u90e8\u4f7f\u7528 \u5728 6.3 - \u5c40\u90e8\u53d8\u91cf \u4e2d\u6211\u4eec\u8bb2\u8fc7\uff0c\u6807\u8bc6\u7b26\u7684\u94fe\u63a5\u5c5e\u6027\u51b3\u5b9a\u4e86\u53d8\u91cf\u7684\u591a\u4e2a\u58f0\u660e\u662f\u5426\u6307\u7684\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002\u540c\u65f6\u6211\u4eec\u4e5f\u4ecb\u7ecd\u4e86\u5c40\u90e8\u53d8\u91cf\u7684\u94fe\u63a5\u5c5e\u6027\u4e3a \u65e0\u94fe\u63a5 \u3002 \u5168\u5c40\u53d8\u91cf\u548c\u51fd\u6570\u7684\u6807\u8bc6\u7b26\u7684\u94fe\u63a5\u5c5e\u6027\u53ef\u4ee5\u662f \u5185\u90e8\u94fe\u63a5(internal linkage) \u6216 \u5916\u90e8\u94fe\u63a5(external linkage) \u3002\u672c\u8282\u8bfe\u4f1a\u9488\u5bf9\u5185\u90e8\u94fe\u63a5\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u800c\u5916\u90e8\u94fe\u63a5\u4f1a\u653e\u5230\u4e0b\u4e00\u8282\u8bfe\u2014\u2014 6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e \u3002 \u5177\u6709\u5185\u90e8\u94fe\u63a5\u5c5e\u6027\u7684\u6807\u8bc6\u7b26\uff0c\u5728\u4e00\u4e2a\u6587\u4ef6\u5185\u53ef\u89c1\u53ef\u7528\uff0c\u4f46\u662f\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u4e0d\u53ef\u8bbf\u95ee\uff08\u5373\u4e0d\u4f1a\u66b4\u9732\u7ed9\u94fe\u63a5\u5668\uff09\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u4e24\u4e2a\u6587\u4ef6\u4e2d\u5177\u6709\u540c\u540d\u7684\u6807\u8bc6\u7b26\uff0c\u4e14\u5b83\u4eec\u5177\u6709\u5185\u90e8\u94fe\u63a5\uff0c\u8fd9\u4e9b\u6807\u8bc6\u7b26\u4f1a\u88ab\u770b\u505a\u662f\u4e92\u76f8\u72ec\u7acb\u7684\u3002 \u5168\u5c40\u53d8\u91cf\u548c\u5185\u90e8\u94fe\u63a5 \u5177\u6709\u5185\u90e8\u94fe\u63a5\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a \u5185\u90e8\u53d8\u91cf \u3002 \u901a\u8fc7 static \u5173\u952e\u5b57\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00\u4e2a\u975e\u5e38\u7684\u5168\u5c40\u53d8\u91cf\u58f0\u660e\u4e3a\u5185\u90e8\u53d8\u91cf\u3002 1 2 3 4 5 6 7 8 9 static int g_x ; // non-constant globals have external linkage by default, but can be given internal linkage via the static keyword const int g_y { 1 }; // const globals have internal linkage by default constexpr int g_z { 2 }; // constexpr globals have internal linkage by default int main () { return 0 ; } Const \u548c constexpr \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u9ed8\u8ba4\u5177\u6709\u5185\u90e8\u94fe\u63a5\u5c5e\u6027\uff08\u56e0\u6b64\u4e0d\u9700\u8981\u5173\u952e\u5b57 static \uff0c\u5373\u4f7f\u7528\u4e86\u4e5f\u4f1a\u88ab\u5ffd\u7565\uff09\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u591a\u4e2a\u6587\u4ef6\u4e2d\u5177\u6709\u5185\u90e8\u53d8\u91cf\u7684\u60c5\u51b5\uff1a a.cpp 1 constexpr int g_x { 2 }; // \u8fd9\u4e2a\u5185\u90e8\u53d8\u91cf g_x \u53ea\u5728 a.cpp \u4e2d\u53ef\u4ee5\u8bbf\u95ee main.cpp 1 2 3 4 5 6 7 8 9 10 #include static int g_x { 3 }; // \u8be5\u5185\u90e8\u53d8\u91cf g_x \u53ea\u5728 main.cpp \u4e2d\u53ef\u4ee5\u8bbf\u95ee int main () { std :: cout << g_x << '\\n' ; // uses main.cpp's g_x, prints 3 return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 3 \u56e0\u4e3a g_x is internal to each file, main.cpp has no idea that a.cpp also has a variable named g_x (and vice versa). \u6269\u5c55\u9605\u8bfb static \u53d8\u91cf\u662f\u4e00\u4e2a\u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26\uff0c\u5b83\u53ef\u4ee5\u540c\u65f6\u8bbe\u7f6e\u53d8\u91cf\u7684\u94fe\u63a5\u5c5e\u6027\u548c \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff08\u4f46\u4e0d\u662f\u5176 \u4f5c\u7528\u57df \uff09\u3002\u6700\u5e38\u7528\u7684[[storage-class-specifier|\u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26]]\u6709 static , extern , and mutable \u3002\u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26\u8fd9\u4e2a\u8bcd\u5e38\u88ab\u7528\u5728\u6280\u672f\u6587\u6863\u4e2d\u3002 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u548c\u5185\u90e8\u94fe\u63a5 \u5728 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \uff0c\u5373\u4e00\u4e2a\u5bf9\u8c61\u6216\u8005\u51fd\u6570\u5728\u4e00\u4e2a\u51fd\u6570\u6216\u7a0b\u5e8f\u4e2d\u4e0d\u80fd\u88ab\u5b9a\u4e49\u4e00\u6b21\u4ee5\u4e0a\u3002 \u4f46\u662f\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b9a\u4e49\u5728\u4e0d\u540c\u6587\u4ef6\u4e2d\u7684\u5185\u90e8\u5bf9\u8c61\uff08\u548c\u51fd\u6570\uff09\u88ab\u770b\u505a\u662f\u72ec\u7acb\u7684\u5b9e\u4f53\uff08\u5373\u4f7f\u5b83\u4eec\u7684\u540d\u5b57\u548c\u7c7b\u578b\u662f\u76f8\u540c\u7684\uff09\uff0c\u56e0\u6b64\u8fd9\u5e76\u4e0d\u4f1a\u8fdd\u53cd\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u3002\u6bcf\u4e2a\u5185\u90e8\u5bf9\u8c61\u4ecd\u7136\u53ea\u88ab\u5b9a\u4e49\u4e86\u4e00\u6b21\u3002 \u5177\u6709\u5185\u90e8\u94fe\u63a5\u7684\u51fd\u6570 \u56e0\u4e3a\u94fe\u63a5\u662f\u6807\u8bc6\u7b26\u7684\u5c5e\u6027\uff08\u800c\u4e0d\u662f\u53d8\u91cf\u7684\u5c5e\u6027\uff09\uff0c\u6240\u4ee5\u51fd\u6570\u7684\u8868\u793a\u7b26\u540c\u6837\u5177\u6709\u94fe\u63a5\u5c5e\u6027\u3002\u51fd\u6570\u9ed8\u8ba4\u662f \u5916\u90e8\u94fe\u63a5 \u7684\uff0c\u4f46\u662f\u4e5f\u53ef\u4ee5\u7528 static \u4fee\u6539\u4e3a\u5185\u90e8\u94fe\u63a5\uff1a add.cpp 1 2 3 4 5 6 // \u51fd\u6570\u88ab\u58f0\u660e\u4e3a static\uff0c\u5148\u5728\u5b83\u53ea\u80fd\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528 // \u5c1d\u8bd5\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u901a\u8fc7\u524d\u5411\u58f0\u660e\u6765\u8bbf\u95ee\u8be5\u51fd\u6570\u4f1a\u5bfc\u81f4\u62a5\u9519 static int add ( int x , int y ) { return x + y ; } main.cpp 1 2 3 4 5 6 7 8 9 10 #include int add ( int x , int y ); // \u524d\u5411\u58f0\u660e add int main () { std :: cout << add ( 3 , 4 ) << '\\n' ; return 0 ; } \u7a0b\u5e8f\u65e0\u6cd5\u94fe\u63a5\uff0c add \u5728 add.cpp \u4ee5\u5916\u7684\u5730\u65b9\u4e0d\u80fd\u88ab\u8bbf\u95ee\u3002 \u5c0f\u7ed3 1 2 3 4 5 6 7 8 9 // \u5185\u90e8\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49 static int g_x ; // \u5b9a\u4e49\u672a\u521d\u59cb\u5316\u7684\u5185\u90e8\u5168\u5c40\u53d8\u91cf\uff08\u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a0\uff09 static int g_x { 1 }; // \u5b9a\u4e49\u521d\u59cb\u5316\u7684\u5185\u90e8\u5168\u5c40\u53d8\u91cf const int g_y { 2 }; // \u5b9a\u4e49\u521d\u59cb\u5316\u7684\u5185\u90e8\u5168\u5c40 const \u53d8\u91cf constexpr int g_y { 3 }; // \u5b9a\u4e49\u521d\u59cb\u5316\u7684\u5185\u90e8\u5168\u5c40 constexpr \u53d8\u91cf // \u5185\u90e8\u51fd\u6570\u5b9a\u4e49 static int foo () {}; // \u5b9a\u4e49\u5185\u90e8\u51fd\u6570 \u6211\u4eec\u4f1a\u5728 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 \u4e2d\u5bf9\u76f8\u5173\u5185\u5bb9\u8fdb\u884c\u66f4\u8be6\u7ec6\u7684\u603b\u7ed3\u3002","title":"6.6 - \u5185\u90e8\u94fe\u63a5"},{"location":"6-6-Internal-linkage/#_1","text":"\u5177\u6709\u5185\u90e8\u94fe\u63a5\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a \u5185\u90e8\u53d8\u91cf \u3002 \u901a\u8fc7 static \u5173\u952e\u5b57\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00\u4e2a\u975e\u5e38\u7684\u5168\u5c40\u53d8\u91cf\u58f0\u660e\u4e3a\u5185\u90e8\u53d8\u91cf\u3002 1 2 3 4 5 6 7 8 9 static int g_x ; // non-constant globals have external linkage by default, but can be given internal linkage via the static keyword const int g_y { 1 }; // const globals have internal linkage by default constexpr int g_z { 2 }; // constexpr globals have internal linkage by default int main () { return 0 ; } Const \u548c constexpr \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u9ed8\u8ba4\u5177\u6709\u5185\u90e8\u94fe\u63a5\u5c5e\u6027\uff08\u56e0\u6b64\u4e0d\u9700\u8981\u5173\u952e\u5b57 static \uff0c\u5373\u4f7f\u7528\u4e86\u4e5f\u4f1a\u88ab\u5ffd\u7565\uff09\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u591a\u4e2a\u6587\u4ef6\u4e2d\u5177\u6709\u5185\u90e8\u53d8\u91cf\u7684\u60c5\u51b5\uff1a a.cpp 1 constexpr int g_x { 2 }; // \u8fd9\u4e2a\u5185\u90e8\u53d8\u91cf g_x \u53ea\u5728 a.cpp \u4e2d\u53ef\u4ee5\u8bbf\u95ee main.cpp 1 2 3 4 5 6 7 8 9 10 #include static int g_x { 3 }; // \u8be5\u5185\u90e8\u53d8\u91cf g_x \u53ea\u5728 main.cpp \u4e2d\u53ef\u4ee5\u8bbf\u95ee int main () { std :: cout << g_x << '\\n' ; // uses main.cpp's g_x, prints 3 return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 3 \u56e0\u4e3a g_x is internal to each file, main.cpp has no idea that a.cpp also has a variable named g_x (and vice versa). \u6269\u5c55\u9605\u8bfb static \u53d8\u91cf\u662f\u4e00\u4e2a\u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26\uff0c\u5b83\u53ef\u4ee5\u540c\u65f6\u8bbe\u7f6e\u53d8\u91cf\u7684\u94fe\u63a5\u5c5e\u6027\u548c \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff08\u4f46\u4e0d\u662f\u5176 \u4f5c\u7528\u57df \uff09\u3002\u6700\u5e38\u7528\u7684[[storage-class-specifier|\u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26]]\u6709 static , extern , and mutable \u3002\u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26\u8fd9\u4e2a\u8bcd\u5e38\u88ab\u7528\u5728\u6280\u672f\u6587\u6863\u4e2d\u3002","title":"\u5168\u5c40\u53d8\u91cf\u548c\u5185\u90e8\u94fe\u63a5"},{"location":"6-6-Internal-linkage/#_2","text":"\u5728 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \uff0c\u5373\u4e00\u4e2a\u5bf9\u8c61\u6216\u8005\u51fd\u6570\u5728\u4e00\u4e2a\u51fd\u6570\u6216\u7a0b\u5e8f\u4e2d\u4e0d\u80fd\u88ab\u5b9a\u4e49\u4e00\u6b21\u4ee5\u4e0a\u3002 \u4f46\u662f\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b9a\u4e49\u5728\u4e0d\u540c\u6587\u4ef6\u4e2d\u7684\u5185\u90e8\u5bf9\u8c61\uff08\u548c\u51fd\u6570\uff09\u88ab\u770b\u505a\u662f\u72ec\u7acb\u7684\u5b9e\u4f53\uff08\u5373\u4f7f\u5b83\u4eec\u7684\u540d\u5b57\u548c\u7c7b\u578b\u662f\u76f8\u540c\u7684\uff09\uff0c\u56e0\u6b64\u8fd9\u5e76\u4e0d\u4f1a\u8fdd\u53cd\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u3002\u6bcf\u4e2a\u5185\u90e8\u5bf9\u8c61\u4ecd\u7136\u53ea\u88ab\u5b9a\u4e49\u4e86\u4e00\u6b21\u3002","title":"\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u548c\u5185\u90e8\u94fe\u63a5"},{"location":"6-6-Internal-linkage/#_3","text":"\u56e0\u4e3a\u94fe\u63a5\u662f\u6807\u8bc6\u7b26\u7684\u5c5e\u6027\uff08\u800c\u4e0d\u662f\u53d8\u91cf\u7684\u5c5e\u6027\uff09\uff0c\u6240\u4ee5\u51fd\u6570\u7684\u8868\u793a\u7b26\u540c\u6837\u5177\u6709\u94fe\u63a5\u5c5e\u6027\u3002\u51fd\u6570\u9ed8\u8ba4\u662f \u5916\u90e8\u94fe\u63a5 \u7684\uff0c\u4f46\u662f\u4e5f\u53ef\u4ee5\u7528 static \u4fee\u6539\u4e3a\u5185\u90e8\u94fe\u63a5\uff1a add.cpp 1 2 3 4 5 6 // \u51fd\u6570\u88ab\u58f0\u660e\u4e3a static\uff0c\u5148\u5728\u5b83\u53ea\u80fd\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528 // \u5c1d\u8bd5\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u901a\u8fc7\u524d\u5411\u58f0\u660e\u6765\u8bbf\u95ee\u8be5\u51fd\u6570\u4f1a\u5bfc\u81f4\u62a5\u9519 static int add ( int x , int y ) { return x + y ; } main.cpp 1 2 3 4 5 6 7 8 9 10 #include int add ( int x , int y ); // \u524d\u5411\u58f0\u660e add int main () { std :: cout << add ( 3 , 4 ) << '\\n' ; return 0 ; } \u7a0b\u5e8f\u65e0\u6cd5\u94fe\u63a5\uff0c add \u5728 add.cpp \u4ee5\u5916\u7684\u5730\u65b9\u4e0d\u80fd\u88ab\u8bbf\u95ee\u3002","title":"\u5177\u6709\u5185\u90e8\u94fe\u63a5\u7684\u51fd\u6570"},{"location":"6-6-Internal-linkage/#_4","text":"1 2 3 4 5 6 7 8 9 // \u5185\u90e8\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49 static int g_x ; // \u5b9a\u4e49\u672a\u521d\u59cb\u5316\u7684\u5185\u90e8\u5168\u5c40\u53d8\u91cf\uff08\u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a0\uff09 static int g_x { 1 }; // \u5b9a\u4e49\u521d\u59cb\u5316\u7684\u5185\u90e8\u5168\u5c40\u53d8\u91cf const int g_y { 2 }; // \u5b9a\u4e49\u521d\u59cb\u5316\u7684\u5185\u90e8\u5168\u5c40 const \u53d8\u91cf constexpr int g_y { 3 }; // \u5b9a\u4e49\u521d\u59cb\u5316\u7684\u5185\u90e8\u5168\u5c40 constexpr \u53d8\u91cf // \u5185\u90e8\u51fd\u6570\u5b9a\u4e49 static int foo () {}; // \u5b9a\u4e49\u5185\u90e8\u51fd\u6570 \u6211\u4eec\u4f1a\u5728 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 \u4e2d\u5bf9\u76f8\u5173\u5185\u5bb9\u8fdb\u884c\u66f4\u8be6\u7ec6\u7684\u603b\u7ed3\u3002","title":"\u5c0f\u7ed3"},{"location":"6-7-External-linkage-and-variable-forward-declarations/","tags":["linkage","declaration"],"text":"Key Takeaway \u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u6807\u8bc6\u7b26\uff0c\u53ef\u4ee5\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u6587\u4ef6\u4e2d\u88ab\u4f7f\u7528\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u524d\u5411\u58f0\u660e\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u4f7f\u7528\u3002 \u94fe\u63a5\u662f\u6807\u8bc6\u7b26\u7684\u9009\u9879\uff0c\u800c\u4e0d\u662f\u53d8\u91cf\u7684\u5c5e\u6027\uff0c\u56e0\u6b64\u51fd\u6570\u4e5f\u6709\u94fe\u63a5\u5c5e\u6027 \u51fd\u6570\u662f\u9ed8\u8ba4\u5916\u90e8\u94fe\u63a5\u7684\uff0c\u53ef\u4ee5\u7528 static \u4fee\u6539\u4e3a\u5185\u90e8\u94fe\u63a5 \u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u5168\u5c40\u53d8\u91cf\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a\u5916\u90e8\u53d8\u91cf\u3002\u4f7f\u7528 extern \u5173\u952e\u5b57\u53ef\u4ee5\u5c06\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u4e3a\u5916\u90e8\u53d8\u91cf\uff08\u4f7f\u5176\u53ef\u4ee5\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u8bbf\u95ee\uff09\u3002\u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\u9ed8\u8ba4\u662f\u5916\u90e8\u53d8\u91cf\u3002 \u524d\u5411\u58f0\u660e\u4e00\u4e2a\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u5916\u90e8\u53d8\u91cf\uff0c\u4e5f\u9700\u8981\u4f7f\u7528 extern \u5173\u952e\u5b57 \u5982\u679c\u4f60\u5e0c\u671b\u5b9a\u4e49\u4e00\u4e2a\u672a\u521d\u59cb\u5316\u7684\u975econst\u5168\u5c40\u53d8\u91cf\u3002\u8bf7\u4e0d\u8981\u4f7f\u7528 extern \u5173\u952e\u5b57\uff0c\u5426\u5219 C++ \u4f1a\u8ba4\u4e3a\u4f60\u662f\u5728\u524d\u5411\u58f0\u660e\u67d0\u4e2a\u53d8\u91cf \u5728C++\u4e2d\uff0c \u6240\u6709 \u5168\u5c40\u53d8\u91cf\u90fd\u5177\u6709\u201c\u6587\u4ef6\u4f5c\u7528\u57df\u201d\uff0c\u540c\u65f6\u94fe\u63a5\u5c5e\u6027\u5219\u51b3\u5b9a\u4e86\u5b83\u4eec\u662f\u5426\u53ef\u4ee5\u88ab\u5176\u4ed6\u6587\u4ef6\u4f7f\u7528\u3002 \u5728\u524d\u9762\u7684\u8bfe\u7a0b\u4e2d ( 6.6 - \u5185\u90e8\u94fe\u63a5 )\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u5982\u4f55\u4f7f\u7528 \u5185\u90e8\u94fe\u63a5 \u5c06\u6807\u8bc6\u7b26\u9650\u5b9a\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u6587\u4ef6\u4e2d\u3002\u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u63a2\u8ba8 \u5916\u90e8\u94fe\u63a5 \u3002 \u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u6807\u8bc6\u7b26\uff0c\u53ef\u4ee5\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u6587\u4ef6\u4e2d\u88ab\u4f7f\u7528\uff0c\u4e5f\u53ef\u4ee5\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u88ab\u4f7f\u7528\uff08\u901a\u8fc7 \u524d\u5411\u58f0\u660e \uff09\u3002\u4ece\u8fd9\u4e2a\u610f\u4e49\u4e0a\u6765\u8bb2\uff0c\u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u53d8\u91cf\u624d\u662f\u771f\u6b63\u7684*\u201d\u5168\u5c40\u201c\u53d8\u91cf\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5728\u7a0b\u5e8f\u7684\u4efb\u4f55\u5730\u65b9\u88ab\u4f7f\u7528\u3002 \u51fd\u6570\u9ed8\u8ba4\u5177\u4f53\u5916\u90e8\u94fe\u63a5\u5c5e\u6027 \u5728 2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u8c03\u7528\u53e6\u5916\u4e00\u4e2a\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\uff0c\u8fd9\u662f\u56e0\u4e3a\u51fd\u6570\u9ed8\u8ba4\u5177\u6709\u5916\u90e8\u94fe\u63a5\u5c5e\u6027\u3002 \u4e3a\u4e86\u80fd\u591f\u8c03\u7528\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\uff0c\u5fc5\u987b\u8981\u5728\u4f7f\u7528\u8be5\u51fd\u6570\u7684\u6587\u4ef6\u4e2d\u8fdb\u884c\u524d\u5411\u58f0\u660e\u3002\u524d\u5411\u58f0\u660e\u544a\u8bc9\u7f16\u8bd1\u5668\u51fd\u6570\u7684\u5b58\u5728\uff0c\u7136\u540e\u94fe\u63a5\u5668\u53ef\u4ee5\u5c06\u51fd\u6570\u8c03\u7528\u5173\u8054\u5230\u5b9e\u9645\u7684\u5b9a\u4e49\u3002 \u4f8b\u5982\uff1a a.cpp 1 2 3 4 5 6 #include void sayHi () // \u51fd\u6570\u9ed8\u8ba4\u5177\u6709\u5916\u90e8\u94fe\u63a5\uff0c\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u53ef\u89c1 { std :: cout << \"Hi!\" ; } main.cpp 1 2 3 4 5 6 7 8 void sayHi (); // \u5bf9\u51fd\u6570\u8fdb\u884c\u524d\u5411\u58f0\u660e\uff0c\u4f7f\u8be5\u51fd\u6570\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u53ef\u4ee5\u88ab\u8bbf\u95ee int main () { sayHi (); // \u8c03\u7528\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\uff0c\u94fe\u63a5\u5668\u4f1a\u5c06\u8c03\u7528\u5173\u8054\u5230\u51fd\u6570\u7684\u5b9a\u4e49 return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 Hi! \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c sayHi() \u88ab\u524d\u5411\u58f0\u660e\u5728 main.cpp \u4e2d\uff0c\u8fd9\u4f7f\u5f97 main.cpp \u53ef\u4ee5\u8bbf\u95ee\u5b9a\u4e49\u5728 a.cpp \u4e2d\u7684 sayHi() \u3002\u524d\u5411\u58f0\u660e\u6ee1\u8db3\u4e86\u7f16\u8bd1\u5668\u7684\u9700\u8981\uff0c\u800c\u94fe\u63a5\u5668\u4e5f\u80fd\u591f\u5c06\u51fd\u6570\u8c03\u7528\u5173\u8054\u5230\u51fd\u6570\u5b9a\u4e49\u3002 \u5982\u679c sayHi() \u5177\u6709\u5185\u90e8\u94fe\u63a5\u5c5e\u6027\uff0c\u5219\u94fe\u63a5\u5668\u4fbf\u4e0d\u80fd\u591f\u5c06\u51fd\u6570\u8c03\u7528\u5173\u8054\u5230\u51fd\u6570\u5b9a\u4e49\uff0c\u4f1a\u4ea7\u751f\u94fe\u63a5\u9519\u8bef\u3002 \u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u5168\u5c40\u53d8\u91cf \u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u5168\u5c40\u53d8\u91cf\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a \u5916\u90e8\u53d8\u91cf(external variable) \u3002\u4f7f\u7528 extern \u5173\u952e\u5b57\u53ef\u4ee5\u5c06\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u4e3a\u5916\u90e8\u53d8\u91cf\uff08\u4f7f\u5176\u53ef\u4ee5\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u8bbf\u95ee\uff09\uff1a 1 2 3 4 5 6 7 8 9 int g_x { 2 }; // \u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\u9ed8\u8ba4\u662f\u5916\u90e8\u94fe\u63a5 extern const int g_y { 3 }; // const \u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u88ab\u5b9a\u4e49\u4e3a extern\uff0c\u4f7f\u5176\u6210\u4e3a\u5916\u90e8\u53d8\u91cf extern constexpr int g_z { 3 }; // constexpr \u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u88ab\u5b9a\u4e49\u4e3a extern\uff0c\u4f7f\u5176\u6210\u4e3a\u5916\u90e8\u53d8\u91cf (\u4f46\u662f\u6ca1\u6709\u610f\u4e49) int main () { return 0 ; } \u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\u9ed8\u8ba4\u662f\u5916\u90e8\u94fe\u63a5\uff08\u5373\u4f7f\u4f7f\u7528 extern \u4e5f\u4f1a\u88ab\u5ffd\u7565\uff09\u3002 \u4f7f\u7528 extern \u5173\u952e\u5b57\u8fdb\u884c\u53d8\u91cf\u7684\u524d\u5411\u58f0\u660e \u4e3a\u4e86\u4f7f\u7528\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u5168\u5c40 \u5916\u90e8\u53d8\u91cf \uff0c\u4f60\u540c\u6837\u9700\u8981\u5728\u4f7f\u7528\u5b83\u7684\u6587\u4ef6\u4e2d\u5bf9\u5176\u8fdb\u884c \u524d\u5411\u58f0\u660e \u3002\u5bf9\u4e8e\u53d8\u91cf\u6765\u8bf4\uff0c\u521b\u5efa\u524d\u5411\u58f0\u660e\u8fd8\u9700\u8981\u4f7f\u7528 extern \u5173\u952e\u5b57\uff08\u4e0d\u9700\u8981\u521d\u59cb\u5316\u503c\uff09\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u524d\u5411\u58f0\u660e\u7684\u4f8b\u5b50\uff1a a.cpp 1 2 3 // \u5168\u5c40\u53d8\u91cf\u5b9a\u4e49 int g_x { 2 }; // \u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\u9ed8\u8ba4\u662f\u5916\u90e8\u94fe\u63a5 extern const int g_y { 3 }; // extern \u4e3a g_y \u521b\u5efa\u5916\u90e8\u94fe\u63a5 main.cpp 1 2 3 4 5 6 7 8 9 10 11 #include extern int g_x ; // \u8fd9\u4e2a extern \u662f\u53d8\u91cf g_x\u7684\u524d\u5411\u58f0\u660e\uff0cg_x\u88ab\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6 extern const int g_y ; // \u8fd9\u4e2a extern \u662f const \u53d8\u91cf g_y \u7684\u524d\u5411\u58f0\u660e\uff0c\u5b83\u4e5f\u88ab\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6 int main () { std :: cout << g_x ; // prints 2 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c a.cpp \u548c main.cpp \u90fd\u5f15\u7528\u4e86\u4e00\u4e2a\u76f8\u540c\u7684\u5168\u5c40\u53d8\u91cf g_x \u3002\u56e0\u6b64\uff0c\u5373\u4f7f g_x \u662f\u5728 a.cpp \u4e2d\u5b9a\u4e49\u548c\u521d\u59cb\u5316\u7684\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u5728 main.cpp \u4e2d\u4f7f\u7528\u5b83\uff08\u9700\u8981\u5bf9 g_x \u8fdb\u884c\u524d\u5411\u58f0\u660e\uff09\u3002 \u6ce8\u610f\uff0c extern \u5173\u952e\u5b57\u5728\u4e0d\u540c\u7684\u8bed\u5883\u4e0b\u6709\u4e0d\u540c\u7684\u542b\u4e49\u3002\u6709\u4e9b\u8bed\u5883\u4e0b\uff0c extern \u8868\u793a \u201c\u4e3a\u53d8\u91cf\u521b\u5efa\u5916\u90e8\u94fe\u63a5\u201d\u3002\u800c\u5728\u53e6\u5916\u7684\u8bed\u53e5\u4e0b\uff0c extern \u8868\u793a \u201c\u8fd9\u662f\u4e00\u4e2a\u5b9a\u4e49\u5728\u5176\u4ed6\u5730\u65b9\u7684\u5916\u90e8\u53d8\u91cf\u7684\u524d\u5411\u58f0\u660e\u201d\u3002\u7684\u786e\u6709\u70b9\u7ed5\uff0c\u6240\u4ee5\u6211\u4eec\u4f1a\u5728 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 \u4e2d\u5bf9\u8fd9\u4e9b\u95ee\u9898\u8fdb\u884c\u8be6\u7ec6\u7684\u603b\u7ed3\u3002 \u6ce8\u610f \u5982\u679c\u4f60\u5e0c\u671b\u5b9a\u4e49\u4e00\u4e2a\u672a\u521d\u59cb\u5316\u7684\u975econst\u5168\u5c40\u53d8\u91cf\u3002\u8bf7\u4e0d\u8981\u4f7f\u7528 extern \u5173\u952e\u5b57\uff0c\u5426\u5219 C++ \u4f1a\u8ba4\u4e3a\u4f60\u662f\u5728\u524d\u5411\u58f0\u660e\u67d0\u4e2a\u53d8\u91cf\u3002 \u6ce8\u610f \u5c3d\u7ba1 constexpr \u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7 extern \u5173\u952e\u5b57\u8d4b\u4e88\u5916\u90e8\u94fe\u63a5\u5c5e\u6027\uff0c\u4f46\u662f\u7531\u4e8e\u5b83\u4eec\u5e76\u4e0d\u80fd\u88ab\u524d\u5411\u58f0\u660e\uff0c\u6240\u4ee5\u8fd9\u4e48\u505a\u5e76\u65e0\u610f\u4e49\u3002 \u8fd9\u662f\u56e0\u4e3a\u7f16\u8bd1\u5668\u5728 \u7f16\u8bd1\u65f6 \u5c31\u5fc5\u987b\u6307\u5b9a constexpr \u53d8\u91cf\u7684\u503c\u3002\u5982\u679c\u8be5\u53d8\u91cf\u88ab\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c06\u65e0\u6cd5\u77e5\u9053\u5b83\u7684\u503c\u3002 ^ce6263 \u6ce8\u610f\uff0c\u51fd\u6570\u7684\u524d\u5411\u58f0\u660e\u4e0d\u9700\u8981\u4f7f\u7528 extern \u5173\u952e\u5b57\u2014\u2014\u7f16\u8bd1\u5668\u53ef\u4ee5\u901a\u8fc7\u662f\u5426\u6709\u51fd\u6570\u4f53\u6765\u5224\u65ad\u6b64\u5904\u662f\u5728\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u8fd8\u662f\u5728\u8fdb\u884c\u524d\u5411\u58f0\u660e\u3002\u53d8\u91cf\u7684\u524d\u5411\u58f0\u660e\u662f\u9700\u8981 extern \u5173\u952e\u5b57\u7684\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u533a\u5206\u53d8\u91cf\u5b9a\u4e49\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e\uff08\u770b\u4e0a\u53bb\u53ef\u80fd\u4f1a\u5b8c\u5168\u4e00\u6837\uff09\uff1a 1 2 3 4 5 6 7 // \u975e\u5e38\u91cf int g_x ; // \u53d8\u91cf\u58f0\u660e\u4e5f\u662f\u5b9a\u4e49 (\u53ef\u4ee5\u521d\u59cb\u5316\u4e5f\u53ef\u4ee5\u4e0d\u521d\u59cb\u5316) extern int g_x ; // \u524d\u5411\u58f0\u660e (\u65e0\u521d\u59cb\u5316\u503c) // \u5e38\u91cf extern const int g_y { 1 }; // \u53d8\u91cf\u5b9a\u4e49(const\u5fc5\u987b\u8981\u521d\u59cb\u5316) extern const int g_y ; // \u524d\u5411\u58f0\u660e (\u65e0\u521d\u59cb\u5316\u503c) \u6587\u4ef6\u4f5c\u7528\u57df vs. \u5168\u5c40\u4f5c\u7528\u57df \u201c\u6587\u4ef6\u4f5c\u7528\u57df\u201d\u548c\u201c\u5168\u5c40\u4f5c\u7528\u57df\u201d\u8fd9\u4e24\u4e2a\u672f\u8bed\u53ef\u80fd\u4f1a\u4ee4\u4eba\u611f\u5230\u56f0\u60d1\uff0c\u8fd9\u4e3b\u8981\u662f\u56e0\u4e3a\u5728\u975e\u6b63\u5f0f\u573a\u5408\u5b83\u4eec\u7684\u7528\u6cd5\u4e0d\u591f\u4e25\u8c28\u3002\u4ece\u6280\u672f\u4e0a\u6765\u8bb2\uff0c\u5728 C++\u4e2d\uff0c \u6240\u6709 \u5168\u5c40\u53d8\u91cf\u90fd\u5177\u6709\u201c\u6587\u4ef6\u4f5c\u7528\u57df\u201d\uff0c\u540c\u65f6\u94fe\u63a5\u5c5e\u6027\u5219\u51b3\u5b9a\u4e86\u5b83\u4eec\u662f\u5426\u53ef\u4ee5\u88ab\u5176\u4ed6\u6587\u4ef6\u4f7f\u7528\u3002 \u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a global.cpp 1 2 int g_x { 2 }; // \u9ed8\u8ba4\u662f\u5916\u90e8\u94fe\u63a5 // g_x \u4ece\u8fd9\u91cc\u79bb\u5f00\u4f5c\u7528\u57df main.cpp 1 2 3 4 5 6 7 8 9 extern int g_x ; // \u5bf9 g_x \u8fdb\u884c\u524d\u5411\u58f0\u660e\uff0c\u4ece\u6b64\u65f6\u5f00\u59cb g_x \u53ef\u4ee5\u88ab\u8be5\u6587\u4ef6\u4f7f\u7528\u3002 int main () { std :: cout << g_x ; // should print 2 return 0 ; } // g_x \u7684\u524d\u5411\u58f0\u660e\u4ece\u6b64\u79bb\u5f00\u4f5c\u7528\u57df \u53d8\u91cf g_x \u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\uff08 global.cpp \uff09\u2014\u2014 \u5b83\u53ef\u4ee5\u88ab\u4f7f\u7528\u7684\u8303\u56f4\u4ece\u5b9a\u4e49\u5f00\u59cb\uff0c\u5230\u6587\u4ef6\u7ed3\u675f\u4e3a\u6b62\uff0c\u4f46\u662f\u5b83\u4e0d\u80fd\u76f4\u63a5\u5728 global.cpp \u4ee5\u5916\u7684\u5730\u65b9\u88ab\u4f7f\u7528\u3002 \u5728 main.cpp \u4e2d\uff0c g_x \u7684\u524d\u5411\u58f0\u660e\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\u2014\u2014\u5b83\u53ef\u4ee5\u88ab\u4f7f\u7528\u7684\u8303\u56f4\u4ece\u58f0\u660e\u5f00\u59cb\uff0c\u5230\u6587\u4ef6\u7ed3\u675f\u4e3a\u6b62\u3002 \u4e0d\u8fc7\uff0c\u6211\u4eec\u901a\u5e38\u975e\u6b63\u5f0f\u5730\u5c06\u201c\u6587\u4ef6\u4f5c\u7528\u57df\u201d\u7528\u6765\u63cf\u8ff0\u5177\u6709\u5185\u90e8\u94fe\u63a5\u7684\u53d8\u91cf\uff0c\u800c\u7528\u201c\u5168\u5c40\u4f5c\u7528\u57df\u201d\u6765\u63cf\u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u53d8\u91cf\uff08\u56e0\u4e3a\u8fd9\u7c7b\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7\u524d\u5411\u58f0\u660e\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u88ab\u4f7f\u7528\uff09\u3002 \u5c0f\u7ed3 1 2 3 4 5 6 7 8 9 // \u5916\u90e8\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49 int g_x ; // \u5b9a\u4e49\u5916\u90e8\u5168\u5c40\u53d8\u91cf\u4f46\u4e0d\u521d\u59cb\u5316\uff08\u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a0\uff09 extern const int g_x { 1 }; // \u5b9a\u4e49\u521d\u59cb\u5316\u7684 const \u5916\u90e8\u5168\u5c40\u53d8\u91cf extern constexpr int g_x { 2 }; // \u5b9a\u4e49\u521d\u59cb\u5316\u7684 constexpr \u5916\u90e8\u5168\u5c40\u53d8\u91cf // Forward declarations extern int g_y ; // \u524d\u5411\u58f0\u660e\u975e\u5e38\u6570\u7684\u5168\u5c40\u53d8\u91cf extern const int g_y ; // \u524d\u5411\u58f0\u660e const \u7684\u5168\u5c40\u53d8\u91cf extern constexpr int g_y ; // \u4e0d\u5141\u8bb8\u8fd9\u6837\u505a\uff1aconstexpr \u53d8\u91cf\u4e0d\u80fd\u524d\u5411\u58f0\u660e \u6211\u4eec\u4f1a\u5728 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 \u4e2d\u5bf9\u76f8\u5173\u5185\u5bb9\u8fdb\u884c\u66f4\u8be6\u7ec6\u7684\u603b\u7ed3\u3002","title":"6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e"},{"location":"6-7-External-linkage-and-variable-forward-declarations/#_1","text":"\u5728 2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u8c03\u7528\u53e6\u5916\u4e00\u4e2a\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\uff0c\u8fd9\u662f\u56e0\u4e3a\u51fd\u6570\u9ed8\u8ba4\u5177\u6709\u5916\u90e8\u94fe\u63a5\u5c5e\u6027\u3002 \u4e3a\u4e86\u80fd\u591f\u8c03\u7528\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\uff0c\u5fc5\u987b\u8981\u5728\u4f7f\u7528\u8be5\u51fd\u6570\u7684\u6587\u4ef6\u4e2d\u8fdb\u884c\u524d\u5411\u58f0\u660e\u3002\u524d\u5411\u58f0\u660e\u544a\u8bc9\u7f16\u8bd1\u5668\u51fd\u6570\u7684\u5b58\u5728\uff0c\u7136\u540e\u94fe\u63a5\u5668\u53ef\u4ee5\u5c06\u51fd\u6570\u8c03\u7528\u5173\u8054\u5230\u5b9e\u9645\u7684\u5b9a\u4e49\u3002 \u4f8b\u5982\uff1a a.cpp 1 2 3 4 5 6 #include void sayHi () // \u51fd\u6570\u9ed8\u8ba4\u5177\u6709\u5916\u90e8\u94fe\u63a5\uff0c\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u53ef\u89c1 { std :: cout << \"Hi!\" ; } main.cpp 1 2 3 4 5 6 7 8 void sayHi (); // \u5bf9\u51fd\u6570\u8fdb\u884c\u524d\u5411\u58f0\u660e\uff0c\u4f7f\u8be5\u51fd\u6570\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u53ef\u4ee5\u88ab\u8bbf\u95ee int main () { sayHi (); // \u8c03\u7528\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\uff0c\u94fe\u63a5\u5668\u4f1a\u5c06\u8c03\u7528\u5173\u8054\u5230\u51fd\u6570\u7684\u5b9a\u4e49 return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 Hi! \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c sayHi() \u88ab\u524d\u5411\u58f0\u660e\u5728 main.cpp \u4e2d\uff0c\u8fd9\u4f7f\u5f97 main.cpp \u53ef\u4ee5\u8bbf\u95ee\u5b9a\u4e49\u5728 a.cpp \u4e2d\u7684 sayHi() \u3002\u524d\u5411\u58f0\u660e\u6ee1\u8db3\u4e86\u7f16\u8bd1\u5668\u7684\u9700\u8981\uff0c\u800c\u94fe\u63a5\u5668\u4e5f\u80fd\u591f\u5c06\u51fd\u6570\u8c03\u7528\u5173\u8054\u5230\u51fd\u6570\u5b9a\u4e49\u3002 \u5982\u679c sayHi() \u5177\u6709\u5185\u90e8\u94fe\u63a5\u5c5e\u6027\uff0c\u5219\u94fe\u63a5\u5668\u4fbf\u4e0d\u80fd\u591f\u5c06\u51fd\u6570\u8c03\u7528\u5173\u8054\u5230\u51fd\u6570\u5b9a\u4e49\uff0c\u4f1a\u4ea7\u751f\u94fe\u63a5\u9519\u8bef\u3002","title":"\u51fd\u6570\u9ed8\u8ba4\u5177\u4f53\u5916\u90e8\u94fe\u63a5\u5c5e\u6027"},{"location":"6-7-External-linkage-and-variable-forward-declarations/#_2","text":"\u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u5168\u5c40\u53d8\u91cf\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a \u5916\u90e8\u53d8\u91cf(external variable) \u3002\u4f7f\u7528 extern \u5173\u952e\u5b57\u53ef\u4ee5\u5c06\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u4e3a\u5916\u90e8\u53d8\u91cf\uff08\u4f7f\u5176\u53ef\u4ee5\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u8bbf\u95ee\uff09\uff1a 1 2 3 4 5 6 7 8 9 int g_x { 2 }; // \u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\u9ed8\u8ba4\u662f\u5916\u90e8\u94fe\u63a5 extern const int g_y { 3 }; // const \u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u88ab\u5b9a\u4e49\u4e3a extern\uff0c\u4f7f\u5176\u6210\u4e3a\u5916\u90e8\u53d8\u91cf extern constexpr int g_z { 3 }; // constexpr \u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u88ab\u5b9a\u4e49\u4e3a extern\uff0c\u4f7f\u5176\u6210\u4e3a\u5916\u90e8\u53d8\u91cf (\u4f46\u662f\u6ca1\u6709\u610f\u4e49) int main () { return 0 ; } \u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\u9ed8\u8ba4\u662f\u5916\u90e8\u94fe\u63a5\uff08\u5373\u4f7f\u4f7f\u7528 extern \u4e5f\u4f1a\u88ab\u5ffd\u7565\uff09\u3002","title":"\u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u5168\u5c40\u53d8\u91cf"},{"location":"6-7-External-linkage-and-variable-forward-declarations/#extern","text":"\u4e3a\u4e86\u4f7f\u7528\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u5168\u5c40 \u5916\u90e8\u53d8\u91cf \uff0c\u4f60\u540c\u6837\u9700\u8981\u5728\u4f7f\u7528\u5b83\u7684\u6587\u4ef6\u4e2d\u5bf9\u5176\u8fdb\u884c \u524d\u5411\u58f0\u660e \u3002\u5bf9\u4e8e\u53d8\u91cf\u6765\u8bf4\uff0c\u521b\u5efa\u524d\u5411\u58f0\u660e\u8fd8\u9700\u8981\u4f7f\u7528 extern \u5173\u952e\u5b57\uff08\u4e0d\u9700\u8981\u521d\u59cb\u5316\u503c\uff09\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u524d\u5411\u58f0\u660e\u7684\u4f8b\u5b50\uff1a a.cpp 1 2 3 // \u5168\u5c40\u53d8\u91cf\u5b9a\u4e49 int g_x { 2 }; // \u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\u9ed8\u8ba4\u662f\u5916\u90e8\u94fe\u63a5 extern const int g_y { 3 }; // extern \u4e3a g_y \u521b\u5efa\u5916\u90e8\u94fe\u63a5 main.cpp 1 2 3 4 5 6 7 8 9 10 11 #include extern int g_x ; // \u8fd9\u4e2a extern \u662f\u53d8\u91cf g_x\u7684\u524d\u5411\u58f0\u660e\uff0cg_x\u88ab\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6 extern const int g_y ; // \u8fd9\u4e2a extern \u662f const \u53d8\u91cf g_y \u7684\u524d\u5411\u58f0\u660e\uff0c\u5b83\u4e5f\u88ab\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6 int main () { std :: cout << g_x ; // prints 2 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c a.cpp \u548c main.cpp \u90fd\u5f15\u7528\u4e86\u4e00\u4e2a\u76f8\u540c\u7684\u5168\u5c40\u53d8\u91cf g_x \u3002\u56e0\u6b64\uff0c\u5373\u4f7f g_x \u662f\u5728 a.cpp \u4e2d\u5b9a\u4e49\u548c\u521d\u59cb\u5316\u7684\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u5728 main.cpp \u4e2d\u4f7f\u7528\u5b83\uff08\u9700\u8981\u5bf9 g_x \u8fdb\u884c\u524d\u5411\u58f0\u660e\uff09\u3002 \u6ce8\u610f\uff0c extern \u5173\u952e\u5b57\u5728\u4e0d\u540c\u7684\u8bed\u5883\u4e0b\u6709\u4e0d\u540c\u7684\u542b\u4e49\u3002\u6709\u4e9b\u8bed\u5883\u4e0b\uff0c extern \u8868\u793a \u201c\u4e3a\u53d8\u91cf\u521b\u5efa\u5916\u90e8\u94fe\u63a5\u201d\u3002\u800c\u5728\u53e6\u5916\u7684\u8bed\u53e5\u4e0b\uff0c extern \u8868\u793a \u201c\u8fd9\u662f\u4e00\u4e2a\u5b9a\u4e49\u5728\u5176\u4ed6\u5730\u65b9\u7684\u5916\u90e8\u53d8\u91cf\u7684\u524d\u5411\u58f0\u660e\u201d\u3002\u7684\u786e\u6709\u70b9\u7ed5\uff0c\u6240\u4ee5\u6211\u4eec\u4f1a\u5728 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 \u4e2d\u5bf9\u8fd9\u4e9b\u95ee\u9898\u8fdb\u884c\u8be6\u7ec6\u7684\u603b\u7ed3\u3002 \u6ce8\u610f \u5982\u679c\u4f60\u5e0c\u671b\u5b9a\u4e49\u4e00\u4e2a\u672a\u521d\u59cb\u5316\u7684\u975econst\u5168\u5c40\u53d8\u91cf\u3002\u8bf7\u4e0d\u8981\u4f7f\u7528 extern \u5173\u952e\u5b57\uff0c\u5426\u5219 C++ \u4f1a\u8ba4\u4e3a\u4f60\u662f\u5728\u524d\u5411\u58f0\u660e\u67d0\u4e2a\u53d8\u91cf\u3002 \u6ce8\u610f \u5c3d\u7ba1 constexpr \u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7 extern \u5173\u952e\u5b57\u8d4b\u4e88\u5916\u90e8\u94fe\u63a5\u5c5e\u6027\uff0c\u4f46\u662f\u7531\u4e8e\u5b83\u4eec\u5e76\u4e0d\u80fd\u88ab\u524d\u5411\u58f0\u660e\uff0c\u6240\u4ee5\u8fd9\u4e48\u505a\u5e76\u65e0\u610f\u4e49\u3002 \u8fd9\u662f\u56e0\u4e3a\u7f16\u8bd1\u5668\u5728 \u7f16\u8bd1\u65f6 \u5c31\u5fc5\u987b\u6307\u5b9a constexpr \u53d8\u91cf\u7684\u503c\u3002\u5982\u679c\u8be5\u53d8\u91cf\u88ab\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c06\u65e0\u6cd5\u77e5\u9053\u5b83\u7684\u503c\u3002 ^ce6263 \u6ce8\u610f\uff0c\u51fd\u6570\u7684\u524d\u5411\u58f0\u660e\u4e0d\u9700\u8981\u4f7f\u7528 extern \u5173\u952e\u5b57\u2014\u2014\u7f16\u8bd1\u5668\u53ef\u4ee5\u901a\u8fc7\u662f\u5426\u6709\u51fd\u6570\u4f53\u6765\u5224\u65ad\u6b64\u5904\u662f\u5728\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u8fd8\u662f\u5728\u8fdb\u884c\u524d\u5411\u58f0\u660e\u3002\u53d8\u91cf\u7684\u524d\u5411\u58f0\u660e\u662f\u9700\u8981 extern \u5173\u952e\u5b57\u7684\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u533a\u5206\u53d8\u91cf\u5b9a\u4e49\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e\uff08\u770b\u4e0a\u53bb\u53ef\u80fd\u4f1a\u5b8c\u5168\u4e00\u6837\uff09\uff1a 1 2 3 4 5 6 7 // \u975e\u5e38\u91cf int g_x ; // \u53d8\u91cf\u58f0\u660e\u4e5f\u662f\u5b9a\u4e49 (\u53ef\u4ee5\u521d\u59cb\u5316\u4e5f\u53ef\u4ee5\u4e0d\u521d\u59cb\u5316) extern int g_x ; // \u524d\u5411\u58f0\u660e (\u65e0\u521d\u59cb\u5316\u503c) // \u5e38\u91cf extern const int g_y { 1 }; // \u53d8\u91cf\u5b9a\u4e49(const\u5fc5\u987b\u8981\u521d\u59cb\u5316) extern const int g_y ; // \u524d\u5411\u58f0\u660e (\u65e0\u521d\u59cb\u5316\u503c)","title":"\u4f7f\u7528 extern \u5173\u952e\u5b57\u8fdb\u884c\u53d8\u91cf\u7684\u524d\u5411\u58f0\u660e"},{"location":"6-7-External-linkage-and-variable-forward-declarations/#vs","text":"\u201c\u6587\u4ef6\u4f5c\u7528\u57df\u201d\u548c\u201c\u5168\u5c40\u4f5c\u7528\u57df\u201d\u8fd9\u4e24\u4e2a\u672f\u8bed\u53ef\u80fd\u4f1a\u4ee4\u4eba\u611f\u5230\u56f0\u60d1\uff0c\u8fd9\u4e3b\u8981\u662f\u56e0\u4e3a\u5728\u975e\u6b63\u5f0f\u573a\u5408\u5b83\u4eec\u7684\u7528\u6cd5\u4e0d\u591f\u4e25\u8c28\u3002\u4ece\u6280\u672f\u4e0a\u6765\u8bb2\uff0c\u5728 C++\u4e2d\uff0c \u6240\u6709 \u5168\u5c40\u53d8\u91cf\u90fd\u5177\u6709\u201c\u6587\u4ef6\u4f5c\u7528\u57df\u201d\uff0c\u540c\u65f6\u94fe\u63a5\u5c5e\u6027\u5219\u51b3\u5b9a\u4e86\u5b83\u4eec\u662f\u5426\u53ef\u4ee5\u88ab\u5176\u4ed6\u6587\u4ef6\u4f7f\u7528\u3002 \u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a global.cpp 1 2 int g_x { 2 }; // \u9ed8\u8ba4\u662f\u5916\u90e8\u94fe\u63a5 // g_x \u4ece\u8fd9\u91cc\u79bb\u5f00\u4f5c\u7528\u57df main.cpp 1 2 3 4 5 6 7 8 9 extern int g_x ; // \u5bf9 g_x \u8fdb\u884c\u524d\u5411\u58f0\u660e\uff0c\u4ece\u6b64\u65f6\u5f00\u59cb g_x \u53ef\u4ee5\u88ab\u8be5\u6587\u4ef6\u4f7f\u7528\u3002 int main () { std :: cout << g_x ; // should print 2 return 0 ; } // g_x \u7684\u524d\u5411\u58f0\u660e\u4ece\u6b64\u79bb\u5f00\u4f5c\u7528\u57df \u53d8\u91cf g_x \u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\uff08 global.cpp \uff09\u2014\u2014 \u5b83\u53ef\u4ee5\u88ab\u4f7f\u7528\u7684\u8303\u56f4\u4ece\u5b9a\u4e49\u5f00\u59cb\uff0c\u5230\u6587\u4ef6\u7ed3\u675f\u4e3a\u6b62\uff0c\u4f46\u662f\u5b83\u4e0d\u80fd\u76f4\u63a5\u5728 global.cpp \u4ee5\u5916\u7684\u5730\u65b9\u88ab\u4f7f\u7528\u3002 \u5728 main.cpp \u4e2d\uff0c g_x \u7684\u524d\u5411\u58f0\u660e\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\u2014\u2014\u5b83\u53ef\u4ee5\u88ab\u4f7f\u7528\u7684\u8303\u56f4\u4ece\u58f0\u660e\u5f00\u59cb\uff0c\u5230\u6587\u4ef6\u7ed3\u675f\u4e3a\u6b62\u3002 \u4e0d\u8fc7\uff0c\u6211\u4eec\u901a\u5e38\u975e\u6b63\u5f0f\u5730\u5c06\u201c\u6587\u4ef6\u4f5c\u7528\u57df\u201d\u7528\u6765\u63cf\u8ff0\u5177\u6709\u5185\u90e8\u94fe\u63a5\u7684\u53d8\u91cf\uff0c\u800c\u7528\u201c\u5168\u5c40\u4f5c\u7528\u57df\u201d\u6765\u63cf\u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u53d8\u91cf\uff08\u56e0\u4e3a\u8fd9\u7c7b\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7\u524d\u5411\u58f0\u660e\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u88ab\u4f7f\u7528\uff09\u3002","title":"\u6587\u4ef6\u4f5c\u7528\u57df vs. \u5168\u5c40\u4f5c\u7528\u57df"},{"location":"6-7-External-linkage-and-variable-forward-declarations/#_3","text":"1 2 3 4 5 6 7 8 9 // \u5916\u90e8\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49 int g_x ; // \u5b9a\u4e49\u5916\u90e8\u5168\u5c40\u53d8\u91cf\u4f46\u4e0d\u521d\u59cb\u5316\uff08\u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a0\uff09 extern const int g_x { 1 }; // \u5b9a\u4e49\u521d\u59cb\u5316\u7684 const \u5916\u90e8\u5168\u5c40\u53d8\u91cf extern constexpr int g_x { 2 }; // \u5b9a\u4e49\u521d\u59cb\u5316\u7684 constexpr \u5916\u90e8\u5168\u5c40\u53d8\u91cf // Forward declarations extern int g_y ; // \u524d\u5411\u58f0\u660e\u975e\u5e38\u6570\u7684\u5168\u5c40\u53d8\u91cf extern const int g_y ; // \u524d\u5411\u58f0\u660e const \u7684\u5168\u5c40\u53d8\u91cf extern constexpr int g_y ; // \u4e0d\u5141\u8bb8\u8fd9\u6837\u505a\uff1aconstexpr \u53d8\u91cf\u4e0d\u80fd\u524d\u5411\u58f0\u660e \u6211\u4eec\u4f1a\u5728 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 \u4e2d\u5bf9\u76f8\u5173\u5185\u5bb9\u8fdb\u884c\u66f4\u8be6\u7ec6\u7684\u603b\u7ed3\u3002","title":"\u5c0f\u7ed3"},{"location":"6-8-Why-non-const-global-variables-are-evil/","tags":["global variable"],"text":"Key Takeaway \u975e const \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u88ab\u6240\u6709\u51fd\u6570\u4fee\u6539\uff0c\u56e0\u6b64\u4f1a\u8ba9\u7a0b\u5e8f\u53d8\u5f97\u96be\u4ee5\u9884\u6599\u3002 \u9759\u6001\u53d8\u91cf\u521d\u59cb\u5316\u6709\u4e24\u4e2a\u9636\u6bb5 \u7b2c\u4e00\u9636\u6bb5\u79f0\u4e3a\u9759\u6001\u521d\u59cb\u5316 \u3002\u5728\u9759\u6001\u521d\u59cb\u5316\u9636\u6bb5\uff0cconstexpr \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\uff08\u5305\u62ec\u5b57\u9762\u91cf\uff09\u90fd\u4f1a\u88ab\u521d\u59cb\u5316\u6210\u5177\u4f53\u7684\u503c\u3002\u6b64\u5916\uff0c\u6ca1\u6709\u663e\u5f0f\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\u4e5f\u4f1a\u88ab\u521d\u59cb\u5316\u4e3a0\u3002 -\u7b2c\u4e8c\u9636\u6bb5\u79f0\u4e3a\u52a8\u6001\u521d\u59cb\u5316\u3002\u8fd9\u4e2a\u9636\u6bb5\u8981\u590d\u6742\u7684\u591a\uff0c\u4f46\u662f\u5b83\u7684\u7cbe\u9ad3\u5728\u4e8e\u5177\u6709\u975e constexpr \u521d\u59cb\u5316\u503c \u7684\u5168\u5c40\u53d8\u91cf\u4f1a\u88ab\u521d\u59cb\u5316\u3002 \u5168\u5c40\u53d8\u91cf\u7684\u52a8\u6001\u521d\u59cb\u5316\u4f1a\u9020\u6210\u5f88\u5927\u95ee\u9898\uff0c\u5c3d\u91cf\u907f\u514d\u52a8\u6001\u521d\u59cb\u5316\u3002 \u5982\u679c\u4f60\u5411\u7f16\u7a0b\u5927\u4f6c\u8ba8\u6559\u4e00\u6761\u7f16\u7a0b\u5b9e\u8df5\u7684\u5efa\u8bae\uff0c\u5f88\u591a\u4eba\u90fd\u4f1a\u5728\u7a0d\u52a0\u601d\u8003\u540e\u544a\u8bc9\u4f60\uff1a\u201c\u907f\u514d\u5168\u5c40\u53d8\u91cf\uff01\u201d\u3002\u8fd9\u662f\u56e0\u4e3a\u5168\u5c40\u53d8\u91cf\u662f\u7f16\u7a0b\u8bed\u8a00\u4e2d\u6700\u88ab\u6ee5\u7528\u7684\u6982\u5ff5\u3002\u5c3d\u7ba1\u5728\u4e00\u4e9b\u7f16\u7a0b\u5c0f\u7ec3\u4e60\u4e2d\uff0c\u5168\u5c40\u53d8\u91cf\u770b\u8d77\u6765\u4eba\u755c\u65e0\u5bb3\uff0c\u4f46\u662f\u5230\u4e86\u5927\u578b\u7a0b\u5e8f\u4e2d\u5c31\u975e\u5e38\u5bb9\u6613\u5bfc\u81f4\u95ee\u9898\u3002 \u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u60f3\u8981\u4f7f\u7528\u5927\u91cf\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u56e0\u4e3a\u5168\u5c40\u53d8\u91cf\u7528\u8d77\u6765\u5f88\u65b9\u4fbf\uff0c\u5c24\u5176\u662f\u8bb8\u591a\u7a0b\u5e8f\u90fd\u9700\u8981\u8fd9\u4e9b\u53d8\u91cf\u7684\u65f6\u5019\uff08\u4f5c\u4e3a\u53c2\u6570\u4f7f\u7528\u7684\u8bdd\uff0c\u4f20\u53c2\u4f1a\u975e\u5e38\u75db\u82e6\uff09\u3002\u4e0d\u8fc7\uff0c\u8fd9\u5e76\u4e0d\u662f\u4e2a\u597d\u4e3b\u610f\u3002\u5f88\u591a\u7a0b\u5e8f\u5458\u90fd\u8ba4\u4e3a\u5e94\u8be5\u575a\u51b3\u907f\u514d\u975e const \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\uff01 \u5728\u6211\u4eec\u6df1\u7a76\u5176\u539f\u56e0\u4e4b\u524d\uff0c\u9996\u5148\u6f84\u6e05\u4e00\u4e2a\u95ee\u9898\u3002\u5f53\u6211\u4eec\u8c08\u8bba\u201c\u5168\u5c40\u662f\u9b54\u9b3c\u201d\u7684\u65f6\u5019\uff0c\u5e76\u4e0d\u662f\u8bf4 \u6240\u6709 \u7684\u5168\u5c40\u53d8\u91cf\u90fd\u662f\u9b54\u9b3c\u3002\u591a\u6570\u60c5\u51b5\u4e0b\u6211\u4eec\u6307\u7684\u662f\u975e const \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u3002 \u4e3a\u4ec0\u4e48\u975e const \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c \u5230\u76ee\u524d\u4f4d\u7f6e\uff0c\u975e const \u7c7b\u578b\u5168\u5c40\u53d8\u91cf\u5371\u9669\u7684\u6700\u5927\u539f\u56e0\uff0c\u662f\u56e0\u4e3a\u5b83\u7684\u503c\u53ef\u4ee5\u88ab\u4efb\u4f55\u51fd\u6570\u4fee\u6539\uff0c\u540c\u65f6\u4e5f\u6ca1\u6709\u7b80\u5355\u7684\u529e\u6cd5\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u5458\u6e05\u695a\u5730\u4e86\u89e3\u5230\u53d1\u751f\u4e86\u4ec0\u4e48\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 int g_mode ; // \u58f0\u660e\u5168\u5c40\u53d8\u91cf (\u9ed8\u8ba4\u4f1a\u521d\u59cb\u5316\u4e3a0) void doSomething () { g_mode = 2 ; // \u5c06\u5168\u5c40\u53d8\u91cf\u8bbe\u7f6e\u4e3a 2 } int main () { g_mode = 1 ; // \u6ce8\u610f\uff1a\u8fd9\u91cc\u53c8\u5c06\u5168\u5c40\u53d8\u91cf g_mode \u53d8\u91cf\u8bbe\u7f6e\u4e3a 1\uff0c\u800c\u5e76\u6ca1\u6709\u58f0\u660e\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf g_mode doSomething (); // \u7a0b\u5e8f\u5458\u6b64\u65f6\u8fd8\u4ee5\u4e3a g_mode \u662f 1 // \u4f46\u662f doSomething \u5df2\u7ecf\u5c06\u5b83\u4fee\u6539\u4e3a 2 \u4e86\u3002 if ( g_mode == 1 ) std :: cout << \"No threat detected. \\n \" ; else std :: cout << \"Launching nuclear missiles... \\n \" ; return 0 ; } \u6ce8\u610f\uff0c\u7a0b\u5e8f\u5458\u9996\u5148\u5c06\u53d8\u91cf g_mode \u8bbe\u7f6e\u4e3a\u4e86 1 \uff0c\u7136\u540e\u5c31\u8c03\u7528\u4e86 doSomething() \u3002\u9664\u975e\u7a0b\u5e8f\u5458\u660e\u786e\u77e5\u9053 doSomething() \u5185\u90e8\u4f1a\u4fee\u6539 g_mode \u7684\u503c\uff0c\u5426\u5219\u4ed6\u53ef\u80fd\u5e76\u4e0d\u4f1a\u60f3\u5230 doSomething() \u4f1a\u4fee\u6539 g_mode \u7684\u503c\uff01\u56e0\u6b64\uff0c\u63a5\u4e0b\u6765 main() \u6240\u505a\u7684\u4e8b\u60c5\u5c31\u4e0d\u50cf\u6211\u4eec\u671f\u671b\u7684\u90a3\u6837\u4e86\u3002 \u7b80\u5355\u6765\u8bf4\uff0c\u5168\u5c40\u53d8\u91cf\u4f1a\u8ba9\u7a0b\u5e8f\u96be\u4ee5\u9884\u6599\u3002 \u7a0b\u5e8f\u4e2d\u7684\u6bcf\u4e2a\u51fd\u6570\u90fd\u53ef\u80fd\u5f88\u5371\u9669\uff0c\u800c\u4e14\u7a0b\u5e8f\u5458\u901a\u5e38\u5e76\u6ca1\u6709\u7b80\u5355\u7684\u529e\u6cd5\u53ef\u4ee5\u5224\u65ad\u54ea\u4e2a\u51fd\u6570\u5371\u9669\u54ea\u4e2a\u4e0d\u5371\u9669\u3002\u5c40\u90e8\u53d8\u91cf\u5c31\u5b89\u5168\u7684\u591a\uff0c\u56e0\u4e3a\u5176\u4ed6\u51fd\u6570\u6ca1\u6709\u529e\u6cd5\u76f4\u63a5\u4fee\u6539\u5b83\u3002 \u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u6709\u5f88\u591a\u5176\u4ed6\u4e0d\u4f7f\u7528\u975econst\u5168\u5c40\u53d8\u91cf\u7684\u7406\u7531\u3002 \u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u7684\u7a0b\u5e8f\u4e2d\uff0c\u4e0b\u9762\u8fd9\u6837\u7684\u51fd\u6570\u5e76\u4e0d\u5c11\u89c1\uff1a 1 2 3 4 5 6 void someFunction () { // useful code if ( g_mode == 4 ) // do something good } \u5f53\u7a0b\u5e8f\u51fa\u73b0\u95ee\u9898\u65f6\uff0c\u4f60\u901a\u8fc7\u8c03\u8bd5\u786e\u5b9a\u4e86\u95ee\u9898\u7684\u539f\u56e0\u662f\u56e0\u4e3a g_mode \u7684\u503c\u662f 3 \u800c\u4e0d\u662f 4\u3002\u6b64\u65f6\u4f60\u5e94\u8be5\u5982\u4f55\u4fee\u590d\u8fd9\u4e2a\u95ee\u9898\u5462\uff1f\u4e3a\u4e86\u4fee\u590d\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f60\u5fc5\u987b\u627e\u5230 g_mode \u53ef\u80fd\u88ab\u8bbe\u7f6e\u4e3a 3 \u7684\u6240\u6709\u7684\u5730\u65b9\uff0c\u7136\u540e\u8ffd\u8e2a\u4ee3\u7801\u8fd0\u884c\uff0c\u5224\u65ad\u5b83\u662f\u4ec0\u4e48\u65f6\u5019\u88ab\u4fee\u6539\u4e3a 3 \u7684\u3002\u800c\u8fd9\u91cc\u9762\u53ef\u80fd\u4f1a\u6d89\u53ca\u5230\u5f88\u591a\u5b8c\u5168\u4e0d\u76f8\u5173\u7684\u4ee3\u7801\u3002 \u4e4b\u524d\u6211\u4eec\u8bf4\u8fc7\uff0c\u5c40\u90e8\u53d8\u91cf\u7684\u58f0\u660e\u5e94\u8be5\u5728\u6700\u63a5\u8fd1\u4f7f\u7528\u5b83\u7684\u5730\u65b9\uff0c\u56e0\u4e3a\u8fd9\u4e48\u505a\u53ef\u4ee5\u5728\u51fa\u73b0\u95ee\u9898\u65f6\uff0c\u6700\u5927\u7a0b\u5ea6\u51cf\u5c11\u4f60\u9700\u8981\u5206\u6790\u7684\u4ee3\u7801\u3002\u5168\u5c40\u53d8\u91cf\u5219\u6b63\u53f7\u76f8\u53cd\u2014\u2014\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u88ab\u4f7f\u7528\uff0c\u6240\u4ee5\u4f60\u5fc5\u987b\u9605\u8bfb\u5168\u90e8\u7684\u4ee3\u7801\u624d\u80fd\u591f\u4e86\u89e3\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u5728\u7b80\u77ed\u7684\u7a0b\u5e8f\u4e2d\u8fd9\u4e5f\u8bb8\u5e76\u4e0d\u662f\u95ee\u9898\uff0c\u4f46\u662f\u5728\u5927\u578b\u7a0b\u5e8f\u4e2d\uff0c\u8fd9\u5c31\u5f88\u9ebb\u70e6\u4e86\u3002 \u4f8b\u5982\uff0c\u4f60\u7684\u7a0b\u5e8f\u53ef\u80fd\u6709 442 \u5904\u4f7f\u7528\u4e86 g_mode \u7684\u5730\u65b9\u3002\u9664\u975e\u6709\u5f88\u597d\u7684\u6587\u6863\u8bf4\u660e\uff0c\u4e0d\u7136\u4f60\u53ef\u80fd\u9700\u8981\u7406\u89e3\u6bcf\u4e00\u5904\u4f7f\u7528\u4e86 g_mode \u7684\u4ee3\u7801\uff0c\u5b83\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u7684\u662f\u5982\u4f55\u4f7f\u7528\u7684\u3001\u6709\u6548\u503c\u662f\u591a\u5c11\u3001\u4e3b\u8981\u529f\u80fd\u662f\u4ec0\u4e48\u3002 \u5168\u5c40\u53d8\u91cf\u8fd8\u4f1a\u7834\u574f\u7a0b\u5e8f\u7684\u6a21\u5757\u6027\u548c\u7075\u6d3b\u6027\u3002\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u503c\u4f7f\u7528\u5176\u53c2\u6570\u4e14\u4e0d\u5177\u6709\u526f\u4f5c\u7528\uff0c\u90a3\u4e48\u5b83\u662f\u5b8c\u5168\u6a21\u5757\u5316\u7684\u3002\u6a21\u5757\u6027\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u7a0b\u5e8f\uff0c\u540c\u65f6\u4e5f\u80fd\u591f\u589e\u5f3a\u7a0b\u5e8f\u7684\u53ef\u590d\u7528\u6027\u3002\u5168\u5c40\u53d8\u91cf\u4f1a\u6781\u5927\u5730\u964d\u4f4e\u7a0b\u5e8f\u7684\u6a21\u5757\u6027\u3002 \u7279\u522b\u5730\uff0c\u907f\u514d\u5c06\u201c\u51b3\u7b56\u70b9\u201d\u53d8\u91cf\uff08\u4f8b\u5982\u6761\u4ef6\u8bed\u53e5\u4e2d\u7684\u53d8\u91cf\uff0c\u5c31\u50cf\u4e0a\u6587\u4e2d\u7684 g_mode \uff09\u5b9a\u4e49\u4e3a\u5168\u5c40\u53d8\u91cf\u3002\u5982\u679c\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u4ec5\u4ec5\u5305\u542b\u67d0\u4e2a\u6570\u636e\uff0c\u4e5f\u8bb8\u5e76\u4e0d\u4f1a\u8ba9\u4f60\u7684\u7a0b\u5e8f\u88ab\u7834\u574f\u3002\u800c\u5982\u679c\u8be5\u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u51b3\u5b9a\u7a0b\u5e8f \u5982\u4f55 \u8fd0\u884c\uff0c\u90a3\u4e48\u5b83\u9020\u6210\u95ee\u9898\u7684\u53ef\u80fd\u6027\u5c31\u4f1a\u5927\u5927\u63d0\u9ad8\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u53ef\u80fd\uff0c\u5c3d\u91cf\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\u800c\u4e0d\u662f\u5168\u5c40\u53d8\u91cf\u3002 \u5168\u5c40\u53d8\u91cf\u7684\u521d\u59cb\u5316\u987a\u5e8f\u95ee\u9898 \u9759\u6001\u53d8\u91cf\u7684\u521d\u59cb\u5316\uff08\u5176\u4e2d\u5305\u542b\u5168\u5c40\u53d8\u91cf\uff09\u662f\u7a0b\u5e8f\u542f\u52a8\u7684\u4e00\u90e8\u5206\uff0c\u5728\u6267\u884c main \u51fd\u6570\u524d\u5c31\u4f1a\u8fdb\u884c\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u6709\u4e24\u4e2a\u9636\u6bb5\uff1a \u7b2c\u4e00\u9636\u6bb5\u79f0\u4e3a \u9759\u6001\u521d\u59cb\u5316(static initialization) \u3002\u5728\u9759\u6001\u521d\u59cb\u5316\u9636\u6bb5\uff0cconstexpr \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\uff08\u5305\u62ec\u5b57\u9762\u91cf\uff09\u90fd\u4f1a\u88ab\u521d\u59cb\u5316\u6210\u5177\u4f53\u7684\u503c\u3002\u6b64\u5916\uff0c\u6ca1\u6709\u663e\u5f0f\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\u4e5f\u4f1a\u88ab\u521d\u59cb\u5316\u4e3a0\u3002 \u7b2c\u4e8c\u9636\u6bb5\u79f0\u4e3a \u52a8\u6001\u521d\u59cb\u5316(dynamic initialization) \u3002\u8fd9\u4e2a\u9636\u6bb5\u8981\u590d\u6742\u7684\u591a\uff0c\u4f46\u662f\u5b83\u7684\u7cbe\u9ad3\u5728\u4e8e\u5177\u6709\u975e constexpr \u521d\u59cb\u5316\u503c \u7684\u5168\u5c40\u53d8\u91cf\u4f1a\u88ab\u521d\u59cb\u5316\u3002 \u975e constexpr \u521d\u59cb\u5316\u503c \u7684\u4f8b\u5b50\u5982\u4e0b\uff1a 1 2 3 4 5 6 int init () { return 5 ; } int g_something { init () }; // \u975e constexpr \u521d\u59cb\u5316 \u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u5168\u5c40\u53d8\u91cf\u7684\u521d\u59cb\u5316\u987a\u5e8f\u548c\u5b83\u4eec\u88ab\u5b9a\u4e49\u7684\u987a\u5e8f\u4e00\u822c\u662f\u4e00\u81f4\u7684\uff08\u4e5f\u6709\u4e00\u4e9b\u4f8b\u5916\u89c4\u5219\uff09\u3002\u8003\u8651\u5230\u8fd9\u4e00\u70b9\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u53d8\u91cf\u7684\u521d\u59cb\u5316\u503c\u4e0d\u4f9d\u8d56\u4e8e\u540e\u9762\u624d\u521d\u59cb\u5316\u7684\u53d8\u91cf\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int initx (); // forward declaration int inity (); // forward declaration int g_x { initx () }; // g_x \u9996\u5148\u521d\u59cb\u5316 int g_y { inity () }; int initx () { return g_y ; // \u51fd\u6570\u8c03\u7528\u65f6\uff0cg_y \u5e76\u6ca1\u6709\u521d\u59cb\u5316 } int inity () { return 5 ; } int main () { std :: cout << g_x << ' ' << g_y << '\\n' ; } \u6253\u5370\u7ed3\u679c\uff1a 1 0 5 \u66f4\u4e25\u91cd\u7684\u95ee\u9898\u662f\uff0c\u4e0d\u540c\u6587\u4ef6\u4e2d\u5168\u5c40\u53d8\u91cf\u7684\u521d\u59cb\u5316\u987a\u5e8f\u662f\u4e0d\u786e\u5b9a\u7684\u3002\u7ed9\u5b9a\u4e24\u4e2a\u6587\u4ef6 a.cpp \u548c b.cpp \uff0c\u6bcf\u4e2a\u6587\u4ef6\u4e2d\u7684\u5168\u5c40\u53d8\u91cf\u90fd\u53ef\u80fd\u9996\u5148\u521d\u59cb\u5316\u3002\u8fd9\u4e5f\u610f\u5473\u7740\uff0c\u5982\u679c\u6587\u4ef6 a.cpp \u4e2d\u7684\u53d8\u91cf\u4f9d\u8d56 b.cpp \u6587\u4ef6\u4e2d\u7684\u53d8\u91cf\uff0c\u90a3\u4e48\u6709 50%\u7684\u51e0\u7387\u4f1a\u9047\u5230\u53d8\u91cf\u672a\u521d\u59cb\u5316\u7684\u60c5\u51b5\u3002 \u6ce8\u610f \u5168\u5c40\u53d8\u91cf\u7684\u52a8\u6001\u521d\u59cb\u5316\u4f1a\u9020\u6210\u5f88\u5927\u95ee\u9898\uff0c\u5c3d\u91cf\u907f\u514d\u52a8\u6001\u521d\u59cb\u5316\u3002 \u4ec0\u4e48\u65f6\u5019\u53ef\u4ee5\u5408\u7406\u4f7f\u7528\u975e const \u7684\u5168\u5c40\u53d8\u91cf \u8fd9\u79cd\u673a\u4f1a\u5e76\u4e0d\u5927\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u90fd\u53ef\u4ee5\u907f\u514d\u4f7f\u7528\u975e const \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u3002\u4f46\u662f\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5ba1\u614e\u5730\u4f7f\u7528\u975e const \u5168\u5c40\u53d8\u91cf\u53cd\u5230\u53ef\u4ee5\u964d\u4f4e\u7a0b\u5e8f\u7684\u590d\u6742\u5ea6\u3002 \u65e5\u5fd7\u6587\u4ef6\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u4f8b\u5b50\uff0c\u5728\u65e5\u5fd7\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u4f1a\u5b58\u653e\u9519\u8bef\u4fe1\u606f\u548c\u8c03\u8bd5\u4fe1\u606f\uff0c\u56e0\u6b64\u5c06\u5176\u5b9a\u4e49\u4e3a\u5168\u5c40\u53d8\u91cf\u662f\u5408\u7406\u7684\uff0c\u56e0\u4e3a\u4e00\u4e2a\u7a0b\u5e8f\u901a\u5e38\u53ea\u4f1a\u6709\u4e00\u4e2a\u65e5\u5fd7\uff0c\u5e76\u4e14\u5b83\u53ef\u80fd\u5728\u7a0b\u5e8f\u4e2d\u7684\u4efb\u4f55\u90e8\u5206\u88ab\u4f7f\u7528\u3002 \u4e0d\u7ba1\u600e\u4e48\u8bf4\uff0c std::cout \u548c std::cin \u5bf9\u8c61\u4e5f\u88ab\u5b9a\u4e49\u6210\u4e86\u5168\u5c40\u53d8\u91cf(\u5728 std \u547d\u540d\u7a7a\u95f4\u4e2d)\u3002 \u4e00\u822c\u8bf4\u6765\uff0c\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u81f3\u5c11\u8981\u6ee1\u8db3\u4e0b\u9762\u4e24\u4e2a\u6761\u4ef6\uff1a\u8be5\u53d8\u91cf\u5728\u7a0b\u5e8f\u4e2d\u7684\u529f\u80fd\u662f\u552f\u4e00\u7684\uff0c\u800c\u4e14\u5b83\u4f1a\u5728\u7a0b\u5e8f\u7684\u5404\u4e2a\u5730\u65b9\u88ab\u4f7f\u7528\u3002 \u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u4f1a\u9519\u8bef\u5730\u8ba4\u4e3a\uff0c\u56e0\u4e3a\u73b0\u5728\u53ea\u9700\u8981\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u6240\u4ee5\u7528\u7528\u4e5f\u6ca1\u5173\u7cfb\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u4f60\u73b0\u5728\u53ef\u80fd\u5728\u5f00\u53d1\u4e00\u4e2a\u5355\u4eba\u6e38\u620f\uff0c\u6240\u4ee5\u53ea\u9700\u8981\u4e00\u4e2a\u7528\u6237\u3002\u4f46\u662f\u4e07\u4e00\u4ee5\u540e\u4f60\u9700\u8981\u4e3a\u5b83\u6dfb\u52a0\u591a\u4eba\u6a21\u5f0f\u5462\uff1f \u6709\u5173\u5168\u5c40\u53d8\u91cf\u7684\u5fe0\u544a \u5982\u679c\u4f60\u6709\u8db3\u591f\u7684\u7406\u7531\u800c\u4f7f\u7528\u4e86\u4e00\u4e2a\u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u8fd9\u91cc\u6709\u4e00\u4e9b\u5fe0\u544a\u5e0c\u671b\u80fd\u591f\u5e2e\u4f60\u5c3d\u53ef\u80fd\u5730\u907f\u514d\u95ee\u9898\u3002\u5f53\u7136\uff0c\u8fd9\u4e9b\u5fe0\u544a\u4e0d\u4ec5\u4ec5\u53ea\u9488\u5bf9\u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u5b83\u5bf9\u6240\u6709\u7684\u5168\u5c40\u53d8\u91cf\u90fd\u662f\u6709\u610f\u4e49\u7684\u3002 \u9996\u5148\uff0c\u8bf7\u4e3a\u4e0d\u5728\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u5168\u5c40\u53d8\u91cf\u6dfb\u52a0 \u201cg\u201d \u6216\u8005 \u201cg_\u201d \u524d\u7f00\uff0c\u6216\u8005\u5e72\u8106\u5c06\u5b83\u4eec\u653e\u5230\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\uff08\u5728 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u4e2d\u6709\u76f8\u5173\u8ba8\u8bba\uff09\u4ee5\u907f\u514d\u547d\u540d\u51b2\u7a81\u3002 \u4f8b\u5982\uff0c\u4e0d\u8981\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 constexpr double gravity { 9.8 }; // unclear if this is a local or global variable from the name int main () { return 0 ; } \u8981\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 namespace constants { constexpr double gravity { 9.8 }; } int main () { return 0 ; } \u5176\u6b21\uff0c\u4e0d\u8981\u5141\u8bb8\u5168\u5c40\u53d8\u91cf\u7684\u76f4\u63a5\u8bbf\u95ee\uff0c\u6700\u597d\u662f\u5c06\u5b83\u4eec\u201c\u5c01\u88c5\u201d\u8d77\u6765\u3002\u786e\u4fdd\u53d8\u91cf\u53ea\u80fd\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u6587\u4ef6\u5185\u90e8\u88ab\u8bbf\u95ee\uff0c\u4f8b\u5982\u4e3a\u53d8\u91cf\u6dfb\u52a0 static \u6216 const \u4fee\u9970\u7b26\uff0c\u7136\u540e\u63d0\u4f9b\u4e00\u4e2a\u5916\u90e8\u7684\u5168\u5c40\u201c\u8bbf\u95ee\u51fd\u6570\u201d\u7528\u4e8e\u8be5\u53d8\u91cf\u7684\u8bbf\u95ee\u3002\u901a\u8fc7\u8fd9\u4e2a\u8bbf\u95ee\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u53d8\u91cf\u7684\u4f7f\u7528\u65b9\u5f0f\u8fdb\u884c\u7ba1\u7406\uff08\u4f8b\u5982\uff1a\u8f93\u5165\u6821\u9a8c\uff0c\u8303\u56f4\u68c0\u67e5\u7b49\u7b49\uff09\u3002\u6b64\u5916\uff0c\u5982\u679c\u4f60\u4ee5\u540e\u5e0c\u671b\u4fee\u6539\u5e95\u5c42\u5b9e\u73b0\uff08\u4f8b\u5982\u4ece\u4e00\u4e2a\u6570\u636e\u5e93\u8fc1\u79fb\u5230\u53e6\u5916\u4e00\u4e2a\u6570\u636e\u5e93\u65f6\uff09\uff0c\u4f60\u53ea\u9700\u4fee\u6539\u8fd9\u4e2a\u8bbf\u95ee\u51fd\u6570\uff0c\u4f7f\u5176\u8bbf\u95ee\u65b0\u7684\u53d8\u91cf\u5373\u53ef\uff0c\u800c\u65e0\u9700\u66ff\u6362\u6e90\u7801\u4e2d\u6bcf\u5904\u4f7f\u7528\u8be5\u5168\u5c40\u53d8\u91cf\u7684\u5730\u3002 \u4f8b\u5982\uff0c\u4e0d\u8981\u8fd9\u4e48\u505a\uff1a 1 2 3 4 namespace constants { extern const double gravity { 9.8 }; // has external linkage, is directly accessible by other files } \u8981\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 namespace constants { constexpr double gravity { 9.8 }; // has internal linkage, is accessible only by this file } double getGravity () // this function can be exported to other files to access the global outside of this file { // We could add logic here if needed later // or change the implementation transparently to the callers return constants :: gravity ; } \u4e00\u4e2a\u63d0\u9192 \u5168\u5c40 const \u53d8\u91cf\u9ed8\u8ba4\u5177\u6709\u5185\u90e8\u94fe\u63a5\u5c5e\u6027\uff0c\u6240\u4ee5 gravity \u5e76\u4e0d\u9700\u8981\u88ab\u5b9a\u4e49\u4e3a static \u3002 \u7b2c\u4e09\uff0c\u5f53\u51fd\u6570\u5fc5\u987b\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u4e0d\u8981\u76f4\u63a5\u5728\u51fd\u6570\u4f53\u4e2d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3002\u5e94\u8be5\u5c06\u5168\u5c40\u53d8\u91cf\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u8fd9\u6837\u7684\u8bdd\uff0c\u4e0d\u7ba1\u4ec0\u4e48\u65f6\u5019\u4f60\u9700\u8981\u4f7f\u7528\u4e0d\u540c\u7684\u503c\u65f6\uff0c\u4f60\u53ea\u9700\u8981\u4fee\u6539\u51fd\u6570\u7684\u5165\u53c2\u5c31\u53ef\u4ee5\uff0c\u8fd9\u4e48\u505a\u6709\u52a9\u4e8e\u4fdd\u6301\u51fd\u6570\u7684\u6a21\u5757\u5316\u3002 \u4e0d\u8981\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include namespace constants { constexpr double gravity { 9.8 }; } // \u8fd9\u4e2a\u51fd\u6570\u53ea\u80fd\u57fa\u4e8e\u5168\u5c40 gravity \u8ba1\u7b97\u901f\u5ea6 double instantVelocity ( int time ) { return constants :: gravity * time ; } int main () { std :: cout << instantVelocity ( 5 ); } \u8981\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include namespace constants { constexpr double gravity { 9.8 }; } // \u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u57fa\u4e8e\u4efb\u4f55 gravity \u503c\u8ba1\u7b97\u901f\u5ea6\uff08\u66f4\u6709\u7528\uff09 double instantVelocity ( int time , double gravity ) { return gravity * time ; } int main () { std :: cout << instantVelocity ( 5 , constants :: gravity ); // \u5c06\u5e38\u91cf\u5f53\u505a\u53c2\u6570\u4f20\u5165 } \u4e00\u4e2a\u7b11\u8bdd \u95ee\uff1a\u5168\u5c40\u53d8\u91cf\u548c\u4ec0\u4e48\u524d\u7f00\u6700\u914d\uff1f \u7b54: // \uff08\u6ce8\u91ca\uff09 C++ \u7b11\u8bdd\u6700\u68d2\u4e86\uff01","title":"6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c"},{"location":"6-8-Why-non-const-global-variables-are-evil/#const","text":"\u5230\u76ee\u524d\u4f4d\u7f6e\uff0c\u975e const \u7c7b\u578b\u5168\u5c40\u53d8\u91cf\u5371\u9669\u7684\u6700\u5927\u539f\u56e0\uff0c\u662f\u56e0\u4e3a\u5b83\u7684\u503c\u53ef\u4ee5\u88ab\u4efb\u4f55\u51fd\u6570\u4fee\u6539\uff0c\u540c\u65f6\u4e5f\u6ca1\u6709\u7b80\u5355\u7684\u529e\u6cd5\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u5458\u6e05\u695a\u5730\u4e86\u89e3\u5230\u53d1\u751f\u4e86\u4ec0\u4e48\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 int g_mode ; // \u58f0\u660e\u5168\u5c40\u53d8\u91cf (\u9ed8\u8ba4\u4f1a\u521d\u59cb\u5316\u4e3a0) void doSomething () { g_mode = 2 ; // \u5c06\u5168\u5c40\u53d8\u91cf\u8bbe\u7f6e\u4e3a 2 } int main () { g_mode = 1 ; // \u6ce8\u610f\uff1a\u8fd9\u91cc\u53c8\u5c06\u5168\u5c40\u53d8\u91cf g_mode \u53d8\u91cf\u8bbe\u7f6e\u4e3a 1\uff0c\u800c\u5e76\u6ca1\u6709\u58f0\u660e\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf g_mode doSomething (); // \u7a0b\u5e8f\u5458\u6b64\u65f6\u8fd8\u4ee5\u4e3a g_mode \u662f 1 // \u4f46\u662f doSomething \u5df2\u7ecf\u5c06\u5b83\u4fee\u6539\u4e3a 2 \u4e86\u3002 if ( g_mode == 1 ) std :: cout << \"No threat detected. \\n \" ; else std :: cout << \"Launching nuclear missiles... \\n \" ; return 0 ; } \u6ce8\u610f\uff0c\u7a0b\u5e8f\u5458\u9996\u5148\u5c06\u53d8\u91cf g_mode \u8bbe\u7f6e\u4e3a\u4e86 1 \uff0c\u7136\u540e\u5c31\u8c03\u7528\u4e86 doSomething() \u3002\u9664\u975e\u7a0b\u5e8f\u5458\u660e\u786e\u77e5\u9053 doSomething() \u5185\u90e8\u4f1a\u4fee\u6539 g_mode \u7684\u503c\uff0c\u5426\u5219\u4ed6\u53ef\u80fd\u5e76\u4e0d\u4f1a\u60f3\u5230 doSomething() \u4f1a\u4fee\u6539 g_mode \u7684\u503c\uff01\u56e0\u6b64\uff0c\u63a5\u4e0b\u6765 main() \u6240\u505a\u7684\u4e8b\u60c5\u5c31\u4e0d\u50cf\u6211\u4eec\u671f\u671b\u7684\u90a3\u6837\u4e86\u3002 \u7b80\u5355\u6765\u8bf4\uff0c\u5168\u5c40\u53d8\u91cf\u4f1a\u8ba9\u7a0b\u5e8f\u96be\u4ee5\u9884\u6599\u3002 \u7a0b\u5e8f\u4e2d\u7684\u6bcf\u4e2a\u51fd\u6570\u90fd\u53ef\u80fd\u5f88\u5371\u9669\uff0c\u800c\u4e14\u7a0b\u5e8f\u5458\u901a\u5e38\u5e76\u6ca1\u6709\u7b80\u5355\u7684\u529e\u6cd5\u53ef\u4ee5\u5224\u65ad\u54ea\u4e2a\u51fd\u6570\u5371\u9669\u54ea\u4e2a\u4e0d\u5371\u9669\u3002\u5c40\u90e8\u53d8\u91cf\u5c31\u5b89\u5168\u7684\u591a\uff0c\u56e0\u4e3a\u5176\u4ed6\u51fd\u6570\u6ca1\u6709\u529e\u6cd5\u76f4\u63a5\u4fee\u6539\u5b83\u3002 \u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u6709\u5f88\u591a\u5176\u4ed6\u4e0d\u4f7f\u7528\u975econst\u5168\u5c40\u53d8\u91cf\u7684\u7406\u7531\u3002 \u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u7684\u7a0b\u5e8f\u4e2d\uff0c\u4e0b\u9762\u8fd9\u6837\u7684\u51fd\u6570\u5e76\u4e0d\u5c11\u89c1\uff1a 1 2 3 4 5 6 void someFunction () { // useful code if ( g_mode == 4 ) // do something good } \u5f53\u7a0b\u5e8f\u51fa\u73b0\u95ee\u9898\u65f6\uff0c\u4f60\u901a\u8fc7\u8c03\u8bd5\u786e\u5b9a\u4e86\u95ee\u9898\u7684\u539f\u56e0\u662f\u56e0\u4e3a g_mode \u7684\u503c\u662f 3 \u800c\u4e0d\u662f 4\u3002\u6b64\u65f6\u4f60\u5e94\u8be5\u5982\u4f55\u4fee\u590d\u8fd9\u4e2a\u95ee\u9898\u5462\uff1f\u4e3a\u4e86\u4fee\u590d\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f60\u5fc5\u987b\u627e\u5230 g_mode \u53ef\u80fd\u88ab\u8bbe\u7f6e\u4e3a 3 \u7684\u6240\u6709\u7684\u5730\u65b9\uff0c\u7136\u540e\u8ffd\u8e2a\u4ee3\u7801\u8fd0\u884c\uff0c\u5224\u65ad\u5b83\u662f\u4ec0\u4e48\u65f6\u5019\u88ab\u4fee\u6539\u4e3a 3 \u7684\u3002\u800c\u8fd9\u91cc\u9762\u53ef\u80fd\u4f1a\u6d89\u53ca\u5230\u5f88\u591a\u5b8c\u5168\u4e0d\u76f8\u5173\u7684\u4ee3\u7801\u3002 \u4e4b\u524d\u6211\u4eec\u8bf4\u8fc7\uff0c\u5c40\u90e8\u53d8\u91cf\u7684\u58f0\u660e\u5e94\u8be5\u5728\u6700\u63a5\u8fd1\u4f7f\u7528\u5b83\u7684\u5730\u65b9\uff0c\u56e0\u4e3a\u8fd9\u4e48\u505a\u53ef\u4ee5\u5728\u51fa\u73b0\u95ee\u9898\u65f6\uff0c\u6700\u5927\u7a0b\u5ea6\u51cf\u5c11\u4f60\u9700\u8981\u5206\u6790\u7684\u4ee3\u7801\u3002\u5168\u5c40\u53d8\u91cf\u5219\u6b63\u53f7\u76f8\u53cd\u2014\u2014\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u88ab\u4f7f\u7528\uff0c\u6240\u4ee5\u4f60\u5fc5\u987b\u9605\u8bfb\u5168\u90e8\u7684\u4ee3\u7801\u624d\u80fd\u591f\u4e86\u89e3\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u5728\u7b80\u77ed\u7684\u7a0b\u5e8f\u4e2d\u8fd9\u4e5f\u8bb8\u5e76\u4e0d\u662f\u95ee\u9898\uff0c\u4f46\u662f\u5728\u5927\u578b\u7a0b\u5e8f\u4e2d\uff0c\u8fd9\u5c31\u5f88\u9ebb\u70e6\u4e86\u3002 \u4f8b\u5982\uff0c\u4f60\u7684\u7a0b\u5e8f\u53ef\u80fd\u6709 442 \u5904\u4f7f\u7528\u4e86 g_mode \u7684\u5730\u65b9\u3002\u9664\u975e\u6709\u5f88\u597d\u7684\u6587\u6863\u8bf4\u660e\uff0c\u4e0d\u7136\u4f60\u53ef\u80fd\u9700\u8981\u7406\u89e3\u6bcf\u4e00\u5904\u4f7f\u7528\u4e86 g_mode \u7684\u4ee3\u7801\uff0c\u5b83\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u7684\u662f\u5982\u4f55\u4f7f\u7528\u7684\u3001\u6709\u6548\u503c\u662f\u591a\u5c11\u3001\u4e3b\u8981\u529f\u80fd\u662f\u4ec0\u4e48\u3002 \u5168\u5c40\u53d8\u91cf\u8fd8\u4f1a\u7834\u574f\u7a0b\u5e8f\u7684\u6a21\u5757\u6027\u548c\u7075\u6d3b\u6027\u3002\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u503c\u4f7f\u7528\u5176\u53c2\u6570\u4e14\u4e0d\u5177\u6709\u526f\u4f5c\u7528\uff0c\u90a3\u4e48\u5b83\u662f\u5b8c\u5168\u6a21\u5757\u5316\u7684\u3002\u6a21\u5757\u6027\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u7a0b\u5e8f\uff0c\u540c\u65f6\u4e5f\u80fd\u591f\u589e\u5f3a\u7a0b\u5e8f\u7684\u53ef\u590d\u7528\u6027\u3002\u5168\u5c40\u53d8\u91cf\u4f1a\u6781\u5927\u5730\u964d\u4f4e\u7a0b\u5e8f\u7684\u6a21\u5757\u6027\u3002 \u7279\u522b\u5730\uff0c\u907f\u514d\u5c06\u201c\u51b3\u7b56\u70b9\u201d\u53d8\u91cf\uff08\u4f8b\u5982\u6761\u4ef6\u8bed\u53e5\u4e2d\u7684\u53d8\u91cf\uff0c\u5c31\u50cf\u4e0a\u6587\u4e2d\u7684 g_mode \uff09\u5b9a\u4e49\u4e3a\u5168\u5c40\u53d8\u91cf\u3002\u5982\u679c\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u4ec5\u4ec5\u5305\u542b\u67d0\u4e2a\u6570\u636e\uff0c\u4e5f\u8bb8\u5e76\u4e0d\u4f1a\u8ba9\u4f60\u7684\u7a0b\u5e8f\u88ab\u7834\u574f\u3002\u800c\u5982\u679c\u8be5\u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u51b3\u5b9a\u7a0b\u5e8f \u5982\u4f55 \u8fd0\u884c\uff0c\u90a3\u4e48\u5b83\u9020\u6210\u95ee\u9898\u7684\u53ef\u80fd\u6027\u5c31\u4f1a\u5927\u5927\u63d0\u9ad8\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u53ef\u80fd\uff0c\u5c3d\u91cf\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\u800c\u4e0d\u662f\u5168\u5c40\u53d8\u91cf\u3002","title":"\u4e3a\u4ec0\u4e48\u975e const \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c"},{"location":"6-8-Why-non-const-global-variables-are-evil/#_1","text":"\u9759\u6001\u53d8\u91cf\u7684\u521d\u59cb\u5316\uff08\u5176\u4e2d\u5305\u542b\u5168\u5c40\u53d8\u91cf\uff09\u662f\u7a0b\u5e8f\u542f\u52a8\u7684\u4e00\u90e8\u5206\uff0c\u5728\u6267\u884c main \u51fd\u6570\u524d\u5c31\u4f1a\u8fdb\u884c\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u6709\u4e24\u4e2a\u9636\u6bb5\uff1a \u7b2c\u4e00\u9636\u6bb5\u79f0\u4e3a \u9759\u6001\u521d\u59cb\u5316(static initialization) \u3002\u5728\u9759\u6001\u521d\u59cb\u5316\u9636\u6bb5\uff0cconstexpr \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\uff08\u5305\u62ec\u5b57\u9762\u91cf\uff09\u90fd\u4f1a\u88ab\u521d\u59cb\u5316\u6210\u5177\u4f53\u7684\u503c\u3002\u6b64\u5916\uff0c\u6ca1\u6709\u663e\u5f0f\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\u4e5f\u4f1a\u88ab\u521d\u59cb\u5316\u4e3a0\u3002 \u7b2c\u4e8c\u9636\u6bb5\u79f0\u4e3a \u52a8\u6001\u521d\u59cb\u5316(dynamic initialization) \u3002\u8fd9\u4e2a\u9636\u6bb5\u8981\u590d\u6742\u7684\u591a\uff0c\u4f46\u662f\u5b83\u7684\u7cbe\u9ad3\u5728\u4e8e\u5177\u6709\u975e constexpr \u521d\u59cb\u5316\u503c \u7684\u5168\u5c40\u53d8\u91cf\u4f1a\u88ab\u521d\u59cb\u5316\u3002 \u975e constexpr \u521d\u59cb\u5316\u503c \u7684\u4f8b\u5b50\u5982\u4e0b\uff1a 1 2 3 4 5 6 int init () { return 5 ; } int g_something { init () }; // \u975e constexpr \u521d\u59cb\u5316 \u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u5168\u5c40\u53d8\u91cf\u7684\u521d\u59cb\u5316\u987a\u5e8f\u548c\u5b83\u4eec\u88ab\u5b9a\u4e49\u7684\u987a\u5e8f\u4e00\u822c\u662f\u4e00\u81f4\u7684\uff08\u4e5f\u6709\u4e00\u4e9b\u4f8b\u5916\u89c4\u5219\uff09\u3002\u8003\u8651\u5230\u8fd9\u4e00\u70b9\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u53d8\u91cf\u7684\u521d\u59cb\u5316\u503c\u4e0d\u4f9d\u8d56\u4e8e\u540e\u9762\u624d\u521d\u59cb\u5316\u7684\u53d8\u91cf\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int initx (); // forward declaration int inity (); // forward declaration int g_x { initx () }; // g_x \u9996\u5148\u521d\u59cb\u5316 int g_y { inity () }; int initx () { return g_y ; // \u51fd\u6570\u8c03\u7528\u65f6\uff0cg_y \u5e76\u6ca1\u6709\u521d\u59cb\u5316 } int inity () { return 5 ; } int main () { std :: cout << g_x << ' ' << g_y << '\\n' ; } \u6253\u5370\u7ed3\u679c\uff1a 1 0 5 \u66f4\u4e25\u91cd\u7684\u95ee\u9898\u662f\uff0c\u4e0d\u540c\u6587\u4ef6\u4e2d\u5168\u5c40\u53d8\u91cf\u7684\u521d\u59cb\u5316\u987a\u5e8f\u662f\u4e0d\u786e\u5b9a\u7684\u3002\u7ed9\u5b9a\u4e24\u4e2a\u6587\u4ef6 a.cpp \u548c b.cpp \uff0c\u6bcf\u4e2a\u6587\u4ef6\u4e2d\u7684\u5168\u5c40\u53d8\u91cf\u90fd\u53ef\u80fd\u9996\u5148\u521d\u59cb\u5316\u3002\u8fd9\u4e5f\u610f\u5473\u7740\uff0c\u5982\u679c\u6587\u4ef6 a.cpp \u4e2d\u7684\u53d8\u91cf\u4f9d\u8d56 b.cpp \u6587\u4ef6\u4e2d\u7684\u53d8\u91cf\uff0c\u90a3\u4e48\u6709 50%\u7684\u51e0\u7387\u4f1a\u9047\u5230\u53d8\u91cf\u672a\u521d\u59cb\u5316\u7684\u60c5\u51b5\u3002 \u6ce8\u610f \u5168\u5c40\u53d8\u91cf\u7684\u52a8\u6001\u521d\u59cb\u5316\u4f1a\u9020\u6210\u5f88\u5927\u95ee\u9898\uff0c\u5c3d\u91cf\u907f\u514d\u52a8\u6001\u521d\u59cb\u5316\u3002","title":"\u5168\u5c40\u53d8\u91cf\u7684\u521d\u59cb\u5316\u987a\u5e8f\u95ee\u9898"},{"location":"6-8-Why-non-const-global-variables-are-evil/#const_1","text":"\u8fd9\u79cd\u673a\u4f1a\u5e76\u4e0d\u5927\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u90fd\u53ef\u4ee5\u907f\u514d\u4f7f\u7528\u975e const \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u3002\u4f46\u662f\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5ba1\u614e\u5730\u4f7f\u7528\u975e const \u5168\u5c40\u53d8\u91cf\u53cd\u5230\u53ef\u4ee5\u964d\u4f4e\u7a0b\u5e8f\u7684\u590d\u6742\u5ea6\u3002 \u65e5\u5fd7\u6587\u4ef6\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u4f8b\u5b50\uff0c\u5728\u65e5\u5fd7\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u4f1a\u5b58\u653e\u9519\u8bef\u4fe1\u606f\u548c\u8c03\u8bd5\u4fe1\u606f\uff0c\u56e0\u6b64\u5c06\u5176\u5b9a\u4e49\u4e3a\u5168\u5c40\u53d8\u91cf\u662f\u5408\u7406\u7684\uff0c\u56e0\u4e3a\u4e00\u4e2a\u7a0b\u5e8f\u901a\u5e38\u53ea\u4f1a\u6709\u4e00\u4e2a\u65e5\u5fd7\uff0c\u5e76\u4e14\u5b83\u53ef\u80fd\u5728\u7a0b\u5e8f\u4e2d\u7684\u4efb\u4f55\u90e8\u5206\u88ab\u4f7f\u7528\u3002 \u4e0d\u7ba1\u600e\u4e48\u8bf4\uff0c std::cout \u548c std::cin \u5bf9\u8c61\u4e5f\u88ab\u5b9a\u4e49\u6210\u4e86\u5168\u5c40\u53d8\u91cf(\u5728 std \u547d\u540d\u7a7a\u95f4\u4e2d)\u3002 \u4e00\u822c\u8bf4\u6765\uff0c\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u81f3\u5c11\u8981\u6ee1\u8db3\u4e0b\u9762\u4e24\u4e2a\u6761\u4ef6\uff1a\u8be5\u53d8\u91cf\u5728\u7a0b\u5e8f\u4e2d\u7684\u529f\u80fd\u662f\u552f\u4e00\u7684\uff0c\u800c\u4e14\u5b83\u4f1a\u5728\u7a0b\u5e8f\u7684\u5404\u4e2a\u5730\u65b9\u88ab\u4f7f\u7528\u3002 \u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u4f1a\u9519\u8bef\u5730\u8ba4\u4e3a\uff0c\u56e0\u4e3a\u73b0\u5728\u53ea\u9700\u8981\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u6240\u4ee5\u7528\u7528\u4e5f\u6ca1\u5173\u7cfb\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u4f60\u73b0\u5728\u53ef\u80fd\u5728\u5f00\u53d1\u4e00\u4e2a\u5355\u4eba\u6e38\u620f\uff0c\u6240\u4ee5\u53ea\u9700\u8981\u4e00\u4e2a\u7528\u6237\u3002\u4f46\u662f\u4e07\u4e00\u4ee5\u540e\u4f60\u9700\u8981\u4e3a\u5b83\u6dfb\u52a0\u591a\u4eba\u6a21\u5f0f\u5462\uff1f","title":"\u4ec0\u4e48\u65f6\u5019\u53ef\u4ee5\u5408\u7406\u4f7f\u7528\u975e const \u7684\u5168\u5c40\u53d8\u91cf"},{"location":"6-8-Why-non-const-global-variables-are-evil/#_2","text":"\u5982\u679c\u4f60\u6709\u8db3\u591f\u7684\u7406\u7531\u800c\u4f7f\u7528\u4e86\u4e00\u4e2a\u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u8fd9\u91cc\u6709\u4e00\u4e9b\u5fe0\u544a\u5e0c\u671b\u80fd\u591f\u5e2e\u4f60\u5c3d\u53ef\u80fd\u5730\u907f\u514d\u95ee\u9898\u3002\u5f53\u7136\uff0c\u8fd9\u4e9b\u5fe0\u544a\u4e0d\u4ec5\u4ec5\u53ea\u9488\u5bf9\u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u5b83\u5bf9\u6240\u6709\u7684\u5168\u5c40\u53d8\u91cf\u90fd\u662f\u6709\u610f\u4e49\u7684\u3002 \u9996\u5148\uff0c\u8bf7\u4e3a\u4e0d\u5728\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u5168\u5c40\u53d8\u91cf\u6dfb\u52a0 \u201cg\u201d \u6216\u8005 \u201cg_\u201d \u524d\u7f00\uff0c\u6216\u8005\u5e72\u8106\u5c06\u5b83\u4eec\u653e\u5230\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\uff08\u5728 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u4e2d\u6709\u76f8\u5173\u8ba8\u8bba\uff09\u4ee5\u907f\u514d\u547d\u540d\u51b2\u7a81\u3002 \u4f8b\u5982\uff0c\u4e0d\u8981\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 constexpr double gravity { 9.8 }; // unclear if this is a local or global variable from the name int main () { return 0 ; } \u8981\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 namespace constants { constexpr double gravity { 9.8 }; } int main () { return 0 ; } \u5176\u6b21\uff0c\u4e0d\u8981\u5141\u8bb8\u5168\u5c40\u53d8\u91cf\u7684\u76f4\u63a5\u8bbf\u95ee\uff0c\u6700\u597d\u662f\u5c06\u5b83\u4eec\u201c\u5c01\u88c5\u201d\u8d77\u6765\u3002\u786e\u4fdd\u53d8\u91cf\u53ea\u80fd\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u6587\u4ef6\u5185\u90e8\u88ab\u8bbf\u95ee\uff0c\u4f8b\u5982\u4e3a\u53d8\u91cf\u6dfb\u52a0 static \u6216 const \u4fee\u9970\u7b26\uff0c\u7136\u540e\u63d0\u4f9b\u4e00\u4e2a\u5916\u90e8\u7684\u5168\u5c40\u201c\u8bbf\u95ee\u51fd\u6570\u201d\u7528\u4e8e\u8be5\u53d8\u91cf\u7684\u8bbf\u95ee\u3002\u901a\u8fc7\u8fd9\u4e2a\u8bbf\u95ee\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u53d8\u91cf\u7684\u4f7f\u7528\u65b9\u5f0f\u8fdb\u884c\u7ba1\u7406\uff08\u4f8b\u5982\uff1a\u8f93\u5165\u6821\u9a8c\uff0c\u8303\u56f4\u68c0\u67e5\u7b49\u7b49\uff09\u3002\u6b64\u5916\uff0c\u5982\u679c\u4f60\u4ee5\u540e\u5e0c\u671b\u4fee\u6539\u5e95\u5c42\u5b9e\u73b0\uff08\u4f8b\u5982\u4ece\u4e00\u4e2a\u6570\u636e\u5e93\u8fc1\u79fb\u5230\u53e6\u5916\u4e00\u4e2a\u6570\u636e\u5e93\u65f6\uff09\uff0c\u4f60\u53ea\u9700\u4fee\u6539\u8fd9\u4e2a\u8bbf\u95ee\u51fd\u6570\uff0c\u4f7f\u5176\u8bbf\u95ee\u65b0\u7684\u53d8\u91cf\u5373\u53ef\uff0c\u800c\u65e0\u9700\u66ff\u6362\u6e90\u7801\u4e2d\u6bcf\u5904\u4f7f\u7528\u8be5\u5168\u5c40\u53d8\u91cf\u7684\u5730\u3002 \u4f8b\u5982\uff0c\u4e0d\u8981\u8fd9\u4e48\u505a\uff1a 1 2 3 4 namespace constants { extern const double gravity { 9.8 }; // has external linkage, is directly accessible by other files } \u8981\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 namespace constants { constexpr double gravity { 9.8 }; // has internal linkage, is accessible only by this file } double getGravity () // this function can be exported to other files to access the global outside of this file { // We could add logic here if needed later // or change the implementation transparently to the callers return constants :: gravity ; }","title":"\u6709\u5173\u5168\u5c40\u53d8\u91cf\u7684\u5fe0\u544a"},{"location":"6-8-Why-non-const-global-variables-are-evil/#_3","text":"\u5168\u5c40 const \u53d8\u91cf\u9ed8\u8ba4\u5177\u6709\u5185\u90e8\u94fe\u63a5\u5c5e\u6027\uff0c\u6240\u4ee5 gravity \u5e76\u4e0d\u9700\u8981\u88ab\u5b9a\u4e49\u4e3a static \u3002 \u7b2c\u4e09\uff0c\u5f53\u51fd\u6570\u5fc5\u987b\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u4e0d\u8981\u76f4\u63a5\u5728\u51fd\u6570\u4f53\u4e2d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3002\u5e94\u8be5\u5c06\u5168\u5c40\u53d8\u91cf\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u8fd9\u6837\u7684\u8bdd\uff0c\u4e0d\u7ba1\u4ec0\u4e48\u65f6\u5019\u4f60\u9700\u8981\u4f7f\u7528\u4e0d\u540c\u7684\u503c\u65f6\uff0c\u4f60\u53ea\u9700\u8981\u4fee\u6539\u51fd\u6570\u7684\u5165\u53c2\u5c31\u53ef\u4ee5\uff0c\u8fd9\u4e48\u505a\u6709\u52a9\u4e8e\u4fdd\u6301\u51fd\u6570\u7684\u6a21\u5757\u5316\u3002 \u4e0d\u8981\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include namespace constants { constexpr double gravity { 9.8 }; } // \u8fd9\u4e2a\u51fd\u6570\u53ea\u80fd\u57fa\u4e8e\u5168\u5c40 gravity \u8ba1\u7b97\u901f\u5ea6 double instantVelocity ( int time ) { return constants :: gravity * time ; } int main () { std :: cout << instantVelocity ( 5 ); } \u8981\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include namespace constants { constexpr double gravity { 9.8 }; } // \u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u57fa\u4e8e\u4efb\u4f55 gravity \u503c\u8ba1\u7b97\u901f\u5ea6\uff08\u66f4\u6709\u7528\uff09 double instantVelocity ( int time , double gravity ) { return gravity * time ; } int main () { std :: cout << instantVelocity ( 5 , constants :: gravity ); // \u5c06\u5e38\u91cf\u5f53\u505a\u53c2\u6570\u4f20\u5165 }","title":"\u4e00\u4e2a\u63d0\u9192"},{"location":"6-8-Why-non-const-global-variables-are-evil/#_4","text":"\u95ee\uff1a\u5168\u5c40\u53d8\u91cf\u548c\u4ec0\u4e48\u524d\u7f00\u6700\u914d\uff1f \u7b54: // \uff08\u6ce8\u91ca\uff09 C++ \u7b11\u8bdd\u6700\u68d2\u4e86\uff01","title":"\u4e00\u4e2a\u7b11\u8bdd"},{"location":"6-9-Sharing-global-constants-across-multiple-files-using-inline-variables/","tags":["inline","global","C++17"],"text":"Key Takeaway \u5168\u5c40\u5e38\u91cf\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u5b83\u662f\u5185\u90e8\u53d8\u91cf\uff0c\u5934\u6587\u4ef6\u5305\u542b\u65f6\u4f1a\u88ab\u590d\u5236\u5230\u5230\u6e90\u6587\u4ef6\u4e2d\u3002 \u5f53\u5168\u5c40\u53d8\u91cf\u4e3a\u5185\u90e8\u53d8\u91cf\u65f6\uff0c\u4f1a\u5bfc\u81f4\u4e24\u4e2a\u95ee\u9898\uff1a \u4fee\u6539\u540e\u6240\u6709\u5305\u542b\u8be5\u5934\u6587\u4ef6\u7684\u6587\u4ef6\u90fd\u8981\u91cd\u65b0\u7f16\u8bd1 \u53d8\u91cf\u590d\u5236\u5360\u7528\u5927\u91cf\u5185\u5b58 \u5c06\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u5728cpp\u6587\u4ef6\u4e2d\uff0c\u5e76\u5728\u5934\u6587\u4ef6\u4e2d\u5bf9\u5176\u8fdb\u884c\u524d\u5411\u58f0\u660e\uff0c\u7136\u540e\u518d\u9700\u8981\u5b83\u7684\u5730\u65b9\u5305\u542b\u5934\u6587\u4ef6\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5168\u5c40\u53d8\u91cf\u4e3a\u5916\u90e8\u53d8\u91cf\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e constexpr \u4e0d\u80fd\u88ab \u524d\u5411\u58f0\u660e \uff0c\u56e0\u6b64\u53ea\u80fd\u4f7f\u7528 const \u3002 \u5f53\u5168\u5c40\u53d8\u91cf\u4e3a\u5916\u90e8\u53d8\u91cf\u65f6\uff0c\u4e5f\u4f1a\u5bfc\u81f4\u4e24\u4e2a\u95ee\u9898\uff1a \u8fd9\u4e9b\u53d8\u91cf\u53ea\u6709\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u6587\u4ef6\u4e2d\u5c5e\u4e8e\u7f16\u8bd1\u65f6\u5e38\u91cf\uff0c\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u90fd\u5c5e\u4e8e\u8fd0\u884c\u65f6\u5e38\u91cf\u3002\u56e0\u6b64\u5728\u9700\u8981\u4f7f\u7528\u7f16\u8bd1\u65f6\u5e38\u91cf\u7684\u5730\u65b9\u4e0d\u80fd\u4f7f\u7528\u5b83\u4eec\u3002 \u8fd0\u884c\u65f6\u5e38\u91cf\u7684\u4f18\u5316\u4e0d\u5982\u7f16\u8bd1\u65f6\u5e38\u91cf \u4e3a\u4e86\u5728\u7f16\u8bd1\u65f6\u4f7f\u7528\u67d0\u4e9b\u53d8\u91cf\uff08\u4f8b\u5982\uff0c\u6570\u7ec4\u5927\u5c0f\uff09\uff0c\u7f16\u8bd1\u5668\u5fc5\u987b\u770b\u5230\u53d8\u91cf\u7684\u5b9a\u4e49\uff08\u800c\u4e0d\u4ec5\u4ec5\u662f\u524d\u9879\u58f0\u660e\uff09\u3002 C++17 \u5f15\u5165\u4e86\u5185\u8054\u53d8\u91cf\uff0c\u5373\u53ef\u4ee5\u88ab\u591a\u5904\u5b9a\u4e49\u7684\u53d8\u91cf\u3002\u7f16\u8bd1\u5668\u4f1a\u5c06\u5b83\u4eec\u7684\u5b9a\u4e49\u5408\u5e76\u3002\u540c\u65f6\u5b83\u8fd8\u5177\u6709\u548c constexpr \u540c\u6837\u7684\u5c5e\u6027\u3002 \u5185\u8054\u53d8\u91cf\u6709\u4e24\u4e2a\u4e3b\u8981\u7684\u9650\u5236\uff0c\u9700\u8981\u6211\u4eec\u9075\u5faa\uff1a \u5185\u8054\u53d8\u91cf\u7684\u6240\u6709\u5b9a\u4e49\u5fc5\u987b\u662f\u5b8c\u5168\u4e00\u81f4\u7684\uff08\u5426\u5219\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \uff09\u3002 \u5185\u8054\u53d8\u91cf\u7684\u5b9a\u4e49\uff08\u800c\u975e\u58f0\u660e\uff09\u5fc5\u987b\u51fa\u73b0\u5728\u4efb\u4f55\u4f7f\u7528\u5b83\u4eec\u7684\u6587\u4ef6\u4e2d\u3002 \u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u652f\u6301 C++17\uff0c\u6700\u597d\u5c06\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u4e3a inline constexpr \u7c7b\u578b\u5e76\u653e\u7f6e\u5728\u5934\u6587\u4ef6\u4e2d\u3002 \u5728\u5f88\u591a\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u6709\u4e9b\u7b26\u53f7\u5e38\u91cf\u9700\u8981\u88ab\u6240\u6709\u7684\u4ee3\u7801\u4f7f\u7528\uff08\u800c\u4e0d\u4ec5\u4ec5\u662f\u88ab\u5c40\u90e8\u7684\u4ee3\u7801\u4f7f\u7528\uff09\u3002\u8fd9\u4e9b\u53d8\u91cf\u53ef\u80fd\u662f\u7269\u7406\u5e38\u91cf\u6216\u6570\u5b66\u5e38\u91cf\uff08\u4f8b\u5982 \u03c0 \u6216\u963f\u4f0f\u4f3d\u5fb7\u7f57\u5e38\u6570\uff09\uff0c\u6216\u8005\u662f\u67d0\u4e2a\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u7684\u53c2\u6570\uff08\u4f8b\u5982\u6469\u64e6\u7cfb\u6570\u6216\u5f15\u529b\u7cfb\u6570\uff09\u3002\u4e0e\u5176\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u5404\u5b9a\u4e49\u4e00\u904d\u8fd9\u4e9b\u53d8\u91cf\uff0c\u4e0d\u5982\u5c06\u5b83\u4eec\u96c6\u4e2d\u5b9a\u4e49\u5728\u4e00\u4e2a\u5730\u65b9\u7136\u540e\u6309\u9700\u4f7f\u7528\u3002\u8fd9\u6837\uff0c\u4e07\u4e00\u4f60\u9700\u8981\u4fee\u6539\u5b83\u4eec\u7684\u503c\uff0c\u4f60\u53ea\u9700\u8981\u5728\u4e00\u5904\u4fee\u6539\u5373\u53ef\u3002 \u672c\u8282\u8bfe\u4f1a\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u4e0a\u8ff0\u5168\u5c40\u5e38\u91cf\u7684\u5b9a\u4e49\u3002 \u5c06\u5168\u5c40\u5e38\u91cf\u4f5c\u4e3a\u5185\u90e8\u53d8\u91cf \u5728 C++17 \u4e4b\u524d\uff0c\u5b9a\u4e49\u5168\u5c40\u5e38\u91cf\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a \u5b9a\u4e49\u4e00\u4e2a\u5934\u6587\u4ef6\u6765\u5b58\u653e\u8fd9\u4e9b\u5e38\u91cf \u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4 (\u53c2\u89c1\uff1a 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u5c06\u5168\u90e8\u7684\u5e38\u91cf\u653e\u5230\u8be5\u547d\u540d\u7a7a\u95f4\u4e2d\uff08\u540c\u65f6\u786e\u4fdd\u5b83\u4eec\u90fd\u662f constexpr \u7c7b\u578b\u7684\uff09 \u5728\u9700\u8981\u4f7f\u7528\u8fd9\u4e9b\u5e38\u91cf\u7684\u5730\u65b9\uff0c #include \u8be5\u5934\u6587\u4ef6 constants.h 1 2 3 4 5 6 7 8 9 10 11 12 13 #ifndef CONSTANTS_H #define CONSTANTS_H // \u5b9a\u4e49\u5305\u542b\u5e38\u91cf\u7684\u547d\u540d\u7a7a\u95f4 namespace constants { // \u9ed8\u8ba4\u5185\u90e8\u94fe\u63a5 constexpr double pi { 3.14159 }; constexpr double avogadro { 6.0221413e23 }; constexpr double myGravity { 9.2 }; // m/s^2 -- gravity is light on this planet // ... other related constants } #endif \u7136\u540e\u5728 .cpp \u6587\u4ef6\u4e2d\u4f7f\u7528 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 ( :: )\u6765\u4f7f\u7528\u8fd9\u4e9b\u5e38\u91cf\uff0c\u8fd0\u7b97\u7b26\u5de6\u4fa7\u662f\u547d\u540d\u7a7a\u95f4\uff0c\u53f3\u4fa7\u662f\u5e38\u91cf\u540d\uff1a main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include \"constants.h\" // include a copy of each constant in this file #include int main () { std :: cout << \"Enter a radius: \" ; int radius {}; std :: cin >> radius ; std :: cout << \"The circumference is: \" << 2.0 * radius * constants :: pi << '\\n' ; return 0 ; } \u5f53\u5934\u6587\u4ef6\u88ab #included \u5230\u4e86 a .cpp \u4e2d\u540e\uff0c\u5934\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u5e38\u91cf\u5b9e\u9645\u4e0a\u90fd\u4f1a\u88ab \u62f7\u8d1d \u5230\u6e90\u6587\u4ef6\u4e2d\u3002\u56e0\u4e3a\u8fd9\u4e9b\u53d8\u91cf\u4e0d\u5c5e\u4e8e\u4efb\u4f55\u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u4eec\u5728\u88ab\u5305\u542b\u5230\u6e90\u6587\u4ef6\u540e\uff0c\u4f1a\u88ab\u5f53\u505a\u5168\u5c40\u53d8\u91cf\u5bf9\u5f85\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u5728\u6587\u4ef6\u7684\u4efb\u4f55\u5730\u65b9\u4f7f\u7528\u5b83\u4eec\u3002 \u56e0\u4e3a const \u5168\u5c40\u53d8\u91cf\u5177\u6709 \u5185\u90e8\u94fe\u63a5 \u5c5e\u6027\uff0c\u56e0\u6b64\u6bcf\u4e2a\u53d8\u91cf\u5728.cpp\u6587\u4ef6\u4e4b\u95f4\u662f\u72ec\u7acb\u7684\uff0c\u800c\u4e14\u94fe\u63a5\u5668\u5e76\u4e0d\u80fd\u770b\u5230\u5b83\u4eec\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7531\u4e8e\u5b83\u4eec\u662f\u5e38\u91cf\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u5c06\u5176\u201c\u4f18\u5316\u6389\u201d\u3002 \u9898\u5916\u8bdd \u201c\u4f18\u5316\u6389\u201d\u8fd9\u4e2a\u672f\u8bed\uff0c\u6307\u7684\u662f\u7f16\u8bd1\u5668\u4e3a\u4e86\u4f18\u5316\u7a0b\u5e8f\u6027\u80fd\u800c\u5220\u9664\u4e86\u67d0\u4e9b\u4e1c\u897f\uff0c\u4f46\u4e0d\u4f1a\u5bf9\u7a0b\u5e8f\u7684\u529f\u80fd\u4ea7\u751f\u5f71\u54cd\u3002\u4f8b\u5982\uff0c\u4f60\u5b9a\u4e49\u4e86\u4e00\u4e2a const \u7c7b\u578b\u7684\u53d8\u91cf x \u5e76\u4e14\u5c06\u5176\u521d\u59cb\u5316\u4e3a 4\u3002\u5219\u7f16\u8bd1\u5668\u4f1a\u5c06\u4efb\u4f55\u5f15\u7528\u8be5\u53d8\u91cf\u7684\u5730\u65b9\uff0c\u76f4\u63a5\u66ff\u6362\u4e3a 4\uff08\u56e0\u4e3a x \u662fconst\u7c7b\u578b\u7684\uff0c\u6240\u4ee5\u5b83\u4e0d\u80fd\u88ab\u4fee\u6539\uff09\uff0c\u4ece\u800c\u907f\u514d\u4e86\u53d8\u91cf\u7684\u5b9a\u4e49\u548c\u521d\u59cb\u5316\u8fc7\u7a0b\u3002 \u5c06\u5168\u5c40\u5e38\u91cf\u4f5c\u4e3a\u5916\u90e8\u53d8\u91cf \u4e0a\u9762\u63d0\u5230\u7684\u65b9\u6cd5\u5b9e\u9645\u4e0a\u6709\u4e00\u4e9b\u7f3a\u9677\u3002 \u5c3d\u7ba1\u5c06 constants.h \u5934\u6587\u4ef6 #included \u5230\u6bcf\u4e00\u4e2a\u9700\u8981\u5b83\u7684\u6e90\u6587\u4ef6\u4e2d\u662f\u975e\u5e38\u7b80\u5355\uff08\u5bf9\u4e8e\u5c0f\u7a0b\u5e8f\u6765\u8bf4\u4e5f\u901a\u5e38\u4e0d\u4f1a\u6709\u4ec0\u4e48\u95ee\u9898\uff09\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u4f46\u6bd5\u7adf\u8fd9\u4f1a\u5bfc\u81f4\u53d8\u91cf\u88ab\u590d\u5236\u5230\u6e90\u6587\u4ef6\u4e2d\u3002\u56e0\u6b64\uff0c\u5982\u679c constants.h \u88ab 20 \u4e2a\u6587\u4ef6\u6240\u5305\u542b\uff0c\u90a3\u4e48\u8fd9\u4e9b\u5168\u5c40\u53d8\u91cf\u5c31\u4f1a\u88ab\u590d\u5236 20 \u6b21\u3002 \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e \u5e76\u4e0d\u80fd\u9632\u6b62\u8be5\u95ee\u9898\u7684\u53d1\u751f\uff0c\u56e0\u4e3a\u5b83\u53ea\u80fd\u4fdd\u8bc1\u5934\u6587\u4ef6\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u88ab\u5305\u542b\u4e00\u6b21\uff0c\u800c\u4e0d\u662f\u963b\u6b62\u5934\u6587\u4ef6\u88ab\u591a\u4e2a\u4e0d\u540c\u7684\u6587\u4ef6\u5305\u542b\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u4f1a\u9762\u4e34\u4e24\u4e2a\u95ee\u9898\uff1a \u4fee\u6539\u5e38\u91cf\u4f1a\u5bfc\u81f4\u6240\u6709\u5305\u542b\u8be5\u5934\u6587\u4ef6\u7684\u6587\u4ef6\u91cd\u65b0\u7f16\u8bd1\uff0c\u5bf9\u4e8e\u5927\u578b\u9879\u76ee\u6765\u8bf4\u8fd9\u4f1a\u6d6a\u8d39\u5f88\u591a\u65f6\u95f4 \u5982\u679c\u5e38\u91cf\u5f88\u5927\uff0c\u800c\u4e14\u65e0\u6cd5\u88ab\u201c\u4f18\u5316\u6389\u201d\uff0c\u90a3\u4e48\u5927\u91cf\u7684\u590d\u5236\u4f1a\u5bfc\u81f4\u5927\u91cf\u7684\u5185\u5b58\u5360\u7528\u3002 \u907f\u514d\u8fd9\u4e2a\u95ee\u9898\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u662f\u5c06\u5168\u5c40\u5e38\u91cf\u5b9a\u4e49\u4e3a \u5916\u90e8\u53d8\u91cf \uff0c\u8fd9\u6837\u4e00\u4e2a\u53d8\u91cf\uff08\u521d\u59cb\u5316\u4e00\u6b21\uff09\u5c31\u53ef\u4ee5\u88ab\u6240\u6709\u7684\u6587\u4ef6\u201c\u5171\u4eab\u4f7f\u7528\u201d\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5fc5\u987b\u5c06\u5e38\u91cf\u5b9a\u4e49\u5728\u4e00\u4e2a a.cpp \u6587\u4ef6\u4e2d\uff08\u786e\u4fdd\u5b83\u53ea\u88ab\u5b9a\u4e49\u4e00\u6b21\uff09\uff0c\u7136\u540e\u5728\u5934\u6587\u4ef6\u4e2d\u6dfb\u52a0\u5176\u524d\u5411\u5b9a\u4e49\uff08\u8be5\u5934\u6587\u4ef6\u4ecd\u9700\u8981\u88ab\u5305\u542b\u5230\u4f7f\u7528\u4e86\u8be5\u53d8\u91cf\u7684\u6587\u4ef6\u4e2d\uff09\u3002 \u4f5c\u8005\u6ce8 \u8fd9\u91cc\u4f7f\u7528\u4e86 const \u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u8fd9\u662f\u56e0\u4e3a constexpr \u7c7b\u578b\u7684\u53d8\u91cf\u4e0d\u80fd\u88ab\u524d\u5411\u58f0\u660e\uff08 6-7-External-linkage-and-variable-forward-declarations#^ce6263 \uff09\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u9700\u8981\u5728\u7f16\u8bd1\u65f6\u77e5\u9053 constexpr \u7c7b\u578b\u53d8\u91cf\u7684\u5b9a\u4e49\uff0c\u800c\u524d\u5411\u58f0\u660e\u5e76\u4e0d\u80fd\u63d0\u4f9b\u5b9a\u4e49\u7684\u4fe1\u606f\u3002 constants.cpp 1 2 3 4 5 6 7 8 9 #include \"constants.h\" namespace constants { // actual global variables extern const double pi { 3.14159 }; extern const double avogadro { 6.0221413e23 }; extern const double myGravity { 9.2 }; // m/s^2 -- gravity is light on this planet } constants.h 1 2 3 4 5 6 7 8 9 10 11 12 #ifndef CONSTANTS_H #define CONSTANTS_H namespace constants { // since the actual variables are inside a namespace, the forward declarations need to be inside a namespace as well extern const double pi ; extern const double avogadro ; extern const double myGravity ; } #endif \u4f7f\u7528\u65b9\u6cd5\u548c\u4e4b\u524d\u662f\u4e00\u6837\u7684\uff1a main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include \"constants.h\" // include all the forward declarations #include int main () { std :: cout << \"Enter a radius: \" ; int radius {}; std :: cin >> radius ; std :: cout << \"The circumference is: \" << 2.0 * radius * constants :: pi << '\\n' ; return 0 ; } \u56e0\u4e3a\u5168\u5c40\u7b26\u53f7\u5e38\u91cf\u5e94\u8be5\u88ab\u653e\u7f6e\u5728\u547d\u540d\u7a7a\u95f4\u4e2d\uff08\u907f\u514d\u4e0e\u5176\u4ed6\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u4ea7\u751f\u547d\u540d\u51b2\u7a81\uff09\uff0c\u6240\u4ee5\u6ca1\u5fc5\u8981\u5bf9\u5176\u4f7f\u7528 g_ \u524d\u7f00\u3002 \u8fd9\u6837\u4e00\u6765\uff0c\u7b26\u53f7\u5e38\u91cf\u53ea\u4f1a\u5728 constants.cpp \u4e2d\u88ab\u521d\u59cb\u5316\u4e00\u6b21\uff0c\u800c\u4e0d\u662f\u5728\u6bcf\u4e2a constants.h \u88ab\u5305\u542b\u7684\u5730\u65b9\u88ab\u521d\u59cb\u5316\u3002\u540c\u65f6\uff0c\u8fd9\u4e9b\u5e38\u6570\u90fd\u4f1a\u88ab\u94fe\u63a5\u5230 constants.cpp \u4e2d\u521d\u59cb\u5316\u7684\u53d8\u91cf\uff0c\u56e0\u6b64\u5f53\u8fd9\u4e9b\u53d8\u91cf\u88ab\u4fee\u6539\u65f6\uff0c\u53ea\u6709 constants.cpp \u9700\u8981\u88ab\u91cd\u65b0\u7f16\u8bd1\u3002 \u4e0d\u8fc7\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u4e0d\u662f\u5b8c\u7f8e\u7684\u3002\u9996\u5148\uff0c\u8fd9\u4e9b\u5e38\u91cf\u73b0\u5728\u53ea\u4f1a\u5728\u5b83\u4eec\u88ab\u5b9a\u4e49\u7684\u6587\u4ef6\u4e2d\u88ab\u770b\u505a \u7f16\u8bd1\u65f6 \u5e38\u91cf\u3002\u800c\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\uff0c\u7f16\u8bd1\u5668\u53ea\u80fd\u770b\u5230\u5b83\u4eec\u7684\u58f0\u660e\uff0c\u800c\u770b\u4e0d\u5230\u5e38\u91cf\u5177\u4f53\u7684\u4e2d\uff08\u5fc5\u987b\u6709\u94fe\u63a5\u5668\u8fdb\u884c\u89e3\u6790\uff09\u3002\u56e0\u6b64\uff0c\u5728 constants.cpp \u4ee5\u5916\u7684\u5730\u65b9\uff0c\u8fd9\u4e9b\u5e38\u91cf\u90fd\u4e0d\u80fd\u88ab\u770b\u505a\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\uff0c\u4e5f\u5c31\u4e0d\u80fd\u5728\u8981\u6c42\u4f7f\u7528\u7f16\u8bd1\u65f6\u5e38\u91cf\u7684\u5730\u65b9\u4f7f\u7528\u5b83\u4eec\u3002\u53e6\u5916\uff0c\u56e0\u4e3a\u7f16\u8bd1\u65f6\u5e38\u91cf\u76f8\u5bf9\u4e8e\u8fd0\u884c\u65f6\u5e38\u91cf\u66f4\u5bb9\u6613\u4f18\u5316\uff0c\u6240\u4ee5\u5bf9\u4e8e\u8fd9\u4e9b\u8fd0\u884c\u65f6\u5e38\u91cf\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u65e0\u6cd5\u5bf9\u5176\u8fdb\u884c\u8db3\u591f\u7684\u4f18\u5316\u3002 \u5173\u952e\u4fe1\u606f \u4e3a\u4e86\u5728\u7f16\u8bd1\u65f6\u4f7f\u7528\u67d0\u4e9b\u53d8\u91cf\uff08\u4f8b\u5982\uff0c\u6570\u7ec4\u5927\u5c0f\uff09\uff0c\u7f16\u8bd1\u5668\u5fc5\u987b\u770b\u5230\u53d8\u91cf\u7684\u5b9a\u4e49\uff08\u800c\u4e0d\u4ec5\u4ec5\u662f\u524d\u9879\u58f0\u660e\uff09\u3002 \u56e0\u4e3a\u7f16\u8bd1\u5668\u662f\u72ec\u7acb\u7f16\u8bd1\u5404\u4e2a\u6e90\u6587\u4ef6\u7684\uff0c\u56e0\u6b64\u5b83\u53ea\u80fd\u5bf9\u6e90\u6587\u4ef6\u4e2d\u5b58\u5728\u5b9a\u4e49\u7684\u53d8\u91cf\u8fdb\u884c\u7f16\u8bd1\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49\u5728 constants.cpp \u7684\u53d8\u91cf\uff0c\u5728\u7f16\u8bd1\u5668\u7f16\u8bd1 main.cpp \u7684\u65f6\u5019\u662f\u4e0d\u53ef\u89c1\u7684\uff0c\u51fa\u4e8e\u8fd9\u4e2a\u539f\u56e0 constexpr \u7684\u5b9a\u4e49\u548c\u58f0\u660e\u4e0d\u80fd\u88ab\u5206\u6563\u5230\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u4e2d\uff0c\u5b83\u5fc5\u987b\u88ab\u5b9a\u4e49\u5728\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\u91cc\u3002 \u51fa\u4e8e\u4e0a\u8ff0\u539f\u56e0\uff0c\u6211\u4eec\u8fd8\u662f\u5efa\u8bae\u6700\u597d\u5c06\u5e38\u91cf\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u3002\u5982\u679c\u56e0\u6b64\u5bfc\u81f4\u4e86\u67d0\u4e9b\u7279\u5b9a\u7684\u95ee\u9898\uff0c\u4e0d\u59a8\u5230\u65f6\u5019\u5728\u628a\u5b83\u4eec\u4e00\u8d77\u79fb\u52a8\u5230.cpp\u6587\u4ef6\u53bb\u3002 \u5c06\u5168\u5c40\u5e38\u91cf\u4f5c\u4e3a\u5185\u8054\u53d8\u91cf (C++17) C++17 \u5f15\u5165\u4e86\u4e00\u4e2a\u65b0\u7684\u6982\u5ff5\u2014\u2014 \u5185\u8054\u53d8\u91cf(inline variables) \u3002\u5728 C++ \u4e2d\uff0c[[inline|\u5185\u8054]]\u7684\u610f\u601d\u9010\u6e10\u5f15\u7533\u6210\u4e86\u201c\u5141\u8bb8\u591a\u5904\u5b9a\u4e49\u201d\u3002\u56e0\u6b64\uff0c\u5185\u8054\u53d8\u91cf\u53ef\u4ee5\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u88ab\u591a\u6b21\u5b9a\u4e49\uff0c\u4e14\u4e0d\u4f1a\u8fdd\u53cd \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u3002\u5185\u8054\u7684\u5168\u5c40\u53d8\u91cf\u9ed8\u8ba4\u5177\u6709\u5916\u90e8\u94fe\u63a5\u5c5e\u6027\u3002 \u94fe\u63a5\u5668\u53ef\u4ee5\u5c06\u6240\u6709\u7684\u5185\u8054\u5b9a\u4e49\u5408\u5e76\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u5b9a\u4e49\uff08\u8fd9\u6837\u5c31\u6ee1\u8db3\u4e86\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u5f53\u6211\u4eec\u628a\u5185\u8054\u53d8\u91cf\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u7684\u540c\u65f6\uff0c\u8fd8\u80fd\u591f\u5c06\u5b83\u4eec\u5728.cpp\u6587\u4ef6\u4e2d\u7684\u5b9a\u4e49\u770b\u505a\u662f\u4e00\u4e2a\u5b9a\u4e49\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4e00\u4e2a\u666e\u901a\u7684\u5e38\u91cf\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u540c\u65f6\u88ab #include \u5230\u4e8610\u4e2a\u4e0d\u540c\u7684\u6e90\u6587\u4ef6\u3002\u5982\u679c\u5b83\u4e0d\u662f\u5185\u8054\u7684\uff0c\u5219\u6b64\u65f6\u4f1a\u4ea7\u751f10\u4e2a\u5b9a\u4e49\u3002\u5982\u679c\u662f\u5185\u8054\u7684\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u9009\u53d6\u4e00\u4e2a\u5b9a\u4e49\u4f5c\u4e3a\u4e3b\u5b9a\u4e49\uff0c\u8fd9\u6837\u4e00\u6765\u5c31\u53ea\u4f1a\u4ea7\u751f\u4e00\u4e2a\u5b9a\u4e49\u3002\u8fd9\u6837\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8282\u77019\u4e2a\u5e38\u91cf\u7684\u5185\u5b58\u7a7a\u95f4\u3002 \u8fd9\u4e9b\u53d8\u91cf\u540c\u65f6\u8fd8\u4fdd\u7559\u4e86 constexpr \u7684\u7279\u6027\uff0c\u56e0\u6b64\u5728\u4efb\u4f55\u5305\u542b\u5b83\u4eec\u7684\u6587\u4ef6\u4e2d\uff0c\u53ea\u8981\u662f constexpr \u53ef\u7528\u7684\u5730\u65b9\uff0c\u8fd9\u4e9b\u53d8\u91cf\u4e5f\u53ef\u7528\u3002\u540c\u65f6\uff0c\u76f8\u6bd4\u8f83\u4e8e\u8fd0\u884c\u65f6\u5e38\u91cf\uff08\u6216\u975e\u5e38\u91cf\uff09\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u6781\u5927\u5730\u4f18\u5316 constexpr \u7c7b\u578b\u7684\u53d8\u91cf \u5185\u8054\u53d8\u91cf\u6709\u4e24\u4e2a\u4e3b\u8981\u7684\u9650\u5236\uff0c\u9700\u8981\u6211\u4eec\u9075\u5faa\uff1a \u5185\u8054\u53d8\u91cf\u7684\u6240\u6709\u5b9a\u4e49\u5fc5\u987b\u662f\u5b8c\u5168\u4e00\u81f4\u7684\uff08\u5426\u5219\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\uff09\u3002 \u5185\u8054\u53d8\u91cf\u7684\u5b9a\u4e49\uff08\u800c\u975e\u58f0\u660e\uff09\u5fc5\u987b\u51fa\u73b0\u5728\u4efb\u4f55\u4f7f\u7528\u5b83\u4eec\u7684\u6587\u4ef6\u4e2d\u3002 \u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u7ee7\u7eed\u6cbf\u7528\u5c06\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u7684\u65b9\u5f0f\uff0c\u540c\u65f6\u8fd8\u53ef\u4ee5\u907f\u514d\u53d8\u91cf\u7684\u91cd\u590d\u5b9a\u4e49\uff1a constants.h 1 2 3 4 5 6 7 8 9 10 11 12 #ifndef CONSTANTS_H #define CONSTANTS_H // define your own namespace to hold constants namespace constants { inline constexpr double pi { 3.14159 }; // note: now inline constexpr inline constexpr double avogadro { 6.0221413e23 }; inline constexpr double myGravity { 9.2 }; // m/s^2 -- gravity is light on this planet // ... other related constants } #endif main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include \"constants.h\" #include int main () { std :: cout << \"Enter a radius: \" ; int radius {}; std :: cin >> radius ; std :: cout << \"The circumference is: \" << 2.0 * radius * constants :: pi << '\\n' ; return 0 ; } \u5c06 constants.h \u5305\u542b\u5230\u6240\u6709\u9700\u8981\u4f7f\u7528\u5b83\u7684\u4ee3\u7801\u6587\u4ef6\u4e2d\uff0c\u4f46\u662f\u8fd9\u4e9b\u53d8\u91cf\u53ea\u4f1a\u88ab\u521d\u59cb\u5316\u4e00\u6b21\uff0c\u5e76\u4e14\u5728\u6240\u6709\u6587\u4ef6\u4e2d\u5171\u4eab\u4f7f\u7528\u3002 \u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u4ecd\u7136\u4e0d\u80fd\u907f\u514d\u6587\u4ef6\u91cd\u590d\u7f16\u8bd1\u7684\u95ee\u9898\u3002\u5f53\u4efb\u4f55\u5e38\u91cf\u88ab\u4fee\u6539\u65f6\uff0c\u6bcf\u4e2a\u5305\u542b\u5b83\u7684\u6587\u4ef6\u90fd\u9700\u8981\u88ab\u91cd\u65b0\u7f16\u8bd1\u3002\u6240\u4ee5\uff0c\u5982\u679c\u4f60\u7684\u5e38\u91cf\u7ecf\u5e38\u9700\u8981\u4fee\u6539\uff08\u51fa\u4e8e\u8c03\u4f18\u7684\u76ee\u7684\uff09\uff0c\u8fd9\u65e0\u7591\u4f1a\u5bfc\u81f4\u5f88\u957f\u7684\u91cd\u65b0\u7f16\u8bd1\u65f6\u95f4\uff0c\u6b64\u65f6\u4f60\u53ef\u4ee5\u8003\u8651\u5c06\u5e38\u91cf\u5f15\u5165\u5230\u5355\u72ec\u7684\u5934\u6587\u4ef6\u4e2d\uff08\u51cf\u5c11include\u7684\u6570\u91cf\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u652f\u6301 C++17\uff0c\u6700\u597d\u5c06\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u4e3a inline constexpr \u7c7b\u578b\u5e76\u653e\u7f6e\u5728\u5934\u6587\u4ef6\u4e2d\u3002 \u6ce8\u610f \u4f7f\u7528 std::string_view \u5b9a\u4e49 constexpr \u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u5728 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \u4e2d\u8fdb\u884c\u4e86\u4ecb\u7ecd\u3002","title":"6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf"},{"location":"6-9-Sharing-global-constants-across-multiple-files-using-inline-variables/#_1","text":"\u5728 C++17 \u4e4b\u524d\uff0c\u5b9a\u4e49\u5168\u5c40\u5e38\u91cf\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a \u5b9a\u4e49\u4e00\u4e2a\u5934\u6587\u4ef6\u6765\u5b58\u653e\u8fd9\u4e9b\u5e38\u91cf \u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4 (\u53c2\u89c1\uff1a 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u5c06\u5168\u90e8\u7684\u5e38\u91cf\u653e\u5230\u8be5\u547d\u540d\u7a7a\u95f4\u4e2d\uff08\u540c\u65f6\u786e\u4fdd\u5b83\u4eec\u90fd\u662f constexpr \u7c7b\u578b\u7684\uff09 \u5728\u9700\u8981\u4f7f\u7528\u8fd9\u4e9b\u5e38\u91cf\u7684\u5730\u65b9\uff0c #include \u8be5\u5934\u6587\u4ef6 constants.h 1 2 3 4 5 6 7 8 9 10 11 12 13 #ifndef CONSTANTS_H #define CONSTANTS_H // \u5b9a\u4e49\u5305\u542b\u5e38\u91cf\u7684\u547d\u540d\u7a7a\u95f4 namespace constants { // \u9ed8\u8ba4\u5185\u90e8\u94fe\u63a5 constexpr double pi { 3.14159 }; constexpr double avogadro { 6.0221413e23 }; constexpr double myGravity { 9.2 }; // m/s^2 -- gravity is light on this planet // ... other related constants } #endif \u7136\u540e\u5728 .cpp \u6587\u4ef6\u4e2d\u4f7f\u7528 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 ( :: )\u6765\u4f7f\u7528\u8fd9\u4e9b\u5e38\u91cf\uff0c\u8fd0\u7b97\u7b26\u5de6\u4fa7\u662f\u547d\u540d\u7a7a\u95f4\uff0c\u53f3\u4fa7\u662f\u5e38\u91cf\u540d\uff1a main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include \"constants.h\" // include a copy of each constant in this file #include int main () { std :: cout << \"Enter a radius: \" ; int radius {}; std :: cin >> radius ; std :: cout << \"The circumference is: \" << 2.0 * radius * constants :: pi << '\\n' ; return 0 ; } \u5f53\u5934\u6587\u4ef6\u88ab #included \u5230\u4e86 a .cpp \u4e2d\u540e\uff0c\u5934\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u5e38\u91cf\u5b9e\u9645\u4e0a\u90fd\u4f1a\u88ab \u62f7\u8d1d \u5230\u6e90\u6587\u4ef6\u4e2d\u3002\u56e0\u4e3a\u8fd9\u4e9b\u53d8\u91cf\u4e0d\u5c5e\u4e8e\u4efb\u4f55\u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u4eec\u5728\u88ab\u5305\u542b\u5230\u6e90\u6587\u4ef6\u540e\uff0c\u4f1a\u88ab\u5f53\u505a\u5168\u5c40\u53d8\u91cf\u5bf9\u5f85\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u5728\u6587\u4ef6\u7684\u4efb\u4f55\u5730\u65b9\u4f7f\u7528\u5b83\u4eec\u3002 \u56e0\u4e3a const \u5168\u5c40\u53d8\u91cf\u5177\u6709 \u5185\u90e8\u94fe\u63a5 \u5c5e\u6027\uff0c\u56e0\u6b64\u6bcf\u4e2a\u53d8\u91cf\u5728.cpp\u6587\u4ef6\u4e4b\u95f4\u662f\u72ec\u7acb\u7684\uff0c\u800c\u4e14\u94fe\u63a5\u5668\u5e76\u4e0d\u80fd\u770b\u5230\u5b83\u4eec\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7531\u4e8e\u5b83\u4eec\u662f\u5e38\u91cf\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u5c06\u5176\u201c\u4f18\u5316\u6389\u201d\u3002 \u9898\u5916\u8bdd \u201c\u4f18\u5316\u6389\u201d\u8fd9\u4e2a\u672f\u8bed\uff0c\u6307\u7684\u662f\u7f16\u8bd1\u5668\u4e3a\u4e86\u4f18\u5316\u7a0b\u5e8f\u6027\u80fd\u800c\u5220\u9664\u4e86\u67d0\u4e9b\u4e1c\u897f\uff0c\u4f46\u4e0d\u4f1a\u5bf9\u7a0b\u5e8f\u7684\u529f\u80fd\u4ea7\u751f\u5f71\u54cd\u3002\u4f8b\u5982\uff0c\u4f60\u5b9a\u4e49\u4e86\u4e00\u4e2a const \u7c7b\u578b\u7684\u53d8\u91cf x \u5e76\u4e14\u5c06\u5176\u521d\u59cb\u5316\u4e3a 4\u3002\u5219\u7f16\u8bd1\u5668\u4f1a\u5c06\u4efb\u4f55\u5f15\u7528\u8be5\u53d8\u91cf\u7684\u5730\u65b9\uff0c\u76f4\u63a5\u66ff\u6362\u4e3a 4\uff08\u56e0\u4e3a x \u662fconst\u7c7b\u578b\u7684\uff0c\u6240\u4ee5\u5b83\u4e0d\u80fd\u88ab\u4fee\u6539\uff09\uff0c\u4ece\u800c\u907f\u514d\u4e86\u53d8\u91cf\u7684\u5b9a\u4e49\u548c\u521d\u59cb\u5316\u8fc7\u7a0b\u3002","title":"\u5c06\u5168\u5c40\u5e38\u91cf\u4f5c\u4e3a\u5185\u90e8\u53d8\u91cf"},{"location":"6-9-Sharing-global-constants-across-multiple-files-using-inline-variables/#_2","text":"\u4e0a\u9762\u63d0\u5230\u7684\u65b9\u6cd5\u5b9e\u9645\u4e0a\u6709\u4e00\u4e9b\u7f3a\u9677\u3002 \u5c3d\u7ba1\u5c06 constants.h \u5934\u6587\u4ef6 #included \u5230\u6bcf\u4e00\u4e2a\u9700\u8981\u5b83\u7684\u6e90\u6587\u4ef6\u4e2d\u662f\u975e\u5e38\u7b80\u5355\uff08\u5bf9\u4e8e\u5c0f\u7a0b\u5e8f\u6765\u8bf4\u4e5f\u901a\u5e38\u4e0d\u4f1a\u6709\u4ec0\u4e48\u95ee\u9898\uff09\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u4f46\u6bd5\u7adf\u8fd9\u4f1a\u5bfc\u81f4\u53d8\u91cf\u88ab\u590d\u5236\u5230\u6e90\u6587\u4ef6\u4e2d\u3002\u56e0\u6b64\uff0c\u5982\u679c constants.h \u88ab 20 \u4e2a\u6587\u4ef6\u6240\u5305\u542b\uff0c\u90a3\u4e48\u8fd9\u4e9b\u5168\u5c40\u53d8\u91cf\u5c31\u4f1a\u88ab\u590d\u5236 20 \u6b21\u3002 \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e \u5e76\u4e0d\u80fd\u9632\u6b62\u8be5\u95ee\u9898\u7684\u53d1\u751f\uff0c\u56e0\u4e3a\u5b83\u53ea\u80fd\u4fdd\u8bc1\u5934\u6587\u4ef6\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u88ab\u5305\u542b\u4e00\u6b21\uff0c\u800c\u4e0d\u662f\u963b\u6b62\u5934\u6587\u4ef6\u88ab\u591a\u4e2a\u4e0d\u540c\u7684\u6587\u4ef6\u5305\u542b\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u4f1a\u9762\u4e34\u4e24\u4e2a\u95ee\u9898\uff1a \u4fee\u6539\u5e38\u91cf\u4f1a\u5bfc\u81f4\u6240\u6709\u5305\u542b\u8be5\u5934\u6587\u4ef6\u7684\u6587\u4ef6\u91cd\u65b0\u7f16\u8bd1\uff0c\u5bf9\u4e8e\u5927\u578b\u9879\u76ee\u6765\u8bf4\u8fd9\u4f1a\u6d6a\u8d39\u5f88\u591a\u65f6\u95f4 \u5982\u679c\u5e38\u91cf\u5f88\u5927\uff0c\u800c\u4e14\u65e0\u6cd5\u88ab\u201c\u4f18\u5316\u6389\u201d\uff0c\u90a3\u4e48\u5927\u91cf\u7684\u590d\u5236\u4f1a\u5bfc\u81f4\u5927\u91cf\u7684\u5185\u5b58\u5360\u7528\u3002 \u907f\u514d\u8fd9\u4e2a\u95ee\u9898\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u662f\u5c06\u5168\u5c40\u5e38\u91cf\u5b9a\u4e49\u4e3a \u5916\u90e8\u53d8\u91cf \uff0c\u8fd9\u6837\u4e00\u4e2a\u53d8\u91cf\uff08\u521d\u59cb\u5316\u4e00\u6b21\uff09\u5c31\u53ef\u4ee5\u88ab\u6240\u6709\u7684\u6587\u4ef6\u201c\u5171\u4eab\u4f7f\u7528\u201d\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5fc5\u987b\u5c06\u5e38\u91cf\u5b9a\u4e49\u5728\u4e00\u4e2a a.cpp \u6587\u4ef6\u4e2d\uff08\u786e\u4fdd\u5b83\u53ea\u88ab\u5b9a\u4e49\u4e00\u6b21\uff09\uff0c\u7136\u540e\u5728\u5934\u6587\u4ef6\u4e2d\u6dfb\u52a0\u5176\u524d\u5411\u5b9a\u4e49\uff08\u8be5\u5934\u6587\u4ef6\u4ecd\u9700\u8981\u88ab\u5305\u542b\u5230\u4f7f\u7528\u4e86\u8be5\u53d8\u91cf\u7684\u6587\u4ef6\u4e2d\uff09\u3002 \u4f5c\u8005\u6ce8 \u8fd9\u91cc\u4f7f\u7528\u4e86 const \u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u8fd9\u662f\u56e0\u4e3a constexpr \u7c7b\u578b\u7684\u53d8\u91cf\u4e0d\u80fd\u88ab\u524d\u5411\u58f0\u660e\uff08 6-7-External-linkage-and-variable-forward-declarations#^ce6263 \uff09\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u9700\u8981\u5728\u7f16\u8bd1\u65f6\u77e5\u9053 constexpr \u7c7b\u578b\u53d8\u91cf\u7684\u5b9a\u4e49\uff0c\u800c\u524d\u5411\u58f0\u660e\u5e76\u4e0d\u80fd\u63d0\u4f9b\u5b9a\u4e49\u7684\u4fe1\u606f\u3002 constants.cpp 1 2 3 4 5 6 7 8 9 #include \"constants.h\" namespace constants { // actual global variables extern const double pi { 3.14159 }; extern const double avogadro { 6.0221413e23 }; extern const double myGravity { 9.2 }; // m/s^2 -- gravity is light on this planet } constants.h 1 2 3 4 5 6 7 8 9 10 11 12 #ifndef CONSTANTS_H #define CONSTANTS_H namespace constants { // since the actual variables are inside a namespace, the forward declarations need to be inside a namespace as well extern const double pi ; extern const double avogadro ; extern const double myGravity ; } #endif \u4f7f\u7528\u65b9\u6cd5\u548c\u4e4b\u524d\u662f\u4e00\u6837\u7684\uff1a main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include \"constants.h\" // include all the forward declarations #include int main () { std :: cout << \"Enter a radius: \" ; int radius {}; std :: cin >> radius ; std :: cout << \"The circumference is: \" << 2.0 * radius * constants :: pi << '\\n' ; return 0 ; } \u56e0\u4e3a\u5168\u5c40\u7b26\u53f7\u5e38\u91cf\u5e94\u8be5\u88ab\u653e\u7f6e\u5728\u547d\u540d\u7a7a\u95f4\u4e2d\uff08\u907f\u514d\u4e0e\u5176\u4ed6\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u4ea7\u751f\u547d\u540d\u51b2\u7a81\uff09\uff0c\u6240\u4ee5\u6ca1\u5fc5\u8981\u5bf9\u5176\u4f7f\u7528 g_ \u524d\u7f00\u3002 \u8fd9\u6837\u4e00\u6765\uff0c\u7b26\u53f7\u5e38\u91cf\u53ea\u4f1a\u5728 constants.cpp \u4e2d\u88ab\u521d\u59cb\u5316\u4e00\u6b21\uff0c\u800c\u4e0d\u662f\u5728\u6bcf\u4e2a constants.h \u88ab\u5305\u542b\u7684\u5730\u65b9\u88ab\u521d\u59cb\u5316\u3002\u540c\u65f6\uff0c\u8fd9\u4e9b\u5e38\u6570\u90fd\u4f1a\u88ab\u94fe\u63a5\u5230 constants.cpp \u4e2d\u521d\u59cb\u5316\u7684\u53d8\u91cf\uff0c\u56e0\u6b64\u5f53\u8fd9\u4e9b\u53d8\u91cf\u88ab\u4fee\u6539\u65f6\uff0c\u53ea\u6709 constants.cpp \u9700\u8981\u88ab\u91cd\u65b0\u7f16\u8bd1\u3002 \u4e0d\u8fc7\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u4e0d\u662f\u5b8c\u7f8e\u7684\u3002\u9996\u5148\uff0c\u8fd9\u4e9b\u5e38\u91cf\u73b0\u5728\u53ea\u4f1a\u5728\u5b83\u4eec\u88ab\u5b9a\u4e49\u7684\u6587\u4ef6\u4e2d\u88ab\u770b\u505a \u7f16\u8bd1\u65f6 \u5e38\u91cf\u3002\u800c\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\uff0c\u7f16\u8bd1\u5668\u53ea\u80fd\u770b\u5230\u5b83\u4eec\u7684\u58f0\u660e\uff0c\u800c\u770b\u4e0d\u5230\u5e38\u91cf\u5177\u4f53\u7684\u4e2d\uff08\u5fc5\u987b\u6709\u94fe\u63a5\u5668\u8fdb\u884c\u89e3\u6790\uff09\u3002\u56e0\u6b64\uff0c\u5728 constants.cpp \u4ee5\u5916\u7684\u5730\u65b9\uff0c\u8fd9\u4e9b\u5e38\u91cf\u90fd\u4e0d\u80fd\u88ab\u770b\u505a\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\uff0c\u4e5f\u5c31\u4e0d\u80fd\u5728\u8981\u6c42\u4f7f\u7528\u7f16\u8bd1\u65f6\u5e38\u91cf\u7684\u5730\u65b9\u4f7f\u7528\u5b83\u4eec\u3002\u53e6\u5916\uff0c\u56e0\u4e3a\u7f16\u8bd1\u65f6\u5e38\u91cf\u76f8\u5bf9\u4e8e\u8fd0\u884c\u65f6\u5e38\u91cf\u66f4\u5bb9\u6613\u4f18\u5316\uff0c\u6240\u4ee5\u5bf9\u4e8e\u8fd9\u4e9b\u8fd0\u884c\u65f6\u5e38\u91cf\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u65e0\u6cd5\u5bf9\u5176\u8fdb\u884c\u8db3\u591f\u7684\u4f18\u5316\u3002 \u5173\u952e\u4fe1\u606f \u4e3a\u4e86\u5728\u7f16\u8bd1\u65f6\u4f7f\u7528\u67d0\u4e9b\u53d8\u91cf\uff08\u4f8b\u5982\uff0c\u6570\u7ec4\u5927\u5c0f\uff09\uff0c\u7f16\u8bd1\u5668\u5fc5\u987b\u770b\u5230\u53d8\u91cf\u7684\u5b9a\u4e49\uff08\u800c\u4e0d\u4ec5\u4ec5\u662f\u524d\u9879\u58f0\u660e\uff09\u3002 \u56e0\u4e3a\u7f16\u8bd1\u5668\u662f\u72ec\u7acb\u7f16\u8bd1\u5404\u4e2a\u6e90\u6587\u4ef6\u7684\uff0c\u56e0\u6b64\u5b83\u53ea\u80fd\u5bf9\u6e90\u6587\u4ef6\u4e2d\u5b58\u5728\u5b9a\u4e49\u7684\u53d8\u91cf\u8fdb\u884c\u7f16\u8bd1\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49\u5728 constants.cpp \u7684\u53d8\u91cf\uff0c\u5728\u7f16\u8bd1\u5668\u7f16\u8bd1 main.cpp \u7684\u65f6\u5019\u662f\u4e0d\u53ef\u89c1\u7684\uff0c\u51fa\u4e8e\u8fd9\u4e2a\u539f\u56e0 constexpr \u7684\u5b9a\u4e49\u548c\u58f0\u660e\u4e0d\u80fd\u88ab\u5206\u6563\u5230\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u4e2d\uff0c\u5b83\u5fc5\u987b\u88ab\u5b9a\u4e49\u5728\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\u91cc\u3002 \u51fa\u4e8e\u4e0a\u8ff0\u539f\u56e0\uff0c\u6211\u4eec\u8fd8\u662f\u5efa\u8bae\u6700\u597d\u5c06\u5e38\u91cf\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u3002\u5982\u679c\u56e0\u6b64\u5bfc\u81f4\u4e86\u67d0\u4e9b\u7279\u5b9a\u7684\u95ee\u9898\uff0c\u4e0d\u59a8\u5230\u65f6\u5019\u5728\u628a\u5b83\u4eec\u4e00\u8d77\u79fb\u52a8\u5230.cpp\u6587\u4ef6\u53bb\u3002","title":"\u5c06\u5168\u5c40\u5e38\u91cf\u4f5c\u4e3a\u5916\u90e8\u53d8\u91cf"},{"location":"6-9-Sharing-global-constants-across-multiple-files-using-inline-variables/#c17","text":"C++17 \u5f15\u5165\u4e86\u4e00\u4e2a\u65b0\u7684\u6982\u5ff5\u2014\u2014 \u5185\u8054\u53d8\u91cf(inline variables) \u3002\u5728 C++ \u4e2d\uff0c[[inline|\u5185\u8054]]\u7684\u610f\u601d\u9010\u6e10\u5f15\u7533\u6210\u4e86\u201c\u5141\u8bb8\u591a\u5904\u5b9a\u4e49\u201d\u3002\u56e0\u6b64\uff0c\u5185\u8054\u53d8\u91cf\u53ef\u4ee5\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u88ab\u591a\u6b21\u5b9a\u4e49\uff0c\u4e14\u4e0d\u4f1a\u8fdd\u53cd \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u3002\u5185\u8054\u7684\u5168\u5c40\u53d8\u91cf\u9ed8\u8ba4\u5177\u6709\u5916\u90e8\u94fe\u63a5\u5c5e\u6027\u3002 \u94fe\u63a5\u5668\u53ef\u4ee5\u5c06\u6240\u6709\u7684\u5185\u8054\u5b9a\u4e49\u5408\u5e76\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u5b9a\u4e49\uff08\u8fd9\u6837\u5c31\u6ee1\u8db3\u4e86\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u5f53\u6211\u4eec\u628a\u5185\u8054\u53d8\u91cf\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u7684\u540c\u65f6\uff0c\u8fd8\u80fd\u591f\u5c06\u5b83\u4eec\u5728.cpp\u6587\u4ef6\u4e2d\u7684\u5b9a\u4e49\u770b\u505a\u662f\u4e00\u4e2a\u5b9a\u4e49\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4e00\u4e2a\u666e\u901a\u7684\u5e38\u91cf\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u540c\u65f6\u88ab #include \u5230\u4e8610\u4e2a\u4e0d\u540c\u7684\u6e90\u6587\u4ef6\u3002\u5982\u679c\u5b83\u4e0d\u662f\u5185\u8054\u7684\uff0c\u5219\u6b64\u65f6\u4f1a\u4ea7\u751f10\u4e2a\u5b9a\u4e49\u3002\u5982\u679c\u662f\u5185\u8054\u7684\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u9009\u53d6\u4e00\u4e2a\u5b9a\u4e49\u4f5c\u4e3a\u4e3b\u5b9a\u4e49\uff0c\u8fd9\u6837\u4e00\u6765\u5c31\u53ea\u4f1a\u4ea7\u751f\u4e00\u4e2a\u5b9a\u4e49\u3002\u8fd9\u6837\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8282\u77019\u4e2a\u5e38\u91cf\u7684\u5185\u5b58\u7a7a\u95f4\u3002 \u8fd9\u4e9b\u53d8\u91cf\u540c\u65f6\u8fd8\u4fdd\u7559\u4e86 constexpr \u7684\u7279\u6027\uff0c\u56e0\u6b64\u5728\u4efb\u4f55\u5305\u542b\u5b83\u4eec\u7684\u6587\u4ef6\u4e2d\uff0c\u53ea\u8981\u662f constexpr \u53ef\u7528\u7684\u5730\u65b9\uff0c\u8fd9\u4e9b\u53d8\u91cf\u4e5f\u53ef\u7528\u3002\u540c\u65f6\uff0c\u76f8\u6bd4\u8f83\u4e8e\u8fd0\u884c\u65f6\u5e38\u91cf\uff08\u6216\u975e\u5e38\u91cf\uff09\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u6781\u5927\u5730\u4f18\u5316 constexpr \u7c7b\u578b\u7684\u53d8\u91cf \u5185\u8054\u53d8\u91cf\u6709\u4e24\u4e2a\u4e3b\u8981\u7684\u9650\u5236\uff0c\u9700\u8981\u6211\u4eec\u9075\u5faa\uff1a \u5185\u8054\u53d8\u91cf\u7684\u6240\u6709\u5b9a\u4e49\u5fc5\u987b\u662f\u5b8c\u5168\u4e00\u81f4\u7684\uff08\u5426\u5219\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\uff09\u3002 \u5185\u8054\u53d8\u91cf\u7684\u5b9a\u4e49\uff08\u800c\u975e\u58f0\u660e\uff09\u5fc5\u987b\u51fa\u73b0\u5728\u4efb\u4f55\u4f7f\u7528\u5b83\u4eec\u7684\u6587\u4ef6\u4e2d\u3002 \u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u7ee7\u7eed\u6cbf\u7528\u5c06\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u7684\u65b9\u5f0f\uff0c\u540c\u65f6\u8fd8\u53ef\u4ee5\u907f\u514d\u53d8\u91cf\u7684\u91cd\u590d\u5b9a\u4e49\uff1a constants.h 1 2 3 4 5 6 7 8 9 10 11 12 #ifndef CONSTANTS_H #define CONSTANTS_H // define your own namespace to hold constants namespace constants { inline constexpr double pi { 3.14159 }; // note: now inline constexpr inline constexpr double avogadro { 6.0221413e23 }; inline constexpr double myGravity { 9.2 }; // m/s^2 -- gravity is light on this planet // ... other related constants } #endif main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include \"constants.h\" #include int main () { std :: cout << \"Enter a radius: \" ; int radius {}; std :: cin >> radius ; std :: cout << \"The circumference is: \" << 2.0 * radius * constants :: pi << '\\n' ; return 0 ; } \u5c06 constants.h \u5305\u542b\u5230\u6240\u6709\u9700\u8981\u4f7f\u7528\u5b83\u7684\u4ee3\u7801\u6587\u4ef6\u4e2d\uff0c\u4f46\u662f\u8fd9\u4e9b\u53d8\u91cf\u53ea\u4f1a\u88ab\u521d\u59cb\u5316\u4e00\u6b21\uff0c\u5e76\u4e14\u5728\u6240\u6709\u6587\u4ef6\u4e2d\u5171\u4eab\u4f7f\u7528\u3002 \u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u4ecd\u7136\u4e0d\u80fd\u907f\u514d\u6587\u4ef6\u91cd\u590d\u7f16\u8bd1\u7684\u95ee\u9898\u3002\u5f53\u4efb\u4f55\u5e38\u91cf\u88ab\u4fee\u6539\u65f6\uff0c\u6bcf\u4e2a\u5305\u542b\u5b83\u7684\u6587\u4ef6\u90fd\u9700\u8981\u88ab\u91cd\u65b0\u7f16\u8bd1\u3002\u6240\u4ee5\uff0c\u5982\u679c\u4f60\u7684\u5e38\u91cf\u7ecf\u5e38\u9700\u8981\u4fee\u6539\uff08\u51fa\u4e8e\u8c03\u4f18\u7684\u76ee\u7684\uff09\uff0c\u8fd9\u65e0\u7591\u4f1a\u5bfc\u81f4\u5f88\u957f\u7684\u91cd\u65b0\u7f16\u8bd1\u65f6\u95f4\uff0c\u6b64\u65f6\u4f60\u53ef\u4ee5\u8003\u8651\u5c06\u5e38\u91cf\u5f15\u5165\u5230\u5355\u72ec\u7684\u5934\u6587\u4ef6\u4e2d\uff08\u51cf\u5c11include\u7684\u6570\u91cf\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u652f\u6301 C++17\uff0c\u6700\u597d\u5c06\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u4e3a inline constexpr \u7c7b\u578b\u5e76\u653e\u7f6e\u5728\u5934\u6587\u4ef6\u4e2d\u3002 \u6ce8\u610f \u4f7f\u7528 std::string_view \u5b9a\u4e49 constexpr \u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u5728 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \u4e2d\u8fdb\u884c\u4e86\u4ecb\u7ecd\u3002","title":"\u5c06\u5168\u5c40\u5e38\u91cf\u4f5c\u4e3a\u5185\u8054\u53d8\u91cf\u00a0(C++17)"},{"location":"6-x-Chapter-6-summary-and-quiz/","tags":["summary","C++17","C++20"],"text":"\u7ae0\u8282\u5c0f\u7ed3 \u672c\u7ae0\u7684\u4ecb\u7ecd\u4e86\u5f88\u591a\u5185\u5bb9\u3002\u4f60\u505a\u7684\u5f88\u597d\uff01 6.1 - \u590d\u5408\u8bed\u53e5\uff08\u8bed\u53e5\u5757\uff09 \u590d\u5408\u8bed\u53e5 \u6216\u8005\u8bed\u53e5\u5757\u662f\u4e00\u7ec40\u4e2a\u6216\u591a\u4e2a\u8bed\u53e5\u7ec4\u6210\u7684\u96c6\u5408\uff0c\u5b83\u4eec\u88ab\u7f16\u8bd1\u5668\u770b\u505a\u4e00\u4e2a\u6574\u4f53\uff0c\u5c31\u597d\u50cf\u4e00\u4e2a\u5355\u4e00\u7684\u8bed\u53e5\u4e00\u6837\u3002\u8bed\u53e5\u5757\u4ece { \u7b26\u53f7\u5f00\u59cb\uff0c\u5230 } \u7b26\u53f7\u7ed3\u675f\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u6240\u6709\u7684\u8bed\u53e5\u3002\u5728\u4efb\u4f55\u5141\u8bb8\u4f7f\u7528\u5355\u4e00\u8bed\u53e5\u7684\u5730\u65b9\uff0c\u4e5f\u90fd\u53ef\u4ee5\u4f7f\u7528\u8bed\u53e5\u5757\u3002\u8bed\u53e5\u5757\u7684\u7ed3\u675f\u5904\u4e0d\u9700\u8981\u4f7f\u7528\u5206\u53f7\u3002\u8bed\u53e5\u5757\u901a\u5e38\u4f1a\u548cif\u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\u3002 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u547d\u540d\u7a7a\u95f4 \u6307\u7684\u662f\u4f60\u4e3a\u4e86\u81ea\u5df1\u7684\u58f0\u660e\u800c\u521b\u5efa\u7684\u547d\u540d\u7a7a\u95f4\u3002\u7531C++\uff08\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff09\u6216\u6807\u51c6\u5e93\uff08 std \u547d\u540d\u7a7a\u95f4\uff09\u63d0\u4f9b\u7684\u547d\u540d\u7a7a\u95f4\u4e0d\u5c5e\u4e8e\u7528\u6237\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\uff1b \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 ( :: )\u6765\u8bbf\u95ee\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u58f0\u660e\u3002\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u544a\u8bc9\u7f16\u8bd1\u5668\u5230\u8fd0\u7b97\u7b26\u5de6\u64cd\u4f5c\u6570\u4e2d\u53bb\u67e5\u627e\u53f3\u64cd\u4f5c\u6570\u3002\u5982\u679c\u4e0d\u63d0\u4f9b\u5de6\u64cd\u4f5c\u6570\uff0c\u5219\u4f1a\u4f7f\u7528\u5168\u5c40\u4f5c\u7528\u57df\u3002 6.3 - \u5c40\u90e8\u53d8\u91cf \u5c40\u90e8\u53d8\u91cf \uff08 6.3 - \u5c40\u90e8\u53d8\u91cf \uff09\u6307\u7684\u662f\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8\u7684\u53d8\u91cf\uff08\u5305\u62ec\u51fd\u6570\u7684 \u5f62\u53c2 \uff09\u3002\u5c40\u90e8\u53d8\u91cf\u5177\u6709\u8bed\u53e5\u5757\u4f5c\u7528\u57df\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u7684\u4f5c\u7528\u57df\u4ece\u53d8\u91cf\u5b9a\u4e49\u5f00\u59cb\uff0c\u5230\u8bed\u53e5\u5757\u7ed3\u675f\u4e3a\u6b62\u3002\u5c40\u90e8\u53d8\u91cf\u5177\u6709 \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u4e5f\u5c31\u662f\u8bf4\u5b83\u5728\u5b9a\u4e49\u65f6\u88ab\u521b\u5efa\uff0c\u5728\u79bb\u5f00\u8bed\u53e5\u5757\u65f6\u88ab\u9500\u6bc1\u3002 6.4 - \u5168\u5c40\u53d8\u91cf \u5168\u5c40\u53d8\u91cf \u6307\u7684\u662f\u5b9a\u4e49\u5728\u51fd\u6570\u5916\u90e8\u7684\u53d8\u91cf\u3002 \u5168\u5c40\u53d8\u91cf\u5177\u6709 \u6587\u4ef6\u4f5c\u7528\u57df \uff0c\u8fd9\u4e5f\u5c31\u610f\u5473\u7740\u5b83\u7684\u53ef\u89c1\u8303\u56f4\u4ece\u53d8\u91cf\u5b9a\u4e49\u5f00\u59cb\u5230\u6587\u4ef6\u7ed3\u675f\u4e3a\u6b62\u3002\u5168\u5c40\u53d8\u91cf\u5177\u6709 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u4e5f\u662f\u8bf4\u8be5\u53d8\u91cf\u4f1a\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u521b\u5efa\uff0c\u7a0b\u5e8f\u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u5c3d\u53ef\u80fd\u907f\u514d\u5bf9\u9759\u6001\u53d8\u91cf\u7684\u52a8\u6001 \u521d\u59cb\u5316\u3002 6.5 - \u53d8\u91cf\u906e\u853d \u58f0\u660e\u5728\u5d4c\u5957\u8bed\u53e5\u5757\u4e2d\u7684\u53d8\u91cf\u4f1a\u5bf9\u5916\u5c42\u8bed\u53e5\u5757\u4e2d\u7684\u540c\u540d\u53d8\u91cf\u5f62\u6210 \u53d8\u91cf\u906e\u853d \uff0c\u53d8\u91cf\u906e\u853d\u5e94\u5f53\u88ab\u907f\u514d\u3002 6.6 - \u5185\u90e8\u94fe\u63a5 & 6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e \u4e00\u4e2a\u6807\u8bc6\u7b26\u7684** \u94fe\u63a5\u5c5e\u6027 \uff0c\u51b3\u5b9a\u4e86\u8be5\u6807\u8bc6\u7b26\u7684\u5176\u4ed6\u58f0\u660e\u503c\u5f97\u662f\u5426\u90fd\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002\u5c40\u90e8\u53d8\u91cf\u6ca1\u6709\u94fe\u63a5\u5c5e\u6027\u3002\u5177\u6709 \u5185\u90e8\u94fe\u63a5 \u5c5e\u6027\u7684\u6807\u8bc6\u7b26\u53ef\u4ee5\u5728\u6587\u4ef6\u4e2d\u88ab\u8bbf\u95ee\uff0c\u4f46\u662f\u4e0d\u80fd\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u88ab\u8bbf\u95ee\u3002\u5177\u6709 \u5916\u90e8\u94fe\u63a5 \u5c5e\u6027\u7684\u6807\u8bc6\u7b26\u5219\u53ef\u4ee5\u5728\u5b9a\u4e49\u5b83\u7684\u6587\u4ef6\u6216\u5176\u4ed6\u6587\u4ef6\u4e2d\u53ef\u89c1\u6216\u88ab\u8bbf\u95ee\uff08\u901a\u8fc7 \u524d\u5411\u58f0\u660e \uff09\u3002 6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c && 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf \u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u975e const \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u3002const \u5168\u5c40\u53d8\u91cf\u901a\u5e38\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\u3002\u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u652f\u6301C++17\uff0c\u8bf7\u4f7f\u7528 inline \u53d8\u91cf\u5b9a\u4e49\u5168\u5c40\u5e38\u91cf\u3002 6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf \u5c40\u90e8\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7 static \u5173\u952e\u5b57\u8d4b\u4e88\u5176 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u3002 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 using \u8bed\u53e5\u5305\u62ec using\u58f0\u660e \u548c using\u6307\u4ee4 \uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u907f\u514d\u4f7f\u7528\u9650\u5b9a\u6807\u8bc6\u7b26\uff0c\u4f46\u662f\u6211\u4eec\u5e94\u8be5\u907f\u514d\u8fd9\u6837\u505a\u3002 6.13 - \u5185\u8054\u51fd\u6570 \u5185\u8054\u51fd\u6570 \u6700\u521d\u7684\u8bbe\u8ba1\u76ee\u7684\u662f\u63d0\u4f9b\u4e00\u79cd\u65b9\u6cd5\u544a\u8bc9\u7f16\u8bd1\u5668\u5c06\u51fd\u6570\u8c03\u7528\u8fdb\u884c\u5185\u8054\u5c55\u5f00\uff0c\u4f46\u6211\u4eec\u5e76\u4e0d\u5e94\u8be5\u51fa\u4e8e\u8be5\u76ee\u7684\u800c\u4f7f\u7528 inline \u5173\u952e\u5b57\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u4e3a\u4f60\u505a\u51b3\u5b9a\u3002\u5728\u73b0\u4ee3 C++ \u4e2d\uff0c inline \u5173\u952e\u5b57\u7528\u4e8e\u5c06\u51fd\u6570\u6392\u9664\u5230 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u7684\u9650\u5b9a\u4e4b\u5916\uff0c\u4ece\u800c\u4f7f\u5176\u5b9a\u4e49\u53ef\u4ee5\u88ab\u5bfc\u5165\u5230\u591a\u4e2a\u6e90\u6587\u4ef6\u4e2d\u80fd\u591f\u3002\u5185\u8054\u51fd\u6570\u901a\u5e38\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u5b83\u4eec\u7684\u5b9a\u4e49\u5c31\u53ef\u4ee5\u5728 #included \u65f6\u88ab\u4e00\u540c\u62f7\u8d1d\u5230\u6e90\u6587\u4ef6\u4e2d\u3002 6.14 - Constexpr \u548c consteval \u51fd\u6570 constexpr \u51fd\u6570\u7684\u8fd4\u56de\u503c \u53ef\u80fd \u4f1a\u5728 \u7f16\u8bd1\u65f6 \u6c42\u503c\u3002\u4f7f\u7528 constexpr \u5173\u952e\u5b57\u53ef\u4ee5\u5c06\u51fd\u6570\u5b9a\u4e49\u4e3a constexpr \u7c7b\u578b\u3002 constexpr \u51fd\u6570 \u6709\u8d44\u683c \u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u800c\u5982\u679c\u5b83\u7684\u8fd4\u56de\u503c\u88ab\u4f7f\u7528\u5728\u9700\u8981\u5e38\u91cf\u7684\u4e0a\u4e0b\u6587\u4e2d\u65f6\uff0c\u5219\u4e00\u5b9a\u4f1a\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u5219\u53ef\u4ee5\u81ea\u7531\u9009\u62e9\u662f\u5728\u7f16\u8bd1\u65f6\u8fd8\u662f\u8fd0\u884c\u65f6\u5bf9\u51fd\u6570\u8fdb\u884c\u6c42\u503c\u3002 C++20 \u5f15\u5165\u4e86\u65b0\u7684\u5173\u952e\u5b57 consteval \uff0c\u5b83\u53ef\u4ee5\u8981\u6c42\u51fd\u6570\u5fc5\u987b\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u5426\u5219\u5c31\u4f1a\u4ea7\u751f\u7f16\u8bd1\u62a5\u9519\u3002\u8fd9\u79cd\u51fd\u6570\u79f0\u4e3a \u5373\u65f6\u51fd\u6570 \u3002 6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4 \u6700\u540e\uff0cC++ \u652f\u6301 \u533f\u540d\u547d\u540d\u7a7a\u95f4 \uff0c\u5b83\u9690\u542b\u5730\u5c06\u5176\u5185\u90e8\u5b9a\u4e49\u7684\u5185\u5bb9\u8d4b\u4e88 \u5185\u90e8\u94fe\u63a5 \u3002 C++ \u540c\u6837\u8fd8\u652f\u6301 \u5185\u8054\u547d\u540d\u7a7a\u95f4 \uff0c\u5b83\u4e3a\u547d\u540d\u7a7a\u95f4\u63d0\u4f9b\u4e86\u539f\u751f\u7684\u7248\u672c\u63a7\u5236\u673a\u5236\u3002 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3","title":"6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5"},{"location":"6-x-Chapter-6-summary-and-quiz/#_1","text":"\u672c\u7ae0\u7684\u4ecb\u7ecd\u4e86\u5f88\u591a\u5185\u5bb9\u3002\u4f60\u505a\u7684\u5f88\u597d\uff01 6.1 - \u590d\u5408\u8bed\u53e5\uff08\u8bed\u53e5\u5757\uff09 \u590d\u5408\u8bed\u53e5 \u6216\u8005\u8bed\u53e5\u5757\u662f\u4e00\u7ec40\u4e2a\u6216\u591a\u4e2a\u8bed\u53e5\u7ec4\u6210\u7684\u96c6\u5408\uff0c\u5b83\u4eec\u88ab\u7f16\u8bd1\u5668\u770b\u505a\u4e00\u4e2a\u6574\u4f53\uff0c\u5c31\u597d\u50cf\u4e00\u4e2a\u5355\u4e00\u7684\u8bed\u53e5\u4e00\u6837\u3002\u8bed\u53e5\u5757\u4ece { \u7b26\u53f7\u5f00\u59cb\uff0c\u5230 } \u7b26\u53f7\u7ed3\u675f\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u6240\u6709\u7684\u8bed\u53e5\u3002\u5728\u4efb\u4f55\u5141\u8bb8\u4f7f\u7528\u5355\u4e00\u8bed\u53e5\u7684\u5730\u65b9\uff0c\u4e5f\u90fd\u53ef\u4ee5\u4f7f\u7528\u8bed\u53e5\u5757\u3002\u8bed\u53e5\u5757\u7684\u7ed3\u675f\u5904\u4e0d\u9700\u8981\u4f7f\u7528\u5206\u53f7\u3002\u8bed\u53e5\u5757\u901a\u5e38\u4f1a\u548cif\u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\u3002 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u547d\u540d\u7a7a\u95f4 \u6307\u7684\u662f\u4f60\u4e3a\u4e86\u81ea\u5df1\u7684\u58f0\u660e\u800c\u521b\u5efa\u7684\u547d\u540d\u7a7a\u95f4\u3002\u7531C++\uff08\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff09\u6216\u6807\u51c6\u5e93\uff08 std \u547d\u540d\u7a7a\u95f4\uff09\u63d0\u4f9b\u7684\u547d\u540d\u7a7a\u95f4\u4e0d\u5c5e\u4e8e\u7528\u6237\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\uff1b \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 ( :: )\u6765\u8bbf\u95ee\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u58f0\u660e\u3002\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u544a\u8bc9\u7f16\u8bd1\u5668\u5230\u8fd0\u7b97\u7b26\u5de6\u64cd\u4f5c\u6570\u4e2d\u53bb\u67e5\u627e\u53f3\u64cd\u4f5c\u6570\u3002\u5982\u679c\u4e0d\u63d0\u4f9b\u5de6\u64cd\u4f5c\u6570\uff0c\u5219\u4f1a\u4f7f\u7528\u5168\u5c40\u4f5c\u7528\u57df\u3002 6.3 - \u5c40\u90e8\u53d8\u91cf \u5c40\u90e8\u53d8\u91cf \uff08 6.3 - \u5c40\u90e8\u53d8\u91cf \uff09\u6307\u7684\u662f\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8\u7684\u53d8\u91cf\uff08\u5305\u62ec\u51fd\u6570\u7684 \u5f62\u53c2 \uff09\u3002\u5c40\u90e8\u53d8\u91cf\u5177\u6709\u8bed\u53e5\u5757\u4f5c\u7528\u57df\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u7684\u4f5c\u7528\u57df\u4ece\u53d8\u91cf\u5b9a\u4e49\u5f00\u59cb\uff0c\u5230\u8bed\u53e5\u5757\u7ed3\u675f\u4e3a\u6b62\u3002\u5c40\u90e8\u53d8\u91cf\u5177\u6709 \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u4e5f\u5c31\u662f\u8bf4\u5b83\u5728\u5b9a\u4e49\u65f6\u88ab\u521b\u5efa\uff0c\u5728\u79bb\u5f00\u8bed\u53e5\u5757\u65f6\u88ab\u9500\u6bc1\u3002 6.4 - \u5168\u5c40\u53d8\u91cf \u5168\u5c40\u53d8\u91cf \u6307\u7684\u662f\u5b9a\u4e49\u5728\u51fd\u6570\u5916\u90e8\u7684\u53d8\u91cf\u3002 \u5168\u5c40\u53d8\u91cf\u5177\u6709 \u6587\u4ef6\u4f5c\u7528\u57df \uff0c\u8fd9\u4e5f\u5c31\u610f\u5473\u7740\u5b83\u7684\u53ef\u89c1\u8303\u56f4\u4ece\u53d8\u91cf\u5b9a\u4e49\u5f00\u59cb\u5230\u6587\u4ef6\u7ed3\u675f\u4e3a\u6b62\u3002\u5168\u5c40\u53d8\u91cf\u5177\u6709 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u4e5f\u662f\u8bf4\u8be5\u53d8\u91cf\u4f1a\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u521b\u5efa\uff0c\u7a0b\u5e8f\u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u5c3d\u53ef\u80fd\u907f\u514d\u5bf9\u9759\u6001\u53d8\u91cf\u7684\u52a8\u6001 \u521d\u59cb\u5316\u3002 6.5 - \u53d8\u91cf\u906e\u853d \u58f0\u660e\u5728\u5d4c\u5957\u8bed\u53e5\u5757\u4e2d\u7684\u53d8\u91cf\u4f1a\u5bf9\u5916\u5c42\u8bed\u53e5\u5757\u4e2d\u7684\u540c\u540d\u53d8\u91cf\u5f62\u6210 \u53d8\u91cf\u906e\u853d \uff0c\u53d8\u91cf\u906e\u853d\u5e94\u5f53\u88ab\u907f\u514d\u3002 6.6 - \u5185\u90e8\u94fe\u63a5 & 6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e \u4e00\u4e2a\u6807\u8bc6\u7b26\u7684** \u94fe\u63a5\u5c5e\u6027 \uff0c\u51b3\u5b9a\u4e86\u8be5\u6807\u8bc6\u7b26\u7684\u5176\u4ed6\u58f0\u660e\u503c\u5f97\u662f\u5426\u90fd\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002\u5c40\u90e8\u53d8\u91cf\u6ca1\u6709\u94fe\u63a5\u5c5e\u6027\u3002\u5177\u6709 \u5185\u90e8\u94fe\u63a5 \u5c5e\u6027\u7684\u6807\u8bc6\u7b26\u53ef\u4ee5\u5728\u6587\u4ef6\u4e2d\u88ab\u8bbf\u95ee\uff0c\u4f46\u662f\u4e0d\u80fd\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u88ab\u8bbf\u95ee\u3002\u5177\u6709 \u5916\u90e8\u94fe\u63a5 \u5c5e\u6027\u7684\u6807\u8bc6\u7b26\u5219\u53ef\u4ee5\u5728\u5b9a\u4e49\u5b83\u7684\u6587\u4ef6\u6216\u5176\u4ed6\u6587\u4ef6\u4e2d\u53ef\u89c1\u6216\u88ab\u8bbf\u95ee\uff08\u901a\u8fc7 \u524d\u5411\u58f0\u660e \uff09\u3002 6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c && 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf \u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u975e const \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u3002const \u5168\u5c40\u53d8\u91cf\u901a\u5e38\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\u3002\u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u652f\u6301C++17\uff0c\u8bf7\u4f7f\u7528 inline \u53d8\u91cf\u5b9a\u4e49\u5168\u5c40\u5e38\u91cf\u3002 6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf \u5c40\u90e8\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7 static \u5173\u952e\u5b57\u8d4b\u4e88\u5176 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u3002 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 using \u8bed\u53e5\u5305\u62ec using\u58f0\u660e \u548c using\u6307\u4ee4 \uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u907f\u514d\u4f7f\u7528\u9650\u5b9a\u6807\u8bc6\u7b26\uff0c\u4f46\u662f\u6211\u4eec\u5e94\u8be5\u907f\u514d\u8fd9\u6837\u505a\u3002 6.13 - \u5185\u8054\u51fd\u6570 \u5185\u8054\u51fd\u6570 \u6700\u521d\u7684\u8bbe\u8ba1\u76ee\u7684\u662f\u63d0\u4f9b\u4e00\u79cd\u65b9\u6cd5\u544a\u8bc9\u7f16\u8bd1\u5668\u5c06\u51fd\u6570\u8c03\u7528\u8fdb\u884c\u5185\u8054\u5c55\u5f00\uff0c\u4f46\u6211\u4eec\u5e76\u4e0d\u5e94\u8be5\u51fa\u4e8e\u8be5\u76ee\u7684\u800c\u4f7f\u7528 inline \u5173\u952e\u5b57\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u4e3a\u4f60\u505a\u51b3\u5b9a\u3002\u5728\u73b0\u4ee3 C++ \u4e2d\uff0c inline \u5173\u952e\u5b57\u7528\u4e8e\u5c06\u51fd\u6570\u6392\u9664\u5230 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u7684\u9650\u5b9a\u4e4b\u5916\uff0c\u4ece\u800c\u4f7f\u5176\u5b9a\u4e49\u53ef\u4ee5\u88ab\u5bfc\u5165\u5230\u591a\u4e2a\u6e90\u6587\u4ef6\u4e2d\u80fd\u591f\u3002\u5185\u8054\u51fd\u6570\u901a\u5e38\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u5b83\u4eec\u7684\u5b9a\u4e49\u5c31\u53ef\u4ee5\u5728 #included \u65f6\u88ab\u4e00\u540c\u62f7\u8d1d\u5230\u6e90\u6587\u4ef6\u4e2d\u3002 6.14 - Constexpr \u548c consteval \u51fd\u6570 constexpr \u51fd\u6570\u7684\u8fd4\u56de\u503c \u53ef\u80fd \u4f1a\u5728 \u7f16\u8bd1\u65f6 \u6c42\u503c\u3002\u4f7f\u7528 constexpr \u5173\u952e\u5b57\u53ef\u4ee5\u5c06\u51fd\u6570\u5b9a\u4e49\u4e3a constexpr \u7c7b\u578b\u3002 constexpr \u51fd\u6570 \u6709\u8d44\u683c \u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u800c\u5982\u679c\u5b83\u7684\u8fd4\u56de\u503c\u88ab\u4f7f\u7528\u5728\u9700\u8981\u5e38\u91cf\u7684\u4e0a\u4e0b\u6587\u4e2d\u65f6\uff0c\u5219\u4e00\u5b9a\u4f1a\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u5219\u53ef\u4ee5\u81ea\u7531\u9009\u62e9\u662f\u5728\u7f16\u8bd1\u65f6\u8fd8\u662f\u8fd0\u884c\u65f6\u5bf9\u51fd\u6570\u8fdb\u884c\u6c42\u503c\u3002 C++20 \u5f15\u5165\u4e86\u65b0\u7684\u5173\u952e\u5b57 consteval \uff0c\u5b83\u53ef\u4ee5\u8981\u6c42\u51fd\u6570\u5fc5\u987b\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u5426\u5219\u5c31\u4f1a\u4ea7\u751f\u7f16\u8bd1\u62a5\u9519\u3002\u8fd9\u79cd\u51fd\u6570\u79f0\u4e3a \u5373\u65f6\u51fd\u6570 \u3002 6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4 \u6700\u540e\uff0cC++ \u652f\u6301 \u533f\u540d\u547d\u540d\u7a7a\u95f4 \uff0c\u5b83\u9690\u542b\u5730\u5c06\u5176\u5185\u90e8\u5b9a\u4e49\u7684\u5185\u5bb9\u8d4b\u4e88 \u5185\u90e8\u94fe\u63a5 \u3002 C++ \u540c\u6837\u8fd8\u652f\u6301 \u5185\u8054\u547d\u540d\u7a7a\u95f4 \uff0c\u5b83\u4e3a\u547d\u540d\u7a7a\u95f4\u63d0\u4f9b\u4e86\u539f\u751f\u7684\u7248\u672c\u63a7\u5236\u673a\u5236\u3002 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3","title":"\u7ae0\u8282\u5c0f\u7ed3"},{"location":"7-1-Control-flow-introduction/","tags":["control flow"],"text":"Key Takeaway \u5f53\u7a0b\u5e8f\u5f00\u59cb\u8fd0\u884c\u65f6\uff0cCPU\u4ece main() \u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\u6307\u4ee4\uff08\u9ed8\u8ba4\u6309\u7167\u987a\u5e8f\u6267\u884c\uff09\uff0c\u5f53\u5230\u8fbe main() \u51fd\u6570\u672b\u5c3e\u65f6\uff0c\u7a0b\u5e8f\u505c\u6b62\u3002CPU\u6267\u884c\u8bed\u53e5\u7684\u7279\u5b9a\u987a\u5e8f\uff0c\u79f0\u4e3a\u6267\u884c\u8def\u5f84\uff08\u7b80\u79f0\u8def\u5f84\uff09\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"You entered \" << x ; return 0 ; } \u8be5\u7a0b\u5e8f\u7684\u6267\u884c\u8def\u5f84\u4f9d\u6b21\u5305\u62ec\u7b2c5\u30017\u30018\u300110\u548c12\u884c\u3002\u8fd9\u662f\u4e00\u4e2a\u76f4\u7ebf\u7a0b\u5e8f\u7684\u4f8b\u5b50\u3002\u76f4\u7ebf\u7a0b\u5e8f\u6bcf\u6b21\u8fd0\u884c\u65f6\u90fd\u91c7\u7528\u76f8\u540c\u7684\u8def\u5f84(\u4ee5\u76f8\u540c\u7684\u987a\u5e8f\u6267\u884c\u76f8\u540c\u7684\u8bed\u53e5)\u3002 \u7136\u800c\uff0c\u8fd9\u5f80\u5f80\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u8981\u6c42\u7528\u6237\u8f93\u5165\uff0c\u800c\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u65e0\u6548\uff0c\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u8981\u6c42\u7528\u6237\u505a\u51fa\u53e6\u4e00\u4e2a\u9009\u62e9\u3002\u8fd9\u5728\u76f4\u7ebf\u987a\u5e8f\u6267\u884c\u7684\u7a0b\u5e8f\u4e2d\u8fd9\u662f\u4e0d\u53ef\u80fd\u7684\u3002\u4e8b\u5b9e\u4e0a\uff0c\u7528\u6237\u53ef\u80fd\u4f1a\u91cd\u590d\u8f93\u5165\u65e0\u6548\u7684\u8f93\u5165\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u80fd\u9700\u8981\u8981\u6c42\u4ed6\u4eec\u505a\u51fa\u53e6\u4e00\u4e2a\u9009\u62e9\u7684\u6b21\u6570\u76f4\u5230\u8fd0\u884c\u65f6\u624d\u77e5\u9053\u3002 \u5e78\u8fd0\u7684\u662f\uff0cC++\u63d0\u4f9b\u4e86\u8bb8\u591a\u4e0d\u540c\u7684\u63a7\u5236\u6d41\u8bed\u53e5(\u4e5f\u79f0\u4e3a\u6d41\u63a7\u5236\u8bed\u53e5)\uff0c\u8fd9\u4e9b\u8bed\u53e5\u5141\u8bb8\u7a0b\u5e8f\u5458\u901a\u8fc7\u7a0b\u5e8f\u6539\u53d8\u6b63\u5e38\u7684\u6267\u884c\u8def\u5f84\u3002\u4f60\u5df2\u7ecf\u770b\u5230\u4e86\u4e00\u4e2a\u5173\u4e8e\u201cif\u8bed\u53e5\u201d\u7684\u4f8b\u5b50(\u5728\u8bfe\u7a0b 4.10 -if\u8bed\u53e5\u7b80\u4ecb \u4e2d\u4ecb\u7ecd)\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u53ea\u5728\u6761\u4ef6\u8868\u8fbe\u5f0f\u4e3a\u771f\u65f6\u6267\u884c\u8bed\u53e5\u3002 \u5f53\u201c\u63a7\u5236\u6d41\u8bed\u53e5\u201d\u5bfc\u81f4\u6267\u884c\u70b9\u66f4\u6539\u4e3a\u975e\u987a\u5e8f\u8bed\u53e5\u65f6\uff0c\u8fd9\u79f0\u4e3a\u5206\u652f\u3002 \u63a7\u5236\u8bed\u53e5\u7684\u5206\u7c7b\uff1a \u5206\u7c7b \u542b\u4e49 \u5b9e\u73b0 \u6761\u4ef6\u8bed\u53e5 \u6761\u4ef6\u8bed\u53e5\u53ea\u5728\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u65f6\u6267\u884c\u4ee3\u7801\u5e8f\u5217 if , switch \u8df3\u8f6c \u8df3\u8f6c\u544a\u8bc9CPU\u4ece\u5176\u4ed6\u4f4d\u7f6e\u5f00\u59cb\u6267\u884c\u8bed\u53e5. Goto , break , continue \u51fd\u6570\u8c03\u7528 \u51fd\u6570\u8c03\u7528\u4f1a\u8df3\u8f6c\u5230\u5176\u4ed6\u5730\u65b9\u6267\u884c\u518d\u8fd4\u56de Function calls , return \u5faa\u73af \u5faa\u73af\u544a\u8bc9\u7a0b\u5e8f\u91cd\u590d\u6267\u884c\u67d0\u4e9b\u8bed\u53e50\u6b21\u6216\u591a\u6b21\uff0c\u77e5\u9053\u6ee1\u8db3\u67d0\u79cd\u6761\u4ef6 while , do-while , for , ranged-for \u6302\u8d77 \u544a\u8bc9\u7a0b\u5e8f\u505c\u6b62\u8fd0\u884c std::exit() , std::abort() \u5f02\u5e38 \u5f02\u5e38\u662f\u4e3a\u9519\u8bef\u5904\u7406\u800c\u8bbe\u8ba1\u7684\u4e00\u79cd\u7279\u6b8a\u7684\u6d41\u63a7\u5236\u7ed3\u6784 try , throw , catch \u6211\u4eec\u4f1a\u5728\u672c\u7ae0\u4ecb\u7ecd\u9664\u5f02\u5e38\u4ee5\u5916\u7684\u5176\u4ed6\u5168\u90e8\u7c7b\u522b\u7684\u63a7\u5236\u8bed\u53e5\u3002 \u5728\u5b66\u4e60\u672c\u7ae0\u524d\uff0c\u4f60\u53ef\u4ee5\u8ba9\u4e00\u4e2a\u7a0b\u5e8f\u505a\u7684\u4e8b\u60c5\u662f\u76f8\u5f53\u6709\u9650\u7684\u3002\u5728\u5b66\u4f1a\u63a7\u5236\u7a0b\u5e8f\u7684\u6d41\u7a0b(\u7279\u522b\u662f\u4f7f\u7528\u5faa\u73af)\u540e\uff0c\u53ef\u4ee5\u505a\u8bb8\u591a\u6709\u8da3\u7684\u4e8b\u60c5\uff01\u6211\u4eec\u5c06\u4e0d\u518d\u5c40\u9650\u4e8e\u73a9\u5177\u7a0b\u5e8f\u548c\u5c0f\u7ec3\u4e60\u2014\u2014\u4f60\u5c06\u80fd\u591f\u7f16\u5199\u771f\u6b63\u6709\u7528\u7684\u7a0b\u5e8f\u3002 \u8fd9\u624d\u662f\u771f\u6b63\u6709\u8da3\u7684\u5f00\u59cb\u3002\u6240\u4ee5\u8ba9\u6211\u4eec\u7ee7\u7eed\u5427\uff01","title":"7.1 - \u63a7\u5236\u6d41"},{"location":"7-10-Break-and-continue/","text":"title: 7.10 - break \u548c continue alias: 7.10 - break \u548c continue origin: /Break-and-continue/ origin_title: \"7.10 - Break and continue\" time: type: translation tags: - break - continue Break \u8bed\u53e5 \u867d\u7136\u60a8\u5df2\u7ecf\u5728 switch \u8bed\u53e5\u7684\u4e0a\u4e0b\u6587\u4e2d\u89c1\u8fc7 break \u8bed\u53e5( 7.4 - switch \u8bed\u53e5\u57fa\u7840 )\uff0c\u4f46\u5b83\u503c\u5f97\u66f4\u4ed4\u7ec6\u7814\u7a76\uff0c\u56e0\u4e3a\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\u63a7\u5236\u6d41\u8bed\u53e5\u3002break\u8bed\u53e5\u5bfc\u81f4while\u5faa\u73af\u3001do-while\u5faa\u73af\u3001for\u5faa\u73af\u6216switch\u8bed\u53e5\u7ed3\u675f\uff0c\u5e76\u5728\u5faa\u73af\u6216switch\u88ab\u6253\u7834\u540e\u7ee7\u7eed\u6267\u884c\u4e0b\u4e00\u4e2a\u8bed\u53e5\u3002 \u4ece switch \u4e2d\u8df3\u51fa \u5728 switch \u8bed\u53e5\u4e2d\uff0c break \u8bed\u53e5\u901a\u5e38\u653e\u7f6e\u5728\u6bcf\u4e2a\u5206\u652f\u7684\u6700\u540e\uff0c\u7528\u4e8e\u9000\u51faswitch\u8bed\u53e5(\u907f\u514d\u8d2f\u7a7f\u6267\u884c\u5206\u652f)\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include void printMath ( int x , int y , char ch ) { switch ( ch ) { case '+' : std :: cout << x << \" + \" << y << \" = \" << x + y << '\\n' ; break ; // don't fall-through to next case case '-' : std :: cout << x << \" - \" << y << \" = \" << x - y << '\\n' ; break ; // don't fall-through to next case case '*' : std :: cout << x << \" * \" << y << \" = \" << x * y << '\\n' ; break ; // don't fall-through to next case case '/' : std :: cout << x << \" / \" << y << \" = \" << x / y << '\\n' ; break ; } } int main () { printMath ( 2 , 3 , '+' ); return 0 ; } \u5173\u4e8e \u8d2f\u7a7f\u5c5e\u6027 \u7684\u66f4\u591a\u8be6\u7ec6\u4fe1\u606f\uff0c\u53c2\u89c1 7.5 - switch fallthrough\u5c5e\u6027\u548c\u4f5c\u7528\u57df \u3002 \u4ece\u5faa\u73af\u4e2d\u8df3\u51fa \u5728\u5faa\u73af\u4e0a\u4e0b\u6587\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528break\u8bed\u53e5\u63d0\u524d\u7ed3\u675f\u5faa\u73af\u5e76\u4ece\u5faa\u73af\u7ed3\u675f\u540e\u7684\u4e0b\u4e00\u6761\u8bed\u53e5\u7ee7\u7eed\u6267\u884c\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include int main () { int sum { 0 }; // Allow the user to enter up to 10 numbers for ( int count { 0 }; count < 10 ; ++ count ) { std :: cout << \"Enter a number to add, or 0 to exit: \" ; int num {}; std :: cin >> num ; // exit loop if user enters 0 if ( num == 0 ) break ; // exit the loop now // otherwise add number to our sum sum += num ; } // execution will continue here after the break std :: cout << \"The sum of all the numbers you entered is: \" << sum << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u5141\u8bb8\u7528\u6237\u6700\u591a\u8f93\u516510\u4e2a\u6570\u5b57\uff0c\u5e76\u663e\u793a\u6700\u540e\u8f93\u5165\u7684\u6240\u6709\u6570\u5b57\u7684\u603b\u548c\u3002\u5982\u679c\u7528\u6237\u8f93\u51650\uff0c\u4e2d\u65ad\u5c06\u5bfc\u81f4\u5faa\u73af\u63d0\u524d\u7ec8\u6b62(\u5728\u8f93\u516510\u4e2a\u6570\u5b57\u4e4b\u524d)\u3002 \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 Enter a number to add, or 0 to exit: 5 Enter a number to add, or 0 to exit: 2 Enter a number to add, or 0 to exit: 1 Enter a number to add, or 0 to exit: 0 The sum of all the numbers you entered is: 8 break \u4e5f\u662f\u4e00\u79cd\u4ece\u4e00\u4e2a\u6545\u610f\u8bbe\u7f6e\u7684\u65e0\u9650\u5faa\u73af\u4e2d\u8df3\u51fa\u7684\u5e38\u89c1\u65b9\u6cd5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { while ( true ) // infinite loop { std :: cout << \"Enter 0 to exit or any other integer to continue: \" ; int num {}; std :: cin >> num ; // exit loop if user enters 0 if ( num == 0 ) break ; } std :: cout << \"We're out! \\n \" ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 Enter 0 to exit or any other integer to continue: 5 Enter 0 to exit or any other integer to continue: 3 Enter 0 to exit or any other integer to continue: 0 We're out! break vs return \u65b0\u7a0b\u5e8f\u5458\u6709\u65f6\u5f88\u96be\u7406\u89e3\u201cbreak\u201d\u548c\u201creturn\u201d\u4e4b\u95f4\u7684\u533a\u522b\u3002break\u8bed\u53e5\u7ec8\u6b62switch\u6216\u5faa\u73af\uff0c\u5e76\u4eceswitch\u6216\u5faa\u73af\u4ee5\u5916\u7684\u7b2c\u4e00\u4e2a\u8bed\u53e5\u7ee7\u7eed\u6267\u884c\u3002return\u8bed\u53e5\u4f1a\u7ec8\u6b62\u5faa\u73af\u6240\u5728\u7684\u6574\u4e2a\u51fd\u6570\uff0c\u5e76\u5728\u51fd\u6570\u88ab\u8c03\u7528\u7684\u5730\u65b9\u7ee7\u7eed\u6267\u884c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include int breakOrReturn () { while ( true ) // infinite loop { std :: cout << \"Enter 'b' to break or 'r' to return: \" ; char ch ; std :: cin >> ch ; if ( ch == 'b' ) break ; // execution will continue at the first statement beyond the loop if ( ch == 'r' ) return 1 ; // return will cause the function to immediately return to the caller (in this case, main()) } // breaking the loop causes execution to resume here std :: cout << \"We broke out of the loop \\n \" ; return 0 ; } int main () { int returnValue { breakOrReturn () }; std :: cout << \"Function breakOrReturn returned \" << returnValue << '\\n' ; return 0 ; } \u8fd0\u884c\u51fd\u6570\u4e24\u6b21\uff1a 1 2 3 4 5 6 Enter 'b' to break or 'r' to return: r Function breakOrReturn returned 1 Enter 'b' to break or 'r' to return: b We broke out of the loop Function breakOrReturn returned 0 continue \u8bed\u53e5 continue \u8bed\u53e5\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u7684\u65b9\u5f0f\u6765\u7ed3\u675f\u5faa\u73af\u7684 \u5f53\u524d\u8fed\u4ee3 \u800c\u4e0d\u7ec8\u6b62\u6574\u4e2a\u5faa\u73af\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 continue \u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { for ( int count { 0 }; count < 10 ; ++ count ) { // if the number is divisible by 4, skip this iteration if (( count % 4 ) == 0 ) continue ; // go to next iteration // If the number is not divisible by 4, keep going std :: cout << count << '\\n' ; // The continue statement jumps to here } return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u8f93\u51fa\u4ece0\u52309\u6240\u6709\u4e0d\u80fd\u88ab4\u6574\u9664\u7684\u6570\uff1a 1 2 3 4 5 6 7 1 2 3 5 6 7 9 continue \u8bed\u53e5\u4f7f\u5f53\u524d\u6267\u884c\u70b9\u8df3\u8f6c\u5230\u5f53\u524d\u5faa\u73af\u7684\u5e95\u90e8\u3002 \u5728 for \u5faa\u73af\u7684\u4e2d\uff0cfor \u5faa\u73af\u7684\u7ed3\u675f\u8bed\u53e5\u4ecd\u7136\u5728 continue \u8bed\u53e5\u4e4b\u540e\u6267\u884c(\u56e0\u4e3a\u8fd9\u53d1\u751f\u5728\u5faa\u73af\u4f53\u7ed3\u675f\u4e4b\u540e)\u3002 \u5728\u4f7f\u7528\u5e26\u6709 while \u6216 do-while \u5faa\u73af\u7684 continue \u8bed\u53e5\u65f6\u8981\u5c0f\u5fc3\u3002\u8fd9\u4e9b\u5faa\u73af\u901a\u5e38\u4f1a\u66f4\u6539\u5faa\u73af\u4f53\u5185\u90e8\u6761\u4ef6\u4e2d\u4f7f\u7528\u7684\u53d8\u91cf\u7684\u503c\u3002\u5982\u679c\u4f7f\u7528 continue \u8bed\u53e5\u5bfc\u81f4\u8fd9\u4e9b\u884c\u88ab\u8df3\u8fc7\uff0c\u90a3\u4e48\u5faa\u73af\u53ef\u80fd\u4f1a\u53d8\u6210\u65e0\u9650\u5faa\u73af! \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { int count { 0 }; while ( count < 10 ) { if ( count == 5 ) continue ; // \u8df3\u8f6c\u5230\u672b\u5c3e std :: cout << count << '\\n' ; ++ count ; // \u5230\u8fbe 5 \u4e4b\u540e\u518d\u4e5f\u4e0d\u4f1a\u88ab\u6267\u884c // \u4ece\u8fd9\u91cc\u6267\u884c } return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u7684\u76ee\u7684\u662f\u6253\u53700\u52309\u4e4b\u95f4\u96645\u4e4b\u5916\u7684\u6240\u6709\u6570\u5b57\u3002\u4f46\u5b83\u5b9e\u9645\u4e0a\u6253\u5370: 1 2 3 4 5 0 1 2 3 4 \u968f\u540e\u5c31\u8fdb\u5165\u4e86\u65e0\u9650\u5faa\u73af\u3002\u5f53 count \u7b49\u4e8e 5 \u65f6\uff0c if \u8bed\u53e5\u6761\u4ef6\u6c42\u503c\u4e3a true \uff0c continue \u8bed\u53e5\u4f7f\u63a7\u5236\u6d41\u8df3\u8f6c\u5230\u5faa\u73af\u5e95\u90e8\u3002\u6240\u4ee5 count \u53d8\u91cf\u65e0\u6cd5\u88ab\u9012\u589e\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4e0b\u4e00\u6b21\u8fed\u4ee3\u65f6 count \u4ecd\u7136\u662f 5 \uff0c\u6240\u4ee5 if \u8bed\u53e5\u6761\u4ef6\u6c42\u503c\u4ecd\u7136\u4e3a true \uff0c\u7a0b\u5e8f\u53d8\u6210\u4e86\u6b7b\u5faa\u73af\u3002 \u5f53\u7136\uff0c\u5982\u679c\u6211\u4eec\u80fd\u591f\u8bc6\u522b\u51fa\u4e00\u4e2a\u5178\u578b\u7684\u8ba1\u6570\u53d8\u91cf\u7684\u8bdd\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528 for \u5faa\u73af\u6765\u4ee3\u66ff while \u6216 do-while \u5faa\u73af\u3002 \u5173\u4e8e break \u548c continue \u7684\u4e89\u8bba \u8bb8\u591a\u6559\u79d1\u4e66\u544a\u8beb\u8bfb\u8005\u4e0d\u8981\u5728\u5faa\u73af\u4e2d\u4f7f\u7528\u201cbreak\u201d\u548c\u201ccontinue\u201d\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5bfc\u81f4\u6267\u884c\u6d41\u56db\u5904\u8df3\u8dc3\uff0c\u4e5f\u4f1a\u4f7f\u903b\u8f91\u6d41\u66f4\u96be\u9075\u5faa\u3002\u4f8b\u5982\uff0c\u5728\u4e00\u4e2a\u590d\u6742\u7684\u903b\u8f91\u7247\u6bb5\u4e2d\u95f4\u7684\u201c\u4e2d\u65ad\u201d\u53ef\u80fd\u88ab\u9519\u8fc7\uff0c\u6216\u8005\u5728\u4ec0\u4e48\u6761\u4ef6\u4e0b\u5e94\u8be5\u89e6\u53d1\u5b83\u53ef\u80fd\u4e0d\u660e\u663e\u3002 \u7136\u800c\uff0c\u5982\u679c\u4f7f\u7528\u5f97\u5f53\uff0cbreak \u548c continue \u53ef\u4ee5\u51cf\u5c11\u5d4c\u5957\u5757\u7684\u6570\u91cf\uff0c\u51cf\u5c11\u5bf9\u590d\u6742\u5faa\u73af\u903b\u8f91\u7684\u9700\u6c42\uff0c\u4ece\u800c\u4f7f\u5faa\u73af\u66f4\u5177\u53ef\u8bfb\u6027\u3002 \u4f8b\u5982\uff0c\u8003\u8651\u4ee5\u4e0b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include int main () { int count { 0 }; // count how many times the loop iterates bool keepLooping { true }; // controls whether the loop ends or not while ( keepLooping ) { std :: cout << \"Enter 'e' to exit this loop or any other character to continue: \" ; char ch {}; std :: cin >> ch ; if ( ch == 'e' ) keepLooping = false ; else { ++ count ; std :: cout << \"We've iterated \" << count << \" times \\n \" ; } } return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u4f7f\u7528\u4e00\u4e2a\u5e03\u5c14\u53d8\u91cf\u6765\u63a7\u5236\u5faa\u73af\u662f\u5426\u7ee7\u7eed\uff0c\u4ee5\u53ca\u4e00\u4e2a\u53ea\u5728\u7528\u6237\u4e0d\u9000\u51fa\u65f6\u8fd0\u884c\u7684\u5d4c\u5957\u5757\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u66f4\u5bb9\u6613\u7406\u89e3\u7684\u7248\u672c\uff0c\u4f7f\u7528 break \u8bed\u53e5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int main () { int count { 0 }; // count how many times the loop iterates while ( true ) // loop until user terminates { std :: cout << \"Enter 'e' to exit this loop or any other character to continue: \" ; char ch {}; std :: cin >> ch ; if ( ch == 'e' ) break ; ++ count ; std :: cout << \"We've iterated \" << count << \" times \\n \" ; } return 0 ; } \u5728\u8fd9\u4e2a\u7248\u672c\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528\u201cbreak\u8bed\u53e5\u201d\uff0c\u907f\u514d\u4e86\u4f7f\u7528\u5e03\u5c14\u53d8\u91cf(\u5e76\u4e14\u5fc5\u987b\u7406\u89e3\u5b83\u7684\u9884\u671f\u7528\u9014\u4ee5\u53ca\u5b83\u7684\u503c\u5728\u54ea\u91cc\u88ab\u66f4\u6539)\u3001\u201celse\u8bed\u53e5\u201d\u548c\u5d4c\u5957\u5757\u3002 \u5c3d\u91cf\u51cf\u5c11\u4f7f\u7528\u7684\u53d8\u91cf\u6570\u91cf\u548c\u4fdd\u6301\u5d4c\u5957\u5757\u7684\u6570\u91cf\u90fd\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7406\u89e3\u6027\uff0c\u800c\u4e0d\u662f\u201cbreak\u201d\u6216\u201ccontinue\u201d\u5bf9\u4ee3\u7801\u7684\u635f\u5bb3\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u8ba4\u4e3a\u660e\u667a\u5730\u4f7f\u7528\u201cbreak\u201d\u6216\u201ccontinue\u201d\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f7f\u7528break\u548ccontinue\u80fd\u591f\u7b80\u5316\u5faa\u73af\u903b\u8f91\uff0c\u5219\u4f7f\u7528\u5b83\u4eec\u3002 \u5173\u4e8e\u63d0\u524d\u8fd4\u56de\u7684\u4e89\u8bba \u5bf9\u4e8e return \u8bed\u53e5\u4e5f\u6709\u7c7b\u4f3c\u7684\u4e89\u8bba\u3002\u4e0d\u662f\u51fd\u6570\u4e2d\u6700\u540e\u4e00\u6761\u8bed\u53e5\u7684\u8fd4\u56de\u8bed\u53e5\u5219\u79f0\u4e4b\u4e3a \u63d0\u524d\u8fd4\u56de\u8bed\u53e5 \u3002\u8bb8\u591a\u7a0b\u5e8f\u5458\u8ba4\u4e3a\u5e94\u8be5\u907f\u514d\u63d0\u524d\u8fd4\u56de\u3002\u5728\u51fd\u6570\u5e95\u90e8\u53ea\u6709\u4e00\u4e2a return \u8bed\u53e5\u7684\u51fd\u6570\u66f4\u7b80\u5355\u2014\u2014\u4f60\u53ef\u4ee5\u5047\u8bbe\u51fd\u6570\u5c06\u63a5\u53d7\u5b83\u7684\u53c2\u6570\uff0c\u6267\u884c\u5b83\u5b9e\u73b0\u7684\u4efb\u4f55\u903b\u8f91\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u6ca1\u6709\u504f\u5dee\u7684\u7ed3\u679c\u3002\u989d\u5916\u7684 return \u8bed\u53e5\u4f1a\u4f7f\u903b\u8f91\u53d8\u5f97\u590d\u6742\u3002 \u53cd\u65b9\u5219\u8ba4\u4e3a\uff0c\u4f7f\u7528\u63d0\u524d\u8fd4\u56de\u53ef\u4ee5\u8ba9\u51fd\u6570\u5728\u5b8c\u6210\u540e\u7acb\u5373\u9000\u51fa\uff0c\u8fd9\u6837\u5c31\u51cf\u5c11\u4e86\u9605\u8bfb\u4e0d\u5fc5\u8981\u7684\u903b\u8f91\uff0c\u5e76\u51cf\u5c11\u4e86\u5bf9\u6761\u4ef6\u5d4c\u5957\u5757\u7684\u9700\u6c42\uff0c\u4ece\u800c\u4f7f\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\u3002 \u6709\u4e9b\u5f00\u53d1\u4eba\u5458\u91c7\u53d6\u4e2d\u95f4\u7acb\u573a\uff0c \u53ea\u5728\u51fd\u6570\u9876\u90e8\u4f7f\u7528\u63d0\u524d\u8fd4\u56de\u6765\u8fdb\u884c\u53c2\u6570\u9a8c\u8bc1 (\u6355\u83b7\u4f20\u5165\u7684\u9519\u8bef\u53c2\u6570)\uff0c\u7136\u540e\u5728\u51fd\u6570\u4e4b\u540e\u7684\u90e8\u5206\u4fdd\u8bc1\u53ea\u4f7f\u7528\u4e00\u4e2a return \u8bed\u53e5\u3002 \u6211\u4eec\u7684\u7acb\u573a\u662f\uff0c\u63d0\u524d\u8fd4\u56de\u5229\u5927\u4e8e\u5f0a\uff0c\u4f46\u5728\u5b9e\u8df5\u4e2d\u8981\u56e0\u5730\u5236\u5b9c\u5730\u4f7f\u7528\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u63d0\u524d\u8fd4\u56de\u80fd\u591f\u7b80\u5316\u51fd\u6570\u903b\u8f91\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u5b83","title":"7 10 Break and continue"},{"location":"7-10-Break-and-continue/#break","text":"\u867d\u7136\u60a8\u5df2\u7ecf\u5728 switch \u8bed\u53e5\u7684\u4e0a\u4e0b\u6587\u4e2d\u89c1\u8fc7 break \u8bed\u53e5( 7.4 - switch \u8bed\u53e5\u57fa\u7840 )\uff0c\u4f46\u5b83\u503c\u5f97\u66f4\u4ed4\u7ec6\u7814\u7a76\uff0c\u56e0\u4e3a\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\u63a7\u5236\u6d41\u8bed\u53e5\u3002break\u8bed\u53e5\u5bfc\u81f4while\u5faa\u73af\u3001do-while\u5faa\u73af\u3001for\u5faa\u73af\u6216switch\u8bed\u53e5\u7ed3\u675f\uff0c\u5e76\u5728\u5faa\u73af\u6216switch\u88ab\u6253\u7834\u540e\u7ee7\u7eed\u6267\u884c\u4e0b\u4e00\u4e2a\u8bed\u53e5\u3002","title":"Break \u8bed\u53e5"},{"location":"7-10-Break-and-continue/#switch","text":"\u5728 switch \u8bed\u53e5\u4e2d\uff0c break \u8bed\u53e5\u901a\u5e38\u653e\u7f6e\u5728\u6bcf\u4e2a\u5206\u652f\u7684\u6700\u540e\uff0c\u7528\u4e8e\u9000\u51faswitch\u8bed\u53e5(\u907f\u514d\u8d2f\u7a7f\u6267\u884c\u5206\u652f)\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include void printMath ( int x , int y , char ch ) { switch ( ch ) { case '+' : std :: cout << x << \" + \" << y << \" = \" << x + y << '\\n' ; break ; // don't fall-through to next case case '-' : std :: cout << x << \" - \" << y << \" = \" << x - y << '\\n' ; break ; // don't fall-through to next case case '*' : std :: cout << x << \" * \" << y << \" = \" << x * y << '\\n' ; break ; // don't fall-through to next case case '/' : std :: cout << x << \" / \" << y << \" = \" << x / y << '\\n' ; break ; } } int main () { printMath ( 2 , 3 , '+' ); return 0 ; } \u5173\u4e8e \u8d2f\u7a7f\u5c5e\u6027 \u7684\u66f4\u591a\u8be6\u7ec6\u4fe1\u606f\uff0c\u53c2\u89c1 7.5 - switch fallthrough\u5c5e\u6027\u548c\u4f5c\u7528\u57df \u3002","title":"\u4ece switch \u4e2d\u8df3\u51fa"},{"location":"7-10-Break-and-continue/#_1","text":"\u5728\u5faa\u73af\u4e0a\u4e0b\u6587\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528break\u8bed\u53e5\u63d0\u524d\u7ed3\u675f\u5faa\u73af\u5e76\u4ece\u5faa\u73af\u7ed3\u675f\u540e\u7684\u4e0b\u4e00\u6761\u8bed\u53e5\u7ee7\u7eed\u6267\u884c\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include int main () { int sum { 0 }; // Allow the user to enter up to 10 numbers for ( int count { 0 }; count < 10 ; ++ count ) { std :: cout << \"Enter a number to add, or 0 to exit: \" ; int num {}; std :: cin >> num ; // exit loop if user enters 0 if ( num == 0 ) break ; // exit the loop now // otherwise add number to our sum sum += num ; } // execution will continue here after the break std :: cout << \"The sum of all the numbers you entered is: \" << sum << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u5141\u8bb8\u7528\u6237\u6700\u591a\u8f93\u516510\u4e2a\u6570\u5b57\uff0c\u5e76\u663e\u793a\u6700\u540e\u8f93\u5165\u7684\u6240\u6709\u6570\u5b57\u7684\u603b\u548c\u3002\u5982\u679c\u7528\u6237\u8f93\u51650\uff0c\u4e2d\u65ad\u5c06\u5bfc\u81f4\u5faa\u73af\u63d0\u524d\u7ec8\u6b62(\u5728\u8f93\u516510\u4e2a\u6570\u5b57\u4e4b\u524d)\u3002 \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 Enter a number to add, or 0 to exit: 5 Enter a number to add, or 0 to exit: 2 Enter a number to add, or 0 to exit: 1 Enter a number to add, or 0 to exit: 0 The sum of all the numbers you entered is: 8 break \u4e5f\u662f\u4e00\u79cd\u4ece\u4e00\u4e2a\u6545\u610f\u8bbe\u7f6e\u7684\u65e0\u9650\u5faa\u73af\u4e2d\u8df3\u51fa\u7684\u5e38\u89c1\u65b9\u6cd5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { while ( true ) // infinite loop { std :: cout << \"Enter 0 to exit or any other integer to continue: \" ; int num {}; std :: cin >> num ; // exit loop if user enters 0 if ( num == 0 ) break ; } std :: cout << \"We're out! \\n \" ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 Enter 0 to exit or any other integer to continue: 5 Enter 0 to exit or any other integer to continue: 3 Enter 0 to exit or any other integer to continue: 0 We're out!","title":"\u4ece\u5faa\u73af\u4e2d\u8df3\u51fa"},{"location":"7-10-Break-and-continue/#break-vs-return","text":"\u65b0\u7a0b\u5e8f\u5458\u6709\u65f6\u5f88\u96be\u7406\u89e3\u201cbreak\u201d\u548c\u201creturn\u201d\u4e4b\u95f4\u7684\u533a\u522b\u3002break\u8bed\u53e5\u7ec8\u6b62switch\u6216\u5faa\u73af\uff0c\u5e76\u4eceswitch\u6216\u5faa\u73af\u4ee5\u5916\u7684\u7b2c\u4e00\u4e2a\u8bed\u53e5\u7ee7\u7eed\u6267\u884c\u3002return\u8bed\u53e5\u4f1a\u7ec8\u6b62\u5faa\u73af\u6240\u5728\u7684\u6574\u4e2a\u51fd\u6570\uff0c\u5e76\u5728\u51fd\u6570\u88ab\u8c03\u7528\u7684\u5730\u65b9\u7ee7\u7eed\u6267\u884c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include int breakOrReturn () { while ( true ) // infinite loop { std :: cout << \"Enter 'b' to break or 'r' to return: \" ; char ch ; std :: cin >> ch ; if ( ch == 'b' ) break ; // execution will continue at the first statement beyond the loop if ( ch == 'r' ) return 1 ; // return will cause the function to immediately return to the caller (in this case, main()) } // breaking the loop causes execution to resume here std :: cout << \"We broke out of the loop \\n \" ; return 0 ; } int main () { int returnValue { breakOrReturn () }; std :: cout << \"Function breakOrReturn returned \" << returnValue << '\\n' ; return 0 ; } \u8fd0\u884c\u51fd\u6570\u4e24\u6b21\uff1a 1 2 3 4 5 6 Enter 'b' to break or 'r' to return: r Function breakOrReturn returned 1 Enter 'b' to break or 'r' to return: b We broke out of the loop Function breakOrReturn returned 0","title":"break vs return"},{"location":"7-10-Break-and-continue/#continue","text":"continue \u8bed\u53e5\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u7684\u65b9\u5f0f\u6765\u7ed3\u675f\u5faa\u73af\u7684 \u5f53\u524d\u8fed\u4ee3 \u800c\u4e0d\u7ec8\u6b62\u6574\u4e2a\u5faa\u73af\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 continue \u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { for ( int count { 0 }; count < 10 ; ++ count ) { // if the number is divisible by 4, skip this iteration if (( count % 4 ) == 0 ) continue ; // go to next iteration // If the number is not divisible by 4, keep going std :: cout << count << '\\n' ; // The continue statement jumps to here } return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u8f93\u51fa\u4ece0\u52309\u6240\u6709\u4e0d\u80fd\u88ab4\u6574\u9664\u7684\u6570\uff1a 1 2 3 4 5 6 7 1 2 3 5 6 7 9 continue \u8bed\u53e5\u4f7f\u5f53\u524d\u6267\u884c\u70b9\u8df3\u8f6c\u5230\u5f53\u524d\u5faa\u73af\u7684\u5e95\u90e8\u3002 \u5728 for \u5faa\u73af\u7684\u4e2d\uff0cfor \u5faa\u73af\u7684\u7ed3\u675f\u8bed\u53e5\u4ecd\u7136\u5728 continue \u8bed\u53e5\u4e4b\u540e\u6267\u884c(\u56e0\u4e3a\u8fd9\u53d1\u751f\u5728\u5faa\u73af\u4f53\u7ed3\u675f\u4e4b\u540e)\u3002 \u5728\u4f7f\u7528\u5e26\u6709 while \u6216 do-while \u5faa\u73af\u7684 continue \u8bed\u53e5\u65f6\u8981\u5c0f\u5fc3\u3002\u8fd9\u4e9b\u5faa\u73af\u901a\u5e38\u4f1a\u66f4\u6539\u5faa\u73af\u4f53\u5185\u90e8\u6761\u4ef6\u4e2d\u4f7f\u7528\u7684\u53d8\u91cf\u7684\u503c\u3002\u5982\u679c\u4f7f\u7528 continue \u8bed\u53e5\u5bfc\u81f4\u8fd9\u4e9b\u884c\u88ab\u8df3\u8fc7\uff0c\u90a3\u4e48\u5faa\u73af\u53ef\u80fd\u4f1a\u53d8\u6210\u65e0\u9650\u5faa\u73af! \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { int count { 0 }; while ( count < 10 ) { if ( count == 5 ) continue ; // \u8df3\u8f6c\u5230\u672b\u5c3e std :: cout << count << '\\n' ; ++ count ; // \u5230\u8fbe 5 \u4e4b\u540e\u518d\u4e5f\u4e0d\u4f1a\u88ab\u6267\u884c // \u4ece\u8fd9\u91cc\u6267\u884c } return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u7684\u76ee\u7684\u662f\u6253\u53700\u52309\u4e4b\u95f4\u96645\u4e4b\u5916\u7684\u6240\u6709\u6570\u5b57\u3002\u4f46\u5b83\u5b9e\u9645\u4e0a\u6253\u5370: 1 2 3 4 5 0 1 2 3 4 \u968f\u540e\u5c31\u8fdb\u5165\u4e86\u65e0\u9650\u5faa\u73af\u3002\u5f53 count \u7b49\u4e8e 5 \u65f6\uff0c if \u8bed\u53e5\u6761\u4ef6\u6c42\u503c\u4e3a true \uff0c continue \u8bed\u53e5\u4f7f\u63a7\u5236\u6d41\u8df3\u8f6c\u5230\u5faa\u73af\u5e95\u90e8\u3002\u6240\u4ee5 count \u53d8\u91cf\u65e0\u6cd5\u88ab\u9012\u589e\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4e0b\u4e00\u6b21\u8fed\u4ee3\u65f6 count \u4ecd\u7136\u662f 5 \uff0c\u6240\u4ee5 if \u8bed\u53e5\u6761\u4ef6\u6c42\u503c\u4ecd\u7136\u4e3a true \uff0c\u7a0b\u5e8f\u53d8\u6210\u4e86\u6b7b\u5faa\u73af\u3002 \u5f53\u7136\uff0c\u5982\u679c\u6211\u4eec\u80fd\u591f\u8bc6\u522b\u51fa\u4e00\u4e2a\u5178\u578b\u7684\u8ba1\u6570\u53d8\u91cf\u7684\u8bdd\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528 for \u5faa\u73af\u6765\u4ee3\u66ff while \u6216 do-while \u5faa\u73af\u3002","title":"continue \u8bed\u53e5"},{"location":"7-10-Break-and-continue/#break-continue","text":"\u8bb8\u591a\u6559\u79d1\u4e66\u544a\u8beb\u8bfb\u8005\u4e0d\u8981\u5728\u5faa\u73af\u4e2d\u4f7f\u7528\u201cbreak\u201d\u548c\u201ccontinue\u201d\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5bfc\u81f4\u6267\u884c\u6d41\u56db\u5904\u8df3\u8dc3\uff0c\u4e5f\u4f1a\u4f7f\u903b\u8f91\u6d41\u66f4\u96be\u9075\u5faa\u3002\u4f8b\u5982\uff0c\u5728\u4e00\u4e2a\u590d\u6742\u7684\u903b\u8f91\u7247\u6bb5\u4e2d\u95f4\u7684\u201c\u4e2d\u65ad\u201d\u53ef\u80fd\u88ab\u9519\u8fc7\uff0c\u6216\u8005\u5728\u4ec0\u4e48\u6761\u4ef6\u4e0b\u5e94\u8be5\u89e6\u53d1\u5b83\u53ef\u80fd\u4e0d\u660e\u663e\u3002 \u7136\u800c\uff0c\u5982\u679c\u4f7f\u7528\u5f97\u5f53\uff0cbreak \u548c continue \u53ef\u4ee5\u51cf\u5c11\u5d4c\u5957\u5757\u7684\u6570\u91cf\uff0c\u51cf\u5c11\u5bf9\u590d\u6742\u5faa\u73af\u903b\u8f91\u7684\u9700\u6c42\uff0c\u4ece\u800c\u4f7f\u5faa\u73af\u66f4\u5177\u53ef\u8bfb\u6027\u3002 \u4f8b\u5982\uff0c\u8003\u8651\u4ee5\u4e0b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include int main () { int count { 0 }; // count how many times the loop iterates bool keepLooping { true }; // controls whether the loop ends or not while ( keepLooping ) { std :: cout << \"Enter 'e' to exit this loop or any other character to continue: \" ; char ch {}; std :: cin >> ch ; if ( ch == 'e' ) keepLooping = false ; else { ++ count ; std :: cout << \"We've iterated \" << count << \" times \\n \" ; } } return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u4f7f\u7528\u4e00\u4e2a\u5e03\u5c14\u53d8\u91cf\u6765\u63a7\u5236\u5faa\u73af\u662f\u5426\u7ee7\u7eed\uff0c\u4ee5\u53ca\u4e00\u4e2a\u53ea\u5728\u7528\u6237\u4e0d\u9000\u51fa\u65f6\u8fd0\u884c\u7684\u5d4c\u5957\u5757\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u66f4\u5bb9\u6613\u7406\u89e3\u7684\u7248\u672c\uff0c\u4f7f\u7528 break \u8bed\u53e5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int main () { int count { 0 }; // count how many times the loop iterates while ( true ) // loop until user terminates { std :: cout << \"Enter 'e' to exit this loop or any other character to continue: \" ; char ch {}; std :: cin >> ch ; if ( ch == 'e' ) break ; ++ count ; std :: cout << \"We've iterated \" << count << \" times \\n \" ; } return 0 ; } \u5728\u8fd9\u4e2a\u7248\u672c\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528\u201cbreak\u8bed\u53e5\u201d\uff0c\u907f\u514d\u4e86\u4f7f\u7528\u5e03\u5c14\u53d8\u91cf(\u5e76\u4e14\u5fc5\u987b\u7406\u89e3\u5b83\u7684\u9884\u671f\u7528\u9014\u4ee5\u53ca\u5b83\u7684\u503c\u5728\u54ea\u91cc\u88ab\u66f4\u6539)\u3001\u201celse\u8bed\u53e5\u201d\u548c\u5d4c\u5957\u5757\u3002 \u5c3d\u91cf\u51cf\u5c11\u4f7f\u7528\u7684\u53d8\u91cf\u6570\u91cf\u548c\u4fdd\u6301\u5d4c\u5957\u5757\u7684\u6570\u91cf\u90fd\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7406\u89e3\u6027\uff0c\u800c\u4e0d\u662f\u201cbreak\u201d\u6216\u201ccontinue\u201d\u5bf9\u4ee3\u7801\u7684\u635f\u5bb3\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u8ba4\u4e3a\u660e\u667a\u5730\u4f7f\u7528\u201cbreak\u201d\u6216\u201ccontinue\u201d\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f7f\u7528break\u548ccontinue\u80fd\u591f\u7b80\u5316\u5faa\u73af\u903b\u8f91\uff0c\u5219\u4f7f\u7528\u5b83\u4eec\u3002","title":"\u5173\u4e8e break \u548c continue \u7684\u4e89\u8bba"},{"location":"7-10-Break-and-continue/#_2","text":"\u5bf9\u4e8e return \u8bed\u53e5\u4e5f\u6709\u7c7b\u4f3c\u7684\u4e89\u8bba\u3002\u4e0d\u662f\u51fd\u6570\u4e2d\u6700\u540e\u4e00\u6761\u8bed\u53e5\u7684\u8fd4\u56de\u8bed\u53e5\u5219\u79f0\u4e4b\u4e3a \u63d0\u524d\u8fd4\u56de\u8bed\u53e5 \u3002\u8bb8\u591a\u7a0b\u5e8f\u5458\u8ba4\u4e3a\u5e94\u8be5\u907f\u514d\u63d0\u524d\u8fd4\u56de\u3002\u5728\u51fd\u6570\u5e95\u90e8\u53ea\u6709\u4e00\u4e2a return \u8bed\u53e5\u7684\u51fd\u6570\u66f4\u7b80\u5355\u2014\u2014\u4f60\u53ef\u4ee5\u5047\u8bbe\u51fd\u6570\u5c06\u63a5\u53d7\u5b83\u7684\u53c2\u6570\uff0c\u6267\u884c\u5b83\u5b9e\u73b0\u7684\u4efb\u4f55\u903b\u8f91\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u6ca1\u6709\u504f\u5dee\u7684\u7ed3\u679c\u3002\u989d\u5916\u7684 return \u8bed\u53e5\u4f1a\u4f7f\u903b\u8f91\u53d8\u5f97\u590d\u6742\u3002 \u53cd\u65b9\u5219\u8ba4\u4e3a\uff0c\u4f7f\u7528\u63d0\u524d\u8fd4\u56de\u53ef\u4ee5\u8ba9\u51fd\u6570\u5728\u5b8c\u6210\u540e\u7acb\u5373\u9000\u51fa\uff0c\u8fd9\u6837\u5c31\u51cf\u5c11\u4e86\u9605\u8bfb\u4e0d\u5fc5\u8981\u7684\u903b\u8f91\uff0c\u5e76\u51cf\u5c11\u4e86\u5bf9\u6761\u4ef6\u5d4c\u5957\u5757\u7684\u9700\u6c42\uff0c\u4ece\u800c\u4f7f\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\u3002 \u6709\u4e9b\u5f00\u53d1\u4eba\u5458\u91c7\u53d6\u4e2d\u95f4\u7acb\u573a\uff0c \u53ea\u5728\u51fd\u6570\u9876\u90e8\u4f7f\u7528\u63d0\u524d\u8fd4\u56de\u6765\u8fdb\u884c\u53c2\u6570\u9a8c\u8bc1 (\u6355\u83b7\u4f20\u5165\u7684\u9519\u8bef\u53c2\u6570)\uff0c\u7136\u540e\u5728\u51fd\u6570\u4e4b\u540e\u7684\u90e8\u5206\u4fdd\u8bc1\u53ea\u4f7f\u7528\u4e00\u4e2a return \u8bed\u53e5\u3002 \u6211\u4eec\u7684\u7acb\u573a\u662f\uff0c\u63d0\u524d\u8fd4\u56de\u5229\u5927\u4e8e\u5f0a\uff0c\u4f46\u5728\u5b9e\u8df5\u4e2d\u8981\u56e0\u5730\u5236\u5b9c\u5730\u4f7f\u7528\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u63d0\u524d\u8fd4\u56de\u80fd\u591f\u7b80\u5316\u51fd\u6570\u903b\u8f91\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u5b83","title":"\u5173\u4e8e\u63d0\u524d\u8fd4\u56de\u7684\u4e89\u8bba"},{"location":"7-11-Halts-exiting-your-program-early/","tags":["halt"],"text":"Key Takeaway \u5728\u672c\u7ae0\u4e2d\u6211\u4eec\u5c06\u4ecb\u7ecd\u7684\u6700\u540e\u4e00\u7c7b\u6d41\u63a7\u5236\u8bed\u53e5\u662f\u6302\u8d77\u8bed\u53e5\u3002\u6302\u8d77\u7ec8\u6b62\u7a0b\u5e8f\u7684\u6d41\u63a7\u5236\u8bed\u53e5\u3002\u5728C++\u4e2d\uff0c\u7a0b\u5e8f\u505c\u6b62\u88ab\u5b9e\u73b0\u4e3a\u51fd\u6570(\u800c\u4e0d\u662f\u5173\u952e\u5b57)\uff0c\u56e0\u6b64\u6211\u4eec\u7684\u505c\u6b62\u8bed\u53e5\u5c06\u662f\u51fd\u6570\u8c03\u7528\u3002 \u8ba9\u6211\u4eec\u7ed5\u4e2a\u5c0f\u5f2f\u5b50\uff0c\u56de\u987e\u4e00\u4e0b\u7a0b\u5e8f\u6b63\u5e38\u9000\u51fa\u65f6\u53d1\u751f\u7684\u60c5\u51b5\u3002\u5f53 main() \u51fd\u6570\u8fd4\u56de\u65f6(\u5230\u8fbe\u51fd\u6570\u7684\u672b\u5c3e\uff0c\u6216\u8005\u901a\u8fc7 return \u8bed\u53e5)\uff0c\u4f1a\u53d1\u751f\u8bb8\u591a\u4e0d\u540c\u7684\u4e8b\u60c5\u3002 \u9996\u5148\uff0c\u56e0\u4e3a\u6211\u4eec\u8981\u79bb\u5f00\u51fd\u6570\uff0c\u6240\u4ee5\u6240\u6709\u5c40\u90e8\u53d8\u91cf\u548c\u51fd\u6570\u5f62\u53c2\u90fd\u88ab\u9500\u6bc1(\u50cf\u5f80\u5e38\u4e00\u6837)\u3002 \u63a5\u4e0b\u6765\uff0c\u4e00\u4e2a\u540d\u4e3a std::exit() \u7684\u7279\u6b8a\u51fd\u6570\u88ab\u8c03\u7528\uff0c\u5e76\u5c06 main() \u7684\u8fd4\u56de\u503c( \u72b6\u6001\u7801 )\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u3002\u90a3\u4e48 std::exit() \u662f\u4ec0\u4e48\u5462? std::exit() \u51fd\u6570 std::exit() \u662f\u4e00\u4e2a\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u6b63\u5e38\u7ec8\u6b62\u7684\u51fd\u6570\u3002\u6b63\u5e38\u7ec8\u6b62\u7684\u610f\u601d\u662f\uff0c\u7a0b\u5e8f\u4ee5\u6211\u4eec\u671f\u671b\u7684\u65b9\u5f0f\u9000\u51fa\u3002\u6ce8\u610f\u672f\u8bed \u6b63\u5e38\u7ec8\u6b62 \u5e76\u4e0d\u9690\u542b\u7a0b\u5e8f\u662f\u5426\u6267\u884c\u6210\u529f\u7684\u542b\u4e49\uff08\u8fd9\u662f\u8fd4\u56de\u72b6\u6001\u7801\u7684\u4f5c\u7528\uff09\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a\u7a0b\u5e8f\u9700\u8981\u7528\u6237\u8f93\u5165\u6587\u4ef6\u540d\u3002\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u662f\u975e\u6cd5\u7684\u6587\u4ef6\u540d\uff0c\u5219\u7a0b\u5e8f\u901a\u5e38\u5e94\u8be5\u8fd4\u56de\u975e\u96f6\u7684\u72b6\u6001\u7801\u6765\u8868\u793a\u5931\u8d25\uff0c\u4f46\u662f\u8be5\u7a0b\u5e8f\u4ecd\u7136\u53ef\u4ee5 \u6b63\u5e38\u7ec8\u6b62 \u3002 std::exit() \u6267\u884c\u8bb8\u591a\u6e05\u7406\u51fd\u6570\u3002\u9996\u5148\uff0c\u9500\u6bc1\u5177\u6709\u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u7684\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u5982\u679c\u4f7f\u7528\u4e86\u4efb\u4f55\u6587\u4ef6\uff0c\u5219\u6267\u884c\u5176\u4ed6\u4e00\u4e9b\u6742\u9879\u6587\u4ef6\u6e05\u7406\u3002\u6700\u540e\uff0c\u63a7\u5236\u88ab\u8fd4\u56de\u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff0c\u4f20\u9012\u7ed9 std::exit() \u7684\u53c2\u6570\u88ab\u7528\u4f5c \u72b6\u6001\u7801 \u3002 \u663e\u5f0f\u8c03\u7528 std::exit() \u867d\u7136 std::exit() \u5728 main() \u7ed3\u675f\u65f6\u4f1a\u88ab\u9690\u5f0f\u8c03\u7528\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u88ab\u663e\u5f0f\u8c03\u7528\uff0c\u4ee5\u5728\u7a0b\u5e8f\u6b63\u5e38\u7ec8\u6b62\u524d\u505c\u6b62\u7a0b\u5e8f\u3002\u5f53\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u8c03\u7528 std::exit() \u65f6\uff0c\u4f60\u9700\u8981\u5305\u542b cstdlib \u5934\u6587\u4ef6\u3002 \u4e0b\u9762\u662f\u663e\u5f0f\u8c03\u7528 std::exit() \u7684\u4e00\u4e2a\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include // for std::exit() #include void cleanup () { // code here to do any kind of cleanup required std :: cout << \"cleanup! \\n \" ; } int main () { std :: cout << 1 << '\\n' ; cleanup (); std :: exit ( 0 ); // terminate and return status code 0 to operating system // The following statements never execute std :: cout << 2 << '\\n' ; return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 2 1 cleanup! \u6ce8\u610f\uff0c std::exit() \u4e4b\u540e\u7684\u8bed\u53e5\u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c\uff0c\u56e0\u4e3a\u7a0b\u5e8f\u5df2\u7ecf\u7ec8\u6b62\u4e86\u3002 \u867d\u7136\u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4ece\u51fd\u6570 main() \u8c03\u7528 std::exit() \uff0c\u4f46\u4e5f\u53ef\u4ee5\u4ece\u4efb\u4f55\u51fd\u6570\u8c03\u7528 std::exit() \u6765\u7ec8\u6b62\u7a0b\u5e8f\u3002 \u5173\u4e8e\u663e\u5f0f\u8c03\u7528 std::exit() \u7684\u4e00\u4e2a\u91cd\u8981\u6ce8\u610f\u4e8b\u9879\u662f\uff1a std::exit() \u4e0d\u4f1a\u6e05\u9664\u4efb\u4f55\u672c\u5730\u53d8\u91cf(\u65e0\u8bba\u662f\u5728\u5f53\u524d\u51fd\u6570\u4e2d\uff0c\u8fd8\u662f\u5728\u8c03\u7528\u6808\u4e2d\u7684\u51fd\u6570)\u3002\u56e0\u6b64\uff0c\u901a\u5e38\u6700\u597d\u907f\u514d\u8c03\u7528 std::exit() \u3002 \u6ce8\u610f std::exit() \u51fd\u6570\u4e0d\u4f1a\u6e05\u7406\u5f53\u524d\u51fd\u6570\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf\u6216\u6e05\u7406\u8c03\u7528\u5806\u6808\u3002 std::atexit \u56e0\u4e3a std::exit() \u4f1a\u7acb\u5373\u7ec8\u6b62\u7a0b\u5e8f\uff0c\u6240\u4ee5\u4f60\u53ef\u80fd\u9700\u8981\u5728\u7ec8\u6b62\u524d\u624b\u52a8\u6267\u884c\u4e00\u4e9b\u6e05\u7406\u64cd\u4f5c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6e05\u7406\u610f\u5473\u7740\u5173\u95ed\u6570\u636e\u5e93\u6216\u7f51\u7edc\u8fde\u63a5\u3001\u91ca\u653e\u6240\u5206\u914d\u7684\u5185\u5b58\u3001\u5c06\u4fe1\u606f\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\u7b49\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8c03\u7528\u51fd\u6570 cleanup() \u6765\u5904\u7406\u6e05\u7406\u4efb\u52a1\u3002\u7136\u800c\uff0c\u8bb0\u4f4f\u5728\u8c03\u7528 exit() \u4e4b\u524d\u624b\u52a8\u8c03\u7528\u4e00\u4e2a\u6e05\u7406\u51fd\u6570\u4f1a\u7ed9\u7a0b\u5e8f\u5458\u589e\u52a0\u8d1f\u62c5\u3002 \u4e3a\u4e86\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u8fd9\u9879\u5de5\u4f5c\uff0cC++\u63d0\u4f9b\u4e86 std::atexit() \u51fd\u6570\uff0c\u5b83\u5141\u8bb8\u6307\u5b9a\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u5728\u7a0b\u5e8f\u7ec8\u6b62\u65f6\u901a\u8fc7 std::exit() \u81ea\u52a8\u8c03\u7528\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 12.1 - \u51fd\u6570\u6307\u9488 \u4e2d\u4ecb\u7ecd\u5982\u4f55\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012 \u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include // for std::exit() #include void cleanup () { // code here to do any kind of cleanup required std :: cout << \"cleanup! \\n \" ; } int main () { // register cleanup() to be called automatically when std::exit() is called std :: atexit ( cleanup ); // note: we use cleanup rather than cleanup() since we're not making a function call to cleanup() right now std :: cout << 1 << '\\n' ; std :: exit ( 0 ); // terminate and return status code 0 to operating system // The following statements never execute std :: cout << 2 << '\\n' ; return 0 ; } \u8be5\u7a0b\u5e8f\u7684\u8f93\u51fa\u4e0e\u524d\u9762\u7684\u793a\u4f8b\u76f8\u540c\uff1a 1 2 1 cleanup! \u4e3a\u4ec0\u4e48\u8981\u8fd9\u4e48\u505a\u5462\uff1f\u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u5728\u4e00\u4e2a\u5730\u65b9\u6307\u5b9a\u4e00\u4e2a\u6e05\u7406\u51fd\u6570(\u53ef\u80fd\u5728 main \u4e2d)\uff0c\u7136\u540e\u4e0d\u5fc5\u62c5\u5fc3\u5728\u8c03\u7528 std::exit() \u4e4b\u524d\u8bb0\u5f97\u663e\u5f0f\u8c03\u7528\u8be5\u51fd\u6570\u3002 \u9996\u5148\uff0c\u7531\u4e8e std::exit() \u5728 main() \u7ec8\u6b62\u65f6\u9690\u5f0f\u8c03\u7528\uff0c\u5982\u679c\u7a0b\u5e8f\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u9000\u51fa\uff0c std::atexit() \u6ce8\u518c\u7684\u4efb\u4f55\u51fd\u6570\u90fd\u4f1a\u88ab\u8c03\u7528\u3002\u7b2c\u4e8c\uff0c\u88ab\u6ce8\u518c\u7684\u51fd\u6570\u5fc5\u987b\u4e0d\u5e26\u53c2\u6570\uff0c\u4e5f\u6ca1\u6709\u8fd4\u56de\u503c\u3002\u6700\u540e\uff0c\u5982\u679c\u4f60\u613f\u610f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 std::atexit() \u6ce8\u518c\u591a\u4e2a\u6e05\u7406\u51fd\u6570\uff0c\u5b83\u4eec\u5c06\u6309\u6ce8\u518c\u7684\u76f8\u53cd\u987a\u5e8f\u8c03\u7528(\u6700\u540e\u6ce8\u518c\u7684\u5c06\u6700\u5148\u8c03\u7528)\u3002 \u6269\u5c55\u9605\u8bfb \u5728\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u4e2d\uff0c\u8c03\u7528 std::exit() \u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83(\u56e0\u4e3a\u8c03\u7528 std::exit() \u7684\u7ebf\u7a0b\u5c06\u6e05\u7406\u4ecd\u7136\u53ef\u80fd\u88ab\u5176\u4ed6\u7ebf\u7a0b\u8bbf\u95ee\u7684\u9759\u6001\u5bf9\u8c61)\u3002\u56e0\u6b64\uff0cC++\u5f15\u5165\u4e86\u53e6\u4e00\u5bf9\u51fd\u6570\uff0c\u5b83\u4eec\u7684\u5de5\u4f5c\u65b9\u5f0f\u7c7b\u4f3c\u4e8e std::exit() \u548c std::atexit() \uff0c\u5b83\u4eec\u88ab\u79f0\u4e3a std::quick_exit() \u548c std::at_quick_exit() \u3002 std::quick_exit() \u4f1a\u6b63\u5e38\u5730\u7ec8\u6b62\u7a0b\u5e8f\uff0c\u4f46\u4e0d\u4f1a\u6e05\u7406\u9759\u6001\u5bf9\u8c61\uff0c\u53ef\u80fd\u4f1a\u4e5f\u53ef\u80fd\u4e0d\u4f1a\u6267\u884c\u5176\u4ed6\u7c7b\u578b\u7684\u6e05\u7406\u3002\u5bf9\u4e8e\u4ee5 std::quick_exit() \u7ed3\u675f\u7684\u7a0b\u5e8f\uff0c std::at_quick_exit() \u6267\u884c\u4e0e std::atexit() \u76f8\u540c\u7684\u4f5c\u7528\u3002 std::abort \u548c std::terminate C++\u5305\u542b\u53e6\u5916\u4e24\u4e2a\u4e0e\u505c\u6b62\u76f8\u5173\u7684\u51fd\u6570\u3002 std::abort() \u51fd\u6570\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u975e\u6b63\u5e38\u7ec8\u6b62\u3002\u5f02\u5e38\u7ec8\u6b62\u610f\u5473\u7740\u7a0b\u5e8f\u6709\u67d0\u79cd\u4e0d\u5bfb\u5e38\u7684\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u7a0b\u5e8f\u4e0d\u80fd\u7ee7\u7eed\u8fd0\u884c\u3002\u4f8b\u5982\uff0c\u8bd5\u56fe\u9664\u4ee50\u5c06\u5bfc\u81f4\u5f02\u5e38\u7ec8\u6b62\u3002 std::abort() \u4e0d\u505a\u4efb\u4f55\u6e05\u7406\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include // for std::abort() #include int main () { std :: cout << 1 << '\\n' ; std :: abort (); // The following statements never execute std :: cout << 2 << '\\n' ; return 0 ; } \u6211\u4eec\u5c06\u5728\u672c\u7ae0\u540e\u9762\u770b\u5230\u9690\u5f0f\u8c03\u7528 std::abort() \u7684\u60c5\u51b5( 7.17 -\u65ad\u8a00\u548cstatic_assert )\u3002 std::terminate() \u51fd\u6570\u901a\u5e38\u4e0e\u5f02\u5e38\u4e00\u8d77\u4f7f\u7528(\u6211\u4eec\u5c06\u5728\u540e\u9762\u7684\u7ae0\u8282\u4e2d\u8ba8\u8bba\u5f02\u5e38)\u3002\u867d\u7136\u53ef\u4ee5\u663e\u5f0f\u5730\u8c03\u7528 std::terminate \uff0c\u4f46\u5728\u6ca1\u6709\u5904\u7406\u5f02\u5e38\u65f6(\u4ee5\u53ca\u5728\u5176\u4ed6\u4e00\u4e9b\u4e0e\u5f02\u5e38\u76f8\u5173\u7684\u60c5\u51b5\u4e0b)\uff0c\u66f4\u591a\u7684\u60c5\u51b5\u662f\u9690\u5f0f\u5730\u8c03\u7528\u5b83\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c std::terminate() \u4f1a\u8c03\u7528 std:: abort() \u3002 \u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u6302\u8d77\u7a0b\u5e8f\uff1f \u7b80\u77ed\u7684\u56de\u7b54\u662f\u201c\u51e0\u4e4e\u6ca1\u6709\u201d\u3002\u9500\u6bc1\u5c40\u90e8\u5bf9\u8c61\u662fC++\u7684\u4e00\u4e2a\u91cd\u8981\u90e8\u5206(\u7279\u522b\u662f\u5f53\u6211\u4eec\u8fdb\u5165\u7c7b\u65f6)\uff0c\u4e0a\u9762\u63d0\u5230\u7684\u51fd\u6570\u90fd\u6ca1\u6709\u6e05\u9664\u5c40\u90e8\u53d8\u91cf\u3002\u5f02\u5e38\u662f\u5904\u7406\u9519\u8bef\u60c5\u51b5\u7684\u66f4\u597d\u3001\u66f4\u5b89\u5168\u7684\u673a\u5236\u3002 \u6700\u4f73\u5b9e\u8df5 \u53ea\u6709\u5728\u6ca1\u6709\u4ece\u4e3b\u51fd\u6570\u6b63\u5e38\u8fd4\u56de\u7684\u5b89\u5168\u65b9\u6cd5\u65f6\u624d\u4f7f\u7528\u6302\u8d77\u3002\u5982\u679c\u6ca1\u6709\u7981\u7528\u5f02\u5e38\uff0c\u6700\u597d\u4f7f\u7528\u5f02\u5e38\u6765\u5b89\u5168\u5730\u5904\u7406\u9519\u8bef\u3002","title":"7.11 - \u6302\u8d77\uff08\u63d0\u524d\u7ed3\u675f\u7a0b\u5e8f\uff09"},{"location":"7-11-Halts-exiting-your-program-early/#stdexit","text":"std::exit() \u662f\u4e00\u4e2a\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u6b63\u5e38\u7ec8\u6b62\u7684\u51fd\u6570\u3002\u6b63\u5e38\u7ec8\u6b62\u7684\u610f\u601d\u662f\uff0c\u7a0b\u5e8f\u4ee5\u6211\u4eec\u671f\u671b\u7684\u65b9\u5f0f\u9000\u51fa\u3002\u6ce8\u610f\u672f\u8bed \u6b63\u5e38\u7ec8\u6b62 \u5e76\u4e0d\u9690\u542b\u7a0b\u5e8f\u662f\u5426\u6267\u884c\u6210\u529f\u7684\u542b\u4e49\uff08\u8fd9\u662f\u8fd4\u56de\u72b6\u6001\u7801\u7684\u4f5c\u7528\uff09\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a\u7a0b\u5e8f\u9700\u8981\u7528\u6237\u8f93\u5165\u6587\u4ef6\u540d\u3002\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u662f\u975e\u6cd5\u7684\u6587\u4ef6\u540d\uff0c\u5219\u7a0b\u5e8f\u901a\u5e38\u5e94\u8be5\u8fd4\u56de\u975e\u96f6\u7684\u72b6\u6001\u7801\u6765\u8868\u793a\u5931\u8d25\uff0c\u4f46\u662f\u8be5\u7a0b\u5e8f\u4ecd\u7136\u53ef\u4ee5 \u6b63\u5e38\u7ec8\u6b62 \u3002 std::exit() \u6267\u884c\u8bb8\u591a\u6e05\u7406\u51fd\u6570\u3002\u9996\u5148\uff0c\u9500\u6bc1\u5177\u6709\u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u7684\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u5982\u679c\u4f7f\u7528\u4e86\u4efb\u4f55\u6587\u4ef6\uff0c\u5219\u6267\u884c\u5176\u4ed6\u4e00\u4e9b\u6742\u9879\u6587\u4ef6\u6e05\u7406\u3002\u6700\u540e\uff0c\u63a7\u5236\u88ab\u8fd4\u56de\u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff0c\u4f20\u9012\u7ed9 std::exit() \u7684\u53c2\u6570\u88ab\u7528\u4f5c \u72b6\u6001\u7801 \u3002","title":"std::exit() \u51fd\u6570"},{"location":"7-11-Halts-exiting-your-program-early/#stdexit_1","text":"\u867d\u7136 std::exit() \u5728 main() \u7ed3\u675f\u65f6\u4f1a\u88ab\u9690\u5f0f\u8c03\u7528\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u88ab\u663e\u5f0f\u8c03\u7528\uff0c\u4ee5\u5728\u7a0b\u5e8f\u6b63\u5e38\u7ec8\u6b62\u524d\u505c\u6b62\u7a0b\u5e8f\u3002\u5f53\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u8c03\u7528 std::exit() \u65f6\uff0c\u4f60\u9700\u8981\u5305\u542b cstdlib \u5934\u6587\u4ef6\u3002 \u4e0b\u9762\u662f\u663e\u5f0f\u8c03\u7528 std::exit() \u7684\u4e00\u4e2a\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include // for std::exit() #include void cleanup () { // code here to do any kind of cleanup required std :: cout << \"cleanup! \\n \" ; } int main () { std :: cout << 1 << '\\n' ; cleanup (); std :: exit ( 0 ); // terminate and return status code 0 to operating system // The following statements never execute std :: cout << 2 << '\\n' ; return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 2 1 cleanup! \u6ce8\u610f\uff0c std::exit() \u4e4b\u540e\u7684\u8bed\u53e5\u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c\uff0c\u56e0\u4e3a\u7a0b\u5e8f\u5df2\u7ecf\u7ec8\u6b62\u4e86\u3002 \u867d\u7136\u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4ece\u51fd\u6570 main() \u8c03\u7528 std::exit() \uff0c\u4f46\u4e5f\u53ef\u4ee5\u4ece\u4efb\u4f55\u51fd\u6570\u8c03\u7528 std::exit() \u6765\u7ec8\u6b62\u7a0b\u5e8f\u3002 \u5173\u4e8e\u663e\u5f0f\u8c03\u7528 std::exit() \u7684\u4e00\u4e2a\u91cd\u8981\u6ce8\u610f\u4e8b\u9879\u662f\uff1a std::exit() \u4e0d\u4f1a\u6e05\u9664\u4efb\u4f55\u672c\u5730\u53d8\u91cf(\u65e0\u8bba\u662f\u5728\u5f53\u524d\u51fd\u6570\u4e2d\uff0c\u8fd8\u662f\u5728\u8c03\u7528\u6808\u4e2d\u7684\u51fd\u6570)\u3002\u56e0\u6b64\uff0c\u901a\u5e38\u6700\u597d\u907f\u514d\u8c03\u7528 std::exit() \u3002 \u6ce8\u610f std::exit() \u51fd\u6570\u4e0d\u4f1a\u6e05\u7406\u5f53\u524d\u51fd\u6570\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf\u6216\u6e05\u7406\u8c03\u7528\u5806\u6808\u3002","title":"\u663e\u5f0f\u8c03\u7528 std::exit()"},{"location":"7-11-Halts-exiting-your-program-early/#stdatexit","text":"\u56e0\u4e3a std::exit() \u4f1a\u7acb\u5373\u7ec8\u6b62\u7a0b\u5e8f\uff0c\u6240\u4ee5\u4f60\u53ef\u80fd\u9700\u8981\u5728\u7ec8\u6b62\u524d\u624b\u52a8\u6267\u884c\u4e00\u4e9b\u6e05\u7406\u64cd\u4f5c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6e05\u7406\u610f\u5473\u7740\u5173\u95ed\u6570\u636e\u5e93\u6216\u7f51\u7edc\u8fde\u63a5\u3001\u91ca\u653e\u6240\u5206\u914d\u7684\u5185\u5b58\u3001\u5c06\u4fe1\u606f\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\u7b49\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8c03\u7528\u51fd\u6570 cleanup() \u6765\u5904\u7406\u6e05\u7406\u4efb\u52a1\u3002\u7136\u800c\uff0c\u8bb0\u4f4f\u5728\u8c03\u7528 exit() \u4e4b\u524d\u624b\u52a8\u8c03\u7528\u4e00\u4e2a\u6e05\u7406\u51fd\u6570\u4f1a\u7ed9\u7a0b\u5e8f\u5458\u589e\u52a0\u8d1f\u62c5\u3002 \u4e3a\u4e86\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u8fd9\u9879\u5de5\u4f5c\uff0cC++\u63d0\u4f9b\u4e86 std::atexit() \u51fd\u6570\uff0c\u5b83\u5141\u8bb8\u6307\u5b9a\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u5728\u7a0b\u5e8f\u7ec8\u6b62\u65f6\u901a\u8fc7 std::exit() \u81ea\u52a8\u8c03\u7528\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 12.1 - \u51fd\u6570\u6307\u9488 \u4e2d\u4ecb\u7ecd\u5982\u4f55\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012 \u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include // for std::exit() #include void cleanup () { // code here to do any kind of cleanup required std :: cout << \"cleanup! \\n \" ; } int main () { // register cleanup() to be called automatically when std::exit() is called std :: atexit ( cleanup ); // note: we use cleanup rather than cleanup() since we're not making a function call to cleanup() right now std :: cout << 1 << '\\n' ; std :: exit ( 0 ); // terminate and return status code 0 to operating system // The following statements never execute std :: cout << 2 << '\\n' ; return 0 ; } \u8be5\u7a0b\u5e8f\u7684\u8f93\u51fa\u4e0e\u524d\u9762\u7684\u793a\u4f8b\u76f8\u540c\uff1a 1 2 1 cleanup! \u4e3a\u4ec0\u4e48\u8981\u8fd9\u4e48\u505a\u5462\uff1f\u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u5728\u4e00\u4e2a\u5730\u65b9\u6307\u5b9a\u4e00\u4e2a\u6e05\u7406\u51fd\u6570(\u53ef\u80fd\u5728 main \u4e2d)\uff0c\u7136\u540e\u4e0d\u5fc5\u62c5\u5fc3\u5728\u8c03\u7528 std::exit() \u4e4b\u524d\u8bb0\u5f97\u663e\u5f0f\u8c03\u7528\u8be5\u51fd\u6570\u3002 \u9996\u5148\uff0c\u7531\u4e8e std::exit() \u5728 main() \u7ec8\u6b62\u65f6\u9690\u5f0f\u8c03\u7528\uff0c\u5982\u679c\u7a0b\u5e8f\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u9000\u51fa\uff0c std::atexit() \u6ce8\u518c\u7684\u4efb\u4f55\u51fd\u6570\u90fd\u4f1a\u88ab\u8c03\u7528\u3002\u7b2c\u4e8c\uff0c\u88ab\u6ce8\u518c\u7684\u51fd\u6570\u5fc5\u987b\u4e0d\u5e26\u53c2\u6570\uff0c\u4e5f\u6ca1\u6709\u8fd4\u56de\u503c\u3002\u6700\u540e\uff0c\u5982\u679c\u4f60\u613f\u610f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 std::atexit() \u6ce8\u518c\u591a\u4e2a\u6e05\u7406\u51fd\u6570\uff0c\u5b83\u4eec\u5c06\u6309\u6ce8\u518c\u7684\u76f8\u53cd\u987a\u5e8f\u8c03\u7528(\u6700\u540e\u6ce8\u518c\u7684\u5c06\u6700\u5148\u8c03\u7528)\u3002 \u6269\u5c55\u9605\u8bfb \u5728\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u4e2d\uff0c\u8c03\u7528 std::exit() \u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83(\u56e0\u4e3a\u8c03\u7528 std::exit() \u7684\u7ebf\u7a0b\u5c06\u6e05\u7406\u4ecd\u7136\u53ef\u80fd\u88ab\u5176\u4ed6\u7ebf\u7a0b\u8bbf\u95ee\u7684\u9759\u6001\u5bf9\u8c61)\u3002\u56e0\u6b64\uff0cC++\u5f15\u5165\u4e86\u53e6\u4e00\u5bf9\u51fd\u6570\uff0c\u5b83\u4eec\u7684\u5de5\u4f5c\u65b9\u5f0f\u7c7b\u4f3c\u4e8e std::exit() \u548c std::atexit() \uff0c\u5b83\u4eec\u88ab\u79f0\u4e3a std::quick_exit() \u548c std::at_quick_exit() \u3002 std::quick_exit() \u4f1a\u6b63\u5e38\u5730\u7ec8\u6b62\u7a0b\u5e8f\uff0c\u4f46\u4e0d\u4f1a\u6e05\u7406\u9759\u6001\u5bf9\u8c61\uff0c\u53ef\u80fd\u4f1a\u4e5f\u53ef\u80fd\u4e0d\u4f1a\u6267\u884c\u5176\u4ed6\u7c7b\u578b\u7684\u6e05\u7406\u3002\u5bf9\u4e8e\u4ee5 std::quick_exit() \u7ed3\u675f\u7684\u7a0b\u5e8f\uff0c std::at_quick_exit() \u6267\u884c\u4e0e std::atexit() \u76f8\u540c\u7684\u4f5c\u7528\u3002","title":"std::atexit"},{"location":"7-11-Halts-exiting-your-program-early/#stdabort-stdterminate","text":"C++\u5305\u542b\u53e6\u5916\u4e24\u4e2a\u4e0e\u505c\u6b62\u76f8\u5173\u7684\u51fd\u6570\u3002 std::abort() \u51fd\u6570\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u975e\u6b63\u5e38\u7ec8\u6b62\u3002\u5f02\u5e38\u7ec8\u6b62\u610f\u5473\u7740\u7a0b\u5e8f\u6709\u67d0\u79cd\u4e0d\u5bfb\u5e38\u7684\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u7a0b\u5e8f\u4e0d\u80fd\u7ee7\u7eed\u8fd0\u884c\u3002\u4f8b\u5982\uff0c\u8bd5\u56fe\u9664\u4ee50\u5c06\u5bfc\u81f4\u5f02\u5e38\u7ec8\u6b62\u3002 std::abort() \u4e0d\u505a\u4efb\u4f55\u6e05\u7406\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include // for std::abort() #include int main () { std :: cout << 1 << '\\n' ; std :: abort (); // The following statements never execute std :: cout << 2 << '\\n' ; return 0 ; } \u6211\u4eec\u5c06\u5728\u672c\u7ae0\u540e\u9762\u770b\u5230\u9690\u5f0f\u8c03\u7528 std::abort() \u7684\u60c5\u51b5( 7.17 -\u65ad\u8a00\u548cstatic_assert )\u3002 std::terminate() \u51fd\u6570\u901a\u5e38\u4e0e\u5f02\u5e38\u4e00\u8d77\u4f7f\u7528(\u6211\u4eec\u5c06\u5728\u540e\u9762\u7684\u7ae0\u8282\u4e2d\u8ba8\u8bba\u5f02\u5e38)\u3002\u867d\u7136\u53ef\u4ee5\u663e\u5f0f\u5730\u8c03\u7528 std::terminate \uff0c\u4f46\u5728\u6ca1\u6709\u5904\u7406\u5f02\u5e38\u65f6(\u4ee5\u53ca\u5728\u5176\u4ed6\u4e00\u4e9b\u4e0e\u5f02\u5e38\u76f8\u5173\u7684\u60c5\u51b5\u4e0b)\uff0c\u66f4\u591a\u7684\u60c5\u51b5\u662f\u9690\u5f0f\u5730\u8c03\u7528\u5b83\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c std::terminate() \u4f1a\u8c03\u7528 std:: abort() \u3002","title":"std::abort \u548c std::terminate"},{"location":"7-11-Halts-exiting-your-program-early/#_1","text":"\u7b80\u77ed\u7684\u56de\u7b54\u662f\u201c\u51e0\u4e4e\u6ca1\u6709\u201d\u3002\u9500\u6bc1\u5c40\u90e8\u5bf9\u8c61\u662fC++\u7684\u4e00\u4e2a\u91cd\u8981\u90e8\u5206(\u7279\u522b\u662f\u5f53\u6211\u4eec\u8fdb\u5165\u7c7b\u65f6)\uff0c\u4e0a\u9762\u63d0\u5230\u7684\u51fd\u6570\u90fd\u6ca1\u6709\u6e05\u9664\u5c40\u90e8\u53d8\u91cf\u3002\u5f02\u5e38\u662f\u5904\u7406\u9519\u8bef\u60c5\u51b5\u7684\u66f4\u597d\u3001\u66f4\u5b89\u5168\u7684\u673a\u5236\u3002 \u6700\u4f73\u5b9e\u8df5 \u53ea\u6709\u5728\u6ca1\u6709\u4ece\u4e3b\u51fd\u6570\u6b63\u5e38\u8fd4\u56de\u7684\u5b89\u5168\u65b9\u6cd5\u65f6\u624d\u4f7f\u7528\u6302\u8d77\u3002\u5982\u679c\u6ca1\u6709\u7981\u7528\u5f02\u5e38\uff0c\u6700\u597d\u4f7f\u7528\u5f02\u5e38\u6765\u5b89\u5168\u5730\u5904\u7406\u9519\u8bef\u3002","title":"\u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u6302\u8d77\u7a0b\u5e8f\uff1f"},{"location":"7-12-Introduction-to-testing-your-code/","tags":["testing"],"text":"Key Takeaway \u5f53\u7a0b\u5e8f\u53ef\u4ee5\u7f16\u8bd1\uff0c\u751a\u81f3\u53ef\u4ee5\u8fd0\u884c\u540e\uff0c\u8fd8\u6709\u4ec0\u4e48\u5de5\u4f5c\u9700\u8981\u505a\u5462\uff1f \u8fd9\u8981\u770b\u60c5\u51b5\u3002\u5982\u679c\u8be5\u7a0b\u5e8f\u53ea\u8fd0\u884c\u4e00\u6b21\u5e76\u88ab\u4e22\u5f03\uff0c\u90a3\u4e48\u4e0d\u9700\u8981\u505a\u5176\u4ed6\u4e8b\u60c5\u4e86\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f60\u7684\u7a0b\u5e8f\u662f\u5426\u80fd\u9002\u7528\u4e8e\u6240\u6709\u60c5\u51b5\u53ef\u80fd\u5e76\u4e0d\u91cd\u8981\u2014\u2014\u5982\u679c\u5b83\u80fd\u6ee1\u8db3\u4f60\u5f53\u524d\u4efb\u52a1\u7684\u9700\u8981\uff0c\u5e76\u4e14\u4f60\u4e5f\u53ea\u9700\u8981\u8fd0\u884c\u5b83\u4e00\u6b21\uff0c\u90a3\u4e48\u4efb\u52a1\u5c31\u5df2\u7ecf\u5b8c\u6210\u4e86\u3002 \u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u5b8c\u5168\u662f\u7ebf\u6027\u7684(\u6ca1\u6709\u6761\u4ef6\uff0c\u5982\u201cIf\u8bed\u53e5\u201d\u6216\u201cswitch\u8bed\u53e5\u201d)\uff0c\u4e0d\u63a5\u53d7\u8f93\u5165\uff0c\u5e76\u4ea7\u751f\u6b63\u786e\u7684\u7b54\u6848\uff0c\u90a3\u4e48\u4f60\u5c31\u5b8c\u6210\u4e86\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u4f60\u5df2\u7ecf\u901a\u8fc7\u8fd0\u884c\u548c\u9a8c\u8bc1\u8f93\u51fa\u6d4b\u8bd5\u4e86\u6574\u4e2a\u7a0b\u5e8f\u3002 \u4f46\u66f4\u6709\u53ef\u80fd\u7684\u60c5\u51b5\u662f\uff0c\u4f60\u7684\u7a0b\u5e8f\u9700\u8981\u88ab\u591a\u6b21\u8fd0\u884c\uff0c\u800c\u4e14\u8be5\u7a0b\u5e8f\u4f7f\u7528\u5faa\u73af\u548c\u6761\u4ef6\u903b\u8f91\uff0c\u80fd\u591f\u63a5\u53d7\u67d0\u79cd\u7c7b\u578b\u7684\u7528\u6237\u8f93\u5165\u3002\u4f60\u53ef\u80fd\u5df2\u7ecf\u7f16\u5199\u4e86\u53ef\u4ee5\u5728\u5c06\u6765\u7684\u5176\u4ed6\u7a0b\u5e8f\u4e2d\u91cd\u7528\u7684\u51fd\u6570\uff0c\u4e5f\u53ef\u80fd\u7ecf\u6dfb\u52a0\u4e86\u4e00\u4e9b\u6700\u521d\u6ca1\u6709\u8ba1\u5212\u7684\u65b0\u529f\u80fd\u3002\u4e5f\u8bb8\u8fd8\u6253\u7b97\u5c06\u8fd9\u4e2a\u7a0b\u5e8f\u5206\u53d1\u7ed9\u5176\u4ed6\u4eba(\u7528\u6237\u53ef\u80fd\u4f1a\u505a\u51fa\u4f60\u610f\u6599\u4e4b\u5916\u7684\u64cd\u4f5c)\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5fc5\u987b\u9a8c\u8bc1\u7a0b\u5e8f\u5728\u5404\u79cd\u5404\u6837\u7684\u6761\u4ef6\u4e0b\u90fd\u80fd\u6b63\u786e\u5de5\u4f5c\u2014\u2014\u8fd9\u9700\u8981\u6d4b\u8bd5\u6765\u4fdd\u8bc1\u3002 \u4ec5\u4ec5\u56e0\u4e3a\u4f60\u7684\u7a0b\u5e8f\u9002\u7528\u4e8e\u4e00\u7ec4\u8f93\u5165\u5e76\u4e0d\u610f\u5473\u7740\u5b83\u5728\u6240\u6709\u60c5\u51b5\u4e0b\u90fd\u80fd\u6b63\u786e\u5de5\u4f5c\u3002 \u8f6f\u4ef6\u9a8c\u8bc1(\u4e5f\u79f0\u4e3a\u8f6f\u4ef6\u6d4b\u8bd5)\u662f\u786e\u5b9a\u8f6f\u4ef6\u5728\u6240\u6709\u60c5\u51b5\u4e0b\u662f\u5426\u6309\u9884\u671f\u5de5\u4f5c\u7684\u8fc7\u7a0b\u3002 \u6709\u5173\u6d4b\u8bd5\u7684\u96be\u9898 \u5728\u8ba8\u8bba\u6d4b\u8bd5\u4ee3\u7801\u7684\u5b9e\u7528\u65b9\u6cd5\u4e4b\u524d\uff0c\u8ba9\u6211\u4eec\u5148\u8c08\u8c08\u4e3a\u4ec0\u4e48\u5168\u9762\u6d4b\u8bd5\u7a0b\u5e8f\u5e76\u4e0d\u5bb9\u6613\u3002 \u8003\u8651\u8fd9\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include void compare ( int x , int y ) { if ( x > y ) std :: cout << x << \" is greater than \" << y << '\\n' ; // case 1 else if ( x < y ) std :: cout << x << \" is less than \" << y << '\\n' ; // case 2 else std :: cout << x << \" is equal to \" << y << '\\n' ; // case 3 } int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another number: \" ; int y {}; std :: cin >> y ; compare ( x , y ); return 0 ; } \u5047\u8bbe\u6709\u4e00\u4e2a4\u5b57\u8282\u7684\u6574\u6570\uff0c\u7528\u6bcf\u79cd\u53ef\u80fd\u7684\u8f93\u5165\u7ec4\u5408\u663e\u5f0f\u5730\u6d4b\u8bd5\u8fd9\u4e2a\u7a0b\u5e8f\u5c06\u9700\u8981\u8fd0\u884c\u7a0b\u5e8f18,446,744,073,709,551,616\u6b21(~18\u4ebf\u4ebf\u6b21)\u3002\u663e\u7136\u8fd9\u662f\u4e0d\u53ef\u884c\u7684\u3002 \u5f53\u7a0b\u5e8f\u8981\u6c42\u7528\u6237\u8f93\u5165\u6216\u8005\u5728\u4ee3\u7801\u4e2d\u5b58\u5728\u6761\u4ef6\u5224\u65ad\u65f6\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u6267\u884c\u7684\u65b9\u5f0f\u7684\u5c31\u76f8\u5e94\u5730\u53d8\u591a\u4e86\u3002\u9664\u4e86\u6700\u7b80\u5355\u7684\u7a0b\u5e8f\u4e4b\u5916\uff0c\u51e0\u4e4e\u4e0d\u53ef\u80fd\u663e\u5f0f\u5730\u6d4b\u8bd5\u6bcf\u79cd\u8f93\u5165\u7ec4\u5408\u3002 \u73b0\u5728\uff0c\u76f4\u89c9\u544a\u8bc9\u6211\u4eec\uff0c\u5176\u5b9e\u5e76\u4e0d\u9700\u8981\u771f\u7684\u8fd0\u884c\u7a0b\u5e8f18\u4ebf\u4ebf\u6b21\u624d\u80fd\u786e\u4fdd\u5b83\u53ef\u4ee5\u6b63\u786e\u5de5\u4f5c\u3002\u4f60\u53ef\u4ee5\u5408\u7406\u5730\u5f97\u51fa\u7ed3\u8bba\uff0c\u5982\u679c\u60c5\u51b51\u9002\u7528\u4e8e\u4e00\u5bf9 x \u548c y \u503c\uff0c\u5176\u4e2d x > y \uff0c\u90a3\u4e48\u5b83\u5e94\u8be5\u9002\u7528\u4e8e\u4efb\u4f55\u4e00\u5bf9\u6ee1\u8db3 x>y \u7684 x \u548c y \u7ec4\u5408\u3002\u9274\u4e8e\u6b64\uff0c\u7a0b\u5e8f\u5b9e\u9645\u4e0a\u53ea\u9700\u8981\u8fd0\u884c\u8fd9\u4e2a\u5927\u7ea6\u4e09\u6b21(\u5728\u51fd\u6570 compare() \u4e2d\u5206\u522b\u8fd0\u884c\u4e00\u6b21)\uff0c\u5c31\u53ef\u4ee5\u9ad8\u5ea6\u786e\u4fe1\u5b83\u80fd\u6309\u9884\u671f\u5de5\u4f5c\u3002\u4e3a\u4e86\u4f7f\u6d4b\u8bd5\u6613\u4e8e\u7ba1\u7406\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u7c7b\u4f3c\u7684\u6280\u5de7\u6765\u5927\u5e45\u51cf\u5c11\u6d4b\u8bd5\u7684\u6b21\u6570\u3002 \u5173\u4e8e\u6d4b\u8bd5\u65b9\u6cd5\u6709\u5f88\u591a\u4e1c\u897f\u53ef\u4ee5\u5199\u2014\u2014\u4e8b\u5b9e\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u6b64\u5199\u4e00\u6574\u7ae0\u3002\u4f46\u7531\u4e8e\u8fd9\u4e0d\u662fC++\u6559\u7a0b\u5e94\u8be5\u5173\u6ce8\u7684\u4e8b\u60c5\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u4f1a\u7b80\u5355\u4ecb\u7ecd\u4f5c\u4e3a\u7a0b\u5e8f\u5458\u5e94\u8be5\u5982\u4f55\u6d4b\u8bd5\u81ea\u5df1\u7684\u4ee3\u7801\u3002\u5728\u63a5\u4e0b\u6765\u7684\u51e0\u4e2a\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u4e00\u4e9b\u5728\u6d4b\u8bd5\u4ee3\u7801\u65f6\u5e94\u8be5\u8003\u8651\u7684\u5b9e\u9645\u95ee\u9898\u3002 \u5316\u6574\u4e3a\u96f6\u8fdb\u884c\u6d4b\u8bd5 \u5047\u8bbe\u4f60\u662f\u4e00\u5bb6\u6c7d\u8f66\u5236\u9020\u5546\uff0c\u6b63\u5728\u5236\u9020\u4e00\u8f86\u5ba2\u6237\u6982\u5ff5\u8f66\u3002\u90a3\u4e48\u4f60\u4f1a\u600e\u4e48\u505a\u5462\uff1f a) \u5728\u7ec4\u88c5\u524d\u5236\u9020 (\u6216\u8d2d\u4e70) \u5e76\u6d4b\u8bd5\u5404\u4e2a\u96f6\u90e8\u4ef6\uff0c\u5982\u679c\u96f6\u90e8\u4ef6\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\uff0c\u5219\u5c06\u5176\u7ec4\u88c5\u5230\u6c7d\u8f66\u4e0a\uff0c\u786e\u4fdd\u4ecd\u7136\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u3002\u6700\u540e\uff0c\u5bf9\u6574\u8f66\u8fdb\u884c\u6d4b\u8bd5\uff0c\u786e\u4fdd\u6240\u6709\u90e8\u5206\u90fd\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u3002 b) \u7528\u6240\u6709\u7684\u90e8\u4ef6\u4e00\u6c14\u5475\u6210\u5730\u9020\u51fa\u4e00\u8f86\u8f66\uff0c\u7136\u540e\u5728\u6700\u540e\u5bf9\u6574\u4e2a\u90e8\u4ef6\u8fdb\u884c\u7b2c\u4e00\u6b21\u6d4b\u8bd5\u3002 \u5f88\u660e\u663e\u9009\u9879a)\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u7136\u800c\uff0c\u8bb8\u591a\u65b0\u7a0b\u5e8f\u5458\u4f1a\u6309\u7167 b) \u63cf\u8ff0\u7684\u65b9\u6cd5\u7f16\u5199\u4ee3\u7801\uff01 \u5728b)\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u6c7d\u8f66\u4e2d\u6709\u4efb\u4f55\u90e8\u4ef6\u6ca1\u6709\u6309\u7167\u9884\u671f\u5de5\u4f5c\uff0c\u5de5\u7a0b\u5e08\u5c31\u5fc5\u987b\u5bf9\u6574\u8f86\u8f66\u8fdb\u884c\u8bca\u65ad\uff0c\u4ee5\u786e\u5b9a\u54ea\u91cc\u51fa\u4e86\u95ee\u9898\u2014\u2014\u95ee\u9898\u53ef\u80fd\u5728\u4efb\u4f55\u5730\u65b9\u3002\u4e00\u4e2a\u75c7\u72b6\u53ef\u80fd\u6709\u5f88\u591a\u539f\u56e0\u2014\u2014\u4f8b\u5982\uff0c\u7531\u4e8e\u706b\u82b1\u585e\u3001\u7535\u6c60\u3001\u71c3\u6cb9\u6cf5\u6216\u5176\u4ed6\u539f\u56e0\uff0c\u6c7d\u8f66\u65e0\u6cd5\u542f\u52a8\uff1f\u8fd9\u65e0\u7591\u4f1a\u6d6a\u8d39\u5927\u91cf\u65f6\u95f4\u3002\u5982\u679c\u53d1\u73b0\u4e86\u95ee\u9898\uff0c\u540e\u679c\u53ef\u80fd\u662f\u707e\u96be\u6027\u7684\u2014\u2014\u4e00\u5904\u4fee\u6539\u53ef\u80fd\u4f1a\u5728\u5176\u4ed6\u591a\u4e2a\u5730\u65b9\u5f15\u8d77\u201c\u6d9f\u6f2a\u6548\u5e94\u201d(\u53d8\u5316)\u3002\u4f8b\u5982\uff0c\u71c3\u6cb9\u6cf5\u592a\u5c0f\u53ef\u80fd\u4f1a\u5bfc\u81f4\u53d1\u52a8\u673a\u91cd\u65b0\u8bbe\u8ba1\uff0c\u4ece\u800c\u5bfc\u81f4\u6c7d\u8f66\u6846\u67b6\u7684\u91cd\u65b0\u8bbe\u8ba1\u3002\u5728\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u4f1a\u91cd\u65b0\u8bbe\u8ba1\u6c7d\u8f66\u7684\u4e00\u4e2a\u5f88\u5927\u7684\u90e8\u5206\uff0c\u53ea\u662f\u4e3a\u4e86\u9002\u5e94\u6700\u521d\u7684\u4e00\u4e2a\u5c0f\u95ee\u9898!\u5f88\u660e\u663e\u9009\u9879a)\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u7136\u800c\uff0c\u8bb8\u591a\u65b0\u7a0b\u5e8f\u5458\u7f16\u5199\u7c7b\u4f3c\u9009\u9879b)\u7684\u4ee3\u7801! \u5bf9\u4e8e a) \u7684\u60c5\u51b5\uff0c\u6c7d\u8f66\u516c\u53f8\u4e00\u8fb9\u7ec4\u88c5\u4e00\u8fb9\u6d4b\u8bd5\u3002\u5982\u679c\u4efb\u4f55\u96f6\u4ef6\u662f\u574f\u7684\uff0c\u4ed6\u4eec\u4f1a\u7acb\u5373\u77e5\u9053\uff0c\u5e76\u80fd\u4fee\u590d/\u66f4\u6362\u5b83\u3002\u4efb\u4f55\u4e1c\u897f\u90fd\u4e0d\u4f1a\u88ab\u96c6\u6210\u5230\u6c7d\u8f66\u4e2d\uff0c\u76f4\u5230\u5b83\u88ab\u8bc1\u660e\u53ef\u4ee5\u5de5\u4f5c\uff0c\u7136\u540e\u4e00\u65e6\u5b83\u88ab\u96c6\u6210\u5230\u6c7d\u8f66\u4e2d\uff0c\u5c31\u4f1a\u518d\u6b21\u8fdb\u884c\u6d4b\u8bd5\u3002\u8fd9\u6837\uff0c\u4efb\u4f55\u610f\u5916\u7684\u95ee\u9898\u90fd\u53ef\u4ee5\u5c3d\u65e9\u53d1\u73b0\uff0c\u800c\u5b83\u4eec\u4ecd\u7136\u662f\u53ef\u4ee5\u8f7b\u677e\u89e3\u51b3\u7684\u5c0f\u95ee\u9898\u3002 \u5f00\u59cb\u7ec4\u88c5\u6574\u8f86\u8f66\u7684\u65f6\u5019\uff0c\u5236\u9020\u5546\u5e94\u8be5\u5bf9\u6c7d\u8f66\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u6709\u5408\u7406\u7684\u4fe1\u5fc3\u2014\u2014\u6bd5\u7adf\uff0c\u6240\u6709\u7684\u90e8\u4ef6\u90fd\u5df2\u7ecf\u5355\u72ec\u6d4b\u8bd5\u8fc7\u4e86\uff0c\u5e76\u4e14\u5728\u521d\u59cb\u96c6\u6210\u65f6\u4e5f\u8fdb\u884c\u4e86\u6d4b\u8bd5\u3002\u5728\u8fd9\u4e00\u70b9\u4e0a\u4ecd\u7136\u6709\u53ef\u80fd\u53d1\u73b0\u610f\u5916\u7684\u95ee\u9898\uff0c\u4f46\u662f\u901a\u8fc7\u6240\u6709\u5148\u524d\u7684\u6d4b\u8bd5\u5c06\u98ce\u9669\u964d\u5230\u6700\u4f4e\u3002 \u4e0a\u9762\u7684\u7c7b\u6bd4\u4e5f\u9002\u7528\u4e8e\u7a0b\u5e8f\uff0c\u5c3d\u7ba1\u7531\u4e8e\u67d0\u79cd\u539f\u56e0\uff0c\u65b0\u7a0b\u5e8f\u5458\u7ecf\u5e38\u6ca1\u6709\u610f\u8bc6\u5230\u8fd9\u4e00\u70b9\u3002\u6700\u597d\u662f\u7f16\u5199\u5c0f\u51fd\u6570(\u6216\u7c7b)\uff0c\u7136\u540e\u7acb\u5373\u7f16\u8bd1\u548c\u6d4b\u8bd5\u5b83\u4eec\u3002\u8fd9\u6837\uff0c\u5982\u679c\u4f60\u72af\u4e86\u9519\u8bef\u5c31\u4f1a\u77e5\u9053\u5b83\u4e00\u5b9a\u662f\u5728\u4e0a\u6b21\u7f16\u8bd1/\u6d4b\u8bd5\u540e\u66f4\u6539\u7684\u5c11\u91cf\u4ee3\u7801\u4e2d\u3002\u8fd9\u610f\u5473\u7740\u8981\u67e5\u770b\u7684\u5730\u65b9\u66f4\u5c11\uff0c\u82b1\u5728\u8c03\u8bd5\u4e0a\u7684\u65f6\u95f4\u4e5f\u66f4\u5c11\u3002 \u5355\u72ec\u6d4b\u8bd5\u4e00\u5c0f\u90e8\u5206\u4ee3\u7801\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u201c\u5355\u5143\u201d\u662f\u6b63\u786e\u7684\uff0c\u8fd9\u79f0\u4e3a\u5355\u5143\u6d4b\u8bd5\u3002\u6bcf\u4e2a\u5355\u5143\u6d4b\u8bd5\u90fd\u88ab\u8bbe\u8ba1\u7528\u6765\u786e\u4fdd\u5355\u5143\u7684\u7279\u5b9a\u884c\u4e3a\u662f\u6b63\u786e\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u7528\u5b9a\u4e49\u826f\u597d\u7684\u5c0f\u5355\u5143(\u51fd\u6570\u6216\u7c7b)\u7f16\u5199\u7a0b\u5e8f\uff0c\u9891\u7e41\u7f16\u8bd1\uff0c\u5e76\u5728\u8fd0\u884c\u65f6\u6d4b\u8bd5\u4ee3\u7801\u3002 \u5982\u679c\u7a0b\u5e8f\u5f88\u77ed\u5e76\u4e14\u63a5\u53d7\u7528\u6237\u8f93\u5165\uff0c\u90a3\u4e48\u5c1d\u8bd5\u5404\u79cd\u7528\u6237\u8f93\u5165\u53ef\u80fd\u5c31\u8db3\u591f\u4e86\u3002\u4f46\u662f\u968f\u7740\u7a0b\u5e8f\u53d8\u5f97\u8d8a\u6765\u8d8a\u957f\uff0c\u8fd9\u5c31\u4e0d\u591f\u4e86\uff0c\u5728\u5c06\u5355\u4e2a\u51fd\u6570\u6216\u7c7b\u96c6\u6210\u5230\u7a0b\u5e8f\u7684\u5176\u4f59\u90e8\u5206\u4e4b\u524d\u5c31\u5bf9\u5b83\u4eec\u8fdb\u884c\u6d4b\u8bd5\u6548\u679c\u4f1a\u66f4\u597d\u3002 \u90a3\u4e48\u6211\u4eec\u5982\u4f55\u4ee5\u5355\u4f4d\u6765\u6d4b\u8bd5\u4ee3\u7801\u5462? \u975e\u6b63\u5f0f\u6d4b\u8bd5 \u6d4b\u8bd5\u4ee3\u7801\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u5728\u7f16\u5199\u7a0b\u5e8f\u65f6\u8fdb\u884c\u975e\u6b63\u5f0f\u6d4b\u8bd5\u3002\u5728\u7f16\u5199\u4e86\u4e00\u4e2a\u4ee3\u7801\u5355\u5143(\u4e00\u4e2a\u51fd\u6570\u3001\u4e00\u4e2a\u7c7b\u6216\u5176\u4ed6\u4e00\u4e9b\u79bb\u6563\u7684\u201c\u5305\u201d\u4ee3\u7801)\u4e4b\u540e\uff0c\u53ef\u4ee5\u7f16\u5199\u4e00\u4e9b\u4ee3\u7801\u6765\u6d4b\u8bd5\u521a\u521a\u6dfb\u52a0\u7684\u5355\u5143\uff0c\u7136\u540e\u5728\u6d4b\u8bd5\u901a\u8fc7\u540e\u5220\u9664\u6d4b\u8bd5\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e0b\u9762\u7684isLowerVowel()\u51fd\u6570\uff0c\u5219\u53ef\u4ee5\u7f16\u5199\u4ee5\u4e0b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include // We want to test the following function bool isLowerVowel ( char c ) { switch ( c ) { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : return true ; default : return false ; } } int main () { // So here's our temporary tests to validate it works std :: cout << isLowerVowel ( 'a' ); // temporary test code, should produce 1 std :: cout << isLowerVowel ( 'q' ); // temporary test code, should produce 0 return 0 ; } \u5982\u679c\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u201c 1 \u201d\u548c\u201c 0 \u201d\uff0c\u90a3\u4e48\u7a0b\u5e8f\u5c31\u662f\u6b63\u786e\u7684\u3002\u4f60\u7684\u51fd\u6570\u9002\u7528\u4e8e\u4e00\u4e9b\u57fa\u672c\u60c5\u51b5\uff0c\u968f\u540e\u4f60\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u4ee3\u7801\u5408\u7406\u5730\u63a8\u65ad\u5b83\u4e5f\u9002\u7528\u4e8e\u90a3\u4e9b\u5c1a\u672a\u6d4b\u8bd5\u7684\u60c5\u51b5( e \uff0c i \uff0c o \u548c u )\u3002\u56e0\u6b64\uff0c\u4f60\u53ef\u4ee5\u5220\u9664\u4e34\u65f6\u6d4b\u8bd5\u4ee3\u7801\uff0c\u5e76\u7ee7\u7eed\u7f16\u7a0b\u3002 \u4fdd\u5b58\u6d4b\u8bd5 \u5c3d\u7ba1\u7f16\u5199\u4e34\u65f6\u7684\u6d4b\u8bd5\u4ee3\u7801\u53ef\u4ee5\u7b80\u5355\u5feb\u901f\u5730\u6d4b\u8bd5\u7a0b\u5e8f\uff0c\u4f46\u5982\u679c\u7a0d\u540e\u8fd8\u9700\u8981\u6d4b\u8bd5\u7a0b\u5e8f\u7684\u8bdd\uff0c\u5c31\u4e0d\u80fd\u4f9d\u8d56\u8fd9\u4e9b\u4e34\u65f6\u7684\u6d4b\u8bd5\u4ee3\u7801\u4e86\u3002\u5f53\u6211\u4eec\u4fee\u6539\u4e86\u4e00\u4e2a\u51fd\u6570\u5e76\u6dfb\u52a0\u65b0\u7684\u529f\u80fd\u540e\uff0c\u5f80\u5f80\u5e0c\u671b\u786e\u4fdd\u6ca1\u6709\u7834\u574f\u7a0b\u5e8f\u7684\u5176\u4ed6\u90e8\u5206\u3002\u51fa\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u5c06\u6d4b\u8bd5\u4ee3\u7801\u4fdd\u5b58\u8d77\u6765\u4ee5\u4fbf\u5c06\u6765\u53ef\u4ee5\u518d\u6b21\u8fd0\u884c\u662f\u66f4\u52a0\u6709\u610f\u4e49\u7684\u505a\u6cd5\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5c06\u6d4b\u8bd5\u79fb\u52a8\u5230 testVowel() \u51fd\u6570\u4e2d\uff0c\u800c\u4e0d\u662f\u5220\u9664\u4f60\u7684\u4e34\u65f6\u6d4b\u8bd5\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include bool isLowerVowel ( char c ) { switch ( c ) { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : return true ; default : return false ; } } // Not called from anywhere right now // But here if you want to retest things later void testVowel () { std :: cout << isLowerVowel ( 'a' ); // temporary test code, should produce 1 std :: cout << isLowerVowel ( 'q' ); // temporary test code, should produce 0 } int main () { return 0 ; } \u5728\u521b\u5efa\u66f4\u591a\u6d4b\u8bd5\u65f6\uff0c\u53ef\u4ee5\u5c06\u5b83\u4eec\u6dfb\u52a0\u5230 testVowel() \u51fd\u6570\u4e2d\u3002 \u81ea\u52a8\u5316\u6d4b\u8bd5 \u4e0a\u8ff0\u6d4b\u8bd5\u51fd\u6570\u7684\u4e00\u4e2a\u95ee\u9898\u662f\uff0c\u5b83\u4f9d\u8d56\u4e8e\u4f60\u5728\u8fd0\u884c\u65f6\u624b\u52a8\u9a8c\u8bc1\u7ed3\u679c\u3002\u8fd9\u8981\u6c42\u4f60\u8981\u8bb0\u4f4f\u671f\u671b\u7684\u7ed3\u679c\u662f\u4ec0\u4e48(\u5047\u8bbe\u60a8\u6ca1\u6709\u8bb0\u5f55\u5b83)\uff0c\u5e76\u624b\u52a8\u5c06\u5b9e\u9645\u7ed3\u679c\u4e0e\u9884\u671f\u7ed3\u679c\u8fdb\u884c\u6bd4\u8f83\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u4e00\u4e2a\u5305\u542b\u6d4b\u8bd5\u548c\u9884\u671f\u7b54\u6848\u7684\u6d4b\u8bd5\u51fd\u6570\u6765\u505a\u5f97\u66f4\u597d\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u4e0d\u5fc5\u5bf9\u5b83\u4eec\u8fdb\u884c\u4eba\u5de5\u6bd4\u8f83\u4e86\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include bool isLowerVowel ( char c ) { switch ( c ) { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : return true ; default : return false ; } } // returns the number of the test that failed, or 0 if all tests passed int testVowel () { if ( isLowerVowel ( 'a' ) != true ) return 1 ; if ( isLowerVowel ( 'q' ) != false ) return 2 ; return 0 ; } int main () { return 0 ; } \u8fd9\u6837\u4e00\u6765\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728\u4efb\u4f55\u65f6\u5019\u8c03\u7528 testVowel() \u51fd\u6570\u6765\u6d4b\u8bd5\u7a0b\u5e8f\uff0c\u786e\u4fdd\u5b83\u59cb\u7ec8\u80fd\u591f\u6b63\u786e\u8fd0\u884c\u3002\u6d4b\u8bd5\u4ee3\u7801\u53ef\u4ee5\u81ea\u52a8\u5e2e\u4f60\u6d4b\u8bd5\u6240\u6709\u7528\u4f8b\uff0c\u8fd4\u56de\u201call good\u201d\uff08\u8fd4\u56de\u503c0\uff09\uff0c\u6216\u8005\u8fd4\u56de\u5931\u8d25\u7528\u4f8b\u7684\u5e8f\u53f7\u3002\u8fd9\u6837\u7684\u6d4b\u8bd5\u5728\u6211\u4eec\u4fee\u6539\u8001\u4ee3\u7801\u65f6\u5c24\u4e3a\u91cd\u8981\uff0c\u56e0\u4e3a\u8c01\u4e5f\u4e0d\u5e0c\u671b\u65e0\u610f\u95f4\u7834\u574f\u7a0b\u5e8f\u539f\u672c\u6b63\u5e38\u7684\u529f\u80fd\uff01 \u5355\u5143\u6d4b\u8bd5\u6846\u67b6 \u56e0\u4e3a\u7f16\u5199\u51fd\u6570\u6765\u6267\u884c\u5176\u4ed6\u51fd\u6570\u662f\u975e\u5e38\u5e38\u89c1\u548c\u6709\u7528\u7684\u9700\u6c42\uff0c\u6240\u4ee5\u6709\u4e00\u4e9b\u5b8c\u6574\u7684\u6846\u67b6(\u79f0\u4e3a\u5355\u5143\u6d4b\u8bd5\u6846\u67b6)\u65e8\u5728\u5e2e\u52a9\u7b80\u5316\u7f16\u5199\u3001\u7ef4\u62a4\u548c\u6267\u884c\u5355\u5143\u6d4b\u8bd5\u7684\u8fc7\u7a0b\u3002\u56e0\u4e3a\u8fd9\u4e9b\u6d89\u53ca\u5230\u7b2c\u4e09\u65b9\u8f6f\u4ef6\uff0c\u6240\u4ee5\u6211\u4eec\u5728\u8fd9\u91cc\u4e0d\u505a\u4ecb\u7ecd\uff0c\u4f46\u662f\u4f60\u5e94\u8be5\u77e5\u9053\u5b83\u4eec\u7684\u5b58\u5728\u3002 \u96c6\u6210\u6d4b\u8bd5 \u4e00\u65e6\u7a0b\u5e8f\u7684\u6bcf\u4e2a\u5355\u5143\u90fd\u5355\u72ec\u6d4b\u8bd5\u8fc7\uff0c\u5b83\u4eec\u5c31\u53ef\u4ee5\u88ab\u96c6\u6210\u5230\u4f60\u7684\u7a0b\u5e8f\u4e2d\uff0c\u5e76\u91cd\u65b0\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u88ab\u6b63\u786e\u5730\u96c6\u6210\u4e86\u3002\u8fd9\u5c31\u662f\u6240\u8c13\u7684\u96c6\u6210\u6d4b\u8bd5\u3002\u96c6\u6210\u6d4b\u8bd5\u5f80\u5f80\u66f4\u52a0\u590d\u6742\u2014\u2014\u76ee\u524d\uff0c\u8fd0\u884c\u51e0\u6b21\u7a0b\u5e8f\u5e76\u62bd\u68c0\u96c6\u6210\u5355\u5143\u7684\u884c\u4e3a\u5c31\u8db3\u591f\u4e86\u3002","title":"7.12 - \u4ee3\u7801\u6d4b\u8bd5"},{"location":"7-12-Introduction-to-testing-your-code/#_1","text":"\u5728\u8ba8\u8bba\u6d4b\u8bd5\u4ee3\u7801\u7684\u5b9e\u7528\u65b9\u6cd5\u4e4b\u524d\uff0c\u8ba9\u6211\u4eec\u5148\u8c08\u8c08\u4e3a\u4ec0\u4e48\u5168\u9762\u6d4b\u8bd5\u7a0b\u5e8f\u5e76\u4e0d\u5bb9\u6613\u3002 \u8003\u8651\u8fd9\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include void compare ( int x , int y ) { if ( x > y ) std :: cout << x << \" is greater than \" << y << '\\n' ; // case 1 else if ( x < y ) std :: cout << x << \" is less than \" << y << '\\n' ; // case 2 else std :: cout << x << \" is equal to \" << y << '\\n' ; // case 3 } int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another number: \" ; int y {}; std :: cin >> y ; compare ( x , y ); return 0 ; } \u5047\u8bbe\u6709\u4e00\u4e2a4\u5b57\u8282\u7684\u6574\u6570\uff0c\u7528\u6bcf\u79cd\u53ef\u80fd\u7684\u8f93\u5165\u7ec4\u5408\u663e\u5f0f\u5730\u6d4b\u8bd5\u8fd9\u4e2a\u7a0b\u5e8f\u5c06\u9700\u8981\u8fd0\u884c\u7a0b\u5e8f18,446,744,073,709,551,616\u6b21(~18\u4ebf\u4ebf\u6b21)\u3002\u663e\u7136\u8fd9\u662f\u4e0d\u53ef\u884c\u7684\u3002 \u5f53\u7a0b\u5e8f\u8981\u6c42\u7528\u6237\u8f93\u5165\u6216\u8005\u5728\u4ee3\u7801\u4e2d\u5b58\u5728\u6761\u4ef6\u5224\u65ad\u65f6\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u6267\u884c\u7684\u65b9\u5f0f\u7684\u5c31\u76f8\u5e94\u5730\u53d8\u591a\u4e86\u3002\u9664\u4e86\u6700\u7b80\u5355\u7684\u7a0b\u5e8f\u4e4b\u5916\uff0c\u51e0\u4e4e\u4e0d\u53ef\u80fd\u663e\u5f0f\u5730\u6d4b\u8bd5\u6bcf\u79cd\u8f93\u5165\u7ec4\u5408\u3002 \u73b0\u5728\uff0c\u76f4\u89c9\u544a\u8bc9\u6211\u4eec\uff0c\u5176\u5b9e\u5e76\u4e0d\u9700\u8981\u771f\u7684\u8fd0\u884c\u7a0b\u5e8f18\u4ebf\u4ebf\u6b21\u624d\u80fd\u786e\u4fdd\u5b83\u53ef\u4ee5\u6b63\u786e\u5de5\u4f5c\u3002\u4f60\u53ef\u4ee5\u5408\u7406\u5730\u5f97\u51fa\u7ed3\u8bba\uff0c\u5982\u679c\u60c5\u51b51\u9002\u7528\u4e8e\u4e00\u5bf9 x \u548c y \u503c\uff0c\u5176\u4e2d x > y \uff0c\u90a3\u4e48\u5b83\u5e94\u8be5\u9002\u7528\u4e8e\u4efb\u4f55\u4e00\u5bf9\u6ee1\u8db3 x>y \u7684 x \u548c y \u7ec4\u5408\u3002\u9274\u4e8e\u6b64\uff0c\u7a0b\u5e8f\u5b9e\u9645\u4e0a\u53ea\u9700\u8981\u8fd0\u884c\u8fd9\u4e2a\u5927\u7ea6\u4e09\u6b21(\u5728\u51fd\u6570 compare() \u4e2d\u5206\u522b\u8fd0\u884c\u4e00\u6b21)\uff0c\u5c31\u53ef\u4ee5\u9ad8\u5ea6\u786e\u4fe1\u5b83\u80fd\u6309\u9884\u671f\u5de5\u4f5c\u3002\u4e3a\u4e86\u4f7f\u6d4b\u8bd5\u6613\u4e8e\u7ba1\u7406\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u7c7b\u4f3c\u7684\u6280\u5de7\u6765\u5927\u5e45\u51cf\u5c11\u6d4b\u8bd5\u7684\u6b21\u6570\u3002 \u5173\u4e8e\u6d4b\u8bd5\u65b9\u6cd5\u6709\u5f88\u591a\u4e1c\u897f\u53ef\u4ee5\u5199\u2014\u2014\u4e8b\u5b9e\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u6b64\u5199\u4e00\u6574\u7ae0\u3002\u4f46\u7531\u4e8e\u8fd9\u4e0d\u662fC++\u6559\u7a0b\u5e94\u8be5\u5173\u6ce8\u7684\u4e8b\u60c5\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u4f1a\u7b80\u5355\u4ecb\u7ecd\u4f5c\u4e3a\u7a0b\u5e8f\u5458\u5e94\u8be5\u5982\u4f55\u6d4b\u8bd5\u81ea\u5df1\u7684\u4ee3\u7801\u3002\u5728\u63a5\u4e0b\u6765\u7684\u51e0\u4e2a\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u4e00\u4e9b\u5728\u6d4b\u8bd5\u4ee3\u7801\u65f6\u5e94\u8be5\u8003\u8651\u7684\u5b9e\u9645\u95ee\u9898\u3002","title":"\u6709\u5173\u6d4b\u8bd5\u7684\u96be\u9898"},{"location":"7-12-Introduction-to-testing-your-code/#_2","text":"\u5047\u8bbe\u4f60\u662f\u4e00\u5bb6\u6c7d\u8f66\u5236\u9020\u5546\uff0c\u6b63\u5728\u5236\u9020\u4e00\u8f86\u5ba2\u6237\u6982\u5ff5\u8f66\u3002\u90a3\u4e48\u4f60\u4f1a\u600e\u4e48\u505a\u5462\uff1f a) \u5728\u7ec4\u88c5\u524d\u5236\u9020 (\u6216\u8d2d\u4e70) \u5e76\u6d4b\u8bd5\u5404\u4e2a\u96f6\u90e8\u4ef6\uff0c\u5982\u679c\u96f6\u90e8\u4ef6\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\uff0c\u5219\u5c06\u5176\u7ec4\u88c5\u5230\u6c7d\u8f66\u4e0a\uff0c\u786e\u4fdd\u4ecd\u7136\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u3002\u6700\u540e\uff0c\u5bf9\u6574\u8f66\u8fdb\u884c\u6d4b\u8bd5\uff0c\u786e\u4fdd\u6240\u6709\u90e8\u5206\u90fd\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u3002 b) \u7528\u6240\u6709\u7684\u90e8\u4ef6\u4e00\u6c14\u5475\u6210\u5730\u9020\u51fa\u4e00\u8f86\u8f66\uff0c\u7136\u540e\u5728\u6700\u540e\u5bf9\u6574\u4e2a\u90e8\u4ef6\u8fdb\u884c\u7b2c\u4e00\u6b21\u6d4b\u8bd5\u3002 \u5f88\u660e\u663e\u9009\u9879a)\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u7136\u800c\uff0c\u8bb8\u591a\u65b0\u7a0b\u5e8f\u5458\u4f1a\u6309\u7167 b) \u63cf\u8ff0\u7684\u65b9\u6cd5\u7f16\u5199\u4ee3\u7801\uff01 \u5728b)\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u6c7d\u8f66\u4e2d\u6709\u4efb\u4f55\u90e8\u4ef6\u6ca1\u6709\u6309\u7167\u9884\u671f\u5de5\u4f5c\uff0c\u5de5\u7a0b\u5e08\u5c31\u5fc5\u987b\u5bf9\u6574\u8f86\u8f66\u8fdb\u884c\u8bca\u65ad\uff0c\u4ee5\u786e\u5b9a\u54ea\u91cc\u51fa\u4e86\u95ee\u9898\u2014\u2014\u95ee\u9898\u53ef\u80fd\u5728\u4efb\u4f55\u5730\u65b9\u3002\u4e00\u4e2a\u75c7\u72b6\u53ef\u80fd\u6709\u5f88\u591a\u539f\u56e0\u2014\u2014\u4f8b\u5982\uff0c\u7531\u4e8e\u706b\u82b1\u585e\u3001\u7535\u6c60\u3001\u71c3\u6cb9\u6cf5\u6216\u5176\u4ed6\u539f\u56e0\uff0c\u6c7d\u8f66\u65e0\u6cd5\u542f\u52a8\uff1f\u8fd9\u65e0\u7591\u4f1a\u6d6a\u8d39\u5927\u91cf\u65f6\u95f4\u3002\u5982\u679c\u53d1\u73b0\u4e86\u95ee\u9898\uff0c\u540e\u679c\u53ef\u80fd\u662f\u707e\u96be\u6027\u7684\u2014\u2014\u4e00\u5904\u4fee\u6539\u53ef\u80fd\u4f1a\u5728\u5176\u4ed6\u591a\u4e2a\u5730\u65b9\u5f15\u8d77\u201c\u6d9f\u6f2a\u6548\u5e94\u201d(\u53d8\u5316)\u3002\u4f8b\u5982\uff0c\u71c3\u6cb9\u6cf5\u592a\u5c0f\u53ef\u80fd\u4f1a\u5bfc\u81f4\u53d1\u52a8\u673a\u91cd\u65b0\u8bbe\u8ba1\uff0c\u4ece\u800c\u5bfc\u81f4\u6c7d\u8f66\u6846\u67b6\u7684\u91cd\u65b0\u8bbe\u8ba1\u3002\u5728\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u4f1a\u91cd\u65b0\u8bbe\u8ba1\u6c7d\u8f66\u7684\u4e00\u4e2a\u5f88\u5927\u7684\u90e8\u5206\uff0c\u53ea\u662f\u4e3a\u4e86\u9002\u5e94\u6700\u521d\u7684\u4e00\u4e2a\u5c0f\u95ee\u9898!\u5f88\u660e\u663e\u9009\u9879a)\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u7136\u800c\uff0c\u8bb8\u591a\u65b0\u7a0b\u5e8f\u5458\u7f16\u5199\u7c7b\u4f3c\u9009\u9879b)\u7684\u4ee3\u7801! \u5bf9\u4e8e a) \u7684\u60c5\u51b5\uff0c\u6c7d\u8f66\u516c\u53f8\u4e00\u8fb9\u7ec4\u88c5\u4e00\u8fb9\u6d4b\u8bd5\u3002\u5982\u679c\u4efb\u4f55\u96f6\u4ef6\u662f\u574f\u7684\uff0c\u4ed6\u4eec\u4f1a\u7acb\u5373\u77e5\u9053\uff0c\u5e76\u80fd\u4fee\u590d/\u66f4\u6362\u5b83\u3002\u4efb\u4f55\u4e1c\u897f\u90fd\u4e0d\u4f1a\u88ab\u96c6\u6210\u5230\u6c7d\u8f66\u4e2d\uff0c\u76f4\u5230\u5b83\u88ab\u8bc1\u660e\u53ef\u4ee5\u5de5\u4f5c\uff0c\u7136\u540e\u4e00\u65e6\u5b83\u88ab\u96c6\u6210\u5230\u6c7d\u8f66\u4e2d\uff0c\u5c31\u4f1a\u518d\u6b21\u8fdb\u884c\u6d4b\u8bd5\u3002\u8fd9\u6837\uff0c\u4efb\u4f55\u610f\u5916\u7684\u95ee\u9898\u90fd\u53ef\u4ee5\u5c3d\u65e9\u53d1\u73b0\uff0c\u800c\u5b83\u4eec\u4ecd\u7136\u662f\u53ef\u4ee5\u8f7b\u677e\u89e3\u51b3\u7684\u5c0f\u95ee\u9898\u3002 \u5f00\u59cb\u7ec4\u88c5\u6574\u8f86\u8f66\u7684\u65f6\u5019\uff0c\u5236\u9020\u5546\u5e94\u8be5\u5bf9\u6c7d\u8f66\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u6709\u5408\u7406\u7684\u4fe1\u5fc3\u2014\u2014\u6bd5\u7adf\uff0c\u6240\u6709\u7684\u90e8\u4ef6\u90fd\u5df2\u7ecf\u5355\u72ec\u6d4b\u8bd5\u8fc7\u4e86\uff0c\u5e76\u4e14\u5728\u521d\u59cb\u96c6\u6210\u65f6\u4e5f\u8fdb\u884c\u4e86\u6d4b\u8bd5\u3002\u5728\u8fd9\u4e00\u70b9\u4e0a\u4ecd\u7136\u6709\u53ef\u80fd\u53d1\u73b0\u610f\u5916\u7684\u95ee\u9898\uff0c\u4f46\u662f\u901a\u8fc7\u6240\u6709\u5148\u524d\u7684\u6d4b\u8bd5\u5c06\u98ce\u9669\u964d\u5230\u6700\u4f4e\u3002 \u4e0a\u9762\u7684\u7c7b\u6bd4\u4e5f\u9002\u7528\u4e8e\u7a0b\u5e8f\uff0c\u5c3d\u7ba1\u7531\u4e8e\u67d0\u79cd\u539f\u56e0\uff0c\u65b0\u7a0b\u5e8f\u5458\u7ecf\u5e38\u6ca1\u6709\u610f\u8bc6\u5230\u8fd9\u4e00\u70b9\u3002\u6700\u597d\u662f\u7f16\u5199\u5c0f\u51fd\u6570(\u6216\u7c7b)\uff0c\u7136\u540e\u7acb\u5373\u7f16\u8bd1\u548c\u6d4b\u8bd5\u5b83\u4eec\u3002\u8fd9\u6837\uff0c\u5982\u679c\u4f60\u72af\u4e86\u9519\u8bef\u5c31\u4f1a\u77e5\u9053\u5b83\u4e00\u5b9a\u662f\u5728\u4e0a\u6b21\u7f16\u8bd1/\u6d4b\u8bd5\u540e\u66f4\u6539\u7684\u5c11\u91cf\u4ee3\u7801\u4e2d\u3002\u8fd9\u610f\u5473\u7740\u8981\u67e5\u770b\u7684\u5730\u65b9\u66f4\u5c11\uff0c\u82b1\u5728\u8c03\u8bd5\u4e0a\u7684\u65f6\u95f4\u4e5f\u66f4\u5c11\u3002 \u5355\u72ec\u6d4b\u8bd5\u4e00\u5c0f\u90e8\u5206\u4ee3\u7801\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u201c\u5355\u5143\u201d\u662f\u6b63\u786e\u7684\uff0c\u8fd9\u79f0\u4e3a\u5355\u5143\u6d4b\u8bd5\u3002\u6bcf\u4e2a\u5355\u5143\u6d4b\u8bd5\u90fd\u88ab\u8bbe\u8ba1\u7528\u6765\u786e\u4fdd\u5355\u5143\u7684\u7279\u5b9a\u884c\u4e3a\u662f\u6b63\u786e\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u7528\u5b9a\u4e49\u826f\u597d\u7684\u5c0f\u5355\u5143(\u51fd\u6570\u6216\u7c7b)\u7f16\u5199\u7a0b\u5e8f\uff0c\u9891\u7e41\u7f16\u8bd1\uff0c\u5e76\u5728\u8fd0\u884c\u65f6\u6d4b\u8bd5\u4ee3\u7801\u3002 \u5982\u679c\u7a0b\u5e8f\u5f88\u77ed\u5e76\u4e14\u63a5\u53d7\u7528\u6237\u8f93\u5165\uff0c\u90a3\u4e48\u5c1d\u8bd5\u5404\u79cd\u7528\u6237\u8f93\u5165\u53ef\u80fd\u5c31\u8db3\u591f\u4e86\u3002\u4f46\u662f\u968f\u7740\u7a0b\u5e8f\u53d8\u5f97\u8d8a\u6765\u8d8a\u957f\uff0c\u8fd9\u5c31\u4e0d\u591f\u4e86\uff0c\u5728\u5c06\u5355\u4e2a\u51fd\u6570\u6216\u7c7b\u96c6\u6210\u5230\u7a0b\u5e8f\u7684\u5176\u4f59\u90e8\u5206\u4e4b\u524d\u5c31\u5bf9\u5b83\u4eec\u8fdb\u884c\u6d4b\u8bd5\u6548\u679c\u4f1a\u66f4\u597d\u3002 \u90a3\u4e48\u6211\u4eec\u5982\u4f55\u4ee5\u5355\u4f4d\u6765\u6d4b\u8bd5\u4ee3\u7801\u5462?","title":"\u5316\u6574\u4e3a\u96f6\u8fdb\u884c\u6d4b\u8bd5"},{"location":"7-12-Introduction-to-testing-your-code/#_3","text":"\u6d4b\u8bd5\u4ee3\u7801\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u5728\u7f16\u5199\u7a0b\u5e8f\u65f6\u8fdb\u884c\u975e\u6b63\u5f0f\u6d4b\u8bd5\u3002\u5728\u7f16\u5199\u4e86\u4e00\u4e2a\u4ee3\u7801\u5355\u5143(\u4e00\u4e2a\u51fd\u6570\u3001\u4e00\u4e2a\u7c7b\u6216\u5176\u4ed6\u4e00\u4e9b\u79bb\u6563\u7684\u201c\u5305\u201d\u4ee3\u7801)\u4e4b\u540e\uff0c\u53ef\u4ee5\u7f16\u5199\u4e00\u4e9b\u4ee3\u7801\u6765\u6d4b\u8bd5\u521a\u521a\u6dfb\u52a0\u7684\u5355\u5143\uff0c\u7136\u540e\u5728\u6d4b\u8bd5\u901a\u8fc7\u540e\u5220\u9664\u6d4b\u8bd5\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e0b\u9762\u7684isLowerVowel()\u51fd\u6570\uff0c\u5219\u53ef\u4ee5\u7f16\u5199\u4ee5\u4e0b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include // We want to test the following function bool isLowerVowel ( char c ) { switch ( c ) { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : return true ; default : return false ; } } int main () { // So here's our temporary tests to validate it works std :: cout << isLowerVowel ( 'a' ); // temporary test code, should produce 1 std :: cout << isLowerVowel ( 'q' ); // temporary test code, should produce 0 return 0 ; } \u5982\u679c\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u201c 1 \u201d\u548c\u201c 0 \u201d\uff0c\u90a3\u4e48\u7a0b\u5e8f\u5c31\u662f\u6b63\u786e\u7684\u3002\u4f60\u7684\u51fd\u6570\u9002\u7528\u4e8e\u4e00\u4e9b\u57fa\u672c\u60c5\u51b5\uff0c\u968f\u540e\u4f60\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u4ee3\u7801\u5408\u7406\u5730\u63a8\u65ad\u5b83\u4e5f\u9002\u7528\u4e8e\u90a3\u4e9b\u5c1a\u672a\u6d4b\u8bd5\u7684\u60c5\u51b5( e \uff0c i \uff0c o \u548c u )\u3002\u56e0\u6b64\uff0c\u4f60\u53ef\u4ee5\u5220\u9664\u4e34\u65f6\u6d4b\u8bd5\u4ee3\u7801\uff0c\u5e76\u7ee7\u7eed\u7f16\u7a0b\u3002","title":"\u975e\u6b63\u5f0f\u6d4b\u8bd5"},{"location":"7-12-Introduction-to-testing-your-code/#_4","text":"\u5c3d\u7ba1\u7f16\u5199\u4e34\u65f6\u7684\u6d4b\u8bd5\u4ee3\u7801\u53ef\u4ee5\u7b80\u5355\u5feb\u901f\u5730\u6d4b\u8bd5\u7a0b\u5e8f\uff0c\u4f46\u5982\u679c\u7a0d\u540e\u8fd8\u9700\u8981\u6d4b\u8bd5\u7a0b\u5e8f\u7684\u8bdd\uff0c\u5c31\u4e0d\u80fd\u4f9d\u8d56\u8fd9\u4e9b\u4e34\u65f6\u7684\u6d4b\u8bd5\u4ee3\u7801\u4e86\u3002\u5f53\u6211\u4eec\u4fee\u6539\u4e86\u4e00\u4e2a\u51fd\u6570\u5e76\u6dfb\u52a0\u65b0\u7684\u529f\u80fd\u540e\uff0c\u5f80\u5f80\u5e0c\u671b\u786e\u4fdd\u6ca1\u6709\u7834\u574f\u7a0b\u5e8f\u7684\u5176\u4ed6\u90e8\u5206\u3002\u51fa\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u5c06\u6d4b\u8bd5\u4ee3\u7801\u4fdd\u5b58\u8d77\u6765\u4ee5\u4fbf\u5c06\u6765\u53ef\u4ee5\u518d\u6b21\u8fd0\u884c\u662f\u66f4\u52a0\u6709\u610f\u4e49\u7684\u505a\u6cd5\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5c06\u6d4b\u8bd5\u79fb\u52a8\u5230 testVowel() \u51fd\u6570\u4e2d\uff0c\u800c\u4e0d\u662f\u5220\u9664\u4f60\u7684\u4e34\u65f6\u6d4b\u8bd5\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include bool isLowerVowel ( char c ) { switch ( c ) { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : return true ; default : return false ; } } // Not called from anywhere right now // But here if you want to retest things later void testVowel () { std :: cout << isLowerVowel ( 'a' ); // temporary test code, should produce 1 std :: cout << isLowerVowel ( 'q' ); // temporary test code, should produce 0 } int main () { return 0 ; } \u5728\u521b\u5efa\u66f4\u591a\u6d4b\u8bd5\u65f6\uff0c\u53ef\u4ee5\u5c06\u5b83\u4eec\u6dfb\u52a0\u5230 testVowel() \u51fd\u6570\u4e2d\u3002","title":"\u4fdd\u5b58\u6d4b\u8bd5"},{"location":"7-12-Introduction-to-testing-your-code/#_5","text":"\u4e0a\u8ff0\u6d4b\u8bd5\u51fd\u6570\u7684\u4e00\u4e2a\u95ee\u9898\u662f\uff0c\u5b83\u4f9d\u8d56\u4e8e\u4f60\u5728\u8fd0\u884c\u65f6\u624b\u52a8\u9a8c\u8bc1\u7ed3\u679c\u3002\u8fd9\u8981\u6c42\u4f60\u8981\u8bb0\u4f4f\u671f\u671b\u7684\u7ed3\u679c\u662f\u4ec0\u4e48(\u5047\u8bbe\u60a8\u6ca1\u6709\u8bb0\u5f55\u5b83)\uff0c\u5e76\u624b\u52a8\u5c06\u5b9e\u9645\u7ed3\u679c\u4e0e\u9884\u671f\u7ed3\u679c\u8fdb\u884c\u6bd4\u8f83\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u4e00\u4e2a\u5305\u542b\u6d4b\u8bd5\u548c\u9884\u671f\u7b54\u6848\u7684\u6d4b\u8bd5\u51fd\u6570\u6765\u505a\u5f97\u66f4\u597d\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u4e0d\u5fc5\u5bf9\u5b83\u4eec\u8fdb\u884c\u4eba\u5de5\u6bd4\u8f83\u4e86\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include bool isLowerVowel ( char c ) { switch ( c ) { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : return true ; default : return false ; } } // returns the number of the test that failed, or 0 if all tests passed int testVowel () { if ( isLowerVowel ( 'a' ) != true ) return 1 ; if ( isLowerVowel ( 'q' ) != false ) return 2 ; return 0 ; } int main () { return 0 ; } \u8fd9\u6837\u4e00\u6765\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728\u4efb\u4f55\u65f6\u5019\u8c03\u7528 testVowel() \u51fd\u6570\u6765\u6d4b\u8bd5\u7a0b\u5e8f\uff0c\u786e\u4fdd\u5b83\u59cb\u7ec8\u80fd\u591f\u6b63\u786e\u8fd0\u884c\u3002\u6d4b\u8bd5\u4ee3\u7801\u53ef\u4ee5\u81ea\u52a8\u5e2e\u4f60\u6d4b\u8bd5\u6240\u6709\u7528\u4f8b\uff0c\u8fd4\u56de\u201call good\u201d\uff08\u8fd4\u56de\u503c0\uff09\uff0c\u6216\u8005\u8fd4\u56de\u5931\u8d25\u7528\u4f8b\u7684\u5e8f\u53f7\u3002\u8fd9\u6837\u7684\u6d4b\u8bd5\u5728\u6211\u4eec\u4fee\u6539\u8001\u4ee3\u7801\u65f6\u5c24\u4e3a\u91cd\u8981\uff0c\u56e0\u4e3a\u8c01\u4e5f\u4e0d\u5e0c\u671b\u65e0\u610f\u95f4\u7834\u574f\u7a0b\u5e8f\u539f\u672c\u6b63\u5e38\u7684\u529f\u80fd\uff01","title":"\u81ea\u52a8\u5316\u6d4b\u8bd5"},{"location":"7-12-Introduction-to-testing-your-code/#_6","text":"\u56e0\u4e3a\u7f16\u5199\u51fd\u6570\u6765\u6267\u884c\u5176\u4ed6\u51fd\u6570\u662f\u975e\u5e38\u5e38\u89c1\u548c\u6709\u7528\u7684\u9700\u6c42\uff0c\u6240\u4ee5\u6709\u4e00\u4e9b\u5b8c\u6574\u7684\u6846\u67b6(\u79f0\u4e3a\u5355\u5143\u6d4b\u8bd5\u6846\u67b6)\u65e8\u5728\u5e2e\u52a9\u7b80\u5316\u7f16\u5199\u3001\u7ef4\u62a4\u548c\u6267\u884c\u5355\u5143\u6d4b\u8bd5\u7684\u8fc7\u7a0b\u3002\u56e0\u4e3a\u8fd9\u4e9b\u6d89\u53ca\u5230\u7b2c\u4e09\u65b9\u8f6f\u4ef6\uff0c\u6240\u4ee5\u6211\u4eec\u5728\u8fd9\u91cc\u4e0d\u505a\u4ecb\u7ecd\uff0c\u4f46\u662f\u4f60\u5e94\u8be5\u77e5\u9053\u5b83\u4eec\u7684\u5b58\u5728\u3002","title":"\u5355\u5143\u6d4b\u8bd5\u6846\u67b6"},{"location":"7-12-Introduction-to-testing-your-code/#_7","text":"\u4e00\u65e6\u7a0b\u5e8f\u7684\u6bcf\u4e2a\u5355\u5143\u90fd\u5355\u72ec\u6d4b\u8bd5\u8fc7\uff0c\u5b83\u4eec\u5c31\u53ef\u4ee5\u88ab\u96c6\u6210\u5230\u4f60\u7684\u7a0b\u5e8f\u4e2d\uff0c\u5e76\u91cd\u65b0\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u88ab\u6b63\u786e\u5730\u96c6\u6210\u4e86\u3002\u8fd9\u5c31\u662f\u6240\u8c13\u7684\u96c6\u6210\u6d4b\u8bd5\u3002\u96c6\u6210\u6d4b\u8bd5\u5f80\u5f80\u66f4\u52a0\u590d\u6742\u2014\u2014\u76ee\u524d\uff0c\u8fd0\u884c\u51e0\u6b21\u7a0b\u5e8f\u5e76\u62bd\u68c0\u96c6\u6210\u5355\u5143\u7684\u884c\u4e3a\u5c31\u8db3\u591f\u4e86\u3002","title":"\u96c6\u6210\u6d4b\u8bd5"},{"location":"7-13-Code-coverage/","tags":["code coverage"],"text":"Key Takeaway \u5728 7.12 - \u4ee3\u7801\u6d4b\u8bd5 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u7f16\u5199\u548c\u4fdd\u5b58\u6d4b\u8bd5\u3002\u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u54ea\u4e9b\u6d4b\u8bd5\u5bf9\u4e8e\u4fdd\u8bc1\u7a0b\u5e8f\u8d28\u91cf\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002 \u4ee3\u7801\u8986\u76d6\u7387 \u672f\u8bed\u4ee3\u7801\u8986\u76d6\u7387\u7528\u4e8e\u63cf\u8ff0\u6d4b\u8bd5\u6267\u884c\u65f6\u53ef\u4ee5\u8fd0\u884c\u7684\u7a0b\u5e8f\u6e90\u4ee3\u7801\u6570\u91cf\u3002\u4ee3\u7801\u8986\u76d6\u7387\u6709\u8bb8\u591a\u4e0d\u540c\u7684\u5ea6\u91cf\u6807\u51c6\u3002\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u90e8\u5206\u4e2d\u4ecb\u7ecd\u4e00\u4e9b\u66f4\u6709\u7528\u548c\u66f4\u53d7\u6b22\u8fce\u7684\u65b9\u6cd5\u3002 \u8bed\u53e5\u8986\u76d6\u7387 \u672f\u8bed\u8bed\u53e5\u8986\u76d6\u7387\u6307\u7684\u662f\u6d4b\u8bd5\u4f8b\u7a0b\u6267\u884c\u8fc7\u7684\u4ee3\u7801\u4e2d\u8bed\u53e5\u7684\u767e\u5206\u6bd4\u3002 \u8003\u8651\u4ee5\u4e0b\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 int foo ( int x , int y ) { int z { y }; if ( x > y ) { z = x ; } return z ; } \u8c03\u7528 foo(1, 0) \u53ef\u4ee5\u5b9e\u73b0\u8be5\u51fd\u6570\u8bed\u53e5\u7684\u5b8c\u5168\u8986\u76d6\uff0c\u5373\u51fd\u6570\u4e2d\u7684\u6bcf\u6761\u8bed\u53e5\u90fd\u4f1a\u6267\u884c\u3002 \u5bf9\u4e8e isLowerVowel() \u51fd\u6570\u6765\u8bf4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 bool isLowerVowel ( char c ) { switch ( c ) // \u8bed\u53e5 1 { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : return true ; // \u8bed\u53e5 2 default : return false ; // \u8bed\u53e5 3 } } \u8fd9\u4e2a\u51fd\u6570\u9700\u8981\u4e24\u6b21\u8c03\u7528\u624d\u80fd\u5b9e\u73b0\u8bed\u53e5\u7684\u5b8c\u5168 \u8986\u76d6\uff0c\u56e0\u4e3a\u8bed\u53e52\u548c3\u4e0d\u53ef\u80fd\u5728\u4e00\u6b21\u51fd\u6570\u8c03\u7528\u4e2d\u540c\u65f6\u6267\u884c\u3002 100%\u7684\u8bed\u53e5\u8986\u76d6\u7387\u662f\u5f88\u597d\u7684\u6307\u6807\uff0c\u4f46\u8fd9\u5176\u5b9e\u5e76\u4e0d\u8db3\u4ee5\u786e\u4fdd\u6b63\u786e\u6027\u3002 \u5206\u652f\u8986\u76d6\u7387 \u5206\u652f\u8986\u76d6\u7387\u662f\u6307\u5df2\u7ecf\u6267\u884c\u7684\u5206\u652f\u7684\u767e\u5206\u6bd4\uff0c\u6bcf\u4e2a\u53ef\u80fd\u7684\u5206\u652f\u5206\u522b\u8ba1\u7b97\u3002\u4e00\u4e2a' if\u8bed\u53e5'\u6709\u4e24\u4e2a\u5206\u652f\u2014\u2014\u4e00\u4e2a\u5206\u652f\u5728\u6761\u4ef6\u4e3a'\u771f'\u65f6\u6267\u884c\uff0c\u53e6\u4e00\u4e2a\u5206\u652f\u5728\u6761\u4ef6\u4e3a'\u5047'\u65f6\u6267\u884c(\u5373\u4f7f\u6ca1\u6709\u76f8\u5e94\u7684' else\u8bed\u53e5'\u8981\u6267\u884c)\u3002\u4e00\u4e2aswitch\u8bed\u53e5\u53ef\u4ee5\u6709\u5f88\u591a\u5206\u652f\u3002 1 2 3 4 5 6 7 8 9 int foo ( int x , int y ) { int z { y }; if ( x > y ) { z = x ; } return z ; } \u5728\u4e4b\u524d\u7684\u4f8b\u5b50\u4e2d\uff0c\u8c03\u7528 foo(1, 0) \u53ef\u4ee5\u5b9e\u73b0100%\u7684\u8bed\u53e5\u8986\u76d6\u7387\uff0c\u5e76\u4e14\u6d4b\u8bd5 x > y \u7684\u60c5\u51b5\uff0c\u4f46\u662f\u5206\u652f\u8986\u76d6\u7387\u5374\u53ea\u670950%\u3002\u6211\u4eec\u9700\u8981\u518d\u8c03\u7528\u4e00\u6b21 foo(0, 1) \uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u6d4b\u5230 if \u8bed\u53e5\u4e0d\u6267\u884c\u7684\u60c5\u51b5\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 bool isLowerVowel ( char c ) { switch ( c ) { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : return true ; default : return false ; } } \u5728 isLowerVowel() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4e5f\u9700\u8981\u8c03\u7528\u4e24\u6b21\u624d\u80fd\u5f97\u5230100%\u7684\u5206\u652f\u8986\u76d6\u7387\uff1a\u7b2c\u4e00\u6b21\u8c03\u7528 (\u4f8b\u5982 isLowerVowel('a') ) \u6d4b\u8bd5\u524d\u9762\u7684\u51e0\u4e2acase\u5206\u652f\uff0c\u7b2c\u4e8c\u6b21\u8c03\u7528(\u4f8b\u5982 isLowerVowel('q') ) \u6765\u6d4b\u8bd5 default \u5206\u652f\u3002\u80fd\u591f\u5728\u4e00\u6b21\u6d4b\u8bd5\u4e2d\u6267\u884c\u5230\u7684\u5206\u652f\uff0c\u4e0d\u9700\u8981\u5206\u522b\u6d4b\u8bd5\u3002 \u8003\u8651\u4e0b\u9762\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 void compare ( int x , int y ) { if ( x > y ) std :: cout << x << \" is greater than \" << y << '\\n' ; // case 1 else if ( x < y ) std :: cout << x << \" is less than \" << y << '\\n' ; // case 2 else std :: cout << x << \" is equal to \" << y << '\\n' ; // case 3 } \u8be5\u51fd\u6570\u9700\u8981\u4e09\u6b21\u8c03\u7528\u624d\u80fd\u5b9e\u73b0100%\u5206\u652f\u8986\u76d6\uff1a compare(1, 0) \u6d4b\u8bd5\u7b2c\u4e00\u4e2a if \u8bed\u53e5\u4e3a\u771f\u7684\u5206\u652f\uff0c compare(0, 1) \u6d4b\u8bd5\u7b2c\u4e00\u4e2a if \u8bed\u53e5\u4e3a\u5047\u4e14\u7b2c\u4e8c\u4e2a if \u8bed\u53e5\u4e3a\u771f\u7684\u5206\u652f\u3002 compare(0, 0) \u6d4b\u8bd5\u4e24\u4e2a if \u8bed\u53e5\u90fd\u4e3a\u5047\uff0c\u6267\u884c else \u7684\u5206\u652f\u3002\u6267\u884c\u5b8c\u8fd9\u4e09\u6b21\u6d4b\u8bd5\u540e\uff0c\u6211\u4eec\u5c31\u6709\u4fe1\u5fc3\u8bf4\u8fd9\u4e2a\u51fd\u6570\u88ab\u53ef\u9760\u5730\u6d4b\u8bd5\u4e86\uff08\u4e09\u6b21\u6d4b\u8bd5\u603b\u597d\u8fc71800\u4ebf\u4ebf\u4e2a\u8f93\u5165\u7ec4\u5408\u5427\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 100%\u5206\u652f\u8986\u76d6\u7387\u662f\u6211\u4eec\u7684\u6d4b\u8bd5\u76ee\u6807\uff01 \u5faa\u73af\u8986\u76d6\u7387 \u5faa\u73af\u8986\u76d6\u7387(\u975e\u6b63\u5f0f\u5730\u79f0\u4e3a0,1,2\u6d4b\u8bd5)\u6307\u7684\u662f\uff0c\u5982\u679c\u4f60\u7684\u4ee3\u7801\u4e2d\u6709\u4e00\u4e2a\u5faa\u73af\uff0c\u5219\u5e94\u8be5\u786e\u4fdd\u5b83\u5728\u8fed\u4ee30\u6b21\u30011\u6b21\u548c2\u6b21\u65f6\u6b63\u5e38\u5de5\u4f5c\u3002\u5982\u679c\u5b83\u5bf92\u6b21\u8fed\u4ee3\u7684\u60c5\u51b5\u6b63\u786e\u5de5\u4f5c\uff0c\u90a3\u4e48\u5b83\u5e94\u8be5\u5bf9\u6240\u6709\u5927\u4e8e2\u6b21\u7684\u8fed\u4ee3\u90fd\u6b63\u786e\u5de5\u4f5c\u3002\u56e0\u6b64\uff0c\u8fd9\u4e09\u4e2a\u6d4b\u8bd5\u6db5\u76d6\u4e86\u6240\u6709\u7684\u53ef\u80fd\u6027(\u56e0\u4e3a\u5faa\u73af\u4e0d\u80fd\u6267\u884c\u8d1f\u6570\u6b21)\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 #include void spam ( int timesToPrint ) { for ( int count { 0 }; count < timesToPrint ; ++ count ) std :: cout << \"Spam! \" ; } \u4e3a\u4e86\u66f4\u597d\u5730\u6d4b\u8bd5\u8fd9\u4e2a\u51fd\u6570\u4e2d\u7684\u5faa\u73af\uff0c\u6211\u4eec\u9700\u8981\u8c03\u7528\u4e09\u6b21\u8be5\u51fd\u6570\uff1a spam(0) \u6d4b\u8bd50\u6b21\u8fed\u4ee3\u7684\u60c5\u51b5\uff0c spam(1) \u6d4b\u8bd51\u6b21\u8fed\u4ee3\u7684\u60c5\u51b5\uff0c\u7136\u540e\u8c03\u7528 spam(2) \u6d4b\u8bd5 2 \u6b21\u8fed\u4ee3\u7684\u60c5\u51b5\u3002\u5982\u679c spam(2) \u53ef\u4ee5\u5de5\u4f5c\uff0c\u5219\u5bf9\u4e8e\u4efb\u610f n>2 \u7684\u60c5\u51b5 spam(n) \u90fd\u5e94\u8be5\u53ef\u4ee5\u5de5\u4f5c\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528 \u201c0,1,2\u6d4b\u8bd5\u201d \u6765\u786e\u4fdd\u4f60\u7684\u5faa\u73af\u5728\u4e0d\u540c\u6b21\u6570\u7684\u8fed\u4ee3\u4e0b\u90fd\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002 \u6d4b\u8bd5\u4e0d\u540c\u7c7b\u522b\u7684\u8f93\u5165 \u5728\u7f16\u5199\u63a5\u53d7\u53c2\u6570\u7684\u51fd\u6570\u6216\u63a5\u53d7\u7528\u6237\u8f93\u5165\u65f6\uff0c\u8981\u8003\u8651\u4e0d\u540c\u7c7b\u522b\u7684\u8f93\u5165\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f7f\u7528\u672f\u8bed\u201c\u7c7b\u522b\u201d\u6765\u8868\u793a\u5177\u6709\u76f8\u4f3c\u7279\u5f81\u7684\u4e00\u7ec4\u8f93\u5165\u3002 \u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u4ea7\u751f\u6574\u6570\u5e73\u65b9\u6839\u7684\u51fd\u6570\uff0c\u7528\u4ec0\u4e48\u503c\u6765\u6d4b\u8bd5\u5b83\u662f\u6709\u610f\u4e49\u7684\uff1f\u4f60\u53ef\u80fd\u4f1a\u4ece\u4e00\u4e9b\u6b63\u5e38\u7684\u503c\u5f00\u59cb\uff0c\u6bd4\u5982\u201c4\u201d\u3002\u4f46\u662f\u7528\u201c0\u201d\u548c\u8d1f\u6570\u6765\u6d4b\u8bd5\u4e5f\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\u3002 \u4ee5\u4e0b\u662f\u5206\u7c7b\u6d4b\u8bd5\u7684\u4e00\u4e9b\u57fa\u672c\u51c6\u5219\uff1a \u5bf9\u4e8e\u6574\u6570\uff0c\u9700\u8981\u8003\u8651\u51fd\u6570\u5982\u4f55\u5904\u7406\u8d1f\u503c\u3001\u96f6\u548c\u6b63\u503c\u3002\u5982\u679c\u53ef\u80fd\u7684\u8bdd\uff0c\u8fd8\u5e94\u8be5\u68c0\u67e5\u662f\u5426\u6ea2\u51fa\u3002 \u5bf9\u4e8e\u6d6e\u70b9\u6570\uff0c\u786e\u4fdd\u8003\u8651\u4e86\u51fd\u6570\u5982\u4f55\u5904\u7406\u6709\u7cbe\u5ea6\u95ee\u9898\u7684\u503c(\u7565\u5927\u4e8e\u6216\u7565\u5c0f\u4e8e\u9884\u671f\u503c)\u3002\u9002\u5408\u6d4b\u8bd5\u7684 double \u7c7b\u578b\u503c\u662f 0.1 \u548c -0.1 (\u7528\u4e8e\u6d4b\u8bd5\u7565\u5927\u4e8e\u9884\u671f\u7684\u6570\u5b57)\u4ee5\u53ca 0.6 \u548c -0.6 (\u7528\u4e8e\u6d4b\u8bd5\u7565\u5c0f\u4e8e\u9884\u671f\u7684\u6570\u5b57)\u3002 \u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0c\u786e\u4fdd\u4f60\u8003\u8651\u4e86\u51fd\u6570\u5982\u4f55\u5904\u7406\u7a7a\u5b57\u7b26\u4e32(\u53ea\u662f\u4e00\u4e2a\u7a7a\u7ed3\u675f\u7b26)\u3001\u6b63\u5e38\u6709\u6548\u7684\u5b57\u7b26\u4e32\u3001\u6709\u7a7a\u683c\u7684\u5b57\u7b26\u4e32\u4ee5\u53ca\u5168\u662f\u7a7a\u683c\u7684\u5b57\u7b26\u4e32\u3002 \u5982\u679c\u51fd\u6570\u4f1a\u5904\u7406\u6307\u9488\uff0c\u4e0d\u8981\u5fd8\u8bb0\u6d4b\u8bd5 nullptr (\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u8fd9\u662f\u4ec0\u4e48\u4e5f\u4e0d\u8981\u7d27\uff0c\u6bd5\u7adf\u6211\u4eec\u8fd8\u6ca1\u4ecb\u7ecd\u8fc7\u5b83)\u3002 \u6700\u4f73\u5b9e\u8df5 \u6d4b\u8bd5\u4e0d\u540c\u7c7b\u522b\u7684\u8f93\u5165\u503c\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u5355\u5143\u80fd\u591f\u6b63\u786e\u5730\u5904\u7406\u5b83\u4eec\u3002","title":"7.13 - \u4ee3\u7801\u8986\u76d6\u7387"},{"location":"7-13-Code-coverage/#_1","text":"\u672f\u8bed\u4ee3\u7801\u8986\u76d6\u7387\u7528\u4e8e\u63cf\u8ff0\u6d4b\u8bd5\u6267\u884c\u65f6\u53ef\u4ee5\u8fd0\u884c\u7684\u7a0b\u5e8f\u6e90\u4ee3\u7801\u6570\u91cf\u3002\u4ee3\u7801\u8986\u76d6\u7387\u6709\u8bb8\u591a\u4e0d\u540c\u7684\u5ea6\u91cf\u6807\u51c6\u3002\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u90e8\u5206\u4e2d\u4ecb\u7ecd\u4e00\u4e9b\u66f4\u6709\u7528\u548c\u66f4\u53d7\u6b22\u8fce\u7684\u65b9\u6cd5\u3002","title":"\u4ee3\u7801\u8986\u76d6\u7387"},{"location":"7-13-Code-coverage/#_2","text":"\u672f\u8bed\u8bed\u53e5\u8986\u76d6\u7387\u6307\u7684\u662f\u6d4b\u8bd5\u4f8b\u7a0b\u6267\u884c\u8fc7\u7684\u4ee3\u7801\u4e2d\u8bed\u53e5\u7684\u767e\u5206\u6bd4\u3002 \u8003\u8651\u4ee5\u4e0b\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 int foo ( int x , int y ) { int z { y }; if ( x > y ) { z = x ; } return z ; } \u8c03\u7528 foo(1, 0) \u53ef\u4ee5\u5b9e\u73b0\u8be5\u51fd\u6570\u8bed\u53e5\u7684\u5b8c\u5168\u8986\u76d6\uff0c\u5373\u51fd\u6570\u4e2d\u7684\u6bcf\u6761\u8bed\u53e5\u90fd\u4f1a\u6267\u884c\u3002 \u5bf9\u4e8e isLowerVowel() \u51fd\u6570\u6765\u8bf4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 bool isLowerVowel ( char c ) { switch ( c ) // \u8bed\u53e5 1 { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : return true ; // \u8bed\u53e5 2 default : return false ; // \u8bed\u53e5 3 } } \u8fd9\u4e2a\u51fd\u6570\u9700\u8981\u4e24\u6b21\u8c03\u7528\u624d\u80fd\u5b9e\u73b0\u8bed\u53e5\u7684\u5b8c\u5168 \u8986\u76d6\uff0c\u56e0\u4e3a\u8bed\u53e52\u548c3\u4e0d\u53ef\u80fd\u5728\u4e00\u6b21\u51fd\u6570\u8c03\u7528\u4e2d\u540c\u65f6\u6267\u884c\u3002 100%\u7684\u8bed\u53e5\u8986\u76d6\u7387\u662f\u5f88\u597d\u7684\u6307\u6807\uff0c\u4f46\u8fd9\u5176\u5b9e\u5e76\u4e0d\u8db3\u4ee5\u786e\u4fdd\u6b63\u786e\u6027\u3002","title":"\u8bed\u53e5\u8986\u76d6\u7387"},{"location":"7-13-Code-coverage/#_3","text":"\u5206\u652f\u8986\u76d6\u7387\u662f\u6307\u5df2\u7ecf\u6267\u884c\u7684\u5206\u652f\u7684\u767e\u5206\u6bd4\uff0c\u6bcf\u4e2a\u53ef\u80fd\u7684\u5206\u652f\u5206\u522b\u8ba1\u7b97\u3002\u4e00\u4e2a' if\u8bed\u53e5'\u6709\u4e24\u4e2a\u5206\u652f\u2014\u2014\u4e00\u4e2a\u5206\u652f\u5728\u6761\u4ef6\u4e3a'\u771f'\u65f6\u6267\u884c\uff0c\u53e6\u4e00\u4e2a\u5206\u652f\u5728\u6761\u4ef6\u4e3a'\u5047'\u65f6\u6267\u884c(\u5373\u4f7f\u6ca1\u6709\u76f8\u5e94\u7684' else\u8bed\u53e5'\u8981\u6267\u884c)\u3002\u4e00\u4e2aswitch\u8bed\u53e5\u53ef\u4ee5\u6709\u5f88\u591a\u5206\u652f\u3002 1 2 3 4 5 6 7 8 9 int foo ( int x , int y ) { int z { y }; if ( x > y ) { z = x ; } return z ; } \u5728\u4e4b\u524d\u7684\u4f8b\u5b50\u4e2d\uff0c\u8c03\u7528 foo(1, 0) \u53ef\u4ee5\u5b9e\u73b0100%\u7684\u8bed\u53e5\u8986\u76d6\u7387\uff0c\u5e76\u4e14\u6d4b\u8bd5 x > y \u7684\u60c5\u51b5\uff0c\u4f46\u662f\u5206\u652f\u8986\u76d6\u7387\u5374\u53ea\u670950%\u3002\u6211\u4eec\u9700\u8981\u518d\u8c03\u7528\u4e00\u6b21 foo(0, 1) \uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u6d4b\u5230 if \u8bed\u53e5\u4e0d\u6267\u884c\u7684\u60c5\u51b5\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 bool isLowerVowel ( char c ) { switch ( c ) { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : return true ; default : return false ; } } \u5728 isLowerVowel() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4e5f\u9700\u8981\u8c03\u7528\u4e24\u6b21\u624d\u80fd\u5f97\u5230100%\u7684\u5206\u652f\u8986\u76d6\u7387\uff1a\u7b2c\u4e00\u6b21\u8c03\u7528 (\u4f8b\u5982 isLowerVowel('a') ) \u6d4b\u8bd5\u524d\u9762\u7684\u51e0\u4e2acase\u5206\u652f\uff0c\u7b2c\u4e8c\u6b21\u8c03\u7528(\u4f8b\u5982 isLowerVowel('q') ) \u6765\u6d4b\u8bd5 default \u5206\u652f\u3002\u80fd\u591f\u5728\u4e00\u6b21\u6d4b\u8bd5\u4e2d\u6267\u884c\u5230\u7684\u5206\u652f\uff0c\u4e0d\u9700\u8981\u5206\u522b\u6d4b\u8bd5\u3002 \u8003\u8651\u4e0b\u9762\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 void compare ( int x , int y ) { if ( x > y ) std :: cout << x << \" is greater than \" << y << '\\n' ; // case 1 else if ( x < y ) std :: cout << x << \" is less than \" << y << '\\n' ; // case 2 else std :: cout << x << \" is equal to \" << y << '\\n' ; // case 3 } \u8be5\u51fd\u6570\u9700\u8981\u4e09\u6b21\u8c03\u7528\u624d\u80fd\u5b9e\u73b0100%\u5206\u652f\u8986\u76d6\uff1a compare(1, 0) \u6d4b\u8bd5\u7b2c\u4e00\u4e2a if \u8bed\u53e5\u4e3a\u771f\u7684\u5206\u652f\uff0c compare(0, 1) \u6d4b\u8bd5\u7b2c\u4e00\u4e2a if \u8bed\u53e5\u4e3a\u5047\u4e14\u7b2c\u4e8c\u4e2a if \u8bed\u53e5\u4e3a\u771f\u7684\u5206\u652f\u3002 compare(0, 0) \u6d4b\u8bd5\u4e24\u4e2a if \u8bed\u53e5\u90fd\u4e3a\u5047\uff0c\u6267\u884c else \u7684\u5206\u652f\u3002\u6267\u884c\u5b8c\u8fd9\u4e09\u6b21\u6d4b\u8bd5\u540e\uff0c\u6211\u4eec\u5c31\u6709\u4fe1\u5fc3\u8bf4\u8fd9\u4e2a\u51fd\u6570\u88ab\u53ef\u9760\u5730\u6d4b\u8bd5\u4e86\uff08\u4e09\u6b21\u6d4b\u8bd5\u603b\u597d\u8fc71800\u4ebf\u4ebf\u4e2a\u8f93\u5165\u7ec4\u5408\u5427\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 100%\u5206\u652f\u8986\u76d6\u7387\u662f\u6211\u4eec\u7684\u6d4b\u8bd5\u76ee\u6807\uff01","title":"\u5206\u652f\u8986\u76d6\u7387"},{"location":"7-13-Code-coverage/#_4","text":"\u5faa\u73af\u8986\u76d6\u7387(\u975e\u6b63\u5f0f\u5730\u79f0\u4e3a0,1,2\u6d4b\u8bd5)\u6307\u7684\u662f\uff0c\u5982\u679c\u4f60\u7684\u4ee3\u7801\u4e2d\u6709\u4e00\u4e2a\u5faa\u73af\uff0c\u5219\u5e94\u8be5\u786e\u4fdd\u5b83\u5728\u8fed\u4ee30\u6b21\u30011\u6b21\u548c2\u6b21\u65f6\u6b63\u5e38\u5de5\u4f5c\u3002\u5982\u679c\u5b83\u5bf92\u6b21\u8fed\u4ee3\u7684\u60c5\u51b5\u6b63\u786e\u5de5\u4f5c\uff0c\u90a3\u4e48\u5b83\u5e94\u8be5\u5bf9\u6240\u6709\u5927\u4e8e2\u6b21\u7684\u8fed\u4ee3\u90fd\u6b63\u786e\u5de5\u4f5c\u3002\u56e0\u6b64\uff0c\u8fd9\u4e09\u4e2a\u6d4b\u8bd5\u6db5\u76d6\u4e86\u6240\u6709\u7684\u53ef\u80fd\u6027(\u56e0\u4e3a\u5faa\u73af\u4e0d\u80fd\u6267\u884c\u8d1f\u6570\u6b21)\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 #include void spam ( int timesToPrint ) { for ( int count { 0 }; count < timesToPrint ; ++ count ) std :: cout << \"Spam! \" ; } \u4e3a\u4e86\u66f4\u597d\u5730\u6d4b\u8bd5\u8fd9\u4e2a\u51fd\u6570\u4e2d\u7684\u5faa\u73af\uff0c\u6211\u4eec\u9700\u8981\u8c03\u7528\u4e09\u6b21\u8be5\u51fd\u6570\uff1a spam(0) \u6d4b\u8bd50\u6b21\u8fed\u4ee3\u7684\u60c5\u51b5\uff0c spam(1) \u6d4b\u8bd51\u6b21\u8fed\u4ee3\u7684\u60c5\u51b5\uff0c\u7136\u540e\u8c03\u7528 spam(2) \u6d4b\u8bd5 2 \u6b21\u8fed\u4ee3\u7684\u60c5\u51b5\u3002\u5982\u679c spam(2) \u53ef\u4ee5\u5de5\u4f5c\uff0c\u5219\u5bf9\u4e8e\u4efb\u610f n>2 \u7684\u60c5\u51b5 spam(n) \u90fd\u5e94\u8be5\u53ef\u4ee5\u5de5\u4f5c\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528 \u201c0,1,2\u6d4b\u8bd5\u201d \u6765\u786e\u4fdd\u4f60\u7684\u5faa\u73af\u5728\u4e0d\u540c\u6b21\u6570\u7684\u8fed\u4ee3\u4e0b\u90fd\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002","title":"\u5faa\u73af\u8986\u76d6\u7387"},{"location":"7-13-Code-coverage/#_5","text":"\u5728\u7f16\u5199\u63a5\u53d7\u53c2\u6570\u7684\u51fd\u6570\u6216\u63a5\u53d7\u7528\u6237\u8f93\u5165\u65f6\uff0c\u8981\u8003\u8651\u4e0d\u540c\u7c7b\u522b\u7684\u8f93\u5165\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f7f\u7528\u672f\u8bed\u201c\u7c7b\u522b\u201d\u6765\u8868\u793a\u5177\u6709\u76f8\u4f3c\u7279\u5f81\u7684\u4e00\u7ec4\u8f93\u5165\u3002 \u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u4ea7\u751f\u6574\u6570\u5e73\u65b9\u6839\u7684\u51fd\u6570\uff0c\u7528\u4ec0\u4e48\u503c\u6765\u6d4b\u8bd5\u5b83\u662f\u6709\u610f\u4e49\u7684\uff1f\u4f60\u53ef\u80fd\u4f1a\u4ece\u4e00\u4e9b\u6b63\u5e38\u7684\u503c\u5f00\u59cb\uff0c\u6bd4\u5982\u201c4\u201d\u3002\u4f46\u662f\u7528\u201c0\u201d\u548c\u8d1f\u6570\u6765\u6d4b\u8bd5\u4e5f\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\u3002 \u4ee5\u4e0b\u662f\u5206\u7c7b\u6d4b\u8bd5\u7684\u4e00\u4e9b\u57fa\u672c\u51c6\u5219\uff1a \u5bf9\u4e8e\u6574\u6570\uff0c\u9700\u8981\u8003\u8651\u51fd\u6570\u5982\u4f55\u5904\u7406\u8d1f\u503c\u3001\u96f6\u548c\u6b63\u503c\u3002\u5982\u679c\u53ef\u80fd\u7684\u8bdd\uff0c\u8fd8\u5e94\u8be5\u68c0\u67e5\u662f\u5426\u6ea2\u51fa\u3002 \u5bf9\u4e8e\u6d6e\u70b9\u6570\uff0c\u786e\u4fdd\u8003\u8651\u4e86\u51fd\u6570\u5982\u4f55\u5904\u7406\u6709\u7cbe\u5ea6\u95ee\u9898\u7684\u503c(\u7565\u5927\u4e8e\u6216\u7565\u5c0f\u4e8e\u9884\u671f\u503c)\u3002\u9002\u5408\u6d4b\u8bd5\u7684 double \u7c7b\u578b\u503c\u662f 0.1 \u548c -0.1 (\u7528\u4e8e\u6d4b\u8bd5\u7565\u5927\u4e8e\u9884\u671f\u7684\u6570\u5b57)\u4ee5\u53ca 0.6 \u548c -0.6 (\u7528\u4e8e\u6d4b\u8bd5\u7565\u5c0f\u4e8e\u9884\u671f\u7684\u6570\u5b57)\u3002 \u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0c\u786e\u4fdd\u4f60\u8003\u8651\u4e86\u51fd\u6570\u5982\u4f55\u5904\u7406\u7a7a\u5b57\u7b26\u4e32(\u53ea\u662f\u4e00\u4e2a\u7a7a\u7ed3\u675f\u7b26)\u3001\u6b63\u5e38\u6709\u6548\u7684\u5b57\u7b26\u4e32\u3001\u6709\u7a7a\u683c\u7684\u5b57\u7b26\u4e32\u4ee5\u53ca\u5168\u662f\u7a7a\u683c\u7684\u5b57\u7b26\u4e32\u3002 \u5982\u679c\u51fd\u6570\u4f1a\u5904\u7406\u6307\u9488\uff0c\u4e0d\u8981\u5fd8\u8bb0\u6d4b\u8bd5 nullptr (\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u8fd9\u662f\u4ec0\u4e48\u4e5f\u4e0d\u8981\u7d27\uff0c\u6bd5\u7adf\u6211\u4eec\u8fd8\u6ca1\u4ecb\u7ecd\u8fc7\u5b83)\u3002 \u6700\u4f73\u5b9e\u8df5 \u6d4b\u8bd5\u4e0d\u540c\u7c7b\u522b\u7684\u8f93\u5165\u503c\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u5355\u5143\u80fd\u591f\u6b63\u786e\u5730\u5904\u7406\u5b83\u4eec\u3002","title":"\u6d4b\u8bd5\u4e0d\u540c\u7c7b\u522b\u7684\u8f93\u5165"},{"location":"7-14-Common-semantic-errors-in-c/","text":"Key Takeaway \u5728 [[3-1-Syntax-and-semantic-errors|3.1 - \u8bed\u6cd5\u548c\u8bed\u4e49\u9519\u8bef] \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86 \u8bed\u6cd5\u9519\u8bef \u2014\u2014\u5f53\u4ee3\u7801\u4e0d\u7b26\u5408C++\u8bed\u4e49\u7684\u8bed\u6cd5\u89c4\u5219\u65f6\uff0c\u5c31\u4f1a\u4ea7\u751f\u8bed\u6cd5\u9519\u8bef\u3002\u7f16\u8bd1\u5668\u80fd\u591f\u8bc6\u522b\u5e76\u62a5\u544a\u8bed\u6cd5\u9519\u8bef\uff0c\u6240\u4ee5\u5b83\u4eec\u5f88\u5bb9\u6613\u88ab\u8bc6\u522b\u5230\uff0c\u901a\u5e38\u4e5f\u5f88\u5bb9\u6613\u4fee\u590d\u3002 \u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86 \u8bed\u4e49\u9519\u8bef \u2014\u2014\u5f53\u4ee3\u7801\u7684\u884c\u4e3a\u4e0d\u7b26\u5408\u4f60\u7684\u672c\u610f\u65f6\uff0c\u5c31\u88ab\u8ba4\u4e3a\u662f\u8bed\u4e49\u9519\u8bef\u3002\u7f16\u8bd1\u5668\u901a\u5e38\u60c5\u51b5\u4e0b\u5e76\u4e0d\u80fd\u8bc6\u522b\u5230\u8bed\u4e49\u9519\u8bef\uff08\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u667a\u80fd\u7684\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5bf9\u8bed\u4e49\u9519\u8bef\u4ea7\u751f\u544a\u8b66\u4fe1\u606f\uff09\u3002 \u8bed\u4e49\u9519\u8bef\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5f88\u591a \u672a\u5b9a\u4e49\u884c\u4e3a \u7c7b\u4f3c\u7684\u75c7\u72b6\uff0c\u4f8b\u5982\u5bfc\u81f4\u7a0b\u5e8f\u4ea7\u751f\u9519\u8bef\u7684\u7ed3\u679c\u3001\u4ea7\u751f\u53e4\u602a\u7684\u7a0b\u5e8f\u884c\u4e3a\u3001\u7834\u574f\u6570\u636e\u3001\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u2014\u2014\u6216\u8005\u4e0d\u4ea7\u751f\u4efb\u4f55\u5f71\u54cd\u3002 \u5728\u7f16\u5199\u7a0b\u5e8f\u65f6\uff0c\u6211\u4eec\u5f88\u96be\u5b8c\u5168\u907f\u514d\u8bed\u4e49\u9519\u8bef\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5728\u4f7f\u7528\u7a0b\u5e8f\u65f6\u6211\u4eec\u5c31\u4f1a\u9047\u5230\u8fd9\u4e9b\u8bed\u4e49\u9519\u8bef\uff1a\u4f8b\u5982\uff0c\u5728\u4f60\u7f16\u5199\u7684\u8ff7\u5bab\u6e38\u620f\u4e2d\uff0c\u6e38\u620f\u89d2\u8272\u7adf\u7136\u80fd\u591f\u7a7f\u5899\uff01\u901a\u8fc7\u5bf9\u7a0b\u5e8f\u8fdb\u884c\u6d4b\u8bd5 ( 7.12 - \u4ee3\u7801\u6d4b\u8bd5 ) \u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u53d1\u73b0\u8bed\u4e49\u9519\u8bef\u3002 \u4e0d\u8fc7\uff0c\u9664\u4e86\u6d4b\u8bd5\u4e4b\u5916\uff0c\u8fd8\u6709\u4e00\u4ef6\u4e8b\u53ef\u4ee5\u5e2e\u52a9\u4f60\u2014\u2014\u90a3\u5c31\u662f\u77e5\u9053\u54ea\u79cd\u7c7b\u578b\u7684\u8bed\u4e49\u9519\u8bef\u6700\u5e38\u89c1\uff0c\u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u5bf9\u8fd9\u4e9b\u60c5\u51b5\u591a\u52a0\u6ce8\u610f\uff01 \u672c\u8282\u8bfe\u4e2d\u6211\u4eec\u4f1a\u4ecb\u7ecd\u51e0\u79cd\u5e38\u89c1\u7684\u610f\u4e49\u9519\u8bef\uff08\u5f88\u591a\u90fd\u548c\u6761\u4ef6\u63a7\u5236\u76f8\u5173\uff09\u3002 \u6761\u4ef6\u903b\u8f91\u9519\u8bef \u6700\u5e38\u89c1\u7684\u8bed\u4e49\u9519\u8bef\u7c7b\u578b\u4e4b\u4e00\u662f \u6761\u4ef6\u903b\u8f91\u9519\u8bef \u3002\u5f53\u7a0b\u5e8f\u5458\u9519\u8bef\u5730\u7f16\u5199\u4e86\u6761\u4ef6\u8bed\u53e5\u6216\u5faa\u73af\u6761\u4ef6\u7684\u903b\u8f91\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u6761\u4ef6\u903b\u8f91\u9519\u8bef\u3002\u8fd9\u91cc\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; if ( x >= 5 ) // oops, we used operator>= instead of operator> std :: cout << x << \" is greater than 5 \\n \" ; return 0 ; } \u8fd0\u884c\u7a0b\u5e8f\uff0c\u5f88\u5bb9\u6613\u89c2\u5bdf\u5230\u903b\u8f91\u9519\u8bef\uff1a 1 2 Enter an integer: 5 5 is greater than 5 \u5f53\u7528\u6237\u8f93\u51655\u65f6\uff0c\u6761\u4ef6\u8868\u8fbe\u5f0f x >= 5 \u6c42\u503c\u4e3a true \uff0c\u6240\u4ee5\u5bf9\u5e94\u7684\u6253\u5370\u8bed\u53e5\u5c31\u6267\u884c\u4e86\u3002 \u53e6\u5916\u4e00\u4e2a\u4f8b\u5b50\u662f\u5173\u4e8e for \u5faa\u73af\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; // oops, we used operator> instead of operator< for ( int count { 1 }; count > x ; ++ count ) { std :: cout << count << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u5e94\u8be5\u6253\u5370\u4ece1\u5230\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\u4e4b\u95f4\u7684\u6240\u6709\u6570\u5b57\u3002\u4f46\u5b83\u5b9e\u9645\u4e0a\u662f\u8fd9\u6837\u505a\u7684\uff1a 1 Enter an integer: 5 \u7a0b\u5e8f\u6ca1\u6709\u6253\u5370\u4efb\u4f55\u5185\u5bb9\u3002\u8fd9\u662f\u56e0\u4e3a\u8fdb\u5165\u5faa\u73af\u7684\u6761\u4ef6 count > x \u6c42\u503c\u4e3a false \uff0c\u6240\u4ee5\u5faa\u73af\u6839\u672c\u6ca1\u6709\u8fdb\u884c\u3002 \u65e0\u9650\u5faa\u73af \u5728 7.7 - \u5faa\u73af\u548c while \u8bed\u53e5 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u65e0\u9650\u5faa\u73af\uff08\u6b7b\u5faa\u73af\uff09\uff0c\u8bf7\u770b\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int count { 1 }; while ( count <= 10 ) // this condition will never be false { std :: cout << count << ' ' ; // so this line will repeatedly execute } std :: cout << '\\n' ; // this line will never execute return 0 ; // this line will never execute } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5fd8\u8bb0\u4e86\u5bf9 count \u8fdb\u884c\u9012\u589e\uff0c\u6240\u4ee5\u5faa\u73af\u6761\u4ef6\u6c42\u503c\u7ed3\u679c\u6c38\u8fdc\u90fd\u4e0d\u4e3a false \uff0c\u5faa\u73af\u4e5f\u5c31\u6c38\u8fdc\u4e0d\u4f1a\u505c\u6b62\uff0c\u53ea\u80fd\u4e0d\u505c\u6253\u5370\uff1a 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 \u76f4\u5230\u7528\u6237\u7ec8\u6b62\u8be5\u7a0b\u5e8f\u3002 \u8fd9\u662f\u53e6\u4e00\u4e2a\u5bb9\u6613\u88ab\u95ee\u5230\u7684\u5c0f\u6d4b\u9a8c\u95ee\u9898\u2014\u2014\u4e0b\u9762\u7684\u4ee3\u7801\u6709\u4ec0\u4e48\u95ee\u9898? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { for ( unsigned int count { 5 }; count >= 0 ; -- count ) { if ( count == 0 ) std :: cout << \"blastoff! \" ; else std :: cout << count << ' ' ; } std :: cout << '\\n' ; return 0 ; } COPY \u7a0b\u5e8f\u7684\u672c\u610f\u662f\u6253\u5370 5 4 3 2 1 blastoff! \uff0c\u4f46\u662f\u5728\u5b8c\u6210\u64cd\u4f5c\u540e\uff0c\u7a0b\u5e8f\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\u505c\u6b62\uff0c\u800c\u662f\u7ee7\u7eed\u6253\u5370\uff1a 1 5 4 3 2 1 blastoff! 4294967295 4294967294 4294967293 4294967292 4294967291 \u7136\u540e\u7ee7\u7eed\u9012\u51cf\u3002\u7a0b\u5e8f\u6c38\u8fdc\u4e0d\u4f1a\u505c\u6b62\uff0c\u56e0\u4e3a\u5f53 count \u662f\u65e0\u7b26\u53f7\u6574\u578b\u65f6 count >= 0 \u6c38\u8fdc\u4e3a\u771f\u3002 \u5dee\u4e00\u9519\u8bef \u5dee\u4e00\u9519\u8bef \u6307\u7684\u662f\u5faa\u73af\u591a\u6267\u884c\u4e00\u6b21\u6216\u5c11\u6267\u884c\u4e00\u6b21\u800c\u4ea7\u751f\u7684\u9519\u8bef\u3002\u60c5\u51b5\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { for ( unsigned int count { 1 }; count < 5 ; ++ count ) { std :: cout << count << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u7a0b\u5e8f\u5458\u5e0c\u671b\u4ee3\u7801\u6253\u5370 1 2 3 4 5 \u3002\u4f46\u662f\uff0c\u7531\u4e8e\u4f7f\u7528\u4e86\u9519\u8bef\u7684\u8fd0\u7b97\u7b26 ( < \u800c\u4e0d\u662f <= )\uff0c\u6240\u4ee5\u5faa\u73af\u5c11\u6267\u884c\u4e86\u4e00\u6b21\uff0c\u7a0b\u5e8f\u6253\u5370 1 2 3 4 \u3002 \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u9519\u8bef \u5728 5.7 - \u903b\u8f91\u8fd0\u7b97\u7b26 \u4e2d\uff0c\u4e0b\u9762\u7a0b\u5e8f\u5b58\u5728\u4f18\u5148\u7ea7\u9519\u8bef\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 5 }; int y { 7 }; if ( ! x > y ) // oops: operator precedence issue std :: cout << x << \" is not greater than \" << y << '\\n' ; else std :: cout << x << \" is greater than \" << y << '\\n' ; return 0 ; } \u56e0\u4e3a\u903b\u8f91\u8fd0\u7b97\u7b26 NOT \u6bd4 operator> \u7684\u4f18\u5148\u7ea7\u66f4\u9ad8\uff0c\u6240\u4ee5\u6761\u4ef6\u8868\u8fbe\u5f0f\u5b9e\u9645\u4e0a\u662f\u6309\u7167 (!x) > y \u6c42\u503c\uff0c\u8fd9\u5e76\u4e0d\u662f\u7a0b\u5e8f\u5458\u6240\u5e0c\u671b\u7684\u3002 \u7ed3\u679c\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u8f93\u51fa\uff1a 1 5 is greater than 7 \u5728\u540c\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u6df7\u5408\u903b\u8f91\u6216\u548c\u903b\u8f91\u4e0e\u65f6\u4e5f\u4f1a\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5(\u903b\u8f91\u4e0e\u4f18\u5148\u4e8e\u903b\u8f91\u6216)\u3002\u4f7f\u7528\u663e\u5f0f\u62ec\u53f7\u53ef\u4ee5\u907f\u514d\u8fd9\u7c7b\u9519\u8bef\u3002 \u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u95ee\u9898 \u4e0b\u9762\u7684\u6d6e\u70b9\u53d8\u91cf\u6ca1\u6709\u8db3\u591f\u7684\u7cbe\u5ea6\u6765\u5b58\u50a8\u6574\u4e2a\u6570\u5b57\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { float f { 0.123456789f }; std :: cout << f << '\\n' ; return 0 ; } \u7531\u4e8e\u7f3a\u4e4f\u7cbe\u5ea6\uff0c\u8fd9\u4e2a\u6570\u5b57\u88ab\u7a0d\u7a0d\u56db\u820d\u4e94\u5165\uff1a 1 0.123457 \u5728 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 \u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86 operator== \u548c operator != \u5728\u5e94\u5bf9\u6d6e\u70b9\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u4ea7\u751f\u7531\u4e8e\u5fae\u5c0f\u6444\u5165\u800c\u5bfc\u81f4\u7684\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { double d { 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 }; // should sum to 1.0 if ( d == 1.0 ) std :: cout << \"equal \\n \" ; else std :: cout << \"not equal \\n \" ; return 0 ; } \u6253\u5370\uff1a 1 not equal \u7531\u4e8e\u7f3a\u4e4f\u7cbe\u5ea6\uff0c\u8fd9\u4e2a\u6570\u5b57\u4f1a\u7565\u5fae\u56db\u820d\u4e94\u5165\uff1a\u4f60\u5bf9\u6d6e\u70b9\u6570\u505a\u7684\u7b97\u672f\u8d8a\u591a\uff0c\u5b83\u5c31\u4f1a\u79ef\u7d2f\u8d8a\u591a\u7684\u5c0f\u56db\u820d\u4e94\u5165\u9519\u8bef\u3002 \u6574\u578b\u9664\u6cd5 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6253\u7b97\u505a\u6d6e\u70b9\u9664\u6cd5\uff0c\u4f46\u56e0\u4e3a\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u662f\u6574\u6570\uff0c\u6240\u4ee5\u6211\u4eec\u6700\u7ec8\u505a\u7684\u662f\u6574\u6570\u9664\u6cd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { int x { 5 }; int y { 3 }; std :: cout << x << \" divided by \" << y << \" is: \" << x / y << '\\n' ; // integer division return 0 ; } \u6253\u5370\uff1a 1 5 divided by 3 is: 1 \u5728 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528 static_cast \u5c06\u6574\u578b\u64cd\u4f5c\u6570\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u8fdb\u884c\u6d6e\u70b9\u6570\u9664\u6cd5\u3002 \u610f\u5916\u9020\u6210\u7684\u7a7a\u8bed\u53e5 \u5728 7.3 - \u5e38\u89c1\u7684 if \u8bed\u53e5\u9519\u8bef \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u7a7a\u8bed\u53e5\u2014\u2014\u7a7a\u8bed\u53e5\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\u3002 \u5728\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u53ea\u60f3\u5728\u5f97\u5230\u7528\u6237\u8bb8\u53ef\u7684\u60c5\u51b5\u4e0b\"\u70b8\u6389\u8fd9\u4e2a\u4e16\u754c\"\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include void blowUpWorld () { std :: cout << \"Kaboom! \\n \" ; } int main () { std :: cout << \"Should we blow up the world again? (y/n): \" ; char c {}; std :: cin >> c ; if ( c == 'y' ); // \u610f\u5916\u5bfc\u81f4\u4e86\u7a7a\u8bed\u53e5 null statement here blowUpWorld (); // \u8be5\u8bed\u53e5\u603b\u662f\u4f1a\u6267\u884c\uff0c\u56e0\u4e3a\u5b83\u4e0d\u5c5e\u4e8eif\u8bed\u53e5 return 0 ; } \u4f46\u662f\uff0c\u56e0\u4e3a\u5b58\u5728\u4e00\u6761\u610f\u5916\u9020\u6210\u7684\u7a7a\u8bed\u53e5\uff0c\u51fd\u6570 blowUpWorld() \u603b\u662f\u4f1a\u6267\u884c\uff0c\u6240\u4ee5\u201c\u4e16\u754c\u201d\u603b\u662f\u4f1a\u88ab\u6bc1\u706d\uff1a 1 2 Should we blow up the world again? (y/n): n Kaboom! \u5f53\u9700\u8981\u590d\u5408\u8bed\u53e5\u65f6\uff0c\u6ca1\u6709\u4f7f\u7528\u590d\u5408\u8bed\u53e5 \u4e0a\u8ff0\u7a0b\u5e8f\u7684\u53e6\u4e00\u79cd\u53d8\u4f53\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void blowUpWorld () { std :: cout << \"Kaboom! \\n \" ; } int main () { std :: cout << \"Should we blow up the world again? (y/n): \" ; char c {}; std :: cin >> c ; if ( c == 'y' ) std :: cout << \"Okay, here we go... \\n \" ; blowUpWorld (); // oops, will always execute. Should be inside compound statement. return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 2 Should we blow up the world again? (y/n): n Kaboom! \u60ac\u5782 else (\u53c2\u89c1 7.3 - \u5e38\u89c1\u7684 if \u8bed\u53e5\u9519\u8bef ) \u4e5f\u4f1a\u5bfc\u81f4\u6b64\u7c7b\u95ee\u9898\u3002 \u8fd8\u6709\u4ec0\u4e48\uff1f \u4ee5\u4e0a\u662f\u65b0\u624bC++\u7a0b\u5e8f\u5458\u5bb9\u6613\u72af\u7684\u3001\u6700\u5e38\u89c1\u8bed\u4e49\u9519\u8bef\uff0c\u5f53\u7136\uff0c\u9664\u4e86\u8fd9\u4e9b\u8fd8\u6709\u66f4\u591a\u3002\u8bfb\u8005\u4eec\uff0c\u5982\u679c\u4f60\u8fd8\u6709\u5176\u4ed6\u4f60\u8ba4\u4e3a\u5e38\u89c1\u7684\u9677\u9631\uff0c\u8bf7\u5728\u8bc4\u8bba\u533a\u544a\u8bc9\u6211\u4eec\u3002","title":"7.14 - C++ \u4e2d\u5e38\u89c1\u7684\u8bed\u4e49\u9519\u8bef"},{"location":"7-14-Common-semantic-errors-in-c/#_1","text":"\u6700\u5e38\u89c1\u7684\u8bed\u4e49\u9519\u8bef\u7c7b\u578b\u4e4b\u4e00\u662f \u6761\u4ef6\u903b\u8f91\u9519\u8bef \u3002\u5f53\u7a0b\u5e8f\u5458\u9519\u8bef\u5730\u7f16\u5199\u4e86\u6761\u4ef6\u8bed\u53e5\u6216\u5faa\u73af\u6761\u4ef6\u7684\u903b\u8f91\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u6761\u4ef6\u903b\u8f91\u9519\u8bef\u3002\u8fd9\u91cc\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; if ( x >= 5 ) // oops, we used operator>= instead of operator> std :: cout << x << \" is greater than 5 \\n \" ; return 0 ; } \u8fd0\u884c\u7a0b\u5e8f\uff0c\u5f88\u5bb9\u6613\u89c2\u5bdf\u5230\u903b\u8f91\u9519\u8bef\uff1a 1 2 Enter an integer: 5 5 is greater than 5 \u5f53\u7528\u6237\u8f93\u51655\u65f6\uff0c\u6761\u4ef6\u8868\u8fbe\u5f0f x >= 5 \u6c42\u503c\u4e3a true \uff0c\u6240\u4ee5\u5bf9\u5e94\u7684\u6253\u5370\u8bed\u53e5\u5c31\u6267\u884c\u4e86\u3002 \u53e6\u5916\u4e00\u4e2a\u4f8b\u5b50\u662f\u5173\u4e8e for \u5faa\u73af\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; // oops, we used operator> instead of operator< for ( int count { 1 }; count > x ; ++ count ) { std :: cout << count << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u5e94\u8be5\u6253\u5370\u4ece1\u5230\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\u4e4b\u95f4\u7684\u6240\u6709\u6570\u5b57\u3002\u4f46\u5b83\u5b9e\u9645\u4e0a\u662f\u8fd9\u6837\u505a\u7684\uff1a 1 Enter an integer: 5 \u7a0b\u5e8f\u6ca1\u6709\u6253\u5370\u4efb\u4f55\u5185\u5bb9\u3002\u8fd9\u662f\u56e0\u4e3a\u8fdb\u5165\u5faa\u73af\u7684\u6761\u4ef6 count > x \u6c42\u503c\u4e3a false \uff0c\u6240\u4ee5\u5faa\u73af\u6839\u672c\u6ca1\u6709\u8fdb\u884c\u3002","title":"\u6761\u4ef6\u903b\u8f91\u9519\u8bef"},{"location":"7-14-Common-semantic-errors-in-c/#_2","text":"\u5728 7.7 - \u5faa\u73af\u548c while \u8bed\u53e5 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u65e0\u9650\u5faa\u73af\uff08\u6b7b\u5faa\u73af\uff09\uff0c\u8bf7\u770b\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int count { 1 }; while ( count <= 10 ) // this condition will never be false { std :: cout << count << ' ' ; // so this line will repeatedly execute } std :: cout << '\\n' ; // this line will never execute return 0 ; // this line will never execute } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5fd8\u8bb0\u4e86\u5bf9 count \u8fdb\u884c\u9012\u589e\uff0c\u6240\u4ee5\u5faa\u73af\u6761\u4ef6\u6c42\u503c\u7ed3\u679c\u6c38\u8fdc\u90fd\u4e0d\u4e3a false \uff0c\u5faa\u73af\u4e5f\u5c31\u6c38\u8fdc\u4e0d\u4f1a\u505c\u6b62\uff0c\u53ea\u80fd\u4e0d\u505c\u6253\u5370\uff1a 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 \u76f4\u5230\u7528\u6237\u7ec8\u6b62\u8be5\u7a0b\u5e8f\u3002 \u8fd9\u662f\u53e6\u4e00\u4e2a\u5bb9\u6613\u88ab\u95ee\u5230\u7684\u5c0f\u6d4b\u9a8c\u95ee\u9898\u2014\u2014\u4e0b\u9762\u7684\u4ee3\u7801\u6709\u4ec0\u4e48\u95ee\u9898? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { for ( unsigned int count { 5 }; count >= 0 ; -- count ) { if ( count == 0 ) std :: cout << \"blastoff! \" ; else std :: cout << count << ' ' ; } std :: cout << '\\n' ; return 0 ; } COPY \u7a0b\u5e8f\u7684\u672c\u610f\u662f\u6253\u5370 5 4 3 2 1 blastoff! \uff0c\u4f46\u662f\u5728\u5b8c\u6210\u64cd\u4f5c\u540e\uff0c\u7a0b\u5e8f\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\u505c\u6b62\uff0c\u800c\u662f\u7ee7\u7eed\u6253\u5370\uff1a 1 5 4 3 2 1 blastoff! 4294967295 4294967294 4294967293 4294967292 4294967291 \u7136\u540e\u7ee7\u7eed\u9012\u51cf\u3002\u7a0b\u5e8f\u6c38\u8fdc\u4e0d\u4f1a\u505c\u6b62\uff0c\u56e0\u4e3a\u5f53 count \u662f\u65e0\u7b26\u53f7\u6574\u578b\u65f6 count >= 0 \u6c38\u8fdc\u4e3a\u771f\u3002","title":"\u65e0\u9650\u5faa\u73af"},{"location":"7-14-Common-semantic-errors-in-c/#_3","text":"\u5dee\u4e00\u9519\u8bef \u6307\u7684\u662f\u5faa\u73af\u591a\u6267\u884c\u4e00\u6b21\u6216\u5c11\u6267\u884c\u4e00\u6b21\u800c\u4ea7\u751f\u7684\u9519\u8bef\u3002\u60c5\u51b5\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { for ( unsigned int count { 1 }; count < 5 ; ++ count ) { std :: cout << count << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u7a0b\u5e8f\u5458\u5e0c\u671b\u4ee3\u7801\u6253\u5370 1 2 3 4 5 \u3002\u4f46\u662f\uff0c\u7531\u4e8e\u4f7f\u7528\u4e86\u9519\u8bef\u7684\u8fd0\u7b97\u7b26 ( < \u800c\u4e0d\u662f <= )\uff0c\u6240\u4ee5\u5faa\u73af\u5c11\u6267\u884c\u4e86\u4e00\u6b21\uff0c\u7a0b\u5e8f\u6253\u5370 1 2 3 4 \u3002","title":"\u5dee\u4e00\u9519\u8bef"},{"location":"7-14-Common-semantic-errors-in-c/#_4","text":"\u5728 5.7 - \u903b\u8f91\u8fd0\u7b97\u7b26 \u4e2d\uff0c\u4e0b\u9762\u7a0b\u5e8f\u5b58\u5728\u4f18\u5148\u7ea7\u9519\u8bef\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 5 }; int y { 7 }; if ( ! x > y ) // oops: operator precedence issue std :: cout << x << \" is not greater than \" << y << '\\n' ; else std :: cout << x << \" is greater than \" << y << '\\n' ; return 0 ; } \u56e0\u4e3a\u903b\u8f91\u8fd0\u7b97\u7b26 NOT \u6bd4 operator> \u7684\u4f18\u5148\u7ea7\u66f4\u9ad8\uff0c\u6240\u4ee5\u6761\u4ef6\u8868\u8fbe\u5f0f\u5b9e\u9645\u4e0a\u662f\u6309\u7167 (!x) > y \u6c42\u503c\uff0c\u8fd9\u5e76\u4e0d\u662f\u7a0b\u5e8f\u5458\u6240\u5e0c\u671b\u7684\u3002 \u7ed3\u679c\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u8f93\u51fa\uff1a 1 5 is greater than 7 \u5728\u540c\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u6df7\u5408\u903b\u8f91\u6216\u548c\u903b\u8f91\u4e0e\u65f6\u4e5f\u4f1a\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5(\u903b\u8f91\u4e0e\u4f18\u5148\u4e8e\u903b\u8f91\u6216)\u3002\u4f7f\u7528\u663e\u5f0f\u62ec\u53f7\u53ef\u4ee5\u907f\u514d\u8fd9\u7c7b\u9519\u8bef\u3002","title":"\u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u9519\u8bef"},{"location":"7-14-Common-semantic-errors-in-c/#_5","text":"\u4e0b\u9762\u7684\u6d6e\u70b9\u53d8\u91cf\u6ca1\u6709\u8db3\u591f\u7684\u7cbe\u5ea6\u6765\u5b58\u50a8\u6574\u4e2a\u6570\u5b57\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { float f { 0.123456789f }; std :: cout << f << '\\n' ; return 0 ; } \u7531\u4e8e\u7f3a\u4e4f\u7cbe\u5ea6\uff0c\u8fd9\u4e2a\u6570\u5b57\u88ab\u7a0d\u7a0d\u56db\u820d\u4e94\u5165\uff1a 1 0.123457 \u5728 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 \u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86 operator== \u548c operator != \u5728\u5e94\u5bf9\u6d6e\u70b9\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u4ea7\u751f\u7531\u4e8e\u5fae\u5c0f\u6444\u5165\u800c\u5bfc\u81f4\u7684\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { double d { 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 }; // should sum to 1.0 if ( d == 1.0 ) std :: cout << \"equal \\n \" ; else std :: cout << \"not equal \\n \" ; return 0 ; } \u6253\u5370\uff1a 1 not equal \u7531\u4e8e\u7f3a\u4e4f\u7cbe\u5ea6\uff0c\u8fd9\u4e2a\u6570\u5b57\u4f1a\u7565\u5fae\u56db\u820d\u4e94\u5165\uff1a\u4f60\u5bf9\u6d6e\u70b9\u6570\u505a\u7684\u7b97\u672f\u8d8a\u591a\uff0c\u5b83\u5c31\u4f1a\u79ef\u7d2f\u8d8a\u591a\u7684\u5c0f\u56db\u820d\u4e94\u5165\u9519\u8bef\u3002","title":"\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u95ee\u9898"},{"location":"7-14-Common-semantic-errors-in-c/#_6","text":"\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6253\u7b97\u505a\u6d6e\u70b9\u9664\u6cd5\uff0c\u4f46\u56e0\u4e3a\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u662f\u6574\u6570\uff0c\u6240\u4ee5\u6211\u4eec\u6700\u7ec8\u505a\u7684\u662f\u6574\u6570\u9664\u6cd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { int x { 5 }; int y { 3 }; std :: cout << x << \" divided by \" << y << \" is: \" << x / y << '\\n' ; // integer division return 0 ; } \u6253\u5370\uff1a 1 5 divided by 3 is: 1 \u5728 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528 static_cast \u5c06\u6574\u578b\u64cd\u4f5c\u6570\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u8fdb\u884c\u6d6e\u70b9\u6570\u9664\u6cd5\u3002","title":"\u6574\u578b\u9664\u6cd5"},{"location":"7-14-Common-semantic-errors-in-c/#_7","text":"\u5728 7.3 - \u5e38\u89c1\u7684 if \u8bed\u53e5\u9519\u8bef \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u7a7a\u8bed\u53e5\u2014\u2014\u7a7a\u8bed\u53e5\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\u3002 \u5728\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u53ea\u60f3\u5728\u5f97\u5230\u7528\u6237\u8bb8\u53ef\u7684\u60c5\u51b5\u4e0b\"\u70b8\u6389\u8fd9\u4e2a\u4e16\u754c\"\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include void blowUpWorld () { std :: cout << \"Kaboom! \\n \" ; } int main () { std :: cout << \"Should we blow up the world again? (y/n): \" ; char c {}; std :: cin >> c ; if ( c == 'y' ); // \u610f\u5916\u5bfc\u81f4\u4e86\u7a7a\u8bed\u53e5 null statement here blowUpWorld (); // \u8be5\u8bed\u53e5\u603b\u662f\u4f1a\u6267\u884c\uff0c\u56e0\u4e3a\u5b83\u4e0d\u5c5e\u4e8eif\u8bed\u53e5 return 0 ; } \u4f46\u662f\uff0c\u56e0\u4e3a\u5b58\u5728\u4e00\u6761\u610f\u5916\u9020\u6210\u7684\u7a7a\u8bed\u53e5\uff0c\u51fd\u6570 blowUpWorld() \u603b\u662f\u4f1a\u6267\u884c\uff0c\u6240\u4ee5\u201c\u4e16\u754c\u201d\u603b\u662f\u4f1a\u88ab\u6bc1\u706d\uff1a 1 2 Should we blow up the world again? (y/n): n Kaboom! \u5f53\u9700\u8981\u590d\u5408\u8bed\u53e5\u65f6\uff0c\u6ca1\u6709\u4f7f\u7528\u590d\u5408\u8bed\u53e5 \u4e0a\u8ff0\u7a0b\u5e8f\u7684\u53e6\u4e00\u79cd\u53d8\u4f53\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void blowUpWorld () { std :: cout << \"Kaboom! \\n \" ; } int main () { std :: cout << \"Should we blow up the world again? (y/n): \" ; char c {}; std :: cin >> c ; if ( c == 'y' ) std :: cout << \"Okay, here we go... \\n \" ; blowUpWorld (); // oops, will always execute. Should be inside compound statement. return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 2 Should we blow up the world again? (y/n): n Kaboom! \u60ac\u5782 else (\u53c2\u89c1 7.3 - \u5e38\u89c1\u7684 if \u8bed\u53e5\u9519\u8bef ) \u4e5f\u4f1a\u5bfc\u81f4\u6b64\u7c7b\u95ee\u9898\u3002","title":"\u610f\u5916\u9020\u6210\u7684\u7a7a\u8bed\u53e5"},{"location":"7-14-Common-semantic-errors-in-c/#_8","text":"\u4ee5\u4e0a\u662f\u65b0\u624bC++\u7a0b\u5e8f\u5458\u5bb9\u6613\u72af\u7684\u3001\u6700\u5e38\u89c1\u8bed\u4e49\u9519\u8bef\uff0c\u5f53\u7136\uff0c\u9664\u4e86\u8fd9\u4e9b\u8fd8\u6709\u66f4\u591a\u3002\u8bfb\u8005\u4eec\uff0c\u5982\u679c\u4f60\u8fd8\u6709\u5176\u4ed6\u4f60\u8ba4\u4e3a\u5e38\u89c1\u7684\u9677\u9631\uff0c\u8bf7\u5728\u8bc4\u8bba\u533a\u544a\u8bc9\u6211\u4eec\u3002","title":"\u8fd8\u6709\u4ec0\u4e48\uff1f"},{"location":"7-15-Detecting-and-handling-errors/","tags":["error handling"],"text":"Key Takeaway In lesson 7.14 -- Common semantic errors in C++ , we covered many types of common C++ semantic errors that new C++ programmers run into with the language. If an error is the result of a misused language feature or logic error, the error can simply be corrected. But most errors in a program don\u2019t occur as the result of inadvertently misusing language features -- rather, most errors occur due to faulty assumptions made by the programmer and/or a lack of proper error detection/handling. For example, in a function designed to look up a grade for a student, you might have assumed: The student being looked up will exist. All student names will be unique. The class uses letter grading (instead of pass/fail). What if any of these assumptions aren\u2019t true? If the programmer didn\u2019t anticipate these cases, the program will likely malfunction or crash when such cases arise (usually at some point in the future, well after the function has been written). There are three key places where assumption errors typically occur: When a function returns, the programmer may have assumed the called function was successful when it was not. When a program receives input (either from the user, or a file), the programmer may have assumed the input was in the correct format and semantically valid when it was not. When a function has been called, the programmer may have assumed the arguments would be semantically valid when they were not. Many new programmers write code and then only test the happy path: only the cases where there are no errors. But you should also be planning for and testing your sad paths, where things can and will go wrong. In lesson 3.10 -- Finding issues before they become problems , we defined defensive programming as the practice of trying to anticipate all of the ways software can be misused, either by end-users, or by developers (either the programmer themselves, or others). Once you\u2019ve anticipated (or discovered) some misuse, the next thing to do is handle it. In this lesson, we\u2019ll talk about error handling strategies (what to do when things go wrong) inside a function. In the subsequent lessons, we\u2019ll talk about validating user input, and then introduce a useful tool to help document and validate assumptions. Handling errors in functions Functions may fail for any number of reasons -- the caller may have passed in an argument with an invalid value, or something may fail within the body of the function. For example, a function that opens a file for reading might fail if the file cannot be found. When this happens, you have quite a few options at your disposal. There is no best way to handle an error -- it really depends on the nature of the problem and whether the problem can be fixed or not. There are 4 general strategies that can be used: Handle the error within the function Pass the error back to the caller to deal with Halt the program Throw an exception Handling the error within the function If possible, the best strategy is to recover from the error in the same function in which the error occurred, so that the error can be contained and corrected without impacting any code outside the function. There are two options here: retry until successful, or cancel the operation being executed. If the error has occurred due to something outside of the program\u2019s control, the program can retry until success is achieved. For example, if the program requires an internet connection, and the user has lost their connection, the program may be able to display a warning and then use a loop to periodically recheck for internet connectivity. Alternatively, if the user has entered invalid input, the program can ask the user to try again, and loop until the user is successful at entering valid input. We\u2019ll show examples of handling invalid input and using loops to retry in the next lesson ( 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406 )\u3002 An alternate strategy is just to ignore the error and/or cancel the operation. For example: 1 2 3 4 5 void printDivision ( int x , int y ) { if ( y != 0 ) std :: cout << static_cast < double > ( x ) / y ; } COPY In the above example, if the user passed in an invalid value for y , we just ignore the request to print the result of the division operation. The primary challenge with doing this is that the caller or user have no way to identify that something went wrong. In such case, printing an error message can be helpful: 1 2 3 4 5 6 7 void printDivision ( int x , int y ) { if ( y != 0 ) std :: cout << static_cast < double > ( x ) / y ; else std :: cerr << \"Error: Could not divide by zero \\n \" ; } COPY However, if the calling function is expecting the called function to produce a return value or some useful side-effect, then just ignoring the error may not be an option. \u76f8\u5173\u5185\u5bb9 We discuss std::cerr in lesson 3.4 \u2014 \u57fa\u672c\u4ee3\u7801\u8c03\u8bd5\u6280\u672f \u5c06\u9519\u8bef\u4f20\u9012\u7ed9\u8c03\u7528\u8005 In many cases, the error can\u2019t reasonably be handled in the function that detects the error. For example, consider the following function: 1 2 3 4 double doDivision ( int x , int y ) { return static_cast < double > ( x ) / y ; } COPY If y is 0 , what should we do? We can\u2019t just skip the program logic, because the function needs to return some value. We shouldn\u2019t ask the user to enter a new value for y because this is a calculation function, and introducing input routines into it may or may not be appropriate for the program calling this function. In such cases, the best option can be to pass the error back to the caller in hopes that the caller will be able to deal with it. How might we do that? If the function has a void return type, it can be changed to return a Boolean that indicates success or failure. For example, instead of: 1 2 3 4 5 6 7 void printDivision ( int x , int y ) { if ( y != 0 ) std :: cout << static_cast < double > ( x ) / y ; else std :: cerr << \"Error: Could not divide by zero \\n \" ; } COPY We can do this: 1 2 3 4 5 6 7 8 9 10 11 12 bool printDivision ( int x , int y ) { if ( y == 0 ) { std :: cerr << \"Error: could not divide by zero \\n \" ; return false ; } std :: cout << static_cast < double > ( x ) / y ; return true ; } COPY That way, the caller can check the return value to see if the function failed for some reason. If the function returns a normal value, things are a little more complicated. In some cases, the full range of return values isn\u2019t used. In such cases, we can use a return value that wouldn\u2019t otherwise be possible to occur normally to indicate an error. For example, consider the following function: 1 2 3 4 5 // The reciprocal of x is 1/x double reciprocal ( double x ) { return 1.0 / x ; } COPY The reciprocal of some number x is defined as 1/x , and a number multiplied by its reciprocal equals 1. However, what happens if the user calls this function as reciprocal(0.0) ? We get a divide by zero error and a program crash, so clearly we should protect against this case. But this function must return a double value, so what value should we return? It turns out that this function will never produce 0.0 as a legitimate result, so we can return 0.0 to indicate an error case. 1 2 3 4 5 6 7 8 // The reciprocal of x is 1/x, returns 0.0 if x=0 double reciprocal ( double x ) { if ( x == 0.0 ) return 0.0 ; return 1.0 / x ; } COPY However, if the full range of return values are needed, then using the return value to indicate an error will not be possible (because the caller would not be able to tell whether the return value is a valid value or an error value). In such a case, an out parameter (covered in lesson 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 ) might be a viable choice. \u81f4\u547d\u9519\u8bef If the error is so bad that the program can not continue to operate properly, this is called a non-recoverable error (also called a fatal error). In such cases, the best thing to do is terminate the program. If your code is in main() or a function called directly from main() , the best thing to do is let main() return a non-zero status code. However, if you\u2019re deep in some nested subfunction, it may not be convenient or possible to propagate the error all the way back to main() . In such a case, a halt statement (such as std::exit() ) can be used. For example: 1 2 3 4 5 6 7 8 9 double doDivision ( int x , int y ) { if ( y == 0 ) { std :: cerr << \"Error: Could not divide by zero \\n \" ; std :: exit ( 1 ); } return static_cast < double > ( x ) / y ; } \u5f02\u5e38 Because returning an error from a function back to the caller is complicated (and the many different ways to do so leads to inconsistency, and inconsistency leads to mistakes), C++ offers an entirely separate way to pass errors back to the caller: exceptions . The basic idea is that when an error occurs, an exception is \u201cthrown\u201d. If the current function does not \u201ccatch\u201d the error, the caller of the function has a chance to catch the error. If the caller does not catch the error, the caller\u2019s caller has a chance to catch the error. The error progressively moves up the call stack until it is either caught and handled (at which point execution continues normally), or until main() fails to handle the error (at which point the program is terminated with an exception error). We cover exception handling in chapter 20 of this tutorial series.","title":"7.15 - \u53d1\u73b0\u9519\u8bef\u548c\u5904\u7406\u9519\u8bef"},{"location":"7-15-Detecting-and-handling-errors/#handling-errors-in-functions","text":"Functions may fail for any number of reasons -- the caller may have passed in an argument with an invalid value, or something may fail within the body of the function. For example, a function that opens a file for reading might fail if the file cannot be found. When this happens, you have quite a few options at your disposal. There is no best way to handle an error -- it really depends on the nature of the problem and whether the problem can be fixed or not. There are 4 general strategies that can be used: Handle the error within the function Pass the error back to the caller to deal with Halt the program Throw an exception","title":"Handling errors in functions"},{"location":"7-15-Detecting-and-handling-errors/#handling-the-error-within-the-function","text":"If possible, the best strategy is to recover from the error in the same function in which the error occurred, so that the error can be contained and corrected without impacting any code outside the function. There are two options here: retry until successful, or cancel the operation being executed. If the error has occurred due to something outside of the program\u2019s control, the program can retry until success is achieved. For example, if the program requires an internet connection, and the user has lost their connection, the program may be able to display a warning and then use a loop to periodically recheck for internet connectivity. Alternatively, if the user has entered invalid input, the program can ask the user to try again, and loop until the user is successful at entering valid input. We\u2019ll show examples of handling invalid input and using loops to retry in the next lesson ( 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406 )\u3002 An alternate strategy is just to ignore the error and/or cancel the operation. For example: 1 2 3 4 5 void printDivision ( int x , int y ) { if ( y != 0 ) std :: cout << static_cast < double > ( x ) / y ; } COPY In the above example, if the user passed in an invalid value for y , we just ignore the request to print the result of the division operation. The primary challenge with doing this is that the caller or user have no way to identify that something went wrong. In such case, printing an error message can be helpful: 1 2 3 4 5 6 7 void printDivision ( int x , int y ) { if ( y != 0 ) std :: cout << static_cast < double > ( x ) / y ; else std :: cerr << \"Error: Could not divide by zero \\n \" ; } COPY However, if the calling function is expecting the called function to produce a return value or some useful side-effect, then just ignoring the error may not be an option. \u76f8\u5173\u5185\u5bb9 We discuss std::cerr in lesson 3.4 \u2014 \u57fa\u672c\u4ee3\u7801\u8c03\u8bd5\u6280\u672f","title":"Handling the error within the function"},{"location":"7-15-Detecting-and-handling-errors/#_1","text":"In many cases, the error can\u2019t reasonably be handled in the function that detects the error. For example, consider the following function: 1 2 3 4 double doDivision ( int x , int y ) { return static_cast < double > ( x ) / y ; } COPY If y is 0 , what should we do? We can\u2019t just skip the program logic, because the function needs to return some value. We shouldn\u2019t ask the user to enter a new value for y because this is a calculation function, and introducing input routines into it may or may not be appropriate for the program calling this function. In such cases, the best option can be to pass the error back to the caller in hopes that the caller will be able to deal with it. How might we do that? If the function has a void return type, it can be changed to return a Boolean that indicates success or failure. For example, instead of: 1 2 3 4 5 6 7 void printDivision ( int x , int y ) { if ( y != 0 ) std :: cout << static_cast < double > ( x ) / y ; else std :: cerr << \"Error: Could not divide by zero \\n \" ; } COPY We can do this: 1 2 3 4 5 6 7 8 9 10 11 12 bool printDivision ( int x , int y ) { if ( y == 0 ) { std :: cerr << \"Error: could not divide by zero \\n \" ; return false ; } std :: cout << static_cast < double > ( x ) / y ; return true ; } COPY That way, the caller can check the return value to see if the function failed for some reason. If the function returns a normal value, things are a little more complicated. In some cases, the full range of return values isn\u2019t used. In such cases, we can use a return value that wouldn\u2019t otherwise be possible to occur normally to indicate an error. For example, consider the following function: 1 2 3 4 5 // The reciprocal of x is 1/x double reciprocal ( double x ) { return 1.0 / x ; } COPY The reciprocal of some number x is defined as 1/x , and a number multiplied by its reciprocal equals 1. However, what happens if the user calls this function as reciprocal(0.0) ? We get a divide by zero error and a program crash, so clearly we should protect against this case. But this function must return a double value, so what value should we return? It turns out that this function will never produce 0.0 as a legitimate result, so we can return 0.0 to indicate an error case. 1 2 3 4 5 6 7 8 // The reciprocal of x is 1/x, returns 0.0 if x=0 double reciprocal ( double x ) { if ( x == 0.0 ) return 0.0 ; return 1.0 / x ; } COPY However, if the full range of return values are needed, then using the return value to indicate an error will not be possible (because the caller would not be able to tell whether the return value is a valid value or an error value). In such a case, an out parameter (covered in lesson 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 ) might be a viable choice.","title":"\u5c06\u9519\u8bef\u4f20\u9012\u7ed9\u8c03\u7528\u8005"},{"location":"7-15-Detecting-and-handling-errors/#_2","text":"If the error is so bad that the program can not continue to operate properly, this is called a non-recoverable error (also called a fatal error). In such cases, the best thing to do is terminate the program. If your code is in main() or a function called directly from main() , the best thing to do is let main() return a non-zero status code. However, if you\u2019re deep in some nested subfunction, it may not be convenient or possible to propagate the error all the way back to main() . In such a case, a halt statement (such as std::exit() ) can be used. For example: 1 2 3 4 5 6 7 8 9 double doDivision ( int x , int y ) { if ( y == 0 ) { std :: cerr << \"Error: Could not divide by zero \\n \" ; std :: exit ( 1 ); } return static_cast < double > ( x ) / y ; }","title":"\u81f4\u547d\u9519\u8bef"},{"location":"7-15-Detecting-and-handling-errors/#_3","text":"Because returning an error from a function back to the caller is complicated (and the many different ways to do so leads to inconsistency, and inconsistency leads to mistakes), C++ offers an entirely separate way to pass errors back to the caller: exceptions . The basic idea is that when an error occurs, an exception is \u201cthrown\u201d. If the current function does not \u201ccatch\u201d the error, the caller of the function has a chance to catch the error. If the caller does not catch the error, the caller\u2019s caller has a chance to catch the error. The error progressively moves up the call stack until it is either caught and handled (at which point execution continues normally), or until main() fails to handle the error (at which point the program is terminated with an exception error). We cover exception handling in chapter 20 of this tutorial series.","title":"\u5f02\u5e38"},{"location":"7-16-std-cin-and-handling-invalid-input/","tags":["error handling"],"text":"Key Takeaway Most programs that have a user interface of some kind need to handle user input. In the programs that you have been writing, you have been using std::cin to ask the user to enter text input. Because text input is so free-form (the user can enter anything), it\u2019s very easy for the user to enter input that is not expected. As you write programs, you should always consider how users will (unintentionally or otherwise) misuse your programs. A well-written program will anticipate how users will misuse it, and either handle those cases gracefully or prevent them from happening in the first place (if possible). A program that handles error cases well is said to be robust . In this lesson, we\u2019ll take a look specifically at ways the user can enter invalid text input via std::cin, and show you some different ways to handle those cases. std::cin \u3001\u7f13\u51b2\u548c\u63d0\u53d6\u64cd\u4f5c In order to discuss how std::cin and operator>> can fail, it first helps to know a little bit about how they work. When we use operator>> to get user input and put it into a variable, this is called an \u201cextraction\u201d. The >> operator is accordingly called the extraction operator when used in this context. When the user enters input in response to an extraction operation, that data is placed in a buffer inside of std::cin. A buffer (also called a data buffer) is simply a piece of memory set aside for storing data temporarily while it\u2019s moved from one place to another. In this case, the buffer is used to hold user input while it\u2019s waiting to be extracted to variables. When the extraction operator is used, the following procedure happens: If there is data already in the input buffer, that data is used for extraction. If the input buffer contains no data, the user is asked to input data for extraction (this is the case most of the time). When the user hits enter, a \u2018\\n\u2019 character will be placed in the input buffer. operator>> extracts as much data from the input buffer as it can into the variable (ignoring any leading whitespace characters, such as spaces, tabs, or \u2018\\n\u2019). Any data that can not be extracted is left in the input buffer for the next extraction. Extraction succeeds if at least one character is extracted from the input buffer. Any unextracted input is left in the input buffer for future extractions. For example: 1 2 int x {}; std :: cin >> x ; COPY If the user enters \u201c5a\u201d, 5 will be extracted, converted to an integer, and assigned to variable x. \u201ca\\n\u201d will be left in the input buffer for the next extraction. Extraction fails if the input data does not match the type of the variable being extracted to. For example: 1 2 int x {}; std :: cin >> x ; COPY If the user were to enter \u2018b\u2019, extraction would fail because \u2018b\u2019 can not be extracted to an integer variable. \u8f93\u5165\u9a8c\u8bc1 The process of checking whether user input conforms to what the program is expecting is called input validation . There are three basic ways to do input validation: Inline (as the user types) Prevent the user from typing invalid input in the first place. Post-entry (after the user types) Let the user enter whatever they want into a string, then validate whether the string is correct, and if so, convert the string to the final variable format. Let the user enter whatever they want, let std::cin and operator>> try to extract it, and handle the error cases. Some graphical user interfaces and advanced text interfaces will let you validate input as the user enters it (character by character). Generally speaking, the programmer provides a validation function that accepts the input the user has entered so far, and returns true if the input is valid, and false otherwise. This function is called every time the user presses a key. If the validation function returns true, the key the user just pressed is accepted. If the validation function returns false, the character the user just input is discarded (and not shown on the screen). Using this method, you can ensure that any input the user enters is guaranteed to be valid, because any invalid keystrokes are discovered and discarded immediately. Unfortunately, std::cin does not support this style of validation. Since strings do not have any restrictions on what characters can be entered, extraction is guaranteed to succeed (though remember that std::cin stops extracting at the first non-leading whitespace character). Once a string is entered, the program can then parse the string to see if it is valid or not. However, parsing strings and converting string input to other types (e.g. numbers) can be challenging, so this is only done in rare cases. Most often, we let std::cin and the extraction operator do the hard work. Under this method, we let the user enter whatever they want, have std::cin and operator>> try to extract it, and deal with the fallout if it fails. This is the easiest method, and the one we\u2019ll talk more about below. \u4f8b\u7a0b Consider the following calculator program that has no error handling: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include double getDouble () { std :: cout << \"Enter a double value: \" ; double x {}; std :: cin >> x ; return x ; } char getOperator () { std :: cout << \"Enter one of the following: +, -, *, or /: \" ; char op {}; std :: cin >> op ; return op ; } void printResult ( double x , char operation , double y ) { switch ( operation ) { case '+' : std :: cout << x << \" + \" << y << \" is \" << x + y << '\\n' ; break ; case '-' : std :: cout << x << \" - \" << y << \" is \" << x - y << '\\n' ; break ; case '*' : std :: cout << x << \" * \" << y << \" is \" << x * y << '\\n' ; break ; case '/' : std :: cout << x << \" / \" << y << \" is \" << x / y << '\\n' ; break ; } } int main () { double x { getDouble () }; char operation { getOperator () }; double y { getDouble () }; printResult ( x , operation , y ); return 0 ; } COPY This simple program asks the user to enter two numbers and a mathematical operator. 1 2 3 4 Enter a double value: 5 Enter one of the following: +, -, *, or /: * Enter a double value: 7 5 * 7 is 35 Now, consider where invalid user input might break this program. First, we ask the user to enter some numbers. What if they enter something other than a number (e.g. \u2018q\u2019)? In this case, extraction will fail. Second, we ask the user to enter one of four possible symbols. What if they enter a character other than one of the symbols we\u2019re expecting? We\u2019ll be able to extract the input, but we don\u2019t currently handle what happens afterward. Third, what if we ask the user to enter a symbol and they enter a string like \u201c q hello\u201d. Although we can extract the \u2018 \u2019 character we need, there\u2019s additional input left in the buffer that could cause problems down the road. \u975e\u6cd5\u8f93\u5165\u7684\u7c7b\u578b We can generally separate input text errors into four types: Input extraction succeeds but the input is meaningless to the program (e.g. entering \u2018k\u2019 as your mathematical operator). Input extraction succeeds but the user enters additional input (e.g. entering \u2018*q hello\u2019 as your mathematical operator). Input extraction fails (e.g. trying to enter \u2018q\u2019 into a numeric input). Input extraction succeeds but the user overflows a numeric value. Thus, to make our programs robust, whenever we ask the user for input, we ideally should determine whether each of the above can possibly occur, and if so, write code to handle those cases. Let\u2019s dig into each of these cases, and how to handle them using std::cin. Error case 1: Extraction succeeds but input is meaningless This is the simplest case. Consider the following execution of the above program: 1 2 3 Enter a double value: 5 Enter one of the following: +, -, *, or /: k Enter a double value: 7 In this case, we asked the user to enter one of four symbols, but they entered \u2018k\u2019 instead. \u2018k\u2019 is a valid character, so std::cin happily extracts it to variable op, and this gets returned to main. But our program wasn\u2019t expecting this to happen, so it doesn\u2019t properly deal with this case (and thus never outputs anything). The solution here is simple: do input validation. This usually consists of 3 steps: Check whether the user\u2019s input was what you were expecting. If so, return the value to the caller. If not, tell the user something went wrong and have them try again. Here\u2019s an updated getOperator() function that does input validation. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 char getOperator () { while ( true ) // Loop until user enters a valid input { std :: cout << \"Enter one of the following: +, -, *, or /: \" ; char operation {}; std :: cin >> operation ; // Check whether the user entered meaningful input switch ( operation ) { case '+' : case '-' : case '*' : case '/' : return operation ; // return it to the caller default : // otherwise tell the user what went wrong std :: cerr << \"Oops, that input is invalid. Please try again. \\n \" ; } } // and try again } COPY As you can see, we\u2019re using a while loop to continuously loop until the user provides valid input. If they don\u2019t, we ask them to try again until they either give us valid input, shutdown the program, or destroy their computer. Error case 2: Extraction succeeds but with extraneous input Consider the following execution of the above program: Enter a double value: 5*7 What do you think happens next? Enter a double value: 5 7 Enter one of the following: +, -, , or /: Enter a double value: 5 * 7 is 35 The program prints the right answer, but the formatting is all messed up. Let\u2019s take a closer look at why. When the user enters \u201c5 7\u201d as input, that input goes into the buffer. Then operator>> extracts the 5 to variable x, leaving \u201c 7\\n\u201d in the buffer. Next, the program prints \u201cEnter one of the following: +, -, , or /:\u201d. However, when the extraction operator was called, it sees \u201c 7\\n\u201d waiting in the buffer to be extracted, so it uses that instead of asking the user for more input. Consequently, it extracts the \u2018*\u2019 character, leaving \u201c7\\n\u201d in the buffer. After asking the user to enter another double value, the \u201c7\u201d in the buffer gets extracted without asking the user. Since the user never had an opportunity to enter additional data and hit enter (causing a newline), the output prompts all run together on the same line. Although the above program works, the execution is messy. It would be better if any extraneous characters entered were simply ignored. Fortunately, it\u2019s easy to ignore characters: 1 std :: cin . ignore ( 100 , '\\n' ); // clear up to 100 characters out of the buffer, or until a '\\n' character is removed COPY This call would remove up to 100 characters, but if the user entered more than 100 characters we\u2019ll get messy output again. To ignore all characters up to the next \u2018\\n\u2019, we can pass std::numeric_limits::max() to std::cin.ignore() . std::numeric_limits::max() returns the largest value that can be stored in a variable of type std::streamsize . Passing this value to std::cin.ignore() causes it to disable the count check. To ignore everything up to and including the next \u2018\\n\u2019 character, we call 1 std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); COPY Because this line is quite long for what it does, it\u2019s handy to wrap it in a function which can be called in place of std::cin.ignore() . 1 2 3 4 5 6 #include // for std::numeric_limits void ignoreLine () { std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); } COPY Since the last character the user entered must be a \u2018\\n\u2019, we can tell std::cin to ignore buffered characters until it finds a newline character (which is removed as well). Let\u2019s update our getDouble() function to ignore any extraneous input: 1 2 3 4 5 6 7 8 double getDouble () { std :: cout << \"Enter a double value: \" ; double x {}; std :: cin >> x ; ignoreLine (); return x ; } COPY Now our program will work as expected, even if we enter \u201c5*7\u201d for the first input -- the 5 will be extracted, and the rest of the characters will be removed from the input buffer. Since the input buffer is now empty, the user will be properly asked for input the next time an extraction operation is performed! \u4f5c\u8005\u6ce8 Some lessons still pass 32767 to std::cin.ignore() . This is a magic number with no special meaning to std::cin.ignore() and should be avoided. If you see such an occurrence, feel free to point it out. Error case 3: Extraction fails Now consider the following execution of our updated calculator program: Enter a double value: a You shouldn\u2019t be surprised that the program doesn\u2019t perform as expected, but how it fails is interesting: 1 2 3 4 Enter a double value: a Enter one of the following: +, -, *, or /: Oops, that input is invalid. Please try again. Enter one of the following: +, -, *, or /: Oops, that input is invalid. Please try again. Enter one of the following: +, -, *, or /: Oops, that input is invalid. Please try again. and that last line keeps printing until the program is closed. This looks pretty similar to the extraneous input case, but it\u2019s a little different. Let\u2019s take a closer look. When the user enters \u2018a\u2019, that character is placed in the buffer. Then operator>> tries to extract \u2018a\u2019 to variable x, which is of type double. Since \u2018a\u2019 can\u2019t be converted to a double, operator>> can\u2019t do the extraction. Two things happen at this point: \u2018a\u2019 is left in the buffer, and std::cin goes into \u201cfailure mode\u201d. Once in \u201cfailure mode\u201d, future requests for input extraction will silently fail. Thus in our calculator program, the output prompts still print, but any requests for further extraction are ignored. This means that instead waiting for us to enter an operation, the input prompt is skipped, and we get stuck in an infinite loop because there is no way to reach one of the valid cases. Fortunately, we can detect whether an extraction has failed and fix it: 1 2 3 4 5 6 if ( std :: cin . fail ()) // has a previous extraction failed? { // yep, so let's handle the failure std :: cin . clear (); // put us back in 'normal' operation mode ignoreLine (); // and remove the bad input } COPY Because std::cin has a Boolean conversion indicating whether the last input succeeded, it\u2019s more idiomatic to write the above as following: 1 2 3 4 5 6 if ( ! std :: cin ) // has a previous extraction failed? { // yep, so let's handle the failure std :: cin . clear (); // put us back in 'normal' operation mode ignoreLine (); // and remove the bad input } COPY That\u2019s it! Let\u2019s integrate that into our getDouble() function: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 double getDouble () { while ( true ) // Loop until user enters a valid input { std :: cout << \"Enter a double value: \" ; double x {}; std :: cin >> x ; if ( ! std :: cin ) // has a previous extraction failed? { // yep, so let's handle the failure std :: cin . clear (); // put us back in 'normal' operation mode ignoreLine (); // and remove the bad input } else // else our extraction succeeded { ignoreLine (); return x ; // so return the value we extracted } } } COPY A failed extraction due to invalid input will cause the variable to be zero-initialized. Zero initialization means the variable is set to 0, 0.0, \u201c\u201d, or whatever value 0 converts to for that type. Error case 4: Extraction succeeds but the user overflows a numeric value Consider the following simple example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include int main () { std :: int16_t x {}; // x is 16 bits, holds from -32768 to 32767 std :: cout << \"Enter a number between -32768 and 32767: \" ; std :: cin >> x ; std :: int16_t y {}; // y is 16 bits, holds from -32768 to 32767 std :: cout << \"Enter another number between -32768 and 32767: \" ; std :: cin >> y ; std :: cout << \"The sum is: \" << x + y << '\\n' ; return 0 ; } COPY What happens if the user enters a number that is too large (e.g. 40000)? Enter a number between -32768 and 32767: 40000 Enter another number between -32768 and 32767: The sum is: 32767 In the above case, std::cin goes immediately into \u201cfailure mode\u201d, but also assigns the closest in-range value to the variable. Consequently, x is left with the assigned value of 32767. Additional inputs are skipped, leaving y with the initialized value of 0. We can handle this kind of error in the same way as a failed extraction. A failed extraction due to invalid input will cause the variable to be zero-initialized. Zero initialization means the variable is set to 0, 0.0, \u201c\u201d, or whatever value 0 converts to for that type. Putting it all together Here\u2019s our example calculator, updated with a few additional bits of error checking: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 #include #include void ignoreLine () { std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); } double getDouble () { while ( true ) // Loop until user enters a valid input { std :: cout << \"Enter a double value: \" ; double x {}; std :: cin >> x ; // Check for failed extraction if ( ! std :: cin ) // has a previous extraction failed? { // yep, so let's handle the failure std :: cin . clear (); // put us back in 'normal' operation mode ignoreLine (); // and remove the bad input std :: cerr << \"Oops, that input is invalid. Please try again. \\n \" ; } else { ignoreLine (); // remove any extraneous input return x ; } } } char getOperator () { while ( true ) // Loop until user enters a valid input { std :: cout << \"Enter one of the following: +, -, *, or /: \" ; char operation {}; std :: cin >> operation ; ignoreLine (); // // remove any extraneous input // Check whether the user entered meaningful input switch ( operation ) { case '+' : case '-' : case '*' : case '/' : return operation ; // return it to the caller default : // otherwise tell the user what went wrong std :: cerr << \"Oops, that input is invalid. Please try again. \\n \" ; } } // and try again } void printResult ( double x , char operation , double y ) { switch ( operation ) { case '+' : std :: cout << x << \" + \" << y << \" is \" << x + y << '\\n' ; break ; case '-' : std :: cout << x << \" - \" << y << \" is \" << x - y << '\\n' ; break ; case '*' : std :: cout << x << \" * \" << y << \" is \" << x * y << '\\n' ; break ; case '/' : std :: cout << x << \" / \" << y << \" is \" << x / y << '\\n' ; break ; default : // Being robust means handling unexpected parameters as well, even though getOperator() guarantees operation is valid in this particular program std :: cerr << \"Something went wrong: printResult() got an invalid operator. \\n \" ; } } int main () { double x { getDouble () }; char operation { getOperator () }; double y { getDouble () }; printResult ( x , operation , y ); return 0 ; } \u5c0f\u7ed3 As you write your programs, consider how users will misuse your program, especially around text input. For each point of text input, consider: Could extraction fail? Could the user enter more input than expected? Could the user enter meaningless input? Could the user overflow an input? You can use if statements and boolean logic to test whether input is expected and meaningful. The following code will clear any extraneous input: 1 std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); The following code will test for and fix failed extractions or overflow: 1 2 3 4 5 6 if ( ! std :: cin ) // has a previous extraction failed or overflowed? { // yep, so let's handle the failure std :: cin . clear (); // put us back in 'normal' operation mode std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // and remove the bad input } Finally, use loops to ask the user to re-enter input if the original input was invalid. \u4f5c\u8005\u6ce8 Input validation is important and useful, but it also tends to make examples more complicated and harder to follow. Accordingly, in future lessons, we will generally not do any kind of input validation unless it\u2019s relevant to something we\u2019re trying to teach.","title":"7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406"},{"location":"7-16-std-cin-and-handling-invalid-input/#stdcin","text":"In order to discuss how std::cin and operator>> can fail, it first helps to know a little bit about how they work. When we use operator>> to get user input and put it into a variable, this is called an \u201cextraction\u201d. The >> operator is accordingly called the extraction operator when used in this context. When the user enters input in response to an extraction operation, that data is placed in a buffer inside of std::cin. A buffer (also called a data buffer) is simply a piece of memory set aside for storing data temporarily while it\u2019s moved from one place to another. In this case, the buffer is used to hold user input while it\u2019s waiting to be extracted to variables. When the extraction operator is used, the following procedure happens: If there is data already in the input buffer, that data is used for extraction. If the input buffer contains no data, the user is asked to input data for extraction (this is the case most of the time). When the user hits enter, a \u2018\\n\u2019 character will be placed in the input buffer. operator>> extracts as much data from the input buffer as it can into the variable (ignoring any leading whitespace characters, such as spaces, tabs, or \u2018\\n\u2019). Any data that can not be extracted is left in the input buffer for the next extraction. Extraction succeeds if at least one character is extracted from the input buffer. Any unextracted input is left in the input buffer for future extractions. For example: 1 2 int x {}; std :: cin >> x ; COPY If the user enters \u201c5a\u201d, 5 will be extracted, converted to an integer, and assigned to variable x. \u201ca\\n\u201d will be left in the input buffer for the next extraction. Extraction fails if the input data does not match the type of the variable being extracted to. For example: 1 2 int x {}; std :: cin >> x ; COPY If the user were to enter \u2018b\u2019, extraction would fail because \u2018b\u2019 can not be extracted to an integer variable.","title":"std::cin\u3001\u7f13\u51b2\u548c\u63d0\u53d6\u64cd\u4f5c"},{"location":"7-16-std-cin-and-handling-invalid-input/#_1","text":"The process of checking whether user input conforms to what the program is expecting is called input validation . There are three basic ways to do input validation: Inline (as the user types) Prevent the user from typing invalid input in the first place. Post-entry (after the user types) Let the user enter whatever they want into a string, then validate whether the string is correct, and if so, convert the string to the final variable format. Let the user enter whatever they want, let std::cin and operator>> try to extract it, and handle the error cases. Some graphical user interfaces and advanced text interfaces will let you validate input as the user enters it (character by character). Generally speaking, the programmer provides a validation function that accepts the input the user has entered so far, and returns true if the input is valid, and false otherwise. This function is called every time the user presses a key. If the validation function returns true, the key the user just pressed is accepted. If the validation function returns false, the character the user just input is discarded (and not shown on the screen). Using this method, you can ensure that any input the user enters is guaranteed to be valid, because any invalid keystrokes are discovered and discarded immediately. Unfortunately, std::cin does not support this style of validation. Since strings do not have any restrictions on what characters can be entered, extraction is guaranteed to succeed (though remember that std::cin stops extracting at the first non-leading whitespace character). Once a string is entered, the program can then parse the string to see if it is valid or not. However, parsing strings and converting string input to other types (e.g. numbers) can be challenging, so this is only done in rare cases. Most often, we let std::cin and the extraction operator do the hard work. Under this method, we let the user enter whatever they want, have std::cin and operator>> try to extract it, and deal with the fallout if it fails. This is the easiest method, and the one we\u2019ll talk more about below.","title":"\u8f93\u5165\u9a8c\u8bc1"},{"location":"7-16-std-cin-and-handling-invalid-input/#_2","text":"Consider the following calculator program that has no error handling: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include double getDouble () { std :: cout << \"Enter a double value: \" ; double x {}; std :: cin >> x ; return x ; } char getOperator () { std :: cout << \"Enter one of the following: +, -, *, or /: \" ; char op {}; std :: cin >> op ; return op ; } void printResult ( double x , char operation , double y ) { switch ( operation ) { case '+' : std :: cout << x << \" + \" << y << \" is \" << x + y << '\\n' ; break ; case '-' : std :: cout << x << \" - \" << y << \" is \" << x - y << '\\n' ; break ; case '*' : std :: cout << x << \" * \" << y << \" is \" << x * y << '\\n' ; break ; case '/' : std :: cout << x << \" / \" << y << \" is \" << x / y << '\\n' ; break ; } } int main () { double x { getDouble () }; char operation { getOperator () }; double y { getDouble () }; printResult ( x , operation , y ); return 0 ; } COPY This simple program asks the user to enter two numbers and a mathematical operator. 1 2 3 4 Enter a double value: 5 Enter one of the following: +, -, *, or /: * Enter a double value: 7 5 * 7 is 35 Now, consider where invalid user input might break this program. First, we ask the user to enter some numbers. What if they enter something other than a number (e.g. \u2018q\u2019)? In this case, extraction will fail. Second, we ask the user to enter one of four possible symbols. What if they enter a character other than one of the symbols we\u2019re expecting? We\u2019ll be able to extract the input, but we don\u2019t currently handle what happens afterward. Third, what if we ask the user to enter a symbol and they enter a string like \u201c q hello\u201d. Although we can extract the \u2018 \u2019 character we need, there\u2019s additional input left in the buffer that could cause problems down the road.","title":"\u4f8b\u7a0b"},{"location":"7-16-std-cin-and-handling-invalid-input/#_3","text":"We can generally separate input text errors into four types: Input extraction succeeds but the input is meaningless to the program (e.g. entering \u2018k\u2019 as your mathematical operator). Input extraction succeeds but the user enters additional input (e.g. entering \u2018*q hello\u2019 as your mathematical operator). Input extraction fails (e.g. trying to enter \u2018q\u2019 into a numeric input). Input extraction succeeds but the user overflows a numeric value. Thus, to make our programs robust, whenever we ask the user for input, we ideally should determine whether each of the above can possibly occur, and if so, write code to handle those cases. Let\u2019s dig into each of these cases, and how to handle them using std::cin.","title":"\u975e\u6cd5\u8f93\u5165\u7684\u7c7b\u578b"},{"location":"7-16-std-cin-and-handling-invalid-input/#error-case-1-extraction-succeeds-but-input-is-meaningless","text":"This is the simplest case. Consider the following execution of the above program: 1 2 3 Enter a double value: 5 Enter one of the following: +, -, *, or /: k Enter a double value: 7 In this case, we asked the user to enter one of four symbols, but they entered \u2018k\u2019 instead. \u2018k\u2019 is a valid character, so std::cin happily extracts it to variable op, and this gets returned to main. But our program wasn\u2019t expecting this to happen, so it doesn\u2019t properly deal with this case (and thus never outputs anything). The solution here is simple: do input validation. This usually consists of 3 steps: Check whether the user\u2019s input was what you were expecting. If so, return the value to the caller. If not, tell the user something went wrong and have them try again. Here\u2019s an updated getOperator() function that does input validation. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 char getOperator () { while ( true ) // Loop until user enters a valid input { std :: cout << \"Enter one of the following: +, -, *, or /: \" ; char operation {}; std :: cin >> operation ; // Check whether the user entered meaningful input switch ( operation ) { case '+' : case '-' : case '*' : case '/' : return operation ; // return it to the caller default : // otherwise tell the user what went wrong std :: cerr << \"Oops, that input is invalid. Please try again. \\n \" ; } } // and try again } COPY As you can see, we\u2019re using a while loop to continuously loop until the user provides valid input. If they don\u2019t, we ask them to try again until they either give us valid input, shutdown the program, or destroy their computer.","title":"Error case 1: Extraction succeeds but input is meaningless"},{"location":"7-16-std-cin-and-handling-invalid-input/#error-case-2-extraction-succeeds-but-with-extraneous-input","text":"Consider the following execution of the above program: Enter a double value: 5*7 What do you think happens next? Enter a double value: 5 7 Enter one of the following: +, -, , or /: Enter a double value: 5 * 7 is 35 The program prints the right answer, but the formatting is all messed up. Let\u2019s take a closer look at why. When the user enters \u201c5 7\u201d as input, that input goes into the buffer. Then operator>> extracts the 5 to variable x, leaving \u201c 7\\n\u201d in the buffer. Next, the program prints \u201cEnter one of the following: +, -, , or /:\u201d. However, when the extraction operator was called, it sees \u201c 7\\n\u201d waiting in the buffer to be extracted, so it uses that instead of asking the user for more input. Consequently, it extracts the \u2018*\u2019 character, leaving \u201c7\\n\u201d in the buffer. After asking the user to enter another double value, the \u201c7\u201d in the buffer gets extracted without asking the user. Since the user never had an opportunity to enter additional data and hit enter (causing a newline), the output prompts all run together on the same line. Although the above program works, the execution is messy. It would be better if any extraneous characters entered were simply ignored. Fortunately, it\u2019s easy to ignore characters: 1 std :: cin . ignore ( 100 , '\\n' ); // clear up to 100 characters out of the buffer, or until a '\\n' character is removed COPY This call would remove up to 100 characters, but if the user entered more than 100 characters we\u2019ll get messy output again. To ignore all characters up to the next \u2018\\n\u2019, we can pass std::numeric_limits::max() to std::cin.ignore() . std::numeric_limits::max() returns the largest value that can be stored in a variable of type std::streamsize . Passing this value to std::cin.ignore() causes it to disable the count check. To ignore everything up to and including the next \u2018\\n\u2019 character, we call 1 std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); COPY Because this line is quite long for what it does, it\u2019s handy to wrap it in a function which can be called in place of std::cin.ignore() . 1 2 3 4 5 6 #include // for std::numeric_limits void ignoreLine () { std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); } COPY Since the last character the user entered must be a \u2018\\n\u2019, we can tell std::cin to ignore buffered characters until it finds a newline character (which is removed as well). Let\u2019s update our getDouble() function to ignore any extraneous input: 1 2 3 4 5 6 7 8 double getDouble () { std :: cout << \"Enter a double value: \" ; double x {}; std :: cin >> x ; ignoreLine (); return x ; } COPY Now our program will work as expected, even if we enter \u201c5*7\u201d for the first input -- the 5 will be extracted, and the rest of the characters will be removed from the input buffer. Since the input buffer is now empty, the user will be properly asked for input the next time an extraction operation is performed! \u4f5c\u8005\u6ce8 Some lessons still pass 32767 to std::cin.ignore() . This is a magic number with no special meaning to std::cin.ignore() and should be avoided. If you see such an occurrence, feel free to point it out.","title":"Error case 2: Extraction succeeds but with extraneous input"},{"location":"7-16-std-cin-and-handling-invalid-input/#error-case-3-extraction-fails","text":"Now consider the following execution of our updated calculator program: Enter a double value: a You shouldn\u2019t be surprised that the program doesn\u2019t perform as expected, but how it fails is interesting: 1 2 3 4 Enter a double value: a Enter one of the following: +, -, *, or /: Oops, that input is invalid. Please try again. Enter one of the following: +, -, *, or /: Oops, that input is invalid. Please try again. Enter one of the following: +, -, *, or /: Oops, that input is invalid. Please try again. and that last line keeps printing until the program is closed. This looks pretty similar to the extraneous input case, but it\u2019s a little different. Let\u2019s take a closer look. When the user enters \u2018a\u2019, that character is placed in the buffer. Then operator>> tries to extract \u2018a\u2019 to variable x, which is of type double. Since \u2018a\u2019 can\u2019t be converted to a double, operator>> can\u2019t do the extraction. Two things happen at this point: \u2018a\u2019 is left in the buffer, and std::cin goes into \u201cfailure mode\u201d. Once in \u201cfailure mode\u201d, future requests for input extraction will silently fail. Thus in our calculator program, the output prompts still print, but any requests for further extraction are ignored. This means that instead waiting for us to enter an operation, the input prompt is skipped, and we get stuck in an infinite loop because there is no way to reach one of the valid cases. Fortunately, we can detect whether an extraction has failed and fix it: 1 2 3 4 5 6 if ( std :: cin . fail ()) // has a previous extraction failed? { // yep, so let's handle the failure std :: cin . clear (); // put us back in 'normal' operation mode ignoreLine (); // and remove the bad input } COPY Because std::cin has a Boolean conversion indicating whether the last input succeeded, it\u2019s more idiomatic to write the above as following: 1 2 3 4 5 6 if ( ! std :: cin ) // has a previous extraction failed? { // yep, so let's handle the failure std :: cin . clear (); // put us back in 'normal' operation mode ignoreLine (); // and remove the bad input } COPY That\u2019s it! Let\u2019s integrate that into our getDouble() function: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 double getDouble () { while ( true ) // Loop until user enters a valid input { std :: cout << \"Enter a double value: \" ; double x {}; std :: cin >> x ; if ( ! std :: cin ) // has a previous extraction failed? { // yep, so let's handle the failure std :: cin . clear (); // put us back in 'normal' operation mode ignoreLine (); // and remove the bad input } else // else our extraction succeeded { ignoreLine (); return x ; // so return the value we extracted } } } COPY A failed extraction due to invalid input will cause the variable to be zero-initialized. Zero initialization means the variable is set to 0, 0.0, \u201c\u201d, or whatever value 0 converts to for that type.","title":"Error case 3: Extraction fails"},{"location":"7-16-std-cin-and-handling-invalid-input/#error-case-4-extraction-succeeds-but-the-user-overflows-a-numeric-value","text":"Consider the following simple example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include int main () { std :: int16_t x {}; // x is 16 bits, holds from -32768 to 32767 std :: cout << \"Enter a number between -32768 and 32767: \" ; std :: cin >> x ; std :: int16_t y {}; // y is 16 bits, holds from -32768 to 32767 std :: cout << \"Enter another number between -32768 and 32767: \" ; std :: cin >> y ; std :: cout << \"The sum is: \" << x + y << '\\n' ; return 0 ; } COPY What happens if the user enters a number that is too large (e.g. 40000)? Enter a number between -32768 and 32767: 40000 Enter another number between -32768 and 32767: The sum is: 32767 In the above case, std::cin goes immediately into \u201cfailure mode\u201d, but also assigns the closest in-range value to the variable. Consequently, x is left with the assigned value of 32767. Additional inputs are skipped, leaving y with the initialized value of 0. We can handle this kind of error in the same way as a failed extraction. A failed extraction due to invalid input will cause the variable to be zero-initialized. Zero initialization means the variable is set to 0, 0.0, \u201c\u201d, or whatever value 0 converts to for that type.","title":"Error case 4: Extraction succeeds but the user overflows a numeric value"},{"location":"7-16-std-cin-and-handling-invalid-input/#putting-it-all-together","text":"Here\u2019s our example calculator, updated with a few additional bits of error checking: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 #include #include void ignoreLine () { std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); } double getDouble () { while ( true ) // Loop until user enters a valid input { std :: cout << \"Enter a double value: \" ; double x {}; std :: cin >> x ; // Check for failed extraction if ( ! std :: cin ) // has a previous extraction failed? { // yep, so let's handle the failure std :: cin . clear (); // put us back in 'normal' operation mode ignoreLine (); // and remove the bad input std :: cerr << \"Oops, that input is invalid. Please try again. \\n \" ; } else { ignoreLine (); // remove any extraneous input return x ; } } } char getOperator () { while ( true ) // Loop until user enters a valid input { std :: cout << \"Enter one of the following: +, -, *, or /: \" ; char operation {}; std :: cin >> operation ; ignoreLine (); // // remove any extraneous input // Check whether the user entered meaningful input switch ( operation ) { case '+' : case '-' : case '*' : case '/' : return operation ; // return it to the caller default : // otherwise tell the user what went wrong std :: cerr << \"Oops, that input is invalid. Please try again. \\n \" ; } } // and try again } void printResult ( double x , char operation , double y ) { switch ( operation ) { case '+' : std :: cout << x << \" + \" << y << \" is \" << x + y << '\\n' ; break ; case '-' : std :: cout << x << \" - \" << y << \" is \" << x - y << '\\n' ; break ; case '*' : std :: cout << x << \" * \" << y << \" is \" << x * y << '\\n' ; break ; case '/' : std :: cout << x << \" / \" << y << \" is \" << x / y << '\\n' ; break ; default : // Being robust means handling unexpected parameters as well, even though getOperator() guarantees operation is valid in this particular program std :: cerr << \"Something went wrong: printResult() got an invalid operator. \\n \" ; } } int main () { double x { getDouble () }; char operation { getOperator () }; double y { getDouble () }; printResult ( x , operation , y ); return 0 ; }","title":"Putting it all together"},{"location":"7-16-std-cin-and-handling-invalid-input/#_4","text":"As you write your programs, consider how users will misuse your program, especially around text input. For each point of text input, consider: Could extraction fail? Could the user enter more input than expected? Could the user enter meaningless input? Could the user overflow an input? You can use if statements and boolean logic to test whether input is expected and meaningful. The following code will clear any extraneous input: 1 std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); The following code will test for and fix failed extractions or overflow: 1 2 3 4 5 6 if ( ! std :: cin ) // has a previous extraction failed or overflowed? { // yep, so let's handle the failure std :: cin . clear (); // put us back in 'normal' operation mode std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // and remove the bad input } Finally, use loops to ask the user to re-enter input if the original input was invalid. \u4f5c\u8005\u6ce8 Input validation is important and useful, but it also tends to make examples more complicated and harder to follow. Accordingly, in future lessons, we will generally not do any kind of input validation unless it\u2019s relevant to something we\u2019re trying to teach.","title":"\u5c0f\u7ed3"},{"location":"7-17-assert-and-static-assert/","tags":["assert","static_assert","C++17"],"text":"In a function that takes parameters, the caller may be able to pass in arguments that are syntactically valid but semantically meaningless. For example, in the previous lesson ( 7.15 - \u53d1\u73b0\u9519\u8bef\u548c\u5904\u7406\u9519\u8bef ), we showed the following sample function: 1 2 3 4 5 6 7 void printDivision ( int x , int y ) { if ( y != 0 ) std :: cout << static_cast < double > ( x ) / y ; else std :: cerr << \"Error: Could not divide by zero \\n \" ; } This function does an explicit check to see if y is 0 , since dividing by zero is a semantic error and will cause the program to crash if executed. In the prior lesson, we discussed a couple of ways to deal with such problems, including halting the program, or skipping the offending statements. Both of those options are problematic though. If a program skips statements due to an error, then it is essentially failing silently. Especially while we are writing and debugging programs, silent failures are bad, because they obscure real problems. Even if we print an error message, that error message may be lost among the other program output, and it may be non-obvious where the error message is being generated or how the conditions that triggered the error message occurred. Some functions may be called tens or hundreds of times, and if only one of those cases is generating a problem, it can be hard to know which one. If the program terminates (via std::exit ) then we will have lost our call stack and any debugging information that might help us isolate the problem. std::abort is a better option for such cases, as typically the developer will be given the option to start debugging at the point where the program aborted. \u524d\u7f6e\u6761\u4ef6\u3001\u4e0d\u53d8\u91cf\u548c\u540e\u7f6e\u6761\u4ef6 In programming, a precondition is any condition that must always be true prior to the execution of component of code. Our check of y is a precondition that ensures y has a valid value before the function continues. It\u2019s more common for functions with preconditions to be written like this: 1 2 3 4 5 6 7 8 9 10 void printDivision ( int x , int y ) { if ( y == 0 ) { std :: cerr << \"Error: Could not divide by zero \\n \" ; return ; } std :: cout << static_cast < double > ( x ) / y ; } COPY An invariant is a condition that must be true while some component is executing. Similarly, a postcondition is something that must be true after the execution of some component of code. Our function doesn\u2019t have any postconditions. \u65ad\u8a00 Using a conditional statement to detect an invalid parameter (or to validate some other kind of assumption), along with printing an error message and terminating the program, is such a common response to problems that C++ provides a shortcut method for doing this. An assertion is an expression that will be true unless there is a bug in the program. If the expression evaluates to true , the assertion statement does nothing. If the conditional expression evaluates to false , an error message is displayed and the program is terminated (via std::abort ). This error message typically contains the expression that failed as text, along with the name of the code file and the line number of the assertion. This makes it very easy to tell not only what the problem was, but where in the code the problem occurred. This can help with debugging efforts immensely. In C++, runtime assertions are implemented via the assert preprocessor macro, which lives in the header. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include // for assert() #include // for std::sqrt #include double calculateTimeUntilObjectHitsGround ( double initialHeight , double gravity ) { assert ( gravity > 0.0 ); // The object won't reach the ground unless there is positive gravity. if ( initialHeight <= 0.0 ) { // The object is already on the ground. Or buried. return 0.0 ; } return std :: sqrt (( 2.0 * initialHeight ) / gravity ); } int main () { std :: cout << \"Took \" << calculateTimeUntilObjectHitsGround ( 100.0 , -9.8 ) << \" second(s) \\n \" ; return 0 ; } COPY When the program calls calculateTimeUntilObjectHitsGround(100.0, -9.8) , assert(gravity > 0.0) will evaluate to false , which will trigger the assert. That will print a message similar to this: 1 dropsimulator: src/main.cpp:6: double calculateTimeUntilObjectHitsGround(double, double): Assertion 'gravity > 0.0' failed. The actual message varies depending on which compiler you use. Although asserts are most often used to validate function parameters, they can be used anywhere you would like to validate that something is true. Although we told you previously to avoid preprocessor macros, asserts are one of the few preprocessor macros that are considered acceptable to use. We encourage you to use assert statements liberally throughout your code. \u4f7f\u65ad\u8a00\u8bed\u53e5\u66f4\u5177\u63cf\u8ff0\u6027 Sometimes assert expressions aren\u2019t very descriptive. Consider the following statement: 1 assert ( found ); COPY If this assert is triggered, the assert will say: 1 Assertion failed: found, file C:\\\\VCProjects\\\\Test.cpp, line 34 What does this even mean? Clearly found was false (since the assert triggered), but what wasn\u2019t found? You\u2019d have to go look at the code to determine that. Fortunately, there\u2019s a little trick you can use to make your assert statements more descriptive. Simply add a string literal joined by a logical AND: 1 assert ( found && \"Car could not be found in database\" ); COPY Here\u2019s why this works: A string literal always evaluates to Boolean true . So if found is false , false && true is false . If found is true , true && true is true . Thus, logical AND-ing a string literal doesn\u2019t impact the evaluation of the assert. However, when the assert triggers, the string literal will be included in the assert message: Assertion failed: found && \"Car could not be found in database\", file C:\\VCProjects\\Test.cpp, line 34 That gives you some additional context as to what went wrong. \u65ad\u8a00 vs \u9519\u8bef\u5904\u7406 Assertions and error handling are similar enough that their purposes can be confused, so let\u2019s clarify: The goal of an assertion is to catch programming errors by documenting something that should never happen. If that thing does happen, then the programmer made an error somewhere, and that error can be identified and fixed. Assertions do not allow recovery from errors (after all, if something should never happen, there\u2019s no need to recover from it), and the program will not produce a friendly error message. On the other hand, error handling is designed to gracefully handle cases that could happen (however rarely) in release configurations. These may or may not be recoverable, but one should always assume a user of the program may encounter them. \u6700\u4f73\u5b9e\u8df5 Use assertions to document cases that should be logically impossible. Assertions are also sometimes used to document cases that were not implemented because they were not needed at the time the programmer wrote the code: 1 2 // Assert with a message, covered in the next section assert ( moved && \"Need to handle case where student was just moved to another classroom\" ); COPY That way, if a future user of the code does encounter a situation where this case is needed, the code will fail with a useful error message, and the programmer can then determine how to implement that case. NDEBUG The assert macro comes with a small performance cost that is incurred each time the assert condition is checked. Furthermore, asserts should (ideally) never be encountered in production code (because your code should already be thoroughly tested). Consequently, many developers prefer that asserts are only active in debug builds. C++ comes with a way to turn off asserts in production code. If the macro NDEBUG is defined, the assert macro gets disabled. Some IDEs set NDEBUG by default as part of the project settings for release configurations. For example, in Visual Studio, the following preprocessor definitions are set at the project level: WIN32;NDEBUG;_CONSOLE . If you\u2019re using Visual Studio and want your asserts to trigger in release builds, you\u2019ll need to remove NDEBUG from this setting. If you\u2019re using an IDE or build system that doesn\u2019t automatically define NDEBUG in release configuration, you will need to add it in the project or compilation settings manually. \u65ad\u8a00\u7684\u9650\u5236\u548c\u63d0\u9192 There are a few pitfalls and limitations to asserts. First, the assert itself can have a bug. If this happens, the assert will either report an error where none exists, or fail to report a bug where one does exist. Second, your asserts should have no side effects -- that is, the program should run the same with and without the assert. Otherwise, what you are testing in a debug configuration will not be the same as in a release configuration (assuming you ship with NDEBUG). Also note that the abort() function terminates the program immediately, without a chance to do any further cleanup (e.g. close a file or database). Because of this, asserts should be used only in cases where corruption isn\u2019t likely to occur if the program terminates unexpectedly. static_assert C++ also has another type of assert called static_assert . A static_assert is an assertion that is checked at compile-time rather than at runtime, with a failing static_assert causing a compile error. Unlike assert, which is declared in the header, static_assert is a keyword, so no header needs to be included to use it. A static_assert takes the following form: 1 static_assert ( condition , diagnostic_message ) If the condition is not true, the diagnostic message is printed. Here\u2019s an example of using static_assert to ensure types have a certain size: 1 2 3 4 5 6 7 static_assert ( sizeof ( long ) == 8 , \"long must be 8 bytes\" ); static_assert ( sizeof ( int ) == 4 , \"int must be 4 bytes\" ); int main () { return 0 ; } COPY On the author\u2019s machine, when compiled, the compiler errors: 1 1>c:\\consoleapplication1\\main.cpp(19): error C2338: long must be 8 bytes Because static_assert is evaluated by the compiler, the condition must be able to be evaluated at compile time. Also, unlike normal assert (which is evaluated at runtime), static_assert can be placed anywhere in the code file (even in the global namespace). Prior to C++17, the diagnostic message must be supplied as the second parameter. Since C++17, providing a diagnostic message is optional. \u5728\u63a5\u6536\u53c2\u6570\u7684\u51fd\u6570\u4e2d\uff0c\u8c03\u7528\u8005\u53ef\u80fd\u4f1a\u4f20\u9012\u8bed\u6cd5\u4e0a\u6709\u6548\u4f46\u8bed\u4e49\u4e0a\u65e0\u610f\u4e49\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u5728\u4e0a\u4e00\u8bfe\u4e2d\uff08 7.15 - \u53d1\u73b0\u9519\u8bef\u548c\u5904\u7406\u9519\u8bef \uff09\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u4ee5\u4e0b\u793a\u4f8b\u51fd\u6570\uff1a 1 2 3 4 5 6 7 void printDivision ( int x , int y ) { if ( y != 0 ) std :: cout << static_cast ( x ) / y ; else std :: cerr << \"Error: Could not divide by zero \\n \" ; } \u8be5\u51fd\u6570\u663e\u5f0f\u68c0\u67e5 y \u662f\u5426\u4e3a 0 \uff0c\u56e0\u4e3a\u9664\u4ee5\u96f6\u662f\u8bed\u4e49\u9519\u8bef\uff0c\u5982\u679c\u6267\u884c\u5c06\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002 \u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u5904\u7406\u6b64\u7c7b\u95ee\u9898\u7684\u51e0\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u505c\u6b62\u7a0b\u5e8f\u6216\u8df3\u8fc7\u6709\u95ee\u9898\u7684\u8bed\u53e5\u3002 \u4f46\u8fd9\u4e24\u79cd\u9009\u9879\u90fd\u6709\u95ee\u9898\u3002\u5982\u679c\u7a0b\u5e8f\u7531\u4e8e\u9519\u8bef\u800c\u8df3\u8fc7\u8bed\u53e5\uff0c\u5219\u5b9e\u9645\u4e0a\u662f\u5728\u9ed8\u9ed8\u5730\u5931\u8d25\u3002\u5c24\u5176\u662f\u5728\u7f16\u5199\u548c\u8c03\u8bd5\u7a0b\u5e8f\u65f6\uff0c\u9759\u9ed8\u5931\u8d25\u662f\u4e0d\u597d\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u4f1a\u63a9\u76d6\u771f\u6b63\u7684\u95ee\u9898\u3002\u5373\u4f7f\u6211\u4eec\u6253\u5370\u9519\u8bef\u6d88\u606f\uff0c\u8be5\u9519\u8bef\u6d88\u606f\u4e5f\u53ef\u80fd\u4f1a\u5728\u5176\u4ed6\u7a0b\u5e8f\u8f93\u51fa\u4e2d\u4e22\u5931\uff0c\u5e76\u4e14\u53ef\u80fd\u4e0d\u660e\u663e\u5730\u751f\u6210\u9519\u8bef\u6d88\u606f\u6216\u89e6\u53d1\u9519\u8bef\u6d88\u606f\u7684\u6761\u4ef6\u5728\u54ea\u91cc\u3002\u6709\u4e9b\u51fd\u6570\u53ef\u80fd\u4f1a\u88ab\u8c03\u7528\u5341\u6b21\u6216\u51e0\u767e\u6b21\uff0c\u5982\u679c\u53ea\u6709\u5176\u4e2d\u4e00\u4e2a\u60c5\u51b5\u751f\u6210\u95ee\u9898\uff0c\u90a3\u4e48\u5f88\u96be\u77e5\u9053\u662f\u54ea\u4e2a\u60c5\u51b5\u3002 \u5982\u679c\u7a0b\u5e8f\u7ec8\u6b62\uff08\u901a\u8fc7 std::exit \uff09\uff0c\u5219\u6211\u4eec\u5c06\u5931\u53bb\u8c03\u7528\u5806\u6808\u548c\u4efb\u4f55\u53ef\u80fd\u5e2e\u52a9\u6211\u4eec\u9694\u79bb\u95ee\u9898\u7684\u8c03\u8bd5\u4fe1\u606f\u3002 std::abort \u662f\u8fd9\u79cd\u60c5\u51b5\u7684\u66f4\u597d\u9009\u62e9\uff0c\u56e0\u4e3a\u901a\u5e38\u5f00\u53d1\u4eba\u5458\u5c06\u6709\u9009\u62e9\u5728\u7a0b\u5e8f\u4e2d\u6b62\u7684\u70b9\u5f00\u59cb\u8c03\u8bd5\u3002 \u524d\u7f6e\u6761\u4ef6\u3001\u4e0d\u53d8\u91cf\u548c\u540e\u7f6e\u6761\u4ef6 \u5728\u7f16\u7a0b\u4e2d\uff0c\u524d\u7f6e\u6761\u4ef6\u662f\u6307\u5728\u4ee3\u7801\u7ec4\u4ef6\u6267\u884c\u4e4b\u524d\u5fc5\u987b\u59cb\u7ec8\u4e3a\u771f\u7684\u4efb\u4f55\u6761\u4ef6\u3002\u6211\u4eec\u5bf9 y \u7684\u68c0\u67e5\u662f\u786e\u4fdd\u51fd\u6570\u7ee7\u7eed\u4e4b\u524d y \u5177\u6709\u6709\u6548\u503c\u7684\u524d\u7f6e\u6761\u4ef6\u3002 \u66f4\u5e38\u89c1\u7684\u662f\uff0c\u5177\u6709\u524d\u7f6e\u6761\u4ef6\u7684\u51fd\u6570\u7f16\u5199\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 void printDivision ( int x , int y ) { if ( y == 0 ) { std :: cerr << \"Error: Could not divide by zero \\n \" ; return ; } std :: cout << static_cast ( x ) / y ; } \u4e0d\u53d8\u91cf\u662f\u6307\u67d0\u4e2a\u7ec4\u4ef6\u6267\u884c\u65f6\u5fc5\u987b\u4e3a\u771f\u7684\u6761\u4ef6\u3002 \u7c7b\u4f3c\u5730\uff0c\u540e\u7f6e\u6761\u4ef6\u662f\u6307\u67d0\u4e2a\u4ee3\u7801\u7ec4\u4ef6\u6267\u884c\u540e\u5fc5\u987b\u4e3a\u771f\u7684\u6761\u4ef6\u3002\u6211\u4eec\u7684\u51fd\u6570\u6ca1\u6709\u4efb\u4f55\u540e\u7f6e\u6761\u4ef6\u3002 \u65ad\u8a00 \u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u68c0\u6d4b\u65e0\u6548\u53c2\u6570\uff08\u6216\u9a8c\u8bc1\u67d0\u79cd\u5176\u4ed6\u7c7b\u578b\u7684\u5047\u8bbe\uff09\uff0c\u5e76\u6253\u5370\u9519\u8bef\u6d88\u606f\u5e76\u7ec8\u6b62\u7a0b\u5e8f\u662f\u5bf9\u95ee\u9898\u7684\u5e38\u89c1\u54cd\u5e94\uff0c\u56e0\u6b64C++\u63d0\u4f9b\u4e86\u4e00\u79cd\u5feb\u6377\u65b9\u6cd5\u6765\u6267\u884c\u6b64\u64cd\u4f5c\u3002 \u65ad\u8a00\u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u9664\u975e\u7a0b\u5e8f\u4e2d\u5b58\u5728\u9519\u8bef\uff0c\u5426\u5219\u8be5\u8868\u8fbe\u5f0f\u5c06\u4e3a\u771f\u3002\u5982\u679c\u8868\u8fbe\u5f0f\u7684\u503c\u4e3a true \uff0c\u5219\u65ad\u8a00\u8bed\u53e5\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\u3002\u5982\u679c\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u503c\u4e3a false \uff0c\u5219\u663e\u793a\u9519\u8bef\u6d88\u606f\u5e76\u901a\u8fc7 std::abort \u7ec8\u6b62\u7a0b\u5e8f\u3002\u6b64\u9519\u8bef\u6d88\u606f\u901a\u5e38\u5305\u542b\u4ee5\u6587\u672c\u5f62\u5f0f\u5931\u8d25\u7684\u8868\u8fbe\u5f0f\uff0c\u4ee5\u53ca\u65ad\u8a00\u7684\u4ee3\u7801\u6587\u4ef6\u7684\u540d\u79f0\u548c\u884c\u53f7\u3002\u8fd9\u4f7f\u5f97\u4e0d\u4ec5\u53ef\u4ee5\u786e\u5b9a\u95ee\u9898\u6240\u5728\uff0c\u8fd8\u53ef\u4ee5\u786e\u5b9a\u95ee\u9898\u53d1\u751f\u7684\u4ee3\u7801\u4f4d\u7f6e\u3002\u8fd9\u53ef\u4ee5\u6781\u5927\u5730\u5e2e\u52a9\u8c03\u8bd5\u5de5\u4f5c\u3002 \u5728C++\u4e2d\uff0c\u8fd0\u884c\u65f6\u65ad\u8a00\u662f\u901a\u8fc7assert\u9884\u5904\u7406\u5b8f\u5b9e\u73b0\u7684\uff0c\u8be5\u5b8f\u4f4d\u4e8e\u5934\u6587\u4ef6\u4e2d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include // for assert() #include // for std::sqrt #include double calculateTimeUntilObjectHitsGround ( double initialHeight , double gravity ) { assert ( gravity > 0.0 ); // The object won't reach the ground unless there is positive gravity. if ( initialHeight <= 0.0 ) { // The object is already on the ground. Or buried. return 0.0 ; } return std :: sqrt (( 2.0 * initialHeight ) / gravity ); } int main () { std :: cout << \"Took \" << calculateTimeUntilObjectHitsGround ( 100.0 , -9.8 ) << \" second(s) \\n \" ; return 0 ; } COPY \u5f53\u7a0b\u5e8f\u8c03\u7528 calculateTimeUntilObjectHitsGround(100.0, -9.8) \u65f6\uff0c assert(gravity > 0.0) \u5c06\u8ba1\u7b97\u4e3a false \uff0c\u8fd9\u5c06\u89e6\u53d1\u65ad\u8a00\u3002\u8fd9\u5c06\u6253\u5370\u7c7b\u4f3c\u4e8e\u4ee5\u4e0b\u5185\u5bb9\u7684\u6d88\u606f\uff1a 1 dropsimulator: src/main.cpp:6: double calculateTimeUntilObjectHitsGround(double, double): Assertion 'gravity > 0.0' failed. \u5b9e\u9645\u6d88\u606f\u56e0\u4f7f\u7528\u7684\u7f16\u8bd1\u5668\u800c\u5f02\u3002 \u5c3d\u7ba1\u65ad\u8a00\u6700\u5e38\u7528\u4e8e\u9a8c\u8bc1\u51fd\u6570\u53c2\u6570\uff0c\u4f46\u5b83\u4eec\u53ef\u4ee5\u7528\u4e8e\u4efb\u4f55\u60a8\u60f3\u8981\u9a8c\u8bc1\u7684\u5185\u5bb9\u3002 \u5c3d\u7ba1\u6211\u4eec\u4e4b\u524d\u544a\u8bc9\u8fc7\u60a8\u8981\u907f\u514d\u9884\u5904\u7406\u5b8f\uff0c\u4f46\u65ad\u8a00\u662f\u5c11\u6570\u88ab\u8ba4\u4e3a\u53ef\u4ee5\u4f7f\u7528\u7684\u9884\u5904\u7406\u5b8f\u4e4b\u4e00\u3002\u6211\u4eec\u9f13\u52b1\u60a8\u5728\u4ee3\u7801\u4e2d\u5927\u91cf\u4f7f\u7528assert\u8bed\u53e5\u3002 \u4f7f\u65ad\u8a00\u8bed\u53e5\u66f4\u5177\u63cf\u8ff0\u6027 \u6709\u65f6\uff0c\u65ad\u8a00\u8868\u8fbe\u5f0f\u7684\u63cf\u8ff0\u6027\u5e76\u4e0d\u597d\uff0c\u4f8b\u5982\u4e0b\u9762\u7684\u8bed\u53e5\uff1a 1 assert ( found ); \u5982\u679c\u89e6\u53d1\u6b64\u65ad\u8a00\uff0c\u5219\u4f1a\u663e\u793a\uff1a 1 Assertion failed: found, file C:\\\\VCProjects\\\\Test.cpp, line 34 \u8fd9\u662f\u4ec0\u4e48\u610f\u601d\uff1f\u663e\u7136\uff0c found \u662f false \uff08\u56e0\u4e3a\u65ad\u8a00\u89e6\u53d1\u4e86\uff09\uff0c\u81f3\u4e8e\u662f\u4ec0\u4e48\u6ca1\u627e\u5230\uff1f\u4f60\u5fc5\u987b\u67e5\u770b\u4ee3\u7801\u624d\u80fd\u786e\u5b9a\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5c0f\u6280\u5de7\u4f7f assert \u8bed\u53e5\u66f4\u5177\u63cf\u8ff0\u6027\u3002\u53ea\u9700\u6dfb\u52a0\u4e00\u4e2a\u903b\u8f91AND\u8fde\u63a5\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff1a 1 assert ( found && \"Car could not be found in database\" ); \u8fd9\u662f\u4e3a\u4ec0\u4e48\u5b83\u6709\u6548\u7684\u539f\u56e0\uff1a\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u59cb\u7ec8\u8ba1\u7b97\u4e3a\u5e03\u5c14\u503c true \u3002\u56e0\u6b64\uff0c\u5982\u679c found \u4e3a false \uff0c\u5219 false && true \u4e3a false \u3002\u5982\u679c found \u4e3a true \uff0c\u5219 true && true \u4e3a true \u3002\u56e0\u6b64\uff0c\u5728\u903b\u8f91AND\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u4e0d\u4f1a\u5f71\u54cd\u65ad\u8a00\u7684\u8bc4\u4f30\u3002 \u4f46\u662f\uff0c\u5f53\u65ad\u8a00\u89e6\u53d1\u65f6\uff0c\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u5c06\u5305\u542b\u5728\u65ad\u8a00\u6d88\u606f\u4e2d\uff1a Assertion failed: found && \"Car could not be found in database\", file C:\\\\VCProjects\\\\Test.cpp, line 34 \u8fd9\u4e3a\u4f60\u63d0\u4f9b\u4e86\u6709\u5173\u51fa\u4e86\u4ec0\u4e48\u95ee\u9898\u7684\u5176\u4ed6\u4e0a\u4e0b\u6587\u3002 \u65ad\u8a00\u4e0e\u9519\u8bef\u5904\u7406 \u65ad\u8a00\u548c\u9519\u8bef\u5904\u7406\u975e\u5e38\u76f8\u4f3c\uff0c\u4ee5\u81f3\u4e8e\u5b83\u4eec\u7684\u76ee\u7684\u53ef\u80fd\u4f1a\u6df7\u6dc6\uff0c\u56e0\u6b64\u8ba9\u6211\u4eec\u6f84\u6e05\uff1a \u65ad\u8a00\u7684\u76ee\u6807\u662f\u901a\u8fc7\u8bb0\u5f55\u6c38\u8fdc\u4e0d\u5e94\u53d1\u751f\u7684\u4e8b\u60c5\u6765\u6355\u83b7\u7f16\u7a0b\u9519\u8bef\u3002\u5982\u679c\u53d1\u751f\u4e86\u90a3\u4ef6\u4e8b\uff0c\u90a3\u4e48\u7a0b\u5e8f\u5458\u5728\u67d0\u4e2a\u5730\u65b9\u72af\u4e86\u9519\u8bef\uff0c\u5e76\u4e14\u53ef\u4ee5\u786e\u5b9a\u5e76\u4fee\u590d\u8be5\u9519\u8bef\u3002\u65ad\u8a00\u4e0d\u5141\u8bb8\u4ece\u9519\u8bef\u4e2d\u6062\u590d\uff08\u6bd5\u7adf\uff0c\u5982\u679c\u67d0\u4ef6\u4e8b\u60c5\u6c38\u8fdc\u4e0d\u5e94\u8be5\u53d1\u751f\uff0c\u90a3\u4e48\u65e0\u9700\u4ece\u4e2d\u6062\u590d\uff09\uff0c\u5e76\u4e14\u7a0b\u5e8f\u4e0d\u4f1a\u751f\u6210\u53cb\u597d\u7684\u9519\u8bef\u6d88\u606f\u3002 \u53e6\u4e00\u65b9\u9762\uff0c\u9519\u8bef\u5904\u7406\u65e8\u5728\u4f18\u96c5\u5730\u5904\u7406\u5728\u53d1\u5e03\u914d\u7f6e\u4e2d\u53ef\u80fd\u53d1\u751f\uff08\u65e0\u8bba\u591a\u4e48\u7f55\u89c1\uff09\u7684\u60c5\u51b5\u3002\u8fd9\u4e9b\u53ef\u80fd\u662f\u53ef\u6062\u590d\u7684\uff0c\u4e5f\u53ef\u80fd\u662f\u4e0d\u53ef\u6062\u590d\u7684\uff0c\u4f46\u662f\u5e94\u59cb\u7ec8\u5047\u5b9a\u7a0b\u5e8f\u7684\u7528\u6237\u53ef\u80fd\u4f1a\u9047\u5230\u5b83\u4eec\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u65ad\u8a00\u8bb0\u5f55\u5e94\u8be5\u5728\u903b\u8f91\u4e0a\u4e0d\u53ef\u80fd\u7684\u60c5\u51b5\u3002 \u65ad\u8a00\u6709\u65f6\u4e5f\u7528\u4e8e\u8bb0\u5f55\u7531\u4e8e\u7f16\u5199\u4ee3\u7801\u65f6\u4e0d\u9700\u8981\u7684\u60c5\u51b5\uff1a 1 2 // Assert with a message, covered in the next section assert ( moved && \"Need to handle case where student was just moved to another classroom\" ); \u8fd9\u6837\uff0c\u5982\u679c\u4ee3\u7801\u7684\u540e\u7eed\u7ef4\u62a4\u8005\u786e\u5b9e\u9047\u5230\u9700\u8981\u8fd9\u79cd\u60c5\u51b5\u7684\u60c5\u51b5\uff0c\u4ee3\u7801\u5c06\u56e0\u4e3a\u4e00\u4e2a\u6709\u7528\u7684\u9519\u8bef\u4fe1\u606f\u800c\u5931\u8d25\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u786e\u5b9a\u5982\u4f55\u5b9e\u73b0\u8fd9\u79cd\u60c5\u51b5\u3002 NDEBUG assert \u5b8f\u4f1a\u5e26\u6765\u4e00\u4e9b\u5c0f\u7684\u6027\u80fd\u635f\u5931\uff0c\u6bcf\u6b21\u68c0\u67e5 assert \u6761\u4ef6\u65f6\u90fd\u4f1a\u4ea7\u751f\u3002\u6b64\u5916\uff0c(\u7406\u60f3\u60c5\u51b5\u4e0b)asserts \u4e0d\u5e94\u8be5\u51fa\u73b0\u5728\u751f\u4ea7\u4ee3\u7801\u4e2d(\u56e0\u4e3a\u4f60\u7684\u4ee3\u7801\u5e94\u8be5\u5df2\u7ecf\u7ecf\u8fc7\u5f7b\u5e95\u6d4b\u8bd5)\u3002\u56e0\u6b64\uff0c\u8bb8\u591a\u5f00\u53d1\u4eba\u5458\u5e0c\u671b asserts \u4ec5\u5728\u8c03\u8bd5\u7248\u672c\u4e2d\u5904\u4e8e\u6d3b\u52a8\u72b6\u6001\u3002C++ \u63d0\u4f9b\u4e86\u4e00\u79cd\u5728\u751f\u4ea7\u4ee3\u7801\u4e2d\u5173\u95ed asserts \u7684\u65b9\u6cd5\u3002\u5982\u679c\u5b9a\u4e49\u4e86\u5b8f NDEBUG \uff0cassert \u5b8f\u5c06\u88ab\u7981\u7528\u3002 \u4e00\u4e9b IDE \u9ed8\u8ba4\u8bbe\u7f6e NDEBUG \u4f5c\u4e3a\u53d1\u5e03\u914d\u7f6e\u7684\u9879\u76ee\u8bbe\u7f6e\u7684\u4e00\u90e8\u5206\u3002\u4f8b\u5982\uff0c\u5728 Visual Studio \u4e2d\uff0c\u4ee5\u4e0b\u9884\u5904\u7406\u5668\u5b9a\u4e49\u88ab\u8bbe\u7f6e\u4e3a\u9879\u76ee\u7ea7\u522b: WIN32;NDEBUG;_CONSOLE \u3002\u5982\u679c\u4f60\u6b63\u5728\u4f7f\u7528 Visual Studio \u5e76\u5e0c\u671b\u5728\u53d1\u5e03\u7248\u672c\u4e2d\u89e6\u53d1 asserts\uff0c\u5219\u9700\u8981\u4ece\u6b64\u8bbe\u7f6e\u4e2d\u5220\u9664 NDEBUG \u3002 \u5982\u679c\u4f60\u4f7f\u7528\u7684 IDE \u6216\u6784\u5efa\u7cfb\u7edf\u5728\u53d1\u5e03\u914d\u7f6e\u4e2d\u6ca1\u6709\u81ea\u52a8\u5b9a\u4e49 NDEBUG \uff0c\u5219\u9700\u8981\u5728\u9879\u76ee\u6216\u7f16\u8bd1\u8bbe\u7f6e\u4e2d\u624b\u52a8\u6dfb\u52a0\u5b83\u3002 \u65ad\u8a00\u7684\u9650\u5236\u548c\u63d0\u9192 \u5173\u4e8e asserts \u6709\u4e00\u4e9b\u9677\u9631\u548c\u9650\u5236\u3002\u9996\u5148\uff0cassert \u672c\u8eab\u53ef\u80fd\u6709 bug\u3002\u5982\u679c\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\uff0cassert \u5c06\u62a5\u544a\u4e0d\u5b58\u5728\u7684\u9519\u8bef\uff0c\u6216\u8005\u65e0\u6cd5\u62a5\u544a\u5b58\u5728\u7684\u9519\u8bef\u3002 \u5176\u6b21\uff0c\u4f60\u7684 asserts \u4e0d\u5e94\u8be5\u6709\u4efb\u4f55\u526f\u4f5c\u7528\u2014\u2014\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u6709 assert \u548c\u6ca1\u6709 assert \u7684\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u5e94\u8be5\u8fd0\u884c\u76f8\u540c\u3002\u5426\u5219\uff0c\u5728\u8c03\u8bd5\u914d\u7f6e\u4e2d\u8fdb\u884c\u6d4b\u8bd5\u7684\u5185\u5bb9\u5c06\u4e0e\u53d1\u5e03\u914d\u7f6e\u4e2d\u8fdb\u884c\u7684\u6d4b\u8bd5\u5185\u5bb9\u4e0d\u540c(\u5047\u8bbe\u4f60\u4f7f\u7528\u4e86 NDEBUG)\u3002 \u8fd8\u8981\u6ce8\u610f\u7684\u662f\uff0cabort() \u51fd\u6570\u4f1a\u7acb\u5373\u7ec8\u6b62\u7a0b\u5e8f\uff0c\u6ca1\u6709\u673a\u4f1a\u8fdb\u884c\u4efb\u4f55\u8fdb\u4e00\u6b65\u7684\u6e05\u7406(\u4f8b\u5982\u5173\u95ed\u6587\u4ef6\u6216\u6570\u636e\u5e93)\u3002\u56e0\u6b64\uff0casserts \u5e94\u8be5\u4ec5\u5728\u7a0b\u5e8f\u610f\u5916\u7ec8\u6b62\u4e0d\u592a\u53ef\u80fd\u5bfc\u81f4\u635f\u574f\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002 static_assert C++ \u8fd8\u6709\u53e6\u4e00\u79cd\u79f0\u4e3a static_assert \u7684 assert \u7c7b\u578b\u3002 static_assert \u662f\u5728\u7f16\u8bd1\u65f6\u800c\u4e0d\u662f\u8fd0\u884c\u65f6\u68c0\u67e5\u7684\u65ad\u8a00\uff0c\u4e00\u4e2a\u5931\u8d25\u7684 static_assert \u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002\u4e0e assert \u4e0d\u540c\uff0cassert \u662f\u5728\u5934\u6587\u4ef6\u4e2d\u58f0\u660e\u7684\uff0c\u800c static_assert \u662f\u4e00\u4e2a\u5173\u952e\u5b57\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u5305\u542b\u5934\u6587\u4ef6\u624d\u80fd\u4f7f\u7528\u5b83\u3002 static_assert \u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 static_assert ( condition , diagnostic_message ) \u5982\u679c\u6761\u4ef6\u4e0d\u6210\u7acb\uff0c\u5c31\u4f1a\u6253\u5370\u51fa\u8bca\u65ad\u4fe1\u606f\u3002\u4e0b\u9762\u662f\u4f7f\u7528 static_assert \u786e\u4fdd\u7c7b\u578b\u5177\u6709\u7279\u5b9a\u5927\u5c0f\u7684\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 static_assert ( sizeof ( long ) == 8 , \"long must be 8 bytes\" ); static_assert ( sizeof ( int ) == 4 , \"int must be 4 bytes\" ); int main () { return 0 ; } \u5728\u4f5c\u8005\u7684\u673a\u5668\u4e0a\uff0c\u7f16\u8bd1\u65f6\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\uff1a 1 1>c:\\consoleapplication1\\main.cpp(19): error C2338: long must be 8 bytes \u56e0\u4e3a static_assert \u662f\u7531\u7f16\u8bd1\u5668\u8bc4\u4f30\u7684\uff0c\u6240\u4ee5\u6761\u4ef6\u5fc5\u987b\u80fd\u591f\u5728\u7f16\u8bd1\u65f6\u8fdb\u884c\u8bc4\u4f30\u3002\u6b64\u5916\uff0c\u4e0e\u6b63\u5e38\u7684 assert \u4e0d\u540c(\u5728\u8fd0\u884c\u65f6\u8bc4\u4f30)\uff0c static_assert \u53ef\u4ee5\u653e\u7f6e\u5728\u4ee3\u7801\u6587\u4ef6\u4e2d\u7684\u4efb\u4f55\u4f4d\u7f6e(\u751a\u81f3\u662f\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d)\u3002 \u5728 C++17 \u4e4b\u524d\uff0c\u8bca\u65ad\u6d88\u606f\u5fc5\u987b\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u53c2\u6570\u63d0\u4f9b\u3002\u81ea C++17 \u4ee5\u6765\uff0c\u63d0\u4f9b\u8bca\u65ad\u6d88\u606f\u662f\u53ef\u9009\u7684\u3002","title":"7.17 - \u65ad\u8a00\u548c static_assert"},{"location":"7-17-assert-and-static-assert/#_1","text":"In programming, a precondition is any condition that must always be true prior to the execution of component of code. Our check of y is a precondition that ensures y has a valid value before the function continues. It\u2019s more common for functions with preconditions to be written like this: 1 2 3 4 5 6 7 8 9 10 void printDivision ( int x , int y ) { if ( y == 0 ) { std :: cerr << \"Error: Could not divide by zero \\n \" ; return ; } std :: cout << static_cast < double > ( x ) / y ; } COPY An invariant is a condition that must be true while some component is executing. Similarly, a postcondition is something that must be true after the execution of some component of code. Our function doesn\u2019t have any postconditions.","title":"\u524d\u7f6e\u6761\u4ef6\u3001\u4e0d\u53d8\u91cf\u548c\u540e\u7f6e\u6761\u4ef6"},{"location":"7-17-assert-and-static-assert/#_2","text":"Using a conditional statement to detect an invalid parameter (or to validate some other kind of assumption), along with printing an error message and terminating the program, is such a common response to problems that C++ provides a shortcut method for doing this. An assertion is an expression that will be true unless there is a bug in the program. If the expression evaluates to true , the assertion statement does nothing. If the conditional expression evaluates to false , an error message is displayed and the program is terminated (via std::abort ). This error message typically contains the expression that failed as text, along with the name of the code file and the line number of the assertion. This makes it very easy to tell not only what the problem was, but where in the code the problem occurred. This can help with debugging efforts immensely. In C++, runtime assertions are implemented via the assert preprocessor macro, which lives in the header. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include // for assert() #include // for std::sqrt #include double calculateTimeUntilObjectHitsGround ( double initialHeight , double gravity ) { assert ( gravity > 0.0 ); // The object won't reach the ground unless there is positive gravity. if ( initialHeight <= 0.0 ) { // The object is already on the ground. Or buried. return 0.0 ; } return std :: sqrt (( 2.0 * initialHeight ) / gravity ); } int main () { std :: cout << \"Took \" << calculateTimeUntilObjectHitsGround ( 100.0 , -9.8 ) << \" second(s) \\n \" ; return 0 ; } COPY When the program calls calculateTimeUntilObjectHitsGround(100.0, -9.8) , assert(gravity > 0.0) will evaluate to false , which will trigger the assert. That will print a message similar to this: 1 dropsimulator: src/main.cpp:6: double calculateTimeUntilObjectHitsGround(double, double): Assertion 'gravity > 0.0' failed. The actual message varies depending on which compiler you use. Although asserts are most often used to validate function parameters, they can be used anywhere you would like to validate that something is true. Although we told you previously to avoid preprocessor macros, asserts are one of the few preprocessor macros that are considered acceptable to use. We encourage you to use assert statements liberally throughout your code.","title":"\u65ad\u8a00"},{"location":"7-17-assert-and-static-assert/#_3","text":"Sometimes assert expressions aren\u2019t very descriptive. Consider the following statement: 1 assert ( found ); COPY If this assert is triggered, the assert will say: 1 Assertion failed: found, file C:\\\\VCProjects\\\\Test.cpp, line 34 What does this even mean? Clearly found was false (since the assert triggered), but what wasn\u2019t found? You\u2019d have to go look at the code to determine that. Fortunately, there\u2019s a little trick you can use to make your assert statements more descriptive. Simply add a string literal joined by a logical AND: 1 assert ( found && \"Car could not be found in database\" ); COPY Here\u2019s why this works: A string literal always evaluates to Boolean true . So if found is false , false && true is false . If found is true , true && true is true . Thus, logical AND-ing a string literal doesn\u2019t impact the evaluation of the assert. However, when the assert triggers, the string literal will be included in the assert message: Assertion failed: found && \"Car could not be found in database\", file C:\\VCProjects\\Test.cpp, line 34 That gives you some additional context as to what went wrong.","title":"\u4f7f\u65ad\u8a00\u8bed\u53e5\u66f4\u5177\u63cf\u8ff0\u6027"},{"location":"7-17-assert-and-static-assert/#vs","text":"Assertions and error handling are similar enough that their purposes can be confused, so let\u2019s clarify: The goal of an assertion is to catch programming errors by documenting something that should never happen. If that thing does happen, then the programmer made an error somewhere, and that error can be identified and fixed. Assertions do not allow recovery from errors (after all, if something should never happen, there\u2019s no need to recover from it), and the program will not produce a friendly error message. On the other hand, error handling is designed to gracefully handle cases that could happen (however rarely) in release configurations. These may or may not be recoverable, but one should always assume a user of the program may encounter them. \u6700\u4f73\u5b9e\u8df5 Use assertions to document cases that should be logically impossible. Assertions are also sometimes used to document cases that were not implemented because they were not needed at the time the programmer wrote the code: 1 2 // Assert with a message, covered in the next section assert ( moved && \"Need to handle case where student was just moved to another classroom\" ); COPY That way, if a future user of the code does encounter a situation where this case is needed, the code will fail with a useful error message, and the programmer can then determine how to implement that case.","title":"\u65ad\u8a00 vs \u9519\u8bef\u5904\u7406"},{"location":"7-17-assert-and-static-assert/#ndebug","text":"The assert macro comes with a small performance cost that is incurred each time the assert condition is checked. Furthermore, asserts should (ideally) never be encountered in production code (because your code should already be thoroughly tested). Consequently, many developers prefer that asserts are only active in debug builds. C++ comes with a way to turn off asserts in production code. If the macro NDEBUG is defined, the assert macro gets disabled. Some IDEs set NDEBUG by default as part of the project settings for release configurations. For example, in Visual Studio, the following preprocessor definitions are set at the project level: WIN32;NDEBUG;_CONSOLE . If you\u2019re using Visual Studio and want your asserts to trigger in release builds, you\u2019ll need to remove NDEBUG from this setting. If you\u2019re using an IDE or build system that doesn\u2019t automatically define NDEBUG in release configuration, you will need to add it in the project or compilation settings manually.","title":"NDEBUG"},{"location":"7-17-assert-and-static-assert/#_4","text":"There are a few pitfalls and limitations to asserts. First, the assert itself can have a bug. If this happens, the assert will either report an error where none exists, or fail to report a bug where one does exist. Second, your asserts should have no side effects -- that is, the program should run the same with and without the assert. Otherwise, what you are testing in a debug configuration will not be the same as in a release configuration (assuming you ship with NDEBUG). Also note that the abort() function terminates the program immediately, without a chance to do any further cleanup (e.g. close a file or database). Because of this, asserts should be used only in cases where corruption isn\u2019t likely to occur if the program terminates unexpectedly.","title":"\u65ad\u8a00\u7684\u9650\u5236\u548c\u63d0\u9192"},{"location":"7-17-assert-and-static-assert/#static_assert","text":"C++ also has another type of assert called static_assert . A static_assert is an assertion that is checked at compile-time rather than at runtime, with a failing static_assert causing a compile error. Unlike assert, which is declared in the header, static_assert is a keyword, so no header needs to be included to use it. A static_assert takes the following form: 1 static_assert ( condition , diagnostic_message ) If the condition is not true, the diagnostic message is printed. Here\u2019s an example of using static_assert to ensure types have a certain size: 1 2 3 4 5 6 7 static_assert ( sizeof ( long ) == 8 , \"long must be 8 bytes\" ); static_assert ( sizeof ( int ) == 4 , \"int must be 4 bytes\" ); int main () { return 0 ; } COPY On the author\u2019s machine, when compiled, the compiler errors: 1 1>c:\\consoleapplication1\\main.cpp(19): error C2338: long must be 8 bytes Because static_assert is evaluated by the compiler, the condition must be able to be evaluated at compile time. Also, unlike normal assert (which is evaluated at runtime), static_assert can be placed anywhere in the code file (even in the global namespace). Prior to C++17, the diagnostic message must be supplied as the second parameter. Since C++17, providing a diagnostic message is optional. \u5728\u63a5\u6536\u53c2\u6570\u7684\u51fd\u6570\u4e2d\uff0c\u8c03\u7528\u8005\u53ef\u80fd\u4f1a\u4f20\u9012\u8bed\u6cd5\u4e0a\u6709\u6548\u4f46\u8bed\u4e49\u4e0a\u65e0\u610f\u4e49\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u5728\u4e0a\u4e00\u8bfe\u4e2d\uff08 7.15 - \u53d1\u73b0\u9519\u8bef\u548c\u5904\u7406\u9519\u8bef \uff09\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u4ee5\u4e0b\u793a\u4f8b\u51fd\u6570\uff1a 1 2 3 4 5 6 7 void printDivision ( int x , int y ) { if ( y != 0 ) std :: cout << static_cast ( x ) / y ; else std :: cerr << \"Error: Could not divide by zero \\n \" ; } \u8be5\u51fd\u6570\u663e\u5f0f\u68c0\u67e5 y \u662f\u5426\u4e3a 0 \uff0c\u56e0\u4e3a\u9664\u4ee5\u96f6\u662f\u8bed\u4e49\u9519\u8bef\uff0c\u5982\u679c\u6267\u884c\u5c06\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002 \u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u5904\u7406\u6b64\u7c7b\u95ee\u9898\u7684\u51e0\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u505c\u6b62\u7a0b\u5e8f\u6216\u8df3\u8fc7\u6709\u95ee\u9898\u7684\u8bed\u53e5\u3002 \u4f46\u8fd9\u4e24\u79cd\u9009\u9879\u90fd\u6709\u95ee\u9898\u3002\u5982\u679c\u7a0b\u5e8f\u7531\u4e8e\u9519\u8bef\u800c\u8df3\u8fc7\u8bed\u53e5\uff0c\u5219\u5b9e\u9645\u4e0a\u662f\u5728\u9ed8\u9ed8\u5730\u5931\u8d25\u3002\u5c24\u5176\u662f\u5728\u7f16\u5199\u548c\u8c03\u8bd5\u7a0b\u5e8f\u65f6\uff0c\u9759\u9ed8\u5931\u8d25\u662f\u4e0d\u597d\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u4f1a\u63a9\u76d6\u771f\u6b63\u7684\u95ee\u9898\u3002\u5373\u4f7f\u6211\u4eec\u6253\u5370\u9519\u8bef\u6d88\u606f\uff0c\u8be5\u9519\u8bef\u6d88\u606f\u4e5f\u53ef\u80fd\u4f1a\u5728\u5176\u4ed6\u7a0b\u5e8f\u8f93\u51fa\u4e2d\u4e22\u5931\uff0c\u5e76\u4e14\u53ef\u80fd\u4e0d\u660e\u663e\u5730\u751f\u6210\u9519\u8bef\u6d88\u606f\u6216\u89e6\u53d1\u9519\u8bef\u6d88\u606f\u7684\u6761\u4ef6\u5728\u54ea\u91cc\u3002\u6709\u4e9b\u51fd\u6570\u53ef\u80fd\u4f1a\u88ab\u8c03\u7528\u5341\u6b21\u6216\u51e0\u767e\u6b21\uff0c\u5982\u679c\u53ea\u6709\u5176\u4e2d\u4e00\u4e2a\u60c5\u51b5\u751f\u6210\u95ee\u9898\uff0c\u90a3\u4e48\u5f88\u96be\u77e5\u9053\u662f\u54ea\u4e2a\u60c5\u51b5\u3002 \u5982\u679c\u7a0b\u5e8f\u7ec8\u6b62\uff08\u901a\u8fc7 std::exit \uff09\uff0c\u5219\u6211\u4eec\u5c06\u5931\u53bb\u8c03\u7528\u5806\u6808\u548c\u4efb\u4f55\u53ef\u80fd\u5e2e\u52a9\u6211\u4eec\u9694\u79bb\u95ee\u9898\u7684\u8c03\u8bd5\u4fe1\u606f\u3002 std::abort \u662f\u8fd9\u79cd\u60c5\u51b5\u7684\u66f4\u597d\u9009\u62e9\uff0c\u56e0\u4e3a\u901a\u5e38\u5f00\u53d1\u4eba\u5458\u5c06\u6709\u9009\u62e9\u5728\u7a0b\u5e8f\u4e2d\u6b62\u7684\u70b9\u5f00\u59cb\u8c03\u8bd5\u3002","title":"static_assert"},{"location":"7-17-assert-and-static-assert/#_5","text":"\u5728\u7f16\u7a0b\u4e2d\uff0c\u524d\u7f6e\u6761\u4ef6\u662f\u6307\u5728\u4ee3\u7801\u7ec4\u4ef6\u6267\u884c\u4e4b\u524d\u5fc5\u987b\u59cb\u7ec8\u4e3a\u771f\u7684\u4efb\u4f55\u6761\u4ef6\u3002\u6211\u4eec\u5bf9 y \u7684\u68c0\u67e5\u662f\u786e\u4fdd\u51fd\u6570\u7ee7\u7eed\u4e4b\u524d y \u5177\u6709\u6709\u6548\u503c\u7684\u524d\u7f6e\u6761\u4ef6\u3002 \u66f4\u5e38\u89c1\u7684\u662f\uff0c\u5177\u6709\u524d\u7f6e\u6761\u4ef6\u7684\u51fd\u6570\u7f16\u5199\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 void printDivision ( int x , int y ) { if ( y == 0 ) { std :: cerr << \"Error: Could not divide by zero \\n \" ; return ; } std :: cout << static_cast ( x ) / y ; } \u4e0d\u53d8\u91cf\u662f\u6307\u67d0\u4e2a\u7ec4\u4ef6\u6267\u884c\u65f6\u5fc5\u987b\u4e3a\u771f\u7684\u6761\u4ef6\u3002 \u7c7b\u4f3c\u5730\uff0c\u540e\u7f6e\u6761\u4ef6\u662f\u6307\u67d0\u4e2a\u4ee3\u7801\u7ec4\u4ef6\u6267\u884c\u540e\u5fc5\u987b\u4e3a\u771f\u7684\u6761\u4ef6\u3002\u6211\u4eec\u7684\u51fd\u6570\u6ca1\u6709\u4efb\u4f55\u540e\u7f6e\u6761\u4ef6\u3002","title":"\u524d\u7f6e\u6761\u4ef6\u3001\u4e0d\u53d8\u91cf\u548c\u540e\u7f6e\u6761\u4ef6"},{"location":"7-17-assert-and-static-assert/#_6","text":"\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u68c0\u6d4b\u65e0\u6548\u53c2\u6570\uff08\u6216\u9a8c\u8bc1\u67d0\u79cd\u5176\u4ed6\u7c7b\u578b\u7684\u5047\u8bbe\uff09\uff0c\u5e76\u6253\u5370\u9519\u8bef\u6d88\u606f\u5e76\u7ec8\u6b62\u7a0b\u5e8f\u662f\u5bf9\u95ee\u9898\u7684\u5e38\u89c1\u54cd\u5e94\uff0c\u56e0\u6b64C++\u63d0\u4f9b\u4e86\u4e00\u79cd\u5feb\u6377\u65b9\u6cd5\u6765\u6267\u884c\u6b64\u64cd\u4f5c\u3002 \u65ad\u8a00\u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u9664\u975e\u7a0b\u5e8f\u4e2d\u5b58\u5728\u9519\u8bef\uff0c\u5426\u5219\u8be5\u8868\u8fbe\u5f0f\u5c06\u4e3a\u771f\u3002\u5982\u679c\u8868\u8fbe\u5f0f\u7684\u503c\u4e3a true \uff0c\u5219\u65ad\u8a00\u8bed\u53e5\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\u3002\u5982\u679c\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u503c\u4e3a false \uff0c\u5219\u663e\u793a\u9519\u8bef\u6d88\u606f\u5e76\u901a\u8fc7 std::abort \u7ec8\u6b62\u7a0b\u5e8f\u3002\u6b64\u9519\u8bef\u6d88\u606f\u901a\u5e38\u5305\u542b\u4ee5\u6587\u672c\u5f62\u5f0f\u5931\u8d25\u7684\u8868\u8fbe\u5f0f\uff0c\u4ee5\u53ca\u65ad\u8a00\u7684\u4ee3\u7801\u6587\u4ef6\u7684\u540d\u79f0\u548c\u884c\u53f7\u3002\u8fd9\u4f7f\u5f97\u4e0d\u4ec5\u53ef\u4ee5\u786e\u5b9a\u95ee\u9898\u6240\u5728\uff0c\u8fd8\u53ef\u4ee5\u786e\u5b9a\u95ee\u9898\u53d1\u751f\u7684\u4ee3\u7801\u4f4d\u7f6e\u3002\u8fd9\u53ef\u4ee5\u6781\u5927\u5730\u5e2e\u52a9\u8c03\u8bd5\u5de5\u4f5c\u3002 \u5728C++\u4e2d\uff0c\u8fd0\u884c\u65f6\u65ad\u8a00\u662f\u901a\u8fc7assert\u9884\u5904\u7406\u5b8f\u5b9e\u73b0\u7684\uff0c\u8be5\u5b8f\u4f4d\u4e8e\u5934\u6587\u4ef6\u4e2d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include // for assert() #include // for std::sqrt #include double calculateTimeUntilObjectHitsGround ( double initialHeight , double gravity ) { assert ( gravity > 0.0 ); // The object won't reach the ground unless there is positive gravity. if ( initialHeight <= 0.0 ) { // The object is already on the ground. Or buried. return 0.0 ; } return std :: sqrt (( 2.0 * initialHeight ) / gravity ); } int main () { std :: cout << \"Took \" << calculateTimeUntilObjectHitsGround ( 100.0 , -9.8 ) << \" second(s) \\n \" ; return 0 ; } COPY \u5f53\u7a0b\u5e8f\u8c03\u7528 calculateTimeUntilObjectHitsGround(100.0, -9.8) \u65f6\uff0c assert(gravity > 0.0) \u5c06\u8ba1\u7b97\u4e3a false \uff0c\u8fd9\u5c06\u89e6\u53d1\u65ad\u8a00\u3002\u8fd9\u5c06\u6253\u5370\u7c7b\u4f3c\u4e8e\u4ee5\u4e0b\u5185\u5bb9\u7684\u6d88\u606f\uff1a 1 dropsimulator: src/main.cpp:6: double calculateTimeUntilObjectHitsGround(double, double): Assertion 'gravity > 0.0' failed. \u5b9e\u9645\u6d88\u606f\u56e0\u4f7f\u7528\u7684\u7f16\u8bd1\u5668\u800c\u5f02\u3002 \u5c3d\u7ba1\u65ad\u8a00\u6700\u5e38\u7528\u4e8e\u9a8c\u8bc1\u51fd\u6570\u53c2\u6570\uff0c\u4f46\u5b83\u4eec\u53ef\u4ee5\u7528\u4e8e\u4efb\u4f55\u60a8\u60f3\u8981\u9a8c\u8bc1\u7684\u5185\u5bb9\u3002 \u5c3d\u7ba1\u6211\u4eec\u4e4b\u524d\u544a\u8bc9\u8fc7\u60a8\u8981\u907f\u514d\u9884\u5904\u7406\u5b8f\uff0c\u4f46\u65ad\u8a00\u662f\u5c11\u6570\u88ab\u8ba4\u4e3a\u53ef\u4ee5\u4f7f\u7528\u7684\u9884\u5904\u7406\u5b8f\u4e4b\u4e00\u3002\u6211\u4eec\u9f13\u52b1\u60a8\u5728\u4ee3\u7801\u4e2d\u5927\u91cf\u4f7f\u7528assert\u8bed\u53e5\u3002","title":"\u65ad\u8a00"},{"location":"7-17-assert-and-static-assert/#_7","text":"\u6709\u65f6\uff0c\u65ad\u8a00\u8868\u8fbe\u5f0f\u7684\u63cf\u8ff0\u6027\u5e76\u4e0d\u597d\uff0c\u4f8b\u5982\u4e0b\u9762\u7684\u8bed\u53e5\uff1a 1 assert ( found ); \u5982\u679c\u89e6\u53d1\u6b64\u65ad\u8a00\uff0c\u5219\u4f1a\u663e\u793a\uff1a 1 Assertion failed: found, file C:\\\\VCProjects\\\\Test.cpp, line 34 \u8fd9\u662f\u4ec0\u4e48\u610f\u601d\uff1f\u663e\u7136\uff0c found \u662f false \uff08\u56e0\u4e3a\u65ad\u8a00\u89e6\u53d1\u4e86\uff09\uff0c\u81f3\u4e8e\u662f\u4ec0\u4e48\u6ca1\u627e\u5230\uff1f\u4f60\u5fc5\u987b\u67e5\u770b\u4ee3\u7801\u624d\u80fd\u786e\u5b9a\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5c0f\u6280\u5de7\u4f7f assert \u8bed\u53e5\u66f4\u5177\u63cf\u8ff0\u6027\u3002\u53ea\u9700\u6dfb\u52a0\u4e00\u4e2a\u903b\u8f91AND\u8fde\u63a5\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff1a 1 assert ( found && \"Car could not be found in database\" ); \u8fd9\u662f\u4e3a\u4ec0\u4e48\u5b83\u6709\u6548\u7684\u539f\u56e0\uff1a\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u59cb\u7ec8\u8ba1\u7b97\u4e3a\u5e03\u5c14\u503c true \u3002\u56e0\u6b64\uff0c\u5982\u679c found \u4e3a false \uff0c\u5219 false && true \u4e3a false \u3002\u5982\u679c found \u4e3a true \uff0c\u5219 true && true \u4e3a true \u3002\u56e0\u6b64\uff0c\u5728\u903b\u8f91AND\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u4e0d\u4f1a\u5f71\u54cd\u65ad\u8a00\u7684\u8bc4\u4f30\u3002 \u4f46\u662f\uff0c\u5f53\u65ad\u8a00\u89e6\u53d1\u65f6\uff0c\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u5c06\u5305\u542b\u5728\u65ad\u8a00\u6d88\u606f\u4e2d\uff1a Assertion failed: found && \"Car could not be found in database\", file C:\\\\VCProjects\\\\Test.cpp, line 34 \u8fd9\u4e3a\u4f60\u63d0\u4f9b\u4e86\u6709\u5173\u51fa\u4e86\u4ec0\u4e48\u95ee\u9898\u7684\u5176\u4ed6\u4e0a\u4e0b\u6587\u3002","title":"\u4f7f\u65ad\u8a00\u8bed\u53e5\u66f4\u5177\u63cf\u8ff0\u6027"},{"location":"7-17-assert-and-static-assert/#_8","text":"\u65ad\u8a00\u548c\u9519\u8bef\u5904\u7406\u975e\u5e38\u76f8\u4f3c\uff0c\u4ee5\u81f3\u4e8e\u5b83\u4eec\u7684\u76ee\u7684\u53ef\u80fd\u4f1a\u6df7\u6dc6\uff0c\u56e0\u6b64\u8ba9\u6211\u4eec\u6f84\u6e05\uff1a \u65ad\u8a00\u7684\u76ee\u6807\u662f\u901a\u8fc7\u8bb0\u5f55\u6c38\u8fdc\u4e0d\u5e94\u53d1\u751f\u7684\u4e8b\u60c5\u6765\u6355\u83b7\u7f16\u7a0b\u9519\u8bef\u3002\u5982\u679c\u53d1\u751f\u4e86\u90a3\u4ef6\u4e8b\uff0c\u90a3\u4e48\u7a0b\u5e8f\u5458\u5728\u67d0\u4e2a\u5730\u65b9\u72af\u4e86\u9519\u8bef\uff0c\u5e76\u4e14\u53ef\u4ee5\u786e\u5b9a\u5e76\u4fee\u590d\u8be5\u9519\u8bef\u3002\u65ad\u8a00\u4e0d\u5141\u8bb8\u4ece\u9519\u8bef\u4e2d\u6062\u590d\uff08\u6bd5\u7adf\uff0c\u5982\u679c\u67d0\u4ef6\u4e8b\u60c5\u6c38\u8fdc\u4e0d\u5e94\u8be5\u53d1\u751f\uff0c\u90a3\u4e48\u65e0\u9700\u4ece\u4e2d\u6062\u590d\uff09\uff0c\u5e76\u4e14\u7a0b\u5e8f\u4e0d\u4f1a\u751f\u6210\u53cb\u597d\u7684\u9519\u8bef\u6d88\u606f\u3002 \u53e6\u4e00\u65b9\u9762\uff0c\u9519\u8bef\u5904\u7406\u65e8\u5728\u4f18\u96c5\u5730\u5904\u7406\u5728\u53d1\u5e03\u914d\u7f6e\u4e2d\u53ef\u80fd\u53d1\u751f\uff08\u65e0\u8bba\u591a\u4e48\u7f55\u89c1\uff09\u7684\u60c5\u51b5\u3002\u8fd9\u4e9b\u53ef\u80fd\u662f\u53ef\u6062\u590d\u7684\uff0c\u4e5f\u53ef\u80fd\u662f\u4e0d\u53ef\u6062\u590d\u7684\uff0c\u4f46\u662f\u5e94\u59cb\u7ec8\u5047\u5b9a\u7a0b\u5e8f\u7684\u7528\u6237\u53ef\u80fd\u4f1a\u9047\u5230\u5b83\u4eec\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u65ad\u8a00\u8bb0\u5f55\u5e94\u8be5\u5728\u903b\u8f91\u4e0a\u4e0d\u53ef\u80fd\u7684\u60c5\u51b5\u3002 \u65ad\u8a00\u6709\u65f6\u4e5f\u7528\u4e8e\u8bb0\u5f55\u7531\u4e8e\u7f16\u5199\u4ee3\u7801\u65f6\u4e0d\u9700\u8981\u7684\u60c5\u51b5\uff1a 1 2 // Assert with a message, covered in the next section assert ( moved && \"Need to handle case where student was just moved to another classroom\" ); \u8fd9\u6837\uff0c\u5982\u679c\u4ee3\u7801\u7684\u540e\u7eed\u7ef4\u62a4\u8005\u786e\u5b9e\u9047\u5230\u9700\u8981\u8fd9\u79cd\u60c5\u51b5\u7684\u60c5\u51b5\uff0c\u4ee3\u7801\u5c06\u56e0\u4e3a\u4e00\u4e2a\u6709\u7528\u7684\u9519\u8bef\u4fe1\u606f\u800c\u5931\u8d25\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u786e\u5b9a\u5982\u4f55\u5b9e\u73b0\u8fd9\u79cd\u60c5\u51b5\u3002","title":"\u65ad\u8a00\u4e0e\u9519\u8bef\u5904\u7406"},{"location":"7-17-assert-and-static-assert/#ndebug_1","text":"assert \u5b8f\u4f1a\u5e26\u6765\u4e00\u4e9b\u5c0f\u7684\u6027\u80fd\u635f\u5931\uff0c\u6bcf\u6b21\u68c0\u67e5 assert \u6761\u4ef6\u65f6\u90fd\u4f1a\u4ea7\u751f\u3002\u6b64\u5916\uff0c(\u7406\u60f3\u60c5\u51b5\u4e0b)asserts \u4e0d\u5e94\u8be5\u51fa\u73b0\u5728\u751f\u4ea7\u4ee3\u7801\u4e2d(\u56e0\u4e3a\u4f60\u7684\u4ee3\u7801\u5e94\u8be5\u5df2\u7ecf\u7ecf\u8fc7\u5f7b\u5e95\u6d4b\u8bd5)\u3002\u56e0\u6b64\uff0c\u8bb8\u591a\u5f00\u53d1\u4eba\u5458\u5e0c\u671b asserts \u4ec5\u5728\u8c03\u8bd5\u7248\u672c\u4e2d\u5904\u4e8e\u6d3b\u52a8\u72b6\u6001\u3002C++ \u63d0\u4f9b\u4e86\u4e00\u79cd\u5728\u751f\u4ea7\u4ee3\u7801\u4e2d\u5173\u95ed asserts \u7684\u65b9\u6cd5\u3002\u5982\u679c\u5b9a\u4e49\u4e86\u5b8f NDEBUG \uff0cassert \u5b8f\u5c06\u88ab\u7981\u7528\u3002 \u4e00\u4e9b IDE \u9ed8\u8ba4\u8bbe\u7f6e NDEBUG \u4f5c\u4e3a\u53d1\u5e03\u914d\u7f6e\u7684\u9879\u76ee\u8bbe\u7f6e\u7684\u4e00\u90e8\u5206\u3002\u4f8b\u5982\uff0c\u5728 Visual Studio \u4e2d\uff0c\u4ee5\u4e0b\u9884\u5904\u7406\u5668\u5b9a\u4e49\u88ab\u8bbe\u7f6e\u4e3a\u9879\u76ee\u7ea7\u522b: WIN32;NDEBUG;_CONSOLE \u3002\u5982\u679c\u4f60\u6b63\u5728\u4f7f\u7528 Visual Studio \u5e76\u5e0c\u671b\u5728\u53d1\u5e03\u7248\u672c\u4e2d\u89e6\u53d1 asserts\uff0c\u5219\u9700\u8981\u4ece\u6b64\u8bbe\u7f6e\u4e2d\u5220\u9664 NDEBUG \u3002 \u5982\u679c\u4f60\u4f7f\u7528\u7684 IDE \u6216\u6784\u5efa\u7cfb\u7edf\u5728\u53d1\u5e03\u914d\u7f6e\u4e2d\u6ca1\u6709\u81ea\u52a8\u5b9a\u4e49 NDEBUG \uff0c\u5219\u9700\u8981\u5728\u9879\u76ee\u6216\u7f16\u8bd1\u8bbe\u7f6e\u4e2d\u624b\u52a8\u6dfb\u52a0\u5b83\u3002","title":"NDEBUG"},{"location":"7-17-assert-and-static-assert/#_9","text":"\u5173\u4e8e asserts \u6709\u4e00\u4e9b\u9677\u9631\u548c\u9650\u5236\u3002\u9996\u5148\uff0cassert \u672c\u8eab\u53ef\u80fd\u6709 bug\u3002\u5982\u679c\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\uff0cassert \u5c06\u62a5\u544a\u4e0d\u5b58\u5728\u7684\u9519\u8bef\uff0c\u6216\u8005\u65e0\u6cd5\u62a5\u544a\u5b58\u5728\u7684\u9519\u8bef\u3002 \u5176\u6b21\uff0c\u4f60\u7684 asserts \u4e0d\u5e94\u8be5\u6709\u4efb\u4f55\u526f\u4f5c\u7528\u2014\u2014\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u6709 assert \u548c\u6ca1\u6709 assert \u7684\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u5e94\u8be5\u8fd0\u884c\u76f8\u540c\u3002\u5426\u5219\uff0c\u5728\u8c03\u8bd5\u914d\u7f6e\u4e2d\u8fdb\u884c\u6d4b\u8bd5\u7684\u5185\u5bb9\u5c06\u4e0e\u53d1\u5e03\u914d\u7f6e\u4e2d\u8fdb\u884c\u7684\u6d4b\u8bd5\u5185\u5bb9\u4e0d\u540c(\u5047\u8bbe\u4f60\u4f7f\u7528\u4e86 NDEBUG)\u3002 \u8fd8\u8981\u6ce8\u610f\u7684\u662f\uff0cabort() \u51fd\u6570\u4f1a\u7acb\u5373\u7ec8\u6b62\u7a0b\u5e8f\uff0c\u6ca1\u6709\u673a\u4f1a\u8fdb\u884c\u4efb\u4f55\u8fdb\u4e00\u6b65\u7684\u6e05\u7406(\u4f8b\u5982\u5173\u95ed\u6587\u4ef6\u6216\u6570\u636e\u5e93)\u3002\u56e0\u6b64\uff0casserts \u5e94\u8be5\u4ec5\u5728\u7a0b\u5e8f\u610f\u5916\u7ec8\u6b62\u4e0d\u592a\u53ef\u80fd\u5bfc\u81f4\u635f\u574f\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002","title":"\u65ad\u8a00\u7684\u9650\u5236\u548c\u63d0\u9192"},{"location":"7-17-assert-and-static-assert/#static_assert_1","text":"C++ \u8fd8\u6709\u53e6\u4e00\u79cd\u79f0\u4e3a static_assert \u7684 assert \u7c7b\u578b\u3002 static_assert \u662f\u5728\u7f16\u8bd1\u65f6\u800c\u4e0d\u662f\u8fd0\u884c\u65f6\u68c0\u67e5\u7684\u65ad\u8a00\uff0c\u4e00\u4e2a\u5931\u8d25\u7684 static_assert \u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002\u4e0e assert \u4e0d\u540c\uff0cassert \u662f\u5728\u5934\u6587\u4ef6\u4e2d\u58f0\u660e\u7684\uff0c\u800c static_assert \u662f\u4e00\u4e2a\u5173\u952e\u5b57\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u5305\u542b\u5934\u6587\u4ef6\u624d\u80fd\u4f7f\u7528\u5b83\u3002 static_assert \u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 static_assert ( condition , diagnostic_message ) \u5982\u679c\u6761\u4ef6\u4e0d\u6210\u7acb\uff0c\u5c31\u4f1a\u6253\u5370\u51fa\u8bca\u65ad\u4fe1\u606f\u3002\u4e0b\u9762\u662f\u4f7f\u7528 static_assert \u786e\u4fdd\u7c7b\u578b\u5177\u6709\u7279\u5b9a\u5927\u5c0f\u7684\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 static_assert ( sizeof ( long ) == 8 , \"long must be 8 bytes\" ); static_assert ( sizeof ( int ) == 4 , \"int must be 4 bytes\" ); int main () { return 0 ; } \u5728\u4f5c\u8005\u7684\u673a\u5668\u4e0a\uff0c\u7f16\u8bd1\u65f6\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\uff1a 1 1>c:\\consoleapplication1\\main.cpp(19): error C2338: long must be 8 bytes \u56e0\u4e3a static_assert \u662f\u7531\u7f16\u8bd1\u5668\u8bc4\u4f30\u7684\uff0c\u6240\u4ee5\u6761\u4ef6\u5fc5\u987b\u80fd\u591f\u5728\u7f16\u8bd1\u65f6\u8fdb\u884c\u8bc4\u4f30\u3002\u6b64\u5916\uff0c\u4e0e\u6b63\u5e38\u7684 assert \u4e0d\u540c(\u5728\u8fd0\u884c\u65f6\u8bc4\u4f30)\uff0c static_assert \u53ef\u4ee5\u653e\u7f6e\u5728\u4ee3\u7801\u6587\u4ef6\u4e2d\u7684\u4efb\u4f55\u4f4d\u7f6e(\u751a\u81f3\u662f\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d)\u3002 \u5728 C++17 \u4e4b\u524d\uff0c\u8bca\u65ad\u6d88\u606f\u5fc5\u987b\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u53c2\u6570\u63d0\u4f9b\u3002\u81ea C++17 \u4ee5\u6765\uff0c\u63d0\u4f9b\u8bca\u65ad\u6d88\u606f\u662f\u53ef\u9009\u7684\u3002","title":"static_assert"},{"location":"7-18-introduction-to-random-number-generation/","text":"Key Takeaway The ability to generate random numbers can be useful in certain kinds of programs, particularly in games, statistical modelling programs, and cryptographic applications that need to encrypt and decrypt things. Take games for example -- without random events, monsters would always attack you the same way, you\u2019d always find the same treasure, the dungeon layout would never change, etc\u2026 and that would not make for a very good game. In real life, we often produce randomization by doing things like flipping a coin, rolling a dice, or shuffling a deck of cards. These events aren\u2019t actually random, but involve so many physical variables (e.g. gravity, friction, air resistance, momentum, etc\u2026) that they become almost impossible to predict or control, and (unless you\u2019re a magician) produce results that are for all intents and purposes random. However, computers aren\u2019t designed to take advantage of physical variables -- your computer can\u2019t toss a coin, throw a dice, or shuffle real cards. Modern computers live in a controlled electrical world where everything is binary (0 or 1) and there is no in-between. By their very nature, computers are designed to produce results that are as predictable as possible. When you tell the computer to calculate 2 + 2, you always want the answer to be 4. Not 3 or 5 on occasion. Consequently, computers are generally incapable of generating truly random numbers (at least through software). Instead, modern programs typically _simulate_randomness using an algorithm. In this lesson, we\u2019ll cover a lot of the theory behind how random numbers are generated in programs, and introduce some terminology we\u2019ll use in future lessons. \u7b97\u6cd5\u548c\u72b6\u6001 First, let\u2019s take a detour through the concepts of algorithms and states. An algorithm is a finite sequence of instructions that can be followed to solve some problem or produce some useful result. For example, let\u2019s say your boss gives you a small text file containing a bunch of unsorted names (one per line), and asks you to sort the list. Since the list is small, and you don\u2019t expect to do this often, you decide to sort it by hand. There are multiple ways to sort a list, but you might do something like this: Create a new empty list to hold the sorted results Scan the list of unsorted names to find the name that comes first alphabetically Cut that name out of the unsorted list and paste it at the bottom of the sorted list Repeat the previous two steps until there are no more names on the unsorted list The above set of steps describes a sorting algorithm (using natural language). By nature, algorithms are reusable -- if your boss asks you to sort another list tomorrow, you can just apply the same algorithm to the new list. Because computers can execute instructions and manipulate data much more quickly than we can, algorithms are often written using programming languages, allowing us to automate tasks. In C++, algorithms are typically implemented as reusable functions. Here\u2019s a simple algorithm for generating a sequence of numbers where each successive number is incremented by 1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int plusOne () { static int s_state { 3 }; // only initialized the first time this function is called // Generate the next number ++ s_state ; // first we modify the state return s_state ; // then we use the new state to generate the next number in the sequence } int main () { std :: cout << plusOne () << '\\n' ; std :: cout << plusOne () << '\\n' ; std :: cout << plusOne () << '\\n' ; return 0 ; } COPY This prints: 1 2 3 4 5 6 This algorithm is pretty simple. The first time we call plusOne() , s_state is initialized to value 3 . Then the next number in the sequence is generated and returned. An algorithm is considered to be stateful if it retains some information across calls. Conversely, a stateless algorithm does not store any information (and must be given all the information it needs to work with when it is called). Our plusOne() function is stateful, in that it uses the static variable s_state to store the last number that was generated. When applied to algorithms, the term state refers to the current values held in stateful variables. To generate the next number in the sequence, our algorithm uses a two step process: First, the current state (initialized from the start value, or preserved from the prior call) is modified to produce a new state. Then, the next number in the sequence is generated from the new state. Our algorithm is considered deterministic, meaning that for a given input (the value provided for start ), it will always produce the same output sequence. \u4f2a\u968f\u673a\u6570\u751f\u6210\u5668 (PRNGs) To simulate randomness, programs typically use a pseudo-random number generator. A pseudo-random number generator (PRNG) is an algorithm that generates a sequence of numbers whose properties simulate a sequence of random numbers. It\u2019s easy to write a basic PRNG algorithm. Here\u2019s a short PRNG example that generates 100 16-bit pseudo-random numbers: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include // For illustrative purposes only, don't use this unsigned int LCG16 () // our PRNG { static unsigned int s_state { 5323 }; // Generate the next number // Due to our use of large constants and overflow, it would be // hard for someone to casually predict what the next number is // going to be from the previous one. s_state = 8253729 * s_state + 2396403 ; // first we modify the state return s_state % 32768 ; // then we use the new state to generate the next number in the sequence } int main () { // Print 100 random numbers for ( int count { 1 }; count <= 100 ; ++ count ) { std :: cout << LCG16 () << '\\t' ; // If we've printed 10 numbers, start a new row if ( count % 10 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY The result of this program is: 1 2 3 4 5 6 7 8 9 10 23070 27857 22756 10839 27946 11613 30448 21987 22070 1001 27388 5999 5442 28789 13576 28411 10830 29441 21780 23687 5466 2957 19232 24595 22118 14873 5932 31135 28018 32421 14648 10539 23166 22833 12612 28343 7562 18877 32592 19011 13974 20553 9052 15311 9634 27861 7528 17243 27310 8033 28020 24807 1466 26605 4992 5235 30406 18041 3980 24063 15826 15109 24984 15755 23262 17809 2468 13079 19946 26141 1968 16035 5878 7337 23484 24623 13826 26933 1480 6075 11022 19393 1492 25927 30234 17485 23520 18643 5926 21209 2028 16991 3634 30565 2552 20971 23358 12785 25092 30583 Each number appears to be pretty random with respect to the previous one. Notice how similar LCG16() is to our plusOne() example above! We can pass LCG16() an initial value that is used to initialize the state. Then to produce the next number in the output sequence, the current state is modified (by applying some mathematical operations) to produce a new state, and the next number in the sequence is generated from that new state. As it turns out, this particular algorithm isn\u2019t very good as a random number generator. But most PRNGs work similarly to LCG16() -- they just typically use more state variables and more complex mathematical operations in order to generate better quality results. PRNG \u7684\u79cd\u5b50 The sequence of \u201crandom numbers\u201d generated by a PRNG is not random at all. Just like our plusOne() function, LCG16() is also deterministic. Once the state has been initialized, LCG16() (and all other PRNGs) will generate the same output sequence. When a PRNG is instantiated, an initial value (or set of values) called a random seed (or seed for short) can be provided to initialize the state of the PRNG. When a PRNG has been initialized with a seed, we say it has been seeded. \u5173\u952e\u4fe1\u606f All of the values that a PRNG will produce are deterministically calculated from the seed value(s). Most PRNGs that produce quality results use at least 16 bytes of state, if not significantly more. However, the size of the seed value can be smaller than the size of the state of the PRNG. When this happens, we say the PRNG has been underseeded. Ideally, every bit in the state is initialized from a seed of equal size, and every bit in the seed has been independently determined somehow. However, if a PRNG is underseeded, some number of bits in the state will need to be initialized from the same bits in the seed. If a PRNG is significantly underseeded (meaning the size of the seed is much smaller than the size of the state), the quality of the random results the PRNG produces can be impacted. \u4ec0\u4e48\u6837\u7684PRNG\u662f\u597dPRNG\uff1f(\u9009\u8bfb) In order to be a good PRNG, the PRNG needs to exhibit a number of properties: The PRNG should generate each number with approximately the same probability. This is called distribution uniformity. If some numbers are generated more often than others, the result of the program that uses the PRNG will be biased! To check distribution uniformity, we can use a histogram. A histogram is a graph that tracks how many times each number has been generated. Since our histograms are text-based, we\u2019ll use a * symbol to represent each time a given number was generated. Consider a PRNG that generates numbers between 1 and 6. If we generate 36 numbers, a PRNG with distribution uniformity should generate a histogram that looks something like this: 1 2 3 4 5 6 1|****** 2|****** 3|****** 4|****** 5|****** 6|****** A PRNG that is biased in some way will generate a histogram that is uneven, like this: 1 2 3 4 5 6 1|*** 2|****** 3|****** 4|****** 5|****** 6|********* or this: 1 2 3 4 5 6 1|**** 2|******** 3|****** 4|******** 5|****** 6|**** Let\u2019s say you\u2019re trying to write a random item generator for a game. When a monster is killed, your code generates a random number between 1 and 6, and if the result is a 6, the monster will drop a rare item instead of a common one. You would expect a 1 in 6 chance of this happening. But if the underlying PRNG is not uniform, and generates a lot more 6s than it should (like the second histogram above), your players will end up getting more rare items than you\u2019d intended, possibly trivializing the difficulty of your game, or messing up your in-game economy. Finding PRNG algorithms that produce uniform results is difficult. The method by which the next number in the sequence is generated shouldn\u2019t be predictable. For example, consider the following PRNG algorithm: return ++num . This PRNG is perfectly uniform, but it is also completely predictable -- and not very useful as a sequence of random numbers! Even sequences of numbers that seem random to the eye (such as the output of LCG16() above) may be trivially predictable by someone who is motivated. By examining just a few numbers generated from the LCG16() function above, it is possible to determine which constants are used ( 8253729 and 2396403 ) to modify the state. Once that is known, it becomes trivial to calculate all of the future numbers that will be generated from this PRNG. Now, imagine you\u2019re running a betting website where users can bet $100. Your website then generates a random number between 0 and 32767. If the number is greater than 20000, the customer wins and you pay them double. Otherwise, they lose. Since the customer wins only 12767/32767 (39%) of the time, your website should make tons of money, right? However, if customers are able to determine which numbers will be generated next, then they can strategically place bets so they always (or usually) win. Congrats, now you get to file for bankruptcy! The PRNG should have a good dimensional distribution of numbers. This means the PRNG should return numbers across the entire range of possible results at random. For example, the PRNG should generate low numbers, middle numbers, high numbers, even numbers, and odd numbers seemingly at random. A PRNG that returned all low numbers, then all high numbers may be uniform and non-predictable, but it\u2019s still going to lead to biased results, particularly if the number of random numbers you actually use is small. The PRNG should have a high period for all seeds All PRNGs are periodic, which means that at some point the sequence of numbers generated will begin to repeat itself. The length of the sequence before a PRNG begins to repeat itself is known as the period. For example, here are the first 100 numbers generated from a PRNG with poor periodicity: 1 2 3 4 5 6 7 8 9 10 112 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 You will note that it generated 9 as the 2nd number, again as the 16th number, and then every 14 numbers after that. This PRNG is stuck generating the following sequence repeatedly: 9-130-97-64-31-152-119-86-53-20-141-108-75-42-(repeat) . This happens because PRNGs are deterministic. Once the state of a PRNG is identical to a prior state, the PRNG will start producing the same sequence of outputs it has produced before -- resulting in a loop. A good PRNG should have a long period for all seed numbers. Designing an algorithm that meets this property can be extremely difficult -- many PRNGs have long periods only for some seeds and not others. If the user happens to pick a seed that results in a state with a short period, then the PRNG won\u2019t do a good job if many random numbers are needed. The PRNG should be efficient Most PRNGs have a state size of less than 4096 bytes, so total memory usage typically isn\u2019t a concern. However, the larger the internal state, the more likely the PRNG is to be underseeded, and the slower the initial seeding will be (since there\u2019s more state to initialize). Second, to generate the next number in sequence, a PRNG has to mix up its internal state by applying various mathematical operations. How much time this takes can vary significantly by PRNG and also by architecture (some PRNGs perform better on certain architectures than others). This doesn\u2019t matter if you only generate random numbers periodically, but can have a huge impact if you need lots of randomness. \u591a\u79cd\u4e0d\u540c\u7684 PRNG \u7b97\u6cd5 Over the years, many different kinds of PRNG algorithms have been developed (Wikipedia has a good list here ). Every PRNG algorithm has strengths and weaknesses that might make it more or less suitable for a particular applications, so selecting the right algorithm for your application is important. Many PRNGs are now considered relatively poor by modern standards -- and there\u2019s no reason to use a PRNG that doesn\u2019t perform well when it\u2019s just as easy to use one that does. C++ \u4e2d\u7684\u968f\u673a\u5316 The randomization capabilities in C++ are accessible via the header of the standard library. Within the random library, there are 6 PRNG families available for use (as of C++20): Type name Family Period State size* Performance Quality Should I use this? minstd_rand minstd_rand0 Linear congruential generator 2^31 4 bytes Bad Awful No mt19937 mt19937_64 Mersenne twister 2^19937 2500 bytes Decent Decent Probably (see next section) ranlux24 ranlux48 Subtract and carry 10^171 96 bytes Awful Good No knuth_b Shuffled linear congruential generator 2^31 1028 bytes Awful Bad No default_random_engine Any of above (implementation defined) Varies Varies ? ? No2 rand() Linear congruential generator 2^31 4 bytes Bad Awful Nono There is zero reason to use knuth_b , default_random_engine , or rand() (which is a random number generator provided for compatibility with C). As of C++20, the Mersenne Twister algorithm is the only PRNG that ships with C++ that has both decent performance and quality. \u6269\u5c55\u9605\u8bfb A test called PracRand is often used to assess the performance and quality of PRNGs (to determine whether they have different kinds of biases). You may also see references to SmallCrush, Crush or BigCrush -- these are other tests that are sometimes used for the same purpose. If you want to see what the output of Pracrand looks like, this website has output for all of the PRNGs that C++ supports as of C++20. \u6240\u4ee5\u6211\u4eec\u5e94\u8be5\u4f7f\u7528 Mersenne Twister\uff0c\u5bf9\u5417\uff1f Probably. For most applications, Mersenne Twister is fine, both in terms of performance and quality. However, it\u2019s worth noting that by modern PRNG standards, Mersenne Twister is a bit outdated . The biggest issue with Mersenne Twister is that its results can be predicted after seeing 624 generated numbers, making it non-suitable for any application that requires non-predictability. If you are developing an application that requires the highest quality random results (e.g. a statistical simulation), the fastest results, or one where non-predictability is important (e.g. cryptography), you\u2019ll need to use a 3rd party library. Popular choices as of the time of writing: The Xoshiro family and Wyrand for non-cryptographic PRNGs. The Chacha family for cryptographic (non-predictable) PRNGs. Okay, now that your eyes are probably bleeding, that\u2019s enough theory. Let\u2019s discuss how to actually generate random numbers with Mersenne Twister in C++.","title":"7.18 - \u968f\u673a\u6570\u751f\u6210\u5668\u7b80\u4ecb"},{"location":"7-18-introduction-to-random-number-generation/#_1","text":"First, let\u2019s take a detour through the concepts of algorithms and states. An algorithm is a finite sequence of instructions that can be followed to solve some problem or produce some useful result. For example, let\u2019s say your boss gives you a small text file containing a bunch of unsorted names (one per line), and asks you to sort the list. Since the list is small, and you don\u2019t expect to do this often, you decide to sort it by hand. There are multiple ways to sort a list, but you might do something like this: Create a new empty list to hold the sorted results Scan the list of unsorted names to find the name that comes first alphabetically Cut that name out of the unsorted list and paste it at the bottom of the sorted list Repeat the previous two steps until there are no more names on the unsorted list The above set of steps describes a sorting algorithm (using natural language). By nature, algorithms are reusable -- if your boss asks you to sort another list tomorrow, you can just apply the same algorithm to the new list. Because computers can execute instructions and manipulate data much more quickly than we can, algorithms are often written using programming languages, allowing us to automate tasks. In C++, algorithms are typically implemented as reusable functions. Here\u2019s a simple algorithm for generating a sequence of numbers where each successive number is incremented by 1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int plusOne () { static int s_state { 3 }; // only initialized the first time this function is called // Generate the next number ++ s_state ; // first we modify the state return s_state ; // then we use the new state to generate the next number in the sequence } int main () { std :: cout << plusOne () << '\\n' ; std :: cout << plusOne () << '\\n' ; std :: cout << plusOne () << '\\n' ; return 0 ; } COPY This prints: 1 2 3 4 5 6 This algorithm is pretty simple. The first time we call plusOne() , s_state is initialized to value 3 . Then the next number in the sequence is generated and returned. An algorithm is considered to be stateful if it retains some information across calls. Conversely, a stateless algorithm does not store any information (and must be given all the information it needs to work with when it is called). Our plusOne() function is stateful, in that it uses the static variable s_state to store the last number that was generated. When applied to algorithms, the term state refers to the current values held in stateful variables. To generate the next number in the sequence, our algorithm uses a two step process: First, the current state (initialized from the start value, or preserved from the prior call) is modified to produce a new state. Then, the next number in the sequence is generated from the new state. Our algorithm is considered deterministic, meaning that for a given input (the value provided for start ), it will always produce the same output sequence.","title":"\u7b97\u6cd5\u548c\u72b6\u6001"},{"location":"7-18-introduction-to-random-number-generation/#prngs","text":"To simulate randomness, programs typically use a pseudo-random number generator. A pseudo-random number generator (PRNG) is an algorithm that generates a sequence of numbers whose properties simulate a sequence of random numbers. It\u2019s easy to write a basic PRNG algorithm. Here\u2019s a short PRNG example that generates 100 16-bit pseudo-random numbers: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include // For illustrative purposes only, don't use this unsigned int LCG16 () // our PRNG { static unsigned int s_state { 5323 }; // Generate the next number // Due to our use of large constants and overflow, it would be // hard for someone to casually predict what the next number is // going to be from the previous one. s_state = 8253729 * s_state + 2396403 ; // first we modify the state return s_state % 32768 ; // then we use the new state to generate the next number in the sequence } int main () { // Print 100 random numbers for ( int count { 1 }; count <= 100 ; ++ count ) { std :: cout << LCG16 () << '\\t' ; // If we've printed 10 numbers, start a new row if ( count % 10 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY The result of this program is: 1 2 3 4 5 6 7 8 9 10 23070 27857 22756 10839 27946 11613 30448 21987 22070 1001 27388 5999 5442 28789 13576 28411 10830 29441 21780 23687 5466 2957 19232 24595 22118 14873 5932 31135 28018 32421 14648 10539 23166 22833 12612 28343 7562 18877 32592 19011 13974 20553 9052 15311 9634 27861 7528 17243 27310 8033 28020 24807 1466 26605 4992 5235 30406 18041 3980 24063 15826 15109 24984 15755 23262 17809 2468 13079 19946 26141 1968 16035 5878 7337 23484 24623 13826 26933 1480 6075 11022 19393 1492 25927 30234 17485 23520 18643 5926 21209 2028 16991 3634 30565 2552 20971 23358 12785 25092 30583 Each number appears to be pretty random with respect to the previous one. Notice how similar LCG16() is to our plusOne() example above! We can pass LCG16() an initial value that is used to initialize the state. Then to produce the next number in the output sequence, the current state is modified (by applying some mathematical operations) to produce a new state, and the next number in the sequence is generated from that new state. As it turns out, this particular algorithm isn\u2019t very good as a random number generator. But most PRNGs work similarly to LCG16() -- they just typically use more state variables and more complex mathematical operations in order to generate better quality results.","title":"\u4f2a\u968f\u673a\u6570\u751f\u6210\u5668 (PRNGs)"},{"location":"7-18-introduction-to-random-number-generation/#prng","text":"The sequence of \u201crandom numbers\u201d generated by a PRNG is not random at all. Just like our plusOne() function, LCG16() is also deterministic. Once the state has been initialized, LCG16() (and all other PRNGs) will generate the same output sequence. When a PRNG is instantiated, an initial value (or set of values) called a random seed (or seed for short) can be provided to initialize the state of the PRNG. When a PRNG has been initialized with a seed, we say it has been seeded. \u5173\u952e\u4fe1\u606f All of the values that a PRNG will produce are deterministically calculated from the seed value(s). Most PRNGs that produce quality results use at least 16 bytes of state, if not significantly more. However, the size of the seed value can be smaller than the size of the state of the PRNG. When this happens, we say the PRNG has been underseeded. Ideally, every bit in the state is initialized from a seed of equal size, and every bit in the seed has been independently determined somehow. However, if a PRNG is underseeded, some number of bits in the state will need to be initialized from the same bits in the seed. If a PRNG is significantly underseeded (meaning the size of the seed is much smaller than the size of the state), the quality of the random results the PRNG produces can be impacted.","title":"PRNG \u7684\u79cd\u5b50"},{"location":"7-18-introduction-to-random-number-generation/#prngprng","text":"In order to be a good PRNG, the PRNG needs to exhibit a number of properties: The PRNG should generate each number with approximately the same probability. This is called distribution uniformity. If some numbers are generated more often than others, the result of the program that uses the PRNG will be biased! To check distribution uniformity, we can use a histogram. A histogram is a graph that tracks how many times each number has been generated. Since our histograms are text-based, we\u2019ll use a * symbol to represent each time a given number was generated. Consider a PRNG that generates numbers between 1 and 6. If we generate 36 numbers, a PRNG with distribution uniformity should generate a histogram that looks something like this: 1 2 3 4 5 6 1|****** 2|****** 3|****** 4|****** 5|****** 6|****** A PRNG that is biased in some way will generate a histogram that is uneven, like this: 1 2 3 4 5 6 1|*** 2|****** 3|****** 4|****** 5|****** 6|********* or this: 1 2 3 4 5 6 1|**** 2|******** 3|****** 4|******** 5|****** 6|**** Let\u2019s say you\u2019re trying to write a random item generator for a game. When a monster is killed, your code generates a random number between 1 and 6, and if the result is a 6, the monster will drop a rare item instead of a common one. You would expect a 1 in 6 chance of this happening. But if the underlying PRNG is not uniform, and generates a lot more 6s than it should (like the second histogram above), your players will end up getting more rare items than you\u2019d intended, possibly trivializing the difficulty of your game, or messing up your in-game economy. Finding PRNG algorithms that produce uniform results is difficult. The method by which the next number in the sequence is generated shouldn\u2019t be predictable. For example, consider the following PRNG algorithm: return ++num . This PRNG is perfectly uniform, but it is also completely predictable -- and not very useful as a sequence of random numbers! Even sequences of numbers that seem random to the eye (such as the output of LCG16() above) may be trivially predictable by someone who is motivated. By examining just a few numbers generated from the LCG16() function above, it is possible to determine which constants are used ( 8253729 and 2396403 ) to modify the state. Once that is known, it becomes trivial to calculate all of the future numbers that will be generated from this PRNG. Now, imagine you\u2019re running a betting website where users can bet $100. Your website then generates a random number between 0 and 32767. If the number is greater than 20000, the customer wins and you pay them double. Otherwise, they lose. Since the customer wins only 12767/32767 (39%) of the time, your website should make tons of money, right? However, if customers are able to determine which numbers will be generated next, then they can strategically place bets so they always (or usually) win. Congrats, now you get to file for bankruptcy! The PRNG should have a good dimensional distribution of numbers. This means the PRNG should return numbers across the entire range of possible results at random. For example, the PRNG should generate low numbers, middle numbers, high numbers, even numbers, and odd numbers seemingly at random. A PRNG that returned all low numbers, then all high numbers may be uniform and non-predictable, but it\u2019s still going to lead to biased results, particularly if the number of random numbers you actually use is small. The PRNG should have a high period for all seeds All PRNGs are periodic, which means that at some point the sequence of numbers generated will begin to repeat itself. The length of the sequence before a PRNG begins to repeat itself is known as the period. For example, here are the first 100 numbers generated from a PRNG with poor periodicity: 1 2 3 4 5 6 7 8 9 10 112 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 You will note that it generated 9 as the 2nd number, again as the 16th number, and then every 14 numbers after that. This PRNG is stuck generating the following sequence repeatedly: 9-130-97-64-31-152-119-86-53-20-141-108-75-42-(repeat) . This happens because PRNGs are deterministic. Once the state of a PRNG is identical to a prior state, the PRNG will start producing the same sequence of outputs it has produced before -- resulting in a loop. A good PRNG should have a long period for all seed numbers. Designing an algorithm that meets this property can be extremely difficult -- many PRNGs have long periods only for some seeds and not others. If the user happens to pick a seed that results in a state with a short period, then the PRNG won\u2019t do a good job if many random numbers are needed. The PRNG should be efficient Most PRNGs have a state size of less than 4096 bytes, so total memory usage typically isn\u2019t a concern. However, the larger the internal state, the more likely the PRNG is to be underseeded, and the slower the initial seeding will be (since there\u2019s more state to initialize). Second, to generate the next number in sequence, a PRNG has to mix up its internal state by applying various mathematical operations. How much time this takes can vary significantly by PRNG and also by architecture (some PRNGs perform better on certain architectures than others). This doesn\u2019t matter if you only generate random numbers periodically, but can have a huge impact if you need lots of randomness.","title":"\u4ec0\u4e48\u6837\u7684PRNG\u662f\u597dPRNG\uff1f(\u9009\u8bfb)"},{"location":"7-18-introduction-to-random-number-generation/#prng_1","text":"Over the years, many different kinds of PRNG algorithms have been developed (Wikipedia has a good list here ). Every PRNG algorithm has strengths and weaknesses that might make it more or less suitable for a particular applications, so selecting the right algorithm for your application is important. Many PRNGs are now considered relatively poor by modern standards -- and there\u2019s no reason to use a PRNG that doesn\u2019t perform well when it\u2019s just as easy to use one that does.","title":"\u591a\u79cd\u4e0d\u540c\u7684 PRNG \u7b97\u6cd5"},{"location":"7-18-introduction-to-random-number-generation/#c","text":"The randomization capabilities in C++ are accessible via the header of the standard library. Within the random library, there are 6 PRNG families available for use (as of C++20): Type name Family Period State size* Performance Quality Should I use this? minstd_rand minstd_rand0 Linear congruential generator 2^31 4 bytes Bad Awful No mt19937 mt19937_64 Mersenne twister 2^19937 2500 bytes Decent Decent Probably (see next section) ranlux24 ranlux48 Subtract and carry 10^171 96 bytes Awful Good No knuth_b Shuffled linear congruential generator 2^31 1028 bytes Awful Bad No default_random_engine Any of above (implementation defined) Varies Varies ? ? No2 rand() Linear congruential generator 2^31 4 bytes Bad Awful Nono There is zero reason to use knuth_b , default_random_engine , or rand() (which is a random number generator provided for compatibility with C). As of C++20, the Mersenne Twister algorithm is the only PRNG that ships with C++ that has both decent performance and quality. \u6269\u5c55\u9605\u8bfb A test called PracRand is often used to assess the performance and quality of PRNGs (to determine whether they have different kinds of biases). You may also see references to SmallCrush, Crush or BigCrush -- these are other tests that are sometimes used for the same purpose. If you want to see what the output of Pracrand looks like, this website has output for all of the PRNGs that C++ supports as of C++20.","title":"C++\u00a0\u4e2d\u7684\u968f\u673a\u5316"},{"location":"7-18-introduction-to-random-number-generation/#mersenne-twister","text":"Probably. For most applications, Mersenne Twister is fine, both in terms of performance and quality. However, it\u2019s worth noting that by modern PRNG standards, Mersenne Twister is a bit outdated . The biggest issue with Mersenne Twister is that its results can be predicted after seeing 624 generated numbers, making it non-suitable for any application that requires non-predictability. If you are developing an application that requires the highest quality random results (e.g. a statistical simulation), the fastest results, or one where non-predictability is important (e.g. cryptography), you\u2019ll need to use a 3rd party library. Popular choices as of the time of writing: The Xoshiro family and Wyrand for non-cryptographic PRNGs. The Chacha family for cryptographic (non-predictable) PRNGs. Okay, now that your eyes are probably bleeding, that\u2019s enough theory. Let\u2019s discuss how to actually generate random numbers with Mersenne Twister in C++.","title":"\u6240\u4ee5\u6211\u4eec\u5e94\u8be5\u4f7f\u7528 Mersenne Twister\uff0c\u5bf9\u5417\uff1f"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/","text":"Key Takeaway In the previous lesson 7.18 -- Introduction to random number generation , we introduced the concept of random number generation, and discussed how PRNG algorithms are typically used to simulate randomness in programs. In this lesson, we\u2019ll take a look at how to generate random numbers in your programs. To access any of the randomization capabilities in C++, we include the header of the standard library. \u5728 C++ \u4e2d\u4f7f\u7528 Mersenne Twister \u751f\u6210\u968f\u673a\u6570 The Mersenne Twister PRNG, besides having a great name, is probably the most popular PRNG across all programming languages. Although it is a bit old by today\u2019s standards, it generally produces quality results and has decent performance. The random library has support for two Mersenne Twister types: mt19937 is a Mersenne Twister that generates 32-bit unsigned integers mt19937_64 is a Mersenne Twister that generates 64-bit unsigned integers Using Mersenne Twister is straightforward: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include // for std::mt19937 int main () { std :: mt19937 mt ; // Instantiate a 32-bit Mersenne Twister // Print a bunch of random numbers for ( int count { 1 }; count <= 40 ; ++ count ) { std :: cout << mt () << '\\t' ; // generate a random number // If we've printed 5 numbers, start a new row if ( count % 5 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY This produces the result: 1 2 3 4 5 6 7 8 3499211612 581869302 3890346734 3586334585 545404204 4161255391 3922919429 949333985 2715962298 1323567403 418932835 2350294565 1196140740 809094426 2348838239 4264392720 4112460519 4279768804 4144164697 4156218106 676943009 3117454609 4168664243 4213834039 4111000746 471852626 2084672536 3427838553 3437178460 1275731771 609397212 20544909 1811450929 483031418 3933054126 2747762695 3402504553 3772830893 4120988587 2163214728 First, we include the header, since that\u2019s where all the random number capabilities live. Next, we instantiate a 32-bit Mersenne Twister engine via the statement std::mt19937 mt . Then, each time we want to generate a random 32-bit unsigned integer, we call mt() . \u5c0f\u8d34\u58eb Since mt is a variable, you may be wondering what mt() means. In lesson 4.17 -- Introduction to std::string , we showed an example where we called the function name.length() , which invoked the length() function on std::string variable name . mt() is a concise syntax for calling the function mt.operator() , which for these PRNG types has been defined to return the next random result in the sequence. The advantage of using operator() instead of a named function is that we don\u2019t need to remember the function\u2019s name, and the concise syntax is less typing. \u4f7f\u7528 Mersenne Twister \u6295\u63b7\u9ab0\u5b50 A 32-bit PRNG will generate random numbers between 0 and 4,294,967,295, but we do not always want numbers in that range. If our program was simulating a board game or a dice game, we\u2019d probably want to simulate the roll of a 6-sided dice by generating random numbers between 1 and 6. If our program was a dungeon adventure, and the player had a sword that did between 7 and 11 damage to monsters, then we\u2019d want to generate random numbers between 7 and 11 whenever the player hit a monster. Unfortunately, PRNGs can\u2019t do this. They can only generate numbers that use the full range. What we need is some way to convert a number that is output from our PRNG into a value in the smaller range we want (with an even probability of each value occurring). While we could write a function to do this ourselves, doing so in a way that produces non-biased results is non-trivial. Fortunately, the random library can help us here, in the form of random number distributions. A random number distribution converts the output of a PRNG into some other distribution of numbers. \u9898\u5916\u8bdd For the stats geeks: a random number distribution is just a probability distribution designed to take PRNG values as input. The random library has many random numbers distributions, most of which you will never use unless you\u2019re doing some kind of statistical analysis. But there\u2019s one random number distribution that\u2019s extremely useful: a uniform distribution is a random number distribution that produces outputs between two numbers X and Y (inclusive) with equal probability. Here\u2019s a similar program to the one above, using a uniform distribution to simulate the roll of a 6-sided dice: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include // for std::mt19937 and std::uniform_int_distribution int main () { std :: mt19937 mt ; // Create a reusable random number generator that generates uniform numbers between 1 and 6 std :: uniform_int_distribution die6 { 1 , 6 }; // for C++14, use std::uniform_int_distribution<> die6{ 1, 6 }; // Print a bunch of random numbers for ( int count { 1 }; count <= 40 ; ++ count ) { std :: cout << die6 ( mt ) << '\\t' ; // generate a roll of the die here // If we've printed 10 numbers, start a new row if ( count % 10 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY This produces the result: 1 2 3 4 3 1 3 6 5 2 6 6 1 2 2 6 1 1 6 1 4 5 2 5 6 2 6 2 1 3 5 4 5 6 1 4 2 3 1 2 2 6 2 1 There are only two noteworthy differences in this example compared to the previous one. First, we\u2019ve created a uniform distribution variable (named die6 ) to generate numbers between 1 and 6. Second, instead of calling mt() to generate 32-bit unsigned integer random numbers, we\u2019re now calling die6(mt) to generate a value between 1 and 6. \u4e0a\u9762\u7684\u7a0b\u5e8f\u5e76\u4e0d\u50cf\u5b83\u770b\u4e0a\u53bb\u90a3\u6837\u968f\u673a Although the results of our dice rolling example above are pretty random, there\u2019s a major flaw with the program. Run the program 3 times and see if you can figure out what it is. Go ahead, we\u2019ll wait. Jeopardy music If you run the program multiple times, you will note that it prints the same numbers every time! While each number in the sequence is random with regards to the previous one, the entire sequence is not random at all! Each run of our program produces the exact same result. Imagine that you\u2019re writing a game of hi-lo, where the user has 10 tries to guess a number that has been picked randomly, and the computer tells the user whether their guess is too high or too low. If the computer picks the same random number every time, the game won\u2019t be interesting past the first time it is played. So let\u2019s take a deeper look at why this is happening, and how we can fix it. In the prior lesson ( 7.18 - \u968f\u673a\u6570\u751f\u6210\u5668\u7b80\u4ecb ), we covered that each number in a PRNG sequence is in a deterministic way. And that the state of the PRNG is initialized from the seed value. Thus, given any starting seed number, PRNGs will always generate the same sequence of numbers from that seed as a result. Because we are default initializing our Mersenne Twister, it is being initialized with the same seed every time the program is run. And because the seed is the same, the random numbers being generated are also the same. In order to make our entire sequence randomized differently each time the program is run, we need to pick a seed that\u2019s not a fixed number. The first answer that probably comes to mind is that we need a random number for our seed! That\u2019s a good thought, but if we need a random number to generate random numbers, then we\u2019re in a catch-22. It turns out, we really don\u2019t need our seed to be a random number -- we just need to pick something that changes each time the program is run. Then we can use our PRNG to generate a unique sequence of pseudo-random numbers from that seed. There are two methods that are commonly used to do this: Use the system clock Use the system\u2019s random device \u4f7f\u7528\u7cfb\u7edf\u65f6\u95f4\u4f5c\u4e3a\u968f\u673a\u6570\u79cd\u5b50 What\u2019s one thing that\u2019s different every time you run your program? Unless you manage to run your program twice at exactly the same moment in time, the answer is that the current time is different. Therefore, if we use the current time as our seed value, then our program will produce a different set of random numbers each time it is run. C and C++ have a long history of PRNGs being seeded using the current time (using the std::time() function), so you will probably see this in a lot of existing code. Fortunately, C++ has a high resolution clock that we can use to generate a seed value. To minimize the chance of two time values being identical if the program is run quickly in succession, we want to use some time measure that changes as quickly as possible. For this, we\u2019ll ask the clock how much time has passed since the earliest time it can measure. This time is measured in \u201cticks\u201d, which is a very small unit of time (usually nanoseconds, but could be milliseconds). 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include // for std::mt19937 #include // for std::chrono int main () { // Seed our Mersenne Twister using the std :: mt19937 mt { static_cast < unsigned int > ( std :: chrono :: steady_clock :: now (). time_since_epoch (). count () ) }; // Create a reusable random number generator that generates uniform numbers between 1 and 6 std :: uniform_int_distribution die6 { 1 , 6 }; // for C++14, use std::uniform_int_distribution<> die6{ 1, 6 }; // Print a bunch of random numbers for ( int count { 1 }; count <= 40 ; ++ count ) { std :: cout << die6 ( mt ) << '\\t' ; // generate a roll of the die here // If we've printed 10 numbers, start a new row if ( count % 10 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY The above program has only two changes from the prior. First, we\u2019re including , which gives us access to the clock. Second, we\u2019re using the current time from the clock as a seed value for our Mersenne Twister. The results generated by this program should now be different each time it is run, which you can verify experimentally by running it several times. The downside of this approach is that if the program is run several times in quick succession, the seeds generated for each run won\u2019t be that different, which can impact the quality of the random results from a statistical standpoint. For normal programs, this doesn\u2019t matter, but for programs that require high quality, independent results, this method of seeding may be insufficient. \u5c0f\u8d34\u58eb std::chrono::high_resolution_clock is a popular choice instead of std::chrono::steady_clock . std::chrono::high_resolution_clock is the clock that uses the most granular unit of time, but it may use the system clock for the current time, which can be changed or rolled back by users. std::chrono::steady_clock may have a less granular tick time, but is the only clock with a guarantee that users can not adjust it. \u4f7f\u7528\u968f\u673a\u6570\u8bbe\u5907\u751f\u6210\u79cd\u5b50 The random library contains a type called std::random_device that is an implementation-defined PRNG. Normally we avoid implementation-defined capabilities because they have no guarantees about quality or portability, but this is one of the exception cases. Typically std::random_device will ask the OS for a random number (how it does this depends on the OS). 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include // for std::mt19937 and std::random_device int main () { std :: mt19937 mt { std :: random_device {}() }; // Create a reusable random number generator that generates uniform numbers between 1 and 6 std :: uniform_int_distribution die6 { 1 , 6 }; // for C++14, use std::uniform_int_distribution<> die6{ 1, 6 }; // Print a bunch of random numbers for ( int count { 1 }; count <= 40 ; ++ count ) { std :: cout << die6 ( mt ) << '\\t' ; // generate a roll of the die here // If we've printed 10 numbers, start a new row if ( count % 10 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY In the above program, we\u2019re seeding our Mersenne Twister with one random number generated from a temporary instance of std::random_device . If you run this program multiple times, it should also produce different results each time. One potential problem with std::random_device : it isn\u2019t required to be non-deterministic, meaning it could , on some systems, produce the same sequence every time the program is run, which is exactly what we\u2019re trying to avoid. There was a bug in MinGW (fixed in GCC 9.2) that would do exactly this, making std::random_device useless. However, the latest versions of the most popular compilers (GCC/MinGW, Clang, Visual Studio) support proper implementations of std::random_device . \u6700\u4f73\u5b9e\u8df5 Use std::random_device to seed your PRNGs (unless it\u2019s not implemented properly for your target compiler/architecture). Q: std::random_device{}() \u662f\u4ec0\u4e48\u610f\u601d? std::random_device{} creates an value-initialized temporary object of type std::random_device . The () then calls operator() on that temporary object, which returns a randomized value (which we use as an initializer for our Mersenne Twister) It\u2019s the equivalent of the calling the following function, which uses a syntax you should be more familiar with: 1 2 3 4 5 unsigned int getRandomDeviceValue () { std :: random_device rd {}; // create a value initialized std::random_device object return rd (); // return the result of operator() to the caller } COPY Using std::random_device{}() allows us to get the same result without creating a named function or named variable, so it\u2019s much more concise. Q: \u5982\u679c std::random_device \u672c\u8eab\u662f\u968f\u673a\u7684\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u76f4\u63a5\u7528\u5b83\u4ee3\u66ff Mersenne Twister \u5462\uff1f Because std::random_device is implementation defined, we can\u2019t assume much about it. It may be expensive to access or it may cause our program to pause while waiting for more random numbers to become available. The pool of numbers that it draws from may also be depleted quickly, which would impact the random results for other applications requesting random numbers via the same method. For this reason, std::random_device is better used to seed other PRNGs rather than as a PRNG itself. Only seed a PRNG once Many PRNGs can be reseeded after the initial seeding. This essentially re-initializes the state of the random number generator, causing it to generate results starting from the new seed state. Reseeding should generally be avoided unless you have a specific reason to do so, as it can cause the results to be less random, or not random at all. \u6700\u4f73\u5b9e\u8df5 Only seed a given pseudo-random number generator once, and do not reseed it. Here\u2019s an example of a common mistake that new programmers make: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include int getCard () { std :: mt19937 mt { std :: random_device {}() }; // this gets created and seeded every time the function is called std :: uniform_int_distribution card { 1 , 52 }; return card ( mt ); } int main () { std :: cout << getCard (); return 0 ; } COPY In the getCard() function, the random number generator is being created and seeded every time the function is called. This is inefficient at best, and will likely cause poor random results. \u8de8\u51fd\u6570\u7684\u968f\u673a\u6570 What happens if we want to use a random number generator in multiple functions? One way is to create (and seed) our PRNG in our main() function, and then pass it everywhere we need it. But that\u2019s a lot of passing for something we may only use sporadically, and in different places. Although you can create a static local std::mt19937 variable in each function that needs it (static so that it only gets seeded once), it\u2019s overkill to have every function that uses a random number generator define and seed its own local generator. A better option in most cases is to create a global random number generator (inside a namespace!). Remember how we told you to avoid non-const global variables? This is an exception. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include // for std::mt19937 and std::random_device namespace Random // capital R to avoid conflicts with functions named random() { std :: mt19937 mt { std :: random_device {}() }; int get ( int min , int max ) { std :: uniform_int_distribution die { min , max }; // we can create a distribution in any function that needs it return die ( mt ); // and then generate a random number from our global generator } } int main () { std :: cout << Random :: get ( 1 , 6 ) << '\\n' ; std :: cout << Random :: get ( 1 , 10 ) << '\\n' ; std :: cout << Random :: get ( 1 , 20 ) << '\\n' ; return 0 ; } COPY In the above example, Random::mt is a global variable that can be accessed from any function. We\u2019ve created Random::get() as an easy way to get a random number between min and max . std::uniform_int_distribution is typically cheap to create, so it\u2019s fine to create when we need it. Mersenne Twister \u548c underseeding issues The internal state of a Mersenne Twister is 624 bytes in size. In the examples above, where we seed from the clock or std::random_device , our seed is only a single 32-bit integer. This means we\u2019re essentially initializing a 624-byte object with a 4-byte value, which is significantly underseeding the Mersenne Twister PRNG. The random library does the best it can to fill in the remaining 620 bytes with \u201crandom\u201d data\u2026 but it can\u2019t work magic. Underseeded PRNG can generate results that are suboptimal for applications that need the highest quality results. For example, seeding std::mt19937 with a single 32-bit value will never generate the number 42 as its first output. So how do we fix this? As of C++20, there\u2019s no easy way. But we do have some suggestions. First, let\u2019s talk about std::seed_seq (which stands for \u201cseed sequence\u201d). In the prior lesson, we mentioned that a seed can be either a single value, or a set of values. std::seed_seq is a type that performs two functions. First, it can hold multiple seed values, so we can use it to seed our PRNG with more than one value. Second, it will generate as many additional unbiased seed values as needed to initialize a PRNG\u2019s state. So if you initialize std::seed_seq with a single 32-bit integer (e.g. from std::random_device) and then initialize a Mersenne Twister with the std::seed_seq object, std::seed_seq will generate 620 bytes of additional seed data. The results won\u2019t be amazingly high quality, but it\u2019s better than nothing. Now on to the ideas. First, the more pieces of random data we can give std::seed_seq to work with, the better. So the easiest idea is to simply use std::random_device to give std::seed_seq more data to work with. If we initialize std::seed_seq with 8 numbers from std::random_device instead of 1, then the remaining numbers generated by std::seed_seq should be that much better: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include int main () { std :: random_device rd ; std :: seed_seq ss { rd (), rd (), rd (), rd (), rd (), rd (), rd (), rd () }; // get 8 integers of random numbers from std::random_device for our seed std :: mt19937 mt { ss }; // initialize our Mersenne Twister with the std::seed_seq // Create a reusable random number generator that generates uniform numbers between 1 and 6 std :: uniform_int_distribution die6 { 1 , 6 }; // for C++14, use std::uniform_int_distribution<> die6{ 1, 6 }; // Print a bunch of random numbers for ( int count { 1 }; count <= 40 ; ++ count ) { std :: cout << die6 ( mt ) << '\\t' ; // generate a roll of the die here // If we've printed 10 numbers, start a new row if ( count % 10 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY This is pretty straightforward so there isn\u2019t much reason not to do this at a minimum. Q: Why not give std::seed_seq 156 integers (624 bytes) from std::random_device ? You can! However, this may be slow, and risks depleting the pool of random numbers that std::random_device uses. Second, you can use other \u201crandom\u201d inputs to std::seed_seq . We\u2019ve already shown you how to get a value from the clock, so you can throw that in easily. Other things that are sometimes used include the current thread id,the address of particular functions, the user\u2019s id, the process id, etc\u2026 Doing that is beyond the scope of this article, but this article has some context and a link to randutils.hpp that implements this. An alternate path is to use a different PRNG with a smaller state. Many good PRNGs use 64 or 128 bits of state, which can easily be initialized using std::seed_seq filled with 8 calls to std::random_device . PRNG \u70ed\u8eab When a PRNG is given a poor quality seed (or underseeded), the initial results of the PRNG may not be high quality. For this reason, some PRNGs benefit from being \u201cwarmed up\u201d, which is a technique where the first N results generated from the PRNG are discarded. This allows the internal state of the PRNG to be mixed up such that the subsequent results should be of higher quality. Typically a few hundred to a few thousand initial results are discarded. The longer the period of the PRNG, the more initial results should be discarded. \u9898\u5916\u8bdd Visual Studio\u2019s implementation of rand() had (or still has?) a bug where the first generated result would not be sufficiently randomized. You may see older programs that use rand() discard a single result as a way to avoid this issue. The seed_seq initialization used by std::mt19937 performs a warm up, so we don\u2019t need to explicitly warm up std::mt19937 objects. \u4f7f\u7528\u968f\u673a\u6570\u5e26\u6765\u7684debug\u96be\u9898 Programs that use random numbers can be difficult to debug because the program may exhibit different behaviors each time it is run. Sometimes it may work, and sometimes it may not. When debugging, it\u2019s helpful to ensure your program executes the same (incorrect) way each time. That way, you can run the program as many times as needed to isolate where the error is. For this reason, when debugging, it\u2019s a useful technique to seed your PRNG with a specific value (e.g. 5 ) that causes the erroneous behavior to occur. This will ensure your program generates the same results each time, making debugging easier. Once you\u2019ve found the error, you can use your normal seeding method to start generating randomized results again. \u968f\u673a\u5316 FAQ Q: Help! My random number generator is generating the same sequence of random numbers. If your random number generator is generating the same sequence of random numbers every time your program is run, you probably didn\u2019t seed it properly (or at all). Make sure you\u2019re seeding it with a value that changes each time the program is run. Q: Help! My random number generator keeps generating the same number over and over. If your random number generator is generating the same number every time you ask it for a random number, then you are probably either reseeding the random number generator before generating a random number, or you\u2019re creating a new random generator for each random number.","title":"7.19 - \u4f7f\u7528 Mersenne Twister \u751f\u6210\u968f\u673a\u6570"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#c-mersenne-twister","text":"The Mersenne Twister PRNG, besides having a great name, is probably the most popular PRNG across all programming languages. Although it is a bit old by today\u2019s standards, it generally produces quality results and has decent performance. The random library has support for two Mersenne Twister types: mt19937 is a Mersenne Twister that generates 32-bit unsigned integers mt19937_64 is a Mersenne Twister that generates 64-bit unsigned integers Using Mersenne Twister is straightforward: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include // for std::mt19937 int main () { std :: mt19937 mt ; // Instantiate a 32-bit Mersenne Twister // Print a bunch of random numbers for ( int count { 1 }; count <= 40 ; ++ count ) { std :: cout << mt () << '\\t' ; // generate a random number // If we've printed 5 numbers, start a new row if ( count % 5 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY This produces the result: 1 2 3 4 5 6 7 8 3499211612 581869302 3890346734 3586334585 545404204 4161255391 3922919429 949333985 2715962298 1323567403 418932835 2350294565 1196140740 809094426 2348838239 4264392720 4112460519 4279768804 4144164697 4156218106 676943009 3117454609 4168664243 4213834039 4111000746 471852626 2084672536 3427838553 3437178460 1275731771 609397212 20544909 1811450929 483031418 3933054126 2747762695 3402504553 3772830893 4120988587 2163214728 First, we include the header, since that\u2019s where all the random number capabilities live. Next, we instantiate a 32-bit Mersenne Twister engine via the statement std::mt19937 mt . Then, each time we want to generate a random 32-bit unsigned integer, we call mt() . \u5c0f\u8d34\u58eb Since mt is a variable, you may be wondering what mt() means. In lesson 4.17 -- Introduction to std::string , we showed an example where we called the function name.length() , which invoked the length() function on std::string variable name . mt() is a concise syntax for calling the function mt.operator() , which for these PRNG types has been defined to return the next random result in the sequence. The advantage of using operator() instead of a named function is that we don\u2019t need to remember the function\u2019s name, and the concise syntax is less typing.","title":"\u5728 C++ \u4e2d\u4f7f\u7528 Mersenne Twister \u751f\u6210\u968f\u673a\u6570"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#mersenne-twister","text":"A 32-bit PRNG will generate random numbers between 0 and 4,294,967,295, but we do not always want numbers in that range. If our program was simulating a board game or a dice game, we\u2019d probably want to simulate the roll of a 6-sided dice by generating random numbers between 1 and 6. If our program was a dungeon adventure, and the player had a sword that did between 7 and 11 damage to monsters, then we\u2019d want to generate random numbers between 7 and 11 whenever the player hit a monster. Unfortunately, PRNGs can\u2019t do this. They can only generate numbers that use the full range. What we need is some way to convert a number that is output from our PRNG into a value in the smaller range we want (with an even probability of each value occurring). While we could write a function to do this ourselves, doing so in a way that produces non-biased results is non-trivial. Fortunately, the random library can help us here, in the form of random number distributions. A random number distribution converts the output of a PRNG into some other distribution of numbers. \u9898\u5916\u8bdd For the stats geeks: a random number distribution is just a probability distribution designed to take PRNG values as input. The random library has many random numbers distributions, most of which you will never use unless you\u2019re doing some kind of statistical analysis. But there\u2019s one random number distribution that\u2019s extremely useful: a uniform distribution is a random number distribution that produces outputs between two numbers X and Y (inclusive) with equal probability. Here\u2019s a similar program to the one above, using a uniform distribution to simulate the roll of a 6-sided dice: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include // for std::mt19937 and std::uniform_int_distribution int main () { std :: mt19937 mt ; // Create a reusable random number generator that generates uniform numbers between 1 and 6 std :: uniform_int_distribution die6 { 1 , 6 }; // for C++14, use std::uniform_int_distribution<> die6{ 1, 6 }; // Print a bunch of random numbers for ( int count { 1 }; count <= 40 ; ++ count ) { std :: cout << die6 ( mt ) << '\\t' ; // generate a roll of the die here // If we've printed 10 numbers, start a new row if ( count % 10 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY This produces the result: 1 2 3 4 3 1 3 6 5 2 6 6 1 2 2 6 1 1 6 1 4 5 2 5 6 2 6 2 1 3 5 4 5 6 1 4 2 3 1 2 2 6 2 1 There are only two noteworthy differences in this example compared to the previous one. First, we\u2019ve created a uniform distribution variable (named die6 ) to generate numbers between 1 and 6. Second, instead of calling mt() to generate 32-bit unsigned integer random numbers, we\u2019re now calling die6(mt) to generate a value between 1 and 6.","title":"\u4f7f\u7528 Mersenne Twister \u6295\u63b7\u9ab0\u5b50"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#_1","text":"Although the results of our dice rolling example above are pretty random, there\u2019s a major flaw with the program. Run the program 3 times and see if you can figure out what it is. Go ahead, we\u2019ll wait. Jeopardy music If you run the program multiple times, you will note that it prints the same numbers every time! While each number in the sequence is random with regards to the previous one, the entire sequence is not random at all! Each run of our program produces the exact same result. Imagine that you\u2019re writing a game of hi-lo, where the user has 10 tries to guess a number that has been picked randomly, and the computer tells the user whether their guess is too high or too low. If the computer picks the same random number every time, the game won\u2019t be interesting past the first time it is played. So let\u2019s take a deeper look at why this is happening, and how we can fix it. In the prior lesson ( 7.18 - \u968f\u673a\u6570\u751f\u6210\u5668\u7b80\u4ecb ), we covered that each number in a PRNG sequence is in a deterministic way. And that the state of the PRNG is initialized from the seed value. Thus, given any starting seed number, PRNGs will always generate the same sequence of numbers from that seed as a result. Because we are default initializing our Mersenne Twister, it is being initialized with the same seed every time the program is run. And because the seed is the same, the random numbers being generated are also the same. In order to make our entire sequence randomized differently each time the program is run, we need to pick a seed that\u2019s not a fixed number. The first answer that probably comes to mind is that we need a random number for our seed! That\u2019s a good thought, but if we need a random number to generate random numbers, then we\u2019re in a catch-22. It turns out, we really don\u2019t need our seed to be a random number -- we just need to pick something that changes each time the program is run. Then we can use our PRNG to generate a unique sequence of pseudo-random numbers from that seed. There are two methods that are commonly used to do this: Use the system clock Use the system\u2019s random device","title":"\u4e0a\u9762\u7684\u7a0b\u5e8f\u5e76\u4e0d\u50cf\u5b83\u770b\u4e0a\u53bb\u90a3\u6837\u968f\u673a"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#_2","text":"What\u2019s one thing that\u2019s different every time you run your program? Unless you manage to run your program twice at exactly the same moment in time, the answer is that the current time is different. Therefore, if we use the current time as our seed value, then our program will produce a different set of random numbers each time it is run. C and C++ have a long history of PRNGs being seeded using the current time (using the std::time() function), so you will probably see this in a lot of existing code. Fortunately, C++ has a high resolution clock that we can use to generate a seed value. To minimize the chance of two time values being identical if the program is run quickly in succession, we want to use some time measure that changes as quickly as possible. For this, we\u2019ll ask the clock how much time has passed since the earliest time it can measure. This time is measured in \u201cticks\u201d, which is a very small unit of time (usually nanoseconds, but could be milliseconds). 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include // for std::mt19937 #include // for std::chrono int main () { // Seed our Mersenne Twister using the std :: mt19937 mt { static_cast < unsigned int > ( std :: chrono :: steady_clock :: now (). time_since_epoch (). count () ) }; // Create a reusable random number generator that generates uniform numbers between 1 and 6 std :: uniform_int_distribution die6 { 1 , 6 }; // for C++14, use std::uniform_int_distribution<> die6{ 1, 6 }; // Print a bunch of random numbers for ( int count { 1 }; count <= 40 ; ++ count ) { std :: cout << die6 ( mt ) << '\\t' ; // generate a roll of the die here // If we've printed 10 numbers, start a new row if ( count % 10 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY The above program has only two changes from the prior. First, we\u2019re including , which gives us access to the clock. Second, we\u2019re using the current time from the clock as a seed value for our Mersenne Twister. The results generated by this program should now be different each time it is run, which you can verify experimentally by running it several times. The downside of this approach is that if the program is run several times in quick succession, the seeds generated for each run won\u2019t be that different, which can impact the quality of the random results from a statistical standpoint. For normal programs, this doesn\u2019t matter, but for programs that require high quality, independent results, this method of seeding may be insufficient. \u5c0f\u8d34\u58eb std::chrono::high_resolution_clock is a popular choice instead of std::chrono::steady_clock . std::chrono::high_resolution_clock is the clock that uses the most granular unit of time, but it may use the system clock for the current time, which can be changed or rolled back by users. std::chrono::steady_clock may have a less granular tick time, but is the only clock with a guarantee that users can not adjust it.","title":"\u4f7f\u7528\u7cfb\u7edf\u65f6\u95f4\u4f5c\u4e3a\u968f\u673a\u6570\u79cd\u5b50"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#_3","text":"The random library contains a type called std::random_device that is an implementation-defined PRNG. Normally we avoid implementation-defined capabilities because they have no guarantees about quality or portability, but this is one of the exception cases. Typically std::random_device will ask the OS for a random number (how it does this depends on the OS). 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include // for std::mt19937 and std::random_device int main () { std :: mt19937 mt { std :: random_device {}() }; // Create a reusable random number generator that generates uniform numbers between 1 and 6 std :: uniform_int_distribution die6 { 1 , 6 }; // for C++14, use std::uniform_int_distribution<> die6{ 1, 6 }; // Print a bunch of random numbers for ( int count { 1 }; count <= 40 ; ++ count ) { std :: cout << die6 ( mt ) << '\\t' ; // generate a roll of the die here // If we've printed 10 numbers, start a new row if ( count % 10 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY In the above program, we\u2019re seeding our Mersenne Twister with one random number generated from a temporary instance of std::random_device . If you run this program multiple times, it should also produce different results each time. One potential problem with std::random_device : it isn\u2019t required to be non-deterministic, meaning it could , on some systems, produce the same sequence every time the program is run, which is exactly what we\u2019re trying to avoid. There was a bug in MinGW (fixed in GCC 9.2) that would do exactly this, making std::random_device useless. However, the latest versions of the most popular compilers (GCC/MinGW, Clang, Visual Studio) support proper implementations of std::random_device . \u6700\u4f73\u5b9e\u8df5 Use std::random_device to seed your PRNGs (unless it\u2019s not implemented properly for your target compiler/architecture).","title":"\u4f7f\u7528\u968f\u673a\u6570\u8bbe\u5907\u751f\u6210\u79cd\u5b50"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#q-stdrandom_device","text":"std::random_device{} creates an value-initialized temporary object of type std::random_device . The () then calls operator() on that temporary object, which returns a randomized value (which we use as an initializer for our Mersenne Twister) It\u2019s the equivalent of the calling the following function, which uses a syntax you should be more familiar with: 1 2 3 4 5 unsigned int getRandomDeviceValue () { std :: random_device rd {}; // create a value initialized std::random_device object return rd (); // return the result of operator() to the caller } COPY Using std::random_device{}() allows us to get the same result without creating a named function or named variable, so it\u2019s much more concise.","title":"Q: \u00a0std::random_device{}()\u00a0\u662f\u4ec0\u4e48\u610f\u601d?"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#q-stdrandom_device-mersenne-twister","text":"Because std::random_device is implementation defined, we can\u2019t assume much about it. It may be expensive to access or it may cause our program to pause while waiting for more random numbers to become available. The pool of numbers that it draws from may also be depleted quickly, which would impact the random results for other applications requesting random numbers via the same method. For this reason, std::random_device is better used to seed other PRNGs rather than as a PRNG itself. Only seed a PRNG once Many PRNGs can be reseeded after the initial seeding. This essentially re-initializes the state of the random number generator, causing it to generate results starting from the new seed state. Reseeding should generally be avoided unless you have a specific reason to do so, as it can cause the results to be less random, or not random at all. \u6700\u4f73\u5b9e\u8df5 Only seed a given pseudo-random number generator once, and do not reseed it. Here\u2019s an example of a common mistake that new programmers make: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include int getCard () { std :: mt19937 mt { std :: random_device {}() }; // this gets created and seeded every time the function is called std :: uniform_int_distribution card { 1 , 52 }; return card ( mt ); } int main () { std :: cout << getCard (); return 0 ; } COPY In the getCard() function, the random number generator is being created and seeded every time the function is called. This is inefficient at best, and will likely cause poor random results.","title":"Q: \u5982\u679c std::random_device \u672c\u8eab\u662f\u968f\u673a\u7684\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u76f4\u63a5\u7528\u5b83\u4ee3\u66ff Mersenne Twister \u5462\uff1f"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#_4","text":"What happens if we want to use a random number generator in multiple functions? One way is to create (and seed) our PRNG in our main() function, and then pass it everywhere we need it. But that\u2019s a lot of passing for something we may only use sporadically, and in different places. Although you can create a static local std::mt19937 variable in each function that needs it (static so that it only gets seeded once), it\u2019s overkill to have every function that uses a random number generator define and seed its own local generator. A better option in most cases is to create a global random number generator (inside a namespace!). Remember how we told you to avoid non-const global variables? This is an exception. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include // for std::mt19937 and std::random_device namespace Random // capital R to avoid conflicts with functions named random() { std :: mt19937 mt { std :: random_device {}() }; int get ( int min , int max ) { std :: uniform_int_distribution die { min , max }; // we can create a distribution in any function that needs it return die ( mt ); // and then generate a random number from our global generator } } int main () { std :: cout << Random :: get ( 1 , 6 ) << '\\n' ; std :: cout << Random :: get ( 1 , 10 ) << '\\n' ; std :: cout << Random :: get ( 1 , 20 ) << '\\n' ; return 0 ; } COPY In the above example, Random::mt is a global variable that can be accessed from any function. We\u2019ve created Random::get() as an easy way to get a random number between min and max . std::uniform_int_distribution is typically cheap to create, so it\u2019s fine to create when we need it.","title":"\u8de8\u51fd\u6570\u7684\u968f\u673a\u6570"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#mersenne-twister-underseeding-issues","text":"The internal state of a Mersenne Twister is 624 bytes in size. In the examples above, where we seed from the clock or std::random_device , our seed is only a single 32-bit integer. This means we\u2019re essentially initializing a 624-byte object with a 4-byte value, which is significantly underseeding the Mersenne Twister PRNG. The random library does the best it can to fill in the remaining 620 bytes with \u201crandom\u201d data\u2026 but it can\u2019t work magic. Underseeded PRNG can generate results that are suboptimal for applications that need the highest quality results. For example, seeding std::mt19937 with a single 32-bit value will never generate the number 42 as its first output. So how do we fix this? As of C++20, there\u2019s no easy way. But we do have some suggestions. First, let\u2019s talk about std::seed_seq (which stands for \u201cseed sequence\u201d). In the prior lesson, we mentioned that a seed can be either a single value, or a set of values. std::seed_seq is a type that performs two functions. First, it can hold multiple seed values, so we can use it to seed our PRNG with more than one value. Second, it will generate as many additional unbiased seed values as needed to initialize a PRNG\u2019s state. So if you initialize std::seed_seq with a single 32-bit integer (e.g. from std::random_device) and then initialize a Mersenne Twister with the std::seed_seq object, std::seed_seq will generate 620 bytes of additional seed data. The results won\u2019t be amazingly high quality, but it\u2019s better than nothing. Now on to the ideas. First, the more pieces of random data we can give std::seed_seq to work with, the better. So the easiest idea is to simply use std::random_device to give std::seed_seq more data to work with. If we initialize std::seed_seq with 8 numbers from std::random_device instead of 1, then the remaining numbers generated by std::seed_seq should be that much better: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include int main () { std :: random_device rd ; std :: seed_seq ss { rd (), rd (), rd (), rd (), rd (), rd (), rd (), rd () }; // get 8 integers of random numbers from std::random_device for our seed std :: mt19937 mt { ss }; // initialize our Mersenne Twister with the std::seed_seq // Create a reusable random number generator that generates uniform numbers between 1 and 6 std :: uniform_int_distribution die6 { 1 , 6 }; // for C++14, use std::uniform_int_distribution<> die6{ 1, 6 }; // Print a bunch of random numbers for ( int count { 1 }; count <= 40 ; ++ count ) { std :: cout << die6 ( mt ) << '\\t' ; // generate a roll of the die here // If we've printed 10 numbers, start a new row if ( count % 10 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY This is pretty straightforward so there isn\u2019t much reason not to do this at a minimum.","title":"Mersenne Twister \u548c underseeding issues"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#q-why-not-give-stdseed_seq-156-integers-624-bytes-from-stdrandom_device","text":"You can! However, this may be slow, and risks depleting the pool of random numbers that std::random_device uses. Second, you can use other \u201crandom\u201d inputs to std::seed_seq . We\u2019ve already shown you how to get a value from the clock, so you can throw that in easily. Other things that are sometimes used include the current thread id,the address of particular functions, the user\u2019s id, the process id, etc\u2026 Doing that is beyond the scope of this article, but this article has some context and a link to randutils.hpp that implements this. An alternate path is to use a different PRNG with a smaller state. Many good PRNGs use 64 or 128 bits of state, which can easily be initialized using std::seed_seq filled with 8 calls to std::random_device .","title":"Q: Why not give std::seed_seq 156 integers (624 bytes) from\u00a0std::random_device?"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#prng","text":"When a PRNG is given a poor quality seed (or underseeded), the initial results of the PRNG may not be high quality. For this reason, some PRNGs benefit from being \u201cwarmed up\u201d, which is a technique where the first N results generated from the PRNG are discarded. This allows the internal state of the PRNG to be mixed up such that the subsequent results should be of higher quality. Typically a few hundred to a few thousand initial results are discarded. The longer the period of the PRNG, the more initial results should be discarded. \u9898\u5916\u8bdd Visual Studio\u2019s implementation of rand() had (or still has?) a bug where the first generated result would not be sufficiently randomized. You may see older programs that use rand() discard a single result as a way to avoid this issue. The seed_seq initialization used by std::mt19937 performs a warm up, so we don\u2019t need to explicitly warm up std::mt19937 objects.","title":"PRNG \u70ed\u8eab"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#debug","text":"Programs that use random numbers can be difficult to debug because the program may exhibit different behaviors each time it is run. Sometimes it may work, and sometimes it may not. When debugging, it\u2019s helpful to ensure your program executes the same (incorrect) way each time. That way, you can run the program as many times as needed to isolate where the error is. For this reason, when debugging, it\u2019s a useful technique to seed your PRNG with a specific value (e.g. 5 ) that causes the erroneous behavior to occur. This will ensure your program generates the same results each time, making debugging easier. Once you\u2019ve found the error, you can use your normal seeding method to start generating randomized results again.","title":"\u4f7f\u7528\u968f\u673a\u6570\u5e26\u6765\u7684debug\u96be\u9898"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#faq","text":"","title":"\u968f\u673a\u5316 FAQ"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#q-help-my-random-number-generator-is-generating-the-same-sequence-of-random-numbers","text":"If your random number generator is generating the same sequence of random numbers every time your program is run, you probably didn\u2019t seed it properly (or at all). Make sure you\u2019re seeding it with a value that changes each time the program is run.","title":"Q: Help! My random number generator is generating the same sequence of random numbers."},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#q-help-my-random-number-generator-keeps-generating-the-same-number-over-and-over","text":"If your random number generator is generating the same number every time you ask it for a random number, then you are probably either reseeding the random number generator before generating a random number, or you\u2019re creating a new random generator for each random number.","title":"Q: Help! My random number generator keeps generating the same number over and over."},{"location":"7-2-If-statements-and-blocks/","tags":["if"],"text":"\u6211\u4eec\u8981\u8ba8\u8bba\u7684\u7b2c\u4e00\u7c7b\u63a7\u5236\u6d41\u8bed\u53e5\u662f\u6761\u4ef6\u8bed\u53e5\u3002\u6761\u4ef6\u8bed\u53e5\u662f\u4e00\u79cd\u6307\u5b9a\u662f\u5426\u5e94\u8be5\u6267\u884c\u67d0\u4e9b\u5173\u8054\u8bed\u53e5\u7684\u8bed\u53e5\u3002 C++\u652f\u6301\u4e24\u79cd\u57fa\u672c\u6761\u4ef6\u8bed\u53e5\uff1aif\u8bed\u53e5(\u6211\u4eec\u5728\u8bfe\u7a0b[[4-10-Introduction-to-if-statements|4.10 -if\u8bed\u53e5\u7b80\u4ecb]\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u5e76\u5c06\u5728\u8fd9\u91cc\u8fdb\u4e00\u6b65\u8ba8\u8bba)\u548c switch \u8bed\u53e5(\u6211\u4eec\u5c06\u5728\u51e0\u8282\u8bfe\u4e2d\u8ba8\u8bba)\u3002 \u5feb\u901f\u590d\u4e60\u4e00\u4e0b if \u8bed\u53e5 if \u8bed\u53e5\u662fC++\u4e2d\u6700\u57fa\u672c\u7684\u6761\u4ef6\u8bed\u53e5\uff0c\u5176\u5f62\u5f0f\u4e3a\uff1a 1 2 if ( condition ) true_statement ; \u6216\u8005\u662f\u5e26 else \u7684\u5f62\u5f0f\uff1a 1 2 3 4 if ( condition ) true_statement ; else false_statement ; \u5982\u679c condition \u6c42\u503c\u4e3a true \uff0c\u5219\u6267\u884c true_statement \u3002\u5982\u679c\u6c42\u503c\u4e3a false \u800c\u4e14\u5b58\u5728 else \u8bed\u53e5\u5219\u6267\u884c false_statement \u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86if\u8bed\u53e5\u548celse\u8bed\u53e5\u7684\u7528\u6cd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x > 10 ) std :: cout << x << \" is greater than 10 \\n \" ; else std :: cout << x << \" is not greater than 10 \\n \" ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u5982\u4f60\u6240\u671f\u671b\u7684\u90a3\u6837\uff1a 1 2 3 4 5 Enter a number: 15 15 is greater than 10 Enter a number: 4 4 is not greater than 10 \u591a\u4e2a\u6761\u4ef6\u8bed\u53e5 \u65b0\u624b\u7a0b\u5e8f\u5458\u53ef\u80fd\u4f1a\u7f16\u5199\u8fd9\u6837\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { std :: cout << \"Enter your height (in cm): \" ; int x {}; std :: cin >> x ; if ( x > 140 ) std :: cout << \"You are tall enough to ride. \\n \" ; else std :: cout << \"You are not tall enough to ride. \\n \" ; std :: cout << \"Too bad! \\n \" ; // focus on this line return 0 ; } \u4e0d\u8fc7\uff0c\u7a0b\u5e8f\u7684\u8fd0\u884c\u7ed3\u679c\u53ef\u80fd\u5e76\u4e0d\u5982\u613f\uff1a 1 2 3 Enter your height (in cm): 180 You are tall enough to ride. Too bad! \u8fd9\u4e2a\u7a0b\u5e8f\u6ca1\u6709\u50cf\u9884\u671f\u7684\u90a3\u6837\u5de5\u4f5c\uff0c\u56e0\u4e3a true_statement \u548c false_statement \u53ea\u80fd\u662f\u4e00\u4e2a\u8bed\u53e5\u3002\u8fd9\u91cc\u7684\u7f29\u8fdb\u6b3a\u9a97\u4e86\u6211\u4eec\u2014\u2014\u4e0a\u9762\u7684\u7a0b\u5e8f\u6267\u884c\u8d77\u6765\u5c31\u597d\u50cf\u5b83\u662f\u8fd9\u6837\u5199\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { std :: cout << \"Enter your height (in cm): \" ; int x {}; std :: cin >> x ; if ( x > 140 ) std :: cout << \"You are tall enough to ride. \\n \" ; else std :: cout << \"You are not tall enough to ride. \\n \" ; std :: cout << \"Too bad! \\n \" ; // focus on this line return 0 ; } \u8fd9\u4e5f\u5c31\u770b\u5230\u5f88\u6e05\u695a\u4e86\uff0c\u663e\u7136 \u201cToo bad!\u201d \u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u90fd\u4f1a\u88ab\u6253\u5370\u3002 \u4f46\u662f\uff0c\u901a\u5e38\u5e0c\u671b\u57fa\u4e8e\u67d0\u4e9b\u6761\u4ef6\u6267\u884c\u591a\u4e2a\u8bed\u53e5\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u590d\u5408\u8bed\u53e5(\u5757): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { std :: cout << \"Enter your height (in cm): \" ; int x {}; std :: cin >> x ; if ( x > 140 ) std :: cout << \"You are tall enough to ride. \\n \" ; else { // note addition of block here std :: cout << \"You are not tall enough to ride. \\n \" ; std :: cout << \"Too bad! \\n \" ; } return 0 ; } \u8bb0\u4f4f\uff0cblock\u88ab\u5f53\u4f5c\u4e00\u4e2a\u5355\u72ec\u7684\u8bed\u53e5\uff0c\u6240\u4ee5\u73b0\u5728\u5de5\u4f5c\u6b63\u5e38\uff1a 1 2 Enter your height (in cm): 180 You are tall enough to ride. 1 2 3 Enter your height (in cm): 130 You are not tall enough to ride. Too bad! \u5e94\u8be5\u5c06\u5355\u72ec\u7684\u6761\u4ef6\u8bed\u53e5\u5199\u4f5c\u8bed\u53e5\u5757\u5417\uff1f \u5bf9\u4e8e if \u6216 else \u540e\u9762\u7684\u5355\u4e2a\u8bed\u53e5\u662f\u5426\u5e94\u8be5\u663e\u5f0f\u5730\u5305\u542b\u5728\u8bed\u53e5\u5757\u4e2d\uff0c\u5728\u7a0b\u5e8f\u5458\u793e\u533a\u4e2d\u5f15\u53d1\u4e86\u5e7f\u6cdb\u7684\u4e89\u8bba\u3002 \u8fd9\u6837\u505a\u901a\u5e38\u6709\u4e24\u4e2a\u7406\u7531\u3002\u9996\u5148\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\uff1a\u8bb0\u4f4f\uff0c\u5757\u88ab\u89c6\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u8bed\u53e5\uff0c\u6240\u4ee5\u73b0\u5728\u5de5\u4f5c\u6b63\u5e38: 1 2 if ( age >= 21 ) purchaseBeer (); \u7136\u540e\uff0c\u5982\u679c\u4f60\u7279\u522b\u5306\u5fd9\u5730\u5c06\u67d0\u4e2a\u65b0\u529f\u80fd\u52a0\u5165\u5230\u7a0b\u5e8f\u4e2d\uff1a 1 2 3 if ( age >= 21 ) purchaseBeer (); gamble (); // will always execute \u4e0a\u8ff0\u4ee3\u7801\u5c06\u5141\u8bb8\u672a\u6210\u5e74\u4eba\u8d4c\u535a\u3002\u5728\u76d1\u72f1\u91cc\u73a9\u5f97\u5f00\u5fc3\uff01 \u5176\u6b21\uff0c\u5b83\u4f1a\u4f7f\u7a0b\u5e8f\u66f4\u96be\u8c03\u8bd5\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u4ee3\u7801\u7247\u6bb5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 if ( age >= 21 ) addBeerToCart (); checkout (); ``` \u5982\u679c\u6211\u4eec\u6000\u7591 ` addBeerToCart () ` \u51fd\u6570\u6709\u95ee\u9898 \uff0c \u5f80\u5f80\u4f1a\u5c06\u5176\u4e34\u65f6\u6ce8\u91ca\u6389 \uff1a ``` cpp if ( age >= 21 ) // addBeerToCart(); checkout (); \u8fd9\u6837\u4e00\u6765\u4f1a\u4f7f\u5f97 checkout() \u6309\u6761\u4ef6\u6267\u884c\uff0c\u8fd9\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c\u3002 \u5982\u679c\u6211\u4eec\u603b\u662f\u5c06if\u548celse\u7684\u8bed\u53e5\u653e\u5728\u8bed\u53e5\u5757\u4e2d\u7684\u8bdd\uff0c\u4e0a\u9762\u7684\u8fd9\u4e9b\u95ee\u9898\u90fd\u53ef\u4ee5\u907f\u514d\u3002 \u53cd\u65b9\u5219\u8ba4\u4e3a\uff0c\u4e0d\u8981\u5728\u5355\u4e2a\u8bed\u53e5\u5468\u56f4\u4f7f\u7528\u5757\u7684\u6700\u4f73\u7406\u7531\u662f\u56e0\u4e3a\u6dfb\u52a0\u5757\u4f1a\u5360\u7528\u5782\u76f4\u7a7a\u95f4\uff0c\u4f7f\u4f60\u4e00\u6b21\u770b\u5230\u7684\u4ee3\u7801\u66f4\u5c11\uff0c\u8fd9\u4f1a\u4f7f\u4ee3\u7801\u53ef\u8bfb\u6027\u53d8\u5dee\uff0c\u5e76\u53ef\u80fd\u5bfc\u81f4\u5176\u4ed6\u66f4\u4e25\u91cd\u7684\u9519\u8bef\u3002 \u793e\u533a\u4f3c\u4e4e\u66f4\u503e\u5411\u4e8e\u59cb\u7ec8\u4f7f\u7528\u5757\uff0c\u5c3d\u7ba1\u8fd9\u79cd\u5efa\u8bae\u5f53\u7136\u4e0d\u662f\u666e\u904d\u5b58\u5728\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u8003\u8651\u5c06\u4e0e\u201c if \u201d\u6216\u201c else \u201d\u76f8\u5173\u7684\u5355\u4e2a\u8bed\u53e5\u653e\u5728\u5757\u4e2d(\u7279\u522b\u662f\u5728\u4f60\u5b66\u4e60\u7684\u65f6\u5019)\u3002\u66f4\u6709\u7ecf\u9a8c\u7684C++\u5f00\u53d1\u4eba\u5458\u6709\u65f6\u4f1a\u5ffd\u7565\u8fd9\u79cd\u505a\u6cd5\uff0c\u800c\u503e\u5411\u4e8e\u66f4\u7d27\u51d1\u7684\u5782\u76f4\u95f4\u8ddd\u3002 \u8fd8\u6709\u4e00\u79cd\u6298\u4e2d\u7684\u529e\u6cd5\uff0c\u5373\u5c06\u5355\u4e00\u6761\u4ef6\u8bed\u53e5\u548cif\u6216else\u5199\u5728\u540c\u4e00\u884c\uff1a 1 if ( age >= 21 ) purchaseBeer (); \u8fd9\u907f\u514d\u4e86\u4e0a\u9762\u63d0\u5230\u7684\u4e24\u4e2a\u7f3a\u70b9\uff0c\u4e0d\u8fc7\u53ef\u8bfb\u6027\u7a0d\u5dee\u4e86\u4e00\u70b9\u3002 \u9690\u5f0f\u8bed\u53e5\u5757 \u5982\u679c\u7a0b\u5e8f\u5458\u6ca1\u6709\u5728 if \u8bed\u53e5\u6216 else \u8bed\u53e5\u7684\u90e8\u5206\u58f0\u660e\u4e00\u4e2a\u8bed\u53e5\u5757\uff0c\u5219\u7f16\u8bd1\u5668\u5c06\u9690\u5f0f\u58f0\u660e\u4e00\u4e2a\u3002\u56e0\u6b64\uff1a 1 2 3 4 if (condition) true_statement; else false_statement; \u7b49\u4ef7\u4e8e\uff1a 1 2 3 4 5 6 7 8 if (condition) { true_statement; } else { false_statement; } \u591a\u6570\u60c5\u51b5\u4e0b\u8fd9\u5e76\u4e0d\u4f1a\u5e26\u6765\u95ee\u9898\uff0c\u4f46\u662f\u65b0\u624b\u7a0b\u5e8f\u5458\u53ef\u80fd\u4f1a\u5c1d\u8bd5\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { if ( true ) int x { 5 }; else int x { 6 }; std :: cout << x ; return 0 ; } \u6b64\u65f6\u4ee3\u7801\u662f\u4e0d\u80fd\u7f16\u8bd1\u7684\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u544a x \u672a\u5b9a\u4e49\u3002\u56e0\u4e3a\u4e0a\u8ff0\u4ee3\u7801\u7b49\u4ef7\u4e8e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { if ( true ) { int x { 5 }; } // x \u9500\u6bc1\u4e86 else { int x { 6 }; } // x \u9500\u6bc1\u4e86 std :: cout << x ; // x \u5e76\u4e0d\u5728\u8be5\u4f5c\u7528\u57df\u4e2d return 0 ; } \u8fd9\u91cc\uff0c\u5f88\u663e\u7136\u53d8\u91cf x \u5177\u6709\u8bed\u53e5\u5757\u4f5c\u7528\u57df\uff0c\u5e76\u4e14\u4f1a\u5728\u8bed\u53e5\u5757\u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u5f53\u6211\u4eec\u5728 std::cout \u4e2d\u8bbf\u95ee x \u65f6\uff0c\u5b83\u5df2\u7ecf\u4e0d\u5b58\u5728\u4e86\u3002","title":"7.2 - if \u8bed\u53e5\u548c\u8bed\u53e5\u5757"},{"location":"7-2-If-statements-and-blocks/#if","text":"if \u8bed\u53e5\u662fC++\u4e2d\u6700\u57fa\u672c\u7684\u6761\u4ef6\u8bed\u53e5\uff0c\u5176\u5f62\u5f0f\u4e3a\uff1a 1 2 if ( condition ) true_statement ; \u6216\u8005\u662f\u5e26 else \u7684\u5f62\u5f0f\uff1a 1 2 3 4 if ( condition ) true_statement ; else false_statement ; \u5982\u679c condition \u6c42\u503c\u4e3a true \uff0c\u5219\u6267\u884c true_statement \u3002\u5982\u679c\u6c42\u503c\u4e3a false \u800c\u4e14\u5b58\u5728 else \u8bed\u53e5\u5219\u6267\u884c false_statement \u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86if\u8bed\u53e5\u548celse\u8bed\u53e5\u7684\u7528\u6cd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x > 10 ) std :: cout << x << \" is greater than 10 \\n \" ; else std :: cout << x << \" is not greater than 10 \\n \" ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u5982\u4f60\u6240\u671f\u671b\u7684\u90a3\u6837\uff1a 1 2 3 4 5 Enter a number: 15 15 is greater than 10 Enter a number: 4 4 is not greater than 10","title":"\u5feb\u901f\u590d\u4e60\u4e00\u4e0b if \u8bed\u53e5"},{"location":"7-2-If-statements-and-blocks/#_1","text":"\u65b0\u624b\u7a0b\u5e8f\u5458\u53ef\u80fd\u4f1a\u7f16\u5199\u8fd9\u6837\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { std :: cout << \"Enter your height (in cm): \" ; int x {}; std :: cin >> x ; if ( x > 140 ) std :: cout << \"You are tall enough to ride. \\n \" ; else std :: cout << \"You are not tall enough to ride. \\n \" ; std :: cout << \"Too bad! \\n \" ; // focus on this line return 0 ; } \u4e0d\u8fc7\uff0c\u7a0b\u5e8f\u7684\u8fd0\u884c\u7ed3\u679c\u53ef\u80fd\u5e76\u4e0d\u5982\u613f\uff1a 1 2 3 Enter your height (in cm): 180 You are tall enough to ride. Too bad! \u8fd9\u4e2a\u7a0b\u5e8f\u6ca1\u6709\u50cf\u9884\u671f\u7684\u90a3\u6837\u5de5\u4f5c\uff0c\u56e0\u4e3a true_statement \u548c false_statement \u53ea\u80fd\u662f\u4e00\u4e2a\u8bed\u53e5\u3002\u8fd9\u91cc\u7684\u7f29\u8fdb\u6b3a\u9a97\u4e86\u6211\u4eec\u2014\u2014\u4e0a\u9762\u7684\u7a0b\u5e8f\u6267\u884c\u8d77\u6765\u5c31\u597d\u50cf\u5b83\u662f\u8fd9\u6837\u5199\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { std :: cout << \"Enter your height (in cm): \" ; int x {}; std :: cin >> x ; if ( x > 140 ) std :: cout << \"You are tall enough to ride. \\n \" ; else std :: cout << \"You are not tall enough to ride. \\n \" ; std :: cout << \"Too bad! \\n \" ; // focus on this line return 0 ; } \u8fd9\u4e5f\u5c31\u770b\u5230\u5f88\u6e05\u695a\u4e86\uff0c\u663e\u7136 \u201cToo bad!\u201d \u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u90fd\u4f1a\u88ab\u6253\u5370\u3002 \u4f46\u662f\uff0c\u901a\u5e38\u5e0c\u671b\u57fa\u4e8e\u67d0\u4e9b\u6761\u4ef6\u6267\u884c\u591a\u4e2a\u8bed\u53e5\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u590d\u5408\u8bed\u53e5(\u5757): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { std :: cout << \"Enter your height (in cm): \" ; int x {}; std :: cin >> x ; if ( x > 140 ) std :: cout << \"You are tall enough to ride. \\n \" ; else { // note addition of block here std :: cout << \"You are not tall enough to ride. \\n \" ; std :: cout << \"Too bad! \\n \" ; } return 0 ; } \u8bb0\u4f4f\uff0cblock\u88ab\u5f53\u4f5c\u4e00\u4e2a\u5355\u72ec\u7684\u8bed\u53e5\uff0c\u6240\u4ee5\u73b0\u5728\u5de5\u4f5c\u6b63\u5e38\uff1a 1 2 Enter your height (in cm): 180 You are tall enough to ride. 1 2 3 Enter your height (in cm): 130 You are not tall enough to ride. Too bad!","title":"\u591a\u4e2a\u6761\u4ef6\u8bed\u53e5"},{"location":"7-2-If-statements-and-blocks/#_2","text":"\u5bf9\u4e8e if \u6216 else \u540e\u9762\u7684\u5355\u4e2a\u8bed\u53e5\u662f\u5426\u5e94\u8be5\u663e\u5f0f\u5730\u5305\u542b\u5728\u8bed\u53e5\u5757\u4e2d\uff0c\u5728\u7a0b\u5e8f\u5458\u793e\u533a\u4e2d\u5f15\u53d1\u4e86\u5e7f\u6cdb\u7684\u4e89\u8bba\u3002 \u8fd9\u6837\u505a\u901a\u5e38\u6709\u4e24\u4e2a\u7406\u7531\u3002\u9996\u5148\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\uff1a\u8bb0\u4f4f\uff0c\u5757\u88ab\u89c6\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u8bed\u53e5\uff0c\u6240\u4ee5\u73b0\u5728\u5de5\u4f5c\u6b63\u5e38: 1 2 if ( age >= 21 ) purchaseBeer (); \u7136\u540e\uff0c\u5982\u679c\u4f60\u7279\u522b\u5306\u5fd9\u5730\u5c06\u67d0\u4e2a\u65b0\u529f\u80fd\u52a0\u5165\u5230\u7a0b\u5e8f\u4e2d\uff1a 1 2 3 if ( age >= 21 ) purchaseBeer (); gamble (); // will always execute \u4e0a\u8ff0\u4ee3\u7801\u5c06\u5141\u8bb8\u672a\u6210\u5e74\u4eba\u8d4c\u535a\u3002\u5728\u76d1\u72f1\u91cc\u73a9\u5f97\u5f00\u5fc3\uff01 \u5176\u6b21\uff0c\u5b83\u4f1a\u4f7f\u7a0b\u5e8f\u66f4\u96be\u8c03\u8bd5\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u4ee3\u7801\u7247\u6bb5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 if ( age >= 21 ) addBeerToCart (); checkout (); ``` \u5982\u679c\u6211\u4eec\u6000\u7591 ` addBeerToCart () ` \u51fd\u6570\u6709\u95ee\u9898 \uff0c \u5f80\u5f80\u4f1a\u5c06\u5176\u4e34\u65f6\u6ce8\u91ca\u6389 \uff1a ``` cpp if ( age >= 21 ) // addBeerToCart(); checkout (); \u8fd9\u6837\u4e00\u6765\u4f1a\u4f7f\u5f97 checkout() \u6309\u6761\u4ef6\u6267\u884c\uff0c\u8fd9\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c\u3002 \u5982\u679c\u6211\u4eec\u603b\u662f\u5c06if\u548celse\u7684\u8bed\u53e5\u653e\u5728\u8bed\u53e5\u5757\u4e2d\u7684\u8bdd\uff0c\u4e0a\u9762\u7684\u8fd9\u4e9b\u95ee\u9898\u90fd\u53ef\u4ee5\u907f\u514d\u3002 \u53cd\u65b9\u5219\u8ba4\u4e3a\uff0c\u4e0d\u8981\u5728\u5355\u4e2a\u8bed\u53e5\u5468\u56f4\u4f7f\u7528\u5757\u7684\u6700\u4f73\u7406\u7531\u662f\u56e0\u4e3a\u6dfb\u52a0\u5757\u4f1a\u5360\u7528\u5782\u76f4\u7a7a\u95f4\uff0c\u4f7f\u4f60\u4e00\u6b21\u770b\u5230\u7684\u4ee3\u7801\u66f4\u5c11\uff0c\u8fd9\u4f1a\u4f7f\u4ee3\u7801\u53ef\u8bfb\u6027\u53d8\u5dee\uff0c\u5e76\u53ef\u80fd\u5bfc\u81f4\u5176\u4ed6\u66f4\u4e25\u91cd\u7684\u9519\u8bef\u3002 \u793e\u533a\u4f3c\u4e4e\u66f4\u503e\u5411\u4e8e\u59cb\u7ec8\u4f7f\u7528\u5757\uff0c\u5c3d\u7ba1\u8fd9\u79cd\u5efa\u8bae\u5f53\u7136\u4e0d\u662f\u666e\u904d\u5b58\u5728\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u8003\u8651\u5c06\u4e0e\u201c if \u201d\u6216\u201c else \u201d\u76f8\u5173\u7684\u5355\u4e2a\u8bed\u53e5\u653e\u5728\u5757\u4e2d(\u7279\u522b\u662f\u5728\u4f60\u5b66\u4e60\u7684\u65f6\u5019)\u3002\u66f4\u6709\u7ecf\u9a8c\u7684C++\u5f00\u53d1\u4eba\u5458\u6709\u65f6\u4f1a\u5ffd\u7565\u8fd9\u79cd\u505a\u6cd5\uff0c\u800c\u503e\u5411\u4e8e\u66f4\u7d27\u51d1\u7684\u5782\u76f4\u95f4\u8ddd\u3002 \u8fd8\u6709\u4e00\u79cd\u6298\u4e2d\u7684\u529e\u6cd5\uff0c\u5373\u5c06\u5355\u4e00\u6761\u4ef6\u8bed\u53e5\u548cif\u6216else\u5199\u5728\u540c\u4e00\u884c\uff1a 1 if ( age >= 21 ) purchaseBeer (); \u8fd9\u907f\u514d\u4e86\u4e0a\u9762\u63d0\u5230\u7684\u4e24\u4e2a\u7f3a\u70b9\uff0c\u4e0d\u8fc7\u53ef\u8bfb\u6027\u7a0d\u5dee\u4e86\u4e00\u70b9\u3002","title":"\u5e94\u8be5\u5c06\u5355\u72ec\u7684\u6761\u4ef6\u8bed\u53e5\u5199\u4f5c\u8bed\u53e5\u5757\u5417\uff1f"},{"location":"7-2-If-statements-and-blocks/#_3","text":"\u5982\u679c\u7a0b\u5e8f\u5458\u6ca1\u6709\u5728 if \u8bed\u53e5\u6216 else \u8bed\u53e5\u7684\u90e8\u5206\u58f0\u660e\u4e00\u4e2a\u8bed\u53e5\u5757\uff0c\u5219\u7f16\u8bd1\u5668\u5c06\u9690\u5f0f\u58f0\u660e\u4e00\u4e2a\u3002\u56e0\u6b64\uff1a 1 2 3 4 if (condition) true_statement; else false_statement; \u7b49\u4ef7\u4e8e\uff1a 1 2 3 4 5 6 7 8 if (condition) { true_statement; } else { false_statement; } \u591a\u6570\u60c5\u51b5\u4e0b\u8fd9\u5e76\u4e0d\u4f1a\u5e26\u6765\u95ee\u9898\uff0c\u4f46\u662f\u65b0\u624b\u7a0b\u5e8f\u5458\u53ef\u80fd\u4f1a\u5c1d\u8bd5\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { if ( true ) int x { 5 }; else int x { 6 }; std :: cout << x ; return 0 ; } \u6b64\u65f6\u4ee3\u7801\u662f\u4e0d\u80fd\u7f16\u8bd1\u7684\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u544a x \u672a\u5b9a\u4e49\u3002\u56e0\u4e3a\u4e0a\u8ff0\u4ee3\u7801\u7b49\u4ef7\u4e8e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { if ( true ) { int x { 5 }; } // x \u9500\u6bc1\u4e86 else { int x { 6 }; } // x \u9500\u6bc1\u4e86 std :: cout << x ; // x \u5e76\u4e0d\u5728\u8be5\u4f5c\u7528\u57df\u4e2d return 0 ; } \u8fd9\u91cc\uff0c\u5f88\u663e\u7136\u53d8\u91cf x \u5177\u6709\u8bed\u53e5\u5757\u4f5c\u7528\u57df\uff0c\u5e76\u4e14\u4f1a\u5728\u8bed\u53e5\u5757\u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u5f53\u6211\u4eec\u5728 std::cout \u4e2d\u8bbf\u95ee x \u65f6\uff0c\u5b83\u5df2\u7ecf\u4e0d\u5b58\u5728\u4e86\u3002","title":"\u9690\u5f0f\u8bed\u53e5\u5757"},{"location":"7-3-Common-if-statement-problems/","tags":["if"],"text":"Key Takeaway \u7ee7\u7eed 7.2 - if \u8bed\u53e5\u548c\u8bed\u53e5\u5757 \u7684\u5185\u5bb9\u3002\u672c\u8282\u8bfe\u6211\u4eec\u4f1a\u4ecb\u7ecd\u4e00\u4e9b\u548cif\u8bed\u53e5\u76f8\u5173\u7684\u5e38\u89c1\u9519\u8bef\u3002 \u5d4c\u5957if\u8bed\u53e5\u548c\u60ac\u5782else\u95ee\u9898 if \u8bed\u53e5\u53ef\u4ee5\u5d4c\u5957\u5728\u5176\u4ed6 if \u8bed\u53e5\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x >= 0 ) // \u5916\u5c42if\u8bed\u53e5 // \u8fd9\u79cd\u5d4c\u5957\u65b9\u5f0f\u662f\u4e0d\u597d\u7684\u7f16\u7a0b\u98ce\u683c if ( x <= 20 ) // \u5185\u5c42if\u8bed\u53e5 std :: cout << x << \" is between 0 and 20 \\n \" ; return 0 ; } \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x >= 0 ) // \u5916\u5c42if\u8bed\u53e5 // \u8fd9\u79cd\u5d4c\u5957\u65b9\u5f0f\u662f\u4e0d\u597d\u7684\u7f16\u7a0b\u98ce\u683c if ( x <= 20 ) // \u5185\u5c42if\u8bed\u53e5 std :: cout << x << \" is between 0 and 20 \\n \" ; // \u8fd9\u4e2a else \u5e94\u8be5\u548c\u54ea\u4e2a if \u8bed\u53e5\u5339\u914d\uff1f else std :: cout << x << \" is negative \\n \" ; return 0 ; } \u4e0a\u9762\u7684\u7a0b\u5e8f\u5f15\u5165\u4e86\u4e00\u4e2a\u6f5c\u5728\u7684\u6b67\u4e49\u6765\u6e90\uff0c\u79f0\u4e3a\u60ac\u5782else\u95ee\u9898\u3002\u4e0a\u9762\u7a0b\u5e8f\u4e2d\u7684 else \u8bed\u53e5\u5e94\u8be5\u4e0e\u5916\u90e8if\u5339\u914d\u8fd8\u662f\u548c\u5185\u90e8if\u5339\u914d\uff1f \u7b54\u6848\u662f else \u8bed\u53e5\u4e0e\u540c\u4e00\u8bed\u53e5\u5757\u4e2d\u6700\u540e\u4e00\u4e2a\u672a\u5339\u914d\u7684 if \u8bed\u53e5\u914d\u5bf9\u3002\u56e0\u6b64\uff0c\u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c else \u4e0e\u5185\u90e8\u7684 if \u8bed\u53e5\u5339\u914d\uff0c\u7b49\u4ef7\u4e8e\u4e0b\u9762\u5f62\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x >= 0 ) // \u5916\u5c42 if \u8bed\u53e5 { if ( x <= 20 ) // \u5185\u5c42 if \u8bed\u53e5 std :: cout << x << \" is between 0 and 20 \\n \" ; else // \u548c\u5185\u5c42 if \u8bed\u53e5\u5339\u914d std :: cout << x << \" is negative \\n \" ; } return 0 ; } \u8fd9\u4f7f\u5f97\u7a0b\u5e8f\u4ea7\u751f\u9519\u8bef\u7684\u7ed3\u679c\uff1a 1 2 Enter a number: 21 21 is negative \u4e3a\u4e86\u907f\u514d\u56e0\u5d4c\u5957 if \u8bed\u53e5\u4ea7\u751f\u7684\u6b67\u4e49\uff0c\u6700\u597d\u7684\u529e\u6cd5\u662f\u5c06\u5185\u90e8\u7684if\u8bed\u53e5\u663e\u5f0f\u5730\u5b9a\u4e49\u5728\u8bed\u53e5\u5757\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x >= 0 ) { if ( x <= 20 ) std :: cout << x << \" is between 0 and 20 \\n \" ; else // attached to inner if statement std :: cout << x << \" is greater than 20 \\n \" ; } else // attached to outer if statement std :: cout << x << \" is negative \\n \" ; return 0 ; } \u6b64\u65f6\uff0c\u8bed\u53e5\u5757\u4e2d\u7684 else \u8bed\u53e5\u4f1a\u548c\u5185\u5c42\u7684 if \u8bed\u53e5\u5339\u914d\uff0c\u800c\u8bed\u53e5\u5757\u5916\u90e8\u7684 else \u5219\u548c\u5916\u5c42\u7684 if \u8bed\u53e5\u5339\u914d\u3002 \u5d4c\u5957\u8bed\u53e5\u5c55\u5f00 \u5d4c\u5957\u7684\u591a\u5c42 if \u8bed\u53e5\u53ef\u4ee5\u901a\u8fc7\u91cd\u65b0\u7ec4\u7ec7\u903b\u8f91\u6216\u4f7f\u7528 \u903b\u8f91\u8fd0\u7b97\u7b26 \u5c55\u5f00\u6210\u4e00\u5c42\u903b\u8f91\u3002\u5d4c\u5957\u8d8a\u5c11\u7684\u4ee3\u7801\u8d8a\u4e0d\u5bb9\u6613\u51fa\u9519\u3002 \u4f8b\u5982\uff0c\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u53ef\u4ee5\u5c55\u5f00\u6210\u5982\u4e0b\u5f62\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x < 0 ) std :: cout << x << \" is negative \\n \" ; else if ( x <= 20 ) // only executes if x >= 0 std :: cout << x << \" is between 0 and 20 \\n \" ; else // only executes if x > 20 std :: cout << x << \" is greater than 20 \\n \" ; return 0 ; } \u4e0b\u9762\u662f\u53e6\u4e00\u4e2a\u4f7f\u7528\u903b\u8f91\u8fd0\u7b97\u7b26\u5728\u4e00\u4e2a if \u8bed\u53e5\u4e2d\u68c0\u67e5\u591a\u4e2a\u6761\u4ef6\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another integer: \" ; int y {}; std :: cin >> y ; if ( x > 0 && y > 0 ) // && \u662f\u903b\u8f91\u4e0e\u2014\u2014\u68c0\u67e5\u662f\u5426\u4e24\u4e2a\u6761\u4ef6\u5747\u4e3a\u771f std :: cout << \"Both numbers are positive \\n \" ; else if ( x > 0 || y > 0 ) // || \u662f\u903b\u8f91\u6216\u2014\u2014\u68c0\u67e5\u662f\u5426\u6709\u4e3a\u771f\u7684\u6761\u4ef6 std :: cout << \"One of the numbers is positive \\n \" ; else std :: cout << \"Neither number is positive \\n \" ; return 0 ; } \u7a7a\u8bed\u53e5 \u7a7a\u8bed\u53e5\u662f\u4e00\u4e2a\u53ea\u5305\u542b\u5206\u53f7\u7684\u8bed\u53e5\uff1a 1 2 if ( x > 10 ) ; // this is a null statement \u7a7a\u8bed\u53e5\u4ec0\u4e48\u90fd\u4e0d\u505a\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u53ea\u6709\u5f53\u8bed\u8a00\u8981\u6c42\u5b58\u5728\u4e00\u6761\u8bed\u53e5\u4f46\u7a0b\u5e8f\u5458\u4e0d\u9700\u8981\u7684\u60c5\u51b5\u4e0b\u624d\u8fd9\u4e48\u505a\u3002\u4e3a\u4e86\u53ef\u8bfb\u6027\uff0c\u201c\u7a7a\u8bed\u53e5\u201d\u901a\u5e38\u88ab\u653e\u5728\u5355\u72ec\u7684\u884c\u4e2d\u3002 \u6211\u4eec\u5c06\u5728\u672c\u7ae0\u540e\u9762\u8bb2\u5230\u5faa\u73af\u7684\u65f6\u5019\u770b\u5230\u6709\u610f\u7684\u7a7a\u8bed\u53e5\u7684\u4f8b\u5b50\u3002\u201c\u7a7a\u8bed\u53e5\u201d\u5f88\u5c11\u4e0e\u201cif\u8bed\u53e5\u201d\u4e00\u8d77\u4f7f\u7528\u3002\u7136\u800c\uff0c\u5b83\u4eec\u53ef\u80fd\u4f1a\u65e0\u610f\u4e2d\u7ed9\u65b0\u624b(\u6216\u7c97\u5fc3\u7684)\u7a0b\u5e8f\u5458\u5e26\u6765\u95ee\u9898\u3002\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 if ( nuclearCodesActivated ()); blowUpTheWorld (); \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u5458\u4e0d\u7ecf\u610f\u95f4\u5728if\u8bed\u53e5\u7684\u672b\u5c3e\u6dfb\u52a0\u4e86\u4e00\u4e2a\u5206\u53f7\u3002\u8fd9\u4e2a\u4e0d\u8d77\u773c\u7684\u9519\u8bef\u53ef\u4ee5\u5f88\u597d\u5730\u7f16\u8bd1\uff0c\u5e76\u4f7f\u4ee3\u7801\u6bb5\u6267\u884c\u8d77\u6765\u5c31\u50cf\u5b83\u662f\u8fd9\u6837\u7f16\u5199\u7684\u4e00\u6837\uff1a 1 2 3 if ( nuclearCodesActivated ()) ; // \u5206\u53f7\u5b9e\u9645\u4e0a\u5236\u9020\u4e86\u4e00\u4e2a\u7a7a\u8bed\u53e5 blowUpTheWorld (); // \u8fd9\u4e2a\u51fd\u6570\u603b\u662f\u4f1a\u6267\u884c \u6ce8\u610f \u6ce8\u610f\u4e0d\u8981\u5728if\u8bed\u53e5\u540e\u9762\u6dfb\u52a0\u5206\u53f7\uff0c\u5426\u5219\u4f60\u7684\u201c\u6761\u4ef6\u8bed\u53e5\u201d\u603b\u662f\u4f1a\u65e0\u6761\u4ef6\u6267\u884c\uff08\u5373\u4f7f\u4f60\u628a\u5b83\u5199\u5728\u8bed\u53e5\u5757\u4e2d\uff09\u3002 \u6761\u4ef6\u8bed\u53e5\u4e2d\u7684\u7b49\u53f7\u548c\u8d4b\u503c\u53f7 \u5728 if \u7684\u6761\u4ef6\u4e2d\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528\u7b49\u53f7 == \u6765\u5224\u65ad\u662f\u5426\u76f8\u7b49\uff0c\u800c\u4e0d\u662f\u8d4b\u503c\u53f7 = \uff0c\u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { std :: cout << \"Enter 0 or 1: \" ; int x {}; std :: cin >> x ; if ( x = 0 ) // \u7cdf\u7cd5, \u6211\u4eec\u4f7f\u7528\u8d4b\u503c\u53f7\u8fdb\u884c\u4e86\u8d4b\u503c\u800c\u4e0d\u662f\u5224\u65ad\u76f8\u7b49 std :: cout << \"You entered 0\" ; else std :: cout << \"You entered 1\" ; return 0 ; } \u7a0b\u5e8f\u53ef\u4ee5\u7f16\u8bd1\u8fd0\u884c\uff0c\u4f46\u662f\u67d0\u4e9b\u60c5\u51b5\u4e0b\u7ed3\u679c\u662f\u9519\u8bef\u7684\uff1a 1 2 Enter 0 or 1: 0 You entered 1 \u4e8b\u5b9e\u4e0a\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u603b\u662f\u4f1a\u4ea7\u751f\u201c\u4f60\u8f93\u5165\u4e861\u201d\u7684\u7ed3\u679c\u3002\u8fd9\u662f\u56e0\u4e3a x = 0 \u9996\u5148\u5c06\u503c 0 \u8d4b\u503c\u7ed9 x \uff0c\u7136\u540e\u8ba1\u7b97 x \u7684\u503c\uff0c\u73b0\u5728\u662f 0 \uff0c\u5373\u5e03\u5c14\u503c false \u3002\u7531\u4e8e\u6761\u4ef6\u603b\u662f false \u6240\u4ee5 else \u8bed\u53e5\u603b\u662f\u6267\u884c\u3002","title":"7.3 - \u5e38\u89c1\u7684 if \u8bed\u53e5\u9519\u8bef"},{"location":"7-3-Common-if-statement-problems/#ifelse","text":"if \u8bed\u53e5\u53ef\u4ee5\u5d4c\u5957\u5728\u5176\u4ed6 if \u8bed\u53e5\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x >= 0 ) // \u5916\u5c42if\u8bed\u53e5 // \u8fd9\u79cd\u5d4c\u5957\u65b9\u5f0f\u662f\u4e0d\u597d\u7684\u7f16\u7a0b\u98ce\u683c if ( x <= 20 ) // \u5185\u5c42if\u8bed\u53e5 std :: cout << x << \" is between 0 and 20 \\n \" ; return 0 ; } \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x >= 0 ) // \u5916\u5c42if\u8bed\u53e5 // \u8fd9\u79cd\u5d4c\u5957\u65b9\u5f0f\u662f\u4e0d\u597d\u7684\u7f16\u7a0b\u98ce\u683c if ( x <= 20 ) // \u5185\u5c42if\u8bed\u53e5 std :: cout << x << \" is between 0 and 20 \\n \" ; // \u8fd9\u4e2a else \u5e94\u8be5\u548c\u54ea\u4e2a if \u8bed\u53e5\u5339\u914d\uff1f else std :: cout << x << \" is negative \\n \" ; return 0 ; } \u4e0a\u9762\u7684\u7a0b\u5e8f\u5f15\u5165\u4e86\u4e00\u4e2a\u6f5c\u5728\u7684\u6b67\u4e49\u6765\u6e90\uff0c\u79f0\u4e3a\u60ac\u5782else\u95ee\u9898\u3002\u4e0a\u9762\u7a0b\u5e8f\u4e2d\u7684 else \u8bed\u53e5\u5e94\u8be5\u4e0e\u5916\u90e8if\u5339\u914d\u8fd8\u662f\u548c\u5185\u90e8if\u5339\u914d\uff1f \u7b54\u6848\u662f else \u8bed\u53e5\u4e0e\u540c\u4e00\u8bed\u53e5\u5757\u4e2d\u6700\u540e\u4e00\u4e2a\u672a\u5339\u914d\u7684 if \u8bed\u53e5\u914d\u5bf9\u3002\u56e0\u6b64\uff0c\u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c else \u4e0e\u5185\u90e8\u7684 if \u8bed\u53e5\u5339\u914d\uff0c\u7b49\u4ef7\u4e8e\u4e0b\u9762\u5f62\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x >= 0 ) // \u5916\u5c42 if \u8bed\u53e5 { if ( x <= 20 ) // \u5185\u5c42 if \u8bed\u53e5 std :: cout << x << \" is between 0 and 20 \\n \" ; else // \u548c\u5185\u5c42 if \u8bed\u53e5\u5339\u914d std :: cout << x << \" is negative \\n \" ; } return 0 ; } \u8fd9\u4f7f\u5f97\u7a0b\u5e8f\u4ea7\u751f\u9519\u8bef\u7684\u7ed3\u679c\uff1a 1 2 Enter a number: 21 21 is negative \u4e3a\u4e86\u907f\u514d\u56e0\u5d4c\u5957 if \u8bed\u53e5\u4ea7\u751f\u7684\u6b67\u4e49\uff0c\u6700\u597d\u7684\u529e\u6cd5\u662f\u5c06\u5185\u90e8\u7684if\u8bed\u53e5\u663e\u5f0f\u5730\u5b9a\u4e49\u5728\u8bed\u53e5\u5757\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x >= 0 ) { if ( x <= 20 ) std :: cout << x << \" is between 0 and 20 \\n \" ; else // attached to inner if statement std :: cout << x << \" is greater than 20 \\n \" ; } else // attached to outer if statement std :: cout << x << \" is negative \\n \" ; return 0 ; } \u6b64\u65f6\uff0c\u8bed\u53e5\u5757\u4e2d\u7684 else \u8bed\u53e5\u4f1a\u548c\u5185\u5c42\u7684 if \u8bed\u53e5\u5339\u914d\uff0c\u800c\u8bed\u53e5\u5757\u5916\u90e8\u7684 else \u5219\u548c\u5916\u5c42\u7684 if \u8bed\u53e5\u5339\u914d\u3002","title":"\u5d4c\u5957if\u8bed\u53e5\u548c\u60ac\u5782else\u95ee\u9898"},{"location":"7-3-Common-if-statement-problems/#_1","text":"\u5d4c\u5957\u7684\u591a\u5c42 if \u8bed\u53e5\u53ef\u4ee5\u901a\u8fc7\u91cd\u65b0\u7ec4\u7ec7\u903b\u8f91\u6216\u4f7f\u7528 \u903b\u8f91\u8fd0\u7b97\u7b26 \u5c55\u5f00\u6210\u4e00\u5c42\u903b\u8f91\u3002\u5d4c\u5957\u8d8a\u5c11\u7684\u4ee3\u7801\u8d8a\u4e0d\u5bb9\u6613\u51fa\u9519\u3002 \u4f8b\u5982\uff0c\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u53ef\u4ee5\u5c55\u5f00\u6210\u5982\u4e0b\u5f62\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x < 0 ) std :: cout << x << \" is negative \\n \" ; else if ( x <= 20 ) // only executes if x >= 0 std :: cout << x << \" is between 0 and 20 \\n \" ; else // only executes if x > 20 std :: cout << x << \" is greater than 20 \\n \" ; return 0 ; } \u4e0b\u9762\u662f\u53e6\u4e00\u4e2a\u4f7f\u7528\u903b\u8f91\u8fd0\u7b97\u7b26\u5728\u4e00\u4e2a if \u8bed\u53e5\u4e2d\u68c0\u67e5\u591a\u4e2a\u6761\u4ef6\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another integer: \" ; int y {}; std :: cin >> y ; if ( x > 0 && y > 0 ) // && \u662f\u903b\u8f91\u4e0e\u2014\u2014\u68c0\u67e5\u662f\u5426\u4e24\u4e2a\u6761\u4ef6\u5747\u4e3a\u771f std :: cout << \"Both numbers are positive \\n \" ; else if ( x > 0 || y > 0 ) // || \u662f\u903b\u8f91\u6216\u2014\u2014\u68c0\u67e5\u662f\u5426\u6709\u4e3a\u771f\u7684\u6761\u4ef6 std :: cout << \"One of the numbers is positive \\n \" ; else std :: cout << \"Neither number is positive \\n \" ; return 0 ; }","title":"\u5d4c\u5957\u8bed\u53e5\u5c55\u5f00"},{"location":"7-3-Common-if-statement-problems/#_2","text":"\u7a7a\u8bed\u53e5\u662f\u4e00\u4e2a\u53ea\u5305\u542b\u5206\u53f7\u7684\u8bed\u53e5\uff1a 1 2 if ( x > 10 ) ; // this is a null statement \u7a7a\u8bed\u53e5\u4ec0\u4e48\u90fd\u4e0d\u505a\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u53ea\u6709\u5f53\u8bed\u8a00\u8981\u6c42\u5b58\u5728\u4e00\u6761\u8bed\u53e5\u4f46\u7a0b\u5e8f\u5458\u4e0d\u9700\u8981\u7684\u60c5\u51b5\u4e0b\u624d\u8fd9\u4e48\u505a\u3002\u4e3a\u4e86\u53ef\u8bfb\u6027\uff0c\u201c\u7a7a\u8bed\u53e5\u201d\u901a\u5e38\u88ab\u653e\u5728\u5355\u72ec\u7684\u884c\u4e2d\u3002 \u6211\u4eec\u5c06\u5728\u672c\u7ae0\u540e\u9762\u8bb2\u5230\u5faa\u73af\u7684\u65f6\u5019\u770b\u5230\u6709\u610f\u7684\u7a7a\u8bed\u53e5\u7684\u4f8b\u5b50\u3002\u201c\u7a7a\u8bed\u53e5\u201d\u5f88\u5c11\u4e0e\u201cif\u8bed\u53e5\u201d\u4e00\u8d77\u4f7f\u7528\u3002\u7136\u800c\uff0c\u5b83\u4eec\u53ef\u80fd\u4f1a\u65e0\u610f\u4e2d\u7ed9\u65b0\u624b(\u6216\u7c97\u5fc3\u7684)\u7a0b\u5e8f\u5458\u5e26\u6765\u95ee\u9898\u3002\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 if ( nuclearCodesActivated ()); blowUpTheWorld (); \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u5458\u4e0d\u7ecf\u610f\u95f4\u5728if\u8bed\u53e5\u7684\u672b\u5c3e\u6dfb\u52a0\u4e86\u4e00\u4e2a\u5206\u53f7\u3002\u8fd9\u4e2a\u4e0d\u8d77\u773c\u7684\u9519\u8bef\u53ef\u4ee5\u5f88\u597d\u5730\u7f16\u8bd1\uff0c\u5e76\u4f7f\u4ee3\u7801\u6bb5\u6267\u884c\u8d77\u6765\u5c31\u50cf\u5b83\u662f\u8fd9\u6837\u7f16\u5199\u7684\u4e00\u6837\uff1a 1 2 3 if ( nuclearCodesActivated ()) ; // \u5206\u53f7\u5b9e\u9645\u4e0a\u5236\u9020\u4e86\u4e00\u4e2a\u7a7a\u8bed\u53e5 blowUpTheWorld (); // \u8fd9\u4e2a\u51fd\u6570\u603b\u662f\u4f1a\u6267\u884c \u6ce8\u610f \u6ce8\u610f\u4e0d\u8981\u5728if\u8bed\u53e5\u540e\u9762\u6dfb\u52a0\u5206\u53f7\uff0c\u5426\u5219\u4f60\u7684\u201c\u6761\u4ef6\u8bed\u53e5\u201d\u603b\u662f\u4f1a\u65e0\u6761\u4ef6\u6267\u884c\uff08\u5373\u4f7f\u4f60\u628a\u5b83\u5199\u5728\u8bed\u53e5\u5757\u4e2d\uff09\u3002","title":"\u7a7a\u8bed\u53e5"},{"location":"7-3-Common-if-statement-problems/#_3","text":"\u5728 if \u7684\u6761\u4ef6\u4e2d\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528\u7b49\u53f7 == \u6765\u5224\u65ad\u662f\u5426\u76f8\u7b49\uff0c\u800c\u4e0d\u662f\u8d4b\u503c\u53f7 = \uff0c\u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { std :: cout << \"Enter 0 or 1: \" ; int x {}; std :: cin >> x ; if ( x = 0 ) // \u7cdf\u7cd5, \u6211\u4eec\u4f7f\u7528\u8d4b\u503c\u53f7\u8fdb\u884c\u4e86\u8d4b\u503c\u800c\u4e0d\u662f\u5224\u65ad\u76f8\u7b49 std :: cout << \"You entered 0\" ; else std :: cout << \"You entered 1\" ; return 0 ; } \u7a0b\u5e8f\u53ef\u4ee5\u7f16\u8bd1\u8fd0\u884c\uff0c\u4f46\u662f\u67d0\u4e9b\u60c5\u51b5\u4e0b\u7ed3\u679c\u662f\u9519\u8bef\u7684\uff1a 1 2 Enter 0 or 1: 0 You entered 1 \u4e8b\u5b9e\u4e0a\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u603b\u662f\u4f1a\u4ea7\u751f\u201c\u4f60\u8f93\u5165\u4e861\u201d\u7684\u7ed3\u679c\u3002\u8fd9\u662f\u56e0\u4e3a x = 0 \u9996\u5148\u5c06\u503c 0 \u8d4b\u503c\u7ed9 x \uff0c\u7136\u540e\u8ba1\u7b97 x \u7684\u503c\uff0c\u73b0\u5728\u662f 0 \uff0c\u5373\u5e03\u5c14\u503c false \u3002\u7531\u4e8e\u6761\u4ef6\u603b\u662f false \u6240\u4ee5 else \u8bed\u53e5\u603b\u662f\u6267\u884c\u3002","title":"\u6761\u4ef6\u8bed\u53e5\u4e2d\u7684\u7b49\u53f7\u548c\u8d4b\u503c\u53f7"},{"location":"7-4-Switch-statement-basics/","tags":["switch","control flow"],"text":"Key Takeaway \u5c3d\u7ba1\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u5f88\u591a\u94fe\u5f0f if-else\uff0c\u4f46\u662f\u8fd9\u4e48\u505a\u4e0d\u4ec5\u5199\u8d77\u6765\u8d39\u52b2\uff0c\u6548\u7387\u4e5f\u4e0d\u9ad8\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include void printDigitName ( int x ) { if ( x == 1 ) std :: cout << \"One\" ; else if ( x == 2 ) std :: cout << \"Two\" ; else if ( x == 3 ) std :: cout << \"Three\" ; else std :: cout << \"Unknown\" ; } int main () { printDigitName ( 2 ); return 0 ; } \u8fd9\u4e2a\u4f8b\u5b50\u5e76\u4e0d\u590d\u6742\uff0cx \u6700\u591a\u4f1a\u88ab\u6c42\u503c\u4e09\u6b21\uff08\u4e0d\u9ad8\u6548\uff09\uff0c\u8bfb\u8005\u4e5f\u5fc5\u987b\u660e\u767d\u88ab\u6c42\u503c\u591a\u6b21\u7684\u662f x \u800c\u4e0d\u662f\u5176\u4ed6\u53d8\u91cf\u3002 \u7531\u4e8e\u9488\u5bf9\u4e00\u7ec4\u4e0d\u540c\u7684\u503c\u6d4b\u8bd5\u53d8\u91cf\u6216\u8868\u8fbe\u5f0f\u662f\u5426\u76f8\u7b49\u662f\u5f88\u5e38\u89c1\u7684\uff0cC++\u63d0\u4f9b\u4e86\u53e6\u5916\u4e00\u79cd\u53ef\u9009\u7684\u6761\u4ef6\u8bed\u53e5\uff0c\u79f0\u4e3a switch\u8bed\u53e5 \uff0c\u4e13\u95e8\u7528\u4e8e\u6b64\u76ee\u7684\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u7528switch\u8bed\u53e5\u5b9e\u73b0\u4e86\u76f8\u540c\u7684\u529f\u80fd\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include void printDigitName ( int x ) { switch ( x ) { case 1 : std :: cout << \"One\" ; return ; case 2 : std :: cout << \"Two\" ; return ; case 3 : std :: cout << \"Three\" ; return ; default : std :: cout << \"Unknown\" ; return ; } } int main () { printDigitName ( 2 ); return 0 ; } switch \u80cc\u540e\u7684\u601d\u60f3\u5f88\u7b80\u5355\uff1a\u8868\u8fbe\u5f0f(\u6709\u65f6\u79f0\u4e3a\u6761\u4ef6)\u6c42\u503c\u540e\u5f97\u5230\u4e00\u4e2a\u503c\u3002\u5982\u679c\u8868\u8fbe\u5f0f\u7684\u503c\u80fd\u591f\u548c\u540e\u9762\u7684\u67d0\u4e2a\u5206\u652f\u6807\u7b7e\u5339\u914d\uff0c\u5219\u8be5\u6807\u7b7e\u540e\u7684\u8bed\u53e5\u5c31\u4f1a\u88ab\u6267\u884c\u3002\u5982\u679c\u6ca1\u6709\u5339\u914d\u7684\u6807\u7b7e\uff0c\u4f46\u662f\u5b58\u5728default\u6807\u7b7e\uff0c\u5219\u6267\u884cdefault\u540e\u9762\u7684\u8bed\u53e5\u3002 \u548cif\u8bed\u53e5\u76f8\u6bd4\uff0cswitch\u8bed\u53e5\u7684\u4f18\u52bf\u5728\u4e8e\u6761\u4ef6\u6c42\u503c\u53ea\u9700\u8981\u505a\u4e00\u6b21\uff08\u66f4\u9ad8\u6548\uff09\uff0c\u800c\u4e14switch\u8bed\u53e5\u770b\u8d77\u6765\u66f4\u6e05\u695a\uff0c\u53ef\u8bfb\u6027\u66f4\u597d\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u53ef\u4ee5\u7684\u8bdd\uff0c\u5c3d\u91cf\u7528 switch \u8bed\u53e5\u4ee3\u66ff if-else \u8bed\u53e5\u94fe\u3002 \u8ba9\u6211\u4eec\u66f4\u8be6\u7ec6\u5730\u7814\u7a76\u8fd9\u4e9b\u6982\u5ff5\u3002 \u521b\u5efa switch \u4f7f\u7528 switch \u5173\u952e\u5b57\uff0c\u540e\u9762\u52a0\u4e0a\u62ec\u53f7\u4ee5\u53ca\u9700\u8981\u5728\u62ec\u53f7\u4e2d\u6c42\u503c\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\uff0c\u5c31\u53ef\u4ee5\u521b\u5efaswitch\u8bed\u53e5\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\u8868\u8fbe\u5f0f\u4f1a\u662f\u4e00\u4e2a\u5355\u4e00\u53d8\u91cf\uff0c\u4f46\u662f\u4e5f\u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u7684\u8868\u8fbe\u5f0f\u3002 \u5173\u4e8e\u8fd9\u91cc\u7684\u6761\u4ef6\u8bed\u53e5\uff0c\u5b83\u7684\u4e00\u4e2a\u9650\u5236\u6761\u4ef6\u662f\u5176\u6c42\u503c\u7ed3\u679c\u5fc5\u987b\u4e3a \u6574\u578b\u7c7b\u578b (\u5982\u4f55\u4f60\u8fd8\u4e0d\u719f\u6089\u6574\u578b\u7c7b\u578b\uff0c\u8bf7\u53c2\u89c1\uff1a 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb ) \u6216 \u6709\u4f5c\u7528\u57df\u679a\u4e3e \u7c7b\u578b(\u53c2\u89c1\uff1a 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b )\uff0c\u6216\u8005\u662f\u4efb\u4f55\u80fd\u8f6c\u6362\u6210\u4e0a\u8ff0\u7c7b\u578b\u7684\u503c\u3002\u6d6e\u70b9\u7c7b\u578b\u3001\u5b57\u7b26\u4e32\u548c\u5927\u591a\u6570\u5176\u4ed6\u975e\u6574\u578b\u7684\u8868\u8fbe\u5f0f\u90fd\u4e0d\u53ef\u7528\u3002 \u6269\u5c55\u9605\u8bfb \u4e3a\u4ec0\u4e48switch\u53ea\u4f7f\u7528\u6574\u6570(\u6216\u679a\u4e3e)\u7c7b\u578b\uff1f\u7b54\u6848\u662f\u56e0\u4e3aswitch\u8bed\u53e5\u88ab\u9ad8\u5ea6\u4f18\u5316\u4e86\u3002\u5386\u53f2\u4e0a\uff0c\u7f16\u8bd1\u5668\u5b9e\u73b0switch\u8bed\u53e5\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u901a\u8fc7 \u8df3\u8868 \u5b9e\u73b0\u7684\uff0c\u800c\u8df3\u8f6c\u8868\u53ea\u9002\u7528\u4e8e\u6574\u6570\u503c\u3002 \u5bf9\u4e8e\u90a3\u4e9b\u5df2\u7ecf\u719f\u6089\u6570\u7ec4\u7684\u4eba\u6765\u8bf4\uff0c\u8df3\u8868\u7684\u5de5\u4f5c\u539f\u7406\u975e\u5e38\u7c7b\u4f3c\u4e8e\u6570\u7ec4\uff0c\u4f7f\u7528\u6574\u6570\u503c\u4f5c\u4e3a\u6570\u7ec4\u7d22\u5f15\uff0c\u76f4\u63a5\u201c\u8df3\u8f6c\u201d\u5230\u7ed3\u679c\u3002\u8fd9\u6bd4\u6267\u884c\u4e00\u5806\u987a\u5e8f\u6bd4\u8f83\u8981\u9ad8\u6548\u5f97\u591a\u3002 \u5f53\u7136\uff0c\u7f16\u8bd1\u5668\u4e0d\u5fc5\u4f7f\u7528\u8df3\u8868\u5b9e\u73b0switch\uff0c\u6709\u65f6\u4e5f\u4e0d\u9700\u8981\u3002\u4ece\u6280\u672f\u4e0a\u8bb2\uff0cC++\u6ca1\u6709\u7406\u7531\u4e0d\u80fd\u653e\u677e\u6b64\u5904\u7684\u7c7b\u578b\u9650\u5236\uff0c\u4ee5\u4fbf\u5176\u4ed6\u7c7b\u578b\u4e5f\u53ef\u4ee5\u4f7f\u7528\uff0c\u53ea\u662f\u76ee\u524d\u8fd8\u6ca1\u6709\u8fd9\u6837\u505a\u7f62\u4e86(\u76f4\u5230C++20)\u3002 \u5728\u6761\u4ef6\u8868\u8fbe\u5f0f\u540e\u9762\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u8bed\u53e5\u5757\u3002\u5728\u8bed\u53e5\u5757\u5185\u90e8\uff0c\u4f7f\u7528\u6807\u7b7e\u6765\u5b9a\u4e49\u6211\u4eec\u60f3\u8981\u6d4b\u8bd5\u662f\u5426\u76f8\u7b49\u7684\u6240\u6709\u503c\uff0c\u6807\u7b7e\u6709\u4e24\u79cd\uff1a \u5206\u652f\u6807\u7b7e \u7b2c\u4e00\u79cd\u6807\u7b7e\u662f\u5206\u652f\u6807\u7b7e\uff0c\u5b83\u4f7f\u7528 case \u5173\u952e\u5b57\u58f0\u660e\uff0c\u540e\u9762\u8ddf\u7740\u4e00\u4e2a\u5e38\u91cf\u8868\u8fbe\u5f0f\u3002\u5e38\u91cf\u8868\u8fbe\u5f0f\u5fc5\u987b\u5339\u914d\u6761\u4ef6\u7684\u7c7b\u578b\uff0c\u6216\u53ef\u4ee5\u8f6c\u6362\u4e3a\u8be5\u7c7b\u578b\u3002 \u5982\u679c\u6761\u4ef6\u8868\u8fbe\u5f0f\u6c42\u503c\u7ed3\u679c\u7b49\u4e8e\u67d0\u4e2a\u5206\u652f\u6807\u7b7e\u7684\u503c\uff0c\u5219\u4ece\u8be5\u6807\u7b7e\u540e\u9762\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u7ee7\u7eed\u987a\u5e8f\u6267\u884c\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6761\u4ef6\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5339\u914d\u4e00\u4e2a\u5206\u652f\u6807\u7b7e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include void printDigitName ( int x ) { switch ( x ) // x \u6c42\u503c\u4e3a 2 { case 1 : std :: cout << \"One\" ; return ; case 2 : // \u5339\u914d std :: cout << \"Two\" ; // \u4ece\u8fd9\u91cc\u5f00\u59cb\u6267\u884c return ; // \u6267\u884c\u5230\u8fd9\u6761\u8bed\u53e5\u540e\u5c31\u8fd4\u56de case 3 : std :: cout << \"Three\" ; return ; default : std :: cout << \"Unknown\" ; return ; } } int main () { printDigitName ( 2 ); return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 Two \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c x \u6c42\u503c\u4e3a2\u3002\u56e0\u4e3a\u5206\u652f\u6807\u7b7e\u4e2d\u4e5f\u67092\uff0c\u6240\u4ee5\u7a0b\u5e8f\u4f1a\u8df3\u8f6c\u5230\u8be5\u6807\u7b7e\u4e0b\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u5f00\u59cb\u6267\u884c\u3002\u8be5\u7a0b\u5e8f\u4f1a\u6253\u5370 Two \uff0c\u7136\u540e\u4ece return \u8bed\u53e5\u8fd4\u56de\u7ed9\u8c03\u7528\u8005\u3002 \u5206\u652f\u6807\u7b7e\u7684\u4e2a\u6570\u5e76\u6ca1\u6709\u73b0\u5728\uff0c\u4f46\u662f\u6bcf\u4e2a\u6807\u7b7e\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u6240\u4ee5\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u9519\u8bef\u7684\uff1a 1 2 3 4 5 6 switch ( x ) { case 54 : case 54 : // error: already used value 54! case '6' : // error: '6' converts to integer value 54, which is already used } default \u6807\u7b7e \u7b2c\u4e8c\u79cd\u6807\u7b7e\u79f0\u4e3a default \u6807\u7b7e(\u7ecf\u5e38\u88ab\u79f0\u4e3a\u9ed8\u8ba4\u5206\u652f)\uff0c\u4f7f\u7528 default \u5173\u952e\u5b57\u5b9a\u4e49\u3002\u5982\u679c\u6761\u4ef6\u8868\u8fbe\u5f0f\u4e0d\u80fd\u591f\u5339\u914d\u4efb\u4f55\u5176\u4ed6\u6807\u7b7e\u4e14default \u6807\u7b7e\u5b58\u5728\uff0c\u5219\u4ecedefault\u6807\u7b7e\u540e\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u5f00\u59cb\u6267\u884c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include void printDigitName ( int x ) { switch ( x ) // x is evaluated to produce value 5 { case 1 : std :: cout << \"One\" ; return ; case 2 : std :: cout << \"Two\" ; return ; case 3 : std :: cout << \"Three\" ; return ; default : // which does not match to any case labels std :: cout << \"Unknown\" ; // so execution starts here return ; // and then we return to the caller } } int main () { printDigitName ( 5 ); return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 Unknown default \u5206\u652f\u662f\u53ef\u9009\u7684\uff0c\u5982\u679c\u6709\u7684\u8bdd\u5219\u53ea\u80fd\u6709\u4e00\u4e2a\u3002\u4e00\u822c\u6765\u8bb2\uff0cdefault\u5206\u652f\u4f1a\u88ab\u653e\u7f6e\u5728switch\u8bed\u53e5\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5206\u652f\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c06 default \u4f5c\u4e3a\u6700\u540e\u4e00\u4e2a\u5206\u652f\u3002 \u6ca1\u6709\u5339\u914d\u7684\u5206\u652f\u4e5f\u6ca1\u6709\u9ed8\u8ba4\u5206\u652f \u5982\u679c\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u503c\u4e0d\u5339\u914d\u4efb\u4f55\u5206\u652f\uff0c\u5e76\u4e14\u6ca1\u6709\u63d0\u4f9b\u9ed8\u8ba4\u5206\u652f\uff0c\u5219switch\u5185\u90e8\u4e0d\u4f1a\u6267\u884c\u4efb\u4f55case\u3002\u5728switch\u7ed3\u675f\u540e\u7ee7\u7eed\u6267\u884c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include void printDigitName ( int x ) { switch ( x ) // x is evaluated to produce value 5 { case 1 : std :: cout << \"One\" ; return ; case 2 : std :: cout << \"Two\" ; return ; case 3 : std :: cout << \"Three\" ; return ; // no matching case exists and there is no default case } // so execution continues here std :: cout << \"Hello\" ; } int main () { printDigitName ( 5 ); std :: cout << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c x \u6c42\u503c\u4e3a 5 \uff0c\u4f46\u662f\u7531\u4e8e\u6ca1\u6709\u5339\u914d\u7684\u5206\u652f\uff0c\u4e5f\u6ca1\u6709\u9ed8\u8ba4\u5206\u652f\u3002\u6240\u4ee5\u4e0d\u4f1a\u8d70\u5230\u4efb\u4f55case\u4e2d\uff0c\u7a0b\u5e8f\u5728switch\u5757\u7ed3\u675f\u540e\u7ee7\u7eed\u5411\u4e0b\u6267\u884c\u3002\u6253\u5370 Hello \u3002 \u4f7f\u7528 break \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 return \u8bed\u53e5\u6765\u505c\u6b62\u6807\u7b7e\u540e\u9762\u8bed\u53e5\u7684\u6267\u884c\u3002\u4f46\u662f\uff0c\u8fd9\u4e5f\u4f1a\u9000\u51fa\u6574\u4e2a\u51fd\u6570\u3002 break \u8bed\u53e5(\u4f7f\u7528 break \u5173\u952e\u5b57\u58f0\u660e)\u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u6211\u4eec\u5df2\u7ecf\u5b8c\u6210\u4e86switch\u8bed\u53e5\u7684\u6267\u884c\u4f46\u9700\u8981\u7ee7\u7eed\u6267\u884c\u5916\u9762\u7684\u8bed\u53e5\u3002\u8fd9\u5141\u8bb8\u6211\u4eec\u5728\u4e0d\u9000\u51fa\u6574\u4e2a\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\u9000\u51fa\u201cswitch\u8bed\u53e5\u201d\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u7528 break \u800c\u4e0d\u662f return \u91cd\u5199\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include void printDigitName ( int x ) { switch ( x ) // x evaluates to 3 { case 1 : std :: cout << \"One\" ; break ; case 2 : std :: cout << \"Two\" ; break ; case 3 : std :: cout << \"Three\" ; // \u4ece\u8fd9\u91cc\u5f00\u59cb\u6267\u884c break ; // \u8df3\u5230 switch \u7ed3\u675f\u65f6 default : std :: cout << \"Unknown\" ; break ; } // \u4ece\u8fd9\u91cc\u7ee7\u7eed\u6267\u884c std :: cout << \" Ah-Ah-Ah!\" ; } int main () { printDigitName ( 3 ); return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 Three Ah-Ah-Ah! \u6700\u4f73\u5b9e\u8df5 \u6807\u7b7e\u4e0b\u7684\u6bcf\u7ec4\u8bed\u53e5\u90fd\u5e94\u8be5\u4ee5\u201cbreak\u8bed\u53e5\u201d\u6216\u201creturn\u8bed\u53e5\u201d\u7ed3\u675f\u3002\u8fd9\u5305\u62ecswitch\u6700\u540e\u4e00\u4e2a\u6807\u7b7e\u4e0b\u9762\u7684\u8bed\u53e5\u3002 \u90a3\u4e48\uff0c\u5982\u679c\u4e0d\u4ee5 break \u6216 return \u4f5c\u4e3a\u4e00\u7ec4\u8bed\u53e5\u7684\u7ed3\u5c3e\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462?\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u63a2\u8ba8\u8fd9\u4e2a\u8bdd\u9898\u548c\u5176\u4ed6\u8bdd\u9898\u3002","title":"7.4 - switch \u8bed\u53e5\u57fa\u7840"},{"location":"7-4-Switch-statement-basics/#switch","text":"\u4f7f\u7528 switch \u5173\u952e\u5b57\uff0c\u540e\u9762\u52a0\u4e0a\u62ec\u53f7\u4ee5\u53ca\u9700\u8981\u5728\u62ec\u53f7\u4e2d\u6c42\u503c\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\uff0c\u5c31\u53ef\u4ee5\u521b\u5efaswitch\u8bed\u53e5\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\u8868\u8fbe\u5f0f\u4f1a\u662f\u4e00\u4e2a\u5355\u4e00\u53d8\u91cf\uff0c\u4f46\u662f\u4e5f\u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u7684\u8868\u8fbe\u5f0f\u3002 \u5173\u4e8e\u8fd9\u91cc\u7684\u6761\u4ef6\u8bed\u53e5\uff0c\u5b83\u7684\u4e00\u4e2a\u9650\u5236\u6761\u4ef6\u662f\u5176\u6c42\u503c\u7ed3\u679c\u5fc5\u987b\u4e3a \u6574\u578b\u7c7b\u578b (\u5982\u4f55\u4f60\u8fd8\u4e0d\u719f\u6089\u6574\u578b\u7c7b\u578b\uff0c\u8bf7\u53c2\u89c1\uff1a 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb ) \u6216 \u6709\u4f5c\u7528\u57df\u679a\u4e3e \u7c7b\u578b(\u53c2\u89c1\uff1a 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b )\uff0c\u6216\u8005\u662f\u4efb\u4f55\u80fd\u8f6c\u6362\u6210\u4e0a\u8ff0\u7c7b\u578b\u7684\u503c\u3002\u6d6e\u70b9\u7c7b\u578b\u3001\u5b57\u7b26\u4e32\u548c\u5927\u591a\u6570\u5176\u4ed6\u975e\u6574\u578b\u7684\u8868\u8fbe\u5f0f\u90fd\u4e0d\u53ef\u7528\u3002 \u6269\u5c55\u9605\u8bfb \u4e3a\u4ec0\u4e48switch\u53ea\u4f7f\u7528\u6574\u6570(\u6216\u679a\u4e3e)\u7c7b\u578b\uff1f\u7b54\u6848\u662f\u56e0\u4e3aswitch\u8bed\u53e5\u88ab\u9ad8\u5ea6\u4f18\u5316\u4e86\u3002\u5386\u53f2\u4e0a\uff0c\u7f16\u8bd1\u5668\u5b9e\u73b0switch\u8bed\u53e5\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u901a\u8fc7 \u8df3\u8868 \u5b9e\u73b0\u7684\uff0c\u800c\u8df3\u8f6c\u8868\u53ea\u9002\u7528\u4e8e\u6574\u6570\u503c\u3002 \u5bf9\u4e8e\u90a3\u4e9b\u5df2\u7ecf\u719f\u6089\u6570\u7ec4\u7684\u4eba\u6765\u8bf4\uff0c\u8df3\u8868\u7684\u5de5\u4f5c\u539f\u7406\u975e\u5e38\u7c7b\u4f3c\u4e8e\u6570\u7ec4\uff0c\u4f7f\u7528\u6574\u6570\u503c\u4f5c\u4e3a\u6570\u7ec4\u7d22\u5f15\uff0c\u76f4\u63a5\u201c\u8df3\u8f6c\u201d\u5230\u7ed3\u679c\u3002\u8fd9\u6bd4\u6267\u884c\u4e00\u5806\u987a\u5e8f\u6bd4\u8f83\u8981\u9ad8\u6548\u5f97\u591a\u3002 \u5f53\u7136\uff0c\u7f16\u8bd1\u5668\u4e0d\u5fc5\u4f7f\u7528\u8df3\u8868\u5b9e\u73b0switch\uff0c\u6709\u65f6\u4e5f\u4e0d\u9700\u8981\u3002\u4ece\u6280\u672f\u4e0a\u8bb2\uff0cC++\u6ca1\u6709\u7406\u7531\u4e0d\u80fd\u653e\u677e\u6b64\u5904\u7684\u7c7b\u578b\u9650\u5236\uff0c\u4ee5\u4fbf\u5176\u4ed6\u7c7b\u578b\u4e5f\u53ef\u4ee5\u4f7f\u7528\uff0c\u53ea\u662f\u76ee\u524d\u8fd8\u6ca1\u6709\u8fd9\u6837\u505a\u7f62\u4e86(\u76f4\u5230C++20)\u3002 \u5728\u6761\u4ef6\u8868\u8fbe\u5f0f\u540e\u9762\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u8bed\u53e5\u5757\u3002\u5728\u8bed\u53e5\u5757\u5185\u90e8\uff0c\u4f7f\u7528\u6807\u7b7e\u6765\u5b9a\u4e49\u6211\u4eec\u60f3\u8981\u6d4b\u8bd5\u662f\u5426\u76f8\u7b49\u7684\u6240\u6709\u503c\uff0c\u6807\u7b7e\u6709\u4e24\u79cd\uff1a","title":"\u521b\u5efa switch"},{"location":"7-4-Switch-statement-basics/#_1","text":"\u7b2c\u4e00\u79cd\u6807\u7b7e\u662f\u5206\u652f\u6807\u7b7e\uff0c\u5b83\u4f7f\u7528 case \u5173\u952e\u5b57\u58f0\u660e\uff0c\u540e\u9762\u8ddf\u7740\u4e00\u4e2a\u5e38\u91cf\u8868\u8fbe\u5f0f\u3002\u5e38\u91cf\u8868\u8fbe\u5f0f\u5fc5\u987b\u5339\u914d\u6761\u4ef6\u7684\u7c7b\u578b\uff0c\u6216\u53ef\u4ee5\u8f6c\u6362\u4e3a\u8be5\u7c7b\u578b\u3002 \u5982\u679c\u6761\u4ef6\u8868\u8fbe\u5f0f\u6c42\u503c\u7ed3\u679c\u7b49\u4e8e\u67d0\u4e2a\u5206\u652f\u6807\u7b7e\u7684\u503c\uff0c\u5219\u4ece\u8be5\u6807\u7b7e\u540e\u9762\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u7ee7\u7eed\u987a\u5e8f\u6267\u884c\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6761\u4ef6\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5339\u914d\u4e00\u4e2a\u5206\u652f\u6807\u7b7e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include void printDigitName ( int x ) { switch ( x ) // x \u6c42\u503c\u4e3a 2 { case 1 : std :: cout << \"One\" ; return ; case 2 : // \u5339\u914d std :: cout << \"Two\" ; // \u4ece\u8fd9\u91cc\u5f00\u59cb\u6267\u884c return ; // \u6267\u884c\u5230\u8fd9\u6761\u8bed\u53e5\u540e\u5c31\u8fd4\u56de case 3 : std :: cout << \"Three\" ; return ; default : std :: cout << \"Unknown\" ; return ; } } int main () { printDigitName ( 2 ); return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 Two \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c x \u6c42\u503c\u4e3a2\u3002\u56e0\u4e3a\u5206\u652f\u6807\u7b7e\u4e2d\u4e5f\u67092\uff0c\u6240\u4ee5\u7a0b\u5e8f\u4f1a\u8df3\u8f6c\u5230\u8be5\u6807\u7b7e\u4e0b\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u5f00\u59cb\u6267\u884c\u3002\u8be5\u7a0b\u5e8f\u4f1a\u6253\u5370 Two \uff0c\u7136\u540e\u4ece return \u8bed\u53e5\u8fd4\u56de\u7ed9\u8c03\u7528\u8005\u3002 \u5206\u652f\u6807\u7b7e\u7684\u4e2a\u6570\u5e76\u6ca1\u6709\u73b0\u5728\uff0c\u4f46\u662f\u6bcf\u4e2a\u6807\u7b7e\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u6240\u4ee5\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u9519\u8bef\u7684\uff1a 1 2 3 4 5 6 switch ( x ) { case 54 : case 54 : // error: already used value 54! case '6' : // error: '6' converts to integer value 54, which is already used }","title":"\u5206\u652f\u6807\u7b7e"},{"location":"7-4-Switch-statement-basics/#default","text":"\u7b2c\u4e8c\u79cd\u6807\u7b7e\u79f0\u4e3a default \u6807\u7b7e(\u7ecf\u5e38\u88ab\u79f0\u4e3a\u9ed8\u8ba4\u5206\u652f)\uff0c\u4f7f\u7528 default \u5173\u952e\u5b57\u5b9a\u4e49\u3002\u5982\u679c\u6761\u4ef6\u8868\u8fbe\u5f0f\u4e0d\u80fd\u591f\u5339\u914d\u4efb\u4f55\u5176\u4ed6\u6807\u7b7e\u4e14default \u6807\u7b7e\u5b58\u5728\uff0c\u5219\u4ecedefault\u6807\u7b7e\u540e\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u5f00\u59cb\u6267\u884c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include void printDigitName ( int x ) { switch ( x ) // x is evaluated to produce value 5 { case 1 : std :: cout << \"One\" ; return ; case 2 : std :: cout << \"Two\" ; return ; case 3 : std :: cout << \"Three\" ; return ; default : // which does not match to any case labels std :: cout << \"Unknown\" ; // so execution starts here return ; // and then we return to the caller } } int main () { printDigitName ( 5 ); return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 Unknown default \u5206\u652f\u662f\u53ef\u9009\u7684\uff0c\u5982\u679c\u6709\u7684\u8bdd\u5219\u53ea\u80fd\u6709\u4e00\u4e2a\u3002\u4e00\u822c\u6765\u8bb2\uff0cdefault\u5206\u652f\u4f1a\u88ab\u653e\u7f6e\u5728switch\u8bed\u53e5\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5206\u652f\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c06 default \u4f5c\u4e3a\u6700\u540e\u4e00\u4e2a\u5206\u652f\u3002","title":"default \u6807\u7b7e"},{"location":"7-4-Switch-statement-basics/#_2","text":"\u5982\u679c\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u503c\u4e0d\u5339\u914d\u4efb\u4f55\u5206\u652f\uff0c\u5e76\u4e14\u6ca1\u6709\u63d0\u4f9b\u9ed8\u8ba4\u5206\u652f\uff0c\u5219switch\u5185\u90e8\u4e0d\u4f1a\u6267\u884c\u4efb\u4f55case\u3002\u5728switch\u7ed3\u675f\u540e\u7ee7\u7eed\u6267\u884c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include void printDigitName ( int x ) { switch ( x ) // x is evaluated to produce value 5 { case 1 : std :: cout << \"One\" ; return ; case 2 : std :: cout << \"Two\" ; return ; case 3 : std :: cout << \"Three\" ; return ; // no matching case exists and there is no default case } // so execution continues here std :: cout << \"Hello\" ; } int main () { printDigitName ( 5 ); std :: cout << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c x \u6c42\u503c\u4e3a 5 \uff0c\u4f46\u662f\u7531\u4e8e\u6ca1\u6709\u5339\u914d\u7684\u5206\u652f\uff0c\u4e5f\u6ca1\u6709\u9ed8\u8ba4\u5206\u652f\u3002\u6240\u4ee5\u4e0d\u4f1a\u8d70\u5230\u4efb\u4f55case\u4e2d\uff0c\u7a0b\u5e8f\u5728switch\u5757\u7ed3\u675f\u540e\u7ee7\u7eed\u5411\u4e0b\u6267\u884c\u3002\u6253\u5370 Hello \u3002","title":"\u6ca1\u6709\u5339\u914d\u7684\u5206\u652f\u4e5f\u6ca1\u6709\u9ed8\u8ba4\u5206\u652f"},{"location":"7-4-Switch-statement-basics/#break","text":"\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 return \u8bed\u53e5\u6765\u505c\u6b62\u6807\u7b7e\u540e\u9762\u8bed\u53e5\u7684\u6267\u884c\u3002\u4f46\u662f\uff0c\u8fd9\u4e5f\u4f1a\u9000\u51fa\u6574\u4e2a\u51fd\u6570\u3002 break \u8bed\u53e5(\u4f7f\u7528 break \u5173\u952e\u5b57\u58f0\u660e)\u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u6211\u4eec\u5df2\u7ecf\u5b8c\u6210\u4e86switch\u8bed\u53e5\u7684\u6267\u884c\u4f46\u9700\u8981\u7ee7\u7eed\u6267\u884c\u5916\u9762\u7684\u8bed\u53e5\u3002\u8fd9\u5141\u8bb8\u6211\u4eec\u5728\u4e0d\u9000\u51fa\u6574\u4e2a\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\u9000\u51fa\u201cswitch\u8bed\u53e5\u201d\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u7528 break \u800c\u4e0d\u662f return \u91cd\u5199\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include void printDigitName ( int x ) { switch ( x ) // x evaluates to 3 { case 1 : std :: cout << \"One\" ; break ; case 2 : std :: cout << \"Two\" ; break ; case 3 : std :: cout << \"Three\" ; // \u4ece\u8fd9\u91cc\u5f00\u59cb\u6267\u884c break ; // \u8df3\u5230 switch \u7ed3\u675f\u65f6 default : std :: cout << \"Unknown\" ; break ; } // \u4ece\u8fd9\u91cc\u7ee7\u7eed\u6267\u884c std :: cout << \" Ah-Ah-Ah!\" ; } int main () { printDigitName ( 3 ); return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 Three Ah-Ah-Ah! \u6700\u4f73\u5b9e\u8df5 \u6807\u7b7e\u4e0b\u7684\u6bcf\u7ec4\u8bed\u53e5\u90fd\u5e94\u8be5\u4ee5\u201cbreak\u8bed\u53e5\u201d\u6216\u201creturn\u8bed\u53e5\u201d\u7ed3\u675f\u3002\u8fd9\u5305\u62ecswitch\u6700\u540e\u4e00\u4e2a\u6807\u7b7e\u4e0b\u9762\u7684\u8bed\u53e5\u3002 \u90a3\u4e48\uff0c\u5982\u679c\u4e0d\u4ee5 break \u6216 return \u4f5c\u4e3a\u4e00\u7ec4\u8bed\u53e5\u7684\u7ed3\u5c3e\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462?\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u63a2\u8ba8\u8fd9\u4e2a\u8bdd\u9898\u548c\u5176\u4ed6\u8bdd\u9898\u3002","title":"\u4f7f\u7528 break"},{"location":"7-5-Switch-fallthrough-and-scoping/","tags":["switch"],"text":"Key Takeaway \u672c\u8282\u8bfe\u7ee7\u7eed\u63a2\u7d22switch\u8bed\u53e5\u3002\u5728\u524d\u9762\u7684\u8bfe\u7a0b\u4e2d\uff08 7.4 - switch \u8bed\u53e5\u57fa\u7840 \uff09\u6211\u4eec\u63d0\u5230\u6bcf\u4e2a\u5206\u652f\u6807\u7b7e\u4e0b\u7684\u8bed\u53e5\u90fd\u5e94\u8be5\u4ee5 break \u6216 return \u7ed3\u5c3e\u3002 \u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u7d22\u5176\u4e2d\u7684\u539f\u56e0\uff0c\u5e76\u8ba8\u8bba\u4e00\u4e9b\u5f00\u5173\u4f5c\u7528\u57df\u7684\u95ee\u9898\uff0c\u8fd9\u4e9b\u95ee\u9898\u6709\u65f6\u4f1a\u7eca\u5012\u65b0\u7a0b\u5e8f\u5458\u3002 \u8d2f\u7a7f \u5f53\u4e00\u4e2aswitch\u8868\u8fbe\u5f0f\u5339\u914d\u4e00\u4e2acase\u6807\u7b7e\u6216\u53ef\u9009\u7684\u9ed8\u8ba4\u6807\u7b7e\u65f6\uff0c\u4ece\u5339\u914d\u6807\u7b7e\u540e\u9762\u7684\u7b2c\u4e00\u4e2a\u8bed\u53e5\u5f00\u59cb\u6267\u884c\u3002\u7136\u540e\u7ee7\u7eed\u6267\u884c\uff0c\u76f4\u5230\u4ee5\u4e0b\u7ec8\u6b62\u6761\u4ef6\u4e4b\u4e00\u53d1\u751f\uff1a \u5230\u8fbeswitch\u672b\u5c3e\uff1b \u9047\u5230\u5176\u4ed6\u63a7\u5236\u6d41\u8bed\u53e5\uff08\u4e00\u822c\u6765\u8bf4\u662f break \u6216\u8005 return \uff09\u5bfc\u81f4switch\u9000\u51fa\uff1b \u5176\u4ed6\u4e2d\u65ad\u5bfc\u81f4\u7a0b\u5e8f\u7684\u6b63\u5e38\u6d41\u7a0b\u88ab\u6253\u65ad(\u4f8b\u5982\uff0c\u64cd\u4f5c\u7cfb\u7edf\u505c\u6b62\u7a0b\u5e8f\u3001\u5b87\u5b99\u5927\u7206\u70b8\u7b49\u7b49)\u3002 \u6ce8\u610f\uff0c\u53e6\u4e00\u4e2acase\u6807\u7b7e\u7684\u51fa\u73b0\u4e0d\u662f\u8fd9\u4e9b\u7ec8\u6b62\u6761\u4ef6\u4e4b\u4e00\u2014\u2014\u56e0\u6b64\uff0c\u5982\u679c\u6ca1\u6709 break \u6216 return \uff0c\u6267\u884c\u5c06\u7ee7\u7eed\u6267\u884c\u540e\u7eed\u7684case\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int main () { switch ( 2 ) { case 1 : // Does not match std :: cout << 1 << '\\n' ; // \u8df3\u8fc7 case 2 : // Match! std :: cout << 2 << '\\n' ; // \u4ece\u8fd9\u91cc\u5f00\u59cb\u6267\u884c case 3 : std :: cout << 3 << '\\n' ; // \u4e5f\u4f1a\u88ab\u6267\u884c case 4 : std :: cout << 4 << '\\n' ; // \u4e5f\u4f1a\u88ab\u6267\u884c default : std :: cout << 5 << '\\n' ; // \u4e5f\u4f1a\u88ab\u6267\u884c } return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 2 3 4 5 \u8fd9\u53ef\u80fd\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684!\u5f53\u6267\u884c\u4ece\u6807\u7b7e\u4e0b\u9762\u7684\u8bed\u53e5\u6d41\u5230\u540e\u7eed\u6807\u7b7e\u4e0b\u9762\u7684\u8bed\u53e5\u65f6\uff0c\u8fd9\u79f0\u4e3a\u8d2f\u7a7f\u3002 \u6ce8\u610f \u4e00\u65e6case\u6216default\u6807\u7b7e\u4e0b\u7684\u8bed\u53e5\u5f00\u59cb\u6267\u884c\uff0c\u5b83\u4eec\u9ed8\u8ba4\u4f1a\u8d2f\u7a7f\u5230\u540e\u7eed\u7684case\u4e2d\u3002 break \u6216 return \u53ef\u4ee5\u9632\u6b62\u8fd9\u79cd\u60c5\u51b5\u3002 \u7531\u4e8e\u4eba\u4eec\u5f88\u5c11\u4f1a\u4e3b\u52a8\u4f7f\u7528\u8d2f\u7a7f\u7279\u6027\uff0c\u56e0\u6b64\u8bb8\u591a\u7f16\u8bd1\u5668\u548c\u4ee3\u7801\u5206\u6790\u5de5\u5177\u4f1a\u5c06\u8d2f\u7a7f\u6807\u8bb0\u4e3a\u8b66\u544a\u3002 \u8d2f\u7a7f [fallthrough](./fallthrough.md) \u5c5e\u6027 \u901a\u8fc7\u6ce8\u91ca\u544a\u8bc9\u5176\u4ed6\u5f00\u53d1\u4eba\u5458\u6545\u610f\u5b9e\u73b0\u7684\u8d2f\u7a7f\u662f\u4e00\u79cd\u5e38\u89c1\u60ef\u4f8b\u3002\u867d\u7136\u5176\u4ed6\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u7406\u89e3\uff0c\u4f46\u7f16\u8bd1\u5668\u548c\u4ee3\u7801\u5206\u6790\u5de5\u5177\u4e0d\u77e5\u9053\u5982\u4f55\u89e3\u91ca\u6ce8\u91ca\uff0c\u56e0\u6b64\u5b83\u65e0\u6cd5\u6d88\u9664\u8b66\u544a\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cC++17\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u5c5e\u6027 [fallthrough](./fallthrough.md) \u3002 \u5c5e\u6027\u662f\u73b0\u4ee3C++\u7684\u4e00\u4e2a\u7279\u6027\uff0c\u5b83\u5141\u8bb8\u7a0b\u5e8f\u5458\u5411\u7f16\u8bd1\u5668\u63d0\u4f9b\u4e00\u4e9b\u5173\u4e8e\u4ee3\u7801\u7684\u9644\u52a0\u6570\u636e\u3002\u8981\u6307\u5b9a\u5c5e\u6027\uff0c\u5c5e\u6027\u540d\u8981\u653e\u5728\u53cc\u5927\u62ec\u53f7\u4e4b\u95f4\u3002\u5c5e\u6027\u4e0d\u662f\u8bed\u53e5\u2014\u2014\u76f8\u53cd\uff0c\u5b83\u4eec\u51e0\u4e4e\u53ef\u4ee5\u5728\u4e0e\u4e0a\u4e0b\u6587\u76f8\u5173\u7684\u4efb\u4f55\u5730\u65b9\u4f7f\u7528\u3002 [fallthrough](./fallthrough.md) \u4f1a\u4fee\u6539\u4e00\u4e2a\u7a7a\u8bed\u53e5\uff0c\u8868\u660e\u6709\u610f\u800c\u4e3a\u4e4b\u7684\u8d2f\u7a7f\u64cd\u4f5c\uff08\u907f\u514d\u89e6\u53d1\u544a\u8b66\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { switch ( 2 ) { case 1 : std :: cout << 1 << '\\n' ; break ; case 2 : std :: cout << 2 << '\\n' ; // \u4ece\u8fd9\u91cc\u5f00\u59cb\u6267\u884c [ fallthrough ](. / fallthrough . md ); // \u6709\u610f\u5730\u8d2f\u7a7f\u64cd\u4f5c\u2014\u2014\u6ce8\u610f\u5206\u53f7\u521b\u5efa\u7684\u7a7a\u8bed\u53e5 case 3 : std :: cout << 3 << '\\n' ; // \u540c\u6837\u4f1a\u6267\u884c break ; } return 0 ; } \u8f93\u51fa\uff1a 1 2 2 3 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528 [fallthrough](./fallthrough.md) \u5c5e\u6027(\u548c\u7a7a\u8bed\u53e5)\u8868\u660e\u6709\u610f\u800c\u4e3a\u4e4b\u7684\u8d2f\u7a7f\u64cd\u4f5c\u3002 \u987a\u5e8f\u5206\u652f\u6807\u7b7e \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u6765\u8fde\u63a5\u591a\u4e2a\u6d4b\u8bd5\u6761\u4ef6\uff1a 1 2 3 4 5 bool isVowel ( char c ) { return ( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' ); } \u5728switch\u8bed\u53e5\u4e2d\u4e5f\u6709\u7c7b\u4f3c\u7684\u573a\u666f\uff1ac \u88ab\u591a\u6b21\u6c42\u503c\u5e76\u6d4b\u8bd5\uff0c\u6b64\u65f6\u8bfb\u4ee3\u7801\u7684\u4eba\u5fc5\u987b\u786e\u4fdd\u6bcf\u6b21\u88ab\u6c42\u503c\u548c\u6bd4\u8f83\u7684\u662fc\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u987a\u5e8f\u6392\u5217\u7684\u591a\u4e2a\u5206\u652f\u6807\u7b7e\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 bool isVowel ( char c ) { switch ( c ) { case 'a' : // if c is 'a' case 'e' : // or if c is 'e' case 'i' : // or if c is 'i' case 'o' : // or if c is 'o' case 'u' : // or if c is 'u' case 'A' : // or if c is 'A' case 'E' : // or if c is 'E' case 'I' : // or if c is 'I' case 'O' : // or if c is 'O' case 'U' : // or if c is 'U' return true ; default : return false ; } } \u8bb0\u4f4f\uff0c\u6267\u884c\u4ece\u5339\u914d\u7684case\u6807\u7b7e\u540e\u7684\u7b2c\u4e00\u4e2a\u8bed\u53e5\u5f00\u59cb\u3002case\u6807\u7b7e\u4e0d\u662f\u8bed\u53e5(\u5b83\u4eec\u662f\u6807\u7b7e)\uff0c\u6240\u4ee5\u5b83\u4eec\u4e0d\u7b97\u6570\u3002 \u5728\u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\uff0c\u80fd\u591f\u5339\u914d\u7684 \u6240\u6709 \u6807\u7b7e\u540e\u9762\u7b2c\u4e00\u4e2a\u8bed\u53e5\u662f return true \uff0c\u56e0\u6b64\uff0c\u5982\u679c\u4efb\u4f55\u6807\u7b7e\u5339\u914d\uff0c\u51fd\u6570\u5c06\u8fd4\u56de true \u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u201c\u5806\u53e0\u201d\u6807\u7b7e\uff0c\u4f7f\u6240\u6709\u8fd9\u4e9b\u6807\u7b7e\u5171\u4eab\u76f8\u540c\u7684\u8bed\u53e5\u3002\u8fd9\u4e0d\u5c5e\u4e8e\u8d2f\u7a7f\uff0c\u6240\u4ee5\u4e0d\u5fc5\u6ce8\u91ca\u6216\u6807\u8bb0\u201c [fallthrough](./fallthrough.md) \u201d\u3002 switch-case \u4f5c\u7528\u57df \u5bf9\u4e8e if \u8bed\u53e5\u6765\u8bf4\uff0c\u6761\u4ef6\u540e\u9762\u53ea\u80fd\u6709\u4e00\u6761\u8bed\u53e5\uff08\u6216\u8bed\u53e5\u5757\uff09\uff0c\u8be5\u8bed\u53e5\u88ab\u8ba4\u4e3a\u9690\u5f0f\u5730\u5c5e\u4e8e\u67d0\u4e2a\u8bed\u53e5\u5757\uff1a 1 2 if ( x > 10 ) std :: cout << x << \" is greater than 10 \\n \" ; // this line implicitly considered to be inside a block \u4f46\u662f\uff0c\u5bf9\u4e8e switch \u8bed\u53e5\u6765\u8bf4\uff0c \u67d0\u4e2a\u6807\u7b7e\u540e\u9762\u7684\u8bed\u53e5\u90fd\u5c5e\u4e8eswitch\u8bed\u53e5\u5757\uff0c\u5e76\u6ca1\u6709\u521b\u5efa\u4efb\u4f55\u7684\u9690\u5f0f\u8bed\u53e5\u5757\u3002 1 2 3 4 5 6 7 8 9 switch ( 1 ) { case 1 : // \u5e76\u4e0d\u4f1a\u521b\u5efa\u9690\u5f0f\u5730\u8bed\u53e5\u5757 foo (); // \u5c5e\u4e8eswitch\u8bed\u53e5\u5757\uff0c\u800c\u4e0d\u662fcase1\uff08\u7684\u9690\u5f0f\u8bed\u53e5\u5757\uff09 break ; // \u5c5e\u4e8eswitch\u8bed\u53e5\u5757\uff0c\u800c\u4e0d\u662fcase1\uff08\u7684\u9690\u5f0f\u8bed\u53e5\u5757\uff09 default : std :: cout << \"default case \\n \" ; break ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0ccase 1 \u540e\u9762\u7684\u4e24\u6761\u8bed\u53e5\u548c default \u540e\u9762\u7684\u8bed\u53e5\u90fd\u5c5e\u4e8e switch \u8bed\u53e5\u5757\u3002 case \u8bed\u53e5\u4e2d\u58f0\u660e\u548c\u521d\u59cb\u5316\u53d8\u91cf \u6211\u4eec\u53ef\u4ee5\u5728 switch \u4e2d\u58f0\u660e\u53d8\u91cf (\u4f46\u4e0d\u80fd\u521d\u59cb\u5316)\uff0c\u5728\u6807\u7b7e\u7684\u524d\u540e\u8fdb\u884c\u90fd\u53ef\u4ee5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 switch ( 1 ) { int a ; // okay: declaration is allowed before the case labels int b { 5 }; // illegal: initialization is not allowed before the case labels case 1 : int y ; // okay but bad practice: declaration is allowed within a case y = 4 ; // okay: assignment is allowed break ; case 2 : int z { 4 }; // \u975e\u6cd5\u64cd\u4f5c: \u5982\u679c\u540e\u7eed\u8fd8\u6709\u5206\u652f\uff0c\u5219\u4e0d\u5141\u8bb8\u521d\u59cb\u5316 y = 5 ; // okay: y was declared above, so we can use it here too break ; case 3 : break ; } \u5c3d\u7ba1\u53d8\u91cf y \u662f\u5728 case 1 \u4e2d\u58f0\u660e\u7684\uff0c\u4f46\u662f\u5728 case 2 \u4e2d\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528\u5b83\u3002\u56e0\u4e3a\u6240\u6709\u7684\u8fd9\u4e9b\u8bed\u53e5\u90fd\u4e0d\u5c5e\u4e8e\u67d0\u4e2a\u9690\u5f0f\u7684\u4f5c\u7528\u57df\u800c\u662f\u5c5e\u4e8eswitch\u8bed\u53e5\u5757\uff0c\u6240\u4ee5\u5b83\u4eec\u90fd\u5728\u540c\u4e00\u4e2a\u4f5c\u7528\u57df\u4e2d\uff0c\u524d\u9762\u5b9a\u4e49\u7684\u53d8\u91cf\u81ea\u7136\u53ef\u4ee5\u5728\u540e\u9762\u4f7f\u7528\uff0c\u5373\u4f7f\u5b9a\u4e49\u5b83\u7684\u5206\u652f\u4ece\u6ca1\u88ab\u6267\u884c\u8fc7\u4e5f\u6ca1\u6709\u95ee\u9898\u3002 \u6362\u53e5\u8bdd\u8bf4\uff0c\u5b9a\u4e49\u4e00\u4e2a\u6ca1\u6709\u521d\u59cb\u5316\u503c\u7684\u53d8\u91cf\u53ea\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\u5728\u8fd9\u4e2a\u4f5c\u7528\u57df\u5185\uff0c\u4ece\u6b64\u65f6\u8d77\uff0c\u8fd9\u4e2a\u53d8\u91cf\u6709\u5b9a\u4e49\u4e86\u3002\u8fd9\u4e00\u5207\u90fd\u53d1\u751f\u5728 \u7f16\u8bd1\u65f6 \uff0c\u5b83\u5e76\u4e0d\u8981\u8be5\u5b9a\u4e49\u5728 \u8fd0\u884c\u65f6 \u88ab\u5b9e\u9645\u6267\u884c\u3002 \u4e0d\u8fc7\uff0c\u53d8\u91cf\u7684\u521d\u59cb\u5316\u662f\u5fc5\u987b\u5728 \u8fd0\u884c\u65f6 \u6267\u884c\u624d\u6709\u6548\u7684\u3002\u53d8\u91cf\u7684\u521d\u59cb\u5316\u53ea\u80fd\u5728\u6700\u540e\u4e00\u4e2a\u5206\u652f\u8fdb\u884c\uff08\u5426\u5219\u8be5\u521d\u59cb\u5316\u53ef\u80fd\u88ab\u8df3\u8fc7\u5bfc\u81f4\u53d8\u91cf\u6ca1\u6709\u88ab\u521d\u59cb\u5316\uff09\u3002\u540c\u65f6\uff0c\u53d8\u91cf\u7684\u521d\u59cb\u5316\u4e5f\u4e0d\u80fd\u5728\u7b2c\u4e00\u4e2a\u5206\u652f\u4e4b\u524d\u8fdb\u884c\uff0c\u56e0\u4e3a\u8fd9\u90e8\u5206\u4ee3\u7801\u6c38\u8fdc\u4e0d\u4f1a\u88ab\u6267\u884c\u3002 \u5982\u679ccase\u9700\u8981\u5b9a\u4e49\u548c/\u6216\u521d\u59cb\u5316\u4e00\u4e2a\u65b0\u53d8\u91cf\uff0c\u6700\u4f73\u505a\u6cd5\u662f\u5728case\u8bed\u53e5\u4e0b\u9762\u7684\u663e\u5f0f\u5757\u4e2d\u5b8c\u6210: 1 2 3 4 5 6 7 8 9 10 11 12 switch ( 1 ) { case 1 : { // note addition of explicit block here int x { 4 }; // okay, variables can be initialized inside a block inside a case std :: cout << x ; break ; } default : std :: cout << \"default case \\n \" ; break ; } COPY \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u9700\u8981\u5b9a\u4e49\u548c/\u6216\u521d\u59cb\u5316\u4e00\u4e2a\u65b0\u53d8\u91cf\uff0c\u6700\u4f73\u505a\u6cd5\u662f\u5728case\u8bed\u53e5\u4e0b\u9762\u7684\u663e\u5f0f\u5757\u4e2d\u5b8c\u6210","title":"7.5 - switch fallthrough\u5c5e\u6027\u548c\u4f5c\u7528\u57df"},{"location":"7-5-Switch-fallthrough-and-scoping/#_1","text":"\u5f53\u4e00\u4e2aswitch\u8868\u8fbe\u5f0f\u5339\u914d\u4e00\u4e2acase\u6807\u7b7e\u6216\u53ef\u9009\u7684\u9ed8\u8ba4\u6807\u7b7e\u65f6\uff0c\u4ece\u5339\u914d\u6807\u7b7e\u540e\u9762\u7684\u7b2c\u4e00\u4e2a\u8bed\u53e5\u5f00\u59cb\u6267\u884c\u3002\u7136\u540e\u7ee7\u7eed\u6267\u884c\uff0c\u76f4\u5230\u4ee5\u4e0b\u7ec8\u6b62\u6761\u4ef6\u4e4b\u4e00\u53d1\u751f\uff1a \u5230\u8fbeswitch\u672b\u5c3e\uff1b \u9047\u5230\u5176\u4ed6\u63a7\u5236\u6d41\u8bed\u53e5\uff08\u4e00\u822c\u6765\u8bf4\u662f break \u6216\u8005 return \uff09\u5bfc\u81f4switch\u9000\u51fa\uff1b \u5176\u4ed6\u4e2d\u65ad\u5bfc\u81f4\u7a0b\u5e8f\u7684\u6b63\u5e38\u6d41\u7a0b\u88ab\u6253\u65ad(\u4f8b\u5982\uff0c\u64cd\u4f5c\u7cfb\u7edf\u505c\u6b62\u7a0b\u5e8f\u3001\u5b87\u5b99\u5927\u7206\u70b8\u7b49\u7b49)\u3002 \u6ce8\u610f\uff0c\u53e6\u4e00\u4e2acase\u6807\u7b7e\u7684\u51fa\u73b0\u4e0d\u662f\u8fd9\u4e9b\u7ec8\u6b62\u6761\u4ef6\u4e4b\u4e00\u2014\u2014\u56e0\u6b64\uff0c\u5982\u679c\u6ca1\u6709 break \u6216 return \uff0c\u6267\u884c\u5c06\u7ee7\u7eed\u6267\u884c\u540e\u7eed\u7684case\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int main () { switch ( 2 ) { case 1 : // Does not match std :: cout << 1 << '\\n' ; // \u8df3\u8fc7 case 2 : // Match! std :: cout << 2 << '\\n' ; // \u4ece\u8fd9\u91cc\u5f00\u59cb\u6267\u884c case 3 : std :: cout << 3 << '\\n' ; // \u4e5f\u4f1a\u88ab\u6267\u884c case 4 : std :: cout << 4 << '\\n' ; // \u4e5f\u4f1a\u88ab\u6267\u884c default : std :: cout << 5 << '\\n' ; // \u4e5f\u4f1a\u88ab\u6267\u884c } return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 2 3 4 5 \u8fd9\u53ef\u80fd\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684!\u5f53\u6267\u884c\u4ece\u6807\u7b7e\u4e0b\u9762\u7684\u8bed\u53e5\u6d41\u5230\u540e\u7eed\u6807\u7b7e\u4e0b\u9762\u7684\u8bed\u53e5\u65f6\uff0c\u8fd9\u79f0\u4e3a\u8d2f\u7a7f\u3002 \u6ce8\u610f \u4e00\u65e6case\u6216default\u6807\u7b7e\u4e0b\u7684\u8bed\u53e5\u5f00\u59cb\u6267\u884c\uff0c\u5b83\u4eec\u9ed8\u8ba4\u4f1a\u8d2f\u7a7f\u5230\u540e\u7eed\u7684case\u4e2d\u3002 break \u6216 return \u53ef\u4ee5\u9632\u6b62\u8fd9\u79cd\u60c5\u51b5\u3002 \u7531\u4e8e\u4eba\u4eec\u5f88\u5c11\u4f1a\u4e3b\u52a8\u4f7f\u7528\u8d2f\u7a7f\u7279\u6027\uff0c\u56e0\u6b64\u8bb8\u591a\u7f16\u8bd1\u5668\u548c\u4ee3\u7801\u5206\u6790\u5de5\u5177\u4f1a\u5c06\u8d2f\u7a7f\u6807\u8bb0\u4e3a\u8b66\u544a\u3002","title":"\u8d2f\u7a7f"},{"location":"7-5-Switch-fallthrough-and-scoping/#fallthroughfallthroughmd","text":"\u901a\u8fc7\u6ce8\u91ca\u544a\u8bc9\u5176\u4ed6\u5f00\u53d1\u4eba\u5458\u6545\u610f\u5b9e\u73b0\u7684\u8d2f\u7a7f\u662f\u4e00\u79cd\u5e38\u89c1\u60ef\u4f8b\u3002\u867d\u7136\u5176\u4ed6\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u7406\u89e3\uff0c\u4f46\u7f16\u8bd1\u5668\u548c\u4ee3\u7801\u5206\u6790\u5de5\u5177\u4e0d\u77e5\u9053\u5982\u4f55\u89e3\u91ca\u6ce8\u91ca\uff0c\u56e0\u6b64\u5b83\u65e0\u6cd5\u6d88\u9664\u8b66\u544a\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cC++17\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u5c5e\u6027 [fallthrough](./fallthrough.md) \u3002 \u5c5e\u6027\u662f\u73b0\u4ee3C++\u7684\u4e00\u4e2a\u7279\u6027\uff0c\u5b83\u5141\u8bb8\u7a0b\u5e8f\u5458\u5411\u7f16\u8bd1\u5668\u63d0\u4f9b\u4e00\u4e9b\u5173\u4e8e\u4ee3\u7801\u7684\u9644\u52a0\u6570\u636e\u3002\u8981\u6307\u5b9a\u5c5e\u6027\uff0c\u5c5e\u6027\u540d\u8981\u653e\u5728\u53cc\u5927\u62ec\u53f7\u4e4b\u95f4\u3002\u5c5e\u6027\u4e0d\u662f\u8bed\u53e5\u2014\u2014\u76f8\u53cd\uff0c\u5b83\u4eec\u51e0\u4e4e\u53ef\u4ee5\u5728\u4e0e\u4e0a\u4e0b\u6587\u76f8\u5173\u7684\u4efb\u4f55\u5730\u65b9\u4f7f\u7528\u3002 [fallthrough](./fallthrough.md) \u4f1a\u4fee\u6539\u4e00\u4e2a\u7a7a\u8bed\u53e5\uff0c\u8868\u660e\u6709\u610f\u800c\u4e3a\u4e4b\u7684\u8d2f\u7a7f\u64cd\u4f5c\uff08\u907f\u514d\u89e6\u53d1\u544a\u8b66\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { switch ( 2 ) { case 1 : std :: cout << 1 << '\\n' ; break ; case 2 : std :: cout << 2 << '\\n' ; // \u4ece\u8fd9\u91cc\u5f00\u59cb\u6267\u884c [ fallthrough ](. / fallthrough . md ); // \u6709\u610f\u5730\u8d2f\u7a7f\u64cd\u4f5c\u2014\u2014\u6ce8\u610f\u5206\u53f7\u521b\u5efa\u7684\u7a7a\u8bed\u53e5 case 3 : std :: cout << 3 << '\\n' ; // \u540c\u6837\u4f1a\u6267\u884c break ; } return 0 ; } \u8f93\u51fa\uff1a 1 2 2 3 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528 [fallthrough](./fallthrough.md) \u5c5e\u6027(\u548c\u7a7a\u8bed\u53e5)\u8868\u660e\u6709\u610f\u800c\u4e3a\u4e4b\u7684\u8d2f\u7a7f\u64cd\u4f5c\u3002","title":"\u8d2f\u7a7f [fallthrough](./fallthrough.md) \u5c5e\u6027"},{"location":"7-5-Switch-fallthrough-and-scoping/#_2","text":"\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u6765\u8fde\u63a5\u591a\u4e2a\u6d4b\u8bd5\u6761\u4ef6\uff1a 1 2 3 4 5 bool isVowel ( char c ) { return ( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' ); } \u5728switch\u8bed\u53e5\u4e2d\u4e5f\u6709\u7c7b\u4f3c\u7684\u573a\u666f\uff1ac \u88ab\u591a\u6b21\u6c42\u503c\u5e76\u6d4b\u8bd5\uff0c\u6b64\u65f6\u8bfb\u4ee3\u7801\u7684\u4eba\u5fc5\u987b\u786e\u4fdd\u6bcf\u6b21\u88ab\u6c42\u503c\u548c\u6bd4\u8f83\u7684\u662fc\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u987a\u5e8f\u6392\u5217\u7684\u591a\u4e2a\u5206\u652f\u6807\u7b7e\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 bool isVowel ( char c ) { switch ( c ) { case 'a' : // if c is 'a' case 'e' : // or if c is 'e' case 'i' : // or if c is 'i' case 'o' : // or if c is 'o' case 'u' : // or if c is 'u' case 'A' : // or if c is 'A' case 'E' : // or if c is 'E' case 'I' : // or if c is 'I' case 'O' : // or if c is 'O' case 'U' : // or if c is 'U' return true ; default : return false ; } } \u8bb0\u4f4f\uff0c\u6267\u884c\u4ece\u5339\u914d\u7684case\u6807\u7b7e\u540e\u7684\u7b2c\u4e00\u4e2a\u8bed\u53e5\u5f00\u59cb\u3002case\u6807\u7b7e\u4e0d\u662f\u8bed\u53e5(\u5b83\u4eec\u662f\u6807\u7b7e)\uff0c\u6240\u4ee5\u5b83\u4eec\u4e0d\u7b97\u6570\u3002 \u5728\u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\uff0c\u80fd\u591f\u5339\u914d\u7684 \u6240\u6709 \u6807\u7b7e\u540e\u9762\u7b2c\u4e00\u4e2a\u8bed\u53e5\u662f return true \uff0c\u56e0\u6b64\uff0c\u5982\u679c\u4efb\u4f55\u6807\u7b7e\u5339\u914d\uff0c\u51fd\u6570\u5c06\u8fd4\u56de true \u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u201c\u5806\u53e0\u201d\u6807\u7b7e\uff0c\u4f7f\u6240\u6709\u8fd9\u4e9b\u6807\u7b7e\u5171\u4eab\u76f8\u540c\u7684\u8bed\u53e5\u3002\u8fd9\u4e0d\u5c5e\u4e8e\u8d2f\u7a7f\uff0c\u6240\u4ee5\u4e0d\u5fc5\u6ce8\u91ca\u6216\u6807\u8bb0\u201c [fallthrough](./fallthrough.md) \u201d\u3002","title":"\u987a\u5e8f\u5206\u652f\u6807\u7b7e"},{"location":"7-5-Switch-fallthrough-and-scoping/#switch-case","text":"\u5bf9\u4e8e if \u8bed\u53e5\u6765\u8bf4\uff0c\u6761\u4ef6\u540e\u9762\u53ea\u80fd\u6709\u4e00\u6761\u8bed\u53e5\uff08\u6216\u8bed\u53e5\u5757\uff09\uff0c\u8be5\u8bed\u53e5\u88ab\u8ba4\u4e3a\u9690\u5f0f\u5730\u5c5e\u4e8e\u67d0\u4e2a\u8bed\u53e5\u5757\uff1a 1 2 if ( x > 10 ) std :: cout << x << \" is greater than 10 \\n \" ; // this line implicitly considered to be inside a block \u4f46\u662f\uff0c\u5bf9\u4e8e switch \u8bed\u53e5\u6765\u8bf4\uff0c \u67d0\u4e2a\u6807\u7b7e\u540e\u9762\u7684\u8bed\u53e5\u90fd\u5c5e\u4e8eswitch\u8bed\u53e5\u5757\uff0c\u5e76\u6ca1\u6709\u521b\u5efa\u4efb\u4f55\u7684\u9690\u5f0f\u8bed\u53e5\u5757\u3002 1 2 3 4 5 6 7 8 9 switch ( 1 ) { case 1 : // \u5e76\u4e0d\u4f1a\u521b\u5efa\u9690\u5f0f\u5730\u8bed\u53e5\u5757 foo (); // \u5c5e\u4e8eswitch\u8bed\u53e5\u5757\uff0c\u800c\u4e0d\u662fcase1\uff08\u7684\u9690\u5f0f\u8bed\u53e5\u5757\uff09 break ; // \u5c5e\u4e8eswitch\u8bed\u53e5\u5757\uff0c\u800c\u4e0d\u662fcase1\uff08\u7684\u9690\u5f0f\u8bed\u53e5\u5757\uff09 default : std :: cout << \"default case \\n \" ; break ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0ccase 1 \u540e\u9762\u7684\u4e24\u6761\u8bed\u53e5\u548c default \u540e\u9762\u7684\u8bed\u53e5\u90fd\u5c5e\u4e8e switch \u8bed\u53e5\u5757\u3002","title":"switch-case \u4f5c\u7528\u57df"},{"location":"7-5-Switch-fallthrough-and-scoping/#case","text":"\u6211\u4eec\u53ef\u4ee5\u5728 switch \u4e2d\u58f0\u660e\u53d8\u91cf (\u4f46\u4e0d\u80fd\u521d\u59cb\u5316)\uff0c\u5728\u6807\u7b7e\u7684\u524d\u540e\u8fdb\u884c\u90fd\u53ef\u4ee5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 switch ( 1 ) { int a ; // okay: declaration is allowed before the case labels int b { 5 }; // illegal: initialization is not allowed before the case labels case 1 : int y ; // okay but bad practice: declaration is allowed within a case y = 4 ; // okay: assignment is allowed break ; case 2 : int z { 4 }; // \u975e\u6cd5\u64cd\u4f5c: \u5982\u679c\u540e\u7eed\u8fd8\u6709\u5206\u652f\uff0c\u5219\u4e0d\u5141\u8bb8\u521d\u59cb\u5316 y = 5 ; // okay: y was declared above, so we can use it here too break ; case 3 : break ; } \u5c3d\u7ba1\u53d8\u91cf y \u662f\u5728 case 1 \u4e2d\u58f0\u660e\u7684\uff0c\u4f46\u662f\u5728 case 2 \u4e2d\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528\u5b83\u3002\u56e0\u4e3a\u6240\u6709\u7684\u8fd9\u4e9b\u8bed\u53e5\u90fd\u4e0d\u5c5e\u4e8e\u67d0\u4e2a\u9690\u5f0f\u7684\u4f5c\u7528\u57df\u800c\u662f\u5c5e\u4e8eswitch\u8bed\u53e5\u5757\uff0c\u6240\u4ee5\u5b83\u4eec\u90fd\u5728\u540c\u4e00\u4e2a\u4f5c\u7528\u57df\u4e2d\uff0c\u524d\u9762\u5b9a\u4e49\u7684\u53d8\u91cf\u81ea\u7136\u53ef\u4ee5\u5728\u540e\u9762\u4f7f\u7528\uff0c\u5373\u4f7f\u5b9a\u4e49\u5b83\u7684\u5206\u652f\u4ece\u6ca1\u88ab\u6267\u884c\u8fc7\u4e5f\u6ca1\u6709\u95ee\u9898\u3002 \u6362\u53e5\u8bdd\u8bf4\uff0c\u5b9a\u4e49\u4e00\u4e2a\u6ca1\u6709\u521d\u59cb\u5316\u503c\u7684\u53d8\u91cf\u53ea\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\u5728\u8fd9\u4e2a\u4f5c\u7528\u57df\u5185\uff0c\u4ece\u6b64\u65f6\u8d77\uff0c\u8fd9\u4e2a\u53d8\u91cf\u6709\u5b9a\u4e49\u4e86\u3002\u8fd9\u4e00\u5207\u90fd\u53d1\u751f\u5728 \u7f16\u8bd1\u65f6 \uff0c\u5b83\u5e76\u4e0d\u8981\u8be5\u5b9a\u4e49\u5728 \u8fd0\u884c\u65f6 \u88ab\u5b9e\u9645\u6267\u884c\u3002 \u4e0d\u8fc7\uff0c\u53d8\u91cf\u7684\u521d\u59cb\u5316\u662f\u5fc5\u987b\u5728 \u8fd0\u884c\u65f6 \u6267\u884c\u624d\u6709\u6548\u7684\u3002\u53d8\u91cf\u7684\u521d\u59cb\u5316\u53ea\u80fd\u5728\u6700\u540e\u4e00\u4e2a\u5206\u652f\u8fdb\u884c\uff08\u5426\u5219\u8be5\u521d\u59cb\u5316\u53ef\u80fd\u88ab\u8df3\u8fc7\u5bfc\u81f4\u53d8\u91cf\u6ca1\u6709\u88ab\u521d\u59cb\u5316\uff09\u3002\u540c\u65f6\uff0c\u53d8\u91cf\u7684\u521d\u59cb\u5316\u4e5f\u4e0d\u80fd\u5728\u7b2c\u4e00\u4e2a\u5206\u652f\u4e4b\u524d\u8fdb\u884c\uff0c\u56e0\u4e3a\u8fd9\u90e8\u5206\u4ee3\u7801\u6c38\u8fdc\u4e0d\u4f1a\u88ab\u6267\u884c\u3002 \u5982\u679ccase\u9700\u8981\u5b9a\u4e49\u548c/\u6216\u521d\u59cb\u5316\u4e00\u4e2a\u65b0\u53d8\u91cf\uff0c\u6700\u4f73\u505a\u6cd5\u662f\u5728case\u8bed\u53e5\u4e0b\u9762\u7684\u663e\u5f0f\u5757\u4e2d\u5b8c\u6210: 1 2 3 4 5 6 7 8 9 10 11 12 switch ( 1 ) { case 1 : { // note addition of explicit block here int x { 4 }; // okay, variables can be initialized inside a block inside a case std :: cout << x ; break ; } default : std :: cout << \"default case \\n \" ; break ; } COPY \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u9700\u8981\u5b9a\u4e49\u548c/\u6216\u521d\u59cb\u5316\u4e00\u4e2a\u65b0\u53d8\u91cf\uff0c\u6700\u4f73\u505a\u6cd5\u662f\u5728case\u8bed\u53e5\u4e0b\u9762\u7684\u663e\u5f0f\u5757\u4e2d\u5b8c\u6210","title":"case \u8bed\u53e5\u4e2d\u58f0\u660e\u548c\u521d\u59cb\u5316\u53d8\u91cf"},{"location":"7-6-Goto-statements/","tags":["goto"],"text":"Key Takeaway The next kind of control flow statement we\u2019ll cover is the unconditional jump. An unconditional jump causes execution to jump to another spot in the code. The term \u201cunconditional\u201d means the jump always happens (unlike an if statement or switch statement , where the jump only happens conditionally based on the result of an expression). In C++, unconditional jumps are implemented via a goto statement, and the spot to jump to is identified through use of a statement label. The following is an example of a goto statement and statement label : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include // for sqrt() function int main () { double x {}; tryAgain : // this is a statement label std :: cout << \"Enter a non-negative number: \" ; std :: cin >> x ; if ( x < 0.0 ) goto tryAgain ; // this is the goto statement std :: cout << \"The square root of \" << x << \" is \" << std :: sqrt ( x ) << '\\n' ; return 0 ; } COPY In this program, the user is asked to enter a non-negative number. However, if a negative number is entered, the program utilizes a goto statement to jump back to the tryAgain label. The user is then asked again to enter a new number. In this way, we can continually ask the user for input until he or she enters something valid. Here\u2019s a sample run of this program: 1 2 3 Enter a non-negative number: -4 Enter a non-negative number: 4 The square root of 4 is 2 \u8bed\u53e5\u6807\u7b7e\u5177\u6709\u51fd\u6570\u4f5c\u7528\u57df In the chapter on object scope ( chapter 6 ), we covered two kinds of scope: local (block) scope, and file (global) scope. Statement labels utilize a third kind of scope: function scope, which means the label is visible throughout the function even before its point of declaration. The goto statement and its corresponding statement label must appear in the same function. While the above example shows a goto statement that jumps backwards (to a preceding point in the function), goto statements can also jump forward: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void printCats ( bool skip ) { if ( skip ) goto end ; // jump forward; statement label 'end' is visible here due to it having function scope std :: cout << \"cats\" ; end : ; // statement labels must be associated with a statement } int main () { printCats ( true ); // jumps over the print statement and doesn't print anything printCats ( false ); // prints \"cats\" return 0 ; } COPY This prints: 1 cats Beyond the jumping forward, there are a couple of interesting things worth mentioning in the program above. First, note that statement labels must be associated with a statement (hence their name: they label a statement). Because the end of the function had no statement, we had to use a null statement so we had a statement to label. Second, we were able to jump to the statement labeled by end even though we hadn\u2019t declared end yet due to statement labels having function scope. No forward declaration of statement labels is necessary. Third, it\u2019s worth explicitly mentioning that the above program is poor form -- it would have been better to use an if statement to skip the print statement than a goto statement to jump over it. There are two primary limitations to jumping: You can jump only within the bounds of a single function (you can\u2019t jump out of one function and into another), and if you jump forward, you can\u2019t jump forward over the initialization of any variable that is still in scope at the location being jumped to. For example: 1 2 3 4 5 6 7 8 int main () { goto skip ; // error: this jump is illegal because... int x { 5 }; // this initialized variable is still in scope at statement label 'skip' skip : x += 3 ; // what would this even evaluate to if x wasn't initialized? return 0 ; } COPY Note that you can jump backwards over a variable initialization, and the variable will be re-initialized when the initialization is executed. \u907f\u514d\u4f7f\u7528 goto \u8bed\u53e5 Use of goto is shunned in C++ (and other modern high level languages as well). Edsger W. Dijkstra , a noted computer scientist, laid out the case for avoiding goto in a famous but difficult to read paper called Go To Statement Considered Harmful . The primary problem with goto is that it allows a programmer to jump around the code arbitrarily. This creates what is not-so-affectionately known as spaghetti code . Spaghetti code is code that has a path of execution that resembles a bowl of spaghetti (all tangled and twisted), making it extremely difficult to follow the logic of such code. As Dijkstra says somewhat humorously, \u201cthe quality of programmers is a decreasing function of the density of go to statements in the programs they produce\u201d. Almost any code written using a goto statement can be more clearly written using other constructs in C++, such as if statements and loops. One notable exception is when you need to exit a nested loop but not the entire function -- in such a case, a goto to just beyond the loops is probably the cleanest solution. \u6700\u4f73\u5b9e\u8df5 Avoid goto statements (unless the alternatives are significantly worse for code readability).","title":"7.6 - goto \u8bed\u53e5"},{"location":"7-6-Goto-statements/#_1","text":"In the chapter on object scope ( chapter 6 ), we covered two kinds of scope: local (block) scope, and file (global) scope. Statement labels utilize a third kind of scope: function scope, which means the label is visible throughout the function even before its point of declaration. The goto statement and its corresponding statement label must appear in the same function. While the above example shows a goto statement that jumps backwards (to a preceding point in the function), goto statements can also jump forward: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void printCats ( bool skip ) { if ( skip ) goto end ; // jump forward; statement label 'end' is visible here due to it having function scope std :: cout << \"cats\" ; end : ; // statement labels must be associated with a statement } int main () { printCats ( true ); // jumps over the print statement and doesn't print anything printCats ( false ); // prints \"cats\" return 0 ; } COPY This prints: 1 cats Beyond the jumping forward, there are a couple of interesting things worth mentioning in the program above. First, note that statement labels must be associated with a statement (hence their name: they label a statement). Because the end of the function had no statement, we had to use a null statement so we had a statement to label. Second, we were able to jump to the statement labeled by end even though we hadn\u2019t declared end yet due to statement labels having function scope. No forward declaration of statement labels is necessary. Third, it\u2019s worth explicitly mentioning that the above program is poor form -- it would have been better to use an if statement to skip the print statement than a goto statement to jump over it. There are two primary limitations to jumping: You can jump only within the bounds of a single function (you can\u2019t jump out of one function and into another), and if you jump forward, you can\u2019t jump forward over the initialization of any variable that is still in scope at the location being jumped to. For example: 1 2 3 4 5 6 7 8 int main () { goto skip ; // error: this jump is illegal because... int x { 5 }; // this initialized variable is still in scope at statement label 'skip' skip : x += 3 ; // what would this even evaluate to if x wasn't initialized? return 0 ; } COPY Note that you can jump backwards over a variable initialization, and the variable will be re-initialized when the initialization is executed.","title":"\u8bed\u53e5\u6807\u7b7e\u5177\u6709\u51fd\u6570\u4f5c\u7528\u57df"},{"location":"7-6-Goto-statements/#goto","text":"Use of goto is shunned in C++ (and other modern high level languages as well). Edsger W. Dijkstra , a noted computer scientist, laid out the case for avoiding goto in a famous but difficult to read paper called Go To Statement Considered Harmful . The primary problem with goto is that it allows a programmer to jump around the code arbitrarily. This creates what is not-so-affectionately known as spaghetti code . Spaghetti code is code that has a path of execution that resembles a bowl of spaghetti (all tangled and twisted), making it extremely difficult to follow the logic of such code. As Dijkstra says somewhat humorously, \u201cthe quality of programmers is a decreasing function of the density of go to statements in the programs they produce\u201d. Almost any code written using a goto statement can be more clearly written using other constructs in C++, such as if statements and loops. One notable exception is when you need to exit a nested loop but not the entire function -- in such a case, a goto to just beyond the loops is probably the cleanest solution. \u6700\u4f73\u5b9e\u8df5 Avoid goto statements (unless the alternatives are significantly worse for code readability).","title":"\u907f\u514d\u4f7f\u7528 goto \u8bed\u53e5"},{"location":"7-7-Intro-to-loops-and-while-statements/","tags":["while","for"],"text":"Key Takeaway \u5faa\u73af \u73b0\u5728\u771f\u6b63\u7684\u4e50\u8da3\u5f00\u59cb\u4e86\u2014\u2014\u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5faa\u73af\u3002\u5faa\u73af\u662f\u63a7\u5236\u6d41\u7ed3\u6784\uff0c\u5b83\u5141\u8bb8\u4e00\u6bb5\u4ee3\u7801\u88ab\u91cd\u590d\u6267\u884c\uff0c\u76f4\u5230\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u3002\u5faa\u73af\u4e3a\u7f16\u7a0b\u5de5\u5177\u5305\u589e\u52a0\u4e86\u5f88\u5927\u7684\u7075\u6d3b\u6027\uff0c\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u5f88\u591a\u4e0d\u4f7f\u7528\u5faa\u73af\u96be\u4ee5\u5b9e\u73b0\u7684\u529f\u80fd\u3002 \u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u60f3\u8981\u6253\u53701\u523010\uff0c\u5982\u679c\u6ca1\u6709\u5faa\u73af\uff0c\u4f60\u53ef\u80fd\u4f1a\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << \"1 2 3 4 5 6 7 8 9 10\" ; std :: cout << \" done! \\n \" ; return 0 ; } \u867d\u7136\u8fd9\u4e48\u505a\u662f\u53ef\u884c\u7684\uff0c\u4f46\u5f53\u6211\u4eec\u9700\u8981\u6253\u5370\u66f4\u591a\u7684\u6570\u5b57\u65f6\uff0c\u5b83\u4f1a\u53d8\u5f97\u8d8a\u6765\u8d8a\u4e0d\u53ef\u884c\uff1a\u5982\u679c\u9700\u8981\u6253\u53701\u52301000\u4e4b\u95f4\u7684\u6240\u6709\u6570\u5b57\u5462\uff1f\u90a3\u6253\u5b57\u7684\u5de5\u4f5c\u91cf\u5c31\u592a\u5927\u4e86\uff01\u800c\u4e14\u8be5\u7a0b\u5e8f\u8981\u6253\u5370\u7684\u5185\u5bb9\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u5c31\u77e5\u9053\u8981\u6253\u5370\u591a\u5c11\u6570\u5b57\u3002 \u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u7a0d\u5fae\u6539\u53d8\u4e00\u4e0b\u53c2\u6570\u3002\u5982\u679c\u9700\u8981\u8ba9\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff0c\u7136\u540e\u6253\u53701\u548c\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\u4e4b\u95f4\u7684\u6240\u6709\u6570\u5b57\uff0c\u90a3\u4f1a\u600e\u4e48\u6837\u5462?\u7528\u6237\u5c06\u8f93\u5165\u7684\u6570\u5b57\u5728\u7f16\u8bd1\u65f6\u662f\u4e0d\u53ef\u77e5\u7684\uff0c\u8981\u600e\u4e48\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5462? while \u8bed\u53e5 while \u8bed\u53e5\uff08\u4e5f\u88ab\u53eb\u505a while \u5faa\u73af\uff09\u662fC++\u4e2d\u6700\u7b80\u5355\u5faa\u73af\uff0c\u5b83\u7684\u5b9a\u4e49\u548c if \u8bed\u53e5\u7279\u522b\u5730\u50cf\uff1a 1 2 while ( condition ) statement ; while \u8bed\u53e5\u7684\u58f0\u660e\u9700\u8981\u4f7f\u7528 while \u5173\u952e\u5b57\u3002\u5f53 while \u8bed\u53e5\u6267\u884c\u65f6\uff0c condition \u4f1a\u88ab\u6c42\u503c\u3002\u5982\u679c\u6c42\u503c\u7ed3\u679c\u662f\u771f\uff0c\u5219\u76f8\u5173\u7684\u8bed\u53e5\u4f1a\u88ab\u6267\u884c\u3002 \u4f46\u662f\uff0c\u548cif\u8bed\u53e5\u4e0d\u540c\u7684\u662f\uff0c while \u8bed\u53e5\u4e2d\u7684 statement \u6267\u884c\u5b8c\u6210\u540e\uff0c\u4f1a\u8fd4\u56de\u5230 while \u8bed\u53e5\u7684\u5f00\u59cb\uff0c\u7136\u540e\u7ee7\u7eed\u6267\u884c\u3001\u8fd9\u610f\u5473\u7740\u53ea\u8981\u6761\u4ef6\u6c42\u503c\u4e3a\u771f\uff0c while \u5faa\u73af\u5c31\u4f1a\u4e0d\u505c\u5730\u6267\u884c\u3002 \u8fd8\u662f\u6253\u53701\u523010\u7684\u4f8b\u5b50\uff0c\u8fd9\u6b21\u4f7f\u7528 while \u5faa\u73af\u5b8c\u6210\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int count { 1 }; while ( count <= 10 ) { std :: cout << count << ' ' ; ++ count ; } std :: cout << \"done! \\n \" ; return 0 ; } \u8f93\u51fa\uff1a 1 1 2 3 4 5 6 7 8 9 10 done! \u8ba9\u6211\u4eec\u4ed4\u7ec6\u770b\u770b\u8fd9\u4e2a\u7a0b\u5e8f\u6b63\u5728\u505a\u4ec0\u4e48\u3002\u9996\u5148\uff0c count \u88ab\u521d\u59cb\u5316\u4e3a1\uff0c\u8fd9\u662f\u6211\u4eec\u8981\u6253\u5370\u7684\u7b2c\u4e00\u4e2a\u6570\u5b57\u3002\u6761\u4ef6 count <= 10 \u4e3a true \uff0c\u56e0\u6b64\u6267\u884c\u8bed\u53e5\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u7684\u8bed\u53e5\u662f\u4e00\u4e2a\u8bed\u53e5\u5757\uff0c\u56e0\u6b64\u5757\u4e2d\u7684\u6240\u6709\u8bed\u53e5\u90fd\u5c06\u6267\u884c\u3002\u5757\u4e2d\u7684\u7b2c\u4e00\u4e2a\u8bed\u53e5\u8f93\u51fa 1 \u548c\u4e00\u4e2a\u7a7a\u683c\uff0c\u7b2c\u4e8c\u4e2a\u8bed\u53e5\u5c06 count \u9012\u589e\u52302\u3002\u63a7\u5236\u73b0\u5728\u8fd4\u56de\u5230 while \u7684\u9876\u90e8\uff0c\u5e76\u518d\u6b21\u8ba1\u7b97\u6761\u4ef6\u3002 2 <= 10 \u7684\u8ba1\u7b97\u7ed3\u679c\u4e3a\u4ecd\u7136 true \uff0c\u56e0\u6b64\u4ee3\u7801\u5757\u5c06\u518d\u6b21\u6267\u884c\u3002\u5faa\u73af\u5c06\u91cd\u590d\u6267\u884c\uff0c\u76f4\u5230 count \u4e3a11 \uff0c\u6b64\u65f6 11 <= 10 \u5c06\u88ab\u8ba1\u7b97\u4e3a false \uff0c\u4e0e\u5faa\u73af\u76f8\u5173\u7684\u8bed\u53e5\u5c06\u88ab\u8df3\u8fc7\u3002\u81f3\u6b64\uff0c\u5faa\u73af\u5b8c\u6210\u4e86\u3002 \u867d\u7136\u4e0a\u9762\u7684\u4ee3\u7801\u770b\u4e0a\u53bb\u4f3c\u4e4e\u6bd4\u624b\u52a8\u8f93\u51651\u523010\u8fd8\u591a\u4e9b\uff0c\u4f46\u662f\u4f60\u53ef\u4ee5\u60f3\u8c61\uff0c\u5982\u679c\u9700\u8981\u6253\u53701\u52301000\uff0c\u6211\u4eec\u53ea\u9700\u8981\u7b80\u5355\u5730\u5c06 count <= 10 \u6539\u4e3a count <= 1000 \u5373\u53ef\u3002 \u521d\u59cb\u6761\u4ef6\u4e3a false \u7684 while \u8bed\u53e5 \u5982\u679c\u521d\u59cb\u5316\u6761\u4ef6\u4e3a false \uff0c\u90a3\u4e48\u8bed\u53e5\u5c31\u5b8c\u5168\u4e0d\u4f1a\u88ab\u6267\u884c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int count { 15 }; while ( count <= 10 ) { std :: cout << count << ' ' ; ++ count ; } std :: cout << \"done! \\n \" ; return 0 ; } \u7531\u4e8e 15 <= 10 \u6c42\u503c\u4e3a false \uff0c\u6240\u4ee5 while \u4e2d\u7684\u6240\u6709\u8bed\u53e5\u90fd\u4f1a\u88ab\u8df3\u8fc7\uff0c\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c\u5e76\u6253\u5370 done \u3002 \u65e0\u9650\u5faa\u73af \u5982\u679c\u8868\u8fbe\u5f0f\u59cb\u7ec8\u6c42\u503c\u4e3a true \uff0c\u5219 while \u5faa\u73af\u5c31\u4f1a\u4e00\u76f4\u8fd0\u884c\u2014\u2014\u79f0\u4e3a\u65e0\u9650\u5faa\u73af\u6216\u6b7b\u5faa\u73af\u3002\u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int count { 1 }; while ( count <= 10 ) // \u8be5\u6761\u4ef6\u7528\u4e8e\u4e0d\u4f1a\u53d8\u4e3afalse { std :: cout << count << ' ' ; // \u53cd\u590d\u6267\u884c } std :: cout << '\\n' ; // \u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c return 0 ; // \u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c } \u56e0\u4e3a count \u59cb\u7ec8\u6ca1\u6709\u88ab\u9012\u589e\uff0c\u6240\u4ee5 count <= 10 \u603b\u662f\u771f\u3002\u56e0\u6b64\u5faa\u73af\u6c38\u8fdc\u4e0d\u4f1a\u505c\u6b62\uff0c\u7a0b\u5e8f\u4e0d\u505c\u5730\u6253\u5370 \u201c1 1 1 1 1\u2033\u3002 \u6709\u610f\u800c\u4e3a\u4e4b\u7684\u65e0\u9650\u5faa\u73af \u6211\u4eec\u53ef\u4ee5\u6545\u610f\u521b\u5efa\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff1a 1 2 3 4 while ( true ) { // this loop will execute forever } \u9000\u51fa\u65e0\u9650\u5faa\u73af\u7684\u552f\u4e00\u65b9\u6cd5\u662f\u4f7f\u7528 return y \u8bed\u53e5\u3001 break \u8bed\u53e5\u3001 exit \u8bed\u53e5\u3001 goto \u8bed\u53e5\u3001\u629b\u51fa\u5f02\u5e38\u6216\u88ab\u5176\u4ed6\u7a0b\u5e8f\u7ec8\u6b62\u3002 \u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { while ( true ) // infinite loop { std :: cout << \"Loop again (y/n)? \" ; char c {}; std :: cin >> c ; if ( c == 'n' ) return 0 ; } return 0 ; } \u8be5\u5faa\u73af\u4f1a\u4e0d\u65ad\u6267\u884c\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165 n \uff0c\u6b64\u65f6if\u8bed\u53e5\u7684\u6761\u4ef6\u6c42\u503c\u4e3a\u771f\uff0c return 0 \u88ab\u6267\u884c\uff0cmain\u51fd\u6570\u9000\u51fa\uff0c\u7a0b\u5e8f\u7ec8\u6b62\u3002 \u5728\u6301\u7eed\u8fd0\u884c\u5e76\u4e3aweb\u8bf7\u6c42\u63d0\u4f9b\u670d\u52a1\u7684web\u670d\u52a1\u5668\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u7ecf\u5e38\u53ef\u4ee5\u770b\u5230\u8fd9\u79cd\u5faa\u73af\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u4e8e\u6709\u610f\u800c\u4e3a\u4e4b\u7684\u6b7b\u5faa\u73af\uff0c\u8bf7\u4f7f\u7528 while(true) \u3002 \u5faa\u73af\u53d8\u91cf \u901a\u5e38\uff0c\u6211\u4eec\u4f1a\u5e0c\u671b\u4e00\u4e2a\u5faa\u73af\u6267\u884c\u4e00\u5b9a\u6b21\u6570\u3002\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u53d8\u91cf\uff0c\u79f0\u4e3a\u8ba1\u6570\u5668\u3002\u5faa\u73af\u53d8\u91cf\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u7528\u4e8e\u8ba1\u7b97\u5faa\u73af\u6267\u884c\u4e86\u591a\u5c11\u6b21\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf count \u662f\u4e00\u4e2a\u5faa\u73af\u53d8\u91cf\u3002 \u5faa\u73af\u53d8\u91cf\u901a\u5e38\u6709\u7b80\u5355\u7684\u540d\u79f0\uff0c\u5982i \u3001 j \u6216 k \u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u60f3\u77e5\u9053\u5faa\u73af\u53d8\u91cf\u5728\u7a0b\u5e8f\u4e2d\u7684\u54ea\u4e2a\u4f4d\u7f6e\u88ab\u4f7f\u7528\uff0c\u6b64\u65f6\u4f60\u4f1a\u641c\u7d22 i \u3001j \u6216 k \uff0c\u6b64\u65f6\u4f1a\u8fd4\u56de\u5927\u91cf\u7684\u641c\u7d22\u7ed3\u679c\uff01\u56e0\u6b64\uff0c\u4e00\u4e9b\u5f00\u53d1\u4eba\u5458\u559c\u6b22\u7528 iii \u3001 jjj \u6216 kkk \u8fd9\u6837\u7684\u53d8\u91cf\u540d\u3002\u56e0\u4e3a\u8fd9\u4e9b\u540d\u79f0\u66f4\u52a0\u72ec\u7279\uff0c\u8fd9\u4f7f\u5f97\u641c\u7d22\u5faa\u73af\u53d8\u91cf\u66f4\u52a0\u5bb9\u6613\uff0c\u5e76\u5e2e\u52a9\u5b83\u4eec\u4f5c\u4e3a\u5faa\u73af\u53d8\u91cf\u8131\u9896\u800c\u51fa\u3002\u4e00\u4e2a\u66f4\u597d\u7684\u4e3b\u610f\u662f\u4f7f\u7528\u201c\u771f\u5b9e\u7684\u201d\u53d8\u91cf\u540d\uff0c\u6bd4\u5982 count \uff0c\u6216\u8005\u4e00\u4e2a\u5173\u4e8e\u4f60\u6b63\u5728\u8ba1\u6570\u7684\u4e1c\u897f\u7684\u66f4\u8be6\u7ec6\u7684\u540d\u5b57(\u4f8b\u5982 userCount )\u3002 \u5faa\u73af\u53d8\u91cf\u5e94\u8be5\u4e3a\u6709\u7b26\u53f7\u6570 \u5faa\u73af\u53d8\u91cf\u51e0\u4e4e\u90fd\u5e94\u8be5\u662f\u6709\u7b26\u53f7\u7684\uff0c\u56e0\u4e3a\u65e0\u7b26\u53f7\u6574\u6570\u53ef\u80fd\u5bfc\u81f4\u610f\u60f3\u4e0d\u5230\u7684\u95ee\u9898\u3002\u8003\u8651\u4ee5\u4e0b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include int main () { unsigned int count { 10 }; // count from 10 down to 0 while ( count >= 0 ) { if ( count == 0 ) { std :: cout << \"blastoff!\" ; } else { std :: cout << count << ' ' ; } -- count ; } std :: cout << '\\n' ; return 0 ; } \u770b\u4e00\u4e0b\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u770b\u770b\u80fd\u5426\u53d1\u73b0\u9519\u8bef\u3002\u5e76\u4e0d\u662f\u5f88\u660e\u663e\u3002 \u4e8b\u5b9e\u8bc1\u660e\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u662f\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u3002\u5b83\u5f00\u59cb\u6253\u5370 10 9 8 7 6 5 4 3 2 1 blastoff \uff0c\u4f46\u968f\u540e\u5c31\u8131\u8f68\u4e86\uff0c\u4ece 4294967295 \u5f00\u59cb\u5012\u6570\u3002\u4e3a\u4ec0\u4e48\uff1f\u56e0\u4e3a\u5faa\u73af\u6761\u4ef6 count >= 0 \u6c38\u8fdc\u4e0d\u4f1a\u4e3a\u5047\uff01\u5f53 count \u4e3a 0 \u65f6\uff0c 0 >= 0 \u4e3a\u771f\u3002\u7136\u540e\u6267\u884c --count \uff0c count \u53cd\u8f6c\u4e3a 4294967295 (\u5047\u8bbe\u662f32\u4f4d\u6574\u6570)\u3002\u7531\u4e8e 4294967295 >= 0 \u4e3a\u771f\uff0c\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c\u3002\u56e0\u4e3a count \u662f\u65e0\u7b26\u53f7\u7684\uff0c\u5b83\u6c38\u8fdc\u4e0d\u53ef\u80fd\u662f\u8d1f\u6570\uff0c\u6240\u4ee5\u5faa\u73af\u6c38\u8fdc\u4e0d\u4f1a\u505c\u6b62\u3002 \u6700\u4f73\u5b9e\u8df5 \u5faa\u73af\u53d8\u91cf\u5e94\u8be5\u662f\u6709\u7b26\u53f7\u6574\u578b\u3002 \u8fed\u4ee3\u6267\u884cN\u6b21 \u6bcf\u6b21\u5faa\u73af\u6267\u884c\u88ab\u79f0\u4e3a\u4e00\u4e2a\u8fed\u4ee3\u3002 \u901a\u5e38\uff0c\u6211\u4eec\u5e0c\u671b\u6bcf\u9694\u7b2c2\u30013\u62164\u6b21\u8fed\u4ee3\u505a\u4e00\u4e9b\u4e8b\u60c5\uff0c\u4f8b\u5982\u6253\u5370\u6362\u884c\u7b26\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728\u8ba1\u6570\u5668\u4e0a\u4f7f\u7528\u6c42\u6a21\u8fd0\u7b97\u8f7b\u677e\u5b9e\u73b0: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include // Iterate through every number between 1 and 50 int main () { int count { 1 }; while ( count <= 50 ) { // print the number (pad numbers under 10 with a leading 0 for formatting purposes) if ( count < 10 ) { std :: cout << '0' ; } std :: cout << count << ' ' ; // \u5982\u679c\u5faa\u73af\u6b21\u6570\u53ef\u4ee5\u6574\u966410\uff0c\u5219\u6253\u5370\u6362\u884c if ( count % 10 == 0 ) { std :: cout << '\\n' ; } // increment the loop counter ++ count ; } return 0 ; } \u6253\u5370\uff1a 1 2 3 4 5 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 \u5d4c\u5957\u5faa\u73af \u5faa\u73af\u53ef\u4ee5\u5d4c\u5957\u5728\u5176\u4ed6\u5faa\u73af\u4e2d\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u88ab\u5d4c\u5957\u5faa\u73af(\u6211\u4eec\u79f0\u4e4b\u4e3a\u5185\u5c42\u5faa\u73af)\u548c\u5916\u5c42\u5faa\u73af\u90fd\u6709\u5404\u81ea\u7684\u8ba1\u6570\u5668\u3002\u6ce8\u610f\uff0c\u5185\u90e8\u5faa\u73af\u7684\u5faa\u73af\u8868\u8fbe\u5f0f\u4e5f\u4f7f\u7528\u4e86\u5916\u90e8\u5faa\u73af\u7684\u8ba1\u6570\u5668\uff01 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include int main () { // outer loops between 1 and 5 int outer { 1 }; while ( outer <= 5 ) { // For each iteration of the outer loop, the code in the body of the loop executes once // inner loops between 1 and outer int inner { 1 }; while ( inner <= outer ) { std :: cout << inner << ' ' ; ++ inner ; } // print a newline at the end of each row std :: cout << '\\n' ; ++ outer ; } return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 2 3 4 5 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 \u65b0\u624b\u7a0b\u5e8f\u5458\u5f80\u5f80\u96be\u4ee5\u7406\u89e3\u5d4c\u5957\u5faa\u73af\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u4e5f\u770b\u4e0d\u61c2\u7684\u8bdd\uff0c\u5343\u4e07\u4e0d\u8981\u7070\u5fc3\u4e27\u6c14\u3002\u5bf9\u4e8e\u5916\u5c42\u5faa\u73af\u7684\u6bcf\u4e00\u6b21\u8fed\u4ee3\uff0c\u5916\u5c42\u5faa\u73af\u7684\u5faa\u73af\u4f53\u5185\u8bed\u53e5\u4f1a\u6267\u884c\u4e00\u6b21\u3002\u7531\u4e8e\u5916\u5c42\u5faa\u73af\u7684\u5faa\u73af\u4f53\u5185\u5305\u62ec\u5185\u5c42\u5faa\u73af\uff0c\u6240\u4ee5\u5185\u5c42\u5faa\u73af\u5728\u6bcf\u4e00\u4e2a\u5916\u5c42\u5faa\u73af\u7684\u8fed\u4ee3\u4e2d\u90fd\u8981\u5b8c\u6574\u6267\u884c\u4e00\u6b21\u3002 \u8ba9\u6211\u4eec\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\u4e0a\u9762\u7684\u4ee3\u7801\uff1a \u9996\u5148\uff0c\u5bf9\u4e8e\u5916\u5c42\u5faa\u73af(\u5faa\u73af\u53d8\u91cf\u4e3a outer ) \u4f1a\u8fed\u4ee3\u6267\u884c5\u6b21\uff08 outer \u4f9d\u6b21\u53d8\u4e3a 1 , 2 , 3 , 4 , \u548c 5 \uff09\u3002 \u5916\u5c42\u5faa\u73af\u7b2c\u4e00\u6b21\u8fed\u4ee3\u65f6 outer \u503c\u4e3a 1 \uff0c\u7136\u540e\u6267\u884c\u5faa\u73af\u4f53\u5185\u7684\u8bed\u53e5\u3002\u5728\u5faa\u73af\u4f53\u5185\uff0c\u6709\u53e6\u5916\u4e00\u4e2a\u5faa\u73af\uff0c\u5176\u5faa\u73af\u53d8\u91cf\u4e3a inner \u3002\u5185\u5c42\u5faa\u73af\u7684\u5faa\u73af\u53d8\u91cf\u4ece 1 \u5230 outer (\u6b64\u65f6\u662f 1 )\uff0c\u6240\u4ee5\u5185\u5c42\u5faa\u73af\u6267\u884c\u4e00\u6b21\uff0c\u6253\u5370 1 \u3002\u7136\u540e\u6253\u5370\u6362\u884c\uff0c\u5e76\u5c06 outer \u9012\u589e\u4e3a 2 \u3002 \u5728\u5916\u5c42\u5faa\u73af\u6267\u884c\u7b2c\u4e8c\u6b21\u8fed\u4ee3\u65f6\uff0c outer \u7684\u503c\u4e3a 2 \uff0c\u7136\u540e\u6267\u884c\u5faa\u73af\u4f53\u3002\u5728\u5faa\u73af\u4f53\u5185\uff0c inner \u4ecd\u7136\u4ece 1 \u5230 outer (\u6b64\u65f6\u4e3a 2 )\u8fdb\u884c\u8fed\u4ee3\u3002 \u6240\u4ee5\u8fd9\u4e00\u6b21\u5185\u5c42\u5faa\u73af\u4f1a\u6267\u884c\u4e24\u6b21\uff0c\u6253\u5370 1 \u548c 2 \u3002 \u7136\u540e\u6253\u5370\u6362\u884c\uff0c\u5e76\u5c06 outer \u9012\u589e\u4e3a 3 \u3002 \u8fed\u4ee3\u7ee7\u7eed\u8fdb\u884c\u7684\u8bdd\uff0c\u5185\u5b58\u5faa\u73af\u4f1a\u4f9d\u6b21\u6253\u5370 1 2 3 \u3001 1 2 3 4 \u4ee5\u53ca 1 2 3 4 5 \u3002\u6700\u7ec8\uff0c outer \u4f1a\u9012\u589e\u5230 6 \uff0c\u6b64\u65f6\u56e0\u4e3a\u5916\u5c42\u7684\u5faa\u73af\u6761\u4ef6( outer <= 5 ) \u6c42\u503c\u4e3afalse\uff0c\u6240\u4ee5\u5916\u5c42\u5faa\u73af\u4f1a\u7ec8\u6b62\u3002\u7a0b\u5e8f\u9000\u51fa\u3002 \u5982\u679c\u60a8\u4ecd\u7136\u611f\u5230\u56f0\u60d1\uff0c\u53ef\u4ee5\u5728\u8c03\u8bd5\u5668\u4e2d\u9010\u884c\u68c0\u67e5\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u5e76\u67e5\u770b\u201cinner\u201d\u548c\u201couter\u201d\u7684\u503c\uff0c\u901a\u8fc7\u8fd9\u4e2a\u65b9\u6cd5\u4f60\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u4ee3\u7801\u7684\u8fd0\u884c\u903b\u8f91\u3002","title":"7.7 - \u5faa\u73af\u548c while \u8bed\u53e5"},{"location":"7-7-Intro-to-loops-and-while-statements/#_1","text":"\u73b0\u5728\u771f\u6b63\u7684\u4e50\u8da3\u5f00\u59cb\u4e86\u2014\u2014\u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5faa\u73af\u3002\u5faa\u73af\u662f\u63a7\u5236\u6d41\u7ed3\u6784\uff0c\u5b83\u5141\u8bb8\u4e00\u6bb5\u4ee3\u7801\u88ab\u91cd\u590d\u6267\u884c\uff0c\u76f4\u5230\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u3002\u5faa\u73af\u4e3a\u7f16\u7a0b\u5de5\u5177\u5305\u589e\u52a0\u4e86\u5f88\u5927\u7684\u7075\u6d3b\u6027\uff0c\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u5f88\u591a\u4e0d\u4f7f\u7528\u5faa\u73af\u96be\u4ee5\u5b9e\u73b0\u7684\u529f\u80fd\u3002 \u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u60f3\u8981\u6253\u53701\u523010\uff0c\u5982\u679c\u6ca1\u6709\u5faa\u73af\uff0c\u4f60\u53ef\u80fd\u4f1a\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << \"1 2 3 4 5 6 7 8 9 10\" ; std :: cout << \" done! \\n \" ; return 0 ; } \u867d\u7136\u8fd9\u4e48\u505a\u662f\u53ef\u884c\u7684\uff0c\u4f46\u5f53\u6211\u4eec\u9700\u8981\u6253\u5370\u66f4\u591a\u7684\u6570\u5b57\u65f6\uff0c\u5b83\u4f1a\u53d8\u5f97\u8d8a\u6765\u8d8a\u4e0d\u53ef\u884c\uff1a\u5982\u679c\u9700\u8981\u6253\u53701\u52301000\u4e4b\u95f4\u7684\u6240\u6709\u6570\u5b57\u5462\uff1f\u90a3\u6253\u5b57\u7684\u5de5\u4f5c\u91cf\u5c31\u592a\u5927\u4e86\uff01\u800c\u4e14\u8be5\u7a0b\u5e8f\u8981\u6253\u5370\u7684\u5185\u5bb9\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u5c31\u77e5\u9053\u8981\u6253\u5370\u591a\u5c11\u6570\u5b57\u3002 \u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u7a0d\u5fae\u6539\u53d8\u4e00\u4e0b\u53c2\u6570\u3002\u5982\u679c\u9700\u8981\u8ba9\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff0c\u7136\u540e\u6253\u53701\u548c\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\u4e4b\u95f4\u7684\u6240\u6709\u6570\u5b57\uff0c\u90a3\u4f1a\u600e\u4e48\u6837\u5462?\u7528\u6237\u5c06\u8f93\u5165\u7684\u6570\u5b57\u5728\u7f16\u8bd1\u65f6\u662f\u4e0d\u53ef\u77e5\u7684\uff0c\u8981\u600e\u4e48\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5462?","title":"\u5faa\u73af"},{"location":"7-7-Intro-to-loops-and-while-statements/#while","text":"while \u8bed\u53e5\uff08\u4e5f\u88ab\u53eb\u505a while \u5faa\u73af\uff09\u662fC++\u4e2d\u6700\u7b80\u5355\u5faa\u73af\uff0c\u5b83\u7684\u5b9a\u4e49\u548c if \u8bed\u53e5\u7279\u522b\u5730\u50cf\uff1a 1 2 while ( condition ) statement ; while \u8bed\u53e5\u7684\u58f0\u660e\u9700\u8981\u4f7f\u7528 while \u5173\u952e\u5b57\u3002\u5f53 while \u8bed\u53e5\u6267\u884c\u65f6\uff0c condition \u4f1a\u88ab\u6c42\u503c\u3002\u5982\u679c\u6c42\u503c\u7ed3\u679c\u662f\u771f\uff0c\u5219\u76f8\u5173\u7684\u8bed\u53e5\u4f1a\u88ab\u6267\u884c\u3002 \u4f46\u662f\uff0c\u548cif\u8bed\u53e5\u4e0d\u540c\u7684\u662f\uff0c while \u8bed\u53e5\u4e2d\u7684 statement \u6267\u884c\u5b8c\u6210\u540e\uff0c\u4f1a\u8fd4\u56de\u5230 while \u8bed\u53e5\u7684\u5f00\u59cb\uff0c\u7136\u540e\u7ee7\u7eed\u6267\u884c\u3001\u8fd9\u610f\u5473\u7740\u53ea\u8981\u6761\u4ef6\u6c42\u503c\u4e3a\u771f\uff0c while \u5faa\u73af\u5c31\u4f1a\u4e0d\u505c\u5730\u6267\u884c\u3002 \u8fd8\u662f\u6253\u53701\u523010\u7684\u4f8b\u5b50\uff0c\u8fd9\u6b21\u4f7f\u7528 while \u5faa\u73af\u5b8c\u6210\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int count { 1 }; while ( count <= 10 ) { std :: cout << count << ' ' ; ++ count ; } std :: cout << \"done! \\n \" ; return 0 ; } \u8f93\u51fa\uff1a 1 1 2 3 4 5 6 7 8 9 10 done! \u8ba9\u6211\u4eec\u4ed4\u7ec6\u770b\u770b\u8fd9\u4e2a\u7a0b\u5e8f\u6b63\u5728\u505a\u4ec0\u4e48\u3002\u9996\u5148\uff0c count \u88ab\u521d\u59cb\u5316\u4e3a1\uff0c\u8fd9\u662f\u6211\u4eec\u8981\u6253\u5370\u7684\u7b2c\u4e00\u4e2a\u6570\u5b57\u3002\u6761\u4ef6 count <= 10 \u4e3a true \uff0c\u56e0\u6b64\u6267\u884c\u8bed\u53e5\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u7684\u8bed\u53e5\u662f\u4e00\u4e2a\u8bed\u53e5\u5757\uff0c\u56e0\u6b64\u5757\u4e2d\u7684\u6240\u6709\u8bed\u53e5\u90fd\u5c06\u6267\u884c\u3002\u5757\u4e2d\u7684\u7b2c\u4e00\u4e2a\u8bed\u53e5\u8f93\u51fa 1 \u548c\u4e00\u4e2a\u7a7a\u683c\uff0c\u7b2c\u4e8c\u4e2a\u8bed\u53e5\u5c06 count \u9012\u589e\u52302\u3002\u63a7\u5236\u73b0\u5728\u8fd4\u56de\u5230 while \u7684\u9876\u90e8\uff0c\u5e76\u518d\u6b21\u8ba1\u7b97\u6761\u4ef6\u3002 2 <= 10 \u7684\u8ba1\u7b97\u7ed3\u679c\u4e3a\u4ecd\u7136 true \uff0c\u56e0\u6b64\u4ee3\u7801\u5757\u5c06\u518d\u6b21\u6267\u884c\u3002\u5faa\u73af\u5c06\u91cd\u590d\u6267\u884c\uff0c\u76f4\u5230 count \u4e3a11 \uff0c\u6b64\u65f6 11 <= 10 \u5c06\u88ab\u8ba1\u7b97\u4e3a false \uff0c\u4e0e\u5faa\u73af\u76f8\u5173\u7684\u8bed\u53e5\u5c06\u88ab\u8df3\u8fc7\u3002\u81f3\u6b64\uff0c\u5faa\u73af\u5b8c\u6210\u4e86\u3002 \u867d\u7136\u4e0a\u9762\u7684\u4ee3\u7801\u770b\u4e0a\u53bb\u4f3c\u4e4e\u6bd4\u624b\u52a8\u8f93\u51651\u523010\u8fd8\u591a\u4e9b\uff0c\u4f46\u662f\u4f60\u53ef\u4ee5\u60f3\u8c61\uff0c\u5982\u679c\u9700\u8981\u6253\u53701\u52301000\uff0c\u6211\u4eec\u53ea\u9700\u8981\u7b80\u5355\u5730\u5c06 count <= 10 \u6539\u4e3a count <= 1000 \u5373\u53ef\u3002","title":"while \u8bed\u53e5"},{"location":"7-7-Intro-to-loops-and-while-statements/#falsewhile","text":"\u5982\u679c\u521d\u59cb\u5316\u6761\u4ef6\u4e3a false \uff0c\u90a3\u4e48\u8bed\u53e5\u5c31\u5b8c\u5168\u4e0d\u4f1a\u88ab\u6267\u884c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int count { 15 }; while ( count <= 10 ) { std :: cout << count << ' ' ; ++ count ; } std :: cout << \"done! \\n \" ; return 0 ; } \u7531\u4e8e 15 <= 10 \u6c42\u503c\u4e3a false \uff0c\u6240\u4ee5 while \u4e2d\u7684\u6240\u6709\u8bed\u53e5\u90fd\u4f1a\u88ab\u8df3\u8fc7\uff0c\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c\u5e76\u6253\u5370 done \u3002","title":"\u521d\u59cb\u6761\u4ef6\u4e3afalse\u7684while\u8bed\u53e5"},{"location":"7-7-Intro-to-loops-and-while-statements/#_2","text":"\u5982\u679c\u8868\u8fbe\u5f0f\u59cb\u7ec8\u6c42\u503c\u4e3a true \uff0c\u5219 while \u5faa\u73af\u5c31\u4f1a\u4e00\u76f4\u8fd0\u884c\u2014\u2014\u79f0\u4e3a\u65e0\u9650\u5faa\u73af\u6216\u6b7b\u5faa\u73af\u3002\u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int count { 1 }; while ( count <= 10 ) // \u8be5\u6761\u4ef6\u7528\u4e8e\u4e0d\u4f1a\u53d8\u4e3afalse { std :: cout << count << ' ' ; // \u53cd\u590d\u6267\u884c } std :: cout << '\\n' ; // \u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c return 0 ; // \u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c } \u56e0\u4e3a count \u59cb\u7ec8\u6ca1\u6709\u88ab\u9012\u589e\uff0c\u6240\u4ee5 count <= 10 \u603b\u662f\u771f\u3002\u56e0\u6b64\u5faa\u73af\u6c38\u8fdc\u4e0d\u4f1a\u505c\u6b62\uff0c\u7a0b\u5e8f\u4e0d\u505c\u5730\u6253\u5370 \u201c1 1 1 1 1\u2033\u3002","title":"\u65e0\u9650\u5faa\u73af"},{"location":"7-7-Intro-to-loops-and-while-statements/#_3","text":"\u6211\u4eec\u53ef\u4ee5\u6545\u610f\u521b\u5efa\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff1a 1 2 3 4 while ( true ) { // this loop will execute forever } \u9000\u51fa\u65e0\u9650\u5faa\u73af\u7684\u552f\u4e00\u65b9\u6cd5\u662f\u4f7f\u7528 return y \u8bed\u53e5\u3001 break \u8bed\u53e5\u3001 exit \u8bed\u53e5\u3001 goto \u8bed\u53e5\u3001\u629b\u51fa\u5f02\u5e38\u6216\u88ab\u5176\u4ed6\u7a0b\u5e8f\u7ec8\u6b62\u3002 \u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { while ( true ) // infinite loop { std :: cout << \"Loop again (y/n)? \" ; char c {}; std :: cin >> c ; if ( c == 'n' ) return 0 ; } return 0 ; } \u8be5\u5faa\u73af\u4f1a\u4e0d\u65ad\u6267\u884c\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165 n \uff0c\u6b64\u65f6if\u8bed\u53e5\u7684\u6761\u4ef6\u6c42\u503c\u4e3a\u771f\uff0c return 0 \u88ab\u6267\u884c\uff0cmain\u51fd\u6570\u9000\u51fa\uff0c\u7a0b\u5e8f\u7ec8\u6b62\u3002 \u5728\u6301\u7eed\u8fd0\u884c\u5e76\u4e3aweb\u8bf7\u6c42\u63d0\u4f9b\u670d\u52a1\u7684web\u670d\u52a1\u5668\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u7ecf\u5e38\u53ef\u4ee5\u770b\u5230\u8fd9\u79cd\u5faa\u73af\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u4e8e\u6709\u610f\u800c\u4e3a\u4e4b\u7684\u6b7b\u5faa\u73af\uff0c\u8bf7\u4f7f\u7528 while(true) \u3002","title":"\u6709\u610f\u800c\u4e3a\u4e4b\u7684\u65e0\u9650\u5faa\u73af"},{"location":"7-7-Intro-to-loops-and-while-statements/#_4","text":"\u901a\u5e38\uff0c\u6211\u4eec\u4f1a\u5e0c\u671b\u4e00\u4e2a\u5faa\u73af\u6267\u884c\u4e00\u5b9a\u6b21\u6570\u3002\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u53d8\u91cf\uff0c\u79f0\u4e3a\u8ba1\u6570\u5668\u3002\u5faa\u73af\u53d8\u91cf\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u7528\u4e8e\u8ba1\u7b97\u5faa\u73af\u6267\u884c\u4e86\u591a\u5c11\u6b21\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf count \u662f\u4e00\u4e2a\u5faa\u73af\u53d8\u91cf\u3002 \u5faa\u73af\u53d8\u91cf\u901a\u5e38\u6709\u7b80\u5355\u7684\u540d\u79f0\uff0c\u5982i \u3001 j \u6216 k \u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u60f3\u77e5\u9053\u5faa\u73af\u53d8\u91cf\u5728\u7a0b\u5e8f\u4e2d\u7684\u54ea\u4e2a\u4f4d\u7f6e\u88ab\u4f7f\u7528\uff0c\u6b64\u65f6\u4f60\u4f1a\u641c\u7d22 i \u3001j \u6216 k \uff0c\u6b64\u65f6\u4f1a\u8fd4\u56de\u5927\u91cf\u7684\u641c\u7d22\u7ed3\u679c\uff01\u56e0\u6b64\uff0c\u4e00\u4e9b\u5f00\u53d1\u4eba\u5458\u559c\u6b22\u7528 iii \u3001 jjj \u6216 kkk \u8fd9\u6837\u7684\u53d8\u91cf\u540d\u3002\u56e0\u4e3a\u8fd9\u4e9b\u540d\u79f0\u66f4\u52a0\u72ec\u7279\uff0c\u8fd9\u4f7f\u5f97\u641c\u7d22\u5faa\u73af\u53d8\u91cf\u66f4\u52a0\u5bb9\u6613\uff0c\u5e76\u5e2e\u52a9\u5b83\u4eec\u4f5c\u4e3a\u5faa\u73af\u53d8\u91cf\u8131\u9896\u800c\u51fa\u3002\u4e00\u4e2a\u66f4\u597d\u7684\u4e3b\u610f\u662f\u4f7f\u7528\u201c\u771f\u5b9e\u7684\u201d\u53d8\u91cf\u540d\uff0c\u6bd4\u5982 count \uff0c\u6216\u8005\u4e00\u4e2a\u5173\u4e8e\u4f60\u6b63\u5728\u8ba1\u6570\u7684\u4e1c\u897f\u7684\u66f4\u8be6\u7ec6\u7684\u540d\u5b57(\u4f8b\u5982 userCount )\u3002","title":"\u5faa\u73af\u53d8\u91cf"},{"location":"7-7-Intro-to-loops-and-while-statements/#_5","text":"\u5faa\u73af\u53d8\u91cf\u51e0\u4e4e\u90fd\u5e94\u8be5\u662f\u6709\u7b26\u53f7\u7684\uff0c\u56e0\u4e3a\u65e0\u7b26\u53f7\u6574\u6570\u53ef\u80fd\u5bfc\u81f4\u610f\u60f3\u4e0d\u5230\u7684\u95ee\u9898\u3002\u8003\u8651\u4ee5\u4e0b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include int main () { unsigned int count { 10 }; // count from 10 down to 0 while ( count >= 0 ) { if ( count == 0 ) { std :: cout << \"blastoff!\" ; } else { std :: cout << count << ' ' ; } -- count ; } std :: cout << '\\n' ; return 0 ; } \u770b\u4e00\u4e0b\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u770b\u770b\u80fd\u5426\u53d1\u73b0\u9519\u8bef\u3002\u5e76\u4e0d\u662f\u5f88\u660e\u663e\u3002 \u4e8b\u5b9e\u8bc1\u660e\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u662f\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u3002\u5b83\u5f00\u59cb\u6253\u5370 10 9 8 7 6 5 4 3 2 1 blastoff \uff0c\u4f46\u968f\u540e\u5c31\u8131\u8f68\u4e86\uff0c\u4ece 4294967295 \u5f00\u59cb\u5012\u6570\u3002\u4e3a\u4ec0\u4e48\uff1f\u56e0\u4e3a\u5faa\u73af\u6761\u4ef6 count >= 0 \u6c38\u8fdc\u4e0d\u4f1a\u4e3a\u5047\uff01\u5f53 count \u4e3a 0 \u65f6\uff0c 0 >= 0 \u4e3a\u771f\u3002\u7136\u540e\u6267\u884c --count \uff0c count \u53cd\u8f6c\u4e3a 4294967295 (\u5047\u8bbe\u662f32\u4f4d\u6574\u6570)\u3002\u7531\u4e8e 4294967295 >= 0 \u4e3a\u771f\uff0c\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c\u3002\u56e0\u4e3a count \u662f\u65e0\u7b26\u53f7\u7684\uff0c\u5b83\u6c38\u8fdc\u4e0d\u53ef\u80fd\u662f\u8d1f\u6570\uff0c\u6240\u4ee5\u5faa\u73af\u6c38\u8fdc\u4e0d\u4f1a\u505c\u6b62\u3002 \u6700\u4f73\u5b9e\u8df5 \u5faa\u73af\u53d8\u91cf\u5e94\u8be5\u662f\u6709\u7b26\u53f7\u6574\u578b\u3002","title":"\u5faa\u73af\u53d8\u91cf\u5e94\u8be5\u4e3a\u6709\u7b26\u53f7\u6570"},{"location":"7-7-Intro-to-loops-and-while-statements/#n","text":"\u6bcf\u6b21\u5faa\u73af\u6267\u884c\u88ab\u79f0\u4e3a\u4e00\u4e2a\u8fed\u4ee3\u3002 \u901a\u5e38\uff0c\u6211\u4eec\u5e0c\u671b\u6bcf\u9694\u7b2c2\u30013\u62164\u6b21\u8fed\u4ee3\u505a\u4e00\u4e9b\u4e8b\u60c5\uff0c\u4f8b\u5982\u6253\u5370\u6362\u884c\u7b26\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728\u8ba1\u6570\u5668\u4e0a\u4f7f\u7528\u6c42\u6a21\u8fd0\u7b97\u8f7b\u677e\u5b9e\u73b0: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include // Iterate through every number between 1 and 50 int main () { int count { 1 }; while ( count <= 50 ) { // print the number (pad numbers under 10 with a leading 0 for formatting purposes) if ( count < 10 ) { std :: cout << '0' ; } std :: cout << count << ' ' ; // \u5982\u679c\u5faa\u73af\u6b21\u6570\u53ef\u4ee5\u6574\u966410\uff0c\u5219\u6253\u5370\u6362\u884c if ( count % 10 == 0 ) { std :: cout << '\\n' ; } // increment the loop counter ++ count ; } return 0 ; } \u6253\u5370\uff1a 1 2 3 4 5 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50","title":"\u8fed\u4ee3\u6267\u884cN\u6b21"},{"location":"7-7-Intro-to-loops-and-while-statements/#_6","text":"\u5faa\u73af\u53ef\u4ee5\u5d4c\u5957\u5728\u5176\u4ed6\u5faa\u73af\u4e2d\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u88ab\u5d4c\u5957\u5faa\u73af(\u6211\u4eec\u79f0\u4e4b\u4e3a\u5185\u5c42\u5faa\u73af)\u548c\u5916\u5c42\u5faa\u73af\u90fd\u6709\u5404\u81ea\u7684\u8ba1\u6570\u5668\u3002\u6ce8\u610f\uff0c\u5185\u90e8\u5faa\u73af\u7684\u5faa\u73af\u8868\u8fbe\u5f0f\u4e5f\u4f7f\u7528\u4e86\u5916\u90e8\u5faa\u73af\u7684\u8ba1\u6570\u5668\uff01 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include int main () { // outer loops between 1 and 5 int outer { 1 }; while ( outer <= 5 ) { // For each iteration of the outer loop, the code in the body of the loop executes once // inner loops between 1 and outer int inner { 1 }; while ( inner <= outer ) { std :: cout << inner << ' ' ; ++ inner ; } // print a newline at the end of each row std :: cout << '\\n' ; ++ outer ; } return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 2 3 4 5 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 \u65b0\u624b\u7a0b\u5e8f\u5458\u5f80\u5f80\u96be\u4ee5\u7406\u89e3\u5d4c\u5957\u5faa\u73af\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u4e5f\u770b\u4e0d\u61c2\u7684\u8bdd\uff0c\u5343\u4e07\u4e0d\u8981\u7070\u5fc3\u4e27\u6c14\u3002\u5bf9\u4e8e\u5916\u5c42\u5faa\u73af\u7684\u6bcf\u4e00\u6b21\u8fed\u4ee3\uff0c\u5916\u5c42\u5faa\u73af\u7684\u5faa\u73af\u4f53\u5185\u8bed\u53e5\u4f1a\u6267\u884c\u4e00\u6b21\u3002\u7531\u4e8e\u5916\u5c42\u5faa\u73af\u7684\u5faa\u73af\u4f53\u5185\u5305\u62ec\u5185\u5c42\u5faa\u73af\uff0c\u6240\u4ee5\u5185\u5c42\u5faa\u73af\u5728\u6bcf\u4e00\u4e2a\u5916\u5c42\u5faa\u73af\u7684\u8fed\u4ee3\u4e2d\u90fd\u8981\u5b8c\u6574\u6267\u884c\u4e00\u6b21\u3002 \u8ba9\u6211\u4eec\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\u4e0a\u9762\u7684\u4ee3\u7801\uff1a \u9996\u5148\uff0c\u5bf9\u4e8e\u5916\u5c42\u5faa\u73af(\u5faa\u73af\u53d8\u91cf\u4e3a outer ) \u4f1a\u8fed\u4ee3\u6267\u884c5\u6b21\uff08 outer \u4f9d\u6b21\u53d8\u4e3a 1 , 2 , 3 , 4 , \u548c 5 \uff09\u3002 \u5916\u5c42\u5faa\u73af\u7b2c\u4e00\u6b21\u8fed\u4ee3\u65f6 outer \u503c\u4e3a 1 \uff0c\u7136\u540e\u6267\u884c\u5faa\u73af\u4f53\u5185\u7684\u8bed\u53e5\u3002\u5728\u5faa\u73af\u4f53\u5185\uff0c\u6709\u53e6\u5916\u4e00\u4e2a\u5faa\u73af\uff0c\u5176\u5faa\u73af\u53d8\u91cf\u4e3a inner \u3002\u5185\u5c42\u5faa\u73af\u7684\u5faa\u73af\u53d8\u91cf\u4ece 1 \u5230 outer (\u6b64\u65f6\u662f 1 )\uff0c\u6240\u4ee5\u5185\u5c42\u5faa\u73af\u6267\u884c\u4e00\u6b21\uff0c\u6253\u5370 1 \u3002\u7136\u540e\u6253\u5370\u6362\u884c\uff0c\u5e76\u5c06 outer \u9012\u589e\u4e3a 2 \u3002 \u5728\u5916\u5c42\u5faa\u73af\u6267\u884c\u7b2c\u4e8c\u6b21\u8fed\u4ee3\u65f6\uff0c outer \u7684\u503c\u4e3a 2 \uff0c\u7136\u540e\u6267\u884c\u5faa\u73af\u4f53\u3002\u5728\u5faa\u73af\u4f53\u5185\uff0c inner \u4ecd\u7136\u4ece 1 \u5230 outer (\u6b64\u65f6\u4e3a 2 )\u8fdb\u884c\u8fed\u4ee3\u3002 \u6240\u4ee5\u8fd9\u4e00\u6b21\u5185\u5c42\u5faa\u73af\u4f1a\u6267\u884c\u4e24\u6b21\uff0c\u6253\u5370 1 \u548c 2 \u3002 \u7136\u540e\u6253\u5370\u6362\u884c\uff0c\u5e76\u5c06 outer \u9012\u589e\u4e3a 3 \u3002 \u8fed\u4ee3\u7ee7\u7eed\u8fdb\u884c\u7684\u8bdd\uff0c\u5185\u5b58\u5faa\u73af\u4f1a\u4f9d\u6b21\u6253\u5370 1 2 3 \u3001 1 2 3 4 \u4ee5\u53ca 1 2 3 4 5 \u3002\u6700\u7ec8\uff0c outer \u4f1a\u9012\u589e\u5230 6 \uff0c\u6b64\u65f6\u56e0\u4e3a\u5916\u5c42\u7684\u5faa\u73af\u6761\u4ef6( outer <= 5 ) \u6c42\u503c\u4e3afalse\uff0c\u6240\u4ee5\u5916\u5c42\u5faa\u73af\u4f1a\u7ec8\u6b62\u3002\u7a0b\u5e8f\u9000\u51fa\u3002 \u5982\u679c\u60a8\u4ecd\u7136\u611f\u5230\u56f0\u60d1\uff0c\u53ef\u4ee5\u5728\u8c03\u8bd5\u5668\u4e2d\u9010\u884c\u68c0\u67e5\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u5e76\u67e5\u770b\u201cinner\u201d\u548c\u201couter\u201d\u7684\u503c\uff0c\u901a\u8fc7\u8fd9\u4e2a\u65b9\u6cd5\u4f60\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u4ee3\u7801\u7684\u8fd0\u884c\u903b\u8f91\u3002","title":"\u5d4c\u5957\u5faa\u73af"},{"location":"7-8-Do-while-statements/","tags":["do-while"],"text":"Key Takeaway \u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u5e0c\u671b\u5411\u7528\u6237\u663e\u793a\u4e00\u4e2a\u83dc\u5355\uff0c\u5e76\u8981\u6c42\u4ed6\u4eec\u8fdb\u884c\u9009\u62e9\u2014\u2014\u5982\u679c\u7528\u6237\u9009\u62e9\u4e86\u65e0\u6548\u7684\u9009\u62e9\uff0c\u5219\u9700\u8981\u518d\u6b21\u8be2\u95ee\u3002\u663e\u7136\uff0c\u83dc\u5355\u548c\u9009\u9879\u5e94\u8be5\u653e\u5728\u67d0\u79cd\u5faa\u73af\u4e2d(\u8fd9\u6837\u624d\u53ef\u4ee5\u53cd\u590d\u8be2\u95ee\u7528\u6237\uff0c\u76f4\u5230\u4ed6\u4eec\u8f93\u5165\u6709\u6548\u7684\u8f93\u5165)\uff0c\u4f46\u662f\u5e94\u8be5\u9009\u62e9\u54ea\u79cd\u7c7b\u578b\u7684\u5faa\u73af\u5462? \u7531\u4e8e while \u5faa\u73af\u4f1a\u5148\u5bf9\u6761\u4ef6\u6c42\u503c\uff0c\u6240\u4ee5\u4f7f\u7528 while \u5faa\u73af\u5f88\u522b\u626d\u3002\u5047\u8bbe\u6211\u4eec\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include int main () { // \u9009\u62e9\u5fc5\u987b\u88ab\u5b9a\u4e49\u5728\u5faa\u73af\u5916 int selection { 0 }; while ( selection != 1 && selection != 2 && selection != 3 && selection != 4 ) { std :: cout << \"Please make a selection: \\n \" ; std :: cout << \"1) Addition \\n \" ; std :: cout << \"2) Subtraction \\n \" ; std :: cout << \"3) Multiplication \\n \" ; std :: cout << \"4) Division \\n \" ; std :: cin >> selection ; } // \u7ee7\u7eed\u9009\u62e9\u8fdb\u884c\u64cd\u4f5c // \u53ef\u4ee5\u4f7f\u7528 switch \u8bed\u53e5\u8fdb\u884c std :: cout << \"You selected option #\" << selection << '\\n' ; return 0 ; } \u4f46\u662f\uff0c\u4e0a\u8ff0\u4ee3\u7801\u80fd\u591f\u6b63\u786e\u5de5\u4f5c\u7684\u539f\u56e0\u662f\u56e0\u4e3a\u6b64\u5904\u7684\u521d\u59cb\u503c\u4e3a0\u2014\u2014\u5b83\u4e0d\u662f\u4efb\u4f55\u5408\u6cd5\u7684 selection \u503c ( 1, 2, 3 or 4 )\u3002\u5982\u679c 0 \u4e5f\u662f\u5408\u6cd5\u7684\u503c\u4f1a\u600e\u6837\uff1f\u6211\u4eec\u5fc5\u987b\u9009\u62e9\u4e00\u4e2a\u4e0d\u540c\u7684\u521d\u59cb\u5316\u503c\u6765\u8868\u793a\u201c\u65e0\u6548\u201d\u2014\u2014\u8fd9\u5bfc\u81f4\u6211\u4eec\u7684\u4ee3\u7801\u4e2d\u5f15\u5165\u9b54\u9b3c\u6570\u5b57( 4.15 -\u5b57\u9762\u91cf )\u3002 \u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\u6765\u8ddf\u8e2a\u6709\u6548\u6027\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include int main () { int selection { 0 }; bool invalid { true }; // new variable just to gate the loop while ( invalid ) { std :: cout << \"Please make a selection: \\n \" ; std :: cout << \"1) Addition \\n \" ; std :: cout << \"2) Subtraction \\n \" ; std :: cout << \"3) Multiplication \\n \" ; std :: cout << \"4) Division \\n \" ; std :: cin >> selection ; invalid = ( selection != 1 && selection != 2 && selection != 3 && selection != 4 ); } // do something with selection here // such as a switch statement std :: cout << \"You selected option #\" << selection << '\\n' ; return 0 ; } \u867d\u7136\u8fd9\u907f\u514d\u4e86\u9b54\u9b3c\u6570\u5b57\uff0c\u4f46\u5f15\u5165\u4e86\u4e00\u4e2a\u65b0\u53d8\u91cf\uff0c\u4ee5\u786e\u4fdd\u5faa\u73af\u80fd\u591f\u81f3\u5c11\u8fd0\u884c\u4e00\u6b21\uff0c\u8fd9\u589e\u52a0\u4e86\u590d\u6742\u6027\u548c\u72af\u9519\u7684\u53ef\u80fd\u6027\u3002 do-while \u8bed\u53e5 \u4e3a\u4e86\u5e2e\u52a9\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\uff0cC++\u63d0\u4f9b\u4e86 do-while \u8bed\u53e5: 1 2 3 do statement ; // \u53ef\u4ee5\u662f\u5355\u4e00\u8bed\u53e5\uff0c\u4e5f\u53ef\u4ee5\u662f\u590d\u5408\u53e5 while ( condition ); do-while \u8bed\u53e5\u662f\u4e00\u4e2a\u5faa\u73af\u6784\u9020\u65b9\u5f0f\uff0c\u5176\u5de5\u4f5c\u539f\u7406\u4e0e while \u5faa\u73af\u7c7b\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u8be5\u8bed\u53e5\u603b\u662f\u81f3\u5c11\u6267\u884c\u4e00\u6b21\u3002\u8bed\u53e5\u6267\u884c\u540e\uff0cdo-while\u5faa\u73af\u68c0\u67e5\u6761\u4ef6\u3002\u5982\u679c\u6761\u4ef6\u7684\u8ba1\u7b97\u7ed3\u679c\u4e3a true \uff0c\u6267\u884c\u8def\u5f84\u5c06\u8df3\u8f6c\u56dedo-while\u5faa\u73af\u7684\u9876\u90e8\u5e76\u518d\u6b21\u6267\u884c\u3002 \u4e0b\u9762\u662f\u6211\u4eec\u4e0a\u9762\u7528do-while\u5faa\u73af\u4ee3\u66ffwhile\u5faa\u73af\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include int main () { // selection must be declared outside of the do-while so we can use it later int selection {}; do { std :: cout << \"Please make a selection: \\n \" ; std :: cout << \"1) Addition \\n \" ; std :: cout << \"2) Subtraction \\n \" ; std :: cout << \"3) Multiplication \\n \" ; std :: cout << \"4) Division \\n \" ; std :: cin >> selection ; } while ( selection != 1 && selection != 2 && selection != 3 && selection != 4 ); // do something with selection here // such as a switch statement std :: cout << \"You selected option #\" << selection << '\\n' ; return 0 ; } \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u65e2\u907f\u514d\u4e86\u9b54\u9b3c\u6570\u5b57\uff0c\u4e5f\u907f\u514d\u4e86\u989d\u5916\u7684\u53d8\u91cf\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u6709\u4e00\u4ef6\u4e8b\u503c\u5f97\u8ba8\u8bba\uff0c\u90a3\u5c31\u662f selection \u53d8\u91cf\u5fc5\u987b\u5728do\u5757\u4e4b\u5916\u58f0\u660e\u3002\u5982\u679c\u5728do\u5757\u4e2d\u58f0\u660e\u4e86 selection \u53d8\u91cf\uff0c\u90a3\u4e48\u5f53do\u5757\u7ec8\u6b62\u65f6\uff0c\u5b83\u5c06\u88ab\u9500\u6bc1\uff0c\u8fd9\u53d1\u751f\u5728\u6761\u4ef6\u503c\u88ab\u6c42\u503c\u4e4b\u524d\u3002\u4f46\u662f\u6211\u4eec\u9700\u8981\u5728while\u6761\u4ef6\u8bed\u53e5\u4e2d\u58f0\u660e\u53d8\u91cf\u2014\u2014\u56e0\u6b64\uff0c selection \u53d8\u91cf\u5fc5\u987b\u5728do\u5757\u4e4b\u5916\u58f0\u660e(\u5373\u4f7f\u5b83\u6ca1\u6709\u5728\u51fd\u6570\u4f53\u540e\u9762\u4f7f\u7528)\u3002 \u5b9e\u9645\u4e0a\uff0cdo-while\u5faa\u73af\u5e76\u4e0d\u5e38\u7528\u3002\u5c06\u6761\u4ef6\u653e\u5728\u5faa\u73af\u7684\u5e95\u90e8\u4f1a\u6a21\u7cca\u5faa\u73af\u6761\u4ef6\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u56e0\u6b64\uff0c\u8bb8\u591a\u5f00\u53d1\u4eba\u5458\u5efa\u8bae\u5b8c\u5168\u907f\u514ddo-while\u5faa\u73af\u3002\u6211\u4eec\u5c06\u91c7\u53d6\u4e00\u79cd\u8f83\u4e3a\u6e29\u548c\u7684\u7acb\u573a\uff0c\u5982\u679c\u4e24\u79cd\u65b9\u6cd5\u90fd\u53ef\u4ee5\u4f7f\u7528\u65f6\uff0c\u4f7f\u7528while\u5faa\u73af\u800c\u4e0d\u662fdo-while\u5faa\u73af\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u7ed9\u51fa\u76f8\u540c\u7684\u9009\u62e9\u65f6\uff0c\u66f4\u503e\u5411\u4e8ewhile\u5faa\u73af\u800c\u4e0d\u662fdo-while\u5faa\u73af\u3002","title":"7.8 - do-while \u8bed\u53e5"},{"location":"7-8-Do-while-statements/#do-while","text":"\u4e3a\u4e86\u5e2e\u52a9\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\uff0cC++\u63d0\u4f9b\u4e86 do-while \u8bed\u53e5: 1 2 3 do statement ; // \u53ef\u4ee5\u662f\u5355\u4e00\u8bed\u53e5\uff0c\u4e5f\u53ef\u4ee5\u662f\u590d\u5408\u53e5 while ( condition ); do-while \u8bed\u53e5\u662f\u4e00\u4e2a\u5faa\u73af\u6784\u9020\u65b9\u5f0f\uff0c\u5176\u5de5\u4f5c\u539f\u7406\u4e0e while \u5faa\u73af\u7c7b\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u8be5\u8bed\u53e5\u603b\u662f\u81f3\u5c11\u6267\u884c\u4e00\u6b21\u3002\u8bed\u53e5\u6267\u884c\u540e\uff0cdo-while\u5faa\u73af\u68c0\u67e5\u6761\u4ef6\u3002\u5982\u679c\u6761\u4ef6\u7684\u8ba1\u7b97\u7ed3\u679c\u4e3a true \uff0c\u6267\u884c\u8def\u5f84\u5c06\u8df3\u8f6c\u56dedo-while\u5faa\u73af\u7684\u9876\u90e8\u5e76\u518d\u6b21\u6267\u884c\u3002 \u4e0b\u9762\u662f\u6211\u4eec\u4e0a\u9762\u7528do-while\u5faa\u73af\u4ee3\u66ffwhile\u5faa\u73af\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include int main () { // selection must be declared outside of the do-while so we can use it later int selection {}; do { std :: cout << \"Please make a selection: \\n \" ; std :: cout << \"1) Addition \\n \" ; std :: cout << \"2) Subtraction \\n \" ; std :: cout << \"3) Multiplication \\n \" ; std :: cout << \"4) Division \\n \" ; std :: cin >> selection ; } while ( selection != 1 && selection != 2 && selection != 3 && selection != 4 ); // do something with selection here // such as a switch statement std :: cout << \"You selected option #\" << selection << '\\n' ; return 0 ; } \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u65e2\u907f\u514d\u4e86\u9b54\u9b3c\u6570\u5b57\uff0c\u4e5f\u907f\u514d\u4e86\u989d\u5916\u7684\u53d8\u91cf\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u6709\u4e00\u4ef6\u4e8b\u503c\u5f97\u8ba8\u8bba\uff0c\u90a3\u5c31\u662f selection \u53d8\u91cf\u5fc5\u987b\u5728do\u5757\u4e4b\u5916\u58f0\u660e\u3002\u5982\u679c\u5728do\u5757\u4e2d\u58f0\u660e\u4e86 selection \u53d8\u91cf\uff0c\u90a3\u4e48\u5f53do\u5757\u7ec8\u6b62\u65f6\uff0c\u5b83\u5c06\u88ab\u9500\u6bc1\uff0c\u8fd9\u53d1\u751f\u5728\u6761\u4ef6\u503c\u88ab\u6c42\u503c\u4e4b\u524d\u3002\u4f46\u662f\u6211\u4eec\u9700\u8981\u5728while\u6761\u4ef6\u8bed\u53e5\u4e2d\u58f0\u660e\u53d8\u91cf\u2014\u2014\u56e0\u6b64\uff0c selection \u53d8\u91cf\u5fc5\u987b\u5728do\u5757\u4e4b\u5916\u58f0\u660e(\u5373\u4f7f\u5b83\u6ca1\u6709\u5728\u51fd\u6570\u4f53\u540e\u9762\u4f7f\u7528)\u3002 \u5b9e\u9645\u4e0a\uff0cdo-while\u5faa\u73af\u5e76\u4e0d\u5e38\u7528\u3002\u5c06\u6761\u4ef6\u653e\u5728\u5faa\u73af\u7684\u5e95\u90e8\u4f1a\u6a21\u7cca\u5faa\u73af\u6761\u4ef6\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u56e0\u6b64\uff0c\u8bb8\u591a\u5f00\u53d1\u4eba\u5458\u5efa\u8bae\u5b8c\u5168\u907f\u514ddo-while\u5faa\u73af\u3002\u6211\u4eec\u5c06\u91c7\u53d6\u4e00\u79cd\u8f83\u4e3a\u6e29\u548c\u7684\u7acb\u573a\uff0c\u5982\u679c\u4e24\u79cd\u65b9\u6cd5\u90fd\u53ef\u4ee5\u4f7f\u7528\u65f6\uff0c\u4f7f\u7528while\u5faa\u73af\u800c\u4e0d\u662fdo-while\u5faa\u73af\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u7ed9\u51fa\u76f8\u540c\u7684\u9009\u62e9\u65f6\uff0c\u66f4\u503e\u5411\u4e8ewhile\u5faa\u73af\u800c\u4e0d\u662fdo-while\u5faa\u73af\u3002","title":"do-while \u8bed\u53e5"},{"location":"7-9-For-statements/","tags":["for"],"text":"C++\u4e2d\u6700\u5e38\u7528\u7684\u8bed\u53e5\u662f for \u8bed\u53e5\u3002\u5f53\u6211\u4eec\u6709\u4e00\u4e2a\u660e\u663e\u7684\u5faa\u73af\u53d8\u91cf\u65f6\uff0c\u9996\u9009\u4f7f\u7528for\u8bed\u53e5(\u4e5f\u79f0\u4e3afor\u5faa\u73af)\uff0c\u56e0\u4e3a\u5b83\u4f7f\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u800c\u7b80\u6d01\u5730\u5b9a\u4e49\u3001\u521d\u59cb\u5316\u3001\u6d4b\u8bd5\u548c\u66f4\u6539\u5faa\u73af\u53d8\u91cf\u7684\u503c\u3002 \u5728 C++11 \u4e2d\uff0c\u6709\u4e24\u79cd for \u5faa\u73af\uff0c\u6211\u4eec\u4f1a\u5728\u672c\u8282\u8bfe\u4ecb\u7ecd\u6700\u5178\u578b\u7684 for \u5faa\u73af\uff0c\u7136\u540e\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\uff08 11.13 - for-each \u5faa\u73af \uff09\u4ecb\u7ecd\u8f83\u65b0\u7684\u7684 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af ( 11.13 -- For-each loops ) \u3002 for \u5faa\u73af\u8bed\u6cd5\u7684\u62bd\u8c61\u5f62\u5f0f\u975e\u5e38\u7b80\u5355\uff1a 1 2 for ( init - statement ; condition ; end - expression ) statement ; \u7406\u89e3 for \u5faa\u73af\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u662f\u5c06\u5176\u8f6c\u6362\u6210\u4e00\u4e2a\u7b49\u4ef7\u7684 while \u5faa\u73af\uff1a 1 2 3 4 5 6 7 8 { // \u6ce8\u610f\u8fd9\u91cc\u7684\u8bed\u53e5\u5757 init - statement ; // \u7528\u4e8e\u5b9a\u4e49\u5faa\u73af\u4e2d\u4f7f\u7528\u7684\u53d8\u91cf while ( condition ) { statement ; end - expression ; // \u7528\u4e8e\u4fee\u6539\u5faa\u73af\u53d8\u91cf } } // \u5728\u5faa\u73af\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u5728\u8fd9\u91cc\u8d85\u51fa\u4e86\u4f5c\u7528\u57df for \u8bed\u53e5\u6c42\u503c for \u8bed\u53e5\u6c42\u503c\u5206\u4e09\u90e8\u5206\uff1a \u9996\u5148\u662f\u6267\u884c init-statement \uff0c\u4f46\u662f\u5b83\u4e4b\u540e\u5728\u5faa\u73af\u521d\u59cb\u5316\u65f6\u6267\u884c\u4e00\u6b21\u3002 init-statement \u901a\u5e38\u7528\u4e8e\u53d8\u91cf\u7684\u5b9a\u4e49\u548c\u521d\u59cb\u5316\u3002\u8fd9\u4e9b\u53d8\u91cf\u5747\u5177\u6709\u201c\u5faa\u73af\u4f5c\u7528\u57df\u201d\u2014\u2014\u8fd9\u53ea\u662f\u5757\u4f5c\u7528\u57df\u7684\u4e00\u79cd\u5f62\u5f0f\uff0c\u8fd9\u4e9b\u53d8\u91cf\u4ece\u5b9a\u4e49\u70b9\u4e00\u76f4\u5b58\u7eed\u5230\u5faa\u73af\u8bed\u53e5\u7ed3\u675f\u3002\u5728\u7b49\u4ef7\u7684while\u5faa\u73af\u4e2d\uff0c\u53ef\u4ee5\u770b\u5230 init-statement \u4f4d\u4e8e\u5305\u542b\u5faa\u73af\u7684\u5757\u4e2d\uff0c\u56e0\u6b64\u5f53\u5305\u542b\u5faa\u73af\u7684\u5757\u7ed3\u675f\u65f6\uff0c init-statement \u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u5c06\u8d85\u51fa\u4f5c\u7528\u57df\u3002 \u5176\u6b21, \u5bf9\u4e8e\u6bcf\u4e2a\u5faa\u73af\u8fed\u4ee3\uff0c\u90fd\u8981\u8ba1\u7b97\u6761\u4ef6\u3002\u5982\u679c\u8be5\u503c\u4e3a true \uff0c\u5219\u6267\u884c\u8bed\u53e5\u3002\u5982\u679c\u8be5\u503c\u4e3a false \uff0c\u5219\u5faa\u73af\u7ec8\u6b62\uff0c\u5e76\u7ee7\u7eed\u6267\u884c\u5faa\u73af\u4ee5\u5916\u7684\u4e0b\u4e00\u4e2a\u8bed\u53e5\u3002 \u6700\u540e\uff0c\u5f53 statement \u6267\u884c\u540e\uff0c\u4f1a\u5bf9 end-expression \u8fdb\u884c\u6c42\u503c\u3002\u901a\u5e38\uff0c\u8be5\u8868\u8fbe\u5f0f\u7528\u4e8e\u589e\u52a0\u6216\u51cf\u5c11 init-statement \u4e2d\u5b9a\u4e49\u7684\u5faa\u73af\u53d8\u91cf\u3002\u5728\u8ba1\u7b97\u7ed3\u675f\u8868\u8fbe\u5f0f\u4e4b\u540e\uff0c\u6267\u884c\u8fd4\u56de\u5230\u7b2c\u4e8c\u6b65(\u518d\u6b21\u8ba1\u7b97\u6761\u4ef6)\u3002 \u63a5\u4e0b\u6765\u770b\u4e00\u4e2a for \u5faa\u73af\u4f8b\u7a0b\uff0c\u4e86\u89e3\u5176\u5de5\u4f5c\u65b9\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { for ( int count { 1 }; count <= 10 ; ++ count ) std :: cout << count << ' ' ; std :: cout << '\\n' ; return 0 ; } \u9996\u5148\uff0c\u58f0\u660e\u4e00\u4e2a\u5faa\u73af\u53d8\u91cf count \u5e76\u5c06\u5176\u521d\u59cb\u5316\u4e3a1\u3002 \u7136\u540e\u5bf9 count <= 10 \u6c42\u503c\uff0c\u56e0\u4e3a count \u7b49\u4e8e1\uff0c\u6240\u4ee5\u6761\u4ef6\u6c42\u503c\u4e3a true \u3002\u56e0\u6b64\uff0c statement \u4f1a\u88ab\u6267\u884c\uff0c\u6253\u53701\u548c\u7a7a\u683c\u3002 \u6700\u540e\uff0c\u5bf9 ++count \u6c42\u503c\uff0c\u5c06 count \u9012\u589e\u4e3a 2 \u3002\u7136\u540e\u56de\u5230\u7b2c\u4e8c\u6b65\u3002 \u73b0\u5728\uff0c\u518d\u6b21\u5bf9 count <= 10 \u6c42\u503c\u3002\u7531\u4e8e count \u5df2\u7ecf\u662f 2 \u4e86\uff0c\u6240\u4ee5\u6c42\u503c\u7ed3\u679c\u4e3a true \uff0c\u518d\u6b21\u6267\u884c\u5faa\u73af\u3002\u8bed\u53e5\u8f93\u51fa 2 \u548c\u4e00\u4e2a\u7a7a\u683c\uff0c count \u88ab\u589e\u52a0\u5230 3 \u3002\u5faa\u73af\u7ee7\u7eed\u8fed\u4ee3\uff0c\u76f4\u5230 count \u88ab\u589e\u52a0\u5230 11 \uff0c\u6b64\u65f6 count <= 10 \u7684\u8ba1\u7b97\u503c\u4e3a false \uff0c\u5faa\u73af\u9000\u51fa\u3002 \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 1 2 3 4 5 6 7 8 9 10 \u5bf9\u4e8e\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u8ba9\u6211\u4eec\u5c06\u5176\u8f6c\u6362\u4e3a\u7b49\u4ef7\u7684 while \u5faa\u73af\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { { // the block here ensures block scope for count int count { 1 }; // our init-statement while ( count <= 10 ) // our condition { std :: cout << count << ' ' ; // our statement ++ count ; // our end-expression } } std :: cout << '\\n' ; } \u770b\u8d77\u6765\u6ca1\u90a3\u4e48\u7cdf\uff0c\u662f\u5427\uff1f\u6ce8\u610f\uff0c\u8fd9\u91cc\u9700\u8981\u4f7f\u7528\u5916\u5927\u62ec\u53f7\uff0c\u56e0\u4e3a\u5f53\u5faa\u73af\u7ed3\u675f\u65f6 count \u8981 \u79bb\u5f00\u4f5c\u7528\u57df \u3002 \u5bf9\u4e8e\u65b0\u7a0b\u5e8f\u5458\u6765\u8bf4\uff0c\u201cfor\u5faa\u73af\u201d\u53ef\u80fd\u5f88\u96be\u8bfb\u61c2\u2014\u2014\u7136\u800c\uff0c\u6709\u7ecf\u9a8c\u7684\u7a0b\u5e8f\u5458\u559c\u6b22\u5b83\u4eec\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u4e00\u79cd\u975e\u5e38\u7d27\u51d1\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u8ba1\u6570\u5668\u6765\u6267\u884c\u5faa\u73af\uff0c\u5173\u4e8e\u5faa\u73af\u53d8\u91cf\u3001\u5faa\u73af\u6761\u4ef6\u548c\u5faa\u73af\u8ba1\u6570\u4fee\u9970\u7b26\u7684\u6240\u6709\u5fc5\u8981\u4fe1\u606f\u90fd\u5728\u524d\u9762\u7ed9\u51fa\u4e86\u3002\u8fd9\u6709\u52a9\u4e8e\u51cf\u5c11\u9519\u8bef\u3002 \u66f4\u591a\u5faa\u73af\u6848\u4f8b \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 for \u5faa\u73af\u8ba1\u7b97\u6574\u6570\u6307\u6570\u7684\u51fd\u6570\u793a\u4f8b: 1 2 3 4 5 6 7 8 9 10 11 12 #include // for fixed-width integers // returns the value base ^ exponent -- watch out for overflow! std :: int64_t pow ( int base , int exponent ) { std :: int64_t total { 1 }; for ( int count { 0 }; count < exponent ; ++ count ) total *= base ; return total ; } This function returns the value base^exponent (base to the exponent power). This is a straightforward incrementing for loop , with count looping from 0 up to (but excluding) exponent . If exponent is 0 , the for loop will execute 0 times, and the function will return 1 . If exponent is 1 , the for loop will execute 1 time, and the function will return 1 * base . If exponent is 2 , the for loop will execute 2 times, and the function will return 1 * base * base . Although most for loops increment the loop variable by 1, we can decrement it as well: 1 2 3 4 5 6 7 8 9 10 11 #include int main () { for ( int count { 9 }; count >= 0 ; -- count ) std :: cout << count << ' ' ; std :: cout << '\\n' ; return 0 ; } COPY This prints the result: 1 9 8 7 6 5 4 3 2 1 0 Alternately, we can change the value of our loop variable by more than 1 with each iteration: 1 2 3 4 5 6 7 8 9 10 11 #include int main () { for ( int count { 0 }; count <= 10 ; count += 2 ) std :: cout << count << ' ' ; std :: cout << '\\n' ; return 0 ; } COPY This prints the result: 1 0 2 4 6 8 10 The perils of operator!= in for-loop conditions When writing a loop condition involving a value, we can often write the condition in many different ways. The following two loops execute identically: 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { for ( int i { 0 }; i < 10 ; ++ i ) // uses < std :: cout << i ; for ( int i { 0 }; i != 10 ; ++ i ) // uses != std :: cout << i ; return 0 ; } COPY So which should we prefer? The former is the better choice, as it will terminate even if i jumps over the value 10 , whereas the latter will not. The following example demonstrates this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { for ( int i { 0 }; i < 10 ; ++ i ) // uses <, still terminates { std :: cout << i ; if ( i == 9 ) ++ i ; // jump over value 10 } for ( int i { 0 }; i != 10 ; ++ i ) // uses !=, infinite loop { std :: cout << i ; if ( i == 9 ) ++ i ; // jump over value 10 } return 0 ; } COPY \u6700\u4f73\u5b9e\u8df5 Avoid operator!= when doing numeric comparisons in the for-loop condition. \u5dee\u4e00\u9519\u8bef One of the biggest problems that new programmers have with for loops (and other loops that utilize counters) are \u5dee\u4e00\u9519\u8bef \u3002 Off-by-one errorsoccur when the loop iterates one too many or one too few times to produce the desired result. Here\u2019s an example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { // oops, we used operator< instead of operator<= for ( int count { 1 }; count < 5 ; ++ count ) { std :: cout << count << ' ' ; } std :: cout << '\\n' ; return 0 ; } COPY This program is supposed to print 1 2 3 4 5 , but it only prints 1 2 3 4 because we used the wrong relational operator. Although the most common cause for these errors is using the wrong relational operator, they can sometimes occur by using pre-increment or pre-decrement instead of post-increment or post-decrement, or vice-versa. \u7701\u7565\u8868\u8fbe\u5f0f It is possible to write for loops that omit any or all of the statements or expressions. For example, in the following example, we\u2019ll omit the init-statement and end-expression, leaving only the condition: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int count { 0 }; for ( ; count < 10 ; ) // no init-statement or end-expression { std :: cout << count << ' ' ; ++ count ; } std :: cout << '\\n' ; return 0 ; } COPY This for loop produces the result: 1 0 1 2 3 4 5 6 7 8 9 Rather than having the for loop do the initialization and incrementing, we\u2019ve done it manually. We have done so purely for academic purposes in this example, but there are cases where not declaring a loop variable (because you already have one) or not incrementing it in the end-expression (because you\u2019re incrementing it some other way) is desired. Although you do not see it very often, it is worth noting that the following example produces an infinite loop: 1 2 for (;;) statement ; COPY The above example is equivalent to: 1 2 while ( true ) statement ; COPY This might be a little unexpected, as you\u2019d probably expect an omitted condition-expression to be treated as false . However, the C++ standard explicitly (and inconsistently) defines that an omitted condition-expression in a for loop should be treated as true . We recommend avoiding this form of the for loop altogether and using while(true) instead. \u6709\u591a\u4e2a\u8ba1\u6570\u7684\u5faa\u73af Although for loops typically iterate over only one variable, sometimes for loops need to work with multiple variables. To assist with this, the programmer can define multiple variables in the init-statement, and can make use of the comma operator to change the value of multiple variables in the end-expression: 1 2 3 4 5 6 7 8 9 #include int main () { for ( int x { 0 }, y { 9 }; x < 10 ; ++ x , -- y ) std :: cout << x << ' ' << y << '\\n' ; return 0 ; } COPY This loop defines and initializes two new variables: x and y . It iterates x over the range 0 to 9 , and after each iteration x is incremented and y is decremented. This program produces the result: 1 2 3 4 5 6 7 8 9 10 0 9 1 8 2 7 3 6 4 5 5 4 6 3 7 2 8 1 9 0 This is about the only place in C++ where defining multiple variables in the same statement, and use of the comma operator is considered an acceptable practice. \u6700\u4f73\u5b9e\u8df5 Defining multiple variables (in the init-statement) and using the comma operator (in the end-expression) is acceptable inside a for statement . \u5d4c\u5957\u5faa\u73af Like other types of loops, for loops can be nested inside other loops. In the following example, we\u2019re nesting a for loop inside another for loop : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { for ( char c { 'a' }; c <= 'e' ; ++ c ) // outer loop on letters { std :: cout << c ; // print our letter first for ( int i { 0 }; i < 3 ; ++ i ) // inner loop on all numbers std :: cout << i ; std :: cout << '\\n' ; } return 0 ; } COPY For each iteration of the outer loop, the inner loop runs in its entirety. Consequently, the output is: 1 2 3 4 5 a012 b012 c012 d012 e012 Here\u2019s some more detail on what\u2019s happening here. The outer loop runs first, and char c is initialized to 'a' . Then c <= 'e' is evaluated, which is true , so the loop body executes. Since c is set to 'a' , this first prints a . Next the inner loop executes entirely (which prints 0 , 1 , and 2 ). Then a newline is printed. Now the outer loop body is finished, so the outer loop returns to the top, c is incremented to 'b' , and the loop condition is re-evaluated. Since the loop condition is still true the next iteration of the outer loop begins. This prints b012\\n . And so on. \u5c0f\u7ed3 For statements are the most commonly used loop in the C++ language. Even though its syntax is typically a bit confusing to new programmers, you will see for loops so often that you will understand them in no time at all! For statements excel when you have a counter variable. If you do not have a counter, a while statement is probably a better choice. \u6700\u4f73\u5b9e\u8df5 Prefer for loops over while loops when there is an obvious loop variable. Prefer while loops over for loops when there is no obvious loop variable.","title":"7.9 - for \u8bed\u53e5"},{"location":"7-9-For-statements/#for","text":"for \u8bed\u53e5\u6c42\u503c\u5206\u4e09\u90e8\u5206\uff1a \u9996\u5148\u662f\u6267\u884c init-statement \uff0c\u4f46\u662f\u5b83\u4e4b\u540e\u5728\u5faa\u73af\u521d\u59cb\u5316\u65f6\u6267\u884c\u4e00\u6b21\u3002 init-statement \u901a\u5e38\u7528\u4e8e\u53d8\u91cf\u7684\u5b9a\u4e49\u548c\u521d\u59cb\u5316\u3002\u8fd9\u4e9b\u53d8\u91cf\u5747\u5177\u6709\u201c\u5faa\u73af\u4f5c\u7528\u57df\u201d\u2014\u2014\u8fd9\u53ea\u662f\u5757\u4f5c\u7528\u57df\u7684\u4e00\u79cd\u5f62\u5f0f\uff0c\u8fd9\u4e9b\u53d8\u91cf\u4ece\u5b9a\u4e49\u70b9\u4e00\u76f4\u5b58\u7eed\u5230\u5faa\u73af\u8bed\u53e5\u7ed3\u675f\u3002\u5728\u7b49\u4ef7\u7684while\u5faa\u73af\u4e2d\uff0c\u53ef\u4ee5\u770b\u5230 init-statement \u4f4d\u4e8e\u5305\u542b\u5faa\u73af\u7684\u5757\u4e2d\uff0c\u56e0\u6b64\u5f53\u5305\u542b\u5faa\u73af\u7684\u5757\u7ed3\u675f\u65f6\uff0c init-statement \u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u5c06\u8d85\u51fa\u4f5c\u7528\u57df\u3002 \u5176\u6b21, \u5bf9\u4e8e\u6bcf\u4e2a\u5faa\u73af\u8fed\u4ee3\uff0c\u90fd\u8981\u8ba1\u7b97\u6761\u4ef6\u3002\u5982\u679c\u8be5\u503c\u4e3a true \uff0c\u5219\u6267\u884c\u8bed\u53e5\u3002\u5982\u679c\u8be5\u503c\u4e3a false \uff0c\u5219\u5faa\u73af\u7ec8\u6b62\uff0c\u5e76\u7ee7\u7eed\u6267\u884c\u5faa\u73af\u4ee5\u5916\u7684\u4e0b\u4e00\u4e2a\u8bed\u53e5\u3002 \u6700\u540e\uff0c\u5f53 statement \u6267\u884c\u540e\uff0c\u4f1a\u5bf9 end-expression \u8fdb\u884c\u6c42\u503c\u3002\u901a\u5e38\uff0c\u8be5\u8868\u8fbe\u5f0f\u7528\u4e8e\u589e\u52a0\u6216\u51cf\u5c11 init-statement \u4e2d\u5b9a\u4e49\u7684\u5faa\u73af\u53d8\u91cf\u3002\u5728\u8ba1\u7b97\u7ed3\u675f\u8868\u8fbe\u5f0f\u4e4b\u540e\uff0c\u6267\u884c\u8fd4\u56de\u5230\u7b2c\u4e8c\u6b65(\u518d\u6b21\u8ba1\u7b97\u6761\u4ef6)\u3002 \u63a5\u4e0b\u6765\u770b\u4e00\u4e2a for \u5faa\u73af\u4f8b\u7a0b\uff0c\u4e86\u89e3\u5176\u5de5\u4f5c\u65b9\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { for ( int count { 1 }; count <= 10 ; ++ count ) std :: cout << count << ' ' ; std :: cout << '\\n' ; return 0 ; } \u9996\u5148\uff0c\u58f0\u660e\u4e00\u4e2a\u5faa\u73af\u53d8\u91cf count \u5e76\u5c06\u5176\u521d\u59cb\u5316\u4e3a1\u3002 \u7136\u540e\u5bf9 count <= 10 \u6c42\u503c\uff0c\u56e0\u4e3a count \u7b49\u4e8e1\uff0c\u6240\u4ee5\u6761\u4ef6\u6c42\u503c\u4e3a true \u3002\u56e0\u6b64\uff0c statement \u4f1a\u88ab\u6267\u884c\uff0c\u6253\u53701\u548c\u7a7a\u683c\u3002 \u6700\u540e\uff0c\u5bf9 ++count \u6c42\u503c\uff0c\u5c06 count \u9012\u589e\u4e3a 2 \u3002\u7136\u540e\u56de\u5230\u7b2c\u4e8c\u6b65\u3002 \u73b0\u5728\uff0c\u518d\u6b21\u5bf9 count <= 10 \u6c42\u503c\u3002\u7531\u4e8e count \u5df2\u7ecf\u662f 2 \u4e86\uff0c\u6240\u4ee5\u6c42\u503c\u7ed3\u679c\u4e3a true \uff0c\u518d\u6b21\u6267\u884c\u5faa\u73af\u3002\u8bed\u53e5\u8f93\u51fa 2 \u548c\u4e00\u4e2a\u7a7a\u683c\uff0c count \u88ab\u589e\u52a0\u5230 3 \u3002\u5faa\u73af\u7ee7\u7eed\u8fed\u4ee3\uff0c\u76f4\u5230 count \u88ab\u589e\u52a0\u5230 11 \uff0c\u6b64\u65f6 count <= 10 \u7684\u8ba1\u7b97\u503c\u4e3a false \uff0c\u5faa\u73af\u9000\u51fa\u3002 \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 1 2 3 4 5 6 7 8 9 10 \u5bf9\u4e8e\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u8ba9\u6211\u4eec\u5c06\u5176\u8f6c\u6362\u4e3a\u7b49\u4ef7\u7684 while \u5faa\u73af\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { { // the block here ensures block scope for count int count { 1 }; // our init-statement while ( count <= 10 ) // our condition { std :: cout << count << ' ' ; // our statement ++ count ; // our end-expression } } std :: cout << '\\n' ; } \u770b\u8d77\u6765\u6ca1\u90a3\u4e48\u7cdf\uff0c\u662f\u5427\uff1f\u6ce8\u610f\uff0c\u8fd9\u91cc\u9700\u8981\u4f7f\u7528\u5916\u5927\u62ec\u53f7\uff0c\u56e0\u4e3a\u5f53\u5faa\u73af\u7ed3\u675f\u65f6 count \u8981 \u79bb\u5f00\u4f5c\u7528\u57df \u3002 \u5bf9\u4e8e\u65b0\u7a0b\u5e8f\u5458\u6765\u8bf4\uff0c\u201cfor\u5faa\u73af\u201d\u53ef\u80fd\u5f88\u96be\u8bfb\u61c2\u2014\u2014\u7136\u800c\uff0c\u6709\u7ecf\u9a8c\u7684\u7a0b\u5e8f\u5458\u559c\u6b22\u5b83\u4eec\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u4e00\u79cd\u975e\u5e38\u7d27\u51d1\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u8ba1\u6570\u5668\u6765\u6267\u884c\u5faa\u73af\uff0c\u5173\u4e8e\u5faa\u73af\u53d8\u91cf\u3001\u5faa\u73af\u6761\u4ef6\u548c\u5faa\u73af\u8ba1\u6570\u4fee\u9970\u7b26\u7684\u6240\u6709\u5fc5\u8981\u4fe1\u606f\u90fd\u5728\u524d\u9762\u7ed9\u51fa\u4e86\u3002\u8fd9\u6709\u52a9\u4e8e\u51cf\u5c11\u9519\u8bef\u3002","title":"for \u8bed\u53e5\u6c42\u503c"},{"location":"7-9-For-statements/#_1","text":"\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 for \u5faa\u73af\u8ba1\u7b97\u6574\u6570\u6307\u6570\u7684\u51fd\u6570\u793a\u4f8b: 1 2 3 4 5 6 7 8 9 10 11 12 #include // for fixed-width integers // returns the value base ^ exponent -- watch out for overflow! std :: int64_t pow ( int base , int exponent ) { std :: int64_t total { 1 }; for ( int count { 0 }; count < exponent ; ++ count ) total *= base ; return total ; } This function returns the value base^exponent (base to the exponent power). This is a straightforward incrementing for loop , with count looping from 0 up to (but excluding) exponent . If exponent is 0 , the for loop will execute 0 times, and the function will return 1 . If exponent is 1 , the for loop will execute 1 time, and the function will return 1 * base . If exponent is 2 , the for loop will execute 2 times, and the function will return 1 * base * base . Although most for loops increment the loop variable by 1, we can decrement it as well: 1 2 3 4 5 6 7 8 9 10 11 #include int main () { for ( int count { 9 }; count >= 0 ; -- count ) std :: cout << count << ' ' ; std :: cout << '\\n' ; return 0 ; } COPY This prints the result: 1 9 8 7 6 5 4 3 2 1 0 Alternately, we can change the value of our loop variable by more than 1 with each iteration: 1 2 3 4 5 6 7 8 9 10 11 #include int main () { for ( int count { 0 }; count <= 10 ; count += 2 ) std :: cout << count << ' ' ; std :: cout << '\\n' ; return 0 ; } COPY This prints the result: 1 0 2 4 6 8 10 The perils of operator!= in for-loop conditions When writing a loop condition involving a value, we can often write the condition in many different ways. The following two loops execute identically: 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { for ( int i { 0 }; i < 10 ; ++ i ) // uses < std :: cout << i ; for ( int i { 0 }; i != 10 ; ++ i ) // uses != std :: cout << i ; return 0 ; } COPY So which should we prefer? The former is the better choice, as it will terminate even if i jumps over the value 10 , whereas the latter will not. The following example demonstrates this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { for ( int i { 0 }; i < 10 ; ++ i ) // uses <, still terminates { std :: cout << i ; if ( i == 9 ) ++ i ; // jump over value 10 } for ( int i { 0 }; i != 10 ; ++ i ) // uses !=, infinite loop { std :: cout << i ; if ( i == 9 ) ++ i ; // jump over value 10 } return 0 ; } COPY \u6700\u4f73\u5b9e\u8df5 Avoid operator!= when doing numeric comparisons in the for-loop condition.","title":"\u66f4\u591a\u5faa\u73af\u6848\u4f8b"},{"location":"7-9-For-statements/#_2","text":"One of the biggest problems that new programmers have with for loops (and other loops that utilize counters) are \u5dee\u4e00\u9519\u8bef \u3002 Off-by-one errorsoccur when the loop iterates one too many or one too few times to produce the desired result. Here\u2019s an example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { // oops, we used operator< instead of operator<= for ( int count { 1 }; count < 5 ; ++ count ) { std :: cout << count << ' ' ; } std :: cout << '\\n' ; return 0 ; } COPY This program is supposed to print 1 2 3 4 5 , but it only prints 1 2 3 4 because we used the wrong relational operator. Although the most common cause for these errors is using the wrong relational operator, they can sometimes occur by using pre-increment or pre-decrement instead of post-increment or post-decrement, or vice-versa.","title":"\u5dee\u4e00\u9519\u8bef"},{"location":"7-9-For-statements/#_3","text":"It is possible to write for loops that omit any or all of the statements or expressions. For example, in the following example, we\u2019ll omit the init-statement and end-expression, leaving only the condition: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int count { 0 }; for ( ; count < 10 ; ) // no init-statement or end-expression { std :: cout << count << ' ' ; ++ count ; } std :: cout << '\\n' ; return 0 ; } COPY This for loop produces the result: 1 0 1 2 3 4 5 6 7 8 9 Rather than having the for loop do the initialization and incrementing, we\u2019ve done it manually. We have done so purely for academic purposes in this example, but there are cases where not declaring a loop variable (because you already have one) or not incrementing it in the end-expression (because you\u2019re incrementing it some other way) is desired. Although you do not see it very often, it is worth noting that the following example produces an infinite loop: 1 2 for (;;) statement ; COPY The above example is equivalent to: 1 2 while ( true ) statement ; COPY This might be a little unexpected, as you\u2019d probably expect an omitted condition-expression to be treated as false . However, the C++ standard explicitly (and inconsistently) defines that an omitted condition-expression in a for loop should be treated as true . We recommend avoiding this form of the for loop altogether and using while(true) instead.","title":"\u7701\u7565\u8868\u8fbe\u5f0f"},{"location":"7-9-For-statements/#_4","text":"Although for loops typically iterate over only one variable, sometimes for loops need to work with multiple variables. To assist with this, the programmer can define multiple variables in the init-statement, and can make use of the comma operator to change the value of multiple variables in the end-expression: 1 2 3 4 5 6 7 8 9 #include int main () { for ( int x { 0 }, y { 9 }; x < 10 ; ++ x , -- y ) std :: cout << x << ' ' << y << '\\n' ; return 0 ; } COPY This loop defines and initializes two new variables: x and y . It iterates x over the range 0 to 9 , and after each iteration x is incremented and y is decremented. This program produces the result: 1 2 3 4 5 6 7 8 9 10 0 9 1 8 2 7 3 6 4 5 5 4 6 3 7 2 8 1 9 0 This is about the only place in C++ where defining multiple variables in the same statement, and use of the comma operator is considered an acceptable practice. \u6700\u4f73\u5b9e\u8df5 Defining multiple variables (in the init-statement) and using the comma operator (in the end-expression) is acceptable inside a for statement .","title":"\u6709\u591a\u4e2a\u8ba1\u6570\u7684\u5faa\u73af"},{"location":"7-9-For-statements/#_5","text":"Like other types of loops, for loops can be nested inside other loops. In the following example, we\u2019re nesting a for loop inside another for loop : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { for ( char c { 'a' }; c <= 'e' ; ++ c ) // outer loop on letters { std :: cout << c ; // print our letter first for ( int i { 0 }; i < 3 ; ++ i ) // inner loop on all numbers std :: cout << i ; std :: cout << '\\n' ; } return 0 ; } COPY For each iteration of the outer loop, the inner loop runs in its entirety. Consequently, the output is: 1 2 3 4 5 a012 b012 c012 d012 e012 Here\u2019s some more detail on what\u2019s happening here. The outer loop runs first, and char c is initialized to 'a' . Then c <= 'e' is evaluated, which is true , so the loop body executes. Since c is set to 'a' , this first prints a . Next the inner loop executes entirely (which prints 0 , 1 , and 2 ). Then a newline is printed. Now the outer loop body is finished, so the outer loop returns to the top, c is incremented to 'b' , and the loop condition is re-evaluated. Since the loop condition is still true the next iteration of the outer loop begins. This prints b012\\n . And so on.","title":"\u5d4c\u5957\u5faa\u73af"},{"location":"7-9-For-statements/#_6","text":"For statements are the most commonly used loop in the C++ language. Even though its syntax is typically a bit confusing to new programmers, you will see for loops so often that you will understand them in no time at all! For statements excel when you have a counter variable. If you do not have a counter, a while statement is probably a better choice. \u6700\u4f73\u5b9e\u8df5 Prefer for loops over while loops when there is an obvious loop variable. Prefer while loops over for loops when there is no obvious loop variable.","title":"\u5c0f\u7ed3"},{"location":"7-x-Chapter-7-summary-and-quiz/","tags":["summary"],"text":"The specific sequence of statements that the CPU executes in a program is called the program\u2019s execution path. A straight-line program takes the same path every time it is run. Control flow statements (also called Flow control statements) allow the programmer to change the normal path of execution. When a control flow statement causes the program to begin executing some non-sequential instruction sequence, this is called a branch. A conditional statement is a statement that specifies whether some associated statement(s) should be executed or not. If statements allow us to execute an associated statement based on whether some condition is true . Else statements execute if the associated condition is false . You can chain together multiple if and else statements. A dangling else occurs when it is ambiguous which if statement an else statement is connected to. Dangling else statements are matched up with the last unmatched if statement in the same block. Thus, we trivially avoid dangling else statements by ensuring the body of an if statement is placed in a block. A null statement is a statement that consists of just a semicolon. It does nothing, and is used when the language requires a statement to exist but the programmer does not need the statement to do anything. Switch statements provide a cleaner and faster method for selecting between a number of matching items. Switch statements only work with integral types. Case labels are used to identify the values for the evaluated condition to match. The statements beneath a default label are executed if no matching case label can be found. When execution flows from a statement underneath a label into statements underneath a subsequent label, this is called fallthrough. A break statement (or return statement ) can be used to prevent fallthrough. The fallthrough attribute can be used to document intentional fallthrough. Goto statements allow the program to jump to somewhere else in the code, either forward or backwards. These should generally be avoided, as they can create spaghetti code, which occurs when a program has a path of execution that resembles a bowl of spaghetti. While loops allow the program to loop as long as a given condition evaluates to true . The condition is evaluated before the loop executes. An infinite loop is a loop that has a condition that always evaluates to true . These loops will loop forever unless another control flow statement is used to stop them. A loop variable (also called a counter) is an integer variable used to count how many times a loop has executed. Each execution of a loop is called an iteration. Do while loops are similar to while loops, but the condition is evaluated after the loop executes instead of before. For loops are the most used loop, and are ideal when you need to loop a specific number of times. An off-by-one error occurs when the loop iterates one too many or one too few times. Break statements allow us to break out of a switch, while, do while, or for loop (also range-based for loops , which we haven\u2019t covered yet). Continue statements allow us to move immediately to the next loop iteration. Halts allow us to terminate our program. Normal termination means the program has exited in an expected way (and the status code will indicate whether it succeeded or not). std::exit() is automatically called at the end of main , or it can be called explicitly to terminate the program. It does some cleanup, but does not cleanup any local variables, or unwind the call stack. Abnormal termination occurs when the program encountered some kind of unexpected error and had to be shut down. std::abort can be called for an abnormal termination. Scope creep occurs when a project\u2019s capabilities grow beyond what was originally intended at the start of the project or project phase. Software verification is the process of testing whether or not the software works as expected in all cases. A unit test is a test designed to test a small portion of the code (typically a function or call) in isolation to ensure a particular behavior occurs as expected. Unit test frameworks can help you organize your unit tests. Integration testing tests the integration of a bunch of units together to ensure they work properly. Code coverage refers to how much of the source code is executed while testing. Statement coverage refers to the percentage of statements in a program that have been exercised by testing routines. Branch coverage refers to the percentage of branches that have been executed by testing routines. Loop coverage (also called the 0, 1, 2 test) means that if you have a loop, you should ensure it works properly when it iterates 0 times, 1 time, and 2 times. The happy path is the path of execution that occurs when there are no errors encountered. A sad path is one where an error or failure state occurs. A non-recoverable error (also called a fatal error) is an error that is severe enough that the program can\u2019t continue running. A program that handles error cases well is robust. A buffer is a piece of memory set aside for storing data temporarily while it is moved from one place to another. The process of checking whether user input conforms to what the program is expecting is called input validation. std::cerr is an output stream (like std::cout ) designed to be used for error messages. A precondition is any condition that must always be true prior to the execution of some segment of code. An invariant is a condition that must be true while some component is executing. A postcondition is any condition that must always be true after the execution of some code. An assertion is an expression that will be true unless there is a bug in the program. In C++, runtime assertions are typically implemented using the assert preprocessor macro. Assertions are usually turned off in non-debug code. A static_assert is an assertion that is evaluated at compile-time. Assertions should be used to document cases that should be logically impossible. Error handling should be used to handle cases that are possible. An algorithm is a finite sequence of instructions that can be followed to solve some problem or produce some useful result. An algorithm is considered to be stateful if it retains some information across calls. Conversely, a stateless algorithm does not store any information (and must be given all the information it needs to work with when it is called). When applied to algorithms, the term state refers to the current values held in stateful variables. An algorithm is considered deterministic if for a given input (the value provided for start ) it will always produce the same output sequence. A pseudo-random number generator (PRNG) is an algorithm that generates a sequence of numbers whose properties simulate a sequence of random numbers. When a PRNG is instantiated, an initial value (or set of values) called a random seed (or seed for short) can be provided to initialize the state of the PRNG. When a PRNG has been initialized with a seed, we say it has been seeded. The size of the seed value can be smaller than the size of the state of the PRNG. When this happens, we say the PRNG has been underseeded. The length of the sequence before a PRNG begins to repeat itself is known as the period. A random number distribution converts the output of a PRNG into some other distribution of numbers. A uniform distribution is a random number distribution that produces outputs between two numbers X and Y (inclusive) with equal probability.","title":"7.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406"},{"location":"8-1-Implicit-type-conversion-coercion/","tags":["type conversion","conversions"],"text":"Key Takeaway \u7c7b\u578b\u8f6c\u6362\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\u89e6\u53d1\uff1a\u9690\u5f0f\u5730\uff08\u7f16\u8bd1\u5668\u9700\u8981\u8fd9\u4e48\u505a\uff09\u6216\u662f\u663e\u5f0f\u5730\uff08\u7531\u7a0b\u5e8f\u5458\u53d1\u8d77\uff09 C++ \u4e2d\u7684\u5927\u90e8\u5206\u7c7b\u578b\u8f6c\u6362\u90fd\u662f\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u3002\u5b83\u4f1a\u53d1\u751f\u5728\u5982\u4e0b\u65f6\u673a\uff1a \u5f53\u4f7f\u7528\u4e0d\u540c\u7c7b\u578b\u7684\u53d8\u91cf\u5bf9\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\u7684\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u6216\u8d4b\u503c\u7684\u65f6\u5019 \u5f53\u51fd\u6570\u7684\u5b9e\u9645\u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u540c\u4e8e\u51fd\u6570\u5b9a\u4e49\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u65f6 \u5f53\u5bf9\u4e0d\u540c\u7c7b\u578b\u7684\u64cd\u4f5c\u6570\u4f7f\u7528\u67d0\u4e9b\u4e8c\u5143\u64cd\u4f5c\u7b26\u65f6 \u5f53\u5728 if \u8bed\u53e5\u4e2d\u4f7f\u7528\u975e\u5e03\u5c14\u503c\u65f6 \u5f53\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u5b9e\u53c2\u4e0e\u51fd\u6570\u5f62\u53c2\u7c7b\u578b\u4e0d\u540c\u65f6 C++ \u6807\u51c6\u5b9a\u4e49\u4e86\u4e0d\u540c\u7684\u57fa\u672c\u7c7b\u578b\u7684\u8f6c\u6362\u65b9\u5f0f\uff0c\u5305\u62ec\uff1a \u6570\u503c\u63d0\u5347(\u53c2\u8003\uff1a 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 )\uff1b \u6570\u503c\u8f6c\u6362(\u53c2\u8003\uff1a 8.3 - \u6570\u503c\u8f6c\u6362 )\uff1b \u7b97\u6570\u8f6c\u6362(\u53c2\u8003\uff1a 8.4 - \u7b97\u6570\u8f6c\u6362 )\uff1b \u5176\u4ed6\u8f6c\u6362 (\u5305\u62ec\u591a\u79cd\u6307\u9488\u548c\u5f15\u7528\u7684\u8f6c\u6362) \u7c7b\u578b\u8f6c\u6362\u7b80\u4ecb \u5bf9\u8c61\u7684\u503c\u662f\u4ee5\u4e00\u7cfb\u5217\u7684 \u6bd4\u7279 \u5b58\u653e\u7684\uff0c\u800c\u6570\u636e\u7c7b\u578b\u5219\u544a\u8bc9\u7f16\u8bd1\u5668\u5e94\u8be5\u5982\u4f55\u5c06\u8fd9\u4e00\u7cfb\u5217\u6bd4\u7279\u89e3\u6790\u6210\u6709\u610f\u4e49\u7684\u503c\u3002\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u4ee5\u4e0d\u540c\u7684\u65b9\u5f0f\u8868\u793a \u76f8\u540c \u7684\u503c\u3002\u4f8b\u5982\uff0c\u6574\u578b\u503c 3 \u53ef\u80fd\u88ab\u5b58\u653e\u4e3a\u4e8c\u8fdb\u5236 0000 0000 0000 0000 0000 0000 0000 0011 \uff0c\u800c\u6d6e\u70b9\u6570 3.0 \u5219\u53ef\u80fd\u88ab\u5b58\u653e\u4e3a\u4e8c\u8fdb\u5236 0100 0000 0100 0000 0000 0000 0000 0000 \u3002 \u90a3\u4e48\u5f53\u6211\u4eec\u8fd9\u6837\u505a\u7684\u65f6\u5019\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462? 1 float f { 3 }; // initialize floating point variable with int 3 \u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u4e0d\u80fd\u4ec5\u4ec5\u628a\u8868\u793a int \u7c7b\u578b\u503c 3 \u7684\u6bd4\u7279\u62f7\u8d1d\u5230\u5185\u5b58\u4e2d\u8868\u793a float \u53d8\u91cf f \u3002\u76f8\u53cd\uff0c\u5b83\u5fc5\u987b\u5c06\u6574\u578b\u503c 3 \u8f6c\u6362\u4e3a\u7b49\u4ef7\u7684\u6d6e\u70b9\u6570\uff0c\u7136\u540e\u624d\u80fd\u5b58\u653e\u5230\u5185\u5b58\u4e2d\u8868\u793a\u53d8\u91cf f \u3002 \u5c06\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u7684\u8fc7\u7a0b\uff0c\u79f0\u4e3a\u7c7b\u578b\u8f6c\u6362\u3002 \u7c7b\u578b\u8f6c\u6362\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\u89e6\u53d1\uff1a\u9690\u5f0f\u5730\uff08\u7f16\u8bd1\u5668\u9700\u8981\u8fd9\u4e48\u505a\uff09\u6216\u662f\u663e\u5f0f\u5730\uff08\u7531\u7a0b\u5e8f\u5458\u53d1\u8d77\uff09\u3002\u6211\u4eec\u4f1a\u5728\u672c\u8282\u8bfe\u4ecb\u7ecd \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362(implicit type conversion) \uff0c\u7136\u540e\u5728\u4e0b\u8282\u8bfe 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u4e2d\u4ecb\u7ecd \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362(Explicit type conversion) \u3002 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\uff08\u4e5f\u53eb\u505a\u81ea\u52a8\u7c7b\u578b\u8f6c\u6362\uff09\u7531\u7f16\u8bd1\u5668\u5728\u9700\u8981\u65f6\uff08\u9700\u8981\u67d0\u4e2a\u53d8\u91cf\u7c7b\u578b\u4f46\u5374\u7ed9\u5b9a\u4e86\u53e6\u5916\u7684\u7c7b\u578b\uff09\u81ea\u52a8\u5b8c\u6210\u3002C++ \u4e2d\u7684\u5927\u90e8\u5206\u7c7b\u578b\u8f6c\u6362\u90fd\u662f\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u3002\u5b83\u4f1a\u53d1\u751f\u5728\u5982\u4e0b\u65f6\u673a\uff1a \u5f53\u4f7f\u7528\u4e0d\u540c\u7c7b\u578b\u7684\u53d8\u91cf\u5bf9\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\u7684\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u6216\u8d4b\u503c\u7684\u65f6\u5019\uff1a 1 2 double d { 3 }; // int 3 \u9690\u5f0f\u8f6c\u6362\u4e3a double \u7c7b\u578b d = 6 ; // int 6 \u9690\u5f0f\u8f6c\u6362\u4e3a double \u7c7b\u578b \u5f53\u51fd\u6570\u7684\u5b9e\u9645\u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u540c\u4e8e\u51fd\u6570\u5b9a\u4e49\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u65f6\uff1a 1 2 3 4 float doSomething () { return 3.0 ; // double 3.0 \u9690\u5f0f\u8f6c\u6362\u4e3a float \u7c7b\u578b } \u5f53\u5bf9\u4e0d\u540c\u7c7b\u578b\u7684\u64cd\u4f5c\u6570\u4f7f\u7528\u67d0\u4e9b\u4e8c\u5143\u64cd\u4f5c\u7b26\u65f6\uff1a 1 double division { 4.0 / 3 }; // int 3 \u9690\u5f0f\u8f6c\u6362\u4e3a double \u7c7b\u578b \u5f53\u5728 if \u8bed\u53e5\u4e2d\u4f7f\u7528\u975e\u5e03\u5c14\u503c\u65f6\uff1a 1 2 3 if ( 5 ) // int 5 \u9690\u5f0f\u8f6c\u6362\u4e3a bool \u7c7b\u578b { } \u5f53\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u5b9e\u53c2\u4e0e\u51fd\u6570\u5f62\u53c2\u7c7b\u578b\u4e0d\u540c\u65f6\uff1a 1 2 3 4 5 void doSomething ( long l ) { } doSomething ( 3 ); // int 3 \u9690\u5f0f\u8f6c\u6362\u4e3a long \u7c7b\u578b \u5f53\u7c7b\u578b\u8f6c\u6362\u89e6\u53d1\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48 \u5f53\u7c7b\u578b\u8f6c\u6362\u89e6\u53d1\u65f6(\u65e0\u8bba\u662f\u9690\u5f0f\u8fd8\u662f\u663e\u5f0f)\uff0c\u7f16\u8bd1\u5668\u9700\u8981\u786e\u5b9a\u662f\u5426\u53ef\u4ee5\u5c06\u503c\u4ece\u5f53\u524d\u7c7b\u578b\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u7c7b\u578b\u3002\u5982\u679c\u53ef\u4ee5\u627e\u5230\u6709\u6548\u7684\u8f6c\u6362\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c06\u751f\u6210\u6240\u9700\u7c7b\u578b\u7684\u65b0\u503c\u3002\u6ce8\u610f\uff0c\u7c7b\u578b\u8f6c\u6362\u4e0d\u4f1a\u6539\u53d8\u88ab\u8f6c\u6362\u7684\u503c\u6216\u5bf9\u8c61\u7684\u503c\u6216\u7c7b\u578b\u3002 \u5982\u679c\u7f16\u8bd1\u5668\u65e0\u6cd5\u627e\u5230\u53ef\u7528\u7684\u8f6c\u6362\uff0c\u5219\u4f1a\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002\u7c7b\u578b\u8f6c\u6362\u5931\u8d25\u7684\u539f\u56e0\u6709\u5f88\u591a\u3002\u4f8b\u5982\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4e0d\u77e5\u9053\u5982\u4f55\u5728\u539f\u59cb\u7c7b\u578b\u548c\u6240\u9700\u7c7b\u578b\u4e4b\u95f4\u8f6c\u6362\u503c\u3002\u6216\u8005\uff0c\u8bed\u53e5\u53ef\u80fd\u4e0d\u5141\u8bb8\u67d0\u4e9b\u7c7b\u578b\u7684\u8f6c\u6362\u3002\u4f8b\u5982: 1 int x { 3.5 }; // \u62ec\u53f7\u7c7b\u578b\u8f6c\u6362\u4e0d\u5141\u8bb8\u7c7b\u578b\u8f6c\u6362\u4ee5\u907f\u514d\u6570\u636e\u4e22\u5931 \u5373\u4f7f\u7f16\u8bd1\u5668\u77e5\u9053\u5982\u4f55\u5c06 double \u7c7b\u578b\u8f6c\u6362\u4e3a int \u7c7b\u578b\uff0c\u4f46\u5728\u4f7f\u7528\u62ec\u53f7\u521d\u59cb\u5316\u65f6\uff0c\u8fd9\u6837\u7684\u8f6c\u6362\u662f\u4e0d\u5141\u8bb8\u7684\u3002 \u8fd8\u6709\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u65e0\u6cd5\u786e\u5b9a\u51e0\u79cd\u53ef\u80fd\u7684\u7c7b\u578b\u8f6c\u6362\u4e2d\u54ea\u4e00\u79cd\u662f\u6700\u9002\u5408\u4f7f\u7528\u7684\u3002\u6211\u4eec\u4f1a\u5728 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u5339\u914d\u6b67\u4e49 \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u90a3\u4e48\u7f16\u8bd1\u5668\u5b9e\u9645\u4e0a\u662f\u5982\u4f55\u786e\u5b9a\u5b83\u662f\u5426\u53ef\u4ee5\u5c06\u4e00\u4e2a\u503c\u4ece\u4e00\u79cd\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u7c7b\u578b\u7684\u5462? \u8f6c\u6362\u6807\u51c6 C++\u8bed\u8a00\u6807\u51c6\u5b9a\u4e49\u4e86\u4e0d\u540c\u7684\u57fa\u672c\u7c7b\u578b(\u4e5f\u5305\u62ec\u4e00\u4e9b\u590d\u5408\u7c7b\u578b)\u5e94\u5f53\u5982\u4f55\u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b\u3002\u8fd9\u4e9b\u8f6c\u6362\u89c4\u5219\u79f0\u4e3a\u8f6c\u6362\u6807\u51c6\u3002 \u8f6c\u6362\u6807\u51c6\u5927\u81f4\u53ef\u5206\u4e3a\u56db\u7c7b\uff0c\u6bcf\u4e00\u7c7b\u6db5\u76d6\u4e0d\u540c\u7684\u8f6c\u6362\u65b9\u5f0f: \u6570\u503c\u63d0\u5347(\u53c2\u8003\uff1a 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 )\uff1b \u6570\u503c\u8f6c\u6362(\u53c2\u8003\uff1a 8.3 - \u6570\u503c\u8f6c\u6362 )\uff1b \u7b97\u6570\u8f6c\u6362(\u53c2\u8003\uff1a 8.4 - \u7b97\u6570\u8f6c\u6362 )\uff1b \u5176\u4ed6\u8f6c\u6362 (\u5305\u62ec\u591a\u79cd\u6307\u9488\u548c\u5f15\u7528\u7684\u8f6c\u6362) \u5f53\u9700\u8981\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u67e5\u770b\u662f\u5426\u6709\u6807\u51c6\u8f6c\u6362\uff0c\u53ef\u4ee5\u7528\u6765\u5c06\u503c\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u7c7b\u578b\u3002\u7f16\u8bd1\u5668\u53ef\u4ee5\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u5e94\u7528 0 \u4e2a\u30011 \u4e2a\u6216\u591a\u4e2a\u6807\u51c6\u8f6c\u6362\u3002 \u9898\u5916\u8bdd \u5982\u4f55\u5728\u4e0d\u8fdb\u884c\u5b9e\u9645\u7c7b\u578b\u8f6c\u6362\u7684\u540c\u65f6\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362? \u4f8b\u5982\uff0c\u5728 int \u548c long \u90fd\u5177\u6709\u76f8\u540c\u5927\u5c0f\u548c\u8303\u56f4\u7684\u67b6\u6784\u4e2d\uff0c\u4f7f\u7528\u76f8\u540c\u7684\u4f4d\u5e8f\u5217\u6765\u8868\u793a\u8fd9\u4e24\u79cd\u7c7b\u578b\u7684\u503c\u3002 \u56e0\u6b64\uff0c\u5728\u8fd9\u4e9b\u7c7b\u578b\u4e4b\u95f4\u8f6c\u6362\u503c\u4e0d\u9700\u8981\u5b9e\u9645\u7684\u8f6c\u6362\u2014\u2014\u503c\u53ef\u4ee5\u7b80\u5355\u5730\u590d\u5236\u3002 \u63cf\u8ff0\u7c7b\u578b\u8f6c\u6362\u5982\u4f55\u5de5\u4f5c\u7684\u4e00\u6574\u5957\u89c4\u5219\u65e2\u5197\u957f\u53c8\u590d\u6742\uff0c\u800c\u4e14\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7c7b\u578b\u8f6c\u6362\u201d\u5c31\u662f\u80fd\u591f\u5de5\u4f5c\u201c\u3002\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u4f60\u9700\u8981\u4e86\u89e3\u7684\u5173\u4e8e\u7c7b\u578b\u8f6c\u6362\u7684\u6700\u91cd\u8981\u7684\u4e8b\u60c5\u3002\u5982\u679c\u4f60\u9700\u8981\u66f4\u8be6\u7ec6\u7684\u8d44\u6599\uff0c\u53ef\u4ee5\u53c2\u8003 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u6280\u672f\u53c2\u8003\u6587\u6863 \u3002","title":"8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362"},{"location":"8-1-Implicit-type-conversion-coercion/#_1","text":"\u5bf9\u8c61\u7684\u503c\u662f\u4ee5\u4e00\u7cfb\u5217\u7684 \u6bd4\u7279 \u5b58\u653e\u7684\uff0c\u800c\u6570\u636e\u7c7b\u578b\u5219\u544a\u8bc9\u7f16\u8bd1\u5668\u5e94\u8be5\u5982\u4f55\u5c06\u8fd9\u4e00\u7cfb\u5217\u6bd4\u7279\u89e3\u6790\u6210\u6709\u610f\u4e49\u7684\u503c\u3002\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u4ee5\u4e0d\u540c\u7684\u65b9\u5f0f\u8868\u793a \u76f8\u540c \u7684\u503c\u3002\u4f8b\u5982\uff0c\u6574\u578b\u503c 3 \u53ef\u80fd\u88ab\u5b58\u653e\u4e3a\u4e8c\u8fdb\u5236 0000 0000 0000 0000 0000 0000 0000 0011 \uff0c\u800c\u6d6e\u70b9\u6570 3.0 \u5219\u53ef\u80fd\u88ab\u5b58\u653e\u4e3a\u4e8c\u8fdb\u5236 0100 0000 0100 0000 0000 0000 0000 0000 \u3002 \u90a3\u4e48\u5f53\u6211\u4eec\u8fd9\u6837\u505a\u7684\u65f6\u5019\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462? 1 float f { 3 }; // initialize floating point variable with int 3 \u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u4e0d\u80fd\u4ec5\u4ec5\u628a\u8868\u793a int \u7c7b\u578b\u503c 3 \u7684\u6bd4\u7279\u62f7\u8d1d\u5230\u5185\u5b58\u4e2d\u8868\u793a float \u53d8\u91cf f \u3002\u76f8\u53cd\uff0c\u5b83\u5fc5\u987b\u5c06\u6574\u578b\u503c 3 \u8f6c\u6362\u4e3a\u7b49\u4ef7\u7684\u6d6e\u70b9\u6570\uff0c\u7136\u540e\u624d\u80fd\u5b58\u653e\u5230\u5185\u5b58\u4e2d\u8868\u793a\u53d8\u91cf f \u3002 \u5c06\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u7684\u8fc7\u7a0b\uff0c\u79f0\u4e3a\u7c7b\u578b\u8f6c\u6362\u3002 \u7c7b\u578b\u8f6c\u6362\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\u89e6\u53d1\uff1a\u9690\u5f0f\u5730\uff08\u7f16\u8bd1\u5668\u9700\u8981\u8fd9\u4e48\u505a\uff09\u6216\u662f\u663e\u5f0f\u5730\uff08\u7531\u7a0b\u5e8f\u5458\u53d1\u8d77\uff09\u3002\u6211\u4eec\u4f1a\u5728\u672c\u8282\u8bfe\u4ecb\u7ecd \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362(implicit type conversion) \uff0c\u7136\u540e\u5728\u4e0b\u8282\u8bfe 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u4e2d\u4ecb\u7ecd \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362(Explicit type conversion) \u3002","title":"\u7c7b\u578b\u8f6c\u6362\u7b80\u4ecb"},{"location":"8-1-Implicit-type-conversion-coercion/#_2","text":"\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\uff08\u4e5f\u53eb\u505a\u81ea\u52a8\u7c7b\u578b\u8f6c\u6362\uff09\u7531\u7f16\u8bd1\u5668\u5728\u9700\u8981\u65f6\uff08\u9700\u8981\u67d0\u4e2a\u53d8\u91cf\u7c7b\u578b\u4f46\u5374\u7ed9\u5b9a\u4e86\u53e6\u5916\u7684\u7c7b\u578b\uff09\u81ea\u52a8\u5b8c\u6210\u3002C++ \u4e2d\u7684\u5927\u90e8\u5206\u7c7b\u578b\u8f6c\u6362\u90fd\u662f\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u3002\u5b83\u4f1a\u53d1\u751f\u5728\u5982\u4e0b\u65f6\u673a\uff1a \u5f53\u4f7f\u7528\u4e0d\u540c\u7c7b\u578b\u7684\u53d8\u91cf\u5bf9\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\u7684\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u6216\u8d4b\u503c\u7684\u65f6\u5019\uff1a 1 2 double d { 3 }; // int 3 \u9690\u5f0f\u8f6c\u6362\u4e3a double \u7c7b\u578b d = 6 ; // int 6 \u9690\u5f0f\u8f6c\u6362\u4e3a double \u7c7b\u578b \u5f53\u51fd\u6570\u7684\u5b9e\u9645\u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u540c\u4e8e\u51fd\u6570\u5b9a\u4e49\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u65f6\uff1a 1 2 3 4 float doSomething () { return 3.0 ; // double 3.0 \u9690\u5f0f\u8f6c\u6362\u4e3a float \u7c7b\u578b } \u5f53\u5bf9\u4e0d\u540c\u7c7b\u578b\u7684\u64cd\u4f5c\u6570\u4f7f\u7528\u67d0\u4e9b\u4e8c\u5143\u64cd\u4f5c\u7b26\u65f6\uff1a 1 double division { 4.0 / 3 }; // int 3 \u9690\u5f0f\u8f6c\u6362\u4e3a double \u7c7b\u578b \u5f53\u5728 if \u8bed\u53e5\u4e2d\u4f7f\u7528\u975e\u5e03\u5c14\u503c\u65f6\uff1a 1 2 3 if ( 5 ) // int 5 \u9690\u5f0f\u8f6c\u6362\u4e3a bool \u7c7b\u578b { } \u5f53\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u5b9e\u53c2\u4e0e\u51fd\u6570\u5f62\u53c2\u7c7b\u578b\u4e0d\u540c\u65f6\uff1a 1 2 3 4 5 void doSomething ( long l ) { } doSomething ( 3 ); // int 3 \u9690\u5f0f\u8f6c\u6362\u4e3a long \u7c7b\u578b","title":"\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362"},{"location":"8-1-Implicit-type-conversion-coercion/#_3","text":"\u5f53\u7c7b\u578b\u8f6c\u6362\u89e6\u53d1\u65f6(\u65e0\u8bba\u662f\u9690\u5f0f\u8fd8\u662f\u663e\u5f0f)\uff0c\u7f16\u8bd1\u5668\u9700\u8981\u786e\u5b9a\u662f\u5426\u53ef\u4ee5\u5c06\u503c\u4ece\u5f53\u524d\u7c7b\u578b\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u7c7b\u578b\u3002\u5982\u679c\u53ef\u4ee5\u627e\u5230\u6709\u6548\u7684\u8f6c\u6362\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c06\u751f\u6210\u6240\u9700\u7c7b\u578b\u7684\u65b0\u503c\u3002\u6ce8\u610f\uff0c\u7c7b\u578b\u8f6c\u6362\u4e0d\u4f1a\u6539\u53d8\u88ab\u8f6c\u6362\u7684\u503c\u6216\u5bf9\u8c61\u7684\u503c\u6216\u7c7b\u578b\u3002 \u5982\u679c\u7f16\u8bd1\u5668\u65e0\u6cd5\u627e\u5230\u53ef\u7528\u7684\u8f6c\u6362\uff0c\u5219\u4f1a\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002\u7c7b\u578b\u8f6c\u6362\u5931\u8d25\u7684\u539f\u56e0\u6709\u5f88\u591a\u3002\u4f8b\u5982\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4e0d\u77e5\u9053\u5982\u4f55\u5728\u539f\u59cb\u7c7b\u578b\u548c\u6240\u9700\u7c7b\u578b\u4e4b\u95f4\u8f6c\u6362\u503c\u3002\u6216\u8005\uff0c\u8bed\u53e5\u53ef\u80fd\u4e0d\u5141\u8bb8\u67d0\u4e9b\u7c7b\u578b\u7684\u8f6c\u6362\u3002\u4f8b\u5982: 1 int x { 3.5 }; // \u62ec\u53f7\u7c7b\u578b\u8f6c\u6362\u4e0d\u5141\u8bb8\u7c7b\u578b\u8f6c\u6362\u4ee5\u907f\u514d\u6570\u636e\u4e22\u5931 \u5373\u4f7f\u7f16\u8bd1\u5668\u77e5\u9053\u5982\u4f55\u5c06 double \u7c7b\u578b\u8f6c\u6362\u4e3a int \u7c7b\u578b\uff0c\u4f46\u5728\u4f7f\u7528\u62ec\u53f7\u521d\u59cb\u5316\u65f6\uff0c\u8fd9\u6837\u7684\u8f6c\u6362\u662f\u4e0d\u5141\u8bb8\u7684\u3002 \u8fd8\u6709\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u65e0\u6cd5\u786e\u5b9a\u51e0\u79cd\u53ef\u80fd\u7684\u7c7b\u578b\u8f6c\u6362\u4e2d\u54ea\u4e00\u79cd\u662f\u6700\u9002\u5408\u4f7f\u7528\u7684\u3002\u6211\u4eec\u4f1a\u5728 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u5339\u914d\u6b67\u4e49 \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u90a3\u4e48\u7f16\u8bd1\u5668\u5b9e\u9645\u4e0a\u662f\u5982\u4f55\u786e\u5b9a\u5b83\u662f\u5426\u53ef\u4ee5\u5c06\u4e00\u4e2a\u503c\u4ece\u4e00\u79cd\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u7c7b\u578b\u7684\u5462?","title":"\u5f53\u7c7b\u578b\u8f6c\u6362\u89e6\u53d1\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48"},{"location":"8-1-Implicit-type-conversion-coercion/#_4","text":"C++\u8bed\u8a00\u6807\u51c6\u5b9a\u4e49\u4e86\u4e0d\u540c\u7684\u57fa\u672c\u7c7b\u578b(\u4e5f\u5305\u62ec\u4e00\u4e9b\u590d\u5408\u7c7b\u578b)\u5e94\u5f53\u5982\u4f55\u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b\u3002\u8fd9\u4e9b\u8f6c\u6362\u89c4\u5219\u79f0\u4e3a\u8f6c\u6362\u6807\u51c6\u3002 \u8f6c\u6362\u6807\u51c6\u5927\u81f4\u53ef\u5206\u4e3a\u56db\u7c7b\uff0c\u6bcf\u4e00\u7c7b\u6db5\u76d6\u4e0d\u540c\u7684\u8f6c\u6362\u65b9\u5f0f: \u6570\u503c\u63d0\u5347(\u53c2\u8003\uff1a 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 )\uff1b \u6570\u503c\u8f6c\u6362(\u53c2\u8003\uff1a 8.3 - \u6570\u503c\u8f6c\u6362 )\uff1b \u7b97\u6570\u8f6c\u6362(\u53c2\u8003\uff1a 8.4 - \u7b97\u6570\u8f6c\u6362 )\uff1b \u5176\u4ed6\u8f6c\u6362 (\u5305\u62ec\u591a\u79cd\u6307\u9488\u548c\u5f15\u7528\u7684\u8f6c\u6362) \u5f53\u9700\u8981\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u67e5\u770b\u662f\u5426\u6709\u6807\u51c6\u8f6c\u6362\uff0c\u53ef\u4ee5\u7528\u6765\u5c06\u503c\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u7c7b\u578b\u3002\u7f16\u8bd1\u5668\u53ef\u4ee5\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u5e94\u7528 0 \u4e2a\u30011 \u4e2a\u6216\u591a\u4e2a\u6807\u51c6\u8f6c\u6362\u3002 \u9898\u5916\u8bdd \u5982\u4f55\u5728\u4e0d\u8fdb\u884c\u5b9e\u9645\u7c7b\u578b\u8f6c\u6362\u7684\u540c\u65f6\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362? \u4f8b\u5982\uff0c\u5728 int \u548c long \u90fd\u5177\u6709\u76f8\u540c\u5927\u5c0f\u548c\u8303\u56f4\u7684\u67b6\u6784\u4e2d\uff0c\u4f7f\u7528\u76f8\u540c\u7684\u4f4d\u5e8f\u5217\u6765\u8868\u793a\u8fd9\u4e24\u79cd\u7c7b\u578b\u7684\u503c\u3002 \u56e0\u6b64\uff0c\u5728\u8fd9\u4e9b\u7c7b\u578b\u4e4b\u95f4\u8f6c\u6362\u503c\u4e0d\u9700\u8981\u5b9e\u9645\u7684\u8f6c\u6362\u2014\u2014\u503c\u53ef\u4ee5\u7b80\u5355\u5730\u590d\u5236\u3002 \u63cf\u8ff0\u7c7b\u578b\u8f6c\u6362\u5982\u4f55\u5de5\u4f5c\u7684\u4e00\u6574\u5957\u89c4\u5219\u65e2\u5197\u957f\u53c8\u590d\u6742\uff0c\u800c\u4e14\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7c7b\u578b\u8f6c\u6362\u201d\u5c31\u662f\u80fd\u591f\u5de5\u4f5c\u201c\u3002\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u4f60\u9700\u8981\u4e86\u89e3\u7684\u5173\u4e8e\u7c7b\u578b\u8f6c\u6362\u7684\u6700\u91cd\u8981\u7684\u4e8b\u60c5\u3002\u5982\u679c\u4f60\u9700\u8981\u66f4\u8be6\u7ec6\u7684\u8d44\u6599\uff0c\u53ef\u4ee5\u53c2\u8003 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u6280\u672f\u53c2\u8003\u6587\u6863 \u3002","title":"\u8f6c\u6362\u6807\u51c6"},{"location":"8-10-Function-overload-differentiation/","tags":["overload"],"text":"Key Takeaway \u51fd\u6570\u4e2d\u7528\u4e8e\u533a\u5206\u91cd\u8f7d\u51fd\u6570\u7684\u90e8\u5206\u6709\uff1a\u5f62\u53c2\u4e2a\u6570\u3001\u5f62\u53c2\u7c7b\u578b\u3001\u7701\u7565\u53f7\u3002 \u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u80fd\u88ab\u7528\u6765\u533a\u5206\u51fd\u6570 \u6210\u5458\u51fd\u6570\u5728\u4e0a\u9762\u7684\u57fa\u7840\u4e0a\u8fd8\u5305\u62ec const \u3001 volatile \u548c\u5f15\u7528\u9650\u5b9a\u7b26 \u5f53\u7f16\u8bd1\u5668\u7f16\u8bd1\u4e00\u4e2a\u51fd\u6570\u65f6\uff0c\u5b83\u4f1a\u6267\u884c \u547d\u540d\u4fee\u9970(Name mangling) \uff0c\u8fd9\u610f\u5473\u7740\u6839\u636e\u5404\u79cd\u6761\u4ef6\u6539\u53d8\u7f16\u8bd1\u540e\u7684\u51fd\u6570\u540d\u79f0(\u201c\u6539\u5199\u201d)\uff0c\u4f8b\u5982\u53c2\u6570\u7684\u6570\u91cf\u548c\u7c7b\u578b\uff0c\u4ee5\u4fbf\u94fe\u63a5\u5668\u6709\u552f\u4e00\u7684\u540d\u79f0\u6765\u5904\u7406\u3002 \u5728\u4e0a\u4e00\u8282\u8bfe\u4e2d ( 8.9 - \u51fd\u6570\u91cd\u8f7d )\uff0c \u6211\u4eec\u5f15\u5165\u4e86\u51fd\u6570\u91cd\u8f7d\u7684\u6982\u5ff5\uff0c\u4f7f\u7528\u51fd\u6570\u91cd\u8f7d\u53ef\u4ee5\u521b\u5efa\u5177\u6709\u76f8\u540c\u540d\u79f0\u7684\u591a\u4e2a\u51fd\u6570\uff0c\u524d\u63d0\u662f\u6bcf\u4e2a\u76f8\u540c\u540d\u79f0\u7684\u51fd\u6570\u5177\u6709\u4e0d\u540c\u7684\u5f62\u53c2\u7c7b\u578b(\u6216\u8005\u53ef\u4ee5\u7528\u5176\u4ed6\u65b9\u5f0f\u533a\u5206\u51fd\u6570)\u3002 \u5728\u8fd9\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u4e86\u89e3\u91cd\u8f7d\u51fd\u6570\u662f\u5982\u4f55\u533a\u5206\u5f00\u6765\u7684\uff0c\u4e0d\u80fd\u88ab\u6b63\u786e\u533a\u5206\u7684\u91cd\u8f7d\u51fd\u6570\u5c06\u5bfc\u81f4\u7f16\u8bd1\u5668\u53d1\u51fa\u7f16\u8bd1\u9519\u8bef\u3002 \u91cd\u8f7d\u51fd\u6570\u662f\u5982\u4f55\u88ab\u533a\u5206\u7684 \u51fd\u6570\u5c5e\u6027 \u88ab\u7528\u4e8e\u533a\u5206\u51fd\u6570 \u5907\u6ce8 \u5f62\u53c2\u4e2a\u6570 Yes \u5f62\u53c2\u7c7b\u578b Yes \u4e0d\u5305\u62ec typedefs \u3001\u7c7b\u578b\u522b\u540d\u3001 const \u9650\u5b9a\u7b26\u3002\u5305\u62ec\u7701\u7565\u53f7(ellipses) \u8fd4\u56de\u503c\u7c7b\u578b No \u6ce8\u610f\uff0c\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u4e0d\u7528\u4e8e\u533a\u5206\u91cd\u8f7d\u51fd\u6570\u3002\u6211\u4eec\u7a0d\u540e\u518d\u8ba8\u8bba\u8fd9\u4e2a\u95ee\u9898\u3002 \u6269\u5c55\u9605\u8bfb \u5bf9\u4e8e \u6210\u5458\u51fd\u6570 \uff0c\u8fd8\u4f1a\u8003\u8651\u9644\u52a0\u7684\u51fd\u6570\u7ea7\u9650\u5b9a\u7b26: \u51fd\u6570\u7ea7\u9650\u5b9a\u7b26 \u662f\u5426\u7528\u4e8e\u91cd\u8f7d const or volatile Yes [[ref-qualifier \u5f15\u7528\u9650\u5b9a\u7b26(Ref-qualifiers)]] \u4f8b\u5982\uff0c const \u6210\u5458\u51fd\u6570\u53ef\u4ee5\u4e0e\u5176\u4ed6\u5b8c\u5168\u76f8\u540c\u7684\u975e const \u6210\u5458\u51fd\u6570\u533a\u522b\u5f00\u6765(\u5373\u4f7f\u5b83\u4eec\u5171\u4eab\u76f8\u540c\u7684\u5f62\u53c2\u96c6)\u3002 \u57fa\u4e8e\u53c2\u6570\u4e2a\u6570\u8fdb\u884c\u91cd\u8f7d \u53ea\u8981\u6bcf\u4e2a\u91cd\u8f7d\u51fd\u6570\u5177\u6709\u4e0d\u540c\u6570\u91cf\u7684\u5f62\u53c2\uff0c\u5c31\u53ef\u4ee5\u533a\u5206\u91cd\u8f7d\u51fd\u6570\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 int add ( int x , int y ) { return x + y ; } int add ( int x , int y , int z ) { return x + y + z ; } \u7f16\u8bd1\u5668\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5c06\u4e24\u4e2a\u6574\u578b\u53c2\u6570\u7684\u51fd\u6570\u8c03\u7528\u5339\u914d\u5230 add(int, int) \uff0c\u800c\u5c06\u4e09\u4e2a\u6574\u578b\u53c2\u6570\u7684\u51fd\u6570\u8c03\u7528\u5339\u914d\u5230 add(int, int, int) \u3002 \u57fa\u4e8e\u53c2\u6570\u7c7b\u578b\u8fdb\u884c\u91cd\u8f7d \u53ea\u8981\u6bcf\u4e2a\u91cd\u8f7d\u51fd\u6570\u7684\u5f62\u53c2\u7c7b\u578b\u5217\u8868\u662f\u4e0d\u540c\u7684\uff0c\u5c31\u53ef\u4ee5\u5bf9\u51fd\u6570\u8fdb\u884c\u533a\u5206\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u6240\u6709\u7684\u91cd\u8f7d\u51fd\u6570\u90fd\u662f\u53ef\u4ee5\u88ab\u533a\u5206\u7684: 1 2 3 4 int add ( int x , int y ); // \u6574\u578b\u7248\u672c double add ( double x , double y ); // \u6d6e\u70b9\u7248\u672c double add ( int x , double y ); // \u6df7\u5408\u7248\u672c double add ( double x , int y ); // \u6df7\u5408\u7248\u672c \u56e0\u4e3a\u7c7b\u578b\u522b\u540d(\u6216 typedefs )\u5e76\u4e0d\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u7c7b\u578b\uff0c\u6240\u4ee5\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u7684\u91cd\u8f7d\u51fd\u6570\u4e0e\u4f7f\u7528\u5bf9\u5e94\u7c7b\u578b\u7684\u91cd\u8f7d\u6ca1\u6709\u533a\u522b\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u6240\u6709\u91cd\u8f7d\u90fd\u662f\u4e0d\u80fd\u88ab\u533a\u5206\u7684(\u5e76\u5c06\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef)\uff1a 1 2 3 4 5 6 typedef int height_t ; // typedef using age_t = int ; // type alias void print ( int value ); void print ( age_t value ); // not differentiated from print(int) void print ( height_t value ); // not differentiated from print(int) \u5bf9\u4e8e\u901a\u8fc7\u503c\u4f20\u9012\u7684\u53c2\u6570\uff0c\u4e5f\u4e0d\u8003\u8651 const \u9650\u5b9a\u7b26\u3002\u56e0\u6b64\uff0c\u4ee5\u4e0b\u51fd\u6570\u662f\u4e0d\u80fd\u88ab\u533a\u5206\u5f00\u7684\uff1a 1 2 void print ( int ); void print ( const int ); // not differentiated from print(int) \u6269\u5c55\u9605\u8bfb \u6211\u4eec\u8fd8\u6ca1\u6709\u6d89\u53ca\u5230\u7701\u7565\u53f7\uff0c\u4f46\u662f\u7701\u7565\u53f7\u53c2\u6570\u88ab\u8ba4\u4e3a\u662f\u4e00\u79cd\u72ec\u7279\u7684\u53c2\u6570\u7c7b\u578b: 1 2 void foo ( int x , int y ); void foo ( int x , ...); // \u548c foo(int, int)\u662f\u4e0d\u540c\u7684 \u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u80fd\u88ab\u7528\u6765\u533a\u5206\u51fd\u6570 \u5728\u533a\u5206\u91cd\u8f7d\u51fd\u6570\u65f6\uff0c\u4e0d\u8003\u8651\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u3002 \u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5:\u60a8\u60f3\u7f16\u5199\u4e00\u4e2a\u8fd4\u56de\u968f\u673a\u6570\u7684\u51fd\u6570\uff0c\u4f46\u662f\u60a8\u9700\u8981\u4e00\u4e2a\u8fd4\u56de\u6574\u6570\u7684\u7248\u672c\uff0c\u4ee5\u53ca\u53e6\u4e00\u4e2a\u8fd4\u56de\u53cc\u7cbe\u5ea6\u6570\u7684\u7248\u672c\u3002\u4f60\u53ef\u80fd\u4f1a\u5fcd\u4e0d\u4f4f\u8fd9\u6837\u505a: 1 2 int getRandomValue (); double getRandomValue (); Visual Studio 2019 \u4e2d\u4f1a\u4ea7\u751f\u5982\u4e0b\u7f16\u8bd1\u9519\u8bef\uff1a 1 error C2556: 'double getRandomValue(void)': overloaded function differs only by return type from 'int getRandomValue(void)' \u8fd9\u662f\u5f88\u663e\u7136\u7684\uff0c\u5047\u8bbe\u4f60\u81ea\u5df1\u662f\u7f16\u8bd1\u5668\uff0c\u5f53\u4f60\u770b\u5230\u4e0b\u9762\u7684\u5b9a\u4e49\u65f6\uff1a 1 getRandomValue (); \u4f60\u5e94\u8be5\u8c03\u7528\u4e24\u4e2a\u51fd\u6570\u4e2d\u7684\u54ea\u4e2a\u5462\uff1f \u9898\u5916\u8bdd \u8fd9\u662f\u4e00\u4e2a\u6709\u610f\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u786e\u4fdd\u4e86\u51fd\u6570\u8c03\u7528\u7684\u884c\u4e3a\u53ef\u4ee5\u72ec\u7acb\u4e8e\u8868\u8fbe\u5f0f\u7684\u5176\u4ed6\u90e8\u5206\u6765\u786e\u5b9a\uff0c\u4ece\u800c\u4f7f\u7406\u89e3\u590d\u6742\u8868\u8fbe\u5f0f\u66f4\u52a0\u7b80\u5355\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u603b\u662f\u53ef\u4ee5\u4ec5\u6839\u636e\u51fd\u6570\u8c03\u7528\u4e2d\u7684\u53c2\u6570\u6765\u51b3\u5b9a\u8c03\u7528\u54ea\u4e2a\u7248\u672c\u7684\u51fd\u6570\u3002\u5982\u679c\u8fd4\u56de\u503c\u7528\u4e8e\u533a\u5206\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u6ca1\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u8bed\u6cd5\u65b9\u6cd5\u6765\u5224\u65ad\u8c03\u7528\u4e86\u54ea\u4e2a\u51fd\u6570\u7684\u91cd\u8f7d\u2014\u2014\u6211\u4eec\u8fd8\u5fc5\u987b\u7406\u89e3\u8fd4\u56de\u503c\u662f\u5982\u4f55\u88ab\u4f7f\u7528\u7684\uff0c\u8fd9\u9700\u8981\u66f4\u591a\u7684\u5206\u6790\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u6700\u597d\u65b9\u6cd5\u662f\u7ed9\u51fd\u6570\u53d6\u4e0d\u540c\u7684\u540d\u5b57\uff1a 1 2 int getRandomInt (); double getRandomDouble (); \u7c7b\u578b\u7b7e\u540d \u51fd\u6570\u7684 \u7c7b\u578b\u7b7e\u540d(type signature) (\u901a\u5e38\u7b80\u79f0\u4e3a\u7b7e\u540d) \u662f\u51fd\u6570\u5934\u7684\u4e00\u90e8\u5206\uff0c\u88ab\u7528\u4e8e\u533a\u5206\u51fd\u6570\u3002\u5728 C++ \u4e2d\uff0c\u7b7e\u540d\u5305\u62ec\u51fd\u6570\u540d\u3001\u53c2\u6570\u6570\u91cf\u3001\u53c2\u6570\u7c7b\u578b\u548c\u51fd\u6570\u7ea7\u7684\u9650\u5b9a\u7b26\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5b83\u4e0d\u5305\u62ec\u8fd4\u56de\u7c7b\u578b\u3002 \u547d\u540d\u4fee\u9970 \u9898\u5916\u8bdd \u5f53\u7f16\u8bd1\u5668\u7f16\u8bd1\u4e00\u4e2a\u51fd\u6570\u65f6\uff0c\u5b83\u4f1a\u6267\u884c \u547d\u540d\u4fee\u9970(Name mangling) \uff0c\u8fd9\u610f\u5473\u7740\u6839\u636e\u5404\u79cd\u6761\u4ef6\u6539\u53d8\u7f16\u8bd1\u540e\u7684\u51fd\u6570\u540d\u79f0(\u201c\u6539\u5199\u201d)\uff0c\u4f8b\u5982\u53c2\u6570\u7684\u6570\u91cf\u548c\u7c7b\u578b\uff0c\u4ee5\u4fbf\u94fe\u63a5\u5668\u6709\u552f\u4e00\u7684\u540d\u79f0\u6765\u5904\u7406\u3002 \u4f8b\u5982\uff0c\u4e00\u4e9b\u539f\u578b\u4e3a int fcn() \u7684\u51fd\u6570\u53ef\u80fd\u4f1a\u88ab\u7f16\u8bd1\u4e3a __fcn_v \uff0c\u800c int fcn(int) \u53ef\u80fd\u4f1a\u88ab\u7f16\u8bd1\u4e3a __fcn_i \u3002\u56e0\u6b64\uff0c\u867d\u7136\u5728\u6e90\u4ee3\u7801\u4e2d\uff0c\u4e24\u4e2a\u91cd\u8f7d\u51fd\u6570\u5171\u4eab\u4e00\u4e2a\u540d\u79f0\uff0c\u4f46\u5728\u7f16\u8bd1\u4ee3\u7801\u4e2d\uff0c\u5b83\u4eec\u7684\u540d\u79f0\u5b9e\u9645\u4e0a\u662f\u552f\u4e00\u7684\u3002 \u6807\u51c6\u6ca1\u6709\u89c4\u5b9a\u540d\u79f0\u5e94\u8be5\u5982\u4f55\u88ab\u4fee\u9970\uff0c\u56e0\u6b64\u4e0d\u540c\u7684\u7f16\u8bd1\u5668\u5c06\u4ea7\u751f\u4e0d\u540c\u7684\u51fd\u6570\u540d\u79f0\u3002","title":"8.10 - \u91cd\u8f7d\u51fd\u6570\u7684\u533a\u5206"},{"location":"8-10-Function-overload-differentiation/#_1","text":"\u51fd\u6570\u5c5e\u6027 \u88ab\u7528\u4e8e\u533a\u5206\u51fd\u6570 \u5907\u6ce8 \u5f62\u53c2\u4e2a\u6570 Yes \u5f62\u53c2\u7c7b\u578b Yes \u4e0d\u5305\u62ec typedefs \u3001\u7c7b\u578b\u522b\u540d\u3001 const \u9650\u5b9a\u7b26\u3002\u5305\u62ec\u7701\u7565\u53f7(ellipses) \u8fd4\u56de\u503c\u7c7b\u578b No \u6ce8\u610f\uff0c\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u4e0d\u7528\u4e8e\u533a\u5206\u91cd\u8f7d\u51fd\u6570\u3002\u6211\u4eec\u7a0d\u540e\u518d\u8ba8\u8bba\u8fd9\u4e2a\u95ee\u9898\u3002 \u6269\u5c55\u9605\u8bfb \u5bf9\u4e8e \u6210\u5458\u51fd\u6570 \uff0c\u8fd8\u4f1a\u8003\u8651\u9644\u52a0\u7684\u51fd\u6570\u7ea7\u9650\u5b9a\u7b26: \u51fd\u6570\u7ea7\u9650\u5b9a\u7b26 \u662f\u5426\u7528\u4e8e\u91cd\u8f7d const or volatile Yes [[ref-qualifier \u5f15\u7528\u9650\u5b9a\u7b26(Ref-qualifiers)]] \u4f8b\u5982\uff0c const \u6210\u5458\u51fd\u6570\u53ef\u4ee5\u4e0e\u5176\u4ed6\u5b8c\u5168\u76f8\u540c\u7684\u975e const \u6210\u5458\u51fd\u6570\u533a\u522b\u5f00\u6765(\u5373\u4f7f\u5b83\u4eec\u5171\u4eab\u76f8\u540c\u7684\u5f62\u53c2\u96c6)\u3002","title":"\u91cd\u8f7d\u51fd\u6570\u662f\u5982\u4f55\u88ab\u533a\u5206\u7684"},{"location":"8-10-Function-overload-differentiation/#_2","text":"\u53ea\u8981\u6bcf\u4e2a\u91cd\u8f7d\u51fd\u6570\u5177\u6709\u4e0d\u540c\u6570\u91cf\u7684\u5f62\u53c2\uff0c\u5c31\u53ef\u4ee5\u533a\u5206\u91cd\u8f7d\u51fd\u6570\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 int add ( int x , int y ) { return x + y ; } int add ( int x , int y , int z ) { return x + y + z ; } \u7f16\u8bd1\u5668\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5c06\u4e24\u4e2a\u6574\u578b\u53c2\u6570\u7684\u51fd\u6570\u8c03\u7528\u5339\u914d\u5230 add(int, int) \uff0c\u800c\u5c06\u4e09\u4e2a\u6574\u578b\u53c2\u6570\u7684\u51fd\u6570\u8c03\u7528\u5339\u914d\u5230 add(int, int, int) \u3002","title":"\u57fa\u4e8e\u53c2\u6570\u4e2a\u6570\u8fdb\u884c\u91cd\u8f7d"},{"location":"8-10-Function-overload-differentiation/#_3","text":"\u53ea\u8981\u6bcf\u4e2a\u91cd\u8f7d\u51fd\u6570\u7684\u5f62\u53c2\u7c7b\u578b\u5217\u8868\u662f\u4e0d\u540c\u7684\uff0c\u5c31\u53ef\u4ee5\u5bf9\u51fd\u6570\u8fdb\u884c\u533a\u5206\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u6240\u6709\u7684\u91cd\u8f7d\u51fd\u6570\u90fd\u662f\u53ef\u4ee5\u88ab\u533a\u5206\u7684: 1 2 3 4 int add ( int x , int y ); // \u6574\u578b\u7248\u672c double add ( double x , double y ); // \u6d6e\u70b9\u7248\u672c double add ( int x , double y ); // \u6df7\u5408\u7248\u672c double add ( double x , int y ); // \u6df7\u5408\u7248\u672c \u56e0\u4e3a\u7c7b\u578b\u522b\u540d(\u6216 typedefs )\u5e76\u4e0d\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u7c7b\u578b\uff0c\u6240\u4ee5\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u7684\u91cd\u8f7d\u51fd\u6570\u4e0e\u4f7f\u7528\u5bf9\u5e94\u7c7b\u578b\u7684\u91cd\u8f7d\u6ca1\u6709\u533a\u522b\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u6240\u6709\u91cd\u8f7d\u90fd\u662f\u4e0d\u80fd\u88ab\u533a\u5206\u7684(\u5e76\u5c06\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef)\uff1a 1 2 3 4 5 6 typedef int height_t ; // typedef using age_t = int ; // type alias void print ( int value ); void print ( age_t value ); // not differentiated from print(int) void print ( height_t value ); // not differentiated from print(int) \u5bf9\u4e8e\u901a\u8fc7\u503c\u4f20\u9012\u7684\u53c2\u6570\uff0c\u4e5f\u4e0d\u8003\u8651 const \u9650\u5b9a\u7b26\u3002\u56e0\u6b64\uff0c\u4ee5\u4e0b\u51fd\u6570\u662f\u4e0d\u80fd\u88ab\u533a\u5206\u5f00\u7684\uff1a 1 2 void print ( int ); void print ( const int ); // not differentiated from print(int) \u6269\u5c55\u9605\u8bfb \u6211\u4eec\u8fd8\u6ca1\u6709\u6d89\u53ca\u5230\u7701\u7565\u53f7\uff0c\u4f46\u662f\u7701\u7565\u53f7\u53c2\u6570\u88ab\u8ba4\u4e3a\u662f\u4e00\u79cd\u72ec\u7279\u7684\u53c2\u6570\u7c7b\u578b: 1 2 void foo ( int x , int y ); void foo ( int x , ...); // \u548c foo(int, int)\u662f\u4e0d\u540c\u7684","title":"\u57fa\u4e8e\u53c2\u6570\u7c7b\u578b\u8fdb\u884c\u91cd\u8f7d"},{"location":"8-10-Function-overload-differentiation/#_4","text":"\u5728\u533a\u5206\u91cd\u8f7d\u51fd\u6570\u65f6\uff0c\u4e0d\u8003\u8651\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u3002 \u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5:\u60a8\u60f3\u7f16\u5199\u4e00\u4e2a\u8fd4\u56de\u968f\u673a\u6570\u7684\u51fd\u6570\uff0c\u4f46\u662f\u60a8\u9700\u8981\u4e00\u4e2a\u8fd4\u56de\u6574\u6570\u7684\u7248\u672c\uff0c\u4ee5\u53ca\u53e6\u4e00\u4e2a\u8fd4\u56de\u53cc\u7cbe\u5ea6\u6570\u7684\u7248\u672c\u3002\u4f60\u53ef\u80fd\u4f1a\u5fcd\u4e0d\u4f4f\u8fd9\u6837\u505a: 1 2 int getRandomValue (); double getRandomValue (); Visual Studio 2019 \u4e2d\u4f1a\u4ea7\u751f\u5982\u4e0b\u7f16\u8bd1\u9519\u8bef\uff1a 1 error C2556: 'double getRandomValue(void)': overloaded function differs only by return type from 'int getRandomValue(void)' \u8fd9\u662f\u5f88\u663e\u7136\u7684\uff0c\u5047\u8bbe\u4f60\u81ea\u5df1\u662f\u7f16\u8bd1\u5668\uff0c\u5f53\u4f60\u770b\u5230\u4e0b\u9762\u7684\u5b9a\u4e49\u65f6\uff1a 1 getRandomValue (); \u4f60\u5e94\u8be5\u8c03\u7528\u4e24\u4e2a\u51fd\u6570\u4e2d\u7684\u54ea\u4e2a\u5462\uff1f \u9898\u5916\u8bdd \u8fd9\u662f\u4e00\u4e2a\u6709\u610f\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u786e\u4fdd\u4e86\u51fd\u6570\u8c03\u7528\u7684\u884c\u4e3a\u53ef\u4ee5\u72ec\u7acb\u4e8e\u8868\u8fbe\u5f0f\u7684\u5176\u4ed6\u90e8\u5206\u6765\u786e\u5b9a\uff0c\u4ece\u800c\u4f7f\u7406\u89e3\u590d\u6742\u8868\u8fbe\u5f0f\u66f4\u52a0\u7b80\u5355\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u603b\u662f\u53ef\u4ee5\u4ec5\u6839\u636e\u51fd\u6570\u8c03\u7528\u4e2d\u7684\u53c2\u6570\u6765\u51b3\u5b9a\u8c03\u7528\u54ea\u4e2a\u7248\u672c\u7684\u51fd\u6570\u3002\u5982\u679c\u8fd4\u56de\u503c\u7528\u4e8e\u533a\u5206\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u6ca1\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u8bed\u6cd5\u65b9\u6cd5\u6765\u5224\u65ad\u8c03\u7528\u4e86\u54ea\u4e2a\u51fd\u6570\u7684\u91cd\u8f7d\u2014\u2014\u6211\u4eec\u8fd8\u5fc5\u987b\u7406\u89e3\u8fd4\u56de\u503c\u662f\u5982\u4f55\u88ab\u4f7f\u7528\u7684\uff0c\u8fd9\u9700\u8981\u66f4\u591a\u7684\u5206\u6790\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u6700\u597d\u65b9\u6cd5\u662f\u7ed9\u51fd\u6570\u53d6\u4e0d\u540c\u7684\u540d\u5b57\uff1a 1 2 int getRandomInt (); double getRandomDouble ();","title":"\u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u80fd\u88ab\u7528\u6765\u533a\u5206\u51fd\u6570"},{"location":"8-10-Function-overload-differentiation/#_5","text":"\u51fd\u6570\u7684 \u7c7b\u578b\u7b7e\u540d(type signature) (\u901a\u5e38\u7b80\u79f0\u4e3a\u7b7e\u540d) \u662f\u51fd\u6570\u5934\u7684\u4e00\u90e8\u5206\uff0c\u88ab\u7528\u4e8e\u533a\u5206\u51fd\u6570\u3002\u5728 C++ \u4e2d\uff0c\u7b7e\u540d\u5305\u62ec\u51fd\u6570\u540d\u3001\u53c2\u6570\u6570\u91cf\u3001\u53c2\u6570\u7c7b\u578b\u548c\u51fd\u6570\u7ea7\u7684\u9650\u5b9a\u7b26\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5b83\u4e0d\u5305\u62ec\u8fd4\u56de\u7c7b\u578b\u3002","title":"\u7c7b\u578b\u7b7e\u540d"},{"location":"8-10-Function-overload-differentiation/#_6","text":"\u9898\u5916\u8bdd \u5f53\u7f16\u8bd1\u5668\u7f16\u8bd1\u4e00\u4e2a\u51fd\u6570\u65f6\uff0c\u5b83\u4f1a\u6267\u884c \u547d\u540d\u4fee\u9970(Name mangling) \uff0c\u8fd9\u610f\u5473\u7740\u6839\u636e\u5404\u79cd\u6761\u4ef6\u6539\u53d8\u7f16\u8bd1\u540e\u7684\u51fd\u6570\u540d\u79f0(\u201c\u6539\u5199\u201d)\uff0c\u4f8b\u5982\u53c2\u6570\u7684\u6570\u91cf\u548c\u7c7b\u578b\uff0c\u4ee5\u4fbf\u94fe\u63a5\u5668\u6709\u552f\u4e00\u7684\u540d\u79f0\u6765\u5904\u7406\u3002 \u4f8b\u5982\uff0c\u4e00\u4e9b\u539f\u578b\u4e3a int fcn() \u7684\u51fd\u6570\u53ef\u80fd\u4f1a\u88ab\u7f16\u8bd1\u4e3a __fcn_v \uff0c\u800c int fcn(int) \u53ef\u80fd\u4f1a\u88ab\u7f16\u8bd1\u4e3a __fcn_i \u3002\u56e0\u6b64\uff0c\u867d\u7136\u5728\u6e90\u4ee3\u7801\u4e2d\uff0c\u4e24\u4e2a\u91cd\u8f7d\u51fd\u6570\u5171\u4eab\u4e00\u4e2a\u540d\u79f0\uff0c\u4f46\u5728\u7f16\u8bd1\u4ee3\u7801\u4e2d\uff0c\u5b83\u4eec\u7684\u540d\u79f0\u5b9e\u9645\u4e0a\u662f\u552f\u4e00\u7684\u3002 \u6807\u51c6\u6ca1\u6709\u89c4\u5b9a\u540d\u79f0\u5e94\u8be5\u5982\u4f55\u88ab\u4fee\u9970\uff0c\u56e0\u6b64\u4e0d\u540c\u7684\u7f16\u8bd1\u5668\u5c06\u4ea7\u751f\u4e0d\u540c\u7684\u51fd\u6570\u540d\u79f0\u3002","title":"\u547d\u540d\u4fee\u9970"},{"location":"8-11-Function-overload-resolution-and-ambiguous-matches/","tags":["overload"],"text":"Key Takeaway \u51fd\u6570\u8c03\u7528\u65f6\u5339\u914d\u91cd\u8f7d\u51fd\u6570\u7684\u8fc7\u7a0b\u53eb\u505a\u91cd\u8f7d\u89e3\u6790\uff0c\u91cd\u8f7d\u89e3\u6790\u5206\u4e3a 6 \u4e2a\u6b65\u9aa4\uff1a \u5b8c\u5168\u5339\u914d \u6570\u503c\u63d0\u5347\u5339\u914d \u6570\u503c\u8f6c\u6362\u5339\u914d \u81ea\u5b9a\u4e49\u7c7b\u578b\u8f6c\u6362\u5339\u914d \u7701\u7565\u53f7\u5339\u914d \u653e\u5f03\u5e76\u62a5\u9519 \u5728\u6bcf\u4e00\u6b65\u4e2d\uff0c\u5982\u679c\u627e\u5230\u4e14\u53ea\u627e\u5230\u4e00\u4e2a\u5339\u914d\u51fd\u6570\uff0c\u5219\u5b8c\u6210\u5339\u914d\uff0c\u5426\u5219\u8fdb\u5165\u4e0b\u4e00\u6b65\u3002\u5982\u679c\u6700\u7ec8\u5339\u914d\u7684\u7ed3\u679c\u5927\u4e8e 1 \u4e2a\u6216\u8005\u4e3a 0\uff0c\u5219\u62a5\u9519\u3002 \u901a\u8fc7\u6570\u503c\u63d0\u5347\u8fdb\u884c\u5339\u914d\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e\u901a\u8fc7\u6570\u503c\u8f6c\u6362\u5339\u914d\u7684\u4f18\u5148\u7ea7 \u5c06\u975e\u5f15\u7528\u7c7b\u578b\u8f6c\u6362\u4e3a\u5f15\u7528\u7c7b\u578b\uff08\u6216\u8005\u53cd\u8fc7\u6765\uff09\u4e5f\u5c5e\u4e8e \u7b80\u5355\u8f6c\u6362 \u5982\u679c\u7f16\u8bd1\u5668\u5728\u67d0\u4e00\u6b65\u627e\u5230\u4e86\u591a\u4e2a\u53ef\u80fd\u7684\u5339\u914d\uff0c\u5219\u4f1a\u5bfc\u81f4 \u4e0d\u660e\u786e\u5339\u914d \u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u8fd9\u4e00\u6b65\u4e2d\u7f16\u8bd1\u5668\u6ca1\u80fd\u627e\u5230\u4e00\u4e2a\u6bd4\u5176\u4ed6\u5339\u914d\u90fd\u597d\u7684\u5339\u914d\u3002\u6ce8\u610f\uff0c\u5728\u6bcf\u4e00\u6b65\u4e2d\u7f16\u8bd1\u5668\u90fd\u4f1a\u5e94\u7528\u6240\u6709\u53ef\u7528\u7684\u8f6c\u6362\u5e76\u8fdb\u884c\u5339\u914d\uff0c\u4e0d\u4f1a\u627e\u5230\u4e00\u4e2a\u5339\u914d\u540e\u5c31\u505c\u6b62\u3002 \u89e3\u51b3\u4e0d\u660e\u786e\u5339\u914d\u7684\u65b9\u6cd5\u662f\uff1a \u5b9a\u4e49\u4e00\u4e2a\u80fd\u591f\u53c2\u6570\u5b8c\u5168\u5339\u914d\u7684\u65b0\u51fd\u6570 \u5f3a\u5236\u7c7b\u578b\u8f6c\u6362 \u5b57\u9762\u91cf\u6dfb\u52a0\u540e\u7f00 \u5728\u4e0a\u8282\u8bfe( 8.10 - \u51fd\u6570\u91cd\u8f7d\u548c\u533a\u5206 )\u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u51fd\u6570\u4e2d\u53ef\u4ee5\u88ab\u7528\u6765\u5bf9\u91cd\u8f7d\u51fd\u6570\u8fdb\u884c\u533a\u5206\u7684\u5c5e\u6027\u3002\u5982\u679c\u4e00\u4e2a\u91cd\u8f7d\u51fd\u6570\u4e0d\u80fd\u548c\u5176\u4ed6\u91cd\u8f7d\u51fd\u6570\u88ab\u533a\u5206\u5f00\u6765\uff0c\u90a3\u4e48\u5c31\u4f1a\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002 \u4e0d\u8fc7\uff0c\u62e5\u6709\u4e00\u7ec4\u80fd\u591f\u88ab\u533a\u5206\u7684\u91cd\u8f7d\u51fd\u6570\u53ea\u662f\u89e3\u51b3\u4e86\u4e00\u534a\u7684\u95ee\u9898\u3002\u5f53\u8c03\u7528\u4efb\u4f55\u51fd\u6570\u65f6\uff0c\u7f16\u8bd1\u5668\u8fd8\u5fc5\u987b\u786e\u4fdd\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\u58f0\u660e\u3002 \u5bf9\u4e8e\u975e\u91cd\u8f7d\u51fd\u6570(\u5177\u6709\u60df\u4e00\u540d\u79f0\u7684\u51fd\u6570)\uff0c\u53ea\u6709\u4e00\u4e2a\u51fd\u6570\u53ef\u80fd\u4e0e\u51fd\u6570\u8c03\u7528\u5339\u914d\u3002\u8be5\u51fd\u6570\u8981\u4e48\u5339\u914d(\u6216\u8005\u53ef\u4ee5\u5728\u5e94\u7528\u7c7b\u578b\u8f6c\u6362\u540e\u5339\u914d)\uff0c\u8981\u4e48\u4e0d\u5339\u914d(\u7ed3\u679c\u662f\u7f16\u8bd1\u9519\u8bef)\u3002\u5bf9\u4e8e\u91cd\u8f7d\u51fd\u6570\uff0c\u53ef\u4ee5\u6709\u8bb8\u591a\u51fd\u6570\u53ef\u80fd\u4e0e\u51fd\u6570\u8c03\u7528\u5339\u914d\u3002\u7531\u4e8e\u51fd\u6570\u8c03\u7528\u53ea\u80fd\u89e3\u6790\u5230\u5176\u4e2d\u4e00\u4e2a\u51fd\u6570\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u5fc5\u987b\u786e\u5b9a\u54ea\u4e2a\u91cd\u8f7d\u51fd\u6570\u662f\u6700\u4f73\u5339\u914d\u7684\u3002\u5c06\u51fd\u6570\u8c03\u7528\u5339\u914d\u5230\u7279\u5b9a\u91cd\u8f7d\u51fd\u6570\u7684\u8fc7\u7a0b\u79f0\u4e3a \u91cd\u8f7d\u89e3\u6790 \u3002 \u5728\u51fd\u6570 \u5b9e\u53c2\u7c7b\u578b \u548c\u51fd\u6570 \u5f62\u53c2\u7c7b\u578b \u5b8c\u5168\u5339\u914d\u7684\u7b80\u5355\u60c5\u51b5\u4e0b\uff0c\u91cd\u8f7d\u51fd\u6570\u7684\u5339\u914d(\u901a\u5e38)\u662f\u5f88\u7b80\u5355\u7684: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void print ( int x ) { std :: cout << x ; } void print ( double d ) { std :: cout << d ; } int main () { print ( 5 ); // 5 is an int, so this matches print(int) print ( 6.7 ); // 6.7 is a double, so this matches print(double) return 0 ; } \u4f46\u662f\uff0c\u5982\u679c\u51fd\u6570\u8c03\u7528\u4e2d\u7684\u53c2\u6570\u7c7b\u578b\u4e0e\u91cd\u8f7d\u51fd\u6570\u4e2d\u7684\u53c2\u6570\u7c7b\u578b \u4e0d\u5b8c\u5168\u5339\u914d \uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void print ( int x ) { std :: cout << x ; } void print ( double d ) { std :: cout << d ; } int main () { print ( 'a' ); // char does not match int or double print ( 5L ); // long does not match int or double return 0 ; } \u6ca1\u6709\u5b8c\u5168\u5339\u914d\u7684\u51fd\u6570\uff0c\u4e0d\u7b49\u4e8e\u6ca1\u6709\u80fd\u591f\u5339\u914d\u7684\u51fd\u6570\u2014\u2014\u6bd5\u7adf\uff0c char \u6216\u8005 long \u53ef\u4ee5\u901a\u8fc7 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u8f6c\u6362\u4e3a int \u6216\u8005 double \u3002\u4f46\u662f\uff0c\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u5982\u4f55\u9009\u62e9 \u6700\u4f73 \u5339\u914d\u5462\uff1f \u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u63a2\u7d22\u7f16\u8bd1\u5668\u662f\u5982\u4f55\u5728\u51fd\u6570\u8c03\u7528\u65f6\u5339\u914d\u7684\u91cd\u8f7d\u51fd\u6570\u7684\u3002 \u89e3\u6790\u91cd\u8f7d\u51fd\u6570\u8c03\u7528 \u5f53\u5bf9\u91cd\u8f7d\u51fd\u6570\u8fdb\u884c\u8c03\u7528\u65f6\uff0c\u7f16\u8bd1\u5668\u901a\u8fc7\u4e00\u7cfb\u5217\u89c4\u5219\u548c\u6b65\u9aa4\u786e\u5b9a\u91cd\u8f7d\u51fd\u6570\u7684\u6700\u4f73\u5339\u914d(\u5982\u679c\u6709\u7684\u8bdd)\u3002 \u5728\u6bcf\u4e2a\u6b65\u9aa4\u4e2d\uff0c\u7f16\u8bd1\u5668\u5bf9\u51fd\u6570\u8c03\u7528\u4e2d\u7684\u5b9e\u53c2\u5e94\u7528\u4e00\u7cfb\u5217\u4e0d\u540c\u7684\u7c7b\u578b\u8f6c\u6362\u3002\u5bf9\u4e8e\u5e94\u7528\u7684\u6bcf\u4e2a\u8f6c\u6362\uff0c\u7f16\u8bd1\u5668\u68c0\u67e5\u91cd\u8f7d\u7684\u51fd\u6570\u73b0\u5728\u662f\u5426\u5339\u914d\u3002\u5728\u5e94\u7528\u4e86\u6240\u6709\u4e0d\u540c\u7684\u7c7b\u578b\u8f6c\u6362\u5e76\u68c0\u67e5\u4e86\u5339\u914d\u4e4b\u540e\uff0c\u8fd9\u4e00\u6b65\u5c31\u5b8c\u6210\u4e86\u3002\u7ed3\u679c\u5c06\u662f\u4ee5\u4e0b\u4e09\u79cd\u53ef\u80fd\u7684\u7ed3\u679c\u4e4b\u4e00\uff1a \u672a\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\u3002\u7f16\u8bd1\u5668\u8fdb\u5165\u4e0b\u4e00\u4e2a\u6b65\u9aa4\u3002 \u627e\u5230\u5355\u4e2a\u5339\u914d\u51fd\u6570\u3002\u8be5\u51fd\u6570\u88ab\u8ba4\u4e3a\u662f\u6700\u4f73\u5339\u914d\u3002\u5339\u914d\u8fc7\u7a0b\u73b0\u5728\u5df2\u7ecf\u5b8c\u6210\uff0c\u540e\u7eed\u6b65\u9aa4\u4e0d\u518d\u6267\u884c\u3002 \u53d1\u73b0\u591a\u4e2a\u5339\u914d\u51fd\u6570\u3002\u7f16\u8bd1\u5668\u5c06\u53d1\u51fa\u4e00\u4e2a \u4e0d\u660e\u786e\u5339\u914d \u7684\u7f16\u8bd1\u9519\u8bef\u3002\u6211\u4eec\u7a0d\u540e\u5c06\u8fdb\u4e00\u6b65\u8ba8\u8bba\u8fd9\u79cd\u60c5\u51b5\u3002 \u5982\u679c\u7f16\u8bd1\u5668\u6267\u884c\u5b8c\u4e00\u7cfb\u5217\u6b65\u9aa4\u540e\uff0c\u4ecd\u7136\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\uff0c\u5b83\u5c06\u751f\u6210\u4e00\u4e2a\u7f16\u8bd1\u9519\u8bef\uff0c\u5373\u65e0\u6cd5\u4e3a\u51fd\u6570\u8c03\u7528\u627e\u5230\u5339\u914d\u7684\u91cd\u8f7d\u51fd\u6570\u3002 \u53c2\u6570\u5339\u914d\u987a\u5e8f \u7b2c\u4e00\u6b65\uff1a\u7f16\u8bd1\u5668\u9996\u5148\u5c1d\u8bd5\u5bf9\u91cd\u8f7d\u51fd\u6570\u8fdb\u884c \u5b8c\u5168\u5339\u914d \u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5206\u4e3a\u4e24\u4e2a\u9636\u6bb5\u3002\u9996\u5148\uff0c\u7f16\u8bd1\u5668\u4f1a\u67e5\u627e\u662f\u5426\u5b58\u5728\u4e00\u4e2a\u91cd\u8f7d\u51fd\u6570\uff0c\u5176\u8c03\u7528\u65f6\u7684\u5b9e\u53c2\u5b8c\u5168\u5339\u914d\u91cd\u8f7d\u51fd\u6570\u7684\u5f62\u53c2\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 void print ( int ) { } void print ( double ) { } int main () { print ( 0 ); // exact match with print(int) print ( 3.4 ); // exact match with print(double) return 0 ; } \u56e0\u4e3a print(0) \u4e2d\u7684 0 \u662f\u4e00\u4e2a\u6574\u578b\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u67e5\u627e\u662f\u5426\u5b58\u5728 print(int) \u91cd\u8f7d\u51fd\u6570\u3002\u56e0\u4e3a\u7684\u786e\u5b58\u5728\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u53ef\u4ee5\u786e\u5b9a\u8be5\u91cd\u8f7d\u51fd\u6570\u4e3a\u88ab\u8c03\u7528\u51fd\u6570\u7684\u7cbe\u786e\u5339\u914d\u3002 \u5176\u6b21\uff0c\u7f16\u8bd1\u5668\u4f1a\u5bf9\u51fd\u6570\u8c03\u7528\u65f6\u7684\u5b9e\u53c2\u8fdb\u884c\u4e00\u7cfb\u5217\u7684 trivial-conversion \u3002\u8fd9\u4e9b\u7b80\u5355\u7684\u8f6c\u6362\u4f1a\u901a\u8fc7\u4fee\u6539\u7c7b\u578b\uff08\u4e0d\u4fee\u6539\u503c\uff09\u7684\u65b9\u5f0f\u67e5\u627e\u53ef\u80fd\u7684\u5339\u914d\u3002\u4f8b\u5982\uff0c\u975e const \u7c7b\u578b\u53ef\u80fd\u4f1a\u88ab\u8f6c\u6362\u4e3a const \u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 void print ( const int ) { } void print ( double ) { } int main () { int x { 0 }; print ( x ); // x trivially converted to const int return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8c03\u7528 print(x) \u65f6\uff0c x \u662f\u4e00\u4e2a int \u3002\u7f16\u8bd1\u5668\u4f1a\u5c06\u5176\u4ece int \u8f6c\u6362\u4e3a const int \uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5339\u914d\u5230 print(const int) \u3002 \u6269\u5c55\u9605\u8bfb \u5c06\u975e\u5f15\u7528\u7c7b\u578b\u8f6c\u6362\u4e3a\u5f15\u7528\u7c7b\u578b\uff08\u6216\u8005\u53cd\u8fc7\u6765\uff09\u4e5f\u5c5e\u4e8e[[[[trivial conversion|\u7b80\u5355\u8f6c\u6362]]\u3002 \u901a\u8fc7 \u7b80\u5355\u8f6c\u6362 \u5339\u914d\u5230\u7684\u91cd\u8f7d\u51fd\u6570\uff0c\u4e5f\u5c5e\u4e8e \u5b8c\u5168\u5339\u914d \u3002 \u7b2c\u4e8c\u6b65\uff1a\u5982\u679c\u6ca1\u6709\u627e\u5230\u5b8c\u5168\u5339\u914d\u7684\u91cd\u8f7d\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u5bf9\u5b9e\u53c2\u8fdb\u884c \u6570\u503c\u63d0\u5347 \uff0c\u5728 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u4e2d\u6211\u4ecb\u7ecd\u8fc7\uff0c\u5bbd\u5ea6\u8f83\u7a84\u7684\u6574\u578b\u548c\u6d6e\u70b9\u578b\u6570\u503c\u662f\u5982\u4f55\u88ab\u81ea\u52a8\u63d0\u5347\u4e3a\u8f83\u5bbd\u7684\u7c7b\u578b\u7684\uff0c\u4f8b\u5982 int \u6216 double \u3002\u5982\u679c\u6570\u503c\u63d0\u5347\u4e4b\u540e\u80fd\u591f\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\uff0c\u5219\u5b8c\u6210\u51fd\u6570\u8c03\u7528\u89e3\u6790\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 void print ( int ) { } void print ( double ) { } int main () { print ( 'a' ); // promoted to match print(int) print ( true ); // promoted to match print(int) print ( 4.5f ); // promoted to match print(double) return 0 ; } \u5bf9\u4e8e print('a') \u6765\u8bf4\uff0c\u7f16\u8bd1\u5668\u5728\u7b2c\u4e00\u6b65\u4e2d\u4e0d\u80fd\u627e\u5230\u80fd\u591f \u5b8c\u5168\u5339\u914d \u5b83\u7684\u91cd\u8f7d\u51fd\u6570 print(char) \uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u5c06\u5b57\u7b26 'a' \u63d0\u5347\u4e3a\u4e00\u4e2a\u6574\u578b\u5e76\u518d\u6b21\u67e5\u8fdb\u884c\u5339\u914d\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u5339\u914d\u5230 print(int) \uff0c\u6240\u4ee5\u51fd\u6570\u8c03\u7528\u89e3\u6790\u4e3a print(int) \u3002 \u7b2c\u4e09\u6b65\uff1a\u5982\u679c\u901a\u8fc7\u6570\u503c\u63d0\u5347\u4ecd\u4e0d\u80fd\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u5bf9\u51fd\u6570\u5b9e\u53c2\u8fdb\u884c \u6570\u503c\u8f6c\u6362 \uff08\u53c2\u89c1\uff1a 8.3 - \u6570\u503c\u8f6c\u6362 \uff09\u540e\uff0c\u518d\u6b21\u8fdb\u884c\u5339\u914d\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // for std::string void print ( double ) { } void print ( std :: string ) { } int main () { print ( 'a' ); // 'a' converted to match print(double) return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u4e0d\u5b58\u5728 print(char) (\u5b8c\u5168\u5339\u914d)\uff0c\u4e5f\u4e0d\u5b58\u5728 print(int) (\u63d0\u5347\u5339\u914d)\uff0c\u6240\u4ee5 'a' \u4f1a\u88ab\u6570\u503c\u8f6c\u6362\u4e3a double \u7c7b\u578b\u5e76\u5339\u914d print(double) \u3002 \u5173\u952e\u4fe1\u606f \u901a\u8fc7\u6570\u503c\u63d0\u5347\u8fdb\u884c\u5339\u914d\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e\u901a\u8fc7\u6570\u503c\u8f6c\u6362\u5339\u914d\u7684\u4f18\u5148\u7ea7\u3002 \u7b2c\u56db\u6b65\uff1a\u5982\u679c\u901a\u8fc7\u6570\u503c\u8f6c\u6362\u4ecd\u7136\u6ca1\u80fd\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u4f7f\u7528\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u8f6c\u6362\u3002\u867d\u7136\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u4ec0\u4e48\u662f\u7528\u6237\u81ea\u5b9a\u4e49\u8f6c\u6362\uff0c\u5176\u5b9e\u5b83\u5c31\u662f\u4e00\u7ec4\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u578b\u4e4b\u95f4\u7684\u9690\u5f0f\u8f6c\u6362\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // \u6211\u4eec\u8fd8\u6ca1\u8bb2\u5230\u7c7b\uff0c\u6240\u4ee5\u770b\u4e0d\u61c2\u4e5f\u4e0d\u7528\u62c5\u5fc3 class X // \u5b9a\u4e49\u4e86\u4e00\u4e2a\u65b0\u7c7b\u578b X { public : operator int () { return 0 ; } // \u7528\u6237\u81ea\u5b9a\u4e49\u7684\u5c06 X \u8f6c\u6362\u4e3a int \u7684\u89c4\u5219 }; void print ( int ) { } void print ( double ) { } int main () { X x ; // \u521b\u5efa X \u7c7b\u578b\u7684\u53d8\u91cf x print ( x ); // x \u901a\u8fc7\u7528\u6237\u81ea\u5b9a\u4e49\u8f6c\u6362\u4ece X \u8f6c\u6362\u4e3a int return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u68c0\u67e5\u662f\u5426\u80fd\u591f\u5b8c\u5168\u5339\u914d print(X) \u3002\u7531\u4e8e\u6ca1\u6709\u627e\u5230\uff0c\u7f16\u8bd1\u5668\u4f1a\u5bf9 x \u8fdb\u884c\u6570\u503c\u63d0\u5347\uff0c\u4f46\u662f\u8fd9\u4e00\u6b65\u65e0\u6cd5\u5b8c\u6210\uff0c\u7136\u540e\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u5bf9 x \u8fdb\u884c\u6570\u503c\u8f6c\u6362\uff0c\u540c\u6837\u4e0d\u80fd\u5b8c\u6210\u3002\u6700\u540e\uff0c\u7f16\u8bd1\u5668\u4f1a\u67e5\u627e\u662f\u5426\u5b58\u5728\u7528\u6237\u5b9a\u4e49\u7684\u8f6c\u6362\uff0c\u56e0\u4e3a\u6211\u4eec\u5b9a\u4e49\u4e86 X \u7c7b\u578b\u8f6c\u6362\u4e3a int \u7c7b\u578b\u7684\u89c4\u5219\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u4f7f\u7528\u8be5\u89c4\u5219\u5c06 X \u8f6c\u6362\u4e3a int \uff0c\u5e76\u5339\u914d print(int) \u3002 \u5728\u8fdb\u884c\u7528\u6237\u81ea\u5b9a\u4e49\u8f6c\u6362\u540e\uff0c\u7f16\u8bd1\u5668\u8fd8\u53ef\u80fd\u5c1d\u8bd5\u989d\u5916\u7684\u9690\u5f0f\u63d0\u5347\u6216\u8f6c\u6362\u6765\u67e5\u627e\u5339\u914d\uff0c\u6240\u4ee5\u5982\u679c\u6211\u4eec\u5b9a\u4e49\u7684\u8f6c\u6362\u89c4\u5219\u662f\u5c06 X \u8f6c\u6362\u4e3a char \u800c\u4e0d\u662f int \uff0c\u7f16\u8bd1\u5668\u4f1a\u7ee7\u7eed\u5bf9 char \u8fdb\u884c\u6570\u503c\u63d0\u5347\u4f7f\u5176\u63d0\u5347\u4e3a int \u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26 \u4e2d\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26\u6765\u521b\u5efa\u7528\u6237\u81ea\u5b9a\u4e49\u8f6c\u6362\u3002 \u6269\u5c55\u9605\u8bfb \u7c7b\u7684 \u6784\u9020\u51fd\u6570(constructor) \u540c\u6837\u5177\u6709\u81ea\u5b9a\u4e49\u8f6c\u6362\u7684\u529f\u80fd\uff0c\u5728\u4e5f\u53ef\u4ee5\u88ab\u7528\u5728\u7b2c\u56db\u6b65\u3002 \u7b2c\u4e94\u6b65\uff1a\u5982\u679c\u901a\u8fc7\u7528\u6237\u81ea\u5b9a\u4e49\u8f6c\u6362\u540e\u4ecd\u7136\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u91cd\u8f7d\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u5339\u914d\u4f7f\u7528\u4e86\u7701\u7565\u53f7\u7684\u51fd\u6570\u3002 \u76f8\u5173\u5185\u5bb9 \u7701\u7565\u53f7\u7684\u4f7f\u7528\u4f1a\u5728 12.6 - \u7701\u7565\u53f7\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 \u4e2d\u4ecb\u7ecd\u3002 \u7b2c\u516d\u6b65\uff1a \u5982\u679c\u5230\u6b64\u8fd8\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u653e\u5f03\u7ee7\u7eed\u67e5\u627e\u5e76\u4ea7\u751f\u4e00\u4e2a\u6ca1\u6709\u627e\u5230\u5339\u914d\u51fd\u6570\u7684\u7f16\u8bd1\u9519\u8bef\u3002 \u4e0d\u660e\u786e\u5339\u914d \u5728\u6ca1\u6709\u91cd\u8f7d\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u8981\u4e48\u89e3\u6790\u4e3a\u4e00\u4e2a\u51fd\u6570\uff0c\u8981\u4e48\u56e0\u4e3a\u627e\u4e0d\u5230\u800c\u4ea7\u751f\u7f16\u8bd1\u62a5\u9519\uff1a 1 2 3 4 5 6 7 8 9 10 11 void foo () { } int main () { foo (); // okay: match found goo (); // compile error: no match found return 0 ; } \u800c\u5728\u5b58\u5728\u91cd\u8f7d\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd8\u6709\u7b2c\u4e09\u79cd\u53ef\u80fd\uff1a \u4e0d\u660e\u786e\u5339\u914d(ambiguous match) \u3002\u5f53\u7f16\u8bd1\u5668\u5728\u4e00\u4e2a\u6b65\u9aa4\u4e2d\u627e\u5230\u4e24\u4e2a\u6216\u4e24\u4e2a\u4ee5\u4e0a\u5339\u914d\u7684\u91cd\u8f7d\u51fd\u6570\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u505c\u6b62\u5339\u914d\u5e76\u62a5\u544a\u627e\u5230\u4e0d\u660e\u786e\u5339\u914d\u51fd\u6570\u8c03\u7528\u3002 \u4e3a\u4e86\u80fd\u591f\u8fdb\u884c\u7f16\u8bd1\uff0c\u7f16\u8bd1\u5668\u5fc5\u987b\u533a\u5206\u6bcf\u4e2a\u91cd\u8f7d\u51fd\u6570\uff0c\u90a3\u4e48\uff0c\u4ec0\u4e48\u60c5\u51b5\u4e0b\u4f1a\u51fa\u73b0\u591a\u4e2a\u5339\u914d\u7684\u60c5\u51b5\u5462\uff1f\u8bf7\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 void print ( int x ) { } void print ( double d ) { } int main () { print ( 5L ); // 5L is type long return 0 ; } \u5b57\u9762\u91cf 5L \u662f long \u578b\u7684\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u67e5\u627e\u662f\u5426\u5b58\u5728\u5b8c\u5168\u5339\u914d\u7684 print(long) \uff0c\u53ef\u60dc\u6ca1\u80fd\u627e\u5230\u3002\u63a5\u4e0b\u6765\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u6570\u503c\u63d0\u5347\uff0c\u4f46\u662f long \u5df2\u7ecf\u4e0d\u80fd\u63d0\u5347\u4e86\uff0c\u6240\u4ee5\u4ecd\u7136\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\u3002 \u63a5\u4e0b\u6765\uff0c\u7f16\u8bd1\u5668\u4f1a\u5bf9 long \u7c7b\u578b\u7684\u5b9e\u53c2\u8fdb\u884c\u6570\u503c\u8f6c\u6362\u3002\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u5b83\u4f1a\u5c1d\u8bd5\u6240\u6709\u7684\u6570\u503c\u8f6c\u6362\u89c4\u5219\uff0c\u6b64\u65f6\u7f16\u8bd1\u5668\u4f1a\u627e\u5230\u4e24\u4e2a\u53ef\u4ee5\u80fd\u7684\u5339\u914d\u3002\u5982\u679c long \u5b9e\u53c2\u901a\u8fc7\u6570\u503c\u8f6c\u6362\u8f6c\u6362\u6210\u4e86 int \uff0c\u90a3\u4e48\u53ef\u4ee5\u5339\u914d print(int) \u3002\u5982\u679c long \u5b9e\u53c2\u8f6c\u6362\u6210\u4e86 double \uff0c\u5219\u53ef\u4ee5\u5339\u914d print(double) \u3002\u56e0\u4e3a\u627e\u5230\u4e86\u4e24\u79cd\u53ef\u80fd\u7684\u5339\u914d\uff0c\u6240\u4ee5\u8be5\u51fd\u6570\u8c03\u7528\u88ab\u8ba4\u4e3a\u662f\u4e0d\u660e\u786e\u7684\u3002 \u5728 Visual Studio 2019 \u4e0a\u4f1a\u4ea7\u751f\u5982\u4e0b\u62a5\u9519\u4fe1\u606f\uff1a 1 2 3 4 error C2668: 'print': ambiguous call to overloaded function message : could be 'void print(double)' message : or 'void print(int)' message : while trying to match the argument list '(long)' \u5173\u952e\u4fe1\u606f \u5982\u679c\u7f16\u8bd1\u5668\u5728\u67d0\u4e00\u6b65\u627e\u5230\u4e86\u591a\u4e2a\u53ef\u80fd\u7684\u5339\u914d\uff0c\u5219\u4f1a\u5bfc\u81f4\u4e0d\u660e\u786e\u5339\u914d\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u8fd9\u4e00\u6b65\u4e2d\u7f16\u8bd1\u5668\u6ca1\u80fd\u627e\u5230\u4e00\u4e2a\u6bd4\u5176\u4ed6\u5339\u914d\u90fd\u597d\u7684\u5339\u914d\u3002 \u518d\u6765\u770b\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 void print ( unsigned int x ) { } void print ( float y ) { } int main () { print ( 0 ); // int \u53ef\u4ee5\u88ab\u6570\u503c\u8f6c\u6362\u4e3a unsigned int \u6216 float print ( 3.14159 ); // double \u53ef\u4ee5\u88ab\u6570\u503c\u8f6c\u6362\u4e3a unsigned int \u6216 float return 0 ; } \u4e5f\u8bb8\u4f60\u4f1a\u8ba4\u4e3a 0 \u4f1a\u88ab\u89e3\u6790\u4e3a unsigned int \u8fdb\u800c\u5339\u914d print(unsigned int) \u3002\u540c\u65f6 3.14159 \u89e3\u6790\u4e3a float \u8fdb\u800c\u5339\u914d print(float) \u3002\u5176\u5b9e\u5b83\u4eec\u90fd\u4f1a\u4ea7\u751f\u4e0d\u660e\u786e\u5339\u914d\u3002 int \u503c 0 \u53ef\u4ee5\u901a\u8fc7\u6570\u503c\u8f6c\u6362\u88ab\u8f6c\u6362\u4e3a unsigned int \u6216\u8005 float \uff0c\u6240\u4ee5\u4e24\u4e2a\u51fd\u6570\u5b83\u90fd\u80fd\u5339\u914d\uff0c\u8fd9\u52bf\u5fc5\u5c06\u5bfc\u81f4\u4e0d\u660e\u786e\u5339\u914d\u4ea7\u751f\u3002 \u5bf9\u4e8e double \u7c7b\u578b\u4e5f\u4e00\u6837\uff0c\u5b83\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a float \u6216\u8005 unsigned int \u3002\u4e0d\u8bba\u5982\u4f55\u8f6c\u6362\uff0c\u90fd\u5c5e\u4e8e\u6570\u503c\u8f6c\u6362\uff0c\u4e5f\u90fd\u80fd\u5339\u914d\u4e24\u4e2a\u51fd\u6570\uff0c\u6240\u4ee5\u662f\u4e0d\u660e\u786e\u5339\u914d\u3002 \u5904\u7406\u4e0d\u660e\u786e\u5339\u914d \u56e0\u4e3a\u4e0d\u660e\u786e\u5339\u914d\u662f\u7f16\u8bd1\u65f6\u9519\u8bef\uff0c\u6240\u4ee5\u5728\u7a0b\u5e8f\u7f16\u8bd1\u4e4b\u524d\uff0c\u9700\u8981\u6d88\u9664\u6b67\u4e49\u3002\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u89e3\u51b3\u8be5\u95ee\u9898\uff1a \u901a\u5e38\uff0c\u6700\u597d\u7684\u65b9\u6cd5\u662f\u7b80\u5355\u5730\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u91cd\u8f7d\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u7684\u5f62\u53c2\u4e0e\u60a8\u8bd5\u56fe\u8c03\u7528\u8be5\u51fd\u6570\u65f6\u4f7f\u7528\u7684\u5f62\u53c2\u7c7b\u578b\u5b8c\u5168\u76f8\u540c\u3002\u7136\u540eC++\u5c31\u80fd\u627e\u5230\u4e0e\u51fd\u6570\u8c03\u7528\u5b8c\u5168\u5339\u914d\u7684\u5bf9\u8c61\u3002 \u6216\u8005\uff0c \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u8f6c\u6362\u4e0d\u660e\u786e\u7684\u53c2\u6570\uff0c\u4ee5\u5339\u914d\u8981\u8c03\u7528\u7684\u51fd\u6570\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u8981\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f print(0) \u5339\u914d print(unsigned int) \uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a: 1 2 int x { 0 }; print ( static_cast < unsigned int > ( x )); // will call print(unsigned int) \u5982\u679c\u53c2\u6570\u662f\u5b57\u9762\u91cf\uff0c\u4f60\u53ef\u4ee5\u4e3a\u5b83\u6dfb\u52a0\u5b57\u9762\u91cf\u540e\u7f00\uff0c\u4f7f\u5176\u80fd\u591f\u88ab\u89e3\u6790\u4e3a\u6b63\u786e\u7684\u7c7b\u578b\uff1a 1 print ( 0u ); // will call print(unsigned int) since 'u' suffix is unsigned int, so this is now an exact match \u5e38\u7528\u7684\u540e\u7f00\u53c2\u89c1\uff1a 4-15-Literals#\u5b57\u9762\u91cf\u540e\u7f00 \u591a\u53c2\u6570\u91cd\u8f7d\u51fd\u6570\u7684\u5339\u914d \u5982\u679c\u6709\u591a\u4e2a\u53c2\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u4f9d\u6b21\u5bf9\u6bcf\u4e2a\u53c2\u6570\u5e94\u7528\u5339\u914d\u89c4\u5219\u3002\u6700\u7ec8\u5339\u914d\u7684\u51fd\u6570\u7684\u8981\u6c42\u662f\uff1a\u6bcf\u4e2a\u53c2\u6570\u90fd\u5339\u914d\u4e14\u81f3\u5c11\u6709\u4e00\u4e2a\u53c2\u6570\u6bd4\u6240\u6709\u5176\u4ed6\u51fd\u6570\u5339\u914d\u5f97\u66f4\u597d\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6240\u9009\u51fd\u6570\u5fc5\u987b\u5bf9\u81f3\u5c11\u4e00\u4e2a\u53c2\u6570\u63d0\u4f9b\u6bd4\u6240\u6709\u5176\u4ed6\u5019\u9009\u51fd\u6570\u66f4\u597d\u7684\u5339\u914d\uff0c\u800c\u5bf9\u6240\u6709\u5176\u4ed6\u53c2\u6570\u4e0d\u5dee\u3002 \u5728\u627e\u5230\u8fd9\u6837\u4e00\u4e2a\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u5b83\u663e\u7136\u662f\u6700\u597d\u7684\u9009\u62e9\u3002\u5982\u679c\u627e\u4e0d\u5230\u8fd9\u6837\u7684\u51fd\u6570\uff0c\u5219\u8be5\u8c03\u7528\u5c06\u88ab\u89c6\u4e3a\u4e8c\u4e49\u6027(\u6216\u4e0d\u5339\u914d)\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include void print ( char c , int x ) { std :: cout << 'a' ; } void print ( char c , double x ) { std :: cout << 'b' ; } void print ( char c , float x ) { std :: cout << 'c' ; } int main () { print ( 'x' , 'a' ); } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u90fd\u80fd\u591f \u5b8c\u5168\u5339\u914d \u3002\u7b2c\u4e00\u4e2a\u51fd\u6570\u901a\u8fc7 \u6570\u503c\u63d0\u5347 \u53ef\u4ee5\u5339\u914d\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u5176\u4ed6\u51e0\u4e2a\u51fd\u6570\u5219\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u624d\u80fd\u5339\u914d\u3002\u56e0\u6b64 print(char, int) \u6bcb\u5eb8\u7f6e\u7591\u662f\u6700\u4f73\u5339\u914d\u51fd\u6570\u3002","title":"8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u4e0d\u660e\u786e\u5339\u914d"},{"location":"8-11-Function-overload-resolution-and-ambiguous-matches/#_1","text":"\u5f53\u5bf9\u91cd\u8f7d\u51fd\u6570\u8fdb\u884c\u8c03\u7528\u65f6\uff0c\u7f16\u8bd1\u5668\u901a\u8fc7\u4e00\u7cfb\u5217\u89c4\u5219\u548c\u6b65\u9aa4\u786e\u5b9a\u91cd\u8f7d\u51fd\u6570\u7684\u6700\u4f73\u5339\u914d(\u5982\u679c\u6709\u7684\u8bdd)\u3002 \u5728\u6bcf\u4e2a\u6b65\u9aa4\u4e2d\uff0c\u7f16\u8bd1\u5668\u5bf9\u51fd\u6570\u8c03\u7528\u4e2d\u7684\u5b9e\u53c2\u5e94\u7528\u4e00\u7cfb\u5217\u4e0d\u540c\u7684\u7c7b\u578b\u8f6c\u6362\u3002\u5bf9\u4e8e\u5e94\u7528\u7684\u6bcf\u4e2a\u8f6c\u6362\uff0c\u7f16\u8bd1\u5668\u68c0\u67e5\u91cd\u8f7d\u7684\u51fd\u6570\u73b0\u5728\u662f\u5426\u5339\u914d\u3002\u5728\u5e94\u7528\u4e86\u6240\u6709\u4e0d\u540c\u7684\u7c7b\u578b\u8f6c\u6362\u5e76\u68c0\u67e5\u4e86\u5339\u914d\u4e4b\u540e\uff0c\u8fd9\u4e00\u6b65\u5c31\u5b8c\u6210\u4e86\u3002\u7ed3\u679c\u5c06\u662f\u4ee5\u4e0b\u4e09\u79cd\u53ef\u80fd\u7684\u7ed3\u679c\u4e4b\u4e00\uff1a \u672a\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\u3002\u7f16\u8bd1\u5668\u8fdb\u5165\u4e0b\u4e00\u4e2a\u6b65\u9aa4\u3002 \u627e\u5230\u5355\u4e2a\u5339\u914d\u51fd\u6570\u3002\u8be5\u51fd\u6570\u88ab\u8ba4\u4e3a\u662f\u6700\u4f73\u5339\u914d\u3002\u5339\u914d\u8fc7\u7a0b\u73b0\u5728\u5df2\u7ecf\u5b8c\u6210\uff0c\u540e\u7eed\u6b65\u9aa4\u4e0d\u518d\u6267\u884c\u3002 \u53d1\u73b0\u591a\u4e2a\u5339\u914d\u51fd\u6570\u3002\u7f16\u8bd1\u5668\u5c06\u53d1\u51fa\u4e00\u4e2a \u4e0d\u660e\u786e\u5339\u914d \u7684\u7f16\u8bd1\u9519\u8bef\u3002\u6211\u4eec\u7a0d\u540e\u5c06\u8fdb\u4e00\u6b65\u8ba8\u8bba\u8fd9\u79cd\u60c5\u51b5\u3002 \u5982\u679c\u7f16\u8bd1\u5668\u6267\u884c\u5b8c\u4e00\u7cfb\u5217\u6b65\u9aa4\u540e\uff0c\u4ecd\u7136\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\uff0c\u5b83\u5c06\u751f\u6210\u4e00\u4e2a\u7f16\u8bd1\u9519\u8bef\uff0c\u5373\u65e0\u6cd5\u4e3a\u51fd\u6570\u8c03\u7528\u627e\u5230\u5339\u914d\u7684\u91cd\u8f7d\u51fd\u6570\u3002","title":"\u89e3\u6790\u91cd\u8f7d\u51fd\u6570\u8c03\u7528"},{"location":"8-11-Function-overload-resolution-and-ambiguous-matches/#_2","text":"\u7b2c\u4e00\u6b65\uff1a\u7f16\u8bd1\u5668\u9996\u5148\u5c1d\u8bd5\u5bf9\u91cd\u8f7d\u51fd\u6570\u8fdb\u884c \u5b8c\u5168\u5339\u914d \u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5206\u4e3a\u4e24\u4e2a\u9636\u6bb5\u3002\u9996\u5148\uff0c\u7f16\u8bd1\u5668\u4f1a\u67e5\u627e\u662f\u5426\u5b58\u5728\u4e00\u4e2a\u91cd\u8f7d\u51fd\u6570\uff0c\u5176\u8c03\u7528\u65f6\u7684\u5b9e\u53c2\u5b8c\u5168\u5339\u914d\u91cd\u8f7d\u51fd\u6570\u7684\u5f62\u53c2\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 void print ( int ) { } void print ( double ) { } int main () { print ( 0 ); // exact match with print(int) print ( 3.4 ); // exact match with print(double) return 0 ; } \u56e0\u4e3a print(0) \u4e2d\u7684 0 \u662f\u4e00\u4e2a\u6574\u578b\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u67e5\u627e\u662f\u5426\u5b58\u5728 print(int) \u91cd\u8f7d\u51fd\u6570\u3002\u56e0\u4e3a\u7684\u786e\u5b58\u5728\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u53ef\u4ee5\u786e\u5b9a\u8be5\u91cd\u8f7d\u51fd\u6570\u4e3a\u88ab\u8c03\u7528\u51fd\u6570\u7684\u7cbe\u786e\u5339\u914d\u3002 \u5176\u6b21\uff0c\u7f16\u8bd1\u5668\u4f1a\u5bf9\u51fd\u6570\u8c03\u7528\u65f6\u7684\u5b9e\u53c2\u8fdb\u884c\u4e00\u7cfb\u5217\u7684 trivial-conversion \u3002\u8fd9\u4e9b\u7b80\u5355\u7684\u8f6c\u6362\u4f1a\u901a\u8fc7\u4fee\u6539\u7c7b\u578b\uff08\u4e0d\u4fee\u6539\u503c\uff09\u7684\u65b9\u5f0f\u67e5\u627e\u53ef\u80fd\u7684\u5339\u914d\u3002\u4f8b\u5982\uff0c\u975e const \u7c7b\u578b\u53ef\u80fd\u4f1a\u88ab\u8f6c\u6362\u4e3a const \u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 void print ( const int ) { } void print ( double ) { } int main () { int x { 0 }; print ( x ); // x trivially converted to const int return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8c03\u7528 print(x) \u65f6\uff0c x \u662f\u4e00\u4e2a int \u3002\u7f16\u8bd1\u5668\u4f1a\u5c06\u5176\u4ece int \u8f6c\u6362\u4e3a const int \uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5339\u914d\u5230 print(const int) \u3002 \u6269\u5c55\u9605\u8bfb \u5c06\u975e\u5f15\u7528\u7c7b\u578b\u8f6c\u6362\u4e3a\u5f15\u7528\u7c7b\u578b\uff08\u6216\u8005\u53cd\u8fc7\u6765\uff09\u4e5f\u5c5e\u4e8e[[[[trivial conversion|\u7b80\u5355\u8f6c\u6362]]\u3002 \u901a\u8fc7 \u7b80\u5355\u8f6c\u6362 \u5339\u914d\u5230\u7684\u91cd\u8f7d\u51fd\u6570\uff0c\u4e5f\u5c5e\u4e8e \u5b8c\u5168\u5339\u914d \u3002 \u7b2c\u4e8c\u6b65\uff1a\u5982\u679c\u6ca1\u6709\u627e\u5230\u5b8c\u5168\u5339\u914d\u7684\u91cd\u8f7d\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u5bf9\u5b9e\u53c2\u8fdb\u884c \u6570\u503c\u63d0\u5347 \uff0c\u5728 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u4e2d\u6211\u4ecb\u7ecd\u8fc7\uff0c\u5bbd\u5ea6\u8f83\u7a84\u7684\u6574\u578b\u548c\u6d6e\u70b9\u578b\u6570\u503c\u662f\u5982\u4f55\u88ab\u81ea\u52a8\u63d0\u5347\u4e3a\u8f83\u5bbd\u7684\u7c7b\u578b\u7684\uff0c\u4f8b\u5982 int \u6216 double \u3002\u5982\u679c\u6570\u503c\u63d0\u5347\u4e4b\u540e\u80fd\u591f\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\uff0c\u5219\u5b8c\u6210\u51fd\u6570\u8c03\u7528\u89e3\u6790\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 void print ( int ) { } void print ( double ) { } int main () { print ( 'a' ); // promoted to match print(int) print ( true ); // promoted to match print(int) print ( 4.5f ); // promoted to match print(double) return 0 ; } \u5bf9\u4e8e print('a') \u6765\u8bf4\uff0c\u7f16\u8bd1\u5668\u5728\u7b2c\u4e00\u6b65\u4e2d\u4e0d\u80fd\u627e\u5230\u80fd\u591f \u5b8c\u5168\u5339\u914d \u5b83\u7684\u91cd\u8f7d\u51fd\u6570 print(char) \uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u5c06\u5b57\u7b26 'a' \u63d0\u5347\u4e3a\u4e00\u4e2a\u6574\u578b\u5e76\u518d\u6b21\u67e5\u8fdb\u884c\u5339\u914d\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u5339\u914d\u5230 print(int) \uff0c\u6240\u4ee5\u51fd\u6570\u8c03\u7528\u89e3\u6790\u4e3a print(int) \u3002 \u7b2c\u4e09\u6b65\uff1a\u5982\u679c\u901a\u8fc7\u6570\u503c\u63d0\u5347\u4ecd\u4e0d\u80fd\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u5bf9\u51fd\u6570\u5b9e\u53c2\u8fdb\u884c \u6570\u503c\u8f6c\u6362 \uff08\u53c2\u89c1\uff1a 8.3 - \u6570\u503c\u8f6c\u6362 \uff09\u540e\uff0c\u518d\u6b21\u8fdb\u884c\u5339\u914d\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // for std::string void print ( double ) { } void print ( std :: string ) { } int main () { print ( 'a' ); // 'a' converted to match print(double) return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u4e0d\u5b58\u5728 print(char) (\u5b8c\u5168\u5339\u914d)\uff0c\u4e5f\u4e0d\u5b58\u5728 print(int) (\u63d0\u5347\u5339\u914d)\uff0c\u6240\u4ee5 'a' \u4f1a\u88ab\u6570\u503c\u8f6c\u6362\u4e3a double \u7c7b\u578b\u5e76\u5339\u914d print(double) \u3002 \u5173\u952e\u4fe1\u606f \u901a\u8fc7\u6570\u503c\u63d0\u5347\u8fdb\u884c\u5339\u914d\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e\u901a\u8fc7\u6570\u503c\u8f6c\u6362\u5339\u914d\u7684\u4f18\u5148\u7ea7\u3002 \u7b2c\u56db\u6b65\uff1a\u5982\u679c\u901a\u8fc7\u6570\u503c\u8f6c\u6362\u4ecd\u7136\u6ca1\u80fd\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u4f7f\u7528\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u8f6c\u6362\u3002\u867d\u7136\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u4ec0\u4e48\u662f\u7528\u6237\u81ea\u5b9a\u4e49\u8f6c\u6362\uff0c\u5176\u5b9e\u5b83\u5c31\u662f\u4e00\u7ec4\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u578b\u4e4b\u95f4\u7684\u9690\u5f0f\u8f6c\u6362\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // \u6211\u4eec\u8fd8\u6ca1\u8bb2\u5230\u7c7b\uff0c\u6240\u4ee5\u770b\u4e0d\u61c2\u4e5f\u4e0d\u7528\u62c5\u5fc3 class X // \u5b9a\u4e49\u4e86\u4e00\u4e2a\u65b0\u7c7b\u578b X { public : operator int () { return 0 ; } // \u7528\u6237\u81ea\u5b9a\u4e49\u7684\u5c06 X \u8f6c\u6362\u4e3a int \u7684\u89c4\u5219 }; void print ( int ) { } void print ( double ) { } int main () { X x ; // \u521b\u5efa X \u7c7b\u578b\u7684\u53d8\u91cf x print ( x ); // x \u901a\u8fc7\u7528\u6237\u81ea\u5b9a\u4e49\u8f6c\u6362\u4ece X \u8f6c\u6362\u4e3a int return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u68c0\u67e5\u662f\u5426\u80fd\u591f\u5b8c\u5168\u5339\u914d print(X) \u3002\u7531\u4e8e\u6ca1\u6709\u627e\u5230\uff0c\u7f16\u8bd1\u5668\u4f1a\u5bf9 x \u8fdb\u884c\u6570\u503c\u63d0\u5347\uff0c\u4f46\u662f\u8fd9\u4e00\u6b65\u65e0\u6cd5\u5b8c\u6210\uff0c\u7136\u540e\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u5bf9 x \u8fdb\u884c\u6570\u503c\u8f6c\u6362\uff0c\u540c\u6837\u4e0d\u80fd\u5b8c\u6210\u3002\u6700\u540e\uff0c\u7f16\u8bd1\u5668\u4f1a\u67e5\u627e\u662f\u5426\u5b58\u5728\u7528\u6237\u5b9a\u4e49\u7684\u8f6c\u6362\uff0c\u56e0\u4e3a\u6211\u4eec\u5b9a\u4e49\u4e86 X \u7c7b\u578b\u8f6c\u6362\u4e3a int \u7c7b\u578b\u7684\u89c4\u5219\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u4f7f\u7528\u8be5\u89c4\u5219\u5c06 X \u8f6c\u6362\u4e3a int \uff0c\u5e76\u5339\u914d print(int) \u3002 \u5728\u8fdb\u884c\u7528\u6237\u81ea\u5b9a\u4e49\u8f6c\u6362\u540e\uff0c\u7f16\u8bd1\u5668\u8fd8\u53ef\u80fd\u5c1d\u8bd5\u989d\u5916\u7684\u9690\u5f0f\u63d0\u5347\u6216\u8f6c\u6362\u6765\u67e5\u627e\u5339\u914d\uff0c\u6240\u4ee5\u5982\u679c\u6211\u4eec\u5b9a\u4e49\u7684\u8f6c\u6362\u89c4\u5219\u662f\u5c06 X \u8f6c\u6362\u4e3a char \u800c\u4e0d\u662f int \uff0c\u7f16\u8bd1\u5668\u4f1a\u7ee7\u7eed\u5bf9 char \u8fdb\u884c\u6570\u503c\u63d0\u5347\u4f7f\u5176\u63d0\u5347\u4e3a int \u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26 \u4e2d\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26\u6765\u521b\u5efa\u7528\u6237\u81ea\u5b9a\u4e49\u8f6c\u6362\u3002 \u6269\u5c55\u9605\u8bfb \u7c7b\u7684 \u6784\u9020\u51fd\u6570(constructor) \u540c\u6837\u5177\u6709\u81ea\u5b9a\u4e49\u8f6c\u6362\u7684\u529f\u80fd\uff0c\u5728\u4e5f\u53ef\u4ee5\u88ab\u7528\u5728\u7b2c\u56db\u6b65\u3002 \u7b2c\u4e94\u6b65\uff1a\u5982\u679c\u901a\u8fc7\u7528\u6237\u81ea\u5b9a\u4e49\u8f6c\u6362\u540e\u4ecd\u7136\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u91cd\u8f7d\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u5339\u914d\u4f7f\u7528\u4e86\u7701\u7565\u53f7\u7684\u51fd\u6570\u3002 \u76f8\u5173\u5185\u5bb9 \u7701\u7565\u53f7\u7684\u4f7f\u7528\u4f1a\u5728 12.6 - \u7701\u7565\u53f7\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 \u4e2d\u4ecb\u7ecd\u3002 \u7b2c\u516d\u6b65\uff1a \u5982\u679c\u5230\u6b64\u8fd8\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u653e\u5f03\u7ee7\u7eed\u67e5\u627e\u5e76\u4ea7\u751f\u4e00\u4e2a\u6ca1\u6709\u627e\u5230\u5339\u914d\u51fd\u6570\u7684\u7f16\u8bd1\u9519\u8bef\u3002","title":"\u53c2\u6570\u5339\u914d\u987a\u5e8f"},{"location":"8-11-Function-overload-resolution-and-ambiguous-matches/#_3","text":"\u5728\u6ca1\u6709\u91cd\u8f7d\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u8981\u4e48\u89e3\u6790\u4e3a\u4e00\u4e2a\u51fd\u6570\uff0c\u8981\u4e48\u56e0\u4e3a\u627e\u4e0d\u5230\u800c\u4ea7\u751f\u7f16\u8bd1\u62a5\u9519\uff1a 1 2 3 4 5 6 7 8 9 10 11 void foo () { } int main () { foo (); // okay: match found goo (); // compile error: no match found return 0 ; } \u800c\u5728\u5b58\u5728\u91cd\u8f7d\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd8\u6709\u7b2c\u4e09\u79cd\u53ef\u80fd\uff1a \u4e0d\u660e\u786e\u5339\u914d(ambiguous match) \u3002\u5f53\u7f16\u8bd1\u5668\u5728\u4e00\u4e2a\u6b65\u9aa4\u4e2d\u627e\u5230\u4e24\u4e2a\u6216\u4e24\u4e2a\u4ee5\u4e0a\u5339\u914d\u7684\u91cd\u8f7d\u51fd\u6570\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u505c\u6b62\u5339\u914d\u5e76\u62a5\u544a\u627e\u5230\u4e0d\u660e\u786e\u5339\u914d\u51fd\u6570\u8c03\u7528\u3002 \u4e3a\u4e86\u80fd\u591f\u8fdb\u884c\u7f16\u8bd1\uff0c\u7f16\u8bd1\u5668\u5fc5\u987b\u533a\u5206\u6bcf\u4e2a\u91cd\u8f7d\u51fd\u6570\uff0c\u90a3\u4e48\uff0c\u4ec0\u4e48\u60c5\u51b5\u4e0b\u4f1a\u51fa\u73b0\u591a\u4e2a\u5339\u914d\u7684\u60c5\u51b5\u5462\uff1f\u8bf7\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 void print ( int x ) { } void print ( double d ) { } int main () { print ( 5L ); // 5L is type long return 0 ; } \u5b57\u9762\u91cf 5L \u662f long \u578b\u7684\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u67e5\u627e\u662f\u5426\u5b58\u5728\u5b8c\u5168\u5339\u914d\u7684 print(long) \uff0c\u53ef\u60dc\u6ca1\u80fd\u627e\u5230\u3002\u63a5\u4e0b\u6765\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u6570\u503c\u63d0\u5347\uff0c\u4f46\u662f long \u5df2\u7ecf\u4e0d\u80fd\u63d0\u5347\u4e86\uff0c\u6240\u4ee5\u4ecd\u7136\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\u3002 \u63a5\u4e0b\u6765\uff0c\u7f16\u8bd1\u5668\u4f1a\u5bf9 long \u7c7b\u578b\u7684\u5b9e\u53c2\u8fdb\u884c\u6570\u503c\u8f6c\u6362\u3002\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u5b83\u4f1a\u5c1d\u8bd5\u6240\u6709\u7684\u6570\u503c\u8f6c\u6362\u89c4\u5219\uff0c\u6b64\u65f6\u7f16\u8bd1\u5668\u4f1a\u627e\u5230\u4e24\u4e2a\u53ef\u4ee5\u80fd\u7684\u5339\u914d\u3002\u5982\u679c long \u5b9e\u53c2\u901a\u8fc7\u6570\u503c\u8f6c\u6362\u8f6c\u6362\u6210\u4e86 int \uff0c\u90a3\u4e48\u53ef\u4ee5\u5339\u914d print(int) \u3002\u5982\u679c long \u5b9e\u53c2\u8f6c\u6362\u6210\u4e86 double \uff0c\u5219\u53ef\u4ee5\u5339\u914d print(double) \u3002\u56e0\u4e3a\u627e\u5230\u4e86\u4e24\u79cd\u53ef\u80fd\u7684\u5339\u914d\uff0c\u6240\u4ee5\u8be5\u51fd\u6570\u8c03\u7528\u88ab\u8ba4\u4e3a\u662f\u4e0d\u660e\u786e\u7684\u3002 \u5728 Visual Studio 2019 \u4e0a\u4f1a\u4ea7\u751f\u5982\u4e0b\u62a5\u9519\u4fe1\u606f\uff1a 1 2 3 4 error C2668: 'print': ambiguous call to overloaded function message : could be 'void print(double)' message : or 'void print(int)' message : while trying to match the argument list '(long)' \u5173\u952e\u4fe1\u606f \u5982\u679c\u7f16\u8bd1\u5668\u5728\u67d0\u4e00\u6b65\u627e\u5230\u4e86\u591a\u4e2a\u53ef\u80fd\u7684\u5339\u914d\uff0c\u5219\u4f1a\u5bfc\u81f4\u4e0d\u660e\u786e\u5339\u914d\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u8fd9\u4e00\u6b65\u4e2d\u7f16\u8bd1\u5668\u6ca1\u80fd\u627e\u5230\u4e00\u4e2a\u6bd4\u5176\u4ed6\u5339\u914d\u90fd\u597d\u7684\u5339\u914d\u3002 \u518d\u6765\u770b\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 void print ( unsigned int x ) { } void print ( float y ) { } int main () { print ( 0 ); // int \u53ef\u4ee5\u88ab\u6570\u503c\u8f6c\u6362\u4e3a unsigned int \u6216 float print ( 3.14159 ); // double \u53ef\u4ee5\u88ab\u6570\u503c\u8f6c\u6362\u4e3a unsigned int \u6216 float return 0 ; } \u4e5f\u8bb8\u4f60\u4f1a\u8ba4\u4e3a 0 \u4f1a\u88ab\u89e3\u6790\u4e3a unsigned int \u8fdb\u800c\u5339\u914d print(unsigned int) \u3002\u540c\u65f6 3.14159 \u89e3\u6790\u4e3a float \u8fdb\u800c\u5339\u914d print(float) \u3002\u5176\u5b9e\u5b83\u4eec\u90fd\u4f1a\u4ea7\u751f\u4e0d\u660e\u786e\u5339\u914d\u3002 int \u503c 0 \u53ef\u4ee5\u901a\u8fc7\u6570\u503c\u8f6c\u6362\u88ab\u8f6c\u6362\u4e3a unsigned int \u6216\u8005 float \uff0c\u6240\u4ee5\u4e24\u4e2a\u51fd\u6570\u5b83\u90fd\u80fd\u5339\u914d\uff0c\u8fd9\u52bf\u5fc5\u5c06\u5bfc\u81f4\u4e0d\u660e\u786e\u5339\u914d\u4ea7\u751f\u3002 \u5bf9\u4e8e double \u7c7b\u578b\u4e5f\u4e00\u6837\uff0c\u5b83\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a float \u6216\u8005 unsigned int \u3002\u4e0d\u8bba\u5982\u4f55\u8f6c\u6362\uff0c\u90fd\u5c5e\u4e8e\u6570\u503c\u8f6c\u6362\uff0c\u4e5f\u90fd\u80fd\u5339\u914d\u4e24\u4e2a\u51fd\u6570\uff0c\u6240\u4ee5\u662f\u4e0d\u660e\u786e\u5339\u914d\u3002","title":"\u4e0d\u660e\u786e\u5339\u914d"},{"location":"8-11-Function-overload-resolution-and-ambiguous-matches/#_4","text":"\u56e0\u4e3a\u4e0d\u660e\u786e\u5339\u914d\u662f\u7f16\u8bd1\u65f6\u9519\u8bef\uff0c\u6240\u4ee5\u5728\u7a0b\u5e8f\u7f16\u8bd1\u4e4b\u524d\uff0c\u9700\u8981\u6d88\u9664\u6b67\u4e49\u3002\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u89e3\u51b3\u8be5\u95ee\u9898\uff1a \u901a\u5e38\uff0c\u6700\u597d\u7684\u65b9\u6cd5\u662f\u7b80\u5355\u5730\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u91cd\u8f7d\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u7684\u5f62\u53c2\u4e0e\u60a8\u8bd5\u56fe\u8c03\u7528\u8be5\u51fd\u6570\u65f6\u4f7f\u7528\u7684\u5f62\u53c2\u7c7b\u578b\u5b8c\u5168\u76f8\u540c\u3002\u7136\u540eC++\u5c31\u80fd\u627e\u5230\u4e0e\u51fd\u6570\u8c03\u7528\u5b8c\u5168\u5339\u914d\u7684\u5bf9\u8c61\u3002 \u6216\u8005\uff0c \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u8f6c\u6362\u4e0d\u660e\u786e\u7684\u53c2\u6570\uff0c\u4ee5\u5339\u914d\u8981\u8c03\u7528\u7684\u51fd\u6570\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u8981\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f print(0) \u5339\u914d print(unsigned int) \uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a: 1 2 int x { 0 }; print ( static_cast < unsigned int > ( x )); // will call print(unsigned int) \u5982\u679c\u53c2\u6570\u662f\u5b57\u9762\u91cf\uff0c\u4f60\u53ef\u4ee5\u4e3a\u5b83\u6dfb\u52a0\u5b57\u9762\u91cf\u540e\u7f00\uff0c\u4f7f\u5176\u80fd\u591f\u88ab\u89e3\u6790\u4e3a\u6b63\u786e\u7684\u7c7b\u578b\uff1a 1 print ( 0u ); // will call print(unsigned int) since 'u' suffix is unsigned int, so this is now an exact match \u5e38\u7528\u7684\u540e\u7f00\u53c2\u89c1\uff1a 4-15-Literals#\u5b57\u9762\u91cf\u540e\u7f00","title":"\u5904\u7406\u4e0d\u660e\u786e\u5339\u914d"},{"location":"8-11-Function-overload-resolution-and-ambiguous-matches/#_5","text":"\u5982\u679c\u6709\u591a\u4e2a\u53c2\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u4f9d\u6b21\u5bf9\u6bcf\u4e2a\u53c2\u6570\u5e94\u7528\u5339\u914d\u89c4\u5219\u3002\u6700\u7ec8\u5339\u914d\u7684\u51fd\u6570\u7684\u8981\u6c42\u662f\uff1a\u6bcf\u4e2a\u53c2\u6570\u90fd\u5339\u914d\u4e14\u81f3\u5c11\u6709\u4e00\u4e2a\u53c2\u6570\u6bd4\u6240\u6709\u5176\u4ed6\u51fd\u6570\u5339\u914d\u5f97\u66f4\u597d\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6240\u9009\u51fd\u6570\u5fc5\u987b\u5bf9\u81f3\u5c11\u4e00\u4e2a\u53c2\u6570\u63d0\u4f9b\u6bd4\u6240\u6709\u5176\u4ed6\u5019\u9009\u51fd\u6570\u66f4\u597d\u7684\u5339\u914d\uff0c\u800c\u5bf9\u6240\u6709\u5176\u4ed6\u53c2\u6570\u4e0d\u5dee\u3002 \u5728\u627e\u5230\u8fd9\u6837\u4e00\u4e2a\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u5b83\u663e\u7136\u662f\u6700\u597d\u7684\u9009\u62e9\u3002\u5982\u679c\u627e\u4e0d\u5230\u8fd9\u6837\u7684\u51fd\u6570\uff0c\u5219\u8be5\u8c03\u7528\u5c06\u88ab\u89c6\u4e3a\u4e8c\u4e49\u6027(\u6216\u4e0d\u5339\u914d)\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include void print ( char c , int x ) { std :: cout << 'a' ; } void print ( char c , double x ) { std :: cout << 'b' ; } void print ( char c , float x ) { std :: cout << 'c' ; } int main () { print ( 'x' , 'a' ); } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u90fd\u80fd\u591f \u5b8c\u5168\u5339\u914d \u3002\u7b2c\u4e00\u4e2a\u51fd\u6570\u901a\u8fc7 \u6570\u503c\u63d0\u5347 \u53ef\u4ee5\u5339\u914d\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u5176\u4ed6\u51e0\u4e2a\u51fd\u6570\u5219\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u624d\u80fd\u5339\u914d\u3002\u56e0\u6b64 print(char, int) \u6bcb\u5eb8\u7f6e\u7591\u662f\u6700\u4f73\u5339\u914d\u51fd\u6570\u3002","title":"\u591a\u53c2\u6570\u91cd\u8f7d\u51fd\u6570\u7684\u5339\u914d"},{"location":"8-12-Default-arguments/","tags":["default"],"text":"Key Takeaway \u9ed8\u8ba4\u53c2\u6570\uff1a void print(int x, int y=10) // 10 is the default argument \u9ed8\u8ba4\u53c2\u6570\u53ea\u80fd\u63d0\u4f9b\u7ed9\u6700\u53f3\u8fb9\u7684\u5f62\u53c2 \u5982\u679c\u51fd\u6570\u6709\u524d\u5411\u58f0\u660e(\u7279\u522b\u662f\u5728\u5934\u6587\u4ef6\u4e2d)\uff0c\u5219\u5c06\u9ed8\u8ba4\u53c2\u6570\u653e\u5728\u90a3\u91cc\u3002\u5426\u5219\uff0c\u5c06\u9ed8\u8ba4\u5b9e\u53c2\u653e\u5165\u51fd\u6570\u5b9a\u4e49\u4e2d\u3002 \u5e26\u6709\u9ed8\u8ba4\u53c2\u6570\u7684\u51fd\u6570\u53ef\u4ee5\u91cd\u8f7d\uff0c\u4f46\u662f\u5bb9\u6613\u9020\u6210 \u4e0d\u660e\u786e\u5339\u914d \u9ed8\u8ba4 \u5b9e\u53c2 \u662f\u4e3a\u51fd\u6570 \u5f62\u53c2 \u63d0\u4f9b\u7684\u9ed8\u8ba4\u503c\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 void print ( int x , int y = 10 ) // 10 is the default argument { std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; } \u5728\u8fdb\u884c\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u8c03\u7528\u65b9\u53ef\u4ee5\u6709\u9009\u62e9\u5730\u4e3a\u4efb\u4f55\u5177\u6709\u9ed8\u8ba4\u5b9e\u53c2\u7684\u51fd\u6570\u5f62\u53c2\u63d0\u4f9b\u5b9e\u53c2\u3002\u5982\u679c\u8c03\u7528\u65b9\u63d0\u4f9b\u4e86\u53c2\u6570\uff0c\u5219\u4f7f\u7528\u51fd\u6570\u8c03\u7528\u4e2d\u53c2\u6570\u7684\u503c\u3002\u5982\u679c\u8c03\u7528\u65b9\u6ca1\u6709\u63d0\u4f9b\u5b9e\u53c2\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u5b9e\u53c2\u7684\u503c\u3002 \u8003\u8651\u4ee5\u4e0b\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include void print ( int x , int y = 4 ) // 4 is the default argument { std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; } int main () { print ( 1 , 2 ); // y will use user-supplied argument 2 print ( 3 ); // y will use default argument 4 } \u8fd9\u4e2a\u7a0b\u5e8f\u4ea7\u751f\u4ee5\u4e0b\u8f93\u51fa\uff1a 1 2 3 4 x: 1 y: 2 x: 3 y: 4 \u5728\u7b2c\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u4e2d\uff0c\u8c03\u7528\u8005\u4e3a\u4e24\u4e2a\u5f62\u53c2\u63d0\u4f9b\u4e86\u663e\u5f0f\u53c2\u6570\uff0c\u56e0\u6b64\u4f7f\u7528\u4e86\u8fd9\u4e9b\u53c2\u6570\u503c\u3002\u5728\u7b2c\u4e8c\u4e2a\u51fd\u6570\u8c03\u7528\u4e2d\uff0c\u8c03\u7528\u8005\u7701\u7565\u4e86\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u56e0\u6b64\u4f7f\u7528\u4e86\u9ed8\u8ba4\u503c 4 \u3002 \u6ce8\u610f\uff0c\u5fc5\u987b\u4f7f\u7528\u7b49\u53f7\u6765\u6307\u5b9a\u9ed8\u8ba4\u53c2\u6570\u3002\u4f7f\u7528\u5706\u62ec\u53f7\u6216\u5927\u62ec\u53f7\u521d\u59cb\u5316\u4e0d\u8d77\u4f5c\u7528: 1 2 3 void foo ( int x = 5 ); // ok void goo ( int x ( 5 )); // compile error void boo ( int x { 5 }); // compile error \u4f55\u65f6\u4f7f\u7528\u9ed8\u8ba4\u5b9e\u53c2 \u5f53\u51fd\u6570\u9700\u8981\u4e00\u4e2a\u5408\u7406\u7684\u9ed8\u8ba4\u503c\u7684\u503c\u65f6\uff0c\u9ed8\u8ba4\u53c2\u6570\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002\u540c\u65f6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u8fd8\u5141\u8bb8\u8c03\u7528\u8005\u4fee\u6539\u8be5\u503c(\u5982\u679c\u4ed6\u4eec\u613f\u610f\u7684\u8bdd)\u3002 \u4f8b\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u7528\u9ed8\u8ba4\u5b9e\u53c2\u7684\u51fd\u6570\u539f\u578b: 1 2 int rollDie ( int sides = 6 ); void openLogFile ( std :: string filename = \"default.log\" ); \u4f5c\u8005\u6ce8 \u56e0\u4e3a\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u662f\u63d0\u4f9b\u4e00\u4e2a\u7279\u5b9a\u7684\u5b9e\u53c2\u503c\u8fd8\u662f\u4f7f\u7528\u9ed8\u8ba4\u503c\uff0c\u6240\u4ee5\u63d0\u4f9b\u4e86\u9ed8\u8ba4\u503c\u7684\u5f62\u53c2\u6709\u65f6\u88ab\u79f0\u4e3a \u53ef\u9009\u5f62\u53c2 \u3002\u4f46\u662f\uff0c\u672f\u8bed \u53ef\u9009\u53c2\u6570(optional parameter) \u4e5f\u7528\u4e8e\u6307\u4ee3\u5176\u4ed6\u51e0\u79cd\u7c7b\u578b\u7684\u53c2\u6570(\u5305\u62ec\u901a\u8fc7 \u4f20\u5730\u5740(pass-by-address) \u65b9\u5f0f\u4f20\u9012\u7684\u53c2\u6570\uff0c\u4ee5\u53ca\u4f7f\u7528 std:: optional \u4f20\u9012\u7684\u53c2\u6570)\uff0c\u6240\u4ee5\u5efa\u8bae\u907f\u514d\u4f7f\u7528\u8fd9\u4e2a\u672f\u8bed\u3002 \u591a\u4e2a\u9ed8\u8ba4\u53c2\u6570 \u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u6709\u591a\u4e2a\u5e26\u9ed8\u8ba4\u5b9e\u53c2\u7684\u5f62\u53c2: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include void print ( int x = 10 , int y = 20 , int z = 30 ) { std :: cout << \"Values: \" << x << \" \" << y << \" \" << z << '\\n' ; } int main () { print ( 1 , 2 , 3 ); // all explicit arguments print ( 1 , 2 ); // rightmost argument defaulted print ( 1 ); // two rightmost arguments defaulted print (); // all arguments defaulted return 0 ; } \u4e0a\u8ff0\u4ee3\u7801\u8f93\u51fa\u7684\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 Values: 1 2 3 Values: 1 2 30 Values: 1 20 30 Values: 10 20 30 C++ \u5e76\u4e0d\u652f\u6301\u5f62\u5982 print(,,3) \u8bed\u6cd5\u7684\u51fd\u6570\u8c03\u7528\u2014\u2014\u5373\u4e3a z \u63d0\u4f9b\u4e00\u4e2a\u503c\uff0c\u540c\u65f6\u4f7f\u7528 x \u548c y \u7684\u9ed8\u8ba4\u503c\u3002\u8fd9\u4f1a\u5e26\u6765\u4e24\u4e2a\u95ee\u9898\uff1a \u9ed8\u8ba4\u53c2\u6570\u53ea\u80fd\u63d0\u4f9b\u7ed9\u6700\u53f3\u8fb9\u7684\u5f62\u53c2\u3002\u4e0b\u9762\u7684\u8bed\u6cd5\u662f\u4e0d\u5141\u8bb8\u7684\uff1a 1 void print ( int x = 10 , int y ); // not allowed \u6cd5\u5219 \u9ed8\u8ba4\u53c2\u6570\u53ea\u80fd\u63d0\u4f9b\u7ed9\u6700\u53f3\u8fb9\u7684\u5f62\u53c2\u3002 \u5982\u679c\u9700\u8981\u4e3a\u51fd\u6570\u8bbe\u7f6e\u591a\u4e2a\u9ed8\u8ba4\u53c2\u6570\uff0c\u5219\u6700\u5de6\u8fb9\u7684\u9ed8\u8ba4\u53c2\u6570\u5e94\u8be5\u662f\u7528\u6237\u6700\u6709\u53ef\u80fd\u8fdb\u884c \u4e3b\u52a8\u8986\u76d6 \u7684\u53c2\u6570\u3002 \u9ed8\u8ba4\u5b9e\u53c2\u4e0d\u80fd\u88ab\u91cd\u590d\u58f0\u660e \u4e00\u65e6\u58f0\u660e\u5b8c\u6210\uff0c\u9ed8\u8ba4\u5b9e\u53c2\u5c31\u4e0d\u80fd(\u5728\u540c\u4e00\u4e2a\u6587\u4ef6\u4e2d)\u88ab\u91cd\u65b0\u58f0\u660e\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u4e8e\u5177\u6709 \u524d\u5411\u58f0\u660e \u548c\u51fd\u6570\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u9ed8\u8ba4\u5b9e\u53c2\u53ef\u4ee5\u5728\u524d\u5411\u58f0\u660e\u6216\u51fd\u6570\u5b9a\u4e49\u4e2d\u58f0\u660e\uff0c\u4f46\u4e0d\u80fd\u540c\u65f6\u5728\u4e24\u8005\u4e2d\u58f0\u660e\u3002 1 2 3 4 5 6 7 8 9 #include void print ( int x , int y = 4 ); // forward declaration void print ( int x , int y = 4 ) // error: redefinition of default argument { std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; } \u6700\u4f73\u5b9e\u8df5 \u662f\u5728\u524d\u5411\u58f0\u660e\u800c\u4e0d\u662f\u5728\u5b9a\u4e49\u4e2d\u58f0\u660e\u9ed8\u8ba4\u53c2\u6570\uff0c\u56e0\u4e3a\u524d\u5411\u58f0\u660e\u66f4\u5bb9\u6613\u88ab\u5176\u4ed6\u6587\u4ef6\u770b\u5230(\u7279\u522b\u662f\u5728\u5934\u6587\u4ef6\u4e2d)\u3002 foo.h 1 2 3 4 #ifndef FOO_H #define FOO_H void print ( int x , int y = 4 ); #endif main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include \"foo.h\" #include void print ( int x , int y ) { std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; } int main () { print ( 5 ); return 0 ; } \u6ce8\u610f\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u80fd\u591f\u4f7f\u7528\u51fd\u6570 print() \u7684\u9ed8\u8ba4\u5b9e\u53c2\uff0c\u56e0\u4e3a main.cpp #include \u4e86 foo.h \uff0c foo.h \u7684\u524d\u5411\u58f0\u660e\u5b9a\u4e49\u4e86\u9ed8\u8ba4\u5b9e\u53c2\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u51fd\u6570\u6709\u524d\u5411\u58f0\u660e(\u7279\u522b\u662f\u5728\u5934\u6587\u4ef6\u4e2d)\uff0c\u5219\u5c06\u9ed8\u8ba4\u53c2\u6570\u653e\u5728\u90a3\u91cc\u3002\u5426\u5219\uff0c\u5c06\u9ed8\u8ba4\u5b9e\u53c2\u653e\u5165\u51fd\u6570\u5b9a\u4e49\u4e2d\u3002 \u9ed8\u8ba4\u53c2\u6570\u548c\u51fd\u6570\u91cd\u8f7d \u5e26\u6709\u9ed8\u8ba4\u53c2\u6570\u7684\u51fd\u6570\u53ef\u4ee5\u91cd\u8f7d\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u662f\u5141\u8bb8\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 void print ( std :: string string ) { } void print ( char ch = ' ' ) { } int main () { print ( \"Hello, world\" ); // resolves to print(std::string) print ( 'a' ); // resolves to print(char) print (); // resolves to print(char) return 0 ; } print() \u51fd\u6570\u7684\u6548\u679c\u548c\u7528\u6237\u663e\u5f0f\u8c03\u7528 print(' ') \u662f\u4e00\u6837\u7684\uff0c\u5b83\u4f1a\u5339\u914d print(char) \u3002 \u518d\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 void print ( int x ); void print ( int x , int y = 10 ); void print ( int x , double y = 20.5 ); \u5e26\u6709\u9ed8\u8ba4\u503c\u7684\u53c2\u6570\u53ef\u4ee5\u7528\u4e8e\u533a\u5206\u51fd\u6570\u91cd\u8f7d(\u610f\u5473\u7740\u4e0a\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u7f16\u8bd1)\u3002\u7136\u800c\uff0c\u8fd9\u6837\u7684\u51fd\u6570\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6f5c\u5728\u7684 \u4e0d\u660e\u786e\u5339\u914d \uff0c\u4f8b\u5982\uff1a 1 2 3 print ( 1 , 2 ); // will resolve to print(int, int) print ( 1 , 2.5 ); // will resolve to print(int, double) print ( 1 ); // ambiguous function call \u5728\u6700\u540e\u4e00\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u5224\u65ad print(1) \u662f\u5e94\u8be5\u89e3\u6790\u4e3a print(int) \u8fd8\u662f\u7b2c\u4e8c\u4e2a\u5f62\u53c2\u6709\u9ed8\u8ba4\u503c\u7684\u4e24\u4e2a\u51fd\u6570\uff08\u4e4b\u4e00\uff09\uff0c\u5bfc\u81f4\u4e00\u4e2a\u4e0d\u660e\u786e\u5339\u914d\u9519\u8bef\u3002 \u5c0f\u7ed3 \u9ed8\u8ba4\u53c2\u6570\u63d0\u4f9b\u4e86\u4e00\u79cd\u6709\u7528\u7684\u673a\u5236\uff0c\u53ef\u4ee5\u4e3a\u7528\u6237\u5e0c\u671b\u6216\u4e0d\u5e0c\u671b\u91cd\u5199\u7684\u53c2\u6570\u6307\u5b9a\u9ed8\u8ba4\u503c\uff0c\u8fd9\u5728 C++\u4e2d\u5f88\u5e38\u89c1\uff0c\u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\u4f60\u4f1a\u770b\u5230\u5f88\u591a\u3002","title":"8.12 - \u9ed8\u8ba4\u53c2\u6570"},{"location":"8-12-Default-arguments/#_1","text":"\u5f53\u51fd\u6570\u9700\u8981\u4e00\u4e2a\u5408\u7406\u7684\u9ed8\u8ba4\u503c\u7684\u503c\u65f6\uff0c\u9ed8\u8ba4\u53c2\u6570\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002\u540c\u65f6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u8fd8\u5141\u8bb8\u8c03\u7528\u8005\u4fee\u6539\u8be5\u503c(\u5982\u679c\u4ed6\u4eec\u613f\u610f\u7684\u8bdd)\u3002 \u4f8b\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u7528\u9ed8\u8ba4\u5b9e\u53c2\u7684\u51fd\u6570\u539f\u578b: 1 2 int rollDie ( int sides = 6 ); void openLogFile ( std :: string filename = \"default.log\" ); \u4f5c\u8005\u6ce8 \u56e0\u4e3a\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u662f\u63d0\u4f9b\u4e00\u4e2a\u7279\u5b9a\u7684\u5b9e\u53c2\u503c\u8fd8\u662f\u4f7f\u7528\u9ed8\u8ba4\u503c\uff0c\u6240\u4ee5\u63d0\u4f9b\u4e86\u9ed8\u8ba4\u503c\u7684\u5f62\u53c2\u6709\u65f6\u88ab\u79f0\u4e3a \u53ef\u9009\u5f62\u53c2 \u3002\u4f46\u662f\uff0c\u672f\u8bed \u53ef\u9009\u53c2\u6570(optional parameter) \u4e5f\u7528\u4e8e\u6307\u4ee3\u5176\u4ed6\u51e0\u79cd\u7c7b\u578b\u7684\u53c2\u6570(\u5305\u62ec\u901a\u8fc7 \u4f20\u5730\u5740(pass-by-address) \u65b9\u5f0f\u4f20\u9012\u7684\u53c2\u6570\uff0c\u4ee5\u53ca\u4f7f\u7528 std:: optional \u4f20\u9012\u7684\u53c2\u6570)\uff0c\u6240\u4ee5\u5efa\u8bae\u907f\u514d\u4f7f\u7528\u8fd9\u4e2a\u672f\u8bed\u3002","title":"\u4f55\u65f6\u4f7f\u7528\u9ed8\u8ba4\u5b9e\u53c2"},{"location":"8-12-Default-arguments/#_2","text":"\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u6709\u591a\u4e2a\u5e26\u9ed8\u8ba4\u5b9e\u53c2\u7684\u5f62\u53c2: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include void print ( int x = 10 , int y = 20 , int z = 30 ) { std :: cout << \"Values: \" << x << \" \" << y << \" \" << z << '\\n' ; } int main () { print ( 1 , 2 , 3 ); // all explicit arguments print ( 1 , 2 ); // rightmost argument defaulted print ( 1 ); // two rightmost arguments defaulted print (); // all arguments defaulted return 0 ; } \u4e0a\u8ff0\u4ee3\u7801\u8f93\u51fa\u7684\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 Values: 1 2 3 Values: 1 2 30 Values: 1 20 30 Values: 10 20 30 C++ \u5e76\u4e0d\u652f\u6301\u5f62\u5982 print(,,3) \u8bed\u6cd5\u7684\u51fd\u6570\u8c03\u7528\u2014\u2014\u5373\u4e3a z \u63d0\u4f9b\u4e00\u4e2a\u503c\uff0c\u540c\u65f6\u4f7f\u7528 x \u548c y \u7684\u9ed8\u8ba4\u503c\u3002\u8fd9\u4f1a\u5e26\u6765\u4e24\u4e2a\u95ee\u9898\uff1a \u9ed8\u8ba4\u53c2\u6570\u53ea\u80fd\u63d0\u4f9b\u7ed9\u6700\u53f3\u8fb9\u7684\u5f62\u53c2\u3002\u4e0b\u9762\u7684\u8bed\u6cd5\u662f\u4e0d\u5141\u8bb8\u7684\uff1a 1 void print ( int x = 10 , int y ); // not allowed \u6cd5\u5219 \u9ed8\u8ba4\u53c2\u6570\u53ea\u80fd\u63d0\u4f9b\u7ed9\u6700\u53f3\u8fb9\u7684\u5f62\u53c2\u3002 \u5982\u679c\u9700\u8981\u4e3a\u51fd\u6570\u8bbe\u7f6e\u591a\u4e2a\u9ed8\u8ba4\u53c2\u6570\uff0c\u5219\u6700\u5de6\u8fb9\u7684\u9ed8\u8ba4\u53c2\u6570\u5e94\u8be5\u662f\u7528\u6237\u6700\u6709\u53ef\u80fd\u8fdb\u884c \u4e3b\u52a8\u8986\u76d6 \u7684\u53c2\u6570\u3002","title":"\u591a\u4e2a\u9ed8\u8ba4\u53c2\u6570"},{"location":"8-12-Default-arguments/#_3","text":"\u4e00\u65e6\u58f0\u660e\u5b8c\u6210\uff0c\u9ed8\u8ba4\u5b9e\u53c2\u5c31\u4e0d\u80fd(\u5728\u540c\u4e00\u4e2a\u6587\u4ef6\u4e2d)\u88ab\u91cd\u65b0\u58f0\u660e\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u4e8e\u5177\u6709 \u524d\u5411\u58f0\u660e \u548c\u51fd\u6570\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u9ed8\u8ba4\u5b9e\u53c2\u53ef\u4ee5\u5728\u524d\u5411\u58f0\u660e\u6216\u51fd\u6570\u5b9a\u4e49\u4e2d\u58f0\u660e\uff0c\u4f46\u4e0d\u80fd\u540c\u65f6\u5728\u4e24\u8005\u4e2d\u58f0\u660e\u3002 1 2 3 4 5 6 7 8 9 #include void print ( int x , int y = 4 ); // forward declaration void print ( int x , int y = 4 ) // error: redefinition of default argument { std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; } \u6700\u4f73\u5b9e\u8df5 \u662f\u5728\u524d\u5411\u58f0\u660e\u800c\u4e0d\u662f\u5728\u5b9a\u4e49\u4e2d\u58f0\u660e\u9ed8\u8ba4\u53c2\u6570\uff0c\u56e0\u4e3a\u524d\u5411\u58f0\u660e\u66f4\u5bb9\u6613\u88ab\u5176\u4ed6\u6587\u4ef6\u770b\u5230(\u7279\u522b\u662f\u5728\u5934\u6587\u4ef6\u4e2d)\u3002 foo.h 1 2 3 4 #ifndef FOO_H #define FOO_H void print ( int x , int y = 4 ); #endif main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include \"foo.h\" #include void print ( int x , int y ) { std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; } int main () { print ( 5 ); return 0 ; } \u6ce8\u610f\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u80fd\u591f\u4f7f\u7528\u51fd\u6570 print() \u7684\u9ed8\u8ba4\u5b9e\u53c2\uff0c\u56e0\u4e3a main.cpp #include \u4e86 foo.h \uff0c foo.h \u7684\u524d\u5411\u58f0\u660e\u5b9a\u4e49\u4e86\u9ed8\u8ba4\u5b9e\u53c2\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u51fd\u6570\u6709\u524d\u5411\u58f0\u660e(\u7279\u522b\u662f\u5728\u5934\u6587\u4ef6\u4e2d)\uff0c\u5219\u5c06\u9ed8\u8ba4\u53c2\u6570\u653e\u5728\u90a3\u91cc\u3002\u5426\u5219\uff0c\u5c06\u9ed8\u8ba4\u5b9e\u53c2\u653e\u5165\u51fd\u6570\u5b9a\u4e49\u4e2d\u3002","title":"\u9ed8\u8ba4\u5b9e\u53c2\u4e0d\u80fd\u88ab\u91cd\u590d\u58f0\u660e"},{"location":"8-12-Default-arguments/#_4","text":"\u5e26\u6709\u9ed8\u8ba4\u53c2\u6570\u7684\u51fd\u6570\u53ef\u4ee5\u91cd\u8f7d\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u662f\u5141\u8bb8\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 void print ( std :: string string ) { } void print ( char ch = ' ' ) { } int main () { print ( \"Hello, world\" ); // resolves to print(std::string) print ( 'a' ); // resolves to print(char) print (); // resolves to print(char) return 0 ; } print() \u51fd\u6570\u7684\u6548\u679c\u548c\u7528\u6237\u663e\u5f0f\u8c03\u7528 print(' ') \u662f\u4e00\u6837\u7684\uff0c\u5b83\u4f1a\u5339\u914d print(char) \u3002 \u518d\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 void print ( int x ); void print ( int x , int y = 10 ); void print ( int x , double y = 20.5 ); \u5e26\u6709\u9ed8\u8ba4\u503c\u7684\u53c2\u6570\u53ef\u4ee5\u7528\u4e8e\u533a\u5206\u51fd\u6570\u91cd\u8f7d(\u610f\u5473\u7740\u4e0a\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u7f16\u8bd1)\u3002\u7136\u800c\uff0c\u8fd9\u6837\u7684\u51fd\u6570\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6f5c\u5728\u7684 \u4e0d\u660e\u786e\u5339\u914d \uff0c\u4f8b\u5982\uff1a 1 2 3 print ( 1 , 2 ); // will resolve to print(int, int) print ( 1 , 2.5 ); // will resolve to print(int, double) print ( 1 ); // ambiguous function call \u5728\u6700\u540e\u4e00\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u5224\u65ad print(1) \u662f\u5e94\u8be5\u89e3\u6790\u4e3a print(int) \u8fd8\u662f\u7b2c\u4e8c\u4e2a\u5f62\u53c2\u6709\u9ed8\u8ba4\u503c\u7684\u4e24\u4e2a\u51fd\u6570\uff08\u4e4b\u4e00\uff09\uff0c\u5bfc\u81f4\u4e00\u4e2a\u4e0d\u660e\u786e\u5339\u914d\u9519\u8bef\u3002","title":"\u9ed8\u8ba4\u53c2\u6570\u548c\u51fd\u6570\u91cd\u8f7d"},{"location":"8-12-Default-arguments/#_5","text":"\u9ed8\u8ba4\u53c2\u6570\u63d0\u4f9b\u4e86\u4e00\u79cd\u6709\u7528\u7684\u673a\u5236\uff0c\u53ef\u4ee5\u4e3a\u7528\u6237\u5e0c\u671b\u6216\u4e0d\u5e0c\u671b\u91cd\u5199\u7684\u53c2\u6570\u6307\u5b9a\u9ed8\u8ba4\u503c\uff0c\u8fd9\u5728 C++\u4e2d\u5f88\u5e38\u89c1\uff0c\u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\u4f60\u4f1a\u770b\u5230\u5f88\u591a\u3002","title":"\u5c0f\u7ed3"},{"location":"8-13-Function-templates/","tags":["Function templates"],"text":"Key Takeaway \u5047\u8bbe\u6211\u4eec\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u6c42\u4e24\u4e2a\u6570\u4e2d\u54ea\u4e2a\u503c\u8f83\u5927\u7684\u51fd\u6570\uff0c\u90a3\u4e48\u6b64\u65f6\u53ef\u80fd\u4f1a\u8fd9\u6837\u505a\uff1a 1 2 3 4 int max ( int x , int y ) { return ( x > y ) ? x : y ; } \u867d\u7136\u8c03\u7528\u8005\u53ef\u4ee5\u5411\u51fd\u6570\u4f20\u9012\u4e0d\u540c\u7684\u503c\uff0c\u4f46\u5f62\u53c2\u7684\u7c7b\u578b\u662f\u56fa\u5b9a\u7684\uff0c\u56e0\u6b64\u8c03\u7528\u8005\u53ea\u80fd\u4f20\u9012 int \u503c\u3002\u8fd9\u610f\u5473\u7740\u8fd9\u4e2a\u51fd\u6570\u5b9e\u9645\u4e0a\u53ea\u9002\u7528\u4e8e\u6574\u6570(\u4ee5\u53ca\u53ef\u4ee5\u63d0\u5347\u4e3a int \u7684\u7c7b\u578b)\u3002 \u90a3\u4e48\uff0c\u5f53\u4f60\u60f3\u627e\u5230\u4e24\u4e2a double \u503c\u7684\u6700\u5927\u503c\u65f6\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f\u56e0\u4e3aC++\u8981\u6c42\u6211\u4eec\u6307\u5b9a\u6240\u6709\u51fd\u6570\u5f62\u53c2\u7684\u7c7b\u578b\uff0c\u89e3\u51b3\u65b9\u6848\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u91cd\u8f7d\u7248\u672c\u7684 max() \uff0c\u5f62\u53c2\u7c7b\u578b\u4e3a double \uff1a 1 2 3 4 double max ( double x , double y ) { return ( x > y ) ? x : y ; } \u6ce8\u610f\uff0c\u5b9e\u73b0 max() \u7684 double \u7248\u672c\u7684\u4ee3\u7801\u4e0e max() \u7684 int \u7248\u672c\u7684\u4ee3\u7801\u5b8c\u5168\u76f8\u540c\uff01\u4e8b\u5b9e\u4e0a\uff0c\u8fd9\u4e2a\u5b9e\u73b0\u9002\u7528\u4e8e\u8bb8\u591a\u4e0d\u540c\u7684\u7c7b\u578b\uff1a\u5305\u62ec int \u3001 double \u3001 long \u3001 long double \u751a\u81f3\u662f\u4f60\u81ea\u5df1\u521b\u5efa\u7684\u65b0\u7c7b\u578b(\u6211\u4eec\u5c06\u5728\u672a\u6765\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u5982\u4f55\u505a)\u3002 \u5fc5\u987b\u4e3a\u6bcf\u4e00\u7ec4\u53c2\u6570\u7c7b\u578b\u4f7f\u7528\u76f8\u540c\u7684\u5b9e\u73b0\u6765\u521b\u5efa\u91cd\u8f7d\u51fd\u6570\uff0c\u8fd9\u662f\u4e00\u4e2a\u4ee4\u4eba\u5934\u75db\u7684\u95ee\u9898\uff0c\u4e5f\u662f\u9519\u8bef\u7684\u6765\u6e90\uff0c\u5e76\u4e14\u660e\u663e\u8fdd\u53cd\u4e86DRY(\u4e0d\u8981\u91cd\u590d\u81ea\u5df1)\u539f\u5219\u3002\u8fd9\u91cc\u8fd8\u6709\u4e00\u4e2a\u4e0d\u592a\u660e\u663e\u7684\u6311\u6218\uff1a\u5e0c\u671b\u4f7f\u7528 max() \u51fd\u6570\u7684\u7a0b\u5e8f\u5458\u53ef\u80fd\u5e0c\u671b\u7528 max() \u7684\u4f5c\u8005\u6ca1\u6709\u9884\u6599\u5230\u7684\u5f62\u53c2\u7c7b\u578b\u8c03\u7528\u5b83(\u56e0\u6b64\u6ca1\u6709\u4e3a\u6b64\u7f16\u5199\u91cd\u8f7d\u51fd\u6570)\u3002 \u6211\u4eec\u9700\u8981\u7684\u662f\u7f16\u5199\u5355\u4e00\u7248\u672c\u7684 max() \uff0c\u5b83\u53ef\u4ee5\u5904\u7406\u4efb\u4f55\u7c7b\u578b\u7684\u53c2\u6570(\u751a\u81f3\u662f\u5728\u7f16\u5199 max() \u4ee3\u7801\u65f6\u53ef\u80fd\u6ca1\u6709\u9884\u6599\u5230\u7684\u7c7b\u578b)\u3002\u6b63\u5e38\u7684\u529f\u80fd\u6839\u672c\u65e0\u6cd5\u80dc\u4efb\u8fd9\u91cc\u7684\u4efb\u52a1\u3002\u5e78\u8fd0\u7684\u662f\uff0cC++\u652f\u6301\u53e6\u4e00\u4e2a\u4e13\u95e8\u4e3a\u89e3\u51b3\u8fd9\u7c7b\u95ee\u9898\u800c\u8bbe\u8ba1\u7684\u7279\u6027\u3002 \u6b22\u8fce\u6765\u5230C++\u6a21\u677f\u7684\u4e16\u754c\u3002 C++\u6a21\u677f\u7b80\u4ecb \u5728C++\u4e2d\uff0c\u6a21\u677f\u7cfb\u7edf\u88ab\u8bbe\u8ba1\u7528\u6765\u7b80\u5316\u521b\u5efa\u80fd\u591f\u5904\u7406\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u51fd\u6570(\u6216\u7c7b)\u7684\u8fc7\u7a0b\u3002 \u6211\u4eec\u4e0d\u518d\u662f\u624b\u52a8\u521b\u5efa\u4e00\u5806\u51e0\u4e4e\u76f8\u540c\u7684\u51fd\u6570\u6216\u7c7b(\u6bcf\u4e00\u7ec4\u4e0d\u540c\u7c7b\u578b\u5bf9\u5e94\u4e00\u4e2a)\uff0c\u800c\u662f\u521b\u5efa\u4e00\u4e2a\u5355\u4e00\u7684\u201c\u6a21\u677f\u201d\u3002\u5c31\u50cf\u666e\u901a\u7684\u5b9a\u4e49\u4e00\u6837\uff0c\u6a21\u677f\u63cf\u8ff0\u51fd\u6570\u6216\u7c7b\u7684\u6837\u5b50\u3002\u4e0e\u5e38\u89c4\u5b9a\u4e49(\u5fc5\u987b\u6307\u5b9a\u6240\u6709\u7c7b\u578b)\u4e0d\u540c\uff0c\u5728\u6a21\u677f\u4e2d\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u6216\u591a\u4e2a\u5360\u4f4d\u7b26\u7c7b\u578b\u3002\u5360\u4f4d\u7b26\u7c7b\u578b\u8868\u793a\u5728\u7f16\u5199\u6a21\u677f\u65f6\u4e0d\u77e5\u9053\u4f46\u5c06\u5728\u540e\u9762\u63d0\u4f9b\u7684\u7c7b\u578b\u3002 \u4e00\u65e6\u5b9a\u4e49\u4e86\u6a21\u677f\uff0c\u7f16\u8bd1\u5668\u5c31\u53ef\u4ee5\u6839\u636e\u9700\u8981\uff0c\u4f7f\u7528\u6a21\u677f\u751f\u6210\u4efb\u610f\u591a\u7684\u91cd\u8f7d\u51fd\u6570(\u6216\u7c7b)\uff0c\u6bcf\u4e2a\u51fd\u6570\u4f7f\u7528\u4e0d\u540c\u7684\u5b9e\u9645\u7c7b\u578b\uff01 \u6700\u7ec8\u7684\u7ed3\u679c\u662f\u4e00\u6837\u7684\u2014\u2014\u6211\u4eec\u5f97\u5230\u4e00\u5806\u51e0\u4e4e\u76f8\u540c\u7684\u51fd\u6570\u6216\u7c7b(\u6bcf\u4e00\u7ec4\u4e0d\u540c\u7684\u7c7b\u578b\u5bf9\u5e94\u4e00\u4e2a)\u3002\u4f46\u662f\u6211\u4eec\u53ea\u9700\u8981\u521b\u5efa\u548c\u7ef4\u62a4\u4e00\u4e2a\u6a21\u677f\uff0c\u7f16\u8bd1\u5668\u4f1a\u5b8c\u6210\u6240\u6709\u8270\u96be\u7684\u5de5\u4f5c\u3002 \u5173\u952e\u4fe1\u606f \u7f16\u8bd1\u5668\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u6a21\u677f\u6765\u751f\u6210\u4e00\u7cfb\u5217\u76f8\u5173\u7684\u51fd\u6570\u6216\u7c7b\uff0c\u6bcf\u4e2a\u51fd\u6570\u6216\u7c7b\u4f7f\u7528\u4e00\u7ec4\u4e0d\u540c\u7684\u7c7b\u578b\u3002 \u9898\u5916\u8bdd Because the concept behind templates can be hard to describe in words, let\u2019s try an analogy. If you were to look up the word \u201ctemplate\u201d in the dictionary, you\u2019d find a definition that was similar to the following: \u201ca template is a model that serves as a pattern for creating similar objects\u201d. One type of template that is very easy to understand is that of a stencil. A stencil is a thin piece of material (such as a piece of cardboard or plastic) with a shape cut out of it (e.g. a happy face). By placing the stencil on top of another object, then spraying paint through the hole, you can very quickly replicate the cut-out shape. The stencil itself only needs to be created once, and then it can be reused as many times as desired, to create the cut out shape in as many different colors as you like. Even better, the color of a shape made with the stencil doesn\u2019t have to be determined until the stencil is actually used. A template is essentially a stencil for creating functions or classes. We create the template (our stencil) once, and then we can use it as many times as needed, to stencil out a function or class for a specific set of actual types. Those actual types don\u2019t need to be determined until the template is actually used. \u56e0\u4e3a\u53ea\u6709\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u6a21\u677f\u65f6(\u800c\u4e0d\u662f\u5728\u7f16\u5199\u6a21\u677f\u65f6)\u624d\u80fd\u786e\u5b9a\u5b9e\u9645\u7c7b\u578b\uff0c\u56e0\u6b64\u6a21\u677f\u7684\u4f5c\u8005\u4e0d\u5fc5\u5c1d\u8bd5\u9884\u6d4b\u53ef\u80fd\u4f7f\u7528\u7684\u6240\u6709\u5b9e\u9645\u7c7b\u578b\u3002\u8fd9\u610f\u5473\u7740\u6a21\u677f\u4ee3\u7801\u53ef\u4ee5\u4e0e\u7f16\u5199\u6a21\u677f\u65f6\u751a\u81f3\u4e0d\u5b58\u5728\u7684\u7c7b\u578b\u4e00\u8d77\u4f7f\u7528\uff01\u7a0d\u540e\uff0c\u5f53\u5f00\u59cb\u63a2\u7d22C++\u6807\u51c6\u5e93\u65f6\uff0c\u6211\u4eec\u5c06\u770b\u5230\u8fd9\u662f\u5982\u4f55\u6d3e\u4e0a\u7528\u573a\u7684\uff0cC++\u6807\u51c6\u5e93\u5145\u6ee1\u4e86\u6a21\u677f\u4ee3\u7801! \u5173\u952e\u4fe1\u606f \u6a21\u677f\u53ef\u4ee5\u4f7f\u7528\u5728\u7f16\u5199\u6a21\u677f\u65f6\u751a\u81f3\u4e0d\u5b58\u5728\u7684\u7c7b\u578b\u3002\u8fd9\u6709\u52a9\u4e8e\u4f7f\u6a21\u677f\u4ee3\u7801\u65e2\u7075\u6d3b\u53c8\u7ecf\u5f97\u8d77\u8003\u9a8c! \u5728\u672c\u8bfe\u7684\u5176\u4f59\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u548c\u63a2\u7d22\u5982\u4f55\u521b\u5efa \u51fd\u6570\u6a21\u677f \uff0c\u5e76\u8be6\u7ec6\u63cf\u8ff0\u5b83\u4eec\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u6211\u4eec\u5c06\u5728\u4ecb\u7ecd\u4e86\u4ec0\u4e48\u662f\u7c7b\u4e4b\u540e\u518d\u8ba8\u8bba \u7c7b\u6a21\u677f \u3002 \u51fd\u6570\u6a21\u677f \u51fd\u6570\u6a21\u677f \u662f\u4e00\u79cd\u7c7b\u4f3c\u51fd\u6570\u7684\u5b9a\u4e49\uff0c\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u6216\u591a\u4e2a\u91cd\u8f7d\u51fd\u6570\uff0c\u6bcf\u4e2a\u91cd\u8f7d\u51fd\u6570\u90fd\u6709\u4e00\u7ec4\u4e0d\u540c\u7684\u5b9e\u9645\u7c7b\u578b\u3002\u8fd9\u5c06\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u53ef\u4ee5\u5904\u7406\u8bb8\u591a\u4e0d\u540c\u7c7b\u578b\u7684\u51fd\u6570\u3002 \u5728\u521b\u5efa\u51fd\u6570\u6a21\u677f\u65f6\uff0c\u5bf9\u4e8e\u6211\u4eec\u5e0c\u671b\u7a0d\u540e\u6307\u5b9a\u7684\u4efb\u4f55\u5f62\u53c2\u7c7b\u578b\u3001\u8fd4\u56de\u7c7b\u578b\u6216\u51fd\u6570\u4f53\u4e2d\u4f7f\u7528\u7684\u7c7b\u578b\uff0c\u6211\u4eec\u90fd\u4f7f\u7528\u5360\u4f4d\u7b26\u7c7b\u578b(\u4e5f\u79f0\u4e3a\u6a21\u677f\u7c7b\u578b)\u3002 \u51fd\u6570\u6a21\u677f\u6700\u597d\u901a\u8fc7\u793a\u4f8b\u6765\u6559\u6388\uff0c\u6240\u4ee5\u8ba9\u6211\u4eec\u5c06\u4e0a\u9762\u793a\u4f8b\u4e2d\u7684 max(int, int) \u51fd\u6570\u8f6c\u6362\u4e3a\u51fd\u6570\u6a21\u677f\u3002\u5b83\u51fa\u5947\u5730\u7b80\u5355\uff0c\u6211\u4eec\u5c06\u89e3\u91ca\u8fd9\u4e00\u8fc7\u7a0b\u4e2d\u53d1\u751f\u4e86\u4ec0\u4e48\u3002 \u521b\u5efa\u6a21\u677f\u5316\u7684 max \u51fd\u6570 \u4e0b\u9762\u4ee3\u7801\u662f int \u7248\u672c\u7684 max() \uff1a 1 2 3 4 int max ( int x , int y ) { return ( x > y ) ? x : y ; } \u6ce8\u610f\uff0c\u6211\u4eec\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\u4f7f\u7528\u4e86\u4e09\u6b21 int \u7c7b\u578b\uff1a\u4e00\u6b21\u7528\u4e8e\u5f62\u53c2 x \uff0c\u4e00\u6b21\u7528\u4e8e\u5f62\u53c2 y \uff0c\u8fd8\u6709\u4e00\u6b21\u7528\u4e8e\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u3002 \u8981\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6a21\u677f\uff0c\u6211\u4eec\u8981\u505a\u4e24\u4ef6\u4e8b\u3002\u9996\u5148\uff0c\u6211\u4eec\u5c06\u7528\u6a21\u677f\u7c7b\u578b\u66ff\u6362\u7279\u5b9a\u7c7b\u578b\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u53ea\u6709\u4e00\u4e2a\u9700\u8981\u66ff\u6362\u7684\u7c7b\u578b( int )\uff0c\u6240\u4ee5\u53ea\u9700\u8981\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\u3002\u901a\u5e38\u7ea6\u5b9a\u4f7f\u7528\u5355\u4e2a\u5927\u5199\u5b57\u6bcd\uff08\u4ece\u5927\u5199\u5b57\u6bcd T \u5f00\u59cb\uff09\u8868\u793a\u6a21\u677f\u7c7b\u578b\u3002 \u4e0b\u9762\u662f\u6211\u4eec\u4f7f\u7528\u5355\u4e00\u6a21\u677f\u7c7b\u578b\u7684\u65b0\u51fd\u6570\uff1a 1 2 3 4 T max ( T x , T y ) // won't compile because we haven't defined T { return ( x > y ) ? x : y ; } \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u5355\u4e2a\u5927\u5199\u5b57\u6bcd(\u4eceT\u5f00\u59cb)\u547d\u540d\u6a21\u677f\u7c7b\u578b(\u4f8b\u5982 T, U, V \u7b49\u2026) \u8fd9\u662f\u4e00\u4e2a\u597d\u7684\u5f00\u59cb\u2014\u2014\u7136\u800c\uff0c\u5b83\u4e0d\u4f1a\u7f16\u8bd1\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053 T \u662f\u4ec0\u4e48\uff01\u8fd9\u4ecd\u7136\u662f\u4e00\u4e2a\u666e\u901a\u7684\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u51fd\u6570\u6a21\u677f\u3002 \u5176\u6b21\uff0c\u6211\u4eec\u4f1a\u544a\u8bc9\u7f16\u8bd1\u5668\u8fd9\u662f\u4e00\u4e2a\u51fd\u6570\u6a21\u677f\uff0c\u800c T \u662f\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\u3002\u8fd9\u662f\u901a\u8fc7\u6240\u8c13\u7684\u6a21\u677f\u53c2\u6570\u58f0\u660e\u6765\u5b8c\u6210\u7684\uff1a 1 2 3 4 5 template < typename T > // this is the template parameter declaration T max ( T x , T y ) // this is the function template definition for max { return ( x > y ) ? x : y ; } \u4ed4\u7ec6\u89c2\u5bdf\u6a21\u677f\u53c2\u6570\u58f0\u660e\u3002\u9996\u5148\u4f7f\u7528\u5173\u952e\u5b57 template \u544a\u8bc9\u7f16\u8bd1\u5668\u521b\u5efa\u4e00\u4e2a\u6a21\u677f\u3002\u63a5\u4e0b\u6765\uff0c\u5728\u5c16\u62ec\u53f7( <> )\u4e2d\u6307\u5b9a\u6a21\u677f\u5c06\u4f7f\u7528\u7684 \u6240\u6709\u6a21\u677f\u7c7b\u578b \u3002\u5bf9\u4e8e\u6bcf\u4e2a\u6a21\u677f\u7c7b\u578b\uff0c\u6211\u4eec\u4f7f\u7528\u5173\u952e\u5b57 typename \u6216 class \uff0c\u540e\u9762\u8ddf\u7740\u6a21\u677f\u7c7b\u578b\u7684\u540d\u79f0(\u4f8b\u5982 \u201cT\u201d)\u3002 \u6bcf\u4e2a\u6a21\u677f\u51fd\u6570(\u6216\u6a21\u677f\u7c7b)\u90fd\u9700\u8981\u58f0\u660e\u81ea\u5df1\u7684\u6a21\u677f\u5f62\u53c2\u3002 \u9898\u5916\u8bdd \u5728\u8fd9\u4e2a\u4e0a\u4e0b\u6587\u4e2d\uff0c typename \u548c class \u5173\u952e\u5b57\u4e4b\u95f4\u6ca1\u6709\u533a\u522b\u3002\u4f60\u4f1a\u7ecf\u5e38\u770b\u5230\u4eba\u4eec\u4f7f\u7528 class \u5173\u952e\u5b57\uff0c\u56e0\u4e3a\u5b83\u66f4\u65e9\u5730\u88ab\u5f15\u5165\u5230\u8bed\u8a00\u4e2d\u3002\u7136\u800c\uff0c\u6211\u4eec\u66f4\u559c\u6b22\u65b0\u5f0f\u7684 typename \u5173\u952e\u5b57\uff0c\u56e0\u4e3a\u5b83\u66f4\u6e05\u695a\u5730\u8868\u660e\u6a21\u677f\u7c7b\u578b\u53ef\u4ee5\u88ab\u4efb\u4f55\u7c7b\u578b(\u4f8b\u5982\u57fa\u672c\u7c7b\u578b)\u66ff\u6362\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u7c7b\u7c7b\u578b\u3002 \u56e0\u4e3a\u8fd9\u4e2a\u51fd\u6570\u6a21\u677f\u53ea\u9700\u8981\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\uff0c\u79f0\u4e3a T \uff0c\u6211\u4eec\u79f0\u5176\u4e3a max \u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728\u8bfe\u7a0b 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f \u4e2d\u8ba8\u8bba\u5982\u4f55\u521b\u5efa\u5177\u6709\u591a\u79cd\u6a21\u677f\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f\u3002 \u641e\u5b9a\uff01\u7b80\u5355\u5230\u4ee4\u4eba\u96be\u4ee5\u7f6e\u4fe1\u3002 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u5982\u4f55\u4f7f\u7528 max \u51fd\u6570\u6a21\u677f\u751f\u6210\u5e26\u6709\u4e0d\u540c\u7c7b\u578b\u53c2\u6570\u7684 max() \u51fd\u6570\u3002","title":"8.13 - \u51fd\u6570\u6a21\u677f"},{"location":"8-13-Function-templates/#c","text":"\u5728C++\u4e2d\uff0c\u6a21\u677f\u7cfb\u7edf\u88ab\u8bbe\u8ba1\u7528\u6765\u7b80\u5316\u521b\u5efa\u80fd\u591f\u5904\u7406\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u51fd\u6570(\u6216\u7c7b)\u7684\u8fc7\u7a0b\u3002 \u6211\u4eec\u4e0d\u518d\u662f\u624b\u52a8\u521b\u5efa\u4e00\u5806\u51e0\u4e4e\u76f8\u540c\u7684\u51fd\u6570\u6216\u7c7b(\u6bcf\u4e00\u7ec4\u4e0d\u540c\u7c7b\u578b\u5bf9\u5e94\u4e00\u4e2a)\uff0c\u800c\u662f\u521b\u5efa\u4e00\u4e2a\u5355\u4e00\u7684\u201c\u6a21\u677f\u201d\u3002\u5c31\u50cf\u666e\u901a\u7684\u5b9a\u4e49\u4e00\u6837\uff0c\u6a21\u677f\u63cf\u8ff0\u51fd\u6570\u6216\u7c7b\u7684\u6837\u5b50\u3002\u4e0e\u5e38\u89c4\u5b9a\u4e49(\u5fc5\u987b\u6307\u5b9a\u6240\u6709\u7c7b\u578b)\u4e0d\u540c\uff0c\u5728\u6a21\u677f\u4e2d\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u6216\u591a\u4e2a\u5360\u4f4d\u7b26\u7c7b\u578b\u3002\u5360\u4f4d\u7b26\u7c7b\u578b\u8868\u793a\u5728\u7f16\u5199\u6a21\u677f\u65f6\u4e0d\u77e5\u9053\u4f46\u5c06\u5728\u540e\u9762\u63d0\u4f9b\u7684\u7c7b\u578b\u3002 \u4e00\u65e6\u5b9a\u4e49\u4e86\u6a21\u677f\uff0c\u7f16\u8bd1\u5668\u5c31\u53ef\u4ee5\u6839\u636e\u9700\u8981\uff0c\u4f7f\u7528\u6a21\u677f\u751f\u6210\u4efb\u610f\u591a\u7684\u91cd\u8f7d\u51fd\u6570(\u6216\u7c7b)\uff0c\u6bcf\u4e2a\u51fd\u6570\u4f7f\u7528\u4e0d\u540c\u7684\u5b9e\u9645\u7c7b\u578b\uff01 \u6700\u7ec8\u7684\u7ed3\u679c\u662f\u4e00\u6837\u7684\u2014\u2014\u6211\u4eec\u5f97\u5230\u4e00\u5806\u51e0\u4e4e\u76f8\u540c\u7684\u51fd\u6570\u6216\u7c7b(\u6bcf\u4e00\u7ec4\u4e0d\u540c\u7684\u7c7b\u578b\u5bf9\u5e94\u4e00\u4e2a)\u3002\u4f46\u662f\u6211\u4eec\u53ea\u9700\u8981\u521b\u5efa\u548c\u7ef4\u62a4\u4e00\u4e2a\u6a21\u677f\uff0c\u7f16\u8bd1\u5668\u4f1a\u5b8c\u6210\u6240\u6709\u8270\u96be\u7684\u5de5\u4f5c\u3002 \u5173\u952e\u4fe1\u606f \u7f16\u8bd1\u5668\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u6a21\u677f\u6765\u751f\u6210\u4e00\u7cfb\u5217\u76f8\u5173\u7684\u51fd\u6570\u6216\u7c7b\uff0c\u6bcf\u4e2a\u51fd\u6570\u6216\u7c7b\u4f7f\u7528\u4e00\u7ec4\u4e0d\u540c\u7684\u7c7b\u578b\u3002 \u9898\u5916\u8bdd Because the concept behind templates can be hard to describe in words, let\u2019s try an analogy. If you were to look up the word \u201ctemplate\u201d in the dictionary, you\u2019d find a definition that was similar to the following: \u201ca template is a model that serves as a pattern for creating similar objects\u201d. One type of template that is very easy to understand is that of a stencil. A stencil is a thin piece of material (such as a piece of cardboard or plastic) with a shape cut out of it (e.g. a happy face). By placing the stencil on top of another object, then spraying paint through the hole, you can very quickly replicate the cut-out shape. The stencil itself only needs to be created once, and then it can be reused as many times as desired, to create the cut out shape in as many different colors as you like. Even better, the color of a shape made with the stencil doesn\u2019t have to be determined until the stencil is actually used. A template is essentially a stencil for creating functions or classes. We create the template (our stencil) once, and then we can use it as many times as needed, to stencil out a function or class for a specific set of actual types. Those actual types don\u2019t need to be determined until the template is actually used. \u56e0\u4e3a\u53ea\u6709\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u6a21\u677f\u65f6(\u800c\u4e0d\u662f\u5728\u7f16\u5199\u6a21\u677f\u65f6)\u624d\u80fd\u786e\u5b9a\u5b9e\u9645\u7c7b\u578b\uff0c\u56e0\u6b64\u6a21\u677f\u7684\u4f5c\u8005\u4e0d\u5fc5\u5c1d\u8bd5\u9884\u6d4b\u53ef\u80fd\u4f7f\u7528\u7684\u6240\u6709\u5b9e\u9645\u7c7b\u578b\u3002\u8fd9\u610f\u5473\u7740\u6a21\u677f\u4ee3\u7801\u53ef\u4ee5\u4e0e\u7f16\u5199\u6a21\u677f\u65f6\u751a\u81f3\u4e0d\u5b58\u5728\u7684\u7c7b\u578b\u4e00\u8d77\u4f7f\u7528\uff01\u7a0d\u540e\uff0c\u5f53\u5f00\u59cb\u63a2\u7d22C++\u6807\u51c6\u5e93\u65f6\uff0c\u6211\u4eec\u5c06\u770b\u5230\u8fd9\u662f\u5982\u4f55\u6d3e\u4e0a\u7528\u573a\u7684\uff0cC++\u6807\u51c6\u5e93\u5145\u6ee1\u4e86\u6a21\u677f\u4ee3\u7801! \u5173\u952e\u4fe1\u606f \u6a21\u677f\u53ef\u4ee5\u4f7f\u7528\u5728\u7f16\u5199\u6a21\u677f\u65f6\u751a\u81f3\u4e0d\u5b58\u5728\u7684\u7c7b\u578b\u3002\u8fd9\u6709\u52a9\u4e8e\u4f7f\u6a21\u677f\u4ee3\u7801\u65e2\u7075\u6d3b\u53c8\u7ecf\u5f97\u8d77\u8003\u9a8c! \u5728\u672c\u8bfe\u7684\u5176\u4f59\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u548c\u63a2\u7d22\u5982\u4f55\u521b\u5efa \u51fd\u6570\u6a21\u677f \uff0c\u5e76\u8be6\u7ec6\u63cf\u8ff0\u5b83\u4eec\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u6211\u4eec\u5c06\u5728\u4ecb\u7ecd\u4e86\u4ec0\u4e48\u662f\u7c7b\u4e4b\u540e\u518d\u8ba8\u8bba \u7c7b\u6a21\u677f \u3002","title":"C++\u6a21\u677f\u7b80\u4ecb"},{"location":"8-13-Function-templates/#_1","text":"\u51fd\u6570\u6a21\u677f \u662f\u4e00\u79cd\u7c7b\u4f3c\u51fd\u6570\u7684\u5b9a\u4e49\uff0c\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u6216\u591a\u4e2a\u91cd\u8f7d\u51fd\u6570\uff0c\u6bcf\u4e2a\u91cd\u8f7d\u51fd\u6570\u90fd\u6709\u4e00\u7ec4\u4e0d\u540c\u7684\u5b9e\u9645\u7c7b\u578b\u3002\u8fd9\u5c06\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u53ef\u4ee5\u5904\u7406\u8bb8\u591a\u4e0d\u540c\u7c7b\u578b\u7684\u51fd\u6570\u3002 \u5728\u521b\u5efa\u51fd\u6570\u6a21\u677f\u65f6\uff0c\u5bf9\u4e8e\u6211\u4eec\u5e0c\u671b\u7a0d\u540e\u6307\u5b9a\u7684\u4efb\u4f55\u5f62\u53c2\u7c7b\u578b\u3001\u8fd4\u56de\u7c7b\u578b\u6216\u51fd\u6570\u4f53\u4e2d\u4f7f\u7528\u7684\u7c7b\u578b\uff0c\u6211\u4eec\u90fd\u4f7f\u7528\u5360\u4f4d\u7b26\u7c7b\u578b(\u4e5f\u79f0\u4e3a\u6a21\u677f\u7c7b\u578b)\u3002 \u51fd\u6570\u6a21\u677f\u6700\u597d\u901a\u8fc7\u793a\u4f8b\u6765\u6559\u6388\uff0c\u6240\u4ee5\u8ba9\u6211\u4eec\u5c06\u4e0a\u9762\u793a\u4f8b\u4e2d\u7684 max(int, int) \u51fd\u6570\u8f6c\u6362\u4e3a\u51fd\u6570\u6a21\u677f\u3002\u5b83\u51fa\u5947\u5730\u7b80\u5355\uff0c\u6211\u4eec\u5c06\u89e3\u91ca\u8fd9\u4e00\u8fc7\u7a0b\u4e2d\u53d1\u751f\u4e86\u4ec0\u4e48\u3002","title":"\u51fd\u6570\u6a21\u677f"},{"location":"8-13-Function-templates/#max","text":"\u4e0b\u9762\u4ee3\u7801\u662f int \u7248\u672c\u7684 max() \uff1a 1 2 3 4 int max ( int x , int y ) { return ( x > y ) ? x : y ; } \u6ce8\u610f\uff0c\u6211\u4eec\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\u4f7f\u7528\u4e86\u4e09\u6b21 int \u7c7b\u578b\uff1a\u4e00\u6b21\u7528\u4e8e\u5f62\u53c2 x \uff0c\u4e00\u6b21\u7528\u4e8e\u5f62\u53c2 y \uff0c\u8fd8\u6709\u4e00\u6b21\u7528\u4e8e\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u3002 \u8981\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6a21\u677f\uff0c\u6211\u4eec\u8981\u505a\u4e24\u4ef6\u4e8b\u3002\u9996\u5148\uff0c\u6211\u4eec\u5c06\u7528\u6a21\u677f\u7c7b\u578b\u66ff\u6362\u7279\u5b9a\u7c7b\u578b\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u53ea\u6709\u4e00\u4e2a\u9700\u8981\u66ff\u6362\u7684\u7c7b\u578b( int )\uff0c\u6240\u4ee5\u53ea\u9700\u8981\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\u3002\u901a\u5e38\u7ea6\u5b9a\u4f7f\u7528\u5355\u4e2a\u5927\u5199\u5b57\u6bcd\uff08\u4ece\u5927\u5199\u5b57\u6bcd T \u5f00\u59cb\uff09\u8868\u793a\u6a21\u677f\u7c7b\u578b\u3002 \u4e0b\u9762\u662f\u6211\u4eec\u4f7f\u7528\u5355\u4e00\u6a21\u677f\u7c7b\u578b\u7684\u65b0\u51fd\u6570\uff1a 1 2 3 4 T max ( T x , T y ) // won't compile because we haven't defined T { return ( x > y ) ? x : y ; } \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u5355\u4e2a\u5927\u5199\u5b57\u6bcd(\u4eceT\u5f00\u59cb)\u547d\u540d\u6a21\u677f\u7c7b\u578b(\u4f8b\u5982 T, U, V \u7b49\u2026) \u8fd9\u662f\u4e00\u4e2a\u597d\u7684\u5f00\u59cb\u2014\u2014\u7136\u800c\uff0c\u5b83\u4e0d\u4f1a\u7f16\u8bd1\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053 T \u662f\u4ec0\u4e48\uff01\u8fd9\u4ecd\u7136\u662f\u4e00\u4e2a\u666e\u901a\u7684\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u51fd\u6570\u6a21\u677f\u3002 \u5176\u6b21\uff0c\u6211\u4eec\u4f1a\u544a\u8bc9\u7f16\u8bd1\u5668\u8fd9\u662f\u4e00\u4e2a\u51fd\u6570\u6a21\u677f\uff0c\u800c T \u662f\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\u3002\u8fd9\u662f\u901a\u8fc7\u6240\u8c13\u7684\u6a21\u677f\u53c2\u6570\u58f0\u660e\u6765\u5b8c\u6210\u7684\uff1a 1 2 3 4 5 template < typename T > // this is the template parameter declaration T max ( T x , T y ) // this is the function template definition for max { return ( x > y ) ? x : y ; } \u4ed4\u7ec6\u89c2\u5bdf\u6a21\u677f\u53c2\u6570\u58f0\u660e\u3002\u9996\u5148\u4f7f\u7528\u5173\u952e\u5b57 template \u544a\u8bc9\u7f16\u8bd1\u5668\u521b\u5efa\u4e00\u4e2a\u6a21\u677f\u3002\u63a5\u4e0b\u6765\uff0c\u5728\u5c16\u62ec\u53f7( <> )\u4e2d\u6307\u5b9a\u6a21\u677f\u5c06\u4f7f\u7528\u7684 \u6240\u6709\u6a21\u677f\u7c7b\u578b \u3002\u5bf9\u4e8e\u6bcf\u4e2a\u6a21\u677f\u7c7b\u578b\uff0c\u6211\u4eec\u4f7f\u7528\u5173\u952e\u5b57 typename \u6216 class \uff0c\u540e\u9762\u8ddf\u7740\u6a21\u677f\u7c7b\u578b\u7684\u540d\u79f0(\u4f8b\u5982 \u201cT\u201d)\u3002 \u6bcf\u4e2a\u6a21\u677f\u51fd\u6570(\u6216\u6a21\u677f\u7c7b)\u90fd\u9700\u8981\u58f0\u660e\u81ea\u5df1\u7684\u6a21\u677f\u5f62\u53c2\u3002 \u9898\u5916\u8bdd \u5728\u8fd9\u4e2a\u4e0a\u4e0b\u6587\u4e2d\uff0c typename \u548c class \u5173\u952e\u5b57\u4e4b\u95f4\u6ca1\u6709\u533a\u522b\u3002\u4f60\u4f1a\u7ecf\u5e38\u770b\u5230\u4eba\u4eec\u4f7f\u7528 class \u5173\u952e\u5b57\uff0c\u56e0\u4e3a\u5b83\u66f4\u65e9\u5730\u88ab\u5f15\u5165\u5230\u8bed\u8a00\u4e2d\u3002\u7136\u800c\uff0c\u6211\u4eec\u66f4\u559c\u6b22\u65b0\u5f0f\u7684 typename \u5173\u952e\u5b57\uff0c\u56e0\u4e3a\u5b83\u66f4\u6e05\u695a\u5730\u8868\u660e\u6a21\u677f\u7c7b\u578b\u53ef\u4ee5\u88ab\u4efb\u4f55\u7c7b\u578b(\u4f8b\u5982\u57fa\u672c\u7c7b\u578b)\u66ff\u6362\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u7c7b\u7c7b\u578b\u3002 \u56e0\u4e3a\u8fd9\u4e2a\u51fd\u6570\u6a21\u677f\u53ea\u9700\u8981\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\uff0c\u79f0\u4e3a T \uff0c\u6211\u4eec\u79f0\u5176\u4e3a max \u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728\u8bfe\u7a0b 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f \u4e2d\u8ba8\u8bba\u5982\u4f55\u521b\u5efa\u5177\u6709\u591a\u79cd\u6a21\u677f\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f\u3002 \u641e\u5b9a\uff01\u7b80\u5355\u5230\u4ee4\u4eba\u96be\u4ee5\u7f6e\u4fe1\u3002 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u5982\u4f55\u4f7f\u7528 max \u51fd\u6570\u6a21\u677f\u751f\u6210\u5e26\u6709\u4e0d\u540c\u7c7b\u578b\u53c2\u6570\u7684 max() \u51fd\u6570\u3002","title":"\u521b\u5efa\u6a21\u677f\u5316\u7684max\u51fd\u6570"},{"location":"8-14-Function-template-instantiation/","tags":["function template"],"text":"Key Takeaway \u5728\u4e0a\u8282\u8bfe\u4e2d ( 8.13 - \u51fd\u6570\u6a21\u677f )\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86 \u51fd\u6570\u6a21\u677f \u4ee5\u53ca\u5982\u4f55\u5c06\u666e\u901a\u51fd\u6570 max() \u8f6c\u6362\u6210 max \u51fd\u6570\u6a21\u677f\uff1a 1 2 3 4 5 template < typename T > T max ( T x , T y ) { return ( x > y ) ? x : y ; } \u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u51fd\u6570\u6a21\u677f\u3002 \u4f7f\u7528\u51fd\u6570\u6a21\u677f \u51fd\u6570\u6a21\u677f\u5e76\u4e0d\u662f\u771f\u6b63\u7684\u51fd\u6570\uff0c\u5176\u4ee3\u7801\u4e0d\u80fd\u88ab\u76f4\u63a5\u7f16\u8bd1\u6216\u6267\u884c\u3002\u51fd\u6570\u6a21\u677f\u53ea\u6709\u4e00\u4e2a\u529f\u80fd\uff1a\u751f\u6210\u51fd\u6570\uff08\u53ef\u4ee5\u88ab\u7f16\u8bd1\u6267\u884c\uff09\u3002 \u4e3a\u4e86\u4f7f\u7528 max \u51fd\u6570\u6a21\u677f\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u5982\u4e0b\u8bed\u6cd5\uff1a 1 max < actual_type > ( arg1 , arg2 ); // actual_type \u662f\u5b9e\u9645\u7c7b\u578b\uff0c\u4f8b\u5982 int\u6216double \u8fd9\u770b\u8d77\u6765\u5f88\u50cf\u666e\u901a\u7684\u51fd\u6570\u8c03\u7528\u2014\u2014\u4e3b\u8981\u7684\u533a\u522b\u662f\u5728\u5c16\u62ec\u53f7\u4e2d\u6dfb\u52a0\u4e86\u7c7b\u578b(\u79f0\u4e3a\u6a21\u677f\u5b9e\u53c2)\uff0c\u5b83\u6307\u5b9a\u4e86\u5c06\u7528\u4e8e\u4ee3\u66ff\u6a21\u677f\u7c7b\u578b\u201cT\u201d\u7684\u5b9e\u9645\u7c7b\u578b\u3002 \u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include template < typename T > T max ( T x , T y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(int, int) return 0 ; } \u5f53\u7f16\u8bd1\u5668\u9047\u5230\u51fd\u6570\u8c03\u7528 max(1,2) \u65f6\uff0c\u5b83\u5c06\u786e\u5b9a max(int, int) \u7684\u51fd\u6570\u5b9a\u4e49\u4e0d\u5b58\u5728\u3002\u56e0\u6b64\uff0c\u7f16\u8bd1\u5668\u5c06\u4f7f\u7528 max \u51fd\u6570\u6a21\u677f\u6765\u521b\u5efa\u4e00\u4e2a\u3002 \u4ece\u51fd\u6570\u6a21\u677f(\u5e26\u6709\u6a21\u677f\u7c7b\u578b)\u521b\u5efa\u51fd\u6570(\u5e26\u6709\u7279\u5b9a\u7c7b\u578b)\u7684\u8fc7\u7a0b\u79f0\u4e3a \u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316 (\u6216\u7b80\u79f0\u5b9e\u4f8b\u5316)\u3002\u5f53\u6b64\u8fc7\u7a0b\u7531\u4e8e\u51fd\u6570\u8c03\u7528\u800c\u53d1\u751f\u65f6\uff0c\u79f0\u4e3a\u9690\u5f0f\u5b9e\u4f8b\u5316\u3002\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u901a\u5e38\u79f0\u4e3a \u51fd\u6570\u5b9e\u4f8b (\u7b80\u79f0\u5b9e\u4f8b)\u6216\u6a21\u677f\u51fd\u6570\u3002\u51fd\u6570\u5b9e\u4f8b\u5728\u6240\u6709\u65b9\u9762\u90fd\u662f\u6b63\u5e38\u7684\u51fd\u6570\u3002 \u5b9e\u4f8b\u5316\u51fd\u6570\u7684\u8fc7\u7a0b\u5f88\u7b80\u5355\uff1a\u7f16\u8bd1\u5668\u57fa\u672c\u4e0a\u662f\u514b\u9686\u51fd\u6570\u6a21\u677f\uff0c\u5e76\u7528\u5b9e\u9645\u7c7b\u578b( int )\u66ff\u6362\u6a21\u677f\u7c7b\u578b( T )\u3002 \u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u8c03\u7528 max(1,2) \u65f6\uff0c\u88ab\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u770b\u8d77\u6765\u50cf\u8fd9\u6837\uff1a 1 2 3 4 5 template <> // ignore this for now int max < int > ( int x , int y ) // the generated function max(int, int) { return ( x > y ) ? x : y ; } \u4e0b\u9762\u662f\u4e0e\u4e0a\u9762\u76f8\u540c\u7684\u793a\u4f8b\uff0c\u663e\u793a\u4e86\u7f16\u8bd1\u5668\u5728\u6240\u6709\u5b9e\u4f8b\u5316\u5b8c\u6210\u540e\u5b9e\u9645\u7f16\u8bd1\u7684\u5185\u5bb9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include // a declaration for our function template (we don't need the definition any more) template < typename T > T max ( T x , T y ); template <> int max < int > ( int x , int y ) // the generated function max(int, int) { return ( x > y ) ? x : y ; } int main () { std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(int, int) return 0 ; } \u4f60\u53ef\u4ee5\u81ea\u5df1\u7f16\u8bd1\u5b83\uff0c\u770b\u770b\u5b83\u662f\u5426\u5de5\u4f5c\u3002\u4e00\u4e2a\u5df2\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u53ea\u5728\u7b2c\u4e00\u6b21\u8c03\u7528\u51fd\u6570\u65f6\u88ab\u5b9e\u4f8b\u5316\u3002\u5bf9\u8be5\u51fd\u6570\u7684\u8fdb\u4e00\u6b65\u8c03\u7528\u88ab\u8def\u7531\u5230\u5df2\u7ecf\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u3002 \u518d\u4e3e\u4e00\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include template < typename T > T max ( T x , T y ) // function template for max(T, T) { return ( x > y ) ? x : y ; } int main () { std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(int, int) std :: cout << max < int > ( 4 , 3 ) << '\\n' ; // calls already instantiated function max(int, int) std :: cout << max < double > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(double, double) return 0 ; } \u8fd9\u4e0e\u524d\u9762\u7684\u4f8b\u5b50\u7c7b\u4f3c\uff0c\u4f46\u662f\u6211\u4eec\u7684\u51fd\u6570\u6a21\u677f\u8fd9\u6b21\u5c06\u7528\u4e8e\u751f\u6210\u4e24\u4e2a\u51fd\u6570:\u4e00\u6b21\u5c06 T \u66ff\u6362\u4e3a int \uff0c\u53e6\u4e00\u6b21\u5c06 T \u66ff\u6362\u4e3a double \u3002\u5728\u6240\u6709\u5b9e\u4f8b\u5316\u4e4b\u540e\uff0c\u7a0b\u5e8f\u770b\u8d77\u6765\u50cf\u8fd9\u6837: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include // a declaration for our function template (we don't need the definition any more) template < typename T > T max ( T x , T y ); template <> int max < int > ( int x , int y ) // the generated function max(int, int) { return ( x > y ) ? x : y ; } template <> double max < double > ( double x , double y ) // the generated function max(double, double) { return ( x > y ) ? x : y ; } int main () { std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(int, int) std :: cout << max < int > ( 4 , 3 ) << '\\n' ; // calls already instantiated function max(int, int) std :: cout << max < double > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(double, double) return 0 ; } \u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a\u5f53\u5b9e\u4f8b\u5316 max \u65f6\uff0c\u5176 \u5b9e\u53c2 \u7c7b\u578b\u4e3a double \u3002\u56e0\u4e3a\u6211\u4eec\u63d0\u4f9b\u7684\u662f \u5b9e\u53c2 \u662f int \uff0c\u6240\u4ee5\u5b83\u4f1a\u88ab \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u4e3a double \u3002 \u6a21\u677f\u53c2\u6570\u63a8\u65ad \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u7528\u4e8e\u5b9e\u4f8b\u5316\u7684\u5b9e\u9645\u7c7b\u578b\u5c06\u4e0e\u51fd\u6570\u5f62\u53c2\u7684\u7c7b\u578b\u5339\u914d\u3002\u4f8b\u5982\uff1a 1 std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // specifying we want to call max \u5728\u8fd9\u4e2a\u51fd\u6570\u8c03\u7528\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u6307\u5b9a\u6211\u4eec\u60f3\u7528 int \u66ff\u6362 T \uff0c\u4f46\u6211\u4eec\u4e5f\u8c03\u7528\u4e86\u5e26\u6709 int \u53c2\u6570\u7684\u51fd\u6570\u3002 \u5728\u5b9e\u53c2\u7684\u7c7b\u578b\u4e0e\u6211\u4eec\u60f3\u8981\u7684\u5b9e\u9645\u7c7b\u578b\u76f8\u5339\u914d\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u6307\u5b9a\u5b9e\u9645\u7684\u7c7b\u578b\u2014\u2014\u76f8\u53cd\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6a21\u677f\u5b9e\u53c2\u63a8\u5bfc\uff0c\u8ba9\u7f16\u8bd1\u5668\u4ece\u51fd\u6570\u8c03\u7528\u4e2d\u7684\u5b9e\u53c2\u7c7b\u578b\u63a8\u5bfc\u51fa\u5e94\u8be5\u4f7f\u7528\u7684\u5b9e\u9645\u7c7b\u578b\u3002 \u4f8b\u5982\uff0c\u4e0d\u7528\u50cf\u8fd9\u6837\u8c03\u7528\u51fd\u6570\uff1a 1 std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // specifying we want to call max \u8fd9\u4e48\u505a\u5373\u53ef\uff1a 1 2 std :: cout << max <> ( 1 , 2 ) << '\\n' ; std :: cout << max ( 1 , 2 ) << '\\n' ; \u5bf9\u4e8e\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u4e24\u4e2a\u8bed\u53e5\uff0c\u7f16\u8bd1\u5668\u4f1a\u53d1\u73b0\u6211\u4eec\u6ca1\u6709\u63d0\u4f9b\u5b9e\u9645\u7c7b\u578b\uff0c\u6240\u4ee5\u5b83\u4f1a\u5c1d\u8bd5\u4ece\u51fd\u6570 \u5b9e\u53c2 \u63a8\u65ad\u5b9e\u9645\u7c7b\u578b\u4ee5\u4f7f\u5176\u80fd\u591f\u751f\u6210\u5408\u9002\u7684 max() \u51fd\u6570\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u7f16\u8bd1\u5668\u80fd\u591f\u901a\u8fc7\u5b9e\u9645\u7c7b\u578b int \u63a8\u65ad\u51fa\u6a21\u677f\u7c7b\u578b\u5e76\u5b9e\u4f8b\u5316\u51fd\u6570 max(int, int) \uff0c\u5176\u6240\u6709\u6a21\u677f\u5f62\u53c2\u90fd\u662f( int )\uff0c\u80fd\u591f\u5339\u914d\u63d0\u4f9b\u7684\u5b9e\u53c2\u7c7b\u578b( int )\u3002 \u8fd9\u4e24\u79cd\u60c5\u51b5\u7684\u533a\u522b\u5728\u4e8e\u7f16\u8bd1\u5668\u5982\u4f55\u89e3\u6790\u6765\u81ea\u4e00\u7ec4\u91cd\u8f7d\u51fd\u6570\u7684\u51fd\u6570\u8c03\u7528\u3002\u5728\u9876\u90e8\u7684\u60c5\u51b5\u4e0b(\u5e26\u6709\u7a7a\u5c16\u62ec\u53f7)\uff0c\u7f16\u8bd1\u5668\u5728\u51b3\u5b9a\u8c03\u7528\u54ea\u4e2a\u91cd\u8f7d\u51fd\u6570\u65f6\uff0c\u53ea\u4f1a\u8003\u8651 max \u6a21\u677f\u51fd\u6570\u91cd\u8f7d\u3002\u5728\u4e0b\u9762\u4e00\u4e2a\u4f8b\u5b50(\u6ca1\u6709\u5c16\u62ec\u53f7)\u4e2d\uff0c\u7f16\u8bd1\u5668\u5c06\u540c\u65f6\u8003\u8651 max \u6a21\u677f\u51fd\u6570\u91cd\u8f7d\u548c max \u975e\u6a21\u677f\u51fd\u6570\u91cd\u8f7d\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include template < typename T > T max ( T x , T y ) { std :: cout << \"called max(int, int) \\n \" ; return ( x > y ) ? x : y ; } int max ( int x , int y ) { std :: cout << \"called max(int, int) \\n \" ; return ( x > y ) ? x : y ; } int main () { std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // selects max(int, int) std :: cout << max <> ( 1 , 2 ) << '\\n' ; // deduces max(int, int) (non-template functions not considered) std :: cout << max ( 1 , 2 ) << '\\n' ; // calls function max(int, int) return 0 ; } \u8bf7\u6ce8\u610f\uff0c\u6700\u4e0b\u9762\u7684\u5199\u6cd5\u770b\u8d77\u6765\u4e0e\u6b63\u5e38\u7684\u51fd\u6570\u8c03\u7528\u662f\u4e00\u6837\u7684\uff01\u8fd9\u901a\u5e38\u662f\u8c03\u7528\u51fd\u6570\u6a21\u677f\u65f6\u4f7f\u7528\u7684\u9996\u9009\u8bed\u6cd5(\u5728\u4ee5\u540e\u7684\u793a\u4f8b\u4e2d\uff0c\u5982\u679c\u53ef\u884c\uff0c\u6211\u4eec\u5c06\u9ed8\u8ba4\u4f7f\u7528\u8fd9\u79cd\u8bed\u6cd5)\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u4f7f\u7528\u51fd\u6570\u6a21\u677f\u65f6\uff0c\u4f7f\u7528\u6b63\u5e38\u7684\u51fd\u6570\u8c03\u7528\u8bed\u6cd5\u3002 \u5e26\u6709\u975e\u6a21\u677f\u53c2\u6570\u7684\u51fd\u6570\u6a21\u677f \u53ef\u4ee5\u521b\u5efa\u540c\u65f6\u5177\u6709\u6a21\u677f\u7c7b\u578b\u548c\u975e\u6a21\u677f\u7c7b\u578b\u53c2\u6570\u7684\u51fd\u6570\u6a21\u677f\u3002\u6a21\u677f\u5f62\u53c2\u53ef\u4ee5\u5339\u914d\u4efb\u4f55\u7c7b\u578b\uff0c\u975e\u6a21\u677f\u5f62\u53c2\u7684\u5de5\u4f5c\u65b9\u5f0f\u4e0e\u666e\u901a\u51fd\u6570\u7684\u5f62\u53c2\u7c7b\u4f3c\u3002 \u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 template < typename T > int someFcn ( T x , double y ) { return 5 ; } int main () { someFcn ( 1 , 3.4 ); // matches someFcn(int, double) someFcn ( 1 , 3.4f ); // matches someFcn(int, double) -- the float is promoted to a double someFcn ( 1.2 , 3.4 ); // matches someFcn(double, double) someFcn ( 1.2f , 3.4 ); // matches someFcn(float, double) someFcn ( 1.2f , 3.4f ); // matches someFcn(float, double) -- the float is promoted to a double return 0 ; } \u8fd9\u4e2a\u51fd\u6570\u6a21\u677f\u6709\u4e00\u4e2a\u6a21\u677f\u5316\u7684\u7b2c\u4e00\u4e2a\u5f62\u53c2\uff0c\u4f46\u662f\u7b2c\u4e8c\u4e2a\u5f62\u53c2\u7684\u7c7b\u578b\u662f\u56fa\u5b9a\u7684 double \u3002\u6ce8\u610f\uff0c\u8fd4\u56de\u7c7b\u578b\u4e5f\u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u7684\u51fd\u6570\u603b\u662f\u8fd4\u56de\u4e00\u4e2a int \u503c\u3002 \u6ce8\u610f\uff0c\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u4e0d\u4fdd\u8bc1\u4e00\u5b9a\u80fd\u591f\u7f16\u8bd1\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include template < typename T > T addOne ( T x ) { return x + 1 ; } int main () { std :: cout << addOne ( 1 ) << '\\n' ; std :: cout << addOne ( 2.3 ) << '\\n' ; return 0 ; } \u7f16\u8bd1\u5668\u4f1a\u7f16\u8bd1\u548c\u6267\u884c\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include template < typename T > T addOne ( T x ); template <> int addOne < int > ( int x ) { return x + 1 ; } template <> double addOne < double > ( double x ) { return x + 1 ; } int main () { std :: cout << addOne ( 1 ) << '\\n' ; // calls addOne(int) std :: cout << addOne ( 2.3 ) << '\\n' ; // calls addOne(double) return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 2 2 3.3 \u4f46\u662f\u5982\u679c\u6211\u4eec\u8fd9\u4e48\u505a\u5462\uff1f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include template < typename T > T addOne ( T x ) { return x + 1 ; } int main () { std :: string hello { \"Hello, world!\" }; std :: cout << addOne ( hello ) << '\\n' ; return 0 ; } \u5f53\u7f16\u8bd1\u5668\u5c1d\u8bd5\u89e3\u6790 addOne(hello) \u65f6\uff0c\u5b83\u65e0\u6cd5\u627e\u5230\u4e00\u4e2a\u975e\u6a21\u677f\u51fd\u6570\u6765\u5339\u914d addOne(std::string) \uff0c\u4f46\u662f\u80fd\u591f\u627e\u5230\u4e00\u4e2a\u51fd\u6570\u6a21\u677f addOne(T) \uff0c\u5e76\u901a\u8fc7\u8be5\u6a21\u677f\u751f\u6210 addOne(std::string) \u51fd\u6570\u3002\u56e0\u6b64\u7f16\u8bd1\u5668\u4f1a\u751f\u6210\u5e76\u7f16\u8bd1\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include template < typename T > T addOne ( T x ); template <> std :: string addOne < std :: string > ( std :: string x ) { return x + 1 ; } int main () { std :: string hello { \"Hello, world!\" }; std :: cout << addOne ( hello ) << '\\n' ; return 0 ; } \u663e\u7136\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\uff0c\u56e0\u4e3a x + 1 \u5e76\u4e0d\u9002\u7528\u4e8e x \u4e3a std::string \u7c7b\u578b\u7684\u60c5\u51b5\u3002\u89e3\u51b3\u529e\u6cd5\u5c31\u662f\u4e0d\u8981\u4f7f\u7528 std::string \u7c7b\u578b\u6765\u8c03\u7528 addOne() \u3002 \u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528\u51fd\u6570\u6a21\u677f \u4e3a\u4e86\u5b9e\u4f8b\u5316\u6a21\u677f\uff0c\u7f16\u8bd1\u5668\u9700\u8981\u770b\u5230\u6a21\u677f\u7684\u5b8c\u6574\u5b9a\u4e49\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u6211\u4eec\u60f3\u5728\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\u4f7f\u7528\u51fd\u6570\u6a21\u677f\uff0c\u6bcf\u4e2a\u4ee3\u7801\u6587\u4ef6\u90fd\u9700\u8981\u51fd\u6570\u6a21\u677f\u5b9a\u4e49\u7684\u526f\u672c\u3002\u56e0\u6b64\uff0c \u6a21\u677f\u901a\u5e38\u7f16\u5199\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u5b83\u4eec\u53ef\u4ee5\u88ab #include \u5230\u4efb\u4f55\u60f3\u8981\u4f7f\u7528\u5b83\u4eec\u7684\u4ee3\u7801\u6587\u4ef6\u4e2d\u3002 \u6a21\u677f\u5b9a\u4e49\u5e76\u4e0d\u53d7\u9650\u4e8e \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \uff0c\u800c\u4e14\u7531\u4e8e\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u901a\u5e38\u662f\u9690\u5f0f \u5185\u8054\u51fd\u6570 \uff0c\u6240\u4ee5\u4e5f\u4e0d\u53d7\u9650\u4e8e\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u3002 Max.h 1 2 3 4 5 6 7 8 9 10 #ifndef MAX_H #define MAX_H template < typename T > T max ( T x , T y ) { return ( x > y ) ? x : y ; } #endif Foo.cp 1 2 3 4 5 6 7 #include \"Max.h\" // import template definition for max() #include void foo () { std :: cout << max ( 3 , 2 ); } main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 #include \"Max.h\" // import template definition for max() #include void foo (); // forward declaration for function foo int main () { std :: cout << max ( 3 , 5 ); foo (); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c main.cpp \u548c foo.cpp \u90fd #include \"Max.h\" \uff0c\u6240\u4ee5\u8fd9\u4e24\u4e2a\u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\u90fd\u53ef\u4ee5\u4f7f\u7528 max \u51fd\u6570\u6a21\u677f\u3002 \u6cdb\u578b\u7f16\u7a0b \u56e0\u4e3a\u6a21\u677f\u7c7b\u578b\u53ef\u4ee5\u7528\u4efb\u4f55\u5b9e\u9645\u7c7b\u578b\u66ff\u6362\uff0c\u6240\u4ee5\u6a21\u677f\u7c7b\u578b\u6709\u65f6\u88ab\u79f0\u4e3a\u6cdb\u578b\u7c7b\u578b\u3002\u7531\u4e8e\u6a21\u677f\u53ef\u4ee5\u4e0d\u53d7\u5177\u4f53\u7c7b\u578b\u7684\u5f71\u54cd\u800c\u7f16\u5199\uff0c\u56e0\u6b64\u4f7f\u7528\u6a21\u677f\u8fdb\u884c\u7f16\u7a0b\u6709\u65f6\u88ab\u79f0\u4e3a\u6cdb\u578b\u7f16\u7a0b\u3002C++\u901a\u5e38\u975e\u5e38\u5173\u6ce8\u7c7b\u578b\u548c\u7c7b\u578b\u68c0\u67e5\uff0c\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u6cdb\u578b\u7f16\u7a0b\u8ba9\u6211\u4eec\u4e13\u6ce8\u4e8e\u7b97\u6cd5\u7684\u903b\u8f91\u548c\u6570\u636e\u7ed3\u6784\u7684\u8bbe\u8ba1\uff0c\u800c\u4e0d\u5fc5\u8fc7\u591a\u5730\u62c5\u5fc3\u7c7b\u578b\u4fe1\u606f\u3002 \u5c0f\u7ed3 \u4e00\u65e6\u4e60\u60ef\u4e86\u7f16\u5199\u51fd\u6570\u6a21\u677f\uff0c\u5c31\u4f1a\u53d1\u73b0\u7f16\u5199\u51fd\u6570\u6a21\u677f\u6240\u82b1\u7684\u65f6\u95f4\u5b9e\u9645\u4e0a\u5e76\u4e0d\u6bd4\u5177\u6709\u5b9e\u9645\u7c7b\u578b\u7684\u51fd\u6570\u957f\u591a\u5c11\u3002\u51fd\u6570\u6a21\u677f\u901a\u8fc7\u6700\u5c0f\u5316\u9700\u8981\u7f16\u5199\u548c\u7ef4\u62a4\u7684\u4ee3\u7801\u6570\u91cf\uff0c\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u4ee3\u7801\u7ef4\u62a4\u548c\u9519\u8bef\u3002 \u51fd\u6570\u6a21\u677f\u4e5f\u786e\u5b9e\u6709\u4e00\u4e9b \u7f3a\u70b9 \uff0c\u5982\u679c\u6211\u4eec\u4e0d\u63d0\u53ca\u5b83\u4eec\uff0c\u90a3\u5c31\u662f\u5931\u804c\u4e86\u3002\u9996\u5148\uff0c\u7f16\u8bd1\u5668\u5c06\u4e3a\u6bcf\u4e2a\u51fd\u6570\u8c03\u7528\u521b\u5efa(\u5e76\u7f16\u8bd1)\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5177\u6709\u4e00\u7ec4\u60df\u4e00\u7684\u53c2\u6570\u7c7b\u578b\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u51fd\u6570\u6a21\u677f\u7f16\u5199\u8d77\u6765\u5f88\u7d27\u51d1\uff0c\u4f46\u5b83\u4eec\u53ef\u80fd\u4f1a\u88ab\u6269\u5c55\u6210\u5927\u91cf\u7684\u4ee3\u7801\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u4ee3\u7801\u81a8\u80c0\u548c\u7f16\u8bd1\u65f6\u95f4\u53d8\u6162\u3002\u51fd\u6570\u6a21\u677f \u66f4\u5927\u7684\u7f3a\u70b9 \u662f\uff0c\u5b83\u4eec\u5f80\u5f80\u4f1a\u4ea7\u751f\u770b\u8d77\u6765\u5f88\u75af\u72c2\u3001\u8fd1\u4e4e\u4e0d\u53ef\u8bfb\u7684\u9519\u8bef\u6d88\u606f\uff0c\u6bd4\u5e38\u89c4\u51fd\u6570\u7684\u9519\u8bef\u6d88\u606f\u66f4\u96be\u89e3\u8bfb\u3002\u8fd9\u4e9b\u9519\u8bef\u6d88\u606f\u53ef\u80fd\u76f8\u5f53\u5413\u4eba\uff0c\u4f46\u4e00\u65e6\u4f60\u80fd\u591f\u7406\u89e3\u4e86\u5b83\u8bd5\u56fe\u4f20\u8fbe\u7684\u4fe1\u606f\uff0c\u5176\u6307\u51fa\u7684\u95ee\u9898\u901a\u5e38\u975e\u5e38\u5bb9\u6613\u89e3\u51b3\u3002 \u6a21\u677f\u4e3a\u7f16\u7a0b\u5de5\u5177\u5305\u5e26\u6765\u4e86\u5f3a\u5927\u529f\u80fd\u548c\u5b89\u5168\u6027\uff0c\u8fd9\u4e9b\u7f3a\u70b9\u53ef\u4ee5\u8bf4\u662f\u7455\u4e0d\u63a9\u745c\u4e86\u3002\u6240\u4ee5\u5728\u4efb\u4f55\u9700\u8981\u7c7b\u578b\u7075\u6d3b\u6027\u7684\u5730\u65b9\u90fd\u53ef\u4ee5\u81ea\u7531\u4f7f\u7528\u6a21\u677f\uff01\u4e00\u4e2a\u597d\u7684\u7ecf\u9a8c\u6cd5\u5219\u662f\uff1a \u9996\u5148\u521b\u5efa\u666e\u901a\u51fd\u6570\uff0c\u5f53\u53d1\u73b0\u9700\u8981\u9488\u5bf9\u4e0d\u540c\u53c2\u6570\u7c7b\u578b \u91cd\u8f7d \u51fd\u6570\u65f6\uff0c\u5219\u5c06\u5b83\u4eec\u8f6c\u6362\u4e3a\u51fd\u6570\u6a21\u677f\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u51fd\u6570\u6a21\u677f\u6765\u7f16\u5199\u6cdb\u578b\u4ee3\u7801\uff0c\u8fd9\u4e9b\u4ee3\u7801\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u5904\u7406\u5404\u79cd\u7c7b\u578b\u3002","title":"8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316"},{"location":"8-14-Function-template-instantiation/#_1","text":"\u51fd\u6570\u6a21\u677f\u5e76\u4e0d\u662f\u771f\u6b63\u7684\u51fd\u6570\uff0c\u5176\u4ee3\u7801\u4e0d\u80fd\u88ab\u76f4\u63a5\u7f16\u8bd1\u6216\u6267\u884c\u3002\u51fd\u6570\u6a21\u677f\u53ea\u6709\u4e00\u4e2a\u529f\u80fd\uff1a\u751f\u6210\u51fd\u6570\uff08\u53ef\u4ee5\u88ab\u7f16\u8bd1\u6267\u884c\uff09\u3002 \u4e3a\u4e86\u4f7f\u7528 max \u51fd\u6570\u6a21\u677f\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u5982\u4e0b\u8bed\u6cd5\uff1a 1 max < actual_type > ( arg1 , arg2 ); // actual_type \u662f\u5b9e\u9645\u7c7b\u578b\uff0c\u4f8b\u5982 int\u6216double \u8fd9\u770b\u8d77\u6765\u5f88\u50cf\u666e\u901a\u7684\u51fd\u6570\u8c03\u7528\u2014\u2014\u4e3b\u8981\u7684\u533a\u522b\u662f\u5728\u5c16\u62ec\u53f7\u4e2d\u6dfb\u52a0\u4e86\u7c7b\u578b(\u79f0\u4e3a\u6a21\u677f\u5b9e\u53c2)\uff0c\u5b83\u6307\u5b9a\u4e86\u5c06\u7528\u4e8e\u4ee3\u66ff\u6a21\u677f\u7c7b\u578b\u201cT\u201d\u7684\u5b9e\u9645\u7c7b\u578b\u3002 \u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include template < typename T > T max ( T x , T y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(int, int) return 0 ; } \u5f53\u7f16\u8bd1\u5668\u9047\u5230\u51fd\u6570\u8c03\u7528 max(1,2) \u65f6\uff0c\u5b83\u5c06\u786e\u5b9a max(int, int) \u7684\u51fd\u6570\u5b9a\u4e49\u4e0d\u5b58\u5728\u3002\u56e0\u6b64\uff0c\u7f16\u8bd1\u5668\u5c06\u4f7f\u7528 max \u51fd\u6570\u6a21\u677f\u6765\u521b\u5efa\u4e00\u4e2a\u3002 \u4ece\u51fd\u6570\u6a21\u677f(\u5e26\u6709\u6a21\u677f\u7c7b\u578b)\u521b\u5efa\u51fd\u6570(\u5e26\u6709\u7279\u5b9a\u7c7b\u578b)\u7684\u8fc7\u7a0b\u79f0\u4e3a \u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316 (\u6216\u7b80\u79f0\u5b9e\u4f8b\u5316)\u3002\u5f53\u6b64\u8fc7\u7a0b\u7531\u4e8e\u51fd\u6570\u8c03\u7528\u800c\u53d1\u751f\u65f6\uff0c\u79f0\u4e3a\u9690\u5f0f\u5b9e\u4f8b\u5316\u3002\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u901a\u5e38\u79f0\u4e3a \u51fd\u6570\u5b9e\u4f8b (\u7b80\u79f0\u5b9e\u4f8b)\u6216\u6a21\u677f\u51fd\u6570\u3002\u51fd\u6570\u5b9e\u4f8b\u5728\u6240\u6709\u65b9\u9762\u90fd\u662f\u6b63\u5e38\u7684\u51fd\u6570\u3002 \u5b9e\u4f8b\u5316\u51fd\u6570\u7684\u8fc7\u7a0b\u5f88\u7b80\u5355\uff1a\u7f16\u8bd1\u5668\u57fa\u672c\u4e0a\u662f\u514b\u9686\u51fd\u6570\u6a21\u677f\uff0c\u5e76\u7528\u5b9e\u9645\u7c7b\u578b( int )\u66ff\u6362\u6a21\u677f\u7c7b\u578b( T )\u3002 \u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u8c03\u7528 max(1,2) \u65f6\uff0c\u88ab\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u770b\u8d77\u6765\u50cf\u8fd9\u6837\uff1a 1 2 3 4 5 template <> // ignore this for now int max < int > ( int x , int y ) // the generated function max(int, int) { return ( x > y ) ? x : y ; } \u4e0b\u9762\u662f\u4e0e\u4e0a\u9762\u76f8\u540c\u7684\u793a\u4f8b\uff0c\u663e\u793a\u4e86\u7f16\u8bd1\u5668\u5728\u6240\u6709\u5b9e\u4f8b\u5316\u5b8c\u6210\u540e\u5b9e\u9645\u7f16\u8bd1\u7684\u5185\u5bb9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include // a declaration for our function template (we don't need the definition any more) template < typename T > T max ( T x , T y ); template <> int max < int > ( int x , int y ) // the generated function max(int, int) { return ( x > y ) ? x : y ; } int main () { std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(int, int) return 0 ; } \u4f60\u53ef\u4ee5\u81ea\u5df1\u7f16\u8bd1\u5b83\uff0c\u770b\u770b\u5b83\u662f\u5426\u5de5\u4f5c\u3002\u4e00\u4e2a\u5df2\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u53ea\u5728\u7b2c\u4e00\u6b21\u8c03\u7528\u51fd\u6570\u65f6\u88ab\u5b9e\u4f8b\u5316\u3002\u5bf9\u8be5\u51fd\u6570\u7684\u8fdb\u4e00\u6b65\u8c03\u7528\u88ab\u8def\u7531\u5230\u5df2\u7ecf\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u3002 \u518d\u4e3e\u4e00\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include template < typename T > T max ( T x , T y ) // function template for max(T, T) { return ( x > y ) ? x : y ; } int main () { std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(int, int) std :: cout << max < int > ( 4 , 3 ) << '\\n' ; // calls already instantiated function max(int, int) std :: cout << max < double > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(double, double) return 0 ; } \u8fd9\u4e0e\u524d\u9762\u7684\u4f8b\u5b50\u7c7b\u4f3c\uff0c\u4f46\u662f\u6211\u4eec\u7684\u51fd\u6570\u6a21\u677f\u8fd9\u6b21\u5c06\u7528\u4e8e\u751f\u6210\u4e24\u4e2a\u51fd\u6570:\u4e00\u6b21\u5c06 T \u66ff\u6362\u4e3a int \uff0c\u53e6\u4e00\u6b21\u5c06 T \u66ff\u6362\u4e3a double \u3002\u5728\u6240\u6709\u5b9e\u4f8b\u5316\u4e4b\u540e\uff0c\u7a0b\u5e8f\u770b\u8d77\u6765\u50cf\u8fd9\u6837: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include // a declaration for our function template (we don't need the definition any more) template < typename T > T max ( T x , T y ); template <> int max < int > ( int x , int y ) // the generated function max(int, int) { return ( x > y ) ? x : y ; } template <> double max < double > ( double x , double y ) // the generated function max(double, double) { return ( x > y ) ? x : y ; } int main () { std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(int, int) std :: cout << max < int > ( 4 , 3 ) << '\\n' ; // calls already instantiated function max(int, int) std :: cout << max < double > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(double, double) return 0 ; } \u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a\u5f53\u5b9e\u4f8b\u5316 max \u65f6\uff0c\u5176 \u5b9e\u53c2 \u7c7b\u578b\u4e3a double \u3002\u56e0\u4e3a\u6211\u4eec\u63d0\u4f9b\u7684\u662f \u5b9e\u53c2 \u662f int \uff0c\u6240\u4ee5\u5b83\u4f1a\u88ab \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u4e3a double \u3002","title":"\u4f7f\u7528\u51fd\u6570\u6a21\u677f"},{"location":"8-14-Function-template-instantiation/#_2","text":"\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u7528\u4e8e\u5b9e\u4f8b\u5316\u7684\u5b9e\u9645\u7c7b\u578b\u5c06\u4e0e\u51fd\u6570\u5f62\u53c2\u7684\u7c7b\u578b\u5339\u914d\u3002\u4f8b\u5982\uff1a 1 std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // specifying we want to call max \u5728\u8fd9\u4e2a\u51fd\u6570\u8c03\u7528\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u6307\u5b9a\u6211\u4eec\u60f3\u7528 int \u66ff\u6362 T \uff0c\u4f46\u6211\u4eec\u4e5f\u8c03\u7528\u4e86\u5e26\u6709 int \u53c2\u6570\u7684\u51fd\u6570\u3002 \u5728\u5b9e\u53c2\u7684\u7c7b\u578b\u4e0e\u6211\u4eec\u60f3\u8981\u7684\u5b9e\u9645\u7c7b\u578b\u76f8\u5339\u914d\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u6307\u5b9a\u5b9e\u9645\u7684\u7c7b\u578b\u2014\u2014\u76f8\u53cd\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6a21\u677f\u5b9e\u53c2\u63a8\u5bfc\uff0c\u8ba9\u7f16\u8bd1\u5668\u4ece\u51fd\u6570\u8c03\u7528\u4e2d\u7684\u5b9e\u53c2\u7c7b\u578b\u63a8\u5bfc\u51fa\u5e94\u8be5\u4f7f\u7528\u7684\u5b9e\u9645\u7c7b\u578b\u3002 \u4f8b\u5982\uff0c\u4e0d\u7528\u50cf\u8fd9\u6837\u8c03\u7528\u51fd\u6570\uff1a 1 std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // specifying we want to call max \u8fd9\u4e48\u505a\u5373\u53ef\uff1a 1 2 std :: cout << max <> ( 1 , 2 ) << '\\n' ; std :: cout << max ( 1 , 2 ) << '\\n' ; \u5bf9\u4e8e\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u4e24\u4e2a\u8bed\u53e5\uff0c\u7f16\u8bd1\u5668\u4f1a\u53d1\u73b0\u6211\u4eec\u6ca1\u6709\u63d0\u4f9b\u5b9e\u9645\u7c7b\u578b\uff0c\u6240\u4ee5\u5b83\u4f1a\u5c1d\u8bd5\u4ece\u51fd\u6570 \u5b9e\u53c2 \u63a8\u65ad\u5b9e\u9645\u7c7b\u578b\u4ee5\u4f7f\u5176\u80fd\u591f\u751f\u6210\u5408\u9002\u7684 max() \u51fd\u6570\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u7f16\u8bd1\u5668\u80fd\u591f\u901a\u8fc7\u5b9e\u9645\u7c7b\u578b int \u63a8\u65ad\u51fa\u6a21\u677f\u7c7b\u578b\u5e76\u5b9e\u4f8b\u5316\u51fd\u6570 max(int, int) \uff0c\u5176\u6240\u6709\u6a21\u677f\u5f62\u53c2\u90fd\u662f( int )\uff0c\u80fd\u591f\u5339\u914d\u63d0\u4f9b\u7684\u5b9e\u53c2\u7c7b\u578b( int )\u3002 \u8fd9\u4e24\u79cd\u60c5\u51b5\u7684\u533a\u522b\u5728\u4e8e\u7f16\u8bd1\u5668\u5982\u4f55\u89e3\u6790\u6765\u81ea\u4e00\u7ec4\u91cd\u8f7d\u51fd\u6570\u7684\u51fd\u6570\u8c03\u7528\u3002\u5728\u9876\u90e8\u7684\u60c5\u51b5\u4e0b(\u5e26\u6709\u7a7a\u5c16\u62ec\u53f7)\uff0c\u7f16\u8bd1\u5668\u5728\u51b3\u5b9a\u8c03\u7528\u54ea\u4e2a\u91cd\u8f7d\u51fd\u6570\u65f6\uff0c\u53ea\u4f1a\u8003\u8651 max \u6a21\u677f\u51fd\u6570\u91cd\u8f7d\u3002\u5728\u4e0b\u9762\u4e00\u4e2a\u4f8b\u5b50(\u6ca1\u6709\u5c16\u62ec\u53f7)\u4e2d\uff0c\u7f16\u8bd1\u5668\u5c06\u540c\u65f6\u8003\u8651 max \u6a21\u677f\u51fd\u6570\u91cd\u8f7d\u548c max \u975e\u6a21\u677f\u51fd\u6570\u91cd\u8f7d\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include template < typename T > T max ( T x , T y ) { std :: cout << \"called max(int, int) \\n \" ; return ( x > y ) ? x : y ; } int max ( int x , int y ) { std :: cout << \"called max(int, int) \\n \" ; return ( x > y ) ? x : y ; } int main () { std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // selects max(int, int) std :: cout << max <> ( 1 , 2 ) << '\\n' ; // deduces max(int, int) (non-template functions not considered) std :: cout << max ( 1 , 2 ) << '\\n' ; // calls function max(int, int) return 0 ; } \u8bf7\u6ce8\u610f\uff0c\u6700\u4e0b\u9762\u7684\u5199\u6cd5\u770b\u8d77\u6765\u4e0e\u6b63\u5e38\u7684\u51fd\u6570\u8c03\u7528\u662f\u4e00\u6837\u7684\uff01\u8fd9\u901a\u5e38\u662f\u8c03\u7528\u51fd\u6570\u6a21\u677f\u65f6\u4f7f\u7528\u7684\u9996\u9009\u8bed\u6cd5(\u5728\u4ee5\u540e\u7684\u793a\u4f8b\u4e2d\uff0c\u5982\u679c\u53ef\u884c\uff0c\u6211\u4eec\u5c06\u9ed8\u8ba4\u4f7f\u7528\u8fd9\u79cd\u8bed\u6cd5)\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u4f7f\u7528\u51fd\u6570\u6a21\u677f\u65f6\uff0c\u4f7f\u7528\u6b63\u5e38\u7684\u51fd\u6570\u8c03\u7528\u8bed\u6cd5\u3002","title":"\u6a21\u677f\u53c2\u6570\u63a8\u65ad"},{"location":"8-14-Function-template-instantiation/#_3","text":"\u53ef\u4ee5\u521b\u5efa\u540c\u65f6\u5177\u6709\u6a21\u677f\u7c7b\u578b\u548c\u975e\u6a21\u677f\u7c7b\u578b\u53c2\u6570\u7684\u51fd\u6570\u6a21\u677f\u3002\u6a21\u677f\u5f62\u53c2\u53ef\u4ee5\u5339\u914d\u4efb\u4f55\u7c7b\u578b\uff0c\u975e\u6a21\u677f\u5f62\u53c2\u7684\u5de5\u4f5c\u65b9\u5f0f\u4e0e\u666e\u901a\u51fd\u6570\u7684\u5f62\u53c2\u7c7b\u4f3c\u3002 \u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 template < typename T > int someFcn ( T x , double y ) { return 5 ; } int main () { someFcn ( 1 , 3.4 ); // matches someFcn(int, double) someFcn ( 1 , 3.4f ); // matches someFcn(int, double) -- the float is promoted to a double someFcn ( 1.2 , 3.4 ); // matches someFcn(double, double) someFcn ( 1.2f , 3.4 ); // matches someFcn(float, double) someFcn ( 1.2f , 3.4f ); // matches someFcn(float, double) -- the float is promoted to a double return 0 ; } \u8fd9\u4e2a\u51fd\u6570\u6a21\u677f\u6709\u4e00\u4e2a\u6a21\u677f\u5316\u7684\u7b2c\u4e00\u4e2a\u5f62\u53c2\uff0c\u4f46\u662f\u7b2c\u4e8c\u4e2a\u5f62\u53c2\u7684\u7c7b\u578b\u662f\u56fa\u5b9a\u7684 double \u3002\u6ce8\u610f\uff0c\u8fd4\u56de\u7c7b\u578b\u4e5f\u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u7684\u51fd\u6570\u603b\u662f\u8fd4\u56de\u4e00\u4e2a int \u503c\u3002 \u6ce8\u610f\uff0c\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u4e0d\u4fdd\u8bc1\u4e00\u5b9a\u80fd\u591f\u7f16\u8bd1\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include template < typename T > T addOne ( T x ) { return x + 1 ; } int main () { std :: cout << addOne ( 1 ) << '\\n' ; std :: cout << addOne ( 2.3 ) << '\\n' ; return 0 ; } \u7f16\u8bd1\u5668\u4f1a\u7f16\u8bd1\u548c\u6267\u884c\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include template < typename T > T addOne ( T x ); template <> int addOne < int > ( int x ) { return x + 1 ; } template <> double addOne < double > ( double x ) { return x + 1 ; } int main () { std :: cout << addOne ( 1 ) << '\\n' ; // calls addOne(int) std :: cout << addOne ( 2.3 ) << '\\n' ; // calls addOne(double) return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 2 2 3.3 \u4f46\u662f\u5982\u679c\u6211\u4eec\u8fd9\u4e48\u505a\u5462\uff1f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include template < typename T > T addOne ( T x ) { return x + 1 ; } int main () { std :: string hello { \"Hello, world!\" }; std :: cout << addOne ( hello ) << '\\n' ; return 0 ; } \u5f53\u7f16\u8bd1\u5668\u5c1d\u8bd5\u89e3\u6790 addOne(hello) \u65f6\uff0c\u5b83\u65e0\u6cd5\u627e\u5230\u4e00\u4e2a\u975e\u6a21\u677f\u51fd\u6570\u6765\u5339\u914d addOne(std::string) \uff0c\u4f46\u662f\u80fd\u591f\u627e\u5230\u4e00\u4e2a\u51fd\u6570\u6a21\u677f addOne(T) \uff0c\u5e76\u901a\u8fc7\u8be5\u6a21\u677f\u751f\u6210 addOne(std::string) \u51fd\u6570\u3002\u56e0\u6b64\u7f16\u8bd1\u5668\u4f1a\u751f\u6210\u5e76\u7f16\u8bd1\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include template < typename T > T addOne ( T x ); template <> std :: string addOne < std :: string > ( std :: string x ) { return x + 1 ; } int main () { std :: string hello { \"Hello, world!\" }; std :: cout << addOne ( hello ) << '\\n' ; return 0 ; } \u663e\u7136\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\uff0c\u56e0\u4e3a x + 1 \u5e76\u4e0d\u9002\u7528\u4e8e x \u4e3a std::string \u7c7b\u578b\u7684\u60c5\u51b5\u3002\u89e3\u51b3\u529e\u6cd5\u5c31\u662f\u4e0d\u8981\u4f7f\u7528 std::string \u7c7b\u578b\u6765\u8c03\u7528 addOne() \u3002","title":"\u5e26\u6709\u975e\u6a21\u677f\u53c2\u6570\u7684\u51fd\u6570\u6a21\u677f"},{"location":"8-14-Function-template-instantiation/#_4","text":"\u4e3a\u4e86\u5b9e\u4f8b\u5316\u6a21\u677f\uff0c\u7f16\u8bd1\u5668\u9700\u8981\u770b\u5230\u6a21\u677f\u7684\u5b8c\u6574\u5b9a\u4e49\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u6211\u4eec\u60f3\u5728\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\u4f7f\u7528\u51fd\u6570\u6a21\u677f\uff0c\u6bcf\u4e2a\u4ee3\u7801\u6587\u4ef6\u90fd\u9700\u8981\u51fd\u6570\u6a21\u677f\u5b9a\u4e49\u7684\u526f\u672c\u3002\u56e0\u6b64\uff0c \u6a21\u677f\u901a\u5e38\u7f16\u5199\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u5b83\u4eec\u53ef\u4ee5\u88ab #include \u5230\u4efb\u4f55\u60f3\u8981\u4f7f\u7528\u5b83\u4eec\u7684\u4ee3\u7801\u6587\u4ef6\u4e2d\u3002 \u6a21\u677f\u5b9a\u4e49\u5e76\u4e0d\u53d7\u9650\u4e8e \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \uff0c\u800c\u4e14\u7531\u4e8e\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u901a\u5e38\u662f\u9690\u5f0f \u5185\u8054\u51fd\u6570 \uff0c\u6240\u4ee5\u4e5f\u4e0d\u53d7\u9650\u4e8e\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u3002 Max.h 1 2 3 4 5 6 7 8 9 10 #ifndef MAX_H #define MAX_H template < typename T > T max ( T x , T y ) { return ( x > y ) ? x : y ; } #endif Foo.cp 1 2 3 4 5 6 7 #include \"Max.h\" // import template definition for max() #include void foo () { std :: cout << max ( 3 , 2 ); } main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 #include \"Max.h\" // import template definition for max() #include void foo (); // forward declaration for function foo int main () { std :: cout << max ( 3 , 5 ); foo (); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c main.cpp \u548c foo.cpp \u90fd #include \"Max.h\" \uff0c\u6240\u4ee5\u8fd9\u4e24\u4e2a\u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\u90fd\u53ef\u4ee5\u4f7f\u7528 max \u51fd\u6570\u6a21\u677f\u3002","title":"\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528\u51fd\u6570\u6a21\u677f"},{"location":"8-14-Function-template-instantiation/#_5","text":"\u56e0\u4e3a\u6a21\u677f\u7c7b\u578b\u53ef\u4ee5\u7528\u4efb\u4f55\u5b9e\u9645\u7c7b\u578b\u66ff\u6362\uff0c\u6240\u4ee5\u6a21\u677f\u7c7b\u578b\u6709\u65f6\u88ab\u79f0\u4e3a\u6cdb\u578b\u7c7b\u578b\u3002\u7531\u4e8e\u6a21\u677f\u53ef\u4ee5\u4e0d\u53d7\u5177\u4f53\u7c7b\u578b\u7684\u5f71\u54cd\u800c\u7f16\u5199\uff0c\u56e0\u6b64\u4f7f\u7528\u6a21\u677f\u8fdb\u884c\u7f16\u7a0b\u6709\u65f6\u88ab\u79f0\u4e3a\u6cdb\u578b\u7f16\u7a0b\u3002C++\u901a\u5e38\u975e\u5e38\u5173\u6ce8\u7c7b\u578b\u548c\u7c7b\u578b\u68c0\u67e5\uff0c\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u6cdb\u578b\u7f16\u7a0b\u8ba9\u6211\u4eec\u4e13\u6ce8\u4e8e\u7b97\u6cd5\u7684\u903b\u8f91\u548c\u6570\u636e\u7ed3\u6784\u7684\u8bbe\u8ba1\uff0c\u800c\u4e0d\u5fc5\u8fc7\u591a\u5730\u62c5\u5fc3\u7c7b\u578b\u4fe1\u606f\u3002","title":"\u6cdb\u578b\u7f16\u7a0b"},{"location":"8-14-Function-template-instantiation/#_6","text":"\u4e00\u65e6\u4e60\u60ef\u4e86\u7f16\u5199\u51fd\u6570\u6a21\u677f\uff0c\u5c31\u4f1a\u53d1\u73b0\u7f16\u5199\u51fd\u6570\u6a21\u677f\u6240\u82b1\u7684\u65f6\u95f4\u5b9e\u9645\u4e0a\u5e76\u4e0d\u6bd4\u5177\u6709\u5b9e\u9645\u7c7b\u578b\u7684\u51fd\u6570\u957f\u591a\u5c11\u3002\u51fd\u6570\u6a21\u677f\u901a\u8fc7\u6700\u5c0f\u5316\u9700\u8981\u7f16\u5199\u548c\u7ef4\u62a4\u7684\u4ee3\u7801\u6570\u91cf\uff0c\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u4ee3\u7801\u7ef4\u62a4\u548c\u9519\u8bef\u3002 \u51fd\u6570\u6a21\u677f\u4e5f\u786e\u5b9e\u6709\u4e00\u4e9b \u7f3a\u70b9 \uff0c\u5982\u679c\u6211\u4eec\u4e0d\u63d0\u53ca\u5b83\u4eec\uff0c\u90a3\u5c31\u662f\u5931\u804c\u4e86\u3002\u9996\u5148\uff0c\u7f16\u8bd1\u5668\u5c06\u4e3a\u6bcf\u4e2a\u51fd\u6570\u8c03\u7528\u521b\u5efa(\u5e76\u7f16\u8bd1)\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5177\u6709\u4e00\u7ec4\u60df\u4e00\u7684\u53c2\u6570\u7c7b\u578b\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u51fd\u6570\u6a21\u677f\u7f16\u5199\u8d77\u6765\u5f88\u7d27\u51d1\uff0c\u4f46\u5b83\u4eec\u53ef\u80fd\u4f1a\u88ab\u6269\u5c55\u6210\u5927\u91cf\u7684\u4ee3\u7801\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u4ee3\u7801\u81a8\u80c0\u548c\u7f16\u8bd1\u65f6\u95f4\u53d8\u6162\u3002\u51fd\u6570\u6a21\u677f \u66f4\u5927\u7684\u7f3a\u70b9 \u662f\uff0c\u5b83\u4eec\u5f80\u5f80\u4f1a\u4ea7\u751f\u770b\u8d77\u6765\u5f88\u75af\u72c2\u3001\u8fd1\u4e4e\u4e0d\u53ef\u8bfb\u7684\u9519\u8bef\u6d88\u606f\uff0c\u6bd4\u5e38\u89c4\u51fd\u6570\u7684\u9519\u8bef\u6d88\u606f\u66f4\u96be\u89e3\u8bfb\u3002\u8fd9\u4e9b\u9519\u8bef\u6d88\u606f\u53ef\u80fd\u76f8\u5f53\u5413\u4eba\uff0c\u4f46\u4e00\u65e6\u4f60\u80fd\u591f\u7406\u89e3\u4e86\u5b83\u8bd5\u56fe\u4f20\u8fbe\u7684\u4fe1\u606f\uff0c\u5176\u6307\u51fa\u7684\u95ee\u9898\u901a\u5e38\u975e\u5e38\u5bb9\u6613\u89e3\u51b3\u3002 \u6a21\u677f\u4e3a\u7f16\u7a0b\u5de5\u5177\u5305\u5e26\u6765\u4e86\u5f3a\u5927\u529f\u80fd\u548c\u5b89\u5168\u6027\uff0c\u8fd9\u4e9b\u7f3a\u70b9\u53ef\u4ee5\u8bf4\u662f\u7455\u4e0d\u63a9\u745c\u4e86\u3002\u6240\u4ee5\u5728\u4efb\u4f55\u9700\u8981\u7c7b\u578b\u7075\u6d3b\u6027\u7684\u5730\u65b9\u90fd\u53ef\u4ee5\u81ea\u7531\u4f7f\u7528\u6a21\u677f\uff01\u4e00\u4e2a\u597d\u7684\u7ecf\u9a8c\u6cd5\u5219\u662f\uff1a \u9996\u5148\u521b\u5efa\u666e\u901a\u51fd\u6570\uff0c\u5f53\u53d1\u73b0\u9700\u8981\u9488\u5bf9\u4e0d\u540c\u53c2\u6570\u7c7b\u578b \u91cd\u8f7d \u51fd\u6570\u65f6\uff0c\u5219\u5c06\u5b83\u4eec\u8f6c\u6362\u4e3a\u51fd\u6570\u6a21\u677f\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u51fd\u6570\u6a21\u677f\u6765\u7f16\u5199\u6cdb\u578b\u4ee3\u7801\uff0c\u8fd9\u4e9b\u4ee3\u7801\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u5904\u7406\u5404\u79cd\u7c7b\u578b\u3002","title":"\u5c0f\u7ed3"},{"location":"8-15-Function-templates-with-multiple-template-types/","tags":["function template","C++20"],"text":"Key Takeaway \u5728 8.13 - \u51fd\u6570\u6a21\u677f \u5728\u6211\u4eec\u7f16\u5199\u4e86\u8ba1\u7b97\u4e24\u4e2a\u503c\u4e2d\u8f83\u5927\u503c\u7684\u51fd \u51fd\u6570\u6a21\u677f \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include template < typename T > T max ( T x , T y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max ( 1 , 2 ) << '\\n' ; // will instantiate max(int, int) std :: cout << max ( 1.5 , 2.5 ) << '\\n' ; // will instantiate max(double, double) return 0 ; } \u73b0\u5728\uff0c\u8003\u8651\u4e0b\u9762\u7c7b\u4f3c\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include template < typename T > T max ( T x , T y ) // <-- \u6ce8\u610f\u8fd9\u91cc { return ( x > y ) ? x : y ; } int main () { std :: cout << max ( 2 , 3.5 ) << '\\n' ; // \u7f16\u8bd1\u9519\u8bef return 0 ; } \u4f60\u53ef\u80fd\u4f1a\u60ca\u8bb6\u5730\u53d1\u73b0\u8fd9\u4e2a\u7a0b\u5e8f\u65e0\u6cd5\u7f16\u8bd1\u3002\u7f16\u8bd1\u5668\u4f1a\u53d1\u51fa\u4e00\u5806(\u53ef\u80fd\u770b\u8d77\u6765\u5f88\u75af\u72c2)\u9519\u8bef\u6d88\u606f\u3002\u5728Visual Studio\u4e0a\uff0c\u7b14\u8005\u5f97\u5230\u4e86\u4ee5\u4e0b\u5185\u5bb9\uff1a 1 2 3 4 5 6 7 Project3.cpp(11,18): error C2672: 'max': no matching overloaded function found Project3.cpp(11,28): error C2782: 'T max(T,T)': template parameter 'T' is ambiguous Project3.cpp(4): message : see declaration of 'max' Project3.cpp(11,28): message : could be 'double' Project3.cpp(11,28): message : or 'int' Project3.cpp(11,28): error C2784: 'T max(T,T)': could not deduce template argument for 'T' from 'double' Project3.cpp(4): message : see declaration of 'max' \u5f53\u8c03\u7528 max(2, 3.5) \u65f6\uff0c\u6211\u4eec\u5b9e\u9645\u4e0a\u4f20\u5165\u4e86\u4e24\u4e2a\u4e0d\u540c\u7c7b\u578b\u7684\u5b9e\u53c2\uff1a int \u548c double \u3002\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u4f7f\u7528\u5c16\u62ec\u53f7\u6765\u6307\u5b9a\u51fd\u6570\u6a21\u677f\u7684\u5b9e\u9645\u7c7b\u578b\uff0c\u7f16\u8bd1\u5668\u4f1a\u9996\u5148\u67e5\u770b\u662f\u5426\u6709\u975e\u6a21\u677f\u51fd\u6570 max(int, double) \u4f9b\u5176\u8c03\u7528\uff0c\u4f46\u5f88\u9057\u61be\u6ca1\u6709\u627e\u5230\u3002 \u63a5\u4e0b\u6765\uff0c\u7f16\u8bd1\u5668\u4f1a\u67e5\u770b\u662f\u5426\u80fd\u627e\u5230\u4e00\u4e2a\u5339\u914d\u7684\u51fd\u6570\u6a21\u677f(\u4f7f\u7528 \u6a21\u677f\u5b9e\u53c2\u63a8\u65ad \uff0c\u53c2\u89c1 8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316 )\u3002\u4f46\u5f88\u9057\u61be\uff0c\u4ecd\u7136\u65e0\u6cd5\u627e\u5230\uff0c\u539f\u56e0\u4e5f\u975e\u5e38\u7b80\u5355\uff1a T \u53ea\u80fd\u8868\u793a\u4e00\u4e2a\u7c7b\u578b\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u6839\u636e max(T, T) \u6a21\u677f\u751f\u6210\u80fd\u591f\u63a5\u53d7\u4e24\u4e2a\u4e0d\u540c \u5f62\u53c2 \u7c7b\u578b\u7684\u51fd\u6570\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c\u51fd\u6570\u6a21\u677f\u4e2d\u7684\u591a\u4e2a\u53c2\u6570\u4e3a\u76f8\u540c\u7c7b\u578b T \uff0c\u5219\u5b83\u4eec\u5bf9\u5e94\u7684\u5b9e\u9645\u7c7b\u578b\u4e5f\u5e94\u8be5\u662f\u76f8\u540c\u7684\u3002 \u56e0\u4e3a\u65e0\u6cd5\u627e\u5230\u975e\u6a21\u677f\u51fd\u6570\uff0c\u4e5f\u65e0\u6cd5\u627e\u5230\u5408\u9002\u7684\u51fd\u6570\u6a21\u677f\uff0c\u6240\u4ee5\u51fd\u6570\u89e3\u6790\u51fa\u9519\uff0c\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\u3002 \u4f60\u53ef\u80fd\u4f1a\u60f3\uff0c\u4e3a\u4ec0\u4e48\u7f16\u8bd1\u5668\u4e0d\u80fd\u751f\u6210\u4e00\u4e2a max(double, double) \u7c7b\u578b\u7684\u51fd\u6570\uff0c\u7136\u540e\u901a\u8fc7 \u6570\u503c\u8f6c\u6362 \u5c06 int \u8f6c\u6362\u4e3a double \u5462\uff1f\u7b54\u6848\u4e5f\u5f88\u7b80\u5355\uff1a\u7c7b\u578b\u8f6c\u6362\u53ea\u6709\u5728\u89e3\u6790\u51fd\u6570 \u91cd\u8f7d \u65f6\u624d\u4f1a\u53d1\u751f\uff0c\u5728\u6267\u884c\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad\u65f6\u5e76\u4e0d\u4f1a\u8fdb\u884c\u3002 \u4e0d\u63d0\u4f9b\u7c7b\u578b\u8f6c\u6362\u662f\u6709\u610f\u800c\u4e3a\u4e4b\u7684\uff0c\u4e14\u81f3\u5c11\u6709\u4e24\u4e2a\u539f\u56e0\u3002\u9996\u5148\uff0c\u5b83\u6709\u52a9\u4e8e\u4f7f\u4e8b\u60c5\u53d8\u5f97\u7b80\u5355\uff1a\u6211\u4eec\u8981\u4e48\u627e\u5230\u51fd\u6570\u8c03\u7528\u5b9e\u53c2\u548c\u6a21\u677f\u7c7b\u578b\u5f62\u53c2\u4e4b\u95f4\u7684\u7cbe\u786e\u5339\u914d\uff0c\u8981\u4e48\u627e\u4e0d\u5230\u3002\u5176\u6b21\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u9700\u8981\u786e\u4fdd\u4e24\u4e2a\u6216\u591a\u4e2a\u5f62\u53c2\u5177\u6709\u76f8\u540c\u7c7b\u578b\u7684\u60c5\u51b5\u4e0b\u521b\u5efa\u51fd\u6570\u6a21\u677f(\u5982\u4e0a\u4f8b\u6240\u793a)\u3002 \u6211\u4eec\u5f97\u53e6\u60f3\u529e\u6cd5\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u7528(\u81f3\u5c11)\u4e09\u79cd\u65b9\u6cd5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002 \u4f7f\u7528 static_cast \u5c06\u5b9e\u53c2\u8f6c\u6362\u4e3a\u5339\u914d\u7684\u7c7b\u578b \u89e3\u51b3\u65b9\u6848 1\uff1a\u5c06\u5b9e\u53c2\u8f6c\u6362\u4e3a\u5339\u914d\u7684\u7c7b\u578b\u7684\u4efb\u52a1\u4ea4\u7ed9\u8c03\u7528\u8005\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include template < typename T > T max ( T x , T y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max ( static_cast < double > ( 2 ), 3.5 ) << '\\n' ; // convert our int to a double so we can call max(double, double) return 0 ; } \u8fd9\u6837\u4e00\u6765\uff0c\u4e24\u4e2a\u5b9e\u53c2\u90fd\u662f double \u7c7b\u578b\u4e86\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u5c06\u4f1a\u5b9e\u4f8b\u5316 max(double, double) \u5e76\u6267\u884c\u51fd\u6570\u8c03\u7528\u3002 \u4e0d\u8fc7\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u592a\u81ea\u7136\uff0c\u53ef\u8bfb\u6027\u4e5f\u4e0d\u4f73\u3002 \u63d0\u4f9b\u5b9e\u9645\u7c7b\u578b \u5982\u679c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u975e\u6a21\u677f\u51fd\u6570 max(double, double) \uff0c\u90a3\u4e48\u662f\u53ef\u4ee5\u8c03\u7528 max(int, double) \u5e76\u901a\u8fc7 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u5c06 int \u8f6c\u6362\u4e3a double \u5e76\u8c03\u7528\u51fd\u6570\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include double max ( double x , double y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max ( 2 , 3.5 ) << '\\n' ; // the int argument will be converted to a double return 0 ; } \u7136\u800c\uff0c \u5f53\u7f16\u8bd1\u5668\u8fdb\u884c\u6a21\u677f\u5b9e\u53c2\u63a8\u5bfc\u65f6\uff0c\u5b83\u4e0d\u4f1a\u8fdb\u884c\u4efb\u4f55\u7c7b\u578b\u8f6c\u6362 \u3002\u5e78\u8fd0\u7684\u662f\uff0c\u5982\u679c\u6307\u5b9a\u8981\u4f7f\u7528\u7684\u5b9e\u9645\u7c7b\u578b\uff0c\u7f16\u8bd1\u5668\u5c31\u4e0d\u5fc5\u4f7f\u7528\u6a21\u677f\u5b9e\u53c2\u63a8\u5bfc\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include template < typename T > T max ( T x , T y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max < double > ( 2 , 3.5 ) << '\\n' ; // we've provided actual type double, so the compiler won't use template argument deduction return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8c03\u7528\u4e86 max(2, 3.5) \u51fd\u6570\u3002\u56e0\u4e3a\u663e\u793a\u63d0\u4f9b\u4e86\u6a21\u677f\u7c7b\u578b double \uff0c\u7f16\u8bd1\u5668\u65e0\u9700\u8fdb\u884c\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad\u3002\u5b83\u4f1a\u76f4\u63a5\u5b9e\u4f8b\u5316 max(double, double) \uff0c\u7136\u540e\u5bf9\u4e0d\u5339\u914d\u7684\u53c2\u6570\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002 int \u5c31\u4f1a\u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a double \u3002 \u5c3d\u7ba1\u8fd9\u4e48\u505a\u7684\u53ef\u8bfb\u6027\u7a0d\u597d\u4e8e static_cast \uff0c\u4f46\u662f\u5982\u679c\u80fd\u591f\u5728\u8c03\u7528\u51fd\u6570 max \u65f6\u4e0d\u8003\u8651\u7c7b\u578b\u5c31\u597d\u4e86\u3002 \u5177\u6709\u591a\u4e2a\u6a21\u677f\u7c7b\u578b\u5f62\u53c2\u7684\u51fd\u6570\u6a21\u677f \u8fd9\u4e2a\u95ee\u9898\u7684\u6839\u56e0\u662f\u56e0\u4e3a\u51fd\u6570\u6a21\u677f\u4e2d\u53ea\u6709\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b ( T ) \uff0c\u6240\u4ee5\u4e24\u4e2a\u53c2\u6570\u90fd\u5fc5\u987b\u662f\u8be5\u7c7b\u578b\u7684\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u6700\u4f73\u7684\u65b9\u6cd5\u662f\u8ba9\u51fd\u6570\u6a21\u677f\u652f\u6301\u591a\u4e2a\u4e0d\u540c\u7684\u7c7b\u578b\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u4f7f\u7528\u4e24\u4e2a\u7c7b\u578b\u5f62\u53c2( T \u548c U )\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include template < typename T , typename U > // We're using two template type parameters named T and U T max ( T x , U y ) // x can resolve to type T, and y can resolve to type U { return ( x > y ) ? x : y ; // uh oh, we have a narrowing conversion problem here } int main () { std :: cout << max ( 2 , 3.5 ) << '\\n' ; return 0 ; } \u56e0\u4e3a\uff0c\u6211\u4eec\u4f7f\u7528\u6a21\u677f\u7c7b\u578b T \u5b9a\u4e49\u4e86 x \uff0c\u4f7f\u7528 U \u5b9a\u4e49\u4e86 y \uff0c\u6240\u4ee5\u73b0\u5728\u4e24\u4e2a\u53c2\u6570\u53ef\u4ee5\u662f\u4e0d\u540c\u7684\u7c7b\u578b\u4e86\u3002 \u5728\u8c03\u7528 max(2, 3.5) \u65f6 T \u88ab\u89e3\u6790\u4e3a int \u800c U \u88ab\u89e3\u6790\u4e3a double \u3002\u7f16\u8bd1\u5668\u5b9e\u4f8b\u5316 max(int, double) \u51fd\u6570\u3002 \u4f46\u662f\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u4ecd\u7136\u6709\u95ee\u9898\uff1a\u5728\u4f7f\u7528\u7b97\u6570\u89c4\u5219\u65f6( 8.4 - \u7b97\u672f\u8f6c\u6362 )\uff0c double \u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e int \u56e0\u6b64\u6761\u4ef6\u8fd0\u7b97\u7b26\u7684\u8fd4\u56de\u503c\u662f double \uff0c\u800c\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a T \u2014\u2014 \u5982\u679c\u6b64\u65f6 T \u88ab\u89e3\u6790\u4e3a\u4e86 int \uff0c\u5219 double \u7c7b\u578b\u7684\u8fd4\u56de\u503c\u5728\u8fd4\u56de\u65f6\u4f1a\u88ab \u7f29\u7a84\u8f6c\u6362 \u4e3a int \u4ece\u800c\u4ea7\u751f\u4e00\u4e2a\u544a\u8b66\uff08\u901a\u5e38\u4e5f\u4f1a\u5bfc\u81f4\u6570\u636e\u4e22\u5931\uff09\u3002 \u5c06\u8fd4\u56de\u503c\u5b9a\u4e49\u4e3a U \u5e76\u4e0d\u80fd\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u56e0\u4e3a T \u548c U \u5728\u4f20\u9012\u65f6\u672c\u8eab\u5c31\u53ef\u4ee5\u4e92\u6362\u3002 \u90a3\u5e94\u8be5\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5462\uff1f\u4e00\u4e2a\u597d\u529e\u6cd5\u662f\u4f7f\u7528 auto \u4f5c\u4e3a\u8fd4\u56de\u7c7b\u578b\u2014\u2014\u8ba9\u7f16\u8bd1\u5668\u4ece\u8fd4\u56de\u8bed\u53e5\u4e2d\u63a8\u65ad\u8fd4\u56de\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include template < typename T , typename U > auto max ( T x , U y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max ( 2 , 3.5 ) << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7248\u672c\u7684 max \u51fd\u6570\u73b0\u5728\u53ef\u4ee5\u5f88\u597d\u5730\u914d\u5408\u5404\u79cd\u53c2\u6570\u7c7b\u578b\u5de5\u4f5c\u4e86\u3002 \u7f29\u5199\u51fd\u6570\u6a21\u677f \uff08C++20\uff09 C++20 \u4e3a auto \u5173\u952e\u5b57\u5f15\u5165\u4e86\u65b0\u7684\u529f\u80fd\uff1a\u5f53\u4f7f\u7528 auto \u5173\u952e\u5b57\u4fee\u9970\u666e\u901a\u51fd\u6570\u7684 \u5f62\u53c2 \u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u5c06\u8be5\u51fd\u6570\u8f6c\u6362\u4e3a\u51fd\u6570\u6a21\u677f\uff0c\u4e14\u6bcf\u4e2a\u88ab auto \u4fee\u9970\u7684\u5f62\u53c2\u90fd\u662f\u72ec\u7acb\u7684\u6a21\u677f\u7c7b\u578b\u5f62\u53c2\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\u521b\u5efa\u7684\u51fd\u6570\u6a21\u677f\u79f0\u4e3a \u7f29\u5199\u51fd\u6570\u6a21\u677f \u3002 \u4f8b\u5982\uff1a 1 2 3 4 auto max ( auto x , auto y ) { return ( x > y ) ? x : y ; } \u4e0a\u9762\u4ee3\u7801\u662fC++20\u4e2d\u5bf9\u4e0b\u9762\u6a21\u677f\u5b9a\u4e49\u7684\u7f29\u5199\u5f62\u5f0f\uff1a 1 2 3 4 5 template < typename T , typename U > auto max ( T x , U y ) { return ( x > y ) ? x : y ; } \u8fd9\u548c\u6211\u4eec\u4e0a\u9762\u5199\u7684 max \u51fd\u6570\u6a21\u677f\u662f\u4e00\u6837\u7684\u3002 \u5728\u5e0c\u671b\u6bcf\u4e2a\u6a21\u677f\u7c7b\u578b\u5f62\u53c2\u90fd\u662f\u72ec\u7acb\u7c7b\u578b\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd9\u79cd\u5f62\u5f0f\u662f\u9996\u9009\u7684\uff0c\u56e0\u4e3a\u5220\u9664\u6a21\u677f\u5f62\u53c2\u58f0\u660e\u884c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u53ef\u8bfb\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u6bcf\u4e2a\u81ea\u52a8\u53c2\u6570\u90fd\u5e94\u8be5\u662f\u72ec\u7acb\u7684\u6a21\u677f\u7c7b\u578b(\u5e76\u4e14\u4f60\u7684\u8bed\u8a00\u6807\u51c6\u8bbe\u7f6e\u4e3aC++20\u6216\u66f4\u65b0\u7248\u672c)\uff0c\u90a3\u4e48\u53ef\u4ee5\u968f\u610f\u4f7f\u7528\u7f29\u5199\u51fd\u6570\u6a21\u677f\u3002","title":"8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f"},{"location":"8-15-Function-templates-with-multiple-template-types/#static_cast","text":"\u89e3\u51b3\u65b9\u6848 1\uff1a\u5c06\u5b9e\u53c2\u8f6c\u6362\u4e3a\u5339\u914d\u7684\u7c7b\u578b\u7684\u4efb\u52a1\u4ea4\u7ed9\u8c03\u7528\u8005\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include template < typename T > T max ( T x , T y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max ( static_cast < double > ( 2 ), 3.5 ) << '\\n' ; // convert our int to a double so we can call max(double, double) return 0 ; } \u8fd9\u6837\u4e00\u6765\uff0c\u4e24\u4e2a\u5b9e\u53c2\u90fd\u662f double \u7c7b\u578b\u4e86\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u5c06\u4f1a\u5b9e\u4f8b\u5316 max(double, double) \u5e76\u6267\u884c\u51fd\u6570\u8c03\u7528\u3002 \u4e0d\u8fc7\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u592a\u81ea\u7136\uff0c\u53ef\u8bfb\u6027\u4e5f\u4e0d\u4f73\u3002","title":"\u4f7f\u7528 static_cast \u5c06\u5b9e\u53c2\u8f6c\u6362\u4e3a\u5339\u914d\u7684\u7c7b\u578b"},{"location":"8-15-Function-templates-with-multiple-template-types/#_1","text":"\u5982\u679c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u975e\u6a21\u677f\u51fd\u6570 max(double, double) \uff0c\u90a3\u4e48\u662f\u53ef\u4ee5\u8c03\u7528 max(int, double) \u5e76\u901a\u8fc7 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u5c06 int \u8f6c\u6362\u4e3a double \u5e76\u8c03\u7528\u51fd\u6570\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include double max ( double x , double y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max ( 2 , 3.5 ) << '\\n' ; // the int argument will be converted to a double return 0 ; } \u7136\u800c\uff0c \u5f53\u7f16\u8bd1\u5668\u8fdb\u884c\u6a21\u677f\u5b9e\u53c2\u63a8\u5bfc\u65f6\uff0c\u5b83\u4e0d\u4f1a\u8fdb\u884c\u4efb\u4f55\u7c7b\u578b\u8f6c\u6362 \u3002\u5e78\u8fd0\u7684\u662f\uff0c\u5982\u679c\u6307\u5b9a\u8981\u4f7f\u7528\u7684\u5b9e\u9645\u7c7b\u578b\uff0c\u7f16\u8bd1\u5668\u5c31\u4e0d\u5fc5\u4f7f\u7528\u6a21\u677f\u5b9e\u53c2\u63a8\u5bfc\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include template < typename T > T max ( T x , T y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max < double > ( 2 , 3.5 ) << '\\n' ; // we've provided actual type double, so the compiler won't use template argument deduction return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8c03\u7528\u4e86 max(2, 3.5) \u51fd\u6570\u3002\u56e0\u4e3a\u663e\u793a\u63d0\u4f9b\u4e86\u6a21\u677f\u7c7b\u578b double \uff0c\u7f16\u8bd1\u5668\u65e0\u9700\u8fdb\u884c\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad\u3002\u5b83\u4f1a\u76f4\u63a5\u5b9e\u4f8b\u5316 max(double, double) \uff0c\u7136\u540e\u5bf9\u4e0d\u5339\u914d\u7684\u53c2\u6570\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002 int \u5c31\u4f1a\u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a double \u3002 \u5c3d\u7ba1\u8fd9\u4e48\u505a\u7684\u53ef\u8bfb\u6027\u7a0d\u597d\u4e8e static_cast \uff0c\u4f46\u662f\u5982\u679c\u80fd\u591f\u5728\u8c03\u7528\u51fd\u6570 max \u65f6\u4e0d\u8003\u8651\u7c7b\u578b\u5c31\u597d\u4e86\u3002","title":"\u63d0\u4f9b\u5b9e\u9645\u7c7b\u578b"},{"location":"8-15-Function-templates-with-multiple-template-types/#_2","text":"\u8fd9\u4e2a\u95ee\u9898\u7684\u6839\u56e0\u662f\u56e0\u4e3a\u51fd\u6570\u6a21\u677f\u4e2d\u53ea\u6709\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b ( T ) \uff0c\u6240\u4ee5\u4e24\u4e2a\u53c2\u6570\u90fd\u5fc5\u987b\u662f\u8be5\u7c7b\u578b\u7684\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u6700\u4f73\u7684\u65b9\u6cd5\u662f\u8ba9\u51fd\u6570\u6a21\u677f\u652f\u6301\u591a\u4e2a\u4e0d\u540c\u7684\u7c7b\u578b\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u4f7f\u7528\u4e24\u4e2a\u7c7b\u578b\u5f62\u53c2( T \u548c U )\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include template < typename T , typename U > // We're using two template type parameters named T and U T max ( T x , U y ) // x can resolve to type T, and y can resolve to type U { return ( x > y ) ? x : y ; // uh oh, we have a narrowing conversion problem here } int main () { std :: cout << max ( 2 , 3.5 ) << '\\n' ; return 0 ; } \u56e0\u4e3a\uff0c\u6211\u4eec\u4f7f\u7528\u6a21\u677f\u7c7b\u578b T \u5b9a\u4e49\u4e86 x \uff0c\u4f7f\u7528 U \u5b9a\u4e49\u4e86 y \uff0c\u6240\u4ee5\u73b0\u5728\u4e24\u4e2a\u53c2\u6570\u53ef\u4ee5\u662f\u4e0d\u540c\u7684\u7c7b\u578b\u4e86\u3002 \u5728\u8c03\u7528 max(2, 3.5) \u65f6 T \u88ab\u89e3\u6790\u4e3a int \u800c U \u88ab\u89e3\u6790\u4e3a double \u3002\u7f16\u8bd1\u5668\u5b9e\u4f8b\u5316 max(int, double) \u51fd\u6570\u3002 \u4f46\u662f\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u4ecd\u7136\u6709\u95ee\u9898\uff1a\u5728\u4f7f\u7528\u7b97\u6570\u89c4\u5219\u65f6( 8.4 - \u7b97\u672f\u8f6c\u6362 )\uff0c double \u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e int \u56e0\u6b64\u6761\u4ef6\u8fd0\u7b97\u7b26\u7684\u8fd4\u56de\u503c\u662f double \uff0c\u800c\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a T \u2014\u2014 \u5982\u679c\u6b64\u65f6 T \u88ab\u89e3\u6790\u4e3a\u4e86 int \uff0c\u5219 double \u7c7b\u578b\u7684\u8fd4\u56de\u503c\u5728\u8fd4\u56de\u65f6\u4f1a\u88ab \u7f29\u7a84\u8f6c\u6362 \u4e3a int \u4ece\u800c\u4ea7\u751f\u4e00\u4e2a\u544a\u8b66\uff08\u901a\u5e38\u4e5f\u4f1a\u5bfc\u81f4\u6570\u636e\u4e22\u5931\uff09\u3002 \u5c06\u8fd4\u56de\u503c\u5b9a\u4e49\u4e3a U \u5e76\u4e0d\u80fd\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u56e0\u4e3a T \u548c U \u5728\u4f20\u9012\u65f6\u672c\u8eab\u5c31\u53ef\u4ee5\u4e92\u6362\u3002 \u90a3\u5e94\u8be5\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5462\uff1f\u4e00\u4e2a\u597d\u529e\u6cd5\u662f\u4f7f\u7528 auto \u4f5c\u4e3a\u8fd4\u56de\u7c7b\u578b\u2014\u2014\u8ba9\u7f16\u8bd1\u5668\u4ece\u8fd4\u56de\u8bed\u53e5\u4e2d\u63a8\u65ad\u8fd4\u56de\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include template < typename T , typename U > auto max ( T x , U y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max ( 2 , 3.5 ) << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7248\u672c\u7684 max \u51fd\u6570\u73b0\u5728\u53ef\u4ee5\u5f88\u597d\u5730\u914d\u5408\u5404\u79cd\u53c2\u6570\u7c7b\u578b\u5de5\u4f5c\u4e86\u3002","title":"\u5177\u6709\u591a\u4e2a\u6a21\u677f\u7c7b\u578b\u5f62\u53c2\u7684\u51fd\u6570\u6a21\u677f"},{"location":"8-15-Function-templates-with-multiple-template-types/#c20","text":"C++20 \u4e3a auto \u5173\u952e\u5b57\u5f15\u5165\u4e86\u65b0\u7684\u529f\u80fd\uff1a\u5f53\u4f7f\u7528 auto \u5173\u952e\u5b57\u4fee\u9970\u666e\u901a\u51fd\u6570\u7684 \u5f62\u53c2 \u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u5c06\u8be5\u51fd\u6570\u8f6c\u6362\u4e3a\u51fd\u6570\u6a21\u677f\uff0c\u4e14\u6bcf\u4e2a\u88ab auto \u4fee\u9970\u7684\u5f62\u53c2\u90fd\u662f\u72ec\u7acb\u7684\u6a21\u677f\u7c7b\u578b\u5f62\u53c2\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\u521b\u5efa\u7684\u51fd\u6570\u6a21\u677f\u79f0\u4e3a \u7f29\u5199\u51fd\u6570\u6a21\u677f \u3002 \u4f8b\u5982\uff1a 1 2 3 4 auto max ( auto x , auto y ) { return ( x > y ) ? x : y ; } \u4e0a\u9762\u4ee3\u7801\u662fC++20\u4e2d\u5bf9\u4e0b\u9762\u6a21\u677f\u5b9a\u4e49\u7684\u7f29\u5199\u5f62\u5f0f\uff1a 1 2 3 4 5 template < typename T , typename U > auto max ( T x , U y ) { return ( x > y ) ? x : y ; } \u8fd9\u548c\u6211\u4eec\u4e0a\u9762\u5199\u7684 max \u51fd\u6570\u6a21\u677f\u662f\u4e00\u6837\u7684\u3002 \u5728\u5e0c\u671b\u6bcf\u4e2a\u6a21\u677f\u7c7b\u578b\u5f62\u53c2\u90fd\u662f\u72ec\u7acb\u7c7b\u578b\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd9\u79cd\u5f62\u5f0f\u662f\u9996\u9009\u7684\uff0c\u56e0\u4e3a\u5220\u9664\u6a21\u677f\u5f62\u53c2\u58f0\u660e\u884c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u53ef\u8bfb\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u6bcf\u4e2a\u81ea\u52a8\u53c2\u6570\u90fd\u5e94\u8be5\u662f\u72ec\u7acb\u7684\u6a21\u677f\u7c7b\u578b(\u5e76\u4e14\u4f60\u7684\u8bed\u8a00\u6807\u51c6\u8bbe\u7f6e\u4e3aC++20\u6216\u66f4\u65b0\u7248\u672c)\uff0c\u90a3\u4e48\u53ef\u4ee5\u968f\u610f\u4f7f\u7528\u7f29\u5199\u51fd\u6570\u6a21\u677f\u3002","title":"\u7f29\u5199\u51fd\u6570\u6a21\u677f\uff08C++20\uff09"},{"location":"8-2-Floating-point-and-integral-promotion/","tags":["numeric promotion","conversions"],"text":"Key Takeaway \u6570\u503c\u63d0\u5347\u5c5e\u4e8e\u4e00\u7c7b\u7c7b\u578b\u8f6c\u6362\uff0c\u5b83\u5c06\u66f4\u7a84\u7684\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982 char \uff09\u8f6c\u6362\u4e3a\u66f4\u5bbd\u7684\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982 int \u6216 double \uff09 \u4f7f\u5176\u80fd\u591f\u66f4\u9ad8\u6548\u5730\u88ab\u5904\u7406 \uff0c\u540c\u65f6\u4e5f\u66f4\u4e0d\u5bb9\u6613\u6ea2\u51fa\u3002\uff08\u505a\u4e0d\u5230\u8fd9\u4e00\u70b9\u7684\u4e0d\u53eb\u6570\u503c \u63d0\u5347 \uff09 \u6570\u503c\u63d0\u5347\u662f\u5b89\u5168\u7684\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u7531\u5730\u4f7f\u7528\u6570\u5b57\u63d0\u5347\uff0c\u5e76\u4e14\u5728\u8fd9\u6837\u505a\u65f6\u4e0d\u4f1a\u53d1\u51fa\u8b66\u544a\u3002 \u5229\u7528\u6570\u503c\u63d0\u5347\uff0c\u53ef\u4ee5\u7f16\u5199\u51fd\u6570\u4f7f\u7528\u67d0\u79cd\u53ef\u4ee5\u88ab\u63d0\u5347\u7684\u7c7b\u578b\u4f5c\u4e3a\u5f62\u53c2\uff0c\u7136\u540e\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u7c7b\u578b\u7684\u5f62\u53c2\u8fdb\u884c\u8c03\u7528\u3002\u5728\u8c03\u7528\u65f6\u8fdb\u81ea\u52a8\u8fdb\u884c\u7c7b\u578b\u63d0\u5347 float -> double \u3002 unsigned char \u6216\u8005 signed char -> int \uff1b unsigned char \uff0c char8_t \u53ca unsigned short -> int \uff0c\u53ea\u8981 int \u7684\u8303\u56f4\u8db3\u591f\u8868\u793a\u8be5\u7c7b\u578b\u7684\u8303\u56f4\uff0c\u5426\u5219\u4f1a\u8f6c\u6362\u4e3a unsigned int \uff1b bool -> int \uff0c false \u8f6c\u6362\u4e3a 0 \uff0c true \u8f6c\u6362\u4e3a 1 \u3002 \u6ce8\u610f\uff1a\u5728\u67d0\u4e9b\u7cfb\u7edf\u4e0a\uff0c\u4e00\u4e9b\u6574\u578b\u53ef\u80fd\u4f1a\u88ab\u8f6c\u6362\u4e3a unsigned int \u800c\u4e0d\u662f int \u3002\u5176\u6b21\uff0c\u4e00\u4e9b\u8f83\u7a84\u7684\u65e0\u7b26\u53f7\u7c7b\u578b(\u5982 unsigned char )\u5c06\u88ab\u8f6c\u6362\u4e3a\u8f83\u5927\u7684\u6709\u7b26\u53f7\u7c7b\u578b(\u5982 int )\u3002\u56e0\u6b64\uff0c\u867d\u7136\u6574\u578b\u63d0\u5347\u662f\u4fdd\u503c\u7684\uff0c\u4f46\u4e0d\u4e00\u5b9a\u80fd\u4fdd\u53f7\u3002 \u5728 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0cC++ \u53ea\u80fd\u4fdd\u8bc1\u6bcf\u79cd\u57fa\u7840\u7c7b\u578b\u6700\u5c0f\u7684\u5c3a\u5bf8\u3002\u800c\u8fd9\u4e9b\u7c7b\u578b\u7684\u5177\u4f53\u5c3a\u5bf8\u8981\u770b\u5177\u4f53\u7684\u7f16\u8bd1\u5668\u548c\u4f53\u7cfb\u7ed3\u6784\u3002 \u6b63\u662f\u56e0\u4e3a\u5141\u8bb8\u8fd9\u79cd\u53d8\u5316\uff0c\u6240\u4ee5 int \u548c double \u6570\u636e\u7c7b\u578b\u624d\u80fd\u5728\u7279\u5b9a\u4f53\u7cfb\u7ed3\u6784\u7684\u8ba1\u7b97\u673a\u4e0a\u88ab\u8bbe\u7f6e\u4e3a\u80fd\u591f\u63d0\u4f9b\u6700\u4f18\u6027\u80fd\u7684\u5927\u5c0f\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a 32 \u4f4d\u8ba1\u7b97\u673a\u6765\u8bf4\uff0c\u5b83\u6bcf\u6b21\u80fd\u591f\u5904\u7406 32 \u4f4d\u7684\u6570\u636e\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c int \u88ab\u8bbe\u7f6e\u4e3a 32 \u4f4d\u5bbd\u5ea6\uff0c\u56e0\u4e3a\u8fd9\u662fCPU\u5904\u7406\u6570\u636e\u65f6\u6700\u201d\u81ea\u7136\u201c\uff08\u4e5f\u5c31\u53ef\u80fd\u662f\u6700\u9ad8\u6548\u7684\uff09\u7684\u5bbd\u5ea6\u3002 \u63d0\u9192 \u6570\u636e\u7c7b\u578b\u5360\u7528\u7684\u4f4d\u6570\u79f0\u4e3a\u5bbd\u5ea6\u3002\u5360\u7528\u7684\u4f4d\u6570\u8d8a\u591a\uff0c\u5bbd\u5ea6\u4e5f\u5c31\u8d8a\u5bbd\uff0c\u800c\u8d8a\u7a84\u7684\u6570\u636e\u5219\u5360\u7528\u7684\u4f4d\u6570\u4e5f\u8d8a\u5c11\u3002 \u4f46\u662f\uff0c\u5982\u679c 32 \u4f4d CPU \u9700\u8981\u4fee\u6539\u4e00\u4e2a 8 \u4f4d\u7684\u503c\uff08\u4f8b\u5982 char \u7c7b\u578b\uff09\u65f6\u4f1a\u600e\u6837\uff1f\u6709\u4e9b 32 \u4f4d\u7684\u5904\u7406\u5668\uff08\u4f8b\u5982 x86 \u7cfb\u5217\uff09\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539 8 \u4f4d\u6216\u8005 16 \u4f4d\u7684\u503c\u3002\u4f46\u662f\u8fd9\u79cd\u60c5\u51b5\u6bd4\u4fee\u6539 32 \u4f4d\u7684\u6570\u636e\u8981\u6162\u5f97\u591a\uff01\u5176\u4ed6\u7684 32 \u4f4d CPU\uff08\u4f8b\u5982 PowerPC\uff09\uff0c\u53ea\u80fd\u64cd\u4f5c 32 \u4f4d\u7684\u503c\uff0c\u800c\u64cd\u4f5c\u66f4\u7a84\u7684\u6570\u636e\u9700\u8981\u4e00\u4e9b\u989d\u5916\u7684\u6280\u5de7\u3002 \u6570\u503c\u63d0\u5347 \u56e0\u4e3a C++ \u88ab\u8bbe\u8ba1\u4e3a\u53ef\u4ee5\u5728\u5e7f\u6cdb\u7684\u5e73\u53f0\u4e0a\u8fdb\u884c\u79fb\u690d\uff0c\u56e0\u6b64\u8bed\u8a00\u7684\u8bbe\u8ba1\u8005\u4eec\u5e76\u4e0d\u60f3\u60f3\u5047\u5b9a\u7ed9\u5b9a\u7684 CPU \u80fd\u591f\u9ad8\u6548\u5730\u64cd\u4f5c\u6bd4\u81ea\u7136\u5bbd\u5ea6\u66f4\u7a84\u7684\u6570\u636e\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u6311\u6218\uff0cC++ \u5b9a\u4e49\u4e86\u4e00\u79cd\u7c7b\u578b\u7684\u6570\u636e\u8f6c\u6362\uff0c\u5176\u975e\u6b63\u5f0f\u540d\u79f0\u53eb\u505a \u6570\u503c\u63d0\u5347(numeric promotions) \u3002\u6570\u503c\u63d0\u5347\u5c5e\u4e8e\u4e00\u7c7b\u7c7b\u578b\u8f6c\u6362\uff0c\u5b83\u5c06\u66f4\u7a84\u7684\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982 char \uff09\u8f6c\u6362\u4e3a\u66f4\u5bbd\u7684\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982 int \u6216 double \uff09\u4f7f\u5176\u80fd\u591f\u66f4\u9ad8\u6548\u5730\u88ab\u5904\u7406\uff0c\u540c\u65f6\u4e5f\u66f4\u4e0d\u5bb9\u6613\u6ea2\u51fa\u3002 \u6240\u6709\u6570\u503c\u63d0\u5347\u90fd\u662f \u503c\u4fdd\u7559(value-preserving) \u7684\uff0c\u8fd9\u610f\u5473\u7740\u539f\u59cb\u7c7b\u578b\u4e2d\u7684\u6240\u6709\u503c\u90fd\u53ef\u4ee5\u5728\u4e0d\u635f\u5931\u6570\u636e\u6216\u7cbe\u5ea6\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u8868\u793a\u3002\u56e0\u4e3a\u6570\u503c\u63d0\u5347\u662f\u5b89\u5168\u7684\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u7531\u5730\u4f7f\u7528\u6570\u5b57\u63d0\u5347\uff0c\u5e76\u4e14\u5728\u8fd9\u6837\u505a\u65f6\u4e0d\u4f1a\u53d1\u51fa\u8b66\u544a\u3002 \u6570\u503c\u63d0\u5347\u80fd\u591f\u907f\u514d\u5197\u4f59\u4ee3\u7801 \u6570\u503c\u63d0\u5347\u8fd8\u53ef\u4ee5\u89e3\u51b3\u5176\u4ed6\u95ee\u9898\u3002\u8003\u8651\u8fd9\u6837\u4e00\u4e2a\u573a\u666f\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u6253\u5370 int \u7c7b\u578b\u7684\u503c\uff1a 1 2 3 4 5 6 #include void printInt ( int x ) { std :: cout << x ; } \u7a0b\u5e8f\u975e\u5e38\u7b80\u5355\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u8fd8\u5e0c\u671b\u6253\u5370 short \u6216\u8005 char \u7c7b\u578b\u7684\u503c\u5462\uff1f\u5982\u679c\u6ca1\u6709\u7c7b\u578b\u8f6c\u6362\u7684\u652f\u6301\uff0c\u6211\u4eec\u5c31\u5fc5\u987b\u7f16\u5199\u53e6\u5916\u4e24\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\u5206\u522b\u7528\u4e8e\u6253\u5370 short \u6216\u8005 char \u7c7b\u578b\u7684\u503c\u3002\u66f4\u4e0d\u7528\u8bf4\u8fd8\u6709\u5176\u4ed6\u7c7b\u578b\u4e86\uff08 unsigned char , signed char , unsigned short , wchar_t , char8_t , char16_t \u548c char32_t \uff09\u3002\u5f88\u663e\u7136\uff0c\u8fd9\u6837\u7684\u4ee3\u7801\u662f\u4e0d\u53ef\u7ef4\u62a4\u7684\u3002 \u6570\u503c\u63d0\u5347\u5728\u8fd9\u91cc\u8d77\u5230\u4e86\u5173\u952e\u7684\u4f5c\u7528\uff1a\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u5177\u6709 int \u548c/\u6216 double \u5f62\u53c2\u7684\u51fd\u6570(\u6bd4\u5982\u4e0a\u9762\u7684 printInt() \u51fd\u6570)\u3002\u7136\u540e\u4f7f\u7528\u7c7b\u578b\u80fd\u591f\u5728\u8c03\u7528\u51fd\u6570\u662f\u901a\u8fc7\u6570\u503c\u63d0\u5347\u7c7b\u578b\u8f6c\u6362\u8fdb\u884c\u5339\u914d\u7684\u53c2\u6570\u5373\u53ef\u3002 \u6570\u503c\u63d0\u5347\u5206\u7c7b \u6570\u503c\u63d0\u5347\u8fd8\u80fd\u591f\u8fdb\u4e00\u6b65\u5206\u4e3a\u4e24\u79cd\u5b50\u7c7b\u578b\uff1a\u6574\u578b\u63d0\u5347\u548c\u6d6e\u70b9\u6570\u63d0\u5347\u3002 \u6d6e\u70b9\u6570\u63d0\u5347 \u5148\u4ece\u7b80\u5355\u7684\u5f00\u59cb\u3002 \u57fa\u4e8e\u6d6e\u70b9\u6570\u63d0\u5347\u7684\u89c4\u5219\uff0c float \u7c7b\u578b\u7684\u503c\u53ef\u4ee5\u88ab\u8f6c\u6362 double \u3002 \u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u63a5\u53d7 double \u53c2\u6570\u7684\u51fd\u6570\uff0c\u7136\u540e\u7528 double \u6216 float \u503c\u8c03\u7528\u5b83\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include void printDouble ( double d ) { std :: cout << d ; } int main () { printDouble ( 5.0 ); // no conversion necessary printDouble ( 4.0f ); // numeric promotion of float to double return 0 ; } \u5728\u7b2c\u4e8c\u6b21\u8c03\u7528 printDouble() \u65f6 float \u5b57\u9762\u91cf 4.0f \u88ab\u63d0\u5347\u4e3a double \uff0c\u56e0\u6b64\u5b9e\u53c2\u7684\u7c7b\u578b\u4e0e\u51fd\u6570\u5f62\u53c2\u7684\u7c7b\u578b\u5339\u914d\u3002 \u6574\u578b\u63d0\u5347 \u6574\u578b\u6570\u503c\u63d0\u5347\u5c31\u66f4\u52a0\u590d\u6742\u4e86\u3002 \u57fa\u4e8e\u6574\u578b\u6570\u503c\u63d0\u5347\u7684\u89c4\u5219\uff0c\u53ef\u4ee5\u8fdb\u884c\u5982\u4e0b\u7684\u7c7b\u578b\u8f6c\u6362\uff1a - \u65e0\u7b26\u53f7 char \u6216\u8005\u6709\u7b26\u53f7 char \u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a int \uff1b - \u65e0\u7b26\u53f7 char \uff0c char8_t \u4ee5\u53ca\u65e0\u7b26\u53f7 short \u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a int \uff0c\u53ea\u8981 int \u7684\u8303\u56f4\u8db3\u591f\u8868\u793a\u8be5\u7c7b\u578b\u7684\u8303\u56f4\uff0c\u5426\u5219\u4f1a\u8f6c\u6362\u4e3a\u65e0\u7b26\u53f7 int \uff1b - \u5982\u679c char \u9ed8\u8ba4\u6709\u7b26\u53f7\u7684\uff0c \u5219\u4f1a\u9075\u5faa\u4e0a\u8ff0\u6709\u7b26\u53f7 char \u7684\u8f6c\u6362\u89c4\u5219\u3002\u5982\u679c\u9ed8\u8ba4\u662f\u65e0\u7b26\u53f7\u7684\uff0c\u5219\u9075\u5faa\u4e0a\u8ff0\u65e0\u7b26\u53f7 char \u7684\u8f6c\u6362\u89c4\u5219\uff1b - bool \u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a int \uff0c false \u8f6c\u6362\u4e3a 0 \uff0c true \u8f6c\u6362\u4e3a 1 \u3002 \u8fd8\u6709\u4e00\u4e9b\u4e0d\u5e38\u7528\u7684\u6574\u578b\u8f6c\u6362\u89c4\u5219\u53ef\u4ee5\u5728 \u8fd9\u91cc \u627e\u5230\u3002 \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u5141\u8bb8\u6211\u4eec\u7f16\u5199\u4e00\u4e2a\u63a5\u53d7\u5f62\u53c2\u7c7b\u578b\u4e3a int \u7684\u51fd\u6570\uff0c\u7136\u540e\u4e0e\u5176\u4ed6\u5404\u79cd\u6574\u578b\u7c7b\u578b\u914d\u5408\u4f7f\u7528\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void printInt ( int x ) { std :: cout << x ; } int main () { printInt ( 2 ); short s { 3 }; // there is no short literal suffix, so we'll use a variable for this one printInt ( s ); // numeric promotion of short to int printInt ( 'a' ); // numeric promotion of char to int printInt ( true ); // numeric promotion of bool to int return 0 ; } \u8fd9\u91cc\u6709\u4e24\u70b9\u503c\u5f97\u6ce8\u610f\u3002\u9996\u5148\uff0c\u5728\u67d0\u4e9b\u7cfb\u7edf\u4e0a\uff0c\u4e00\u4e9b\u6574\u578b\u53ef\u80fd\u4f1a\u88ab\u8f6c\u6362\u4e3a unsigned int \u800c\u4e0d\u662f int \u3002\u5176\u6b21\uff0c\u4e00\u4e9b\u8f83\u7a84\u7684\u65e0\u7b26\u53f7\u7c7b\u578b(\u5982 unsigned char )\u5c06\u88ab\u8f6c\u6362\u4e3a\u8f83\u5927\u7684\u6709\u7b26\u53f7\u7c7b\u578b(\u5982 int )\u3002\u56e0\u6b64\uff0c\u867d\u7136\u6574\u578b\u63d0\u5347\u662f\u4fdd\u503c\u7684\uff0c\u4f46\u4e0d\u4e00\u5b9a\u80fd\u4fdd\u53f7\u3002 \u4e0d\u662f\u6240\u6709\u7684\u503c\u4fdd\u7559\u7c7b\u578b\u8f6c\u6362\u90fd\u662f\u6570\u503c\u63d0\u5347 \u4e00\u4e9b \u503c\u4fdd\u7559(value-preserving) \u7684\u7c7b\u578b\u8f6c\u6362(\u4f8b\u5982 char \u5230 short \u3001 int \u5230 long \u6216\u8005 int \u5230 double ) \u5728 C++ \u4e2d\u5e76\u4e0d\u88ab\u770b\u505a\u6570\u503c\u63d0\u5347\uff08\u5b83\u4eec\u5c5e\u4e8e \u6570\u503c\u8f6c\u6362 \uff09\uff0c\u6211\u4eec\u4f1a\u5728 8.3 - \u6570\u503c\u8f6c\u6362 \u4e2d\u8fdb\u884c\u4ecb\u7ecd)\u3002\u8fd9\u662f\u56e0\u4e3a\u8fd9\u6837\u7684\u8f6c\u6362\u4e0d\u80fd\u5e2e\u52a9\u5b9e\u73b0\u5c06\u8f83\u5c0f\u7c7b\u578b\u8f6c\u6362\u4e3a\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u5904\u7406\u7684\u8f83\u5927\u7c7b\u578b\u7684\u76ee\u6807\u3002 \u8fd9\u79cd\u533a\u522b\u4e3b\u8981\u662f\u5b66\u672f\u4e0a\u7684\u3002\u4f46\u662f\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u4f1a\u503e\u5411\u4e8e\u6570\u503c\u63d0\u5347\u800c\u4e0d\u662f\u6570\u503c\u8f6c\u6362\u3002\u5f53\u6211\u4eec\u8ba8\u8bba\u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u65f6\uff0c\u6211\u4eec\u5c06\u770b\u5230\u8fd9\u6837\u505a\u7684\u4e0d\u540c\u4e4b\u5904(\u53c2\u89c1\uff1a 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u5339\u914d\u6b67\u4e49 )\u3002","title":"8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347"},{"location":"8-2-Floating-point-and-integral-promotion/#_1","text":"\u56e0\u4e3a C++ \u88ab\u8bbe\u8ba1\u4e3a\u53ef\u4ee5\u5728\u5e7f\u6cdb\u7684\u5e73\u53f0\u4e0a\u8fdb\u884c\u79fb\u690d\uff0c\u56e0\u6b64\u8bed\u8a00\u7684\u8bbe\u8ba1\u8005\u4eec\u5e76\u4e0d\u60f3\u60f3\u5047\u5b9a\u7ed9\u5b9a\u7684 CPU \u80fd\u591f\u9ad8\u6548\u5730\u64cd\u4f5c\u6bd4\u81ea\u7136\u5bbd\u5ea6\u66f4\u7a84\u7684\u6570\u636e\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u6311\u6218\uff0cC++ \u5b9a\u4e49\u4e86\u4e00\u79cd\u7c7b\u578b\u7684\u6570\u636e\u8f6c\u6362\uff0c\u5176\u975e\u6b63\u5f0f\u540d\u79f0\u53eb\u505a \u6570\u503c\u63d0\u5347(numeric promotions) \u3002\u6570\u503c\u63d0\u5347\u5c5e\u4e8e\u4e00\u7c7b\u7c7b\u578b\u8f6c\u6362\uff0c\u5b83\u5c06\u66f4\u7a84\u7684\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982 char \uff09\u8f6c\u6362\u4e3a\u66f4\u5bbd\u7684\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982 int \u6216 double \uff09\u4f7f\u5176\u80fd\u591f\u66f4\u9ad8\u6548\u5730\u88ab\u5904\u7406\uff0c\u540c\u65f6\u4e5f\u66f4\u4e0d\u5bb9\u6613\u6ea2\u51fa\u3002 \u6240\u6709\u6570\u503c\u63d0\u5347\u90fd\u662f \u503c\u4fdd\u7559(value-preserving) \u7684\uff0c\u8fd9\u610f\u5473\u7740\u539f\u59cb\u7c7b\u578b\u4e2d\u7684\u6240\u6709\u503c\u90fd\u53ef\u4ee5\u5728\u4e0d\u635f\u5931\u6570\u636e\u6216\u7cbe\u5ea6\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u8868\u793a\u3002\u56e0\u4e3a\u6570\u503c\u63d0\u5347\u662f\u5b89\u5168\u7684\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u7531\u5730\u4f7f\u7528\u6570\u5b57\u63d0\u5347\uff0c\u5e76\u4e14\u5728\u8fd9\u6837\u505a\u65f6\u4e0d\u4f1a\u53d1\u51fa\u8b66\u544a\u3002","title":"\u6570\u503c\u63d0\u5347"},{"location":"8-2-Floating-point-and-integral-promotion/#_2","text":"\u6570\u503c\u63d0\u5347\u8fd8\u53ef\u4ee5\u89e3\u51b3\u5176\u4ed6\u95ee\u9898\u3002\u8003\u8651\u8fd9\u6837\u4e00\u4e2a\u573a\u666f\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u6253\u5370 int \u7c7b\u578b\u7684\u503c\uff1a 1 2 3 4 5 6 #include void printInt ( int x ) { std :: cout << x ; } \u7a0b\u5e8f\u975e\u5e38\u7b80\u5355\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u8fd8\u5e0c\u671b\u6253\u5370 short \u6216\u8005 char \u7c7b\u578b\u7684\u503c\u5462\uff1f\u5982\u679c\u6ca1\u6709\u7c7b\u578b\u8f6c\u6362\u7684\u652f\u6301\uff0c\u6211\u4eec\u5c31\u5fc5\u987b\u7f16\u5199\u53e6\u5916\u4e24\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\u5206\u522b\u7528\u4e8e\u6253\u5370 short \u6216\u8005 char \u7c7b\u578b\u7684\u503c\u3002\u66f4\u4e0d\u7528\u8bf4\u8fd8\u6709\u5176\u4ed6\u7c7b\u578b\u4e86\uff08 unsigned char , signed char , unsigned short , wchar_t , char8_t , char16_t \u548c char32_t \uff09\u3002\u5f88\u663e\u7136\uff0c\u8fd9\u6837\u7684\u4ee3\u7801\u662f\u4e0d\u53ef\u7ef4\u62a4\u7684\u3002 \u6570\u503c\u63d0\u5347\u5728\u8fd9\u91cc\u8d77\u5230\u4e86\u5173\u952e\u7684\u4f5c\u7528\uff1a\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u5177\u6709 int \u548c/\u6216 double \u5f62\u53c2\u7684\u51fd\u6570(\u6bd4\u5982\u4e0a\u9762\u7684 printInt() \u51fd\u6570)\u3002\u7136\u540e\u4f7f\u7528\u7c7b\u578b\u80fd\u591f\u5728\u8c03\u7528\u51fd\u6570\u662f\u901a\u8fc7\u6570\u503c\u63d0\u5347\u7c7b\u578b\u8f6c\u6362\u8fdb\u884c\u5339\u914d\u7684\u53c2\u6570\u5373\u53ef\u3002","title":"\u6570\u503c\u63d0\u5347\u80fd\u591f\u907f\u514d\u5197\u4f59\u4ee3\u7801"},{"location":"8-2-Floating-point-and-integral-promotion/#_3","text":"\u6570\u503c\u63d0\u5347\u8fd8\u80fd\u591f\u8fdb\u4e00\u6b65\u5206\u4e3a\u4e24\u79cd\u5b50\u7c7b\u578b\uff1a\u6574\u578b\u63d0\u5347\u548c\u6d6e\u70b9\u6570\u63d0\u5347\u3002","title":"\u6570\u503c\u63d0\u5347\u5206\u7c7b"},{"location":"8-2-Floating-point-and-integral-promotion/#_4","text":"\u5148\u4ece\u7b80\u5355\u7684\u5f00\u59cb\u3002 \u57fa\u4e8e\u6d6e\u70b9\u6570\u63d0\u5347\u7684\u89c4\u5219\uff0c float \u7c7b\u578b\u7684\u503c\u53ef\u4ee5\u88ab\u8f6c\u6362 double \u3002 \u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u63a5\u53d7 double \u53c2\u6570\u7684\u51fd\u6570\uff0c\u7136\u540e\u7528 double \u6216 float \u503c\u8c03\u7528\u5b83\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include void printDouble ( double d ) { std :: cout << d ; } int main () { printDouble ( 5.0 ); // no conversion necessary printDouble ( 4.0f ); // numeric promotion of float to double return 0 ; } \u5728\u7b2c\u4e8c\u6b21\u8c03\u7528 printDouble() \u65f6 float \u5b57\u9762\u91cf 4.0f \u88ab\u63d0\u5347\u4e3a double \uff0c\u56e0\u6b64\u5b9e\u53c2\u7684\u7c7b\u578b\u4e0e\u51fd\u6570\u5f62\u53c2\u7684\u7c7b\u578b\u5339\u914d\u3002","title":"\u6d6e\u70b9\u6570\u63d0\u5347"},{"location":"8-2-Floating-point-and-integral-promotion/#_5","text":"\u6574\u578b\u6570\u503c\u63d0\u5347\u5c31\u66f4\u52a0\u590d\u6742\u4e86\u3002 \u57fa\u4e8e\u6574\u578b\u6570\u503c\u63d0\u5347\u7684\u89c4\u5219\uff0c\u53ef\u4ee5\u8fdb\u884c\u5982\u4e0b\u7684\u7c7b\u578b\u8f6c\u6362\uff1a - \u65e0\u7b26\u53f7 char \u6216\u8005\u6709\u7b26\u53f7 char \u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a int \uff1b - \u65e0\u7b26\u53f7 char \uff0c char8_t \u4ee5\u53ca\u65e0\u7b26\u53f7 short \u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a int \uff0c\u53ea\u8981 int \u7684\u8303\u56f4\u8db3\u591f\u8868\u793a\u8be5\u7c7b\u578b\u7684\u8303\u56f4\uff0c\u5426\u5219\u4f1a\u8f6c\u6362\u4e3a\u65e0\u7b26\u53f7 int \uff1b - \u5982\u679c char \u9ed8\u8ba4\u6709\u7b26\u53f7\u7684\uff0c \u5219\u4f1a\u9075\u5faa\u4e0a\u8ff0\u6709\u7b26\u53f7 char \u7684\u8f6c\u6362\u89c4\u5219\u3002\u5982\u679c\u9ed8\u8ba4\u662f\u65e0\u7b26\u53f7\u7684\uff0c\u5219\u9075\u5faa\u4e0a\u8ff0\u65e0\u7b26\u53f7 char \u7684\u8f6c\u6362\u89c4\u5219\uff1b - bool \u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a int \uff0c false \u8f6c\u6362\u4e3a 0 \uff0c true \u8f6c\u6362\u4e3a 1 \u3002 \u8fd8\u6709\u4e00\u4e9b\u4e0d\u5e38\u7528\u7684\u6574\u578b\u8f6c\u6362\u89c4\u5219\u53ef\u4ee5\u5728 \u8fd9\u91cc \u627e\u5230\u3002 \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u5141\u8bb8\u6211\u4eec\u7f16\u5199\u4e00\u4e2a\u63a5\u53d7\u5f62\u53c2\u7c7b\u578b\u4e3a int \u7684\u51fd\u6570\uff0c\u7136\u540e\u4e0e\u5176\u4ed6\u5404\u79cd\u6574\u578b\u7c7b\u578b\u914d\u5408\u4f7f\u7528\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void printInt ( int x ) { std :: cout << x ; } int main () { printInt ( 2 ); short s { 3 }; // there is no short literal suffix, so we'll use a variable for this one printInt ( s ); // numeric promotion of short to int printInt ( 'a' ); // numeric promotion of char to int printInt ( true ); // numeric promotion of bool to int return 0 ; } \u8fd9\u91cc\u6709\u4e24\u70b9\u503c\u5f97\u6ce8\u610f\u3002\u9996\u5148\uff0c\u5728\u67d0\u4e9b\u7cfb\u7edf\u4e0a\uff0c\u4e00\u4e9b\u6574\u578b\u53ef\u80fd\u4f1a\u88ab\u8f6c\u6362\u4e3a unsigned int \u800c\u4e0d\u662f int \u3002\u5176\u6b21\uff0c\u4e00\u4e9b\u8f83\u7a84\u7684\u65e0\u7b26\u53f7\u7c7b\u578b(\u5982 unsigned char )\u5c06\u88ab\u8f6c\u6362\u4e3a\u8f83\u5927\u7684\u6709\u7b26\u53f7\u7c7b\u578b(\u5982 int )\u3002\u56e0\u6b64\uff0c\u867d\u7136\u6574\u578b\u63d0\u5347\u662f\u4fdd\u503c\u7684\uff0c\u4f46\u4e0d\u4e00\u5b9a\u80fd\u4fdd\u53f7\u3002","title":"\u6574\u578b\u63d0\u5347"},{"location":"8-2-Floating-point-and-integral-promotion/#_6","text":"\u4e00\u4e9b \u503c\u4fdd\u7559(value-preserving) \u7684\u7c7b\u578b\u8f6c\u6362(\u4f8b\u5982 char \u5230 short \u3001 int \u5230 long \u6216\u8005 int \u5230 double ) \u5728 C++ \u4e2d\u5e76\u4e0d\u88ab\u770b\u505a\u6570\u503c\u63d0\u5347\uff08\u5b83\u4eec\u5c5e\u4e8e \u6570\u503c\u8f6c\u6362 \uff09\uff0c\u6211\u4eec\u4f1a\u5728 8.3 - \u6570\u503c\u8f6c\u6362 \u4e2d\u8fdb\u884c\u4ecb\u7ecd)\u3002\u8fd9\u662f\u56e0\u4e3a\u8fd9\u6837\u7684\u8f6c\u6362\u4e0d\u80fd\u5e2e\u52a9\u5b9e\u73b0\u5c06\u8f83\u5c0f\u7c7b\u578b\u8f6c\u6362\u4e3a\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u5904\u7406\u7684\u8f83\u5927\u7c7b\u578b\u7684\u76ee\u6807\u3002 \u8fd9\u79cd\u533a\u522b\u4e3b\u8981\u662f\u5b66\u672f\u4e0a\u7684\u3002\u4f46\u662f\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u4f1a\u503e\u5411\u4e8e\u6570\u503c\u63d0\u5347\u800c\u4e0d\u662f\u6570\u503c\u8f6c\u6362\u3002\u5f53\u6211\u4eec\u8ba8\u8bba\u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u65f6\uff0c\u6211\u4eec\u5c06\u770b\u5230\u8fd9\u6837\u505a\u7684\u4e0d\u540c\u4e4b\u5904(\u53c2\u89c1\uff1a 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u5339\u914d\u6b67\u4e49 )\u3002","title":"\u4e0d\u662f\u6240\u6709\u7684\u503c\u4fdd\u7559\u7c7b\u578b\u8f6c\u6362\u90fd\u662f\u6570\u503c\u63d0\u5347"},{"location":"8-3-Numeric-conversions/","tags":["numeric conversions","conversions","static_cast"],"text":"Key Takeaway \u7b26\u5408\u6570\u503c\u63d0\u5347\u89c4\u5219\u7684\u7c7b\u578b\u8f6c\u6362\u662f\u9996\u5148\u662f\u6570\u503c\u63d0\u5347\uff0c\u4e0d\u662f\u6570\u503c\u8f6c\u6362 \u6570\u503c\u8f6c\u6362\u6db5\u76d6\u4e86\u6570\u503c\u63d0\u5347\u89c4\u5219\u672a\u6d89\u53ca\u7684\u5176\u4ed6\u7c7b\u578b\u8f6c\u6362 5 \u79cd\u57fa\u672c\u6570\u503c\u8f6c\u6362\uff1a \u6574\u578b\u8f6c\u6574\u578b \u6d6e\u70b9\u8f6c\u6d6e\u70b9 \u6d6e\u70b9\u8f6c\u6574\u578b \u6574\u578b\u8f6c\u6d6e\u70b9 \u6574\u578b\u6216\u6d6e\u70b9\u8f6c\u5e03\u5c14 \u6570\u503c\u63d0\u5347\u603b\u662f\u5b89\u5168\u7684\uff0c\u4f46\u662f\u6570\u503c\u8f6c\u6362\u4e0d\u4e00\u5b9a\uff0c\u53ef\u80fd\u9020\u6210\u6570\u636e\u6216\u7cbe\u5ea6\u4e22\u5931 \u7f29\u7a84\u8f6c\u6362\u4f1a\u9020\u6210\u7cbe\u5ea6\u4e22\u5931\u3002\u7f16\u8bd1\u5668\u4f1a\u5728\u53d1\u751f\u9690\u5f0f\u7f29\u7a84\u8f6c\u6362\u65f6\u62a5\u544a\u9519\u8bef \u5c3d\u53ef\u80fd\u907f\u514d\u7f29\u7a84\u8f6c\u6362\u3002\u5982\u679c\u4e00\u5b9a\u8981\u8fdb\u884c\u8f6c\u6362\uff0c\u4f7f\u7528 static_cast \u663e\u5f0f\u5730\u8fdb\u884c\u7f29\u7a84\u8f6c\u6362\u3002 \u62ec\u53f7\u521d\u59cb\u5316\u4e0d\u5141\u8bb8\u9690\u5f0f\u5730\u7f29\u7a84\u8f6c\u6362 \u4e00\u4e9b\u9700\u8981\u6ce8\u610f\u7684\u91cd\u8981\u89c4\u5219\uff1a \u5c06\u503c\u8f6c\u6362\u4e3a\u5176\u8303\u56f4\u4e0d\u652f\u6301\u8be5\u503c\u7684\u7c7b\u578b\u5c06\u53ef\u80fd\u5bfc\u81f4\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c \u53ea\u8981\u503c\u9002\u5408\u8f83\u5c0f\u7c7b\u578b\u7684\u8303\u56f4\uff0c\u4ece\u8f83\u5927\u7684\u6574\u578b\u6216\u6d6e\u70b9\u7c7b\u578b\u8f6c\u6362\u4e3a\u540c\u4e00\u65cf\u7684\u8f83\u5c0f\u7c7b\u578b\u901a\u5e38\u662f\u6709\u6548\u7684 \u6574\u6570\u8f6c\u6d6e\u70b9\u6570\uff0c\u53ea\u8981\u8303\u56f4ok\u5c31\u53ef\u4ee5 \u6d6e\u70b9\u6570\u8f6c\u6574\u6570\uff0c\u53ea\u8981\u8303\u56f4ok\u5c31\u53ef\u4ee5\uff0c\u4f46\u662f\u5c0f\u6570\u90e8\u5206\u4f1a\u4e22\u5931 \u5728\u4e0a\u4e00\u8282\u8bfe ( 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 ) \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86 \u6570\u503c\u63d0\u5347 \uff0c\u5b83\u5c5e\u4e8e\u4e00\u79cd\u7c7b\u578b\u8f6c\u6362\uff0c\u53ef\u4ee5\u5c06\u66f4\u7a84\u7684\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u66f4\u5bbd\u7684\u6570\u636e\u7c7b\u578b(\u901a\u5e38\u662f int \u6216 double ) \u4f7f\u5176\u53ef\u4ee5\u88ab\u66f4\u9ad8\u6548\u5730\u5904\u7406\u3002 C++ \u652f\u6301\u53e6\u4e00\u79cd\u7c7b\u6570\u503c\u7c7b\u578b\u8f6c\u6362\uff0c\u79f0\u4e3a \u6570\u503c\u8f6c\u6362(numeric conversions) \uff0c\u5b83\u6db5\u76d6\u4e86\u6570\u503c\u63d0\u5347\u89c4\u5219\u6ca1\u6709\u6db5\u76d6\u7684\u5176\u4ed6\u7c7b\u578b\u8f6c\u6362\u3002 \u5173\u952e\u4fe1\u606f \u4efb\u4f55\u7b26\u5408 \u6570\u503c\u63d0\u5347 \u89c4\u5219\uff08 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 \uff09\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u90fd\u5c5e\u4e8e \u6570\u503c\u63d0\u5347 \u800c\u4e0d\u662f\u6570\u503c\u8f6c\u6362\u3002 \u4e94\u79cd\u57fa\u672c\u7684\u6570\u503c\u8f6c\u6362\uff1a \u628a\u4e00\u79cd\u6574\u578b\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u6574\u578b\u7c7b\u578b\uff08\u6574\u578b\u63d0\u5347\u9664\u5916\uff09 1 2 3 short s = 3 ; // convert int to short long l = 3 ; // convert int to long char ch = s ; // convert short to char \u628a\u4e00\u79cd\u6d6e\u70b9\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u6d6e\u70b9\u7c7b\u578b\uff08\u6d6e\u70b9\u63d0\u5347\u9664\u5916\uff09: 1 2 float f = 3.0 ; // convert double to float long double ld = 3.0 ; // convert double to long double \u5c06\u6d6e\u70b9\u6570\u7c7b\u578b\u8f6c\u6362\u4e3a\u4efb\u4f55\u6574\u578b\u7c7b\u578b\uff1a 1 int i = 3.5 ; // convert double to int \u5c06\u6574\u578b\u7c7b\u578b\u8f6c\u6362\u4e3a\u4efb\u4f55\u6d6e\u70b9\u7c7b\u578b\uff1a 1 double d = 3 ; // convert int to double \u5c06\u6574\u578b\u6216\u6d6e\u70b9\u578b\u8f6c\u6362\u4e3abool\u7c7b\u578b\uff1a 1 2 bool b1 = 3 ; // convert int to bool bool b2 = 3.0 ; // convert double to bool \u9898\u5916\u8bdd \u7531\u4e8e\u5927\u62ec\u53f7\u521d\u59cb\u5316\u4e0d\u5141\u8bb8\u8fdb\u884c\u4e00\u4e9b\u6570\u503c\u8f6c\u6362(\u7a0d\u540e\u4f1a\u8be6\u7ec6\u4ecb\u7ecd)\uff0c\u4e3a\u4e86\u4f7f\u793a\u4f8b\u7b80\u5355\uff0c\u6211\u4eec\u5728\u672c\u8bfe\u4e2d\u4f7f\u7528\u4e86\u590d\u5236\u521d\u59cb\u5316(\u8fd9\u79cd\u521d\u59cb\u5316\u65b9\u6cd5\u6ca1\u6709\u8fd9\u6837\u7684\u9650\u5236)\u3002 \u7f29\u7a84\u8f6c\u6362 \u4e0e\u6570\u503c\u63d0\u5347(\u59cb\u7ec8\u662f\u5b89\u5168\u7684)\u4e0d\u540c\uff0c\u6570\u503c\u8f6c\u6362\u53ef\u80fd(\u4e5f\u53ef\u80fd\u4e0d\u4f1a)\u5bfc\u81f4\u6570\u636e\u6216\u7cbe\u5ea6\u7684\u4e22\u5931\u3002 \u6709\u4e9b\u6570\u503c\u8f6c\u6362\u603b\u662f\u5b89\u5168\u7684(\u4f8b\u5982 int \u5230 long \uff0c\u6216\u8005 int \u5230 double )\u3002\u5176\u4ed6\u6570\u503c\u8f6c\u6362\uff0c\u4f8b\u5982 double \u5230 int \uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u4e22\u5931(\u53d6\u51b3\u4e8e\u8f6c\u6362\u7684\u7279\u5b9a\u503c\u548c/\u6216\u57fa\u7840\u7c7b\u578b\u7684\u8303\u56f4)\uff1a 1 2 int i1 = 3.5 ; // the 0.5 is dropped, resulting in lost data int i2 = 3.0 ; // okay, will be converted to value 3, so no data is lost \u5728 C++ \u4e2d \u7f29\u7a84\u8f6c\u6362(narrowing conversion) \u662f\u4e00\u79cd\u53ef\u80fd\u4f1a\u9020\u6210\u6570\u636e\u4e22\u5931\u7684\u6570\u503c\u8f6c\u6362\u3002\u8fd9\u79cd\u7f29\u7a84\u8f6c\u6362\u5305\u62ec\u4e3a\uff1a \u6d6e\u70b9\u6570\u7c7b\u578b\u8f6c\u6362\u4e3a\u6574\u578b\u7c7b\u578b\uff1b \u8f83\u5bbd\u7684\u6d6e\u70b9\u7c7b\u578b\u8f6c\u6362\u4e3a\u8f83\u7a84\u7684\u6d6e\u70b9\u7c7b\u578b\uff0c\u9664\u975e\u88ab\u8f6c\u6362\u7684\u503c\u662f constexpr \u7c7b\u578b\u4e14\u5b83\u7684\u8303\u56f4\u5728\u76ee\u6807\u7c7b\u578b\u8303\u56f4\u5185(\u5373\u4f7f\u8f83\u7a84\u7684\u7c7b\u578b\u4e0d\u5177\u5907\u5b58\u653e\u5b8c\u6574\u6570\u636e\u7684\u7cbe\u5ea6\uff09\u3002 \u4ece\u6574\u6570\u7c7b\u578b\u8f6c\u6362\u4e3a\u6d6e\u70b9\u7c7b\u578b\uff0c\u9664\u975e\u8f6c\u6362\u7684\u503c\u662f constexpr \u4e14\u5728\u76ee\u6807\u7c7b\u578b\u8303\u56f4\u5185\uff0c\u5e76\u4e14\u53ef\u4ee5\u8f6c\u6362\u56de\u539f\u59cb\u7c7b\u578b\u800c\u4e0d\u4e22\u5931\u6570\u636e\u3002 \u4ece\u8f83\u5bbd\u7684\u6574\u578b\u8f6c\u6362\u4e3a\u8f83\u7a84\u7684\u6574\u578b\uff0c\u9664\u975e\u8f6c\u6362\u7684\u503c\u662f constexpr \u4e14\u6574\u578b\u63d0\u5347\u540e\u9002\u5408\u76ee\u6807\u7c7b\u578b\u3002 \u597d\u6d88\u606f\u662f\u4f60\u4e0d\u9700\u8981\u8bb0\u4f4f\u8fd9\u4e9b\u3002\u5f53\u7f16\u8bd1\u5668\u786e\u5b9a\u9700\u8981\u9690\u5f0f\u7f29\u7a84\u8f6c\u6362\u65f6\uff0c\u5b83\u901a\u5e38\u4f1a\u53d1\u51fa\u8b66\u544a(\u6216\u9519\u8bef)\u3002 \u6ce8\u610f \u5f53\u5c06 signed int \u8f6c\u6362\u4e3a unsigned int \u65f6\uff0c\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a \u4e0d\u4f1a \u53d1\u51fa\u8b66\u544a\uff0c\u53cd\u4e4b\u4ea6\u7136\uff0c\u5373\u4f7f\u8fd9\u4e9b\u662f\u7f29\u7a84\u8f6c\u6362\u3002\u8981\u7279\u522b\u5c0f\u5fc3\u8fd9\u4e9b\u7c7b\u578b\u4e4b\u95f4\u65e0\u610f\u7684\u8f6c\u6362(\u7279\u522b\u662f\u5f53\u5411\u5e26\u76f8\u53cd\u7b26\u53f7\u5f62\u53c2\u7684\u51fd\u6570\u4f20\u9012\u5b9e\u53c2\u65f6)\u3002 \u4f8b\u5982\uff0c\u5f53\u7f16\u8bd1\u4e0b\u9762\u7684\u7a0b\u5e8f\u65f6\uff1a 1 2 3 4 int main () { int i = 3.5 ; } Visual Studio \u4f1a\u4ea7\u751f\u5982\u4e0b\u544a\u8b66\u4fe1\u606f\uff1a 1 warning C4244: 'initializing': conversion from 'double' to 'int', possible loss of data \u4e00\u822c\u6765\u8bf4\uff0c\u5e94\u8be5\u907f\u514d\u7f29\u7a84\u8f6c\u6362\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u9700\u8981\u8fd9\u6837\u505a\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u60a8\u5e94\u8be5\u4f7f\u7528 static_cast \u663e\u5f0f\u5730\u8fdb\u884c\u7f29\u7a84\u8f6c\u6362\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 void someFcn ( int i ) { } int main () { double d { 5.0 }; someFcn ( d ); // \u4e0d\u597d: \u7f16\u8bd1\u5668\u4f1a\u4ea7\u751f\u7f29\u7a84\u8f6c\u6362\u544a\u8b66 someFcn ( static_cast < int > ( d )); // \u597d\uff1a\u663e\u5f0f\u5730\u544a\u77e5\u7f16\u8bd1\u5668\u4ec5\u7f29\u7a84\u8f6c\u6362\uff0c\u4e0d\u4f1a\u4ea7\u751f\u544a\u8b66 return 0 ; } \u6700\u4f73\u5b9e\u8df5 \u5c3d\u53ef\u80fd\u907f\u514d\u7f29\u7a84\u8f6c\u6362\u3002\u5982\u679c\u4e00\u5b9a\u8981\u8fdb\u884c\u8f6c\u6362\uff0c\u4f7f\u7528 static_cast \u663e\u5f0f\u5730\u8fdb\u884c\u7f29\u7a84\u8f6c\u6362\u3002 \u62ec\u53f7\u521d\u59cb\u5316\u4e0d\u5141\u8bb8\u9690\u5f0f\u7f29\u7a84\u8f6c\u6362 \u5728\u4f7f\u7528\u5927\u62ec\u53f7\u521d\u59cb\u5316\u65f6\uff0c\u4e25\u683c\u7981\u6b62\u7f29\u7a84\u8f6c\u6362(\u8fd9\u4e5f\u662f\u9996\u9009\u6b64\u521d\u59cb\u5316\u5f62\u5f0f\u7684\u4e3b\u8981\u539f\u56e0\u4e4b\u4e00)\uff1a 1 2 3 4 int main () { int i { 3.5 }; // won't compile } Visual Studio \u4f1a\u4ea7\u751f\u5982\u4e0b\u9519\u8bef\u4fe1\u606f\uff1a 1 error C2397: conversion from 'double' to 'int' requires a narrowing conversion \u5173\u4e8e\u6570\u503c\u8f6c\u6362\u7684\u66f4\u591a\u4fe1\u606f \u6570\u503c\u8f6c\u6362\u7684\u5177\u4f53\u89c4\u5219\u53c8\u591a\u53c8\u590d\u6742\uff0c\u6240\u4ee5\u8fd9\u91cc\u6211\u4eec\u53ea\u8bb2\u6700\u91cd\u8981\u3001\u6700\u9700\u8981\u8bb0\u5fc6\u7684\u3002 \u5728 \u6240\u6709 \u60c5\u51b5\u4e0b\uff0c\u5c06\u503c\u8f6c\u6362\u4e3a\u5176\u8303\u56f4\u4e0d\u652f\u6301\u8be5\u503c\u7684\u7c7b\u578b\u5c06\u53ef\u80fd\u5bfc\u81f4\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 int main () { int i { 30000 }; char c = i ; // chars have range -128 to 127 std :: cout << static_cast < int > ( c ); return 0 ; } \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4e00\u4e2a\u5927\u6574\u6570\u8d4b\u7ed9\u4e00\u4e2a char \u578b\u53d8\u91cf(\u8303\u56f4\u4e3a-128\u5230127)\u3002\u8fd9\u4f1a\u5bfc\u81f4 char \u6ea2\u51fa\uff0c\u5e76\u4ea7\u751f\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c: 1 48 \u53ea\u8981\u503c\u9002\u5408\u8f83\u5c0f\u7c7b\u578b\u7684\u8303\u56f4\uff0c\u4ece\u8f83\u5927\u7684\u6574\u578b\u6216\u6d6e\u70b9\u7c7b\u578b\u8f6c\u6362\u4e3a\u540c\u4e00\u65cf\u7684\u8f83\u5c0f\u7c7b\u578b\u901a\u5e38\u662f\u6709\u6548\u7684\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 int i { 2 }; short s = i ; // convert from int to short std :: cout << s << '\\n' ; double d { 0.1234 }; float f = d ; std :: cout << f << '\\n' ; \u80fd\u591f\u8f93\u51fa\u9884\u671f\u7684\u7ed3\u679c\uff1a 1 2 2 0.1234 \u5bf9\u4e8e\u6d6e\u70b9\u6570\u7684\u4f8b\u5b50\uff0c\u5728\u66f4\u5c0f\u7684\u7c7b\u578b\u4e2d\u53ef\u80fd\u51fa\u73b0\u820d\u5165\u8bef\u5dee\uff0c\u4f8b\u5982\uff1a 1 2 float f = 0.123456789 ; // double \u503c 0.123456789 \u6709 9 \u4f4d\u6709\u6548\u6570\u7ec4\uff0c\u4f46\u662f float \u53ea\u80fd\u652f\u6301 7 \u4f4d\u6709\u6548\u6570\u5b57 std :: cout << std :: setprecision ( 9 ) << f << '\\n' ; // std::setprecision defined in iomanip header \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u7531\u4e8e float \u4e0d\u80fd\u652f\u6301 double \u7c7b\u578b\u7684\u7cbe\u5ea6\uff0c\u51fa\u73b0\u4e86\u7cbe\u5ea6\u4e22\u5931\u7684\u95ee\u9898\uff1a 1 0.123456791 \u4ece\u6574\u6570\u5230\u6d6e\u70b9\u6570\u7684\u8f6c\u6362\u901a\u5e38\u53ea\u8981\u503c\u5728\u6d6e\u70b9\u7c7b\u578b\u7684\u8303\u56f4\u5185\u5c31\u53ef\u4ee5\u5de5\u4f5c\u3002\u4f8b\u5982: 1 2 3 int i { 10 }; float f = i ; std :: cout << f ; \u53ef\u4ee5\u8f93\u51fa\u9884\u671f\u7684\u7ed3\u679c\uff1a 1 10 \u4ece\u6d6e\u70b9\u6570\u5230\u6574\u6570\u7684\u8f6c\u6362\u53ea\u8981\u503c\u5728\u6574\u6570\u7684\u8303\u56f4\u5185\u5c31\u53ef\u4ee5\u5de5\u4f5c\uff0c\u4f46\u662f\u4efb\u4f55\u5c0f\u6570\u503c\u90fd\u4f1a\u4e22\u5931\u3002\u4f8b\u5982: 1 2 int i = 3.5 ; std :: cout << i << '\\n' ; \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4e22\u5931\u4e86\u5c0f\u6570\u503c(.5)\uff0c\u7559\u4e0b\u4ee5\u4e0b\u7ed3\u679c\uff1a 1 3 \u867d\u7136\u6570\u503c\u8f6c\u6362\u89c4\u5219\u770b\u8d77\u6765\u5f88\u53ef\u6015\uff0c\u4f46\u5b9e\u9645\u4e0a\uff0c\u5982\u679c\u4f60\u8bd5\u56fe\u505a\u4e00\u4e9b\u5371\u9669\u7684\u4e8b\u60c5(\u4e0d\u5305\u62ec\u4e00\u4e9b\u6709\u7b26\u53f7/\u65e0\u7b26\u53f7\u7684\u8f6c\u6362)\uff0c\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u53d1\u51fa\u8b66\u544a\u3002","title":"8.3 - \u6570\u503c\u8f6c\u6362"},{"location":"8-3-Numeric-conversions/#_1","text":"\u628a\u4e00\u79cd\u6574\u578b\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u6574\u578b\u7c7b\u578b\uff08\u6574\u578b\u63d0\u5347\u9664\u5916\uff09 1 2 3 short s = 3 ; // convert int to short long l = 3 ; // convert int to long char ch = s ; // convert short to char \u628a\u4e00\u79cd\u6d6e\u70b9\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u6d6e\u70b9\u7c7b\u578b\uff08\u6d6e\u70b9\u63d0\u5347\u9664\u5916\uff09: 1 2 float f = 3.0 ; // convert double to float long double ld = 3.0 ; // convert double to long double \u5c06\u6d6e\u70b9\u6570\u7c7b\u578b\u8f6c\u6362\u4e3a\u4efb\u4f55\u6574\u578b\u7c7b\u578b\uff1a 1 int i = 3.5 ; // convert double to int \u5c06\u6574\u578b\u7c7b\u578b\u8f6c\u6362\u4e3a\u4efb\u4f55\u6d6e\u70b9\u7c7b\u578b\uff1a 1 double d = 3 ; // convert int to double \u5c06\u6574\u578b\u6216\u6d6e\u70b9\u578b\u8f6c\u6362\u4e3abool\u7c7b\u578b\uff1a 1 2 bool b1 = 3 ; // convert int to bool bool b2 = 3.0 ; // convert double to bool \u9898\u5916\u8bdd \u7531\u4e8e\u5927\u62ec\u53f7\u521d\u59cb\u5316\u4e0d\u5141\u8bb8\u8fdb\u884c\u4e00\u4e9b\u6570\u503c\u8f6c\u6362(\u7a0d\u540e\u4f1a\u8be6\u7ec6\u4ecb\u7ecd)\uff0c\u4e3a\u4e86\u4f7f\u793a\u4f8b\u7b80\u5355\uff0c\u6211\u4eec\u5728\u672c\u8bfe\u4e2d\u4f7f\u7528\u4e86\u590d\u5236\u521d\u59cb\u5316(\u8fd9\u79cd\u521d\u59cb\u5316\u65b9\u6cd5\u6ca1\u6709\u8fd9\u6837\u7684\u9650\u5236)\u3002","title":"\u4e94\u79cd\u57fa\u672c\u7684\u6570\u503c\u8f6c\u6362\uff1a"},{"location":"8-3-Numeric-conversions/#_2","text":"\u4e0e\u6570\u503c\u63d0\u5347(\u59cb\u7ec8\u662f\u5b89\u5168\u7684)\u4e0d\u540c\uff0c\u6570\u503c\u8f6c\u6362\u53ef\u80fd(\u4e5f\u53ef\u80fd\u4e0d\u4f1a)\u5bfc\u81f4\u6570\u636e\u6216\u7cbe\u5ea6\u7684\u4e22\u5931\u3002 \u6709\u4e9b\u6570\u503c\u8f6c\u6362\u603b\u662f\u5b89\u5168\u7684(\u4f8b\u5982 int \u5230 long \uff0c\u6216\u8005 int \u5230 double )\u3002\u5176\u4ed6\u6570\u503c\u8f6c\u6362\uff0c\u4f8b\u5982 double \u5230 int \uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u4e22\u5931(\u53d6\u51b3\u4e8e\u8f6c\u6362\u7684\u7279\u5b9a\u503c\u548c/\u6216\u57fa\u7840\u7c7b\u578b\u7684\u8303\u56f4)\uff1a 1 2 int i1 = 3.5 ; // the 0.5 is dropped, resulting in lost data int i2 = 3.0 ; // okay, will be converted to value 3, so no data is lost \u5728 C++ \u4e2d \u7f29\u7a84\u8f6c\u6362(narrowing conversion) \u662f\u4e00\u79cd\u53ef\u80fd\u4f1a\u9020\u6210\u6570\u636e\u4e22\u5931\u7684\u6570\u503c\u8f6c\u6362\u3002\u8fd9\u79cd\u7f29\u7a84\u8f6c\u6362\u5305\u62ec\u4e3a\uff1a \u6d6e\u70b9\u6570\u7c7b\u578b\u8f6c\u6362\u4e3a\u6574\u578b\u7c7b\u578b\uff1b \u8f83\u5bbd\u7684\u6d6e\u70b9\u7c7b\u578b\u8f6c\u6362\u4e3a\u8f83\u7a84\u7684\u6d6e\u70b9\u7c7b\u578b\uff0c\u9664\u975e\u88ab\u8f6c\u6362\u7684\u503c\u662f constexpr \u7c7b\u578b\u4e14\u5b83\u7684\u8303\u56f4\u5728\u76ee\u6807\u7c7b\u578b\u8303\u56f4\u5185(\u5373\u4f7f\u8f83\u7a84\u7684\u7c7b\u578b\u4e0d\u5177\u5907\u5b58\u653e\u5b8c\u6574\u6570\u636e\u7684\u7cbe\u5ea6\uff09\u3002 \u4ece\u6574\u6570\u7c7b\u578b\u8f6c\u6362\u4e3a\u6d6e\u70b9\u7c7b\u578b\uff0c\u9664\u975e\u8f6c\u6362\u7684\u503c\u662f constexpr \u4e14\u5728\u76ee\u6807\u7c7b\u578b\u8303\u56f4\u5185\uff0c\u5e76\u4e14\u53ef\u4ee5\u8f6c\u6362\u56de\u539f\u59cb\u7c7b\u578b\u800c\u4e0d\u4e22\u5931\u6570\u636e\u3002 \u4ece\u8f83\u5bbd\u7684\u6574\u578b\u8f6c\u6362\u4e3a\u8f83\u7a84\u7684\u6574\u578b\uff0c\u9664\u975e\u8f6c\u6362\u7684\u503c\u662f constexpr \u4e14\u6574\u578b\u63d0\u5347\u540e\u9002\u5408\u76ee\u6807\u7c7b\u578b\u3002 \u597d\u6d88\u606f\u662f\u4f60\u4e0d\u9700\u8981\u8bb0\u4f4f\u8fd9\u4e9b\u3002\u5f53\u7f16\u8bd1\u5668\u786e\u5b9a\u9700\u8981\u9690\u5f0f\u7f29\u7a84\u8f6c\u6362\u65f6\uff0c\u5b83\u901a\u5e38\u4f1a\u53d1\u51fa\u8b66\u544a(\u6216\u9519\u8bef)\u3002 \u6ce8\u610f \u5f53\u5c06 signed int \u8f6c\u6362\u4e3a unsigned int \u65f6\uff0c\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a \u4e0d\u4f1a \u53d1\u51fa\u8b66\u544a\uff0c\u53cd\u4e4b\u4ea6\u7136\uff0c\u5373\u4f7f\u8fd9\u4e9b\u662f\u7f29\u7a84\u8f6c\u6362\u3002\u8981\u7279\u522b\u5c0f\u5fc3\u8fd9\u4e9b\u7c7b\u578b\u4e4b\u95f4\u65e0\u610f\u7684\u8f6c\u6362(\u7279\u522b\u662f\u5f53\u5411\u5e26\u76f8\u53cd\u7b26\u53f7\u5f62\u53c2\u7684\u51fd\u6570\u4f20\u9012\u5b9e\u53c2\u65f6)\u3002 \u4f8b\u5982\uff0c\u5f53\u7f16\u8bd1\u4e0b\u9762\u7684\u7a0b\u5e8f\u65f6\uff1a 1 2 3 4 int main () { int i = 3.5 ; } Visual Studio \u4f1a\u4ea7\u751f\u5982\u4e0b\u544a\u8b66\u4fe1\u606f\uff1a 1 warning C4244: 'initializing': conversion from 'double' to 'int', possible loss of data \u4e00\u822c\u6765\u8bf4\uff0c\u5e94\u8be5\u907f\u514d\u7f29\u7a84\u8f6c\u6362\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u9700\u8981\u8fd9\u6837\u505a\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u60a8\u5e94\u8be5\u4f7f\u7528 static_cast \u663e\u5f0f\u5730\u8fdb\u884c\u7f29\u7a84\u8f6c\u6362\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 void someFcn ( int i ) { } int main () { double d { 5.0 }; someFcn ( d ); // \u4e0d\u597d: \u7f16\u8bd1\u5668\u4f1a\u4ea7\u751f\u7f29\u7a84\u8f6c\u6362\u544a\u8b66 someFcn ( static_cast < int > ( d )); // \u597d\uff1a\u663e\u5f0f\u5730\u544a\u77e5\u7f16\u8bd1\u5668\u4ec5\u7f29\u7a84\u8f6c\u6362\uff0c\u4e0d\u4f1a\u4ea7\u751f\u544a\u8b66 return 0 ; } \u6700\u4f73\u5b9e\u8df5 \u5c3d\u53ef\u80fd\u907f\u514d\u7f29\u7a84\u8f6c\u6362\u3002\u5982\u679c\u4e00\u5b9a\u8981\u8fdb\u884c\u8f6c\u6362\uff0c\u4f7f\u7528 static_cast \u663e\u5f0f\u5730\u8fdb\u884c\u7f29\u7a84\u8f6c\u6362\u3002","title":"\u7f29\u7a84\u8f6c\u6362"},{"location":"8-3-Numeric-conversions/#_3","text":"\u5728\u4f7f\u7528\u5927\u62ec\u53f7\u521d\u59cb\u5316\u65f6\uff0c\u4e25\u683c\u7981\u6b62\u7f29\u7a84\u8f6c\u6362(\u8fd9\u4e5f\u662f\u9996\u9009\u6b64\u521d\u59cb\u5316\u5f62\u5f0f\u7684\u4e3b\u8981\u539f\u56e0\u4e4b\u4e00)\uff1a 1 2 3 4 int main () { int i { 3.5 }; // won't compile } Visual Studio \u4f1a\u4ea7\u751f\u5982\u4e0b\u9519\u8bef\u4fe1\u606f\uff1a 1 error C2397: conversion from 'double' to 'int' requires a narrowing conversion","title":"\u62ec\u53f7\u521d\u59cb\u5316\u4e0d\u5141\u8bb8\u9690\u5f0f\u7f29\u7a84\u8f6c\u6362"},{"location":"8-3-Numeric-conversions/#_4","text":"\u6570\u503c\u8f6c\u6362\u7684\u5177\u4f53\u89c4\u5219\u53c8\u591a\u53c8\u590d\u6742\uff0c\u6240\u4ee5\u8fd9\u91cc\u6211\u4eec\u53ea\u8bb2\u6700\u91cd\u8981\u3001\u6700\u9700\u8981\u8bb0\u5fc6\u7684\u3002 \u5728 \u6240\u6709 \u60c5\u51b5\u4e0b\uff0c\u5c06\u503c\u8f6c\u6362\u4e3a\u5176\u8303\u56f4\u4e0d\u652f\u6301\u8be5\u503c\u7684\u7c7b\u578b\u5c06\u53ef\u80fd\u5bfc\u81f4\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 int main () { int i { 30000 }; char c = i ; // chars have range -128 to 127 std :: cout << static_cast < int > ( c ); return 0 ; } \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4e00\u4e2a\u5927\u6574\u6570\u8d4b\u7ed9\u4e00\u4e2a char \u578b\u53d8\u91cf(\u8303\u56f4\u4e3a-128\u5230127)\u3002\u8fd9\u4f1a\u5bfc\u81f4 char \u6ea2\u51fa\uff0c\u5e76\u4ea7\u751f\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c: 1 48 \u53ea\u8981\u503c\u9002\u5408\u8f83\u5c0f\u7c7b\u578b\u7684\u8303\u56f4\uff0c\u4ece\u8f83\u5927\u7684\u6574\u578b\u6216\u6d6e\u70b9\u7c7b\u578b\u8f6c\u6362\u4e3a\u540c\u4e00\u65cf\u7684\u8f83\u5c0f\u7c7b\u578b\u901a\u5e38\u662f\u6709\u6548\u7684\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 int i { 2 }; short s = i ; // convert from int to short std :: cout << s << '\\n' ; double d { 0.1234 }; float f = d ; std :: cout << f << '\\n' ; \u80fd\u591f\u8f93\u51fa\u9884\u671f\u7684\u7ed3\u679c\uff1a 1 2 2 0.1234 \u5bf9\u4e8e\u6d6e\u70b9\u6570\u7684\u4f8b\u5b50\uff0c\u5728\u66f4\u5c0f\u7684\u7c7b\u578b\u4e2d\u53ef\u80fd\u51fa\u73b0\u820d\u5165\u8bef\u5dee\uff0c\u4f8b\u5982\uff1a 1 2 float f = 0.123456789 ; // double \u503c 0.123456789 \u6709 9 \u4f4d\u6709\u6548\u6570\u7ec4\uff0c\u4f46\u662f float \u53ea\u80fd\u652f\u6301 7 \u4f4d\u6709\u6548\u6570\u5b57 std :: cout << std :: setprecision ( 9 ) << f << '\\n' ; // std::setprecision defined in iomanip header \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u7531\u4e8e float \u4e0d\u80fd\u652f\u6301 double \u7c7b\u578b\u7684\u7cbe\u5ea6\uff0c\u51fa\u73b0\u4e86\u7cbe\u5ea6\u4e22\u5931\u7684\u95ee\u9898\uff1a 1 0.123456791 \u4ece\u6574\u6570\u5230\u6d6e\u70b9\u6570\u7684\u8f6c\u6362\u901a\u5e38\u53ea\u8981\u503c\u5728\u6d6e\u70b9\u7c7b\u578b\u7684\u8303\u56f4\u5185\u5c31\u53ef\u4ee5\u5de5\u4f5c\u3002\u4f8b\u5982: 1 2 3 int i { 10 }; float f = i ; std :: cout << f ; \u53ef\u4ee5\u8f93\u51fa\u9884\u671f\u7684\u7ed3\u679c\uff1a 1 10 \u4ece\u6d6e\u70b9\u6570\u5230\u6574\u6570\u7684\u8f6c\u6362\u53ea\u8981\u503c\u5728\u6574\u6570\u7684\u8303\u56f4\u5185\u5c31\u53ef\u4ee5\u5de5\u4f5c\uff0c\u4f46\u662f\u4efb\u4f55\u5c0f\u6570\u503c\u90fd\u4f1a\u4e22\u5931\u3002\u4f8b\u5982: 1 2 int i = 3.5 ; std :: cout << i << '\\n' ; \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4e22\u5931\u4e86\u5c0f\u6570\u503c(.5)\uff0c\u7559\u4e0b\u4ee5\u4e0b\u7ed3\u679c\uff1a 1 3 \u867d\u7136\u6570\u503c\u8f6c\u6362\u89c4\u5219\u770b\u8d77\u6765\u5f88\u53ef\u6015\uff0c\u4f46\u5b9e\u9645\u4e0a\uff0c\u5982\u679c\u4f60\u8bd5\u56fe\u505a\u4e00\u4e9b\u5371\u9669\u7684\u4e8b\u60c5(\u4e0d\u5305\u62ec\u4e00\u4e9b\u6709\u7b26\u53f7/\u65e0\u7b26\u53f7\u7684\u8f6c\u6362)\uff0c\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u53d1\u51fa\u8b66\u544a\u3002","title":"\u5173\u4e8e\u6570\u503c\u8f6c\u6362\u7684\u66f4\u591a\u4fe1\u606f"},{"location":"8-4-Arithmetic-conversions/","tags":["arithmetic conversions","conversions","typeinfo"],"text":"Key Takeaway C++ \u4e2d\u7684\u4e00\u4e9b\u4e8c\u5143\u8fd0\u7b97\u7b26\u8981\u6c42\u4e24\u4e2a\u64cd\u4f5c\u6570\u5177\u6709\u76f8\u540c\u7684\u4f18\u5148\u7ea7\uff0c\u5982\u679c\u4e0d\u6ee1\u8db3\u5219\u9700\u8981\u8fdb\u884c\u7b97\u672f\u8f6c\u6362 \u7b97\u672f\u8f6c\u6362\u89c4\u5219 \u5982\u679c\u81f3\u5c11\u6709\u4e00\u4e2a\u64cd\u4f5c\u6570\u5728\u4f18\u5148\u7ea7\u5217\u8868\u4e2d\uff0c\u5219\u5177\u6709\u8f83\u4f4e\u4f18\u5148\u7ea7\u7684\u64cd\u4f5c\u6570\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5177\u6709\u8f83\u9ad8\u4f18\u5148\u7ea7\u7684\u64cd\u4f5c\u6570\uff1b \u5426\u5219 (\u4e24\u4e2a\u64cd\u4f5c\u6570\u7684\u7c7b\u578b\u5747\u4e0d\u5728\u8868\u4e2d)\uff0c\u5219\u4e24\u4e2a\u64cd\u4f5c\u6570\u4f1a\u8fdb\u884c \u6570\u503c\u63d0\u5347 (\u53c2\u89c1\uff1a 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 )\u3002 \u4f7f\u7528 typeid \u8fd0\u7b97\u7b26 (\u5728 \u5934\u6587\u4ef6\u4e2d)\uff0c\u6765\u663e\u793a\u8868\u8fbe\u5f0f\u7ed3\u679c\u6240\u5c5e\u7684\u7c7b\u578b\u3002 \u65e0\u7b26\u53f7\u6574\u578b \u88ab\u7528\u5728\u7b97\u672f\u8868\u8fbe\u5f0f\u4e2d\u7684\u65f6\u5019\uff0c\u7531\u4e8e\u4f18\u5148\u7ea7\u6bd4 \u6574\u578b \u9ad8\uff0c\u4f1a\u5bfc\u81f4\u6574\u578b\u88ab\u8f6c\u6362\u4e3a\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u5982\u679c\u662f\u7b26\u53f7\u53ef\u80fd\u51fa\u73b0\u53cd\u8f6c\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u4e0d\u8981\u4f7f\u7528\u65e0\u7b26\u53f7\u6574\u578b\u7684\u539f\u56e0 \u5728 5.1 - \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b \u4e2d\u6211\u4eec\u8ba8\u8bba\u8fc7\uff0c\u8868\u8fbe\u5f0f\u662f\u5982\u4f55\u57fa\u4e8e\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b\u8fdb\u884c\u8fd0\u7b97\u7684\u3002 \u8003\u8651\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a 1 int x { 2 + 3 }; \u5f53\u4e8c\u5143\u8fd0\u7b97\u7b26 operator+ \u6267\u884c\u65f6\uff0c\u5b83\u63a5\u53d7\u4e86\u4e24\u4e2a\u64cd\u4f5c\u6570\uff0c\u4e14\u5747\u4e3a int \u7c7b\u578b\u3002\u56e0\u4e3a\u4e24\u4e2a\u64cd\u4f5c\u6570\u7684\u7c7b\u578b\u662f\u76f8\u540c\u7684\uff0c\u6240\u4ee5\u5b83\u4eec\u4f1a\u88ab\u7528\u6765\u6267\u884c\u8ba1\u7b97\u7136\u540e\u8fd4\u56de\u7ed3\u679c\u3002\u56e0\u6b64 2 + 3 \u4f1a\u5f97\u5230 int \u503c 5 \u3002 \u4f46\u662f\uff0c\u5982\u679c\u4e24\u4e2a\u64cd\u4f5c\u6570\u7c7b\u578b\u4e0d\u540c\u5462\uff1f 1 ??? y { 2 + 3.5 }; \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c operator+ \u7684\u4e24\u4e2a\u64cd\u4f5c\u6570\uff0c\u4e00\u4e2a\u662f int \uff0c\u53e6\u4e00\u4e2a\u662f double \u3002\u90a3\u4e48\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u5e94\u8be5\u662f\u4ec0\u4e48\u7c7b\u578b\u7684\u5462\uff1f\u662f int \u8fd8\u662f double \uff1f\u8fd8\u662f\u5176\u4ed6\u7684\u5462\uff1f\u5982\u679c\u662f\u5728\u5b9a\u4e49\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u5728\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u4f8b\u5982\u5728\u4f7f\u7528 std::cout << \u7684\u65f6\u5019\uff0c\u8ba1\u7b97\u5f97\u5230\u7684\u7c7b\u578b\u4f1a\u5f71\u54cd\u8f93\u51fa\u7684\u7ed3\u679c\u3002 \u5728C++\u4e2d\uff0c\u67d0\u4e9b\u64cd\u4f5c\u7b26\u8981\u6c42\u5b83\u4eec\u7684\u64cd\u4f5c\u6570\u5177\u6709\u76f8\u540c\u7684\u7c7b\u578b\u3002\u5982\u679c\u5728\u8c03\u7528\u8fd0\u7b97\u7b26\u65f6\uff0c\u4e24\u4e2a\u64cd\u4f5c\u6570\u7c7b\u578b\u4e0d\u540c\uff0c\u5219\u4f7f\u7528\u4e00\u7ec4\u79f0\u4e3a\u666e\u901a\u7b97\u672f\u8f6c\u6362\u7684\u89c4\u5219\u5c06\u5176\u4e2d\u4e00\u4e2a\u6216\u4e24\u4e2a\u64cd\u4f5c\u6570\u9690\u5f0f\u8f6c\u6362\u4e3a\u5339\u914d\u7c7b\u578b\u3002 \u9700\u8981\u64cd\u4f5c\u6570\u5177\u6709\u76f8\u540c\u7c7b\u578b\u7684\u8fd0\u7b97\u7b26 \u4e0b\u5217\u8fd0\u7b97\u7b26\u9700\u8981\u64cd\u4f5c\u6570\u5177\u6709\u76f8\u540c\u7c7b\u578b\uff1a \u4e8c\u5143\u7b97\u672f\u8fd0\u7b97\u7b26\uff1a + , - , * , / , % \u4e8c\u5143\u5173\u7cfb\u8fd0\u7b97\u7b26\uff1a < , > , <= , >= , == , != \u4e8c\u5143\u6309\u4f4d\u8fd0\u7b97\u7b26\uff1a & , ^ , | \u6761\u4ef6\u8fd0\u7b97\u7b26 ?: (\u5305\u62ec\u6761\u4ef6\uff0c\u8981\u6c42\u4e3a\u5e03\u5c14\u7c7b\u578b) \u4e00\u822c\u7b97\u672f\u8f6c\u6362\u89c4\u5219 \u4e00\u822c\u7b97\u672f\u8f6c\u6362\u89c4\u5219\u975e\u5e38\u7b80\u5355\u3002\u7f16\u8bd1\u5668\u5177\u6709\u4e00\u4e2a\u6309\u7167\u4f18\u5148\u7ea7\u6392\u5e8f\u7684\u7c7b\u578b\u5217\u8868\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a long double (\u6700\u9ad8\u4f18\u5148\u7ea7) double float unsigned long long long long unsigned long long unsigned int int (\u6700\u4f4e\u4f18\u5148\u7ea7) \u4ee5\u53ca\u4e24\u6761\u89c4\u5219\uff1a \u5982\u679c\u81f3\u5c11\u6709\u4e00\u4e2a\u64cd\u4f5c\u6570\u5728\u4f18\u5148\u7ea7\u5217\u8868\u4e2d\uff0c\u5219\u5177\u6709\u8f83\u4f4e\u4f18\u5148\u7ea7\u7684\u64cd\u4f5c\u6570\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5177\u6709\u8f83\u9ad8\u4f18\u5148\u7ea7\u7684\u64cd\u4f5c\u6570\uff1b \u5426\u5219 (\u4e24\u4e2a\u64cd\u4f5c\u6570\u7684\u7c7b\u578b\u5747\u4e0d\u5728\u8868\u4e2d)\uff0c\u5219\u4e24\u4e2a\u64cd\u4f5c\u6570\u4f1a\u8fdb\u884c \u6570\u503c\u63d0\u5347 (\u53c2\u89c1\uff1a 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 )\u3002 \u4e00\u4e9b\u4f8b\u5b50 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528 typeid \u8fd0\u7b97\u7b26 (\u5728 \u5934\u6587\u4ef6\u4e2d)\uff0c\u6765\u663e\u793a\u8868\u8fbe\u5f0f\u7ed3\u679c\u6240\u5c5e\u7684\u7c7b\u578b\u3002 \u9996\u5148\uff0c\u5c06 int \u548c double \u76f8\u52a0\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include #include // for typeid() int main () { int i { 2 }; double d { 3.5 }; std :: cout << typeid ( i + d ). name () << ' ' << i + d << '\\n' ; // \u663e\u793a i + d \u7684\u7c7b\u578b return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c double \u7c7b\u578b\u64cd\u4f5c\u6570\u7684\u4f18\u5148\u7ea7\u6700\u9ad8\uff0c\u6240\u4ee5\u4f18\u5148\u7ea7\u8f83\u4f4e\u7684\u64cd\u4f5c\u6570 (\u4f8b\u5982 int ) \u7684\u7c7b\u578b\u4f1a\u88ab\u8f6c\u6362\u4e3a double \u503c 2.0 . Then double values 2.0 and 3.5 are added to produce double result 5.5 . \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u4fe1\u606f\uff1a 1 double 5.5 \u6ce8\u610f\uff0c\u4f60\u7684\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u4ea7\u751f\u7a0d\u5fae\u4e0d\u540c\u7684\u7ed3\u679c\uff0c\u56e0\u4e3a typeid.name() \u8f93\u51fa\u7684\u7ed3\u679c\u662f\u7531\u7f16\u8bd1\u5668\u51b3\u5b9a\u7684\u3002 \u8bd1\u8005\u6ce8 \u5728g++\u4e0a\u4f7f\u7528 typeid \u8fd0\u7b97\u7b26 \uff0c\u8f93\u51fa\u7684\u7ed3\u679c\u4e3a \u4fee\u9970\u540d \uff0c\u53ef\u4ee5\u4f7f\u7528 1 a.out | c++filt --types \u5f97\u5230\u53ef\u8bfb\u7684\u7ed3\u679c\u3002 \u53c2\u8003 \u73b0\u5728\uff0c\u5c06\u4e24\u4e2a short \u7c7b\u578b\u7684\u503c\u76f8\u52a0\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include #include // for typeid() int main () { short a { 4 }; short b { 5 }; std :: cout << typeid ( a + b ). name () << ' ' << a + b << '\\n' ; // \u663e\u793a a + b \u7684\u7c7b\u578b return 0 ; } \u56e0\u4e3a\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u6ca1\u6709\u51fa\u73b0\u5728\u4f18\u5148\u7ea7\u5217\u8868\u4e2d\uff0c\u6240\u4ee5\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u88ab\u6574\u578b\u63d0\u5347\u4e3a int \u7c7b\u578b\u3002\u4e24\u4e2a int \u76f8\u52a0\u7684\u7ed3\u679c\u662f\u4e00\u4e2a int \uff0c\u6b63\u5982\u4f60\u6240\u671f\u671b\u7684\u90a3\u6837: 1 int 9 \u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u95ee\u9898 \u5f53\u6df7\u5408\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u503c\u65f6\uff0c\u8fd9\u79cd\u4f18\u5148\u7ea7\u5c42\u6b21\u7ed3\u6784\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u770b\u770b\u4e0b\u9762\u7684\u4ee3\u7801: 1 2 3 4 5 6 7 8 9 #include #include // for typeid() int main () { std :: cout << typeid ( 5u-10 ). name () << ' ' << 5u - 10 ; // 5u \u5373 5 \u88ab\u5f53\u505a\u65e0\u7b26\u53f7\u6574\u578b\u5904\u7406 return 0 ; } \u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u8868\u8fbe\u5f0f 5u - 10 \u6c42\u503c\u4f1a\u7b49\u4e8e -5 \uff0c\u56e0\u4e3a 5 - 10 = -5 \u3002\u4f46\u5b9e\u9645\u7ed3\u679c\u5374\u4e0d\u662f\u8fd9\u6837\u7684\uff1a 1 unsigned int 4294967291 \u56e0\u4e3a unsigned int \u64cd\u4f5c\u6570\u5177\u6709\u66f4\u9ad8\u7684\u4f18\u5148\u7ea7\uff0c\u6240\u4ee5 int \u64cd\u4f5c\u6570\u88ab\u8f6c\u6362\u6210\u4e86 unsigned int \u3002\u800c\u4e14 -5 \u8d85\u8fc7\u4e86 unsigned int \u80fd\u591f\u8868\u793a\u7684\u8303\u56f4\uff0c\u6240\u4ee5\u7ed3\u679c\u624d\u4f1a\u51fa\u4e4e\u6211\u4eec\u7684\u610f\u6599\u3002 \u4e0b\u9762\u662f\u53e6\u4e00\u4e2a\u53cd\u76f4\u89c9\u7ed3\u679c\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << std :: boolalpha << ( -3 < 5u ); return 0 ; } \u5c3d\u7ba1\uff0c\u5f88\u663e\u7136 5 \u662f\u6bd4 -3 \u5927\u7684\u3002\u5f53\u8868\u8fbe\u5f0f\u6c42\u503c\u65f6\uff0c -3 \u4f1a\u88ab\u8f6c\u6362\u4e3a\u4e00\u4e2a unsigned int \uff08\u53cd\u8f6c\u6210\u4e00\u4e2a\u5f88\u5927\u7684\u6570\uff09\uff0c\u8fd9\u4e2a\u6570\u5927\u4e8e 5 \u3002\u56e0\u6b64\uff0c\u4e0a\u9762\u7684\u8868\u8fbe\u5f0f\u4f1a\u8f93\u51fa false \u800c\u4e0d\u662f true \u3002 \u8fd9\u4e5f\u662f\u907f\u514d\u65e0\u7b26\u53f7\u6574\u6570\u7684\u4e3b\u8981\u539f\u56e0\u4e4b\u4e00\u2014\u2014\u5f53\u5728\u7b97\u672f\u8868\u8fbe\u5f0f\u4e2d\u5c06\u5b83\u4eec\u4e0e\u6709\u7b26\u53f7\u6574\u6570\u6df7\u5408\u4f7f\u7528\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u610f\u5916\u7ed3\u679c\u3002\u7f16\u8bd1\u5668\u751a\u81f3\u53ef\u80fd\u4e0d\u4f1a\u53d1\u51fa\u8b66\u544a\u3002","title":"8.4 - \u7b97\u672f\u8f6c\u6362"},{"location":"8-4-Arithmetic-conversions/#_1","text":"\u4e0b\u5217\u8fd0\u7b97\u7b26\u9700\u8981\u64cd\u4f5c\u6570\u5177\u6709\u76f8\u540c\u7c7b\u578b\uff1a \u4e8c\u5143\u7b97\u672f\u8fd0\u7b97\u7b26\uff1a + , - , * , / , % \u4e8c\u5143\u5173\u7cfb\u8fd0\u7b97\u7b26\uff1a < , > , <= , >= , == , != \u4e8c\u5143\u6309\u4f4d\u8fd0\u7b97\u7b26\uff1a & , ^ , | \u6761\u4ef6\u8fd0\u7b97\u7b26 ?: (\u5305\u62ec\u6761\u4ef6\uff0c\u8981\u6c42\u4e3a\u5e03\u5c14\u7c7b\u578b)","title":"\u9700\u8981\u64cd\u4f5c\u6570\u5177\u6709\u76f8\u540c\u7c7b\u578b\u7684\u8fd0\u7b97\u7b26"},{"location":"8-4-Arithmetic-conversions/#_2","text":"\u4e00\u822c\u7b97\u672f\u8f6c\u6362\u89c4\u5219\u975e\u5e38\u7b80\u5355\u3002\u7f16\u8bd1\u5668\u5177\u6709\u4e00\u4e2a\u6309\u7167\u4f18\u5148\u7ea7\u6392\u5e8f\u7684\u7c7b\u578b\u5217\u8868\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a long double (\u6700\u9ad8\u4f18\u5148\u7ea7) double float unsigned long long long long unsigned long long unsigned int int (\u6700\u4f4e\u4f18\u5148\u7ea7) \u4ee5\u53ca\u4e24\u6761\u89c4\u5219\uff1a \u5982\u679c\u81f3\u5c11\u6709\u4e00\u4e2a\u64cd\u4f5c\u6570\u5728\u4f18\u5148\u7ea7\u5217\u8868\u4e2d\uff0c\u5219\u5177\u6709\u8f83\u4f4e\u4f18\u5148\u7ea7\u7684\u64cd\u4f5c\u6570\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5177\u6709\u8f83\u9ad8\u4f18\u5148\u7ea7\u7684\u64cd\u4f5c\u6570\uff1b \u5426\u5219 (\u4e24\u4e2a\u64cd\u4f5c\u6570\u7684\u7c7b\u578b\u5747\u4e0d\u5728\u8868\u4e2d)\uff0c\u5219\u4e24\u4e2a\u64cd\u4f5c\u6570\u4f1a\u8fdb\u884c \u6570\u503c\u63d0\u5347 (\u53c2\u89c1\uff1a 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 )\u3002","title":"\u4e00\u822c\u7b97\u672f\u8f6c\u6362\u89c4\u5219"},{"location":"8-4-Arithmetic-conversions/#_3","text":"\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528 typeid \u8fd0\u7b97\u7b26 (\u5728 \u5934\u6587\u4ef6\u4e2d)\uff0c\u6765\u663e\u793a\u8868\u8fbe\u5f0f\u7ed3\u679c\u6240\u5c5e\u7684\u7c7b\u578b\u3002 \u9996\u5148\uff0c\u5c06 int \u548c double \u76f8\u52a0\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include #include // for typeid() int main () { int i { 2 }; double d { 3.5 }; std :: cout << typeid ( i + d ). name () << ' ' << i + d << '\\n' ; // \u663e\u793a i + d \u7684\u7c7b\u578b return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c double \u7c7b\u578b\u64cd\u4f5c\u6570\u7684\u4f18\u5148\u7ea7\u6700\u9ad8\uff0c\u6240\u4ee5\u4f18\u5148\u7ea7\u8f83\u4f4e\u7684\u64cd\u4f5c\u6570 (\u4f8b\u5982 int ) \u7684\u7c7b\u578b\u4f1a\u88ab\u8f6c\u6362\u4e3a double \u503c 2.0 . Then double values 2.0 and 3.5 are added to produce double result 5.5 . \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u4fe1\u606f\uff1a 1 double 5.5 \u6ce8\u610f\uff0c\u4f60\u7684\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u4ea7\u751f\u7a0d\u5fae\u4e0d\u540c\u7684\u7ed3\u679c\uff0c\u56e0\u4e3a typeid.name() \u8f93\u51fa\u7684\u7ed3\u679c\u662f\u7531\u7f16\u8bd1\u5668\u51b3\u5b9a\u7684\u3002 \u8bd1\u8005\u6ce8 \u5728g++\u4e0a\u4f7f\u7528 typeid \u8fd0\u7b97\u7b26 \uff0c\u8f93\u51fa\u7684\u7ed3\u679c\u4e3a \u4fee\u9970\u540d \uff0c\u53ef\u4ee5\u4f7f\u7528 1 a.out | c++filt --types \u5f97\u5230\u53ef\u8bfb\u7684\u7ed3\u679c\u3002 \u53c2\u8003 \u73b0\u5728\uff0c\u5c06\u4e24\u4e2a short \u7c7b\u578b\u7684\u503c\u76f8\u52a0\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include #include // for typeid() int main () { short a { 4 }; short b { 5 }; std :: cout << typeid ( a + b ). name () << ' ' << a + b << '\\n' ; // \u663e\u793a a + b \u7684\u7c7b\u578b return 0 ; } \u56e0\u4e3a\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u6ca1\u6709\u51fa\u73b0\u5728\u4f18\u5148\u7ea7\u5217\u8868\u4e2d\uff0c\u6240\u4ee5\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u88ab\u6574\u578b\u63d0\u5347\u4e3a int \u7c7b\u578b\u3002\u4e24\u4e2a int \u76f8\u52a0\u7684\u7ed3\u679c\u662f\u4e00\u4e2a int \uff0c\u6b63\u5982\u4f60\u6240\u671f\u671b\u7684\u90a3\u6837: 1 int 9","title":"\u4e00\u4e9b\u4f8b\u5b50"},{"location":"8-4-Arithmetic-conversions/#_4","text":"\u5f53\u6df7\u5408\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u503c\u65f6\uff0c\u8fd9\u79cd\u4f18\u5148\u7ea7\u5c42\u6b21\u7ed3\u6784\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u770b\u770b\u4e0b\u9762\u7684\u4ee3\u7801: 1 2 3 4 5 6 7 8 9 #include #include // for typeid() int main () { std :: cout << typeid ( 5u-10 ). name () << ' ' << 5u - 10 ; // 5u \u5373 5 \u88ab\u5f53\u505a\u65e0\u7b26\u53f7\u6574\u578b\u5904\u7406 return 0 ; } \u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u8868\u8fbe\u5f0f 5u - 10 \u6c42\u503c\u4f1a\u7b49\u4e8e -5 \uff0c\u56e0\u4e3a 5 - 10 = -5 \u3002\u4f46\u5b9e\u9645\u7ed3\u679c\u5374\u4e0d\u662f\u8fd9\u6837\u7684\uff1a 1 unsigned int 4294967291 \u56e0\u4e3a unsigned int \u64cd\u4f5c\u6570\u5177\u6709\u66f4\u9ad8\u7684\u4f18\u5148\u7ea7\uff0c\u6240\u4ee5 int \u64cd\u4f5c\u6570\u88ab\u8f6c\u6362\u6210\u4e86 unsigned int \u3002\u800c\u4e14 -5 \u8d85\u8fc7\u4e86 unsigned int \u80fd\u591f\u8868\u793a\u7684\u8303\u56f4\uff0c\u6240\u4ee5\u7ed3\u679c\u624d\u4f1a\u51fa\u4e4e\u6211\u4eec\u7684\u610f\u6599\u3002 \u4e0b\u9762\u662f\u53e6\u4e00\u4e2a\u53cd\u76f4\u89c9\u7ed3\u679c\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << std :: boolalpha << ( -3 < 5u ); return 0 ; } \u5c3d\u7ba1\uff0c\u5f88\u663e\u7136 5 \u662f\u6bd4 -3 \u5927\u7684\u3002\u5f53\u8868\u8fbe\u5f0f\u6c42\u503c\u65f6\uff0c -3 \u4f1a\u88ab\u8f6c\u6362\u4e3a\u4e00\u4e2a unsigned int \uff08\u53cd\u8f6c\u6210\u4e00\u4e2a\u5f88\u5927\u7684\u6570\uff09\uff0c\u8fd9\u4e2a\u6570\u5927\u4e8e 5 \u3002\u56e0\u6b64\uff0c\u4e0a\u9762\u7684\u8868\u8fbe\u5f0f\u4f1a\u8f93\u51fa false \u800c\u4e0d\u662f true \u3002 \u8fd9\u4e5f\u662f\u907f\u514d\u65e0\u7b26\u53f7\u6574\u6570\u7684\u4e3b\u8981\u539f\u56e0\u4e4b\u4e00\u2014\u2014\u5f53\u5728\u7b97\u672f\u8868\u8fbe\u5f0f\u4e2d\u5c06\u5b83\u4eec\u4e0e\u6709\u7b26\u53f7\u6574\u6570\u6df7\u5408\u4f7f\u7528\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u610f\u5916\u7ed3\u679c\u3002\u7f16\u8bd1\u5668\u751a\u81f3\u53ef\u80fd\u4e0d\u4f1a\u53d1\u51fa\u8b66\u544a\u3002","title":"\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u95ee\u9898"},{"location":"8-5-Explicit-type-conversion-casting-and-static-cast/","tags":["explicit-type-conversion","conversions","static-cast"],"text":"Key Takeaway C++\u652f\u6301 5 \u79cd\u7c7b\u578b\u7684\u663e\u793a\u7c7b\u578b\u8f6c\u6362: C\u98ce\u683c\u7c7b\u578b\u8f6c\u6362 \u3001 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u3001 const \u7c7b\u578b\u8f6c\u6362 \u3001 \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u548c \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 \u3002\u540e\u56db\u79cd\u7c7b\u578b\u6709\u65f6\u79f0\u4e3a \u5177\u540d\u540d\u7c7b\u578b\u8f6c\u6362(named cast) \u3002 C \u8bed\u8a00\u7c7b\u578b\u7684\u7c7b\u578b\u8f6c\u6362\u5728\u4e0d\u540c\u7684\u8bed\u5883\u4e0b\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u6548\u679c\uff0c\u5c3d\u91cf\u4e0d\u8981\u4f7f\u7528 \u4f18\u5148\u4f7f\u7528 static_cast \uff0c\u5b83\u63d0\u4f9b\u4e86 \u8fd0\u884c\u65f6 \u7684\u7c7b\u578b\u68c0\u67e5\u673a\u5236\uff0c\u4e0d\u5bb9\u6613\u72af\u9519\u3002 \u4f7f\u7528 static_cast \u8fdb\u884c\u663e\u5f0f\u5730\u7f29\u7a84\u8f6c\u6362 \u5728 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u9690\u5f0f\u5730\u5c06\u4e00\u79cd\u7c7b\u578b\u7684\u503c\u8f6c\u6362\u6210\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\uff0c\u5373 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u3002\u5f53\u4f60\u60f3\u8981\u5c06\u4e00\u4e2a\u6570\u503c\u7c7b\u578b\u901a\u8fc7 \u6570\u503c\u63d0\u5347 \u7684\u65b9\u5f0f\u8f6c\u6362\u4e3a\u66f4\u5bbd\u7684\u7c7b\u578b\u65f6\uff0c\u4f7f\u7528\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u662f\u53ef\u4ee5\u7684\u3002 \u5f88\u591a\u65b0\u624b C++ \u7a0b\u5e8f\u5458\u4f1a\u8fd9\u6837\u505a\uff1a 1 double d = 10 / 4 ; // \u901a\u8fc7\u6574\u578b\u4e58\u6cd5\uff0c\u5c06\u503c\u521d\u59cb\u5316\u4e3a 2.0 \u56e0\u4e3a 10 \u548c 4 \u90fd\u662f int \u7c7b\u578b\uff0c\u6240\u4ee5\u6267\u884c\u7684\u662f\u6574\u578b\u9664\u6cd5\uff0c\u8868\u8fbe\u5f0f\u7684\u6c42\u503c\u7ed3\u679c\u4e3a int \u503c 2 \u3002\u7136\u540e\uff0c\u8be5\u503c\u5728\u88ab\u7528\u4e8e\u521d\u59cb\u5316\u53d8\u91cf d \u4e4b\u524d\u88ab\u8f6c\u6362\u6210\u4e86 double \u7c7b\u578b\u7684 2.0 \u3002 \u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u5458\u5e76\u4e0d\u4f1a\u6545\u610f\u8fd9\u4e48\u505a\u3002 \u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u5b57\u9762\u91cf\uff0c\u5c06\u4e0a\u9762\u7684 int \u578b\u64cd\u4f5c\u6570\u66ff\u6362\u6210\u4e86 double \u64cd\u4f5c\u6570\uff0c\u8fd9\u6837\u4e00\u6765\u5c31\u4f1a\u8fdb\u884c\u6d6e\u70b9\u6570\u9664\u6cd5\uff1a 1 double d = 10.0 / 4.0 ; // does floating point division, initializes d with value 2.5 \u4f46\u662f\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u53d8\u91cf\u800c\u975e\u5b57\u9762\u91cf\u5462\uff1f\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 int x { 10 }; int y { 4 }; double d = x / y ; // does integer division, initializes d with value 2.0 \u56e0\u4e3a\u6267\u884c\u4e86\u6574\u578b\u9664\u6cd5\uff0c\u6240\u4ee5\u53d8\u91cf d \u7684\u503c\u6700\u7ec8\u4e3a 2.0 \u3002\u90a3\u4e48\u6211\u4eec\u5e94\u8be5\u5982\u4f55\u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u8fd9\u91cc\u9700\u8981\u4f7f\u7528\u6d6e\u70b9\u6570\u9664\u6cd5\u5462\uff1f\u5b57\u9762\u91cf\u540e\u7f00\u5e76\u4e0d\u80fd\u88ab\u7528\u5728\u53d8\u91cf\u4e0a\u3002\u56e0\u6b64\uff0c\u9700\u8981\u4e00\u79cd\u80fd\u591f\u5c06\u53d8\u91cf\u8f6c\u6362\u4e3a\u6d6e\u70b9\u7c7b\u578b\u7684\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u4f7f\u7528\u6d6e\u70b9\u6570\u9664\u6cd5\u3002 \u5e78\u8fd0\u7684\u662f\uff0cC++\u63d0\u4f9b\u4e86\u8bb8\u591a\u4e0d\u540c\u7684\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26(\u901a\u5e38\u79f0\u4e3a\u7c7b\u578b\u8f6c\u6362)\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u8bf7\u6c42\u7f16\u8bd1\u5668\u6267\u884c\u7c7b\u578b\u8f6c\u6362\u3002\u56e0\u4e3a\u7c7b\u578b\u8f6c\u6362\u662f\u7a0b\u5e8f\u5458\u7684\u663e\u5f0f\u8bf7\u6c42\uff0c\u6240\u4ee5\u8fd9\u79cd\u5f62\u5f0f\u7684\u7c7b\u578b\u8f6c\u6362\u901a\u5e38\u79f0\u4e3a \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 (\u4e0e\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u76f8\u53cd\uff0c\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u662f\u7f16\u8bd1\u5668\u81ea\u52a8\u6267\u884c\u7684\u7c7b\u578b\u8f6c\u6362)\u3002 \u7c7b\u578b\u8f6c\u6362 C++\u652f\u6301 5 \u79cd\u7c7b\u578b\u7684\u663e\u793a\u7c7b\u578b\u8f6c\u6362: C\u98ce\u683c\u7c7b\u578b\u8f6c\u6362 \u3001 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u3001 const \u7c7b\u578b\u8f6c\u6362 \u3001 \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u548c \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 \u3002\u540e\u56db\u79cd\u7c7b\u578b\u6709\u65f6\u79f0\u4e3a \u5177\u540d\u540d\u7c7b\u578b\u8f6c\u6362(named cast) \u3002 \u5728\u672c\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd C\u98ce\u683c\u7c7b\u578b\u8f6c\u6362 \u548c \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 18.10 -- Dynamic casting \u4e2d\u4ecb\u7ecd\u52a8\u6001\u7c7b\u578b\u8f6c\u6362\uff0c\u4f46\u662f\u6211\u4eec\u9996\u5148\u8981\u4ecb\u7ecd\u4e00\u4e9b\u524d\u7f6e\u5185\u5bb9\u3002 \u901a\u5e38\u5e94\u8be5\u907f\u514d\u4f7f\u7528 const \u7c7b\u578b\u8f6c\u6362 \u548c \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 \uff0c\u56e0\u4e3a\u53ea\u6709\u5f88\u5c11\u7684\u60c5\u51b5\u4e0b\u4f1a\u9700\u8981\u4f7f\u7528\u5b83\u4eec\uff0c\u800c\u4e14\u4f7f\u7528\u4e0d\u5f53\u662f\u975e\u5e38\u6709\u5bb3\u7684\u3002 \u6ce8\u610f \u9664\u975e\u4f60\u6709\u5145\u5206\u7684\u7406\u7531\uff0c\u5426\u5219\u8bf7\u907f\u514d\u4f7f\u7528 const \u7c7b\u578b\u8f6c\u6362 \u548c \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 C\u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362 \u5728\u6807\u51c6\u7684C\u8bed\u8a00\u4e2d\uff0c\u5f3a\u5236\u8f6c\u6362\u662f\u901a\u8fc7 () \u8fd0\u7b97\u7b26\u5b8c\u6210\u7684\uff0c\u62ec\u53f7\u5185\u4e3a\u9700\u8981\u8f6c\u6362\u7684\u7c7b\u578b\u540d\u3002\u5728C++\u4e2d\uff0c\u4f60\u4ecd\u7136\u53ef\u4ee5\u5728\u7531C\u8bed\u8a00\u8f6c\u6362\u800c\u6765\u7684\u4ee3\u7801\u4e2d\u770b\u5230\u5b83\u4eec\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { int x { 10 }; int y { 4 }; double d { ( double ) x / y }; // convert x to a double so we get floating point division std :: cout << d ; // prints 2.5 return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u8981\u6c42\u7f16\u8bd1\u5668\u5c06 x \u8f6c\u6362\u4e3a double \u3002\u56e0\u4e3a\uff0c / \u5de6\u4fa7\u7684\u64cd\u4f5c\u6570\u88ab\u8f6c\u6362\u6210\u4e86\u6d6e\u70b9\u6570\uff0c\u6240\u4ee5\u64cd\u4f5c\u7b26\u53f3\u4fa7\u7684\u6570\u540c\u6837\u4e5f\u4f1a\u88ab\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff08 \u6570\u503c\u8f6c\u6362 \uff09\uff0c\u7136\u540e\u8868\u8fbe\u5f0f\u5c31\u4f1a\u6309\u7167\u6d6e\u70b9\u6570\u9664\u6cd5\u800c\u4e0d\u662f\u6574\u578b\u9664\u6cd5\u6c42\u503c\uff01 C++ \u5141\u8bb8\u4f60\u4f7f\u7528\u4e00\u79cd\u66f4\u52a0\u7c7b\u4f3c\u51fd\u6570\u8c03\u7528\u7684\u8bed\u6cd5\u6765\u4f7f\u7528C\u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362\uff1a 1 double d { double ( x ) / y }; // convert x to a double so we get floating point division \u8fd9\u79cd\u65b9\u5f0f\u5b9e\u73b0\u7684\u7c7b\u578b\u8f6c\u6362\u548c\u4e4b\u524d\u4e00\u79cd\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u4f46\u662f\u5c06\u9700\u8981\u8f6c\u6362\u7684\u53d8\u91cf\u653e\u5728\u62ec\u53f7\u4e86\uff0c\u66f4\u5bb9\u6613\u770b\u6e05\u695a\u88ab\u8f6c\u6362\u7684\u5bf9\u8c61\u662f\u4ec0\u4e48\u3002 \u867d\u7136\u201cc\u98ce\u683c\u7c7b\u578b\u8f6c\u6362\u201d\u770b\u8d77\u6765\u662f\u5355\u4e00\u7c7b\u578b\u8f6c\u6362\uff0c\u4f46\u5b9e\u9645\u4e0a\u5b83\u53ef\u4ee5\u6839\u636e\u4e0a\u4e0b\u6587\u6267\u884c\u5404\u79cd\u4e0d\u540c\u7684\u8f6c\u6362\u3002\u8fd9\u53ef\u4ee5\u5305\u62ec\u201c\u9759\u6001\u7c7b\u578b\u8f6c\u6362\u201d\u3001\u201cconst\u7c7b\u578b\u8f6c\u6362\u201d\u6216\u201c\u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362\u201d(\u6211\u4eec\u5728\u4e0a\u9762\u63d0\u5230\u7684\u540e\u4e24\u79cd\u7c7b\u578b\u5e94\u8be5\u907f\u514d)\u3002\u56e0\u6b64\uff0c\u201cC\u98ce\u683c\u5f3a\u5236\u8f6c\u6362\u201d\u6709\u53ef\u80fd\u88ab\u65e0\u610f\u4e2d\u8bef\u7528\uff0c\u800c\u4e0d\u4f1a\u4ea7\u751f\u9884\u671f\u7684\u884c\u4e3a\uff0c\u800c\u4f7f\u7528c++\u5f3a\u5236\u8f6c\u6362\u5219\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002 \u76f8\u5173\u5185\u5bb9 \u5982\u679c\u4f60\u5f88\u597d\u5947\uff0cC\u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u53ef\u4ee5\u53c2\u8003 \u8fd9\u7bc7\u6587\u7ae0 \u3002 \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u4f7f\u7528C\u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362\u3002 static_cast C++ \u5f15\u5165\u4e86\u4e00\u4e2a\u65b0\u7684\u5f3a\u5236\u8f6c\u6362\u8fd0\u7b97\u7b26 static_cast \uff0c\u7528\u4e8e\u5c06\u4e00\u79cd\u7c7b\u578b\u7684\u503c\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\u3002 \u5728\u4e4b\u524d\u7684\u8bfe\u4e2d\uff0c\u4f60\u53ef\u80fd\u5df2\u7ecf\u89c1\u8bc6\u8fc7\u5982\u4f55\u4f7f\u7528 static_cast \u5c06 char \u8f6c\u6362\u4e3a int \u4f7f\u5f97 std::cout \u53ef\u4ee5\u6253\u5370\u6574\u578b\u800c\u4e0d\u662f char \uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char c { 'a' }; std :: cout << c << ' ' << static_cast < int > ( c ) << '\\n' ; // prints a 97 return 0 ; } static_cast \u8fd0\u7b97\u7b26\u5c06\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u8f93\u5165\uff0c\u7136\u540e\u5c06\u8868\u8fbe\u5f0f\u6c42\u503c\u7684\u7ed3\u679c\u8f6c\u6362\u4e3a \u5c16\u62ec\u53f7 \u4e2d\u6307\u5b9a\u7684\u7c7b\u578b\u3002 static_cast \u662f\u5c06\u4e00\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\u7684\u6700\u4f73\u9014\u5f84\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { int x { 10 }; int y { 4 }; // static cast x to a double so we get floating point division double d { static_cast < double > ( x ) / y }; std :: cout << d ; // prints 2.5 return 0 ; } static_cast \u6700\u5927\u7684\u4f18\u52bf\u662f\u5b83\u63d0\u4f9b\u4e86 \u8fd0\u884c\u65f6 \u7684\u7c7b\u578b\u68c0\u67e5\u673a\u5236\uff0c\u8fd9\u6837\u5c31\u4e0d\u5bb9\u6613\u72af\u4e0b\u7531\u7c97\u5fc3\u5bfc\u81f4\u7684\u95ee\u9898\u3002 static_cast \u7684\u529f\u80fd\u4e0d\u5982 C \u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362\uff08\u6545\u610f\u7684\uff09\u5f3a\u5927\uff0c\u6240\u4ee5\u4f60\u4e0d\u4f1a\u65e0\u610f\u95f4\u79fb\u9664 const \u6216\u5176\u4ed6\u4f60\u672c\u4e0d\u5e0c\u671b\u53d1\u751f\u7684\u4e8b\u60c5\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u4f18\u5148\u4f7f\u7528 static_cast \u3002 \u4f7f\u7528 static_cast \u8fdb\u884c\u663e\u5f0f\u5730\u7f29\u7a84\u8f6c\u6362 \u5f53\u6211\u4eec\u8fdb\u884c\u5177\u6709\u6f5c\u5728\u5371\u9669\u7684\uff08\u7f29\u7a84\uff09\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u53d1\u51fa\u544a\u8b66\u3002\u4f8b\u5982\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a 1 2 int i { 48 }; char ch = i ; // \u9690\u5f0f\u5730\u7f29\u7a84\u8f6c\u6362 \u5c06 int (2 \u5b57\u8282\u6216\u8005 4 \u5b57\u8282) \u8f6c\u6362\u4e3a char (1 \u5b57\u8282)\u901a\u5e38\u662f\u4e0d\u5b89\u5168\u7684\uff08\u56e0\u4e3a\u7f16\u8bd1\u5668\u65e0\u6cd5\u5224\u65ad\u6574\u578b\u503c\u662f\u5426\u4f1a\u8d85\u51fa char \u80fd\u591f\u8868\u793a\u7684\u8303\u56f4\uff09\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u4ea7\u751f\u8b66\u544a\u3002\u800c\u5982\u679c\u6211\u4eec\u4f7f\u7528\u7684\u662f \u5217\u8868\u521d\u59cb\u5316 \uff08 \u62ec\u53f7\u521d\u59cb\u5316 \uff09\uff0c\u7f16\u8bd1\u5668\u5219\u901a\u5e38\u4f1a\u4ea7\u751f\u4e00\u4e2a\u7f16\u8bd1\u9519\u8bef\u3002(\u53c2\u89c1\uff1a \u62ec\u53f7\u521d\u59cb\u5316\u4e0d\u5141\u8bb8\u9690\u5f0f\u7f29\u7a84\u8f6c\u6362 ) \u4e3a\u4e86\u907f\u514d\u8fd9\u4e9b\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 static_cast \u663e\u5f0f\u5730\u5c06\u6574\u578b\u8f6c\u6362\u4e3a char \uff1a 1 2 3 4 int i { 48 }; // explicit conversion from int to char, so that a char is assigned to variable ch char ch { static_cast < char > ( i ) }; \u8fd9\u6837\u505a\u65f6\uff0c\u6211\u4eec\u663e\u5f0f\u5730\u544a\u8bc9\u7f16\u8bd1\u5668\u8fd9\u4e2a\u8f6c\u6362\u662f\u6709\u610f\u7684\uff0c\u540e\u679c\u81ea\u8d1f(\u4f8b\u5982\uff0c\u6ea2\u51fa char \u7684\u8303\u56f4)\u3002\u7531\u4e8e\u8fd9\u4e2a static_cast \u7684\u8f93\u51fa\u7c7b\u578b\u4e3a char \uff0c\u53d8\u91cf ch \u7684\u521d\u59cb\u5316\u7684\u7c7b\u578b\u662f\u5339\u914d\u7684\uff0c\u56e0\u6b64\u4e0d\u4f1a\u4ea7\u751f\u8b66\u544a\u6216\u9519\u8bef\u3002 \u4e0b\u9762\u662f\u53e6\u4e00\u4e2a\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u62b1\u6028\u5c06 double \u8f6c\u6362\u4e3a int \u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u4e22\u5931\u7684\u4f8b\u5b50: 1 2 int i { 100 }; i = i / 2.5 ; \u544a\u8bc9\u7f16\u8bd1\u5668\u4e0a\u8ff0\u8f6c\u6362\u662f\u6709\u610f\u800c\u4e3a\u4e4b\u7684\uff1a 1 2 int i { 100 }; i = static_cast < int > ( i / 2.5 );","title":"8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast"},{"location":"8-5-Explicit-type-conversion-casting-and-static-cast/#_1","text":"C++\u652f\u6301 5 \u79cd\u7c7b\u578b\u7684\u663e\u793a\u7c7b\u578b\u8f6c\u6362: C\u98ce\u683c\u7c7b\u578b\u8f6c\u6362 \u3001 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u3001 const \u7c7b\u578b\u8f6c\u6362 \u3001 \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u548c \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 \u3002\u540e\u56db\u79cd\u7c7b\u578b\u6709\u65f6\u79f0\u4e3a \u5177\u540d\u540d\u7c7b\u578b\u8f6c\u6362(named cast) \u3002 \u5728\u672c\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd C\u98ce\u683c\u7c7b\u578b\u8f6c\u6362 \u548c \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 18.10 -- Dynamic casting \u4e2d\u4ecb\u7ecd\u52a8\u6001\u7c7b\u578b\u8f6c\u6362\uff0c\u4f46\u662f\u6211\u4eec\u9996\u5148\u8981\u4ecb\u7ecd\u4e00\u4e9b\u524d\u7f6e\u5185\u5bb9\u3002 \u901a\u5e38\u5e94\u8be5\u907f\u514d\u4f7f\u7528 const \u7c7b\u578b\u8f6c\u6362 \u548c \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 \uff0c\u56e0\u4e3a\u53ea\u6709\u5f88\u5c11\u7684\u60c5\u51b5\u4e0b\u4f1a\u9700\u8981\u4f7f\u7528\u5b83\u4eec\uff0c\u800c\u4e14\u4f7f\u7528\u4e0d\u5f53\u662f\u975e\u5e38\u6709\u5bb3\u7684\u3002 \u6ce8\u610f \u9664\u975e\u4f60\u6709\u5145\u5206\u7684\u7406\u7531\uff0c\u5426\u5219\u8bf7\u907f\u514d\u4f7f\u7528 const \u7c7b\u578b\u8f6c\u6362 \u548c \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362","title":"\u7c7b\u578b\u8f6c\u6362"},{"location":"8-5-Explicit-type-conversion-casting-and-static-cast/#c","text":"\u5728\u6807\u51c6\u7684C\u8bed\u8a00\u4e2d\uff0c\u5f3a\u5236\u8f6c\u6362\u662f\u901a\u8fc7 () \u8fd0\u7b97\u7b26\u5b8c\u6210\u7684\uff0c\u62ec\u53f7\u5185\u4e3a\u9700\u8981\u8f6c\u6362\u7684\u7c7b\u578b\u540d\u3002\u5728C++\u4e2d\uff0c\u4f60\u4ecd\u7136\u53ef\u4ee5\u5728\u7531C\u8bed\u8a00\u8f6c\u6362\u800c\u6765\u7684\u4ee3\u7801\u4e2d\u770b\u5230\u5b83\u4eec\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { int x { 10 }; int y { 4 }; double d { ( double ) x / y }; // convert x to a double so we get floating point division std :: cout << d ; // prints 2.5 return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u8981\u6c42\u7f16\u8bd1\u5668\u5c06 x \u8f6c\u6362\u4e3a double \u3002\u56e0\u4e3a\uff0c / \u5de6\u4fa7\u7684\u64cd\u4f5c\u6570\u88ab\u8f6c\u6362\u6210\u4e86\u6d6e\u70b9\u6570\uff0c\u6240\u4ee5\u64cd\u4f5c\u7b26\u53f3\u4fa7\u7684\u6570\u540c\u6837\u4e5f\u4f1a\u88ab\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff08 \u6570\u503c\u8f6c\u6362 \uff09\uff0c\u7136\u540e\u8868\u8fbe\u5f0f\u5c31\u4f1a\u6309\u7167\u6d6e\u70b9\u6570\u9664\u6cd5\u800c\u4e0d\u662f\u6574\u578b\u9664\u6cd5\u6c42\u503c\uff01 C++ \u5141\u8bb8\u4f60\u4f7f\u7528\u4e00\u79cd\u66f4\u52a0\u7c7b\u4f3c\u51fd\u6570\u8c03\u7528\u7684\u8bed\u6cd5\u6765\u4f7f\u7528C\u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362\uff1a 1 double d { double ( x ) / y }; // convert x to a double so we get floating point division \u8fd9\u79cd\u65b9\u5f0f\u5b9e\u73b0\u7684\u7c7b\u578b\u8f6c\u6362\u548c\u4e4b\u524d\u4e00\u79cd\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u4f46\u662f\u5c06\u9700\u8981\u8f6c\u6362\u7684\u53d8\u91cf\u653e\u5728\u62ec\u53f7\u4e86\uff0c\u66f4\u5bb9\u6613\u770b\u6e05\u695a\u88ab\u8f6c\u6362\u7684\u5bf9\u8c61\u662f\u4ec0\u4e48\u3002 \u867d\u7136\u201cc\u98ce\u683c\u7c7b\u578b\u8f6c\u6362\u201d\u770b\u8d77\u6765\u662f\u5355\u4e00\u7c7b\u578b\u8f6c\u6362\uff0c\u4f46\u5b9e\u9645\u4e0a\u5b83\u53ef\u4ee5\u6839\u636e\u4e0a\u4e0b\u6587\u6267\u884c\u5404\u79cd\u4e0d\u540c\u7684\u8f6c\u6362\u3002\u8fd9\u53ef\u4ee5\u5305\u62ec\u201c\u9759\u6001\u7c7b\u578b\u8f6c\u6362\u201d\u3001\u201cconst\u7c7b\u578b\u8f6c\u6362\u201d\u6216\u201c\u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362\u201d(\u6211\u4eec\u5728\u4e0a\u9762\u63d0\u5230\u7684\u540e\u4e24\u79cd\u7c7b\u578b\u5e94\u8be5\u907f\u514d)\u3002\u56e0\u6b64\uff0c\u201cC\u98ce\u683c\u5f3a\u5236\u8f6c\u6362\u201d\u6709\u53ef\u80fd\u88ab\u65e0\u610f\u4e2d\u8bef\u7528\uff0c\u800c\u4e0d\u4f1a\u4ea7\u751f\u9884\u671f\u7684\u884c\u4e3a\uff0c\u800c\u4f7f\u7528c++\u5f3a\u5236\u8f6c\u6362\u5219\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002 \u76f8\u5173\u5185\u5bb9 \u5982\u679c\u4f60\u5f88\u597d\u5947\uff0cC\u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u53ef\u4ee5\u53c2\u8003 \u8fd9\u7bc7\u6587\u7ae0 \u3002 \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u4f7f\u7528C\u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362\u3002","title":"C\u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362"},{"location":"8-5-Explicit-type-conversion-casting-and-static-cast/#static_cast","text":"C++ \u5f15\u5165\u4e86\u4e00\u4e2a\u65b0\u7684\u5f3a\u5236\u8f6c\u6362\u8fd0\u7b97\u7b26 static_cast \uff0c\u7528\u4e8e\u5c06\u4e00\u79cd\u7c7b\u578b\u7684\u503c\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\u3002 \u5728\u4e4b\u524d\u7684\u8bfe\u4e2d\uff0c\u4f60\u53ef\u80fd\u5df2\u7ecf\u89c1\u8bc6\u8fc7\u5982\u4f55\u4f7f\u7528 static_cast \u5c06 char \u8f6c\u6362\u4e3a int \u4f7f\u5f97 std::cout \u53ef\u4ee5\u6253\u5370\u6574\u578b\u800c\u4e0d\u662f char \uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char c { 'a' }; std :: cout << c << ' ' << static_cast < int > ( c ) << '\\n' ; // prints a 97 return 0 ; } static_cast \u8fd0\u7b97\u7b26\u5c06\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u8f93\u5165\uff0c\u7136\u540e\u5c06\u8868\u8fbe\u5f0f\u6c42\u503c\u7684\u7ed3\u679c\u8f6c\u6362\u4e3a \u5c16\u62ec\u53f7 \u4e2d\u6307\u5b9a\u7684\u7c7b\u578b\u3002 static_cast \u662f\u5c06\u4e00\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\u7684\u6700\u4f73\u9014\u5f84\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { int x { 10 }; int y { 4 }; // static cast x to a double so we get floating point division double d { static_cast < double > ( x ) / y }; std :: cout << d ; // prints 2.5 return 0 ; } static_cast \u6700\u5927\u7684\u4f18\u52bf\u662f\u5b83\u63d0\u4f9b\u4e86 \u8fd0\u884c\u65f6 \u7684\u7c7b\u578b\u68c0\u67e5\u673a\u5236\uff0c\u8fd9\u6837\u5c31\u4e0d\u5bb9\u6613\u72af\u4e0b\u7531\u7c97\u5fc3\u5bfc\u81f4\u7684\u95ee\u9898\u3002 static_cast \u7684\u529f\u80fd\u4e0d\u5982 C \u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362\uff08\u6545\u610f\u7684\uff09\u5f3a\u5927\uff0c\u6240\u4ee5\u4f60\u4e0d\u4f1a\u65e0\u610f\u95f4\u79fb\u9664 const \u6216\u5176\u4ed6\u4f60\u672c\u4e0d\u5e0c\u671b\u53d1\u751f\u7684\u4e8b\u60c5\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u4f18\u5148\u4f7f\u7528 static_cast \u3002","title":"static_cast"},{"location":"8-5-Explicit-type-conversion-casting-and-static-cast/#static_cast_1","text":"\u5f53\u6211\u4eec\u8fdb\u884c\u5177\u6709\u6f5c\u5728\u5371\u9669\u7684\uff08\u7f29\u7a84\uff09\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u53d1\u51fa\u544a\u8b66\u3002\u4f8b\u5982\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a 1 2 int i { 48 }; char ch = i ; // \u9690\u5f0f\u5730\u7f29\u7a84\u8f6c\u6362 \u5c06 int (2 \u5b57\u8282\u6216\u8005 4 \u5b57\u8282) \u8f6c\u6362\u4e3a char (1 \u5b57\u8282)\u901a\u5e38\u662f\u4e0d\u5b89\u5168\u7684\uff08\u56e0\u4e3a\u7f16\u8bd1\u5668\u65e0\u6cd5\u5224\u65ad\u6574\u578b\u503c\u662f\u5426\u4f1a\u8d85\u51fa char \u80fd\u591f\u8868\u793a\u7684\u8303\u56f4\uff09\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u4ea7\u751f\u8b66\u544a\u3002\u800c\u5982\u679c\u6211\u4eec\u4f7f\u7528\u7684\u662f \u5217\u8868\u521d\u59cb\u5316 \uff08 \u62ec\u53f7\u521d\u59cb\u5316 \uff09\uff0c\u7f16\u8bd1\u5668\u5219\u901a\u5e38\u4f1a\u4ea7\u751f\u4e00\u4e2a\u7f16\u8bd1\u9519\u8bef\u3002(\u53c2\u89c1\uff1a \u62ec\u53f7\u521d\u59cb\u5316\u4e0d\u5141\u8bb8\u9690\u5f0f\u7f29\u7a84\u8f6c\u6362 ) \u4e3a\u4e86\u907f\u514d\u8fd9\u4e9b\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 static_cast \u663e\u5f0f\u5730\u5c06\u6574\u578b\u8f6c\u6362\u4e3a char \uff1a 1 2 3 4 int i { 48 }; // explicit conversion from int to char, so that a char is assigned to variable ch char ch { static_cast < char > ( i ) }; \u8fd9\u6837\u505a\u65f6\uff0c\u6211\u4eec\u663e\u5f0f\u5730\u544a\u8bc9\u7f16\u8bd1\u5668\u8fd9\u4e2a\u8f6c\u6362\u662f\u6709\u610f\u7684\uff0c\u540e\u679c\u81ea\u8d1f(\u4f8b\u5982\uff0c\u6ea2\u51fa char \u7684\u8303\u56f4)\u3002\u7531\u4e8e\u8fd9\u4e2a static_cast \u7684\u8f93\u51fa\u7c7b\u578b\u4e3a char \uff0c\u53d8\u91cf ch \u7684\u521d\u59cb\u5316\u7684\u7c7b\u578b\u662f\u5339\u914d\u7684\uff0c\u56e0\u6b64\u4e0d\u4f1a\u4ea7\u751f\u8b66\u544a\u6216\u9519\u8bef\u3002 \u4e0b\u9762\u662f\u53e6\u4e00\u4e2a\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u62b1\u6028\u5c06 double \u8f6c\u6362\u4e3a int \u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u4e22\u5931\u7684\u4f8b\u5b50: 1 2 int i { 100 }; i = i / 2.5 ; \u544a\u8bc9\u7f16\u8bd1\u5668\u4e0a\u8ff0\u8f6c\u6362\u662f\u6709\u610f\u800c\u4e3a\u4e4b\u7684\uff1a 1 2 int i { 100 }; i = static_cast < int > ( i / 2.5 );","title":"\u4f7f\u7528 static_cast \u8fdb\u884c\u663e\u5f0f\u5730\u7f29\u7a84\u8f6c\u6362"},{"location":"8-6-Typedefs-and-type-aliases/","tags":["typedef","alias"],"text":"Key Takeaway using distance_t = double; // distance_t \u4f5c\u4e3a double \u7684\u522b\u540d \u7c7b\u578b\u522b\u540d\u5e76\u975e\u65b0\u7c7b\u578b \u8bf4\u7c7b\u578b\u522b\u540d\u5e76\u4e0d\u662f \u7c7b\u578b\u5b89\u5168 \u7684\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668 \u8bed\u4e49\u9519\u8bef \uff08\u5c3d\u7ba1\u8bed\u6cd5\u662f\u6b63\u786e\u7684\uff09 \u7c7b\u578b\u522b\u540d\u7684\u4f5c\u7528\u57df\u89c4\u5219\u548c\u53d8\u91cf\u6807\u8bc6\u7b26\u7684\u89c4\u5219\u662f\u4e00\u81f4\u7684 \u4f7f\u7528\u7c7b\u578b\u522b\u540d\u53ef\u4ee5\u5b9a\u4e49\u4e0e\u5e73\u53f0\u65e0\u5173\u7684\u7c7b\u578b \u7c7b\u578b\u522b\u540d\u53ef\u4ee5\u7b80\u5316\u590d\u6742\u7c7b\u578b\u540d\u7684\u8f93\u5165\u548c\u7406\u89e3 \u5f53\u7c7b\u578b\u522b\u540d\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\u3001\u4ee3\u7801\u7ef4\u62a4\u6027\u65f6\uff0c\u660e\u667a\u800c\u5ba1\u614e\u5730\u4f7f\u7528\u5b83\u4eec\u3002 \u7c7b\u578b\u522b\u540d \u5728 C++ \u4e2d\uff0c using \u5173\u952e\u5b57\u8fd8\u53ef\u4ee5\u88ab\u7528\u6765\u4e3a\u5df2\u6709\u7684\u7c7b\u578b\u521b\u5efa\u4e00\u4e2a \u7c7b\u578b\u522b\u540d(type aliases) \u3002\u521b\u5efa\u522b\u540d\u65f6\uff0c\u5728 using \u5173\u952e\u5b57\u540e\u9762\u7d27\u63a5\u7740\u7684\u662f\u522b\u540d\uff0c\u7136\u540e\u662f\u4e00\u4e2a\u7b49\u53f7\u4ee5\u53ca\u4e00\u4e2a\u5df2\u6709\u7684\u7c7b\u578b\u540d\uff0c\u4f8b\u5982\uff1a 1 using distance_t = double ; // distance_t \u4f5c\u4e3a double \u7684\u522b\u540d \u5f88\u591a\u7c7b\u578b\u522b\u540d\u4f1a\u4f7f\u7528 \u201c _t \u201d \u6216 \u201c _type \u201d \u540e\u7f00\u6765\u51cf\u5c11\u547d\u540d\u51b2\u7a81\u7684\u51e0\u7387\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u4e60\u60ef\u7684\u4e00\u81f4\u6027\u5e76\u4e0d\u597d\uff0c\u5f88\u591a\u7c7b\u578b\u522b\u540d\u662f\u6ca1\u6709\u540e\u7f00\u7684\u3002 1 2 using distance_type = double ; // \u6ca1\u95ee\u9898, \u540e\u9762\u7684\u7ae0\u8282\u591a\u4f1a\u4f7f\u7528\u8fd9\u79cd\u5f62\u5f0f using distance = double ; // \u8fd9\u6837\u4e5f\u53ef\u4ee5\uff0c, \u4f46\u662f\u53ef\u80fd\u4f1a\u548c\u5176\u4ed6\u53d8\u91cf\u540d\u9020\u6210\u6df7\u6dc6\u6216\u51b2\u7a81 \u4e00\u65e6\u5b8c\u6210\u5b9a\u4e49\u540e\uff0c \u7c7b\u578b\u522b\u540d \u53ef\u4ee5\u5728\u4efb\u4f55\u4f7f\u7528 \u7c7b\u578b \u7684\u5730\u65b9\u4f7f\u7528\u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf\uff1a 1 distance_t milesToDestination { 3.4 }; // \u5b9a\u4e49 double \u7c7b\u578b\u7684\u53d8\u91cf \u5f53\u7f16\u8bd1\u5668\u9047\u5230\u4e00\u4e2a\u7c7b\u578b\u522b\u540d\u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u628a\u522b\u540d\u66ff\u6362\u4e3a\u771f\u6b63\u7684\u7c7b\u578b\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { using distance_t = double ; // \u5b9a\u4e49 distance_t \u4f5c\u4e3a double \u7684\u522b\u540d distance_t milesToDestination { 3.4 }; // \u5b9a\u4e49 double \u7c7b\u578b\u53d8\u91cf std :: cout << milesToDestination << '\\n' ; // \u6253\u5370 double \u503c return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 3.4 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u5c06 distance_t \u5b9a\u4e49\u4e3a double \u7684\u522b\u540d\u3002 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a distance_t \u7c7b\u578b\u7684\u53d8\u91cf milesToDestination \u3002\u56e0\u4e3a\u7f16\u8bd1\u5668\u6307\u5b9a distance_t \u662f\u4e00\u4e2a\u522b\u540d\uff0c\u6240\u4ee5\u5b83\u4f1a\u4f7f\u7528\u771f\u5b9e\u7684\u53d8\u91cf\u7c7b\u578b\uff0c\u5373 double \u3002\u56e0\u6b64\uff0c\u53d8\u91cf milesToDestination \u5728\u7f16\u8bd1\u65f6\u5b9e\u9645\u4e0a\u662f double \u7c7b\u578b\u7684\uff0c\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u5b83\u7684\u884c\u4e3a\u4e5f\u662f\u548c double \u7c7b\u578b\u5b8c\u5168\u4e00\u81f4\u7684\u3002 \u6700\u540e\uff0c\u5c06 milesToDestination \u7684\u503c\u4f5c\u4e3a double \u7c7b\u578b\u6253\u5370\u3002 \u7c7b\u578b\u522b\u540d\u5e76\u4e0d\u662f\u4e00\u79cd\u65b0\u7684\u7c7b\u578b \u7c7b\u578b\u522b\u540d\u5728\u521b\u5efa\u65f6\u5e76\u6ca1\u6709\u5b9e\u9645\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7c7b\u578b\u2014\u2014\u5b83\u53ea\u662f\u4e3a\u5df2\u6709\u7684\u7c7b\u578b\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u6807\u8bc6\u7b26\u3002\u7c7b\u578b\u522b\u540d\u548c\u5b83\u5bf9\u5e94\u7684\u7c7b\u578b\u662f\u53ef\u4ee5\u5b8c\u5168\u4e92\u6362\u7684\u3002 \u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u505a\u4e00\u4e9b\u8bed\u6cd5\u4e0a\u6b63\u786e\uff0c\u4f46\u662f\u8bed\u4e49\u4e0a\u6ca1\u4ec0\u4e48\u5b9e\u9645\u610f\u4e49\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 int main () { using miles_t = long ; // define miles_t as an alias for type long using speed_t = long ; // define speed_t as an alias for type long miles_t distance { 5 }; // distance is actually just a long speed_t mhz { 3200 }; // mhz is actually just a long // The following is syntactically valid (but semantically meaningless) distance = mhz ; return 0 ; } \u5c3d\u7ba1\uff0c\u4ece\u6982\u5ff5\u4e0a\u6765\u8bb2\u6211\u4eec\u5e0c\u671b miles_t \u548c speed_t \u5177\u6709\u4e0d\u540c\u7684\u542b\u4e49\uff0c\u4f46\u662f\u5b83\u4eec\u5b9e\u9645\u4e0a\u90fd\u662f long \u7c7b\u578b\u7684\u3002\u8fd9\u4e5f\u610f\u5473\u7740 miles_t \u3001 speed_t \u548c long \u53ef\u4ee5\u4e92\u6362\u4f7f\u7528\u3002\u7684\u786e\uff0c\u5f53\u6211\u4eec\u628a speed_t \u7c7b\u578b\u7684\u503c\u8d4b\u503c\u7ed9 miles_t \u7c7b\u578b\u7684\u503c\u662f\uff0c\u7f16\u8bd1\u5668\u4f1a\u8ba4\u4e3a\u8fd9\u53ea\u662f\u5c06\u4e00\u4e2a long \u8d4b\u503c\u7ed9\u4e86\u53e6\u5916\u4e00\u4e2a long \uff0c\u6240\u4ee5\u4e0d\u4f1a\u4ea7\u751f\u4efb\u4f55\u8b66\u544a\u6216\u8005\u62a5\u9519\u4fe1\u606f\u3002 \u56e0\u4e3a\u7f16\u8bd1\u5668\u4e0d\u4f1a\u8bc6\u522b\u8fd9\u4e9b \u8bed\u4e49\u9519\u8bef \uff0c\u6240\u4ee5\u6211\u4eec\u8bf4\u7c7b\u578b\u522b\u540d\u5e76\u4e0d\u662f \u7c7b\u578b\u5b89\u5168 \u7684\u3002\u4e0d\u8fc7\uff0c\u5b83\u4eec\u4ecd\u7136\u975e\u5e38\u6709\u7528\u3002 \u6ce8\u610f \u6ce8\u610f\u4e0d\u8981\u6df7\u6dc6\u4f7f\u7528\u4e24\u4e2a\u8bed\u4e49\u4e0d\u540c\u7684\u7c7b\u578b\u522b\u540d \u9898\u5916\u8bdd \u6709\u4e9b\u8bed\u8a00\u652f\u6301\u5f3a\u7c7b\u578btypedef(\u6216\u5f3a\u7c7b\u578b\u522b\u540d)\u7684\u6982\u5ff5\u3002\u5f3a\u7c7b\u578btypedef\u5b9a\u4e49\u5b9e\u9645\u4e0a\u521b\u5efa\u4e86\u4e00\u4e2a\u5177\u6709\u539f\u7c7b\u578b\u6240\u6709\u539f\u59cb\u5c5e\u6027\u7684\u65b0\u7c7b\u578b\uff0c\u4f46\u5982\u679c\u8bd5\u56fe\u5c06\u522b\u540d\u7c7b\u578b\u7684\u503c\u548c\u5f3a\u7c7b\u578b\u5b9a\u4e49\u7684\u503c\u6df7\u5408\u4f7f\u7528\uff0c\u7f16\u8bd1\u5668\u5c06\u629b\u51fa\u9519\u8bef\u3002\u5bf9\u4e8e C++20 \u6765\u8bf4\uff0cC++ \u5e76\u4e0d\u76f4\u63a5\u652f\u6301\u5f3a\u7c7b\u578btypedef(\u5c3d\u7ba1\u679a\u4e3e\u7c7b\u6709\u7c7b\u4f3c\u4e4b\u5904\uff0c\u53c2\u89c1\uff1a 10.4 -- Scoped enumerations (enum classes) )\uff0c\u4f46\u662f\u6709\u5f88\u591a\u7b2c\u4e09\u65b9\u7684 C++ \u5e93\u90fd\u5b9e\u73b0\u4e86\u5f3atypedef\u7c7b\u4f3c\u7684\u884c\u4e3a\u3002 \u7c7b\u578b\u522b\u540d\u7684\u4f5c\u7528\u57df \u7531\u4e8e\u4f5c\u7528\u57df\u662f\u6807\u8bc6\u7b26\u7684\u5c5e\u6027\uff0c\u7c7b\u578b\u522b\u540d\u6807\u8bc6\u7b26\u9075\u5faa\u4e0e\u53d8\u91cf\u6807\u8bc6\u7b26\u76f8\u540c\u7684\u4f5c\u7528\u57df\u89c4\u5219\uff1a\u5728\u5757\u5185\u5b9a\u4e49\u7684\u7c7b\u578b\u522b\u540d\u5177\u6709\u5757\u4f5c\u7528\u57df\uff0c\u5e76\u4e14\u4ec5\u5728\u8be5\u5757\u5185\u53ef\u7528\uff0c\u800c\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u7c7b\u578b\u522b\u540d\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\uff0c\u76f4\u5230\u6587\u4ef6\u672b\u5c3e\u90fd\u53ef\u7528\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c miles_t \u548c speed_t \u53ea\u5728 main() \u51fd\u6570\u4e2d\u53ef\u7528\u3002 \u5982\u679c\u4f60\u9700\u8981\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528\u4e00\u4e2a\u6216\u591a\u4e2a\u7c7b\u578b\u522b\u540d\uff0c\u5b83\u4eec\u53ef\u4ee5\u5728\u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\uff0c\u5e76\u5728\u4efb\u4f55\u9700\u8981\u4f7f\u7528\u8be5\u5b9a\u4e49\u7684\u4ee3\u7801\u6587\u4ef6\u4e2d #include \uff1a mytypes.h 1 2 3 4 5 6 7 #ifndef MYTYPES #define MYTYPES using miles_t = long ; using speed_t = long ; #endif \u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5 include \u7684\u7c7b\u578b\u522b\u540d\u5177\u6709\u5168\u5c40\u4f5c\u7528\u57df\u3002 Typedef typedef (\u662f\u201ctype definition\u201d\u7684\u7f29\u5199) \u5173\u952e\u5b57\u7684\u8bed\u4e49\u548c using \u662f\u7c7b\u4f3c\u7684\uff0c\u4f46\u662f\u8bed\u6cd5\u987a\u5e8f\u662f\u76f8\u53cd\u7684\u3002 1 2 3 // \u4e0b\u9762\u4e24\u4e2a\u522b\u540d\u662f\u4e00\u81f4\u7684 typedef long miles_t ; using miles_t = long ; typedef \u5b58\u5728\u4e8eC++\u4e2d\u4ecd\u7136\u662f\u51fa\u4e8e\u5386\u53f2\u539f\u56e0\uff0c\u5e76\u4e0d\u63a8\u8350\u4f7f\u7528\u5b83\u3002 typedef \u8fd8\u6709\u4e00\u4e9b\u8bed\u6cd5\u95ee\u9898\u3002\u9996\u5148\uff0c\u5f88\u5bb9\u6613\u5fd8\u8bb0\u5e94\u8be5\u628a typedef \u540d\u653e\u5728\u524d\u9762\u8fd8\u662f\u5e94\u8be5\u628a\u522b\u540d\u653e\u5728\u524d\u9762\u3002\u4e0b\u9762\u54ea\u4e2a\u662f\u5bf9\u7684\uff1f 1 2 typedef distance_t double ; // \u9519\u8bef (typedef \u540d\u5e94\u8be5\u653e\u5728\u524d\u9762) typedef double distance_t ; // \u6b63\u786e (\u9700\u8981\u521b\u5efa\u522b\u540d\u7684\u7c7b\u578b\u5e94\u8be5\u653e\u5728\u524d\u9762) \u8fd9\u4e2a\u662f\u5f88\u5bb9\u6613\u5199\u53cd\u7684\uff0c\u4e0d\u8fc7\u597d\u5728\u5199\u53cd\u7684\u65f6\u5019\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002 \u5176\u6b21\uff0c\u5728\u5904\u7406\u590d\u6742\u7c7b\u578b\u65f6\uff0c typedef \u7684\u8bed\u6cd5\u7279\u522b\u4e11\u964b\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u8fd9\u4e2a typedef \u7684\u4f8b\u5b50\u5c31\u975e\u5e38\u96be\u4ee5\u9605\u8bfb\uff0c\u800c\u4f7f\u7528 using \u521b\u5efa\u7684\u7b49\u4ef7\u7684\u7c7b\u578b\u522b\u540d\u5219\u66f4\u5bb9\u6613\u9605\u8bfb\uff1a 1 2 typedef int ( * fcn_t )( double , char ); // fcn_t hard to find using fcn_t = int ( * )( double , char ); // fcn_t easier to find \u5728\u4e0a\u9762\u7684 typedef \u5b9a\u4e49\u4e2d\uff0c\u65b0\u7c7b\u578b( fcn_t )\u88ab\u85cf\u5728\u4e86\u5b9a\u4e49\u7684\u4e2d\u95f4\u90e8\u5206\uff0c\u8fd9\u4f7f\u5f97\u8be5\u5b9a\u4e49\u53d8\u5f97\u5f88\u96be\u9605\u8bfb\u3002 \u7b2c\u4e09\uff0c\u201ctypedef\u201d \u8fd9\u4e2a\u770b\u8d77\u6765\u50cf\u662f\u8981\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u7c7b\u578b\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\uff0c\u4ece\u4e4b\u524d\u7684\u4f8b\u5b50\u53ef\u4ee5\u770b\u51fa\uff0c\u7c7b\u578b\u548c\u7c7b\u578b\u522b\u540d\u662f\u53ef\u4ee5\u4e92\u6362\u4f7f\u7528\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u521b\u5efa\u7c7b\u578b\u522b\u540d\u65f6\uff0c\u5c3d\u91cf\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u8bed\u6cd5\u800c\u4e0d\u662f typedef \u8bed\u6cd5\u3002 \u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u4f7f\u7528\u7c7b\u578b\u522b\u540d\uff1f \u5728\u4e86\u89e3\u4e86\u4ec0\u4e48\u662f\u7c7b\u578b\u522b\u540d\u4e4b\u540e\uff0c\u662f\u65f6\u5019\u8be5\u804a\u804a\uff0c\u5408\u9002\u5e94\u8be5\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u5462\uff1f \u5728\u5e73\u53f0\u65e0\u5173\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528\u7c7b\u578b\u522b\u540d \u7c7b\u578b\u522b\u540d\u7684\u4e00\u4e2a\u7528\u9014\u662f\uff0c\u5b83\u4eec\u53ef\u4ee5\u7528\u6765\u9690\u85cf\u5e73\u53f0\u7279\u5b9a\u7684\u7ec6\u8282\u3002\u5728\u67d0\u4e9b\u5e73\u53f0\u4e0a\uff0c int \u4e3a2\u5b57\u8282\uff0c\u800c\u5728\u5176\u4ed6\u5e73\u53f0\u4e0a\u4e3a 4 \u5b57\u8282\u3002\u56e0\u6b64\uff0c\u5728\u7f16\u5199\u4e0e\u5e73\u53f0\u65e0\u5173\u7684\u4ee3\u7801\u65f6\uff0c\u4f7f\u7528 int \u5b58\u50a8\u8d85\u8fc7 2 \u5b57\u8282\u7684\u4fe1\u606f\u53ef\u80fd\u4f1a\u6709\u6f5c\u5728\u7684\u5371\u9669\u3002 \u56e0\u4e3a\u4ece\u5b57\u9762\u4e0a\u5e76\u4e0d\u80fd\u770b\u51fa char \u3001 short \u3001 int \u548c long \u7684\u5927\u5c0f\uff0c\u6240\u4ee5\u8de8\u5e73\u53f0\u7a0b\u5e8f\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u6765\u5b9a\u4e49\u4ee5\u4f4d\u4e3a\u5355\u4f4d\u5305\u542b\u7c7b\u578b\u5927\u5c0f\u7684\u522b\u540d\u662f\u76f8\u5f53\u5e38\u89c1\u7684\u3002\u4f8b\u5982\uff0c int8_t \u662f\u4e00\u4e2a 8 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\uff0c int16_t \u662f\u4e00\u4e2a 16 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\uff0c int32_t \u662f\u4e00\u4e2a32\u4f4d\u6709\u7b26\u53f7\u6574\u6570\u3002\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u6709\u52a9\u4e8e\u9632\u6b62\u9519\u8bef\uff0c\u5e76\u66f4\u6e05\u695a\u5730\u8bf4\u660e\u5bf9\u53d8\u91cf\u5927\u5c0f\u7684\u671f\u671b\u503c\u662f\u591a\u5c11\u3002 \u4e3a\u4e86\u786e\u4fdd\u6bcf\u4e2a\u522b\u540d\u7c7b\u578b\u89e3\u6790\u4e3a\u6b63\u786e\u5927\u5c0f\u7684\u7c7b\u578b\uff0c\u8fd9\u7c7b\u7c7b\u578b\u522b\u540d\u901a\u5e38\u4e0e\u9884\u5904\u7406\u5668\u6307\u4ee4\u4e00\u8d77\u4f7f\u7528: 1 2 3 4 5 6 7 8 9 #ifdef INT_2_BYTES using int8_t = char ; using int16_t = int ; using int32_t = long ; #else using int8_t = char ; using int16_t = short ; using int32_t = int ; #endif \u5728\u67d0\u4e9b\u673a\u5668\u4e0a\uff0c\u6574\u578b\u53ea\u5360 2 \u4e2a\u5b57\u8282\uff0c\u8fd9\u65f6\u5b8f INT_2_BYTES \u662f\u88ab #defined \u8fc7\u7684\uff0c\u6240\u4ee5\u7a0b\u5e8f\u5728\u7f16\u8bd1\u65f6\u4f1a\u5c06\u8fd9\u6bb5\u4ee3\u7801\u7684\u4e0a\u534a\u90e8\u5206\u5b9a\u4e49\u7684\u7c7b\u578b\u522b\u540d\u8fdb\u884c\u7f16\u8bd1\u3002\u5728\u6574\u578b\u5360 4 \u4e2a\u5b57\u8282\u7684\u673a\u5668\u4e0a\uff0c \u5b8f INT_2_BYTES \u4e0d\u5b58\u5728\uff0c\u56e0\u6b64\u4e0b\u534a\u90e8\u5206\u7684\u7c7b\u578b\u522b\u540d\u4f1a\u88ab\u7f16\u8bd1\u3002 \u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528 char \u3001 short \u3001 int \u548c long \u7684\u7ec4\u5408\u5728\u7279\u5b9a\u7684\u673a\u5668\u4e0a\u5c06 int8_t \u89e3\u6790\u4e3a 1 \u4e2a\u5b57\u8282\u7684\u6574\u6570\uff0c int16_t \u89e3\u6790\u4e3a 2 \u4e2a\u5b57\u8282\u7684\u6574\u6570\uff0c int32_t \u89e3\u6790\u4e3a 4 \u4e2a\u5b57\u8282\u7684\u6574\u6570\u3002 \u5bf9\u4e8e\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b (\u4f8b\u5982 std::int_fast16_t \u548c std::int_least32_t ) \u548c size_t \u7c7b\u578b (\u53c2\u89c1\uff1a 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t ) \u5176\u5b9e\u4e5f\u6b63\u662f\u8fd9\u4e9b\u57fa\u7840\u7c7b\u578b\u7684\u522b\u540d\u7f62\u4e86\u3002 \u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728\u4f7f\u7528 std::cout \u6253\u5370 8 \u4f4d\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u7684\u65f6\u5019\uff0c\u4f60\u591a\u534a\u4f1a\u6253\u5370\u51fa\u4e00\u4e2a\u5b57\u7b26\u7684\u539f\u56e0\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 #include // for fixed-width integers #include int main () { std :: int_least8_t x { 97 }; // int_least8_t \u5b9e\u9645\u4e0a\u662f char \u7684\u7c7b\u578b\u522b\u540d std :: cout << x ; return 0 ; } \u7a0b\u5e8f\u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 a \u56e0\u4e3a std::int_least8_t \u901a\u5e38\u88ab\u5b9a\u4e49\u4e3a char \u7684\u7c7b\u578b\u522b\u540d\uff0c\u53d8\u91cf x \u5c06\u88ab\u5b9a\u4e49\u4e3a char \u3002 \u800c char \u7c7b\u578b\u5c06\u5176\u503c\u6253\u5370\u4e3aASCII\u5b57\u7b26\uff0c\u800c\u4e0d\u662f\u6574\u6570\u503c\u3002 \u4f7f\u7528\u7c7b\u578b\u522b\u540d\u7b80\u5316\u7c7b\u578b \u867d\u7136\u6211\u4eec\u76ee\u524d\u53ea\u4f7f\u7528\u8fc7\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u662f\u5bf9\u4e8e\u9ad8\u9636C++\u7f16\u7a0b\u6765\u8bf4\uff0c\u7c7b\u578b\u901a\u5e38\u662f\u590d\u6742\u4e14\u5197\u957f\u7684\uff0c\u5982\u679c\u4f60\u624b\u52a8\u8f93\u5165\u5b83\u4eec\u4f1a\u975e\u5e38\u8d39\u52b2\u3002\u4f8b\u5982\uff0c\u6709\u4e9b\u51fd\u6570\u548c\u53d8\u91cf\u7684\u5b9a\u4e49\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // for std::string #include // for std::vector #include // for std::pair bool hasDuplicates ( std :: vector < std :: pair < std :: string , int >> pairlist ) { // some code here return false ; } int main () { std :: vector < std :: pair < std :: string , int >> pairlist ; return 0 ; } \u5728\u6bcf\u4e2a\u9700\u8981\u7684\u5730\u65b9\u8f93\u5165 std::vector> \u4e0d\u4ec5\u7e41\u7410\uff0c\u800c\u4e14\u975e\u5e38\u5bb9\u6613\u51fa\u9519\u3002\u5982\u679c\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u5219\u7b80\u5355\u7684\u591a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include // for std::string #include // for std::vector #include // for std::pair using pairlist_t = std :: vector < std :: pair < std :: string , int >> ; // make pairlist_t an alias for this crazy type bool hasDuplicates ( pairlist_t pairlist ) // use pairlist_t in a function parameter { // some code here return false ; } int main () { pairlist_t pairlist ; // instantiate a pairlist_t variable return 0 ; } \u770b\u8d77\u6765\u597d\u591a\u4e86\u5bf9\u5427\uff01\u73b0\u5728\uff0c\u4f60\u9700\u8981\u8f93\u5165\u7684\u662f pairlist_t \u800c\u4e0d\u662f std::vector> \u3002 \u5982\u679c\u4f60\u8fd8\u4e0d\u77e5\u9053 std::vector \u3001 std::pair \u6216\u8005\u90a3\u4e9b\u770b\u8d77\u6765\u5f88\u5413\u4eba\u7684\u5c16\u62ec\u53f7\u662f\u4ec0\u4e48\uff0c\u4e5f\u4e0d\u7528\u62c5\u5fc3\u3002\u73b0\u5728\u4f60\u53ea\u9700\u8981\u77e5\u9053\u7c7b\u578b\u522b\u540d\u53ef\u4ee5\u7b80\u5316\u7c7b\u578b\u3001\u4f7f\u4f60\u7684\u4ee3\u7801\u66f4\u6613\u8bfb\uff0c\u51cf\u5c11\u7e41\u7410\u7684\u8f93\u5165\u5c31\u53ef\u4ee5\u4e86\u3002 \u8fd9\u4e5f\u662f\u7c7b\u578b\u522b\u540d\u6700\u6709\u7528\u7684\u5730\u65b9\u3002 \u4f7f\u7528\u7c7b\u578b\u522b\u540d\u8ba9\u7a0b\u5e8f\u66f4\u6e05\u6670 \u7c7b\u578b\u522b\u540d\u8fd8\u6709\u52a9\u4e8e\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8df5\u884c\u4ee3\u7801\u5373\u6587\u6863\u7684\u601d\u60f3\u3002 With variables, we have the variable\u2019s identifier to help document the purpose of the variable. But consider the case of a function\u2019s return value. Data types such as char , int , long , double , and bool are good for describing what type a function returns, but more often we want to know what purpose a return value serves. \u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7\u6807\u8bc6\u7b26\uff08\u53d8\u91cf\u540d\uff09\u6765\u5e2e\u52a9\u8868\u793a\u5b83\u7684\u7528\u9014\u3002\u4f46\u662f\u5bf9\u4e8e\u51fd\u6570\u8fd4\u56de\u503c\u6765\u8bf4\uff0c\u5c3d\u7ba1 char \u3001 int \u3001 long \u3001 double \u548c bool \u53ef\u4ee5\u8868\u660e\u51fd\u6570\u503c\u7684\u7c7b\u578b\uff0c\u4f46\u5b83\u4eec\u5e76\u6ca1\u6709\u529e\u6cd5\u5f88\u597d\u5730\u8868\u660e\u51fd\u6570\u8fd4\u56de\u503c\u7684\u7528\u9014\u7a76\u7adf\u662f\u4ec0\u4e48\u3002 \u5bf9\u4e8e\u4e0b\u9762\u7684\u51fd\u6570\u6765\u8bf4\uff1a 1 int gradeTest (); \u51fd\u6570\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u662f\u6574\u578b\uff0c\u4f46\u662f\u8fd9\u4e2a\u6574\u578b\u8fd4\u56de\u503c\u7684\u542b\u4e49\u5230\u5e95\u662f\u4ec0\u4e48\u5462\uff1f\u662f\u5b66\u751f\u7684\u5206\u6570\uff1f\u662f\u6ca1\u6709\u4f5c\u7b54\u7684\u95ee\u9898\u6570\uff1f\u662f\u5b66\u53f7\uff1f\u8fd8\u662f\u9519\u8bef\u7801\uff1f\u6ca1\u6709\u4eba\u77e5\u9053\uff0c\u56e0\u4e3a int \u5e76\u4e0d\u80fd\u8868\u8fbe\u8fd9\u5c42\u542b\u4e49\u3002\u5982\u679c\u5e78\u8fd0\u7684\u8bdd\uff0c\u4e5f\u8bb8\u51fd\u6570\u6709\u6587\u6863\u53ef\u4f9b\u6211\u4eec\u53c2\u8003\u3002\u5982\u679c\u4e0d\u5e78\u7684\u8bdd\uff0c\u4f60\u5c31\u5fc5\u987b\u9605\u8bfb\u6e90\u7801\u7136\u540e\u53bb\u731c\u6d4b\u5b83\u7684\u7528\u9014\u4e86\u3002 \u800c\u5bf9\u4e8e\u4e0b\u9762\u8fd9\u4e2a\u4f7f\u7528\u4e86\u7c7b\u578b\u522b\u540d\u7684\u7b49\u4ef7\u7684\u4f8b\u5b50\u6765\u8bf4\uff1a 1 2 using testScore_t = int ; testScore_t gradeTest (); \u8fd4\u56de\u503c\u7c7b\u578b\u4e3a testScore_t \uff0c\u5b83\u80fd\u591f\u66f4\u597d\u5730\u8868\u660e\u51fd\u6570\u7684\u8fd4\u56de\u503c\u8868\u793a\u4e00\u6b21\u6d4b\u9a8c\u7684\u5206\u6570\u3002 \u6839\u636e\u6211\u4eec\u7684\u7ecf\u9a8c\uff0c\u521b\u5efa\u7c7b\u578b\u522b\u540d\u53ea\u662f\u4e3a\u4e86\u8bb0\u5f55\u5355\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u662f\u4e0d\u503c\u5f97\u7684(\u4f7f\u7528\u6ce8\u91ca\u4ee3\u66ff)\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u5df2\u7ecf\u56e0\u4e3a\u5176\u4ed6\u539f\u56e0\u521b\u5efa\u4e86\u7c7b\u578b\u522b\u540d\uff0c\u8fd9\u53ef\u80fd\u662f\u4e00\u4e2a\u989d\u5916\u597d\u5904\u3002 \u4f7f\u7528\u7c7b\u578b\u522b\u540d\u63d0\u9ad8\u4ee3\u7801\u53ef\u7ef4\u62a4\u6027 \u4f7f\u7528\u7c7b\u578b\u522b\u540d\u8fd8\u53ef\u4ee5\u5728\u4e0d\u8fdb\u884c\u5927\u91cf\u6539\u52a8\u7684\u60c5\u51b5\u4e0b\u4fee\u6539\u5bf9\u8c61\u7684\u57fa\u672c\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5b66\u751f\u7684ID\u539f\u672c\u4f7f\u7528 short \u6765\u4fdd\u5b58\uff0c\u4f46\u540e\u6765\u51b3\u5b9a\u7528 long \u6765\u4ee3\u66ff\uff0c\u6b64\u65f6\u4f60\u5c31\u5fc5\u987b\u5c06\u5927\u91cf\u7684 short \u66ff\u6362\u4e3a long \u3002\u8fd9\u4e2a\u65f6\u5019\u5f80\u5f80\u5f88\u96be\u786e\u5b9a\u7a76\u7adf\u54ea\u4e9b short \u662f\u7528\u6765\u4fdd\u5b58ID\u7684\uff0c\u800c\u54ea\u4e9b\u5219\u7528\u4e8e\u5176\u4ed6\u76ee\u7684\u3002 \u7136\u800c\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u4e86\u7c7b\u578b\u522b\u540d\uff0c\u90a3\u4e48\u66f4\u6539\u7c7b\u578b\u5c31\u50cf\u66f4\u65b0\u7c7b\u578b\u522b\u540d\u4e00\u6837\u7b80\u5355(\u4f8b\u5982\uff0c\u5c06 using studentID_t = short; \u4fee\u6539\u4e3a using studentID_t = long; )\u3002 \u867d\u7136\u8fd9\u770b\u8d77\u6765\u662f\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u7279\u6027\uff0c\u4f46\u6bcf\u5f53\u7c7b\u578b\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u6211\u4eec\u4ecd\u7136\u9700\u8981\u4fdd\u6301\u8c28\u614e\uff0c\u56e0\u4e3a\u7a0b\u5e8f\u7684\u884c\u4e3a\u4e5f\u53ef\u80fd\u968f\u4e4b\u53d1\u751f\u53d8\u5316\u3002\u5f53\u5c06\u7c7b\u578b\u522b\u540d\u7684\u7c7b\u578b\u66f4\u6539\u4e3a\u4e0d\u540c\u7c7b\u578b\u65cf\u4e2d\u7684\u7c7b\u578b\u65f6\u5c24\u5176\u5982\u6b64(\u4f8b\u5982\uff0c\u5c06\u6574\u6570\u66f4\u6539\u4e3a\u6d6e\u70b9\u503c\uff0c\u6216\u53cd\u4e4b\u4ea6\u7136)\uff01\u65b0\u7c7b\u578b\u53ef\u80fd\u6709\u6d6e\u70b9\u6570\u6bd4\u8f83\u6216\u6574\u6570/\u6d6e\u70b9\u9664\u6cd5\u95ee\u9898\uff0c\u6216\u5176\u4ed6\u65e7\u7c7b\u578b\u6240\u6ca1\u6709\u7684\u95ee\u9898\u3002\u5982\u679c\u4f60\u5c06\u73b0\u6709\u7684\u7c7b\u578b\u66f4\u6539\u4e3a\u5176\u4ed6\u7c7b\u578b\uff0c\u4ee3\u7801\u5e94\u8be5\u91cd\u65b0\u3001\u5f7b\u5e95\u5730\u6d4b\u8bd5\u3002 \u7f3a\u70b9\u548c\u7ed3\u8bba \u7136\u7c7b\u578b\u522b\u540d\u63d0\u4f9b\u4e86\u4e00\u4e9b\u597d\u5904\uff0c\u4f46\u5b83\u4eec\u4e5f\u5728\u4ee3\u7801\u4e2d\u5f15\u5165\u4e86\u53e6\u4e00\u4e2a\u9700\u8981\u7406\u89e3\u7684\u6807\u8bc6\u7b26\u3002\u5982\u679c\u8fd9\u4e00\u70b9\u6ca1\u6709\u88ab\u53ef\u8bfb\u6027\u6216\u7406\u89e3\u7684\u597d\u5904\u6240\u62b5\u6d88\uff0c\u90a3\u4e48\u7c7b\u578b\u522b\u540d\u5f0a\u5927\u4e8e\u5229\u3002 \u7c7b\u578b\u522b\u540d\u4f7f\u7528\u4e0d\u5f53\u7684\u8bdd\uff0c\u4f1a\u5c06\u719f\u6089\u7684\u7c7b\u578b(\u4f8b\u5982 std::string )\u9690\u85cf\u5728\u81ea\u5b9a\u4e49\u540d\u79f0\u540e\u9762\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b(\u4f8b\u5982\u667a\u80fd\u6307\u9488\uff0c\u6211\u4eec\u5c06\u5728\u4ee5\u540e\u7684\u7ae0\u8282\u4e2d\u8ba8\u8bba)\uff0c\u6a21\u7cca\u7684\u7c7b\u578b\u4fe1\u606f\u4e5f\u53ef\u80fd\u4e0d\u5229\u4e8e\u7406\u89e3\u7c7b\u578b\u5e94\u8be5\u5982\u4f55\u5de5\u4f5c\u3002 \u56e0\u6b64\uff0c\u7c7b\u578b\u522b\u540d\u5e94\u8be5\u4e3b\u8981\u7528\u4e8e\u5bf9\u4ee3\u7801\u53ef\u8bfb\u6027\u6216\u4ee3\u7801\u7ef4\u62a4\u6709\u660e\u663e\u597d\u5904\u7684\u60c5\u51b5\u3002\u8fd9\u65e2\u662f\u4e00\u95e8\u79d1\u5b66\uff0c\u4e5f\u662f\u4e00\u95e8\u827a\u672f\u3002\u5f53\u7c7b\u578b\u522b\u540d\u53ef\u4ee5\u5728\u4ee3\u7801\u7684\u8bb8\u591a\u5730\u65b9\u4f7f\u7528\u800c\u4e0d\u662f\u5728\u5be5\u5be5\u51e0\u5904\u4f7f\u7528\u65f6\uff0c\u624d\u662f\u5b83\u4eec\u5927\u653e\u5f02\u5f69\u7684\u673a\u4f1a\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u7c7b\u578b\u522b\u540d\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\u3001\u4ee3\u7801\u7ef4\u62a4\u6027\u65f6\uff0c\u660e\u667a\u800c\u5ba1\u614e\u5730\u4f7f\u7528\u5b83\u4eec\u3002","title":"8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d"},{"location":"8-6-Typedefs-and-type-aliases/#_1","text":"\u5728 C++ \u4e2d\uff0c using \u5173\u952e\u5b57\u8fd8\u53ef\u4ee5\u88ab\u7528\u6765\u4e3a\u5df2\u6709\u7684\u7c7b\u578b\u521b\u5efa\u4e00\u4e2a \u7c7b\u578b\u522b\u540d(type aliases) \u3002\u521b\u5efa\u522b\u540d\u65f6\uff0c\u5728 using \u5173\u952e\u5b57\u540e\u9762\u7d27\u63a5\u7740\u7684\u662f\u522b\u540d\uff0c\u7136\u540e\u662f\u4e00\u4e2a\u7b49\u53f7\u4ee5\u53ca\u4e00\u4e2a\u5df2\u6709\u7684\u7c7b\u578b\u540d\uff0c\u4f8b\u5982\uff1a 1 using distance_t = double ; // distance_t \u4f5c\u4e3a double \u7684\u522b\u540d \u5f88\u591a\u7c7b\u578b\u522b\u540d\u4f1a\u4f7f\u7528 \u201c _t \u201d \u6216 \u201c _type \u201d \u540e\u7f00\u6765\u51cf\u5c11\u547d\u540d\u51b2\u7a81\u7684\u51e0\u7387\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u4e60\u60ef\u7684\u4e00\u81f4\u6027\u5e76\u4e0d\u597d\uff0c\u5f88\u591a\u7c7b\u578b\u522b\u540d\u662f\u6ca1\u6709\u540e\u7f00\u7684\u3002 1 2 using distance_type = double ; // \u6ca1\u95ee\u9898, \u540e\u9762\u7684\u7ae0\u8282\u591a\u4f1a\u4f7f\u7528\u8fd9\u79cd\u5f62\u5f0f using distance = double ; // \u8fd9\u6837\u4e5f\u53ef\u4ee5\uff0c, \u4f46\u662f\u53ef\u80fd\u4f1a\u548c\u5176\u4ed6\u53d8\u91cf\u540d\u9020\u6210\u6df7\u6dc6\u6216\u51b2\u7a81 \u4e00\u65e6\u5b8c\u6210\u5b9a\u4e49\u540e\uff0c \u7c7b\u578b\u522b\u540d \u53ef\u4ee5\u5728\u4efb\u4f55\u4f7f\u7528 \u7c7b\u578b \u7684\u5730\u65b9\u4f7f\u7528\u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf\uff1a 1 distance_t milesToDestination { 3.4 }; // \u5b9a\u4e49 double \u7c7b\u578b\u7684\u53d8\u91cf \u5f53\u7f16\u8bd1\u5668\u9047\u5230\u4e00\u4e2a\u7c7b\u578b\u522b\u540d\u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u628a\u522b\u540d\u66ff\u6362\u4e3a\u771f\u6b63\u7684\u7c7b\u578b\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { using distance_t = double ; // \u5b9a\u4e49 distance_t \u4f5c\u4e3a double \u7684\u522b\u540d distance_t milesToDestination { 3.4 }; // \u5b9a\u4e49 double \u7c7b\u578b\u53d8\u91cf std :: cout << milesToDestination << '\\n' ; // \u6253\u5370 double \u503c return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 3.4 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u5c06 distance_t \u5b9a\u4e49\u4e3a double \u7684\u522b\u540d\u3002 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a distance_t \u7c7b\u578b\u7684\u53d8\u91cf milesToDestination \u3002\u56e0\u4e3a\u7f16\u8bd1\u5668\u6307\u5b9a distance_t \u662f\u4e00\u4e2a\u522b\u540d\uff0c\u6240\u4ee5\u5b83\u4f1a\u4f7f\u7528\u771f\u5b9e\u7684\u53d8\u91cf\u7c7b\u578b\uff0c\u5373 double \u3002\u56e0\u6b64\uff0c\u53d8\u91cf milesToDestination \u5728\u7f16\u8bd1\u65f6\u5b9e\u9645\u4e0a\u662f double \u7c7b\u578b\u7684\uff0c\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u5b83\u7684\u884c\u4e3a\u4e5f\u662f\u548c double \u7c7b\u578b\u5b8c\u5168\u4e00\u81f4\u7684\u3002 \u6700\u540e\uff0c\u5c06 milesToDestination \u7684\u503c\u4f5c\u4e3a double \u7c7b\u578b\u6253\u5370\u3002","title":"\u7c7b\u578b\u522b\u540d"},{"location":"8-6-Typedefs-and-type-aliases/#_2","text":"\u7c7b\u578b\u522b\u540d\u5728\u521b\u5efa\u65f6\u5e76\u6ca1\u6709\u5b9e\u9645\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7c7b\u578b\u2014\u2014\u5b83\u53ea\u662f\u4e3a\u5df2\u6709\u7684\u7c7b\u578b\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u6807\u8bc6\u7b26\u3002\u7c7b\u578b\u522b\u540d\u548c\u5b83\u5bf9\u5e94\u7684\u7c7b\u578b\u662f\u53ef\u4ee5\u5b8c\u5168\u4e92\u6362\u7684\u3002 \u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u505a\u4e00\u4e9b\u8bed\u6cd5\u4e0a\u6b63\u786e\uff0c\u4f46\u662f\u8bed\u4e49\u4e0a\u6ca1\u4ec0\u4e48\u5b9e\u9645\u610f\u4e49\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 int main () { using miles_t = long ; // define miles_t as an alias for type long using speed_t = long ; // define speed_t as an alias for type long miles_t distance { 5 }; // distance is actually just a long speed_t mhz { 3200 }; // mhz is actually just a long // The following is syntactically valid (but semantically meaningless) distance = mhz ; return 0 ; } \u5c3d\u7ba1\uff0c\u4ece\u6982\u5ff5\u4e0a\u6765\u8bb2\u6211\u4eec\u5e0c\u671b miles_t \u548c speed_t \u5177\u6709\u4e0d\u540c\u7684\u542b\u4e49\uff0c\u4f46\u662f\u5b83\u4eec\u5b9e\u9645\u4e0a\u90fd\u662f long \u7c7b\u578b\u7684\u3002\u8fd9\u4e5f\u610f\u5473\u7740 miles_t \u3001 speed_t \u548c long \u53ef\u4ee5\u4e92\u6362\u4f7f\u7528\u3002\u7684\u786e\uff0c\u5f53\u6211\u4eec\u628a speed_t \u7c7b\u578b\u7684\u503c\u8d4b\u503c\u7ed9 miles_t \u7c7b\u578b\u7684\u503c\u662f\uff0c\u7f16\u8bd1\u5668\u4f1a\u8ba4\u4e3a\u8fd9\u53ea\u662f\u5c06\u4e00\u4e2a long \u8d4b\u503c\u7ed9\u4e86\u53e6\u5916\u4e00\u4e2a long \uff0c\u6240\u4ee5\u4e0d\u4f1a\u4ea7\u751f\u4efb\u4f55\u8b66\u544a\u6216\u8005\u62a5\u9519\u4fe1\u606f\u3002 \u56e0\u4e3a\u7f16\u8bd1\u5668\u4e0d\u4f1a\u8bc6\u522b\u8fd9\u4e9b \u8bed\u4e49\u9519\u8bef \uff0c\u6240\u4ee5\u6211\u4eec\u8bf4\u7c7b\u578b\u522b\u540d\u5e76\u4e0d\u662f \u7c7b\u578b\u5b89\u5168 \u7684\u3002\u4e0d\u8fc7\uff0c\u5b83\u4eec\u4ecd\u7136\u975e\u5e38\u6709\u7528\u3002 \u6ce8\u610f \u6ce8\u610f\u4e0d\u8981\u6df7\u6dc6\u4f7f\u7528\u4e24\u4e2a\u8bed\u4e49\u4e0d\u540c\u7684\u7c7b\u578b\u522b\u540d \u9898\u5916\u8bdd \u6709\u4e9b\u8bed\u8a00\u652f\u6301\u5f3a\u7c7b\u578btypedef(\u6216\u5f3a\u7c7b\u578b\u522b\u540d)\u7684\u6982\u5ff5\u3002\u5f3a\u7c7b\u578btypedef\u5b9a\u4e49\u5b9e\u9645\u4e0a\u521b\u5efa\u4e86\u4e00\u4e2a\u5177\u6709\u539f\u7c7b\u578b\u6240\u6709\u539f\u59cb\u5c5e\u6027\u7684\u65b0\u7c7b\u578b\uff0c\u4f46\u5982\u679c\u8bd5\u56fe\u5c06\u522b\u540d\u7c7b\u578b\u7684\u503c\u548c\u5f3a\u7c7b\u578b\u5b9a\u4e49\u7684\u503c\u6df7\u5408\u4f7f\u7528\uff0c\u7f16\u8bd1\u5668\u5c06\u629b\u51fa\u9519\u8bef\u3002\u5bf9\u4e8e C++20 \u6765\u8bf4\uff0cC++ \u5e76\u4e0d\u76f4\u63a5\u652f\u6301\u5f3a\u7c7b\u578btypedef(\u5c3d\u7ba1\u679a\u4e3e\u7c7b\u6709\u7c7b\u4f3c\u4e4b\u5904\uff0c\u53c2\u89c1\uff1a 10.4 -- Scoped enumerations (enum classes) )\uff0c\u4f46\u662f\u6709\u5f88\u591a\u7b2c\u4e09\u65b9\u7684 C++ \u5e93\u90fd\u5b9e\u73b0\u4e86\u5f3atypedef\u7c7b\u4f3c\u7684\u884c\u4e3a\u3002","title":"\u7c7b\u578b\u522b\u540d\u5e76\u4e0d\u662f\u4e00\u79cd\u65b0\u7684\u7c7b\u578b"},{"location":"8-6-Typedefs-and-type-aliases/#_3","text":"\u7531\u4e8e\u4f5c\u7528\u57df\u662f\u6807\u8bc6\u7b26\u7684\u5c5e\u6027\uff0c\u7c7b\u578b\u522b\u540d\u6807\u8bc6\u7b26\u9075\u5faa\u4e0e\u53d8\u91cf\u6807\u8bc6\u7b26\u76f8\u540c\u7684\u4f5c\u7528\u57df\u89c4\u5219\uff1a\u5728\u5757\u5185\u5b9a\u4e49\u7684\u7c7b\u578b\u522b\u540d\u5177\u6709\u5757\u4f5c\u7528\u57df\uff0c\u5e76\u4e14\u4ec5\u5728\u8be5\u5757\u5185\u53ef\u7528\uff0c\u800c\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u7c7b\u578b\u522b\u540d\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\uff0c\u76f4\u5230\u6587\u4ef6\u672b\u5c3e\u90fd\u53ef\u7528\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c miles_t \u548c speed_t \u53ea\u5728 main() \u51fd\u6570\u4e2d\u53ef\u7528\u3002 \u5982\u679c\u4f60\u9700\u8981\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528\u4e00\u4e2a\u6216\u591a\u4e2a\u7c7b\u578b\u522b\u540d\uff0c\u5b83\u4eec\u53ef\u4ee5\u5728\u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\uff0c\u5e76\u5728\u4efb\u4f55\u9700\u8981\u4f7f\u7528\u8be5\u5b9a\u4e49\u7684\u4ee3\u7801\u6587\u4ef6\u4e2d #include \uff1a mytypes.h 1 2 3 4 5 6 7 #ifndef MYTYPES #define MYTYPES using miles_t = long ; using speed_t = long ; #endif \u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5 include \u7684\u7c7b\u578b\u522b\u540d\u5177\u6709\u5168\u5c40\u4f5c\u7528\u57df\u3002","title":"\u7c7b\u578b\u522b\u540d\u7684\u4f5c\u7528\u57df"},{"location":"8-6-Typedefs-and-type-aliases/#typedef","text":"typedef (\u662f\u201ctype definition\u201d\u7684\u7f29\u5199) \u5173\u952e\u5b57\u7684\u8bed\u4e49\u548c using \u662f\u7c7b\u4f3c\u7684\uff0c\u4f46\u662f\u8bed\u6cd5\u987a\u5e8f\u662f\u76f8\u53cd\u7684\u3002 1 2 3 // \u4e0b\u9762\u4e24\u4e2a\u522b\u540d\u662f\u4e00\u81f4\u7684 typedef long miles_t ; using miles_t = long ; typedef \u5b58\u5728\u4e8eC++\u4e2d\u4ecd\u7136\u662f\u51fa\u4e8e\u5386\u53f2\u539f\u56e0\uff0c\u5e76\u4e0d\u63a8\u8350\u4f7f\u7528\u5b83\u3002 typedef \u8fd8\u6709\u4e00\u4e9b\u8bed\u6cd5\u95ee\u9898\u3002\u9996\u5148\uff0c\u5f88\u5bb9\u6613\u5fd8\u8bb0\u5e94\u8be5\u628a typedef \u540d\u653e\u5728\u524d\u9762\u8fd8\u662f\u5e94\u8be5\u628a\u522b\u540d\u653e\u5728\u524d\u9762\u3002\u4e0b\u9762\u54ea\u4e2a\u662f\u5bf9\u7684\uff1f 1 2 typedef distance_t double ; // \u9519\u8bef (typedef \u540d\u5e94\u8be5\u653e\u5728\u524d\u9762) typedef double distance_t ; // \u6b63\u786e (\u9700\u8981\u521b\u5efa\u522b\u540d\u7684\u7c7b\u578b\u5e94\u8be5\u653e\u5728\u524d\u9762) \u8fd9\u4e2a\u662f\u5f88\u5bb9\u6613\u5199\u53cd\u7684\uff0c\u4e0d\u8fc7\u597d\u5728\u5199\u53cd\u7684\u65f6\u5019\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002 \u5176\u6b21\uff0c\u5728\u5904\u7406\u590d\u6742\u7c7b\u578b\u65f6\uff0c typedef \u7684\u8bed\u6cd5\u7279\u522b\u4e11\u964b\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u8fd9\u4e2a typedef \u7684\u4f8b\u5b50\u5c31\u975e\u5e38\u96be\u4ee5\u9605\u8bfb\uff0c\u800c\u4f7f\u7528 using \u521b\u5efa\u7684\u7b49\u4ef7\u7684\u7c7b\u578b\u522b\u540d\u5219\u66f4\u5bb9\u6613\u9605\u8bfb\uff1a 1 2 typedef int ( * fcn_t )( double , char ); // fcn_t hard to find using fcn_t = int ( * )( double , char ); // fcn_t easier to find \u5728\u4e0a\u9762\u7684 typedef \u5b9a\u4e49\u4e2d\uff0c\u65b0\u7c7b\u578b( fcn_t )\u88ab\u85cf\u5728\u4e86\u5b9a\u4e49\u7684\u4e2d\u95f4\u90e8\u5206\uff0c\u8fd9\u4f7f\u5f97\u8be5\u5b9a\u4e49\u53d8\u5f97\u5f88\u96be\u9605\u8bfb\u3002 \u7b2c\u4e09\uff0c\u201ctypedef\u201d \u8fd9\u4e2a\u770b\u8d77\u6765\u50cf\u662f\u8981\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u7c7b\u578b\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\uff0c\u4ece\u4e4b\u524d\u7684\u4f8b\u5b50\u53ef\u4ee5\u770b\u51fa\uff0c\u7c7b\u578b\u548c\u7c7b\u578b\u522b\u540d\u662f\u53ef\u4ee5\u4e92\u6362\u4f7f\u7528\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u521b\u5efa\u7c7b\u578b\u522b\u540d\u65f6\uff0c\u5c3d\u91cf\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u8bed\u6cd5\u800c\u4e0d\u662f typedef \u8bed\u6cd5\u3002","title":"Typedef"},{"location":"8-6-Typedefs-and-type-aliases/#_4","text":"\u5728\u4e86\u89e3\u4e86\u4ec0\u4e48\u662f\u7c7b\u578b\u522b\u540d\u4e4b\u540e\uff0c\u662f\u65f6\u5019\u8be5\u804a\u804a\uff0c\u5408\u9002\u5e94\u8be5\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u5462\uff1f","title":"\u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u4f7f\u7528\u7c7b\u578b\u522b\u540d\uff1f"},{"location":"8-6-Typedefs-and-type-aliases/#_5","text":"\u7c7b\u578b\u522b\u540d\u7684\u4e00\u4e2a\u7528\u9014\u662f\uff0c\u5b83\u4eec\u53ef\u4ee5\u7528\u6765\u9690\u85cf\u5e73\u53f0\u7279\u5b9a\u7684\u7ec6\u8282\u3002\u5728\u67d0\u4e9b\u5e73\u53f0\u4e0a\uff0c int \u4e3a2\u5b57\u8282\uff0c\u800c\u5728\u5176\u4ed6\u5e73\u53f0\u4e0a\u4e3a 4 \u5b57\u8282\u3002\u56e0\u6b64\uff0c\u5728\u7f16\u5199\u4e0e\u5e73\u53f0\u65e0\u5173\u7684\u4ee3\u7801\u65f6\uff0c\u4f7f\u7528 int \u5b58\u50a8\u8d85\u8fc7 2 \u5b57\u8282\u7684\u4fe1\u606f\u53ef\u80fd\u4f1a\u6709\u6f5c\u5728\u7684\u5371\u9669\u3002 \u56e0\u4e3a\u4ece\u5b57\u9762\u4e0a\u5e76\u4e0d\u80fd\u770b\u51fa char \u3001 short \u3001 int \u548c long \u7684\u5927\u5c0f\uff0c\u6240\u4ee5\u8de8\u5e73\u53f0\u7a0b\u5e8f\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u6765\u5b9a\u4e49\u4ee5\u4f4d\u4e3a\u5355\u4f4d\u5305\u542b\u7c7b\u578b\u5927\u5c0f\u7684\u522b\u540d\u662f\u76f8\u5f53\u5e38\u89c1\u7684\u3002\u4f8b\u5982\uff0c int8_t \u662f\u4e00\u4e2a 8 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\uff0c int16_t \u662f\u4e00\u4e2a 16 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\uff0c int32_t \u662f\u4e00\u4e2a32\u4f4d\u6709\u7b26\u53f7\u6574\u6570\u3002\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u6709\u52a9\u4e8e\u9632\u6b62\u9519\u8bef\uff0c\u5e76\u66f4\u6e05\u695a\u5730\u8bf4\u660e\u5bf9\u53d8\u91cf\u5927\u5c0f\u7684\u671f\u671b\u503c\u662f\u591a\u5c11\u3002 \u4e3a\u4e86\u786e\u4fdd\u6bcf\u4e2a\u522b\u540d\u7c7b\u578b\u89e3\u6790\u4e3a\u6b63\u786e\u5927\u5c0f\u7684\u7c7b\u578b\uff0c\u8fd9\u7c7b\u7c7b\u578b\u522b\u540d\u901a\u5e38\u4e0e\u9884\u5904\u7406\u5668\u6307\u4ee4\u4e00\u8d77\u4f7f\u7528: 1 2 3 4 5 6 7 8 9 #ifdef INT_2_BYTES using int8_t = char ; using int16_t = int ; using int32_t = long ; #else using int8_t = char ; using int16_t = short ; using int32_t = int ; #endif \u5728\u67d0\u4e9b\u673a\u5668\u4e0a\uff0c\u6574\u578b\u53ea\u5360 2 \u4e2a\u5b57\u8282\uff0c\u8fd9\u65f6\u5b8f INT_2_BYTES \u662f\u88ab #defined \u8fc7\u7684\uff0c\u6240\u4ee5\u7a0b\u5e8f\u5728\u7f16\u8bd1\u65f6\u4f1a\u5c06\u8fd9\u6bb5\u4ee3\u7801\u7684\u4e0a\u534a\u90e8\u5206\u5b9a\u4e49\u7684\u7c7b\u578b\u522b\u540d\u8fdb\u884c\u7f16\u8bd1\u3002\u5728\u6574\u578b\u5360 4 \u4e2a\u5b57\u8282\u7684\u673a\u5668\u4e0a\uff0c \u5b8f INT_2_BYTES \u4e0d\u5b58\u5728\uff0c\u56e0\u6b64\u4e0b\u534a\u90e8\u5206\u7684\u7c7b\u578b\u522b\u540d\u4f1a\u88ab\u7f16\u8bd1\u3002 \u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528 char \u3001 short \u3001 int \u548c long \u7684\u7ec4\u5408\u5728\u7279\u5b9a\u7684\u673a\u5668\u4e0a\u5c06 int8_t \u89e3\u6790\u4e3a 1 \u4e2a\u5b57\u8282\u7684\u6574\u6570\uff0c int16_t \u89e3\u6790\u4e3a 2 \u4e2a\u5b57\u8282\u7684\u6574\u6570\uff0c int32_t \u89e3\u6790\u4e3a 4 \u4e2a\u5b57\u8282\u7684\u6574\u6570\u3002 \u5bf9\u4e8e\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b (\u4f8b\u5982 std::int_fast16_t \u548c std::int_least32_t ) \u548c size_t \u7c7b\u578b (\u53c2\u89c1\uff1a 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t ) \u5176\u5b9e\u4e5f\u6b63\u662f\u8fd9\u4e9b\u57fa\u7840\u7c7b\u578b\u7684\u522b\u540d\u7f62\u4e86\u3002 \u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728\u4f7f\u7528 std::cout \u6253\u5370 8 \u4f4d\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u7684\u65f6\u5019\uff0c\u4f60\u591a\u534a\u4f1a\u6253\u5370\u51fa\u4e00\u4e2a\u5b57\u7b26\u7684\u539f\u56e0\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 #include // for fixed-width integers #include int main () { std :: int_least8_t x { 97 }; // int_least8_t \u5b9e\u9645\u4e0a\u662f char \u7684\u7c7b\u578b\u522b\u540d std :: cout << x ; return 0 ; } \u7a0b\u5e8f\u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 a \u56e0\u4e3a std::int_least8_t \u901a\u5e38\u88ab\u5b9a\u4e49\u4e3a char \u7684\u7c7b\u578b\u522b\u540d\uff0c\u53d8\u91cf x \u5c06\u88ab\u5b9a\u4e49\u4e3a char \u3002 \u800c char \u7c7b\u578b\u5c06\u5176\u503c\u6253\u5370\u4e3aASCII\u5b57\u7b26\uff0c\u800c\u4e0d\u662f\u6574\u6570\u503c\u3002","title":"\u5728\u5e73\u53f0\u65e0\u5173\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528\u7c7b\u578b\u522b\u540d"},{"location":"8-6-Typedefs-and-type-aliases/#_6","text":"\u867d\u7136\u6211\u4eec\u76ee\u524d\u53ea\u4f7f\u7528\u8fc7\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u662f\u5bf9\u4e8e\u9ad8\u9636C++\u7f16\u7a0b\u6765\u8bf4\uff0c\u7c7b\u578b\u901a\u5e38\u662f\u590d\u6742\u4e14\u5197\u957f\u7684\uff0c\u5982\u679c\u4f60\u624b\u52a8\u8f93\u5165\u5b83\u4eec\u4f1a\u975e\u5e38\u8d39\u52b2\u3002\u4f8b\u5982\uff0c\u6709\u4e9b\u51fd\u6570\u548c\u53d8\u91cf\u7684\u5b9a\u4e49\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // for std::string #include // for std::vector #include // for std::pair bool hasDuplicates ( std :: vector < std :: pair < std :: string , int >> pairlist ) { // some code here return false ; } int main () { std :: vector < std :: pair < std :: string , int >> pairlist ; return 0 ; } \u5728\u6bcf\u4e2a\u9700\u8981\u7684\u5730\u65b9\u8f93\u5165 std::vector> \u4e0d\u4ec5\u7e41\u7410\uff0c\u800c\u4e14\u975e\u5e38\u5bb9\u6613\u51fa\u9519\u3002\u5982\u679c\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u5219\u7b80\u5355\u7684\u591a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include // for std::string #include // for std::vector #include // for std::pair using pairlist_t = std :: vector < std :: pair < std :: string , int >> ; // make pairlist_t an alias for this crazy type bool hasDuplicates ( pairlist_t pairlist ) // use pairlist_t in a function parameter { // some code here return false ; } int main () { pairlist_t pairlist ; // instantiate a pairlist_t variable return 0 ; } \u770b\u8d77\u6765\u597d\u591a\u4e86\u5bf9\u5427\uff01\u73b0\u5728\uff0c\u4f60\u9700\u8981\u8f93\u5165\u7684\u662f pairlist_t \u800c\u4e0d\u662f std::vector> \u3002 \u5982\u679c\u4f60\u8fd8\u4e0d\u77e5\u9053 std::vector \u3001 std::pair \u6216\u8005\u90a3\u4e9b\u770b\u8d77\u6765\u5f88\u5413\u4eba\u7684\u5c16\u62ec\u53f7\u662f\u4ec0\u4e48\uff0c\u4e5f\u4e0d\u7528\u62c5\u5fc3\u3002\u73b0\u5728\u4f60\u53ea\u9700\u8981\u77e5\u9053\u7c7b\u578b\u522b\u540d\u53ef\u4ee5\u7b80\u5316\u7c7b\u578b\u3001\u4f7f\u4f60\u7684\u4ee3\u7801\u66f4\u6613\u8bfb\uff0c\u51cf\u5c11\u7e41\u7410\u7684\u8f93\u5165\u5c31\u53ef\u4ee5\u4e86\u3002 \u8fd9\u4e5f\u662f\u7c7b\u578b\u522b\u540d\u6700\u6709\u7528\u7684\u5730\u65b9\u3002","title":"\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u7b80\u5316\u7c7b\u578b"},{"location":"8-6-Typedefs-and-type-aliases/#_7","text":"\u7c7b\u578b\u522b\u540d\u8fd8\u6709\u52a9\u4e8e\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8df5\u884c\u4ee3\u7801\u5373\u6587\u6863\u7684\u601d\u60f3\u3002 With variables, we have the variable\u2019s identifier to help document the purpose of the variable. But consider the case of a function\u2019s return value. Data types such as char , int , long , double , and bool are good for describing what type a function returns, but more often we want to know what purpose a return value serves. \u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7\u6807\u8bc6\u7b26\uff08\u53d8\u91cf\u540d\uff09\u6765\u5e2e\u52a9\u8868\u793a\u5b83\u7684\u7528\u9014\u3002\u4f46\u662f\u5bf9\u4e8e\u51fd\u6570\u8fd4\u56de\u503c\u6765\u8bf4\uff0c\u5c3d\u7ba1 char \u3001 int \u3001 long \u3001 double \u548c bool \u53ef\u4ee5\u8868\u660e\u51fd\u6570\u503c\u7684\u7c7b\u578b\uff0c\u4f46\u5b83\u4eec\u5e76\u6ca1\u6709\u529e\u6cd5\u5f88\u597d\u5730\u8868\u660e\u51fd\u6570\u8fd4\u56de\u503c\u7684\u7528\u9014\u7a76\u7adf\u662f\u4ec0\u4e48\u3002 \u5bf9\u4e8e\u4e0b\u9762\u7684\u51fd\u6570\u6765\u8bf4\uff1a 1 int gradeTest (); \u51fd\u6570\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u662f\u6574\u578b\uff0c\u4f46\u662f\u8fd9\u4e2a\u6574\u578b\u8fd4\u56de\u503c\u7684\u542b\u4e49\u5230\u5e95\u662f\u4ec0\u4e48\u5462\uff1f\u662f\u5b66\u751f\u7684\u5206\u6570\uff1f\u662f\u6ca1\u6709\u4f5c\u7b54\u7684\u95ee\u9898\u6570\uff1f\u662f\u5b66\u53f7\uff1f\u8fd8\u662f\u9519\u8bef\u7801\uff1f\u6ca1\u6709\u4eba\u77e5\u9053\uff0c\u56e0\u4e3a int \u5e76\u4e0d\u80fd\u8868\u8fbe\u8fd9\u5c42\u542b\u4e49\u3002\u5982\u679c\u5e78\u8fd0\u7684\u8bdd\uff0c\u4e5f\u8bb8\u51fd\u6570\u6709\u6587\u6863\u53ef\u4f9b\u6211\u4eec\u53c2\u8003\u3002\u5982\u679c\u4e0d\u5e78\u7684\u8bdd\uff0c\u4f60\u5c31\u5fc5\u987b\u9605\u8bfb\u6e90\u7801\u7136\u540e\u53bb\u731c\u6d4b\u5b83\u7684\u7528\u9014\u4e86\u3002 \u800c\u5bf9\u4e8e\u4e0b\u9762\u8fd9\u4e2a\u4f7f\u7528\u4e86\u7c7b\u578b\u522b\u540d\u7684\u7b49\u4ef7\u7684\u4f8b\u5b50\u6765\u8bf4\uff1a 1 2 using testScore_t = int ; testScore_t gradeTest (); \u8fd4\u56de\u503c\u7c7b\u578b\u4e3a testScore_t \uff0c\u5b83\u80fd\u591f\u66f4\u597d\u5730\u8868\u660e\u51fd\u6570\u7684\u8fd4\u56de\u503c\u8868\u793a\u4e00\u6b21\u6d4b\u9a8c\u7684\u5206\u6570\u3002 \u6839\u636e\u6211\u4eec\u7684\u7ecf\u9a8c\uff0c\u521b\u5efa\u7c7b\u578b\u522b\u540d\u53ea\u662f\u4e3a\u4e86\u8bb0\u5f55\u5355\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u662f\u4e0d\u503c\u5f97\u7684(\u4f7f\u7528\u6ce8\u91ca\u4ee3\u66ff)\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u5df2\u7ecf\u56e0\u4e3a\u5176\u4ed6\u539f\u56e0\u521b\u5efa\u4e86\u7c7b\u578b\u522b\u540d\uff0c\u8fd9\u53ef\u80fd\u662f\u4e00\u4e2a\u989d\u5916\u597d\u5904\u3002","title":"\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u8ba9\u7a0b\u5e8f\u66f4\u6e05\u6670"},{"location":"8-6-Typedefs-and-type-aliases/#_8","text":"\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u8fd8\u53ef\u4ee5\u5728\u4e0d\u8fdb\u884c\u5927\u91cf\u6539\u52a8\u7684\u60c5\u51b5\u4e0b\u4fee\u6539\u5bf9\u8c61\u7684\u57fa\u672c\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5b66\u751f\u7684ID\u539f\u672c\u4f7f\u7528 short \u6765\u4fdd\u5b58\uff0c\u4f46\u540e\u6765\u51b3\u5b9a\u7528 long \u6765\u4ee3\u66ff\uff0c\u6b64\u65f6\u4f60\u5c31\u5fc5\u987b\u5c06\u5927\u91cf\u7684 short \u66ff\u6362\u4e3a long \u3002\u8fd9\u4e2a\u65f6\u5019\u5f80\u5f80\u5f88\u96be\u786e\u5b9a\u7a76\u7adf\u54ea\u4e9b short \u662f\u7528\u6765\u4fdd\u5b58ID\u7684\uff0c\u800c\u54ea\u4e9b\u5219\u7528\u4e8e\u5176\u4ed6\u76ee\u7684\u3002 \u7136\u800c\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u4e86\u7c7b\u578b\u522b\u540d\uff0c\u90a3\u4e48\u66f4\u6539\u7c7b\u578b\u5c31\u50cf\u66f4\u65b0\u7c7b\u578b\u522b\u540d\u4e00\u6837\u7b80\u5355(\u4f8b\u5982\uff0c\u5c06 using studentID_t = short; \u4fee\u6539\u4e3a using studentID_t = long; )\u3002 \u867d\u7136\u8fd9\u770b\u8d77\u6765\u662f\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u7279\u6027\uff0c\u4f46\u6bcf\u5f53\u7c7b\u578b\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u6211\u4eec\u4ecd\u7136\u9700\u8981\u4fdd\u6301\u8c28\u614e\uff0c\u56e0\u4e3a\u7a0b\u5e8f\u7684\u884c\u4e3a\u4e5f\u53ef\u80fd\u968f\u4e4b\u53d1\u751f\u53d8\u5316\u3002\u5f53\u5c06\u7c7b\u578b\u522b\u540d\u7684\u7c7b\u578b\u66f4\u6539\u4e3a\u4e0d\u540c\u7c7b\u578b\u65cf\u4e2d\u7684\u7c7b\u578b\u65f6\u5c24\u5176\u5982\u6b64(\u4f8b\u5982\uff0c\u5c06\u6574\u6570\u66f4\u6539\u4e3a\u6d6e\u70b9\u503c\uff0c\u6216\u53cd\u4e4b\u4ea6\u7136)\uff01\u65b0\u7c7b\u578b\u53ef\u80fd\u6709\u6d6e\u70b9\u6570\u6bd4\u8f83\u6216\u6574\u6570/\u6d6e\u70b9\u9664\u6cd5\u95ee\u9898\uff0c\u6216\u5176\u4ed6\u65e7\u7c7b\u578b\u6240\u6ca1\u6709\u7684\u95ee\u9898\u3002\u5982\u679c\u4f60\u5c06\u73b0\u6709\u7684\u7c7b\u578b\u66f4\u6539\u4e3a\u5176\u4ed6\u7c7b\u578b\uff0c\u4ee3\u7801\u5e94\u8be5\u91cd\u65b0\u3001\u5f7b\u5e95\u5730\u6d4b\u8bd5\u3002","title":"\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u63d0\u9ad8\u4ee3\u7801\u53ef\u7ef4\u62a4\u6027"},{"location":"8-6-Typedefs-and-type-aliases/#_9","text":"\u7136\u7c7b\u578b\u522b\u540d\u63d0\u4f9b\u4e86\u4e00\u4e9b\u597d\u5904\uff0c\u4f46\u5b83\u4eec\u4e5f\u5728\u4ee3\u7801\u4e2d\u5f15\u5165\u4e86\u53e6\u4e00\u4e2a\u9700\u8981\u7406\u89e3\u7684\u6807\u8bc6\u7b26\u3002\u5982\u679c\u8fd9\u4e00\u70b9\u6ca1\u6709\u88ab\u53ef\u8bfb\u6027\u6216\u7406\u89e3\u7684\u597d\u5904\u6240\u62b5\u6d88\uff0c\u90a3\u4e48\u7c7b\u578b\u522b\u540d\u5f0a\u5927\u4e8e\u5229\u3002 \u7c7b\u578b\u522b\u540d\u4f7f\u7528\u4e0d\u5f53\u7684\u8bdd\uff0c\u4f1a\u5c06\u719f\u6089\u7684\u7c7b\u578b(\u4f8b\u5982 std::string )\u9690\u85cf\u5728\u81ea\u5b9a\u4e49\u540d\u79f0\u540e\u9762\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b(\u4f8b\u5982\u667a\u80fd\u6307\u9488\uff0c\u6211\u4eec\u5c06\u5728\u4ee5\u540e\u7684\u7ae0\u8282\u4e2d\u8ba8\u8bba)\uff0c\u6a21\u7cca\u7684\u7c7b\u578b\u4fe1\u606f\u4e5f\u53ef\u80fd\u4e0d\u5229\u4e8e\u7406\u89e3\u7c7b\u578b\u5e94\u8be5\u5982\u4f55\u5de5\u4f5c\u3002 \u56e0\u6b64\uff0c\u7c7b\u578b\u522b\u540d\u5e94\u8be5\u4e3b\u8981\u7528\u4e8e\u5bf9\u4ee3\u7801\u53ef\u8bfb\u6027\u6216\u4ee3\u7801\u7ef4\u62a4\u6709\u660e\u663e\u597d\u5904\u7684\u60c5\u51b5\u3002\u8fd9\u65e2\u662f\u4e00\u95e8\u79d1\u5b66\uff0c\u4e5f\u662f\u4e00\u95e8\u827a\u672f\u3002\u5f53\u7c7b\u578b\u522b\u540d\u53ef\u4ee5\u5728\u4ee3\u7801\u7684\u8bb8\u591a\u5730\u65b9\u4f7f\u7528\u800c\u4e0d\u662f\u5728\u5be5\u5be5\u51e0\u5904\u4f7f\u7528\u65f6\uff0c\u624d\u662f\u5b83\u4eec\u5927\u653e\u5f02\u5f69\u7684\u673a\u4f1a\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u7c7b\u578b\u522b\u540d\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\u3001\u4ee3\u7801\u7ef4\u62a4\u6027\u65f6\uff0c\u660e\u667a\u800c\u5ba1\u614e\u5730\u4f7f\u7528\u5b83\u4eec\u3002","title":"\u7f3a\u70b9\u548c\u7ed3\u8bba"},{"location":"8-7-Type-deduction-for-objects-using-the%20auto-keyword/","tags":["type deduction","data type","auto"],"text":"Key Takeaway \u7c7b\u578b\u63a8\u65ad\u5fc5\u987b\u914d\u5408\u53d8\u91cf\u521d\u59cb\u5316\u503c\u4f7f\u7528\uff0c\u8fd9\u6837\u53ef\u4ee5\u5fd8\u8bb0\u521d\u59cb\u5316\u7684\u53d8\u91cf \u7c7b\u578b\u63a8\u65ad\u4f1a\u4e22\u5f03 const \uff0c\u5fc5\u987b\u989d\u5916\u6dfb\u52a0 const auto \u5982\u679c\u5e0c\u671b\u5b57\u7b26\u4e32\u63a8\u65ad\u51fa\u7684\u7c7b\u578b\u4e3a std:: string \u6216 std:: string_view \uff0c\u4f60\u9700\u8981\u4f7f\u7528 s \u6216\u8005 sv \u5b57\u9762\u91cf\u540e\u7f00 \u5728\u4e0b\u9762\u8fd9\u4e2a\u7b80\u5355\u7684\u53d8\u91cf\u5b9a\u4e49\u4e2d\uff0c\u5b58\u5728\u4e00\u4e2a\u96be\u4ee5\u5bdf\u89c9\u7684\u5197\u4f59\u70b9\uff1a 1 double d { 5.0 }; \u56e0\u4e3a C++ \u5c5e\u4e8e\u5f3a\u7c7b\u578b\u8bed\u8a00\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u4e3a\u6bcf\u4e00\u4e2a\u5bf9\u8c61\u6307\u660e\u5176\u7c7b\u578b\u3002\u56e0\u6b64\uff0c\u8fd9\u91cc\u6211\u4eec\u663e\u5f0f\u5730\u6307\u660e\u4e86\u53d8\u91cf d \u7684\u7c7b\u578b\u4e3a double \u3002 \u4f46\u662f\uff0c\u65e2\u7136\u88ab\u7528\u6765\u521d\u59cb\u5316 d \u7684 \u5b57\u9762\u91cf 5.0 \u5df2\u7ecf\u662f double \u7c7b\u578b\u7684\u4e86\uff0c\u96be\u9053\u518d\u4e3a d \u6307\u5b9a double \u4e0d\u591a\u4e8e\u5417\uff1f \u76f8\u5173\u5185\u5bb9 \u5173\u4e8e\u5b57\u9762\u91cf\u7684\u7c7b\u578b\uff0c\u53c2\u89c1 4.15 - \u5b57\u9762\u91cf \u3002 \u5982\u679c\u6211\u4eec\u5e0c\u671b\u4e00\u4e2a\u53d8\u91cf\u53ca\u5176\u521d\u59cb\u5316\u5f0f\u5177\u6709\u76f8\u540c\u7684\u7c7b\u578b\uff0c\u90a3\u4e48\u4e24\u6b21\u63d0\u4f9b\u4e86\u76f8\u540c\u7684\u7c7b\u578b\u4fe1\u606f\u5176\u5b9e\u662f\u591a\u4f59\u7684\u3002 \u53d8\u91cf\u521d\u59cb\u5316\u7684\u7c7b\u578b\u8f6c\u6362 \u7c7b\u578b\u63a8\u65ad\u662f\u4e00\u79cd\u5141\u8bb8\u7f16\u8bd1\u5668\u901a\u8fc7\u5bf9\u8c61\u7684\u521d\u59cb\u5316\u503c\u63a8\u65ad\u5bf9\u8c61\u7c7b\u578b\u7684\u7279\u6027\u3002\u4f7f\u7528 auto \u5173\u952e\u5b57\u4ee3\u66ff\u53d8\u91cf\u7684\u7c7b\u578b\u5c31\u53ef\u4ee5\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u3002 1 2 3 4 5 6 7 8 int main () { auto d { 5.0 }; // 5.0 is a double literal, so d will be type double auto i { 1 + 2 }; // 1 + 2 evaluates to an int, so i will be type int auto x { i }; // i is an int, so x will be type int too return 0 ; } \u5728\u7b2c\u4e00\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a 5.0 \u662f double \u7c7b\u578b\u7684\u5b57\u9762\u91cf\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u53ef\u4ee5\u63a8\u65ad\u51fa\u53d8\u91cf d \u4e5f\u5e94\u8be5\u662f double \u7c7b\u578b\u7684\u3002\u5728\u7b2c\u4e8c\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8868\u8fbe\u5f0f 1 + 2 \u4f1a\u6c42\u503c\u5f97\u5230\u4e00\u4e2a int \u7c7b\u7684\u89e3\uff0c\u56e0\u6b64\u53d8\u91cf i \u662f int \u7c7b\u578b\u7684\u3002\u5728\u7b2c\u4e09\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a i \u7684\u7c7b\u578b\u5df2\u7ecf\u88ab\u63a8\u65ad\u4e3a int \u7c7b\u578b\uff0c\u56e0\u6b64\u8fd9\u91cc\u7684 x \u81ea\u7136\u4e5f\u4f1a\u88ab\u63a8\u65ad\u4e3a int \u7c7b\u578b\u3002 \u56e0\u4e3a\u51fd\u6570\u8c03\u7528\u4e5f\u662f\u4e00\u79cd\u5408\u6cd5\u7684\u8868\u8fbe\u5f0f\uff0c\u6240\u4ee5\u51fd\u6570\u4f5c\u4e3a\u521d\u59cb\u5316\u503c\u65f6\uff0c\u4e5f\u53ef\u4ee5\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\uff1a 1 2 3 4 5 6 7 8 9 10 int add ( int x , int y ) { return x + y ; } int main () { auto sum { add ( 5 , 6 ) }; // add() returns an int, so sum's type will be deduced to int return 0 ; } add() \u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e3a int \uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u5c06\u53d8\u91cf sum \u7684\u7c7b\u578b\u63a8\u65ad\u4e3a int \u3002 \u5982\u679c\u5bf9\u8c61\u6ca1\u6709\u521d\u59cb\u5316\u503c\u6216\u4f7f\u7528\u4e86\u7a7a\u7684\u521d\u59cb\u5316\u503c\uff0c\u5219\u65e0\u6cd5\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\u3002\u4e0b\u9762\u7684\u4e24\u4e2a\u53d8\u91cf\u5b9a\u4e49\u662f\u975e\u6cd5\u7684\uff1a 1 2 3 4 5 6 7 int main () { auto x ; // The compiler is unable to deduce the type of x auto y { }; // The compiler is unable to deduce the type of y return 0 ; } \u867d\u7136\u5bf9\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u53ea\u80fd\u5c11\u6253\u51e0\u4e2a\u5b57\uff0c\u4f46\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4f1a\u770b\u5230\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7c7b\u578b\u53ef\u80fd\u4f1a\u53d8\u5f97\u590d\u6742\u548c\u5197\u957f\uff08\u6216\u8005\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u7c7b\u578b\u4f1a\u53d8\u5f97\u96be\u4ee5\u786e\u5b9a\uff09\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528 auto \u53ef\u4ee5\u8282\u7701\u5927\u91cf\u7684\u8f93\u5165(\u548c\u9519\u522b\u5b57)\u3002 \u76f8\u5173\u5185\u5bb9 \u6307\u9488\u548c\u5f15\u7528\u7684\u7c7b\u578b\u63a8\u65ad\u6709\u4e9b\u590d\u6742\uff0c\u6211\u4eec\u4f1a\u5728 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548c const \u7684\u7c7b\u578b\u63a8\u65ad \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u7c7b\u578b\u63a8\u65ad\u4f1a\u4e22\u5f03 const \u4fee\u9970\u7b26 \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7c7b\u578b\u63a8\u65ad\u4f1a\u4e22\u5f03 const \u4fee\u9970\u7b26\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 int main () { const int x { 5 }; // x has type const int auto y { x }; // y will be type int (const is dropped) } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c x \u7684\u7c7b\u578b\u672c\u6765\u662f const int \uff0c\u4f46\u662f\u5f53\u4f7f\u7528 x \u4f5c\u4e3a y \u7684\u521d\u59cb\u5316\u503c\u662f\uff0c\u7c7b\u578b\u63a8\u65ad\u5f97\u5230\u7684\u7ed3\u679c\u662f int \u800c\u4e0d\u662f int const \u3002 \u5982\u679c\u4f60\u60f3\u8981\u4e00\u4e2a\u63a8\u65ad\u7684\u7c7b\u578b\u662f const \uff0c\u4f60\u5fc5\u987b\u81ea\u5df1\u63d0\u4f9b const \u3002\u4e3a\u6b64\uff0c\u53ea\u9700\u5c06\u5173\u952e\u5b57 const \u4e0e auto \u7ed3\u5408\u4f7f\u7528\u5373\u53ef\uff1a 1 2 3 4 5 6 7 int main () { const int x { 5 }; // x has type const int auto y { x }; // y will be type int (const is dropped) const auto z { x }; // z will be type const int (const is reapplied) } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528 x \u4f5c\u4e3a\u521d\u59cb\u5316\u503c\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\u7684\u7ed3\u679c\u662f int ( const \u88ab\u4e22\u5f03\u4e86)\uff0c\u4f46\u662f\u56e0\u4e3a\u6211\u4eec\u5728\u5b9a\u4e49\u53d8\u91cf z \u65f6\u91cd\u65b0\u6dfb\u52a0\u4e86 const \u4fee\u9970\u7b26\uff0c\u6240\u4ee5 z \u7684\u7c7b\u578b\u4e3a const int \u3002 string \u5b57\u9762\u91cf\u7684\u7c7b\u578b\u63a8\u65ad \u51fa\u4e8e\u67d0\u4e9b\u5386\u53f2\u539f\u56e0\uff0c\u5b57\u7b26\u4e32\u5728 C++ \u4e2d\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u7c7b\u578b\u3002\u56e0\u6b64\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u80fd\u5e76\u4e0d\u80fd\u5982\u4f60\u6240\u613f\u5730\u5de5\u4f5c\uff1a 1 auto s { \"Hello, world\" }; // s will be type const char*, not std::string \u5982\u679c\u4f60\u5e0c\u671b\u63a8\u65ad\u51fa\u7684\u7c7b\u578b\u4e3a std:: string \u6216 std:: string_view \uff0c\u4f60\u9700\u8981\u4f7f\u7528 s \u6216\u8005 sv \u5b57\u9762\u91cf\u540e\u7f00 (\u53c2\u8003\uff1a 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf ): 1 2 3 4 5 6 7 8 9 10 11 12 #include #include int main () { using namespace std :: literals ; // easiest way to access the s and sv suffixes auto s1 { \"goo\" s }; // \"goo\"s is a std::string literal, so s1 will be deduced as a std::string auto s2 { \"moo\" sv }; // \"moo\"sv is a std::string_view literal, so s2 will be deduced as a std::string_view return 0 ; } \u7c7b\u578b\u63a8\u65ad\u7684\u4f18\u7f3a\u70b9 \u7c7b\u578b\u63a8\u65ad\u4e0d\u4ec5\u65b9\u4fbf\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u6709\u7528\u7684\u5730\u65b9\u3002 \u9996\u5148\uff0c\u5728\u8fde\u7eed\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\u7684\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u53ef\u4ee5\u8ba9\u53d8\u91cf\u540d\u5bf9\u9f50\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff1a 1 2 3 4 5 6 7 // harder to read int a { 5 }; double b { 6.7 }; // easier to read auto c { 5 }; auto d { 6.7 }; \u5176\u6b21\uff0c\u7c7b\u578b\u63a8\u65ad\u53ea\u80fd\u591f\u5728\u5177\u6709\u53d8\u91cf\u521d\u59cb\u5316\u503c\u662f\u624d\u80fd\u4f7f\u7528\uff0c\u5982\u679c\u6211\u4eec\u517b\u6210\u4e86\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u7684\u4e60\u60ef\u7684\u8bdd\uff0c\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u907f\u514d\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\uff1a 1 2 int x ; // oops, we forgot to initialize x, but the compiler may not complain auto y ; // the compiler will error out because it can't deduce a type for y \u7b2c\u4e09\uff0c\u901a\u8fc7\u7c7b\u578b\u63a8\u65ad\uff0c\u53ef\u4ee5\u786e\u4fdd\u4e0d\u53d1\u751f\u5bf9\u6027\u80fd\u4ea7\u751f\u5f71\u54cd\u7684\u7c7b\u578b\u8f6c\u6362\uff1a 1 2 double x { 5 }; // \u4e0d\u597d: int \u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a double auto y { 5 }; // \u597d\uff1ay \u662f int \u5e76\u4e0d\u4f1a\u53d1\u751f\u7c7b\u578b\u8f6c\u6362 \u5f53\u7136\uff0c\u7c7b\u578b\u63a8\u65ad\u4e5f\u6709\u4e00\u4e9b\u7f3a\u70b9\u3002 \u9996\u5148\uff0c\u7c7b\u578b\u63a8\u65ad\u6a21\u7cca\u5316\u4e86\u4ee3\u7801\u4e2d\u7684\u7c7b\u578b\u4fe1\u606f\u3002\u5c3d\u7ba1\u4f18\u79c0\u7684 IDE \u53ef\u4ee5\u663e\u793a\u63a8\u65ad\u540e\u7684\u7c7b\u578b\uff08\u4f8b\u5982\uff0c\u5f53\u9f20\u6807\u60ac\u6d6e\u5728\u53d8\u91cf\u4e0a\u65f6\uff09\uff0c\u4f46\u662f\u5728\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u65f6\u66f4\u5bb9\u6613\u53d1\u751f\u4e0e\u7c7b\u578b\u76f8\u5173\u7684\u9519\u8bef\u3002 \u4f8b\u5982\uff1a 1 auto y { 5 }; // oops, we wanted a double here but we accidentally provided an int literal \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u5982\u679c\u6211\u4eec\u663e\u5f0f\u5730\u5c06 y \u6307\u5b9a\u4e3a double \u7c7b\u578b\uff0c\u90a3\u4e48\u5373\u4f7f\u6211\u4eec\u610f\u5916\u5730\u4e3a y \u63d0\u4f9b\u4e86\u4e00\u4e2a int \u5b57\u9762\u91cf\u7684\u521d\u59cb\u5316\u503c\uff0c y \u4ecd\u7136\u8fd8\u662f double \u7c7b\u578b\uff0c\u800c\u5982\u679c\u4f7f\u7528\u7c7b\u578b\u63a8\u6f14\uff0c y \u5c06\u88ab\u63a8\u6f14\u4e3a int \u7c7b\u578b\u3002 \u518d\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { auto x { 3 }; auto y { 2 }; std :: cout << x / y ; // oops, we wanted floating point division here return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u5c31\u4e0d\u5bb9\u6613\u770b\u51fa\u4e0b\u9762\u8fdb\u884c\u7684\u662f\u6574\u6570\u9664\u6cd5\u800c\u4e0d\u662f\u6d6e\u70b9\u9664\u6cd5\u3002 \u5176\u6b21\uff0c\u5982\u679c\u521d\u59cb\u5316\u5f0f\u7684\u7c7b\u578b\u6539\u53d8\uff0c\u4f7f\u7528\u7c7b\u578b\u6f14\u7ece\u7684\u53d8\u91cf\u7684\u7c7b\u578b\u4e5f\u4f1a\u6539\u53d8\uff0c\u6b64\u65f6\u53ef\u80fd\u4f1a\u53d1\u751f\u610f\u5916\u7684\u95ee\u9898\u3002\u4f8b\u5982\uff1a 1 auto sum { add ( 5 , 6 ) + gravity }; \u5982\u679c add \u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4ece int \u88ab\u4fee\u6539\u4e3a double \u4e86\uff0c\u6216\u8005 gravity \u7684\u7c7b\u578b\u4ece int \u88ab\u4fee\u6539\u4e3a double \u4e86\uff0c\u90a3\u4e48 sum \u7684\u7c7b\u578b\u540c\u6837\u4f1a\u88ab\u91cd\u65b0\u63a8\u65ad\u4e3a double \u3002 \u603b\u7684\u6765\u8bf4\uff0c\u73b0\u4ee3 C++ \u7684\u5171\u8bc6\u662f\uff0c\u7c7b\u578b\u6f14\u7ece\u7528\u4e8e\u5bf9\u8c61\u901a\u5e38\u662f\u5b89\u5168\u7684\uff0c\u8fd9\u6837\u505a\u53ef\u4ee5\u51cf\u5c11\u5bf9\u7c7b\u578b\u4fe1\u606f\u7684\u5f3a\u8c03\uff0c\u4ece\u800c\u66f4\u597d\u5730\u51f8\u663e\u4ee3\u7801\u7684\u903b\u8f91\uff0c\u4f9d\u6b21\u6765\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u53d8\u91cf\u4f7f\u7528\u7c7b\u578b\u6f14\u7ece\uff0c\u9664\u975e\u9700\u8981\u786e\u4fdd\u4f7f\u7528\u7279\u5b9a\u7684\u7c7b\u578b\u3002 \u4f5c\u8005\u6ce8 \u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\uff0c\u5f53\u6211\u4eec\u8ba4\u4e3a\u663e\u793a\u7c7b\u578b\u4fe1\u606f\u6709\u52a9\u4e8e\u7406\u89e3\u6982\u5ff5\u6216\u793a\u4f8b\u65f6\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u4f7f\u7528\u663e\u5f0f\u7c7b\u578b\uff0c\u800c\u4e0d\u662f\u7c7b\u578b\u63a8\u65ad\u3002","title":"8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad"},{"location":"8-7-Type-deduction-for-objects-using-the%20auto-keyword/#_1","text":"\u7c7b\u578b\u63a8\u65ad\u662f\u4e00\u79cd\u5141\u8bb8\u7f16\u8bd1\u5668\u901a\u8fc7\u5bf9\u8c61\u7684\u521d\u59cb\u5316\u503c\u63a8\u65ad\u5bf9\u8c61\u7c7b\u578b\u7684\u7279\u6027\u3002\u4f7f\u7528 auto \u5173\u952e\u5b57\u4ee3\u66ff\u53d8\u91cf\u7684\u7c7b\u578b\u5c31\u53ef\u4ee5\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u3002 1 2 3 4 5 6 7 8 int main () { auto d { 5.0 }; // 5.0 is a double literal, so d will be type double auto i { 1 + 2 }; // 1 + 2 evaluates to an int, so i will be type int auto x { i }; // i is an int, so x will be type int too return 0 ; } \u5728\u7b2c\u4e00\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a 5.0 \u662f double \u7c7b\u578b\u7684\u5b57\u9762\u91cf\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u53ef\u4ee5\u63a8\u65ad\u51fa\u53d8\u91cf d \u4e5f\u5e94\u8be5\u662f double \u7c7b\u578b\u7684\u3002\u5728\u7b2c\u4e8c\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8868\u8fbe\u5f0f 1 + 2 \u4f1a\u6c42\u503c\u5f97\u5230\u4e00\u4e2a int \u7c7b\u7684\u89e3\uff0c\u56e0\u6b64\u53d8\u91cf i \u662f int \u7c7b\u578b\u7684\u3002\u5728\u7b2c\u4e09\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a i \u7684\u7c7b\u578b\u5df2\u7ecf\u88ab\u63a8\u65ad\u4e3a int \u7c7b\u578b\uff0c\u56e0\u6b64\u8fd9\u91cc\u7684 x \u81ea\u7136\u4e5f\u4f1a\u88ab\u63a8\u65ad\u4e3a int \u7c7b\u578b\u3002 \u56e0\u4e3a\u51fd\u6570\u8c03\u7528\u4e5f\u662f\u4e00\u79cd\u5408\u6cd5\u7684\u8868\u8fbe\u5f0f\uff0c\u6240\u4ee5\u51fd\u6570\u4f5c\u4e3a\u521d\u59cb\u5316\u503c\u65f6\uff0c\u4e5f\u53ef\u4ee5\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\uff1a 1 2 3 4 5 6 7 8 9 10 int add ( int x , int y ) { return x + y ; } int main () { auto sum { add ( 5 , 6 ) }; // add() returns an int, so sum's type will be deduced to int return 0 ; } add() \u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e3a int \uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u5c06\u53d8\u91cf sum \u7684\u7c7b\u578b\u63a8\u65ad\u4e3a int \u3002 \u5982\u679c\u5bf9\u8c61\u6ca1\u6709\u521d\u59cb\u5316\u503c\u6216\u4f7f\u7528\u4e86\u7a7a\u7684\u521d\u59cb\u5316\u503c\uff0c\u5219\u65e0\u6cd5\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\u3002\u4e0b\u9762\u7684\u4e24\u4e2a\u53d8\u91cf\u5b9a\u4e49\u662f\u975e\u6cd5\u7684\uff1a 1 2 3 4 5 6 7 int main () { auto x ; // The compiler is unable to deduce the type of x auto y { }; // The compiler is unable to deduce the type of y return 0 ; } \u867d\u7136\u5bf9\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u53ea\u80fd\u5c11\u6253\u51e0\u4e2a\u5b57\uff0c\u4f46\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4f1a\u770b\u5230\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7c7b\u578b\u53ef\u80fd\u4f1a\u53d8\u5f97\u590d\u6742\u548c\u5197\u957f\uff08\u6216\u8005\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u7c7b\u578b\u4f1a\u53d8\u5f97\u96be\u4ee5\u786e\u5b9a\uff09\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528 auto \u53ef\u4ee5\u8282\u7701\u5927\u91cf\u7684\u8f93\u5165(\u548c\u9519\u522b\u5b57)\u3002 \u76f8\u5173\u5185\u5bb9 \u6307\u9488\u548c\u5f15\u7528\u7684\u7c7b\u578b\u63a8\u65ad\u6709\u4e9b\u590d\u6742\uff0c\u6211\u4eec\u4f1a\u5728 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548c const \u7684\u7c7b\u578b\u63a8\u65ad \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"\u53d8\u91cf\u521d\u59cb\u5316\u7684\u7c7b\u578b\u8f6c\u6362"},{"location":"8-7-Type-deduction-for-objects-using-the%20auto-keyword/#const","text":"\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7c7b\u578b\u63a8\u65ad\u4f1a\u4e22\u5f03 const \u4fee\u9970\u7b26\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 int main () { const int x { 5 }; // x has type const int auto y { x }; // y will be type int (const is dropped) } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c x \u7684\u7c7b\u578b\u672c\u6765\u662f const int \uff0c\u4f46\u662f\u5f53\u4f7f\u7528 x \u4f5c\u4e3a y \u7684\u521d\u59cb\u5316\u503c\u662f\uff0c\u7c7b\u578b\u63a8\u65ad\u5f97\u5230\u7684\u7ed3\u679c\u662f int \u800c\u4e0d\u662f int const \u3002 \u5982\u679c\u4f60\u60f3\u8981\u4e00\u4e2a\u63a8\u65ad\u7684\u7c7b\u578b\u662f const \uff0c\u4f60\u5fc5\u987b\u81ea\u5df1\u63d0\u4f9b const \u3002\u4e3a\u6b64\uff0c\u53ea\u9700\u5c06\u5173\u952e\u5b57 const \u4e0e auto \u7ed3\u5408\u4f7f\u7528\u5373\u53ef\uff1a 1 2 3 4 5 6 7 int main () { const int x { 5 }; // x has type const int auto y { x }; // y will be type int (const is dropped) const auto z { x }; // z will be type const int (const is reapplied) } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528 x \u4f5c\u4e3a\u521d\u59cb\u5316\u503c\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\u7684\u7ed3\u679c\u662f int ( const \u88ab\u4e22\u5f03\u4e86)\uff0c\u4f46\u662f\u56e0\u4e3a\u6211\u4eec\u5728\u5b9a\u4e49\u53d8\u91cf z \u65f6\u91cd\u65b0\u6dfb\u52a0\u4e86 const \u4fee\u9970\u7b26\uff0c\u6240\u4ee5 z \u7684\u7c7b\u578b\u4e3a const int \u3002","title":"\u7c7b\u578b\u63a8\u65ad\u4f1a\u4e22\u5f03 const \u4fee\u9970\u7b26"},{"location":"8-7-Type-deduction-for-objects-using-the%20auto-keyword/#string","text":"\u51fa\u4e8e\u67d0\u4e9b\u5386\u53f2\u539f\u56e0\uff0c\u5b57\u7b26\u4e32\u5728 C++ \u4e2d\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u7c7b\u578b\u3002\u56e0\u6b64\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u80fd\u5e76\u4e0d\u80fd\u5982\u4f60\u6240\u613f\u5730\u5de5\u4f5c\uff1a 1 auto s { \"Hello, world\" }; // s will be type const char*, not std::string \u5982\u679c\u4f60\u5e0c\u671b\u63a8\u65ad\u51fa\u7684\u7c7b\u578b\u4e3a std:: string \u6216 std:: string_view \uff0c\u4f60\u9700\u8981\u4f7f\u7528 s \u6216\u8005 sv \u5b57\u9762\u91cf\u540e\u7f00 (\u53c2\u8003\uff1a 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf ): 1 2 3 4 5 6 7 8 9 10 11 12 #include #include int main () { using namespace std :: literals ; // easiest way to access the s and sv suffixes auto s1 { \"goo\" s }; // \"goo\"s is a std::string literal, so s1 will be deduced as a std::string auto s2 { \"moo\" sv }; // \"moo\"sv is a std::string_view literal, so s2 will be deduced as a std::string_view return 0 ; }","title":"string\u5b57\u9762\u91cf\u7684\u7c7b\u578b\u63a8\u65ad"},{"location":"8-7-Type-deduction-for-objects-using-the%20auto-keyword/#_2","text":"\u7c7b\u578b\u63a8\u65ad\u4e0d\u4ec5\u65b9\u4fbf\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u6709\u7528\u7684\u5730\u65b9\u3002 \u9996\u5148\uff0c\u5728\u8fde\u7eed\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\u7684\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u53ef\u4ee5\u8ba9\u53d8\u91cf\u540d\u5bf9\u9f50\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff1a 1 2 3 4 5 6 7 // harder to read int a { 5 }; double b { 6.7 }; // easier to read auto c { 5 }; auto d { 6.7 }; \u5176\u6b21\uff0c\u7c7b\u578b\u63a8\u65ad\u53ea\u80fd\u591f\u5728\u5177\u6709\u53d8\u91cf\u521d\u59cb\u5316\u503c\u662f\u624d\u80fd\u4f7f\u7528\uff0c\u5982\u679c\u6211\u4eec\u517b\u6210\u4e86\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u7684\u4e60\u60ef\u7684\u8bdd\uff0c\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u907f\u514d\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\uff1a 1 2 int x ; // oops, we forgot to initialize x, but the compiler may not complain auto y ; // the compiler will error out because it can't deduce a type for y \u7b2c\u4e09\uff0c\u901a\u8fc7\u7c7b\u578b\u63a8\u65ad\uff0c\u53ef\u4ee5\u786e\u4fdd\u4e0d\u53d1\u751f\u5bf9\u6027\u80fd\u4ea7\u751f\u5f71\u54cd\u7684\u7c7b\u578b\u8f6c\u6362\uff1a 1 2 double x { 5 }; // \u4e0d\u597d: int \u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a double auto y { 5 }; // \u597d\uff1ay \u662f int \u5e76\u4e0d\u4f1a\u53d1\u751f\u7c7b\u578b\u8f6c\u6362 \u5f53\u7136\uff0c\u7c7b\u578b\u63a8\u65ad\u4e5f\u6709\u4e00\u4e9b\u7f3a\u70b9\u3002 \u9996\u5148\uff0c\u7c7b\u578b\u63a8\u65ad\u6a21\u7cca\u5316\u4e86\u4ee3\u7801\u4e2d\u7684\u7c7b\u578b\u4fe1\u606f\u3002\u5c3d\u7ba1\u4f18\u79c0\u7684 IDE \u53ef\u4ee5\u663e\u793a\u63a8\u65ad\u540e\u7684\u7c7b\u578b\uff08\u4f8b\u5982\uff0c\u5f53\u9f20\u6807\u60ac\u6d6e\u5728\u53d8\u91cf\u4e0a\u65f6\uff09\uff0c\u4f46\u662f\u5728\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u65f6\u66f4\u5bb9\u6613\u53d1\u751f\u4e0e\u7c7b\u578b\u76f8\u5173\u7684\u9519\u8bef\u3002 \u4f8b\u5982\uff1a 1 auto y { 5 }; // oops, we wanted a double here but we accidentally provided an int literal \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u5982\u679c\u6211\u4eec\u663e\u5f0f\u5730\u5c06 y \u6307\u5b9a\u4e3a double \u7c7b\u578b\uff0c\u90a3\u4e48\u5373\u4f7f\u6211\u4eec\u610f\u5916\u5730\u4e3a y \u63d0\u4f9b\u4e86\u4e00\u4e2a int \u5b57\u9762\u91cf\u7684\u521d\u59cb\u5316\u503c\uff0c y \u4ecd\u7136\u8fd8\u662f double \u7c7b\u578b\uff0c\u800c\u5982\u679c\u4f7f\u7528\u7c7b\u578b\u63a8\u6f14\uff0c y \u5c06\u88ab\u63a8\u6f14\u4e3a int \u7c7b\u578b\u3002 \u518d\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { auto x { 3 }; auto y { 2 }; std :: cout << x / y ; // oops, we wanted floating point division here return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u5c31\u4e0d\u5bb9\u6613\u770b\u51fa\u4e0b\u9762\u8fdb\u884c\u7684\u662f\u6574\u6570\u9664\u6cd5\u800c\u4e0d\u662f\u6d6e\u70b9\u9664\u6cd5\u3002 \u5176\u6b21\uff0c\u5982\u679c\u521d\u59cb\u5316\u5f0f\u7684\u7c7b\u578b\u6539\u53d8\uff0c\u4f7f\u7528\u7c7b\u578b\u6f14\u7ece\u7684\u53d8\u91cf\u7684\u7c7b\u578b\u4e5f\u4f1a\u6539\u53d8\uff0c\u6b64\u65f6\u53ef\u80fd\u4f1a\u53d1\u751f\u610f\u5916\u7684\u95ee\u9898\u3002\u4f8b\u5982\uff1a 1 auto sum { add ( 5 , 6 ) + gravity }; \u5982\u679c add \u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4ece int \u88ab\u4fee\u6539\u4e3a double \u4e86\uff0c\u6216\u8005 gravity \u7684\u7c7b\u578b\u4ece int \u88ab\u4fee\u6539\u4e3a double \u4e86\uff0c\u90a3\u4e48 sum \u7684\u7c7b\u578b\u540c\u6837\u4f1a\u88ab\u91cd\u65b0\u63a8\u65ad\u4e3a double \u3002 \u603b\u7684\u6765\u8bf4\uff0c\u73b0\u4ee3 C++ \u7684\u5171\u8bc6\u662f\uff0c\u7c7b\u578b\u6f14\u7ece\u7528\u4e8e\u5bf9\u8c61\u901a\u5e38\u662f\u5b89\u5168\u7684\uff0c\u8fd9\u6837\u505a\u53ef\u4ee5\u51cf\u5c11\u5bf9\u7c7b\u578b\u4fe1\u606f\u7684\u5f3a\u8c03\uff0c\u4ece\u800c\u66f4\u597d\u5730\u51f8\u663e\u4ee3\u7801\u7684\u903b\u8f91\uff0c\u4f9d\u6b21\u6765\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u53d8\u91cf\u4f7f\u7528\u7c7b\u578b\u6f14\u7ece\uff0c\u9664\u975e\u9700\u8981\u786e\u4fdd\u4f7f\u7528\u7279\u5b9a\u7684\u7c7b\u578b\u3002 \u4f5c\u8005\u6ce8 \u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\uff0c\u5f53\u6211\u4eec\u8ba4\u4e3a\u663e\u793a\u7c7b\u578b\u4fe1\u606f\u6709\u52a9\u4e8e\u7406\u89e3\u6982\u5ff5\u6216\u793a\u4f8b\u65f6\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u4f7f\u7528\u663e\u5f0f\u7c7b\u578b\uff0c\u800c\u4e0d\u662f\u7c7b\u578b\u63a8\u65ad\u3002","title":"\u7c7b\u578b\u63a8\u65ad\u7684\u4f18\u7f3a\u70b9"},{"location":"8-8-Type-deduction-for-functions/","tags":["type deduction","C++14","C++20"],"text":"Key Takeaway \u5728 C++14 \u4e2d\uff0c auto \u53ef\u4ee5\u88ab\u7528\u4e8e\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u6b64\u65f6\u9700\u8981\u4fdd\u8bc1 \u6240\u6709\u8fd4\u56de\u503c\u7c7b\u578b\u662f\u4e00\u81f4\u7684 \uff0c\u5426\u5219\u9700\u8981\u8fdb\u884c\u8f6c\u6362 \u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u4f7f\u7528 auto \u7684\u6700\u5927\u7f3a\u70b9\u8fd9\u4e9b\u51fd\u6570\u5728\u4f7f\u7528\u524d\u5fc5\u987b\u5df2\u7ecf\u5b8c\u6574\u5b9a\u4e49\uff08\u53ea\u6709 \u524d\u5411\u58f0\u660e \uff09\u662f\u4e0d\u591f\u7684\u3002\u8fd9\u4e5f\u610f\u5473\u7740 auto \u7c7b\u578b\u8fd4\u56de\u503c\u7684\u51fd\u6570\u901a\u5e38\u53ea\u80fd\u591f\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u6587\u4ef6\u4e2d\u4f7f\u7528\u3002 auto add(int x, int y) -> int \u540e\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\u3002\u8fd9\u79cd\u5f62\u5f0f\u53ef\u4ee5\u5bf9\u9f50\u51fd\u6570\u540d\u6216\u662f\u914d\u5408 lambda \u4f7f\u7528 \u51fd\u6570\u5f62\u53c2\u7684\u7c7b\u578b\u4e0d\u80fd\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 int add ( int x , int y ) { return x + y ; } \u4f46\u7f16\u8bd1\u8fd9\u4e2a\u51fd\u6570\u7684\u65f6\u5019\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u786e\u5b9a x + y \u7684\u6c42\u503c\u7ed3\u679c\u4e3a int \u7c7b\u578b\uff0c\u7136\u540e\u5b83\u4f1a\u786e\u4fdd\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u548c\u8be5\u7c7b\u578b\u5339\u914d\uff08\u6216\u8005\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a\u51fd\u6570\u58f0\u660e\u7684\u8fd4\u56de\u7c7b\u578b\uff09\u3002 \u56e0\u4e3a\u7f16\u8bd1\u5668\u5df2\u7ecf\u80fd\u591f\u4ece return \u8bed\u53e5\u63a8\u65ad\u8fd4\u56de\u503c\u7684\u7c7b\u578b\uff0c\u6240\u4ee5\u5728 C++14 \u4e2d\uff0c auto \u53ef\u4ee5\u88ab\u7528\u4e8e\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u5c06 auto \u5173\u952e\u5b57\u653e\u7f6e\u5728\u539f\u6765\u7528\u4e8e\u58f0\u660e\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u7684\u5730\u65b9\u5c31\u53ef\u4ee5\u3002 \u4f8b\u5982\uff1a 1 2 3 4 auto add ( int x , int y ) { return x + y ; } \u7531\u4e8e return \u8bed\u53e5\u8fd4\u56de\u7684\u662f\u4e00\u4e2a int \u7c7b\u578b\u7684\u503c\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u5c06\u51fd\u6570\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u63a8\u65ad\u4e3a int \u3002 \u5f53\u4f7f\u7528 auto \u8fd4\u56de\u503c\u7c7b\u578b\u662f\uff0c\u6240\u6709\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u5fc5\u987b\u662f\u4e00\u81f4\u7684\uff0c\u5426\u5219\u4f1a\u9020\u6210\u9519\u8bef\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 auto someFcn ( bool b ) { if ( b ) return 5 ; // return type int else return 6.7 ; // return type double } \u5728\u4e0a\u9762\u7684\u51fd\u6570\u4e2d\uff0c\u7531\u4e8e\u4e24\u4e2a return \u8bed\u53e5\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u4e0d\u540c\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u63d0\u793a\u9519\u8bef\u3002 \u5982\u679c\u6709\u4e9b\u60c5\u51b5\u4e0b\u9700\u8981\u4f7f\u7528\u4e0d\u540c\u7684\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u90a3\u4e48\u8981\u4e48\u4f60\u4e3a\u51fd\u6570\u663e\u5f0f\u5730\u6307\u660e\u8fd4\u56de\u503c\u7c7b\u578b\uff08\u6b64\u65f6\u7f16\u8bd1\u5668\u4f1a\u5c06\u4e0d\u5339\u914d\u7684\u8fd4\u56de\u503c\u8fdb\u884c \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \uff09\uff0c\u6216\u8005\u4f60\u53ef\u4ee5\u663e\u5f0f\u5730\u5c06 return \u8bed\u53e5\u7684\u8fd4\u56de\u503c\u8f6c\u6362\u6210\u76f8\u540c\u7684\u7c7b\u578b\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06 5 \u4fee\u6539\u4e3a 5.0 \uff0c\u6216\u8005\u5bf9\u4e8e\u975e\u5b57\u9762\u91cf\u7684\u60c5\u51b5\u8fd8\u53ef\u4ee5\u4f7f\u7528 static_cast \u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u4f7f\u7528 auto \u7684\u6700\u5927\u7f3a\u70b9\u8fd9\u4e9b\u51fd\u6570\u5728\u4f7f\u7528\u524d\u5fc5\u987b\u5df2\u7ecf\u5b8c\u6574\u5b9a\u4e49\uff08\u53ea\u6709 \u524d\u5411\u58f0\u660e \uff09\u662f\u4e0d\u591f\u7684\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include auto foo (); int main () { std :: cout << foo (); // the compiler has only seen a forward declaration at this point return 0 ; } auto foo () { return 5 ; } \u5728\u4f5c\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u4ea7\u751f\u5982\u4e0b\u7f16\u8bd1\u9519\u8bef\uff1a 1 error C3779: 'foo': a function that returns 'auto' cannot be used before it is defined. \u8fd9\u662f\u7406\u6240\u5f53\u7136\u7684\uff0c\u56e0\u4e3a\u4ec5\u51ed\u524d\u5411\u58f0\u660e\u7f16\u8bd1\u5668\u65e0\u6cd5\u5bf9\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u8fdb\u884c\u63a8\u65ad\u3002\u8fd9\u4e5f\u610f\u5473\u7740 auto \u7c7b\u578b\u8fd4\u56de\u503c\u7684\u51fd\u6570\u901a\u5e38\u53ea\u80fd\u591f\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u6587\u4ef6\u4e2d\u4f7f\u7528\u3002 \u4e0e\u5bf9\u8c61\u7684\u7c7b\u578b\u63a8\u65ad\u4e0d\u540c\uff0c\u5bf9\u4e8e\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u63a8\u65ad\u7684\u6700\u4f73\u5b9e\u8df5\u5e76\u6ca1\u6709\u592a\u591a\u5171\u8bc6\u3002\u5f53\u5bf9\u5bf9\u8c61\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u65f6\uff0c\u521d\u59cb\u5316\u5f0f\u603b\u662f\u4f5c\u4e3a\u540c\u4e00\u8bed\u53e5\u7684\u4e00\u90e8\u5206\u51fa\u73b0\uff0c\u56e0\u6b64\u786e\u5b9a\u8981\u63a8\u65ad\u7684\u7c7b\u578b\u901a\u5e38\u4e0d\u4f1a\u592a\u9ebb\u70e6\u3002\u5bf9\u4e8e\u51fd\u6570\uff0c\u60c5\u51b5\u5c31\u4e0d\u662f\u8fd9\u6837\u4e86\u2014\u2014\u5f53\u67e5\u770b\u51fd\u6570\u7684\u539f\u578b\u65f6\uff0c\u6ca1\u6709\u4e0a\u4e0b\u6587\u6765\u5e2e\u52a9\u6307\u793a\u51fd\u6570\u8fd4\u56de\u7684\u7c7b\u578b\u3002\u867d\u7136\u4e00\u4e2a\u4f18\u79c0\u7684 IDE \u5e94\u8be5\u6e05\u695a\u63a8\u65ad\u51fa\u7684\u51fd\u6570\u7c7b\u578b\u662f\u4ec0\u4e48\uff0c\u4f46\u5728\u6ca1\u6709\u8fd9\u79cd\u529f\u80fd\u7684\u65f6\u5019\uff0c\u7528\u6237\u5b9e\u9645\u4e0a\u5fc5\u987b\u6df1\u5165\u5230\u51fd\u6570\u4f53\u672c\u8eab\u6765\u786e\u5b9a\u51fd\u6570\u8fd4\u56de\u7684\u7c7b\u578b\u3002\u72af\u9519\u8bef\u7684\u51e0\u7387\u66f4\u9ad8\u3002\u6b64\u5916\uff0c\u51fd\u6570\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u63a8\u65ad\u4e0d\u80fd\u901a\u8fc7 \u524d\u5411\u58f0\u660e \u4f7f\u7528\uff0c\u8fd9\u4e5f\u9650\u5236\u4e86\u5b83\u4eec\u5728\u591a\u6587\u4ef6\u7a0b\u5e8f\u4e2d\u7684\u53ef\u7528\u6027\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u4e8e\u4e00\u822c\u51fd\u6570\u6765\u8bf4\uff0c\u6700\u597d\u4f7f\u7528\u663e\u5f0f\u7684\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u800c\u4e0d\u662f\u7c7b\u578b\u63a8\u65ad\u3002 \u540e\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\u8bed\u6cd5 auto \u4e5f\u53ef\u4ee5\u7528\u4e8e\u540e\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\u8bed\u6cd5\uff0c\u5373\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u88ab\u653e\u7f6e\u5728\u51fd\u6570\u539f\u578b\u7684\u540e\u9762\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 int add ( int x , int y ) { return ( x + y ); } \u5b83\u7b49\u4ef7\u4e8e\u4e0b\u9762\u540e\u7f6e\u8fd4\u56de\u503c\u8bed\u6cd5\u5f62\u5f0f\uff1a 1 2 3 4 auto add ( int x , int y ) -> int { return ( x + y ); } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c auto \u4e0d\u518d\u88ab\u7528\u6765\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\u2014\u2014\u5b83\u53ea\u662f\u540e\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\u8bed\u6cd5\u7684\u4e00\u90e8\u5206\u3002 \u8fd9\u79cd\u8bed\u6cd5\u6709\u4ec0\u4e48\u7528\u5462\uff1f \u8fd9\u79cd\u8bed\u6cd5\u53ef\u4ee5\u5e2e\u6211\u4eec\u5bf9\u9f50\u51fd\u6570\u540d\uff1a 1 2 3 4 auto add ( int x , int y ) -> int ; auto divide ( double x , double y ) -> double ; auto printSomething () -> void ; auto generateSubstring ( const std :: string & s , int start , int len ) -> std :: string ; \u6b64\u5916\uff0c\u4e00\u4e9b C++ \u9ad8\u7ea7\u7279\u6027\u4e5f\u9700\u8981\u540e\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\u8bed\u6cd5\u7684\u652f\u6301\uff0c\u4f8b\u5982 lambda \u8868\u8fbe\u5f0f\uff08\u53c2\u89c1\uff1a 12.7 -- Introduction to lambdas (anonymous functions) \uff09 \u5c31\u76ee\u524d\u800c\u8a00\uff0c\u6211\u4eec\u8fd8\u662f\u5efa\u8bae\u5927\u5bb6\u4f7f\u7528\u4f20\u7edf\u7684\u51fd\u6570\u8fd4\u56de\u503c\u8bed\u6cd5\uff0c\u9664\u975e\u662f\u9700\u8981\u914d\u5408\u4e00\u4e9b\u7279\u6b8a\u7684\u7279\u6027\u6765\u4f7f\u7528\u65f6\u3002 ## \u51fd\u6570\u5f62\u53c2\u7684\u7c7b\u578b\u4e0d\u80fd\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad \u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u5728\u5b66\u4e60\u8fc7\u7c7b\u578b\u63a8\u65ad\u540e\uff0c\u4f1a\u5c1d\u8bd5\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include void addAndPrint ( auto x , auto y ) { std :: cout << x + y ; } int main () { addAndPrint ( 2 , 3 ); // case 1: call addAndPrint with int parameters addAndPrint ( 4.5 , 6.7 ); // case 2: call addAndPrint with double parameters } \u4e0d\u5e78\u7684\u662f\uff0c\u7c7b\u578b\u63a8\u6f14\u4e0d\u9002\u7528\u4e8e\u51fd\u6570\u5f62\u53c2\uff0c\u5728++ 20 \u4e4b\u524d\uff0c\u4e0a\u9762\u7684\u7a0b\u5e8f\u65e0\u6cd5\u7f16\u8bd1(\u4f1a\u5c06\u5f97\u5230\u5173\u4e8e\u51fd\u6570\u5f62\u53c2\u4e0d\u80fd\u5177\u6709\u81ea\u52a8\u7c7b\u578b\u7684\u9519\u8bef)\u3002 \u5728 C++20 \u4e2d\uff0c auto \u5173\u952e\u5b57\u518d\u6b21\u5f97\u5230\u4e86\u6269\u5c55\uff0c\u6240\u4ee5\u4e0a\u8ff0\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u7f16\u8bd1\u4e86\u3002\u4e0d\u8fc7\uff0c\u6b64\u65f6 auto \u5e76\u4e0d\u662f\u88ab\u7528\u505a\u7c7b\u578b\u63a8\u65ad\uff0c\u800c\u662f\u7528\u4e8e\u89e6\u53d1 \u51fd\u6570\u6a21\u677f(function template) \u7279\u6027\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 8.13 - \u51fd\u6570\u6a21\u677f \u4e2d\u4ecb\u7ecd\u51fd\u6570\u6a21\u677f\u5e76\u5728 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f \u4e2d\u8ba8\u8bba auto \u5728\u6b64\u8bed\u5883\u4e2d\u7684\u7528\u9014\u3002","title":"8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad"},{"location":"8-8-Type-deduction-for-functions/#_1","text":"auto \u4e5f\u53ef\u4ee5\u7528\u4e8e\u540e\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\u8bed\u6cd5\uff0c\u5373\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u88ab\u653e\u7f6e\u5728\u51fd\u6570\u539f\u578b\u7684\u540e\u9762\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 int add ( int x , int y ) { return ( x + y ); } \u5b83\u7b49\u4ef7\u4e8e\u4e0b\u9762\u540e\u7f6e\u8fd4\u56de\u503c\u8bed\u6cd5\u5f62\u5f0f\uff1a 1 2 3 4 auto add ( int x , int y ) -> int { return ( x + y ); } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c auto \u4e0d\u518d\u88ab\u7528\u6765\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\u2014\u2014\u5b83\u53ea\u662f\u540e\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\u8bed\u6cd5\u7684\u4e00\u90e8\u5206\u3002 \u8fd9\u79cd\u8bed\u6cd5\u6709\u4ec0\u4e48\u7528\u5462\uff1f \u8fd9\u79cd\u8bed\u6cd5\u53ef\u4ee5\u5e2e\u6211\u4eec\u5bf9\u9f50\u51fd\u6570\u540d\uff1a 1 2 3 4 auto add ( int x , int y ) -> int ; auto divide ( double x , double y ) -> double ; auto printSomething () -> void ; auto generateSubstring ( const std :: string & s , int start , int len ) -> std :: string ; \u6b64\u5916\uff0c\u4e00\u4e9b C++ \u9ad8\u7ea7\u7279\u6027\u4e5f\u9700\u8981\u540e\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\u8bed\u6cd5\u7684\u652f\u6301\uff0c\u4f8b\u5982 lambda \u8868\u8fbe\u5f0f\uff08\u53c2\u89c1\uff1a 12.7 -- Introduction to lambdas (anonymous functions) \uff09 \u5c31\u76ee\u524d\u800c\u8a00\uff0c\u6211\u4eec\u8fd8\u662f\u5efa\u8bae\u5927\u5bb6\u4f7f\u7528\u4f20\u7edf\u7684\u51fd\u6570\u8fd4\u56de\u503c\u8bed\u6cd5\uff0c\u9664\u975e\u662f\u9700\u8981\u914d\u5408\u4e00\u4e9b\u7279\u6b8a\u7684\u7279\u6027\u6765\u4f7f\u7528\u65f6\u3002","title":"\u540e\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\u8bed\u6cd5"},{"location":"8-8-Type-deduction-for-functions/#_2","text":"\u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u5728\u5b66\u4e60\u8fc7\u7c7b\u578b\u63a8\u65ad\u540e\uff0c\u4f1a\u5c1d\u8bd5\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include void addAndPrint ( auto x , auto y ) { std :: cout << x + y ; } int main () { addAndPrint ( 2 , 3 ); // case 1: call addAndPrint with int parameters addAndPrint ( 4.5 , 6.7 ); // case 2: call addAndPrint with double parameters } \u4e0d\u5e78\u7684\u662f\uff0c\u7c7b\u578b\u63a8\u6f14\u4e0d\u9002\u7528\u4e8e\u51fd\u6570\u5f62\u53c2\uff0c\u5728++ 20 \u4e4b\u524d\uff0c\u4e0a\u9762\u7684\u7a0b\u5e8f\u65e0\u6cd5\u7f16\u8bd1(\u4f1a\u5c06\u5f97\u5230\u5173\u4e8e\u51fd\u6570\u5f62\u53c2\u4e0d\u80fd\u5177\u6709\u81ea\u52a8\u7c7b\u578b\u7684\u9519\u8bef)\u3002 \u5728 C++20 \u4e2d\uff0c auto \u5173\u952e\u5b57\u518d\u6b21\u5f97\u5230\u4e86\u6269\u5c55\uff0c\u6240\u4ee5\u4e0a\u8ff0\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u7f16\u8bd1\u4e86\u3002\u4e0d\u8fc7\uff0c\u6b64\u65f6 auto \u5e76\u4e0d\u662f\u88ab\u7528\u505a\u7c7b\u578b\u63a8\u65ad\uff0c\u800c\u662f\u7528\u4e8e\u89e6\u53d1 \u51fd\u6570\u6a21\u677f(function template) \u7279\u6027\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 8.13 - \u51fd\u6570\u6a21\u677f \u4e2d\u4ecb\u7ecd\u51fd\u6570\u6a21\u677f\u5e76\u5728 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f \u4e2d\u8ba8\u8bba auto \u5728\u6b64\u8bed\u5883\u4e2d\u7684\u7528\u9014\u3002","title":"## \u51fd\u6570\u5f62\u53c2\u7684\u7c7b\u578b\u4e0d\u80fd\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad"},{"location":"8-9-Introduction-to-function-overloading/","tags":["overloading"],"text":"Key Takeaway \u4f7f\u7528\u76f8\u540c\u51fd\u6570\u540d\u4f46\u5f62\u53c2\u4e0d\u540c\u7684\u51fd\u6570\uff0c\u79f0\u4e3a\u51fd\u6570\u91cd\u8f7d\u3002 \u53ea\u8981\u7f16\u8bd1\u5668\u80fd\u591f\u533a\u5206\u6bcf\u4e2a\u91cd\u8f7d\u51fd\u6570\uff0c\u51fd\u6570\u5c31\u53ef\u4ee5\u88ab\u91cd\u8f7d\u3002\u5982\u679c\u4e0d\u80fd\u533a\u5206\u91cd\u8f7d\u51fd\u6570\uff0c\u5c06\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 C++ \u4e2d\u7684\u64cd\u4f5c\u7b26\u5176\u5b9e\u4e5f\u662f\u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u4eec\u4e5f\u53ef\u4ee5\u88ab\u91cd\u8f7d \u91cd\u8f7d\u51fd\u6570\u80fd\u591f\u8fdb\u884c\u7f16\u8bd1\uff0c\u5fc5\u987b\u6ee1\u8db3\u4e24\u4e2a\u6761\u4ef6\uff1a \u6bcf\u4e2a\u91cd\u8f7d\u7684\u51fd\u6570\u90fd\u5fc5\u987b\u80fd\u591f\u533a\u5206\u3002\u6211\u4eec\u4f1a\u5728 8.10 - \u51fd\u6570\u91cd\u8f7d\u548c\u533a\u5206 \u4e2d\u4ecb\u7ecd\u5b83\u4eec\u662f\u5982\u4f55\u88ab\u533a\u5206\u7684\u3002 \u6bcf\u4e2a\u88ab\u8c03\u7528\u7684\u91cd\u8f7d\u51fd\u6570\u5728\u91cd\u8f7d\u89e3\u6790\u65f6\u5fc5\u987b\u80fd\u591f\u5b9a\u4f4d\u5230\u4e00\u4e2a\u51fd\u6570\u3002\u6211\u4eec\u4f1a\u5728 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u5339\u914d\u6b67\u4e49 \u4e2d\u4ecb\u7ecd\u5982\u4f55\u8fdb\u884c\u91cd\u8f7d\u51fd \u8003\u8651\u4e0b\u9762\u7684\u51fd\u6570\uff1a 1 2 3 4 int add ( int x , int y ) { return x + y ; } \u8fd9\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u5c06\u4e24\u4e2a\u6574\u6570\u76f8\u52a0\u5e76\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u7ed3\u679c\u3002\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u8fd8\u9700\u8981\u4e00\u4e2a\u53ef\u4ee5\u5c06\u4e24\u4e2a\u6d6e\u70b9\u6570\u76f8\u52a0\u7684\u51fd\u6570\u5462\uff1f\u8fd9\u4e2a add() \u51fd\u6570\u5e76\u4e0d\u80fd\u5b9e\u73b0\u6d6e\u70b9\u6570\u76f8\u52a0\uff0c\u56e0\u4e3a\u4efb\u4f55\u6d6e\u70b9 \u5f62\u53c2 \u90fd\u4f1a\u88ab\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5bfc\u81f4\u6d6e\u70b9\u5f62\u53c2\u4e22\u5931\u5b83\u4eec\u7684\u5c0f\u6570\u503c\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u5b9a\u4e49\u591a\u4e2a\u540d\u79f0\u7565\u6709\u4e0d\u540c\u7684\u51fd\u6570: 1 2 3 4 5 6 7 8 9 int addInteger ( int x , int y ) { return x + y ; } double addDouble ( double x , double y ) { return x + y ; } \u4f46\u662f\uff0c\u4e3a\u4e86\u8fbe\u5230\u6700\u4f73\u6548\u679c\uff0c\u4f60\u5fc5\u987b\u4e3a\u5177\u6709\u4e0d\u540c\u7c7b\u578b\u53c2\u6570\u7684\u7c7b\u4f3c\u51fd\u6570\uff0c\u5b9a\u4e49\u4e00\u81f4\u7684\u547d\u540d\u6807\u51c6\uff0c\u540c\u65f6\u8bb0\u4f4f\u8fd9\u4e9b\u51fd\u6570\u7684\u540d\u79f0\uff0c\u5e76\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u51fd\u6570\u3002 \u5c06\u6765\uff0c\u5982\u679c\u6211\u4eec\u53c8\u9700\u8981\u4e00\u4e2a\u7c7b\u4f3c\u7684\u51fd\u6570\uff0c\u5c06\u4e09\u4e2a\u6574\u6570\u76f8\u52a0\u800c\u4e0d\u662f\u4e24\u4e2a\u6574\u6570\u76f8\u52a0\u7684\u65f6\u5019\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u4e3a\u6bcf\u4e00\u4e2a\u51fd\u6570\u521b\u5efa\u4e0d\u540c\u7684\u540d\u79f0\u4f1a\u4f7f\u4ee3\u7801\u53d8\u5f97\u96be\u4ee5\u7ef4\u62a4\u3002 \u51fd\u6570\u91cd\u8f7d\u7b80\u4ecb \u5e78\u8fd0\u7684\u662f\uff0cC++ \u63d0\u4f9b\u4e86\u4e00\u4e2a\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\u3002\u51fd\u6570\u91cd\u8f7d\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u76f8\u540c\u7684\u540d\u79f0\u521b\u5efa\u591a\u4e2a\u51fd\u6570\uff0c\u53ea\u8981\u6bcf\u4e2a\u76f8\u540c\u540d\u79f0\u7684\u51fd\u6570\u5177\u6709\u4e0d\u540c\u7684\u5f62\u53c2\u7c7b\u578b(\u6216\u8005\u53ef\u4ee5\u7528\u5176\u4ed6\u65b9\u5f0f\u533a\u5206\u51fd\u6570)\u3002\u6bcf\u4e2a\u51fd\u6570\u90fd\u5171\u4eab\u4e00\u4e2a\u540d\u79f0(\u5728\u540c\u4e00\u4e2a\u4f5c\u7528\u57df\u4e2d)\uff0c\u79f0\u4e3a \u91cd\u8f7d(overload) \u51fd\u6570(\u6709\u65f6\u7b80\u79f0\u91cd\u8f7d)\u3002 \u5bf9 add() \u51fd\u6570\u8fdb\u884c\u91cd\u8f7d\uff0c\u6211\u4eec\u53ef\u4ee5\u58f0\u660e\u53e6\u5916\u4e00\u4e2a add() \u51fd\u6570\uff0c\u4f46\u662f\u53c2\u6570\u7c7b\u578b\u4e3a double \uff1a 1 2 3 4 double add ( double x , double y ) { return x + y ; } \u6b64\u65f6\uff0c\u5728\u540c\u4e00\u4e2a\u4f5c\u7528\u57df\u4e2d\uff0c\u51fa\u73b0\u4e86\u4e24\u4e2a\u540c\u540d\u7684 add() \u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int add ( int x , int y ) // integer version { return x + y ; } double add ( double x , double y ) // floating point version { return x + y ; } int main () { return 0 ; } \u4ee5\u4e0a\u7a0b\u5e8f\u662f\u53ef\u4ee5\u7f16\u8bd1\u7684\u3002\u5c3d\u7ba1\u4f60\u53ef\u80fd\u62c5\u5fc3\u8fd9\u4e9b\u51fd\u6570\u4f1a\u51fa\u73b0\u547d\u540d\u51b2\u7a81\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u4f1a\u3002\u56e0\u4e3a\u8fd9\u4e9b\u51fd\u6570\u7684\u5f62\u53c2\u7c7b\u578b\u4e0d\u540c\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u80fd\u591f\u533a\u5206\u8fd9\u4e9b\u51fd\u6570\uff0c\u5e76\u5c06\u5b83\u4eec\u89c6\u4e3a\u5171\u4eab\u4e00\u4e2a\u540d\u79f0\u7684\u5355\u72ec\u51fd\u6570\u3002 \u5173\u952e\u4fe1\u606f \u53ea\u8981\u7f16\u8bd1\u5668\u80fd\u591f\u533a\u5206\u6bcf\u4e2a\u91cd\u8f7d\u51fd\u6570\uff0c\u51fd\u6570\u5c31\u53ef\u4ee5\u88ab\u91cd\u8f7d\u3002\u5982\u679c\u4e0d\u80fd\u533a\u5206\u91cd\u8f7d\u51fd\u6570\uff0c\u5c06\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 \u76f8\u5173\u5185\u5bb9 \u56e0\u4e3a C++ \u4e2d\u7684\u64cd\u4f5c\u7b26\u5176\u5b9e\u4e5f\u662f\u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u4eec\u4e5f\u53ef\u4ee5\u88ab\u91cd\u8f7d\uff0c\u6211\u4eec\u4f1a\u5728 14.1 -- Introduction to operator overloading \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u51fd\u6570\u91cd\u8f7d\u89e3\u6790 \u6b64\u5916\uff0c\u5f53\u8c03\u7528\u88ab\u91cd\u8f7d\u7684\u51fd\u6570\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u57fa\u4e8e\u5f62\u53c2\u6765\u5339\u914d\u6700\u5408\u9002\u7684\u51fd\u6570\u8c03\u7528\u2014\u2014\u79f0\u4e3a \u91cd\u8f7d\u89e3\u6790(overload resolution) \u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int add ( int x , int y ) { return x + y ; } double add ( double x , double y ) { return x + y ; } int main () { std :: cout << add ( 1 , 2 ); // calls add(int, int) std :: cout << '\\n' ; std :: cout << add ( 1.2 , 3.4 ); // calls add(double, double) return 0 ; } \u4e0a\u8ff0\u51fd\u6570\u7f16\u8bd1\u6267\u884c\u7684\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4.6 \u5f53\u63d0\u4f9b\u6574\u578b\u5f62\u53c2\u65f6\uff08 add(1, 2) \uff09\u7f16\u8bd1\u5668\u53ef\u4ee5\u5224\u65ad\u51fa\u6211\u4eec\u9700\u8981\u8c03\u7528\u7684\u662f add(int, int) \u3002\u800c\u5f53\u6211\u4eec\u63d0\u4f9b\u6d6e\u70b9\u7c7b\u578b\u7684\u5f62\u53c2\u65f6\uff08 add(1.2, 3.4) \uff09\uff0c\u7f16\u8bd1\u5668\u80fd\u591f\u786e\u5b9a\u6211\u4eec\u8c03\u7528\u7684\u662f add(double, double) \u3002 \u4f7f\u91cd\u8f7d\u7684\u51fd\u6570\u80fd\u591f\u7f16\u8bd1 \u4e3a\u4e86\u4f7f\u4f7f\u7528\u4e86\u91cd\u8f7d\u51fd\u6570\u7684\u7a0b\u5e8f\u80fd\u591f\u8fdb\u884c\u7f16\u8bd1\uff0c\u5fc5\u987b\u6ee1\u8db3\u4e24\u4e2a\u6761\u4ef6\uff1a \u6bcf\u4e2a\u91cd\u8f7d\u7684\u51fd\u6570\u90fd\u5fc5\u987b\u80fd\u591f\u533a\u5206\u3002\u6211\u4eec\u4f1a\u5728 8.10 - \u51fd\u6570\u91cd\u8f7d\u548c\u533a\u5206 \u4e2d\u4ecb\u7ecd\u5b83\u4eec\u662f\u5982\u4f55\u88ab\u533a\u5206\u7684\u3002 \u6bcf\u4e2a\u88ab\u8c03\u7528\u7684\u91cd\u8f7d\u51fd\u6570\u5728\u91cd\u8f7d\u89e3\u6790\u65f6\u5fc5\u987b\u80fd\u591f\u5b9a\u4f4d\u5230\u4e00\u4e2a\u51fd\u6570\u3002\u6211\u4eec\u4f1a\u5728 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u5339\u914d\u6b67\u4e49 \u4e2d\u4ecb\u7ecd\u5982\u4f55\u8fdb\u884c\u91cd\u8f7d\u51fd\u6570\u7684\u5339\u914d\u3002 \u5982\u679c\u91cd\u8f7d\u51fd\u6570\u4e0d\u80fd\u88ab\u533a\u5206\uff0c\u6216\u8005\u91cd\u8f7d\u51fd\u6570\u7684\u51fd\u6570\u8c03\u7528\u65e0\u6cd5\u89e3\u6790\u4e3a\u91cd\u8f7d\u51fd\u6570\uff0c\u5219\u5c06\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u533a\u5206\u91cd\u8f7d\u51fd\u6570\u3002\u7136\u540e\uff0c\u5728\u518d\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u7d22\u7f16\u8bd1\u5668\u5982\u4f55\u5c06\u51fd\u6570\u8c03\u7528\u89e3\u6790\u4e3a\u91cd\u8f7d\u51fd\u6570\u3002 \u5c0f\u7ed3 \u51fd\u6570\u91cd\u8f7d\u901a\u8fc7\u51cf\u5c11\u9700\u8981\u8bb0\u4f4f\u7684\u51fd\u6570\u540d\u7684\u6570\u91cf\u6765\u964d\u4f4e\u7a0b\u5e8f\u7684\u590d\u6742\u6027\u3002\u5b83\u53ef\u4ee5\u800c\u4e14\u5e94\u8be5\u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u51fd\u6570\u91cd\u8f7d\u53ef\u4ee5\u7b80\u5316\u7a0b\u5e8f\u3002","title":"8.9 - \u51fd\u6570\u91cd\u8f7d"},{"location":"8-9-Introduction-to-function-overloading/#_1","text":"\u5e78\u8fd0\u7684\u662f\uff0cC++ \u63d0\u4f9b\u4e86\u4e00\u4e2a\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\u3002\u51fd\u6570\u91cd\u8f7d\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u76f8\u540c\u7684\u540d\u79f0\u521b\u5efa\u591a\u4e2a\u51fd\u6570\uff0c\u53ea\u8981\u6bcf\u4e2a\u76f8\u540c\u540d\u79f0\u7684\u51fd\u6570\u5177\u6709\u4e0d\u540c\u7684\u5f62\u53c2\u7c7b\u578b(\u6216\u8005\u53ef\u4ee5\u7528\u5176\u4ed6\u65b9\u5f0f\u533a\u5206\u51fd\u6570)\u3002\u6bcf\u4e2a\u51fd\u6570\u90fd\u5171\u4eab\u4e00\u4e2a\u540d\u79f0(\u5728\u540c\u4e00\u4e2a\u4f5c\u7528\u57df\u4e2d)\uff0c\u79f0\u4e3a \u91cd\u8f7d(overload) \u51fd\u6570(\u6709\u65f6\u7b80\u79f0\u91cd\u8f7d)\u3002 \u5bf9 add() \u51fd\u6570\u8fdb\u884c\u91cd\u8f7d\uff0c\u6211\u4eec\u53ef\u4ee5\u58f0\u660e\u53e6\u5916\u4e00\u4e2a add() \u51fd\u6570\uff0c\u4f46\u662f\u53c2\u6570\u7c7b\u578b\u4e3a double \uff1a 1 2 3 4 double add ( double x , double y ) { return x + y ; } \u6b64\u65f6\uff0c\u5728\u540c\u4e00\u4e2a\u4f5c\u7528\u57df\u4e2d\uff0c\u51fa\u73b0\u4e86\u4e24\u4e2a\u540c\u540d\u7684 add() \u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int add ( int x , int y ) // integer version { return x + y ; } double add ( double x , double y ) // floating point version { return x + y ; } int main () { return 0 ; } \u4ee5\u4e0a\u7a0b\u5e8f\u662f\u53ef\u4ee5\u7f16\u8bd1\u7684\u3002\u5c3d\u7ba1\u4f60\u53ef\u80fd\u62c5\u5fc3\u8fd9\u4e9b\u51fd\u6570\u4f1a\u51fa\u73b0\u547d\u540d\u51b2\u7a81\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u4f1a\u3002\u56e0\u4e3a\u8fd9\u4e9b\u51fd\u6570\u7684\u5f62\u53c2\u7c7b\u578b\u4e0d\u540c\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u80fd\u591f\u533a\u5206\u8fd9\u4e9b\u51fd\u6570\uff0c\u5e76\u5c06\u5b83\u4eec\u89c6\u4e3a\u5171\u4eab\u4e00\u4e2a\u540d\u79f0\u7684\u5355\u72ec\u51fd\u6570\u3002 \u5173\u952e\u4fe1\u606f \u53ea\u8981\u7f16\u8bd1\u5668\u80fd\u591f\u533a\u5206\u6bcf\u4e2a\u91cd\u8f7d\u51fd\u6570\uff0c\u51fd\u6570\u5c31\u53ef\u4ee5\u88ab\u91cd\u8f7d\u3002\u5982\u679c\u4e0d\u80fd\u533a\u5206\u91cd\u8f7d\u51fd\u6570\uff0c\u5c06\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 \u76f8\u5173\u5185\u5bb9 \u56e0\u4e3a C++ \u4e2d\u7684\u64cd\u4f5c\u7b26\u5176\u5b9e\u4e5f\u662f\u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u4eec\u4e5f\u53ef\u4ee5\u88ab\u91cd\u8f7d\uff0c\u6211\u4eec\u4f1a\u5728 14.1 -- Introduction to operator overloading \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"\u51fd\u6570\u91cd\u8f7d\u7b80\u4ecb"},{"location":"8-9-Introduction-to-function-overloading/#_2","text":"\u6b64\u5916\uff0c\u5f53\u8c03\u7528\u88ab\u91cd\u8f7d\u7684\u51fd\u6570\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u57fa\u4e8e\u5f62\u53c2\u6765\u5339\u914d\u6700\u5408\u9002\u7684\u51fd\u6570\u8c03\u7528\u2014\u2014\u79f0\u4e3a \u91cd\u8f7d\u89e3\u6790(overload resolution) \u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int add ( int x , int y ) { return x + y ; } double add ( double x , double y ) { return x + y ; } int main () { std :: cout << add ( 1 , 2 ); // calls add(int, int) std :: cout << '\\n' ; std :: cout << add ( 1.2 , 3.4 ); // calls add(double, double) return 0 ; } \u4e0a\u8ff0\u51fd\u6570\u7f16\u8bd1\u6267\u884c\u7684\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4.6 \u5f53\u63d0\u4f9b\u6574\u578b\u5f62\u53c2\u65f6\uff08 add(1, 2) \uff09\u7f16\u8bd1\u5668\u53ef\u4ee5\u5224\u65ad\u51fa\u6211\u4eec\u9700\u8981\u8c03\u7528\u7684\u662f add(int, int) \u3002\u800c\u5f53\u6211\u4eec\u63d0\u4f9b\u6d6e\u70b9\u7c7b\u578b\u7684\u5f62\u53c2\u65f6\uff08 add(1.2, 3.4) \uff09\uff0c\u7f16\u8bd1\u5668\u80fd\u591f\u786e\u5b9a\u6211\u4eec\u8c03\u7528\u7684\u662f add(double, double) \u3002","title":"\u51fd\u6570\u91cd\u8f7d\u89e3\u6790"},{"location":"8-9-Introduction-to-function-overloading/#_3","text":"\u4e3a\u4e86\u4f7f\u4f7f\u7528\u4e86\u91cd\u8f7d\u51fd\u6570\u7684\u7a0b\u5e8f\u80fd\u591f\u8fdb\u884c\u7f16\u8bd1\uff0c\u5fc5\u987b\u6ee1\u8db3\u4e24\u4e2a\u6761\u4ef6\uff1a \u6bcf\u4e2a\u91cd\u8f7d\u7684\u51fd\u6570\u90fd\u5fc5\u987b\u80fd\u591f\u533a\u5206\u3002\u6211\u4eec\u4f1a\u5728 8.10 - \u51fd\u6570\u91cd\u8f7d\u548c\u533a\u5206 \u4e2d\u4ecb\u7ecd\u5b83\u4eec\u662f\u5982\u4f55\u88ab\u533a\u5206\u7684\u3002 \u6bcf\u4e2a\u88ab\u8c03\u7528\u7684\u91cd\u8f7d\u51fd\u6570\u5728\u91cd\u8f7d\u89e3\u6790\u65f6\u5fc5\u987b\u80fd\u591f\u5b9a\u4f4d\u5230\u4e00\u4e2a\u51fd\u6570\u3002\u6211\u4eec\u4f1a\u5728 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u5339\u914d\u6b67\u4e49 \u4e2d\u4ecb\u7ecd\u5982\u4f55\u8fdb\u884c\u91cd\u8f7d\u51fd\u6570\u7684\u5339\u914d\u3002 \u5982\u679c\u91cd\u8f7d\u51fd\u6570\u4e0d\u80fd\u88ab\u533a\u5206\uff0c\u6216\u8005\u91cd\u8f7d\u51fd\u6570\u7684\u51fd\u6570\u8c03\u7528\u65e0\u6cd5\u89e3\u6790\u4e3a\u91cd\u8f7d\u51fd\u6570\uff0c\u5219\u5c06\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u533a\u5206\u91cd\u8f7d\u51fd\u6570\u3002\u7136\u540e\uff0c\u5728\u518d\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u7d22\u7f16\u8bd1\u5668\u5982\u4f55\u5c06\u51fd\u6570\u8c03\u7528\u89e3\u6790\u4e3a\u91cd\u8f7d\u51fd\u6570\u3002","title":"\u4f7f\u91cd\u8f7d\u7684\u51fd\u6570\u80fd\u591f\u7f16\u8bd1"},{"location":"8-9-Introduction-to-function-overloading/#_4","text":"\u51fd\u6570\u91cd\u8f7d\u901a\u8fc7\u51cf\u5c11\u9700\u8981\u8bb0\u4f4f\u7684\u51fd\u6570\u540d\u7684\u6570\u91cf\u6765\u964d\u4f4e\u7a0b\u5e8f\u7684\u590d\u6742\u6027\u3002\u5b83\u53ef\u4ee5\u800c\u4e14\u5e94\u8be5\u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u51fd\u6570\u91cd\u8f7d\u53ef\u4ee5\u7b80\u5316\u7a0b\u5e8f\u3002","title":"\u5c0f\u7ed3"},{"location":"8-x-Chapter-8-summary-and-quiz/","tags":["summary"],"text":"\u5b66\u5b8c\u54af\uff01\u672c\u7ae0\u7684\u4e3b\u9898(\u7279\u522b\u662f\u7c7b\u578b\u522b\u540d\u3001\u91cd\u8f7d\u51fd\u6570\u548c\u51fd\u6570\u6a21\u677f)\u5728 C++ \u6807\u51c6\u5e93\u4e2d\u968f\u5904\u53ef\u89c1\u3002\u6211\u4eec\u8fd8\u6709\u4e00\u7ae0\u8981\u8bb2(\u4ecb\u7ecd\u590d\u5408\u7c7b\u578b)\uff0c\u7136\u540e\u6211\u4eec\u5c06\u51c6\u5907\u597d\u6df1\u5165\u7814\u7a76\u6807\u51c6\u5e93\u4e2d\u6700\u6709\u7528\u7684\u90e8\u5206\uff01 \u590d\u4e60 \u5c06\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u7684\u8fc7\u7a0b\u53eb\u505a \u7c7b\u578b\u8f6c\u6362 \u3002 \u5f53\u9700\u8981\u4e00\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u63d0\u4f9b\u4e86\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u65f6\uff0c\u5219\u4f1a\u6267\u884c \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 (\u4e5f\u79f0\u4e3a\u81ea\u52a8\u7c7b\u578b\u8f6c\u6362\u6216\u5f3a\u5236\u8f6c\u6362)\u3002\u5982\u679c\u7f16\u8bd1\u5668\u80fd\u591f\u627e\u5230\u4e24\u79cd\u7c7b\u578b\u4e4b\u95f4\u8fdb\u8fdb\u884c\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u7684\u89c4\u5219\uff0c\u5219\u5b83\u4f1a\u6267\u884c\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\uff0c\u5426\u5219\u5c31\u4f1a\u4ea7\u751f\u7f16\u8bd1\u62a5\u9519\u3002\uff08 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \uff09 C++ \u8bed\u8a00\u5b9a\u4e49\u4e86\u8bb8\u591a\u57fa\u672c\u7c7b\u578b\u4e4b\u95f4\u7684\u5185\u7f6e\u8f6c\u6362\u65b9\u5f0f(\u4ee5\u53ca\u4e00\u4e9b\u66f4\u9ad8\u7ea7\u7684\u7c7b\u578b\u7684\u8f6c\u6362)\uff0c\u79f0\u4e3a \u6807\u51c6\u8f6c\u6362 \u3002\u5176\u4e2d\u5305\u62ec \u6570\u503c\u63d0\u5347 \u3001 \u6570\u503c\u8f6c\u6362 \u548c \u7b97\u6570\u8f6c\u6362 \u6570\u503c\u63d0\u5347 \uff08 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 \uff09\u662f\u5c06\u8f83\u5c0f\u7684\u6570\u503c\u7c7b\u578b\u8f6c\u6362\u4e3a\u8f83\u5927\u7684\u6570\u503c\u7c7b\u578b(\u901a\u5e38\u4e3a int \u6216 double )\uff0c\u8fd9\u6837CPU\u5c31\u53ef\u4ee5\u5bf9\u4e0e\u5904\u7406\u5668\u81ea\u7136\u6570\u636e\u5927\u5c0f\u76f8\u5339\u914d\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002\u6570\u503c\u63d0\u5347\u5305\u62ec\u6574\u6570\u63d0\u5347\u548c\u6d6e\u70b9\u63d0\u5347\u3002\u6570\u5b57\u63d0\u5347\u662f\u4fdd\u503c\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4e0d\u4f1a\u4e22\u5931\u503c\u6216\u503c\u7684\u7cbe\u5ea6\u3002 \u6570\u503c\u8f6c\u6362 \uff08 8.3 - \u6570\u503c\u8f6c\u6362 \uff09\u6570\u503c\u8f6c\u6362\u662f\u57fa\u672c\u7c7b\u578b\u4e4b\u95f4\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u800c\u4e0d\u662f\u6570\u503c\u63d0\u5347\u3002\u6536\u7f29\u8f6c\u6362\u53ef\u80fd\u4f1a\u5bfc\u81f4\u503c\u6216\u7cbe\u5ea6\u7684\u635f\u5931\u3002 \u5728C++\u4e2d\uff0c\u67d0\u4e9b\u4e8c\u5143\u64cd\u4f5c\u7b26\u8981\u6c42\u5b83\u4eec\u7684\u64cd\u4f5c\u6570\u5177\u6709\u76f8\u540c\u7684\u7c7b\u578b\u3002\u5982\u679c\u63d0\u4f9b\u4e86\u4e0d\u540c\u7c7b\u578b\u7684\u64cd\u4f5c\u6570\uff0c\u5219\u4f7f\u7528\u4e00\u7ec4\u79f0\u4e3a \u666e\u901a\u7b97\u672f\u8f6c\u6362 \uff08 8.4 - \u7b97\u672f\u8f6c\u6362 \uff09\u89c4\u5219\u5c06\u5176\u4e2d\u4e00\u4e2a\u6216\u4e24\u4e2a\u64cd\u4f5c\u6570\u9690\u5f0f\u8f6c\u6362\u4e3a\u5339\u914d\u7c7b\u578b\u3002 \u5f53\u7a0b\u5e8f\u5458\u663e\u5f0f\u5730\u901a\u8fc7 cast \u6765\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u79f0\u4e3a \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \uff08 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast \uff09\u3002C++\u652f\u63015\u79cd\u7c7b\u578b\u5f3a\u5236\u8f6c\u6362\uff1a C\u98ce\u683c\u7c7b\u578b\u8f6c\u6362 \u3001 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u3001 const \u7c7b\u578b\u8f6c\u6362 \u3001 \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u548c \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 \u3002\u4e00\u822c\u6765\u8bf4\uff0c\u4f60\u5e94\u8be5\u907f\u514d\u4f7f\u7528\"C\u98ce\u683c\u7c7b\u578b\u8f6c\u6362\u201d\u3001\u201cconst\u7c7b\u578b\u8f6c\u6362\u201d\u548c\u201c\u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362\u201d\u3002 static_cast \u7528\u4e8e\u5c06\u4e00\u4e2a\u503c\u4ece\u4e00\u79cd\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u7c7b\u578b\uff0c\u5b83\u662f\u76ee\u524dC++\u4e2d\u88ab\u6700\u591a\u4f7f\u7528\u7684\u5f3a\u5236\u8f6c\u6362\u3002 typedef \u548c\u7c7b\u578b\u522b\u540d \u5141\u8bb8\u7a0b\u5e8f\u5458\u4e3a\u6570\u636e\u7c7b\u578b\u521b\u5efa\u522b\u540d\u3002\u8fd9\u4e9b\u522b\u540d\u4e0d\u662f\u65b0\u7c7b\u578b\uff0c\u5e76\u4e14\u4e0e\u539f\u7c7b\u578b\u7684\u64cd\u4f5c\u76f8\u540c\u3002Typedefs \u548c\u7c7b\u578b\u522b\u540d\u4e0d\u63d0\u4f9b\u4efb\u4f55\u7c7b\u578b\u5b89\u5168\u673a\u5236\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u8981\u5047\u5b9a\u7c7b\u578b\u522b\u540d\u548c\u5176\u539f\u7c7b\u578b\u662f\u6709\u533a\u522b\u7684\u3002 auto \u5173\u952e\u5b57\u6709\u5f88\u591a\u7528\u9014\u3002\u9996\u5148\uff0c auto \u53ef\u7528\u4e8e\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad( 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad )\uff0c\u5b83\u5c06\u4ece\u53d8\u91cf\u7684\u521d\u59cb\u5316\u503c\u63a8\u65ad\u51fa\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u4f46\u662f\uff0c\u7c7b\u578b\u63a8\u65ad\u4f1a\u4e22\u5f03 const \u548c\u5f15\u7528\uff0c\u6240\u4ee5\u5982\u679c\u9700\u8981\uff0c\u8bf7\u786e\u4fdd\u5c06\u5b83\u4eec\u6dfb\u52a0\u56de\u6765\u3002 auto \u4e5f\u53ef\u4ee5\u7528\u4f5c\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u7684\u63a8\u65ad\uff08 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad \uff09\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u4ece\u51fd\u6570\u7684 return \u8bed\u53e5\u4e2d\u63a8\u65ad\u51fa\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\uff0c\u4e0d\u8fc7\u5bf9\u4e8e\u666e\u901a\u51fd\u6570\u5e94\u8be5\u907f\u514d\u8fd9\u6837\u505a\u3002 auto \u8fd8\u88ab\u7528\u4f5c\u540e\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\u8bed\u6cd5\u3002 \u51fd\u6570\u91cd\u8f7d \uff08 8.9 - \u51fd\u6570\u91cd\u8f7d \uff09\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u76f8\u540c\u7684\u540d\u79f0\u521b\u5efa\u591a\u4e2a\u51fd\u6570\uff0c\u53ea\u8981\u6bcf\u4e2a\u76f8\u540c\u540d\u79f0\u7684\u51fd\u6570\u5177\u6709\u4e0d\u540c\u7684\u5f62\u53c2\u7c7b\u578b\u96c6\u5408\u5373\u53ef(\u6216\u8005\u53ef\u4ee5\u7528\u5176\u4ed6\u65b9\u5f0f\u533a\u5206\u51fd\u6570)\u3002\u8fd9\u6837\u7684\u51fd\u6570\u79f0\u4e3a\u91cd\u8f7d\u51fd\u6570(\u6216\u7b80\u79f0 \u91cd\u8f7d )\u3002\u8fd4\u56de\u7c7b\u578b\u4e0d\u80fd\u88ab\u7528\u6765\u8fdb\u884c\u91cd\u8f7d\u51fd\u6570\u7684\u533a\u5206\u3002 \u5728\u89e3\u6790\u91cd\u8f7d\u51fd\u6570\u65f6\uff08 8.10 - \u51fd\u6570\u91cd\u8f7d\u548c\u533a\u5206 \uff09\uff0c\u5982\u679c\u6ca1\u6709\u627e\u5230\u7cbe\u786e\u5339\u914d\uff0c\u7f16\u8bd1\u5668\u5c06\u4f18\u5148\u9009\u62e9\u53ef\u4ee5\u901a\u8fc7\u6570\u503c\u63d0\u5347\u5339\u914d\u7684\u91cd\u8f7d\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u90a3\u4e9b\u9700\u8981\u6570\u503c\u8f6c\u6362\u7684\u91cd\u8f7d\u51fd\u6570\u3002\u5f53\u5bf9\u5df2\u91cd\u8f7d\u7684\u51fd\u6570\u8fdb\u884c\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u5c1d\u8bd5\u6839\u636e\u51fd\u6570\u8c03\u7528\u4e2d\u4f7f\u7528\u7684\u53c2\u6570\u5c06\u51fd\u6570\u8c03\u7528\u4e0e\u9002\u5f53\u7684\u91cd\u8f7d\u5339\u914d\u3002\u8fd9\u88ab\u79f0\u4e3a \u91cd\u8f7d\u89e3\u6790 \u3002 \u5f53\u7f16\u8bd1\u5668\u53d1\u73b0\u4e24\u4e2a\u6216\u591a\u4e2a\u51fd\u6570\u53ef\u4ee5\u5c06\u51fd\u6570\u8c03\u7528\u4e0e\u91cd\u8f7d\u51fd\u6570\u5339\u914d\uff0c\u4f46\u65e0\u6cd5\u786e\u5b9a\u54ea\u4e00\u4e2a\u662f\u6700\u597d\u7684\u65f6\uff0c\u5c31\u4f1a\u51fa\u73b0 \u4e0d\u660e\u786e\u5339\u914d \uff08 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u4e0d\u660e\u786e\u5339\u914d \uff09\u3002 \u9ed8\u8ba4\u5b9e\u53c2 \u662f\u4e3a\u51fd\u6570\u5f62\u53c2\u63d0\u4f9b\u7684\u9ed8\u8ba4\u503c\uff08 8.12 - \u9ed8\u8ba4\u53c2\u6570 \uff09\u3002\u5e26\u9ed8\u8ba4\u5b9e\u53c2\u7684\u5f62\u53c2\u5fc5\u987b\u59cb\u7ec8\u662f\u6700\u53f3\u8fb9\u7684\u5f62\u53c2\uff0c\u5e76\u4e14\u5728\u89e3\u6790\u91cd\u8f7d\u51fd\u6570\u65f6\u4e0d\u4f7f\u7528\u5b83\u4eec\u6765\u533a\u5206\u51fd\u6570\u3002 \u51fd\u6570\u6a21\u677f\uff08 8.13 - \u51fd\u6570\u6a21\u677f \uff09\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7c7b\u4f3c\u51fd\u6570\u7684\u5b9a\u4e49\uff0c\u4f5c\u4e3a\u521b\u5efa\u76f8\u5173\u51fd\u6570\u7684\u6a21\u5f0f\u3002\u5728\u51fd\u6570\u6a21\u677f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 \u6a21\u677f\u7c7b\u578b \u4f5c\u4e3a\u4ee5\u540e\u8981\u6307\u5b9a\u7684\u4efb\u4f55\u7c7b\u578b\u7684\u5360\u4f4d\u7b26\u3002\u544a\u8bc9\u7f16\u8bd1\u5668\u6211\u4eec\u6b63\u5728\u5b9a\u4e49\u4e00\u4e2a\u6a21\u677f\u5e76\u58f0\u660e\u6a21\u677f\u7c7b\u578b\u7684\u8bed\u6cd5\u79f0\u4e3a \u6a21\u677f\u5f62\u53c2\u58f0\u660e \u3002 \u4ece\u51fd\u6570\u6a21\u677f(\u4f7f\u7528\u6a21\u677f\u7c7b\u578b)\u521b\u5efa\u51fd\u6570(\u4f7f\u7528\u7279\u5b9a\u7c7b\u578b)\u7684\u8fc7\u7a0b\u7b80\u79f0\u4e3a \u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316 (\u6216\u5b9e\u4f8b\u5316)\u3002\u5f53\u8fd9\u4e2a\u8fc7\u7a0b\u7531\u4e8e\u51fd\u6570\u8c03\u7528\u800c\u53d1\u751f\u65f6\uff0c\u5b83\u88ab\u79f0\u4e3a\u201c\u9690\u5f0f\u5b9e\u4f8b\u5316\u201d\u3002\u4e00\u4e2a\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u88ab\u79f0\u4e3a \u51fd\u6570\u5b9e\u4f8b (\u6216\u7b80\u79f0 \u5b9e\u4f8b \uff0c\u6709\u65f6\u4e5f\u79f0\u4e3a \u6a21\u677f\u51fd\u6570 )\u3002 \u6a21\u677f\u5b9e\u53c2\u63a8\u5bfc\u5141\u8bb8\u7f16\u8bd1\u5668\u4ece\u51fd\u6570\u8c03\u7528\u7684\u5b9e\u53c2\u63a8\u5bfc\u51fa\u5e94\u8be5\u7528\u4e8e\u5b9e\u4f8b\u5316\u51fd\u6570\u7684\u5b9e\u9645\u7c7b\u578b\u3002\u6a21\u677f\u53c2\u6570\u63a8\u5bfc\u4e0d\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002 \u6a21\u677f\u7c7b\u578b\u6709\u65f6\u88ab\u79f0\u4e3a \u6cdb\u578b\u7c7b\u578b \uff0c\u4f7f\u7528\u6a21\u677f\u7f16\u7a0b\u6709\u65f6\u88ab\u79f0\u4e3a \u6cdb\u578b\u7f16\u7a0b \u3002 \u5728C++ 20\u4e2d\uff0c\u5f53 auto \u5173\u952e\u5b57\u5728\u666e\u901a\u51fd\u6570\u4e2d\u7528\u4f5c\u5f62\u53c2\u7c7b\u578b\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u5c06\u51fd\u6570\u8f6c\u6362\u4e3a\u51fd\u6570\u6a21\u677f\uff0c\u6bcf\u4e2a auto \u5f62\u53c2\u90fd\u6210\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684\u6a21\u677f\u7c7b\u578b\u5f62\u53c2\u3002\u8fd9\u79cd\u521b\u5efa\u51fd\u6570\u6a21\u677f\u7684\u65b9\u6cd5\u79f0\u4e3a \u7f29\u5199\u51fd\u6570\u6a21\u677f \u3002","title":"8.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d"},{"location":"8-x-Chapter-8-summary-and-quiz/#_1","text":"\u5c06\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u7684\u8fc7\u7a0b\u53eb\u505a \u7c7b\u578b\u8f6c\u6362 \u3002 \u5f53\u9700\u8981\u4e00\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u63d0\u4f9b\u4e86\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u65f6\uff0c\u5219\u4f1a\u6267\u884c \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 (\u4e5f\u79f0\u4e3a\u81ea\u52a8\u7c7b\u578b\u8f6c\u6362\u6216\u5f3a\u5236\u8f6c\u6362)\u3002\u5982\u679c\u7f16\u8bd1\u5668\u80fd\u591f\u627e\u5230\u4e24\u79cd\u7c7b\u578b\u4e4b\u95f4\u8fdb\u8fdb\u884c\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u7684\u89c4\u5219\uff0c\u5219\u5b83\u4f1a\u6267\u884c\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\uff0c\u5426\u5219\u5c31\u4f1a\u4ea7\u751f\u7f16\u8bd1\u62a5\u9519\u3002\uff08 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \uff09 C++ \u8bed\u8a00\u5b9a\u4e49\u4e86\u8bb8\u591a\u57fa\u672c\u7c7b\u578b\u4e4b\u95f4\u7684\u5185\u7f6e\u8f6c\u6362\u65b9\u5f0f(\u4ee5\u53ca\u4e00\u4e9b\u66f4\u9ad8\u7ea7\u7684\u7c7b\u578b\u7684\u8f6c\u6362)\uff0c\u79f0\u4e3a \u6807\u51c6\u8f6c\u6362 \u3002\u5176\u4e2d\u5305\u62ec \u6570\u503c\u63d0\u5347 \u3001 \u6570\u503c\u8f6c\u6362 \u548c \u7b97\u6570\u8f6c\u6362 \u6570\u503c\u63d0\u5347 \uff08 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 \uff09\u662f\u5c06\u8f83\u5c0f\u7684\u6570\u503c\u7c7b\u578b\u8f6c\u6362\u4e3a\u8f83\u5927\u7684\u6570\u503c\u7c7b\u578b(\u901a\u5e38\u4e3a int \u6216 double )\uff0c\u8fd9\u6837CPU\u5c31\u53ef\u4ee5\u5bf9\u4e0e\u5904\u7406\u5668\u81ea\u7136\u6570\u636e\u5927\u5c0f\u76f8\u5339\u914d\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002\u6570\u503c\u63d0\u5347\u5305\u62ec\u6574\u6570\u63d0\u5347\u548c\u6d6e\u70b9\u63d0\u5347\u3002\u6570\u5b57\u63d0\u5347\u662f\u4fdd\u503c\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4e0d\u4f1a\u4e22\u5931\u503c\u6216\u503c\u7684\u7cbe\u5ea6\u3002 \u6570\u503c\u8f6c\u6362 \uff08 8.3 - \u6570\u503c\u8f6c\u6362 \uff09\u6570\u503c\u8f6c\u6362\u662f\u57fa\u672c\u7c7b\u578b\u4e4b\u95f4\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u800c\u4e0d\u662f\u6570\u503c\u63d0\u5347\u3002\u6536\u7f29\u8f6c\u6362\u53ef\u80fd\u4f1a\u5bfc\u81f4\u503c\u6216\u7cbe\u5ea6\u7684\u635f\u5931\u3002 \u5728C++\u4e2d\uff0c\u67d0\u4e9b\u4e8c\u5143\u64cd\u4f5c\u7b26\u8981\u6c42\u5b83\u4eec\u7684\u64cd\u4f5c\u6570\u5177\u6709\u76f8\u540c\u7684\u7c7b\u578b\u3002\u5982\u679c\u63d0\u4f9b\u4e86\u4e0d\u540c\u7c7b\u578b\u7684\u64cd\u4f5c\u6570\uff0c\u5219\u4f7f\u7528\u4e00\u7ec4\u79f0\u4e3a \u666e\u901a\u7b97\u672f\u8f6c\u6362 \uff08 8.4 - \u7b97\u672f\u8f6c\u6362 \uff09\u89c4\u5219\u5c06\u5176\u4e2d\u4e00\u4e2a\u6216\u4e24\u4e2a\u64cd\u4f5c\u6570\u9690\u5f0f\u8f6c\u6362\u4e3a\u5339\u914d\u7c7b\u578b\u3002 \u5f53\u7a0b\u5e8f\u5458\u663e\u5f0f\u5730\u901a\u8fc7 cast \u6765\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u79f0\u4e3a \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \uff08 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast \uff09\u3002C++\u652f\u63015\u79cd\u7c7b\u578b\u5f3a\u5236\u8f6c\u6362\uff1a C\u98ce\u683c\u7c7b\u578b\u8f6c\u6362 \u3001 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u3001 const \u7c7b\u578b\u8f6c\u6362 \u3001 \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u548c \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 \u3002\u4e00\u822c\u6765\u8bf4\uff0c\u4f60\u5e94\u8be5\u907f\u514d\u4f7f\u7528\"C\u98ce\u683c\u7c7b\u578b\u8f6c\u6362\u201d\u3001\u201cconst\u7c7b\u578b\u8f6c\u6362\u201d\u548c\u201c\u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362\u201d\u3002 static_cast \u7528\u4e8e\u5c06\u4e00\u4e2a\u503c\u4ece\u4e00\u79cd\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u7c7b\u578b\uff0c\u5b83\u662f\u76ee\u524dC++\u4e2d\u88ab\u6700\u591a\u4f7f\u7528\u7684\u5f3a\u5236\u8f6c\u6362\u3002 typedef \u548c\u7c7b\u578b\u522b\u540d \u5141\u8bb8\u7a0b\u5e8f\u5458\u4e3a\u6570\u636e\u7c7b\u578b\u521b\u5efa\u522b\u540d\u3002\u8fd9\u4e9b\u522b\u540d\u4e0d\u662f\u65b0\u7c7b\u578b\uff0c\u5e76\u4e14\u4e0e\u539f\u7c7b\u578b\u7684\u64cd\u4f5c\u76f8\u540c\u3002Typedefs \u548c\u7c7b\u578b\u522b\u540d\u4e0d\u63d0\u4f9b\u4efb\u4f55\u7c7b\u578b\u5b89\u5168\u673a\u5236\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u8981\u5047\u5b9a\u7c7b\u578b\u522b\u540d\u548c\u5176\u539f\u7c7b\u578b\u662f\u6709\u533a\u522b\u7684\u3002 auto \u5173\u952e\u5b57\u6709\u5f88\u591a\u7528\u9014\u3002\u9996\u5148\uff0c auto \u53ef\u7528\u4e8e\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad( 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad )\uff0c\u5b83\u5c06\u4ece\u53d8\u91cf\u7684\u521d\u59cb\u5316\u503c\u63a8\u65ad\u51fa\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u4f46\u662f\uff0c\u7c7b\u578b\u63a8\u65ad\u4f1a\u4e22\u5f03 const \u548c\u5f15\u7528\uff0c\u6240\u4ee5\u5982\u679c\u9700\u8981\uff0c\u8bf7\u786e\u4fdd\u5c06\u5b83\u4eec\u6dfb\u52a0\u56de\u6765\u3002 auto \u4e5f\u53ef\u4ee5\u7528\u4f5c\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u7684\u63a8\u65ad\uff08 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad \uff09\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u4ece\u51fd\u6570\u7684 return \u8bed\u53e5\u4e2d\u63a8\u65ad\u51fa\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\uff0c\u4e0d\u8fc7\u5bf9\u4e8e\u666e\u901a\u51fd\u6570\u5e94\u8be5\u907f\u514d\u8fd9\u6837\u505a\u3002 auto \u8fd8\u88ab\u7528\u4f5c\u540e\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\u8bed\u6cd5\u3002 \u51fd\u6570\u91cd\u8f7d \uff08 8.9 - \u51fd\u6570\u91cd\u8f7d \uff09\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u76f8\u540c\u7684\u540d\u79f0\u521b\u5efa\u591a\u4e2a\u51fd\u6570\uff0c\u53ea\u8981\u6bcf\u4e2a\u76f8\u540c\u540d\u79f0\u7684\u51fd\u6570\u5177\u6709\u4e0d\u540c\u7684\u5f62\u53c2\u7c7b\u578b\u96c6\u5408\u5373\u53ef(\u6216\u8005\u53ef\u4ee5\u7528\u5176\u4ed6\u65b9\u5f0f\u533a\u5206\u51fd\u6570)\u3002\u8fd9\u6837\u7684\u51fd\u6570\u79f0\u4e3a\u91cd\u8f7d\u51fd\u6570(\u6216\u7b80\u79f0 \u91cd\u8f7d )\u3002\u8fd4\u56de\u7c7b\u578b\u4e0d\u80fd\u88ab\u7528\u6765\u8fdb\u884c\u91cd\u8f7d\u51fd\u6570\u7684\u533a\u5206\u3002 \u5728\u89e3\u6790\u91cd\u8f7d\u51fd\u6570\u65f6\uff08 8.10 - \u51fd\u6570\u91cd\u8f7d\u548c\u533a\u5206 \uff09\uff0c\u5982\u679c\u6ca1\u6709\u627e\u5230\u7cbe\u786e\u5339\u914d\uff0c\u7f16\u8bd1\u5668\u5c06\u4f18\u5148\u9009\u62e9\u53ef\u4ee5\u901a\u8fc7\u6570\u503c\u63d0\u5347\u5339\u914d\u7684\u91cd\u8f7d\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u90a3\u4e9b\u9700\u8981\u6570\u503c\u8f6c\u6362\u7684\u91cd\u8f7d\u51fd\u6570\u3002\u5f53\u5bf9\u5df2\u91cd\u8f7d\u7684\u51fd\u6570\u8fdb\u884c\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u5c1d\u8bd5\u6839\u636e\u51fd\u6570\u8c03\u7528\u4e2d\u4f7f\u7528\u7684\u53c2\u6570\u5c06\u51fd\u6570\u8c03\u7528\u4e0e\u9002\u5f53\u7684\u91cd\u8f7d\u5339\u914d\u3002\u8fd9\u88ab\u79f0\u4e3a \u91cd\u8f7d\u89e3\u6790 \u3002 \u5f53\u7f16\u8bd1\u5668\u53d1\u73b0\u4e24\u4e2a\u6216\u591a\u4e2a\u51fd\u6570\u53ef\u4ee5\u5c06\u51fd\u6570\u8c03\u7528\u4e0e\u91cd\u8f7d\u51fd\u6570\u5339\u914d\uff0c\u4f46\u65e0\u6cd5\u786e\u5b9a\u54ea\u4e00\u4e2a\u662f\u6700\u597d\u7684\u65f6\uff0c\u5c31\u4f1a\u51fa\u73b0 \u4e0d\u660e\u786e\u5339\u914d \uff08 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u4e0d\u660e\u786e\u5339\u914d \uff09\u3002 \u9ed8\u8ba4\u5b9e\u53c2 \u662f\u4e3a\u51fd\u6570\u5f62\u53c2\u63d0\u4f9b\u7684\u9ed8\u8ba4\u503c\uff08 8.12 - \u9ed8\u8ba4\u53c2\u6570 \uff09\u3002\u5e26\u9ed8\u8ba4\u5b9e\u53c2\u7684\u5f62\u53c2\u5fc5\u987b\u59cb\u7ec8\u662f\u6700\u53f3\u8fb9\u7684\u5f62\u53c2\uff0c\u5e76\u4e14\u5728\u89e3\u6790\u91cd\u8f7d\u51fd\u6570\u65f6\u4e0d\u4f7f\u7528\u5b83\u4eec\u6765\u533a\u5206\u51fd\u6570\u3002 \u51fd\u6570\u6a21\u677f\uff08 8.13 - \u51fd\u6570\u6a21\u677f \uff09\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7c7b\u4f3c\u51fd\u6570\u7684\u5b9a\u4e49\uff0c\u4f5c\u4e3a\u521b\u5efa\u76f8\u5173\u51fd\u6570\u7684\u6a21\u5f0f\u3002\u5728\u51fd\u6570\u6a21\u677f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 \u6a21\u677f\u7c7b\u578b \u4f5c\u4e3a\u4ee5\u540e\u8981\u6307\u5b9a\u7684\u4efb\u4f55\u7c7b\u578b\u7684\u5360\u4f4d\u7b26\u3002\u544a\u8bc9\u7f16\u8bd1\u5668\u6211\u4eec\u6b63\u5728\u5b9a\u4e49\u4e00\u4e2a\u6a21\u677f\u5e76\u58f0\u660e\u6a21\u677f\u7c7b\u578b\u7684\u8bed\u6cd5\u79f0\u4e3a \u6a21\u677f\u5f62\u53c2\u58f0\u660e \u3002 \u4ece\u51fd\u6570\u6a21\u677f(\u4f7f\u7528\u6a21\u677f\u7c7b\u578b)\u521b\u5efa\u51fd\u6570(\u4f7f\u7528\u7279\u5b9a\u7c7b\u578b)\u7684\u8fc7\u7a0b\u7b80\u79f0\u4e3a \u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316 (\u6216\u5b9e\u4f8b\u5316)\u3002\u5f53\u8fd9\u4e2a\u8fc7\u7a0b\u7531\u4e8e\u51fd\u6570\u8c03\u7528\u800c\u53d1\u751f\u65f6\uff0c\u5b83\u88ab\u79f0\u4e3a\u201c\u9690\u5f0f\u5b9e\u4f8b\u5316\u201d\u3002\u4e00\u4e2a\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u88ab\u79f0\u4e3a \u51fd\u6570\u5b9e\u4f8b (\u6216\u7b80\u79f0 \u5b9e\u4f8b \uff0c\u6709\u65f6\u4e5f\u79f0\u4e3a \u6a21\u677f\u51fd\u6570 )\u3002 \u6a21\u677f\u5b9e\u53c2\u63a8\u5bfc\u5141\u8bb8\u7f16\u8bd1\u5668\u4ece\u51fd\u6570\u8c03\u7528\u7684\u5b9e\u53c2\u63a8\u5bfc\u51fa\u5e94\u8be5\u7528\u4e8e\u5b9e\u4f8b\u5316\u51fd\u6570\u7684\u5b9e\u9645\u7c7b\u578b\u3002\u6a21\u677f\u53c2\u6570\u63a8\u5bfc\u4e0d\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002 \u6a21\u677f\u7c7b\u578b\u6709\u65f6\u88ab\u79f0\u4e3a \u6cdb\u578b\u7c7b\u578b \uff0c\u4f7f\u7528\u6a21\u677f\u7f16\u7a0b\u6709\u65f6\u88ab\u79f0\u4e3a \u6cdb\u578b\u7f16\u7a0b \u3002 \u5728C++ 20\u4e2d\uff0c\u5f53 auto \u5173\u952e\u5b57\u5728\u666e\u901a\u51fd\u6570\u4e2d\u7528\u4f5c\u5f62\u53c2\u7c7b\u578b\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u5c06\u51fd\u6570\u8f6c\u6362\u4e3a\u51fd\u6570\u6a21\u677f\uff0c\u6bcf\u4e2a auto \u5f62\u53c2\u90fd\u6210\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684\u6a21\u677f\u7c7b\u578b\u5f62\u53c2\u3002\u8fd9\u79cd\u521b\u5efa\u51fd\u6570\u6a21\u677f\u7684\u65b9\u6cd5\u79f0\u4e3a \u7f29\u5199\u51fd\u6570\u6a21\u677f \u3002","title":"\u590d\u4e60"},{"location":"9-1-Introduction-to-compound-data-types/","tags":["compound data types"],"text":"Key Takeaway C++\u652f\u6301\u4e0b\u5217\u590d\u5408\u6570\u636e\u7ed3\u6784 \u51fd\u6570 Functions \u6570\u7ec4 Arrays \u6307\u9488 Pointer types: \u6307\u5411\u5bf9\u8c61\u7684\u6307\u9488 Pointer to object \u6307\u5411\u51fd\u6570\u7684\u6307\u9488 Pointer to function \u6307\u5411\u6210\u5458\u7684\u6307\u9488 Pointer to member types: \u6307\u5411\u6570\u636e\u6210\u5458\u7684\u6307\u9488 Pointer to data member \u6307\u5411\u6210\u5458\u51fd\u6570\u7684\u6307\u9488 Pointer to member function \u5f15\u7528 Reference types: \u5de6\u503c\u5f15\u7528 L-value references \u53f3\u503c\u5f15\u7528 R-value references \u679a\u4e3e Enumerated types: \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b Unscoped enumerations \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e Scoped enumerations \u7c7b Class types: \u7ed3\u6784\u4f53 Structs \u7c7b Classes \u8054\u5408\u4f53 Unions \u5728 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u57fa\u7840\u6570\u636e\u7c7b\u578b\uff0c\u5b83\u4eec\u662fC++\u8bed\u8a00\u6838\u5fc3\u7684\u4e00\u90e8\u5206\u3002 \u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u4f7f\u7528\u8fd9\u4e9b\u57fa\u7840\u6570\u636e\u7c7b\u578b\u7f16\u5199\u4e86\u4e00\u4e9b\u7a0b\u5e8f\uff0c\u5c24\u5176\u591a\u5730\u7528\u5230\u4e86 int \u3002\u5c3d\u7ba1\u8fd9\u4e9b\u57fa\u672c\u6570\u636e\u7c7b\u578b\u975e\u5e38\u6709\u7528\u4e5f\u975e\u5e38\u6613\u7528\uff0c\u5b83\u4eec\u5e76\u4e0d\u80fd\u6db5\u76d6\u6211\u4eec\u6240\u6709\u7684\u9700\u6c42\uff0c\u5c24\u5176\u662f\u5728\u5f53\u6211\u4eec\u7f16\u5199\u590d\u6742\u7a0b\u5e8f\u7684\u65f6\u5019\u3002 \u4f8b\u5982\uff0c\u5f53\u7f16\u5199\u4e00\u4e2a\u8ba1\u7b97\u7a0b\u5e8f\u5bf9\u4e24\u4e2a\u5206\u6570\u8fdb\u884c\u76f8\u4e58\u7684\u65f6\u5019\uff0c\u4f60\u8be5\u5982\u4f55\u8868\u793a\u5206\u6570\u5462\uff1f\u4f60\u53ef\u80fd\u4f1a\u60f3\u5230\u4f7f\u7528\u4e00\u7ec4\u6574\u578b\uff08\u4e00\u4e2a\u4f5c\u4e3a\u5206\u5b50\uff0c\u4e00\u4e2a\u4f5c\u4e3a\u5206\u6bcd\uff09\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include int main () { // Our first fraction int num1 {}; int den1 {}; // Our second fraction int num2 {}; int den2 {}; char ignore {}; std :: cout << \"Enter a fraction: \" ; std :: cin >> num1 >> ignore >> den1 ; std :: cout << \"Enter a fraction: \" ; std :: cin >> num2 >> ignore >> den2 ; std :: cout << \"The two fractions multiplied: \" << num1 * num2 << '/' << den1 * den2 ; return 0 ; } \u8fd0\u884c\u7a0b\u5e8f\uff1a 1 2 3 Enter a fraction: 1/2 Enter a fraction: 3/4 The two fractions multiplied: 3/8 \u5c3d\u7ba1\u7a0b\u5e8f\u53ef\u4ee5\u8fd0\u884c\uff0c\u4f46\u662f\u8fd8\u6709\u5f88\u591a\u53ef\u4ee5\u6539\u8fdb\u7684\u5730\u65b9\u3002\u9996\u5148\uff0c\u4e00\u5bf9\u6574\u578b\u6570\u7684\u4e24\u4e2a\u6574\u578b\u7684\u8054\u7cfb\u662f\u5f88\u677e\u6563\u7684\u2014\u2014\u79bb\u5f00\u6ce8\u91ca\u548c\u4e0a\u4e0b\u6587\u4e4b\u540e\uff0c\u4f60\u5f88\u96be\u5c06\u5b83\u4eec\u8054\u7cfb\u8d77\u6765\u3002\u7b2c\u4e8c\uff0c\u4e3a\u4e86\u9075\u5faaDRY\u539f\u5219 \uff0c\u6211\u4eec\u5e94\u8be5\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u4e13\u95e8\u5904\u7406\u5206\u6570\u7684\u8f93\u5165\uff0c\u4f46\u53ef\u60dc\u7684\u51fd\u6570\u53ea\u80fd\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u6240\u4ee5\u6211\u4eec\u6ca1\u529e\u6cd5\u5c06\u5206\u5b50\u5206\u6bcd\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002 \u73b0\u5728\uff0c\u8003\u8651\u8fd9\u6837\u4e00\u79cd\u6e05\u6670\uff0c\u4f60\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u5305\u542b\u5458\u5de5ID\u7684\u5217\u8868\u3002\u4f60\u4f1a\u600e\u4e48\u505a\u5462\uff1f\u4e5f\u8bb8\u662f\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 int main () { int id1 { 42 }; int id2 { 57 }; int id3 { 162 }; // and so on } \u4f46\u662f\u5982\u679c\u4f60\u6709100\u591a\u540d\u5458\u5de5\u600e\u4e48\u529e\uff1f\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b9a\u4e49100\u4e2a\u53d8\u91cf\u540d\u3002\u5176\u6b21\u5982\u679c\u4f60\u9700\u8981\u5c06\u5b83\u4eec\u5168\u90e8\u6253\u5370\u51fa\u6765\u600e\u4e48\u529e\uff1f\u4f60\u53c8\u5982\u4f55\u5c06\u5b83\u4eec\u4f20\u9012\u7ed9\u51fd\u6570\u5462\uff1f\u4f60\u9700\u8981\u6572\u5f88\u591a\u5f88\u591a\u7684\u4ee3\u7801\uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u53ef\u6301\u7eed\u7684\u3002 \u663e\u7136\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u53ea\u80fd\u966a\u6211\u4eec\u8d70\u8fd9\u4e48\u8fdc\u4e86\u3002 \u590d\u5408\u6570\u636e\u7c7b\u578b \u5e78\u8fd0\u7684\u662f\uff0cC++\u652f\u6301\u7b2c\u4e8c\u7c7b\u6570\u636e\u7c7b\u578b\uff0c\u79f0\u4e3a \u590d\u5408\u6570\u636e\u7c7b\u578b \u3002\u590d\u5408\u6570\u636e\u7c7b\u578b\uff08\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a\u7ec4\u5408\u6570\u636e\u7c7b\u578b\uff09\u662f\u5efa\u7acb\u5728\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6216\u5176\u4ed6\u590d\u5408\u6570\u636e\u7c7b\u578b\u4e0a\u7684\u7c7b\u578b\uff0c\u6bcf\u4e2a\u590d\u6742\u6570\u636e\u7c7b\u578b\u90fd\u6709\u5176\u72ec\u7279\u7684\u5c5e\u6027\u3002 \u5728\u672c\u7ae0\u8282\u53ca\u540e\u7eed\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4f1a\u4f7f\u7528\u590d\u6742\u6570\u636e\u7c7b\u578b\u4f18\u96c5\u5730\u89e3\u51b3\u4e0a\u9762\u63d0\u5230\u7684\u90a3\u4e9b\u4ee4\u6211\u4eec\u5934\u75db\u7684\u95ee\u9898\u3002 C++ \u652f\u6301\u4e0b\u5217\u590d\u5408\u6570\u636e\u7c7b\u578b\uff1a \u51fd\u6570 Functions \u6570\u7ec4 Arrays \u6307\u9488 Pointer types: \u6307\u5411\u5bf9\u8c61\u7684\u6307\u9488 Pointer to object \u6307\u5411\u51fd\u6570\u7684\u6307\u9488 Pointer to function \u6307\u5411\u6210\u5458\u7684\u6307\u9488 Pointer to member types: \u6307\u5411\u6570\u636e\u6210\u5458\u7684\u6307\u9488 Pointer to data member \u6307\u5411\u6210\u5458\u51fd\u6570\u7684\u6307\u9488 Pointer to member function \u5f15\u7528 Reference types: \u5de6\u503c\u5f15\u7528 L-value references \u53f3\u503c\u5f15\u7528 R-value references \u679a\u4e3e Enumerated types: \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b Unscoped enumerations \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e Scoped enumerations \u7c7b Class types: \u7ed3\u6784\u4f53 Structs \u7c7b Classes \u8054\u5408\u4f53 Unions \u6211\u4eec\u5176\u5b9e\u5df2\u7ecf\u5728\u9891\u7e41\u4f7f\u7528\u4e00\u79cd\u590d\u5408\u7c7b\u578b\u2014\u2014\u51fd\u6570\u3002\u4f8b\u5982\uff1a 1 2 3 void doSomething ( int x , double y ) { } \u8fd9\u4e2a\u51fd\u6570\u7684\u7c7b\u578b\u662f void(int, double) \u6ce8\u610f\uff0c\u8fd9\u4e2a\u7c7b\u578b\u5305\u542b\u4e86\u591a\u4e2a\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u4ece\u800c\u5f62\u6210\u4e86\u590d\u5408\u7c7b\u578b\u3002\u5f53\u7136\uff0c\u51fd\u6570\u4e5f\u6709\u5176\u7279\u6b8a\u7684\u884c\u4e3a\uff08\u4f8b\u5982\uff1a\u53ef\u4ee5\u88ab\u8c03\u7528\uff09\u3002 \u5173\u4e8e\u8fd9\u4e00\u8bdd\u9898\u6709\u5f88\u591a\u5185\u5bb9\u9700\u8981\u4ecb\u7ecd\uff0c\u56e0\u6b64\u6211\u4eec\u4f1a\u4f7f\u7528\u4e09\u4e2a\u7ae0\u8282\u6765\u4ecb\u7ecd\u3002\u5728\u672c\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ecb\u7ecd\u4e00\u4e9b\u7b80\u5355\u76f4\u89c2\u7684\u590d\u5408\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec \u5de6\u503c\u5f15\u7528 \u548c\u6307\u9488\u3002\u5728\u4e0b\u4e00\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5219\u4f1a\u4ecb\u7ecd \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u3001 \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e \u548c\u57fa\u672c\u7684\u7ed3\u6784\u4f53\u3002\u7136\u540e\uff0c\u518d\u4e0b\u4e00\u7ae0\u8282\u6211\u4eec\u4f1a\u66f4\u52a0\u6df1\u5165\u5730\u4ecb\u7ecd\u975e\u5e38\u6709\u7528\u7684\u6570\u7ec4\u7c7b\u578b\uff0c\u5305\u62ec std::string ( 4.17 - std::string \u7b80\u4ecb )\uff0c\u5b83\u5176\u5b9e\u662f\u4e00\u79cdclass\u7c7b\u578b\u3002 \u51c6\u5907\u597d\u4e86\u5417\uff01Let\u2019s go!","title":"9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b"},{"location":"9-1-Introduction-to-compound-data-types/#_1","text":"\u5e78\u8fd0\u7684\u662f\uff0cC++\u652f\u6301\u7b2c\u4e8c\u7c7b\u6570\u636e\u7c7b\u578b\uff0c\u79f0\u4e3a \u590d\u5408\u6570\u636e\u7c7b\u578b \u3002\u590d\u5408\u6570\u636e\u7c7b\u578b\uff08\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a\u7ec4\u5408\u6570\u636e\u7c7b\u578b\uff09\u662f\u5efa\u7acb\u5728\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6216\u5176\u4ed6\u590d\u5408\u6570\u636e\u7c7b\u578b\u4e0a\u7684\u7c7b\u578b\uff0c\u6bcf\u4e2a\u590d\u6742\u6570\u636e\u7c7b\u578b\u90fd\u6709\u5176\u72ec\u7279\u7684\u5c5e\u6027\u3002 \u5728\u672c\u7ae0\u8282\u53ca\u540e\u7eed\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4f1a\u4f7f\u7528\u590d\u6742\u6570\u636e\u7c7b\u578b\u4f18\u96c5\u5730\u89e3\u51b3\u4e0a\u9762\u63d0\u5230\u7684\u90a3\u4e9b\u4ee4\u6211\u4eec\u5934\u75db\u7684\u95ee\u9898\u3002","title":"\u590d\u5408\u6570\u636e\u7c7b\u578b"},{"location":"9-1-Introduction-to-compound-data-types/#c","text":"\u51fd\u6570 Functions \u6570\u7ec4 Arrays \u6307\u9488 Pointer types: \u6307\u5411\u5bf9\u8c61\u7684\u6307\u9488 Pointer to object \u6307\u5411\u51fd\u6570\u7684\u6307\u9488 Pointer to function \u6307\u5411\u6210\u5458\u7684\u6307\u9488 Pointer to member types: \u6307\u5411\u6570\u636e\u6210\u5458\u7684\u6307\u9488 Pointer to data member \u6307\u5411\u6210\u5458\u51fd\u6570\u7684\u6307\u9488 Pointer to member function \u5f15\u7528 Reference types: \u5de6\u503c\u5f15\u7528 L-value references \u53f3\u503c\u5f15\u7528 R-value references \u679a\u4e3e Enumerated types: \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b Unscoped enumerations \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e Scoped enumerations \u7c7b Class types: \u7ed3\u6784\u4f53 Structs \u7c7b Classes \u8054\u5408\u4f53 Unions \u6211\u4eec\u5176\u5b9e\u5df2\u7ecf\u5728\u9891\u7e41\u4f7f\u7528\u4e00\u79cd\u590d\u5408\u7c7b\u578b\u2014\u2014\u51fd\u6570\u3002\u4f8b\u5982\uff1a 1 2 3 void doSomething ( int x , double y ) { } \u8fd9\u4e2a\u51fd\u6570\u7684\u7c7b\u578b\u662f void(int, double) \u6ce8\u610f\uff0c\u8fd9\u4e2a\u7c7b\u578b\u5305\u542b\u4e86\u591a\u4e2a\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u4ece\u800c\u5f62\u6210\u4e86\u590d\u5408\u7c7b\u578b\u3002\u5f53\u7136\uff0c\u51fd\u6570\u4e5f\u6709\u5176\u7279\u6b8a\u7684\u884c\u4e3a\uff08\u4f8b\u5982\uff1a\u53ef\u4ee5\u88ab\u8c03\u7528\uff09\u3002 \u5173\u4e8e\u8fd9\u4e00\u8bdd\u9898\u6709\u5f88\u591a\u5185\u5bb9\u9700\u8981\u4ecb\u7ecd\uff0c\u56e0\u6b64\u6211\u4eec\u4f1a\u4f7f\u7528\u4e09\u4e2a\u7ae0\u8282\u6765\u4ecb\u7ecd\u3002\u5728\u672c\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ecb\u7ecd\u4e00\u4e9b\u7b80\u5355\u76f4\u89c2\u7684\u590d\u5408\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec \u5de6\u503c\u5f15\u7528 \u548c\u6307\u9488\u3002\u5728\u4e0b\u4e00\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5219\u4f1a\u4ecb\u7ecd \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u3001 \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e \u548c\u57fa\u672c\u7684\u7ed3\u6784\u4f53\u3002\u7136\u540e\uff0c\u518d\u4e0b\u4e00\u7ae0\u8282\u6211\u4eec\u4f1a\u66f4\u52a0\u6df1\u5165\u5730\u4ecb\u7ecd\u975e\u5e38\u6709\u7528\u7684\u6570\u7ec4\u7c7b\u578b\uff0c\u5305\u62ec std::string ( 4.17 - std::string \u7b80\u4ecb )\uff0c\u5b83\u5176\u5b9e\u662f\u4e00\u79cdclass\u7c7b\u578b\u3002 \u51c6\u5907\u597d\u4e86\u5417\uff01Let\u2019s go!","title":"C++ \u652f\u6301\u4e0b\u5217\u590d\u5408\u6570\u636e\u7c7b\u578b\uff1a"},{"location":"9-10-Pass-by-address-part-2/","tags":["pointer","address"],"text":"\u672c\u8282\u8bfe\u7ee7\u7eed 9.9 - \u6309\u5730\u5740\u4f20\u9012 \u7684\u5185\u5bb9\u3002 \u201c\u53ef\u9009\u201d\u53c2\u6570\u6309\u5730\u5740\u4f20\u9012 \u4f20\u9012\u5730\u5740\u7684\u4e00\u4e2a\u66f4\u5e38\u89c1\u7684\u7528\u9014\u662f\u5141\u8bb8\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u201c\u53ef\u9009\u201d\u53c2\u6570\u3002\u4e3e\u4f8b\u8bf4\u660e\u6bd4\u63cf\u8ff0\u66f4\u5bb9\u6613\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include void greet ( std :: string * name = nullptr ) { std :: cout << \"Hello \" ; std :: cout << ( name ? * name : \"guest\" ) << '\\n' ; } int main () { greet (); // we don't know who the user is yet std :: string joe { \"Joe\" }; greet ( & joe ); // we know the user is joe return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 Hello guest Hello Joe \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c greet() \u51fd\u6570\u7684\u53c2\u6570\u662f\u6309\u5730\u5740\u4f20\u9012\u7684\uff0c\u4e14\u5177\u6709\u9ed8\u8ba4\u503c nullptr \u3002\u5728 main() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4e24\u6b21\u8c03\u7528\u8be5\u51fd\u6570\u3002\u7b2c\u4e00\u6b21\u8c03\u7528\u65f6\uff0c\u6211\u4eec\u4e0d\u77e5\u9053\u7528\u6237\u662f\u8c01\uff0c\u6240\u4ee5\u8c03\u7528 greet() \u65f6\u6ca1\u6709\u4f20\u9012\u4efb\u4f55 \u5b9e\u53c2 \uff0c\u6b64\u65f6\u53c2\u6570 name \u4f7f\u7528\u9ed8\u8ba4\u503c nullptr \uff0c\u51fd\u6570\u6253\u5370 \u201cguest\u201d\u3002 \u7b2c\u4e8c\u6b21\u8c03\u7528\u65f6\uff0c\u6211\u4eec\u4f20\u9012\u4e86\u4e00\u4e2a\u6709\u6548\u7684\u7528\u6237\u2014\u2014 greet(&joe) \u3002\u6b64\u65f6 name \u53c2\u6570\u901a\u8fc7\u5730\u5740\u5f97\u5230\u4e86 joe \uff0c\u51fd\u6570\u4f7f\u7528\u5b83\u6253\u5370\u4e86\u7528\u6237\u540d \u201cJoe\u201d\u3002 \u7136\u800c\uff0c\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u51fd\u6570\u91cd\u8f7d\u662f\u5b9e\u73b0\u76f8\u540c\u7ed3\u679c\u7684\u66f4\u597d\u9009\u62e9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include #include void greet ( std :: string_view name ) { std :: cout << \"Hello \" << name << '\\n' ; } void greet () { greet ( \"guest\" ); } int main () { greet (); // we don't know who the user is yet std :: string joe { \"Joe\" }; greet ( joe ); // we know the user is joe return 0 ; } \u8fd9\u6837\u505a\u6709\u5f88\u591a\u597d\u5904\uff1a\u6211\u4eec\u4e0d\u518d\u9700\u8981\u62c5\u5fc3 \u89e3\u5f15\u7528 \u7a7a\u6307\u9488\uff0c\u800c\u4e14\u5982\u679c\u9700\u8981\u7684\u8bdd\uff0c\u6211\u4eec\u53ef\u4ee5\u4f20\u5165\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u3002 \u6539\u53d8\u6307\u9488\u53c2\u6570\u6307\u5411\u7684\u503c \u5f53\u5c06\u5730\u5740\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\uff0c\u8be5\u5730\u5740\u5c06\u4ece\u5b9e\u53c2\u590d\u5236\u5230\u6307\u9488\u5f62\u53c2\u4e2d(\u8fd9\u5f88\u597d\uff0c\u56e0\u4e3a\u590d\u5236\u5730\u5740\u975e\u5e38\u5feb)\u3002\u73b0\u5728\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include // [[maybe_unused]] gets rid of compiler warnings about ptr2 being set but not used void nullify ([[ maybe_unused ]] int * ptr2 ) { ptr2 = nullptr ; // Make the function parameter a null pointer } int main () { int x { 5 }; int * ptr { & x }; // ptr points to x std :: cout << \"ptr is \" << ( ptr ? \"non-null \\n \" : \"null \\n \" ); nullify ( ptr ); std :: cout << \"ptr is \" << ( ptr ? \"non-null \\n \" : \"null \\n \" ); return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 ptr is non-null ptr is non-null \u5982\u4f60\u6240\u89c1\uff0c\u4fee\u6539 \u5f62\u53c2 \u6307\u9488\u7684\u5730\u5740\u5bf9 \u5b9e\u53c2 \u6ca1\u6709\u5f71\u54cd( ptr \u4ecd\u7136\u6307\u5411 x )\u3002\u5f53\u8c03\u7528\u51fd\u6570 nullify() \u88ab\u8c03\u7528\u65f6 ptr2 \u62f7\u8d1d\u4e86\u4e00\u4efd ptr \u6240\u6301\u6709\u7684\u5730\u5740\uff08\u6307\u5411 x \uff09\u3002\u5f53\u51fd\u6570\u4fee\u6539 ptr2 \u7684\u503c\u65f6\uff0c\u5b83\u53ea\u662f\u4fee\u6539\u4e86\u8fd9\u4efd\u62f7\u8d1d\u7684\u526f\u672c\u7f62\u4e86\u3002 \u5982\u679c\u6211\u4eec\u60f3\u8ba9\u51fd\u6570\u6539\u53d8\u6307\u9488\u53c2\u6570\u7684\u6307\u5411\uff0c\u8be5\u600e\u4e48\u529e\u5462\uff1f \u901a\u8fc7\u5f15\u7528\u4f20\u5730\u5740\uff1fPass by address\u2026 by reference? \u662f\u7684\uff0c\u53ef\u4ee5\u8fd9\u4e48\u505a\u3002\u5c31\u50cf\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u666e\u901a\u53d8\u91cf\u4e00\u6837\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7 \u6309\u5f15\u7528\u4f20\u9012 \u6307\u9488\u3002 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5f15\u7528\u4f20\u9012 ptr2 \u6307\u5411\u7684\u5730\u5740\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void nullify ( int *& refptr ) // refptr is now a reference to a pointer { refptr = nullptr ; // Make the function parameter a null pointer } int main () { int x { 5 }; int * ptr { & x }; // ptr points to x std :: cout << \"ptr is \" << ( ptr ? \"non-null \\n \" : \"null \\n \" ); nullify ( ptr ); std :: cout << \"ptr is \" << ( ptr ? \"non-null \\n \" : \"null \\n \" ); return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 2 ptr is non-null ptr is null \u56e0\u4e3a refptr \u662f\u4e00\u4e2a\u6307\u9488\u7684\u5f15\u7528\uff0c\u6240\u4ee5\u5f53 ptr \u4f20\u5165\u65f6\uff0c refptr \u88ab\u7ed1\u5b9a ptr \u3002\u8fd9\u610f\u5473\u7740\u4efb\u4f55\u5bf9 refptr \u7684\u4fee\u6539\u90fd\u4f1a\u4f5c\u7528\u4e8e ptr \u3002 \u9898\u5916\u8bdd \u56e0\u4e3a\u5bf9\u6307\u9488\u7684\u5f15\u7528\u76f8\u5f53\u7f55\u89c1\uff0c\u5176\u8bed\u6cd5\u5f88\u5bb9\u6613\u6df7\u6dc6(\u5b83\u662f int*& \u8fd8\u662f int&* ?)\u597d\u6d88\u606f\u662f\uff0c\u5982\u679c\u53cd\u5411\u64cd\u4f5c\uff0c\u7f16\u8bd1\u5668\u5c06\u51fa\u9519\uff0c\u56e0\u4e3a\u4e0d\u80fd\u6709\u6307\u5411\u5f15\u7528\u7684\u6307\u9488(\u56e0\u4e3a\u6307\u9488\u5fc5\u987b\u4fdd\u5b58\u5bf9\u8c61\u7684\u5730\u5740\uff0c\u800c\u5f15\u7528\u4e0d\u662f\u5bf9\u8c61)\u3002\u7136\u540e\u4f60\u53ef\u4ee5\u628a\u5b83\u6362\u8fc7\u6765\u3002 \u4e3a\u4ec0\u4e48\u73b0\u5728\u4e0d\u63a8\u8350\u4f7f\u7528 0 \u6216 NULL \u8868\u793a\u7a7a\u6307\u9488\u4e86\uff1f\uff08\u9009\u8bfb\uff09 \u5728\u672c\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u89e3\u91ca\u4e3a\u4ec0\u4e48\u4e0d\u518d\u4f7f\u7528 0 \u6216 NULL \u3002 \u5b57\u9762\u91cf\u201c 0 \u201d\u65e2\u53ef\u4ee5\u88ab\u89e3\u91ca\u4e3a\u6574\u6570 \u5b57\u9762\u91cf \uff0c\u4e5f\u53ef\u4ee5\u88ab\u89e3\u91ca\u4e3a\u7a7a\u6307\u9488\u5b57\u9762\u91cf\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60f3\u8981\u533a\u5206\u5176\u51c6\u786e\u542b\u4e49\u5e76\u4e0d\u5bb9\u6613\u2014\u2014\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u8ba4\u4e3a\u6211\u4eec\u6307\u7684\u662f\u5176\u4e2d\u4e00\u4e2a\u542b\u4e49\uff0c\u800c\u6211\u4eec\u6307\u7684\u662f\u53e6\u4e00\u4e2a\u2014\u2014\u8fd9\u4f1a\u7ed9\u7a0b\u5e8f\u7684\u884c\u4e3a\u5e26\u6765\u610f\u60f3\u4e0d\u5230\u7684\u540e\u679c\u3002 \u9884\u5904\u7406\u5668 \u5b8f NULL \u5e76\u6ca1\u6709\u5728\u6807\u51c6\u4e2d\u660e\u786e\u5b9a\u4e49\uff0c\u5b83\u7684\u503c\u53ef\u80fd\u662f 0 , 0L , ((void*)0) , \u6216\u5176\u4ed6\u503c\u3002 \u5728\u8bfe\u7a0b 8.9 - \u51fd\u6570\u91cd\u8f7d \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u51fd\u6570 \u91cd\u8f7d (\u591a\u4e2a\u51fd\u6570\u53ef\u4ee5\u5177\u6709\u76f8\u540c\u7684\u540d\u79f0\uff0c\u53ea\u8981\u5b83\u4eec\u53ef\u4ee5\u901a\u8fc7\u5f62\u53c2\u7684\u6570\u91cf\u6216\u7c7b\u578b\u6765\u533a\u5206)\u3002\u7f16\u8bd1\u5668\u53ef\u4ee5\u901a\u8fc7\u4f5c\u4e3a\u51fd\u6570\u8c03\u7528\u4e00\u90e8\u5206\u4f20\u5165\u7684\u53c2\u6570\u6765\u786e\u5b9a\u60a8\u60f3\u8981\u54ea\u4e2a\u91cd\u8f7d\u51fd\u6570\u3002 \u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f7f\u7528 0 \u6216 NULL \u65f6\u53ef\u80fd\u5e26\u6765\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include #include // \u5b9a\u4e49\u4e86 NULL void print ( int x ) // \u51fd\u6570\u63a5\u53d7\u6574\u6570 { std :: cout << \"print(int): \" << x << '\\n' ; } void print ( int * ptr ) // \u63a5\u53d7\u4e00\u4e2a\u6574\u578b\u6307\u9488 { std :: cout << \"print(int*): \" << ( ptr ? \"non-null \\n \" : \"null \\n \" ); } int main () { int x { 5 }; int * ptr { & x }; print ( ptr ); // \u603b\u662f\u8c03\u7528 print(int*) \u56e0\u4e3aptr\u7c7b\u578b\u4e3a int* (good) print ( 0 ); // \u603b\u662f\u8c03\u7528 print(int) \u56e0\u4e3a 0 \u662f\u4e00\u4e2a\u6574\u578b\u5b57\u9762\u91cf (\u4f46\u613f\u8fd9\u662f\u6211\u4eec\u6240\u5e0c\u671b\u7684) print ( NULL ); // \u8fd9\u4e2a\u8bed\u53e5\u53ef\u80fd\u505a\u4ee5\u4e0b\u4efb\u4f55\u4e8b\uff1a // \u8c03\u7528 print(int) (Visual Studio \u4f1a\u8fd9\u4e48\u505a) // \u8c03\u7528 print(int*) // \u5bfc\u81f4\u6b67\u4e49\uff0c\u5f15\u53d1\u7f16\u8bd1\u9519\u8bef(gcc \u548c Clang \u4f1a\u8fd9\u4e48\u505a) print ( nullptr ); // \u603b\u662f\u8c03\u7528 print(int*) return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a(\u4f7f\u7528 Visual Studio)\uff0c\u6253\u5370\uff1a 1 2 3 4 print(int*): non-null print(int): 0 print(int): 0 print(int*): null \u5f53\u4f20\u9012\u53c2\u65700\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u4f18\u5148\u8c03\u7528 print(int) \u800c\u4e0d\u662f print(int*) \uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u95ee\u9898\uff0c\u5982\u679c\u6211\u4eec\u7684\u672c\u610f\u662f\u5e0c\u671b\u4f7f\u7528\u7a7a\u6307\u9488\u5e76\u8c03\u7528 print(int*) \u7684\u8bdd\u3002 \u5728 NULL \u88ab\u5b9a\u4e49\u4e3a\u503c 0 \u7684\u60c5\u51b5\u4e0b\uff0c print(NULL) \u4e5f\u4f1a\u8c03\u7528 print(int) \uff0c\u800c\u4e0d\u662f print(int*) \uff0c\u5c31\u50cf\u4f60\u671f\u671b\u7684NULL\u6307\u9488\u5b57\u9762\u91cf\u4e00\u6837\u3002\u5728 NULL \u6ca1\u6709\u5b9a\u4e49\u4e3a 0 \u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5176\u4ed6\u884c\u4e3a\uff0c\u5982\u8c03\u7528 print(int*) \u6216\u7f16\u8bd1\u9519\u8bef\u3002 \u4f7f\u7528 nullptr \u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u4e8c\u4e49\u6027(\u5b83\u603b\u662f\u4f1a\u5bfc\u81f4\u51fd\u6570 print(int*) \u88ab\u8c03\u7528)\uff0c\u56e0\u4e3a nullptr \u53ea\u80fd\u5339\u914d\u6307\u9488\u7c7b\u578b\u3002 std::nullptr_t (\u9009\u8bfb) \u56e0\u4e3a nullptr \u53ef\u4ee5\u5728\u91cd\u8f7d\u51fd\u6570\u4e2d\u533a\u5206\u4e0e\u6574\u578b\u503c\uff0c\u6240\u4ee5\u5b83\u5fc5\u5b9a\u5c5e\u4e8e\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\u3002\u90a3\u4e48\uff0c nullptr \u662f\u4ec0\u4e48\u7c7b\u578b\u5462\uff1f\u5b9e\u9645\u4e0a\uff0c nullptr \u7684\u7c7b\u578b\u4e3a std::nullptr_t (\u5b9a\u4e49\u5728 )\u3002 std::nullptr_t \u53ea\u80fd\u4fdd\u5b58\u4e00\u4e2a\u503c\uff1a nullptr \uff01\u5c3d\u7ba1\u6709\u4eba\u89c9\u5f97\u8fd9\u5f88\u8822\uff0c\u4f46\u662f\u6709\u4e00\u79cd\u60c5\u51b5\u4e0b\u662f\u6709\u7528\u7684\u3002\u5982\u679c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u5e76\u4e14\u53ea\u63a5\u6536 nullptr \u5b57\u9762\u91cf \u5b9e\u53c2\uff0c\u5219\u53ef\u4ee5\u8ba9\u5f62\u53c2\u4e3a std::nullptr_t \u7c7b\u578b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include // for std::nullptr_t void print ( std :: nullptr_t ) { std :: cout << \"in print(std::nullptr_t) \\n \" ; } void print ( int * ) { std :: cout << \"in print(int*) \\n \" ; } int main () { print ( nullptr ); // calls print(std::nullptr_t) int x { 5 }; int * ptr { & x }; print ( ptr ); // calls print(int*) ptr = nullptr ; print ( ptr ); // calls print(int*) (since ptr has type int*) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570\u8c03\u7528 print(nullptr) \u4f1a\u88ab\u89e3\u6790\u4e3a print(std::nullptr_t) \u800c\u4e0d\u662f print(int*) \uff0c\u56e0\u4e3a\u524d\u8005\u65e0\u9700\u7c7b\u578b\u8f6c\u6362\u5373\u53ef\u5339\u914d\u3002 \u6709\u4e00\u79cd\u60c5\u51b5\u53ef\u80fd\u4f1a\u8ba9\u4eba\u6709\u70b9\u56f0\u60d1\uff0c\u90a3\u5c31\u662f\u5f53 ptr \u503c\u4e3a nullptr \u65f6\u8c03\u7528 print(ptr) \u3002\u8bb0\u4f4f\uff0c\u51fd\u6570\u91cd\u8f7d\u5339\u914d\u7684\u662f\u7c7b\u578b\uff0c\u800c\u4e0d\u662f\u503c\uff0c ptr \u7684\u7c7b\u578b\u662f int* \u3002\u56e0\u6b64\uff0c print(int*) \u5c06\u88ab\u5339\u914d\u3002 print(std::nullptr_t) \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u751a\u81f3\u4e0d\u9700\u8981\u8003\u8651\uff0c\u56e0\u4e3a\u6307\u9488\u7c7b\u578b\u4e0d\u4f1a\u9690\u5f0f\u8f6c\u6362\u4e3a std::nullptr_t \u3002 \u4f60\u53ef\u80fd\u6c38\u8fdc\u90fd\u4e0d\u9700\u8981\u4f7f\u7528\u5b83\uff0c\u4f46\u4e3a\u4e86\u4ee5\u9632\u4e07\u4e00\uff0c\u77e5\u9053\u5b83\u662f\u5f88\u597d\u7684\u3002 \u5176\u5b9e\u90fd\u662f\u6309\u503c\u4f20\u9012 \u73b0\u5728\u60a8\u5df2\u7ecf\u7406\u89e3\u4e86\u901a\u8fc7\u5f15\u7528\u3001\u5730\u5740\u548c\u503c\u4f20\u9012\u4e4b\u95f4\u7684\u57fa\u672c\u533a\u522b\uff0c\u8ba9\u6211\u4eec\u6682\u65f6\u7b80\u5316\u4e00\u4e0b\u3002\uff1a\uff09 \u867d\u7136\u7f16\u8bd1\u5668\u901a\u5e38\u53ef\u4ee5\u5c06\u5f15\u7528\u4f18\u5316\u6389\uff0c\u4f46\u5728\u6709\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8fd9\u662f\u4e0d\u53ef\u80fd\u7684\uff0c\u800c\u5f15\u7528\u5b9e\u9645\u4e0a\u662f\u9700\u8981\u7684\u3002\u5f15\u7528\u901a\u5e38\u7531\u7f16\u8bd1\u5668\u4f7f\u7528\u6307\u9488\u5b9e\u73b0\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5728\u5e55\u540e\uff0c\u5f15\u7528\u4f20\u9012\u672c\u8d28\u4e0a\u53ea\u662f\u5730\u5740\u4f20\u9012(\u5bf9\u5f15\u7528\u7684\u8bbf\u95ee\u6267\u884c\u9690\u5f0f\u89e3\u5f15\u7528)\u3002 \u5728\u4e0a\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u63d0\u5230\u8fc7\uff0c\u901a\u8fc7\u5730\u5740\u4f20\u9012\u53ea\u662f\u5c06\u4e00\u4e2a\u5730\u5740\u4ece\u8c03\u7528\u8005\u590d\u5236\u5230\u88ab\u8c03\u7528\u51fd\u6570\u2014\u2014\u5b83\u53ea\u662f\u6309\u503c\u4f20\u9012\u4e00\u4e2a\u5730\u5740\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u8fd9\u6837\u7684\u7ed3\u8bba\uff1aC++\u786e\u5b9e\u662f\u6309\u503c\u4f20\u9012\u6240\u6709\u5185\u5bb9\u7684\uff01 \u6309\u5730\u5740\u4f20\u9012 \u6216 \u6309\u5f15\u7528\u4f20\u9012 \u7684\u53c2\u6570\uff0c\u5176\u7279\u522b\u4e4b\u5904\u53ea\u5728\u4e8e\u4f20\u9012\u8fdb\u6765\u7684\u5730\u5740\u53ef\u4ee5\u88ab\u89e3\u5f15\u7528\uff0c\u6240\u4ee5\u80fd\u901a\u8fc7\u5b83\u4fee\u6539\u5b9e\u53c2\uff0c\u800c\u666e\u901a\u5f62\u53c2\u4e0d\u80fd\u505a\u5230\u8fd9\u4e00\u70b9\u3002","title":"9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2"},{"location":"9-10-Pass-by-address-part-2/#_1","text":"\u4f20\u9012\u5730\u5740\u7684\u4e00\u4e2a\u66f4\u5e38\u89c1\u7684\u7528\u9014\u662f\u5141\u8bb8\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u201c\u53ef\u9009\u201d\u53c2\u6570\u3002\u4e3e\u4f8b\u8bf4\u660e\u6bd4\u63cf\u8ff0\u66f4\u5bb9\u6613\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include void greet ( std :: string * name = nullptr ) { std :: cout << \"Hello \" ; std :: cout << ( name ? * name : \"guest\" ) << '\\n' ; } int main () { greet (); // we don't know who the user is yet std :: string joe { \"Joe\" }; greet ( & joe ); // we know the user is joe return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 Hello guest Hello Joe \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c greet() \u51fd\u6570\u7684\u53c2\u6570\u662f\u6309\u5730\u5740\u4f20\u9012\u7684\uff0c\u4e14\u5177\u6709\u9ed8\u8ba4\u503c nullptr \u3002\u5728 main() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4e24\u6b21\u8c03\u7528\u8be5\u51fd\u6570\u3002\u7b2c\u4e00\u6b21\u8c03\u7528\u65f6\uff0c\u6211\u4eec\u4e0d\u77e5\u9053\u7528\u6237\u662f\u8c01\uff0c\u6240\u4ee5\u8c03\u7528 greet() \u65f6\u6ca1\u6709\u4f20\u9012\u4efb\u4f55 \u5b9e\u53c2 \uff0c\u6b64\u65f6\u53c2\u6570 name \u4f7f\u7528\u9ed8\u8ba4\u503c nullptr \uff0c\u51fd\u6570\u6253\u5370 \u201cguest\u201d\u3002 \u7b2c\u4e8c\u6b21\u8c03\u7528\u65f6\uff0c\u6211\u4eec\u4f20\u9012\u4e86\u4e00\u4e2a\u6709\u6548\u7684\u7528\u6237\u2014\u2014 greet(&joe) \u3002\u6b64\u65f6 name \u53c2\u6570\u901a\u8fc7\u5730\u5740\u5f97\u5230\u4e86 joe \uff0c\u51fd\u6570\u4f7f\u7528\u5b83\u6253\u5370\u4e86\u7528\u6237\u540d \u201cJoe\u201d\u3002 \u7136\u800c\uff0c\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u51fd\u6570\u91cd\u8f7d\u662f\u5b9e\u73b0\u76f8\u540c\u7ed3\u679c\u7684\u66f4\u597d\u9009\u62e9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include #include void greet ( std :: string_view name ) { std :: cout << \"Hello \" << name << '\\n' ; } void greet () { greet ( \"guest\" ); } int main () { greet (); // we don't know who the user is yet std :: string joe { \"Joe\" }; greet ( joe ); // we know the user is joe return 0 ; } \u8fd9\u6837\u505a\u6709\u5f88\u591a\u597d\u5904\uff1a\u6211\u4eec\u4e0d\u518d\u9700\u8981\u62c5\u5fc3 \u89e3\u5f15\u7528 \u7a7a\u6307\u9488\uff0c\u800c\u4e14\u5982\u679c\u9700\u8981\u7684\u8bdd\uff0c\u6211\u4eec\u53ef\u4ee5\u4f20\u5165\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u3002","title":"\u201c\u53ef\u9009\u201d\u53c2\u6570\u6309\u5730\u5740\u4f20\u9012"},{"location":"9-10-Pass-by-address-part-2/#_2","text":"\u5f53\u5c06\u5730\u5740\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\uff0c\u8be5\u5730\u5740\u5c06\u4ece\u5b9e\u53c2\u590d\u5236\u5230\u6307\u9488\u5f62\u53c2\u4e2d(\u8fd9\u5f88\u597d\uff0c\u56e0\u4e3a\u590d\u5236\u5730\u5740\u975e\u5e38\u5feb)\u3002\u73b0\u5728\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include // [[maybe_unused]] gets rid of compiler warnings about ptr2 being set but not used void nullify ([[ maybe_unused ]] int * ptr2 ) { ptr2 = nullptr ; // Make the function parameter a null pointer } int main () { int x { 5 }; int * ptr { & x }; // ptr points to x std :: cout << \"ptr is \" << ( ptr ? \"non-null \\n \" : \"null \\n \" ); nullify ( ptr ); std :: cout << \"ptr is \" << ( ptr ? \"non-null \\n \" : \"null \\n \" ); return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 ptr is non-null ptr is non-null \u5982\u4f60\u6240\u89c1\uff0c\u4fee\u6539 \u5f62\u53c2 \u6307\u9488\u7684\u5730\u5740\u5bf9 \u5b9e\u53c2 \u6ca1\u6709\u5f71\u54cd( ptr \u4ecd\u7136\u6307\u5411 x )\u3002\u5f53\u8c03\u7528\u51fd\u6570 nullify() \u88ab\u8c03\u7528\u65f6 ptr2 \u62f7\u8d1d\u4e86\u4e00\u4efd ptr \u6240\u6301\u6709\u7684\u5730\u5740\uff08\u6307\u5411 x \uff09\u3002\u5f53\u51fd\u6570\u4fee\u6539 ptr2 \u7684\u503c\u65f6\uff0c\u5b83\u53ea\u662f\u4fee\u6539\u4e86\u8fd9\u4efd\u62f7\u8d1d\u7684\u526f\u672c\u7f62\u4e86\u3002 \u5982\u679c\u6211\u4eec\u60f3\u8ba9\u51fd\u6570\u6539\u53d8\u6307\u9488\u53c2\u6570\u7684\u6307\u5411\uff0c\u8be5\u600e\u4e48\u529e\u5462\uff1f","title":"\u6539\u53d8\u6307\u9488\u53c2\u6570\u6307\u5411\u7684\u503c"},{"location":"9-10-Pass-by-address-part-2/#pass-by-address-by-reference","text":"\u662f\u7684\uff0c\u53ef\u4ee5\u8fd9\u4e48\u505a\u3002\u5c31\u50cf\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u666e\u901a\u53d8\u91cf\u4e00\u6837\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7 \u6309\u5f15\u7528\u4f20\u9012 \u6307\u9488\u3002 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5f15\u7528\u4f20\u9012 ptr2 \u6307\u5411\u7684\u5730\u5740\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void nullify ( int *& refptr ) // refptr is now a reference to a pointer { refptr = nullptr ; // Make the function parameter a null pointer } int main () { int x { 5 }; int * ptr { & x }; // ptr points to x std :: cout << \"ptr is \" << ( ptr ? \"non-null \\n \" : \"null \\n \" ); nullify ( ptr ); std :: cout << \"ptr is \" << ( ptr ? \"non-null \\n \" : \"null \\n \" ); return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 2 ptr is non-null ptr is null \u56e0\u4e3a refptr \u662f\u4e00\u4e2a\u6307\u9488\u7684\u5f15\u7528\uff0c\u6240\u4ee5\u5f53 ptr \u4f20\u5165\u65f6\uff0c refptr \u88ab\u7ed1\u5b9a ptr \u3002\u8fd9\u610f\u5473\u7740\u4efb\u4f55\u5bf9 refptr \u7684\u4fee\u6539\u90fd\u4f1a\u4f5c\u7528\u4e8e ptr \u3002 \u9898\u5916\u8bdd \u56e0\u4e3a\u5bf9\u6307\u9488\u7684\u5f15\u7528\u76f8\u5f53\u7f55\u89c1\uff0c\u5176\u8bed\u6cd5\u5f88\u5bb9\u6613\u6df7\u6dc6(\u5b83\u662f int*& \u8fd8\u662f int&* ?)\u597d\u6d88\u606f\u662f\uff0c\u5982\u679c\u53cd\u5411\u64cd\u4f5c\uff0c\u7f16\u8bd1\u5668\u5c06\u51fa\u9519\uff0c\u56e0\u4e3a\u4e0d\u80fd\u6709\u6307\u5411\u5f15\u7528\u7684\u6307\u9488(\u56e0\u4e3a\u6307\u9488\u5fc5\u987b\u4fdd\u5b58\u5bf9\u8c61\u7684\u5730\u5740\uff0c\u800c\u5f15\u7528\u4e0d\u662f\u5bf9\u8c61)\u3002\u7136\u540e\u4f60\u53ef\u4ee5\u628a\u5b83\u6362\u8fc7\u6765\u3002","title":"\u901a\u8fc7\u5f15\u7528\u4f20\u5730\u5740\uff1fPass by address\u2026 by reference?"},{"location":"9-10-Pass-by-address-part-2/#0-null","text":"\u5728\u672c\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u89e3\u91ca\u4e3a\u4ec0\u4e48\u4e0d\u518d\u4f7f\u7528 0 \u6216 NULL \u3002 \u5b57\u9762\u91cf\u201c 0 \u201d\u65e2\u53ef\u4ee5\u88ab\u89e3\u91ca\u4e3a\u6574\u6570 \u5b57\u9762\u91cf \uff0c\u4e5f\u53ef\u4ee5\u88ab\u89e3\u91ca\u4e3a\u7a7a\u6307\u9488\u5b57\u9762\u91cf\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60f3\u8981\u533a\u5206\u5176\u51c6\u786e\u542b\u4e49\u5e76\u4e0d\u5bb9\u6613\u2014\u2014\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u8ba4\u4e3a\u6211\u4eec\u6307\u7684\u662f\u5176\u4e2d\u4e00\u4e2a\u542b\u4e49\uff0c\u800c\u6211\u4eec\u6307\u7684\u662f\u53e6\u4e00\u4e2a\u2014\u2014\u8fd9\u4f1a\u7ed9\u7a0b\u5e8f\u7684\u884c\u4e3a\u5e26\u6765\u610f\u60f3\u4e0d\u5230\u7684\u540e\u679c\u3002 \u9884\u5904\u7406\u5668 \u5b8f NULL \u5e76\u6ca1\u6709\u5728\u6807\u51c6\u4e2d\u660e\u786e\u5b9a\u4e49\uff0c\u5b83\u7684\u503c\u53ef\u80fd\u662f 0 , 0L , ((void*)0) , \u6216\u5176\u4ed6\u503c\u3002 \u5728\u8bfe\u7a0b 8.9 - \u51fd\u6570\u91cd\u8f7d \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u51fd\u6570 \u91cd\u8f7d (\u591a\u4e2a\u51fd\u6570\u53ef\u4ee5\u5177\u6709\u76f8\u540c\u7684\u540d\u79f0\uff0c\u53ea\u8981\u5b83\u4eec\u53ef\u4ee5\u901a\u8fc7\u5f62\u53c2\u7684\u6570\u91cf\u6216\u7c7b\u578b\u6765\u533a\u5206)\u3002\u7f16\u8bd1\u5668\u53ef\u4ee5\u901a\u8fc7\u4f5c\u4e3a\u51fd\u6570\u8c03\u7528\u4e00\u90e8\u5206\u4f20\u5165\u7684\u53c2\u6570\u6765\u786e\u5b9a\u60a8\u60f3\u8981\u54ea\u4e2a\u91cd\u8f7d\u51fd\u6570\u3002 \u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f7f\u7528 0 \u6216 NULL \u65f6\u53ef\u80fd\u5e26\u6765\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include #include // \u5b9a\u4e49\u4e86 NULL void print ( int x ) // \u51fd\u6570\u63a5\u53d7\u6574\u6570 { std :: cout << \"print(int): \" << x << '\\n' ; } void print ( int * ptr ) // \u63a5\u53d7\u4e00\u4e2a\u6574\u578b\u6307\u9488 { std :: cout << \"print(int*): \" << ( ptr ? \"non-null \\n \" : \"null \\n \" ); } int main () { int x { 5 }; int * ptr { & x }; print ( ptr ); // \u603b\u662f\u8c03\u7528 print(int*) \u56e0\u4e3aptr\u7c7b\u578b\u4e3a int* (good) print ( 0 ); // \u603b\u662f\u8c03\u7528 print(int) \u56e0\u4e3a 0 \u662f\u4e00\u4e2a\u6574\u578b\u5b57\u9762\u91cf (\u4f46\u613f\u8fd9\u662f\u6211\u4eec\u6240\u5e0c\u671b\u7684) print ( NULL ); // \u8fd9\u4e2a\u8bed\u53e5\u53ef\u80fd\u505a\u4ee5\u4e0b\u4efb\u4f55\u4e8b\uff1a // \u8c03\u7528 print(int) (Visual Studio \u4f1a\u8fd9\u4e48\u505a) // \u8c03\u7528 print(int*) // \u5bfc\u81f4\u6b67\u4e49\uff0c\u5f15\u53d1\u7f16\u8bd1\u9519\u8bef(gcc \u548c Clang \u4f1a\u8fd9\u4e48\u505a) print ( nullptr ); // \u603b\u662f\u8c03\u7528 print(int*) return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a(\u4f7f\u7528 Visual Studio)\uff0c\u6253\u5370\uff1a 1 2 3 4 print(int*): non-null print(int): 0 print(int): 0 print(int*): null \u5f53\u4f20\u9012\u53c2\u65700\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u4f18\u5148\u8c03\u7528 print(int) \u800c\u4e0d\u662f print(int*) \uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u95ee\u9898\uff0c\u5982\u679c\u6211\u4eec\u7684\u672c\u610f\u662f\u5e0c\u671b\u4f7f\u7528\u7a7a\u6307\u9488\u5e76\u8c03\u7528 print(int*) \u7684\u8bdd\u3002 \u5728 NULL \u88ab\u5b9a\u4e49\u4e3a\u503c 0 \u7684\u60c5\u51b5\u4e0b\uff0c print(NULL) \u4e5f\u4f1a\u8c03\u7528 print(int) \uff0c\u800c\u4e0d\u662f print(int*) \uff0c\u5c31\u50cf\u4f60\u671f\u671b\u7684NULL\u6307\u9488\u5b57\u9762\u91cf\u4e00\u6837\u3002\u5728 NULL \u6ca1\u6709\u5b9a\u4e49\u4e3a 0 \u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5176\u4ed6\u884c\u4e3a\uff0c\u5982\u8c03\u7528 print(int*) \u6216\u7f16\u8bd1\u9519\u8bef\u3002 \u4f7f\u7528 nullptr \u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u4e8c\u4e49\u6027(\u5b83\u603b\u662f\u4f1a\u5bfc\u81f4\u51fd\u6570 print(int*) \u88ab\u8c03\u7528)\uff0c\u56e0\u4e3a nullptr \u53ea\u80fd\u5339\u914d\u6307\u9488\u7c7b\u578b\u3002","title":"\u4e3a\u4ec0\u4e48\u73b0\u5728\u4e0d\u63a8\u8350\u4f7f\u7528\u00a00\u00a0\u6216\u00a0NULL\u00a0\u8868\u793a\u7a7a\u6307\u9488\u4e86\uff1f\uff08\u9009\u8bfb\uff09"},{"location":"9-10-Pass-by-address-part-2/#stdnullptr_t","text":"\u56e0\u4e3a nullptr \u53ef\u4ee5\u5728\u91cd\u8f7d\u51fd\u6570\u4e2d\u533a\u5206\u4e0e\u6574\u578b\u503c\uff0c\u6240\u4ee5\u5b83\u5fc5\u5b9a\u5c5e\u4e8e\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\u3002\u90a3\u4e48\uff0c nullptr \u662f\u4ec0\u4e48\u7c7b\u578b\u5462\uff1f\u5b9e\u9645\u4e0a\uff0c nullptr \u7684\u7c7b\u578b\u4e3a std::nullptr_t (\u5b9a\u4e49\u5728 )\u3002 std::nullptr_t \u53ea\u80fd\u4fdd\u5b58\u4e00\u4e2a\u503c\uff1a nullptr \uff01\u5c3d\u7ba1\u6709\u4eba\u89c9\u5f97\u8fd9\u5f88\u8822\uff0c\u4f46\u662f\u6709\u4e00\u79cd\u60c5\u51b5\u4e0b\u662f\u6709\u7528\u7684\u3002\u5982\u679c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u5e76\u4e14\u53ea\u63a5\u6536 nullptr \u5b57\u9762\u91cf \u5b9e\u53c2\uff0c\u5219\u53ef\u4ee5\u8ba9\u5f62\u53c2\u4e3a std::nullptr_t \u7c7b\u578b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include // for std::nullptr_t void print ( std :: nullptr_t ) { std :: cout << \"in print(std::nullptr_t) \\n \" ; } void print ( int * ) { std :: cout << \"in print(int*) \\n \" ; } int main () { print ( nullptr ); // calls print(std::nullptr_t) int x { 5 }; int * ptr { & x }; print ( ptr ); // calls print(int*) ptr = nullptr ; print ( ptr ); // calls print(int*) (since ptr has type int*) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570\u8c03\u7528 print(nullptr) \u4f1a\u88ab\u89e3\u6790\u4e3a print(std::nullptr_t) \u800c\u4e0d\u662f print(int*) \uff0c\u56e0\u4e3a\u524d\u8005\u65e0\u9700\u7c7b\u578b\u8f6c\u6362\u5373\u53ef\u5339\u914d\u3002 \u6709\u4e00\u79cd\u60c5\u51b5\u53ef\u80fd\u4f1a\u8ba9\u4eba\u6709\u70b9\u56f0\u60d1\uff0c\u90a3\u5c31\u662f\u5f53 ptr \u503c\u4e3a nullptr \u65f6\u8c03\u7528 print(ptr) \u3002\u8bb0\u4f4f\uff0c\u51fd\u6570\u91cd\u8f7d\u5339\u914d\u7684\u662f\u7c7b\u578b\uff0c\u800c\u4e0d\u662f\u503c\uff0c ptr \u7684\u7c7b\u578b\u662f int* \u3002\u56e0\u6b64\uff0c print(int*) \u5c06\u88ab\u5339\u914d\u3002 print(std::nullptr_t) \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u751a\u81f3\u4e0d\u9700\u8981\u8003\u8651\uff0c\u56e0\u4e3a\u6307\u9488\u7c7b\u578b\u4e0d\u4f1a\u9690\u5f0f\u8f6c\u6362\u4e3a std::nullptr_t \u3002 \u4f60\u53ef\u80fd\u6c38\u8fdc\u90fd\u4e0d\u9700\u8981\u4f7f\u7528\u5b83\uff0c\u4f46\u4e3a\u4e86\u4ee5\u9632\u4e07\u4e00\uff0c\u77e5\u9053\u5b83\u662f\u5f88\u597d\u7684\u3002","title":"std::nullptr_t (\u9009\u8bfb)"},{"location":"9-10-Pass-by-address-part-2/#_3","text":"\u73b0\u5728\u60a8\u5df2\u7ecf\u7406\u89e3\u4e86\u901a\u8fc7\u5f15\u7528\u3001\u5730\u5740\u548c\u503c\u4f20\u9012\u4e4b\u95f4\u7684\u57fa\u672c\u533a\u522b\uff0c\u8ba9\u6211\u4eec\u6682\u65f6\u7b80\u5316\u4e00\u4e0b\u3002\uff1a\uff09 \u867d\u7136\u7f16\u8bd1\u5668\u901a\u5e38\u53ef\u4ee5\u5c06\u5f15\u7528\u4f18\u5316\u6389\uff0c\u4f46\u5728\u6709\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8fd9\u662f\u4e0d\u53ef\u80fd\u7684\uff0c\u800c\u5f15\u7528\u5b9e\u9645\u4e0a\u662f\u9700\u8981\u7684\u3002\u5f15\u7528\u901a\u5e38\u7531\u7f16\u8bd1\u5668\u4f7f\u7528\u6307\u9488\u5b9e\u73b0\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5728\u5e55\u540e\uff0c\u5f15\u7528\u4f20\u9012\u672c\u8d28\u4e0a\u53ea\u662f\u5730\u5740\u4f20\u9012(\u5bf9\u5f15\u7528\u7684\u8bbf\u95ee\u6267\u884c\u9690\u5f0f\u89e3\u5f15\u7528)\u3002 \u5728\u4e0a\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u63d0\u5230\u8fc7\uff0c\u901a\u8fc7\u5730\u5740\u4f20\u9012\u53ea\u662f\u5c06\u4e00\u4e2a\u5730\u5740\u4ece\u8c03\u7528\u8005\u590d\u5236\u5230\u88ab\u8c03\u7528\u51fd\u6570\u2014\u2014\u5b83\u53ea\u662f\u6309\u503c\u4f20\u9012\u4e00\u4e2a\u5730\u5740\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u8fd9\u6837\u7684\u7ed3\u8bba\uff1aC++\u786e\u5b9e\u662f\u6309\u503c\u4f20\u9012\u6240\u6709\u5185\u5bb9\u7684\uff01 \u6309\u5730\u5740\u4f20\u9012 \u6216 \u6309\u5f15\u7528\u4f20\u9012 \u7684\u53c2\u6570\uff0c\u5176\u7279\u522b\u4e4b\u5904\u53ea\u5728\u4e8e\u4f20\u9012\u8fdb\u6765\u7684\u5730\u5740\u53ef\u4ee5\u88ab\u89e3\u5f15\u7528\uff0c\u6240\u4ee5\u80fd\u901a\u8fc7\u5b83\u4fee\u6539\u5b9e\u53c2\uff0c\u800c\u666e\u901a\u5f62\u53c2\u4e0d\u80fd\u505a\u5230\u8fd9\u4e00\u70b9\u3002","title":"\u5176\u5b9e\u90fd\u662f\u6309\u503c\u4f20\u9012"},{"location":"9-11-Return-by-reference-and-return-by-address/","tags":["reference","address","return"],"text":"Key Takeaway \u5f15\u7528\u8fd4\u56de\u7684\u5bf9\u8c61\u5fc5\u987b\u5b58\u5728\u4e8e\u8fd4\u56de\u5f15\u7528\u7684\u51fd\u6570\u7684\u4f5c\u7528\u57df\u4e4b\u5916\uff0c\u5426\u5219\u5c06\u5bfc\u81f4\u60ac\u7a7a\u5f15\u7528\u3002\u6c38\u8fdc\u4e0d\u8981\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u5c40\u90e8\u53d8\u91cf \u4e0d\u8981\u6309\u5f15\u7528\u8fd4\u56de\u975econst\u7684\u5c40\u90e8\u9759\u6001\u53d8\u91cf \u5982\u679c\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5f15\u7528\uff0c\u5e76\u4e14\u8be5\u5f15\u7528\u7528\u4e8e\u521d\u59cb\u5316\u6216\u8d4b\u503c\u7ed9\u4e00\u4e2a\u975e\u5f15\u7528\u53d8\u91cf\uff0c\u5219\u8fd4\u56de\u503c\u5c06\u88ab\u590d\u5236(\u5c31\u50cf\u5b83\u662f\u901a\u8fc7value\u8fd4\u56de\u7684\u4e00\u6837)\u3002 \u5982\u679c\u53c2\u6570\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u5219\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u8be5\u53c2\u6570\u662f\u5b89\u5168\u7684 \u5728\u4e0a\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u5f53\u5b9e\u53c2 \u6309\u503c\u4f20\u9012 \u65f6\uff0c\u5b9e\u53c2\u7684\u503c\u4f1a\u88ab \u62f7\u8d1d \u4e00\u4efd\u5230 \u5f62\u53c2 \u3002 \u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\u6765\u8bf4\uff08\u62f7\u8d1d\u5f00\u9500\u5c0f\uff09\uff0c\u8fd9\u6ca1\u6709\u4ec0\u4e48\u95ee\u9898\u3002\u4f46\u662f\u5bf9\u4e8e \u7c7b\u7c7b\u578b \u6765\u8bf4\uff0c\u62f7\u8d1d\u5f00\u9500\u901a\u5e38\u4f1a\u5f88\u5927\uff08\u4f8b\u5982 std::string )\u3002\u901a\u8fc7 \u6309\u5f15\u7528\u4f20\u9012 \uff08\u901a\u5e38\u4e3a const \uff09\u6216\u6309 \u6309\u5730\u5740\u4f20\u9012 \u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u5f00\u9500\u3002 \u5728\u6309\u503c\u8fd4\u56de\u65f6\uff0c\u6211\u4eec\u4f1a\u9047\u5230\u7c7b\u4f3c\u7684\u60c5\u51b5\uff1a\u8fd4\u56de\u503c\u7684\u526f\u672c\u88ab\u4f20\u9012\u56de\u8c03\u7528\u8005\u3002\u5982\u679c\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u662f\u7c7b\u7c7b\u578b\uff0c\u5219\u4ee3\u4ef7\u53ef\u80fd\u5f88\u9ad8\u3002 1 std :: string returnByValue (); // \u8fd4\u56de std::string \u7684\u62f7\u8d1d(expensive) \u6309\u5f15\u7528\u8fd4\u56de \u5728\u5c06\u7c7b\u7c7b\u578b\u4f20\u9012\u56de\u8c03\u7528\u8005\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd(\u4e5f\u53ef\u80fd\u4e0d)\u5e0c\u671b \u6309\u5f15\u7528\u8fd4\u56de \u3002\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u8fd4\u56de\u4e00\u4e2a\u7ed1\u5b9a\u5230\u88ab\u8fd4\u56de\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u8fd9\u6837\u5c31\u907f\u514d\u4e86\u5bf9\u8fd4\u56de\u503c\u8fdb\u884c\u590d\u5236\u3002\u8981\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\uff0c\u53ea\u9700\u5c06\u51fd\u6570\u7684\u8fd4\u56de\u503c\u5b9a\u4e49\u4e3a\u5f15\u7528\u7c7b\u578b: 1 2 std :: string & returnByReference (); // returns a reference to an existing std::string (cheap) const std :: string & returnByReferenceToConst (); // returns a const reference to an existing std::string (cheap) \u4e0b\u9762\u4f8b\u7a0b\u5c55\u793a\u4e86\u5176\u539f\u7406\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include const std :: string & getProgramName () // \u8fd4\u56deconst\u5f15\u7528 { static const std :: string s_programName { \"Calculator\" }; // \u9759\u6001\u6301\u7eed\u65f6\u95f4\uff0c\u6301\u7eed\u5230\u7a0b\u5e8f\u7ed3\u675f return s_programName ; } int main () { std :: cout << \"This program is named \" << getProgramName (); return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 This program is named Calculator \u56e0\u4e3a getProgramName() \u8fd4\u56de const \u5f15\u7528\uff0c\u6240\u4ee5 return s_programName \u6267\u884c\u65f6\uff0c getProgramName() \u4f1a\u8fd4\u56de const \u5f15\u7528\u5230 s_programName (\u907f\u514d\u4e86\u62f7\u8d1d\u5bf9\u8c61)\u3002\u8c03\u7528\u8005\u53ef\u4ee5\u4f7f\u7528\u8be5\u5f15\u7528\u6765\u8bbf\u95ee s_programName \u7684\u503c\uff0c\u4ece\u800c\u5c06\u5176\u6253\u5370\u51fa\u6765\u3002 \u6309\u5f15\u7528\u8fd4\u56de\u7684\u5bf9\u8c61\u5176\u6301\u7eed\u65f6\u95f4\u5fc5\u987b\u8d85\u8fc7\u51fd\u6570\u8c03\u7528\u672c\u8eab \u4f7f\u7528\u6309\u5f15\u7528\u8fd4\u56de\u6700\u9700\u8981\u6ce8\u610f\u7684\u4e8b\u662f\uff1a\u7a0b\u5e8f\u5458\u5fc5\u987b\u786e\u4fdd\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u6bd4\u8fd4\u56de\u5f15\u7528\u7684\u51fd\u6570\u5bff\u547d\u957f\u3002\u5426\u5219\uff0c\u8fd4\u56de\u7684\u5f15\u7528\u5c06\u79f0\u4e3a \u60ac\u5782 \u5f15\u7528(\u5f15\u7528\u4e00\u4e2a\u5df2\u88ab\u9500\u6bc1\u7684\u5bf9\u8c61)\uff0c\u4f7f\u7528\u8be5\u5f15\u7528\u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u5728\u4e0a\u9762\u7684 \u7a0b\u5e8f\u4e2d\uff0c\u56e0\u4e3a s_programName \u5177\u6709 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u6240\u4ee5\u5b83\u7684\u77e5\u9053\u7a0b\u5e8f\u7ed3\u675f\u624d\u4f1a\u88ab\u9500\u6bc1\u3002\u5f53 main \u51fd\u6570\u8bbf\u95ee\u8be5\u8fd4\u56de\u7684\u5f15\u7528\u65f6\uff0c\u5b83\u5b9e\u9645\u8bbf\u95ee\u7684\u662f s_programName \uff0c\u8fd9\u65f6\u662f\u6ca1\u6709\u95ee\u9898\u7684\uff0c\u5728\u8fd9\u4e2a\u65f6\u95f4\u70b9\u4e0a\u5b83\u8fd8\u6ca1\u6709\u88ab\u9500\u6bc1\u3002 \u63a5\u4e0b\u6765\uff0c\u4fee\u6539\u4e0a\u9762\u7684\u7a0b\u5e8f\uff0c\u770b\u770b\u51fd\u6570\u8fd4\u56de\u60ac\u5782\u5f15\u7528\u7684\u60c5\u51b5\u4e0b\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 #include #include const std :: string & getProgramName () { const std :: string programName { \"Calculator\" }; // now a local variable, destroyed when function ends return programName ; } int main () { std :: cout << \"This program is named \" << getProgramName (); return 0 ; } ``` \u8be5\u7a0b\u5e8f\u7684\u8fd0\u884c\u7ed3\u679c\u662f\u672a\u5b9a\u4e49\u7684 \u3002 \u5f53 ` getProgramName () ` \u8fd4\u56de\u65f6 \uff0c \u4e00\u4e2a\u5f15\u7528\u7ed1\u5b9a\u5230\u4e86\u5c40\u90e8\u53d8\u91cf ` programName ` \u3002 \u4e5f\u6b63\u662f\u56e0\u4e3a\u8be5\u53d8\u91cf\u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf \uff0c \u5b83\u5177\u6709 [ \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 ](. / automatic - storage - duration . md ) \uff0c \u5b83\u4f1a\u5728\u51fd\u6570\u672b\u5c3e\u88ab\u9500\u6bc1 \uff0c \u6240\u4ee5\u8fd4\u56de\u7684\u5f15\u7528\u5c31\u662f\u4f1a\u662f\u4e00\u4e2a\u60ac\u5782\u5f15\u7528 \uff0c \u5728 ` main ` \u51fd\u6570\u4e2d\u4f7f\u7528\u8be5\u60ac\u5782\u5f15\u7528\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u5982\u679c\u8bd5\u56fe\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u5c40\u90e8\u53d8\u91cf \uff0c \u73b0\u4ee3\u7f16\u8bd1\u5668\u5c06\u4ea7\u751f\u8b66\u544a\u6216\u9519\u8bef ( \u56e0\u6b64 \uff0c \u4e0a\u9762\u7684\u7a0b\u5e8f\u751a\u81f3\u53ef\u80fd\u65e0\u6cd5\u7f16\u8bd1 ) \uff0c \u4f46\u7f16\u8bd1\u5668\u6709\u65f6\u5728\u68c0\u6d4b\u66f4\u590d\u6742\u7684\u60c5\u51b5\u65f6\u4f1a\u9047\u5230\u9ebb\u70e6 \u3002 !!! warning \"\u6ce8\u610f\" == \u5f15\u7528\u8fd4\u56de\u7684\u5bf9\u8c61\u5fc5\u987b\u5b58\u5728\u4e8e\u8fd4\u56de\u5f15\u7528\u7684\u51fd\u6570\u7684\u4f5c\u7528\u57df\u4e4b\u5916 \uff0c \u5426\u5219\u5c06\u5bfc\u81f4\u60ac\u7a7a\u5f15\u7528 \u3002 \u6c38\u8fdc\u4e0d\u8981\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u5c40\u90e8\u53d8\u91cf \u3002 == ## \u4e0d\u8981\u6309\u5f15\u7528\u8fd4\u56de\u975econst\u7684\u5c40\u90e8\u9759\u6001\u53d8\u91cf \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d \uff0c \u6211\u4eec\u6309\u5f15\u7528\u8fd4\u56de\u7684\u662f\u4e00\u4e2aconst\u7684\u5c40\u90e8\u9759\u6001\u53d8\u91cf \uff0c \u5e76\u4ee5\u6b64\u6765\u6f14\u793a\u6309\u5f15\u7528\u4f20\u9012\u7684\u65b9\u5f0f \u3002 \u4f46\u662f \uff0c \u6309\u5f15\u7528\u8fd4\u56de\u975econst\u7684\u9759\u6001\u53d8\u91cf\u5e76\u4e0d\u7b26\u5408\u4e60\u60ef \uff0c \u901a\u5e38\u5e94\u8be5\u907f\u514d\u8fd9\u4e48\u505a \u3002 \u8fd9\u4e48\u505a\u4f1a\u6709\u4ec0\u4e48\u95ee\u9898 \uff1f \u8bf7\u770b\u4e0b\u9762\u7684\u7a0b\u5e8f \uff1a ``` cpp #include #include const int & getNextId () { static int s_x { 0 }; // \u6ce8\u610f: \u53d8\u91cf\u662f\u975econst\u7684 ++ s_x ; // \u751f\u6210\u4e0b\u4e00\u4e2aid return s_x ; // \u6309\u5f15\u7528\u8fd4\u56de } int main () { const int & id1 { getNextId () }; // id1 \u662f\u4e00\u4e2a\u5f15\u7528 const int & id2 { getNextId () }; // id2 \u662f\u4e00\u4e2a\u5f15\u7528 std :: cout << id1 << id2 << '\\n' ; return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 22 \u4e4b\u6240\u4ee5\u8fd9\u6837\u662f\u56e0\u4e3a id1 \u548c id2 \u5f15\u7528\u7684\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61(\u5373\u9759\u6001\u53d8\u91cf s_x )\uff0c\u6240\u4ee5\u4efb\u4f55\u5bf9\u8be5\u53d8\u91cf\u7684\u4fee\u6539(\u4f8b\u5982 getNextId() )\uff0c\u90fd\u4f1a\u5f71\u54cd\u5230\u6240\u6709\u5f15\u7528\u3002\u901a\u8fc7const\u5f15\u7528\u8fd4\u56de\u9759\u6001\u5c40\u90e8\u503c\u7684\u7a0b\u5e8f\u7ecf\u5e38\u51fa\u73b0\u7684\u53e6\u4e00\u4e2a\u95ee\u9898\u662f\uff0c\u6ca1\u6709\u6807\u51c6\u5316\u7684\u65b9\u6cd5\u5c06 s_x \u91cd\u7f6e\u56de\u9ed8\u8ba4\u72b6\u6001\u3002\u8fd9\u6837\u7684\u7a0b\u5e8f\u5fc5\u987b\u4f7f\u7528\u975e\u60ef\u7528\u7684\u89e3\u51b3\u65b9\u6848(\u4f8b\u5982\u91cd\u7f6e\u53c2\u6570)\uff0c\u6216\u8005\u53ea\u80fd\u901a\u8fc7\u9000\u51fa\u548c\u91cd\u65b0\u542f\u52a8\u7a0b\u5e8f\u6765\u91cd\u7f6e\u3002 \u867d\u7136\u4e0a\u9762\u7684\u4f8b\u5b50\u6709\u70b9\u50bb\uff0c\u4f46\u7a0b\u5e8f\u5458\u6709\u65f6\u4f1a\u4e3a\u4e86\u4f18\u5316\u76ee\u7684\u800c\u5c1d\u8bd5\u4e0a\u9762\u7684\u505a\u6cd5\uff0c\u7136\u540e\u7a0b\u5e8f\u5c31\u4e0d\u80fd\u6309\u9884\u671f\u5de5\u4f5c\u4e86\u3002 \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u8fd4\u56de\u5bf9\u975econst\u5c40\u90e8\u9759\u6001\u53d8\u91cf\u7684\u5f15\u7528\u3002 \u5982\u679c\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u7684\u5c40\u90e8\u53d8\u91cf\u7684\u521b\u5efa\u6210\u672c\u5f88\u9ad8(\u56e0\u6b64\u4e0d\u5fc5\u6bcf\u6b21\u51fd\u6570\u8c03\u7528\u90fd\u91cd\u65b0\u521b\u5efa\u8be5\u53d8\u91cf)\uff0c\u5219\u6709\u65f6\u4f1a\u8fd4\u56de\u5bf9 const \u5c40\u90e8\u9759\u6001\u53d8\u91cf\u7684const\u5f15\u7528\u3002\u4f46\u8fd9\u662f\u7f55\u89c1\u7684\u3002 \u8fd4\u56de\u4e00\u4e2a\u6307\u5411const\u5168\u5c40\u53d8\u91cf\u7684const\u5f15\u7528\u6709\u65f6\u4e5f\u662f\u4e00\u79cd\u7528\u4e8e\u5c01\u88c5\u5bf9\u5168\u5c40\u53d8\u91cf\u8bbf\u95ee\u7684\u65b9\u5f0f \u3002\u6211\u4eec\u5728\u8bfe\u7a0b 6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c \u4e2d\u8ba8\u8bba\u8fd9\u4e2a\u95ee\u9898\u3002\u5982\u679c\u6709\u610f\u4e14\u8c28\u614e\u5730\u4f7f\u7528\uff0c\u8fd9\u4e5f\u662f\u53ef\u4ee5\u7684\u3002 \u4f7f\u7528\u8fd4\u56de\u7684\u5f15\u7528\u6765\u8bbf\u95ee/\u521d\u59cb\u5316\u666e\u901a\u53d8\u91cf\u65f6\u4f1a\u521b\u5efa\u62f7\u8d1d \u5982\u679c\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5f15\u7528\uff0c\u5e76\u4e14\u8be5\u5f15\u7528\u7528\u4e8e\u521d\u59cb\u5316\u6216\u8d4b\u503c\u7ed9\u4e00\u4e2a\u975e\u5f15\u7528\u53d8\u91cf\uff0c\u5219\u8fd4\u56de\u503c\u5c06\u88ab\u590d\u5236(\u5c31\u50cf\u5b83\u662f\u901a\u8fc7value\u8fd4\u56de\u7684\u4e00\u6837)\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include const int & getNextId () { static int s_x { 0 }; ++ s_x ; return s_x ; } int main () { const int id1 { getNextId () }; // id1 is a normal variable now and receives a copy of the value returned by reference from getNextId() const int id2 { getNextId () }; // id2 is a normal variable now and receives a copy of the value returned by reference from getNextId() std :: cout << id1 << id2 << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c getNextId() \u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5f15\u7528\uff0c\u4f46\u662f id1 \u548c id2 \u90fd\u662f\u975e\u5f15\u7528\u7684\u666e\u901a\u53d8\u91cf\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8fd4\u56de\u7684\u5f15\u7528\u7ed1\u5b9a\u7684\u503c\u4f1a\u88ab\u62f7\u8d1d\u5230\u8fd9\u4e2a\u666e\u901a\u53d8\u91cf\uff0c\u56e0\u6b64\u7a0b\u5e8f\u6253\u5370\uff1a 1 12 \u5f53\u7136\uff0c\u8fd9\u4e5f\u8fdd\u80cc\u4e86\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u503c\u7684\u76ee\u7684\u3002 \u8fd8\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u7a0b\u5e8f\u8fd4\u56de\u4e00\u4e2a\u60ac\u5782\u5f15\u7528\uff0c\u5219\u8be5\u5f15\u7528\u5728\u590d\u5236\u4e4b\u524d\u4e00\u76f4\u60ac\u7a7a\uff0c\u8fd9\u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include const std :: string & getProgramName () // will return a const reference { const std :: string programName { \"Calculator\" }; return programName ; } int main () { std :: string name { getProgramName () }; // makes a copy of a dangling reference std :: cout << \"This program is named \" << name << '\\n' ; // undefined behavior return 0 ; } \u6309\u5f15\u7528\u8fd4\u56de\u6309\u5f15\u7528\u4f20\u9012\u7684\u53c2\u6570\u6ca1\u6709\u95ee\u9898 \u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u5bf9\u8c61\u662f\u6709\u610f\u4e49\u7684\uff0c\u6211\u4eec\u5c06\u5728\u672a\u6765\u7684\u8bfe\u7a0b\u4e2d\u9047\u5230\u8bb8\u591a\u8fd9\u6837\u7684\u60c5\u51b5\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u73b0\u5728\u53ef\u4ee5\u4e3e\u4e00\u4e2a\u6709\u7528\u7684\u4f8b\u5b50\u3002 \u5982\u679c\u53c2\u6570\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u5219\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u8be5\u53c2\u6570\u662f\u5b89\u5168\u7684 \u3002\u8fd9\u662f\u6709\u610f\u4e49\u7684\uff1a\u4e3a\u4e86\u5c06\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u53c2\u6570\u5fc5\u987b\u5b58\u5728\u4e8e\u8c03\u7528\u8005\u7684\u4f5c\u7528\u57df\u4e2d\u3002\u5f53\u88ab\u8c03\u7528\u7684\u51fd\u6570\u8fd4\u56de\u65f6\uff0c\u8be5\u5bf9\u8c61\u5fc5\u987b\u4ecd\u7136\u5b58\u5728\u4e8e\u8c03\u7528\u8005\u7684\u4f5c\u7528\u57df\u4e2d\u3002 \u4e0b\u9762\u662f\u8fd9\u6837\u4e00\u4e2a\u51fd\u6570\u7684\u7b80\u5355\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include // Takes two std::string objects, returns the one that comes first alphabetically const std :: string & firstAlphabetical ( const std :: string & a , const std :: string & b ) { return ( a < b ) ? a : b ; // We can use operator< on std::string to determine which comes first alphabetically } int main () { std :: string hello { \"Hello\" }; std :: string world { \"World\" }; std :: cout << firstAlphabetical ( hello , world ) << '\\n' ; return 0 ; } \u8f93\u51fa\uff1a 1 Hello \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8c03\u7528\u8005\u6309\u5f15\u7528\u4f20\u9012\u4e86\u4e24\u4e2a std::string \u5bf9\u8c61\uff0c\u7136\u540e\u7ecf\u8fc7\u6bd4\u8f83\uff0c\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e2d\u6309\u7167\u5b57\u6bcd\u8868\u6bd4\u8f83\u6392\u5728\u524d\u9762\u7684\u5bf9\u8c61\u88ab\u6309\u5f15\u7528(const)\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u5982\u679c\u6211\u4eec\u662f\u6309\u7167\u503c\u4f20\u9012\uff0c\u5219\u4f1a\u5bfc\u81f4\u521b\u5efa std::string \u7684\u4e09\u4e2a\u62f7\u8d1d\uff08\u6bcf\u4e2a\u5f62\u53c2\u62f7\u8d1d\u4e00\u6b21\u3001\u8fd4\u56de\u503c\u62f7\u8d1d\u4e00\u6b21\uff09\u3002\u800c\u4f7f\u7528\u5f15\u7528\u4f20\u9012\u5219\u53ef\u4ee5\u907f\u514d\u8fd9\u4e9b\u62f7\u8d1d\u3002 \u8c03\u7528\u8005\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u4fee\u6539\u503c caller can modify values through the reference \u5f53\u5b9e\u53c2\u6309\u975econst\u5f15\u7528\u4f20\u9012\u65f6\uff0c\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u4fee\u6539\u5b9e\u53c2\u7684\u503c\u3002 \u7c7b\u4f3c\u7684\uff0c\u5f53\u6309\u975econst\u5f15\u7528\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u65f6\uff0c\u8c03\u7528\u8005\u53ef\u4ee5\u4f7f\u7528\u8be5\u5f15\u7528\u4fee\u6539\u8fd4\u56de\u503c\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include // \u63a5\u53d7\u4e24\u4e2a\u6574\u578b\u7684\u975econst\u5f15\u7528\uff0c\u8fd4\u56de\u5176\u4e2d\u8f83\u5927\u7684\u4e00\u4e2a\uff08\u6309\u5f15\u7528\u8fd4\u56de\uff09 int & max ( int & x , int & y ) { return ( x > y ) ? x : y ; } int main () { int a { 5 }; int b { 6 }; max ( a , b ) = 7 ; // \u5c06a\u548cb\u4e2d\u8f83\u5927\u7684\u4e00\u4e2a\u7684\u503c\u8bbe\u7f6e\u4e3a 7 std :: cout << a << b << '\\n' ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c max(a, b) \u5728\u8c03\u7528 max() \u51fd\u6570\u65f6\u4f20\u5165 a \u548c b \u4f5c\u4e3a\u5b9e\u53c2\u3002\u5f15\u7528\u5f62\u53c2 x \u7ed1\u5b9a\u5230\u5b9e\u53c2 a \uff0c\u800c\u5f15\u7528\u5f62\u53c2 y \u7ed1\u5b9a\u5230\u5b9e\u53c2 b \u3002\u968f\u540e\uff0c\u51fd\u6570\u4f1a\u5224\u65ad x ( 5 ) \u548c y ( 6 ) \u54ea\u4e2a\u6bd4\u8f83\u5927\u3002\u672c\u4f8b\u4e2d\u663e\u7136 y \u66f4\u5927\uff0c\u56e0\u6b64 y (\u4ecd\u7136\u7ed1\u5b9a\u5230 b ) \u88ab\u6309\u5f15\u7528\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u8c03\u7528\u8005\u968f\u540e\u901a\u8fc7\u8fd4\u56de\u7684\u5f15\u7528\u5c06 b \u8d4b\u503c\u4e3a 7\u3002 \u56e0\u6b64\uff0c\u8868\u8fbe\u5f0f max(a, b) = 7 \u6700\u7ec8\u89e3\u6790\u4e3a b = 7 \u3002 \u7a0b\u5e8f\u6253\u5370\uff1a 1 57 \u6309\u5730\u5740\u8fd4\u56de \u6309\u5730\u5740\u8fd4\u56de \u4e0e \u6309\u5f15\u7528\u8fd4\u56de \u7684\u5de5\u4f5c\u539f\u7406\u51e0\u4e4e\u76f8\u540c\uff0c\u53ea\u4e0d\u8fc7\u8fd4\u56de\u7684\u662f\u6307\u5411\u5bf9\u8c61\u7684\u6307\u9488\u800c\u4e0d\u662f\u5bf9\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u6309\u5730\u5740\u8fd4\u56de\u6709\u4e0e\u6309\u5f15\u7528\u8fd4\u56de\u76f8\u540c\u7684\u6ce8\u610f\u4e8b\u9879\u2014\u2014\u6309\u5730\u5740\u8fd4\u56de\u7684\u5bf9\u8c61\u5fc5\u987b\u6bd4\u8fd4\u56de\u5730\u5740\u7684\u51fd\u6570\u7684\u4f5c\u7528\u57df\u66f4\u957f\u4e45\uff0c\u5426\u5219\u8c03\u7528\u8005\u5c06\u6536\u5230\u4e00\u4e2a\u60ac\u5782\u6307\u9488\u3002 \u6309\u5730\u5740\u8fd4\u56de\u6bd4\u6309\u5f15\u7528\u8fd4\u56de\u7684\u4e3b\u8981\u4f18\u70b9\u662f\uff0c\u5982\u679c\u6ca1\u6709\u8981\u8fd4\u56de\u7684\u6709\u6548\u5bf9\u8c61\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u8fd4\u56de nullptr \u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u60f3\u8981\u641c\u7d22\u7684\u5b66\u751f\u5217\u8868\u3002\u5982\u679c\u5728\u5217\u8868\u4e2d\u627e\u5230\u4e86\u8981\u67e5\u627e\u7684\u5b66\u751f\uff0c\u5219\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u8868\u793a\u5339\u914d\u5b66\u751f\u7684\u5bf9\u8c61\u7684\u6307\u9488\u3002\u5982\u679c\u6211\u4eec\u6ca1\u6709\u627e\u5230\u4efb\u4f55\u5339\u914d\u7684\u5b66\u751f\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd4\u56de nullptr \u6765\u8868\u793a\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u5b66\u751f\u5bf9\u8c61\u3002 \u5176\u4e3b\u8981\u7f3a\u70b9\u5219\u662f\u8c03\u7528\u8005\u5fc5\u987b\u8bb0\u5f97\u5728\u89e3\u5f15\u7528\u8fd4\u56de\u503c\u4e4b\u524d\u8fdb\u884c\u6307\u9488\u5224\u7a7a\u64cd\u4f5c\uff0c\u5219\u53ef\u80fd\u4f1a\u53d1\u751f\u7a7a\u6307\u9488\u89e3\u5f15\u7528\u5e76\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u7531\u4e8e\u8fd9\u79cd\u5371\u9669\uff0c\u9664\u975e\u9700\u8981\u8fd4\u56de\u201c\u65e0\u5bf9\u8c61\u201d\u7684\u80fd\u529b\uff0c\u5426\u5219\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u5e94\u4f18\u5148\u4e8e\u901a\u8fc7\u5730\u5740\u8fd4\u56de\u3002 \u6700\u4f73\u5b9e\u8df5 \u9996\u9009\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u800c\u4e0d\u662f\u901a\u8fc7\u5730\u5740\u8fd4\u56de\uff0c\u9664\u975e\u8fd4\u56de\u201c\u65e0\u5bf9\u8c61\u201d(\u4f7f\u7528 nullptr )\u7684\u80fd\u529b\u5f88\u91cd\u8981\u3002","title":"9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de"},{"location":"9-11-Return-by-reference-and-return-by-address/#_1","text":"\u5728\u5c06\u7c7b\u7c7b\u578b\u4f20\u9012\u56de\u8c03\u7528\u8005\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd(\u4e5f\u53ef\u80fd\u4e0d)\u5e0c\u671b \u6309\u5f15\u7528\u8fd4\u56de \u3002\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u8fd4\u56de\u4e00\u4e2a\u7ed1\u5b9a\u5230\u88ab\u8fd4\u56de\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u8fd9\u6837\u5c31\u907f\u514d\u4e86\u5bf9\u8fd4\u56de\u503c\u8fdb\u884c\u590d\u5236\u3002\u8981\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\uff0c\u53ea\u9700\u5c06\u51fd\u6570\u7684\u8fd4\u56de\u503c\u5b9a\u4e49\u4e3a\u5f15\u7528\u7c7b\u578b: 1 2 std :: string & returnByReference (); // returns a reference to an existing std::string (cheap) const std :: string & returnByReferenceToConst (); // returns a const reference to an existing std::string (cheap) \u4e0b\u9762\u4f8b\u7a0b\u5c55\u793a\u4e86\u5176\u539f\u7406\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include const std :: string & getProgramName () // \u8fd4\u56deconst\u5f15\u7528 { static const std :: string s_programName { \"Calculator\" }; // \u9759\u6001\u6301\u7eed\u65f6\u95f4\uff0c\u6301\u7eed\u5230\u7a0b\u5e8f\u7ed3\u675f return s_programName ; } int main () { std :: cout << \"This program is named \" << getProgramName (); return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 This program is named Calculator \u56e0\u4e3a getProgramName() \u8fd4\u56de const \u5f15\u7528\uff0c\u6240\u4ee5 return s_programName \u6267\u884c\u65f6\uff0c getProgramName() \u4f1a\u8fd4\u56de const \u5f15\u7528\u5230 s_programName (\u907f\u514d\u4e86\u62f7\u8d1d\u5bf9\u8c61)\u3002\u8c03\u7528\u8005\u53ef\u4ee5\u4f7f\u7528\u8be5\u5f15\u7528\u6765\u8bbf\u95ee s_programName \u7684\u503c\uff0c\u4ece\u800c\u5c06\u5176\u6253\u5370\u51fa\u6765\u3002","title":"\u6309\u5f15\u7528\u8fd4\u56de"},{"location":"9-11-Return-by-reference-and-return-by-address/#_2","text":"\u4f7f\u7528\u6309\u5f15\u7528\u8fd4\u56de\u6700\u9700\u8981\u6ce8\u610f\u7684\u4e8b\u662f\uff1a\u7a0b\u5e8f\u5458\u5fc5\u987b\u786e\u4fdd\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u6bd4\u8fd4\u56de\u5f15\u7528\u7684\u51fd\u6570\u5bff\u547d\u957f\u3002\u5426\u5219\uff0c\u8fd4\u56de\u7684\u5f15\u7528\u5c06\u79f0\u4e3a \u60ac\u5782 \u5f15\u7528(\u5f15\u7528\u4e00\u4e2a\u5df2\u88ab\u9500\u6bc1\u7684\u5bf9\u8c61)\uff0c\u4f7f\u7528\u8be5\u5f15\u7528\u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u5728\u4e0a\u9762\u7684 \u7a0b\u5e8f\u4e2d\uff0c\u56e0\u4e3a s_programName \u5177\u6709 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u6240\u4ee5\u5b83\u7684\u77e5\u9053\u7a0b\u5e8f\u7ed3\u675f\u624d\u4f1a\u88ab\u9500\u6bc1\u3002\u5f53 main \u51fd\u6570\u8bbf\u95ee\u8be5\u8fd4\u56de\u7684\u5f15\u7528\u65f6\uff0c\u5b83\u5b9e\u9645\u8bbf\u95ee\u7684\u662f s_programName \uff0c\u8fd9\u65f6\u662f\u6ca1\u6709\u95ee\u9898\u7684\uff0c\u5728\u8fd9\u4e2a\u65f6\u95f4\u70b9\u4e0a\u5b83\u8fd8\u6ca1\u6709\u88ab\u9500\u6bc1\u3002 \u63a5\u4e0b\u6765\uff0c\u4fee\u6539\u4e0a\u9762\u7684\u7a0b\u5e8f\uff0c\u770b\u770b\u51fd\u6570\u8fd4\u56de\u60ac\u5782\u5f15\u7528\u7684\u60c5\u51b5\u4e0b\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 #include #include const std :: string & getProgramName () { const std :: string programName { \"Calculator\" }; // now a local variable, destroyed when function ends return programName ; } int main () { std :: cout << \"This program is named \" << getProgramName (); return 0 ; } ``` \u8be5\u7a0b\u5e8f\u7684\u8fd0\u884c\u7ed3\u679c\u662f\u672a\u5b9a\u4e49\u7684 \u3002 \u5f53 ` getProgramName () ` \u8fd4\u56de\u65f6 \uff0c \u4e00\u4e2a\u5f15\u7528\u7ed1\u5b9a\u5230\u4e86\u5c40\u90e8\u53d8\u91cf ` programName ` \u3002 \u4e5f\u6b63\u662f\u56e0\u4e3a\u8be5\u53d8\u91cf\u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf \uff0c \u5b83\u5177\u6709 [ \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 ](. / automatic - storage - duration . md ) \uff0c \u5b83\u4f1a\u5728\u51fd\u6570\u672b\u5c3e\u88ab\u9500\u6bc1 \uff0c \u6240\u4ee5\u8fd4\u56de\u7684\u5f15\u7528\u5c31\u662f\u4f1a\u662f\u4e00\u4e2a\u60ac\u5782\u5f15\u7528 \uff0c \u5728 ` main ` \u51fd\u6570\u4e2d\u4f7f\u7528\u8be5\u60ac\u5782\u5f15\u7528\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u5982\u679c\u8bd5\u56fe\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u5c40\u90e8\u53d8\u91cf \uff0c \u73b0\u4ee3\u7f16\u8bd1\u5668\u5c06\u4ea7\u751f\u8b66\u544a\u6216\u9519\u8bef ( \u56e0\u6b64 \uff0c \u4e0a\u9762\u7684\u7a0b\u5e8f\u751a\u81f3\u53ef\u80fd\u65e0\u6cd5\u7f16\u8bd1 ) \uff0c \u4f46\u7f16\u8bd1\u5668\u6709\u65f6\u5728\u68c0\u6d4b\u66f4\u590d\u6742\u7684\u60c5\u51b5\u65f6\u4f1a\u9047\u5230\u9ebb\u70e6 \u3002 !!! warning \"\u6ce8\u610f\" == \u5f15\u7528\u8fd4\u56de\u7684\u5bf9\u8c61\u5fc5\u987b\u5b58\u5728\u4e8e\u8fd4\u56de\u5f15\u7528\u7684\u51fd\u6570\u7684\u4f5c\u7528\u57df\u4e4b\u5916 \uff0c \u5426\u5219\u5c06\u5bfc\u81f4\u60ac\u7a7a\u5f15\u7528 \u3002 \u6c38\u8fdc\u4e0d\u8981\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u5c40\u90e8\u53d8\u91cf \u3002 == ## \u4e0d\u8981\u6309\u5f15\u7528\u8fd4\u56de\u975econst\u7684\u5c40\u90e8\u9759\u6001\u53d8\u91cf \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d \uff0c \u6211\u4eec\u6309\u5f15\u7528\u8fd4\u56de\u7684\u662f\u4e00\u4e2aconst\u7684\u5c40\u90e8\u9759\u6001\u53d8\u91cf \uff0c \u5e76\u4ee5\u6b64\u6765\u6f14\u793a\u6309\u5f15\u7528\u4f20\u9012\u7684\u65b9\u5f0f \u3002 \u4f46\u662f \uff0c \u6309\u5f15\u7528\u8fd4\u56de\u975econst\u7684\u9759\u6001\u53d8\u91cf\u5e76\u4e0d\u7b26\u5408\u4e60\u60ef \uff0c \u901a\u5e38\u5e94\u8be5\u907f\u514d\u8fd9\u4e48\u505a \u3002 \u8fd9\u4e48\u505a\u4f1a\u6709\u4ec0\u4e48\u95ee\u9898 \uff1f \u8bf7\u770b\u4e0b\u9762\u7684\u7a0b\u5e8f \uff1a ``` cpp #include #include const int & getNextId () { static int s_x { 0 }; // \u6ce8\u610f: \u53d8\u91cf\u662f\u975econst\u7684 ++ s_x ; // \u751f\u6210\u4e0b\u4e00\u4e2aid return s_x ; // \u6309\u5f15\u7528\u8fd4\u56de } int main () { const int & id1 { getNextId () }; // id1 \u662f\u4e00\u4e2a\u5f15\u7528 const int & id2 { getNextId () }; // id2 \u662f\u4e00\u4e2a\u5f15\u7528 std :: cout << id1 << id2 << '\\n' ; return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 22 \u4e4b\u6240\u4ee5\u8fd9\u6837\u662f\u56e0\u4e3a id1 \u548c id2 \u5f15\u7528\u7684\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61(\u5373\u9759\u6001\u53d8\u91cf s_x )\uff0c\u6240\u4ee5\u4efb\u4f55\u5bf9\u8be5\u53d8\u91cf\u7684\u4fee\u6539(\u4f8b\u5982 getNextId() )\uff0c\u90fd\u4f1a\u5f71\u54cd\u5230\u6240\u6709\u5f15\u7528\u3002\u901a\u8fc7const\u5f15\u7528\u8fd4\u56de\u9759\u6001\u5c40\u90e8\u503c\u7684\u7a0b\u5e8f\u7ecf\u5e38\u51fa\u73b0\u7684\u53e6\u4e00\u4e2a\u95ee\u9898\u662f\uff0c\u6ca1\u6709\u6807\u51c6\u5316\u7684\u65b9\u6cd5\u5c06 s_x \u91cd\u7f6e\u56de\u9ed8\u8ba4\u72b6\u6001\u3002\u8fd9\u6837\u7684\u7a0b\u5e8f\u5fc5\u987b\u4f7f\u7528\u975e\u60ef\u7528\u7684\u89e3\u51b3\u65b9\u6848(\u4f8b\u5982\u91cd\u7f6e\u53c2\u6570)\uff0c\u6216\u8005\u53ea\u80fd\u901a\u8fc7\u9000\u51fa\u548c\u91cd\u65b0\u542f\u52a8\u7a0b\u5e8f\u6765\u91cd\u7f6e\u3002 \u867d\u7136\u4e0a\u9762\u7684\u4f8b\u5b50\u6709\u70b9\u50bb\uff0c\u4f46\u7a0b\u5e8f\u5458\u6709\u65f6\u4f1a\u4e3a\u4e86\u4f18\u5316\u76ee\u7684\u800c\u5c1d\u8bd5\u4e0a\u9762\u7684\u505a\u6cd5\uff0c\u7136\u540e\u7a0b\u5e8f\u5c31\u4e0d\u80fd\u6309\u9884\u671f\u5de5\u4f5c\u4e86\u3002 \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u8fd4\u56de\u5bf9\u975econst\u5c40\u90e8\u9759\u6001\u53d8\u91cf\u7684\u5f15\u7528\u3002 \u5982\u679c\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u7684\u5c40\u90e8\u53d8\u91cf\u7684\u521b\u5efa\u6210\u672c\u5f88\u9ad8(\u56e0\u6b64\u4e0d\u5fc5\u6bcf\u6b21\u51fd\u6570\u8c03\u7528\u90fd\u91cd\u65b0\u521b\u5efa\u8be5\u53d8\u91cf)\uff0c\u5219\u6709\u65f6\u4f1a\u8fd4\u56de\u5bf9 const \u5c40\u90e8\u9759\u6001\u53d8\u91cf\u7684const\u5f15\u7528\u3002\u4f46\u8fd9\u662f\u7f55\u89c1\u7684\u3002 \u8fd4\u56de\u4e00\u4e2a\u6307\u5411const\u5168\u5c40\u53d8\u91cf\u7684const\u5f15\u7528\u6709\u65f6\u4e5f\u662f\u4e00\u79cd\u7528\u4e8e\u5c01\u88c5\u5bf9\u5168\u5c40\u53d8\u91cf\u8bbf\u95ee\u7684\u65b9\u5f0f \u3002\u6211\u4eec\u5728\u8bfe\u7a0b 6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c \u4e2d\u8ba8\u8bba\u8fd9\u4e2a\u95ee\u9898\u3002\u5982\u679c\u6709\u610f\u4e14\u8c28\u614e\u5730\u4f7f\u7528\uff0c\u8fd9\u4e5f\u662f\u53ef\u4ee5\u7684\u3002","title":"\u6309\u5f15\u7528\u8fd4\u56de\u7684\u5bf9\u8c61\u5176\u6301\u7eed\u65f6\u95f4\u5fc5\u987b\u8d85\u8fc7\u51fd\u6570\u8c03\u7528\u672c\u8eab"},{"location":"9-11-Return-by-reference-and-return-by-address/#_3","text":"\u5982\u679c\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5f15\u7528\uff0c\u5e76\u4e14\u8be5\u5f15\u7528\u7528\u4e8e\u521d\u59cb\u5316\u6216\u8d4b\u503c\u7ed9\u4e00\u4e2a\u975e\u5f15\u7528\u53d8\u91cf\uff0c\u5219\u8fd4\u56de\u503c\u5c06\u88ab\u590d\u5236(\u5c31\u50cf\u5b83\u662f\u901a\u8fc7value\u8fd4\u56de\u7684\u4e00\u6837)\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include const int & getNextId () { static int s_x { 0 }; ++ s_x ; return s_x ; } int main () { const int id1 { getNextId () }; // id1 is a normal variable now and receives a copy of the value returned by reference from getNextId() const int id2 { getNextId () }; // id2 is a normal variable now and receives a copy of the value returned by reference from getNextId() std :: cout << id1 << id2 << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c getNextId() \u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5f15\u7528\uff0c\u4f46\u662f id1 \u548c id2 \u90fd\u662f\u975e\u5f15\u7528\u7684\u666e\u901a\u53d8\u91cf\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8fd4\u56de\u7684\u5f15\u7528\u7ed1\u5b9a\u7684\u503c\u4f1a\u88ab\u62f7\u8d1d\u5230\u8fd9\u4e2a\u666e\u901a\u53d8\u91cf\uff0c\u56e0\u6b64\u7a0b\u5e8f\u6253\u5370\uff1a 1 12 \u5f53\u7136\uff0c\u8fd9\u4e5f\u8fdd\u80cc\u4e86\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u503c\u7684\u76ee\u7684\u3002 \u8fd8\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u7a0b\u5e8f\u8fd4\u56de\u4e00\u4e2a\u60ac\u5782\u5f15\u7528\uff0c\u5219\u8be5\u5f15\u7528\u5728\u590d\u5236\u4e4b\u524d\u4e00\u76f4\u60ac\u7a7a\uff0c\u8fd9\u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include const std :: string & getProgramName () // will return a const reference { const std :: string programName { \"Calculator\" }; return programName ; } int main () { std :: string name { getProgramName () }; // makes a copy of a dangling reference std :: cout << \"This program is named \" << name << '\\n' ; // undefined behavior return 0 ; }","title":"\u4f7f\u7528\u8fd4\u56de\u7684\u5f15\u7528\u6765\u8bbf\u95ee/\u521d\u59cb\u5316\u666e\u901a\u53d8\u91cf\u65f6\u4f1a\u521b\u5efa\u62f7\u8d1d"},{"location":"9-11-Return-by-reference-and-return-by-address/#_4","text":"\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u5bf9\u8c61\u662f\u6709\u610f\u4e49\u7684\uff0c\u6211\u4eec\u5c06\u5728\u672a\u6765\u7684\u8bfe\u7a0b\u4e2d\u9047\u5230\u8bb8\u591a\u8fd9\u6837\u7684\u60c5\u51b5\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u73b0\u5728\u53ef\u4ee5\u4e3e\u4e00\u4e2a\u6709\u7528\u7684\u4f8b\u5b50\u3002 \u5982\u679c\u53c2\u6570\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u5219\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u8be5\u53c2\u6570\u662f\u5b89\u5168\u7684 \u3002\u8fd9\u662f\u6709\u610f\u4e49\u7684\uff1a\u4e3a\u4e86\u5c06\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u53c2\u6570\u5fc5\u987b\u5b58\u5728\u4e8e\u8c03\u7528\u8005\u7684\u4f5c\u7528\u57df\u4e2d\u3002\u5f53\u88ab\u8c03\u7528\u7684\u51fd\u6570\u8fd4\u56de\u65f6\uff0c\u8be5\u5bf9\u8c61\u5fc5\u987b\u4ecd\u7136\u5b58\u5728\u4e8e\u8c03\u7528\u8005\u7684\u4f5c\u7528\u57df\u4e2d\u3002 \u4e0b\u9762\u662f\u8fd9\u6837\u4e00\u4e2a\u51fd\u6570\u7684\u7b80\u5355\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include // Takes two std::string objects, returns the one that comes first alphabetically const std :: string & firstAlphabetical ( const std :: string & a , const std :: string & b ) { return ( a < b ) ? a : b ; // We can use operator< on std::string to determine which comes first alphabetically } int main () { std :: string hello { \"Hello\" }; std :: string world { \"World\" }; std :: cout << firstAlphabetical ( hello , world ) << '\\n' ; return 0 ; } \u8f93\u51fa\uff1a 1 Hello \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8c03\u7528\u8005\u6309\u5f15\u7528\u4f20\u9012\u4e86\u4e24\u4e2a std::string \u5bf9\u8c61\uff0c\u7136\u540e\u7ecf\u8fc7\u6bd4\u8f83\uff0c\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e2d\u6309\u7167\u5b57\u6bcd\u8868\u6bd4\u8f83\u6392\u5728\u524d\u9762\u7684\u5bf9\u8c61\u88ab\u6309\u5f15\u7528(const)\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u5982\u679c\u6211\u4eec\u662f\u6309\u7167\u503c\u4f20\u9012\uff0c\u5219\u4f1a\u5bfc\u81f4\u521b\u5efa std::string \u7684\u4e09\u4e2a\u62f7\u8d1d\uff08\u6bcf\u4e2a\u5f62\u53c2\u62f7\u8d1d\u4e00\u6b21\u3001\u8fd4\u56de\u503c\u62f7\u8d1d\u4e00\u6b21\uff09\u3002\u800c\u4f7f\u7528\u5f15\u7528\u4f20\u9012\u5219\u53ef\u4ee5\u907f\u514d\u8fd9\u4e9b\u62f7\u8d1d\u3002","title":"\u6309\u5f15\u7528\u8fd4\u56de\u6309\u5f15\u7528\u4f20\u9012\u7684\u53c2\u6570\u6ca1\u6709\u95ee\u9898"},{"location":"9-11-Return-by-reference-and-return-by-address/#caller-can-modify-values-through-the-reference","text":"\u5f53\u5b9e\u53c2\u6309\u975econst\u5f15\u7528\u4f20\u9012\u65f6\uff0c\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u4fee\u6539\u5b9e\u53c2\u7684\u503c\u3002 \u7c7b\u4f3c\u7684\uff0c\u5f53\u6309\u975econst\u5f15\u7528\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u65f6\uff0c\u8c03\u7528\u8005\u53ef\u4ee5\u4f7f\u7528\u8be5\u5f15\u7528\u4fee\u6539\u8fd4\u56de\u503c\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include // \u63a5\u53d7\u4e24\u4e2a\u6574\u578b\u7684\u975econst\u5f15\u7528\uff0c\u8fd4\u56de\u5176\u4e2d\u8f83\u5927\u7684\u4e00\u4e2a\uff08\u6309\u5f15\u7528\u8fd4\u56de\uff09 int & max ( int & x , int & y ) { return ( x > y ) ? x : y ; } int main () { int a { 5 }; int b { 6 }; max ( a , b ) = 7 ; // \u5c06a\u548cb\u4e2d\u8f83\u5927\u7684\u4e00\u4e2a\u7684\u503c\u8bbe\u7f6e\u4e3a 7 std :: cout << a << b << '\\n' ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c max(a, b) \u5728\u8c03\u7528 max() \u51fd\u6570\u65f6\u4f20\u5165 a \u548c b \u4f5c\u4e3a\u5b9e\u53c2\u3002\u5f15\u7528\u5f62\u53c2 x \u7ed1\u5b9a\u5230\u5b9e\u53c2 a \uff0c\u800c\u5f15\u7528\u5f62\u53c2 y \u7ed1\u5b9a\u5230\u5b9e\u53c2 b \u3002\u968f\u540e\uff0c\u51fd\u6570\u4f1a\u5224\u65ad x ( 5 ) \u548c y ( 6 ) \u54ea\u4e2a\u6bd4\u8f83\u5927\u3002\u672c\u4f8b\u4e2d\u663e\u7136 y \u66f4\u5927\uff0c\u56e0\u6b64 y (\u4ecd\u7136\u7ed1\u5b9a\u5230 b ) \u88ab\u6309\u5f15\u7528\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u8c03\u7528\u8005\u968f\u540e\u901a\u8fc7\u8fd4\u56de\u7684\u5f15\u7528\u5c06 b \u8d4b\u503c\u4e3a 7\u3002 \u56e0\u6b64\uff0c\u8868\u8fbe\u5f0f max(a, b) = 7 \u6700\u7ec8\u89e3\u6790\u4e3a b = 7 \u3002 \u7a0b\u5e8f\u6253\u5370\uff1a 1 57","title":"\u8c03\u7528\u8005\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u4fee\u6539\u503c caller can modify values through the reference"},{"location":"9-11-Return-by-reference-and-return-by-address/#_5","text":"\u6309\u5730\u5740\u8fd4\u56de \u4e0e \u6309\u5f15\u7528\u8fd4\u56de \u7684\u5de5\u4f5c\u539f\u7406\u51e0\u4e4e\u76f8\u540c\uff0c\u53ea\u4e0d\u8fc7\u8fd4\u56de\u7684\u662f\u6307\u5411\u5bf9\u8c61\u7684\u6307\u9488\u800c\u4e0d\u662f\u5bf9\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u6309\u5730\u5740\u8fd4\u56de\u6709\u4e0e\u6309\u5f15\u7528\u8fd4\u56de\u76f8\u540c\u7684\u6ce8\u610f\u4e8b\u9879\u2014\u2014\u6309\u5730\u5740\u8fd4\u56de\u7684\u5bf9\u8c61\u5fc5\u987b\u6bd4\u8fd4\u56de\u5730\u5740\u7684\u51fd\u6570\u7684\u4f5c\u7528\u57df\u66f4\u957f\u4e45\uff0c\u5426\u5219\u8c03\u7528\u8005\u5c06\u6536\u5230\u4e00\u4e2a\u60ac\u5782\u6307\u9488\u3002 \u6309\u5730\u5740\u8fd4\u56de\u6bd4\u6309\u5f15\u7528\u8fd4\u56de\u7684\u4e3b\u8981\u4f18\u70b9\u662f\uff0c\u5982\u679c\u6ca1\u6709\u8981\u8fd4\u56de\u7684\u6709\u6548\u5bf9\u8c61\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u8fd4\u56de nullptr \u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u60f3\u8981\u641c\u7d22\u7684\u5b66\u751f\u5217\u8868\u3002\u5982\u679c\u5728\u5217\u8868\u4e2d\u627e\u5230\u4e86\u8981\u67e5\u627e\u7684\u5b66\u751f\uff0c\u5219\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u8868\u793a\u5339\u914d\u5b66\u751f\u7684\u5bf9\u8c61\u7684\u6307\u9488\u3002\u5982\u679c\u6211\u4eec\u6ca1\u6709\u627e\u5230\u4efb\u4f55\u5339\u914d\u7684\u5b66\u751f\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd4\u56de nullptr \u6765\u8868\u793a\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u5b66\u751f\u5bf9\u8c61\u3002 \u5176\u4e3b\u8981\u7f3a\u70b9\u5219\u662f\u8c03\u7528\u8005\u5fc5\u987b\u8bb0\u5f97\u5728\u89e3\u5f15\u7528\u8fd4\u56de\u503c\u4e4b\u524d\u8fdb\u884c\u6307\u9488\u5224\u7a7a\u64cd\u4f5c\uff0c\u5219\u53ef\u80fd\u4f1a\u53d1\u751f\u7a7a\u6307\u9488\u89e3\u5f15\u7528\u5e76\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u7531\u4e8e\u8fd9\u79cd\u5371\u9669\uff0c\u9664\u975e\u9700\u8981\u8fd4\u56de\u201c\u65e0\u5bf9\u8c61\u201d\u7684\u80fd\u529b\uff0c\u5426\u5219\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u5e94\u4f18\u5148\u4e8e\u901a\u8fc7\u5730\u5740\u8fd4\u56de\u3002 \u6700\u4f73\u5b9e\u8df5 \u9996\u9009\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u800c\u4e0d\u662f\u901a\u8fc7\u5730\u5740\u8fd4\u56de\uff0c\u9664\u975e\u8fd4\u56de\u201c\u65e0\u5bf9\u8c61\u201d(\u4f7f\u7528 nullptr )\u7684\u80fd\u529b\u5f88\u91cd\u8981\u3002","title":"\u6309\u5730\u5740\u8fd4\u56de"},{"location":"9-12-Type-deduction-with-pointers-references-and-const/","tags":["const","type deduction"],"text":"Key Takeaway \u5728 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86 auto \u5173\u952e\u5b57\uff0c\u5b83\u544a\u8bc9\u7f16\u8bd1\u5668\u6839\u636e\u521d\u59cb\u5316\u503c\u5bf9\u53d8\u91cf\u7684\u7c7b\u578b\u8fdb\u884c\u63a8\u65ad\uff1a 1 2 3 4 5 6 7 8 int getVal (); // some function that returns an int by value int main () { auto val { getVal () }; // val deduced as type int return 0 ; } \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u7c7b\u578b\u63a8\u65ad\u4f1a\u4e22\u5f03 const \u9650\u5b9a\u7b26\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 const double foo () { return 5.6 ; } int main () { const double cd { 7.8 }; auto x { cd }; // double (const dropped) auto y { foo () }; // double (const dropped) return 0 ; } \u5982\u679c\u9700\u8981\uff0c\u6211\u4eec\u9700\u8981\u624b\u52a8\u5c06 const \u9650\u5b9a\u7b26\u91cd\u65b0\u6dfb\u52a0\u5230\u5b9a\u4e49\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 const double foo () { return 5.6 ; } int main () { const double cd { 7.8 }; const auto x { cd }; // const double (const reapplied) const auto y { foo () }; // const double (const reapplied) return 0 ; } \u7c7b\u578b\u63a8\u65ad\u4f1a\u4e22\u5f03\u5f15\u7528\u4fee\u9970\u7b26 \u9664\u4e86\u5220\u9664const\u9650\u5b9a\u7b26\u5916\uff0c\u7c7b\u578b\u63a8\u65ad\u4e5f\u5c06\u5220\u9664\u5f15\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 #include std :: string & getRef (); // some function that returns a reference int main () { auto ref { getRef () }; // type deduced as std::string (not std::string&) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf ref \u4f7f\u7528\u4e86\u7c7b\u578b\u63a8\u65ad\u3002\u5c3d\u7ba1\u51fd\u6570 getRef() \u8fd4\u56de\u7684\u662f std::string& \uff0c\u4f46\u5f15\u7528\u9650\u5b9a\u7b26\u88ab\u4e22\u5f03\u4e86\uff0c\u6240\u4ee5 ref \u7684\u7c7b\u578b\u4e3a std::string \u3002 \u7c7b\u4f3c\u4e8e const \u9650\u5b9a\u7b26\uff0c\u5982\u679c\u5f15\u7528\u9650\u5b9a\u7b26\u88ab\u4e22\u5f03\u4e86\uff0c\u4e5f\u53ef\u4ee5\u663e\u5f0f\u5730\u88ab\u518d\u6b21\u52a0\u5230\u5b9a\u4e49\u4e0a\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include std :: string & getRef (); // some function that returns a reference to const int main () { auto ref1 { getRef () }; // std::string (reference dropped) auto & ref2 { getRef () }; // std::string& (reference reapplied) return 0 ; } \u9876\u5c42 const \u548c\u5e95\u5c42 const \u9876\u5c42 const \u662f\u5e94\u7528\u4e8e\u5bf9\u8c61\u672c\u8eab\u7684 const \u9650\u5b9a\u7b26 \u3002\u4f8b\u5982\uff1a 1 2 const int x ; // this const applies to x, so it is top-level int * const ptr ; // this const applies to ptr, so it is top-level \u76f8\u6bd4\u4e4b\u4e0b\uff0c \u5e95\u5c42 const \u662f\u4e00\u4e2a\u5e94\u7528\u4e8e\u88ab\u5f15\u7528\u6216\u6307\u5411\u7684\u5bf9\u8c61\u7684 const \u9650\u5b9a\u7b26 \uff1a 1 2 const int & ref ; // this const applies to the object being referenced, so it is low-level const int * ptr ; // this const applies to the object being pointed to, so it is low-level const \u503c\u7684\u5f15\u7528\u603b\u662f\u4e00\u4e2a\u5e95\u5c42const\u3002\u6307\u9488\u53ef\u4ee5\u662f\u9876\u5c42 const\u3001\u5e95\u5c42const\u6216\u4e24\u8005\u90fd\u662f\uff1a 1 const int * const ptr ; // the left const is low-level, the right const is top-level \u7c7b\u578b\u63a8\u65ad\u5728\u4e22\u5f03 const \u9650\u5b9a\u7b26\u65f6\uff0c\u53ea\u4f1a\u4e22\u5f03\u9876\u5c42 const\u3002\u5e95\u5c42 const \u4e0d\u53d7\u5f71\u54cd\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u4f8b\u5b50\u4e2d\u8bf4\u660e\u8fd9\u4e00\u70b9\u3002 \u7c7b\u578b\u63a8\u65ad\u548cconst\u5f15\u7528 \u5982\u679c\u521d\u59cb\u5316\u503c\u662f\u5bf9const\u7684\u5f15\u7528\uff0c\u5219\u9996\u5148\u5220\u9664\u5f15\u7528(\u5982\u679c\u9002\u7528\uff0c\u7136\u540e\u91cd\u65b0\u5e94\u7528)\uff0c\u7136\u540e\u4ece\u7ed3\u679c\u4e2d\u5220\u9664\u4efb\u4f55\u9876\u7ea7const\u3002 1 2 3 4 5 6 7 8 9 10 #include const std :: string & getRef (); // some function that returns a reference to const int main () { auto ref1 { getRef () }; // std::string (reference dropped, then top-level const dropped from result) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c getRef() \u8fd4\u56de const std::string& \uff0c\u8be5\u5f15\u7528\u4f1a\u88ab\u9996\u5148\u4e22\u5f03\uff0c\u8fd4\u56de\u503c\u7c7b\u578b\u53d8\u4e3a const std::string \u3002\u6b64\u65f6\uff0c\u8be5 const \u662f\u4e00\u4e2a\u9876\u5c42 const\uff0c\u6240\u4ee5\u4e5f\u4f1a\u88ab\u4e22\u5f03\uff0c\u8fd4\u56de\u503c\u7c7b\u578b\u8fdb\u4e00\u6b65\u53d8\u4e3a std::string \u3002 \u4e24\u79cd\u6807\u8bc6\u7b26\u90fd\u53ef\u4ee5\u88ab\u624b\u52a8\u586b\u56de\u53bb\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include const std :: string & getRef (); // some function that returns a const reference int main () { auto ref1 { getRef () }; // std::string (top-level const and reference dropped) const auto ref2 { getRef () }; // const std::string (const reapplied, reference dropped) auto & ref3 { getRef () }; // const std::string& (reference reapplied, low-level const not dropped) const auto & ref4 { getRef () }; // const std::string& (reference and const reapplied) return 0 ; } ref1 \u5df2\u7ecf\u5728\u4e0a\u4e00\u4e2a\u4f8b\u5b50\u4e2d\u4ecb\u7ecd\u8fc7\u4e86\uff0c\u5bf9\u4e8e ref2 \uff0c\u5b83\u548c ref1 \u7c7b\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u91cd\u65b0\u6dfb\u52a0\u4e86 const \u9650\u5b9a\u7b26\uff0c\u6240\u4ee5\u6700\u7ec8\u7c7b\u578b\u4e3a const std::string \u3002 ref3 \u5219\u9700\u8981\u7279\u522b\u6ce8\u610f\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5f15\u7528\u4f1a\u88ab\u5220\u9664\uff0c\u4f46\u7531\u4e8e\u6211\u4eec\u5df2\u7ecf\u91cd\u65b0\u5e94\u7528\u4e86\u5f15\u7528\uff0c\u6240\u4ee5\u5b83\u4e0d\u4f1a\u88ab\u5220\u9664\u3002\u8fd9\u610f\u5473\u7740\u7c7b\u578b\u4ecd\u7136\u662f const std::string& \u3002\u56e0\u4e3a\u8fd9\u4e2aconst\u662f\u4e00\u4e2a\u5e95\u5c42\u7684const\uff0c\u6240\u4ee5\u5b83\u4e0d\u4f1a\u88ab\u4e22\u5f03\u3002\u56e0\u6b64\uff0c\u63a8\u5bfc\u51fa\u7684\u7c7b\u578b\u662f const std::string& \u3002 ref4 \u7684\u60c5\u51b5\u7c7b\u4f3c\u4e8e ref3 \uff0c\u53ea\u4e0d\u8fc7\u91cd\u65b0\u6dfb\u52a0\u5927\u91cf const \u9650\u5b9a\u7b26\u3002\u7531\u4e8e\u7c7b\u578b\u5df2\u7ecf\u63a8\u65ad\u4e3a\u6307\u5411\u5e38\u91cf\u7684\u5f15\u7528\uff0c\u6240\u4ee5\u6dfb\u52a0 const \u5176\u5b9e\u662f\u591a\u4f59\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8fd9\u91cc\u6211\u4eec\u660e\u786e\u5730\u6307\u51fa\u7ed3\u679c\u5e94\u8be5\u662f const \u7684\uff0c\u542b\u4e49\u975e\u5e38\u6e05\u695a\uff08 ref3 \u7684\u4f8b\u5b50\u4e2d\uff0c\u5e38\u91cf\u6027\u662f\u9690\u5f0f\u7684\uff0c\u5e76\u6ca1\u6709\u90a3\u4e48\u660e\u663e\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f60\u60f3\u8981\u4e00\u4e2aconst\u5f15\u7528\uff0c\u5373\u4f7f\u4e0d\u662f\u4e25\u683c\u5fc5\u8981\u7684\uff0c\u4e5f\u8981\u91cd\u65b0\u5e94\u7528 const \u9650\u5b9a\u7b26\uff0c\u56e0\u4e3a\u5b83\u4f7f\u4f60\u7684\u610f\u56fe\u66f4\u660e\u786e\uff0c\u5e76\u6709\u52a9\u4e8e\u9632\u6b62\u9519\u8bef\u3002 \u7c7b\u578b\u63a8\u65ad\u548c\u6307\u9488 \u4e0e\u5f15\u7528\u4e0d\u540c\uff0c\u7c7b\u578b\u6f14\u7ece\u4e0d\u4f1a\u4e22\u5f03\u6307\u9488\uff1a 1 2 3 4 5 6 7 8 9 10 #include std :: string * getPtr (); // some function that returns a pointer int main () { auto ptr1 { getPtr () }; // std::string* return 0 ; } \u6211\u4eec\u4e5f\u53ef\u4ee5\u5728\u6307\u9488\u7c7b\u578b\u6f14\u7ece\u7684\u540c\u65f6\u4f7f\u7528\u661f\u53f7\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include std :: string * getPtr (); // some function that returns a pointer int main () { auto ptr1 { getPtr () }; // std::string* auto * ptr2 { getPtr () }; // std::string* return 0 ; } auto \u548c auto* \u7684\u533a\u522b(\u9009\u8bfb) \u5728\u4f7f\u7528 auto \u548c\u6307\u9488\u7c7b\u578b\u7684\u521d\u59cb\u5316\u503c\u65f6\uff0c\u7c7b\u578b\u63a8\u65ad\u4f1a\u5305\u62ec\u6307\u9488\uff0c\u6240\u4ee5\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684 ptr1 \u7c7b\u578b\u4f1a\u88ab\u66ff\u6362\uff1a std::string* \u3002 \u5982\u679c\u5c06 auto* \u548c\u6307\u9488\u7c7b\u578b\u521d\u59cb\u5316\u503c\u4e00\u8d77\u4f7f\u7528\uff0c\u5219 auto \u7684\u7c7b\u578b\u63a8\u65ad\u4e0d\u4f1a\u5305\u542b\u6307\u9488\u2014\u2014\u6307\u9488\u662f\u5728\u7c7b\u578b\u63a8\u65ad\u5b8c\u6210\u540e\u88ab\u52a0\u4e0a\u7684\u3002\u6240\u4ee5\u5bf9\u4e8e\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684 ptr2 \uff0c auto \u66ff\u6362\u4e3a std::string \uff0c\u800c\u6307\u9488\u7684\u661f\u53f7\u662f\u540e\u52a0\u4e0a\u53bb\u7684\u3002 \u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4e0a\u9762\u4e24\u4e2a\u4f8b\u5b50\u7684\u5b9e\u9645\u6548\u679c\u662f\u4e00\u6837\u7684( ptr1 \u548c ptr2 \u90fd\u6700\u7ec8\u63a8\u65ad\u4e3a std::string* )\u3002 \u4f46\u662f\uff0c\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d auto \u548c auto* \u8fd8\u662f\u5b58\u5728\u4e0d\u5c11\u533a\u522b\u7684\u3002\u9996\u5148 auto* \u5fc5\u987b\u89e3\u6790\u4e3a\u4e00\u4e2a\u6307\u9488\u521d\u59cb\u5316\u503c\uff0c\u5426\u5219\u7f16\u8bd1\u4f1a\u62a5\u9519\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include std :: string * getPtr (); // some function that returns a pointer int main () { auto ptr3 { * getPtr () }; // std::string (\u5bf9 getPtr() \u89e3\u5f15\u7528\u4e86) auto * ptr4 { * getPtr () }; // \u4e0d\u80fd\u7f16\u8bd1 (\u521d\u59cb\u5316\u503c\u4e0d\u662f\u6307\u9488) return 0 ; } \u8fd9\u4e48\u505a\u662f\u6709\u610f\u4e49\u7684\uff0c\u5728 ptr4 \u7684\u4f8b\u5b50\u4e2d\uff0c auto \u5f97\u5230\u4e86 std::string \uff0c\u7136\u540e\u6307\u9488\u88ab\u91cd\u65b0\u52a0\u4e0a\u3002\u56e0\u6b64 ptr4 \u7684\u7c7b\u578b\u662f std::string* \uff0c\u6211\u4eec\u4e0d\u80fd\u4f7f\u7528\u4e00\u4e2a\u975e\u6307\u9488\u521d\u59cb\u5316\u503c\u6765\u521d\u59cb\u5316 std::string* \u7c7b\u578b\u7684\u53d8\u91cf\u3002 \u5176\u6b21\uff0c\u5f53\u8868\u8fbe\u5f0f\u4e2d\u51fa\u73b0 const \u65f6\uff0c auto \u548c auto* \u7684\u884c\u4e3a\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e00\u70b9\u3002 \u7c7b\u578b\u63a8\u65ad\u548cconst\u6307\u9488(\u9009\u8bfb) \u56e0\u4e3a\u6307\u9488\u4e0d\u4f1a\u88ab\u4e22\u5f03\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u5fc5\u62c5\u5fc3\u8fd9\u4e2a\u95ee\u9898\u3002\u4f46\u662f\u5bf9\u4e8e\u6307\u9488\uff0c\u6211\u4eec\u65e2\u8981\u8003\u8651\u6307\u9488\u5e38\u91cf\u4e5f\u8981\u8003\u8651\u5e38\u91cf\u6307\u9488\uff0c\u4ee5\u53ca auto \u548c auto* \u3002\u5c31\u50cf\u5f15\u7528\u4e00\u6837\uff0c\u5728\u6307\u9488\u7c7b\u578b\u63a8\u65ad\u671f\u95f4\uff0c\u53ea\u6709\u9876\u5c42const\u4f1a\u88ab\u4e22\u5f03\u3002 \u5148\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include std :: string * getPtr (); // some function that returns a pointer int main () { const auto ptr1 { getPtr () }; // std::string* const auto const ptr2 { getPtr () }; // std::string* const const auto * ptr3 { getPtr () }; // const std::string* auto * const ptr4 { getPtr () }; // std::string* const return 0 ; } \u4e0d\u8bba\u662f auto const \u8fd8\u662f const auto \uff0c\u90fd\u8868\u793a \u201c\u5c06\u7c7b\u578b\u63a8\u65ad\u7ed3\u679c\u53d8\u4e3aconst\u201d\u3002\u6240\u4ee5 ptr1 \u548c ptr2 \u7684\u4f8b\u5b50\u4e2d\uff0c\u63a8\u65ad\u7684\u7ed3\u679c\u4e3a std::string* \uff0c\u968f\u5373\u53c8\u88ab\u6dfb\u52a0\u4e86 const \uff0c\u6700\u7ec8\u5f97\u5230 std::string* const \u3002\u8fd9\u5c31\u548c const int \u4e0e int const \u662f\u4e00\u56de\u4e8b\u4e00\u6837\u3002 \u4f46\u662f\u5bf9\u4e8e auto* \uff0c const \u9650\u5b9a\u7b26\u7684\u4f4d\u7f6e\u4f1a\u5e26\u6765\u5f71\u54cd\u3002 const \u5728\u5de6\u9762\u8868\u793a\u201c\u63a8\u65ad\u5f97\u5230\u7684\u6307\u9488\u7c7b\u578b\u6307\u5411\u5e38\u91cf\u201d\uff0c\u800c const \u5728\u53f3\u65f6\u8868\u793a \u201c\u63a8\u65ad\u5f97\u5230\u7684\u6307\u9488\u4f5c\u4e3aconst\u6307\u9488\u201d\u3002\u56e0\u6b64 ptr3 \u6700\u7ec8\u662f\u4ee5\u4e00\u4e2a\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488\uff0c\u800c ptr4 \u5219\u662f\u4e00\u4e2a\u5e38\u91cf\u6307\u9488\u3002 \u518d\u770b\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5176\u4e2d\u521d\u59cb\u5316\u503c\u662f\u6307\u5411\u5e38\u91cf\u7684\u5e38\u91cf\u6307\u9488\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include const std :: string * const getConstPtr (); // some function that returns a const pointer to a const value int main () { auto ptr1 { getConstPtr () }; // const std::string* auto * ptr2 { getConstPtr () }; // const std::string* auto const ptr3 { getConstPtr () }; // const std::string* const const auto ptr4 { getConstPtr () }; // const std::string* const auto * const ptr5 { getConstPtr () }; // const std::string* const const auto * ptr6 { getConstPtr () }; // const std::string* const auto const ptr7 { getConstPtr () }; // error: const qualifer can not be applied twice const auto * const ptr8 { getConstPtr () }; // const std::string* const return 0 ; } ptr1 \u548c ptr2 \u5f88\u7b80\u5355\u3002\u9876\u5c42 const (\u4fee\u9970\u6307\u9488\u7684 const ) \u88ab\u4e22\u5f03\u3002\u4fee\u9970\u88ab\u6307\u5bf9\u8c61\u7684\u5e95\u5c42 const \u5219\u88ab\u4fdd\u6301\u4e0b\u6765\u3002\u6240\u4ee5\u5728\u8fd9\u4e24\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6700\u7ec8\u7684\u7c7b\u578b\u90fd\u662f const std::string* \u3002 ptr3 \u548c ptr4 \u7684\u4f8b\u5b50\u4e5f\u5f88\u7b80\u5355\u3002\u9876\u5c42 const \u88ab\u4e22\u5f03\uff0c\u4f46\u662f\u53c8\u88ab\u91cd\u65b0\u6dfb\u52a0\u4e86\u3002\u4fee\u9970\u88ab\u6307\u5bf9\u8c61\u7684\u5e95\u5c42 const \u88ab\u4fdd\u7559\u3002\u6240\u4ee5\u6700\u7ec8\u7c7b\u578b\u4e3a const std::string* const \u3002 ptr5 \u548c ptr6 \u4e0e\u4e4b\u524d\u4f8b\u5b50\u4e2d\u7684\u60c5\u51b5\u7c7b\u4f3c\u3002\u5b83\u4eec\u7684\u9876\u5c42 const \u90fd\u88ab\u4e22\u5f03\u4e86\u3002\u5bf9\u4e8e ptr5 \uff0c auto* const \u88ab\u91cd\u65b0\u6dfb\u52a0\uff0c\u6700\u7ec8\u7c7b\u578b\u4e3a const std::string* const \u3002\u5bf9\u4e8e ptr6 \uff0c const auto* \u5e94\u7528\u4e8e\u88ab\u6307\u5bf9\u8c61\uff08\u6b64\u65f6\u5df2\u7ecf\u662f const \uff09\uff0c\u56e0\u6b64\u6700\u7ec8\u7c7b\u578b\u4e3a const std::string* \u3002 \u5bf9\u4e8e ptr7 \uff0c\u6211\u4eec\u5e94\u7528\u4e86\u4e24\u6b21 const \u9650\u5b9a\u7b26\uff0c\u8fd9\u662f\u4e0d\u5141\u8bb8\u7684\uff0c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u62a5\u9519\u3002 \u5bf9\u4e8e ptr8 \uff0c\u6211\u4eec\u5728\u6307\u9488\u7684\u4e24\u4fa7\u6dfb\u52a0\u4e86 const \uff08\u53ef\u4ee5\u8fd9\u4e48\u505a\u56e0\u4e3a auto* \u4e00\u5b9a\u662f\u6307\u9488\u7c7b\u578b\uff09\uff0c\u6240\u4ee5\u6700\u7ec8\u7ed3\u679c\u4e3a const std::string* const \u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a const \u6307\u9488\uff0c\u5373\u4f7f\u4e0d\u662f\u4e25\u683c\u5fc5\u8981\u7684\uff0c\u4e5f\u8981\u91cd\u65b0\u5e94\u7528 const \u9650\u5b9a\u7b26\uff0c\u56e0\u4e3a\u5b83\u4f7f\u4f60\u7684\u610f\u56fe\u66f4\u660e\u786e\uff0c\u5e76\u6709\u52a9\u4e8e\u9632\u6b62\u9519\u8bef\u3002","title":"9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad"},{"location":"9-12-Type-deduction-with-pointers-references-and-const/#_1","text":"\u9664\u4e86\u5220\u9664const\u9650\u5b9a\u7b26\u5916\uff0c\u7c7b\u578b\u63a8\u65ad\u4e5f\u5c06\u5220\u9664\u5f15\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 #include std :: string & getRef (); // some function that returns a reference int main () { auto ref { getRef () }; // type deduced as std::string (not std::string&) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf ref \u4f7f\u7528\u4e86\u7c7b\u578b\u63a8\u65ad\u3002\u5c3d\u7ba1\u51fd\u6570 getRef() \u8fd4\u56de\u7684\u662f std::string& \uff0c\u4f46\u5f15\u7528\u9650\u5b9a\u7b26\u88ab\u4e22\u5f03\u4e86\uff0c\u6240\u4ee5 ref \u7684\u7c7b\u578b\u4e3a std::string \u3002 \u7c7b\u4f3c\u4e8e const \u9650\u5b9a\u7b26\uff0c\u5982\u679c\u5f15\u7528\u9650\u5b9a\u7b26\u88ab\u4e22\u5f03\u4e86\uff0c\u4e5f\u53ef\u4ee5\u663e\u5f0f\u5730\u88ab\u518d\u6b21\u52a0\u5230\u5b9a\u4e49\u4e0a\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include std :: string & getRef (); // some function that returns a reference to const int main () { auto ref1 { getRef () }; // std::string (reference dropped) auto & ref2 { getRef () }; // std::string& (reference reapplied) return 0 ; }","title":"\u7c7b\u578b\u63a8\u65ad\u4f1a\u4e22\u5f03\u5f15\u7528\u4fee\u9970\u7b26"},{"location":"9-12-Type-deduction-with-pointers-references-and-const/#const-const","text":"\u9876\u5c42 const \u662f\u5e94\u7528\u4e8e\u5bf9\u8c61\u672c\u8eab\u7684 const \u9650\u5b9a\u7b26 \u3002\u4f8b\u5982\uff1a 1 2 const int x ; // this const applies to x, so it is top-level int * const ptr ; // this const applies to ptr, so it is top-level \u76f8\u6bd4\u4e4b\u4e0b\uff0c \u5e95\u5c42 const \u662f\u4e00\u4e2a\u5e94\u7528\u4e8e\u88ab\u5f15\u7528\u6216\u6307\u5411\u7684\u5bf9\u8c61\u7684 const \u9650\u5b9a\u7b26 \uff1a 1 2 const int & ref ; // this const applies to the object being referenced, so it is low-level const int * ptr ; // this const applies to the object being pointed to, so it is low-level const \u503c\u7684\u5f15\u7528\u603b\u662f\u4e00\u4e2a\u5e95\u5c42const\u3002\u6307\u9488\u53ef\u4ee5\u662f\u9876\u5c42 const\u3001\u5e95\u5c42const\u6216\u4e24\u8005\u90fd\u662f\uff1a 1 const int * const ptr ; // the left const is low-level, the right const is top-level \u7c7b\u578b\u63a8\u65ad\u5728\u4e22\u5f03 const \u9650\u5b9a\u7b26\u65f6\uff0c\u53ea\u4f1a\u4e22\u5f03\u9876\u5c42 const\u3002\u5e95\u5c42 const \u4e0d\u53d7\u5f71\u54cd\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u4f8b\u5b50\u4e2d\u8bf4\u660e\u8fd9\u4e00\u70b9\u3002","title":"\u9876\u5c42 const \u548c\u5e95\u5c42 const"},{"location":"9-12-Type-deduction-with-pointers-references-and-const/#const","text":"\u5982\u679c\u521d\u59cb\u5316\u503c\u662f\u5bf9const\u7684\u5f15\u7528\uff0c\u5219\u9996\u5148\u5220\u9664\u5f15\u7528(\u5982\u679c\u9002\u7528\uff0c\u7136\u540e\u91cd\u65b0\u5e94\u7528)\uff0c\u7136\u540e\u4ece\u7ed3\u679c\u4e2d\u5220\u9664\u4efb\u4f55\u9876\u7ea7const\u3002 1 2 3 4 5 6 7 8 9 10 #include const std :: string & getRef (); // some function that returns a reference to const int main () { auto ref1 { getRef () }; // std::string (reference dropped, then top-level const dropped from result) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c getRef() \u8fd4\u56de const std::string& \uff0c\u8be5\u5f15\u7528\u4f1a\u88ab\u9996\u5148\u4e22\u5f03\uff0c\u8fd4\u56de\u503c\u7c7b\u578b\u53d8\u4e3a const std::string \u3002\u6b64\u65f6\uff0c\u8be5 const \u662f\u4e00\u4e2a\u9876\u5c42 const\uff0c\u6240\u4ee5\u4e5f\u4f1a\u88ab\u4e22\u5f03\uff0c\u8fd4\u56de\u503c\u7c7b\u578b\u8fdb\u4e00\u6b65\u53d8\u4e3a std::string \u3002 \u4e24\u79cd\u6807\u8bc6\u7b26\u90fd\u53ef\u4ee5\u88ab\u624b\u52a8\u586b\u56de\u53bb\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include const std :: string & getRef (); // some function that returns a const reference int main () { auto ref1 { getRef () }; // std::string (top-level const and reference dropped) const auto ref2 { getRef () }; // const std::string (const reapplied, reference dropped) auto & ref3 { getRef () }; // const std::string& (reference reapplied, low-level const not dropped) const auto & ref4 { getRef () }; // const std::string& (reference and const reapplied) return 0 ; } ref1 \u5df2\u7ecf\u5728\u4e0a\u4e00\u4e2a\u4f8b\u5b50\u4e2d\u4ecb\u7ecd\u8fc7\u4e86\uff0c\u5bf9\u4e8e ref2 \uff0c\u5b83\u548c ref1 \u7c7b\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u91cd\u65b0\u6dfb\u52a0\u4e86 const \u9650\u5b9a\u7b26\uff0c\u6240\u4ee5\u6700\u7ec8\u7c7b\u578b\u4e3a const std::string \u3002 ref3 \u5219\u9700\u8981\u7279\u522b\u6ce8\u610f\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5f15\u7528\u4f1a\u88ab\u5220\u9664\uff0c\u4f46\u7531\u4e8e\u6211\u4eec\u5df2\u7ecf\u91cd\u65b0\u5e94\u7528\u4e86\u5f15\u7528\uff0c\u6240\u4ee5\u5b83\u4e0d\u4f1a\u88ab\u5220\u9664\u3002\u8fd9\u610f\u5473\u7740\u7c7b\u578b\u4ecd\u7136\u662f const std::string& \u3002\u56e0\u4e3a\u8fd9\u4e2aconst\u662f\u4e00\u4e2a\u5e95\u5c42\u7684const\uff0c\u6240\u4ee5\u5b83\u4e0d\u4f1a\u88ab\u4e22\u5f03\u3002\u56e0\u6b64\uff0c\u63a8\u5bfc\u51fa\u7684\u7c7b\u578b\u662f const std::string& \u3002 ref4 \u7684\u60c5\u51b5\u7c7b\u4f3c\u4e8e ref3 \uff0c\u53ea\u4e0d\u8fc7\u91cd\u65b0\u6dfb\u52a0\u5927\u91cf const \u9650\u5b9a\u7b26\u3002\u7531\u4e8e\u7c7b\u578b\u5df2\u7ecf\u63a8\u65ad\u4e3a\u6307\u5411\u5e38\u91cf\u7684\u5f15\u7528\uff0c\u6240\u4ee5\u6dfb\u52a0 const \u5176\u5b9e\u662f\u591a\u4f59\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8fd9\u91cc\u6211\u4eec\u660e\u786e\u5730\u6307\u51fa\u7ed3\u679c\u5e94\u8be5\u662f const \u7684\uff0c\u542b\u4e49\u975e\u5e38\u6e05\u695a\uff08 ref3 \u7684\u4f8b\u5b50\u4e2d\uff0c\u5e38\u91cf\u6027\u662f\u9690\u5f0f\u7684\uff0c\u5e76\u6ca1\u6709\u90a3\u4e48\u660e\u663e\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f60\u60f3\u8981\u4e00\u4e2aconst\u5f15\u7528\uff0c\u5373\u4f7f\u4e0d\u662f\u4e25\u683c\u5fc5\u8981\u7684\uff0c\u4e5f\u8981\u91cd\u65b0\u5e94\u7528 const \u9650\u5b9a\u7b26\uff0c\u56e0\u4e3a\u5b83\u4f7f\u4f60\u7684\u610f\u56fe\u66f4\u660e\u786e\uff0c\u5e76\u6709\u52a9\u4e8e\u9632\u6b62\u9519\u8bef\u3002","title":"\u7c7b\u578b\u63a8\u65ad\u548cconst\u5f15\u7528"},{"location":"9-12-Type-deduction-with-pointers-references-and-const/#_2","text":"\u4e0e\u5f15\u7528\u4e0d\u540c\uff0c\u7c7b\u578b\u6f14\u7ece\u4e0d\u4f1a\u4e22\u5f03\u6307\u9488\uff1a 1 2 3 4 5 6 7 8 9 10 #include std :: string * getPtr (); // some function that returns a pointer int main () { auto ptr1 { getPtr () }; // std::string* return 0 ; } \u6211\u4eec\u4e5f\u53ef\u4ee5\u5728\u6307\u9488\u7c7b\u578b\u6f14\u7ece\u7684\u540c\u65f6\u4f7f\u7528\u661f\u53f7\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include std :: string * getPtr (); // some function that returns a pointer int main () { auto ptr1 { getPtr () }; // std::string* auto * ptr2 { getPtr () }; // std::string* return 0 ; }","title":"\u7c7b\u578b\u63a8\u65ad\u548c\u6307\u9488"},{"location":"9-12-Type-deduction-with-pointers-references-and-const/#auto-auto","text":"\u5728\u4f7f\u7528 auto \u548c\u6307\u9488\u7c7b\u578b\u7684\u521d\u59cb\u5316\u503c\u65f6\uff0c\u7c7b\u578b\u63a8\u65ad\u4f1a\u5305\u62ec\u6307\u9488\uff0c\u6240\u4ee5\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684 ptr1 \u7c7b\u578b\u4f1a\u88ab\u66ff\u6362\uff1a std::string* \u3002 \u5982\u679c\u5c06 auto* \u548c\u6307\u9488\u7c7b\u578b\u521d\u59cb\u5316\u503c\u4e00\u8d77\u4f7f\u7528\uff0c\u5219 auto \u7684\u7c7b\u578b\u63a8\u65ad\u4e0d\u4f1a\u5305\u542b\u6307\u9488\u2014\u2014\u6307\u9488\u662f\u5728\u7c7b\u578b\u63a8\u65ad\u5b8c\u6210\u540e\u88ab\u52a0\u4e0a\u7684\u3002\u6240\u4ee5\u5bf9\u4e8e\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684 ptr2 \uff0c auto \u66ff\u6362\u4e3a std::string \uff0c\u800c\u6307\u9488\u7684\u661f\u53f7\u662f\u540e\u52a0\u4e0a\u53bb\u7684\u3002 \u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4e0a\u9762\u4e24\u4e2a\u4f8b\u5b50\u7684\u5b9e\u9645\u6548\u679c\u662f\u4e00\u6837\u7684( ptr1 \u548c ptr2 \u90fd\u6700\u7ec8\u63a8\u65ad\u4e3a std::string* )\u3002 \u4f46\u662f\uff0c\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d auto \u548c auto* \u8fd8\u662f\u5b58\u5728\u4e0d\u5c11\u533a\u522b\u7684\u3002\u9996\u5148 auto* \u5fc5\u987b\u89e3\u6790\u4e3a\u4e00\u4e2a\u6307\u9488\u521d\u59cb\u5316\u503c\uff0c\u5426\u5219\u7f16\u8bd1\u4f1a\u62a5\u9519\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include std :: string * getPtr (); // some function that returns a pointer int main () { auto ptr3 { * getPtr () }; // std::string (\u5bf9 getPtr() \u89e3\u5f15\u7528\u4e86) auto * ptr4 { * getPtr () }; // \u4e0d\u80fd\u7f16\u8bd1 (\u521d\u59cb\u5316\u503c\u4e0d\u662f\u6307\u9488) return 0 ; } \u8fd9\u4e48\u505a\u662f\u6709\u610f\u4e49\u7684\uff0c\u5728 ptr4 \u7684\u4f8b\u5b50\u4e2d\uff0c auto \u5f97\u5230\u4e86 std::string \uff0c\u7136\u540e\u6307\u9488\u88ab\u91cd\u65b0\u52a0\u4e0a\u3002\u56e0\u6b64 ptr4 \u7684\u7c7b\u578b\u662f std::string* \uff0c\u6211\u4eec\u4e0d\u80fd\u4f7f\u7528\u4e00\u4e2a\u975e\u6307\u9488\u521d\u59cb\u5316\u503c\u6765\u521d\u59cb\u5316 std::string* \u7c7b\u578b\u7684\u53d8\u91cf\u3002 \u5176\u6b21\uff0c\u5f53\u8868\u8fbe\u5f0f\u4e2d\u51fa\u73b0 const \u65f6\uff0c auto \u548c auto* \u7684\u884c\u4e3a\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e00\u70b9\u3002","title":"auto \u548c auto* \u7684\u533a\u522b(\u9009\u8bfb)"},{"location":"9-12-Type-deduction-with-pointers-references-and-const/#const_1","text":"\u56e0\u4e3a\u6307\u9488\u4e0d\u4f1a\u88ab\u4e22\u5f03\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u5fc5\u62c5\u5fc3\u8fd9\u4e2a\u95ee\u9898\u3002\u4f46\u662f\u5bf9\u4e8e\u6307\u9488\uff0c\u6211\u4eec\u65e2\u8981\u8003\u8651\u6307\u9488\u5e38\u91cf\u4e5f\u8981\u8003\u8651\u5e38\u91cf\u6307\u9488\uff0c\u4ee5\u53ca auto \u548c auto* \u3002\u5c31\u50cf\u5f15\u7528\u4e00\u6837\uff0c\u5728\u6307\u9488\u7c7b\u578b\u63a8\u65ad\u671f\u95f4\uff0c\u53ea\u6709\u9876\u5c42const\u4f1a\u88ab\u4e22\u5f03\u3002 \u5148\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include std :: string * getPtr (); // some function that returns a pointer int main () { const auto ptr1 { getPtr () }; // std::string* const auto const ptr2 { getPtr () }; // std::string* const const auto * ptr3 { getPtr () }; // const std::string* auto * const ptr4 { getPtr () }; // std::string* const return 0 ; } \u4e0d\u8bba\u662f auto const \u8fd8\u662f const auto \uff0c\u90fd\u8868\u793a \u201c\u5c06\u7c7b\u578b\u63a8\u65ad\u7ed3\u679c\u53d8\u4e3aconst\u201d\u3002\u6240\u4ee5 ptr1 \u548c ptr2 \u7684\u4f8b\u5b50\u4e2d\uff0c\u63a8\u65ad\u7684\u7ed3\u679c\u4e3a std::string* \uff0c\u968f\u5373\u53c8\u88ab\u6dfb\u52a0\u4e86 const \uff0c\u6700\u7ec8\u5f97\u5230 std::string* const \u3002\u8fd9\u5c31\u548c const int \u4e0e int const \u662f\u4e00\u56de\u4e8b\u4e00\u6837\u3002 \u4f46\u662f\u5bf9\u4e8e auto* \uff0c const \u9650\u5b9a\u7b26\u7684\u4f4d\u7f6e\u4f1a\u5e26\u6765\u5f71\u54cd\u3002 const \u5728\u5de6\u9762\u8868\u793a\u201c\u63a8\u65ad\u5f97\u5230\u7684\u6307\u9488\u7c7b\u578b\u6307\u5411\u5e38\u91cf\u201d\uff0c\u800c const \u5728\u53f3\u65f6\u8868\u793a \u201c\u63a8\u65ad\u5f97\u5230\u7684\u6307\u9488\u4f5c\u4e3aconst\u6307\u9488\u201d\u3002\u56e0\u6b64 ptr3 \u6700\u7ec8\u662f\u4ee5\u4e00\u4e2a\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488\uff0c\u800c ptr4 \u5219\u662f\u4e00\u4e2a\u5e38\u91cf\u6307\u9488\u3002 \u518d\u770b\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5176\u4e2d\u521d\u59cb\u5316\u503c\u662f\u6307\u5411\u5e38\u91cf\u7684\u5e38\u91cf\u6307\u9488\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include const std :: string * const getConstPtr (); // some function that returns a const pointer to a const value int main () { auto ptr1 { getConstPtr () }; // const std::string* auto * ptr2 { getConstPtr () }; // const std::string* auto const ptr3 { getConstPtr () }; // const std::string* const const auto ptr4 { getConstPtr () }; // const std::string* const auto * const ptr5 { getConstPtr () }; // const std::string* const const auto * ptr6 { getConstPtr () }; // const std::string* const auto const ptr7 { getConstPtr () }; // error: const qualifer can not be applied twice const auto * const ptr8 { getConstPtr () }; // const std::string* const return 0 ; } ptr1 \u548c ptr2 \u5f88\u7b80\u5355\u3002\u9876\u5c42 const (\u4fee\u9970\u6307\u9488\u7684 const ) \u88ab\u4e22\u5f03\u3002\u4fee\u9970\u88ab\u6307\u5bf9\u8c61\u7684\u5e95\u5c42 const \u5219\u88ab\u4fdd\u6301\u4e0b\u6765\u3002\u6240\u4ee5\u5728\u8fd9\u4e24\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6700\u7ec8\u7684\u7c7b\u578b\u90fd\u662f const std::string* \u3002 ptr3 \u548c ptr4 \u7684\u4f8b\u5b50\u4e5f\u5f88\u7b80\u5355\u3002\u9876\u5c42 const \u88ab\u4e22\u5f03\uff0c\u4f46\u662f\u53c8\u88ab\u91cd\u65b0\u6dfb\u52a0\u4e86\u3002\u4fee\u9970\u88ab\u6307\u5bf9\u8c61\u7684\u5e95\u5c42 const \u88ab\u4fdd\u7559\u3002\u6240\u4ee5\u6700\u7ec8\u7c7b\u578b\u4e3a const std::string* const \u3002 ptr5 \u548c ptr6 \u4e0e\u4e4b\u524d\u4f8b\u5b50\u4e2d\u7684\u60c5\u51b5\u7c7b\u4f3c\u3002\u5b83\u4eec\u7684\u9876\u5c42 const \u90fd\u88ab\u4e22\u5f03\u4e86\u3002\u5bf9\u4e8e ptr5 \uff0c auto* const \u88ab\u91cd\u65b0\u6dfb\u52a0\uff0c\u6700\u7ec8\u7c7b\u578b\u4e3a const std::string* const \u3002\u5bf9\u4e8e ptr6 \uff0c const auto* \u5e94\u7528\u4e8e\u88ab\u6307\u5bf9\u8c61\uff08\u6b64\u65f6\u5df2\u7ecf\u662f const \uff09\uff0c\u56e0\u6b64\u6700\u7ec8\u7c7b\u578b\u4e3a const std::string* \u3002 \u5bf9\u4e8e ptr7 \uff0c\u6211\u4eec\u5e94\u7528\u4e86\u4e24\u6b21 const \u9650\u5b9a\u7b26\uff0c\u8fd9\u662f\u4e0d\u5141\u8bb8\u7684\uff0c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u62a5\u9519\u3002 \u5bf9\u4e8e ptr8 \uff0c\u6211\u4eec\u5728\u6307\u9488\u7684\u4e24\u4fa7\u6dfb\u52a0\u4e86 const \uff08\u53ef\u4ee5\u8fd9\u4e48\u505a\u56e0\u4e3a auto* \u4e00\u5b9a\u662f\u6307\u9488\u7c7b\u578b\uff09\uff0c\u6240\u4ee5\u6700\u7ec8\u7ed3\u679c\u4e3a const std::string* const \u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a const \u6307\u9488\uff0c\u5373\u4f7f\u4e0d\u662f\u4e25\u683c\u5fc5\u8981\u7684\uff0c\u4e5f\u8981\u91cd\u65b0\u5e94\u7528 const \u9650\u5b9a\u7b26\uff0c\u56e0\u4e3a\u5b83\u4f7f\u4f60\u7684\u610f\u56fe\u66f4\u660e\u786e\uff0c\u5e76\u6709\u52a9\u4e8e\u9632\u6b62\u9519\u8bef\u3002","title":"\u7c7b\u578b\u63a8\u65ad\u548cconst\u6307\u9488(\u9009\u8bfb)"},{"location":"9-2-Value-categories-lvalues-and-rvalues/","tags":["lvalues","rvalues"],"text":"Key Takeaway \u5de6\u503c\u5c31\u662f\u89e3\u6790\u5230\u6709\u6807\u8bc6\u7b26\u7684\u8868\u8fbe\u5f0f \u8bb0\u5fc6\u7a8d\u95e8\uff1a\u5de6\u503c\u51fa\u73b0\u5728\u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u5de6\u8fb9\uff0c\u53f3\u503c\u51fa\u73b0\u5728\u8d4b\u503c\u8868\u8fbe\u5f0f\u7684\u53f3\u8fb9\u3002\u5de6\u4fa7\u7684\u662f\u88ab\u8d4b\u503c\u7684\u4e00\u65b9\uff0c\u56e0\u6b64\u5fc5\u987b\u662f\u53ef\u53d8\u7684\u4e14\u5177\u6709\u6807\u8bc6\u7b26\u3002\u53f3\u4fa7\u663e\u7136\u53ef\u4ee5\u662f\u5b57\u9762\u91cf\u3001\u51fd\u6570\u548c\u5bf9\u8c61\uff0c\u5b83\u4eec\u90fd\u5c5e\u4e8e\u53f3\u503c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u56e0\u4e3a\u5de6\u503c\u53ef\u4ee5\u81ea\u52a8\u8f6c\u6362\u4e3a\u53f3\u503c\uff0c\u6240\u4ee5\u5de6\u503c\u53ef\u4ee5\u51fa\u73b0\u5728\u53f3\u503c\u7684\u4f4d\u7f6e\uff08\u5373\u53ef\u4ee5\u51fa\u73b0\u5728\u53f3\u4fa7\uff09 \u53f3\u503c\u4f1a\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u540e\u88ab\u9500\u6bc1\uff0c\u800c\u5de6\u503c\u5219\u4f1a\u7ee7\u7eed\u5b58\u5728 \u5728\u5f00\u59cb\u4ecb\u7ecd\u7b2c\u4e00\u4e2a\u590d\u5408\u7c7b\u578b \u5de6\u503c\u5f15\u7528 \u524d\uff0c\u8ba9\u6211\u4eec\u5148\u6765\u4e86\u89e3\u4e00\u4e0b\u4ec0\u4e48\u662f \u5de6\u503c \u3002 \u5728 1.10 - \u8868\u8fbe\u5f0f\u7b80\u4ecb \u4e2d\uff0c\u6211\u4eec\u5c06\u8868\u8fbe\u5f0f\u5b9a\u4e49\u4e3a\uff1a\u201c\u5b57\u9762\u91cf\u3001\u53d8\u91cf\u3001\u64cd\u4f5c\u7b26\u548c\u51fd\u6570\u8c03\u7528\u7684\u7ec4\u5408\u5e76\u53ef\u4ee5\u6267\u884c\u4ea7\u751f\u4e00\u4e2a\u5355\u4e00\u7684\u503c\u201d\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << 2 + 3 ; // The expression 2 + 3 produces the value 5 return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u8868\u8fbe\u5f0f 2 + 3 \u6c42\u503c\u4f1a\u5f97\u5230 5\uff0c\u7136\u540e 5 \u88ab\u6253\u5370\u5230\u63a7\u5236\u53f0\u4e0a\u3002 \u5728 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 \u4e2d\uff0c\u6211\u4eec\u8fd8\u4ecb\u7ecd\u8fc7\uff0c\u8868\u8fbe\u5f0f\u662f\u53ef\u4ee5\u6709 \u526f\u4f5c\u7528 \u7684\uff0c\u5373\u5176\u7ed3\u679c\u7684\u751f\u547d\u5468\u671f\u4f1a\u8d85\u8fc7\u8868\u8fbe\u5f0f\u672c\u8eab\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { int x { 5 }; ++ x ; // This expression statement has the side-effect of incrementing x std :: cout << x ; // prints 6 return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u8868\u8fbe\u5f0f ++x \u4f1a\u5c06 x \u7684\u503c\u9012\u589e\uff0c\u5176\u503c\u7684\u6539\u53d8\u5373\u4f7f\u5728\u8868\u8fbe\u5f0f\u6c42\u503c\u5b8c\u6210\u540e\u4e5f\u662f\u5b58\u5728\u7684\u3002 \u9664\u4e86\u4ea7\u751f\u503c\u548c\u526f\u4f5c\u7528\u4e4b\u5916\uff0c\u8868\u8fbe\u5f0f\u8fd8\u53ef\u4ee5\u505a\u53e6\u5916\u4e00\u4ef6\u4e8b\uff0c\u5b83\u4eec\u53ef\u4ee5\u6c42\u503c\u5f97\u5230\u5bf9\u8c61\u548c\u51fd\u6570\u2014\u2014\u6211\u4eec\u7a0d\u540e\u4f1a\u4ecb\u7ecd\u76f8\u5173\u5185\u5bb9\u3002 \u8868\u8fbe\u5f0f\u7684\u5c5e\u6027 \u4e3a\u4e86\u786e\u5b9a\u8868\u8fbe\u5f0f\u5982\u4f55\u6c42\u503c\u4ee5\u53ca\u53ef\u4ee5\u5728\u54ea\u91cc\u88ab\u4f7f\u7528\uff0cC++\u4e2d\u7684\u6240\u6709\u8868\u8fbe\u5f0f\u90fd\u6709\u4e24\u4e2a\u5c5e\u6027\uff1a \u8868\u8fbe\u5f0f\u7c7b\u578b \u548c \u8868\u8fbe\u5f0f\u503c\u7c7b\u522b \u3002 \u8868\u8fbe\u5f0f\u7684\u7c7b\u578b \u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u7b49\u4e8e\u8868\u8fbe\u5f0f\u6c42\u503c\u5f97\u5230\u7684\u503c\u3001\u5bf9\u8c61\u6216\u51fd\u6570\u7684\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { auto v1 { 12 / 4 }; // int / int => int auto v2 { 12.0 / 4 }; // double / int => double return 0 ; } \u5bf9\u4e8e v1 \uff0c\u7f16\u8bd1\u5668\u4f1a\u5728 \u7f16\u8bd1\u65f6 \u786e\u5b9a\u4e24\u4e2a int \u7684\u6c42\u503c\u7ed3\u679c\u662f int \uff0c\u6240\u4ee5 int \u5c31\u662f\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u3002\u901a\u8fc7 \u7c7b\u578b\u63a8\u65ad \uff0c int \u6700\u7ec8\u4f1a\u4f5c\u4e3a v1 \u7684\u7c7b\u578b\u3002 \u5bf9\u4e8e v2 \uff0c\u7f16\u8bd1\u5668\u4f1a\u5728 \u7f16\u8bd1\u65f6 \u786e\u5b9a\uff0c\u4e00\u4e2a double \u64cd\u4f5c\u6570\u548c\u4e00\u4e2a int \u64cd\u4f5c\u6570\u7684\u6c42\u503c\u7ed3\u679c\u662f double \u7c7b\u578b\u7684\u3002\u8fd8\u8bb0\u5f97\u5417\uff0c\u7b97\u6570\u8fd0\u7b97\u7b26\u8981\u6c42\u4e24\u4e2a\u64cd\u4f5c\u6570\u5177\u6709\u5339\u914d\u7684\u7c7b\u578b\uff0c\u56e0\u6b64\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c int \u64cd\u4f5c\u6570\u4f1a\u88ab\u8f6c\u6362\u4e3a double \uff0c\u968f\u540e\u5c31\u4f1a\u6267\u884c\u6d6e\u70b9\u6570\u9664\u6cd5\u3002\u6240\u4ee5 double \u4f1a\u6210\u4e3a\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u3002 \u7f16\u8bd1\u5668\u4f1a\u4f7f\u7528\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u6765\u5224\u65ad\u5728\u7279\u5b9a\u4e0a\u4e0b\u6587\u4e2d\u662f\u4e0d\u662f\u5408\u6cd5\u7684\u8868\u8fbe\u5f0f \uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void print ( int x ) { std :: cout << x ; } int main () { print ( \"foo\" ); // error: print() was expecting an int argument, we tried to pass in a string literal return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c print(int) \u51fd\u6570\u671f\u671b\u4e00\u4e2a int \u7c7b\u578b\u7684 \u5f62\u53c2 \u3002\u4e0d\u8fc7\uff0c\u6b64\u5904\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\uff08\u5b57\u7b26\u4e32\"foo\"\uff09\u5e76\u4e0d\u5339\u914d\uff0c\u800c\u4e14\u6ca1\u6709\u53ef\u7528\u7684\u7c7b\u578b\u8f6c\u6362\u53ef\u7528\u3002\u6240\u4ee5\u4f1a\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002 \u6ce8\u610f\uff0c\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u786e\u5b9a\uff08\u5426\u5219\u7c7b\u578b\u68c0\u67e5\u548c\u7c7b\u578b\u63a8\u65ad\u5c06\u65e0\u6cd5\u8fdb\u884c\uff09\u2014\u2014\u4e0d\u8fc7\uff0c\u8868\u8fbe\u5f0f\u7684\u503c\u5219\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u786e\u5b9a\uff08\u5982\u679c \u8868\u8fbe\u5f0f\u662fconstexpr\uff09\u4e5f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u786e\u5b9a\uff08\u5bf9\u4e8e\u4e0d\u662fconstexpr\u7c7b\u578b\u7684\u8868\u8fbe\u5f0f\uff09\u3002 \u8868\u8fbe\u5f0f\u7684\u503c\u7c7b\u522b \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 int main () { int x {}; x = 5 ; // valid: we can assign 5 to x 5 = x ; // error: can not assign value of x to literal value 5 return 0 ; } \u5176\u4e2d\u7684\u4e00\u4e2a\u8d4b\u503c\u8bed\u53e5\u662f\u5408\u6cd5\u7684\uff08\u5c06 5 \u8d4b\u503c\u7ed9 x\uff09\uff0c\u53e6\u5916\u4e00\u4e2a\u5219\u662f\u5408\u6cd5\u7684\uff08\u5c06x\u7684\u503c\u8d4b\u503c\u7ed9\u5b57\u9762\u91cf5\u662f\u4ec0\u4e48\u610f\u601d\uff1f\uff09\u3002\u53ef\u662f\uff0c\u7f16\u8bd1\u5668\u662f\u5982\u4f55\u5224\u65ad\u8d4b\u503c\u8bed\u53e5\u4e2d\u7684\u8868\u8fbe\u5f0f\u662f\u5426\u5408\u6cd5\u5462\uff1f \u8fd9\u4e2a\u95ee\u9898\u7684\u7b54\u6848\u5c31\u5728\u4e8e\u7b2c\u4e8c\u4e2a\u8868\u8fbe\u5f0f\u5c5e\u6027\uff1a\u8868\u8fbe\u5f0f \u503c\u7c7b\u578b \u3002\u503c\u7c7b\u578b\u8868\u660e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4f1a\u6c42\u503c\u5f97\u5230\u4e00\u4e2a\u503c\u3001\u4e00\u4e2a\u51fd\u6570\u8fd8\u662f\u4e00\u4e2a\u5bf9\u8c61\u3002 \u5728 C++11 \u4e4b\u524d\uff0cC++ \u4e2d\u53ea\u6709\u5577\u4e2a\u53ef\u80fd\u7684\u503c\u7c7b\u578b\uff1a \u5de6\u503c \u548c \u53f3\u503c \u3002 \u5728 C++11 \u4e2d\uff0c\u65b0\u589e\u4e86\u4e09\u4e2a\u503c\u7c7b\u578b ( glvalue \u3001 prvalue \u548c xvalue )\uff0c\u7528\u4e8e\u652f\u6301 \u79fb\u52a8\u8bed\u4e49 \u3002 \u4f5c\u8005\u6ce8 \u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u53ea\u5173\u6ce8 C++11 \u4e4b\u524d\u7684\u4e24\u79cd\u503c\u7c7b\u578b\uff08\u4e5f\u662f\u6211\u4eec\u76ee\u524d\u6240\u9700\u8981\u7684\uff09\u4f5c\u4e3a\u7b80\u4ecb\u3002\u6211\u4eec\u4f1a\u5728 M.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u79fb\u52a8\u548c\u667a\u80fd\u6307\u9488 \u4e00\u7ae0\u4e2d\u4ecb\u7ecd\u79fb\u52a8\u8bed\u4e49\u548c\u5176\u4ed6\u7684\u51e0\u79cd\u503c\u7c7b\u578b\u3002 Lvalue \u548c rvalue \u8868\u8fbe\u5f0f \u5de6\u503c lvalue (\u662f \u201cleft value\u201d \u6216 \u201clocator value\u201d\u7684\u7f29\u5199\uff0c\u6709\u65f6\u4e5f\u5199\u4f5c \u201cl-value\u201d) \u662f\u4e00\u79cd\u8868\u8fbe\u5f0f\uff0c\u5b83\u6700\u7ec8\u4f1a\u6c42\u503c\u5f97\u5230\u4e00\u4e2a\u5177\u6709 \u8eab\u4efd\u7279\u5f81 \u7684\u51fd\u6570\u6216\u5bf9\u8c61\u3002\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u5177\u6709 \u6807\u8bc6\u7b26(identifier) \uff08\u4f8b\u5982\u53d8\u91cf\u540d\u6216\u51fd\u6570\u540d\uff09\u6216\u4e00\u4e2a\u53ef\u88ab\u6807\u8bc6\u7684\u5185\u5b58\u5730\u5740\uff08\u53ef\u4ee5\u901a\u8fc7 & \u8fd0\u7b97\u7b26\u53d6\u5730\u5740\u2014\u2014\u4f1a\u5728 9.6 - \u6307\u9488\u7b80\u4ecb \u4e2d\u8fdb\u884c\u4ecb\u7ecd\uff09\u5219\u79f0\u5176\u5177\u6709\u8eab\u4efd\u7279\u5f81\u3002\u5177\u6709\u6807\u8bc6\u7684\u5bf9\u8c61\u751f\u547d\u5468\u671f\u4f1a\u8d85\u8fc7\u8868\u8fbe\u5f0f\u672c\u8eab\u3002 1 2 3 4 5 6 7 8 9 10 #include int main () { int x {}; std :: cout << x << '\\n' ; // x is an lvalue expression return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u8868\u8fbe\u5f0f x \u662f\u4e00\u4e2a\u5de6\u503c\u8868\u8fbe\u5f0f\uff0c\u56e0\u4e3a\u5b83\u6c42\u503c\u5f97\u5230\u53d8\u91cf x \uff08\u5b83\u5177\u6709\u6807\u8bc6\u7b26\uff09\u3002 \u56e0\u4e3a\u8bed\u8a00\u5f15\u5165\u4e86\u5e38\u91cf\uff0c\u5de6\u503c\u73b0\u5728\u6709\u4e24\u4e2a\u5b50\u7c7b\u578b\uff1a\u53ef\u53d8\u5de6\u503c\u2014\u2014\u5b83\u7684\u503c\u662f\u53ef\u4ee5\u6539\u53d8\u7684\u548c\u4e0d\u53ef\u53d8\u5de6\u503c\u2014\u2014\u5b83\u7684\u503c\u4e0d\u53ef\u4ee5\u88ab\u4fee\u6539\uff08\u56e0\u4e3a\u8be5\u5de6\u503c\u662f const \u6216 constexpr\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int x {}; const double d {}; std :: cout << x << '\\n' ; // x is a modifiable lvalue expression std :: cout << d << '\\n' ; // d is a non-modifiable lvalue expression return 0 ; } rvalue (\u662f\u201cright value\u201d\u7684\u7b80\u79f0\uff0c\u6709\u65f6\u5019\u4e5f\u5199\u4f5c r-value ) \u662f\u4e00\u79cd\u8868\u8fbe\u5f0f\uff0c\u9664\u4e86\u5de6\u503c\u4e4b\u5916\u7684\u90fd\u662f\u53f3\u503c\u3002\u5e38\u89c1\u7684\u53f3\u503c\u5305\u62ec \u5b57\u9762\u91cf \uff08\u9664\u4e86\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff0c\u5b83\u662f\u5de6\u503c\uff09\u548c\u51fd\u6570\u6216\u64cd\u4f5c\u7b26\u7684\u8fd4\u56de\u503c\u3002 \u53f3\u503c \u5176\u4f5c\u7528\u57df\u53ea\u5b58\u5728\u4e8e\u4f7f\u7528\u5b83\u7684\u8868\u8fbe\u5f0f\u4e2d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int return5 () { return 5 ; } int main () { int x { 5 }; // 5 is an rvalue expression const double d { 1.2 }; // 1.2 is an rvalue expression std :: cout << x << '\\n' ; // x is a modifiable lvalue expression std :: cout << d << '\\n' ; // d is a non-modifiable lvalue expression std :: cout << return5 (); // return5() is an rvalue expression (since the result is returned by value) std :: cout << x + 1 << '\\n' ; // x + 1 is a rvalue std :: cout << static_cast < int > ( d ) << '\\n' ; // the result of static casting d to an int is an rvalue return 0 ; } \u4f60\u53ef\u80fd\u4f1a\u597d\u5947\u4e3a\u4ec0\u4e48 return5() \u548c x + 1 \u5c5e\u4e8e\u53f3\u503c\uff1a\u95ee\u9898\u7684\u7b54\u6848\u5728\u4e8e\u8fd9\u4e9b\u8868\u8fbe\u5f0f\u7684\u503c\u5728\u4ea7\u751f\u540e\u5fc5\u987b\u9a6c\u4e0a\u4f7f\u7528\uff08\u5728\u8868\u8fbe\u5f0f\u7684\u4f5c\u7528\u57df\u5185\uff09\u6216\u88ab\u4e22\u5f03\u3002 \u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u56de\u7b54\u524d\u9762\u7684\u95ee\u9898\u4e86\uff0c\u4e3a\u4ec0\u4e48 x = 5 \u662f\u5408\u6cd5\u7684\u4f46 5 = x \u5219\u662f\u4e0d\u5408\u6cd5\uff1a\u8d4b\u503c\u8fd0\u7b97\u7b26\u8981\u6c42\u5176\u5de6\u64cd\u4f5c\u6570\u662f\u4e00\u4e2a\u53ef\u4fee\u6539\u7684\u5de6\u503c\u8868\u8fbe\u5f0f\uff0c\u4e14\u53f3\u64cd\u4f5c\u6570\u4e3a\u4e00\u4e2a\u53f3\u503c\u8868\u8fbe\u5f0f\u3002\u8d4b\u503c\u64cd\u4f5c 5 = x \u5931\u8d25\u662f\u56e0\u4e3a 5 \u5e76\u4e0d\u662f\u4e00\u4e2a\u5de6\u503c\u3002 1 2 3 4 5 6 7 8 9 10 int main () { int x {}; // Assignment requires the left operand to be a modifiable lvalue expression and the right operand to be an rvalue expression x = 5 ; // valid: x is a modifiable lvalue expression and 5 is an rvalue expression 5 = x ; // error: 5 is an rvalue expression and x is a modifiable lvalue expression return 0 ; } \u76f8\u5173\u5185\u5bb9 \u5de6\u503c\u548c\u53f3\u503c\u8868\u8fbe\u5f0f\u7684\u5b8c\u6574\u5217\u8868\u53c2\u8003 \u8fd9\u91cc \u3002\u5728C++11\u4e2d\uff0c\u53f3\u503c\u88ab\u5206\u6210\u4e86\u4e24\u4e2a\u5b50\u7c7b\u522b\uff1aprvalues \u548c xvalues\u3002\u6b64\u5904\u6211\u4eec\u8c08\u8bba\u7684 rvalues \u7efc\u5408\u4e86\u8fd9\u4e24\u79cd\u7c7b\u578b\u7684\u7279\u70b9\u3002 L-value \u548c r-value \u7684\u8f6c\u6362 \u5728\u4e0a\u9762\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u4e2d\u6211\u4eec\u63d0\u5230\uff0c\u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u53f3\u4fa7\u8fd0\u7b97\u6570\u5fc5\u987b\u662f\u4e00\u4e2a\u53f3\u503c\u8868\u8fbe\u5f0f\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u6b63\u786e\u8fd0\u884c\u5462\uff1f 1 2 3 4 5 6 7 8 9 int main () { int x { 1 }; int y { 2 }; x = y ; // y \u662f\u4e00\u4e2a\u53ef\u4ee5\u4fee\u6539\u7684\u5de6\u503c\u800c\u4e0d\u662f\u53f3\u503c\uff0c\u4e3a\u4ec0\u4e48\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u662f\u5408\u6cd5\u7684\u5462\uff1f return 0 ; } \u8fd9\u662f\u56e0\u4e3a\u5de6\u503c\u53ef\u4ee5\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u53f3\u503c\uff0c\u6240\u4ee5\u4efb\u4f55\u53ef\u4ee5\u4f7f\u7528\u53f3\u503c\u7684\u5730\u65b9\u90fd\u53ef\u4ee5\u4f7f\u7528\u5de6\u503c\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 int main () { int x { 2 }; x = x + 1 ; return 0 ; } \u5728\u8fd9\u4e2a\u8bed\u53e5\u4e2d\uff0c\u53d8\u91cfx\u7684\u4f7f\u7528\u5b58\u5728\u4e24\u4e2a\u4e0d\u540c\u7684\u8bed\u5883\u3002\u8d4b\u503c\u8fd0\u7b97\u7b26\u5de6\u4fa7\u7684x\u662f\u4e00\u4e2a\u5de6\u503c\u8868\u8fbe\u5f0f\uff0c\u5b83\u6c42\u503c\u5f97\u5230\u53d8\u91cf x\u3002\u8d4b\u503c\u8fd0\u7b97\u7b26\u53f3\u4fa7\u7684 x+1 \u5219\u662f\u4e00\u4e2a\u53f3\u503c\u8868\u8fbe\u5f0f\uff0c\u5b83\u6c42\u503c\u5f97\u52303\u3002 \u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u8be6\u7ec6\u5730\u4ecb\u7ecd\u4e86\u5de6\u503c\uff0c\u63a5\u4e0b\u6765\u5c31\u662f\u6211\u4eec\u8981\u63a5\u89e6\u5230\u7684\u7b2c\u4e00\u4e2a\u590d\u6742\u8868\u8fbe\u5f0f\u7c7b\u578b\uff1a \u5de6\u503c\u5f15\u7528 \u3002 \u5173\u952e\u4fe1\u606f \u8fa8\u522b\u5de6\u503c\u8868\u8fbe\u5f0f\u548c\u53f3\u503c\u8868\u8fbe\u5f0f\u7684\u5173\u952e\u6cd5\u5219\uff1a - \u5de6\u503c\u8868\u8fbe\u5f0f\u662f\u90a3\u4e9b\u6c42\u503c\u4e3a\u53d8\u91cf\u6216\u5176\u4ed6\u5177\u6709\u8eab\u4efd\u6807\u8bc6\u7684\u53d8\u91cf\uff0c\u4e14\u5b83\u4eec\u53ef\u4ee5\u6301\u7eed\u5230\u8868\u8fbe\u5f0f\u7ed3\u675f\u540e\u3002 - \u53f3\u503c\u8868\u8fbe\u5f0f\u5219\u662f\u90a3\u4e9b\u53ef\u4ee5\u6c42\u503c\u4e3a\u5b57\u9762\u91cf\u7684\u8868\u8fbe\u5f0f\uff0c\u6216\u8005\u662f\u51fd\u6570\u548c\u64cd\u4f5c\u7b26\u7684\u8fd4\u56de\u503c\uff0c\u5b83\u4eec\u4f1a\u5728\u8868\u8fbe\u5f0f\u89e3\u91ca\u540e\u88ab\u4e22\u5f03\u3002","title":"9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09"},{"location":"9-2-Value-categories-lvalues-and-rvalues/#_1","text":"\u4e3a\u4e86\u786e\u5b9a\u8868\u8fbe\u5f0f\u5982\u4f55\u6c42\u503c\u4ee5\u53ca\u53ef\u4ee5\u5728\u54ea\u91cc\u88ab\u4f7f\u7528\uff0cC++\u4e2d\u7684\u6240\u6709\u8868\u8fbe\u5f0f\u90fd\u6709\u4e24\u4e2a\u5c5e\u6027\uff1a \u8868\u8fbe\u5f0f\u7c7b\u578b \u548c \u8868\u8fbe\u5f0f\u503c\u7c7b\u522b \u3002","title":"\u8868\u8fbe\u5f0f\u7684\u5c5e\u6027"},{"location":"9-2-Value-categories-lvalues-and-rvalues/#_2","text":"\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u7b49\u4e8e\u8868\u8fbe\u5f0f\u6c42\u503c\u5f97\u5230\u7684\u503c\u3001\u5bf9\u8c61\u6216\u51fd\u6570\u7684\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { auto v1 { 12 / 4 }; // int / int => int auto v2 { 12.0 / 4 }; // double / int => double return 0 ; } \u5bf9\u4e8e v1 \uff0c\u7f16\u8bd1\u5668\u4f1a\u5728 \u7f16\u8bd1\u65f6 \u786e\u5b9a\u4e24\u4e2a int \u7684\u6c42\u503c\u7ed3\u679c\u662f int \uff0c\u6240\u4ee5 int \u5c31\u662f\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u3002\u901a\u8fc7 \u7c7b\u578b\u63a8\u65ad \uff0c int \u6700\u7ec8\u4f1a\u4f5c\u4e3a v1 \u7684\u7c7b\u578b\u3002 \u5bf9\u4e8e v2 \uff0c\u7f16\u8bd1\u5668\u4f1a\u5728 \u7f16\u8bd1\u65f6 \u786e\u5b9a\uff0c\u4e00\u4e2a double \u64cd\u4f5c\u6570\u548c\u4e00\u4e2a int \u64cd\u4f5c\u6570\u7684\u6c42\u503c\u7ed3\u679c\u662f double \u7c7b\u578b\u7684\u3002\u8fd8\u8bb0\u5f97\u5417\uff0c\u7b97\u6570\u8fd0\u7b97\u7b26\u8981\u6c42\u4e24\u4e2a\u64cd\u4f5c\u6570\u5177\u6709\u5339\u914d\u7684\u7c7b\u578b\uff0c\u56e0\u6b64\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c int \u64cd\u4f5c\u6570\u4f1a\u88ab\u8f6c\u6362\u4e3a double \uff0c\u968f\u540e\u5c31\u4f1a\u6267\u884c\u6d6e\u70b9\u6570\u9664\u6cd5\u3002\u6240\u4ee5 double \u4f1a\u6210\u4e3a\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u3002 \u7f16\u8bd1\u5668\u4f1a\u4f7f\u7528\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u6765\u5224\u65ad\u5728\u7279\u5b9a\u4e0a\u4e0b\u6587\u4e2d\u662f\u4e0d\u662f\u5408\u6cd5\u7684\u8868\u8fbe\u5f0f \uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void print ( int x ) { std :: cout << x ; } int main () { print ( \"foo\" ); // error: print() was expecting an int argument, we tried to pass in a string literal return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c print(int) \u51fd\u6570\u671f\u671b\u4e00\u4e2a int \u7c7b\u578b\u7684 \u5f62\u53c2 \u3002\u4e0d\u8fc7\uff0c\u6b64\u5904\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\uff08\u5b57\u7b26\u4e32\"foo\"\uff09\u5e76\u4e0d\u5339\u914d\uff0c\u800c\u4e14\u6ca1\u6709\u53ef\u7528\u7684\u7c7b\u578b\u8f6c\u6362\u53ef\u7528\u3002\u6240\u4ee5\u4f1a\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002 \u6ce8\u610f\uff0c\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u786e\u5b9a\uff08\u5426\u5219\u7c7b\u578b\u68c0\u67e5\u548c\u7c7b\u578b\u63a8\u65ad\u5c06\u65e0\u6cd5\u8fdb\u884c\uff09\u2014\u2014\u4e0d\u8fc7\uff0c\u8868\u8fbe\u5f0f\u7684\u503c\u5219\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u786e\u5b9a\uff08\u5982\u679c \u8868\u8fbe\u5f0f\u662fconstexpr\uff09\u4e5f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u786e\u5b9a\uff08\u5bf9\u4e8e\u4e0d\u662fconstexpr\u7c7b\u578b\u7684\u8868\u8fbe\u5f0f\uff09\u3002","title":"\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b"},{"location":"9-2-Value-categories-lvalues-and-rvalues/#_3","text":"\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 int main () { int x {}; x = 5 ; // valid: we can assign 5 to x 5 = x ; // error: can not assign value of x to literal value 5 return 0 ; } \u5176\u4e2d\u7684\u4e00\u4e2a\u8d4b\u503c\u8bed\u53e5\u662f\u5408\u6cd5\u7684\uff08\u5c06 5 \u8d4b\u503c\u7ed9 x\uff09\uff0c\u53e6\u5916\u4e00\u4e2a\u5219\u662f\u5408\u6cd5\u7684\uff08\u5c06x\u7684\u503c\u8d4b\u503c\u7ed9\u5b57\u9762\u91cf5\u662f\u4ec0\u4e48\u610f\u601d\uff1f\uff09\u3002\u53ef\u662f\uff0c\u7f16\u8bd1\u5668\u662f\u5982\u4f55\u5224\u65ad\u8d4b\u503c\u8bed\u53e5\u4e2d\u7684\u8868\u8fbe\u5f0f\u662f\u5426\u5408\u6cd5\u5462\uff1f \u8fd9\u4e2a\u95ee\u9898\u7684\u7b54\u6848\u5c31\u5728\u4e8e\u7b2c\u4e8c\u4e2a\u8868\u8fbe\u5f0f\u5c5e\u6027\uff1a\u8868\u8fbe\u5f0f \u503c\u7c7b\u578b \u3002\u503c\u7c7b\u578b\u8868\u660e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4f1a\u6c42\u503c\u5f97\u5230\u4e00\u4e2a\u503c\u3001\u4e00\u4e2a\u51fd\u6570\u8fd8\u662f\u4e00\u4e2a\u5bf9\u8c61\u3002 \u5728 C++11 \u4e4b\u524d\uff0cC++ \u4e2d\u53ea\u6709\u5577\u4e2a\u53ef\u80fd\u7684\u503c\u7c7b\u578b\uff1a \u5de6\u503c \u548c \u53f3\u503c \u3002 \u5728 C++11 \u4e2d\uff0c\u65b0\u589e\u4e86\u4e09\u4e2a\u503c\u7c7b\u578b ( glvalue \u3001 prvalue \u548c xvalue )\uff0c\u7528\u4e8e\u652f\u6301 \u79fb\u52a8\u8bed\u4e49 \u3002 \u4f5c\u8005\u6ce8 \u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u53ea\u5173\u6ce8 C++11 \u4e4b\u524d\u7684\u4e24\u79cd\u503c\u7c7b\u578b\uff08\u4e5f\u662f\u6211\u4eec\u76ee\u524d\u6240\u9700\u8981\u7684\uff09\u4f5c\u4e3a\u7b80\u4ecb\u3002\u6211\u4eec\u4f1a\u5728 M.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u79fb\u52a8\u548c\u667a\u80fd\u6307\u9488 \u4e00\u7ae0\u4e2d\u4ecb\u7ecd\u79fb\u52a8\u8bed\u4e49\u548c\u5176\u4ed6\u7684\u51e0\u79cd\u503c\u7c7b\u578b\u3002","title":"\u8868\u8fbe\u5f0f\u7684\u503c\u7c7b\u522b"},{"location":"9-2-Value-categories-lvalues-and-rvalues/#lvalue-rvalue","text":"\u5de6\u503c lvalue (\u662f \u201cleft value\u201d \u6216 \u201clocator value\u201d\u7684\u7f29\u5199\uff0c\u6709\u65f6\u4e5f\u5199\u4f5c \u201cl-value\u201d) \u662f\u4e00\u79cd\u8868\u8fbe\u5f0f\uff0c\u5b83\u6700\u7ec8\u4f1a\u6c42\u503c\u5f97\u5230\u4e00\u4e2a\u5177\u6709 \u8eab\u4efd\u7279\u5f81 \u7684\u51fd\u6570\u6216\u5bf9\u8c61\u3002\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u5177\u6709 \u6807\u8bc6\u7b26(identifier) \uff08\u4f8b\u5982\u53d8\u91cf\u540d\u6216\u51fd\u6570\u540d\uff09\u6216\u4e00\u4e2a\u53ef\u88ab\u6807\u8bc6\u7684\u5185\u5b58\u5730\u5740\uff08\u53ef\u4ee5\u901a\u8fc7 & \u8fd0\u7b97\u7b26\u53d6\u5730\u5740\u2014\u2014\u4f1a\u5728 9.6 - \u6307\u9488\u7b80\u4ecb \u4e2d\u8fdb\u884c\u4ecb\u7ecd\uff09\u5219\u79f0\u5176\u5177\u6709\u8eab\u4efd\u7279\u5f81\u3002\u5177\u6709\u6807\u8bc6\u7684\u5bf9\u8c61\u751f\u547d\u5468\u671f\u4f1a\u8d85\u8fc7\u8868\u8fbe\u5f0f\u672c\u8eab\u3002 1 2 3 4 5 6 7 8 9 10 #include int main () { int x {}; std :: cout << x << '\\n' ; // x is an lvalue expression return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u8868\u8fbe\u5f0f x \u662f\u4e00\u4e2a\u5de6\u503c\u8868\u8fbe\u5f0f\uff0c\u56e0\u4e3a\u5b83\u6c42\u503c\u5f97\u5230\u53d8\u91cf x \uff08\u5b83\u5177\u6709\u6807\u8bc6\u7b26\uff09\u3002 \u56e0\u4e3a\u8bed\u8a00\u5f15\u5165\u4e86\u5e38\u91cf\uff0c\u5de6\u503c\u73b0\u5728\u6709\u4e24\u4e2a\u5b50\u7c7b\u578b\uff1a\u53ef\u53d8\u5de6\u503c\u2014\u2014\u5b83\u7684\u503c\u662f\u53ef\u4ee5\u6539\u53d8\u7684\u548c\u4e0d\u53ef\u53d8\u5de6\u503c\u2014\u2014\u5b83\u7684\u503c\u4e0d\u53ef\u4ee5\u88ab\u4fee\u6539\uff08\u56e0\u4e3a\u8be5\u5de6\u503c\u662f const \u6216 constexpr\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int x {}; const double d {}; std :: cout << x << '\\n' ; // x is a modifiable lvalue expression std :: cout << d << '\\n' ; // d is a non-modifiable lvalue expression return 0 ; } rvalue (\u662f\u201cright value\u201d\u7684\u7b80\u79f0\uff0c\u6709\u65f6\u5019\u4e5f\u5199\u4f5c r-value ) \u662f\u4e00\u79cd\u8868\u8fbe\u5f0f\uff0c\u9664\u4e86\u5de6\u503c\u4e4b\u5916\u7684\u90fd\u662f\u53f3\u503c\u3002\u5e38\u89c1\u7684\u53f3\u503c\u5305\u62ec \u5b57\u9762\u91cf \uff08\u9664\u4e86\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff0c\u5b83\u662f\u5de6\u503c\uff09\u548c\u51fd\u6570\u6216\u64cd\u4f5c\u7b26\u7684\u8fd4\u56de\u503c\u3002 \u53f3\u503c \u5176\u4f5c\u7528\u57df\u53ea\u5b58\u5728\u4e8e\u4f7f\u7528\u5b83\u7684\u8868\u8fbe\u5f0f\u4e2d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int return5 () { return 5 ; } int main () { int x { 5 }; // 5 is an rvalue expression const double d { 1.2 }; // 1.2 is an rvalue expression std :: cout << x << '\\n' ; // x is a modifiable lvalue expression std :: cout << d << '\\n' ; // d is a non-modifiable lvalue expression std :: cout << return5 (); // return5() is an rvalue expression (since the result is returned by value) std :: cout << x + 1 << '\\n' ; // x + 1 is a rvalue std :: cout << static_cast < int > ( d ) << '\\n' ; // the result of static casting d to an int is an rvalue return 0 ; } \u4f60\u53ef\u80fd\u4f1a\u597d\u5947\u4e3a\u4ec0\u4e48 return5() \u548c x + 1 \u5c5e\u4e8e\u53f3\u503c\uff1a\u95ee\u9898\u7684\u7b54\u6848\u5728\u4e8e\u8fd9\u4e9b\u8868\u8fbe\u5f0f\u7684\u503c\u5728\u4ea7\u751f\u540e\u5fc5\u987b\u9a6c\u4e0a\u4f7f\u7528\uff08\u5728\u8868\u8fbe\u5f0f\u7684\u4f5c\u7528\u57df\u5185\uff09\u6216\u88ab\u4e22\u5f03\u3002 \u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u56de\u7b54\u524d\u9762\u7684\u95ee\u9898\u4e86\uff0c\u4e3a\u4ec0\u4e48 x = 5 \u662f\u5408\u6cd5\u7684\u4f46 5 = x \u5219\u662f\u4e0d\u5408\u6cd5\uff1a\u8d4b\u503c\u8fd0\u7b97\u7b26\u8981\u6c42\u5176\u5de6\u64cd\u4f5c\u6570\u662f\u4e00\u4e2a\u53ef\u4fee\u6539\u7684\u5de6\u503c\u8868\u8fbe\u5f0f\uff0c\u4e14\u53f3\u64cd\u4f5c\u6570\u4e3a\u4e00\u4e2a\u53f3\u503c\u8868\u8fbe\u5f0f\u3002\u8d4b\u503c\u64cd\u4f5c 5 = x \u5931\u8d25\u662f\u56e0\u4e3a 5 \u5e76\u4e0d\u662f\u4e00\u4e2a\u5de6\u503c\u3002 1 2 3 4 5 6 7 8 9 10 int main () { int x {}; // Assignment requires the left operand to be a modifiable lvalue expression and the right operand to be an rvalue expression x = 5 ; // valid: x is a modifiable lvalue expression and 5 is an rvalue expression 5 = x ; // error: 5 is an rvalue expression and x is a modifiable lvalue expression return 0 ; } \u76f8\u5173\u5185\u5bb9 \u5de6\u503c\u548c\u53f3\u503c\u8868\u8fbe\u5f0f\u7684\u5b8c\u6574\u5217\u8868\u53c2\u8003 \u8fd9\u91cc \u3002\u5728C++11\u4e2d\uff0c\u53f3\u503c\u88ab\u5206\u6210\u4e86\u4e24\u4e2a\u5b50\u7c7b\u522b\uff1aprvalues \u548c xvalues\u3002\u6b64\u5904\u6211\u4eec\u8c08\u8bba\u7684 rvalues \u7efc\u5408\u4e86\u8fd9\u4e24\u79cd\u7c7b\u578b\u7684\u7279\u70b9\u3002","title":"Lvalue \u548c rvalue \u8868\u8fbe\u5f0f"},{"location":"9-2-Value-categories-lvalues-and-rvalues/#l-value-r-value","text":"\u5728\u4e0a\u9762\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u4e2d\u6211\u4eec\u63d0\u5230\uff0c\u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u53f3\u4fa7\u8fd0\u7b97\u6570\u5fc5\u987b\u662f\u4e00\u4e2a\u53f3\u503c\u8868\u8fbe\u5f0f\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u6b63\u786e\u8fd0\u884c\u5462\uff1f 1 2 3 4 5 6 7 8 9 int main () { int x { 1 }; int y { 2 }; x = y ; // y \u662f\u4e00\u4e2a\u53ef\u4ee5\u4fee\u6539\u7684\u5de6\u503c\u800c\u4e0d\u662f\u53f3\u503c\uff0c\u4e3a\u4ec0\u4e48\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u662f\u5408\u6cd5\u7684\u5462\uff1f return 0 ; } \u8fd9\u662f\u56e0\u4e3a\u5de6\u503c\u53ef\u4ee5\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u53f3\u503c\uff0c\u6240\u4ee5\u4efb\u4f55\u53ef\u4ee5\u4f7f\u7528\u53f3\u503c\u7684\u5730\u65b9\u90fd\u53ef\u4ee5\u4f7f\u7528\u5de6\u503c\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 int main () { int x { 2 }; x = x + 1 ; return 0 ; } \u5728\u8fd9\u4e2a\u8bed\u53e5\u4e2d\uff0c\u53d8\u91cfx\u7684\u4f7f\u7528\u5b58\u5728\u4e24\u4e2a\u4e0d\u540c\u7684\u8bed\u5883\u3002\u8d4b\u503c\u8fd0\u7b97\u7b26\u5de6\u4fa7\u7684x\u662f\u4e00\u4e2a\u5de6\u503c\u8868\u8fbe\u5f0f\uff0c\u5b83\u6c42\u503c\u5f97\u5230\u53d8\u91cf x\u3002\u8d4b\u503c\u8fd0\u7b97\u7b26\u53f3\u4fa7\u7684 x+1 \u5219\u662f\u4e00\u4e2a\u53f3\u503c\u8868\u8fbe\u5f0f\uff0c\u5b83\u6c42\u503c\u5f97\u52303\u3002 \u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u8be6\u7ec6\u5730\u4ecb\u7ecd\u4e86\u5de6\u503c\uff0c\u63a5\u4e0b\u6765\u5c31\u662f\u6211\u4eec\u8981\u63a5\u89e6\u5230\u7684\u7b2c\u4e00\u4e2a\u590d\u6742\u8868\u8fbe\u5f0f\u7c7b\u578b\uff1a \u5de6\u503c\u5f15\u7528 \u3002 \u5173\u952e\u4fe1\u606f \u8fa8\u522b\u5de6\u503c\u8868\u8fbe\u5f0f\u548c\u53f3\u503c\u8868\u8fbe\u5f0f\u7684\u5173\u952e\u6cd5\u5219\uff1a - \u5de6\u503c\u8868\u8fbe\u5f0f\u662f\u90a3\u4e9b\u6c42\u503c\u4e3a\u53d8\u91cf\u6216\u5176\u4ed6\u5177\u6709\u8eab\u4efd\u6807\u8bc6\u7684\u53d8\u91cf\uff0c\u4e14\u5b83\u4eec\u53ef\u4ee5\u6301\u7eed\u5230\u8868\u8fbe\u5f0f\u7ed3\u675f\u540e\u3002 - \u53f3\u503c\u8868\u8fbe\u5f0f\u5219\u662f\u90a3\u4e9b\u53ef\u4ee5\u6c42\u503c\u4e3a\u5b57\u9762\u91cf\u7684\u8868\u8fbe\u5f0f\uff0c\u6216\u8005\u662f\u51fd\u6570\u548c\u64cd\u4f5c\u7b26\u7684\u8fd4\u56de\u503c\uff0c\u5b83\u4eec\u4f1a\u5728\u8868\u8fbe\u5f0f\u89e3\u91ca\u540e\u88ab\u4e22\u5f03\u3002","title":"L-value \u548c r-value \u7684\u8f6c\u6362"},{"location":"9-3-Lvalue-references/","tags":["Lvalue"],"text":"Key Takeaway \u5f15\u7528\u662f\u5bf9\u8c61\u7684\u522b\u540d\uff0c\u5728\u521b\u5efa\u65f6\u8fdb\u884c\u7ed1\u5b9a\uff0c\u7136\u540e\u5c31\u4e0d\u53ef\u4ee5\u88ab\u4fee\u6539 \u5f15\u7528\u6709\u5de6\u503c\u5f15\u7528\u548c\u53f3\u503c\u5f15\u7528\u4e24\u79cd\u3002\u5de6\u503c\u5f15\u7528\u53c8\u5206\u4e3a\u53ef\u53d8\u5de6\u503c\u5f15\u7528\u548c\u4e0d\u53ef\u53d8\u5de6\u503c\u5f15\u7528 \u5f15\u7528\u548c\u88ab\u5f15\u7528\u5bf9\u8c61\u5177\u6709\u72ec\u7acb\u7684\u751f\u547d\u5468\u671f \u5f15\u7528\u4e0d\u662f\u5bf9\u8c61\uff0c\u6240\u4ee5\u4e0d\u5360\u7528\u5b58\u50a8\u7a7a\u95f4\uff0c\u5f53\u7136\u4e5f\u4e0d\u80fd\u88ab\u7528\u5728\u6240\u6709\u5141\u8bb8\u4f7f\u7528\u5bf9\u8c61\u7684\u5730\u65b9\u3002 std::reference_wrapper \u53ef\u63d0\u4f9b\u5141\u8bb8\u518d\u6b21\u7ed1\u5b9a\u7684\u5f15\u7528 \u5728 C++ \u4e2d\uff0c\u5f15\u7528\u5176\u5b9e\u5c31\u662f\u67d0\u4e2a\u5df2\u5b58\u5728\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u4e00\u65e6\u5f15\u7528\u88ab\u5b9a\u4e49\uff0c\u4efb\u4f55\u5bf9\u5f15\u7528\u7684\u64cd\u4f5c\u90fd\u4f1a\u76f4\u63a5\u5e94\u7528\u4e8e\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u3002 \u5173\u952e\u4fe1\u606f \u5f15\u7528\u5728\u672c\u8d28\u4e0a\u548c\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u662f\u4e00\u56de\u4e8b\u3002 \u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u6765\u8bfb\u53d6\u6216\u4fee\u6539\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u3002\u5c3d\u7ba1\u73b0\u5728\u5f15\u7528\u770b\u4e0a\u53bb\u6709\u70b9\u50bb\uff0c\u6ca1\u5565\u7528\uff0c\u751a\u81f3\u8fd8\u5f88\u591a\u4f59\uff0c\u4f46\u5b9e\u9645\u4e0a\u5728C++\u4e2d\u6211\u4eec\u4f1a\u5927\u91cf\u5730\u4f7f\u7528\u5f15\u7528\u3002 \u4f60\u4e5f\u53ef\u4ee5\u521b\u5efa\u5bf9\u51fd\u6570\u7684\u5f15\u7528\uff0c\u5c3d\u7ba1\u8fd9\u4e2a\u505a\u6cd5\u5e76\u4e0d\u5e38\u89c1\u3002 \u73b0\u4ee3C++\u5b58\u5728\u4e24\u79cd\u7c7b\u578b\u7684\u5f15\u7528\uff1a \u5de6\u503c\u5f15\u7528 \u548c \u53f3\u503c\u5f15\u7528 \u3002\u5728\u672c\u7ae0\u4e2d\u6211\u4eec\u4f1a\u9996\u5148\u8ba8\u8bba\u5de6\u503c\u5f15\u7528\u3002 \u76f8\u5173\u5185\u5bb9 \u56e0\u4e3a\u6211\u4eec\u5728\u672c\u7ae0\u4f1a\u8ba8\u8bba\u5de6\u503c\u548c\u53f3\u503c\uff0c\u5982\u679c\u4f60\u9700\u8981\u590d\u4e60\u4e00\u4e0b\u7684\u8bdd\uff0c\u8bf7\u53c2\u8003 9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09 \u3002 \u53f3\u503c\u5f15\u7528\u4f1a\u5728 \u79fb\u52a8\u8bed\u4e49 \u7ae0\u8282\u8fdb\u884c\u4ecb\u7ecd ( chapter M ). \u5de6\u503c\u5f15\u7528\u7c7b\u578b \u5de6\u503c\u5f15\u7528\uff08\u901a\u5e38\u7b80\u79f0\u4e3a\u5f15\u7528\u56e0\u4e3aC++11\u4e4b\u524d\u53ea\u6709\u8fd9\u4e00\u79cd\u7c7b\u578b\u7684\u5f15\u7528\uff09\u662f\u4e00\u4e2a\u5df2\u5b58\u5728\u5de6\u503c\uff08\u4f8b\u5982\u67d0\u4e2a\u53d8\u91cf\uff09\u7684\u522b\u540d\u3002 \u58f0\u660e\u5de6\u503c\u7c7b\u578b\u53ef\u4ee5\u5728\u7c7b\u578b\u58f0\u660e\u65f6\u4f7f\u7528 & \u7b26\u53f7\uff1a 1 2 3 int // a normal int type int & // an lvalue reference to an int object double & // an lvalue reference to a double object \u5de6\u503c\u5f15\u7528\u53d8\u91cf \u5de6\u503c\u5f15\u7528\u7c7b\u578b\u53ef\u4ee5\u7528\u6765\u58f0\u660e\u4e00\u4e2a\u5de6\u503c\u5f15\u7528\u53d8\u91cf\u3002\u5de6\u503c\u5f15\u7528\u53d8\u91cf\u4f5c\u4e3a\u5bf9\u4e00\u4e2a\u5de6\u503c\u7684\u5f15\u7528\u6765\u4f7f\u7528\uff08\u901a\u5e38\u662f\u53e6\u5916\u4e00\u4e2a\u53d8\u91cf\uff09\u3002 \u521b\u5efa\u5de6\u503c\u5f15\u7528\u53d8\u91cf\uff0c\u53ea\u9700\u8981\u4f7f\u7528\u5de6\u503c\u5f15\u7528\u7c7b\u578b\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u5373\u53ef\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { int x { 5 }; // x is a normal integer variable int & ref { x }; // ref is an lvalue reference variable that can now be used as an alias for variable x std :: cout << x << '\\n' ; // print the value of x (5) std :: cout << ref << '\\n' ; // print the value of x via ref (5) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528 int& \u5b9a\u4e49 ref \u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5bf9 int \u7c7b\u578b\u5de6\u503c\u7684\u5f15\u7528\uff0c\u7136\u540e\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u5de6\u503c\u8868\u8fbe\u5f0f x \u5bf9\u5176\u8fdb\u884c\u4e86\u521d\u59cb\u5316\u3002\u4ece\u6b64\u4ee5\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06 ref \u4f5c\u4e3a x \u7684\u540c\u4e49\u8bcd\u6765\u4f7f\u7528\u3002\u7a0b\u5e8f\u4f1a\u6253\u5370\u5982\u4e0b\u5185\u5bb9\uff1a 1 2 5 5 \u4ece\u7f16\u8bd1\u5668\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u5c06 & \u653e\u5728\u7c7b\u578b\u65c1\u8fb9( int& ref )\u8fd8\u662f\u53d8\u91cf\u65c1\u8fb9( int &ref )\u5e76\u6ca1\u6709\u4ec0\u4e48\u533a\u522b\uff0c\u9009\u62e9\u90a3\u79cd\u5199\u6cd5\u53ea\u5173\u4e4e\u4e8e\u98ce\u683c\u3002\u73b0\u4ee3C++\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u9009\u62e9\u5c06 & \u7d27\u6328\u7740\u7c7b\u578b\u5173\u952e\u5b57\u653e\u7f6e\uff0c\u56e0\u4e3a\u8fd9\u6837\u505a\u53ef\u4ee5\u66f4\u6e05\u695a\u5730\u8868\u660e\u5f15\u7528\u662f\u7c7b\u578b\u4fe1\u606f\u7684\u4e00\u90e8\u5206\uff0c\u800c\u4e0d\u662f\u6807\u8bc6\u7b26\u7684\u4e00\u90e8\u5206\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u5b9a\u4e49\u5f15\u7528\u7684\u65f6\u5019\uff0c\u5c06 & \u7d27\u6328\u7740\u7c7b\u578b\u5173\u952e\u8bcd\u653e\u7f6e\uff08\u800c\u4e0d\u662f\u53d8\u91cf\u540d\uff09\u3002 \u6269\u5c55\u9605\u8bfb \u5bf9\u4e8e\u90a3\u4e9b\u5df2\u7ecf\u719f\u6089\u6307\u9488\u7684\u5f00\u53d1\u8005\u6765\u8bf4\uff0c & \u5728\u8fd9\u91cc\u542b\u4e49\u5e76\u4e0d\u662f\u53d6\u5730\u5740\u800c\u662f\u5de6\u503c\u5f15\u7528\u3002 \u901a\u8fc7\u5de6\u503c\u5f15\u7528\u6765\u4fee\u6539\u503c \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u901a\u8fc7\u5f15\u7528\u6765\u83b7\u53d6\u88ab\u5f15\u7528\u5bf9\u8c61\u7684\u503c\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u6765\u4fee\u6539\u88ab\u5f15\u7528\u5bf9\u8c61\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { int x { 5 }; // normal integer variable int & ref { x }; // ref is now an alias for variable x std :: cout << x << ref ; // print 55 x = 6 ; // x now has value 6 std :: cout << x << ref ; // prints 66 ref = 7 ; // the object being referenced (x) now has value 7 std :: cout << x << ref ; // prints 77 return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 556677 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c ref \u662f x \u7684\u522b\u540d\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 x \u6216\u8005 ref \u6765\u4fee\u6539 x \u7684\u503c\u3002 \u5de6\u503c\u5f15\u7528\u7684\u521d\u59cb\u5316 \u548c\u5e38\u91cf\u975e\u5e38\u7c7b\u4f3c\uff0c\u6240\u6709\u7684\u5f15\u7528\u90fd\u5fc5\u987b\u88ab\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 int main () { int & invalidRef ; // error: references must be initialized int x { 5 }; int & ref { x }; // okay: reference to int is bound to int variable return 0 ; } \u5f53\u4f7f\u7528\u4e00\u4e2a\u5bf9\u8c61(\u6216\u51fd\u6570)\u521d\u59cb\u5316\u5f15\u7528\u65f6\uff0c\u6211\u4eec\u8bf4\u5b83\u88ab\u7ed1\u5b9a\u5230\u4e86\u90a3\u4e2a\u5bf9\u8c61(\u6216\u51fd\u6570)\u3002\u7ed1\u5b9a\u6b64\u7c7b\u5f15\u7528\u7684\u8fc7\u7a0b\u79f0\u4e3a\u5f15\u7528\u7ed1\u5b9a\u3002\u88ab\u5f15\u7528\u7684\u5bf9\u8c61(\u6216\u51fd\u6570)\u6709\u65f6\u79f0\u4e3a referent\u3002 \u4f5c\u6307\u5f15\u4e0e\u5fc5\u987b\u88ab\u7ed1\u5b9a\u5230\u4e00\u4e2a\u53ef\u4fee\u6539\u5de6\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 int main () { int x { 5 }; int & ref { x }; // valid: lvalue reference bound to a modifiable lvalue const int y { 5 }; int & invalidRef { y }; // invalid: can't bind to a non-modifiable lvalue int & invalidRef2 { 0 }; // invalid: can't bind to an r-value return 0 ; } \u5de6\u503c\u5f15\u7528\u4e0d\u80fd\u88ab\u7ed1\u5b9a\u5230\u4e00\u4e2a\u4e0d\u53ef\u4fee\u6539\u7684\u5de6\u503c\uff08\u5426\u5219\u4f60\u5c82\u4e0d\u662f\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u6765\u4fee\u6539\u8fd9\u4e2a\u503c\u54af\uff0c\u8fd9\u663e\u7136\u8fdd\u80cc\u4e86\u5e38\u91cf\u7684\u5e38\u91cf\u6027\uff09\u3002\u56e0\u6b64\uff0c\u5de6\u503c\u5f15\u7528\u6709\u65f6\u4e5f\u79f0\u4e3a\u975econst\u53d8\u91cf\u7684\u5de6\u503c\u5f15\u7528\uff08\u7b80\u79f0\u4e3a\u975econst\u53d8\u91cf\u7684\u5f15\u7528\uff09\u3002 \u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5de6\u503c\u5f15\u7528\u7684\u7c7b\u578b\u5fc5\u987b\u548c\u88ab\u5f15\u7528\u5bf9\u8c61\u7684\u7c7b\u578b\u76f8\u5339\u914d\uff08\u4f8b\u5916\u7684\u60c5\u51b5\u4f1a\u5728\u6211\u4eec\u8ba8\u8bba\u7ee7\u627f\u65f6\u9047\u5230\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 int main () { int x { 5 }; int & ref { x }; // okay: reference to int is bound to int variable double y { 6.0 }; int & invalidRef { y }; // invalid; reference to int cannot bind to double variable double & invalidRef2 { x }; // invalid: reference to double cannot bind to int variable return 0 ; } \u5bf9 void \u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528\u662f\u4e0d\u53ef\u4ee5\u7684\uff08\u8fd9\u4e48\u505a\u7684\u76ee\u7684\u53c8\u662f\u4ec0\u4e48\u5462\uff1f\uff09 \u5f15\u7528\u4e0d\u80fd\u88ab\u91cd\u65b0\u8bbe\u7f6e(\u4f7f\u5176\u5f15\u7528\u5176\u4ed6\u5bf9\u8c61) \u5f15\u7528\u4e00\u65e6\u88ab\u521d\u59cb\u5316\uff0c\u5b83\u4fbf\u4e0d\u80fd\u591f\u88ab\u91cd\u65b0\u7ed1\u5b9a\u5230\u5176\u4ed6\u5bf9\u8c61\u3002 \u65b0\u624b C++ \u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u5c1d\u8bd5\u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\u91cd\u65b0\u8bbe\u7f6e\u5f15\u7528\u3002\u8fd9\u4e48\u505a\u662f\u53ef\u4ee5\u7f16\u8bd1\u5e76\u8fd0\u884c\u7684\uff0c\u4f46\u662f\u5b83\u7684\u6548\u679c\u5e76\u4e0d\u662f\u6211\u4eec\u671f\u671b\u7684\u90a3\u6837\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { int x { 5 }; int y { 6 }; int & ref { x }; // ref is now an alias for x ref = y ; // assigns 6 (the value of y) to x (the object being referenced by ref) // The above line does NOT change ref into a reference to variable y! std :: cout << x ; // user is expecting this to print 5 return 0 ; } \u7ed3\u679c\u53ef\u80fd\u51fa\u4eba\u610f\u6599\uff1a 1 6 \u5f53\u8868\u8fbe\u5f0f\u4e2d\u7684\u5f15\u7528\u8fdb\u884c\u6c42\u503c\u65f6\uff0c\u5b83\u4f1a\u89e3\u6790\u6210\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u3002\u6240\u4ee5 ref = y \u5e76\u4e0d\u4f1a\u4e3a ref \u91cd\u65b0\u8bbe\u7f6e\u4e3a y \u7684\u5f15\u7528\u3002\u5b9e\u9645\u60c5\u51b5\u662f ref \u662f x \u7684\u5f15\u7528\uff0c\u56e0\u6b64\u4e0a\u9762\u7684\u8868\u8fbe\u5f0f\u7b49\u4ef7\u4e8e x = y \uff0c\u56e0\u4e3a y \u7684\u503c\u4e3a 6 \uff0c\u6240\u4ee5 x \u88ab\u8d4b\u503c\u4e3a 6 \u3002 \u5de6\u503c\u5f15\u7528\u7684\u4f5c\u7528\u57df\u548c\u6301\u7eed\u65f6\u95f4 \u5f15\u7528\u53d8\u91cf\u7684 \u4f5c\u7528\u57df \u548c \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u6240\u9075\u5faa\u7684\u89c4\u5219\u548c\u4e00\u822c\u53d8\u91cf\u662f\u4e00\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { int x { 5 }; // normal integer int & ref { x }; // reference to variable value return 0 ; } // x and ref die here \u5f15\u7528\u548c\u88ab\u5f15\u7528\u5bf9\u8c61\u5177\u6709\u72ec\u7acb\u7684\u751f\u547d\u5468\u671f \u9664\u4e86\u67d0\u79cd\u4f8b\u5916\u60c5\u51b5\uff08\u4e0b\u4e00\u8282\u4f1a\u4ecb\u7ecd\uff09\u4e4b\u5916\uff0c \u5f15\u7528\u548c\u88ab\u5f15\u7528\u5bf9\u8c61\u5177\u6709\u72ec\u7acb\u7684\u751f\u547d\u5468\u671f \u3002\u6362\u8a00\u4e4b\uff0c\u4e0b\u9762\u4e24\u6761\u662f\u6b63\u786e\u7684\uff1a \u88ab\u5f15\u7528\u5bf9\u8c61\u53ef\u4ee5\u5148\u4e8e\u5f15\u7528\u9500\u6bc1\uff1b \u5f15\u7528\u53ef\u4ee5\u5148\u4e8e\u88ab\u5f15\u7528\u5bf9\u8c61\u9500\u6bc1\u3002 \u5982\u679c\u5f15\u7528\u5148\u4e8e\u88ab\u5f15\u7528\u5bf9\u8c61\u9500\u6bc1\uff0c\u5b8c\u5168\u4e0d\u4f1a\u5f71\u54cd\u5230\u88ab\u5f15\u7528\u5bf9\u8c61\uff0c\u4e0b\u9762\u7a0b\u5e8f\u53ef\u4ee5\u8868\u660e\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int x { 5 }; { int & ref { x }; // ref is a reference to x std :: cout << ref ; // prints value of ref (5) } // ref is destroyed here -- x is unaware of this std :: cout << x ; // prints value of x (5) return 0 ; } // x destroyed here \u6253\u5370\u7ed3\u679c\uff1a 1 55 \u5f53 ref \u88ab\u9500\u6bc1\u540e\uff0c\u53d8\u91cf x \u4e00\u5207\u5982\u6545\u3002 \u5168\u7136\u4e0d\u77e5\u6253\u5b83\u7684\u5f15\u7528\u5df2\u7ecf\u88ab\u9500\u6bc1\u4e86\u3002 \u60ac\u5782\u5f15\u7528 \u5f53\u4e00\u4e2a\u5bf9\u8c61\u5148\u4e8e\u5b83\u7684\u5f15\u7528\u88ab\u9500\u6bc1\uff0c\u5219\u5176\u5f15\u7528\u6b64\u65f6\u4f1a\u7ed1\u5b9a\u5230\u4e00\u4e2a\u5df2\u7ecf\u4e0d\u5b58\u5728\u7684\u5bf9\u8c61\u3002\u6b64\u65f6\u8be5\u5f15\u7528\u4f1a\u6210\u4e3a\u4e00\u4e2a \u60ac\u5782 \u5f15\u7528\u3002\u8bbf\u95ee\u60ac\u5782\u5f15\u7528\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u60ac\u5782\u6307\u9488\u5176\u5b9e\u5f88\u5bb9\u6613\u907f\u514d\uff0c\u4f46\u662f\u6211\u4eec\u4f1a\u5728 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 \u4e2d\u50cf\u4f60\u5c55\u793a\u4e00\u4e2a\u7531\u60ac\u5782\u6307\u9488\u5f15\u53d1\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u6848\u4f8b\u3002 \u5f15\u7528\u4e0d\u662f\u5bf9\u8c61 \u4e5f\u8bb8\u51fa\u4e4e\u4f60\u7684\u610f\u6599\uff0c\u5728C++\u4e2d\u5f15\u7528\u5e76\u4e0d\u662f\u4e00\u4e2a\u5bf9\u8c61\u3002 \u5f15\u7528\u5e76\u4e0d\u9700\u8981\u5b58\u5728\u6216\u5360\u6709\u5b58\u50a8\u7a7a\u95f4 \u3002\u5982\u679c\u53ef\u80fd\u7684\u8bdd\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u4f18\u5316\u5f15\u7528\uff0c\u4f7f\u7528\u88ab\u5f15\u7528\u5bf9\u8c61\u5bf9\u5176\u8fdb\u884c\u66ff\u6362\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u5e76\u603b\u662f\u53ef\u884c\u7684\uff0c\u6b64\u65f6\u5f15\u7528\u4fbf\u9700\u8981\u5b58\u50a8\u7a7a\u95f4\u3002 \u8fd9\u4e5f\u610f\u5473\u7740\u201c\u5f15\u7528\u53d8\u91cf\u201d\u8fd9\u4e2a\u672f\u8bed\u6709\u4e9b\u7528\u8bcd\u4e0d\u5f53\uff0c\u56e0\u4e3a\u53d8\u91cf\u662f\u5bf9\u8c61\u7684\u540d\u5b57\uff0c\u800c\u5f15\u7528\u5e76\u4e0d\u662f\u5bf9\u8c61\u3002 \u56e0\u4e3a==\u5f15\u7528\u5e76\u4e0d\u662f\u5bf9\u8c61\uff0c\u6240\u4ee5\u4f60\u5e76\u4e0d\u80fd\u5728\u4efb\u4f55\u80fd\u4f7f\u7528\u5bf9\u8c61\u7684\u5730\u65b9\u6765\u4f7f\u7528\u5b83\uff08\u4f8b\u5982\uff0c\u4f60\u4e0d\u80fd\u521b\u5efa\u5f15\u7528\u7684\u5f15\u7528\uff0c\u56e0\u4e3a\u4f5c\u6307\u5f15\u4e0e\u5fc5\u987b\u5f15\u7528\u4e00\u4e2a\u6709\u8eab\u4efd\u6807\u8bc6\u7684\u5bf9\u8c61\uff09==\u3002\u4e07\u4e00\u4f60\u9700\u8981\u4e00\u4e2a\u652f\u6301\u91cd\u65b0\u7ed1\u5b9a\u7684\u5bf9\u8c61\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 std::reference_wrapper (\u5728 16.3 -- Aggregation \u4e2d\u4ecb\u7ecd)\u3002 \u9898\u5916\u8bdd \u8003\u8651\u4e0b\u5217\u53d8\u91cf\uff1a 1 2 3 int var {}; int & ref1 { var }; // an lvalue reference bound to var int & ref2 { ref1 }; // an lvalue reference bound to var \u56e0\u4e3a ref2 (\u662f\u4e00\u4e2a\u5f15\u7528) \u88ab\u521d\u59cb\u5316\u4e3a ref1 (\u4e5f\u662f\u4e00\u4e2a\u5f15\u7528)\uff0c\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a ref2 \u662f\u4e00\u4e2a\u5f15\u7528\u7684\u5f15\u7528\u3002\u4f46\u5176\u5b9e\u5b83\u4e0d\u662f\uff0c\u56e0\u4e3a ref1 \u662f var \u7684\u5f15\u7528\uff0c\u5f53\u6211\u4eec\u5728\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u5b83\u7684\u7684\u65f6\u5019\uff08\u4f5c\u4e3a\u521d\u59cb\u5316\u503c\uff09\uff0c ref1 \u4f1a\u6c42\u503c\u5f97\u5230 var \u3002\u6240\u4ee5 ref2 \u5176\u5b9e\u53ea\u662f\u4e00\u4e2a\u666e\u901a\u7684 \u5de6\u503c\u5f15\u7528 \uff08\u6b63\u5982\u5b83\u7684\u7c7b\u578b\u6240\u8868\u660e\u7684\u90a3\u6837\uff09\uff0c\u5b83\u4e5f\u4e0e var \u7ed1\u5b9a\u3002 \u5f15\u7528\u7684\u5f15\u7528\u672c\u6765\u53ef\u4ee5\u4f7f\u7528\u8bed\u6cd5 int&& \u7684\uff0c\u53ef\u60dcC++\u5e76\u4e0d\u652f\u6301\u5f15\u7528\u7684\u5f15\u7528\uff0c\u56e0\u6b64\u8fd9\u4e2a\u8bed\u6cd5\u5f62\u5f0f\u5728 C++11\u4e2d\u88ab\u7528\u6765\u8868\u793a \u53f3\u503c\u5f15\u7528 \uff09\u3002","title":"9.3 - \u5de6\u503c\u5f15\u7528"},{"location":"9-3-Lvalue-references/#_1","text":"\u5de6\u503c\u5f15\u7528\uff08\u901a\u5e38\u7b80\u79f0\u4e3a\u5f15\u7528\u56e0\u4e3aC++11\u4e4b\u524d\u53ea\u6709\u8fd9\u4e00\u79cd\u7c7b\u578b\u7684\u5f15\u7528\uff09\u662f\u4e00\u4e2a\u5df2\u5b58\u5728\u5de6\u503c\uff08\u4f8b\u5982\u67d0\u4e2a\u53d8\u91cf\uff09\u7684\u522b\u540d\u3002 \u58f0\u660e\u5de6\u503c\u7c7b\u578b\u53ef\u4ee5\u5728\u7c7b\u578b\u58f0\u660e\u65f6\u4f7f\u7528 & \u7b26\u53f7\uff1a 1 2 3 int // a normal int type int & // an lvalue reference to an int object double & // an lvalue reference to a double object","title":"\u5de6\u503c\u5f15\u7528\u7c7b\u578b"},{"location":"9-3-Lvalue-references/#_2","text":"\u5de6\u503c\u5f15\u7528\u7c7b\u578b\u53ef\u4ee5\u7528\u6765\u58f0\u660e\u4e00\u4e2a\u5de6\u503c\u5f15\u7528\u53d8\u91cf\u3002\u5de6\u503c\u5f15\u7528\u53d8\u91cf\u4f5c\u4e3a\u5bf9\u4e00\u4e2a\u5de6\u503c\u7684\u5f15\u7528\u6765\u4f7f\u7528\uff08\u901a\u5e38\u662f\u53e6\u5916\u4e00\u4e2a\u53d8\u91cf\uff09\u3002 \u521b\u5efa\u5de6\u503c\u5f15\u7528\u53d8\u91cf\uff0c\u53ea\u9700\u8981\u4f7f\u7528\u5de6\u503c\u5f15\u7528\u7c7b\u578b\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u5373\u53ef\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { int x { 5 }; // x is a normal integer variable int & ref { x }; // ref is an lvalue reference variable that can now be used as an alias for variable x std :: cout << x << '\\n' ; // print the value of x (5) std :: cout << ref << '\\n' ; // print the value of x via ref (5) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528 int& \u5b9a\u4e49 ref \u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5bf9 int \u7c7b\u578b\u5de6\u503c\u7684\u5f15\u7528\uff0c\u7136\u540e\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u5de6\u503c\u8868\u8fbe\u5f0f x \u5bf9\u5176\u8fdb\u884c\u4e86\u521d\u59cb\u5316\u3002\u4ece\u6b64\u4ee5\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06 ref \u4f5c\u4e3a x \u7684\u540c\u4e49\u8bcd\u6765\u4f7f\u7528\u3002\u7a0b\u5e8f\u4f1a\u6253\u5370\u5982\u4e0b\u5185\u5bb9\uff1a 1 2 5 5 \u4ece\u7f16\u8bd1\u5668\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u5c06 & \u653e\u5728\u7c7b\u578b\u65c1\u8fb9( int& ref )\u8fd8\u662f\u53d8\u91cf\u65c1\u8fb9( int &ref )\u5e76\u6ca1\u6709\u4ec0\u4e48\u533a\u522b\uff0c\u9009\u62e9\u90a3\u79cd\u5199\u6cd5\u53ea\u5173\u4e4e\u4e8e\u98ce\u683c\u3002\u73b0\u4ee3C++\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u9009\u62e9\u5c06 & \u7d27\u6328\u7740\u7c7b\u578b\u5173\u952e\u5b57\u653e\u7f6e\uff0c\u56e0\u4e3a\u8fd9\u6837\u505a\u53ef\u4ee5\u66f4\u6e05\u695a\u5730\u8868\u660e\u5f15\u7528\u662f\u7c7b\u578b\u4fe1\u606f\u7684\u4e00\u90e8\u5206\uff0c\u800c\u4e0d\u662f\u6807\u8bc6\u7b26\u7684\u4e00\u90e8\u5206\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u5b9a\u4e49\u5f15\u7528\u7684\u65f6\u5019\uff0c\u5c06 & \u7d27\u6328\u7740\u7c7b\u578b\u5173\u952e\u8bcd\u653e\u7f6e\uff08\u800c\u4e0d\u662f\u53d8\u91cf\u540d\uff09\u3002 \u6269\u5c55\u9605\u8bfb \u5bf9\u4e8e\u90a3\u4e9b\u5df2\u7ecf\u719f\u6089\u6307\u9488\u7684\u5f00\u53d1\u8005\u6765\u8bf4\uff0c & \u5728\u8fd9\u91cc\u542b\u4e49\u5e76\u4e0d\u662f\u53d6\u5730\u5740\u800c\u662f\u5de6\u503c\u5f15\u7528\u3002","title":"\u5de6\u503c\u5f15\u7528\u53d8\u91cf"},{"location":"9-3-Lvalue-references/#_3","text":"\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u901a\u8fc7\u5f15\u7528\u6765\u83b7\u53d6\u88ab\u5f15\u7528\u5bf9\u8c61\u7684\u503c\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u6765\u4fee\u6539\u88ab\u5f15\u7528\u5bf9\u8c61\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { int x { 5 }; // normal integer variable int & ref { x }; // ref is now an alias for variable x std :: cout << x << ref ; // print 55 x = 6 ; // x now has value 6 std :: cout << x << ref ; // prints 66 ref = 7 ; // the object being referenced (x) now has value 7 std :: cout << x << ref ; // prints 77 return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 556677 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c ref \u662f x \u7684\u522b\u540d\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 x \u6216\u8005 ref \u6765\u4fee\u6539 x \u7684\u503c\u3002","title":"\u901a\u8fc7\u5de6\u503c\u5f15\u7528\u6765\u4fee\u6539\u503c"},{"location":"9-3-Lvalue-references/#_4","text":"\u548c\u5e38\u91cf\u975e\u5e38\u7c7b\u4f3c\uff0c\u6240\u6709\u7684\u5f15\u7528\u90fd\u5fc5\u987b\u88ab\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 int main () { int & invalidRef ; // error: references must be initialized int x { 5 }; int & ref { x }; // okay: reference to int is bound to int variable return 0 ; } \u5f53\u4f7f\u7528\u4e00\u4e2a\u5bf9\u8c61(\u6216\u51fd\u6570)\u521d\u59cb\u5316\u5f15\u7528\u65f6\uff0c\u6211\u4eec\u8bf4\u5b83\u88ab\u7ed1\u5b9a\u5230\u4e86\u90a3\u4e2a\u5bf9\u8c61(\u6216\u51fd\u6570)\u3002\u7ed1\u5b9a\u6b64\u7c7b\u5f15\u7528\u7684\u8fc7\u7a0b\u79f0\u4e3a\u5f15\u7528\u7ed1\u5b9a\u3002\u88ab\u5f15\u7528\u7684\u5bf9\u8c61(\u6216\u51fd\u6570)\u6709\u65f6\u79f0\u4e3a referent\u3002 \u4f5c\u6307\u5f15\u4e0e\u5fc5\u987b\u88ab\u7ed1\u5b9a\u5230\u4e00\u4e2a\u53ef\u4fee\u6539\u5de6\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 int main () { int x { 5 }; int & ref { x }; // valid: lvalue reference bound to a modifiable lvalue const int y { 5 }; int & invalidRef { y }; // invalid: can't bind to a non-modifiable lvalue int & invalidRef2 { 0 }; // invalid: can't bind to an r-value return 0 ; } \u5de6\u503c\u5f15\u7528\u4e0d\u80fd\u88ab\u7ed1\u5b9a\u5230\u4e00\u4e2a\u4e0d\u53ef\u4fee\u6539\u7684\u5de6\u503c\uff08\u5426\u5219\u4f60\u5c82\u4e0d\u662f\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u6765\u4fee\u6539\u8fd9\u4e2a\u503c\u54af\uff0c\u8fd9\u663e\u7136\u8fdd\u80cc\u4e86\u5e38\u91cf\u7684\u5e38\u91cf\u6027\uff09\u3002\u56e0\u6b64\uff0c\u5de6\u503c\u5f15\u7528\u6709\u65f6\u4e5f\u79f0\u4e3a\u975econst\u53d8\u91cf\u7684\u5de6\u503c\u5f15\u7528\uff08\u7b80\u79f0\u4e3a\u975econst\u53d8\u91cf\u7684\u5f15\u7528\uff09\u3002 \u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5de6\u503c\u5f15\u7528\u7684\u7c7b\u578b\u5fc5\u987b\u548c\u88ab\u5f15\u7528\u5bf9\u8c61\u7684\u7c7b\u578b\u76f8\u5339\u914d\uff08\u4f8b\u5916\u7684\u60c5\u51b5\u4f1a\u5728\u6211\u4eec\u8ba8\u8bba\u7ee7\u627f\u65f6\u9047\u5230\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 int main () { int x { 5 }; int & ref { x }; // okay: reference to int is bound to int variable double y { 6.0 }; int & invalidRef { y }; // invalid; reference to int cannot bind to double variable double & invalidRef2 { x }; // invalid: reference to double cannot bind to int variable return 0 ; } \u5bf9 void \u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528\u662f\u4e0d\u53ef\u4ee5\u7684\uff08\u8fd9\u4e48\u505a\u7684\u76ee\u7684\u53c8\u662f\u4ec0\u4e48\u5462\uff1f\uff09","title":"\u5de6\u503c\u5f15\u7528\u7684\u521d\u59cb\u5316"},{"location":"9-3-Lvalue-references/#_5","text":"\u5f15\u7528\u4e00\u65e6\u88ab\u521d\u59cb\u5316\uff0c\u5b83\u4fbf\u4e0d\u80fd\u591f\u88ab\u91cd\u65b0\u7ed1\u5b9a\u5230\u5176\u4ed6\u5bf9\u8c61\u3002 \u65b0\u624b C++ \u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u5c1d\u8bd5\u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\u91cd\u65b0\u8bbe\u7f6e\u5f15\u7528\u3002\u8fd9\u4e48\u505a\u662f\u53ef\u4ee5\u7f16\u8bd1\u5e76\u8fd0\u884c\u7684\uff0c\u4f46\u662f\u5b83\u7684\u6548\u679c\u5e76\u4e0d\u662f\u6211\u4eec\u671f\u671b\u7684\u90a3\u6837\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { int x { 5 }; int y { 6 }; int & ref { x }; // ref is now an alias for x ref = y ; // assigns 6 (the value of y) to x (the object being referenced by ref) // The above line does NOT change ref into a reference to variable y! std :: cout << x ; // user is expecting this to print 5 return 0 ; } \u7ed3\u679c\u53ef\u80fd\u51fa\u4eba\u610f\u6599\uff1a 1 6 \u5f53\u8868\u8fbe\u5f0f\u4e2d\u7684\u5f15\u7528\u8fdb\u884c\u6c42\u503c\u65f6\uff0c\u5b83\u4f1a\u89e3\u6790\u6210\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u3002\u6240\u4ee5 ref = y \u5e76\u4e0d\u4f1a\u4e3a ref \u91cd\u65b0\u8bbe\u7f6e\u4e3a y \u7684\u5f15\u7528\u3002\u5b9e\u9645\u60c5\u51b5\u662f ref \u662f x \u7684\u5f15\u7528\uff0c\u56e0\u6b64\u4e0a\u9762\u7684\u8868\u8fbe\u5f0f\u7b49\u4ef7\u4e8e x = y \uff0c\u56e0\u4e3a y \u7684\u503c\u4e3a 6 \uff0c\u6240\u4ee5 x \u88ab\u8d4b\u503c\u4e3a 6 \u3002","title":"\u5f15\u7528\u4e0d\u80fd\u88ab\u91cd\u65b0\u8bbe\u7f6e(\u4f7f\u5176\u5f15\u7528\u5176\u4ed6\u5bf9\u8c61)"},{"location":"9-3-Lvalue-references/#_6","text":"\u5f15\u7528\u53d8\u91cf\u7684 \u4f5c\u7528\u57df \u548c \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u6240\u9075\u5faa\u7684\u89c4\u5219\u548c\u4e00\u822c\u53d8\u91cf\u662f\u4e00\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { int x { 5 }; // normal integer int & ref { x }; // reference to variable value return 0 ; } // x and ref die here","title":"\u5de6\u503c\u5f15\u7528\u7684\u4f5c\u7528\u57df\u548c\u6301\u7eed\u65f6\u95f4"},{"location":"9-3-Lvalue-references/#_7","text":"\u9664\u4e86\u67d0\u79cd\u4f8b\u5916\u60c5\u51b5\uff08\u4e0b\u4e00\u8282\u4f1a\u4ecb\u7ecd\uff09\u4e4b\u5916\uff0c \u5f15\u7528\u548c\u88ab\u5f15\u7528\u5bf9\u8c61\u5177\u6709\u72ec\u7acb\u7684\u751f\u547d\u5468\u671f \u3002\u6362\u8a00\u4e4b\uff0c\u4e0b\u9762\u4e24\u6761\u662f\u6b63\u786e\u7684\uff1a \u88ab\u5f15\u7528\u5bf9\u8c61\u53ef\u4ee5\u5148\u4e8e\u5f15\u7528\u9500\u6bc1\uff1b \u5f15\u7528\u53ef\u4ee5\u5148\u4e8e\u88ab\u5f15\u7528\u5bf9\u8c61\u9500\u6bc1\u3002 \u5982\u679c\u5f15\u7528\u5148\u4e8e\u88ab\u5f15\u7528\u5bf9\u8c61\u9500\u6bc1\uff0c\u5b8c\u5168\u4e0d\u4f1a\u5f71\u54cd\u5230\u88ab\u5f15\u7528\u5bf9\u8c61\uff0c\u4e0b\u9762\u7a0b\u5e8f\u53ef\u4ee5\u8868\u660e\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int x { 5 }; { int & ref { x }; // ref is a reference to x std :: cout << ref ; // prints value of ref (5) } // ref is destroyed here -- x is unaware of this std :: cout << x ; // prints value of x (5) return 0 ; } // x destroyed here \u6253\u5370\u7ed3\u679c\uff1a 1 55 \u5f53 ref \u88ab\u9500\u6bc1\u540e\uff0c\u53d8\u91cf x \u4e00\u5207\u5982\u6545\u3002 \u5168\u7136\u4e0d\u77e5\u6253\u5b83\u7684\u5f15\u7528\u5df2\u7ecf\u88ab\u9500\u6bc1\u4e86\u3002","title":"\u5f15\u7528\u548c\u88ab\u5f15\u7528\u5bf9\u8c61\u5177\u6709\u72ec\u7acb\u7684\u751f\u547d\u5468\u671f"},{"location":"9-3-Lvalue-references/#_8","text":"\u5f53\u4e00\u4e2a\u5bf9\u8c61\u5148\u4e8e\u5b83\u7684\u5f15\u7528\u88ab\u9500\u6bc1\uff0c\u5219\u5176\u5f15\u7528\u6b64\u65f6\u4f1a\u7ed1\u5b9a\u5230\u4e00\u4e2a\u5df2\u7ecf\u4e0d\u5b58\u5728\u7684\u5bf9\u8c61\u3002\u6b64\u65f6\u8be5\u5f15\u7528\u4f1a\u6210\u4e3a\u4e00\u4e2a \u60ac\u5782 \u5f15\u7528\u3002\u8bbf\u95ee\u60ac\u5782\u5f15\u7528\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u60ac\u5782\u6307\u9488\u5176\u5b9e\u5f88\u5bb9\u6613\u907f\u514d\uff0c\u4f46\u662f\u6211\u4eec\u4f1a\u5728 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 \u4e2d\u50cf\u4f60\u5c55\u793a\u4e00\u4e2a\u7531\u60ac\u5782\u6307\u9488\u5f15\u53d1\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u6848\u4f8b\u3002","title":"\u60ac\u5782\u5f15\u7528"},{"location":"9-3-Lvalue-references/#_9","text":"\u4e5f\u8bb8\u51fa\u4e4e\u4f60\u7684\u610f\u6599\uff0c\u5728C++\u4e2d\u5f15\u7528\u5e76\u4e0d\u662f\u4e00\u4e2a\u5bf9\u8c61\u3002 \u5f15\u7528\u5e76\u4e0d\u9700\u8981\u5b58\u5728\u6216\u5360\u6709\u5b58\u50a8\u7a7a\u95f4 \u3002\u5982\u679c\u53ef\u80fd\u7684\u8bdd\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u4f18\u5316\u5f15\u7528\uff0c\u4f7f\u7528\u88ab\u5f15\u7528\u5bf9\u8c61\u5bf9\u5176\u8fdb\u884c\u66ff\u6362\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u5e76\u603b\u662f\u53ef\u884c\u7684\uff0c\u6b64\u65f6\u5f15\u7528\u4fbf\u9700\u8981\u5b58\u50a8\u7a7a\u95f4\u3002 \u8fd9\u4e5f\u610f\u5473\u7740\u201c\u5f15\u7528\u53d8\u91cf\u201d\u8fd9\u4e2a\u672f\u8bed\u6709\u4e9b\u7528\u8bcd\u4e0d\u5f53\uff0c\u56e0\u4e3a\u53d8\u91cf\u662f\u5bf9\u8c61\u7684\u540d\u5b57\uff0c\u800c\u5f15\u7528\u5e76\u4e0d\u662f\u5bf9\u8c61\u3002 \u56e0\u4e3a==\u5f15\u7528\u5e76\u4e0d\u662f\u5bf9\u8c61\uff0c\u6240\u4ee5\u4f60\u5e76\u4e0d\u80fd\u5728\u4efb\u4f55\u80fd\u4f7f\u7528\u5bf9\u8c61\u7684\u5730\u65b9\u6765\u4f7f\u7528\u5b83\uff08\u4f8b\u5982\uff0c\u4f60\u4e0d\u80fd\u521b\u5efa\u5f15\u7528\u7684\u5f15\u7528\uff0c\u56e0\u4e3a\u4f5c\u6307\u5f15\u4e0e\u5fc5\u987b\u5f15\u7528\u4e00\u4e2a\u6709\u8eab\u4efd\u6807\u8bc6\u7684\u5bf9\u8c61\uff09==\u3002\u4e07\u4e00\u4f60\u9700\u8981\u4e00\u4e2a\u652f\u6301\u91cd\u65b0\u7ed1\u5b9a\u7684\u5bf9\u8c61\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 std::reference_wrapper (\u5728 16.3 -- Aggregation \u4e2d\u4ecb\u7ecd)\u3002 \u9898\u5916\u8bdd \u8003\u8651\u4e0b\u5217\u53d8\u91cf\uff1a 1 2 3 int var {}; int & ref1 { var }; // an lvalue reference bound to var int & ref2 { ref1 }; // an lvalue reference bound to var \u56e0\u4e3a ref2 (\u662f\u4e00\u4e2a\u5f15\u7528) \u88ab\u521d\u59cb\u5316\u4e3a ref1 (\u4e5f\u662f\u4e00\u4e2a\u5f15\u7528)\uff0c\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a ref2 \u662f\u4e00\u4e2a\u5f15\u7528\u7684\u5f15\u7528\u3002\u4f46\u5176\u5b9e\u5b83\u4e0d\u662f\uff0c\u56e0\u4e3a ref1 \u662f var \u7684\u5f15\u7528\uff0c\u5f53\u6211\u4eec\u5728\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u5b83\u7684\u7684\u65f6\u5019\uff08\u4f5c\u4e3a\u521d\u59cb\u5316\u503c\uff09\uff0c ref1 \u4f1a\u6c42\u503c\u5f97\u5230 var \u3002\u6240\u4ee5 ref2 \u5176\u5b9e\u53ea\u662f\u4e00\u4e2a\u666e\u901a\u7684 \u5de6\u503c\u5f15\u7528 \uff08\u6b63\u5982\u5b83\u7684\u7c7b\u578b\u6240\u8868\u660e\u7684\u90a3\u6837\uff09\uff0c\u5b83\u4e5f\u4e0e var \u7ed1\u5b9a\u3002 \u5f15\u7528\u7684\u5f15\u7528\u672c\u6765\u53ef\u4ee5\u4f7f\u7528\u8bed\u6cd5 int&& \u7684\uff0c\u53ef\u60dcC++\u5e76\u4e0d\u652f\u6301\u5f15\u7528\u7684\u5f15\u7528\uff0c\u56e0\u6b64\u8fd9\u4e2a\u8bed\u6cd5\u5f62\u5f0f\u5728 C++11\u4e2d\u88ab\u7528\u6765\u8868\u793a \u53f3\u503c\u5f15\u7528 \uff09\u3002","title":"\u5f15\u7528\u4e0d\u662f\u5bf9\u8c61"},{"location":"9-4-Lvalue-references-to-const/","tags":["lvalue references","const"],"text":"Key Takeaway \u5de6\u503c\u5f15\u7528\u53ea\u80fd\u7ed1\u5b9a\u5230\u53ef\u4fee\u6539\u7684\u5de6\u503c\u3002 \u5bf9const\u7684\u5de6\u503c\u5f15\u7528\u53ef\u4ee5\u7ed1\u5b9a\u5230\u53ef\u4fee\u6539\u7684\u5de6\u503c\u3001\u4e0d\u53ef\u4fee\u6539\u7684\u5de6\u503c\u548c\u53f3\u503c\u3002\u8fd9\u4f7f\u5f97\u5b83\u4eec\u6210\u4e3a\u4e00\u79cd\u66f4\u52a0\u7075\u6d3b\u7684\u5f15\u7528\u7c7b\u578b\u3002 \u5728\u4e0a\u8282\u8bfe ( 9.3 - \u5de6\u503c\u5f15\u7528 ) \u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u4e3a\u58f0\u660e \u5de6\u503c\u5f15\u7528 \u53ea\u80fd\u7ed1\u5b9a\u5230\u4e00\u4e2a\u53ef\u4fee\u6539\u7684 \u5de6\u503c \u4e0a\u3002\u8fd9\u610f\u5473\u7740\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u975e\u6cd5\u7684\uff1a 1 2 3 4 5 6 7 int main () { const int x { 5 }; // x is a non-modifiable (const) lvalue int & ref { x }; // error: ref can not bind to non-modifiable lvalue return 0 ; } \u8fd9\u662f\u4e0d\u5141\u8bb8\u7684\uff0c\u56e0\u4e3a\u8fd9\u6837\u6211\u4eec\u5c31\u80fd\u591f\u901a\u8fc7\u975e const \u5f15\u7528( ref )\u4fee\u6539 const \u53d8\u91cf( x )\u3002 \u4f46\u5982\u679c\u6211\u4eec\u60f3\u8981\u4e00\u4e2a const \u53d8\u91cf\u6765\u521b\u5efa\u5f15\u7528\uff0c\u8be5\u600e\u4e48\u529e\u5462\uff1f\u666e\u901a\u7684\u5de6\u503c\u5f15\u7528(\u6307\u5411\u975econst\u503c)\u662f\u4e0d\u884c\u7684\u3002 \u6307\u5411 const \u7684\u5de6\u503c\u5f15\u7528 \u4f7f\u7528 const \u5173\u952e\u5b57\u58f0\u660e\u5de6\u503c\u5f15\u7528\uff0c\u5373\u8981\u6c42\u5de6\u503c\u5f15\u7528\u5c06\u5176\u5f15\u7528\u7684\u5bf9\u8c61\u5f53\u505aconst\u770b\u5230\u3002\u6b64\u65f6\u79f0\u8be5\u5f15\u7528\u4e3a\u6307\u5411const\u5bf9\u8c61\u7684\u5de6\u503c\u5f15\u7528\uff08\u6709\u65f6\u79f0\u4e3a\u6307\u5411const\u7684\u5f15\u7528\u6216 const\u5de6\u503c\u5f15\u7528 \uff09\u3002 \u5bf9const\u5f15\u7528\u53ef\u4ee5\u7ed1\u5b9a\u5230\u4e0d\u53ef\u4fee\u6539\u7684\u5de6\u503c: 1 2 3 4 5 6 7 int main () { const int x { 5 }; // x is a non-modifiable lvalue const int & ref { x }; // okay: ref is a an lvalue reference to a const value return 0 ; } \u56e0\u4e3aconst\u7684\u5de6\u503c\u5f15\u7528\u5c06\u5b83\u4eec\u5f15\u7528\u7684\u5bf9\u8c61\u89c6\u4e3aconst\uff0c\u6240\u4ee5\u5b83\u4eec\u53ef\u4ee5\u7528\u4e8e\u8bbf\u95ee\u4f46\u4e0d\u80fd\u4fee\u6539\u88ab\u5f15\u7528\u7684\u503c: 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { const int x { 5 }; // x is a non-modifiable lvalue const int & ref { x }; // okay: ref is a an lvalue reference to a const value std :: cout << ref ; // okay: we can access the const object ref = 6 ; // error: we can not modify a const object return 0 ; } \u4f7f\u7528\u53ef\u4fee\u6539\u5de6\u503c\u521d\u59cb\u5316const\u5de6\u503c\u5f15\u7528 const\u5de6\u503c\u5f15\u7528\u4e5f\u53ef\u4ee5\u7ed1\u5b9a\u5230\u53ef\u4fee\u6539\u7684\u5de6\u503c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u5f15\u7528\u8bbf\u95ee\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u65f6\u5c06\u88ab\u89c6\u4e3aconst(\u5373\u4f7f\u5e95\u5c42\u5bf9\u8c61\u4e0d\u662fconst): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 5 }; // x is a modifiable lvalue const int & ref { x }; // okay: we can bind a const reference to a modifiable lvalue std :: cout << ref ; // okay: we can access the object through our const reference ref = 7 ; // error: we can not modify an object through a const reference x = 6 ; // okay: x is a modifiable lvalue, we can still modify it through the original identifier return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06const\u5f15\u7528 ref \u7ed1\u5b9a\u5230\u53ef\u4fee\u6539\u5de6\u503c x \u3002\u968f\u540e\u4fbf\u53ef\u4ee5\u4f7f\u7528 ref \u8bbf\u95ee x \uff0c\u4f46\u662f\u56e0\u4e3a ref \u662f const \u7684\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u80fd\u901a\u8fc7 ref \u4fee\u6539 x \u3002\u7136\u800c\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539 x \u3002 \u6700\u4f73\u5b9e\u8df5 \u9664\u975e\u9700\u8981\u4fee\u6539\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\uff0c\u6700\u597d\u4f7f\u7528\u201cconst\u5de6\u503c\u5f15\u7528\u201d\uff0c\u800c\u4e0d\u662f\u201c\u975econst\u7684\u5de6\u503c\u5f15\u7528\u201d\u3002 \u4f7f\u7528\u53f3\u503c\u521d\u59cb\u5316const\u5de6\u503c\u5f15\u7528 \u4e5f\u8bb8\u4ee4\u4eba\u60ca\u8bb6\u7684\u662f\uff0cconst\u5de6\u503c\u5f15\u7528\u4e5f\u53ef\u4ee5\u7ed1\u5b9a\u5230\u53f3\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { const int & ref { 5 }; // okay: 5 is an rvalue std :: cout << ref ; // prints 5 return 0 ; } \u5f53\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\u65f6\uff0c\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\u5e76\u7528\u53f3\u503c\u521d\u59cb\u5316\uff0c\u5bf9const\u7684\u5f15\u7528\u88ab\u7ed1\u5b9a\u5230\u8be5\u4e34\u65f6\u5bf9\u8c61\u3002 \u4e34\u65f6\u5bf9\u8c61(\u6709\u65f6\u4e5f\u79f0\u4e3a\u533f\u540d\u5bf9\u8c61)\u662f\u5728\u5355\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u4e3a\u4e34\u65f6\u4f7f\u7528(\u7136\u540e\u9500\u6bc1)\u800c\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u4e34\u65f6\u5bf9\u8c61\u5b8c\u5168\u6ca1\u6709\u4f5c\u7528\u57df(\u8fd9\u662f\u6709\u610f\u4e49\u7684\uff0c\u56e0\u4e3a==\u4f5c\u7528\u57df\u662f\u6807\u8bc6\u7b26\u7684\u5c5e\u6027\uff0c\u800c\u4e34\u65f6\u5bf9\u8c61\u6ca1\u6709\u6807\u8bc6\u7b26==)\u3002\u8fd9\u610f\u5473\u7740\u4e34\u65f6\u5bf9\u8c61\u53ea\u80fd\u5728\u521b\u5efa\u5b83\u7684\u4f4d\u7f6e\u76f4\u63a5\u4f7f\u7528\uff0c\u56e0\u4e3a\u6ca1\u6709\u529e\u6cd5\u5728\u8be5\u4f4d\u7f6e\u4e4b\u540e\u518d\u5f15\u7528\u5b83\u3002 const \u5f15\u7528\u7ed1\u5b9a\u5230\u4e34\u65f6\u5bf9\u8c61\u65f6\u4f1a\u5ef6\u957f\u4e34\u65f6\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f \u4e34\u65f6\u5bf9\u8c61\u901a\u5e38\u5728\u521b\u5efa\u5b83\u4eec\u7684\u8868\u8fbe\u5f0f\u7684\u672b\u5c3e\u88ab\u9500\u6bc1\u3002 \u4f46\u662f\uff0c\u5982\u679c\u4e3a\u4fdd\u5b58\u53f3\u503c 5 \u800c\u521b\u5efa\u7684\u4e34\u65f6\u5bf9\u8c61\u5728\u521d\u59cb\u5316 ref \u7684\u8868\u8fbe\u5f0f\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\uff0c\u90a3\u4e48\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u4f1a\u53d1\u751f\u4ec0\u4e48\u60c5\u51b5\u5462\uff1f\u5f15\u7528 ref \u5c06\u53d8\u6210 \u60ac\u5782 \u5f15\u7528(\u5f15\u7528\u4e00\u4e2a\u5df2\u7ecf\u88ab\u9500\u6bc1\u7684\u5bf9\u8c61)\uff0c\u5f53\u6211\u4eec\u8bd5\u56fe\u8bbf\u95ee ref \u65f6\uff0c\u5c06\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u4e0b\u7684 \u60ac\u5782 \u5f15\u7528\uff0cC++\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u89c4\u5219\uff1a \u5f53const\u5de6\u503c\u5f15\u7528\u88ab\u7ed1\u5b9a\u5230\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\u65f6\uff0c\u4e34\u65f6\u5bf9\u8c61\u7684\u751f\u5b58\u671f\u5c06\u88ab\u6269\u5c55\u5230\u4e0e\u5f15\u7528\u7684\u751f\u5b58\u671f\u76f8\u5339\u914d\u3002 1 2 3 4 5 6 7 8 9 10 #include int main () { const int & ref { 5 }; // The temporary object holding value 5 has its lifetime extended to match ref std :: cout << ref ; // Therefore, we can safely use it here return 0 ; } // Both ref and the temporary object die here \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53\u4f7f\u7528\u53f3\u503c 5 \u521d\u59cb\u5316 ref \u65f6\uff0c\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\u88ab\u521b\u5efa\u5e76\u7ed1\u5b9a\u5230 ref \u3002\u8be5\u4e34\u65f6\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u5c31\u4f1a\u88ab\u6269\u5c55\u5230 ref \u7684\u751f\u547d\u5468\u671f\u3002Thus, we can safely print the value of ref in the next statement. Then both ref and the temporary object go out of scope and are destroyed at the end of the block. \u5173\u952e\u4fe1\u606f \u5de6\u503c\u5f15\u7528\u53ea\u80fd\u7ed1\u5b9a\u5230\u53ef\u4fee\u6539\u7684\u5de6\u503c\u3002 \u5bf9const\u7684\u5de6\u503c\u5f15\u7528\u53ef\u4ee5\u7ed1\u5b9a\u5230\u53ef\u4fee\u6539\u7684\u5de6\u503c\u3001\u4e0d\u53ef\u4fee\u6539\u7684\u5de6\u503c\u548c\u53f3\u503c\u3002\u8fd9\u4f7f\u5f97\u5b83\u4eec\u6210\u4e3a\u4e00\u79cd\u66f4\u52a0\u7075\u6d3b\u7684\u5f15\u7528\u7c7b\u578b\u3002 \u90a3\u4e48\uff0c\u4e3a\u4ec0\u4e48C++\u5141\u8bb8const\u5f15\u7528\u7ed1\u5b9a\u5230\u53f3\u503c\u5462\uff1f\u6211\u4eec\u5c06\u5728 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 \u4e2d\u56de\u7b54\u8fd9\u4e2a\u95ee\u9898!","title":"9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528"},{"location":"9-4-Lvalue-references-to-const/#const","text":"\u4f7f\u7528 const \u5173\u952e\u5b57\u58f0\u660e\u5de6\u503c\u5f15\u7528\uff0c\u5373\u8981\u6c42\u5de6\u503c\u5f15\u7528\u5c06\u5176\u5f15\u7528\u7684\u5bf9\u8c61\u5f53\u505aconst\u770b\u5230\u3002\u6b64\u65f6\u79f0\u8be5\u5f15\u7528\u4e3a\u6307\u5411const\u5bf9\u8c61\u7684\u5de6\u503c\u5f15\u7528\uff08\u6709\u65f6\u79f0\u4e3a\u6307\u5411const\u7684\u5f15\u7528\u6216 const\u5de6\u503c\u5f15\u7528 \uff09\u3002 \u5bf9const\u5f15\u7528\u53ef\u4ee5\u7ed1\u5b9a\u5230\u4e0d\u53ef\u4fee\u6539\u7684\u5de6\u503c: 1 2 3 4 5 6 7 int main () { const int x { 5 }; // x is a non-modifiable lvalue const int & ref { x }; // okay: ref is a an lvalue reference to a const value return 0 ; } \u56e0\u4e3aconst\u7684\u5de6\u503c\u5f15\u7528\u5c06\u5b83\u4eec\u5f15\u7528\u7684\u5bf9\u8c61\u89c6\u4e3aconst\uff0c\u6240\u4ee5\u5b83\u4eec\u53ef\u4ee5\u7528\u4e8e\u8bbf\u95ee\u4f46\u4e0d\u80fd\u4fee\u6539\u88ab\u5f15\u7528\u7684\u503c: 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { const int x { 5 }; // x is a non-modifiable lvalue const int & ref { x }; // okay: ref is a an lvalue reference to a const value std :: cout << ref ; // okay: we can access the const object ref = 6 ; // error: we can not modify a const object return 0 ; }","title":"\u6307\u5411 const \u7684\u5de6\u503c\u5f15\u7528"},{"location":"9-4-Lvalue-references-to-const/#const_1","text":"const\u5de6\u503c\u5f15\u7528\u4e5f\u53ef\u4ee5\u7ed1\u5b9a\u5230\u53ef\u4fee\u6539\u7684\u5de6\u503c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u5f15\u7528\u8bbf\u95ee\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u65f6\u5c06\u88ab\u89c6\u4e3aconst(\u5373\u4f7f\u5e95\u5c42\u5bf9\u8c61\u4e0d\u662fconst): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 5 }; // x is a modifiable lvalue const int & ref { x }; // okay: we can bind a const reference to a modifiable lvalue std :: cout << ref ; // okay: we can access the object through our const reference ref = 7 ; // error: we can not modify an object through a const reference x = 6 ; // okay: x is a modifiable lvalue, we can still modify it through the original identifier return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06const\u5f15\u7528 ref \u7ed1\u5b9a\u5230\u53ef\u4fee\u6539\u5de6\u503c x \u3002\u968f\u540e\u4fbf\u53ef\u4ee5\u4f7f\u7528 ref \u8bbf\u95ee x \uff0c\u4f46\u662f\u56e0\u4e3a ref \u662f const \u7684\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u80fd\u901a\u8fc7 ref \u4fee\u6539 x \u3002\u7136\u800c\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539 x \u3002 \u6700\u4f73\u5b9e\u8df5 \u9664\u975e\u9700\u8981\u4fee\u6539\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\uff0c\u6700\u597d\u4f7f\u7528\u201cconst\u5de6\u503c\u5f15\u7528\u201d\uff0c\u800c\u4e0d\u662f\u201c\u975econst\u7684\u5de6\u503c\u5f15\u7528\u201d\u3002","title":"\u4f7f\u7528\u53ef\u4fee\u6539\u5de6\u503c\u521d\u59cb\u5316const\u5de6\u503c\u5f15\u7528"},{"location":"9-4-Lvalue-references-to-const/#const_2","text":"\u4e5f\u8bb8\u4ee4\u4eba\u60ca\u8bb6\u7684\u662f\uff0cconst\u5de6\u503c\u5f15\u7528\u4e5f\u53ef\u4ee5\u7ed1\u5b9a\u5230\u53f3\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { const int & ref { 5 }; // okay: 5 is an rvalue std :: cout << ref ; // prints 5 return 0 ; } \u5f53\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\u65f6\uff0c\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\u5e76\u7528\u53f3\u503c\u521d\u59cb\u5316\uff0c\u5bf9const\u7684\u5f15\u7528\u88ab\u7ed1\u5b9a\u5230\u8be5\u4e34\u65f6\u5bf9\u8c61\u3002 \u4e34\u65f6\u5bf9\u8c61(\u6709\u65f6\u4e5f\u79f0\u4e3a\u533f\u540d\u5bf9\u8c61)\u662f\u5728\u5355\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u4e3a\u4e34\u65f6\u4f7f\u7528(\u7136\u540e\u9500\u6bc1)\u800c\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u4e34\u65f6\u5bf9\u8c61\u5b8c\u5168\u6ca1\u6709\u4f5c\u7528\u57df(\u8fd9\u662f\u6709\u610f\u4e49\u7684\uff0c\u56e0\u4e3a==\u4f5c\u7528\u57df\u662f\u6807\u8bc6\u7b26\u7684\u5c5e\u6027\uff0c\u800c\u4e34\u65f6\u5bf9\u8c61\u6ca1\u6709\u6807\u8bc6\u7b26==)\u3002\u8fd9\u610f\u5473\u7740\u4e34\u65f6\u5bf9\u8c61\u53ea\u80fd\u5728\u521b\u5efa\u5b83\u7684\u4f4d\u7f6e\u76f4\u63a5\u4f7f\u7528\uff0c\u56e0\u4e3a\u6ca1\u6709\u529e\u6cd5\u5728\u8be5\u4f4d\u7f6e\u4e4b\u540e\u518d\u5f15\u7528\u5b83\u3002","title":"\u4f7f\u7528\u53f3\u503c\u521d\u59cb\u5316const\u5de6\u503c\u5f15\u7528"},{"location":"9-4-Lvalue-references-to-const/#const_3","text":"\u4e34\u65f6\u5bf9\u8c61\u901a\u5e38\u5728\u521b\u5efa\u5b83\u4eec\u7684\u8868\u8fbe\u5f0f\u7684\u672b\u5c3e\u88ab\u9500\u6bc1\u3002 \u4f46\u662f\uff0c\u5982\u679c\u4e3a\u4fdd\u5b58\u53f3\u503c 5 \u800c\u521b\u5efa\u7684\u4e34\u65f6\u5bf9\u8c61\u5728\u521d\u59cb\u5316 ref \u7684\u8868\u8fbe\u5f0f\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\uff0c\u90a3\u4e48\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u4f1a\u53d1\u751f\u4ec0\u4e48\u60c5\u51b5\u5462\uff1f\u5f15\u7528 ref \u5c06\u53d8\u6210 \u60ac\u5782 \u5f15\u7528(\u5f15\u7528\u4e00\u4e2a\u5df2\u7ecf\u88ab\u9500\u6bc1\u7684\u5bf9\u8c61)\uff0c\u5f53\u6211\u4eec\u8bd5\u56fe\u8bbf\u95ee ref \u65f6\uff0c\u5c06\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u4e0b\u7684 \u60ac\u5782 \u5f15\u7528\uff0cC++\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u89c4\u5219\uff1a \u5f53const\u5de6\u503c\u5f15\u7528\u88ab\u7ed1\u5b9a\u5230\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\u65f6\uff0c\u4e34\u65f6\u5bf9\u8c61\u7684\u751f\u5b58\u671f\u5c06\u88ab\u6269\u5c55\u5230\u4e0e\u5f15\u7528\u7684\u751f\u5b58\u671f\u76f8\u5339\u914d\u3002 1 2 3 4 5 6 7 8 9 10 #include int main () { const int & ref { 5 }; // The temporary object holding value 5 has its lifetime extended to match ref std :: cout << ref ; // Therefore, we can safely use it here return 0 ; } // Both ref and the temporary object die here \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53\u4f7f\u7528\u53f3\u503c 5 \u521d\u59cb\u5316 ref \u65f6\uff0c\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\u88ab\u521b\u5efa\u5e76\u7ed1\u5b9a\u5230 ref \u3002\u8be5\u4e34\u65f6\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u5c31\u4f1a\u88ab\u6269\u5c55\u5230 ref \u7684\u751f\u547d\u5468\u671f\u3002Thus, we can safely print the value of ref in the next statement. Then both ref and the temporary object go out of scope and are destroyed at the end of the block. \u5173\u952e\u4fe1\u606f \u5de6\u503c\u5f15\u7528\u53ea\u80fd\u7ed1\u5b9a\u5230\u53ef\u4fee\u6539\u7684\u5de6\u503c\u3002 \u5bf9const\u7684\u5de6\u503c\u5f15\u7528\u53ef\u4ee5\u7ed1\u5b9a\u5230\u53ef\u4fee\u6539\u7684\u5de6\u503c\u3001\u4e0d\u53ef\u4fee\u6539\u7684\u5de6\u503c\u548c\u53f3\u503c\u3002\u8fd9\u4f7f\u5f97\u5b83\u4eec\u6210\u4e3a\u4e00\u79cd\u66f4\u52a0\u7075\u6d3b\u7684\u5f15\u7528\u7c7b\u578b\u3002 \u90a3\u4e48\uff0c\u4e3a\u4ec0\u4e48C++\u5141\u8bb8const\u5f15\u7528\u7ed1\u5b9a\u5230\u53f3\u503c\u5462\uff1f\u6211\u4eec\u5c06\u5728 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 \u4e2d\u56de\u7b54\u8fd9\u4e2a\u95ee\u9898!","title":"const \u5f15\u7528\u7ed1\u5b9a\u5230\u4e34\u65f6\u5bf9\u8c61\u65f6\u4f1a\u5ef6\u957f\u4e34\u65f6\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f"},{"location":"9-5-Pass-by-lvalue-reference/","tags":["Lvalue"],"text":"Key Takeaway \u5f62\u53c2\u5b9a\u4e49\u4e3a\u6307\u5411\u975econst\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u5b83\u53ea\u80fd\u63a5\u6536\u975econst\u7c7b\u578b\u7684\u5b9e\u53c2\uff0c\u65e0\u6cd5\u914d\u5408\u975econst\u7c7b\u578b\u548c\u5b57\u9762\u91cf\u4f7f\u7528\uff0c\u6781\u5927\u5730\u9650\u5236\u4e86\u5b83\u7684\u4f5c\u7528\u3002\u56e0\u6b64\u4e00\u822c\u53ef\u4ee5\u5b9a\u4e49\u4e3aconst\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u6b64\u65f6\u5c31\u53ef\u4ee5\u914d\u5408const\u7c7b\u578b\u3001\u975econst\u7c7b\u578b\u548c\u5b57\u9762\u91cf\u6765\u4f7f\u7528\u3002\u4f46\u6b64\u65f6\u4f60\u5c31\u4e0d\u80fd\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5b9e\u53c2\u7684\u5185\u5bb9\u3002 \u5bf9\u4e8e\u62f7\u8d1d\u5f00\u9500\u5c0f\u7684\u5bf9\u8c61\uff0c\u4f7f\u7528\u6309\u503c\u4f20\u9012\uff0c\u5bf9\u4e8e\u62f7\u8d1d\u5f00\u9500\u5927\u7684\u5bf9\u8c61\uff0c\u4f7f\u7528\u6309const\u5f15\u7528\u4f20\u9012\u3002\u5982\u679c\u4e0d\u786e\u5b9a\u5f00\u9500\u5927\u8fd8\u662f\u5c0f\uff0c\u5219\u4f7f\u7528\u6309const\u5f15\u7528\u4f20\u9012\u3002 \u5982\u679c\u5bf9\u8c61\u4f7f\u7528\u5185\u5b58\u4e0d\u8d85\u8fc7\u4e24\u4e2a \u5b57\u957f \u4e14\u6ca1\u6709\u989d\u5916\u7684\u8bbe\u7f6e\u5de5\u4f5c\uff08\u6253\u5f00\u6587\u4ef6\u6216\u5206\u914d\u5185\u5b58\uff09\uff0c\u5219\u53ef\u4ee5\u8ba4\u4e3a\u62f7\u8d1d\u5f00\u9500\u5c0f \u62f7\u8d1d\u5f00\u9500\u5c0f\u7684\u7c7b\u578b\u901a\u5e38\u6709\uff1a\u6240\u6709\u57fa\u672c\u7c7b\u578b\u3001\u679a\u4e3e\u7c7b\u578b\u548c std::string_view . \u62f7\u8d1d\u5f00\u9500\u5927\u7684\u7c7b\u578b\u901a\u5e38\u6709\uff1a std::array , std::string , std::vector , \u548c std::ostream . \u5728\u524d\u9762\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5de6\u503c\u5f15\u7528 ( 9.3 - \u5de6\u503c\u5f15\u7528 ) \u548c\u6307\u5411const\u7684\u5de6\u503c\u5f15\u7528 ( 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 )\u3002\u5355\u72ec\u6765\u770b\uff0c\u8fd9\u4e24\u79cd\u5f15\u7528\u770b\u4e0a\u53bb\u90fd\u6ca1\u5565\u7528\u2014\u2014\u4e3a\u4ec0\u4e48\u6211\u4eec\u80fd\u591f\u76f4\u63a5\u8bbf\u95ee\u53d8\u91cf\u5374\u8fd8\u8981\u4e3a\u5176\u521b\u5efa\u4e00\u4e2a\u522b\u540d\u5462\uff1f \u672c\u8282\u8bfe\u6211\u4eec\u4f1a\u5411\u4f60\u5c55\u793a\uff0c\u4e3a\u4ec0\u4e48\u5f15\u7528\u975e\u5e38\u6709\u7528\u3002\u800c\u4e14\u4ece\u672c\u7ae0\u5f00\u59cb\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u6211\u4eec\u4f1a\u7ecf\u5e38\u4f7f\u7528\u5b83\u4eec\u3002 \u9996\u5148\uff0c\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\uff08\u56de\u770b 2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2 \uff09\uff0c\u6211\u4eec\u8ba8\u8bba\u8fc7 \u6309\u503c\u4f20\u9012 \uff0c\u5373 \u5b9e\u53c2 \u5728\u4f20\u5165\u51fd\u6570\u4f5c\u4e3a \u5f62\u53c2 \u65f6\uff0c\u901a\u8fc7\u521b\u5efa\u62f7\u8d1d\u7684\u65b9\u5f0f\u4f20\u5165\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include void printValue ( int y ) { std :: cout << y << '\\n' ; } // y is destroyed here int main () { int x { 2 }; printValue ( x ); // x is passed by value (copied) into parameter y (inexpensive) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53 printValue(x) \u88ab\u8c03\u7528\u7684\u65f6\u5019\uff0c x \u7684\u503c\u4f1a\u88ab\u62f7\u8d1d\u5230\u5f62\u53c2 y \u3002\u5f53\u5230\u8fbe\u51fd\u6570\u7ed3\u5c3e\u7684\u65f6\u5019\uff0c y \u5c31\u4f1a\u88ab\u9500\u6bc1\u3002 \u8fd9\u610f\u5473\u7740\u5f53\u6211\u4eec\u8c03\u7528\u51fd\u6570\u7684\u65f6\u5019\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u5b9e\u53c2\u503c\u7684\u62f7\u8d1d\uff0c\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u540e\u4fbf\u5c06\u5176\u9500\u6bc1\u4e86\uff01\u5e78\u8fd0\u5730\u662f\uff0c\u56e0\u4e3a\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u62f7\u8d1d\u5f00\u9500\u5e76\u4e0d\u5927\uff0c\u6240\u4ee5\u8fd9\u5e76\u4e0d\u4f1a\u5e26\u6765\u4ec0\u4e48\u95ee\u9898\u3002 \u6709\u4e9b\u5bf9\u8c61\u7684\u62f7\u8d1d\u5f00\u9500\u5f88\u5927 \u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u591a\u6570\u6570\u636e\u7c7b\u578b\u90fd\u5c5e\u4e8e\u7c7b(\u4f8b\u5982 std::string )\u3002\u7c7b\u7c7b\u578b\u7684\u62f7\u8d1d\u5f00\u9500\u901a\u5e38\u662f\u5f88\u5927\u7684\u3002\u5982\u679c\u53ef\u80fd\uff0c\u5e94\u8be5\u5c3d\u53ef\u80fd\u5730\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\uff0c\u5c24\u5176\u662f\u5728\u8fd9\u4e9b\u53d8\u91cf\u7528\u5b8c\u5373\u5f03\u7684\u60c5\u51b5\u4e0b\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u6b63\u662f\u8868\u660e\u4e86\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include void printValue ( std :: string y ) { std :: cout << y << '\\n' ; } // y is destroyed here int main () { std :: string x { \"Hello, world!\" }; // x is a std::string printValue ( x ); // x is passed by value (copied) into parameter y (expensive) return 0 ; } \u7a0b\u5e8f\u4f1a\u6253\u5370\uff1a 1 Hello, world! \u5c3d\u7ba1\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u548c\u6211\u4eec\u60f3\u7684\u4e00\u6837\uff0c\u4f46\u5176\u6548\u7387\u662f\u5f88\u4f4e\u7684\u3002\u548c\u524d\u9762\u7684\u7a0b\u5e8f\u4e00\u6837\u5f53\u6211\u4eec\u8c03\u7528 printValue() \u7684\u65f6\u5019\uff0c\u5b9e\u53c2 x \u4f1a\u88ab\u62f7\u8d1d\u5230\u5f62\u53c2 y \u3002\u4e0d\u8fc7\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b9e\u53c2 std::string \u5e76\u4e0d\u50cf int \u90a3\u6837\uff0c std::string \u662f\u4e00\u79cd\u7c7b\u7c7b\u578b\uff0c\u5bf9\u5b83\u7684\u62f7\u8d1d\u5f00\u9500\u662f\u975e\u5e38\u5927\u7684\uff0c\u66f4\u4e0d\u7528\u8bf4\u6bcf\u6b21\u8c03\u7528 printValue() \u90fd\u4f1a\u4ea7\u751f\u8fd9\u6837\u7684\u5f00\u9500\u3002 \u6211\u4eec\u53ef\u4ee5\u505a\u7684\u66f4\u597d\u3002 \u6309\u5f15\u7528\u4f20\u9012 \u907f\u514d\u51fd\u6570\u8c03\u7528\u65f6\u4ea7\u751f\u7684\u62f7\u8d1d\u5f00\u9500\uff0c\u53ef\u901a\u8fc7 \u6309\u5f15\u7528\u4f20\u9012 \u6765\u4ee3\u66ff \u6309\u503c\u4f20\u9012 \u3002\u5f53\u4f7f\u7528\u6309\u5f15\u7528\u4f20\u9012\u65f6\uff0c\u6211\u4eec\u5c06\u51fd\u6570\u7684\u5f62\u53c2\u58f0\u660e\u4e3a\u5f15\u7528\u7c7b\u578b\uff08\u6216\u6307\u5411const\u7684\u5f15\u7528\uff09\u800c\u4e0d\u662f\u666e\u901a\u7c7b\u578b\u3002\u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u6bcf\u4e2a\u5f15\u7528\u7c7b\u578b\u7684\u5f62\u53c2\u4f1a\u88ab\u7ed1\u5b9a\u5230\u4f20\u5165\u7684\u5b9e\u53c2\u3002\u56e0\u4e3a\u5f15\u7528\u5176\u5b9e\u662f\u5b9e\u53c2\u7684\u522b\u540d\uff0c\u6240\u4ee5\u5e76\u4e0d\u4f1a\u521b\u5efa\u62f7\u8d1d\u3002 \u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u548c\u4e0a\u9762\u7684\u4f8b\u5b50\u662f\u4e00\u81f4\u7684\uff0c\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\u5176\u4f7f\u7528\u4e86\u6309\u5f15\u7528\u4f20\u9012\u800c\u4e0d\u662f\u6309\u503c\u51fa\u4f20\u9012\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include void printValue ( std :: string & y ) // type changed to std::string& { std :: cout << y << '\\n' ; } // y is destroyed here int main () { std :: string x { \"Hello, world!\" }; printValue ( x ); // x is now passed by reference into reference parameter y (inexpensive) return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u548c\u4e0a\u9762\u7684\u7a0b\u5e8f\u6bcf\u592a\u5927\u533a\u522b\uff0c\u9664\u4e86\u5f62\u53c2 y \u7684\u7c7b\u578b\u4ece std::string \u88ab\u6539\u4e3a std::string& (\u5de6\u503c\u5f15\u7528)\u3002\u73b0\u5728\uff0c\u5f53 printValue(x) \u88ab\u8c03\u7528\u7684\u65f6\u5019\uff0c\u5de6\u503c\u5f15\u7528\u7c7b\u578b\u7684\u5f62\u53c2 y \u88ab\u7ed1\u5b9a\u5230\u5b9e\u53c2 x \u3002\u5f15\u7528\u7684\u7ed1\u5b9a\u5f00\u9500\u603b\u662f\u5f88\u5c0f\u7684\uff0c\u5b83\u4e0d\u9700\u8981\u521b\u5efa x \u7684\u62f7\u8d1d\u3002\u56e0\u4e3a\u5f15\u7528\u662f\u88ab\u5f15\u7528\u5bf9\u8c61\u7684\u522b\u540d\uff0c\u6240\u4ee5\u5f53 printValue() \u4f7f\u7528 y \u65f6\uff0c\u5b83\u8bbf\u95ee\u7684\u662f\u5b9e\u53c2 x \u672c\u8eab\uff08\u800c\u4e0d\u662f x \u7684\u62f7\u8d1d\uff09\u3002 \u5173\u952e\u4fe1\u606f \u6309\u5f15\u7528\u4f20\u9012\u5141\u8bb8\u6211\u4eec\u5411\u51fd\u6570\u4f20\u9012\u53c2\u6570\u800c\u4e0d\u9700\u521b\u5efa\u62f7\u8d1d\u3002 \u6309\u5f15\u7528\u4f20\u9012\u5141\u8bb8\u51fd\u6570\u4fee\u6539\u5b9e\u53c2\u7684\u503c \u5f53\u4e00\u4e2a\u5bf9\u8c61\u901a\u8fc7\u6309\u503c\u4f20\u9012\u7684\u65b9\u5f0f\u4f20\u5165\u51fd\u6570\u65f6\uff0c\u51fd\u6570\u63a5\u6536\u5230\u7684\u662f\u5176\u62f7\u8d1d\u3002\u8fd9\u4e5f\u5c31\u610f\u5473\u7740\u4efb\u4f55\u5bf9\u5f62\u53c2\u7684\u4fee\u6539\u90fd\u662f\u5bf9\u8fd9\u4efd\u62f7\u8d1d\u7684\u4fee\u6539\uff0c\u800c\u4e0d\u662f\u5b9e\u53c2\u672c\u8eab\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void addOne ( int y ) // y is a copy of x { ++ y ; // this modifies the copy of x, not the actual object x } int main () { int x { 5 }; std :: cout << \"value = \" << x << '\\n' ; addOne ( x ); std :: cout << \"value = \" << x << '\\n' ; // x has not been modified return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u5f62\u53c2 y \u662f\u5b9e\u53c2 x \u7684\u62f7\u8d1d\uff0c\u5f53\u6211\u4eec\u5bf9 y \u8fdb\u884c\u9012\u589e\u65f6\uff0c\u5b83\u53ea\u4f1a\u5f71\u54cd\u5230 y \uff0c\u56e0\u6b64\u7a0b\u5e8f\u7684\u8f93\u51fa\u4e3a\uff1a 1 2 value = 5 value = 5 \u7136\u800c\uff0c\u7531\u4e8e\u5f15\u7528\u6765\u8bf4\uff0c\u56e0\u4e3a\u5b83\u5c31\u662f\u88ab\u5f15\u7528\u5bf9\u8c61\u672c\u8eab\uff0c\u5f53\u4f7f\u7528\u6309\u5f15\u7528\u4f20\u9012\u65f6\uff0c\u5bf9\u5f15\u7528\u5f62\u53c2\u7684\u4efb\u4f55\u66f4\u6539\u90fd\u5c06\u5f71\u54cd\u5b9e\u53c2\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void addOne ( int & y ) // y is bound to the actual object x { ++ y ; // this modifies the actual object x } int main () { int x { 5 }; std :: cout << \"value = \" << x << '\\n' ; addOne ( x ); std :: cout << \"value = \" << x << '\\n' ; // x has been modified return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u4e3a\uff1a 1 2 value = 5 value = 6 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c x \u9996\u5148\u88ab\u521d\u59cb\u5316\u4e3a 5\u3002\u5f53 addOne(x) \u88ab\u8c03\u7528\u65f6\uff0c\u5f15\u7528\u7c7b\u578b\u5f62\u53c2 y \u4f1a\u88ab\u7ed1\u5b9a\u5230\u5b9e\u53c2 x \u3002\u5f53\u6211\u4eec\u5728 addOne() \u51fd\u6570\u4e2d\u5bf9 y \u8fdb\u884c\u9012\u589e\u65f6\uff0c\u5b83\u5b9e\u9645\u4fee\u6539\u4e86\u5b9e\u53c2 x \u7684\u503c\uff08\u800c\u4e0d\u662f\u5b83\u7684\u62f7\u8d1d\uff09\u3002\u56e0\u6b64\u5f53 addOne() \u8c03\u7528\u7ed3\u675f\u540e\u53d8\u91cf\u7684\u503c\u4ecd\u7136\u662f\u88ab\u4fee\u6539\u4e86\u3002 \u5173\u952e\u4fe1\u606f \u901a\u8fc7\u6309\u503c\u4f20\u9012\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u4fee\u6539\u4f20\u5165\u7684\u5b9e\u53c2\u3002 \u51fd\u6570\u4fee\u6539\u4f20\u5165\u53c2\u6570\u503c\u7684\u80fd\u529b\u662f\u5f88\u6709\u7528\u7684\u3002\u5047\u8bbe\u4f60\u7f16\u5199\u4e86\u4e00\u4e2a\u51fd\u6570\u6765\u5224\u65ad\u602a\u7269\u662f\u5426\u6210\u529f\u653b\u51fb\u4e86\u73a9\u5bb6\u3002\u5982\u679c\u662f\u8fd9\u6837\uff0c\u602a\u7269\u5e94\u8be5\u5bf9\u73a9\u5bb6\u7684\u751f\u547d\u503c\u9020\u6210\u4e00\u5b9a\u7684\u4f24\u5bb3\u3002\u5982\u679c\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u73a9\u5bb6\u5bf9\u8c61\uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539\u4f20\u5165\u7684\u5b9e\u9645\u73a9\u5bb6\u5bf9\u8c61\u7684\u5065\u5eb7\u72b6\u51b5\u3002\u5982\u679c\u4f60\u901a\u8fc7\u503c\u4f20\u9012\u73a9\u5bb6\u5bf9\u8c61\uff0c\u4f60\u53ea\u80fd\u4fee\u6539\u73a9\u5bb6\u5bf9\u8c61\u526f\u672c\u7684\u5065\u5eb7\u503c\uff0c\u8fd9\u5c31\u6ca1\u4ec0\u4e48\u7528\u4e86\u3002 \u4f20\u9012\u6307\u5411\u975econst\u7c7b\u578b\u7684\u5f15\u7528\u65f6\u53ea\u80fd\u63a5\u6536\u53ef\u4ee5\u4fee\u6539\u7684\u5de6\u503c\u5b9e\u53c2 \u56e0\u4e3a\u6267\u884c\u975econst\u503c\u7684\u5f15\u7528\u53ea\u80fd\u7ed1\u5b9a\u5230\u4e00\u4e2a\u53ef\u4fee\u6539\u7684 \u5de6\u503c \uff08\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u975econst\u7c7b\u578b\u53d8\u91cf\uff09\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u6309\u5f15\u7528\u4f20\u9012\u53ea\u80fd\u914d\u5408\u53ef\u4fee\u6539\u5de6\u503c\u6765\u4f7f\u7528\u3002\u4ece\u5b9e\u7528\u6027\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u8fd9\u65e0\u7591\u6781\u5927\u5730\u9650\u5236\u4e86\u6309\u503c\u4f20\u9012\u7684\u5b9e\u7528\u6027\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u80fd\u591f\u4e3a\u5176\u4f20\u9012const\u53d8\u91cf\u548c \u5b57\u9762\u91cf \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include void printValue ( int & y ) // y only accepts modifiable lvalues { std :: cout << y << '\\n' ; } int main () { int x { 5 }; printValue ( x ); // ok: x is a modifiable lvalue const int z { 5 }; printValue ( z ); // error: z is a non-modifiable lvalue printValue ( 5 ); // error: 5 is an rvalue return 0 ; } \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u6709\u66f4\u597d\u7684\u529e\u6cd5\u3002 \u4f20\u9012\u6307\u5411 const \u7684\u5f15\u7528 \u4e0d\u540c\u4e8e\u6307\u5411\u975econst\u7684\u5f15\u7528\uff08\u53ea\u80fd\u7ed1\u5b9a\u53ef\u4fee\u6539\u5de6\u503c\uff09\uff0cconst\u7c7b\u578b\u5f15\u7528\u53ef\u4ee5\u7ed1\u5b9a\u53ef\u4fee\u6539\u5de6\u503c\u3001\u4e0d\u53ef\u4fee\u6539\u5de6\u503c\u548c\u53f3\u503c\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u628a\u5f62\u53c2\u5b9a\u4e49\u4e3aconst\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u5219\u53ef\u4ee5\u5c06\u5176\u7ed1\u5b9a\u5230\u4efb\u4f55\u7c7b\u578b\u7684\u5b9e\u53c2\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include void printValue ( const int & y ) // y is now a const reference { std :: cout << y << '\\n' ; } int main () { int x { 5 }; printValue ( x ); // ok: x is a modifiable lvalue const int z { 5 }; printValue ( z ); // ok: z is a non-modifiable lvalue printValue ( 5 ); // ok: 5 is a literal rvalue return 0 ; } \u6309const\u5f15\u7528\u4f20\u9012\u63d0\u4f9b\u4e86\u548c\u6309\u5f15\u7528\u4f20\u9012\u4e00\u6837\u7684\u4f18\u70b9\uff08\u907f\u514d\u5b9e\u53c2\u62f7\u8d1d\uff09\uff0c\u540c\u65f6\u8fd8\u53ef\u4ee5\u907f\u514d\u51fd\u6570\u5bf9\u5b9e\u53c2\u8fdb\u884c\u4fee\u6539\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u662f\u65e0\u6cd5\u8fdb\u884c\u7684\uff0c\u56e0\u4e3a ref \u662f const \u7684\uff1a 1 2 3 4 void addOne ( const int & ref ) { ++ ref ; // not allowed: ref is const } \u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5176\u5b9e\u5e76\u4e0d\u5e0c\u671b\u51fd\u6570\u7684\u5b9e\u53c2\u88ab\u4fee\u6539\u3002 \u6700\u4f73\u5b9e\u8df5 \u6309\u5f15\u7528\u4f20\u9012\u6700\u597d\u5b9a\u4e49\u4e3aconst\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u9664\u975e\u4f60\u6709\u5f88\u597d\u7684\u7406\u7531\uff08\u4f8b\u5982\u51fd\u6570\u5fc5\u987b\u4fee\u6539\u5b9e\u53c2\u7684\u503c\uff09\u3002 \u73b0\u5728\uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u53ef\u4ee5\u4e86\u89e3\u652f\u6301const\u7c7b\u578b\u5de6\u503c\u5f15\u7528\u7684\u52a8\u673a\u4e86\uff0c\u5982\u679c\u6ca1\u6709\u5b83\uff0c\u6211\u4eec\u5c31\u65e0\u6cd5\u4f7f\u7528\u6309\u5f15\u7528\u4f20\u9012\u7684\u51fd\u6570\u4f20\u9012\u5b57\u9762\u91cf\uff08\u6216\u5176\u4ed6\u53f3\u503c\uff09\u3002 \u540c\u65f6\u4f7f\u7528\u6309\u503c\u4f20\u9012\u548c\u6309\u5f15\u7528\u4f20\u9012 \u5177\u6709\u591a\u4e2a\u5f62\u53c2\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u5206\u522b\u6307\u5b9a\u6bcf\u4e2a\u53c2\u6570\u662f\u6309\u503c\u4f20\u9012\u8fd8\u662f\u6309\u5f15\u7528\u4f20\u9012\uff1a \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include void foo ( int a , int & b , const std :: string & c ) { } int main () { int x { 5 }; const std :: string s { \"Hello, world!\" }; foo ( 5 , x , s ); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7b2c\u4e00\u4e2a\u5b9e\u53c2\u662f\u6309\u503c\u4f20\u9012\u7684\uff0c\u800c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5219\u662f\u6309\u5f15\u7528\u4f20\u9012\u7684\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570\u5219\u662f\u6309const\u5f15\u7528\u4f20\u9012\u7684\u3002 \u4f55\u65f6\u4f7f\u7528\u6309\u5f15\u7528\u4f20\u9012 \u56e0\u4e3a\u7c7b\u7c7b\u578b\u7684\u62f7\u8d1d\u5f00\u9500\u5f88\u5927\uff0c\u7c7b\u7c7b\u578b\u901a\u5e38\u4f1a\u6309\u5f15\u7528\u4f20\u9012\u800c\u4e0d\u662f\u6309\u503c\u4f20\u9012\u4ee5\u907f\u514d\u6602\u8d35\u7684\u62f7\u8d1d\u5f00\u9500\u3002\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u62f7\u8d1d\u5f00\u9500\u662f\u5f88\u5c0f\u7684\uff0c\u6240\u4ee5\u5b83\u4eec\u901a\u5e38\u4f1a\u6309\u503c\u4f20\u9012\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4f7f\u7528 \u6309\u503c\u4f20\u9012 \uff0c\u800c\u5bf9\u4e8e\u7c7b\u6216\u8005\u7ed3\u6784\u4f53\u4f7f\u7528const\u7684\u6309\u503c\u4f20\u9012\u3002 \u6309\u503c\u4f20\u9012\u548c\u6309\u5f15\u7528\u4f20\u9012\u7684\u5f00\u9500\u6bd4\u8f83\uff08\u8fdb\u9636\u8bdd\u9898\uff09 \u5176\u5b9e\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u7684\u7c7b\u7c7b\u578b\u90fd\u5fc5\u987b \u6309\u503c\u4f20\u9012 \u3002\u5f53\u7136\u4f60\u53ef\u80fd\u4f1a\u95ee\uff0c\u90fd\u6309\u503c\u4f20\u9012\u6709\u4f55\u4e0d\u53ef\u5462\uff1f\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u4f1a\u8be6\u7ec6\u5730\u5206\u6790\u6309\u503c\u4f20\u9012\u548c\u6309\u5f15\u7528\u4f20\u9012\u7684\u5f00\u9500\uff0c\u5e76\u63d0\u51fa\u5982\u4f55\u9009\u62e9\u4e24\u79cd\u65b9\u5f0f\u7684\u6700\u4f73\u5b9e\u8df5\u3002 \u6709\u4e24\u4e2a\u5173\u952e\u70b9\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u6309\u503c\u4f20\u9012\uff0c\u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u6309\u5f15\u7528\u4f20\u9012\uff1a \u9996\u5148\uff0c\u5bf9\u8c61\u62f7\u8d1d\u7684\u5f00\u9500\u5f88\u5927\u7a0b\u5ea6\u4e0a\u53d6\u51b3\u4e8e\u4e24\u65b9\u9762\uff1a \u5bf9\u8c61\u7684\u5927\u5c0f\u3002\u5bf9\u8c61\u5360\u7528\u8d8a\u591a\u7684\u5185\u5b58\uff0c\u62f7\u8d1d\u4e5f\u5c31\u8d8a\u8d39\u65f6\u95f4\uff1b \u5176\u4ed6\u989d\u5916\u8bbe\u7f6e\u6240\u9700\u7684\u65f6\u95f4\u3002\u6709\u4e9b\u7c7b\u4f1a\u5728\u5b9e\u4f8b\u5316\u65f6\u8fdb\u884c\u989d\u5916\u7684\u8bbe\u7f6e\u64cd\u4f5c\uff08\u4f8b\u5982\u6253\u5f00\u6587\u4ef6\u6216\u6570\u636e\u5e93\u3001\u6216\u8005\u5206\u914d\u52a8\u6001\u5185\u5b58\uff09\u3002\u8fd9\u4e9b\u64cd\u4f5c\u65e0\u7591\u4e5f\u4f1a\u5728\u5bf9\u8c61\u62f7\u8d1d\u65f6\u5360\u636e\u4e00\u5b9a\u7684\u65f6\u95f4\u3002 \u53e6\u4e00\u65b9\u9762\uff0c\u5f15\u7528\u7684\u7ed1\u5b9a\u603b\u662f\u975e\u5e38\u5feb\u7684\uff08\u51e0\u4e4e\u548c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u62f7\u8d1d\u4e00\u6837\u5feb\uff09\u3002 \u5176\u6b21\uff0c\u901a\u8fc7\u5f15\u7528\u8bbf\u95ee\u5bf9\u8c61\u6bd4\u76f4\u63a5\u8bbf\u95ee\u5bf9\u8c61\u7684\u901f\u5ea6\u7565\u6162\u3002\u4f7f\u7528\u53d8\u91cf\u6807\u8bc6\u7b26\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u8be5\u53d8\u91cf\u5bf9\u5e94\u7684\u5185\u5b58\u5730\u5740\u4ee5\u83b7\u53d6\u503c\u3002\u5bf9\u4e8e\u5f15\u7528\uff0c\u901a\u5e38\u6709\u4e00\u4e2a\u989d\u5916\u7684\u6b65\u9aa4\uff1a\u7f16\u8bd1\u5668\u5fc5\u987b\u9996\u5148\u786e\u5b9a\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\uff0c\u53ea\u6709\u8fd9\u6837\u5b83\u624d\u80fd\u5230\u8be5\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u5e76\u8bbf\u95ee\u8be5\u503c\u3002\u6b64\u5916\uff0c\u7f16\u8bd1\u5668\u5728\u4f18\u5316\u4ee3\u7801\u65f6\uff0c\u6709\u65f6\u5019\u9488\u5bf9\u6309\u503c\u4f20\u9012\u7684\u8fdb\u884c\u7684\u4f18\u5316\u4f1a\u76f8\u5bf9\u4e8e\u6309\u5f15\u7528\u4f20\u9012\u7684\u4ee3\u7801\u66f4\u9ad8\u6548\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u7f16\u8bd1\u5668\u751f\u6210\u7684\u5bf9\u8c61\u6309\u5f15\u7528\u4f20\u9012\u4ee3\u7801\u901a\u5e38\u6bd4\u5176\u751f\u6210\u7684\u5bf9\u8c61\u6309\u503c\u4f20\u9012\u4ee3\u7801\u8981\u6162\u4e00\u4e9b\u3002 \u6211\u4eec\u73b0\u5728\u53ef\u4ee5\u56de\u7b54\u4e3a\u4ec0\u4e48\u4e0d\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u6240\u6709\u5185\u5bb9\u7684\u95ee\u9898\u4e86: \u5bf9\u4e8e\u62f7\u8d1d\u5f00\u9500\u4e0d\u5927\u7684\u5bf9\u8c61\uff0c\u62f7\u8d1d\u4f20\u9012\u7684\u5f00\u9500\u548c\u5f15\u7528\u7ed1\u5b9a\u7684\u5f00\u9500\u53ef\u80fd\u5dee\u4e0d\u591a\uff0c\u6240\u4ee5\u5e94\u8be5\u4f7f\u7528\u6309\u503c\u4f20\u9012\uff0c\u4ee5\u4fbf\u7f16\u8bd1\u5668\u751f\u6210\u66f4\u9ad8\u6548\u7684\u4ee3\u7801\uff1b \u5bf9\u4e8e\u62f7\u8d1d\u5f00\u9500\u5f88\u5927\u7684\u5bf9\u8c61\uff0c\u62f7\u8d1d\u5f00\u9500\u5360\u636e\u4e86\u5927\u5934\uff0c\u6240\u4ee5\u5e94\u8be5\u4f7f\u7528\u6309\u503c\u4f20\u9012\uff08const\uff09\u6765\u907f\u514d\u62f7\u8d1d\u3002 \u8bd1\u8005\u6ce8 \u6309\u5f15\u7528\u4f20\u9012\u5141\u8bb8\u4fee\u6539\u503c\u5e76\u4e0d\u662f\u539f\u56e0\uff0c\u56e0\u4e3a\u5982\u679c\u4e0d\u5e0c\u671b\u4fee\u6539\u503c\u53ef\u4ee5\u4f20\u9012const\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u66f4\u4e0d\u7528\u8bf4const\u7c7b\u578b\u7684\u5f15\u7528\u6709\u66f4\u597d\u7684\u6cdb\u7528\u6027\u2014\u2014\u80fd\u914d\u5408\u53f3\u503c\u4f7f\u7528 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u4e8e\u62f7\u8d1d\u5f00\u9500\u5c0f\u7684\u5bf9\u8c61\uff0c\u4f7f\u7528\u6309\u503c\u4f20\u9012\uff0c\u5bf9\u4e8e\u62f7\u8d1d\u5f00\u9500\u5927\u7684\u5bf9\u8c61\uff0c\u4f7f\u7528\u6309const\u5f15\u7528\u4f20\u9012\u3002\u5982\u679c\u4e0d\u786e\u5b9a\u5f00\u9500\u5927\u8fd8\u662f\u5c0f\uff0c\u5219\u4f7f\u7528\u6309const\u5f15\u7528\u4f20\u9012\u3002 \u6700\u540e\u4e00\u4e2a\u95ee\u9898\uff0c\u201c\u62f7\u8d1d\u5f00\u9500\u5c0f\u201d\u662f\u5982\u4f55\u8861\u91cf\u7684\uff1f\u8fd9\u4e2a\u95ee\u9898\u6ca1\u6709\u7edd\u5bf9\u7684\u7b54\u6848\uff0c\u8fd9\u53d6\u51b3\u4e8e\u7f16\u8bd1\u5668\u3001\u4f7f\u7528\u573a\u666f\u548c\u67b6\u6784\u3002\u4f46\u662f\uff0c\u7ecf\u9a8c\u6cd5\u5219\u662f\u8fd9\u6837\u7684\uff1a\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u4f7f\u7528\u7684\u5185\u5b58\u4e0d\u8d85\u8fc7\u4e24\u4e2a \u5b57\u957f \uff08\u201d\u5b57\u957f\u201c\u5927\u7ea6\u662f\u5185\u5b58\u5730\u5740\u7684\u957f\u5ea6\uff09\uff0c\u800c\u4e14\u6ca1\u6709\u989d\u5916\u7684\u8bbe\u7f6e\u8fc7\u7a0b\uff0c\u5219\u53ef\u4ee5\u8ba4\u4e3a\u662f\u201d\u62f7\u8d1d\u5f00\u9500\u5c0f\u201c\u3002 \u4e0b\u9762\u8fd9\u6bb5\u7a0b\u5e8f\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53ef\u4ee5\u7528\u4e8e\u5224\u65ad\u5bf9\u8c61\u4f7f\u7528\u5185\u5b58\u662f\u5426\u5927\u4e8e\u4e24\u4e2a\u5b57\u957f\uff08 isSmall \uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include // \u5982\u679c\u7c7b\u578b\u6216\u5bf9\u8c61\u4f7f\u7528\u7684\u5806\u5185\u5b58\u4e0d\u8d85\u8fc7\u4e24\u4e2a\u5b57\u957f\u5219\u8fd4\u56detrue #define isSmall(T) (sizeof(T) <= 2 * sizeof(void*)) struct S { double a , b , c ; }; int main () { std :: cout << std :: boolalpha ; // \u5c06bool\u7c7b\u578b\u6253\u5370\u4e3atrue\u6216false\uff0c\u800c\u4e0d\u662f01 std :: cout << isSmall ( int ) << '\\n' ; // true std :: cout << isSmall ( double ) << '\\n' ; // true std :: cout << isSmall ( S ) << '\\n' ; // false return 0 ; } \u9898\u5916\u8bdd \u6211\u4eec\u5728\u8fd9\u91cc\u4f7f\u7528\u4e86\u4e00\u4e2a\u9884\u5904\u7406\u5668\u5b8f\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u7c7b\u578b\u4e2d\u8fdb\u884c\u66ff\u6362(\u666e\u901a\u51fd\u6570\u4e0d\u5141\u8bb8\u8fd9\u6837\u505a)\u3002 \u7136\u800c\uff0c\u5f88\u96be\u77e5\u9053\u7c7b\u7c7b\u578b\u5bf9\u8c61\u662f\u5426\u5177\u6709\u8bbe\u7f6e\u6210\u672c\u3002\u6700\u597d\u5047\u8bbe\u5927\u591a\u6570\u6807\u51c6\u5e93\u7c7b\u90fd\u6709\u8bbe\u7f6e\u6210\u672c\uff0c\u9664\u975e\u4f60\u77e5\u9053\u5b83\u4eec\u6ca1\u6709\u3002 \u5c0f\u8d34\u58eb \u5982\u679c sizeof(T) <= 2 * sizeof(void*) \u800c\u4e14\u7c7b\u578bT\u6ca1\u6709\u989d\u5916\u7684\u8bbe\u7f6e\u64cd\u4f5c\uff0c\u5219\u53ef\u4ee5\u8ba4\u4e3aT\u7684\u62f7\u8d1d\u5f00\u9500\u5c0f\u3002 \u62f7\u8d1d\u5f00\u9500\u5c0f\u7684\u7c7b\u578b\u901a\u5e38\u6709\uff1a\u6240\u6709\u57fa\u672c\u7c7b\u578b\u3001\u679a\u4e3e\u7c7b\u578b\u548c std::string_view . \u62f7\u8d1d\u5f00\u9500\u5927\u7684\u7c7b\u578b\u901a\u5e38\u6709\uff1a std::array , std::string , std::vector , \u548c std::ostream .","title":"9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528"},{"location":"9-5-Pass-by-lvalue-reference/#_1","text":"\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u591a\u6570\u6570\u636e\u7c7b\u578b\u90fd\u5c5e\u4e8e\u7c7b(\u4f8b\u5982 std::string )\u3002\u7c7b\u7c7b\u578b\u7684\u62f7\u8d1d\u5f00\u9500\u901a\u5e38\u662f\u5f88\u5927\u7684\u3002\u5982\u679c\u53ef\u80fd\uff0c\u5e94\u8be5\u5c3d\u53ef\u80fd\u5730\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\uff0c\u5c24\u5176\u662f\u5728\u8fd9\u4e9b\u53d8\u91cf\u7528\u5b8c\u5373\u5f03\u7684\u60c5\u51b5\u4e0b\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u6b63\u662f\u8868\u660e\u4e86\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include void printValue ( std :: string y ) { std :: cout << y << '\\n' ; } // y is destroyed here int main () { std :: string x { \"Hello, world!\" }; // x is a std::string printValue ( x ); // x is passed by value (copied) into parameter y (expensive) return 0 ; } \u7a0b\u5e8f\u4f1a\u6253\u5370\uff1a 1 Hello, world! \u5c3d\u7ba1\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u548c\u6211\u4eec\u60f3\u7684\u4e00\u6837\uff0c\u4f46\u5176\u6548\u7387\u662f\u5f88\u4f4e\u7684\u3002\u548c\u524d\u9762\u7684\u7a0b\u5e8f\u4e00\u6837\u5f53\u6211\u4eec\u8c03\u7528 printValue() \u7684\u65f6\u5019\uff0c\u5b9e\u53c2 x \u4f1a\u88ab\u62f7\u8d1d\u5230\u5f62\u53c2 y \u3002\u4e0d\u8fc7\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b9e\u53c2 std::string \u5e76\u4e0d\u50cf int \u90a3\u6837\uff0c std::string \u662f\u4e00\u79cd\u7c7b\u7c7b\u578b\uff0c\u5bf9\u5b83\u7684\u62f7\u8d1d\u5f00\u9500\u662f\u975e\u5e38\u5927\u7684\uff0c\u66f4\u4e0d\u7528\u8bf4\u6bcf\u6b21\u8c03\u7528 printValue() \u90fd\u4f1a\u4ea7\u751f\u8fd9\u6837\u7684\u5f00\u9500\u3002 \u6211\u4eec\u53ef\u4ee5\u505a\u7684\u66f4\u597d\u3002","title":"\u6709\u4e9b\u5bf9\u8c61\u7684\u62f7\u8d1d\u5f00\u9500\u5f88\u5927"},{"location":"9-5-Pass-by-lvalue-reference/#_2","text":"\u907f\u514d\u51fd\u6570\u8c03\u7528\u65f6\u4ea7\u751f\u7684\u62f7\u8d1d\u5f00\u9500\uff0c\u53ef\u901a\u8fc7 \u6309\u5f15\u7528\u4f20\u9012 \u6765\u4ee3\u66ff \u6309\u503c\u4f20\u9012 \u3002\u5f53\u4f7f\u7528\u6309\u5f15\u7528\u4f20\u9012\u65f6\uff0c\u6211\u4eec\u5c06\u51fd\u6570\u7684\u5f62\u53c2\u58f0\u660e\u4e3a\u5f15\u7528\u7c7b\u578b\uff08\u6216\u6307\u5411const\u7684\u5f15\u7528\uff09\u800c\u4e0d\u662f\u666e\u901a\u7c7b\u578b\u3002\u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u6bcf\u4e2a\u5f15\u7528\u7c7b\u578b\u7684\u5f62\u53c2\u4f1a\u88ab\u7ed1\u5b9a\u5230\u4f20\u5165\u7684\u5b9e\u53c2\u3002\u56e0\u4e3a\u5f15\u7528\u5176\u5b9e\u662f\u5b9e\u53c2\u7684\u522b\u540d\uff0c\u6240\u4ee5\u5e76\u4e0d\u4f1a\u521b\u5efa\u62f7\u8d1d\u3002 \u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u548c\u4e0a\u9762\u7684\u4f8b\u5b50\u662f\u4e00\u81f4\u7684\uff0c\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\u5176\u4f7f\u7528\u4e86\u6309\u5f15\u7528\u4f20\u9012\u800c\u4e0d\u662f\u6309\u503c\u51fa\u4f20\u9012\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include void printValue ( std :: string & y ) // type changed to std::string& { std :: cout << y << '\\n' ; } // y is destroyed here int main () { std :: string x { \"Hello, world!\" }; printValue ( x ); // x is now passed by reference into reference parameter y (inexpensive) return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u548c\u4e0a\u9762\u7684\u7a0b\u5e8f\u6bcf\u592a\u5927\u533a\u522b\uff0c\u9664\u4e86\u5f62\u53c2 y \u7684\u7c7b\u578b\u4ece std::string \u88ab\u6539\u4e3a std::string& (\u5de6\u503c\u5f15\u7528)\u3002\u73b0\u5728\uff0c\u5f53 printValue(x) \u88ab\u8c03\u7528\u7684\u65f6\u5019\uff0c\u5de6\u503c\u5f15\u7528\u7c7b\u578b\u7684\u5f62\u53c2 y \u88ab\u7ed1\u5b9a\u5230\u5b9e\u53c2 x \u3002\u5f15\u7528\u7684\u7ed1\u5b9a\u5f00\u9500\u603b\u662f\u5f88\u5c0f\u7684\uff0c\u5b83\u4e0d\u9700\u8981\u521b\u5efa x \u7684\u62f7\u8d1d\u3002\u56e0\u4e3a\u5f15\u7528\u662f\u88ab\u5f15\u7528\u5bf9\u8c61\u7684\u522b\u540d\uff0c\u6240\u4ee5\u5f53 printValue() \u4f7f\u7528 y \u65f6\uff0c\u5b83\u8bbf\u95ee\u7684\u662f\u5b9e\u53c2 x \u672c\u8eab\uff08\u800c\u4e0d\u662f x \u7684\u62f7\u8d1d\uff09\u3002 \u5173\u952e\u4fe1\u606f \u6309\u5f15\u7528\u4f20\u9012\u5141\u8bb8\u6211\u4eec\u5411\u51fd\u6570\u4f20\u9012\u53c2\u6570\u800c\u4e0d\u9700\u521b\u5efa\u62f7\u8d1d\u3002","title":"\u6309\u5f15\u7528\u4f20\u9012"},{"location":"9-5-Pass-by-lvalue-reference/#_3","text":"\u5f53\u4e00\u4e2a\u5bf9\u8c61\u901a\u8fc7\u6309\u503c\u4f20\u9012\u7684\u65b9\u5f0f\u4f20\u5165\u51fd\u6570\u65f6\uff0c\u51fd\u6570\u63a5\u6536\u5230\u7684\u662f\u5176\u62f7\u8d1d\u3002\u8fd9\u4e5f\u5c31\u610f\u5473\u7740\u4efb\u4f55\u5bf9\u5f62\u53c2\u7684\u4fee\u6539\u90fd\u662f\u5bf9\u8fd9\u4efd\u62f7\u8d1d\u7684\u4fee\u6539\uff0c\u800c\u4e0d\u662f\u5b9e\u53c2\u672c\u8eab\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void addOne ( int y ) // y is a copy of x { ++ y ; // this modifies the copy of x, not the actual object x } int main () { int x { 5 }; std :: cout << \"value = \" << x << '\\n' ; addOne ( x ); std :: cout << \"value = \" << x << '\\n' ; // x has not been modified return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u5f62\u53c2 y \u662f\u5b9e\u53c2 x \u7684\u62f7\u8d1d\uff0c\u5f53\u6211\u4eec\u5bf9 y \u8fdb\u884c\u9012\u589e\u65f6\uff0c\u5b83\u53ea\u4f1a\u5f71\u54cd\u5230 y \uff0c\u56e0\u6b64\u7a0b\u5e8f\u7684\u8f93\u51fa\u4e3a\uff1a 1 2 value = 5 value = 5 \u7136\u800c\uff0c\u7531\u4e8e\u5f15\u7528\u6765\u8bf4\uff0c\u56e0\u4e3a\u5b83\u5c31\u662f\u88ab\u5f15\u7528\u5bf9\u8c61\u672c\u8eab\uff0c\u5f53\u4f7f\u7528\u6309\u5f15\u7528\u4f20\u9012\u65f6\uff0c\u5bf9\u5f15\u7528\u5f62\u53c2\u7684\u4efb\u4f55\u66f4\u6539\u90fd\u5c06\u5f71\u54cd\u5b9e\u53c2\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void addOne ( int & y ) // y is bound to the actual object x { ++ y ; // this modifies the actual object x } int main () { int x { 5 }; std :: cout << \"value = \" << x << '\\n' ; addOne ( x ); std :: cout << \"value = \" << x << '\\n' ; // x has been modified return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u4e3a\uff1a 1 2 value = 5 value = 6 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c x \u9996\u5148\u88ab\u521d\u59cb\u5316\u4e3a 5\u3002\u5f53 addOne(x) \u88ab\u8c03\u7528\u65f6\uff0c\u5f15\u7528\u7c7b\u578b\u5f62\u53c2 y \u4f1a\u88ab\u7ed1\u5b9a\u5230\u5b9e\u53c2 x \u3002\u5f53\u6211\u4eec\u5728 addOne() \u51fd\u6570\u4e2d\u5bf9 y \u8fdb\u884c\u9012\u589e\u65f6\uff0c\u5b83\u5b9e\u9645\u4fee\u6539\u4e86\u5b9e\u53c2 x \u7684\u503c\uff08\u800c\u4e0d\u662f\u5b83\u7684\u62f7\u8d1d\uff09\u3002\u56e0\u6b64\u5f53 addOne() \u8c03\u7528\u7ed3\u675f\u540e\u53d8\u91cf\u7684\u503c\u4ecd\u7136\u662f\u88ab\u4fee\u6539\u4e86\u3002 \u5173\u952e\u4fe1\u606f \u901a\u8fc7\u6309\u503c\u4f20\u9012\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u4fee\u6539\u4f20\u5165\u7684\u5b9e\u53c2\u3002 \u51fd\u6570\u4fee\u6539\u4f20\u5165\u53c2\u6570\u503c\u7684\u80fd\u529b\u662f\u5f88\u6709\u7528\u7684\u3002\u5047\u8bbe\u4f60\u7f16\u5199\u4e86\u4e00\u4e2a\u51fd\u6570\u6765\u5224\u65ad\u602a\u7269\u662f\u5426\u6210\u529f\u653b\u51fb\u4e86\u73a9\u5bb6\u3002\u5982\u679c\u662f\u8fd9\u6837\uff0c\u602a\u7269\u5e94\u8be5\u5bf9\u73a9\u5bb6\u7684\u751f\u547d\u503c\u9020\u6210\u4e00\u5b9a\u7684\u4f24\u5bb3\u3002\u5982\u679c\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u73a9\u5bb6\u5bf9\u8c61\uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539\u4f20\u5165\u7684\u5b9e\u9645\u73a9\u5bb6\u5bf9\u8c61\u7684\u5065\u5eb7\u72b6\u51b5\u3002\u5982\u679c\u4f60\u901a\u8fc7\u503c\u4f20\u9012\u73a9\u5bb6\u5bf9\u8c61\uff0c\u4f60\u53ea\u80fd\u4fee\u6539\u73a9\u5bb6\u5bf9\u8c61\u526f\u672c\u7684\u5065\u5eb7\u503c\uff0c\u8fd9\u5c31\u6ca1\u4ec0\u4e48\u7528\u4e86\u3002","title":"\u6309\u5f15\u7528\u4f20\u9012\u5141\u8bb8\u51fd\u6570\u4fee\u6539\u5b9e\u53c2\u7684\u503c"},{"location":"9-5-Pass-by-lvalue-reference/#const","text":"\u56e0\u4e3a\u6267\u884c\u975econst\u503c\u7684\u5f15\u7528\u53ea\u80fd\u7ed1\u5b9a\u5230\u4e00\u4e2a\u53ef\u4fee\u6539\u7684 \u5de6\u503c \uff08\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u975econst\u7c7b\u578b\u53d8\u91cf\uff09\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u6309\u5f15\u7528\u4f20\u9012\u53ea\u80fd\u914d\u5408\u53ef\u4fee\u6539\u5de6\u503c\u6765\u4f7f\u7528\u3002\u4ece\u5b9e\u7528\u6027\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u8fd9\u65e0\u7591\u6781\u5927\u5730\u9650\u5236\u4e86\u6309\u503c\u4f20\u9012\u7684\u5b9e\u7528\u6027\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u80fd\u591f\u4e3a\u5176\u4f20\u9012const\u53d8\u91cf\u548c \u5b57\u9762\u91cf \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include void printValue ( int & y ) // y only accepts modifiable lvalues { std :: cout << y << '\\n' ; } int main () { int x { 5 }; printValue ( x ); // ok: x is a modifiable lvalue const int z { 5 }; printValue ( z ); // error: z is a non-modifiable lvalue printValue ( 5 ); // error: 5 is an rvalue return 0 ; } \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u6709\u66f4\u597d\u7684\u529e\u6cd5\u3002","title":"\u4f20\u9012\u6307\u5411\u975econst\u7c7b\u578b\u7684\u5f15\u7528\u65f6\u53ea\u80fd\u63a5\u6536\u53ef\u4ee5\u4fee\u6539\u7684\u5de6\u503c\u5b9e\u53c2"},{"location":"9-5-Pass-by-lvalue-reference/#const_1","text":"\u4e0d\u540c\u4e8e\u6307\u5411\u975econst\u7684\u5f15\u7528\uff08\u53ea\u80fd\u7ed1\u5b9a\u53ef\u4fee\u6539\u5de6\u503c\uff09\uff0cconst\u7c7b\u578b\u5f15\u7528\u53ef\u4ee5\u7ed1\u5b9a\u53ef\u4fee\u6539\u5de6\u503c\u3001\u4e0d\u53ef\u4fee\u6539\u5de6\u503c\u548c\u53f3\u503c\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u628a\u5f62\u53c2\u5b9a\u4e49\u4e3aconst\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u5219\u53ef\u4ee5\u5c06\u5176\u7ed1\u5b9a\u5230\u4efb\u4f55\u7c7b\u578b\u7684\u5b9e\u53c2\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include void printValue ( const int & y ) // y is now a const reference { std :: cout << y << '\\n' ; } int main () { int x { 5 }; printValue ( x ); // ok: x is a modifiable lvalue const int z { 5 }; printValue ( z ); // ok: z is a non-modifiable lvalue printValue ( 5 ); // ok: 5 is a literal rvalue return 0 ; } \u6309const\u5f15\u7528\u4f20\u9012\u63d0\u4f9b\u4e86\u548c\u6309\u5f15\u7528\u4f20\u9012\u4e00\u6837\u7684\u4f18\u70b9\uff08\u907f\u514d\u5b9e\u53c2\u62f7\u8d1d\uff09\uff0c\u540c\u65f6\u8fd8\u53ef\u4ee5\u907f\u514d\u51fd\u6570\u5bf9\u5b9e\u53c2\u8fdb\u884c\u4fee\u6539\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u662f\u65e0\u6cd5\u8fdb\u884c\u7684\uff0c\u56e0\u4e3a ref \u662f const \u7684\uff1a 1 2 3 4 void addOne ( const int & ref ) { ++ ref ; // not allowed: ref is const } \u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5176\u5b9e\u5e76\u4e0d\u5e0c\u671b\u51fd\u6570\u7684\u5b9e\u53c2\u88ab\u4fee\u6539\u3002 \u6700\u4f73\u5b9e\u8df5 \u6309\u5f15\u7528\u4f20\u9012\u6700\u597d\u5b9a\u4e49\u4e3aconst\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u9664\u975e\u4f60\u6709\u5f88\u597d\u7684\u7406\u7531\uff08\u4f8b\u5982\u51fd\u6570\u5fc5\u987b\u4fee\u6539\u5b9e\u53c2\u7684\u503c\uff09\u3002 \u73b0\u5728\uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u53ef\u4ee5\u4e86\u89e3\u652f\u6301const\u7c7b\u578b\u5de6\u503c\u5f15\u7528\u7684\u52a8\u673a\u4e86\uff0c\u5982\u679c\u6ca1\u6709\u5b83\uff0c\u6211\u4eec\u5c31\u65e0\u6cd5\u4f7f\u7528\u6309\u5f15\u7528\u4f20\u9012\u7684\u51fd\u6570\u4f20\u9012\u5b57\u9762\u91cf\uff08\u6216\u5176\u4ed6\u53f3\u503c\uff09\u3002","title":"\u4f20\u9012\u6307\u5411 const \u7684\u5f15\u7528"},{"location":"9-5-Pass-by-lvalue-reference/#_4","text":"\u5177\u6709\u591a\u4e2a\u5f62\u53c2\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u5206\u522b\u6307\u5b9a\u6bcf\u4e2a\u53c2\u6570\u662f\u6309\u503c\u4f20\u9012\u8fd8\u662f\u6309\u5f15\u7528\u4f20\u9012\uff1a \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include void foo ( int a , int & b , const std :: string & c ) { } int main () { int x { 5 }; const std :: string s { \"Hello, world!\" }; foo ( 5 , x , s ); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7b2c\u4e00\u4e2a\u5b9e\u53c2\u662f\u6309\u503c\u4f20\u9012\u7684\uff0c\u800c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5219\u662f\u6309\u5f15\u7528\u4f20\u9012\u7684\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570\u5219\u662f\u6309const\u5f15\u7528\u4f20\u9012\u7684\u3002","title":"\u540c\u65f6\u4f7f\u7528\u6309\u503c\u4f20\u9012\u548c\u6309\u5f15\u7528\u4f20\u9012"},{"location":"9-5-Pass-by-lvalue-reference/#_5","text":"\u56e0\u4e3a\u7c7b\u7c7b\u578b\u7684\u62f7\u8d1d\u5f00\u9500\u5f88\u5927\uff0c\u7c7b\u7c7b\u578b\u901a\u5e38\u4f1a\u6309\u5f15\u7528\u4f20\u9012\u800c\u4e0d\u662f\u6309\u503c\u4f20\u9012\u4ee5\u907f\u514d\u6602\u8d35\u7684\u62f7\u8d1d\u5f00\u9500\u3002\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u62f7\u8d1d\u5f00\u9500\u662f\u5f88\u5c0f\u7684\uff0c\u6240\u4ee5\u5b83\u4eec\u901a\u5e38\u4f1a\u6309\u503c\u4f20\u9012\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4f7f\u7528 \u6309\u503c\u4f20\u9012 \uff0c\u800c\u5bf9\u4e8e\u7c7b\u6216\u8005\u7ed3\u6784\u4f53\u4f7f\u7528const\u7684\u6309\u503c\u4f20\u9012\u3002","title":"\u4f55\u65f6\u4f7f\u7528\u6309\u5f15\u7528\u4f20\u9012"},{"location":"9-5-Pass-by-lvalue-reference/#_6","text":"\u5176\u5b9e\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u7684\u7c7b\u7c7b\u578b\u90fd\u5fc5\u987b \u6309\u503c\u4f20\u9012 \u3002\u5f53\u7136\u4f60\u53ef\u80fd\u4f1a\u95ee\uff0c\u90fd\u6309\u503c\u4f20\u9012\u6709\u4f55\u4e0d\u53ef\u5462\uff1f\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u4f1a\u8be6\u7ec6\u5730\u5206\u6790\u6309\u503c\u4f20\u9012\u548c\u6309\u5f15\u7528\u4f20\u9012\u7684\u5f00\u9500\uff0c\u5e76\u63d0\u51fa\u5982\u4f55\u9009\u62e9\u4e24\u79cd\u65b9\u5f0f\u7684\u6700\u4f73\u5b9e\u8df5\u3002 \u6709\u4e24\u4e2a\u5173\u952e\u70b9\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u6309\u503c\u4f20\u9012\uff0c\u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u6309\u5f15\u7528\u4f20\u9012\uff1a \u9996\u5148\uff0c\u5bf9\u8c61\u62f7\u8d1d\u7684\u5f00\u9500\u5f88\u5927\u7a0b\u5ea6\u4e0a\u53d6\u51b3\u4e8e\u4e24\u65b9\u9762\uff1a \u5bf9\u8c61\u7684\u5927\u5c0f\u3002\u5bf9\u8c61\u5360\u7528\u8d8a\u591a\u7684\u5185\u5b58\uff0c\u62f7\u8d1d\u4e5f\u5c31\u8d8a\u8d39\u65f6\u95f4\uff1b \u5176\u4ed6\u989d\u5916\u8bbe\u7f6e\u6240\u9700\u7684\u65f6\u95f4\u3002\u6709\u4e9b\u7c7b\u4f1a\u5728\u5b9e\u4f8b\u5316\u65f6\u8fdb\u884c\u989d\u5916\u7684\u8bbe\u7f6e\u64cd\u4f5c\uff08\u4f8b\u5982\u6253\u5f00\u6587\u4ef6\u6216\u6570\u636e\u5e93\u3001\u6216\u8005\u5206\u914d\u52a8\u6001\u5185\u5b58\uff09\u3002\u8fd9\u4e9b\u64cd\u4f5c\u65e0\u7591\u4e5f\u4f1a\u5728\u5bf9\u8c61\u62f7\u8d1d\u65f6\u5360\u636e\u4e00\u5b9a\u7684\u65f6\u95f4\u3002 \u53e6\u4e00\u65b9\u9762\uff0c\u5f15\u7528\u7684\u7ed1\u5b9a\u603b\u662f\u975e\u5e38\u5feb\u7684\uff08\u51e0\u4e4e\u548c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u62f7\u8d1d\u4e00\u6837\u5feb\uff09\u3002 \u5176\u6b21\uff0c\u901a\u8fc7\u5f15\u7528\u8bbf\u95ee\u5bf9\u8c61\u6bd4\u76f4\u63a5\u8bbf\u95ee\u5bf9\u8c61\u7684\u901f\u5ea6\u7565\u6162\u3002\u4f7f\u7528\u53d8\u91cf\u6807\u8bc6\u7b26\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u8be5\u53d8\u91cf\u5bf9\u5e94\u7684\u5185\u5b58\u5730\u5740\u4ee5\u83b7\u53d6\u503c\u3002\u5bf9\u4e8e\u5f15\u7528\uff0c\u901a\u5e38\u6709\u4e00\u4e2a\u989d\u5916\u7684\u6b65\u9aa4\uff1a\u7f16\u8bd1\u5668\u5fc5\u987b\u9996\u5148\u786e\u5b9a\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\uff0c\u53ea\u6709\u8fd9\u6837\u5b83\u624d\u80fd\u5230\u8be5\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u5e76\u8bbf\u95ee\u8be5\u503c\u3002\u6b64\u5916\uff0c\u7f16\u8bd1\u5668\u5728\u4f18\u5316\u4ee3\u7801\u65f6\uff0c\u6709\u65f6\u5019\u9488\u5bf9\u6309\u503c\u4f20\u9012\u7684\u8fdb\u884c\u7684\u4f18\u5316\u4f1a\u76f8\u5bf9\u4e8e\u6309\u5f15\u7528\u4f20\u9012\u7684\u4ee3\u7801\u66f4\u9ad8\u6548\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u7f16\u8bd1\u5668\u751f\u6210\u7684\u5bf9\u8c61\u6309\u5f15\u7528\u4f20\u9012\u4ee3\u7801\u901a\u5e38\u6bd4\u5176\u751f\u6210\u7684\u5bf9\u8c61\u6309\u503c\u4f20\u9012\u4ee3\u7801\u8981\u6162\u4e00\u4e9b\u3002 \u6211\u4eec\u73b0\u5728\u53ef\u4ee5\u56de\u7b54\u4e3a\u4ec0\u4e48\u4e0d\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u6240\u6709\u5185\u5bb9\u7684\u95ee\u9898\u4e86: \u5bf9\u4e8e\u62f7\u8d1d\u5f00\u9500\u4e0d\u5927\u7684\u5bf9\u8c61\uff0c\u62f7\u8d1d\u4f20\u9012\u7684\u5f00\u9500\u548c\u5f15\u7528\u7ed1\u5b9a\u7684\u5f00\u9500\u53ef\u80fd\u5dee\u4e0d\u591a\uff0c\u6240\u4ee5\u5e94\u8be5\u4f7f\u7528\u6309\u503c\u4f20\u9012\uff0c\u4ee5\u4fbf\u7f16\u8bd1\u5668\u751f\u6210\u66f4\u9ad8\u6548\u7684\u4ee3\u7801\uff1b \u5bf9\u4e8e\u62f7\u8d1d\u5f00\u9500\u5f88\u5927\u7684\u5bf9\u8c61\uff0c\u62f7\u8d1d\u5f00\u9500\u5360\u636e\u4e86\u5927\u5934\uff0c\u6240\u4ee5\u5e94\u8be5\u4f7f\u7528\u6309\u503c\u4f20\u9012\uff08const\uff09\u6765\u907f\u514d\u62f7\u8d1d\u3002 \u8bd1\u8005\u6ce8 \u6309\u5f15\u7528\u4f20\u9012\u5141\u8bb8\u4fee\u6539\u503c\u5e76\u4e0d\u662f\u539f\u56e0\uff0c\u56e0\u4e3a\u5982\u679c\u4e0d\u5e0c\u671b\u4fee\u6539\u503c\u53ef\u4ee5\u4f20\u9012const\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u66f4\u4e0d\u7528\u8bf4const\u7c7b\u578b\u7684\u5f15\u7528\u6709\u66f4\u597d\u7684\u6cdb\u7528\u6027\u2014\u2014\u80fd\u914d\u5408\u53f3\u503c\u4f7f\u7528 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u4e8e\u62f7\u8d1d\u5f00\u9500\u5c0f\u7684\u5bf9\u8c61\uff0c\u4f7f\u7528\u6309\u503c\u4f20\u9012\uff0c\u5bf9\u4e8e\u62f7\u8d1d\u5f00\u9500\u5927\u7684\u5bf9\u8c61\uff0c\u4f7f\u7528\u6309const\u5f15\u7528\u4f20\u9012\u3002\u5982\u679c\u4e0d\u786e\u5b9a\u5f00\u9500\u5927\u8fd8\u662f\u5c0f\uff0c\u5219\u4f7f\u7528\u6309const\u5f15\u7528\u4f20\u9012\u3002 \u6700\u540e\u4e00\u4e2a\u95ee\u9898\uff0c\u201c\u62f7\u8d1d\u5f00\u9500\u5c0f\u201d\u662f\u5982\u4f55\u8861\u91cf\u7684\uff1f\u8fd9\u4e2a\u95ee\u9898\u6ca1\u6709\u7edd\u5bf9\u7684\u7b54\u6848\uff0c\u8fd9\u53d6\u51b3\u4e8e\u7f16\u8bd1\u5668\u3001\u4f7f\u7528\u573a\u666f\u548c\u67b6\u6784\u3002\u4f46\u662f\uff0c\u7ecf\u9a8c\u6cd5\u5219\u662f\u8fd9\u6837\u7684\uff1a\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u4f7f\u7528\u7684\u5185\u5b58\u4e0d\u8d85\u8fc7\u4e24\u4e2a \u5b57\u957f \uff08\u201d\u5b57\u957f\u201c\u5927\u7ea6\u662f\u5185\u5b58\u5730\u5740\u7684\u957f\u5ea6\uff09\uff0c\u800c\u4e14\u6ca1\u6709\u989d\u5916\u7684\u8bbe\u7f6e\u8fc7\u7a0b\uff0c\u5219\u53ef\u4ee5\u8ba4\u4e3a\u662f\u201d\u62f7\u8d1d\u5f00\u9500\u5c0f\u201c\u3002 \u4e0b\u9762\u8fd9\u6bb5\u7a0b\u5e8f\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53ef\u4ee5\u7528\u4e8e\u5224\u65ad\u5bf9\u8c61\u4f7f\u7528\u5185\u5b58\u662f\u5426\u5927\u4e8e\u4e24\u4e2a\u5b57\u957f\uff08 isSmall \uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include // \u5982\u679c\u7c7b\u578b\u6216\u5bf9\u8c61\u4f7f\u7528\u7684\u5806\u5185\u5b58\u4e0d\u8d85\u8fc7\u4e24\u4e2a\u5b57\u957f\u5219\u8fd4\u56detrue #define isSmall(T) (sizeof(T) <= 2 * sizeof(void*)) struct S { double a , b , c ; }; int main () { std :: cout << std :: boolalpha ; // \u5c06bool\u7c7b\u578b\u6253\u5370\u4e3atrue\u6216false\uff0c\u800c\u4e0d\u662f01 std :: cout << isSmall ( int ) << '\\n' ; // true std :: cout << isSmall ( double ) << '\\n' ; // true std :: cout << isSmall ( S ) << '\\n' ; // false return 0 ; } \u9898\u5916\u8bdd \u6211\u4eec\u5728\u8fd9\u91cc\u4f7f\u7528\u4e86\u4e00\u4e2a\u9884\u5904\u7406\u5668\u5b8f\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u7c7b\u578b\u4e2d\u8fdb\u884c\u66ff\u6362(\u666e\u901a\u51fd\u6570\u4e0d\u5141\u8bb8\u8fd9\u6837\u505a)\u3002 \u7136\u800c\uff0c\u5f88\u96be\u77e5\u9053\u7c7b\u7c7b\u578b\u5bf9\u8c61\u662f\u5426\u5177\u6709\u8bbe\u7f6e\u6210\u672c\u3002\u6700\u597d\u5047\u8bbe\u5927\u591a\u6570\u6807\u51c6\u5e93\u7c7b\u90fd\u6709\u8bbe\u7f6e\u6210\u672c\uff0c\u9664\u975e\u4f60\u77e5\u9053\u5b83\u4eec\u6ca1\u6709\u3002 \u5c0f\u8d34\u58eb \u5982\u679c sizeof(T) <= 2 * sizeof(void*) \u800c\u4e14\u7c7b\u578bT\u6ca1\u6709\u989d\u5916\u7684\u8bbe\u7f6e\u64cd\u4f5c\uff0c\u5219\u53ef\u4ee5\u8ba4\u4e3aT\u7684\u62f7\u8d1d\u5f00\u9500\u5c0f\u3002 \u62f7\u8d1d\u5f00\u9500\u5c0f\u7684\u7c7b\u578b\u901a\u5e38\u6709\uff1a\u6240\u6709\u57fa\u672c\u7c7b\u578b\u3001\u679a\u4e3e\u7c7b\u578b\u548c std::string_view . \u62f7\u8d1d\u5f00\u9500\u5927\u7684\u7c7b\u578b\u901a\u5e38\u6709\uff1a std::array , std::string , std::vector , \u548c std::ostream .","title":"\u6309\u503c\u4f20\u9012\u548c\u6309\u5f15\u7528\u4f20\u9012\u7684\u5f00\u9500\u6bd4\u8f83\uff08\u8fdb\u9636\u8bdd\u9898\uff09"},{"location":"9-6-Introduction-to-pointers/","tags":["pointer"],"text":"Key Takeaway \u6307\u9488\u548c\u5f15\u7528\u4e4b\u95f4\u8fd8\u6709\u4e00\u4e9b\u503c\u5f97\u4e00\u63d0\u7684\u533a\u522b\uff1a \u5f15\u7528\u5fc5\u987b\u88ab\u521d\u59cb\u5316\uff0c\u6307\u9488\u5e76\u4e0d\u662f\u5fc5\u987b\u521d\u59cb\u5316\uff08\u4f46\u662f\u4f60\u5e94\u8be5\u521d\u59cb\u5316\uff09\uff1b \u5f15\u7528\u4e0d\u662f\u5bf9\u8c61\uff0c\u6307\u9488\u662f\u5bf9\u8c61\uff1b \u5f15\u7528\u4e0d\u80fd\u88ab\u91cd\u65b0\u8bbe\u7f6e\uff08\u4fee\u6539\u5f15\u7528\u4f7f\u5176\u7ed1\u5b9a\u5230\u5176\u4ed6\u5bf9\u8c61\uff09\uff0c\u6307\u9488\u5219\u53ef\u4ee5\u88ab\u4fee\u6539\u6307\u5411\u5176\u4ed6\u5bf9\u8c61\uff1b \u5f15\u7528\u603b\u662f\u7ed1\u5b9a\u5230\u67d0\u4e2a\u5bf9\u8c61\uff0c\u6307\u9488\u53ef\u4ee5\u4e0d\u6267\u884c\u4efb\u4f55\u5bf9\u8c61\uff1b \u5f15\u7528\u662f\u201d\u5b89\u5168\u201c\u7684\uff08\u9664\u4e86 \u60ac\u5782 \u5f15\u7528\uff09\uff0c\u6307\u9488\u5219\u662f\u5371\u9669\u7684\u3002 \u6307\u9488\u662f C++ \u4e2d\u7684\u5386\u53f2\u9057\u7559\u4ea7\u7269\u4e4b\u4e00\uff0c\u4e5f\u662f\u5f88\u591a\u4eba\u5b66\u4e60C++\u65f6\u5bb9\u6613\u5361\u58f3\u7684\u5730\u65b9\u3002\u4f46\u662f\uff0c\u7a0d\u540e\u4f60\u4f1a\u770b\u5230\uff0c\u5176\u5b9e\u6307\u9488\u5e76\u4e0d\u53ef\u6015\u3002 \u5b9e\u9645\u4e0a\uff0c\u6307\u9488\u7684\u884c\u4e3a\u548c \u5de6\u503c\u5f15\u7528 \u662f\u5f88\u50cf\u7684\u3002\u4f46\u662f\u5728\u6211\u4eec\u7ee7\u7eed\u4ecb\u7ecd\u4e4b\u524d\uff0c\u5148\u6765\u5de6\u4e00\u4e9b\u51c6\u5907\u5de5\u4f5c\u3002 \u76f8\u5173\u5185\u5bb9 \u5982\u679c\u4f60\u8fd8\u4e0d\u719f\u6089\u5de6\u503c\u5f15\u7528\uff0c\u73b0\u5728\u662f\u590d\u4e60\u7684\u597d\u65f6\u673a\u3002 9.3 - \u5de6\u503c\u5f15\u7528 \u3001 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 \u548c 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 \u90fd\u662f\u975e\u5e38\u6709\u5e2e\u52a9\u7684\u6750\u6599\u3002 \u8003\u8651\u4e0b\u4e0b\u9762\u4ee3\u7801\uff1a 1 char x {}; // chars use 1 byte of memory \u8fd9\u884c\u5b9a\u4e49\u5728\u751f\u6210\u4ee3\u7801\u65f6\uff0cRAM\u4e2d\u7684\u4e00\u6bb5\u5185\u5b58\u5c31\u4f1a\u88ab\u6307\u5b9a\u7ed9\u8be5\u5bf9\u8c61\u3002\u5bf9\u4e8e\u8fd9\u4e2a\u4f8b\u5b50\u6765\u8bf4\uff0c\u5047\u8bbe\u53d8\u91cfx\u6240\u7528\u7684\u5185\u5b58\u5730\u5740\u4e3a140\u3002\u90a3\u4e48\u6bcf\u5f53\u6211\u4eec\u5728\u8868\u8fbe\u5f0f\u6216\u8005\u8bed\u53e5\u4e2d\u4f7f\u7528x\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u81ea\u52a8\u5730\u5230\u5185\u5b58\u5730\u5740140\u7684\u5730\u65b9\u53bb\u53d6\u503c\u3002 \u8fd9\u4e48\u505a\u6700\u5927\u7684\u597d\u5904\u662f\u6211\u4eec\u65e0\u9700\u5173\u5fc3\u5185\u5b58\u5730\u5740\u7684\u6307\u5b9a\u548c\u4f7f\u7528\uff0c\u4e5f\u4e0d\u9700\u8981\u5173\u5fc3\u5b58\u653e\u5bf9\u8c61\u503c\u4f7f\u7528\u4e86\u591a\u5c11\u4f60\u6751\u3002\u6211\u4eec\u53ea\u9700\u8981\u901a\u8fc7\u53d8\u91cf\u7684 \u6807\u8bc6\u7b26(identifier) \u4e5f\u5c31\u662f\u53d8\u91cf\u540d\u5373\u53ef\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u5c06\u5176\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u5185\u5b58\u5730\u5740\u3002\u7f16\u8bd1\u5668\u4e3a\u6211\u4eec\u4ee3\u52b3\u4e86\u5168\u90e8\u7684\u5bfb\u5740\u5de5\u4f5c\u3002 \u5bf9\u4e8e\u5f15\u7528\u6765\u8bf4\u4e5f\u662f\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 int main () { char x {}; // assume this is assigned memory address 140 char & ref { x }; // ref is an lvalue reference to x (when used with a type, & means lvalue reference) return 0 ; } \u56e0\u4e3a ref \u662f x \u7684\u522b\u540d\uff0c\u6240\u4ee5\u4efb\u4f55\u4f7f\u7528 ref \u7684\u5730\u65b9\uff0c\u7a0b\u5e8f\u4ecd\u7136\u4f1a\u5230\u5185\u5b58\u5730\u5740140\u7684\u5730\u65b9\u53bb\u53d6\u503c\u3002\u7f16\u8bd1\u5668\u540c\u6837\u4e3a\u6211\u4eec\u4ee3\u52b3\u4e86\u5bfb\u5740\u7684\u8fc7\u7a0b\uff0c\u6211\u4eec\u65e0\u9700\u64cd\u5fc3\u3002 \u53d6\u5730\u5740\u8fd0\u7b97\u7b26 (&) \u5c3d\u7ba1\u5728\u9ed8\u8ba4\u7684\u60c5\u51b5\u4e0b\uff0c\u53d8\u91cf\u7684\u5730\u5740\u5e76\u4e0d\u4f1a\u66b4\u9732\u7ed9\u7528\u6237\uff0c\u4f46\u8be5\u5730\u5740\u5b9e\u9645\u4e0a\u662f\u53ef\u4ee5\u83b7\u53d6\u7684\u3002 \u53d6\u5730\u5740\u64cd\u4f5c\u7b26 & \u5c31\u53ef\u4ee5\u8fd4\u56de\u5176\u64cd\u4f5c\u6570\u7684\u5730\u5740\uff0c\u975e\u5e38\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { int x { 5 }; std :: cout << x << '\\n' ; // print the value of variable x std :: cout << & x << '\\n' ; // print the memory address of variable x return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u8fd4\u56de\u5982\u4e0b\u7ed3\u679c\uff1a 1 2 5 0027FEA0 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u53d6\u5730\u5740\u8fd0\u7b97\u7b26\u6765\u83b7\u53d6\u53d8\u91cfx\u7684\u5730\u5740\u5e76\u5c06\u5176\u6253\u5370\u5904\u7406\u3002\u5185\u5b58\u5730\u5740\u901a\u5e38\u662f\u4ee5\u5341\u516d\u8fdb\u5236\u503c\u7684\u5f62\u5f0f\u6253\u5370\u7684\uff08\u53c2\u8003 4.16 - \u6570\u503c\u7cfb\u7edf\uff08\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236\uff09 \u4e86\u89e3\u5341\u516d\u8fdb\u5236\u7684\u5185\u5bb9\uff09\uff0c\u4f46\u901a\u5e38\u4e0d\u5305\u62ec\u524d\u7f00 0x \u3002 \u5bf9\u4e8e\u4f7f\u7528\u5185\u5b58\u6570\u8d85\u8fc7\u4e00\u4e2a\u5b57\u8282\u7684\u5bf9\u8c61\uff0c\u53d6\u5730\u5740\u4f1a\u8fd4\u56de\u8be5\u6bb5\u5185\u5b58\u7684\u9996\u5730\u5740\uff08\u7b2c\u4e00\u4e2a\u5b57\u8282\uff09\u3002 \u5c0f\u8d34\u58eb & \u7b26\u53f7\u5728\u4e0d\u540c\u7684\u8bed\u5883\u4e0b\u6709\u4e0d\u540c\u7684\u542b\u4e49\uff0c\u6bd4\u8f83\u5bb9\u6613\u6df7\u6dc6\uff1a \u5f53\u540e\u9762\u63a5\u7740\u7c7b\u578b\u540d\u65f6\uff0c&\u8868\u793a\u4e00\u4e2a\u5de6\u503c\u5f15\u7528: int& ref \uff1b \u5f53\u7528\u4e8e\u4e00\u5143\u8868\u8fbe\u5f0f\u65f6\uff0c&\u8868\u793a\u53d6\u5730\u5740\u8fd0\u7b97\u7b26: std::cout << &x . \u5f53\u7528\u4e8e\u4e8c\u5143\u8868\u8fbe\u5f0f\u65f6\uff0c&\u662f \u6309\u4f4d\u4e0e\u64cd\u4f5c : std::cout << x & y . \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 * \u4ec5\u4ec5\u83b7\u5f97\u53d8\u91cf\u7684\u5730\u5740\uff0c\u5f80\u5f80\u4e0d\u662f\u5f88\u6709\u7528\u3002 \u5bf9\u4e8e\u4e00\u4e2a\u5730\u5740\u6765\u8bf4\uff0c\u6700\u6709\u7528\u7684\u64cd\u4f5c\u662f\u83b7\u53d6\u5b83\u5b58\u653e\u7684\u503c\u3002 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \uff08\u4e5f\u53eb\u95f4\u63a5\u8bbf\u95ee\u8fd0\u7b97\u7b26\uff09\uff0c\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u5730\u5740\u5b58\u653e\u7684\u503c\uff08\u4f5c\u4e3a\u5de6\u503c\u8fd4\u56de\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int x { 5 }; std :: cout << x << '\\n' ; // print the value of variable x std :: cout << & x << '\\n' ; // print the memory address of variable x std :: cout << * ( & x ) << '\\n' ; // print the value at the memory address of variable x (parentheses not required, but make it easier to read) return 0 ; } \u5728\u7b14\u8005\u673a\u5668\u4e0a\u8fd4\u56de\u4e0b\u9762\u5185\u5bb9\uff1a 1 2 3 5 0027FEA0 5 \u8fd9\u4e2a\u7a0b\u5e8f\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a\u53d8\u91cfx\u5e76\u4e14\u6253\u5370\u51fa\u5b83\u7684\u503c\u3002\u7136\u540e\u6211\u4eec\u6253\u5370\u51fa\u4e86\u53d8\u91cfx\u7684\u5730\u5740\u3002\u6700\u540e\uff0c\u4f7f\u7528\u89e3\u5f15\u7528\u8fd0\u7b97\u7b26\u4ece\u53d8\u91cfx\u7684\u5730\u5740\u83b7\u53d6\u503c\uff08\u5c31\u662fx\u7684\u503c\uff09\uff0c\u7136\u540e\u6253\u5370\u51fa\u6765\u3002 \u5173\u952e\u4fe1\u606f \u5bf9\u4e8e\u4e00\u4e2a\u7ed9\u5b9a\u5730\u5740\uff0c\u53ef\u4ee5\u4f7f\u7528 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u83b7\u53d6\u8be5\u5730\u5740\u4fdd\u5b58\u7684\u503c\uff08\u5de6\u503c\uff09 \u89e3\u5f15\u7528\u548c\u53d6\u5730\u5740\u662f\u4e00\u5bf9\u76f8\u53cd\u7684\u64cd\u4f5c\uff0c\u53d6\u5730\u5740\u83b7\u53d6\u5bf9\u8c61\u7684\u5730\u5740\uff0c\u89e3\u5f15\u7528\u4ece\u5730\u5740\u83b7\u53d6\u5bf9\u8c61\u3002 \u5c0f\u8d34\u58eb \u5c3d\u7ba1\u89e3\u5f15\u7528\u770b\u8d77\u6765\u5c31\u50cf\u4e58\u6cd5\uff0c\u4f46\u662f\u4f60\u53ef\u4ee5\u533a\u5206\u5b83\u4eec\uff0c\u56e0\u4e3a\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26\u662f\u4e00\u5143\u7684\uff0c\u800c\u4e58\u6cd5\u64cd\u4f5c\u7b26\u662f\u4e8c\u5143\u7684\u3002 \u83b7\u53d6\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\uff0c\u7136\u540e\u7acb\u5373\u89e3\u5f15\u7528\u8be5\u5730\u5740\u4ee5\u83b7\u5f97\u503c\uff0c\u8fd9\u5176\u5b9e\u4e5f\u4e0d\u7b97\u662f\u4ec0\u4e48\u6709\u7528\u7684\u64cd\u4f5c(\u6bd5\u7adf\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u53d8\u91cf\u7684\u503c)\u3002 \u4e0d\u8fc7\uff0c\u5728\u4e86\u89e3\u53d6\u5730\u5740\u548c\u89e3\u5f15\u7528\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb\u4ecb\u7ecd\u6307\u9488\u4e86\u3002 \u6307\u9488 \u6307\u9488\u53ef\u4ee5\u770b\u505a\u662f\u4e00\u4e2a\u6301\u6709\u5185\u5b58\u5730\u5740\u7684\u5bf9\u8c61\uff08\u901a\u5e38\u662f\u5176\u4ed6\u5bf9\u8c61\u7684\u5730\u5740\uff09\u3002\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u5728\u540e\u9762\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528\u8be5\u5730\u5740\u3002 \u9898\u5916\u8bdd \u5728\u73b0\u4ee3 C++ \u4e2d\uff0c\u6211\u4eec\u6b64\u5904\u8c08\u8bba\u7684\u6307\u9488\u88ab\u79f0\u4e3a \u539f\u59cb\u6307\u9488 \u6216 \u7b28\u6307\u9488 \uff0c\u4ee5\u4fbf\u5c06\u5176\u4e0e \u667a\u80fd\u6307\u9488 \u533a\u522b\u5f00\u6765\u3002\u667a\u80fd\u6307\u9488\u4f1a\u5728 chapter M \u4ecb\u7ecd\u3002 \u548c\u4f7f\u7528&\u5b9a\u4e49\u5f15\u7528\u7684\u505a\u6cd5\u7c7b\u4f3c\uff0c\u6307\u9488\u7c7b\u578b\u4f7f\u7528 * \u6765\u5b9a\u4e49\uff1a 1 2 3 4 int ; // a normal int int & ; // an lvalue reference to an int value int * ; // a pointer to an int value (holds the address of an integer value) \u8981\u521b\u5efa\u6307\u9488\u53d8\u91cf\uff0c\u53ea\u9700\u5b9a\u4e49\u4e00\u4e2a\u6307\u9488\u7c7b\u578b\u7684\u53d8\u91cf: 1 2 3 4 5 6 7 8 9 int main () { int x { 5 }; // normal variable int & ref { x }; // a reference to an integer (bound to x) int * ptr ; // a pointer to an integer return 0 ; } \u6ce8\u610f\uff0c\u8fd9\u4e2a\u661f\u53f7\u662f\u6307\u9488\u58f0\u660e\u8bed\u6cd5\u7684\u4e00\u90e8\u5206\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26\u3002 \u6700\u4f73\u5b9e\u8df5 \u58f0\u660e\u6307\u9488\u7c7b\u578b\u65f6\uff0c\u5728\u7c7b\u578b\u540d\u79f0\u65c1\u8fb9\u52a0\u4e0a\u661f\u53f7\u3002 \u6ce8\u610f \u5c3d\u7ba1\u901a\u5e38\u60c5\u51b5\u4e0b\u6211\u4eec\u4e0d\u5e94\u8be5\u5728\u4e00\u884c\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\uff0c\u4f46\u662f\u5982\u679c\u9700\u8981\u8fd9\u4e48\u505a\uff0c\u5219\u6bcf\u4e2a\u6307\u9488\u53d8\u91cf\u524d\u9762\u90fd\u9700\u8981\u661f\u53f7 1 2 int * ptr1 , ptr2 ; // incorrect: ptr1 is a pointer to an int, but ptr2 is just a plain int! int * ptr3 , * ptr4 ; // correct: ptr3 and p4 are both pointers to an int \u867d\u7136\u8fd9\u4e2a\u4f8b\u5b50\u65f6\u5e38\u88ab\u4f5c\u4e3a\u201d\u661f\u53f7\u4e0d\u5e94\u8be5\u4e0e\u7c7b\u578b\u540d\u653e\u5728\u4e00\u8d77\u201c\u7684\u8bba\u636e\uff0c\u4f46\u6211\u4eec\u5012\u4e0d\u5982\u8bf4\u8fd9\u662f\u4e00\u4e2a\u201d\u4e0d\u5e94\u5728\u4e00\u884c\u4e2d\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\u7684\u201c\u4f8b\u5b50\u3002 \u6307\u9488\u7684\u521d\u59cb\u5316 \u548c\u666e\u901a\u7684\u53d8\u91cf\u4e0d\u540c\uff0c \u6307\u9488\u9ed8\u8ba4\u662f\u4e0d\u521d\u59cb\u5316\u7684 \u3002\u4e00\u4e2a\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u7684\u6307\u9488\uff0c\u6709\u65f6\u79f0\u4e3a \u91ce\u6307\u9488 \u3002\u91ce\u6307\u9488\u4e2d\u5b58\u653e\u7684\u662f\u4e00\u4e2a\u65e0\u7528\u7684\u5730\u5740\uff0c\u5bf9\u91ce\u6307\u9488\u89e3\u5f15\u7528\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4e00\u5b9a\u8981\u5c06\u6307\u9488\u521d\u59cb\u5316\u4e3a\u4e00\u4e2a\u5df2\u77e5\u7684\u503c\u3002 \u6700\u4f73\u5b9e\u8df5 \u6307\u9488\u5fc5\u987b\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 10 int main () { int x { 5 }; int * ptr ; // \u4e00\u4e2a\u672a\u521d\u59cb\u5316\u7684\u6307\u9488 (\u5b58\u653e\u8fd9\u65e0\u6548\u5730\u5740) int * ptr2 {}; // \u4e00\u4e2a\u7a7a\u6307\u9488 (\u7a0d\u540e\u8ba8\u8bba\u8fd9\u79cd\u60c5\u51b5) int * ptr3 { & x }; // \u6307\u9488\uff0c\u521d\u59cb\u5316\u4e3a\u53d8\u91cfx\u7684\u5730\u5740 return 0 ; } \u7531\u4e8e\u6307\u9488\u4fdd\u5b58\u7684\u662f\u5730\u5740\uff0c\u6240\u4ee5\u5176\u521d\u59cb\u5316\u503c\u5fc5\u987b\u662f\u4e00\u4e2a\u5730\u5740\u3002\u901a\u5e38\uff0c\u6307\u9488\u7528\u4e8e\u4fdd\u5b58\u53e6\u4e00\u4e2a\u53d8\u91cf\u7684\u5730\u5740(\u53ef\u4ee5\u4f7f\u7528\u53d6\u5730\u5740\u64cd\u4f5c\u7b26(&)\u83b7\u53d6)\u3002 \u4e00\u65e6\u6211\u4eec\u6709\u4e86\u4e00\u4e2a\u6301\u6709\u53e6\u4e00\u4e2a\u5bf9\u8c61\u5730\u5740\u7684\u6307\u9488\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26 * \u6765\u8bbf\u95ee\u8be5\u5730\u5740\u7684\u503c\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int x { 5 }; std :: cout << x << '\\n' ; // \u6253\u5370\u53d8\u91cf x \u7684\u503c int * ptr { & x }; // ptr \u4fdd\u5b58\u7740 x \u7684\u5730\u5740 std :: cout << * ptr << '\\n' ; // \u4f7f\u7528\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26\u6253\u5370\u8be5\u5730\u5740\u7684\u5185\u5bb9 return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 5 5 \u5f62\u8c61\u4e00\u70b9\u6765\u770b\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u662f\u8fd9\u6837\u7684\uff1a \u8fd9\u5c31\u662f\u6307\u9488\u540d\u79f0\u7684\u7531\u6765\u2014\u2014 ptr \u4fdd\u5b58\u7740 x \u7684\u5730\u5740\uff0c\u6240\u4ee5\u6211\u4eec\u8bf4 ptr \u662f\u201c\u6307\u5411 x \u3002 \u4f5c\u8005\u6ce8 \u5173\u4e8e\u6307\u9488\u547d\u540d\u7684\u6ce8\u610f\u4e8b\u9879:\u201cX\u6307\u9488\u201d(\u5176\u4e2dX\u662f\u67d0\u79cd\u7c7b\u578b)\u662f\u201c\u6307\u5411X\u7684\u6307\u9488\u201d\u7684\u5e38\u7528\u7f29\u5199\u3002\u6240\u4ee5\u5f53\u6211\u4eec\u8bf4\u201c\u4e00\u4e2a\u6574\u6570\u6307\u9488\u201d\u65f6\uff0c\u6211\u4eec\u5b9e\u9645\u4e0a\u662f\u6307\u201c\u4e00\u4e2a\u6307\u5411\u6574\u578b\u53d8\u91cf\u7684\u6307\u9488\u201d\u3002\u5f53\u6211\u4eec\u8ba8\u8bbaconst\u6307\u9488\u65f6\uff0c\u8fd9\u79cd\u533a\u522b\u5c31\u5f88\u91cd\u8981\u4e86\u3002 \u5c31\u50cf\u5f15\u7528\u7684\u7c7b\u578b\u5fc5\u987b\u5339\u914d\u88ab\u5f15\u7528\u5bf9\u8c61\u7684\u7c7b\u578b\u4e00\u6837\uff0c\u6307\u9488\u7684\u7c7b\u578b\u5fc5\u987b\u5339\u914d\u88ab\u6307\u5411\u5bf9\u8c61\u7684\u7c7b\u578b: 1 2 3 4 5 6 7 8 9 10 int main () { int i { 5 }; double d { 7.0 }; int * iPtr { & i }; // ok: a pointer to an int can point to an int object int * iPtr2 { & d }; // not okay: a pointer to an int can't point to a double double * dPtr { & d }; // ok: a pointer to a double can point to a double object double * dPtr2 { & i }; // not okay: a pointer to a double can't point to an int } \u9664\u4e86\u4e00\u4e2a\u4f8b\u5916\uff0c\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u8ba8\u8bba\uff0c\u7528\u5b57\u9762\u91cf\u521d\u59cb\u5316\u6307\u9488\u662f\u4e0d\u5141\u8bb8\u7684\uff1a 1 2 int * ptr { 5 }; // \u4e0d\u53ef\u4ee5 int * ptr { 0x0012FF7C }; // \u4e0d\u53ef\u4ee5, 0x0012FF7C \u88ab\u770b\u505a\u6574\u578b\u5b57\u9762\u91cf \u6307\u9488\u548c\u8d4b\u503c \u5728\u8c08\u8bba\u6307\u9488\u7684\u8d4b\u503c\u65f6\uff0c\u53ef\u80fd\u6709\u4e24\u79cd\u542b\u4e49\uff1a \u6539\u53d8\u6307\u9488\u7684\u503c\u4f7f\u5176\u6307\u5411\u5176\u4ed6\u5730\u5740\uff08\u7ed9\u6307\u9488\u8d4b\u503c\u4e00\u4e2a\u65b0\u5730\u5740\uff09\uff1b \u6539\u53d8\u6307\u9488\u6240\u6307\u5185\u5bb9\u7684\u503c\uff08\u7ed9\u6307\u9488\u89e3\u5f15\u7528\u7684\u7ed3\u679c\u8d4b\u65b0\u503c\uff09\u3002 \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u6307\u9488\u88ab\u66f4\u6539\u4e3a\u6307\u5411\u53e6\u4e00\u4e2a\u5bf9\u8c61\u7684\u60c5\u51b5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { int x { 5 }; int * ptr { & x }; // ptr \u521d\u59cb\u5316\u4e3a\u6307\u5411 x std :: cout << * ptr << '\\n' ; // \u6253\u5370\u5730\u5740\u503c(x\u7684\u5730\u5740) int y { 6 }; ptr = & y ; // // ptr \u66f4\u6539\u4e3a\u6307\u5411 y std :: cout << * ptr << '\\n' ; // \u6253\u5370\u5730\u5740\u503c (y\u7684\u5730\u5740) return 0 ; } \u6253\u5370\uff1a 1 2 5 6 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u6307\u9488 ptr \u5e76\u7528x\u7684\u5730\u5740\u521d\u59cb\u5316\u5b83\u3002\u5bf9\u6307\u9488\u89e3\u5f15\u7528\u4ee5\u6253\u5370\u88ab\u6307\u5411\u7684\u503c( 5 )\u3002\u7136\u540e\u4f7f\u7528\u8d4b\u503c\u64cd\u4f5c\u7b26\u5c06 ptr \u4fdd\u5b58\u7684\u5730\u5740\u66f4\u6539\u4e3ay\u7684\u5730\u5740\u3002\u7136\u540e\u518d\u6b21\u89e3\u5f15\u7528\u8be5\u6307\u9488\u4ee5\u6253\u5370\u88ab\u6307\u5411\u7684\u503c(\u73b0\u5728\u662f 6 )\u3002 \u73b0\u5728\u8ba9\u6211\u4eec\u770b\u770b\u5982\u4f55\u4f7f\u7528\u6307\u9488\u6765\u6539\u53d8\u88ab\u6307\u5411\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { int x { 5 }; int * ptr { & x }; // initialize ptr with address of variable x std :: cout << x << '\\n' ; // print x's value std :: cout << * ptr << '\\n' ; // print the value at the address that ptr is holding (x's address) * ptr = 6 ; // The object at the address held by ptr (x) assigned value 6 (note that ptr is dereferenced here) std :: cout << x << '\\n' ; std :: cout << * ptr << '\\n' ; // print the value at the address that ptr is holding (x's address) return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 5 5 6 6 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u6307\u9488 ptr \uff0c\u5b83\u7684\u521d\u59cb\u5316\u4e3a\u53d8\u91cf x \u7684\u5730\u5740\uff0c\u7136\u540e\u6253\u5370\u4e86 x \u548c *ptr \u7684\u503c( 5 )\u3002\u56e0\u4e3a *ptr \u8fd4\u56de\u7684\u662f\u5de6\u503c\uff0c\u6240\u6211\u4eec\u53ef\u4ee5\u628a\u5b83\u7528\u5728\u8d4b\u503c\u53f7\u7684\u5de6\u4fa7\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06 ptr \u6240\u6307\u7684\u53d8\u91cf\u7684\u503c\u4fee\u6539\u4e3a 6 \u3002\u7136\u540e\uff0c\u518d\u6b21\u6253\u5370 x \u548c *ptr \u4ee5\u4fbf\u786e\u5b9a\u6211\u4eec\u7684\u4fee\u6539\u662f\u5426\u751f\u6548\u3002 \u5173\u952e\u4fe1\u606f \u5728\u4f7f\u7528\u6307\u9488\u65f6\uff0c\u5982\u679c\u4e0d\u540c\u65f6\u4f7f\u7528 \u89e3\u5f15\u7528 \u7684\u65f6\u5019\uff0c\u6211\u4eec\u4f7f\u7528\u7684\u662f\u6307\u9488\u5b58\u653e\u7684\u5730\u5740\u3002\u4fee\u6539\u8be5\u5730\u5740\u5c31\u53ef\u4ee5\u6539\u53d8\u6307\u9488\u7684\u6307\u5411 ( ptr = &y )\u3002 \u5f53\u5bf9\u6307\u9488\u5e94\u7528\u89e3\u5f15\u7528\u65f6( *ptr )\uff0c\u6211\u4eec\u8bbf\u95ee\u7684\u5219\u662f\u6307\u9488\u6240\u6307\u7684\u5bf9\u8c61\u3002\u4fee\u6539( *ptr = 6; ) \u5b83\u53ef\u4ee5\u6539\u53d8\u8be5\u53d8\u91cf\u7684\u503c\u3002 \u6307\u9488\u548c\u5de6\u503c\u5f15\u7528\u5f88\u50cf \u6307\u9488\u548c\u5de6\u503c\u5f15\u7528\u7684\u884c\u4e3a\u7c7b\u4f3c\u3002\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include int main () { int x { 5 }; int & ref { x }; // get a reference to x int * ptr { & x }; // get a pointer to x std :: cout << x ; std :: cout << ref ; // use the reference to print x's value (5) std :: cout << * ptr << '\\n' ; // use the pointer to print x's value (5) ref = 6 ; // use the reference to change the value of x std :: cout << x ; std :: cout << ref ; // use the reference to print x's value (6) std :: cout << * ptr << '\\n' ; // use the pointer to print x's value (6) * ptr = 7 ; // use the pointer to change the value of x std :: cout << x ; std :: cout << ref ; // use the reference to print x's value (7) std :: cout << * ptr << '\\n' ; // use the pointer to print x's value (7) return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 2 3 555 666 777 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u503c\u4e3a5\u7684\u666e\u901a\u53d8\u91cf x \uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u5de6\u503c\u5f15\u7528\u548c\u4e00\u4e2a\u6307\u5411 x \u7684\u6307\u9488\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528\u5de6\u503c\u5f15\u7528\u5c06\u503c\u4ece5\u66f4\u6539\u4e3a6\uff0c\u5e76\u8bf4\u660e\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6240\u6709\u4e09\u4e2a\u65b9\u6cd5\u8bbf\u95ee\u66f4\u65b0\u540e\u7684\u503c\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u89e3\u5f15\u7528\u7684\u6307\u9488\u5c06\u503c\u4ece6\u6539\u4e3a7\uff0c\u5e76\u518d\u6b21\u8868\u660e\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6240\u6709\u4e09\u79cd\u65b9\u6cd5\u8bbf\u95ee\u66f4\u65b0\u540e\u7684\u503c\u3002 \u56e0\u6b64\uff0c\u6307\u9488\u548c\u5f15\u7528\u90fd\u63d0\u4f9b\u4e86\u95f4\u63a5\u8bbf\u95ee\u53e6\u4e00\u4e2a\u5bf9\u8c61\u7684\u65b9\u6cd5\u3002\u4e3b\u8981\u7684\u533a\u522b\u5728\u4e8e\uff0c\u5bf9\u4e8e\u6307\u9488\uff0c\u6211\u4eec\u9700\u8981\u663e\u5f0f\u5730\u83b7\u53d6\u8981\u6307\u5411\u7684\u5730\u5740\uff0c\u5e76\u4e14\u663e\u5f0f\u5730\u89e3\u5f15\u7528\u6307\u9488\u6765\u83b7\u53d6\u503c\u3002\u5bf9\u4e8e\u5f15\u7528\uff0c\u5730\u5740\u548c\u89e3\u5f15\u7528\u90fd\u662f\u9690\u5f0f\u8fdb\u884c\u7684\u3002 \u6307\u9488\u548c\u5f15\u7528\u4e4b\u95f4\u8fd8\u6709\u4e00\u4e9b\u503c\u5f97\u4e00\u63d0\u7684\u533a\u522b\uff1a \u5f15\u7528\u5fc5\u987b\u88ab\u521d\u59cb\u5316\uff0c\u6307\u9488\u5e76\u4e0d\u662f\u5fc5\u987b\u521d\u59cb\u5316\uff08\u4f46\u662f\u4f60\u5e94\u8be5\u521d\u59cb\u5316\uff09\uff1b \u5f15\u7528\u4e0d\u662f\u5bf9\u8c61\uff0c\u6307\u9488\u662f\u5bf9\u8c61\uff1b \u5f15\u7528\u4e0d\u80fd\u88ab\u91cd\u65b0\u8bbe\u7f6e\uff08\u4fee\u6539\u5f15\u7528\u4f7f\u5176\u7ed1\u5b9a\u5230\u5176\u4ed6\u5bf9\u8c61\uff09\uff0c\u6307\u9488\u5219\u53ef\u4ee5\u88ab\u4fee\u6539\u6307\u5411\u5176\u4ed6\u5bf9\u8c61\uff1b \u5f15\u7528\u603b\u662f\u7ed1\u5b9a\u5230\u67d0\u4e2a\u5bf9\u8c61\uff0c\u6307\u9488\u53ef\u4ee5\u4e0d\u6267\u884c\u4efb\u4f55\u5bf9\u8c61\uff1b \u5f15\u7528\u662f\u201d\u5b89\u5168\u201c\u7684\uff08\u9664\u4e86 \u60ac\u5782 \u5f15\u7528\uff09\uff0c\u6307\u9488\u5219\u662f\u5371\u9669\u7684\u3002 \u53d6\u5730\u5740\u8fd0\u7b97\u7b26\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u6307\u9488 \u9700\u8981\u6ce8\u610f\uff0c \u53d6\u5730\u5740\u64cd\u4f5c\u7b26 & \u8fd4\u56de\u7684\u5e76\u4e0d\u662f\u5730\u5740\u7684\u5b57\u9762\u91cf\uff0c\u5b83\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u6307\u5411\u64cd\u4f5c\u6570\u5730\u5740\u7684\u6307\u9488\uff0c\u8be5\u6307\u9488\u7684\u7c7b\u578b\u53d6\u51b3\u4e8e\u53c2\u6570\u7684\u7c7b\u578b\uff08\u4f8b\u5982\uff0c\u83b7\u53d6\u6574\u578b\u53d8\u91cf\u7684\u5730\u5740\u8fd4\u56de\u7684\u662f\u6574\u578b\u6307\u9488\uff09\u3002 \u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { int x { 4 }; std :: cout << typeid ( & x ). name () << '\\n' ; // print the type of &x return 0 ; } \u5728 Visual Studio \u4e2d\u4f1a\u6253\u5370\u5982\u4e0b\u5185\u5bb9\uff1a 1 int * \u5982\u679c\u662fgcc\uff0c\u5219\u4f1a\u6253\u5370\u201cpi\u201d (\u6307\u5411int\u7684\u6307\u9488\u2014\u2014pointer to int)\u3002\u56e0\u4e3a typeid().name() \u7684\u7ed3\u679c\u53d6\u51b3\u4e8e\u7f16\u8bd1\u5668\uff08 typeid \u8fd0\u7b97\u7b26 \uff09\uff0c\u6240\u4ee5\u5b83\u7684\u6253\u5370\u7ed3\u679c\u53ef\u80fd\u662f\u4e0d\u540c\u7684\uff0c\u4e0d\u8fc7\u610f\u601d\u662f\u4e00\u6837\u7684\u3002 \u6307\u9488\u7684\u5927\u5c0f \u6307\u9488\u7684\u5927\u5c0f\u53d6\u51b3\u4e8e\u8be5\u53ef\u6267\u884c\u7a0b\u5e8f\u7f16\u8bd1\u7684\u4f53\u7cfb\u7ed3\u6784\u2014\u201432\u4f4d\u53ef\u6267\u884c\u7a0b\u5e8f\u4f7f\u752832\u4f4d\u5730\u5740\u2014\u2014\u5219\u4e00\u4e2a\u6307\u9488\u572832\u4f4d\u673a\u5668\u4e0a\u7684\u5927\u5c0f\u662f4\u4e2a\u5b57\u8282\u3002\u5bf9\u4e8e64\u4f4d\u53ef\u6267\u884c\u7a0b\u5e8f\uff0c\u6307\u9488\u5927\u5c0f\u4e3a64\u4f4d\uff088\u5b57\u8282\uff09\u3002\u6ce8\u610f\uff0c\u4e0d\u8bba\u6307\u9488\u6240\u6307\u5bf9\u8c61\u7684\u5927\u5c0f\u662f\u591a\u5c11\uff0c\u6307\u9488\u672c\u8eab\u7684\u5927\u5c0f\u603b\u662f\u7b26\u5408\u4e0a\u9762\u7684\u89c4\u5219\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () // \u5047\u8bbe\u4e3a32\u4f4d\u7a0b\u5e8f { char * chPtr {}; // char \u4e3a1\u4e2a\u5b57\u8282 int * iPtr {}; // int\u901a\u5e38\u662f4\u5b57\u8282 long double * ldPtr {}; // long doubles \u901a\u5e38\u662f8\u5b57\u8282\u621612\u5b57\u8282 std :: cout << sizeof ( chPtr ) << '\\n' ; // \u6253\u5370 4 std :: cout << sizeof ( iPtr ) << '\\n' ; // \u6253\u5370 4 std :: cout << sizeof ( ldPtr ) << '\\n' ; // \u6253\u5370 4 return 0 ; } \u6307\u9488\u7684\u5927\u5c0f\u603b\u662f\u76f8\u540c\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u6307\u9488\u53ea\u662f\u4e00\u4e2a\u5185\u5b58\u5730\u5740\uff0c\u800c\u8bbf\u95ee\u5185\u5b58\u5730\u5740\u6240\u9700\u7684\u6bd4\u7279\u6570\u662f\u6052\u5b9a\u7684\u3002 \u60ac\u5782\u6307\u9488 \u5c31\u50cf\u60ac\u5782\u5f15\u7528\u4e00\u6837\uff0c\u60ac\u5782\u6307\u9488\u662f\u4fdd\u5b58\u4e0d\u518d\u6709\u6548\u7684\u5bf9\u8c61\u5730\u5740\u7684\u6307\u9488(\u4f8b\u5982\uff0c\u5bf9\u8c61\u5df2\u88ab\u9500\u6bc1)\u3002\u5bf9\u60ac\u6d6e\u6307\u9488\u7684\u89e3\u5f15\u7528\u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u521b\u5efa\u60ac\u6d6e\u6307\u9488\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int main () { int x { 5 }; int * ptr { & x }; std :: cout << * ptr << '\\n' ; // valid { int y { 6 }; ptr = & y ; std :: cout << * ptr << '\\n' ; // valid } // y goes out of scope, and ptr is now dangling std :: cout << * ptr << '\\n' ; // undefined behavior from dereferencing a dangling pointer return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 2 3 5 6 6 \u4f46\u662f\uff0c\u7ed3\u679c\u4e5f\u53ef\u80fd\u4e0d\u662f\u4e0a\u9762\u8fd9\u6837\uff0c\u56e0\u4e3a ptr \u6240\u6307\u7684\u5bf9\u8c61\u5728 \u79bb\u5f00\u4f5c\u7528\u57df \u65f6(\u5185\u5c42\u8bed\u53e5\u5757)\u4ee5\u53ca\u88ab\u9500\u6bc1\u4e86\uff0c\u6b64\u65f6\u7684 ptr \u662f\u4e00\u4e2a\u60ac\u5782\u6307\u9488\u3002 \u5c0f\u7ed3 \u6307\u9488\u662f\u4fdd\u5b58\u5185\u5b58\u5730\u5740\u7684\u53d8\u91cf\u3002\u53ef\u4ee5\u4f7f\u7528 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u5bf9\u5b83\u4eec\u8fdb\u884c\u89e3\u5f15\u7528\uff0c\u4ee5\u83b7\u53d6\u6307\u9488\u6240\u6301\u6709\u7684\u5730\u5740\u7684\u503c\u3002\u5bf9 \u91ce\u6307\u9488 \u6216 \u60ac\u5782 \u6307\u9488(\u6216\u7a7a\u6307\u9488)\u7684\u89e3\u5f15\u7528\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u5e76\u53ef\u80fd\u5bfc\u81f4\u5e94\u7528\u7a0b\u5e8f\u5d29\u6e83\u3002 \u6307\u9488\u6bd4\u5f15\u7528\u66f4\u7075\u6d3b\uff0c\u4e5f\u66f4\u5371\u9669\u3002\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\u7ee7\u7eed\u63a2\u8ba8\u8fd9\u4e2a\u95ee\u9898\u3002","title":"9.6 - \u6307\u9488\u7b80\u4ecb"},{"location":"9-6-Introduction-to-pointers/#_1","text":"\u5c3d\u7ba1\u5728\u9ed8\u8ba4\u7684\u60c5\u51b5\u4e0b\uff0c\u53d8\u91cf\u7684\u5730\u5740\u5e76\u4e0d\u4f1a\u66b4\u9732\u7ed9\u7528\u6237\uff0c\u4f46\u8be5\u5730\u5740\u5b9e\u9645\u4e0a\u662f\u53ef\u4ee5\u83b7\u53d6\u7684\u3002 \u53d6\u5730\u5740\u64cd\u4f5c\u7b26 & \u5c31\u53ef\u4ee5\u8fd4\u56de\u5176\u64cd\u4f5c\u6570\u7684\u5730\u5740\uff0c\u975e\u5e38\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { int x { 5 }; std :: cout << x << '\\n' ; // print the value of variable x std :: cout << & x << '\\n' ; // print the memory address of variable x return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u8fd4\u56de\u5982\u4e0b\u7ed3\u679c\uff1a 1 2 5 0027FEA0 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u53d6\u5730\u5740\u8fd0\u7b97\u7b26\u6765\u83b7\u53d6\u53d8\u91cfx\u7684\u5730\u5740\u5e76\u5c06\u5176\u6253\u5370\u5904\u7406\u3002\u5185\u5b58\u5730\u5740\u901a\u5e38\u662f\u4ee5\u5341\u516d\u8fdb\u5236\u503c\u7684\u5f62\u5f0f\u6253\u5370\u7684\uff08\u53c2\u8003 4.16 - \u6570\u503c\u7cfb\u7edf\uff08\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236\uff09 \u4e86\u89e3\u5341\u516d\u8fdb\u5236\u7684\u5185\u5bb9\uff09\uff0c\u4f46\u901a\u5e38\u4e0d\u5305\u62ec\u524d\u7f00 0x \u3002 \u5bf9\u4e8e\u4f7f\u7528\u5185\u5b58\u6570\u8d85\u8fc7\u4e00\u4e2a\u5b57\u8282\u7684\u5bf9\u8c61\uff0c\u53d6\u5730\u5740\u4f1a\u8fd4\u56de\u8be5\u6bb5\u5185\u5b58\u7684\u9996\u5730\u5740\uff08\u7b2c\u4e00\u4e2a\u5b57\u8282\uff09\u3002 \u5c0f\u8d34\u58eb & \u7b26\u53f7\u5728\u4e0d\u540c\u7684\u8bed\u5883\u4e0b\u6709\u4e0d\u540c\u7684\u542b\u4e49\uff0c\u6bd4\u8f83\u5bb9\u6613\u6df7\u6dc6\uff1a \u5f53\u540e\u9762\u63a5\u7740\u7c7b\u578b\u540d\u65f6\uff0c&\u8868\u793a\u4e00\u4e2a\u5de6\u503c\u5f15\u7528: int& ref \uff1b \u5f53\u7528\u4e8e\u4e00\u5143\u8868\u8fbe\u5f0f\u65f6\uff0c&\u8868\u793a\u53d6\u5730\u5740\u8fd0\u7b97\u7b26: std::cout << &x . \u5f53\u7528\u4e8e\u4e8c\u5143\u8868\u8fbe\u5f0f\u65f6\uff0c&\u662f \u6309\u4f4d\u4e0e\u64cd\u4f5c : std::cout << x & y .","title":"\u53d6\u5730\u5740\u8fd0\u7b97\u7b26 (&)"},{"location":"9-6-Introduction-to-pointers/#_2","text":"\u4ec5\u4ec5\u83b7\u5f97\u53d8\u91cf\u7684\u5730\u5740\uff0c\u5f80\u5f80\u4e0d\u662f\u5f88\u6709\u7528\u3002 \u5bf9\u4e8e\u4e00\u4e2a\u5730\u5740\u6765\u8bf4\uff0c\u6700\u6709\u7528\u7684\u64cd\u4f5c\u662f\u83b7\u53d6\u5b83\u5b58\u653e\u7684\u503c\u3002 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \uff08\u4e5f\u53eb\u95f4\u63a5\u8bbf\u95ee\u8fd0\u7b97\u7b26\uff09\uff0c\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u5730\u5740\u5b58\u653e\u7684\u503c\uff08\u4f5c\u4e3a\u5de6\u503c\u8fd4\u56de\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int x { 5 }; std :: cout << x << '\\n' ; // print the value of variable x std :: cout << & x << '\\n' ; // print the memory address of variable x std :: cout << * ( & x ) << '\\n' ; // print the value at the memory address of variable x (parentheses not required, but make it easier to read) return 0 ; } \u5728\u7b14\u8005\u673a\u5668\u4e0a\u8fd4\u56de\u4e0b\u9762\u5185\u5bb9\uff1a 1 2 3 5 0027FEA0 5 \u8fd9\u4e2a\u7a0b\u5e8f\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a\u53d8\u91cfx\u5e76\u4e14\u6253\u5370\u51fa\u5b83\u7684\u503c\u3002\u7136\u540e\u6211\u4eec\u6253\u5370\u51fa\u4e86\u53d8\u91cfx\u7684\u5730\u5740\u3002\u6700\u540e\uff0c\u4f7f\u7528\u89e3\u5f15\u7528\u8fd0\u7b97\u7b26\u4ece\u53d8\u91cfx\u7684\u5730\u5740\u83b7\u53d6\u503c\uff08\u5c31\u662fx\u7684\u503c\uff09\uff0c\u7136\u540e\u6253\u5370\u51fa\u6765\u3002 \u5173\u952e\u4fe1\u606f \u5bf9\u4e8e\u4e00\u4e2a\u7ed9\u5b9a\u5730\u5740\uff0c\u53ef\u4ee5\u4f7f\u7528 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u83b7\u53d6\u8be5\u5730\u5740\u4fdd\u5b58\u7684\u503c\uff08\u5de6\u503c\uff09 \u89e3\u5f15\u7528\u548c\u53d6\u5730\u5740\u662f\u4e00\u5bf9\u76f8\u53cd\u7684\u64cd\u4f5c\uff0c\u53d6\u5730\u5740\u83b7\u53d6\u5bf9\u8c61\u7684\u5730\u5740\uff0c\u89e3\u5f15\u7528\u4ece\u5730\u5740\u83b7\u53d6\u5bf9\u8c61\u3002 \u5c0f\u8d34\u58eb \u5c3d\u7ba1\u89e3\u5f15\u7528\u770b\u8d77\u6765\u5c31\u50cf\u4e58\u6cd5\uff0c\u4f46\u662f\u4f60\u53ef\u4ee5\u533a\u5206\u5b83\u4eec\uff0c\u56e0\u4e3a\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26\u662f\u4e00\u5143\u7684\uff0c\u800c\u4e58\u6cd5\u64cd\u4f5c\u7b26\u662f\u4e8c\u5143\u7684\u3002 \u83b7\u53d6\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\uff0c\u7136\u540e\u7acb\u5373\u89e3\u5f15\u7528\u8be5\u5730\u5740\u4ee5\u83b7\u5f97\u503c\uff0c\u8fd9\u5176\u5b9e\u4e5f\u4e0d\u7b97\u662f\u4ec0\u4e48\u6709\u7528\u7684\u64cd\u4f5c(\u6bd5\u7adf\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u53d8\u91cf\u7684\u503c)\u3002 \u4e0d\u8fc7\uff0c\u5728\u4e86\u89e3\u53d6\u5730\u5740\u548c\u89e3\u5f15\u7528\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb\u4ecb\u7ecd\u6307\u9488\u4e86\u3002","title":"\u89e3\u5f15\u7528\u8fd0\u7b97\u7b26*"},{"location":"9-6-Introduction-to-pointers/#_3","text":"\u6307\u9488\u53ef\u4ee5\u770b\u505a\u662f\u4e00\u4e2a\u6301\u6709\u5185\u5b58\u5730\u5740\u7684\u5bf9\u8c61\uff08\u901a\u5e38\u662f\u5176\u4ed6\u5bf9\u8c61\u7684\u5730\u5740\uff09\u3002\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u5728\u540e\u9762\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528\u8be5\u5730\u5740\u3002 \u9898\u5916\u8bdd \u5728\u73b0\u4ee3 C++ \u4e2d\uff0c\u6211\u4eec\u6b64\u5904\u8c08\u8bba\u7684\u6307\u9488\u88ab\u79f0\u4e3a \u539f\u59cb\u6307\u9488 \u6216 \u7b28\u6307\u9488 \uff0c\u4ee5\u4fbf\u5c06\u5176\u4e0e \u667a\u80fd\u6307\u9488 \u533a\u522b\u5f00\u6765\u3002\u667a\u80fd\u6307\u9488\u4f1a\u5728 chapter M \u4ecb\u7ecd\u3002 \u548c\u4f7f\u7528&\u5b9a\u4e49\u5f15\u7528\u7684\u505a\u6cd5\u7c7b\u4f3c\uff0c\u6307\u9488\u7c7b\u578b\u4f7f\u7528 * \u6765\u5b9a\u4e49\uff1a 1 2 3 4 int ; // a normal int int & ; // an lvalue reference to an int value int * ; // a pointer to an int value (holds the address of an integer value) \u8981\u521b\u5efa\u6307\u9488\u53d8\u91cf\uff0c\u53ea\u9700\u5b9a\u4e49\u4e00\u4e2a\u6307\u9488\u7c7b\u578b\u7684\u53d8\u91cf: 1 2 3 4 5 6 7 8 9 int main () { int x { 5 }; // normal variable int & ref { x }; // a reference to an integer (bound to x) int * ptr ; // a pointer to an integer return 0 ; } \u6ce8\u610f\uff0c\u8fd9\u4e2a\u661f\u53f7\u662f\u6307\u9488\u58f0\u660e\u8bed\u6cd5\u7684\u4e00\u90e8\u5206\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26\u3002 \u6700\u4f73\u5b9e\u8df5 \u58f0\u660e\u6307\u9488\u7c7b\u578b\u65f6\uff0c\u5728\u7c7b\u578b\u540d\u79f0\u65c1\u8fb9\u52a0\u4e0a\u661f\u53f7\u3002 \u6ce8\u610f \u5c3d\u7ba1\u901a\u5e38\u60c5\u51b5\u4e0b\u6211\u4eec\u4e0d\u5e94\u8be5\u5728\u4e00\u884c\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\uff0c\u4f46\u662f\u5982\u679c\u9700\u8981\u8fd9\u4e48\u505a\uff0c\u5219\u6bcf\u4e2a\u6307\u9488\u53d8\u91cf\u524d\u9762\u90fd\u9700\u8981\u661f\u53f7 1 2 int * ptr1 , ptr2 ; // incorrect: ptr1 is a pointer to an int, but ptr2 is just a plain int! int * ptr3 , * ptr4 ; // correct: ptr3 and p4 are both pointers to an int \u867d\u7136\u8fd9\u4e2a\u4f8b\u5b50\u65f6\u5e38\u88ab\u4f5c\u4e3a\u201d\u661f\u53f7\u4e0d\u5e94\u8be5\u4e0e\u7c7b\u578b\u540d\u653e\u5728\u4e00\u8d77\u201c\u7684\u8bba\u636e\uff0c\u4f46\u6211\u4eec\u5012\u4e0d\u5982\u8bf4\u8fd9\u662f\u4e00\u4e2a\u201d\u4e0d\u5e94\u5728\u4e00\u884c\u4e2d\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\u7684\u201c\u4f8b\u5b50\u3002","title":"\u6307\u9488"},{"location":"9-6-Introduction-to-pointers/#_4","text":"\u548c\u666e\u901a\u7684\u53d8\u91cf\u4e0d\u540c\uff0c \u6307\u9488\u9ed8\u8ba4\u662f\u4e0d\u521d\u59cb\u5316\u7684 \u3002\u4e00\u4e2a\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u7684\u6307\u9488\uff0c\u6709\u65f6\u79f0\u4e3a \u91ce\u6307\u9488 \u3002\u91ce\u6307\u9488\u4e2d\u5b58\u653e\u7684\u662f\u4e00\u4e2a\u65e0\u7528\u7684\u5730\u5740\uff0c\u5bf9\u91ce\u6307\u9488\u89e3\u5f15\u7528\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4e00\u5b9a\u8981\u5c06\u6307\u9488\u521d\u59cb\u5316\u4e3a\u4e00\u4e2a\u5df2\u77e5\u7684\u503c\u3002 \u6700\u4f73\u5b9e\u8df5 \u6307\u9488\u5fc5\u987b\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 10 int main () { int x { 5 }; int * ptr ; // \u4e00\u4e2a\u672a\u521d\u59cb\u5316\u7684\u6307\u9488 (\u5b58\u653e\u8fd9\u65e0\u6548\u5730\u5740) int * ptr2 {}; // \u4e00\u4e2a\u7a7a\u6307\u9488 (\u7a0d\u540e\u8ba8\u8bba\u8fd9\u79cd\u60c5\u51b5) int * ptr3 { & x }; // \u6307\u9488\uff0c\u521d\u59cb\u5316\u4e3a\u53d8\u91cfx\u7684\u5730\u5740 return 0 ; } \u7531\u4e8e\u6307\u9488\u4fdd\u5b58\u7684\u662f\u5730\u5740\uff0c\u6240\u4ee5\u5176\u521d\u59cb\u5316\u503c\u5fc5\u987b\u662f\u4e00\u4e2a\u5730\u5740\u3002\u901a\u5e38\uff0c\u6307\u9488\u7528\u4e8e\u4fdd\u5b58\u53e6\u4e00\u4e2a\u53d8\u91cf\u7684\u5730\u5740(\u53ef\u4ee5\u4f7f\u7528\u53d6\u5730\u5740\u64cd\u4f5c\u7b26(&)\u83b7\u53d6)\u3002 \u4e00\u65e6\u6211\u4eec\u6709\u4e86\u4e00\u4e2a\u6301\u6709\u53e6\u4e00\u4e2a\u5bf9\u8c61\u5730\u5740\u7684\u6307\u9488\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26 * \u6765\u8bbf\u95ee\u8be5\u5730\u5740\u7684\u503c\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int x { 5 }; std :: cout << x << '\\n' ; // \u6253\u5370\u53d8\u91cf x \u7684\u503c int * ptr { & x }; // ptr \u4fdd\u5b58\u7740 x \u7684\u5730\u5740 std :: cout << * ptr << '\\n' ; // \u4f7f\u7528\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26\u6253\u5370\u8be5\u5730\u5740\u7684\u5185\u5bb9 return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 5 5 \u5f62\u8c61\u4e00\u70b9\u6765\u770b\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u662f\u8fd9\u6837\u7684\uff1a \u8fd9\u5c31\u662f\u6307\u9488\u540d\u79f0\u7684\u7531\u6765\u2014\u2014 ptr \u4fdd\u5b58\u7740 x \u7684\u5730\u5740\uff0c\u6240\u4ee5\u6211\u4eec\u8bf4 ptr \u662f\u201c\u6307\u5411 x \u3002 \u4f5c\u8005\u6ce8 \u5173\u4e8e\u6307\u9488\u547d\u540d\u7684\u6ce8\u610f\u4e8b\u9879:\u201cX\u6307\u9488\u201d(\u5176\u4e2dX\u662f\u67d0\u79cd\u7c7b\u578b)\u662f\u201c\u6307\u5411X\u7684\u6307\u9488\u201d\u7684\u5e38\u7528\u7f29\u5199\u3002\u6240\u4ee5\u5f53\u6211\u4eec\u8bf4\u201c\u4e00\u4e2a\u6574\u6570\u6307\u9488\u201d\u65f6\uff0c\u6211\u4eec\u5b9e\u9645\u4e0a\u662f\u6307\u201c\u4e00\u4e2a\u6307\u5411\u6574\u578b\u53d8\u91cf\u7684\u6307\u9488\u201d\u3002\u5f53\u6211\u4eec\u8ba8\u8bbaconst\u6307\u9488\u65f6\uff0c\u8fd9\u79cd\u533a\u522b\u5c31\u5f88\u91cd\u8981\u4e86\u3002 \u5c31\u50cf\u5f15\u7528\u7684\u7c7b\u578b\u5fc5\u987b\u5339\u914d\u88ab\u5f15\u7528\u5bf9\u8c61\u7684\u7c7b\u578b\u4e00\u6837\uff0c\u6307\u9488\u7684\u7c7b\u578b\u5fc5\u987b\u5339\u914d\u88ab\u6307\u5411\u5bf9\u8c61\u7684\u7c7b\u578b: 1 2 3 4 5 6 7 8 9 10 int main () { int i { 5 }; double d { 7.0 }; int * iPtr { & i }; // ok: a pointer to an int can point to an int object int * iPtr2 { & d }; // not okay: a pointer to an int can't point to a double double * dPtr { & d }; // ok: a pointer to a double can point to a double object double * dPtr2 { & i }; // not okay: a pointer to a double can't point to an int } \u9664\u4e86\u4e00\u4e2a\u4f8b\u5916\uff0c\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u8ba8\u8bba\uff0c\u7528\u5b57\u9762\u91cf\u521d\u59cb\u5316\u6307\u9488\u662f\u4e0d\u5141\u8bb8\u7684\uff1a 1 2 int * ptr { 5 }; // \u4e0d\u53ef\u4ee5 int * ptr { 0x0012FF7C }; // \u4e0d\u53ef\u4ee5, 0x0012FF7C \u88ab\u770b\u505a\u6574\u578b\u5b57\u9762\u91cf","title":"\u6307\u9488\u7684\u521d\u59cb\u5316"},{"location":"9-6-Introduction-to-pointers/#_5","text":"\u5728\u8c08\u8bba\u6307\u9488\u7684\u8d4b\u503c\u65f6\uff0c\u53ef\u80fd\u6709\u4e24\u79cd\u542b\u4e49\uff1a \u6539\u53d8\u6307\u9488\u7684\u503c\u4f7f\u5176\u6307\u5411\u5176\u4ed6\u5730\u5740\uff08\u7ed9\u6307\u9488\u8d4b\u503c\u4e00\u4e2a\u65b0\u5730\u5740\uff09\uff1b \u6539\u53d8\u6307\u9488\u6240\u6307\u5185\u5bb9\u7684\u503c\uff08\u7ed9\u6307\u9488\u89e3\u5f15\u7528\u7684\u7ed3\u679c\u8d4b\u65b0\u503c\uff09\u3002 \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u6307\u9488\u88ab\u66f4\u6539\u4e3a\u6307\u5411\u53e6\u4e00\u4e2a\u5bf9\u8c61\u7684\u60c5\u51b5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { int x { 5 }; int * ptr { & x }; // ptr \u521d\u59cb\u5316\u4e3a\u6307\u5411 x std :: cout << * ptr << '\\n' ; // \u6253\u5370\u5730\u5740\u503c(x\u7684\u5730\u5740) int y { 6 }; ptr = & y ; // // ptr \u66f4\u6539\u4e3a\u6307\u5411 y std :: cout << * ptr << '\\n' ; // \u6253\u5370\u5730\u5740\u503c (y\u7684\u5730\u5740) return 0 ; } \u6253\u5370\uff1a 1 2 5 6 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u6307\u9488 ptr \u5e76\u7528x\u7684\u5730\u5740\u521d\u59cb\u5316\u5b83\u3002\u5bf9\u6307\u9488\u89e3\u5f15\u7528\u4ee5\u6253\u5370\u88ab\u6307\u5411\u7684\u503c( 5 )\u3002\u7136\u540e\u4f7f\u7528\u8d4b\u503c\u64cd\u4f5c\u7b26\u5c06 ptr \u4fdd\u5b58\u7684\u5730\u5740\u66f4\u6539\u4e3ay\u7684\u5730\u5740\u3002\u7136\u540e\u518d\u6b21\u89e3\u5f15\u7528\u8be5\u6307\u9488\u4ee5\u6253\u5370\u88ab\u6307\u5411\u7684\u503c(\u73b0\u5728\u662f 6 )\u3002 \u73b0\u5728\u8ba9\u6211\u4eec\u770b\u770b\u5982\u4f55\u4f7f\u7528\u6307\u9488\u6765\u6539\u53d8\u88ab\u6307\u5411\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { int x { 5 }; int * ptr { & x }; // initialize ptr with address of variable x std :: cout << x << '\\n' ; // print x's value std :: cout << * ptr << '\\n' ; // print the value at the address that ptr is holding (x's address) * ptr = 6 ; // The object at the address held by ptr (x) assigned value 6 (note that ptr is dereferenced here) std :: cout << x << '\\n' ; std :: cout << * ptr << '\\n' ; // print the value at the address that ptr is holding (x's address) return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 5 5 6 6 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u6307\u9488 ptr \uff0c\u5b83\u7684\u521d\u59cb\u5316\u4e3a\u53d8\u91cf x \u7684\u5730\u5740\uff0c\u7136\u540e\u6253\u5370\u4e86 x \u548c *ptr \u7684\u503c( 5 )\u3002\u56e0\u4e3a *ptr \u8fd4\u56de\u7684\u662f\u5de6\u503c\uff0c\u6240\u6211\u4eec\u53ef\u4ee5\u628a\u5b83\u7528\u5728\u8d4b\u503c\u53f7\u7684\u5de6\u4fa7\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06 ptr \u6240\u6307\u7684\u53d8\u91cf\u7684\u503c\u4fee\u6539\u4e3a 6 \u3002\u7136\u540e\uff0c\u518d\u6b21\u6253\u5370 x \u548c *ptr \u4ee5\u4fbf\u786e\u5b9a\u6211\u4eec\u7684\u4fee\u6539\u662f\u5426\u751f\u6548\u3002 \u5173\u952e\u4fe1\u606f \u5728\u4f7f\u7528\u6307\u9488\u65f6\uff0c\u5982\u679c\u4e0d\u540c\u65f6\u4f7f\u7528 \u89e3\u5f15\u7528 \u7684\u65f6\u5019\uff0c\u6211\u4eec\u4f7f\u7528\u7684\u662f\u6307\u9488\u5b58\u653e\u7684\u5730\u5740\u3002\u4fee\u6539\u8be5\u5730\u5740\u5c31\u53ef\u4ee5\u6539\u53d8\u6307\u9488\u7684\u6307\u5411 ( ptr = &y )\u3002 \u5f53\u5bf9\u6307\u9488\u5e94\u7528\u89e3\u5f15\u7528\u65f6( *ptr )\uff0c\u6211\u4eec\u8bbf\u95ee\u7684\u5219\u662f\u6307\u9488\u6240\u6307\u7684\u5bf9\u8c61\u3002\u4fee\u6539( *ptr = 6; ) \u5b83\u53ef\u4ee5\u6539\u53d8\u8be5\u53d8\u91cf\u7684\u503c\u3002","title":"\u6307\u9488\u548c\u8d4b\u503c"},{"location":"9-6-Introduction-to-pointers/#_6","text":"\u6307\u9488\u548c\u5de6\u503c\u5f15\u7528\u7684\u884c\u4e3a\u7c7b\u4f3c\u3002\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include int main () { int x { 5 }; int & ref { x }; // get a reference to x int * ptr { & x }; // get a pointer to x std :: cout << x ; std :: cout << ref ; // use the reference to print x's value (5) std :: cout << * ptr << '\\n' ; // use the pointer to print x's value (5) ref = 6 ; // use the reference to change the value of x std :: cout << x ; std :: cout << ref ; // use the reference to print x's value (6) std :: cout << * ptr << '\\n' ; // use the pointer to print x's value (6) * ptr = 7 ; // use the pointer to change the value of x std :: cout << x ; std :: cout << ref ; // use the reference to print x's value (7) std :: cout << * ptr << '\\n' ; // use the pointer to print x's value (7) return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 2 3 555 666 777 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u503c\u4e3a5\u7684\u666e\u901a\u53d8\u91cf x \uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u5de6\u503c\u5f15\u7528\u548c\u4e00\u4e2a\u6307\u5411 x \u7684\u6307\u9488\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528\u5de6\u503c\u5f15\u7528\u5c06\u503c\u4ece5\u66f4\u6539\u4e3a6\uff0c\u5e76\u8bf4\u660e\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6240\u6709\u4e09\u4e2a\u65b9\u6cd5\u8bbf\u95ee\u66f4\u65b0\u540e\u7684\u503c\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u89e3\u5f15\u7528\u7684\u6307\u9488\u5c06\u503c\u4ece6\u6539\u4e3a7\uff0c\u5e76\u518d\u6b21\u8868\u660e\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6240\u6709\u4e09\u79cd\u65b9\u6cd5\u8bbf\u95ee\u66f4\u65b0\u540e\u7684\u503c\u3002 \u56e0\u6b64\uff0c\u6307\u9488\u548c\u5f15\u7528\u90fd\u63d0\u4f9b\u4e86\u95f4\u63a5\u8bbf\u95ee\u53e6\u4e00\u4e2a\u5bf9\u8c61\u7684\u65b9\u6cd5\u3002\u4e3b\u8981\u7684\u533a\u522b\u5728\u4e8e\uff0c\u5bf9\u4e8e\u6307\u9488\uff0c\u6211\u4eec\u9700\u8981\u663e\u5f0f\u5730\u83b7\u53d6\u8981\u6307\u5411\u7684\u5730\u5740\uff0c\u5e76\u4e14\u663e\u5f0f\u5730\u89e3\u5f15\u7528\u6307\u9488\u6765\u83b7\u53d6\u503c\u3002\u5bf9\u4e8e\u5f15\u7528\uff0c\u5730\u5740\u548c\u89e3\u5f15\u7528\u90fd\u662f\u9690\u5f0f\u8fdb\u884c\u7684\u3002 \u6307\u9488\u548c\u5f15\u7528\u4e4b\u95f4\u8fd8\u6709\u4e00\u4e9b\u503c\u5f97\u4e00\u63d0\u7684\u533a\u522b\uff1a \u5f15\u7528\u5fc5\u987b\u88ab\u521d\u59cb\u5316\uff0c\u6307\u9488\u5e76\u4e0d\u662f\u5fc5\u987b\u521d\u59cb\u5316\uff08\u4f46\u662f\u4f60\u5e94\u8be5\u521d\u59cb\u5316\uff09\uff1b \u5f15\u7528\u4e0d\u662f\u5bf9\u8c61\uff0c\u6307\u9488\u662f\u5bf9\u8c61\uff1b \u5f15\u7528\u4e0d\u80fd\u88ab\u91cd\u65b0\u8bbe\u7f6e\uff08\u4fee\u6539\u5f15\u7528\u4f7f\u5176\u7ed1\u5b9a\u5230\u5176\u4ed6\u5bf9\u8c61\uff09\uff0c\u6307\u9488\u5219\u53ef\u4ee5\u88ab\u4fee\u6539\u6307\u5411\u5176\u4ed6\u5bf9\u8c61\uff1b \u5f15\u7528\u603b\u662f\u7ed1\u5b9a\u5230\u67d0\u4e2a\u5bf9\u8c61\uff0c\u6307\u9488\u53ef\u4ee5\u4e0d\u6267\u884c\u4efb\u4f55\u5bf9\u8c61\uff1b \u5f15\u7528\u662f\u201d\u5b89\u5168\u201c\u7684\uff08\u9664\u4e86 \u60ac\u5782 \u5f15\u7528\uff09\uff0c\u6307\u9488\u5219\u662f\u5371\u9669\u7684\u3002","title":"\u6307\u9488\u548c\u5de6\u503c\u5f15\u7528\u5f88\u50cf"},{"location":"9-6-Introduction-to-pointers/#_7","text":"\u9700\u8981\u6ce8\u610f\uff0c \u53d6\u5730\u5740\u64cd\u4f5c\u7b26 & \u8fd4\u56de\u7684\u5e76\u4e0d\u662f\u5730\u5740\u7684\u5b57\u9762\u91cf\uff0c\u5b83\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u6307\u5411\u64cd\u4f5c\u6570\u5730\u5740\u7684\u6307\u9488\uff0c\u8be5\u6307\u9488\u7684\u7c7b\u578b\u53d6\u51b3\u4e8e\u53c2\u6570\u7684\u7c7b\u578b\uff08\u4f8b\u5982\uff0c\u83b7\u53d6\u6574\u578b\u53d8\u91cf\u7684\u5730\u5740\u8fd4\u56de\u7684\u662f\u6574\u578b\u6307\u9488\uff09\u3002 \u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { int x { 4 }; std :: cout << typeid ( & x ). name () << '\\n' ; // print the type of &x return 0 ; } \u5728 Visual Studio \u4e2d\u4f1a\u6253\u5370\u5982\u4e0b\u5185\u5bb9\uff1a 1 int * \u5982\u679c\u662fgcc\uff0c\u5219\u4f1a\u6253\u5370\u201cpi\u201d (\u6307\u5411int\u7684\u6307\u9488\u2014\u2014pointer to int)\u3002\u56e0\u4e3a typeid().name() \u7684\u7ed3\u679c\u53d6\u51b3\u4e8e\u7f16\u8bd1\u5668\uff08 typeid \u8fd0\u7b97\u7b26 \uff09\uff0c\u6240\u4ee5\u5b83\u7684\u6253\u5370\u7ed3\u679c\u53ef\u80fd\u662f\u4e0d\u540c\u7684\uff0c\u4e0d\u8fc7\u610f\u601d\u662f\u4e00\u6837\u7684\u3002","title":"\u53d6\u5730\u5740\u8fd0\u7b97\u7b26\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u6307\u9488"},{"location":"9-6-Introduction-to-pointers/#_8","text":"\u6307\u9488\u7684\u5927\u5c0f\u53d6\u51b3\u4e8e\u8be5\u53ef\u6267\u884c\u7a0b\u5e8f\u7f16\u8bd1\u7684\u4f53\u7cfb\u7ed3\u6784\u2014\u201432\u4f4d\u53ef\u6267\u884c\u7a0b\u5e8f\u4f7f\u752832\u4f4d\u5730\u5740\u2014\u2014\u5219\u4e00\u4e2a\u6307\u9488\u572832\u4f4d\u673a\u5668\u4e0a\u7684\u5927\u5c0f\u662f4\u4e2a\u5b57\u8282\u3002\u5bf9\u4e8e64\u4f4d\u53ef\u6267\u884c\u7a0b\u5e8f\uff0c\u6307\u9488\u5927\u5c0f\u4e3a64\u4f4d\uff088\u5b57\u8282\uff09\u3002\u6ce8\u610f\uff0c\u4e0d\u8bba\u6307\u9488\u6240\u6307\u5bf9\u8c61\u7684\u5927\u5c0f\u662f\u591a\u5c11\uff0c\u6307\u9488\u672c\u8eab\u7684\u5927\u5c0f\u603b\u662f\u7b26\u5408\u4e0a\u9762\u7684\u89c4\u5219\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () // \u5047\u8bbe\u4e3a32\u4f4d\u7a0b\u5e8f { char * chPtr {}; // char \u4e3a1\u4e2a\u5b57\u8282 int * iPtr {}; // int\u901a\u5e38\u662f4\u5b57\u8282 long double * ldPtr {}; // long doubles \u901a\u5e38\u662f8\u5b57\u8282\u621612\u5b57\u8282 std :: cout << sizeof ( chPtr ) << '\\n' ; // \u6253\u5370 4 std :: cout << sizeof ( iPtr ) << '\\n' ; // \u6253\u5370 4 std :: cout << sizeof ( ldPtr ) << '\\n' ; // \u6253\u5370 4 return 0 ; } \u6307\u9488\u7684\u5927\u5c0f\u603b\u662f\u76f8\u540c\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u6307\u9488\u53ea\u662f\u4e00\u4e2a\u5185\u5b58\u5730\u5740\uff0c\u800c\u8bbf\u95ee\u5185\u5b58\u5730\u5740\u6240\u9700\u7684\u6bd4\u7279\u6570\u662f\u6052\u5b9a\u7684\u3002","title":"\u6307\u9488\u7684\u5927\u5c0f"},{"location":"9-6-Introduction-to-pointers/#_9","text":"\u5c31\u50cf\u60ac\u5782\u5f15\u7528\u4e00\u6837\uff0c\u60ac\u5782\u6307\u9488\u662f\u4fdd\u5b58\u4e0d\u518d\u6709\u6548\u7684\u5bf9\u8c61\u5730\u5740\u7684\u6307\u9488(\u4f8b\u5982\uff0c\u5bf9\u8c61\u5df2\u88ab\u9500\u6bc1)\u3002\u5bf9\u60ac\u6d6e\u6307\u9488\u7684\u89e3\u5f15\u7528\u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u521b\u5efa\u60ac\u6d6e\u6307\u9488\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int main () { int x { 5 }; int * ptr { & x }; std :: cout << * ptr << '\\n' ; // valid { int y { 6 }; ptr = & y ; std :: cout << * ptr << '\\n' ; // valid } // y goes out of scope, and ptr is now dangling std :: cout << * ptr << '\\n' ; // undefined behavior from dereferencing a dangling pointer return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 2 3 5 6 6 \u4f46\u662f\uff0c\u7ed3\u679c\u4e5f\u53ef\u80fd\u4e0d\u662f\u4e0a\u9762\u8fd9\u6837\uff0c\u56e0\u4e3a ptr \u6240\u6307\u7684\u5bf9\u8c61\u5728 \u79bb\u5f00\u4f5c\u7528\u57df \u65f6(\u5185\u5c42\u8bed\u53e5\u5757)\u4ee5\u53ca\u88ab\u9500\u6bc1\u4e86\uff0c\u6b64\u65f6\u7684 ptr \u662f\u4e00\u4e2a\u60ac\u5782\u6307\u9488\u3002","title":"\u60ac\u5782\u6307\u9488"},{"location":"9-6-Introduction-to-pointers/#_10","text":"\u6307\u9488\u662f\u4fdd\u5b58\u5185\u5b58\u5730\u5740\u7684\u53d8\u91cf\u3002\u53ef\u4ee5\u4f7f\u7528 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u5bf9\u5b83\u4eec\u8fdb\u884c\u89e3\u5f15\u7528\uff0c\u4ee5\u83b7\u53d6\u6307\u9488\u6240\u6301\u6709\u7684\u5730\u5740\u7684\u503c\u3002\u5bf9 \u91ce\u6307\u9488 \u6216 \u60ac\u5782 \u6307\u9488(\u6216\u7a7a\u6307\u9488)\u7684\u89e3\u5f15\u7528\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u5e76\u53ef\u80fd\u5bfc\u81f4\u5e94\u7528\u7a0b\u5e8f\u5d29\u6e83\u3002 \u6307\u9488\u6bd4\u5f15\u7528\u66f4\u7075\u6d3b\uff0c\u4e5f\u66f4\u5371\u9669\u3002\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\u7ee7\u7eed\u63a2\u8ba8\u8fd9\u4e2a\u95ee\u9898\u3002","title":"\u5c0f\u7ed3"},{"location":"9-7-Null-pointers/","tags":["pointer"],"text":"Key Takeaway \u6307\u9488\u53ef\u4ee5\u901a\u8fc7\u503c\u521d\u59cb\u5316\u8bbe\u4e3a\u7a7a\u503c\u3002 int* ptr{}; \u5f53\u9700\u8981\u7a7a\u6307\u9488\u5b57\u9762\u91cf\u7528\u4e8e\u6307\u9488\u7684\u521d\u59cb\u5316\u3001\u8d4b\u503c\u6216\u4f5c\u4e3a\u53c2\u6570\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 nullptr \u5bf9\u7a7a\u6307\u9488\u89e3\u5f15\u7528\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a \u6574\u578b\u503c\u4f1a\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff1a\u6574\u578b\u503c0\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c false \uff0c\u800c\u5176\u4ed6\u4efb\u4f55\u6574\u578b\u503c\u90fd\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c true \u5982\u679c\u6307\u9488\u662f\u7a7a\u6307\u9488\u5219\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u7c7b\u578bfalse\uff0c\u5982\u679c\u4e0d\u662f\u975e\u7a7a\u6307\u9488\u5219\u8f6c\u6362\u4e3atrue \u6761\u4ef6\u5224\u65ad\u53ea\u80fd\u7528\u4e8e\u533a\u5206\u7a7a\u6307\u9488\u4e0e\u975e\u7a7a\u6307\u9488\u3002\u6ca1\u6709\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u786e\u5b9a\u975e\u7a7a\u6307\u9488\u662f\u6307\u5411\u6709\u6548\u5bf9\u8c61\u8fd8\u662f\u60ac\u7a7a\u6307\u9488(\u6307\u5411\u65e0\u6548\u5bf9\u8c61)\u3002\u6307\u9488\u5e94\u8be5\u4fdd\u5b58\u6709\u6548\u5bf9\u8c61\u7684\u5730\u5740\uff0c\u6216\u8005\u8bbe\u7f6e\u4e3anullptr\u3002\u8fd9\u6837\u6211\u4eec\u53ea\u9700\u8981\u6d4b\u8bd5\u6307\u9488\u662f\u5426\u4e3a\u7a7a\uff0c\u5e76\u4e14\u53ef\u4ee5\u5047\u8bbe\u4efb\u4f55\u975e\u7a7a\u6307\u9488\u90fd\u662f\u6709\u6548\u7684\u3002 \u5f53\u4e00\u4e2a\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\uff0c\u6307\u5411\u88ab\u9500\u6bc1\u5bf9\u8c61\u7684\u4efb\u4f55\u6307\u9488\u90fd\u5c06\u6210\u4e3a\u60ac\u5782\u6307\u9488(\u5b83\u4eec\u4e0d\u4f1a\u81ea\u52a8\u8bbe\u7f6e\u4e3a nullptr )\u3002\u68c0\u6d4b\u8fd9\u4e9b\u60c5\u51b5\u5e76\u786e\u4fdd\u8fd9\u4e9b\u6307\u9488\u968f\u540e\u88ab\u8bbe\u7f6e\u4e3a nullptr \u662f\u4f60\u7684\u8d23\u4efb\u3002 \u73b0\u4ee3C++\u4e2d\u5e94\u8be5\u907f\u514d\u4f7f\u75280\u548cNULL\u8fd9\u4e24\u4e2a\u5386\u53f2\u9057\u7559\u7684\u6307\u9488\u5b57\u9762\u91cf\uff08\u5e94\u8be5\u4f7f\u7528 nullpt \u6765\u4ee3\u66ff\uff09 \u5728\u4e0a\u8282\u8bfe\u4e2d ( 9.6 - \u6307\u9488\u7b80\u4ecb )\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u6307\u9488\u7684\u57fa\u672c\u77e5\u8bc6\u2014\u2014\u6307\u9488\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5b83\u6301\u6709\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61\u7684\u5730\u5740\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 \u89e3\u5f15\u7528\u64cd\u4f5c\u7b26 \u5bf9\u5730\u5740\u8fdb\u884c\u89e3\u5f15\u7528\u4ee5\u83b7\u5f97\u8be5\u5730\u5740\u5b58\u653e\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int x { 5 }; std :: cout << x << '\\n' ; // print the value of variable x int * ptr { & x }; // ptr holds the address of x std :: cout << * ptr << '\\n' ; // use dereference operator to print the value at the address that ptr is holding (which is x's address) return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 5 5 \u5728\u4e0a\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u8fd8\u6ce8\u610f\u5230\u6307\u9488\u53ef\u4ee5\u4e0d\u6307\u5411\u4efb\u4f55\u4e1c\u897f\u3002\u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u63a2\u7d22\u8fd9\u79cd\u6307\u9488(\u4ee5\u53ca\u6307\u5411\u865a\u65e0\u7684\u5404\u79cd\u542b\u4e49)\u3002 \u7a7a\u6307\u9488\uff08Null\uff09 \u9664\u4e86\u5185\u5b58\u5730\u5740\uff0c\u6307\u9488\u8fd8\u53ef\u4ee5\u4fdd\u5b58\u4e00\u4e2a\u989d\u5916\u7684\u503c\uff1a\u7a7a\u503c\u3002\u7a7a\u503c(\u901a\u5e38\u7f29\u5199\u4e3anull)\u662f\u4e00\u79cd\u7279\u6b8a\u503c\uff0c\u8868\u793a\u67d0\u7269\u6ca1\u6709\u503c\u3002\u5f53\u6307\u9488\u6301\u6709\u7a7a\u503c\u65f6\uff0c\u610f\u5473\u7740\u8be5\u6307\u9488\u4e0d\u6307\u5411\u4efb\u4f55\u4e1c\u897f\u3002\u8fd9\u6837\u7684\u6307\u9488\u79f0\u4e3a\u7a7a\u6307\u9488\u3002 \u521b\u5efa\u7a7a\u6307\u9488\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u503c\u521d\u59cb\u5316: 1 2 3 4 5 6 int main () { int * ptr {}; // ptr is now a null pointer, and is not holding an address return 0 ; } \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f60\u4e0d\u80fd\u5c06\u6307\u9488\u521d\u59cb\u5316\u4e3a\u4e00\u4e2a\u6709\u6548\u7684\u5730\u5740\uff0c\u8bf7\u4f7f\u7528\u503c\u521d\u59cb\u5316\u5c06\u5176\u521d\u59cb\u5316\u4e3a\u7a7a\u6307\u9488 \u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8d4b\u503c\u6765\u6539\u53d8\u6307\u9488\u6240\u6307\u5411\u7684\u5bf9\u8c61\uff0c\u6240\u4ee5\u6700\u521d\u88ab\u8bbe\u7f6e\u4e3a\u7a7a\u7684\u6307\u9488\u4ee5\u540e\u53ef\u4ee5\u88ab\u66f4\u6539\u4e3a\u6307\u5411\u4e00\u4e2a\u6709\u6548\u7684\u5bf9\u8c61: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { int * ptr {}; // ptr is a null pointer, and is not holding an address int x { 5 }; ptr = & x ; // ptr now pointing at object x (no longer a null pointer) std :: cout << * ptr << '\\n' ; // print value of x through dereferenced ptr return 0 ; } nullptr \u5173\u952e\u5b57 \u7c7b\u4f3c\u4e8e true \u548c false \u7528\u4e8e\u8868\u793a\u5e03\u5c14\u5b57\u9762\u91cf\u503c\uff0c nullptr \u5173\u952e\u5b57\u5219\u7528\u4e8e\u8868\u793a\u7a7a\u6307\u9488\u5b57\u9762\u91cf\u3002\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 nullptr \u663e\u5f0f\u5730\u5c06\u6307\u9488\u521d\u59cb\u5316\u6216\u8d4b\u503c\u4e3a\u7a7a\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 int main () { int * ptr { nullptr }; // can use nullptr to initialize a pointer to be a null pointer int value { 5 }; int * ptr2 { & value }; // ptr2 is a valid pointer ptr2 = nullptr ; // Can assign nullptr to make the pointer a null pointer someFunction ( nullptr ); // we can also pass nullptr to a function that has a pointer parameter return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u6307\u9488 ptr2 \u7684\u503c\u8bbe\u7f6e\u4e3a nullptr \uff0c\u8fd9\u4f7f\u5f97 ptr2 \u79f0\u4e3a\u4e00\u4e2a\u7a7a\u6307\u9488\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u9700\u8981\u7a7a\u6307\u9488\u5b57\u9762\u91cf\u7528\u4e8e\u6307\u9488\u7684\u521d\u59cb\u5316\u3001\u8d4b\u503c\u6216\u4f5c\u4e3a\u53c2\u6570\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 nullptr \u3002 \u5bf9\u7a7a\u6307\u9488\u89e3\u5f15\u7528\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a \u7c7b\u4f3c\u4e8e\u5bf9\u60ac\u5782\u6307\u9488\u6216\u91ce\u6307\u9488\u89e3\u5f15\u7528\uff0c\u5bf9\u7a7a\u6307\u9488\u89e3\u5f15\u7528\u4e5f\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4f1a\u4f7f\u4f60\u7684\u7a0b\u5e8f\u5d29\u6e83\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u8bf4\u660e\u4e86\u8fd9\u4e00\u70b9\uff0c\u5f53\u4f60\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u5b83\u53ef\u80fd\u4f1a\u5d29\u6e83\u6216\u5f02\u5e38\u7ec8\u6b62(\u6765\u5427\uff0c\u5c1d\u8bd5\u4e00\u4e0b\uff0c\u4e0d\u4f1a\u641e\u574f\u4f60\u7684\u673a\u5668\u7684)\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { int * ptr {}; // Create a null pointer std :: cout << * ptr << '\\n' ; // Dereference the null pointer return 0 ; } \u4ece\u6982\u5ff5\u4e0a\u8bb2\uff0c\u8fd9\u662f\u6709\u9053\u7406\u7684\u3002\u5bf9\u6307\u9488\u7684\u89e3\u5f15\u7528\u610f\u5473\u7740\u201c\u5230\u6307\u9488\u6240\u6307\u5411\u7684\u5730\u5740\u5e76\u8bbf\u95ee\u90a3\u91cc\u7684\u503c\u201d\u3002\u7a7a\u6307\u9488\u6ca1\u6709\u5730\u5740\u3002\u56e0\u6b64\uff0c\u5f53\u4f60\u8bd5\u56fe\u8bbf\u95ee\u8be5\u5730\u5740\u7684\u503c\u65f6\uff0c\u5b83\u5e94\u8be5\u505a\u4ec0\u4e48? \u610f\u5916\u5730\u89e3\u5f15\u7528\u7a7a\u6307\u9488\u548c\u60ac\u7a7a\u6307\u9488\u662fC++\u7a0b\u5e8f\u5458\u6700\u5e38\u72af\u7684\u9519\u8bef\u4e4b\u4e00\uff0c\u5e76\u4e14\u53ef\u80fd\u662fC++\u7a0b\u5e8f\u5728\u5b9e\u8df5\u4e2d\u5d29\u6e83\u7684\u6700\u5e38\u89c1\u539f\u56e0\u3002 \u6ce8\u610f \u65e0\u8bba\u4f55\u65f6\u4f7f\u7528\u6307\u9488\uff0c\u90fd\u9700\u8981\u683c\u5916\u5c0f\u5fc3\uff0c\u786e\u4fdd\u4ee3\u7801\u6ca1\u6709\u89e3\u5f15\u7528\u7a7a\u6307\u9488\u6216\u60ac\u6d6e\u6307\u9488\uff0c\u56e0\u4e3a\u8fd9\u5c06\u5bfc\u81f4\u672a\u5b9a\u4e49\u7684\u884c\u4e3a(\u53ef\u80fd\u5bfc\u81f4\u5e94\u7528\u7a0b\u5e8f\u5d29\u6e83)\u3002 \u68c0\u67e5\u7a7a\u6307\u9488 \u6b63\u5982\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u6d4b\u8bd5\u5224\u65ad\u5e03\u5c14\u503c\u662f true \u8fd8\u662f false \u90a3\u6837\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5224\u65ad\u4e00\u4e2a\u6307\u9488\u7684\u503c\u662f\u5426\u662f nullptr \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { int x { 5 }; int * ptr { & x }; // pointers convert to Boolean false if they are null, and Boolean true if they are non-null if ( ptr == nullptr ) // explicit test for equivalence std :: cout << \"ptr is null \\n \" ; else std :: cout << \"ptr is non-null \\n \" ; int * nullPtr {}; std :: cout << \"nullPtr is \" << ( nullPtr == nullptr ? \"null \\n \" : \"non-null \\n \" ); // explicit test for equivalence return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u63a5\u8fc7\u4e3a\uff1a 1 2 ptr is non-null nullPtr is null \u5728 4.9 - \u5e03\u5c14\u503c \u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u77e5\u9053==\u6574\u578b\u503c\u4f1a\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff1a\u6574\u578b\u503c0\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c false \uff0c\u800c\u5176\u4ed6\u4efb\u4f55\u6574\u578b\u503c\u90fd\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c true \u3002== \u7c7b\u4f3c\u5730\uff0c\u6307\u9488\u4e5f\u4f1a\u9690\u5f0f\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff1a\u7a7a\u6307\u9488\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c false \uff0c\u975e\u7a7a\u6307\u9488\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c true \u3002\u8fd9\u6837\u4e00\u6765\u6211\u4eec\u53ef\u4ee5\u4e0d\u5fc5\u663e\u5f0f\u5730\u6d4b\u8bd5 nullptr \uff0c\u53ea\u4f7f\u7528\u9690\u5f0f\u8f6c\u6362\u5230\u5e03\u5c14\u503c\u6765\u6d4b\u8bd5\u6307\u9488\u662f\u5426\u4e3a\u7a7a\u6307\u9488\u5373\u53ef\u3002\u4e0b\u9762\u7684\u7a0b\u5e8f\u548c\u4e4b\u524d\u7684\u7a0b\u5e8f\u6548\u679c\u4e00\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { int x { 5 }; int * ptr { & x }; // \u5982\u679c\u6307\u9488\u662f\u7a7a\u6307\u9488\u5219\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u7c7b\u578bfalse\uff0c\u5982\u679c\u4e0d\u662f\u975e\u7a7a\u6307\u9488\u5219\u8f6c\u6362\u4e3atrue if ( ptr ) // implicit conversion to Boolean std :: cout << \"ptr is non-null \\n \" ; else std :: cout << \"ptr is null \\n \" ; int * nullPtr {}; std :: cout << \"nullPtr is \" << ( nullPtr ? \"non-null \\n \" : \"null \\n \" ); // implicit conversion to Boolean return 0 ; } \u6ce8\u610f \u6761\u4ef6\u5224\u65ad\u53ea\u80fd\u7528\u4e8e\u533a\u5206\u7a7a\u6307\u9488\u4e0e\u975e\u7a7a\u6307\u9488\u3002\u6ca1\u6709\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u786e\u5b9a\u975e\u7a7a\u6307\u9488\u662f\u6307\u5411\u6709\u6548\u5bf9\u8c61\u8fd8\u662f\u60ac\u7a7a\u6307\u9488(\u6307\u5411\u65e0\u6548\u5bf9\u8c61)\u3002 \u4f7f\u7528 nullptr \u907f\u514d\u60ac\u5782\u6307\u9488 \u5bf9\u7a7a\u6307\u9488\u6216\u60ac\u7a7a\u6307\u9488\u7684\u89e3\u5f15\u7528\u5c06\u5bfc\u81f4\u672a\u5b9a\u4e49\u7684\u884c\u4e3a\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u4e0d\u5728\u4ee3\u7801\u4e2d\u505a\u8fd9\u4e24\u4ef6\u4e8b\u3002 \u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u907f\u514d\u5bf9\u7a7a\u6307\u9488\u7684\u89e3\u5f15\u7528\uff0c\u5728\u5c1d\u8bd5\u89e3\u5f15\u7528\u4e4b\u524d\u5148\u5bf9\u6307\u9488\u8fdb\u884c\u5224\u7a7a\u5373\u53ef\uff1a 1 2 3 4 5 // Assume ptr is some pointer that may or may not be a null pointer if ( ptr ) // if ptr is not a null pointer std :: cout << * ptr << '\\n' ; // okay to dereference else // do something else that doesn't involve dereferencing ptr (print an error message, do nothing at all, etc...) \u4f46\u662f\uff0c\u60ac\u5782\u6307\u9488\u5982\u4f55\u5224\u65ad\u5462\uff1f\u56e0\u4e3a\u6ca1\u6709\u529e\u6cd5\u68c0\u6d4b\u6307\u9488\u662f\u5426\u662f \u60ac\u5782 \u72b6\u6001\uff0c\u6240\u4ee5\u5fc5\u987b\u4ece\u6e90\u5934\u4e0a\u907f\u514d\u5728\u7a0b\u5e8f\u4e2d\u51fa\u73b0\u4efb\u4f55\u60ac\u6d6e\u6307\u9488\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u4efb\u4f55\u4e0d\u6307\u5411\u6709\u6548\u5bf9\u8c61\u7684\u6307\u9488\u90fd\u88ab\u8bbe\u7f6e\u4e3a nullptr \u3002 \u8fd9\u6837\uff0c\u5728\u5bf9\u6307\u9488\u8fdb\u884c\u89e3\u5f15\u7528\u4e4b\u524d\uff0c\u6211\u4eec\u53ea\u9700\u8981\u6d4b\u8bd5\u5b83\u662f\u5426\u4e3a\u7a7a\u2014\u2014\u5982\u679c\u5b83\u975e\u7a7a\uff0c\u6211\u4eec\u5c31\u5047\u5b9a\u8be5\u6307\u9488\u4e0d\u662f \u60ac\u5782 \u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u6307\u9488\u5e94\u8be5\u4fdd\u5b58\u6709\u6548\u5bf9\u8c61\u7684\u5730\u5740\uff0c\u6216\u8005\u8bbe\u7f6e\u4e3anullptr\u3002\u8fd9\u6837\u6211\u4eec\u53ea\u9700\u8981\u6d4b\u8bd5\u6307\u9488\u662f\u5426\u4e3a\u7a7a\uff0c\u5e76\u4e14\u53ef\u4ee5\u5047\u8bbe\u4efb\u4f55\u975e\u7a7a\u6307\u9488\u90fd\u662f\u6709\u6548\u7684\u3002 \u4e0d\u5e78\u7684\u662f\uff0c\u907f\u514d\u60ac\u5782\u6307\u9488\u5e76\u4e0d\u5bb9\u6613\uff1a\u5f53\u4e00\u4e2a\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\uff0c\u4efb\u4f55\u6307\u5411\u8be5\u5bf9\u8c61\u7684\u6307\u9488\u90fd\u4f1a\u53d8\u6210\u60ac\u5782\u6307\u9488\uff0c\u800c\u4e14\u8fd9\u4e9b\u6307\u9488\u5e76\u4e0d\u4f1a\u4f1a\u81ea\u52a8\u5730\u8bbe\u7f6e\u4e3a\u7a7a\u6307\u9488\uff01\u56e0\u6b64\uff0c\u7a0b\u5e8f\u5458\u6709\u8d23\u4efb\u786e\u4fdd\u6240\u6709\u6307\u5411\u521a\u521a\u88ab\u9500\u6bc1\u7684\u5bf9\u8c61\u7684\u6307\u9488\u90fd\u88ab\u6b63\u786e\u5730\u8bbe\u7f6e\u4e3a nullptr \u3002 \u6ce8\u610f \u5f53\u4e00\u4e2a\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\uff0c\u6307\u5411\u88ab\u9500\u6bc1\u5bf9\u8c61\u7684\u4efb\u4f55\u6307\u9488\u90fd\u5c06\u6210\u4e3a\u60ac\u5782\u6307\u9488(\u5b83\u4eec\u4e0d\u4f1a\u81ea\u52a8\u8bbe\u7f6e\u4e3a nullptr )\u3002\u68c0\u6d4b\u8fd9\u4e9b\u60c5\u51b5\u5e76\u786e\u4fdd\u8fd9\u4e9b\u6307\u9488\u968f\u540e\u88ab\u8bbe\u7f6e\u4e3a nullptr \u662f\u4f60\u7684\u8d23\u4efb\u3002 \u9057\u7559\u7684\u6307\u9488\u5b57\u9762\u91cf 0 \u548c NULL \u5728\u5386\u53f2\u9057\u7559\u4ee3\u7801\u4e2d\uff0c\u4f60\u53ef\u80fd\u4f1a\u770b\u5230\u9664 nullptr \u4ee5\u5916\u7684\u6307\u9488\u5b57\u9762\u91cf\u3002 \u7b2c\u4e00\u4e2a\u5b57\u9762\u91cf\u662f 0 \u3002\u5728\u6307\u9488\u7684\u8bed\u5883\u4e2d\uff0c 0 \u5c31\u88ab\u8ba4\u4e3a\u662f\u7a7a\u503c\uff0c\u800c\u4e14\u4e5f\u662f\u552f\u4e00\u53ef\u4ee5\u88ab\u8d4b\u503c\u7ed9\u6307\u9488\u7684\u6574\u578b\u5b57\u9762\u91cf\u3002 1 2 3 4 5 6 7 8 9 int main () { float * ptr { 0 }; // ptr is now a null pointer (for example only, don't do this) float * ptr2 ; // ptr2 is uninitialized ptr2 = 0 ; // ptr2 is now a null pointer (for example only, don't do this) return 0 ; } \u9898\u5916\u8bdd \u5728\u73b0\u4ee3\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u4e2d\uff0c\u5730\u5740\u201c0\u201d\u901a\u5e38\u7528\u4e8e\u8868\u793a\u7a7a\u6307\u9488\u3002\u4f46\u662f\uff0cC++\u6807\u51c6\u5e76\u6ca1\u6709\u5bf9\u6b64\u505a\u51fa\u4fdd\u8bc1\uff0c\u800c\u4e14\u4e00\u4e9b\u4f53\u7cfb\u7ed3\u6784\u4e5f\u7684\u786e\u4f1a\u4f7f\u7528\u5176\u4ed6\u503c\u3002\u5f53\u5728\u7a7a\u6307\u9488\u7684\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\u201c0\u201d\u5b57\u9762\u503c\u65f6\uff0c\u5b83\u5c06\u88ab\u8f6c\u6362\u4e3a\u5f53\u524d\u4f53\u7cfb\u7ed3\u6784\u7528\u6765\u8868\u793a\u7a7a\u6307\u9488\u7684\u4efb\u4f55\u5730\u5740\u3002 \u6b64\u5916\uff0c\u8fd8\u6709\u4e00\u4e2a\u540d\u4e3a NULL \u7684\u9884\u5904\u7406\u5668\u5b8f(\u5728 \u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49)\u3002\u8fd9\u4e2a\u5b8f\u662f\u4eceC\u8bed\u8a00\u4e2d\u7ee7\u627f\u7684\uff0c\u5728C\u8bed\u8a00\u4e2d\u5b83\u901a\u5e38\u7528\u4e8e\u6307\u793a\u7a7a\u6307\u9488\u3002 1 2 3 4 5 6 7 8 9 #include // for NULL int main () { double * ptr { NULL }; // ptr is a null pointer double * ptr2 ; // ptr2 is uninitialized ptr2 = NULL ; // ptr2 is now a null pointer } \u73b0\u4ee3C++\u4e2d\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u4e0a\u8ff0\u4e24\u79cd\u5b57\u9762\u91cf\uff08\u5e94\u8be5\u4f7f\u7528 nullpt \u6765\u4ee3\u66ff\uff09\uff0c\u6211\u4eec\u4f1a\u5728 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 \u4e2d\u4ecb\u7ecd\u4e3a\u4ec0\u4e48\u8981\u8fd9\u4e48\u505a\u3002 \u5c3d\u53ef\u80fd\u4f7f\u7528\u5f15\u7528\u800c\u4e0d\u662f\u6307\u9488 \u6307\u9488\u548c\u5f15\u7528\u90fd\u4f7f\u6211\u4eec\u80fd\u591f\u95f4\u63a5\u8bbf\u95ee\u5176\u4ed6\u5bf9\u8c61\u3002 \u6307\u9488\u8fd8\u6709\u5176\u4ed6\u529f\u80fd\uff0c\u53ef\u4ee5\u6539\u53d8\u5b83\u4eec\u6240\u6307\u5411\u7684\u5bf9\u8c61\uff0c\u4e5f\u53ef\u4ee5\u6307\u5411null\u3002\u7136\u800c\uff0c\u8fd9\u4e9b\u6307\u9488\u529f\u80fd\u672c\u8eab\u4e5f\u5f88\u5371\u9669\uff1a\u7a7a\u6307\u9488\u6709\u88ab\u89e3\u5f15\u7528\u7684\u98ce\u9669\uff0c\u800c\u6539\u53d8\u6307\u9488\u6240\u6307\u5411\u7684\u5bf9\u8c61\u7684\u80fd\u529b\u4e5f\u4f7f\u5176\u66f4\u5bb9\u6613\u5bfc\u81f4\u60ac\u5782\u6307\u9488\u7684\u51fa\u73b0\uff1a 1 2 3 4 5 6 7 8 9 10 11 int main () { int * ptr { }; { int x { 5 }; ptr = & x ; // \u5c06\u6307\u9488\u6307\u5411\u4e00\u4e2a\u5373\u5c06\u88ab\u9500\u6bc1\u7684\u5bf9\u8c61\uff08\u5982\u679c\u662f\u5f15\u7528\u5219\u4e0d\u53ef\u80fd\u8fd9\u4e48\u505a\uff09 } // ptr \u73b0\u5728\u662f\u60ac\u5782\u6307\u9488 return 0 ; } \u56e0\u4e3a\u5f15\u7528\u4e0d\u80fd\u88ab\u7ed1\u5b9a\u5230null\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u5fc5\u62c5\u5fc3\u7a7a\u5f15\u7528\u3002\u7531\u4e8e\u5f15\u7528\u5728\u521b\u5efa\u65f6\u5fc5\u987b\u7ed1\u5b9a\u5230\u6709\u6548\u7684\u5bf9\u8c61\uff0c\u7136\u540e\u4e0d\u80fd\u91cd\u65b0\u5b9a\u4f4d\uff0c\u56e0\u6b64\u4e5f\u66f4\u4e0d\u5bb9\u6613\u51fa\u73b0\u60ac\u5782\u6307\u9488\u3002 \u56e0\u4e3a\u5f15\u7528\u66f4\u5b89\u5168\uff0c\u6240\u4ee5\u5e94\u8be5\u4f18\u5148\u4f7f\u7528\u5f15\u7528\u800c\u4e0d\u662f\u6307\u9488\uff0c\u9664\u975e\u9700\u8981\u6307\u9488\u63d0\u4f9b\u7684\u989d\u5916\u529f\u80fd\u3002 \u6700\u4f73\u5b9e\u8df5 \u9664\u975e\u9700\u8981\u6307\u9488\u63d0\u4f9b\u7684\u9644\u52a0\u529f\u80fd\uff0c\u5426\u5219\u6700\u597d\u4f7f\u7528\u5f15\u7528\u800c\u4e0d\u662f\u6307\u9488\u3002","title":"9.7 - \u7a7a\u6307\u9488"},{"location":"9-7-Null-pointers/#null","text":"\u9664\u4e86\u5185\u5b58\u5730\u5740\uff0c\u6307\u9488\u8fd8\u53ef\u4ee5\u4fdd\u5b58\u4e00\u4e2a\u989d\u5916\u7684\u503c\uff1a\u7a7a\u503c\u3002\u7a7a\u503c(\u901a\u5e38\u7f29\u5199\u4e3anull)\u662f\u4e00\u79cd\u7279\u6b8a\u503c\uff0c\u8868\u793a\u67d0\u7269\u6ca1\u6709\u503c\u3002\u5f53\u6307\u9488\u6301\u6709\u7a7a\u503c\u65f6\uff0c\u610f\u5473\u7740\u8be5\u6307\u9488\u4e0d\u6307\u5411\u4efb\u4f55\u4e1c\u897f\u3002\u8fd9\u6837\u7684\u6307\u9488\u79f0\u4e3a\u7a7a\u6307\u9488\u3002 \u521b\u5efa\u7a7a\u6307\u9488\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u503c\u521d\u59cb\u5316: 1 2 3 4 5 6 int main () { int * ptr {}; // ptr is now a null pointer, and is not holding an address return 0 ; } \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f60\u4e0d\u80fd\u5c06\u6307\u9488\u521d\u59cb\u5316\u4e3a\u4e00\u4e2a\u6709\u6548\u7684\u5730\u5740\uff0c\u8bf7\u4f7f\u7528\u503c\u521d\u59cb\u5316\u5c06\u5176\u521d\u59cb\u5316\u4e3a\u7a7a\u6307\u9488 \u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8d4b\u503c\u6765\u6539\u53d8\u6307\u9488\u6240\u6307\u5411\u7684\u5bf9\u8c61\uff0c\u6240\u4ee5\u6700\u521d\u88ab\u8bbe\u7f6e\u4e3a\u7a7a\u7684\u6307\u9488\u4ee5\u540e\u53ef\u4ee5\u88ab\u66f4\u6539\u4e3a\u6307\u5411\u4e00\u4e2a\u6709\u6548\u7684\u5bf9\u8c61: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { int * ptr {}; // ptr is a null pointer, and is not holding an address int x { 5 }; ptr = & x ; // ptr now pointing at object x (no longer a null pointer) std :: cout << * ptr << '\\n' ; // print value of x through dereferenced ptr return 0 ; }","title":"\u7a7a\u6307\u9488\uff08Null\uff09"},{"location":"9-7-Null-pointers/#nullptr","text":"\u7c7b\u4f3c\u4e8e true \u548c false \u7528\u4e8e\u8868\u793a\u5e03\u5c14\u5b57\u9762\u91cf\u503c\uff0c nullptr \u5173\u952e\u5b57\u5219\u7528\u4e8e\u8868\u793a\u7a7a\u6307\u9488\u5b57\u9762\u91cf\u3002\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 nullptr \u663e\u5f0f\u5730\u5c06\u6307\u9488\u521d\u59cb\u5316\u6216\u8d4b\u503c\u4e3a\u7a7a\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 int main () { int * ptr { nullptr }; // can use nullptr to initialize a pointer to be a null pointer int value { 5 }; int * ptr2 { & value }; // ptr2 is a valid pointer ptr2 = nullptr ; // Can assign nullptr to make the pointer a null pointer someFunction ( nullptr ); // we can also pass nullptr to a function that has a pointer parameter return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u6307\u9488 ptr2 \u7684\u503c\u8bbe\u7f6e\u4e3a nullptr \uff0c\u8fd9\u4f7f\u5f97 ptr2 \u79f0\u4e3a\u4e00\u4e2a\u7a7a\u6307\u9488\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u9700\u8981\u7a7a\u6307\u9488\u5b57\u9762\u91cf\u7528\u4e8e\u6307\u9488\u7684\u521d\u59cb\u5316\u3001\u8d4b\u503c\u6216\u4f5c\u4e3a\u53c2\u6570\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 nullptr \u3002","title":"nullptr \u5173\u952e\u5b57"},{"location":"9-7-Null-pointers/#_1","text":"\u7c7b\u4f3c\u4e8e\u5bf9\u60ac\u5782\u6307\u9488\u6216\u91ce\u6307\u9488\u89e3\u5f15\u7528\uff0c\u5bf9\u7a7a\u6307\u9488\u89e3\u5f15\u7528\u4e5f\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4f1a\u4f7f\u4f60\u7684\u7a0b\u5e8f\u5d29\u6e83\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u8bf4\u660e\u4e86\u8fd9\u4e00\u70b9\uff0c\u5f53\u4f60\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u5b83\u53ef\u80fd\u4f1a\u5d29\u6e83\u6216\u5f02\u5e38\u7ec8\u6b62(\u6765\u5427\uff0c\u5c1d\u8bd5\u4e00\u4e0b\uff0c\u4e0d\u4f1a\u641e\u574f\u4f60\u7684\u673a\u5668\u7684)\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { int * ptr {}; // Create a null pointer std :: cout << * ptr << '\\n' ; // Dereference the null pointer return 0 ; } \u4ece\u6982\u5ff5\u4e0a\u8bb2\uff0c\u8fd9\u662f\u6709\u9053\u7406\u7684\u3002\u5bf9\u6307\u9488\u7684\u89e3\u5f15\u7528\u610f\u5473\u7740\u201c\u5230\u6307\u9488\u6240\u6307\u5411\u7684\u5730\u5740\u5e76\u8bbf\u95ee\u90a3\u91cc\u7684\u503c\u201d\u3002\u7a7a\u6307\u9488\u6ca1\u6709\u5730\u5740\u3002\u56e0\u6b64\uff0c\u5f53\u4f60\u8bd5\u56fe\u8bbf\u95ee\u8be5\u5730\u5740\u7684\u503c\u65f6\uff0c\u5b83\u5e94\u8be5\u505a\u4ec0\u4e48? \u610f\u5916\u5730\u89e3\u5f15\u7528\u7a7a\u6307\u9488\u548c\u60ac\u7a7a\u6307\u9488\u662fC++\u7a0b\u5e8f\u5458\u6700\u5e38\u72af\u7684\u9519\u8bef\u4e4b\u4e00\uff0c\u5e76\u4e14\u53ef\u80fd\u662fC++\u7a0b\u5e8f\u5728\u5b9e\u8df5\u4e2d\u5d29\u6e83\u7684\u6700\u5e38\u89c1\u539f\u56e0\u3002 \u6ce8\u610f \u65e0\u8bba\u4f55\u65f6\u4f7f\u7528\u6307\u9488\uff0c\u90fd\u9700\u8981\u683c\u5916\u5c0f\u5fc3\uff0c\u786e\u4fdd\u4ee3\u7801\u6ca1\u6709\u89e3\u5f15\u7528\u7a7a\u6307\u9488\u6216\u60ac\u6d6e\u6307\u9488\uff0c\u56e0\u4e3a\u8fd9\u5c06\u5bfc\u81f4\u672a\u5b9a\u4e49\u7684\u884c\u4e3a(\u53ef\u80fd\u5bfc\u81f4\u5e94\u7528\u7a0b\u5e8f\u5d29\u6e83)\u3002","title":"\u5bf9\u7a7a\u6307\u9488\u89e3\u5f15\u7528\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a"},{"location":"9-7-Null-pointers/#_2","text":"\u6b63\u5982\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u6d4b\u8bd5\u5224\u65ad\u5e03\u5c14\u503c\u662f true \u8fd8\u662f false \u90a3\u6837\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5224\u65ad\u4e00\u4e2a\u6307\u9488\u7684\u503c\u662f\u5426\u662f nullptr \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { int x { 5 }; int * ptr { & x }; // pointers convert to Boolean false if they are null, and Boolean true if they are non-null if ( ptr == nullptr ) // explicit test for equivalence std :: cout << \"ptr is null \\n \" ; else std :: cout << \"ptr is non-null \\n \" ; int * nullPtr {}; std :: cout << \"nullPtr is \" << ( nullPtr == nullptr ? \"null \\n \" : \"non-null \\n \" ); // explicit test for equivalence return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u63a5\u8fc7\u4e3a\uff1a 1 2 ptr is non-null nullPtr is null \u5728 4.9 - \u5e03\u5c14\u503c \u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u77e5\u9053==\u6574\u578b\u503c\u4f1a\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff1a\u6574\u578b\u503c0\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c false \uff0c\u800c\u5176\u4ed6\u4efb\u4f55\u6574\u578b\u503c\u90fd\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c true \u3002== \u7c7b\u4f3c\u5730\uff0c\u6307\u9488\u4e5f\u4f1a\u9690\u5f0f\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff1a\u7a7a\u6307\u9488\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c false \uff0c\u975e\u7a7a\u6307\u9488\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c true \u3002\u8fd9\u6837\u4e00\u6765\u6211\u4eec\u53ef\u4ee5\u4e0d\u5fc5\u663e\u5f0f\u5730\u6d4b\u8bd5 nullptr \uff0c\u53ea\u4f7f\u7528\u9690\u5f0f\u8f6c\u6362\u5230\u5e03\u5c14\u503c\u6765\u6d4b\u8bd5\u6307\u9488\u662f\u5426\u4e3a\u7a7a\u6307\u9488\u5373\u53ef\u3002\u4e0b\u9762\u7684\u7a0b\u5e8f\u548c\u4e4b\u524d\u7684\u7a0b\u5e8f\u6548\u679c\u4e00\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { int x { 5 }; int * ptr { & x }; // \u5982\u679c\u6307\u9488\u662f\u7a7a\u6307\u9488\u5219\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u7c7b\u578bfalse\uff0c\u5982\u679c\u4e0d\u662f\u975e\u7a7a\u6307\u9488\u5219\u8f6c\u6362\u4e3atrue if ( ptr ) // implicit conversion to Boolean std :: cout << \"ptr is non-null \\n \" ; else std :: cout << \"ptr is null \\n \" ; int * nullPtr {}; std :: cout << \"nullPtr is \" << ( nullPtr ? \"non-null \\n \" : \"null \\n \" ); // implicit conversion to Boolean return 0 ; } \u6ce8\u610f \u6761\u4ef6\u5224\u65ad\u53ea\u80fd\u7528\u4e8e\u533a\u5206\u7a7a\u6307\u9488\u4e0e\u975e\u7a7a\u6307\u9488\u3002\u6ca1\u6709\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u786e\u5b9a\u975e\u7a7a\u6307\u9488\u662f\u6307\u5411\u6709\u6548\u5bf9\u8c61\u8fd8\u662f\u60ac\u7a7a\u6307\u9488(\u6307\u5411\u65e0\u6548\u5bf9\u8c61)\u3002","title":"\u68c0\u67e5\u7a7a\u6307\u9488"},{"location":"9-7-Null-pointers/#nullptr_1","text":"\u5bf9\u7a7a\u6307\u9488\u6216\u60ac\u7a7a\u6307\u9488\u7684\u89e3\u5f15\u7528\u5c06\u5bfc\u81f4\u672a\u5b9a\u4e49\u7684\u884c\u4e3a\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u4e0d\u5728\u4ee3\u7801\u4e2d\u505a\u8fd9\u4e24\u4ef6\u4e8b\u3002 \u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u907f\u514d\u5bf9\u7a7a\u6307\u9488\u7684\u89e3\u5f15\u7528\uff0c\u5728\u5c1d\u8bd5\u89e3\u5f15\u7528\u4e4b\u524d\u5148\u5bf9\u6307\u9488\u8fdb\u884c\u5224\u7a7a\u5373\u53ef\uff1a 1 2 3 4 5 // Assume ptr is some pointer that may or may not be a null pointer if ( ptr ) // if ptr is not a null pointer std :: cout << * ptr << '\\n' ; // okay to dereference else // do something else that doesn't involve dereferencing ptr (print an error message, do nothing at all, etc...) \u4f46\u662f\uff0c\u60ac\u5782\u6307\u9488\u5982\u4f55\u5224\u65ad\u5462\uff1f\u56e0\u4e3a\u6ca1\u6709\u529e\u6cd5\u68c0\u6d4b\u6307\u9488\u662f\u5426\u662f \u60ac\u5782 \u72b6\u6001\uff0c\u6240\u4ee5\u5fc5\u987b\u4ece\u6e90\u5934\u4e0a\u907f\u514d\u5728\u7a0b\u5e8f\u4e2d\u51fa\u73b0\u4efb\u4f55\u60ac\u6d6e\u6307\u9488\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u4efb\u4f55\u4e0d\u6307\u5411\u6709\u6548\u5bf9\u8c61\u7684\u6307\u9488\u90fd\u88ab\u8bbe\u7f6e\u4e3a nullptr \u3002 \u8fd9\u6837\uff0c\u5728\u5bf9\u6307\u9488\u8fdb\u884c\u89e3\u5f15\u7528\u4e4b\u524d\uff0c\u6211\u4eec\u53ea\u9700\u8981\u6d4b\u8bd5\u5b83\u662f\u5426\u4e3a\u7a7a\u2014\u2014\u5982\u679c\u5b83\u975e\u7a7a\uff0c\u6211\u4eec\u5c31\u5047\u5b9a\u8be5\u6307\u9488\u4e0d\u662f \u60ac\u5782 \u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u6307\u9488\u5e94\u8be5\u4fdd\u5b58\u6709\u6548\u5bf9\u8c61\u7684\u5730\u5740\uff0c\u6216\u8005\u8bbe\u7f6e\u4e3anullptr\u3002\u8fd9\u6837\u6211\u4eec\u53ea\u9700\u8981\u6d4b\u8bd5\u6307\u9488\u662f\u5426\u4e3a\u7a7a\uff0c\u5e76\u4e14\u53ef\u4ee5\u5047\u8bbe\u4efb\u4f55\u975e\u7a7a\u6307\u9488\u90fd\u662f\u6709\u6548\u7684\u3002 \u4e0d\u5e78\u7684\u662f\uff0c\u907f\u514d\u60ac\u5782\u6307\u9488\u5e76\u4e0d\u5bb9\u6613\uff1a\u5f53\u4e00\u4e2a\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\uff0c\u4efb\u4f55\u6307\u5411\u8be5\u5bf9\u8c61\u7684\u6307\u9488\u90fd\u4f1a\u53d8\u6210\u60ac\u5782\u6307\u9488\uff0c\u800c\u4e14\u8fd9\u4e9b\u6307\u9488\u5e76\u4e0d\u4f1a\u4f1a\u81ea\u52a8\u5730\u8bbe\u7f6e\u4e3a\u7a7a\u6307\u9488\uff01\u56e0\u6b64\uff0c\u7a0b\u5e8f\u5458\u6709\u8d23\u4efb\u786e\u4fdd\u6240\u6709\u6307\u5411\u521a\u521a\u88ab\u9500\u6bc1\u7684\u5bf9\u8c61\u7684\u6307\u9488\u90fd\u88ab\u6b63\u786e\u5730\u8bbe\u7f6e\u4e3a nullptr \u3002 \u6ce8\u610f \u5f53\u4e00\u4e2a\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\uff0c\u6307\u5411\u88ab\u9500\u6bc1\u5bf9\u8c61\u7684\u4efb\u4f55\u6307\u9488\u90fd\u5c06\u6210\u4e3a\u60ac\u5782\u6307\u9488(\u5b83\u4eec\u4e0d\u4f1a\u81ea\u52a8\u8bbe\u7f6e\u4e3a nullptr )\u3002\u68c0\u6d4b\u8fd9\u4e9b\u60c5\u51b5\u5e76\u786e\u4fdd\u8fd9\u4e9b\u6307\u9488\u968f\u540e\u88ab\u8bbe\u7f6e\u4e3a nullptr \u662f\u4f60\u7684\u8d23\u4efb\u3002","title":"\u4f7f\u7528 nullptr \u907f\u514d\u60ac\u5782\u6307\u9488"},{"location":"9-7-Null-pointers/#0-null","text":"\u5728\u5386\u53f2\u9057\u7559\u4ee3\u7801\u4e2d\uff0c\u4f60\u53ef\u80fd\u4f1a\u770b\u5230\u9664 nullptr \u4ee5\u5916\u7684\u6307\u9488\u5b57\u9762\u91cf\u3002 \u7b2c\u4e00\u4e2a\u5b57\u9762\u91cf\u662f 0 \u3002\u5728\u6307\u9488\u7684\u8bed\u5883\u4e2d\uff0c 0 \u5c31\u88ab\u8ba4\u4e3a\u662f\u7a7a\u503c\uff0c\u800c\u4e14\u4e5f\u662f\u552f\u4e00\u53ef\u4ee5\u88ab\u8d4b\u503c\u7ed9\u6307\u9488\u7684\u6574\u578b\u5b57\u9762\u91cf\u3002 1 2 3 4 5 6 7 8 9 int main () { float * ptr { 0 }; // ptr is now a null pointer (for example only, don't do this) float * ptr2 ; // ptr2 is uninitialized ptr2 = 0 ; // ptr2 is now a null pointer (for example only, don't do this) return 0 ; } \u9898\u5916\u8bdd \u5728\u73b0\u4ee3\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u4e2d\uff0c\u5730\u5740\u201c0\u201d\u901a\u5e38\u7528\u4e8e\u8868\u793a\u7a7a\u6307\u9488\u3002\u4f46\u662f\uff0cC++\u6807\u51c6\u5e76\u6ca1\u6709\u5bf9\u6b64\u505a\u51fa\u4fdd\u8bc1\uff0c\u800c\u4e14\u4e00\u4e9b\u4f53\u7cfb\u7ed3\u6784\u4e5f\u7684\u786e\u4f1a\u4f7f\u7528\u5176\u4ed6\u503c\u3002\u5f53\u5728\u7a7a\u6307\u9488\u7684\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\u201c0\u201d\u5b57\u9762\u503c\u65f6\uff0c\u5b83\u5c06\u88ab\u8f6c\u6362\u4e3a\u5f53\u524d\u4f53\u7cfb\u7ed3\u6784\u7528\u6765\u8868\u793a\u7a7a\u6307\u9488\u7684\u4efb\u4f55\u5730\u5740\u3002 \u6b64\u5916\uff0c\u8fd8\u6709\u4e00\u4e2a\u540d\u4e3a NULL \u7684\u9884\u5904\u7406\u5668\u5b8f(\u5728 \u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49)\u3002\u8fd9\u4e2a\u5b8f\u662f\u4eceC\u8bed\u8a00\u4e2d\u7ee7\u627f\u7684\uff0c\u5728C\u8bed\u8a00\u4e2d\u5b83\u901a\u5e38\u7528\u4e8e\u6307\u793a\u7a7a\u6307\u9488\u3002 1 2 3 4 5 6 7 8 9 #include // for NULL int main () { double * ptr { NULL }; // ptr is a null pointer double * ptr2 ; // ptr2 is uninitialized ptr2 = NULL ; // ptr2 is now a null pointer } \u73b0\u4ee3C++\u4e2d\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u4e0a\u8ff0\u4e24\u79cd\u5b57\u9762\u91cf\uff08\u5e94\u8be5\u4f7f\u7528 nullpt \u6765\u4ee3\u66ff\uff09\uff0c\u6211\u4eec\u4f1a\u5728 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 \u4e2d\u4ecb\u7ecd\u4e3a\u4ec0\u4e48\u8981\u8fd9\u4e48\u505a\u3002","title":"\u9057\u7559\u7684\u6307\u9488\u5b57\u9762\u91cf 0 \u548c NULL"},{"location":"9-7-Null-pointers/#_3","text":"\u6307\u9488\u548c\u5f15\u7528\u90fd\u4f7f\u6211\u4eec\u80fd\u591f\u95f4\u63a5\u8bbf\u95ee\u5176\u4ed6\u5bf9\u8c61\u3002 \u6307\u9488\u8fd8\u6709\u5176\u4ed6\u529f\u80fd\uff0c\u53ef\u4ee5\u6539\u53d8\u5b83\u4eec\u6240\u6307\u5411\u7684\u5bf9\u8c61\uff0c\u4e5f\u53ef\u4ee5\u6307\u5411null\u3002\u7136\u800c\uff0c\u8fd9\u4e9b\u6307\u9488\u529f\u80fd\u672c\u8eab\u4e5f\u5f88\u5371\u9669\uff1a\u7a7a\u6307\u9488\u6709\u88ab\u89e3\u5f15\u7528\u7684\u98ce\u9669\uff0c\u800c\u6539\u53d8\u6307\u9488\u6240\u6307\u5411\u7684\u5bf9\u8c61\u7684\u80fd\u529b\u4e5f\u4f7f\u5176\u66f4\u5bb9\u6613\u5bfc\u81f4\u60ac\u5782\u6307\u9488\u7684\u51fa\u73b0\uff1a 1 2 3 4 5 6 7 8 9 10 11 int main () { int * ptr { }; { int x { 5 }; ptr = & x ; // \u5c06\u6307\u9488\u6307\u5411\u4e00\u4e2a\u5373\u5c06\u88ab\u9500\u6bc1\u7684\u5bf9\u8c61\uff08\u5982\u679c\u662f\u5f15\u7528\u5219\u4e0d\u53ef\u80fd\u8fd9\u4e48\u505a\uff09 } // ptr \u73b0\u5728\u662f\u60ac\u5782\u6307\u9488 return 0 ; } \u56e0\u4e3a\u5f15\u7528\u4e0d\u80fd\u88ab\u7ed1\u5b9a\u5230null\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u5fc5\u62c5\u5fc3\u7a7a\u5f15\u7528\u3002\u7531\u4e8e\u5f15\u7528\u5728\u521b\u5efa\u65f6\u5fc5\u987b\u7ed1\u5b9a\u5230\u6709\u6548\u7684\u5bf9\u8c61\uff0c\u7136\u540e\u4e0d\u80fd\u91cd\u65b0\u5b9a\u4f4d\uff0c\u56e0\u6b64\u4e5f\u66f4\u4e0d\u5bb9\u6613\u51fa\u73b0\u60ac\u5782\u6307\u9488\u3002 \u56e0\u4e3a\u5f15\u7528\u66f4\u5b89\u5168\uff0c\u6240\u4ee5\u5e94\u8be5\u4f18\u5148\u4f7f\u7528\u5f15\u7528\u800c\u4e0d\u662f\u6307\u9488\uff0c\u9664\u975e\u9700\u8981\u6307\u9488\u63d0\u4f9b\u7684\u989d\u5916\u529f\u80fd\u3002 \u6700\u4f73\u5b9e\u8df5 \u9664\u975e\u9700\u8981\u6307\u9488\u63d0\u4f9b\u7684\u9644\u52a0\u529f\u80fd\uff0c\u5426\u5219\u6700\u597d\u4f7f\u7528\u5f15\u7528\u800c\u4e0d\u662f\u6307\u9488\u3002","title":"\u5c3d\u53ef\u80fd\u4f7f\u7528\u5f15\u7528\u800c\u4e0d\u662f\u6307\u9488"},{"location":"9-8-Pointers-and-const/","tags":["pointer","const"],"text":"Key Takeaway \u7c7b\u578b\u524dconst\u8868\u793a\u7c7b\u578b\u662fconst\uff0c\u6307\u9488\u524d\uff08\u661f\u53f7\u540e\uff09const\u8868\u793a\u6307\u9488\u662fconst \u5c31\u50cf\u5bf9const\u53d8\u91cf\u7684\u5f15\u7528\u4e00\u6837\uff0c\u6307\u5411const\u7684\u6307\u9488\u4e5f\u53ef\u4ee5\u6307\u5411\u975econst\u53d8\u91cf\u3002\u6307\u5411const\u7684\u6307\u9488\u5c06\u88ab\u6307\u5411\u7684\u503c\u89c6\u4e3a\u5e38\u91cf\uff0c\u800c\u4e0d\u7ba1\u8be5\u5730\u5740\u7684\u5bf9\u8c61\u6700\u521d\u662f\u5426\u5b9a\u4e49\u4e3aconst \u603b\u800c\u8a00\u4e4b\uff0c\u4f60\u53ea\u9700\u8981\u8bb0\u4f4f4\u6761\u89c4\u5219\uff0c\u800c\u4e14\u5b83\u4eec\u975e\u5e38\u7b26\u5408\u903b\u8f91: \u975econst\u6307\u9488\u53ef\u4ee5\u88ab\u8d4b\u4e88\u53e6\u4e00\u4e2a\u5730\u5740\u6765\u6539\u53d8\u5b83\u6240\u6307\u5411\u7684\u5bf9\u8c61\uff1b const\u6307\u9488\u603b\u662f\u6307\u5411\u76f8\u540c\u7684\u5730\u5740\uff0c\u4e14\u8be5\u5730\u5740\u4e0d\u80fd\u66f4\u6539\uff1b \u6307\u5411\u975econst\u503c\u7684\u6307\u9488\u53ef\u4ee5\u6539\u53d8\u5b83\u6240\u6307\u5411\u7684\u503c\uff0c\u4f46\u8be5\u6307\u9488\u4e0d\u80fd\u6307\u5411const\u7c7b\u578b\u53d8\u91cf\uff1b \u6307\u5411const\u503c\u7684\u6307\u9488\u5728\u901a\u8fc7\u8be5\u6307\u9488\u8bbf\u95ee\u65f6\u5c06\u8be5\u503c\u89c6\u4e3aconst\uff0c\u56e0\u6b64\u4e0d\u80fd\u66f4\u6539\u5b83\u6240\u6307\u5411\u7684\u503c\u3002\u5b83\u4eec\u53ef\u4ee5\u6307\u5411const\u6216\u975econst\u7684 \u5de6\u503c (\u4f46\u4e0d\u80fd\u6307\u5411\u6ca1\u6709\u5730\u5740\u7684 \u53f3\u503c ) \u8bf7\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int main () { int x { 5 }; int * ptr { & x }; // ptr is a normal (non-const) pointer int y { 6 }; ptr = & y ; // we can point at another value * ptr = 7 ; // we can change the value at the address being held return 0 ; } \u4f7f\u7528\u666e\u901a(\u975e const )\u6307\u9488\uff0c\u65e2\u53ef\u4ee5\u66f4\u6539\u6307\u9488\u6240\u6307\u5411\u7684\u5bf9\u8c61(\u901a\u8fc7\u4e3a\u6307\u9488\u5206\u914d\u4e00\u4e2a\u65b0\u5730\u5740\u6765\u4fdd\u5b58)\uff0c\u4e5f\u53ef\u4ee5\u66f4\u6539\u88ab\u4fdd\u5b58\u5730\u5740\u5904\u7684\u503c(\u901a\u8fc7\u4e3a\u89e3\u5f15\u7528\u6307\u9488\u5206\u914d\u4e00\u4e2a\u65b0\u503c)\u3002 \u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u8981\u6307\u5411\u7684\u503c\u662f const \uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462? 1 2 3 4 5 6 7 int main () { const int x { 5 }; // x \u662f const int * ptr { & x }; // \u7f16\u8bd1\u9519\u8bef\uff1a\u4e0d\u80fd\u628aconst int* \u8f6c\u6362\u4e3a int* return 0 ; } \u4e0a\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u65e0\u6cd5\u7f16\u8bd1\u2014\u2014 \u666e\u901a\u6307\u9488\u4e0d\u80fd\u6307\u5411const\u53d8\u91cf \u3002\u8fd9\u6837\u7684\u9650\u5236\u662f\u6709\u5fc5\u8981\u7684\uff0c\u56e0\u4e3aconst\u53d8\u91cf\u7684\u503c\u662f\u4e0d\u80fd\u66f4\u6539\u7684\u3002\u5141\u8bb8\u7a0b\u5e8f\u5458\u5c06\u975econst\u6307\u9488\u8bbe\u7f6e\u4e3aconst\u503c\u5c06\u5141\u8bb8\u7a0b\u5e8f\u5458\u89e3\u5f15\u7528\u8be5\u6307\u9488\u5e76\u66f4\u6539\u8be5\u503c\u3002\u8fd9\u5c06\u8fdd\u53cd\u53d8\u91cf\u7684\u5e38\u91cf\u6027\u3002 \u6307\u5411\u5e38\u91cf\u7684\u6307\u9488 \u6307\u9488\u6307\u5411\u4e00\u4e2aconst\u7684\u503c\uff08\u4e00\u822c\u7b80\u79f0\u4e3a \u6307\u5411\u5e38\u91cf\u7684\u6307\u9488 \uff09\uff0c\u4e5f\u5c31\u662f\u8bf4\u4e00\u4e2a\u975e\u5e38\u91cf\u7684\u6307\u9488\u6307\u5411\u4e86\u4e00\u4e2a\u5e38\u91cf\u503c\u3002 \u58f0\u660e\u4e00\u4e2a\u53ef\u4ee5\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488\uff0c\u9700\u8981\u5728\u6307\u9488\u7684\u6570\u636e\u7c7b\u578b\u524d\u6dfb\u52a0\u5173\u952e\u5b57 const \uff1a 1 2 3 4 5 6 7 8 9 int main () { const int x { 5 }; const int * ptr { & x }; // okay: ptr is pointing to a \"const int\" * ptr = 6 ; // not allowed: we can't change a const value return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d ptr \u6307\u5411\u4e86\u4e00\u4e2a const int \u3002\u56e0\u4e3a\u6307\u9488\u6240\u6307\u7684\u6570\u636e\u7c7b\u578b\u662fconst\uff0c\u6240\u4ee5\u4e0d\u80fd\u901a\u8fc7\u6307\u9488\u4fee\u6539\u5b83\u6307\u7684\u8fd9\u4e2a\u503c\u3002 \u4f46\u662f\uff0c\u56e0\u4e3a\u6307\u9488\u672c\u8eab\u4e0d\u662fconst\u7684\uff08\u53ea\u662f\u6307\u5411\u4e00\u4e2aconst\u7c7b\u578b\u7684\u503c\u53d8\u91cf\uff09\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4fee\u6539\u6307\u9488\u672c\u8eab\u6240\u6301\u6709\u7684\u5730\u5740\u4f7f\u5176\u6307\u5411\u5176\u4ed6\u53d8\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { const int x { 5 }; const int * ptr { & x }; // ptr \u6307\u5411 const int x const int y { 6 }; ptr = & y ; // okay: \u91cd\u65b0\u6307\u5411 const int y return 0 ; } \u5c31\u50cf\u5bf9const\u53d8\u91cf\u7684\u5f15\u7528\u4e00\u6837\uff0c\u6307\u5411const\u7684\u6307\u9488\u4e5f\u53ef\u4ee5\u6307\u5411\u975econst\u53d8\u91cf\u3002\u6307\u5411const\u7684\u6307\u9488\u5c06\u88ab\u6307\u5411\u7684\u503c\u89c6\u4e3a\u5e38\u91cf\uff0c\u800c\u4e0d\u7ba1\u8be5\u5730\u5740\u7684\u5bf9\u8c61\u6700\u521d\u662f\u5426\u5b9a\u4e49\u4e3aconst: 1 2 3 4 5 6 7 8 9 10 int main () { int x { 5 }; // non-const const int * ptr { & x }; // ptr points to a \"const int\" * ptr = 6 ; // not allowed: ptr points to a \"const int\" so we can't change the value through ptr x = 6 ; // allowed: the value is still non-const when accessed through non-const identifier x return 0 ; } \u5e38\u91cf\u6307\u9488 \u6307\u9488\u81ea\u8eab\u4e5f\u53ef\u4ee5\u662f\u5e38\u91cf\u3002\u4e00\u4e2a\u5e38\u91cf\u6307\u9488\u7684\u5730\u5740\uff0c\u5728\u521d\u59cb\u5316\u4e4b\u540e\u662f\u4e0d\u80fd\u591f\u88ab\u4fee\u6539\u7684\u3002 \u58f0\u660e\u4e00\u4e2a\u5e38\u91cf\u6307\u9488\uff0c\u53ea\u9700\u8981\u5728\u6307\u9488\u58f0\u660e\u7684\u661f\u53f7\u540e\u6dfb\u52a0 const \u5173\u952e\u5b57\uff1a 1 2 3 4 5 6 7 int main () { int x { 5 }; int * const ptr { & x }; // const after the asterisk means this is a const pointer return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c ptr \u662f\u4e00\u4e2a\u6307\u5411(\u975e const ) int \u503c\u7684 const \u6307\u9488\u3002 \u5c31\u50cf\u666e\u901a\u7684 const \u53d8\u91cf\u4e00\u6837\uff0c const \u6307\u9488\u5fc5\u987b\u5728\u5b9a\u4e49\u65f6\u521d\u59cb\u5316\uff0c\u5e76\u4e14\u8fd9\u4e2a\u503c\u4e0d\u80fd\u901a\u8fc7\u8d4b\u503c\u6765\u6539\u53d8: 1 2 3 4 5 6 7 8 9 10 int main () { int x { 5 }; int y { 6 }; int * const ptr { & x }; // okay: the const pointer is initialized to the address of x ptr = & y ; // error: once initialized, a const pointer can not be changed. return 0 ; } \u4e0d\u8fc7\uff0c\u8be5\u6307\u9488\u6240\u6307\u7684\u5bf9\u8c61\u5e76\u4e0d\u662f\u4e00\u4e2a\u5e38\u91cf\uff0c\u6240\u4ee5\u4ecd\u65e7\u53ef\u4ee5\u901a\u8fc7\u5bf9\u6307\u9488\u89e3\u5f15\u7528\u7136\u540e\u4fee\u6539\u8be5\u53d8\u91cf\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 9 int main () { int x { 5 }; int * const ptr { & x }; // ptr will always point to x * ptr = 6 ; // okay: the value being pointed to is non-const return 0 ; } \u6307\u5411\u5e38\u91cf\u7684\u5e38\u91cf\u6307\u9488 \u6700\u540e\u4e00\u79cd\u7ec4\u5408\uff0c\u5373\u6307\u9488\u53ef\u4ee5\u88ab\u58f0\u660e\u4e3a\u5e38\u91cf\u6307\u9488\u540c\u65f6\u5b83\u6240\u6307\u7684\u503c\u4e5f\u662f\u5e38\u91cf\uff0c\u5728\u7c7b\u578b\u524d\u548c\u661f\u53f7\u540e\u5206\u522b\u6dfb\u52a0const\u5173\u952e\u5b57\u5373\u53ef\uff1a 1 2 3 4 5 6 7 int main () { int value { 5 }; const int * const ptr { & value }; // a const pointer to a const value return 0 ; } \u6307\u5411const\u503c\u7684 \u5e38\u91cf\u6307\u9488 \u4e0d\u80fd\u6539\u53d8\u5176\u5730\u5740\uff0c\u4e5f\u4e0d\u80fd\u901a\u8fc7\u8be5\u6307\u9488\u6539\u53d8\u5b83\u6240\u6307\u5411\u7684\u503c\u3002\u5b83\u53ea\u80fd\u901a\u8fc7\u89e3\u5f15\u7528\u4ee5\u83b7\u5f97\u5b83\u6240\u6307\u5411\u7684\u503c\u3002 \u590d\u4e60 \u603b\u800c\u8a00\u4e4b\uff0c\u4f60\u53ea\u9700\u8981\u8bb0\u4f4f4\u6761\u89c4\u5219\uff0c\u800c\u4e14\u5b83\u4eec\u975e\u5e38\u7b26\u5408\u903b\u8f91: \u975econst\u6307\u9488\u53ef\u4ee5\u88ab\u8d4b\u4e88\u53e6\u4e00\u4e2a\u5730\u5740\u6765\u6539\u53d8\u5b83\u6240\u6307\u5411\u7684\u5bf9\u8c61\uff1b const\u6307\u9488\u603b\u662f\u6307\u5411\u76f8\u540c\u7684\u5730\u5740\uff0c\u4e14\u8be5\u5730\u5740\u4e0d\u80fd\u66f4\u6539\uff1b \u6307\u5411\u975econst\u503c\u7684\u6307\u9488\u53ef\u4ee5\u6539\u53d8\u5b83\u6240\u6307\u5411\u7684\u503c\uff0c\u4f46\u8be5\u6307\u9488\u4e0d\u80fd\u6307\u5411const\u7c7b\u578b\u53d8\u91cf\uff1b \u6307\u5411const\u503c\u7684\u6307\u9488\u5728\u901a\u8fc7\u8be5\u6307\u9488\u8bbf\u95ee\u65f6\u5c06\u8be5\u503c\u89c6\u4e3aconst\uff0c\u56e0\u6b64\u4e0d\u80fd\u66f4\u6539\u5b83\u6240\u6307\u5411\u7684\u503c\u3002\u5b83\u4eec\u53ef\u4ee5\u6307\u5411const\u6216\u975econst\u7684 \u5de6\u503c (\u4f46\u4e0d\u80fd\u6307\u5411\u6ca1\u6709\u5730\u5740\u7684 \u53f3\u503c ) \u5728\u914d\u5408const\u5173\u952e\u5b57\u4f7f\u7528\u6307\u9488\u65f6\uff0c\u58f0\u660e\u7684\u8bed\u6cd5\u8fd8\u662f\u6709\u70b9\u590d\u6742\u7684\uff1a \u6307\u9488\u7684\u7c7b\u578b\u51b3\u5b9a\u4e86\u5b83\u80fd\u6307\u5411\u5bf9\u8c61\u7684\u7c7b\u578b\u3002\u6240\u4ee5const\u5bf9\u6307\u9488\u7c7b\u578b\u7684\u4fee\u9970\u51b3\u5b9a\u4e86\u6307\u9488\u6240\u6307\u53d8\u91cf\u662f\u5426\u4e3aconst\uff1b \u661f\u53f7\u540e\u9762\u7684 const \u5173\u952e\u5b57\u8868\u793a\u6307\u9488\u672c\u8eab\u662f\u5e38\u91cf\uff0c\u4e0d\u80fd\u88ab\u8d4b\u4e88\u65b0\u7684\u5730\u5740\u3002 1 2 3 4 5 6 7 8 9 10 11 int main () { int value { 5 }; int * ptr0 { & value }; // ptr0 \u6307\u5411 \"int\" \uff0c\u6307\u9488\u672c\u8eab\u4e0d\u662f\u5e38\u91cf\uff0c\u662f\u4e00\u4e2a\u666e\u901a\u6307\u9488 const int * ptr1 { & value }; // ptr1 \u6307\u5411 \"const int\", \u6307\u9488\u672c\u8eab\u4e0d\u662f\u5e38\u91cf, \u662f\u4e00\u4e2a\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488 int * const ptr2 { & value }; // ptr2 \u6307\u5411 \"int\",\u6307\u9488\u672c\u8eab\u662f\u5e38\u91cf, \u662f\u4e00\u4e2a\u6307\u5411\u975e\u5e38\u91cf\u7684\u5e38\u91cf\u6307\u9488 const int * const ptr3 { & value }; // ptr3 \u6307\u5411 \"const int\", \u6307\u9488\u672c\u8eab\u662f\u5e38\u91cf, \u662f\u4e00\u4e2a\u6307\u5411\u5e38\u91cf\u7684\u5e38\u91cf\u6307\u9488 return 0 ; }","title":"9.8 - \u6307\u9488\u548cconst"},{"location":"9-8-Pointers-and-const/#_1","text":"\u6307\u9488\u6307\u5411\u4e00\u4e2aconst\u7684\u503c\uff08\u4e00\u822c\u7b80\u79f0\u4e3a \u6307\u5411\u5e38\u91cf\u7684\u6307\u9488 \uff09\uff0c\u4e5f\u5c31\u662f\u8bf4\u4e00\u4e2a\u975e\u5e38\u91cf\u7684\u6307\u9488\u6307\u5411\u4e86\u4e00\u4e2a\u5e38\u91cf\u503c\u3002 \u58f0\u660e\u4e00\u4e2a\u53ef\u4ee5\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488\uff0c\u9700\u8981\u5728\u6307\u9488\u7684\u6570\u636e\u7c7b\u578b\u524d\u6dfb\u52a0\u5173\u952e\u5b57 const \uff1a 1 2 3 4 5 6 7 8 9 int main () { const int x { 5 }; const int * ptr { & x }; // okay: ptr is pointing to a \"const int\" * ptr = 6 ; // not allowed: we can't change a const value return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d ptr \u6307\u5411\u4e86\u4e00\u4e2a const int \u3002\u56e0\u4e3a\u6307\u9488\u6240\u6307\u7684\u6570\u636e\u7c7b\u578b\u662fconst\uff0c\u6240\u4ee5\u4e0d\u80fd\u901a\u8fc7\u6307\u9488\u4fee\u6539\u5b83\u6307\u7684\u8fd9\u4e2a\u503c\u3002 \u4f46\u662f\uff0c\u56e0\u4e3a\u6307\u9488\u672c\u8eab\u4e0d\u662fconst\u7684\uff08\u53ea\u662f\u6307\u5411\u4e00\u4e2aconst\u7c7b\u578b\u7684\u503c\u53d8\u91cf\uff09\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4fee\u6539\u6307\u9488\u672c\u8eab\u6240\u6301\u6709\u7684\u5730\u5740\u4f7f\u5176\u6307\u5411\u5176\u4ed6\u53d8\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { const int x { 5 }; const int * ptr { & x }; // ptr \u6307\u5411 const int x const int y { 6 }; ptr = & y ; // okay: \u91cd\u65b0\u6307\u5411 const int y return 0 ; } \u5c31\u50cf\u5bf9const\u53d8\u91cf\u7684\u5f15\u7528\u4e00\u6837\uff0c\u6307\u5411const\u7684\u6307\u9488\u4e5f\u53ef\u4ee5\u6307\u5411\u975econst\u53d8\u91cf\u3002\u6307\u5411const\u7684\u6307\u9488\u5c06\u88ab\u6307\u5411\u7684\u503c\u89c6\u4e3a\u5e38\u91cf\uff0c\u800c\u4e0d\u7ba1\u8be5\u5730\u5740\u7684\u5bf9\u8c61\u6700\u521d\u662f\u5426\u5b9a\u4e49\u4e3aconst: 1 2 3 4 5 6 7 8 9 10 int main () { int x { 5 }; // non-const const int * ptr { & x }; // ptr points to a \"const int\" * ptr = 6 ; // not allowed: ptr points to a \"const int\" so we can't change the value through ptr x = 6 ; // allowed: the value is still non-const when accessed through non-const identifier x return 0 ; }","title":"\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488"},{"location":"9-8-Pointers-and-const/#_2","text":"\u6307\u9488\u81ea\u8eab\u4e5f\u53ef\u4ee5\u662f\u5e38\u91cf\u3002\u4e00\u4e2a\u5e38\u91cf\u6307\u9488\u7684\u5730\u5740\uff0c\u5728\u521d\u59cb\u5316\u4e4b\u540e\u662f\u4e0d\u80fd\u591f\u88ab\u4fee\u6539\u7684\u3002 \u58f0\u660e\u4e00\u4e2a\u5e38\u91cf\u6307\u9488\uff0c\u53ea\u9700\u8981\u5728\u6307\u9488\u58f0\u660e\u7684\u661f\u53f7\u540e\u6dfb\u52a0 const \u5173\u952e\u5b57\uff1a 1 2 3 4 5 6 7 int main () { int x { 5 }; int * const ptr { & x }; // const after the asterisk means this is a const pointer return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c ptr \u662f\u4e00\u4e2a\u6307\u5411(\u975e const ) int \u503c\u7684 const \u6307\u9488\u3002 \u5c31\u50cf\u666e\u901a\u7684 const \u53d8\u91cf\u4e00\u6837\uff0c const \u6307\u9488\u5fc5\u987b\u5728\u5b9a\u4e49\u65f6\u521d\u59cb\u5316\uff0c\u5e76\u4e14\u8fd9\u4e2a\u503c\u4e0d\u80fd\u901a\u8fc7\u8d4b\u503c\u6765\u6539\u53d8: 1 2 3 4 5 6 7 8 9 10 int main () { int x { 5 }; int y { 6 }; int * const ptr { & x }; // okay: the const pointer is initialized to the address of x ptr = & y ; // error: once initialized, a const pointer can not be changed. return 0 ; } \u4e0d\u8fc7\uff0c\u8be5\u6307\u9488\u6240\u6307\u7684\u5bf9\u8c61\u5e76\u4e0d\u662f\u4e00\u4e2a\u5e38\u91cf\uff0c\u6240\u4ee5\u4ecd\u65e7\u53ef\u4ee5\u901a\u8fc7\u5bf9\u6307\u9488\u89e3\u5f15\u7528\u7136\u540e\u4fee\u6539\u8be5\u53d8\u91cf\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 9 int main () { int x { 5 }; int * const ptr { & x }; // ptr will always point to x * ptr = 6 ; // okay: the value being pointed to is non-const return 0 ; }","title":"\u5e38\u91cf\u6307\u9488"},{"location":"9-8-Pointers-and-const/#_3","text":"\u6700\u540e\u4e00\u79cd\u7ec4\u5408\uff0c\u5373\u6307\u9488\u53ef\u4ee5\u88ab\u58f0\u660e\u4e3a\u5e38\u91cf\u6307\u9488\u540c\u65f6\u5b83\u6240\u6307\u7684\u503c\u4e5f\u662f\u5e38\u91cf\uff0c\u5728\u7c7b\u578b\u524d\u548c\u661f\u53f7\u540e\u5206\u522b\u6dfb\u52a0const\u5173\u952e\u5b57\u5373\u53ef\uff1a 1 2 3 4 5 6 7 int main () { int value { 5 }; const int * const ptr { & value }; // a const pointer to a const value return 0 ; } \u6307\u5411const\u503c\u7684 \u5e38\u91cf\u6307\u9488 \u4e0d\u80fd\u6539\u53d8\u5176\u5730\u5740\uff0c\u4e5f\u4e0d\u80fd\u901a\u8fc7\u8be5\u6307\u9488\u6539\u53d8\u5b83\u6240\u6307\u5411\u7684\u503c\u3002\u5b83\u53ea\u80fd\u901a\u8fc7\u89e3\u5f15\u7528\u4ee5\u83b7\u5f97\u5b83\u6240\u6307\u5411\u7684\u503c\u3002","title":"\u6307\u5411\u5e38\u91cf\u7684\u5e38\u91cf\u6307\u9488"},{"location":"9-8-Pointers-and-const/#_4","text":"\u603b\u800c\u8a00\u4e4b\uff0c\u4f60\u53ea\u9700\u8981\u8bb0\u4f4f4\u6761\u89c4\u5219\uff0c\u800c\u4e14\u5b83\u4eec\u975e\u5e38\u7b26\u5408\u903b\u8f91: \u975econst\u6307\u9488\u53ef\u4ee5\u88ab\u8d4b\u4e88\u53e6\u4e00\u4e2a\u5730\u5740\u6765\u6539\u53d8\u5b83\u6240\u6307\u5411\u7684\u5bf9\u8c61\uff1b const\u6307\u9488\u603b\u662f\u6307\u5411\u76f8\u540c\u7684\u5730\u5740\uff0c\u4e14\u8be5\u5730\u5740\u4e0d\u80fd\u66f4\u6539\uff1b \u6307\u5411\u975econst\u503c\u7684\u6307\u9488\u53ef\u4ee5\u6539\u53d8\u5b83\u6240\u6307\u5411\u7684\u503c\uff0c\u4f46\u8be5\u6307\u9488\u4e0d\u80fd\u6307\u5411const\u7c7b\u578b\u53d8\u91cf\uff1b \u6307\u5411const\u503c\u7684\u6307\u9488\u5728\u901a\u8fc7\u8be5\u6307\u9488\u8bbf\u95ee\u65f6\u5c06\u8be5\u503c\u89c6\u4e3aconst\uff0c\u56e0\u6b64\u4e0d\u80fd\u66f4\u6539\u5b83\u6240\u6307\u5411\u7684\u503c\u3002\u5b83\u4eec\u53ef\u4ee5\u6307\u5411const\u6216\u975econst\u7684 \u5de6\u503c (\u4f46\u4e0d\u80fd\u6307\u5411\u6ca1\u6709\u5730\u5740\u7684 \u53f3\u503c ) \u5728\u914d\u5408const\u5173\u952e\u5b57\u4f7f\u7528\u6307\u9488\u65f6\uff0c\u58f0\u660e\u7684\u8bed\u6cd5\u8fd8\u662f\u6709\u70b9\u590d\u6742\u7684\uff1a \u6307\u9488\u7684\u7c7b\u578b\u51b3\u5b9a\u4e86\u5b83\u80fd\u6307\u5411\u5bf9\u8c61\u7684\u7c7b\u578b\u3002\u6240\u4ee5const\u5bf9\u6307\u9488\u7c7b\u578b\u7684\u4fee\u9970\u51b3\u5b9a\u4e86\u6307\u9488\u6240\u6307\u53d8\u91cf\u662f\u5426\u4e3aconst\uff1b \u661f\u53f7\u540e\u9762\u7684 const \u5173\u952e\u5b57\u8868\u793a\u6307\u9488\u672c\u8eab\u662f\u5e38\u91cf\uff0c\u4e0d\u80fd\u88ab\u8d4b\u4e88\u65b0\u7684\u5730\u5740\u3002 1 2 3 4 5 6 7 8 9 10 11 int main () { int value { 5 }; int * ptr0 { & value }; // ptr0 \u6307\u5411 \"int\" \uff0c\u6307\u9488\u672c\u8eab\u4e0d\u662f\u5e38\u91cf\uff0c\u662f\u4e00\u4e2a\u666e\u901a\u6307\u9488 const int * ptr1 { & value }; // ptr1 \u6307\u5411 \"const int\", \u6307\u9488\u672c\u8eab\u4e0d\u662f\u5e38\u91cf, \u662f\u4e00\u4e2a\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488 int * const ptr2 { & value }; // ptr2 \u6307\u5411 \"int\",\u6307\u9488\u672c\u8eab\u662f\u5e38\u91cf, \u662f\u4e00\u4e2a\u6307\u5411\u975e\u5e38\u91cf\u7684\u5e38\u91cf\u6307\u9488 const int * const ptr3 { & value }; // ptr3 \u6307\u5411 \"const int\", \u6307\u9488\u672c\u8eab\u662f\u5e38\u91cf, \u662f\u4e00\u4e2a\u6307\u5411\u5e38\u91cf\u7684\u5e38\u91cf\u6307\u9488 return 0 ; }","title":"\u590d\u4e60"},{"location":"9-9-Pass-by-address/","tags":["pointer","address"],"text":"\u5728\u524d\u9762\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u51fd\u6570\u7684\u4e24\u79cd\u4f20\u53c2\u65b9\u5f0f\uff1a \u6309\u503c\u4f20\u9012 ( 2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2 ) \u548c \u6309\u5f15\u7528\u4f20\u9012 ( 9.5 -- Pass by lvalue reference )\u3002 \u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u57fa\u4e8e std::string \u5bf9\u8c61\u5206\u522b\u6f14\u793a\u4e86\u4e0a\u9762\u4e24\u79cd\u4f20\u53c2\u65b9\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include void printByValue ( std :: string val ) // \u5f62\u53c2\u662f\u5b9e\u53c2\u7684\u62f7\u8d1d { std :: cout << val << '\\n' ; // \u6253\u5370\u62f7\u8d1d\u7684\u503c } void printByReference ( const std :: string & ref ) // \u5f62\u53c2\u7ed1\u5b9a\u5230str { std :: cout << ref << '\\n' ; // \u901a\u8fc7\u5f15\u7528\u76f4\u63a5\u6253\u5370str } int main () { std :: string str { \"Hello, world!\" }; printByValue ( str ); // \u4f20\u9012str\u7684\u503c\uff0c\u4f1a\u521b\u5efa\u62f7\u8d1d printByReference ( str ); // \u6309\u5f15\u7528\u4f20\u9012 str \uff0c\u4e0d\u4f1a\u521b\u5efa\u62f7\u8d1d return 0 ; } When we pass argument str by value, the function parameter val receives a copy of the argument. Because the parameter is a copy of the argument, any changes to the val are made to the copy, not the original argument. When we pass argument str by reference, the reference parameter ref is bound to the actual argument. This avoids making a copy of the argument. Because our reference parameter is const, we are not allowed to change ref . But if ref were non-const, any changes we made to ref would change str . In both cases, the caller is providing the actual object ( str ) to be passed as an argument to the function call. Pass by address C++ provides a third way to pass values to a function, called pass by address. With pass by address, instead of providing an object as an argument, the caller provides an object\u2019s address (via a pointer). This pointer (holding the address of the object) is copied into a pointer parameter of the called function (which now also holds the address of the object). The function can then dereference that pointer to access the object whose address was passed. Here\u2019s a version of the above program that adds a pass by address variant: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include #include void printByValue ( std :: string val ) // The function parameter is a copy of str { std :: cout << val << '\\n' ; // print the value via the copy } void printByReference ( const std :: string & ref ) // The function parameter is a reference that binds to str { std :: cout << ref << '\\n' ; // print the value via the reference } void printByAddress ( const std :: string * ptr ) // The function parameter is a pointer that holds the address of str { std :: cout << * ptr << '\\n' ; // print the value via the dereferenced pointer } int main () { std :: string str { \"Hello, world!\" }; printByValue ( str ); // pass str by value, makes a copy of str printByReference ( str ); // pass str by reference, does not make a copy of str printByAddress ( & str ); // pass str by address, does not make a copy of str return 0 ; } COPY Note how similar all three of these versions are. Let\u2019s explore the pass by address version in more detail. First, because we want our printByAddress() function to use pass by address, we\u2019ve made our function parameter a pointer named ptr . Since printByAddress() will use ptr in a read-only manner, ptr is a pointer to a const value. 1 2 3 4 void printByAddress ( const std :: string * ptr ) { std :: cout << * ptr << '\\n' ; // print the value via the dereferenced pointer } COPY Inside the printByAddress() function, we dereference ptr parameter to access the value of the object being pointed to. Second, when the function is called, we can\u2019t just pass in the str object -- we need to pass in the address of str . The easiest way to do that is to use the address-of operator (&) to get a pointer holding the address of str : 1 printByAddress ( & str ); // use address-of operator (&) to get pointer holding address of str COPY When this call is executed, &str will create a pointer holding the address of str . This address is then copied into function parameter ptr as part of the function call. Because ptr now holds the address of str , when the function dereferences ptr , it will get the value of str , which the function prints to the console. That\u2019s it. Although we use the address-of operator in the above example to get the address of str , if we already had a pointer variable holding the address of str , we could use that instead: 1 2 3 4 5 6 7 8 9 10 11 12 13 int main () { std :: string str { \"Hello, world!\" }; printByValue ( str ); // pass str by value, makes a copy of str printByReference ( str ); // pass str by reference, does not make a copy of str printByAddress ( & str ); // pass str by address, does not make a copy of str std :: string * ptr { & str }; // define a pointer variable holding the address of str printByAddress ( ptr ); // pass str by address, does not make a copy of str return 0 ; } \u6309\u5730\u5740\u4f20\u9012\u4e0d\u4f1a\u521b\u5efa\u88ab\u6307\u7684\u5bf9\u8c61\u7684\u62f7\u8d1d Consider the following statements: 1 2 std :: string str { \"Hello, world!\" }; printByAddress ( & str ); // use address-of operator (&) to get pointer holding address of str As we noted in 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 , 9.5 -- Pass by lvalue reference , copying a std::string is expensive, so that\u2019s something we want to avoid. When we pass a std::string by address, we\u2019re not copying the actual std::string object -- we\u2019re just copying the pointer (holding the address of the object) from the caller to the called function. Since an address is typically only 4 or 8 bytes, a pointer is only 4 or 8 bytes, so copying a pointer is always fast. Thus, just like pass by reference, pass by address is fast, and avoids making a copy of the argument object. Pass by address allows the function to modify the argument\u2019s value When we pass an object by address, the function receives the address of the passed object, which it can access via dereferencing. Because this is the address of the actual argument object being passed (not a copy of the object), if the function parameter is a pointer to non-const, the function can modify the argument via the pointer parameter: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void changeValue ( int * ptr ) // note: ptr is a pointer to non-const in this example { * ptr = 6 ; // change the value to 6 } int main () { int x { 5 }; std :: cout << \"x = \" << x << '\\n' ; changeValue ( & x ); // we're passing the address of x to the function std :: cout << \"x = \" << x << '\\n' ; return 0 ; } COPY This prints: 1 2 x = 5 x = 6 As you can see, the argument is modified and this modification persists even after changeValue() has finished running. If a function is not supposed to modify the object being passed in, the function parameter can be made a pointer to const: 1 2 3 4 void changeValue ( const int * ptr ) // note: ptr is now a pointer to a const { * ptr = 6 ; // error: can not change const value } COPY \u6307\u9488\u5224\u7a7a Now consider this fairly innocent looking program: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void print ( int * ptr ) { std :: cout << * ptr ; } int main () { int x { 5 }; print ( & x ); int * myptr {}; print ( myptr ); return 0 ; } COPY When this program is run, it will print the value 5 and then most likely crash. In the call to print(myptr) , myptr is a null pointer, so function parameter ptr will also be a null pointer. When this null pointer is dereferenced in the body of the function, undefined behavior results. When passing a parameter by address, care should be taken to ensure the pointer is not a null pointer before you dereference the value. One way to do that is to use a conditional statement: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void print ( int * ptr ) { if ( ptr ) // if ptr is not a null pointer { std :: cout << * ptr ; } } int main () { int x { 5 }; print ( & x ); print ( nullptr ); return 0 ; } COPY In the above program, we\u2019re testing ptr to ensure it is not null before we dereference it. While this is fine for such a simple function, in more complicated functions this can result in redundant logic (testing if ptr is not null multiple times) or nesting of the primary logic of the function (if contained in a block). In most cases, it is more effective to do the opposite: test whether the function parameter is null as a precondition ( 7.17 -- Assert and static_assert ) and handle the negative case immediately: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include void print ( int * ptr ) { if ( ! ptr ) // if ptr is a null pointer, early return back to the caller return ; // if we reached this point, we can assume ptr is valid // so no more testing or nesting required std :: cout << * ptr ; } int main () { int x { 5 }; print ( & x ); print ( nullptr ); return 0 ; } COPY If a null pointer should never be passed to the function, an assert (which we covered in lesson 7.17 -- Assert and static_assert ) can be used instead (or also) (as asserts are intended to document things that should never happen): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include void print ( const int * ptr ) // now a pointer to a const int { assert ( ptr ); // fail the program in debug mode if a null pointer is passed (since this should never happen) // (optionally) handle this as an error case in production mode so we don't crash if it does happen if ( ! ptr ) return ; std :: cout << * ptr ; } int main () { int x { 5 }; print ( & x ); print ( nullptr ); return 0 ; } COPY \u63a8\u8350\u6309\uff08const\uff09\u5f15\u7528\u4f20\u9012 Note that function print() in the example above doesn\u2019t handle null values very well -- it effectively just aborts the function. Given this, why allow a user to pass in a null value at all? Pass by reference has the same benefits as pass by address without the risk of inadvertently dereferencing a null pointer. Pass by const reference has a few other advantages over pass by address. First, because an object being passed by address must have an address, only lvalues can be passed by address (as rvalues don\u2019t have addresses). Pass by const reference is more flexible, as it can accept lvalues and rvalues: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include void printByValue ( int val ) // The function parameter is a copy of the argument { std :: cout << val << '\\n' ; // print the value via the copy } void printByReference ( const int & ref ) // The function parameter is a reference that binds to the argument { std :: cout << ref << '\\n' ; // print the value via the reference } void printByAddress ( const int * ptr ) // The function parameter is a pointer that holds the address of the argument { std :: cout << * ptr << '\\n' ; // print the value via the dereferenced pointer } int main () { printByValue ( 5 ); // valid (but makes a copy) printByReference ( 5 ); // valid (because the parameter is a const reference) printByAddress ( & 5 ); // error: can't take address of r-value return 0 ; } COPY Second, the syntax for pass by reference is natural, as we can just pass in literals or objects. With pass by address, our code ends up littered with ampersands (&) and asterisks (*). In modern C++, most things that can be done with pass by address are better accomplished through other methods. Follow this common maxim: \u201cPass by reference when you can, pass by address when you must\u201d. \u6700\u4f73\u5b9e\u8df5 Prefer pass by reference to pass by address unless you have a specific reason to use pass by address.","title":"9.9 - \u6309\u5730\u5740\u4f20\u9012"},{"location":"9-9-Pass-by-address/#pass-by-address","text":"C++ provides a third way to pass values to a function, called pass by address. With pass by address, instead of providing an object as an argument, the caller provides an object\u2019s address (via a pointer). This pointer (holding the address of the object) is copied into a pointer parameter of the called function (which now also holds the address of the object). The function can then dereference that pointer to access the object whose address was passed. Here\u2019s a version of the above program that adds a pass by address variant: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include #include void printByValue ( std :: string val ) // The function parameter is a copy of str { std :: cout << val << '\\n' ; // print the value via the copy } void printByReference ( const std :: string & ref ) // The function parameter is a reference that binds to str { std :: cout << ref << '\\n' ; // print the value via the reference } void printByAddress ( const std :: string * ptr ) // The function parameter is a pointer that holds the address of str { std :: cout << * ptr << '\\n' ; // print the value via the dereferenced pointer } int main () { std :: string str { \"Hello, world!\" }; printByValue ( str ); // pass str by value, makes a copy of str printByReference ( str ); // pass str by reference, does not make a copy of str printByAddress ( & str ); // pass str by address, does not make a copy of str return 0 ; } COPY Note how similar all three of these versions are. Let\u2019s explore the pass by address version in more detail. First, because we want our printByAddress() function to use pass by address, we\u2019ve made our function parameter a pointer named ptr . Since printByAddress() will use ptr in a read-only manner, ptr is a pointer to a const value. 1 2 3 4 void printByAddress ( const std :: string * ptr ) { std :: cout << * ptr << '\\n' ; // print the value via the dereferenced pointer } COPY Inside the printByAddress() function, we dereference ptr parameter to access the value of the object being pointed to. Second, when the function is called, we can\u2019t just pass in the str object -- we need to pass in the address of str . The easiest way to do that is to use the address-of operator (&) to get a pointer holding the address of str : 1 printByAddress ( & str ); // use address-of operator (&) to get pointer holding address of str COPY When this call is executed, &str will create a pointer holding the address of str . This address is then copied into function parameter ptr as part of the function call. Because ptr now holds the address of str , when the function dereferences ptr , it will get the value of str , which the function prints to the console. That\u2019s it. Although we use the address-of operator in the above example to get the address of str , if we already had a pointer variable holding the address of str , we could use that instead: 1 2 3 4 5 6 7 8 9 10 11 12 13 int main () { std :: string str { \"Hello, world!\" }; printByValue ( str ); // pass str by value, makes a copy of str printByReference ( str ); // pass str by reference, does not make a copy of str printByAddress ( & str ); // pass str by address, does not make a copy of str std :: string * ptr { & str }; // define a pointer variable holding the address of str printByAddress ( ptr ); // pass str by address, does not make a copy of str return 0 ; }","title":"Pass by address"},{"location":"9-9-Pass-by-address/#_1","text":"Consider the following statements: 1 2 std :: string str { \"Hello, world!\" }; printByAddress ( & str ); // use address-of operator (&) to get pointer holding address of str As we noted in 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 , 9.5 -- Pass by lvalue reference , copying a std::string is expensive, so that\u2019s something we want to avoid. When we pass a std::string by address, we\u2019re not copying the actual std::string object -- we\u2019re just copying the pointer (holding the address of the object) from the caller to the called function. Since an address is typically only 4 or 8 bytes, a pointer is only 4 or 8 bytes, so copying a pointer is always fast. Thus, just like pass by reference, pass by address is fast, and avoids making a copy of the argument object.","title":"\u6309\u5730\u5740\u4f20\u9012\u4e0d\u4f1a\u521b\u5efa\u88ab\u6307\u7684\u5bf9\u8c61\u7684\u62f7\u8d1d"},{"location":"9-9-Pass-by-address/#pass-by-address-allows-the-function-to-modify-the-arguments-value","text":"When we pass an object by address, the function receives the address of the passed object, which it can access via dereferencing. Because this is the address of the actual argument object being passed (not a copy of the object), if the function parameter is a pointer to non-const, the function can modify the argument via the pointer parameter: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void changeValue ( int * ptr ) // note: ptr is a pointer to non-const in this example { * ptr = 6 ; // change the value to 6 } int main () { int x { 5 }; std :: cout << \"x = \" << x << '\\n' ; changeValue ( & x ); // we're passing the address of x to the function std :: cout << \"x = \" << x << '\\n' ; return 0 ; } COPY This prints: 1 2 x = 5 x = 6 As you can see, the argument is modified and this modification persists even after changeValue() has finished running. If a function is not supposed to modify the object being passed in, the function parameter can be made a pointer to const: 1 2 3 4 void changeValue ( const int * ptr ) // note: ptr is now a pointer to a const { * ptr = 6 ; // error: can not change const value } COPY","title":"Pass by address allows the function to modify the argument\u2019s value"},{"location":"9-9-Pass-by-address/#_2","text":"Now consider this fairly innocent looking program: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void print ( int * ptr ) { std :: cout << * ptr ; } int main () { int x { 5 }; print ( & x ); int * myptr {}; print ( myptr ); return 0 ; } COPY When this program is run, it will print the value 5 and then most likely crash. In the call to print(myptr) , myptr is a null pointer, so function parameter ptr will also be a null pointer. When this null pointer is dereferenced in the body of the function, undefined behavior results. When passing a parameter by address, care should be taken to ensure the pointer is not a null pointer before you dereference the value. One way to do that is to use a conditional statement: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void print ( int * ptr ) { if ( ptr ) // if ptr is not a null pointer { std :: cout << * ptr ; } } int main () { int x { 5 }; print ( & x ); print ( nullptr ); return 0 ; } COPY In the above program, we\u2019re testing ptr to ensure it is not null before we dereference it. While this is fine for such a simple function, in more complicated functions this can result in redundant logic (testing if ptr is not null multiple times) or nesting of the primary logic of the function (if contained in a block). In most cases, it is more effective to do the opposite: test whether the function parameter is null as a precondition ( 7.17 -- Assert and static_assert ) and handle the negative case immediately: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include void print ( int * ptr ) { if ( ! ptr ) // if ptr is a null pointer, early return back to the caller return ; // if we reached this point, we can assume ptr is valid // so no more testing or nesting required std :: cout << * ptr ; } int main () { int x { 5 }; print ( & x ); print ( nullptr ); return 0 ; } COPY If a null pointer should never be passed to the function, an assert (which we covered in lesson 7.17 -- Assert and static_assert ) can be used instead (or also) (as asserts are intended to document things that should never happen): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include void print ( const int * ptr ) // now a pointer to a const int { assert ( ptr ); // fail the program in debug mode if a null pointer is passed (since this should never happen) // (optionally) handle this as an error case in production mode so we don't crash if it does happen if ( ! ptr ) return ; std :: cout << * ptr ; } int main () { int x { 5 }; print ( & x ); print ( nullptr ); return 0 ; } COPY","title":"\u6307\u9488\u5224\u7a7a"},{"location":"9-9-Pass-by-address/#const","text":"Note that function print() in the example above doesn\u2019t handle null values very well -- it effectively just aborts the function. Given this, why allow a user to pass in a null value at all? Pass by reference has the same benefits as pass by address without the risk of inadvertently dereferencing a null pointer. Pass by const reference has a few other advantages over pass by address. First, because an object being passed by address must have an address, only lvalues can be passed by address (as rvalues don\u2019t have addresses). Pass by const reference is more flexible, as it can accept lvalues and rvalues: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include void printByValue ( int val ) // The function parameter is a copy of the argument { std :: cout << val << '\\n' ; // print the value via the copy } void printByReference ( const int & ref ) // The function parameter is a reference that binds to the argument { std :: cout << ref << '\\n' ; // print the value via the reference } void printByAddress ( const int * ptr ) // The function parameter is a pointer that holds the address of the argument { std :: cout << * ptr << '\\n' ; // print the value via the dereferenced pointer } int main () { printByValue ( 5 ); // valid (but makes a copy) printByReference ( 5 ); // valid (because the parameter is a const reference) printByAddress ( & 5 ); // error: can't take address of r-value return 0 ; } COPY Second, the syntax for pass by reference is natural, as we can just pass in literals or objects. With pass by address, our code ends up littered with ampersands (&) and asterisks (*). In modern C++, most things that can be done with pass by address are better accomplished through other methods. Follow this common maxim: \u201cPass by reference when you can, pass by address when you must\u201d. \u6700\u4f73\u5b9e\u8df5 Prefer pass by reference to pass by address unless you have a specific reason to use pass by address.","title":"\u63a8\u8350\u6309\uff08const\uff09\u5f15\u7528\u4f20\u9012"},{"location":"9-x-Chapter-9-comprehensive-quiz/","tags":["summary"],"text":"\u590d\u4e60 9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b \u590d\u5408\u6570\u636e\u7c7b\u578b (\u4e5f\u79f0\u4f5c \u7ec4\u5408\u6570\u636e\u7c7b\u578b )\uff0c\u6307\u7684\u662f\u54ea\u4e9b\u7531\u57fa\u7840\u6570\u636e\u7c7b\u578b\u7ec4\u6210\u7684\u6570\u636e\u7c7b\u578b\uff08\u6216\u7531\u5176\u4ed6\u590d\u5408\u7c7b\u578b\u7ec4\u6210\uff09\u3002 9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09 \u4e00\u4e2a\u8868\u8fbe\u5f0f\u7684\u503c\u7c7b\u522b\uff08value category\uff09\u8868\u660e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u6700\u7ec8\u4f1a\u88ab\u89e3\u6790\u4e3a\u4e00\u4e2a\u503c\u3001\u4e00\u4e2a\u51fd\u6570\u8fd8\u662f\u67d0\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\u3002 \u5de6\u503c \u8868\u8fbe\u5f0f\u6c42\u503c\u4e3a\u4e00\u4e2a\u5177\u6709\u6807\u8bc6\u7684\u51fd\u6570\u6216\u8005\u4e00\u4e2a\u5bf9\u8c61\u3002\u4e00\u4e2a\u5bf9\u8c61\u5177\u6709\u6807\u8bc6\uff0c\u610f\u5473\u7740\u5b83\u5177\u6709\u6807\u8bc6\u7b26\u6216\u8005\u4e00\u4e2a\u5177\u6709\u6807\u8bc6\u7684\u5185\u5b58\u5730\u5740\u3002\u5de6\u503c\u6709\u4e24\u4e2a\u5b50\u7c7b\uff1a\u53ef\u53d8\u5de6\u503c\u662f\u53ef\u4ee5\u88ab\u4fee\u6539\u7684\u5de6\u503c\uff0c\u4e0d\u53ef\u53d8\u5de6\u503c\u662f\u4e0d\u53ef\u4ee5\u88ab\u4fee\u6539\u7684\u5de6\u503c\uff08\u901a\u5e38\u56e0\u4e3a\u5b83\u4eec\u662f const \u6216 constexpr \u7c7b\u578b\u7684\uff09\u3002 \u5f53\u8868\u8fbe\u5f0f\u4e0d\u662f\u5de6\u503c\u65f6\uff0c\u5b83\u4fbf\u662f \u53f3\u503c \u3002\u5305\u62ec\u5b57\u9762\u91cf\uff08\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u9664\u5916\uff09\u548c\u51fd\u6570\u6216\u64cd\u4f5c\uff08\u5f53 \u6309\u503c\u8fd4\u56de \u65f6\uff09\u7684\u8fd4\u56de\u503c\u3002 9.3 - \u5de6\u503c\u5f15\u7528 \u5f15\u7528 \u662f\u67d0\u4e2a\u5df2\u7ecf \u5b58\u5728 \u7684\u5bf9\u8c61\u7684\u522b\u540d\u3002\u5f15\u7528\u4e00\u65e6\u88ab\u5b9a\u4e49\uff0c\u4efb\u4f55\u5bf9\u5f15\u7528\u7684\u64cd\u4f5c\u90fd\u76f8\u5f53\u4e8e\u64cd\u4f5c\u8be5\u5f15\u7528\u6240\u8868\u793a\u7684\u5bf9\u8c61\u3002C++ \u6709\u4e24\u79cd\u5f15\u7528\uff0c\u5de6\u503c\u5f15\u7528\u548c\u53f3\u503c\u5f15\u7528\u3002\u5de6\u503c\u5f15\u7528\uff08\u7b80\u79f0\u5f15\u7528\uff09\u5c31\u662f\u67d0\u4e2a\u5de6\u503c\u7684\u522b\u540d\u3002\u5de6\u503c\u5f15\u7528\u53d8\u91cf\u5c31\u662f\u4e00\u4e2a\u7528\u4f5c\u5de6\u503c\u5f15\u7528\u7684\u53d8\u91cf\uff0c\u5176\u5f15\u7528\u7684\u5de6\u503c\u901a\u5e38\u662f\u53e6\u5916\u4e00\u4e2a\u53d8\u91cf\u3002 \u5f53\u4f7f\u7528\u5bf9\u8c61\u6216\u51fd\u6570\u5bf9\u5f15\u7528\u8fdb\u884c\u521d\u59cb\u5316\u4e4b\u540e\uff0c\u6211\u4eec\u8bf4\u8be5\u5f15\u7528\u548c\u5bf9\u8c61\u6216\u51fd\u6570\u7ed1\u5b9a\u4e86\u3002\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u6216\u8005\u51fd\u6570\u79f0\u4e3areferent\u3002 \u5de6\u503c\u5f15\u7528\u4e0d\u53ef\u4ee5\u88ab\u7ed1\u5b9a\u5230\u4e0d\u53ef\u4fee\u6539\u7684\u5de6\u503c\u6216\u53f3\u503c\uff08\u4e0d\u7136\u7684\u8bdd\u4f60\u5c31\u53ef\u4ee5\u901a\u8fc7\u8be5\u5f15\u7528\u6765\u4fee\u6539\u5b83\u4eec\u7684\u503c\u4e86\uff0c\u8fd9\u8fdd\u53cd\u4e86\u5b83\u4eec\u4f5c\u4e3a\u5e38\u91cf\u7684\u5c5e\u6027\uff09\u3002\u56e0\u6b64\uff0c\u5de6\u503c\u5f15\u7528\u6709\u65f6\u4e5f\u88ab\u79f0\u4e3a\u5de6\u503c\u975e\u5e38\u91cf\u5f15\u7528\uff08\u7b80\u79f0\u5e38\u91cf\u5f15\u7528\uff09\u3002 \u5f15\u7528\u4e00\u65e6\u521d\u59cb\u5316\uff0c\u5b83\u4fbf\u4e0d\u80fd\u591f\u88ab\u91cd\u7f6e\uff0c\u4e5f\u5c31\u662f\u8bf4\u4e0d\u80fd\u4fee\u6539\u5b83\u53bb\u5f15\u7528\u522b\u7684\u5bf9\u8c61\u3002 \u5f53\u5982\u679c\u5bf9\u8c61\u5148\u4e8e\u5b83\u7684\u5f15\u7528\u88ab\u9500\u6bc1\uff0c\u5219\u8be5\u5f15\u7528\u4f1a\u6307\u5411\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u5bf9\u8c61\uff0c\u6b64\u65f6\u8be5\u5f15\u7528\u88ab\u79f0\u4e3a \u60ac\u5782 \u5f15\u7528\u3002\u8bbf\u95ee\u60ac\u5782\u5f15\u7528\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 \u5f53\u4f7f\u7528 const \u5173\u952e\u5b57\u6765\u4fee\u9970\u4e00\u4e2a\u5de6\u503c\u5f15\u7528\u65f6\uff0c\u5373\u8981\u6c42\u8be5\u5f15\u7528\u5c06\u5b83\u6240\u5f15\u7528\u7684\u5bf9\u8c61\u770b\u505a\u5e38\u91cf\u3002\u6b64\u65f6\u6210\u8be5\u5f15\u7528\u4e3a \u6307\u5411\u5e38\u91cf\u503c\u7684\u5de6\u503c\u5f15\u7528 \uff08\u7b80\u79f0\u6307\u5411\u5e38\u91cf\u7684\u5f15\u7528\u6216\u5e38\u91cf\u5f15\u7528\uff09\u3002 \u5e38\u91cf\u5f15\u7528 \u53ef\u4ee5\u88ab\u7ed1\u5b9a\u5230\u53ef\u4ee5\u4fee\u6539\u5de6\u503c\uff0c\u4e0d\u53ef\u4fee\u6539\u5de6\u503c\u548c\u53f3\u503c\u3002 \u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff08\u6709\u65f6\u79f0\u4e3a\u672a\u547d\u540d\u5bf9\u8c61\u6216\u533f\u540d\u5bf9\u8c61\uff09\u662f\u6307\u5728\u4e00\u4e2a\u5355\u4e00\u8868\u8fbe\u5f0f\u4e2d\u4e3a\u4e86\u4e34\u65f6\u4f7f\u7528\u800c\u521b\u5efa\u7684\u5bf9\u8c61\uff08\u4f7f\u7528\u540e\u9500\u6bc1\uff09\u3002 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 \u5f53\u8fdb\u884c \u6309\u5f15\u7528\u4f20\u9012 \u65f6\uff0c\u6211\u4eec\u5c06\u51fd\u6570\u7684 \u5f62\u53c2 \u58f0\u660e\u4e3a\u5f15\u7528\u6216\u5e38\u91cf\u5f15\u7528\u800c\u4e0d\u662f\u666e\u901a\u7684\u53d8\u91cf\u3002\u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u5f62\u53c2\u4e2d\u7684\u5f15\u7528\u4f1a\u7ed1\u5b9a\u5230 \u5b9e\u53c2 \u3002\u56e0\u4e3a\u5f15\u7528\u53ea\u662f\u5bf9\u8c61\u7684\u522b\u540d\uff0c\u6240\u4ee5\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5e76\u4e0d\u4f1a\u521b\u5efa\u5b9e\u53c2\u7684\u62f7\u8d1d\u3002 \u53d6\u5730\u5740\u8fd0\u7b97\u7b26 (&) \u4f1a\u8fd4\u56de\u5176\u64cd\u4f5c\u6570\u7684\u5730\u5740\u3002\u800c \u89e3\u5f15\u7528(*) \u8fd0\u7b97\u7b26\u5219\u4f1a\u8fd4\u56de\u7ed9\u5b9a\u5730\u5740\u5b58\u653e\u7684\u5de6\u503c\u3002 9.6 - \u6307\u9488\u7b80\u4ecb \u6307\u9488\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5b83 \u6301\u6709 \u4e00\u4e2a\u5185\u5b58\u5730\u5740\uff08\u901a\u5e38\u662f\u53e6\u5916\u4e00\u4e2a\u53d8\u91cf\u7684\u5730\u5740\uff09\u4f5c\u4e3a\u5176\u503c\u3002\u4f7f\u7528\u6307\u9488\u6211\u4eec\u53ef\u4ee5\u5c06\u67d0\u4e2a\u5bf9\u8c61\u7684\u5730\u5740\u4fdd\u5b58\u8d77\u6765\u4ee5\u4fbf\u7a0d\u540e\u4f7f\u7528\u3002\u548c\u4e0d\u540c\u7684\u53d8\u91cf\u4e00\u6837\uff0c\u6307\u9488\u4e0d\u4f1a\u88ab\u9ed8\u8ba4\u521d\u59cb\u5316\u3002\u800c\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u7684\u6307\u9488\u901a\u5e38\u88ab\u79f0\u4e3a \u91ce\u6307\u9488 \u3002 \u60ac\u5782 \u6307\u9488\u5219\u662f\u90a3\u4e9b\u4fdd\u5b58\u7740\u65e0\u6548\u5730\u5740\u7684\u6307\u9488\uff08\u8be5\u5730\u5740\u4e2d\u7684\u5bf9\u8c61\u5df2\u7ecf\u88ab\u9500\u6bc1\uff09\u3002 9.7 - \u7a7a\u6307\u9488 \u9664\u4e86\u5185\u5b58\u5730\u5740\uff0c\u6307\u9488\u8fd8\u53ef\u4ee5\u4fdd\u5b58\u4e00\u4e2a\u7279\u6b8a\u503c\uff0c\u5373\u7a7a\u503c\uff08null\uff09\u3002null\u662f\u4e00\u4e2a\u7279\u6b8a\u503c\u5b83\u8868\u793a\u7a7a\u503c\u3002\u5f53\u6307\u9488\u7684\u503c\u4e3anull\u65f6\uff0c\u8868\u793a\u5b83\u6ca1\u6709\u6307\u5411\u4efb\u4f55\u5730\u5740\u3002\u6b64\u65f6\u8be5\u6307\u9488\u88ab\u79f0\u4e3a\u7a7a\u6307\u9488\u3002\u5173\u952e\u5b57 nullptr \u662f\u7a7a\u6307\u9488\u7684\u5b57\u9762\u91cf\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 nullptr \u6765\u663e\u5f0f\u5730\u4e3a\u6307\u9488\u521d\u59cb\u5316\u6216\u8d4b\u503c\u4e3a\u7a7a\u503c\u3002 \u6307\u9488\u5e94\u5f53\u6301\u6709\u4e00\u4e2a\u5305\u542b\u6709\u6548\u5bf9\u8c61\u7684\u5730\u5740\uff0c\u6216\u8005\u8bbe\u7f6e\u4e3a nullptr \u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6211\u4eec\u53ea\u9700\u8981\u5224\u65ad\u6307\u9488\u662f\u4e0d\u662f\u7a7a\u5373\u53ef\uff0c\u5426\u5219\u6211\u4eec\u5c31\u8ba4\u4e3a\u5b83\u662f\u6709\u6548\u7684\u3002 9.8 - \u6307\u9488\u548cconst \u6307\u9488\u5982\u679c\u6307\u5411\u4e00\u4e2aconst\u5e38\u91cf\uff08\u6709\u65f6\u7b80\u79f0\u4e3a\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488\uff09\uff0c\u5219\u8868\u793a\u4e00\u4e2a\uff08\u975e\u5e38\u91cf\uff09\u6307\u9488\u6307\u5411\u4e00\u4e2a\u5e38\u91cf\u3002 \u800c\u4e00\u4e2a\u5e38\u91cf\u6307\u9488\uff0c\u5219\u662f\u5728\u5176\u6301\u6709\u5730\u5740\u88ab\u521d\u59cb\u540e\uff0c\u4e0d\u80fd\u518d\u6539\u53d8\u7684\u6307\u9488\u3002 \u6307\u5411\u5e38\u91cf\u7684\u5e38\u91cf\u6307\u9488\uff0c\u5176\u6307\u9488\u672c\u8eab\u6240\u6301\u6709\u7684\u5730\u5740\u4e0d\u80fd\u6539\u53d8\uff0c\u4e5f\u4e0d\u80fd\u4fee\u6539\u8be5\u5730\u5740\u6240\u6307\u5411\u7684\u5bf9\u8c61\u3002 9.9 - \u6309\u5730\u5740\u4f20\u9012 \u5bf9\u4e8e \u4f20\u5730\u5740 \u6765\u8bf4\uff0c\u8c03\u7528\u8005\u4f1a\u4f7f\u7528\u5bf9\u8c61\u7684\u5730\u5740\uff08\u901a\u8fc7\u6307\u9488\uff09\u800c\u4e0d\u662f\u5bf9\u8c61\u672c\u8eab\u4f5c\u4e3a \u5b9e\u53c2 \u3002\u8be5\u6307\u9488\uff08\u6301\u6709\u8be5\u5bf9\u8c61\u7684\u5730\u5740\uff09\u4f1a\u88ab\u62f7\u8d1d\u5230\u88ab\u8c03\u51fd\u6570\u7684\u6307\u9488\u53c2\u6570\uff08 \u5f62\u53c2 \uff09\uff0c\u8be5\u53c2\u6570\u6b64\u65f6\u4fdd\u5b58\u7740\u8be5\u5bf9\u8c61\u6240\u5728\u5730\u5740\uff08\u7684\u4e00\u4efd\u62f7\u8d1d\uff09\u3002\u968f\u540e\uff0c\u51fd\u6570\u53ef\u4ee5\u5bf9\u8be5\u6307\u9488\u8fdb\u884c \u89e3\u5f15\u7528 \u4ee5\u8bbf\u95ee\u8be5\u5bf9\u8c61\u3002 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 \u6309\u5f15\u7528\u8fd4\u56de \u65f6\uff0c\u51fd\u6570\u8fd4\u56de\u7ed1\u5b9a\u5230\u8fd4\u56de\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4e0d\u4f1a\u5bf9\u8fd4\u56de\u503c\u8fdb\u884c\u62f7\u8d1d\u3002\u5728\u8fd4\u56de\u5f15\u7528\u65f6\uff0c\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\uff1a\u8be5\u5f15\u7528\u6240\u7ed1\u5b9a\u7684\u5bf9\u8c61\u5176\u751f\u547d\u5468\u671f\u8981\u6bd4\u51fd\u6570\u957f\u3002\u5426\u5219\u8fd4\u56de\u7684\u5f15\u7528\u5c06\u53d8\u6210 \u60ac\u5782 \u5f15\u7528\uff08\u5f15\u7528\u7684\u5bf9\u8c61\u88ab\u9500\u6bc1\u4e86\uff09\uff0c\u6b64\u65f6\u7ee7\u7eed\u4f7f\u7528\u8be5\u5f15\u7528\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 \u5982\u679c\u5b9e\u53c2\u901a\u8fc7\u5f15\u7528\u7684\u65b9\u5f0f\u88ab\u4f20\u5165\uff0c\u90a3\u4e48\u4f7f\u7528\u6309\u5f15\u7528\u8fd4\u56de\u8be5\u53c2\u6570\u4e5f\u662f\u66f4\u5b89\u5168\u7684 \u3002 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de \u5982\u679c\u4e00\u4e2a\u51fd\u6570\u8fd4\u56de\u5f15\u7528\uff0c\u4e14\u8be5\u5f15\u7528\u88ab\u7528\u6765\u521d\u59cb\u5316\u6216\u8d4b\u503c\u7ed9\u4e00\u4e2a\u975e\u5f15\u7528\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u90a3\u4e48\u8be5\u8fd4\u56de\u503c\u4f1a\u88ab\u62f7\u8d1d\uff08\u548c \u6309\u503c\u8fd4\u56de \u7684\u6548\u679c\u662f\u4e00\u6837\u7684\uff09\u3002 \u6309\u5730\u5740\u8fd4\u56de \u548c \u6309\u5f15\u7528\u8fd4\u56de \u7684\u5de5\u4f5c\u539f\u7406\u51e0\u4e4e\u662f\u4e00\u6837\u7684\uff0c\u533a\u522b\u53ea\u5728\u4e8e\u524d\u8005\u8fd4\u56de\u7684\u662f\u6307\u5411\u5bf9\u8c61\u7684\u6307\u9488\u800c\u540e\u8005\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5bf9\u8c61\u7684\u5f15\u7528\u3002 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad \u4f7f\u7528 auto \u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\u65f6\uff0c\u5b83\u4f1a\u79fb\u9664\u5f15\u7528\u6807\u8bb0\u6216\u5176\u4ed6\u9876\u5c42\u5e38\u91cf\u4fee\u9970\u7b26\u3002\u5982\u679c\uff0c\u4f60\u4ecd\u9700\u8981\u8fd9\u4e9b\u6807\u8bc6\u7b26\uff0c\u5219\u9700\u8981\u5c06\u5176\u4f5c\u4e3a\u53d8\u91cf\u58f0\u660e\u7684\u4e00\u90e8\u5206\u8fdb\u884c\u6dfb\u52a0\u3002","title":"9.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f15\u7528\u548c\u6307\u9488"},{"location":"9-x-Chapter-9-comprehensive-quiz/#_1","text":"9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b \u590d\u5408\u6570\u636e\u7c7b\u578b (\u4e5f\u79f0\u4f5c \u7ec4\u5408\u6570\u636e\u7c7b\u578b )\uff0c\u6307\u7684\u662f\u54ea\u4e9b\u7531\u57fa\u7840\u6570\u636e\u7c7b\u578b\u7ec4\u6210\u7684\u6570\u636e\u7c7b\u578b\uff08\u6216\u7531\u5176\u4ed6\u590d\u5408\u7c7b\u578b\u7ec4\u6210\uff09\u3002 9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09 \u4e00\u4e2a\u8868\u8fbe\u5f0f\u7684\u503c\u7c7b\u522b\uff08value category\uff09\u8868\u660e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u6700\u7ec8\u4f1a\u88ab\u89e3\u6790\u4e3a\u4e00\u4e2a\u503c\u3001\u4e00\u4e2a\u51fd\u6570\u8fd8\u662f\u67d0\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\u3002 \u5de6\u503c \u8868\u8fbe\u5f0f\u6c42\u503c\u4e3a\u4e00\u4e2a\u5177\u6709\u6807\u8bc6\u7684\u51fd\u6570\u6216\u8005\u4e00\u4e2a\u5bf9\u8c61\u3002\u4e00\u4e2a\u5bf9\u8c61\u5177\u6709\u6807\u8bc6\uff0c\u610f\u5473\u7740\u5b83\u5177\u6709\u6807\u8bc6\u7b26\u6216\u8005\u4e00\u4e2a\u5177\u6709\u6807\u8bc6\u7684\u5185\u5b58\u5730\u5740\u3002\u5de6\u503c\u6709\u4e24\u4e2a\u5b50\u7c7b\uff1a\u53ef\u53d8\u5de6\u503c\u662f\u53ef\u4ee5\u88ab\u4fee\u6539\u7684\u5de6\u503c\uff0c\u4e0d\u53ef\u53d8\u5de6\u503c\u662f\u4e0d\u53ef\u4ee5\u88ab\u4fee\u6539\u7684\u5de6\u503c\uff08\u901a\u5e38\u56e0\u4e3a\u5b83\u4eec\u662f const \u6216 constexpr \u7c7b\u578b\u7684\uff09\u3002 \u5f53\u8868\u8fbe\u5f0f\u4e0d\u662f\u5de6\u503c\u65f6\uff0c\u5b83\u4fbf\u662f \u53f3\u503c \u3002\u5305\u62ec\u5b57\u9762\u91cf\uff08\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u9664\u5916\uff09\u548c\u51fd\u6570\u6216\u64cd\u4f5c\uff08\u5f53 \u6309\u503c\u8fd4\u56de \u65f6\uff09\u7684\u8fd4\u56de\u503c\u3002 9.3 - \u5de6\u503c\u5f15\u7528 \u5f15\u7528 \u662f\u67d0\u4e2a\u5df2\u7ecf \u5b58\u5728 \u7684\u5bf9\u8c61\u7684\u522b\u540d\u3002\u5f15\u7528\u4e00\u65e6\u88ab\u5b9a\u4e49\uff0c\u4efb\u4f55\u5bf9\u5f15\u7528\u7684\u64cd\u4f5c\u90fd\u76f8\u5f53\u4e8e\u64cd\u4f5c\u8be5\u5f15\u7528\u6240\u8868\u793a\u7684\u5bf9\u8c61\u3002C++ \u6709\u4e24\u79cd\u5f15\u7528\uff0c\u5de6\u503c\u5f15\u7528\u548c\u53f3\u503c\u5f15\u7528\u3002\u5de6\u503c\u5f15\u7528\uff08\u7b80\u79f0\u5f15\u7528\uff09\u5c31\u662f\u67d0\u4e2a\u5de6\u503c\u7684\u522b\u540d\u3002\u5de6\u503c\u5f15\u7528\u53d8\u91cf\u5c31\u662f\u4e00\u4e2a\u7528\u4f5c\u5de6\u503c\u5f15\u7528\u7684\u53d8\u91cf\uff0c\u5176\u5f15\u7528\u7684\u5de6\u503c\u901a\u5e38\u662f\u53e6\u5916\u4e00\u4e2a\u53d8\u91cf\u3002 \u5f53\u4f7f\u7528\u5bf9\u8c61\u6216\u51fd\u6570\u5bf9\u5f15\u7528\u8fdb\u884c\u521d\u59cb\u5316\u4e4b\u540e\uff0c\u6211\u4eec\u8bf4\u8be5\u5f15\u7528\u548c\u5bf9\u8c61\u6216\u51fd\u6570\u7ed1\u5b9a\u4e86\u3002\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u6216\u8005\u51fd\u6570\u79f0\u4e3areferent\u3002 \u5de6\u503c\u5f15\u7528\u4e0d\u53ef\u4ee5\u88ab\u7ed1\u5b9a\u5230\u4e0d\u53ef\u4fee\u6539\u7684\u5de6\u503c\u6216\u53f3\u503c\uff08\u4e0d\u7136\u7684\u8bdd\u4f60\u5c31\u53ef\u4ee5\u901a\u8fc7\u8be5\u5f15\u7528\u6765\u4fee\u6539\u5b83\u4eec\u7684\u503c\u4e86\uff0c\u8fd9\u8fdd\u53cd\u4e86\u5b83\u4eec\u4f5c\u4e3a\u5e38\u91cf\u7684\u5c5e\u6027\uff09\u3002\u56e0\u6b64\uff0c\u5de6\u503c\u5f15\u7528\u6709\u65f6\u4e5f\u88ab\u79f0\u4e3a\u5de6\u503c\u975e\u5e38\u91cf\u5f15\u7528\uff08\u7b80\u79f0\u5e38\u91cf\u5f15\u7528\uff09\u3002 \u5f15\u7528\u4e00\u65e6\u521d\u59cb\u5316\uff0c\u5b83\u4fbf\u4e0d\u80fd\u591f\u88ab\u91cd\u7f6e\uff0c\u4e5f\u5c31\u662f\u8bf4\u4e0d\u80fd\u4fee\u6539\u5b83\u53bb\u5f15\u7528\u522b\u7684\u5bf9\u8c61\u3002 \u5f53\u5982\u679c\u5bf9\u8c61\u5148\u4e8e\u5b83\u7684\u5f15\u7528\u88ab\u9500\u6bc1\uff0c\u5219\u8be5\u5f15\u7528\u4f1a\u6307\u5411\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u5bf9\u8c61\uff0c\u6b64\u65f6\u8be5\u5f15\u7528\u88ab\u79f0\u4e3a \u60ac\u5782 \u5f15\u7528\u3002\u8bbf\u95ee\u60ac\u5782\u5f15\u7528\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 \u5f53\u4f7f\u7528 const \u5173\u952e\u5b57\u6765\u4fee\u9970\u4e00\u4e2a\u5de6\u503c\u5f15\u7528\u65f6\uff0c\u5373\u8981\u6c42\u8be5\u5f15\u7528\u5c06\u5b83\u6240\u5f15\u7528\u7684\u5bf9\u8c61\u770b\u505a\u5e38\u91cf\u3002\u6b64\u65f6\u6210\u8be5\u5f15\u7528\u4e3a \u6307\u5411\u5e38\u91cf\u503c\u7684\u5de6\u503c\u5f15\u7528 \uff08\u7b80\u79f0\u6307\u5411\u5e38\u91cf\u7684\u5f15\u7528\u6216\u5e38\u91cf\u5f15\u7528\uff09\u3002 \u5e38\u91cf\u5f15\u7528 \u53ef\u4ee5\u88ab\u7ed1\u5b9a\u5230\u53ef\u4ee5\u4fee\u6539\u5de6\u503c\uff0c\u4e0d\u53ef\u4fee\u6539\u5de6\u503c\u548c\u53f3\u503c\u3002 \u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff08\u6709\u65f6\u79f0\u4e3a\u672a\u547d\u540d\u5bf9\u8c61\u6216\u533f\u540d\u5bf9\u8c61\uff09\u662f\u6307\u5728\u4e00\u4e2a\u5355\u4e00\u8868\u8fbe\u5f0f\u4e2d\u4e3a\u4e86\u4e34\u65f6\u4f7f\u7528\u800c\u521b\u5efa\u7684\u5bf9\u8c61\uff08\u4f7f\u7528\u540e\u9500\u6bc1\uff09\u3002 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 \u5f53\u8fdb\u884c \u6309\u5f15\u7528\u4f20\u9012 \u65f6\uff0c\u6211\u4eec\u5c06\u51fd\u6570\u7684 \u5f62\u53c2 \u58f0\u660e\u4e3a\u5f15\u7528\u6216\u5e38\u91cf\u5f15\u7528\u800c\u4e0d\u662f\u666e\u901a\u7684\u53d8\u91cf\u3002\u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u5f62\u53c2\u4e2d\u7684\u5f15\u7528\u4f1a\u7ed1\u5b9a\u5230 \u5b9e\u53c2 \u3002\u56e0\u4e3a\u5f15\u7528\u53ea\u662f\u5bf9\u8c61\u7684\u522b\u540d\uff0c\u6240\u4ee5\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5e76\u4e0d\u4f1a\u521b\u5efa\u5b9e\u53c2\u7684\u62f7\u8d1d\u3002 \u53d6\u5730\u5740\u8fd0\u7b97\u7b26 (&) \u4f1a\u8fd4\u56de\u5176\u64cd\u4f5c\u6570\u7684\u5730\u5740\u3002\u800c \u89e3\u5f15\u7528(*) \u8fd0\u7b97\u7b26\u5219\u4f1a\u8fd4\u56de\u7ed9\u5b9a\u5730\u5740\u5b58\u653e\u7684\u5de6\u503c\u3002 9.6 - \u6307\u9488\u7b80\u4ecb \u6307\u9488\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5b83 \u6301\u6709 \u4e00\u4e2a\u5185\u5b58\u5730\u5740\uff08\u901a\u5e38\u662f\u53e6\u5916\u4e00\u4e2a\u53d8\u91cf\u7684\u5730\u5740\uff09\u4f5c\u4e3a\u5176\u503c\u3002\u4f7f\u7528\u6307\u9488\u6211\u4eec\u53ef\u4ee5\u5c06\u67d0\u4e2a\u5bf9\u8c61\u7684\u5730\u5740\u4fdd\u5b58\u8d77\u6765\u4ee5\u4fbf\u7a0d\u540e\u4f7f\u7528\u3002\u548c\u4e0d\u540c\u7684\u53d8\u91cf\u4e00\u6837\uff0c\u6307\u9488\u4e0d\u4f1a\u88ab\u9ed8\u8ba4\u521d\u59cb\u5316\u3002\u800c\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u7684\u6307\u9488\u901a\u5e38\u88ab\u79f0\u4e3a \u91ce\u6307\u9488 \u3002 \u60ac\u5782 \u6307\u9488\u5219\u662f\u90a3\u4e9b\u4fdd\u5b58\u7740\u65e0\u6548\u5730\u5740\u7684\u6307\u9488\uff08\u8be5\u5730\u5740\u4e2d\u7684\u5bf9\u8c61\u5df2\u7ecf\u88ab\u9500\u6bc1\uff09\u3002 9.7 - \u7a7a\u6307\u9488 \u9664\u4e86\u5185\u5b58\u5730\u5740\uff0c\u6307\u9488\u8fd8\u53ef\u4ee5\u4fdd\u5b58\u4e00\u4e2a\u7279\u6b8a\u503c\uff0c\u5373\u7a7a\u503c\uff08null\uff09\u3002null\u662f\u4e00\u4e2a\u7279\u6b8a\u503c\u5b83\u8868\u793a\u7a7a\u503c\u3002\u5f53\u6307\u9488\u7684\u503c\u4e3anull\u65f6\uff0c\u8868\u793a\u5b83\u6ca1\u6709\u6307\u5411\u4efb\u4f55\u5730\u5740\u3002\u6b64\u65f6\u8be5\u6307\u9488\u88ab\u79f0\u4e3a\u7a7a\u6307\u9488\u3002\u5173\u952e\u5b57 nullptr \u662f\u7a7a\u6307\u9488\u7684\u5b57\u9762\u91cf\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 nullptr \u6765\u663e\u5f0f\u5730\u4e3a\u6307\u9488\u521d\u59cb\u5316\u6216\u8d4b\u503c\u4e3a\u7a7a\u503c\u3002 \u6307\u9488\u5e94\u5f53\u6301\u6709\u4e00\u4e2a\u5305\u542b\u6709\u6548\u5bf9\u8c61\u7684\u5730\u5740\uff0c\u6216\u8005\u8bbe\u7f6e\u4e3a nullptr \u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6211\u4eec\u53ea\u9700\u8981\u5224\u65ad\u6307\u9488\u662f\u4e0d\u662f\u7a7a\u5373\u53ef\uff0c\u5426\u5219\u6211\u4eec\u5c31\u8ba4\u4e3a\u5b83\u662f\u6709\u6548\u7684\u3002 9.8 - \u6307\u9488\u548cconst \u6307\u9488\u5982\u679c\u6307\u5411\u4e00\u4e2aconst\u5e38\u91cf\uff08\u6709\u65f6\u7b80\u79f0\u4e3a\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488\uff09\uff0c\u5219\u8868\u793a\u4e00\u4e2a\uff08\u975e\u5e38\u91cf\uff09\u6307\u9488\u6307\u5411\u4e00\u4e2a\u5e38\u91cf\u3002 \u800c\u4e00\u4e2a\u5e38\u91cf\u6307\u9488\uff0c\u5219\u662f\u5728\u5176\u6301\u6709\u5730\u5740\u88ab\u521d\u59cb\u540e\uff0c\u4e0d\u80fd\u518d\u6539\u53d8\u7684\u6307\u9488\u3002 \u6307\u5411\u5e38\u91cf\u7684\u5e38\u91cf\u6307\u9488\uff0c\u5176\u6307\u9488\u672c\u8eab\u6240\u6301\u6709\u7684\u5730\u5740\u4e0d\u80fd\u6539\u53d8\uff0c\u4e5f\u4e0d\u80fd\u4fee\u6539\u8be5\u5730\u5740\u6240\u6307\u5411\u7684\u5bf9\u8c61\u3002 9.9 - \u6309\u5730\u5740\u4f20\u9012 \u5bf9\u4e8e \u4f20\u5730\u5740 \u6765\u8bf4\uff0c\u8c03\u7528\u8005\u4f1a\u4f7f\u7528\u5bf9\u8c61\u7684\u5730\u5740\uff08\u901a\u8fc7\u6307\u9488\uff09\u800c\u4e0d\u662f\u5bf9\u8c61\u672c\u8eab\u4f5c\u4e3a \u5b9e\u53c2 \u3002\u8be5\u6307\u9488\uff08\u6301\u6709\u8be5\u5bf9\u8c61\u7684\u5730\u5740\uff09\u4f1a\u88ab\u62f7\u8d1d\u5230\u88ab\u8c03\u51fd\u6570\u7684\u6307\u9488\u53c2\u6570\uff08 \u5f62\u53c2 \uff09\uff0c\u8be5\u53c2\u6570\u6b64\u65f6\u4fdd\u5b58\u7740\u8be5\u5bf9\u8c61\u6240\u5728\u5730\u5740\uff08\u7684\u4e00\u4efd\u62f7\u8d1d\uff09\u3002\u968f\u540e\uff0c\u51fd\u6570\u53ef\u4ee5\u5bf9\u8be5\u6307\u9488\u8fdb\u884c \u89e3\u5f15\u7528 \u4ee5\u8bbf\u95ee\u8be5\u5bf9\u8c61\u3002 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 \u6309\u5f15\u7528\u8fd4\u56de \u65f6\uff0c\u51fd\u6570\u8fd4\u56de\u7ed1\u5b9a\u5230\u8fd4\u56de\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4e0d\u4f1a\u5bf9\u8fd4\u56de\u503c\u8fdb\u884c\u62f7\u8d1d\u3002\u5728\u8fd4\u56de\u5f15\u7528\u65f6\uff0c\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\uff1a\u8be5\u5f15\u7528\u6240\u7ed1\u5b9a\u7684\u5bf9\u8c61\u5176\u751f\u547d\u5468\u671f\u8981\u6bd4\u51fd\u6570\u957f\u3002\u5426\u5219\u8fd4\u56de\u7684\u5f15\u7528\u5c06\u53d8\u6210 \u60ac\u5782 \u5f15\u7528\uff08\u5f15\u7528\u7684\u5bf9\u8c61\u88ab\u9500\u6bc1\u4e86\uff09\uff0c\u6b64\u65f6\u7ee7\u7eed\u4f7f\u7528\u8be5\u5f15\u7528\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 \u5982\u679c\u5b9e\u53c2\u901a\u8fc7\u5f15\u7528\u7684\u65b9\u5f0f\u88ab\u4f20\u5165\uff0c\u90a3\u4e48\u4f7f\u7528\u6309\u5f15\u7528\u8fd4\u56de\u8be5\u53c2\u6570\u4e5f\u662f\u66f4\u5b89\u5168\u7684 \u3002 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de \u5982\u679c\u4e00\u4e2a\u51fd\u6570\u8fd4\u56de\u5f15\u7528\uff0c\u4e14\u8be5\u5f15\u7528\u88ab\u7528\u6765\u521d\u59cb\u5316\u6216\u8d4b\u503c\u7ed9\u4e00\u4e2a\u975e\u5f15\u7528\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u90a3\u4e48\u8be5\u8fd4\u56de\u503c\u4f1a\u88ab\u62f7\u8d1d\uff08\u548c \u6309\u503c\u8fd4\u56de \u7684\u6548\u679c\u662f\u4e00\u6837\u7684\uff09\u3002 \u6309\u5730\u5740\u8fd4\u56de \u548c \u6309\u5f15\u7528\u8fd4\u56de \u7684\u5de5\u4f5c\u539f\u7406\u51e0\u4e4e\u662f\u4e00\u6837\u7684\uff0c\u533a\u522b\u53ea\u5728\u4e8e\u524d\u8005\u8fd4\u56de\u7684\u662f\u6307\u5411\u5bf9\u8c61\u7684\u6307\u9488\u800c\u540e\u8005\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5bf9\u8c61\u7684\u5f15\u7528\u3002 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad \u4f7f\u7528 auto \u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\u65f6\uff0c\u5b83\u4f1a\u79fb\u9664\u5f15\u7528\u6807\u8bb0\u6216\u5176\u4ed6\u9876\u5c42\u5e38\u91cf\u4fee\u9970\u7b26\u3002\u5982\u679c\uff0c\u4f60\u4ecd\u9700\u8981\u8fd9\u4e9b\u6807\u8bc6\u7b26\uff0c\u5219\u9700\u8981\u5c06\u5176\u4f5c\u4e3a\u53d8\u91cf\u58f0\u660e\u7684\u4e00\u90e8\u5206\u8fdb\u884c\u6dfb\u52a0\u3002","title":"\u590d\u4e60"},{"location":"A-1-static-and-dynamic-libraries/","text":"title: A.1 - \u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93 alias: A.1 - \u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93 origin: /a1-static-and-dynamic-libraries/ origin_title: \"A.1 \u2014 Static and dynamic libraries\" time: 2022-9-13 type: translation Key Takeaway \u5e93 \u662f\u4e00\u4e2a\u4ee3\u7801\u5305\uff0c\u5b83\u53ef\u4ee5\u88ab\u8bb8\u591a\u7a0b\u5e8f\u91cd\u7528\u3002\u901a\u5e38\uff0cC++\u5e93\u5206\u4e3a\u4e24\u90e8\u5206\uff1a \u5b9a\u4e49\u516c\u5f00\u63a5\u53e3(\u63d0\u4f9b\u529f\u80fd)\u7684\u5934\u6587\u4ef6\u3002 \u4e00\u79cd\u9884\u7f16\u8bd1\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u9884\u7f16\u8bd1\u6210\u673a\u5668\u8bed\u8a00\u7684\u529f\u80fd\u7684\u5b9e\u73b0\u3002 \u6709\u4e9b\u5e93\u53ef\u80fd\u88ab\u62c6\u5206\u4e3a\u591a\u4e2a\u6587\u4ef6\u548c/\u6216\u5177\u6709\u591a\u4e2a\u5934\u6587\u4ef6\u3002 \u5e93\u88ab\u9884\u7f16\u8bd1\u6709\u51e0\u4e2a\u539f\u56e0\u3002\u9996\u5148\uff0c\u7531\u4e8e\u5e93\u5f88\u5c11\u66f4\u6539\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u7ecf\u5e38\u91cd\u65b0\u7f16\u8bd1\u3002\u6bcf\u6b21\u7f16\u5199\u4f7f\u7528\u5e93\u7684\u7a0b\u5e8f\u65f6\u91cd\u65b0\u7f16\u8bd1\u5e93\u662f\u6d6a\u8d39\u65f6\u95f4\u7684\u3002\u5176\u6b21\uff0c\u56e0\u4e3a\u9884\u7f16\u8bd1\u5bf9\u8c61\u662f\u7528\u673a\u5668\u8bed\u8a00\u7f16\u5199\u7684\uff0c\u6240\u4ee5\u5b83\u963b\u6b62\u4e86\u4eba\u4eec\u8bbf\u95ee\u6216\u66f4\u6539\u6e90\u4ee3\u7801\uff0c\u8fd9\u5bf9\u4e8e\u4f01\u4e1a\u6216\u56e0\u4e3a\u77e5\u8bc6\u4ea7\u6743\u539f\u56e0\u4e0d\u60f3\u8ba9\u6e90\u4ee3\u7801\u53ef\u7528\u7684\u4eba\u6765\u8bf4\u5f88\u91cd\u8981\u3002 \u5e93\u53ef\u4ee5\u5206\u4e3a\u4e24\u7c7b\uff1a\u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93 \u9759\u6001\u5e93 (\u4e5f\u79f0\u4e3a \u5b58\u6863 archive )\u7531\u7f16\u8bd1\u540e\u76f4\u63a5\u94fe\u63a5\u5230\u7a0b\u5e8f\u4e2d\u7a0b\u5e8f\u7ec4\u6210\u3002\u5f53\u7f16\u8bd1\u4f7f\u7528\u9759\u6001\u5e93\u7684\u7a0b\u5e8f\u65f6\uff0c\u7a0b\u5e8f\u4f7f\u7528\u7684\u9759\u6001\u5e93\u7684\u6240\u6709\u529f\u80fd\u90fd\u6210\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u7684\u4e00\u90e8\u5206\u3002\u5728Windows\u4e0a\uff0c\u9759\u6001\u5e93\u901a\u5e38\u6709\u4e00\u4e2a .lib \u6269\u5c55\u540d\uff0c\u800c\u5728Linux\u4e0a\uff0c\u9759\u6001\u5e93\u901a\u5e38\u6709\u4e00\u4e2a .a (\u5b58\u6863)\u6269\u5c55\u540d\u3002\u9759\u6001\u5e93\u7684\u4e00\u4e2a\u4f18\u70b9\u662f\uff0c\u8ba9\u7528\u6237\u8fd0\u884c\u4f60\u7684\u7a0b\u5e8f\uff0c\u53ea\u9700\u5206\u53d1\u53ef\u6267\u884c\u6587\u4ef6\u3002\u56e0\u4e3a\u5e93\u6210\u4e3a\u4e86\u7a0b\u5e8f\u7684\u4e00\u90e8\u5206\uff0c\u8fd9\u5c31\u786e\u4fdd\u4e86\u7a0b\u5e8f\u603b\u662f\u4f7f\u7528\u6b63\u786e\u7248\u672c\u7684\u5e93\u3002\u53e6\u5916\uff0c\u7531\u4e8e\u9759\u6001\u5e93\u6210\u4e3a\u7a0b\u5e8f\u7684\u4e00\u90e8\u5206\uff0c\u4f60\u53ef\u4ee5\u50cf\u4e3a\u81ea\u5df1\u7684\u7a0b\u5e8f\u7f16\u5199\u7684\u529f\u80fd\u4e00\u6837\u4f7f\u7528\u5b83\u4eec\u3002\u7f3a\u70b9\u662f\uff0c\u7531\u4e8e\u5e93\u7684\u526f\u672c\u6210\u4e3a\u6bcf\u4e2a\u4f7f\u7528\u5b83\u7684\u53ef\u6267\u884c\u6587\u4ef6\u7684\u4e00\u90e8\u5206\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u5927\u91cf\u7684\u7a7a\u95f4\u6d6a\u8d39\u3002\u9759\u6001\u5e93\u4e5f\u4e0d\u5bb9\u6613\u5347\u7ea7\u2014\u2014\u8981\u66f4\u65b0\u5e93\uff0c\u9700\u8981\u66ff\u6362\u6574\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u3002 \u52a8\u6001\u5e93(\u4e5f\u79f0\u4e3a \u5171\u4eab\u5e93 shared library )\u7531\u5728\u8fd0\u884c\u65f6\u52a0\u8f7d\u5230\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u7a0b\u5e8f\u7ec4\u6210\u3002\u5f53\u7f16\u8bd1\u4e00\u4e2a\u4f7f\u7528\u52a8\u6001\u5e93\u7684\u7a0b\u5e8f\u65f6\uff0c\u5e93\u4e0d\u4f1a\u6210\u4e3a\u53ef\u6267\u884c\u7a0b\u5e8f\u7684\u4e00\u90e8\u5206\u2014\u2014\u5b83\u4ecd\u7136\u662f\u4e00\u4e2a\u72ec\u7acb\u7684\u5355\u5143\u3002\u5728Windows\u4e0a\uff0c\u52a8\u6001\u5e93\u901a\u5e38\u5177\u6709 .dll (\u52a8\u6001\u94fe\u63a5\u5e93)\u6269\u5c55\u540d\uff0c\u800c\u5728Linux\u4e0a\uff0c\u52a8\u6001\u5e93\u901a\u5e38\u5177\u6709 .so (\u5171\u4eab\u5bf9\u8c61)\u6269\u5c55\u540d\u3002\u52a8\u6001\u5e93\u7684\u4e00\u4e2a\u4f18\u70b9\u662f\u8bb8\u591a\u7a0b\u5e8f\u53ef\u4ee5\u5171\u4eab\u4e00\u4e2a\u526f\u672c\uff0c\u8fd9\u8282\u7701\u4e86\u7a7a\u95f4\u3002\u4e5f\u8bb8\u66f4\u5927\u7684\u4f18\u70b9\u662f\u52a8\u6001\u5e93\u53ef\u4ee5\u5347\u7ea7\u5230\u65b0\u7248\u672c\uff0c\u800c\u4e0d\u9700\u8981\u66ff\u6362\u4f7f\u7528\u5b83\u7684\u6240\u6709\u53ef\u6267\u884c\u7a0b\u5e8f\u3002 \u56e0\u4e3a\u52a8\u6001\u5e93\u6ca1\u6709\u94fe\u63a5\u5230\u7a0b\u5e8f\u4e2d\uff0c\u6240\u4ee5\u4f7f\u7528\u52a8\u6001\u5e93\u7684\u7a0b\u5e8f\u5fc5\u987b\u663e\u5f0f\u52a0\u8f7d\u52a8\u6001\u5e93\u5e76\u4e0e\u4e4b\u8fde\u63a5\u3002\u8fd9\u79cd\u673a\u5236\u53ef\u80fd\u4ee4\u4eba\u56f0\u60d1\uff0c\u5e76\u4f7f\u4e0e\u52a8\u6001\u5e93\u7684\u63a5\u53e3\u53d8\u5f97\u5c34\u5c2c\u3002\u4e3a\u4e86\u4f7f\u52a8\u6001\u5e93\u66f4\u5bb9\u6613\u4f7f\u7528\uff0c\u53ef\u4ee5\u4f7f\u7528\u5bfc\u5165\u5e93\u3002 \u5bfc\u5165\u5e93 \u662f\u4e00\u4e2a\u81ea\u52a8\u52a0\u8f7d\u548c\u4f7f\u7528 \u52a8\u6001\u5e93 \u7684\u5e93\u3002\u5728Windows\u4e0a\uff0c\u8fd9\u901a\u5e38\u662f\u901a\u8fc7\u4e0e\u52a8\u6001\u5e93( .dll )\u540c\u540d\u7684\u5c0f\u578b\u9759\u6001\u5e93( .lib )\u6765\u5b8c\u6210\u7684\u3002\u5728\u7f16\u8bd1\u65f6\u5c06\u9759\u6001\u5e93\u94fe\u63a5\u5230\u7a0b\u5e8f\u4e2d\uff0c\u7136\u540e\u5c31\u53ef\u4ee5\u50cf\u4f7f\u7528\u9759\u6001\u5e93\u4e00\u6837\u6709\u6548\u5730\u4f7f\u7528\u52a8\u6001\u5e93\u7684\u529f\u80fd\u3002\u5728Linux\u4e0a\uff0c\u5171\u4eab\u5bf9\u8c61( .so )\u6587\u4ef6\u65e2\u662f\u52a8\u6001\u5e93\u53c8\u662f\u5bfc\u5165\u5e93\u3002 \u5927\u591a\u6570\u94fe\u63a5\u5668\u53ef\u4ee5\u5728\u521b\u5efa\u52a8\u6001\u5e93\u65f6\u4e3a\u52a8\u6001\u5e93\u6784\u5efa\u5bfc\u5165\u5e93 \u3002 \u5b89\u88c5\u548c\u4f7f\u7528\u5e93 \u73b0\u5728\u4f60\u5df2\u7ecf\u4e86\u89e3\u4e86\u4e0d\u540c\u7c7b\u578b\u7684\u5e93\uff0c\u8ba9\u6211\u4eec\u8ba8\u8bba\u4e00\u4e0b\u5982\u4f55\u5728\u7a0b\u5e8f\u4e2d\u5b9e\u9645\u4f7f\u7528\u5e93\u3002\u5728C++\u4e2d\u5b89\u88c5\u5e93\u901a\u5e38\u5305\u62ec4\u4e2a\u6b65\u9aa4\uff1a \u83b7\u5f97\u5e93\u6700\u597d\u7684\u65b9\u5f0f\u662f\u4e3a\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u8f7d\u9884\u7f16\u8bd1\u7684\u5305(\u5982\u679c\u5b83\u5b58\u5728\u7684\u8bdd)\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u4e0d\u5fc5\u81ea\u5df1\u7f16\u8bd1\u5e93\u3002\u5982\u679c\u6ca1\u6709\uff0c\u90a3\u4e48\u6211\u4eec\u5c06\u4e0d\u5f97\u4e0d\u4e0b\u8f7d\u4e00\u4e2a\u6e90\u4ee3\u7801\u5305\u5e76\u81ea\u5df1\u7f16\u8bd1\u5b83(\u8fd9\u8d85\u51fa\u4e86\u672c\u8bfe\u7684\u8303\u56f4)\u3002\u5728Windows\u4e0a\uff0c\u5e93\u901a\u5e38\u4ee5 .zip \u6587\u4ef6\u7684\u5f62\u5f0f\u5206\u53d1\u3002\u5728Linux\u4e0a\uff0c\u5e93\u901a\u5e38\u4ee5\u5305\u7684\u5f62\u5f0f\u5206\u53d1(\u4f8b\u5982 . rpm )\u3002\u5305\u7ba1\u7406\u5668\u53ef\u80fd\u5df2\u7ecf\u5217\u51fa\u4e86\u4e00\u4e9b\u66f4\u6d41\u884c\u7684\u5e93(\u4f8b\u5982SDL)\uff0c\u4ee5\u4fbf\u4e8e\u5b89\u88c5\uff0c\u6240\u4ee5\u8bf7\u5148\u68c0\u67e5\u8fd9\u4e9b\u5e93\u3002 \u5b89\u88c5\u5e93\u3002\u5728Linux\u4e0a\uff0c\u901a\u5e38\u4f7f\u7528\u5305\u7ba1\u7406\u5668\u5b8c\u6210\u6240\u6709\u5de5\u4f5c\u3002\u5728Windows\u4e0a\uff0c\u8fd9\u901a\u5e38\u6d89\u53ca\u5c06\u5e93\u89e3\u538b\u7f29\u5230\u4f60\u6240\u9009\u62e9\u7684\u76ee\u5f55\u3002\u6211\u4eec\u5efa\u8bae\u5c06\u6240\u6709\u7684\u5e93\u653e\u5728\u4e00\u4e2a\u4f4d\u7f6e\u4ee5\u4fbf\u4e8e\u8bbf\u95ee\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u4e00\u4e2a\u540d\u4e3a C:\\Libs \u7684\u76ee\u5f55\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5e93\u653e\u5728\u5b83\u81ea\u5df1\u7684\u5b50\u76ee\u5f55\u4e2d\u3002 \u786e\u4fdd\u7f16\u8bd1\u5668\u77e5\u9053\u5728\u54ea\u91cc\u67e5\u627e\u5e93\u7684\u5934\u6587\u4ef6\u3002\u5728Windows\u4e0a\uff0c\u901a\u5e38\u8fd9\u662f\u4f60\u5b89\u88c5\u5e93\u6587\u4ef6\u7684\u76ee\u5f55\u7684include\u5b50\u76ee\u5f55(\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u628a\u5e93\u5b89\u88c5\u5230 C:\\libs\\SDL-1.2.11 \uff0c\u5934\u6587\u4ef6\u53ef\u80fd\u5728 C:\\libs\\SDL-1.2.11\\include )\u3002\u5728Linux\u4e0a\uff0c\u5934\u6587\u4ef6\u901a\u5e38\u5b89\u88c5\u5230 /usr/include \uff0c\u5b83\u5e94\u8be5\u5df2\u7ecf\u662f\u5305\u542b\u6587\u4ef6\u641c\u7d22\u8def\u5f84\u7684\u4e00\u90e8\u5206\u3002\u4f46\u662f\uff0c\u5982\u679c\u6587\u4ef6\u5b89\u88c5\u5728\u5176\u4ed6\u5730\u65b9\uff0c\u5219\u5fc5\u987b\u544a\u8bc9\u7f16\u8bd1\u5668\u5728\u54ea\u91cc\u627e\u5230\u5b83\u4eec\u3002 \u544a\u8bc9\u94fe\u63a5\u5668\u5728\u54ea\u91cc\u67e5\u627e\u5e93\u6587\u4ef6\u3002\u4e0e\u6b65\u9aa43\u4e00\u6837\uff0c\u8fd9\u901a\u5e38\u6d89\u53ca\u5230\u5411\u94fe\u63a5\u5668\u67e5\u627e\u5e93\u7684\u4f4d\u7f6e\u5217\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u76ee\u5f55\u3002\u5728Windows\u4e0a\uff0c\u8fd9\u901a\u5e38\u662f\u60a8\u5b89\u88c5\u5e93\u6587\u4ef6\u7684\u76ee\u5f55\u7684 /lib \u5b50\u76ee\u5f55\u3002\u5728Linux\u4e0a\uff0c\u5e93\u901a\u5e38\u5b89\u88c5\u5230 /usr/lib \uff0c\u5b83\u5e94\u8be5\u5df2\u7ecf\u662f\u5e93\u641c\u7d22\u8def\u5f84\u7684\u4e00\u90e8\u5206\u3002 \u4e00\u65e6\u5b89\u88c5\u4e86\u5e93\uff0c\u5e76\u4e14IDE\u77e5\u9053\u5728\u54ea\u91cc\u67e5\u627e\u5b83\u4eec\u540e\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u60f3\u8981\u4f7f\u7528\u5e93\u7684\u9879\u76ee\uff0c\u901a\u5e38\u8fd8\u9700\u8981\u6267\u884c\u4ee5\u4e0b3\u4e2a\u6b65\u9aa4: \u5982\u679c\u4f7f\u7528\u9759\u6001\u5e93\u6216\u5bfc\u5165\u5e93\uff0c\u8bf7\u544a\u8bc9\u94fe\u63a5\u5668\u8981\u94fe\u63a5\u54ea\u4e9b\u5e93\u6587\u4ef6\u3002 \u4f7f\u7528 #include \u5728\u4f60\u7684\u7a0b\u5e8f\u4e2d\u5305\u542b\u5e93\u7684\u5934\u6587\u4ef6\u3002\u8fd9\u53ef\u4ee5\u544a\u8bc9\u7f16\u8bd1\u5668\u8be5\u5e93\u63d0\u4f9b\u7684\u529f\u80fd\u6709\u54ea\u4e9b\u3002 \u5982\u679c\u4f7f\u7528\u52a8\u6001\u5e93\uff0c\u8bf7\u786e\u4fdd\u7a0b\u5e8f\u77e5\u9053\u5728\u54ea\u91cc\u53ef\u4ee5\u627e\u5230\u5b83\u4eec\u3002\u5728Linux\u4e0b\uff0c\u5e93\u901a\u5e38\u5b89\u88c5\u5230 /usr/lib \uff0c\u5b83\u4f4d\u4e8e LD_LIBRARY_PATH \u73af\u5883\u53d8\u91cf\u7684\u9ed8\u8ba4\u641c\u7d22\u8def\u5f84\u4e2d\u3002\u5728Windows\u4e0a\uff0c\u9ed8\u8ba4\u641c\u7d22\u8def\u5f84\u5305\u62ec\u8fd0\u884c\u7a0b\u5e8f\u7684\u76ee\u5f55\u3001\u8c03\u7528 SetDllDirectory() \u8bbe\u7f6e\u7684\u76ee\u5f55\u3001Windows\u3001System\u548cSystem32\u76ee\u5f55\u4ee5\u53capath\u73af\u5883\u53d8\u91cf\u4e2d\u7684\u76ee\u5f55\u3002\u4f7f\u7528 .dll \u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u5c06 .dll \u590d\u5236\u5230\u53ef\u6267\u884c\u6587\u4ef6\u7684\u4f4d\u7f6e\u3002\u7531\u4e8e\u4f60\u901a\u5e38\u4f1a\u5c06 .dll \u4e0e\u53ef\u6267\u884c\u6587\u4ef6\u4e00\u8d77\u5206\u53d1\uff0c\u56e0\u6b64\u5c06\u5b83\u4eec\u653e\u5728\u4e00\u8d77\u662f\u6709\u610f\u4e49\u7684\u3002 \u6b65\u9aa43-5\u6d89\u53ca\u5230\u914d\u7f6eIDE\u2014\u2014\u5e78\u8fd0\u7684\u662f\uff0c\u51e0\u4e4e\u6240\u6709IDE\u5728\u505a\u8fd9\u4e9b\u4e8b\u60c5\u65f6\u90fd\u4ee5\u76f8\u540c\u7684\u65b9\u5f0f\u5de5\u4f5c\u3002\u4e0d\u5e78\u7684\u662f\uff0c\u56e0\u4e3a\u6bcf\u4e2aIDE\u90fd\u6709\u4e0d\u540c\u7684\u63a5\u53e3\uff0c\u6240\u4ee5\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u6700\u56f0\u96be\u7684\u90e8\u5206\u4ec5\u4ec5\u662f\u627e\u5230\u6267\u884c\u8fd9\u4e9b\u6b65\u9aa4\u7684\u5408\u9002\u4f4d\u7f6e\u3002\u56e0\u6b64\uff0c\u5728\u672c\u8282\u63a5\u4e0b\u6765\u7684\u51e0\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4e3a Visual Studio Express 2005 \u548c Code::Blocks \u6267\u884c\u6240\u6709\u8fd9\u4e9b\u6b65\u9aa4\u3002\u5982\u679c\u4f60\u6b63\u5728\u4f7f\u7528\u53e6\u4e00\u79cdIDE\uff0c\u8bf7\u540c\u65f6\u9605\u8bfb\u8fd9\u4e24\u79cdIDE\u7684\u4f7f\u7528\u65b9\u5f0f\u2014\u2014\u8bfb\u5b8c\u4e4b\u540e\u4f60\u5e94\u8be5\u6709\u8db3\u591f\u7684\u4fe1\u606f\u6765\u4f7f\u7528\u81ea\u5df1\u7684IDE\u8fdb\u884c\u76f8\u540c\u7684\u64cd\u4f5c\uff0c\u53ea\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u641c\u7d22\u3002","title":"A 1 static and dynamic libraries"},{"location":"A-1-static-and-dynamic-libraries/#_1","text":"\u73b0\u5728\u4f60\u5df2\u7ecf\u4e86\u89e3\u4e86\u4e0d\u540c\u7c7b\u578b\u7684\u5e93\uff0c\u8ba9\u6211\u4eec\u8ba8\u8bba\u4e00\u4e0b\u5982\u4f55\u5728\u7a0b\u5e8f\u4e2d\u5b9e\u9645\u4f7f\u7528\u5e93\u3002\u5728C++\u4e2d\u5b89\u88c5\u5e93\u901a\u5e38\u5305\u62ec4\u4e2a\u6b65\u9aa4\uff1a \u83b7\u5f97\u5e93\u6700\u597d\u7684\u65b9\u5f0f\u662f\u4e3a\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u8f7d\u9884\u7f16\u8bd1\u7684\u5305(\u5982\u679c\u5b83\u5b58\u5728\u7684\u8bdd)\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u4e0d\u5fc5\u81ea\u5df1\u7f16\u8bd1\u5e93\u3002\u5982\u679c\u6ca1\u6709\uff0c\u90a3\u4e48\u6211\u4eec\u5c06\u4e0d\u5f97\u4e0d\u4e0b\u8f7d\u4e00\u4e2a\u6e90\u4ee3\u7801\u5305\u5e76\u81ea\u5df1\u7f16\u8bd1\u5b83(\u8fd9\u8d85\u51fa\u4e86\u672c\u8bfe\u7684\u8303\u56f4)\u3002\u5728Windows\u4e0a\uff0c\u5e93\u901a\u5e38\u4ee5 .zip \u6587\u4ef6\u7684\u5f62\u5f0f\u5206\u53d1\u3002\u5728Linux\u4e0a\uff0c\u5e93\u901a\u5e38\u4ee5\u5305\u7684\u5f62\u5f0f\u5206\u53d1(\u4f8b\u5982 . rpm )\u3002\u5305\u7ba1\u7406\u5668\u53ef\u80fd\u5df2\u7ecf\u5217\u51fa\u4e86\u4e00\u4e9b\u66f4\u6d41\u884c\u7684\u5e93(\u4f8b\u5982SDL)\uff0c\u4ee5\u4fbf\u4e8e\u5b89\u88c5\uff0c\u6240\u4ee5\u8bf7\u5148\u68c0\u67e5\u8fd9\u4e9b\u5e93\u3002 \u5b89\u88c5\u5e93\u3002\u5728Linux\u4e0a\uff0c\u901a\u5e38\u4f7f\u7528\u5305\u7ba1\u7406\u5668\u5b8c\u6210\u6240\u6709\u5de5\u4f5c\u3002\u5728Windows\u4e0a\uff0c\u8fd9\u901a\u5e38\u6d89\u53ca\u5c06\u5e93\u89e3\u538b\u7f29\u5230\u4f60\u6240\u9009\u62e9\u7684\u76ee\u5f55\u3002\u6211\u4eec\u5efa\u8bae\u5c06\u6240\u6709\u7684\u5e93\u653e\u5728\u4e00\u4e2a\u4f4d\u7f6e\u4ee5\u4fbf\u4e8e\u8bbf\u95ee\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u4e00\u4e2a\u540d\u4e3a C:\\Libs \u7684\u76ee\u5f55\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5e93\u653e\u5728\u5b83\u81ea\u5df1\u7684\u5b50\u76ee\u5f55\u4e2d\u3002 \u786e\u4fdd\u7f16\u8bd1\u5668\u77e5\u9053\u5728\u54ea\u91cc\u67e5\u627e\u5e93\u7684\u5934\u6587\u4ef6\u3002\u5728Windows\u4e0a\uff0c\u901a\u5e38\u8fd9\u662f\u4f60\u5b89\u88c5\u5e93\u6587\u4ef6\u7684\u76ee\u5f55\u7684include\u5b50\u76ee\u5f55(\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u628a\u5e93\u5b89\u88c5\u5230 C:\\libs\\SDL-1.2.11 \uff0c\u5934\u6587\u4ef6\u53ef\u80fd\u5728 C:\\libs\\SDL-1.2.11\\include )\u3002\u5728Linux\u4e0a\uff0c\u5934\u6587\u4ef6\u901a\u5e38\u5b89\u88c5\u5230 /usr/include \uff0c\u5b83\u5e94\u8be5\u5df2\u7ecf\u662f\u5305\u542b\u6587\u4ef6\u641c\u7d22\u8def\u5f84\u7684\u4e00\u90e8\u5206\u3002\u4f46\u662f\uff0c\u5982\u679c\u6587\u4ef6\u5b89\u88c5\u5728\u5176\u4ed6\u5730\u65b9\uff0c\u5219\u5fc5\u987b\u544a\u8bc9\u7f16\u8bd1\u5668\u5728\u54ea\u91cc\u627e\u5230\u5b83\u4eec\u3002 \u544a\u8bc9\u94fe\u63a5\u5668\u5728\u54ea\u91cc\u67e5\u627e\u5e93\u6587\u4ef6\u3002\u4e0e\u6b65\u9aa43\u4e00\u6837\uff0c\u8fd9\u901a\u5e38\u6d89\u53ca\u5230\u5411\u94fe\u63a5\u5668\u67e5\u627e\u5e93\u7684\u4f4d\u7f6e\u5217\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u76ee\u5f55\u3002\u5728Windows\u4e0a\uff0c\u8fd9\u901a\u5e38\u662f\u60a8\u5b89\u88c5\u5e93\u6587\u4ef6\u7684\u76ee\u5f55\u7684 /lib \u5b50\u76ee\u5f55\u3002\u5728Linux\u4e0a\uff0c\u5e93\u901a\u5e38\u5b89\u88c5\u5230 /usr/lib \uff0c\u5b83\u5e94\u8be5\u5df2\u7ecf\u662f\u5e93\u641c\u7d22\u8def\u5f84\u7684\u4e00\u90e8\u5206\u3002 \u4e00\u65e6\u5b89\u88c5\u4e86\u5e93\uff0c\u5e76\u4e14IDE\u77e5\u9053\u5728\u54ea\u91cc\u67e5\u627e\u5b83\u4eec\u540e\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u60f3\u8981\u4f7f\u7528\u5e93\u7684\u9879\u76ee\uff0c\u901a\u5e38\u8fd8\u9700\u8981\u6267\u884c\u4ee5\u4e0b3\u4e2a\u6b65\u9aa4: \u5982\u679c\u4f7f\u7528\u9759\u6001\u5e93\u6216\u5bfc\u5165\u5e93\uff0c\u8bf7\u544a\u8bc9\u94fe\u63a5\u5668\u8981\u94fe\u63a5\u54ea\u4e9b\u5e93\u6587\u4ef6\u3002 \u4f7f\u7528 #include \u5728\u4f60\u7684\u7a0b\u5e8f\u4e2d\u5305\u542b\u5e93\u7684\u5934\u6587\u4ef6\u3002\u8fd9\u53ef\u4ee5\u544a\u8bc9\u7f16\u8bd1\u5668\u8be5\u5e93\u63d0\u4f9b\u7684\u529f\u80fd\u6709\u54ea\u4e9b\u3002 \u5982\u679c\u4f7f\u7528\u52a8\u6001\u5e93\uff0c\u8bf7\u786e\u4fdd\u7a0b\u5e8f\u77e5\u9053\u5728\u54ea\u91cc\u53ef\u4ee5\u627e\u5230\u5b83\u4eec\u3002\u5728Linux\u4e0b\uff0c\u5e93\u901a\u5e38\u5b89\u88c5\u5230 /usr/lib \uff0c\u5b83\u4f4d\u4e8e LD_LIBRARY_PATH \u73af\u5883\u53d8\u91cf\u7684\u9ed8\u8ba4\u641c\u7d22\u8def\u5f84\u4e2d\u3002\u5728Windows\u4e0a\uff0c\u9ed8\u8ba4\u641c\u7d22\u8def\u5f84\u5305\u62ec\u8fd0\u884c\u7a0b\u5e8f\u7684\u76ee\u5f55\u3001\u8c03\u7528 SetDllDirectory() \u8bbe\u7f6e\u7684\u76ee\u5f55\u3001Windows\u3001System\u548cSystem32\u76ee\u5f55\u4ee5\u53capath\u73af\u5883\u53d8\u91cf\u4e2d\u7684\u76ee\u5f55\u3002\u4f7f\u7528 .dll \u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u5c06 .dll \u590d\u5236\u5230\u53ef\u6267\u884c\u6587\u4ef6\u7684\u4f4d\u7f6e\u3002\u7531\u4e8e\u4f60\u901a\u5e38\u4f1a\u5c06 .dll \u4e0e\u53ef\u6267\u884c\u6587\u4ef6\u4e00\u8d77\u5206\u53d1\uff0c\u56e0\u6b64\u5c06\u5b83\u4eec\u653e\u5728\u4e00\u8d77\u662f\u6709\u610f\u4e49\u7684\u3002 \u6b65\u9aa43-5\u6d89\u53ca\u5230\u914d\u7f6eIDE\u2014\u2014\u5e78\u8fd0\u7684\u662f\uff0c\u51e0\u4e4e\u6240\u6709IDE\u5728\u505a\u8fd9\u4e9b\u4e8b\u60c5\u65f6\u90fd\u4ee5\u76f8\u540c\u7684\u65b9\u5f0f\u5de5\u4f5c\u3002\u4e0d\u5e78\u7684\u662f\uff0c\u56e0\u4e3a\u6bcf\u4e2aIDE\u90fd\u6709\u4e0d\u540c\u7684\u63a5\u53e3\uff0c\u6240\u4ee5\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u6700\u56f0\u96be\u7684\u90e8\u5206\u4ec5\u4ec5\u662f\u627e\u5230\u6267\u884c\u8fd9\u4e9b\u6b65\u9aa4\u7684\u5408\u9002\u4f4d\u7f6e\u3002\u56e0\u6b64\uff0c\u5728\u672c\u8282\u63a5\u4e0b\u6765\u7684\u51e0\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4e3a Visual Studio Express 2005 \u548c Code::Blocks \u6267\u884c\u6240\u6709\u8fd9\u4e9b\u6b65\u9aa4\u3002\u5982\u679c\u4f60\u6b63\u5728\u4f7f\u7528\u53e6\u4e00\u79cdIDE\uff0c\u8bf7\u540c\u65f6\u9605\u8bfb\u8fd9\u4e24\u79cdIDE\u7684\u4f7f\u7528\u65b9\u5f0f\u2014\u2014\u8bfb\u5b8c\u4e4b\u540e\u4f60\u5e94\u8be5\u6709\u8db3\u591f\u7684\u4fe1\u606f\u6765\u4f7f\u7528\u81ea\u5df1\u7684IDE\u8fdb\u884c\u76f8\u540c\u7684\u64cd\u4f5c\uff0c\u53ea\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u641c\u7d22\u3002","title":"\u5b89\u88c5\u548c\u4f7f\u7528\u5e93"},{"location":"A-2-using-libraries-with-visual-studio/","text":"Key Takeaway \u56de\u987e\u4e00\u4e0b\u4f7f\u7528\u5e93\u9700\u8981\u5b8c\u6210\u7684\u51e0\u4e2a\u6b65\u9aa4\uff1a \u5bf9\u4e8e\u6bcf\u4e2a\u5e93\uff1a \u83b7\u53d6\u5e93\u3002\u4ece\u5b98\u7f51\u6216\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u4e0b\u8f7d\u5e93\uff1b \u5b89\u88c5\u5e93\u3002\u5c06\u5e93\u89e3\u538b\u6216\u5b89\u88c5\u5230\u7279\u5b9a\u7684\u76ee\u5f55\u3002 \u5bf9\u4e8e\u6bcf\u4e2a\u9879\u76ee\uff1a \u544a\u77e5\u7f16\u8bd1\u5668\u5230\u54ea\u91cc\u5bfb\u627e\u5934\u6587\u4ef6\uff1b \u544a\u77e5\u94fe\u63a5\u5668\u5230\u54ea\u91cc\u5bfb\u627e\u5e93\u6587\u4ef6\uff1b \u544a\u77e5\u94fe\u63a5\u5668\u94fe\u63a5\u54ea\u4e2a\u9759\u6001\u5e93\u6216\u5bfc\u5165\u5e93\u6587\u4ef6\uff1b \u5728\u7a0b\u5e8f\u4e2d #include \u5934\u6587\u4ef6\uff1b \u786e\u4fdd\u7a0b\u5e8f\u77e5\u9053\u5230\u54ea\u91cc\u5bfb\u627e\u52a8\u6001\u5e93\u3002 \u6ce8\u610f\uff1a\u672c\u8bfe\u4e2d\u7684\u4f8b\u5b50\u57fa\u4e8e Visual Studio 2005 express\uff0c\u4f46\u662f\u4ece\u90a3\u65f6\u8d77\u8fd9\u4e2a\u8fc7\u7a0b\u5e76\u6ca1\u6709\u53d1\u751f\u592a\u5927\u7684\u53d8\u5316\u3002 **\u6b65\u9aa41\u548c2\u2014\u2014\u83b7\u53d6\u548c\u5b89\u88c5\u5e93 \u4e0b\u8f7d\u5e93\u5230\u672c\u5730\u7535\u8111\uff0c\u53c2\u89c1 A.1 - \u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93 \u3002 \u6b65\u9aa43\u548c4\u2014\u2014\u544a\u8bc9\u7f16\u8bd1\u5668\u5230\u54ea\u91cc\u67e5\u627e\u5934\u6587\u4ef6\u548c\u5e93\u6587\u4ef6 \u8fdb\u5165\u9879\u76ee\u83dc\u5355\uff0c\u9009\u62e9\u9879\u76ee->\u5c5e\u6027(\u5b83\u5e94\u8be5\u5728\u5e95\u90e8) \u5728\u201c\u914d\u7f6e\u201d\u4e0b\u62c9\u83dc\u5355\u4e2d\uff0c\u786e\u4fdd\u9009\u62e9\u4e86\u201c\u6240\u6709\u914d\u7f6e\u201d\u3002 \u5728\u5de6\u4fa7\u7a97\u53e3\u7a97\u683c\u4e2d\uff0c\u9009\u62e9\u201c\u914d\u7f6e\u5c5e\u6027\u201d->\u201cVC++\u76ee\u5f55\u201d\u3002 \u5728\u201cInclude Directories\u201d\u884c\uff0c\u4e3a\u5e93\u6dfb\u52a0.h\u6587\u4ef6\u7684\u8def\u5f84(\u786e\u4fdd\u8fd9\u4e0e\u524d\u9762\u7684\u6761\u76ee\u7528\u5206\u53f7\u5206\u9694)\u3002 \u5728\u201c\u5e93\u76ee\u5f55\u201d\u4e2d\uff0c\u6dfb\u52a0\u5e93\u7684 .lib \u6587\u4ef6\u7684\u8def\u5f84\u3002 \u70b9\u51fb\u201c\u786e\u5b9a\u201d\u3002 \u6b65\u9aa4 5 \u2014\u2014 \u544a\u8bc9\u94fe\u63a5\u5668\u7a0b\u5e8f\u4f7f\u7528\u54ea\u4e9b\u5e93 \u5bf9\u4e8e\u7b2c5\u6b65\uff0c\u9700\u8981\u5c06\u5e93\u4e2d\u7684 .lib \u6587\u4ef6\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u3002\u6211\u4eec\u5728\u6bcf\u4e2a\u9879\u76ee\u7684\u57fa\u7840\u4e0a\u8fd9\u6837\u505a\u3002 \u8fdb\u5165\u9879\u76ee\u83dc\u5355\uff0c\u9009\u62e9\u9879\u76ee->\u5c5e\u6027(\u5b83\u5e94\u8be5\u5728\u5e95\u90e8) \u5728\u201c\u914d\u7f6e\u201d\u4e0b\u62c9\u83dc\u5355\u4e2d\uff0c\u786e\u4fdd\u9009\u62e9\u4e86\u201c\u6240\u6709\u914d\u7f6e\u201d\u3002 \u5728\u5de6\u4fa7\u7a97\u53e3\u4e2d\uff0c\u9009\u62e9\u201cConfiguration Properties\u201d->\u201cLinker\u201d->\u201cInput\u201d\u3002 \u5c06.lib\u6587\u4ef6\u7684\u540d\u79f0\u6dfb\u52a0\u5230\u201c\u9644\u52a0\u4f9d\u8d56\u9879\u201d\u5217\u8868\u4e2d(\u7528\u5206\u53f7\u4e0e\u524d\u9762\u7684\u6761\u76ee\u5206\u9694) \u70b9\u51fb\u201c\u786e\u5b9a\u201d\u3002 **\u6b65\u9aa4 6\u548c 7 \u2014\u2014 #include \u5934\u6587\u4ef6 \u53ea\u9700\u50cf\u5f80\u5e38\u4e00\u6837\u5c06\u5e93\u4e2d\u7684\u5934\u6587\u4ef6 #include \u5230\u9879\u76ee\u4e2d\u3002","title":"A.2 - \u5728visual studio\u4e2d\u4f7f\u7528\u5e93"},{"location":"A-3-using-libraries-with-Code-Blocks/","text":"Key Takeaway \u56de\u987e\u4e00\u4e0b\u4f7f\u7528\u5e93\u9700\u8981\u5b8c\u6210\u7684\u51e0\u4e2a\u6b65\u9aa4\uff1a \u5bf9\u4e8e\u6bcf\u4e2a\u5e93\uff1a \u83b7\u53d6\u5e93\u3002\u4ece\u5b98\u7f51\u6216\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u4e0b\u8f7d\u5e93\uff1b \u5b89\u88c5\u5e93\u3002\u5c06\u5e93\u89e3\u538b\u6216\u5b89\u88c5\u5230\u7279\u5b9a\u7684\u76ee\u5f55\u3002 \u544a\u77e5\u7f16\u8bd1\u5668\u5230\u54ea\u91cc\u5bfb\u627e\u5934\u6587\u4ef6\uff1b \u544a\u77e5\u94fe\u63a5\u5668\u5230\u54ea\u91cc\u5bfb\u627e\u5e93\u6587\u4ef6\uff1b \u5bf9\u4e8e\u6bcf\u4e2a\u9879\u76ee\uff1a \u544a\u77e5\u94fe\u63a5\u5668\u94fe\u63a5\u54ea\u4e2a\u9759\u6001\u5e93\u6216\u5bfc\u5165\u5e93\u6587\u4ef6\uff1b \u5728\u7a0b\u5e8f\u4e2d #include \u5934\u6587\u4ef6\uff1b \u786e\u4fdd\u7a0b\u5e8f\u77e5\u9053\u5230\u54ea\u91cc\u5bfb\u627e\u52a8\u6001\u5e93\u3002 **\u6b65\u9aa41\u548c2\u2014\u2014\u83b7\u53d6\u548c\u5b89\u88c5\u5e93 \u4e0b\u8f7d\u5e93\u5230\u672c\u5730\u7535\u8111\uff0c\u53c2\u89c1 A.1 - \u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93 \u3002 \u6b65\u9aa43\u548c4\u2014\u2014\u544a\u8bc9\u7f16\u8bd1\u5668\u5230\u54ea\u91cc\u67e5\u627e\u5934\u6587\u4ef6\u548c\u5e93\u6587\u4ef6 \u4e0b\u9762\u7684\u64cd\u4f5c\u662f\u9488\u5bf9\u5168\u5c40\u7684\u8bbe\u7f6e\uff0c\u8fd9\u6837\u6240\u6709\u7684\u9879\u76ee\u90fd\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u5e93\u3002\u56e0\u6b64\uff0c\u4e0b\u9762\u7684\u6b65\u9aa4\u6bcf\u4e2a\u5e93\u53ea\u9700\u8981\u6267\u884c\u4e00\u6b21\u3002 \u627e\u5230 \u201cSettings menu\u201d \u7136\u540e\u9009\u62e9 \u201cCompiler\u201d\u3002 \u70b9\u51fb \u201cDirectories\u201d \u9009\u9879\u5361\u627e\u5230\u9ed8\u8ba4\u7684 \u201cCompiler\u201d \u5b50\u9009\u9879\u5361\u3002 \u6309\u4e0b\u201cAdd\u201d\u6309\u94ae\uff0c\u4e3a\u5e93\u7684\u5934\u6587\u4ef6\u6dfb\u52a0\u8def\u5f84\u3002\u5982\u679c\u4f60\u4f7f\u7528Linux\u5e76\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u5b89\u88c5\u5e93\uff0c\u8fd9\u91cc\u5217\u51fa\u4e86 /usr/include \uff1b \u70b9\u51fb \u201cLinker\u201d \u6807\u7b7e\u3002\u70b9\u51fb \u201cAdd\u201d \u6309\u94ae\u5e76\u6dfb\u52a0 .lib \u6587\u4ef6\u7684\u5730\u5740\u3002\u5982\u679c\u4f60\u8fd0\u884c\u7684\u662f LInux \u4e14\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u5b89\u88c5\u7684\u5e93\uff0c\u8bf7\u786e\u4fdd\u5728\u6b64\u5904\u5217\u51fa /usr/lib \uff1b \u70b9\u51fbOK\u6309\u94ae\u3002 \u6b65\u9aa4 5 \u2014\u2014 \u544a\u8bc9\u94fe\u63a5\u5668\u7a0b\u5e8f\u4f7f\u7528\u54ea\u4e9b\u5e93 \u5bf9\u4e8e\u6b65\u9aa4 5\uff0c\u6211\u4eec\u9700\u8981\u5c06\u5e93\u6587\u4ef6\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u3002\u8fd9\u4e2a\u64cd\u4f5c\u6bcf\u4e2a\u9879\u76ee\u90fd\u9700\u8981\u505a\u4e00\u6b21\u3002 \u53f3\u952edefault workspace\u4e0b\u9762\u7684\u9879\u76ee\u540d\uff0c\u9009\u62e9\u201cBuild options\u201d\uff1b \u70b9\u51fb linker \u9009\u9879\u5361\u3002\u5728 \u201cLink libraries\u201d \u7a97\u53e3\u4e0b\u70b9\u51fb \u201cAdd\u201d \u6309\u94ae\u5e76\u6dfb\u52a0\u5e93\uff1b \u70b9\u51fbOK\u6309\u94ae\u3002 \u6b65\u9aa4 6\u548c 7 \u2014\u2014 #include \u5934\u6587\u4ef6 \u53ea\u9700\u50cf\u5f80\u5e38\u4e00\u6837\u5c06\u5e93\u4e2d\u7684\u5934\u6587\u4ef6 #include \u5230\u9879\u76ee\u4e2d\u3002 \u53c2\u89c1\uff1a A.1 - \u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93 \u83b7\u53d6\u66f4\u591a\u5173\u4e8e\u6b65\u9aa4 7 \u7684\u4fe1\u606f","title":"A.3 - \u5728Code Blocks\u4e2d\u4f7f\u7528\u5e93"},{"location":"A-4-C%2B%2BFAQ/","text":"Key Takeaway \u6709\u4e00\u4e9b\u95ee\u9898\u5f80\u5f80\u88ab\u95ee\u4e86\u4e00\u904d\u53c8\u4e00\u904d\u3002\u672cFAQ\u5c06\u5c1d\u8bd5\u56de\u7b54\u6700\u5e38\u89c1\u7684\u95ee\u9898\u3002 \u95ee: \u4e3a\u4ec0\u4e48\u4e0d\u5e94\u8be5\u4f7f\u7528 \u201c using namespace std \u201d? \u8bed\u53e5 using namespace std; \u4f7f\u7528\u4e86 using \u6307\u4ee4 \u2014\u2014\u5b83\u5c06\u67d0\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6240\u6709\u6807\u8bc6\u7b26\u90fd\u5bfc\u5165\u5230\u5f53\u524d\u547d\u540d\u7a7a\u95f4\u3002 \u4f60\u53ef\u80fd\u770b\u8fc7\u8fd9\u6837\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 #include using namespace std ; int main () { cout << \"Hello world!\" ; return 0 ; } \u8fd9\u4e48\u505a\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4e0d\u5fc5\u663e\u5f0f\u6307\u660e std \u7684\u547d\u540d\u7a7a\u95f4\uff0c\u518d\u4e5f\u4e0d\u7528\u5199 std:: \u4e86\u3002\u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u53ea\u9700\u8981\u4f7f\u7528 cout \u800c\u4e0d\u9700\u8981\u4f7f\u7528 std::cout \u3002\u542c\u8d77\u6765\u5f88\u4e0d\u9519\u5bf9\u5427\uff1f \u7136\u800c\uff0c\u5f53\u7f16\u8bd1\u5668\u9047\u5230 using namespace std \u65f6\uff0c\u5b83\u4f1a\u5c06\u5728 namespace std \u4e2d\u627e\u5230\u7684\u6bcf\u4e00\u4e2a\u6807\u8bc6\u7b26\u90fd\u5bfc\u5165\u5168\u5c40\u4f5c\u7528\u57df(\u56e0\u4e3ausing\u6307\u4ee4\u5c31\u653e\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d)\u3002\u8fd9\u5e26\u6765\u4e863\u4e2a\u4e3b\u8981\u6311\u6218\uff1a \u4f60\u6240\u9009\u62e9\u7684\u6807\u8bc6\u7b26\u4e0e std \u540d\u79f0\u7a7a\u95f4\u4e2d\u5df2\u7ecf\u5b58\u5728\u7684\u6807\u8bc6\u7b26\u4e4b\u95f4\u53d1\u751f\u547d\u540d\u51b2\u7a81\u7684\u51e0\u7387\u5927\u5927\u589e\u52a0\uff1b \u6807\u51c6\u5e93\u7684\u65b0\u7248\u672c\u53ef\u80fd\u4f1a\u7834\u574f\u4f60\u5f53\u524d\u53ef\u4ee5\u5de5\u4f5c\u7684\u7a0b\u5e8f\u3002\u8fd9\u4e9b\u672a\u6765\u7684\u7248\u672c\u53ef\u80fd\u4f1a\u5f15\u5165\u5bfc\u81f4\u65b0\u7684\u547d\u540d\u51b2\u7a81\u7684\u540d\u79f0\uff0c\u6216\u8005\u5728\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u7684\u884c\u4e3a\u53ef\u80fd\u4f1a\u6084\u65e0\u58f0\u606f\u5730\u51fa\u4e4e\u610f\u6599\u5730\u6539\u53d8! \u7f3a\u5c11 std:: \u524d\u7f00\u4f1a\u4f7f\u8bfb\u8005\u96be\u4ee5\u533a\u5206\u54ea\u4e9b\u540d\u5b57\u5c5e\u4e8e std \u5e93\u54ea\u4e9b\u662f\u7528\u6237\u5b9a\u4e49\u7684\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u63a8\u8350\u907f\u514d\u4f7f\u7528 using namespace std (\u4ee5\u53ca\u5176\u4ed6 using \u8bed\u53e5)\u3002\u901a\u8fc7\u5b83\u8282\u7701\u7684\u6253\u5b57\u65f6\u95f4\u548c\u53ef\u80fd\u5e26\u6765\u7684\u98ce\u9669\u76f8\u6bd4\u662f\u4e0d\u5212\u7b97\u7684\u3002 \u76f8\u5173\u5185\u5bb9 \u53c2\u89c1 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 \u83b7\u53d6\u66f4\u591a\u4fe1\u606f\u3002 \u95ee\uff1a\u4e3a\u4ec0\u4e48\u4f7f\u7528\u67d0\u4e9b\u529f\u80fd\u65f6\u4e0d\u9700\u8981\u5305\u542b\u5934\u6587\u4ef6\uff1f \u5934\u6587\u4ef6\u53ef\u4ee5\u201c #include \u201d\u5176\u4ed6\u5934\u6587\u4ef6\u3002\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u5305\u542b\u4e00\u4e2a\u5934\u6587\u4ef6\u65f6\uff0c\u540c\u65f6\u5c31\u53ef\u4ee5\u83b7\u5f97\u5b83\u5305\u542b\u7684\u6240\u6709\u9644\u52a0\u5934\u6587\u4ef6(\u4ee5\u53ca\u90a3\u4e9b\u5934\u6587\u4ef6\u5305\u542b\u7684\u6240\u6709\u5934\u6587\u4ef6)\u3002\u6240\u6709\u6ca1\u6709\u663e\u5f0f\u5305\u542b\u7684\u9644\u52a0\u5934\u6587\u4ef6\u79f0\u4e3a\u201c\u4f20\u9012\u5305\u542b\u201d\u3002 \u6bd4\u65b9\u8bf4\uff0c main.cpp \u4e2d #included \uff0c\u540c\u65f6\u4f60\u7684\u7f16\u8bd1\u5668\u5728 \u4e2d #included (\u6216\u8005\u5176\u4ed6\u5934\u6587\u4ef6 #included )\u3002 \u5373\u4f7f\u5b83\u53ef\u4ee5\u5728\u7f16\u8bd1\u5668\u4e0a\u7f16\u8bd1\uff0c\u4e5f\u4e0d\u5e94\u8be5\u4f9d\u8d56\u5b83\u3002\u56e0\u4e3a\u4f60\u80fd\u591f\u7f16\u8bd1\u7684\u4e1c\u897f\u53ef\u80fd\u4e0d\u80fd\u5728\u5176\u4ed6\u7f16\u8bd1\u5668\u4e0a\u7f16\u8bd1\uff0c\u751a\u81f3\u4e0d\u80fd\u5728\u4f60\u7684\u7f16\u8bd1\u5668\u7684\u672a\u6765\u7248\u672c\u4e0a\u7f16\u8bd1\u3002 \u6ca1\u6709\u529e\u6cd5\u5728\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\u65f6\u53d1\u51fa\u8b66\u544a\uff0c\u6216\u9632\u6b62\u5b83\u53d1\u751f\u3002\u6211\u4eec\u80fd\u505a\u7684\u5c31\u662f\u4e3a\u6240\u4f7f\u7528\u7684\u6240\u6709\u4e1c\u897f\u663e\u5f0f\u5730\u5305\u62ec\u9002\u5f53\u7684\u5934\u6587\u4ef6\u3002\u5728\u51e0\u4e2a\u4e0d\u540c\u7684\u7f16\u8bd1\u5668\u4e0a\u7f16\u8bd1\u4f60\u7684\u7a0b\u5e8f\u53ef\u80fd\u6709\u52a9\u4e8e\u8bc6\u522b\u88ab\u5176\u4ed6\u7f16\u8bd1\u5668\u4f20\u9012\u5305\u542b\u7684\u5934\u6587\u4ef6\u3002 \u76f8\u5173\u5185\u5bb9 \u53c2\u89c1 2.11 - \u5934\u6587\u4ef6 \u95ee\uff1a\u4e3a\u4ec0\u4e48\uff08\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u4ee3\u7801\uff09\u6709\u8fd9\u6837\u7684\u7ed3\u679c\uff1f \u5f53\u7a0b\u5e8f\u6267\u884c\u975eC++\u6807\u51c6\u5b9a\u4e49\u7684\u64cd\u4f5c\u65f6\uff0c\u4f1a\u53d1\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u5b9e\u73b0\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u4ee3\u7801\u53ef\u80fd\u4f1a\u51fa\u73b0\u4ee5\u4e0b\u4efb\u4f55\u75c7\u72b6\uff1a \u7a0b\u5e8f\u6bcf\u6b21\u8fd0\u884c\u90fd\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u7ed3\u679c\uff1b \u7a0b\u5e8f\u59cb\u7ec8\u4ea7\u751f\u76f8\u540c\u7684\u9519\u8bef\u7ed3\u679c\uff1b \u7a0b\u5e8f\u884c\u4e3a\u4e0d\u4e00\u81f4(\u6709\u65f6\u4ea7\u751f\u6b63\u786e\u7684\u7ed3\u679c\uff0c\u6709\u65f6\u4e0d)\uff1b \u7a0b\u5e8f\u4f3c\u4e4e\u5728\u5de5\u4f5c\uff0c\u4f46\u5728\u7a0b\u5e8f\u540e\u9762\u4ea7\u751f\u4e86\u4e0d\u6b63\u786e\u7684\u7ed3\u679c\uff1b \u7a0b\u5e8f\u7acb\u5373\u5d29\u6e83\u6216\u4e00\u6bb5\u65f6\u95f4\u540e\u5d29\u6e83\uff1b \u7a0b\u5e8f\u53ef\u4ee5\u5728\u4e00\u4e9b\u7f16\u8bd1\u5668\u4e0a\u5de5\u4f5c\uff0c\u4f46\u4e0d\u80fd\u5728\u5176\u4ed6\u7f16\u8bd1\u5668\u4e0a\u5de5\u4f5c\uff1b \u7a0b\u5e8f\u53ef\u4ee5\u5de5\u4f5c\uff0c\u4f46\u662f\u5728\u6539\u53d8\u4e00\u4e9b\u5176\u4ed6\u770b\u8d77\u6765\u4e0d\u76f8\u5173\u7684\u4ee3\u7801\u540e\u5c31\u4e0d\u80fd\u5de5\u4f5c\u4e86 \u5f53\u7136\uff0c\u4f60\u7684\u4ee3\u7801\u4e5f\u53ef\u80fd\u4f1a\u4ea7\u751f\u6b63\u786e\u7684\u7ed3\u679c\u3002 \u8bfb\u8005\u7ecf\u5e38\u4f1a\u5728\u63d0\u95ee\u4e2d\u95ee\u5230\u4e3a\u4ec0\u4e48\u5728\u4ed6\u4eec\u7684\u7cfb\u7edf\u4e0a\u4f1a\u4ea7\u751f\u7279\u5b9a\u7684\u73b0\u8c61\uff0c\u662f\u4ec0\u4e48\u539f\u56e0\u5bfc\u81f4\u7684\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u5f88\u96be\u8bf4\uff0c\u56e0\u4e3a\u751f\u6210\u7684\u7ed3\u679c\u53ef\u80fd\u53d6\u51b3\u4e8e\u5f53\u524d\u7a0b\u5e8f\u72b6\u6001\u3001\u7f16\u8bd1\u5668\u8bbe\u7f6e\u3001\u7f16\u8bd1\u5668\u5982\u4f55\u5b9e\u73b0\u67d0\u4e2a\u7279\u6027\u3001\u8ba1\u7b97\u673a\u7684\u67b6\u6784\u548c/\u6216\u64cd\u4f5c\u7cfb\u7edf\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6253\u5370\u4e00\u4e2a\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\u7684\u503c\uff0c\u53ef\u80fd\u4f1a\u5f97\u5230\u5783\u573e\uff0c\u6216\u8005\u603b\u662f\u5f97\u5230\u4e00\u4e2a\u7279\u5b9a\u7684\u503c\u3002\u8fd9\u53d6\u51b3\u4e8e\u5b83\u662f\u4ec0\u4e48\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u7f16\u8bd1\u5668\u5982\u4f55\u5728\u5185\u5b58\u4e2d\u5e03\u5c40\u53d8\u91cf\uff0c\u4ee5\u53ca\u5728\u6b64\u4e4b\u524d\u5185\u5b58\u4e2d\u6709\u4ec0\u4e48(\u53ef\u80fd\u53d7\u5230\u64cd\u4f5c\u7cfb\u7edf\u6216\u5728\u6b64\u70b9\u4e4b\u524d\u7684\u7a0b\u5e8f\u72b6\u6001\u7684\u5f71\u54cd)\u3002 \u867d\u7136\u56de\u7b54\u4e0a\u8ff0\u95ee\u9898\u53ef\u80fd\u5f88\u6709\u8da3\uff0c\u4f46\u603b\u4f53\u4e0a\u8fd9\u6837\u7684\u7b54\u6848\u5e76\u6ca1\u6709\u4ec0\u4e48\u7528\u7528(\u800c\u4e14\u53ef\u80fd\u4f1a\u5728\u5176\u4ed6\u4e1c\u897f\u53d1\u751f\u53d8\u5316\u65f6\u53d1\u751f\u53d8\u5316)\u3002\u8fd9\u5c31\u50cf\u5728\u95ee:\u201c\u5f53\u6211\u628a\u5b89\u5168\u5e26\u7a7f\u8fc7\u65b9\u5411\u76d8\u5e76\u8fde\u63a5\u5230\u6cb9\u95e8\u4e0a\u65f6\uff0c\u4e3a\u4ec0\u4e48\u6211\u5728\u96e8\u5929\u8f6c\u5934\u65f6\uff0c\u6c7d\u8f66\u4f1a\u5411\u8f6c\uff1f\u201d\u6700\u597d\u7684\u7b54\u6848\u4e0d\u662f\u5bf9\u6b63\u5728\u53d1\u751f\u7684\u4e8b\u60c5\u505a\u51fa\u89e3\u91ca\uff0c\u800c\u662f\u201c\u4e0d\u8981\u90a3\u6837\u505a\u201d\u3002 \u76f8\u5173\u5185\u5bb9 \u672a\u5b9a\u4e49\u884c\u4e3a \u8bf7\u53c2\u8003 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u95ee\uff1a\u6211\u5c1d\u8bd5\u7f16\u8bd1\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5b83\u672c\u5e94\u8be5\u5de5\u4f5c\u5374\u7f16\u8bd1\u62a5\u9519\u4e86\uff0c\u4e3a\u4ec0\u4e48\uff1f \u6700\u5e38\u89c1\u7684\u539f\u56e0\u662f\uff0c\u4f60\u7684\u9879\u76ee\u6b63\u5728\u4f7f\u7528\u8bed\u8a00\u6807\u51c6\u662f\u4e0d\u5bf9\u7684\u3002 C++\u5728\u6bcf\u4e2a\u65b0\u7684\u8bed\u8a00\u6807\u51c6\u4e2d\u90fd\u5f15\u5165\u4e86\u8bb8\u591a\u65b0\u7279\u6027\u3002\u5982\u679c\u6211\u4eec\u7684\u4e00\u4e2a\u793a\u4f8b\u4f7f\u7528C++ 17\u4e2d\u5f15\u5165\u7684\u7279\u6027\uff0c\u4f46\u4f60\u7684\u7a0b\u5e8f\u6b63\u5728\u4f7f\u7528C++ 14\u8bed\u8a00\u6807\u51c6\u7f16\u8bd1\uff0c\u90a3\u4e48\u5b83\u5c06\u65e0\u6cd5\u7f16\u8bd1\uff0c\u56e0\u4e3a\u6211\u4eec\u6b63\u5728\u4f7f\u7528\u7684\u7279\u6027\u4e0d\u5b58\u5728\u3002\u5c1d\u8bd5\u6807\u51c6\u8bbe\u7f6e\u4e3a\u7f16\u8bd1\u5668\u652f\u6301\u7684\u6700\u65b0\u7248\u672c\uff0c\u770b\u770b\u8fd9\u662f\u5426\u89e3\u51b3\u4e86\u95ee\u9898\u3002 \u76f8\u5173\u5185\u5bb9 \u53c2\u8003 0.12 -- Configuring your compiler: Choosing a language standard . \u4e5f\u6709\u53ef\u80fd\u4f60\u7684\u7f16\u8bd1\u5668\u8fd8\u4e0d\u652f\u6301\u7279\u5b9a\u7684\u7279\u6027\uff0c\u6216\u8005\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6709\u4e00\u4e2abug\u5bfc\u81f4\u5176\u65e0\u6cd5\u4f7f\u7528\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5c1d\u8bd5\u5c06\u7f16\u8bd1\u5668\u66f4\u65b0\u5230\u53ef\u7528\u7684\u6700\u65b0\u7248\u672c\u3002","title":"A.4 \u2014 C++ FAQ"},{"location":"A-4-C%2B%2BFAQ/#using-namespace-std","text":"\u8bed\u53e5 using namespace std; \u4f7f\u7528\u4e86 using \u6307\u4ee4 \u2014\u2014\u5b83\u5c06\u67d0\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6240\u6709\u6807\u8bc6\u7b26\u90fd\u5bfc\u5165\u5230\u5f53\u524d\u547d\u540d\u7a7a\u95f4\u3002 \u4f60\u53ef\u80fd\u770b\u8fc7\u8fd9\u6837\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 #include using namespace std ; int main () { cout << \"Hello world!\" ; return 0 ; } \u8fd9\u4e48\u505a\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4e0d\u5fc5\u663e\u5f0f\u6307\u660e std \u7684\u547d\u540d\u7a7a\u95f4\uff0c\u518d\u4e5f\u4e0d\u7528\u5199 std:: \u4e86\u3002\u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u53ea\u9700\u8981\u4f7f\u7528 cout \u800c\u4e0d\u9700\u8981\u4f7f\u7528 std::cout \u3002\u542c\u8d77\u6765\u5f88\u4e0d\u9519\u5bf9\u5427\uff1f \u7136\u800c\uff0c\u5f53\u7f16\u8bd1\u5668\u9047\u5230 using namespace std \u65f6\uff0c\u5b83\u4f1a\u5c06\u5728 namespace std \u4e2d\u627e\u5230\u7684\u6bcf\u4e00\u4e2a\u6807\u8bc6\u7b26\u90fd\u5bfc\u5165\u5168\u5c40\u4f5c\u7528\u57df(\u56e0\u4e3ausing\u6307\u4ee4\u5c31\u653e\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d)\u3002\u8fd9\u5e26\u6765\u4e863\u4e2a\u4e3b\u8981\u6311\u6218\uff1a \u4f60\u6240\u9009\u62e9\u7684\u6807\u8bc6\u7b26\u4e0e std \u540d\u79f0\u7a7a\u95f4\u4e2d\u5df2\u7ecf\u5b58\u5728\u7684\u6807\u8bc6\u7b26\u4e4b\u95f4\u53d1\u751f\u547d\u540d\u51b2\u7a81\u7684\u51e0\u7387\u5927\u5927\u589e\u52a0\uff1b \u6807\u51c6\u5e93\u7684\u65b0\u7248\u672c\u53ef\u80fd\u4f1a\u7834\u574f\u4f60\u5f53\u524d\u53ef\u4ee5\u5de5\u4f5c\u7684\u7a0b\u5e8f\u3002\u8fd9\u4e9b\u672a\u6765\u7684\u7248\u672c\u53ef\u80fd\u4f1a\u5f15\u5165\u5bfc\u81f4\u65b0\u7684\u547d\u540d\u51b2\u7a81\u7684\u540d\u79f0\uff0c\u6216\u8005\u5728\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u7684\u884c\u4e3a\u53ef\u80fd\u4f1a\u6084\u65e0\u58f0\u606f\u5730\u51fa\u4e4e\u610f\u6599\u5730\u6539\u53d8! \u7f3a\u5c11 std:: \u524d\u7f00\u4f1a\u4f7f\u8bfb\u8005\u96be\u4ee5\u533a\u5206\u54ea\u4e9b\u540d\u5b57\u5c5e\u4e8e std \u5e93\u54ea\u4e9b\u662f\u7528\u6237\u5b9a\u4e49\u7684\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u63a8\u8350\u907f\u514d\u4f7f\u7528 using namespace std (\u4ee5\u53ca\u5176\u4ed6 using \u8bed\u53e5)\u3002\u901a\u8fc7\u5b83\u8282\u7701\u7684\u6253\u5b57\u65f6\u95f4\u548c\u53ef\u80fd\u5e26\u6765\u7684\u98ce\u9669\u76f8\u6bd4\u662f\u4e0d\u5212\u7b97\u7684\u3002 \u76f8\u5173\u5185\u5bb9 \u53c2\u89c1 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 \u83b7\u53d6\u66f4\u591a\u4fe1\u606f\u3002","title":"\u95ee: \u4e3a\u4ec0\u4e48\u4e0d\u5e94\u8be5\u4f7f\u7528 \u201cusing namespace std\u201d?"},{"location":"A-4-C%2B%2BFAQ/#_1","text":"\u5934\u6587\u4ef6\u53ef\u4ee5\u201c #include \u201d\u5176\u4ed6\u5934\u6587\u4ef6\u3002\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u5305\u542b\u4e00\u4e2a\u5934\u6587\u4ef6\u65f6\uff0c\u540c\u65f6\u5c31\u53ef\u4ee5\u83b7\u5f97\u5b83\u5305\u542b\u7684\u6240\u6709\u9644\u52a0\u5934\u6587\u4ef6(\u4ee5\u53ca\u90a3\u4e9b\u5934\u6587\u4ef6\u5305\u542b\u7684\u6240\u6709\u5934\u6587\u4ef6)\u3002\u6240\u6709\u6ca1\u6709\u663e\u5f0f\u5305\u542b\u7684\u9644\u52a0\u5934\u6587\u4ef6\u79f0\u4e3a\u201c\u4f20\u9012\u5305\u542b\u201d\u3002 \u6bd4\u65b9\u8bf4\uff0c main.cpp \u4e2d #included \uff0c\u540c\u65f6\u4f60\u7684\u7f16\u8bd1\u5668\u5728 \u4e2d #included (\u6216\u8005\u5176\u4ed6\u5934\u6587\u4ef6 #included )\u3002 \u5373\u4f7f\u5b83\u53ef\u4ee5\u5728\u7f16\u8bd1\u5668\u4e0a\u7f16\u8bd1\uff0c\u4e5f\u4e0d\u5e94\u8be5\u4f9d\u8d56\u5b83\u3002\u56e0\u4e3a\u4f60\u80fd\u591f\u7f16\u8bd1\u7684\u4e1c\u897f\u53ef\u80fd\u4e0d\u80fd\u5728\u5176\u4ed6\u7f16\u8bd1\u5668\u4e0a\u7f16\u8bd1\uff0c\u751a\u81f3\u4e0d\u80fd\u5728\u4f60\u7684\u7f16\u8bd1\u5668\u7684\u672a\u6765\u7248\u672c\u4e0a\u7f16\u8bd1\u3002 \u6ca1\u6709\u529e\u6cd5\u5728\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\u65f6\u53d1\u51fa\u8b66\u544a\uff0c\u6216\u9632\u6b62\u5b83\u53d1\u751f\u3002\u6211\u4eec\u80fd\u505a\u7684\u5c31\u662f\u4e3a\u6240\u4f7f\u7528\u7684\u6240\u6709\u4e1c\u897f\u663e\u5f0f\u5730\u5305\u62ec\u9002\u5f53\u7684\u5934\u6587\u4ef6\u3002\u5728\u51e0\u4e2a\u4e0d\u540c\u7684\u7f16\u8bd1\u5668\u4e0a\u7f16\u8bd1\u4f60\u7684\u7a0b\u5e8f\u53ef\u80fd\u6709\u52a9\u4e8e\u8bc6\u522b\u88ab\u5176\u4ed6\u7f16\u8bd1\u5668\u4f20\u9012\u5305\u542b\u7684\u5934\u6587\u4ef6\u3002 \u76f8\u5173\u5185\u5bb9 \u53c2\u89c1 2.11 - \u5934\u6587\u4ef6","title":"\u95ee\uff1a\u4e3a\u4ec0\u4e48\u4f7f\u7528\u67d0\u4e9b\u529f\u80fd\u65f6\u4e0d\u9700\u8981\u5305\u542b\u5934\u6587\u4ef6\uff1f"},{"location":"A-4-C%2B%2BFAQ/#_2","text":"\u5f53\u7a0b\u5e8f\u6267\u884c\u975eC++\u6807\u51c6\u5b9a\u4e49\u7684\u64cd\u4f5c\u65f6\uff0c\u4f1a\u53d1\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u5b9e\u73b0\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u4ee3\u7801\u53ef\u80fd\u4f1a\u51fa\u73b0\u4ee5\u4e0b\u4efb\u4f55\u75c7\u72b6\uff1a \u7a0b\u5e8f\u6bcf\u6b21\u8fd0\u884c\u90fd\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u7ed3\u679c\uff1b \u7a0b\u5e8f\u59cb\u7ec8\u4ea7\u751f\u76f8\u540c\u7684\u9519\u8bef\u7ed3\u679c\uff1b \u7a0b\u5e8f\u884c\u4e3a\u4e0d\u4e00\u81f4(\u6709\u65f6\u4ea7\u751f\u6b63\u786e\u7684\u7ed3\u679c\uff0c\u6709\u65f6\u4e0d)\uff1b \u7a0b\u5e8f\u4f3c\u4e4e\u5728\u5de5\u4f5c\uff0c\u4f46\u5728\u7a0b\u5e8f\u540e\u9762\u4ea7\u751f\u4e86\u4e0d\u6b63\u786e\u7684\u7ed3\u679c\uff1b \u7a0b\u5e8f\u7acb\u5373\u5d29\u6e83\u6216\u4e00\u6bb5\u65f6\u95f4\u540e\u5d29\u6e83\uff1b \u7a0b\u5e8f\u53ef\u4ee5\u5728\u4e00\u4e9b\u7f16\u8bd1\u5668\u4e0a\u5de5\u4f5c\uff0c\u4f46\u4e0d\u80fd\u5728\u5176\u4ed6\u7f16\u8bd1\u5668\u4e0a\u5de5\u4f5c\uff1b \u7a0b\u5e8f\u53ef\u4ee5\u5de5\u4f5c\uff0c\u4f46\u662f\u5728\u6539\u53d8\u4e00\u4e9b\u5176\u4ed6\u770b\u8d77\u6765\u4e0d\u76f8\u5173\u7684\u4ee3\u7801\u540e\u5c31\u4e0d\u80fd\u5de5\u4f5c\u4e86 \u5f53\u7136\uff0c\u4f60\u7684\u4ee3\u7801\u4e5f\u53ef\u80fd\u4f1a\u4ea7\u751f\u6b63\u786e\u7684\u7ed3\u679c\u3002 \u8bfb\u8005\u7ecf\u5e38\u4f1a\u5728\u63d0\u95ee\u4e2d\u95ee\u5230\u4e3a\u4ec0\u4e48\u5728\u4ed6\u4eec\u7684\u7cfb\u7edf\u4e0a\u4f1a\u4ea7\u751f\u7279\u5b9a\u7684\u73b0\u8c61\uff0c\u662f\u4ec0\u4e48\u539f\u56e0\u5bfc\u81f4\u7684\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u5f88\u96be\u8bf4\uff0c\u56e0\u4e3a\u751f\u6210\u7684\u7ed3\u679c\u53ef\u80fd\u53d6\u51b3\u4e8e\u5f53\u524d\u7a0b\u5e8f\u72b6\u6001\u3001\u7f16\u8bd1\u5668\u8bbe\u7f6e\u3001\u7f16\u8bd1\u5668\u5982\u4f55\u5b9e\u73b0\u67d0\u4e2a\u7279\u6027\u3001\u8ba1\u7b97\u673a\u7684\u67b6\u6784\u548c/\u6216\u64cd\u4f5c\u7cfb\u7edf\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6253\u5370\u4e00\u4e2a\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\u7684\u503c\uff0c\u53ef\u80fd\u4f1a\u5f97\u5230\u5783\u573e\uff0c\u6216\u8005\u603b\u662f\u5f97\u5230\u4e00\u4e2a\u7279\u5b9a\u7684\u503c\u3002\u8fd9\u53d6\u51b3\u4e8e\u5b83\u662f\u4ec0\u4e48\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u7f16\u8bd1\u5668\u5982\u4f55\u5728\u5185\u5b58\u4e2d\u5e03\u5c40\u53d8\u91cf\uff0c\u4ee5\u53ca\u5728\u6b64\u4e4b\u524d\u5185\u5b58\u4e2d\u6709\u4ec0\u4e48(\u53ef\u80fd\u53d7\u5230\u64cd\u4f5c\u7cfb\u7edf\u6216\u5728\u6b64\u70b9\u4e4b\u524d\u7684\u7a0b\u5e8f\u72b6\u6001\u7684\u5f71\u54cd)\u3002 \u867d\u7136\u56de\u7b54\u4e0a\u8ff0\u95ee\u9898\u53ef\u80fd\u5f88\u6709\u8da3\uff0c\u4f46\u603b\u4f53\u4e0a\u8fd9\u6837\u7684\u7b54\u6848\u5e76\u6ca1\u6709\u4ec0\u4e48\u7528\u7528(\u800c\u4e14\u53ef\u80fd\u4f1a\u5728\u5176\u4ed6\u4e1c\u897f\u53d1\u751f\u53d8\u5316\u65f6\u53d1\u751f\u53d8\u5316)\u3002\u8fd9\u5c31\u50cf\u5728\u95ee:\u201c\u5f53\u6211\u628a\u5b89\u5168\u5e26\u7a7f\u8fc7\u65b9\u5411\u76d8\u5e76\u8fde\u63a5\u5230\u6cb9\u95e8\u4e0a\u65f6\uff0c\u4e3a\u4ec0\u4e48\u6211\u5728\u96e8\u5929\u8f6c\u5934\u65f6\uff0c\u6c7d\u8f66\u4f1a\u5411\u8f6c\uff1f\u201d\u6700\u597d\u7684\u7b54\u6848\u4e0d\u662f\u5bf9\u6b63\u5728\u53d1\u751f\u7684\u4e8b\u60c5\u505a\u51fa\u89e3\u91ca\uff0c\u800c\u662f\u201c\u4e0d\u8981\u90a3\u6837\u505a\u201d\u3002 \u76f8\u5173\u5185\u5bb9 \u672a\u5b9a\u4e49\u884c\u4e3a \u8bf7\u53c2\u8003 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a \u3002","title":"\u95ee\uff1a\u4e3a\u4ec0\u4e48\uff08\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u4ee3\u7801\uff09\u6709\u8fd9\u6837\u7684\u7ed3\u679c\uff1f"},{"location":"A-4-C%2B%2BFAQ/#_3","text":"\u6700\u5e38\u89c1\u7684\u539f\u56e0\u662f\uff0c\u4f60\u7684\u9879\u76ee\u6b63\u5728\u4f7f\u7528\u8bed\u8a00\u6807\u51c6\u662f\u4e0d\u5bf9\u7684\u3002 C++\u5728\u6bcf\u4e2a\u65b0\u7684\u8bed\u8a00\u6807\u51c6\u4e2d\u90fd\u5f15\u5165\u4e86\u8bb8\u591a\u65b0\u7279\u6027\u3002\u5982\u679c\u6211\u4eec\u7684\u4e00\u4e2a\u793a\u4f8b\u4f7f\u7528C++ 17\u4e2d\u5f15\u5165\u7684\u7279\u6027\uff0c\u4f46\u4f60\u7684\u7a0b\u5e8f\u6b63\u5728\u4f7f\u7528C++ 14\u8bed\u8a00\u6807\u51c6\u7f16\u8bd1\uff0c\u90a3\u4e48\u5b83\u5c06\u65e0\u6cd5\u7f16\u8bd1\uff0c\u56e0\u4e3a\u6211\u4eec\u6b63\u5728\u4f7f\u7528\u7684\u7279\u6027\u4e0d\u5b58\u5728\u3002\u5c1d\u8bd5\u6807\u51c6\u8bbe\u7f6e\u4e3a\u7f16\u8bd1\u5668\u652f\u6301\u7684\u6700\u65b0\u7248\u672c\uff0c\u770b\u770b\u8fd9\u662f\u5426\u89e3\u51b3\u4e86\u95ee\u9898\u3002 \u76f8\u5173\u5185\u5bb9 \u53c2\u8003 0.12 -- Configuring your compiler: Choosing a language standard . \u4e5f\u6709\u53ef\u80fd\u4f60\u7684\u7f16\u8bd1\u5668\u8fd8\u4e0d\u652f\u6301\u7279\u5b9a\u7684\u7279\u6027\uff0c\u6216\u8005\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6709\u4e00\u4e2abug\u5bfc\u81f4\u5176\u65e0\u6cd5\u4f7f\u7528\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5c1d\u8bd5\u5c06\u7f16\u8bd1\u5668\u66f4\u65b0\u5230\u53ef\u7528\u7684\u6700\u65b0\u7248\u672c\u3002","title":"\u95ee\uff1a\u6211\u5c1d\u8bd5\u7f16\u8bd1\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5b83\u672c\u5e94\u8be5\u5de5\u4f5c\u5374\u7f16\u8bd1\u62a5\u9519\u4e86\uff0c\u4e3a\u4ec0\u4e48\uff1f"},{"location":"Associations/","tags":["\u8bcd\u6c47\u8868","Associations"],"text":"","title":"\u5173\u8054"},{"location":"Associative-containers/","tags":["\u8bcd\u6c47\u8868","Associative-containers"],"text":"","title":"\u5173\u8054\u5bb9\u5668"},{"location":"B-1-introduction-to-C%2B%2B11/","text":"C++11 \u662f\u4ec0\u4e48\uff1f 2011\u5e748\u670811\u65e5\uff0c ISO (\u56fd\u9645\u6807\u51c6\u5316\u7ec4\u7ec7) \u901a\u8fc7\u4e86C++\u7684\u65b0\u7248\u672c\u2014\u2014C++11\u3002C++11 \u4e3aC++\u8bed\u8a00\u6dfb\u52a0\u4e86\u4e00\u7cfb\u5217\u5168\u65b0\u7684\u7279\u6027\uff01\u662f\u5426\u4f7f\u7528\u8fd9\u4e9b\u65b0\u529f\u80fd\u662f\u53ef\u9009\u7684\u2014\u2014\u4f46\u6beb\u65e0\u7591\u95ee\uff0c\u4f60\u4f1a\u53d1\u73b0\u5176\u4e2d\u4e00\u4e9b\u529f\u80fd\u662f\u975e\u5e38\u6709\u7528\u7684\u3002\u6559\u7a0b\u90fd\u5df2\u66f4\u65b0\u4e3aC++ 11\u517c\u5bb9\u3002 C++ 11\u7684\u8bbe\u8ba1\u76ee\u6807 Bjarne Stroustrup \u8fd9\u6837\u63cf\u8ff0C++11\u7684\u76ee\u6807\uff1a \u626c\u957f\u907f\u77ed\u2014\u2014\u6269\u5927C++\u7684\u4f18\u52bf\u800c\u4e0d\u662f\u8bd5\u56fe\u5c06C++\u6269\u5c55\u5230\u5b83\u53ef\u80fd\u8f83\u5f31\u7684\u65b0\u9886\u57df\uff08\u4f8b\u5982\u6709\u5927\u91cfGUI\u7684Windows\u5e94\u7528\u7a0b\u5e8f)\uff0c\u4e13\u6ce8\u4e8e\u8ba9\u5b83\u66f4\u597d\u5730\u505a\u5b83\u64c5\u957f\u7684\u4e8b\u60c5\u3002 \u4f7fC++\u66f4\u5bb9\u6613\u5b66\u4e60\u3001\u4f7f\u7528\u548c\u6559\u6388\u2014\u2014\u63d0\u4f9b\u4f7f\u8bed\u8a00\u66f4\u4e00\u81f4\u548c\u66f4\u5bb9\u6613\u4f7f\u7528\u7684\u529f\u80fd\u3002 \u4e3a\u6b64\uff0c\u7f16\u5199\u8be5\u8bed\u8a00\u7684\u59d4\u5458\u4f1a\u8bd5\u56fe\u9075\u5b88\u4ee5\u4e0b\u57fa\u672c\u539f\u5219\uff1a \u5c3d\u53ef\u80fd\u4fdd\u6301\u7a33\u5b9a\u6027\u4ee5\u53ca\u4e0e\u65e7\u7248\u672cC++\u548cC\u7684\u517c\u5bb9\u6027\u3002\u5728C++03\u80fd\u591f\u5de5\u4f5c\u7684\u7a0b\u5e8f\u4ecd\u7136\u5e94\u8be5\u53ef\u4ee5\u5728C++11\u4e0b\u5de5\u4f5c\u3002 \u5c06\u6838\u5fc3\u8bed\u8a00\u7684\u6269\u5c55\u6570\u91cf\u4fdd\u6301\u5728\u6700\u4f4e\u9650\u5ea6\uff0c\u5e76\u5c06\u5927\u90e8\u5206\u7684\u66f4\u6539\u653e\u5728\u6807\u51c6\u5e93\u4e2d(\u8fd9\u4e2a\u76ee\u6807\u5728\u8fd9\u4e2a\u7248\u672c\u4e2d\u6ca1\u6709\u5f88\u597d\u5730\u5b9e\u73b0) \u4e13\u6ce8\u4e8e\u6539\u8fdb\u62bd\u8c61\u673a\u5236(\u7c7b\u3001\u6a21\u677f)\uff0c\u800c\u4e0d\u662f\u6dfb\u52a0\u673a\u5236\u6765\u5904\u7406\u7279\u5b9a\u7684\u3001\u7f55\u89c1\u7684\u60c5\u51b5\u3002 \u4e3a\u65b0\u624b\u548c\u4e13\u5bb6\u6dfb\u52a0\u65b0\u7684\u529f\u80fd\u3002\u7ed9\u6bcf\u4e2a\u4eba\u4e00\u70b9\u597d\u5904\uff01 \u63d0\u9ad8\u7c7b\u578b\u5b89\u5168\uff0c\u9632\u6b62\u65e0\u5fc3\u7684\u9519\u8bef\u3002 \u63d0\u9ad8\u6027\u80fd\uff0c\u5e76\u5141\u8bb8C++\u76f4\u63a5\u4e0e\u786c\u4ef6\u534f\u540c\u5de5\u4f5c\u3002 \u8003\u8651\u53ef\u7528\u6027\u548c\u751f\u6001\u7cfb\u7edf\u95ee\u9898\u3002C++\u9700\u8981\u4e0e\u5176\u4ed6\u5de5\u5177\u5f88\u597d\u914d\u5408\uff0c\u6613\u4e8e\u4f7f\u7528\u548c\u6559\u6388\uff0c\u7b49\u7b49\u3002 C++ 11\u5728\u4e3b\u65e8\u601d\u60f3\u4e0a\u4e0eC++03\u76f8\u5dee\u4e0d\u5927\uff0c\u4f46\u5b83\u786e\u5b9e\u589e\u52a0\u4e86\u5927\u91cf\u7684\u65b0\u529f\u80fd\u3002 C++11\u4e2d\u7684\u91cd\u8981\u65b0\u7279\u6027 \u8fd9\u91cc\u662fC++ 11\u6dfb\u52a0\u7684\u4e3b\u8981\u7279\u6027\u7684\u5217\u8868\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e2a\u5217\u8868\u4e0d\u662f\u5168\u9762\u7684\uff0c\u800c\u662f\u65e8\u5728\u7a81\u51fa\u4e00\u4e9b\u611f\u5174\u8da3\u7684\u5173\u952e\u7279\u6027\u3002 auto ( 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad ) char16_t \u548c char32_t \u4ee5\u53ca\u5176\u4ed6\u5b57\u9762\u91cf\u7684\u652f\u6301( \u6682\u65e0\u6559\u7a0b ) constexpr ( 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf ) decltype ( \u6682\u65e0\u6559\u7a0b ) default \u4fee\u9970\u7b26 ( \u6682\u65e0\u6559\u7a0b ) \u59d4\u6258\u6784\u9020\u51fd\u6570 ( 13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570 ) delete \u4fee\u9970\u7b26 ( 14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57 ) \u679a\u4e3e\u7c7b ( 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 ) Extern templates ( \u6682\u65e0\u6559\u7a0b ) \u533f\u540d\u51fd\u6570 ( 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 ) \u548c \u95ed\u5305 ( 12.8 - lambda \u95ed\u5305 ) long long int ( 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 ) \u79fb\u52a8\u6784\u9020\u51fd\u6570 \u548c \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 ( M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c ) Noexcept \u8bf4\u660e\u7b26 (quick mention in 20.4 ) nullptr ( 9.7 - \u7a7a\u6307\u9488 ) override \u548c final \u8bf4\u660e\u7b26( 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b ) \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af ( 11.13 - for-each \u5faa\u73af ) \u53f3\u503c\u5f15\u7528 ( M.2 - \u53f3\u503c\u5f15\u7528 ) static_assert ( 7.17 - \u65ad\u8a00\u548c static_assert ) std::initializer_list ( 16.7 - std::initializer_list ) \u672b\u5c3e\u8fd4\u56de\u7c7b\u578b\u8bed\u6cd5( 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad ) \u7c7b\u578b\u522b\u540d ( 8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d ) typedef \u53ef\u4ee5\u7528\u4e8e\u6a21\u677f\u7c7b \u7edf\u4e00\u521d\u59cb\u5316 ( 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb ) \u81ea\u5b9a\u4e49\u5b57\u9762\u91cf ( \u6682\u65e0\u6559\u7a0b ) \u53ef\u53d8\u6a21\u677f(Variadic templates )( \u6682\u65e0\u6559\u7a0b ) Two >> symbols without a space between them will now properly be interpreted as closing a template object C++\u6807\u51c6\u5e93\u4e2d\u8fd8\u6709\u8bb8\u591a\u65b0\u7c7b\u53ef\u4f9b\u4f7f\u7528\u3002 \u66f4\u597d\u5730\u652f\u6301\u591a\u7ebf\u7a0b\u548c\u7ebf\u7a0b\u672c\u5730\u5b58\u50a8 ( \u6682\u65e0\u6559\u7a0b ) \u54c8\u5e0c\u8868 ( \u6682\u65e0\u6559\u7a0b ) \u6539\u8fdb\u968f\u673a\u6570\u751f\u6210(\u5728 7.19 - \u4f7f\u7528 Mersenne Twister \u751f\u6210\u968f\u673a\u6570 \u4e2d\u8fdb\u884c\u4e86\u8ba8\u8bba) \u5f15\u7528\u5305\u88c5 ( 18.9 - \u5bf9\u8c61\u5207\u7247 ) \u6b63\u5219\u8868\u8fbe\u5f0f ( \u6682\u65e0\u6559\u7a0b ) std::auto_ptr \u88ab\u5f03\u7528 ( M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb ) std::tuple ( \u6682\u65e0\u6559\u7a0b ) std::unique_ptr ( M.6 \u2014 std::unique_ptr )","title":"B.1 - C++11"},{"location":"B-1-introduction-to-C%2B%2B11/#c11","text":"2011\u5e748\u670811\u65e5\uff0c ISO (\u56fd\u9645\u6807\u51c6\u5316\u7ec4\u7ec7) \u901a\u8fc7\u4e86C++\u7684\u65b0\u7248\u672c\u2014\u2014C++11\u3002C++11 \u4e3aC++\u8bed\u8a00\u6dfb\u52a0\u4e86\u4e00\u7cfb\u5217\u5168\u65b0\u7684\u7279\u6027\uff01\u662f\u5426\u4f7f\u7528\u8fd9\u4e9b\u65b0\u529f\u80fd\u662f\u53ef\u9009\u7684\u2014\u2014\u4f46\u6beb\u65e0\u7591\u95ee\uff0c\u4f60\u4f1a\u53d1\u73b0\u5176\u4e2d\u4e00\u4e9b\u529f\u80fd\u662f\u975e\u5e38\u6709\u7528\u7684\u3002\u6559\u7a0b\u90fd\u5df2\u66f4\u65b0\u4e3aC++ 11\u517c\u5bb9\u3002","title":"C++11 \u662f\u4ec0\u4e48\uff1f"},{"location":"B-1-introduction-to-C%2B%2B11/#c-11","text":"Bjarne Stroustrup \u8fd9\u6837\u63cf\u8ff0C++11\u7684\u76ee\u6807\uff1a \u626c\u957f\u907f\u77ed\u2014\u2014\u6269\u5927C++\u7684\u4f18\u52bf\u800c\u4e0d\u662f\u8bd5\u56fe\u5c06C++\u6269\u5c55\u5230\u5b83\u53ef\u80fd\u8f83\u5f31\u7684\u65b0\u9886\u57df\uff08\u4f8b\u5982\u6709\u5927\u91cfGUI\u7684Windows\u5e94\u7528\u7a0b\u5e8f)\uff0c\u4e13\u6ce8\u4e8e\u8ba9\u5b83\u66f4\u597d\u5730\u505a\u5b83\u64c5\u957f\u7684\u4e8b\u60c5\u3002 \u4f7fC++\u66f4\u5bb9\u6613\u5b66\u4e60\u3001\u4f7f\u7528\u548c\u6559\u6388\u2014\u2014\u63d0\u4f9b\u4f7f\u8bed\u8a00\u66f4\u4e00\u81f4\u548c\u66f4\u5bb9\u6613\u4f7f\u7528\u7684\u529f\u80fd\u3002 \u4e3a\u6b64\uff0c\u7f16\u5199\u8be5\u8bed\u8a00\u7684\u59d4\u5458\u4f1a\u8bd5\u56fe\u9075\u5b88\u4ee5\u4e0b\u57fa\u672c\u539f\u5219\uff1a \u5c3d\u53ef\u80fd\u4fdd\u6301\u7a33\u5b9a\u6027\u4ee5\u53ca\u4e0e\u65e7\u7248\u672cC++\u548cC\u7684\u517c\u5bb9\u6027\u3002\u5728C++03\u80fd\u591f\u5de5\u4f5c\u7684\u7a0b\u5e8f\u4ecd\u7136\u5e94\u8be5\u53ef\u4ee5\u5728C++11\u4e0b\u5de5\u4f5c\u3002 \u5c06\u6838\u5fc3\u8bed\u8a00\u7684\u6269\u5c55\u6570\u91cf\u4fdd\u6301\u5728\u6700\u4f4e\u9650\u5ea6\uff0c\u5e76\u5c06\u5927\u90e8\u5206\u7684\u66f4\u6539\u653e\u5728\u6807\u51c6\u5e93\u4e2d(\u8fd9\u4e2a\u76ee\u6807\u5728\u8fd9\u4e2a\u7248\u672c\u4e2d\u6ca1\u6709\u5f88\u597d\u5730\u5b9e\u73b0) \u4e13\u6ce8\u4e8e\u6539\u8fdb\u62bd\u8c61\u673a\u5236(\u7c7b\u3001\u6a21\u677f)\uff0c\u800c\u4e0d\u662f\u6dfb\u52a0\u673a\u5236\u6765\u5904\u7406\u7279\u5b9a\u7684\u3001\u7f55\u89c1\u7684\u60c5\u51b5\u3002 \u4e3a\u65b0\u624b\u548c\u4e13\u5bb6\u6dfb\u52a0\u65b0\u7684\u529f\u80fd\u3002\u7ed9\u6bcf\u4e2a\u4eba\u4e00\u70b9\u597d\u5904\uff01 \u63d0\u9ad8\u7c7b\u578b\u5b89\u5168\uff0c\u9632\u6b62\u65e0\u5fc3\u7684\u9519\u8bef\u3002 \u63d0\u9ad8\u6027\u80fd\uff0c\u5e76\u5141\u8bb8C++\u76f4\u63a5\u4e0e\u786c\u4ef6\u534f\u540c\u5de5\u4f5c\u3002 \u8003\u8651\u53ef\u7528\u6027\u548c\u751f\u6001\u7cfb\u7edf\u95ee\u9898\u3002C++\u9700\u8981\u4e0e\u5176\u4ed6\u5de5\u5177\u5f88\u597d\u914d\u5408\uff0c\u6613\u4e8e\u4f7f\u7528\u548c\u6559\u6388\uff0c\u7b49\u7b49\u3002 C++ 11\u5728\u4e3b\u65e8\u601d\u60f3\u4e0a\u4e0eC++03\u76f8\u5dee\u4e0d\u5927\uff0c\u4f46\u5b83\u786e\u5b9e\u589e\u52a0\u4e86\u5927\u91cf\u7684\u65b0\u529f\u80fd\u3002","title":"C++ 11\u7684\u8bbe\u8ba1\u76ee\u6807"},{"location":"B-1-introduction-to-C%2B%2B11/#c11_1","text":"\u8fd9\u91cc\u662fC++ 11\u6dfb\u52a0\u7684\u4e3b\u8981\u7279\u6027\u7684\u5217\u8868\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e2a\u5217\u8868\u4e0d\u662f\u5168\u9762\u7684\uff0c\u800c\u662f\u65e8\u5728\u7a81\u51fa\u4e00\u4e9b\u611f\u5174\u8da3\u7684\u5173\u952e\u7279\u6027\u3002 auto ( 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad ) char16_t \u548c char32_t \u4ee5\u53ca\u5176\u4ed6\u5b57\u9762\u91cf\u7684\u652f\u6301( \u6682\u65e0\u6559\u7a0b ) constexpr ( 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf ) decltype ( \u6682\u65e0\u6559\u7a0b ) default \u4fee\u9970\u7b26 ( \u6682\u65e0\u6559\u7a0b ) \u59d4\u6258\u6784\u9020\u51fd\u6570 ( 13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570 ) delete \u4fee\u9970\u7b26 ( 14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57 ) \u679a\u4e3e\u7c7b ( 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 ) Extern templates ( \u6682\u65e0\u6559\u7a0b ) \u533f\u540d\u51fd\u6570 ( 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 ) \u548c \u95ed\u5305 ( 12.8 - lambda \u95ed\u5305 ) long long int ( 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 ) \u79fb\u52a8\u6784\u9020\u51fd\u6570 \u548c \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 ( M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c ) Noexcept \u8bf4\u660e\u7b26 (quick mention in 20.4 ) nullptr ( 9.7 - \u7a7a\u6307\u9488 ) override \u548c final \u8bf4\u660e\u7b26( 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b ) \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af ( 11.13 - for-each \u5faa\u73af ) \u53f3\u503c\u5f15\u7528 ( M.2 - \u53f3\u503c\u5f15\u7528 ) static_assert ( 7.17 - \u65ad\u8a00\u548c static_assert ) std::initializer_list ( 16.7 - std::initializer_list ) \u672b\u5c3e\u8fd4\u56de\u7c7b\u578b\u8bed\u6cd5( 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad ) \u7c7b\u578b\u522b\u540d ( 8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d ) typedef \u53ef\u4ee5\u7528\u4e8e\u6a21\u677f\u7c7b \u7edf\u4e00\u521d\u59cb\u5316 ( 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb ) \u81ea\u5b9a\u4e49\u5b57\u9762\u91cf ( \u6682\u65e0\u6559\u7a0b ) \u53ef\u53d8\u6a21\u677f(Variadic templates )( \u6682\u65e0\u6559\u7a0b ) Two >> symbols without a space between them will now properly be interpreted as closing a template object C++\u6807\u51c6\u5e93\u4e2d\u8fd8\u6709\u8bb8\u591a\u65b0\u7c7b\u53ef\u4f9b\u4f7f\u7528\u3002 \u66f4\u597d\u5730\u652f\u6301\u591a\u7ebf\u7a0b\u548c\u7ebf\u7a0b\u672c\u5730\u5b58\u50a8 ( \u6682\u65e0\u6559\u7a0b ) \u54c8\u5e0c\u8868 ( \u6682\u65e0\u6559\u7a0b ) \u6539\u8fdb\u968f\u673a\u6570\u751f\u6210(\u5728 7.19 - \u4f7f\u7528 Mersenne Twister \u751f\u6210\u968f\u673a\u6570 \u4e2d\u8fdb\u884c\u4e86\u8ba8\u8bba) \u5f15\u7528\u5305\u88c5 ( 18.9 - \u5bf9\u8c61\u5207\u7247 ) \u6b63\u5219\u8868\u8fbe\u5f0f ( \u6682\u65e0\u6559\u7a0b ) std::auto_ptr \u88ab\u5f03\u7528 ( M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb ) std::tuple ( \u6682\u65e0\u6559\u7a0b ) std::unique_ptr ( M.6 \u2014 std::unique_ptr )","title":"C++11\u4e2d\u7684\u91cd\u8981\u65b0\u7279\u6027"},{"location":"B-2-introduction-to-C%2B%2B14/","text":"Key Takeaway C++14 \u662f\u4ec0\u4e48\uff1f 2014\u5e748\u670818\u65e5\uff0c ISO (\u56fd\u9645\u6807\u51c6\u5316\u7ec4\u7ec7) \u901a\u8fc7\u4e86C++\u7684\u65b0\u7248\u672c\u2014\u2014C++14\u3002C++14\u6ca1\u6709\u50cfC++11\u90a3\u6837\u6dfb\u52a0\u5927\u91cf\u65b0\u7279\u6027\uff0c\u76f8\u5bf9\u6765\u8bf4\u5b83\u53ea\u8fdb\u884c\u4e86\u8f83\u5c11\u5730\u66f4\u65b0\uff0c\u4e3b\u8981\u4e13\u6ce8\u4e8e\u4fee\u6539bug\u548c\u6539\u8fdb\u8bed\u8a00\u3002 C++14 \u4e2d\u7684\u6539\u8fdb \u8fd9\u91cc\u662fC++14\u6dfb\u52a0\u7684\u4e3b\u8981\u6539\u8fdb\u7684\u5217\u8868\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e2a\u5217\u8868\u4e0d\u662f\u5168\u9762\u7684\uff0c\u800c\u662f\u65e8\u5728\u7a81\u51fa\u4e00\u4e9b\u611f\u5174\u8da3\u7684\u5173\u952e\u6539\u8fdb\u3002 \u805a\u5408\u6210\u5458\u521d\u59cb\u5316( 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 ) \u4e8c\u8fdb\u5236\u5b57\u9762\u91cf ( 4.15 - \u5b57\u9762\u91cf ) [[deprecated]] \u5c5e\u6027 ( \u6682\u65e0\u6559\u7a0b ) \u6570\u5b57\u5206\u9694\u7b26 ( 4.15 - \u5b57\u9762\u91cf ) \u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad( 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad ) \u6cdb\u578b lambdas ( 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 ) Relaxed constexpr functions ( 6.14 - Constexpr \u548c consteval \u51fd\u6570 ) \u53d8\u91cf\u6a21\u677f ( \u6682\u65e0\u6559\u7a0b ) std::make_unique ( M.6 \u2014 std::unique_ptr )","title":"B.2 - C++14"},{"location":"B-2-introduction-to-C%2B%2B14/#c14","text":"2014\u5e748\u670818\u65e5\uff0c ISO (\u56fd\u9645\u6807\u51c6\u5316\u7ec4\u7ec7) \u901a\u8fc7\u4e86C++\u7684\u65b0\u7248\u672c\u2014\u2014C++14\u3002C++14\u6ca1\u6709\u50cfC++11\u90a3\u6837\u6dfb\u52a0\u5927\u91cf\u65b0\u7279\u6027\uff0c\u76f8\u5bf9\u6765\u8bf4\u5b83\u53ea\u8fdb\u884c\u4e86\u8f83\u5c11\u5730\u66f4\u65b0\uff0c\u4e3b\u8981\u4e13\u6ce8\u4e8e\u4fee\u6539bug\u548c\u6539\u8fdb\u8bed\u8a00\u3002","title":"C++14 \u662f\u4ec0\u4e48\uff1f"},{"location":"B-2-introduction-to-C%2B%2B14/#c14_1","text":"\u8fd9\u91cc\u662fC++14\u6dfb\u52a0\u7684\u4e3b\u8981\u6539\u8fdb\u7684\u5217\u8868\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e2a\u5217\u8868\u4e0d\u662f\u5168\u9762\u7684\uff0c\u800c\u662f\u65e8\u5728\u7a81\u51fa\u4e00\u4e9b\u611f\u5174\u8da3\u7684\u5173\u952e\u6539\u8fdb\u3002 \u805a\u5408\u6210\u5458\u521d\u59cb\u5316( 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 ) \u4e8c\u8fdb\u5236\u5b57\u9762\u91cf ( 4.15 - \u5b57\u9762\u91cf ) [[deprecated]] \u5c5e\u6027 ( \u6682\u65e0\u6559\u7a0b ) \u6570\u5b57\u5206\u9694\u7b26 ( 4.15 - \u5b57\u9762\u91cf ) \u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad( 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad ) \u6cdb\u578b lambdas ( 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 ) Relaxed constexpr functions ( 6.14 - Constexpr \u548c consteval \u51fd\u6570 ) \u53d8\u91cf\u6a21\u677f ( \u6682\u65e0\u6559\u7a0b ) std::make_unique ( M.6 \u2014 std::unique_ptr )","title":"C++14 \u4e2d\u7684\u6539\u8fdb"},{"location":"B-3-introduction-to-C%2B%2B17/","text":"Key Takeaway C++17 \u662f\u4ec0\u4e48\uff1f 2017\u5e749\u6708\uff0c ISO (\u56fd\u9645\u6807\u51c6\u5316\u7ec4\u7ec7) \u901a\u8fc7\u4e86C++\u7684\u65b0\u7248\u672c\u2014\u2014C++17\u3002C++17\u5305\u542b\u4e86\u5927\u91cf\u7684\u66f4\u65b0\u3002 C++17 \u4e2d\u7684\u6539\u8fdb \u8fd9\u91cc\u662fC++17\u6dfb\u52a0\u7684\u4e3b\u8981\u6539\u8fdb\u7684\u5217\u8868\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e2a\u5217\u8868\u4e0d\u662f\u5168\u9762\u7684\uff0c\u800c\u662f\u65e8\u5728\u7a81\u51fa\u4e00\u4e9b\u611f\u5174\u8da3\u7684\u5173\u952e\u6539\u8fdb\u3002 __has_include \u9884\u5904\u7406\u5668\u6807\u8bc6\u7b26\uff0c\u68c0\u67e5\u5934\u6587\u4ef6\u662f\u5426\u5305\u542b( \u6682\u65e0\u6559\u7a0b ) \u7f16\u8bd1\u65f6\u6c42\u503c\u7684 if \u8bed\u53e5( \u6682\u65e0\u6559\u7a0b ) if \u548c switch \u8bed\u53e5\u4e2d\u7684\u521d\u59cb\u5316\u503c( \u6682\u65e0\u6559\u7a0b ) \u5185\u8054\u53d8\u91cf ( 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf ) \u9ad8\u9636\u51fd\u6570\u8868\u8fbe\u5f0f(Fold expressions) ( \u6682\u65e0\u6559\u7a0b ) \u5d4c\u5957\u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u901a\u8fc7 namespace X::Y \u6765\u5b9a\u4e49( 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 ) \u79fb\u9664 std::auto_ptr \u548c\u5176\u4ed6\u7684\u4e00\u4e9b\u5e9f\u5f03\u7c7b\u578b static_assert \u4e0d\u518d\u8981\u6c42\u8bca\u65ad\u4fe1\u606f\u53c2\u6570( 7.17 - \u65ad\u8a00\u548c static_assert ) std::any ( \u6682\u65e0\u6559\u7a0b ) std::byte ( \u6682\u65e0\u6559\u7a0b ) std::filesystem ( \u6682\u65e0\u6559\u7a0b ) std::optional ( \u6682\u65e0\u6559\u7a0b ) std::shared_ptr \u53ef\u7528\u4e8e\u7ba1\u7406C\u8bed\u8a00\u6570\u7ec4 (\u4f46\u8fd8\u4e0d\u80fd\u901a\u8fc7 std::make_shared \u521b\u5efa) ( M.7 \u2014 std::shared_ptr ) std::size ( 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 ) std::string_view ( 4.18 - std::string_view \u7b80\u4ecb ) \u7ed3\u6784\u5316\u7ed1\u5b9a\u58f0\u660e \u6784\u9020\u51fd\u6570\u7684\u6a21\u677f\u63a8\u65ad( \u6682\u65e0\u6559\u7a0b ) \u4e09\u5b57\u7b26\u7ec4\uff08Trigraphs\uff09\u88ab\u79fb\u9664 \u53ef\u4ee5\u5728\u6a21\u677f\u7684\u6a21\u677f\u53c2\u6570\u4e2d\u4f7f\u7528 typename \uff08\u800c\u4e0d\u662f class \uff09 UTF-8 (u8) \u5b57\u7b26\u5b57\u9762\u91cf( \u6682\u65e0\u6559\u7a0b )","title":"B.3 - C++17"},{"location":"B-3-introduction-to-C%2B%2B17/#c17","text":"2017\u5e749\u6708\uff0c ISO (\u56fd\u9645\u6807\u51c6\u5316\u7ec4\u7ec7) \u901a\u8fc7\u4e86C++\u7684\u65b0\u7248\u672c\u2014\u2014C++17\u3002C++17\u5305\u542b\u4e86\u5927\u91cf\u7684\u66f4\u65b0\u3002","title":"C++17 \u662f\u4ec0\u4e48\uff1f"},{"location":"B-3-introduction-to-C%2B%2B17/#c17_1","text":"\u8fd9\u91cc\u662fC++17\u6dfb\u52a0\u7684\u4e3b\u8981\u6539\u8fdb\u7684\u5217\u8868\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e2a\u5217\u8868\u4e0d\u662f\u5168\u9762\u7684\uff0c\u800c\u662f\u65e8\u5728\u7a81\u51fa\u4e00\u4e9b\u611f\u5174\u8da3\u7684\u5173\u952e\u6539\u8fdb\u3002 __has_include \u9884\u5904\u7406\u5668\u6807\u8bc6\u7b26\uff0c\u68c0\u67e5\u5934\u6587\u4ef6\u662f\u5426\u5305\u542b( \u6682\u65e0\u6559\u7a0b ) \u7f16\u8bd1\u65f6\u6c42\u503c\u7684 if \u8bed\u53e5( \u6682\u65e0\u6559\u7a0b ) if \u548c switch \u8bed\u53e5\u4e2d\u7684\u521d\u59cb\u5316\u503c( \u6682\u65e0\u6559\u7a0b ) \u5185\u8054\u53d8\u91cf ( 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf ) \u9ad8\u9636\u51fd\u6570\u8868\u8fbe\u5f0f(Fold expressions) ( \u6682\u65e0\u6559\u7a0b ) \u5d4c\u5957\u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u901a\u8fc7 namespace X::Y \u6765\u5b9a\u4e49( 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 ) \u79fb\u9664 std::auto_ptr \u548c\u5176\u4ed6\u7684\u4e00\u4e9b\u5e9f\u5f03\u7c7b\u578b static_assert \u4e0d\u518d\u8981\u6c42\u8bca\u65ad\u4fe1\u606f\u53c2\u6570( 7.17 - \u65ad\u8a00\u548c static_assert ) std::any ( \u6682\u65e0\u6559\u7a0b ) std::byte ( \u6682\u65e0\u6559\u7a0b ) std::filesystem ( \u6682\u65e0\u6559\u7a0b ) std::optional ( \u6682\u65e0\u6559\u7a0b ) std::shared_ptr \u53ef\u7528\u4e8e\u7ba1\u7406C\u8bed\u8a00\u6570\u7ec4 (\u4f46\u8fd8\u4e0d\u80fd\u901a\u8fc7 std::make_shared \u521b\u5efa) ( M.7 \u2014 std::shared_ptr ) std::size ( 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 ) std::string_view ( 4.18 - std::string_view \u7b80\u4ecb ) \u7ed3\u6784\u5316\u7ed1\u5b9a\u58f0\u660e \u6784\u9020\u51fd\u6570\u7684\u6a21\u677f\u63a8\u65ad( \u6682\u65e0\u6559\u7a0b ) \u4e09\u5b57\u7b26\u7ec4\uff08Trigraphs\uff09\u88ab\u79fb\u9664 \u53ef\u4ee5\u5728\u6a21\u677f\u7684\u6a21\u677f\u53c2\u6570\u4e2d\u4f7f\u7528 typename \uff08\u800c\u4e0d\u662f class \uff09 UTF-8 (u8) \u5b57\u7b26\u5b57\u9762\u91cf( \u6682\u65e0\u6559\u7a0b )","title":"C++17 \u4e2d\u7684\u6539\u8fdb"},{"location":"B-4-introduction-to-C%2B%2B20/","text":"Key Takeaway C++20 \u662f\u4ec0\u4e48\uff1f 2020\u5e742\u6708\uff0c ISO (\u56fd\u9645\u6807\u51c6\u5316\u7ec4\u7ec7) \u901a\u8fc7\u4e86C++\u7684\u65b0\u7248\u672c\u2014\u2014C++20\u3002C++20\u662f\u81eaC++11\u4ee5\u6765\uff0c\u5305\u542b\u6700\u591a\u53d8\u5316\u7684\u7248\u672c\u3002 C++20 \u7684\u6539\u8fdb \u8fd9\u91cc\u5217\u51fa\u4e86C++ 20\u589e\u52a0\u7684\u4e3b\u8981\u6539\u8fdb\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e2a\u5217\u8868\u4e0d\u662f\u5168\u9762\u7684\uff0c\u800c\u662f\u65e8\u5728\u7a81\u51fa\u4e00\u4e9b\u611f\u5174\u8da3\u7684\u5173\u952e\u6539\u8fdb\u70b9\u3002 \u901a\u8fc7 auto \u53c2\u6570\u652f\u6301 \u7f29\u5199\u51fd\u6570\u6a21\u677f ( 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f ) Chrono \u6269\u5c55\u7528\u4e8e\u652f\u6301\u65e5\u5386\u548c\u65f6\u533a( \u6682\u65e0\u6559\u7a0b ) Concepts\uff0c\u7528\u4e8e\u5bf9\u6a21\u677f\u53c2\u6570\u8fdb\u884c\u9650\u5236 ( \u6682\u65e0\u6559\u7a0b ) Constexpr virtual functions, unions , try , catch , dynamic_cast , and typeid ( \u6682\u65e0\u6559\u7a0b ) Constinit \u5173\u952e\u5b57\uff0c\u7528\u4e8e\u65ad\u8a00\u67d0\u4e2a\u53d8\u91cf\u88ab\u9759\u6001\u521d\u59cb\u5316( \u6682\u65e0\u6559\u7a0b ) \u534f\u7a0b ( \u6682\u65e0\u6559\u7a0b ) \u6307\u5b9a\u521d\u59cb\u5316 ( 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 ) \u4f7f\u7528 consteval \u521b\u5efa\u5373\u65f6\u51fd\u6570 ( 6.14 - Constexpr \u548c consteval \u51fd\u6570 ) \u6a21\u5757\uff0c\u4f5c\u4e3a #include \u7684\u66ff\u4ee3\u54c1( \u6682\u65e0\u6559\u7a0b ) Ranges ( \u6682\u65e0\u6559\u7a0b ) std::erase ( \u6682\u65e0\u6559\u7a0b ) std::make_shared for arrays ( \u6682\u65e0\u6559\u7a0b ) std::map::contains() ( \u6682\u65e0\u6559\u7a0b ) std::span ( \u6682\u65e0\u6559\u7a0b ) \u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u5e93( \u6682\u65e0\u6559\u7a0b \uff0c\u53c2\u89c1 https://en.cppreference.com/w/cpp/utility/format ) \u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u4f5c\u4e3a\u6a21\u677f\u53c2\u6570 ( \u6682\u65e0\u6559\u7a0b ) \u4f7f\u7528\u98de\u8239\u8fd0\u7b97\u7b26 <=> \u8fdb\u884c\u4e09\u8def\u6bd4\u8f83( \u6682\u65e0\u6559\u7a0b ) Using scoped enums ( \u6682\u65e0\u6559\u7a0b ) Views ( \u6682\u65e0\u6559\u7a0b )","title":"B.4 - C++20"},{"location":"B-4-introduction-to-C%2B%2B20/#c20","text":"2020\u5e742\u6708\uff0c ISO (\u56fd\u9645\u6807\u51c6\u5316\u7ec4\u7ec7) \u901a\u8fc7\u4e86C++\u7684\u65b0\u7248\u672c\u2014\u2014C++20\u3002C++20\u662f\u81eaC++11\u4ee5\u6765\uff0c\u5305\u542b\u6700\u591a\u53d8\u5316\u7684\u7248\u672c\u3002","title":"C++20 \u662f\u4ec0\u4e48\uff1f"},{"location":"B-4-introduction-to-C%2B%2B20/#c20_1","text":"\u8fd9\u91cc\u5217\u51fa\u4e86C++ 20\u589e\u52a0\u7684\u4e3b\u8981\u6539\u8fdb\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e2a\u5217\u8868\u4e0d\u662f\u5168\u9762\u7684\uff0c\u800c\u662f\u65e8\u5728\u7a81\u51fa\u4e00\u4e9b\u611f\u5174\u8da3\u7684\u5173\u952e\u6539\u8fdb\u70b9\u3002 \u901a\u8fc7 auto \u53c2\u6570\u652f\u6301 \u7f29\u5199\u51fd\u6570\u6a21\u677f ( 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f ) Chrono \u6269\u5c55\u7528\u4e8e\u652f\u6301\u65e5\u5386\u548c\u65f6\u533a( \u6682\u65e0\u6559\u7a0b ) Concepts\uff0c\u7528\u4e8e\u5bf9\u6a21\u677f\u53c2\u6570\u8fdb\u884c\u9650\u5236 ( \u6682\u65e0\u6559\u7a0b ) Constexpr virtual functions, unions , try , catch , dynamic_cast , and typeid ( \u6682\u65e0\u6559\u7a0b ) Constinit \u5173\u952e\u5b57\uff0c\u7528\u4e8e\u65ad\u8a00\u67d0\u4e2a\u53d8\u91cf\u88ab\u9759\u6001\u521d\u59cb\u5316( \u6682\u65e0\u6559\u7a0b ) \u534f\u7a0b ( \u6682\u65e0\u6559\u7a0b ) \u6307\u5b9a\u521d\u59cb\u5316 ( 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 ) \u4f7f\u7528 consteval \u521b\u5efa\u5373\u65f6\u51fd\u6570 ( 6.14 - Constexpr \u548c consteval \u51fd\u6570 ) \u6a21\u5757\uff0c\u4f5c\u4e3a #include \u7684\u66ff\u4ee3\u54c1( \u6682\u65e0\u6559\u7a0b ) Ranges ( \u6682\u65e0\u6559\u7a0b ) std::erase ( \u6682\u65e0\u6559\u7a0b ) std::make_shared for arrays ( \u6682\u65e0\u6559\u7a0b ) std::map::contains() ( \u6682\u65e0\u6559\u7a0b ) std::span ( \u6682\u65e0\u6559\u7a0b ) \u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u5e93( \u6682\u65e0\u6559\u7a0b \uff0c\u53c2\u89c1 https://en.cppreference.com/w/cpp/utility/format ) \u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u4f5c\u4e3a\u6a21\u677f\u53c2\u6570 ( \u6682\u65e0\u6559\u7a0b ) \u4f7f\u7528\u98de\u8239\u8fd0\u7b97\u7b26 <=> \u8fdb\u884c\u4e09\u8def\u6bd4\u8f83( \u6682\u65e0\u6559\u7a0b ) Using scoped enums ( \u6682\u65e0\u6559\u7a0b ) Views ( \u6682\u65e0\u6559\u7a0b )","title":"C++20 \u7684\u6539\u8fdb"},{"location":"Binding/","tags":["\u8bcd\u6c47\u8868","Binding"],"text":"","title":"\u7ed1\u5b9a"},{"location":"C-style-casts/","tags":["\u8bcd\u6c47\u8868","C-style-casts"],"text":"","title":"C \u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362"},{"location":"Composition/","text":"","title":"Composition"},{"location":"Early-binding/","tags":["\u8bcd\u6c47\u8868","Early binding"],"text":"\u7f16\u8bd1\u5668(\u6216\u94fe\u63a5\u5668)\u80fd\u591f\u76f4\u63a5\u5c06\u6807\u8bc6\u7b26\u540d(\u5982\u51fd\u6570\u540d\u6216\u53d8\u91cf\u540d)\u4e0e\u673a\u5668\u5730\u5740\u5173\u8054\u8d77\u6765 \u533a\u522b \u540e\u671f\u7ed1\u5b9a","title":"\u65e9\u671f\u7ed1\u5b9a"},{"location":"Encapsulation/","tags":["\u8bcd\u6c47\u8868","Encapsulation"],"text":"","title":"\u5c01\u88c5"},{"location":"Expression-parameters/","tags":["\u8bcd\u6c47\u8868","Expression-parameters"],"text":"","title":"\u8868\u8fbe\u5f0f\u53c2\u6570"},{"location":"Frankenobject/","tags":["\u8bcd\u6c47\u8868","Frankenobject"],"text":"","title":"\u7f1d\u5408\u602a\u5bf9\u8c61"},{"location":"Late-binding/","tags":["\u8bcd\u6c47\u8868","Late binding"],"text":"","title":"\u540e\u671f\u7ed1\u5b9a"},{"location":"M-1-introduction-to-smart-pointers-and-move-semantics/","tags":["smart-pointers","move-semantics"],"text":"Key Takeaway \u79fb\u52a8\u8bed\u4e49\u548c\u62f7\u8d1d\u8bed\u4e49\u662f\u4e8c\u9009\u4e00\u7684\uff0c\u53ea\u6709\u5728\u53ef\u80fd\u53d1\u751f\u62f7\u8d1d\u8bed\u4e49\u7684\u5730\u65b9\uff0c\u624d\u5e94\u8be5\u8003\u8651\u5b83\u6267\u884c\u7684\u662f\u79fb\u52a8\u8bed\u4e49\u8fd8\u662f\u62f7\u8d1d\u8bed\u4e49\uff08\u53d6\u51b3\u4e8e\u53f3\u503c\u8fd8\u662f\u5de6\u503c\uff09 \u8003\u8651\u4e0b\u9762\u51fd\u6570\uff0c\u51fd\u6570\u4e2d\u52a8\u6001\u5206\u914d\u4e86\u4e00\u4e2a\u8d44\u6e90\uff1a 1 2 3 4 5 6 7 8 void someFunction () { Resource * ptr = new Resource (); // Resource is a struct or class // do stuff with ptr here delete ptr ; } \u5c3d\u7ba1\u4e0a\u9762\u7684\u4ee3\u7801\u770b\u8d77\u6765\u76f8\u5f53\u7b80\u5355\uff0c\u4f46\u662f\u5728\u5b9e\u8df5\u4e2d\u5f88\u5bb9\u6613\u5fd8\u8bb0\u91ca\u653e ptr \u3002\u5373\u4f7f\u4f60\u8bb0\u5f97\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u5220\u9664 ptr \uff0c\u5982\u679c\u51fd\u6570\u63d0\u524d\u9000\u51fa\uff0c\u4e5f\u6709\u8bb8\u591a\u56e0\u7d20\u5bfc\u81f4 ptr \u4e0d\u80fd\u88ab\u6b63\u786e\u5220\u9664\uff0c\u4f8b\u5982\u51fd\u6570\u7684\u63d0\u524d\u8fd4\u56de\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void someFunction () { Resource * ptr = new Resource (); int x ; std :: cout << \"Enter an integer: \" ; std :: cin >> x ; if ( x == 0 ) return ; // \u51fd\u6570\u63d0\u524d\u8fd4\u56de\uff0cptr \u65e0\u6cd5\u88ab\u5220\u9664\uff01 // do stuff with ptr here delete ptr ; } \u629b\u51fa\u5f02\u5e38\u4e5f\u53ef\u80fd\u4f1a\u5bfc\u81f4\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void someFunction () { Resource * ptr = new Resource (); int x ; std :: cout << \"Enter an integer: \" ; std :: cin >> x ; if ( x == 0 ) throw 0 ; // the function returns early, and ptr won\u2019t be deleted! // do stuff with ptr here delete ptr ; } \u5728\u4e0a\u8ff0\u4e24\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u63d0\u524d\u8fd4\u56de\u6216throw\u8bed\u53e5\uff0c\u90fd\u53ef\u80fd\u4f1a\u5bfc\u81f4\u51fd\u6570\u5728\u6ca1\u6709\u5220\u9664 ptr \u7684\u60c5\u51b5\u4e0b\u7ec8\u6b62\u3002\u56e0\u6b64\uff0c\u5206\u914d\u7ed9\u53d8\u91cf ptr \u7684\u5185\u5b58\u53ef\u80fd\u51fa\u73b0\u5185\u5b58\u6cc4\u6f0f(\u800c\u4e14\u662f\u6bcf\u6b21\u8c03\u7528\u6b64\u51fd\u6570\u5e76\u63d0\u524d\u8fd4\u56de\u65f6\u90fd\u4f1a\u6cc4\u6f0f)\u3002 \u672c\u8d28\u4e0a\uff0c\u53d1\u751f\u8fd9\u7c7b\u95ee\u9898\u662f\u56e0\u4e3a\u6307\u9488\u53d8\u91cf\u4e0d\u5177\u5907\u81ea\u6211\u6e05\u7406\u7684\u80fd\u529b\u3002 \u667a\u80fd\u6307\u9488\u7c7b\u53ef\u4ee5\u62ef\u6551\u6211\u4eec\u5417\uff1f \u5bf9\u4e8e\u7c7b\u5bf9\u8c61\u6765\u8bf4\uff0c\u5b83\u4eec\u6700\u5927\u7684\u4f18\u52bf\u662f\u53ef\u4ee5\u5728 \u79bb\u5f00\u4f5c\u7528\u57df \u662f\u81ea\u52a8\u6267\u884c \u6790\u6784\u51fd\u6570 \u3002\u8fd9\u6837\u4e00\u6765\uff0c\u5982\u679c\u6211\u4eec\u5728\u6784\u9020\u51fd\u6570\u4e2d\u5206\u914d\u6216\u83b7\u53d6\u5185\u5b58\uff0c\u5219\u53ef\u4ee5\u5728\u6790\u6784\u51fd\u6570\u4e2d\u8fdb\u884c\u91ca\u653e\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u786e\u4fdd\u5185\u5b58\u5728\u5bf9\u8c61\u88ab\u9500\u6bc1\u524d\uff08\u4e0d\u8bba\u662f\u79bb\u5f00\u4f5c\u7528\u57df\u8fd8\u662f\u663e\u5f0f\u5220\u9664\u7b49\uff09\u91ca\u653e\u3002\u8fd9\u4e5f\u6b63\u662f \u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09 \u7f16\u7a0b\u8303\u5f0f\u7684\u6838\u5fc3\u601d\u60f3\uff08\u89c1 13.9 - \u6790\u6784\u51fd\u6570 \uff09\u3002 \u90a3\u4e48\uff0c\u662f\u5426\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7c7b\uff0c\u5e2e\u52a9\u6211\u4eec\u7ba1\u7406\u548c\u6e05\u7406\u6307\u9488\u5462\uff1f\u5f53\u7136\u53ef\u4ee5\uff01 \u8003\u8651\u8bbe\u8ba1\u8fd9\u6837\u4e00\u4e2a\u7c7b\uff0c\u5b83\u7684\u552f\u4e00\u5de5\u4f5c\uff0c\u5c31\u662f\u7ba1\u7406\u4e00\u4e2a\u4f20\u5165\u7684\u6307\u9488\u3002\u5f53\u8be5\u7c7b\u5bf9\u8c61\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c\u91ca\u653e\u6307\u9488\u6240\u6307\u5411\u7684\u5185\u5b58\u3002\u53ea\u8981\u8be5\u5bf9\u8c61\u88ab\u58f0\u660e\u4e3a\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4fdd\u8bc1\uff0c\u5f53\u5b83\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff08\u4e0d\u8bba\u4f55\u65f6\u3001\u4e5f\u4e0d\u8bba\u51fd\u6570\u5982\u4f55\u7ec8\u6b62\uff09\uff0c\u5b83\u7ba1\u7406\u7684\u6307\u9488\u5c31\u4f1a\u88ab\u9500\u6bc1\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #include template < typename T > class Auto_ptr1 { T * m_ptr ; public : // \u901a\u8fc7\u6784\u9020\u51fd\u6570\u4f20\u5165\u4e00\u4e2a\u6307\u9488\uff0c\u4f7f\u8be5\u7c7b\u62e5\u6709\u5b83 Auto_ptr1 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } // \u6790\u6784\u51fd\u6570\u4f1a\u786e\u4fdd\u6307\u9488\u88ab\u5220\u9664 ~ Auto_ptr1 () { delete m_ptr ; } // \u91cd\u8f7d\u89e3\u5f15\u7528\u548c\u6210\u5458\u8fd0\u7b97\u7b26 T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } }; // A sample class to prove the above works class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { Auto_ptr1 < Resource > res ( new Resource ()); // \u6ce8\u610f\u6b64\u5904\u5206\u914d\u7684\u8d44\u6e90 // ... \u6ca1\u6709\u663e\u5f0f\u5220\u9664\u6307\u9488 // \u6ce8\u610f\uff0c\u5c16\u62ec\u53f7\u4e2d\u7684Resource\u4e0d\u9700\u8981\u661f\u53f7\uff0c\u661f\u53f7\u662f\u6a21\u677f\u63d0\u4f9b\u7684 return 0 ; } // res \u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u5e76\u4e14\u5220\u9664\u4e86\u8d44\u6e90 \u7a0b\u5e8f\u6253\u5370\uff1a 1 2 Resource acquired Resource destroyed \u8003\u8651\u4e00\u4e0b\u8fd9\u4e2a\u7a0b\u5e8f\u548c\u7c7b\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u9996\u5148\uff0c\u52a8\u6001\u521b\u5efa Resource \uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u6a21\u677f\u5316\u7684 Auto_ptr1 \u7c7b\u3002\u4ece\u8fd9\u4e00\u523b\u5f00\u59cb\uff0c Auto_ptr1 \u53d8\u91cf res \u62e5\u6709\u8be5\u8d44\u6e90\u5bf9\u8c61( Auto_ptr1 \u662f m_ptr \u662f \u7ec4\u5408\u5173\u7cfb )\u3002\u56e0\u4e3a res \u88ab\u58f0\u660e\u4e3a\u5c40\u90e8\u53d8\u91cf\u5e76\u5177\u6709\u8bed\u53e5\u5757\u4f5c\u7528\u57df\uff0c\u6240\u4ee5\u5f53\u8bed\u53e5\u5757\u7ed3\u675f\u65f6\uff0c\u5b83\u5c06\u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1(\u4e0d\u7528\u62c5\u5fc3\u5fd8\u8bb0\u91ca\u653e\u5b83)\u3002\u7531\u4e8e\u5b83\u662f\u4e00\u4e2a\u7c7b\uff0c\u56e0\u6b64\u5728\u9500\u6bc1\u5b83\u65f6\uff0c\u5c06\u8c03\u7528 Auto_ptr1 \u6790\u6784\u51fd\u6570\u3002\u8be5\u6790\u6784\u51fd\u6570\u5c06\u786e\u4fdd\u5b83\u6240\u6301\u6709\u7684 Resource \u6307\u9488\u88ab\u5220\u9664! \u53ea\u8981 Auto_ptr1 \u88ab\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf(\u5177\u6709\u81ea\u52a8\u6301\u7eed\u65f6\u95f4\uff0c\u56e0\u6b64\u7c7b\u540d\u4e2d\u6709\u201cAuto\u201d\u90e8\u5206)\uff0c\u65e0\u8bba\u51fd\u6570\u5982\u4f55\u7ec8\u6b62(\u5373\u4f7f\u5b83\u63d0\u524d\u7ec8\u6b62)\uff0c Resource \u603b\u662f\u53ef\u4ee5\u5728\u58f0\u660e\u5b83\u7684\u5757\u7684\u672b\u5c3e\u88ab\u9500\u6bc1\u3002 \u8fd9\u79cd\u7c7b\u88ab\u79f0\u4e3a\u667a\u80fd\u6307\u9488\u3002 \u667a\u80fd\u6307\u9488\u7c7b \u662f\u4e00\u79cd\u7ec4\u5408\u7c7b\uff0c\u5b83\u7528\u4e8e\u7ba1\u7406\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\uff0c\u5e76\u4e14\u80fd\u591f\u786e\u4fdd\u6307\u9488\u5bf9\u8c61\u5728\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\u88ab\u5220\u9664\uff08\u5185\u7f6e\u7684\u6307\u9488\u5bf9\u8c61\u88ab\u79f0\u4e3a\u201c\u7b28\u6307\u9488\u201d\uff0c\u6b63\u662f\u56e0\u4e3a\u5b83\u4eec\u4e0d\u80fd\u81ea\u5df1\u6e05\u7406\u81ea\u5df1\u6240\u7ba1\u7406\u7684\u5185\u5b58\uff09\u3002 \u73b0\u5728\u8ba9\u6211\u4eec\u56de\u5230\u4e0a\u9762\u7684 someFunction() \u4f8b\u5b50\uff0c\u770b\u770b\u5982\u4f55\u4f7f\u7528\u667a\u80fd\u6307\u9488\u89e3\u51b3\u6211\u4eec\u9762\u4e34\u7684\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 #include template < typename T > class Auto_ptr1 { T * m_ptr ; public : // Pass in a pointer to \"own\" via the constructor Auto_ptr1 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } // The destructor will make sure it gets deallocated ~ Auto_ptr1 () { delete m_ptr ; } // Overload dereference and operator-> so we can use Auto_ptr1 like m_ptr. T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } }; // A sample class to prove the above works class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } void sayHi () { std :: cout << \"Hi! \\n \" ; } }; void someFunction () { Auto_ptr1 < Resource > ptr ( new Resource ()); // ptr \u73b0\u5728\u62e5\u6709 Resource int x ; std :: cout << \"Enter an integer: \" ; std :: cin >> x ; if ( x == 0 ) return ; // \u51fd\u6570\u63d0\u524d\u8fd4\u56de // do stuff with ptr here ptr -> sayHi (); } int main () { someFunction (); return 0 ; } \u5982\u679c\u7528\u6237\u8f93\u5165\u975e0\uff0c\u5219\u7a0b\u5e8f\u6253\u5370\uff1a 1 2 3 Resource acquired Hi! Resource destroyed \u5982\u679c\u7528\u6237\u8f93\u51650\uff0c\u5219\u7a0b\u5e8f\u4f1a\u63d0\u524d\u9000\u51fa\uff0c\u6253\u5370\uff1a 1 2 Resource acquired Resource destroyed \u6ce8\u610f\uff0c\u5373\u4f7f\u5728\u7528\u6237\u8f93\u5165\u96f6\u4e14\u51fd\u6570\u63d0\u524d\u7ec8\u6b62\u7684\u60c5\u51b5\u4e0b\uff0c\u8d44\u6e90\u4ecd\u7136\u88ab\u6b63\u786e\u5730\u91ca\u653e\u3002 \u56e0\u4e3a ptr \u53d8\u91cf\u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u6240\u4ee5 ptr \u5c06\u5728\u51fd\u6570\u7ec8\u6b62\u65f6\u88ab\u9500\u6bc1(\u4e0d\u7ba1\u5b83\u4ee5\u4f55\u79cd\u65b9\u5f0f\u7ec8\u6b62)\u3002\u56e0\u4e3a Auto_ptr1 \u6790\u6784\u51fd\u6570\u5c06\u6e05\u7406 Resource \uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4fdd\u8bc1 Resource \u5c06\u88ab\u6b63\u786e\u5730\u6e05\u7406\u3002 \u4e25\u91cd\u7f3a\u9677 Auto_ptr1 \u7c7b\u6709\u4e00\u4e2a\u4e25\u91cd\u7684\u7f3a\u9677\uff0c\u662f\u7531\u81ea\u52a8\u751f\u6210\u7684\u4ee3\u7801\u5f15\u8d77\u7684\u3002\u4f60\u80fd\u770b\u51fa\u6765\u5417\uff1f (\u63d0\u793a\uff1a\u5982\u679c\u7f16\u8bd1\u5668\u4f1a\u4e3a\u7c7b\u63d0\u4f9b\u54ea\u4e9b\u9ed8\u8ba4\u7684\u51fd\u6570\uff1f) \u65f6\u95f4\u5230\uff01 \u731c\u5230\u4e86\u5417\uff1f\u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include // Same as above template < typename T > class Auto_ptr1 { T * m_ptr ; public : Auto_ptr1 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } ~ Auto_ptr1 () { delete m_ptr ; } T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } }; class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { Auto_ptr1 < Resource > res1 ( new Resource ()); Auto_ptr1 < Resource > res2 ( res1 ); // \u6ca1\u6709\u521d\u59cb\u5316res2\uff0c\u800c\u662f\u8fdb\u884c\u8d4b\u503c return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 Resource acquired Resource destroyed Resource destroyed \u7a0b\u5e8f\u5f88\u53ef\u80fd(\u4f46\u4e0d\u4e00\u5b9a)\u5728\u6b64\u65f6\u5d29\u6e83\u3002\u53d1\u73b0\u95ee\u9898\u7684\u6240\u5728\u4e86\u5417\uff1f\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u63d0\u4f9b\u590d\u5236\u6784\u9020\u51fd\u6570\u6216\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u6240\u4ee5C++\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u4e2a\u3002\u5b83\u63d0\u4f9b\u7684\u51fd\u6570\u53ea\u505a\u6d45\u62f7\u8d1d\u3002\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u7528 res1 \u521d\u59cb\u5316 res2 \u65f6\uff0c\u4e24\u4e2a Auto_ptr1 \u53d8\u91cf\u90fd\u6307\u5411\u540c\u4e00\u4e2a Resource \u3002\u5f53 res2 \u8d85\u51fa\u4f5c\u7528\u57df\u65f6\uff0c\u5b83\u5220\u9664\u8d44\u6e90\uff0c\u7ed9 res1 \u7559\u4e0b\u4e00\u4e2a \u60ac\u5782 \u6307\u9488\u3002\u5f53 res1 \u53bb\u5220\u9664\u5b83\u7684(\u5df2\u7ecf\u5220\u9664\u7684)\u8d44\u6e90\u65f6\uff0c\u7a0b\u5e8f\u5d29\u6e83! \u4e0b\u9762\u7684\u4ee3\u7801\u4e5f\u4f1a\u51fa\u73b0\u7c7b\u4f3c\u7684\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 void passByValue ( Auto_ptr1 < Resource > res ) { } int main () { Auto_ptr1 < Resource > res1 ( new Resource ()); passByValue ( res1 ); return 0 ; } \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c res1 \u5c06\u6309\u503c\u590d\u5236\u5230 passByValue \u7684\u53c2\u6570 res \u4e2d\uff0c\u5bfc\u81f4 Resource \u6307\u9488\u88ab\u590d\u5236\uff0c\u8fdb\u800c\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002 \u663e\u7136\u8fd9\u4e0d\u662f\u4ec0\u4e48\u597d\u4e8b\u3002\u6211\u4eec\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898? \u4e00\u79cd\u65b9\u6cd5\u662f\u663e\u5f0f\u5730\u5b9a\u4e49\u548c\u5220\u9664\u590d\u5236\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u4ece\u800c\u4ece\u6839\u672c\u4e0a\u963b\u6b62\u62f7\u8d1d\u3002\u8fd9\u4f1a\u963b\u6b62\u6309\u503c\u4f20\u9012\u7684\u60c5\u51b5(\u8fd9\u5f88\u597d\uff0c\u56e0\u4e3a\u6211\u4eec\u672c\u6765\u5c31\u4e0d\u5e94\u8be5\u6309\u503c\u4f20\u9012\u5b83\u4eec)\u3002 \u4f46\u662f\uff0c\u6211\u4eec\u5982\u4f55\u4ece\u51fd\u6570\u8fd4\u56de Auto_ptr1 \u7ed9\u8c03\u7528\u8005\u5462? 1 2 3 4 5 ??? generateResource () { Resource * r { new Resource () }; return Auto_ptr1 ( r ); } \u6211\u4eec\u65e0\u6cd5\u5c06 Auto_ptr1 \u6309\u5f15\u7528\u8fd4\u56de \uff0c\u56e0\u4e3a\u5c40\u90e8\u53d8\u91cf Auto_ptr1 \u4f1a\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u5220\u9664\uff0c\u6240\u4ee5\u4e3b\u8c03\u51fd\u6570\u5f97\u5230\u7684\u662f\u4e00\u4e2a\u60ac\u5782\u5f15\u7528\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u6307\u9488r\u8fd4\u56de\u4e3a Resource* \uff0c\u4f46\u662f\u6211\u4eec\u53ef\u80fd\u4f1a\u5728\u540e\u9762\u5fd8\u8bb0\u5220\u9664 r \uff0c\u8fd9\u5c31\u8fdd\u80cc\u4e86\u4f7f\u7528\u667a\u80fd\u6307\u9488\u7684\u521d\u8877\u3002\u6240\u4ee5\uff0c\u6309\u503c\u8fd4\u56de Auto_ptr1 \u662f\u552f\u4e00\u6709\u610f\u4e49\u7684\u9009\u9879\u2014\u2014\u4f46\u6211\u4eec\u6700\u7ec8\u4f1a\u5f97\u5230\u6d45\u62f7\u8d1d\u3001\u91cd\u590d\u7684\u6307\u9488\u5e76\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002 \u53e6\u4e00\u79cd\u9009\u62e9\u662f\u91cd\u5199 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\u6765\u8fdb\u884c \u6df1\u62f7\u8d1d \u3002\u8fd9\u6837\uff0c\u6211\u4eec\u81f3\u5c11\u53ef\u4ee5\u907f\u514d\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684\u91cd\u590d\u6307\u9488\u3002\u4f46\u662f\u62f7\u8d1d\u7684\u5f00\u9500\u53ef\u80fd\u4f1a\u5f88\u5927(\u800c\u4e14\u53ef\u80fd\u4e0d\u9700\u8981\u751a\u81f3\u4e0d\u53ef\u80fd)\uff0c\u800c\u4e14\u6211\u4eec\u4e0d\u5e0c\u671b\u4ec5\u4ec5\u4e3a\u4e86\u4ece\u51fd\u6570\u8fd4\u56de Auto_ptr1 \u800c\u5bf9\u5bf9\u8c61\u8fdb\u884c\u4e0d\u5fc5\u8981\u7684\u590d\u5236\u3002\u53e6\u5916\uff0c\u5bf9\u666e\u901a\u6307\u9488\u8d4b\u503c\u6216\u521d\u59cb\u5316\u65f6\u5e76\u4e0d\u4f1a\u62f7\u8d1d\u5bf9\u8c61\uff0c\u4e3a\u4ec0\u4e48\u667a\u80fd\u6307\u9488\u8981\u5177\u6709\u4e0d\u540c\u7684\u903b\u8f91\u5462\uff1f \u8be5\u600e\u4e48\u505a\u5462\uff1f \u79fb\u52a8\u8bed\u4e49 \u5982\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba9\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u8fd0\u7b97\u7b26\u4e0d\u53bb\u62f7\u8d1d\u6307\u9488\uff08\u62f7\u8d1d\u8bed\u4e49\uff09\uff0c\u800c\u662f\u5c06\u5b83\u6240\u7ba1\u7406\u7684\u8d44\u6e90\u4f20\u9012\u6216\u79fb\u52a8\u7ed9\u76ee\u6807\u6307\u9488\u5462\uff1f\u8fd9\u6b63\u662f \u79fb\u52a8\u8bed\u4e49 \u80cc\u540e\u7684\u6838\u5fc3\u601d\u60f3\u3002\u79fb\u52a8\u8bed\u4e49\u8bf4\u7684\u5c31\u662f\u5bf9\u8c61\u6240\u6709\u6743\u53d1\u751f\u4e86\u8f6c\u79fb\u800c\u4e0d\u662f\u62f7\u8d1d\u3002 \u66f4\u65b0\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 #include template < typename T > class Auto_ptr2 { T * m_ptr ; public : Auto_ptr2 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } ~ Auto_ptr2 () { delete m_ptr ; } // \u4f7f\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49 Auto_ptr2 ( Auto_ptr2 & a ) // \u6ce8\u610f: \u4e0d\u80fd\u662f const { m_ptr = a . m_ptr ; // \u76ee\u7684\u5bf9\u8c61\u83b7\u5f97\u6e90\u5bf9\u8c61\u7684\u6307\u9488 a . m_ptr = nullptr ; // \u786e\u4fdd\u6e90\u5bf9\u8c61\u4e0d\u518d\u62e5\u6709\u8be5\u6307\u9488 } // \u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49 Auto_ptr2 & operator = ( Auto_ptr2 & a ) // \u6ce8\u610f: \u4e0d\u80fd\u662f const { if ( & a == this ) return * this ; delete m_ptr ; // \u786e\u4fdd\u76ee\u7684\u5bf9\u8c61\u7684\u6307\u9488\u6240\u6307\u7684\u8d44\u6e90\u88ab\u91ca\u653e m_ptr = a . m_ptr ; // \u76ee\u7684\u5bf9\u8c61\u83b7\u5f97\u6e90\u5bf9\u8c61\u7684\u6307\u9488 a . m_ptr = nullptr ; // \u786e\u4fdd\u6e90\u5bf9\u8c61\u4e0d\u518d\u62e5\u6709\u8be5\u6307\u9488 return * this ; } T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } bool isNull () const { return m_ptr == nullptr ; } }; class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { Auto_ptr2 < Resource > res1 ( new Resource ()); Auto_ptr2 < Resource > res2 ; // Start as nullptr std :: cout << \"res1 is \" << ( res1 . isNull () ? \"null \\n \" : \"not null \\n \" ); std :: cout << \"res2 is \" << ( res2 . isNull () ? \"null \\n \" : \"not null \\n \" ); res2 = res1 ; // res2 assumes ownership, res1 is set to null std :: cout << \"Ownership transferred \\n \" ; std :: cout << \"res1 is \" << ( res1 . isNull () ? \"null \\n \" : \"not null \\n \" ); std :: cout << \"res2 is \" << ( res2 . isNull () ? \"null \\n \" : \"not null \\n \" ); return 0 ; } \u6253\u5370\uff1a 1 2 3 4 5 6 7 Resource acquired res1 is not null res2 is null Ownership transferred res1 is null res2 is not null Resource destroyed \u6ce8\u610f\uff0c\u6211\u4eec\u91cd\u8f7d\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u73b0\u5728\u53ef\u4ee5\u628a m_ptr \u7684\u6240\u6709\u6743\u4ece res1 \u4f20\u9012\u7ed9 res2 \uff01\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c06\u4e0d\u4f1a\u5f97\u5230\u91cd\u590d\u7684\u6307\u9488\uff0c\u8d44\u6e90\u6e05\u7406\u53ef\u4ee5\u987a\u5229\u8fdb\u884c\u3002 std::auto_ptr \u4e3a\u4ec0\u4e48\u662f\u7cdf\u7cd5\u7684\u5b9e\u73b0 \u73b0\u5728\u662f\u8ba8\u8bba std::auto_ptr \u7684\u5408\u9002\u65f6\u673a\u4e86\u3002 std::auto_ptr \u5728C++ 98\u4e2d\u5f15\u5165\uff0c\u5728C++ 17\u4e2d\u88ab\u5220\u9664\uff0c\u5b83\u662fC++\u7b2c\u4e00\u6b21\u5c1d\u8bd5\u6807\u51c6\u5316\u667a\u80fd\u6307\u9488\u3002 auto_ptr \u9009\u62e9\u4e86 Auto_ptr2 \u7c7b\u4e00\u6837\u7684\u79fb\u52a8\u8bed\u4e49\u5b9e\u73b0\u65b9\u5f0f\u3002 \u7136\u800c\uff0c std::auto_ptr (\u4ee5\u53ca\u6211\u4eec\u7684 Auto_ptr2 \u7c7b)\u6709\u8bb8\u591a\u95ee\u9898\uff0c\u4f7f\u5f97\u4f7f\u7528\u5b83\u5f88\u5371\u9669\u3002 \u9996\u5148\uff0c\u7531\u4e8e std::auto_ptr \u901a\u8fc7\u590d\u5236\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\u5b9e\u73b0\u4e86\u79fb\u52a8\u8bed\u4e49\uff0c\u5c06 std::auto_ptr \u6309\u503c\u4f20\u9012\u7ed9\u51fd\u6570\u5c06\u5bfc\u81f4\u8d44\u6e90\u88ab\u79fb\u52a8\u5230\u51fd\u6570\u5f62\u53c2\u4e2d(\u5e76\u5728\u51fd\u6570\u5f62\u53c2\u8d85\u51fa\u4f5c\u7528\u57df\u65f6\u9500\u6bc1)\u3002\u7136\u540e\uff0c\u5f53\u4f60\u4ece\u8c03\u7528\u8005\u8bbf\u95ee auto_ptr \u5b9e\u53c2\u65f6(\u6ca1\u6709\u610f\u8bc6\u5230\u5b83\u5df2\u7ecf\u88ab\u79fb\u52a8\u548c\u5220\u9664\u4e86)\u65f6\uff0c\u8fdb\u884c \u89e3\u5f15\u7528 \u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002 \u5176\u6b21\uff0c std::auto_ptr \u603b\u662f\u4f7f\u7528\u975e\u6570\u7ec4\u5220\u9664\u6765\u5220\u9664\u5176\u5185\u5bb9\u3002\u8fd9\u610f\u5473\u7740 auto_ptr \u4e0d\u80fd\u6b63\u786e\u5730\u5904\u7406\u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4\uff0c\u56e0\u4e3a\u5b83\u4f7f\u7528\u4e86\u9519\u8bef\u7684\u5185\u5b58\u91ca\u653e\u65b9\u5f0f\u3002\u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u5b83\u4e0d\u80fd\u963b\u6b62\u60a8\u5411\u5b83\u4f20\u9012\u52a8\u6001\u6570\u7ec4\uff0c\u8fd9\u6837\u5b83\u5c31\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002 \u6700\u540e\uff0c auto_ptr \u4e0d\u80fd\u5f88\u597d\u5730\u5904\u7406\u6807\u51c6\u5e93\u4e2d\u7684\u8bb8\u591a\u5176\u4ed6\u7c7b\uff0c\u5305\u62ec\u5927\u591a\u6570\u5bb9\u5668\u548c\u7b97\u6cd5\u3002\u8fd9\u662f\u56e0\u4e3a\u90a3\u4e9b\u6807\u51c6\u5e93\u7c7b\u5047\u5b9a\u5f53\u5b83\u4eec\u590d\u5236\u4e00\u4e2a\u8d44\u6e90\u65f6\uff0c\u5b83\u8fdb\u884c\u4e86\u5b9e\u9645\u7684\u590d\u5236\uff0c\u800c\u4e0d\u662f\u79fb\u52a8\u3002 \u7531\u4e8e\u4e0a\u8ff0\u7f3a\u70b9\uff0c std::auto_ptr \u5728C++ 11\u4e2d\u5df2\u5f03\u7528\uff0c\u5728C++ 17\u4e2d\u5df2\u5220\u9664\u3002 \u7ee7\u7eed std::auto_ptr \u7684\u6838\u5fc3\u95ee\u9898\u662f\uff1aC++ 11\u4e4b\u524d\uff0cC++\u8bed\u8a00\u6839\u672c\u6ca1\u6709\u533a\u5206\u201c\u62f7\u8d1d\u8bed\u4e49\u201d\u548c\u201c\u79fb\u52a8\u8bed\u4e49\u201d\u7684\u673a\u5236\u3002\u5bf9\u62f7\u8d1d\u8bed\u4e49\u91cd\u5199\u6765\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49\uff0c\u4f1a\u5bfc\u81f4\u5947\u602a\u7684\u8fb9\u754c\u60c5\u51b5\u548c\u4e0d\u6613\u5bdf\u89c9\u7684\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5199 res1 = res2 \uff0c\u4f46\u65e0\u6cd5\u77e5\u9053 res2 \u662f\u5426\u4f1a\u88ab\u6539\u53d8\uff01 \u6b63\u56e0\u4e3a\u5982\u6b64\uff0c\u5728C++ 11\u4e2d\uff0c\u6b63\u5f0f\u5b9a\u4e49\u4e86\u201c\u79fb\u52a8\u201d\u7684\u6982\u5ff5\uff0c\u5e76\u5728\u8bed\u8a00\u4e2d\u6dfb\u52a0\u4e86\u201c\u79fb\u52a8\u8bed\u4e49\u201d\uff0c\u4ee5\u6b63\u786e\u533a\u5206\u590d\u5236\u548c\u79fb\u52a8\u3002\u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u79fb\u52a8\u8bed\u4e49\u662f\u5f88\u6709\u7528\u7684\uff0c\u5728\u672c\u7ae0\u7684\u5176\u4f59\u90e8\u5206\u6211\u4eec\u4f1a\u7ee7\u7eed\u63a2\u7d22\u79fb\u52a8\u8bed\u4e49\u7684\u5176\u4ed6\u8bdd\u9898\u3002\u6211\u4eec\u8fd8\u5c06\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\u4fee\u590d Auto_ptr2 \u7c7b\u3002 \u5728C++ 11\u4e2d\uff0c std::auto_ptr \u5df2\u7ecf\u88ab\u4e00\u5806\u5176\u4ed6\u7c7b\u578b\u7684\u201c\u79fb\u52a8\u8bed\u4e49\u517c\u5bb9\u7684\u201d\u667a\u80fd\u6307\u9488\u6240\u53d6\u4ee3\uff1a std::unique_ptr , std::weak_ptr \u548c std::shared_ptr \u3002\u6211\u4eec\u8fd8\u5c06\u63a2\u8ba8\u5176\u4e2d\u6700\u6d41\u884c\u7684\u4e24\u4e2a\uff1a unique_ptr (\u5b83\u76f4\u63a5\u66ff\u6362\u4e86 auto_ptr )\u548c shared_ptr \u3002","title":"M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb"},{"location":"M-1-introduction-to-smart-pointers-and-move-semantics/#_1","text":"\u5bf9\u4e8e\u7c7b\u5bf9\u8c61\u6765\u8bf4\uff0c\u5b83\u4eec\u6700\u5927\u7684\u4f18\u52bf\u662f\u53ef\u4ee5\u5728 \u79bb\u5f00\u4f5c\u7528\u57df \u662f\u81ea\u52a8\u6267\u884c \u6790\u6784\u51fd\u6570 \u3002\u8fd9\u6837\u4e00\u6765\uff0c\u5982\u679c\u6211\u4eec\u5728\u6784\u9020\u51fd\u6570\u4e2d\u5206\u914d\u6216\u83b7\u53d6\u5185\u5b58\uff0c\u5219\u53ef\u4ee5\u5728\u6790\u6784\u51fd\u6570\u4e2d\u8fdb\u884c\u91ca\u653e\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u786e\u4fdd\u5185\u5b58\u5728\u5bf9\u8c61\u88ab\u9500\u6bc1\u524d\uff08\u4e0d\u8bba\u662f\u79bb\u5f00\u4f5c\u7528\u57df\u8fd8\u662f\u663e\u5f0f\u5220\u9664\u7b49\uff09\u91ca\u653e\u3002\u8fd9\u4e5f\u6b63\u662f \u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09 \u7f16\u7a0b\u8303\u5f0f\u7684\u6838\u5fc3\u601d\u60f3\uff08\u89c1 13.9 - \u6790\u6784\u51fd\u6570 \uff09\u3002 \u90a3\u4e48\uff0c\u662f\u5426\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7c7b\uff0c\u5e2e\u52a9\u6211\u4eec\u7ba1\u7406\u548c\u6e05\u7406\u6307\u9488\u5462\uff1f\u5f53\u7136\u53ef\u4ee5\uff01 \u8003\u8651\u8bbe\u8ba1\u8fd9\u6837\u4e00\u4e2a\u7c7b\uff0c\u5b83\u7684\u552f\u4e00\u5de5\u4f5c\uff0c\u5c31\u662f\u7ba1\u7406\u4e00\u4e2a\u4f20\u5165\u7684\u6307\u9488\u3002\u5f53\u8be5\u7c7b\u5bf9\u8c61\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c\u91ca\u653e\u6307\u9488\u6240\u6307\u5411\u7684\u5185\u5b58\u3002\u53ea\u8981\u8be5\u5bf9\u8c61\u88ab\u58f0\u660e\u4e3a\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4fdd\u8bc1\uff0c\u5f53\u5b83\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff08\u4e0d\u8bba\u4f55\u65f6\u3001\u4e5f\u4e0d\u8bba\u51fd\u6570\u5982\u4f55\u7ec8\u6b62\uff09\uff0c\u5b83\u7ba1\u7406\u7684\u6307\u9488\u5c31\u4f1a\u88ab\u9500\u6bc1\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #include template < typename T > class Auto_ptr1 { T * m_ptr ; public : // \u901a\u8fc7\u6784\u9020\u51fd\u6570\u4f20\u5165\u4e00\u4e2a\u6307\u9488\uff0c\u4f7f\u8be5\u7c7b\u62e5\u6709\u5b83 Auto_ptr1 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } // \u6790\u6784\u51fd\u6570\u4f1a\u786e\u4fdd\u6307\u9488\u88ab\u5220\u9664 ~ Auto_ptr1 () { delete m_ptr ; } // \u91cd\u8f7d\u89e3\u5f15\u7528\u548c\u6210\u5458\u8fd0\u7b97\u7b26 T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } }; // A sample class to prove the above works class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { Auto_ptr1 < Resource > res ( new Resource ()); // \u6ce8\u610f\u6b64\u5904\u5206\u914d\u7684\u8d44\u6e90 // ... \u6ca1\u6709\u663e\u5f0f\u5220\u9664\u6307\u9488 // \u6ce8\u610f\uff0c\u5c16\u62ec\u53f7\u4e2d\u7684Resource\u4e0d\u9700\u8981\u661f\u53f7\uff0c\u661f\u53f7\u662f\u6a21\u677f\u63d0\u4f9b\u7684 return 0 ; } // res \u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u5e76\u4e14\u5220\u9664\u4e86\u8d44\u6e90 \u7a0b\u5e8f\u6253\u5370\uff1a 1 2 Resource acquired Resource destroyed \u8003\u8651\u4e00\u4e0b\u8fd9\u4e2a\u7a0b\u5e8f\u548c\u7c7b\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u9996\u5148\uff0c\u52a8\u6001\u521b\u5efa Resource \uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u6a21\u677f\u5316\u7684 Auto_ptr1 \u7c7b\u3002\u4ece\u8fd9\u4e00\u523b\u5f00\u59cb\uff0c Auto_ptr1 \u53d8\u91cf res \u62e5\u6709\u8be5\u8d44\u6e90\u5bf9\u8c61( Auto_ptr1 \u662f m_ptr \u662f \u7ec4\u5408\u5173\u7cfb )\u3002\u56e0\u4e3a res \u88ab\u58f0\u660e\u4e3a\u5c40\u90e8\u53d8\u91cf\u5e76\u5177\u6709\u8bed\u53e5\u5757\u4f5c\u7528\u57df\uff0c\u6240\u4ee5\u5f53\u8bed\u53e5\u5757\u7ed3\u675f\u65f6\uff0c\u5b83\u5c06\u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1(\u4e0d\u7528\u62c5\u5fc3\u5fd8\u8bb0\u91ca\u653e\u5b83)\u3002\u7531\u4e8e\u5b83\u662f\u4e00\u4e2a\u7c7b\uff0c\u56e0\u6b64\u5728\u9500\u6bc1\u5b83\u65f6\uff0c\u5c06\u8c03\u7528 Auto_ptr1 \u6790\u6784\u51fd\u6570\u3002\u8be5\u6790\u6784\u51fd\u6570\u5c06\u786e\u4fdd\u5b83\u6240\u6301\u6709\u7684 Resource \u6307\u9488\u88ab\u5220\u9664! \u53ea\u8981 Auto_ptr1 \u88ab\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf(\u5177\u6709\u81ea\u52a8\u6301\u7eed\u65f6\u95f4\uff0c\u56e0\u6b64\u7c7b\u540d\u4e2d\u6709\u201cAuto\u201d\u90e8\u5206)\uff0c\u65e0\u8bba\u51fd\u6570\u5982\u4f55\u7ec8\u6b62(\u5373\u4f7f\u5b83\u63d0\u524d\u7ec8\u6b62)\uff0c Resource \u603b\u662f\u53ef\u4ee5\u5728\u58f0\u660e\u5b83\u7684\u5757\u7684\u672b\u5c3e\u88ab\u9500\u6bc1\u3002 \u8fd9\u79cd\u7c7b\u88ab\u79f0\u4e3a\u667a\u80fd\u6307\u9488\u3002 \u667a\u80fd\u6307\u9488\u7c7b \u662f\u4e00\u79cd\u7ec4\u5408\u7c7b\uff0c\u5b83\u7528\u4e8e\u7ba1\u7406\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\uff0c\u5e76\u4e14\u80fd\u591f\u786e\u4fdd\u6307\u9488\u5bf9\u8c61\u5728\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\u88ab\u5220\u9664\uff08\u5185\u7f6e\u7684\u6307\u9488\u5bf9\u8c61\u88ab\u79f0\u4e3a\u201c\u7b28\u6307\u9488\u201d\uff0c\u6b63\u662f\u56e0\u4e3a\u5b83\u4eec\u4e0d\u80fd\u81ea\u5df1\u6e05\u7406\u81ea\u5df1\u6240\u7ba1\u7406\u7684\u5185\u5b58\uff09\u3002 \u73b0\u5728\u8ba9\u6211\u4eec\u56de\u5230\u4e0a\u9762\u7684 someFunction() \u4f8b\u5b50\uff0c\u770b\u770b\u5982\u4f55\u4f7f\u7528\u667a\u80fd\u6307\u9488\u89e3\u51b3\u6211\u4eec\u9762\u4e34\u7684\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 #include template < typename T > class Auto_ptr1 { T * m_ptr ; public : // Pass in a pointer to \"own\" via the constructor Auto_ptr1 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } // The destructor will make sure it gets deallocated ~ Auto_ptr1 () { delete m_ptr ; } // Overload dereference and operator-> so we can use Auto_ptr1 like m_ptr. T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } }; // A sample class to prove the above works class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } void sayHi () { std :: cout << \"Hi! \\n \" ; } }; void someFunction () { Auto_ptr1 < Resource > ptr ( new Resource ()); // ptr \u73b0\u5728\u62e5\u6709 Resource int x ; std :: cout << \"Enter an integer: \" ; std :: cin >> x ; if ( x == 0 ) return ; // \u51fd\u6570\u63d0\u524d\u8fd4\u56de // do stuff with ptr here ptr -> sayHi (); } int main () { someFunction (); return 0 ; } \u5982\u679c\u7528\u6237\u8f93\u5165\u975e0\uff0c\u5219\u7a0b\u5e8f\u6253\u5370\uff1a 1 2 3 Resource acquired Hi! Resource destroyed \u5982\u679c\u7528\u6237\u8f93\u51650\uff0c\u5219\u7a0b\u5e8f\u4f1a\u63d0\u524d\u9000\u51fa\uff0c\u6253\u5370\uff1a 1 2 Resource acquired Resource destroyed \u6ce8\u610f\uff0c\u5373\u4f7f\u5728\u7528\u6237\u8f93\u5165\u96f6\u4e14\u51fd\u6570\u63d0\u524d\u7ec8\u6b62\u7684\u60c5\u51b5\u4e0b\uff0c\u8d44\u6e90\u4ecd\u7136\u88ab\u6b63\u786e\u5730\u91ca\u653e\u3002 \u56e0\u4e3a ptr \u53d8\u91cf\u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u6240\u4ee5 ptr \u5c06\u5728\u51fd\u6570\u7ec8\u6b62\u65f6\u88ab\u9500\u6bc1(\u4e0d\u7ba1\u5b83\u4ee5\u4f55\u79cd\u65b9\u5f0f\u7ec8\u6b62)\u3002\u56e0\u4e3a Auto_ptr1 \u6790\u6784\u51fd\u6570\u5c06\u6e05\u7406 Resource \uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4fdd\u8bc1 Resource \u5c06\u88ab\u6b63\u786e\u5730\u6e05\u7406\u3002","title":"\u667a\u80fd\u6307\u9488\u7c7b\u53ef\u4ee5\u62ef\u6551\u6211\u4eec\u5417\uff1f"},{"location":"M-1-introduction-to-smart-pointers-and-move-semantics/#_2","text":"Auto_ptr1 \u7c7b\u6709\u4e00\u4e2a\u4e25\u91cd\u7684\u7f3a\u9677\uff0c\u662f\u7531\u81ea\u52a8\u751f\u6210\u7684\u4ee3\u7801\u5f15\u8d77\u7684\u3002\u4f60\u80fd\u770b\u51fa\u6765\u5417\uff1f (\u63d0\u793a\uff1a\u5982\u679c\u7f16\u8bd1\u5668\u4f1a\u4e3a\u7c7b\u63d0\u4f9b\u54ea\u4e9b\u9ed8\u8ba4\u7684\u51fd\u6570\uff1f) \u65f6\u95f4\u5230\uff01 \u731c\u5230\u4e86\u5417\uff1f\u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include // Same as above template < typename T > class Auto_ptr1 { T * m_ptr ; public : Auto_ptr1 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } ~ Auto_ptr1 () { delete m_ptr ; } T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } }; class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { Auto_ptr1 < Resource > res1 ( new Resource ()); Auto_ptr1 < Resource > res2 ( res1 ); // \u6ca1\u6709\u521d\u59cb\u5316res2\uff0c\u800c\u662f\u8fdb\u884c\u8d4b\u503c return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 Resource acquired Resource destroyed Resource destroyed \u7a0b\u5e8f\u5f88\u53ef\u80fd(\u4f46\u4e0d\u4e00\u5b9a)\u5728\u6b64\u65f6\u5d29\u6e83\u3002\u53d1\u73b0\u95ee\u9898\u7684\u6240\u5728\u4e86\u5417\uff1f\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u63d0\u4f9b\u590d\u5236\u6784\u9020\u51fd\u6570\u6216\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u6240\u4ee5C++\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u4e2a\u3002\u5b83\u63d0\u4f9b\u7684\u51fd\u6570\u53ea\u505a\u6d45\u62f7\u8d1d\u3002\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u7528 res1 \u521d\u59cb\u5316 res2 \u65f6\uff0c\u4e24\u4e2a Auto_ptr1 \u53d8\u91cf\u90fd\u6307\u5411\u540c\u4e00\u4e2a Resource \u3002\u5f53 res2 \u8d85\u51fa\u4f5c\u7528\u57df\u65f6\uff0c\u5b83\u5220\u9664\u8d44\u6e90\uff0c\u7ed9 res1 \u7559\u4e0b\u4e00\u4e2a \u60ac\u5782 \u6307\u9488\u3002\u5f53 res1 \u53bb\u5220\u9664\u5b83\u7684(\u5df2\u7ecf\u5220\u9664\u7684)\u8d44\u6e90\u65f6\uff0c\u7a0b\u5e8f\u5d29\u6e83! \u4e0b\u9762\u7684\u4ee3\u7801\u4e5f\u4f1a\u51fa\u73b0\u7c7b\u4f3c\u7684\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 void passByValue ( Auto_ptr1 < Resource > res ) { } int main () { Auto_ptr1 < Resource > res1 ( new Resource ()); passByValue ( res1 ); return 0 ; } \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c res1 \u5c06\u6309\u503c\u590d\u5236\u5230 passByValue \u7684\u53c2\u6570 res \u4e2d\uff0c\u5bfc\u81f4 Resource \u6307\u9488\u88ab\u590d\u5236\uff0c\u8fdb\u800c\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002 \u663e\u7136\u8fd9\u4e0d\u662f\u4ec0\u4e48\u597d\u4e8b\u3002\u6211\u4eec\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898? \u4e00\u79cd\u65b9\u6cd5\u662f\u663e\u5f0f\u5730\u5b9a\u4e49\u548c\u5220\u9664\u590d\u5236\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u4ece\u800c\u4ece\u6839\u672c\u4e0a\u963b\u6b62\u62f7\u8d1d\u3002\u8fd9\u4f1a\u963b\u6b62\u6309\u503c\u4f20\u9012\u7684\u60c5\u51b5(\u8fd9\u5f88\u597d\uff0c\u56e0\u4e3a\u6211\u4eec\u672c\u6765\u5c31\u4e0d\u5e94\u8be5\u6309\u503c\u4f20\u9012\u5b83\u4eec)\u3002 \u4f46\u662f\uff0c\u6211\u4eec\u5982\u4f55\u4ece\u51fd\u6570\u8fd4\u56de Auto_ptr1 \u7ed9\u8c03\u7528\u8005\u5462? 1 2 3 4 5 ??? generateResource () { Resource * r { new Resource () }; return Auto_ptr1 ( r ); } \u6211\u4eec\u65e0\u6cd5\u5c06 Auto_ptr1 \u6309\u5f15\u7528\u8fd4\u56de \uff0c\u56e0\u4e3a\u5c40\u90e8\u53d8\u91cf Auto_ptr1 \u4f1a\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u5220\u9664\uff0c\u6240\u4ee5\u4e3b\u8c03\u51fd\u6570\u5f97\u5230\u7684\u662f\u4e00\u4e2a\u60ac\u5782\u5f15\u7528\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u6307\u9488r\u8fd4\u56de\u4e3a Resource* \uff0c\u4f46\u662f\u6211\u4eec\u53ef\u80fd\u4f1a\u5728\u540e\u9762\u5fd8\u8bb0\u5220\u9664 r \uff0c\u8fd9\u5c31\u8fdd\u80cc\u4e86\u4f7f\u7528\u667a\u80fd\u6307\u9488\u7684\u521d\u8877\u3002\u6240\u4ee5\uff0c\u6309\u503c\u8fd4\u56de Auto_ptr1 \u662f\u552f\u4e00\u6709\u610f\u4e49\u7684\u9009\u9879\u2014\u2014\u4f46\u6211\u4eec\u6700\u7ec8\u4f1a\u5f97\u5230\u6d45\u62f7\u8d1d\u3001\u91cd\u590d\u7684\u6307\u9488\u5e76\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002 \u53e6\u4e00\u79cd\u9009\u62e9\u662f\u91cd\u5199 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\u6765\u8fdb\u884c \u6df1\u62f7\u8d1d \u3002\u8fd9\u6837\uff0c\u6211\u4eec\u81f3\u5c11\u53ef\u4ee5\u907f\u514d\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684\u91cd\u590d\u6307\u9488\u3002\u4f46\u662f\u62f7\u8d1d\u7684\u5f00\u9500\u53ef\u80fd\u4f1a\u5f88\u5927(\u800c\u4e14\u53ef\u80fd\u4e0d\u9700\u8981\u751a\u81f3\u4e0d\u53ef\u80fd)\uff0c\u800c\u4e14\u6211\u4eec\u4e0d\u5e0c\u671b\u4ec5\u4ec5\u4e3a\u4e86\u4ece\u51fd\u6570\u8fd4\u56de Auto_ptr1 \u800c\u5bf9\u5bf9\u8c61\u8fdb\u884c\u4e0d\u5fc5\u8981\u7684\u590d\u5236\u3002\u53e6\u5916\uff0c\u5bf9\u666e\u901a\u6307\u9488\u8d4b\u503c\u6216\u521d\u59cb\u5316\u65f6\u5e76\u4e0d\u4f1a\u62f7\u8d1d\u5bf9\u8c61\uff0c\u4e3a\u4ec0\u4e48\u667a\u80fd\u6307\u9488\u8981\u5177\u6709\u4e0d\u540c\u7684\u903b\u8f91\u5462\uff1f \u8be5\u600e\u4e48\u505a\u5462\uff1f","title":"\u4e25\u91cd\u7f3a\u9677"},{"location":"M-1-introduction-to-smart-pointers-and-move-semantics/#_3","text":"\u5982\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba9\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u8fd0\u7b97\u7b26\u4e0d\u53bb\u62f7\u8d1d\u6307\u9488\uff08\u62f7\u8d1d\u8bed\u4e49\uff09\uff0c\u800c\u662f\u5c06\u5b83\u6240\u7ba1\u7406\u7684\u8d44\u6e90\u4f20\u9012\u6216\u79fb\u52a8\u7ed9\u76ee\u6807\u6307\u9488\u5462\uff1f\u8fd9\u6b63\u662f \u79fb\u52a8\u8bed\u4e49 \u80cc\u540e\u7684\u6838\u5fc3\u601d\u60f3\u3002\u79fb\u52a8\u8bed\u4e49\u8bf4\u7684\u5c31\u662f\u5bf9\u8c61\u6240\u6709\u6743\u53d1\u751f\u4e86\u8f6c\u79fb\u800c\u4e0d\u662f\u62f7\u8d1d\u3002 \u66f4\u65b0\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 #include template < typename T > class Auto_ptr2 { T * m_ptr ; public : Auto_ptr2 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } ~ Auto_ptr2 () { delete m_ptr ; } // \u4f7f\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49 Auto_ptr2 ( Auto_ptr2 & a ) // \u6ce8\u610f: \u4e0d\u80fd\u662f const { m_ptr = a . m_ptr ; // \u76ee\u7684\u5bf9\u8c61\u83b7\u5f97\u6e90\u5bf9\u8c61\u7684\u6307\u9488 a . m_ptr = nullptr ; // \u786e\u4fdd\u6e90\u5bf9\u8c61\u4e0d\u518d\u62e5\u6709\u8be5\u6307\u9488 } // \u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49 Auto_ptr2 & operator = ( Auto_ptr2 & a ) // \u6ce8\u610f: \u4e0d\u80fd\u662f const { if ( & a == this ) return * this ; delete m_ptr ; // \u786e\u4fdd\u76ee\u7684\u5bf9\u8c61\u7684\u6307\u9488\u6240\u6307\u7684\u8d44\u6e90\u88ab\u91ca\u653e m_ptr = a . m_ptr ; // \u76ee\u7684\u5bf9\u8c61\u83b7\u5f97\u6e90\u5bf9\u8c61\u7684\u6307\u9488 a . m_ptr = nullptr ; // \u786e\u4fdd\u6e90\u5bf9\u8c61\u4e0d\u518d\u62e5\u6709\u8be5\u6307\u9488 return * this ; } T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } bool isNull () const { return m_ptr == nullptr ; } }; class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { Auto_ptr2 < Resource > res1 ( new Resource ()); Auto_ptr2 < Resource > res2 ; // Start as nullptr std :: cout << \"res1 is \" << ( res1 . isNull () ? \"null \\n \" : \"not null \\n \" ); std :: cout << \"res2 is \" << ( res2 . isNull () ? \"null \\n \" : \"not null \\n \" ); res2 = res1 ; // res2 assumes ownership, res1 is set to null std :: cout << \"Ownership transferred \\n \" ; std :: cout << \"res1 is \" << ( res1 . isNull () ? \"null \\n \" : \"not null \\n \" ); std :: cout << \"res2 is \" << ( res2 . isNull () ? \"null \\n \" : \"not null \\n \" ); return 0 ; } \u6253\u5370\uff1a 1 2 3 4 5 6 7 Resource acquired res1 is not null res2 is null Ownership transferred res1 is null res2 is not null Resource destroyed \u6ce8\u610f\uff0c\u6211\u4eec\u91cd\u8f7d\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u73b0\u5728\u53ef\u4ee5\u628a m_ptr \u7684\u6240\u6709\u6743\u4ece res1 \u4f20\u9012\u7ed9 res2 \uff01\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c06\u4e0d\u4f1a\u5f97\u5230\u91cd\u590d\u7684\u6307\u9488\uff0c\u8d44\u6e90\u6e05\u7406\u53ef\u4ee5\u987a\u5229\u8fdb\u884c\u3002","title":"\u79fb\u52a8\u8bed\u4e49"},{"location":"M-1-introduction-to-smart-pointers-and-move-semantics/#stdauto_ptr","text":"\u73b0\u5728\u662f\u8ba8\u8bba std::auto_ptr \u7684\u5408\u9002\u65f6\u673a\u4e86\u3002 std::auto_ptr \u5728C++ 98\u4e2d\u5f15\u5165\uff0c\u5728C++ 17\u4e2d\u88ab\u5220\u9664\uff0c\u5b83\u662fC++\u7b2c\u4e00\u6b21\u5c1d\u8bd5\u6807\u51c6\u5316\u667a\u80fd\u6307\u9488\u3002 auto_ptr \u9009\u62e9\u4e86 Auto_ptr2 \u7c7b\u4e00\u6837\u7684\u79fb\u52a8\u8bed\u4e49\u5b9e\u73b0\u65b9\u5f0f\u3002 \u7136\u800c\uff0c std::auto_ptr (\u4ee5\u53ca\u6211\u4eec\u7684 Auto_ptr2 \u7c7b)\u6709\u8bb8\u591a\u95ee\u9898\uff0c\u4f7f\u5f97\u4f7f\u7528\u5b83\u5f88\u5371\u9669\u3002 \u9996\u5148\uff0c\u7531\u4e8e std::auto_ptr \u901a\u8fc7\u590d\u5236\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\u5b9e\u73b0\u4e86\u79fb\u52a8\u8bed\u4e49\uff0c\u5c06 std::auto_ptr \u6309\u503c\u4f20\u9012\u7ed9\u51fd\u6570\u5c06\u5bfc\u81f4\u8d44\u6e90\u88ab\u79fb\u52a8\u5230\u51fd\u6570\u5f62\u53c2\u4e2d(\u5e76\u5728\u51fd\u6570\u5f62\u53c2\u8d85\u51fa\u4f5c\u7528\u57df\u65f6\u9500\u6bc1)\u3002\u7136\u540e\uff0c\u5f53\u4f60\u4ece\u8c03\u7528\u8005\u8bbf\u95ee auto_ptr \u5b9e\u53c2\u65f6(\u6ca1\u6709\u610f\u8bc6\u5230\u5b83\u5df2\u7ecf\u88ab\u79fb\u52a8\u548c\u5220\u9664\u4e86)\u65f6\uff0c\u8fdb\u884c \u89e3\u5f15\u7528 \u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002 \u5176\u6b21\uff0c std::auto_ptr \u603b\u662f\u4f7f\u7528\u975e\u6570\u7ec4\u5220\u9664\u6765\u5220\u9664\u5176\u5185\u5bb9\u3002\u8fd9\u610f\u5473\u7740 auto_ptr \u4e0d\u80fd\u6b63\u786e\u5730\u5904\u7406\u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4\uff0c\u56e0\u4e3a\u5b83\u4f7f\u7528\u4e86\u9519\u8bef\u7684\u5185\u5b58\u91ca\u653e\u65b9\u5f0f\u3002\u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u5b83\u4e0d\u80fd\u963b\u6b62\u60a8\u5411\u5b83\u4f20\u9012\u52a8\u6001\u6570\u7ec4\uff0c\u8fd9\u6837\u5b83\u5c31\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002 \u6700\u540e\uff0c auto_ptr \u4e0d\u80fd\u5f88\u597d\u5730\u5904\u7406\u6807\u51c6\u5e93\u4e2d\u7684\u8bb8\u591a\u5176\u4ed6\u7c7b\uff0c\u5305\u62ec\u5927\u591a\u6570\u5bb9\u5668\u548c\u7b97\u6cd5\u3002\u8fd9\u662f\u56e0\u4e3a\u90a3\u4e9b\u6807\u51c6\u5e93\u7c7b\u5047\u5b9a\u5f53\u5b83\u4eec\u590d\u5236\u4e00\u4e2a\u8d44\u6e90\u65f6\uff0c\u5b83\u8fdb\u884c\u4e86\u5b9e\u9645\u7684\u590d\u5236\uff0c\u800c\u4e0d\u662f\u79fb\u52a8\u3002 \u7531\u4e8e\u4e0a\u8ff0\u7f3a\u70b9\uff0c std::auto_ptr \u5728C++ 11\u4e2d\u5df2\u5f03\u7528\uff0c\u5728C++ 17\u4e2d\u5df2\u5220\u9664\u3002","title":"std::auto_ptr \u4e3a\u4ec0\u4e48\u662f\u7cdf\u7cd5\u7684\u5b9e\u73b0"},{"location":"M-1-introduction-to-smart-pointers-and-move-semantics/#_4","text":"std::auto_ptr \u7684\u6838\u5fc3\u95ee\u9898\u662f\uff1aC++ 11\u4e4b\u524d\uff0cC++\u8bed\u8a00\u6839\u672c\u6ca1\u6709\u533a\u5206\u201c\u62f7\u8d1d\u8bed\u4e49\u201d\u548c\u201c\u79fb\u52a8\u8bed\u4e49\u201d\u7684\u673a\u5236\u3002\u5bf9\u62f7\u8d1d\u8bed\u4e49\u91cd\u5199\u6765\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49\uff0c\u4f1a\u5bfc\u81f4\u5947\u602a\u7684\u8fb9\u754c\u60c5\u51b5\u548c\u4e0d\u6613\u5bdf\u89c9\u7684\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5199 res1 = res2 \uff0c\u4f46\u65e0\u6cd5\u77e5\u9053 res2 \u662f\u5426\u4f1a\u88ab\u6539\u53d8\uff01 \u6b63\u56e0\u4e3a\u5982\u6b64\uff0c\u5728C++ 11\u4e2d\uff0c\u6b63\u5f0f\u5b9a\u4e49\u4e86\u201c\u79fb\u52a8\u201d\u7684\u6982\u5ff5\uff0c\u5e76\u5728\u8bed\u8a00\u4e2d\u6dfb\u52a0\u4e86\u201c\u79fb\u52a8\u8bed\u4e49\u201d\uff0c\u4ee5\u6b63\u786e\u533a\u5206\u590d\u5236\u548c\u79fb\u52a8\u3002\u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u79fb\u52a8\u8bed\u4e49\u662f\u5f88\u6709\u7528\u7684\uff0c\u5728\u672c\u7ae0\u7684\u5176\u4f59\u90e8\u5206\u6211\u4eec\u4f1a\u7ee7\u7eed\u63a2\u7d22\u79fb\u52a8\u8bed\u4e49\u7684\u5176\u4ed6\u8bdd\u9898\u3002\u6211\u4eec\u8fd8\u5c06\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\u4fee\u590d Auto_ptr2 \u7c7b\u3002 \u5728C++ 11\u4e2d\uff0c std::auto_ptr \u5df2\u7ecf\u88ab\u4e00\u5806\u5176\u4ed6\u7c7b\u578b\u7684\u201c\u79fb\u52a8\u8bed\u4e49\u517c\u5bb9\u7684\u201d\u667a\u80fd\u6307\u9488\u6240\u53d6\u4ee3\uff1a std::unique_ptr , std::weak_ptr \u548c std::shared_ptr \u3002\u6211\u4eec\u8fd8\u5c06\u63a2\u8ba8\u5176\u4e2d\u6700\u6d41\u884c\u7684\u4e24\u4e2a\uff1a unique_ptr (\u5b83\u76f4\u63a5\u66ff\u6362\u4e86 auto_ptr )\u548c shared_ptr \u3002","title":"\u7ee7\u7eed"},{"location":"M-2-R-value-references/","tags":["R-value-references","references"],"text":"Key Takeaway \u5728\u7b2c\u4e5d\u7ae0\u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86 \u503c\u7c7b\u578b ( 9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09 )\u2014\u2014\u503c\u7c7b\u578b\u662f\u8868\u8fbe\u5f0f\u7684\u4e00\u4e2a\u5c5e\u6027\uff0c\u7528\u4e8e\u786e\u5b9a\u8868\u8fbe\u5f0f\u6c42\u503c\u5f97\u5230\u503c\u3001\u51fd\u6570\u8fd8\u662f\u5bf9\u8c61\u3002\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86 \u5de6\u503c \u548c \u53f3\u503c \uff0c\u4ee5\u4fbf\u5c55\u5f00\u5bf9 \u5de6\u503c\u5f15\u7528 \u7684\u8ba8\u8bba\u3002 \u5982\u679c\u4f60\u5bf9\u5de6\u503c\u548c\u53f3\u503c\u611f\u5230\u751f\u758f\u4e86\uff0c\u73b0\u5728\u6b63\u662f\u590d\u4e60\u5b83\u4eec\u7684\u597d\u65f6\u673a\u3002 \u590d\u4e60\uff1a\u5de6\u503c \u5728C++11\u4e4b\u524d\uff0cC++\u4e2d\u53ea\u6709\u5de6\u503c\u5f15\u7528\u8fd9\u4e00\u79cd\u5f15\u7528\uff0c\u6240\u4ee5\u5b83\u88ab\u79f0\u4e3a\u201c\u5f15\u7528\u201d\u3002\u4f46\u662f\u5230\u4e86C++11\uff0c\u5b83\u88ab\u91cd\u65b0\u547d\u540d\u4e3a\u5de6\u503c\u5f15\u7528\u3002 \u5de6\u503c\u5f15\u7528\u5fc5\u987b\u901a\u8fc7\u53ef\u4fee\u6539\u5de6\u503c\u521d\u59cb\u5316\u3002 \u5de6\u503c\u5f15\u7528 \u53ef\u7528\u4e8e\u521d\u59cb\u5316 \u53ef\u4fee\u6539 Modifiable l-values Yes Yes Non-modifiable l-values No No R-values No No const \u5bf9\u8c61\u7684\u5de6\u503c\u5f15\u7528\u53ef\u4ee5\u901a\u8fc7\u53ef\u4fee\u6539\u3001\u4e0d\u53ef\u4fee\u6539\u7684\u5de6\u503c\u6216\u53f3\u503c\u521d\u59cb\u5316\u3002\u4f46\u662f\uff0c\u8fd9\u4e9b\u503c\u90fd\u4e0d\u80fd\u88ab\u4fee\u6539\u3002 const\u5de6\u503c\u7684\u5f15\u7528 \u53ef\u7528\u4e8e\u521d\u59cb\u5316 \u53ef\u4fee\u6539 Modifiable l-values Yes No Non-modifiable l-values Yes No R-values Yes No const \u5bf9\u8c61\u7684\u5de6\u503c\u5f15\u7528\u5c24\u5176\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u914d\u5408\u4efb\u4f55\u7c7b\u578b\u7684\u5b9e\u53c2\uff08\u5de6\u503c\u6216\u53f3\u503c\uff09\u5de5\u4f5c\uff0c\u4e14\u4e0d\u4f1a\u521b\u5efa\u62f7\u8d1d\u3002 R-value references C++11 \u6dfb\u52a0\u4e86\u4e00\u79cd\u65b0\u7684\u5f15\u7528\u7c7b\u578b\uff0c\u79f0\u4e3a \u53f3\u503c\u5f15\u7528 \u3002\u53f3\u503c\u5f15\u7528\u88ab\u8bbe\u8ba1\u51fa\u6765\u7684\u76ee\u7684\uff0c\u662f\u4e3a\u4e86\u914d\u5408\u53f3\u503c\u4f7f\u7528\uff08\u4e5f\u53ea\u80fd\u914d\u5408\u53f3\u503c\u4f7f\u7528\uff09\u3002\u521b\u5efa\u5de6\u503c\u5f15\u7528\u4f7f\u7528\u4e00\u4e2a & \u7b26\u53f7\uff0c\u521b\u5efa\u53f3\u503c\u5f15\u7528\u5219\u4f7f\u7528\u4e24\u4e2a\uff1a 1 2 3 int x { 5 }; int & lref { x }; // \u901a\u8fc7\u5de6\u503c\u6765\u521d\u59cb\u5316\u4e00\u4e2a\u5de6\u503c\u5f15\u7528 int && rref { 5 }; // \u4f7f\u7528\u53f3\u503c 5\uff08\u5b57\u9762\u91cf\uff09\u6765\u521d\u59cb\u5316\u53f3\u503c\u5f15\u7528 \u53f3\u503c\u5f15\u7528\u4e0d\u80fd\u591f\u901a\u8fc7\u5de6\u503c\u6765\u521d\u59cb\u5316\u3002 \u53f3\u503c\u5f15\u7528 \u53ef\u7528\u4e8e\u521d\u59cb\u5316 \u53ef\u4fee\u6539 Modifiable l-values No No Non-modifiable l-values No No R-values Yes Yes const\u53f3\u503c\u5f15\u7528 \u53ef\u7528\u4e8e\u521d\u59cb\u5316 \u53ef\u4fee\u6539 Modifiable l-values No No Non-modifiable l-values No No R-values Yes No \u53f3\u503c\u5f15\u7528\u6709\u4e24\u4e2a\u6709\u7528\u7684\u5c5e\u6027\u3002\u9996\u5148\uff0c \u53f3\u503c\u5f15\u7528\u53ef\u4ee5\u5c06\u521d\u59cb\u5316\u5b83\u4eec\u7684\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u5ef6\u957f\u5230\u81ea\u5df1\u7684\u751f\u547d\u5468\u671f (\u5bf9const\u5bf9\u8c61\u7684\u5de6\u503c\u5f15\u7528\u4e5f\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9)\u3002\u5176\u6b21\uff0c\u4f60==\u53ef\u4ee5\u901a\u8fc7\u6307\u5411\u975econst\u53f3\u503c\u5f15\u7528\u6765\u4fee\u6539\u53f3\u503c\u3002== \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include class Fraction { private : int m_numerator ; int m_denominator ; public : Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } }; int main () { auto && rref { Fraction { 3 , 5 } }; // \u5bf9\u4e34\u65f6 Fraction \u7684\u53f3\u503c\u5f15\u7528 // operator<< \u7684\u53c2\u6570f1\u4f1a\u7ed1\u5b9a\u5230\u4e34\u65f6\u5bf9\u8c61\uff0c\u800c\u4e0d\u4f1a\u521b\u5efa\u62f7\u8d1d std :: cout << rref << '\\n' ; return 0 ; } // rref (\u548c\u4e34\u65f6\u7684Fraction) \u79bb\u5f00\u4f5c\u7528\u57df \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 3/5 \u4f5c\u4e3a\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff0c Fraction(3, 5) \u901a\u5e38\u4f1a\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u540e\u5c31\u79bb\u5f00\u4f5c\u7528\u57df\u3002\u4f46\u662f\uff0c\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528\u5b83\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\uff0c\u6240\u4ee5\u5b83\u7684\u6301\u7eed\u65f6\u95f4\u88ab\u5ef6\u957f\u5230\u4e86\u548c\u8be5\u53f3\u503c\u5f15\u7528\u4e00\u6837\u2014\u2014\u8bed\u53e5\u5757\u7ed3\u5c3e\u65f6\u79bb\u5f00\u4f5c\u7528\u57df\u3002\u7136\u540e\uff0c\u6211\u4eec\u53d8\u53ef\u4ee5\u901a\u8fc7\u8be5\u53f3\u503c\u5f15\u7528\u6253\u5370 Fraction \u7684\u503c\u3002 \u73b0\u5728\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u4e0d\u90a3\u4e48\u76f4\u89c2\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 #include int main () { int && rref { 5 }; // \u901a\u8fc7\u5b57\u9762\u91cf\u521d\u59cb\u5316\u53f3\u503c\u5f15\u7528\uff0c\u6b64\u65f6\u521b\u5efa\u4e86\u4e34\u65f6\u5bf9\u8c615 rref = 10 ; std :: cout << rref << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 10 \u867d\u7136\u7528\u5b57\u9762\u503c\u521d\u59cb\u5316\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\uff0c\u7136\u540e\u518d\u901a\u8fc7\u53f3\u503c\u5f15\u7528\u4fee\u6539\u8be5\u53f3\u503c\u770b\u8d77\u6765\u6709\u4e9b\u5947\u602a\u3002\u4f46\u662f\u5f53\u6211\u4eec\u8fd9\u4e48\u505a\u65f6\uff0c\u4f1a\u4ece\u5b57\u9762\u503c\u6784\u9020\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff0c\u6b64\u65f6\u53f3\u503c\u5f15\u7528\u5f15\u7528\u7684\u5bf9\u8c61\u662f\u8be5\u4e34\u65f6\u5bf9\u8c61\u800c\u4e0d\u662f\u5b57\u9762\u91cf\u672c\u8eab\u3002 \u4e0d\u8fc7\uff0c\u4e0a\u9762\u4e24\u4e2a\u4f8b\u5b50\u5e76\u4e0d\u662f\u5e38\u89c1\u7684\u53f3\u503c\u4f7f\u7528\u573a\u5408\u3002 \u4f7f\u7528\u53f3\u503c\u5f15\u7528\u4f5c\u4e3a\u51fd\u6570\u5f62\u53c2 \u53f3\u503c\u5f15\u7528\u901a\u5e38\u7528\u4f5c\u51fd\u6570\u5f62\u53c2 \u3002\u5728\u91cd\u8f7d\u51fd\u6570\u65f6\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u51fd\u6570\u7684\u884c\u4e3a\u5728\u5904\u7406\u5de6\u503c\u53f3\u503c\u65f6\u4e0d\u540c\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u53f3\u503c\u4f5c\u4e3a\u51fd\u6570\u5f62\u53c2\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include void fun ( const int & lref ) // \u5de6\u503c\u5f62\u53c2\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570 { std :: cout << \"l-value reference to const \\n \" ; } void fun ( int && rref ) // \u53f3\u503c\u5f62\u53c2\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570 { std :: cout << \"r-value reference \\n \" ; } int main () { int x { 5 }; fun ( x ); // l-value argument calls l-value version of function fun ( 5 ); // r-value argument calls r-value version of function return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 l-value reference to const r-value reference \u5982\u4f60\u6240\u89c1\uff0c\u5f53\u4f20\u9012\u4e00\u4e2a\u5de6\u503c\u65f6\uff0c\u91cd\u8f7d\u51fd\u6570\u89e3\u6790\u4e3a\u5177\u6709\u5de6\u503c\u5f15\u7528\u7684\u7248\u672c\u3002\u5f53\u4f20\u9012\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\uff0c\u91cd\u8f7d\u51fd\u6570\u89e3\u6790\u5230\u5177\u6709\u53f3\u503c\u5f15\u7528\u7684\u7248\u672c(\u8fd9\u88ab\u8ba4\u4e3a\u662f\u6bd4\u5de6\u503c\u5f15\u7528const\u66f4\u597d\u7684\u5339\u914d)\u3002 \u4e3a\u4ec0\u4e48\u8981\u8fd9\u4e48\u505a\u5462\uff1f\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8bfe\u4e2d\u66f4\u8be6\u7ec6\u5730\u8ba8\u8bba\u8fd9\u4e2a\u95ee\u9898\u3002\u6beb\u65e0\u7591\u95ee\uff0c\u8fd9\u662f \u79fb\u52a8\u8bed\u4e49 \u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002 \u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a 1 2 int && ref { 5 }; fun ( ref ); \u4e0a\u9762\u4ee3\u7801\u5b9e\u9645\u4e0a\u4f1a\u8c03\u7528\u5de6\u503c\u5f15\u7528\u7248\u672c\u7684\u51fd\u6570\uff01\u5c3d\u7ba1 ref \u662f\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\uff0c\u4f46\u662f\u5b83\u672c\u8eab\u662f\u4e00\u4e2a\u5de6\u503c\uff08\u6240\u6709\u6709\u540d\u5b57\u7684\u53d8\u91cf\u90fd\u662f\u5de6\u503c\uff09\u3002\u9020\u6210\u6df7\u6dc6\u7684\u6839\u672c\u539f\u56e0\u5728\u4e8e\uff0c\u6211\u4eec\u5728\u4e0d\u540c\u7684\u8bed\u5883\u4e2d\u4f7f\u7528\u4e86\u672f\u8bed\u201d\u53f3\u503c\u201c\u3002\u5e94\u8be5\u8fd9\u6837\u7406\u89e3\uff1a \u547d\u540d\u5bf9\u8c61\u662f\u5de6\u503c\uff0c\u533f\u540d\u5bf9\u8c61\u662f\u53f3\u503c\u3002\u547d\u540d\u5bf9\u8c61\u6216\u533f\u540d\u5bf9\u8c61\u7684\u7c7b\u578b\u4e0e\u5b83\u662f\u5de6\u503c\u8fd8\u662f\u53f3\u503c\u65e0\u5173 \u3002\u6216\u8005\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c\u53f3\u503c\u5f15\u7528\u4e0d\u53eb\u53f3\u503c\u5f15\u7528\u7684\u8bdd\uff0c\u5c31\u4e0d\u4f1a\u51fa\u73b0\u8fd9\u79cd\u65e0\u89e3\u4e86\u3002 \u8fd4\u56de\u53f3\u503c\u5f15\u7528 \u60a8\u51e0\u4e4e\u4e0d\u5e94\u8be5\u8fd4\u56de\u53f3\u503c\u7684\u5f15\u7528\uff0c\u539f\u56e0\u4e0e\u4f60\u51e0\u4e4e\u4e0d\u5e94\u8be5\u8fd4\u56de\u5de6\u503c\u7684\u5f15\u7528\u76f8\u540c \u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5f53\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u6700\u7ec8\u4f1a\u6210\u4e3a\u4e00\u4e2a \u60ac\u5782 \u5f15\u7528\u3002","title":"M.2 - \u53f3\u503c\u5f15\u7528"},{"location":"M-2-R-value-references/#_1","text":"\u5728C++11\u4e4b\u524d\uff0cC++\u4e2d\u53ea\u6709\u5de6\u503c\u5f15\u7528\u8fd9\u4e00\u79cd\u5f15\u7528\uff0c\u6240\u4ee5\u5b83\u88ab\u79f0\u4e3a\u201c\u5f15\u7528\u201d\u3002\u4f46\u662f\u5230\u4e86C++11\uff0c\u5b83\u88ab\u91cd\u65b0\u547d\u540d\u4e3a\u5de6\u503c\u5f15\u7528\u3002 \u5de6\u503c\u5f15\u7528\u5fc5\u987b\u901a\u8fc7\u53ef\u4fee\u6539\u5de6\u503c\u521d\u59cb\u5316\u3002 \u5de6\u503c\u5f15\u7528 \u53ef\u7528\u4e8e\u521d\u59cb\u5316 \u53ef\u4fee\u6539 Modifiable l-values Yes Yes Non-modifiable l-values No No R-values No No const \u5bf9\u8c61\u7684\u5de6\u503c\u5f15\u7528\u53ef\u4ee5\u901a\u8fc7\u53ef\u4fee\u6539\u3001\u4e0d\u53ef\u4fee\u6539\u7684\u5de6\u503c\u6216\u53f3\u503c\u521d\u59cb\u5316\u3002\u4f46\u662f\uff0c\u8fd9\u4e9b\u503c\u90fd\u4e0d\u80fd\u88ab\u4fee\u6539\u3002 const\u5de6\u503c\u7684\u5f15\u7528 \u53ef\u7528\u4e8e\u521d\u59cb\u5316 \u53ef\u4fee\u6539 Modifiable l-values Yes No Non-modifiable l-values Yes No R-values Yes No const \u5bf9\u8c61\u7684\u5de6\u503c\u5f15\u7528\u5c24\u5176\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u914d\u5408\u4efb\u4f55\u7c7b\u578b\u7684\u5b9e\u53c2\uff08\u5de6\u503c\u6216\u53f3\u503c\uff09\u5de5\u4f5c\uff0c\u4e14\u4e0d\u4f1a\u521b\u5efa\u62f7\u8d1d\u3002","title":"\u590d\u4e60\uff1a\u5de6\u503c"},{"location":"M-2-R-value-references/#r-value-references","text":"C++11 \u6dfb\u52a0\u4e86\u4e00\u79cd\u65b0\u7684\u5f15\u7528\u7c7b\u578b\uff0c\u79f0\u4e3a \u53f3\u503c\u5f15\u7528 \u3002\u53f3\u503c\u5f15\u7528\u88ab\u8bbe\u8ba1\u51fa\u6765\u7684\u76ee\u7684\uff0c\u662f\u4e3a\u4e86\u914d\u5408\u53f3\u503c\u4f7f\u7528\uff08\u4e5f\u53ea\u80fd\u914d\u5408\u53f3\u503c\u4f7f\u7528\uff09\u3002\u521b\u5efa\u5de6\u503c\u5f15\u7528\u4f7f\u7528\u4e00\u4e2a & \u7b26\u53f7\uff0c\u521b\u5efa\u53f3\u503c\u5f15\u7528\u5219\u4f7f\u7528\u4e24\u4e2a\uff1a 1 2 3 int x { 5 }; int & lref { x }; // \u901a\u8fc7\u5de6\u503c\u6765\u521d\u59cb\u5316\u4e00\u4e2a\u5de6\u503c\u5f15\u7528 int && rref { 5 }; // \u4f7f\u7528\u53f3\u503c 5\uff08\u5b57\u9762\u91cf\uff09\u6765\u521d\u59cb\u5316\u53f3\u503c\u5f15\u7528 \u53f3\u503c\u5f15\u7528\u4e0d\u80fd\u591f\u901a\u8fc7\u5de6\u503c\u6765\u521d\u59cb\u5316\u3002 \u53f3\u503c\u5f15\u7528 \u53ef\u7528\u4e8e\u521d\u59cb\u5316 \u53ef\u4fee\u6539 Modifiable l-values No No Non-modifiable l-values No No R-values Yes Yes const\u53f3\u503c\u5f15\u7528 \u53ef\u7528\u4e8e\u521d\u59cb\u5316 \u53ef\u4fee\u6539 Modifiable l-values No No Non-modifiable l-values No No R-values Yes No \u53f3\u503c\u5f15\u7528\u6709\u4e24\u4e2a\u6709\u7528\u7684\u5c5e\u6027\u3002\u9996\u5148\uff0c \u53f3\u503c\u5f15\u7528\u53ef\u4ee5\u5c06\u521d\u59cb\u5316\u5b83\u4eec\u7684\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u5ef6\u957f\u5230\u81ea\u5df1\u7684\u751f\u547d\u5468\u671f (\u5bf9const\u5bf9\u8c61\u7684\u5de6\u503c\u5f15\u7528\u4e5f\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9)\u3002\u5176\u6b21\uff0c\u4f60==\u53ef\u4ee5\u901a\u8fc7\u6307\u5411\u975econst\u53f3\u503c\u5f15\u7528\u6765\u4fee\u6539\u53f3\u503c\u3002== \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include class Fraction { private : int m_numerator ; int m_denominator ; public : Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } }; int main () { auto && rref { Fraction { 3 , 5 } }; // \u5bf9\u4e34\u65f6 Fraction \u7684\u53f3\u503c\u5f15\u7528 // operator<< \u7684\u53c2\u6570f1\u4f1a\u7ed1\u5b9a\u5230\u4e34\u65f6\u5bf9\u8c61\uff0c\u800c\u4e0d\u4f1a\u521b\u5efa\u62f7\u8d1d std :: cout << rref << '\\n' ; return 0 ; } // rref (\u548c\u4e34\u65f6\u7684Fraction) \u79bb\u5f00\u4f5c\u7528\u57df \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 3/5 \u4f5c\u4e3a\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff0c Fraction(3, 5) \u901a\u5e38\u4f1a\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u540e\u5c31\u79bb\u5f00\u4f5c\u7528\u57df\u3002\u4f46\u662f\uff0c\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528\u5b83\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\uff0c\u6240\u4ee5\u5b83\u7684\u6301\u7eed\u65f6\u95f4\u88ab\u5ef6\u957f\u5230\u4e86\u548c\u8be5\u53f3\u503c\u5f15\u7528\u4e00\u6837\u2014\u2014\u8bed\u53e5\u5757\u7ed3\u5c3e\u65f6\u79bb\u5f00\u4f5c\u7528\u57df\u3002\u7136\u540e\uff0c\u6211\u4eec\u53d8\u53ef\u4ee5\u901a\u8fc7\u8be5\u53f3\u503c\u5f15\u7528\u6253\u5370 Fraction \u7684\u503c\u3002 \u73b0\u5728\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u4e0d\u90a3\u4e48\u76f4\u89c2\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 #include int main () { int && rref { 5 }; // \u901a\u8fc7\u5b57\u9762\u91cf\u521d\u59cb\u5316\u53f3\u503c\u5f15\u7528\uff0c\u6b64\u65f6\u521b\u5efa\u4e86\u4e34\u65f6\u5bf9\u8c615 rref = 10 ; std :: cout << rref << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 10 \u867d\u7136\u7528\u5b57\u9762\u503c\u521d\u59cb\u5316\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\uff0c\u7136\u540e\u518d\u901a\u8fc7\u53f3\u503c\u5f15\u7528\u4fee\u6539\u8be5\u53f3\u503c\u770b\u8d77\u6765\u6709\u4e9b\u5947\u602a\u3002\u4f46\u662f\u5f53\u6211\u4eec\u8fd9\u4e48\u505a\u65f6\uff0c\u4f1a\u4ece\u5b57\u9762\u503c\u6784\u9020\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff0c\u6b64\u65f6\u53f3\u503c\u5f15\u7528\u5f15\u7528\u7684\u5bf9\u8c61\u662f\u8be5\u4e34\u65f6\u5bf9\u8c61\u800c\u4e0d\u662f\u5b57\u9762\u91cf\u672c\u8eab\u3002 \u4e0d\u8fc7\uff0c\u4e0a\u9762\u4e24\u4e2a\u4f8b\u5b50\u5e76\u4e0d\u662f\u5e38\u89c1\u7684\u53f3\u503c\u4f7f\u7528\u573a\u5408\u3002","title":"R-value references"},{"location":"M-2-R-value-references/#_2","text":"\u53f3\u503c\u5f15\u7528\u901a\u5e38\u7528\u4f5c\u51fd\u6570\u5f62\u53c2 \u3002\u5728\u91cd\u8f7d\u51fd\u6570\u65f6\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u51fd\u6570\u7684\u884c\u4e3a\u5728\u5904\u7406\u5de6\u503c\u53f3\u503c\u65f6\u4e0d\u540c\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u53f3\u503c\u4f5c\u4e3a\u51fd\u6570\u5f62\u53c2\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include void fun ( const int & lref ) // \u5de6\u503c\u5f62\u53c2\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570 { std :: cout << \"l-value reference to const \\n \" ; } void fun ( int && rref ) // \u53f3\u503c\u5f62\u53c2\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570 { std :: cout << \"r-value reference \\n \" ; } int main () { int x { 5 }; fun ( x ); // l-value argument calls l-value version of function fun ( 5 ); // r-value argument calls r-value version of function return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 l-value reference to const r-value reference \u5982\u4f60\u6240\u89c1\uff0c\u5f53\u4f20\u9012\u4e00\u4e2a\u5de6\u503c\u65f6\uff0c\u91cd\u8f7d\u51fd\u6570\u89e3\u6790\u4e3a\u5177\u6709\u5de6\u503c\u5f15\u7528\u7684\u7248\u672c\u3002\u5f53\u4f20\u9012\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\uff0c\u91cd\u8f7d\u51fd\u6570\u89e3\u6790\u5230\u5177\u6709\u53f3\u503c\u5f15\u7528\u7684\u7248\u672c(\u8fd9\u88ab\u8ba4\u4e3a\u662f\u6bd4\u5de6\u503c\u5f15\u7528const\u66f4\u597d\u7684\u5339\u914d)\u3002 \u4e3a\u4ec0\u4e48\u8981\u8fd9\u4e48\u505a\u5462\uff1f\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8bfe\u4e2d\u66f4\u8be6\u7ec6\u5730\u8ba8\u8bba\u8fd9\u4e2a\u95ee\u9898\u3002\u6beb\u65e0\u7591\u95ee\uff0c\u8fd9\u662f \u79fb\u52a8\u8bed\u4e49 \u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002 \u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a 1 2 int && ref { 5 }; fun ( ref ); \u4e0a\u9762\u4ee3\u7801\u5b9e\u9645\u4e0a\u4f1a\u8c03\u7528\u5de6\u503c\u5f15\u7528\u7248\u672c\u7684\u51fd\u6570\uff01\u5c3d\u7ba1 ref \u662f\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\uff0c\u4f46\u662f\u5b83\u672c\u8eab\u662f\u4e00\u4e2a\u5de6\u503c\uff08\u6240\u6709\u6709\u540d\u5b57\u7684\u53d8\u91cf\u90fd\u662f\u5de6\u503c\uff09\u3002\u9020\u6210\u6df7\u6dc6\u7684\u6839\u672c\u539f\u56e0\u5728\u4e8e\uff0c\u6211\u4eec\u5728\u4e0d\u540c\u7684\u8bed\u5883\u4e2d\u4f7f\u7528\u4e86\u672f\u8bed\u201d\u53f3\u503c\u201c\u3002\u5e94\u8be5\u8fd9\u6837\u7406\u89e3\uff1a \u547d\u540d\u5bf9\u8c61\u662f\u5de6\u503c\uff0c\u533f\u540d\u5bf9\u8c61\u662f\u53f3\u503c\u3002\u547d\u540d\u5bf9\u8c61\u6216\u533f\u540d\u5bf9\u8c61\u7684\u7c7b\u578b\u4e0e\u5b83\u662f\u5de6\u503c\u8fd8\u662f\u53f3\u503c\u65e0\u5173 \u3002\u6216\u8005\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c\u53f3\u503c\u5f15\u7528\u4e0d\u53eb\u53f3\u503c\u5f15\u7528\u7684\u8bdd\uff0c\u5c31\u4e0d\u4f1a\u51fa\u73b0\u8fd9\u79cd\u65e0\u89e3\u4e86\u3002","title":"\u4f7f\u7528\u53f3\u503c\u5f15\u7528\u4f5c\u4e3a\u51fd\u6570\u5f62\u53c2"},{"location":"M-2-R-value-references/#_3","text":"\u60a8\u51e0\u4e4e\u4e0d\u5e94\u8be5\u8fd4\u56de\u53f3\u503c\u7684\u5f15\u7528\uff0c\u539f\u56e0\u4e0e\u4f60\u51e0\u4e4e\u4e0d\u5e94\u8be5\u8fd4\u56de\u5de6\u503c\u7684\u5f15\u7528\u76f8\u540c \u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5f53\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u6700\u7ec8\u4f1a\u6210\u4e3a\u4e00\u4e2a \u60ac\u5782 \u5f15\u7528\u3002","title":"\u8fd4\u56de\u53f3\u503c\u5f15\u7528"},{"location":"M-3-move-constructors-and-move-assignment/","tags":["R-value-references","references"],"text":"Key Takeaway \u5b9e\u73b0\u62f7\u8d1d\u8bed\u4e49\u65f6\u9700\u8981\u4f7f\u7528const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528\u4f5c\u4e3a\u5f62\u53c2\uff0c\u800c\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49\u65f6\uff0c\u9700\u8981\u4f7f\u7528\u975econst\u7684\u53f3\u503c\u5f62\u53c2\uff1b \u7f16\u8bd1\u5668\u4e0d\u4f1a\u63d0\u4f9b\u9ed8\u8ba4\u7684\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u9664\u975e\u8be5\u7c7b\u6ca1\u6709\u5b9a\u4e49\u4efb\u4f55\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3001\u62f7\u8d1d\u8d4b\u503c\u3001\u79fb\u52a8\u8d4b\u503c\u6216\u6790\u6784\u51fd\u6570 \u5de6\u503c\u7a0d\u540e\u53ef\u80fd\u4f1a\u88ab\u4f7f\u7528\uff0c\u6240\u4ee5\u53ea\u6709\u62f7\u8d1d\u5b83\u662f\u5b89\u5168\u7684\u3002\u53f3\u503c\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u540e\u603b\u662f\u4f1a\u88ab\u9500\u6bc1\uff0c\u6240\u4ee5\u79fb\u52a8\u5b83\u662f\u5b89\u5168\u7684 \u6709\u4e86\u53f3\u503c\u5f15\u7528\uff0c\u51fd\u6570\u5c31\u80fd\u591f\u57fa\u4e8e\u53c2\u6570\u662f\u5de6\u503c\u8fd8\u662f\u53f3\u503c\u8868\u73b0\u51fa\u4e0d\u540c\u7684\u884c\u6587\uff0c\u4e5f\u5c31\u53ef\u4ee5\u9488\u5bf9\u53f3\u503c\u4f7f\u7528\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c \u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49\u65f6\uff0c\u8981\u4fdd\u6301\u53cc\u65b9\u90fd\u5904\u4e8e\u6709\u6548\u72b6\u6001 \u5c40\u90e8\u53d8\u91cf\u5de6\u503c\u4ece\u51fd\u6570\u4e2d\u6309\u503c\u8fd4\u56de\u65f6\uff0cC++\u4f1a\u79fb\u52a8\u5b83\u800c\u4e0d\u662f\u62f7\u8d1d\u5b83\uff0c\u5f53\u7136\u6709\u7684\u7f16\u8bd1\u5668\u4e5f\u4f1a\u76f4\u63a5\u5ffd\u7565\u3002 \u5728\u652f\u6301\u79fb\u52a8\u8bed\u4e49\u7684\u7c7b\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u5220\u9664\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u51fd\u6570\uff0c\u4ee5\u786e\u4fdd\u4e0d\u4f1a\u6267\u884c\u62f7\u8d1d \u5728 M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86 std::auto_ptr \uff0c\u8fdb\u800c\u5f15\u7533\u51fa\u4e86 \u79fb\u52a8\u8bed\u4e49 \u7684\u91cd\u8981\u6027\u3002\u7136\u540e\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u5f53\u5c06\u672c\u6765\u88ab\u8bbe\u8ba1\u4e3a\u62f7\u8d1d\u8bed\u4e49\u7684\u51fd\u6570\uff08 \u62f7\u8d1d\u521d\u59cb\u5316 \u548c \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \uff09\u88ab\u7528\u6765\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49\u65f6\u4f1a\u5e26\u6765\u7684\u95ee\u9898\u3002 \u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u6df1\u5165\u5206\u6790C++11\u662f\u5982\u4f55\u901a\u8fc7 \u79fb\u52a8\u6784\u9020\u51fd\u6570 \u548c \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 \u6765\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\u7684\u3002 \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c \u9996\u5148\uff0c\u590d\u4e60\u4e00\u4e0b \u62f7\u8d1d\u8bed\u4e49 \u3002 \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u7528\u4e8e\u901a\u8fc7\u62f7\u8d1d\u4e00\u4e2a\u540c\u7c7b\u5bf9\u8c61\u6765\u521d\u59cb\u5316\u53e6\u4e00\u4e2a\u5bf9\u8c61\u3002\u62f7\u8d1d\u8d4b\u503c\u5219\u7528\u4e8e\u5c06\u4e00\u4e2a\u7c7b\u5bf9\u8c61\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u73b0\u6709\u7c7b\u5bf9\u8c61\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u6ca1\u6709\u663e\u5f0f\u63d0\u4f9b\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0cC++\u4f1a\u63d0\u4f9b\u4e00\u4e2a\u9ed8\u8ba4\u7248\u672c\u3002\u8fd9\u4e9b\u7f16\u8bd1\u5668\u63d0\u4f9b\u7684\u51fd\u6570\u4f1a\u6267\u884c \u6d45\u62f7\u8d1d \uff0c\u8fd9\u53ef\u80fd\u4f1a\u7ed9\u5206\u914d\u52a8\u6001\u5185\u5b58\u7684\u7c7b\u5e26\u6765\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u5904\u7406\u52a8\u6001\u5185\u5b58\u7684\u7c7b\u5e94\u8be5\u91cd\u5199\u8fd9\u4e9b\u51fd\u6570\u6765\u8fdb\u884c \u6df1\u62f7\u8d1d \u3002 \u56de\u5230\u672c\u7ae0\u7b2c\u4e00\u8bfe\u4e2d\u7684 Auto_ptr \u667a\u80fd\u6307\u9488\u7c7b\u793a\u4f8b\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u5b9e\u73b0\u4e86\u6df1\u62f7\u8d1d\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u64cd\u4f5c\u7b26\u7684\u7248\u672c\u80fd\u5426\u6b63\u5e38\u5de5\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 #include template < typename T > class Auto_ptr3 { T * m_ptr ; public : Auto_ptr3 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } ~ Auto_ptr3 () { delete m_ptr ; } // \u62f7\u8d1d\u6784\u9020\u51fd\u6570 // \u5c06 a.m_ptr \u6df1\u62f7\u8d1d\u5230 m_ptr Auto_ptr3 ( const Auto_ptr3 & a ) { m_ptr = new T ; * m_ptr = * a . m_ptr ; //\u89e3\u5f15\u7528\u7684\u7ed3\u679c\u662f\u4e00\u4e2aResource\u5bf9\u8c61\uff0c\u5b83\u80fd\u591f\u901a\u8fc7\u9ed8\u8ba4\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u62f7\u8d1d\u8d4b\u503c\u81ea\u5df1 } // \u62f7\u8d1d\u8d4b\u503c // \u5c06 a.m_ptr \u6df1\u62f7\u8d1d\u5230 m_ptr Auto_ptr3 & operator = ( const Auto_ptr3 & a ) { // Self-assignment detection if ( & a == this ) return * this ; // Release any resource we're holding delete m_ptr ; // Copy the resource m_ptr = new T ; * m_ptr = * a . m_ptr ; return * this ; } T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } bool isNull () const { return m_ptr == nullptr ; } }; class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; Auto_ptr3 < Resource > generateResource () { Auto_ptr3 < Resource > res { new Resource }; return res ; // \u8fd4\u56de\u503c\u4f1a\u89e6\u53d1\u62f7\u8d1d\u6784\u9020\u51fd\u6570 } int main () { Auto_ptr3 < Resource > mainres ; mainres = generateResource (); // \u8d4b\u503c\u4f1a\u89e6\u53d1\u62f7\u8d1d\u8d4b\u503c return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c generateResource() \u51fd\u6570\u4f1a\u521b\u5efa\u4e00\u4e2a\u88ab\u667a\u80fd\u6307\u9488\u5c01\u88c5\u7684\u8d44\u6e90\uff0c\u7136\u540e\u8be5\u8d44\u6e90\u4f1a\u88ab\u8fd4\u56de\u7ed9\u4e3b\u51fd\u6570\u3002\u4e3b\u51fd\u6570\u968f\u540e\u5c06\u5176\u8d4b\u503c\u7ed9 Auto_ptr3 \u5bf9\u8c61\u3002 \u7a0b\u5e8f\u8fd0\u884c\u65f6\u6253\u5370\uff1a 1 2 3 4 5 6 Resource acquired Resource acquired Resource destroyed Resource acquired Resource destroyed Resource destroyed (\u6ce8\u610f:\u5982\u679c\u7f16\u8bd1\u5668\u7701\u7565\u4e86generateResource()\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u4f60\u53ef\u80fd\u53ea\u4f1a\u5f97\u52304\u884c\u8f93\u51fa) \u5982\u6b64\u7b80\u5355\u7684\u7a0b\u5e8f\uff0c\u7adf\u7136\u8981\u521b\u5efa\u5e76\u9500\u6bc1\u8fd9\u4e48\u591a\u6b21\uff1f\u4ec0\u4e48\u60c5\u51b5\uff01 \u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\u67096\u4e2a\u5173\u952e\u6b65\u9aa4(\u6bcf\u4e2a\u6253\u5370\u4fe1\u606f\u4e00\u4e2a\u6b65\u9aa4)\uff1a \u5728 generateResource() \u4e2d\uff0c\u5c40\u90e8\u53d8\u91cf res \u88ab\u521b\u5efa\uff0c\u540c\u65f6\u88ab\u4e00\u4e2a\u52a8\u6001\u5206\u914d\u7684 Resource \u5bf9\u8c61\u521d\u59cb\u5316\uff0c\u56e0\u6b64\u7b2c\u4e00\u6b21\u6253\u5370\u4e86 \u201cResource acquired\u201d\uff1b Res \u6309\u503c\u8fd4\u56de main() \u3002\u8fd9\u91cc\u4f7f\u7528\u6309\u503c\u8fd4\u56de\u662f\u56e0\u4e3a res \u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u5982\u679c\u6309\u5730\u5740\u8fd4\u56de\u6216\u6309\u5f15\u7528\u8fd4\u56de\u5219\u4f1a\u5e26\u6765\u95ee\u9898\u56e0\u4e3a res \u5728 generateResource() \u51fd\u6570\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u4e86\u3002\u56e0\u6b64\uff0c res \u4f1a\u88ab==\u62f7\u8d1d\u6784\u5efa\u5230\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\u4e2d==\u3002\u56e0\u4e3a\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4f7f\u7528\u4e86\u6df1\u62f7\u8d1d\uff0c\u6240\u4ee5\u5b83\u5206\u914d\u4e86\u4e00\u4e2a\u65b0\u7684 Resource \uff0c\u6240\u4ee5\u7b2c\u4e8c\u6b21\u6253\u5370\u4e86 \u201cResource acquired\u201d\uff1b Res \u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u6700\u521d\u88ab\u521b\u5efa\u7684 Resource \u5bf9\u8c61\u5c31\u9500\u6bc1\u4e86\uff0c\u56e0\u6b64\u7b2c\u4e00\u6253\u5370\u4e86 \u201cResource destroyed\u201d\uff1b \u4e34\u65f6\u5bf9\u4e8e\u901a\u8fc7\u62f7\u8d1d\u8d4b\u503c\u88ab\u8d4b\u503c\u7ed9\u4e86 mainres \u3002\u56e0\u4e3a\u6b64\u5904\u7684\u62f7\u8d1d\u8d4b\u503c\u4f7f\u7528\u4e86\u6df1\u62f7\u8d1d\uff0c\u6240\u4ee5\u5b83\u5206\u914d\u4e86\u4e00\u4e2a\u65b0\u7684 Resource \uff0c\u6240\u4ee5\u7b2c\u4e09\u6b21\u6253\u5370\u4e86 \u201cResource acquired\u201d\uff1b \u5f53\u8d4b\u503c\u8868\u8fbe\u5f0f\u7ed3\u675f\u540e\uff0c\u4e34\u65f6\u5bf9\u8c61\u5c31\u79bb\u5f00\u4e86\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1\uff0c\u56e0\u6b64\u7b2c\u4e8c\u6b21\u6253\u5370\u4e86\u201cResource destroyed\u201d\uff1b \u5728 main() \u7684\u672b\u5c3e\uff0c mainres \u79bb\u5f00\u4e86\u4f5c\u7528\u57df\uff0c\u4e8e\u662f\u6253\u5370\u4e86\u6700\u540e\u4e00\u4e2a \u201cResource destroyed\u201d \u3002 \u7b80\u800c\u8a00\u4e4b\uff0c\u6211\u4eec\u8c03\u7528\u4e86\u4e00\u6b21\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u62f7\u8d1d\u6784\u9020\u4e86 res \u5230\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff0c\u7136\u540e\u8c03\u7528\u4e86\u4e00\u6b21\u62f7\u8d1d\u8d4b\u503c\u5c06\u4e34\u65f6\u5bf9\u8c61\u590d\u5236\u5230 mainres \u5bf9\u8c61\uff0c\u6240\u4ee5\u6700\u7ec8\u603b\u5171\u5206\u914d\u548c\u9500\u6bc1\u4e863\u4e2a\u5355\u72ec\u7684\u5bf9\u8c61\u3002 \u6548\u7387\u5f88\u4f4e\uff0c\u4f46\u81f3\u5c11\u4e0d\u4f1a\u5d29\u6e83\uff01 \u4f46\u662f\uff0c\u5982\u679c\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\u7684\u8bdd\uff0c\u6548\u679c\u4f1a\u66f4\u597d\uff01 \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c C++ 11\u4e3a\u4e86\u652f\u6301\u79fb\u52a8\u8bed\u4e49\uff0c\u65b0\u5b9a\u4e49\u4e86\u4e24\u4e2a\u51fd\u6570\uff1a\u4e00\u4e2a\u662f \u79fb\u52a8\u6784\u9020\u51fd\u6570 \uff0c\u4e00\u4e2a\u662f \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 \u3002\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u7684\u76ee\u7684\u662f\u5c06\u4e00\u4e2a\u5bf9\u8c61\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u5bf9\u8c61\uff0c\u800c\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u7684\u76ee\u7684\u662f\u5c06\u8d44\u6e90\u7684\u6240\u6709\u6743\u4ece\u4e00\u4e2a\u5bf9\u8c61\u8f6c\u79fb\u5230\u53e6\u4e00\u4e2a\u5bf9\u8c61(\u8fd9\u901a\u5e38\u6bd4\u590d\u5236\u6210\u672c\u4f4e\u5f97\u591a)\u3002 \u5b9a\u4e49\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\uff0c\u548c\u5b9a\u4e49\u5b83\u4eec\u7684\u201c\u62f7\u8d1d\u8bed\u4e49\u201d\u540c\u50da\u7c7b\u4f3c\u3002\u4e0d\u8fc7\uff0c \u5b9e\u73b0\u62f7\u8d1d\u8bed\u4e49\u65f6\u9700\u8981\u4f7f\u7528const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528\u4f5c\u4e3a\u5f62\u53c2\uff0c\u800c\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49\u65f6\uff0c\u9700\u8981\u4f7f\u7528\u975econst\u7684\u53f3\u503c\u5f62\u53c2\u3002 \u8fd9\u91cc\u662f\u4e0e\u4e0a\u9762\u76f8\u540c\u7684 Auto_ptr3 \u7c7b\uff0c\u6dfb\u52a0\u4e86\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002\u4e3a\u4e86\u8fdb\u884c\u6bd4\u8f83\uff0c\u6211\u4eec\u4fdd\u7559\u4e86\u6df1\u62f7\u8d1d\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 #include template < typename T > class Auto_ptr4 { T * m_ptr ; public : Auto_ptr4 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } ~ Auto_ptr4 () { delete m_ptr ; } // \u62f7\u8d1d\u6784\u9020\u51fd\u6570 // \u5c06 a.m_ptr \u6df1\u62f7\u8d1d\u5230 m_ptr Auto_ptr4 ( const Auto_ptr4 & a ) { m_ptr = new T ; * m_ptr = * a . m_ptr ; } // \u79fb\u52a8\u6784\u9020\u51fd\u6570 // \u8f6c\u79fb a.m_ptr \u7684\u8d44\u6e90\u7ed9 m_ptr Auto_ptr4 ( Auto_ptr4 && a ) noexcept //\u6ce8\u610f\u4f7f\u7528\u7684\u662f\u53f3\u503c\u5f15\u7528 : m_ptr ( a . m_ptr ) { a . m_ptr = nullptr ; // we'll talk more about this line below } // \u62f7\u8d1d\u8d4b\u503c // \u5c06 a.m_ptr \u6df1\u62f7\u8d1d\u5230 m_ptr Auto_ptr4 & operator = ( const Auto_ptr4 & a ) { // Self-assignment detection if ( & a == this ) return * this ; // Release any resource we're holding delete m_ptr ; // Copy the resource m_ptr = new T ; * m_ptr = * a . m_ptr ; return * this ; } // \u79fb\u52a8\u8d4b\u503c // \u8f6c\u79fb a.m_ptr \u7684\u8d44\u6e90\u7ed9 m_ptr Auto_ptr4 & operator = ( Auto_ptr4 && a ) noexcept { // Self-assignment detection if ( & a == this ) return * this ; // \u91ca\u653e\u5f53\u524d\u6301\u6709\u7684\u8d44\u6e90 delete m_ptr ; // \u8f6c\u79fb\u6240\u6709\u6743 m_ptr = a . m_ptr ; a . m_ptr = nullptr ; // we'll talk more about this line below return * this ; } T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } bool isNull () const { return m_ptr == nullptr ; } }; class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; Auto_ptr4 < Resource > generateResource () { Auto_ptr4 < Resource > res { new Resource }; return res ; // \u6b64\u5904\u4f1a\u8c03\u7528\u79fb\u52a8\u6784\u9020\u51fd\u6570 } int main () { Auto_ptr4 < Resource > mainres ; mainres = generateResource (); // \u6b64\u5904\u8d4b\u503c\u4f1a\u8c03\u7528\u79fb\u52a8\u8d4b\u503c return 0 ; } \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26\u5e76\u4e0d\u590d\u6742\u3002\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4e0d\u518d\u5c06\u6e90\u5bf9\u8c61( a )\u6df1\u62f7\u8d1d\u5230\u9690\u5f0f\u5bf9\u8c61\u4e2d\uff0c\u800c\u662f\u8f6c\u79fb\u4e86\u6e90\u5bf9\u8c61\u7684\u8d44\u6e90\u3002\u8fd9\u6d89\u53ca\u5230\u5c06\u6e90\u6307\u9488\u6d45\u62f7\u8d1d\u5230\u9690\u5f0f\u5bf9\u8c61\u4e2d\uff0c\u7136\u540e\u5c06\u6e90\u6307\u9488\u8bbe\u7f6e\u4e3a\u7a7a\u3002 \u7a0b\u5e8f\u8fd0\u884c\u8f93\u51fa\uff1a 1 2 Resource acquired Resource destroyed \u6548\u679c\u597d\u6781\u4e86\uff01 \u7a0b\u5e8f\u7684\u6d41\u7a0b\u548c\u4e4b\u524d\u7684\u5b8c\u5168\u4e00\u6837\u3002\u4f46\u662f\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\u8c03\u7528\u7684\u4e0d\u662f\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u800c\u662f\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002\u518d\u6df1\u5165\u4e00\u70b9\u770b\uff1a \u5728 generateResource() \uff0c\u5c40\u90e8\u53d8\u91cf res \u88ab\u521b\u5efa\uff0c\u540c\u65f6\u88ab\u4e00\u4e2a\u52a8\u6001\u5206\u914d\u7684 Resource \u5bf9\u8c61\u521d\u59cb\u5316\uff0c\u56e0\u6b64\u7b2c\u4e00\u6b21\u6253\u5370\u4e86 \u201cResource acquired\u201d\uff1b Res \u6309\u503c\u8fd4\u56de main() \u3002 res \u4f1a\u88ab==\u79fb\u52a8\u6784\u9020\u5230\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\u4e2d==\uff0c\u7136\u540e\u5c06\u5b58\u653e\u5728 res \u52a8\u6001\u521b\u5efa\u7684\u5bf9\u8c61\u7684\u6240\u6709\u6743\u8f6c\u79fb\u7ed9\u4e86\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff1b Res \u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u4f46\u662f\u7531\u4e8e res \u5df2\u7ecf\u4e0d\u7ba1\u7406\u6307\u9488\u4e86\uff08\u5df2\u7ecf\u8f6c\u79fb\u7ed9\u4e86\u4e34\u65f6\u5bf9\u8c61\uff09\uff0c\u6240\u4ee5\u4ec0\u4e48\u4e8b\u60c5\u90fd\u4e0d\u4f1a\u53d1\u751f\uff1b \u4e34\u65f6\u5bf9\u8c61\u88ab\u79fb\u52a8\u8d4b\u503c\u7ed9 mainres \uff0c\u6b64\u65f6\u5b58\u653e\u5728\u4e34\u65f6\u5bf9\u8c61\u4e2d\u7684\u52a8\u6001\u521b\u5efa\u7684\u8d44\u6e90\u5c31\u88ab\u8f6c\u79fb\u7ed9\u4e86 mainres \uff1b \u5f53\u8d4b\u503c\u8868\u8fbe\u5f0f\u7ed3\u675f\u65f6\uff0c\u4e34\u65f6\u5bf9\u8c61\u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1\uff0c\u4f46\u662f\u7531\u4e8e\u5b83\u5df2\u7ecf\u4e0d\u518d\u7ba1\u7406\u4efb\u4f55\u6307\u9488\uff08\u5df2\u7ecf\u8f6c\u79fb\u7ed9\u4e86 mainres \uff09\uff0c\u6240\u4ee5\u4ecd\u7136\u4ec0\u4e48\u90fd\u4e0d\u4f1a\u53d1\u751f\u3002 \u5728 main() \u7ed3\u675f\u7684\u5730\u65b9\uff0c mainres \u4f1a\u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u6240\u4ee5\u6253\u5370\u4e86 \u201cResource destroyed\u201d \u3002 \u56e0\u6b64\uff0c\u8fd9\u6b21\uff0c Resource \u5e76\u6ca1\u6709\u88ab\u590d\u5236\u4e24\u6b21(\u4e00\u6b21\u662f\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u4e00\u6b21\u662f\u62f7\u8d1d\u8d4b\u503c)\uff0c\u800c\u662f\u5c06\u5176\u8f6c\u79fb\u4e86\u4e24\u6b21\u3002\u8fd9\u4e48\u505a\u6548\u7387\u660e\u663e\u66f4\u9ad8\uff0c\u56e0\u4e3a\u8d44\u6e90\u53ea\u6784\u5efa\u548c\u6467\u6bc1\u4e00\u6b21\uff0c\u800c\u4e0d\u662f\u4e09\u6b21\u3002 \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u4f1a\u5728\u4f55\u65f6\u88ab\u8c03\u7528\uff1f \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u5728\u5b9a\u4e49\u540e\u624d\u4f1a\u88ab\u8c03\u7528\uff0c\u800c\u4e14\u7528\u4e8e\u6784\u9020\u6216\u8d4b\u503c\u7684\u53c2\u6570\u9700\u8981\u662f\u4e00\u4e2a\u53f3\u503c\u3002\u6700\u5178\u578b\u7684\u662f\uff0c\u8fd9\u4e2a\u53f3\u503c\u662f\u4e00\u4e2a\u5b57\u9762\u91cf\u6216\u4e34\u65f6\u503c\u3002 \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c \u7f16\u8bd1\u5668\u4e0d\u4f1a\u63d0\u4f9b\u9ed8\u8ba4\u7684\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u9664\u975e\u8be5\u7c7b\u6ca1\u6709\u5b9a\u4e49\u4efb\u4f55\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3001\u62f7\u8d1d\u8d4b\u503c\u3001\u79fb\u52a8\u8d4b\u503c\u6216\u6790\u6784\u51fd\u6570 \u3002 \u79fb\u52a8\u8bed\u4e49\u80cc\u540e\u7684\u5173\u952e\u4fe1\u606f \u73b0\u5728\u4f60\u5df2\u7ecf\u5177\u6709\u7406\u89e3\u79fb\u52a8\u8bed\u4e49\u80cc\u540e\u5173\u952e\u4fe1\u606f\u7684\u80cc\u666f\u77e5\u8bc6\u4e86\u3002 \u5f53\u4f7f\u7528\u4e00\u4e2a \u5de6\u503c \u5b9e\u53c2\u6784\u9020\u4e00\u4e2a\u5bf9\u8c61\u6216\u8fdb\u884c\u8d4b\u503c\u65f6\uff0c\u6211\u4eec\u552f\u4e00\u80fd\u505a\u7684\u5408\u7406\u7684\u4e8b\u60c5\u5c31\u662f\u590d\u5236\u8fd9\u4e2a\u5de6\u503c\u3002\u6211\u4eec\u4e0d\u80fd\u5047\u8bbe\u6539\u53d8\u53ef\u4ee5\u5b89\u5168\u5730\u6539\u53d8\u8fd9\u4e2a\u5de6\u503c\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u5728\u7a0d\u540e\u7684\u7a0b\u5e8f\u4e2d\u518d\u6b21\u88ab\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u8868\u8fbe\u5f0f a = b \uff0c\u6211\u4eec\u4e0d\u80fd\u671f\u671b\u6709\u4efb\u4f55\u5408\u7406\u7684\u65b9\u5f0f\u53ef\u4ee5\u7528\u6765\u4fee\u6539b\u7684\u503c\u3002 \u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a \u53f3\u503c \u6765\u6784\u9020\u4e00\u4e2a\u5bf9\u8c61\u6216\u8d4b\u503c\uff0c\u7531\u4e8e\u53f3\u503c\u53ea\u662f\u67d0\u79cd\u7c7b\u578b\u7684\u4e34\u65f6\u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4e0d\u5fc5\u590d\u5236\u5b83(\u590d\u5236\u5f00\u9500\u5f88\u5927)\uff0c\u800c\u662f\u53ef\u4ee5\u7b80\u5355\u5730\u5c06\u5b83\u7ba1\u7406\u7684\u8d44\u6e90\u8f6c\u79fb\u5230\u6b63\u5728\u6784\u9020\u6216\u5206\u914d\u7684\u5bf9\u8c61\u5373\u53ef\u3002\u8fd9\u6837\u505a\u662f\u5b89\u5168\u7684\uff0c\u56e0\u4e3a\u4e34\u65f6\u53d8\u91cf\u65e0\u8bba\u5982\u4f55\u90fd\u4f1a\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u3002\u663e\u7136\uff0c\u5b83\u6c38\u8fdc\u4e0d\u4f1a\u518d\u88ab\u4f7f\u7528! c++ 11\u901a\u8fc7 \u53f3\u503c\u5f15\u7528 \uff0c\u4f7f\u6211\u4eec\u80fd\u591f\u5728\u5b9e\u53c2\u662f\u53f3\u503c\u548c\u5de6\u503c\u65f6\u9009\u62e9\u4e0d\u540c\u7684\u884c\u4e3a\uff0c\u4ee5\u4fbf\u8bbe\u8ba1\u51fa\u66f4\u806a\u660e\u3001\u66f4\u6709\u6548\u7684\u5bf9\u8c61\u884c\u4e3a\u3002 \u79fb\u52a8\u51fd\u6570\u5e94\u8be5\u4fdd\u6301\u4e24\u4e2a\u5bf9\u8c61\u90fd\u5904\u4e8e\u6709\u6548\u72b6\u6001 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4e0d\u8bba\u662f\u79fb\u52a8\u6784\u9020\u51fd\u6570\u8fd8\u79fb\u52a8\u8d4b\u503c\u51fd\u6570\uff0c\u90fd\u4f1a\u5c06 a.m_ptr \u8bbe\u7f6e\u4e3a nullptr \uff08\u7a7a\u6307\u9488\u5b57\u9762\u91cf\uff09\u3002\u8fd9\u4e48\u505a\u770b\u4e0a\u53bb\u6709\u70b9\u591a\u4f59\u2014\u2014\u6bd5\u7adf\u8fd9\u53ea\u662f\u4e00\u4e2a\u4e34\u65f6\u7684\u53f3\u503c\u3002\u65e2\u7136\u5b83\u7a0d\u540e\u5c31\u4f1a\u88ab\u9500\u6bc1\uff0c\u4e3a\u5565\u8fd8\u8981\u624b\u5de5\u201c\u6e05\u7406\u201d\u5b83\u5462\uff1f \u7b54\u6848\u5f88\u7b80\u5355\uff1a\u5f53 a \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c\u6790\u6784\u51fd\u6570\u4f1a\u88ab\u8c03\u7528\uff0c\u6240\u4ee5 a.m_ptr \u4f1a\u88ab\u5220\u9664\u3002\u5728\u64cd\u4f5c\u6267\u884c\u65f6\uff0c\u5982\u679c a.m_ptr \u4ecd\u7136\u6307\u5411\u4e0e m_ptr \u6307\u5411\u76f8\u540c\u7684\u5bf9\u8c61\uff0c\u5219 m_ptr \u663e\u7136\u4f1a\u53d8\u6210\u4e00\u4e2a \u60ac\u5782 \u6307\u9488\u3002\u5f53\u6211\u4eec\u540e\u9762\u4f7f\u7528\uff08\u6216\u9500\u6bc1\uff09\u5305\u542b\u4e86 m_ptr \u7684\u5bf9\u8c61\u65f6\uff0c\u5c31\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u5728\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49\u65f6\uff0c\u786e\u4fdd\u88ab\u79fb\u52a8\u7684\u5bf9\u8c61\u4e5f\u5904\u4e8e\u6709\u6548\u72b6\u6001\u662f\u5f88\u91cd\u8981\u7684\uff0c\u8fd9\u6837\u5b83\u5c31\u53ef\u4ee5\u6b63\u786e\u5730\u9500\u6bc1(\u800c\u4e0d\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u7684\u884c\u4e3a)\u3002 \u81ea\u52a8\u53d8\u91cf\u5de6\u503c\u6309\u503c\u8fd4\u56de\u65f6\u53ef\u80fd\u4f1a\u8fdb\u884c\u79fb\u52a8\u800c\u4e0d\u662f\u62f7\u8d1d \u5728\u4e0a\u9762 Auto_ptr4 \u793a\u4f8b\u7684 generateResource() \u51fd\u6570\u4e2d\uff0c\u5f53\u53d8\u91cf res \u6309\u503c\u8fd4\u56de\u65f6\uff0c\u5b83\u5c06\u88ab\u79fb\u52a8\u800c\u4e0d\u662f\u590d\u5236\uff0c\u5373\u4f7f res \u662f\u4e00\u4e2a\u5de6\u503c\u3002==C++\u89c4\u8303\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u89c4\u5219\uff0c\u5373\u51fd\u6570\u6309\u503c\u8fd4\u56de\u7684\u81ea\u52a8\u5bf9\u8c61\u53ef\u4ee5\u79fb\u52a8\uff0c\u5373\u4f7f\u5b83\u4eec\u662f\u5de6\u503c\u3002==\u8fd9\u662f\u6709\u9053\u7406\u7684\uff0c\u56e0\u4e3a res \u65e0\u8bba\u5982\u4f55\u90fd\u4f1a\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\uff01\u90a3\u4e48\u6211\u4eec\u8fd8\u4e0d\u5982\u7a83\u53d6\u5b83\u7684\u8d44\u6e90\uff0c\u800c\u4e0d\u662f\u6267\u884c\u6602\u8d35\u4e14\u65e0\u7528\u7684\u62f7\u8d1d\u64cd\u4f5c\u3002 \u867d\u7136\u7f16\u8bd1\u5668\u53ef\u4ee5\u79fb\u52a8\u5de6\u503c\u8fd4\u56de\u503c\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u80fd\u591f\u5b8c\u5168\u907f\u514d\u62f7\u8d1d(\u8fd9\u5b8c\u5168\u907f\u514d\u4e86\u590d\u5236\u6216\u79fb\u52a8\u7684\u9700\u8981)\u5c31\u66f4\u597d\u4e86\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u6784\u9020\u51fd\u6570\u751a\u81f3\u90fd\u4e0d\u4f1a\u88ab\u8c03\u7528\u3002 \u963b\u6b62\u62f7\u8d1d \u5728\u4e0a\u9762\u7684 Auto_ptr4 \u7c7b\u4e2d\uff0c\u4e3a\u4e86\u8fdb\u884c\u5bf9\u7167\uff0c\u6211\u4eec\u4fdd\u7559\u4e86\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u64cd\u4f5c\u7b26\u3002 \u4f46\u5728\u652f\u6301\u79fb\u52a8\u8bed\u4e49\u7684\u7c7b\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u5220\u9664\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u51fd\u6570\uff0c\u4ee5\u786e\u4fdd\u4e0d\u4f1a\u6267\u884c\u62f7\u8d1d \u3002\u5728 Auto_ptr \u7c7b\u4e2d\uff0c\u6211\u4eec\u4e0d\u60f3\u62f7\u8d1d\u6a21\u677f\u5316\u5bf9\u8c61T\u2014\u2014\u56e0\u4e3a\u5b83\u7684\u5f00\u9500\u5f88\u5927\uff0c\u800c\u4e14\u7c7bT\u751a\u81f3\u53ef\u80fd\u4e0d\u652f\u6301\u590d\u5236\uff01 \u4e0b\u9762\u662f Auto_ptr \u7684\u4e00\u4e2a\u7248\u672c\uff0c\u5b83\u652f\u6301\u79fb\u52a8\u8bed\u4e49\u4f46\u4e0d\u652f\u6301\u590d\u5236\u8bed\u4e49: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 #include template < typename T > class Auto_ptr5 { T * m_ptr ; public : Auto_ptr5 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } ~ Auto_ptr5 () { delete m_ptr ; } // \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u2014\u2014 \u4e0d\u5141\u8bb8\u62f7\u8d1d! Auto_ptr5 ( const Auto_ptr5 & a ) = delete ; // Move constructor // Transfer ownership of a.m_ptr to m_ptr Auto_ptr5 ( Auto_ptr5 && a ) noexcept : m_ptr ( a . m_ptr ) { a . m_ptr = nullptr ; } // \u62f7\u8d1d\u8d4b\u503c \u2014\u2014 \u4e0d\u5141\u8bb8\u62f7\u8d1d Auto_ptr5 & operator = ( const Auto_ptr5 & a ) = delete ; // Move assignment // Transfer ownership of a.m_ptr to m_ptr Auto_ptr5 & operator = ( Auto_ptr5 && a ) noexcept { // Self-assignment detection if ( & a == this ) return * this ; // Release any resource we're holding delete m_ptr ; // Transfer ownership of a.m_ptr to m_ptr m_ptr = a . m_ptr ; a . m_ptr = nullptr ; return * this ; } T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } bool isNull () const { return m_ptr == nullptr ; } }; \u5982\u679c\u4f60\u5c1d\u8bd5\u5c06\u4e00\u4e2a Auto_ptr5 \u7c7b\u578b\u7684\u5de6\u503c\u6309\u503c\u4f20\u9012\u7ed9\u4e00\u4e2a\u51fd\u6570\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u62b1\u6028\u7528\u4e8e\u521d\u59cb\u5316\u51fd\u6570\u53c2\u6570\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u88ab\u5220\u9664\u4e86\u3002\u8fd9\u662f\u597d\u4e8b\uff0c\u56e0\u4e3a\u6211\u4eec\u5e94\u8be5\u901a\u8fc7\u6309\u5f15\u7528\u4f20\u9012\u7684\u65b9\u5f0f\u4f20\u9012 Auto_ptr5 \uff01 Auto_ptr5 (\u7ec8\u4e8e)\u4e00\u4e2a\u5f88\u597d\u7684\u667a\u80fd\u6307\u9488\u7c7b\u3002\u800c\u4e14\uff0c\u5b9e\u9645\u4e0a\u6807\u51c6\u5e93\u5305\u542b\u4e00\u4e2a\u975e\u5e38\u7c7b\u4f3c\u4e8e\u5b83\u7684\u7c7b\uff0c\u540d\u4e3a std::unique_ptr \u3002\u6211\u4eec\u5c06\u5728\u672c\u7ae0\u540e\u9762\u8ba8\u8bba\u66f4\u591a\u5173\u4e8e std::unique_ptr \u7684\u5185\u5bb9\u3002 \u53e6\u4e00\u4e2a\u4f8b\u5b50 \u8ba9\u6211\u4eec\u6765\u770b\u770b\u53e6\u4e00\u4e2a\u4f7f\u7528\u52a8\u6001\u5185\u5b58\u7684\u7c7b\uff1a\u4e00\u4e2a\u7b80\u5355\u7684\u52a8\u6001\u7684\u6a21\u677f\u5316\u6570\u7ec4\u3002\u8be5\u7c7b\u5305\u542b\u4e00\u4e2a \u6df1\u62f7\u8d1d \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 #include template < typename T > class DynamicArray { private : T * m_array ; int m_length ; public : DynamicArray ( int length ) : m_array ( new T [ length ]), m_length ( length ) { } ~ DynamicArray () { delete [] m_array ; } // Copy constructor DynamicArray ( const DynamicArray & arr ) : m_length ( arr . m_length ) { m_array = new T [ m_length ]; for ( int i = 0 ; i < m_length ; ++ i ) m_array [ i ] = arr . m_array [ i ]; } // Copy assignment DynamicArray & operator = ( const DynamicArray & arr ) { if ( & arr == this ) return * this ; delete [] m_array ; m_length = arr . m_length ; m_array = new T [ m_length ]; for ( int i = 0 ; i < m_length ; ++ i ) m_array [ i ] = arr . m_array [ i ]; return * this ; } int getLength () const { return m_length ; } T & operator []( int index ) { return m_array [ index ]; } const T & operator []( int index ) const { return m_array [ index ]; } }; \u73b0\u5728\u8ba9\u6211\u4eec\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u7c7b\u3002\u4e3a\u4e86\u6f14\u793a\u5728\u5806\u4e0a\u5206\u914d\u4e00\u767e\u4e07\u4e2a\u6574\u6570\u65f6\u8be5\u7c7b\u7684\u6267\u884c\u60c5\u51b5\uff0c\u6211\u4eec\u5c06\u5229\u7528\u5728\u7b2c 13.18 -\u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 \u4e2d\u5f00\u53d1\u7684Timer\u7c7b\u3002\u6211\u4eec\u5c06\u4f7f\u7528Timer\u7c7b\u6765\u5bf9\u4ee3\u7801\u8fd0\u884c\u65f6\u95f4\u8fdb\u884c\u7edf\u8ba1\uff0c\u4ee5\u5c55\u793a\u62f7\u8d1d\u548c\u79fb\u52a8\u4e4b\u95f4\u7684\u6027\u80fd\u5dee\u5f02\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include #include // for std::chrono functions // Uses the above DynamicArray class class Timer { private : // Type aliases to make accessing nested type easier using Clock = std :: chrono :: high_resolution_clock ; using Second = std :: chrono :: duration < double , std :: ratio < 1 > > ; std :: chrono :: time_point < Clock > m_beg { Clock :: now () }; public : void reset () { m_beg = Clock :: now (); } double elapsed () const { return std :: chrono :: duration_cast < Second > ( Clock :: now () - m_beg ). count (); } }; // Return a copy of arr with all of the values doubled DynamicArray < int > cloneArrayAndDouble ( const DynamicArray < int > & arr ) { DynamicArray < int > dbl ( arr . getLength ()); for ( int i = 0 ; i < arr . getLength (); ++ i ) dbl [ i ] = arr [ i ] * 2 ; return dbl ; } int main () { Timer t ; DynamicArray < int > arr ( 1000000 ); for ( int i = 0 ; i < arr . getLength (); i ++ ) arr [ i ] = i ; arr = cloneArrayAndDouble ( arr ); std :: cout << t . elapsed (); } \u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\uff0c\u4ee5release\u6a21\u5f0f\u7f16\u8bd1\uff0c\u7a0b\u5e8f\u6267\u884c\u8017\u65f6 0.00825559 \u79d2\u3002 \u518d\u6b21\u8fd0\u884c\u76f8\u540c\u7684\u7a0b\u5e8f\uff0c\u540c\u65f6\u5c06\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u66ff\u66ff\u6362\u4e3a\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 template < typename T > class DynamicArray { private : T * m_array ; int m_length ; public : DynamicArray ( int length ) : m_array ( new T [ length ]), m_length ( length ) { } ~ DynamicArray () { delete [] m_array ; } // Copy constructor DynamicArray ( const DynamicArray & arr ) = delete ; // Copy assignment DynamicArray & operator = ( const DynamicArray & arr ) = delete ; // Move constructor DynamicArray ( DynamicArray && arr ) noexcept : m_array ( arr . m_array ), m_length ( arr . m_length ) { arr . m_length = 0 ; arr . m_array = nullptr ; } // Move assignment DynamicArray & operator = ( DynamicArray && arr ) noexcept { if ( & arr == this ) return * this ; delete [] m_array ; m_length = arr . m_length ; m_array = arr . m_array ; arr . m_length = 0 ; arr . m_array = nullptr ; return * this ; } int getLength () const { return m_length ; } T & operator []( int index ) { return m_array [ index ]; } const T & operator []( int index ) const { return m_array [ index ]; } }; #include #include // for std::chrono functions class Timer { private : // Type aliases to make accessing nested type easier using Clock = std :: chrono :: high_resolution_clock ; using Second = std :: chrono :: duration < double , std :: ratio < 1 > > ; std :: chrono :: time_point < Clock > m_beg { Clock :: now () }; public : void reset () { m_beg = Clock :: now (); } double elapsed () const { return std :: chrono :: duration_cast < Second > ( Clock :: now () - m_beg ). count (); } }; // Return a copy of arr with all of the values doubled DynamicArray < int > cloneArrayAndDouble ( const DynamicArray < int > & arr ) { DynamicArray < int > dbl ( arr . getLength ()); for ( int i = 0 ; i < arr . getLength (); ++ i ) dbl [ i ] = arr [ i ] * 2 ; return dbl ; } int main () { Timer t ; DynamicArray < int > arr ( 1000000 ); for ( int i = 0 ; i < arr . getLength (); i ++ ) arr [ i ] = i ; arr = cloneArrayAndDouble ( arr ); std :: cout << t . elapsed (); } \u540c\u6837\u7684\u673a\u5668\u4e0a\uff0c\u7a0b\u5e8f\u6267\u884c\u8017\u65f6 0.0056 \u79d2\u3002 \u6bd4\u8f83\u4e24\u4e2a\u7248\u672c\u7684\u8fd0\u884c\u65f6\u95f4\uff0c0.0056 / 0.00825559 = 67.8%\u3002\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\u7684\u7248\u672c\u901f\u5ea6\u5feb\u4e50 47.4% \uff01 \u4e0d\u8981\u4f7f\u7528 std::swap \u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49 \u7531\u4e8e\u79fb\u52a8\u8bed\u4e49\u7684\u76ee\u6807\u662f\u5c06\u8d44\u6e90\u4ece\u6e90\u5bf9\u8c61\u79fb\u52a8\u5230\u76ee\u6807\u5bf9\u8c61\uff0c\u6240\u4ee5\u4f60\u53ef\u80fd\u4f1a\u8003\u8651\u4f7f\u7528 std::swap() \u5b9e\u73b0\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002\u7136\u800c\uff0c\u8fd9\u5e76\u4e0d\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\uff0c\u56e0\u4e3a std::swap() \u4f1a\u5728\u53ef\u79fb\u52a8\u5bf9\u8c61\u4e0a\u8c03\u7528\u79fb\u52a8\u6784\u9020\u51fd\u6570 \u548c \u79fb\u52a8\u8d4b\u503c\uff0c\u8fd9\u5c06\u5bfc\u81f4\u65e0\u9650\u9012\u5f52\u3002\u4f60\u53ef\u4ee5\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u770b\u5230\u8fd9\u79cd\u60c5\u51b5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include #include class Name { private : std :: string m_name ; // std::string is move capable public : Name ( std :: string name ) : m_name { name } { } Name ( Name & name ) = delete ; Name & operator = ( Name & name ) = delete ; Name ( Name && name ) { std :: cout << \"Move ctor \\n \" ; std :: swap ( * this , name ); // bad! } Name & operator = ( Name && name ) { std :: cout << \"Move assign \\n \" ; std :: swap ( * this , name ); // bad! return * this ; } const std :: string & get () { return m_name ; } }; int main () { Name n1 { \"Alex\" }; n1 = Name { \"Joe\" }; // invokes move assignment std :: cout << n1 . get () << '\\n' ; return 0 ; } \u6253\u5370\uff1a 1 2 3 4 5 Move assign Move ctor Move ctor Move ctor Move ctor \u4e00\u76f4\u6253\u5370\u3002\u3002\u3002\u76f4\u5230\u51fd\u6570\u6808\u6ea2\u51fa\u3002 \u4f60\u53ef\u4ee5\u4f7f\u7528\u81ea\u5df1\u7684 swap \u51fd\u6570\u5b9e\u73b0\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\uff0c\u53ea\u8981\u8be5 swap \u51fd\u6570\u4e0d\u8981\u8c03\u7528\u79fb\u52a8\u6784\u9020\u51fd\u6570\u6216\u79fb\u52a8\u8d4b\u503c\u5373\u53ef\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 #include #include class Name { private : std :: string m_name ; public : Name ( std :: string name ) : m_name { name } { } Name ( Name & name ) = delete ; Name & operator = ( Name & name ) = delete ; // Create our own swap friend function to swap the members of Name friend void swap ( Name & a , Name & b ) noexcept { // \u5bf9std::string \u7c7b\u578b\u7684\u6210\u5458\u76f4\u63a5\u4f7f\u7528std:swap // not on Name std :: swap ( a . m_name , b . m_name ); } Name ( Name && name ) { std :: cout << \"Move ctor \\n \" ; swap ( * this , name ); // Now calling our swap, not std::swap } Name & operator = ( Name && name ) { std :: cout << \"Move assign \\n \" ; swap ( * this , name ); // Now calling our swap, not std::swap return * this ; } }; int main () { Name n1 { \"Alex\" }; n1 = Name { \"Joe\" }; // invokes move assignment std :: cout << n1 . get () << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u7b26\u5408\u9884\u671f\uff1a 1 2 Move assign Joe","title":"M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c"},{"location":"M-3-move-constructors-and-move-assignment/#_1","text":"\u9996\u5148\uff0c\u590d\u4e60\u4e00\u4e0b \u62f7\u8d1d\u8bed\u4e49 \u3002 \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u7528\u4e8e\u901a\u8fc7\u62f7\u8d1d\u4e00\u4e2a\u540c\u7c7b\u5bf9\u8c61\u6765\u521d\u59cb\u5316\u53e6\u4e00\u4e2a\u5bf9\u8c61\u3002\u62f7\u8d1d\u8d4b\u503c\u5219\u7528\u4e8e\u5c06\u4e00\u4e2a\u7c7b\u5bf9\u8c61\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u73b0\u6709\u7c7b\u5bf9\u8c61\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u6ca1\u6709\u663e\u5f0f\u63d0\u4f9b\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0cC++\u4f1a\u63d0\u4f9b\u4e00\u4e2a\u9ed8\u8ba4\u7248\u672c\u3002\u8fd9\u4e9b\u7f16\u8bd1\u5668\u63d0\u4f9b\u7684\u51fd\u6570\u4f1a\u6267\u884c \u6d45\u62f7\u8d1d \uff0c\u8fd9\u53ef\u80fd\u4f1a\u7ed9\u5206\u914d\u52a8\u6001\u5185\u5b58\u7684\u7c7b\u5e26\u6765\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u5904\u7406\u52a8\u6001\u5185\u5b58\u7684\u7c7b\u5e94\u8be5\u91cd\u5199\u8fd9\u4e9b\u51fd\u6570\u6765\u8fdb\u884c \u6df1\u62f7\u8d1d \u3002 \u56de\u5230\u672c\u7ae0\u7b2c\u4e00\u8bfe\u4e2d\u7684 Auto_ptr \u667a\u80fd\u6307\u9488\u7c7b\u793a\u4f8b\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u5b9e\u73b0\u4e86\u6df1\u62f7\u8d1d\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u64cd\u4f5c\u7b26\u7684\u7248\u672c\u80fd\u5426\u6b63\u5e38\u5de5\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 #include template < typename T > class Auto_ptr3 { T * m_ptr ; public : Auto_ptr3 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } ~ Auto_ptr3 () { delete m_ptr ; } // \u62f7\u8d1d\u6784\u9020\u51fd\u6570 // \u5c06 a.m_ptr \u6df1\u62f7\u8d1d\u5230 m_ptr Auto_ptr3 ( const Auto_ptr3 & a ) { m_ptr = new T ; * m_ptr = * a . m_ptr ; //\u89e3\u5f15\u7528\u7684\u7ed3\u679c\u662f\u4e00\u4e2aResource\u5bf9\u8c61\uff0c\u5b83\u80fd\u591f\u901a\u8fc7\u9ed8\u8ba4\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u62f7\u8d1d\u8d4b\u503c\u81ea\u5df1 } // \u62f7\u8d1d\u8d4b\u503c // \u5c06 a.m_ptr \u6df1\u62f7\u8d1d\u5230 m_ptr Auto_ptr3 & operator = ( const Auto_ptr3 & a ) { // Self-assignment detection if ( & a == this ) return * this ; // Release any resource we're holding delete m_ptr ; // Copy the resource m_ptr = new T ; * m_ptr = * a . m_ptr ; return * this ; } T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } bool isNull () const { return m_ptr == nullptr ; } }; class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; Auto_ptr3 < Resource > generateResource () { Auto_ptr3 < Resource > res { new Resource }; return res ; // \u8fd4\u56de\u503c\u4f1a\u89e6\u53d1\u62f7\u8d1d\u6784\u9020\u51fd\u6570 } int main () { Auto_ptr3 < Resource > mainres ; mainres = generateResource (); // \u8d4b\u503c\u4f1a\u89e6\u53d1\u62f7\u8d1d\u8d4b\u503c return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c generateResource() \u51fd\u6570\u4f1a\u521b\u5efa\u4e00\u4e2a\u88ab\u667a\u80fd\u6307\u9488\u5c01\u88c5\u7684\u8d44\u6e90\uff0c\u7136\u540e\u8be5\u8d44\u6e90\u4f1a\u88ab\u8fd4\u56de\u7ed9\u4e3b\u51fd\u6570\u3002\u4e3b\u51fd\u6570\u968f\u540e\u5c06\u5176\u8d4b\u503c\u7ed9 Auto_ptr3 \u5bf9\u8c61\u3002 \u7a0b\u5e8f\u8fd0\u884c\u65f6\u6253\u5370\uff1a 1 2 3 4 5 6 Resource acquired Resource acquired Resource destroyed Resource acquired Resource destroyed Resource destroyed (\u6ce8\u610f:\u5982\u679c\u7f16\u8bd1\u5668\u7701\u7565\u4e86generateResource()\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u4f60\u53ef\u80fd\u53ea\u4f1a\u5f97\u52304\u884c\u8f93\u51fa) \u5982\u6b64\u7b80\u5355\u7684\u7a0b\u5e8f\uff0c\u7adf\u7136\u8981\u521b\u5efa\u5e76\u9500\u6bc1\u8fd9\u4e48\u591a\u6b21\uff1f\u4ec0\u4e48\u60c5\u51b5\uff01 \u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\u67096\u4e2a\u5173\u952e\u6b65\u9aa4(\u6bcf\u4e2a\u6253\u5370\u4fe1\u606f\u4e00\u4e2a\u6b65\u9aa4)\uff1a \u5728 generateResource() \u4e2d\uff0c\u5c40\u90e8\u53d8\u91cf res \u88ab\u521b\u5efa\uff0c\u540c\u65f6\u88ab\u4e00\u4e2a\u52a8\u6001\u5206\u914d\u7684 Resource \u5bf9\u8c61\u521d\u59cb\u5316\uff0c\u56e0\u6b64\u7b2c\u4e00\u6b21\u6253\u5370\u4e86 \u201cResource acquired\u201d\uff1b Res \u6309\u503c\u8fd4\u56de main() \u3002\u8fd9\u91cc\u4f7f\u7528\u6309\u503c\u8fd4\u56de\u662f\u56e0\u4e3a res \u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u5982\u679c\u6309\u5730\u5740\u8fd4\u56de\u6216\u6309\u5f15\u7528\u8fd4\u56de\u5219\u4f1a\u5e26\u6765\u95ee\u9898\u56e0\u4e3a res \u5728 generateResource() \u51fd\u6570\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u4e86\u3002\u56e0\u6b64\uff0c res \u4f1a\u88ab==\u62f7\u8d1d\u6784\u5efa\u5230\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\u4e2d==\u3002\u56e0\u4e3a\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4f7f\u7528\u4e86\u6df1\u62f7\u8d1d\uff0c\u6240\u4ee5\u5b83\u5206\u914d\u4e86\u4e00\u4e2a\u65b0\u7684 Resource \uff0c\u6240\u4ee5\u7b2c\u4e8c\u6b21\u6253\u5370\u4e86 \u201cResource acquired\u201d\uff1b Res \u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u6700\u521d\u88ab\u521b\u5efa\u7684 Resource \u5bf9\u8c61\u5c31\u9500\u6bc1\u4e86\uff0c\u56e0\u6b64\u7b2c\u4e00\u6253\u5370\u4e86 \u201cResource destroyed\u201d\uff1b \u4e34\u65f6\u5bf9\u4e8e\u901a\u8fc7\u62f7\u8d1d\u8d4b\u503c\u88ab\u8d4b\u503c\u7ed9\u4e86 mainres \u3002\u56e0\u4e3a\u6b64\u5904\u7684\u62f7\u8d1d\u8d4b\u503c\u4f7f\u7528\u4e86\u6df1\u62f7\u8d1d\uff0c\u6240\u4ee5\u5b83\u5206\u914d\u4e86\u4e00\u4e2a\u65b0\u7684 Resource \uff0c\u6240\u4ee5\u7b2c\u4e09\u6b21\u6253\u5370\u4e86 \u201cResource acquired\u201d\uff1b \u5f53\u8d4b\u503c\u8868\u8fbe\u5f0f\u7ed3\u675f\u540e\uff0c\u4e34\u65f6\u5bf9\u8c61\u5c31\u79bb\u5f00\u4e86\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1\uff0c\u56e0\u6b64\u7b2c\u4e8c\u6b21\u6253\u5370\u4e86\u201cResource destroyed\u201d\uff1b \u5728 main() \u7684\u672b\u5c3e\uff0c mainres \u79bb\u5f00\u4e86\u4f5c\u7528\u57df\uff0c\u4e8e\u662f\u6253\u5370\u4e86\u6700\u540e\u4e00\u4e2a \u201cResource destroyed\u201d \u3002 \u7b80\u800c\u8a00\u4e4b\uff0c\u6211\u4eec\u8c03\u7528\u4e86\u4e00\u6b21\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u62f7\u8d1d\u6784\u9020\u4e86 res \u5230\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff0c\u7136\u540e\u8c03\u7528\u4e86\u4e00\u6b21\u62f7\u8d1d\u8d4b\u503c\u5c06\u4e34\u65f6\u5bf9\u8c61\u590d\u5236\u5230 mainres \u5bf9\u8c61\uff0c\u6240\u4ee5\u6700\u7ec8\u603b\u5171\u5206\u914d\u548c\u9500\u6bc1\u4e863\u4e2a\u5355\u72ec\u7684\u5bf9\u8c61\u3002 \u6548\u7387\u5f88\u4f4e\uff0c\u4f46\u81f3\u5c11\u4e0d\u4f1a\u5d29\u6e83\uff01 \u4f46\u662f\uff0c\u5982\u679c\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\u7684\u8bdd\uff0c\u6548\u679c\u4f1a\u66f4\u597d\uff01","title":"\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c"},{"location":"M-3-move-constructors-and-move-assignment/#_2","text":"C++ 11\u4e3a\u4e86\u652f\u6301\u79fb\u52a8\u8bed\u4e49\uff0c\u65b0\u5b9a\u4e49\u4e86\u4e24\u4e2a\u51fd\u6570\uff1a\u4e00\u4e2a\u662f \u79fb\u52a8\u6784\u9020\u51fd\u6570 \uff0c\u4e00\u4e2a\u662f \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 \u3002\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u7684\u76ee\u7684\u662f\u5c06\u4e00\u4e2a\u5bf9\u8c61\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u5bf9\u8c61\uff0c\u800c\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u7684\u76ee\u7684\u662f\u5c06\u8d44\u6e90\u7684\u6240\u6709\u6743\u4ece\u4e00\u4e2a\u5bf9\u8c61\u8f6c\u79fb\u5230\u53e6\u4e00\u4e2a\u5bf9\u8c61(\u8fd9\u901a\u5e38\u6bd4\u590d\u5236\u6210\u672c\u4f4e\u5f97\u591a)\u3002 \u5b9a\u4e49\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\uff0c\u548c\u5b9a\u4e49\u5b83\u4eec\u7684\u201c\u62f7\u8d1d\u8bed\u4e49\u201d\u540c\u50da\u7c7b\u4f3c\u3002\u4e0d\u8fc7\uff0c \u5b9e\u73b0\u62f7\u8d1d\u8bed\u4e49\u65f6\u9700\u8981\u4f7f\u7528const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528\u4f5c\u4e3a\u5f62\u53c2\uff0c\u800c\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49\u65f6\uff0c\u9700\u8981\u4f7f\u7528\u975econst\u7684\u53f3\u503c\u5f62\u53c2\u3002 \u8fd9\u91cc\u662f\u4e0e\u4e0a\u9762\u76f8\u540c\u7684 Auto_ptr3 \u7c7b\uff0c\u6dfb\u52a0\u4e86\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002\u4e3a\u4e86\u8fdb\u884c\u6bd4\u8f83\uff0c\u6211\u4eec\u4fdd\u7559\u4e86\u6df1\u62f7\u8d1d\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 #include template < typename T > class Auto_ptr4 { T * m_ptr ; public : Auto_ptr4 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } ~ Auto_ptr4 () { delete m_ptr ; } // \u62f7\u8d1d\u6784\u9020\u51fd\u6570 // \u5c06 a.m_ptr \u6df1\u62f7\u8d1d\u5230 m_ptr Auto_ptr4 ( const Auto_ptr4 & a ) { m_ptr = new T ; * m_ptr = * a . m_ptr ; } // \u79fb\u52a8\u6784\u9020\u51fd\u6570 // \u8f6c\u79fb a.m_ptr \u7684\u8d44\u6e90\u7ed9 m_ptr Auto_ptr4 ( Auto_ptr4 && a ) noexcept //\u6ce8\u610f\u4f7f\u7528\u7684\u662f\u53f3\u503c\u5f15\u7528 : m_ptr ( a . m_ptr ) { a . m_ptr = nullptr ; // we'll talk more about this line below } // \u62f7\u8d1d\u8d4b\u503c // \u5c06 a.m_ptr \u6df1\u62f7\u8d1d\u5230 m_ptr Auto_ptr4 & operator = ( const Auto_ptr4 & a ) { // Self-assignment detection if ( & a == this ) return * this ; // Release any resource we're holding delete m_ptr ; // Copy the resource m_ptr = new T ; * m_ptr = * a . m_ptr ; return * this ; } // \u79fb\u52a8\u8d4b\u503c // \u8f6c\u79fb a.m_ptr \u7684\u8d44\u6e90\u7ed9 m_ptr Auto_ptr4 & operator = ( Auto_ptr4 && a ) noexcept { // Self-assignment detection if ( & a == this ) return * this ; // \u91ca\u653e\u5f53\u524d\u6301\u6709\u7684\u8d44\u6e90 delete m_ptr ; // \u8f6c\u79fb\u6240\u6709\u6743 m_ptr = a . m_ptr ; a . m_ptr = nullptr ; // we'll talk more about this line below return * this ; } T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } bool isNull () const { return m_ptr == nullptr ; } }; class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; Auto_ptr4 < Resource > generateResource () { Auto_ptr4 < Resource > res { new Resource }; return res ; // \u6b64\u5904\u4f1a\u8c03\u7528\u79fb\u52a8\u6784\u9020\u51fd\u6570 } int main () { Auto_ptr4 < Resource > mainres ; mainres = generateResource (); // \u6b64\u5904\u8d4b\u503c\u4f1a\u8c03\u7528\u79fb\u52a8\u8d4b\u503c return 0 ; } \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26\u5e76\u4e0d\u590d\u6742\u3002\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4e0d\u518d\u5c06\u6e90\u5bf9\u8c61( a )\u6df1\u62f7\u8d1d\u5230\u9690\u5f0f\u5bf9\u8c61\u4e2d\uff0c\u800c\u662f\u8f6c\u79fb\u4e86\u6e90\u5bf9\u8c61\u7684\u8d44\u6e90\u3002\u8fd9\u6d89\u53ca\u5230\u5c06\u6e90\u6307\u9488\u6d45\u62f7\u8d1d\u5230\u9690\u5f0f\u5bf9\u8c61\u4e2d\uff0c\u7136\u540e\u5c06\u6e90\u6307\u9488\u8bbe\u7f6e\u4e3a\u7a7a\u3002 \u7a0b\u5e8f\u8fd0\u884c\u8f93\u51fa\uff1a 1 2 Resource acquired Resource destroyed \u6548\u679c\u597d\u6781\u4e86\uff01 \u7a0b\u5e8f\u7684\u6d41\u7a0b\u548c\u4e4b\u524d\u7684\u5b8c\u5168\u4e00\u6837\u3002\u4f46\u662f\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\u8c03\u7528\u7684\u4e0d\u662f\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u800c\u662f\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002\u518d\u6df1\u5165\u4e00\u70b9\u770b\uff1a \u5728 generateResource() \uff0c\u5c40\u90e8\u53d8\u91cf res \u88ab\u521b\u5efa\uff0c\u540c\u65f6\u88ab\u4e00\u4e2a\u52a8\u6001\u5206\u914d\u7684 Resource \u5bf9\u8c61\u521d\u59cb\u5316\uff0c\u56e0\u6b64\u7b2c\u4e00\u6b21\u6253\u5370\u4e86 \u201cResource acquired\u201d\uff1b Res \u6309\u503c\u8fd4\u56de main() \u3002 res \u4f1a\u88ab==\u79fb\u52a8\u6784\u9020\u5230\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\u4e2d==\uff0c\u7136\u540e\u5c06\u5b58\u653e\u5728 res \u52a8\u6001\u521b\u5efa\u7684\u5bf9\u8c61\u7684\u6240\u6709\u6743\u8f6c\u79fb\u7ed9\u4e86\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff1b Res \u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u4f46\u662f\u7531\u4e8e res \u5df2\u7ecf\u4e0d\u7ba1\u7406\u6307\u9488\u4e86\uff08\u5df2\u7ecf\u8f6c\u79fb\u7ed9\u4e86\u4e34\u65f6\u5bf9\u8c61\uff09\uff0c\u6240\u4ee5\u4ec0\u4e48\u4e8b\u60c5\u90fd\u4e0d\u4f1a\u53d1\u751f\uff1b \u4e34\u65f6\u5bf9\u8c61\u88ab\u79fb\u52a8\u8d4b\u503c\u7ed9 mainres \uff0c\u6b64\u65f6\u5b58\u653e\u5728\u4e34\u65f6\u5bf9\u8c61\u4e2d\u7684\u52a8\u6001\u521b\u5efa\u7684\u8d44\u6e90\u5c31\u88ab\u8f6c\u79fb\u7ed9\u4e86 mainres \uff1b \u5f53\u8d4b\u503c\u8868\u8fbe\u5f0f\u7ed3\u675f\u65f6\uff0c\u4e34\u65f6\u5bf9\u8c61\u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1\uff0c\u4f46\u662f\u7531\u4e8e\u5b83\u5df2\u7ecf\u4e0d\u518d\u7ba1\u7406\u4efb\u4f55\u6307\u9488\uff08\u5df2\u7ecf\u8f6c\u79fb\u7ed9\u4e86 mainres \uff09\uff0c\u6240\u4ee5\u4ecd\u7136\u4ec0\u4e48\u90fd\u4e0d\u4f1a\u53d1\u751f\u3002 \u5728 main() \u7ed3\u675f\u7684\u5730\u65b9\uff0c mainres \u4f1a\u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u6240\u4ee5\u6253\u5370\u4e86 \u201cResource destroyed\u201d \u3002 \u56e0\u6b64\uff0c\u8fd9\u6b21\uff0c Resource \u5e76\u6ca1\u6709\u88ab\u590d\u5236\u4e24\u6b21(\u4e00\u6b21\u662f\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u4e00\u6b21\u662f\u62f7\u8d1d\u8d4b\u503c)\uff0c\u800c\u662f\u5c06\u5176\u8f6c\u79fb\u4e86\u4e24\u6b21\u3002\u8fd9\u4e48\u505a\u6548\u7387\u660e\u663e\u66f4\u9ad8\uff0c\u56e0\u4e3a\u8d44\u6e90\u53ea\u6784\u5efa\u548c\u6467\u6bc1\u4e00\u6b21\uff0c\u800c\u4e0d\u662f\u4e09\u6b21\u3002","title":"\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c"},{"location":"M-3-move-constructors-and-move-assignment/#_3","text":"\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u5728\u5b9a\u4e49\u540e\u624d\u4f1a\u88ab\u8c03\u7528\uff0c\u800c\u4e14\u7528\u4e8e\u6784\u9020\u6216\u8d4b\u503c\u7684\u53c2\u6570\u9700\u8981\u662f\u4e00\u4e2a\u53f3\u503c\u3002\u6700\u5178\u578b\u7684\u662f\uff0c\u8fd9\u4e2a\u53f3\u503c\u662f\u4e00\u4e2a\u5b57\u9762\u91cf\u6216\u4e34\u65f6\u503c\u3002 \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c \u7f16\u8bd1\u5668\u4e0d\u4f1a\u63d0\u4f9b\u9ed8\u8ba4\u7684\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u9664\u975e\u8be5\u7c7b\u6ca1\u6709\u5b9a\u4e49\u4efb\u4f55\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3001\u62f7\u8d1d\u8d4b\u503c\u3001\u79fb\u52a8\u8d4b\u503c\u6216\u6790\u6784\u51fd\u6570 \u3002","title":"\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u4f1a\u5728\u4f55\u65f6\u88ab\u8c03\u7528\uff1f"},{"location":"M-3-move-constructors-and-move-assignment/#_4","text":"\u73b0\u5728\u4f60\u5df2\u7ecf\u5177\u6709\u7406\u89e3\u79fb\u52a8\u8bed\u4e49\u80cc\u540e\u5173\u952e\u4fe1\u606f\u7684\u80cc\u666f\u77e5\u8bc6\u4e86\u3002 \u5f53\u4f7f\u7528\u4e00\u4e2a \u5de6\u503c \u5b9e\u53c2\u6784\u9020\u4e00\u4e2a\u5bf9\u8c61\u6216\u8fdb\u884c\u8d4b\u503c\u65f6\uff0c\u6211\u4eec\u552f\u4e00\u80fd\u505a\u7684\u5408\u7406\u7684\u4e8b\u60c5\u5c31\u662f\u590d\u5236\u8fd9\u4e2a\u5de6\u503c\u3002\u6211\u4eec\u4e0d\u80fd\u5047\u8bbe\u6539\u53d8\u53ef\u4ee5\u5b89\u5168\u5730\u6539\u53d8\u8fd9\u4e2a\u5de6\u503c\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u5728\u7a0d\u540e\u7684\u7a0b\u5e8f\u4e2d\u518d\u6b21\u88ab\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u8868\u8fbe\u5f0f a = b \uff0c\u6211\u4eec\u4e0d\u80fd\u671f\u671b\u6709\u4efb\u4f55\u5408\u7406\u7684\u65b9\u5f0f\u53ef\u4ee5\u7528\u6765\u4fee\u6539b\u7684\u503c\u3002 \u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a \u53f3\u503c \u6765\u6784\u9020\u4e00\u4e2a\u5bf9\u8c61\u6216\u8d4b\u503c\uff0c\u7531\u4e8e\u53f3\u503c\u53ea\u662f\u67d0\u79cd\u7c7b\u578b\u7684\u4e34\u65f6\u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4e0d\u5fc5\u590d\u5236\u5b83(\u590d\u5236\u5f00\u9500\u5f88\u5927)\uff0c\u800c\u662f\u53ef\u4ee5\u7b80\u5355\u5730\u5c06\u5b83\u7ba1\u7406\u7684\u8d44\u6e90\u8f6c\u79fb\u5230\u6b63\u5728\u6784\u9020\u6216\u5206\u914d\u7684\u5bf9\u8c61\u5373\u53ef\u3002\u8fd9\u6837\u505a\u662f\u5b89\u5168\u7684\uff0c\u56e0\u4e3a\u4e34\u65f6\u53d8\u91cf\u65e0\u8bba\u5982\u4f55\u90fd\u4f1a\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u3002\u663e\u7136\uff0c\u5b83\u6c38\u8fdc\u4e0d\u4f1a\u518d\u88ab\u4f7f\u7528! c++ 11\u901a\u8fc7 \u53f3\u503c\u5f15\u7528 \uff0c\u4f7f\u6211\u4eec\u80fd\u591f\u5728\u5b9e\u53c2\u662f\u53f3\u503c\u548c\u5de6\u503c\u65f6\u9009\u62e9\u4e0d\u540c\u7684\u884c\u4e3a\uff0c\u4ee5\u4fbf\u8bbe\u8ba1\u51fa\u66f4\u806a\u660e\u3001\u66f4\u6709\u6548\u7684\u5bf9\u8c61\u884c\u4e3a\u3002","title":"\u79fb\u52a8\u8bed\u4e49\u80cc\u540e\u7684\u5173\u952e\u4fe1\u606f"},{"location":"M-3-move-constructors-and-move-assignment/#_5","text":"\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4e0d\u8bba\u662f\u79fb\u52a8\u6784\u9020\u51fd\u6570\u8fd8\u79fb\u52a8\u8d4b\u503c\u51fd\u6570\uff0c\u90fd\u4f1a\u5c06 a.m_ptr \u8bbe\u7f6e\u4e3a nullptr \uff08\u7a7a\u6307\u9488\u5b57\u9762\u91cf\uff09\u3002\u8fd9\u4e48\u505a\u770b\u4e0a\u53bb\u6709\u70b9\u591a\u4f59\u2014\u2014\u6bd5\u7adf\u8fd9\u53ea\u662f\u4e00\u4e2a\u4e34\u65f6\u7684\u53f3\u503c\u3002\u65e2\u7136\u5b83\u7a0d\u540e\u5c31\u4f1a\u88ab\u9500\u6bc1\uff0c\u4e3a\u5565\u8fd8\u8981\u624b\u5de5\u201c\u6e05\u7406\u201d\u5b83\u5462\uff1f \u7b54\u6848\u5f88\u7b80\u5355\uff1a\u5f53 a \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c\u6790\u6784\u51fd\u6570\u4f1a\u88ab\u8c03\u7528\uff0c\u6240\u4ee5 a.m_ptr \u4f1a\u88ab\u5220\u9664\u3002\u5728\u64cd\u4f5c\u6267\u884c\u65f6\uff0c\u5982\u679c a.m_ptr \u4ecd\u7136\u6307\u5411\u4e0e m_ptr \u6307\u5411\u76f8\u540c\u7684\u5bf9\u8c61\uff0c\u5219 m_ptr \u663e\u7136\u4f1a\u53d8\u6210\u4e00\u4e2a \u60ac\u5782 \u6307\u9488\u3002\u5f53\u6211\u4eec\u540e\u9762\u4f7f\u7528\uff08\u6216\u9500\u6bc1\uff09\u5305\u542b\u4e86 m_ptr \u7684\u5bf9\u8c61\u65f6\uff0c\u5c31\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u5728\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49\u65f6\uff0c\u786e\u4fdd\u88ab\u79fb\u52a8\u7684\u5bf9\u8c61\u4e5f\u5904\u4e8e\u6709\u6548\u72b6\u6001\u662f\u5f88\u91cd\u8981\u7684\uff0c\u8fd9\u6837\u5b83\u5c31\u53ef\u4ee5\u6b63\u786e\u5730\u9500\u6bc1(\u800c\u4e0d\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u7684\u884c\u4e3a)\u3002","title":"\u79fb\u52a8\u51fd\u6570\u5e94\u8be5\u4fdd\u6301\u4e24\u4e2a\u5bf9\u8c61\u90fd\u5904\u4e8e\u6709\u6548\u72b6\u6001"},{"location":"M-3-move-constructors-and-move-assignment/#_6","text":"\u5728\u4e0a\u9762 Auto_ptr4 \u793a\u4f8b\u7684 generateResource() \u51fd\u6570\u4e2d\uff0c\u5f53\u53d8\u91cf res \u6309\u503c\u8fd4\u56de\u65f6\uff0c\u5b83\u5c06\u88ab\u79fb\u52a8\u800c\u4e0d\u662f\u590d\u5236\uff0c\u5373\u4f7f res \u662f\u4e00\u4e2a\u5de6\u503c\u3002==C++\u89c4\u8303\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u89c4\u5219\uff0c\u5373\u51fd\u6570\u6309\u503c\u8fd4\u56de\u7684\u81ea\u52a8\u5bf9\u8c61\u53ef\u4ee5\u79fb\u52a8\uff0c\u5373\u4f7f\u5b83\u4eec\u662f\u5de6\u503c\u3002==\u8fd9\u662f\u6709\u9053\u7406\u7684\uff0c\u56e0\u4e3a res \u65e0\u8bba\u5982\u4f55\u90fd\u4f1a\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\uff01\u90a3\u4e48\u6211\u4eec\u8fd8\u4e0d\u5982\u7a83\u53d6\u5b83\u7684\u8d44\u6e90\uff0c\u800c\u4e0d\u662f\u6267\u884c\u6602\u8d35\u4e14\u65e0\u7528\u7684\u62f7\u8d1d\u64cd\u4f5c\u3002 \u867d\u7136\u7f16\u8bd1\u5668\u53ef\u4ee5\u79fb\u52a8\u5de6\u503c\u8fd4\u56de\u503c\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u80fd\u591f\u5b8c\u5168\u907f\u514d\u62f7\u8d1d(\u8fd9\u5b8c\u5168\u907f\u514d\u4e86\u590d\u5236\u6216\u79fb\u52a8\u7684\u9700\u8981)\u5c31\u66f4\u597d\u4e86\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u6784\u9020\u51fd\u6570\u751a\u81f3\u90fd\u4e0d\u4f1a\u88ab\u8c03\u7528\u3002","title":"\u81ea\u52a8\u53d8\u91cf\u5de6\u503c\u6309\u503c\u8fd4\u56de\u65f6\u53ef\u80fd\u4f1a\u8fdb\u884c\u79fb\u52a8\u800c\u4e0d\u662f\u62f7\u8d1d"},{"location":"M-3-move-constructors-and-move-assignment/#_7","text":"\u5728\u4e0a\u9762\u7684 Auto_ptr4 \u7c7b\u4e2d\uff0c\u4e3a\u4e86\u8fdb\u884c\u5bf9\u7167\uff0c\u6211\u4eec\u4fdd\u7559\u4e86\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u64cd\u4f5c\u7b26\u3002 \u4f46\u5728\u652f\u6301\u79fb\u52a8\u8bed\u4e49\u7684\u7c7b\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u5220\u9664\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u51fd\u6570\uff0c\u4ee5\u786e\u4fdd\u4e0d\u4f1a\u6267\u884c\u62f7\u8d1d \u3002\u5728 Auto_ptr \u7c7b\u4e2d\uff0c\u6211\u4eec\u4e0d\u60f3\u62f7\u8d1d\u6a21\u677f\u5316\u5bf9\u8c61T\u2014\u2014\u56e0\u4e3a\u5b83\u7684\u5f00\u9500\u5f88\u5927\uff0c\u800c\u4e14\u7c7bT\u751a\u81f3\u53ef\u80fd\u4e0d\u652f\u6301\u590d\u5236\uff01 \u4e0b\u9762\u662f Auto_ptr \u7684\u4e00\u4e2a\u7248\u672c\uff0c\u5b83\u652f\u6301\u79fb\u52a8\u8bed\u4e49\u4f46\u4e0d\u652f\u6301\u590d\u5236\u8bed\u4e49: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 #include template < typename T > class Auto_ptr5 { T * m_ptr ; public : Auto_ptr5 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } ~ Auto_ptr5 () { delete m_ptr ; } // \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u2014\u2014 \u4e0d\u5141\u8bb8\u62f7\u8d1d! Auto_ptr5 ( const Auto_ptr5 & a ) = delete ; // Move constructor // Transfer ownership of a.m_ptr to m_ptr Auto_ptr5 ( Auto_ptr5 && a ) noexcept : m_ptr ( a . m_ptr ) { a . m_ptr = nullptr ; } // \u62f7\u8d1d\u8d4b\u503c \u2014\u2014 \u4e0d\u5141\u8bb8\u62f7\u8d1d Auto_ptr5 & operator = ( const Auto_ptr5 & a ) = delete ; // Move assignment // Transfer ownership of a.m_ptr to m_ptr Auto_ptr5 & operator = ( Auto_ptr5 && a ) noexcept { // Self-assignment detection if ( & a == this ) return * this ; // Release any resource we're holding delete m_ptr ; // Transfer ownership of a.m_ptr to m_ptr m_ptr = a . m_ptr ; a . m_ptr = nullptr ; return * this ; } T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } bool isNull () const { return m_ptr == nullptr ; } }; \u5982\u679c\u4f60\u5c1d\u8bd5\u5c06\u4e00\u4e2a Auto_ptr5 \u7c7b\u578b\u7684\u5de6\u503c\u6309\u503c\u4f20\u9012\u7ed9\u4e00\u4e2a\u51fd\u6570\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u62b1\u6028\u7528\u4e8e\u521d\u59cb\u5316\u51fd\u6570\u53c2\u6570\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u88ab\u5220\u9664\u4e86\u3002\u8fd9\u662f\u597d\u4e8b\uff0c\u56e0\u4e3a\u6211\u4eec\u5e94\u8be5\u901a\u8fc7\u6309\u5f15\u7528\u4f20\u9012\u7684\u65b9\u5f0f\u4f20\u9012 Auto_ptr5 \uff01 Auto_ptr5 (\u7ec8\u4e8e)\u4e00\u4e2a\u5f88\u597d\u7684\u667a\u80fd\u6307\u9488\u7c7b\u3002\u800c\u4e14\uff0c\u5b9e\u9645\u4e0a\u6807\u51c6\u5e93\u5305\u542b\u4e00\u4e2a\u975e\u5e38\u7c7b\u4f3c\u4e8e\u5b83\u7684\u7c7b\uff0c\u540d\u4e3a std::unique_ptr \u3002\u6211\u4eec\u5c06\u5728\u672c\u7ae0\u540e\u9762\u8ba8\u8bba\u66f4\u591a\u5173\u4e8e std::unique_ptr \u7684\u5185\u5bb9\u3002","title":"\u963b\u6b62\u62f7\u8d1d"},{"location":"M-3-move-constructors-and-move-assignment/#_8","text":"\u8ba9\u6211\u4eec\u6765\u770b\u770b\u53e6\u4e00\u4e2a\u4f7f\u7528\u52a8\u6001\u5185\u5b58\u7684\u7c7b\uff1a\u4e00\u4e2a\u7b80\u5355\u7684\u52a8\u6001\u7684\u6a21\u677f\u5316\u6570\u7ec4\u3002\u8be5\u7c7b\u5305\u542b\u4e00\u4e2a \u6df1\u62f7\u8d1d \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 #include template < typename T > class DynamicArray { private : T * m_array ; int m_length ; public : DynamicArray ( int length ) : m_array ( new T [ length ]), m_length ( length ) { } ~ DynamicArray () { delete [] m_array ; } // Copy constructor DynamicArray ( const DynamicArray & arr ) : m_length ( arr . m_length ) { m_array = new T [ m_length ]; for ( int i = 0 ; i < m_length ; ++ i ) m_array [ i ] = arr . m_array [ i ]; } // Copy assignment DynamicArray & operator = ( const DynamicArray & arr ) { if ( & arr == this ) return * this ; delete [] m_array ; m_length = arr . m_length ; m_array = new T [ m_length ]; for ( int i = 0 ; i < m_length ; ++ i ) m_array [ i ] = arr . m_array [ i ]; return * this ; } int getLength () const { return m_length ; } T & operator []( int index ) { return m_array [ index ]; } const T & operator []( int index ) const { return m_array [ index ]; } }; \u73b0\u5728\u8ba9\u6211\u4eec\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u7c7b\u3002\u4e3a\u4e86\u6f14\u793a\u5728\u5806\u4e0a\u5206\u914d\u4e00\u767e\u4e07\u4e2a\u6574\u6570\u65f6\u8be5\u7c7b\u7684\u6267\u884c\u60c5\u51b5\uff0c\u6211\u4eec\u5c06\u5229\u7528\u5728\u7b2c 13.18 -\u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 \u4e2d\u5f00\u53d1\u7684Timer\u7c7b\u3002\u6211\u4eec\u5c06\u4f7f\u7528Timer\u7c7b\u6765\u5bf9\u4ee3\u7801\u8fd0\u884c\u65f6\u95f4\u8fdb\u884c\u7edf\u8ba1\uff0c\u4ee5\u5c55\u793a\u62f7\u8d1d\u548c\u79fb\u52a8\u4e4b\u95f4\u7684\u6027\u80fd\u5dee\u5f02\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include #include // for std::chrono functions // Uses the above DynamicArray class class Timer { private : // Type aliases to make accessing nested type easier using Clock = std :: chrono :: high_resolution_clock ; using Second = std :: chrono :: duration < double , std :: ratio < 1 > > ; std :: chrono :: time_point < Clock > m_beg { Clock :: now () }; public : void reset () { m_beg = Clock :: now (); } double elapsed () const { return std :: chrono :: duration_cast < Second > ( Clock :: now () - m_beg ). count (); } }; // Return a copy of arr with all of the values doubled DynamicArray < int > cloneArrayAndDouble ( const DynamicArray < int > & arr ) { DynamicArray < int > dbl ( arr . getLength ()); for ( int i = 0 ; i < arr . getLength (); ++ i ) dbl [ i ] = arr [ i ] * 2 ; return dbl ; } int main () { Timer t ; DynamicArray < int > arr ( 1000000 ); for ( int i = 0 ; i < arr . getLength (); i ++ ) arr [ i ] = i ; arr = cloneArrayAndDouble ( arr ); std :: cout << t . elapsed (); } \u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\uff0c\u4ee5release\u6a21\u5f0f\u7f16\u8bd1\uff0c\u7a0b\u5e8f\u6267\u884c\u8017\u65f6 0.00825559 \u79d2\u3002 \u518d\u6b21\u8fd0\u884c\u76f8\u540c\u7684\u7a0b\u5e8f\uff0c\u540c\u65f6\u5c06\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u66ff\u66ff\u6362\u4e3a\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 template < typename T > class DynamicArray { private : T * m_array ; int m_length ; public : DynamicArray ( int length ) : m_array ( new T [ length ]), m_length ( length ) { } ~ DynamicArray () { delete [] m_array ; } // Copy constructor DynamicArray ( const DynamicArray & arr ) = delete ; // Copy assignment DynamicArray & operator = ( const DynamicArray & arr ) = delete ; // Move constructor DynamicArray ( DynamicArray && arr ) noexcept : m_array ( arr . m_array ), m_length ( arr . m_length ) { arr . m_length = 0 ; arr . m_array = nullptr ; } // Move assignment DynamicArray & operator = ( DynamicArray && arr ) noexcept { if ( & arr == this ) return * this ; delete [] m_array ; m_length = arr . m_length ; m_array = arr . m_array ; arr . m_length = 0 ; arr . m_array = nullptr ; return * this ; } int getLength () const { return m_length ; } T & operator []( int index ) { return m_array [ index ]; } const T & operator []( int index ) const { return m_array [ index ]; } }; #include #include // for std::chrono functions class Timer { private : // Type aliases to make accessing nested type easier using Clock = std :: chrono :: high_resolution_clock ; using Second = std :: chrono :: duration < double , std :: ratio < 1 > > ; std :: chrono :: time_point < Clock > m_beg { Clock :: now () }; public : void reset () { m_beg = Clock :: now (); } double elapsed () const { return std :: chrono :: duration_cast < Second > ( Clock :: now () - m_beg ). count (); } }; // Return a copy of arr with all of the values doubled DynamicArray < int > cloneArrayAndDouble ( const DynamicArray < int > & arr ) { DynamicArray < int > dbl ( arr . getLength ()); for ( int i = 0 ; i < arr . getLength (); ++ i ) dbl [ i ] = arr [ i ] * 2 ; return dbl ; } int main () { Timer t ; DynamicArray < int > arr ( 1000000 ); for ( int i = 0 ; i < arr . getLength (); i ++ ) arr [ i ] = i ; arr = cloneArrayAndDouble ( arr ); std :: cout << t . elapsed (); } \u540c\u6837\u7684\u673a\u5668\u4e0a\uff0c\u7a0b\u5e8f\u6267\u884c\u8017\u65f6 0.0056 \u79d2\u3002 \u6bd4\u8f83\u4e24\u4e2a\u7248\u672c\u7684\u8fd0\u884c\u65f6\u95f4\uff0c0.0056 / 0.00825559 = 67.8%\u3002\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\u7684\u7248\u672c\u901f\u5ea6\u5feb\u4e50 47.4% \uff01","title":"\u53e6\u4e00\u4e2a\u4f8b\u5b50"},{"location":"M-3-move-constructors-and-move-assignment/#stdswap","text":"\u7531\u4e8e\u79fb\u52a8\u8bed\u4e49\u7684\u76ee\u6807\u662f\u5c06\u8d44\u6e90\u4ece\u6e90\u5bf9\u8c61\u79fb\u52a8\u5230\u76ee\u6807\u5bf9\u8c61\uff0c\u6240\u4ee5\u4f60\u53ef\u80fd\u4f1a\u8003\u8651\u4f7f\u7528 std::swap() \u5b9e\u73b0\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002\u7136\u800c\uff0c\u8fd9\u5e76\u4e0d\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\uff0c\u56e0\u4e3a std::swap() \u4f1a\u5728\u53ef\u79fb\u52a8\u5bf9\u8c61\u4e0a\u8c03\u7528\u79fb\u52a8\u6784\u9020\u51fd\u6570 \u548c \u79fb\u52a8\u8d4b\u503c\uff0c\u8fd9\u5c06\u5bfc\u81f4\u65e0\u9650\u9012\u5f52\u3002\u4f60\u53ef\u4ee5\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u770b\u5230\u8fd9\u79cd\u60c5\u51b5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include #include class Name { private : std :: string m_name ; // std::string is move capable public : Name ( std :: string name ) : m_name { name } { } Name ( Name & name ) = delete ; Name & operator = ( Name & name ) = delete ; Name ( Name && name ) { std :: cout << \"Move ctor \\n \" ; std :: swap ( * this , name ); // bad! } Name & operator = ( Name && name ) { std :: cout << \"Move assign \\n \" ; std :: swap ( * this , name ); // bad! return * this ; } const std :: string & get () { return m_name ; } }; int main () { Name n1 { \"Alex\" }; n1 = Name { \"Joe\" }; // invokes move assignment std :: cout << n1 . get () << '\\n' ; return 0 ; } \u6253\u5370\uff1a 1 2 3 4 5 Move assign Move ctor Move ctor Move ctor Move ctor \u4e00\u76f4\u6253\u5370\u3002\u3002\u3002\u76f4\u5230\u51fd\u6570\u6808\u6ea2\u51fa\u3002 \u4f60\u53ef\u4ee5\u4f7f\u7528\u81ea\u5df1\u7684 swap \u51fd\u6570\u5b9e\u73b0\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\uff0c\u53ea\u8981\u8be5 swap \u51fd\u6570\u4e0d\u8981\u8c03\u7528\u79fb\u52a8\u6784\u9020\u51fd\u6570\u6216\u79fb\u52a8\u8d4b\u503c\u5373\u53ef\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 #include #include class Name { private : std :: string m_name ; public : Name ( std :: string name ) : m_name { name } { } Name ( Name & name ) = delete ; Name & operator = ( Name & name ) = delete ; // Create our own swap friend function to swap the members of Name friend void swap ( Name & a , Name & b ) noexcept { // \u5bf9std::string \u7c7b\u578b\u7684\u6210\u5458\u76f4\u63a5\u4f7f\u7528std:swap // not on Name std :: swap ( a . m_name , b . m_name ); } Name ( Name && name ) { std :: cout << \"Move ctor \\n \" ; swap ( * this , name ); // Now calling our swap, not std::swap } Name & operator = ( Name && name ) { std :: cout << \"Move assign \\n \" ; swap ( * this , name ); // Now calling our swap, not std::swap return * this ; } }; int main () { Name n1 { \"Alex\" }; n1 = Name { \"Joe\" }; // invokes move assignment std :: cout << n1 . get () << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u7b26\u5408\u9884\u671f\uff1a 1 2 Move assign Joe","title":"\u4e0d\u8981\u4f7f\u7528 std::swap \u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49"},{"location":"M-4-std-move/","tags":["move"],"text":"Key Takeaway \u53ea\u6709\u53f3\u503c\u624d\u80fd\u89e6\u53d1 \u79fb\u52a8\u8bed\u4e49 \uff0c\u56e0\u6b64\u5fc5\u987b\u662f\u53f3\u503c\uff08\u5b57\u9762\u91cf\u3001\u533f\u540d\u5bf9\u8c61\uff09\u6216\u8005\u88ab std::move() \u4f5c\u7528\u7684\u5de6\u503c\u624d\u80fd\u591f\u89e6\u53d1 std::move() \u4f1a\u63d0\u793a\u7f16\u8bd1\u5668\uff0c\u7a0b\u5e8f\u5458\u5df2\u7ecf\u4e0d\u518d\u9700\u8981\u8be5\u503c\u4e86\u3002\u6240\u4ee5\uff0c std::move() \u5e94\u8be5\u88ab\u5e94\u7528\u4e8e\u90a3\u4e9b\u5e0c\u671b\u8d44\u6e90\u88ab\u79fb\u51fa\u7684\u5bf9\u8c61\uff0c\u4e14\u6b64\u540e\u4e0d\u5e94\u5f53\u5047\u5b9a\u8be5\u5bf9\u8c61\u8fd8\u6301\u6709\u8be5\u8d44\u6e90\u3002\u7ed9\u8d44\u6e90\u88ab\u79fb\u51fa\u7684\u5bf9\u8c61\u8d4b\u65b0\u503c\u662f\u53ef\u4ee5\u7684\u3002 \u968f\u7740\u4f60\u5f00\u59cb\u9891\u7e41\u5730\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\uff0c\u4f60\u4f1a\u53d1\u73b0\u6709\u65f6\u5019\u5728\u4f60\u9700\u8981\u4f7f\u7528\u5b83\u7684\u65f6\u5019\uff0c\u5bf9\u8c61\u662f\u5de6\u503c\u800c\u4e0d\u662f\u53f3\u503c\u5bfc\u81f4\u4f60\u65e0\u6cd5\u4f7f\u7528\u3002\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a swap \u51fd\u6570\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include template < class T > void myswapCopy ( T & a , T & b ) { T tmp { a }; // invokes copy constructor a = b ; // invokes copy assignment b = tmp ; // invokes copy assignment } int main () { std :: string x { \"abc\" }; std :: string y { \"de\" }; std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; myswapCopy ( x , y ); std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; return 0 ; } \u4f20\u5165\u4e24\u4e2aT\u7c7b\u578b\u7684\u5bf9\u8c61\uff08\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u662f std::string \uff09\u7ed9\u51fd\u6570\uff0c\u7136\u540e\u5c06\u5b83\u4eec\u7684\u503c\u4ea4\u6362\uff08\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u521b\u5efa\u4e86\u4e09\u4e2a\u526f\u672c\uff09\u3002\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 3 4 x: abc y: de x: de y: abc \u8fd9\u4e2a\u7248\u672c\u7684 swap \u51fd\u6570\u521b\u5efa\u4e86\u4e09\u4e2a\u526f\u672c\uff0c \u800c\u521b\u5efa\u526f\u672c\u662f\u4f4e\u6548\u7684\u64cd\u4f5c\uff0c\u8fd9\u5176\u4e2d\u6d89\u53ca\u5230\u4e86\u591a\u6b21\u5b57\u7b26\u4e32\u7684\u521b\u5efa\u548c\u9500\u6bc1\uff0c\u663e\u7136\u6548\u7387\u662f\u5f88\u4f4e\u7684\u3002 \u5b9e\u9645\u4e0a\uff0c\u8fd9\u91cc\u5e76\u4e0d\u9700\u8981\u521b\u5efa\u62f7\u8d1d\uff0c\u56e0\u4e3a\u6211\u4eec\u6267\u884c\u5e0c\u671b\u5c06a\u548cb\u7684\u503c\u4ea4\u6362\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u4f7f\u7528\u4e09\u6b21\u79fb\u52a8\u4e5f\u53ef\u5b8c\u6210\u554a\uff01\u6240\u4ee5\u5982\u679c\u80fd\u5c06\u62f7\u8d1d\u8bed\u4e49\u66ff\u6362\u4e3a\u79fb\u52a8\u8bed\u4e49\uff0c\u4ee3\u7801\u65e0\u7591\u4f1a\u66f4\u9ad8\u6548\u3002 \u4f46\u662f\u5e94\u8be5\u600e\u4e48\u505a\u5462\uff1f\u8fd9\u91cc\u7684\u95ee\u9898\u5728\u4e8e\uff0ca\u548cb\u90fd\u662f \u5de6\u503c\u5f15\u7528 \uff0c\u800c\u4e0d\u662f \u53f3\u503c\u5f15\u7528 \uff0c\u6240\u4ee5\u6ca1\u6709\u529e\u6cd5\u8c03\u7528 \u79fb\u52a8\u6784\u9020\u51fd\u6570 \u548c \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 \u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u57fa\u4e8e\u5de6\u503c\u5f15\u7528\u6267\u884c\u7684\u662f\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002\u90a3\u7a76\u7adf\u5e94\u8be5\u5982\u4f55\u5b9e\u73b0\u5462\uff1f std::move \u5728 C++11 \u4e2d \u6807\u51c6\u5e93\u63d0\u4f9b\u4e86 std::move \u51fd\u6570\u7528\u4e8e\u5c06\u5b9e\u53c2\u8f6c\u6362\uff08\u4f7f\u7528 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \uff09\u4e3a\u53f3\u503c\u5f15\u7528\uff0c\u4ee5\u4fbf\u6fc0\u6d3b\u79fb\u52a8\u64cd\u4f5c\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 std::move \u5bf9\u4e00\u4e2a\u5de6\u503c\u7c7b\u578b\u8fdb\u884c\u8f6c\u6362\uff0c\u4f7f\u5176\u80fd\u591f\u88ab\u79fb\u52a8\u3002 std::move \u5b9a\u4e49\u5728utility\u5934\u6587\u4ef6\u4e2d\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u548c\u4e4b\u524d\u7684\u7c7b\u4f3c\uff0c\u4f46\u662f myswapMove() \u51fd\u6570\u4f7f\u7528 std::move \u51fd\u6570\u5c06\u5de6\u503c\u8f6c\u6362\u6210\u4e86\u53f3\u503c\uff0c\u6240\u4ee5\u8c03\u7528\u4e86\u79fb\u52a8\u8bed\u4e49\u76f8\u5173\u64cd\u4f5c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include #include // for std::move template < class T > void myswapMove ( T & a , T & b ) { T tmp { std :: move ( a ) }; // invokes move constructor a = std :: move ( b ); // invokes move assignment b = std :: move ( tmp ); // invokes move assignment } int main () { std :: string x { \"abc\" }; std :: string y { \"de\" }; std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; myswapMove ( x , y ); std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 3 4 x: abc y: de x: de y: abc \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4e00\u6837\uff0c\u4f46\u662f\u6548\u7387\u8981\u9ad8\u5f97\u591a\u3002\u5f53 tmp \u521d\u59cb\u5316\u65f6\uff0c\u5b83\u6ca1\u6709\u521b\u5efax\u526f\u672c\uff0c\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528 std::move \u5c06\u5de6\u503cx\u8f6c\u6362\u6210\u4e86\u4e00\u4e2a\u53f3\u503c\u3002\u56e0\u4e3a\u53c2\u6570\u662f\u53f3\u503c\uff0c\u6240\u4ee5\u8c03\u7528\u4e86\u79fb\u52a8\u6784\u9020\u51fd\u6570\uff0cx\u88ab\u79fb\u52a8\u5230\u4e86 tmp \u3002 \u7ecf\u8fc7\u51e0\u6b21\u8f6c\u6362\uff0c\u53d8\u91cfx\u7684\u503c\u88ab\u79fb\u52a8\u5230\u4e86y\uff0c\u800cy\u7684\u503c\u4e5f\u88ab\u79fb\u52a8\u5230\u4e86x\u3002 \u53e6\u5916\u4e00\u4e2a\u4f8b\u5b50 \u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528 std::move \u6765\u586b\u5145\u5bb9\u5668\u7684\u5143\u7d20\uff0c\u4f8b\u5982\u4f7f\u7528\u5de6\u503c\u586b\u5145 std::vector \u3002 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528\u62f7\u8d1d\u8bed\u4e49\u5411 vector \u6dfb\u52a0\u5143\u7d20\u3002\u7136\u540e\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\u505a\u540c\u6837\u7684\u64cd\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include #include // for std::move #include int main () { std :: vector < std :: string > v ; // \u8fd9\u91cc\u4f7f\u7528 std::string \u662f\u56e0\u4e3a\u5b83\u662f\u53ef\u79fb\u52a8\u7684 (std::string_view \u4e0d\u53ef\u79fb\u52a8) std :: string str { \"Knock\" }; std :: cout << \"Copying str \\n \" ; v . push_back ( str ); // \u8c03\u7528\u5de6\u503c\u7248\u672c\u7684 push_back\uff0c\u5b83\u4f1a\u5c06str\u62f7\u8d1d\u5230\u6570\u7ec4\u5143\u7d20 std :: cout << \"str: \" << str << '\\n' ; std :: cout << \"vector: \" << v [ 0 ] << '\\n' ; std :: cout << \" \\n Moving str \\n \" ; v . push_back ( std :: move ( str )); // \u8c03\u7528\u53f3\u503c\u7248\u672c\u7684 push_back\uff0c\u5b83\u4f1a\u5c06str\u79fb\u52a8\u5230\u6570\u7ec4\u5143\u7d20 std :: cout << \"str: \" << str << '\\n' ; // \u6253\u5370\u4e2d\u5956\u7ed3\u679c std :: cout << \"vector:\" << v [ 0 ] << ' ' << v [ 1 ] << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\u4f1a\u8f93\u51fa\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 5 6 7 Copying str str: Knock vector: Knock Moving str str: vector: Knock Knock \u5728\u7b2c\u4e00\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f20\u5165 push_back() \u7684\u662f\u4e00\u4e2a\u5de6\u503c\uff0c\u6240\u4ee5\u5b83\u4f1a\u4f7f\u7528\u62f7\u8d1d\u8bed\u4e49\u5411 vector \u6dfb\u52a0\u5143\u7d20\u3002\u56e0\u6b64\uff0c\u539f\u5b57\u7b26\u4e32\u8fd8\u88ab\u4fdd\u5b58\u5728 str \u4e2d\u3002 \u5bf9\u4e8e\u7b2c\u4e8c\u79cd\u60c5\u51b5\uff0c\u4f20\u5165 push_back() \u7684\u662f\u4e00\u4e2a\u53f3\u503c\uff08\u51c6\u786e\u6765\u8bb2\u662f\u901a\u8fc7 std::move \u5c06\u5de6\u503c\u8f6c\u6362\u6210\u7684\u53f3\u503c\uff09\uff0c\u6240\u4ee5\u5b83\u4f1a\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\u5411 vector \u6dfb\u52a0\u5143\u7d20\u3002\u8fd9\u4e2a\u64cd\u4f5c\u65e0\u7591\u662f\u66f4\u52a0\u9ad8\u6548\u7684\uff0c\u56e0\u4e3a vector \u7684\u5143\u7d20\u201c\u7a83\u53d6\u4e86\u201d\u5b57\u7b26\u4e32\u7684\u503c\uff0c\u800c\u6ca1\u6709\u521b\u5efa\u5b83\u7684\u62f7\u8d1d\u3002 \u8d44\u6e90\u88ab\u79fb\u52a8\u7684\u5bf9\u8c61\u5904\u4e8e\u4e00\u79cd\u6709\u6548\uff0c\u4f46\u53ef\u80fd\u4e0d\u786e\u5b9a\u7684\u72b6\u6001 \u5f53\u6211\u4eec\u4ece\u4e00\u4e2a \u4e34\u65f6\u5bf9\u8c61 \u4e2d\u628a\u8d44\u6e90\u79fb\u52a8\u51fa\u6765\u65f6\uff0c\u8fd9\u4e2a\u4e34\u65f6\u5bf9\u8c61\u4e2d\u8fd8\u5269\u4e0b\u4ec0\u4e48\u5176\u5b9e\u5e76\u4e0d\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u9a6c\u4e0a\u5c31\u8981\u88ab\u9500\u6bc1\u4e86\u3002\u4f46\u662f\u5bf9\u4e8e\u4e00\u4e2a \u5de6\u503c\u5bf9\u8c61 \u6765\u8bf4\uff0c\u5f53\u6211\u4eec\u5bf9\u5b83\u4f7f\u7528 std::move() \u540e\u4f1a\u600e\u4e48\u6837\u5462\uff1f\u56e0\u4e3a\u8be5\u5de6\u503c\u5728\u5176\u8d44\u6e90\u88ab\u79fb\u52a8\u540e\uff0c\u4ecd\u7136\u53ef\u80fd\u4f1a\u88ab\u8bbf\u95ee\uff08\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f53\u6211\u4eec\u6253\u5370 str \u7684\u503c\u65f6\uff0c\u5185\u5bb9\u5df2\u7ecf\u6ca1\u6709\u4e86\uff09\uff0c\u6240\u4ee5\u6709\u5fc5\u8981\u77e5\u9053\u8be5\u5bf9\u8c61\u8d44\u6e90\u88ab\u79fb\u52a8\u540e\uff0c\u8fd8\u6709\u4ec0\u4e48\u7559\u5728\u539f\u5bf9\u8c61\u4e2d\u3002 \u5173\u4e8e\u8fd9\u4e2a\u95ee\u9898\u7684\u601d\u8003\u6709\u4e24\u4e2a\u6d3e\u7cfb\u3002\u4e00\u6d3e\u4eba\u4efb\u52a1\uff0c\u8d44\u6e90\u5df2\u7ecf\u88ab\u79fb\u52a8\u8d70\u7684\u5bf9\u8c61\uff0c\u5e94\u8be5\u88ab\u91cd\u7f6e\u4e3a\u67d0\u79cd\u9ed8\u8ba4\u72b6\u6001\u62160\u5e62\uff0c\u56e0\u4e3a\u5b83\u4e0d\u518d\u62e5\u6709\u8d44\u6e90\u3002\u4e0a\u9762\u7684\u4f8b\u5b50\u5f88\u597d\u5730\u5c55\u793a\u4e86\u8fd9\u79cd\u60c5\u51b5\u3002 \u53e6\u5916\u4e00\u6d3e\u4eba\u5219\u8ba4\u4e3a\u5e94\u8be5\u201c\u600e\u4e48\u65b9\u4fbf\u600e\u4e48\u6765\u201d\uff0c\u5982\u679c\u6e05\u7406\u8be5\u5bf9\u8c61\u5f88\u9ebb\u70e6\uff0c\u90a3\u5c31\u4e0d\u5fc5\u6e05\u7406\u3002 \u90a3\u4e48\uff0c\u6807\u51c6\u5e93\u662f\u5982\u4f55\u505a\u7684\u5462\uff1f\u5173\u4e8e\u8fd9\u4e2a\u95ee\u9898\uff0cC++\u6807\u51c6\u8ba4\u4e3a\uff1a\u201c\u9664\u975e\u6709\u660e\u786e\u7684\u89c4\u5b9a\uff0c\u5426\u5219C++\u6807\u51c6\u5e93\u4e2d\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u5728\u8d44\u6e90\u88ab\u79fb\u52a8\u540e\u5e94\u8be5\u88ab\u7f6e\u4e8e \u4efb\u610f\u4e00\u79cd\u6709\u6548 \u7684\u72b6\u6001\u201d\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53\u5de6\u503c\u6253\u5370 str \u503c\u65f6\uff0c\u7a0b\u5e8f\u8f93\u51fa\u7684\u7a7a\u5b57\u7b26\u4e32\u3002\u4f46\u662f\uff0c\u8fd9\u5e76\u4e0d\u662f\u4e00\u5b9a\u7684\uff0c\u6253\u5370\u4efb\u4f55\u5176\u4ed6\u5408\u6cd5\u7684\u5b57\u7b26\u4e32\u4e5f\u53ef\u4ee5\uff0c\u5305\u62ec\u7a7a\u5b57\u7b26\u4e32\u3001\u539f\u672c\u7684\u5b57\u7b26\u4e32\u6216\u4efb\u4f55\u5408\u6cd5\u7684\u5b57\u7b26\u4e32\u3002\u56e0\u6b64\uff0c \u6211\u4eec\u5e94\u8be5\u907f\u514d\u7ee7\u7eed\u4f7f\u7528\u5df2\u7ecf\u88ab\u79fb\u52a8\u7684\u5de6\u503c\u5bf9\u8c61\uff0c\u56e0\u4e3a\u5176\u7ed3\u679c\u53d6\u51b3\u4e8e\u7f16\u8bd1\u5668\u7684\u5177\u4f53\u5b9e\u73b0\u3002 \u5728\u6709\u4e9b\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u7ee7\u7eed\u4f7f\u7528\u5df2\u7ecf\u88ab\u79fb\u52a8\u7684\u5bf9\u8c61\uff08\u800c\u4e0d\u5e0c\u671b\u91cd\u65b0\u5206\u914d\u4e00\u4e2a\u65b0\u5bf9\u8c61\uff09\u3002\u4f8b\u5982\uff0c\u5728\u4e0a\u9762 myswapMove() \u7684\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06a\u7684\u8d44\u6e90\u79fb\u51fa\uff0c\u7136\u540e\u5c06\u5176\u4ed6\u8d44\u6e90\u79fb\u52a8\u5230a\u3002\u8fd9\u4e48\u505a\u53ef\u884c\u662f\u56e0\u4e3a\uff0ca\u7684\u503c\u88ab\u79fb\u9664\u5230a\u518d\u6b21\u83b7\u5f97\u4e00\u4e2a\u786e\u5b9a\u7684\u503c\u7684\u8fd9\u6bb5\u65f6\u95f4\u5185\uff0c\u6211\u4eec\u4e0d\u4f1a\u4f7f\u7528a\u7684\u503c\u3002 \u5bf9\u4e8e\u4e00\u4e2a\u88ab\u79fb\u52a8\u4e86\u8d44\u6e90\u7684\u5bf9\u8c61\u6765\u8bf4\uff0c\u8c03\u7528\u4efb\u4f55\u4e0d\u4f9d\u8d56\u5176\u503c\u7684\u51fd\u6570\u662f\u5b89\u5168\u7684 \u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u3001\u91cd\u7f6e\u8be5\u5bf9\u8c61\u7684\u503c\uff08\u4f7f\u7528 = \u6216 clear() \u6216 reset() \u6210\u5458\u51fd\u6570\uff09\u3002\u6211\u4eec\u53ef\u4ee5\u68c0\u67e5\u5b83\u7684\u72b6\u6001(\u4f8b\u5982\u4f7f\u7528 empty() \u5224\u65ad\u8be5\u5bf9\u8c61\u662f\u5426\u6709\u503c)\u3002\u4f46\u662f\uff0c\u6211\u4eec\u5fc5\u987b\u907f\u514d operator[] \u6216 front() \u8fd9\u6837\u7684\u51fd\u6570\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u51fd\u6570\u4f9d\u8d56\u4e8e\u5bf9\u8c61\u4e2d\u5b58\u653e\u7684\u503c\uff0c\u800c\u88ab\u79fb\u52a8\u540e\u7684\u5bf9\u8c61\u53ef\u80fd\u53ef\u4ee5\uff0c\u4e5f\u53ef\u80fd\u4e0d\u80fd\u591f\u63d0\u4f9b\u8fd9\u4e9b\u503c\u3002 \u5173\u952e\u4fe1\u606f std::move() \u4f1a\u63d0\u793a\u7f16\u8bd1\u5668\uff0c\u7a0b\u5e8f\u5458\u5df2\u7ecf\u4e0d\u518d\u9700\u8981\u8be5\u503c\u4e86\u3002\u6240\u4ee5\uff0c std::move() \u5e94\u8be5\u88ab\u5e94\u7528\u4e8e\u90a3\u4e9b\u5e0c\u671b\u8d44\u6e90\u88ab\u79fb\u51fa\u7684\u5bf9\u8c61\uff0c\u4e14\u6b64\u540e\u4e0d\u5e94\u5f53\u5047\u5b9a\u8be5\u5bf9\u8c61\u8fd8\u6301\u6709\u8be5\u8d44\u6e90\u3002\u7ed9\u8d44\u6e90\u88ab\u79fb\u51fa\u7684\u5bf9\u8c61\u8d4b\u65b0\u503c\u662f\u53ef\u4ee5\u7684\u3002 std::move \u8fd8\u6709\u4ec0\u4e48\u7528\u9014\uff1f std::move \u5728\u8fdb\u884c\u6570\u7ec4\u6392\u5e8f\u65f6\u4e5f\u5f88\u6709\u7528\u3002\u5f88\u591a\u6392\u5e8f\u7b97\u6cd5 (\u4f8b\u5982\u9009\u62e9\u6392\u5e8f\u548c\u5192\u6ce1\u6392\u5e8f)\u662f\u57fa\u4e8e\u4ea4\u6362\u4e24\u4e2a\u5143\u7d20\u800c\u5b9e\u73b0\u7684\u3002\u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u57fa\u4e8e\u62f7\u8d1d\u8bed\u4e49\u7684\u4ea4\u6362\uff0c\u73b0\u5728\u53ef\u4ee5\u5c06\u5b83\u66ff\u6362\u6210\u57fa\u4e8e\u79fb\u52a8\u8bed\u4e49\u7684\u4ea4\u6362\u4e86\uff0c\u8fd9\u4e48\u505a\u6548\u7387\u4f1a\u9ad8\u5f88\u591a\u3002 \u6b64\u5916\uff0c\u5f53\u6211\u4eec\u9700\u8981\u5c06\u67d0\u4e2a\u667a\u80fd\u6307\u9488\u7ba1\u7406\u7684\u5185\u5bb9\u8f6c\u79fb\u5230\u53e6\u4e00\u4e2a\u667a\u80fd\u6307\u9488\u65f6\uff0c\u8be5\u51fd\u6570\u4e5f\u5f88\u6709\u7528\u3002 \u5c0f\u7ed3 \u5f53\u4e00\u4e2a\u5de6\u503c\u9700\u8981\u88ab\u5f53\u505a\u53f3\u503c\u4f7f\u7528\uff0c\u4ee5\u4fbf\u89e6\u53d1\u79fb\u52a8\u8bed\u4e49\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 std::move \u3002","title":"M.4 - std::move"},{"location":"M-4-std-move/#stdmove","text":"\u5728 C++11 \u4e2d \u6807\u51c6\u5e93\u63d0\u4f9b\u4e86 std::move \u51fd\u6570\u7528\u4e8e\u5c06\u5b9e\u53c2\u8f6c\u6362\uff08\u4f7f\u7528 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \uff09\u4e3a\u53f3\u503c\u5f15\u7528\uff0c\u4ee5\u4fbf\u6fc0\u6d3b\u79fb\u52a8\u64cd\u4f5c\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 std::move \u5bf9\u4e00\u4e2a\u5de6\u503c\u7c7b\u578b\u8fdb\u884c\u8f6c\u6362\uff0c\u4f7f\u5176\u80fd\u591f\u88ab\u79fb\u52a8\u3002 std::move \u5b9a\u4e49\u5728utility\u5934\u6587\u4ef6\u4e2d\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u548c\u4e4b\u524d\u7684\u7c7b\u4f3c\uff0c\u4f46\u662f myswapMove() \u51fd\u6570\u4f7f\u7528 std::move \u51fd\u6570\u5c06\u5de6\u503c\u8f6c\u6362\u6210\u4e86\u53f3\u503c\uff0c\u6240\u4ee5\u8c03\u7528\u4e86\u79fb\u52a8\u8bed\u4e49\u76f8\u5173\u64cd\u4f5c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include #include // for std::move template < class T > void myswapMove ( T & a , T & b ) { T tmp { std :: move ( a ) }; // invokes move constructor a = std :: move ( b ); // invokes move assignment b = std :: move ( tmp ); // invokes move assignment } int main () { std :: string x { \"abc\" }; std :: string y { \"de\" }; std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; myswapMove ( x , y ); std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 3 4 x: abc y: de x: de y: abc \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4e00\u6837\uff0c\u4f46\u662f\u6548\u7387\u8981\u9ad8\u5f97\u591a\u3002\u5f53 tmp \u521d\u59cb\u5316\u65f6\uff0c\u5b83\u6ca1\u6709\u521b\u5efax\u526f\u672c\uff0c\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528 std::move \u5c06\u5de6\u503cx\u8f6c\u6362\u6210\u4e86\u4e00\u4e2a\u53f3\u503c\u3002\u56e0\u4e3a\u53c2\u6570\u662f\u53f3\u503c\uff0c\u6240\u4ee5\u8c03\u7528\u4e86\u79fb\u52a8\u6784\u9020\u51fd\u6570\uff0cx\u88ab\u79fb\u52a8\u5230\u4e86 tmp \u3002 \u7ecf\u8fc7\u51e0\u6b21\u8f6c\u6362\uff0c\u53d8\u91cfx\u7684\u503c\u88ab\u79fb\u52a8\u5230\u4e86y\uff0c\u800cy\u7684\u503c\u4e5f\u88ab\u79fb\u52a8\u5230\u4e86x\u3002","title":"std::move"},{"location":"M-4-std-move/#_1","text":"\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528 std::move \u6765\u586b\u5145\u5bb9\u5668\u7684\u5143\u7d20\uff0c\u4f8b\u5982\u4f7f\u7528\u5de6\u503c\u586b\u5145 std::vector \u3002 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528\u62f7\u8d1d\u8bed\u4e49\u5411 vector \u6dfb\u52a0\u5143\u7d20\u3002\u7136\u540e\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\u505a\u540c\u6837\u7684\u64cd\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include #include // for std::move #include int main () { std :: vector < std :: string > v ; // \u8fd9\u91cc\u4f7f\u7528 std::string \u662f\u56e0\u4e3a\u5b83\u662f\u53ef\u79fb\u52a8\u7684 (std::string_view \u4e0d\u53ef\u79fb\u52a8) std :: string str { \"Knock\" }; std :: cout << \"Copying str \\n \" ; v . push_back ( str ); // \u8c03\u7528\u5de6\u503c\u7248\u672c\u7684 push_back\uff0c\u5b83\u4f1a\u5c06str\u62f7\u8d1d\u5230\u6570\u7ec4\u5143\u7d20 std :: cout << \"str: \" << str << '\\n' ; std :: cout << \"vector: \" << v [ 0 ] << '\\n' ; std :: cout << \" \\n Moving str \\n \" ; v . push_back ( std :: move ( str )); // \u8c03\u7528\u53f3\u503c\u7248\u672c\u7684 push_back\uff0c\u5b83\u4f1a\u5c06str\u79fb\u52a8\u5230\u6570\u7ec4\u5143\u7d20 std :: cout << \"str: \" << str << '\\n' ; // \u6253\u5370\u4e2d\u5956\u7ed3\u679c std :: cout << \"vector:\" << v [ 0 ] << ' ' << v [ 1 ] << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\u4f1a\u8f93\u51fa\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 5 6 7 Copying str str: Knock vector: Knock Moving str str: vector: Knock Knock \u5728\u7b2c\u4e00\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f20\u5165 push_back() \u7684\u662f\u4e00\u4e2a\u5de6\u503c\uff0c\u6240\u4ee5\u5b83\u4f1a\u4f7f\u7528\u62f7\u8d1d\u8bed\u4e49\u5411 vector \u6dfb\u52a0\u5143\u7d20\u3002\u56e0\u6b64\uff0c\u539f\u5b57\u7b26\u4e32\u8fd8\u88ab\u4fdd\u5b58\u5728 str \u4e2d\u3002 \u5bf9\u4e8e\u7b2c\u4e8c\u79cd\u60c5\u51b5\uff0c\u4f20\u5165 push_back() \u7684\u662f\u4e00\u4e2a\u53f3\u503c\uff08\u51c6\u786e\u6765\u8bb2\u662f\u901a\u8fc7 std::move \u5c06\u5de6\u503c\u8f6c\u6362\u6210\u7684\u53f3\u503c\uff09\uff0c\u6240\u4ee5\u5b83\u4f1a\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\u5411 vector \u6dfb\u52a0\u5143\u7d20\u3002\u8fd9\u4e2a\u64cd\u4f5c\u65e0\u7591\u662f\u66f4\u52a0\u9ad8\u6548\u7684\uff0c\u56e0\u4e3a vector \u7684\u5143\u7d20\u201c\u7a83\u53d6\u4e86\u201d\u5b57\u7b26\u4e32\u7684\u503c\uff0c\u800c\u6ca1\u6709\u521b\u5efa\u5b83\u7684\u62f7\u8d1d\u3002","title":"\u53e6\u5916\u4e00\u4e2a\u4f8b\u5b50"},{"location":"M-4-std-move/#_2","text":"\u5f53\u6211\u4eec\u4ece\u4e00\u4e2a \u4e34\u65f6\u5bf9\u8c61 \u4e2d\u628a\u8d44\u6e90\u79fb\u52a8\u51fa\u6765\u65f6\uff0c\u8fd9\u4e2a\u4e34\u65f6\u5bf9\u8c61\u4e2d\u8fd8\u5269\u4e0b\u4ec0\u4e48\u5176\u5b9e\u5e76\u4e0d\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u9a6c\u4e0a\u5c31\u8981\u88ab\u9500\u6bc1\u4e86\u3002\u4f46\u662f\u5bf9\u4e8e\u4e00\u4e2a \u5de6\u503c\u5bf9\u8c61 \u6765\u8bf4\uff0c\u5f53\u6211\u4eec\u5bf9\u5b83\u4f7f\u7528 std::move() \u540e\u4f1a\u600e\u4e48\u6837\u5462\uff1f\u56e0\u4e3a\u8be5\u5de6\u503c\u5728\u5176\u8d44\u6e90\u88ab\u79fb\u52a8\u540e\uff0c\u4ecd\u7136\u53ef\u80fd\u4f1a\u88ab\u8bbf\u95ee\uff08\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f53\u6211\u4eec\u6253\u5370 str \u7684\u503c\u65f6\uff0c\u5185\u5bb9\u5df2\u7ecf\u6ca1\u6709\u4e86\uff09\uff0c\u6240\u4ee5\u6709\u5fc5\u8981\u77e5\u9053\u8be5\u5bf9\u8c61\u8d44\u6e90\u88ab\u79fb\u52a8\u540e\uff0c\u8fd8\u6709\u4ec0\u4e48\u7559\u5728\u539f\u5bf9\u8c61\u4e2d\u3002 \u5173\u4e8e\u8fd9\u4e2a\u95ee\u9898\u7684\u601d\u8003\u6709\u4e24\u4e2a\u6d3e\u7cfb\u3002\u4e00\u6d3e\u4eba\u4efb\u52a1\uff0c\u8d44\u6e90\u5df2\u7ecf\u88ab\u79fb\u52a8\u8d70\u7684\u5bf9\u8c61\uff0c\u5e94\u8be5\u88ab\u91cd\u7f6e\u4e3a\u67d0\u79cd\u9ed8\u8ba4\u72b6\u6001\u62160\u5e62\uff0c\u56e0\u4e3a\u5b83\u4e0d\u518d\u62e5\u6709\u8d44\u6e90\u3002\u4e0a\u9762\u7684\u4f8b\u5b50\u5f88\u597d\u5730\u5c55\u793a\u4e86\u8fd9\u79cd\u60c5\u51b5\u3002 \u53e6\u5916\u4e00\u6d3e\u4eba\u5219\u8ba4\u4e3a\u5e94\u8be5\u201c\u600e\u4e48\u65b9\u4fbf\u600e\u4e48\u6765\u201d\uff0c\u5982\u679c\u6e05\u7406\u8be5\u5bf9\u8c61\u5f88\u9ebb\u70e6\uff0c\u90a3\u5c31\u4e0d\u5fc5\u6e05\u7406\u3002 \u90a3\u4e48\uff0c\u6807\u51c6\u5e93\u662f\u5982\u4f55\u505a\u7684\u5462\uff1f\u5173\u4e8e\u8fd9\u4e2a\u95ee\u9898\uff0cC++\u6807\u51c6\u8ba4\u4e3a\uff1a\u201c\u9664\u975e\u6709\u660e\u786e\u7684\u89c4\u5b9a\uff0c\u5426\u5219C++\u6807\u51c6\u5e93\u4e2d\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u5728\u8d44\u6e90\u88ab\u79fb\u52a8\u540e\u5e94\u8be5\u88ab\u7f6e\u4e8e \u4efb\u610f\u4e00\u79cd\u6709\u6548 \u7684\u72b6\u6001\u201d\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53\u5de6\u503c\u6253\u5370 str \u503c\u65f6\uff0c\u7a0b\u5e8f\u8f93\u51fa\u7684\u7a7a\u5b57\u7b26\u4e32\u3002\u4f46\u662f\uff0c\u8fd9\u5e76\u4e0d\u662f\u4e00\u5b9a\u7684\uff0c\u6253\u5370\u4efb\u4f55\u5176\u4ed6\u5408\u6cd5\u7684\u5b57\u7b26\u4e32\u4e5f\u53ef\u4ee5\uff0c\u5305\u62ec\u7a7a\u5b57\u7b26\u4e32\u3001\u539f\u672c\u7684\u5b57\u7b26\u4e32\u6216\u4efb\u4f55\u5408\u6cd5\u7684\u5b57\u7b26\u4e32\u3002\u56e0\u6b64\uff0c \u6211\u4eec\u5e94\u8be5\u907f\u514d\u7ee7\u7eed\u4f7f\u7528\u5df2\u7ecf\u88ab\u79fb\u52a8\u7684\u5de6\u503c\u5bf9\u8c61\uff0c\u56e0\u4e3a\u5176\u7ed3\u679c\u53d6\u51b3\u4e8e\u7f16\u8bd1\u5668\u7684\u5177\u4f53\u5b9e\u73b0\u3002 \u5728\u6709\u4e9b\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u7ee7\u7eed\u4f7f\u7528\u5df2\u7ecf\u88ab\u79fb\u52a8\u7684\u5bf9\u8c61\uff08\u800c\u4e0d\u5e0c\u671b\u91cd\u65b0\u5206\u914d\u4e00\u4e2a\u65b0\u5bf9\u8c61\uff09\u3002\u4f8b\u5982\uff0c\u5728\u4e0a\u9762 myswapMove() \u7684\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06a\u7684\u8d44\u6e90\u79fb\u51fa\uff0c\u7136\u540e\u5c06\u5176\u4ed6\u8d44\u6e90\u79fb\u52a8\u5230a\u3002\u8fd9\u4e48\u505a\u53ef\u884c\u662f\u56e0\u4e3a\uff0ca\u7684\u503c\u88ab\u79fb\u9664\u5230a\u518d\u6b21\u83b7\u5f97\u4e00\u4e2a\u786e\u5b9a\u7684\u503c\u7684\u8fd9\u6bb5\u65f6\u95f4\u5185\uff0c\u6211\u4eec\u4e0d\u4f1a\u4f7f\u7528a\u7684\u503c\u3002 \u5bf9\u4e8e\u4e00\u4e2a\u88ab\u79fb\u52a8\u4e86\u8d44\u6e90\u7684\u5bf9\u8c61\u6765\u8bf4\uff0c\u8c03\u7528\u4efb\u4f55\u4e0d\u4f9d\u8d56\u5176\u503c\u7684\u51fd\u6570\u662f\u5b89\u5168\u7684 \u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u3001\u91cd\u7f6e\u8be5\u5bf9\u8c61\u7684\u503c\uff08\u4f7f\u7528 = \u6216 clear() \u6216 reset() \u6210\u5458\u51fd\u6570\uff09\u3002\u6211\u4eec\u53ef\u4ee5\u68c0\u67e5\u5b83\u7684\u72b6\u6001(\u4f8b\u5982\u4f7f\u7528 empty() \u5224\u65ad\u8be5\u5bf9\u8c61\u662f\u5426\u6709\u503c)\u3002\u4f46\u662f\uff0c\u6211\u4eec\u5fc5\u987b\u907f\u514d operator[] \u6216 front() \u8fd9\u6837\u7684\u51fd\u6570\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u51fd\u6570\u4f9d\u8d56\u4e8e\u5bf9\u8c61\u4e2d\u5b58\u653e\u7684\u503c\uff0c\u800c\u88ab\u79fb\u52a8\u540e\u7684\u5bf9\u8c61\u53ef\u80fd\u53ef\u4ee5\uff0c\u4e5f\u53ef\u80fd\u4e0d\u80fd\u591f\u63d0\u4f9b\u8fd9\u4e9b\u503c\u3002 \u5173\u952e\u4fe1\u606f std::move() \u4f1a\u63d0\u793a\u7f16\u8bd1\u5668\uff0c\u7a0b\u5e8f\u5458\u5df2\u7ecf\u4e0d\u518d\u9700\u8981\u8be5\u503c\u4e86\u3002\u6240\u4ee5\uff0c std::move() \u5e94\u8be5\u88ab\u5e94\u7528\u4e8e\u90a3\u4e9b\u5e0c\u671b\u8d44\u6e90\u88ab\u79fb\u51fa\u7684\u5bf9\u8c61\uff0c\u4e14\u6b64\u540e\u4e0d\u5e94\u5f53\u5047\u5b9a\u8be5\u5bf9\u8c61\u8fd8\u6301\u6709\u8be5\u8d44\u6e90\u3002\u7ed9\u8d44\u6e90\u88ab\u79fb\u51fa\u7684\u5bf9\u8c61\u8d4b\u65b0\u503c\u662f\u53ef\u4ee5\u7684\u3002","title":"\u8d44\u6e90\u88ab\u79fb\u52a8\u7684\u5bf9\u8c61\u5904\u4e8e\u4e00\u79cd\u6709\u6548\uff0c\u4f46\u53ef\u80fd\u4e0d\u786e\u5b9a\u7684\u72b6\u6001"},{"location":"M-4-std-move/#stdmove_1","text":"std::move \u5728\u8fdb\u884c\u6570\u7ec4\u6392\u5e8f\u65f6\u4e5f\u5f88\u6709\u7528\u3002\u5f88\u591a\u6392\u5e8f\u7b97\u6cd5 (\u4f8b\u5982\u9009\u62e9\u6392\u5e8f\u548c\u5192\u6ce1\u6392\u5e8f)\u662f\u57fa\u4e8e\u4ea4\u6362\u4e24\u4e2a\u5143\u7d20\u800c\u5b9e\u73b0\u7684\u3002\u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u57fa\u4e8e\u62f7\u8d1d\u8bed\u4e49\u7684\u4ea4\u6362\uff0c\u73b0\u5728\u53ef\u4ee5\u5c06\u5b83\u66ff\u6362\u6210\u57fa\u4e8e\u79fb\u52a8\u8bed\u4e49\u7684\u4ea4\u6362\u4e86\uff0c\u8fd9\u4e48\u505a\u6548\u7387\u4f1a\u9ad8\u5f88\u591a\u3002 \u6b64\u5916\uff0c\u5f53\u6211\u4eec\u9700\u8981\u5c06\u67d0\u4e2a\u667a\u80fd\u6307\u9488\u7ba1\u7406\u7684\u5185\u5bb9\u8f6c\u79fb\u5230\u53e6\u4e00\u4e2a\u667a\u80fd\u6307\u9488\u65f6\uff0c\u8be5\u51fd\u6570\u4e5f\u5f88\u6709\u7528\u3002","title":"std::move \u8fd8\u6709\u4ec0\u4e48\u7528\u9014\uff1f"},{"location":"M-4-std-move/#_3","text":"\u5f53\u4e00\u4e2a\u5de6\u503c\u9700\u8981\u88ab\u5f53\u505a\u53f3\u503c\u4f7f\u7528\uff0c\u4ee5\u4fbf\u89e6\u53d1\u79fb\u52a8\u8bed\u4e49\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 std::move \u3002","title":"\u5c0f\u7ed3"},{"location":"M-5-std-move-if-noexcept/","tags":["move"],"text":"Key Takeaway (\u611f\u8c22\u8bfb\u8005 Koe \u8d21\u732e\u4e86\u672c\u8282\u8bfe\u7684\u8349\u7a3f\uff01) \u5728 20.9 -- Exception specifications and noexcept \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86 noexcept exception specifier and operator, which this lesson builds on. We also covered the strong exception guarantee , which guarantees that if a function is interrupted by an exception, no memory will be leaked and the program state will not be changed. In particular, all constructors should uphold the strong exception guarantee, so that the rest of the program won\u2019t be left in an altered state if construction of an object fails. The move constructors exception problem Consider the case where we are copying some object, and the copy fails for some reason (e.g. the machine is out of memory). In such a case, the object being copied is not harmed in any way, because the source object doesn\u2019t need to be modified to create a copy. We can discard the failed copy, and move on. The strong exception guarantee is upheld. Now consider the case where we are instead moving an object. A move operation transfers ownership of a given resource from the source to the destination object. If the move operation is interrupted by an exception after the transfer of ownership occurs, then our source object will be left in a modified state. This isn\u2019t a problem if the source object is a temporary object and going to be discarded after the move anyway -- but for non-temporary objects, we\u2019ve now damaged the source object. To comply with the strong exception guarantee , we\u2019d need to move the resource back to the source object, but if the move failed the first time, there\u2019s no guarantee the move back will succeed either. How can we give move constructors the strong exception guarantee ? It is simple enough to avoid throwing exceptions in the body of a move constructor, but a move constructor may invoke other constructors that are potentially throwing . Take for example the move constructor for std::pair , which must try to move each subobject in the source pair into the new pair object. 1 2 3 4 5 6 7 // Example move constructor definition for std::pair // Take in an 'old' pair, and then move construct the new pair's 'first' and 'second' subobjects from the 'old' ones template < typename T1 , typename T2 > pair < T1 , T2 >:: pair ( pair && old ) : first ( std :: move ( old . first )), second ( std :: move ( old . second )) {} COPY Now lets use two classes, MoveClass and CopyClass , which we will pair together to demonstrate the strong exception guarantee problem with move constructors: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 #include #include // For std::pair, std::make_pair, std::move, std::move_if_noexcept #include // std::runtime_error class MoveClass { private : int * m_resource {}; public : MoveClass () = default ; MoveClass ( int resource ) : m_resource { new int { resource } } {} // Copy constructor MoveClass ( const MoveClass & that ) { // deep copy if ( that . m_resource != nullptr ) { m_resource = new int { * that . m_resource }; } } // Move constructor MoveClass ( MoveClass && that ) noexcept : m_resource { that . m_resource } { that . m_resource = nullptr ; } ~ MoveClass () { std :: cout << \"destroying \" << * this << '\\n' ; delete m_resource ; } friend std :: ostream & operator << ( std :: ostream & out , const MoveClass & moveClass ) { out << \"MoveClass(\" ; if ( moveClass . m_resource == nullptr ) { out << \"empty\" ; } else { out << * moveClass . m_resource ; } out << ')' ; return out ; } }; class CopyClass { public : bool m_throw {}; CopyClass () = default ; // Copy constructor throws an exception when copying from a CopyClass object where its m_throw is 'true' CopyClass ( const CopyClass & that ) : m_throw { that . m_throw } { if ( m_throw ) { throw std :: runtime_error { \"abort!\" }; } } }; int main () { // We can make a std::pair without any problems: std :: pair my_pair { MoveClass { 13 }, CopyClass {} }; std :: cout << \"my_pair.first: \" << my_pair . first << '\\n' ; // But the problem arises when we try to move that pair into another pair. try { my_pair . second . m_throw = true ; // To trigger copy constructor exception // The following line will throw an exception std :: pair moved_pair { std :: move ( my_pair ) }; // We'll comment out this line later // std::pair moved_pair{ std::move_if_noexcept(my_pair) }; // We'll uncomment this line later std :: cout << \"moved pair exists \\n \" ; // Never prints } catch ( const std :: exception & ex ) { std :: cerr << \"Error found: \" << ex . what () << '\\n' ; } std :: cout << \"my_pair.first: \" << my_pair . first << '\\n' ; return 0 ; } COPY The above program prints: 1 2 3 4 5 6 destroying MoveClass(empty) my_pair.first: MoveClass(13) destroying MoveClass(13) Error found: abort! my_pair.first: MoveClass(empty) destroying MoveClass(empty) Let\u2019s explore what happened. The first printed line shows the temporary MoveClass object used to initialize my_pair gets destroyed as soon as the my_pair instantiation statement has been executed. It is empty since the MoveClass subobject in my_pair was move constructed from it, demonstrated by the next line which shows my_pair.first contains the MoveClass object with value 13 . It gets interesting in the third line. We created moved_pair by copy constructing its CopyClass subobject (it doesn\u2019t have a move constructor), but that copy construction threw an exception since we changed the Boolean flag. Construction of moved_pair was aborted by the exception, and its already-constructed members were destroyed. In this case, the MoveClass member was destroyed, printing destroying MoveClass(13) variable . Next we see the Error found: abort! message printed by main() . When we try to print my_pair.first again, it shows the MoveClass member is empty. Since moved_pair was initialized with std::move , the MoveClass member (which has a move constructor) got move constructed and my_pair.first was nulled. Finally, my_pair was destroyed at the end of main(). To summarize the above results: the move constructor of std::pair used the throwing copy constructor of CopyClass . This copy constructor threw an exception, causing the creation of moved_pair to abort, and my_pair.first to be permanently damaged. The strong exception guarantee was not preserved. std::move_if_noexcept to the rescue Note that the above problem could have been avoided if std::pair had tried to do a copy instead of a move. In that case, moved_pair would have failed to construct, but my_pair would not have been altered. But copying instead of moving has a performance cost that we don\u2019t want to pay for all objects -- ideally we want to do a move if we can do so safely, and a copy otherwise. Fortunately, C++ has a two mechanisms that, when used in combination, let us do exactly that. First, because noexcept functions are no-throw/no-fail, they implicitly meet the criteria for the strong exception guarantee . Thus, a noexcept move constructor is guaranteed to succeed. Second, we can use the standard library function std::move_if_noexcept() to determine whether a move or a copy should be performed. std::move_if_noexcept is a counterpart to std::move , and is used in the same way. If the compiler can tell that an object passed as an argument to std::move_if_noexcept won\u2019t throw an exception when it is move constructed (or if the object is move-only and has no copy constructor), then std::move_if_noexcept will perform identically to std::move() (and return the object converted to an r-value). Otherwise, std::move_if_noexcept will return a normal l-value reference to the object. \u5173\u952e\u4fe1\u606f std::move_if_noexcept will return a movable r-value if the object has a noexcept move constructor, otherwise it will return a copyable l-value. We can use the noexcept specifier in conjunction with std::move_if_noexcept to use move semantics only when a strong exception guarantee exists (and use copy semantics otherwise). Let\u2019s update the code in the previous example as follows: 1 2 //std::pair moved_pair{std::move(my_pair)}; // comment out this line now std :: pair moved_pair { std :: move_if_noexcept ( my_pair )}; // and uncomment this line COPY Running the program again prints: 1 2 3 4 5 6 destroying MoveClass(empty) my_pair.first: MoveClass(13) destroying MoveClass(13) Error found: abort! my_pair.first: MoveClass(13) destroying MoveClass(13) As you can see, after the exception was thrown, the subobject my_pair.first still points to the value 13 . The move constructor of std::pair isn\u2019t noexcept (as of C++20), so std::move_if_noexcept returns my_pair as an l-value reference. This causes moved_pair to be created via the copy constructor (rather than the move constructor). The copy constructor can throw safely, because it doesn\u2019t modify the source object. The standard library uses std::move_if_noexcept often to optimize for functions that are noexcept . For example, std::vector::resize will use move semantics if the element type has a noexcept move constructor, and copy semantics otherwise. This means std::vector will generally operate faster with objects that have a noexcept move constructor. \u6ce8\u610f If a type has both potentially throwing move semantics and deleted copy semantics (the copy constructor and copy assignment operator are unavailable), then std::move_if_noexcept will waive the strong guarantee and invoke move semantics. This conditional waiving of the strong guarantee is ubiquitous in the standard library container classes, since they use std::move_if_noexcept often.","title":"M.5 - std::move_if_noexcept"},{"location":"M-5-std-move-if-noexcept/#the-move-constructors-exception-problem","text":"Consider the case where we are copying some object, and the copy fails for some reason (e.g. the machine is out of memory). In such a case, the object being copied is not harmed in any way, because the source object doesn\u2019t need to be modified to create a copy. We can discard the failed copy, and move on. The strong exception guarantee is upheld. Now consider the case where we are instead moving an object. A move operation transfers ownership of a given resource from the source to the destination object. If the move operation is interrupted by an exception after the transfer of ownership occurs, then our source object will be left in a modified state. This isn\u2019t a problem if the source object is a temporary object and going to be discarded after the move anyway -- but for non-temporary objects, we\u2019ve now damaged the source object. To comply with the strong exception guarantee , we\u2019d need to move the resource back to the source object, but if the move failed the first time, there\u2019s no guarantee the move back will succeed either. How can we give move constructors the strong exception guarantee ? It is simple enough to avoid throwing exceptions in the body of a move constructor, but a move constructor may invoke other constructors that are potentially throwing . Take for example the move constructor for std::pair , which must try to move each subobject in the source pair into the new pair object. 1 2 3 4 5 6 7 // Example move constructor definition for std::pair // Take in an 'old' pair, and then move construct the new pair's 'first' and 'second' subobjects from the 'old' ones template < typename T1 , typename T2 > pair < T1 , T2 >:: pair ( pair && old ) : first ( std :: move ( old . first )), second ( std :: move ( old . second )) {} COPY Now lets use two classes, MoveClass and CopyClass , which we will pair together to demonstrate the strong exception guarantee problem with move constructors: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 #include #include // For std::pair, std::make_pair, std::move, std::move_if_noexcept #include // std::runtime_error class MoveClass { private : int * m_resource {}; public : MoveClass () = default ; MoveClass ( int resource ) : m_resource { new int { resource } } {} // Copy constructor MoveClass ( const MoveClass & that ) { // deep copy if ( that . m_resource != nullptr ) { m_resource = new int { * that . m_resource }; } } // Move constructor MoveClass ( MoveClass && that ) noexcept : m_resource { that . m_resource } { that . m_resource = nullptr ; } ~ MoveClass () { std :: cout << \"destroying \" << * this << '\\n' ; delete m_resource ; } friend std :: ostream & operator << ( std :: ostream & out , const MoveClass & moveClass ) { out << \"MoveClass(\" ; if ( moveClass . m_resource == nullptr ) { out << \"empty\" ; } else { out << * moveClass . m_resource ; } out << ')' ; return out ; } }; class CopyClass { public : bool m_throw {}; CopyClass () = default ; // Copy constructor throws an exception when copying from a CopyClass object where its m_throw is 'true' CopyClass ( const CopyClass & that ) : m_throw { that . m_throw } { if ( m_throw ) { throw std :: runtime_error { \"abort!\" }; } } }; int main () { // We can make a std::pair without any problems: std :: pair my_pair { MoveClass { 13 }, CopyClass {} }; std :: cout << \"my_pair.first: \" << my_pair . first << '\\n' ; // But the problem arises when we try to move that pair into another pair. try { my_pair . second . m_throw = true ; // To trigger copy constructor exception // The following line will throw an exception std :: pair moved_pair { std :: move ( my_pair ) }; // We'll comment out this line later // std::pair moved_pair{ std::move_if_noexcept(my_pair) }; // We'll uncomment this line later std :: cout << \"moved pair exists \\n \" ; // Never prints } catch ( const std :: exception & ex ) { std :: cerr << \"Error found: \" << ex . what () << '\\n' ; } std :: cout << \"my_pair.first: \" << my_pair . first << '\\n' ; return 0 ; } COPY The above program prints: 1 2 3 4 5 6 destroying MoveClass(empty) my_pair.first: MoveClass(13) destroying MoveClass(13) Error found: abort! my_pair.first: MoveClass(empty) destroying MoveClass(empty) Let\u2019s explore what happened. The first printed line shows the temporary MoveClass object used to initialize my_pair gets destroyed as soon as the my_pair instantiation statement has been executed. It is empty since the MoveClass subobject in my_pair was move constructed from it, demonstrated by the next line which shows my_pair.first contains the MoveClass object with value 13 . It gets interesting in the third line. We created moved_pair by copy constructing its CopyClass subobject (it doesn\u2019t have a move constructor), but that copy construction threw an exception since we changed the Boolean flag. Construction of moved_pair was aborted by the exception, and its already-constructed members were destroyed. In this case, the MoveClass member was destroyed, printing destroying MoveClass(13) variable . Next we see the Error found: abort! message printed by main() . When we try to print my_pair.first again, it shows the MoveClass member is empty. Since moved_pair was initialized with std::move , the MoveClass member (which has a move constructor) got move constructed and my_pair.first was nulled. Finally, my_pair was destroyed at the end of main(). To summarize the above results: the move constructor of std::pair used the throwing copy constructor of CopyClass . This copy constructor threw an exception, causing the creation of moved_pair to abort, and my_pair.first to be permanently damaged. The strong exception guarantee was not preserved.","title":"The move constructors exception problem"},{"location":"M-5-std-move-if-noexcept/#stdmove_if_noexcept-to-the-rescue","text":"Note that the above problem could have been avoided if std::pair had tried to do a copy instead of a move. In that case, moved_pair would have failed to construct, but my_pair would not have been altered. But copying instead of moving has a performance cost that we don\u2019t want to pay for all objects -- ideally we want to do a move if we can do so safely, and a copy otherwise. Fortunately, C++ has a two mechanisms that, when used in combination, let us do exactly that. First, because noexcept functions are no-throw/no-fail, they implicitly meet the criteria for the strong exception guarantee . Thus, a noexcept move constructor is guaranteed to succeed. Second, we can use the standard library function std::move_if_noexcept() to determine whether a move or a copy should be performed. std::move_if_noexcept is a counterpart to std::move , and is used in the same way. If the compiler can tell that an object passed as an argument to std::move_if_noexcept won\u2019t throw an exception when it is move constructed (or if the object is move-only and has no copy constructor), then std::move_if_noexcept will perform identically to std::move() (and return the object converted to an r-value). Otherwise, std::move_if_noexcept will return a normal l-value reference to the object. \u5173\u952e\u4fe1\u606f std::move_if_noexcept will return a movable r-value if the object has a noexcept move constructor, otherwise it will return a copyable l-value. We can use the noexcept specifier in conjunction with std::move_if_noexcept to use move semantics only when a strong exception guarantee exists (and use copy semantics otherwise). Let\u2019s update the code in the previous example as follows: 1 2 //std::pair moved_pair{std::move(my_pair)}; // comment out this line now std :: pair moved_pair { std :: move_if_noexcept ( my_pair )}; // and uncomment this line COPY Running the program again prints: 1 2 3 4 5 6 destroying MoveClass(empty) my_pair.first: MoveClass(13) destroying MoveClass(13) Error found: abort! my_pair.first: MoveClass(13) destroying MoveClass(13) As you can see, after the exception was thrown, the subobject my_pair.first still points to the value 13 . The move constructor of std::pair isn\u2019t noexcept (as of C++20), so std::move_if_noexcept returns my_pair as an l-value reference. This causes moved_pair to be created via the copy constructor (rather than the move constructor). The copy constructor can throw safely, because it doesn\u2019t modify the source object. The standard library uses std::move_if_noexcept often to optimize for functions that are noexcept . For example, std::vector::resize will use move semantics if the element type has a noexcept move constructor, and copy semantics otherwise. This means std::vector will generally operate faster with objects that have a noexcept move constructor. \u6ce8\u610f If a type has both potentially throwing move semantics and deleted copy semantics (the copy constructor and copy assignment operator are unavailable), then std::move_if_noexcept will waive the strong guarantee and invoke move semantics. This conditional waiving of the strong guarantee is ubiquitous in the standard library container classes, since they use std::move_if_noexcept often.","title":"std::move_if_noexcept to the rescue"},{"location":"M-6-std-unique-ptr/","tags":["move","C++14","C++17"],"text":"Key Takeaway \u667a\u80fd\u6307\u9488\u6c38\u8fdc\u90fd\u4e0d\u5e94\u8be5\u88ab\u52a8\u6001\u521b\u5efa\uff0c\u603b\u662f\u5e94\u8be5\u5728\u6808\u4e0a std::unique_ptr \u667a\u80fd\u6307\u9488\u662f\u5176\u7ba1\u7406\u8d44\u6e90\u7684\u552f\u4e00\u62e5\u6709\u8005\uff0c\u4e14\u53ea\u6709\u79fb\u52a8\u8bed\u4e49 std::unique_ptr \u5e76\u4e0d\u4e00\u5b9a\u603b\u662f\u7ba1\u7406\u7740\u67d0\u4e2a\u5bf9\u8c61\u2014\u2014\u6709\u53ef\u80fd\u662f\u5b83\u88ab\u521d\u59cb\u5316\u4e3a\u7a7a\uff08\u4f7f\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u6216 nullptr \u5b57\u9762\u91cf\u4f5c\u4e3a\u53c2\u6570\uff09\uff0c\u4e5f\u6709\u53ef\u80fd\u662f\u5b83\u7ba1\u7406\u7684\u8d44\u6e90\u88ab\u8f6c\u79fb\u4e86\uff0c\u901a\u8fc7\u76f4\u63a5\u5bf9\u5b83\u5224\u65adtrue\u8fd8\u662ffalse\u53ef\u4ee5\u77e5\u9053\u5b83\u662f\u5426\u6b63\u5728\u7ba1\u7406\u8d44\u6e90 \u6a21\u677f\u5316\u51fd\u6570 std::make_unique \u6bd4\u76f4\u63a5\u4f7f\u7528 std::unique_ptr \u66f4\u597d\u7528\u4e5f\u66f4\u5b89\u5168\uff0c\u5b83\u8fd4\u56de\u7684\u662f\u7ba1\u7406\u8d44\u6e90\u7684 std::unique_ptr \u51fd\u6570\u4f20\u53c2\u65f6\uff0c\u4e00\u822c\u4e0d\u5e0c\u671b\u8f6c\u79fb\u6240\u6709\u6743\uff0c\u5426\u5219\u8d44\u6e90\u4f1a\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u3002\u6b64\u65f6\u6700\u597d\u4f20\u9012\u667a\u80fd\u6307\u9488\u7ba1\u7406\u7684\u8d44\u6e90\u672c\u8eab\uff08\u6309\u503c\u4f20\u9012\u6216\u6309\u5730\u5740\u4f20\u9012\u90fd\u53ef\u4ee5\uff09\uff0c\u4f46\u662f\u4e0d\u8981\u628a\u667a\u80fd\u6307\u9488\u7684\u5f15\u7528\u76f4\u63a5\u4f20\u8fdb\u53bb \u5728\u672c\u7ae0\u5f00\u59cb\u7684\u65f6\u5019\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u4f7f\u7528\u6307\u9488\u53ef\u80fd\u4f1a\u5f15\u53d1\u7684bug\u548c\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u51fd\u6570\u7684\u63d0\u524d\u8fd4\u56de\u3001\u5f02\u5e38\u7684\u629b\u51fa\u548c\u6307\u9488\u5220\u9664\u4e0d\u5f53\u90fd\u53ef\u80fd\u5bfc\u81f4\u4e0a\u8ff0\u95ee\u9898\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void someFunction () { auto * ptr { new Resource () }; int x {}; std :: cout << \"Enter an integer: \" ; std :: cin >> x ; if ( x == 0 ) throw 0 ; // the function returns early, and ptr won\u2019t be deleted! // do stuff with ptr here delete ptr ; } \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u5b66\u4e60\u4e86 \u79fb\u52a8\u8bed\u4e49 \u7684\u57fa\u672c\u5185\u5bb9\uff0c\u63a5\u4e0b\u6765\u53ef\u4ee5\u7ee7\u7eed\u56de\u5230\u667a\u80fd\u6307\u9488\u7c7b\u7684\u8ba8\u8bba\u4e86\u3002\u56de\u5fc6\u4e00\u4e0b\uff0c\u667a\u80fd\u6307\u9488\u662f\u4e00\u4e2a\u7528\u4e8e\u7ba1\u7406\u52a8\u6001\u5206\u914d\u5185\u5b58\u7684\u7c7b\u3002\u5c3d\u7ba1\u667a\u80fd\u6307\u9488\u4e5f\u63d0\u4f9b\u5176\u4ed6\u529f\u80fd\uff0c\u4f46\u662f\u5b83\u6700\u672c\u8d28\u7684\u529f\u80fd\u8fd8\u662f\u7ba1\u7406\u52a8\u6001\u5206\u914d\u7684\u8d44\u6e90\uff0c\u5e76\u4e14\u786e\u4fdd\u5728\u6070\u5f53\u7684\u65f6\u5019\uff08\u901a\u5e38\u662f\u667a\u80fd\u6307\u9488 \u79bb\u5f00\u4f5c\u7528\u57df \u65f6\uff09\uff0c\u8be5\u52a8\u6001\u8d44\u6e90\u53ef\u4ee5\u88ab\u6b63\u786e\u5730\u6e05\u7406\u3002 \u56e0\u6b64\uff0c \u667a\u80fd\u6307\u9488\u672c\u8eab\u6c38\u8fdc\u90fd\u4e0d\u5e94\u8be5\u88ab\u52a8\u6001\u521b\u5efa \uff08\u5426\u5219\u667a\u80fd\u6307\u9488\u5bf9\u8c61\u672c\u8eab\u53ef\u80fd\u4f1a\u88ab\u5fd8\u8bb0\u91ca\u653e\uff0c\u4ece\u800c\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\uff09\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u667a\u80fd\u6307\u9488\u5e94\u8be5\u88ab\u521b\u5efa\u5728\u6808\u4e0a\uff08\u4f5c\u4e3a \u5c40\u90e8\u53d8\u91cf \u6216\u4f5c\u4e3a\u5176\u4ed6\u7c7b\u7684\u6210\u5458\uff09\uff0c\u6211\u4eec\u4fdd\u8bc1\u667a\u80fd\u6307\u9488\u5728\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff08\u5305\u542b\u8be5\u6307\u9488\u7684\u51fd\u6570\u6216\u5bf9\u8c61\u7ed3\u675f\u65f6\uff09\uff0c\u5b83\u6240\u62e5\u6709\u7684\u8d44\u6e90\u4f1a\u88ab\u6070\u5f53\u5730\u91ca\u653e\u3002 C++11 \u6807\u51c6\u5e93\u63d0\u4f9b\u4e864\u79cd\u667a\u80fd\u6307\u9488\u7c7b\uff1a std::auto_ptr (\u5728 C++17 \u4e2d\u5df2\u7ecf\u5220\u9664\u4e86)\uff0c std::unique_ptr \uff0c std::shared_ptr \u548c std::weak_ptr \u3002\u5176\u4e2d std::unique_ptr \u662f\u76ee\u524d\u6700\u4e3a\u5e38\u7528\u7684\u4e00\u4e2a\u6307\u9488\u7c7b\uff0c\u6240\u4ee5\u6211\u4eec\u7a0d\u540e\u4f1a\u9996\u5148\u4ecb\u7ecd\u5b83\uff0c\u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ecb\u7ecd std::shared_ptr \u548c std::weak_ptr \u3002 std::unique_ptr std::unique_ptr \u5728C++11\u4e2d\u88ab\u7528\u6765\u66ff\u4ee3 std::auto_ptr \u3002\u8be5\u667a\u80fd\u6307\u9488\u88ab\u7528\u6765\u7ba1\u7406\u52a8\u6001\u5206\u914d\u7684\u5bf9\u8c61\uff0c\u4e14\u8be5\u5bf9\u8c61\u5e76\u4e0d\u88ab\u591a\u4e2a\u5176\u4ed6\u5bf9\u8c61\u6240\u5171\u4eab\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c std::unique_ptr \u5355\u72ec\u62e5\u6709\u5b83\u6240\u7ba1\u7406\u7684\u8d44\u6e90\uff0c\u800c\u4e0d\u4f1a\u548c\u5176\u4ed6\u7c7b\u5171\u4eab\u8d44\u6e90\u7684\u6240\u6709\u6743\u3002 std::unique_ptr \u88ab\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d\u3002 \u518d\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include // for std::unique_ptr class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { // allocate a Resource object and have it owned by std::unique_ptr std :: unique_ptr < Resource > res { new Resource () }; return 0 ; } // res goes out of scope here, and the allocated Resource is destroyed \u56e0\u4e3a std::unique_ptr \u88ab\u5206\u914d\u5728\u6808\u4e0a\uff0c\u6240\u4ee5\u5b83\u6700\u7ec8\u4e00\u5b9a\u4f1a\u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u5f53\u5b83\u79bb\u5f00\u65f6\uff0c\u5b83\u4f1a\u786e\u4fdd Resource \u88ab\u5220\u9664\u3002 \u548c std::auto_ptr \u4e0d\u540c\u7684\u662f std::unique_ptr \u6b63\u786e\u5730\u5b9e\u73b0\u4e86\u79fb\u52a8\u8bed\u4e49\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include #include // for std::unique_ptr #include // for std::move class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { std :: unique_ptr < Resource > res1 { new Resource {} }; // \u521b\u5efa Resource std :: unique_ptr < Resource > res2 {}; // \u521d\u59cb\u5316\u4e3a\u7a7a\u6307\u9488 std :: cout << \"res1 is \" << ( res1 ? \"not null \\n \" : \"null \\n \" ); std :: cout << \"res2 is \" << ( res2 ? \"not null \\n \" : \"null \\n \" ); // res2 = res1; // \u4e0d\u80fd\u7f16\u8bd1: \u56e0\u4e3a\u62f7\u8d1d\u8d4b\u503c\u88ab\u7981\u7528\u4e86 res2 = std :: move ( res1 ); // res2 \u83b7\u53d6\u8d44\u6e90\u6240\u6709\u6743\uff0cres1 \u8bbe\u4e3a\u7a7a std :: cout << \"Ownership transferred \\n \" ; std :: cout << \"res1 is \" << ( res1 ? \"not null \\n \" : \"null \\n \" ); std :: cout << \"res2 is \" << ( res2 ? \"not null \\n \" : \"null \\n \" ); return 0 ; } // Resource destroyed here when res2 goes out of scope \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 2 3 4 5 6 7 Resource acquired res1 is not null res2 is null Ownership transferred res1 is null res2 is not null Resource destroyed \u56e0\u4e3a std::unique_ptr \u5728\u8bbe\u8ba1\u65f6\u8003\u8651\u4e86\u79fb\u52a8\u8bed\u4e49\uff0c\u6240\u4ee5\u5176 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u548c \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u90fd\u88ab\u7981\u7528\u4e86\u3002\u5982\u679c\u4f60\u60f3\u8981\u8f6c\u79fb std::unique_ptr \u7ba1\u7406\u7684\u8d44\u6e90\u7684\u8bdd\uff0c\u5c31\u5fc5\u987b\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49 \u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u79fb\u52a8\u8bed\u4e49\u662f\u901a\u8fc7 std::move \u5b9e\u73b0\u7684\uff08\u5b83\u628a res1 \u8f6c\u6362\u6210\u4e86\u4e00\u4e2a \u53f3\u503c \uff0c\u56e0\u6b64\u89e6\u53d1\u4e86\u79fb\u52a8\u8d4b\u503c\u800c\u4e0d\u662f\u62f7\u8d1d\u8d4b\u503c\uff09\u3002 \u8bbf\u95ee\u88ab\u667a\u80fd\u6307\u9488\u7ba1\u7406\u7684\u5bf9\u8c61 std::unique_ptr \u7c7b\u91cd\u8f7d\u4e86 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u548c \u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26-> \uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e24\u4e2a\u8fd0\u7b97\u7b26\u6765\u8fd4\u56de\u88ab\u7ba1\u7406\u7684\u5bf9\u8c61\u3002\u89e3\u5f15\u7528\u8fd0\u7b97\u7b26\u4f1a\u8fd4\u56de\u88ab\u7ba1\u7406\u8d44\u6e90\u7684\u5f15\u7528\uff0c\u800c operator-> \u8fd4\u56de\u4e00\u4e2a\u6307\u9488\u3002 \u8bb0\u4f4f\uff0c std::unique_ptr \u5e76\u4e0d\u4e00\u5b9a\u603b\u662f\u7ba1\u7406\u7740\u67d0\u4e2a\u5bf9\u8c61\u2014\u2014\u6709\u53ef\u80fd\u662f\u5b83\u88ab\u521d\u59cb\u5316\u4e3a\u7a7a\uff08\u4f7f\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u6216 nullptr \u5b57\u9762\u91cf\u4f5c\u4e3a\u53c2\u6570\uff09\uff0c\u4e5f\u6709\u53ef\u80fd\u662f\u5b83\u7ba1\u7406\u7684\u8d44\u6e90\u88ab\u8f6c\u79fb\u4e86 \u3002 \u6240\u4ee5\u5728\u4f7f\u7528\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u4e4b\u524d\uff0c\u5fc5\u987b\u9996\u5148\u68c0\u67e5 std::unique_ptr \u662f\u5426\u6709\u8d44\u6e90\u3002 \u56e0\u4e3a std::unique_ptr \u4f1a\u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a\u5e03\u5c14\u7c7b\u578b\uff0c\u6240\u4ee5\u53ea\u9700\u8981\u5bf9\u8be5\u6307\u9488\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\u5c31\u53ef\u4ee5\uff0c\u5f53\u8fd4\u56de true \u65f6\u8bf4\u660e\u5b83\u5305\u542b\u8d44\u6e90\u3002 Here\u2019s an example of this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include // for std::unique_ptr class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } friend std :: ostream & operator << ( std :: ostream & out , const Resource & res ) { out << \"I am a resource\" ; return out ; } }; int main () { std :: unique_ptr < Resource > res { new Resource {} }; if ( res ) // use implicit cast to bool to ensure res contains a Resource std :: cout << * res << '\\n' ; // print the Resource that res is owning return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 Resource acquired I am a resource Resource destroyed \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u91cd\u8f7d\u7684 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u6765\u83b7\u53d6 std::unique_ptr res \u7ba1\u7406\u7684 Resource \u5bf9\u8c61\uff0c\u7136\u540e\u5c06\u5176\u9001\u53bb std::cout \u6253\u5370\u3002 std::unique_ptr \u548c\u6570\u7ec4 \u548c std::auto_ptr \u4e0d\u4e00\u6837\u7684\u662f\uff0c std::unique_ptr \u8db3\u591f\u53ea\u80fd\uff0c\u5b83\u61c2\u5f97\u4f7f\u7528\u6070\u5f53\u7684 delete \u53bb\u5220\u9664\u5185\u5b58\uff08\u666e\u901a delete \u6216\u6570\u7ec4 delete \uff09\uff0c\u6240\u4ee5\uff0c std::unique_ptr \u65e2\u53ef\u4ee5\u7528\u4e8e\u4e00\u822c\u5bf9\u8c61\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6570\u7ec4\u3002 \u4f46\u662f\uff0c \u76f8\u5bf9\u4e8e\u4f7f\u7528 std::unique_ptr \u7ba1\u7406\u4e00\u4e2a\u56fa\u5b9a\u6570\u7ec4\u6216C\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528 std::array \u6216\u8005 std::vector (\u6216\u8005 std::string ) \u603b\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002 \u6700\u4f73\u5b9e\u8df5 \u76f8\u5bf9\u4e8e\u4f7f\u7528 std::unique_ptr \u7ba1\u7406\u4e00\u4e2a\u56fa\u5b9a\u6570\u7ec4\u3001\u52a8\u6001\u6570\u7ec4\u6216C\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528 std::array \u6216\u8005 std::vector (\u6216\u8005 std::string ) \u603b\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002 std::make_unique C++14 \u65b0\u589e\u4e86\u4e00\u4e2a\u540d\u4e3a std::make_unique() \u7684\u51fd\u6570\u3002\u8fd9\u4e2a\u6a21\u677f\u5316\u51fd\u6570\u57fa\u4e8e\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\u6784\u5efa\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528\u4f20\u5165\u7684\u53c2\u6570\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include // for std::unique_ptr \u548c std::make_unique #include class Fraction { private : int m_numerator { 0 }; int m_denominator { 1 }; public : Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } }; int main () { // \u521b\u5efa\u4e00\u4e2a\u52a8\u6001\u5206\u914d\u7684 Fraction // \u8fd9\u91cc\u53ef\u4ee5\u914d\u5408\u4f7f\u7528\u81ea\u52a8\u7c7b\u578b\u63a8\u65ad\u83b7\u5f97\u66f4\u597d\u7684\u6548\u679c auto f1 { std :: make_unique < Fraction > ( 3 , 5 ) }; std :: cout << * f1 << '\\n' ; // \u521b\u5efa\u4e00\u4e2a\u52a8\u6001\u5206\u914d\u7684 Fraction \u6570\u7ec4\uff0c\u957f\u5ea6\u4e3a4 auto f2 { std :: make_unique < Fraction [] > ( 4 ) }; std :: cout << f2 [ 0 ] << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 3/5 0/1 \u4f7f\u7528 std::make_unique() \u662f\u4e00\u79cd\u53ef\u9009\u7684\u529e\u6cd5\uff0c\u4f46\u662f\u76f8\u5bf9\u4e8e\u76f4\u63a5\u521b\u5efa std::unique_ptr \uff0c\u6211\u4eec\u66f4\u63a8\u8350\u8fd9\u79cd\u505a\u6cd5\u3002\u4e0d\u5173\u56e0\u4e3a\u4f7f\u7528 std::make_unique \u7684\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff08\u5c24\u5176\u662f\u4f7f\u7528\u4e86 \u7c7b\u578b\u63a8\u65ad \u540e\uff09\uff0c\u5b83\u8fd8\u53ef\u4ee5\u89e3\u51b3C++\u53c2\u6570\u6c42\u503c\u987a\u5e8f\u6ca1\u6709\u89c4\u8303\u800c\u5f15\u53d1\u7684\u5f02\u5e38\u5b89\u5168\u95ee\u9898\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528 std::make_unique() \u6765\u4ee3\u66ff std::unique_ptr \u548c new \u3002 \u5f02\u5e38\u5b89\u5168\u95ee\u9898 \u5982\u679c\u4f60\u5bf9\u4e0a\u6587\u63d0\u5230\u7684 \u5f02\u5e38\u5b89\u5168\u95ee\u9898 \u611f\u5230\u597d\u5947\u7684\u8bdd\uff0c\u8fd9\u4e00\u5c0f\u7ed3\u6211\u4eec\u4f1a\u8be6\u7ec6\u8fdb\u884c\u4ecb\u7ecd\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 some_function ( std :: unique_ptr < T > ( new T ), function_that_can_throw_exception ()); \u5bf9\u4e8e\u5982\u4f55\u6267\u884c\u4e0a\u8ff0\u51fd\u6570\u8c03\u7528\uff0c\u7f16\u8bd1\u5668\u6709\u5f88\u5927\u7684\u7075\u6d3b\u6027\u3002\u5b83\u53ef\u4ee5\u5148\u521b\u5efa\u65b0\u7684\u7c7b\u578bT\uff0c\u7136\u540e\u8c03\u7528 function_that_can_throw_exception() \u5417\uff0c\u7136\u540e\u521b\u5efa std::unique_ptr \u53bb\u7ba1\u7406\u52a8\u6001\u5206\u914d\u7684T\u3002\u5982\u679c function_that_can_throw_exception() \u629b\u51fa\u4e86\u4f9d\u6b21\uff0c\u90a3\u4e48T\u5728\u88ab\u5206\u914d\u5185\u5b58\u540e\uff0c\u6ca1\u6709\u88ab\u91ca\u653e\uff0c\u56e0\u4e3a\u7528\u4e8e\u7ba1\u7406\u5b83\u7684\u667a\u80fd\u6307\u9488\u8fd8\u6ca1\u6709\u88ab\u521b\u5efa\u3002\u663e\u7136\u8fd9\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002 std::make_unique() \u5ba2\u670d\u4e86\u6574\u4e2a\u95ee\u9898\uff0c\u56e0\u4e3a\u5bf9\u8c61T\u548c std::unique_ptr \u7684\u521b\u5efa\u90fd\u662f\u5728 std::make_unique() \u4e2d\u5b8c\u6210\u7684\uff0c\u6267\u884c\u987a\u5e8f\u6ca1\u6709\u6b67\u4e49\u3002 \u51fd\u6570\u8fd4\u56de std::unique_ptr std::unique_ptr \u53ef\u4ee5\u5b89\u5168\u5730\u4ece\u51fd\u6570\u4e2d \u6309\u503c\u8fd4\u56de \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // for std::unique_ptr std :: unique_ptr < Resource > createResource () { return std :: make_unique < Resource > (); //\u533f\u540d\u5bf9\u8c61\u662f\u53f3\u503c\uff0c\u6240\u4ee5\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49 } int main () { auto ptr { createResource () }; // do whatever return 0 ; } \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c createResource() \u6309\u503c\u8fd4\u56de\u4e86 std::unique_ptr \u3002\u5982\u679c\u8fd9\u4e2a\u503c\u6ca1\u6709\u88ab\u8d4b\u503c\u7ed9\u4efb\u4f55\u5176\u4ed6\u7684\u5bf9\u8c61\uff0c\u90a3\u4e48\u4e34\u65f6\u5bf9\u8c61\u5c31\u4f1a\u79bb\u5f00\u4f5c\u7528\u57df\uff0c Resource \u4e5f\u4f1a\u88ab\u6e05\u7406\u3002\u5982\u679c\u5b83\u88ab\u8d4b\u503c\u7ed9\u5176\u4ed6\u5bf9\u8c61 (\u50cf main() \u4e2d\u90a3\u6837)\uff0c\u5728C++14\u6216\u66f4\u65e9\u7684\u4ee3\u7801\u4e2d\uff0c \u79fb\u52a8\u8bed\u4e49 \u4f1a\u88ab\u7528\u6765\u5c06\u8fd4\u56de\u503c Resource \u8f6c\u79fb\u7ed9\u88ab\u8d4b\u503c\u7684\u5bf9\u8c61\uff08\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684 ptr \uff09\u3002\u5728C++17\u6216\u66f4\u65b0\u7684\u4ee3\u7801\u4e2d\uff0c\u8fd9\u4e2a\u8fd4\u56de\u503c\u4f1a\u88ab\u7701\u7565\uff0c\u56e0\u6b64\u901a\u8fc7 std::unique_ptr \u8fd4\u56de\u8d44\u6e90\u8fd4\u56de\u539f\u59cb\u6307\u9488\u8981\u5b89\u5168\u7684\u591a\uff01 \u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u4f60\u4e0d\u5e94\u8be5\u5c06 std::unique_ptr \u6309\u6307\u9488\u8fd4\u56de\uff08\u6c38\u8fdc\u4e0d\u8981\uff09\u6216\u8005\u6309\u5f15\u7528\u8fd4\u56de\uff08\u9664\u975e\u4f60\u6709\u8db3\u591f\u7684\u7406\u7531\uff09\u3002 \u4f20\u9012 std::unique_ptr \u7ed9\u51fd\u6570 \u5982\u679c\u4f60\u9700\u8981\u51fd\u6570\u83b7\u53d6 std::unique_ptr \u7ba1\u7406\u7684\u8d44\u6e90\uff0c\u4f7f\u7528 \u6309\u503c\u4f20\u9012 \u3002\u6ce8\u610f\uff0c\u7531\u4e8e \u62f7\u8d1d\u8bed\u4e49 \u88ab\u7981\u7528\u4e86\uff0c\u6240\u4ee5\u5fc5\u987b\u5bf9\u5b9e\u9645\u4f20\u5165\u7684\u53c2\u6570\u8c03\u7528 std::move \u3002 \u8bd1\u8005\u6ce8 \u9700\u4e0d\u9700\u8981\u4f7f\u7528 std::move \u8981\u770b\u4f20\u5165\u7684\u662f\u5de6\u503c\u8fd8\u662f\u53f3\u503c\uff0c\u5de6\u503c\u624d\u9700\u8981\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include #include // for std::unique_ptr #include // for std::move class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } friend std :: ostream & operator << ( std :: ostream & out , const Resource & res ) { out << \"I am a resource\" ; return out ; } }; void takeOwnership ( std :: unique_ptr < Resource > res ) { if ( res ) std :: cout << * res << '\\n' ; } // the Resource is destroyed here int main () { auto ptr { std :: make_unique < Resource > () }; // takeOwnership(ptr); // This doesn't work, need to use move semantics takeOwnership ( std :: move ( ptr )); // ok: \u4f7f\u7528\u79fb\u52a8\u8bed\u4e49 std :: cout << \"Ending program \\n \" ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 3 4 Resource acquired I am a resource Resource destroyed Ending program \u6ce8\u610f\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c Resource \u7684\u6240\u6709\u6743\u88ab\u8f6c\u79fb\u7ed9\u4e86 takeOwnership() \uff0c\u6240\u4ee5 Resource \u4f1a\u5728 takeOwnership() \u51fd\u6570\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\uff0c\u800c\u4e0d\u662f main() \u3002 \u4f46\u662f\uff0c\u5927\u591a\u6570\u65f6\u5019\uff0c\u6211\u4eec\u4e0d\u4f1a\u5e0c\u671b\u67d0\u4e2a\u51fd\u6570\u62e5\u6709\u8d44\u6e90\u7684\u6240\u6709\u6743\u3002\u5c3d\u7ba1\u4f60\u53ef\u4ee5 \u6309\u5f15\u7528\u4f20\u9012 std::unique_ptr (\u8fd9\u6837\u4e00\u6765\u51fd\u6570\u5c31\u65e0\u9700\u8003\u8651\u6240\u6709\u6743\u76f4\u63a5\u4f7f\u7528\u5bf9\u8c61\u672c\u8eab)\uff0c\u4f46\u662f\u8fd9\u79cd\u505a\u6cd5\u53ea\u6709\u5728\u51fd\u6570\u53ef\u80fd\u9700\u8981\u6539\u53d8\u88ab\u7ba1\u7406\u7684\u5bf9\u8c61\u65f6\u624d\u4f7f\u7528\u3002 \u76f8\u53cd\uff0c\u6700\u597d\u662f \u76f4\u63a5\u4f20\u9012\u8d44\u6e90\u672c\u8eab (\u901a\u8fc7\u6307\u9488\u6216\u5f15\u7528\uff0c\u53d6\u51b3\u4e8enull\u662f\u5426\u662f\u6709\u6548\u53c2\u6570)\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u51fd\u6570\u5c31\u65e0\u9700\u5173\u5fc3\u8d44\u6e90\u662f\u5982\u4f55\u88ab\u7ba1\u7406\u7684\u4e86\u3002\u8981\u4ece std::unique_ptr \u4e2d\u83b7\u53d6\u539f\u59cb\u8d44\u6e90\u6307\u9488\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 get() \u6210\u5458\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include // for std::unique_ptr #include class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } friend std :: ostream & operator << ( std :: ostream & out , const Resource & res ) { out << \"I am a resource\" ; return out ; } }; // \u51fd\u6570\u503c\u4f1a\u4f7f\u7528\u8d44\u6e90\uff0c\u6240\u4ee5\u5b83\u53ea\u9700\u8981\u83b7\u53d6\u6307\u5411\u8d44\u6e90\u672c\u8eab\u7684\u6307\u9488\uff0c\u800c\u4e0d\u9700\u8981\u83b7\u53d6\u5b8c\u6574\u7684 std::unique_ptr \u7684\u5f15\u7528 void useResource ( Resource * res ) { if ( res ) std :: cout << * res << '\\n' ; else std :: cout << \"No resource \\n \" ; } int main () { auto ptr { std :: make_unique < Resource > () }; useResource ( ptr . get ()); // note: \u901a\u8fc7 get() \u83b7\u53d6\u88ab\u7ba1\u7406\u7684\u8d44\u6e90 std :: cout << \"Ending program \\n \" ; return 0 ; } // The Resource is destroyed here \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 2 3 4 Resource acquired I am a resource Ending program Resource destroyed std::unique_ptr \u548c\u7c7b \u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5c06 std::unique_ptr \u4f5c\u4e3a\u4e00\u4e2a\u7c7b\u6210\u5458\uff08\u5f62\u6210 \u7ec4\u5408\u5173\u7cfb \u5173\u7cfb\uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4f60\u5c31\u4e0d\u9700\u8981\u5728\u7c7b\u7684\u6790\u6784\u51fd\u6570\u4e2d\u5220\u9664\u8be5\u52a8\u5185\u5b58\uff0c\u56e0\u4e3a std::unique_ptr \u4f1a\u5728\u7c7b\u5bf9\u8c61\u9500\u6bc1\u65f6\u81ea\u52a8\u9500\u6bc1\u3002 \u4f46\u662f\uff0c\u5982\u679c\u7c7b\u6ca1\u6709\u88ab\u6b63\u786e\u5730\u9500\u6bc1\u7684\u8bdd\uff08\u4f8b\u5982\uff0c\u52a8\u6001\u5206\u914d\u4e86\u5185\u5b58\u4f46\u6ca1\u6709\u88ab\u91ca\u653e\uff09\uff0c\u4e8e\u662f std::unique_ptr \u6210\u5458\u4e5f\u4e0d\u4f1a\u88ab\u9500\u6bc1\uff0c\u6240\u4ee5\u5b83\u7ba1\u7406\u7684\u8d44\u6e90\u4e5f\u4e0d\u4f1a\u88ab\u91ca\u653e\u3002 std::unique_ptr \u7684\u8bef\u7528 std::unique_ptrs \u6709\u4e24\u79cd\u5178\u578b\u7684\u8bef\u7528\uff0c\u4f46\u662f\u5b83\u4eec\u5176\u5b9e\u5f88\u5bb9\u6613\u907f\u514d\u3002\u9996\u5148\uff0c\u4e0d\u8981\u8ba9\u591a\u4e2a\u7c7b\u7ba1\u7406\u540c\u4e00\u4e2a\u8d44\u6e90\uff0c\u4f8b\u5982\uff1a 1 2 3 Resource * res { new Resource () }; std :: unique_ptr < Resource > res1 { res }; std :: unique_ptr < Resource > res2 { res }; \u5c3d\u7ba1\u8bed\u6cd5\u4e0a\u5b8c\u5168\u6b63\u786e\uff0c\u4f46\u662f\u6700\u7ec8\u7684\u7ed3\u679c\u662f res1 \u548c res2 \u90fd\u4f1a\u53bb\u5220\u9664\u8d44\u6e90 Resource \uff0c\u8fdb\u800c\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u7b2c\u4e8c\uff0c\u4e0d\u8981\u624b\u52a8\u91ca\u653e\u5df2\u7ecf\u5728 std::unique_ptr \u7ba1\u7406\u4e0b\u7684\u8d44\u6e90\uff1a 1 2 3 Resource * res { new Resource () }; std :: unique_ptr < Resource > res1 { res }; delete res ; \u8fd9\u4e48\u505a\u7684\u8bdd\uff0c std::unique_ptr \u5c31\u4f1a\u518d\u6b21\u5220\u9664\u8fd9\u4e2a\u5df2\u7ecf\u88ab\u91ca\u653e\u7684\u8d44\u6e90\uff0c\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4f7f\u7528 std::make_unique() \u53ef\u4ee5\u907f\u514d\u4e0a\u8ff0\u4e24\u4e2a\u95ee\u9898\u3002","title":"M.6 \u2014 std::unique_ptr"},{"location":"M-6-std-unique-ptr/#stdunique_ptr","text":"std::unique_ptr \u5728C++11\u4e2d\u88ab\u7528\u6765\u66ff\u4ee3 std::auto_ptr \u3002\u8be5\u667a\u80fd\u6307\u9488\u88ab\u7528\u6765\u7ba1\u7406\u52a8\u6001\u5206\u914d\u7684\u5bf9\u8c61\uff0c\u4e14\u8be5\u5bf9\u8c61\u5e76\u4e0d\u88ab\u591a\u4e2a\u5176\u4ed6\u5bf9\u8c61\u6240\u5171\u4eab\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c std::unique_ptr \u5355\u72ec\u62e5\u6709\u5b83\u6240\u7ba1\u7406\u7684\u8d44\u6e90\uff0c\u800c\u4e0d\u4f1a\u548c\u5176\u4ed6\u7c7b\u5171\u4eab\u8d44\u6e90\u7684\u6240\u6709\u6743\u3002 std::unique_ptr \u88ab\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d\u3002 \u518d\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include // for std::unique_ptr class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { // allocate a Resource object and have it owned by std::unique_ptr std :: unique_ptr < Resource > res { new Resource () }; return 0 ; } // res goes out of scope here, and the allocated Resource is destroyed \u56e0\u4e3a std::unique_ptr \u88ab\u5206\u914d\u5728\u6808\u4e0a\uff0c\u6240\u4ee5\u5b83\u6700\u7ec8\u4e00\u5b9a\u4f1a\u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u5f53\u5b83\u79bb\u5f00\u65f6\uff0c\u5b83\u4f1a\u786e\u4fdd Resource \u88ab\u5220\u9664\u3002 \u548c std::auto_ptr \u4e0d\u540c\u7684\u662f std::unique_ptr \u6b63\u786e\u5730\u5b9e\u73b0\u4e86\u79fb\u52a8\u8bed\u4e49\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include #include // for std::unique_ptr #include // for std::move class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { std :: unique_ptr < Resource > res1 { new Resource {} }; // \u521b\u5efa Resource std :: unique_ptr < Resource > res2 {}; // \u521d\u59cb\u5316\u4e3a\u7a7a\u6307\u9488 std :: cout << \"res1 is \" << ( res1 ? \"not null \\n \" : \"null \\n \" ); std :: cout << \"res2 is \" << ( res2 ? \"not null \\n \" : \"null \\n \" ); // res2 = res1; // \u4e0d\u80fd\u7f16\u8bd1: \u56e0\u4e3a\u62f7\u8d1d\u8d4b\u503c\u88ab\u7981\u7528\u4e86 res2 = std :: move ( res1 ); // res2 \u83b7\u53d6\u8d44\u6e90\u6240\u6709\u6743\uff0cres1 \u8bbe\u4e3a\u7a7a std :: cout << \"Ownership transferred \\n \" ; std :: cout << \"res1 is \" << ( res1 ? \"not null \\n \" : \"null \\n \" ); std :: cout << \"res2 is \" << ( res2 ? \"not null \\n \" : \"null \\n \" ); return 0 ; } // Resource destroyed here when res2 goes out of scope \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 2 3 4 5 6 7 Resource acquired res1 is not null res2 is null Ownership transferred res1 is null res2 is not null Resource destroyed \u56e0\u4e3a std::unique_ptr \u5728\u8bbe\u8ba1\u65f6\u8003\u8651\u4e86\u79fb\u52a8\u8bed\u4e49\uff0c\u6240\u4ee5\u5176 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u548c \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u90fd\u88ab\u7981\u7528\u4e86\u3002\u5982\u679c\u4f60\u60f3\u8981\u8f6c\u79fb std::unique_ptr \u7ba1\u7406\u7684\u8d44\u6e90\u7684\u8bdd\uff0c\u5c31\u5fc5\u987b\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49 \u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u79fb\u52a8\u8bed\u4e49\u662f\u901a\u8fc7 std::move \u5b9e\u73b0\u7684\uff08\u5b83\u628a res1 \u8f6c\u6362\u6210\u4e86\u4e00\u4e2a \u53f3\u503c \uff0c\u56e0\u6b64\u89e6\u53d1\u4e86\u79fb\u52a8\u8d4b\u503c\u800c\u4e0d\u662f\u62f7\u8d1d\u8d4b\u503c\uff09\u3002","title":"std::unique_ptr"},{"location":"M-6-std-unique-ptr/#_1","text":"std::unique_ptr \u7c7b\u91cd\u8f7d\u4e86 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u548c \u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26-> \uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e24\u4e2a\u8fd0\u7b97\u7b26\u6765\u8fd4\u56de\u88ab\u7ba1\u7406\u7684\u5bf9\u8c61\u3002\u89e3\u5f15\u7528\u8fd0\u7b97\u7b26\u4f1a\u8fd4\u56de\u88ab\u7ba1\u7406\u8d44\u6e90\u7684\u5f15\u7528\uff0c\u800c operator-> \u8fd4\u56de\u4e00\u4e2a\u6307\u9488\u3002 \u8bb0\u4f4f\uff0c std::unique_ptr \u5e76\u4e0d\u4e00\u5b9a\u603b\u662f\u7ba1\u7406\u7740\u67d0\u4e2a\u5bf9\u8c61\u2014\u2014\u6709\u53ef\u80fd\u662f\u5b83\u88ab\u521d\u59cb\u5316\u4e3a\u7a7a\uff08\u4f7f\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u6216 nullptr \u5b57\u9762\u91cf\u4f5c\u4e3a\u53c2\u6570\uff09\uff0c\u4e5f\u6709\u53ef\u80fd\u662f\u5b83\u7ba1\u7406\u7684\u8d44\u6e90\u88ab\u8f6c\u79fb\u4e86 \u3002 \u6240\u4ee5\u5728\u4f7f\u7528\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u4e4b\u524d\uff0c\u5fc5\u987b\u9996\u5148\u68c0\u67e5 std::unique_ptr \u662f\u5426\u6709\u8d44\u6e90\u3002 \u56e0\u4e3a std::unique_ptr \u4f1a\u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a\u5e03\u5c14\u7c7b\u578b\uff0c\u6240\u4ee5\u53ea\u9700\u8981\u5bf9\u8be5\u6307\u9488\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\u5c31\u53ef\u4ee5\uff0c\u5f53\u8fd4\u56de true \u65f6\u8bf4\u660e\u5b83\u5305\u542b\u8d44\u6e90\u3002 Here\u2019s an example of this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include // for std::unique_ptr class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } friend std :: ostream & operator << ( std :: ostream & out , const Resource & res ) { out << \"I am a resource\" ; return out ; } }; int main () { std :: unique_ptr < Resource > res { new Resource {} }; if ( res ) // use implicit cast to bool to ensure res contains a Resource std :: cout << * res << '\\n' ; // print the Resource that res is owning return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 Resource acquired I am a resource Resource destroyed \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u91cd\u8f7d\u7684 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u6765\u83b7\u53d6 std::unique_ptr res \u7ba1\u7406\u7684 Resource \u5bf9\u8c61\uff0c\u7136\u540e\u5c06\u5176\u9001\u53bb std::cout \u6253\u5370\u3002","title":"\u8bbf\u95ee\u88ab\u667a\u80fd\u6307\u9488\u7ba1\u7406\u7684\u5bf9\u8c61"},{"location":"M-6-std-unique-ptr/#stdunique_ptr_1","text":"\u548c std::auto_ptr \u4e0d\u4e00\u6837\u7684\u662f\uff0c std::unique_ptr \u8db3\u591f\u53ea\u80fd\uff0c\u5b83\u61c2\u5f97\u4f7f\u7528\u6070\u5f53\u7684 delete \u53bb\u5220\u9664\u5185\u5b58\uff08\u666e\u901a delete \u6216\u6570\u7ec4 delete \uff09\uff0c\u6240\u4ee5\uff0c std::unique_ptr \u65e2\u53ef\u4ee5\u7528\u4e8e\u4e00\u822c\u5bf9\u8c61\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6570\u7ec4\u3002 \u4f46\u662f\uff0c \u76f8\u5bf9\u4e8e\u4f7f\u7528 std::unique_ptr \u7ba1\u7406\u4e00\u4e2a\u56fa\u5b9a\u6570\u7ec4\u6216C\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528 std::array \u6216\u8005 std::vector (\u6216\u8005 std::string ) \u603b\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002 \u6700\u4f73\u5b9e\u8df5 \u76f8\u5bf9\u4e8e\u4f7f\u7528 std::unique_ptr \u7ba1\u7406\u4e00\u4e2a\u56fa\u5b9a\u6570\u7ec4\u3001\u52a8\u6001\u6570\u7ec4\u6216C\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528 std::array \u6216\u8005 std::vector (\u6216\u8005 std::string ) \u603b\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002","title":"std::unique_ptr \u548c\u6570\u7ec4"},{"location":"M-6-std-unique-ptr/#stdmake_unique","text":"C++14 \u65b0\u589e\u4e86\u4e00\u4e2a\u540d\u4e3a std::make_unique() \u7684\u51fd\u6570\u3002\u8fd9\u4e2a\u6a21\u677f\u5316\u51fd\u6570\u57fa\u4e8e\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\u6784\u5efa\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528\u4f20\u5165\u7684\u53c2\u6570\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include // for std::unique_ptr \u548c std::make_unique #include class Fraction { private : int m_numerator { 0 }; int m_denominator { 1 }; public : Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } }; int main () { // \u521b\u5efa\u4e00\u4e2a\u52a8\u6001\u5206\u914d\u7684 Fraction // \u8fd9\u91cc\u53ef\u4ee5\u914d\u5408\u4f7f\u7528\u81ea\u52a8\u7c7b\u578b\u63a8\u65ad\u83b7\u5f97\u66f4\u597d\u7684\u6548\u679c auto f1 { std :: make_unique < Fraction > ( 3 , 5 ) }; std :: cout << * f1 << '\\n' ; // \u521b\u5efa\u4e00\u4e2a\u52a8\u6001\u5206\u914d\u7684 Fraction \u6570\u7ec4\uff0c\u957f\u5ea6\u4e3a4 auto f2 { std :: make_unique < Fraction [] > ( 4 ) }; std :: cout << f2 [ 0 ] << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 3/5 0/1 \u4f7f\u7528 std::make_unique() \u662f\u4e00\u79cd\u53ef\u9009\u7684\u529e\u6cd5\uff0c\u4f46\u662f\u76f8\u5bf9\u4e8e\u76f4\u63a5\u521b\u5efa std::unique_ptr \uff0c\u6211\u4eec\u66f4\u63a8\u8350\u8fd9\u79cd\u505a\u6cd5\u3002\u4e0d\u5173\u56e0\u4e3a\u4f7f\u7528 std::make_unique \u7684\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff08\u5c24\u5176\u662f\u4f7f\u7528\u4e86 \u7c7b\u578b\u63a8\u65ad \u540e\uff09\uff0c\u5b83\u8fd8\u53ef\u4ee5\u89e3\u51b3C++\u53c2\u6570\u6c42\u503c\u987a\u5e8f\u6ca1\u6709\u89c4\u8303\u800c\u5f15\u53d1\u7684\u5f02\u5e38\u5b89\u5168\u95ee\u9898\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528 std::make_unique() \u6765\u4ee3\u66ff std::unique_ptr \u548c new \u3002","title":"std::make_unique"},{"location":"M-6-std-unique-ptr/#_2","text":"\u5982\u679c\u4f60\u5bf9\u4e0a\u6587\u63d0\u5230\u7684 \u5f02\u5e38\u5b89\u5168\u95ee\u9898 \u611f\u5230\u597d\u5947\u7684\u8bdd\uff0c\u8fd9\u4e00\u5c0f\u7ed3\u6211\u4eec\u4f1a\u8be6\u7ec6\u8fdb\u884c\u4ecb\u7ecd\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 some_function ( std :: unique_ptr < T > ( new T ), function_that_can_throw_exception ()); \u5bf9\u4e8e\u5982\u4f55\u6267\u884c\u4e0a\u8ff0\u51fd\u6570\u8c03\u7528\uff0c\u7f16\u8bd1\u5668\u6709\u5f88\u5927\u7684\u7075\u6d3b\u6027\u3002\u5b83\u53ef\u4ee5\u5148\u521b\u5efa\u65b0\u7684\u7c7b\u578bT\uff0c\u7136\u540e\u8c03\u7528 function_that_can_throw_exception() \u5417\uff0c\u7136\u540e\u521b\u5efa std::unique_ptr \u53bb\u7ba1\u7406\u52a8\u6001\u5206\u914d\u7684T\u3002\u5982\u679c function_that_can_throw_exception() \u629b\u51fa\u4e86\u4f9d\u6b21\uff0c\u90a3\u4e48T\u5728\u88ab\u5206\u914d\u5185\u5b58\u540e\uff0c\u6ca1\u6709\u88ab\u91ca\u653e\uff0c\u56e0\u4e3a\u7528\u4e8e\u7ba1\u7406\u5b83\u7684\u667a\u80fd\u6307\u9488\u8fd8\u6ca1\u6709\u88ab\u521b\u5efa\u3002\u663e\u7136\u8fd9\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002 std::make_unique() \u5ba2\u670d\u4e86\u6574\u4e2a\u95ee\u9898\uff0c\u56e0\u4e3a\u5bf9\u8c61T\u548c std::unique_ptr \u7684\u521b\u5efa\u90fd\u662f\u5728 std::make_unique() \u4e2d\u5b8c\u6210\u7684\uff0c\u6267\u884c\u987a\u5e8f\u6ca1\u6709\u6b67\u4e49\u3002","title":"\u5f02\u5e38\u5b89\u5168\u95ee\u9898"},{"location":"M-6-std-unique-ptr/#stdunique_ptr_2","text":"std::unique_ptr \u53ef\u4ee5\u5b89\u5168\u5730\u4ece\u51fd\u6570\u4e2d \u6309\u503c\u8fd4\u56de \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // for std::unique_ptr std :: unique_ptr < Resource > createResource () { return std :: make_unique < Resource > (); //\u533f\u540d\u5bf9\u8c61\u662f\u53f3\u503c\uff0c\u6240\u4ee5\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49 } int main () { auto ptr { createResource () }; // do whatever return 0 ; } \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c createResource() \u6309\u503c\u8fd4\u56de\u4e86 std::unique_ptr \u3002\u5982\u679c\u8fd9\u4e2a\u503c\u6ca1\u6709\u88ab\u8d4b\u503c\u7ed9\u4efb\u4f55\u5176\u4ed6\u7684\u5bf9\u8c61\uff0c\u90a3\u4e48\u4e34\u65f6\u5bf9\u8c61\u5c31\u4f1a\u79bb\u5f00\u4f5c\u7528\u57df\uff0c Resource \u4e5f\u4f1a\u88ab\u6e05\u7406\u3002\u5982\u679c\u5b83\u88ab\u8d4b\u503c\u7ed9\u5176\u4ed6\u5bf9\u8c61 (\u50cf main() \u4e2d\u90a3\u6837)\uff0c\u5728C++14\u6216\u66f4\u65e9\u7684\u4ee3\u7801\u4e2d\uff0c \u79fb\u52a8\u8bed\u4e49 \u4f1a\u88ab\u7528\u6765\u5c06\u8fd4\u56de\u503c Resource \u8f6c\u79fb\u7ed9\u88ab\u8d4b\u503c\u7684\u5bf9\u8c61\uff08\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684 ptr \uff09\u3002\u5728C++17\u6216\u66f4\u65b0\u7684\u4ee3\u7801\u4e2d\uff0c\u8fd9\u4e2a\u8fd4\u56de\u503c\u4f1a\u88ab\u7701\u7565\uff0c\u56e0\u6b64\u901a\u8fc7 std::unique_ptr \u8fd4\u56de\u8d44\u6e90\u8fd4\u56de\u539f\u59cb\u6307\u9488\u8981\u5b89\u5168\u7684\u591a\uff01 \u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u4f60\u4e0d\u5e94\u8be5\u5c06 std::unique_ptr \u6309\u6307\u9488\u8fd4\u56de\uff08\u6c38\u8fdc\u4e0d\u8981\uff09\u6216\u8005\u6309\u5f15\u7528\u8fd4\u56de\uff08\u9664\u975e\u4f60\u6709\u8db3\u591f\u7684\u7406\u7531\uff09\u3002","title":"\u51fd\u6570\u8fd4\u56de std::unique_ptr"},{"location":"M-6-std-unique-ptr/#stdunique_ptr_3","text":"\u5982\u679c\u4f60\u9700\u8981\u51fd\u6570\u83b7\u53d6 std::unique_ptr \u7ba1\u7406\u7684\u8d44\u6e90\uff0c\u4f7f\u7528 \u6309\u503c\u4f20\u9012 \u3002\u6ce8\u610f\uff0c\u7531\u4e8e \u62f7\u8d1d\u8bed\u4e49 \u88ab\u7981\u7528\u4e86\uff0c\u6240\u4ee5\u5fc5\u987b\u5bf9\u5b9e\u9645\u4f20\u5165\u7684\u53c2\u6570\u8c03\u7528 std::move \u3002 \u8bd1\u8005\u6ce8 \u9700\u4e0d\u9700\u8981\u4f7f\u7528 std::move \u8981\u770b\u4f20\u5165\u7684\u662f\u5de6\u503c\u8fd8\u662f\u53f3\u503c\uff0c\u5de6\u503c\u624d\u9700\u8981\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include #include // for std::unique_ptr #include // for std::move class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } friend std :: ostream & operator << ( std :: ostream & out , const Resource & res ) { out << \"I am a resource\" ; return out ; } }; void takeOwnership ( std :: unique_ptr < Resource > res ) { if ( res ) std :: cout << * res << '\\n' ; } // the Resource is destroyed here int main () { auto ptr { std :: make_unique < Resource > () }; // takeOwnership(ptr); // This doesn't work, need to use move semantics takeOwnership ( std :: move ( ptr )); // ok: \u4f7f\u7528\u79fb\u52a8\u8bed\u4e49 std :: cout << \"Ending program \\n \" ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 3 4 Resource acquired I am a resource Resource destroyed Ending program \u6ce8\u610f\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c Resource \u7684\u6240\u6709\u6743\u88ab\u8f6c\u79fb\u7ed9\u4e86 takeOwnership() \uff0c\u6240\u4ee5 Resource \u4f1a\u5728 takeOwnership() \u51fd\u6570\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\uff0c\u800c\u4e0d\u662f main() \u3002 \u4f46\u662f\uff0c\u5927\u591a\u6570\u65f6\u5019\uff0c\u6211\u4eec\u4e0d\u4f1a\u5e0c\u671b\u67d0\u4e2a\u51fd\u6570\u62e5\u6709\u8d44\u6e90\u7684\u6240\u6709\u6743\u3002\u5c3d\u7ba1\u4f60\u53ef\u4ee5 \u6309\u5f15\u7528\u4f20\u9012 std::unique_ptr (\u8fd9\u6837\u4e00\u6765\u51fd\u6570\u5c31\u65e0\u9700\u8003\u8651\u6240\u6709\u6743\u76f4\u63a5\u4f7f\u7528\u5bf9\u8c61\u672c\u8eab)\uff0c\u4f46\u662f\u8fd9\u79cd\u505a\u6cd5\u53ea\u6709\u5728\u51fd\u6570\u53ef\u80fd\u9700\u8981\u6539\u53d8\u88ab\u7ba1\u7406\u7684\u5bf9\u8c61\u65f6\u624d\u4f7f\u7528\u3002 \u76f8\u53cd\uff0c\u6700\u597d\u662f \u76f4\u63a5\u4f20\u9012\u8d44\u6e90\u672c\u8eab (\u901a\u8fc7\u6307\u9488\u6216\u5f15\u7528\uff0c\u53d6\u51b3\u4e8enull\u662f\u5426\u662f\u6709\u6548\u53c2\u6570)\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u51fd\u6570\u5c31\u65e0\u9700\u5173\u5fc3\u8d44\u6e90\u662f\u5982\u4f55\u88ab\u7ba1\u7406\u7684\u4e86\u3002\u8981\u4ece std::unique_ptr \u4e2d\u83b7\u53d6\u539f\u59cb\u8d44\u6e90\u6307\u9488\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 get() \u6210\u5458\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include // for std::unique_ptr #include class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } friend std :: ostream & operator << ( std :: ostream & out , const Resource & res ) { out << \"I am a resource\" ; return out ; } }; // \u51fd\u6570\u503c\u4f1a\u4f7f\u7528\u8d44\u6e90\uff0c\u6240\u4ee5\u5b83\u53ea\u9700\u8981\u83b7\u53d6\u6307\u5411\u8d44\u6e90\u672c\u8eab\u7684\u6307\u9488\uff0c\u800c\u4e0d\u9700\u8981\u83b7\u53d6\u5b8c\u6574\u7684 std::unique_ptr \u7684\u5f15\u7528 void useResource ( Resource * res ) { if ( res ) std :: cout << * res << '\\n' ; else std :: cout << \"No resource \\n \" ; } int main () { auto ptr { std :: make_unique < Resource > () }; useResource ( ptr . get ()); // note: \u901a\u8fc7 get() \u83b7\u53d6\u88ab\u7ba1\u7406\u7684\u8d44\u6e90 std :: cout << \"Ending program \\n \" ; return 0 ; } // The Resource is destroyed here \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 2 3 4 Resource acquired I am a resource Ending program Resource destroyed","title":"\u4f20\u9012 std::unique_ptr \u7ed9\u51fd\u6570"},{"location":"M-6-std-unique-ptr/#stdunique_ptr_4","text":"\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5c06 std::unique_ptr \u4f5c\u4e3a\u4e00\u4e2a\u7c7b\u6210\u5458\uff08\u5f62\u6210 \u7ec4\u5408\u5173\u7cfb \u5173\u7cfb\uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4f60\u5c31\u4e0d\u9700\u8981\u5728\u7c7b\u7684\u6790\u6784\u51fd\u6570\u4e2d\u5220\u9664\u8be5\u52a8\u5185\u5b58\uff0c\u56e0\u4e3a std::unique_ptr \u4f1a\u5728\u7c7b\u5bf9\u8c61\u9500\u6bc1\u65f6\u81ea\u52a8\u9500\u6bc1\u3002 \u4f46\u662f\uff0c\u5982\u679c\u7c7b\u6ca1\u6709\u88ab\u6b63\u786e\u5730\u9500\u6bc1\u7684\u8bdd\uff08\u4f8b\u5982\uff0c\u52a8\u6001\u5206\u914d\u4e86\u5185\u5b58\u4f46\u6ca1\u6709\u88ab\u91ca\u653e\uff09\uff0c\u4e8e\u662f std::unique_ptr \u6210\u5458\u4e5f\u4e0d\u4f1a\u88ab\u9500\u6bc1\uff0c\u6240\u4ee5\u5b83\u7ba1\u7406\u7684\u8d44\u6e90\u4e5f\u4e0d\u4f1a\u88ab\u91ca\u653e\u3002","title":"std::unique_ptr \u548c\u7c7b"},{"location":"M-6-std-unique-ptr/#stdunique_ptr_5","text":"std::unique_ptrs \u6709\u4e24\u79cd\u5178\u578b\u7684\u8bef\u7528\uff0c\u4f46\u662f\u5b83\u4eec\u5176\u5b9e\u5f88\u5bb9\u6613\u907f\u514d\u3002\u9996\u5148\uff0c\u4e0d\u8981\u8ba9\u591a\u4e2a\u7c7b\u7ba1\u7406\u540c\u4e00\u4e2a\u8d44\u6e90\uff0c\u4f8b\u5982\uff1a 1 2 3 Resource * res { new Resource () }; std :: unique_ptr < Resource > res1 { res }; std :: unique_ptr < Resource > res2 { res }; \u5c3d\u7ba1\u8bed\u6cd5\u4e0a\u5b8c\u5168\u6b63\u786e\uff0c\u4f46\u662f\u6700\u7ec8\u7684\u7ed3\u679c\u662f res1 \u548c res2 \u90fd\u4f1a\u53bb\u5220\u9664\u8d44\u6e90 Resource \uff0c\u8fdb\u800c\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u7b2c\u4e8c\uff0c\u4e0d\u8981\u624b\u52a8\u91ca\u653e\u5df2\u7ecf\u5728 std::unique_ptr \u7ba1\u7406\u4e0b\u7684\u8d44\u6e90\uff1a 1 2 3 Resource * res { new Resource () }; std :: unique_ptr < Resource > res1 { res }; delete res ; \u8fd9\u4e48\u505a\u7684\u8bdd\uff0c std::unique_ptr \u5c31\u4f1a\u518d\u6b21\u5220\u9664\u8fd9\u4e2a\u5df2\u7ecf\u88ab\u91ca\u653e\u7684\u8d44\u6e90\uff0c\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4f7f\u7528 std::make_unique() \u53ef\u4ee5\u907f\u514d\u4e0a\u8ff0\u4e24\u4e2a\u95ee\u9898\u3002","title":"std::unique_ptr \u7684\u8bef\u7528"},{"location":"M-7-std-shared-ptr/","tags":["shared_ptr","C++11","C++14","C++17","C++20"],"text":"Key Takeaway \u5982\u679c\u4f60\u9700\u8981\u4e3a\u76f8\u540c\u7684\u8d44\u6e90\u521b\u5efa\u989d\u5916\u7684 std::shared_ptr \uff0c\u8bf7\u4ece\u5df2\u6709\u7684 std::shared_ptr \u590d\u5236\u4e00\u4efd\u3002 std::unique_ptr \u88ab\u8bbe\u8ba1\u51fa\u6765\u72ec\u5360\u5b83\u6240\u7ba1\u7406\u7684\u8d44\u6e90\uff0c\u4e0e\u6b64\u4e0d\u540c\u7684\u662f std::shared_ptr \u5219\u662f\u4e3a\u4e86\u89e3\u51b3\u591a\u4e2a\u6307\u9488\u5171\u540c\u7ba1\u7406\u540c\u4e00\u4e2a\u8d44\u6e90\u7684\u60c5\u51b5\u3002 \u8fd9\u4e5f\u610f\u5473\u7740\uff0c\u591a\u4e2a std::shared_ptr \u6307\u5411\u540c\u4e00\u4e2a\u8d44\u6e90\u662f\u53ef\u4ee5\u7684\u3002 std::shared_ptr \u5185\u90e8\u4f1a\u81ea\u52a8\u8ffd\u8e2a\u5f53\u524d\u5171\u4eab\u8be5\u8d44\u6e90\u7684 std::shared_ptr \u7684\u4e2a\u6570\u3002\u53ea\u6709\u8fd8\u6709\u4e00\u4e2a std::shared_ptr \u8fd8\u6307\u5411\u8d44\u6e90\uff0c\u8be5\u8d44\u6e90\u5c31\u4e0d\u4f1a\u88ab\u91ca\u653e\uff0c\u5373\u4f7f\u4efb\u610f\u4e00\u4e2a std::shared_ptr \u88ab\u9500\u6bc1\u3002\u5f53\u6700\u540e\u4e00\u4e2a\u6307\u5411\u8be5\u8d44\u6e90\u7684 std::shared_ptr \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff08\u6216\u4e0d\u518d\u6307\u5411\u8be5\u8d44\u6e90\uff09\uff0c\u8d44\u6e90\u624d\u4f1a\u88ab\u91ca\u653e\u3002 \u548c std::unique_ptr \u4e00\u6837 std::shared_ptr \u4e5f\u88ab\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include // for std::shared_ptr class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { // \u5206\u914d\u4e00\u4e2a Resource \u5bf9\u8c61\u5e76\u8ba9 std::shared_ptr \u62e5\u6709\u5b83 Resource * res { new Resource }; std :: shared_ptr < Resource > ptr1 { res }; { std :: shared_ptr < Resource > ptr2 { ptr1 }; // \u521b\u5efa\u53e6\u5916\u4e00\u4e2a std::shared_ptr \u4e5f\u6307\u5411\u8be5\u8d44\u6e90 std :: cout << \"Killing one shared pointer \\n \" ; } // ptr2 \u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u4ec0\u4e48\u90fd\u6ca1\u6709\u53d1\u751f std :: cout << \"Killing another shared pointer \\n \" ; return 0 ; } // ptr1 \u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u8d44\u6e90\u88ab\u91ca\u653e \u6253\u5370\uff1a 1 2 3 4 Resource acquired Killing one shared pointer Killing another shared pointer Resource destroyed \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u52a8\u6001 Resource \u5bf9\u8c61\uff0c\u7136\u540e\u5c06 std::shared_ptr \u7c7b\u578b\u7684 ptr1 \u6307\u5411\u5b83\uff0c\u5bf9\u8d44\u6e90\u8fdb\u884c\u7ba1\u7406\u3002\u5728\u5d4c\u5957\u7684\u8bed\u53e5\u5757\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u521b\u5efa\u4e86\u7b2c\u4e8c\u4e2a std::shared_ptr ( ptr2 )\u4e5f\u6307\u5411\u540c\u4e00\u4e2a Resource \u3002\u5f53 ptr2 \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c Resource \u5e76\u6ca1\u6709\u88ab\u91ca\u653e\uff0c\u56e0\u4e3a ptr1 \u4ecd\u7136\u6307\u5411\u8be5\u8d44\u6e90\u3002\u5f53 ptr1 \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c ptr1 \u4f1a\u610f\u8bc6\u5230\u6b64\u65f6\u6ca1\u6709 std::shared_ptr \u7ba1\u7406\u8be5\u8d44\u6e90\u4e86\uff0c\u6240\u4ee5\u5b83\u4f1a\u91ca\u653e Resource \u3002 \u6ce8\u610f\uff0c\u6211\u4eec\u662f\u4ece\u7b2c\u4e00\u4e2a\u6307\u9488\u521b\u5efa\u7684\u7b2c\u4e8c\u4e2a\u6307\u9488\uff0c\u8fd9\u4e00\u70b9\u975e\u5e38\u91cd\u8981\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include // for std::shared_ptr class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { Resource * res { new Resource }; std :: shared_ptr < Resource > ptr1 { res }; { std :: shared_ptr < Resource > ptr2 { res }; // \u76f4\u63a5\u901a\u8fc7res\u521b\u5efa ptr2 std :: cout << \"Killing one shared pointer \\n \" ; } // ptr2 \u79bb\u5f00\u4f5c\u7528\u57df Resource \u88ab\u9500\u6bc1 std :: cout << \"Killing another shared pointer \\n \" ; return 0 ; } // ptr1 \u79bb\u5f00\u4f5c\u7528\u57df\uff0cResource \u518d\u6b21\u88ab\u9500\u6bc1 \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 3 4 5 Resource acquired Killing one shared pointer Resource destroyed Killing another shared pointer Resource destroyed \u7d27\u63a5\u7740\u5c31\u5d29\u6e83\u4e86\uff08\u81f3\u5c11\u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\u4f1a\u5d29\u6e83\uff09\u3002 \u548c\u4e0a\u4e00\u4e2a\u7a0b\u5e8f\u4e0d\u540c\u7684\u662f\uff0c\u8fd9\u91cc\u521b\u5efa\u7684\u4e24\u4e2a std::shared_ptr \u662f\u76f8\u4e92\u72ec\u7acb\u7684\u3002\u5176\u7ed3\u679c\u5c31\u662f\u5b83\u4eec\u5e76\u4e0d\u77e5\u9053\u5bf9\u65b9\u548c\u81ea\u5df1\u6307\u5411\u4e86\u540c\u4e00\u4e2a Resource \uff0c\u5b83\u4eec\u4e5f\u4e92\u76f8\u4e0d\u77e5\u9053\u5bf9\u65b9\u7684\u5b58\u5728\u3002\u5f53 ptr2 \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c\u5b83\u8ba4\u4e3a\u53ea\u6709\u81ea\u5df1\u5728\u7ba1\u7406\u8be5\u8d44\u6e90\uff0c\u6240\u4ee5\u5c31\u4f1a\u91ca\u653e\u5b83\u3002\u800c\u5f53 ptr1 \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c\u5b83\u4e5f\u8ba4\u4e3a\u53ea\u6709\u81ea\u5df1\u5728\u7ba1\u7406\u8be5\u8d44\u6e90\uff0c\u6240\u4ee5\u4f1a\u518d\u6b21\u91ca\u653e\u3002\u95ee\u9898\u5c31\u6b64\u53d1\u751f\u4e86\uff01 \u5e78\u597d\uff0c\u8fd9\u4e2a\u95ee\u9898\u5f88\u5bb9\u6613\u907f\u514d\uff1a\u5982\u679c\u4f60\u9700\u8981\u4e3a\u4e00\u4e2a\u8d44\u6e90\u518d\u521b\u5efa\u4e00\u4e2a std::shared_ptr \uff0c\u8bf7\u4ece\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684 std::shared_ptr \u514b\u9686\u3002 \u6700\u4f73\u5b9e\u8df5 =\u5982\u679c\u4f60\u9700\u8981\u4e3a\u76f8\u540c\u7684\u8d44\u6e90\u521b\u5efa\u989d\u5916\u7684 std::shared_ptr \uff0c\u8bf7\u4ece\u5df2\u6709\u7684 std::shared_ptr \u590d\u5236\u4e00\u4efd\u3002 std::make_shared \u7c7b\u4f3cC++14\u4e2d\u7684 std::make_unique() \u53ef\u4ee5\u7528\u6765\u521b\u5efa std::unique_ptr \uff0c std::make_shared() \u4e5f\u53ef\u4ee5\uff08\u5e94\u8be5\uff09\u88ab\u7528\u6765\u521b\u5efa std::shared_ptr \u3002 std::make_shared() \u53ef\u4ee5\u5728 C++11 \u4e2d\u4f7f\u7528\u3002 \u8fd9\u4e2a\u4f8b\u5b50\u4f7f\u7528 std::make_shared() \u5bf9\u539f\u6765\u7684\u7a0b\u5e8f\u8fdb\u884c\u4e86\u4fee\u6539\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include // for std::shared_ptr class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { // \u5206\u914d\u4e00\u4e2a Resource \u5bf9\u8c61\u5e76\u5c06\u5176\u4ea4\u7ed9 std::shared_ptr \u7ba1\u7406 auto ptr1 { std :: make_shared < Resource > () }; { auto ptr2 { ptr1 }; // \u590d\u5236 ptr1 \u521b\u5efa ptr2 std :: cout << \"Killing one shared pointer \\n \" ; } // ptr2 \u79bb\u5f00\u4f5c\u7528\u57df\u4f46\u662f\u4ec0\u4e48\u90fd\u6ca1\u6709\u53d1\u751f std :: cout << \"Killing another shared pointer \\n \" ; return 0 ; } // ptr1 \u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u8d44\u6e90\u88ab\u9500\u6bc1 \u4f7f\u7528 std::make_shared() \u7684\u7406\u7531\u548c\u4f7f\u7528 std::make_unique() \u7684\u7406\u7531\u7c7b\u4f3c\u2014\u2014 std::make_shared() \u66f4\u7b80\u6d01\u4e5f\u66f4\u5b89\u5168(\u4f7f\u7528\u8be5\u65b9\u6cd5\u7684\u60c5\u51b5\u4e0b\uff0c\u4e0d\u53ef\u80fd\u521b\u5efa\u4e24\u4e2a\u6307\u5411\u5934\u4e00\u4e2a\u8d44\u6e90\u7684 std::shared_ptr )\u3002 std::make_shared() \u6548\u7387\u4e5f\u66f4\u9ad8\uff0c\u5b83\u4f1a\u8ddf\u8e2a\u6307\u5411\u7279\u5b9a\u8d44\u6e90\u7684\u6307\u9488\u7684\u4e2a\u6570\u3002 std::shared_ptr \u5185\u5e55 \u548c std::unique_ptr \u5185\u90e8\u4f7f\u7528\u4e86\u4e00\u4e2a\u6307\u9488\u4e0d\u540c\uff0c std::shared_ptr \u5185\u90e8\u4f7f\u7528\u4e86\u4e24\u4e2a\u6307\u9488\u3002\u5176\u4e2d\u4e00\u4e2a\u6307\u9488\u6307\u5411\u88ab\u7ba1\u7406\u7684\u8d44\u6e90\u3002\u53e6\u5916\u4e00\u4e2a\u6307\u9488\u5219\u6307\u5411\u4e00\u4e2a\u201c\u63a7\u5236\u5757\u201d\uff0c\u63a7\u5236\u5757\u662f\u4e00\u4e2a\u52a8\u6001\u5206\u914d\u7684\u5bf9\u8c61\uff0c\u7528\u4e8e\u8ffd\u8e2a\u4e00\u7cfb\u5217\u7684\u4fe1\u606f\uff0c\u5176\u4e2d\u5c31\u5305\u62ec\u6709\u591a\u5c11 std::shared_ptr \u6307\u5411\u4e86\u8be5\u5bf9\u8c61 \u3002\u5f53 std::shared_ptr \u901a\u8fc7 std::shared_ptr \u6784\u9020\u51fd\u6570\u521b\u5efa\u65f6\uff0c\u88ab\u7ba1\u7406\u5bf9\u8c61\u7684\u5185\u5b58\uff08\u901a\u5e38\u662f\u4f20\u5165\u7684\uff09\u548c\u63a7\u5236\u5757\uff08\u901a\u5e38\u7531\u6784\u9020\u51fd\u6570\u521b\u5efa\uff09\u7684\u5185\u5b58\u4f1a\u88ab\u5206\u522b\u5206\u914d\u3002 \u4f46\u662f\uff0c\u5f53\u4f7f\u7528 std::make_shared() \u521b\u5efa\u65f6\uff0c\u4e24\u4e2a\u5185\u5b58\u7684\u5206\u914d\u53ef\u4ee5\u88ab\u4f18\u5316\u4e3a\u4e00\u6b21\u5185\u5b58\u5206\u914d\uff0c\u4ece\u800c\u63d0\u5347\u6027\u80fd\u3002 \u8fd9\u4e5f\u89e3\u91ca\u4e86\u4e3a\u4ec0\u4e48\u5206\u914d\u521b\u5efa\u4e24\u4e2a\u6307\u5411\u76f8\u540c\u8d44\u6e90\u7684 std::shared_ptr \u65f6\u4f1a\u6709\u95ee\u9898\u3002\u6bcf\u4e2a std::shared_ptr \u90fd\u4f1a\u6709\u4e00\u4e2a\u6307\u5411\u8be5\u8d44\u6e90\u7684\u6307\u9488\uff0c\u4f46\u540c\u65f6\uff0c\u6bcf\u4e2a std::shared_ptr \u4e5f\u90fd\u4f1a\u72ec\u7acb\u5206\u914d\u81ea\u5df1\u7684\u63a7\u5236\u5757\uff0c\u56e0\u6b64\u63a7\u5236\u5757\u4e2d\u90fd\u53ea\u4f1a\u663e\u793a\u6709\u4e00\u4e2a\u6307\u9488\u6307\u5411\u88ab\u7ba1\u7406\u7684\u8d44\u6e90\u3002\u5f53 std::shared_ptr \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c\u5b83\u5c31\u4f1a\u91ca\u653e\u8d44\u6e90\uff0c\u800c\u4e0d\u4f1a\u610f\u8bc6\u5230\u8fd8\u6709\u5176\u4ed6\u7684 std::shared_ptr \u4e5f\u5728\u7ba1\u7406\u7740\u8be5\u8d44\u6e90\u3002 \u4f46\u662f\uff0c\u5f53 std::shared_ptr \u901a\u8fc7 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u88ab\u521b\u5efa\u65f6\uff0c\u63a7\u5236\u5757\u4f1a\u88ab\u6b63\u786e\u5730\u66f4\u65b0\uff0c\u4f7f\u5176\u80fd\u591f\u663e\u793a\u53c8\u6709\u53e6\u5916\u4e00\u4e2a std::shared_ptr \u5f00\u59cb\u7ba1\u7406\u8fd9\u4e2a\u8d44\u6e90\u4e86\u3002 std::shared_ptr \u53ef\u4ee5\u901a\u8fc7 std::unique_ptr \u521b\u5efa std::unique_ptr \u53ef\u4ee5\u901a\u8fc7\u7279\u6b8a\u7684\u6784\u9020\u51fd\u6570\uff08\u63a5\u53d7\u4e00\u4e2a std::unique_ptr \u53f3\u503c\uff09\u88ab\u8f6c\u6362\u4e3a std::shared_ptr \u3002 std::unique_ptr \u7684\u5185\u5bb9\u4f1a\u88ab\u79fb\u52a8\u5230 std::shared_ptr \u3002 \u4f46\u662f\uff0c std::shared_ptr \u5e76\u4e0d\u80fd\u88ab\u5b89\u5168\u5730\u8f6c\u6362\u4e3a std::unique_ptr \u3002\u8fd9\u610f\u5473\u7740\u5f53\u4f60\u521b\u5efa\u7684\u51fd\u6570\u9700\u8981\u8fd4\u56de\u4e00\u4e2a\u667a\u80fd\u6307\u9488\u65f6\uff0c\u6240\u4ee5\u4f60\u6700\u597d\u8fd4\u56de std::unique_ptr \u5e76\u5c06\u5176\u8d4b\u503c\u7ed9 std::shared_ptr \u3002 std::shared_ptr \u7684\u9677\u9631 std::shared_ptr \u4e5f\u5b58\u5728 std::unique_ptr \u6240\u5177\u6709\u7684\u4e00\u4e9b\u95ee\u9898\u2014\u2014\u5982\u679c std::shared_ptr \u6ca1\u6709\u88ab\u6b63\u5e38\u5730\u9500\u6bc1\uff08\u53ef\u80fd\u662f\u56e0\u4e3a\u52a8\u6001\u5185\u5b58\u6ca1\u6709\u88ab\u91ca\u653e\u6216\u90e8\u5206\u5bf9\u8c61\u6ca1\u6709\u88ab\u91ca\u653e\uff09\u5219\u5176\u7ba1\u7406\u7684\u8d44\u6e90\u4e5f\u4e0d\u4f1a\u88ab\u91ca\u653e\u3002\u4f7f\u7528 std::unique_ptr \u65f6\uff0c\u4f60\u53ea\u9700\u8981\u5173\u5fc3\u4e00\u4e2a\u667a\u80fd\u6307\u9488\u662f\u5426\u88ab\u6b63\u5e38\u7684\u9500\u6bc1\u3002\u4f46\u662f\u5728\u4f7f\u7528 std::shared_ptr \u65f6\uff0c\u4f60\u9700\u8981\u5173\u6ce8\u7ba1\u7406\u8be5\u5bf9\u8c61\u7684\u6240\u6709\u6307\u9488\u3002\u5982\u679c\u5b83\u4eec\u4e2d\u4efb\u4f55\u4e00\u4e2a\u6ca1\u6709\u88ab\u9500\u6bc1\uff0c\u5219\u8d44\u6e90\u4e5f\u4e0d\u4f1a\u88ab\u6b63\u5e38\u9500\u6bc1\u3002 std::shared_ptr \u548c\u6570\u7ec4 \u5728 C++17 \u53ca\u4e4b\u524d\u7684\u7248\u672c\u4e2d\uff0c std::shared_ptr \u5e76\u4e0d\u652f\u6301\u7ba1\u7406\u6570\u7ec4\uff0c\u800c\u4e14\u4e5f\u4e0d\u5e94\u8be5\u88ab\u7528\u6765\u7ba1\u7406C\u8bed\u8a00\u98ce\u683c\u7684\u6570\u7ec4\u3002\u5728 C++20 \u4e2d std::shared_ptr \u5df2\u7ecf\u53ef\u4ee5\u652f\u6301\u6570\u7ec4\u4e86\u3002 \u5c0f\u7ed3 \u8bbe\u8ba1 std::shared_ptr \u7684\u521d\u8877\uff0c\u662f\u4e3a\u4e86\u652f\u6301\u591a\u4e2a\u6307\u9488\u80fd\u591f\u5171\u540c\u7ba1\u7406\u4e00\u4e2a\u8d44\u6e90\u3002\u5f53\u6700\u540e\u4e00\u4e2a std::shared_ptr \u88ab\u9500\u6bc1\u65f6\uff0c\u88ab\u7ba1\u7406\u7684\u8d44\u6e90\u4e5f\u5c31\u4f1a\u88ab\u9500\u6bc1\u4e86\u3002","title":"M.7 \u2014 std::shared_ptr"},{"location":"M-7-std-shared-ptr/#stdmake_shared","text":"\u7c7b\u4f3cC++14\u4e2d\u7684 std::make_unique() \u53ef\u4ee5\u7528\u6765\u521b\u5efa std::unique_ptr \uff0c std::make_shared() \u4e5f\u53ef\u4ee5\uff08\u5e94\u8be5\uff09\u88ab\u7528\u6765\u521b\u5efa std::shared_ptr \u3002 std::make_shared() \u53ef\u4ee5\u5728 C++11 \u4e2d\u4f7f\u7528\u3002 \u8fd9\u4e2a\u4f8b\u5b50\u4f7f\u7528 std::make_shared() \u5bf9\u539f\u6765\u7684\u7a0b\u5e8f\u8fdb\u884c\u4e86\u4fee\u6539\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include // for std::shared_ptr class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { // \u5206\u914d\u4e00\u4e2a Resource \u5bf9\u8c61\u5e76\u5c06\u5176\u4ea4\u7ed9 std::shared_ptr \u7ba1\u7406 auto ptr1 { std :: make_shared < Resource > () }; { auto ptr2 { ptr1 }; // \u590d\u5236 ptr1 \u521b\u5efa ptr2 std :: cout << \"Killing one shared pointer \\n \" ; } // ptr2 \u79bb\u5f00\u4f5c\u7528\u57df\u4f46\u662f\u4ec0\u4e48\u90fd\u6ca1\u6709\u53d1\u751f std :: cout << \"Killing another shared pointer \\n \" ; return 0 ; } // ptr1 \u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u8d44\u6e90\u88ab\u9500\u6bc1 \u4f7f\u7528 std::make_shared() \u7684\u7406\u7531\u548c\u4f7f\u7528 std::make_unique() \u7684\u7406\u7531\u7c7b\u4f3c\u2014\u2014 std::make_shared() \u66f4\u7b80\u6d01\u4e5f\u66f4\u5b89\u5168(\u4f7f\u7528\u8be5\u65b9\u6cd5\u7684\u60c5\u51b5\u4e0b\uff0c\u4e0d\u53ef\u80fd\u521b\u5efa\u4e24\u4e2a\u6307\u5411\u5934\u4e00\u4e2a\u8d44\u6e90\u7684 std::shared_ptr )\u3002 std::make_shared() \u6548\u7387\u4e5f\u66f4\u9ad8\uff0c\u5b83\u4f1a\u8ddf\u8e2a\u6307\u5411\u7279\u5b9a\u8d44\u6e90\u7684\u6307\u9488\u7684\u4e2a\u6570\u3002","title":"std::make_shared"},{"location":"M-7-std-shared-ptr/#stdshared_ptr","text":"\u548c std::unique_ptr \u5185\u90e8\u4f7f\u7528\u4e86\u4e00\u4e2a\u6307\u9488\u4e0d\u540c\uff0c std::shared_ptr \u5185\u90e8\u4f7f\u7528\u4e86\u4e24\u4e2a\u6307\u9488\u3002\u5176\u4e2d\u4e00\u4e2a\u6307\u9488\u6307\u5411\u88ab\u7ba1\u7406\u7684\u8d44\u6e90\u3002\u53e6\u5916\u4e00\u4e2a\u6307\u9488\u5219\u6307\u5411\u4e00\u4e2a\u201c\u63a7\u5236\u5757\u201d\uff0c\u63a7\u5236\u5757\u662f\u4e00\u4e2a\u52a8\u6001\u5206\u914d\u7684\u5bf9\u8c61\uff0c\u7528\u4e8e\u8ffd\u8e2a\u4e00\u7cfb\u5217\u7684\u4fe1\u606f\uff0c\u5176\u4e2d\u5c31\u5305\u62ec\u6709\u591a\u5c11 std::shared_ptr \u6307\u5411\u4e86\u8be5\u5bf9\u8c61 \u3002\u5f53 std::shared_ptr \u901a\u8fc7 std::shared_ptr \u6784\u9020\u51fd\u6570\u521b\u5efa\u65f6\uff0c\u88ab\u7ba1\u7406\u5bf9\u8c61\u7684\u5185\u5b58\uff08\u901a\u5e38\u662f\u4f20\u5165\u7684\uff09\u548c\u63a7\u5236\u5757\uff08\u901a\u5e38\u7531\u6784\u9020\u51fd\u6570\u521b\u5efa\uff09\u7684\u5185\u5b58\u4f1a\u88ab\u5206\u522b\u5206\u914d\u3002 \u4f46\u662f\uff0c\u5f53\u4f7f\u7528 std::make_shared() \u521b\u5efa\u65f6\uff0c\u4e24\u4e2a\u5185\u5b58\u7684\u5206\u914d\u53ef\u4ee5\u88ab\u4f18\u5316\u4e3a\u4e00\u6b21\u5185\u5b58\u5206\u914d\uff0c\u4ece\u800c\u63d0\u5347\u6027\u80fd\u3002 \u8fd9\u4e5f\u89e3\u91ca\u4e86\u4e3a\u4ec0\u4e48\u5206\u914d\u521b\u5efa\u4e24\u4e2a\u6307\u5411\u76f8\u540c\u8d44\u6e90\u7684 std::shared_ptr \u65f6\u4f1a\u6709\u95ee\u9898\u3002\u6bcf\u4e2a std::shared_ptr \u90fd\u4f1a\u6709\u4e00\u4e2a\u6307\u5411\u8be5\u8d44\u6e90\u7684\u6307\u9488\uff0c\u4f46\u540c\u65f6\uff0c\u6bcf\u4e2a std::shared_ptr \u4e5f\u90fd\u4f1a\u72ec\u7acb\u5206\u914d\u81ea\u5df1\u7684\u63a7\u5236\u5757\uff0c\u56e0\u6b64\u63a7\u5236\u5757\u4e2d\u90fd\u53ea\u4f1a\u663e\u793a\u6709\u4e00\u4e2a\u6307\u9488\u6307\u5411\u88ab\u7ba1\u7406\u7684\u8d44\u6e90\u3002\u5f53 std::shared_ptr \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c\u5b83\u5c31\u4f1a\u91ca\u653e\u8d44\u6e90\uff0c\u800c\u4e0d\u4f1a\u610f\u8bc6\u5230\u8fd8\u6709\u5176\u4ed6\u7684 std::shared_ptr \u4e5f\u5728\u7ba1\u7406\u7740\u8be5\u8d44\u6e90\u3002 \u4f46\u662f\uff0c\u5f53 std::shared_ptr \u901a\u8fc7 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u88ab\u521b\u5efa\u65f6\uff0c\u63a7\u5236\u5757\u4f1a\u88ab\u6b63\u786e\u5730\u66f4\u65b0\uff0c\u4f7f\u5176\u80fd\u591f\u663e\u793a\u53c8\u6709\u53e6\u5916\u4e00\u4e2a std::shared_ptr \u5f00\u59cb\u7ba1\u7406\u8fd9\u4e2a\u8d44\u6e90\u4e86\u3002","title":"std::shared_ptr \u5185\u5e55"},{"location":"M-7-std-shared-ptr/#stdshared_ptr-stdunique_ptr","text":"std::unique_ptr \u53ef\u4ee5\u901a\u8fc7\u7279\u6b8a\u7684\u6784\u9020\u51fd\u6570\uff08\u63a5\u53d7\u4e00\u4e2a std::unique_ptr \u53f3\u503c\uff09\u88ab\u8f6c\u6362\u4e3a std::shared_ptr \u3002 std::unique_ptr \u7684\u5185\u5bb9\u4f1a\u88ab\u79fb\u52a8\u5230 std::shared_ptr \u3002 \u4f46\u662f\uff0c std::shared_ptr \u5e76\u4e0d\u80fd\u88ab\u5b89\u5168\u5730\u8f6c\u6362\u4e3a std::unique_ptr \u3002\u8fd9\u610f\u5473\u7740\u5f53\u4f60\u521b\u5efa\u7684\u51fd\u6570\u9700\u8981\u8fd4\u56de\u4e00\u4e2a\u667a\u80fd\u6307\u9488\u65f6\uff0c\u6240\u4ee5\u4f60\u6700\u597d\u8fd4\u56de std::unique_ptr \u5e76\u5c06\u5176\u8d4b\u503c\u7ed9 std::shared_ptr \u3002","title":"std::shared_ptr \u53ef\u4ee5\u901a\u8fc7 std::unique_ptr \u521b\u5efa"},{"location":"M-7-std-shared-ptr/#stdshared_ptr_1","text":"std::shared_ptr \u4e5f\u5b58\u5728 std::unique_ptr \u6240\u5177\u6709\u7684\u4e00\u4e9b\u95ee\u9898\u2014\u2014\u5982\u679c std::shared_ptr \u6ca1\u6709\u88ab\u6b63\u5e38\u5730\u9500\u6bc1\uff08\u53ef\u80fd\u662f\u56e0\u4e3a\u52a8\u6001\u5185\u5b58\u6ca1\u6709\u88ab\u91ca\u653e\u6216\u90e8\u5206\u5bf9\u8c61\u6ca1\u6709\u88ab\u91ca\u653e\uff09\u5219\u5176\u7ba1\u7406\u7684\u8d44\u6e90\u4e5f\u4e0d\u4f1a\u88ab\u91ca\u653e\u3002\u4f7f\u7528 std::unique_ptr \u65f6\uff0c\u4f60\u53ea\u9700\u8981\u5173\u5fc3\u4e00\u4e2a\u667a\u80fd\u6307\u9488\u662f\u5426\u88ab\u6b63\u5e38\u7684\u9500\u6bc1\u3002\u4f46\u662f\u5728\u4f7f\u7528 std::shared_ptr \u65f6\uff0c\u4f60\u9700\u8981\u5173\u6ce8\u7ba1\u7406\u8be5\u5bf9\u8c61\u7684\u6240\u6709\u6307\u9488\u3002\u5982\u679c\u5b83\u4eec\u4e2d\u4efb\u4f55\u4e00\u4e2a\u6ca1\u6709\u88ab\u9500\u6bc1\uff0c\u5219\u8d44\u6e90\u4e5f\u4e0d\u4f1a\u88ab\u6b63\u5e38\u9500\u6bc1\u3002","title":"std::shared_ptr \u7684\u9677\u9631"},{"location":"M-7-std-shared-ptr/#stdshared_ptr_2","text":"\u5728 C++17 \u53ca\u4e4b\u524d\u7684\u7248\u672c\u4e2d\uff0c std::shared_ptr \u5e76\u4e0d\u652f\u6301\u7ba1\u7406\u6570\u7ec4\uff0c\u800c\u4e14\u4e5f\u4e0d\u5e94\u8be5\u88ab\u7528\u6765\u7ba1\u7406C\u8bed\u8a00\u98ce\u683c\u7684\u6570\u7ec4\u3002\u5728 C++20 \u4e2d std::shared_ptr \u5df2\u7ecf\u53ef\u4ee5\u652f\u6301\u6570\u7ec4\u4e86\u3002","title":"std::shared_ptr \u548c\u6570\u7ec4"},{"location":"M-7-std-shared-ptr/#_1","text":"\u8bbe\u8ba1 std::shared_ptr \u7684\u521d\u8877\uff0c\u662f\u4e3a\u4e86\u652f\u6301\u591a\u4e2a\u6307\u9488\u80fd\u591f\u5171\u540c\u7ba1\u7406\u4e00\u4e2a\u8d44\u6e90\u3002\u5f53\u6700\u540e\u4e00\u4e2a std::shared_ptr \u88ab\u9500\u6bc1\u65f6\uff0c\u88ab\u7ba1\u7406\u7684\u8d44\u6e90\u4e5f\u5c31\u4f1a\u88ab\u9500\u6bc1\u4e86\u3002","title":"\u5c0f\u7ed3"},{"location":"M-8-circular-dependency-issues-with-std-shared-ptr-and-std-weak-ptr/","tags":["shared_ptr","circular dependency","weak_ptr"],"text":"Key Takeaway In the previous lesson, we saw how std::shared_ptr allowed us to have multiple smart pointers co-owning the same resource. However, in certain cases, this can become problematic. Consider the following case, where the shared pointers in two separate objects each point at the other object: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include #include // for std::shared_ptr #include class Person { std :: string m_name ; std :: shared_ptr < Person > m_partner ; // initially created empty public : Person ( const std :: string & name ) : m_name ( name ) { std :: cout << m_name << \" created \\n \" ; } ~ Person () { std :: cout << m_name << \" destroyed \\n \" ; } friend bool partnerUp ( std :: shared_ptr < Person > & p1 , std :: shared_ptr < Person > & p2 ) { if ( ! p1 || ! p2 ) return false ; p1 -> m_partner = p2 ; p2 -> m_partner = p1 ; std :: cout << p1 -> m_name << \" is now partnered with \" << p2 -> m_name << '\\n' ; return true ; } }; int main () { auto lucy { std :: make_shared < Person > ( \"Lucy\" ) }; // create a Person named \"Lucy\" auto ricky { std :: make_shared < Person > ( \"Ricky\" ) }; // create a Person named \"Ricky\" partnerUp ( lucy , ricky ); // Make \"Lucy\" point to \"Ricky\" and vice-versa return 0 ; } COPY In the above example, we dynamically allocate two Persons, \u201cLucy\u201d and \u201cRicky\u201d using make_shared() (to ensure lucy and ricky are destroyed at the end of main()). Then we partner them up. This sets the std::shared_ptr inside \u201cLucy\u201d to point at \u201cRicky\u201d, and the std::shared_ptr inside \u201cRicky\u201d to point at \u201cLucy\u201d. Shared pointers are meant to be shared, so it\u2019s fine that both the lucy shared pointer and Rick\u2019s m_partner shared pointer both point at \u201cLucy\u201d (and vice-versa). However, this program doesn\u2019t execute as expected: 1 2 3 Lucy created Ricky created Lucy is now partnered with Ricky And that\u2019s it. No deallocations took place. Uh oh. What happened? After partnerUp() is called, there are two shared pointers pointing to \u201cRicky\u201d (ricky, and Lucy\u2019s m_partner) and two shared pointers pointing to \u201cLucy\u201d (lucy, and Ricky\u2019s m_partner). At the end of main(), the ricky shared pointer goes out of scope first. When that happens, ricky checks if there are any other shared pointers that co-own the Person \u201cRicky\u201d. There are (Lucy\u2019s m_partner). Because of this, it doesn\u2019t deallocate \u201cRicky\u201d (if it did, then Lucy\u2019s m_partner would end up as a dangling pointer). At this point, we now have one shared pointer to \u201cRicky\u201d (Lucy\u2019s m_partner) and two shared pointers to \u201cLucy\u201d (lucy, and Ricky\u2019s m_partner). Next the lucy shared pointer goes out of scope, and the same thing happens. The shared pointer lucy checks if there are any other shared pointers co-owning the Person \u201cLucy\u201d. There are (Ricky\u2019s m_partner), so \u201cLucy\u201d isn\u2019t deallocated. At this point, there is one shared pointer to \u201cLucy\u201d (Ricky\u2019s m_partner) and one shared pointer to \u201cRicky\u201d (Lucy\u2019s m_partner). Then the program ends -- and neither Person \u201cLucy\u201d or \u201cRicky\u201d have been deallocated! Essentially, \u201cLucy\u201d ends up keeping \u201cRicky\u201d from being destroyed, and \u201cRicky\u201d ends up keeping \u201cLucy\u201d from being destroyed. It turns out that this can happen any time shared pointers form a circular reference. \u5faa\u73af\u5f15\u7528 A Circular reference (also called a cyclical reference or a cycle ) is a series of references where each object references the next, and the last object references back to the first, causing a referential loop. The references do not need to be actual C++ references -- they can be pointers, unique IDs, or any other means of identifying specific objects. In the context of shared pointers, the references will be pointers. This is exactly what we see in the case above: \u201cLucy\u201d points at \u201cRicky\u201d, and \u201cRicky\u201d points at \u201cLucy\u201d. With three pointers, you\u2019d get the same thing when A points at B, B points at C, and C points at A. The practical effect of having shared pointers form a cycle is that each object ends up keeping the next object alive -- with the last object keeping the first object alive. Thus, no objects in the series can be deallocated because they all think some other object still needs it! A reductive case It turns out, this cyclical reference issue can even happen with a single std::shared_ptr -- a std::shared_ptr referencing the object that contains it is still a cycle (just a reductive one). Although it\u2019s fairly unlikely that this would ever happen in practice, we\u2019ll show you for additional comprehension: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include // for std::shared_ptr class Resource { public : std :: shared_ptr < Resource > m_ptr {}; // initially created empty Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { auto ptr1 { std :: make_shared < Resource > () }; ptr1 -> m_ptr = ptr1 ; // m_ptr is now sharing the Resource that contains it return 0 ; } COPY In the above example, when ptr1 goes out of scope, the Resource is not deallocated because the Resource\u2019s m_ptr is sharing the Resource. At that point, the only way for the Resource to be released would be to set m_ptr to something else (so nothing is sharing the Resource any longer). But we can\u2019t access m_ptr because ptr1 is out of scope, so we no longer have a way to do this. The Resource has become a memory leak. Thus, the program prints: Resource acquired and that\u2019s it. std::weak_ptr \u5230\u5e95\u662f\u4ec0\u4e48\uff1f std::weak_ptr was designed to solve the \u201ccyclical ownership\u201d problem described above. A std::weak_ptr is an observer -- it can observe and access the same object as a std::shared_ptr (or other std::weak_ptrs) but it is not considered an owner. Remember, when a std::shared pointer goes out of scope, it only considers whether other std::shared_ptr are co-owning the object. std::weak_ptr does not count! Let\u2019s solve our Person-al issue using a std::weak_ptr: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include #include // for std::shared_ptr and std::weak_ptr #include class Person { std :: string m_name ; std :: weak_ptr < Person > m_partner ; // note: This is now a std::weak_ptr public : Person ( const std :: string & name ) : m_name ( name ) { std :: cout << m_name << \" created \\n \" ; } ~ Person () { std :: cout << m_name << \" destroyed \\n \" ; } friend bool partnerUp ( std :: shared_ptr < Person > & p1 , std :: shared_ptr < Person > & p2 ) { if ( ! p1 || ! p2 ) return false ; p1 -> m_partner = p2 ; p2 -> m_partner = p1 ; std :: cout << p1 -> m_name << \" is now partnered with \" << p2 -> m_name << '\\n' ; return true ; } }; int main () { auto lucy { std :: make_shared < Person > ( \"Lucy\" ) }; auto ricky { std :: make_shared < Person > ( \"Ricky\" ) }; partnerUp ( lucy , ricky ); return 0 ; } COPY This code behaves properly: 1 2 3 4 5 Lucy created Ricky created Lucy is now partnered with Ricky Ricky destroyed Lucy destroyed Functionally, it works almost identically to the problematic example. However, now when ricky goes out of scope, it sees that there are no other std::shared_ptr pointing at \u201cRicky\u201d (the std::weak_ptr from \u201cLucy\u201d doesn\u2019t count). Therefore, it will deallocate \u201cRicky\u201d. The same occurs for lucy. \u4f7f\u7528 std::weak_ptr The downside of std::weak_ptr is that std::weak_ptr are not directly usable (they have no operator->). To use a std::weak_ptr, you must first convert it into a std::shared_ptr. Then you can use the std::shared_ptr. To convert a std::weak_ptr into a std::shared_ptr, you can use the lock() member function. Here\u2019s the above example, updated to show this off: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include #include // for std::shared_ptr and std::weak_ptr #include class Person { std :: string m_name ; std :: weak_ptr < Person > m_partner ; // note: This is now a std::weak_ptr public : Person ( const std :: string & name ) : m_name ( name ) { std :: cout << m_name << \" created \\n \" ; } ~ Person () { std :: cout << m_name << \" destroyed \\n \" ; } friend bool partnerUp ( std :: shared_ptr < Person > & p1 , std :: shared_ptr < Person > & p2 ) { if ( ! p1 || ! p2 ) return false ; p1 -> m_partner = p2 ; p2 -> m_partner = p1 ; std :: cout << p1 -> m_name << \" is now partnered with \" << p2 -> m_name << \" \\'\\n '; return true ; } const std :: shared_ptr < Person > getPartner () const { return m_partner . lock (); } // use lock() to convert weak_ptr to shared_ptr const std :: string & getName () const { return m_name ; } }; int main () { auto lucy { std :: make_shared < Person > ( \"Lucy\" ) }; auto ricky { std :: make_shared < Person > ( \"Ricky\" ) }; partnerUp ( lucy , ricky ); auto partner = ricky -> getPartner (); // get shared_ptr to Ricky's partner std :: cout << ricky -> getName () << \"'s partner is: \" << partner -> getName () << '\\n' ; return 0 ; } COPY This prints: 1 2 3 4 5 6 Lucy created Ricky created Lucy is now partnered with Ricky Ricky's partner is: Lucy Ricky destroyed Lucy destroyed We don\u2019t have to worry about circular dependencies with std::shared_ptr variable \u201cpartner\u201d since it\u2019s just a local variable inside the function. It will eventually go out of scope at the end of the function and the reference count will be decremented by 1. Dangling pointers with std::weak_ptr Because std::weak_ptr won\u2019t keep an owned resource alive, it\u2019s possible for a std::weak_ptr to be left pointing to a resource that has been deallocated by a std::shared_ptr. Such a std::weak_ptr is dangling, and using it will cause undefined behavior. Here\u2019s a simple example showing how this can happen: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 // h/t to reader Waldo for this example #include #include class Resource { public : Resource () { std :: cerr << \"Resource acquired \\n \" ; } ~ Resource () { std :: cerr << \"Resource destroyed \\n \" ; } }; auto getWeakPtr () { auto ptr { std :: make_shared < Resource > () }; // Resource acquired return std :: weak_ptr { ptr }; } // ptr goes out of scope, Resource destroyed int main () { std :: cerr << \"Getting weak_ptr... \\n \" ; auto ptr { getWeakPtr () }; // dangling std :: cerr << \"Done. \\n \" ; } COPY In the above example, inside getWeakPtr() we use std::make_shared() to create a std::shared_ptr variable named ptr that owns a Resource object. The function returns a std::weak_ptr back to the caller, which does not increment the reference count. Then because ptr is a local variable, it goes out of scope at the end of the function, which decrements the reference count to 0 and deallocates the Resource object. The returned std::weak_ptr is left dangling, pointing to a Resource that was deallocated. \u5c0f\u7ed3 std::shared_ptr can be used when you need multiple smart pointers that can co-own a resource. The resource will be deallocated when the last std::shared_ptr goes out of scope. std::weak_ptr can be used when you want a smart pointer that can see and use a shared resource, but does not participate in the ownership of that resource.","title":"M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898"},{"location":"M-8-circular-dependency-issues-with-std-shared-ptr-and-std-weak-ptr/#_1","text":"A Circular reference (also called a cyclical reference or a cycle ) is a series of references where each object references the next, and the last object references back to the first, causing a referential loop. The references do not need to be actual C++ references -- they can be pointers, unique IDs, or any other means of identifying specific objects. In the context of shared pointers, the references will be pointers. This is exactly what we see in the case above: \u201cLucy\u201d points at \u201cRicky\u201d, and \u201cRicky\u201d points at \u201cLucy\u201d. With three pointers, you\u2019d get the same thing when A points at B, B points at C, and C points at A. The practical effect of having shared pointers form a cycle is that each object ends up keeping the next object alive -- with the last object keeping the first object alive. Thus, no objects in the series can be deallocated because they all think some other object still needs it!","title":"\u5faa\u73af\u5f15\u7528"},{"location":"M-8-circular-dependency-issues-with-std-shared-ptr-and-std-weak-ptr/#a-reductive-case","text":"It turns out, this cyclical reference issue can even happen with a single std::shared_ptr -- a std::shared_ptr referencing the object that contains it is still a cycle (just a reductive one). Although it\u2019s fairly unlikely that this would ever happen in practice, we\u2019ll show you for additional comprehension: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include // for std::shared_ptr class Resource { public : std :: shared_ptr < Resource > m_ptr {}; // initially created empty Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { auto ptr1 { std :: make_shared < Resource > () }; ptr1 -> m_ptr = ptr1 ; // m_ptr is now sharing the Resource that contains it return 0 ; } COPY In the above example, when ptr1 goes out of scope, the Resource is not deallocated because the Resource\u2019s m_ptr is sharing the Resource. At that point, the only way for the Resource to be released would be to set m_ptr to something else (so nothing is sharing the Resource any longer). But we can\u2019t access m_ptr because ptr1 is out of scope, so we no longer have a way to do this. The Resource has become a memory leak. Thus, the program prints: Resource acquired and that\u2019s it.","title":"A reductive case"},{"location":"M-8-circular-dependency-issues-with-std-shared-ptr-and-std-weak-ptr/#stdweak_ptr","text":"std::weak_ptr was designed to solve the \u201ccyclical ownership\u201d problem described above. A std::weak_ptr is an observer -- it can observe and access the same object as a std::shared_ptr (or other std::weak_ptrs) but it is not considered an owner. Remember, when a std::shared pointer goes out of scope, it only considers whether other std::shared_ptr are co-owning the object. std::weak_ptr does not count! Let\u2019s solve our Person-al issue using a std::weak_ptr: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include #include // for std::shared_ptr and std::weak_ptr #include class Person { std :: string m_name ; std :: weak_ptr < Person > m_partner ; // note: This is now a std::weak_ptr public : Person ( const std :: string & name ) : m_name ( name ) { std :: cout << m_name << \" created \\n \" ; } ~ Person () { std :: cout << m_name << \" destroyed \\n \" ; } friend bool partnerUp ( std :: shared_ptr < Person > & p1 , std :: shared_ptr < Person > & p2 ) { if ( ! p1 || ! p2 ) return false ; p1 -> m_partner = p2 ; p2 -> m_partner = p1 ; std :: cout << p1 -> m_name << \" is now partnered with \" << p2 -> m_name << '\\n' ; return true ; } }; int main () { auto lucy { std :: make_shared < Person > ( \"Lucy\" ) }; auto ricky { std :: make_shared < Person > ( \"Ricky\" ) }; partnerUp ( lucy , ricky ); return 0 ; } COPY This code behaves properly: 1 2 3 4 5 Lucy created Ricky created Lucy is now partnered with Ricky Ricky destroyed Lucy destroyed Functionally, it works almost identically to the problematic example. However, now when ricky goes out of scope, it sees that there are no other std::shared_ptr pointing at \u201cRicky\u201d (the std::weak_ptr from \u201cLucy\u201d doesn\u2019t count). Therefore, it will deallocate \u201cRicky\u201d. The same occurs for lucy.","title":"std::weak_ptr \u5230\u5e95\u662f\u4ec0\u4e48\uff1f"},{"location":"M-8-circular-dependency-issues-with-std-shared-ptr-and-std-weak-ptr/#stdweak_ptr_1","text":"The downside of std::weak_ptr is that std::weak_ptr are not directly usable (they have no operator->). To use a std::weak_ptr, you must first convert it into a std::shared_ptr. Then you can use the std::shared_ptr. To convert a std::weak_ptr into a std::shared_ptr, you can use the lock() member function. Here\u2019s the above example, updated to show this off: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include #include // for std::shared_ptr and std::weak_ptr #include class Person { std :: string m_name ; std :: weak_ptr < Person > m_partner ; // note: This is now a std::weak_ptr public : Person ( const std :: string & name ) : m_name ( name ) { std :: cout << m_name << \" created \\n \" ; } ~ Person () { std :: cout << m_name << \" destroyed \\n \" ; } friend bool partnerUp ( std :: shared_ptr < Person > & p1 , std :: shared_ptr < Person > & p2 ) { if ( ! p1 || ! p2 ) return false ; p1 -> m_partner = p2 ; p2 -> m_partner = p1 ; std :: cout << p1 -> m_name << \" is now partnered with \" << p2 -> m_name << \" \\'\\n '; return true ; } const std :: shared_ptr < Person > getPartner () const { return m_partner . lock (); } // use lock() to convert weak_ptr to shared_ptr const std :: string & getName () const { return m_name ; } }; int main () { auto lucy { std :: make_shared < Person > ( \"Lucy\" ) }; auto ricky { std :: make_shared < Person > ( \"Ricky\" ) }; partnerUp ( lucy , ricky ); auto partner = ricky -> getPartner (); // get shared_ptr to Ricky's partner std :: cout << ricky -> getName () << \"'s partner is: \" << partner -> getName () << '\\n' ; return 0 ; } COPY This prints: 1 2 3 4 5 6 Lucy created Ricky created Lucy is now partnered with Ricky Ricky's partner is: Lucy Ricky destroyed Lucy destroyed We don\u2019t have to worry about circular dependencies with std::shared_ptr variable \u201cpartner\u201d since it\u2019s just a local variable inside the function. It will eventually go out of scope at the end of the function and the reference count will be decremented by 1.","title":"\u4f7f\u7528 std::weak_ptr"},{"location":"M-8-circular-dependency-issues-with-std-shared-ptr-and-std-weak-ptr/#dangling-pointers-with-stdweak_ptr","text":"Because std::weak_ptr won\u2019t keep an owned resource alive, it\u2019s possible for a std::weak_ptr to be left pointing to a resource that has been deallocated by a std::shared_ptr. Such a std::weak_ptr is dangling, and using it will cause undefined behavior. Here\u2019s a simple example showing how this can happen: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 // h/t to reader Waldo for this example #include #include class Resource { public : Resource () { std :: cerr << \"Resource acquired \\n \" ; } ~ Resource () { std :: cerr << \"Resource destroyed \\n \" ; } }; auto getWeakPtr () { auto ptr { std :: make_shared < Resource > () }; // Resource acquired return std :: weak_ptr { ptr }; } // ptr goes out of scope, Resource destroyed int main () { std :: cerr << \"Getting weak_ptr... \\n \" ; auto ptr { getWeakPtr () }; // dangling std :: cerr << \"Done. \\n \" ; } COPY In the above example, inside getWeakPtr() we use std::make_shared() to create a std::shared_ptr variable named ptr that owns a Resource object. The function returns a std::weak_ptr back to the caller, which does not increment the reference count. Then because ptr is a local variable, it goes out of scope at the end of the function, which decrements the reference count to 0 and deallocates the Resource object. The returned std::weak_ptr is left dangling, pointing to a Resource that was deallocated.","title":"Dangling pointers with std::weak_ptr"},{"location":"M-8-circular-dependency-issues-with-std-shared-ptr-and-std-weak-ptr/#_2","text":"std::shared_ptr can be used when you need multiple smart pointers that can co-own a resource. The resource will be deallocated when the last std::shared_ptr goes out of scope. std::weak_ptr can be used when you want a smart pointer that can see and use a shared resource, but does not participate in the ownership of that resource.","title":"\u5c0f\u7ed3"},{"location":"M-x-chapter-M-comprehensive-review/","tags":["summary"],"text":"\u590d\u4e60 M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb \u667a\u80fd\u6307\u9488\u7c7b \u662f\u4e00\u79cd\u7ec4\u5408\u7c7b\uff0c\u7528\u4e8e\u5bf9\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u8fdb\u884c\u7ba1\u7406\u5e76\u786e\u4fdd\u8be5\u5185\u5b58\u5728\u667a\u80fd\u6307\u9488\u6307\u5411\u7684\u5bf9\u8c61 \u79bb\u5f00\u4f5c\u7528\u57df \u540e\u88ab\u91ca\u653e\u3002 \u62f7\u8d1d\u8bed\u4e49 \u5141\u8bb8\u7c7b\u88ab\u590d\u5236\uff0c\u8be5\u8fc7\u7a0b\u4e3b\u8981\u901a\u8fc7 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u548c \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u6765\u5b8c\u6210\u3002 \u79fb\u52a8\u8bed\u4e49 \u6307\u7684\u662f\u7c7b\u5c06\u5bf9\u8c61\u7684\u6240\u6709\u6743\u8f6c\u8bd1\u7ed9\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u521b\u5efa\u4e00\u4e2a\u62f7\u8d1d\uff0c\u8be5\u8fc7\u7a0b\u4e3b\u8981\u901a\u8fc7 \u79fb\u52a8\u6784\u9020\u51fd\u6570 \u548c \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 \u6765\u5b8c\u6210\u3002 std::auto_ptr \u5df2\u7ecf\u88ab\u5f03\u7528\uff0c\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u3002 M.2 - \u53f3\u503c\u5f15\u7528 \u4e00\u4e2a \u53f3\u503c\u5f15\u7528 \u5f15\u7528\u5e94\u8be5\u4f7f\u7528\u4e00\u4e2a\u53f3\u503c\u6765\u521d\u59cb\u5316\u3002 \u53f3\u503c\u5f15\u7528\u4f7f\u7528\u53cc && \u53f7\u521b\u5efa\u3002\u7f16\u5199\u5c06\u53f3\u503c\u5f15\u7528\u4f5c\u4e3a \u5f62\u53c2 \u7684\u51fd\u6570\u662f\u53ef\u4ee5\u7684\uff0c\u4f46\u662f\u7edd\u5bf9\u4e0d\u8981\u5c06\u53f3\u503c\u5f15\u7528\u4f5c\u4e3a\u8fd4\u56de\u503c\u3002 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c \u5f53\u6211\u4eec\u6784\u5efa\u5bf9\u8c61\u6216\u5bf9\u5bf9\u8c61\u8d4b\u503c\u65f6\uff0c\u5982\u679c\u53c2\u6570\u662f \u5de6\u503c \uff0c\u5219\u552f\u4e00\u80fd\u505a\u7684\u5408\u7406\u64cd\u4f5c\u662f\u62f7\u8d1d\u8be5\u5de6\u503c\u3002\u6211\u4eec\u4e0d\u80fd\u5047\u5b9a\u6539\u53d8\u8be5\u5de6\u503c\u662f\u5b89\u5168\u7684\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u4f1a\u5728\u540e\u9762\u7684\u7a0b\u5e8f\u4e2d\u518d\u6b21\u88ab\u4f7f\u7528\u3002\u5bf9\u4e8e\u8868\u8fbe\u5f0f \u201ca = b\u201d\uff0c\u6211\u4eec\u6ca1\u6709\u7406\u7531\u671f\u671bb\u4ee5\u4efb\u4f55\u65b9\u5f0f\u88ab\u6539\u53d8\u3002 \u4f46\u662f\uff0c\u5f53\u6784\u5efa\u5bf9\u8c61\u6216\u8005\u8d4b\u503c\u5bf9\u8c61\u65f6\uff0c\u5982\u679c\u53c2\u6570\u662f \u53f3\u503c \uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u786e\u5b9a\u8be5\u53f3\u503c\u80af\u5b9a\u662f\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\u3002\u4e0e\u5176\u62f7\u8d1d\uff08\u5f00\u9500\u5927\uff09\u5b83\uff0c\u4e0d\u5982\u76f4\u63a5\u5c06\u5b83\u7684\u8d44\u6e90\u8f6c\u79fb\uff08\u5f00\u9500\u5c0f\uff09\u7ed9\u88ab\u6784\u5efa\u6216\u88ab\u8d4b\u503c\u7684\u5bf9\u8c61\u3002\u8fd9\u4e48\u505a\u662f\u5b89\u5168\u7684\uff0c\u56e0\u4e3a\u4e34\u65f6\u5bf9\u8c61\u603b\u662f\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u65f6\u5c31\u88ab\u9500\u6bc1\uff0c\u6240\u4ee5\u80af\u5b9a\u4e0d\u4f1a\u518d\u6b21\u88ab\u4f7f\u7528\u4e86\u3002 \u4f60\u53ef\u4ee5\u4f7f\u7528 delete \u5173\u952e\u5b57\u7981\u7528\u62f7\u8d1d\u8bed\u4e49\uff08\u901a\u8fc7\u5220\u9664\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26\uff09 M.4 - std::move std::move \u5141\u8bb8\u6211\u4eec\u5c06\u5de6\u503c\u5f53\u505a\u53f3\u503c\u5904\u7406\u3002\u5f53\u9700\u8981\u57fa\u4e8e\u7ec4\u7ec7\u8c03\u7528\u6216\u6fc0\u6d3b\u79fb\u52a8\u8bed\u4e49\u800c\u4e0d\u662f\u62f7\u8d1d\u8bed\u4e49\u65f6\u8fd9\u5f88\u6709\u7528\u3002 M.5 - std::move_if_noexcept \u5f53\u5bf9\u8c61\u6709\u4e00\u4e2a noexcept \u79fb\u52a8\u6784\u9020\u51fd\u6570\u65f6\uff0c std::move_if_noexcept \u4f1a\u8fd4\u56de\u4e00\u4e2a\u53ef\u79fb\u52a8\u7684\u53f3\u503c\u3002\u5426\u5219\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u53ef\u4ee5\u62f7\u8d1d\u7684\u5de6\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 noexcept \u4fee\u9970\u7b26\u548c std::move_if_noexcept \u786e\u4fdd\u53ea\u5728\u5177\u6709 \u5f3a\u5f02\u5e38\u4fdd\u8bc1 \u65f6\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\uff0c\u5176\u4ed6\u60c5\u51b5\u4e0b\u4f7f\u7528\u62f7\u8d1d\u8bed\u4e49\u3002 M.6 \u2014 std::unique_ptr \u667a\u80fd\u6307\u9488 std::unique_ptr \u5f88\u5e38\u7528\u3002\u5b83\u7528\u4e8e\u7ba1\u7406\u4e00\u4e2a\u4e0d\u53ef\u5171\u4eab\u7684\u8d44\u6e90\u3002\u5e94\u8be5\u4f7f\u7528 std::make_unique() (C++14) \u6765\u521b\u5efa std::unique_ptr \u3002 std::unique_ptr \u7981\u7528\u4e86\u62f7\u8d1d\u8bed\u4e49 M.7 \u2014 std::shared_ptr \u5f53\u591a\u4e2a\u5bf9\u8c61\u9700\u8981\u8bbf\u95ee\u540c\u4e00\u4e2a\u8d44\u6e90\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 std::shared_ptr \u3002\u5728\u6700\u540e\u4e00\u4e2a std::shared_ptr \u88ab\u9500\u6bc1\u524d\uff0c\u8d44\u6e90\u4e0d\u4f1a\u88ab\u9500\u6bc1\u3002\u5e94\u8be5\u4f7f\u7528 std::make_shared() \u6765\u521b\u5efa std::shared_ptr \u3002 \u5982\u679c\u9700\u8981\u521b\u5efa std::shared_ptr \u6307\u5411\u4e00\u4e2a\u76f8\u540c\u7684\u8d44\u6e90\uff0c\u5219\u9700\u8981\u4f7f\u7528\u62f7\u8d1d\u8bed\u4e49\u4ece std::shared_ptr \u521b\u5efa\u3002 M.8 \u2014 \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898 \u5f53\u4f60\u9700\u8981\u4e00\u4e2a\u6216\u591a\u4e2a\u5bf9\u8c61\u6765\u89c2\u5bdf\u6216\u83b7\u53d6\u7531 std::shared_pr \u7ba1\u7406\u7684\u8d44\u6e90\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u667a\u80fd\u6307\u9488 std::weak_ptr \u3002\u4f46\u662f\uff0c\u548c std::shared_ptr \u4e0d\u540c\u7684\u662f\uff0c\u5728\u5224\u65ad\u8d44\u6e90\u662f\u5426\u9700\u8981\u88ab\u9500\u6bc1\u65f6\uff0c\u6307\u5411\u5b83\u7684 std::weak_ptr \u7684\u4e2a\u6570\u4e0d\u4f1a\u88ab\u8003\u8651\u5728\u5185\u3002","title":"M.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u79fb\u52a8\u548c\u667a\u80fd\u6307\u9488"},{"location":"M-x-chapter-M-comprehensive-review/#_1","text":"M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb \u667a\u80fd\u6307\u9488\u7c7b \u662f\u4e00\u79cd\u7ec4\u5408\u7c7b\uff0c\u7528\u4e8e\u5bf9\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u8fdb\u884c\u7ba1\u7406\u5e76\u786e\u4fdd\u8be5\u5185\u5b58\u5728\u667a\u80fd\u6307\u9488\u6307\u5411\u7684\u5bf9\u8c61 \u79bb\u5f00\u4f5c\u7528\u57df \u540e\u88ab\u91ca\u653e\u3002 \u62f7\u8d1d\u8bed\u4e49 \u5141\u8bb8\u7c7b\u88ab\u590d\u5236\uff0c\u8be5\u8fc7\u7a0b\u4e3b\u8981\u901a\u8fc7 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u548c \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u6765\u5b8c\u6210\u3002 \u79fb\u52a8\u8bed\u4e49 \u6307\u7684\u662f\u7c7b\u5c06\u5bf9\u8c61\u7684\u6240\u6709\u6743\u8f6c\u8bd1\u7ed9\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u521b\u5efa\u4e00\u4e2a\u62f7\u8d1d\uff0c\u8be5\u8fc7\u7a0b\u4e3b\u8981\u901a\u8fc7 \u79fb\u52a8\u6784\u9020\u51fd\u6570 \u548c \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 \u6765\u5b8c\u6210\u3002 std::auto_ptr \u5df2\u7ecf\u88ab\u5f03\u7528\uff0c\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u3002 M.2 - \u53f3\u503c\u5f15\u7528 \u4e00\u4e2a \u53f3\u503c\u5f15\u7528 \u5f15\u7528\u5e94\u8be5\u4f7f\u7528\u4e00\u4e2a\u53f3\u503c\u6765\u521d\u59cb\u5316\u3002 \u53f3\u503c\u5f15\u7528\u4f7f\u7528\u53cc && \u53f7\u521b\u5efa\u3002\u7f16\u5199\u5c06\u53f3\u503c\u5f15\u7528\u4f5c\u4e3a \u5f62\u53c2 \u7684\u51fd\u6570\u662f\u53ef\u4ee5\u7684\uff0c\u4f46\u662f\u7edd\u5bf9\u4e0d\u8981\u5c06\u53f3\u503c\u5f15\u7528\u4f5c\u4e3a\u8fd4\u56de\u503c\u3002 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c \u5f53\u6211\u4eec\u6784\u5efa\u5bf9\u8c61\u6216\u5bf9\u5bf9\u8c61\u8d4b\u503c\u65f6\uff0c\u5982\u679c\u53c2\u6570\u662f \u5de6\u503c \uff0c\u5219\u552f\u4e00\u80fd\u505a\u7684\u5408\u7406\u64cd\u4f5c\u662f\u62f7\u8d1d\u8be5\u5de6\u503c\u3002\u6211\u4eec\u4e0d\u80fd\u5047\u5b9a\u6539\u53d8\u8be5\u5de6\u503c\u662f\u5b89\u5168\u7684\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u4f1a\u5728\u540e\u9762\u7684\u7a0b\u5e8f\u4e2d\u518d\u6b21\u88ab\u4f7f\u7528\u3002\u5bf9\u4e8e\u8868\u8fbe\u5f0f \u201ca = b\u201d\uff0c\u6211\u4eec\u6ca1\u6709\u7406\u7531\u671f\u671bb\u4ee5\u4efb\u4f55\u65b9\u5f0f\u88ab\u6539\u53d8\u3002 \u4f46\u662f\uff0c\u5f53\u6784\u5efa\u5bf9\u8c61\u6216\u8005\u8d4b\u503c\u5bf9\u8c61\u65f6\uff0c\u5982\u679c\u53c2\u6570\u662f \u53f3\u503c \uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u786e\u5b9a\u8be5\u53f3\u503c\u80af\u5b9a\u662f\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\u3002\u4e0e\u5176\u62f7\u8d1d\uff08\u5f00\u9500\u5927\uff09\u5b83\uff0c\u4e0d\u5982\u76f4\u63a5\u5c06\u5b83\u7684\u8d44\u6e90\u8f6c\u79fb\uff08\u5f00\u9500\u5c0f\uff09\u7ed9\u88ab\u6784\u5efa\u6216\u88ab\u8d4b\u503c\u7684\u5bf9\u8c61\u3002\u8fd9\u4e48\u505a\u662f\u5b89\u5168\u7684\uff0c\u56e0\u4e3a\u4e34\u65f6\u5bf9\u8c61\u603b\u662f\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u65f6\u5c31\u88ab\u9500\u6bc1\uff0c\u6240\u4ee5\u80af\u5b9a\u4e0d\u4f1a\u518d\u6b21\u88ab\u4f7f\u7528\u4e86\u3002 \u4f60\u53ef\u4ee5\u4f7f\u7528 delete \u5173\u952e\u5b57\u7981\u7528\u62f7\u8d1d\u8bed\u4e49\uff08\u901a\u8fc7\u5220\u9664\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26\uff09 M.4 - std::move std::move \u5141\u8bb8\u6211\u4eec\u5c06\u5de6\u503c\u5f53\u505a\u53f3\u503c\u5904\u7406\u3002\u5f53\u9700\u8981\u57fa\u4e8e\u7ec4\u7ec7\u8c03\u7528\u6216\u6fc0\u6d3b\u79fb\u52a8\u8bed\u4e49\u800c\u4e0d\u662f\u62f7\u8d1d\u8bed\u4e49\u65f6\u8fd9\u5f88\u6709\u7528\u3002 M.5 - std::move_if_noexcept \u5f53\u5bf9\u8c61\u6709\u4e00\u4e2a noexcept \u79fb\u52a8\u6784\u9020\u51fd\u6570\u65f6\uff0c std::move_if_noexcept \u4f1a\u8fd4\u56de\u4e00\u4e2a\u53ef\u79fb\u52a8\u7684\u53f3\u503c\u3002\u5426\u5219\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u53ef\u4ee5\u62f7\u8d1d\u7684\u5de6\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 noexcept \u4fee\u9970\u7b26\u548c std::move_if_noexcept \u786e\u4fdd\u53ea\u5728\u5177\u6709 \u5f3a\u5f02\u5e38\u4fdd\u8bc1 \u65f6\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\uff0c\u5176\u4ed6\u60c5\u51b5\u4e0b\u4f7f\u7528\u62f7\u8d1d\u8bed\u4e49\u3002 M.6 \u2014 std::unique_ptr \u667a\u80fd\u6307\u9488 std::unique_ptr \u5f88\u5e38\u7528\u3002\u5b83\u7528\u4e8e\u7ba1\u7406\u4e00\u4e2a\u4e0d\u53ef\u5171\u4eab\u7684\u8d44\u6e90\u3002\u5e94\u8be5\u4f7f\u7528 std::make_unique() (C++14) \u6765\u521b\u5efa std::unique_ptr \u3002 std::unique_ptr \u7981\u7528\u4e86\u62f7\u8d1d\u8bed\u4e49 M.7 \u2014 std::shared_ptr \u5f53\u591a\u4e2a\u5bf9\u8c61\u9700\u8981\u8bbf\u95ee\u540c\u4e00\u4e2a\u8d44\u6e90\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 std::shared_ptr \u3002\u5728\u6700\u540e\u4e00\u4e2a std::shared_ptr \u88ab\u9500\u6bc1\u524d\uff0c\u8d44\u6e90\u4e0d\u4f1a\u88ab\u9500\u6bc1\u3002\u5e94\u8be5\u4f7f\u7528 std::make_shared() \u6765\u521b\u5efa std::shared_ptr \u3002 \u5982\u679c\u9700\u8981\u521b\u5efa std::shared_ptr \u6307\u5411\u4e00\u4e2a\u76f8\u540c\u7684\u8d44\u6e90\uff0c\u5219\u9700\u8981\u4f7f\u7528\u62f7\u8d1d\u8bed\u4e49\u4ece std::shared_ptr \u521b\u5efa\u3002 M.8 \u2014 \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898 \u5f53\u4f60\u9700\u8981\u4e00\u4e2a\u6216\u591a\u4e2a\u5bf9\u8c61\u6765\u89c2\u5bdf\u6216\u83b7\u53d6\u7531 std::shared_pr \u7ba1\u7406\u7684\u8d44\u6e90\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u667a\u80fd\u6307\u9488 std::weak_ptr \u3002\u4f46\u662f\uff0c\u548c std::shared_ptr \u4e0d\u540c\u7684\u662f\uff0c\u5728\u5224\u65ad\u8d44\u6e90\u662f\u5426\u9700\u8981\u88ab\u9500\u6bc1\u65f6\uff0c\u6307\u5411\u5b83\u7684 std::weak_ptr \u7684\u4e2a\u6570\u4e0d\u4f1a\u88ab\u8003\u8651\u5728\u5185\u3002","title":"\u590d\u4e60"},{"location":"MOC/","text":"Mind Map 1. C++ \u57fa\u7840 1.1 - \u7a0b\u5e8f\u7ed3\u6784\u548c\u8bed\u53e5 1.2 - \u6ce8\u91ca 1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 1.5 - iostream \u7b80\u4ecb\uff1acout\uff0ccin \u548c endl 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a 1.7 - \u5173\u952e\u5b57\u548c\u6807\u8bc6\u7b26 1.8 - \u7a7a\u767d\u7b26\u548c\u57fa\u672c\u6392\u7248 1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 1.10 - \u8868\u8fbe\u5f0f\u7b80\u4ecb 1.11 - \u5f00\u53d1\u4f60\u7684\u7b2c\u4e00\u4e2a\u7a0b\u5e8f 1.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - C++\u57fa\u7840 2. C++ \u57fa\u7840\u2014\u2014\u51fd\u6570\u548c\u6587\u4ef6 2.1 - \u51fd\u6570\u7b80\u4ecb 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 2.3 - void \u51fd\u6570 2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2 2.5 - \u5c40\u90e8\u4f5c\u7528\u57df 2.6 - \u4e3a\u4ec0\u4e48\u51fd\u6570\u6709\u7528\uff0c\u5982\u4f55\u9ad8\u6548\u4f7f\u7528\u51fd\u6570 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f 2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb 2.11 - \u5934\u6587\u4ef6 2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e 2.13 - \u5982\u4f55\u8bbe\u8ba1\u7b2c\u4e00\u4e2a\u7a0b\u5e8f 2.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570\u4e0e\u6587\u4ef6 3. \u8c03\u8bd5 C++ \u7a0b\u5e8f \ud83d\udea7 3.1 - \u8bed\u6cd5\u548c\u8bed\u4e49\u9519\u8bef 3.2 - \u4ee3\u7801\u8c03\u8bd5\u6b65\u9aa4 3.3 - \u4ee3\u7801\u8c03\u8bd5\u7b56\u7565 3.4 \u2014 \u57fa\u672c\u4ee3\u7801\u8c03\u8bd5\u6280\u672f 3.5 - \u66f4\u591a\u4ee3\u7801\u8c03\u8bd5\u6280\u672f 3.6 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u5355\u6b65\u8c03\u8bd5 3.7 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8fd0\u884c\u548c\u65ad\u70b9 3.8 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u76d1\u63a7\u53d8\u91cf 3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808 3.10 - \u5728\u95ee\u9898\u53d1\u751f\u524d\u53d1\u73b0\u95ee\u9898 3.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4ee3\u7801\u8c03\u8bd5 4. \u57fa\u7840\u6570\u636e\u7c7b\u578b 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb 4.2 - Void 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 4.4 - \u6709\u7b26\u53f7\u6574\u578b 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 4.8 - \u6d6e\u70b9\u6570 4.9 - \u5e03\u5c14\u503c 4.10 - if \u8bed\u53e5\u7b80\u4ecb 4.11 - \u5b57\u7b26 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr 4.15 - \u5b57\u9762\u91cf 4.16 - \u6570\u503c\u7cfb\u7edf\uff08\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236\uff09 4.17 - std::string \u7b80\u4ecb 4.18 - std::string_view \u7b80\u4ecb 4.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u57fa\u672c\u6570\u636e\u7c7b\u578b 5. \u8fd0\u7b97\u7b26 5.1 - \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 5.3 - \u6c42\u6a21\u548c\u6307\u6570\u8fd0\u7b97 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 5.5 - \u9017\u53f7\u548c\u6761\u4ef6\u8fd0\u7b97\u7b26 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 5.7 - \u903b\u8f91\u8fd0\u7b97\u7b26 5.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26 6. \u4f5c\u7528\u57df\u3001\u751f\u547d\u5468\u671f\u548c\u94fe\u63a5 6.1 - \u590d\u5408\u8bed\u53e5\uff08\u8bed\u53e5\u5757\uff09 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 6.3 - \u5c40\u90e8\u53d8\u91cf 6.4 - \u5168\u5c40\u53d8\u91cf 6.5 - \u53d8\u91cf\u906e\u853d 6.6 - \u5185\u90e8\u94fe\u63a5 6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e 6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf 6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 6.13 - \u5185\u8054\u51fd\u6570 6.14 - Constexpr \u548c consteval \u51fd\u6570 6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 7. \u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406 \ud83d\udea7 7.1 - \u63a7\u5236\u6d41 7.2 - if \u8bed\u53e5\u548c\u8bed\u53e5\u5757 7.3 - \u5e38\u89c1\u7684 if \u8bed\u53e5\u9519\u8bef 7.4 - switch \u8bed\u53e5\u57fa\u7840 7.5 - switch fallthrough\u5c5e\u6027\u548c\u4f5c\u7528\u57df 7.6 - goto \u8bed\u53e5 7.7 - \u5faa\u73af\u548c while \u8bed\u53e5 7.8 - do-while \u8bed\u53e5 7.9 - for \u8bed\u53e5 7.10 - break \u548c continue 7.11 - \u6302\u8d77\uff08\u63d0\u524d\u7ed3\u675f\u7a0b\u5e8f\uff09 7.12 - \u4ee3\u7801\u6d4b\u8bd5 7.13 - \u4ee3\u7801\u8986\u76d6\u7387 7.14 - C++ \u4e2d\u5e38\u89c1\u7684\u8bed\u4e49\u9519\u8bef 7.15 - \u53d1\u73b0\u9519\u8bef\u548c\u5904\u7406\u9519\u8bef 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406 7.17 - \u65ad\u8a00\u548c static_assert 7.18 - \u968f\u673a\u6570\u751f\u6210\u5668\u7b80\u4ecb 7.19 - \u4f7f\u7528 Mersenne Twister \u751f\u6210\u968f\u673a\u6570 7.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406 8. \u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 8.3 - \u6570\u503c\u8f6c\u6362 8.4 - \u7b97\u672f\u8f6c\u6362 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast 8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad 8.9 - \u51fd\u6570\u91cd\u8f7d 8.10 - \u91cd\u8f7d\u51fd\u6570\u7684\u533a\u5206 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u4e0d\u660e\u786e\u5339\u914d 8.12 - \u9ed8\u8ba4\u53c2\u6570 8.13 - \u51fd\u6570\u6a21\u677f 8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f 8.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 9. \u590d\u5408\u7c7b\u578b-\u5f15\u7528\u548c\u6307\u9488 9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b 9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09 9.3 - \u5de6\u503c\u5f15\u7528 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 9.6 - \u6307\u9488\u7b80\u4ecb 9.7 - \u7a7a\u6307\u9488 9.8 - \u6307\u9488\u548cconst 9.9 - \u6309\u5730\u5740\u4f20\u9012 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad 9.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f15\u7528\u548c\u6307\u9488 10. \u590d\u5408\u7c7b\u578b-\u679a\u4e3e\u548c\u7ed3\u6784\u4f53 10.1 - \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7b80\u4ecb 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b 10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 10.8 - \u7ed3\u6784\u4f53\u4f20\u9012\u53ca\u5176\u4ed6 10.9 - \u57fa\u4e8e\u6307\u9488\u548c\u5f15\u7528\u7684\u6210\u5458\u9009\u62e9 10.10 - \u7c7b\u6a21\u677f 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD 10.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u679a\u4e3e\u548c\u7ed3\u6784\u4f53 10.y - \u5b66\u4f1a\u4f7f\u7528C++\u53c2\u8003\u624b\u518c 11. \u6570\u7ec4\uff0c\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d 11.1 - \u6570\u7ec4\uff08\u7b2c\u4e00\u90e8\u5206\uff09 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.3 - \u6570\u7ec4\u548c\u5faa\u73af 11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f 11.5 - \u591a\u7ef4\u6570\u7ec4 11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.8 - \u6307\u9488\u548c\u6570\u7ec4 11.9 - \u6307\u9488\u8fd0\u7b97\u548c\u6570\u7ec4\u7d22\u5f15 11.10 - C \u98ce\u683c\u5b57\u7b26\u4e32\u7b26\u53f7\u5e38\u91cf 11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d 11.12 - \u52a8\u6001\u5206\u914d\u6570\u7ec4 11.13 - for-each \u5faa\u73af 11.14 - void \u6307\u9488 11.15 - \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u548c\u591a\u7ef4\u6570\u7ec4 11.16 - \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb 11.17 - \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb 11.18 - \u8fed\u4ee3\u5668\u7b80\u4ecb 11.19 - \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb 11.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5206\u914d 12. \u51fd\u6570 12.1 - \u51fd\u6570\u6307\u9488 12.2 - \u6808\u548c\u5806 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a 12.4 - \u9012\u5f52 12.5 - \u547d\u4ee4\u884c\u53c2\u6570 12.6 - \u7701\u7565\u53f7\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb 12.8 - lambda \u95ed\u5305 12.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570 13. \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u57fa\u7840 13.1 - \u6b22\u8fce\u6765\u5230\u9762\u5411\u5bf9\u8c61\u7684\u4e16\u754c 13.2 - \u7c7b\u548c\u7c7b\u6210\u5458 13.3 - \u8bbf\u95ee\u4fee\u9970\u7b26 public \u548c private 13.4 - \u6210\u5458\u8bbf\u95ee\u51fd\u6570\u4e0e\u5c01\u88c5 13.5 - \u6784\u9020\u51fd\u6570 13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 13.8 - \u91cd\u53e0\u548c\u59d4\u6d3e\u6784\u9020\u51fd\u6570 13.9 - \u6790\u6784\u51fd\u6570 13.10 - \u9690\u85cf\u7684this\u6307\u9488 13.11 - \u7c7b\u4ee3\u7801\u548c\u5934\u6587\u4ef6 13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570 13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf 13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570 13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b 13.16 - \u533f\u540d\u5bf9\u8c61 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b 13.18 - \u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 13.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u9762\u5411\u5bf9\u8c61\u57fa\u7840 14. \u64cd\u4f5c\u7b26\u91cd\u8f7d \ud83d\udea7 14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26 14.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26\u91cd\u8f7d 16. \u5bf9\u8c61\u5173\u7cfb\u7b80\u4ecb 16.1 - \u5bf9\u8c61\u5173\u7cfb 16.2 - \u7ec4\u5408\u5173\u7cfb 16.3 - \u805a\u5408\u5173\u7cfb 16.4 - \u5173\u8054\u5173\u7cfb 16.5 - \u4f9d\u8d56\u5173\u7cfb 16.6 - \u5bb9\u5668\u7c7b 16.7 - std::initializer_list 16.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5bf9\u8c61\u5173\u7cfb 17. \u7ee7\u627f 17.1 - \u7ee7\u627f\u7b80\u4ecb 17.2 - C++\u7ee7\u627f\u57fa\u7840 17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f 17.4 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u548c\u521d\u59cb\u5316 17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 17.6 - \u4e3a\u6d3e\u751f\u7c7b\u6dfb\u52a0\u65b0\u529f\u80fd 17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570 17.9 - \u591a\u91cd\u7ee7\u627f 17.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7ee7\u627f 18. \u865a\u51fd\u6570 18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b 18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316 18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a 18.6 - \u865a\u51fd\u6570\u8868 18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u7c7b 18.8 - \u865a\u57fa\u7c7b 18.9 - \u5bf9\u8c61\u5207\u7247 18.10 - \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 18.11 - \u4f7f\u7528<<\u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b 18.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u865a\u51fd\u6570 19. \u6a21\u677f\u548c\u7c7b \ud83d\udea7 19.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6a21\u677f\u548c\u7c7b 20. \u5f02\u5e38 \ud83d\udea7 20.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f02\u5e38 20.4 M. move \u548c\u667a\u80fd\u6307\u9488 M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb M.2 - \u53f3\u503c\u5f15\u7528 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c M.4 - std::move M.5 - std::move_if_noexcept M.6 \u2014 std::unique_ptr M.7 \u2014 std::shared_ptr M.8 \u2014 \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898 M.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u79fb\u52a8\u548c\u667a\u80fd\u6307\u9488 21. STL 21.1 - \u6807\u51c6\u5e93 21.2 - STL\u5bb9\u5668 21.3 - STL\u8fed\u4ee3\u5668 21.4 - STL\u7b97\u6cd5 22. std \u5b57\u7b26\u4e32 22.1 - std::string \u548c std::wstring 22.2 - std-string \u7684\u6784\u9020\u548c\u6790\u6784 22.3 - std-string \u7684\u957f\u5ea6\u548c\u5bb9\u91cf 22.4 - std-string \u5b57\u7b26\u8bbf\u95ee\u548cC\u5b57\u7b26\u6570\u7ec4\u8f6c\u6362 22.5 - std-string \u8d4b\u503c\u548c\u4ea4\u6362 22.6 - std-string \u8ffd\u52a0 22.7 - std-string \u63d2\u5165 23. \u8f93\u5165\u8f93\u51fa 23.1 - \u8f93\u5165\u8f93\u51fa\u6d41 23.2 - \u4f7f\u7528istream\u5904\u7406\u8f93\u5165 23.3 - \u4f7f\u7528ostream\u548cios\u5904\u7406\u8f93\u51fa 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b 23.5 - \u6d41\u72b6\u6001\u548c\u8f93\u5165\u9a8c\u8bc1 23.6 - \u57fa\u672c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa 23.7 - \u968f\u673a\u6587\u4ef6\u8f93\u5165\u8f93\u51fa A. \u5176\u4ed6\u8bdd\u9898 A.1 - \u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93 A.2 - \u5728visual studio\u4e2d\u4f7f\u7528\u5e93 A.3 - \u5728Code Blocks\u4e2d\u4f7f\u7528\u5e93 A.4 \u2014 C++ FAQ B. C++\u6807\u51c6\u66f4\u65b0 B.1 - C++11 B.2 - C++14 B.3 - C++17 B.4 - C++20 O1. \u4f4d\u8fd0\u7b97 \ud83d\udea7 O.1 - bit\u6807\u8bb0\u548c\u901a\u8fc7std::bitset\u64cd\u4f5cbit O.2 - \u4f4d\u8fd0\u7b97\u7b26 O.3 - \u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u548c\u63a9\u7801\u64cd\u4f5c\u4f4d O.4 - \u4e8c\u8fdb\u5236\u548c\u5341\u8fdb\u5236\u8f6c\u6362","title":"\u603b\u76ee\u5f55"},{"location":"MOC/#1-c","text":"1.1 - \u7a0b\u5e8f\u7ed3\u6784\u548c\u8bed\u53e5 1.2 - \u6ce8\u91ca 1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 1.5 - iostream \u7b80\u4ecb\uff1acout\uff0ccin \u548c endl 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a 1.7 - \u5173\u952e\u5b57\u548c\u6807\u8bc6\u7b26 1.8 - \u7a7a\u767d\u7b26\u548c\u57fa\u672c\u6392\u7248 1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 1.10 - \u8868\u8fbe\u5f0f\u7b80\u4ecb 1.11 - \u5f00\u53d1\u4f60\u7684\u7b2c\u4e00\u4e2a\u7a0b\u5e8f 1.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - C++\u57fa\u7840","title":"1. C++ \u57fa\u7840"},{"location":"MOC/#2-c","text":"2.1 - \u51fd\u6570\u7b80\u4ecb 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 2.3 - void \u51fd\u6570 2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2 2.5 - \u5c40\u90e8\u4f5c\u7528\u57df 2.6 - \u4e3a\u4ec0\u4e48\u51fd\u6570\u6709\u7528\uff0c\u5982\u4f55\u9ad8\u6548\u4f7f\u7528\u51fd\u6570 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f 2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb 2.11 - \u5934\u6587\u4ef6 2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e 2.13 - \u5982\u4f55\u8bbe\u8ba1\u7b2c\u4e00\u4e2a\u7a0b\u5e8f 2.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570\u4e0e\u6587\u4ef6","title":"2. C++ \u57fa\u7840\u2014\u2014\u51fd\u6570\u548c\u6587\u4ef6"},{"location":"MOC/#3-c","text":"3.1 - \u8bed\u6cd5\u548c\u8bed\u4e49\u9519\u8bef 3.2 - \u4ee3\u7801\u8c03\u8bd5\u6b65\u9aa4 3.3 - \u4ee3\u7801\u8c03\u8bd5\u7b56\u7565 3.4 \u2014 \u57fa\u672c\u4ee3\u7801\u8c03\u8bd5\u6280\u672f 3.5 - \u66f4\u591a\u4ee3\u7801\u8c03\u8bd5\u6280\u672f 3.6 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u5355\u6b65\u8c03\u8bd5 3.7 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8fd0\u884c\u548c\u65ad\u70b9 3.8 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u76d1\u63a7\u53d8\u91cf 3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808 3.10 - \u5728\u95ee\u9898\u53d1\u751f\u524d\u53d1\u73b0\u95ee\u9898 3.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4ee3\u7801\u8c03\u8bd5","title":"3. \u8c03\u8bd5 C++ \u7a0b\u5e8f \ud83d\udea7"},{"location":"MOC/#4","text":"4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb 4.2 - Void 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 4.4 - \u6709\u7b26\u53f7\u6574\u578b 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 4.8 - \u6d6e\u70b9\u6570 4.9 - \u5e03\u5c14\u503c 4.10 - if \u8bed\u53e5\u7b80\u4ecb 4.11 - \u5b57\u7b26 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr 4.15 - \u5b57\u9762\u91cf 4.16 - \u6570\u503c\u7cfb\u7edf\uff08\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236\uff09 4.17 - std::string \u7b80\u4ecb 4.18 - std::string_view \u7b80\u4ecb 4.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u57fa\u672c\u6570\u636e\u7c7b\u578b","title":"4. \u57fa\u7840\u6570\u636e\u7c7b\u578b"},{"location":"MOC/#5","text":"5.1 - \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 5.3 - \u6c42\u6a21\u548c\u6307\u6570\u8fd0\u7b97 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 5.5 - \u9017\u53f7\u548c\u6761\u4ef6\u8fd0\u7b97\u7b26 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 5.7 - \u903b\u8f91\u8fd0\u7b97\u7b26 5.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26","title":"5. \u8fd0\u7b97\u7b26"},{"location":"MOC/#6","text":"6.1 - \u590d\u5408\u8bed\u53e5\uff08\u8bed\u53e5\u5757\uff09 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 6.3 - \u5c40\u90e8\u53d8\u91cf 6.4 - \u5168\u5c40\u53d8\u91cf 6.5 - \u53d8\u91cf\u906e\u853d 6.6 - \u5185\u90e8\u94fe\u63a5 6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e 6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf 6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 6.13 - \u5185\u8054\u51fd\u6570 6.14 - Constexpr \u548c consteval \u51fd\u6570 6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5","title":"6. \u4f5c\u7528\u57df\u3001\u751f\u547d\u5468\u671f\u548c\u94fe\u63a5"},{"location":"MOC/#7","text":"7.1 - \u63a7\u5236\u6d41 7.2 - if \u8bed\u53e5\u548c\u8bed\u53e5\u5757 7.3 - \u5e38\u89c1\u7684 if \u8bed\u53e5\u9519\u8bef 7.4 - switch \u8bed\u53e5\u57fa\u7840 7.5 - switch fallthrough\u5c5e\u6027\u548c\u4f5c\u7528\u57df 7.6 - goto \u8bed\u53e5 7.7 - \u5faa\u73af\u548c while \u8bed\u53e5 7.8 - do-while \u8bed\u53e5 7.9 - for \u8bed\u53e5 7.10 - break \u548c continue 7.11 - \u6302\u8d77\uff08\u63d0\u524d\u7ed3\u675f\u7a0b\u5e8f\uff09 7.12 - \u4ee3\u7801\u6d4b\u8bd5 7.13 - \u4ee3\u7801\u8986\u76d6\u7387 7.14 - C++ \u4e2d\u5e38\u89c1\u7684\u8bed\u4e49\u9519\u8bef 7.15 - \u53d1\u73b0\u9519\u8bef\u548c\u5904\u7406\u9519\u8bef 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406 7.17 - \u65ad\u8a00\u548c static_assert 7.18 - \u968f\u673a\u6570\u751f\u6210\u5668\u7b80\u4ecb 7.19 - \u4f7f\u7528 Mersenne Twister \u751f\u6210\u968f\u673a\u6570 7.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406","title":"7. \u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406 \ud83d\udea7"},{"location":"MOC/#8","text":"8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 8.3 - \u6570\u503c\u8f6c\u6362 8.4 - \u7b97\u672f\u8f6c\u6362 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast 8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad 8.9 - \u51fd\u6570\u91cd\u8f7d 8.10 - \u91cd\u8f7d\u51fd\u6570\u7684\u533a\u5206 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u4e0d\u660e\u786e\u5339\u914d 8.12 - \u9ed8\u8ba4\u53c2\u6570 8.13 - \u51fd\u6570\u6a21\u677f 8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f 8.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5","title":"8. \u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d"},{"location":"MOC/#9-","text":"9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b 9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09 9.3 - \u5de6\u503c\u5f15\u7528 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 9.6 - \u6307\u9488\u7b80\u4ecb 9.7 - \u7a7a\u6307\u9488 9.8 - \u6307\u9488\u548cconst 9.9 - \u6309\u5730\u5740\u4f20\u9012 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad 9.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f15\u7528\u548c\u6307\u9488","title":"9. \u590d\u5408\u7c7b\u578b-\u5f15\u7528\u548c\u6307\u9488"},{"location":"MOC/#10-","text":"10.1 - \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7b80\u4ecb 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b 10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 10.8 - \u7ed3\u6784\u4f53\u4f20\u9012\u53ca\u5176\u4ed6 10.9 - \u57fa\u4e8e\u6307\u9488\u548c\u5f15\u7528\u7684\u6210\u5458\u9009\u62e9 10.10 - \u7c7b\u6a21\u677f 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD 10.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u679a\u4e3e\u548c\u7ed3\u6784\u4f53 10.y - \u5b66\u4f1a\u4f7f\u7528C++\u53c2\u8003\u624b\u518c","title":"10. \u590d\u5408\u7c7b\u578b-\u679a\u4e3e\u548c\u7ed3\u6784\u4f53"},{"location":"MOC/#11","text":"11.1 - \u6570\u7ec4\uff08\u7b2c\u4e00\u90e8\u5206\uff09 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.3 - \u6570\u7ec4\u548c\u5faa\u73af 11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f 11.5 - \u591a\u7ef4\u6570\u7ec4 11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.8 - \u6307\u9488\u548c\u6570\u7ec4 11.9 - \u6307\u9488\u8fd0\u7b97\u548c\u6570\u7ec4\u7d22\u5f15 11.10 - C \u98ce\u683c\u5b57\u7b26\u4e32\u7b26\u53f7\u5e38\u91cf 11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d 11.12 - \u52a8\u6001\u5206\u914d\u6570\u7ec4 11.13 - for-each \u5faa\u73af 11.14 - void \u6307\u9488 11.15 - \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u548c\u591a\u7ef4\u6570\u7ec4 11.16 - \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb 11.17 - \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb 11.18 - \u8fed\u4ee3\u5668\u7b80\u4ecb 11.19 - \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb 11.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5206\u914d","title":"11. \u6570\u7ec4\uff0c\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d"},{"location":"MOC/#12","text":"12.1 - \u51fd\u6570\u6307\u9488 12.2 - \u6808\u548c\u5806 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a 12.4 - \u9012\u5f52 12.5 - \u547d\u4ee4\u884c\u53c2\u6570 12.6 - \u7701\u7565\u53f7\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb 12.8 - lambda \u95ed\u5305 12.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570","title":"12. \u51fd\u6570"},{"location":"MOC/#13","text":"13.1 - \u6b22\u8fce\u6765\u5230\u9762\u5411\u5bf9\u8c61\u7684\u4e16\u754c 13.2 - \u7c7b\u548c\u7c7b\u6210\u5458 13.3 - \u8bbf\u95ee\u4fee\u9970\u7b26 public \u548c private 13.4 - \u6210\u5458\u8bbf\u95ee\u51fd\u6570\u4e0e\u5c01\u88c5 13.5 - \u6784\u9020\u51fd\u6570 13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 13.8 - \u91cd\u53e0\u548c\u59d4\u6d3e\u6784\u9020\u51fd\u6570 13.9 - \u6790\u6784\u51fd\u6570 13.10 - \u9690\u85cf\u7684this\u6307\u9488 13.11 - \u7c7b\u4ee3\u7801\u548c\u5934\u6587\u4ef6 13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570 13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf 13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570 13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b 13.16 - \u533f\u540d\u5bf9\u8c61 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b 13.18 - \u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 13.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u9762\u5411\u5bf9\u8c61\u57fa\u7840","title":"13. \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u57fa\u7840"},{"location":"MOC/#14","text":"14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26 14.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26\u91cd\u8f7d","title":"14. \u64cd\u4f5c\u7b26\u91cd\u8f7d \ud83d\udea7"},{"location":"MOC/#16","text":"16.1 - \u5bf9\u8c61\u5173\u7cfb 16.2 - \u7ec4\u5408\u5173\u7cfb 16.3 - \u805a\u5408\u5173\u7cfb 16.4 - \u5173\u8054\u5173\u7cfb 16.5 - \u4f9d\u8d56\u5173\u7cfb 16.6 - \u5bb9\u5668\u7c7b 16.7 - std::initializer_list 16.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5bf9\u8c61\u5173\u7cfb","title":"16. \u5bf9\u8c61\u5173\u7cfb\u7b80\u4ecb"},{"location":"MOC/#17","text":"17.1 - \u7ee7\u627f\u7b80\u4ecb 17.2 - C++\u7ee7\u627f\u57fa\u7840 17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f 17.4 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u548c\u521d\u59cb\u5316 17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 17.6 - \u4e3a\u6d3e\u751f\u7c7b\u6dfb\u52a0\u65b0\u529f\u80fd 17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570 17.9 - \u591a\u91cd\u7ee7\u627f 17.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7ee7\u627f","title":"17. \u7ee7\u627f"},{"location":"MOC/#18","text":"18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b 18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316 18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a 18.6 - \u865a\u51fd\u6570\u8868 18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u7c7b 18.8 - \u865a\u57fa\u7c7b 18.9 - \u5bf9\u8c61\u5207\u7247 18.10 - \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 18.11 - \u4f7f\u7528<<\u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b 18.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u865a\u51fd\u6570","title":"18. \u865a\u51fd\u6570"},{"location":"MOC/#19","text":"19.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6a21\u677f\u548c\u7c7b","title":"19. \u6a21\u677f\u548c\u7c7b \ud83d\udea7"},{"location":"MOC/#20","text":"20.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f02\u5e38 20.4","title":"20. \u5f02\u5e38 \ud83d\udea7"},{"location":"MOC/#m-move","text":"M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb M.2 - \u53f3\u503c\u5f15\u7528 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c M.4 - std::move M.5 - std::move_if_noexcept M.6 \u2014 std::unique_ptr M.7 \u2014 std::shared_ptr M.8 \u2014 \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898 M.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u79fb\u52a8\u548c\u667a\u80fd\u6307\u9488","title":"M. move \u548c\u667a\u80fd\u6307\u9488"},{"location":"MOC/#21-stl","text":"21.1 - \u6807\u51c6\u5e93 21.2 - STL\u5bb9\u5668 21.3 - STL\u8fed\u4ee3\u5668 21.4 - STL\u7b97\u6cd5","title":"21. STL"},{"location":"MOC/#22-std","text":"22.1 - std::string \u548c std::wstring 22.2 - std-string \u7684\u6784\u9020\u548c\u6790\u6784 22.3 - std-string \u7684\u957f\u5ea6\u548c\u5bb9\u91cf 22.4 - std-string \u5b57\u7b26\u8bbf\u95ee\u548cC\u5b57\u7b26\u6570\u7ec4\u8f6c\u6362 22.5 - std-string \u8d4b\u503c\u548c\u4ea4\u6362 22.6 - std-string \u8ffd\u52a0 22.7 - std-string \u63d2\u5165","title":"22. std \u5b57\u7b26\u4e32"},{"location":"MOC/#23","text":"23.1 - \u8f93\u5165\u8f93\u51fa\u6d41 23.2 - \u4f7f\u7528istream\u5904\u7406\u8f93\u5165 23.3 - \u4f7f\u7528ostream\u548cios\u5904\u7406\u8f93\u51fa 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b 23.5 - \u6d41\u72b6\u6001\u548c\u8f93\u5165\u9a8c\u8bc1 23.6 - \u57fa\u672c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa 23.7 - \u968f\u673a\u6587\u4ef6\u8f93\u5165\u8f93\u51fa","title":"23. \u8f93\u5165\u8f93\u51fa"},{"location":"MOC/#a","text":"A.1 - \u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93 A.2 - \u5728visual studio\u4e2d\u4f7f\u7528\u5e93 A.3 - \u5728Code Blocks\u4e2d\u4f7f\u7528\u5e93 A.4 \u2014 C++ FAQ","title":"A. \u5176\u4ed6\u8bdd\u9898"},{"location":"MOC/#b-c","text":"B.1 - C++11 B.2 - C++14 B.3 - C++17 B.4 - C++20","title":"B. C++\u6807\u51c6\u66f4\u65b0"},{"location":"MOC/#o1","text":"O.1 - bit\u6807\u8bb0\u548c\u901a\u8fc7std::bitset\u64cd\u4f5cbit O.2 - \u4f4d\u8fd0\u7b97\u7b26 O.3 - \u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u548c\u63a9\u7801\u64cd\u4f5c\u4f4d O.4 - \u4e8c\u8fdb\u5236\u548c\u5341\u8fdb\u5236\u8f6c\u6362","title":"O1. \u4f4d\u8fd0\u7b97 \ud83d\udea7"},{"location":"Multiple%20inheritance/","tags":["\u8bcd\u6c47\u8868","Multiple inheritance"],"text":"","title":"\u591a\u91cd\u7ee7\u627f"},{"location":"Name-mangling/","tags":["\u8bcd\u6c47\u8868","Name-mangling"],"text":"","title":"\u540d\u79f0\u4fee\u9970"},{"location":"O-1-bit-flags-and-bit-manipulation-via-std-bitset/","tags":["bit"],"text":"Key Takeaway \u5728\u73b0\u4ee3\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u4e0a\uff0c\u6700\u5c0f\u7684\u53ef\u5bfb\u5740\u5185\u5b58\u5355\u5143\u662f\u5b57\u8282\u3002\u7531\u4e8e\u6240\u6709\u5bf9\u8c61\u90fd\u9700\u8981\u5177\u6709\u552f\u4e00\u7684\u5185\u5b58\u5730\u5740\uff0c\u8fd9\u610f\u5473\u7740\u5bf9\u8c61\u7684\u5927\u5c0f\u5fc5\u987b\u81f3\u5c11\u4e3a\u4e00\u4e2a\u5b57\u8282\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u53d8\u91cf\u7c7b\u578b\uff0c\u8fd9\u5f88\u597d\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u5e03\u5c14\u503c\uff0c\u8fd9\u6709\u70b9\u6d6a\u8d39(\u53cc\u5173\u8bed\uff09 \u201c\u539f\u6587:this is a bit wasteful\u201d \u3002\u5e03\u5c14\u7c7b\u578b\u53ea\u6709\u4e24\u79cd\u72b6\u6001\uff1a\u771f \uff081\uff09 \u6216\u5047 \uff080\uff09\u3002\u8fd9\u7ec4\u72b6\u6001\u53ea\u9700\u8981\u4e00\u4e2a\u4f4d\u6765\u5b58\u50a8\u3002\u4f46\u662f\uff0c\u5982\u679c\u53d8\u91cf\u5fc5\u987b\u81f3\u5c11\u662f\u4e00\u4e2a\u5b57\u8282\uff0c\u5e76\u4e14\u4e00\u4e2a\u5b57\u8282\u662f 8 \u4f4d\uff0c\u8fd9\u610f\u5473\u7740\u5e03\u5c14\u503c\u4f7f\u7528 1 \u4f4d\uff0c\u800c\u5176\u4ed6 7 \u4f4d\u672a\u4f7f\u7528\u3002 \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4e48\u505a\u5f88\u597d \u2014\u2014 \u6211\u4eec\u901a\u5e38\u4e0d\u4f1a\u5bf9\u5185\u5b58\u7ba1\u7406\u5982\u6b64\u82db\u523b\uff0c\u4ee5\u81f3\u4e8e\u6211\u4eec\u9700\u8981\u5173\u5fc37\u4e2a\u6d6a\u8d39\u7684\u4f4d\uff08\u6211\u4eec\u66f4\u5e94\u8be5\u4f7f\u4ee3\u7801\u6613\u4e8e\u7406\u89e3\uff0c\u6613\u4e8e\u7ef4\u62a4\uff09\u3002 \u4f46\u662f\uff0c\u5728\u67d0\u4e9b\u5b58\u50a8\u5bc6\u96c6\u578b\u60c5\u51b5\u4e0b\uff0c\u51fa\u4e8e\u5b58\u50a8\u6548\u7387\u76ee\u7684\uff0c\u5c06 8 \u4e2a\u5355\u72ec\u7684\u5e03\u5c14\u503c\u201c\u6253\u5305\u201d\u5230\u5355\u4e2a\u5b57\u8282\u4e2d\u53ef\u80fd\u5f88\u6709\u7528\u3002 \u505a\u8fd9\u4e9b\u4e8b\u60c5\u9700\u8981\u6211\u4eec\u53ef\u4ee5\u5728\u4f4d\u7ea7\u522b\u64cd\u4f5c\u5bf9\u8c61\u3002\u5e78\u8fd0\u7684\u662f\uff0cC++\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u505a\u5230\u8fd9\u4e00\u70b9\u7684\u5de5\u5177\u3002\u4fee\u6539\u5bf9\u8c61\u4e2d\u7684\u5355\u4e2a\u4f4d\u79f0\u4e3a \u4f4d\u64cd\u4f5c \u3002 \u4f4d\u64cd\u4f5c\u5728\u52a0\u5bc6\u548c\u538b\u7f29\u7b97\u6cd5\u4e2d\u4e5f\u5f88\u6709\u7528\u3002 \u4f4d\u6807\u5fd7 \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u4f7f\u7528\u53d8\u91cf\u6765\u4fdd\u5b58\u5355\u4e2a\u503c\uff1a 1 2 int foo { 5 }; // assign foo the value 5 (probably uses 32 bits of storage) std :: cout << foo ; // print the value 5 \u4f46\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u4eec\u89c6\u4e3a\u5355\u4e2a\u4f4d\u7684\u96c6\u5408\uff0c\u800c\u4e0d\u662f\u5c06\u5bf9\u8c61\u89c6\u4e3a\u5305\u542b\u5355\u4e2a\u503c\u7684\u5bf9\u8c61\u3002\u5f53\u5bf9\u8c61\u7684\u5355\u4e2a\u4f4d\u7528\u4f5c\u5e03\u5c14\u503c\u65f6\uff0c\u8fd9\u4e9b\u4f4d\u79f0\u4e3a \u4f4d\u6807\u5fd7 \u3002 \u8981\u5b9a\u4e49\u4e00\u7ec4\u4f4d\u6807\u5fd7\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528 \u9002\u5f53\u5927\u5c0f\u7684\u65e0\u7b26\u53f7\u6574\u6570 \uff088 \u4f4d\u300116 \u4f4d\u300132 \u4f4d\u7b49......\u53d6\u51b3\u4e8e\u6211\u4eec\u6709\u591a\u5c11\u6807\u5fd7\uff09\uff0c\u6216 std::bitset \u3002 1 2 3 #include // for std::bitset std :: bitset < 8 > mybitset {}; // 8 bits in size means room for 8 flags \u6700\u4f73\u5b9e\u8df5 \u4f4d\u64cd\u4f5c\u662f\u5c11\u6570\u5e94\u8be5\u660e\u786e\u4f7f\u7528\u65e0\u7b26\u53f7\u6574\u6570\uff08\u6216 std\uff1a\uff1abitset\uff09\u7684\u60c5\u51b5\u4e4b\u4e00\u3002 \u5728\u672c\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u5c55\u793a\u5982\u4f55\u901a\u8fc7 std::bitset \u4ee5\u7b80\u5355\u7684\u65b9\u5f0f\u8fdb\u884c\u4f4d\u64cd\u4f5c\u3002\u5728\u4e0b\u4e00\u7ec4\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u4ee5\u66f4\u56f0\u96be\u4f46\u7528\u9014\u5e7f\u6cdb\u7684\u65b9\u5f0f\u505a\u5230\u8fd9\u4e00\u70b9\u3002 \u4f4d\u7f16\u53f7\u548c\u4f4d\u4f4d\u7f6e \u7ed9\u5b9a\u4e00\u4e2a\u4f4d\u5e8f\u5217\uff0c\u6211\u4eec\u901a\u5e38\u4ece\u53f3\u5230\u5de6\u5bf9\u4f4d\u8fdb\u884c\u7f16\u53f7\uff0c \u4ece 0\uff08\u800c\u4e0d\u662f 1\uff09\u5f00\u59cb \u3002\u6bcf\u4e2a\u6570\u5b57\u8868\u793a\u4e00\u4e2a \u4f4d\u4f4d\u7f6e \u3002 1 2 76543210 Bit position 00000101 Bit sequence \u7ed9\u5b9a\u4f4d\u5e8f\u5217 0000 0101\uff0c\u4f4d\u4e8e\u4f4d\u7f6e 0 \u548c 2 \u7684\u4f4d\u7684\u503c\u4e3a 1\uff0c\u5176\u4ed6\u4f4d\u7684\u503c\u4e3a 0\u3002 \u901a\u8fc7 std::bitset \u64cd\u4f5c\u4f4d \u5728 \u7b2c 4.16 \u8bfe\u2014\u2014\u6570\u5b57\u7cfb\u7edf\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236 \u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 std::bitset \u6253\u5370\u4e8c\u8fdb\u5236\u503c\u3002\u7136\u800c\uff0c\u8fd9\u5e76\u4e0d\u662f std::bitset \u80fd\u505a\u7684\u552f\u4e00\u6709\u7528\u7684\u4e8b\u60c5\u3002 std::bitset \u63d0\u4f9b\u4e86 4 \u4e2a\u53ef\u7528\u4e8e\u4f4d\u64cd\u4f5c\u7684\u5173\u952e\u51fd\u6570\uff1a test() \u5141\u8bb8\u6211\u4eec\u67e5\u8be2\u67d0\u4e2a\u4f4d\u662f 0 \u8fd8\u662f 1 set() \u5141\u8bb8\u6211\u4eec\u6253\u5f00\u4e00\u4e2a\u4f4d\uff08\u5982\u679c\u4f4d\u5df2\u7ecf\u6253\u5f00\uff0c\u8fd9\u5c06\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\uff09 reset() \u5141\u8bb8\u6211\u4eec\u5173\u95ed\u4e00\u4e2a\u4f4d\uff08\u5982\u679c\u8be5\u4f4d\u5df2\u7ecf\u5173\u95ed\uff0c\u8fd9\u5c06\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\uff09 flip() \u5141\u8bb8\u6211\u4eec\u5c06\u4f4d\u503c\u4ece 0 \u7ffb\u8f6c\u4e3a 1\uff0c\u53cd\u4e4b\u4ea6\u7136 \u8fd9\u4e9b\u51fd\u6570\u4e2d\u7684\u6bcf\u4e00\u4e2a\u90fd\u5c06\u6211\u4eec\u8981\u64cd\u4f5c\u7684\u4f4d\u7684\u4f4d\u7f6e\u4f5c\u4e3a\u5b83\u4eec\u552f\u4e00\u7684\u53c2\u6570\u3002 \u8fd9\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include int main () { std :: bitset < 8 > bits { 0b0000'0101 }; // we need 8 bits, start with bit pattern 0000 0101 bits . set ( 3 ); // set bit position 3 to 1 (now we have 0000 1101) bits . flip ( 4 ); // flip bit 4 (now we have 0001 1101) bits . reset ( 4 ); // set bit 4 back to 0 (now we have 0000 1101) std :: cout << \"All the bits: \" << bits << '\\n' ; std :: cout << \"Bit 3 has value: \" << bits . test ( 3 ) << '\\n' ; std :: cout << \"Bit 4 has value: \" << bits . test ( 4 ) << '\\n' ; return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 3 All the bits: 00001101 Bit 3 has value: 1 Bit 4 has value: 0 \u5c0f\u8d34\u58eb \u5982\u679c\u60a8\u9700\u8981\u590d\u4e60\u4ec0\u4e48\u662f 0b \u524d\u7f00\u6216 ' \u5206\u9694\u7b26\uff0c\u8bf7\u67e5\u770b 4.16 - \u6570\u5b57\u7cfb\u7edf\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236 \u5982\u679c\u6211\u4eec\u60f3\u4e00\u6b21\u83b7\u53d6\u6216\u8bbe\u7f6e\u591a\u4e2a\u4f4d\u600e\u4e48\u529e std::bitset \u5e76\u4e0d\u5bb9\u6613\u5b9e\u73b0\u8fd9\u4e2a\u9700\u6c42\u3002\u4e3a\u4e86\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u6216\u8005\u5982\u679c\u6211\u4eec\u60f3\u4f7f\u7528\u65e0\u7b26\u53f7\u6574\u6570\u4f4d\u6807\u5fd7\u800c\u4e0d\u662f std::bitset\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u66f4\u4f20\u7edf\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u51e0\u8282\u8bfe\u4e2d\u4ecb\u7ecd\u8fd9\u4e9b\u5185\u5bb9\u3002 std::bitset \u7684\u5927\u5c0f \u4e00\u4e2a\u6f5c\u5728\u7684\u60ca\u559c\u662f std::bitset \u9488\u5bf9\u901f\u5ea6\u8fdb\u884c\u4e86\u4f18\u5316 \uff0c\u800c\u4e0d\u662f\u8282\u7701\u5185\u5b58\u3002std::bitset \u7684\u5927\u5c0f\u901a\u5e38\u662f\u4fdd\u5b58\u4f4d\u6240\u9700\u7684\u5b57\u8282\u6570\uff0c \u56db\u820d\u4e94\u5165\u5230(rounded up to) \u6700\u63a5\u8fd1\u7684 sizeof(size_t) \uff0c\u5728 32 \u4f4d\u673a\u5668\u4e0a\u4e3a 4 \u5b57\u8282\uff0c\u5728 64 \u4f4d\u673a\u5668\u4e0a\u4e3a 8 \u5b57\u8282\u3002 \u56e0\u6b64\uff0c std::bitset<8> \u901a\u5e38\u4f7f\u7528 4 \u6216 8 \u5b57\u8282\u7684\u5185\u5b58\uff0c\u5373\u4f7f\u5b83\u5728\u6280\u672f\u4e0a\u53ea\u9700\u8981 1 \u4e2a\u5b57\u8282\u6765\u5b58\u50a8 8 \u4f4d\u3002\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u60f3\u8981\u65b9\u4fbf\u800c\u4e0d\u662f\u8282\u7701\u5185\u5b58\u65f6\uff0cstd::bitset \u662f\u6700\u6709\u7528\u7684\u3002","title":"O.1 - bit\u6807\u8bb0\u548c\u901a\u8fc7std::bitset\u64cd\u4f5cbit"},{"location":"O-1-bit-flags-and-bit-manipulation-via-std-bitset/#_1","text":"\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u4f7f\u7528\u53d8\u91cf\u6765\u4fdd\u5b58\u5355\u4e2a\u503c\uff1a 1 2 int foo { 5 }; // assign foo the value 5 (probably uses 32 bits of storage) std :: cout << foo ; // print the value 5 \u4f46\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u4eec\u89c6\u4e3a\u5355\u4e2a\u4f4d\u7684\u96c6\u5408\uff0c\u800c\u4e0d\u662f\u5c06\u5bf9\u8c61\u89c6\u4e3a\u5305\u542b\u5355\u4e2a\u503c\u7684\u5bf9\u8c61\u3002\u5f53\u5bf9\u8c61\u7684\u5355\u4e2a\u4f4d\u7528\u4f5c\u5e03\u5c14\u503c\u65f6\uff0c\u8fd9\u4e9b\u4f4d\u79f0\u4e3a \u4f4d\u6807\u5fd7 \u3002 \u8981\u5b9a\u4e49\u4e00\u7ec4\u4f4d\u6807\u5fd7\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528 \u9002\u5f53\u5927\u5c0f\u7684\u65e0\u7b26\u53f7\u6574\u6570 \uff088 \u4f4d\u300116 \u4f4d\u300132 \u4f4d\u7b49......\u53d6\u51b3\u4e8e\u6211\u4eec\u6709\u591a\u5c11\u6807\u5fd7\uff09\uff0c\u6216 std::bitset \u3002 1 2 3 #include // for std::bitset std :: bitset < 8 > mybitset {}; // 8 bits in size means room for 8 flags \u6700\u4f73\u5b9e\u8df5 \u4f4d\u64cd\u4f5c\u662f\u5c11\u6570\u5e94\u8be5\u660e\u786e\u4f7f\u7528\u65e0\u7b26\u53f7\u6574\u6570\uff08\u6216 std\uff1a\uff1abitset\uff09\u7684\u60c5\u51b5\u4e4b\u4e00\u3002 \u5728\u672c\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u5c55\u793a\u5982\u4f55\u901a\u8fc7 std::bitset \u4ee5\u7b80\u5355\u7684\u65b9\u5f0f\u8fdb\u884c\u4f4d\u64cd\u4f5c\u3002\u5728\u4e0b\u4e00\u7ec4\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u4ee5\u66f4\u56f0\u96be\u4f46\u7528\u9014\u5e7f\u6cdb\u7684\u65b9\u5f0f\u505a\u5230\u8fd9\u4e00\u70b9\u3002","title":"\u4f4d\u6807\u5fd7"},{"location":"O-1-bit-flags-and-bit-manipulation-via-std-bitset/#_2","text":"\u7ed9\u5b9a\u4e00\u4e2a\u4f4d\u5e8f\u5217\uff0c\u6211\u4eec\u901a\u5e38\u4ece\u53f3\u5230\u5de6\u5bf9\u4f4d\u8fdb\u884c\u7f16\u53f7\uff0c \u4ece 0\uff08\u800c\u4e0d\u662f 1\uff09\u5f00\u59cb \u3002\u6bcf\u4e2a\u6570\u5b57\u8868\u793a\u4e00\u4e2a \u4f4d\u4f4d\u7f6e \u3002 1 2 76543210 Bit position 00000101 Bit sequence \u7ed9\u5b9a\u4f4d\u5e8f\u5217 0000 0101\uff0c\u4f4d\u4e8e\u4f4d\u7f6e 0 \u548c 2 \u7684\u4f4d\u7684\u503c\u4e3a 1\uff0c\u5176\u4ed6\u4f4d\u7684\u503c\u4e3a 0\u3002","title":"\u4f4d\u7f16\u53f7\u548c\u4f4d\u4f4d\u7f6e"},{"location":"O-1-bit-flags-and-bit-manipulation-via-std-bitset/#stdbitset","text":"\u5728 \u7b2c 4.16 \u8bfe\u2014\u2014\u6570\u5b57\u7cfb\u7edf\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236 \u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 std::bitset \u6253\u5370\u4e8c\u8fdb\u5236\u503c\u3002\u7136\u800c\uff0c\u8fd9\u5e76\u4e0d\u662f std::bitset \u80fd\u505a\u7684\u552f\u4e00\u6709\u7528\u7684\u4e8b\u60c5\u3002 std::bitset \u63d0\u4f9b\u4e86 4 \u4e2a\u53ef\u7528\u4e8e\u4f4d\u64cd\u4f5c\u7684\u5173\u952e\u51fd\u6570\uff1a test() \u5141\u8bb8\u6211\u4eec\u67e5\u8be2\u67d0\u4e2a\u4f4d\u662f 0 \u8fd8\u662f 1 set() \u5141\u8bb8\u6211\u4eec\u6253\u5f00\u4e00\u4e2a\u4f4d\uff08\u5982\u679c\u4f4d\u5df2\u7ecf\u6253\u5f00\uff0c\u8fd9\u5c06\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\uff09 reset() \u5141\u8bb8\u6211\u4eec\u5173\u95ed\u4e00\u4e2a\u4f4d\uff08\u5982\u679c\u8be5\u4f4d\u5df2\u7ecf\u5173\u95ed\uff0c\u8fd9\u5c06\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\uff09 flip() \u5141\u8bb8\u6211\u4eec\u5c06\u4f4d\u503c\u4ece 0 \u7ffb\u8f6c\u4e3a 1\uff0c\u53cd\u4e4b\u4ea6\u7136 \u8fd9\u4e9b\u51fd\u6570\u4e2d\u7684\u6bcf\u4e00\u4e2a\u90fd\u5c06\u6211\u4eec\u8981\u64cd\u4f5c\u7684\u4f4d\u7684\u4f4d\u7f6e\u4f5c\u4e3a\u5b83\u4eec\u552f\u4e00\u7684\u53c2\u6570\u3002 \u8fd9\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include int main () { std :: bitset < 8 > bits { 0b0000'0101 }; // we need 8 bits, start with bit pattern 0000 0101 bits . set ( 3 ); // set bit position 3 to 1 (now we have 0000 1101) bits . flip ( 4 ); // flip bit 4 (now we have 0001 1101) bits . reset ( 4 ); // set bit 4 back to 0 (now we have 0000 1101) std :: cout << \"All the bits: \" << bits << '\\n' ; std :: cout << \"Bit 3 has value: \" << bits . test ( 3 ) << '\\n' ; std :: cout << \"Bit 4 has value: \" << bits . test ( 4 ) << '\\n' ; return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 3 All the bits: 00001101 Bit 3 has value: 1 Bit 4 has value: 0 \u5c0f\u8d34\u58eb \u5982\u679c\u60a8\u9700\u8981\u590d\u4e60\u4ec0\u4e48\u662f 0b \u524d\u7f00\u6216 ' \u5206\u9694\u7b26\uff0c\u8bf7\u67e5\u770b 4.16 - \u6570\u5b57\u7cfb\u7edf\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236","title":"\u901a\u8fc7 std::bitset \u64cd\u4f5c\u4f4d"},{"location":"O-1-bit-flags-and-bit-manipulation-via-std-bitset/#_3","text":"std::bitset \u5e76\u4e0d\u5bb9\u6613\u5b9e\u73b0\u8fd9\u4e2a\u9700\u6c42\u3002\u4e3a\u4e86\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u6216\u8005\u5982\u679c\u6211\u4eec\u60f3\u4f7f\u7528\u65e0\u7b26\u53f7\u6574\u6570\u4f4d\u6807\u5fd7\u800c\u4e0d\u662f std::bitset\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u66f4\u4f20\u7edf\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u51e0\u8282\u8bfe\u4e2d\u4ecb\u7ecd\u8fd9\u4e9b\u5185\u5bb9\u3002","title":"\u5982\u679c\u6211\u4eec\u60f3\u4e00\u6b21\u83b7\u53d6\u6216\u8bbe\u7f6e\u591a\u4e2a\u4f4d\u600e\u4e48\u529e"},{"location":"O-1-bit-flags-and-bit-manipulation-via-std-bitset/#stdbitset_1","text":"\u4e00\u4e2a\u6f5c\u5728\u7684\u60ca\u559c\u662f std::bitset \u9488\u5bf9\u901f\u5ea6\u8fdb\u884c\u4e86\u4f18\u5316 \uff0c\u800c\u4e0d\u662f\u8282\u7701\u5185\u5b58\u3002std::bitset \u7684\u5927\u5c0f\u901a\u5e38\u662f\u4fdd\u5b58\u4f4d\u6240\u9700\u7684\u5b57\u8282\u6570\uff0c \u56db\u820d\u4e94\u5165\u5230(rounded up to) \u6700\u63a5\u8fd1\u7684 sizeof(size_t) \uff0c\u5728 32 \u4f4d\u673a\u5668\u4e0a\u4e3a 4 \u5b57\u8282\uff0c\u5728 64 \u4f4d\u673a\u5668\u4e0a\u4e3a 8 \u5b57\u8282\u3002 \u56e0\u6b64\uff0c std::bitset<8> \u901a\u5e38\u4f7f\u7528 4 \u6216 8 \u5b57\u8282\u7684\u5185\u5b58\uff0c\u5373\u4f7f\u5b83\u5728\u6280\u672f\u4e0a\u53ea\u9700\u8981 1 \u4e2a\u5b57\u8282\u6765\u5b58\u50a8 8 \u4f4d\u3002\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u60f3\u8981\u65b9\u4fbf\u800c\u4e0d\u662f\u8282\u7701\u5185\u5b58\u65f6\uff0cstd::bitset \u662f\u6700\u6709\u7528\u7684\u3002","title":"std::bitset \u7684\u5927\u5c0f"},{"location":"O-2-bitwise-operators/","tags":["bit"],"text":"Key Takeaway \u6309\u4f4d\u8fd0\u7b97\u7b26 C++ \u63d0\u4f9b 6 \u4f4d\u64cd\u4f5c\u8fd0\u7b97\u7b26\uff0c\u901a\u5e38\u79f0\u4e3a \u6309\u4f4d\u8fd0\u7b97 \u7b26\uff1a Operator Symbol Form Operation \u5de6\u79fb << x << y x \u4e2d\u7684\u6240\u6709\u4f4d\u5de6\u79fb y \u4f4d \u53f3\u79fb >> x >> y x \u4e2d\u7684\u6240\u6709\u4f4d\u53f3\u79fb y \u4f4d \u6309\u4f4d\u975e ~ ~x x \u4e2d\u7684\u6240\u6709\u4f4d\u7ffb\u8f6c \u6309\u4f4d\u4e0e & x & y x \u4e2d\u7684\u6bcf\u4e00\u4f4d AND y \u4e2d\u7684\u6bcf\u4e00\u4f4d \u6309\u4f4d\u6216 | \u00d7 | y x \u4e2d\u7684\u6bcf\u4e00\u4f4d OR y \u4e2d\u7684\u6bcf\u4e00\u4f4d \u6309\u4f4d\u5f02\u6216 ^ x^y x \u4e2d\u7684\u6bcf\u4e00\u4f4d XOR y \u4e2d\u7684\u6bcf\u4e00\u4f4d \u4f5c\u8005\u6ce8 \u5728\u4ee5\u4e0b\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4e3b\u8981\u4f7f\u7528 4 \u4f4d\u4e8c\u8fdb\u5236\u503c\u3002\u8fd9\u662f\u4e3a\u4e86\u65b9\u4fbf\u548c\u4fdd\u6301\u793a\u4f8b\u7b80\u5355\u3002\u5728\u5b9e\u9645\u7a0b\u5e8f\u4e2d\uff0c\u4f7f\u7528\u7684\u4f4d\u6570\u53d6\u51b3\u4e8e\u5bf9\u8c61\u7684\u5927\u5c0f\uff08\u4f8b\u5982\uff0c\u4e00\u4e2a 2 \u5b57\u8282\u7684\u5bf9\u8c61\u5c06\u5b58\u50a8 16 \u4f4d\uff09\u3002 \u4e3a\u4e86\u53ef\u8bfb\u6027\uff0c\u6211\u4eec\u8fd8\u5c06\u7701\u7565\u4ee3\u7801\u793a\u4f8b\u4e4b\u5916\u7684 0b \u524d\u7f00\uff08\u4f8b\u5982\uff0c\u6211\u4eec\u5c06\u53ea\u4f7f\u7528 0101 \u800c\u4e0d\u662f 0b0101\uff09\u3002 \u6309\u4f4d\u8fd0\u7b97\u7b26\u662f\u4e3a\u6574\u6570\u7c7b\u578b\u548c std::bitset \u5b9a\u4e49\u7684\u3002\u6211\u4eec\u5c06\u5728\u793a\u4f8b\u4e2d\u4f7f\u7528 std::bitset\uff0c\u56e0\u4e3a\u5b83\u66f4\u5bb9\u6613\u4ee5\u4e8c\u8fdb\u5236\u5f62\u5f0f\u6253\u5370\u8f93\u51fa\u3002 \u907f\u514d\u4f7f\u7528\u5e26\u7b26\u53f7\u64cd\u4f5c\u6570\u7684\u6309\u4f4d\u8fd0\u7b97\u7b26 \uff0c\u56e0\u4e3a\u5728 C++20 \u4e4b\u524d\uff0c\u8bb8\u591a\u8fd0\u7b97\u7b26\u5c06\u8fd4\u56de\u5b9e\u73b0\u5b9a\u4e49\u7684\u7ed3\u679c\uff0c\u6216\u6709\u7740\u4f7f\u7528\u65e0\u7b26\u53f7\u64cd\u4f5c\u6570\uff08\u6216 std::bitset\uff09\u53ef\u4ee5\u8f7b\u677e\u907f\u514d\u7684\u5176\u4ed6\u6f5c\u5728\u95ee\u9898\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e3a\u907f\u514d\u610f\u5916\uff0c\u8bf7\u4f7f\u7528\u5e26\u65e0\u7b26\u53f7\u64cd\u4f5c\u6570\u7684\u6309\u4f4d\u8fd0\u7b97\u7b26\u6216 std::bitset\u3002 \u6309\u4f4d\u5de6\u79fb (<<) \u548c\u6309\u4f4d\u53f3\u79fb (>>) \u8fd0\u7b97\u7b26 \u6309\u4f4d\u5de6\u79fb (<<) \u8fd0\u7b97\u7b26\u5c06\u4f4d\u5411\u5de6\u79fb\u52a8\u3002\u5de6\u64cd\u4f5c\u6570\u662f\u8981\u79fb\u4f4d\u7684\u4f4d\u7684\u8868\u8fbe\u5f0f\uff0c\u53f3\u64cd\u4f5c\u6570\u662f\u8981\u5de6\u79fb\u7684\u6574\u6570\u4f4d\u6570\u3002 \u6240\u4ee5\u5f53\u6211\u4eec\u8bf4 \u65f6 x << 1 \uff0c\u6211\u4eec\u662f\u5728\u8bf4\u201c\u5c06\u53d8\u91cf x \u4e2d\u7684\u4f4d\u5de6\u79fb\u4e00\u4f4d\u201d\u3002\u4ece\u53f3\u4fa7\u79fb\u5165\u7684\u65b0\u4f4d\u63a5\u6536\u503c 0\u3002 0011 << 1 \u662f 0110 0011 << 2 \u662f 1100 0011 << 3 \u662f 1000 \u8bf7\u6ce8\u610f\uff0c\u5728\u7b2c\u4e09\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5c06\u4e00\u4e2a1\u4ece\u5de6\u7aef\u504f\u79fb\u51fa\u4e86\uff01\u4ece\u4e8c\u8fdb\u5236\u6570\u672b\u5c3e\u79fb\u51fa\u7684\u4f4d\u5c06\u6c38\u8fdc\u4e22\u5931\u3002 \u6309\u4f4d\u53f3\u79fb (>>) \u8fd0\u7b97\u7b26\u5c06\u4f4d\u53f3\u79fb\u3002 1100 >> 1 \u662f 0110 1100 >> 2 \u662f 0011 1100 >> 3 \u662f 0001 \u8bf7\u6ce8\u610f\uff0c\u5728\u7b2c\u4e09\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5c06\u4e00\u4e2a1\u4ece\u53f3\u7aef\u504f\u79fb\u51fa\u4e86\uff0c\u56e0\u6b64\u5b83\u4e22\u5931\u4e86\u3002 \u8fd9\u662f\u8fdb\u884c\u4e00\u4e9b\u4f4d\u79fb\u7684\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include #include int main () { std :: bitset < 4 > x { 0b1100 }; std :: cout << x << '\\n' ; std :: cout << ( x >> 1 ) << '\\n' ; // shift right by 1, yielding 0110 std :: cout << ( x << 1 ) << '\\n' ; // shift left by 1, yielding 1000 return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 3 1100 0110 1000 \u4ec0\u4e48\uff01\uff1foperator<< \u548c operator>> \u4e0d\u662f\u7528\u4e8e\u8f93\u5165\u548c\u8f93\u51fa\u5417\uff1f \u4ed6\u4eec\u80af\u5b9a\u662f\u3002 \u4eca\u5929\u7684\u7a0b\u5e8f\u901a\u5e38\u4e0d\u4f1a\u5927\u91cf\u4f7f\u7528\u6309\u4f4d\u5de6\u79fb\u548c\u53f3\u79fb\u8fd0\u7b97\u7b26\u6765\u79fb\u4f4d\u3002\u76f8\u53cd\uff0c\u60a8\u503e\u5411\u4e8e\u770b\u5230\u4e0e std::cout\uff08\u6216\u5176\u4ed6\u6d41\u5bf9\u8c61\uff09\u4e00\u8d77\u4f7f\u7528\u7684\u6309\u4f4d\u5de6\u79fb\u8fd0\u7b97\u7b26\u6765\u8f93\u51fa\u6587\u672c\u3002\u8003\u8651\u4ee5\u4e0b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include #include int main () { unsigned int x { 0b0100 }; x = x << 1 ; // use operator<< for left shift std :: cout << std :: bitset < 4 > { x } << '\\n' ; // use operator<< for output return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u6253\u5370\uff1a 1 1000 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0coperator<< \u662f\u5982\u4f55\u77e5\u9053\u5728\u4e00\u79cd\u60c5\u51b5\u4e0b\u79fb\u4f4d\u800c\u5728\u53e6\u4e00\u79cd\u60c5\u51b5\u4e0b\u8f93\u51fa x \u7684\u5462\uff1f\u7b54\u6848\u662f std::cout \u5df2\u7ecf \u91cd\u8f7d \uff08\u63d0\u4f9b\u4e86\u66ff\u4ee3\u5b9a\u4e49\uff09 operator<< \u6267\u884c\u63a7\u5236\u53f0\u8f93\u51fa\u800c\u4e0d\u662f\u4f4d\u79fb\u3002 \u5f53\u7f16\u8bd1\u5668\u770b\u5230 operator<< \u7684\u5de6\u64cd\u4f5c\u6570\u662f std::cout \u65f6\uff0c\u5b83\u77e5\u9053\u5b83\u5e94\u8be5\u8c03\u7528 std::cout \u91cd\u8f7d\u7684 operator<< \u7248\u672c\u6765\u8fdb\u884c\u8f93\u51fa\u3002\u5982\u679c\u5de6\u64cd\u4f5c\u6570\u662f\u6574\u6570\u7c7b\u578b\uff0c\u5219 operator<< \u77e5\u9053\u5b83\u5e94\u8be5\u6267\u884c\u5176\u901a\u5e38\u7684\u4f4d\u79fb\u884c\u4e3a\u3002 \u8fd9\u540c\u6837\u9002\u7528\u4e8e\u8fd0\u7b97\u7b26>>\u3002 \u8bf7\u6ce8\u610f\uff0c\u5982\u679c\u60a8\u5bf9\u8f93\u51fa\u548c\u5de6\u79fb\u90fd\u4f7f\u7528\u8fd0\u7b97\u7b26 <<\uff0c\u5219\u9700\u8981\u62ec\u53f7\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include #include int main () { std :: bitset < 4 > x { 0b0110 }; std :: cout << x << 1 << '\\n' ; // print value of x (0110), then 1 std :: cout << ( x << 1 ) << '\\n' ; // print x left shifted by 1 (1100) return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 01101 1100 \u7b2c\u4e00\u884c\u6253\u5370 x \u7684\u503c (0110)\uff0c\u7136\u540e\u662f\u6587\u5b57 1\u3002\u7b2c\u4e8c\u884c\u6253\u5370 x \u5de6\u79fb 1 (1100) \u7684\u503c\u3002 \u6211\u4eec\u5c06\u5728\u4ee5\u540e\u7684\u90e8\u5206\u4e2d\u66f4\u591a\u5730\u8ba8\u8bba\u8fd0\u7b97\u7b26\u91cd\u8f7d\uff0c\u5305\u62ec\u8ba8\u8bba\u5982\u4f55\u4e3a\u60a8\u81ea\u5df1\u7684\u76ee\u7684\u91cd\u8f7d\u8fd0\u7b97\u7b26\u3002 \u6309\u4f4d\u975e \u6309\u4f4d\u975e \u8fd0\u7b97\u7b26 (~) \u53ef\u80fd\u662f\u6240\u6709\u6309\u4f4d\u8fd0\u7b97\u7b26\u4e2d\u6700\u5bb9\u6613\u7406\u89e3\u7684\u3002\u5b83\u53ea\u662f\u5c06\u6bcf\u4e2a\u4f4d\u4ece 0 \u7ffb\u8f6c\u4e3a 1\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u8bf7\u6ce8\u610f\uff0c \u6309\u4f4d NOT \u7684\u7ed3\u679c\u53d6\u51b3\u4e8e\u60a8\u7684\u6570\u636e\u7c7b\u578b\u7684\u5927\u5c0f\u3002 \u7ffb\u8f6c 4 \u4f4d\uff1a ~0100 \u662f 1011 \u7ffb\u8f6c 8 \u4f4d\uff1a ~0000 0100 \u662f 1111 1011 \u5728 4 \u4f4d\u548c 8 \u4f4d\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4ece\u76f8\u540c\u7684\u6570\u5b57\u5f00\u59cb\uff08\u4e8c\u8fdb\u5236 0100 \u4e0e 0000 0100 \u76f8\u540c\uff0c\u5341\u8fdb\u5236 7 \u4e0e 07 \u76f8\u540c\uff09\uff0c\u4f46\u6700\u7ec8\u5f97\u5230\u4e0d\u540c\u7684\u7ed3\u679c\u3002 \u6211\u4eec\u53ef\u4ee5\u5728\u4ee5\u4e0b\u7a0b\u5e8f\u4e2d\u770b\u5230\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 7 8 9 #include #include int main () { std :: cout << ~ std :: bitset < 4 > { 0b0100 } << ' ' << ~ std :: bitset < 8 > { 0b0100 } << '\\n' ; return 0 ; } ```\u8fd9\u6253\u5370\uff1a \u8fd9\u6253\u5370\uff1a 1011 11111011 1 2 3 4 5 ## \u6309\u4f4d\u6216 **\u6309\u4f4d\u6216**(|) \u7684\u5de5\u4f5c\u65b9\u5f0f\u4e0e\u5176\u5bf9\u5e94\u7684*\u903b\u8f91\u6216*\u975e\u5e38\u76f8\u4f3c\u3002\u4f46\u662f\uff0c\u4e0d\u662f\u5c06*OR*\u5e94\u7528\u4e8e\u64cd\u4f5c\u6570\u4ee5\u4ea7\u751f\u5355\u4e2a\u7ed3\u679c\uff0c\u800c\u662f*\u5c06\u6309\u4f4d OR*\u5e94\u7528\u4e8e\u6bcf\u4e00\u4f4d\uff01\u4f8b\u5982\uff0c\u8003\u8651\u8868\u8fbe\u5f0f`0b0101 | 0b0110`\u3002 \u8981\u8fdb\u884c\uff08\u4efb\u4f55\uff09\u6309\u4f4d\u8fd0\u7b97\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u50cf\u8fd9\u6837\u5c06\u4e24\u4e2a\u64cd\u4f5c\u6570\u6392\u5217\u8d77\u6765\uff1a 0 1 0 1 OR 0 1 1 0 1 2 3 \u7136\u540e\u5c06\u64cd\u4f5c\u5e94\u7528\u4e8e\u6bcf\u4e00*\u5217\u4f4d*\u3002 \u5982\u679c\u60a8\u8fd8\u8bb0\u5f97\u7684\u8bdd\uff0c\u5982\u679c\u5de6\u64cd\u4f5c\u6570\u3001\u53f3\u64cd\u4f5c\u6570\u6216\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u4e3a*\u771f (1) \uff0c\u5219**\u903b\u8f91 OR*\u7684\u8ba1\u7b97\u7ed3\u679c\u4e3a*\u771f (1* ) \uff0c\u5426\u5219*\u4e3a 0*\u3002\u5982\u679c\u5de6\u4fa7\u4f4d\u3001\u53f3\u4fa7\u4f4d\u6216\u4e24\u4e2a\u4f4d\u90fd\u4e3a*1*\uff0c*\u5219\u6309\u4f4d\u6216*\u8ba1\u7b97\u7ed3\u679c\u4e3a*1 \uff0c\u5426\u5219\u4e3a**0*\u3002\u56e0\u6b64\uff0c\u8868\u8fbe\u5f0f\u7684\u8ba1\u7b97\u7ed3\u679c\u5982\u4e0b\uff1a 0 1 0 1 OR 0 1 1 0 0 1 1 1 1 2 3 4 5 6 7 8 9 10 11 12 \u6211\u4eec\u7684\u7ed3\u679c\u662f 0111 \u4e8c\u8fdb\u5236\u3002 ```cpp #include #include int main() { std::cout << (std::bitset<4>{ 0b0101 } | std::bitset<4>{ 0b0110 }) << '\\n'; return 0; } \u8fd9\u6253\u5370\uff1a 1 0111 \u6211\u4eec\u53ef\u4ee5\u5bf9\u590d\u5408 OR \u8868\u8fbe\u5f0f\u505a\u540c\u6837\u7684\u4e8b\u60c5\uff0c\u4f8b\u5982 0b0111 | 0b0011 | 0b0001 . \u5982\u679c\u4e00\u5217\u4e2d\u7684\u4efb\u4f55\u4f4d\u4e3a 1 \uff0c\u5219\u8be5\u5217\u7684\u7ed3\u679c\u4e3a 1 \u3002 1 2 3 4 5 0 1 1 1 OR 0 0 1 1 OR 0 0 0 1 -------- 0 1 1 1 \u8fd9\u662f\u4e0a\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 #include #include int main () { std :: cout << ( std :: bitset < 4 > { 0b0111 } | std :: bitset < 4 > { 0b0011 } | std :: bitset < 4 > { 0b0001 }) << '\\n' ; return 0 ; } \u8fd9\u6253\u5370\uff1a 1 0111 \u6309\u4f4d\u4e0e \u6309\u4f4d\u4e0e (&) \u7684\u5de5\u4f5c\u65b9\u5f0f\u4e0e\u4e0a\u8ff0\u7c7b\u4f3c\u3002\u5982\u679c\u5de6\u53f3\u64cd\u4f5c\u6570\u7684\u8ba1\u7b97\u7ed3\u679c\u90fd\u4e3a true \uff0c\u5219 \u903b\u8f91 AND\u7684\u8ba1\u7b97\u7ed3\u679c\u4e3a true \u3002\u5982\u679c\u5217\u4e2d\u7684\u4e24\u4f4d\u90fd\u4e3a 1 \uff0c\u5219\u6309\u4f4d\u4e0e \u8ba1\u7b97\u7ed3\u679c\u4e3a \u771f (1) \u3002\u8003\u8651\u8868\u8fbe\u5f0f\u3002\u6392\u5217\u6bcf\u4e2a\u4f4d\u5e76\u5bf9\u6bcf\u4e00\u5217\u4f4d\u5e94\u7528 AND \u8fd0\u7b97\uff1a 0b0101 & 0b0110 1 2 3 4 0 1 0 1 AND 0 1 1 0 -------- 0 1 0 0 1 2 3 4 5 6 7 8 #include #include int main () { std :: cout << ( std :: bitset < 4 > { 0b0101 } & std :: bitset < 4 > { 0b0110 }) << '\\n' ; return 0 ; } \u8fd9\u6253\u5370\uff1a 1 0100 \u540c\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u590d\u5408 AND \u8868\u8fbe\u5f0f\u505a\u540c\u6837\u7684\u4e8b\u60c5\uff0c\u4f8b\u5982 0b0001 & 0b0011 & 0b0111 . \u5982\u679c\u4e00\u5217\u4e2d\u7684\u6240\u6709\u4f4d\u5747\u4e3a 1\uff0c\u5219\u8be5\u5217\u7684\u7ed3\u679c\u4e3a 1\u3002 1 2 3 4 5 0 0 0 1 AND 0 0 1 1 AND 0 1 1 1 -------- 0 0 0 1 1 2 3 4 5 6 7 8 9 #include #include int main () { std :: cout << ( std :: bitset < 4 > { 0b0001 } & std :: bitset < 4 > { 0b0011 } & std :: bitset < 4 > { 0b0111 }) << '\\n' ; return 0 ; } \u8fd9\u6253\u5370\uff1a 1 0001 \u6309\u4f4d\u5f02\u6216 \u6700\u540e\u4e00\u4e2a\u8fd0\u7b97\u7b26\u662f \u6309\u4f4d\u5f02\u6216 (^)\uff0c\u4e5f\u79f0\u4e3a \u5f02\u6216 \u3002 \u8bc4\u4f30\u4e24\u4e2a\u64cd\u4f5c\u6570\u65f6\uff0c\u5982\u679c XOR \u7684\u4e00\u4e2a \u4e14\u53ea\u6709\u4e00\u4e2a \u64cd\u4f5c\u6570\u4e3atrue (1) \uff0c\u5219 XOR \u7684\u8ba1\u7b97\u7ed3\u679c\u4e3a true (1) \u3002\u5982\u679c\u4e24\u8005\u90fd\u4e0d\u4e3a\u771f\uff0c\u5219\u8ba1\u7b97\u7ed3\u679c\u4e3a 0 \u3002\u8003\u8651\u8868\u8fbe\u5f0f\uff1a 0b0110 ^ 0b0011 1 2 3 4 0 1 1 0 XOR 0 0 1 1 ------- 0 1 0 1 \u4e5f\u53ef\u4ee5\u8bc4\u4f30\u590d\u5408 XOR \u8868\u8fbe\u5f0f\u5217\u6837\u5f0f\uff0c\u4f8b\u5982 0b0001 ^ 0b0011 ^ 0b0111 . \u5982\u679c\u4e00\u5217\u4e2d\u6709\u5076\u6570\u4e2a 1 \u4f4d\uff0c\u5219\u7ed3\u679c\u4e3a 0 \u3002\u5982\u679c\u4e00\u5217\u4e2d\u6709\u5947\u6570\u4e2a 1 \u4f4d\uff0c\u5219\u7ed3\u679c\u4e3a 1 \u3002 1 2 3 4 5 0 0 0 1 XOR 0 0 1 1 XOR 0 1 1 1 -------- 0 1 0 1 \u6309\u4f4d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u4e0e\u7b97\u672f\u8d4b\u503c\u8fd0\u7b97\u7b26\u7c7b\u4f3c\uff0cC++ \u63d0\u4f9b\u4e86\u6309\u4f4d\u8d4b\u503c\u8fd0\u7b97\u7b26\uff0c\u4ee5\u65b9\u4fbf\u53d8\u91cf\u7684\u4fee\u6539\u3002 Operator Symbol Form Operation \u5de6\u79fb\u5206\u914d <<= x <<= y x \u5de6\u79fb y \u4f4d \u53f3\u79fb\u5206\u914d >>= x >>= y x \u53f3\u79fb y \u4f4d \u6309\u4f4d\u6216\u8d4b\u503c |= x |= y \u5c06 x | y \u8d4b\u503c\u7ed9x \u6309\u4f4d\u4e0e\u8d4b\u503c &= x &= y \u5c06 x & y \u8d4b\u503c\u7ed9 x \u6309\u4f4d\u5f02\u6216\u8d4b\u503c ^= x^=y \u5c06 x ^ y \u8d4b\u503c\u7ed9 x \u4f8b\u5982 x = x >> 1; \uff0c\u60a8\u53ef\u4ee5\u4e0d\u5199 \uff0c\u800c\u5199 x >>= 1; . 1 2 3 4 5 6 7 8 9 10 11 #include #include int main () { std :: bitset < 4 > bits { 0b0100 }; bits >>= 1 ; std :: cout << bits << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u6253\u5370\uff1a 1 0010 \u6982\u62ec \u603b\u7ed3\u5982\u4f55\u4f7f\u7528\u5217\u65b9\u6cd5\u6309\u4f4d\u8fd0\u7b97\uff1a \u5728\u8bc4\u4f30 \u6309\u4f4d\u6216 \u65f6\uff0c\u5982\u679c\u5217\u4e2d\u7684\u4efb\u4f55\u4f4d\u4e3a 1\uff0c\u5219\u8be5\u5217\u7684\u7ed3\u679c\u4e3a 1\u3002 \u5728\u8bc4\u4f30 \u6309\u4f4d\u4e0e \u65f6\uff0c\u5982\u679c\u5217\u4e2d\u7684\u6240\u6709\u4f4d\u5747\u4e3a 1\uff0c\u5219\u8be5\u5217\u7684\u7ed3\u679c\u4e3a 1\u3002 \u5728\u8bc4\u4f30 \u6309\u4f4d\u5f02\u6216 \u65f6\uff0c\u5982\u679c\u4e00\u5217\u4e2d\u6709\u5947\u6570\u4e2a 1 \u4f4d\uff0c\u8be5\u5217\u7684\u7ed3\u679c\u4e3a 1\u3002","title":"O.2 - \u4f4d\u8fd0\u7b97\u7b26"},{"location":"O-2-bitwise-operators/#_1","text":"C++ \u63d0\u4f9b 6 \u4f4d\u64cd\u4f5c\u8fd0\u7b97\u7b26\uff0c\u901a\u5e38\u79f0\u4e3a \u6309\u4f4d\u8fd0\u7b97 \u7b26\uff1a Operator Symbol Form Operation \u5de6\u79fb << x << y x \u4e2d\u7684\u6240\u6709\u4f4d\u5de6\u79fb y \u4f4d \u53f3\u79fb >> x >> y x \u4e2d\u7684\u6240\u6709\u4f4d\u53f3\u79fb y \u4f4d \u6309\u4f4d\u975e ~ ~x x \u4e2d\u7684\u6240\u6709\u4f4d\u7ffb\u8f6c \u6309\u4f4d\u4e0e & x & y x \u4e2d\u7684\u6bcf\u4e00\u4f4d AND y \u4e2d\u7684\u6bcf\u4e00\u4f4d \u6309\u4f4d\u6216 | \u00d7 | y x \u4e2d\u7684\u6bcf\u4e00\u4f4d OR y \u4e2d\u7684\u6bcf\u4e00\u4f4d \u6309\u4f4d\u5f02\u6216 ^ x^y x \u4e2d\u7684\u6bcf\u4e00\u4f4d XOR y \u4e2d\u7684\u6bcf\u4e00\u4f4d \u4f5c\u8005\u6ce8 \u5728\u4ee5\u4e0b\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4e3b\u8981\u4f7f\u7528 4 \u4f4d\u4e8c\u8fdb\u5236\u503c\u3002\u8fd9\u662f\u4e3a\u4e86\u65b9\u4fbf\u548c\u4fdd\u6301\u793a\u4f8b\u7b80\u5355\u3002\u5728\u5b9e\u9645\u7a0b\u5e8f\u4e2d\uff0c\u4f7f\u7528\u7684\u4f4d\u6570\u53d6\u51b3\u4e8e\u5bf9\u8c61\u7684\u5927\u5c0f\uff08\u4f8b\u5982\uff0c\u4e00\u4e2a 2 \u5b57\u8282\u7684\u5bf9\u8c61\u5c06\u5b58\u50a8 16 \u4f4d\uff09\u3002 \u4e3a\u4e86\u53ef\u8bfb\u6027\uff0c\u6211\u4eec\u8fd8\u5c06\u7701\u7565\u4ee3\u7801\u793a\u4f8b\u4e4b\u5916\u7684 0b \u524d\u7f00\uff08\u4f8b\u5982\uff0c\u6211\u4eec\u5c06\u53ea\u4f7f\u7528 0101 \u800c\u4e0d\u662f 0b0101\uff09\u3002 \u6309\u4f4d\u8fd0\u7b97\u7b26\u662f\u4e3a\u6574\u6570\u7c7b\u578b\u548c std::bitset \u5b9a\u4e49\u7684\u3002\u6211\u4eec\u5c06\u5728\u793a\u4f8b\u4e2d\u4f7f\u7528 std::bitset\uff0c\u56e0\u4e3a\u5b83\u66f4\u5bb9\u6613\u4ee5\u4e8c\u8fdb\u5236\u5f62\u5f0f\u6253\u5370\u8f93\u51fa\u3002 \u907f\u514d\u4f7f\u7528\u5e26\u7b26\u53f7\u64cd\u4f5c\u6570\u7684\u6309\u4f4d\u8fd0\u7b97\u7b26 \uff0c\u56e0\u4e3a\u5728 C++20 \u4e4b\u524d\uff0c\u8bb8\u591a\u8fd0\u7b97\u7b26\u5c06\u8fd4\u56de\u5b9e\u73b0\u5b9a\u4e49\u7684\u7ed3\u679c\uff0c\u6216\u6709\u7740\u4f7f\u7528\u65e0\u7b26\u53f7\u64cd\u4f5c\u6570\uff08\u6216 std::bitset\uff09\u53ef\u4ee5\u8f7b\u677e\u907f\u514d\u7684\u5176\u4ed6\u6f5c\u5728\u95ee\u9898\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e3a\u907f\u514d\u610f\u5916\uff0c\u8bf7\u4f7f\u7528\u5e26\u65e0\u7b26\u53f7\u64cd\u4f5c\u6570\u7684\u6309\u4f4d\u8fd0\u7b97\u7b26\u6216 std::bitset\u3002","title":"\u6309\u4f4d\u8fd0\u7b97\u7b26"},{"location":"O-2-bitwise-operators/#_2","text":"\u6309\u4f4d\u5de6\u79fb (<<) \u8fd0\u7b97\u7b26\u5c06\u4f4d\u5411\u5de6\u79fb\u52a8\u3002\u5de6\u64cd\u4f5c\u6570\u662f\u8981\u79fb\u4f4d\u7684\u4f4d\u7684\u8868\u8fbe\u5f0f\uff0c\u53f3\u64cd\u4f5c\u6570\u662f\u8981\u5de6\u79fb\u7684\u6574\u6570\u4f4d\u6570\u3002 \u6240\u4ee5\u5f53\u6211\u4eec\u8bf4 \u65f6 x << 1 \uff0c\u6211\u4eec\u662f\u5728\u8bf4\u201c\u5c06\u53d8\u91cf x \u4e2d\u7684\u4f4d\u5de6\u79fb\u4e00\u4f4d\u201d\u3002\u4ece\u53f3\u4fa7\u79fb\u5165\u7684\u65b0\u4f4d\u63a5\u6536\u503c 0\u3002 0011 << 1 \u662f 0110 0011 << 2 \u662f 1100 0011 << 3 \u662f 1000 \u8bf7\u6ce8\u610f\uff0c\u5728\u7b2c\u4e09\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5c06\u4e00\u4e2a1\u4ece\u5de6\u7aef\u504f\u79fb\u51fa\u4e86\uff01\u4ece\u4e8c\u8fdb\u5236\u6570\u672b\u5c3e\u79fb\u51fa\u7684\u4f4d\u5c06\u6c38\u8fdc\u4e22\u5931\u3002 \u6309\u4f4d\u53f3\u79fb (>>) \u8fd0\u7b97\u7b26\u5c06\u4f4d\u53f3\u79fb\u3002 1100 >> 1 \u662f 0110 1100 >> 2 \u662f 0011 1100 >> 3 \u662f 0001 \u8bf7\u6ce8\u610f\uff0c\u5728\u7b2c\u4e09\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5c06\u4e00\u4e2a1\u4ece\u53f3\u7aef\u504f\u79fb\u51fa\u4e86\uff0c\u56e0\u6b64\u5b83\u4e22\u5931\u4e86\u3002 \u8fd9\u662f\u8fdb\u884c\u4e00\u4e9b\u4f4d\u79fb\u7684\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include #include int main () { std :: bitset < 4 > x { 0b1100 }; std :: cout << x << '\\n' ; std :: cout << ( x >> 1 ) << '\\n' ; // shift right by 1, yielding 0110 std :: cout << ( x << 1 ) << '\\n' ; // shift left by 1, yielding 1000 return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 3 1100 0110 1000","title":"\u6309\u4f4d\u5de6\u79fb (<<) \u548c\u6309\u4f4d\u53f3\u79fb (>>) \u8fd0\u7b97\u7b26"},{"location":"O-2-bitwise-operators/#operator-operator","text":"\u4ed6\u4eec\u80af\u5b9a\u662f\u3002 \u4eca\u5929\u7684\u7a0b\u5e8f\u901a\u5e38\u4e0d\u4f1a\u5927\u91cf\u4f7f\u7528\u6309\u4f4d\u5de6\u79fb\u548c\u53f3\u79fb\u8fd0\u7b97\u7b26\u6765\u79fb\u4f4d\u3002\u76f8\u53cd\uff0c\u60a8\u503e\u5411\u4e8e\u770b\u5230\u4e0e std::cout\uff08\u6216\u5176\u4ed6\u6d41\u5bf9\u8c61\uff09\u4e00\u8d77\u4f7f\u7528\u7684\u6309\u4f4d\u5de6\u79fb\u8fd0\u7b97\u7b26\u6765\u8f93\u51fa\u6587\u672c\u3002\u8003\u8651\u4ee5\u4e0b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include #include int main () { unsigned int x { 0b0100 }; x = x << 1 ; // use operator<< for left shift std :: cout << std :: bitset < 4 > { x } << '\\n' ; // use operator<< for output return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u6253\u5370\uff1a 1 1000 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0coperator<< \u662f\u5982\u4f55\u77e5\u9053\u5728\u4e00\u79cd\u60c5\u51b5\u4e0b\u79fb\u4f4d\u800c\u5728\u53e6\u4e00\u79cd\u60c5\u51b5\u4e0b\u8f93\u51fa x \u7684\u5462\uff1f\u7b54\u6848\u662f std::cout \u5df2\u7ecf \u91cd\u8f7d \uff08\u63d0\u4f9b\u4e86\u66ff\u4ee3\u5b9a\u4e49\uff09 operator<< \u6267\u884c\u63a7\u5236\u53f0\u8f93\u51fa\u800c\u4e0d\u662f\u4f4d\u79fb\u3002 \u5f53\u7f16\u8bd1\u5668\u770b\u5230 operator<< \u7684\u5de6\u64cd\u4f5c\u6570\u662f std::cout \u65f6\uff0c\u5b83\u77e5\u9053\u5b83\u5e94\u8be5\u8c03\u7528 std::cout \u91cd\u8f7d\u7684 operator<< \u7248\u672c\u6765\u8fdb\u884c\u8f93\u51fa\u3002\u5982\u679c\u5de6\u64cd\u4f5c\u6570\u662f\u6574\u6570\u7c7b\u578b\uff0c\u5219 operator<< \u77e5\u9053\u5b83\u5e94\u8be5\u6267\u884c\u5176\u901a\u5e38\u7684\u4f4d\u79fb\u884c\u4e3a\u3002 \u8fd9\u540c\u6837\u9002\u7528\u4e8e\u8fd0\u7b97\u7b26>>\u3002 \u8bf7\u6ce8\u610f\uff0c\u5982\u679c\u60a8\u5bf9\u8f93\u51fa\u548c\u5de6\u79fb\u90fd\u4f7f\u7528\u8fd0\u7b97\u7b26 <<\uff0c\u5219\u9700\u8981\u62ec\u53f7\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include #include int main () { std :: bitset < 4 > x { 0b0110 }; std :: cout << x << 1 << '\\n' ; // print value of x (0110), then 1 std :: cout << ( x << 1 ) << '\\n' ; // print x left shifted by 1 (1100) return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 01101 1100 \u7b2c\u4e00\u884c\u6253\u5370 x \u7684\u503c (0110)\uff0c\u7136\u540e\u662f\u6587\u5b57 1\u3002\u7b2c\u4e8c\u884c\u6253\u5370 x \u5de6\u79fb 1 (1100) \u7684\u503c\u3002 \u6211\u4eec\u5c06\u5728\u4ee5\u540e\u7684\u90e8\u5206\u4e2d\u66f4\u591a\u5730\u8ba8\u8bba\u8fd0\u7b97\u7b26\u91cd\u8f7d\uff0c\u5305\u62ec\u8ba8\u8bba\u5982\u4f55\u4e3a\u60a8\u81ea\u5df1\u7684\u76ee\u7684\u91cd\u8f7d\u8fd0\u7b97\u7b26\u3002","title":"\u4ec0\u4e48\uff01\uff1foperator<< \u548c operator>> \u4e0d\u662f\u7528\u4e8e\u8f93\u5165\u548c\u8f93\u51fa\u5417\uff1f"},{"location":"O-2-bitwise-operators/#_3","text":"\u6309\u4f4d\u975e \u8fd0\u7b97\u7b26 (~) \u53ef\u80fd\u662f\u6240\u6709\u6309\u4f4d\u8fd0\u7b97\u7b26\u4e2d\u6700\u5bb9\u6613\u7406\u89e3\u7684\u3002\u5b83\u53ea\u662f\u5c06\u6bcf\u4e2a\u4f4d\u4ece 0 \u7ffb\u8f6c\u4e3a 1\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u8bf7\u6ce8\u610f\uff0c \u6309\u4f4d NOT \u7684\u7ed3\u679c\u53d6\u51b3\u4e8e\u60a8\u7684\u6570\u636e\u7c7b\u578b\u7684\u5927\u5c0f\u3002 \u7ffb\u8f6c 4 \u4f4d\uff1a ~0100 \u662f 1011 \u7ffb\u8f6c 8 \u4f4d\uff1a ~0000 0100 \u662f 1111 1011 \u5728 4 \u4f4d\u548c 8 \u4f4d\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4ece\u76f8\u540c\u7684\u6570\u5b57\u5f00\u59cb\uff08\u4e8c\u8fdb\u5236 0100 \u4e0e 0000 0100 \u76f8\u540c\uff0c\u5341\u8fdb\u5236 7 \u4e0e 07 \u76f8\u540c\uff09\uff0c\u4f46\u6700\u7ec8\u5f97\u5230\u4e0d\u540c\u7684\u7ed3\u679c\u3002 \u6211\u4eec\u53ef\u4ee5\u5728\u4ee5\u4e0b\u7a0b\u5e8f\u4e2d\u770b\u5230\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 7 8 9 #include #include int main () { std :: cout << ~ std :: bitset < 4 > { 0b0100 } << ' ' << ~ std :: bitset < 8 > { 0b0100 } << '\\n' ; return 0 ; } ```\u8fd9\u6253\u5370\uff1a \u8fd9\u6253\u5370\uff1a 1011 11111011 1 2 3 4 5 ## \u6309\u4f4d\u6216 **\u6309\u4f4d\u6216**(|) \u7684\u5de5\u4f5c\u65b9\u5f0f\u4e0e\u5176\u5bf9\u5e94\u7684*\u903b\u8f91\u6216*\u975e\u5e38\u76f8\u4f3c\u3002\u4f46\u662f\uff0c\u4e0d\u662f\u5c06*OR*\u5e94\u7528\u4e8e\u64cd\u4f5c\u6570\u4ee5\u4ea7\u751f\u5355\u4e2a\u7ed3\u679c\uff0c\u800c\u662f*\u5c06\u6309\u4f4d OR*\u5e94\u7528\u4e8e\u6bcf\u4e00\u4f4d\uff01\u4f8b\u5982\uff0c\u8003\u8651\u8868\u8fbe\u5f0f`0b0101 | 0b0110`\u3002 \u8981\u8fdb\u884c\uff08\u4efb\u4f55\uff09\u6309\u4f4d\u8fd0\u7b97\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u50cf\u8fd9\u6837\u5c06\u4e24\u4e2a\u64cd\u4f5c\u6570\u6392\u5217\u8d77\u6765\uff1a 0 1 0 1 OR 0 1 1 0 1 2 3 \u7136\u540e\u5c06\u64cd\u4f5c\u5e94\u7528\u4e8e\u6bcf\u4e00*\u5217\u4f4d*\u3002 \u5982\u679c\u60a8\u8fd8\u8bb0\u5f97\u7684\u8bdd\uff0c\u5982\u679c\u5de6\u64cd\u4f5c\u6570\u3001\u53f3\u64cd\u4f5c\u6570\u6216\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u4e3a*\u771f (1) \uff0c\u5219**\u903b\u8f91 OR*\u7684\u8ba1\u7b97\u7ed3\u679c\u4e3a*\u771f (1* ) \uff0c\u5426\u5219*\u4e3a 0*\u3002\u5982\u679c\u5de6\u4fa7\u4f4d\u3001\u53f3\u4fa7\u4f4d\u6216\u4e24\u4e2a\u4f4d\u90fd\u4e3a*1*\uff0c*\u5219\u6309\u4f4d\u6216*\u8ba1\u7b97\u7ed3\u679c\u4e3a*1 \uff0c\u5426\u5219\u4e3a**0*\u3002\u56e0\u6b64\uff0c\u8868\u8fbe\u5f0f\u7684\u8ba1\u7b97\u7ed3\u679c\u5982\u4e0b\uff1a 0 1 0 1 OR 0 1 1 0 0 1 1 1 1 2 3 4 5 6 7 8 9 10 11 12 \u6211\u4eec\u7684\u7ed3\u679c\u662f 0111 \u4e8c\u8fdb\u5236\u3002 ```cpp #include #include int main() { std::cout << (std::bitset<4>{ 0b0101 } | std::bitset<4>{ 0b0110 }) << '\\n'; return 0; } \u8fd9\u6253\u5370\uff1a 1 0111 \u6211\u4eec\u53ef\u4ee5\u5bf9\u590d\u5408 OR \u8868\u8fbe\u5f0f\u505a\u540c\u6837\u7684\u4e8b\u60c5\uff0c\u4f8b\u5982 0b0111 | 0b0011 | 0b0001 . \u5982\u679c\u4e00\u5217\u4e2d\u7684\u4efb\u4f55\u4f4d\u4e3a 1 \uff0c\u5219\u8be5\u5217\u7684\u7ed3\u679c\u4e3a 1 \u3002 1 2 3 4 5 0 1 1 1 OR 0 0 1 1 OR 0 0 0 1 -------- 0 1 1 1 \u8fd9\u662f\u4e0a\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 #include #include int main () { std :: cout << ( std :: bitset < 4 > { 0b0111 } | std :: bitset < 4 > { 0b0011 } | std :: bitset < 4 > { 0b0001 }) << '\\n' ; return 0 ; } \u8fd9\u6253\u5370\uff1a 1 0111","title":"\u6309\u4f4d\u975e"},{"location":"O-2-bitwise-operators/#_4","text":"\u6309\u4f4d\u4e0e (&) \u7684\u5de5\u4f5c\u65b9\u5f0f\u4e0e\u4e0a\u8ff0\u7c7b\u4f3c\u3002\u5982\u679c\u5de6\u53f3\u64cd\u4f5c\u6570\u7684\u8ba1\u7b97\u7ed3\u679c\u90fd\u4e3a true \uff0c\u5219 \u903b\u8f91 AND\u7684\u8ba1\u7b97\u7ed3\u679c\u4e3a true \u3002\u5982\u679c\u5217\u4e2d\u7684\u4e24\u4f4d\u90fd\u4e3a 1 \uff0c\u5219\u6309\u4f4d\u4e0e \u8ba1\u7b97\u7ed3\u679c\u4e3a \u771f (1) \u3002\u8003\u8651\u8868\u8fbe\u5f0f\u3002\u6392\u5217\u6bcf\u4e2a\u4f4d\u5e76\u5bf9\u6bcf\u4e00\u5217\u4f4d\u5e94\u7528 AND \u8fd0\u7b97\uff1a 0b0101 & 0b0110 1 2 3 4 0 1 0 1 AND 0 1 1 0 -------- 0 1 0 0 1 2 3 4 5 6 7 8 #include #include int main () { std :: cout << ( std :: bitset < 4 > { 0b0101 } & std :: bitset < 4 > { 0b0110 }) << '\\n' ; return 0 ; } \u8fd9\u6253\u5370\uff1a 1 0100 \u540c\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u590d\u5408 AND \u8868\u8fbe\u5f0f\u505a\u540c\u6837\u7684\u4e8b\u60c5\uff0c\u4f8b\u5982 0b0001 & 0b0011 & 0b0111 . \u5982\u679c\u4e00\u5217\u4e2d\u7684\u6240\u6709\u4f4d\u5747\u4e3a 1\uff0c\u5219\u8be5\u5217\u7684\u7ed3\u679c\u4e3a 1\u3002 1 2 3 4 5 0 0 0 1 AND 0 0 1 1 AND 0 1 1 1 -------- 0 0 0 1 1 2 3 4 5 6 7 8 9 #include #include int main () { std :: cout << ( std :: bitset < 4 > { 0b0001 } & std :: bitset < 4 > { 0b0011 } & std :: bitset < 4 > { 0b0111 }) << '\\n' ; return 0 ; } \u8fd9\u6253\u5370\uff1a 1 0001","title":"\u6309\u4f4d\u4e0e"},{"location":"O-2-bitwise-operators/#_5","text":"\u6700\u540e\u4e00\u4e2a\u8fd0\u7b97\u7b26\u662f \u6309\u4f4d\u5f02\u6216 (^)\uff0c\u4e5f\u79f0\u4e3a \u5f02\u6216 \u3002 \u8bc4\u4f30\u4e24\u4e2a\u64cd\u4f5c\u6570\u65f6\uff0c\u5982\u679c XOR \u7684\u4e00\u4e2a \u4e14\u53ea\u6709\u4e00\u4e2a \u64cd\u4f5c\u6570\u4e3atrue (1) \uff0c\u5219 XOR \u7684\u8ba1\u7b97\u7ed3\u679c\u4e3a true (1) \u3002\u5982\u679c\u4e24\u8005\u90fd\u4e0d\u4e3a\u771f\uff0c\u5219\u8ba1\u7b97\u7ed3\u679c\u4e3a 0 \u3002\u8003\u8651\u8868\u8fbe\u5f0f\uff1a 0b0110 ^ 0b0011 1 2 3 4 0 1 1 0 XOR 0 0 1 1 ------- 0 1 0 1 \u4e5f\u53ef\u4ee5\u8bc4\u4f30\u590d\u5408 XOR \u8868\u8fbe\u5f0f\u5217\u6837\u5f0f\uff0c\u4f8b\u5982 0b0001 ^ 0b0011 ^ 0b0111 . \u5982\u679c\u4e00\u5217\u4e2d\u6709\u5076\u6570\u4e2a 1 \u4f4d\uff0c\u5219\u7ed3\u679c\u4e3a 0 \u3002\u5982\u679c\u4e00\u5217\u4e2d\u6709\u5947\u6570\u4e2a 1 \u4f4d\uff0c\u5219\u7ed3\u679c\u4e3a 1 \u3002 1 2 3 4 5 0 0 0 1 XOR 0 0 1 1 XOR 0 1 1 1 -------- 0 1 0 1","title":"\u6309\u4f4d\u5f02\u6216"},{"location":"O-2-bitwise-operators/#_6","text":"\u4e0e\u7b97\u672f\u8d4b\u503c\u8fd0\u7b97\u7b26\u7c7b\u4f3c\uff0cC++ \u63d0\u4f9b\u4e86\u6309\u4f4d\u8d4b\u503c\u8fd0\u7b97\u7b26\uff0c\u4ee5\u65b9\u4fbf\u53d8\u91cf\u7684\u4fee\u6539\u3002 Operator Symbol Form Operation \u5de6\u79fb\u5206\u914d <<= x <<= y x \u5de6\u79fb y \u4f4d \u53f3\u79fb\u5206\u914d >>= x >>= y x \u53f3\u79fb y \u4f4d \u6309\u4f4d\u6216\u8d4b\u503c |= x |= y \u5c06 x | y \u8d4b\u503c\u7ed9x \u6309\u4f4d\u4e0e\u8d4b\u503c &= x &= y \u5c06 x & y \u8d4b\u503c\u7ed9 x \u6309\u4f4d\u5f02\u6216\u8d4b\u503c ^= x^=y \u5c06 x ^ y \u8d4b\u503c\u7ed9 x \u4f8b\u5982 x = x >> 1; \uff0c\u60a8\u53ef\u4ee5\u4e0d\u5199 \uff0c\u800c\u5199 x >>= 1; . 1 2 3 4 5 6 7 8 9 10 11 #include #include int main () { std :: bitset < 4 > bits { 0b0100 }; bits >>= 1 ; std :: cout << bits << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u6253\u5370\uff1a 1 0010","title":"\u6309\u4f4d\u8d4b\u503c\u8fd0\u7b97\u7b26"},{"location":"O-2-bitwise-operators/#_7","text":"\u603b\u7ed3\u5982\u4f55\u4f7f\u7528\u5217\u65b9\u6cd5\u6309\u4f4d\u8fd0\u7b97\uff1a \u5728\u8bc4\u4f30 \u6309\u4f4d\u6216 \u65f6\uff0c\u5982\u679c\u5217\u4e2d\u7684\u4efb\u4f55\u4f4d\u4e3a 1\uff0c\u5219\u8be5\u5217\u7684\u7ed3\u679c\u4e3a 1\u3002 \u5728\u8bc4\u4f30 \u6309\u4f4d\u4e0e \u65f6\uff0c\u5982\u679c\u5217\u4e2d\u7684\u6240\u6709\u4f4d\u5747\u4e3a 1\uff0c\u5219\u8be5\u5217\u7684\u7ed3\u679c\u4e3a 1\u3002 \u5728\u8bc4\u4f30 \u6309\u4f4d\u5f02\u6216 \u65f6\uff0c\u5982\u679c\u4e00\u5217\u4e2d\u6709\u5947\u6570\u4e2a 1 \u4f4d\uff0c\u8be5\u5217\u7684\u7ed3\u679c\u4e3a 1\u3002","title":"\u6982\u62ec"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/","tags":["bit"],"text":"Key Takeaway \u4f4d\u63a9\u7801 \u4e3a\u4e86\u64cd\u4f5c\u5355\u4e2a\u4f4d\uff08\u4f8b\u5982\u6253\u5f00\u6216\u5173\u95ed\u5b83\u4eec\uff09\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e9b\u65b9\u6cd5\u6765\u8bc6\u522b\u6211\u4eec\u60f3\u8981\u64cd\u4f5c\u7684\u7279\u5b9a\u4f4d\u3002\u4e0d\u5e78\u7684\u662f\uff0c\u6309\u4f4d\u8fd0\u7b97\u7b26\u4e0d\u77e5\u9053\u5982\u4f55\u5904\u7406\u4f4d\u4f4d\u7f6e\u3002\u76f8\u53cd\uff0c\u4ed6\u4eec\u4f7f\u7528\u4f4d\u63a9\u7801\u3002 \u4f4d\u63a9\u7801 \u662f\u4e00\u7ec4\u9884\u5b9a\u4e49\u7684\u4f4d\uff0c\u7528\u4e8e\u9009\u62e9\u54ea\u4e9b\u7279\u5b9a\u4f4d\u5c06\u88ab\u540e\u7eed\u64cd\u4f5c\u4fee\u6539\u3002 \u8003\u8651\u4e00\u4e2a\u771f\u5b9e\u7684\u6848\u4f8b\uff0c\u60a8\u60f3\u8981\u7ed8\u5236\u4e00\u4e2a\u7a97\u6846\u3002\u5982\u679c\u60a8\u4e0d\u5c0f\u5fc3\uff0c\u60a8\u4e0d\u4ec5\u4f1a\u5192\u7740\u7c89\u5237\u7a97\u6846\u7684\u98ce\u9669\uff0c\u8fd8\u4f1a\u5192\u7740\u7c89\u5237\u73bb\u7483\u672c\u8eab\u7684\u98ce\u9669\u3002\u60a8\u53ef\u4ee5\u8d2d\u4e70\u4e00\u4e9b\u906e\u853d\u80f6\u5e26\u5e76\u5c06\u5176\u8d34\u5728\u73bb\u7483\u548c\u60a8\u4e0d\u60f3\u6d82\u6f06\u7684\u4efb\u4f55\u5176\u4ed6\u90e8\u4ef6\u4e0a\u3002\u7136\u540e\uff0c\u5f53\u60a8\u7ed8\u753b\u65f6\uff0c\u906e\u853d\u80f6\u5e26\u4f1a\u963b\u6b62\u6cb9\u6f06\u63a5\u89e6\u5230\u60a8\u4e0d\u60f3\u6d82\u6f06\u7684\u4efb\u4f55\u4e1c\u897f\u3002\u6700\u540e\uff0c\u53ea\u6709\u672a\u906e\u7f69\u7684\u90e8\u5206\uff08\u60a8\u8981\u7ed8\u5236\u7684\u90e8\u5206\uff09\u88ab\u7ed8\u5236\u3002 \u4f4d\u63a9\u7801\u672c\u8d28\u4e0a\u5bf9\u4f4d\u6267\u884c\u76f8\u540c\u7684\u529f\u80fd\u2014\u2014 \u4f4d\u63a9\u7801\u963b\u6b62\u6309\u4f4d\u8fd0\u7b97\u7b26\u63a5\u89e6\u6211\u4eec\u4e0d\u60f3\u4fee\u6539\u7684\u4f4d\uff0c\u5e76\u5141\u8bb8\u8bbf\u95ee\u6211\u4eec\u786e\u5b9e\u60f3\u8981\u4fee\u6539\u7684\u4f4d\u3002 \u8ba9\u6211\u4eec\u9996\u5148\u63a2\u8ba8\u5982\u4f55\u5b9a\u4e49\u4e00\u4e9b\u7b80\u5355\u7684\u4f4d\u63a9\u7801\uff0c\u7136\u540e\u6211\u4eec\u5c06\u5411\u60a8\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u5b83\u4eec\u3002 \u5728 C++14 \u4e2d\u5b9a\u4e49\u4f4d\u63a9\u7801 \u6700\u7b80\u5355\u7684\u4e00\u7ec4\u4f4d\u63a9\u7801\u662f\u4e3a\u6bcf\u4e2a\u4f4d\u4f4d\u7f6e\u5b9a\u4e49\u4e00\u4e2a\u4f4d\u63a9\u7801\u3002\u6211\u4eec\u7528 0 \u6765\u5c4f\u853d\u6211\u4eec\u4e0d\u5173\u5fc3\u7684\u4f4d\uff0c\u7528 1 \u6765\u8868\u793a\u6211\u4eec\u60f3\u8981\u4fee\u6539\u7684\u4f4d\u3002 \u5c3d\u7ba1\u4f4d\u63a9\u7801\u53ef\u4ee5\u662f\u6587\u5b57\uff0c\u4f46\u5b83\u4eec\u901a\u5e38\u88ab\u5b9a\u4e49\u4e3a\u7b26\u53f7\u5e38\u91cf\uff0c\u56e0\u6b64\u53ef\u4ee5\u4e3a\u5b83\u4eec\u6307\u5b9a\u4e00\u4e2a\u6709\u610f\u4e49\u7684\u540d\u79f0\u5e76\u6613\u4e8e\u91cd\u7528\u3002 \u56e0\u4e3a C++14 \u652f\u6301\u4e8c\u8fdb\u5236\u6587\u5b57\uff0c\u6240\u4ee5\u5b9a\u4e49\u8fd9\u4e9b\u4f4d\u63a9\u7801\u5f88\u5bb9\u6613\uff1a 1 2 3 4 5 6 7 8 9 10 #include constexpr std :: uint8_t mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: uint8_t mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: uint8_t mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: uint8_t mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: uint8_t mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: uint8_t mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: uint8_t mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: uint8_t mask7 { 0b1000'0000 }; // represents bit 7 \u73b0\u5728\u6211\u4eec\u6709\u4e00\u7ec4\u4ee3\u8868\u6bcf\u4e2a\u4f4d\u4f4d\u7f6e\u7684\u7b26\u53f7\u5e38\u91cf\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u6765\u64cd\u4f5c\u4f4d\uff08\u7a0d\u540e\u6211\u4eec\u5c06\u5c55\u793a\u5982\u4f55\u64cd\u4f5c\uff09\u3002 \u5728 C++11 \u6216\u66f4\u65e9\u7248\u672c\u4e2d\u5b9a\u4e49\u4f4d\u63a9\u7801 \u56e0\u4e3a C++11 \u4e0d\u652f\u6301\u4e8c\u8fdb\u5236\u6587\u5b57 \uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u6765\u8bbe\u7f6e\u7b26\u53f7\u5e38\u91cf\u3002\u6709\u4e24\u79cd\u5f88\u597d\u7684\u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\u3002 \u7b2c\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u5341\u516d\u8fdb\u5236\u6587\u5b57\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u5728 4.15-\u5b57\u9762\u91cf \u4e2d\u8ba8\u8bba\u4e86\u5341\u516d\u8fdb\u5236\u3002 \u5341\u516d\u8fdb\u5236\u8f6c\u4e8c\u8fdb\u5236\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a \u5341\u516d\u8fdb\u5236 0 1 2 3 4 5 6 7 8 9 A B C D E F \u4e8c\u8fdb\u5236 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u50cf\u8fd9\u6837\u4f7f\u7528\u5341\u516d\u8fdb\u5236\u5b9a\u4e49\u4f4d\u63a9\u7801\uff1a 1 2 3 4 5 6 7 8 constexpr std :: uint8_t mask0 { 0x01 }; // hex for 0000 0001 constexpr std :: uint8_t mask1 { 0x02 }; // hex for 0000 0010 constexpr std :: uint8_t mask2 { 0x04 }; // hex for 0000 0100 constexpr std :: uint8_t mask3 { 0x08 }; // hex for 0000 1000 constexpr std :: uint8_t mask4 { 0x10 }; // hex for 0001 0000 constexpr std :: uint8_t mask5 { 0x20 }; // hex for 0010 0000 constexpr std :: uint8_t mask6 { 0x40 }; // hex for 0100 0000 constexpr std :: uint8_t mask7 { 0x80 }; // hex for 1000 0000 \u6709\u65f6\u4f1a\u7701\u7565\u524d\u5bfc\u7684\u5341\u516d\u8fdb\u5236 0\uff08\u4f8b\u5982\uff0c 0x01 \u60a8\u53ea\u4f1a\u770b\u5230 0x1 \uff09\u3002\u65e0\u8bba\u54ea\u79cd\u65b9\u5f0f\uff0c\u5982\u679c\u60a8\u4e0d\u719f\u6089\u5341\u516d\u8fdb\u5236\u5230\u4e8c\u8fdb\u5236\u7684\u8f6c\u6362\uff0c\u8fd9\u53ef\u80fd\u6709\u70b9\u96be\u4ee5\u9605\u8bfb\u3002 \u4e00\u79cd\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5de6\u79fb\u8fd0\u7b97\u7b26\u5c06\u4e00\u4f4d\u79fb\u52a8\u5230\u6b63\u786e\u7684\u4f4d\u7f6e\uff1a 1 2 3 4 5 6 7 8 constexpr std :: uint8_t mask0 { 1 << 0 }; // 0000 0001 constexpr std :: uint8_t mask1 { 1 << 1 }; // 0000 0010 constexpr std :: uint8_t mask2 { 1 << 2 }; // 0000 0100 constexpr std :: uint8_t mask3 { 1 << 3 }; // 0000 1000 constexpr std :: uint8_t mask4 { 1 << 4 }; // 0001 0000 constexpr std :: uint8_t mask5 { 1 << 5 }; // 0010 0000 constexpr std :: uint8_t mask6 { 1 << 6 }; // 0100 0000 constexpr std :: uint8_t mask7 { 1 << 7 }; // 1000 0000 \u6d4b\u8bd5\u4e00\u4e0b\uff08\u770b\u5b83\u662f1\u8fd8\u662f0\uff09 \u73b0\u5728\u6211\u4eec\u6709\u4e86\u4e00\u7ec4\u4f4d\u63a9\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u4eec\u4e0e\u4f4d\u6807\u5fd7\u53d8\u91cf\u7ed3\u5408\u4f7f\u7528\u6765\u64cd\u4f5c\u6211\u4eec\u7684\u4f4d\u6807\u5fd7\u3002 \u8981\u786e\u5b9a\u67d0\u4e2a\u4f4d\u662f\u5f00\u8fd8\u662f\u5173\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u4e0e \u7ed3\u5408\u76f8\u5e94\u4f4d\u7684\u4f4d\u63a9\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include int main () { constexpr std :: uint8_t mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: uint8_t mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: uint8_t mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: uint8_t mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: uint8_t mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: uint8_t mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: uint8_t mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: uint8_t mask7 { 0b1000'0000 }; // represents bit 7 std :: uint8_t flags { 0b0000'0101 }; // 8 bits in size means room for 8 flags std :: cout << \"bit 0 is \" << (( flags & mask0 ) ? \"on \\n \" : \"off \\n \" ); std :: cout << \"bit 1 is \" << (( flags & mask1 ) ? \"on \\n \" : \"off \\n \" ); return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 bit 1 is off bit 1 is on \u7f6e1\u4e00\u4f4d \u8981\u8bbe\u7f6e\uff08\u7f6e1\uff09\u4f4d\uff0c\u6211\u4eec\u5c06\u6309\u4f4d\u6216\u7b49\u4e8e\uff08\u8fd0\u7b97\u7b26 |=\uff09\u4e0e\u76f8\u5e94\u4f4d\u7684\u4f4d\u63a9\u7801\u7ed3\u5408\u4f7f\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include int main () { constexpr std :: uint8_t mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: uint8_t mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: uint8_t mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: uint8_t mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: uint8_t mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: uint8_t mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: uint8_t mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: uint8_t mask7 { 0b1000'0000 }; // represents bit 7 std :: uint8_t flags { 0b0000'0101 }; // 8 bits in size means room for 8 flags std :: cout << \"bit 1 is \" << (( flags & mask1 ) ? \"on \\n \" : \"off \\n \" ); flags |= mask1 ; // turn on bit 1 std :: cout << \"bit 1 is \" << (( flags & mask1 ) ? \"on \\n \" : \"off \\n \" ); return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 bit 1 is off bit 1 is on \u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6309\u4f4d\u6216 \u540c\u65f6\u7f6e1\u591a\u4e2a\u4f4d\uff1a 1 flags |= ( mask4 | mask5 ); // turn bits 4 and 5 on at the same time \u7f6e0\u4e00\u4f4d \u8981\u6e05\u9664\u4f4d\uff08\u7f6e0\uff09\uff0c\u6211\u4eec\u540c\u65f6\u4f7f\u7528 \u6309\u4f4d\u4e0e \u548c \u6309\u4f4d\u975e \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include int main () { constexpr std :: uint8_t mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: uint8_t mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: uint8_t mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: uint8_t mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: uint8_t mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: uint8_t mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: uint8_t mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: uint8_t mask7 { 0b1000'0000 }; // represents bit 7 std :: uint8_t flags { 0b0000'0101 }; // 8 bits in size means room for 8 flags std :: cout << \"bit 2 is \" << (( flags & mask2 ) ? \"on \\n \" : \"off \\n \" ); flags &= ~ mask2 ; // turn off bit 2 std :: cout << \"bit 2 is \" << (( flags & mask2 ) ? \"on \\n \" : \"off \\n \" ); return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 bit 2 is on bit 2 is off \u6211\u4eec\u53ef\u4ee5\u540c\u65f6\u5173\u95ed\u591a\u4e2a\u4f4d\uff1a 1 flags &= ~ ( mask4 | mask5 ); // turn bits 4 and 5 off at the same time \u7ffb\u8f6c\u4e00\u4f4d \u8981\u5207\u6362\u4f4d\u72b6\u6001\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u5f02\u6216 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include int main () { constexpr std :: uint8_t mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: uint8_t mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: uint8_t mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: uint8_t mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: uint8_t mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: uint8_t mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: uint8_t mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: uint8_t mask7 { 0b1000'0000 }; // represents bit 7 std :: uint8_t flags { 0b0000'0101 }; // 8 bits in size means room for 8 flags std :: cout << \"bit 2 is \" << (( flags & mask2 ) ? \"on \\n \" : \"off \\n \" ); flags ^= mask2 ; // flip bit 2 std :: cout << \"bit 2 is \" << (( flags & mask2 ) ? \"on \\n \" : \"off \\n \" ); flags ^= mask2 ; // flip bit 2 std :: cout << \"bit 2 is \" << (( flags & mask2 ) ? \"on \\n \" : \"off \\n \" ); return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 3 bit 2 is on bit 2 is off bit 2 is on \u6211\u4eec\u53ef\u4ee5\u540c\u65f6\u7ffb\u8f6c\u591a\u4e2a\u4f4d\uff1a 1 flags ^= ( mask4 | mask5 ); // flip bits 4 and 5 at the same time \u4f4d\u63a9\u7801\u548c std::bitset std::bitset \u652f\u6301\u5168\u5957\u4f4d\u8fd0\u7b97\u7b26\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u4f7f\u7528\u51fd\u6570\uff08\u6d4b\u8bd5\u3001\u8bbe\u7f6e\u3001\u91cd\u7f6e\u548c\u7ffb\u8f6c\uff09\u4fee\u6539\u5355\u4e2a\u4f4d\u66f4\u5bb9\u6613\uff0c\u4f46\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4f7f\u7528\u6309\u4f4d\u8fd0\u7b97\u7b26\u548c\u4f4d\u63a9\u7801\u3002 \u4f60\u4e3a\u4ec0\u4e48\u8981\u8fd9\u6837\u505a\uff1f \u8fd9\u4e9b\u51fd\u6570test(),set(),reset(),flip()\u53ea\u5141\u8bb8\u60a8\u4fee\u6539\u5355\u4e2a\u4f4d\u3002\u6309\u4f4d\u8fd0\u7b97\u7b26\u5141\u8bb8\u60a8\u4e00\u6b21\u4fee\u6539\u591a\u4e2a\u4f4d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include #include #include int main () { constexpr std :: bitset < 8 > mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: bitset < 8 > mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: bitset < 8 > mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: bitset < 8 > mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: bitset < 8 > mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: bitset < 8 > mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: bitset < 8 > mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: bitset < 8 > mask7 { 0b1000'0000 }; // represents bit 7 std :: bitset < 8 > flags { 0b0000'0101 }; // 8 bits in size means room for 8 flags std :: cout << \"bit 1 is \" << ( flags . test ( 1 ) ? \"on \\n \" : \"off \\n \" ); std :: cout << \"bit 2 is \" << ( flags . test ( 2 ) ? \"on \\n \" : \"off \\n \" ); flags ^= ( mask1 | mask2 ); // flip bits 1 and 2 std :: cout << \"bit 1 is \" << ( flags . test ( 1 ) ? \"on \\n \" : \"off \\n \" ); std :: cout << \"bit 2 is \" << ( flags . test ( 2 ) ? \"on \\n \" : \"off \\n \" ); flags |= ( mask1 | mask2 ); // turn bits 1 and 2 on std :: cout << \"bit 1 is \" << ( flags . test ( 1 ) ? \"on \\n \" : \"off \\n \" ); std :: cout << \"bit 2 is \" << ( flags . test ( 2 ) ? \"on \\n \" : \"off \\n \" ); flags &= ~ ( mask1 | mask2 ); // turn bits 1 and 2 off std :: cout << \"bit 1 is \" << ( flags . test ( 1 ) ? \"on \\n \" : \"off \\n \" ); std :: cout << \"bit 2 is \" << ( flags . test ( 2 ) ? \"on \\n \" : \"off \\n \" ); return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 3 4 5 6 7 8 bit 1 is off bit 2 is on bit 1 is on bit 2 is off bit 1 is on bit 2 is on bit 1 is off bit 2 is off \u4f7f\u4f4d\u63a9\u7801\u6709\u610f\u4e49 \u5c06\u6211\u4eec\u7684\u4f4d\u63a9\u7801\u547d\u540d\u4e3a\u201cmask1\u201d\u6216\u201cmask2\u201d\u53ef\u4ee5\u544a\u8bc9\u6211\u4eec\u6b63\u5728\u64cd\u4f5c\u7684\u662f\u54ea\u4e2a\u4f4d\uff0c\u4f46\u4e0d\u4f1a\u544a\u8bc9\u6211\u4eec\u8be5\u4f4d\u6807\u5fd7\u5b9e\u9645\u7528\u4e8e\u4ec0\u4e48\u3002 \u6700\u4f73\u505a\u6cd5\u662f\u4e3a\u60a8\u7684\u4f4d\u63a9\u7801\u63d0\u4f9b\u6709\u7528\u7684\u540d\u79f0\uff0c\u4f5c\u4e3a\u8bb0\u5f55\u4f4d\u6807\u5fd7\u542b\u4e49\u7684\u4e00\u79cd\u65b9\u5f0f\u3002\u8fd9\u662f\u6211\u4eec\u53ef\u80fd\u7f16\u5199\u7684\u6e38\u620f\u7684\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include int main () { // Define a bunch of physical/emotional states constexpr std :: uint8_t isHungry { 1 << 0 }; // 0000 0001 constexpr std :: uint8_t isSad { 1 << 1 }; // 0000 0010 constexpr std :: uint8_t isMad { 1 << 2 }; // 0000 0100 constexpr std :: uint8_t isHappy { 1 << 3 }; // 0000 1000 constexpr std :: uint8_t isLaughing { 1 << 4 }; // 0001 0000 constexpr std :: uint8_t isAsleep { 1 << 5 }; // 0010 0000 constexpr std :: uint8_t isDead { 1 << 6 }; // 0100 0000 constexpr std :: uint8_t isCrying { 1 << 7 }; // 1000 0000 std :: uint8_t me {}; // all flags/options turned off to start me |= ( isHappy | isLaughing ); // I am happy and laughing me &= ~ isLaughing ; // I am no longer laughing // Query a few states // (we'll use static_cast to interpret the results as a boolean value) std :: cout << \"I am happy? \" << static_cast < bool > ( me & isHappy ) << '\\n' ; std :: cout << \"I am laughing? \" << static_cast < bool > ( me & isLaughing ) << '\\n' ; return 0 ; } \u4e0b\u9762\u662f\u4f7f\u7528 std::bitset \u5b9e\u73b0\u7684\u76f8\u540c\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include int main () { // Define a bunch of physical/emotional states constexpr std :: bitset < 8 > isHungry { 0b0000'0001 }; constexpr std :: bitset < 8 > isSad { 0b0000'0010 }; constexpr std :: bitset < 8 > isMad { 0b0000'0100 }; constexpr std :: bitset < 8 > isHappy { 0b0000'1000 }; constexpr std :: bitset < 8 > isLaughing { 0b0001'0000 }; constexpr std :: bitset < 8 > isAsleep { 0b0010'0000 }; constexpr std :: bitset < 8 > isDead { 0b0100'0000 }; constexpr std :: bitset < 8 > isCrying { 0b1000'0000 }; std :: bitset < 8 > me {}; // all flags/options turned off to start me |= ( isHappy | isLaughing ); // I am happy and laughing me &= ~ isLaughing ; // I am no longer laughing // Query a few states (we use the any() function to see if any bits remain set) std :: cout << \"I am happy? \" << ( me & isHappy ). any () << '\\n' ; std :: cout << \"I am laughing? \" << ( me & isLaughing ). any () << '\\n' ; return 0 ; } \u8fd9\u91cc\u6709\u4e24\u4e2a\u6ce8\u610f\u4e8b\u9879\uff1a\u9996\u5148\uff0cstd::bitset \u6ca1\u6709\u5141\u8bb8\u60a8\u4f7f\u7528\u4f4d\u63a9\u7801\u67e5\u8be2\u4f4d\u7684\u597d\u51fd\u6570\u3002\u56e0\u6b64\uff0c\u5982\u679c\u60a8\u60f3\u4f7f\u7528\u4f4d\u63a9\u7801\u800c\u4e0d\u662f\u4f4d\u7f6e\u7d22\u5f15\uff0c\u5219\u5fc5\u987b\u4f7f\u7528 \u6309\u4f4d\u4e0e \u67e5\u8be2\u4f4d\u3002\u5176\u6b21\uff0c\u6211\u4eec\u4f7f\u7528 any() \u51fd\u6570\uff0c\u5982\u679c\u8bbe\u7f6e\u4e86\u4efb\u4f55\u4f4d\uff0c\u8be5\u51fd\u6570\u8fd4\u56de true\uff0c\u5426\u5219\u8fd4\u56de false\uff0c\u4ee5\u67e5\u770b\u6211\u4eec\u67e5\u8be2\u7684\u4f4d\u662f\u5426\u4fdd\u6301\u6253\u5f00\u6216\u5173\u95ed\u3002 \u4ec0\u4e48\u65f6\u5019\u4f4d\u6807\u5fd7\u6700\u6709\u7528\uff1f \u7ec6\u5fc3\u7684\u8bfb\u8005\u53ef\u80fd\u4f1a\u6ce8\u610f\u5230\uff0c\u4e0a\u9762\u7684\u793a\u4f8b\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\u8282\u7701\u4efb\u4f55\u5185\u5b58\u30028 \u4e2a\u5e03\u5c14\u503c\u901a\u5e38\u9700\u8981 8 \u4e2a\u5b57\u8282\u3002\u4f46\u662f\u4e0a\u9762\u7684\u4f8b\u5b50\u4f7f\u7528\u4e86 9 \u4e2a\u5b57\u8282\uff088 \u4e2a\u5b57\u8282\u7528\u4e8e\u5b9a\u4e49\u4f4d\u63a9\u7801\uff0c1 \u4e2a\u5b57\u8282\u7528\u4e8e\u6807\u5fd7\u53d8\u91cf\uff09\uff01 \u5f53\u60a8\u6709\u8bb8\u591a\u76f8\u540c\u7684\u6807\u5fd7\u53d8\u91cf\u65f6\uff0c\u4f4d\u6807\u5fd7\u6700\u6709\u610f\u4e49\u3002\u4f8b\u5982\uff0c\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5047\u8bbe\u60a8\u6709 100 \u4e2a\u800c\u4e0d\u662f\u4e00\u4e2a\u4eba\uff08\u6211\uff09\u3002\u5982\u679c\u60a8\u6bcf\u4e2a\u4eba\u4f7f\u7528 8 \u4e2a\u5e03\u5c14\u503c\uff08\u6bcf\u4e2a\u53ef\u80fd\u7684\u72b6\u6001\u4e00\u4e2a\uff09\uff0c\u60a8\u5c06\u4f7f\u7528 800 \u4e2a\u5b57\u8282\u7684\u5185\u5b58\u3002\u5bf9\u4e8e\u4f4d\u6807\u5fd7\uff0c\u60a8\u5c06\u4f7f\u7528 8 \u4e2a\u5b57\u8282\u4f5c\u4e3a\u4f4d\u63a9\u7801\uff0c\u4f7f\u7528 100 \u4e2a\u5b57\u8282\u4f5c\u4e3a\u4f4d\u6807\u5fd7\u53d8\u91cf\uff0c\u603b\u5171\u9700\u8981 108 \u4e2a\u5b57\u8282\u7684\u5185\u5b58\u2014\u2014\u5927\u7ea6\u51cf\u5c11 8 \u500d\u7684\u5185\u5b58\u3002 \u5bf9\u4e8e\u5927\u591a\u6570\u7a0b\u5e8f\uff0c\u4f7f\u7528\u4f4d\u6807\u5fd7\u8282\u7701\u7684\u5185\u5b58\u91cf\u4e0d\u503c\u5f97\u589e\u52a0\u590d\u6742\u6027\u3002\u4f46\u662f\u5728\u6709\u6570\u4e07\u751a\u81f3\u6570\u767e\u4e07\u4e2a\u76f8\u4f3c\u5bf9\u8c61\u7684\u7a0b\u5e8f\u4e2d\uff0c\u4f7f\u7528\u4f4d\u6807\u5fd7\u53ef\u4ee5\u5927\u5927\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u3002\u5982\u679c\u9700\u8981\uff0c\u5c06\u5176\u5305\u542b\u5728\u60a8\u7684\u5de5\u5177\u5305\u4e2d\u662f\u4e00\u9879\u6709\u7528\u7684\u4f18\u5316\u3002 \u5728\u53e6\u4e00\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f4d\u6807\u5fd7\u548c\u4f4d\u63a9\u7801\u662f\u6709\u610f\u4e49\u7684\u3002\u60f3\u8c61\u4e00\u4e0b\uff0c\u60a8\u6709\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u91c7\u7528 32 \u79cd\u4e0d\u540c\u9009\u9879\u7684\u4efb\u610f\u7ec4\u5408\u3002\u7f16\u5199\u8be5\u51fd\u6570\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 32 \u4e2a\u5355\u72ec\u7684\u5e03\u5c14\u53c2\u6570\uff1a 1 void someFunction ( bool option1 , bool option2 , bool option3 , bool option4 , bool option5 , bool option6 , bool option7 , bool option8 , bool option9 , bool option10 , bool option11 , bool option12 , bool option13 , bool option14 , bool option15 , bool option16 , bool option17 , bool option18 , bool option19 , bool option20 , bool option21 , bool option22 , bool option23 , bool option24 , bool option25 , bool option26 , bool option27 , bool option28 , bool option29 , bool option30 , bool option31 , bool option32 ); \u5e0c\u671b\u4f60\u80fd\u7ed9\u4f60\u7684\u53c2\u6570\u8d77\u66f4\u5177\u63cf\u8ff0\u6027\u7684\u540d\u5b57\uff0c\u4f46\u8fd9\u91cc\u7684\u91cd\u70b9\u662f\u5411\u4f60\u5c55\u793a\u53c2\u6570\u5217\u8868\u662f\u591a\u4e48\u4ee4\u4eba\u8ba8\u538c\u3002 \u7136\u540e\u5f53\u4f60\u60f3\u8c03\u7528\u9009\u9879 10 \u548c 32 \u8bbe\u7f6e\u4e3a true \u7684\u51fd\u6570\u65f6\uff0c\u4f60\u5fc5\u987b\u8fd9\u6837\u505a\uff1a 1 someFunction ( false , false , false , false , false , false , false , false , false , true , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , true ); \u8fd9\u975e\u5e38\u96be\u4ee5\u9605\u8bfb\uff08\u9009\u9879 9\u300110 \u6216 11 \u662f\u5426\u8bbe\u7f6e\u4e3a true\uff1f\uff09\uff0c\u5e76\u4e14\u8fd8\u610f\u5473\u7740\u60a8\u5fc5\u987b\u8bb0\u4f4f\u54ea\u4e2a\u53c2\u6570\u5bf9\u5e94\u4e8e\u54ea\u4e2a\u9009\u9879\uff08\u5c06\u201c\u7f16\u8f91\u6807\u5fd7\u201d\u8bbe\u7f6e\u4e3a\u7b2c 9\u300110 \u6216\u7b2c 11 \u4e2a\u53c2\u6570\uff1f\uff09\u5b83\u4e5f\u53ef\u80fd\u4e0d\u662f\u5f88\u9ad8\u6548\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u51fd\u6570\u8c03\u7528\u90fd\u5fc5\u987b\u5c06 32 \u4e2a\u5e03\u5c14\u503c\u4ece\u8c03\u7528\u8005\u590d\u5236\u5230\u51fd\u6570\u3002 \u76f8\u53cd\uff0c\u5982\u679c\u60a8\u4f7f\u7528\u8fd9\u6837\u7684\u4f4d\u6807\u5fd7\u5b9a\u4e49\u51fd\u6570\uff1a 1 void someFunction ( std :: bitset < 32 > options ); \u7136\u540e\u4f60\u53ef\u4ee5\u4f7f\u7528\u4f4d\u6807\u5fd7\u6765\u53ea\u4f20\u9012\u4f60\u60f3\u8981\u7684\u9009\u9879\uff1a 1 someFunction ( option10 | option32 ); \u8fd9\u4e0d\u4ec5\u66f4\u5177\u53ef\u8bfb\u6027\uff0c\u800c\u4e14\u6027\u80fd\u4e5f\u53ef\u80fd\u66f4\u9ad8\uff0c\u56e0\u4e3a\u5b83\u53ea\u6d89\u53ca 2 \u4e2a\u64cd\u4f5c\uff08\u4e00\u4e2a \u6309\u4f4d\u6216 \u548c\u4e00\u4e2a\u53c2\u6570\u590d\u5236\uff09\u3002 \u8fd9\u5c31\u662f\u5e7f\u53d7\u597d\u8bc4\u7684 3d \u56fe\u5f62\u5e93 OpenGL \u9009\u62e9\u4f7f\u7528\u4f4d\u6807\u5fd7\u53c2\u6570\u800c\u4e0d\u662f\u8bb8\u591a\u8fde\u7eed\u7684\u5e03\u5c14\u53c2\u6570\u7684\u539f\u56e0\u4e4b\u4e00\u3002 \u4e0b\u9762\u662f\u6765\u81ea OpenGL \u7684\u793a\u4f8b\u51fd\u6570\u8c03\u7528\uff1a 1 glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // clear the color and the depth buffer GL_COLOR_BUFFER_BIT \u548c GL_DEPTH_BUFFER_BIT \u662f\u4f4d\u63a9\u7801\uff0c\u5b9a\u4e49\u5982\u4e0b\uff08\u5728 gl2.h \u4e2d\uff09\uff1a 1 2 3 #define GL_DEPTH_BUFFER_BIT 0x00000100 #define GL_STENCIL_BUFFER_BIT 0x00000400 #define GL_COLOR_BUFFER_BIT 0x00004000 \u6d89\u53ca\u591a\u4e2a\u4f4d\u7684\u4f4d\u63a9\u7801 \u5c3d\u7ba1\u4f4d\u63a9\u7801\u901a\u5e38\u7528\u4e8e\u9009\u62e9\u5355\u4e2a\u4f4d\uff0c\u4f46\u5b83\u4eec\u4e5f\u53ef\u7528\u4e8e\u9009\u62e9\u591a\u4e2a\u4f4d\u3002\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u7a0d\u5fae\u590d\u6742\u4e00\u70b9\u7684\u4f8b\u5b50\u3002 \u7535\u89c6\u673a\u3001\u663e\u793a\u5668\u7b49\u5f69\u8272\u663e\u793a\u8bbe\u5907\u7531\u6570\u767e\u4e07\u4e2a\u50cf\u7d20\u7ec4\u6210\uff0c\u6bcf\u4e2a\u50cf\u7d20\u90fd\u53ef\u4ee5\u663e\u793a\u4e00\u4e2a\u989c\u8272\u7684\u70b9\u3002\u989c\u8272\u70b9\u7531\u4e09\u675f\u5149\u7ec4\u6210\uff1a\u4e00\u675f\u7ea2\u8272\u3001\u4e00\u675f\u7eff\u8272\u548c\u4e00\u675f\u84dd\u8272 (RGB)\u3002\u901a\u8fc7\u6539\u53d8\u989c\u8272\u7684\u5f3a\u5ea6\uff0c\u53ef\u4ee5\u5236\u6210\u8272\u8c31\u4e0a\u7684\u4efb\u4f55\u989c\u8272\u3002\u901a\u5e38\uff0c\u7ed9\u5b9a\u50cf\u7d20\u7684 R\u3001G \u548c B \u7684\u6570\u91cf\u7531 8 \u4f4d\u65e0\u7b26\u53f7\u6574\u6570\u8868\u793a\u3002\u4f8b\u5982\uff0c\u7ea2\u8272\u50cf\u7d20\u7684 R=255\u3001G=0\u3001B=0\u3002\u7d2b\u8272\u50cf\u7d20\u7684 R=255\u3001G=0\u3001B=255\u3002\u4e2d\u7070\u8272\u50cf\u7d20\u5c06\u5177\u6709 R=127\u3001G=127\u3001B=127\u3002 \u5728\u4e3a\u50cf\u7d20\u5206\u914d\u989c\u8272\u503c\u65f6\uff0c\u9664\u4e86 R\u3001G \u548c B \u4e4b\u5916\uff0c\u901a\u5e38\u8fd8\u4f1a\u4f7f\u7528\u79f0\u4e3a A \u7684\u7b2c 4 \u4e2a\u503c\u3002\u201cA\u201d\u4ee3\u8868\u201calpha\u201d\uff0c\u5b83\u63a7\u5236\u989c\u8272\u7684\u900f\u660e\u5ea6\u3002\u5982\u679c A=0\uff0c\u989c\u8272\u662f\u5b8c\u5168\u900f\u660e\u7684\u3002\u5982\u679c A=255\uff0c\u5219\u989c\u8272\u4e0d\u900f\u660e\u3002 R\u3001G\u3001B \u548c A \u901a\u5e38\u5b58\u50a8\u4e3a\u5355\u4e2a 32 \u4f4d\u6574\u6570\uff0c\u6bcf\u4e2a\u5206\u91cf\u4f7f\u7528 8 \u4f4d\uff1a 32 \u4f4d RGBA \u503c 31-24 \u4f4d 23-16 \u4f4d 15-8 \u4f4d 7-0 \u4f4d RRRRRRRR GGGGGGGG BBBBBBBB AAAAAAAA red green blue alpha \u4e0b\u9762\u7684\u7a0b\u5e8f\u8981\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a 32 \u4f4d\u7684\u5341\u516d\u8fdb\u5236\u503c\uff0c\u7136\u540e\u63d0\u53d6 R\u3001G\u3001B \u548c A \u7684 8 \u4f4d\u989c\u8272\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include #include int main () { constexpr std :: uint32_t redBits { 0xFF000000 }; constexpr std :: uint32_t greenBits { 0x00FF0000 }; constexpr std :: uint32_t blueBits { 0x0000FF00 }; constexpr std :: uint32_t alphaBits { 0x000000FF }; std :: cout << \"Enter a 32-bit RGBA color value in hexadecimal (e.g. FF7F3300): \" ; std :: uint32_t pixel {}; std :: cin >> std :: hex >> pixel ; // std::hex allows us to read in a hex value // use Bitwise AND to isolate red pixels, // then right shift the value into the lower 8 bits std :: uint8_t red { static_cast < std :: uint8_t > (( pixel & redBits ) >> 24 ) }; std :: uint8_t green { static_cast < std :: uint8_t > (( pixel & greenBits ) >> 16 ) }; std :: uint8_t blue { static_cast < std :: uint8_t > (( pixel & blueBits ) >> 8 ) }; std :: uint8_t alpha { static_cast < std :: uint8_t > ( pixel & alphaBits ) }; std :: cout << \"Your color contains: \\n \" ; std :: cout << std :: hex ; // print the following values in hex std :: cout << static_cast < int > ( red ) << \" red \\n \" ; std :: cout << static_cast < int > ( green ) << \" green \\n \" ; std :: cout << static_cast < int > ( blue ) << \" blue \\n \" ; std :: cout << static_cast < int > ( alpha ) << \" alpha \\n \" ; return 0 ; } \u8fd9\u4f1a\u4ea7\u751f\u8f93\u51fa\uff1a 1 2 3 4 5 6 Enter a 32-bit RGBA color value in hexadecimal (e.g. FF7F3300): FF7F3300 Your color contains: ff red 7f green 33 blue 0 alpha \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u4e0e \u6765\u67e5\u8be2\u6211\u4eec\u611f\u5174\u8da3\u7684 8 \u4f4d\u96c6\u5408\uff0c\u7136\u540e\u5c06\u5b83\u4eec \u53f3 \u79fb\u5230\u4e00\u4e2a 8 \u4f4d\u503c\u4e2d\uff0c\u4ee5\u4fbf\u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u4eec\u4f5c\u4e3a\u5341\u516d\u8fdb\u5236\u503c\u6253\u5370\u56de\u6765\u3002 \u6982\u62ec \u603b\u7ed3\u5982\u4f55\u8bbe\u7f6e\u3001\u6e05\u9664\u3001\u5207\u6362\u548c\u67e5\u8be2\u4f4d\u6807\u5fd7\uff1a \u8981\u67e5\u8be2\u4f4d\u72b6\u6001\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u4e0e \uff1a 1 if ( flags & option4 ) ... // if option4 is set, do something \u8981\u8bbe\u7f6e\u4f4d\uff08\u6253\u5f00\uff09\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u6216 \uff1a 1 2 flags |= option4 ; // turn option 4 on. flags |= ( option4 | option5 ); // turn options 4 and 5 on. \u8981\u6e05\u9664\u4f4d\uff08\u5173\u95ed\uff09\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d AND \u548c \u6309\u4f4d NOT \uff1a 1 2 flags &= ~ option4 ; // turn option 4 off flags &= ~ ( option4 | option5 ); // turn options 4 and 5 off \u8981\u7ffb\u8f6c\u4f4d\u72b6\u6001\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u5f02\u6216 \uff1a 1 2 flags ^= option4 ; // flip option4 from on to off, or vice versa flags ^= ( option4 | option5 ); // flip options 4 and 5","title":"O.3 - \u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u548c\u63a9\u7801\u64cd\u4f5c\u4f4d"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#_1","text":"\u4e3a\u4e86\u64cd\u4f5c\u5355\u4e2a\u4f4d\uff08\u4f8b\u5982\u6253\u5f00\u6216\u5173\u95ed\u5b83\u4eec\uff09\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e9b\u65b9\u6cd5\u6765\u8bc6\u522b\u6211\u4eec\u60f3\u8981\u64cd\u4f5c\u7684\u7279\u5b9a\u4f4d\u3002\u4e0d\u5e78\u7684\u662f\uff0c\u6309\u4f4d\u8fd0\u7b97\u7b26\u4e0d\u77e5\u9053\u5982\u4f55\u5904\u7406\u4f4d\u4f4d\u7f6e\u3002\u76f8\u53cd\uff0c\u4ed6\u4eec\u4f7f\u7528\u4f4d\u63a9\u7801\u3002 \u4f4d\u63a9\u7801 \u662f\u4e00\u7ec4\u9884\u5b9a\u4e49\u7684\u4f4d\uff0c\u7528\u4e8e\u9009\u62e9\u54ea\u4e9b\u7279\u5b9a\u4f4d\u5c06\u88ab\u540e\u7eed\u64cd\u4f5c\u4fee\u6539\u3002 \u8003\u8651\u4e00\u4e2a\u771f\u5b9e\u7684\u6848\u4f8b\uff0c\u60a8\u60f3\u8981\u7ed8\u5236\u4e00\u4e2a\u7a97\u6846\u3002\u5982\u679c\u60a8\u4e0d\u5c0f\u5fc3\uff0c\u60a8\u4e0d\u4ec5\u4f1a\u5192\u7740\u7c89\u5237\u7a97\u6846\u7684\u98ce\u9669\uff0c\u8fd8\u4f1a\u5192\u7740\u7c89\u5237\u73bb\u7483\u672c\u8eab\u7684\u98ce\u9669\u3002\u60a8\u53ef\u4ee5\u8d2d\u4e70\u4e00\u4e9b\u906e\u853d\u80f6\u5e26\u5e76\u5c06\u5176\u8d34\u5728\u73bb\u7483\u548c\u60a8\u4e0d\u60f3\u6d82\u6f06\u7684\u4efb\u4f55\u5176\u4ed6\u90e8\u4ef6\u4e0a\u3002\u7136\u540e\uff0c\u5f53\u60a8\u7ed8\u753b\u65f6\uff0c\u906e\u853d\u80f6\u5e26\u4f1a\u963b\u6b62\u6cb9\u6f06\u63a5\u89e6\u5230\u60a8\u4e0d\u60f3\u6d82\u6f06\u7684\u4efb\u4f55\u4e1c\u897f\u3002\u6700\u540e\uff0c\u53ea\u6709\u672a\u906e\u7f69\u7684\u90e8\u5206\uff08\u60a8\u8981\u7ed8\u5236\u7684\u90e8\u5206\uff09\u88ab\u7ed8\u5236\u3002 \u4f4d\u63a9\u7801\u672c\u8d28\u4e0a\u5bf9\u4f4d\u6267\u884c\u76f8\u540c\u7684\u529f\u80fd\u2014\u2014 \u4f4d\u63a9\u7801\u963b\u6b62\u6309\u4f4d\u8fd0\u7b97\u7b26\u63a5\u89e6\u6211\u4eec\u4e0d\u60f3\u4fee\u6539\u7684\u4f4d\uff0c\u5e76\u5141\u8bb8\u8bbf\u95ee\u6211\u4eec\u786e\u5b9e\u60f3\u8981\u4fee\u6539\u7684\u4f4d\u3002 \u8ba9\u6211\u4eec\u9996\u5148\u63a2\u8ba8\u5982\u4f55\u5b9a\u4e49\u4e00\u4e9b\u7b80\u5355\u7684\u4f4d\u63a9\u7801\uff0c\u7136\u540e\u6211\u4eec\u5c06\u5411\u60a8\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u5b83\u4eec\u3002","title":"\u4f4d\u63a9\u7801"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#c14","text":"\u6700\u7b80\u5355\u7684\u4e00\u7ec4\u4f4d\u63a9\u7801\u662f\u4e3a\u6bcf\u4e2a\u4f4d\u4f4d\u7f6e\u5b9a\u4e49\u4e00\u4e2a\u4f4d\u63a9\u7801\u3002\u6211\u4eec\u7528 0 \u6765\u5c4f\u853d\u6211\u4eec\u4e0d\u5173\u5fc3\u7684\u4f4d\uff0c\u7528 1 \u6765\u8868\u793a\u6211\u4eec\u60f3\u8981\u4fee\u6539\u7684\u4f4d\u3002 \u5c3d\u7ba1\u4f4d\u63a9\u7801\u53ef\u4ee5\u662f\u6587\u5b57\uff0c\u4f46\u5b83\u4eec\u901a\u5e38\u88ab\u5b9a\u4e49\u4e3a\u7b26\u53f7\u5e38\u91cf\uff0c\u56e0\u6b64\u53ef\u4ee5\u4e3a\u5b83\u4eec\u6307\u5b9a\u4e00\u4e2a\u6709\u610f\u4e49\u7684\u540d\u79f0\u5e76\u6613\u4e8e\u91cd\u7528\u3002 \u56e0\u4e3a C++14 \u652f\u6301\u4e8c\u8fdb\u5236\u6587\u5b57\uff0c\u6240\u4ee5\u5b9a\u4e49\u8fd9\u4e9b\u4f4d\u63a9\u7801\u5f88\u5bb9\u6613\uff1a 1 2 3 4 5 6 7 8 9 10 #include constexpr std :: uint8_t mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: uint8_t mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: uint8_t mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: uint8_t mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: uint8_t mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: uint8_t mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: uint8_t mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: uint8_t mask7 { 0b1000'0000 }; // represents bit 7 \u73b0\u5728\u6211\u4eec\u6709\u4e00\u7ec4\u4ee3\u8868\u6bcf\u4e2a\u4f4d\u4f4d\u7f6e\u7684\u7b26\u53f7\u5e38\u91cf\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u6765\u64cd\u4f5c\u4f4d\uff08\u7a0d\u540e\u6211\u4eec\u5c06\u5c55\u793a\u5982\u4f55\u64cd\u4f5c\uff09\u3002","title":"\u5728 C++14 \u4e2d\u5b9a\u4e49\u4f4d\u63a9\u7801"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#c11","text":"\u56e0\u4e3a C++11 \u4e0d\u652f\u6301\u4e8c\u8fdb\u5236\u6587\u5b57 \uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u6765\u8bbe\u7f6e\u7b26\u53f7\u5e38\u91cf\u3002\u6709\u4e24\u79cd\u5f88\u597d\u7684\u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\u3002 \u7b2c\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u5341\u516d\u8fdb\u5236\u6587\u5b57\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u5728 4.15-\u5b57\u9762\u91cf \u4e2d\u8ba8\u8bba\u4e86\u5341\u516d\u8fdb\u5236\u3002 \u5341\u516d\u8fdb\u5236\u8f6c\u4e8c\u8fdb\u5236\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a \u5341\u516d\u8fdb\u5236 0 1 2 3 4 5 6 7 8 9 A B C D E F \u4e8c\u8fdb\u5236 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u50cf\u8fd9\u6837\u4f7f\u7528\u5341\u516d\u8fdb\u5236\u5b9a\u4e49\u4f4d\u63a9\u7801\uff1a 1 2 3 4 5 6 7 8 constexpr std :: uint8_t mask0 { 0x01 }; // hex for 0000 0001 constexpr std :: uint8_t mask1 { 0x02 }; // hex for 0000 0010 constexpr std :: uint8_t mask2 { 0x04 }; // hex for 0000 0100 constexpr std :: uint8_t mask3 { 0x08 }; // hex for 0000 1000 constexpr std :: uint8_t mask4 { 0x10 }; // hex for 0001 0000 constexpr std :: uint8_t mask5 { 0x20 }; // hex for 0010 0000 constexpr std :: uint8_t mask6 { 0x40 }; // hex for 0100 0000 constexpr std :: uint8_t mask7 { 0x80 }; // hex for 1000 0000 \u6709\u65f6\u4f1a\u7701\u7565\u524d\u5bfc\u7684\u5341\u516d\u8fdb\u5236 0\uff08\u4f8b\u5982\uff0c 0x01 \u60a8\u53ea\u4f1a\u770b\u5230 0x1 \uff09\u3002\u65e0\u8bba\u54ea\u79cd\u65b9\u5f0f\uff0c\u5982\u679c\u60a8\u4e0d\u719f\u6089\u5341\u516d\u8fdb\u5236\u5230\u4e8c\u8fdb\u5236\u7684\u8f6c\u6362\uff0c\u8fd9\u53ef\u80fd\u6709\u70b9\u96be\u4ee5\u9605\u8bfb\u3002 \u4e00\u79cd\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5de6\u79fb\u8fd0\u7b97\u7b26\u5c06\u4e00\u4f4d\u79fb\u52a8\u5230\u6b63\u786e\u7684\u4f4d\u7f6e\uff1a 1 2 3 4 5 6 7 8 constexpr std :: uint8_t mask0 { 1 << 0 }; // 0000 0001 constexpr std :: uint8_t mask1 { 1 << 1 }; // 0000 0010 constexpr std :: uint8_t mask2 { 1 << 2 }; // 0000 0100 constexpr std :: uint8_t mask3 { 1 << 3 }; // 0000 1000 constexpr std :: uint8_t mask4 { 1 << 4 }; // 0001 0000 constexpr std :: uint8_t mask5 { 1 << 5 }; // 0010 0000 constexpr std :: uint8_t mask6 { 1 << 6 }; // 0100 0000 constexpr std :: uint8_t mask7 { 1 << 7 }; // 1000 0000","title":"\u5728 C++11 \u6216\u66f4\u65e9\u7248\u672c\u4e2d\u5b9a\u4e49\u4f4d\u63a9\u7801"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#10","text":"\u73b0\u5728\u6211\u4eec\u6709\u4e86\u4e00\u7ec4\u4f4d\u63a9\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u4eec\u4e0e\u4f4d\u6807\u5fd7\u53d8\u91cf\u7ed3\u5408\u4f7f\u7528\u6765\u64cd\u4f5c\u6211\u4eec\u7684\u4f4d\u6807\u5fd7\u3002 \u8981\u786e\u5b9a\u67d0\u4e2a\u4f4d\u662f\u5f00\u8fd8\u662f\u5173\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u4e0e \u7ed3\u5408\u76f8\u5e94\u4f4d\u7684\u4f4d\u63a9\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include int main () { constexpr std :: uint8_t mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: uint8_t mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: uint8_t mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: uint8_t mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: uint8_t mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: uint8_t mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: uint8_t mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: uint8_t mask7 { 0b1000'0000 }; // represents bit 7 std :: uint8_t flags { 0b0000'0101 }; // 8 bits in size means room for 8 flags std :: cout << \"bit 0 is \" << (( flags & mask0 ) ? \"on \\n \" : \"off \\n \" ); std :: cout << \"bit 1 is \" << (( flags & mask1 ) ? \"on \\n \" : \"off \\n \" ); return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 bit 1 is off bit 1 is on","title":"\u6d4b\u8bd5\u4e00\u4e0b\uff08\u770b\u5b83\u662f1\u8fd8\u662f0\uff09"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#1","text":"\u8981\u8bbe\u7f6e\uff08\u7f6e1\uff09\u4f4d\uff0c\u6211\u4eec\u5c06\u6309\u4f4d\u6216\u7b49\u4e8e\uff08\u8fd0\u7b97\u7b26 |=\uff09\u4e0e\u76f8\u5e94\u4f4d\u7684\u4f4d\u63a9\u7801\u7ed3\u5408\u4f7f\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include int main () { constexpr std :: uint8_t mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: uint8_t mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: uint8_t mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: uint8_t mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: uint8_t mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: uint8_t mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: uint8_t mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: uint8_t mask7 { 0b1000'0000 }; // represents bit 7 std :: uint8_t flags { 0b0000'0101 }; // 8 bits in size means room for 8 flags std :: cout << \"bit 1 is \" << (( flags & mask1 ) ? \"on \\n \" : \"off \\n \" ); flags |= mask1 ; // turn on bit 1 std :: cout << \"bit 1 is \" << (( flags & mask1 ) ? \"on \\n \" : \"off \\n \" ); return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 bit 1 is off bit 1 is on \u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6309\u4f4d\u6216 \u540c\u65f6\u7f6e1\u591a\u4e2a\u4f4d\uff1a 1 flags |= ( mask4 | mask5 ); // turn bits 4 and 5 on at the same time","title":"\u7f6e1\u4e00\u4f4d"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#0","text":"\u8981\u6e05\u9664\u4f4d\uff08\u7f6e0\uff09\uff0c\u6211\u4eec\u540c\u65f6\u4f7f\u7528 \u6309\u4f4d\u4e0e \u548c \u6309\u4f4d\u975e \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include int main () { constexpr std :: uint8_t mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: uint8_t mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: uint8_t mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: uint8_t mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: uint8_t mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: uint8_t mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: uint8_t mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: uint8_t mask7 { 0b1000'0000 }; // represents bit 7 std :: uint8_t flags { 0b0000'0101 }; // 8 bits in size means room for 8 flags std :: cout << \"bit 2 is \" << (( flags & mask2 ) ? \"on \\n \" : \"off \\n \" ); flags &= ~ mask2 ; // turn off bit 2 std :: cout << \"bit 2 is \" << (( flags & mask2 ) ? \"on \\n \" : \"off \\n \" ); return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 bit 2 is on bit 2 is off \u6211\u4eec\u53ef\u4ee5\u540c\u65f6\u5173\u95ed\u591a\u4e2a\u4f4d\uff1a 1 flags &= ~ ( mask4 | mask5 ); // turn bits 4 and 5 off at the same time","title":"\u7f6e0\u4e00\u4f4d"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#_2","text":"\u8981\u5207\u6362\u4f4d\u72b6\u6001\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u5f02\u6216 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include int main () { constexpr std :: uint8_t mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: uint8_t mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: uint8_t mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: uint8_t mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: uint8_t mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: uint8_t mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: uint8_t mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: uint8_t mask7 { 0b1000'0000 }; // represents bit 7 std :: uint8_t flags { 0b0000'0101 }; // 8 bits in size means room for 8 flags std :: cout << \"bit 2 is \" << (( flags & mask2 ) ? \"on \\n \" : \"off \\n \" ); flags ^= mask2 ; // flip bit 2 std :: cout << \"bit 2 is \" << (( flags & mask2 ) ? \"on \\n \" : \"off \\n \" ); flags ^= mask2 ; // flip bit 2 std :: cout << \"bit 2 is \" << (( flags & mask2 ) ? \"on \\n \" : \"off \\n \" ); return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 3 bit 2 is on bit 2 is off bit 2 is on \u6211\u4eec\u53ef\u4ee5\u540c\u65f6\u7ffb\u8f6c\u591a\u4e2a\u4f4d\uff1a 1 flags ^= ( mask4 | mask5 ); // flip bits 4 and 5 at the same time","title":"\u7ffb\u8f6c\u4e00\u4f4d"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#stdbitset","text":"std::bitset \u652f\u6301\u5168\u5957\u4f4d\u8fd0\u7b97\u7b26\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u4f7f\u7528\u51fd\u6570\uff08\u6d4b\u8bd5\u3001\u8bbe\u7f6e\u3001\u91cd\u7f6e\u548c\u7ffb\u8f6c\uff09\u4fee\u6539\u5355\u4e2a\u4f4d\u66f4\u5bb9\u6613\uff0c\u4f46\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4f7f\u7528\u6309\u4f4d\u8fd0\u7b97\u7b26\u548c\u4f4d\u63a9\u7801\u3002 \u4f60\u4e3a\u4ec0\u4e48\u8981\u8fd9\u6837\u505a\uff1f \u8fd9\u4e9b\u51fd\u6570test(),set(),reset(),flip()\u53ea\u5141\u8bb8\u60a8\u4fee\u6539\u5355\u4e2a\u4f4d\u3002\u6309\u4f4d\u8fd0\u7b97\u7b26\u5141\u8bb8\u60a8\u4e00\u6b21\u4fee\u6539\u591a\u4e2a\u4f4d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include #include #include int main () { constexpr std :: bitset < 8 > mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: bitset < 8 > mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: bitset < 8 > mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: bitset < 8 > mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: bitset < 8 > mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: bitset < 8 > mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: bitset < 8 > mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: bitset < 8 > mask7 { 0b1000'0000 }; // represents bit 7 std :: bitset < 8 > flags { 0b0000'0101 }; // 8 bits in size means room for 8 flags std :: cout << \"bit 1 is \" << ( flags . test ( 1 ) ? \"on \\n \" : \"off \\n \" ); std :: cout << \"bit 2 is \" << ( flags . test ( 2 ) ? \"on \\n \" : \"off \\n \" ); flags ^= ( mask1 | mask2 ); // flip bits 1 and 2 std :: cout << \"bit 1 is \" << ( flags . test ( 1 ) ? \"on \\n \" : \"off \\n \" ); std :: cout << \"bit 2 is \" << ( flags . test ( 2 ) ? \"on \\n \" : \"off \\n \" ); flags |= ( mask1 | mask2 ); // turn bits 1 and 2 on std :: cout << \"bit 1 is \" << ( flags . test ( 1 ) ? \"on \\n \" : \"off \\n \" ); std :: cout << \"bit 2 is \" << ( flags . test ( 2 ) ? \"on \\n \" : \"off \\n \" ); flags &= ~ ( mask1 | mask2 ); // turn bits 1 and 2 off std :: cout << \"bit 1 is \" << ( flags . test ( 1 ) ? \"on \\n \" : \"off \\n \" ); std :: cout << \"bit 2 is \" << ( flags . test ( 2 ) ? \"on \\n \" : \"off \\n \" ); return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 3 4 5 6 7 8 bit 1 is off bit 2 is on bit 1 is on bit 2 is off bit 1 is on bit 2 is on bit 1 is off bit 2 is off","title":"\u4f4d\u63a9\u7801\u548c std::bitset"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#_3","text":"\u5c06\u6211\u4eec\u7684\u4f4d\u63a9\u7801\u547d\u540d\u4e3a\u201cmask1\u201d\u6216\u201cmask2\u201d\u53ef\u4ee5\u544a\u8bc9\u6211\u4eec\u6b63\u5728\u64cd\u4f5c\u7684\u662f\u54ea\u4e2a\u4f4d\uff0c\u4f46\u4e0d\u4f1a\u544a\u8bc9\u6211\u4eec\u8be5\u4f4d\u6807\u5fd7\u5b9e\u9645\u7528\u4e8e\u4ec0\u4e48\u3002 \u6700\u4f73\u505a\u6cd5\u662f\u4e3a\u60a8\u7684\u4f4d\u63a9\u7801\u63d0\u4f9b\u6709\u7528\u7684\u540d\u79f0\uff0c\u4f5c\u4e3a\u8bb0\u5f55\u4f4d\u6807\u5fd7\u542b\u4e49\u7684\u4e00\u79cd\u65b9\u5f0f\u3002\u8fd9\u662f\u6211\u4eec\u53ef\u80fd\u7f16\u5199\u7684\u6e38\u620f\u7684\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include int main () { // Define a bunch of physical/emotional states constexpr std :: uint8_t isHungry { 1 << 0 }; // 0000 0001 constexpr std :: uint8_t isSad { 1 << 1 }; // 0000 0010 constexpr std :: uint8_t isMad { 1 << 2 }; // 0000 0100 constexpr std :: uint8_t isHappy { 1 << 3 }; // 0000 1000 constexpr std :: uint8_t isLaughing { 1 << 4 }; // 0001 0000 constexpr std :: uint8_t isAsleep { 1 << 5 }; // 0010 0000 constexpr std :: uint8_t isDead { 1 << 6 }; // 0100 0000 constexpr std :: uint8_t isCrying { 1 << 7 }; // 1000 0000 std :: uint8_t me {}; // all flags/options turned off to start me |= ( isHappy | isLaughing ); // I am happy and laughing me &= ~ isLaughing ; // I am no longer laughing // Query a few states // (we'll use static_cast to interpret the results as a boolean value) std :: cout << \"I am happy? \" << static_cast < bool > ( me & isHappy ) << '\\n' ; std :: cout << \"I am laughing? \" << static_cast < bool > ( me & isLaughing ) << '\\n' ; return 0 ; } \u4e0b\u9762\u662f\u4f7f\u7528 std::bitset \u5b9e\u73b0\u7684\u76f8\u540c\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include int main () { // Define a bunch of physical/emotional states constexpr std :: bitset < 8 > isHungry { 0b0000'0001 }; constexpr std :: bitset < 8 > isSad { 0b0000'0010 }; constexpr std :: bitset < 8 > isMad { 0b0000'0100 }; constexpr std :: bitset < 8 > isHappy { 0b0000'1000 }; constexpr std :: bitset < 8 > isLaughing { 0b0001'0000 }; constexpr std :: bitset < 8 > isAsleep { 0b0010'0000 }; constexpr std :: bitset < 8 > isDead { 0b0100'0000 }; constexpr std :: bitset < 8 > isCrying { 0b1000'0000 }; std :: bitset < 8 > me {}; // all flags/options turned off to start me |= ( isHappy | isLaughing ); // I am happy and laughing me &= ~ isLaughing ; // I am no longer laughing // Query a few states (we use the any() function to see if any bits remain set) std :: cout << \"I am happy? \" << ( me & isHappy ). any () << '\\n' ; std :: cout << \"I am laughing? \" << ( me & isLaughing ). any () << '\\n' ; return 0 ; } \u8fd9\u91cc\u6709\u4e24\u4e2a\u6ce8\u610f\u4e8b\u9879\uff1a\u9996\u5148\uff0cstd::bitset \u6ca1\u6709\u5141\u8bb8\u60a8\u4f7f\u7528\u4f4d\u63a9\u7801\u67e5\u8be2\u4f4d\u7684\u597d\u51fd\u6570\u3002\u56e0\u6b64\uff0c\u5982\u679c\u60a8\u60f3\u4f7f\u7528\u4f4d\u63a9\u7801\u800c\u4e0d\u662f\u4f4d\u7f6e\u7d22\u5f15\uff0c\u5219\u5fc5\u987b\u4f7f\u7528 \u6309\u4f4d\u4e0e \u67e5\u8be2\u4f4d\u3002\u5176\u6b21\uff0c\u6211\u4eec\u4f7f\u7528 any() \u51fd\u6570\uff0c\u5982\u679c\u8bbe\u7f6e\u4e86\u4efb\u4f55\u4f4d\uff0c\u8be5\u51fd\u6570\u8fd4\u56de true\uff0c\u5426\u5219\u8fd4\u56de false\uff0c\u4ee5\u67e5\u770b\u6211\u4eec\u67e5\u8be2\u7684\u4f4d\u662f\u5426\u4fdd\u6301\u6253\u5f00\u6216\u5173\u95ed\u3002","title":"\u4f7f\u4f4d\u63a9\u7801\u6709\u610f\u4e49"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#_4","text":"\u7ec6\u5fc3\u7684\u8bfb\u8005\u53ef\u80fd\u4f1a\u6ce8\u610f\u5230\uff0c\u4e0a\u9762\u7684\u793a\u4f8b\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\u8282\u7701\u4efb\u4f55\u5185\u5b58\u30028 \u4e2a\u5e03\u5c14\u503c\u901a\u5e38\u9700\u8981 8 \u4e2a\u5b57\u8282\u3002\u4f46\u662f\u4e0a\u9762\u7684\u4f8b\u5b50\u4f7f\u7528\u4e86 9 \u4e2a\u5b57\u8282\uff088 \u4e2a\u5b57\u8282\u7528\u4e8e\u5b9a\u4e49\u4f4d\u63a9\u7801\uff0c1 \u4e2a\u5b57\u8282\u7528\u4e8e\u6807\u5fd7\u53d8\u91cf\uff09\uff01 \u5f53\u60a8\u6709\u8bb8\u591a\u76f8\u540c\u7684\u6807\u5fd7\u53d8\u91cf\u65f6\uff0c\u4f4d\u6807\u5fd7\u6700\u6709\u610f\u4e49\u3002\u4f8b\u5982\uff0c\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5047\u8bbe\u60a8\u6709 100 \u4e2a\u800c\u4e0d\u662f\u4e00\u4e2a\u4eba\uff08\u6211\uff09\u3002\u5982\u679c\u60a8\u6bcf\u4e2a\u4eba\u4f7f\u7528 8 \u4e2a\u5e03\u5c14\u503c\uff08\u6bcf\u4e2a\u53ef\u80fd\u7684\u72b6\u6001\u4e00\u4e2a\uff09\uff0c\u60a8\u5c06\u4f7f\u7528 800 \u4e2a\u5b57\u8282\u7684\u5185\u5b58\u3002\u5bf9\u4e8e\u4f4d\u6807\u5fd7\uff0c\u60a8\u5c06\u4f7f\u7528 8 \u4e2a\u5b57\u8282\u4f5c\u4e3a\u4f4d\u63a9\u7801\uff0c\u4f7f\u7528 100 \u4e2a\u5b57\u8282\u4f5c\u4e3a\u4f4d\u6807\u5fd7\u53d8\u91cf\uff0c\u603b\u5171\u9700\u8981 108 \u4e2a\u5b57\u8282\u7684\u5185\u5b58\u2014\u2014\u5927\u7ea6\u51cf\u5c11 8 \u500d\u7684\u5185\u5b58\u3002 \u5bf9\u4e8e\u5927\u591a\u6570\u7a0b\u5e8f\uff0c\u4f7f\u7528\u4f4d\u6807\u5fd7\u8282\u7701\u7684\u5185\u5b58\u91cf\u4e0d\u503c\u5f97\u589e\u52a0\u590d\u6742\u6027\u3002\u4f46\u662f\u5728\u6709\u6570\u4e07\u751a\u81f3\u6570\u767e\u4e07\u4e2a\u76f8\u4f3c\u5bf9\u8c61\u7684\u7a0b\u5e8f\u4e2d\uff0c\u4f7f\u7528\u4f4d\u6807\u5fd7\u53ef\u4ee5\u5927\u5927\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u3002\u5982\u679c\u9700\u8981\uff0c\u5c06\u5176\u5305\u542b\u5728\u60a8\u7684\u5de5\u5177\u5305\u4e2d\u662f\u4e00\u9879\u6709\u7528\u7684\u4f18\u5316\u3002 \u5728\u53e6\u4e00\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f4d\u6807\u5fd7\u548c\u4f4d\u63a9\u7801\u662f\u6709\u610f\u4e49\u7684\u3002\u60f3\u8c61\u4e00\u4e0b\uff0c\u60a8\u6709\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u91c7\u7528 32 \u79cd\u4e0d\u540c\u9009\u9879\u7684\u4efb\u610f\u7ec4\u5408\u3002\u7f16\u5199\u8be5\u51fd\u6570\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 32 \u4e2a\u5355\u72ec\u7684\u5e03\u5c14\u53c2\u6570\uff1a 1 void someFunction ( bool option1 , bool option2 , bool option3 , bool option4 , bool option5 , bool option6 , bool option7 , bool option8 , bool option9 , bool option10 , bool option11 , bool option12 , bool option13 , bool option14 , bool option15 , bool option16 , bool option17 , bool option18 , bool option19 , bool option20 , bool option21 , bool option22 , bool option23 , bool option24 , bool option25 , bool option26 , bool option27 , bool option28 , bool option29 , bool option30 , bool option31 , bool option32 ); \u5e0c\u671b\u4f60\u80fd\u7ed9\u4f60\u7684\u53c2\u6570\u8d77\u66f4\u5177\u63cf\u8ff0\u6027\u7684\u540d\u5b57\uff0c\u4f46\u8fd9\u91cc\u7684\u91cd\u70b9\u662f\u5411\u4f60\u5c55\u793a\u53c2\u6570\u5217\u8868\u662f\u591a\u4e48\u4ee4\u4eba\u8ba8\u538c\u3002 \u7136\u540e\u5f53\u4f60\u60f3\u8c03\u7528\u9009\u9879 10 \u548c 32 \u8bbe\u7f6e\u4e3a true \u7684\u51fd\u6570\u65f6\uff0c\u4f60\u5fc5\u987b\u8fd9\u6837\u505a\uff1a 1 someFunction ( false , false , false , false , false , false , false , false , false , true , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , true ); \u8fd9\u975e\u5e38\u96be\u4ee5\u9605\u8bfb\uff08\u9009\u9879 9\u300110 \u6216 11 \u662f\u5426\u8bbe\u7f6e\u4e3a true\uff1f\uff09\uff0c\u5e76\u4e14\u8fd8\u610f\u5473\u7740\u60a8\u5fc5\u987b\u8bb0\u4f4f\u54ea\u4e2a\u53c2\u6570\u5bf9\u5e94\u4e8e\u54ea\u4e2a\u9009\u9879\uff08\u5c06\u201c\u7f16\u8f91\u6807\u5fd7\u201d\u8bbe\u7f6e\u4e3a\u7b2c 9\u300110 \u6216\u7b2c 11 \u4e2a\u53c2\u6570\uff1f\uff09\u5b83\u4e5f\u53ef\u80fd\u4e0d\u662f\u5f88\u9ad8\u6548\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u51fd\u6570\u8c03\u7528\u90fd\u5fc5\u987b\u5c06 32 \u4e2a\u5e03\u5c14\u503c\u4ece\u8c03\u7528\u8005\u590d\u5236\u5230\u51fd\u6570\u3002 \u76f8\u53cd\uff0c\u5982\u679c\u60a8\u4f7f\u7528\u8fd9\u6837\u7684\u4f4d\u6807\u5fd7\u5b9a\u4e49\u51fd\u6570\uff1a 1 void someFunction ( std :: bitset < 32 > options ); \u7136\u540e\u4f60\u53ef\u4ee5\u4f7f\u7528\u4f4d\u6807\u5fd7\u6765\u53ea\u4f20\u9012\u4f60\u60f3\u8981\u7684\u9009\u9879\uff1a 1 someFunction ( option10 | option32 ); \u8fd9\u4e0d\u4ec5\u66f4\u5177\u53ef\u8bfb\u6027\uff0c\u800c\u4e14\u6027\u80fd\u4e5f\u53ef\u80fd\u66f4\u9ad8\uff0c\u56e0\u4e3a\u5b83\u53ea\u6d89\u53ca 2 \u4e2a\u64cd\u4f5c\uff08\u4e00\u4e2a \u6309\u4f4d\u6216 \u548c\u4e00\u4e2a\u53c2\u6570\u590d\u5236\uff09\u3002 \u8fd9\u5c31\u662f\u5e7f\u53d7\u597d\u8bc4\u7684 3d \u56fe\u5f62\u5e93 OpenGL \u9009\u62e9\u4f7f\u7528\u4f4d\u6807\u5fd7\u53c2\u6570\u800c\u4e0d\u662f\u8bb8\u591a\u8fde\u7eed\u7684\u5e03\u5c14\u53c2\u6570\u7684\u539f\u56e0\u4e4b\u4e00\u3002 \u4e0b\u9762\u662f\u6765\u81ea OpenGL \u7684\u793a\u4f8b\u51fd\u6570\u8c03\u7528\uff1a 1 glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // clear the color and the depth buffer GL_COLOR_BUFFER_BIT \u548c GL_DEPTH_BUFFER_BIT \u662f\u4f4d\u63a9\u7801\uff0c\u5b9a\u4e49\u5982\u4e0b\uff08\u5728 gl2.h \u4e2d\uff09\uff1a 1 2 3 #define GL_DEPTH_BUFFER_BIT 0x00000100 #define GL_STENCIL_BUFFER_BIT 0x00000400 #define GL_COLOR_BUFFER_BIT 0x00004000","title":"\u4ec0\u4e48\u65f6\u5019\u4f4d\u6807\u5fd7\u6700\u6709\u7528\uff1f"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#_5","text":"\u5c3d\u7ba1\u4f4d\u63a9\u7801\u901a\u5e38\u7528\u4e8e\u9009\u62e9\u5355\u4e2a\u4f4d\uff0c\u4f46\u5b83\u4eec\u4e5f\u53ef\u7528\u4e8e\u9009\u62e9\u591a\u4e2a\u4f4d\u3002\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u7a0d\u5fae\u590d\u6742\u4e00\u70b9\u7684\u4f8b\u5b50\u3002 \u7535\u89c6\u673a\u3001\u663e\u793a\u5668\u7b49\u5f69\u8272\u663e\u793a\u8bbe\u5907\u7531\u6570\u767e\u4e07\u4e2a\u50cf\u7d20\u7ec4\u6210\uff0c\u6bcf\u4e2a\u50cf\u7d20\u90fd\u53ef\u4ee5\u663e\u793a\u4e00\u4e2a\u989c\u8272\u7684\u70b9\u3002\u989c\u8272\u70b9\u7531\u4e09\u675f\u5149\u7ec4\u6210\uff1a\u4e00\u675f\u7ea2\u8272\u3001\u4e00\u675f\u7eff\u8272\u548c\u4e00\u675f\u84dd\u8272 (RGB)\u3002\u901a\u8fc7\u6539\u53d8\u989c\u8272\u7684\u5f3a\u5ea6\uff0c\u53ef\u4ee5\u5236\u6210\u8272\u8c31\u4e0a\u7684\u4efb\u4f55\u989c\u8272\u3002\u901a\u5e38\uff0c\u7ed9\u5b9a\u50cf\u7d20\u7684 R\u3001G \u548c B \u7684\u6570\u91cf\u7531 8 \u4f4d\u65e0\u7b26\u53f7\u6574\u6570\u8868\u793a\u3002\u4f8b\u5982\uff0c\u7ea2\u8272\u50cf\u7d20\u7684 R=255\u3001G=0\u3001B=0\u3002\u7d2b\u8272\u50cf\u7d20\u7684 R=255\u3001G=0\u3001B=255\u3002\u4e2d\u7070\u8272\u50cf\u7d20\u5c06\u5177\u6709 R=127\u3001G=127\u3001B=127\u3002 \u5728\u4e3a\u50cf\u7d20\u5206\u914d\u989c\u8272\u503c\u65f6\uff0c\u9664\u4e86 R\u3001G \u548c B \u4e4b\u5916\uff0c\u901a\u5e38\u8fd8\u4f1a\u4f7f\u7528\u79f0\u4e3a A \u7684\u7b2c 4 \u4e2a\u503c\u3002\u201cA\u201d\u4ee3\u8868\u201calpha\u201d\uff0c\u5b83\u63a7\u5236\u989c\u8272\u7684\u900f\u660e\u5ea6\u3002\u5982\u679c A=0\uff0c\u989c\u8272\u662f\u5b8c\u5168\u900f\u660e\u7684\u3002\u5982\u679c A=255\uff0c\u5219\u989c\u8272\u4e0d\u900f\u660e\u3002 R\u3001G\u3001B \u548c A \u901a\u5e38\u5b58\u50a8\u4e3a\u5355\u4e2a 32 \u4f4d\u6574\u6570\uff0c\u6bcf\u4e2a\u5206\u91cf\u4f7f\u7528 8 \u4f4d\uff1a 32 \u4f4d RGBA \u503c 31-24 \u4f4d 23-16 \u4f4d 15-8 \u4f4d 7-0 \u4f4d RRRRRRRR GGGGGGGG BBBBBBBB AAAAAAAA red green blue alpha \u4e0b\u9762\u7684\u7a0b\u5e8f\u8981\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a 32 \u4f4d\u7684\u5341\u516d\u8fdb\u5236\u503c\uff0c\u7136\u540e\u63d0\u53d6 R\u3001G\u3001B \u548c A \u7684 8 \u4f4d\u989c\u8272\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include #include int main () { constexpr std :: uint32_t redBits { 0xFF000000 }; constexpr std :: uint32_t greenBits { 0x00FF0000 }; constexpr std :: uint32_t blueBits { 0x0000FF00 }; constexpr std :: uint32_t alphaBits { 0x000000FF }; std :: cout << \"Enter a 32-bit RGBA color value in hexadecimal (e.g. FF7F3300): \" ; std :: uint32_t pixel {}; std :: cin >> std :: hex >> pixel ; // std::hex allows us to read in a hex value // use Bitwise AND to isolate red pixels, // then right shift the value into the lower 8 bits std :: uint8_t red { static_cast < std :: uint8_t > (( pixel & redBits ) >> 24 ) }; std :: uint8_t green { static_cast < std :: uint8_t > (( pixel & greenBits ) >> 16 ) }; std :: uint8_t blue { static_cast < std :: uint8_t > (( pixel & blueBits ) >> 8 ) }; std :: uint8_t alpha { static_cast < std :: uint8_t > ( pixel & alphaBits ) }; std :: cout << \"Your color contains: \\n \" ; std :: cout << std :: hex ; // print the following values in hex std :: cout << static_cast < int > ( red ) << \" red \\n \" ; std :: cout << static_cast < int > ( green ) << \" green \\n \" ; std :: cout << static_cast < int > ( blue ) << \" blue \\n \" ; std :: cout << static_cast < int > ( alpha ) << \" alpha \\n \" ; return 0 ; } \u8fd9\u4f1a\u4ea7\u751f\u8f93\u51fa\uff1a 1 2 3 4 5 6 Enter a 32-bit RGBA color value in hexadecimal (e.g. FF7F3300): FF7F3300 Your color contains: ff red 7f green 33 blue 0 alpha \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u4e0e \u6765\u67e5\u8be2\u6211\u4eec\u611f\u5174\u8da3\u7684 8 \u4f4d\u96c6\u5408\uff0c\u7136\u540e\u5c06\u5b83\u4eec \u53f3 \u79fb\u5230\u4e00\u4e2a 8 \u4f4d\u503c\u4e2d\uff0c\u4ee5\u4fbf\u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u4eec\u4f5c\u4e3a\u5341\u516d\u8fdb\u5236\u503c\u6253\u5370\u56de\u6765\u3002","title":"\u6d89\u53ca\u591a\u4e2a\u4f4d\u7684\u4f4d\u63a9\u7801"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#_6","text":"\u603b\u7ed3\u5982\u4f55\u8bbe\u7f6e\u3001\u6e05\u9664\u3001\u5207\u6362\u548c\u67e5\u8be2\u4f4d\u6807\u5fd7\uff1a \u8981\u67e5\u8be2\u4f4d\u72b6\u6001\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u4e0e \uff1a 1 if ( flags & option4 ) ... // if option4 is set, do something \u8981\u8bbe\u7f6e\u4f4d\uff08\u6253\u5f00\uff09\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u6216 \uff1a 1 2 flags |= option4 ; // turn option 4 on. flags |= ( option4 | option5 ); // turn options 4 and 5 on. \u8981\u6e05\u9664\u4f4d\uff08\u5173\u95ed\uff09\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d AND \u548c \u6309\u4f4d NOT \uff1a 1 2 flags &= ~ option4 ; // turn option 4 off flags &= ~ ( option4 | option5 ); // turn options 4 and 5 off \u8981\u7ffb\u8f6c\u4f4d\u72b6\u6001\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u5f02\u6216 \uff1a 1 2 flags ^= option4 ; // flip option4 from on to off, or vice versa flags ^= ( option4 | option5 ); // flip options 4 and 5","title":"\u6982\u62ec"},{"location":"O-4-converting-between-binary-and-decimal/","tags":["bit"],"text":"Key Takeaway \u8003\u8651\u4e00\u4e2a\u666e\u901a\u7684\u5341\u8fdb\u5236\u6570\uff0c\u6bd4\u5982 5623\u3002\u6211\u4eec\u76f4\u89c2\u5730\u7406\u89e3\u8fd9\u4e9b\u6570\u5b57\u7684\u610f\u601d\u662f (5 * 1000) + (6 * 100) + (2 * 10) + (3 * 1)\u3002\u56e0\u4e3a\u6709 10 \u4e2a\u5341\u8fdb\u5236\u6570\uff0c\u6240\u4ee5\u5de6\u8fb9\u6bcf\u4e2a\u540e\u7eed\u6570\u5b57\u7684\u503c\u90fd\u4f1a\u589e\u52a0 10 \u500d\u3002 \u4e8c\u8fdb\u5236\u6570\u7684\u5de5\u4f5c\u65b9\u5f0f\u76f8\u540c\uff0c\u53ea\u662f\u56e0\u4e3a\u53ea\u6709 2 \u4e2a\u4e8c\u8fdb\u5236\u6570\u5b57\uff080 \u548c 1\uff09\uff0c\u6bcf\u4e2a\u6570\u5b57\u7684\u503c\u589e\u52a0 2 \u500d\u3002\u5c31\u50cf\u9017\u53f7\u901a\u5e38\u7528\u4e8e\u4f7f\u5927\u5341\u8fdb\u5236\u6570\u6613\u4e8e\u9605\u8bfb\uff08\u4f8b\u59821,427,435)\uff0c\u6211\u4eec\u7ecf\u5e38\u4ee5 4 \u4f4d\u4e3a\u4e00\u7ec4\u7f16\u5199\u4e8c\u8fdb\u5236\u6570\uff0c\u4ee5\u4fbf\u4e8e\u9605\u8bfb\uff08\u4f8b\u5982 1101 0101\uff09\u3002 \u4e0b\u8868\u4ee5\u5341\u8fdb\u5236\u548c\u4e8c\u8fdb\u5236\u8ba1\u6570\u5230 15\uff1a \u5341\u8fdb\u5236\u503c \u4e8c\u8fdb\u5236\u503c 0 0 1 1 2 10 3 11 4 100 5 101 6 110 7 111 8 1000 9 1001 10 1010 11 1011 12 1100 13 1101 14 1110 15 1111 \u5c06\u4e8c\u8fdb\u5236\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236 \u5728\u4ee5\u4e0b\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5047\u8bbe\u6211\u4eec\u6b63\u5728\u5904\u7406\u65e0\u7b26\u53f7\u6574\u6570\u3002 \u8003\u8651 8 \u4f4d\uff081 \u5b57\u8282\uff09\u4e8c\u8fdb\u5236\u6570 0101 1110\u3002 \u4e8c\u8fdb\u5236 0101 1110 \u8868\u793a (0 * 128) + (1 * 64) + (0 * 32) + (1 * 16) + (1 * 8) + (1 * 4) + (1 * 2) + (0 * 1)\u3002\u5982\u679c\u6211\u4eec\u5c06\u6240\u6709\u8fd9\u4e9b\u90e8\u5206\u76f8\u52a0\uff0c\u6211\u4eec\u5f97\u5230\u5341\u8fdb\u5236\u6570 64 + 16 + 8 + 4 + 2 = 94\u3002 \u8fd9\u662f\u8868\u683c\u683c\u5f0f\u7684\u76f8\u540c\u8fc7\u7a0b\u3002\u6211\u4eec\u5c06\u6bcf\u4e2a\u4e8c\u8fdb\u5236\u6570\u5b57\u4e58\u4ee5\u5176\u6570\u5b57\u503c\uff08\u7531\u5176\u4f4d\u7f6e\u786e\u5b9a\uff09\u3002\u5c06\u6240\u6709\u8fd9\u4e9b\u503c\u52a0\u8d77\u6765\u5c31\u662f\u603b\u6570\u3002 \u5c06 0101 1110 \u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\uff1a \u4e8c\u8fdb\u5236\u6570\u5b57 0 1 0 1 1 1 1 0 * \u6570\u5b57\u503c 128 64 32 16 8 4 2 1 = \u603b\u8ba1 (94) 0 64 0 16 8 4 2 0 \u8ba9\u6211\u4eec\u5c06 1001 0111 \u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\uff1a \u4e8c\u8fdb\u5236\u6570\u5b57 1 0 0 1 0 1 1 1 * \u6570\u5b57\u503c 128 64 32 16 8 4 2 1 = \u603b\u8ba1 (151) 128 0 0 16 0 4 2 1 1001 0111 \u4e8c\u8fdb\u5236 = \u5341\u8fdb\u5236\u7684 151\u3002 \u8fd9\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u6269\u5c55\u5230 16 \u4f4d\u6216 32 \u4f4d\u4e8c\u8fdb\u5236\u6570\uff0c\u53ea\u9700\u6dfb\u52a0\u66f4\u591a\u5217\u5373\u53ef\u3002\u8bf7\u6ce8\u610f\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4ece\u53f3\u7aef\u5f00\u59cb\uff0c\u7136\u540e\u5411\u5de6\u79fb\u52a8\uff0c\u8fb9\u8d70\u8fb9\u5c06\u6570\u5b57\u503c\u4e58\u4ee5 2\u3002 \u5341\u8fdb\u5236\u8f6c\u4e8c\u8fdb\u5236\u7684\u65b9\u6cd5\u4e00 \u4ece\u5341\u8fdb\u5236\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u6709\u70b9\u68d8\u624b\uff0c\u4f46\u4ecd\u7136\u975e\u5e38\u7b80\u5355\u3002\u6709\u4e24\u79cd\u597d\u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\u3002 \u7b2c\u4e00\u79cd\u65b9\u6cd5\u662f\u8fde\u7eed\u9664\u4ee5 2\uff0c\u7136\u540e\u8bb0\u4e0b\u4f59\u6570\u3002\u4e8c\u8fdb\u5236\u6570\u662f\u5728\u4f59\u6570\u7684\u672b\u5c3e\u6784\u9020\u7684\uff0c\u4ece\u4e0b\u5230\u4e0a\u3002 \u5c06 148 \u4ece\u5341\u8fdb\u5236\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\uff08\u4f7f\u7528 r \u8868\u793a\u4f59\u6570\uff09\uff1a 148 / 2 = 74 r0 74 / 2 = 37 r0 37 / 2 = 18 r1 18 / 2 = 9 r0 9 / 2 = 4 r1 4 / 2 = 2 r0 2 / 2 = 1 r0 1 / 2 = 0 r1 \u4ece\u4e0b\u5f80\u4e0a \u5199\u51fa\u6240\u6709\u4f59\u6570\uff1a1001 0100 148 \u5341\u8fdb\u5236 = 1001 0100 \u4e8c\u8fdb\u5236\u3002 \u60a8\u53ef\u4ee5\u901a\u8fc7\u5c06\u4e8c\u8fdb\u5236\u8f6c\u6362\u56de\u5341\u8fdb\u5236\u6765\u9a8c\u8bc1\u6b64\u7b54\u6848\uff1a (1 * 128) + (0 * 64) + (0 * 32) + (1 * 16) + (0 * 8) + (1 * 4) + (0 * 2) + (0 * 1) = 148 \u5341\u8fdb\u5236\u8f6c\u4e8c\u8fdb\u5236\u7684\u65b9\u6cd5\u4e8c \u7b2c\u4e8c\u79cd\u65b9\u6cd5\u6d89\u53ca\u9006\u5411\u8ba1\u7b97\u6bcf\u4e2a\u4f4d\u5fc5\u987b\u662f\u4ec0\u4e48\u3002\u5bf9\u4e8e\u8f83\u5c0f\u7684\u4e8c\u8fdb\u5236\u6570\uff0c\u6b64\u65b9\u6cd5\u53ef\u80fd\u66f4\u5bb9\u6613\u3002 \u518d\u6b21\u8003\u8651\u5341\u8fdb\u5236\u6570 148\u3002\u5c0f\u4e8e148\u76842\u7684\u6700\u5927\u6b21\u65b9\u662f\u591a\u5c11\uff1f128\uff0c\u6240\u4ee5\u6211\u4eec\u5c06\u4ece\u90a3\u91cc\u5f00\u59cb\u3002 \u662f 148 >= 128 \u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 128 \u4f4d\u5fc5\u987b\u662f 1. 148 - 128 = 20\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u9700\u8981\u518d\u627e\u5230\u503c 20 \u7684\u4f4d\u3002 \u662f 20 >= 64 \u5417\uff1f\u4e0d\uff0c\u6240\u4ee564\u4f4d\u5fc5\u987b\u662f0\u300220 >=32\u5417\uff1f\u4e0d\u662f\uff0c\u6240\u4ee532\u4f4d\u4e00\u5b9a\u662f0\u300220 >= 16\u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 16 \u4f4d\u5fc5\u987b\u662f 1. 20 - 16 = 4\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u9700\u8981\u627e\u5230\u66f4\u591a\u7684 4 \u4f4d\u3002 \u662f 4 >= 8 \u5417\uff1f\u4e0d\u662f\uff0c\u6240\u4ee58\u4f4d\u4e00\u5b9a\u662f0\u3002 \u662f4 >= 4\u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5\u7b2c 4 \u4f4d\u5fc5\u987b\u662f 1\u30024 - 4 = 0\uff0c\u8fd9\u610f\u5473\u7740\u6240\u6709\u5176\u4f59\u4f4d\u5fc5\u987b\u662f 0\u3002 148 = (1 * 128) + (0 * 64) + (0 * 32) + (1 * 16) + (0 * 8) + (1 * 4) + (0 * 2) + (0 * 1) = 1001 0100 \u8868\u683c\u5f62\u5f0f\uff1a \u4e8c\u8fdb\u5236\u6570 1 0 0 1 0 1 0 0 * \u6570\u5b57\u503c 128 64 32 16 8 4 2 1 = \u603b\u8ba1 (148) 128 0 0 16 0 4 0 0 \u53e6\u4e00\u4e2a\u4f8b\u5b50 \u8ba9\u6211\u4eec\u4f7f\u7528\u65b9\u6cd5 1 \u5c06 117 \u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\uff1a 117 / 2 = 58 r1 58 / 2 = 29 r0 29 / 2 = 14 r1 14 / 2 = 7 r0 7 / 2 = 3 r1 3 / 2 = 1 r1 1 / 2 = 0 r1 \u4ece\u4e0b\u5f80\u4e0a\u7684\u4f59\u6570\u6784\u9020\u6570\uff0c117 = 111 0101 \u4e8c\u8fdb\u5236 \u5e76\u4f7f\u7528\u65b9\u6cd52\uff1a 2 \u5c0f\u4e8e 117 \u7684\u6700\u5927\u6b21\u65b9\u662f 64\u3002 \u662f 117 >= 64 \u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 64 \u4f4d\u5fc5\u987b\u662f 1\u3002117 - 64 = 53\u300253 >= 32 \u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 32 \u4f4d\u5fc5\u987b\u662f 1\u300253 - 32 = 21\u300221 >= 16 \u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 16 \u4f4d\u5fc5\u987b\u662f 1\u3002 21 - 16 = 5\u3002 5 >= 8 \u5417\uff1f\u4e0d\u662f\uff0c\u6240\u4ee58\u4f4d\u4e00\u5b9a\u662f0\u3002 \u662f5 >= 4\u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5\u7b2c 4 \u4f4d\u5fc5\u987b\u662f 1\u30025 - 4 = 1\u30021 >= 2\uff1f\u4e0d\u662f\uff0c\u6240\u4ee52\u4f4d\u4e00\u5b9a\u662f0\u3002 \u662f1 >= 1\u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 1 \u4f4d\u5fc5\u987b\u662f 1\u3002 117 \u5341\u8fdb\u5236 = 111 0101 \u4e8c\u8fdb\u5236\u3002 \u76f8\u52a0\u4e8c\u8fdb\u5236\u6570 \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff08\u7a0d\u540e\u6211\u4eec\u4f1a\u770b\u5230\uff09\uff0c\u80fd\u591f\u5c06\u4e24\u4e2a\u4e8c\u8fdb\u5236\u6570\u76f8\u52a0\u662f\u5f88\u6709\u7528\u7684\u3002\u76f8\u52a0\u4e8c\u8fdb\u5236\u6570\u51fa\u5947\u5730\u5bb9\u6613\uff08\u751a\u81f3\u53ef\u80fd\u6bd4\u6dfb\u52a0\u5341\u8fdb\u5236\u6570\u66f4\u5bb9\u6613\uff09\uff0c\u5c3d\u7ba1\u4e00\u5f00\u59cb\u5b83\u53ef\u80fd\u770b\u8d77\u6765\u5f88\u5947\u602a\uff0c\u56e0\u4e3a\u60a8\u4e0d\u4e60\u60ef\u5b83\u3002 \u8003\u8651\u4e24\u4e2a\u5c0f\u7684\u4e8c\u8fdb\u5236\u6570\uff1a 0110\uff08\u5341\u8fdb\u5236\u4e3a 6\uff09+ 0111\uff08\u5341\u8fdb\u5236\u4e3a 7\uff09 \u8ba9\u6211\u4eec\u6dfb\u52a0\u8fd9\u4e9b\u3002\u9996\u5148\uff0c\u50cf\u4e0a\u9762\u4e00\u6837\u5c06\u5b83\u4eec\u6392\u6210\u4e00\u884c\u3002\u7136\u540e\uff0c\u4ece\u53f3\u5411\u5de6\u5f00\u59cb\uff0c\u6211\u4eec\u5c06\u6bcf\u4e00\u5217\u6570\u5b57\u76f8\u52a0\uff0c\u5c31\u50cf\u6211\u4eec\u5728\u5341\u8fdb\u5236\u6570\u4e2d\u6240\u505a\u7684\u90a3\u6837\u3002\u4f46\u662f\uff0c\u7531\u4e8e\u4e8c\u8fdb\u5236\u6570\u5b57\u53ea\u80fd\u662f 0 \u6216 1\uff0c\u6240\u4ee5\u53ea\u6709 4 \u79cd\u53ef\u80fd\uff1a 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 0\uff0c\u5c06 1 \u79fb\u81f3\u4e0b\u4e00\u5217 \u8ba9\u6211\u4eec\u505a\u7b2c\u4e00\u5217\uff1a 1 2 3 4 0110\uff08\u5341\u8fdb\u5236\u4e3a 6\uff09+ 0111\uff08\u5341\u8fdb\u5236\u4e3a 7\uff09 ---- 1 0 + 1 = 1\u3002\u7b80\u5355\u3002 \u7b2c\u4e8c\u680f\uff1a 1 2 3 4 5 1 0110\uff08\u5341\u8fdb\u5236\u4e3a 6\uff09+ 0111\uff08\u5341\u8fdb\u5236\u4e3a 7\uff09 ---- 01 1 + 1 = 0\uff0c\u5e26\u4e00\u4e2a\u8fdb\u4f4d\u5230\u4e0b\u4e00\u5217 \u7b2c\u4e09\u680f\uff1a 1 2 3 4 5 11 0110\uff08\u5341\u8fdb\u5236\u4e3a 6\uff09+ 0111\uff08\u5341\u8fdb\u5236\u4e3a 7\uff09 ---- 101 \u8fd9\u4e2a\u6709\u70b9\u68d8\u624b\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c1 + 1 = 0\uff0c\u5e26\u4e00\u4e2a\u8fdb\u4f4d\u5230\u4e0b\u4e00\u5217\u3002\u4f46\u662f\uff0c\u6211\u4eec\u5df2\u7ecf\u4ece\u4e0a\u4e00\u5217\u4e2d\u643a\u5e26\u4e86\u4e00\u4e2a 1\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u52a0 1\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5728\u8fd9\u4e00\u5217\u4e2d\u5f97\u5230\u4e00\u4e2a 1\uff0c\u5e76\u5c06 1 \u5e26\u5165\u4e0b\u4e00\u5217 \u6700\u540e\u4e00\u680f\uff1a 1 2 3 4 5 11 0110\uff08\u5341\u8fdb\u5236\u4e3a 6\uff09+ 0111\uff08\u5341\u8fdb\u5236\u4e3a 7\uff09 ---- 1101 0 + 0 = 0\uff0c\u4f46\u6709\u4e00\u4e2a\u8fdb\u4f4d\u7684 1\uff0c\u6240\u4ee5\u6211\u4eec\u52a0 1\u30021101 = \u5341\u8fdb\u5236\u7684 13\u3002 \u73b0\u5728\uff0c\u6211\u4eec\u5982\u4f55\u7ed9\u4efb\u4f55\u7ed9\u5b9a\u7684\u4e8c\u8fdb\u5236\u6570\uff08\u4f8b\u5982 1011 0011\uff09\u52a0 1\uff1f\u540c\u4e0a\uff0c\u53ea\u662f\u6700\u4e0b\u9762\u7684\u6570\u662f\u4e8c\u8fdb\u52361\u3002 1 2 3 4 5 1\u4e2a\uff08\u8fdb\u4f4d\u67f1\uff09 1011 0011\uff08\u539f\u59cb\u4e8c\u8fdb\u5236\u6570\uff09 0000 0001\uff08\u4e8c\u8fdb\u5236\u4e3a 1\uff09 --------- 1011 0100 \u6709\u7b26\u53f7\u6570\u548c\u8865\u7801 \u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u53ea\u5904\u7406\u4e86\u65e0\u7b26\u53f7\u6574\u6570\u3002\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4e86\u89e3\u5982\u4f55\u5904\u7406\u6709\u7b26\u53f7\u6570\uff08\u53ef\u4ee5\u4e3a\u8d1f\u6570\uff09\u3002 \u6709\u7b26\u53f7\u6574\u6570\u901a\u5e38\u4f7f\u7528\u79f0\u4e3a \u4e8c\u8fdb\u5236\u8865\u7801 \u7684\u65b9\u6cd5\u5b58\u50a8\u3002\u5728\u4e8c\u8fdb\u5236\u8865\u7801\u4e2d\uff0c\u6700\u5de6\u8fb9\uff08\u6700\u9ad8\u6709\u6548\u4f4d\uff09\u7684\u4f4d\u7528\u4f5c\u7b26\u53f7\u4f4d\u3002\u7b26\u53f7\u4f4d 0 \u8868\u793a\u6570\u5b57\u4e3a\u6b63\u6570\uff0c\u7b26\u53f7\u4f4d 1 \u8868\u793a\u6570\u5b57\u4e3a\u8d1f\u6570\u3002 \u6b63\u7b26\u53f7\u6570\u4e0e\u65e0\u7b26\u53f7\u6b63\u6570\u4e00\u6837\u4ee5\u4e8c\u8fdb\u5236\u8868\u793a\uff08\u7b26\u53f7\u4f4d\u8bbe\u7f6e\u4e3a 0\uff09\u3002 \u8d1f\u7b26\u53f7\u6570\u4ee5\u4e8c\u8fdb\u5236\u8868\u793a\u4e3a\u6b63\u6570\u7684\u6309\u4f4d\u53d6\u53cd\u52a0 1\u3002 \u5341\u8fdb\u5236\u8f6c\u4e8c\u8fdb\u5236\uff08\u4e8c\u8fdb\u5236\u8865\u7801\uff09 \u4f8b\u5982\uff0c\u8fd9\u662f\u6211\u4eec\u5982\u4f55\u7528\u4e8c\u8fdb\u5236\u4e8c\u8fdb\u5236\u8865\u7801\u8868\u793a -5\uff1a \u9996\u5148\u6211\u4eec\u627e\u51fa 5 \u7684\u4e8c\u8fdb\u5236\u8868\u793a\uff1a0000 0101 \u7136\u540e\u6211\u4eec\u53cd\u8f6c\u6240\u6709\u4f4d\uff1a1111 1010 \u7136\u540e\u6211\u4eec\u6dfb\u52a0 1\uff1a1111 1011 \u5c06 -76 \u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\uff1a \u4e8c\u8fdb\u5236\u6b63 76\uff1a0100 1100 \u53cd\u8f6c\u6240\u6709\u4f4d\uff1a1011 0011 \u52a0 1\uff1a1011 0100 \u4e3a\u4ec0\u4e48\u8981\u52a01\uff1f\u8003\u8651\u6570\u5b57 0\u3002\u5982\u679c\u5c06\u8d1f\u503c\u7b80\u5355\u5730\u8868\u793a\u4e3a\u6b63\u6570\u7684\u5012\u6570\uff0c\u5219 0 \u5c06\u6709\u4e24\u79cd\u8868\u793a\u5f62\u5f0f\uff1a0000 0000\uff08\u6b63\u96f6\uff09\u548c 1111 1111\uff08\u8d1f\u96f6\uff09\u3002\u901a\u8fc7\u52a0 1\uff0c1111 1111 \u6709\u610f\u6ea2\u51fa\u5e76\u53d8\u6210 0000 0000\u3002\u8fd9\u53ef\u4ee5\u9632\u6b62 0 \u6709\u4e24\u79cd\u8868\u793a\u5f62\u5f0f\uff0c\u5e76\u7b80\u5316\u4e86\u4e00\u4e9b\u7528\u8d1f\u6570\u8fdb\u884c\u7b97\u672f\u8fd0\u7b97\u6240\u9700\u7684\u5185\u90e8\u903b\u8f91\u3002 \u5c06\u4e8c\u8fdb\u5236\uff08\u4e8c\u8fdb\u5236\u8865\u7801\uff09\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236 \u8981\u5c06\u4e8c\u8fdb\u5236\u8865\u7801\u8f6c\u6362\u56de\u5341\u8fdb\u5236\uff0c\u9996\u5148\u8981\u67e5\u770b\u7b26\u53f7\u4f4d\u3002 \u5982\u679c\u7b26\u53f7\u4f4d\u4e3a 0\uff0c\u53ea\u9700\u6309\u7167\u4e0a\u9762\u65e0\u7b26\u53f7\u6570\u6240\u793a\u7684\u65b9\u5f0f\u8f6c\u6362\u6570\u5b57\u3002 \u5982\u679c\u7b26\u53f7\u4f4d\u662f1\uff0c\u90a3\u4e48\u6211\u4eec\u628a\u4f4d\u53d6\u53cd\uff0c\u52a01\uff0c\u7136\u540e\u8f6c\u6210\u5341\u8fdb\u5236\uff0c\u7136\u540e\u8ba9\u90a3\u4e2a\u5341\u8fdb\u5236\u6570\u53d8\u6210\u8d1f\u6570\uff08\u56e0\u4e3a\u7b26\u53f7\u4f4d\u672c\u6765\u5c31\u662f\u8d1f\u6570\uff09\u3002 \u4f8b\u5982\uff0c\u8981\u5c06 1001 1110 \u4ece\u4e8c\u8fdb\u5236\u8865\u7801\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u6570\uff1a \u7ed9\u5b9a\uff1a1001 1110 \u53cd\u8f6c\u4f4d\uff1a0110 0001 \u52a0 1\uff1a0110 0010 \u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\uff1a(0 * 128) + (1 * 64) + (1 * 32 ) + (0 * 16) + (0 * 8) + (0 * 4) + (1 * 2) + (0 * 1) = 64 + 32 + 2 = 98 \u7531\u4e8e\u539f\u59cb\u7b26\u53f7\u4f4d\u4e3a\u8d1f\uff0c\u6240\u4ee5\u6700\u540e\u7684\u503c\u662f-98\u3002 \u4e3a\u4ec0\u4e48\u7c7b\u578b\u5f88\u91cd\u8981 \u8003\u8651\u4e8c\u8fdb\u5236\u503c 1011 0100\u3002\u8fd9\u4ee3\u8868\u4ec0\u4e48\u503c\uff1f\u4f60\u53ef\u80fd\u4f1a\u8bf4 180\uff0c\u5982\u679c\u8fd9\u662f\u4e00\u4e2a\u6807\u51c6\u7684\u65e0\u7b26\u53f7\u4e8c\u8fdb\u5236\u6570\uff0c\u4f60\u5c31\u662f\u5bf9\u7684\u3002 \u4f46\u662f\uff0c\u5982\u679c\u4f7f\u7528\u4e8c\u8fdb\u5236\u8865\u7801\u5b58\u50a8\u6b64\u503c\uff0c\u5219\u5b83\u5c06\u662f -76\u3002 \u5982\u679c\u8be5\u503c\u4ee5\u5176\u4ed6\u65b9\u5f0f\u7f16\u7801\uff0c\u5219\u5b83\u53ef\u80fd\u5b8c\u5168\u4e0d\u540c\u3002 \u90a3\u4e48 C++ \u662f\u5982\u4f55\u77e5\u9053\u5c06\u5305\u542b\u4e8c\u8fdb\u5236 1011 0100 \u7684\u53d8\u91cf\u6253\u5370\u4e3a 180 \u8fd8\u662f -76 \u5462\uff1f \u5982\u679c\u7ae0\u8282\u6807\u9898\u6ca1\u6709\u900f\u9732\uff0c\u8fd9\u5c31\u662f\u7c7b\u578b\u53d1\u6325\u4f5c\u7528\u7684\u5730\u65b9\u3002\u53d8\u91cf\u7684\u7c7b\u578b\u51b3\u5b9a\u4e86\u53d8\u91cf\u7684\u503c\u5982\u4f55\u7f16\u7801\u4e3a\u4e8c\u8fdb\u5236\uff0c\u4ee5\u53ca\u5982\u4f55\u89e3\u7801\u56de\u4e00\u4e2a\u503c\u3002\u56e0\u6b64\uff0c \u5982\u679c\u53d8\u91cf\u7c7b\u578b\u662f\u65e0\u7b26\u53f7\u6574\u6570\uff0c\u5b83\u4f1a\u77e5\u9053 1011 0100 \u662f\u6807\u51c6\u4e8c\u8fdb\u5236\uff0c\u5e94\u8be5\u6253\u5370\u4e3a 180\u3002\u5982\u679c\u53d8\u91cf\u662f\u6709\u7b26\u53f7\u6574\u6570\uff0c\u5b83\u4f1a\u77e5\u9053 1011 0100 \u662f\u4f7f\u7528\u4e8c\u8fdb\u5236\u8865\u7801\u7f16\u7801\u7684\uff08\u73b0\u5728\u4fdd\u8bc1\u4e3aC++20 \u7684\uff09\uff0c\u5e76\u4e14\u5e94\u8be5\u6253\u5370\u4e3a -76 \u3002 \u5982\u4f55\u5c06\u6d6e\u70b9\u6570\u4ece\u4e8c\u8fdb\u5236\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\uff1f \u6d6e\u70b9\u6570\u5982\u4f55\u4ece\u4e8c\u8fdb\u5236\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u8981\u590d\u6742\u5f97\u591a\uff0c\u800c\u4e14\u60a8\u53ef\u80fd\u6c38\u8fdc\u90fd\u4e0d\u9700\u8981\u77e5\u9053\u3002\u4f46\u662f\uff0c\u5982\u679c\u60a8\u5f88\u597d\u5947\uff0c\u8bf7\u8bbf\u95ee \u6b64\u7ad9\u70b9 \uff0c\u8be5\u7ad9\u70b9\u5f88\u597d\u5730\u8be6\u7ec6\u89e3\u91ca\u4e86\u8be5\u4e3b\u9898\u3002","title":"O.4 - \u4e8c\u8fdb\u5236\u548c\u5341\u8fdb\u5236\u8f6c\u6362"},{"location":"O-4-converting-between-binary-and-decimal/#_1","text":"\u5728\u4ee5\u4e0b\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5047\u8bbe\u6211\u4eec\u6b63\u5728\u5904\u7406\u65e0\u7b26\u53f7\u6574\u6570\u3002 \u8003\u8651 8 \u4f4d\uff081 \u5b57\u8282\uff09\u4e8c\u8fdb\u5236\u6570 0101 1110\u3002 \u4e8c\u8fdb\u5236 0101 1110 \u8868\u793a (0 * 128) + (1 * 64) + (0 * 32) + (1 * 16) + (1 * 8) + (1 * 4) + (1 * 2) + (0 * 1)\u3002\u5982\u679c\u6211\u4eec\u5c06\u6240\u6709\u8fd9\u4e9b\u90e8\u5206\u76f8\u52a0\uff0c\u6211\u4eec\u5f97\u5230\u5341\u8fdb\u5236\u6570 64 + 16 + 8 + 4 + 2 = 94\u3002 \u8fd9\u662f\u8868\u683c\u683c\u5f0f\u7684\u76f8\u540c\u8fc7\u7a0b\u3002\u6211\u4eec\u5c06\u6bcf\u4e2a\u4e8c\u8fdb\u5236\u6570\u5b57\u4e58\u4ee5\u5176\u6570\u5b57\u503c\uff08\u7531\u5176\u4f4d\u7f6e\u786e\u5b9a\uff09\u3002\u5c06\u6240\u6709\u8fd9\u4e9b\u503c\u52a0\u8d77\u6765\u5c31\u662f\u603b\u6570\u3002 \u5c06 0101 1110 \u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\uff1a \u4e8c\u8fdb\u5236\u6570\u5b57 0 1 0 1 1 1 1 0 * \u6570\u5b57\u503c 128 64 32 16 8 4 2 1 = \u603b\u8ba1 (94) 0 64 0 16 8 4 2 0 \u8ba9\u6211\u4eec\u5c06 1001 0111 \u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\uff1a \u4e8c\u8fdb\u5236\u6570\u5b57 1 0 0 1 0 1 1 1 * \u6570\u5b57\u503c 128 64 32 16 8 4 2 1 = \u603b\u8ba1 (151) 128 0 0 16 0 4 2 1 1001 0111 \u4e8c\u8fdb\u5236 = \u5341\u8fdb\u5236\u7684 151\u3002 \u8fd9\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u6269\u5c55\u5230 16 \u4f4d\u6216 32 \u4f4d\u4e8c\u8fdb\u5236\u6570\uff0c\u53ea\u9700\u6dfb\u52a0\u66f4\u591a\u5217\u5373\u53ef\u3002\u8bf7\u6ce8\u610f\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4ece\u53f3\u7aef\u5f00\u59cb\uff0c\u7136\u540e\u5411\u5de6\u79fb\u52a8\uff0c\u8fb9\u8d70\u8fb9\u5c06\u6570\u5b57\u503c\u4e58\u4ee5 2\u3002","title":"\u5c06\u4e8c\u8fdb\u5236\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236"},{"location":"O-4-converting-between-binary-and-decimal/#_2","text":"\u4ece\u5341\u8fdb\u5236\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u6709\u70b9\u68d8\u624b\uff0c\u4f46\u4ecd\u7136\u975e\u5e38\u7b80\u5355\u3002\u6709\u4e24\u79cd\u597d\u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\u3002 \u7b2c\u4e00\u79cd\u65b9\u6cd5\u662f\u8fde\u7eed\u9664\u4ee5 2\uff0c\u7136\u540e\u8bb0\u4e0b\u4f59\u6570\u3002\u4e8c\u8fdb\u5236\u6570\u662f\u5728\u4f59\u6570\u7684\u672b\u5c3e\u6784\u9020\u7684\uff0c\u4ece\u4e0b\u5230\u4e0a\u3002 \u5c06 148 \u4ece\u5341\u8fdb\u5236\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\uff08\u4f7f\u7528 r \u8868\u793a\u4f59\u6570\uff09\uff1a 148 / 2 = 74 r0 74 / 2 = 37 r0 37 / 2 = 18 r1 18 / 2 = 9 r0 9 / 2 = 4 r1 4 / 2 = 2 r0 2 / 2 = 1 r0 1 / 2 = 0 r1 \u4ece\u4e0b\u5f80\u4e0a \u5199\u51fa\u6240\u6709\u4f59\u6570\uff1a1001 0100 148 \u5341\u8fdb\u5236 = 1001 0100 \u4e8c\u8fdb\u5236\u3002 \u60a8\u53ef\u4ee5\u901a\u8fc7\u5c06\u4e8c\u8fdb\u5236\u8f6c\u6362\u56de\u5341\u8fdb\u5236\u6765\u9a8c\u8bc1\u6b64\u7b54\u6848\uff1a (1 * 128) + (0 * 64) + (0 * 32) + (1 * 16) + (0 * 8) + (1 * 4) + (0 * 2) + (0 * 1) = 148","title":"\u5341\u8fdb\u5236\u8f6c\u4e8c\u8fdb\u5236\u7684\u65b9\u6cd5\u4e00"},{"location":"O-4-converting-between-binary-and-decimal/#_3","text":"\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u6d89\u53ca\u9006\u5411\u8ba1\u7b97\u6bcf\u4e2a\u4f4d\u5fc5\u987b\u662f\u4ec0\u4e48\u3002\u5bf9\u4e8e\u8f83\u5c0f\u7684\u4e8c\u8fdb\u5236\u6570\uff0c\u6b64\u65b9\u6cd5\u53ef\u80fd\u66f4\u5bb9\u6613\u3002 \u518d\u6b21\u8003\u8651\u5341\u8fdb\u5236\u6570 148\u3002\u5c0f\u4e8e148\u76842\u7684\u6700\u5927\u6b21\u65b9\u662f\u591a\u5c11\uff1f128\uff0c\u6240\u4ee5\u6211\u4eec\u5c06\u4ece\u90a3\u91cc\u5f00\u59cb\u3002 \u662f 148 >= 128 \u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 128 \u4f4d\u5fc5\u987b\u662f 1. 148 - 128 = 20\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u9700\u8981\u518d\u627e\u5230\u503c 20 \u7684\u4f4d\u3002 \u662f 20 >= 64 \u5417\uff1f\u4e0d\uff0c\u6240\u4ee564\u4f4d\u5fc5\u987b\u662f0\u300220 >=32\u5417\uff1f\u4e0d\u662f\uff0c\u6240\u4ee532\u4f4d\u4e00\u5b9a\u662f0\u300220 >= 16\u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 16 \u4f4d\u5fc5\u987b\u662f 1. 20 - 16 = 4\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u9700\u8981\u627e\u5230\u66f4\u591a\u7684 4 \u4f4d\u3002 \u662f 4 >= 8 \u5417\uff1f\u4e0d\u662f\uff0c\u6240\u4ee58\u4f4d\u4e00\u5b9a\u662f0\u3002 \u662f4 >= 4\u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5\u7b2c 4 \u4f4d\u5fc5\u987b\u662f 1\u30024 - 4 = 0\uff0c\u8fd9\u610f\u5473\u7740\u6240\u6709\u5176\u4f59\u4f4d\u5fc5\u987b\u662f 0\u3002 148 = (1 * 128) + (0 * 64) + (0 * 32) + (1 * 16) + (0 * 8) + (1 * 4) + (0 * 2) + (0 * 1) = 1001 0100 \u8868\u683c\u5f62\u5f0f\uff1a \u4e8c\u8fdb\u5236\u6570 1 0 0 1 0 1 0 0 * \u6570\u5b57\u503c 128 64 32 16 8 4 2 1 = \u603b\u8ba1 (148) 128 0 0 16 0 4 0 0 \u53e6\u4e00\u4e2a\u4f8b\u5b50 \u8ba9\u6211\u4eec\u4f7f\u7528\u65b9\u6cd5 1 \u5c06 117 \u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\uff1a 117 / 2 = 58 r1 58 / 2 = 29 r0 29 / 2 = 14 r1 14 / 2 = 7 r0 7 / 2 = 3 r1 3 / 2 = 1 r1 1 / 2 = 0 r1 \u4ece\u4e0b\u5f80\u4e0a\u7684\u4f59\u6570\u6784\u9020\u6570\uff0c117 = 111 0101 \u4e8c\u8fdb\u5236 \u5e76\u4f7f\u7528\u65b9\u6cd52\uff1a 2 \u5c0f\u4e8e 117 \u7684\u6700\u5927\u6b21\u65b9\u662f 64\u3002 \u662f 117 >= 64 \u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 64 \u4f4d\u5fc5\u987b\u662f 1\u3002117 - 64 = 53\u300253 >= 32 \u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 32 \u4f4d\u5fc5\u987b\u662f 1\u300253 - 32 = 21\u300221 >= 16 \u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 16 \u4f4d\u5fc5\u987b\u662f 1\u3002 21 - 16 = 5\u3002 5 >= 8 \u5417\uff1f\u4e0d\u662f\uff0c\u6240\u4ee58\u4f4d\u4e00\u5b9a\u662f0\u3002 \u662f5 >= 4\u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5\u7b2c 4 \u4f4d\u5fc5\u987b\u662f 1\u30025 - 4 = 1\u30021 >= 2\uff1f\u4e0d\u662f\uff0c\u6240\u4ee52\u4f4d\u4e00\u5b9a\u662f0\u3002 \u662f1 >= 1\u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 1 \u4f4d\u5fc5\u987b\u662f 1\u3002 117 \u5341\u8fdb\u5236 = 111 0101 \u4e8c\u8fdb\u5236\u3002","title":"\u5341\u8fdb\u5236\u8f6c\u4e8c\u8fdb\u5236\u7684\u65b9\u6cd5\u4e8c"},{"location":"O-4-converting-between-binary-and-decimal/#_4","text":"\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff08\u7a0d\u540e\u6211\u4eec\u4f1a\u770b\u5230\uff09\uff0c\u80fd\u591f\u5c06\u4e24\u4e2a\u4e8c\u8fdb\u5236\u6570\u76f8\u52a0\u662f\u5f88\u6709\u7528\u7684\u3002\u76f8\u52a0\u4e8c\u8fdb\u5236\u6570\u51fa\u5947\u5730\u5bb9\u6613\uff08\u751a\u81f3\u53ef\u80fd\u6bd4\u6dfb\u52a0\u5341\u8fdb\u5236\u6570\u66f4\u5bb9\u6613\uff09\uff0c\u5c3d\u7ba1\u4e00\u5f00\u59cb\u5b83\u53ef\u80fd\u770b\u8d77\u6765\u5f88\u5947\u602a\uff0c\u56e0\u4e3a\u60a8\u4e0d\u4e60\u60ef\u5b83\u3002 \u8003\u8651\u4e24\u4e2a\u5c0f\u7684\u4e8c\u8fdb\u5236\u6570\uff1a 0110\uff08\u5341\u8fdb\u5236\u4e3a 6\uff09+ 0111\uff08\u5341\u8fdb\u5236\u4e3a 7\uff09 \u8ba9\u6211\u4eec\u6dfb\u52a0\u8fd9\u4e9b\u3002\u9996\u5148\uff0c\u50cf\u4e0a\u9762\u4e00\u6837\u5c06\u5b83\u4eec\u6392\u6210\u4e00\u884c\u3002\u7136\u540e\uff0c\u4ece\u53f3\u5411\u5de6\u5f00\u59cb\uff0c\u6211\u4eec\u5c06\u6bcf\u4e00\u5217\u6570\u5b57\u76f8\u52a0\uff0c\u5c31\u50cf\u6211\u4eec\u5728\u5341\u8fdb\u5236\u6570\u4e2d\u6240\u505a\u7684\u90a3\u6837\u3002\u4f46\u662f\uff0c\u7531\u4e8e\u4e8c\u8fdb\u5236\u6570\u5b57\u53ea\u80fd\u662f 0 \u6216 1\uff0c\u6240\u4ee5\u53ea\u6709 4 \u79cd\u53ef\u80fd\uff1a 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 0\uff0c\u5c06 1 \u79fb\u81f3\u4e0b\u4e00\u5217 \u8ba9\u6211\u4eec\u505a\u7b2c\u4e00\u5217\uff1a 1 2 3 4 0110\uff08\u5341\u8fdb\u5236\u4e3a 6\uff09+ 0111\uff08\u5341\u8fdb\u5236\u4e3a 7\uff09 ---- 1 0 + 1 = 1\u3002\u7b80\u5355\u3002 \u7b2c\u4e8c\u680f\uff1a 1 2 3 4 5 1 0110\uff08\u5341\u8fdb\u5236\u4e3a 6\uff09+ 0111\uff08\u5341\u8fdb\u5236\u4e3a 7\uff09 ---- 01 1 + 1 = 0\uff0c\u5e26\u4e00\u4e2a\u8fdb\u4f4d\u5230\u4e0b\u4e00\u5217 \u7b2c\u4e09\u680f\uff1a 1 2 3 4 5 11 0110\uff08\u5341\u8fdb\u5236\u4e3a 6\uff09+ 0111\uff08\u5341\u8fdb\u5236\u4e3a 7\uff09 ---- 101 \u8fd9\u4e2a\u6709\u70b9\u68d8\u624b\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c1 + 1 = 0\uff0c\u5e26\u4e00\u4e2a\u8fdb\u4f4d\u5230\u4e0b\u4e00\u5217\u3002\u4f46\u662f\uff0c\u6211\u4eec\u5df2\u7ecf\u4ece\u4e0a\u4e00\u5217\u4e2d\u643a\u5e26\u4e86\u4e00\u4e2a 1\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u52a0 1\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5728\u8fd9\u4e00\u5217\u4e2d\u5f97\u5230\u4e00\u4e2a 1\uff0c\u5e76\u5c06 1 \u5e26\u5165\u4e0b\u4e00\u5217 \u6700\u540e\u4e00\u680f\uff1a 1 2 3 4 5 11 0110\uff08\u5341\u8fdb\u5236\u4e3a 6\uff09+ 0111\uff08\u5341\u8fdb\u5236\u4e3a 7\uff09 ---- 1101 0 + 0 = 0\uff0c\u4f46\u6709\u4e00\u4e2a\u8fdb\u4f4d\u7684 1\uff0c\u6240\u4ee5\u6211\u4eec\u52a0 1\u30021101 = \u5341\u8fdb\u5236\u7684 13\u3002 \u73b0\u5728\uff0c\u6211\u4eec\u5982\u4f55\u7ed9\u4efb\u4f55\u7ed9\u5b9a\u7684\u4e8c\u8fdb\u5236\u6570\uff08\u4f8b\u5982 1011 0011\uff09\u52a0 1\uff1f\u540c\u4e0a\uff0c\u53ea\u662f\u6700\u4e0b\u9762\u7684\u6570\u662f\u4e8c\u8fdb\u52361\u3002 1 2 3 4 5 1\u4e2a\uff08\u8fdb\u4f4d\u67f1\uff09 1011 0011\uff08\u539f\u59cb\u4e8c\u8fdb\u5236\u6570\uff09 0000 0001\uff08\u4e8c\u8fdb\u5236\u4e3a 1\uff09 --------- 1011 0100","title":"\u76f8\u52a0\u4e8c\u8fdb\u5236\u6570"},{"location":"O-4-converting-between-binary-and-decimal/#_5","text":"\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u53ea\u5904\u7406\u4e86\u65e0\u7b26\u53f7\u6574\u6570\u3002\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4e86\u89e3\u5982\u4f55\u5904\u7406\u6709\u7b26\u53f7\u6570\uff08\u53ef\u4ee5\u4e3a\u8d1f\u6570\uff09\u3002 \u6709\u7b26\u53f7\u6574\u6570\u901a\u5e38\u4f7f\u7528\u79f0\u4e3a \u4e8c\u8fdb\u5236\u8865\u7801 \u7684\u65b9\u6cd5\u5b58\u50a8\u3002\u5728\u4e8c\u8fdb\u5236\u8865\u7801\u4e2d\uff0c\u6700\u5de6\u8fb9\uff08\u6700\u9ad8\u6709\u6548\u4f4d\uff09\u7684\u4f4d\u7528\u4f5c\u7b26\u53f7\u4f4d\u3002\u7b26\u53f7\u4f4d 0 \u8868\u793a\u6570\u5b57\u4e3a\u6b63\u6570\uff0c\u7b26\u53f7\u4f4d 1 \u8868\u793a\u6570\u5b57\u4e3a\u8d1f\u6570\u3002 \u6b63\u7b26\u53f7\u6570\u4e0e\u65e0\u7b26\u53f7\u6b63\u6570\u4e00\u6837\u4ee5\u4e8c\u8fdb\u5236\u8868\u793a\uff08\u7b26\u53f7\u4f4d\u8bbe\u7f6e\u4e3a 0\uff09\u3002 \u8d1f\u7b26\u53f7\u6570\u4ee5\u4e8c\u8fdb\u5236\u8868\u793a\u4e3a\u6b63\u6570\u7684\u6309\u4f4d\u53d6\u53cd\u52a0 1\u3002 \u5341\u8fdb\u5236\u8f6c\u4e8c\u8fdb\u5236\uff08\u4e8c\u8fdb\u5236\u8865\u7801\uff09 \u4f8b\u5982\uff0c\u8fd9\u662f\u6211\u4eec\u5982\u4f55\u7528\u4e8c\u8fdb\u5236\u4e8c\u8fdb\u5236\u8865\u7801\u8868\u793a -5\uff1a \u9996\u5148\u6211\u4eec\u627e\u51fa 5 \u7684\u4e8c\u8fdb\u5236\u8868\u793a\uff1a0000 0101 \u7136\u540e\u6211\u4eec\u53cd\u8f6c\u6240\u6709\u4f4d\uff1a1111 1010 \u7136\u540e\u6211\u4eec\u6dfb\u52a0 1\uff1a1111 1011 \u5c06 -76 \u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\uff1a \u4e8c\u8fdb\u5236\u6b63 76\uff1a0100 1100 \u53cd\u8f6c\u6240\u6709\u4f4d\uff1a1011 0011 \u52a0 1\uff1a1011 0100 \u4e3a\u4ec0\u4e48\u8981\u52a01\uff1f\u8003\u8651\u6570\u5b57 0\u3002\u5982\u679c\u5c06\u8d1f\u503c\u7b80\u5355\u5730\u8868\u793a\u4e3a\u6b63\u6570\u7684\u5012\u6570\uff0c\u5219 0 \u5c06\u6709\u4e24\u79cd\u8868\u793a\u5f62\u5f0f\uff1a0000 0000\uff08\u6b63\u96f6\uff09\u548c 1111 1111\uff08\u8d1f\u96f6\uff09\u3002\u901a\u8fc7\u52a0 1\uff0c1111 1111 \u6709\u610f\u6ea2\u51fa\u5e76\u53d8\u6210 0000 0000\u3002\u8fd9\u53ef\u4ee5\u9632\u6b62 0 \u6709\u4e24\u79cd\u8868\u793a\u5f62\u5f0f\uff0c\u5e76\u7b80\u5316\u4e86\u4e00\u4e9b\u7528\u8d1f\u6570\u8fdb\u884c\u7b97\u672f\u8fd0\u7b97\u6240\u9700\u7684\u5185\u90e8\u903b\u8f91\u3002","title":"\u6709\u7b26\u53f7\u6570\u548c\u8865\u7801"},{"location":"O-4-converting-between-binary-and-decimal/#_6","text":"\u8981\u5c06\u4e8c\u8fdb\u5236\u8865\u7801\u8f6c\u6362\u56de\u5341\u8fdb\u5236\uff0c\u9996\u5148\u8981\u67e5\u770b\u7b26\u53f7\u4f4d\u3002 \u5982\u679c\u7b26\u53f7\u4f4d\u4e3a 0\uff0c\u53ea\u9700\u6309\u7167\u4e0a\u9762\u65e0\u7b26\u53f7\u6570\u6240\u793a\u7684\u65b9\u5f0f\u8f6c\u6362\u6570\u5b57\u3002 \u5982\u679c\u7b26\u53f7\u4f4d\u662f1\uff0c\u90a3\u4e48\u6211\u4eec\u628a\u4f4d\u53d6\u53cd\uff0c\u52a01\uff0c\u7136\u540e\u8f6c\u6210\u5341\u8fdb\u5236\uff0c\u7136\u540e\u8ba9\u90a3\u4e2a\u5341\u8fdb\u5236\u6570\u53d8\u6210\u8d1f\u6570\uff08\u56e0\u4e3a\u7b26\u53f7\u4f4d\u672c\u6765\u5c31\u662f\u8d1f\u6570\uff09\u3002 \u4f8b\u5982\uff0c\u8981\u5c06 1001 1110 \u4ece\u4e8c\u8fdb\u5236\u8865\u7801\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u6570\uff1a \u7ed9\u5b9a\uff1a1001 1110 \u53cd\u8f6c\u4f4d\uff1a0110 0001 \u52a0 1\uff1a0110 0010 \u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\uff1a(0 * 128) + (1 * 64) + (1 * 32 ) + (0 * 16) + (0 * 8) + (0 * 4) + (1 * 2) + (0 * 1) = 64 + 32 + 2 = 98 \u7531\u4e8e\u539f\u59cb\u7b26\u53f7\u4f4d\u4e3a\u8d1f\uff0c\u6240\u4ee5\u6700\u540e\u7684\u503c\u662f-98\u3002","title":"\u5c06\u4e8c\u8fdb\u5236\uff08\u4e8c\u8fdb\u5236\u8865\u7801\uff09\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236"},{"location":"O-4-converting-between-binary-and-decimal/#_7","text":"\u8003\u8651\u4e8c\u8fdb\u5236\u503c 1011 0100\u3002\u8fd9\u4ee3\u8868\u4ec0\u4e48\u503c\uff1f\u4f60\u53ef\u80fd\u4f1a\u8bf4 180\uff0c\u5982\u679c\u8fd9\u662f\u4e00\u4e2a\u6807\u51c6\u7684\u65e0\u7b26\u53f7\u4e8c\u8fdb\u5236\u6570\uff0c\u4f60\u5c31\u662f\u5bf9\u7684\u3002 \u4f46\u662f\uff0c\u5982\u679c\u4f7f\u7528\u4e8c\u8fdb\u5236\u8865\u7801\u5b58\u50a8\u6b64\u503c\uff0c\u5219\u5b83\u5c06\u662f -76\u3002 \u5982\u679c\u8be5\u503c\u4ee5\u5176\u4ed6\u65b9\u5f0f\u7f16\u7801\uff0c\u5219\u5b83\u53ef\u80fd\u5b8c\u5168\u4e0d\u540c\u3002 \u90a3\u4e48 C++ \u662f\u5982\u4f55\u77e5\u9053\u5c06\u5305\u542b\u4e8c\u8fdb\u5236 1011 0100 \u7684\u53d8\u91cf\u6253\u5370\u4e3a 180 \u8fd8\u662f -76 \u5462\uff1f \u5982\u679c\u7ae0\u8282\u6807\u9898\u6ca1\u6709\u900f\u9732\uff0c\u8fd9\u5c31\u662f\u7c7b\u578b\u53d1\u6325\u4f5c\u7528\u7684\u5730\u65b9\u3002\u53d8\u91cf\u7684\u7c7b\u578b\u51b3\u5b9a\u4e86\u53d8\u91cf\u7684\u503c\u5982\u4f55\u7f16\u7801\u4e3a\u4e8c\u8fdb\u5236\uff0c\u4ee5\u53ca\u5982\u4f55\u89e3\u7801\u56de\u4e00\u4e2a\u503c\u3002\u56e0\u6b64\uff0c \u5982\u679c\u53d8\u91cf\u7c7b\u578b\u662f\u65e0\u7b26\u53f7\u6574\u6570\uff0c\u5b83\u4f1a\u77e5\u9053 1011 0100 \u662f\u6807\u51c6\u4e8c\u8fdb\u5236\uff0c\u5e94\u8be5\u6253\u5370\u4e3a 180\u3002\u5982\u679c\u53d8\u91cf\u662f\u6709\u7b26\u53f7\u6574\u6570\uff0c\u5b83\u4f1a\u77e5\u9053 1011 0100 \u662f\u4f7f\u7528\u4e8c\u8fdb\u5236\u8865\u7801\u7f16\u7801\u7684\uff08\u73b0\u5728\u4fdd\u8bc1\u4e3aC++20 \u7684\uff09\uff0c\u5e76\u4e14\u5e94\u8be5\u6253\u5370\u4e3a -76 \u3002","title":"\u4e3a\u4ec0\u4e48\u7c7b\u578b\u5f88\u91cd\u8981"},{"location":"O-4-converting-between-binary-and-decimal/#_8","text":"\u6d6e\u70b9\u6570\u5982\u4f55\u4ece\u4e8c\u8fdb\u5236\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u8981\u590d\u6742\u5f97\u591a\uff0c\u800c\u4e14\u60a8\u53ef\u80fd\u6c38\u8fdc\u90fd\u4e0d\u9700\u8981\u77e5\u9053\u3002\u4f46\u662f\uff0c\u5982\u679c\u60a8\u5f88\u597d\u5947\uff0c\u8bf7\u8bbf\u95ee \u6b64\u7ad9\u70b9 \uff0c\u8be5\u7ad9\u70b9\u5f88\u597d\u5730\u8be6\u7ec6\u89e3\u91ca\u4e86\u8be5\u4e3b\u9898\u3002","title":"\u5982\u4f55\u5c06\u6d6e\u70b9\u6570\u4ece\u4e8c\u8fdb\u5236\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\uff1f"},{"location":"Off-by-one/","tags":["\u8bcd\u6c47\u8868","Off-by-one"],"text":"","title":"\u5dee\u4e00\u9519\u8bef"},{"location":"Output-manipulators/","tags":["\u8bcd\u6c47\u8868","Output-manipulators"],"text":"","title":"\u8f93\u51fa\u64cd\u7eb5\u5668"},{"location":"RAII%20%28Resource%20Acquisition%20Is%20Initialization%29/","tags":["\u8bcd\u6c47\u8868","RAII (Resource Acquisition Is Initialization)"],"text":"","title":"\u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09"},{"location":"Run-time-type-information-RTTI/","tags":["\u8bcd\u6c47\u8868","Run-time-type-information-RTTI"],"text":"","title":"\u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f(RTTI)"},{"location":"Sequence-containers/","tags":["\u8bcd\u6c47\u8868","Sequence-containers"],"text":"","title":"\u987a\u5e8f\u5bb9\u5668"},{"location":"Template-specialization/","tags":["\u8bcd\u6c47\u8868","Template-specialization"],"text":"","title":"\u6a21\u677f\u7279\u5316"},{"location":"abbreviated%20function%20templates/","tags":["\u8bcd\u6c47\u8868","abbreviated function templates"],"text":"","title":"\u7f29\u5199\u51fd\u6570\u6a21\u677f"},{"location":"abstract-class/","tags":["\u8bcd\u6c47\u8868","\u62bd\u8c61\u7c7b"],"text":"","title":"\u62bd\u8c61\u7c7b"},{"location":"abstraction/","tags":["\u8bcd\u6c47\u8868","abstraction"],"text":"","title":"\u62bd\u8c61"},{"location":"access-function/","tags":["\u8bcd\u6c47\u8868","access-function"],"text":"","title":"\u6210\u5458\u8bbf\u95ee\u51fd\u6570"},{"location":"access-specifiers/","tags":["\u8bcd\u6c47\u8868","access-specifiers"],"text":"","title":"\u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26"},{"location":"address-of-operator/","tags":["\u8bcd\u6c47\u8868","address-of-operator"],"text":"","title":"\u53d6\u5730\u5740\u64cd\u4f5c\u7b26 &"},{"location":"aggregate-data-type/","tags":["\u8bcd\u6c47\u8868","aggregate-data-type"],"text":"\u79f0\u4e3a aggregates\uff08\u805a\u5408\u7269\uff09 \u805a\u5408\u7c7b\u578b\u5305\u62ec\uff1a - array type - class type (typically, struct or union), that has - no private or protected direct (since C++17)non-static data members - no user-declared constructors(until C++11) - no user-provided , inherited , or explicit constructors (since C++11) (until C++20) - no user-declared or inherited constructors(since C++20) - no virtual, private, or protected (since C++17) base classes - no virtual member functions - no default member initializers (since C++11) (until C++14) \u805a\u5408\u7c7b\u578b\u4e2d\u5143\u7d20\u7684\u7279\u70b9 - for an array, the array elements in increasing subscript order, or - for a class, the non-static data members that are not anonymous bit-fields , in declaration order.(until C++17) - for a class, the direct base classes in declaration order, followed by the direct non-static data members that are neither anonymous bit-fields nor members of an anonymous union , in declaration order.(since C++17) C++ \u4e2d\u7684\u805a\u5408\u6570\u636e\u7c7b\u578b\u5fc5\u987b\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\uff1a \u662f\u4e00\u4e2a \u7c7b\u7c7b\u578b (\u5305\u62ec struct, class \u6216 union) \u6216\u6570\u7ec4 (\u5305\u62ec\u5185\u7f6e\u6570\u7ec4\u6216 std::array )\uff1b \u6ca1\u6709\u79c1\u6709\u6216\u53d7\u4fdd\u62a4\u7684\u975e\u9759\u6001\u6570\u636e\u6210\u5458\uff1b \u6ca1\u6709\u7528\u6237\u58f0\u660e\u7684\u6216\u7ee7\u627f\u7684 \u6784\u9020\u51fd\u6570 \uff1b \u6ca1\u6709 \u57fa\u7c7b \uff1b \u6ca1\u6709 \u865a\u51fd\u6570 \uff1b","title":"\u805a\u5408\u6570\u636e\u7c7b\u578b"},{"location":"aggregate-initialization/","tags":["\u8bcd\u6c47\u8868","aggregate-initialization"],"text":"\u805a\u5408\u521d\u59cb\u5316\u4f7f\u7528\u5927\u62ec\u53f7\uff0c\u4e3b\u8981\u7528\u4e8e\u521d\u59cb\u5316\u7ed3\u6784\u4f53\u7684\uff0c\u5982\u679c\u662f\u7c7b\u7684\u8bdd\u5219\u5fc5\u987b\u90fd\u662fpublic\u6210\u5458\uff08\u4e0d\u592a\u5e38\u7528\uff09\uff1a 1 2 3 4 5 struct Data { std :: string name ; double value ; }; Data x { \"test1\" , 6.778 }; \u4eceC++17\u5f00\u59cb\uff0c\u805a\u5408\u8fd8\u53ef\u4ee5\u6709\u57fa\u7c7b\uff0c\u56e0\u6b64\u53ef\u4ee5\u521d\u59cb\u5316\u4ece\u5176\u4ed6\u7c7b/\u7ed3\u6784\u4f53\u6d3e\u751f\u800c\u6765\u7684\u7ed3\u6784\u4f53\uff1a 1 2 3 4 struct MoreData : Data { bool done ; }; MoreData y {{ \"test1\" , 6.778 }, false }; https://toutiao.io/posts/tt34ysp/preview https://en.cppreference.com/w/cpp/language/aggregate_initialization \u805a\u5408\u521d\u59cb\u5316\u6709\u4e09\u79cd\u5f62\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee frank = { 1 , 32 , 60000.0 }; // \u62f7\u8d1d\u5217\u8868\u521d\u59cb\u5316\uff0c\u4f7f\u7528\u5927\u62ec\u53f7 Employee robert ( 3 , 45 , 62500.0 ); // \u4f7f\u7528\u5c0f\u62ec\u53f7\u7684\u76f4\u63a5\u521d\u59cb\u5316(C++20) Employee joe { 2 , 28 , 45000.0 }; // \u4f7f\u7528\u5927\u62ec\u53f7\u5217\u8868\u7684\u5217\u8868\u521d\u59cb\u5316\uff08\u63a8\u8350\uff09 return 0 ; }","title":"\u805a\u5408\u521d\u59cb\u5316"},{"location":"aggregation/","tags":["\u8bcd\u6c47\u8868","aggregation"],"text":"","title":"\u805a\u5408"},{"location":"ambiguous-match/","tags":["\u8bcd\u6c47\u8868","ambiguous-match"],"text":"","title":"\u4e0d\u660e\u786e\u5339\u914d"},{"location":"anonymous-object/","tags":["\u8bcd\u6c47\u8868","anonymous-object"],"text":"","title":"\u533f\u540d\u5bf9\u8c61"},{"location":"arguments/","tags":["\u8bcd\u6c47\u8868","arguments"],"text":"\u8c03\u7528\u51fd\u6570\u65f6\u4f7f\u7528\u7684\u53c2\u6570\u3002 \u5b9e\u53c2\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u5f0f\u5411 \u5f62\u53c2 \u4f20\u9012\uff1a 1. \u6309\u503c\u4f20\u9012 \u65f6\uff0c\u5f62\u53c2\u5f97\u5230\u4e00\u4efd\u5b9e\u53c2\u7684\u62f7\u8d1d 2. \u6309\u5f15\u7528\u4f20\u9012 \u65f6\uff0c\u5f62\u53c2\uff08\u58f0\u660e\u4e3a\u5f15\u7528\u7c7b\u578b\uff09\u88ab\u7ed1\u5b9a\u5230\u5b9e\u53c2\uff0c\u4ece\u800c\u53ef\u4ee5\u4fee\u6539\u5b9e\u53c2 3. \u6309\u5730\u5740\u4f20\u9012 \u662f\uff0c\u5f62\u53c2\uff08\u58f0\u660e\u4e3a\u6307\u9488\u7c7b\u578b\uff09\u83b7\u5f97\u5f62\u53c2\u6301\u6709\u5730\u5740\u7684\u62f7\u8d1d\uff0c\u548c\u5b9e\u53c2\u6307\u5411\u540c\u4e00\u4e2a\u5730\u5740\uff0c\u53ef\u4ee5\u4fee\u6539\u8be5\u5730\u5740\u5b58\u653e\u7684\u503c\u3002\u4f46\u662f\u5730\u5740\u672c\u8eab\u662f\u5b9e\u53c2\u7684\u62f7\u8d1d\uff0c\u6240\u4ee5\u4fee\u6539\u5f62\u53c2\u7684\u503c\uff08\u5730\u5740\uff09\u4e0d\u4f1a\u5f71\u54cd\u5230\u5b9e\u53c2\uff0c\u5b9e\u53c2\u4ecd\u7136\u6301\u6709\u539f\u5730\u5740\u3002","title":"\u5b9e\u53c2"},{"location":"arity/","tags":["\u8bcd\u6c47\u8868","arity"],"text":"","title":"\u5143\u6570"},{"location":"automatic-memory-allocation/","tags":["\u8bcd\u6c47\u8868","automatic-memory-allocation"],"text":"","title":"\u81ea\u52a8\u5185\u5b58\u5206\u914d"},{"location":"automatic-storage-duration/","tags":["\u8bcd\u6c47\u8868","automatic-storage-duration"],"text":"","title":"\u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4"},{"location":"base-class/","tags":["\u8bcd\u6c47\u8868","base-class"],"text":"","title":"\u57fa\u7c7b"},{"location":"bit/","tags":["\u8bcd\u6c47\u8868","bit"],"text":"","title":"\u4f4d"},{"location":"bitwise-and/","tags":["\u8bcd\u6c47\u8868","bitwise-and"],"text":"","title":null},{"location":"bitwise-or/","tags":["\u8bcd\u6c47\u8868","bitwise-or"],"text":"","title":"\u6309\u4f4d\u6216"},{"location":"bss%20segment/","tags":["\u8bcd\u6c47\u8868","bss segment"],"text":"","title":"bss\u6bb5"},{"location":"byte/","tags":["\u8bcd\u6c47\u8868","byte"],"text":"","title":"\u5b57\u8282"},{"location":"call-stack/","tags":["\u8bcd\u6c47\u8868","call-stack"],"text":"","title":"\u8c03\u7528\u6808"},{"location":"callable-object/","tags":["\u8bcd\u6c47\u8868","callable-object"],"text":"","title":"\u53ef\u8c03\u7528\u5bf9\u8c61"},{"location":"callback%20functions/","tags":["\u8bcd\u6c47\u8868","callback functions"],"text":"","title":"\u56de\u8c03\u51fd\u6570"},{"location":"child-class/","tags":["\u8bcd\u6c47\u8868","child-class"],"text":"","title":"\u5b50\u7c7b"},{"location":"class-instance/","tags":["\u8bcd\u6c47\u8868","\u7c7b\u5b9e\u4f8b"],"text":"","title":"\u7c7b\u5b9e\u4f8b"},{"location":"class-template-argument-deduction/","tags":["\u8bcd\u6c47\u8868","class-template-argument-deduction"],"text":"","title":"\u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad"},{"location":"class-template/","tags":["\u8bcd\u6c47\u8868","class-template"],"text":"","title":"\u7c7b\u6a21\u677f"},{"location":"class-type/","tags":["\u8bcd\u6c47\u8868","class-type"],"text":"\u5305\u62ec\u7ed3\u6784\u4f53\u3001\u7c7b\u548c\u8054\u5408\u4f53","title":"\u7c7b\u7c7b\u578b"},{"location":"closure/","tags":["\u8bcd\u6c47\u8868","closure"],"text":"","title":"\u95ed\u5305"},{"location":"code%20segment/","tags":["\u8bcd\u6c47\u8868","code segment"],"text":"","title":"\u4ee3\u7801\u6bb5"},{"location":"compile-time/","tags":["glossary","compile-time"],"text":"","title":"\u7f16\u8bd1\u65f6"},{"location":"compiler/","tags":["\u8bcd\u6c47\u8868","compiler"],"text":"","title":"\u7f16\u8bd1\u5668"},{"location":"composite-types/","tags":["\u8bcd\u6c47\u8868","composite-types"],"text":"","title":"\u7ec4\u5408\u7c7b\u578b"},{"location":"const-cast/","tags":["\u8bcd\u6c47\u8868","const-cast"],"text":"","title":"const \u7c7b\u578b\u8f6c\u6362"},{"location":"const-member-function/","tags":["\u8bcd\u6c47\u8868","const-member-function"],"text":"","title":"const \u6210\u5458\u51fd\u6570"},{"location":"const-pointer/","tags":["\u8bcd\u6c47\u8868","const-pointer"],"text":"","title":"\u5e38\u91cf\u6307\u9488"},{"location":"constant%20expression/","tags":["\u8bcd\u6c47\u8868","constant expression"],"text":"","title":"\u5e38\u91cf\u8868\u8fbe\u5f0f"},{"location":"constructor/","tags":["\u8bcd\u6c47\u8868","\u6784\u9020\u51fd\u6570"],"text":"","title":"\u6784\u9020\u51fd\u6570"},{"location":"container-adapter/","tags":["\u8bcd\u6c47\u8868","container-adapter"],"text":"","title":"\u5bb9\u5668\u9002\u914d\u5668"},{"location":"container-class/","tags":["\u8bcd\u6c47\u8868","container-class"],"text":"","title":"\u5bb9\u5668\u7c7b"},{"location":"converting-constructors/","tags":["\u8bcd\u6c47\u8868","converting-constructors"],"text":"","title":"\u8f6c\u6362\u6784\u9020\u51fd\u6570"},{"location":"copy-assignment-operator/","tags":["\u8bcd\u6c47\u8868","\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26"],"text":"","title":"\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26"},{"location":"copy-constructor/","tags":["\u8bcd\u6c47\u8868","copy-constructor"],"text":"","title":"\u62f7\u8d1d\u6784\u9020\u51fd\u6570"},{"location":"copy-constructors/","tags":["\u8bcd\u6c47\u8868","copy-constructors"],"text":"","title":"\u62f7\u8d1d\u6784\u9020\u51fd\u6570"},{"location":"copy-initialization/","tags":["\u8bcd\u6c47\u8868","copy-initialization","initialization"],"text":"","title":"\u62f7\u8d1d\u521d\u59cb\u5316"},{"location":"copy-semantics/","tags":["\u8bcd\u6c47\u8868","copy-semantics"],"text":"","title":"\u62f7\u8d1d\u8bed\u4e49"},{"location":"copy-swap%E4%B9%A0%E8%AF%AD/","text":"","title":"Copy swap\u4e60\u8bed"},{"location":"covariant/","tags":["\u8bcd\u6c47\u8868","covariant"],"text":"\u5982\u679c\u865a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f\u6307\u5411\u76f8\u540c\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u5176\u91cd\u5199\u51fd\u6570\u4e5f\u53ef\u4ee5\u8fd4\u56de\u6307\u5411\u6d3e\u751f\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528\u3002\u8fd9\u4e2a\u673a\u5236\u79f0\u4e3a\u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b","title":"\u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b"},{"location":"dangling/","tags":["\u8bcd\u6c47\u8868","dangling"],"text":"\u6307\u5411\u4e0d\u5b58\u5728\u5bf9\u8c61\u7684\u6307\u9488\u6216\u5f15\u7528","title":"\u60ac\u5782"},{"location":"data%20segment/","tags":["\u8bcd\u6c47\u8868","data segment"],"text":"","title":"\u6570\u636e\u6bb5"},{"location":"declaration/","tags":["glossary","declaration"],"text":"","title":"\u58f0\u660e"},{"location":"deep-copy/","tags":["\u8bcd\u6c47\u8868","deep-copy"],"text":"","title":"\u6df1\u62f7\u8d1d"},{"location":"default-constructor/","tags":["\u8bcd\u6c47\u8868","default-constructor"],"text":"","title":"\u9ed8\u8ba4\u6784\u9020\u51fd\u6570"},{"location":"default-initialization/","tags":["\u8bcd\u6c47\u8868","default-initialization"],"text":"","title":"\u9ed8\u8ba4\u521d\u59cb\u5316"},{"location":"default-member-initializer/","tags":["\u8bcd\u6c47\u8868","default-member-initializer"],"text":"","title":"\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c"},{"location":"definition/","tags":["glossary","definition"],"text":"","title":"\u5b9a\u4e49"},{"location":"delegating-constructors/","tags":["\u8bcd\u6c47\u8868","delegating-constructors"],"text":"","title":"\u59d4\u6258\u6784\u9020\u51fd\u6570"},{"location":"dependency/","tags":["\u8bcd\u6c47\u8868","dependency"],"text":"","title":"\u4f9d\u8d56"},{"location":"dereference-operator/","tags":["\u8bcd\u6c47\u8868","dereference-operator"],"text":"","title":"\u89e3\u5f15\u7528\u8fd0\u7b97\u7b26"},{"location":"derived-class/","tags":["\u8bcd\u6c47\u8868","derived-class"],"text":"","title":"\u6d3e\u751f\u7c7b"},{"location":"designated-initializers/","tags":["\u8bcd\u6c47\u8868","designated-initializers"],"text":"","title":"\u6307\u5b9a\u521d\u59cb\u5316"},{"location":"destructor/","tags":["\u8bcd\u6c47\u8868","destructor"],"text":"","title":"\u6790\u6784\u51fd\u6570"},{"location":"direct-initialization/","tags":["\u8bcd\u6c47\u8868","direct-initialization","initialization"],"text":"\u521d\u59cb\u5316 \u4f7f\u7528\u5706\u62ec\u53f7\u7684\u521d\u59cb\u5316","title":"\u76f4\u63a5\u521d\u59cb\u5316"},{"location":"downcasting/","tags":["\u8bcd\u6c47\u8868","downcasting"],"text":"\u5c06\u57fa\u7c7b\u6307\u9488\u8f6c\u6362\u4e3a\u6d3e\u751f\u7c7b\u6307\u9488","title":"\u5411\u4e0b\u8f6c\u6362"},{"location":"dumb-pointer/","tags":["\u8bcd\u6c47\u8868","dumb-pointer"],"text":"","title":"\u7b28\u6307\u9488"},{"location":"dynamic-array/","tags":["\u8bcd\u6c47\u8868","\u52a8\u6001\u6570\u7ec4"],"text":"","title":"\u52a8\u6001\u6570\u7ec4"},{"location":"dynamic-casts/","tags":["\u8bcd\u6c47\u8868","dynamic-casts"],"text":"","title":"\u52a8\u6001\u7c7b\u578b\u8f6c\u6362"},{"location":"dynamic-duration/","tags":["\u8bcd\u6c47\u8868","dynamic duration"],"text":"","title":"\u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4"},{"location":"dynamic-initialization/","tags":["\u8bcd\u6c47\u8868","dynamic-initialization"],"text":"","title":"\u52a8\u6001\u521d\u59cb\u5316"},{"location":"dynamic-memory-allocation/","tags":["\u8bcd\u6c47\u8868","dynamic-memory-allocation"],"text":"","title":"\u52a8\u6001\u5185\u5b58\u5206\u914d"},{"location":"enum-class/","tags":["\u8bcd\u6c47\u8868","enum-class"],"text":"\u679a\u4e3e\u7c7b\u4e0d\u662f\u7c7b\uff0c\u5b83\u662fenum class\u521b\u5efa\u7684 \u6709\u4f5c\u7528\u57df\u679a\u4e3e","title":"\u679a\u4e3e\u7c7b"},{"location":"evaluation/","tags":["\u8bcd\u6c47\u8868","evaluation"],"text":"","title":"\u6c42\u503c"},{"location":"exception-safety-issue/","tags":["\u8bcd\u6c47\u8868","exception-safety-issue"],"text":"","title":"\u5f02\u5e38\u5b89\u5168\u95ee\u9898"},{"location":"explicit-type-conversion/","tags":["\u8bcd\u6c47\u8868","explicit-type-conversion"],"text":"","title":"\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362"},{"location":"expression-type/","tags":["\u8bcd\u6c47\u8868","expression type"],"text":"","title":"\u8868\u8fbe\u5f0f\u7c7b\u578b"},{"location":"external-linkage/","tags":["\u8bcd\u6c47\u8868","external-linkage"],"text":"","title":"\u5916\u90e8\u94fe\u63a5"},{"location":"external-variable/","tags":["\u8bcd\u6c47\u8868","external-variable"],"text":"","title":"\u5916\u90e8\u53d8\u91cf"},{"location":"extraction-operator/","tags":["\u8bcd\u6c47\u8868","extraction-operator"],"text":"","title":"\u63d0\u53d6\u8fd0\u7b97\u7b26"},{"location":"fallthrough/","tags":["\u8bcd\u6c47\u8868","fallthrough"],"text":"","title":"\u8d2f\u7a7f\u5c5e\u6027"},{"location":"file-scope/","tags":["\u8bcd\u6c47\u8868","file-scope"],"text":"","title":"\u6587\u4ef6\u4f5c\u7528\u57df"},{"location":"fixed-array/","tags":["\u8bcd\u6c47\u8868","fixed-array"],"text":"","title":"\u56fa\u5b9a\u6570\u7ec4"},{"location":"format-group/","tags":["\u8bcd\u6c47\u8868","format-group"],"text":"","title":"\u683c\u5f0f\u7ec4"},{"location":"forward-declaration/","tags":["glossary","forward declaration"],"text":"","title":"\u524d\u5411\u58f0\u660e"},{"location":"friend-class/","tags":["\u8bcd\u6c47\u8868","friend-class"],"text":"","title":"\u53cb\u5143\u7c7b"},{"location":"friend-function/","tags":["\u8bcd\u6c47\u8868","friend-function"],"text":"","title":"\u53cb\u5143\u51fd\u6570"},{"location":"function-instance/","tags":["\u8bcd\u6c47\u8868","function-instance"],"text":"","title":"\u51fd\u6570\u5b9e\u4f8b"},{"location":"function-literal/","tags":["\u8bcd\u6c47\u8868","function-literal"],"text":"","title":"\u51fd\u6570\u5b57\u9762\u91cf"},{"location":"function-pointer/","tags":["\u8bcd\u6c47\u8868","function-pointer"],"text":"","title":"\u51fd\u6570\u6307\u9488"},{"location":"function-prototype/","tags":["\u8bcd\u6c47\u8868","\u51fd\u6570\u539f\u578b(function prototype)"],"text":"","title":"\u51fd\u6570\u539f\u578b"},{"location":"function-template-instantiation/","tags":["\u8bcd\u6c47\u8868","function-template-instantiation"],"text":"","title":"\u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316"},{"location":"function-template/","tags":["\u8bcd\u6c47\u8868","function-template"],"text":"\u7f16\u8bd1\u5668\u4f1a\u6839\u636e\u6240\u9700\u7684\u7c7b\u578b\u81ea\u52a8\u521b\u5efa\u5bf9\u5e94\u7684\u51fd\u6570","title":"\u51fd\u6570\u6a21\u677f"},{"location":"generic-algorithm/","tags":["\u8bcd\u6c47\u8868","generic-algorithm"],"text":"","title":"\u6cdb\u578b\u7b97\u6cd5"},{"location":"global-variable/","tags":["glossary","global variable"],"text":"","title":"\u5168\u5c40\u53d8\u91cf"},{"location":"going-out-of-scope/","tags":["\u8bcd\u6c47\u8868","\u79bb\u5f00\u4f5c\u7528\u57df"],"text":"","title":"\u79bb\u5f00\u4f5c\u7528\u57df"},{"location":"header-guard/","text":"title: \u5934\u6587\u4ef6\u9632\u8303 alias: \u5934\u6587\u4ef6\u9632\u8303 english:header-guard type: glossary tags: - glossary - header-guard \u4e5f\u79f0\u4e3a include \u9632\u8303 \u6216 \u5934\u6587\u4ef6\u9632\u5fa1\u5f0f\u58f0\u660e","title":"Header guard"},{"location":"heap/","tags":["\u8bcd\u6c47\u8868","heap"],"text":"","title":"\u5806"},{"location":"identifier/","tags":["glossary","identifier"],"text":"","title":"\u6807\u8bc6\u7b26(identifier)"},{"location":"identity/","tags":["\u8bcd\u6c47\u8868","identity"],"text":"","title":"\u8eab\u4efd\u7279\u5f81"},{"location":"immediate-functions/","tags":["\u8bcd\u6c47\u8868","immediate-functions"],"text":"","title":"\u5373\u65f6\u51fd\u6570"},{"location":"implicit-constructor/","tags":["\u8bcd\u6c47\u8868","implicit-constructor"],"text":"","title":"\u9690\u5f0f\u6784\u9020\u51fd\u6570"},{"location":"implicit-object/","tags":["\u8bcd\u6c47\u8868","implicit-object"],"text":"","title":"\u9690\u5f0f\u5bf9\u8c61"},{"location":"implicit-type-conversion/","tags":["\u8bcd\u6c47\u8868","implicit-type-conversion"],"text":"","title":"\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362"},{"location":"inheritance/","tags":["\u8bcd\u6c47\u8868","inheritance"],"text":"","title":"\u7ee7\u627f"},{"location":"initialization/","tags":["\u8bcd\u6c47\u8868","\u521d\u59cb\u5316"],"text":"C++ \u652f\u6301\u4e09\u79cd\u7c7b\u578b\u7684\u521d\u59cb\u5316\uff1a \u62f7\u8d1d\u521d\u59cb\u5316 \u3001 \u76f4\u63a5\u521d\u59cb\u5316 \u548c \u7edf\u4e00\u521d\u59cb\u5316 \uff08\u4e5f\u79f0\u4e3a \u5217\u8868\u521d\u59cb\u5316 \u6216 \u62ec\u53f7\u521d\u59cb\u5316 \uff09\u3002 \u805a\u5408\u521d\u59cb\u5316 \uff0c\u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u521d\u59cb\u5316\u4e00\u4e2a \u805a\u5408\u6570\u636e\u7c7b\u578b \uff0c\u662f \u5217\u8868\u521d\u59cb\u5316 \u7684\u4e00\u79cd\u5f62\u5f0f \u521d\u59cb\u5316\u503c\u5217\u8868 16.7 - std::initializer_list 0\u521d\u59cb\u5316 \u503c\u521d\u59cb\u5316 initializer \u6307\u5b9a\u521d\u59cb\u5316 \u9ed8\u8ba4\u521d\u59cb\u5316 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09","title":"\u521d\u59cb\u5316"},{"location":"initializer-list/","tags":["\u8bcd\u6c47\u8868","initializer-list"],"text":"\u7528\u4e8e \u805a\u5408\u521d\u59cb\u5316 \u666e\u901a\u7c7b\u4e0d\u9700\u8981\u521d\u59cb\u503c\u5217\u8868\uff0c\u5bb9\u5668\u7c7b\u9700\u8981\u3002\u5b9e\u73b0\u65f6\u9700\u8981\u7279\u6b8a\u7684\u6784\u9020\u51fd\u6570\u652f\u6301\u3002 \u6240\u4ee5\u521d\u59cb\u5316\u503c\u5217\u8868\u662f\u5bf9\u5bb9\u5668\u7c7b\u6765\u8bf4\u7684\u3002\u552f\u4e00\u7279\u4f8b\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e2d\u7684\u6570\u7ec4\uff0c\u56e0\u4e3a\u5b83\u4e5f\u662f\u5bb9\u5668 \u4e00\u822c\u63d0\u4f9b\u521d\u59cb\u5316\u503c\u5217\u8868\u6784\u9020\u51fd\u6570\u65f6\u4e5f\u8981\u63d0\u4f9b\u5bf9\u5e94\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u91cd\u8f7d \u5bf9\u4e8e\u4e00\u822c\u7c7b\u6765\u8bf4\u53ea\u6709 \u5217\u8868\u521d\u59cb\u5316 \u3002\u4f46\u662f\u6709 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u7684\u6982\u5ff5\uff0c\u662f\u6784\u9020\u51fd\u6570\u540e\u9762\u7684\u5217\u8868\u3002 1 2 3 4 5 6 7 //\u90fd\u662f\u5229\u7528\u805a\u5408\u521d\u59cb\u5316 std :: array < int , 5 > myArray = { 9 , 7 , 5 , 3 , 1 }; // \u521d\u59cb\u5316\u503c\u5217\u8868 std :: array < int , 5 > myArray2 { 9 , 7 , 5 , 3 , 1 }; // \u662f\u5217\u8868\u521d\u59cb\u5316\u5417\uff1f\u5e94\u8be5\u4e5f\u662f\u521d\u59cb\u5316\u503c\u5217\u8868\u8bed\u6cd5\uff0c\u8c03\u7528\u5bf9\u5e94\u7684\u6784\u9020\u51fd\u6570 std :: array < int , 3 > a1 { { 1 , 2 , 3 } }; // \u5728CPP14\u4e4b\u524d\u9700\u8981\u4f7f\u7528\u8fd9\u79cd\u5f62\u5f0f int prime [ 5 ]{ 2 , 3 , 5 , 7 , 11 }; int prime [ 5 ] = { 2 , 3 , 5 , 7 , 11 }; \u5bf9\u4e8e\u5bb9\u5668\u7c7b\uff0c\u4e0d\u5e94\u8be5\u4f7f\u7528\u4e00\u822c\u7684\u5217\u8868\u521d\u59cb\u5316\uff1f\u662f\u5426\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u521d\u59cb\u5316\u76f4\u63a5\u521d\u59cb\u5316\u5bb9\u5668\u7c7b\u4e2d\u7684\u6570\u636e\u6210\u5458\uff1f\u90a3\u6837\u4e5f\u5e94\u8be5\u4f7f\u7528\u53cc\u5c42\u62ec\u53f7\u5427\uff1f","title":"\u521d\u59cb\u5316\u503c\u5217\u8868"},{"location":"initializer/","tags":["\u8bcd\u6c47\u8868","\u521d\u59cb\u5316\u503c(initializer)"],"text":"","title":"\u521d\u59cb\u5316\u503c"},{"location":"inline-function/","tags":["\u8bcd\u6c47\u8868","inline-function"],"text":"","title":"\u5185\u8054\u51fd\u6570"},{"location":"inline-namespace/","tags":["\u8bcd\u6c47\u8868","inline-namespace"],"text":"","title":"\u5185\u8054\u547d\u540d\u7a7a\u95f4"},{"location":"inline-variables/","tags":["\u8bcd\u6c47\u8868","inline-variables"],"text":"","title":"\u5185\u8054\u53d8\u91cf"},{"location":"input-manipulators/","tags":["\u8bcd\u6c47\u8868","input-manipulators"],"text":"","title":"\u8f93\u5165\u64cd\u7eb5\u5668"},{"location":"insertion-operator/","tags":["\u8bcd\u6c47\u8868","insertion-operator"],"text":"","title":"\u63d2\u5165\u8fd0\u7b97\u7b26"},{"location":"instance/","tags":["\u8bcd\u6c47\u8868","instance"],"text":"","title":"\u5b9e\u4f8b"},{"location":"instantiated/","tags":["glossary","instantiated"],"text":"","title":"\u5b9e\u4f8b\u5316"},{"location":"instuction/","tags":["glossary","instuction"],"text":"","title":"\u6307\u4ee4"},{"location":"integral-type/","tags":["\u8bcd\u6c47\u8868","integral-type"],"text":"","title":"\u6574\u578b\u7c7b\u578b"},{"location":"interface-class/","tags":["\u8bcd\u6c47\u8868","interface-class"],"text":"","title":"\u63a5\u53e3\u7c7b"},{"location":"internal-linkage/","tags":["\u8bcd\u6c47\u8868","internal-linkage"],"text":"","title":"\u5185\u90e8\u94fe\u63a5"},{"location":"internal-variable/","tags":["\u8bcd\u6c47\u8868","internal-variable"],"text":"","title":"\u5185\u90e8\u53d8\u91cf"},{"location":"iterating/","tags":["\u8bcd\u6c47\u8868","iterating"],"text":"","title":"\u904d\u5386"},{"location":"iterator/","tags":["\u8bcd\u6c47\u8868","iterator"],"text":"","title":"\u8fed\u4ee3\u5668"},{"location":"lambda/","tags":["\u8bcd\u6c47\u8868","lambda"],"text":"","title":"\u533f\u540d\u51fd\u6570"},{"location":"link/","tags":["\u8bcd\u6c47\u8868","link"],"text":"","title":"\u94fe\u63a5"},{"location":"linkage/","tags":["\u8bcd\u6c47\u8868","linkage"],"text":"","title":"\u94fe\u63a5\u5c5e\u6027"},{"location":"linker/","tags":["glossary","linker"],"text":"","title":"\u94fe\u63a5\u5668(linker)"},{"location":"list%20assignment%20operator/","tags":["\u8bcd\u6c47\u8868","list assignment operator"],"text":"","title":"\u7c7b\u522b\u8d4b\u503c\u8fd0\u7b97\u7b26"},{"location":"list-constructor/","tags":["\u8bcd\u6c47\u8868","list-constructor"],"text":"","title":"\u5217\u8868\u6784\u9020\u51fd\u6570"},{"location":"list-initialization/","tags":["\u8bcd\u6c47\u8868","list-initialization","initialization"],"text":"\u548c \u7edf\u4e00\u521d\u59cb\u5316 \u3001 \u62ec\u53f7\u521d\u59cb\u5316 \u662f\u4e00\u56de\u4e8b\uff0c\u548c \u521d\u59cb\u5316\u503c\u5217\u8868 \u6709\u4ec0\u4e48\u533a\u522b 1 2 std :: array < int , 5 > myArray = { 9 , 7 , 5 , 3 , 1 }; // \u521d\u59cb\u5316\u503c\u5217\u8868 std :: array < int , 5 > myArray2 { 9 , 7 , 5 , 3 , 1 }; // \u5217\u8868\u521d\u59cb\u5316 https://en.cppreference.com/w/cpp/language/list_initialization","title":"\u5217\u8868\u521d\u59cb\u5316"},{"location":"literals/","tags":["\u8bcd\u6c47\u8868","literals"],"text":"","title":"\u5b57\u9762\u91cf"},{"location":"local-variable/","text":"title: \u5c40\u90e8\u53d8\u91cf alias: \u5c40\u90e8\u53d8\u91cf english:local-variable type: glossary tags: - glossary - local-variable","title":"Local variable"},{"location":"lvalue-reference/","tags":["\u8bcd\u6c47\u8868","lvalue-reference"],"text":"","title":"\u5de6\u503c\u5f15\u7528"},{"location":"lvalue/","tags":["\u8bcd\u6c47\u8868","lvalue"],"text":"","title":"\u5de6\u503c"},{"location":"macro/","tags":["glossary","macro"],"text":"","title":"\u5b8f"},{"location":"member-access-operator/","tags":["\u8bcd\u6c47\u8868","member access operator"],"text":"\u5bf9\u4e8e\u5bf9\u8c61\u662f . \uff0c\u5bf9\u4e8e\u6307\u9488\u662f ->","title":"\u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26"},{"location":"member-function/","tags":["\u8bcd\u6c47\u8868","member-function"],"text":"","title":"\u6210\u5458\u51fd\u6570"},{"location":"member-initializer-list/","tags":["\u8bcd\u6c47\u8868","member-initializer-list"],"text":"1 2 3 4 Something () : m_value1 { 1 }, m_value2 { 2.2 }, m_value3 { 'c' } // Initialize our member variables { // No need for assignment here }","title":"\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868"},{"location":"member-selection-operator/","tags":["\u8bcd\u6c47\u8868","member-selection-operator"],"text":"","title":"\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26"},{"location":"member-types/","tags":["\u8bcd\u6c47\u8868","member-types"],"text":"\u5d4c\u5957\u5728\u7c7b\u4e2d\u7684\u7c7b\u578b","title":"\u6210\u5458\u7c7b\u578b"},{"location":"member-variable/","tags":["\u8bcd\u6c47\u8868","member-variable"],"text":"","title":"\u6210\u5458\u53d8\u91cf"},{"location":"memberwise%20initialization/","tags":["\u8bcd\u6c47\u8868","memberwise initialization"],"text":"","title":"\u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316"},{"location":"memberwise-assignment/","tags":["\u8bcd\u6c47\u8868","memberwise-assignment"],"text":"","title":"\u6210\u5458\u4f9d\u6b21\u8d4b\u503c"},{"location":"memberwise-copy/","tags":["\u8bcd\u6c47\u8868","memberwise-copy"],"text":"","title":"\u6210\u5458\u4f9d\u6b21\u62f7\u8d1d"},{"location":"memory-leak/","tags":["\u8bcd\u6c47\u8868","memory-leak"],"text":"","title":"\u5185\u5b58\u6cc4\u6f0f"},{"location":"mixin/","tags":["\u8bcd\u6c47\u8868","mixin"],"text":"","title":null},{"location":"move-assignment-operator/","tags":["\u8bcd\u6c47\u8868","move assignment operator"],"text":"","title":"\u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26"},{"location":"move-constructor/","tags":["\u8bcd\u6c47\u8868","move-constructor"],"text":"","title":"\u79fb\u52a8\u6784\u9020\u51fd\u6570"},{"location":"move-semantics/","tags":["\u8bcd\u6c47\u8868","move-semantics"],"text":"","title":"\u79fb\u52a8\u8bed\u4e49"},{"location":"named-cast/","tags":["\u8bcd\u6c47\u8868","named-cast"],"text":"","title":"\u5177\u540d\u7c7b\u578b\u8f6c\u6362"},{"location":"namespace/","tags":["\u8bcd\u6c47\u8868","namespace"],"text":"","title":"\u547d\u540d\u7a7a\u95f4"},{"location":"narrowing-convertions/","tags":["\u8bcd\u6c47\u8868","narrowing-convertions"],"text":"","title":"\u7f29\u7a84\u8f6c\u6362"},{"location":"non-static-member-initialization/","tags":["\u8bcd\u6c47\u8868","\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316"],"text":"\u5728\u5b9a\u4e49\u7ed3\u6784\u4f53\uff08\u6216\u7c7b\uff09\u7c7b\u578b\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u6210\u5458\u90fd\u63d0\u4f9b\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\u4f5c\u4e3a\u5176\u5b9a\u4e49\u7684\u4e00\u90e8\u5206\u3002\u8be5\u503c\u79f0\u4e3a \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c \u3002","title":"\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316"},{"location":"numeric%20promotions/","tags":["\u8bcd\u6c47\u8868","numeric promotions"],"text":"","title":"\u6570\u503c\u63d0\u5347"},{"location":"numeric-conversions/","tags":["\u8bcd\u6c47\u8868","numeric-conversions"],"text":"","title":"\u6570\u503c\u8f6c\u6362"},{"location":"object-composition/","tags":["\u8bcd\u6c47\u8868","object-composition"],"text":"","title":"\u5bf9\u8c61\u7ec4\u5408"},{"location":"object-like-macros/","tags":["glossary","object-like-macros"],"text":"","title":"\u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f"},{"location":"object-slicing/","tags":["\u8bcd\u6c47\u8868","object-slicing"],"text":"\u5c06\u6d3e\u751f\u7c7b\u5bf9\u8c61\u8d4b\u503c\u7ed9\u57fa\u7c7b\u5bf9\u8c61\u79f0\u4e3a\uff0c\u56e0\u4e3a\u88ab\u8d4b\u503c\u7684\u57fa\u7c7b\u5bf9\u8c61\u53ea\u80fd\u63a5\u6536\u6d3e\u751f\u7c7b\u4e2d\u7684\u57fa\u7c7b\u90e8\u5206\uff0c\u6d3e\u751f\u7c7b\u90e8\u5206\u88ab\u201d\u5207\u6389\u4e86\u201c","title":"\u5bf9\u8c61\u5207\u7247"},{"location":"object/","tags":["\u8bcd\u6c47\u8868","\u5bf9\u8c61(objec)"],"text":"","title":"\u5bf9\u8c61"},{"location":"one-definition-rule/","tags":["glossary","one-definition-rule"],"text":"wiki link \u4e0d\u4f1a\u9650\u5236\u7c7b\u6a21\u677f\u5b9a\u4e49","title":"\u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule)"},{"location":"operands/","tags":["\u8bcd\u6c47\u8868","operands"],"text":"","title":"\u64cd\u4f5c\u6570"},{"location":"operator/","tags":["\u8bcd\u6c47\u8868","operator"],"text":"","title":"\u8fd0\u7b97\u7b26"},{"location":"optional-parameter/","tags":["optional-parameter"],"text":"Key Takeaway","title":"\u53ef\u9009\u53c2\u6570"},{"location":"out-of-scope/","tags":["\u8bcd\u6c47\u8868","\u8d85\u51fa\u4f5c\u7528\u57df(out of scope)"],"text":"","title":"\u8d85\u51fa\u4f5c\u7528\u57df"},{"location":"overload-resolution/","tags":["\u8bcd\u6c47\u8868","overload-resolution"],"text":"","title":"\u91cd\u8f7d\u89e3\u6790"},{"location":"overload/","tags":["\u8bcd\u6c47\u8868","overload"],"text":"","title":"\u91cd\u8f7d"},{"location":"override/","tags":["\u8bcd\u6c47\u8868","override"],"text":"\u533a\u5206\u4e8e \u91cd\u8f7d \uff0c\u91cd\u5199\u662f\u7b7e\u540d\u8981\u76f8\u540c\uff08\u6d3e\u751f\u7c7b\u4e2d\uff09\uff0c\u91cd\u8f7d\u662f\u7b7e\u540d\u4e0d\u540c\uff08\u540c\u7c7b\u4e2d\uff09","title":"\u91cd\u5199"},{"location":"parameters/","tags":["glossary","parameters"],"text":"\u5b9a\u4e49\u51fd\u6570\u65f6\u4f7f\u7528\u7684\u53c2\u6570\u3002 \u5b9e\u53c2 \u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u5f0f\u5411\u5f62\u53c2\u4f20\u9012\uff1a 1. \u6309\u503c\u4f20\u9012 \u65f6\uff0c\u5f62\u53c2\u5f97\u5230\u4e00\u4efd\u5b9e\u53c2\u7684\u62f7\u8d1d 2. \u6309\u5f15\u7528\u4f20\u9012 \u65f6\uff0c\u5f62\u53c2\uff08\u58f0\u660e\u4e3a\u5f15\u7528\u7c7b\u578b\uff09\u88ab\u7ed1\u5b9a\u5230\u5b9e\u53c2\uff0c\u4ece\u800c\u53ef\u4ee5\u4fee\u6539\u5b9e\u53c2 3. \u6309\u5730\u5740\u4f20\u9012 \u662f\uff0c\u5f62\u53c2\uff08\u58f0\u660e\u4e3a\u6307\u9488\u7c7b\u578b\uff09\u83b7\u5f97\u5f62\u53c2\u6301\u6709\u5730\u5740\u7684\u62f7\u8d1d\uff0c\u548c\u5b9e\u53c2\u6307\u5411\u540c\u4e00\u4e2a\u5730\u5740\uff0c\u53ef\u4ee5\u4fee\u6539\u8be5\u5730\u5740\u5b58\u653e\u7684\u503c\u3002\u4f46\u662f\u5730\u5740\u672c\u8eab\u662f\u5b9e\u53c2\u7684\u62f7\u8d1d\uff0c\u6240\u4ee5\u4fee\u6539\u5f62\u53c2\u7684\u503c\uff08\u5730\u5740\uff09\u4e0d\u4f1a\u5f71\u54cd\u5230\u5b9e\u53c2\uff0c\u5b9e\u53c2\u4ecd\u7136\u6301\u6709\u539f\u5730\u5740\u3002","title":"\u5f62\u53c2"},{"location":"parent-class/","tags":["\u8bcd\u6c47\u8868","parent-class"],"text":"","title":"\u7236\u7c7b"},{"location":"pass-by-address/","tags":["\u8bcd\u6c47\u8868","pass-by-address"],"text":"","title":"\u6309\u5730\u5740\u4f20\u9012"},{"location":"pass-by-reference/","tags":["\u8bcd\u6c47\u8868","pass-by-reference"],"text":"","title":"\u6309\u5f15\u7528\u4f20\u9012"},{"location":"pass-by-value/","tags":["glossary","pass-by-value"],"text":"","title":"\u6309\u503c\u4f20\u9012"},{"location":"placeholder-types/","tags":["\u8bcd\u6c47\u8868","placeholder-types"],"text":"","title":"\u5360\u4f4d\u7b26\u7c7b\u578b"},{"location":"pointer-to-const/","tags":["\u8bcd\u6c47\u8868","pointer-to-const"],"text":"","title":"\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488"},{"location":"polymorphism/","tags":["\u8bcd\u6c47\u8868","polymorphism"],"text":"","title":"\u591a\u6001"},{"location":"preprocessor-directive/","tags":["glossary","preprocessor"],"text":"","title":"\u9884\u5904\u7406\u5668\u6307\u4ee4"},{"location":"preprocessor/","tags":["glossary","preprocessor"],"text":"","title":"\u9884\u5904\u7406\u5668"},{"location":"private-inheritance/","tags":["\u8bcd\u6c47\u8868","private-inheritance"],"text":"","title":"\u79c1\u6709\u7ee7\u627f"},{"location":"private-member/","tags":["\u8bcd\u6c47\u8868","private-member"],"text":"","title":"\u79c1\u6709\u6210\u5458"},{"location":"protected-inheritance/","tags":["\u8bcd\u6c47\u8868","protected-inheritance"],"text":"","title":"\u53d7\u4fdd\u62a4\u7ee7\u627f"},{"location":"protected-members/","tags":["\u8bcd\u6c47\u8868","protected-members"],"text":"","title":"\u53d7\u4fdd\u62a4\u6210\u5458"},{"location":"public-inheritance/","tags":["\u8bcd\u6c47\u8868","public-inheritance"],"text":"","title":"\u516c\u5f00\u7ee7\u627f"},{"location":"public-member/","tags":["\u8bcd\u6c47\u8868","public-member"],"text":"\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u7684\u7ed3\u6784\u6216\u7c7b\u7684\u6210\u5458","title":"\u516c\u6709\u6210\u5458"},{"location":"pure-declaration/","tags":["glossary","declaration","pure-declaration"],"text":"\u76f8\u5173\u6982\u5ff5\uff1a \u58f0\u660e(declaration)","title":"\u7eaf\u58f0\u660e(pure-declaration)"},{"location":"pure-virtual/","tags":["\u8bcd\u6c47\u8868","pure-virtual"],"text":"","title":"\u7eaf\u865a\u51fd\u6570"},{"location":"qualifier/","tags":["glossary","qualifier"],"text":"\u4e5f\u79f0\u4e3a\u4fee\u9970\u7b26\u3002\u672c\u9879\u76ee\u7ffb\u8bd1\u65f6\u53c2\u8003\u4e86\u4faf\u6377\u8001\u5e08\u5728Essential C++ \u4e2d\u6587\u7248\u4e2d\u7684\u8bd1\u6cd5\u3002","title":"\u9650\u5b9a\u7b26"},{"location":"quiz/","text":"\u9762\u8bd5\u516b\u80a1\u6587","title":"Quiz"},{"location":"random-access-memory/","tags":["\u8bcd\u6c47\u8868","random-access-memory"],"text":"","title":"\u968f\u673a\u8bbf\u95ee\u5185\u5b58"},{"location":"range-based-for-loops/","tags":["\u8bcd\u6c47\u8868","range-based-for-loops"],"text":"\u6240\u6709\u5177\u6709 begin() \u548c end() \u6210\u5458\u51fd\u6570\u7684\u7c7b\u578b\uff0c\u6216\u8005\u53ef\u4ee5\u914d\u5408 std::begin() \u548c std::end() \u4f7f\u7528\u7684\u7c7b\u578b\uff0c\u90fd\u53ef\u4ee5\u7528\u4e8e\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af\u3002","title":"\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af"},{"location":"raw-pointer/","tags":["\u8bcd\u6c47\u8868","raw-pointer"],"text":"","title":"\u539f\u59cb\u6307\u9488"},{"location":"refactoring/","tags":["glossary","refactoring"],"text":"","title":"\u91cd\u6784"},{"location":"reference-container/","tags":["\u8bcd\u6c47\u8868","reference-container"],"text":"","title":"\u5f15\u7528\u5bb9\u5668"},{"location":"reflexive%20association/","tags":["\u8bcd\u6c47\u8868","reflexive association"],"text":"","title":"\u53cd\u8eab\u5173\u8054"},{"location":"reinterpret-casts/","tags":["\u8bcd\u6c47\u8868","reinterpret-casts"],"text":"","title":"\u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362"},{"location":"return-by-address/","tags":["\u8bcd\u6c47\u8868","return-by-address"],"text":"","title":"\u6309\u5730\u5740\u8fd4\u56de"},{"location":"return-by-reference/","tags":["\u8bcd\u6c47\u8868","return-by-reference"],"text":"","title":"\u6309\u5f15\u7528\u8fd4\u56de"},{"location":"return-by-value/","tags":["glossary","return-by-value"],"text":"","title":"\u6309\u503c\u8fd4\u56de"},{"location":"return-type-deduction/","tags":["\u8bcd\u6c47\u8868","return-type-deduction"],"text":"\u7c7b\u578b\u63a8\u65ad \u7684\u4e00\u79cd\uff0c\u4e00\u822c\u4e0d\u63a8\u8350\u4f7f\u7528\u3002\u4f7f\u7528\u65f6\u9700\u8981\u786e\u4fdd\u6240\u6709\u7684\u8fd4\u56de\u8bed\u53e5\u8fd4\u56de\u76f8\u540c\u7684\u7c7b\u578b\u3002 \u5728 \u533f\u540d\u51fd\u6570 \u4e2d\u7ecf\u5e38\u4f1a\u4f7f\u7528","title":"\u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad"},{"location":"rounding-error/","tags":["\u8bcd\u6c47\u8868","rounding-error"],"text":"","title":"\u820d\u5165\u8bef\u5dee"},{"location":"runtime/","tags":["glossary","runtime"],"text":"","title":"\u8fd0\u884c\u65f6"},{"location":"rvalue-reference/","tags":["\u8bcd\u6c47\u8868","rvalue-reference"],"text":"","title":"\u53f3\u503c\u5f15\u7528"},{"location":"rvalue/","tags":["\u8bcd\u6c47\u8868","rvalue"],"text":"\u6ce8\u610f\uff0c\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u662f\u5de6\u503c\uff0c\u56e0\u4e3a\u5b83\u5360\u7528\u5185\u5b58\u3002\u80cc\u666f\u662f\u4e3a\u4e86\u517c\u5bb9C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32","title":"\u53f3\u503c"},{"location":"scaling/","tags":["\u8bcd\u6c47\u8868","scaling"],"text":"","title":"\u6307\u9488\u7f29\u653e"},{"location":"scope-resolution-operator/","tags":["glossary","scope-resolution-operator"],"text":"::","title":"\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26"},{"location":"scope/","tags":["glossary","scope"],"text":"","title":"\u4f5c\u7528\u57df"},{"location":"scoped-enumerations/","tags":["\u8bcd\u6c47\u8868","scoped-enumerations"],"text":"","title":"\u6709\u4f5c\u7528\u57df\u679a\u4e3e"},{"location":"segment/","tags":["\u8bcd\u6c47\u8868","segment"],"text":"","title":"\u5185\u5b58\u6bb5"},{"location":"sentinel/","tags":["\u8bcd\u6c47\u8868","sentinel"],"text":"","title":"\u54e8\u5175"},{"location":"shadow/","tags":["\u8bcd\u6c47\u8868","shadow"],"text":"","title":"\u53d8\u91cf\u906e\u853d"},{"location":"shallow-copy/","tags":["\u8bcd\u6c47\u8868","shallow-copy"],"text":"","title":"\u6d45\u62f7\u8d1d"},{"location":"side-effects/","tags":["\u8bcd\u6c47\u8868","side-effects"],"text":"","title":"\u526f\u4f5c\u7528"},{"location":"signature/","tags":["\u8bcd\u6c47\u8868","signature"],"text":"","title":"\u7b7e\u540d"},{"location":"smart%20pointer%20class/","tags":["\u8bcd\u6c47\u8868","smart pointer class"],"text":"","title":"\u667a\u80fd\u6307\u9488\u7c7b"},{"location":"smart-pointer/","tags":["\u8bcd\u6c47\u8868","smart-pointer"],"text":"","title":"\u667a\u80fd\u6307\u9488"},{"location":"snake-case/","tags":["\u8bcd\u6c47\u8868","\u86c7\u5f62\u547d\u540d\u6cd5(snake_case)"],"text":"","title":"\u86c7\u5f62\u547d\u540d\u6cd5"},{"location":"stack-frame/","tags":["\u8bcd\u6c47\u8868","stack-frame"],"text":"","title":"\u6808\u5e27"},{"location":"stack-overflow/","tags":["\u8bcd\u6c47\u8868","stack-overflow"],"text":"","title":"\u5806\u6808\u6ea2\u51fa"},{"location":"stack/","tags":["\u8bcd\u6c47\u8868","stack"],"text":"","title":"\u6808"},{"location":"standard-stream/","tags":["\u8bcd\u6c47\u8868","standard-stream"],"text":"","title":"\u6807\u51c6\u6d41"},{"location":"statement/","tags":["glossary","statement"],"text":"","title":"\u8bed\u53e5"},{"location":"static-casts/","tags":["\u8bcd\u6c47\u8868","static-casts"],"text":"","title":"\u9759\u6001\u7c7b\u578b\u8f6c\u6362"},{"location":"static-initialization/","tags":["\u8bcd\u6c47\u8868","static-initialization"],"text":"","title":"\u9759\u6001\u521d\u59cb\u5316"},{"location":"static-member-functions/","tags":["\u8bcd\u6c47\u8868","static-member-functions"],"text":"","title":"\u9759\u6001\u6210\u5458\u51fd\u6570"},{"location":"static-member-variables/","tags":["\u8bcd\u6c47\u8868","static-member-variables"],"text":"","title":"\u9759\u6001\u6210\u5458\u53d8\u91cf"},{"location":"static-memory-allocation/","tags":["\u8bcd\u6c47\u8868","static-memory-allocation"],"text":"","title":"\u9759\u6001\u5185\u5b58\u5206\u914d"},{"location":"static-storage-duration/","tags":["\u8bcd\u6c47\u8868","static-storage-duration"],"text":"","title":"\u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4"},{"location":"static-variables/","tags":["\u8bcd\u6c47\u8868","static-variables"],"text":"","title":"\u9759\u6001\u53d8\u91cf"},{"location":"storage-duration/","tags":["\u8bcd\u6c47\u8868","storage-duration"],"text":"","title":"\u5b58\u50a8\u6301\u7eed\u65f6\u95f4"},{"location":"strong-except/","tags":["\u8bcd\u6c47\u8868","strong-except"],"text":"","title":"\u5f3a\u5f02\u5e38\u4fdd\u8bc1"},{"location":"struct-padding/","tags":["\u8bcd\u6c47\u8868","struct-padding"],"text":"","title":"\u7ed3\u6784\u4f53\u586b\u5145"},{"location":"stub/","text":"title:\u6253\u6869 alias: \u6253\u6869 english:stub type: glossary tags: - glossary - stub","title":"Stub"},{"location":"subscripts/","tags":["\u8bcd\u6c47\u8868","subscripts"],"text":"","title":"\u4e0b\u6807"},{"location":"super-class/","tags":["\u8bcd\u6c47\u8868","super-class"],"text":"","title":"\u8d85\u7c7b"},{"location":"symbolic-constants/","tags":["glossary","symbolic-constants"],"text":"","title":"\u7b26\u53f7\u5e38\u91cf"},{"location":"syntax/","tags":["\u8bcd\u6c47\u8868","\u8bed\u6cd5(syntax)"],"text":"","title":"\u8bed\u6cd5"},{"location":"tabledata/","text":"Category Meaning Implementated in C++ by Conditional statements Conditional statements cause a sequence of code to execute only if some condition is met. If, switch Jumps Jumps tell the CPU to start executing the statements at some other location. Goto, break, continue Function calls Function calls are jumps to some other location and back. Function calls, return Loops Loops tell the program to repeatedly execute some sequence of code zero or more times, until some condition is met. While, do-while, for, ranged-for Halts Halts tell the program to quit running. std::exit(), std::abort() Exceptions Exceptions are a special kind of flow control structure designed for error handling. Try, throw, catch","title":"Tabledata"},{"location":"tags/","text":"Tags Associations \u5173\u8054 Associative-containers \u5173\u8054\u5bb9\u5668 Binding \u7ed1\u5b9a C++11 11.13 - for-each \u5faa\u73af 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb 4.11 - \u5b57\u7b26 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 M.7 \u2014 std::shared_ptr C++14 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 4.15 - \u5b57\u9762\u91cf 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad M.6 \u2014 std::unique_ptr M.7 \u2014 std::shared_ptr C++17 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 11.16 \u2014 \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb 11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 4.18 - std::string_view \u7b80\u4ecb 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 7.17 - \u65ad\u8a00\u548c static_assert M.6 \u2014 std::unique_ptr M.7 \u2014 std::shared_ptr C++20 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 11.13 - for-each \u5faa\u73af 11.19 \u2014 \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 6.14 - Constexpr \u548c consteval \u51fd\u6570 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad M.7 \u2014 std::shared_ptr C-style-casts C \u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362 CTAD 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD Early binding \u65e9\u671f\u7ed1\u5b9a Encapsulation \u5c01\u88c5 Expression-parameters \u8868\u8fbe\u5f0f\u53c2\u6570 Frankenobject \u7f1d\u5408\u602a\u5bf9\u8c61 Function templates 8.13 - \u51fd\u6570\u6a21\u677f Late binding \u540e\u671f\u7ed1\u5b9a Lvalue 9.3 - \u5de6\u503c\u5f15\u7528 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 Multiple inheritance \u591a\u91cd\u7ee7\u627f Name-mangling \u540d\u79f0\u4fee\u9970 Numeral systems 4.16 - \u6570\u503c\u7cfb\u7edf\uff08\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236\uff09 Off-by-one \u5dee\u4e00\u9519\u8bef Output-manipulators \u8f93\u51fa\u64cd\u7eb5\u5668 Pointer arithmetic 11.9 - \u6307\u9488\u8fd0\u7b97\u548c\u6570\u7ec4\u7d22\u5f15 R-value-references M.2 - \u53f3\u503c\u5f15\u7528 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c RAII (Resource Acquisition Is Initialization) \u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09 Run-time-type-information-RTTI \u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f(RTTI) Sequence-containers \u987a\u5e8f\u5bb9\u5668 Syntax 3.1 - \u8bed\u6cd5\u548c\u8bed\u4e49\u9519\u8bef Template-specialization \u6a21\u677f\u7279\u5316 abbreviated function templates \u7f29\u5199\u51fd\u6570\u6a21\u677f abstract-base-classes 18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b abstraction \u62bd\u8c61 access specifier 13.3 - \u8bbf\u95ee\u4fee\u9970\u7b26 public \u548c private access-function \u6210\u5458\u8bbf\u95ee\u51fd\u6570 access-specifiers 17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 address 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de 9.9 - \u6309\u5730\u5740\u4f20\u9012 address-of-operator \u53d6\u5730\u5740\u64cd\u4f5c\u7b26 & aggregate initialization 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 aggregate-data-type \u805a\u5408\u6570\u636e\u7c7b\u578b aggregate-initialization \u805a\u5408\u521d\u59cb\u5316 aggregation 16.3 - \u805a\u5408\u5173\u7cfb \u805a\u5408 algorithms 11.19 \u2014 \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb alias 8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d ambiguous-match \u4e0d\u660e\u786e\u5339\u914d anonymous-object \u533f\u540d\u5bf9\u8c61 argument 12.5 - \u547d\u4ee4\u884c\u53c2\u6570 2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2 arguments \u5b9e\u53c2 arithmetic conversions 8.4 - \u7b97\u672f\u8f6c\u6362 arity \u5143\u6570 array 11.1 - \u6570\u7ec4\uff08\u7b2c\u4e00\u90e8\u5206\uff09 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.3 - \u6570\u7ec4\u548c\u5faa\u73af 11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f 11.5 - \u591a\u7ef4\u6570\u7ec4 11.8 - \u6307\u9488\u548c\u6570\u7ec4 arrays 11.12 - \u52a8\u6001\u5206\u914d\u6570\u7ec4 assert 7.17 - \u65ad\u8a00\u548c static_assert assignment 14.15 - \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26 association 16.4 - \u5173\u8054\u5173\u7cfb auto 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad automatic-memory-allocation \u81ea\u52a8\u5185\u5b58\u5206\u914d automatic-storage-duration \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 base-class \u57fa\u7c7b bit O.1 - bit\u6807\u8bb0\u548c\u901a\u8fc7std::bitset\u64cd\u4f5cbit O.2 - \u4f4d\u8fd0\u7b97\u7b26 O.3 - \u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u548c\u63a9\u7801\u64cd\u4f5c\u4f4d O.4 - \u4e8c\u8fdb\u5236\u548c\u5341\u8fdb\u5236\u8f6c\u6362 \u4f4d bitwise-and None bitwise-or \u6309\u4f4d\u6216 bool 4.9 - \u5e03\u5c14\u503c boolean 4.10 - if \u8bed\u53e5\u7b80\u4ecb bss segment bss\u6bb5 byte \u5b57\u8282 c-style 11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 call stack 3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808 call-stack \u8c03\u7528\u6808 callable-object \u53ef\u8c03\u7528\u5bf9\u8c61 callback functions \u56de\u8c03\u51fd\u6570 char 4.11 - \u5b57\u7b26 child-class \u5b50\u7c7b circular dependency M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898 class 13.11 - \u7c7b\u4ee3\u7801\u548c\u5934\u6587\u4ef6 13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570 13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf 13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570 13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b 13.16 - \u533f\u540d\u5bf9\u8c61 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b 13.18 - \u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 13.2 - \u7c7b\u548c\u7c7b\u6210\u5458 class template 10.10 - \u7c7b\u6a21\u677f 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD class-template \u7c7b\u6a21\u677f class-template-argument-deduction \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad class-type \u7c7b\u7c7b\u578b closure \u95ed\u5305 code coverage 7.13 - \u4ee3\u7801\u8986\u76d6\u7387 code segment \u4ee3\u7801\u6bb5 comments 1.2 - \u6ce8\u91ca compile-time \u7f16\u8bd1\u65f6 compiler \u7f16\u8bd1\u5668 composite-types \u7ec4\u5408\u7c7b\u578b composition 16.2 - \u7ec4\u5408\u5173\u7cfb compound data types 9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b const 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 9.8 - \u6307\u9488\u548cconst const class 13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570 const-cast const \u7c7b\u578b\u8f6c\u6362 const-member-function const \u6210\u5458\u51fd\u6570 const-pointer \u5e38\u91cf\u6307\u9488 constant expression \u5e38\u91cf\u8868\u8fbe\u5f0f constants 4.15 - \u5b57\u9762\u91cf consteval 6.14 - Constexpr \u548c consteval \u51fd\u6570 constexpr 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr 6.14 - Constexpr \u548c consteval \u51fd\u6570 constructor 13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570 14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570 constructors 13.5 - \u6784\u9020\u51fd\u6570 13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 container classes 16.6 - \u5bb9\u5668\u7c7b container-adapter \u5bb9\u5668\u9002\u914d\u5668 container-class \u5bb9\u5668\u7c7b containers 21.2 - STL\u5bb9\u5668 21.3 - STL\u8fed\u4ee3\u5668 21.4 - STL\u7b97\u6cd5 control flow 7.1 - \u63a7\u5236\u6d41 7.4 - switch \u8bed\u53e5\u57fa\u7840 conversions 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 8.3 - \u6570\u503c\u8f6c\u6362 8.4 - \u7b97\u672f\u8f6c\u6362 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast converting constructors 14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57 converting-constructors \u8f6c\u6362\u6784\u9020\u51fd\u6570 copy constructor 14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570 copy initialization 14.13 - \u62f7\u8d1d\u521d\u59cb\u5316 copy-constructor \u62f7\u8d1d\u6784\u9020\u51fd\u6570 copy-constructors \u62f7\u8d1d\u6784\u9020\u51fd\u6570 copy-initialization \u62f7\u8d1d\u521d\u59cb\u5316 copy-semantics \u62f7\u8d1d\u8bed\u4e49 covariant \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b covariant-return-types 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b dangling \u60ac\u5782 data segment \u6570\u636e\u6bb5 data type 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb 4.4 - \u6709\u7b26\u53f7\u6574\u578b 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 4.8 - \u6d6e\u70b9\u6570 4.9 - \u5e03\u5c14\u503c 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad debugger 3.6 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u5355\u6b65\u8c03\u8bd5 3.7 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8fd0\u884c\u548c\u65ad\u70b9 3.8 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u76d1\u63a7\u53d8\u91cf 3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808 debugging 3.2 - \u4ee3\u7801\u8c03\u8bd5\u6b65\u9aa4 3.3 - \u4ee3\u7801\u8c03\u8bd5\u7b56\u7565 3.4 \u2014 \u57fa\u672c\u4ee3\u7801\u8c03\u8bd5\u6280\u672f 3.5 - \u66f4\u591a\u4ee3\u7801\u8c03\u8bd5\u6280\u672f declaration 6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e \u58f0\u660e \u7eaf\u58f0\u660e(pure-declaration) deep copy 14.16 - \u6d45\u62f7\u8d1d\u548c\u6df1\u62f7\u8d1d 14.17 - \u91cd\u8f7d\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u6a21\u677f deep-copy \u6df1\u62f7\u8d1d default 8.12 - \u9ed8\u8ba4\u53c2\u6570 default-constructor \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 default-initialization \u9ed8\u8ba4\u521d\u59cb\u5316 default-member-initializer \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c definition \u5b9a\u4e49 delegating 13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570 delegating-constructors \u59d4\u6258\u6784\u9020\u51fd\u6570 delete 11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d 14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57 dependencies 16.5 - \u4f9d\u8d56\u5173\u7cfb dependency \u4f9d\u8d56 dereference-operator \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 derived-class \u6d3e\u751f\u7c7b desgin 2.13 - \u5982\u4f55\u8bbe\u8ba1\u7b2c\u4e00\u4e2a\u7a0b\u5e8f designated-initializers \u6307\u5b9a\u521d\u59cb\u5316 destructor \u6790\u6784\u51fd\u6570 destructors 13.9 - \u6790\u6784\u51fd\u6570 direct-initialization \u76f4\u63a5\u521d\u59cb\u5316 do-while 7.8 - do-while \u8bed\u53e5 downcasting \u5411\u4e0b\u8f6c\u6362 dumb-pointer \u7b28\u6307\u9488 dynamic casting 18.10 - \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 dynamic duration \u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 dynamic-casts \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 dynamic-initialization \u52a8\u6001\u521d\u59cb\u5316 dynamic-memory 11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d dynamic-memory-allocation \u52a8\u6001\u5185\u5b58\u5206\u914d early-binding 18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a ellipsis 12.6 - \u7701\u7565\u53f7\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 encapsulation 13.4 - \u6210\u5458\u8bbf\u95ee\u51fd\u6570\u4e0e\u5c01\u88c5 enum 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b 10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 enum-class \u679a\u4e3e\u7c7b error handling 7.15 - \u53d1\u73b0\u9519\u8bef\u548c\u5904\u7406\u9519\u8bef 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406 evaluation \u6c42\u503c excalidraw Drawing 2023 02 11 13.33.50.excalidraw Template.excalidraw C++ basic onepage Moc onepage exception-safety-issue \u5f02\u5e38\u5b89\u5168\u95ee\u9898 explicit 14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57 explicit-type-conversion 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 expression 1.10 - \u8868\u8fbe\u5f0f\u7b80\u4ecb expression type \u8868\u8fbe\u5f0f\u7c7b\u578b external-linkage \u5916\u90e8\u94fe\u63a5 external-variable \u5916\u90e8\u53d8\u91cf extraction-operator \u63d0\u53d6\u8fd0\u7b97\u7b26 fallthrough \u8d2f\u7a7f\u5c5e\u6027 file 23.6 - \u57fa\u672c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa file-scope \u6587\u4ef6\u4f5c\u7528\u57df final 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b fixed-array \u56fa\u5b9a\u6570\u7ec4 fixed-width 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t float 4.8 - \u6d6e\u70b9\u6570 float point 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 for 7.7 - \u5faa\u73af\u548c while \u8bed\u53e5 7.9 - for \u8bed\u53e5 for-each 11.13 - for-each \u5faa\u73af fordward declaration 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 format-group \u683c\u5f0f\u7ec4 formatting 1.8 - \u7a7a\u767d\u7b26\u548c\u57fa\u672c\u6392\u7248 forward declaration \u524d\u5411\u58f0\u660e friend 13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b 13.16 - \u533f\u540d\u5bf9\u8c61 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b 13.18 - \u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 friend-class \u53cb\u5143\u7c7b friend-function \u53cb\u5143\u51fd\u6570 function 12.1 - \u51fd\u6570\u6307\u9488 2.1 - \u51fd\u6570\u7b80\u4ecb 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 2.6 - \u4e3a\u4ec0\u4e48\u51fd\u6570\u6709\u7528\uff0c\u5982\u4f55\u9ad8\u6548\u4f7f\u7528\u51fd\u6570 function template 8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f function-instance \u51fd\u6570\u5b9e\u4f8b function-literal \u51fd\u6570\u5b57\u9762\u91cf function-pointer \u51fd\u6570\u6307\u9488 function-template \u51fd\u6570\u6a21\u677f function-template-instantiation \u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316 generic-algorithm \u6cdb\u578b\u7b97\u6cd5 global 6.4 - \u5168\u5c40\u53d8\u91cf 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf global variable 6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c \u5168\u5c40\u53d8\u91cf glossary \u7f16\u8bd1\u65f6 \u58f0\u660e \u5b9a\u4e49 \u524d\u5411\u58f0\u660e \u5168\u5c40\u53d8\u91cf \u6807\u8bc6\u7b26(identifier) \u5b9e\u4f8b\u5316 \u6307\u4ee4 \u94fe\u63a5\u5668(linker) \u5b8f \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u5f62\u53c2 \u6309\u503c\u4f20\u9012 \u9884\u5904\u7406\u5668\u6307\u4ee4 \u9884\u5904\u7406\u5668 \u7eaf\u58f0\u660e(pure-declaration) \u9650\u5b9a\u7b26 \u91cd\u6784 \u6309\u503c\u8fd4\u56de \u8fd0\u884c\u65f6 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u4f5c\u7528\u57df \u8bed\u53e5 \u7b26\u53f7\u5e38\u91cf goto 7.6 - goto \u8bed\u53e5 halt 7.11 - \u6302\u8d77\uff08\u63d0\u524d\u7ed3\u675f\u7a0b\u5e8f\uff09 header 2.11 - \u5934\u6587\u4ef6 header guards 2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e heap 12.2 - \u6808\u548c\u5806 \u5806 identifier 1.7 - \u5173\u952e\u5b57\u548c\u6807\u8bc6\u7b26 \u6807\u8bc6\u7b26(identifier) identity \u8eab\u4efd\u7279\u5f81 if 4.10 - if \u8bed\u53e5\u7b80\u4ecb 7.2 - if \u8bed\u53e5\u548c\u8bed\u53e5\u5757 7.3 - \u5e38\u89c1\u7684 if \u8bed\u53e5\u9519\u8bef immediate-functions \u5373\u65f6\u51fd\u6570 implicit-constructor \u9690\u5f0f\u6784\u9020\u51fd\u6570 implicit-object \u9690\u5f0f\u5bf9\u8c61 implicit-type-conversion \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 inheritance 17.1 - \u7ee7\u627f\u7b80\u4ecb 17.2 - C++\u7ee7\u627f\u57fa\u7840 17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f 17.4 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u548c\u521d\u59cb\u5316 17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 17.6 - \u4e3a\u6d3e\u751f\u7c7b\u6dfb\u52a0\u65b0\u529f\u80fd 17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570 17.9 - \u591a\u91cd\u7ee7\u627f 18.11 - \u4f7f\u7528 << \u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b \u7ee7\u627f initialization 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u62f7\u8d1d\u521d\u59cb\u5316 \u76f4\u63a5\u521d\u59cb\u5316 \u5217\u8868\u521d\u59cb\u5316 initializer-list \u521d\u59cb\u5316\u503c\u5217\u8868 initializer_list 16.7 - std::initializer_list inline 6.13 - \u5185\u8054\u51fd\u6570 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf inline namespace 6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4 inline-function \u5185\u8054\u51fd\u6570 inline-namespace \u5185\u8054\u547d\u540d\u7a7a\u95f4 inline-variables \u5185\u8054\u53d8\u91cf input-manipulator 4.17 - std::string \u7b80\u4ecb input-manipulators \u8f93\u5165\u64cd\u7eb5\u5668 insertion-operator \u63d2\u5165\u8fd0\u7b97\u7b26 instance \u5b9e\u4f8b instantiated \u5b9e\u4f8b\u5316 instuction \u6307\u4ee4 integer 4.4 - \u6709\u7b26\u53f7\u6574\u578b 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t integral-type \u6574\u578b\u7c7b\u578b interface-class \u63a5\u53e3\u7c7b interface-classes 18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b internal-linkage \u5185\u90e8\u94fe\u63a5 internal-variable \u5185\u90e8\u53d8\u91cf io 23.1 - \u8f93\u5165\u8f93\u51fa\u6d41 23.2 - \u4f7f\u7528istream\u5904\u7406\u8f93\u5165 23.3 - \u4f7f\u7528ostream\u548cios\u5904\u7406\u8f93\u51fa 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b 23.5 - \u6d41\u72b6\u6001\u548c\u8f93\u5165\u9a8c\u8bc1 23.6 - \u57fa\u672c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa 23.7 - \u968f\u673a\u6587\u4ef6\u8f93\u5165\u8f93\u51fa iostream 1.5 - iostream \u7b80\u4ecb\uff1acout\uff0ccin \u548c endl iterating \u904d\u5386 iterator 11.18 \u2014 \u8fed\u4ee3\u5668\u7b80\u4ecb \u8fed\u4ee3\u5668 lambda 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 \u533f\u540d\u51fd\u6570 late-binding 18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a link \u94fe\u63a5 linkage 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 6.3 - \u5c40\u90e8\u53d8\u91cf 6.6 - \u5185\u90e8\u94fe\u63a5 6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e \u94fe\u63a5\u5c5e\u6027 linker \u94fe\u63a5\u5668(linker) list assignment operator \u7c7b\u522b\u8d4b\u503c\u8fd0\u7b97\u7b26 list-constructor \u5217\u8868\u6784\u9020\u51fd\u6570 list-initialization \u5217\u8868\u521d\u59cb\u5316 literal 4.15 - \u5b57\u9762\u91cf literals 1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 \u5b57\u9762\u91cf local variable 6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf local variables 6.3 - \u5c40\u90e8\u53d8\u91cf loop 11.3 - \u6570\u7ec4\u548c\u5faa\u73af lvalue \u5de6\u503c lvalue references 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 lvalue-reference \u5de6\u503c\u5f15\u7528 lvalues 9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09 macro \u5b8f member access operator \u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26 member selection 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 member-function \u6210\u5458\u51fd\u6570 member-initializer-list \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 member-selection-operator \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 member-types \u6210\u5458\u7c7b\u578b member-variable \u6210\u5458\u53d8\u91cf members 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 memberwise initialization \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 memberwise-assignment \u6210\u5458\u4f9d\u6b21\u8d4b\u503c memberwise-copy \u6210\u5458\u4f9d\u6b21\u62f7\u8d1d memory-leak \u5185\u5b58\u6cc4\u6f0f mixin None move M.4 - std::move M.5 - std::move_if_noexcept M.6 \u2014 std::unique_ptr move assignment operator \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 move-constructor \u79fb\u52a8\u6784\u9020\u51fd\u6570 move-semantics M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb \u79fb\u52a8\u8bed\u4e49 multiple-inheritance 17.9 - \u591a\u91cd\u7ee7\u627f named-cast \u5177\u540d\u7c7b\u578b\u8f6c\u6362 namespace 2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u547d\u540d\u7a7a\u95f4 namespace aliases 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 narrowing-convertions \u7f29\u7a84\u8f6c\u6362 new 11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d numeric conversions 8.3 - \u6570\u503c\u8f6c\u6362 numeric promotion 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 numeric promotions \u6570\u503c\u63d0\u5347 numeric-conversions \u6570\u503c\u8f6c\u6362 object 1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf 13.1 - \u6b22\u8fce\u6765\u5230\u9762\u5411\u5bf9\u8c61\u7684\u4e16\u754c 16.1 - \u5bf9\u8c61\u5173\u7cfb 16.2 - \u7ec4\u5408\u5173\u7cfb 16.3 - \u805a\u5408\u5173\u7cfb 16.4 - \u5173\u8054\u5173\u7cfb 16.5 - \u4f9d\u8d56\u5173\u7cfb 16.6 - \u5bb9\u5668\u7c7b 16.7 - std::initializer_list object-composition \u5bf9\u8c61\u7ec4\u5408 object-like-macros \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f object-slicing \u5bf9\u8c61\u5207\u7247 one-definition-rule \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) operands \u64cd\u4f5c\u6570 operator 1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 5.1 - \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 5.3 - \u6c42\u6a21\u548c\u6307\u6570\u8fd0\u7b97 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 5.5 - \u9017\u53f7\u548c\u6761\u4ef6\u8fd0\u7b97\u7b26 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 5.7 - \u903b\u8f91\u8fd0\u7b97\u7b26 \u8fd0\u7b97\u7b26 operator<< 18.11 - \u4f7f\u7528 << \u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b optional-parameter \u53ef\u9009\u53c2\u6570 overflow 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 overlapping 13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570 overload 14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26 14.15 - \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 8.10 - \u91cd\u8f7d\u51fd\u6570\u7684\u533a\u5206 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u4e0d\u660e\u786e\u5339\u914d \u91cd\u8f7d overload-resolution \u91cd\u8f7d\u89e3\u6790 overloading 8.9 - \u51fd\u6570\u91cd\u8f7d override 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b \u91cd\u5199 overriding 17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570 overriding-virtualization 18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316 parameter 2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2 parameters \u5f62\u53c2 parent-class \u7236\u7c7b parenthesis 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 pass-by-address \u6309\u5730\u5740\u4f20\u9012 pass-by-reference \u6309\u5f15\u7528\u4f20\u9012 pass-by-value \u6309\u503c\u4f20\u9012 placeholder-types \u5360\u4f4d\u7b26\u7c7b\u578b pointer 11.15 \u2014 \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u548c\u591a\u7ef4\u6570\u7ec4 11.8 - \u6307\u9488\u548c\u6570\u7ec4 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 9.6 - \u6307\u9488\u7b80\u4ecb 9.7 - \u7a7a\u6307\u9488 9.8 - \u6307\u9488\u548cconst 9.9 - \u6309\u5730\u5740\u4f20\u9012 pointer-to-const \u6307\u5411\u5e38\u91cf\u7684\u6307\u9488 pointers 18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 polymorphism 18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001 \u591a\u6001 pragma once 2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e preprocessor 2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb \u9884\u5904\u7406\u5668\u6307\u4ee4 \u9884\u5904\u7406\u5668 private-inheritance \u79c1\u6709\u7ee7\u627f private-member \u79c1\u6709\u6210\u5458 program organization 2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f protected-inheritance \u53d7\u4fdd\u62a4\u7ee7\u627f protected-members \u53d7\u4fdd\u62a4\u6210\u5458 public-inheritance \u516c\u5f00\u7ee7\u627f public-member \u516c\u6709\u6210\u5458 pure-declaration \u7eaf\u58f0\u660e(pure-declaration) pure-virtual \u7eaf\u865a\u51fd\u6570 pure-virtual-functions 18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b qualifier \u9650\u5b9a\u7b26 random-access-memory \u968f\u673a\u8bbf\u95ee\u5185\u5b58 range-based-for-loops \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af raw-pointer \u539f\u59cb\u6307\u9488 recursion 12.4 - \u9012\u5f52 refactoring \u91cd\u6784 reference 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de reference-container \u5f15\u7528\u5bb9\u5668 references 18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 M.2 - \u53f3\u503c\u5f15\u7528 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c reflexive association \u53cd\u8eab\u5173\u8054 reinterpret-casts \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 return 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de return-by-address \u6309\u5730\u5740\u8fd4\u56de return-by-reference \u6309\u5f15\u7528\u8fd4\u56de return-by-value \u6309\u503c\u8fd4\u56de return-type-deduction \u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad rounding-error \u820d\u5165\u8bef\u5dee runtime \u8fd0\u884c\u65f6 rvalue \u53f3\u503c rvalue-reference \u53f3\u503c\u5f15\u7528 rvalues 9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09 scaling \u6307\u9488\u7f29\u653e scientific notation 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 scope 2.5 - \u5c40\u90e8\u4f5c\u7528\u57df 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 \u4f5c\u7528\u57df scope resolution operator 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 scope-resolution-operator \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 scoped enumerations 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 scoped-enumerations \u6709\u4f5c\u7528\u57df\u679a\u4e3e segment \u5185\u5b58\u6bb5 sentinel \u54e8\u5175 setup 0.8 - \u4e00\u4e9b\u5178\u578b\u7684 C++ \u95ee\u9898 shadow \u53d8\u91cf\u906e\u853d shallow copy 14.16 - \u6d45\u62f7\u8d1d\u548c\u6df1\u62f7\u8d1d 14.17 - \u91cd\u8f7d\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u6a21\u677f shallow-copy \u6d45\u62f7\u8d1d shared_ptr M.7 \u2014 std::shared_ptr M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898 side-effects \u526f\u4f5c\u7528 signature \u7b7e\u540d size_t 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t sizeof 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 smart pointer class \u667a\u80fd\u6307\u9488\u7c7b smart-pointer \u667a\u80fd\u6307\u9488 smart-pointers M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb sort 11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f stack 12.2 - \u6808\u548c\u5806 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a \u6808 stack-frame \u6808\u5e27 stack-overflow \u5806\u6808\u6ea2\u51fa standard-stream \u6807\u51c6\u6d41 statement 6.1 - \u590d\u5408\u8bed\u53e5\uff08\u8bed\u53e5\u5757\uff09 \u8bed\u53e5 statements 1.1 - \u7a0b\u5e8f\u7ed3\u6784\u548c\u8bed\u53e5 static 6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf static membe function 13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570 static member 13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 static member variable 13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf static-cast 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast static-casts \u9759\u6001\u7c7b\u578b\u8f6c\u6362 static-initialization \u9759\u6001\u521d\u59cb\u5316 static-member-functions \u9759\u6001\u6210\u5458\u51fd\u6570 static-member-variables \u9759\u6001\u6210\u5458\u53d8\u91cf static-memory-allocation \u9759\u6001\u5185\u5b58\u5206\u914d static-storage-duration \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 static-variables \u9759\u6001\u53d8\u91cf static_assert 7.17 - \u65ad\u8a00\u548c static_assert static_cast 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast 8.3 - \u6570\u503c\u8f6c\u6362 std::array 11.16 \u2014 \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb std::string_view 11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 stl 21.1 - \u6807\u51c6\u5e93 21.2 - STL\u5bb9\u5668 21.3 - STL\u8fed\u4ee3\u5668 21.4 - STL\u7b97\u6cd5 storage duration 6.3 - \u5c40\u90e8\u53d8\u91cf 6.4 - \u5168\u5c40\u53d8\u91cf storage-duration \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 string 11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 22.1 - std::string \u548c std::wstring 22.2 - std-string \u7684\u6784\u9020\u548c\u6790\u6784 22.3 - std-string \u7684\u957f\u5ea6\u548c\u5bb9\u91cf 22.4 - std-string \u5b57\u7b26\u8bbf\u95ee\u548cC\u5b57\u7b26\u6570\u7ec4\u8f6c\u6362 22.5 - std-string \u8d4b\u503c\u548c\u4ea4\u6362 22.6 - std-string \u8ffd\u52a0 22.7 - std-string \u63d2\u5165 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b 4.17 - std::string \u7b80\u4ecb string_view 4.18 - std::string_view \u7b80\u4ecb strong-except \u5f3a\u5f02\u5e38\u4fdd\u8bc1 struct 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 10.8 - \u7ed3\u6784\u4f53\u4f20\u9012\u53ca\u5176\u4ed6 10.9 - \u57fa\u4e8e\u6307\u9488\u548c\u5f15\u7528\u7684\u6210\u5458\u9009\u62e9 struct-padding \u7ed3\u6784\u4f53\u586b\u5145 subscript 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 subscripts \u4e0b\u6807 summary 1.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - C++\u57fa\u7840 10.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u679a\u4e3e\u548c\u7ed3\u6784\u4f53 10.y - \u5b66\u4f1a\u4f7f\u7528C++\u53c2\u8003\u624b\u518c 11.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5206\u914d 12.8 - lambda \u95ed\u5305 12.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570 13.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u9762\u5411\u5bf9\u8c61\u57fa\u7840 14.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26\u91cd\u8f7d 16.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5bf9\u8c61\u5173\u7cfb 17.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7ee7\u627f 18.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u865a\u51fd\u6570 19.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6a21\u677f\u548c\u7c7b 2.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570\u4e0e\u6587\u4ef6 20.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f02\u5e38 3.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4ee3\u7801\u8c03\u8bd5 4.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u57fa\u672c\u6570\u636e\u7c7b\u578b 5.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 7.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406 8.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d 9.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f15\u7528\u548c\u6307\u9488 M.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u79fb\u52a8\u548c\u667a\u80fd\u6307\u9488 super-class \u8d85\u7c7b switch 7.4 - switch \u8bed\u53e5\u57fa\u7840 7.5 - switch fallthrough\u5c5e\u6027\u548c\u4f5c\u7528\u57df symbolic constants 11.10 - C \u98ce\u683c\u5b57\u7b26\u4e32\u7b26\u53f7\u5e38\u91cf symbolic-constants \u7b26\u53f7\u5e38\u91cf template class specialization \u7c7b\u6a21\u677f\u7279\u5316 testing 7.12 - \u4ee3\u7801\u6d4b\u8bd5 the-diamond-problem \u83f1\u5f62\u7ee7\u627f\u95ee\u9898 this 13.10 - \u9690\u85cf\u7684this\u6307\u9488 translation \u7ffb\u8bd1 type 10.1 - \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7b80\u4ecb type conversion 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 type deduction 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u63a8\u65ad type-aliases \u7c7b\u578b\u522b\u540d type-conversion \u7c7b\u578b\u8f6c\u6362 type-definition \u7c7b\u578b\u5b9a\u4e49 type-inference \u7c7b\u578b\u63a8\u65ad type-safe \u7c7b\u578b\u5b89\u5168 type-signature \u7c7b\u578b\u7b7e\u540d typecast 14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26 typedef 8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d typeid typeid \u8fd0\u7b97\u7b26 typeinfo 8.4 - \u7b97\u672f\u8f6c\u6362 unary 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 undefined behavior 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 4.4 - \u6709\u7b26\u53f7\u6574\u578b undefined-behavior \u672a\u5b9a\u4e49\u884c\u4e3a uniform initialization \u7edf\u4e00\u521d\u59cb\u5316 unmanned namespace 6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4 unnamed-namespace \u533f\u540d\u547d\u540d\u7a7a\u95f4 unscoped enumerations 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b 10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa unscoped-enumerations \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b upcasting \u5411\u4e0a\u8f6c\u6362 using 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 using-declaration using \u58f0\u660e using-directive using \u6307\u4ee4 value-category \u503c\u7c7b\u578b value-container \u503c\u5bb9\u5668 value-initialization \u503c\u521d\u59cb\u5316 value-preserving \u503c\u4fdd\u7559 variable 1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 variable shadowing 6.5 - \u53d8\u91cf\u906e\u853d vector 11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a virtual functions 18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001 virtual-assignment 18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316 virtual-base-class \u865a\u57fa\u7c7b virtual-base-classes 18.8 - \u865a\u57fa\u7c7b 18.9 - \u5bf9\u8c61\u5207\u7247 virtual-destructor \u865a\u6790\u6784\u51fd\u6570 virtual-destructors 18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316 virtual-function \u865a\u51fd\u6570 virtual-table 18.6 - \u865a\u51fd\u6570\u8868 \u865a\u8868 void 11.14 \u2014 void \u6307\u9488 2.3 - void \u51fd\u6570 4.2 - Void void pointer void \u6307\u9488 weak_ptr M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898 while 7.7 - \u5faa\u73af\u548c while \u8bed\u53e5 wild-pointer \u91ce\u6307\u9488 word \u5b57\u957f wstring 22.1 - std::string \u548c std::wstring zero-initialization 0\u521d\u59cb\u5316 \u51fd\u6570\u539f\u578b(function prototype) \u51fd\u6570\u539f\u578b \u521d\u59cb\u5316 \u521d\u59cb\u5316 \u521d\u59cb\u5316\u503c(initializer) \u521d\u59cb\u5316\u503c \u52a8\u6001\u6570\u7ec4 \u52a8\u6001\u6570\u7ec4 \u5bf9\u8c61(objec) \u5bf9\u8c61 \u62bd\u8c61\u7c7b \u62bd\u8c61\u7c7b \u62ec\u53f7\u521d\u59cb\u5316 None \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u6784\u9020\u51fd\u6570 \u6784\u9020\u51fd\u6570 \u6a21\u677f\u7c7b\u578b\u53c2\u6570 \u6a21\u677f\u7c7b\u578b\u53c2\u6570 \u79bb\u5f00\u4f5c\u7528\u57df \u79bb\u5f00\u4f5c\u7528\u57df \u7b80\u5355\u8f6c\u6362(trivial conversion) \u7b80\u5355\u8f6c\u6362 \u7c7b\u5b9e\u4f8b \u7c7b\u5b9e\u4f8b \u86c7\u5f62\u547d\u540d\u6cd5(snake_case) \u86c7\u5f62\u547d\u540d\u6cd5 \u8bcd\u6c47\u8868 \u5173\u8054 \u5173\u8054\u5bb9\u5668 \u7ed1\u5b9a C \u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362 \u65e9\u671f\u7ed1\u5b9a \u5c01\u88c5 \u8868\u8fbe\u5f0f\u53c2\u6570 \u7f1d\u5408\u602a\u5bf9\u8c61 \u540e\u671f\u7ed1\u5b9a \u591a\u91cd\u7ee7\u627f \u540d\u79f0\u4fee\u9970 \u5dee\u4e00\u9519\u8bef \u8f93\u51fa\u64cd\u7eb5\u5668 \u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09 \u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f(RTTI) \u987a\u5e8f\u5bb9\u5668 \u6a21\u677f\u7279\u5316 \u7f29\u5199\u51fd\u6570\u6a21\u677f \u62bd\u8c61\u7c7b \u62bd\u8c61 \u6210\u5458\u8bbf\u95ee\u51fd\u6570 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u53d6\u5730\u5740\u64cd\u4f5c\u7b26 & \u805a\u5408\u6570\u636e\u7c7b\u578b \u805a\u5408\u521d\u59cb\u5316 \u805a\u5408 \u4e0d\u660e\u786e\u5339\u914d \u533f\u540d\u5bf9\u8c61 \u5b9e\u53c2 \u5143\u6570 \u81ea\u52a8\u5185\u5b58\u5206\u914d \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u57fa\u7c7b \u4f4d None \u6309\u4f4d\u6216 bss\u6bb5 \u5b57\u8282 \u8c03\u7528\u6808 \u53ef\u8c03\u7528\u5bf9\u8c61 \u56de\u8c03\u51fd\u6570 \u5b50\u7c7b \u7c7b\u5b9e\u4f8b \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad \u7c7b\u6a21\u677f \u7c7b\u7c7b\u578b \u95ed\u5305 \u4ee3\u7801\u6bb5 \u7f16\u8bd1\u5668 \u7ec4\u5408\u7c7b\u578b const \u7c7b\u578b\u8f6c\u6362 const \u6210\u5458\u51fd\u6570 \u5e38\u91cf\u6307\u9488 \u5e38\u91cf\u8868\u8fbe\u5f0f \u6784\u9020\u51fd\u6570 \u5bb9\u5668\u9002\u914d\u5668 \u5bb9\u5668\u7c7b \u8f6c\u6362\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u521d\u59cb\u5316 \u62f7\u8d1d\u8bed\u4e49 \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b \u60ac\u5782 \u6570\u636e\u6bb5 \u6df1\u62f7\u8d1d \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 \u9ed8\u8ba4\u521d\u59cb\u5316 \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c \u59d4\u6258\u6784\u9020\u51fd\u6570 \u4f9d\u8d56 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u6d3e\u751f\u7c7b \u6307\u5b9a\u521d\u59cb\u5316 \u6790\u6784\u51fd\u6570 \u76f4\u63a5\u521d\u59cb\u5316 \u5411\u4e0b\u8f6c\u6362 \u7b28\u6307\u9488 \u52a8\u6001\u6570\u7ec4 \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u52a8\u6001\u521d\u59cb\u5316 \u52a8\u6001\u5185\u5b58\u5206\u914d \u679a\u4e3e\u7c7b \u6c42\u503c \u5f02\u5e38\u5b89\u5168\u95ee\u9898 \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u8868\u8fbe\u5f0f\u7c7b\u578b \u5916\u90e8\u94fe\u63a5 \u5916\u90e8\u53d8\u91cf \u63d0\u53d6\u8fd0\u7b97\u7b26 \u8d2f\u7a7f\u5c5e\u6027 \u6587\u4ef6\u4f5c\u7528\u57df \u56fa\u5b9a\u6570\u7ec4 \u683c\u5f0f\u7ec4 \u53cb\u5143\u7c7b \u53cb\u5143\u51fd\u6570 \u51fd\u6570\u5b9e\u4f8b \u51fd\u6570\u5b57\u9762\u91cf \u51fd\u6570\u6307\u9488 \u51fd\u6570\u539f\u578b \u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316 \u51fd\u6570\u6a21\u677f \u6cdb\u578b\u7b97\u6cd5 \u79bb\u5f00\u4f5c\u7528\u57df \u5806 \u8eab\u4efd\u7279\u5f81 \u5373\u65f6\u51fd\u6570 \u9690\u5f0f\u6784\u9020\u51fd\u6570 \u9690\u5f0f\u5bf9\u8c61 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u7ee7\u627f \u521d\u59cb\u5316 \u521d\u59cb\u5316\u503c\u5217\u8868 \u521d\u59cb\u5316\u503c \u5185\u8054\u51fd\u6570 \u5185\u8054\u547d\u540d\u7a7a\u95f4 \u5185\u8054\u53d8\u91cf \u8f93\u5165\u64cd\u7eb5\u5668 \u63d2\u5165\u8fd0\u7b97\u7b26 \u5b9e\u4f8b \u6574\u578b\u7c7b\u578b \u63a5\u53e3\u7c7b \u5185\u90e8\u94fe\u63a5 \u5185\u90e8\u53d8\u91cf \u904d\u5386 \u8fed\u4ee3\u5668 \u533f\u540d\u51fd\u6570 \u94fe\u63a5 \u94fe\u63a5\u5c5e\u6027 \u7c7b\u522b\u8d4b\u503c\u8fd0\u7b97\u7b26 \u5217\u8868\u6784\u9020\u51fd\u6570 \u5217\u8868\u521d\u59cb\u5316 \u5b57\u9762\u91cf \u5de6\u503c\u5f15\u7528 \u5de6\u503c \u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26 \u6210\u5458\u51fd\u6570 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 \u6210\u5458\u7c7b\u578b \u6210\u5458\u53d8\u91cf \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \u6210\u5458\u4f9d\u6b21\u8d4b\u503c \u6210\u5458\u4f9d\u6b21\u62f7\u8d1d \u5185\u5b58\u6cc4\u6f0f None \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 \u79fb\u52a8\u6784\u9020\u51fd\u6570 \u79fb\u52a8\u8bed\u4e49 \u5177\u540d\u7c7b\u578b\u8f6c\u6362 \u547d\u540d\u7a7a\u95f4 \u7f29\u7a84\u8f6c\u6362 \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u6570\u503c\u63d0\u5347 \u6570\u503c\u8f6c\u6362 \u5bf9\u8c61\u7ec4\u5408 \u5bf9\u8c61\u5207\u7247 \u5bf9\u8c61 \u64cd\u4f5c\u6570 \u8fd0\u7b97\u7b26 \u8d85\u51fa\u4f5c\u7528\u57df \u91cd\u8f7d\u89e3\u6790 \u91cd\u8f7d \u91cd\u5199 \u7236\u7c7b \u6309\u5730\u5740\u4f20\u9012 \u6309\u5f15\u7528\u4f20\u9012 \u5360\u4f4d\u7b26\u7c7b\u578b \u6307\u5411\u5e38\u91cf\u7684\u6307\u9488 \u591a\u6001 \u79c1\u6709\u7ee7\u627f \u79c1\u6709\u6210\u5458 \u53d7\u4fdd\u62a4\u7ee7\u627f \u53d7\u4fdd\u62a4\u6210\u5458 \u516c\u5f00\u7ee7\u627f \u516c\u6709\u6210\u5458 \u7eaf\u865a\u51fd\u6570 \u968f\u673a\u8bbf\u95ee\u5185\u5b58 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u539f\u59cb\u6307\u9488 \u5f15\u7528\u5bb9\u5668 \u53cd\u8eab\u5173\u8054 \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 \u6309\u5730\u5740\u8fd4\u56de \u6309\u5f15\u7528\u8fd4\u56de \u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad \u820d\u5165\u8bef\u5dee \u53f3\u503c\u5f15\u7528 \u53f3\u503c \u6307\u9488\u7f29\u653e \u6709\u4f5c\u7528\u57df\u679a\u4e3e \u5185\u5b58\u6bb5 \u54e8\u5175 \u53d8\u91cf\u906e\u853d \u6d45\u62f7\u8d1d \u526f\u4f5c\u7528 \u7b7e\u540d \u667a\u80fd\u6307\u9488\u7c7b \u667a\u80fd\u6307\u9488 \u86c7\u5f62\u547d\u540d\u6cd5 \u6808\u5e27 \u5806\u6808\u6ea2\u51fa \u6808 \u6807\u51c6\u6d41 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u9759\u6001\u521d\u59cb\u5316 \u9759\u6001\u6210\u5458\u51fd\u6570 \u9759\u6001\u6210\u5458\u53d8\u91cf \u9759\u6001\u5185\u5b58\u5206\u914d \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u9759\u6001\u53d8\u91cf \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u5f3a\u5f02\u5e38\u4fdd\u8bc1 \u7ed3\u6784\u4f53\u586b\u5145 \u4e0b\u6807 \u8d85\u7c7b \u8bed\u6cd5 \u7c7b\u6a21\u677f\u7279\u5316 \u6a21\u677f\u7c7b\u578b\u53c2\u6570 \u83f1\u5f62\u7ee7\u627f\u95ee\u9898 \u7ffb\u8bd1 \u7b80\u5355\u8f6c\u6362 \u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u522b\u540d \u7c7b\u578b\u8f6c\u6362 \u7c7b\u578b\u5b9a\u4e49 \u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u5b89\u5168 \u7c7b\u578b\u7b7e\u540d typeid \u8fd0\u7b97\u7b26 \u672a\u5b9a\u4e49\u884c\u4e3a \u7edf\u4e00\u521d\u59cb\u5316 \u533f\u540d\u547d\u540d\u7a7a\u95f4 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u5411\u4e0a\u8f6c\u6362 using \u58f0\u660e using \u6307\u4ee4 \u503c\u7c7b\u578b \u503c\u5bb9\u5668 \u503c\u521d\u59cb\u5316 \u503c\u4fdd\u7559 \u865a\u57fa\u7c7b \u865a\u6790\u6784\u51fd\u6570 \u865a\u51fd\u6570 \u865a\u8868 void \u6307\u9488 \u91ce\u6307\u9488 \u5b57\u957f 0\u521d\u59cb\u5316 None \u8bed\u6cd5(syntax) \u8bed\u6cd5 \u8d85\u51fa\u4f5c\u7528\u57df(out of scope) \u8d85\u51fa\u4f5c\u7528\u57df \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316","title":"\u7d22\u5f15"},{"location":"tags/#tags","text":"","title":"Tags"},{"location":"tags/#associations","text":"\u5173\u8054","title":"Associations"},{"location":"tags/#associative-containers","text":"\u5173\u8054\u5bb9\u5668","title":"Associative-containers"},{"location":"tags/#binding","text":"\u7ed1\u5b9a","title":"Binding"},{"location":"tags/#c11","text":"11.13 - for-each \u5faa\u73af 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb 4.11 - \u5b57\u7b26 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 M.7 \u2014 std::shared_ptr","title":"C++11"},{"location":"tags/#c14","text":"12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 4.15 - \u5b57\u9762\u91cf 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad M.6 \u2014 std::unique_ptr M.7 \u2014 std::shared_ptr","title":"C++14"},{"location":"tags/#c17","text":"10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 11.16 \u2014 \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb 11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 4.18 - std::string_view \u7b80\u4ecb 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 7.17 - \u65ad\u8a00\u548c static_assert M.6 \u2014 std::unique_ptr M.7 \u2014 std::shared_ptr","title":"C++17"},{"location":"tags/#c20","text":"10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 11.13 - for-each \u5faa\u73af 11.19 \u2014 \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 6.14 - Constexpr \u548c consteval \u51fd\u6570 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad M.7 \u2014 std::shared_ptr","title":"C++20"},{"location":"tags/#c-style-casts","text":"C \u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362","title":"C-style-casts"},{"location":"tags/#ctad","text":"10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD","title":"CTAD"},{"location":"tags/#early-binding","text":"\u65e9\u671f\u7ed1\u5b9a","title":"Early binding"},{"location":"tags/#encapsulation","text":"\u5c01\u88c5","title":"Encapsulation"},{"location":"tags/#expression-parameters","text":"\u8868\u8fbe\u5f0f\u53c2\u6570","title":"Expression-parameters"},{"location":"tags/#frankenobject","text":"\u7f1d\u5408\u602a\u5bf9\u8c61","title":"Frankenobject"},{"location":"tags/#function-templates","text":"8.13 - \u51fd\u6570\u6a21\u677f","title":"Function templates"},{"location":"tags/#late-binding","text":"\u540e\u671f\u7ed1\u5b9a","title":"Late binding"},{"location":"tags/#lvalue","text":"9.3 - \u5de6\u503c\u5f15\u7528 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528","title":"Lvalue"},{"location":"tags/#multiple-inheritance","text":"\u591a\u91cd\u7ee7\u627f","title":"Multiple inheritance"},{"location":"tags/#name-mangling","text":"\u540d\u79f0\u4fee\u9970","title":"Name-mangling"},{"location":"tags/#numeral-systems","text":"4.16 - \u6570\u503c\u7cfb\u7edf\uff08\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236\uff09","title":"Numeral systems"},{"location":"tags/#off-by-one","text":"\u5dee\u4e00\u9519\u8bef","title":"Off-by-one"},{"location":"tags/#output-manipulators","text":"\u8f93\u51fa\u64cd\u7eb5\u5668","title":"Output-manipulators"},{"location":"tags/#pointer-arithmetic","text":"11.9 - \u6307\u9488\u8fd0\u7b97\u548c\u6570\u7ec4\u7d22\u5f15","title":"Pointer arithmetic"},{"location":"tags/#r-value-references","text":"M.2 - \u53f3\u503c\u5f15\u7528 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c","title":"R-value-references"},{"location":"tags/#raii-resource-acquisition-is-initialization","text":"\u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09","title":"RAII (Resource Acquisition Is Initialization)"},{"location":"tags/#run-time-type-information-rtti","text":"\u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f(RTTI)","title":"Run-time-type-information-RTTI"},{"location":"tags/#sequence-containers","text":"\u987a\u5e8f\u5bb9\u5668","title":"Sequence-containers"},{"location":"tags/#syntax","text":"3.1 - \u8bed\u6cd5\u548c\u8bed\u4e49\u9519\u8bef","title":"Syntax"},{"location":"tags/#template-specialization","text":"\u6a21\u677f\u7279\u5316","title":"Template-specialization"},{"location":"tags/#abbreviated-function-templates","text":"\u7f29\u5199\u51fd\u6570\u6a21\u677f","title":"abbreviated function templates"},{"location":"tags/#abstract-base-classes","text":"18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b","title":"abstract-base-classes"},{"location":"tags/#abstraction","text":"\u62bd\u8c61","title":"abstraction"},{"location":"tags/#access-specifier","text":"13.3 - \u8bbf\u95ee\u4fee\u9970\u7b26 public \u548c private","title":"access specifier"},{"location":"tags/#access-function","text":"\u6210\u5458\u8bbf\u95ee\u51fd\u6570","title":"access-function"},{"location":"tags/#access-specifiers","text":"17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26","title":"access-specifiers"},{"location":"tags/#address","text":"9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de 9.9 - \u6309\u5730\u5740\u4f20\u9012","title":"address"},{"location":"tags/#address-of-operator","text":"\u53d6\u5730\u5740\u64cd\u4f5c\u7b26 &","title":"address-of-operator"},{"location":"tags/#aggregate-initialization","text":"10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316","title":"aggregate initialization"},{"location":"tags/#aggregate-data-type","text":"\u805a\u5408\u6570\u636e\u7c7b\u578b","title":"aggregate-data-type"},{"location":"tags/#aggregate-initialization_1","text":"\u805a\u5408\u521d\u59cb\u5316","title":"aggregate-initialization"},{"location":"tags/#aggregation","text":"16.3 - \u805a\u5408\u5173\u7cfb \u805a\u5408","title":"aggregation"},{"location":"tags/#algorithms","text":"11.19 \u2014 \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb","title":"algorithms"},{"location":"tags/#alias","text":"8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d","title":"alias"},{"location":"tags/#ambiguous-match","text":"\u4e0d\u660e\u786e\u5339\u914d","title":"ambiguous-match"},{"location":"tags/#anonymous-object","text":"\u533f\u540d\u5bf9\u8c61","title":"anonymous-object"},{"location":"tags/#argument","text":"12.5 - \u547d\u4ee4\u884c\u53c2\u6570 2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2","title":"argument"},{"location":"tags/#arguments","text":"\u5b9e\u53c2","title":"arguments"},{"location":"tags/#arithmetic-conversions","text":"8.4 - \u7b97\u672f\u8f6c\u6362","title":"arithmetic conversions"},{"location":"tags/#arity","text":"\u5143\u6570","title":"arity"},{"location":"tags/#array","text":"11.1 - \u6570\u7ec4\uff08\u7b2c\u4e00\u90e8\u5206\uff09 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.3 - \u6570\u7ec4\u548c\u5faa\u73af 11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f 11.5 - \u591a\u7ef4\u6570\u7ec4 11.8 - \u6307\u9488\u548c\u6570\u7ec4","title":"array"},{"location":"tags/#arrays","text":"11.12 - \u52a8\u6001\u5206\u914d\u6570\u7ec4","title":"arrays"},{"location":"tags/#assert","text":"7.17 - \u65ad\u8a00\u548c static_assert","title":"assert"},{"location":"tags/#assignment","text":"14.15 - \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26","title":"assignment"},{"location":"tags/#association","text":"16.4 - \u5173\u8054\u5173\u7cfb","title":"association"},{"location":"tags/#auto","text":"8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad","title":"auto"},{"location":"tags/#automatic-memory-allocation","text":"\u81ea\u52a8\u5185\u5b58\u5206\u914d","title":"automatic-memory-allocation"},{"location":"tags/#automatic-storage-duration","text":"\u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4","title":"automatic-storage-duration"},{"location":"tags/#base-class","text":"\u57fa\u7c7b","title":"base-class"},{"location":"tags/#bit","text":"O.1 - bit\u6807\u8bb0\u548c\u901a\u8fc7std::bitset\u64cd\u4f5cbit O.2 - \u4f4d\u8fd0\u7b97\u7b26 O.3 - \u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u548c\u63a9\u7801\u64cd\u4f5c\u4f4d O.4 - \u4e8c\u8fdb\u5236\u548c\u5341\u8fdb\u5236\u8f6c\u6362 \u4f4d","title":"bit"},{"location":"tags/#bitwise-and","text":"None","title":"bitwise-and"},{"location":"tags/#bitwise-or","text":"\u6309\u4f4d\u6216","title":"bitwise-or"},{"location":"tags/#bool","text":"4.9 - \u5e03\u5c14\u503c","title":"bool"},{"location":"tags/#boolean","text":"4.10 - if \u8bed\u53e5\u7b80\u4ecb","title":"boolean"},{"location":"tags/#bss-segment","text":"bss\u6bb5","title":"bss segment"},{"location":"tags/#byte","text":"\u5b57\u8282","title":"byte"},{"location":"tags/#c-style","text":"11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32","title":"c-style"},{"location":"tags/#call-stack","text":"3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808","title":"call stack"},{"location":"tags/#call-stack_1","text":"\u8c03\u7528\u6808","title":"call-stack"},{"location":"tags/#callable-object","text":"\u53ef\u8c03\u7528\u5bf9\u8c61","title":"callable-object"},{"location":"tags/#callback-functions","text":"\u56de\u8c03\u51fd\u6570","title":"callback functions"},{"location":"tags/#char","text":"4.11 - \u5b57\u7b26","title":"char"},{"location":"tags/#child-class","text":"\u5b50\u7c7b","title":"child-class"},{"location":"tags/#circular-dependency","text":"M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898","title":"circular dependency"},{"location":"tags/#class","text":"13.11 - \u7c7b\u4ee3\u7801\u548c\u5934\u6587\u4ef6 13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570 13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf 13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570 13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b 13.16 - \u533f\u540d\u5bf9\u8c61 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b 13.18 - \u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 13.2 - \u7c7b\u548c\u7c7b\u6210\u5458","title":"class"},{"location":"tags/#class-template","text":"10.10 - \u7c7b\u6a21\u677f 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD","title":"class template"},{"location":"tags/#class-template_1","text":"\u7c7b\u6a21\u677f","title":"class-template"},{"location":"tags/#class-template-argument-deduction","text":"\u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad","title":"class-template-argument-deduction"},{"location":"tags/#class-type","text":"\u7c7b\u7c7b\u578b","title":"class-type"},{"location":"tags/#closure","text":"\u95ed\u5305","title":"closure"},{"location":"tags/#code-coverage","text":"7.13 - \u4ee3\u7801\u8986\u76d6\u7387","title":"code coverage"},{"location":"tags/#code-segment","text":"\u4ee3\u7801\u6bb5","title":"code segment"},{"location":"tags/#comments","text":"1.2 - \u6ce8\u91ca","title":"comments"},{"location":"tags/#compile-time","text":"\u7f16\u8bd1\u65f6","title":"compile-time"},{"location":"tags/#compiler","text":"\u7f16\u8bd1\u5668","title":"compiler"},{"location":"tags/#composite-types","text":"\u7ec4\u5408\u7c7b\u578b","title":"composite-types"},{"location":"tags/#composition","text":"16.2 - \u7ec4\u5408\u5173\u7cfb","title":"composition"},{"location":"tags/#compound-data-types","text":"9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b","title":"compound data types"},{"location":"tags/#const","text":"4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 9.8 - \u6307\u9488\u548cconst","title":"const"},{"location":"tags/#const-class","text":"13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570","title":"const class"},{"location":"tags/#const-cast","text":"const \u7c7b\u578b\u8f6c\u6362","title":"const-cast"},{"location":"tags/#const-member-function","text":"const \u6210\u5458\u51fd\u6570","title":"const-member-function"},{"location":"tags/#const-pointer","text":"\u5e38\u91cf\u6307\u9488","title":"const-pointer"},{"location":"tags/#constant-expression","text":"\u5e38\u91cf\u8868\u8fbe\u5f0f","title":"constant expression"},{"location":"tags/#constants","text":"4.15 - \u5b57\u9762\u91cf","title":"constants"},{"location":"tags/#consteval","text":"6.14 - Constexpr \u548c consteval \u51fd\u6570","title":"consteval"},{"location":"tags/#constexpr","text":"4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr 6.14 - Constexpr \u548c consteval \u51fd\u6570","title":"constexpr"},{"location":"tags/#constructor","text":"13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570 14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570","title":"constructor"},{"location":"tags/#constructors","text":"13.5 - \u6784\u9020\u51fd\u6570 13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868","title":"constructors"},{"location":"tags/#container-classes","text":"16.6 - \u5bb9\u5668\u7c7b","title":"container classes"},{"location":"tags/#container-adapter","text":"\u5bb9\u5668\u9002\u914d\u5668","title":"container-adapter"},{"location":"tags/#container-class","text":"\u5bb9\u5668\u7c7b","title":"container-class"},{"location":"tags/#containers","text":"21.2 - STL\u5bb9\u5668 21.3 - STL\u8fed\u4ee3\u5668 21.4 - STL\u7b97\u6cd5","title":"containers"},{"location":"tags/#control-flow","text":"7.1 - \u63a7\u5236\u6d41 7.4 - switch \u8bed\u53e5\u57fa\u7840","title":"control flow"},{"location":"tags/#conversions","text":"8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 8.3 - \u6570\u503c\u8f6c\u6362 8.4 - \u7b97\u672f\u8f6c\u6362 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast","title":"conversions"},{"location":"tags/#converting-constructors","text":"14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57","title":"converting constructors"},{"location":"tags/#converting-constructors_1","text":"\u8f6c\u6362\u6784\u9020\u51fd\u6570","title":"converting-constructors"},{"location":"tags/#copy-constructor","text":"14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570","title":"copy constructor"},{"location":"tags/#copy-initialization","text":"14.13 - \u62f7\u8d1d\u521d\u59cb\u5316","title":"copy initialization"},{"location":"tags/#copy-constructor_1","text":"\u62f7\u8d1d\u6784\u9020\u51fd\u6570","title":"copy-constructor"},{"location":"tags/#copy-constructors","text":"\u62f7\u8d1d\u6784\u9020\u51fd\u6570","title":"copy-constructors"},{"location":"tags/#copy-initialization_1","text":"\u62f7\u8d1d\u521d\u59cb\u5316","title":"copy-initialization"},{"location":"tags/#copy-semantics","text":"\u62f7\u8d1d\u8bed\u4e49","title":"copy-semantics"},{"location":"tags/#covariant","text":"\u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b","title":"covariant"},{"location":"tags/#covariant-return-types","text":"18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b","title":"covariant-return-types"},{"location":"tags/#dangling","text":"\u60ac\u5782","title":"dangling"},{"location":"tags/#data-segment","text":"\u6570\u636e\u6bb5","title":"data segment"},{"location":"tags/#data-type","text":"4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb 4.4 - \u6709\u7b26\u53f7\u6574\u578b 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 4.8 - \u6d6e\u70b9\u6570 4.9 - \u5e03\u5c14\u503c 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad","title":"data type"},{"location":"tags/#debugger","text":"3.6 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u5355\u6b65\u8c03\u8bd5 3.7 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8fd0\u884c\u548c\u65ad\u70b9 3.8 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u76d1\u63a7\u53d8\u91cf 3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808","title":"debugger"},{"location":"tags/#debugging","text":"3.2 - \u4ee3\u7801\u8c03\u8bd5\u6b65\u9aa4 3.3 - \u4ee3\u7801\u8c03\u8bd5\u7b56\u7565 3.4 \u2014 \u57fa\u672c\u4ee3\u7801\u8c03\u8bd5\u6280\u672f 3.5 - \u66f4\u591a\u4ee3\u7801\u8c03\u8bd5\u6280\u672f","title":"debugging"},{"location":"tags/#declaration","text":"6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e \u58f0\u660e \u7eaf\u58f0\u660e(pure-declaration)","title":"declaration"},{"location":"tags/#deep-copy","text":"14.16 - \u6d45\u62f7\u8d1d\u548c\u6df1\u62f7\u8d1d 14.17 - \u91cd\u8f7d\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u6a21\u677f","title":"deep copy"},{"location":"tags/#deep-copy_1","text":"\u6df1\u62f7\u8d1d","title":"deep-copy"},{"location":"tags/#default","text":"8.12 - \u9ed8\u8ba4\u53c2\u6570","title":"default"},{"location":"tags/#default-constructor","text":"\u9ed8\u8ba4\u6784\u9020\u51fd\u6570","title":"default-constructor"},{"location":"tags/#default-initialization","text":"\u9ed8\u8ba4\u521d\u59cb\u5316","title":"default-initialization"},{"location":"tags/#default-member-initializer","text":"\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c","title":"default-member-initializer"},{"location":"tags/#definition","text":"\u5b9a\u4e49","title":"definition"},{"location":"tags/#delegating","text":"13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570","title":"delegating"},{"location":"tags/#delegating-constructors","text":"\u59d4\u6258\u6784\u9020\u51fd\u6570","title":"delegating-constructors"},{"location":"tags/#delete","text":"11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d 14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57","title":"delete"},{"location":"tags/#dependencies","text":"16.5 - \u4f9d\u8d56\u5173\u7cfb","title":"dependencies"},{"location":"tags/#dependency","text":"\u4f9d\u8d56","title":"dependency"},{"location":"tags/#dereference-operator","text":"\u89e3\u5f15\u7528\u8fd0\u7b97\u7b26","title":"dereference-operator"},{"location":"tags/#derived-class","text":"\u6d3e\u751f\u7c7b","title":"derived-class"},{"location":"tags/#desgin","text":"2.13 - \u5982\u4f55\u8bbe\u8ba1\u7b2c\u4e00\u4e2a\u7a0b\u5e8f","title":"desgin"},{"location":"tags/#designated-initializers","text":"\u6307\u5b9a\u521d\u59cb\u5316","title":"designated-initializers"},{"location":"tags/#destructor","text":"\u6790\u6784\u51fd\u6570","title":"destructor"},{"location":"tags/#destructors","text":"13.9 - \u6790\u6784\u51fd\u6570","title":"destructors"},{"location":"tags/#direct-initialization","text":"\u76f4\u63a5\u521d\u59cb\u5316","title":"direct-initialization"},{"location":"tags/#do-while","text":"7.8 - do-while \u8bed\u53e5","title":"do-while"},{"location":"tags/#downcasting","text":"\u5411\u4e0b\u8f6c\u6362","title":"downcasting"},{"location":"tags/#dumb-pointer","text":"\u7b28\u6307\u9488","title":"dumb-pointer"},{"location":"tags/#dynamic-casting","text":"18.10 - \u52a8\u6001\u7c7b\u578b\u8f6c\u6362","title":"dynamic casting"},{"location":"tags/#dynamic-duration","text":"\u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4","title":"dynamic duration"},{"location":"tags/#dynamic-casts","text":"\u52a8\u6001\u7c7b\u578b\u8f6c\u6362","title":"dynamic-casts"},{"location":"tags/#dynamic-initialization","text":"\u52a8\u6001\u521d\u59cb\u5316","title":"dynamic-initialization"},{"location":"tags/#dynamic-memory","text":"11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d","title":"dynamic-memory"},{"location":"tags/#dynamic-memory-allocation","text":"\u52a8\u6001\u5185\u5b58\u5206\u914d","title":"dynamic-memory-allocation"},{"location":"tags/#early-binding_1","text":"18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a","title":"early-binding"},{"location":"tags/#ellipsis","text":"12.6 - \u7701\u7565\u53f7\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83","title":"ellipsis"},{"location":"tags/#encapsulation_1","text":"13.4 - \u6210\u5458\u8bbf\u95ee\u51fd\u6570\u4e0e\u5c01\u88c5","title":"encapsulation"},{"location":"tags/#enum","text":"10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b 10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09","title":"enum"},{"location":"tags/#enum-class","text":"\u679a\u4e3e\u7c7b","title":"enum-class"},{"location":"tags/#error-handling","text":"7.15 - \u53d1\u73b0\u9519\u8bef\u548c\u5904\u7406\u9519\u8bef 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406","title":"error handling"},{"location":"tags/#evaluation","text":"\u6c42\u503c","title":"evaluation"},{"location":"tags/#excalidraw","text":"Drawing 2023 02 11 13.33.50.excalidraw Template.excalidraw C++ basic onepage Moc onepage","title":"excalidraw"},{"location":"tags/#exception-safety-issue","text":"\u5f02\u5e38\u5b89\u5168\u95ee\u9898","title":"exception-safety-issue"},{"location":"tags/#explicit","text":"14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57","title":"explicit"},{"location":"tags/#explicit-type-conversion","text":"8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362","title":"explicit-type-conversion"},{"location":"tags/#expression","text":"1.10 - \u8868\u8fbe\u5f0f\u7b80\u4ecb","title":"expression"},{"location":"tags/#expression-type","text":"\u8868\u8fbe\u5f0f\u7c7b\u578b","title":"expression type"},{"location":"tags/#external-linkage","text":"\u5916\u90e8\u94fe\u63a5","title":"external-linkage"},{"location":"tags/#external-variable","text":"\u5916\u90e8\u53d8\u91cf","title":"external-variable"},{"location":"tags/#extraction-operator","text":"\u63d0\u53d6\u8fd0\u7b97\u7b26","title":"extraction-operator"},{"location":"tags/#fallthrough","text":"\u8d2f\u7a7f\u5c5e\u6027","title":"fallthrough"},{"location":"tags/#file","text":"23.6 - \u57fa\u672c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa","title":"file"},{"location":"tags/#file-scope","text":"\u6587\u4ef6\u4f5c\u7528\u57df","title":"file-scope"},{"location":"tags/#final","text":"18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b","title":"final"},{"location":"tags/#fixed-array","text":"\u56fa\u5b9a\u6570\u7ec4","title":"fixed-array"},{"location":"tags/#fixed-width","text":"4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t","title":"fixed-width"},{"location":"tags/#float","text":"4.8 - \u6d6e\u70b9\u6570","title":"float"},{"location":"tags/#float-point","text":"5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83","title":"float point"},{"location":"tags/#for","text":"7.7 - \u5faa\u73af\u548c while \u8bed\u53e5 7.9 - for \u8bed\u53e5","title":"for"},{"location":"tags/#for-each","text":"11.13 - for-each \u5faa\u73af","title":"for-each"},{"location":"tags/#fordward-declaration","text":"2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49","title":"fordward declaration"},{"location":"tags/#format-group","text":"\u683c\u5f0f\u7ec4","title":"format-group"},{"location":"tags/#formatting","text":"1.8 - \u7a7a\u767d\u7b26\u548c\u57fa\u672c\u6392\u7248","title":"formatting"},{"location":"tags/#forward-declaration","text":"\u524d\u5411\u58f0\u660e","title":"forward declaration"},{"location":"tags/#friend","text":"13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b 13.16 - \u533f\u540d\u5bf9\u8c61 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b 13.18 - \u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26","title":"friend"},{"location":"tags/#friend-class","text":"\u53cb\u5143\u7c7b","title":"friend-class"},{"location":"tags/#friend-function","text":"\u53cb\u5143\u51fd\u6570","title":"friend-function"},{"location":"tags/#function","text":"12.1 - \u51fd\u6570\u6307\u9488 2.1 - \u51fd\u6570\u7b80\u4ecb 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 2.6 - \u4e3a\u4ec0\u4e48\u51fd\u6570\u6709\u7528\uff0c\u5982\u4f55\u9ad8\u6548\u4f7f\u7528\u51fd\u6570","title":"function"},{"location":"tags/#function-template","text":"8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f","title":"function template"},{"location":"tags/#function-instance","text":"\u51fd\u6570\u5b9e\u4f8b","title":"function-instance"},{"location":"tags/#function-literal","text":"\u51fd\u6570\u5b57\u9762\u91cf","title":"function-literal"},{"location":"tags/#function-pointer","text":"\u51fd\u6570\u6307\u9488","title":"function-pointer"},{"location":"tags/#function-template_1","text":"\u51fd\u6570\u6a21\u677f","title":"function-template"},{"location":"tags/#function-template-instantiation","text":"\u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316","title":"function-template-instantiation"},{"location":"tags/#generic-algorithm","text":"\u6cdb\u578b\u7b97\u6cd5","title":"generic-algorithm"},{"location":"tags/#global","text":"6.4 - \u5168\u5c40\u53d8\u91cf 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf","title":"global"},{"location":"tags/#global-variable","text":"6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c \u5168\u5c40\u53d8\u91cf","title":"global variable"},{"location":"tags/#glossary","text":"\u7f16\u8bd1\u65f6 \u58f0\u660e \u5b9a\u4e49 \u524d\u5411\u58f0\u660e \u5168\u5c40\u53d8\u91cf \u6807\u8bc6\u7b26(identifier) \u5b9e\u4f8b\u5316 \u6307\u4ee4 \u94fe\u63a5\u5668(linker) \u5b8f \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u5f62\u53c2 \u6309\u503c\u4f20\u9012 \u9884\u5904\u7406\u5668\u6307\u4ee4 \u9884\u5904\u7406\u5668 \u7eaf\u58f0\u660e(pure-declaration) \u9650\u5b9a\u7b26 \u91cd\u6784 \u6309\u503c\u8fd4\u56de \u8fd0\u884c\u65f6 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u4f5c\u7528\u57df \u8bed\u53e5 \u7b26\u53f7\u5e38\u91cf","title":"glossary"},{"location":"tags/#goto","text":"7.6 - goto \u8bed\u53e5","title":"goto"},{"location":"tags/#halt","text":"7.11 - \u6302\u8d77\uff08\u63d0\u524d\u7ed3\u675f\u7a0b\u5e8f\uff09","title":"halt"},{"location":"tags/#header","text":"2.11 - \u5934\u6587\u4ef6","title":"header"},{"location":"tags/#header-guards","text":"2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e","title":"header guards"},{"location":"tags/#heap","text":"12.2 - \u6808\u548c\u5806 \u5806","title":"heap"},{"location":"tags/#identifier","text":"1.7 - \u5173\u952e\u5b57\u548c\u6807\u8bc6\u7b26 \u6807\u8bc6\u7b26(identifier)","title":"identifier"},{"location":"tags/#identity","text":"\u8eab\u4efd\u7279\u5f81","title":"identity"},{"location":"tags/#if","text":"4.10 - if \u8bed\u53e5\u7b80\u4ecb 7.2 - if \u8bed\u53e5\u548c\u8bed\u53e5\u5757 7.3 - \u5e38\u89c1\u7684 if \u8bed\u53e5\u9519\u8bef","title":"if"},{"location":"tags/#immediate-functions","text":"\u5373\u65f6\u51fd\u6570","title":"immediate-functions"},{"location":"tags/#implicit-constructor","text":"\u9690\u5f0f\u6784\u9020\u51fd\u6570","title":"implicit-constructor"},{"location":"tags/#implicit-object","text":"\u9690\u5f0f\u5bf9\u8c61","title":"implicit-object"},{"location":"tags/#implicit-type-conversion","text":"\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362","title":"implicit-type-conversion"},{"location":"tags/#inheritance","text":"17.1 - \u7ee7\u627f\u7b80\u4ecb 17.2 - C++\u7ee7\u627f\u57fa\u7840 17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f 17.4 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u548c\u521d\u59cb\u5316 17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 17.6 - \u4e3a\u6d3e\u751f\u7c7b\u6dfb\u52a0\u65b0\u529f\u80fd 17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570 17.9 - \u591a\u91cd\u7ee7\u627f 18.11 - \u4f7f\u7528 << \u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b \u7ee7\u627f","title":"inheritance"},{"location":"tags/#initialization","text":"1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u62f7\u8d1d\u521d\u59cb\u5316 \u76f4\u63a5\u521d\u59cb\u5316 \u5217\u8868\u521d\u59cb\u5316","title":"initialization"},{"location":"tags/#initializer-list","text":"\u521d\u59cb\u5316\u503c\u5217\u8868","title":"initializer-list"},{"location":"tags/#initializer_list","text":"16.7 - std::initializer_list","title":"initializer_list"},{"location":"tags/#inline","text":"6.13 - \u5185\u8054\u51fd\u6570 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf","title":"inline"},{"location":"tags/#inline-namespace","text":"6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4","title":"inline namespace"},{"location":"tags/#inline-function","text":"\u5185\u8054\u51fd\u6570","title":"inline-function"},{"location":"tags/#inline-namespace_1","text":"\u5185\u8054\u547d\u540d\u7a7a\u95f4","title":"inline-namespace"},{"location":"tags/#inline-variables","text":"\u5185\u8054\u53d8\u91cf","title":"inline-variables"},{"location":"tags/#input-manipulator","text":"4.17 - std::string \u7b80\u4ecb","title":"input-manipulator"},{"location":"tags/#input-manipulators","text":"\u8f93\u5165\u64cd\u7eb5\u5668","title":"input-manipulators"},{"location":"tags/#insertion-operator","text":"\u63d2\u5165\u8fd0\u7b97\u7b26","title":"insertion-operator"},{"location":"tags/#instance","text":"\u5b9e\u4f8b","title":"instance"},{"location":"tags/#instantiated","text":"\u5b9e\u4f8b\u5316","title":"instantiated"},{"location":"tags/#instuction","text":"\u6307\u4ee4","title":"instuction"},{"location":"tags/#integer","text":"4.4 - \u6709\u7b26\u53f7\u6574\u578b 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t","title":"integer"},{"location":"tags/#integral-type","text":"\u6574\u578b\u7c7b\u578b","title":"integral-type"},{"location":"tags/#interface-class","text":"\u63a5\u53e3\u7c7b","title":"interface-class"},{"location":"tags/#interface-classes","text":"18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b","title":"interface-classes"},{"location":"tags/#internal-linkage","text":"\u5185\u90e8\u94fe\u63a5","title":"internal-linkage"},{"location":"tags/#internal-variable","text":"\u5185\u90e8\u53d8\u91cf","title":"internal-variable"},{"location":"tags/#io","text":"23.1 - \u8f93\u5165\u8f93\u51fa\u6d41 23.2 - \u4f7f\u7528istream\u5904\u7406\u8f93\u5165 23.3 - \u4f7f\u7528ostream\u548cios\u5904\u7406\u8f93\u51fa 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b 23.5 - \u6d41\u72b6\u6001\u548c\u8f93\u5165\u9a8c\u8bc1 23.6 - \u57fa\u672c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa 23.7 - \u968f\u673a\u6587\u4ef6\u8f93\u5165\u8f93\u51fa","title":"io"},{"location":"tags/#iostream","text":"1.5 - iostream \u7b80\u4ecb\uff1acout\uff0ccin \u548c endl","title":"iostream"},{"location":"tags/#iterating","text":"\u904d\u5386","title":"iterating"},{"location":"tags/#iterator","text":"11.18 \u2014 \u8fed\u4ee3\u5668\u7b80\u4ecb \u8fed\u4ee3\u5668","title":"iterator"},{"location":"tags/#lambda","text":"12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 \u533f\u540d\u51fd\u6570","title":"lambda"},{"location":"tags/#late-binding_1","text":"18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a","title":"late-binding"},{"location":"tags/#link","text":"\u94fe\u63a5","title":"link"},{"location":"tags/#linkage","text":"6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 6.3 - \u5c40\u90e8\u53d8\u91cf 6.6 - \u5185\u90e8\u94fe\u63a5 6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e \u94fe\u63a5\u5c5e\u6027","title":"linkage"},{"location":"tags/#linker","text":"\u94fe\u63a5\u5668(linker)","title":"linker"},{"location":"tags/#list-assignment-operator","text":"\u7c7b\u522b\u8d4b\u503c\u8fd0\u7b97\u7b26","title":"list assignment operator"},{"location":"tags/#list-constructor","text":"\u5217\u8868\u6784\u9020\u51fd\u6570","title":"list-constructor"},{"location":"tags/#list-initialization","text":"\u5217\u8868\u521d\u59cb\u5316","title":"list-initialization"},{"location":"tags/#literal","text":"4.15 - \u5b57\u9762\u91cf","title":"literal"},{"location":"tags/#literals","text":"1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 \u5b57\u9762\u91cf","title":"literals"},{"location":"tags/#local-variable","text":"6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf","title":"local variable"},{"location":"tags/#local-variables","text":"6.3 - \u5c40\u90e8\u53d8\u91cf","title":"local variables"},{"location":"tags/#loop","text":"11.3 - \u6570\u7ec4\u548c\u5faa\u73af","title":"loop"},{"location":"tags/#lvalue_1","text":"\u5de6\u503c","title":"lvalue"},{"location":"tags/#lvalue-references","text":"9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528","title":"lvalue references"},{"location":"tags/#lvalue-reference","text":"\u5de6\u503c\u5f15\u7528","title":"lvalue-reference"},{"location":"tags/#lvalues","text":"9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09","title":"lvalues"},{"location":"tags/#macro","text":"\u5b8f","title":"macro"},{"location":"tags/#member-access-operator","text":"\u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26","title":"member access operator"},{"location":"tags/#member-selection","text":"10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9","title":"member selection"},{"location":"tags/#member-function","text":"\u6210\u5458\u51fd\u6570","title":"member-function"},{"location":"tags/#member-initializer-list","text":"\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868","title":"member-initializer-list"},{"location":"tags/#member-selection-operator","text":"\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26","title":"member-selection-operator"},{"location":"tags/#member-types","text":"\u6210\u5458\u7c7b\u578b","title":"member-types"},{"location":"tags/#member-variable","text":"\u6210\u5458\u53d8\u91cf","title":"member-variable"},{"location":"tags/#members","text":"10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9","title":"members"},{"location":"tags/#memberwise-initialization","text":"\u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316","title":"memberwise initialization"},{"location":"tags/#memberwise-assignment","text":"\u6210\u5458\u4f9d\u6b21\u8d4b\u503c","title":"memberwise-assignment"},{"location":"tags/#memberwise-copy","text":"\u6210\u5458\u4f9d\u6b21\u62f7\u8d1d","title":"memberwise-copy"},{"location":"tags/#memory-leak","text":"\u5185\u5b58\u6cc4\u6f0f","title":"memory-leak"},{"location":"tags/#mixin","text":"None","title":"mixin"},{"location":"tags/#move","text":"M.4 - std::move M.5 - std::move_if_noexcept M.6 \u2014 std::unique_ptr","title":"move"},{"location":"tags/#move-assignment-operator","text":"\u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26","title":"move assignment operator"},{"location":"tags/#move-constructor","text":"\u79fb\u52a8\u6784\u9020\u51fd\u6570","title":"move-constructor"},{"location":"tags/#move-semantics","text":"M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb \u79fb\u52a8\u8bed\u4e49","title":"move-semantics"},{"location":"tags/#multiple-inheritance_1","text":"17.9 - \u591a\u91cd\u7ee7\u627f","title":"multiple-inheritance"},{"location":"tags/#named-cast","text":"\u5177\u540d\u7c7b\u578b\u8f6c\u6362","title":"named-cast"},{"location":"tags/#namespace","text":"2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u547d\u540d\u7a7a\u95f4","title":"namespace"},{"location":"tags/#namespace-aliases","text":"6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26","title":"namespace aliases"},{"location":"tags/#narrowing-convertions","text":"\u7f29\u7a84\u8f6c\u6362","title":"narrowing-convertions"},{"location":"tags/#new","text":"11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d","title":"new"},{"location":"tags/#numeric-conversions","text":"8.3 - \u6570\u503c\u8f6c\u6362","title":"numeric conversions"},{"location":"tags/#numeric-promotion","text":"8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347","title":"numeric promotion"},{"location":"tags/#numeric-promotions","text":"\u6570\u503c\u63d0\u5347","title":"numeric promotions"},{"location":"tags/#numeric-conversions_1","text":"\u6570\u503c\u8f6c\u6362","title":"numeric-conversions"},{"location":"tags/#object","text":"1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf 13.1 - \u6b22\u8fce\u6765\u5230\u9762\u5411\u5bf9\u8c61\u7684\u4e16\u754c 16.1 - \u5bf9\u8c61\u5173\u7cfb 16.2 - \u7ec4\u5408\u5173\u7cfb 16.3 - \u805a\u5408\u5173\u7cfb 16.4 - \u5173\u8054\u5173\u7cfb 16.5 - \u4f9d\u8d56\u5173\u7cfb 16.6 - \u5bb9\u5668\u7c7b 16.7 - std::initializer_list","title":"object"},{"location":"tags/#object-composition","text":"\u5bf9\u8c61\u7ec4\u5408","title":"object-composition"},{"location":"tags/#object-like-macros","text":"\u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f","title":"object-like-macros"},{"location":"tags/#object-slicing","text":"\u5bf9\u8c61\u5207\u7247","title":"object-slicing"},{"location":"tags/#one-definition-rule","text":"\u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule)","title":"one-definition-rule"},{"location":"tags/#operands","text":"\u64cd\u4f5c\u6570","title":"operands"},{"location":"tags/#operator","text":"1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 5.1 - \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 5.3 - \u6c42\u6a21\u548c\u6307\u6570\u8fd0\u7b97 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 5.5 - \u9017\u53f7\u548c\u6761\u4ef6\u8fd0\u7b97\u7b26 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 5.7 - \u903b\u8f91\u8fd0\u7b97\u7b26 \u8fd0\u7b97\u7b26","title":"operator"},{"location":"tags/#operator_1","text":"18.11 - \u4f7f\u7528 << \u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b","title":"operator<<"},{"location":"tags/#optional-parameter","text":"\u53ef\u9009\u53c2\u6570","title":"optional-parameter"},{"location":"tags/#overflow","text":"4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83","title":"overflow"},{"location":"tags/#overlapping","text":"13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570","title":"overlapping"},{"location":"tags/#overload","text":"14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26 14.15 - \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 8.10 - \u91cd\u8f7d\u51fd\u6570\u7684\u533a\u5206 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u4e0d\u660e\u786e\u5339\u914d \u91cd\u8f7d","title":"overload"},{"location":"tags/#overload-resolution","text":"\u91cd\u8f7d\u89e3\u6790","title":"overload-resolution"},{"location":"tags/#overloading","text":"8.9 - \u51fd\u6570\u91cd\u8f7d","title":"overloading"},{"location":"tags/#override","text":"18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b \u91cd\u5199","title":"override"},{"location":"tags/#overriding","text":"17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570","title":"overriding"},{"location":"tags/#overriding-virtualization","text":"18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316","title":"overriding-virtualization"},{"location":"tags/#parameter","text":"2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2","title":"parameter"},{"location":"tags/#parameters","text":"\u5f62\u53c2","title":"parameters"},{"location":"tags/#parent-class","text":"\u7236\u7c7b","title":"parent-class"},{"location":"tags/#parenthesis","text":"14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26","title":"parenthesis"},{"location":"tags/#pass-by-address","text":"\u6309\u5730\u5740\u4f20\u9012","title":"pass-by-address"},{"location":"tags/#pass-by-reference","text":"\u6309\u5f15\u7528\u4f20\u9012","title":"pass-by-reference"},{"location":"tags/#pass-by-value","text":"\u6309\u503c\u4f20\u9012","title":"pass-by-value"},{"location":"tags/#placeholder-types","text":"\u5360\u4f4d\u7b26\u7c7b\u578b","title":"placeholder-types"},{"location":"tags/#pointer","text":"11.15 \u2014 \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u548c\u591a\u7ef4\u6570\u7ec4 11.8 - \u6307\u9488\u548c\u6570\u7ec4 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 9.6 - \u6307\u9488\u7b80\u4ecb 9.7 - \u7a7a\u6307\u9488 9.8 - \u6307\u9488\u548cconst 9.9 - \u6309\u5730\u5740\u4f20\u9012","title":"pointer"},{"location":"tags/#pointer-to-const","text":"\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488","title":"pointer-to-const"},{"location":"tags/#pointers","text":"18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528","title":"pointers"},{"location":"tags/#polymorphism","text":"18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001 \u591a\u6001","title":"polymorphism"},{"location":"tags/#pragma-once","text":"2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e","title":"pragma once"},{"location":"tags/#preprocessor","text":"2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb \u9884\u5904\u7406\u5668\u6307\u4ee4 \u9884\u5904\u7406\u5668","title":"preprocessor"},{"location":"tags/#private-inheritance","text":"\u79c1\u6709\u7ee7\u627f","title":"private-inheritance"},{"location":"tags/#private-member","text":"\u79c1\u6709\u6210\u5458","title":"private-member"},{"location":"tags/#program-organization","text":"2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f","title":"program organization"},{"location":"tags/#protected-inheritance","text":"\u53d7\u4fdd\u62a4\u7ee7\u627f","title":"protected-inheritance"},{"location":"tags/#protected-members","text":"\u53d7\u4fdd\u62a4\u6210\u5458","title":"protected-members"},{"location":"tags/#public-inheritance","text":"\u516c\u5f00\u7ee7\u627f","title":"public-inheritance"},{"location":"tags/#public-member","text":"\u516c\u6709\u6210\u5458","title":"public-member"},{"location":"tags/#pure-declaration","text":"\u7eaf\u58f0\u660e(pure-declaration)","title":"pure-declaration"},{"location":"tags/#pure-virtual","text":"\u7eaf\u865a\u51fd\u6570","title":"pure-virtual"},{"location":"tags/#pure-virtual-functions","text":"18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b","title":"pure-virtual-functions"},{"location":"tags/#qualifier","text":"\u9650\u5b9a\u7b26","title":"qualifier"},{"location":"tags/#random-access-memory","text":"\u968f\u673a\u8bbf\u95ee\u5185\u5b58","title":"random-access-memory"},{"location":"tags/#range-based-for-loops","text":"\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af","title":"range-based-for-loops"},{"location":"tags/#raw-pointer","text":"\u539f\u59cb\u6307\u9488","title":"raw-pointer"},{"location":"tags/#recursion","text":"12.4 - \u9012\u5f52","title":"recursion"},{"location":"tags/#refactoring","text":"\u91cd\u6784","title":"refactoring"},{"location":"tags/#reference","text":"9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de","title":"reference"},{"location":"tags/#reference-container","text":"\u5f15\u7528\u5bb9\u5668","title":"reference-container"},{"location":"tags/#references","text":"18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 M.2 - \u53f3\u503c\u5f15\u7528 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c","title":"references"},{"location":"tags/#reflexive-association","text":"\u53cd\u8eab\u5173\u8054","title":"reflexive association"},{"location":"tags/#reinterpret-casts","text":"\u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362","title":"reinterpret-casts"},{"location":"tags/#return","text":"2.2 - \u51fd\u6570\u8fd4\u56de\u503c 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de","title":"return"},{"location":"tags/#return-by-address","text":"\u6309\u5730\u5740\u8fd4\u56de","title":"return-by-address"},{"location":"tags/#return-by-reference","text":"\u6309\u5f15\u7528\u8fd4\u56de","title":"return-by-reference"},{"location":"tags/#return-by-value","text":"\u6309\u503c\u8fd4\u56de","title":"return-by-value"},{"location":"tags/#return-type-deduction","text":"\u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad","title":"return-type-deduction"},{"location":"tags/#rounding-error","text":"\u820d\u5165\u8bef\u5dee","title":"rounding-error"},{"location":"tags/#runtime","text":"\u8fd0\u884c\u65f6","title":"runtime"},{"location":"tags/#rvalue","text":"\u53f3\u503c","title":"rvalue"},{"location":"tags/#rvalue-reference","text":"\u53f3\u503c\u5f15\u7528","title":"rvalue-reference"},{"location":"tags/#rvalues","text":"9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09","title":"rvalues"},{"location":"tags/#scaling","text":"\u6307\u9488\u7f29\u653e","title":"scaling"},{"location":"tags/#scientific-notation","text":"4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5","title":"scientific notation"},{"location":"tags/#scope","text":"2.5 - \u5c40\u90e8\u4f5c\u7528\u57df 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 \u4f5c\u7528\u57df","title":"scope"},{"location":"tags/#scope-resolution-operator","text":"6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26","title":"scope resolution operator"},{"location":"tags/#scope-resolution-operator_1","text":"\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26","title":"scope-resolution-operator"},{"location":"tags/#scoped-enumerations","text":"10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09","title":"scoped enumerations"},{"location":"tags/#scoped-enumerations_1","text":"\u6709\u4f5c\u7528\u57df\u679a\u4e3e","title":"scoped-enumerations"},{"location":"tags/#segment","text":"\u5185\u5b58\u6bb5","title":"segment"},{"location":"tags/#sentinel","text":"\u54e8\u5175","title":"sentinel"},{"location":"tags/#setup","text":"0.8 - \u4e00\u4e9b\u5178\u578b\u7684 C++ \u95ee\u9898","title":"setup"},{"location":"tags/#shadow","text":"\u53d8\u91cf\u906e\u853d","title":"shadow"},{"location":"tags/#shallow-copy","text":"14.16 - \u6d45\u62f7\u8d1d\u548c\u6df1\u62f7\u8d1d 14.17 - \u91cd\u8f7d\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u6a21\u677f","title":"shallow copy"},{"location":"tags/#shallow-copy_1","text":"\u6d45\u62f7\u8d1d","title":"shallow-copy"},{"location":"tags/#shared_ptr","text":"M.7 \u2014 std::shared_ptr M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898","title":"shared_ptr"},{"location":"tags/#side-effects","text":"\u526f\u4f5c\u7528","title":"side-effects"},{"location":"tags/#signature","text":"\u7b7e\u540d","title":"signature"},{"location":"tags/#size_t","text":"4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t","title":"size_t"},{"location":"tags/#sizeof","text":"4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26","title":"sizeof"},{"location":"tags/#smart-pointer-class","text":"\u667a\u80fd\u6307\u9488\u7c7b","title":"smart pointer class"},{"location":"tags/#smart-pointer","text":"\u667a\u80fd\u6307\u9488","title":"smart-pointer"},{"location":"tags/#smart-pointers","text":"M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb","title":"smart-pointers"},{"location":"tags/#sort","text":"11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f","title":"sort"},{"location":"tags/#stack","text":"12.2 - \u6808\u548c\u5806 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a \u6808","title":"stack"},{"location":"tags/#stack-frame","text":"\u6808\u5e27","title":"stack-frame"},{"location":"tags/#stack-overflow","text":"\u5806\u6808\u6ea2\u51fa","title":"stack-overflow"},{"location":"tags/#standard-stream","text":"\u6807\u51c6\u6d41","title":"standard-stream"},{"location":"tags/#statement","text":"6.1 - \u590d\u5408\u8bed\u53e5\uff08\u8bed\u53e5\u5757\uff09 \u8bed\u53e5","title":"statement"},{"location":"tags/#statements","text":"1.1 - \u7a0b\u5e8f\u7ed3\u6784\u548c\u8bed\u53e5","title":"statements"},{"location":"tags/#static","text":"6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf","title":"static"},{"location":"tags/#static-membe-function","text":"13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570","title":"static membe function"},{"location":"tags/#static-member","text":"13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316","title":"static member"},{"location":"tags/#static-member-variable","text":"13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf","title":"static member variable"},{"location":"tags/#static-cast","text":"8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast","title":"static-cast"},{"location":"tags/#static-casts","text":"\u9759\u6001\u7c7b\u578b\u8f6c\u6362","title":"static-casts"},{"location":"tags/#static-initialization","text":"\u9759\u6001\u521d\u59cb\u5316","title":"static-initialization"},{"location":"tags/#static-member-functions","text":"\u9759\u6001\u6210\u5458\u51fd\u6570","title":"static-member-functions"},{"location":"tags/#static-member-variables","text":"\u9759\u6001\u6210\u5458\u53d8\u91cf","title":"static-member-variables"},{"location":"tags/#static-memory-allocation","text":"\u9759\u6001\u5185\u5b58\u5206\u914d","title":"static-memory-allocation"},{"location":"tags/#static-storage-duration","text":"\u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4","title":"static-storage-duration"},{"location":"tags/#static-variables","text":"\u9759\u6001\u53d8\u91cf","title":"static-variables"},{"location":"tags/#static_assert","text":"7.17 - \u65ad\u8a00\u548c static_assert","title":"static_assert"},{"location":"tags/#static_cast","text":"4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast 8.3 - \u6570\u503c\u8f6c\u6362","title":"static_cast"},{"location":"tags/#stdarray","text":"11.16 \u2014 \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb","title":"std::array"},{"location":"tags/#stdstring_view","text":"11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09","title":"std::string_view"},{"location":"tags/#stl","text":"21.1 - \u6807\u51c6\u5e93 21.2 - STL\u5bb9\u5668 21.3 - STL\u8fed\u4ee3\u5668 21.4 - STL\u7b97\u6cd5","title":"stl"},{"location":"tags/#storage-duration","text":"6.3 - \u5c40\u90e8\u53d8\u91cf 6.4 - \u5168\u5c40\u53d8\u91cf","title":"storage duration"},{"location":"tags/#storage-duration_1","text":"\u5b58\u50a8\u6301\u7eed\u65f6\u95f4","title":"storage-duration"},{"location":"tags/#string","text":"11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 22.1 - std::string \u548c std::wstring 22.2 - std-string \u7684\u6784\u9020\u548c\u6790\u6784 22.3 - std-string \u7684\u957f\u5ea6\u548c\u5bb9\u91cf 22.4 - std-string \u5b57\u7b26\u8bbf\u95ee\u548cC\u5b57\u7b26\u6570\u7ec4\u8f6c\u6362 22.5 - std-string \u8d4b\u503c\u548c\u4ea4\u6362 22.6 - std-string \u8ffd\u52a0 22.7 - std-string \u63d2\u5165 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b 4.17 - std::string \u7b80\u4ecb","title":"string"},{"location":"tags/#string_view","text":"4.18 - std::string_view \u7b80\u4ecb","title":"string_view"},{"location":"tags/#strong-except","text":"\u5f3a\u5f02\u5e38\u4fdd\u8bc1","title":"strong-except"},{"location":"tags/#struct","text":"10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 10.8 - \u7ed3\u6784\u4f53\u4f20\u9012\u53ca\u5176\u4ed6 10.9 - \u57fa\u4e8e\u6307\u9488\u548c\u5f15\u7528\u7684\u6210\u5458\u9009\u62e9","title":"struct"},{"location":"tags/#struct-padding","text":"\u7ed3\u6784\u4f53\u586b\u5145","title":"struct-padding"},{"location":"tags/#subscript","text":"14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26","title":"subscript"},{"location":"tags/#subscripts","text":"\u4e0b\u6807","title":"subscripts"},{"location":"tags/#summary","text":"1.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - C++\u57fa\u7840 10.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u679a\u4e3e\u548c\u7ed3\u6784\u4f53 10.y - \u5b66\u4f1a\u4f7f\u7528C++\u53c2\u8003\u624b\u518c 11.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5206\u914d 12.8 - lambda \u95ed\u5305 12.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570 13.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u9762\u5411\u5bf9\u8c61\u57fa\u7840 14.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26\u91cd\u8f7d 16.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5bf9\u8c61\u5173\u7cfb 17.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7ee7\u627f 18.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u865a\u51fd\u6570 19.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6a21\u677f\u548c\u7c7b 2.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570\u4e0e\u6587\u4ef6 20.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f02\u5e38 3.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4ee3\u7801\u8c03\u8bd5 4.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u57fa\u672c\u6570\u636e\u7c7b\u578b 5.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 7.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406 8.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d 9.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f15\u7528\u548c\u6307\u9488 M.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u79fb\u52a8\u548c\u667a\u80fd\u6307\u9488","title":"summary"},{"location":"tags/#super-class","text":"\u8d85\u7c7b","title":"super-class"},{"location":"tags/#switch","text":"7.4 - switch \u8bed\u53e5\u57fa\u7840 7.5 - switch fallthrough\u5c5e\u6027\u548c\u4f5c\u7528\u57df","title":"switch"},{"location":"tags/#symbolic-constants","text":"11.10 - C \u98ce\u683c\u5b57\u7b26\u4e32\u7b26\u53f7\u5e38\u91cf","title":"symbolic constants"},{"location":"tags/#symbolic-constants_1","text":"\u7b26\u53f7\u5e38\u91cf","title":"symbolic-constants"},{"location":"tags/#template-class-specialization","text":"\u7c7b\u6a21\u677f\u7279\u5316","title":"template class specialization"},{"location":"tags/#testing","text":"7.12 - \u4ee3\u7801\u6d4b\u8bd5","title":"testing"},{"location":"tags/#the-diamond-problem","text":"\u83f1\u5f62\u7ee7\u627f\u95ee\u9898","title":"the-diamond-problem"},{"location":"tags/#this","text":"13.10 - \u9690\u85cf\u7684this\u6307\u9488","title":"this"},{"location":"tags/#translation","text":"\u7ffb\u8bd1","title":"translation"},{"location":"tags/#type","text":"10.1 - \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7b80\u4ecb","title":"type"},{"location":"tags/#type-conversion","text":"4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362","title":"type conversion"},{"location":"tags/#type-deduction","text":"8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u63a8\u65ad","title":"type deduction"},{"location":"tags/#type-aliases","text":"\u7c7b\u578b\u522b\u540d","title":"type-aliases"},{"location":"tags/#type-conversion_1","text":"\u7c7b\u578b\u8f6c\u6362","title":"type-conversion"},{"location":"tags/#type-definition","text":"\u7c7b\u578b\u5b9a\u4e49","title":"type-definition"},{"location":"tags/#type-inference","text":"\u7c7b\u578b\u63a8\u65ad","title":"type-inference"},{"location":"tags/#type-safe","text":"\u7c7b\u578b\u5b89\u5168","title":"type-safe"},{"location":"tags/#type-signature","text":"\u7c7b\u578b\u7b7e\u540d","title":"type-signature"},{"location":"tags/#typecast","text":"14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26","title":"typecast"},{"location":"tags/#typedef","text":"8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d","title":"typedef"},{"location":"tags/#typeid","text":"typeid \u8fd0\u7b97\u7b26","title":"typeid"},{"location":"tags/#typeinfo","text":"8.4 - \u7b97\u672f\u8f6c\u6362","title":"typeinfo"},{"location":"tags/#unary","text":"14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26","title":"unary"},{"location":"tags/#undefined-behavior","text":"1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 4.4 - \u6709\u7b26\u53f7\u6574\u578b","title":"undefined behavior"},{"location":"tags/#undefined-behavior_1","text":"\u672a\u5b9a\u4e49\u884c\u4e3a","title":"undefined-behavior"},{"location":"tags/#uniform-initialization","text":"\u7edf\u4e00\u521d\u59cb\u5316","title":"uniform initialization"},{"location":"tags/#unmanned-namespace","text":"6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4","title":"unmanned namespace"},{"location":"tags/#unnamed-namespace","text":"\u533f\u540d\u547d\u540d\u7a7a\u95f4","title":"unnamed-namespace"},{"location":"tags/#unscoped-enumerations","text":"10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b 10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa","title":"unscoped enumerations"},{"location":"tags/#unscoped-enumerations_1","text":"\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b","title":"unscoped-enumerations"},{"location":"tags/#upcasting","text":"\u5411\u4e0a\u8f6c\u6362","title":"upcasting"},{"location":"tags/#using","text":"6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4","title":"using"},{"location":"tags/#using-declaration","text":"using \u58f0\u660e","title":"using-declaration"},{"location":"tags/#using-directive","text":"using \u6307\u4ee4","title":"using-directive"},{"location":"tags/#value-category","text":"\u503c\u7c7b\u578b","title":"value-category"},{"location":"tags/#value-container","text":"\u503c\u5bb9\u5668","title":"value-container"},{"location":"tags/#value-initialization","text":"\u503c\u521d\u59cb\u5316","title":"value-initialization"},{"location":"tags/#value-preserving","text":"\u503c\u4fdd\u7559","title":"value-preserving"},{"location":"tags/#variable","text":"1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316","title":"variable"},{"location":"tags/#variable-shadowing","text":"6.5 - \u53d8\u91cf\u906e\u853d","title":"variable shadowing"},{"location":"tags/#vector","text":"11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a","title":"vector"},{"location":"tags/#virtual-functions","text":"18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001","title":"virtual functions"},{"location":"tags/#virtual-assignment","text":"18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316","title":"virtual-assignment"},{"location":"tags/#virtual-base-class","text":"\u865a\u57fa\u7c7b","title":"virtual-base-class"},{"location":"tags/#virtual-base-classes","text":"18.8 - \u865a\u57fa\u7c7b 18.9 - \u5bf9\u8c61\u5207\u7247","title":"virtual-base-classes"},{"location":"tags/#virtual-destructor","text":"\u865a\u6790\u6784\u51fd\u6570","title":"virtual-destructor"},{"location":"tags/#virtual-destructors","text":"18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316","title":"virtual-destructors"},{"location":"tags/#virtual-function","text":"\u865a\u51fd\u6570","title":"virtual-function"},{"location":"tags/#virtual-table","text":"18.6 - \u865a\u51fd\u6570\u8868 \u865a\u8868","title":"virtual-table"},{"location":"tags/#void","text":"11.14 \u2014 void \u6307\u9488 2.3 - void \u51fd\u6570 4.2 - Void","title":"void"},{"location":"tags/#void-pointer","text":"void \u6307\u9488","title":"void pointer"},{"location":"tags/#weak_ptr","text":"M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898","title":"weak_ptr"},{"location":"tags/#while","text":"7.7 - \u5faa\u73af\u548c while \u8bed\u53e5","title":"while"},{"location":"tags/#wild-pointer","text":"\u91ce\u6307\u9488","title":"wild-pointer"},{"location":"tags/#word","text":"\u5b57\u957f","title":"word"},{"location":"tags/#wstring","text":"22.1 - std::string \u548c std::wstring","title":"wstring"},{"location":"tags/#zero-initialization","text":"0\u521d\u59cb\u5316","title":"zero-initialization"},{"location":"tags/#function-prototype","text":"\u51fd\u6570\u539f\u578b","title":"\u51fd\u6570\u539f\u578b(function prototype)"},{"location":"tags/#_1","text":"\u521d\u59cb\u5316","title":"\u521d\u59cb\u5316"},{"location":"tags/#initializer","text":"\u521d\u59cb\u5316\u503c","title":"\u521d\u59cb\u5316\u503c(initializer)"},{"location":"tags/#_2","text":"\u52a8\u6001\u6570\u7ec4","title":"\u52a8\u6001\u6570\u7ec4"},{"location":"tags/#objec","text":"\u5bf9\u8c61","title":"\u5bf9\u8c61(objec)"},{"location":"tags/#_3","text":"\u62bd\u8c61\u7c7b","title":"\u62bd\u8c61\u7c7b"},{"location":"tags/#_4","text":"None","title":"\u62ec\u53f7\u521d\u59cb\u5316"},{"location":"tags/#_5","text":"\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26","title":"\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26"},{"location":"tags/#_6","text":"\u6784\u9020\u51fd\u6570","title":"\u6784\u9020\u51fd\u6570"},{"location":"tags/#_7","text":"\u6a21\u677f\u7c7b\u578b\u53c2\u6570","title":"\u6a21\u677f\u7c7b\u578b\u53c2\u6570"},{"location":"tags/#_8","text":"\u79bb\u5f00\u4f5c\u7528\u57df","title":"\u79bb\u5f00\u4f5c\u7528\u57df"},{"location":"tags/#trivial-conversion","text":"\u7b80\u5355\u8f6c\u6362","title":"\u7b80\u5355\u8f6c\u6362(trivial conversion)"},{"location":"tags/#_9","text":"\u7c7b\u5b9e\u4f8b","title":"\u7c7b\u5b9e\u4f8b"},{"location":"tags/#snake_case","text":"\u86c7\u5f62\u547d\u540d\u6cd5","title":"\u86c7\u5f62\u547d\u540d\u6cd5(snake_case)"},{"location":"tags/#_10","text":"\u5173\u8054 \u5173\u8054\u5bb9\u5668 \u7ed1\u5b9a C \u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362 \u65e9\u671f\u7ed1\u5b9a \u5c01\u88c5 \u8868\u8fbe\u5f0f\u53c2\u6570 \u7f1d\u5408\u602a\u5bf9\u8c61 \u540e\u671f\u7ed1\u5b9a \u591a\u91cd\u7ee7\u627f \u540d\u79f0\u4fee\u9970 \u5dee\u4e00\u9519\u8bef \u8f93\u51fa\u64cd\u7eb5\u5668 \u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09 \u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f(RTTI) \u987a\u5e8f\u5bb9\u5668 \u6a21\u677f\u7279\u5316 \u7f29\u5199\u51fd\u6570\u6a21\u677f \u62bd\u8c61\u7c7b \u62bd\u8c61 \u6210\u5458\u8bbf\u95ee\u51fd\u6570 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u53d6\u5730\u5740\u64cd\u4f5c\u7b26 & \u805a\u5408\u6570\u636e\u7c7b\u578b \u805a\u5408\u521d\u59cb\u5316 \u805a\u5408 \u4e0d\u660e\u786e\u5339\u914d \u533f\u540d\u5bf9\u8c61 \u5b9e\u53c2 \u5143\u6570 \u81ea\u52a8\u5185\u5b58\u5206\u914d \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u57fa\u7c7b \u4f4d None \u6309\u4f4d\u6216 bss\u6bb5 \u5b57\u8282 \u8c03\u7528\u6808 \u53ef\u8c03\u7528\u5bf9\u8c61 \u56de\u8c03\u51fd\u6570 \u5b50\u7c7b \u7c7b\u5b9e\u4f8b \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad \u7c7b\u6a21\u677f \u7c7b\u7c7b\u578b \u95ed\u5305 \u4ee3\u7801\u6bb5 \u7f16\u8bd1\u5668 \u7ec4\u5408\u7c7b\u578b const \u7c7b\u578b\u8f6c\u6362 const \u6210\u5458\u51fd\u6570 \u5e38\u91cf\u6307\u9488 \u5e38\u91cf\u8868\u8fbe\u5f0f \u6784\u9020\u51fd\u6570 \u5bb9\u5668\u9002\u914d\u5668 \u5bb9\u5668\u7c7b \u8f6c\u6362\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u521d\u59cb\u5316 \u62f7\u8d1d\u8bed\u4e49 \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b \u60ac\u5782 \u6570\u636e\u6bb5 \u6df1\u62f7\u8d1d \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 \u9ed8\u8ba4\u521d\u59cb\u5316 \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c \u59d4\u6258\u6784\u9020\u51fd\u6570 \u4f9d\u8d56 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u6d3e\u751f\u7c7b \u6307\u5b9a\u521d\u59cb\u5316 \u6790\u6784\u51fd\u6570 \u76f4\u63a5\u521d\u59cb\u5316 \u5411\u4e0b\u8f6c\u6362 \u7b28\u6307\u9488 \u52a8\u6001\u6570\u7ec4 \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u52a8\u6001\u521d\u59cb\u5316 \u52a8\u6001\u5185\u5b58\u5206\u914d \u679a\u4e3e\u7c7b \u6c42\u503c \u5f02\u5e38\u5b89\u5168\u95ee\u9898 \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u8868\u8fbe\u5f0f\u7c7b\u578b \u5916\u90e8\u94fe\u63a5 \u5916\u90e8\u53d8\u91cf \u63d0\u53d6\u8fd0\u7b97\u7b26 \u8d2f\u7a7f\u5c5e\u6027 \u6587\u4ef6\u4f5c\u7528\u57df \u56fa\u5b9a\u6570\u7ec4 \u683c\u5f0f\u7ec4 \u53cb\u5143\u7c7b \u53cb\u5143\u51fd\u6570 \u51fd\u6570\u5b9e\u4f8b \u51fd\u6570\u5b57\u9762\u91cf \u51fd\u6570\u6307\u9488 \u51fd\u6570\u539f\u578b \u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316 \u51fd\u6570\u6a21\u677f \u6cdb\u578b\u7b97\u6cd5 \u79bb\u5f00\u4f5c\u7528\u57df \u5806 \u8eab\u4efd\u7279\u5f81 \u5373\u65f6\u51fd\u6570 \u9690\u5f0f\u6784\u9020\u51fd\u6570 \u9690\u5f0f\u5bf9\u8c61 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u7ee7\u627f \u521d\u59cb\u5316 \u521d\u59cb\u5316\u503c\u5217\u8868 \u521d\u59cb\u5316\u503c \u5185\u8054\u51fd\u6570 \u5185\u8054\u547d\u540d\u7a7a\u95f4 \u5185\u8054\u53d8\u91cf \u8f93\u5165\u64cd\u7eb5\u5668 \u63d2\u5165\u8fd0\u7b97\u7b26 \u5b9e\u4f8b \u6574\u578b\u7c7b\u578b \u63a5\u53e3\u7c7b \u5185\u90e8\u94fe\u63a5 \u5185\u90e8\u53d8\u91cf \u904d\u5386 \u8fed\u4ee3\u5668 \u533f\u540d\u51fd\u6570 \u94fe\u63a5 \u94fe\u63a5\u5c5e\u6027 \u7c7b\u522b\u8d4b\u503c\u8fd0\u7b97\u7b26 \u5217\u8868\u6784\u9020\u51fd\u6570 \u5217\u8868\u521d\u59cb\u5316 \u5b57\u9762\u91cf \u5de6\u503c\u5f15\u7528 \u5de6\u503c \u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26 \u6210\u5458\u51fd\u6570 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 \u6210\u5458\u7c7b\u578b \u6210\u5458\u53d8\u91cf \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \u6210\u5458\u4f9d\u6b21\u8d4b\u503c \u6210\u5458\u4f9d\u6b21\u62f7\u8d1d \u5185\u5b58\u6cc4\u6f0f None \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 \u79fb\u52a8\u6784\u9020\u51fd\u6570 \u79fb\u52a8\u8bed\u4e49 \u5177\u540d\u7c7b\u578b\u8f6c\u6362 \u547d\u540d\u7a7a\u95f4 \u7f29\u7a84\u8f6c\u6362 \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u6570\u503c\u63d0\u5347 \u6570\u503c\u8f6c\u6362 \u5bf9\u8c61\u7ec4\u5408 \u5bf9\u8c61\u5207\u7247 \u5bf9\u8c61 \u64cd\u4f5c\u6570 \u8fd0\u7b97\u7b26 \u8d85\u51fa\u4f5c\u7528\u57df \u91cd\u8f7d\u89e3\u6790 \u91cd\u8f7d \u91cd\u5199 \u7236\u7c7b \u6309\u5730\u5740\u4f20\u9012 \u6309\u5f15\u7528\u4f20\u9012 \u5360\u4f4d\u7b26\u7c7b\u578b \u6307\u5411\u5e38\u91cf\u7684\u6307\u9488 \u591a\u6001 \u79c1\u6709\u7ee7\u627f \u79c1\u6709\u6210\u5458 \u53d7\u4fdd\u62a4\u7ee7\u627f \u53d7\u4fdd\u62a4\u6210\u5458 \u516c\u5f00\u7ee7\u627f \u516c\u6709\u6210\u5458 \u7eaf\u865a\u51fd\u6570 \u968f\u673a\u8bbf\u95ee\u5185\u5b58 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u539f\u59cb\u6307\u9488 \u5f15\u7528\u5bb9\u5668 \u53cd\u8eab\u5173\u8054 \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 \u6309\u5730\u5740\u8fd4\u56de \u6309\u5f15\u7528\u8fd4\u56de \u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad \u820d\u5165\u8bef\u5dee \u53f3\u503c\u5f15\u7528 \u53f3\u503c \u6307\u9488\u7f29\u653e \u6709\u4f5c\u7528\u57df\u679a\u4e3e \u5185\u5b58\u6bb5 \u54e8\u5175 \u53d8\u91cf\u906e\u853d \u6d45\u62f7\u8d1d \u526f\u4f5c\u7528 \u7b7e\u540d \u667a\u80fd\u6307\u9488\u7c7b \u667a\u80fd\u6307\u9488 \u86c7\u5f62\u547d\u540d\u6cd5 \u6808\u5e27 \u5806\u6808\u6ea2\u51fa \u6808 \u6807\u51c6\u6d41 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u9759\u6001\u521d\u59cb\u5316 \u9759\u6001\u6210\u5458\u51fd\u6570 \u9759\u6001\u6210\u5458\u53d8\u91cf \u9759\u6001\u5185\u5b58\u5206\u914d \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u9759\u6001\u53d8\u91cf \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u5f3a\u5f02\u5e38\u4fdd\u8bc1 \u7ed3\u6784\u4f53\u586b\u5145 \u4e0b\u6807 \u8d85\u7c7b \u8bed\u6cd5 \u7c7b\u6a21\u677f\u7279\u5316 \u6a21\u677f\u7c7b\u578b\u53c2\u6570 \u83f1\u5f62\u7ee7\u627f\u95ee\u9898 \u7ffb\u8bd1 \u7b80\u5355\u8f6c\u6362 \u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u522b\u540d \u7c7b\u578b\u8f6c\u6362 \u7c7b\u578b\u5b9a\u4e49 \u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u5b89\u5168 \u7c7b\u578b\u7b7e\u540d typeid \u8fd0\u7b97\u7b26 \u672a\u5b9a\u4e49\u884c\u4e3a \u7edf\u4e00\u521d\u59cb\u5316 \u533f\u540d\u547d\u540d\u7a7a\u95f4 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u5411\u4e0a\u8f6c\u6362 using \u58f0\u660e using \u6307\u4ee4 \u503c\u7c7b\u578b \u503c\u5bb9\u5668 \u503c\u521d\u59cb\u5316 \u503c\u4fdd\u7559 \u865a\u57fa\u7c7b \u865a\u6790\u6784\u51fd\u6570 \u865a\u51fd\u6570 \u865a\u8868 void \u6307\u9488 \u91ce\u6307\u9488 \u5b57\u957f 0\u521d\u59cb\u5316 None","title":"\u8bcd\u6c47\u8868"},{"location":"tags/#syntax_1","text":"\u8bed\u6cd5","title":"\u8bed\u6cd5(syntax)"},{"location":"tags/#out-of-scope","text":"\u8d85\u51fa\u4f5c\u7528\u57df","title":"\u8d85\u51fa\u4f5c\u7528\u57df(out of scope)"},{"location":"tags/#_11","text":"\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316","title":"\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316"},{"location":"template-class-specialization/","tags":["\u8bcd\u6c47\u8868","template class specialization"],"text":"","title":"\u7c7b\u6a21\u677f\u7279\u5316"},{"location":"template-type-parameters/","tags":["\u8bcd\u6c47\u8868","\u6a21\u677f\u7c7b\u578b\u53c2\u6570"],"text":"","title":"\u6a21\u677f\u7c7b\u578b\u53c2\u6570"},{"location":"the-diamond-problem/","tags":["\u8bcd\u6c47\u8868","the-diamond-problem"],"text":"","title":"\u83f1\u5f62\u7ee7\u627f\u95ee\u9898"},{"location":"transitive%20includes/","text":"title: \u95f4\u63a5\u5305\u542b alias: \u95f4\u63a5\u5305\u542b english:transitive includes type: glossary tags: - glossary - transitive includes","title":"Transitive includes"},{"location":"translation/","tags":["\u8bcd\u6c47\u8868","translation"],"text":"\u7ffb\u8bd1","title":"\u7ffb\u8bd1"},{"location":"trivial-conversion/","tags":["\u8bcd\u6c47\u8868","\u7b80\u5355\u8f6c\u6362(trivial conversion)"],"text":"","title":"\u7b80\u5355\u8f6c\u6362"},{"location":"type%20deduction/","tags":["\u8bcd\u6c47\u8868","type deduction"],"text":"","title":"\u7c7b\u578b\u63a8\u65ad"},{"location":"type-aliases/","tags":["\u8bcd\u6c47\u8868","type-aliases"],"text":"using distance_t = double; // distance_t \u4f5c\u4e3a double \u7684\u522b\u540d \u53ef\u7528\u4e8e\u7b80\u5316 \u51fd\u6570\u6307\u9488 \u8bed\u6cd5","title":"\u7c7b\u578b\u522b\u540d"},{"location":"type-conversion/","tags":["\u8bcd\u6c47\u8868","type-conversion"],"text":"","title":"\u7c7b\u578b\u8f6c\u6362"},{"location":"type-definition/","tags":["\u8bcd\u6c47\u8868","type-definition"],"text":"","title":"\u7c7b\u578b\u5b9a\u4e49"},{"location":"type-inference/","tags":["\u8bcd\u6c47\u8868","type-inference"],"text":"","title":"\u7c7b\u578b\u63a8\u65ad"},{"location":"type-safe/","tags":["\u8bcd\u6c47\u8868","type-safe"],"text":"","title":"\u7c7b\u578b\u5b89\u5168"},{"location":"type-signature/","tags":["\u8bcd\u6c47\u8868","type-signature"],"text":"","title":"\u7c7b\u578b\u7b7e\u540d"},{"location":"typeid/","tags":["\u8bcd\u6c47\u8868","typeid"],"text":"\u5728g++\u4e0a\uff0c\u8f93\u51fa\u7684\u7ed3\u679c\u4e3a \u4fee\u9970\u540d \uff0c\u53ef\u4ee5\u4f7f\u7528 1 a.out | c++filt --types \u5f97\u5230\u53ef\u8bfb\u7684\u7ed3\u679c\u3002 \u53c2\u8003","title":"typeid \u8fd0\u7b97\u7b26"},{"location":"undefined-behavior/","tags":["\u8bcd\u6c47\u8868","undefined-behavior"],"text":"","title":"\u672a\u5b9a\u4e49\u884c\u4e3a"},{"location":"uniform-initialization/","tags":["\u8bcd\u6c47\u8868","uniform initialization"],"text":"","title":"\u7edf\u4e00\u521d\u59cb\u5316"},{"location":"unnamed-namespace/","tags":["\u8bcd\u6c47\u8868","unnamed-namespace"],"text":"","title":"\u533f\u540d\u547d\u540d\u7a7a\u95f4"},{"location":"unscoped-enumerations/","tags":["\u8bcd\u6c47\u8868","unscoped-enumerations"],"text":"","title":"\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b"},{"location":"upcasting/","tags":["\u8bcd\u6c47\u8868","upcasting"],"text":"","title":"\u5411\u4e0a\u8f6c\u6362"},{"location":"using-declaration/","tags":["\u8bcd\u6c47\u8868","using-declaration"],"text":"","title":"using \u58f0\u660e"},{"location":"using-directive/","tags":["\u8bcd\u6c47\u8868","using-directive"],"text":"","title":"using \u6307\u4ee4"},{"location":"value-category/","tags":["\u8bcd\u6c47\u8868","value-category"],"text":"","title":"\u503c\u7c7b\u578b"},{"location":"value-container/","tags":["\u8bcd\u6c47\u8868","value-container"],"text":"","title":"\u503c\u5bb9\u5668"},{"location":"value-initialization/","tags":["\u8bcd\u6c47\u8868","value-initialization"],"text":"\u503c\u521d\u59cb\u5316 \uff1a\u901a\u8fc7\u7a7a\u521d\u59cb\u5316\u503c\u6784\u9020\u5bf9\u8c61","title":"\u503c\u521d\u59cb\u5316"},{"location":"value-preserving/","tags":["\u8bcd\u6c47\u8868","value-preserving"],"text":"","title":"\u503c\u4fdd\u7559"},{"location":"virtual-base-class/","tags":["\u8bcd\u6c47\u8868","virtual-base-class"],"text":"","title":"\u865a\u57fa\u7c7b"},{"location":"virtual-destructor/","tags":["\u8bcd\u6c47\u8868","virtual-destructor"],"text":"","title":"\u865a\u6790\u6784\u51fd\u6570"},{"location":"virtual-function/","tags":["\u8bcd\u6c47\u8868","virtual-function"],"text":"","title":"\u865a\u51fd\u6570"},{"location":"virtual-table/","tags":["\u8bcd\u6c47\u8868","virtual-table"],"text":"","title":"\u865a\u8868"},{"location":"void%20pointer/","tags":["\u8bcd\u6c47\u8868","void pointer"],"text":"","title":"void \u6307\u9488"},{"location":"wild-pointer/","tags":["\u8bcd\u6c47\u8868","wild-pointer"],"text":"\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u7684\u6307\u9488","title":"\u91ce\u6307\u9488"},{"location":"word/","tags":["\u8bcd\u6c47\u8868","word"],"text":"","title":"\u5b57\u957f"},{"location":"zero-initialization/","tags":["\u8bcd\u6c47\u8868","zero-initialization"],"text":"","title":"0\u521d\u59cb\u5316"},{"location":"%E6%8B%AC%E5%8F%B7%E5%88%9D%E5%A7%8B%E5%8C%96/","tags":["\u8bcd\u6c47\u8868","\u62ec\u53f7\u521d\u59cb\u5316"],"text":"","title":null},{"location":"%E7%BF%BB%E8%AF%91%E7%BA%A6%E5%AE%9A/","text":"\u82f1\u6587\u539f\u6587\u4f7f\u7528 variable \u6307\u4ee3\u4e86\u53d8\u91cf\u3001\u51fd\u6570\u3001\u5bf9\u8c61\u7b49\u591a\u79cd\u7c7b\u578b\uff0c\u5e76\u4e0d\u4ec5\u9650\u4e8e\u201d\u53d8\u91cf\u201c\uff0c\u5728\u7ffb\u8bd1\u4e2d\u6211\u4eec\u9075\u5faa\u539f\u6587\u8fd9\u4e00\u7ea6\u5b9a {} \u7ffb\u8bd1\u4e3a\u82b1\u62ec\u53f7 \u6bcf\u8282\u8bfe\u540e\u7684\u5c0f\u6d4b\u8bd5\u6682\u65f6\u4e0d\u7ffb\u8bd1\uff0c\u5168\u90e8\u5b8c\u7ed3\u540e\u7edf\u4e00\u7ffb\u8bd1\u5e76\u89e3\u6790 \u5c3d\u7ba1\u81ea\u7136\u8bed\u8a00\u4e2d\u7684\u53e5\u6cd5\u548c\u8bed\u6cd5\u662f\u4e0d\u540c\u7684\uff0c\u5728\u7ffb\u8bd1\u8ba1\u7b97\u673a\u6587\u6863\u65f6\uff0c\u53e5\u6cd5(syntax)\u901a\u5e38\u4e5f\u88ab\u8bd1\u4e3a\u8bed\u6cd5\uff0c\u672c\u9879\u76ee\u4e5f\u91c7\u53d6\u8be5\u505a\u6cd5\u3002 operator \u4e00\u822c\u7ffb\u8bd1\u4e3a\u64cd\u4f5c\u7b26\u6216\u8fd0\u7b97\u7b26\uff0c\u672c\u6587\u7edf\u4e00\u7ffb\u8bd1\u4e3a\u8fd0\u7b97\u7b26\u3002 specifiers \u7ffb\u8bd1\u4e3a\u4fee\u9970\u7b26 Callout 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 !!! success \"\u6700\u4f73\u5b9e\u8df5\" <> !!! tldr \"\u5173\u952e\u4fe1\u606f\" <> !!! info \"\u4f5c\u8005\u6ce8\" <> !!! cite \"\u9898\u5916\u8bdd\" <\u5185\u5bb9> !!! info \"\u6269\u5c55\u9605\u8bfb\" <\u5185\u5bb9> !!! note \"\u6cd5\u5219\" <> !!! question \"Q & A\" !!! tip \"\u5c0f\u8d34\u58eb\" <> !!! warning \"\u6ce8\u610f\" <>","title":"\u7ffb\u8bd1\u7ea6\u5b9a"},{"location":"%E7%BF%BB%E8%AF%91%E7%BA%A6%E5%AE%9A/#callout","text":"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 !!! success \"\u6700\u4f73\u5b9e\u8df5\" <> !!! tldr \"\u5173\u952e\u4fe1\u606f\" <> !!! info \"\u4f5c\u8005\u6ce8\" <> !!! cite \"\u9898\u5916\u8bdd\" <\u5185\u5bb9> !!! info \"\u6269\u5c55\u9605\u8bfb\" <\u5185\u5bb9> !!! note \"\u6cd5\u5219\" <> !!! question \"Q & A\" !!! tip \"\u5c0f\u8d34\u58eb\" <> !!! warning \"\u6ce8\u610f\" <>","title":"Callout"},{"location":"images/onepage/Drawing%202023-02-11%2013.33.50.excalidraw/","tags":["excalidraw"],"text":"\u26a0 Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. \u26a0 Text Elements \u4e00\u7ea7\u6807\u9898 ^2Qkzc43x Header2 L ^NyuMC5Oo \u4e8c\u7ea7\u6807\u9898 ^1THwLYGb Header3 M ^RqaR2vTw \u4e09\u7ea7\u6807\u9898 ^3NfRm2iw Text S ^oNO98VDR \u6b63\u6587 ^lg2Qrkc8 Chrome\u7f29\u653e45%\u6253\u5370 ^w9XRlZgg %% Drawing 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 { \"type\" : \"excalidraw\" , \"version\" : 2 , \"source\" : \"https://excalidraw.com\" , \"elements\" : [ { \"type\" : \"text\" , \"version\" : 555 , \"versionNonce\" : 662979008 , \"isDeleted\" : false , \"id\" : \"2Qkzc43x\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1530.2689639876626 , \"y\" : 1040.4671415627722 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 155 , \"height\" : 50 , \"seed\" : 2122546293 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636025 , \"link\" : null , \"locked\" : false , \"fontSize\" : 36 , \"fontFamily\" : 2 , \"text\" : \"\u4e00\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e00\u7ea7\u6807\u9898 \" , \"baseline\" : 38 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e00\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 532 , \"versionNonce\" : 858372672 , \"isDeleted\" : false , \"id\" : \"NyuMC5Oo\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1535.3314377403399 , \"y\" : 1112.760604189277 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 139 , \"height\" : 32 , \"seed\" : 987263323 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636025 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 2 , \"text\" : \"Header2 L \" , \"rawText\" : \"Header2 L \" , \"baseline\" : 26 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Header2 L \" }, { \"type\" : \"text\" , \"version\" : 532 , \"versionNonce\" : 957745600 , \"isDeleted\" : false , \"id\" : \"1THwLYGb\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1535.3314377403399 , \"y\" : 1148.760604189278 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 121 , \"height\" : 39 , \"seed\" : 1378231765 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 2 , \"text\" : \"\u4e8c\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e8c\u7ea7\u6807\u9898 \" , \"baseline\" : 30 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e8c\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 550 , \"versionNonce\" : 309883456 , \"isDeleted\" : false , \"id\" : \"RqaR2vTw\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1540.1305209719483 , \"y\" : 1231.8259114810737 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 100 , \"height\" : 23 , \"seed\" : 1729596923 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 20 , \"fontFamily\" : 2 , \"text\" : \"Header3 M\" , \"rawText\" : \"Header3 M\" , \"baseline\" : 19 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Header3 M\" }, { \"type\" : \"text\" , \"version\" : 550 , \"versionNonce\" : 1160696256 , \"isDeleted\" : false , \"id\" : \"3NfRm2iw\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1540.1305209719483 , \"y\" : 1257.3259114810737 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 87 , \"height\" : 28 , \"seed\" : 1651286837 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 20 , \"fontFamily\" : 2 , \"text\" : \"\u4e09\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e09\u7ea7\u6807\u9898 \" , \"baseline\" : 21 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e09\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 554 , \"versionNonce\" : 120597056 , \"isDeleted\" : false , \"id\" : \"oNO98VDR\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1543.4508169287008 , \"y\" : 1333.351421558052 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 46 , \"height\" : 19 , \"seed\" : 1116346011 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"text\" : \"Text S\" , \"rawText\" : \"Text S\" , \"baseline\" : 15 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Text S\" }, { \"type\" : \"text\" , \"version\" : 582 , \"versionNonce\" : 1659804096 , \"isDeleted\" : false , \"id\" : \"lg2Qrkc8\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1543.4508169287008 , \"y\" : 1356.9284304866233 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 33 , \"height\" : 23 , \"seed\" : 331117717 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"text\" : \"\u6b63\u6587\" , \"rawText\" : \"\u6b63\u6587\" , \"baseline\" : 17 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u6b63\u6587\" }, { \"type\" : \"rectangle\" , \"version\" : 143 , \"versionNonce\" : 2083578432 , \"isDeleted\" : false , \"id\" : \"vefkyDhSzZRGpVKbqbz-A\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1501.1026384102925 , \"y\" : 1018.4363377836585 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 1754 , \"height\" : 1240 , \"seed\" : 457283003 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false }, { \"type\" : \"text\" , \"version\" : 172 , \"versionNonce\" : 1165320640 , \"isDeleted\" : false , \"id\" : \"w9XRlZgg\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2969.3187842436255 , \"y\" : 2200.7865982003254 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"#fab005\" , \"width\" : 254 , \"height\" : 36 , \"seed\" : 1635294037 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"rawText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"baseline\" : 29 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" }, { \"id\" : \"qiMkuWD7\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1204.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 2107232832 , \"version\" : 2 , \"versionNonce\" : 5937728 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" }, { \"id\" : \"D2qyHaD2\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1233.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 525562304 , \"version\" : 2 , \"versionNonce\" : 1004743104 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"RzU4paPb\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1262.6650309654767 , \"width\" : 107 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1193642560 , \"version\" : 2 , \"versionNonce\" : 1183666752 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"text\" : \"| Process 1 |\" , \"rawText\" : \"| Process 1 |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| Process 1 |\" }, { \"id\" : \"ned75VoD\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1291.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1333737920 , \"version\" : 2 , \"versionNonce\" : 827847104 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"bSOpMKrI\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1320.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1450852928 , \"version\" : 2 , \"versionNonce\" : 631835200 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" }, { \"id\" : \"1GngddyL\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1349.6650309654767 , \"width\" : 58 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 384958912 , \"version\" : 2 , \"versionNonce\" : 754170304 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"Fgrq3YbS\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1378.6650309654767 , \"width\" : 58 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 175635008 , \"version\" : 2 , \"versionNonce\" : 411943488 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"DyanYWrG\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1407.6650309654767 , \"width\" : 66 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1959512512 , \"version\" : 2 , \"versionNonce\" : 1373012416 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"v v\" , \"rawText\" : \"v v\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"v v\" }, { \"id\" : \"BKIPuNmW\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1436.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1711190592 , \"version\" : 2 , \"versionNonce\" : 1041988160 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" }, { \"id\" : \"5QSZqMpi\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1465.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 70029760 , \"version\" : 2 , \"versionNonce\" : 1595951552 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"vhjm4Sfg\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1494.6650309654767 , \"width\" : 107 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 267283008 , \"version\" : 2 , \"versionNonce\" : 1496461888 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| Process 2 |\" , \"rawText\" : \"| Process 2 |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| Process 2 |\" }, { \"id\" : \"jPGVeFjH\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1523.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 2083805632 , \"version\" : 2 , \"versionNonce\" : 804327872 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"lPqCpAQM\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1552.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1395073600 , \"version\" : 2 , \"versionNonce\" : 1353837120 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" }, { \"id\" : \"KXMgPLu1\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1581.6650309654767 , \"width\" : 58 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 805511616 , \"version\" : 2 , \"versionNonce\" : 996727232 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"0gm4X5Po\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1610.6650309654767 , \"width\" : 58 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 512285248 , \"version\" : 2 , \"versionNonce\" : 796566080 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"dZiNiEbb\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1639.6650309654767 , \"width\" : 66 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 246999488 , \"version\" : 2 , \"versionNonce\" : 346530240 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"v v\" , \"rawText\" : \"v v\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"v v\" }, { \"id\" : \"alDnoWo9\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1668.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 83071552 , \"version\" : 2 , \"versionNonce\" : 611080768 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" }, { \"id\" : \"RfAk4D6J\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1697.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 594915776 , \"version\" : 2 , \"versionNonce\" : 1791846848 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"rttDgSOC\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1726.6650309654767 , \"width\" : 107 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1028082240 , \"version\" : 2 , \"versionNonce\" : 40309312 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| Process n |\" , \"rawText\" : \"| Process n |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| Process n |\" }, { \"id\" : \"StAWfpEW\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1755.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 358185408 , \"version\" : 2 , \"versionNonce\" : 150614464 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"N7lr98Ho\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1784.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 576979520 , \"version\" : 2 , \"versionNonce\" : 1078643264 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" } ], \"appState\" : { \"theme\" : \"light\" , \"viewBackgroundColor\" : \"white\" , \"currentItemStrokeColor\" : \"#000000\" , \"currentItemBackgroundColor\" : \"transparent\" , \"currentItemFillStyle\" : \"solid\" , \"currentItemStrokeWidth\" : 4 , \"currentItemStrokeStyle\" : \"solid\" , \"currentItemRoughness\" : 1 , \"currentItemOpacity\" : 100 , \"currentItemFontFamily\" : 2 , \"currentItemFontSize\" : 16 , \"currentItemTextAlign\" : \"left\" , \"currentItemStartArrowhead\" : null , \"currentItemEndArrowhead\" : \"arrow\" , \"scrollX\" : -1190.8299111375652 , \"scrollY\" : -943.1167355109312 , \"zoom\" : { \"value\" : 0.55 }, \"currentItemRoundness\" : \"round\" , \"gridSize\" : null , \"colorPalette\" : { \"elementStroke\" : [ \"#000000\" , \"#343a40\" , \"#495057\" , \"#c92a2a\" , \"#a61e4d\" , \"#862e9c\" , \"#5f3dc4\" , \"#364fc7\" , \"#1864ab\" , \"#0b7285\" , \"#087f5b\" , \"#2b8a3e\" , \"#5c940d\" , \"#e67700\" , \"#d9480f\" ], \"elementBackground\" : [ \"transparent\" , \"#ffffff\" , \"#F8F9FA\" , \"#EDEDE9\" , \"#ADB5BD\" , \"#8cb369\" , \"#f4e285\" , \"#f4a259\" , \"#5b8e7d\" , \"#bc4b51\" , \"#05668d\" , \"#028090\" , \"#00a896\" , \"#02c39a\" , \"#f0f3bd\" , \"#c9e4ca\" , \"#87bba2\" , \"#55828b\" , \"#3b6064\" , \"#FEE440\" , \"#ced4da\" , \"#868e96\" , \"#fa5252\" , \"#e64980\" , \"#be4bdb\" , \"#7950f2\" , \"#4c6ef5\" , \"#228be6\" , \"#15aabf\" , \"#12b886\" , \"#40c057\" , \"#82c91e\" , \"#fab005\" , \"#fd7e14\" , \"#A30000\" ], \"canvasBackground\" : [ \"#ffffff\" , \"#f8f9fa\" , \"#f1f3f5\" , \"#fff5f5\" , \"#fff0f6\" , \"#f8f0fc\" , \"#f3f0ff\" , \"#edf2ff\" , \"#e7f5ff\" , \"#e3fafc\" , \"#e6fcf5\" , \"#ebfbee\" , \"#f4fce3\" , \"#fff9db\" , \"#fff4e6\" ] }, \"currentStrokeOptions\" : null , \"previousGridSize\" : null }, \"files\" : {} } %%","title":"Drawing 2023 02 11 13.33.50.excalidraw"},{"location":"images/onepage/Drawing%202023-02-11%2013.33.50.excalidraw/#text-elements","text":"\u4e00\u7ea7\u6807\u9898 ^2Qkzc43x Header2 L ^NyuMC5Oo \u4e8c\u7ea7\u6807\u9898 ^1THwLYGb Header3 M ^RqaR2vTw \u4e09\u7ea7\u6807\u9898 ^3NfRm2iw Text S ^oNO98VDR \u6b63\u6587 ^lg2Qrkc8 Chrome\u7f29\u653e45%\u6253\u5370 ^w9XRlZgg %%","title":"Text Elements"},{"location":"images/onepage/Drawing%202023-02-11%2013.33.50.excalidraw/#drawing","text":"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 { \"type\" : \"excalidraw\" , \"version\" : 2 , \"source\" : \"https://excalidraw.com\" , \"elements\" : [ { \"type\" : \"text\" , \"version\" : 555 , \"versionNonce\" : 662979008 , \"isDeleted\" : false , \"id\" : \"2Qkzc43x\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1530.2689639876626 , \"y\" : 1040.4671415627722 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 155 , \"height\" : 50 , \"seed\" : 2122546293 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636025 , \"link\" : null , \"locked\" : false , \"fontSize\" : 36 , \"fontFamily\" : 2 , \"text\" : \"\u4e00\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e00\u7ea7\u6807\u9898 \" , \"baseline\" : 38 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e00\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 532 , \"versionNonce\" : 858372672 , \"isDeleted\" : false , \"id\" : \"NyuMC5Oo\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1535.3314377403399 , \"y\" : 1112.760604189277 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 139 , \"height\" : 32 , \"seed\" : 987263323 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636025 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 2 , \"text\" : \"Header2 L \" , \"rawText\" : \"Header2 L \" , \"baseline\" : 26 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Header2 L \" }, { \"type\" : \"text\" , \"version\" : 532 , \"versionNonce\" : 957745600 , \"isDeleted\" : false , \"id\" : \"1THwLYGb\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1535.3314377403399 , \"y\" : 1148.760604189278 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 121 , \"height\" : 39 , \"seed\" : 1378231765 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 2 , \"text\" : \"\u4e8c\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e8c\u7ea7\u6807\u9898 \" , \"baseline\" : 30 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e8c\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 550 , \"versionNonce\" : 309883456 , \"isDeleted\" : false , \"id\" : \"RqaR2vTw\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1540.1305209719483 , \"y\" : 1231.8259114810737 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 100 , \"height\" : 23 , \"seed\" : 1729596923 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 20 , \"fontFamily\" : 2 , \"text\" : \"Header3 M\" , \"rawText\" : \"Header3 M\" , \"baseline\" : 19 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Header3 M\" }, { \"type\" : \"text\" , \"version\" : 550 , \"versionNonce\" : 1160696256 , \"isDeleted\" : false , \"id\" : \"3NfRm2iw\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1540.1305209719483 , \"y\" : 1257.3259114810737 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 87 , \"height\" : 28 , \"seed\" : 1651286837 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 20 , \"fontFamily\" : 2 , \"text\" : \"\u4e09\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e09\u7ea7\u6807\u9898 \" , \"baseline\" : 21 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e09\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 554 , \"versionNonce\" : 120597056 , \"isDeleted\" : false , \"id\" : \"oNO98VDR\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1543.4508169287008 , \"y\" : 1333.351421558052 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 46 , \"height\" : 19 , \"seed\" : 1116346011 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"text\" : \"Text S\" , \"rawText\" : \"Text S\" , \"baseline\" : 15 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Text S\" }, { \"type\" : \"text\" , \"version\" : 582 , \"versionNonce\" : 1659804096 , \"isDeleted\" : false , \"id\" : \"lg2Qrkc8\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1543.4508169287008 , \"y\" : 1356.9284304866233 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 33 , \"height\" : 23 , \"seed\" : 331117717 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"text\" : \"\u6b63\u6587\" , \"rawText\" : \"\u6b63\u6587\" , \"baseline\" : 17 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u6b63\u6587\" }, { \"type\" : \"rectangle\" , \"version\" : 143 , \"versionNonce\" : 2083578432 , \"isDeleted\" : false , \"id\" : \"vefkyDhSzZRGpVKbqbz-A\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1501.1026384102925 , \"y\" : 1018.4363377836585 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 1754 , \"height\" : 1240 , \"seed\" : 457283003 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false }, { \"type\" : \"text\" , \"version\" : 172 , \"versionNonce\" : 1165320640 , \"isDeleted\" : false , \"id\" : \"w9XRlZgg\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2969.3187842436255 , \"y\" : 2200.7865982003254 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"#fab005\" , \"width\" : 254 , \"height\" : 36 , \"seed\" : 1635294037 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"rawText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"baseline\" : 29 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" }, { \"id\" : \"qiMkuWD7\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1204.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 2107232832 , \"version\" : 2 , \"versionNonce\" : 5937728 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" }, { \"id\" : \"D2qyHaD2\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1233.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 525562304 , \"version\" : 2 , \"versionNonce\" : 1004743104 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"RzU4paPb\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1262.6650309654767 , \"width\" : 107 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1193642560 , \"version\" : 2 , \"versionNonce\" : 1183666752 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"text\" : \"| Process 1 |\" , \"rawText\" : \"| Process 1 |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| Process 1 |\" }, { \"id\" : \"ned75VoD\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1291.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1333737920 , \"version\" : 2 , \"versionNonce\" : 827847104 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"bSOpMKrI\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1320.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1450852928 , \"version\" : 2 , \"versionNonce\" : 631835200 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" }, { \"id\" : \"1GngddyL\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1349.6650309654767 , \"width\" : 58 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 384958912 , \"version\" : 2 , \"versionNonce\" : 754170304 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"Fgrq3YbS\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1378.6650309654767 , \"width\" : 58 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 175635008 , \"version\" : 2 , \"versionNonce\" : 411943488 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"DyanYWrG\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1407.6650309654767 , \"width\" : 66 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1959512512 , \"version\" : 2 , \"versionNonce\" : 1373012416 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"v v\" , \"rawText\" : \"v v\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"v v\" }, { \"id\" : \"BKIPuNmW\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1436.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1711190592 , \"version\" : 2 , \"versionNonce\" : 1041988160 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" }, { \"id\" : \"5QSZqMpi\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1465.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 70029760 , \"version\" : 2 , \"versionNonce\" : 1595951552 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"vhjm4Sfg\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1494.6650309654767 , \"width\" : 107 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 267283008 , \"version\" : 2 , \"versionNonce\" : 1496461888 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| Process 2 |\" , \"rawText\" : \"| Process 2 |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| Process 2 |\" }, { \"id\" : \"jPGVeFjH\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1523.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 2083805632 , \"version\" : 2 , \"versionNonce\" : 804327872 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"lPqCpAQM\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1552.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1395073600 , \"version\" : 2 , \"versionNonce\" : 1353837120 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" }, { \"id\" : \"KXMgPLu1\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1581.6650309654767 , \"width\" : 58 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 805511616 , \"version\" : 2 , \"versionNonce\" : 996727232 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"0gm4X5Po\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1610.6650309654767 , \"width\" : 58 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 512285248 , \"version\" : 2 , \"versionNonce\" : 796566080 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"dZiNiEbb\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1639.6650309654767 , \"width\" : 66 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 246999488 , \"version\" : 2 , \"versionNonce\" : 346530240 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"v v\" , \"rawText\" : \"v v\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"v v\" }, { \"id\" : \"alDnoWo9\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1668.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 83071552 , \"version\" : 2 , \"versionNonce\" : 611080768 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" }, { \"id\" : \"RfAk4D6J\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1697.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 594915776 , \"version\" : 2 , \"versionNonce\" : 1791846848 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"rttDgSOC\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1726.6650309654767 , \"width\" : 107 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1028082240 , \"version\" : 2 , \"versionNonce\" : 40309312 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| Process n |\" , \"rawText\" : \"| Process n |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| Process n |\" }, { \"id\" : \"StAWfpEW\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1755.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 358185408 , \"version\" : 2 , \"versionNonce\" : 150614464 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"N7lr98Ho\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1784.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 576979520 , \"version\" : 2 , \"versionNonce\" : 1078643264 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" } ], \"appState\" : { \"theme\" : \"light\" , \"viewBackgroundColor\" : \"white\" , \"currentItemStrokeColor\" : \"#000000\" , \"currentItemBackgroundColor\" : \"transparent\" , \"currentItemFillStyle\" : \"solid\" , \"currentItemStrokeWidth\" : 4 , \"currentItemStrokeStyle\" : \"solid\" , \"currentItemRoughness\" : 1 , \"currentItemOpacity\" : 100 , \"currentItemFontFamily\" : 2 , \"currentItemFontSize\" : 16 , \"currentItemTextAlign\" : \"left\" , \"currentItemStartArrowhead\" : null , \"currentItemEndArrowhead\" : \"arrow\" , \"scrollX\" : -1190.8299111375652 , \"scrollY\" : -943.1167355109312 , \"zoom\" : { \"value\" : 0.55 }, \"currentItemRoundness\" : \"round\" , \"gridSize\" : null , \"colorPalette\" : { \"elementStroke\" : [ \"#000000\" , \"#343a40\" , \"#495057\" , \"#c92a2a\" , \"#a61e4d\" , \"#862e9c\" , \"#5f3dc4\" , \"#364fc7\" , \"#1864ab\" , \"#0b7285\" , \"#087f5b\" , \"#2b8a3e\" , \"#5c940d\" , \"#e67700\" , \"#d9480f\" ], \"elementBackground\" : [ \"transparent\" , \"#ffffff\" , \"#F8F9FA\" , \"#EDEDE9\" , \"#ADB5BD\" , \"#8cb369\" , \"#f4e285\" , \"#f4a259\" , \"#5b8e7d\" , \"#bc4b51\" , \"#05668d\" , \"#028090\" , \"#00a896\" , \"#02c39a\" , \"#f0f3bd\" , \"#c9e4ca\" , \"#87bba2\" , \"#55828b\" , \"#3b6064\" , \"#FEE440\" , \"#ced4da\" , \"#868e96\" , \"#fa5252\" , \"#e64980\" , \"#be4bdb\" , \"#7950f2\" , \"#4c6ef5\" , \"#228be6\" , \"#15aabf\" , \"#12b886\" , \"#40c057\" , \"#82c91e\" , \"#fab005\" , \"#fd7e14\" , \"#A30000\" ], \"canvasBackground\" : [ \"#ffffff\" , \"#f8f9fa\" , \"#f1f3f5\" , \"#fff5f5\" , \"#fff0f6\" , \"#f8f0fc\" , \"#f3f0ff\" , \"#edf2ff\" , \"#e7f5ff\" , \"#e3fafc\" , \"#e6fcf5\" , \"#ebfbee\" , \"#f4fce3\" , \"#fff9db\" , \"#fff4e6\" ] }, \"currentStrokeOptions\" : null , \"previousGridSize\" : null }, \"files\" : {} } %%","title":"Drawing"},{"location":"images/onepage/Template.excalidraw/","tags":["excalidraw"],"text":"\u26a0 Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. \u26a0 Text Elements \u4e00\u7ea7\u6807\u9898 ^2Qkzc43x Header2 L ^NyuMC5Oo \u4e8c\u7ea7\u6807\u9898 ^1THwLYGb Header3 M ^RqaR2vTw \u4e09\u7ea7\u6807\u9898 ^3NfRm2iw Text S ^oNO98VDR \u6b63\u6587 ^lg2Qrkc8 Chrome\u7f29\u653e45%\u6253\u5370 ^w9XRlZgg %% Drawing 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 { \"type\" : \"excalidraw\" , \"version\" : 2 , \"source\" : \"https://excalidraw.com\" , \"elements\" : [ { \"type\" : \"text\" , \"version\" : 554 , \"versionNonce\" : 1059646139 , \"isDeleted\" : false , \"id\" : \"2Qkzc43x\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1530.2689639876626 , \"y\" : 1040.4671415627722 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 156 , \"height\" : 50 , \"seed\" : 2122546293 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103436870 , \"link\" : null , \"locked\" : false , \"fontSize\" : 36 , \"fontFamily\" : 2 , \"text\" : \"\u4e00\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e00\u7ea7\u6807\u9898 \" , \"baseline\" : 38 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e00\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 531 , \"versionNonce\" : 482119867 , \"isDeleted\" : false , \"id\" : \"NyuMC5Oo\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1535.3314377403399 , \"y\" : 1112.760604189277 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 140 , \"height\" : 32 , \"seed\" : 987263323 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103446136 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 2 , \"text\" : \"Header2 L \" , \"rawText\" : \"Header2 L \" , \"baseline\" : 26 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Header2 L \" }, { \"type\" : \"text\" , \"version\" : 531 , \"versionNonce\" : 127806581 , \"isDeleted\" : false , \"id\" : \"1THwLYGb\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1535.3314377403399 , \"y\" : 1148.760604189278 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 122 , \"height\" : 39 , \"seed\" : 1378231765 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103446136 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 2 , \"text\" : \"\u4e8c\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e8c\u7ea7\u6807\u9898 \" , \"baseline\" : 30 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e8c\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 549 , \"versionNonce\" : 1778434395 , \"isDeleted\" : false , \"id\" : \"RqaR2vTw\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1540.1305209719483 , \"y\" : 1231.8259114810737 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 101 , \"height\" : 23 , \"seed\" : 1729596923 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103446136 , \"link\" : null , \"locked\" : false , \"fontSize\" : 20 , \"fontFamily\" : 2 , \"text\" : \"Header3 M\" , \"rawText\" : \"Header3 M\" , \"baseline\" : 19 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Header3 M\" }, { \"type\" : \"text\" , \"version\" : 549 , \"versionNonce\" : 1147852245 , \"isDeleted\" : false , \"id\" : \"3NfRm2iw\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1540.1305209719483 , \"y\" : 1257.3259114810737 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 88 , \"height\" : 28 , \"seed\" : 1651286837 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103446136 , \"link\" : null , \"locked\" : false , \"fontSize\" : 20 , \"fontFamily\" : 2 , \"text\" : \"\u4e09\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e09\u7ea7\u6807\u9898 \" , \"baseline\" : 21 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e09\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 553 , \"versionNonce\" : 750116347 , \"isDeleted\" : false , \"id\" : \"oNO98VDR\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1543.4508169287008 , \"y\" : 1333.351421558052 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 47 , \"height\" : 19 , \"seed\" : 1116346011 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103446136 , \"link\" : null , \"locked\" : false , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"text\" : \"Text S\" , \"rawText\" : \"Text S\" , \"baseline\" : 15 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Text S\" }, { \"type\" : \"text\" , \"version\" : 581 , \"versionNonce\" : 1409519547 , \"isDeleted\" : false , \"id\" : \"lg2Qrkc8\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1543.4508169287008 , \"y\" : 1356.9284304866233 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 34 , \"height\" : 23 , \"seed\" : 331117717 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103455324 , \"link\" : null , \"locked\" : false , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"text\" : \"\u6b63\u6587\" , \"rawText\" : \"\u6b63\u6587\" , \"baseline\" : 17 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u6b63\u6587\" }, { \"type\" : \"rectangle\" , \"version\" : 142 , \"versionNonce\" : 227795253 , \"isDeleted\" : false , \"id\" : \"vefkyDhSzZRGpVKbqbz-A\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1501.1026384102925 , \"y\" : 1018.4363377836585 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 1754 , \"height\" : 1240 , \"seed\" : 457283003 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103205233 , \"link\" : null , \"locked\" : false }, { \"type\" : \"text\" , \"version\" : 171 , \"versionNonce\" : 29873979 , \"isDeleted\" : false , \"id\" : \"w9XRlZgg\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2969.3187842436255 , \"y\" : 2200.7865982003254 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"#fab005\" , \"width\" : 268 , \"height\" : 40 , \"seed\" : 1635294037 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103356545 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"rawText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"baseline\" : 30 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" }, { \"id\" : \"9QJQIml8\" , \"type\" : \"text\" , \"x\" : 2645.3753656830195 , \"y\" : 1408.0741218745675 , \"width\" : 7 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"seed\" : 443708244 , \"version\" : 3 , \"versionNonce\" : 1005643988 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1665035496509 , \"link\" : null , \"locked\" : false , \"text\" : \"\" , \"rawText\" : \"\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"\" } ], \"appState\" : { \"theme\" : \"light\" , \"viewBackgroundColor\" : \"white\" , \"currentItemStrokeColor\" : \"#000000\" , \"currentItemBackgroundColor\" : \"transparent\" , \"currentItemFillStyle\" : \"solid\" , \"currentItemStrokeWidth\" : 4 , \"currentItemStrokeStyle\" : \"solid\" , \"currentItemRoughness\" : 1 , \"currentItemOpacity\" : 100 , \"currentItemFontFamily\" : 2 , \"currentItemFontSize\" : 16 , \"currentItemTextAlign\" : \"left\" , \"currentItemStrokeSharpness\" : \"sharp\" , \"currentItemStartArrowhead\" : null , \"currentItemEndArrowhead\" : \"arrow\" , \"currentItemLinearStrokeSharpness\" : \"round\" , \"gridSize\" : null , \"colorPalette\" : { \"elementStroke\" : [ \"#000000\" , \"#343a40\" , \"#495057\" , \"#c92a2a\" , \"#a61e4d\" , \"#862e9c\" , \"#5f3dc4\" , \"#364fc7\" , \"#1864ab\" , \"#0b7285\" , \"#087f5b\" , \"#2b8a3e\" , \"#5c940d\" , \"#e67700\" , \"#d9480f\" ], \"elementBackground\" : [ \"transparent\" , \"#ffffff\" , \"#F8F9FA\" , \"#EDEDE9\" , \"#ADB5BD\" , \"#8cb369\" , \"#f4e285\" , \"#f4a259\" , \"#5b8e7d\" , \"#bc4b51\" , \"#05668d\" , \"#028090\" , \"#00a896\" , \"#02c39a\" , \"#f0f3bd\" , \"#c9e4ca\" , \"#87bba2\" , \"#55828b\" , \"#3b6064\" , \"#FEE440\" , \"#ced4da\" , \"#868e96\" , \"#fa5252\" , \"#e64980\" , \"#be4bdb\" , \"#7950f2\" , \"#4c6ef5\" , \"#228be6\" , \"#15aabf\" , \"#12b886\" , \"#40c057\" , \"#82c91e\" , \"#fab005\" , \"#fd7e14\" , \"#A30000\" ], \"canvasBackground\" : [ \"#ffffff\" , \"#f8f9fa\" , \"#f1f3f5\" , \"#fff5f5\" , \"#fff0f6\" , \"#f8f0fc\" , \"#f3f0ff\" , \"#edf2ff\" , \"#e7f5ff\" , \"#e3fafc\" , \"#e6fcf5\" , \"#ebfbee\" , \"#f4fce3\" , \"#fff9db\" , \"#fff4e6\" ] } }, \"files\" : {} } %%","title":"Template.excalidraw"},{"location":"images/onepage/Template.excalidraw/#text-elements","text":"\u4e00\u7ea7\u6807\u9898 ^2Qkzc43x Header2 L ^NyuMC5Oo \u4e8c\u7ea7\u6807\u9898 ^1THwLYGb Header3 M ^RqaR2vTw \u4e09\u7ea7\u6807\u9898 ^3NfRm2iw Text S ^oNO98VDR \u6b63\u6587 ^lg2Qrkc8 Chrome\u7f29\u653e45%\u6253\u5370 ^w9XRlZgg %%","title":"Text Elements"},{"location":"images/onepage/Template.excalidraw/#drawing","text":"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 { \"type\" : \"excalidraw\" , \"version\" : 2 , \"source\" : \"https://excalidraw.com\" , \"elements\" : [ { \"type\" : \"text\" , \"version\" : 554 , \"versionNonce\" : 1059646139 , \"isDeleted\" : false , \"id\" : \"2Qkzc43x\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1530.2689639876626 , \"y\" : 1040.4671415627722 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 156 , \"height\" : 50 , \"seed\" : 2122546293 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103436870 , \"link\" : null , \"locked\" : false , \"fontSize\" : 36 , \"fontFamily\" : 2 , \"text\" : \"\u4e00\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e00\u7ea7\u6807\u9898 \" , \"baseline\" : 38 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e00\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 531 , \"versionNonce\" : 482119867 , \"isDeleted\" : false , \"id\" : \"NyuMC5Oo\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1535.3314377403399 , \"y\" : 1112.760604189277 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 140 , \"height\" : 32 , \"seed\" : 987263323 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103446136 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 2 , \"text\" : \"Header2 L \" , \"rawText\" : \"Header2 L \" , \"baseline\" : 26 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Header2 L \" }, { \"type\" : \"text\" , \"version\" : 531 , \"versionNonce\" : 127806581 , \"isDeleted\" : false , \"id\" : \"1THwLYGb\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1535.3314377403399 , \"y\" : 1148.760604189278 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 122 , \"height\" : 39 , \"seed\" : 1378231765 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103446136 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 2 , \"text\" : \"\u4e8c\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e8c\u7ea7\u6807\u9898 \" , \"baseline\" : 30 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e8c\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 549 , \"versionNonce\" : 1778434395 , \"isDeleted\" : false , \"id\" : \"RqaR2vTw\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1540.1305209719483 , \"y\" : 1231.8259114810737 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 101 , \"height\" : 23 , \"seed\" : 1729596923 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103446136 , \"link\" : null , \"locked\" : false , \"fontSize\" : 20 , \"fontFamily\" : 2 , \"text\" : \"Header3 M\" , \"rawText\" : \"Header3 M\" , \"baseline\" : 19 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Header3 M\" }, { \"type\" : \"text\" , \"version\" : 549 , \"versionNonce\" : 1147852245 , \"isDeleted\" : false , \"id\" : \"3NfRm2iw\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1540.1305209719483 , \"y\" : 1257.3259114810737 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 88 , \"height\" : 28 , \"seed\" : 1651286837 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103446136 , \"link\" : null , \"locked\" : false , \"fontSize\" : 20 , \"fontFamily\" : 2 , \"text\" : \"\u4e09\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e09\u7ea7\u6807\u9898 \" , \"baseline\" : 21 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e09\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 553 , \"versionNonce\" : 750116347 , \"isDeleted\" : false , \"id\" : \"oNO98VDR\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1543.4508169287008 , \"y\" : 1333.351421558052 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 47 , \"height\" : 19 , \"seed\" : 1116346011 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103446136 , \"link\" : null , \"locked\" : false , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"text\" : \"Text S\" , \"rawText\" : \"Text S\" , \"baseline\" : 15 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Text S\" }, { \"type\" : \"text\" , \"version\" : 581 , \"versionNonce\" : 1409519547 , \"isDeleted\" : false , \"id\" : \"lg2Qrkc8\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1543.4508169287008 , \"y\" : 1356.9284304866233 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 34 , \"height\" : 23 , \"seed\" : 331117717 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103455324 , \"link\" : null , \"locked\" : false , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"text\" : \"\u6b63\u6587\" , \"rawText\" : \"\u6b63\u6587\" , \"baseline\" : 17 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u6b63\u6587\" }, { \"type\" : \"rectangle\" , \"version\" : 142 , \"versionNonce\" : 227795253 , \"isDeleted\" : false , \"id\" : \"vefkyDhSzZRGpVKbqbz-A\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1501.1026384102925 , \"y\" : 1018.4363377836585 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 1754 , \"height\" : 1240 , \"seed\" : 457283003 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103205233 , \"link\" : null , \"locked\" : false }, { \"type\" : \"text\" , \"version\" : 171 , \"versionNonce\" : 29873979 , \"isDeleted\" : false , \"id\" : \"w9XRlZgg\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2969.3187842436255 , \"y\" : 2200.7865982003254 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"#fab005\" , \"width\" : 268 , \"height\" : 40 , \"seed\" : 1635294037 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103356545 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"rawText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"baseline\" : 30 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" }, { \"id\" : \"9QJQIml8\" , \"type\" : \"text\" , \"x\" : 2645.3753656830195 , \"y\" : 1408.0741218745675 , \"width\" : 7 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"seed\" : 443708244 , \"version\" : 3 , \"versionNonce\" : 1005643988 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1665035496509 , \"link\" : null , \"locked\" : false , \"text\" : \"\" , \"rawText\" : \"\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"\" } ], \"appState\" : { \"theme\" : \"light\" , \"viewBackgroundColor\" : \"white\" , \"currentItemStrokeColor\" : \"#000000\" , \"currentItemBackgroundColor\" : \"transparent\" , \"currentItemFillStyle\" : \"solid\" , \"currentItemStrokeWidth\" : 4 , \"currentItemStrokeStyle\" : \"solid\" , \"currentItemRoughness\" : 1 , \"currentItemOpacity\" : 100 , \"currentItemFontFamily\" : 2 , \"currentItemFontSize\" : 16 , \"currentItemTextAlign\" : \"left\" , \"currentItemStrokeSharpness\" : \"sharp\" , \"currentItemStartArrowhead\" : null , \"currentItemEndArrowhead\" : \"arrow\" , \"currentItemLinearStrokeSharpness\" : \"round\" , \"gridSize\" : null , \"colorPalette\" : { \"elementStroke\" : [ \"#000000\" , \"#343a40\" , \"#495057\" , \"#c92a2a\" , \"#a61e4d\" , \"#862e9c\" , \"#5f3dc4\" , \"#364fc7\" , \"#1864ab\" , \"#0b7285\" , \"#087f5b\" , \"#2b8a3e\" , \"#5c940d\" , \"#e67700\" , \"#d9480f\" ], \"elementBackground\" : [ \"transparent\" , \"#ffffff\" , \"#F8F9FA\" , \"#EDEDE9\" , \"#ADB5BD\" , \"#8cb369\" , \"#f4e285\" , \"#f4a259\" , \"#5b8e7d\" , \"#bc4b51\" , \"#05668d\" , \"#028090\" , \"#00a896\" , \"#02c39a\" , \"#f0f3bd\" , \"#c9e4ca\" , \"#87bba2\" , \"#55828b\" , \"#3b6064\" , \"#FEE440\" , \"#ced4da\" , \"#868e96\" , \"#fa5252\" , \"#e64980\" , \"#be4bdb\" , \"#7950f2\" , \"#4c6ef5\" , \"#228be6\" , \"#15aabf\" , \"#12b886\" , \"#40c057\" , \"#82c91e\" , \"#fab005\" , \"#fd7e14\" , \"#A30000\" ], \"canvasBackground\" : [ \"#ffffff\" , \"#f8f9fa\" , \"#f1f3f5\" , \"#fff5f5\" , \"#fff0f6\" , \"#f8f0fc\" , \"#f3f0ff\" , \"#edf2ff\" , \"#e7f5ff\" , \"#e3fafc\" , \"#e6fcf5\" , \"#ebfbee\" , \"#f4fce3\" , \"#fff9db\" , \"#fff4e6\" ] } }, \"files\" : {} } %%","title":"Drawing"},{"location":"images/onepage/c%2B%2B-basic-onepage/","tags":["excalidraw"],"text":"\u26a0 Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. \u26a0 Text Elements C++ \u57fa\u7840 ^lwSrVksx %% Drawing 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 { \"type\" : \"excalidraw\" , \"version\" : 2 , \"source\" : \"https://excalidraw.com\" , \"elements\" : [ { \"id\" : \"ZXOOsHpZx0cLKavt_neEO\" , \"type\" : \"rectangle\" , \"x\" : 1526.7604451938828 , \"y\" : 1087.1960299121963 , \"width\" : 148.51637279132316 , \"height\" : 10.141263341084612 , \"angle\" : 0 , \"strokeColor\" : \"#ffff\" , \"backgroundColor\" : \"#02c39a\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 50 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"seed\" : 850846956 , \"version\" : 462 , \"versionNonce\" : 916899820 , \"isDeleted\" : false , \"boundElements\" : null , \"updated\" : 1665060708977 , \"link\" : null , \"locked\" : false }, { \"type\" : \"rectangle\" , \"version\" : 142 , \"versionNonce\" : 227795253 , \"isDeleted\" : false , \"id\" : \"vefkyDhSzZRGpVKbqbz-A\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1501.1026384102925 , \"y\" : 1018.4363377836585 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 1754 , \"height\" : 1240 , \"seed\" : 457283003 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103205233 , \"link\" : null , \"locked\" : false }, { \"type\" : \"text\" , \"version\" : 869 , \"versionNonce\" : 1653346924 , \"isDeleted\" : false , \"id\" : \"lwSrVksx\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dashed\" , \"roughness\" : 0 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1531.385308864838 , \"y\" : 1056.5940082382049 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 145 , \"height\" : 46 , \"seed\" : 416331988 , \"groupIds\" : [ \"cKrkJIDYpZUA6XddFe5PY\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665060550725 , \"link\" : null , \"locked\" : false , \"fontSize\" : 36 , \"fontFamily\" : 4 , \"text\" : \"C++ \u57fa\u7840\" , \"rawText\" : \"C++ \u57fa\u7840\" , \"baseline\" : 37 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"C++ \u57fa\u7840\" }, { \"type\" : \"text\" , \"version\" : 172 , \"versionNonce\" : 274743148 , \"isDeleted\" : true , \"id\" : \"w9XRlZgg\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2969.3187842436255 , \"y\" : 2200.7865982003254 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"#fab005\" , \"width\" : 255 , \"height\" : 36 , \"seed\" : 1635294037 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665060725588 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"rawText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"baseline\" : 29 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" } ], \"appState\" : { \"theme\" : \"light\" , \"viewBackgroundColor\" : \"#ffffff\" , \"currentItemStrokeColor\" : \"#ffff\" , \"currentItemBackgroundColor\" : \"#02c39a\" , \"currentItemFillStyle\" : \"solid\" , \"currentItemStrokeWidth\" : 1 , \"currentItemStrokeStyle\" : \"solid\" , \"currentItemRoughness\" : 1 , \"currentItemOpacity\" : 50 , \"currentItemFontFamily\" : 2 , \"currentItemFontSize\" : 16 , \"currentItemTextAlign\" : \"left\" , \"currentItemStrokeSharpness\" : \"sharp\" , \"currentItemStartArrowhead\" : null , \"currentItemEndArrowhead\" : \"arrow\" , \"currentItemLinearStrokeSharpness\" : \"round\" , \"gridSize\" : null , \"colorPalette\" : { \"elementStroke\" : [ \"#000000\" , \"#343a40\" , \"#495057\" , \"#c92a2a\" , \"#a61e4d\" , \"#862e9c\" , \"#5f3dc4\" , \"#364fc7\" , \"#1864ab\" , \"#0b7285\" , \"#087f5b\" , \"#2b8a3e\" , \"#5c940d\" , \"#e67700\" , \"#d9480f\" ], \"elementBackground\" : [ \"transparent\" , \"#ffffff\" , \"#F8F9FA\" , \"#EDEDE9\" , \"#ADB5BD\" , \"#8cb369\" , \"#f4e285\" , \"#f4a259\" , \"#5b8e7d\" , \"#bc4b51\" , \"#05668d\" , \"#028090\" , \"#00a896\" , \"#02c39a\" , \"#f0f3bd\" , \"#c9e4ca\" , \"#87bba2\" , \"#55828b\" , \"#3b6064\" , \"#FEE440\" , \"#ced4da\" , \"#868e96\" , \"#fa5252\" , \"#e64980\" , \"#be4bdb\" , \"#7950f2\" , \"#4c6ef5\" , \"#228be6\" , \"#15aabf\" , \"#12b886\" , \"#40c057\" , \"#82c91e\" , \"#fab005\" , \"#fd7e14\" , \"#A30000\" ], \"canvasBackground\" : [ \"#ffffff\" , \"#f8f9fa\" , \"#f1f3f5\" , \"#fff5f5\" , \"#fff0f6\" , \"#f8f0fc\" , \"#f3f0ff\" , \"#edf2ff\" , \"#e7f5ff\" , \"#e3fafc\" , \"#e6fcf5\" , \"#ebfbee\" , \"#f4fce3\" , \"#fff9db\" , \"#fff4e6\" ] } }, \"files\" : {} } %%","title":"C++ basic onepage"},{"location":"images/onepage/c%2B%2B-basic-onepage/#text-elements","text":"C++ \u57fa\u7840 ^lwSrVksx %%","title":"Text Elements"},{"location":"images/onepage/c%2B%2B-basic-onepage/#drawing","text":"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 { \"type\" : \"excalidraw\" , \"version\" : 2 , \"source\" : \"https://excalidraw.com\" , \"elements\" : [ { \"id\" : \"ZXOOsHpZx0cLKavt_neEO\" , \"type\" : \"rectangle\" , \"x\" : 1526.7604451938828 , \"y\" : 1087.1960299121963 , \"width\" : 148.51637279132316 , \"height\" : 10.141263341084612 , \"angle\" : 0 , \"strokeColor\" : \"#ffff\" , \"backgroundColor\" : \"#02c39a\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 50 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"seed\" : 850846956 , \"version\" : 462 , \"versionNonce\" : 916899820 , \"isDeleted\" : false , \"boundElements\" : null , \"updated\" : 1665060708977 , \"link\" : null , \"locked\" : false }, { \"type\" : \"rectangle\" , \"version\" : 142 , \"versionNonce\" : 227795253 , \"isDeleted\" : false , \"id\" : \"vefkyDhSzZRGpVKbqbz-A\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1501.1026384102925 , \"y\" : 1018.4363377836585 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 1754 , \"height\" : 1240 , \"seed\" : 457283003 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103205233 , \"link\" : null , \"locked\" : false }, { \"type\" : \"text\" , \"version\" : 869 , \"versionNonce\" : 1653346924 , \"isDeleted\" : false , \"id\" : \"lwSrVksx\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dashed\" , \"roughness\" : 0 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1531.385308864838 , \"y\" : 1056.5940082382049 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 145 , \"height\" : 46 , \"seed\" : 416331988 , \"groupIds\" : [ \"cKrkJIDYpZUA6XddFe5PY\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665060550725 , \"link\" : null , \"locked\" : false , \"fontSize\" : 36 , \"fontFamily\" : 4 , \"text\" : \"C++ \u57fa\u7840\" , \"rawText\" : \"C++ \u57fa\u7840\" , \"baseline\" : 37 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"C++ \u57fa\u7840\" }, { \"type\" : \"text\" , \"version\" : 172 , \"versionNonce\" : 274743148 , \"isDeleted\" : true , \"id\" : \"w9XRlZgg\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2969.3187842436255 , \"y\" : 2200.7865982003254 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"#fab005\" , \"width\" : 255 , \"height\" : 36 , \"seed\" : 1635294037 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665060725588 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"rawText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"baseline\" : 29 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" } ], \"appState\" : { \"theme\" : \"light\" , \"viewBackgroundColor\" : \"#ffffff\" , \"currentItemStrokeColor\" : \"#ffff\" , \"currentItemBackgroundColor\" : \"#02c39a\" , \"currentItemFillStyle\" : \"solid\" , \"currentItemStrokeWidth\" : 1 , \"currentItemStrokeStyle\" : \"solid\" , \"currentItemRoughness\" : 1 , \"currentItemOpacity\" : 50 , \"currentItemFontFamily\" : 2 , \"currentItemFontSize\" : 16 , \"currentItemTextAlign\" : \"left\" , \"currentItemStrokeSharpness\" : \"sharp\" , \"currentItemStartArrowhead\" : null , \"currentItemEndArrowhead\" : \"arrow\" , \"currentItemLinearStrokeSharpness\" : \"round\" , \"gridSize\" : null , \"colorPalette\" : { \"elementStroke\" : [ \"#000000\" , \"#343a40\" , \"#495057\" , \"#c92a2a\" , \"#a61e4d\" , \"#862e9c\" , \"#5f3dc4\" , \"#364fc7\" , \"#1864ab\" , \"#0b7285\" , \"#087f5b\" , \"#2b8a3e\" , \"#5c940d\" , \"#e67700\" , \"#d9480f\" ], \"elementBackground\" : [ \"transparent\" , \"#ffffff\" , \"#F8F9FA\" , \"#EDEDE9\" , \"#ADB5BD\" , \"#8cb369\" , \"#f4e285\" , \"#f4a259\" , \"#5b8e7d\" , \"#bc4b51\" , \"#05668d\" , \"#028090\" , \"#00a896\" , \"#02c39a\" , \"#f0f3bd\" , \"#c9e4ca\" , \"#87bba2\" , \"#55828b\" , \"#3b6064\" , \"#FEE440\" , \"#ced4da\" , \"#868e96\" , \"#fa5252\" , \"#e64980\" , \"#be4bdb\" , \"#7950f2\" , \"#4c6ef5\" , \"#228be6\" , \"#15aabf\" , \"#12b886\" , \"#40c057\" , \"#82c91e\" , \"#fab005\" , \"#fd7e14\" , \"#A30000\" ], \"canvasBackground\" : [ \"#ffffff\" , \"#f8f9fa\" , \"#f1f3f5\" , \"#fff5f5\" , \"#fff0f6\" , \"#f8f0fc\" , \"#f3f0ff\" , \"#edf2ff\" , \"#e7f5ff\" , \"#e3fafc\" , \"#e6fcf5\" , \"#ebfbee\" , \"#f4fce3\" , \"#fff9db\" , \"#fff4e6\" ] } }, \"files\" : {} } %%","title":"Drawing"},{"location":"images/onepage/moc-onepage/","tags":["excalidraw"],"text":"\u26a0 Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. \u26a0 Text Elements C++\u57fa\u7840 ^1mr2Eo26 C++ \u8c03\u8bd5\u6280\u672f ^bb31ZJyB \u57fa\u7840\u6570\u636e\u7c7b\u578b ^olTbwHhy \u8fd0\u7b97\u7b26 ^kA66qMSC \u4f5c\u7528\u57df\u3001\u751f\u547d\u5468\u671f\u548c\u94fe\u63a5\u72b6\u6001 ^BNKiTPV9 \u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406 ^AfT65zJz \u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d ^YxPeVAuU \u590d\u5408\u6570\u636e\u7c7b\u578b ^uMlZJGo6 \u679a\u4e3e\u3001\u7ed3\u6784\u4f53\u3001\u8054\u5408\u4f53\u3001\u7c7b ^HYmrPSvn \u6307\u9488\u548c\u5f15\u7528 ^QnFX9MK3 \u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d ^XOSz3Pfr \u51fd\u6570 ^lYY3Wnxo \u9762\u5411\u5bf9\u8c61 ^gjbfsqQD \u64cd\u4f5c\u7b26\u91cd\u8f7d ^tz6YZ7RQ \u5bf9\u8c61\u5173\u7cfb ^bjVsOrRK \u7ee7\u627f ^3GV1Izm9 \u865a\u51fd\u6570 ^sx6WmlZy \u6a21\u677f\u548c\u7c7b ^GIXAA467 \u5f02\u5e38 ^YeDrvOh6 move\u548c\u667a\u80fd\u6307\u9488 ^FMB76L5Z std::string ^7SrQWl95 \u8f93\u5165\u8f93\u51fa ^tTXxyfZG STL ^CQVGISOo Embedded files 5c9c5c8928e9096c0f746787692758a8ef5b1684: icon/icon-cpp.png %% Drawing 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 { \"type\" : \"excalidraw\" , \"version\" : 2 , \"source\" : \"https://excalidraw.com\" , \"elements\" : [ { \"type\" : \"rectangle\" , \"version\" : 164 , \"versionNonce\" : 673785684 , \"isDeleted\" : false , \"id\" : \"vefkyDhSzZRGpVKbqbz-A\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1501.1026384102925 , \"y\" : 1018.4363377836585 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 1754 , \"height\" : 1240 , \"seed\" : 457283003 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false }, { \"type\" : \"text\" , \"version\" : 752 , \"versionNonce\" : 668903404 , \"isDeleted\" : false , \"id\" : \"1mr2Eo26\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2003.183699440322 , \"y\" : 1183.007709557343 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 106 , \"height\" : 35 , \"seed\" : 13740908 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"C++\u57fa\u7840\" , \"rawText\" : \"C++\u57fa\u7840\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"C++\u57fa\u7840\" }, { \"type\" : \"image\" , \"version\" : 782 , \"versionNonce\" : 1219566804 , \"isDeleted\" : false , \"id\" : \"3nYL79EK\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2252.5261386504494 , \"y\" : 1418.0528792824043 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 264.0676833547609 , \"height\" : 264.0676833547609 , \"seed\" : 3786 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"status\" : \"pending\" , \"fileId\" : \"5c9c5c8928e9096c0f746787692758a8ef5b1684\" , \"scale\" : [ 1 , 1 ] }, { \"type\" : \"text\" , \"version\" : 643 , \"versionNonce\" : 739953260 , \"isDeleted\" : false , \"id\" : \"bb31ZJyB\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1940.183699440322 , \"y\" : 1258.617154313309 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 169 , \"height\" : 35 , \"seed\" : 1304755948 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"C++ \u8c03\u8bd5\u6280\u672f\" , \"rawText\" : \"C++ \u8c03\u8bd5\u6280\u672f\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"C++ \u8c03\u8bd5\u6280\u672f\" }, { \"type\" : \"text\" , \"version\" : 602 , \"versionNonce\" : 1335779924 , \"isDeleted\" : false , \"id\" : \"olTbwHhy\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1939.183699440322 , \"y\" : 1322.7847071940316 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 170 , \"height\" : 35 , \"seed\" : 642744916 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u57fa\u7840\u6570\u636e\u7c7b\u578b\" , \"rawText\" : \"\u57fa\u7840\u6570\u636e\u7c7b\u578b\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u57fa\u7840\u6570\u636e\u7c7b\u578b\" }, { \"type\" : \"text\" , \"version\" : 545 , \"versionNonce\" : 1236180204 , \"isDeleted\" : false , \"id\" : \"kA66qMSC\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2023.183699440322 , \"y\" : 1390.3187826424135 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 86 , \"height\" : 35 , \"seed\" : 1262260308 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u8fd0\u7b97\u7b26\" , \"rawText\" : \"\u8fd0\u7b97\u7b26\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u8fd0\u7b97\u7b26\" }, { \"type\" : \"text\" , \"version\" : 513 , \"versionNonce\" : 1673983956 , \"isDeleted\" : false , \"id\" : \"BNKiTPV9\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1743.183699440322 , \"y\" : 1460.1662352073538 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 366 , \"height\" : 35 , \"seed\" : 103667180 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u4f5c\u7528\u57df\u3001\u751f\u547d\u5468\u671f\u548c\u94fe\u63a5\u72b6\u6001\" , \"rawText\" : \"\u4f5c\u7528\u57df\u3001\u751f\u547d\u5468\u671f\u548c\u94fe\u63a5\u72b6\u6001\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4f5c\u7528\u57df\u3001\u751f\u547d\u5468\u671f\u548c\u94fe\u63a5\u72b6\u6001\" }, { \"type\" : \"text\" , \"version\" : 410 , \"versionNonce\" : 1512316780 , \"isDeleted\" : false , \"id\" : \"AfT65zJz\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1883.183699440322 , \"y\" : 1532.43628691515 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 226 , \"height\" : 35 , \"seed\" : 934101612 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406\" , \"rawText\" : \"\u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406\" }, { \"type\" : \"text\" , \"version\" : 414 , \"versionNonce\" : 1631640916 , \"isDeleted\" : false , \"id\" : \"YxPeVAuU\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1855.1836994403218 , \"y\" : 1604.8325837511927 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 254 , \"height\" : 35 , \"seed\" : 1503980140 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d\" , \"rawText\" : \"\u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d\" }, { \"type\" : \"text\" , \"version\" : 389 , \"versionNonce\" : 768447316 , \"isDeleted\" : false , \"id\" : \"gjbfsqQD\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2643.1852495106145 , \"y\" : 1192.485649027449 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 114 , \"height\" : 35 , \"seed\" : 1106618220 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u9762\u5411\u5bf9\u8c61\" , \"rawText\" : \"\u9762\u5411\u5bf9\u8c61\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u9762\u5411\u5bf9\u8c61\" }, { \"type\" : \"text\" , \"version\" : 489 , \"versionNonce\" : 235338732 , \"isDeleted\" : false , \"id\" : \"tz6YZ7RQ\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1277.5153128755035 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 142 , \"height\" : 35 , \"seed\" : 845482580 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u64cd\u4f5c\u7b26\u91cd\u8f7d\" , \"rawText\" : \"\u64cd\u4f5c\u7b26\u91cd\u8f7d\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u64cd\u4f5c\u7b26\u91cd\u8f7d\" }, { \"type\" : \"text\" , \"version\" : 309 , \"versionNonce\" : 1994918100 , \"isDeleted\" : false , \"id\" : \"bjVsOrRK\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1348.5229993106886 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 114 , \"height\" : 35 , \"seed\" : 939697876 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u5bf9\u8c61\u5173\u7cfb\" , \"rawText\" : \"\u5bf9\u8c61\u5173\u7cfb\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u5bf9\u8c61\u5173\u7cfb\" }, { \"type\" : \"text\" , \"version\" : 309 , \"versionNonce\" : 1377345132 , \"isDeleted\" : false , \"id\" : \"3GV1Izm9\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1418.7836946882596 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 58 , \"height\" : 35 , \"seed\" : 1275507820 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u7ee7\u627f\" , \"rawText\" : \"\u7ee7\u627f\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u7ee7\u627f\" }, { \"type\" : \"text\" , \"version\" : 318 , \"versionNonce\" : 1880608340 , \"isDeleted\" : false , \"id\" : \"sx6WmlZy\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1495.3576598567095 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 86 , \"height\" : 35 , \"seed\" : 960700012 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u865a\u51fd\u6570\" , \"rawText\" : \"\u865a\u51fd\u6570\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u865a\u51fd\u6570\" }, { \"type\" : \"text\" , \"version\" : 394 , \"versionNonce\" : 416411884 , \"isDeleted\" : false , \"id\" : \"GIXAA467\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1584.2367228285116 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 114 , \"height\" : 35 , \"seed\" : 1105428308 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u6a21\u677f\u548c\u7c7b\" , \"rawText\" : \"\u6a21\u677f\u548c\u7c7b\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u6a21\u677f\u548c\u7c7b\" }, { \"type\" : \"text\" , \"version\" : 258 , \"versionNonce\" : 171107284 , \"isDeleted\" : false , \"id\" : \"YeDrvOh6\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1657.7953289861653 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 58 , \"height\" : 35 , \"seed\" : 1041638380 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u5f02\u5e38\" , \"rawText\" : \"\u5f02\u5e38\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u5f02\u5e38\" }, { \"type\" : \"text\" , \"version\" : 283 , \"versionNonce\" : 297595756 , \"isDeleted\" : false , \"id\" : \"FMB76L5Z\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1737.4953083229743 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 202 , \"height\" : 35 , \"seed\" : 237917908 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"move\u548c\u667a\u80fd\u6307\u9488\" , \"rawText\" : \"move\u548c\u667a\u80fd\u6307\u9488\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"move\u548c\u667a\u80fd\u6307\u9488\" }, { \"type\" : \"text\" , \"version\" : 238 , \"versionNonce\" : 726616404 , \"isDeleted\" : false , \"id\" : \"7SrQWl95\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1885.8813504963032 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 117 , \"height\" : 35 , \"seed\" : 1096469484 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"std::string\" , \"rawText\" : \"std::string\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"std::string\" }, { \"type\" : \"text\" , \"version\" : 302 , \"versionNonce\" : 1797498348 , \"isDeleted\" : false , \"id\" : \"tTXxyfZG\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1963.5937900037088 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 114 , \"height\" : 35 , \"seed\" : 228038508 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u8f93\u5165\u8f93\u51fa\" , \"rawText\" : \"\u8f93\u5165\u8f93\u51fa\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u8f93\u5165\u8f93\u51fa\" }, { \"type\" : \"text\" , \"version\" : 246 , \"versionNonce\" : 113442516 , \"isDeleted\" : false , \"id\" : \"CQVGISOo\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1803.2140152263964 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 47 , \"height\" : 35 , \"seed\" : 839597780 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"STL\" , \"rawText\" : \"STL\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"STL\" }, { \"id\" : \"Z47K0mod_t1fau6ZQNkND\" , \"type\" : \"freedraw\" , \"x\" : 2067.560768035615 , \"y\" : 1841.9648764467327 , \"width\" : 104.40199822995942 , \"height\" : 138.54831266364909 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"round\" , \"seed\" : 239673044 , \"version\" : 88 , \"versionNonce\" : 1884203372 , \"isDeleted\" : false , \"boundElements\" : null , \"updated\" : 1665111053227 , \"link\" : null , \"locked\" : false , \"points\" : [ [ 0 , 0 ], [ 0 , -0.2439299024065349 ], [ -0.2439299024065349 , -0.2439299024065349 ], [ -4.464304404359382 , -0.2439299024065349 ], [ -8.59949703563052 , -0.2439299024065349 ], [ -12.734689666901886 , 0.782124448985769 ], [ -16.424613428700923 , 1.7694597682500444 ], [ -21.89948457160108 , 5.350970240090646 ], [ -25.67459020408205 , 9.745580386619395 ], [ -28.67531519400245 , 15.01136875602856 ], [ -32.32651992367346 , 21.89948457160108 ], [ -34.35539720718111 , 27.773161745419884 ], [ -37.25932461678167 , 39.32304902920441 ], [ -38.072424291469815 , 46.617714682121004 ], [ -38.39766416134512 , 57.89656874100956 ], [ -39.27271428743825 , 67.05361983928333 ], [ -40.89891363681454 , 74.34828549219992 ], [ -42.97038185566271 , 85.89817277598445 ], [ -45.0031310423833 , 93.19283842890104 ], [ -46.30409052188429 , 96.47234045014352 ], [ -48.08903790298541 , 101.08764907980185 ], [ -50.241816089302574 , 105.38158974279781 ], [ -57.098956679172716 , 116.88888609124183 ], [ -59.73572276709001 , 119.17330898679415 ], [ -63.75475830197729 , 122.82064181325245 ], [ -68.98957144568385 , 127.62180179712527 ], [ -74.8709924259283 , 130.40957211034197 ], [ -77.31029144999275 , 131.0135890115389 ], [ -81.00021521179178 , 133.0153629725569 ], [ -85.13540784306292 , 134.38601670988828 ], [ -92.31391639959566 , 135.79926138256064 ], [ -97.42482864049248 , 137.25509699057352 ], [ -101.60261220710458 , 137.94042385923944 ], [ -104.40199822995942 , 138.30438276124255 ], [ -104.40199822995942 , 138.30438276124255 ] ], \"pressures\" : [ 1 , 0.9714285714285714 , 0.9428571428571428 , 0.9142857142857143 , 0.8857142857142857 , 0.8571428571428571 , 0.8285714285714286 , 0.8 , 0.7714285714285715 , 0.7428571428571429 , 0.7142857142857143 , 0.6857142857142857 , 0.6571428571428571 , 0.6285714285714286 , 0.6 , 0.5714285714285714 , 0.5428571428571428 , 0.5142857142857142 , 0.4857142857142857 , 0.45714285714285713 , 0.42857142857142855 , 0.4 , 0.37142857142857144 , 0.34285714285714286 , 0.3142857142857143 , 0.2857142857142857 , 0.2571428571428571 , 0.22857142857142856 , 0.2 , 0.17142857142857143 , 0.14285714285714285 , 0.11428571428571428 , 0.08571428571428572 , 0.05714285714285714 , 0.02857142857142857 ], \"simulatePressure\" : false , \"lastCommittedPoint\" : [ -104.40199822995942 , 138.30438276124255 ] }, { \"id\" : \"KyxvbIi7weJQGwq2RPa_j\" , \"type\" : \"freedraw\" , \"x\" : 2062.453727697931 , \"y\" : 1843.2851954422977 , \"width\" : 84.45008097439722 , \"height\" : 47.33014487327637 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"round\" , \"seed\" : 208092372 , \"version\" : 76 , \"versionNonce\" : 6026068 , \"isDeleted\" : false , \"boundElements\" : null , \"updated\" : 1665111053227 , \"link\" : null , \"locked\" : false , \"points\" : [ [ 0 , 0 ], [ -0.4878598048128424 , 0 ], [ -0.7317897072193773 , 0 ], [ -2.361860959808382 , 0 ], [ -3.422762440116003 , 0.2632894184705492 ], [ -6.346049365780345 , 0.8866658357314918 ], [ -11.983540443618267 , 2.768410797152683 ], [ -19.893838707370378 , 5.258044562983514 ], [ -28.4120257755319 , 9.548113322766767 ], [ -32.55109031001575 , 12.951516246818528 ], [ -38.796470192263314 , 18.360565035101217 ], [ -41.96368702033442 , 21.233517218999395 ], [ -47.76766993632282 , 26.336685653470795 ], [ -54.01304981857038 , 31.745734441753484 ], [ -60.095809765880404 , 35.39306726821178 ], [ -67.17364883888013 , 39.543747512333994 ], [ -71.55664327577051 , 42.05274079422907 ], [ -78.31311438210787 , 45.79687120100721 ], [ -82.08822001458861 , 46.82679745561222 ], [ -83.47436136477131 , 47.09008687408277 ], [ -83.96222116958415 , 47.09008687408277 ], [ -84.45008097439722 , 47.33014487327637 ], [ -84.45008097439722 , 47.33014487327637 ] ], \"pressures\" : [ 1 , 0.9565217391304348 , 0.9130434782608695 , 0.8695652173913043 , 0.8260869565217391 , 0.782608695652174 , 0.7391304347826086 , 0.6956521739130435 , 0.6521739130434783 , 0.6086956521739131 , 0.5652173913043478 , 0.5217391304347826 , 0.4782608695652174 , 0.43478260869565216 , 0.391304347826087 , 0.34782608695652173 , 0.30434782608695654 , 0.2608695652173913 , 0.21739130434782608 , 0.17391304347826086 , 0.13043478260869565 , 0.08695652173913043 , 0.043478260869565216 ], \"simulatePressure\" : false , \"lastCommittedPoint\" : [ -84.45008097439722 , 47.33014487327637 ] }, { \"id\" : \"16LsNqHn0Q7NpMmUHfFRJ\" , \"type\" : \"freedraw\" , \"x\" : 2068.2151196785785 , \"y\" : 1840.2922142588027 , \"width\" : 111.45660588368241 , \"height\" : 16.571745750787386 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"round\" , \"seed\" : 1556751700 , \"version\" : 78 , \"versionNonce\" : 974035948 , \"isDeleted\" : false , \"boundElements\" : null , \"updated\" : 1665111053227 , \"link\" : null , \"locked\" : false , \"points\" : [ [ 0 , 0 ], [ -3.2872458276679026 , 0 ], [ -6.373152688270238 , 0 ], [ -9.617807580597173 , 0.32136796666259215 ], [ -12.459784538792974 , 0.32136796666259215 ], [ -17.082836974876955 , -1.4635794144387546 ], [ -23.773485726596846 , -3.2098077634118454 ], [ -28.28038106629674 , -5.1186560473224745 ], [ -32.21810663371525 , -7.228843298298898 ], [ -35.90803039551429 , -8.223922423988824 ], [ -41.386773441627156 , -9.931431740833887 ], [ -48.727901933097655 , -11.979668540405328 ], [ -55.054591782813986 , -13.176086633160821 ], [ -61.582620599595884 , -13.950467275721167 ], [ -72.78016469101567 , -15.352096238754939 ], [ -84.21389487841634 , -15.801237011439753 ], [ -90.74192369519847 , -16.250377784124794 ], [ -96.6233446754427 , -16.250377784124794 ], [ -101.49419891714615 , -15.139141562050781 ], [ -106.72901206085271 , -14.109215307445993 ], [ -108.11515341103541 , -14.109215307445993 ], [ -109.5012947612181 , -14.109215307445993 ], [ -111.21267598127588 , -13.82656637291143 ], [ -111.45660588368241 , -13.82656637291143 ], [ -111.45660588368241 , -13.82656637291143 ] ], \"pressures\" : [ 1 , 0.96 , 0.92 , 0.88 , 0.84 , 0.8 , 0.76 , 0.72 , 0.68 , 0.64 , 0.6 , 0.56 , 0.52 , 0.48 , 0.44 , 0.4 , 0.36 , 0.32 , 0.28 , 0.24 , 0.2 , 0.16 , 0.12 , 0.08 , 0.04 ], \"simulatePressure\" : false , \"lastCommittedPoint\" : [ -111.45660588368241 , -13.82656637291143 ] }, { \"id\" : \"Xbq_iHMIlWBVyWWgQsPSB\" , \"type\" : \"freedraw\" , \"x\" : 2063.3520092433005 , \"y\" : 1837.4192620749045 , \"width\" : 92.45717681846872 , \"height\" : 109.34254672949305 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"round\" , \"seed\" : 500221036 , \"version\" : 81 , \"versionNonce\" : 682256596 , \"isDeleted\" : false , \"boundElements\" : null , \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"points\" : [ [ 0 , 0 ], [ -1.060901480307166 , -0.26716132168303375 ], [ -4.3481473079750685 , -1.2622404473729603 ], [ -7.635393135642971 , -2.601918959001978 ], [ -11.549887283784301 , -5.440024013984839 ], [ -14.77905456326016 , -9.439700032808105 ], [ -20.602396995312574 , -23.982568500087837 ], [ -24.64466394947658 , -37.495510712762325 ], [ -26.65418171692022 , -44.310060367291726 ], [ -28.683059000427875 , -51.283358053545726 ], [ -31.343056507621895 , -60.002884088773044 ], [ -33.00797488912622 , -63.69280785057208 ], [ -38.11888713002327 , -73.79460333276938 ], [ -40.69370276653581 , -78.56091618772712 ], [ -43.39241930585786 , -83.24979097842879 ], [ -52.321028114576166 , -94.81129397185168 ], [ -55.06233558923941 , -97.91656034851803 ], [ -57.66425454824139 , -99.86799956776963 ], [ -61.00957892410133 , -102.22211672115236 ], [ -65.07120539432935 , -104.11547739221214 ], [ -69.24898896094123 , -105.4977468391819 ], [ -73.42677252755311 , -106.88001628615166 ], [ -75.86607155161755 , -107.18589663996295 ], [ -83.8189607507104 , -108.28551715239837 ], [ -88.52332315426338 , -108.67270747367843 ], [ -92.21324691606242 , -109.34254672949305 ], [ -92.45717681846872 , -109.34254672949305 ], [ -92.45717681846872 , -109.34254672949305 ] ], \"pressures\" : [ 1 , 0.9642857142857143 , 0.9285714285714286 , 0.8928571428571429 , 0.8571428571428571 , 0.8214285714285714 , 0.7857142857142857 , 0.75 , 0.7142857142857143 , 0.6785714285714286 , 0.6428571428571429 , 0.6071428571428571 , 0.5714285714285714 , 0.5357142857142857 , 0.5 , 0.4642857142857143 , 0.42857142857142855 , 0.39285714285714285 , 0.35714285714285715 , 0.32142857142857145 , 0.2857142857142857 , 0.25 , 0.21428571428571427 , 0.17857142857142858 , 0.14285714285714285 , 0.10714285714285714 , 0.07142857142857142 , 0.03571428571428571 ], \"simulatePressure\" : false , \"lastCommittedPoint\" : [ -92.45717681846872 , -109.34254672949305 ] }, { \"type\" : \"text\" , \"version\" : 641 , \"versionNonce\" : 1546403436 , \"isDeleted\" : false , \"id\" : \"uMlZJGo6\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2080.4926584636933 , \"y\" : 1827.2122128352817 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 170 , \"height\" : 35 , \"seed\" : 1992861652 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u590d\u5408\u6570\u636e\u7c7b\u578b\" , \"rawText\" : \"\u590d\u5408\u6570\u636e\u7c7b\u578b\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u590d\u5408\u6570\u636e\u7c7b\u578b\" }, { \"type\" : \"text\" , \"version\" : 358 , \"versionNonce\" : 2056138324 , \"isDeleted\" : false , \"id\" : \"QnFX9MK3\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1805.0035911794105 , \"y\" : 1714.6381277495311 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 142 , \"height\" : 35 , \"seed\" : 176679148 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u6307\u9488\u548c\u5f15\u7528\" , \"rawText\" : \"\u6307\u9488\u548c\u5f15\u7528\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u6307\u9488\u548c\u5f15\u7528\" }, { \"type\" : \"text\" , \"version\" : 581 , \"versionNonce\" : 1601278188 , \"isDeleted\" : false , \"id\" : \"HYmrPSvn\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1608.013708893797 , \"y\" : 1807.1527917457315 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 338 , \"height\" : 35 , \"seed\" : 1357615060 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u679a\u4e3e\u3001\u7ed3\u6784\u4f53\u3001\u8054\u5408\u4f53\u3001\u7c7b\" , \"rawText\" : \"\u679a\u4e3e\u3001\u7ed3\u6784\u4f53\u3001\u8054\u5408\u4f53\u3001\u7c7b\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u679a\u4e3e\u3001\u7ed3\u6784\u4f53\u3001\u8054\u5408\u4f53\u3001\u7c7b\" }, { \"type\" : \"text\" , \"version\" : 360 , \"versionNonce\" : 920470484 , \"isDeleted\" : false , \"id\" : \"XOSz3Pfr\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1596.8748581279638 , \"y\" : 1869.4389768060942 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 366 , \"height\" : 35 , \"seed\" : 226773716 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d\" , \"rawText\" : \"\u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d\" }, { \"type\" : \"text\" , \"version\" : 433 , \"versionNonce\" : 594056044 , \"isDeleted\" : false , \"id\" : \"lYY3Wnxo\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1876.9415330172199 , \"y\" : 1962.435173187996 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 58 , \"height\" : 35 , \"seed\" : 1218028 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u51fd\u6570\" , \"rawText\" : \"\u51fd\u6570\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u51fd\u6570\" } ], \"appState\" : { \"theme\" : \"light\" , \"viewBackgroundColor\" : \"white\" , \"currentItemStrokeColor\" : \"#000000\" , \"currentItemBackgroundColor\" : \"transparent\" , \"currentItemFillStyle\" : \"solid\" , \"currentItemStrokeWidth\" : 2 , \"currentItemStrokeStyle\" : \"solid\" , \"currentItemRoughness\" : 1 , \"currentItemOpacity\" : 100 , \"currentItemFontFamily\" : 4 , \"currentItemFontSize\" : 28 , \"currentItemTextAlign\" : \"left\" , \"currentItemStrokeSharpness\" : \"sharp\" , \"currentItemStartArrowhead\" : null , \"currentItemEndArrowhead\" : \"arrow\" , \"currentItemLinearStrokeSharpness\" : \"round\" , \"gridSize\" : null , \"colorPalette\" : { \"elementStroke\" : [ \"#000000\" , \"#343a40\" , \"#495057\" , \"#c92a2a\" , \"#a61e4d\" , \"#862e9c\" , \"#5f3dc4\" , \"#364fc7\" , \"#1864ab\" , \"#0b7285\" , \"#087f5b\" , \"#2b8a3e\" , \"#5c940d\" , \"#e67700\" , \"#d9480f\" ], \"elementBackground\" : [ \"transparent\" , \"#ffffff\" , \"#F8F9FA\" , \"#EDEDE9\" , \"#ADB5BD\" , \"#8cb369\" , \"#f4e285\" , \"#f4a259\" , \"#5b8e7d\" , \"#bc4b51\" , \"#05668d\" , \"#028090\" , \"#00a896\" , \"#02c39a\" , \"#f0f3bd\" , \"#c9e4ca\" , \"#87bba2\" , \"#55828b\" , \"#3b6064\" , \"#FEE440\" , \"#ced4da\" , \"#868e96\" , \"#fa5252\" , \"#e64980\" , \"#be4bdb\" , \"#7950f2\" , \"#4c6ef5\" , \"#228be6\" , \"#15aabf\" , \"#12b886\" , \"#40c057\" , \"#82c91e\" , \"#fab005\" , \"#fd7e14\" , \"#A30000\" ], \"canvasBackground\" : [ \"#ffffff\" , \"#f8f9fa\" , \"#f1f3f5\" , \"#fff5f5\" , \"#fff0f6\" , \"#f8f0fc\" , \"#f3f0ff\" , \"#edf2ff\" , \"#e7f5ff\" , \"#e3fafc\" , \"#e6fcf5\" , \"#ebfbee\" , \"#f4fce3\" , \"#fff9db\" , \"#fff4e6\" ] } }, \"files\" : {} } %%","title":"Moc onepage"},{"location":"images/onepage/moc-onepage/#text-elements","text":"C++\u57fa\u7840 ^1mr2Eo26 C++ \u8c03\u8bd5\u6280\u672f ^bb31ZJyB \u57fa\u7840\u6570\u636e\u7c7b\u578b ^olTbwHhy \u8fd0\u7b97\u7b26 ^kA66qMSC \u4f5c\u7528\u57df\u3001\u751f\u547d\u5468\u671f\u548c\u94fe\u63a5\u72b6\u6001 ^BNKiTPV9 \u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406 ^AfT65zJz \u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d ^YxPeVAuU \u590d\u5408\u6570\u636e\u7c7b\u578b ^uMlZJGo6 \u679a\u4e3e\u3001\u7ed3\u6784\u4f53\u3001\u8054\u5408\u4f53\u3001\u7c7b ^HYmrPSvn \u6307\u9488\u548c\u5f15\u7528 ^QnFX9MK3 \u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d ^XOSz3Pfr \u51fd\u6570 ^lYY3Wnxo \u9762\u5411\u5bf9\u8c61 ^gjbfsqQD \u64cd\u4f5c\u7b26\u91cd\u8f7d ^tz6YZ7RQ \u5bf9\u8c61\u5173\u7cfb ^bjVsOrRK \u7ee7\u627f ^3GV1Izm9 \u865a\u51fd\u6570 ^sx6WmlZy \u6a21\u677f\u548c\u7c7b ^GIXAA467 \u5f02\u5e38 ^YeDrvOh6 move\u548c\u667a\u80fd\u6307\u9488 ^FMB76L5Z std::string ^7SrQWl95 \u8f93\u5165\u8f93\u51fa ^tTXxyfZG STL ^CQVGISOo","title":"Text Elements"},{"location":"images/onepage/moc-onepage/#embedded-files","text":"5c9c5c8928e9096c0f746787692758a8ef5b1684: icon/icon-cpp.png %%","title":"Embedded files"},{"location":"images/onepage/moc-onepage/#drawing","text":"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 { \"type\" : \"excalidraw\" , \"version\" : 2 , \"source\" : \"https://excalidraw.com\" , \"elements\" : [ { \"type\" : \"rectangle\" , \"version\" : 164 , \"versionNonce\" : 673785684 , \"isDeleted\" : false , \"id\" : \"vefkyDhSzZRGpVKbqbz-A\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1501.1026384102925 , \"y\" : 1018.4363377836585 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 1754 , \"height\" : 1240 , \"seed\" : 457283003 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false }, { \"type\" : \"text\" , \"version\" : 752 , \"versionNonce\" : 668903404 , \"isDeleted\" : false , \"id\" : \"1mr2Eo26\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2003.183699440322 , \"y\" : 1183.007709557343 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 106 , \"height\" : 35 , \"seed\" : 13740908 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"C++\u57fa\u7840\" , \"rawText\" : \"C++\u57fa\u7840\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"C++\u57fa\u7840\" }, { \"type\" : \"image\" , \"version\" : 782 , \"versionNonce\" : 1219566804 , \"isDeleted\" : false , \"id\" : \"3nYL79EK\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2252.5261386504494 , \"y\" : 1418.0528792824043 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 264.0676833547609 , \"height\" : 264.0676833547609 , \"seed\" : 3786 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"status\" : \"pending\" , \"fileId\" : \"5c9c5c8928e9096c0f746787692758a8ef5b1684\" , \"scale\" : [ 1 , 1 ] }, { \"type\" : \"text\" , \"version\" : 643 , \"versionNonce\" : 739953260 , \"isDeleted\" : false , \"id\" : \"bb31ZJyB\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1940.183699440322 , \"y\" : 1258.617154313309 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 169 , \"height\" : 35 , \"seed\" : 1304755948 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"C++ \u8c03\u8bd5\u6280\u672f\" , \"rawText\" : \"C++ \u8c03\u8bd5\u6280\u672f\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"C++ \u8c03\u8bd5\u6280\u672f\" }, { \"type\" : \"text\" , \"version\" : 602 , \"versionNonce\" : 1335779924 , \"isDeleted\" : false , \"id\" : \"olTbwHhy\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1939.183699440322 , \"y\" : 1322.7847071940316 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 170 , \"height\" : 35 , \"seed\" : 642744916 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u57fa\u7840\u6570\u636e\u7c7b\u578b\" , \"rawText\" : \"\u57fa\u7840\u6570\u636e\u7c7b\u578b\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u57fa\u7840\u6570\u636e\u7c7b\u578b\" }, { \"type\" : \"text\" , \"version\" : 545 , \"versionNonce\" : 1236180204 , \"isDeleted\" : false , \"id\" : \"kA66qMSC\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2023.183699440322 , \"y\" : 1390.3187826424135 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 86 , \"height\" : 35 , \"seed\" : 1262260308 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u8fd0\u7b97\u7b26\" , \"rawText\" : \"\u8fd0\u7b97\u7b26\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u8fd0\u7b97\u7b26\" }, { \"type\" : \"text\" , \"version\" : 513 , \"versionNonce\" : 1673983956 , \"isDeleted\" : false , \"id\" : \"BNKiTPV9\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1743.183699440322 , \"y\" : 1460.1662352073538 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 366 , \"height\" : 35 , \"seed\" : 103667180 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u4f5c\u7528\u57df\u3001\u751f\u547d\u5468\u671f\u548c\u94fe\u63a5\u72b6\u6001\" , \"rawText\" : \"\u4f5c\u7528\u57df\u3001\u751f\u547d\u5468\u671f\u548c\u94fe\u63a5\u72b6\u6001\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4f5c\u7528\u57df\u3001\u751f\u547d\u5468\u671f\u548c\u94fe\u63a5\u72b6\u6001\" }, { \"type\" : \"text\" , \"version\" : 410 , \"versionNonce\" : 1512316780 , \"isDeleted\" : false , \"id\" : \"AfT65zJz\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1883.183699440322 , \"y\" : 1532.43628691515 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 226 , \"height\" : 35 , \"seed\" : 934101612 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406\" , \"rawText\" : \"\u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406\" }, { \"type\" : \"text\" , \"version\" : 414 , \"versionNonce\" : 1631640916 , \"isDeleted\" : false , \"id\" : \"YxPeVAuU\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1855.1836994403218 , \"y\" : 1604.8325837511927 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 254 , \"height\" : 35 , \"seed\" : 1503980140 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d\" , \"rawText\" : \"\u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d\" }, { \"type\" : \"text\" , \"version\" : 389 , \"versionNonce\" : 768447316 , \"isDeleted\" : false , \"id\" : \"gjbfsqQD\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2643.1852495106145 , \"y\" : 1192.485649027449 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 114 , \"height\" : 35 , \"seed\" : 1106618220 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u9762\u5411\u5bf9\u8c61\" , \"rawText\" : \"\u9762\u5411\u5bf9\u8c61\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u9762\u5411\u5bf9\u8c61\" }, { \"type\" : \"text\" , \"version\" : 489 , \"versionNonce\" : 235338732 , \"isDeleted\" : false , \"id\" : \"tz6YZ7RQ\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1277.5153128755035 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 142 , \"height\" : 35 , \"seed\" : 845482580 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u64cd\u4f5c\u7b26\u91cd\u8f7d\" , \"rawText\" : \"\u64cd\u4f5c\u7b26\u91cd\u8f7d\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u64cd\u4f5c\u7b26\u91cd\u8f7d\" }, { \"type\" : \"text\" , \"version\" : 309 , \"versionNonce\" : 1994918100 , \"isDeleted\" : false , \"id\" : \"bjVsOrRK\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1348.5229993106886 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 114 , \"height\" : 35 , \"seed\" : 939697876 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u5bf9\u8c61\u5173\u7cfb\" , \"rawText\" : \"\u5bf9\u8c61\u5173\u7cfb\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u5bf9\u8c61\u5173\u7cfb\" }, { \"type\" : \"text\" , \"version\" : 309 , \"versionNonce\" : 1377345132 , \"isDeleted\" : false , \"id\" : \"3GV1Izm9\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1418.7836946882596 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 58 , \"height\" : 35 , \"seed\" : 1275507820 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u7ee7\u627f\" , \"rawText\" : \"\u7ee7\u627f\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u7ee7\u627f\" }, { \"type\" : \"text\" , \"version\" : 318 , \"versionNonce\" : 1880608340 , \"isDeleted\" : false , \"id\" : \"sx6WmlZy\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1495.3576598567095 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 86 , \"height\" : 35 , \"seed\" : 960700012 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u865a\u51fd\u6570\" , \"rawText\" : \"\u865a\u51fd\u6570\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u865a\u51fd\u6570\" }, { \"type\" : \"text\" , \"version\" : 394 , \"versionNonce\" : 416411884 , \"isDeleted\" : false , \"id\" : \"GIXAA467\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1584.2367228285116 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 114 , \"height\" : 35 , \"seed\" : 1105428308 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u6a21\u677f\u548c\u7c7b\" , \"rawText\" : \"\u6a21\u677f\u548c\u7c7b\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u6a21\u677f\u548c\u7c7b\" }, { \"type\" : \"text\" , \"version\" : 258 , \"versionNonce\" : 171107284 , \"isDeleted\" : false , \"id\" : \"YeDrvOh6\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1657.7953289861653 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 58 , \"height\" : 35 , \"seed\" : 1041638380 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u5f02\u5e38\" , \"rawText\" : \"\u5f02\u5e38\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u5f02\u5e38\" }, { \"type\" : \"text\" , \"version\" : 283 , \"versionNonce\" : 297595756 , \"isDeleted\" : false , \"id\" : \"FMB76L5Z\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1737.4953083229743 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 202 , \"height\" : 35 , \"seed\" : 237917908 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"move\u548c\u667a\u80fd\u6307\u9488\" , \"rawText\" : \"move\u548c\u667a\u80fd\u6307\u9488\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"move\u548c\u667a\u80fd\u6307\u9488\" }, { \"type\" : \"text\" , \"version\" : 238 , \"versionNonce\" : 726616404 , \"isDeleted\" : false , \"id\" : \"7SrQWl95\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1885.8813504963032 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 117 , \"height\" : 35 , \"seed\" : 1096469484 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"std::string\" , \"rawText\" : \"std::string\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"std::string\" }, { \"type\" : \"text\" , \"version\" : 302 , \"versionNonce\" : 1797498348 , \"isDeleted\" : false , \"id\" : \"tTXxyfZG\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1963.5937900037088 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 114 , \"height\" : 35 , \"seed\" : 228038508 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u8f93\u5165\u8f93\u51fa\" , \"rawText\" : \"\u8f93\u5165\u8f93\u51fa\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u8f93\u5165\u8f93\u51fa\" }, { \"type\" : \"text\" , \"version\" : 246 , \"versionNonce\" : 113442516 , \"isDeleted\" : false , \"id\" : \"CQVGISOo\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1803.2140152263964 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 47 , \"height\" : 35 , \"seed\" : 839597780 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"STL\" , \"rawText\" : \"STL\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"STL\" }, { \"id\" : \"Z47K0mod_t1fau6ZQNkND\" , \"type\" : \"freedraw\" , \"x\" : 2067.560768035615 , \"y\" : 1841.9648764467327 , \"width\" : 104.40199822995942 , \"height\" : 138.54831266364909 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"round\" , \"seed\" : 239673044 , \"version\" : 88 , \"versionNonce\" : 1884203372 , \"isDeleted\" : false , \"boundElements\" : null , \"updated\" : 1665111053227 , \"link\" : null , \"locked\" : false , \"points\" : [ [ 0 , 0 ], [ 0 , -0.2439299024065349 ], [ -0.2439299024065349 , -0.2439299024065349 ], [ -4.464304404359382 , -0.2439299024065349 ], [ -8.59949703563052 , -0.2439299024065349 ], [ -12.734689666901886 , 0.782124448985769 ], [ -16.424613428700923 , 1.7694597682500444 ], [ -21.89948457160108 , 5.350970240090646 ], [ -25.67459020408205 , 9.745580386619395 ], [ -28.67531519400245 , 15.01136875602856 ], [ -32.32651992367346 , 21.89948457160108 ], [ -34.35539720718111 , 27.773161745419884 ], [ -37.25932461678167 , 39.32304902920441 ], [ -38.072424291469815 , 46.617714682121004 ], [ -38.39766416134512 , 57.89656874100956 ], [ -39.27271428743825 , 67.05361983928333 ], [ -40.89891363681454 , 74.34828549219992 ], [ -42.97038185566271 , 85.89817277598445 ], [ -45.0031310423833 , 93.19283842890104 ], [ -46.30409052188429 , 96.47234045014352 ], [ -48.08903790298541 , 101.08764907980185 ], [ -50.241816089302574 , 105.38158974279781 ], [ -57.098956679172716 , 116.88888609124183 ], [ -59.73572276709001 , 119.17330898679415 ], [ -63.75475830197729 , 122.82064181325245 ], [ -68.98957144568385 , 127.62180179712527 ], [ -74.8709924259283 , 130.40957211034197 ], [ -77.31029144999275 , 131.0135890115389 ], [ -81.00021521179178 , 133.0153629725569 ], [ -85.13540784306292 , 134.38601670988828 ], [ -92.31391639959566 , 135.79926138256064 ], [ -97.42482864049248 , 137.25509699057352 ], [ -101.60261220710458 , 137.94042385923944 ], [ -104.40199822995942 , 138.30438276124255 ], [ -104.40199822995942 , 138.30438276124255 ] ], \"pressures\" : [ 1 , 0.9714285714285714 , 0.9428571428571428 , 0.9142857142857143 , 0.8857142857142857 , 0.8571428571428571 , 0.8285714285714286 , 0.8 , 0.7714285714285715 , 0.7428571428571429 , 0.7142857142857143 , 0.6857142857142857 , 0.6571428571428571 , 0.6285714285714286 , 0.6 , 0.5714285714285714 , 0.5428571428571428 , 0.5142857142857142 , 0.4857142857142857 , 0.45714285714285713 , 0.42857142857142855 , 0.4 , 0.37142857142857144 , 0.34285714285714286 , 0.3142857142857143 , 0.2857142857142857 , 0.2571428571428571 , 0.22857142857142856 , 0.2 , 0.17142857142857143 , 0.14285714285714285 , 0.11428571428571428 , 0.08571428571428572 , 0.05714285714285714 , 0.02857142857142857 ], \"simulatePressure\" : false , \"lastCommittedPoint\" : [ -104.40199822995942 , 138.30438276124255 ] }, { \"id\" : \"KyxvbIi7weJQGwq2RPa_j\" , \"type\" : \"freedraw\" , \"x\" : 2062.453727697931 , \"y\" : 1843.2851954422977 , \"width\" : 84.45008097439722 , \"height\" : 47.33014487327637 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"round\" , \"seed\" : 208092372 , \"version\" : 76 , \"versionNonce\" : 6026068 , \"isDeleted\" : false , \"boundElements\" : null , \"updated\" : 1665111053227 , \"link\" : null , \"locked\" : false , \"points\" : [ [ 0 , 0 ], [ -0.4878598048128424 , 0 ], [ -0.7317897072193773 , 0 ], [ -2.361860959808382 , 0 ], [ -3.422762440116003 , 0.2632894184705492 ], [ -6.346049365780345 , 0.8866658357314918 ], [ -11.983540443618267 , 2.768410797152683 ], [ -19.893838707370378 , 5.258044562983514 ], [ -28.4120257755319 , 9.548113322766767 ], [ -32.55109031001575 , 12.951516246818528 ], [ -38.796470192263314 , 18.360565035101217 ], [ -41.96368702033442 , 21.233517218999395 ], [ -47.76766993632282 , 26.336685653470795 ], [ -54.01304981857038 , 31.745734441753484 ], [ -60.095809765880404 , 35.39306726821178 ], [ -67.17364883888013 , 39.543747512333994 ], [ -71.55664327577051 , 42.05274079422907 ], [ -78.31311438210787 , 45.79687120100721 ], [ -82.08822001458861 , 46.82679745561222 ], [ -83.47436136477131 , 47.09008687408277 ], [ -83.96222116958415 , 47.09008687408277 ], [ -84.45008097439722 , 47.33014487327637 ], [ -84.45008097439722 , 47.33014487327637 ] ], \"pressures\" : [ 1 , 0.9565217391304348 , 0.9130434782608695 , 0.8695652173913043 , 0.8260869565217391 , 0.782608695652174 , 0.7391304347826086 , 0.6956521739130435 , 0.6521739130434783 , 0.6086956521739131 , 0.5652173913043478 , 0.5217391304347826 , 0.4782608695652174 , 0.43478260869565216 , 0.391304347826087 , 0.34782608695652173 , 0.30434782608695654 , 0.2608695652173913 , 0.21739130434782608 , 0.17391304347826086 , 0.13043478260869565 , 0.08695652173913043 , 0.043478260869565216 ], \"simulatePressure\" : false , \"lastCommittedPoint\" : [ -84.45008097439722 , 47.33014487327637 ] }, { \"id\" : \"16LsNqHn0Q7NpMmUHfFRJ\" , \"type\" : \"freedraw\" , \"x\" : 2068.2151196785785 , \"y\" : 1840.2922142588027 , \"width\" : 111.45660588368241 , \"height\" : 16.571745750787386 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"round\" , \"seed\" : 1556751700 , \"version\" : 78 , \"versionNonce\" : 974035948 , \"isDeleted\" : false , \"boundElements\" : null , \"updated\" : 1665111053227 , \"link\" : null , \"locked\" : false , \"points\" : [ [ 0 , 0 ], [ -3.2872458276679026 , 0 ], [ -6.373152688270238 , 0 ], [ -9.617807580597173 , 0.32136796666259215 ], [ -12.459784538792974 , 0.32136796666259215 ], [ -17.082836974876955 , -1.4635794144387546 ], [ -23.773485726596846 , -3.2098077634118454 ], [ -28.28038106629674 , -5.1186560473224745 ], [ -32.21810663371525 , -7.228843298298898 ], [ -35.90803039551429 , -8.223922423988824 ], [ -41.386773441627156 , -9.931431740833887 ], [ -48.727901933097655 , -11.979668540405328 ], [ -55.054591782813986 , -13.176086633160821 ], [ -61.582620599595884 , -13.950467275721167 ], [ -72.78016469101567 , -15.352096238754939 ], [ -84.21389487841634 , -15.801237011439753 ], [ -90.74192369519847 , -16.250377784124794 ], [ -96.6233446754427 , -16.250377784124794 ], [ -101.49419891714615 , -15.139141562050781 ], [ -106.72901206085271 , -14.109215307445993 ], [ -108.11515341103541 , -14.109215307445993 ], [ -109.5012947612181 , -14.109215307445993 ], [ -111.21267598127588 , -13.82656637291143 ], [ -111.45660588368241 , -13.82656637291143 ], [ -111.45660588368241 , -13.82656637291143 ] ], \"pressures\" : [ 1 , 0.96 , 0.92 , 0.88 , 0.84 , 0.8 , 0.76 , 0.72 , 0.68 , 0.64 , 0.6 , 0.56 , 0.52 , 0.48 , 0.44 , 0.4 , 0.36 , 0.32 , 0.28 , 0.24 , 0.2 , 0.16 , 0.12 , 0.08 , 0.04 ], \"simulatePressure\" : false , \"lastCommittedPoint\" : [ -111.45660588368241 , -13.82656637291143 ] }, { \"id\" : \"Xbq_iHMIlWBVyWWgQsPSB\" , \"type\" : \"freedraw\" , \"x\" : 2063.3520092433005 , \"y\" : 1837.4192620749045 , \"width\" : 92.45717681846872 , \"height\" : 109.34254672949305 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"round\" , \"seed\" : 500221036 , \"version\" : 81 , \"versionNonce\" : 682256596 , \"isDeleted\" : false , \"boundElements\" : null , \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"points\" : [ [ 0 , 0 ], [ -1.060901480307166 , -0.26716132168303375 ], [ -4.3481473079750685 , -1.2622404473729603 ], [ -7.635393135642971 , -2.601918959001978 ], [ -11.549887283784301 , -5.440024013984839 ], [ -14.77905456326016 , -9.439700032808105 ], [ -20.602396995312574 , -23.982568500087837 ], [ -24.64466394947658 , -37.495510712762325 ], [ -26.65418171692022 , -44.310060367291726 ], [ -28.683059000427875 , -51.283358053545726 ], [ -31.343056507621895 , -60.002884088773044 ], [ -33.00797488912622 , -63.69280785057208 ], [ -38.11888713002327 , -73.79460333276938 ], [ -40.69370276653581 , -78.56091618772712 ], [ -43.39241930585786 , -83.24979097842879 ], [ -52.321028114576166 , -94.81129397185168 ], [ -55.06233558923941 , -97.91656034851803 ], [ -57.66425454824139 , -99.86799956776963 ], [ -61.00957892410133 , -102.22211672115236 ], [ -65.07120539432935 , -104.11547739221214 ], [ -69.24898896094123 , -105.4977468391819 ], [ -73.42677252755311 , -106.88001628615166 ], [ -75.86607155161755 , -107.18589663996295 ], [ -83.8189607507104 , -108.28551715239837 ], [ -88.52332315426338 , -108.67270747367843 ], [ -92.21324691606242 , -109.34254672949305 ], [ -92.45717681846872 , -109.34254672949305 ], [ -92.45717681846872 , -109.34254672949305 ] ], \"pressures\" : [ 1 , 0.9642857142857143 , 0.9285714285714286 , 0.8928571428571429 , 0.8571428571428571 , 0.8214285714285714 , 0.7857142857142857 , 0.75 , 0.7142857142857143 , 0.6785714285714286 , 0.6428571428571429 , 0.6071428571428571 , 0.5714285714285714 , 0.5357142857142857 , 0.5 , 0.4642857142857143 , 0.42857142857142855 , 0.39285714285714285 , 0.35714285714285715 , 0.32142857142857145 , 0.2857142857142857 , 0.25 , 0.21428571428571427 , 0.17857142857142858 , 0.14285714285714285 , 0.10714285714285714 , 0.07142857142857142 , 0.03571428571428571 ], \"simulatePressure\" : false , \"lastCommittedPoint\" : [ -92.45717681846872 , -109.34254672949305 ] }, { \"type\" : \"text\" , \"version\" : 641 , \"versionNonce\" : 1546403436 , \"isDeleted\" : false , \"id\" : \"uMlZJGo6\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2080.4926584636933 , \"y\" : 1827.2122128352817 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 170 , \"height\" : 35 , \"seed\" : 1992861652 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u590d\u5408\u6570\u636e\u7c7b\u578b\" , \"rawText\" : \"\u590d\u5408\u6570\u636e\u7c7b\u578b\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u590d\u5408\u6570\u636e\u7c7b\u578b\" }, { \"type\" : \"text\" , \"version\" : 358 , \"versionNonce\" : 2056138324 , \"isDeleted\" : false , \"id\" : \"QnFX9MK3\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1805.0035911794105 , \"y\" : 1714.6381277495311 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 142 , \"height\" : 35 , \"seed\" : 176679148 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u6307\u9488\u548c\u5f15\u7528\" , \"rawText\" : \"\u6307\u9488\u548c\u5f15\u7528\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u6307\u9488\u548c\u5f15\u7528\" }, { \"type\" : \"text\" , \"version\" : 581 , \"versionNonce\" : 1601278188 , \"isDeleted\" : false , \"id\" : \"HYmrPSvn\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1608.013708893797 , \"y\" : 1807.1527917457315 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 338 , \"height\" : 35 , \"seed\" : 1357615060 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u679a\u4e3e\u3001\u7ed3\u6784\u4f53\u3001\u8054\u5408\u4f53\u3001\u7c7b\" , \"rawText\" : \"\u679a\u4e3e\u3001\u7ed3\u6784\u4f53\u3001\u8054\u5408\u4f53\u3001\u7c7b\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u679a\u4e3e\u3001\u7ed3\u6784\u4f53\u3001\u8054\u5408\u4f53\u3001\u7c7b\" }, { \"type\" : \"text\" , \"version\" : 360 , \"versionNonce\" : 920470484 , \"isDeleted\" : false , \"id\" : \"XOSz3Pfr\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1596.8748581279638 , \"y\" : 1869.4389768060942 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 366 , \"height\" : 35 , \"seed\" : 226773716 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d\" , \"rawText\" : \"\u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d\" }, { \"type\" : \"text\" , \"version\" : 433 , \"versionNonce\" : 594056044 , \"isDeleted\" : false , \"id\" : \"lYY3Wnxo\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1876.9415330172199 , \"y\" : 1962.435173187996 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 58 , \"height\" : 35 , \"seed\" : 1218028 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u51fd\u6570\" , \"rawText\" : \"\u51fd\u6570\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u51fd\u6570\" } ], \"appState\" : { \"theme\" : \"light\" , \"viewBackgroundColor\" : \"white\" , \"currentItemStrokeColor\" : \"#000000\" , \"currentItemBackgroundColor\" : \"transparent\" , \"currentItemFillStyle\" : \"solid\" , \"currentItemStrokeWidth\" : 2 , \"currentItemStrokeStyle\" : \"solid\" , \"currentItemRoughness\" : 1 , \"currentItemOpacity\" : 100 , \"currentItemFontFamily\" : 4 , \"currentItemFontSize\" : 28 , \"currentItemTextAlign\" : \"left\" , \"currentItemStrokeSharpness\" : \"sharp\" , \"currentItemStartArrowhead\" : null , \"currentItemEndArrowhead\" : \"arrow\" , \"currentItemLinearStrokeSharpness\" : \"round\" , \"gridSize\" : null , \"colorPalette\" : { \"elementStroke\" : [ \"#000000\" , \"#343a40\" , \"#495057\" , \"#c92a2a\" , \"#a61e4d\" , \"#862e9c\" , \"#5f3dc4\" , \"#364fc7\" , \"#1864ab\" , \"#0b7285\" , \"#087f5b\" , \"#2b8a3e\" , \"#5c940d\" , \"#e67700\" , \"#d9480f\" ], \"elementBackground\" : [ \"transparent\" , \"#ffffff\" , \"#F8F9FA\" , \"#EDEDE9\" , \"#ADB5BD\" , \"#8cb369\" , \"#f4e285\" , \"#f4a259\" , \"#5b8e7d\" , \"#bc4b51\" , \"#05668d\" , \"#028090\" , \"#00a896\" , \"#02c39a\" , \"#f0f3bd\" , \"#c9e4ca\" , \"#87bba2\" , \"#55828b\" , \"#3b6064\" , \"#FEE440\" , \"#ced4da\" , \"#868e96\" , \"#fa5252\" , \"#e64980\" , \"#be4bdb\" , \"#7950f2\" , \"#4c6ef5\" , \"#228be6\" , \"#15aabf\" , \"#12b886\" , \"#40c057\" , \"#82c91e\" , \"#fab005\" , \"#fd7e14\" , \"#A30000\" ], \"canvasBackground\" : [ \"#ffffff\" , \"#f8f9fa\" , \"#f1f3f5\" , \"#fff5f5\" , \"#fff0f6\" , \"#f8f0fc\" , \"#f3f0ff\" , \"#edf2ff\" , \"#e7f5ff\" , \"#e3fafc\" , \"#e6fcf5\" , \"#ebfbee\" , \"#f4fce3\" , \"#fff9db\" , \"#fff4e6\" ] } }, \"files\" : {} } %%","title":"Drawing"},{"location":"images/onepage/Scripts/Downloaded/Add%20Link%20and%20Open%20Page/","text":"/* Prompt for a file in the vault. Add a link above or below (based on settings) the selected element, to the selected file. If no file is selected then the script creates a new file following the default filename defined for excalidraw embeds. Creates empty markdown file by default, this can be changed to creating a drawing by default via settings. ```javascript */ if(!ea.verifyMinimumPluginVersion || !ea.verifyMinimumPluginVersion(\"1.5.21\")) { new Notice(\"This script requires a newer version of Excalidraw. Please install the latest version.\"); return; } const BLANK_DRAWING = [\"---\",\"\",\"excalidraw-plugin: parsed\",\"\",\"---\",\" \u26a0 Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. \u26a0 \",\"\",\"\",\"%%\",\"# Drawing\",\"\\x60\\x60\\x60json\",'{\"type\":\"excalidraw\",\"version\":2,\"source\":\"https://excalidraw.com\",\"elements\":[],\"appState\":{\"gridSize\":null,\"viewBackgroundColor\":\"#ffffff\"}}',\"\\x60\\x60\\x60\",\"%%\"].join(\"\\n\"); settings = ea.getScriptSettings(); //set default values on first run if(!settings[\"Link position\"]) { settings = { \"Link position\" : { value: \"below\", valueset: [\"above\",\"below\"], description: \"Add link below or above the selected object?\" }, \"Link font size\" : { value: 12 } }; ea.setScriptSettings(settings); } if(!settings[\"New document should be an Excalidraw drawing\"]) { settings = { \"New document should be an Excalidraw drawing\": { value: false, description: \"When adding a new document, should the new document be a blank markdown document (toggle == off) or a blank Excalidraw drawing (toggle=on)?\" }, ...settings }; ea.setScriptSettings(settings); } const below = settings[\"Link position\"].value === \"below\"; const newDocExcalidraw = settings[\"New document should be an Excalidraw drawing\"].value; const fontSize = Math.floor(settings[\"Link font size\"].value); elements = ea.getViewSelectedElements(); if(elements.length === 0) { new Notice(\"No selected elements\"); return; } const files = app.vault.getFiles() const filePaths = files.map((f)=>f.path); file = await utils.suggester(filePaths,files,\"Select file or press ESC to create a new document\"); alias = null; if(file) { alias = file.basename; } else { const prefix = ea.targetView.file.path.substring(0,ea.targetView.file.path.length-3); const timestamp = moment(Date.now()).format(ea.plugin.settings.drawingFilenameDateTime); file = await app.vault.create( ${prefix} ${timestamp}.md ,newDocExcalidraw?BLANK_DRAWING:\"\"); if(newDocExcalidraw) await new Promise(r => setTimeout(r, 100)); //wait for metadata cache to update, so file opens as excalidraw } const filepath = app.metadataCache.fileToLinktext(file,ea.targetView.file.path,true); ea.style.textAlign = \"center\"; ea.style.fontSize = fontSize; const textElementsIfAny = elements.filter(el=>el.type===\"text\"); if(textElementsIfAny.length>0) ea.style.fontFamily = textElementsIfAny[0].fontFamily; ea.style.strokeColor = elements[0].strokeColor; const box = ea.getBoundingBox(elements); const linkText = [[${filepath}${alias?\"|\"+alias:\"\"}]] ; const size = ea.measureText(alias?ea.plugin.settings.linkPrefix+alias:linkText); const id = ea.addText( box.topX+(box.width-size.width)/2, below ? box.topY + box.height + size.height : box.topY - size.height - 3, linkText ); ea.copyViewElementsToEAforEditing(elements); ea.addToGroup(elements.map((e)=>e.id).concat([id])); ea.addElementsToView(false,true,true); ea.openFileInNewOrAdjacentLeaf(file);","title":"Add Link and Open Page"},{"location":"images/onepage/Scripts/Downloaded/Box%20Selected%20Elements/","text":"/* This script will add an encapsulating box around the currently selected elements in Excalidraw. See documentation for more details: https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html ```javascript */ if(!ea.verifyMinimumPluginVersion || !ea.verifyMinimumPluginVersion(\"1.5.21\")) { new Notice(\"This script requires a newer version of Excalidraw. Please install the latest version.\"); return; } settings = ea.getScriptSettings(); //set default values on first run if(!settings[\"Default padding\"]) { settings = { \"Prompt for padding?\": true, \"Default padding\" : { value: 10, description: \"Padding between the bounding box of the selected elements, and the box the script creates\" } }; ea.setScriptSettings(settings); } let padding = settings[\"Default padding\"].value; if(settings[\"Prompt for padding?\"]) { padding = parseInt (await utils.inputPrompt(\"padding?\",\"number\",padding.toString())); } if(isNaN(padding)) { new Notice(\"The padding value provided is not a number\"); return; } elements = ea.getViewSelectedElements(); const box = ea.getBoundingBox(elements); color = ea .getExcalidrawAPI() .getAppState() .currentItemStrokeColor; //uncomment for random color: //color = '#'+(Math.random() 0xFFFFFF<<0).toString(16).padStart(6,\"0\"); ea.style.strokeColor = color; id = ea.addRect( box.topX - padding, box.topY - padding, box.width + 2 padding, box.height + 2*padding ); ea.copyViewElementsToEAforEditing(elements); ea.addToGroup([id].concat(elements.map((el)=>el.id))); ea.addElementsToView(false,false);","title":"Box Selected Elements"},{"location":"images/onepage/Scripts/Downloaded/Bullet%20Point/","text":"/* Download this file and save to your Obsidian Vault including the first line, or open it in \"Raw\" and copy the entire contents to Obsidian. This script will add a small circle to the top left of each text element in the selection and add the text and the \"bullet point\" into a group. See documentation for more details: https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html ```javascript */ elements = ea.getViewSelectedElements().filter((el)=>el.type===\"text\"); ea.copyViewElementsToEAforEditing(elements); const padding = 10; elements.forEach((el)=>{ ea.style.strokeColor = el.strokeColor; const size = el.fontSize/2; const ellipseId = ea.addEllipse( el.x-padding-size, el.y+size/2, size, size ); ea.addToGroup([el.id,ellipseId]); }); ea.addElementsToView();","title":"Bullet Point"},{"location":"images/onepage/Scripts/Downloaded/Convert%20freedraw%20to%20line/","text":"/* Convert selected freedraw objects into editable lines. This will allow you to adjust your drawings by dragging line points and will also allow you to select shape fill in case of enclosed lines. You can adjust conversion point density in settings. ```javascript */ if(!ea.verifyMinimumPluginVersion || !ea.verifyMinimumPluginVersion(\"1.5.21\")) { new Notice(\"This script requires a newer version of Excalidraw. Please install the latest version.\"); return; } settings = ea.getScriptSettings(); //set default values on first run if(!settings[\"Point density\"]) { settings = { \"Point density\" : { value: \"7:1\", valueset: [\"1:1\",\"2:1\",\"3:1\",\"4:1\",\"5:1\",\"6:1\",\"7:1\",\"8:1\",\"9:1\",\"10:1\",\"11:1\"], description: \"A freedraw object has many points. Converting freedraw to a line with too many points will result in an impractical object that is hard to edit. This setting sepcifies how many points from freedraw should be averaged to form a point on the line\" }, }; ea.setScriptSettings(settings); } const scale = settings[\"Point density\"].value; const setSize = parseInt(scale.substring(0,scale.indexOf(\":\"))); const elements = ea.getViewSelectedElements().filter(el=>el.type===\"freedraw\"); if(elements.length === 0) { new Notice(\"No freedraw object is selected\"); } ea.style.roughness=0; ea.style.strokeSharpness=\"round\"; elements.forEach((el)=>{ points = []; points.push(el.points[0]); for(i=1;i [original text](../../../../selected folder/original text.md) See documentation for more details: https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html ```javascript */ folders = new Set(); app.vault.getFiles().forEach((f)=> folders.add(f.path.substring(0,f.path.lastIndexOf(\"/\"))) ); f = Array.from(folders); folder = await utils.suggester(f,f); folder = folder??\"\"; //if exiting suggester with ESC folder = folder === \"\" ? folder : folder + \"/\"; elements = ea.getViewSelectedElements().filter((el)=>el.type===\"text\"); elements.forEach((el)=>{ el.rawText = \"[[\"+folder+el.rawText+\"|\"+el.rawText+\"]]\"; el.text = \"[[\"+folder+el.text+\"|\"+el.text+\"]]\"; el.originalText = \"[[\"+folder+el.originalText+\"|\"+el.originalText+\"]]\"; }) ea.copyViewElementsToEAforEditing(elements); ea.addElementsToView();","title":"Convert text to link with folder and alias"},{"location":"images/onepage/Scripts/Downloaded/Expand%20rectangles%20horizontally%20keep%20text%20centered/","text":"/* Download this file and save to your Obsidian Vault including the first line, or open it in \"Raw\" and copy the entire contents to Obsidian. This script expands the width of the selected rectangles until they are all the same width and keep the text centered. See documentation for more details: https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html ```javascript */ const elements = ea.getViewSelectedElements(); const topGroups = ea.getMaximumGroups(elements); const allIndividualArrows = ea.getMaximumGroups(ea.getViewElements()) .reduce((result, group) => (group.length === 1 && (group[0].type === 'arrow')) ? [...result, group[0]] : result, []); const groupWidths = topGroups .map((g) => { if(g.length === 1 && (g[0].type === 'arrow' || g[0].type === 'line')) { // ignore individual lines return { minLeft: 0, maxRight: 0 }; } return g.reduce( (pre, cur, i) => { if (i === 0) { return { minLeft: cur.x, maxRight: cur.x + cur.width, index: i, }; } else { return { minLeft: cur.x < pre.minLeft ? cur.x : pre.minLeft, maxRight: cur.x + cur.width > pre.maxRight ? cur.x + cur.width : pre.maxRight, index: i, }; } }, { minLeft: 0, maxRight: 0 } ); }) .map((r) => { r.width = r.maxRight - r.minLeft; return r; }); const maxGroupWidth = Math.max(...groupWidths.map((g) => g.width)); for (var i = 0; i < topGroups.length; i++) { const rects = topGroups[i] .filter((el) => el.type === \"rectangle\") .sort((lha, rha) => lha.x - rha.x); const texts = topGroups[i] .filter((el) => el.type === \"text\") .sort((lha, rha) => lha.x - rha.x); const groupWith = groupWidths[i].width; if (groupWith < maxGroupWidth) { const distance = maxGroupWidth - groupWith; const perRectDistance = distance / rects.length; const textsWithRectIndex = []; for (var j = 0; j < rects.length; j++) { const rect = rects[j]; const rectLeft = rect.x; const rectTop = rect.y; const rectRight = rect.x + rect.width; const rectBottom = rect.y + rect.height; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 const textsWithRect = texts.filter(text => text.x >= rectLeft && text.x <= rectRight && text.y >= rectTop && text.y <= rectBottom); textsWithRectIndex[j] = textsWithRect; } for (var j = 0; j < rects.length; j++) { const rect = rects[j]; rect.x = rect.x + perRectDistance * j - perRectDistance / 2; rect.width += perRectDistance; const textsWithRect = textsWithRectIndex[j]; if(textsWithRect) { for(const text of textsWithRect) { text.x = text.x + perRectDistance * j; } } // recalculate the position of the points const startBindingLines = allIndividualArrows.filter(el => (el.startBinding||{}).elementId === rect.id); for(startBindingLine of startBindingLines) { recalculateStartPointOfLine(startBindingLine, rect); } const endBindingLines = allIndividualArrows.filter(el => (el.endBinding||{}).elementId === rect.id); for(endBindingLine of endBindingLines) { recalculateEndPointOfLine(endBindingLine, rect); } } } } ea.copyViewElementsToEAforEditing(elements); await ea.addElementsToView(false, false); function recalculateStartPointOfLine(line, el) { const aX = el.x + el.width/2; const bX = line.x + line.points[1][0]; const aY = el.y + el.height/2; const bY = line.y + line.points[1][1]; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 line.startBinding.gap = 8; line.startBinding.focus = 0; const intersectA = ea.intersectElementWithLine( el, [bX, bY], [aX, aY], line.startBinding.gap ); if(intersectA.length > 0) { line.points[0] = [0, 0]; for(var i = 1; i 0) { line.points[line.points.length - 1] = [intersectA[0][0] - line.x, intersectA[0][1] - line.y]; } }","title":"Expand rectangles horizontally keep text centered"},{"location":"images/onepage/Scripts/Downloaded/Expand%20rectangles%20horizontally/","text":"/* Download this file and save to your Obsidian Vault including the first line, or open it in \"Raw\" and copy the entire contents to Obsidian. This script expands the width of the selected rectangles until they are all the same width. See documentation for more details: https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html ```javascript */ const elements = ea.getViewSelectedElements(); const topGroups = ea.getMaximumGroups(elements); const allIndividualArrows = ea.getMaximumGroups(ea.getViewElements()) .reduce((result, group) => (group.length === 1 && (group[0].type === 'arrow' || group[0].type === 'line')) ? [...result, group[0]] : result, []); const groupWidths = topGroups .map((g) => { if(g.length === 1 && (g[0].type === 'arrow' || g[0].type === 'line')) { // ignore individual lines return { minLeft: 0, maxRight: 0 }; } return g.reduce( (pre, cur, i) => { if (i === 0) { return { minLeft: cur.x, maxRight: cur.x + cur.width, index: i, }; } else { return { minLeft: cur.x < pre.minLeft ? cur.x : pre.minLeft, maxRight: cur.x + cur.width > pre.maxRight ? cur.x + cur.width : pre.maxRight, index: i, }; } }, { minLeft: 0, maxRight: 0 } ); }) .map((r) => { r.width = r.maxRight - r.minLeft; return r; }); const maxGroupWidth = Math.max(...groupWidths.map((g) => g.width)); for (var i = 0; i < topGroups.length; i++) { const rects = topGroups[i] .filter((el) => el.type === \"rectangle\") .sort((lha, rha) => lha.x - rha.x); const groupWith = groupWidths[i].width; if (groupWith < maxGroupWidth) { const distance = maxGroupWidth - groupWith; const perRectDistance = distance / rects.length; for (var j = 0; j < rects.length; j++) { const rect = rects[j]; rect.x = rect.x + perRectDistance * j; rect.width += perRectDistance; 1 2 3 4 5 6 7 8 9 10 11 // recalculate the position of the points const startBindingLines = allIndividualArrows.filter(el => (el.startBinding||{}).elementId === rect.id); for(startBindingLine of startBindingLines) { recalculateStartPointOfLine(startBindingLine, rect); } const endBindingLines = allIndividualArrows.filter(el => (el.endBinding||{}).elementId === rect.id); for(endBindingLine of endBindingLines) { recalculateEndPointOfLine(endBindingLine, rect); } } } } ea.copyViewElementsToEAforEditing(elements); await ea.addElementsToView(false, false); function recalculateStartPointOfLine(line, el) { const aX = el.x + el.width/2; const bX = line.x + line.points[1][0]; const aY = el.y + el.height/2; const bY = line.y + line.points[1][1]; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 line.startBinding.gap = 8; line.startBinding.focus = 0; const intersectA = ea.intersectElementWithLine( el, [bX, bY], [aX, aY], line.startBinding.gap ); if(intersectA.length > 0) { line.points[0] = [0, 0]; for(var i = 1; i 0) { line.points[line.points.length - 1] = [intersectA[0][0] - line.x, intersectA[0][1] - line.y]; } }","title":"Expand rectangles horizontally"},{"location":"images/onepage/Scripts/Downloaded/Expand%20rectangles%20vertically%20keep%20text%20centered/","text":"/* Download this file and save to your Obsidian Vault including the first line, or open it in \"Raw\" and copy the entire contents to Obsidian. This script expands the height of the selected rectangles until they are all the same height and keep the text centered. See documentation for more details: https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html ```javascript */ const elements = ea.getViewSelectedElements(); const topGroups = ea.getMaximumGroups(elements); const allIndividualArrows = ea.getMaximumGroups(ea.getViewElements()) .reduce((result, group) => (group.length === 1 && (group[0].type === 'arrow' || group[0].type === 'line')) ? [...result, group[0]] : result, []); const groupHeights = topGroups .map((g) => { if(g.length === 1 && (g[0].type === 'arrow' || g[0].type === 'line')) { // ignore individual lines return { minTop: 0, maxBottom: 0 }; } return g.reduce( (pre, cur, i) => { if (i === 0) { return { minTop: cur.y, maxBottom: cur.y + cur.height, index: i, }; } else { return { minTop: cur.y < pre.minTop ? cur.y : pre.minTop, maxBottom: cur.y + cur.height > pre.maxBottom ? cur.y + cur.height : pre.maxBottom, index: i, }; } }, { minTop: 0, maxBottom: 0 } ); }) .map((r) => { r.height = r.maxBottom - r.minTop; return r; }); const maxGroupHeight = Math.max(...groupHeights.map((g) => g.height)); for (var i = 0; i < topGroups.length; i++) { const rects = topGroups[i] .filter((el) => el.type === \"rectangle\") .sort((lha, rha) => lha.y - rha.y); const texts = topGroups[i] .filter((el) => el.type === \"text\") .sort((lha, rha) => lha.y - rha.y); const groupWith = groupHeights[i].height; if (groupWith < maxGroupHeight) { const distance = maxGroupHeight - groupWith; const perRectDistance = distance / rects.length; const textsWithRectIndex = []; for (var j = 0; j < rects.length; j++) { const rect = rects[j]; const rectLeft = rect.x; const rectTop = rect.y; const rectRight = rect.x + rect.width; const rectBottom = rect.y + rect.height; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 const textsWithRect = texts.filter(text => text.x >= rectLeft && text.x <= rectRight && text.y >= rectTop && text.y <= rectBottom); textsWithRectIndex[j] = textsWithRect; } for (var j = 0; j < rects.length; j++) { const rect = rects[j]; rect.y = rect.y + perRectDistance * j - perRectDistance / 2; rect.height += perRectDistance; const textsWithRect = textsWithRectIndex[j]; if(textsWithRect) { for(const text of textsWithRect) { text.y = text.y + perRectDistance * j; } } // recalculate the position of the points const startBindingLines = allIndividualArrows.filter(el => (el.startBinding||{}).elementId === rect.id); for(startBindingLine of startBindingLines) { recalculateStartPointOfLine(startBindingLine, rect); } const endBindingLines = allIndividualArrows.filter(el => (el.endBinding||{}).elementId === rect.id); for(endBindingLine of endBindingLines) { recalculateEndPointOfLine(endBindingLine, rect); } } } } ea.copyViewElementsToEAforEditing(elements); await ea.addElementsToView(false, false); function recalculateStartPointOfLine(line, el) { const aX = el.x + el.width/2; const bX = line.x + line.points[1][0]; const aY = el.y + el.height/2; const bY = line.y + line.points[1][1]; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 line.startBinding.gap = 8; line.startBinding.focus = 0; const intersectA = ea.intersectElementWithLine( el, [bX, bY], [aX, aY], line.startBinding.gap ); if(intersectA.length > 0) { line.points[0] = [0, 0]; for(var i = 1; i 0) { line.points[line.points.length - 1] = [intersectA[0][0] - line.x, intersectA[0][1] - line.y]; } }","title":"Expand rectangles vertically keep text centered"},{"location":"images/onepage/Scripts/Downloaded/Expand%20rectangles%20vertically/","text":"/* Download this file and save to your Obsidian Vault including the first line, or open it in \"Raw\" and copy the entire contents to Obsidian. This script expands the height of the selected rectangles until they are all the same height. ```javascript */ const elements = ea.getViewSelectedElements(); const topGroups = ea.getMaximumGroups(elements); const allLines = ea.getViewElements().filter(el => el.type === 'arrow' || el.type === 'line'); const allIndividualArrows = ea.getMaximumGroups(ea.getViewElements()) .reduce((result, group) => (group.length === 1 && (group[0].type === 'arrow' || group[0].type === 'line')) ? [...result, group[0]] : result, []); const groupHeights = topGroups .map((g) => { if(g.length === 1 && (g[0].type === 'arrow' || g[0].type === 'line')) { // ignore individual lines return { minTop: 0, maxBottom: 0 }; } return g.reduce( (pre, cur, i) => { if (i === 0) { return { minTop: cur.y, maxBottom: cur.y + cur.height, index: i, }; } else { return { minTop: cur.y < pre.minTop ? cur.y : pre.minTop, maxBottom: cur.y + cur.height > pre.maxBottom ? cur.y + cur.height : pre.maxBottom, index: i, }; } }, { minTop: 0, maxBottom: 0 } ); }) .map((r) => { r.height = r.maxBottom - r.minTop; return r; }); const maxGroupHeight = Math.max(...groupHeights.map((g) => g.height)); for (var i = 0; i < topGroups.length; i++) { const rects = topGroups[i] .filter((el) => el.type === \"rectangle\") .sort((lha, rha) => lha.y - rha.y); const groupWidth = groupHeights[i].height; if (groupWidth < maxGroupHeight) { const distance = maxGroupHeight - groupWidth; const perRectDistance = distance / rects.length; for (var j = 0; j < rects.length; j++) { const rect = rects[j]; rect.y = rect.y + perRectDistance * j; rect.height += perRectDistance; 1 2 3 4 5 6 7 8 9 10 11 // recalculate the position of the points const startBindingLines = allIndividualArrows.filter(el => (el.startBinding||{}).elementId === rect.id); for(startBindingLine of startBindingLines) { recalculateStartPointOfLine(startBindingLine, rect); } const endBindingLines = allIndividualArrows.filter(el => (el.endBinding||{}).elementId === rect.id); for(endBindingLine of endBindingLines) { recalculateEndPointOfLine(endBindingLine, rect); } } } } ea.copyViewElementsToEAforEditing(elements); await ea.addElementsToView(false, false); function recalculateStartPointOfLine(line, el) { const aX = el.x + el.width/2; const bX = line.x + line.points[1][0]; const aY = el.y + el.height/2; const bY = line.y + line.points[1][1]; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 line.startBinding.gap = 8; line.startBinding.focus = 0; const intersectA = ea.intersectElementWithLine( el, [bX, bY], [aX, aY], line.startBinding.gap ); if(intersectA.length > 0) { line.points[0] = [0, 0]; for(var i = 1; i 0) { line.points[line.points.length - 1] = [intersectA[0][0] - line.x, intersectA[0][1] - line.y]; } }","title":"Expand rectangles vertically"},{"location":"images/onepage/Scripts/Downloaded/Fixed%20spacing/","text":"/* Download this file and save to your Obsidian Vault including the first line, or open it in \"Raw\" and copy the entire contents to Obsidian. The script arranges the selected elements horizontally with a fixed spacing. When we create an architecture diagram or mind map, we often need to arrange a large number of elements in a fixed spacing. Fixed spacing and Fixed vertical Distance scripts can save us a lot of time. ```javascript */ if(!ea.verifyMinimumPluginVersion || !ea.verifyMinimumPluginVersion(\"1.5.21\")) { new Notice(\"This script requires a newer version of Excalidraw. Please install the latest version.\"); return; } settings = ea.getScriptSettings(); //set default values on first run if(!settings[\"Default spacing\"]) { settings = { \"Prompt for spacing?\": true, \"Default spacing\" : { value: 10, description: \"Fixed horizontal spacing between elements\" }, \"Remember last spacing?\": false }; ea.setScriptSettings(settings); } let spacingStr = settings[\"Default spacing\"].value.toString(); const rememberLastSpacing = settings[\"Remember last spacing?\"]; if(settings[\"Prompt for spacing?\"]) { spacingStr = await utils.inputPrompt(\"spacing?\",\"number\",spacingStr); } const spacing = parseInt(spacingStr); if(isNaN(spacing)) { return; } if(rememberLastSpacing) { settings[\"Default spacing\"].value = spacing; ea.setScriptSettings(settings); } const elements=ea.getViewSelectedElements(); const topGroups = ea.getMaximumGroups(elements) .filter(els => !(els.length === 1 && els[0].type ===\"arrow\")) // ignore individual arrows .filter(els => !(els.length === 1 && (els[0].containerId))); // ignore text in stickynote const groups = topGroups.sort((lha,rha) => lha[0].x - rha[0].x); for(var i=0; i 0) { const preGroup = groups[i-1]; const curGroup = groups[i]; 1 2 3 4 5 6 7 8 const preRight = Math.max(...preGroup.map(el => el.x + el.width)); const curLeft = Math.min(...curGroup.map(el => el.x)); const distance = curLeft - preRight - spacing; for(const curEl of curGroup) { curEl.x = curEl.x - distance; } } } ea.copyViewElementsToEAforEditing(elements); await ea.addElementsToView(false, false);","title":"Fixed spacing"},{"location":"images/onepage/Scripts/Downloaded/Fixed%20vertical%20distance/","text":"/* Download this file and save to your Obsidian Vault including the first line, or open it in \"Raw\" and copy the entire contents to Obsidian. The script arranges the selected elements vertically with a fixed spacing. When we create an architecture diagram or mind map, we often need to arrange a large number of elements in a fixed spacing. Fixed spacing and Fixed vertical Distance scripts can save us a lot of time. ```javascript */ if(!ea.verifyMinimumPluginVersion || !ea.verifyMinimumPluginVersion(\"1.5.21\")) { new Notice(\"This script requires a newer version of Excalidraw. Please install the latest version.\"); return; } settings = ea.getScriptSettings(); //set default values on first run if(!settings[\"Default spacing\"]) { settings = { \"Prompt for spacing?\": true, \"Default spacing\" : { value: 10, description: \"Fixed vertical spacing between elements\" }, \"Remember last spacing?\": false }; ea.setScriptSettings(settings); } let spacingStr = settings[\"Default spacing\"].value.toString(); const rememberLastSpacing = settings[\"Remember last spacing?\"]; if(settings[\"Prompt for spacing?\"]) { spacingStr = await utils.inputPrompt(\"spacing?\",\"number\",spacingStr); } const spacing = parseInt(spacingStr); if(isNaN(spacing)) { return; } if(rememberLastSpacing) { settings[\"Default spacing\"].value = spacing; ea.setScriptSettings(settings); } const elements=ea.getViewSelectedElements(); const topGroups = ea.getMaximumGroups(elements) .filter(els => !(els.length === 1 && els[0].type ===\"arrow\")) // ignore individual arrows .filter(els => !(els.length === 1 && (els[0].containerId))); // ignore text in stickynote const groups = topGroups.sort((lha,rha) => lha[0].y - rha[0].y); for(var i=0; i 0) { const preGroup = groups[i-1]; const curGroup = groups[i]; 1 2 3 4 5 6 7 8 const preBottom = Math.max(...preGroup.map(el => el.y + el.height)); const curTop = Math.min(...curGroup.map(el => el.y)); const distance = curTop - preBottom - spacing; for(const curEl of curGroup) { curEl.y = curEl.y - distance; } } } ea.copyViewElementsToEAforEditing(elements); await ea.addElementsToView(false, false);","title":"Fixed vertical distance"},{"location":"images/onepage/Scripts/Downloaded/Modify%20stroke%20width%20of%20selected%20elements/","text":"/* Download this file and save to your Obsidian Vault including the first line, or open it in \"Raw\" and copy the entire contents to Obsidian. This script will set the stroke width of selected elements. This is helpful, for example, when you scale freedraw sketches and want to reduce or increase their line width. See documentation for more details: https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html ```javascript */ let width = (ea.getViewSelectedElement().strokeWidth??1).toString(); width = await utils.inputPrompt(\"Width?\",\"number\",width); const elements=ea.getViewSelectedElements(); ea.copyViewElementsToEAforEditing(elements); ea.getElements().forEach((el)=>el.strokeWidth=width); ea.addElementsToView();","title":"Modify stroke width of selected elements"},{"location":"images/onepage/Scripts/Downloaded/Normalize%20Selected%20Arrows/","text":"/* This script will reset the start and end positions of the selected arrows. The arrow will point to the center of the connected box and will have a gap of 8px from the box. Tips: If you are drawing a flowchart, you can use Normalize Selected Arrows script to correct the position of the start and end points of the arrows, then use Elbow connectors script, and you will get the perfect connecting line! ```javascript */ if(!ea.verifyMinimumPluginVersion || !ea.verifyMinimumPluginVersion(\"1.5.21\")) { new Notice(\"This script requires a newer version of Excalidraw. Please install the latest version.\"); return; } settings = ea.getScriptSettings(); //set default values on first run if(!settings[\"Gap\"]) { settings = { \"Gap\" : { value: 8, description: \"The value of the gap between the connection line and the element, which must be greater than 0. If you want the connector to be next to the element, set it to 1.\" } }; ea.setScriptSettings(settings); } let gapValue = settings[\"Gap\"].value; const selectedIndividualArrows = ea.getMaximumGroups(ea.getViewSelectedElements()) .reduce((result, group) => (group.length === 1 && (group[0].type === 'arrow' || group[0].type === 'line')) ? [...result, group[0]] : result, []); const allElements = ea.getViewElements(); for(const arrow of selectedIndividualArrows) { const startBindingEl = allElements.filter(el => el.id === (arrow.startBinding||{}).elementId)[0]; const endBindingEl = allElements.filter(el => el.id === (arrow.endBinding||{}).elementId)[0]; 1 2 3 4 5 6 if(startBindingEl) { recalculateStartPointOfLine(arrow, startBindingEl, endBindingEl, gapValue); } if(endBindingEl) { recalculateEndPointOfLine(arrow, endBindingEl, startBindingEl, gapValue); } } ea.copyViewElementsToEAforEditing(selectedIndividualArrows); await ea.addElementsToView(false,false); function recalculateStartPointOfLine(line, el, elB, gapValue) { const aX = el.x + el.width/2; const bX = (line.points.length <=2 && elB) ? elB.x + elB.width/2 : line.x + line.points[1][0]; const aY = el.y + el.height/2; const bY = (line.points.length <=2 && elB) ? elB.y + elB.height/2 : line.y + line.points[1][1]; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 line.startBinding.gap = gapValue; line.startBinding.focus = 0; const intersectA = ea.intersectElementWithLine( el, [bX, bY], [aX, aY], line.startBinding.gap ); if(intersectA.length > 0) { line.points[0] = [0, 0]; for(var i = 1; i 0) { line.points[line.points.length - 1] = [intersectA[0][0] - line.x, intersectA[0][1] - line.y]; } }","title":"Normalize Selected Arrows"},{"location":"images/onepage/Scripts/Downloaded/Organic%20Line/","text":"/* Converts selected freedraw lines such that pencil pressure will decrease from maximum to minimum from the beginning of the line to its end. The resulting line is placed at the back of the layers, under all other items. Helpful when drawing organic mindmaps. ```javascript */ let elements = ea.getViewSelectedElements().filter((el)=>[\"freedraw\",\"line\",\"arrow\"].includes(el.type)); if(elements.length === 0) { elements = ea.getViewSelectedElements(); const len = elements.length; if(len === 0 || [\"freedraw\",\"line\",\"arrow\"].includes(elements[len].type)) { return; } elements = [elements[len]]; } elements.forEach((el)=>{ el.simulatePressure = false; el.type = \"freedraw\"; el.pressures = []; const len = el.points.length; for(i=0;i ea.moveViewElementToZIndex(el.id,0));","title":"Organic Line"},{"location":"images/onepage/Scripts/Downloaded/Repeat%20Elements/","text":"/* Download this file and save to your Obsidian Vault including the first line, or open it in \"Raw\" and copy the entire contents to Obsidian. This script will detect the difference between 2 selected elements, including position, size, angle, stroke and background color, and create several elements that repeat these differences based on the number of repetitions entered by the user. See documentation for more details: https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html ```javascript */ let repeatNum = parseInt(await utils.inputPrompt(\"repeat times?\",\"number\",\"5\")); if(!repeatNum) { new Notice(\"Please enter a number.\"); return; } const selectedElements = ea.getViewSelectedElements().sort((lha,rha) => lha.x === rha.x? (lha.y === rha.y? (lha.width === rha.width? (lha.height - rha.height) : lha.width - rha.width) : lha.y - rha.y) : lha.x - rha.x); if(selectedElements.length !== 2) { new Notice(\"Please select 2 elements.\"); return; } if(selectedElements[0].type !== selectedElements[1].type) { new Notice(\"The selected elements must be of the same type.\"); return; } const xDistance = selectedElements[1].x - selectedElements[0].x; const yDistance = selectedElements[1].y - selectedElements[0].y; const widthDistance = selectedElements[1].width - selectedElements[0].width; const heightDistance = selectedElements[1].height - selectedElements[0].height; const angleDistance = selectedElements[1].angle - selectedElements[0].angle; const bgColor1 = ea.colorNameToHex(selectedElements[0].backgroundColor); const rgbBgColor1 = parseColorString(bgColor1); const bgColor2 = ea.colorNameToHex(selectedElements[1].backgroundColor); const rgbBgColor2 = parseColorString(bgColor2); let bgHDistance = 0; let bgSDistance = 0; let bgLDistance = 0; if(rgbBgColor1 && rgbBgColor2) { const bgHsl1 = ea.rgbToHsl([rgbBgColor1.value[0], rgbBgColor1.value[1], rgbBgColor1.value[2]]); const bgHsl2 = ea.rgbToHsl([rgbBgColor2.value[0], rgbBgColor2.value[1], rgbBgColor2.value[2]]); 1 2 3 bgHDistance = bgHsl2[0] - bgHsl1[0]; bgSDistance = bgHsl2[1] - bgHsl1[1]; bgLDistance = bgHsl2[2] - bgHsl1[2]; } const strokeColor1 = ea.colorNameToHex(selectedElements[0].strokeColor); const rgbStrokeColor1 = parseColorString(strokeColor1); const strokeColor2 = ea.colorNameToHex(selectedElements[1].strokeColor); const rgbStrokeColor2 = parseColorString(strokeColor2); let strokeHDistance = 0; let strokeSDistance = 0; let strokeLDistance = 0; if(rgbStrokeColor1 && rgbStrokeColor2) { const strokeHsl1 = ea.rgbToHsl([rgbStrokeColor1.value[0], rgbStrokeColor1.value[1], rgbStrokeColor1.value[2]]); const strokeHsl2 = ea.rgbToHsl([rgbStrokeColor2.value[0], rgbStrokeColor2.value[1], rgbStrokeColor2.value[2]]); 1 2 3 strokeHDistance = strokeHsl2[0] - strokeHsl1[0]; strokeSDistance = strokeHsl2[1] - strokeHsl1[1]; strokeLDistance = strokeHsl2[2] - strokeHsl1[2]; } ea.copyViewElementsToEAforEditing(selectedElements); for(let i=0; i = 0 && newHeight >= 0) { if(newEl.type === 'arrow' || newEl.type === 'line' || newEl.type === 'freedraw') { const minX = Math.min(...newEl.points.map(pt => pt[0])); const minY = Math.min(...newEl.points.map(pt => pt[1])); for(let j = 0; j < newEl.points.length; j++) { if(newEl.points[j][0] > minX) { newEl.points[j][0] = newEl.points[j][0] + ((newEl.points[j][0] - minX) / originWidth) * (newWidth - originWidth); } if(newEl.points[j][1] > minY) { newEl.points[j][1] = newEl.points[j][1] + ((newEl.points[j][1] - minY) / originHeight) * (newHeight - originHeight); } } } else { newEl.width = newWidth; newEl.height = newHeight; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 if(rgbBgColor1 && rgbBgColor2) { const bgHsl2 = ea.rgbToHsl([rgbBgColor2.value[0], rgbBgColor2.value[1], rgbBgColor2.value[2]]); const newBgH = bgHsl2[0] + bgHDistance * (i + 1); const newBgS = bgHsl2[1] + bgSDistance * (i + 1); const newBgL = bgHsl2[2] + bgLDistance * (i + 1); if(newBgH >= 0 && newBgH <= 360 && newBgS >= 0 && newBgS <= 100 && newBgL >= 0 && newBgL <= 100) { const newBgRgb = ea.hslToRgb([newBgH, newBgS, newBgL]); newEl.backgroundColor = rgbColorToString(newBgRgb, rgbBgColor1.model); } } if(rgbStrokeColor1 && rgbStrokeColor2) { const strokeHsl2 = ea.rgbToHsl([rgbStrokeColor2.value[0], rgbStrokeColor2.value[1], rgbStrokeColor2.value[2]]); const newStrokeH = strokeHsl2[0] + strokeHDistance * (i + 1); const newStrokeS = strokeHsl2[1] + strokeSDistance * (i + 1); const newStrokeL = strokeHsl2[2] + strokeLDistance * (i + 1); if(newStrokeH >= 0 && newStrokeH <= 360 && newStrokeS >= 0 && newStrokeS <= 100 && newStrokeL >= 0 && newStrokeL <= 100) { const newStrokeRgb = ea.hslToRgb([newStrokeH, newStrokeS, newStrokeL]); newEl.strokeColor = rgbColorToString(newStrokeRgb, rgbStrokeColor1.model); } } } await ea.addElementsToView(false, false, true); function parseColorString(string) { var prefix = string.substring(0, 3).toLowerCase(); var val; var model; switch (prefix) { case 'hsl': val = ea.hslToRgb(parseHslColorString(string)); model = 'hsl'; break; case 'hwb': val = hwbToRgb(parseHwbColorString(string)); model = 'hwb'; break; default: val = parseRgbColorString(string); model = 'rgb'; break; } 1 2 3 4 5 if (!val) { return null; } return {model: model, value: val}; }; function parseRgbColorString(string) { if (!string) { return null; } var colorNames={}; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 var abbr = /^#([a-f0-9]{3,4})$/i; var hex = /^#([a-f0-9]{6})([a-f0-9]{2})?$/i; var rgba = /^rgba?\\(\\s*([+-]?\\d+)(?=[\\s,])\\s*(?:,\\s*)?([+-]?\\d+)(?=[\\s,])\\s*(?:,\\s*)?([+-]?\\d+)\\s*(?:[,|\\/]\\s*([+-]?[\\d\\.]+)(%?)\\s*)?\\)$/; var per = /^rgba?\\(\\s*([+-]?[\\d\\.]+)\\%\\s*,?\\s*([+-]?[\\d\\.]+)\\%\\s*,?\\s*([+-]?[\\d\\.]+)\\%\\s*(?:[,|\\/]\\s*([+-]?[\\d\\.]+)(%?)\\s*)?\\)$/; var keyword = /^(\\w+)$/; var rgb = [0, 0, 0, 1]; var match; var i; var hexAlpha; if (match = string.match(hex)) { hexAlpha = match[2]; match = match[1]; for (i = 0; i < 3; i++) { var i2 = i * 2; rgb[i] = parseInt(match.slice(i2, i2 + 2), 16); } if (hexAlpha) { rgb[3] = parseInt(hexAlpha, 16) / 255; } } else if (match = string.match(abbr)) { match = match[1]; hexAlpha = match[3]; for (i = 0; i < 3; i++) { rgb[i] = parseInt(match[i] + match[i], 16); } if (hexAlpha) { rgb[3] = parseInt(hexAlpha + hexAlpha, 16) / 255; } } else if (match = string.match(rgba)) { for (i = 0; i < 3; i++) { rgb[i] = parseInt(match[i + 1], 0); } if (match[4]) { if (match[5]) { rgb[3] = parseFloat(match[4]) * 0.01; } else { rgb[3] = parseFloat(match[4]); } } } else if (match = string.match(per)) { for (i = 0; i < 3; i++) { rgb[i] = Math.round(parseFloat(match[i + 1]) * 2.55); } if (match[4]) { if (match[5]) { rgb[3] = parseFloat(match[4]) * 0.01; } else { rgb[3] = parseFloat(match[4]); } } } else if (match = string.match(keyword)) { if (match[1] === 'transparent') { return [0, 0, 0, 0]; } if (!hasOwnProperty.call(colorNames, match[1])) { return null; } rgb = colorNames[match[1]]; rgb[3] = 1; return rgb; } else { return null; } for (i = 0; i < 3; i++) { rgb[i] = clamp(rgb[i], 0, 255); } rgb[3] = clamp(rgb[3], 0, 1); return rgb; } function parseHslColorString(string) { if (!string) { return null; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 var hsl = /^hsla?\\(\\s*([+-]?(?:\\d{0,3}\\.)?\\d+)(?:deg)?\\s*,?\\s*([+-]?[\\d\\.]+)%\\s*,?\\s*([+-]?[\\d\\.]+)%\\s*(?:[,|\\/]\\s*([+-]?(?=\\.\\d|\\d)(?:0|[1-9]\\d*)?(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)\\s*)?\\)$/; var match = string.match(hsl); if (match) { var alpha = parseFloat(match[4]); var h = ((parseFloat(match[1]) % 360) + 360) % 360; var s = clamp(parseFloat(match[2]), 0, 100); var l = clamp(parseFloat(match[3]), 0, 100); var a = clamp(isNaN(alpha) ? 1 : alpha, 0, 1); return [h, s, l, a]; } return null; } function parseHwbColorString(string) { if (!string) { return null; } 1 2 3 4 5 6 7 8 9 10 11 12 13 var hwb = /^hwb\\(\\s*([+-]?\\d{0,3}(?:\\.\\d+)?)(?:deg)?\\s*,\\s*([+-]?[\\d\\.]+)%\\s*,\\s*([+-]?[\\d\\.]+)%\\s*(?:,\\s*([+-]?(?=\\.\\d|\\d)(?:0|[1-9]\\d*)?(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)\\s*)?\\)$/; var match = string.match(hwb); if (match) { var alpha = parseFloat(match[4]); var h = ((parseFloat(match[1]) % 360) + 360) % 360; var w = clamp(parseFloat(match[2]), 0, 100); var b = clamp(parseFloat(match[3]), 0, 100); var a = clamp(isNaN(alpha) ? 1 : alpha, 0, 1); return [h, w, b, a]; } return null; } function rgbColorToString(color, model) { switch (model) { case 'hsl': return rgbColorToHslString(color); case 'hwb': return rgbColorToHwbString(color); default: return ea.rgbToHexString(color); } } function rgbColorToHslString(rgb) { var hsl = ea.rgbToHsl(rgb); return 'hsl(' + hsl[0] + ', ' + hsl[1] + '%, ' + hsl[2] + '%)' }; function rgbColorToHwbString(rgb) { var hwb = rgbToHwb(rgb); 1 return 'hwb(' + hwb[0] + ', ' + hwb[1] + '%, ' + hwb[2] + '%)'; }; function rgbToHwb(rgb) { const r = rgb[0]; const g = rgb[1]; let b = rgb[2]; const h = convert.rgb.hsl(rgb)[0]; const w = 1 / 255 * Math.min(r, Math.min(g, b)); 1 2 3 b = 1 - 1 / 255 * Math.max(r, Math.max(g, b)); return [h, w * 100, b * 100]; }; function hwbToRgb(hwb) { const h = hwb[0] / 360; let wh = hwb[1] / 100; let bl = hwb[2] / 100; const ratio = wh + bl; let f; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 // Wh + bl cant be > 1 if (ratio > 1) { wh /= ratio; bl /= ratio; } const i = Math.floor(6 * h); const v = 1 - bl; f = 6 * h - i; if ((i & 0x01) !== 0) { f = 1 - f; } const n = wh + f * (v - wh); let r; let g; let b; switch (i) { default: case 6: case 0: r = v; g = n; b = wh; break; case 1: r = n; g = v; b = wh; break; case 2: r = wh; g = v; b = n; break; case 3: r = wh; g = n; b = v; break; case 4: r = n; g = wh; b = v; break; case 5: r = v; g = wh; b = n; break; } return [r * 255, g * 255, b * 255]; } function clamp(num, min, max) { return Math.min(Math.max(min, num), max); }","title":"Repeat Elements"},{"location":"images/onepage/Scripts/Downloaded/Set%20Dimensions/","text":"/* Download this file and save to your Obsidian Vault including the first line, or open it in \"Raw\" and copy the entire contents to Obsidian. Currently there is no way to specify the exact location and size of objects in Excalidraw. You can bridge this gap with the following simple script. See documentation for more details: https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html ```javascript */ const elements = ea.getViewSelectedElements(); if(elements.length === 0) return; const el = ea.getLargestElement(elements); const sizeIn = [ Math.round(el.x), Math.round(el.y), Math.round(el.width), Math.round(el.height) ].join(\",\"); let res = await utils.inputPrompt(\"x,y,width,height?\",null,sizeIn); res = res.split(\",\"); if(res.length !== 4) return; let size = []; for (v of res) { const i = parseInt(v); if(isNaN(i)) return; size.push(i); } el.x = size[0]; el.y = size[1]; el.width = size[2]; el.height = size[3]; ea.copyViewElementsToEAforEditing([el]); ea.addElementsToView(false,false);","title":"Set Dimensions"},{"location":"images/onepage/Scripts/Downloaded/Set%20Text%20Alignment/","text":"/* Sets text alignment of text block (cetner, right, left). Useful if you want to set a keyboard shortcut for selecting text alignment. See documentation for more details: https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html ```javascript */ elements = ea.getViewSelectedElements().filter((el)=>el.type===\"text\"); if(elements.length===0) return; let align = [\"left\",\"right\",\"center\"]; align = await utils.suggester(align,align); elements.forEach((el)=>el.textAlign = align); ea.copyViewElementsToEAforEditing(elements); ea.addElementsToView(false,false);","title":"Set Text Alignment"},{"location":"images/onepage/Scripts/Downloaded/Split%20text%20by%20lines/","text":"/* requires Excalidraw 1.5.1 or higher Download this file and save to your Obsidian Vault including the first line, or open it in \"Raw\" and copy the entire contents to Obsidian. Split lines of text into separate text elements for easier reorganization See documentation for more details: https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html ```javascript / elements = ea.getViewSelectedElements().filter((el)=>el.type===\"text\"); elements.forEach((el)=>{ ea.style.strokeColor = el.strokeColor; ea.style.fontFamily = el.fontFamily; ea.style.fontSize = el.fontSize; const text = el.text.split(\"\\n\"); for(i=0;iel.type===\"text\"); elements.forEach((el)=>{ ea.style.strokeColor = el.strokeColor; ea.style.fontFamily = el.fontFamily; ea.style.fontSize = el.fontSize; const text = el.text.split(\"\\n\"); for(i=0;i { app.workspace.off(EVENT,window.brainGraphEventHandler); if(isBoolean(window.excalidrawView?.linksAlwaysOpenInANewPane)) { window.excalidrawView.linksAlwaysOpenInANewPane = false; } const ea = ExcalidrawAutomate; ea.setView(window.excalidrawView); if(ea.targetView?.excalidrawAPI) { try { ea.targetView.semaphores.saving = false; ea.getExcalidrawAPI().updateScene({appState:{viewModeEnabled:false}}); } catch {} } delete window.excalidrawView; delete window.excalidrawFile; delete window.lastfilePath; new Notice(\"Brain Graph Off\") setTimeout(()=>delete window.brainGraphEventHandler); } //Turn off event handler if it is already running if(window.brainGraphEventHandler) { removeEventHandler(); return; } //------------------------------------------------------- // Load Settings //------------------------------------------------------- let saveSettings = false; settings = ea.getScriptSettings(); //set default values on first run if(!settings[\"Max number of nodes/domain\"]) { settings = { \"Confirmation prompt at startup\": { value: true, description: \"Prompt me to confirm starting of the script because \" + \"it will overwrite the current active drawing. \" + \"You can disable this warning by turning off this switch\" }, \"Max number of nodes/domain\": { value: 30, description: \"Maximum number of items to show in each domain: parents, children, siblings, jumps.\" }, \"Infer non-Breadcrumbs links\": { value: true, description: \"Links on the page are children, backlinks to the page are \" + \"parents. Breadcrumbs take priority. Inferred nodes have a dashed border.\" }, \"Hide attachments\": { value: true, description: \"Hide attachments. Will only have an effect if Infer non-Breadcrumbs links is turned on.\" }, \"Font family\": { value: \"Code\", valueset: [\"Hand-drawn\",\"Normal\",\"Code\",\"Fourth (custom) Font\"] }, \"Stroke roughness\": { value: \"Architect\", valueset: [\"Architect\", \"Artist\", \"Cartoonist\"] }, \"Rectangle stroke sharpness\": { value: \"round\", valueset: [\"sharp\", \"round\"] }, \"Central font size\": { value: 30, description: \"Font size of the central node\" }, \"Font size\": { value: 20, description: \"Font size of jumps, children and parents\" }, \"Siblings font size\": { value: 15, description: \"Font size of siblings\" }, \"Max label length\": { value: 30, description: \"Maximum number of characters to display from node title. Longer nodes will end with '...'\" }, \"Padding\": { value: 10, description: \"Padding of the node rectangle\" }, \"Gate offset\": { value: 15, description: \"The offset to the left and right of the parent and child gates.\" }, \"Gate radius\": { value: 5, description: \"The radius of the 3 small circles (alias: gates) serving as connection points for nodes\" }, \"Canvas color\": { value: \"hsl(208, 80%, 23%)\", description: \"Any legal HTML color (#000000, rgb, color-name, etc.).\" }, \"Gate color\": { value: \"white\", description: \"Any legal HTML color (#000000, rgb, color-name, etc.).\" }, \"Link color\": { value: \"hsl(0, 0%, 41%)\", description: \"Any legal HTML color (#000000, rgb, color-name, etc.).\" }, \"Central-node background color\": { value: \"#C49A13\", description: \"Any legal HTML color (#000000, rgb, color-name, etc.).\" }, \"Central-node color\": { value: \"black\", description: \"Any legal HTML color (#000000, rgb, color-name, etc.).\" }, \"Breadcrumbs-node background color\": { value: \"rgba(0,0,0,0.4)\", description: \"Any legal HTML color (#000000, rgb, color-name, etc.).\" }, \"Breadcrumbs-node color\": { value: \"white\", description: \"Any legal HTML color (#000000, rgb, color-name, etc.).\" }, \"Non-breadcrumbs-node background color\": { value: \"rgba(0,0,5,0.7)\", description: \"Any legal HTML color (#000000, rgb, color-name, etc.).\" }, \"Non-breadcrumbs-node color\": { value: \"hsl(208, 80%, 77%)\", description: \"Any legal HTML color (#000000, rgb, color-name, etc.).\" }, \"Virtual-node background color\": { value: \"rgba(255,0,0,0.4)\", description: \"Any legal HTML color (#000000, rgb, color-name, etc.).\" }, \"Virtual-node color\": { value: \"white\", description: \"Any legal HTML color (#000000, rgb, color-name, etc.).\" } }; saveSettings = true; } if(!settings[\"Display alias if available\"]) { settings[\"Display alias if available\"] = { value: true, description: \"Displays the page alias instead of the \" + \"filename if it is specified in the page's front matter. \" }; saveSettings = true; } if(!settings[\"Graph settings JSON\"]) { settings[\"Graph settings JSON\"] = { height: \"450px\", value: {\\n \"breadcrumbs\": {\\n \"down\": [\"children\", \"child\"],\\n \"up\": [\"parents\", \"parent\"],\\n \"jump\": [\"jump\", \"jumps\"]\\n },\\n \"tags\": {\\n \"#excalidraw\": {\\n \"nodeColor\": \"hsl(59, 80%, 77%)\",\\n \"gateColor\": \"#fd7e14\",\\n \"borderColor\": \"black\",\\n \"backgroundColor\": \"rgba(50,50,50,0.5)\",\\n \"prefix\": \"\ud83c\udfa8 \"\\n },\\n \"#dnp\": {\\n \"prefix\": \"\ud83d\uddd3 \"\\n }\\n }\\n} , description: This may contain two elements:
      1. A specification of your breadcrumbs hierarchy. Note, that if you have the Breadcrumbs plugin installed and enabled then TheBrain-navigation script will take your hierarchy settings from Breadcrumbs. If Breadcrumbs is disabled, this specification will be used.
      2. Formatting of nodes based on page tags. You can specify special formatting rules for tags. If multiple tags are present on the page the first matching a specification will be used. You may provide partial specifications as well. e.g. if you only specify prefix, the other attributes will follow your default settings.
      { "breadcrumbs": { "down": ["children", "child"], "up": ["parents", "parent"], "jump": ["jump", "jumps"] }, "tags": { "#excalidraw": { "nodeColor": "hsl(59, 80%, 77%)", "gateColor": "#fd7e14", "borderColor": "black", "backgroundColor": "rgba(50,50,50,0.5)", "prefix": "\ud83c\udfa8 " }, "#dnp": { "prefix": "\ud83d\uddd3 " } } } 
      }; saveSettings = true; } if(saveSettings) { ea.setScriptSettings(settings); } const SHOW_CONFIRMATION_PROMPT = settings[\"Confirmation prompt at startup\"].value; const MAX_ITEMS = Math.floor(settings[\"Max number of nodes/domain\"].value)??40; const INCLUDE_OBSIDIAN_LINKS = settings[\"Infer non-Breadcrumbs links\"].value; const HIDE_ATTACHMENTS = settings[\"Hide attachments\"].value; const FONT_FAMILY = settings[\"Font family\"].value === \"Hand-drawn\" ? 1 : settings[\"Font family\"].value === \"Normal\" ? 2 : settings[\"Font family\"].value === \"Code\" ? 3 : 4; const STROKE_ROUGHNESS = settings[\"Stroke roughness\"].value === \"Architect\" ? 0 : settings[\"Stroke roughness\"].value === \"Artist\" ? 1 : 2; const STROKE_SHARPNESS = settings[\"Rectangle stroke sharpness\"].value; const CENTRAL_FONT_SIZE = Math.floor(settings[\"Central font size\"].value)??30; const FONT_SIZE = Math.floor(settings[\"Font size\"].value)??20; const DISTANT_FONT_SIZE = Math.floor(settings[\"Siblings font size\"].value)??15; const MAX_LABEL_LENGTH = Math.floor(settings[\"Max label length\"].value)??30; const PADDING = Math.floor(settings[\"Padding\"].value)??10; const GATE_OFFSET = Math.floor(settings[\"Gate offset\"].value)??15; const GATE_RADIUS = Math.floor(settings[\"Gate radius\"].value)??5; const BG_COLOR = settings[\"Canvas color\"].value; const GATE_COLOR = settings[\"Gate color\"].value; const LINK_COLOR = settings[\"Link color\"].value; const CENTRAL_NODE_BG_COLOR = settings[\"Central-node background color\"].value; const CENTRAL_NODE_COLOR = settings[\"Central-node color\"].value; const NODE_BG_COLOR = settings[\"Breadcrumbs-node background color\"].value; const NODE_COLOR = settings[\"Breadcrumbs-node color\"].value; const OBSIDIAN_NODE_BG_COLOR = settings[\"Non-breadcrumbs-node background color\"].value; const OBSIDIAN_NODE_COLOR = settings[\"Non-breadcrumbs-node color\"].value; const VIRTUAL_NODE_BG_COLOR = settings[\"Virtual-node background color\"].value; const VIRTUAL_NODE_COLOR = settings[\"Virtual-node color\"].value; const USE_ALIAS = settings[\"Display alias if available\"].value; let formattingJSON = {}; try { formattingJSON = JSON.parse(settings[\"Graph settings JSON\"].value); } catch (e) { new Notice(\"Error reading graph settings JSON, see developer console for more information\",4000); console.log(e); }; const NODE_FORMATTING = formattingJSON?.tags??{}; const FORMATTED_TAGS = Object.keys(NODE_FORMATTING); //------------------------------------------------------- // Load breadcrumbs hierarchies const HIERARCHIES = new Map(); if(window.BCAPI) { //read breadcrumbs if available const getHierarchyFields = (direction) => { const values = new Set(direction); direction.forEach( d => BCAPI.plugin.settings.userHiers.forEach( h => h[d].forEach( x => values.add(x) ) ) ); return Array.from(values); } HIERARCHIES.set(\"down\",getHierarchyFields([\"down\"])); HIERARCHIES.set(\"up\",getHierarchyFields([\"up\"])); HIERARCHIES.set(\"jump\",getHierarchyFields([\"prev\",\"next\"])); } else { HIERARCHIES.set(\"down\",formattingJSON?.breadcrumbs?.down??[\"down\",\"child\",\"children\"]); HIERARCHIES.set(\"up\",formattingJSON?.breadcrumbs?.up??[\"up\", \"parent\",\"parents\"]); HIERARCHIES.set(\"jump\",formattingJSON?.breadcrumbs?.jump??[\"jump\", \"jumps\", \"next\", \"previous\"]); } //------------------------------------------------------- // Initialization //------------------------------------------------------- if(SHOW_CONFIRMATION_PROMPT) { const result = await utils.inputPrompt( \"This will overwrite the current active drawing\", \"type: 'ok' to Continue\" ); if(result !== \"ok\") return; } const measureText = (text,fontSize) => { ea.style.fontSize = fontSize; return ea.measureText(text); } ea.style.fontFamily = FONT_FAMILY; const TEXT_SIZE = measureText(\"m\".repeat(MAX_LABEL_LENGTH+3),FONT_SIZE); const NODE_WIDTH = TEXT_SIZE.width + 3 * PADDING; const NODE_HEIGHT = 2 * (TEXT_SIZE.height + 2 * PADDING); ea.getExcalidrawAPI().updateScene({ appState: { viewModeEnabled:true, theme: \"light\", viewBackgroundColor: BG_COLOR }, elements:[] }); ea.style.strokeColor = NODE_COLOR; ea.addText(0,0,\"Open a document in another pane and click it to get started.\\n\\n\" + \"For the best experience enable 'Open in adjacent pane'\\nin Excalidraw settings \" + \"under 'Links and Transclusion'.\", {textAlign:\"center\"}); await ea.addElementsToView(); ea.getExcalidrawAPI().zoomToFit(); window.excalidrawView = ea.targetView; window.excalidrawFile = ea.targetView.file; ea.targetView.linksAlwaysOpenInANewPane = true; new Notice(\"Brain Graph On\"); //------------------------------------------------------- // Supporting functions and classes //------------------------------------------------------- const getFilenameFromPath = (path) => { const mdFile = path.endsWith(\".md\"); const filename = path.substring(path.lastIndexOf(\"/\")+1); return mdFile ? filename.slice(0,-3) : filename; } const getExtension = (path) => { const lastDot = path.lastIndexOf(\".\"); if(lastDot === -1) return \"md\"; return path.slice(lastDot+1); } const distinct = (data) => Array.from(new Set(data)); class Layout { constructor(spec) { this.spec = spec; this.nodes = []; this.renderedNodes = []; } layout(columns = this.spec.columns) { const generateLayoutVector = (pattern) => { const res = []; let cur = 1; let state = true; pattern .map(p => Math.floor(p)) .forEach(cnt => { for(let i=0;i items%2 ? generateLayoutVector([(columns-items)/2,items,(columns-items)/2]) : generateLayoutVector([(columns-items)/2,items/2,1,items/2,(columns-items)/2]); const sortedNodes = this.nodes.sort((a,b) => a.label.toLowerCase() < b.label.toLowerCase() ? -1 : 1) const itemCount = sortedNodes.length; if(itemCount === 0) return; const rowCount = Math.ceil(itemCount / columns); this.renderedNodes = Array(rowCount).fill().map(( ,i) => (i+1 < rowCount) || (itemCount % columns === 0) ? Array(columns).fill().map(( ,j) => sortedNodes[i columns+j]) //full row : getRowLayout(itemCount % columns).map(idx => idx ? sortedNodes[i columns+idx-1]:null)); } render() { this.layout(); const rows = this.renderedNodes.length; const height = rows * this.spec.rowHeight; const top = (this.spec.top === null && this.spec.bottom === null) //unconstrained ? this.spec.origoY - height/2 : this.spec.top !== null ? (this.spec.origoY - height/2) < this.spec.top //top constrained ? this.spec.top : this.spec.origoY - height/2 : (this.spec.origoY + height/2) > this.spec.bottom //bottom constrained ? this.spec.bottom - height : this.spec.origoY - height/2; const center00 = { x: this.spec.origoX - (this.spec.columns === 1 ? 0 : (this.spec.columns-1)/2 this.spec.columnWidth), y: top }; this.renderedNodes.forEach((nodes,row) => nodes.forEach((node,idx) => { if(!node) return; node.setCenter({ x: center00.x + idx this.spec.columnWidth, y: center00.y + row*this.spec.rowHeight }); node.render(); }) ); } } class Node { constructor(spec) { const dvPage = spec.file ? DataviewAPI?.page(spec.file.path) : null; const tag = (dvPage?.file?.tags?.values??[]).filter(t=>FORMATTED_TAGS.some(x=>t.startsWith(x)))[0]; if(tag) { const format = NODE_FORMATTING[FORMATTED_TAGS.filter(x=>tag.startsWith(x))[0]]; spec.gateColor = format.gateColor ?? spec.gateColor; spec.backgroundColor = format.backgroundColor ?? spec.backgroundColor; spec.nodeColor = format.nodeColor ?? spec.nodeColor; spec.borderColor = format.borderColor ?? spec.borderColor; spec.prefix = format.prefix; } this.spec = spec; 1 2 3 4 5 6 7 8 9 10 11 12 const aliases = (spec.file && USE_ALIAS) ? (dvPage?.file?.aliases?.values??[]) : []; const label = (spec.prefix??\"\") + (aliases.length > 0 ? aliases[0] : (spec.file ? (spec.file.extension === \"md\" ? spec.file.basename : spec.file.name) : spec.nodeTitle)); this.label = label.length > spec.maxLabelLength ? label.substring(0,spec.maxLabelLength-1) + \"...\" : label; this.labelSize = measureText(this.label, spec.fontSize); } setCenter(center) { this.center = center; } render() { ea.style.fontSize = this.spec.fontSize; ea.style.strokeColor = this.spec.file ? this.spec.nodeColor : this.spec.virtualNodeColor; ea.style.backgroundColor = \"transparent\"; this.id = ea.addText( this.center.x - this.labelSize.width / 2, this.center.y - this.labelSize.height / 2, this.label, { wrapAt: this.spec.maxLabelLength+5, textAlign: \"center\", box: true, boxPadding: this.spec.padding } ); const box = ea.getElement(this.id); box.link = [[${this.spec.file?.path??this.spec.nodeTitle}]] ; box.backgroundColor = this.spec.file ? this.spec.backgroundColor : this.spec.virtualNodeBGColor; box.strokeColor = this.spec.borderColor; box.strokeStyle = this.spec.strokeStyle??\"solid\"; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ea.style.strokeColor = this.spec.gateColor; ea.style.backgroundColor = this.spec.hasJumps ? this.spec.gateColor : \"transparent\"; this.jumpGateId = ea.addEllipse( this.spec.jumpOnLeft ? this.center.x - this.spec.gateRadius * 2 - this.spec.padding - this.labelSize.width / 2 : this.center.x + this.spec.padding + this.labelSize.width / 2, this.center.y - this.spec.gateRadius, this.spec.gateRadius * 2, this.spec.gateRadius * 2 ); ea.style.backgroundColor = this.spec.hasParents ? this.spec.gateColor : \"transparent\"; this.parentGateId = ea.addEllipse( this.center.x - this.spec.gateRadius - this.spec.gateOffset, this.center.y - 2 * this.spec.gateRadius - this.spec.padding - this.labelSize.height / 2, this.spec.gateRadius * 2, this.spec.gateRadius * 2 ); ea.style.backgroundColor = this.spec.hasChildren ? this.spec.gateColor : \"transparent\"; this.childGateId = ea.addEllipse( this.center.x - this.spec.gateRadius + this.spec.gateOffset, this.center.y + this.spec.padding + this.labelSize.height / 2, this.spec.gateRadius * 2, this.spec.gateRadius * 2 ); ea.addToGroup([this.jumpGateId,this.parentGateId,this.childGateId,this.id, box.boundElements[0].id]); } } const addNodes = (nodesMap, root, nodes, layout, options) => { nodes.forEach(filePath => { const node = new Node({ nodeTitle: getFilenameFromPath(filePath), file: app.vault.getAbstractFileByPath(filePath), hasChildren: false, hasParents: false, hasJumps: false, ...options }); nodesMap.set(filePath,node); layout.nodes.push(node); }); } //------------------------------------------------------- // Breadcrumbs workaround to handle full filepath //------------------------------------------------------- const getPathOrSelf = (link,host) => { return (app.metadataCache.getFirstLinkpathDest(link,host)?.path)??link; } const readDVField = (field,file) => { const res = new Set(); if(field.values) { field.values.forEach(l=>{ if(l.type === \"file\") res.add(getPathOrSelf(l.path,file.path)); }); return Array.from(res); } if(field.path) { return [getPathOrSelf(field.path,file.path)]; } const m = field.matchAll(/[ []*[[([ #]|] )[^]] ]]/g); while(!(r=m.next()).done) { if(r.value[1]) { res.add(getPathOrSelf(r.value[1],file.path)); } } return Array.from(res); } const getDVFieldLinks = (page,dir,retSet=false) => { const fields = HIERARCHIES.get(dir); const links = new Set(); const processed = new Set(); fields.forEach(f => { if(page[f] && !processed.has(f)) { processed.add(f); readDVField(page[f],page.file).forEach(l=>links.add(l)) }; }); return retSet ? links : Array.from(links); } //------------------------------------------------------- // Event handler //------------------------------------------------------- window.brainGraphEventHandler = async (leaf) => { //sleep for 100ms to give time for leaf.view.file to be set await new Promise((resolve) => setTimeout(resolve, 100)); //------------------------------------------------------- //terminate event handler if view no longer exists or file has changed if(!window.excalidrawView?.file || window.excalidrawView.file.path !== window.excalidrawFile?.path) { removeEventHandler(); return; } if(!leaf?.view?.file) return; const rootFile = leaf.view.file; if (rootFile.path === window.excalidrawFile.path) return; //brainview drawing is active if(window.lastfilePath && window.lastfilePath === rootFile.path) return; //don't reload the file if it has not changed window.lastfilePath = rootFile.path; //------------------------------------------------------- //I must reinitialize ea because in the event handler the script engine ea will no longer be available ea = ExcalidrawAutomate; ea.reset(); ea.setView(window.excalidrawView); ea.style.fontFamily = FONT_FAMILY; ea.style.roughness = STROKE_ROUGHNESS; ea.style.strokeSharpness = STROKE_SHARPNESS; ea.getExcalidrawAPI().updateScene({elements:[]}); ea.style.verticalAlign = \"middle\"; ea.style.strokeSharpness = \"round\"; ea.style.fillStyle = \"solid\"; //------------------------------------------------------- //build list of nodes const dvPage = DataviewAPI.page(rootFile.path); //workaround because const parentsSet = dvPage ? getDVFieldLinks(dvPage,\"up\",true) : new Set(); parentsSet.delete(rootFile.path); const childrenSet = dvPage ? getDVFieldLinks(dvPage,\"down\",true) : new Set(); childrenSet.delete(rootFile.path); const jumpsSet = dvPage ? getDVFieldLinks(dvPage,\"jump\",true) : new Set(); jumpsSet.delete(rootFile.path); let backlinksSet = new Set( Object.keys((app.metadataCache.getBacklinksForFile(rootFile)?.data)??{}) .map(l=>app.metadataCache.getFirstLinkpathDest(l,rootFile.path)?.path??l) ); backlinksSet.forEach(l=>{ const page = DataviewAPI.page(l); if(page) { if(getDVFieldLinks(page,\"down\",true).has(rootFile.path)) { parentsSet.add(l); backlinksSet.delete(l); return; } if(getDVFieldLinks(page,\"up\",true).has(rootFile.path)) { childrenSet.add(l); backlinksSet.delete(l); return; } if(getDVFieldLinks(page,\"jump\", true).has(rootFile.path)) { jumpsSet.add(l); backlinksSet.delete(l); return; } } }) const parents = Array.from(parentsSet).slice(0,MAX_ITEMS); const children = Array.from(childrenSet).slice(0,MAX_ITEMS); const jumps = Array.from(jumpsSet).slice(0,MAX_ITEMS); //adding links from the document, not explicitly declared as a breadcrumb const forwardLinks = INCLUDE_OBSIDIAN_LINKS ? distinct(app.metadataCache .getLinks()[rootFile.path]?.map( l=>app.metadataCache.getFirstLinkpathDest(l.link.match(/[ #]*/)[0],rootFile.path)??l.link.match(/[ #]*/)[0] ).filter(f=>f && (!HIDE_ATTACHMENTS || (f.extension??getExtension(f)) === \"md\")) .map(f=>f.path??f) .filter(l=>!parentsSet.has(l) && !childrenSet.has(l) && !jumpsSet.has(l) && l!==rootFile.path && !backlinksSet.has(l)) .slice(0,Math.max(MAX_ITEMS-children.length)) ) : []; const forwardlinksSet = new Set(forwardLinks); const backLinks = INCLUDE_OBSIDIAN_LINKS ? Array.from(backlinksSet) .filter(l=>!parentsSet.has(l) && !childrenSet.has(l) && !jumpsSet.has(l) && l!==rootFile.path && !forwardlinksSet.has(l)) .slice(0,Math.max(MAX_ITEMS-parents.length)) : []; backlinksSet = new Set(backLinks); const sharedParents = INCLUDE_OBSIDIAN_LINKS ? backLinks.concat(parents) : parents; const siblings = distinct( sharedParents.map(p=>{ const page = DataviewAPI.page(p); return page ? getDVFieldLinks(page,\"down\") .filter(l=>!parentsSet.has(l) && !childrenSet.has(l) && !jumpsSet.has(l) && l!==rootFile.path && !forwardlinksSet.has(l) && !backlinksSet.has(l)) : []; }).flat() ).slice(0,MAX_ITEMS); const siblingsSet = new Set(siblings); const inverseInferredSiblingsMap = new Map(); const inferredSiblings = INCLUDE_OBSIDIAN_LINKS ? distinct( sharedParents.map(p=>{ f = app.vault.getAbstractFileByPath(p); //app.metadataCache.getFirstLinkpathDest(p,rootFile.path); if(!f) { return []; } const res = Object.keys((app.metadataCache.getBacklinksForFile(f)?.data)??{}) .map(l=>app.metadataCache.getFirstLinkpathDest(l,rootFile.path)?.path??l) .filter(l=>!parentsSet.has(l) && !childrenSet.has(l) && !jumpsSet.has(l) && l!==rootFile.path && !forwardlinksSet.has(l) && !backlinksSet.has(l) && !siblingsSet.has(l)); res.forEach(r=>inverseInferredSiblingsMap.set(r,p)); return res; }).flat() ).slice(0,Math.max(MAX_ITEMS-siblings.length)) : []; const inferredSiblingsSet = new Set(inferredSiblings); //------------------------------------------------------- // Generate layout and nodes const nodesMap = new Map(); const linksMap = new Map(); const lCenter = new Layout({ origoX: 0, origoY: 0, top: null, bottom: null, columns: 1, columnWidth: NODE_WIDTH, rowHeight: NODE_HEIGHT }); const manyChildren = (children.length + forwardLinks.length) >10; const manySiblings = (siblings.length + inferredSiblings.length) > 10; const singleParent = (parents.length + backLinks.length) <= 1 const lChildren = new Layout({ origoX: 0, origoY: 2.5 * NODE_HEIGHT, top: 2.5 * NODE_HEIGHT - NODE_HEIGHT/2, bottom: null, columns: manyChildren ? 5 : 3, columnWidth: NODE_WIDTH, rowHeight: NODE_HEIGHT }); const lJumps = new Layout({ origoX: (manyChildren ? -3 : -2) * NODE_WIDTH, origoY: 0, top: null, bottom: null, columns: 1, columnWidth: NODE_WIDTH, rowHeight: NODE_HEIGHT }); const lParents = new Layout({ origoX: 0, origoY: -2.5 * NODE_HEIGHT, top: null, bottom: -2.5 * NODE_HEIGHT + NODE_HEIGHT/2, columns: 3, columnWidth: NODE_WIDTH, rowHeight: NODE_HEIGHT }); const lSiblings = new Layout({ origoX: NODE_WIDTH * ((singleParent ? 0 : 1) + (manySiblings ? 2 : 1)), origoY: -2.5 * NODE_HEIGHT, top: null, bottom: NODE_HEIGHT, columns: (manySiblings ? 3 : 1), columnWidth: NODE_WIDTH, rowHeight: NODE_HEIGHT }) const rootNode = new Node({ file: rootFile, hasChildren: false, hasParents: false, hasJumps: false, fontSize: CENTRAL_FONT_SIZE, jumpOnLeft: true, maxLabelLength: 2*MAX_LABEL_LENGTH, gateRadius: GATE_RADIUS, gateOffset: GATE_OFFSET, padding: PADDING, nodeColor: CENTRAL_NODE_COLOR, gateColor: GATE_COLOR, borderColor: CENTRAL_NODE_COLOR, backgroundColor: CENTRAL_NODE_BG_COLOR, virtualNodeBGColor: VIRTUAL_NODE_BG_COLOR }); nodesMap.set(rootFile.path,rootNode); lCenter.nodes.push(rootNode); addNodes( nodesMap, rootFile, parents, lParents, { fontSize: FONT_SIZE, jumpOnLeft: true, maxLabelLength: MAX_LABEL_LENGTH, gateRadius: GATE_RADIUS, gateOffset: GATE_OFFSET, padding: PADDING, nodeColor: NODE_COLOR, gateColor: GATE_COLOR, borderColor: \"transparent\", backgroundColor: NODE_BG_COLOR, virtualNodeColor: VIRTUAL_NODE_COLOR, virtualNodeBGColor: VIRTUAL_NODE_BG_COLOR } ); addNodes( nodesMap, rootFile, backLinks, lParents, { fontSize: FONT_SIZE, jumpOnLeft: true, maxLabelLength: MAX_LABEL_LENGTH, gateRadius: GATE_RADIUS, gateOffset: GATE_OFFSET, padding: PADDING, nodeColor: OBSIDIAN_NODE_COLOR, gateColor: GATE_COLOR, borderColor: OBSIDIAN_NODE_BG_COLOR, strokeStyle: \"dotted\", strokeWidth: 3, backgroundColor: OBSIDIAN_NODE_BG_COLOR, virtualNodeColor: VIRTUAL_NODE_COLOR, virtualNodeBGColor: VIRTUAL_NODE_BG_COLOR } ); addNodes( nodesMap, rootFile, children, lChildren, { fontSize: FONT_SIZE, jumpOnLeft: true, maxLabelLength: MAX_LABEL_LENGTH, gateRadius: GATE_RADIUS, gateOffset: GATE_OFFSET, padding: PADDING, nodeColor: NODE_COLOR, gateColor: GATE_COLOR, borderColor: \"transparent\", backgroundColor: NODE_BG_COLOR, virtualNodeColor: VIRTUAL_NODE_COLOR, virtualNodeBGColor: VIRTUAL_NODE_BG_COLOR } ); addNodes( nodesMap, rootFile, forwardLinks, lChildren, { fontSize: FONT_SIZE, jumpOnLeft: true, maxLabelLength: MAX_LABEL_LENGTH, gateRadius: GATE_RADIUS, gateOffset: GATE_OFFSET, padding: PADDING, nodeColor: OBSIDIAN_NODE_COLOR, gateColor: GATE_COLOR, borderColor: OBSIDIAN_NODE_BG_COLOR, strokeStyle: \"dotted\", strokeWidth: 3, backgroundColor: OBSIDIAN_NODE_BG_COLOR, virtualNodeColor: VIRTUAL_NODE_COLOR, virtualNodeBGColor: VIRTUAL_NODE_BG_COLOR } ); addNodes( nodesMap, rootFile, jumps, lJumps, { fontSize: FONT_SIZE, jumpOnLeft: false, maxLabelLength: MAX_LABEL_LENGTH, gateRadius: GATE_RADIUS, gateOffset: GATE_OFFSET, padding: PADDING, nodeColor: NODE_COLOR, gateColor: GATE_COLOR, borderColor: \"transparent\", backgroundColor: NODE_BG_COLOR, virtualNodeColor: VIRTUAL_NODE_COLOR, virtualNodeBGColor: VIRTUAL_NODE_BG_COLOR } ); addNodes( nodesMap, rootFile, siblings, lSiblings, { fontSize: DISTANT_FONT_SIZE, jumpOnLeft: true, maxLabelLength: MAX_LABEL_LENGTH, gateRadius: GATE_RADIUS, gateOffset: GATE_OFFSET, padding: PADDING, nodeColor: NODE_COLOR, gateColor: GATE_COLOR, borderColor: \"transparent\", backgroundColor: NODE_BG_COLOR, virtualNodeColor: VIRTUAL_NODE_COLOR, virtualNodeBGColor: VIRTUAL_NODE_BG_COLOR } ); addNodes( nodesMap, rootFile, inferredSiblings, lSiblings, { fontSize: DISTANT_FONT_SIZE, jumpOnLeft: true, maxLabelLength: MAX_LABEL_LENGTH, gateRadius: GATE_RADIUS, gateOffset: GATE_OFFSET, padding: PADDING, nodeColor: OBSIDIAN_NODE_COLOR, gateColor: GATE_COLOR, borderColor: OBSIDIAN_NODE_BG_COLOR, strokeStyle: \"dotted\", strokeWidth: 3, backgroundColor: OBSIDIAN_NODE_BG_COLOR, virtualNodeColor: VIRTUAL_NODE_COLOR, virtualNodeBGColor: VIRTUAL_NODE_BG_COLOR } ); //------------------------------------------------------- // Generate links for all displayed nodes const separator = \"?\"; //any character disallowed in filenames Array.from(nodesMap.keys()).forEach(filePath => { const node = nodesMap.get(filePath); const dvPage = DataviewAPI.page(filePath); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 //------------------------------------------------------- // Add links originating from node const parent = forwardlinksSet.has(filePath) ? rootFile.path : inverseInferredSiblingsMap.get(filePath); const parents = dvPage ? (parent ? getDVFieldLinks(dvPage,\"up\",true).add(parent) : getDVFieldLinks(dvPage,\"up\",true)) : (parent ? new Set([parent]) : new Set()); const child = backlinksSet.has(filePath) ? rootFile.path : null const children = dvPage ? (child ? getDVFieldLinks(dvPage,\"down\", true).add(child) : getDVFieldLinks(dvPage,\"down\", true)) : (child ? new Set([child]) : new Set()); const jumps = dvPage ? getDVFieldLinks(dvPage,\"jump\", true) : new Set(); //siblings are left out in this case on purpose const addLinks = (nodes,relation) => nodes.forEach(n => { if(!nodesMap.has(n)) return; if(linksMap.has(filePath + separator + n)) return; linksMap.set(filePath + separator + n,relation); linksMap.set(n + separator + filePath,null); }); addLinks(parents,[\"parent\",\"child\"]); addLinks(children, [\"child\",\"parent\"]); addLinks(jumps, [\"jump\",\"jump\"]); //------------------------------------------------------- //Set gate fill //- if node has outgoing links node.spec.hasChildren = node.spec.hasChildren || children.size>0; node.spec.hasParents = node.spec.hasParents || parents.size>0; node.spec.hasJumps = node.spec.hasJumps || jumps.size>0; //- of the nodes on the other end of outgoing links // this is required on top of backlinks, to handle // nodes for referenced but not yet created pages parents.forEach(p=>{ const n = nodesMap.get(p); if(n) n.spec.hasChildren = true; }); children.forEach(p=>{ const n = nodesMap.get(p); if(n) n.spec.hasParents = true; }); jumps.forEach(p=>{ const n = nodesMap.get(p); if(n) n.spec.hasJumps = true; }); //- if node has an incoming link from a node outside the visible graph if(node.spec.file && !(node.spec.hasParents && node.spec.hasChildren && node.hasJumps)) { const nodeBacklinks = new Set( Object.keys((app.metadataCache.getBacklinksForFile(node.spec.file)?.data)??{}) .map(l=>app.metadataCache.getFirstLinkpathDest(l,filePath)?.path??l) ); nodeBacklinks.forEach(l=>{ if(node.spec.hasParents && node.spec.hasChildren && node.hasJumps) return; const page = DataviewAPI.page(l); if(page) { if(getDVFieldLinks(page,\"down\",true).has(filePath)) { node.spec.hasParents = true; return; } if(getDVFieldLinks(page,\"up\",true).has(filePath)) { node.spec.hasChildren = true; return; } if(getDVFieldLinks(page,\"jump\",true).has(filePath)) { node.spec.hasJumps = true; return; } if(INCLUDE_OBSIDIAN_LINKS) { node.spec.hasParents = true; return; } } }); } if(!node.spec.hasChildren && INCLUDE_OBSIDIAN_LINKS && node.spec.file) { const forwardLinks = app.metadataCache .getLinks()[filePath]?.map( l=>app.metadataCache.getFirstLinkpathDest(l.link.match(/[^#]*/)[0],rootFile.path)??l.link.match(/[^#]*/)[0] ).filter(f=>f && (!HIDE_ATTACHMENTS || (f.extension??getExtension(f)) === \"md\")) .map(f=>f.path??f) .filter(l=>!parents.has(l) && !children.has(l) && !jumps.has(l) && l!==rootFile.path); if(forwardLinks.length > 0) { node.spec.hasChildren = true; } } }); //------------------------------------------------------- // Render lCenter.render(); lParents.render(); lChildren.render(); lJumps.render(); lSiblings.render(); const getGate = (key,value) => (value === \"parent\") ? nodesMap.get(key).parentGateId : (value === \"child\") ? nodesMap.get(key).childGateId : nodesMap.get(key).jumpGateId; ea.style.strokeColor = LINK_COLOR; for([key,value] of linksMap) { if(value) { const k=key.split(separator); const gate1 = getGate(k[0],value[0]); const gate2 = getGate(k[1],value[1]); ea.connectObjects(gate1, null, gate2, null, { startArrowHead: null, endArrowHead: null }); } } elements = ea.getElements(); ea.getExcalidrawAPI().updateScene({ elements: elements.filter( el=>el.type===\"arrow\" ).concat(elements.filter(el=>el.type!==\"arrow\")) }) ea.getExcalidrawAPI().zoomToFit(); } //------------------------------------------------------- // Initiate event listner and trigger plex if file is open //------------------------------------------------------- app.workspace.on(EVENT, window.brainGraphEventHandler); ea.targetView.semaphores.saving = true; //disable saving by setting this Excalidraw flag (not published API) const mdLeaf = app.workspace.getLeavesOfType(\"markdown\"); if(mdLeaf.length>0) { window.brainGraphEventHandler(mdLeaf[0]); return; } const mdExcalidrawLeaf = app.workspace.getLeavesOfType(\"excalidraw\").filter(l=>l!==ea.targetView.leaf); if(mdExcalidrawLeaf.length>0) { window.brainGraphEventHandler(mdExcalidrawLeaf[0]); return; } const mdImageLeaf = app.workspace.getLeavesOfType(\"image\"); if(mdImageLeaf.length>0) { window.brainGraphEventHandler(mdImageLeaf[0]); return; }","title":"TheBrain navigation"},{"location":"tags/","text":"Tags Associations \u5173\u8054 Associative-containers \u5173\u8054\u5bb9\u5668 Binding \u7ed1\u5b9a C++11 11.13 - for-each \u5faa\u73af 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb 4.11 - \u5b57\u7b26 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 M.7 \u2014 std::shared_ptr C++14 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 4.15 - \u5b57\u9762\u91cf 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad M.6 \u2014 std::unique_ptr M.7 \u2014 std::shared_ptr C++17 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 11.16 \u2014 \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb 11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 4.18 - std::string_view \u7b80\u4ecb 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 7.17 - \u65ad\u8a00\u548c static_assert M.6 \u2014 std::unique_ptr M.7 \u2014 std::shared_ptr C++20 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 11.13 - for-each \u5faa\u73af 11.19 \u2014 \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 6.14 - Constexpr \u548c consteval \u51fd\u6570 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad M.7 \u2014 std::shared_ptr C-style-casts C \u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362 CTAD 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD Early binding \u65e9\u671f\u7ed1\u5b9a Encapsulation \u5c01\u88c5 Expression-parameters \u8868\u8fbe\u5f0f\u53c2\u6570 Frankenobject \u7f1d\u5408\u602a\u5bf9\u8c61 Function templates 8.13 - \u51fd\u6570\u6a21\u677f Late binding \u540e\u671f\u7ed1\u5b9a Lvalue 9.3 - \u5de6\u503c\u5f15\u7528 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 Multiple inheritance \u591a\u91cd\u7ee7\u627f Name-mangling \u540d\u79f0\u4fee\u9970 Numeral systems 4.16 - \u6570\u503c\u7cfb\u7edf\uff08\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236\uff09 Off-by-one \u5dee\u4e00\u9519\u8bef Output-manipulators \u8f93\u51fa\u64cd\u7eb5\u5668 Pointer arithmetic 11.9 - \u6307\u9488\u8fd0\u7b97\u548c\u6570\u7ec4\u7d22\u5f15 R-value-references M.2 - \u53f3\u503c\u5f15\u7528 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c RAII (Resource Acquisition Is Initialization) \u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09 Run-time-type-information-RTTI \u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f(RTTI) Sequence-containers \u987a\u5e8f\u5bb9\u5668 Syntax 3.1 - \u8bed\u6cd5\u548c\u8bed\u4e49\u9519\u8bef Template-specialization \u6a21\u677f\u7279\u5316 abbreviated function templates \u7f29\u5199\u51fd\u6570\u6a21\u677f abstract-base-classes 18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b abstraction \u62bd\u8c61 access specifier 13.3 - \u8bbf\u95ee\u4fee\u9970\u7b26 public \u548c private access-function \u6210\u5458\u8bbf\u95ee\u51fd\u6570 access-specifiers 17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 address 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de 9.9 - \u6309\u5730\u5740\u4f20\u9012 address-of-operator \u53d6\u5730\u5740\u64cd\u4f5c\u7b26 & aggregate initialization 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 aggregate-data-type \u805a\u5408\u6570\u636e\u7c7b\u578b aggregate-initialization \u805a\u5408\u521d\u59cb\u5316 aggregation 16.3 - \u805a\u5408\u5173\u7cfb \u805a\u5408 algorithms 11.19 \u2014 \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb alias 8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d ambiguous-match \u4e0d\u660e\u786e\u5339\u914d anonymous-object \u533f\u540d\u5bf9\u8c61 argument 12.5 - \u547d\u4ee4\u884c\u53c2\u6570 2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2 arguments \u5b9e\u53c2 arithmetic conversions 8.4 - \u7b97\u672f\u8f6c\u6362 arity \u5143\u6570 array 11.1 - \u6570\u7ec4\uff08\u7b2c\u4e00\u90e8\u5206\uff09 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.3 - \u6570\u7ec4\u548c\u5faa\u73af 11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f 11.5 - \u591a\u7ef4\u6570\u7ec4 11.8 - \u6307\u9488\u548c\u6570\u7ec4 arrays 11.12 - \u52a8\u6001\u5206\u914d\u6570\u7ec4 assert 7.17 - \u65ad\u8a00\u548c static_assert assignment 14.15 - \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26 association 16.4 - \u5173\u8054\u5173\u7cfb auto 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad automatic-memory-allocation \u81ea\u52a8\u5185\u5b58\u5206\u914d automatic-storage-duration \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 base-class \u57fa\u7c7b bit O.1 - bit\u6807\u8bb0\u548c\u901a\u8fc7std::bitset\u64cd\u4f5cbit O.2 - \u4f4d\u8fd0\u7b97\u7b26 O.3 - \u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u548c\u63a9\u7801\u64cd\u4f5c\u4f4d O.4 - \u4e8c\u8fdb\u5236\u548c\u5341\u8fdb\u5236\u8f6c\u6362 \u4f4d bitwise-and None bitwise-or \u6309\u4f4d\u6216 bool 4.9 - \u5e03\u5c14\u503c boolean 4.10 - if \u8bed\u53e5\u7b80\u4ecb bss segment bss\u6bb5 byte \u5b57\u8282 c-style 11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 call stack 3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808 call-stack \u8c03\u7528\u6808 callable-object \u53ef\u8c03\u7528\u5bf9\u8c61 callback functions \u56de\u8c03\u51fd\u6570 char 4.11 - \u5b57\u7b26 child-class \u5b50\u7c7b circular dependency M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898 class 13.11 - \u7c7b\u4ee3\u7801\u548c\u5934\u6587\u4ef6 13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570 13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf 13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570 13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b 13.16 - \u533f\u540d\u5bf9\u8c61 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b 13.18 - \u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 13.2 - \u7c7b\u548c\u7c7b\u6210\u5458 class template 10.10 - \u7c7b\u6a21\u677f 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD class-template \u7c7b\u6a21\u677f class-template-argument-deduction \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad class-type \u7c7b\u7c7b\u578b closure \u95ed\u5305 code coverage 7.13 - \u4ee3\u7801\u8986\u76d6\u7387 code segment \u4ee3\u7801\u6bb5 comments 1.2 - \u6ce8\u91ca compile-time \u7f16\u8bd1\u65f6 compiler \u7f16\u8bd1\u5668 composite-types \u7ec4\u5408\u7c7b\u578b composition 16.2 - \u7ec4\u5408\u5173\u7cfb compound data types 9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b const 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 9.8 - \u6307\u9488\u548cconst const class 13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570 const-cast const \u7c7b\u578b\u8f6c\u6362 const-member-function const \u6210\u5458\u51fd\u6570 const-pointer \u5e38\u91cf\u6307\u9488 constant expression \u5e38\u91cf\u8868\u8fbe\u5f0f constants 4.15 - \u5b57\u9762\u91cf consteval 6.14 - Constexpr \u548c consteval \u51fd\u6570 constexpr 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr 6.14 - Constexpr \u548c consteval \u51fd\u6570 constructor 13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570 14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570 constructors 13.5 - \u6784\u9020\u51fd\u6570 13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 container classes 16.6 - \u5bb9\u5668\u7c7b container-adapter \u5bb9\u5668\u9002\u914d\u5668 container-class \u5bb9\u5668\u7c7b containers 21.2 - STL\u5bb9\u5668 21.3 - STL\u8fed\u4ee3\u5668 21.4 - STL\u7b97\u6cd5 control flow 7.1 - \u63a7\u5236\u6d41 7.4 - switch \u8bed\u53e5\u57fa\u7840 conversions 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 8.3 - \u6570\u503c\u8f6c\u6362 8.4 - \u7b97\u672f\u8f6c\u6362 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast converting constructors 14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57 converting-constructors \u8f6c\u6362\u6784\u9020\u51fd\u6570 copy constructor 14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570 copy initialization 14.13 - \u62f7\u8d1d\u521d\u59cb\u5316 copy-constructor \u62f7\u8d1d\u6784\u9020\u51fd\u6570 copy-constructors \u62f7\u8d1d\u6784\u9020\u51fd\u6570 copy-initialization \u62f7\u8d1d\u521d\u59cb\u5316 copy-semantics \u62f7\u8d1d\u8bed\u4e49 covariant \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b covariant-return-types 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b dangling \u60ac\u5782 data segment \u6570\u636e\u6bb5 data type 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb 4.4 - \u6709\u7b26\u53f7\u6574\u578b 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 4.8 - \u6d6e\u70b9\u6570 4.9 - \u5e03\u5c14\u503c 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad debugger 3.6 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u5355\u6b65\u8c03\u8bd5 3.7 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8fd0\u884c\u548c\u65ad\u70b9 3.8 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u76d1\u63a7\u53d8\u91cf 3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808 debugging 3.2 - \u4ee3\u7801\u8c03\u8bd5\u6b65\u9aa4 3.3 - \u4ee3\u7801\u8c03\u8bd5\u7b56\u7565 3.4 \u2014 \u57fa\u672c\u4ee3\u7801\u8c03\u8bd5\u6280\u672f 3.5 - \u66f4\u591a\u4ee3\u7801\u8c03\u8bd5\u6280\u672f declaration 6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e \u58f0\u660e \u7eaf\u58f0\u660e(pure-declaration) deep copy 14.16 - \u6d45\u62f7\u8d1d\u548c\u6df1\u62f7\u8d1d 14.17 - \u91cd\u8f7d\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u6a21\u677f deep-copy \u6df1\u62f7\u8d1d default 8.12 - \u9ed8\u8ba4\u53c2\u6570 default-constructor \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 default-initialization \u9ed8\u8ba4\u521d\u59cb\u5316 default-member-initializer \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c definition \u5b9a\u4e49 delegating 13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570 delegating-constructors \u59d4\u6258\u6784\u9020\u51fd\u6570 delete 11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d 14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57 dependencies 16.5 - \u4f9d\u8d56\u5173\u7cfb dependency \u4f9d\u8d56 dereference-operator \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 derived-class \u6d3e\u751f\u7c7b desgin 2.13 - \u5982\u4f55\u8bbe\u8ba1\u7b2c\u4e00\u4e2a\u7a0b\u5e8f designated-initializers \u6307\u5b9a\u521d\u59cb\u5316 destructor \u6790\u6784\u51fd\u6570 destructors 13.9 - \u6790\u6784\u51fd\u6570 direct-initialization \u76f4\u63a5\u521d\u59cb\u5316 do-while 7.8 - do-while \u8bed\u53e5 downcasting \u5411\u4e0b\u8f6c\u6362 dumb-pointer \u7b28\u6307\u9488 dynamic casting 18.10 - \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 dynamic duration \u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 dynamic-casts \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 dynamic-initialization \u52a8\u6001\u521d\u59cb\u5316 dynamic-memory 11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d dynamic-memory-allocation \u52a8\u6001\u5185\u5b58\u5206\u914d early-binding 18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a ellipsis 12.6 - \u7701\u7565\u53f7\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 encapsulation 13.4 - \u6210\u5458\u8bbf\u95ee\u51fd\u6570\u4e0e\u5c01\u88c5 enum 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b 10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 enum-class \u679a\u4e3e\u7c7b error handling 7.15 - \u53d1\u73b0\u9519\u8bef\u548c\u5904\u7406\u9519\u8bef 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406 evaluation \u6c42\u503c excalidraw Drawing 2023 02 11 13.33.50.excalidraw Template.excalidraw C++ basic onepage Moc onepage exception-safety-issue \u5f02\u5e38\u5b89\u5168\u95ee\u9898 explicit 14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57 explicit-type-conversion 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 expression 1.10 - \u8868\u8fbe\u5f0f\u7b80\u4ecb expression type \u8868\u8fbe\u5f0f\u7c7b\u578b external-linkage \u5916\u90e8\u94fe\u63a5 external-variable \u5916\u90e8\u53d8\u91cf extraction-operator \u63d0\u53d6\u8fd0\u7b97\u7b26 fallthrough \u8d2f\u7a7f\u5c5e\u6027 file 23.6 - \u57fa\u672c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa file-scope \u6587\u4ef6\u4f5c\u7528\u57df final 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b fixed-array \u56fa\u5b9a\u6570\u7ec4 fixed-width 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t float 4.8 - \u6d6e\u70b9\u6570 float point 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 for 7.7 - \u5faa\u73af\u548c while \u8bed\u53e5 7.9 - for \u8bed\u53e5 for-each 11.13 - for-each \u5faa\u73af fordward declaration 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 format-group \u683c\u5f0f\u7ec4 formatting 1.8 - \u7a7a\u767d\u7b26\u548c\u57fa\u672c\u6392\u7248 forward declaration \u524d\u5411\u58f0\u660e friend 13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b 13.16 - \u533f\u540d\u5bf9\u8c61 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b 13.18 - \u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 friend-class \u53cb\u5143\u7c7b friend-function \u53cb\u5143\u51fd\u6570 function 12.1 - \u51fd\u6570\u6307\u9488 2.1 - \u51fd\u6570\u7b80\u4ecb 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 2.6 - \u4e3a\u4ec0\u4e48\u51fd\u6570\u6709\u7528\uff0c\u5982\u4f55\u9ad8\u6548\u4f7f\u7528\u51fd\u6570 function template 8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f function-instance \u51fd\u6570\u5b9e\u4f8b function-literal \u51fd\u6570\u5b57\u9762\u91cf function-pointer \u51fd\u6570\u6307\u9488 function-template \u51fd\u6570\u6a21\u677f function-template-instantiation \u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316 generic-algorithm \u6cdb\u578b\u7b97\u6cd5 global 6.4 - \u5168\u5c40\u53d8\u91cf 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf global variable 6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c \u5168\u5c40\u53d8\u91cf glossary \u7f16\u8bd1\u65f6 \u58f0\u660e \u5b9a\u4e49 \u524d\u5411\u58f0\u660e \u5168\u5c40\u53d8\u91cf \u6807\u8bc6\u7b26(identifier) \u5b9e\u4f8b\u5316 \u6307\u4ee4 \u94fe\u63a5\u5668(linker) \u5b8f \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u5f62\u53c2 \u6309\u503c\u4f20\u9012 \u9884\u5904\u7406\u5668\u6307\u4ee4 \u9884\u5904\u7406\u5668 \u7eaf\u58f0\u660e(pure-declaration) \u9650\u5b9a\u7b26 \u91cd\u6784 \u6309\u503c\u8fd4\u56de \u8fd0\u884c\u65f6 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u4f5c\u7528\u57df \u8bed\u53e5 \u7b26\u53f7\u5e38\u91cf goto 7.6 - goto \u8bed\u53e5 halt 7.11 - \u6302\u8d77\uff08\u63d0\u524d\u7ed3\u675f\u7a0b\u5e8f\uff09 header 2.11 - \u5934\u6587\u4ef6 header guards 2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e heap 12.2 - \u6808\u548c\u5806 \u5806 identifier 1.7 - \u5173\u952e\u5b57\u548c\u6807\u8bc6\u7b26 \u6807\u8bc6\u7b26(identifier) identity \u8eab\u4efd\u7279\u5f81 if 4.10 - if \u8bed\u53e5\u7b80\u4ecb 7.2 - if \u8bed\u53e5\u548c\u8bed\u53e5\u5757 7.3 - \u5e38\u89c1\u7684 if \u8bed\u53e5\u9519\u8bef immediate-functions \u5373\u65f6\u51fd\u6570 implicit-constructor \u9690\u5f0f\u6784\u9020\u51fd\u6570 implicit-object \u9690\u5f0f\u5bf9\u8c61 implicit-type-conversion \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 inheritance 17.1 - \u7ee7\u627f\u7b80\u4ecb 17.2 - C++\u7ee7\u627f\u57fa\u7840 17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f 17.4 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u548c\u521d\u59cb\u5316 17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 17.6 - \u4e3a\u6d3e\u751f\u7c7b\u6dfb\u52a0\u65b0\u529f\u80fd 17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570 17.9 - \u591a\u91cd\u7ee7\u627f 18.11 - \u4f7f\u7528 << \u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b \u7ee7\u627f initialization 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u62f7\u8d1d\u521d\u59cb\u5316 \u76f4\u63a5\u521d\u59cb\u5316 \u5217\u8868\u521d\u59cb\u5316 initializer-list \u521d\u59cb\u5316\u503c\u5217\u8868 initializer_list 16.7 - std::initializer_list inline 6.13 - \u5185\u8054\u51fd\u6570 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf inline namespace 6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4 inline-function \u5185\u8054\u51fd\u6570 inline-namespace \u5185\u8054\u547d\u540d\u7a7a\u95f4 inline-variables \u5185\u8054\u53d8\u91cf input-manipulator 4.17 - std::string \u7b80\u4ecb input-manipulators \u8f93\u5165\u64cd\u7eb5\u5668 insertion-operator \u63d2\u5165\u8fd0\u7b97\u7b26 instance \u5b9e\u4f8b instantiated \u5b9e\u4f8b\u5316 instuction \u6307\u4ee4 integer 4.4 - \u6709\u7b26\u53f7\u6574\u578b 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t integral-type \u6574\u578b\u7c7b\u578b interface-class \u63a5\u53e3\u7c7b interface-classes 18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b internal-linkage \u5185\u90e8\u94fe\u63a5 internal-variable \u5185\u90e8\u53d8\u91cf io 23.1 - \u8f93\u5165\u8f93\u51fa\u6d41 23.2 - \u4f7f\u7528istream\u5904\u7406\u8f93\u5165 23.3 - \u4f7f\u7528ostream\u548cios\u5904\u7406\u8f93\u51fa 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b 23.5 - \u6d41\u72b6\u6001\u548c\u8f93\u5165\u9a8c\u8bc1 23.6 - \u57fa\u672c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa 23.7 - \u968f\u673a\u6587\u4ef6\u8f93\u5165\u8f93\u51fa iostream 1.5 - iostream \u7b80\u4ecb\uff1acout\uff0ccin \u548c endl iterating \u904d\u5386 iterator 11.18 \u2014 \u8fed\u4ee3\u5668\u7b80\u4ecb \u8fed\u4ee3\u5668 lambda 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 \u533f\u540d\u51fd\u6570 late-binding 18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a link \u94fe\u63a5 linkage 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 6.3 - \u5c40\u90e8\u53d8\u91cf 6.6 - \u5185\u90e8\u94fe\u63a5 6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e \u94fe\u63a5\u5c5e\u6027 linker \u94fe\u63a5\u5668(linker) list assignment operator \u7c7b\u522b\u8d4b\u503c\u8fd0\u7b97\u7b26 list-constructor \u5217\u8868\u6784\u9020\u51fd\u6570 list-initialization \u5217\u8868\u521d\u59cb\u5316 literal 4.15 - \u5b57\u9762\u91cf literals 1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 \u5b57\u9762\u91cf local variable 6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf local variables 6.3 - \u5c40\u90e8\u53d8\u91cf loop 11.3 - \u6570\u7ec4\u548c\u5faa\u73af lvalue \u5de6\u503c lvalue references 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 lvalue-reference \u5de6\u503c\u5f15\u7528 lvalues 9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09 macro \u5b8f member access operator \u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26 member selection 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 member-function \u6210\u5458\u51fd\u6570 member-initializer-list \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 member-selection-operator \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 member-types \u6210\u5458\u7c7b\u578b member-variable \u6210\u5458\u53d8\u91cf members 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 memberwise initialization \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 memberwise-assignment \u6210\u5458\u4f9d\u6b21\u8d4b\u503c memberwise-copy \u6210\u5458\u4f9d\u6b21\u62f7\u8d1d memory-leak \u5185\u5b58\u6cc4\u6f0f mixin None move M.4 - std::move M.5 - std::move_if_noexcept M.6 \u2014 std::unique_ptr move assignment operator \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 move-constructor \u79fb\u52a8\u6784\u9020\u51fd\u6570 move-semantics M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb \u79fb\u52a8\u8bed\u4e49 multiple-inheritance 17.9 - \u591a\u91cd\u7ee7\u627f named-cast \u5177\u540d\u7c7b\u578b\u8f6c\u6362 namespace 2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u547d\u540d\u7a7a\u95f4 namespace aliases 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 narrowing-convertions \u7f29\u7a84\u8f6c\u6362 new 11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d numeric conversions 8.3 - \u6570\u503c\u8f6c\u6362 numeric promotion 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 numeric promotions \u6570\u503c\u63d0\u5347 numeric-conversions \u6570\u503c\u8f6c\u6362 object 1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf 13.1 - \u6b22\u8fce\u6765\u5230\u9762\u5411\u5bf9\u8c61\u7684\u4e16\u754c 16.1 - \u5bf9\u8c61\u5173\u7cfb 16.2 - \u7ec4\u5408\u5173\u7cfb 16.3 - \u805a\u5408\u5173\u7cfb 16.4 - \u5173\u8054\u5173\u7cfb 16.5 - \u4f9d\u8d56\u5173\u7cfb 16.6 - \u5bb9\u5668\u7c7b 16.7 - std::initializer_list object-composition \u5bf9\u8c61\u7ec4\u5408 object-like-macros \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f object-slicing \u5bf9\u8c61\u5207\u7247 one-definition-rule \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) operands \u64cd\u4f5c\u6570 operator 1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 5.1 - \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 5.3 - \u6c42\u6a21\u548c\u6307\u6570\u8fd0\u7b97 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 5.5 - \u9017\u53f7\u548c\u6761\u4ef6\u8fd0\u7b97\u7b26 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 5.7 - \u903b\u8f91\u8fd0\u7b97\u7b26 \u8fd0\u7b97\u7b26 operator<< 18.11 - \u4f7f\u7528 << \u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b optional-parameter \u53ef\u9009\u53c2\u6570 overflow 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 overlapping 13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570 overload 14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26 14.15 - \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 8.10 - \u91cd\u8f7d\u51fd\u6570\u7684\u533a\u5206 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u4e0d\u660e\u786e\u5339\u914d \u91cd\u8f7d overload-resolution \u91cd\u8f7d\u89e3\u6790 overloading 8.9 - \u51fd\u6570\u91cd\u8f7d override 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b \u91cd\u5199 overriding 17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570 overriding-virtualization 18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316 parameter 2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2 parameters \u5f62\u53c2 parent-class \u7236\u7c7b parenthesis 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 pass-by-address \u6309\u5730\u5740\u4f20\u9012 pass-by-reference \u6309\u5f15\u7528\u4f20\u9012 pass-by-value \u6309\u503c\u4f20\u9012 placeholder-types \u5360\u4f4d\u7b26\u7c7b\u578b pointer 11.15 \u2014 \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u548c\u591a\u7ef4\u6570\u7ec4 11.8 - \u6307\u9488\u548c\u6570\u7ec4 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 9.6 - \u6307\u9488\u7b80\u4ecb 9.7 - \u7a7a\u6307\u9488 9.8 - \u6307\u9488\u548cconst 9.9 - \u6309\u5730\u5740\u4f20\u9012 pointer-to-const \u6307\u5411\u5e38\u91cf\u7684\u6307\u9488 pointers 18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 polymorphism 18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001 \u591a\u6001 pragma once 2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e preprocessor 2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb \u9884\u5904\u7406\u5668\u6307\u4ee4 \u9884\u5904\u7406\u5668 private-inheritance \u79c1\u6709\u7ee7\u627f private-member \u79c1\u6709\u6210\u5458 program organization 2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f protected-inheritance \u53d7\u4fdd\u62a4\u7ee7\u627f protected-members \u53d7\u4fdd\u62a4\u6210\u5458 public-inheritance \u516c\u5f00\u7ee7\u627f public-member \u516c\u6709\u6210\u5458 pure-declaration \u7eaf\u58f0\u660e(pure-declaration) pure-virtual \u7eaf\u865a\u51fd\u6570 pure-virtual-functions 18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b qualifier \u9650\u5b9a\u7b26 random-access-memory \u968f\u673a\u8bbf\u95ee\u5185\u5b58 range-based-for-loops \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af raw-pointer \u539f\u59cb\u6307\u9488 recursion 12.4 - \u9012\u5f52 refactoring \u91cd\u6784 reference 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de reference-container \u5f15\u7528\u5bb9\u5668 references 18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 M.2 - \u53f3\u503c\u5f15\u7528 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c reflexive association \u53cd\u8eab\u5173\u8054 reinterpret-casts \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 return 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de return-by-address \u6309\u5730\u5740\u8fd4\u56de return-by-reference \u6309\u5f15\u7528\u8fd4\u56de return-by-value \u6309\u503c\u8fd4\u56de return-type-deduction \u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad rounding-error \u820d\u5165\u8bef\u5dee runtime \u8fd0\u884c\u65f6 rvalue \u53f3\u503c rvalue-reference \u53f3\u503c\u5f15\u7528 rvalues 9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09 scaling \u6307\u9488\u7f29\u653e scientific notation 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 scope 2.5 - \u5c40\u90e8\u4f5c\u7528\u57df 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 \u4f5c\u7528\u57df scope resolution operator 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 scope-resolution-operator \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 scoped enumerations 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 scoped-enumerations \u6709\u4f5c\u7528\u57df\u679a\u4e3e segment \u5185\u5b58\u6bb5 sentinel \u54e8\u5175 setup 0.8 - \u4e00\u4e9b\u5178\u578b\u7684 C++ \u95ee\u9898 shadow \u53d8\u91cf\u906e\u853d shallow copy 14.16 - \u6d45\u62f7\u8d1d\u548c\u6df1\u62f7\u8d1d 14.17 - \u91cd\u8f7d\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u6a21\u677f shallow-copy \u6d45\u62f7\u8d1d shared_ptr M.7 \u2014 std::shared_ptr M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898 side-effects \u526f\u4f5c\u7528 signature \u7b7e\u540d size_t 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t sizeof 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 smart pointer class \u667a\u80fd\u6307\u9488\u7c7b smart-pointer \u667a\u80fd\u6307\u9488 smart-pointers M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb sort 11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f stack 12.2 - \u6808\u548c\u5806 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a \u6808 stack-frame \u6808\u5e27 stack-overflow \u5806\u6808\u6ea2\u51fa standard-stream \u6807\u51c6\u6d41 statement 6.1 - \u590d\u5408\u8bed\u53e5\uff08\u8bed\u53e5\u5757\uff09 \u8bed\u53e5 statements 1.1 - \u7a0b\u5e8f\u7ed3\u6784\u548c\u8bed\u53e5 static 6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf static membe function 13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570 static member 13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 static member variable 13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf static-cast 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast static-casts \u9759\u6001\u7c7b\u578b\u8f6c\u6362 static-initialization \u9759\u6001\u521d\u59cb\u5316 static-member-functions \u9759\u6001\u6210\u5458\u51fd\u6570 static-member-variables \u9759\u6001\u6210\u5458\u53d8\u91cf static-memory-allocation \u9759\u6001\u5185\u5b58\u5206\u914d static-storage-duration \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 static-variables \u9759\u6001\u53d8\u91cf static_assert 7.17 - \u65ad\u8a00\u548c static_assert static_cast 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast 8.3 - \u6570\u503c\u8f6c\u6362 std::array 11.16 \u2014 \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb std::string_view 11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 stl 21.1 - \u6807\u51c6\u5e93 21.2 - STL\u5bb9\u5668 21.3 - STL\u8fed\u4ee3\u5668 21.4 - STL\u7b97\u6cd5 storage duration 6.3 - \u5c40\u90e8\u53d8\u91cf 6.4 - \u5168\u5c40\u53d8\u91cf storage-duration \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 string 11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 22.1 - std::string \u548c std::wstring 22.2 - std-string \u7684\u6784\u9020\u548c\u6790\u6784 22.3 - std-string \u7684\u957f\u5ea6\u548c\u5bb9\u91cf 22.4 - std-string \u5b57\u7b26\u8bbf\u95ee\u548cC\u5b57\u7b26\u6570\u7ec4\u8f6c\u6362 22.5 - std-string \u8d4b\u503c\u548c\u4ea4\u6362 22.6 - std-string \u8ffd\u52a0 22.7 - std-string \u63d2\u5165 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b 4.17 - std::string \u7b80\u4ecb string_view 4.18 - std::string_view \u7b80\u4ecb strong-except \u5f3a\u5f02\u5e38\u4fdd\u8bc1 struct 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 10.8 - \u7ed3\u6784\u4f53\u4f20\u9012\u53ca\u5176\u4ed6 10.9 - \u57fa\u4e8e\u6307\u9488\u548c\u5f15\u7528\u7684\u6210\u5458\u9009\u62e9 struct-padding \u7ed3\u6784\u4f53\u586b\u5145 subscript 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 subscripts \u4e0b\u6807 summary 1.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - C++\u57fa\u7840 10.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u679a\u4e3e\u548c\u7ed3\u6784\u4f53 10.y - \u5b66\u4f1a\u4f7f\u7528C++\u53c2\u8003\u624b\u518c 11.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5206\u914d 12.8 - lambda \u95ed\u5305 12.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570 13.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u9762\u5411\u5bf9\u8c61\u57fa\u7840 14.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26\u91cd\u8f7d 16.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5bf9\u8c61\u5173\u7cfb 17.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7ee7\u627f 18.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u865a\u51fd\u6570 19.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6a21\u677f\u548c\u7c7b 2.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570\u4e0e\u6587\u4ef6 20.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f02\u5e38 3.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4ee3\u7801\u8c03\u8bd5 4.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u57fa\u672c\u6570\u636e\u7c7b\u578b 5.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 7.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406 8.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d 9.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f15\u7528\u548c\u6307\u9488 M.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u79fb\u52a8\u548c\u667a\u80fd\u6307\u9488 super-class \u8d85\u7c7b switch 7.4 - switch \u8bed\u53e5\u57fa\u7840 7.5 - switch fallthrough\u5c5e\u6027\u548c\u4f5c\u7528\u57df symbolic constants 11.10 - C \u98ce\u683c\u5b57\u7b26\u4e32\u7b26\u53f7\u5e38\u91cf symbolic-constants \u7b26\u53f7\u5e38\u91cf template class specialization \u7c7b\u6a21\u677f\u7279\u5316 testing 7.12 - \u4ee3\u7801\u6d4b\u8bd5 the-diamond-problem \u83f1\u5f62\u7ee7\u627f\u95ee\u9898 this 13.10 - \u9690\u85cf\u7684this\u6307\u9488 translation \u7ffb\u8bd1 type 10.1 - \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7b80\u4ecb type conversion 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 type deduction 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u63a8\u65ad type-aliases \u7c7b\u578b\u522b\u540d type-conversion \u7c7b\u578b\u8f6c\u6362 type-definition \u7c7b\u578b\u5b9a\u4e49 type-inference \u7c7b\u578b\u63a8\u65ad type-safe \u7c7b\u578b\u5b89\u5168 type-signature \u7c7b\u578b\u7b7e\u540d typecast 14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26 typedef 8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d typeid typeid \u8fd0\u7b97\u7b26 typeinfo 8.4 - \u7b97\u672f\u8f6c\u6362 unary 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 undefined behavior 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 4.4 - \u6709\u7b26\u53f7\u6574\u578b undefined-behavior \u672a\u5b9a\u4e49\u884c\u4e3a uniform initialization \u7edf\u4e00\u521d\u59cb\u5316 unmanned namespace 6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4 unnamed-namespace \u533f\u540d\u547d\u540d\u7a7a\u95f4 unscoped enumerations 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b 10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa unscoped-enumerations \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b upcasting \u5411\u4e0a\u8f6c\u6362 using 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 using-declaration using \u58f0\u660e using-directive using \u6307\u4ee4 value-category \u503c\u7c7b\u578b value-container \u503c\u5bb9\u5668 value-initialization \u503c\u521d\u59cb\u5316 value-preserving \u503c\u4fdd\u7559 variable 1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 variable shadowing 6.5 - \u53d8\u91cf\u906e\u853d vector 11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a virtual functions 18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001 virtual-assignment 18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316 virtual-base-class \u865a\u57fa\u7c7b virtual-base-classes 18.8 - \u865a\u57fa\u7c7b 18.9 - \u5bf9\u8c61\u5207\u7247 virtual-destructor \u865a\u6790\u6784\u51fd\u6570 virtual-destructors 18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316 virtual-function \u865a\u51fd\u6570 virtual-table 18.6 - \u865a\u51fd\u6570\u8868 \u865a\u8868 void 11.14 \u2014 void \u6307\u9488 2.3 - void \u51fd\u6570 4.2 - Void void pointer void \u6307\u9488 weak_ptr M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898 while 7.7 - \u5faa\u73af\u548c while \u8bed\u53e5 wild-pointer \u91ce\u6307\u9488 word \u5b57\u957f wstring 22.1 - std::string \u548c std::wstring zero-initialization 0\u521d\u59cb\u5316 \u51fd\u6570\u539f\u578b(function prototype) \u51fd\u6570\u539f\u578b \u521d\u59cb\u5316 \u521d\u59cb\u5316 \u521d\u59cb\u5316\u503c(initializer) \u521d\u59cb\u5316\u503c \u52a8\u6001\u6570\u7ec4 \u52a8\u6001\u6570\u7ec4 \u5bf9\u8c61(objec) \u5bf9\u8c61 \u62bd\u8c61\u7c7b \u62bd\u8c61\u7c7b \u62ec\u53f7\u521d\u59cb\u5316 None \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u6784\u9020\u51fd\u6570 \u6784\u9020\u51fd\u6570 \u6a21\u677f\u7c7b\u578b\u53c2\u6570 \u6a21\u677f\u7c7b\u578b\u53c2\u6570 \u79bb\u5f00\u4f5c\u7528\u57df \u79bb\u5f00\u4f5c\u7528\u57df \u7b80\u5355\u8f6c\u6362(trivial conversion) \u7b80\u5355\u8f6c\u6362 \u7c7b\u5b9e\u4f8b \u7c7b\u5b9e\u4f8b \u86c7\u5f62\u547d\u540d\u6cd5(snake_case) \u86c7\u5f62\u547d\u540d\u6cd5 \u8bcd\u6c47\u8868 \u5173\u8054 \u5173\u8054\u5bb9\u5668 \u7ed1\u5b9a C \u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362 \u65e9\u671f\u7ed1\u5b9a \u5c01\u88c5 \u8868\u8fbe\u5f0f\u53c2\u6570 \u7f1d\u5408\u602a\u5bf9\u8c61 \u540e\u671f\u7ed1\u5b9a \u591a\u91cd\u7ee7\u627f \u540d\u79f0\u4fee\u9970 \u5dee\u4e00\u9519\u8bef \u8f93\u51fa\u64cd\u7eb5\u5668 \u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09 \u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f(RTTI) \u987a\u5e8f\u5bb9\u5668 \u6a21\u677f\u7279\u5316 \u7f29\u5199\u51fd\u6570\u6a21\u677f \u62bd\u8c61\u7c7b \u62bd\u8c61 \u6210\u5458\u8bbf\u95ee\u51fd\u6570 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u53d6\u5730\u5740\u64cd\u4f5c\u7b26 & \u805a\u5408\u6570\u636e\u7c7b\u578b \u805a\u5408\u521d\u59cb\u5316 \u805a\u5408 \u4e0d\u660e\u786e\u5339\u914d \u533f\u540d\u5bf9\u8c61 \u5b9e\u53c2 \u5143\u6570 \u81ea\u52a8\u5185\u5b58\u5206\u914d \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u57fa\u7c7b \u4f4d None \u6309\u4f4d\u6216 bss\u6bb5 \u5b57\u8282 \u8c03\u7528\u6808 \u53ef\u8c03\u7528\u5bf9\u8c61 \u56de\u8c03\u51fd\u6570 \u5b50\u7c7b \u7c7b\u5b9e\u4f8b \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad \u7c7b\u6a21\u677f \u7c7b\u7c7b\u578b \u95ed\u5305 \u4ee3\u7801\u6bb5 \u7f16\u8bd1\u5668 \u7ec4\u5408\u7c7b\u578b const \u7c7b\u578b\u8f6c\u6362 const \u6210\u5458\u51fd\u6570 \u5e38\u91cf\u6307\u9488 \u5e38\u91cf\u8868\u8fbe\u5f0f \u6784\u9020\u51fd\u6570 \u5bb9\u5668\u9002\u914d\u5668 \u5bb9\u5668\u7c7b \u8f6c\u6362\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u521d\u59cb\u5316 \u62f7\u8d1d\u8bed\u4e49 \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b \u60ac\u5782 \u6570\u636e\u6bb5 \u6df1\u62f7\u8d1d \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 \u9ed8\u8ba4\u521d\u59cb\u5316 \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c \u59d4\u6258\u6784\u9020\u51fd\u6570 \u4f9d\u8d56 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u6d3e\u751f\u7c7b \u6307\u5b9a\u521d\u59cb\u5316 \u6790\u6784\u51fd\u6570 \u76f4\u63a5\u521d\u59cb\u5316 \u5411\u4e0b\u8f6c\u6362 \u7b28\u6307\u9488 \u52a8\u6001\u6570\u7ec4 \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u52a8\u6001\u521d\u59cb\u5316 \u52a8\u6001\u5185\u5b58\u5206\u914d \u679a\u4e3e\u7c7b \u6c42\u503c \u5f02\u5e38\u5b89\u5168\u95ee\u9898 \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u8868\u8fbe\u5f0f\u7c7b\u578b \u5916\u90e8\u94fe\u63a5 \u5916\u90e8\u53d8\u91cf \u63d0\u53d6\u8fd0\u7b97\u7b26 \u8d2f\u7a7f\u5c5e\u6027 \u6587\u4ef6\u4f5c\u7528\u57df \u56fa\u5b9a\u6570\u7ec4 \u683c\u5f0f\u7ec4 \u53cb\u5143\u7c7b \u53cb\u5143\u51fd\u6570 \u51fd\u6570\u5b9e\u4f8b \u51fd\u6570\u5b57\u9762\u91cf \u51fd\u6570\u6307\u9488 \u51fd\u6570\u539f\u578b \u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316 \u51fd\u6570\u6a21\u677f \u6cdb\u578b\u7b97\u6cd5 \u79bb\u5f00\u4f5c\u7528\u57df \u5806 \u8eab\u4efd\u7279\u5f81 \u5373\u65f6\u51fd\u6570 \u9690\u5f0f\u6784\u9020\u51fd\u6570 \u9690\u5f0f\u5bf9\u8c61 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u7ee7\u627f \u521d\u59cb\u5316 \u521d\u59cb\u5316\u503c\u5217\u8868 \u521d\u59cb\u5316\u503c \u5185\u8054\u51fd\u6570 \u5185\u8054\u547d\u540d\u7a7a\u95f4 \u5185\u8054\u53d8\u91cf \u8f93\u5165\u64cd\u7eb5\u5668 \u63d2\u5165\u8fd0\u7b97\u7b26 \u5b9e\u4f8b \u6574\u578b\u7c7b\u578b \u63a5\u53e3\u7c7b \u5185\u90e8\u94fe\u63a5 \u5185\u90e8\u53d8\u91cf \u904d\u5386 \u8fed\u4ee3\u5668 \u533f\u540d\u51fd\u6570 \u94fe\u63a5 \u94fe\u63a5\u5c5e\u6027 \u7c7b\u522b\u8d4b\u503c\u8fd0\u7b97\u7b26 \u5217\u8868\u6784\u9020\u51fd\u6570 \u5217\u8868\u521d\u59cb\u5316 \u5b57\u9762\u91cf \u5de6\u503c\u5f15\u7528 \u5de6\u503c \u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26 \u6210\u5458\u51fd\u6570 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 \u6210\u5458\u7c7b\u578b \u6210\u5458\u53d8\u91cf \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \u6210\u5458\u4f9d\u6b21\u8d4b\u503c \u6210\u5458\u4f9d\u6b21\u62f7\u8d1d \u5185\u5b58\u6cc4\u6f0f None \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 \u79fb\u52a8\u6784\u9020\u51fd\u6570 \u79fb\u52a8\u8bed\u4e49 \u5177\u540d\u7c7b\u578b\u8f6c\u6362 \u547d\u540d\u7a7a\u95f4 \u7f29\u7a84\u8f6c\u6362 \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u6570\u503c\u63d0\u5347 \u6570\u503c\u8f6c\u6362 \u5bf9\u8c61\u7ec4\u5408 \u5bf9\u8c61\u5207\u7247 \u5bf9\u8c61 \u64cd\u4f5c\u6570 \u8fd0\u7b97\u7b26 \u8d85\u51fa\u4f5c\u7528\u57df \u91cd\u8f7d\u89e3\u6790 \u91cd\u8f7d \u91cd\u5199 \u7236\u7c7b \u6309\u5730\u5740\u4f20\u9012 \u6309\u5f15\u7528\u4f20\u9012 \u5360\u4f4d\u7b26\u7c7b\u578b \u6307\u5411\u5e38\u91cf\u7684\u6307\u9488 \u591a\u6001 \u79c1\u6709\u7ee7\u627f \u79c1\u6709\u6210\u5458 \u53d7\u4fdd\u62a4\u7ee7\u627f \u53d7\u4fdd\u62a4\u6210\u5458 \u516c\u5f00\u7ee7\u627f \u516c\u6709\u6210\u5458 \u7eaf\u865a\u51fd\u6570 \u968f\u673a\u8bbf\u95ee\u5185\u5b58 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u539f\u59cb\u6307\u9488 \u5f15\u7528\u5bb9\u5668 \u53cd\u8eab\u5173\u8054 \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 \u6309\u5730\u5740\u8fd4\u56de \u6309\u5f15\u7528\u8fd4\u56de \u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad \u820d\u5165\u8bef\u5dee \u53f3\u503c\u5f15\u7528 \u53f3\u503c \u6307\u9488\u7f29\u653e \u6709\u4f5c\u7528\u57df\u679a\u4e3e \u5185\u5b58\u6bb5 \u54e8\u5175 \u53d8\u91cf\u906e\u853d \u6d45\u62f7\u8d1d \u526f\u4f5c\u7528 \u7b7e\u540d \u667a\u80fd\u6307\u9488\u7c7b \u667a\u80fd\u6307\u9488 \u86c7\u5f62\u547d\u540d\u6cd5 \u6808\u5e27 \u5806\u6808\u6ea2\u51fa \u6808 \u6807\u51c6\u6d41 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u9759\u6001\u521d\u59cb\u5316 \u9759\u6001\u6210\u5458\u51fd\u6570 \u9759\u6001\u6210\u5458\u53d8\u91cf \u9759\u6001\u5185\u5b58\u5206\u914d \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u9759\u6001\u53d8\u91cf \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u5f3a\u5f02\u5e38\u4fdd\u8bc1 \u7ed3\u6784\u4f53\u586b\u5145 \u4e0b\u6807 \u8d85\u7c7b \u8bed\u6cd5 \u7c7b\u6a21\u677f\u7279\u5316 \u6a21\u677f\u7c7b\u578b\u53c2\u6570 \u83f1\u5f62\u7ee7\u627f\u95ee\u9898 \u7ffb\u8bd1 \u7b80\u5355\u8f6c\u6362 \u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u522b\u540d \u7c7b\u578b\u8f6c\u6362 \u7c7b\u578b\u5b9a\u4e49 \u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u5b89\u5168 \u7c7b\u578b\u7b7e\u540d typeid \u8fd0\u7b97\u7b26 \u672a\u5b9a\u4e49\u884c\u4e3a \u7edf\u4e00\u521d\u59cb\u5316 \u533f\u540d\u547d\u540d\u7a7a\u95f4 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u5411\u4e0a\u8f6c\u6362 using \u58f0\u660e using \u6307\u4ee4 \u503c\u7c7b\u578b \u503c\u5bb9\u5668 \u503c\u521d\u59cb\u5316 \u503c\u4fdd\u7559 \u865a\u57fa\u7c7b \u865a\u6790\u6784\u51fd\u6570 \u865a\u51fd\u6570 \u865a\u8868 void \u6307\u9488 \u91ce\u6307\u9488 \u5b57\u957f 0\u521d\u59cb\u5316 None \u8bed\u6cd5(syntax) \u8bed\u6cd5 \u8d85\u51fa\u4f5c\u7528\u57df(out of scope) \u8d85\u51fa\u4f5c\u7528\u57df \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316","title":"\u7d22\u5f15"},{"location":"tags/#tags","text":"","title":"Tags"},{"location":"tags/#associations","text":"\u5173\u8054","title":"Associations"},{"location":"tags/#associative-containers","text":"\u5173\u8054\u5bb9\u5668","title":"Associative-containers"},{"location":"tags/#binding","text":"\u7ed1\u5b9a","title":"Binding"},{"location":"tags/#c11","text":"11.13 - for-each \u5faa\u73af 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb 4.11 - \u5b57\u7b26 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 M.7 \u2014 std::shared_ptr","title":"C++11"},{"location":"tags/#c14","text":"12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 4.15 - \u5b57\u9762\u91cf 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad M.6 \u2014 std::unique_ptr M.7 \u2014 std::shared_ptr","title":"C++14"},{"location":"tags/#c17","text":"10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 11.16 \u2014 \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb 11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 4.18 - std::string_view \u7b80\u4ecb 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 7.17 - \u65ad\u8a00\u548c static_assert M.6 \u2014 std::unique_ptr M.7 \u2014 std::shared_ptr","title":"C++17"},{"location":"tags/#c20","text":"10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 11.13 - for-each \u5faa\u73af 11.19 \u2014 \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 6.14 - Constexpr \u548c consteval \u51fd\u6570 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad M.7 \u2014 std::shared_ptr","title":"C++20"},{"location":"tags/#c-style-casts","text":"C \u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362","title":"C-style-casts"},{"location":"tags/#ctad","text":"10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD","title":"CTAD"},{"location":"tags/#early-binding","text":"\u65e9\u671f\u7ed1\u5b9a","title":"Early binding"},{"location":"tags/#encapsulation","text":"\u5c01\u88c5","title":"Encapsulation"},{"location":"tags/#expression-parameters","text":"\u8868\u8fbe\u5f0f\u53c2\u6570","title":"Expression-parameters"},{"location":"tags/#frankenobject","text":"\u7f1d\u5408\u602a\u5bf9\u8c61","title":"Frankenobject"},{"location":"tags/#function-templates","text":"8.13 - \u51fd\u6570\u6a21\u677f","title":"Function templates"},{"location":"tags/#late-binding","text":"\u540e\u671f\u7ed1\u5b9a","title":"Late binding"},{"location":"tags/#lvalue","text":"9.3 - \u5de6\u503c\u5f15\u7528 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528","title":"Lvalue"},{"location":"tags/#multiple-inheritance","text":"\u591a\u91cd\u7ee7\u627f","title":"Multiple inheritance"},{"location":"tags/#name-mangling","text":"\u540d\u79f0\u4fee\u9970","title":"Name-mangling"},{"location":"tags/#numeral-systems","text":"4.16 - \u6570\u503c\u7cfb\u7edf\uff08\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236\uff09","title":"Numeral systems"},{"location":"tags/#off-by-one","text":"\u5dee\u4e00\u9519\u8bef","title":"Off-by-one"},{"location":"tags/#output-manipulators","text":"\u8f93\u51fa\u64cd\u7eb5\u5668","title":"Output-manipulators"},{"location":"tags/#pointer-arithmetic","text":"11.9 - \u6307\u9488\u8fd0\u7b97\u548c\u6570\u7ec4\u7d22\u5f15","title":"Pointer arithmetic"},{"location":"tags/#r-value-references","text":"M.2 - \u53f3\u503c\u5f15\u7528 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c","title":"R-value-references"},{"location":"tags/#raii-resource-acquisition-is-initialization","text":"\u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09","title":"RAII (Resource Acquisition Is Initialization)"},{"location":"tags/#run-time-type-information-rtti","text":"\u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f(RTTI)","title":"Run-time-type-information-RTTI"},{"location":"tags/#sequence-containers","text":"\u987a\u5e8f\u5bb9\u5668","title":"Sequence-containers"},{"location":"tags/#syntax","text":"3.1 - \u8bed\u6cd5\u548c\u8bed\u4e49\u9519\u8bef","title":"Syntax"},{"location":"tags/#template-specialization","text":"\u6a21\u677f\u7279\u5316","title":"Template-specialization"},{"location":"tags/#abbreviated-function-templates","text":"\u7f29\u5199\u51fd\u6570\u6a21\u677f","title":"abbreviated function templates"},{"location":"tags/#abstract-base-classes","text":"18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b","title":"abstract-base-classes"},{"location":"tags/#abstraction","text":"\u62bd\u8c61","title":"abstraction"},{"location":"tags/#access-specifier","text":"13.3 - \u8bbf\u95ee\u4fee\u9970\u7b26 public \u548c private","title":"access specifier"},{"location":"tags/#access-function","text":"\u6210\u5458\u8bbf\u95ee\u51fd\u6570","title":"access-function"},{"location":"tags/#access-specifiers","text":"17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26","title":"access-specifiers"},{"location":"tags/#address","text":"9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de 9.9 - \u6309\u5730\u5740\u4f20\u9012","title":"address"},{"location":"tags/#address-of-operator","text":"\u53d6\u5730\u5740\u64cd\u4f5c\u7b26 &","title":"address-of-operator"},{"location":"tags/#aggregate-initialization","text":"10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316","title":"aggregate initialization"},{"location":"tags/#aggregate-data-type","text":"\u805a\u5408\u6570\u636e\u7c7b\u578b","title":"aggregate-data-type"},{"location":"tags/#aggregate-initialization_1","text":"\u805a\u5408\u521d\u59cb\u5316","title":"aggregate-initialization"},{"location":"tags/#aggregation","text":"16.3 - \u805a\u5408\u5173\u7cfb \u805a\u5408","title":"aggregation"},{"location":"tags/#algorithms","text":"11.19 \u2014 \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb","title":"algorithms"},{"location":"tags/#alias","text":"8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d","title":"alias"},{"location":"tags/#ambiguous-match","text":"\u4e0d\u660e\u786e\u5339\u914d","title":"ambiguous-match"},{"location":"tags/#anonymous-object","text":"\u533f\u540d\u5bf9\u8c61","title":"anonymous-object"},{"location":"tags/#argument","text":"12.5 - \u547d\u4ee4\u884c\u53c2\u6570 2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2","title":"argument"},{"location":"tags/#arguments","text":"\u5b9e\u53c2","title":"arguments"},{"location":"tags/#arithmetic-conversions","text":"8.4 - \u7b97\u672f\u8f6c\u6362","title":"arithmetic conversions"},{"location":"tags/#arity","text":"\u5143\u6570","title":"arity"},{"location":"tags/#array","text":"11.1 - \u6570\u7ec4\uff08\u7b2c\u4e00\u90e8\u5206\uff09 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.3 - \u6570\u7ec4\u548c\u5faa\u73af 11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f 11.5 - \u591a\u7ef4\u6570\u7ec4 11.8 - \u6307\u9488\u548c\u6570\u7ec4","title":"array"},{"location":"tags/#arrays","text":"11.12 - \u52a8\u6001\u5206\u914d\u6570\u7ec4","title":"arrays"},{"location":"tags/#assert","text":"7.17 - \u65ad\u8a00\u548c static_assert","title":"assert"},{"location":"tags/#assignment","text":"14.15 - \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26","title":"assignment"},{"location":"tags/#association","text":"16.4 - \u5173\u8054\u5173\u7cfb","title":"association"},{"location":"tags/#auto","text":"8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad","title":"auto"},{"location":"tags/#automatic-memory-allocation","text":"\u81ea\u52a8\u5185\u5b58\u5206\u914d","title":"automatic-memory-allocation"},{"location":"tags/#automatic-storage-duration","text":"\u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4","title":"automatic-storage-duration"},{"location":"tags/#base-class","text":"\u57fa\u7c7b","title":"base-class"},{"location":"tags/#bit","text":"O.1 - bit\u6807\u8bb0\u548c\u901a\u8fc7std::bitset\u64cd\u4f5cbit O.2 - \u4f4d\u8fd0\u7b97\u7b26 O.3 - \u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u548c\u63a9\u7801\u64cd\u4f5c\u4f4d O.4 - \u4e8c\u8fdb\u5236\u548c\u5341\u8fdb\u5236\u8f6c\u6362 \u4f4d","title":"bit"},{"location":"tags/#bitwise-and","text":"None","title":"bitwise-and"},{"location":"tags/#bitwise-or","text":"\u6309\u4f4d\u6216","title":"bitwise-or"},{"location":"tags/#bool","text":"4.9 - \u5e03\u5c14\u503c","title":"bool"},{"location":"tags/#boolean","text":"4.10 - if \u8bed\u53e5\u7b80\u4ecb","title":"boolean"},{"location":"tags/#bss-segment","text":"bss\u6bb5","title":"bss segment"},{"location":"tags/#byte","text":"\u5b57\u8282","title":"byte"},{"location":"tags/#c-style","text":"11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32","title":"c-style"},{"location":"tags/#call-stack","text":"3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808","title":"call stack"},{"location":"tags/#call-stack_1","text":"\u8c03\u7528\u6808","title":"call-stack"},{"location":"tags/#callable-object","text":"\u53ef\u8c03\u7528\u5bf9\u8c61","title":"callable-object"},{"location":"tags/#callback-functions","text":"\u56de\u8c03\u51fd\u6570","title":"callback functions"},{"location":"tags/#char","text":"4.11 - \u5b57\u7b26","title":"char"},{"location":"tags/#child-class","text":"\u5b50\u7c7b","title":"child-class"},{"location":"tags/#circular-dependency","text":"M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898","title":"circular dependency"},{"location":"tags/#class","text":"13.11 - \u7c7b\u4ee3\u7801\u548c\u5934\u6587\u4ef6 13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570 13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf 13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570 13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b 13.16 - \u533f\u540d\u5bf9\u8c61 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b 13.18 - \u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 13.2 - \u7c7b\u548c\u7c7b\u6210\u5458","title":"class"},{"location":"tags/#class-template","text":"10.10 - \u7c7b\u6a21\u677f 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD","title":"class template"},{"location":"tags/#class-template_1","text":"\u7c7b\u6a21\u677f","title":"class-template"},{"location":"tags/#class-template-argument-deduction","text":"\u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad","title":"class-template-argument-deduction"},{"location":"tags/#class-type","text":"\u7c7b\u7c7b\u578b","title":"class-type"},{"location":"tags/#closure","text":"\u95ed\u5305","title":"closure"},{"location":"tags/#code-coverage","text":"7.13 - \u4ee3\u7801\u8986\u76d6\u7387","title":"code coverage"},{"location":"tags/#code-segment","text":"\u4ee3\u7801\u6bb5","title":"code segment"},{"location":"tags/#comments","text":"1.2 - \u6ce8\u91ca","title":"comments"},{"location":"tags/#compile-time","text":"\u7f16\u8bd1\u65f6","title":"compile-time"},{"location":"tags/#compiler","text":"\u7f16\u8bd1\u5668","title":"compiler"},{"location":"tags/#composite-types","text":"\u7ec4\u5408\u7c7b\u578b","title":"composite-types"},{"location":"tags/#composition","text":"16.2 - \u7ec4\u5408\u5173\u7cfb","title":"composition"},{"location":"tags/#compound-data-types","text":"9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b","title":"compound data types"},{"location":"tags/#const","text":"4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 9.8 - \u6307\u9488\u548cconst","title":"const"},{"location":"tags/#const-class","text":"13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570","title":"const class"},{"location":"tags/#const-cast","text":"const \u7c7b\u578b\u8f6c\u6362","title":"const-cast"},{"location":"tags/#const-member-function","text":"const \u6210\u5458\u51fd\u6570","title":"const-member-function"},{"location":"tags/#const-pointer","text":"\u5e38\u91cf\u6307\u9488","title":"const-pointer"},{"location":"tags/#constant-expression","text":"\u5e38\u91cf\u8868\u8fbe\u5f0f","title":"constant expression"},{"location":"tags/#constants","text":"4.15 - \u5b57\u9762\u91cf","title":"constants"},{"location":"tags/#consteval","text":"6.14 - Constexpr \u548c consteval \u51fd\u6570","title":"consteval"},{"location":"tags/#constexpr","text":"4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr 6.14 - Constexpr \u548c consteval \u51fd\u6570","title":"constexpr"},{"location":"tags/#constructor","text":"13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570 14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570","title":"constructor"},{"location":"tags/#constructors","text":"13.5 - \u6784\u9020\u51fd\u6570 13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868","title":"constructors"},{"location":"tags/#container-classes","text":"16.6 - \u5bb9\u5668\u7c7b","title":"container classes"},{"location":"tags/#container-adapter","text":"\u5bb9\u5668\u9002\u914d\u5668","title":"container-adapter"},{"location":"tags/#container-class","text":"\u5bb9\u5668\u7c7b","title":"container-class"},{"location":"tags/#containers","text":"21.2 - STL\u5bb9\u5668 21.3 - STL\u8fed\u4ee3\u5668 21.4 - STL\u7b97\u6cd5","title":"containers"},{"location":"tags/#control-flow","text":"7.1 - \u63a7\u5236\u6d41 7.4 - switch \u8bed\u53e5\u57fa\u7840","title":"control flow"},{"location":"tags/#conversions","text":"8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 8.3 - \u6570\u503c\u8f6c\u6362 8.4 - \u7b97\u672f\u8f6c\u6362 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast","title":"conversions"},{"location":"tags/#converting-constructors","text":"14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57","title":"converting constructors"},{"location":"tags/#converting-constructors_1","text":"\u8f6c\u6362\u6784\u9020\u51fd\u6570","title":"converting-constructors"},{"location":"tags/#copy-constructor","text":"14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570","title":"copy constructor"},{"location":"tags/#copy-initialization","text":"14.13 - \u62f7\u8d1d\u521d\u59cb\u5316","title":"copy initialization"},{"location":"tags/#copy-constructor_1","text":"\u62f7\u8d1d\u6784\u9020\u51fd\u6570","title":"copy-constructor"},{"location":"tags/#copy-constructors","text":"\u62f7\u8d1d\u6784\u9020\u51fd\u6570","title":"copy-constructors"},{"location":"tags/#copy-initialization_1","text":"\u62f7\u8d1d\u521d\u59cb\u5316","title":"copy-initialization"},{"location":"tags/#copy-semantics","text":"\u62f7\u8d1d\u8bed\u4e49","title":"copy-semantics"},{"location":"tags/#covariant","text":"\u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b","title":"covariant"},{"location":"tags/#covariant-return-types","text":"18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b","title":"covariant-return-types"},{"location":"tags/#dangling","text":"\u60ac\u5782","title":"dangling"},{"location":"tags/#data-segment","text":"\u6570\u636e\u6bb5","title":"data segment"},{"location":"tags/#data-type","text":"4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb 4.4 - \u6709\u7b26\u53f7\u6574\u578b 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 4.8 - \u6d6e\u70b9\u6570 4.9 - \u5e03\u5c14\u503c 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad","title":"data type"},{"location":"tags/#debugger","text":"3.6 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u5355\u6b65\u8c03\u8bd5 3.7 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8fd0\u884c\u548c\u65ad\u70b9 3.8 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u76d1\u63a7\u53d8\u91cf 3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808","title":"debugger"},{"location":"tags/#debugging","text":"3.2 - \u4ee3\u7801\u8c03\u8bd5\u6b65\u9aa4 3.3 - \u4ee3\u7801\u8c03\u8bd5\u7b56\u7565 3.4 \u2014 \u57fa\u672c\u4ee3\u7801\u8c03\u8bd5\u6280\u672f 3.5 - \u66f4\u591a\u4ee3\u7801\u8c03\u8bd5\u6280\u672f","title":"debugging"},{"location":"tags/#declaration","text":"6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e \u58f0\u660e \u7eaf\u58f0\u660e(pure-declaration)","title":"declaration"},{"location":"tags/#deep-copy","text":"14.16 - \u6d45\u62f7\u8d1d\u548c\u6df1\u62f7\u8d1d 14.17 - \u91cd\u8f7d\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u6a21\u677f","title":"deep copy"},{"location":"tags/#deep-copy_1","text":"\u6df1\u62f7\u8d1d","title":"deep-copy"},{"location":"tags/#default","text":"8.12 - \u9ed8\u8ba4\u53c2\u6570","title":"default"},{"location":"tags/#default-constructor","text":"\u9ed8\u8ba4\u6784\u9020\u51fd\u6570","title":"default-constructor"},{"location":"tags/#default-initialization","text":"\u9ed8\u8ba4\u521d\u59cb\u5316","title":"default-initialization"},{"location":"tags/#default-member-initializer","text":"\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c","title":"default-member-initializer"},{"location":"tags/#definition","text":"\u5b9a\u4e49","title":"definition"},{"location":"tags/#delegating","text":"13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570","title":"delegating"},{"location":"tags/#delegating-constructors","text":"\u59d4\u6258\u6784\u9020\u51fd\u6570","title":"delegating-constructors"},{"location":"tags/#delete","text":"11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d 14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57","title":"delete"},{"location":"tags/#dependencies","text":"16.5 - \u4f9d\u8d56\u5173\u7cfb","title":"dependencies"},{"location":"tags/#dependency","text":"\u4f9d\u8d56","title":"dependency"},{"location":"tags/#dereference-operator","text":"\u89e3\u5f15\u7528\u8fd0\u7b97\u7b26","title":"dereference-operator"},{"location":"tags/#derived-class","text":"\u6d3e\u751f\u7c7b","title":"derived-class"},{"location":"tags/#desgin","text":"2.13 - \u5982\u4f55\u8bbe\u8ba1\u7b2c\u4e00\u4e2a\u7a0b\u5e8f","title":"desgin"},{"location":"tags/#designated-initializers","text":"\u6307\u5b9a\u521d\u59cb\u5316","title":"designated-initializers"},{"location":"tags/#destructor","text":"\u6790\u6784\u51fd\u6570","title":"destructor"},{"location":"tags/#destructors","text":"13.9 - \u6790\u6784\u51fd\u6570","title":"destructors"},{"location":"tags/#direct-initialization","text":"\u76f4\u63a5\u521d\u59cb\u5316","title":"direct-initialization"},{"location":"tags/#do-while","text":"7.8 - do-while \u8bed\u53e5","title":"do-while"},{"location":"tags/#downcasting","text":"\u5411\u4e0b\u8f6c\u6362","title":"downcasting"},{"location":"tags/#dumb-pointer","text":"\u7b28\u6307\u9488","title":"dumb-pointer"},{"location":"tags/#dynamic-casting","text":"18.10 - \u52a8\u6001\u7c7b\u578b\u8f6c\u6362","title":"dynamic casting"},{"location":"tags/#dynamic-duration","text":"\u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4","title":"dynamic duration"},{"location":"tags/#dynamic-casts","text":"\u52a8\u6001\u7c7b\u578b\u8f6c\u6362","title":"dynamic-casts"},{"location":"tags/#dynamic-initialization","text":"\u52a8\u6001\u521d\u59cb\u5316","title":"dynamic-initialization"},{"location":"tags/#dynamic-memory","text":"11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d","title":"dynamic-memory"},{"location":"tags/#dynamic-memory-allocation","text":"\u52a8\u6001\u5185\u5b58\u5206\u914d","title":"dynamic-memory-allocation"},{"location":"tags/#early-binding_1","text":"18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a","title":"early-binding"},{"location":"tags/#ellipsis","text":"12.6 - \u7701\u7565\u53f7\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83","title":"ellipsis"},{"location":"tags/#encapsulation_1","text":"13.4 - \u6210\u5458\u8bbf\u95ee\u51fd\u6570\u4e0e\u5c01\u88c5","title":"encapsulation"},{"location":"tags/#enum","text":"10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b 10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09","title":"enum"},{"location":"tags/#enum-class","text":"\u679a\u4e3e\u7c7b","title":"enum-class"},{"location":"tags/#error-handling","text":"7.15 - \u53d1\u73b0\u9519\u8bef\u548c\u5904\u7406\u9519\u8bef 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406","title":"error handling"},{"location":"tags/#evaluation","text":"\u6c42\u503c","title":"evaluation"},{"location":"tags/#excalidraw","text":"Drawing 2023 02 11 13.33.50.excalidraw Template.excalidraw C++ basic onepage Moc onepage","title":"excalidraw"},{"location":"tags/#exception-safety-issue","text":"\u5f02\u5e38\u5b89\u5168\u95ee\u9898","title":"exception-safety-issue"},{"location":"tags/#explicit","text":"14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57","title":"explicit"},{"location":"tags/#explicit-type-conversion","text":"8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362","title":"explicit-type-conversion"},{"location":"tags/#expression","text":"1.10 - \u8868\u8fbe\u5f0f\u7b80\u4ecb","title":"expression"},{"location":"tags/#expression-type","text":"\u8868\u8fbe\u5f0f\u7c7b\u578b","title":"expression type"},{"location":"tags/#external-linkage","text":"\u5916\u90e8\u94fe\u63a5","title":"external-linkage"},{"location":"tags/#external-variable","text":"\u5916\u90e8\u53d8\u91cf","title":"external-variable"},{"location":"tags/#extraction-operator","text":"\u63d0\u53d6\u8fd0\u7b97\u7b26","title":"extraction-operator"},{"location":"tags/#fallthrough","text":"\u8d2f\u7a7f\u5c5e\u6027","title":"fallthrough"},{"location":"tags/#file","text":"23.6 - \u57fa\u672c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa","title":"file"},{"location":"tags/#file-scope","text":"\u6587\u4ef6\u4f5c\u7528\u57df","title":"file-scope"},{"location":"tags/#final","text":"18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b","title":"final"},{"location":"tags/#fixed-array","text":"\u56fa\u5b9a\u6570\u7ec4","title":"fixed-array"},{"location":"tags/#fixed-width","text":"4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t","title":"fixed-width"},{"location":"tags/#float","text":"4.8 - \u6d6e\u70b9\u6570","title":"float"},{"location":"tags/#float-point","text":"5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83","title":"float point"},{"location":"tags/#for","text":"7.7 - \u5faa\u73af\u548c while \u8bed\u53e5 7.9 - for \u8bed\u53e5","title":"for"},{"location":"tags/#for-each","text":"11.13 - for-each \u5faa\u73af","title":"for-each"},{"location":"tags/#fordward-declaration","text":"2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49","title":"fordward declaration"},{"location":"tags/#format-group","text":"\u683c\u5f0f\u7ec4","title":"format-group"},{"location":"tags/#formatting","text":"1.8 - \u7a7a\u767d\u7b26\u548c\u57fa\u672c\u6392\u7248","title":"formatting"},{"location":"tags/#forward-declaration","text":"\u524d\u5411\u58f0\u660e","title":"forward declaration"},{"location":"tags/#friend","text":"13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b 13.16 - \u533f\u540d\u5bf9\u8c61 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b 13.18 - \u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26","title":"friend"},{"location":"tags/#friend-class","text":"\u53cb\u5143\u7c7b","title":"friend-class"},{"location":"tags/#friend-function","text":"\u53cb\u5143\u51fd\u6570","title":"friend-function"},{"location":"tags/#function","text":"12.1 - \u51fd\u6570\u6307\u9488 2.1 - \u51fd\u6570\u7b80\u4ecb 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 2.6 - \u4e3a\u4ec0\u4e48\u51fd\u6570\u6709\u7528\uff0c\u5982\u4f55\u9ad8\u6548\u4f7f\u7528\u51fd\u6570","title":"function"},{"location":"tags/#function-template","text":"8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f","title":"function template"},{"location":"tags/#function-instance","text":"\u51fd\u6570\u5b9e\u4f8b","title":"function-instance"},{"location":"tags/#function-literal","text":"\u51fd\u6570\u5b57\u9762\u91cf","title":"function-literal"},{"location":"tags/#function-pointer","text":"\u51fd\u6570\u6307\u9488","title":"function-pointer"},{"location":"tags/#function-template_1","text":"\u51fd\u6570\u6a21\u677f","title":"function-template"},{"location":"tags/#function-template-instantiation","text":"\u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316","title":"function-template-instantiation"},{"location":"tags/#generic-algorithm","text":"\u6cdb\u578b\u7b97\u6cd5","title":"generic-algorithm"},{"location":"tags/#global","text":"6.4 - \u5168\u5c40\u53d8\u91cf 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf","title":"global"},{"location":"tags/#global-variable","text":"6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c \u5168\u5c40\u53d8\u91cf","title":"global variable"},{"location":"tags/#glossary","text":"\u7f16\u8bd1\u65f6 \u58f0\u660e \u5b9a\u4e49 \u524d\u5411\u58f0\u660e \u5168\u5c40\u53d8\u91cf \u6807\u8bc6\u7b26(identifier) \u5b9e\u4f8b\u5316 \u6307\u4ee4 \u94fe\u63a5\u5668(linker) \u5b8f \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u5f62\u53c2 \u6309\u503c\u4f20\u9012 \u9884\u5904\u7406\u5668\u6307\u4ee4 \u9884\u5904\u7406\u5668 \u7eaf\u58f0\u660e(pure-declaration) \u9650\u5b9a\u7b26 \u91cd\u6784 \u6309\u503c\u8fd4\u56de \u8fd0\u884c\u65f6 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u4f5c\u7528\u57df \u8bed\u53e5 \u7b26\u53f7\u5e38\u91cf","title":"glossary"},{"location":"tags/#goto","text":"7.6 - goto \u8bed\u53e5","title":"goto"},{"location":"tags/#halt","text":"7.11 - \u6302\u8d77\uff08\u63d0\u524d\u7ed3\u675f\u7a0b\u5e8f\uff09","title":"halt"},{"location":"tags/#header","text":"2.11 - \u5934\u6587\u4ef6","title":"header"},{"location":"tags/#header-guards","text":"2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e","title":"header guards"},{"location":"tags/#heap","text":"12.2 - \u6808\u548c\u5806 \u5806","title":"heap"},{"location":"tags/#identifier","text":"1.7 - \u5173\u952e\u5b57\u548c\u6807\u8bc6\u7b26 \u6807\u8bc6\u7b26(identifier)","title":"identifier"},{"location":"tags/#identity","text":"\u8eab\u4efd\u7279\u5f81","title":"identity"},{"location":"tags/#if","text":"4.10 - if \u8bed\u53e5\u7b80\u4ecb 7.2 - if \u8bed\u53e5\u548c\u8bed\u53e5\u5757 7.3 - \u5e38\u89c1\u7684 if \u8bed\u53e5\u9519\u8bef","title":"if"},{"location":"tags/#immediate-functions","text":"\u5373\u65f6\u51fd\u6570","title":"immediate-functions"},{"location":"tags/#implicit-constructor","text":"\u9690\u5f0f\u6784\u9020\u51fd\u6570","title":"implicit-constructor"},{"location":"tags/#implicit-object","text":"\u9690\u5f0f\u5bf9\u8c61","title":"implicit-object"},{"location":"tags/#implicit-type-conversion","text":"\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362","title":"implicit-type-conversion"},{"location":"tags/#inheritance","text":"17.1 - \u7ee7\u627f\u7b80\u4ecb 17.2 - C++\u7ee7\u627f\u57fa\u7840 17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f 17.4 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u548c\u521d\u59cb\u5316 17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 17.6 - \u4e3a\u6d3e\u751f\u7c7b\u6dfb\u52a0\u65b0\u529f\u80fd 17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570 17.9 - \u591a\u91cd\u7ee7\u627f 18.11 - \u4f7f\u7528 << \u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b \u7ee7\u627f","title":"inheritance"},{"location":"tags/#initialization","text":"1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u62f7\u8d1d\u521d\u59cb\u5316 \u76f4\u63a5\u521d\u59cb\u5316 \u5217\u8868\u521d\u59cb\u5316","title":"initialization"},{"location":"tags/#initializer-list","text":"\u521d\u59cb\u5316\u503c\u5217\u8868","title":"initializer-list"},{"location":"tags/#initializer_list","text":"16.7 - std::initializer_list","title":"initializer_list"},{"location":"tags/#inline","text":"6.13 - \u5185\u8054\u51fd\u6570 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf","title":"inline"},{"location":"tags/#inline-namespace","text":"6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4","title":"inline namespace"},{"location":"tags/#inline-function","text":"\u5185\u8054\u51fd\u6570","title":"inline-function"},{"location":"tags/#inline-namespace_1","text":"\u5185\u8054\u547d\u540d\u7a7a\u95f4","title":"inline-namespace"},{"location":"tags/#inline-variables","text":"\u5185\u8054\u53d8\u91cf","title":"inline-variables"},{"location":"tags/#input-manipulator","text":"4.17 - std::string \u7b80\u4ecb","title":"input-manipulator"},{"location":"tags/#input-manipulators","text":"\u8f93\u5165\u64cd\u7eb5\u5668","title":"input-manipulators"},{"location":"tags/#insertion-operator","text":"\u63d2\u5165\u8fd0\u7b97\u7b26","title":"insertion-operator"},{"location":"tags/#instance","text":"\u5b9e\u4f8b","title":"instance"},{"location":"tags/#instantiated","text":"\u5b9e\u4f8b\u5316","title":"instantiated"},{"location":"tags/#instuction","text":"\u6307\u4ee4","title":"instuction"},{"location":"tags/#integer","text":"4.4 - \u6709\u7b26\u53f7\u6574\u578b 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t","title":"integer"},{"location":"tags/#integral-type","text":"\u6574\u578b\u7c7b\u578b","title":"integral-type"},{"location":"tags/#interface-class","text":"\u63a5\u53e3\u7c7b","title":"interface-class"},{"location":"tags/#interface-classes","text":"18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b","title":"interface-classes"},{"location":"tags/#internal-linkage","text":"\u5185\u90e8\u94fe\u63a5","title":"internal-linkage"},{"location":"tags/#internal-variable","text":"\u5185\u90e8\u53d8\u91cf","title":"internal-variable"},{"location":"tags/#io","text":"23.1 - \u8f93\u5165\u8f93\u51fa\u6d41 23.2 - \u4f7f\u7528istream\u5904\u7406\u8f93\u5165 23.3 - \u4f7f\u7528ostream\u548cios\u5904\u7406\u8f93\u51fa 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b 23.5 - \u6d41\u72b6\u6001\u548c\u8f93\u5165\u9a8c\u8bc1 23.6 - \u57fa\u672c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa 23.7 - \u968f\u673a\u6587\u4ef6\u8f93\u5165\u8f93\u51fa","title":"io"},{"location":"tags/#iostream","text":"1.5 - iostream \u7b80\u4ecb\uff1acout\uff0ccin \u548c endl","title":"iostream"},{"location":"tags/#iterating","text":"\u904d\u5386","title":"iterating"},{"location":"tags/#iterator","text":"11.18 \u2014 \u8fed\u4ee3\u5668\u7b80\u4ecb \u8fed\u4ee3\u5668","title":"iterator"},{"location":"tags/#lambda","text":"12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 \u533f\u540d\u51fd\u6570","title":"lambda"},{"location":"tags/#late-binding_1","text":"18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a","title":"late-binding"},{"location":"tags/#link","text":"\u94fe\u63a5","title":"link"},{"location":"tags/#linkage","text":"6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 6.3 - \u5c40\u90e8\u53d8\u91cf 6.6 - \u5185\u90e8\u94fe\u63a5 6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e \u94fe\u63a5\u5c5e\u6027","title":"linkage"},{"location":"tags/#linker","text":"\u94fe\u63a5\u5668(linker)","title":"linker"},{"location":"tags/#list-assignment-operator","text":"\u7c7b\u522b\u8d4b\u503c\u8fd0\u7b97\u7b26","title":"list assignment operator"},{"location":"tags/#list-constructor","text":"\u5217\u8868\u6784\u9020\u51fd\u6570","title":"list-constructor"},{"location":"tags/#list-initialization","text":"\u5217\u8868\u521d\u59cb\u5316","title":"list-initialization"},{"location":"tags/#literal","text":"4.15 - \u5b57\u9762\u91cf","title":"literal"},{"location":"tags/#literals","text":"1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 \u5b57\u9762\u91cf","title":"literals"},{"location":"tags/#local-variable","text":"6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf","title":"local variable"},{"location":"tags/#local-variables","text":"6.3 - \u5c40\u90e8\u53d8\u91cf","title":"local variables"},{"location":"tags/#loop","text":"11.3 - \u6570\u7ec4\u548c\u5faa\u73af","title":"loop"},{"location":"tags/#lvalue_1","text":"\u5de6\u503c","title":"lvalue"},{"location":"tags/#lvalue-references","text":"9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528","title":"lvalue references"},{"location":"tags/#lvalue-reference","text":"\u5de6\u503c\u5f15\u7528","title":"lvalue-reference"},{"location":"tags/#lvalues","text":"9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09","title":"lvalues"},{"location":"tags/#macro","text":"\u5b8f","title":"macro"},{"location":"tags/#member-access-operator","text":"\u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26","title":"member access operator"},{"location":"tags/#member-selection","text":"10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9","title":"member selection"},{"location":"tags/#member-function","text":"\u6210\u5458\u51fd\u6570","title":"member-function"},{"location":"tags/#member-initializer-list","text":"\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868","title":"member-initializer-list"},{"location":"tags/#member-selection-operator","text":"\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26","title":"member-selection-operator"},{"location":"tags/#member-types","text":"\u6210\u5458\u7c7b\u578b","title":"member-types"},{"location":"tags/#member-variable","text":"\u6210\u5458\u53d8\u91cf","title":"member-variable"},{"location":"tags/#members","text":"10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9","title":"members"},{"location":"tags/#memberwise-initialization","text":"\u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316","title":"memberwise initialization"},{"location":"tags/#memberwise-assignment","text":"\u6210\u5458\u4f9d\u6b21\u8d4b\u503c","title":"memberwise-assignment"},{"location":"tags/#memberwise-copy","text":"\u6210\u5458\u4f9d\u6b21\u62f7\u8d1d","title":"memberwise-copy"},{"location":"tags/#memory-leak","text":"\u5185\u5b58\u6cc4\u6f0f","title":"memory-leak"},{"location":"tags/#mixin","text":"None","title":"mixin"},{"location":"tags/#move","text":"M.4 - std::move M.5 - std::move_if_noexcept M.6 \u2014 std::unique_ptr","title":"move"},{"location":"tags/#move-assignment-operator","text":"\u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26","title":"move assignment operator"},{"location":"tags/#move-constructor","text":"\u79fb\u52a8\u6784\u9020\u51fd\u6570","title":"move-constructor"},{"location":"tags/#move-semantics","text":"M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb \u79fb\u52a8\u8bed\u4e49","title":"move-semantics"},{"location":"tags/#multiple-inheritance_1","text":"17.9 - \u591a\u91cd\u7ee7\u627f","title":"multiple-inheritance"},{"location":"tags/#named-cast","text":"\u5177\u540d\u7c7b\u578b\u8f6c\u6362","title":"named-cast"},{"location":"tags/#namespace","text":"2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u547d\u540d\u7a7a\u95f4","title":"namespace"},{"location":"tags/#namespace-aliases","text":"6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26","title":"namespace aliases"},{"location":"tags/#narrowing-convertions","text":"\u7f29\u7a84\u8f6c\u6362","title":"narrowing-convertions"},{"location":"tags/#new","text":"11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d","title":"new"},{"location":"tags/#numeric-conversions","text":"8.3 - \u6570\u503c\u8f6c\u6362","title":"numeric conversions"},{"location":"tags/#numeric-promotion","text":"8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347","title":"numeric promotion"},{"location":"tags/#numeric-promotions","text":"\u6570\u503c\u63d0\u5347","title":"numeric promotions"},{"location":"tags/#numeric-conversions_1","text":"\u6570\u503c\u8f6c\u6362","title":"numeric-conversions"},{"location":"tags/#object","text":"1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf 13.1 - \u6b22\u8fce\u6765\u5230\u9762\u5411\u5bf9\u8c61\u7684\u4e16\u754c 16.1 - \u5bf9\u8c61\u5173\u7cfb 16.2 - \u7ec4\u5408\u5173\u7cfb 16.3 - \u805a\u5408\u5173\u7cfb 16.4 - \u5173\u8054\u5173\u7cfb 16.5 - \u4f9d\u8d56\u5173\u7cfb 16.6 - \u5bb9\u5668\u7c7b 16.7 - std::initializer_list","title":"object"},{"location":"tags/#object-composition","text":"\u5bf9\u8c61\u7ec4\u5408","title":"object-composition"},{"location":"tags/#object-like-macros","text":"\u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f","title":"object-like-macros"},{"location":"tags/#object-slicing","text":"\u5bf9\u8c61\u5207\u7247","title":"object-slicing"},{"location":"tags/#one-definition-rule","text":"\u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule)","title":"one-definition-rule"},{"location":"tags/#operands","text":"\u64cd\u4f5c\u6570","title":"operands"},{"location":"tags/#operator","text":"1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 5.1 - \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 5.3 - \u6c42\u6a21\u548c\u6307\u6570\u8fd0\u7b97 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 5.5 - \u9017\u53f7\u548c\u6761\u4ef6\u8fd0\u7b97\u7b26 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 5.7 - \u903b\u8f91\u8fd0\u7b97\u7b26 \u8fd0\u7b97\u7b26","title":"operator"},{"location":"tags/#operator_1","text":"18.11 - \u4f7f\u7528 << \u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b","title":"operator<<"},{"location":"tags/#optional-parameter","text":"\u53ef\u9009\u53c2\u6570","title":"optional-parameter"},{"location":"tags/#overflow","text":"4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83","title":"overflow"},{"location":"tags/#overlapping","text":"13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570","title":"overlapping"},{"location":"tags/#overload","text":"14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26 14.15 - \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 8.10 - \u91cd\u8f7d\u51fd\u6570\u7684\u533a\u5206 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u4e0d\u660e\u786e\u5339\u914d \u91cd\u8f7d","title":"overload"},{"location":"tags/#overload-resolution","text":"\u91cd\u8f7d\u89e3\u6790","title":"overload-resolution"},{"location":"tags/#overloading","text":"8.9 - \u51fd\u6570\u91cd\u8f7d","title":"overloading"},{"location":"tags/#override","text":"18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b \u91cd\u5199","title":"override"},{"location":"tags/#overriding","text":"17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570","title":"overriding"},{"location":"tags/#overriding-virtualization","text":"18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316","title":"overriding-virtualization"},{"location":"tags/#parameter","text":"2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2","title":"parameter"},{"location":"tags/#parameters","text":"\u5f62\u53c2","title":"parameters"},{"location":"tags/#parent-class","text":"\u7236\u7c7b","title":"parent-class"},{"location":"tags/#parenthesis","text":"14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26","title":"parenthesis"},{"location":"tags/#pass-by-address","text":"\u6309\u5730\u5740\u4f20\u9012","title":"pass-by-address"},{"location":"tags/#pass-by-reference","text":"\u6309\u5f15\u7528\u4f20\u9012","title":"pass-by-reference"},{"location":"tags/#pass-by-value","text":"\u6309\u503c\u4f20\u9012","title":"pass-by-value"},{"location":"tags/#placeholder-types","text":"\u5360\u4f4d\u7b26\u7c7b\u578b","title":"placeholder-types"},{"location":"tags/#pointer","text":"11.15 \u2014 \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u548c\u591a\u7ef4\u6570\u7ec4 11.8 - \u6307\u9488\u548c\u6570\u7ec4 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 9.6 - \u6307\u9488\u7b80\u4ecb 9.7 - \u7a7a\u6307\u9488 9.8 - \u6307\u9488\u548cconst 9.9 - \u6309\u5730\u5740\u4f20\u9012","title":"pointer"},{"location":"tags/#pointer-to-const","text":"\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488","title":"pointer-to-const"},{"location":"tags/#pointers","text":"18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528","title":"pointers"},{"location":"tags/#polymorphism","text":"18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001 \u591a\u6001","title":"polymorphism"},{"location":"tags/#pragma-once","text":"2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e","title":"pragma once"},{"location":"tags/#preprocessor","text":"2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb \u9884\u5904\u7406\u5668\u6307\u4ee4 \u9884\u5904\u7406\u5668","title":"preprocessor"},{"location":"tags/#private-inheritance","text":"\u79c1\u6709\u7ee7\u627f","title":"private-inheritance"},{"location":"tags/#private-member","text":"\u79c1\u6709\u6210\u5458","title":"private-member"},{"location":"tags/#program-organization","text":"2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f","title":"program organization"},{"location":"tags/#protected-inheritance","text":"\u53d7\u4fdd\u62a4\u7ee7\u627f","title":"protected-inheritance"},{"location":"tags/#protected-members","text":"\u53d7\u4fdd\u62a4\u6210\u5458","title":"protected-members"},{"location":"tags/#public-inheritance","text":"\u516c\u5f00\u7ee7\u627f","title":"public-inheritance"},{"location":"tags/#public-member","text":"\u516c\u6709\u6210\u5458","title":"public-member"},{"location":"tags/#pure-declaration","text":"\u7eaf\u58f0\u660e(pure-declaration)","title":"pure-declaration"},{"location":"tags/#pure-virtual","text":"\u7eaf\u865a\u51fd\u6570","title":"pure-virtual"},{"location":"tags/#pure-virtual-functions","text":"18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b","title":"pure-virtual-functions"},{"location":"tags/#qualifier","text":"\u9650\u5b9a\u7b26","title":"qualifier"},{"location":"tags/#random-access-memory","text":"\u968f\u673a\u8bbf\u95ee\u5185\u5b58","title":"random-access-memory"},{"location":"tags/#range-based-for-loops","text":"\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af","title":"range-based-for-loops"},{"location":"tags/#raw-pointer","text":"\u539f\u59cb\u6307\u9488","title":"raw-pointer"},{"location":"tags/#recursion","text":"12.4 - \u9012\u5f52","title":"recursion"},{"location":"tags/#refactoring","text":"\u91cd\u6784","title":"refactoring"},{"location":"tags/#reference","text":"9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de","title":"reference"},{"location":"tags/#reference-container","text":"\u5f15\u7528\u5bb9\u5668","title":"reference-container"},{"location":"tags/#references","text":"18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 M.2 - \u53f3\u503c\u5f15\u7528 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c","title":"references"},{"location":"tags/#reflexive-association","text":"\u53cd\u8eab\u5173\u8054","title":"reflexive association"},{"location":"tags/#reinterpret-casts","text":"\u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362","title":"reinterpret-casts"},{"location":"tags/#return","text":"2.2 - \u51fd\u6570\u8fd4\u56de\u503c 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de","title":"return"},{"location":"tags/#return-by-address","text":"\u6309\u5730\u5740\u8fd4\u56de","title":"return-by-address"},{"location":"tags/#return-by-reference","text":"\u6309\u5f15\u7528\u8fd4\u56de","title":"return-by-reference"},{"location":"tags/#return-by-value","text":"\u6309\u503c\u8fd4\u56de","title":"return-by-value"},{"location":"tags/#return-type-deduction","text":"\u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad","title":"return-type-deduction"},{"location":"tags/#rounding-error","text":"\u820d\u5165\u8bef\u5dee","title":"rounding-error"},{"location":"tags/#runtime","text":"\u8fd0\u884c\u65f6","title":"runtime"},{"location":"tags/#rvalue","text":"\u53f3\u503c","title":"rvalue"},{"location":"tags/#rvalue-reference","text":"\u53f3\u503c\u5f15\u7528","title":"rvalue-reference"},{"location":"tags/#rvalues","text":"9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09","title":"rvalues"},{"location":"tags/#scaling","text":"\u6307\u9488\u7f29\u653e","title":"scaling"},{"location":"tags/#scientific-notation","text":"4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5","title":"scientific notation"},{"location":"tags/#scope","text":"2.5 - \u5c40\u90e8\u4f5c\u7528\u57df 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 \u4f5c\u7528\u57df","title":"scope"},{"location":"tags/#scope-resolution-operator","text":"6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26","title":"scope resolution operator"},{"location":"tags/#scope-resolution-operator_1","text":"\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26","title":"scope-resolution-operator"},{"location":"tags/#scoped-enumerations","text":"10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09","title":"scoped enumerations"},{"location":"tags/#scoped-enumerations_1","text":"\u6709\u4f5c\u7528\u57df\u679a\u4e3e","title":"scoped-enumerations"},{"location":"tags/#segment","text":"\u5185\u5b58\u6bb5","title":"segment"},{"location":"tags/#sentinel","text":"\u54e8\u5175","title":"sentinel"},{"location":"tags/#setup","text":"0.8 - \u4e00\u4e9b\u5178\u578b\u7684 C++ \u95ee\u9898","title":"setup"},{"location":"tags/#shadow","text":"\u53d8\u91cf\u906e\u853d","title":"shadow"},{"location":"tags/#shallow-copy","text":"14.16 - \u6d45\u62f7\u8d1d\u548c\u6df1\u62f7\u8d1d 14.17 - \u91cd\u8f7d\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u6a21\u677f","title":"shallow copy"},{"location":"tags/#shallow-copy_1","text":"\u6d45\u62f7\u8d1d","title":"shallow-copy"},{"location":"tags/#shared_ptr","text":"M.7 \u2014 std::shared_ptr M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898","title":"shared_ptr"},{"location":"tags/#side-effects","text":"\u526f\u4f5c\u7528","title":"side-effects"},{"location":"tags/#signature","text":"\u7b7e\u540d","title":"signature"},{"location":"tags/#size_t","text":"4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t","title":"size_t"},{"location":"tags/#sizeof","text":"4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26","title":"sizeof"},{"location":"tags/#smart-pointer-class","text":"\u667a\u80fd\u6307\u9488\u7c7b","title":"smart pointer class"},{"location":"tags/#smart-pointer","text":"\u667a\u80fd\u6307\u9488","title":"smart-pointer"},{"location":"tags/#smart-pointers","text":"M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb","title":"smart-pointers"},{"location":"tags/#sort","text":"11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f","title":"sort"},{"location":"tags/#stack","text":"12.2 - \u6808\u548c\u5806 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a \u6808","title":"stack"},{"location":"tags/#stack-frame","text":"\u6808\u5e27","title":"stack-frame"},{"location":"tags/#stack-overflow","text":"\u5806\u6808\u6ea2\u51fa","title":"stack-overflow"},{"location":"tags/#standard-stream","text":"\u6807\u51c6\u6d41","title":"standard-stream"},{"location":"tags/#statement","text":"6.1 - \u590d\u5408\u8bed\u53e5\uff08\u8bed\u53e5\u5757\uff09 \u8bed\u53e5","title":"statement"},{"location":"tags/#statements","text":"1.1 - \u7a0b\u5e8f\u7ed3\u6784\u548c\u8bed\u53e5","title":"statements"},{"location":"tags/#static","text":"6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf","title":"static"},{"location":"tags/#static-membe-function","text":"13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570","title":"static membe function"},{"location":"tags/#static-member","text":"13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316","title":"static member"},{"location":"tags/#static-member-variable","text":"13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf","title":"static member variable"},{"location":"tags/#static-cast","text":"8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast","title":"static-cast"},{"location":"tags/#static-casts","text":"\u9759\u6001\u7c7b\u578b\u8f6c\u6362","title":"static-casts"},{"location":"tags/#static-initialization","text":"\u9759\u6001\u521d\u59cb\u5316","title":"static-initialization"},{"location":"tags/#static-member-functions","text":"\u9759\u6001\u6210\u5458\u51fd\u6570","title":"static-member-functions"},{"location":"tags/#static-member-variables","text":"\u9759\u6001\u6210\u5458\u53d8\u91cf","title":"static-member-variables"},{"location":"tags/#static-memory-allocation","text":"\u9759\u6001\u5185\u5b58\u5206\u914d","title":"static-memory-allocation"},{"location":"tags/#static-storage-duration","text":"\u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4","title":"static-storage-duration"},{"location":"tags/#static-variables","text":"\u9759\u6001\u53d8\u91cf","title":"static-variables"},{"location":"tags/#static_assert","text":"7.17 - \u65ad\u8a00\u548c static_assert","title":"static_assert"},{"location":"tags/#static_cast","text":"4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast 8.3 - \u6570\u503c\u8f6c\u6362","title":"static_cast"},{"location":"tags/#stdarray","text":"11.16 \u2014 \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb","title":"std::array"},{"location":"tags/#stdstring_view","text":"11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09","title":"std::string_view"},{"location":"tags/#stl","text":"21.1 - \u6807\u51c6\u5e93 21.2 - STL\u5bb9\u5668 21.3 - STL\u8fed\u4ee3\u5668 21.4 - STL\u7b97\u6cd5","title":"stl"},{"location":"tags/#storage-duration","text":"6.3 - \u5c40\u90e8\u53d8\u91cf 6.4 - \u5168\u5c40\u53d8\u91cf","title":"storage duration"},{"location":"tags/#storage-duration_1","text":"\u5b58\u50a8\u6301\u7eed\u65f6\u95f4","title":"storage-duration"},{"location":"tags/#string","text":"11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 22.1 - std::string \u548c std::wstring 22.2 - std-string \u7684\u6784\u9020\u548c\u6790\u6784 22.3 - std-string \u7684\u957f\u5ea6\u548c\u5bb9\u91cf 22.4 - std-string \u5b57\u7b26\u8bbf\u95ee\u548cC\u5b57\u7b26\u6570\u7ec4\u8f6c\u6362 22.5 - std-string \u8d4b\u503c\u548c\u4ea4\u6362 22.6 - std-string \u8ffd\u52a0 22.7 - std-string \u63d2\u5165 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b 4.17 - std::string \u7b80\u4ecb","title":"string"},{"location":"tags/#string_view","text":"4.18 - std::string_view \u7b80\u4ecb","title":"string_view"},{"location":"tags/#strong-except","text":"\u5f3a\u5f02\u5e38\u4fdd\u8bc1","title":"strong-except"},{"location":"tags/#struct","text":"10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 10.8 - \u7ed3\u6784\u4f53\u4f20\u9012\u53ca\u5176\u4ed6 10.9 - \u57fa\u4e8e\u6307\u9488\u548c\u5f15\u7528\u7684\u6210\u5458\u9009\u62e9","title":"struct"},{"location":"tags/#struct-padding","text":"\u7ed3\u6784\u4f53\u586b\u5145","title":"struct-padding"},{"location":"tags/#subscript","text":"14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26","title":"subscript"},{"location":"tags/#subscripts","text":"\u4e0b\u6807","title":"subscripts"},{"location":"tags/#summary","text":"1.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - C++\u57fa\u7840 10.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u679a\u4e3e\u548c\u7ed3\u6784\u4f53 10.y - \u5b66\u4f1a\u4f7f\u7528C++\u53c2\u8003\u624b\u518c 11.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5206\u914d 12.8 - lambda \u95ed\u5305 12.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570 13.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u9762\u5411\u5bf9\u8c61\u57fa\u7840 14.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26\u91cd\u8f7d 16.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5bf9\u8c61\u5173\u7cfb 17.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7ee7\u627f 18.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u865a\u51fd\u6570 19.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6a21\u677f\u548c\u7c7b 2.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570\u4e0e\u6587\u4ef6 20.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f02\u5e38 3.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4ee3\u7801\u8c03\u8bd5 4.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u57fa\u672c\u6570\u636e\u7c7b\u578b 5.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 7.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406 8.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d 9.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f15\u7528\u548c\u6307\u9488 M.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u79fb\u52a8\u548c\u667a\u80fd\u6307\u9488","title":"summary"},{"location":"tags/#super-class","text":"\u8d85\u7c7b","title":"super-class"},{"location":"tags/#switch","text":"7.4 - switch \u8bed\u53e5\u57fa\u7840 7.5 - switch fallthrough\u5c5e\u6027\u548c\u4f5c\u7528\u57df","title":"switch"},{"location":"tags/#symbolic-constants","text":"11.10 - C \u98ce\u683c\u5b57\u7b26\u4e32\u7b26\u53f7\u5e38\u91cf","title":"symbolic constants"},{"location":"tags/#symbolic-constants_1","text":"\u7b26\u53f7\u5e38\u91cf","title":"symbolic-constants"},{"location":"tags/#template-class-specialization","text":"\u7c7b\u6a21\u677f\u7279\u5316","title":"template class specialization"},{"location":"tags/#testing","text":"7.12 - \u4ee3\u7801\u6d4b\u8bd5","title":"testing"},{"location":"tags/#the-diamond-problem","text":"\u83f1\u5f62\u7ee7\u627f\u95ee\u9898","title":"the-diamond-problem"},{"location":"tags/#this","text":"13.10 - \u9690\u85cf\u7684this\u6307\u9488","title":"this"},{"location":"tags/#translation","text":"\u7ffb\u8bd1","title":"translation"},{"location":"tags/#type","text":"10.1 - \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7b80\u4ecb","title":"type"},{"location":"tags/#type-conversion","text":"4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362","title":"type conversion"},{"location":"tags/#type-deduction","text":"8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u63a8\u65ad","title":"type deduction"},{"location":"tags/#type-aliases","text":"\u7c7b\u578b\u522b\u540d","title":"type-aliases"},{"location":"tags/#type-conversion_1","text":"\u7c7b\u578b\u8f6c\u6362","title":"type-conversion"},{"location":"tags/#type-definition","text":"\u7c7b\u578b\u5b9a\u4e49","title":"type-definition"},{"location":"tags/#type-inference","text":"\u7c7b\u578b\u63a8\u65ad","title":"type-inference"},{"location":"tags/#type-safe","text":"\u7c7b\u578b\u5b89\u5168","title":"type-safe"},{"location":"tags/#type-signature","text":"\u7c7b\u578b\u7b7e\u540d","title":"type-signature"},{"location":"tags/#typecast","text":"14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26","title":"typecast"},{"location":"tags/#typedef","text":"8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d","title":"typedef"},{"location":"tags/#typeid","text":"typeid \u8fd0\u7b97\u7b26","title":"typeid"},{"location":"tags/#typeinfo","text":"8.4 - \u7b97\u672f\u8f6c\u6362","title":"typeinfo"},{"location":"tags/#unary","text":"14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26","title":"unary"},{"location":"tags/#undefined-behavior","text":"1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 4.4 - \u6709\u7b26\u53f7\u6574\u578b","title":"undefined behavior"},{"location":"tags/#undefined-behavior_1","text":"\u672a\u5b9a\u4e49\u884c\u4e3a","title":"undefined-behavior"},{"location":"tags/#uniform-initialization","text":"\u7edf\u4e00\u521d\u59cb\u5316","title":"uniform initialization"},{"location":"tags/#unmanned-namespace","text":"6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4","title":"unmanned namespace"},{"location":"tags/#unnamed-namespace","text":"\u533f\u540d\u547d\u540d\u7a7a\u95f4","title":"unnamed-namespace"},{"location":"tags/#unscoped-enumerations","text":"10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b 10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa","title":"unscoped enumerations"},{"location":"tags/#unscoped-enumerations_1","text":"\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b","title":"unscoped-enumerations"},{"location":"tags/#upcasting","text":"\u5411\u4e0a\u8f6c\u6362","title":"upcasting"},{"location":"tags/#using","text":"6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4","title":"using"},{"location":"tags/#using-declaration","text":"using \u58f0\u660e","title":"using-declaration"},{"location":"tags/#using-directive","text":"using \u6307\u4ee4","title":"using-directive"},{"location":"tags/#value-category","text":"\u503c\u7c7b\u578b","title":"value-category"},{"location":"tags/#value-container","text":"\u503c\u5bb9\u5668","title":"value-container"},{"location":"tags/#value-initialization","text":"\u503c\u521d\u59cb\u5316","title":"value-initialization"},{"location":"tags/#value-preserving","text":"\u503c\u4fdd\u7559","title":"value-preserving"},{"location":"tags/#variable","text":"1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316","title":"variable"},{"location":"tags/#variable-shadowing","text":"6.5 - \u53d8\u91cf\u906e\u853d","title":"variable shadowing"},{"location":"tags/#vector","text":"11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a","title":"vector"},{"location":"tags/#virtual-functions","text":"18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001","title":"virtual functions"},{"location":"tags/#virtual-assignment","text":"18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316","title":"virtual-assignment"},{"location":"tags/#virtual-base-class","text":"\u865a\u57fa\u7c7b","title":"virtual-base-class"},{"location":"tags/#virtual-base-classes","text":"18.8 - \u865a\u57fa\u7c7b 18.9 - \u5bf9\u8c61\u5207\u7247","title":"virtual-base-classes"},{"location":"tags/#virtual-destructor","text":"\u865a\u6790\u6784\u51fd\u6570","title":"virtual-destructor"},{"location":"tags/#virtual-destructors","text":"18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316","title":"virtual-destructors"},{"location":"tags/#virtual-function","text":"\u865a\u51fd\u6570","title":"virtual-function"},{"location":"tags/#virtual-table","text":"18.6 - \u865a\u51fd\u6570\u8868 \u865a\u8868","title":"virtual-table"},{"location":"tags/#void","text":"11.14 \u2014 void \u6307\u9488 2.3 - void \u51fd\u6570 4.2 - Void","title":"void"},{"location":"tags/#void-pointer","text":"void \u6307\u9488","title":"void pointer"},{"location":"tags/#weak_ptr","text":"M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898","title":"weak_ptr"},{"location":"tags/#while","text":"7.7 - \u5faa\u73af\u548c while \u8bed\u53e5","title":"while"},{"location":"tags/#wild-pointer","text":"\u91ce\u6307\u9488","title":"wild-pointer"},{"location":"tags/#word","text":"\u5b57\u957f","title":"word"},{"location":"tags/#wstring","text":"22.1 - std::string \u548c std::wstring","title":"wstring"},{"location":"tags/#zero-initialization","text":"0\u521d\u59cb\u5316","title":"zero-initialization"},{"location":"tags/#function-prototype","text":"\u51fd\u6570\u539f\u578b","title":"\u51fd\u6570\u539f\u578b(function prototype)"},{"location":"tags/#_1","text":"\u521d\u59cb\u5316","title":"\u521d\u59cb\u5316"},{"location":"tags/#initializer","text":"\u521d\u59cb\u5316\u503c","title":"\u521d\u59cb\u5316\u503c(initializer)"},{"location":"tags/#_2","text":"\u52a8\u6001\u6570\u7ec4","title":"\u52a8\u6001\u6570\u7ec4"},{"location":"tags/#objec","text":"\u5bf9\u8c61","title":"\u5bf9\u8c61(objec)"},{"location":"tags/#_3","text":"\u62bd\u8c61\u7c7b","title":"\u62bd\u8c61\u7c7b"},{"location":"tags/#_4","text":"None","title":"\u62ec\u53f7\u521d\u59cb\u5316"},{"location":"tags/#_5","text":"\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26","title":"\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26"},{"location":"tags/#_6","text":"\u6784\u9020\u51fd\u6570","title":"\u6784\u9020\u51fd\u6570"},{"location":"tags/#_7","text":"\u6a21\u677f\u7c7b\u578b\u53c2\u6570","title":"\u6a21\u677f\u7c7b\u578b\u53c2\u6570"},{"location":"tags/#_8","text":"\u79bb\u5f00\u4f5c\u7528\u57df","title":"\u79bb\u5f00\u4f5c\u7528\u57df"},{"location":"tags/#trivial-conversion","text":"\u7b80\u5355\u8f6c\u6362","title":"\u7b80\u5355\u8f6c\u6362(trivial conversion)"},{"location":"tags/#_9","text":"\u7c7b\u5b9e\u4f8b","title":"\u7c7b\u5b9e\u4f8b"},{"location":"tags/#snake_case","text":"\u86c7\u5f62\u547d\u540d\u6cd5","title":"\u86c7\u5f62\u547d\u540d\u6cd5(snake_case)"},{"location":"tags/#_10","text":"\u5173\u8054 \u5173\u8054\u5bb9\u5668 \u7ed1\u5b9a C \u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362 \u65e9\u671f\u7ed1\u5b9a \u5c01\u88c5 \u8868\u8fbe\u5f0f\u53c2\u6570 \u7f1d\u5408\u602a\u5bf9\u8c61 \u540e\u671f\u7ed1\u5b9a \u591a\u91cd\u7ee7\u627f \u540d\u79f0\u4fee\u9970 \u5dee\u4e00\u9519\u8bef \u8f93\u51fa\u64cd\u7eb5\u5668 \u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09 \u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f(RTTI) \u987a\u5e8f\u5bb9\u5668 \u6a21\u677f\u7279\u5316 \u7f29\u5199\u51fd\u6570\u6a21\u677f \u62bd\u8c61\u7c7b \u62bd\u8c61 \u6210\u5458\u8bbf\u95ee\u51fd\u6570 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u53d6\u5730\u5740\u64cd\u4f5c\u7b26 & \u805a\u5408\u6570\u636e\u7c7b\u578b \u805a\u5408\u521d\u59cb\u5316 \u805a\u5408 \u4e0d\u660e\u786e\u5339\u914d \u533f\u540d\u5bf9\u8c61 \u5b9e\u53c2 \u5143\u6570 \u81ea\u52a8\u5185\u5b58\u5206\u914d \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u57fa\u7c7b \u4f4d None \u6309\u4f4d\u6216 bss\u6bb5 \u5b57\u8282 \u8c03\u7528\u6808 \u53ef\u8c03\u7528\u5bf9\u8c61 \u56de\u8c03\u51fd\u6570 \u5b50\u7c7b \u7c7b\u5b9e\u4f8b \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad \u7c7b\u6a21\u677f \u7c7b\u7c7b\u578b \u95ed\u5305 \u4ee3\u7801\u6bb5 \u7f16\u8bd1\u5668 \u7ec4\u5408\u7c7b\u578b const \u7c7b\u578b\u8f6c\u6362 const \u6210\u5458\u51fd\u6570 \u5e38\u91cf\u6307\u9488 \u5e38\u91cf\u8868\u8fbe\u5f0f \u6784\u9020\u51fd\u6570 \u5bb9\u5668\u9002\u914d\u5668 \u5bb9\u5668\u7c7b \u8f6c\u6362\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u521d\u59cb\u5316 \u62f7\u8d1d\u8bed\u4e49 \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b \u60ac\u5782 \u6570\u636e\u6bb5 \u6df1\u62f7\u8d1d \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 \u9ed8\u8ba4\u521d\u59cb\u5316 \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c \u59d4\u6258\u6784\u9020\u51fd\u6570 \u4f9d\u8d56 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u6d3e\u751f\u7c7b \u6307\u5b9a\u521d\u59cb\u5316 \u6790\u6784\u51fd\u6570 \u76f4\u63a5\u521d\u59cb\u5316 \u5411\u4e0b\u8f6c\u6362 \u7b28\u6307\u9488 \u52a8\u6001\u6570\u7ec4 \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u52a8\u6001\u521d\u59cb\u5316 \u52a8\u6001\u5185\u5b58\u5206\u914d \u679a\u4e3e\u7c7b \u6c42\u503c \u5f02\u5e38\u5b89\u5168\u95ee\u9898 \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u8868\u8fbe\u5f0f\u7c7b\u578b \u5916\u90e8\u94fe\u63a5 \u5916\u90e8\u53d8\u91cf \u63d0\u53d6\u8fd0\u7b97\u7b26 \u8d2f\u7a7f\u5c5e\u6027 \u6587\u4ef6\u4f5c\u7528\u57df \u56fa\u5b9a\u6570\u7ec4 \u683c\u5f0f\u7ec4 \u53cb\u5143\u7c7b \u53cb\u5143\u51fd\u6570 \u51fd\u6570\u5b9e\u4f8b \u51fd\u6570\u5b57\u9762\u91cf \u51fd\u6570\u6307\u9488 \u51fd\u6570\u539f\u578b \u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316 \u51fd\u6570\u6a21\u677f \u6cdb\u578b\u7b97\u6cd5 \u79bb\u5f00\u4f5c\u7528\u57df \u5806 \u8eab\u4efd\u7279\u5f81 \u5373\u65f6\u51fd\u6570 \u9690\u5f0f\u6784\u9020\u51fd\u6570 \u9690\u5f0f\u5bf9\u8c61 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u7ee7\u627f \u521d\u59cb\u5316 \u521d\u59cb\u5316\u503c\u5217\u8868 \u521d\u59cb\u5316\u503c \u5185\u8054\u51fd\u6570 \u5185\u8054\u547d\u540d\u7a7a\u95f4 \u5185\u8054\u53d8\u91cf \u8f93\u5165\u64cd\u7eb5\u5668 \u63d2\u5165\u8fd0\u7b97\u7b26 \u5b9e\u4f8b \u6574\u578b\u7c7b\u578b \u63a5\u53e3\u7c7b \u5185\u90e8\u94fe\u63a5 \u5185\u90e8\u53d8\u91cf \u904d\u5386 \u8fed\u4ee3\u5668 \u533f\u540d\u51fd\u6570 \u94fe\u63a5 \u94fe\u63a5\u5c5e\u6027 \u7c7b\u522b\u8d4b\u503c\u8fd0\u7b97\u7b26 \u5217\u8868\u6784\u9020\u51fd\u6570 \u5217\u8868\u521d\u59cb\u5316 \u5b57\u9762\u91cf \u5de6\u503c\u5f15\u7528 \u5de6\u503c \u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26 \u6210\u5458\u51fd\u6570 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 \u6210\u5458\u7c7b\u578b \u6210\u5458\u53d8\u91cf \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \u6210\u5458\u4f9d\u6b21\u8d4b\u503c \u6210\u5458\u4f9d\u6b21\u62f7\u8d1d \u5185\u5b58\u6cc4\u6f0f None \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 \u79fb\u52a8\u6784\u9020\u51fd\u6570 \u79fb\u52a8\u8bed\u4e49 \u5177\u540d\u7c7b\u578b\u8f6c\u6362 \u547d\u540d\u7a7a\u95f4 \u7f29\u7a84\u8f6c\u6362 \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u6570\u503c\u63d0\u5347 \u6570\u503c\u8f6c\u6362 \u5bf9\u8c61\u7ec4\u5408 \u5bf9\u8c61\u5207\u7247 \u5bf9\u8c61 \u64cd\u4f5c\u6570 \u8fd0\u7b97\u7b26 \u8d85\u51fa\u4f5c\u7528\u57df \u91cd\u8f7d\u89e3\u6790 \u91cd\u8f7d \u91cd\u5199 \u7236\u7c7b \u6309\u5730\u5740\u4f20\u9012 \u6309\u5f15\u7528\u4f20\u9012 \u5360\u4f4d\u7b26\u7c7b\u578b \u6307\u5411\u5e38\u91cf\u7684\u6307\u9488 \u591a\u6001 \u79c1\u6709\u7ee7\u627f \u79c1\u6709\u6210\u5458 \u53d7\u4fdd\u62a4\u7ee7\u627f \u53d7\u4fdd\u62a4\u6210\u5458 \u516c\u5f00\u7ee7\u627f \u516c\u6709\u6210\u5458 \u7eaf\u865a\u51fd\u6570 \u968f\u673a\u8bbf\u95ee\u5185\u5b58 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u539f\u59cb\u6307\u9488 \u5f15\u7528\u5bb9\u5668 \u53cd\u8eab\u5173\u8054 \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 \u6309\u5730\u5740\u8fd4\u56de \u6309\u5f15\u7528\u8fd4\u56de \u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad \u820d\u5165\u8bef\u5dee \u53f3\u503c\u5f15\u7528 \u53f3\u503c \u6307\u9488\u7f29\u653e \u6709\u4f5c\u7528\u57df\u679a\u4e3e \u5185\u5b58\u6bb5 \u54e8\u5175 \u53d8\u91cf\u906e\u853d \u6d45\u62f7\u8d1d \u526f\u4f5c\u7528 \u7b7e\u540d \u667a\u80fd\u6307\u9488\u7c7b \u667a\u80fd\u6307\u9488 \u86c7\u5f62\u547d\u540d\u6cd5 \u6808\u5e27 \u5806\u6808\u6ea2\u51fa \u6808 \u6807\u51c6\u6d41 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u9759\u6001\u521d\u59cb\u5316 \u9759\u6001\u6210\u5458\u51fd\u6570 \u9759\u6001\u6210\u5458\u53d8\u91cf \u9759\u6001\u5185\u5b58\u5206\u914d \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u9759\u6001\u53d8\u91cf \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u5f3a\u5f02\u5e38\u4fdd\u8bc1 \u7ed3\u6784\u4f53\u586b\u5145 \u4e0b\u6807 \u8d85\u7c7b \u8bed\u6cd5 \u7c7b\u6a21\u677f\u7279\u5316 \u6a21\u677f\u7c7b\u578b\u53c2\u6570 \u83f1\u5f62\u7ee7\u627f\u95ee\u9898 \u7ffb\u8bd1 \u7b80\u5355\u8f6c\u6362 \u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u522b\u540d \u7c7b\u578b\u8f6c\u6362 \u7c7b\u578b\u5b9a\u4e49 \u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u5b89\u5168 \u7c7b\u578b\u7b7e\u540d typeid \u8fd0\u7b97\u7b26 \u672a\u5b9a\u4e49\u884c\u4e3a \u7edf\u4e00\u521d\u59cb\u5316 \u533f\u540d\u547d\u540d\u7a7a\u95f4 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u5411\u4e0a\u8f6c\u6362 using \u58f0\u660e using \u6307\u4ee4 \u503c\u7c7b\u578b \u503c\u5bb9\u5668 \u503c\u521d\u59cb\u5316 \u503c\u4fdd\u7559 \u865a\u57fa\u7c7b \u865a\u6790\u6784\u51fd\u6570 \u865a\u51fd\u6570 \u865a\u8868 void \u6307\u9488 \u91ce\u6307\u9488 \u5b57\u957f 0\u521d\u59cb\u5316 None","title":"\u8bcd\u6c47\u8868"},{"location":"tags/#syntax_1","text":"\u8bed\u6cd5","title":"\u8bed\u6cd5(syntax)"},{"location":"tags/#out-of-scope","text":"\u8d85\u51fa\u4f5c\u7528\u57df","title":"\u8d85\u51fa\u4f5c\u7528\u57df(out of scope)"},{"location":"tags/#_11","text":"\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316","title":"\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316"}]} \ No newline at end of file +{"config":{"indexing":"full","lang":["en"],"min_search_length":3,"prebuild_index":false,"separator":"[\\s\\-]+"},"docs":[{"location":"","text":"LearnCpp \u4e2d\u6587\u7ffb\u8bd1\u7ad9\u70b9 \u672c\u7ad9\u51fa\u4e8e\u5b66\u4e60\u7684\u76ee\u7684\uff0c\u5bf9 https://www.learncpp.com \u7ad9\u70b9\u7684\u5185\u5bb9\u8fdb\u884c\u4e86\u7ffb\u8bd1\u3002Learn C++ \u662f\u6211\u770b\u5230\u7684\u6700\u57fa\u7840\u3001\u6700\u7ec6\u81f4\u4e5f\u6700\u5b9e\u9645\u7684C++\u521d\u5b66\u8005\u6559\u7a0b\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u8be5\u7f51\u7ad9\u5b8c\u5168\u514d\u8d39\u800c\u4e14\u4ece2007\u5e74\u6d3b\u8dc3\u81f3\u4eca\uff082022\u5e74\uff09\uff0c\u5e0c\u671b\u5927\u5bb6\u5728\u9605\u8bfb\u672c\u4e2d\u6587\u8bd1\u672c\u7684\u540c\u65f6\uff0c\u80fd\u591f\u5230\u539f\u7f51\u7ad9\u53bb\u770b\u770b\uff0c\u901a\u8fc7\u6d4f\u89c8\u5e7f\u544a\u6216\u8d5e\u52a9\u7684\u65b9\u5f0f\u652f\u6301\u539f\u4f5c\u8005Alex\u3002 \u6b64\u5916\uff0c\u7531\u4e8e\u539f\u8bfe\u7a0b\u975e\u5e38\u57fa\u7840\uff0c\u4e8b\u65e0\u5de8\u7ec6\u5730\u4ecb\u7ecd\u4e86\u8bed\u8a00\u7684\u65b9\u65b9\u9762\u9762\uff0c\u5bf9\u4e8e\u6709\u5176\u4ed6\u8bed\u8a00\u7f16\u7a0b\u57fa\u7840\u7684\u8bfb\u8005\u6765\u8bf4\u53ef\u80fd\u6709\u4e9b\u5570\u55e6\uff0c\u56e0\u6b64\u8bd1\u8005\u5728\u7ffb\u8bd1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4e5f\u5bf9\u6bcf\u8282\u8bfe\u7684\u77e5\u8bc6\u70b9\u8fdb\u884c\u4e86\u63d0\u70bc\uff0c\u5c06\u6bcf\u8282\u8bfe\u4e2d\u6700\u4e3a\u7cbe\u534e\u7684\u90e8\u5206\u5217\u5728\u6587\u7ae0\u5f00\u5934\u90e8\u5206\uff0c\u4f9b\u6b64\u7c7b\u8bfb\u8005\u53c2\u8003\u3002 \u7ffb\u8bd1\u987a\u5e8f\uff08\u6309\u7ae0\u8282\uff09 \u57fa\u7840\uff1a1\u30012\u30014\u30015\u30016\u30018\u30019\u300111\u300121\u300122\u300123\u30017\u300112 \u9762\u5411\u5bf9\u8c61\uff1a13\u300114\u300115\u300116\u300117\u300118\u300119\u300120 \u5176\u4ed6\uff1a3\u3001M\u3001O\u3001A\u3001B\u3001C \u672c\u9879\u76ee\u7684\u4f18\u52bf \u8001\u53f8\u673a\u4e2d\u6587\u7cbe\u8bd1\uff08\u5e9f\u8bdd\uff09 \u66f4\u52a0\u7f8e\u89c2\u7684\u6392\u7248\u548c\u6e32\u67d3\u6548\u679c \u65e0\u5e7f\u544a \u63d0\u53d6\u4e86\u5feb\u901f\u8bb0\u5fc6\u70b9\u7528\u4e8e\u590d\u4e60 \u901a\u8fc7\u5bc6\u96c6\u7684\u4ea4\u53c9\u5f15\u7528\u3001\u5185\u90e8\u94fe\u63a5\u548ctag\uff0c\u5e2e\u52a9\u8bfb\u8005\u66f4\u52a0\u65b9\u4fbf\u5730\u627e\u5230\u76f8\u5173\u5185\u5bb9\u5e76\u5efa\u7acb\u7f51\u72b6\u601d\u7ef4 \u672c\u9879\u76ee\u7684\u52a3\u52bf \u548c\u539f\u7248\u672c\u5728\u66f4\u65b0\u901f\u5ea6\u4e0a\u5b58\u5728\u5dee\u5f02\uff0c\u66f4\u65b0\u548c\u52d8\u8bef\u90fd\u662f\u6ede\u540e\u7684\uff08\u5e9f\u8bdd\uff09 \u6682\u672a\u5305\u542b\u6bcf\u8282\u8bfe\u7684\u5c0f\u6d4b\u9a8c\u90e8\u5206 \u6ca1\u6709\u5305\u542b\u539f\u6587\u4e0b\u65b9\u7684\u7f51\u53cb\u8ba8\u8bba\u5185\u5bb9 \u8bd1\u8005\u7684\u5176\u4ed6\u7ffb\u8bd1\u9879\u76ee\uff1a \u9879\u76ee Star \u8ba1\u7b97\u673a\u6559\u80b2\u4e2d\u7f3a\u5931\u7684\u4e00\u8bfe - The Missing Semester awesome-python-cn awesome-go-cn","title":"\u5173\u4e8e\u672c\u7ad9"},{"location":"#learncpp","text":"\u672c\u7ad9\u51fa\u4e8e\u5b66\u4e60\u7684\u76ee\u7684\uff0c\u5bf9 https://www.learncpp.com \u7ad9\u70b9\u7684\u5185\u5bb9\u8fdb\u884c\u4e86\u7ffb\u8bd1\u3002Learn C++ \u662f\u6211\u770b\u5230\u7684\u6700\u57fa\u7840\u3001\u6700\u7ec6\u81f4\u4e5f\u6700\u5b9e\u9645\u7684C++\u521d\u5b66\u8005\u6559\u7a0b\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u8be5\u7f51\u7ad9\u5b8c\u5168\u514d\u8d39\u800c\u4e14\u4ece2007\u5e74\u6d3b\u8dc3\u81f3\u4eca\uff082022\u5e74\uff09\uff0c\u5e0c\u671b\u5927\u5bb6\u5728\u9605\u8bfb\u672c\u4e2d\u6587\u8bd1\u672c\u7684\u540c\u65f6\uff0c\u80fd\u591f\u5230\u539f\u7f51\u7ad9\u53bb\u770b\u770b\uff0c\u901a\u8fc7\u6d4f\u89c8\u5e7f\u544a\u6216\u8d5e\u52a9\u7684\u65b9\u5f0f\u652f\u6301\u539f\u4f5c\u8005Alex\u3002 \u6b64\u5916\uff0c\u7531\u4e8e\u539f\u8bfe\u7a0b\u975e\u5e38\u57fa\u7840\uff0c\u4e8b\u65e0\u5de8\u7ec6\u5730\u4ecb\u7ecd\u4e86\u8bed\u8a00\u7684\u65b9\u65b9\u9762\u9762\uff0c\u5bf9\u4e8e\u6709\u5176\u4ed6\u8bed\u8a00\u7f16\u7a0b\u57fa\u7840\u7684\u8bfb\u8005\u6765\u8bf4\u53ef\u80fd\u6709\u4e9b\u5570\u55e6\uff0c\u56e0\u6b64\u8bd1\u8005\u5728\u7ffb\u8bd1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4e5f\u5bf9\u6bcf\u8282\u8bfe\u7684\u77e5\u8bc6\u70b9\u8fdb\u884c\u4e86\u63d0\u70bc\uff0c\u5c06\u6bcf\u8282\u8bfe\u4e2d\u6700\u4e3a\u7cbe\u534e\u7684\u90e8\u5206\u5217\u5728\u6587\u7ae0\u5f00\u5934\u90e8\u5206\uff0c\u4f9b\u6b64\u7c7b\u8bfb\u8005\u53c2\u8003\u3002","title":"LearnCpp \u4e2d\u6587\u7ffb\u8bd1\u7ad9\u70b9"},{"location":"#_1","text":"\u57fa\u7840\uff1a1\u30012\u30014\u30015\u30016\u30018\u30019\u300111\u300121\u300122\u300123\u30017\u300112 \u9762\u5411\u5bf9\u8c61\uff1a13\u300114\u300115\u300116\u300117\u300118\u300119\u300120 \u5176\u4ed6\uff1a3\u3001M\u3001O\u3001A\u3001B\u3001C \u672c\u9879\u76ee\u7684\u4f18\u52bf \u8001\u53f8\u673a\u4e2d\u6587\u7cbe\u8bd1\uff08\u5e9f\u8bdd\uff09 \u66f4\u52a0\u7f8e\u89c2\u7684\u6392\u7248\u548c\u6e32\u67d3\u6548\u679c \u65e0\u5e7f\u544a \u63d0\u53d6\u4e86\u5feb\u901f\u8bb0\u5fc6\u70b9\u7528\u4e8e\u590d\u4e60 \u901a\u8fc7\u5bc6\u96c6\u7684\u4ea4\u53c9\u5f15\u7528\u3001\u5185\u90e8\u94fe\u63a5\u548ctag\uff0c\u5e2e\u52a9\u8bfb\u8005\u66f4\u52a0\u65b9\u4fbf\u5730\u627e\u5230\u76f8\u5173\u5185\u5bb9\u5e76\u5efa\u7acb\u7f51\u72b6\u601d\u7ef4 \u672c\u9879\u76ee\u7684\u52a3\u52bf \u548c\u539f\u7248\u672c\u5728\u66f4\u65b0\u901f\u5ea6\u4e0a\u5b58\u5728\u5dee\u5f02\uff0c\u66f4\u65b0\u548c\u52d8\u8bef\u90fd\u662f\u6ede\u540e\u7684\uff08\u5e9f\u8bdd\uff09 \u6682\u672a\u5305\u542b\u6bcf\u8282\u8bfe\u7684\u5c0f\u6d4b\u9a8c\u90e8\u5206 \u6ca1\u6709\u5305\u542b\u539f\u6587\u4e0b\u65b9\u7684\u7f51\u53cb\u8ba8\u8bba\u5185\u5bb9 \u8bd1\u8005\u7684\u5176\u4ed6\u7ffb\u8bd1\u9879\u76ee\uff1a \u9879\u76ee Star \u8ba1\u7b97\u673a\u6559\u80b2\u4e2d\u7f3a\u5931\u7684\u4e00\u8bfe - The Missing Semester awesome-python-cn awesome-go-cn","title":"\u7ffb\u8bd1\u987a\u5e8f\uff08\u6309\u7ae0\u8282\uff09"},{"location":"0-8-A-few-common-C%2B%2B-problems/","tags":["setup"],"text":"","title":"0.8 - \u4e00\u4e9b\u5178\u578b\u7684 C++ \u95ee\u9898"},{"location":"1-1-Statements-and-the-structure-of-a-program/","tags":["statements"],"text":"\u7ae0\u8282\u7b80\u4ecb \u6b22\u8fce\u6765\u5230\u6211\u4eec C++\u6559\u7a0b\u7684\u7b2c\u4e00\u4e2a\u4e3b\u8981\u7ae0\u8282\uff01 \u672c\u7ae0\u8282\u5c06\u9996\u5148\u4ecb\u7ecd\u6709\u5173 C++ \u7a0b\u5e8f\u6700\u57fa\u672c\u7684\u8bdd\u9898\u3002\u7531\u4e8e\u672c\u7ae0\u8282\u6d89\u53ca\u7684\u8bdd\u9898\u8f83\u591a\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u4f1a\u5bf9\u6bcf\u4e00\u4e2a\u90e8\u5206\u90fd\u8fdb\u884c\u76f8\u5bf9\u7b80\u5355\u7684\u4ecb\u7ecd\uff0c\u5176\u76ee\u7684\u53ea\u662f\u4e3a\u4e86\u8ba9\u4f60\u80fd\u591f\u7406\u89e3\u6784\u6210 C++\u7a0b\u5e8f\u7684\u57fa\u672c\u5143\u7d20\u6709\u54ea\u4e9b\u3002\u5728\u5b8c\u6210\u672c\u7ae0\u8282\u7684\u5b66\u4e60\u540e\uff0c\u4f60\u5e94\u5f53\u53ef\u4ee5\u72ec\u7acb\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\u4e86\u3002 \u5728\u540e\u7eed\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u4f1a\u590d\u4e60\u5e76\u6df1\u5165\u63a2\u8ba8\u8fd9\u4e9b\u8bdd\u9898\uff0c\u540c\u65f6\u4ecb\u7ecd\u5c24\u5176\u884d\u751f\u51fa\u7684\u4e00\u4e9b\u65b0\u7684\u6982\u5ff5\u3002 \u4e3a\u4e86\u4f7f\u6bcf\u8282\u8bfe\u90fd\u53ef\u4ee5\u4fdd\u6301\u9002\u5f53\u7684\u4f53\u91cf\uff0c\u6709\u4e9b\u8bdd\u9898\u88ab\u5206\u5272\u6210\u4e86\u591a\u8282\u8bfe\u3002\u5982\u679c\u4f60\u89c9\u5f97\u6709\u4e9b\u91cd\u8981\u7684\u6982\u5ff5\u5728\u4e00\u8282\u8bfe\u4e2d\u5c1a\u672a\u4ecb\u7ecd\uff0c\u90a3\u4e48\u5b83\u5f88\u53ef\u80fd\u4f1a\u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u8bb2\u89e3\u3002 \u8bed\u53e5 \u8ba1\u7b97\u673a\u7a0b\u5e8f\u6307\u7684\u662f\u63a7\u5236\u8ba1\u7b97\u673a\u5b8c\u6210\u67d0\u4ef6\u4e8b\u6240\u9700\u8981\u7684\u4e00\u7cfb\u5217 \u6307\u4ee4(instuction) \u3002 \u8bed\u53e5 \u662f\u6307\u4ee4\u7684\u4e00\u79cd\uff0c\u5b83\u63a7\u5236\u8ba1\u7b97\u673a\u5b8c\u6210\u67d0\u4e9b\u64cd\u4f5c\u3002 \u5c31\u76ee\u524d\u800c\u8a00\uff0c\u8bed\u53e5\u662f C++\u7a0b\u5e8f\u4e2d\u6700\u5e38\u89c1\u7684\u4e00\u79cd\u6307\u4ee4\u7c7b\u578b\u3002\u8fd9\u4e3b\u8981\u662f\u56e0\u4e3a\u5b83\u4eec\u662f C++\u8bed\u8a00\u4e2d\u6700\u7b80\u5355\u7684\u3001\u72ec\u7acb\u7684\u8ba1\u7b97\u5355\u5143\u3002\u4ece\u8fd9\u4e2a\u610f\u4e49\u4e0a\u6765\u8bb2\uff0c\u8bed\u53e5\u4e4b\u4e8e\u7f16\u7a0b\u8bed\u8a00\uff0c\u5c31\u597d\u6bd4\u53e5\u5b50\u4e4b\u4e8e\u81ea\u7136\u8bed\u8a00\u3002\u5f53\u4f60\u9700\u8981\u5411\u5176\u4ed6\u4eba\u4f20\u9012\u4fe1\u606f\u7684\u65f6\u5019\uff0c\u901a\u5e38\u6211\u4eec\u4f1a\u4f7f\u7528\u53e5\u5b50\uff0c\u800c\u4e0d\u662f\u4e00\u4e9b\u968f\u673a\u7684\u8bcd\u8bed\u6216\u97f3\u8282\u3002 \u5728 C++\u4e2d\uff0c\u6211\u4eec\u4e00\u822c\u4e5f\u662f\u901a\u8fc7\u8bed\u53e5\u5411\u7a0b\u5e8f\u4f20\u8fbe\u6211\u4eec\u7684\u610f\u56fe\u7684\u3002 \u5927\u591a\u6570\uff08\u4f46\u4e0d\u662f\u5168\u90e8\uff09\u7684 C++\u8bed\u53e5\u4ee5\u5206\u53f7\u7ed3\u5c3e\u3002\u5982\u679c\u4f60\u770b\u5230\u4e00\u884c\u4ee5\u5206\u53f7\u7ed3\u5c3e\u7684\u4ee3\u7801\uff0c\u90a3\u4e48\u5b83\u591a\u534a\u5c31\u662f\u4e00\u6761\u8bed\u53e5\u4e86\u3002 \u5728 C++\u8fd9\u6837\u7684\u9ad8\u7ea7\u8bed\u8a00\u4e2d\uff0c\u4e00\u6761\u8bed\u53e5\u53ef\u80fd\u4f1a\u88ab\u7f16\u8bd1\u4e3a\u591a\u6761\u673a\u5668\u6307\u4ee4\u3002 \u6269\u5c55\u9605\u8bfb C++\u4e2d\u6709\u5f88\u591a\u4e0d\u540c\u7c7b\u578b\u7684\u8bed\u53e5\uff1a \u58f0\u660e\u8bed\u53e5 Declaration statements \u8df3\u8f6c\u8bed\u53e5 Jump statements \u8868\u8fbe\u5f0f\u8bed\u53e5 Expression statements \u590d\u5408\u8bed\u53e5 Compound statements \u9009\u62e9\u8bed\u53e5\uff08\u6761\u4ef6\uff09Selection statements (conditionals) \u8fed\u4ee3\u8bed\u53e5\uff08\u5faa\u73af\uff09Iteration statements (loops) Try \u8bed\u53e5\u5757 Try blocks \u5728\u7ed3\u675f\u672c\u6559\u7a0b\u7684\u5b66\u4e60\u540e\uff0c\u4f60\u5c06\u638c\u63e1\u4e0a\u8ff0\u5168\u90e8\u7684\u8bed\u53e5\u7c7b\u578b\uff01 \u51fd\u6570\u548c main \u51fd\u6570 \u5728 C++ \u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5408\u5e76\u4f7f\u7528\u4e00\u7ec4\u8bed\u53e5\uff0c\u8fd9\u4e00\u7ec4\u8bed\u53e5\u79f0\u4e3a\u4e00\u4e2a\u51fd\u6570\u3002\u51fd\u6570\u5c31\u662f\u4e00\u7ec4\u987a\u5e8f\u6267\u884c\uff08\u4ece\u4e0a\u5411\u4e0b\u6309\u987a\u5e8f\u6267\u884c\uff09\u7684\u8bed\u53e5\u7684\u96c6\u5408\u3002\u5728\u6211\u4eec\u5b66\u4e60\u7f16\u7a0b\u7a0b\u5e8f\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u4f1a\u7f16\u5199\u81ea\u5df1\u7684\u51fd\u6570\uff0c\u6839\u636e\u6211\u4eec\u7684\u76ee\u7684\u6df7\u5408\u6216\u4f7f\u7528\u8bed\u53e5\uff08\u5177\u4f53\u505a\u6cd5\u5c06\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\uff09\u3002 \u6cd5\u5219 \u6bcf\u4e2a C++\u7a0b\u5e8f\u90fd\u5fc5\u987b\u5305\u542b\u4e00\u4e2a\u51fd\u6570\u540d\u4e3a main \u7684\u51fd\u6570(main \u5168\u90e8\u4e3a\u5c0f\u5199\u5b57\u6bcd)\u3002\u5f53\u7a0b\u5e8f\u5f00\u59cb\u8fd0\u884c\u7684\u65f6\u5019\uff0c main \u51fd\u6570\u4e2d\u7684\u4ee3\u7801\u4f1a\u88ab\u987a\u5e8f\u6267\u884c\u3002 \u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u901a\u5e38\u4f1a\u5728\u5b8c\u6210 main \u51fd\u6570\u4e2d\u6700\u540e\u4e00\u6761\u8bed\u53e5\u540e\u7ec8\u6b62\uff08\u5c3d\u7ba1\u4e5f\u53ef\u80fd\u63d0\u524d\u7ec8\u6b62\u6216\u8005\u5728\u7ed3\u675f\u540e\u6267\u884c\u4e00\u4e9b\u6e05\u7406\u64cd\u4f5c\uff09\u3002 \u7f16\u5199\u7684\u51fd\u6570\u4e00\u822c\u662f\u4e3a\u4e86\u5b8c\u6210\u67d0\u4e2a\u7279\u5b9a\u7684\u5de5\u4f5c\u3002\u4f8b\u5982\uff0c\u540d\u4e3a \"max\" \u7684\u51fd\u6570\u4e00\u822c\u7528\u4e8e\u4e24\u4e2a\u6570\u5b57\u6bd4\u5927\u5c0f\u3002\u800c\u540d\u4e3a \u201ccalculateGrade\u201d \u7684\u51fd\u6570\u5219\u7528\u4e8e\u8ba1\u7b97\u5b66\u751f\u6210\u7ee9\uff08grade\uff09\u3002\u6211\u4eec\u4f1a\u5728\u51fd\u6570\u4e00\u8282\u4e2d\u5bf9\u5176\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u6bd5\u7adf\u51fd\u6570\u662f\u6784\u5efa\u7a0b\u5e8f\u7684\u6700\u5e38\u7528\u7684\u5de5\u5177\u3002 \u4f5c\u8005\u6ce8 \u5f53\u6211\u4eec\u8c08\u8bba\u51fd\u6570\u65f6\uff0c\u901a\u5e38\u4f1a\u5728\u51fd\u6570\u540d\u540e\u9762\u52a0\u4e0a\u4e00\u7ec4\u62ec\u53f7\u4f5c\u4e3a\u4e00\u79cd\u901f\u8bb0\u6cd5\u3002\u4f8b\u5982\uff0c\u5f53\u4f60\u770b\u5230 main() \u6216\u8005 doSomething() \u7684\u65f6\u5019\uff0c\u4f60\u5e94\u8be5\u6e05\u695a\u6b64\u65f6\u6211\u4eec\u5728\u8c08\u8bba\u4e00\u4e2a\u540d\u4e3a main \u6216\u8005 doSomething \u7684\u51fd\u6570\u3002\u8fd9\u4e48\u505a\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5c06\u51fd\u6570\u548c\u5176\u4ed6\u7c7b\u578b\u7684\u540d\u79f0\u533a\u5206\u5f00\u6765\uff08\u4f8b\u5982\u53d8\u91cf\u540d\uff09\uff0c\u540c\u65f6\u4e0d\u5fc5\u6bcf\u6b21\u90fd\u52a0\u4e0a\u201c\u51fd\u6570\u201d\u4e8c\u5b57\u3002 Hello world! \u7a0b\u5e8f\u5256\u6790 \u73b0\u5728\uff0c\u4f60\u5df2\u7ecf\u521d\u6b65\u4e86\u89e3\u4e86\u4ec0\u4e48\u662f\u8bed\u53e5\u4ee5\u53ca\u4ec0\u4e48\u662f\u51fd\u6570\uff0c\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u56de\u5230 \u201cHello world\u201d \u7a0b\u5e8f\u5e76\u9010\u884c\u5bf9\u5176\u8fdb\u884c\u5206\u6790\u3002 1 2 3 4 5 6 7 #include int main () { std :: cout << \"Hello world!\" ; return 0 ; } \u7b2c\u4e00\u884c \u6bd4\u8f83\u7279\u6b8a\uff0c\u79f0\u4e3a \u9884\u5904\u7406\u5668\u6307\u4ee4 (preprocessor directive) \u3002\u8be5\u6307\u4ee4\u8868\u660e\u7528\u6237\u9700\u8981\u4f7f\u7528 iostream \u5e93\u7684\u5185\u5bb9\uff0c\u8be5\u5e93\u5c5e\u4e8e C++\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u4f7f\u7528\u8be5\u5e93\u63d0\u4f9b\u7684\u529f\u80fd\u4ece\u63a7\u5236\u53f0(console)\u8bfb\u5199\u6587\u672c\u3002\u6211\u4eec\u9700\u8981\u8fd9\u4e00\u884c\u4ee3\u7801\uff0c\u662f\u4e3a\u4e86\u5728\u7b2c\u4e94\u884c\u4f7f\u7528 std:: cout \u3002\u5982\u679c\u5c06\u8fd9\u4e00\u884c\u5220\u9664\uff0c\u5219\u4f1a\u5bfc\u81f4\u7b2c\u4e94\u884c\u7f16\u8bd1\u65f6\u62a5\u9519\uff0c\u56e0\u4e3a\u8fd9\u6837 \u7f16\u8bd1\u5668(compiler) \u5c31\u4e0d\u77e5\u9053 std:: cout \u662f\u4ec0\u4e48\u4e86\u3002 \u7b2c\u4e8c\u884c \u662f\u7a7a\u884c\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c06\u5176\u5ffd\u7565\u3002\u5b83\u7684\u5b58\u5728\u4ec5\u4ec5\u662f\u4e3a\u4e86\u8ba9\u7a0b\u5e8f\u53ef\u8bfb\u6027\u53d8\u5f97\u66f4\u597d\uff08\u5c06 #include \u9884\u5904\u7406\u5668\u6307\u4ee4\u548c\u7a0b\u5e8f\u7684\u5176\u4ed6\u90e8\u5206\u5206\u5272\u5f00\u6765\uff09\u3002 \u7b2c\u4e09\u884c \u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u6b64\u5904\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a main \u7684\u51fd\u6570\u3002\u4e4b\u524d\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u5230\uff0c\u6bcf\u4e2a C++\u7a0b\u5e8f\u90fd\u5fc5\u987b\u6709\u4e00\u4e2a main \u51fd\u6570\uff0c\u5426\u5219\u5c06\u4f1a \u94fe\u63a5(link) \u5931\u8d25\u3002 \u7b2c\u56db\u884c \u548c \u7b2c\u4e03\u884c \u544a\u8bc9\u7f16\u8bd1\u5668\u63a5\u4e0b\u6765\u54ea\u4e9b\u884c\u4ee3\u7801\u5c5e\u4e8e main \u51fd\u6570\u3002\u6240\u6709\u4f4d\u4e8e\u7b2c\u56db\u884c\u548c\u7b2c\u4e03\u884c\u62ec\u53f7\u5185\u7684\u4ee3\u7801\uff0c\u90fd\u5c5e\u4e8e main \u51fd\u6570\u7684\u4e00\u90e8\u5206\uff0c\u79f0\u4e3a\u51fd\u6570\u4f53\u3002 \u7b2c\u4e94\u884c \u662f main \u51fd\u6570\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\uff0c\u8be5\u8bed\u53e5\u4e5f\u662f\u7a0b\u5e8f\u6267\u884c\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u3002 std:: cout \uff08\u8868\u793a\u201ccharacter output\u201d\uff0c\u5373\u5b57\u7b26\u8f93\u51fa\uff09\u548c << \u8fd0\u7b97\u7b26\u53ef\u4ee5\u5c06\u5b57\u6bcd\u6216\u6570\u5b57\u4ece\u63a7\u5236\u53f0\u8f93\u51fa\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u5b57\u7b26\u4e32\u6587\u672c \u201cHello world!\u201d \u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002\u8fd9\u884c\u4ee3\u7801\u7684\u6267\u884c\u6548\u679c\u5c31\u662f\u4e3a\u7a0b\u5e8f\u521b\u5efa\u4e00\u4e2a\u53ef\u89c6\u5316\u7684\u8f93\u51fa\u7ed3\u679c\u3002 \u7b2c\u516d\u884c \u662f\u4e00\u4e2a\u8fd4\u56de\u8bed\u53e5\u3002\u5f53\u7a0b\u5e8f\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u5b83\u9700\u8981\u5411\u64cd\u4f5c\u7cfb\u7edf\u8fd4\u56de\u4e00\u4e2a\u503c\u6765\u8868\u660e\u5176\u6267\u884c\u7684\u7ed3\u679c\u662f\u6210\u529f\u8fd8\u662f\u5931\u8d25\u3002\u8be5\u8fd4\u56de\u8bed\u53e5\u5411\u64cd\u4f5c\u7cfb\u7edf\u8fd4\u56de\u4e86 0\uff0c\u8868\u793a\"\u4e00\u5207\u6b63\u5e38\"\u3002\u8fd9\u662f\u672c\u7a0b\u5e8f\u6267\u884c\u7684\u6700\u540e\u4e00\u6761\u8bed\u53e5\u3002 \u6211\u4eec\u7f16\u5199\u7684\u6240\u6709\u7a0b\u5e8f\uff0c\u90fd\u4f1a\u9075\u5faa\u4e0a\u8ff0\u901a\u7528\u6a21\u677f\uff08\u6216\u5176\u53d8\u79cd\u7248\u672c\uff09\u3002 \u4f5c\u8005\u6ce8 \u5982\u679c\u4e0a\u8ff0\u89e3\u91ca\u4ecd\u7136\u6709\u8ba9\u4f60\u56f0\u60d1\u7684\u5730\u65b9\uff0c\u522b\u62c5\u5fc3\uff0c\u8fd9\u5f88\u6b63\u5e38\u3002\u8fd9\u91cc\u7684\u76ee\u7684\u53ea\u662f\u8ba9\u4f60\u5bf9\u7a0b\u5e8f\u6709\u4e00\u4e2a\u5b8f\u89c2\u7684\u6982\u5ff5\uff0c\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u5c06\u4f1a\u5bf9\u76f8\u5173\u7684\u5185\u5bb9\u8fdb\u884c\u8be6\u7ec6\u89e3\u91ca\u5e76\u8f85\u4ee5\u6848\u4f8b\u3002 \u4f60\u53ef\u4ee5\u81ea\u884c\u7f16\u8bd1\u5e76\u8fd0\u884c\u4e0a\u8ff0\u7a0b\u5e8f\uff0c\u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u4f60\u5c06\u5728\u63a7\u5236\u53f0\u4e2d\u770b\u5230\u5982\u4e0b\u6253\u5370\u4fe1\u606f\uff1a 1 Hello world! \u5982\u679c\u4f60\u5728\u7f16\u8bd1\u6216\u8fd0\u884c\u7a0b\u5e8f\u65f6\u9047\u5230\u4e86\u95ee\u9898\uff0c\u8bf7\u53c2\u8003 \u4e00\u4e9b\u5178\u578b\u7684 C++ \u95ee\u9898 \u8bed\u6cd5\u548c\u8bed\u6cd5\u9519\u8bef \u82f1\u8bed\u4e2d\u7684\u53e5\u5b50\u57fa\u4e8e\u4f60\u6240\u5b66\u7684\u8bed\u6cd5\u89c4\u5219(grammatical rules)\u6765\u6784\u5efa\u7684\u3002\u4f8b\u5982\uff0c\u901a\u5e38\u4e00\u4e2a\u53e5\u5b50\u4f1a\u4ee5\u53e5\u53f7\u7ed3\u5c3e\u3002\u5728\u4e00\u95e8\u8bed\u8a00\u4e2d\uff0c\u89c4\u5b9a\u4e00\u4e2a\u53e5\u5b50\u5e94\u8be5\u5982\u4f55\u6784\u5efa\u7684\u89c4\u5219\uff0c\u79f0\u4e3a \u8bed\u6cd5 \uff08\u8bd1\u6ce8\uff1a\u53e5\u6cd5\u5728\u540e\u7eed\u7684\u7ffb\u8bd1\u4e2d\u5c06\u4f7f\u7528\u8bed\u6cd5\u4ee3\u66ff\uff0c\u89c1 \u7ffb\u8bd1\u7ea6\u5b9a \uff09\u3002\u5982\u679c\u4f60\u5fd8\u8bb0\u4e86\u5728\u4e24\u4e2a\u53e5\u5b50\u4e4b\u95f4\u5199\u4e0b\u53e5\u53f7\uff0c\u90a3\u4e48\u5c31\u4f1a\u8fdd\u53cd\u4e86\u82f1\u8bed\u7684\u53e5\u6cd5\u3002 C++ \u4e5f\u6709\u5b83\u7684 \u8bed\u6cd5 \u89c4\u5219\uff0c\u5b83\u89c4\u5b9a\u4e86\u4e00\u4e2a\u7a0b\u5e8f\u5e94\u8be5\u5982\u4f55\u88ab\u7f16\u5199\u624d\u80fd\u88ab\u8ba4\u4e3a\u662f\u5408\u6cd5\u7684\u3002\u5728\u7f16\u8bd1\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u7f16\u8bd1\u5668\u9700\u8981\u786e\u4fdd\u4f60\u7684\u7a0b\u5e8f\u6ee1\u8db3\u57fa\u672c\u7684 C++\u8bed\u6cd5\u3002\u5982\u679c\u4f60\u8fdd\u53cd\u4e86\u67d0\u6761\u89c4\u5219\uff0c\u7f16\u8bd1\u5668\u5c31\u4f1a\u5411\u4f60\u62a5\u544a\u4e00\u4e2a\u9519\u8bef\u3002 \u5982\u679c\uff0c\u6211\u4eec\u5c06\u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\u7b2c\u4e94\u884c\u7684\u5206\u53f7\u7701\u7565\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f 1 2 3 4 5 6 7 #include int main () { std :: cout << \"Hello world!\" return 0 ; } \u4f60\u53ef\u4ee5\u8bd5\u8bd5\u53bb\u7f16\u8bd1\u4e00\u4e0b\u8be5\u7a0b\u5e8f\u3002 Visual Studio \u5728\u7f16\u8bd1\u65f6\u4f1a\u62a5\u544a\u5982\u4e0b\u9519\u8bef (\u4f60\u7684\u7f16\u8bd1\u5668\u5728\u8868\u8ff0\u8be5\u9519\u8bef\u65f6\u53ef\u80fd\u4f1a\u4f7f\u7528\u4e0d\u540c\u7684\u8bed\u8a00)\uff1a 1 c:\\vcprojects\\test1.cpp(6): error C2143: syntax error : missing ';' before 'return' \u8be5\u9519\u8bef\u4fe1\u606f\u662f\u544a\u8bc9\u4f60\uff0c\u9519\u8bef\u53d1\u751f\u5728\u7b2c\u516d\u884c\uff1a\u7a0b\u5e8f\u671f\u671b\u5728 return \u8bed\u53e5\u524d\u9762\u68c0\u6d4b\u5230\u4e00\u4e2a\u5206\u53f7\uff0c\u4f46\u5b83\u5374\u6ca1\u6709\u3002\u5c3d\u7ba1\u7f16\u8bd1\u5668\u544a\u8bc9\u4e86\u6211\u4eec\u8be5\u8bed\u6cd5\u95ee\u9898\u53d1\u751f\u7684\u884c\u53f7\uff0c\u5176\u5b9e\u9645\u53d1\u751f\u7684\u4f4d\u7f6e\u5374\u5728\u4e0a\u4e00\u884c\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u95ee\u9898\u51fa\u5728 \u7b2c\u4e94\u884c \u7684\u672b\u5c3e\uff08\u7f16\u8bd1\u5668\u5728\u8bfb\u53d6\u5230\u7b2c\u516d\u884c\u65f6\u624d\u80fd\u53d1\u73b0\u8be5\u95ee\u9898\uff09\u3002 \u8bed\u6cd5\u9519\u8bef\u5728\u7f16\u7a0b\u65f6\u5f88\u5e38\u89c1\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u8fd9\u79cd\u7c7b\u578b\u7684\u9519\u8bef\u901a\u5e38\u6bd4\u8f83\u5bb9\u6613\u5b9a\u4f4d\u548c\u4fee\u590d\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u4f1a\u660e\u786e\u5730\u6307\u5411\u9519\u8bef\u53d1\u751f\u7684\u4f4d\u7f6e\u3002\u7a0b\u5e8f\u7684\u7f16\u8bd1\u53ea\u6709\u5728\u6240\u6709\u7684\u9519\u8bef\u90fd\u88ab\u4fee\u590d\u540e\u624d\u80fd\u591f\u5b8c\u6210\u3002 \u4f60\u53ef\u4ee5\u8bd5\u7740\u4ece\u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\u5220\u9664\u67d0\u4e9b\u5b57\u7b26\uff0c\u751a\u81f3\u5220\u9664\u6574\u884c\uff0c\u7136\u540e\u89c2\u5bdf\u7f16\u8bd1\u5668\u7684\u62a5\u9519\u4fe1\u606f\u3002\u73b0\u5728\uff0c\u5c06\u5206\u53f7\u5199\u5728\u7b2c\u4e94\u884c\u7684\u672b\u5c3e\u5e76\u5220\u9664 1\u30013 \u6216\u8005 4 \u884c\uff0c\u770b\u770b\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f","title":"1.1 - \u7a0b\u5e8f\u7ed3\u6784\u548c\u8bed\u53e5"},{"location":"1-1-Statements-and-the-structure-of-a-program/#_1","text":"\u6b22\u8fce\u6765\u5230\u6211\u4eec C++\u6559\u7a0b\u7684\u7b2c\u4e00\u4e2a\u4e3b\u8981\u7ae0\u8282\uff01 \u672c\u7ae0\u8282\u5c06\u9996\u5148\u4ecb\u7ecd\u6709\u5173 C++ \u7a0b\u5e8f\u6700\u57fa\u672c\u7684\u8bdd\u9898\u3002\u7531\u4e8e\u672c\u7ae0\u8282\u6d89\u53ca\u7684\u8bdd\u9898\u8f83\u591a\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u4f1a\u5bf9\u6bcf\u4e00\u4e2a\u90e8\u5206\u90fd\u8fdb\u884c\u76f8\u5bf9\u7b80\u5355\u7684\u4ecb\u7ecd\uff0c\u5176\u76ee\u7684\u53ea\u662f\u4e3a\u4e86\u8ba9\u4f60\u80fd\u591f\u7406\u89e3\u6784\u6210 C++\u7a0b\u5e8f\u7684\u57fa\u672c\u5143\u7d20\u6709\u54ea\u4e9b\u3002\u5728\u5b8c\u6210\u672c\u7ae0\u8282\u7684\u5b66\u4e60\u540e\uff0c\u4f60\u5e94\u5f53\u53ef\u4ee5\u72ec\u7acb\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\u4e86\u3002 \u5728\u540e\u7eed\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u4f1a\u590d\u4e60\u5e76\u6df1\u5165\u63a2\u8ba8\u8fd9\u4e9b\u8bdd\u9898\uff0c\u540c\u65f6\u4ecb\u7ecd\u5c24\u5176\u884d\u751f\u51fa\u7684\u4e00\u4e9b\u65b0\u7684\u6982\u5ff5\u3002 \u4e3a\u4e86\u4f7f\u6bcf\u8282\u8bfe\u90fd\u53ef\u4ee5\u4fdd\u6301\u9002\u5f53\u7684\u4f53\u91cf\uff0c\u6709\u4e9b\u8bdd\u9898\u88ab\u5206\u5272\u6210\u4e86\u591a\u8282\u8bfe\u3002\u5982\u679c\u4f60\u89c9\u5f97\u6709\u4e9b\u91cd\u8981\u7684\u6982\u5ff5\u5728\u4e00\u8282\u8bfe\u4e2d\u5c1a\u672a\u4ecb\u7ecd\uff0c\u90a3\u4e48\u5b83\u5f88\u53ef\u80fd\u4f1a\u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u8bb2\u89e3\u3002","title":"\u7ae0\u8282\u7b80\u4ecb"},{"location":"1-1-Statements-and-the-structure-of-a-program/#_2","text":"\u8ba1\u7b97\u673a\u7a0b\u5e8f\u6307\u7684\u662f\u63a7\u5236\u8ba1\u7b97\u673a\u5b8c\u6210\u67d0\u4ef6\u4e8b\u6240\u9700\u8981\u7684\u4e00\u7cfb\u5217 \u6307\u4ee4(instuction) \u3002 \u8bed\u53e5 \u662f\u6307\u4ee4\u7684\u4e00\u79cd\uff0c\u5b83\u63a7\u5236\u8ba1\u7b97\u673a\u5b8c\u6210\u67d0\u4e9b\u64cd\u4f5c\u3002 \u5c31\u76ee\u524d\u800c\u8a00\uff0c\u8bed\u53e5\u662f C++\u7a0b\u5e8f\u4e2d\u6700\u5e38\u89c1\u7684\u4e00\u79cd\u6307\u4ee4\u7c7b\u578b\u3002\u8fd9\u4e3b\u8981\u662f\u56e0\u4e3a\u5b83\u4eec\u662f C++\u8bed\u8a00\u4e2d\u6700\u7b80\u5355\u7684\u3001\u72ec\u7acb\u7684\u8ba1\u7b97\u5355\u5143\u3002\u4ece\u8fd9\u4e2a\u610f\u4e49\u4e0a\u6765\u8bb2\uff0c\u8bed\u53e5\u4e4b\u4e8e\u7f16\u7a0b\u8bed\u8a00\uff0c\u5c31\u597d\u6bd4\u53e5\u5b50\u4e4b\u4e8e\u81ea\u7136\u8bed\u8a00\u3002\u5f53\u4f60\u9700\u8981\u5411\u5176\u4ed6\u4eba\u4f20\u9012\u4fe1\u606f\u7684\u65f6\u5019\uff0c\u901a\u5e38\u6211\u4eec\u4f1a\u4f7f\u7528\u53e5\u5b50\uff0c\u800c\u4e0d\u662f\u4e00\u4e9b\u968f\u673a\u7684\u8bcd\u8bed\u6216\u97f3\u8282\u3002 \u5728 C++\u4e2d\uff0c\u6211\u4eec\u4e00\u822c\u4e5f\u662f\u901a\u8fc7\u8bed\u53e5\u5411\u7a0b\u5e8f\u4f20\u8fbe\u6211\u4eec\u7684\u610f\u56fe\u7684\u3002 \u5927\u591a\u6570\uff08\u4f46\u4e0d\u662f\u5168\u90e8\uff09\u7684 C++\u8bed\u53e5\u4ee5\u5206\u53f7\u7ed3\u5c3e\u3002\u5982\u679c\u4f60\u770b\u5230\u4e00\u884c\u4ee5\u5206\u53f7\u7ed3\u5c3e\u7684\u4ee3\u7801\uff0c\u90a3\u4e48\u5b83\u591a\u534a\u5c31\u662f\u4e00\u6761\u8bed\u53e5\u4e86\u3002 \u5728 C++\u8fd9\u6837\u7684\u9ad8\u7ea7\u8bed\u8a00\u4e2d\uff0c\u4e00\u6761\u8bed\u53e5\u53ef\u80fd\u4f1a\u88ab\u7f16\u8bd1\u4e3a\u591a\u6761\u673a\u5668\u6307\u4ee4\u3002 \u6269\u5c55\u9605\u8bfb C++\u4e2d\u6709\u5f88\u591a\u4e0d\u540c\u7c7b\u578b\u7684\u8bed\u53e5\uff1a \u58f0\u660e\u8bed\u53e5 Declaration statements \u8df3\u8f6c\u8bed\u53e5 Jump statements \u8868\u8fbe\u5f0f\u8bed\u53e5 Expression statements \u590d\u5408\u8bed\u53e5 Compound statements \u9009\u62e9\u8bed\u53e5\uff08\u6761\u4ef6\uff09Selection statements (conditionals) \u8fed\u4ee3\u8bed\u53e5\uff08\u5faa\u73af\uff09Iteration statements (loops) Try \u8bed\u53e5\u5757 Try blocks \u5728\u7ed3\u675f\u672c\u6559\u7a0b\u7684\u5b66\u4e60\u540e\uff0c\u4f60\u5c06\u638c\u63e1\u4e0a\u8ff0\u5168\u90e8\u7684\u8bed\u53e5\u7c7b\u578b\uff01","title":"\u8bed\u53e5"},{"location":"1-1-Statements-and-the-structure-of-a-program/#main","text":"\u5728 C++ \u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5408\u5e76\u4f7f\u7528\u4e00\u7ec4\u8bed\u53e5\uff0c\u8fd9\u4e00\u7ec4\u8bed\u53e5\u79f0\u4e3a\u4e00\u4e2a\u51fd\u6570\u3002\u51fd\u6570\u5c31\u662f\u4e00\u7ec4\u987a\u5e8f\u6267\u884c\uff08\u4ece\u4e0a\u5411\u4e0b\u6309\u987a\u5e8f\u6267\u884c\uff09\u7684\u8bed\u53e5\u7684\u96c6\u5408\u3002\u5728\u6211\u4eec\u5b66\u4e60\u7f16\u7a0b\u7a0b\u5e8f\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u4f1a\u7f16\u5199\u81ea\u5df1\u7684\u51fd\u6570\uff0c\u6839\u636e\u6211\u4eec\u7684\u76ee\u7684\u6df7\u5408\u6216\u4f7f\u7528\u8bed\u53e5\uff08\u5177\u4f53\u505a\u6cd5\u5c06\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\uff09\u3002 \u6cd5\u5219 \u6bcf\u4e2a C++\u7a0b\u5e8f\u90fd\u5fc5\u987b\u5305\u542b\u4e00\u4e2a\u51fd\u6570\u540d\u4e3a main \u7684\u51fd\u6570(main \u5168\u90e8\u4e3a\u5c0f\u5199\u5b57\u6bcd)\u3002\u5f53\u7a0b\u5e8f\u5f00\u59cb\u8fd0\u884c\u7684\u65f6\u5019\uff0c main \u51fd\u6570\u4e2d\u7684\u4ee3\u7801\u4f1a\u88ab\u987a\u5e8f\u6267\u884c\u3002 \u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u901a\u5e38\u4f1a\u5728\u5b8c\u6210 main \u51fd\u6570\u4e2d\u6700\u540e\u4e00\u6761\u8bed\u53e5\u540e\u7ec8\u6b62\uff08\u5c3d\u7ba1\u4e5f\u53ef\u80fd\u63d0\u524d\u7ec8\u6b62\u6216\u8005\u5728\u7ed3\u675f\u540e\u6267\u884c\u4e00\u4e9b\u6e05\u7406\u64cd\u4f5c\uff09\u3002 \u7f16\u5199\u7684\u51fd\u6570\u4e00\u822c\u662f\u4e3a\u4e86\u5b8c\u6210\u67d0\u4e2a\u7279\u5b9a\u7684\u5de5\u4f5c\u3002\u4f8b\u5982\uff0c\u540d\u4e3a \"max\" \u7684\u51fd\u6570\u4e00\u822c\u7528\u4e8e\u4e24\u4e2a\u6570\u5b57\u6bd4\u5927\u5c0f\u3002\u800c\u540d\u4e3a \u201ccalculateGrade\u201d \u7684\u51fd\u6570\u5219\u7528\u4e8e\u8ba1\u7b97\u5b66\u751f\u6210\u7ee9\uff08grade\uff09\u3002\u6211\u4eec\u4f1a\u5728\u51fd\u6570\u4e00\u8282\u4e2d\u5bf9\u5176\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u6bd5\u7adf\u51fd\u6570\u662f\u6784\u5efa\u7a0b\u5e8f\u7684\u6700\u5e38\u7528\u7684\u5de5\u5177\u3002 \u4f5c\u8005\u6ce8 \u5f53\u6211\u4eec\u8c08\u8bba\u51fd\u6570\u65f6\uff0c\u901a\u5e38\u4f1a\u5728\u51fd\u6570\u540d\u540e\u9762\u52a0\u4e0a\u4e00\u7ec4\u62ec\u53f7\u4f5c\u4e3a\u4e00\u79cd\u901f\u8bb0\u6cd5\u3002\u4f8b\u5982\uff0c\u5f53\u4f60\u770b\u5230 main() \u6216\u8005 doSomething() \u7684\u65f6\u5019\uff0c\u4f60\u5e94\u8be5\u6e05\u695a\u6b64\u65f6\u6211\u4eec\u5728\u8c08\u8bba\u4e00\u4e2a\u540d\u4e3a main \u6216\u8005 doSomething \u7684\u51fd\u6570\u3002\u8fd9\u4e48\u505a\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5c06\u51fd\u6570\u548c\u5176\u4ed6\u7c7b\u578b\u7684\u540d\u79f0\u533a\u5206\u5f00\u6765\uff08\u4f8b\u5982\u53d8\u91cf\u540d\uff09\uff0c\u540c\u65f6\u4e0d\u5fc5\u6bcf\u6b21\u90fd\u52a0\u4e0a\u201c\u51fd\u6570\u201d\u4e8c\u5b57\u3002","title":"\u51fd\u6570\u548c main \u51fd\u6570"},{"location":"1-1-Statements-and-the-structure-of-a-program/#hello-world","text":"\u73b0\u5728\uff0c\u4f60\u5df2\u7ecf\u521d\u6b65\u4e86\u89e3\u4e86\u4ec0\u4e48\u662f\u8bed\u53e5\u4ee5\u53ca\u4ec0\u4e48\u662f\u51fd\u6570\uff0c\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u56de\u5230 \u201cHello world\u201d \u7a0b\u5e8f\u5e76\u9010\u884c\u5bf9\u5176\u8fdb\u884c\u5206\u6790\u3002 1 2 3 4 5 6 7 #include int main () { std :: cout << \"Hello world!\" ; return 0 ; } \u7b2c\u4e00\u884c \u6bd4\u8f83\u7279\u6b8a\uff0c\u79f0\u4e3a \u9884\u5904\u7406\u5668\u6307\u4ee4 (preprocessor directive) \u3002\u8be5\u6307\u4ee4\u8868\u660e\u7528\u6237\u9700\u8981\u4f7f\u7528 iostream \u5e93\u7684\u5185\u5bb9\uff0c\u8be5\u5e93\u5c5e\u4e8e C++\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u4f7f\u7528\u8be5\u5e93\u63d0\u4f9b\u7684\u529f\u80fd\u4ece\u63a7\u5236\u53f0(console)\u8bfb\u5199\u6587\u672c\u3002\u6211\u4eec\u9700\u8981\u8fd9\u4e00\u884c\u4ee3\u7801\uff0c\u662f\u4e3a\u4e86\u5728\u7b2c\u4e94\u884c\u4f7f\u7528 std:: cout \u3002\u5982\u679c\u5c06\u8fd9\u4e00\u884c\u5220\u9664\uff0c\u5219\u4f1a\u5bfc\u81f4\u7b2c\u4e94\u884c\u7f16\u8bd1\u65f6\u62a5\u9519\uff0c\u56e0\u4e3a\u8fd9\u6837 \u7f16\u8bd1\u5668(compiler) \u5c31\u4e0d\u77e5\u9053 std:: cout \u662f\u4ec0\u4e48\u4e86\u3002 \u7b2c\u4e8c\u884c \u662f\u7a7a\u884c\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c06\u5176\u5ffd\u7565\u3002\u5b83\u7684\u5b58\u5728\u4ec5\u4ec5\u662f\u4e3a\u4e86\u8ba9\u7a0b\u5e8f\u53ef\u8bfb\u6027\u53d8\u5f97\u66f4\u597d\uff08\u5c06 #include \u9884\u5904\u7406\u5668\u6307\u4ee4\u548c\u7a0b\u5e8f\u7684\u5176\u4ed6\u90e8\u5206\u5206\u5272\u5f00\u6765\uff09\u3002 \u7b2c\u4e09\u884c \u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u6b64\u5904\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a main \u7684\u51fd\u6570\u3002\u4e4b\u524d\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u5230\uff0c\u6bcf\u4e2a C++\u7a0b\u5e8f\u90fd\u5fc5\u987b\u6709\u4e00\u4e2a main \u51fd\u6570\uff0c\u5426\u5219\u5c06\u4f1a \u94fe\u63a5(link) \u5931\u8d25\u3002 \u7b2c\u56db\u884c \u548c \u7b2c\u4e03\u884c \u544a\u8bc9\u7f16\u8bd1\u5668\u63a5\u4e0b\u6765\u54ea\u4e9b\u884c\u4ee3\u7801\u5c5e\u4e8e main \u51fd\u6570\u3002\u6240\u6709\u4f4d\u4e8e\u7b2c\u56db\u884c\u548c\u7b2c\u4e03\u884c\u62ec\u53f7\u5185\u7684\u4ee3\u7801\uff0c\u90fd\u5c5e\u4e8e main \u51fd\u6570\u7684\u4e00\u90e8\u5206\uff0c\u79f0\u4e3a\u51fd\u6570\u4f53\u3002 \u7b2c\u4e94\u884c \u662f main \u51fd\u6570\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\uff0c\u8be5\u8bed\u53e5\u4e5f\u662f\u7a0b\u5e8f\u6267\u884c\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u3002 std:: cout \uff08\u8868\u793a\u201ccharacter output\u201d\uff0c\u5373\u5b57\u7b26\u8f93\u51fa\uff09\u548c << \u8fd0\u7b97\u7b26\u53ef\u4ee5\u5c06\u5b57\u6bcd\u6216\u6570\u5b57\u4ece\u63a7\u5236\u53f0\u8f93\u51fa\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u5b57\u7b26\u4e32\u6587\u672c \u201cHello world!\u201d \u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002\u8fd9\u884c\u4ee3\u7801\u7684\u6267\u884c\u6548\u679c\u5c31\u662f\u4e3a\u7a0b\u5e8f\u521b\u5efa\u4e00\u4e2a\u53ef\u89c6\u5316\u7684\u8f93\u51fa\u7ed3\u679c\u3002 \u7b2c\u516d\u884c \u662f\u4e00\u4e2a\u8fd4\u56de\u8bed\u53e5\u3002\u5f53\u7a0b\u5e8f\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u5b83\u9700\u8981\u5411\u64cd\u4f5c\u7cfb\u7edf\u8fd4\u56de\u4e00\u4e2a\u503c\u6765\u8868\u660e\u5176\u6267\u884c\u7684\u7ed3\u679c\u662f\u6210\u529f\u8fd8\u662f\u5931\u8d25\u3002\u8be5\u8fd4\u56de\u8bed\u53e5\u5411\u64cd\u4f5c\u7cfb\u7edf\u8fd4\u56de\u4e86 0\uff0c\u8868\u793a\"\u4e00\u5207\u6b63\u5e38\"\u3002\u8fd9\u662f\u672c\u7a0b\u5e8f\u6267\u884c\u7684\u6700\u540e\u4e00\u6761\u8bed\u53e5\u3002 \u6211\u4eec\u7f16\u5199\u7684\u6240\u6709\u7a0b\u5e8f\uff0c\u90fd\u4f1a\u9075\u5faa\u4e0a\u8ff0\u901a\u7528\u6a21\u677f\uff08\u6216\u5176\u53d8\u79cd\u7248\u672c\uff09\u3002 \u4f5c\u8005\u6ce8 \u5982\u679c\u4e0a\u8ff0\u89e3\u91ca\u4ecd\u7136\u6709\u8ba9\u4f60\u56f0\u60d1\u7684\u5730\u65b9\uff0c\u522b\u62c5\u5fc3\uff0c\u8fd9\u5f88\u6b63\u5e38\u3002\u8fd9\u91cc\u7684\u76ee\u7684\u53ea\u662f\u8ba9\u4f60\u5bf9\u7a0b\u5e8f\u6709\u4e00\u4e2a\u5b8f\u89c2\u7684\u6982\u5ff5\uff0c\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u5c06\u4f1a\u5bf9\u76f8\u5173\u7684\u5185\u5bb9\u8fdb\u884c\u8be6\u7ec6\u89e3\u91ca\u5e76\u8f85\u4ee5\u6848\u4f8b\u3002 \u4f60\u53ef\u4ee5\u81ea\u884c\u7f16\u8bd1\u5e76\u8fd0\u884c\u4e0a\u8ff0\u7a0b\u5e8f\uff0c\u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u4f60\u5c06\u5728\u63a7\u5236\u53f0\u4e2d\u770b\u5230\u5982\u4e0b\u6253\u5370\u4fe1\u606f\uff1a 1 Hello world! \u5982\u679c\u4f60\u5728\u7f16\u8bd1\u6216\u8fd0\u884c\u7a0b\u5e8f\u65f6\u9047\u5230\u4e86\u95ee\u9898\uff0c\u8bf7\u53c2\u8003 \u4e00\u4e9b\u5178\u578b\u7684 C++ \u95ee\u9898","title":"Hello world! \u7a0b\u5e8f\u5256\u6790"},{"location":"1-1-Statements-and-the-structure-of-a-program/#_3","text":"\u82f1\u8bed\u4e2d\u7684\u53e5\u5b50\u57fa\u4e8e\u4f60\u6240\u5b66\u7684\u8bed\u6cd5\u89c4\u5219(grammatical rules)\u6765\u6784\u5efa\u7684\u3002\u4f8b\u5982\uff0c\u901a\u5e38\u4e00\u4e2a\u53e5\u5b50\u4f1a\u4ee5\u53e5\u53f7\u7ed3\u5c3e\u3002\u5728\u4e00\u95e8\u8bed\u8a00\u4e2d\uff0c\u89c4\u5b9a\u4e00\u4e2a\u53e5\u5b50\u5e94\u8be5\u5982\u4f55\u6784\u5efa\u7684\u89c4\u5219\uff0c\u79f0\u4e3a \u8bed\u6cd5 \uff08\u8bd1\u6ce8\uff1a\u53e5\u6cd5\u5728\u540e\u7eed\u7684\u7ffb\u8bd1\u4e2d\u5c06\u4f7f\u7528\u8bed\u6cd5\u4ee3\u66ff\uff0c\u89c1 \u7ffb\u8bd1\u7ea6\u5b9a \uff09\u3002\u5982\u679c\u4f60\u5fd8\u8bb0\u4e86\u5728\u4e24\u4e2a\u53e5\u5b50\u4e4b\u95f4\u5199\u4e0b\u53e5\u53f7\uff0c\u90a3\u4e48\u5c31\u4f1a\u8fdd\u53cd\u4e86\u82f1\u8bed\u7684\u53e5\u6cd5\u3002 C++ \u4e5f\u6709\u5b83\u7684 \u8bed\u6cd5 \u89c4\u5219\uff0c\u5b83\u89c4\u5b9a\u4e86\u4e00\u4e2a\u7a0b\u5e8f\u5e94\u8be5\u5982\u4f55\u88ab\u7f16\u5199\u624d\u80fd\u88ab\u8ba4\u4e3a\u662f\u5408\u6cd5\u7684\u3002\u5728\u7f16\u8bd1\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u7f16\u8bd1\u5668\u9700\u8981\u786e\u4fdd\u4f60\u7684\u7a0b\u5e8f\u6ee1\u8db3\u57fa\u672c\u7684 C++\u8bed\u6cd5\u3002\u5982\u679c\u4f60\u8fdd\u53cd\u4e86\u67d0\u6761\u89c4\u5219\uff0c\u7f16\u8bd1\u5668\u5c31\u4f1a\u5411\u4f60\u62a5\u544a\u4e00\u4e2a\u9519\u8bef\u3002 \u5982\u679c\uff0c\u6211\u4eec\u5c06\u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\u7b2c\u4e94\u884c\u7684\u5206\u53f7\u7701\u7565\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f 1 2 3 4 5 6 7 #include int main () { std :: cout << \"Hello world!\" return 0 ; } \u4f60\u53ef\u4ee5\u8bd5\u8bd5\u53bb\u7f16\u8bd1\u4e00\u4e0b\u8be5\u7a0b\u5e8f\u3002 Visual Studio \u5728\u7f16\u8bd1\u65f6\u4f1a\u62a5\u544a\u5982\u4e0b\u9519\u8bef (\u4f60\u7684\u7f16\u8bd1\u5668\u5728\u8868\u8ff0\u8be5\u9519\u8bef\u65f6\u53ef\u80fd\u4f1a\u4f7f\u7528\u4e0d\u540c\u7684\u8bed\u8a00)\uff1a 1 c:\\vcprojects\\test1.cpp(6): error C2143: syntax error : missing ';' before 'return' \u8be5\u9519\u8bef\u4fe1\u606f\u662f\u544a\u8bc9\u4f60\uff0c\u9519\u8bef\u53d1\u751f\u5728\u7b2c\u516d\u884c\uff1a\u7a0b\u5e8f\u671f\u671b\u5728 return \u8bed\u53e5\u524d\u9762\u68c0\u6d4b\u5230\u4e00\u4e2a\u5206\u53f7\uff0c\u4f46\u5b83\u5374\u6ca1\u6709\u3002\u5c3d\u7ba1\u7f16\u8bd1\u5668\u544a\u8bc9\u4e86\u6211\u4eec\u8be5\u8bed\u6cd5\u95ee\u9898\u53d1\u751f\u7684\u884c\u53f7\uff0c\u5176\u5b9e\u9645\u53d1\u751f\u7684\u4f4d\u7f6e\u5374\u5728\u4e0a\u4e00\u884c\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u95ee\u9898\u51fa\u5728 \u7b2c\u4e94\u884c \u7684\u672b\u5c3e\uff08\u7f16\u8bd1\u5668\u5728\u8bfb\u53d6\u5230\u7b2c\u516d\u884c\u65f6\u624d\u80fd\u53d1\u73b0\u8be5\u95ee\u9898\uff09\u3002 \u8bed\u6cd5\u9519\u8bef\u5728\u7f16\u7a0b\u65f6\u5f88\u5e38\u89c1\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u8fd9\u79cd\u7c7b\u578b\u7684\u9519\u8bef\u901a\u5e38\u6bd4\u8f83\u5bb9\u6613\u5b9a\u4f4d\u548c\u4fee\u590d\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u4f1a\u660e\u786e\u5730\u6307\u5411\u9519\u8bef\u53d1\u751f\u7684\u4f4d\u7f6e\u3002\u7a0b\u5e8f\u7684\u7f16\u8bd1\u53ea\u6709\u5728\u6240\u6709\u7684\u9519\u8bef\u90fd\u88ab\u4fee\u590d\u540e\u624d\u80fd\u591f\u5b8c\u6210\u3002 \u4f60\u53ef\u4ee5\u8bd5\u7740\u4ece\u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\u5220\u9664\u67d0\u4e9b\u5b57\u7b26\uff0c\u751a\u81f3\u5220\u9664\u6574\u884c\uff0c\u7136\u540e\u89c2\u5bdf\u7f16\u8bd1\u5668\u7684\u62a5\u9519\u4fe1\u606f\u3002\u73b0\u5728\uff0c\u5c06\u5206\u53f7\u5199\u5728\u7b2c\u4e94\u884c\u7684\u672b\u5c3e\u5e76\u5220\u9664 1\u30013 \u6216\u8005 4 \u884c\uff0c\u770b\u770b\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f","title":"\u8bed\u6cd5\u548c\u8bed\u6cd5\u9519\u8bef"},{"location":"1-10-Introduction-to-expressions/","tags":["expression"],"text":"\u8868\u8fbe\u5f0f \u8003\u8651\u4ee5\u4e0b\u8bed\u53e5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // five() is a function that returns the value 5 int five () { return 5 ; } int main () { int a { 2 }; // initialize variable a with literal value 2 int b { 2 + 3 }; // initialize variable b with computed value 5 int c { ( 2 * 3 ) + 4 }; // initialize variable c with computed value 10 int d { b }; // initialize variable d with variable value 5 int e { five () }; // initialize variable e with function return value 5 return 0 ; } \u4e0a\u8ff0\u5404\u8bed\u53e5\u90fd\u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\u3002\u6ce8\u610f\u4e0a\u8ff0\u53d8\u91cf\u7684\u521d\u59cb\u5316\u503c\u7684\u6784\u5efa\uff0c\u4f7f\u7528\u4e86\u4e0d\u540c\u7684\u5143\u7d20\uff1a\u5b57\u9762\u91cf\u3001\u53d8\u91cf\u3001\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u3002\u7136\u800c\uff0cC++\u4f1a\u5c06\u4e0a\u8ff0\u6240\u6709\u7684\u5f62\u5f0f\uff0c\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5355\u503c\uff0c\u5e76\u7528\u8be5\u503c\u6765\u5b8c\u6210\u53d8\u91cf\u7684\u521d\u59cb\u5316\u3002 \u4e0a\u8ff0\u521d\u59cb\u5316\u503c\u4e4b\u95f4\u6709\u4ec0\u4e48\u5171\u540c\u70b9\u5462\uff1f\u5b83\u4eec\u90fd\u5229\u7528\u4e86\u8868\u8fbe\u5f0f\u3002 \u8868\u8fbe\u5f0f \u662f\u5b57\u9762\u91cf\u3001\u53d8\u91cf\u3001\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u8c03\u7528\u7684\u7ed3\u5408\u4f53\u4e14\u6700\u7ec8\u53ef\u4ee5\u4ea7\u751f\u4e00\u4e2a\u5355\u503c\u3002\u6267\u884c\u8868\u8fbe\u5f0f\u7684\u8fc7\u7a0b\u79f0\u4e3a\u6c42\u503c\uff0c\u6700\u7ec8\u751f\u6210\u7684\u503c\u5219\u79f0\u4e3a\u8868\u8fbe\u5f0f\u7684 \u7ed3\u679c \u3002 \u5f53\u8868\u8fbe\u5f0f\u5b8c\u6210\u6c42\u503c\u540e\uff0c\u8868\u8fbe\u5f0f\u4e2d\u7684\u6bcf\u4e00\u9879\uff08term\uff09\u4e5f\u90fd\u5b8c\u6210\u4e86\u6c42\u503c\uff0c\u76f4\u5230\u5f97\u5230\u4e00\u4e2a\u6700\u7ec8\u7684\u5355\u503c\u7ed3\u679c\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u8868\u8fbe\u5f0f\u7684\u4f8b\u5b50\uff0c\u8868\u8fbe\u5f0f\u540e\u9762\u7684\u6ce8\u91ca\u89e3\u91ca\u4e86\u5b83\u7684\u6c42\u503c\u65b9\u6cd5\u3002 1 2 3 4 5 6 7 2 // 2 \u662f\u4e00\u4e2a\u5b57\u9762\u91cf\uff0c\u6c42\u503c\u7ed3\u679c\u4e3a 2 \"Hello world!\" // \"Hello world!\" \u662f\u4e00\u4e2a\u5b57\u9762\u91cf\uff0c\u6c42\u503c\u7ed3\u679c\u4e3a\u5b57\u7b26\u4e32 \"Hello world!\" x // x \u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u6c42\u503c\u7ed3\u679c\u5373 x \u7684\u503c 2 + 3 // \u8fd0\u7b97\u7b26 + \u5c06 2 \u548c 3 \u76f8\u52a0\u5f97\u5230 5 x = 2 + 3 // 2 + 3 \u6c42\u503c\u5f97\u5230 5\uff0c\u7136\u540e\u8d4b\u503c\u7ed9\u53d8\u91cf x std :: cout << x // x \u6c42\u503c\u5f97\u5230 x \u7684\u503c\uff0c\u7136\u540e\u5c06\u5176\u6253\u5370\u5230\u63a7\u5236\u53f0\u754c\u9762\u4e0a five () // \u5bf9\u51fd\u6570 five() \u7684\u8fd4\u56de\u503c\u6c42\u503c \u6b63\u5982\u4f60\u770b\u5230\u7684\u90a3\u6837\uff0c\u5b57\u9762\u91cf\u6c42\u503c\u7684\u7ed3\u679c\u5c31\u662f\u5176\u672c\u8eab\u3002\u53d8\u91cf\u6c42\u503c\u7684\u7ed3\u679c\uff0c\u662f\u53d8\u91cf\u7684\u503c\u3002\u867d\u7136\u51fd\u6570\u8c03\u7528\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\uff0c\u4f46\u662f\u5728\u8868\u8fbe\u5f0f\u7684\u8bed\u5883\u4e2d\uff0c\u51fd\u6570\u8c03\u7528\u7684\u6c42\u503c\u7ed3\u679c\u662f\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002\u8fd0\u7b97\u7b26\u7684\u6c42\u503c\u5219\u5141\u8bb8\u6211\u4eec\u5408\u5e76\u591a\u4e2a\u503c\u5e76\u6700\u7ec8\u5f97\u5230\u4e00\u4e2a\u65b0\u503c\u3002 \u6ce8\u610f\uff0c\u8868\u8fbe\u5f0f\u5e76\u4e0d\u4ee5\u5206\u53f7\u7ed3\u5c3e\uff0c\u800c\u4e14\u4e5f\u4e0d\u80fd\u4f5c\u4e3a\u5355\u72ec\u7684\u5143\u7d20\u8fdb\u884c\u7f16\u8bd1\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u8981\u7f16\u8bd1 x = 5 \uff0c\u4f60\u7684\u7f16\u8bd1\u5668\u5c06\u4f1a\u4fdd\u5b58\uff08\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4f1a\u63d0\u793a\u4f60\u7f3a\u5c11\u5206\u53f7\uff09\u3002\u5b9e\u9645\u4e0a\uff0c\u8868\u8fbe\u5f0f\u603b\u662f\u4f5c\u4e3a\u8bed\u53e5\u7684\u4e00\u90e8\u5206\u8fdb\u884c\u6c42\u503c\u7684\u3002 \u4f8b\u5982\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u6761\u8bed\u53e5\uff1a 1 int x { 2 + 3 }; // 2 + 3 \u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\u5b83\u4e0d\u4ee5\u5206\u53f7\u7ed3\u5c3e -- \u5206\u53f7\u5e94\u8be5\u4f4d\u4e8e\u5305\u542b\u8be5\u8868\u8fbe\u5f0f\u7684\u8bed\u53e5\u7684\u672b\u5c3e \u5982\u679c\u4f60\u60f3\u8981\u6df1\u7a76\u8be5\u8868\u8fbe\u5f0f\u7684\u8bed\u6cd5\uff0c\u5b83\u5e94\u8be5\u662f\u5982\u4e0b\u8fd9\u79cd\u5f62\u5f0f\uff1a 1 type identifier { expression }; Type \u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u7684\u7c7b\u578b\uff08\u8fd9\u91cc\u4f7f\u7528\u4e86 int \uff09\uff0c\u6807\u8bc6\u7b26\u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u7684\u53d8\u91cf\u540d\uff08\u8fd9\u91cc\u4f7f\u7528\u4e86 x \uff09\u3002\u8868\u8fbe\u5f0f\u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u7684\u8868\u8fbe\u5f0f\uff08\u8fd9\u91cc\u4f7f\u7528\u4e86 2+3\uff0c\u5373\u4e24\u4e2a\u5b57\u9762\u91cf\u548c\u4e00\u4e2a\u8fd0\u7b97\u7b26\uff09\u3002 \u5173\u952e\u4fe1\u606f \u5728 C++ \u4e2d\uff0c\u4efb\u4f55\u5141\u8bb8\u4f60\u4f7f\u7528\u4e00\u4e2a\u5355\u503c\u7684\u5730\u65b9\uff0c\u4f60\u90fd\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u8868\u8fbe\u5f0f\u6765\u66ff\u4ee3\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u6700\u7ec8\u4f1a\u6c42\u503c\u5f97\u5230\u4e00\u4e2a\u5355\u503c\u3002 \u8868\u8fbe\u5f0f\u8bed\u53e5 \u6709\u4e9b\u8868\u8fbe\u5f0f\u672c\u8eab\u662f\u6709\u5176\u7279\u5b9a\u7528\u9014\u7684\uff08\u4f8b\u5982 x = 5 )\u3002\u4f46\u662f\uff0c\u524d\u9762\u6211\u4eec\u4e5f\u8bf4\u8fc7\uff0c\u8868\u8fbe\u5f0f\u5fc5\u987b\u662f\u8bed\u53e5\u7684\u4e00\u90e8\u5206\uff0c\u90a3\u4e48\u5982\u4f55\u624d\u80fd\u5355\u72ec\u5730\u4f7f\u7528\u8fd9\u4e9b\u7279\u5b9a\u7684\u8868\u8fbe\u5f0f\u5462\uff1f \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8868\u8fbe\u5f0f\u8f6c\u6362\u4e3a\u4e00\u4e2a\u7b49\u4ef7\u7684\u8bed\u53e5\uff08\u79f0\u4e3a\u8868\u8fbe\u5f0f\u8bed\u53e5\uff09\u3002 \u8868\u8fbe\u5f0f\u8bed\u53e5 \u5305\u542b\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u7136\u540e\u7d27\u63a5\u7740\u4e00\u4e2a\u5206\u53f7\u3002\u5f53\u8be5\u8bed\u53e5\u6267\u884c\u65f6\uff0c\u8868\u8fbe\u5f0f\u5c31\u4f1a\u88ab\u6c42\u503c\uff08\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u4f1a\u88ab\u4e22\u5f03\uff09\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4efb\u610f\u8868\u8fbe\u5f0f\uff08\u4f8b\u5982 x = 5 \uff09\u8f6c\u6362\u6210\u7b49\u4ef7\u7684\u8868\u8fbe\u5f0f\u8bed\u53e5\uff08\u4f8b\u5982\uff0c x = 5; \uff09\u5e76\u7f16\u8bd1\u3002 \u6ce8\u610f\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u8bd1\u8868\u8fbe\u5f0f\u8bed\u53e5\uff0c\u4f46\u8fd9\u6837\u505a\u6709\u65f6\u662f\u6ca1\u6709\u610f\u4e49\u7684\uff08\u4f8b\u5982 2 * 3; \uff09\uff0c\u5b83\u7684\u6c42\u503c\u7ed3\u679c\u4e3a 6\uff0c\u7136\u540e 6 \u5c31\u88ab\u4e22\u5f03\u4e86\u3002 \u6cd5\u5219 \u8868\u8fbe\u5f0f\u4e2d\u8ba1\u7b97\u5f97\u5230\u7684\u503c\u4f1a\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u540e\u88ab\u4e22\u5f03\u3002","title":"1.10 - \u8868\u8fbe\u5f0f\u7b80\u4ecb"},{"location":"1-10-Introduction-to-expressions/#_1","text":"\u8003\u8651\u4ee5\u4e0b\u8bed\u53e5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // five() is a function that returns the value 5 int five () { return 5 ; } int main () { int a { 2 }; // initialize variable a with literal value 2 int b { 2 + 3 }; // initialize variable b with computed value 5 int c { ( 2 * 3 ) + 4 }; // initialize variable c with computed value 10 int d { b }; // initialize variable d with variable value 5 int e { five () }; // initialize variable e with function return value 5 return 0 ; } \u4e0a\u8ff0\u5404\u8bed\u53e5\u90fd\u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\u3002\u6ce8\u610f\u4e0a\u8ff0\u53d8\u91cf\u7684\u521d\u59cb\u5316\u503c\u7684\u6784\u5efa\uff0c\u4f7f\u7528\u4e86\u4e0d\u540c\u7684\u5143\u7d20\uff1a\u5b57\u9762\u91cf\u3001\u53d8\u91cf\u3001\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u3002\u7136\u800c\uff0cC++\u4f1a\u5c06\u4e0a\u8ff0\u6240\u6709\u7684\u5f62\u5f0f\uff0c\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5355\u503c\uff0c\u5e76\u7528\u8be5\u503c\u6765\u5b8c\u6210\u53d8\u91cf\u7684\u521d\u59cb\u5316\u3002 \u4e0a\u8ff0\u521d\u59cb\u5316\u503c\u4e4b\u95f4\u6709\u4ec0\u4e48\u5171\u540c\u70b9\u5462\uff1f\u5b83\u4eec\u90fd\u5229\u7528\u4e86\u8868\u8fbe\u5f0f\u3002 \u8868\u8fbe\u5f0f \u662f\u5b57\u9762\u91cf\u3001\u53d8\u91cf\u3001\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u8c03\u7528\u7684\u7ed3\u5408\u4f53\u4e14\u6700\u7ec8\u53ef\u4ee5\u4ea7\u751f\u4e00\u4e2a\u5355\u503c\u3002\u6267\u884c\u8868\u8fbe\u5f0f\u7684\u8fc7\u7a0b\u79f0\u4e3a\u6c42\u503c\uff0c\u6700\u7ec8\u751f\u6210\u7684\u503c\u5219\u79f0\u4e3a\u8868\u8fbe\u5f0f\u7684 \u7ed3\u679c \u3002 \u5f53\u8868\u8fbe\u5f0f\u5b8c\u6210\u6c42\u503c\u540e\uff0c\u8868\u8fbe\u5f0f\u4e2d\u7684\u6bcf\u4e00\u9879\uff08term\uff09\u4e5f\u90fd\u5b8c\u6210\u4e86\u6c42\u503c\uff0c\u76f4\u5230\u5f97\u5230\u4e00\u4e2a\u6700\u7ec8\u7684\u5355\u503c\u7ed3\u679c\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u8868\u8fbe\u5f0f\u7684\u4f8b\u5b50\uff0c\u8868\u8fbe\u5f0f\u540e\u9762\u7684\u6ce8\u91ca\u89e3\u91ca\u4e86\u5b83\u7684\u6c42\u503c\u65b9\u6cd5\u3002 1 2 3 4 5 6 7 2 // 2 \u662f\u4e00\u4e2a\u5b57\u9762\u91cf\uff0c\u6c42\u503c\u7ed3\u679c\u4e3a 2 \"Hello world!\" // \"Hello world!\" \u662f\u4e00\u4e2a\u5b57\u9762\u91cf\uff0c\u6c42\u503c\u7ed3\u679c\u4e3a\u5b57\u7b26\u4e32 \"Hello world!\" x // x \u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u6c42\u503c\u7ed3\u679c\u5373 x \u7684\u503c 2 + 3 // \u8fd0\u7b97\u7b26 + \u5c06 2 \u548c 3 \u76f8\u52a0\u5f97\u5230 5 x = 2 + 3 // 2 + 3 \u6c42\u503c\u5f97\u5230 5\uff0c\u7136\u540e\u8d4b\u503c\u7ed9\u53d8\u91cf x std :: cout << x // x \u6c42\u503c\u5f97\u5230 x \u7684\u503c\uff0c\u7136\u540e\u5c06\u5176\u6253\u5370\u5230\u63a7\u5236\u53f0\u754c\u9762\u4e0a five () // \u5bf9\u51fd\u6570 five() \u7684\u8fd4\u56de\u503c\u6c42\u503c \u6b63\u5982\u4f60\u770b\u5230\u7684\u90a3\u6837\uff0c\u5b57\u9762\u91cf\u6c42\u503c\u7684\u7ed3\u679c\u5c31\u662f\u5176\u672c\u8eab\u3002\u53d8\u91cf\u6c42\u503c\u7684\u7ed3\u679c\uff0c\u662f\u53d8\u91cf\u7684\u503c\u3002\u867d\u7136\u51fd\u6570\u8c03\u7528\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\uff0c\u4f46\u662f\u5728\u8868\u8fbe\u5f0f\u7684\u8bed\u5883\u4e2d\uff0c\u51fd\u6570\u8c03\u7528\u7684\u6c42\u503c\u7ed3\u679c\u662f\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002\u8fd0\u7b97\u7b26\u7684\u6c42\u503c\u5219\u5141\u8bb8\u6211\u4eec\u5408\u5e76\u591a\u4e2a\u503c\u5e76\u6700\u7ec8\u5f97\u5230\u4e00\u4e2a\u65b0\u503c\u3002 \u6ce8\u610f\uff0c\u8868\u8fbe\u5f0f\u5e76\u4e0d\u4ee5\u5206\u53f7\u7ed3\u5c3e\uff0c\u800c\u4e14\u4e5f\u4e0d\u80fd\u4f5c\u4e3a\u5355\u72ec\u7684\u5143\u7d20\u8fdb\u884c\u7f16\u8bd1\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u8981\u7f16\u8bd1 x = 5 \uff0c\u4f60\u7684\u7f16\u8bd1\u5668\u5c06\u4f1a\u4fdd\u5b58\uff08\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4f1a\u63d0\u793a\u4f60\u7f3a\u5c11\u5206\u53f7\uff09\u3002\u5b9e\u9645\u4e0a\uff0c\u8868\u8fbe\u5f0f\u603b\u662f\u4f5c\u4e3a\u8bed\u53e5\u7684\u4e00\u90e8\u5206\u8fdb\u884c\u6c42\u503c\u7684\u3002 \u4f8b\u5982\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u6761\u8bed\u53e5\uff1a 1 int x { 2 + 3 }; // 2 + 3 \u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\u5b83\u4e0d\u4ee5\u5206\u53f7\u7ed3\u5c3e -- \u5206\u53f7\u5e94\u8be5\u4f4d\u4e8e\u5305\u542b\u8be5\u8868\u8fbe\u5f0f\u7684\u8bed\u53e5\u7684\u672b\u5c3e \u5982\u679c\u4f60\u60f3\u8981\u6df1\u7a76\u8be5\u8868\u8fbe\u5f0f\u7684\u8bed\u6cd5\uff0c\u5b83\u5e94\u8be5\u662f\u5982\u4e0b\u8fd9\u79cd\u5f62\u5f0f\uff1a 1 type identifier { expression }; Type \u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u7684\u7c7b\u578b\uff08\u8fd9\u91cc\u4f7f\u7528\u4e86 int \uff09\uff0c\u6807\u8bc6\u7b26\u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u7684\u53d8\u91cf\u540d\uff08\u8fd9\u91cc\u4f7f\u7528\u4e86 x \uff09\u3002\u8868\u8fbe\u5f0f\u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u7684\u8868\u8fbe\u5f0f\uff08\u8fd9\u91cc\u4f7f\u7528\u4e86 2+3\uff0c\u5373\u4e24\u4e2a\u5b57\u9762\u91cf\u548c\u4e00\u4e2a\u8fd0\u7b97\u7b26\uff09\u3002 \u5173\u952e\u4fe1\u606f \u5728 C++ \u4e2d\uff0c\u4efb\u4f55\u5141\u8bb8\u4f60\u4f7f\u7528\u4e00\u4e2a\u5355\u503c\u7684\u5730\u65b9\uff0c\u4f60\u90fd\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u8868\u8fbe\u5f0f\u6765\u66ff\u4ee3\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u6700\u7ec8\u4f1a\u6c42\u503c\u5f97\u5230\u4e00\u4e2a\u5355\u503c\u3002","title":"\u8868\u8fbe\u5f0f"},{"location":"1-10-Introduction-to-expressions/#_2","text":"\u6709\u4e9b\u8868\u8fbe\u5f0f\u672c\u8eab\u662f\u6709\u5176\u7279\u5b9a\u7528\u9014\u7684\uff08\u4f8b\u5982 x = 5 )\u3002\u4f46\u662f\uff0c\u524d\u9762\u6211\u4eec\u4e5f\u8bf4\u8fc7\uff0c\u8868\u8fbe\u5f0f\u5fc5\u987b\u662f\u8bed\u53e5\u7684\u4e00\u90e8\u5206\uff0c\u90a3\u4e48\u5982\u4f55\u624d\u80fd\u5355\u72ec\u5730\u4f7f\u7528\u8fd9\u4e9b\u7279\u5b9a\u7684\u8868\u8fbe\u5f0f\u5462\uff1f \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8868\u8fbe\u5f0f\u8f6c\u6362\u4e3a\u4e00\u4e2a\u7b49\u4ef7\u7684\u8bed\u53e5\uff08\u79f0\u4e3a\u8868\u8fbe\u5f0f\u8bed\u53e5\uff09\u3002 \u8868\u8fbe\u5f0f\u8bed\u53e5 \u5305\u542b\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u7136\u540e\u7d27\u63a5\u7740\u4e00\u4e2a\u5206\u53f7\u3002\u5f53\u8be5\u8bed\u53e5\u6267\u884c\u65f6\uff0c\u8868\u8fbe\u5f0f\u5c31\u4f1a\u88ab\u6c42\u503c\uff08\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u4f1a\u88ab\u4e22\u5f03\uff09\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4efb\u610f\u8868\u8fbe\u5f0f\uff08\u4f8b\u5982 x = 5 \uff09\u8f6c\u6362\u6210\u7b49\u4ef7\u7684\u8868\u8fbe\u5f0f\u8bed\u53e5\uff08\u4f8b\u5982\uff0c x = 5; \uff09\u5e76\u7f16\u8bd1\u3002 \u6ce8\u610f\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u8bd1\u8868\u8fbe\u5f0f\u8bed\u53e5\uff0c\u4f46\u8fd9\u6837\u505a\u6709\u65f6\u662f\u6ca1\u6709\u610f\u4e49\u7684\uff08\u4f8b\u5982 2 * 3; \uff09\uff0c\u5b83\u7684\u6c42\u503c\u7ed3\u679c\u4e3a 6\uff0c\u7136\u540e 6 \u5c31\u88ab\u4e22\u5f03\u4e86\u3002 \u6cd5\u5219 \u8868\u8fbe\u5f0f\u4e2d\u8ba1\u7b97\u5f97\u5230\u7684\u503c\u4f1a\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u540e\u88ab\u4e22\u5f03\u3002","title":"\u8868\u8fbe\u5f0f\u8bed\u53e5"},{"location":"1-11-Developing-your-first-program/","text":"\u524d\u9762\u7684\u8bfe\u7a0b\u5df2\u7ecf\u4ecb\u7ecd\u4e86\u4e0d\u5c11\u672f\u8bed\u548c\u6982\u5ff5\uff0c\u8fd9\u4e9b\u672f\u8bed\u548c\u6982\u5ff5\u51e0\u4e4e\u4f1a\u5728\u6211\u4eec\u7f16\u5199\u7684\u4efb\u4f55\u7a0b\u5e8f\u4e2d\u51fa\u73b0\u3002\u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u8fd0\u7528\u6240\u5b66\u7684\u5185\u5bb9\uff0c\u52a8\u624b\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\u3002 \u5c0f\u7a0b\u5e8f\uff1a\u8f93\u5165\u7684\u6570\u5b57\u4e58\u4ee5 2 \u9996\u5148\uff0c\u6211\u4eec\u4f1a\u8981\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6574\u578b\u6570\u5e76\u7b49\u5f85\u7528\u6237\u5b8c\u6210\u8f93\u5165\uff0c\u7136\u540e\u6211\u4eec\u4f1a\u5c06\u8be5\u503c\u4e58\u4ee5 2 \u5e76\u544a\u8bc9\u7528\u6237\u8ba1\u7b97\u7ed3\u679c\u3002\u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff08\u5047\u8bbe\u8f93\u5165\u503c\u4e3a 4\uff09\uff1a 1 2 Enter an integer: 4 Double that number is: 8 \u4e0b\u9762\u8ba9\u6211\u4eec\u6765 \u9010\u6b65 \u5b8c\u6210\u4e0a\u8ff0\u529f\u80fd\uff1a \u6700\u4f73\u5b9e\u8df5 \u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u4f1a\u53bb\u5c1d\u8bd5\u4e00\u6b21\u6027\u5b8c\u6210\u6574\u4e2a\u7a0b\u5e8f\u7684\u7f16\u5199\uff0c\u7136\u540e\u4fbf\u4f1a\u56e0\u4e3a\u5927\u91cf\u62a5\u9519\u4fe1\u606f\u800c\u611f\u5230\u65e0\u6240\u9002\u4ece\u3002\u6bd4\u8f83\u597d\u7684\u65b9\u5f0f\u662f\u6bcf\u6b21\u6dfb\u52a0\u4e00\u90e8\u5206\u5185\u5bb9\u5e76\u4fdd\u8bc1\u5176\u80fd\u591f\u901a\u8fc7\u7f16\u8bd1\u548c\u901a\u8fc7\u6d4b\u8bd5\u3002\u5f53\u4f60\u80fd\u591f\u786e\u4fdd\u5176\u6b63\u786e\u5de5\u4f5c\u540e\uff0c\u518d\u7f16\u5199\u63a5\u4e0b\u6765\u7684\u4ee3\u7801\u3002 \u672c\u8282\u8bfe\u4e5f\u4f1a\u91c7\u53d6\u4e0a\u8ff0\u7b56\u7565\u3002\u6211\u4eec\u4f1a\u5c06\u7a0b\u5e8f\u7684\u5b9e\u73b0\u5206\u4e3a\u591a\u4e2a\u6b65\u9aa4\uff0c\u8bf7\u5c06\u5404\u6b65\u9aa4\u4e2d\u7684\u4ee3\u7801 \u8f93\u5165 \uff08\u4e0d\u8981\u590d\u5236\u7c98\u8d34\uff09\u8ba1\u7b97\u673a\uff0c\u7f16\u8bd1\u5e76\u4e14\u8fd0\u884c\u3002 \u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u63a7\u5236\u53f0\u7a0b\u5e8f\u9879\u76ee\u3002 \u73b0\u5728\uff0c\u7f16\u5199\u4e00\u4e2a\u57fa\u672c\u7684\u7a0b\u5e8f\u6846\u67b6\u3002\u6211\u4eec\u77e5\u9053\uff0c\u7f16\u5199\u4e00\u4e2a main() \u7a0b\u5e8f\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u6b65\u9aa4\uff08\u56e0\u4e3a C++\u7a0b\u5e8f\u5fc5\u987b\u5177\u6709\u4e00\u4e2a main \u51fd\u6570\uff09\u3002\u5982\u679c\u4f60\u7684 IDE \u6ca1\u6709\u4e3a\u4f60\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u7a7a\u51fd\u6570\u7684\u8bdd\uff0c\u8bf7\u50cf\u4e0b\u9762\u8fd9\u6837\u81ea\u884c\u7f16\u5199\u4e00\u4e2a\uff1a 1 2 3 4 int main () { return 0 ; } \u7531\u4e8e\u9700\u8981\u5411\u63a7\u5236\u53f0\u8f93\u51fa\u4fe1\u606f\u4ee5\u53ca\u4ece\u7528\u6237\u83b7\u53d6\u6570\uff0c\u56e0\u6b64\u5fc5\u987b\u8981\u5305\u542b iostream \u5934\u6587\u4ef6\u4ee5\u4fbf\u80fd\u591f\u8bbf\u95ee std:: cout \u548c std:: cin \u3002 1 2 3 4 5 6 #include int main () { return 0 ; } \u73b0\u5728\uff0c\u5411\u7528\u6237\u53d1\u9001\u4fe1\u606f\uff0c\u63d0\u793a\u4ed6\u8f93\u5165\u4e00\u4e2a\u6574\u578b\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << \"Enter an integer: \" ; return 0 ; } \u81f3\u6b64\uff0c\u8be5\u7a0b\u5e8f\u5e94\u8be5\u80fd\u591f\u5f97\u5230\u5982\u4e0b\u7684\u8f93\u51fa\u7ed3\u679c\uff1a 1 Enter an integer: \u7136\u540e\u7a0b\u5e8f\u5c31\u7ec8\u6b62\u4e86\u3002 \u63a5\u4e0b\u6765\uff0c\u9700\u8981\u4ece\u7528\u6237\u83b7\u53d6\u8f93\u5165\u4fe1\u606f\uff0c\u8fd9\u91cc\u4f7f\u7528 std:: cin \u548c operator>> \u6765\u5b8c\u6210\u3002\u4f46\u662f\u6211\u4eec\u5fc5\u987b\u5b9a\u4e49\u4e00\u4e2a\u7528\u6765\u5b58\u653e\u8f93\u5165\u7ed3\u679c\u7684\u53d8\u91cf\u3002 1 2 3 4 5 6 7 8 9 10 11 #include int main () { std :: cout << \"Enter an integer: \" ; int num { }; // define variable num as an integer variable std :: cin << num ; // get integer value from user's keyboard return 0 ; } \u662f\u65f6\u5019\u518d\u6b21\u7f16\u8bd1\u7a0b\u5e8f\u4e86... \u7cdf\u7cd5\uff01\u4f5c\u8005\u7684 Visual Studio 2017 \u62a5\u544a\u4e86\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 5 6 7 1>------ Build started: Project: Double, Configuration: Release Win32 ------ 1>Double.cpp 1>c:\\vcprojects\\double\\double.cpp(8): error C2678: binary '<<': no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion) 1>c:\\vcprojects\\double\\double.cpp: note: could be 'built-in C++ operator<<(bool, int)' 1>c:\\vcprojects\\double\\double.cpp: note: while trying to match the argument list '(std::istream, int)' 1>Done building project \"Double.vcxproj\" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== \u6709\u7f16\u8bd1\u9519\u8bef\uff01 \u9996\u5148\uff0c\u7531\u4e8e\u8fd9\u6b21\u7f16\u8bd1\u53d1\u751f\u5728\u6700\u65b0\u4e00\u6b21\u4ee3\u7801\u4fee\u6539\u4e4b\u540e\uff0c\u7136\u540e\u5c31\u62a5\u9519\u4e86\uff0c\u8bf4\u660e\u9519\u8bef\u662f\u5728\u6700\u540e\u4e00\u6b21\u4fee\u6539\u4ee3\u7801\u65f6\u88ab\u5f15\u5165\u7684\uff087\u30018 \u4e24\u884c\uff09\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u5206\u6790\u7684\u4ee3\u7801\u76f8\u5bf9\u4e8e\u5168\u90e8\u4ee3\u7801\u8d77\u59cb\u662f\u5f88\u5c11\u7684\u3002\u7b2c\u4e03\u884c\u975e\u5e38\u7b80\u5355\uff08\u4ec5\u4ec5\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53d8\u91cf\uff09\uff0c\u9519\u8bef\u5f88\u53ef\u80fd\u4e0d\u5728\u8fd9\u4e00\u884c\u3002\u8fd9\u6837\u4e00\u6765\u7b2c\u516b\u884c\u7684\u5acc\u7591\u5c31\u5f88\u5927\u4e86\u3002 \u5176\u6b21\uff0c\u8fd9\u91cc\u7684\u62a5\u9519\u4fe1\u606f\u5e76\u4e0d\u662f\u975e\u5e38\u6613\u8bfb\u3002\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u4ece\u4e2d\u5bfb\u627e\u4e00\u4e9b\u5173\u952e\u4fe1\u606f\uff1a\u7f16\u8bd1\u5668\u544a\u8bc9\u6211\u4eec\uff0c\u9519\u8bef\u53d1\u9001\u5728\u7b2c\u516b\u884c\u3002\u8fd9\u610f\u5473\u7740\u9519\u8bef\u5f88\u53ef\u80fd\u53d1\u751f\u5728\u7b2c\u516b\u884c\u6216\u7b2c\u516b\u884c\u4e4b\u524d\uff0c\u8fd9\u4e5f\u5370\u8bc1\u4e86\u6211\u4eec\u4e4b\u524d\u7684\u731c\u60f3\u3002\u7136\u540e\uff0c\u7f16\u8bd1\u5668\u544a\u8bc9\u6211\u4eec\uff0c\u5b83\u6ca1\u6709\u80fd\u591f\u627e\u5230\u4e00\u4e2a\u80fd\u591f\u5728\u7b26\u53f7\u5de6\u4fa7\u63a5\u53d7\u4e00\u4e2a std:: istream \u7c7b\u578b\uff08\u5373 std:: cin \u7684\u7c7b\u578b\uff09\u7684\u64cd\u4f5c\u6570\u7684 << \u8fd0\u7b97\u7b26\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u8fd0\u7b97\u7b26 << \u4e0d\u77e5\u9053\u5982\u4f55\u5904\u7406 std:: cin \u3002\u56e0\u6b64\uff0c\u5f15\u8d77\u9519\u8bef\u7684\u8981\u4e48\u662f\u4f7f\u7528 std:: cin \u4e0d\u5f53\uff0c\u8981\u4e48\u662f\u4f7f\u7528 << \u4e0d\u5f53\u3002 \u770b\u51fa\u95ee\u9898\u4e86\u5417\uff1f\u5982\u679c\u8fd8\u6ca1\u6709\uff0c\u90a3\u518d\u4ed4\u7ec6\u770b\u770b\u3002 \u4e0b\u9762\u4ee3\u7801\u662f\u6b63\u786e\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { std :: cout << \"Enter an integer: \" ; int num { }; std :: cin >> num ; // std::cin uses operator >>, not operator < // worst version int main () { std :: cout << \"Enter an integer: \" ; int num { }; std :: cin >> num ; num = num * 2 ; // double num's value, then assign that value back to num std :: cout << \"Double that number is: \" << num << '\\n' ; return 0 ; } \u5728\u8fd9\u4e2a\u65b9\u6848\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u8868\u8fbe\u5f0f\u5c06 num \u7684\u503c\u4e58\u4ee5 2\uff0c\u7136\u540e\u5c06\u5176\u518d\u6b21\u8d4b\u503c\u7ed9 num \u3002\u6b64\u65f6\uff0c num \u7684\u503c\u5c31\u662f\u4e58\u4ee5 2 \u540e\u7684\u7ed3\u679c\u4e86\u3002 \u4e3a\u4ec0\u4e48\u8bf4\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u597d\u5462\uff1f \u5728\u8d4b\u503c\u8bed\u53e5\u4e4b\u524d\uff0c num \u5305\u542b\u7684\u662f\u7528\u6237\u7684\u8f93\u5165\uff0c\u5728\u8d4b\u503c\u8bed\u53e5\u4e4b\u540e\uff0c\u5b83\u5305\u542b\u7684\u662f\u4e00\u4e2a\u4e0d\u540c\u7684\u503c\u3002\u8fd9\u4f1a\u4ee4\u4eba\u611f\u5230\u56f0\u60d1\u3002 \u901a\u8fc7\u4e3a\u63a5\u6536\u8f93\u5165\u7684\u53d8\u91cf\u8d4b\u65b0\u503c\uff0c\u6211\u4eec\u8986\u76d6\u4e86\u5b83\u539f\u6709\u7684\u503c\uff0c\u90a3\u4e48\u5982\u679c\u5728\u540e\u7eed\u6269\u5c55\u7a0b\u5e8f\u7684\u65f6\u5019\u5e0c\u671b\u518d\u6b21\u4f7f\u7528\u8fd9\u4e2a\u8f93\u5165\u503c\u6765\u505a\u4e9b\u522b\u7684\u4e8b\u60c5\u5c31\u65e0\u6cd5\u5b9e\u73b0\u4e86\uff08\u4f8b\u5982\u5c06\u8f93\u5165\u7ed3\u679c\u4e58\u4ee5 3\uff09\uff0c\u56e0\u4e3a\u5b83\u5df2\u7ecf\u88ab\u4e22\u5f03\u4e86\u3002 \u8fd8\u4e0d\u9519\u7684\u89e3\u51b3\u65b9\u6848 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // less-bad version int main () { std :: cout << \"Enter an integer: \" ; int num { }; std :: cin >> num ; int doublenum { num * 2 }; // define a new variable and initialize it with num * 2 std :: cout << \"Double that number is: \" << doublenum << '\\n' ; // then print the value of that variable here return 0 ; } \u8fd9\u4e2a\u65b9\u6848\u975e\u5e38\u7684\u76f4\u767d\uff0c\u5b83\u89e3\u51b3\u4e86\u4e0a\u4e00\u79cd\u65b9\u6848\u4e2d\u4f1a\u9762\u4e34\u7684\u4e24\u4e2a\u95ee\u9898\u3002 \u8fd9\u4e2a\u65b9\u6848\u7684\u4e3b\u8981\u7f3a\u70b9\u5728\u4e8e\uff0c\u8fd9\u91cc\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\uff08\u7740\u65e0\u7591\u589e\u52a0\u4e86\u590d\u6742\u6027\uff09\u6765\u5b58\u50a8\u4e00\u4e2a\u4e00\u6b21\u6027\u7684\u503c\u3002\u4e00\u5b9a\u8fd8\u6709\u66f4\u597d\u7684\u65b9\u6cd5\u3002 \u6700\u4f73\u65b9\u6848 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // preferred version int main () { std :: cout << \"Enter an integer: \" ; int num { }; std :: cin >> num ; std :: cout << \"Double that number is: \" << num * 2 << '\\n' ; // use an expression to multiply num * 2 at the point where we are going to print it return 0 ; } \u8fd9\u79cd\u65b9\u6cd5\u662f\u8fd9\u4e00\u7cfb\u5217\u89e3\u51b3\u65b9\u6848\u4e2d\u6700\u63a8\u8350\u7684\u3002\u5f53 std:: cout \u6267\u884c\u7684\u65f6\u5019\uff0c\u8868\u8fbe\u5f0f num * 2 \u4f1a\u8fdb\u884c\u6c42\u503c\uff0c\u5176\u7ed3\u679c\u5c31\u662f\u4e24\u500d\u7684 num \u503c\u3002\u8be5\u503c\u4f1a\u88ab\u6253\u5370\u5904\u7406\uff0c\u540c\u65f6\u53d8\u91cf num \u7684\u503c\u5e76\u6ca1\u6709\u88ab\u4fee\u6539\uff0c\u65b9\u4fbf\u6211\u4eec\u540e\u7eed\u7ee7\u7eed\u4f7f\u7528\u5b83\u3002 \u8fd9\u4e2a\u7248\u672c\u662f\u6211\u4eec\u63d0\u4f9b\u7684\u53c2\u8003\u7b54\u6848\u3002 \u4f5c\u8005\u6ce8 \u7f16\u7a0b\u7684\u9996\u8981\u76ee\u6807\uff0c\u662f\u8ba9\u7a0b\u5e8f\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u3002\u4e0d\u80fd\u5de5\u4f5c\u7684\u7a0b\u5e8f\u4e0d\u8bba\u5199\u7684\u591a\u597d s\uff0c\u90fd\u662f\u6ca1\u6709\u610f\u4e49\u7684\u3002 \u7136\u800c\uff0c\u6211\u5f88\u559c\u6b22\u7684\u4e00\u53e5\u540d\u8a00\u662f\u8fd9\u4e48\u8bf4\u7684\uff1a\u4f60\u5fc5\u987b\u5199\u8fc7\u4e00\u904d\u4ee3\u7801\u4e4b\u540e\uff0c\u624d\u80fd\u77e5\u9053\u4f60\u6700\u521d\u5e94\u8be5\u5982\u4f55\u53bb\u5199\u5b83\u3002\u8fd9\u53e5\u8bdd\u9053\u51fa\u4e86\u8fd9\u6837\u4e00\u4e2a\u4e8b\u5b9e\uff0c\u6700\u4f73\u7684\u89e3\u51b3\u65b9\u6848\u5f80\u5f80\u4e0d\u660e\u663e\u7684\uff0c\u800c\u6700\u5148\u60f3\u5230\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5f80\u5f80\u53c8\u4e0d\u662f\u6700\u4f73\u7684\u3002 \u5728\u6211\u4eec\u4e13\u6ce8\u4e8e\u5982\u4f55\u89e3\u51b3\u95ee\u9898\u5e76\u4f7f\u5f97\u7a0b\u5e8f\u8fd0\u884c\u8d77\u6765\u7684\u65f6\u5019\uff0c\u82b1\u5927\u91cf\u7684\u65f6\u95f4\u53d6\u7422\u78e8\u8fd9\u4e9b\u6211\u4eec\u751a\u81f3\u90fd\u4e0d\u77e5\u9053\u662f\u5426\u6700\u7ec8\u4f1a\u88ab\u4f7f\u7528\u7684\u4ee3\u7801\uff0c\u662f\u4e0d\u5212\u7b97\u3002\u56e0\u6b64\u6211\u4eec\u9700\u8981\u53e6\u8f9f\u8e4a\u5f84\uff0c\u6682\u65f6\u8df3\u8fc7\u5f02\u5e38\u5904\u7406\u548c\u6ce8\u91ca\uff0c\u4ee3\u7801\u4e2d\u5145\u65a5\u7740\u5927\u91cf\u7684\u5e2e\u52a9\u5b9a\u4f4d\u95ee\u9898\u7684\u8c03\u8bd5\u4ee3\u7801\u4e5f\u65e0\u6240\u8c13\uff0c\u6211\u4eec\u8981\u5b66\u4f1a\u6478\u7740\u77f3\u5934\u8fc7\u6cb3\uff0c\u800c\u5f53\u4e00\u4e2a\u65b9\u6cd5\u5f7b\u5e95\u884c\u4e0d\u901a\u7684\u65f6\u5019\uff0c\u5c31\u6362\u4e00\u79cd\u65b9\u6cd5\u4ece\u65b0\u5f00\u59cb\u3002 \u8fd9\u4e48\u505a\u7684\u7ed3\u679c\u5c31\u662f\uff0c\u6700\u521d\u7248\u672c\u7684\u4ee3\u7801\u5f80\u5f80\u7f3a\u4e4f\u6761\u7406\u3001\u5065\u58ee\u6027\u4e5f\u4e0d\u4f73\u3001\u53ef\u8bfb\u6027\u4e0d\u597d\uff0c\u5c31\u66f4\u522b\u63d0\u4ec0\u4e48\u7b80\u4ecb\u660e\u4e86\u4e86\u3002\u56e0\u6b64\uff0c\u5f53\u4f60\u7684\u4ee3\u7801\u53ef\u4ee5\u5de5\u4f5c\u4e4b\u540e\uff0c\u4f60\u7684\u5de5\u4f5c\u8fd8\u6ca1\u6709\u5b8c\u6210\uff08\u51fa\u53d1\u4f60\u7f16\u5199\u7684\u4ee3\u7801\u662f\u4e00\u6b21\u6027\u7684\uff09\u3002\u63a5\u4e0b\u6765\uff0c\u4f60\u9700\u8981\u6e05\u7406\u4f60\u7684\u4ee3\u7801\uff0c\u5305\u62ec\u5220\u9664\uff08\u6216\u6ce8\u91ca\u6389\uff09\u5e2e\u52a9\u8c03\u8bd5\u7684\u4e34\u65f6\u6027\u4ee3\u7801\u3001\u7f16\u5199\u6ce8\u91ca\u3001\u5904\u7406\u53ef\u80fd\u53d1\u751f\u7684\u9519\u8bef\u3001\u7f8e\u5316\u4ee3\u7801\u7684\u6392\u7248\u5e76\u786e\u4fdd\u4ee3\u7801\u9075\u5faa\u4e86\u76f8\u5173\u7684\u6700\u4f73\u5b9e\u8df5\u3002\u6b64\u5916\uff0c\u4f60\u7684\u4ee3\u7801\u53ef\u80fd\u5e76\u4e0d\u662f\u6700\u7cbe\u7b80\u7684\uff0c\u4e5f\u8bb8\u91cc\u9762\u8fd8\u5b58\u5728\u7740\u53ef\u4ee5\u88ab\u5408\u5e76\u7684\u5197\u4f59\u903b\u8f91\uff0c\u6216\u8005\u5b58\u5728\u7740\u53ef\u4ee5\u88ab\u5408\u5e76\u7684\u8bed\u53e5\uff0c\u4ea6\u6216\u662f\u6709\u4e9b\u53d8\u91cf\u662f\u6ca1\u6709\u5fc5\u8981\u7684\uff0c\u4ee5\u53ca\u5176\u4ed6\u5404\u79cd\u53ef\u4ee5\u88ab\u7b80\u5316\u7684\u5e38\u89c1\u3002\u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u90fd\u8feb\u5207\u7684\u60f3\u8981\u53bb\u4f18\u5316\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u800c\u5b9e\u9645\u4e0a\u7a0b\u5e8f\u7684\u53ef\u7ef4\u62a4\u6027\u624d\u662f\u5b83\u4eec\u6700\u5e94\u8be5\u4f18\u5148\u8003\u8651\u7684\u4e8b\u60c5\u3002 \u672c\u6559\u7a0b\u63d0\u4f9b\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5f88\u5c11\u662f\u7b2c\u4e00\u6b21\u5c31\u5f97\u5230\u6700\u4f73\u89e3\u51b3\u65b9\u6848\u7684\u3002\u5b9e\u9645\u4e0a\uff0c\u5b83\u4eec\u90fd\u7ecf\u8fc7\u4e86\u4e0d\u65ad\u7684\u4fee\u6539\uff0c\u76f4\u5230\u6211\u4eec\u8ba4\u4e3a\u6ca1\u6709\u4ec0\u4e48\u53ef\u4ee5\u518d\u4f18\u5316\u7684\u4e86\u3002\u5373\u4f7f\u662f\u8fd9\u6837\uff0c\u5f88\u591a\u65f6\u5019\u8bfb\u8005\u8fd8\u662f\u80fd\u591f\u5411\u6211\u4eec\u63d0\u51fa\u5f88\u591a\u4f18\u5316\u7684\u5efa\u8bae\uff01 \u8bf4\u8fd9\u4e48\u591a\uff0c\u6211\u60f3\u8868\u8fbe\u7684\u662f\uff1a\u5982\u679c\u4f60\u6ca1\u6709\u4e00\u6b21\u6027\u60f3\u5230\u4e00\u4e2a\u6700\u4f18\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u8bf7\u4e0d\u8981\u4e3a\u6b64\u611f\u5230\u6cae\u4e27\uff0c\u8fd9\u5f88\u6b63\u5e38\u3002\u7cbe\u76ca\u6c42\u7cbe\u5728\u7f16\u7a0b\u4e2d\u662f\u4e00\u4e2a\u8fed\u4ee3\u7684\u8fc7\u7a0b\u3002 \u4f5c\u8005\u6ce8 \u8fd8\u6709\u4e00\u4ef6\u4e8b\uff1a\u4f60\u53ef\u80fd\u4f1a\u60f3\uff0c\u201dC++\u4e2d\u6709\u8fd9\u4e48\u591a\u7684\u89c4\u5219\u548c\u6982\u5ff5\uff0c\u6211\u600e\u4e48\u624d\u80fd\u8bb0\u4f4f\u5b83\u4eec\u5462\uff1f\u201c \u7b80\u5355\u6765\u8bf4\uff0c\u4f60\u5e76\u4e0d\u9700\u8981\u8bb0\u4f4f\u5b83\u4eec\u3002\u4f7f\u7528 C++\uff0c\u4e00\u65b9\u9762\u5728\u4e8e\u4f7f\u7528\u4f60\u5df2\u7ecf\u638c\u63e1\u7684\u77e5\u8bc6\uff0c\u53e6\u4e00\u65b9\u9762\u5728\u4e8e\u638c\u63e1\u5982\u4f55\u67e5\u627e\u5e76\u4f7f\u7528\u4f60\u6ca1\u6709\u638c\u63e1\u7684\u77e5\u8bc6\u3002 \u5f53\u4f60\u7b2c\u4e00\u6b21\u9605\u8bfb\u672c\u6559\u7a0b\u7684\u65f6\u5019\uff0c\u4e0d\u8981\u628a\u6ce8\u610f\u529b\u653e\u5728\u8bb0\u5fc6\u89c4\u5219\u4e0a\uff0c\u800c\u662f\u8981\u91cd\u70b9\u7406\u89e3\u4f7f\u7528C++\u4f60\u80fd\u505a\u54ea\u4e9b\u4e8b\u3002\u7136\u540e\uff0c\u5f53\u4f60\u771f\u6b63\u9700\u8981\u5728\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u67d0\u4e2a\u529f\u80fd\u7684\u65f6\u5019\uff0c\u4f60\u53ef\u4ee5\u518d\u56de\u6765\uff08\u6216\u8005\u5230\u5176\u4ed6\u7f51\u7ad9\uff09\u53bb\u5237\u65b0\u5de9\u56fa\u4f60\u7684\u77e5\u8bc6\u5e76\u638c\u63e1\u5e94\u8be5\u5982\u4f55\u53bb\u505a\u3002","title":"1.11 - \u5f00\u53d1\u4f60\u7684\u7b2c\u4e00\u4e2a\u7a0b\u5e8f"},{"location":"1-11-Developing-your-first-program/#2","text":"\u9996\u5148\uff0c\u6211\u4eec\u4f1a\u8981\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6574\u578b\u6570\u5e76\u7b49\u5f85\u7528\u6237\u5b8c\u6210\u8f93\u5165\uff0c\u7136\u540e\u6211\u4eec\u4f1a\u5c06\u8be5\u503c\u4e58\u4ee5 2 \u5e76\u544a\u8bc9\u7528\u6237\u8ba1\u7b97\u7ed3\u679c\u3002\u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff08\u5047\u8bbe\u8f93\u5165\u503c\u4e3a 4\uff09\uff1a 1 2 Enter an integer: 4 Double that number is: 8 \u4e0b\u9762\u8ba9\u6211\u4eec\u6765 \u9010\u6b65 \u5b8c\u6210\u4e0a\u8ff0\u529f\u80fd\uff1a \u6700\u4f73\u5b9e\u8df5 \u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u4f1a\u53bb\u5c1d\u8bd5\u4e00\u6b21\u6027\u5b8c\u6210\u6574\u4e2a\u7a0b\u5e8f\u7684\u7f16\u5199\uff0c\u7136\u540e\u4fbf\u4f1a\u56e0\u4e3a\u5927\u91cf\u62a5\u9519\u4fe1\u606f\u800c\u611f\u5230\u65e0\u6240\u9002\u4ece\u3002\u6bd4\u8f83\u597d\u7684\u65b9\u5f0f\u662f\u6bcf\u6b21\u6dfb\u52a0\u4e00\u90e8\u5206\u5185\u5bb9\u5e76\u4fdd\u8bc1\u5176\u80fd\u591f\u901a\u8fc7\u7f16\u8bd1\u548c\u901a\u8fc7\u6d4b\u8bd5\u3002\u5f53\u4f60\u80fd\u591f\u786e\u4fdd\u5176\u6b63\u786e\u5de5\u4f5c\u540e\uff0c\u518d\u7f16\u5199\u63a5\u4e0b\u6765\u7684\u4ee3\u7801\u3002 \u672c\u8282\u8bfe\u4e5f\u4f1a\u91c7\u53d6\u4e0a\u8ff0\u7b56\u7565\u3002\u6211\u4eec\u4f1a\u5c06\u7a0b\u5e8f\u7684\u5b9e\u73b0\u5206\u4e3a\u591a\u4e2a\u6b65\u9aa4\uff0c\u8bf7\u5c06\u5404\u6b65\u9aa4\u4e2d\u7684\u4ee3\u7801 \u8f93\u5165 \uff08\u4e0d\u8981\u590d\u5236\u7c98\u8d34\uff09\u8ba1\u7b97\u673a\uff0c\u7f16\u8bd1\u5e76\u4e14\u8fd0\u884c\u3002 \u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u63a7\u5236\u53f0\u7a0b\u5e8f\u9879\u76ee\u3002 \u73b0\u5728\uff0c\u7f16\u5199\u4e00\u4e2a\u57fa\u672c\u7684\u7a0b\u5e8f\u6846\u67b6\u3002\u6211\u4eec\u77e5\u9053\uff0c\u7f16\u5199\u4e00\u4e2a main() \u7a0b\u5e8f\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u6b65\u9aa4\uff08\u56e0\u4e3a C++\u7a0b\u5e8f\u5fc5\u987b\u5177\u6709\u4e00\u4e2a main \u51fd\u6570\uff09\u3002\u5982\u679c\u4f60\u7684 IDE \u6ca1\u6709\u4e3a\u4f60\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u7a7a\u51fd\u6570\u7684\u8bdd\uff0c\u8bf7\u50cf\u4e0b\u9762\u8fd9\u6837\u81ea\u884c\u7f16\u5199\u4e00\u4e2a\uff1a 1 2 3 4 int main () { return 0 ; } \u7531\u4e8e\u9700\u8981\u5411\u63a7\u5236\u53f0\u8f93\u51fa\u4fe1\u606f\u4ee5\u53ca\u4ece\u7528\u6237\u83b7\u53d6\u6570\uff0c\u56e0\u6b64\u5fc5\u987b\u8981\u5305\u542b iostream \u5934\u6587\u4ef6\u4ee5\u4fbf\u80fd\u591f\u8bbf\u95ee std:: cout \u548c std:: cin \u3002 1 2 3 4 5 6 #include int main () { return 0 ; } \u73b0\u5728\uff0c\u5411\u7528\u6237\u53d1\u9001\u4fe1\u606f\uff0c\u63d0\u793a\u4ed6\u8f93\u5165\u4e00\u4e2a\u6574\u578b\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << \"Enter an integer: \" ; return 0 ; } \u81f3\u6b64\uff0c\u8be5\u7a0b\u5e8f\u5e94\u8be5\u80fd\u591f\u5f97\u5230\u5982\u4e0b\u7684\u8f93\u51fa\u7ed3\u679c\uff1a 1 Enter an integer: \u7136\u540e\u7a0b\u5e8f\u5c31\u7ec8\u6b62\u4e86\u3002 \u63a5\u4e0b\u6765\uff0c\u9700\u8981\u4ece\u7528\u6237\u83b7\u53d6\u8f93\u5165\u4fe1\u606f\uff0c\u8fd9\u91cc\u4f7f\u7528 std:: cin \u548c operator>> \u6765\u5b8c\u6210\u3002\u4f46\u662f\u6211\u4eec\u5fc5\u987b\u5b9a\u4e49\u4e00\u4e2a\u7528\u6765\u5b58\u653e\u8f93\u5165\u7ed3\u679c\u7684\u53d8\u91cf\u3002 1 2 3 4 5 6 7 8 9 10 11 #include int main () { std :: cout << \"Enter an integer: \" ; int num { }; // define variable num as an integer variable std :: cin << num ; // get integer value from user's keyboard return 0 ; } \u662f\u65f6\u5019\u518d\u6b21\u7f16\u8bd1\u7a0b\u5e8f\u4e86... \u7cdf\u7cd5\uff01\u4f5c\u8005\u7684 Visual Studio 2017 \u62a5\u544a\u4e86\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 5 6 7 1>------ Build started: Project: Double, Configuration: Release Win32 ------ 1>Double.cpp 1>c:\\vcprojects\\double\\double.cpp(8): error C2678: binary '<<': no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion) 1>c:\\vcprojects\\double\\double.cpp: note: could be 'built-in C++ operator<<(bool, int)' 1>c:\\vcprojects\\double\\double.cpp: note: while trying to match the argument list '(std::istream, int)' 1>Done building project \"Double.vcxproj\" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== \u6709\u7f16\u8bd1\u9519\u8bef\uff01 \u9996\u5148\uff0c\u7531\u4e8e\u8fd9\u6b21\u7f16\u8bd1\u53d1\u751f\u5728\u6700\u65b0\u4e00\u6b21\u4ee3\u7801\u4fee\u6539\u4e4b\u540e\uff0c\u7136\u540e\u5c31\u62a5\u9519\u4e86\uff0c\u8bf4\u660e\u9519\u8bef\u662f\u5728\u6700\u540e\u4e00\u6b21\u4fee\u6539\u4ee3\u7801\u65f6\u88ab\u5f15\u5165\u7684\uff087\u30018 \u4e24\u884c\uff09\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u5206\u6790\u7684\u4ee3\u7801\u76f8\u5bf9\u4e8e\u5168\u90e8\u4ee3\u7801\u8d77\u59cb\u662f\u5f88\u5c11\u7684\u3002\u7b2c\u4e03\u884c\u975e\u5e38\u7b80\u5355\uff08\u4ec5\u4ec5\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53d8\u91cf\uff09\uff0c\u9519\u8bef\u5f88\u53ef\u80fd\u4e0d\u5728\u8fd9\u4e00\u884c\u3002\u8fd9\u6837\u4e00\u6765\u7b2c\u516b\u884c\u7684\u5acc\u7591\u5c31\u5f88\u5927\u4e86\u3002 \u5176\u6b21\uff0c\u8fd9\u91cc\u7684\u62a5\u9519\u4fe1\u606f\u5e76\u4e0d\u662f\u975e\u5e38\u6613\u8bfb\u3002\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u4ece\u4e2d\u5bfb\u627e\u4e00\u4e9b\u5173\u952e\u4fe1\u606f\uff1a\u7f16\u8bd1\u5668\u544a\u8bc9\u6211\u4eec\uff0c\u9519\u8bef\u53d1\u9001\u5728\u7b2c\u516b\u884c\u3002\u8fd9\u610f\u5473\u7740\u9519\u8bef\u5f88\u53ef\u80fd\u53d1\u751f\u5728\u7b2c\u516b\u884c\u6216\u7b2c\u516b\u884c\u4e4b\u524d\uff0c\u8fd9\u4e5f\u5370\u8bc1\u4e86\u6211\u4eec\u4e4b\u524d\u7684\u731c\u60f3\u3002\u7136\u540e\uff0c\u7f16\u8bd1\u5668\u544a\u8bc9\u6211\u4eec\uff0c\u5b83\u6ca1\u6709\u80fd\u591f\u627e\u5230\u4e00\u4e2a\u80fd\u591f\u5728\u7b26\u53f7\u5de6\u4fa7\u63a5\u53d7\u4e00\u4e2a std:: istream \u7c7b\u578b\uff08\u5373 std:: cin \u7684\u7c7b\u578b\uff09\u7684\u64cd\u4f5c\u6570\u7684 << \u8fd0\u7b97\u7b26\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u8fd0\u7b97\u7b26 << \u4e0d\u77e5\u9053\u5982\u4f55\u5904\u7406 std:: cin \u3002\u56e0\u6b64\uff0c\u5f15\u8d77\u9519\u8bef\u7684\u8981\u4e48\u662f\u4f7f\u7528 std:: cin \u4e0d\u5f53\uff0c\u8981\u4e48\u662f\u4f7f\u7528 << \u4e0d\u5f53\u3002 \u770b\u51fa\u95ee\u9898\u4e86\u5417\uff1f\u5982\u679c\u8fd8\u6ca1\u6709\uff0c\u90a3\u518d\u4ed4\u7ec6\u770b\u770b\u3002 \u4e0b\u9762\u4ee3\u7801\u662f\u6b63\u786e\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { std :: cout << \"Enter an integer: \" ; int num { }; std :: cin >> num ; // std::cin uses operator >>, not operator < // worst version int main () { std :: cout << \"Enter an integer: \" ; int num { }; std :: cin >> num ; num = num * 2 ; // double num's value, then assign that value back to num std :: cout << \"Double that number is: \" << num << '\\n' ; return 0 ; } \u5728\u8fd9\u4e2a\u65b9\u6848\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u8868\u8fbe\u5f0f\u5c06 num \u7684\u503c\u4e58\u4ee5 2\uff0c\u7136\u540e\u5c06\u5176\u518d\u6b21\u8d4b\u503c\u7ed9 num \u3002\u6b64\u65f6\uff0c num \u7684\u503c\u5c31\u662f\u4e58\u4ee5 2 \u540e\u7684\u7ed3\u679c\u4e86\u3002 \u4e3a\u4ec0\u4e48\u8bf4\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u597d\u5462\uff1f \u5728\u8d4b\u503c\u8bed\u53e5\u4e4b\u524d\uff0c num \u5305\u542b\u7684\u662f\u7528\u6237\u7684\u8f93\u5165\uff0c\u5728\u8d4b\u503c\u8bed\u53e5\u4e4b\u540e\uff0c\u5b83\u5305\u542b\u7684\u662f\u4e00\u4e2a\u4e0d\u540c\u7684\u503c\u3002\u8fd9\u4f1a\u4ee4\u4eba\u611f\u5230\u56f0\u60d1\u3002 \u901a\u8fc7\u4e3a\u63a5\u6536\u8f93\u5165\u7684\u53d8\u91cf\u8d4b\u65b0\u503c\uff0c\u6211\u4eec\u8986\u76d6\u4e86\u5b83\u539f\u6709\u7684\u503c\uff0c\u90a3\u4e48\u5982\u679c\u5728\u540e\u7eed\u6269\u5c55\u7a0b\u5e8f\u7684\u65f6\u5019\u5e0c\u671b\u518d\u6b21\u4f7f\u7528\u8fd9\u4e2a\u8f93\u5165\u503c\u6765\u505a\u4e9b\u522b\u7684\u4e8b\u60c5\u5c31\u65e0\u6cd5\u5b9e\u73b0\u4e86\uff08\u4f8b\u5982\u5c06\u8f93\u5165\u7ed3\u679c\u4e58\u4ee5 3\uff09\uff0c\u56e0\u4e3a\u5b83\u5df2\u7ecf\u88ab\u4e22\u5f03\u4e86\u3002","title":"\u4e0d\u597d\u7684\u89e3\u51b3\u65b9\u6848\uff1a"},{"location":"1-11-Developing-your-first-program/#_2","text":"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // less-bad version int main () { std :: cout << \"Enter an integer: \" ; int num { }; std :: cin >> num ; int doublenum { num * 2 }; // define a new variable and initialize it with num * 2 std :: cout << \"Double that number is: \" << doublenum << '\\n' ; // then print the value of that variable here return 0 ; } \u8fd9\u4e2a\u65b9\u6848\u975e\u5e38\u7684\u76f4\u767d\uff0c\u5b83\u89e3\u51b3\u4e86\u4e0a\u4e00\u79cd\u65b9\u6848\u4e2d\u4f1a\u9762\u4e34\u7684\u4e24\u4e2a\u95ee\u9898\u3002 \u8fd9\u4e2a\u65b9\u6848\u7684\u4e3b\u8981\u7f3a\u70b9\u5728\u4e8e\uff0c\u8fd9\u91cc\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\uff08\u7740\u65e0\u7591\u589e\u52a0\u4e86\u590d\u6742\u6027\uff09\u6765\u5b58\u50a8\u4e00\u4e2a\u4e00\u6b21\u6027\u7684\u503c\u3002\u4e00\u5b9a\u8fd8\u6709\u66f4\u597d\u7684\u65b9\u6cd5\u3002","title":"\u8fd8\u4e0d\u9519\u7684\u89e3\u51b3\u65b9\u6848"},{"location":"1-11-Developing-your-first-program/#_3","text":"1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // preferred version int main () { std :: cout << \"Enter an integer: \" ; int num { }; std :: cin >> num ; std :: cout << \"Double that number is: \" << num * 2 << '\\n' ; // use an expression to multiply num * 2 at the point where we are going to print it return 0 ; } \u8fd9\u79cd\u65b9\u6cd5\u662f\u8fd9\u4e00\u7cfb\u5217\u89e3\u51b3\u65b9\u6848\u4e2d\u6700\u63a8\u8350\u7684\u3002\u5f53 std:: cout \u6267\u884c\u7684\u65f6\u5019\uff0c\u8868\u8fbe\u5f0f num * 2 \u4f1a\u8fdb\u884c\u6c42\u503c\uff0c\u5176\u7ed3\u679c\u5c31\u662f\u4e24\u500d\u7684 num \u503c\u3002\u8be5\u503c\u4f1a\u88ab\u6253\u5370\u5904\u7406\uff0c\u540c\u65f6\u53d8\u91cf num \u7684\u503c\u5e76\u6ca1\u6709\u88ab\u4fee\u6539\uff0c\u65b9\u4fbf\u6211\u4eec\u540e\u7eed\u7ee7\u7eed\u4f7f\u7528\u5b83\u3002 \u8fd9\u4e2a\u7248\u672c\u662f\u6211\u4eec\u63d0\u4f9b\u7684\u53c2\u8003\u7b54\u6848\u3002 \u4f5c\u8005\u6ce8 \u7f16\u7a0b\u7684\u9996\u8981\u76ee\u6807\uff0c\u662f\u8ba9\u7a0b\u5e8f\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u3002\u4e0d\u80fd\u5de5\u4f5c\u7684\u7a0b\u5e8f\u4e0d\u8bba\u5199\u7684\u591a\u597d s\uff0c\u90fd\u662f\u6ca1\u6709\u610f\u4e49\u7684\u3002 \u7136\u800c\uff0c\u6211\u5f88\u559c\u6b22\u7684\u4e00\u53e5\u540d\u8a00\u662f\u8fd9\u4e48\u8bf4\u7684\uff1a\u4f60\u5fc5\u987b\u5199\u8fc7\u4e00\u904d\u4ee3\u7801\u4e4b\u540e\uff0c\u624d\u80fd\u77e5\u9053\u4f60\u6700\u521d\u5e94\u8be5\u5982\u4f55\u53bb\u5199\u5b83\u3002\u8fd9\u53e5\u8bdd\u9053\u51fa\u4e86\u8fd9\u6837\u4e00\u4e2a\u4e8b\u5b9e\uff0c\u6700\u4f73\u7684\u89e3\u51b3\u65b9\u6848\u5f80\u5f80\u4e0d\u660e\u663e\u7684\uff0c\u800c\u6700\u5148\u60f3\u5230\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5f80\u5f80\u53c8\u4e0d\u662f\u6700\u4f73\u7684\u3002 \u5728\u6211\u4eec\u4e13\u6ce8\u4e8e\u5982\u4f55\u89e3\u51b3\u95ee\u9898\u5e76\u4f7f\u5f97\u7a0b\u5e8f\u8fd0\u884c\u8d77\u6765\u7684\u65f6\u5019\uff0c\u82b1\u5927\u91cf\u7684\u65f6\u95f4\u53d6\u7422\u78e8\u8fd9\u4e9b\u6211\u4eec\u751a\u81f3\u90fd\u4e0d\u77e5\u9053\u662f\u5426\u6700\u7ec8\u4f1a\u88ab\u4f7f\u7528\u7684\u4ee3\u7801\uff0c\u662f\u4e0d\u5212\u7b97\u3002\u56e0\u6b64\u6211\u4eec\u9700\u8981\u53e6\u8f9f\u8e4a\u5f84\uff0c\u6682\u65f6\u8df3\u8fc7\u5f02\u5e38\u5904\u7406\u548c\u6ce8\u91ca\uff0c\u4ee3\u7801\u4e2d\u5145\u65a5\u7740\u5927\u91cf\u7684\u5e2e\u52a9\u5b9a\u4f4d\u95ee\u9898\u7684\u8c03\u8bd5\u4ee3\u7801\u4e5f\u65e0\u6240\u8c13\uff0c\u6211\u4eec\u8981\u5b66\u4f1a\u6478\u7740\u77f3\u5934\u8fc7\u6cb3\uff0c\u800c\u5f53\u4e00\u4e2a\u65b9\u6cd5\u5f7b\u5e95\u884c\u4e0d\u901a\u7684\u65f6\u5019\uff0c\u5c31\u6362\u4e00\u79cd\u65b9\u6cd5\u4ece\u65b0\u5f00\u59cb\u3002 \u8fd9\u4e48\u505a\u7684\u7ed3\u679c\u5c31\u662f\uff0c\u6700\u521d\u7248\u672c\u7684\u4ee3\u7801\u5f80\u5f80\u7f3a\u4e4f\u6761\u7406\u3001\u5065\u58ee\u6027\u4e5f\u4e0d\u4f73\u3001\u53ef\u8bfb\u6027\u4e0d\u597d\uff0c\u5c31\u66f4\u522b\u63d0\u4ec0\u4e48\u7b80\u4ecb\u660e\u4e86\u4e86\u3002\u56e0\u6b64\uff0c\u5f53\u4f60\u7684\u4ee3\u7801\u53ef\u4ee5\u5de5\u4f5c\u4e4b\u540e\uff0c\u4f60\u7684\u5de5\u4f5c\u8fd8\u6ca1\u6709\u5b8c\u6210\uff08\u51fa\u53d1\u4f60\u7f16\u5199\u7684\u4ee3\u7801\u662f\u4e00\u6b21\u6027\u7684\uff09\u3002\u63a5\u4e0b\u6765\uff0c\u4f60\u9700\u8981\u6e05\u7406\u4f60\u7684\u4ee3\u7801\uff0c\u5305\u62ec\u5220\u9664\uff08\u6216\u6ce8\u91ca\u6389\uff09\u5e2e\u52a9\u8c03\u8bd5\u7684\u4e34\u65f6\u6027\u4ee3\u7801\u3001\u7f16\u5199\u6ce8\u91ca\u3001\u5904\u7406\u53ef\u80fd\u53d1\u751f\u7684\u9519\u8bef\u3001\u7f8e\u5316\u4ee3\u7801\u7684\u6392\u7248\u5e76\u786e\u4fdd\u4ee3\u7801\u9075\u5faa\u4e86\u76f8\u5173\u7684\u6700\u4f73\u5b9e\u8df5\u3002\u6b64\u5916\uff0c\u4f60\u7684\u4ee3\u7801\u53ef\u80fd\u5e76\u4e0d\u662f\u6700\u7cbe\u7b80\u7684\uff0c\u4e5f\u8bb8\u91cc\u9762\u8fd8\u5b58\u5728\u7740\u53ef\u4ee5\u88ab\u5408\u5e76\u7684\u5197\u4f59\u903b\u8f91\uff0c\u6216\u8005\u5b58\u5728\u7740\u53ef\u4ee5\u88ab\u5408\u5e76\u7684\u8bed\u53e5\uff0c\u4ea6\u6216\u662f\u6709\u4e9b\u53d8\u91cf\u662f\u6ca1\u6709\u5fc5\u8981\u7684\uff0c\u4ee5\u53ca\u5176\u4ed6\u5404\u79cd\u53ef\u4ee5\u88ab\u7b80\u5316\u7684\u5e38\u89c1\u3002\u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u90fd\u8feb\u5207\u7684\u60f3\u8981\u53bb\u4f18\u5316\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u800c\u5b9e\u9645\u4e0a\u7a0b\u5e8f\u7684\u53ef\u7ef4\u62a4\u6027\u624d\u662f\u5b83\u4eec\u6700\u5e94\u8be5\u4f18\u5148\u8003\u8651\u7684\u4e8b\u60c5\u3002 \u672c\u6559\u7a0b\u63d0\u4f9b\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5f88\u5c11\u662f\u7b2c\u4e00\u6b21\u5c31\u5f97\u5230\u6700\u4f73\u89e3\u51b3\u65b9\u6848\u7684\u3002\u5b9e\u9645\u4e0a\uff0c\u5b83\u4eec\u90fd\u7ecf\u8fc7\u4e86\u4e0d\u65ad\u7684\u4fee\u6539\uff0c\u76f4\u5230\u6211\u4eec\u8ba4\u4e3a\u6ca1\u6709\u4ec0\u4e48\u53ef\u4ee5\u518d\u4f18\u5316\u7684\u4e86\u3002\u5373\u4f7f\u662f\u8fd9\u6837\uff0c\u5f88\u591a\u65f6\u5019\u8bfb\u8005\u8fd8\u662f\u80fd\u591f\u5411\u6211\u4eec\u63d0\u51fa\u5f88\u591a\u4f18\u5316\u7684\u5efa\u8bae\uff01 \u8bf4\u8fd9\u4e48\u591a\uff0c\u6211\u60f3\u8868\u8fbe\u7684\u662f\uff1a\u5982\u679c\u4f60\u6ca1\u6709\u4e00\u6b21\u6027\u60f3\u5230\u4e00\u4e2a\u6700\u4f18\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u8bf7\u4e0d\u8981\u4e3a\u6b64\u611f\u5230\u6cae\u4e27\uff0c\u8fd9\u5f88\u6b63\u5e38\u3002\u7cbe\u76ca\u6c42\u7cbe\u5728\u7f16\u7a0b\u4e2d\u662f\u4e00\u4e2a\u8fed\u4ee3\u7684\u8fc7\u7a0b\u3002 \u4f5c\u8005\u6ce8 \u8fd8\u6709\u4e00\u4ef6\u4e8b\uff1a\u4f60\u53ef\u80fd\u4f1a\u60f3\uff0c\u201dC++\u4e2d\u6709\u8fd9\u4e48\u591a\u7684\u89c4\u5219\u548c\u6982\u5ff5\uff0c\u6211\u600e\u4e48\u624d\u80fd\u8bb0\u4f4f\u5b83\u4eec\u5462\uff1f\u201c \u7b80\u5355\u6765\u8bf4\uff0c\u4f60\u5e76\u4e0d\u9700\u8981\u8bb0\u4f4f\u5b83\u4eec\u3002\u4f7f\u7528 C++\uff0c\u4e00\u65b9\u9762\u5728\u4e8e\u4f7f\u7528\u4f60\u5df2\u7ecf\u638c\u63e1\u7684\u77e5\u8bc6\uff0c\u53e6\u4e00\u65b9\u9762\u5728\u4e8e\u638c\u63e1\u5982\u4f55\u67e5\u627e\u5e76\u4f7f\u7528\u4f60\u6ca1\u6709\u638c\u63e1\u7684\u77e5\u8bc6\u3002 \u5f53\u4f60\u7b2c\u4e00\u6b21\u9605\u8bfb\u672c\u6559\u7a0b\u7684\u65f6\u5019\uff0c\u4e0d\u8981\u628a\u6ce8\u610f\u529b\u653e\u5728\u8bb0\u5fc6\u89c4\u5219\u4e0a\uff0c\u800c\u662f\u8981\u91cd\u70b9\u7406\u89e3\u4f7f\u7528C++\u4f60\u80fd\u505a\u54ea\u4e9b\u4e8b\u3002\u7136\u540e\uff0c\u5f53\u4f60\u771f\u6b63\u9700\u8981\u5728\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u67d0\u4e2a\u529f\u80fd\u7684\u65f6\u5019\uff0c\u4f60\u53ef\u4ee5\u518d\u56de\u6765\uff08\u6216\u8005\u5230\u5176\u4ed6\u7f51\u7ad9\uff09\u53bb\u5237\u65b0\u5de9\u56fa\u4f60\u7684\u77e5\u8bc6\u5e76\u638c\u63e1\u5e94\u8be5\u5982\u4f55\u53bb\u505a\u3002","title":"\u6700\u4f73\u65b9\u6848"},{"location":"1-2-comments/","tags":["comments"],"text":"Key Takeaway \u5728\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u7ea7\u522b\uff0c\u4f7f\u7528\u6ce8\u91ca\u6765\u63cf\u8ff0\u201c\u662f\u4ec0\u4e48\u201d\u3002 \u5728\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u5185\u90e8\uff0c\u4f7f\u7528\u6ce8\u91ca\u6765\u63cf\u8ff0\u201c\u600e\u4e48\u505a\u201d\u3002 \u5728\u8bed\u53e5\u7ea7\u522b\uff0c\u4f7f\u7528\u6ce8\u91ca\u6765\u63cf\u8ff0\u201c\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u505a\u201d\u3002 \u6ce8\u91ca\u662f\u76f4\u63a5\u63d2\u5165\u5230\u7a0b\u5e8f\u6e90\u4ee3\u7801\uff0c\u4f9b\u7a0b\u5e8f\u5458\u9605\u8bfb\u7684\u7b14\u8bb0\u3002\u6ce8\u91ca\u88ab\u7f16\u8bd1\u5668\u5ffd\u7565\uff0c\u4ec5\u4f9b\u7a0b\u5e8f\u5458\u4f7f\u7528\u3002 \u5728C++\u4e2d\u6709\u4e24\u79cd\u4e0d\u540c\u98ce\u683c\u7684\u6ce8\u91ca\uff0c\u5b83\u4eec\u90fd\u6709\u76f8\u540c\u7684\u76ee\u7684\uff1a\u5e2e\u52a9\u7a0b\u5e8f\u5458\u4ee5\u67d0\u79cd\u65b9\u5f0f\u8bb0\u5f55\u4ee3\u7801\u3002 \u5355\u884c\u6ce8\u91ca \u4f7f\u7528 // \u5f00\u5934\u53ef\u4ee5\u521b\u5efa\u5355\u884c\u6ce8\u91ca\uff0c\u8be5\u7b26\u53f7\u544a\u8bc9\u7f16\u8bd1\u5668\u5ffd\u7565\u8be5\u7b26\u53f7\u4e4b\u540e\u5230\u8be5\u884c\u672b\u5c3e\u7684\u5168\u90e8\u5185\u5bb9\u3002\u4f8b\u5982\uff1a 1 std :: cout << \"Hello world!\" ; // Everything from here to the end of the line is ignored \u4e00\u822c\u6765\u8bb2\uff0c\u5355\u884c\u6ce8\u91ca\u7528\u4e8e\u5bf9\u67d0\u884c\u4ee3\u7801\u8fdb\u884c\u5feb\u901f\u6ce8\u91ca\u3002 1 2 3 std :: cout << \"Hello world! \\n \" ; // std::cout lives in the iostream library std :: cout << \"It is very nice to meet you! \\n \" ; // these comments make the code hard to read std :: cout << \"Yeah! \\n \" ; // especially when lines are different lengths \u5c06\u6ce8\u91ca\u5199\u5728\u4ee3\u7801\u884c\u7684\u53f3\u4fa7\uff0c\u4f1a\u8ba9\u4ee3\u7801\u548c\u6ce8\u91ca\u7684\u53ef\u8bfb\u6027\u53d8\u5dee\uff0c\u5c24\u5176\u662f\u8fd9\u884c\u6bd4\u8f83\u957f\u7684\u60c5\u51b5\u3002\u5982\u679c\u8be5\u884c\u4e0d\u957f\uff0c\u6ce8\u91ca\u5c31\u653e\u5728\u540e\u9762\u5427\uff0c\u901a\u5e38\u653e\u7f6e\u5728\u4e0b\u4e00\u4e2a\u5236\u8868\u7b26\u7684\u4f4d\u7f6e\uff0c\u50cf\u8fd9\u6837\uff1a 1 2 3 std :: cout << \"Hello world! \\n \" ; // std::cout lives in the iostream library std :: cout << \"It is very nice to meet you! \\n \" ; // this is much easier to read std :: cout << \"Yeah! \\n \" ; // don't you think so? \u4f46\u662f\uff0c\u5982\u679c\u884c\u5f88\u957f\uff0c\u5c06\u6ce8\u91ca\u653e\u5728\u53f3\u8fb9\u4f1a\u4f7f\u8fd9\u4e00\u884c\u53d8\u5f97\u7279\u522b\u957f\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5355\u884c\u6ce8\u91ca\u901a\u5e38\u653e\u5728\u5b83\u6240\u6ce8\u91ca\u7684\u884c\u4e4b\u4e0a\uff1a 1 2 3 4 5 6 7 8 // std::cout lives in the iostream library std :: cout << \"Hello world! \\n \" ; // this is much easier to read std :: cout << \"It is very nice to meet you! \\n \" ; // don't you think so? std :: cout << \"Yeah! \\n \" ; \u4f5c\u8005\u6ce8 \u4e0a\u9762\u7684\u8bed\u53e5\u662f\u6211\u4eec\u7b2c\u4e00\u6b21\u9047\u5230\u201c\u4ee3\u7801\u7247\u6bb5\u201d\u3002\u201c\u4ee3\u7801\u7247\u6bb5\u201d\u4e0d\u662f\u5b8c\u6574\u7684\u7a0b\u5e8f\uff0c\u6240\u4ee5\u5b83\u4eec\u4e0d\u80fd\u88ab\u7f16\u8bd1\u3002\u5b83\u4eec\u7684\u5b58\u5728\u662f\u4e3a\u4e86\u4ee5\u7b80\u6d01\u7684\u65b9\u5f0f\u6f14\u793a\u7279\u5b9a\u7684\u6982\u5ff5\u3002 \u5982\u679c\u4f60\u60f3\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\u7247\u6bb5\uff0c\u5219\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5b8c\u6574\u7684\u7a0b\u5e8f\uff0c\u4ee5\u4fbf\u7f16\u8bd1\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u8be5\u7a0b\u5e8f\u770b\u8d77\u6765\u50cf\u8fd9\u6837: 1 2 3 4 5 6 7 8 #include int main () { // Replace this line with the snippet of code you'd like to compile return 0 ; } \u591a\u884c\u6ce8\u91ca /* \u548c */ \u7b26\u53f7\u5bf9\u8868\u793aC\u98ce\u683c\u7684\u591a\u884c\u6ce8\u91ca\u3002\u5728\u8fd9\u4e24\u4e2a\u7b26\u53f7\u4e4b\u95f4\u7684\u4efb\u610f\u5185\u5bb9\u90fd\u4f1a\u88ab\u7f16\u8bd1\u5668\u5ffd\u7565 1 2 3 /* This is a multi-line comment. This line will be ignored. So will this one. */ \u56e0\u4e3a\u7b26\u53f7\u4e4b\u95f4\u7684\u6240\u6709\u5185\u5bb9\u90fd\u88ab\u5ffd\u7565\u4e86\uff0c\u4f60\u6709\u65f6\u4f1a\u770b\u5230\u7a0b\u5e8f\u5458\u201c\u7f8e\u5316\u201d\u4ed6\u4eec\u7684\u591a\u884c\u6ce8\u91ca\uff1a 1 2 3 4 /* This is a multi-line comment. * the matching asterisks to the left * can make this easier to read */ \u591a\u884c\u6837\u5f0f\u7684\u6ce8\u91ca\u4e0d\u80fd\u5d4c\u5957\u3002\u56e0\u6b64\uff0c\u4ee5\u4e0b\u60c5\u51b5\u5c06\u4ea7\u751f\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\uff1a 1 2 /* This is a multi-line /* comment */ this is not inside the comment */ // The above comment ends at the first */, not the second */ \u5f53\u7f16\u8bd1\u5668\u5c1d\u8bd5\u7f16\u8bd1\u5b83\u65f6\uff0c\u5b83\u5c06\u5ffd\u7565\u4ece\u7b2c\u4e00\u4e2a /* \u5230\u7b2c\u4e00\u4e2a */ \u4e2d\u7684\u6240\u6709\u5185\u5bb9\u3002\u56e0\u4e3a\u201c this is not inside the comment */ \u201d\u4e0d\u88ab\u8ba4\u4e3a\u662f\u6ce8\u91ca\u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u7f16\u8bd1\u5b83\u3002\u8fd9\u5c06\u4e0d\u53ef\u907f\u514d\u5730\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8bed\u6cd5\u9ad8\u4eae\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a\u6ce8\u91ca\u5177\u6709\u7279\u5b9a\u7684\u989c\u8272\uff0c\u6240\u4ee5\u4e0d\u5c5e\u4e8e\u6ce8\u91ca\u7684\u4e00\u90e8\u5206\u5f88\u5bb9\u6613\u88ab\u770b\u51fa\u6765\u3002 \u6ce8\u610f \u4e0d\u8981\u5728\u5176\u4ed6\u591a\u884c\u6ce8\u91ca\u4e2d\u4f7f\u7528\u591a\u884c\u6ce8\u91ca\u3002\u5c06\u5355\u884c\u6ce8\u91ca\u5c01\u88c5\u5728\u591a\u884c\u6ce8\u91ca\u4e2d\u662f\u53ef\u4ee5\u7684\u3002 \u6b63\u786e\u5730\u4f7f\u7528\u6ce8\u91ca \u901a\u5e38\uff0c\u6ce8\u91ca\u5e94\u8be5\u7528\u4e8e\u5b8c\u6210\u4e09\u4ef6\u4e8b\u3002\u7b2c\u4e00\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u7ed9\u5b9a\u7684\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\uff0c\u6ce8\u91ca\u6700\u597d\u7528\u6765\u63cf\u8ff0\u8be5\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u7684\u529f\u80fd\u3002\u5b83\u4eec\u901a\u5e38\u653e\u5728\u6587\u4ef6\u6216\u5e93\u7684\u9876\u90e8\uff0c\u6216\u7d27\u6328\u7740\u51fd\u6570\u7684\u524d\u9762\u3002\u4f8b\u5982: 1 // This program calculates the student's final grade based on his test and homework scores. 1 // This function uses Newton's method to approximate the root of a given equation. 1 // The following lines generate a random item based on rarity, level, and a weight factor. \u6240\u6709\u8fd9\u4e9b\u6ce8\u91ca\u90fd\u53ef\u4ee5\u8ba9\u8bfb\u8005\u5728\u4e0d\u67e5\u770b\u5b9e\u9645\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u5f88\u597d\u5730\u4e86\u89e3\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u5b9e\u73b0\u4e86\u4ec0\u4e48\u529f\u80fd\u3002\u7528\u6237(\u53ef\u80fd\u662f\u5176\u4ed6\u4eba\uff0c\u5982\u679c\u4f60\u8bd5\u56fe\u91cd\u7528\u4ee5\u524d\u7f16\u5199\u7684\u4ee3\u7801\uff0c\u4e5f\u53ef\u80fd\u662f\u4f60\u81ea\u5df1)\u4e00\u773c\u5c31\u80fd\u770b\u51fa\u4ee3\u7801\u662f\u5426\u4e0e\u4ed6\u6216\u5979\u60f3\u8981\u5b9e\u73b0\u7684\u76ee\u6807\u76f8\u5173\u3002\u5728\u56e2\u961f\u4e2d\u5de5\u4f5c\u65f6\uff0c\u8fd9\u4e00\u70b9\u5c24\u5176\u91cd\u8981\uff0c\u56e0\u4e3a\u4e0d\u662f\u6bcf\u4e2a\u4eba\u90fd\u719f\u6089\u6240\u6709\u7684\u4ee3\u7801\u3002 \u7b2c\u4e8c\uff0c\u5728\u4e0a\u9762\u63cf\u8ff0\u7684\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u4e2d\uff0c\u6ce8\u91ca\u53ef\u4ee5\u7528\u6765\u63cf\u8ff0\u4ee3\u7801\u5c06\u5982\u4f55\u5b9e\u73b0\u5b83\u7684\u76ee\u6807\u3002 1 2 3 /* To calculate the final grade, we sum all the weighted midterm and homework scores and then divide by the number of scores to assign a percentage, which is used to calculate a letter grade. */ 1 2 3 4 5 6 // To generate a random item, we're going to do the following: // 1) Put all of the items of the desired rarity on a list // 2) Calculate a probability for each item based on level and weight factor // 3) Choose a random number // 4) Figure out which item that random number corresponds to // 5) Return the appropriate item \u8fd9\u4e9b\u6ce8\u91ca\u8ba9\u7528\u6237\u4e86\u89e3\u4ee3\u7801\u5c06\u5982\u4f55\u5b9e\u73b0\u5176\u76ee\u6807\uff0c\u800c\u4e0d\u5fc5\u4e86\u89e3\u6bcf\u4e00\u884c\u4ee3\u7801\u7684\u529f\u80fd\u3002 \u7b2c\u4e09\uff0c\u5728\u8bed\u53e5\u7ea7\u522b\uff0c\u6ce8\u91ca\u5e94\u8be5\u88ab\u7528\u6765\u63cf\u8ff0\u4ee3\u7801 \u4e3a\u4ec0\u4e48 \u505a\u67d0\u4e8b\uff0c\u800c\u4e0d\u662f\u88ab\u7528\u6765\u89e3\u91ca\u8be5\u884c\u4ee3\u7801\u662f\u5728 \u505a\u4ec0\u4e48\u4e8b \u3002\u5982\u679c\u4f60\u7f16\u5199\u7684\u4ee3\u7801\u975e\u5e38\u590d\u6742\uff0c\u4ee5\u81f3\u4e8e\u9700\u8981\u6ce8\u91ca\u6765\u89e3\u91ca\u8bed\u53e5\u5728\u505a\u4ec0\u4e48\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u91cd\u5199\u8bed\u53e5\uff0c\u800c\u4e0d\u662f\u4e3a\u5176\u6dfb\u52a0\u6ce8\u91ca\u3002 \u4e0b\u9762\u662f\u4e00\u4e9b\u5177\u4f53\u7684\u4f8b\u5b50\uff08\u6709\u597d\u6709\u574f\uff09\uff1a \u53cd\u9762\u6559\u6750\uff1a 1 2 // Set sight range to 0 sight = 0 ; \u7406\u7531\uff1a\u770b\u8bed\u53e5\u5c31\u77e5\u9053\u5b83\u662f\u5728\u7ed9 sight \u8d4b\u503c\uff0c\u65e0\u9700\u6ce8\u91ca\u3002 \u6b63\u786e\u793a\u4f8b\uff1a 1 2 // The player just drank a potion of blindness and can not see anything sight = 0 ; \u7406\u7531\uff1a\u8bf4\u660e\u4e3a\u4ec0\u4e48\u8981\u628a player \u7684 sight \u8bbe\u7f6e\u4e3a0\u3002 \u53cd\u9762\u6559\u6750\uff1a 1 2 // Calculate the cost of the items cost = quantity * 2 * storePrice ; \u7406\u7531\uff1a\u5f88\u663e\u7136\u662f\u5728\u8ba1\u7b97 cost \uff0c\u4f46\u662f\u6ca1\u6709\u89e3\u91ca\u4e3a\u4ec0\u4e48\u8981\u4e58\u4ee52\uff1f \u6b63\u786e\u793a\u4f8b\uff1a 1 2 // \u6211\u4eec\u9700\u8981\u5c06\u6570\u91cf\u4e58\u4ee52\u56e0\u4e3a\u5b83\u4eec\u662f\u6210\u5bf9\u8d2d\u4e70\u7684 cost = quantity * 2 * storePrice ; \u7406\u7531\uff1a\u8fd9\u6837\u5c31\u80fd\u7406\u89e3\u516c\u5f0f\u7684\u542b\u4e49\u4e86\u3002 \u7a0b\u5e8f\u5458\u5e38\u5e38\u4e0d\u5f97\u4e0d\u5728\u7528\u4e00\u79cd\u65b9\u6cd5\u89e3\u51b3\u95ee\u9898\uff0c\u8fd8\u662f\u7528\u53e6\u4e00\u79cd\u65b9\u6cd5\u89e3\u51b3\u95ee\u9898\u4e4b\u95f4\u505a\u51fa\u8270\u96be\u7684\u51b3\u5b9a\u3002\u6ce8\u91ca\u662f\u63d0\u9192\u4f60\u81ea\u5df1(\u6216\u544a\u8bc9\u522b\u4eba)\u505a\u51fa\u8fd9\u4e2a\u51b3\u5b9a\u800c\u4e0d\u662f\u53e6\u4e00\u4e2a\u51b3\u5b9a\u7684\u539f\u56e0\u7684\u597d\u65b9\u6cd5\u3002 \u6b63\u786e\u793a\u4f8b\uff1a 1 2 // We decided to use a linked list instead of an array because // arrays do insertion too slowly. 1 2 // We're going to use Newton's method to find the root of a number because // there is no deterministic way to solve these equations. \u6700\u540e\uff0c\u5e94\u8be5\u4ee5\u4e00\u79cd\u5bf9\u4e0d\u77e5\u9053\u4ee3\u7801\u662f\u505a\u4ec0\u4e48\u7684\u4eba\u6765\u8bf4\u6709\u610f\u4e49\u7684\u65b9\u5f0f\u7f16\u5199\u6ce8\u91ca\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u5458\u4f1a\u8bf4:\u201c\u8fd9\u5f88\u660e\u663e\uff01\u6211\u4e0d\u4f1a\u5fd8\u8bb0\u8fd9\u4ef6\u4e8b\u7684\u3002\u201d\u4f60\u731c\u600e\u4e48\u7740\uff1f\u8fd9\u5e76\u4e0d\u660e\u663e\uff0c\u4f60\u4f1a\u60ca\u8bb6\u4e8e\u81ea\u5df1\u6709\u591a\u5065\u5fd8\u3002\u4f60(\u6216\u5176\u4ed6\u4eba)\u4ee5\u540e\u4f1a\u611f\u8c22\u4f60\u7528\u4eba\u7c7b\u8bed\u8a00\u5199\u4e0b\u4ee3\u7801\u7684\u5185\u5bb9\u3001\u65b9\u5f0f\u548c\u539f\u56e0\u3002\u9605\u8bfb\u4e00\u884c\u884c\u4ee3\u7801\u5f88\u5bb9\u6613\uff0c\u4f46\u7406\u89e3\u5176\u8981\u5b8c\u6210\u7684\u76ee\u6807\u5374\u4e0d\u7b80\u5355\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u4f60\u7684\u4ee3\u7801\u505a\u5927\u91cf\u7684\u6ce8\u91ca\uff0c\u5199\u6ce8\u91ca\u65f6\u5c31\u50cf\u5bf9\u4e00\u4e2a\u4e0d\u77e5\u9053\u4ee3\u7801\u662f\u505a\u4ec0\u4e48\u7684\u4eba\u8bf4\u8bdd\u4e00\u6837\u3002\u4e0d\u8981\u4ee5\u4e3a\u4f60\u4f1a\u8bb0\u5f97\u4e3a\u4ec0\u4e48\u4f60\u505a\u4e86\u7279\u5b9a\u7684\u9009\u62e9\u3002 \u4f5c\u8005\u6ce8 \u5728\u672c\u7cfb\u5217\u6559\u7a0b\u7684\u5176\u4f59\u90e8\u5206\u4e2d\uff0c\u6211\u4eec\u5c06\u5728\u4ee3\u7801\u5757\u4e2d\u4f7f\u7528\u6ce8\u91ca\u6765\u8ba9\u4f60\u5173\u6ce8\u7279\u5b9a\u7684\u5185\u5bb9\uff0c\u6216\u8005\u5e2e\u52a9\u8bf4\u660e\u4ee3\u7801\u662f\u5982\u4f55\u5de5\u4f5c\u7684(\u540c\u65f6\u786e\u4fdd\u7a0b\u5e8f\u80fd\u591f\u7f16\u8bd1)\u3002\u7cbe\u660e\u7684\u8bfb\u8005\u4f1a\u6ce8\u610f\u5230\uff0c\u6309\u7167\u4e0a\u8ff0\u6807\u51c6\uff0c\u5927\u591a\u6570\u8bc4\u8bba\u90fd\u5f88\u7cdf\u7cd5\u3002:)\u5f53\u4f60\u9605\u8bfb\u5269\u4e0b\u7684\u6559\u7a0b\u65f6\uff0c\u8bf7\u8bb0\u4f4f\uff0c\u8fd9\u4e9b\u6ce8\u91ca\u662f\u4e3a\u6559\u80b2\u76ee\u7684\u670d\u52a1\u7684\uff0c\u800c\u4e0d\u662f\u8bd5\u56fe\u6f14\u793a\u597d\u7684\u6ce8\u91ca\u662f\u4ec0\u4e48\u6837\u5b50\u7684\u3002 \u5c06\u4ee3\u7801\u6ce8\u91ca\u6389 \u5c06\u4e00\u884c\u6216\u591a\u884c\u4ee3\u7801\u8f6c\u6362\u4e3a\u6ce8\u91ca\u79f0\u4e3a\u6ce8\u91ca\u6389\u4ee3\u7801\u3002\u8fd9\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u7684\u65b9\u6cd5(\u4e34\u65f6)\u5c06\u90e8\u5206\u4ee3\u7801\u6392\u9664\u5728\u7f16\u8bd1\u540e\u7684\u7a0b\u5e8f\u4e2d\u3002 \u8981\u6ce8\u91ca\u6389\u4e00\u884c\u4ee3\u7801\uff0c\u53ea\u9700\u4f7f\u7528 // \u6837\u5f0f\u6ce8\u91ca\u5c06\u4e00\u884c\u4ee3\u7801\u4e34\u65f6\u53d8\u6210\u6ce8\u91ca: \u672a\u6ce8\u91ca\u6389\uff1a 1 std :: cout << 1 ; \u6ce8\u91ca\u6389\uff1a 1 // std::cout << 1; \u8981\u6ce8\u91ca\u6389\u4ee3\u7801\u5757\uff0c\u53ef\u4ee5\u5728\u591a\u884c\u4ee3\u7801\u4e0a\u4f7f\u7528 // \uff0c\u6216\u8005\u4f7f\u7528 /* */ \u6ce8\u91ca\u5c06\u4ee3\u7801\u5757\u4e34\u65f6\u8f6c\u6362\u4e3a\u6ce8\u91ca\u3002 \u672a\u6ce8\u91ca\u6389\uff1a 1 2 3 std :: cout << 1 ; std :: cout << 2 ; std :: cout << 3 ; \u6ce8\u91ca\u6389\uff1a 1 2 3 // std::cout << 1; // std::cout << 2; // std::cout << 3; \u6216 1 2 3 4 5 /* std::cout << 1; std::cout << 2; std::cout << 3; */ \u4f60\u9700\u8981\u8fd9\u4e48\u505a\u7684\u539f\u56e0\u6709\u5f88\u591a\uff1a \u4f60\u6b63\u5728\u5904\u7406\u4e00\u6bb5\u5c1a\u672a\u7f16\u8bd1\u7684\u65b0\u4ee3\u7801\uff0c\u9700\u8981\u8fd0\u884c\u8be5\u7a0b\u5e8f\u3002\u5982\u679c\u6709\u7f16\u8bd1\u5668\u9519\u8bef\uff0c\u7f16\u8bd1\u5668\u5c06\u4e0d\u5141\u8bb8\u7f16\u8bd1\u4ee3\u7801\u3002\u6ce8\u91ca\u6389\u4e0d\u80fd\u7f16\u8bd1\u7684\u4ee3\u7801\u5c06\u5141\u8bb8\u7a0b\u5e8f\u7f16\u8bd1\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u518d\u6b21\u8fd0\u884c\u3002\u51c6\u5907\u597d\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u53d6\u6d88\u5bf9\u4ee3\u7801\u7684\u6ce8\u91ca\uff0c\u5e76\u7ee7\u7eed\u5904\u7406\u5b83\u3002 \u4f60\u5df2\u7ecf\u7f16\u5199\u4e86\u53ef\u7f16\u8bd1\u4f46\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u7684\u65b0\u4ee3\u7801\uff0c\u5e76\u4e14\u76f4\u5230\u4ee5\u540e\u624d\u6709\u65f6\u95f4\u4fee\u590d\u5b83\u3002\u6ce8\u91ca\u6389\u635f\u574f\u7684\u4ee3\u7801\u5c06\u786e\u4fdd\u635f\u574f\u7684\u4ee3\u7801\u4e0d\u4f1a\u6267\u884c\u5e76\u5bfc\u81f4\u95ee\u9898\uff0c\u76f4\u5230\u4f60\u80fd\u591f\u4fee\u590d\u5b83\u3002 \u627e\u5230\u9519\u8bef\u7684\u6765\u6e90\u3002\u5982\u679c\u7a0b\u5e8f\u6ca1\u6709\u4ea7\u751f\u9884\u671f\u7684\u7ed3\u679c(\u6216\u5d29\u6e83)\uff0c\u6709\u65f6\u53ef\u4ee5\u7981\u7528\u90e8\u5206\u4ee3\u7801\uff0c\u770b\u770b\u662f\u5426\u53ef\u4ee5\u5206\u79bb\u51fa\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\u7684\u539f\u56e0\u3002\u5982\u679c\u6ce8\u91ca\u6389\u4e86\u4e00\u884c\u6216\u591a\u884c\u4ee3\u7801\u540e\u7a0b\u5e8f\u5f00\u59cb\u6309\u9884\u671f\u8fd0\u884c(\u6216\u505c\u6b62\u5d29\u6e83)\uff0c\u90a3\u4e48\u9519\u8bef\u53ef\u80fd\u4f4d\u4e8e\u6700\u540e\u6ce8\u91ca\u6389\u7684\u4ee3\u7801\u4e2d\u3002 \u4f60\u5e0c\u671b\u7528\u53e6\u4e00\u6bb5\u4ee3\u7801\u66ff\u6362\u4e00\u6bb5\u4ee3\u7801\uff0c\u4f46\u4f60\u6682\u65f6\u4e0d\u60f3\u5220\u9664\u539f\u4ee3\u7801\uff0c\u6b64\u65f6\u53ef\u4ee5\u5c06\u5176\u5148\u6ce8\u91ca\u6389\uff0c\u4fdd\u7559\u5728\u90a3\u91cc\u4ee5\u4f9b\u53c2\u8003\uff0c\u76f4\u5230\u65b0\u4ee3\u7801\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u4e3a\u6b62\u3002\u4e00\u65e6\u786e\u5b9a\u65b0\u4ee3\u7801\u6b63\u5e38\u5de5\u4f5c\uff0c\u5c31\u53ef\u4ee5\u5220\u9664\u6ce8\u91ca\u6389\u7684\u65e7\u4ee3\u7801\u3002\u5982\u679c\u4f60\u4e0d\u80fd\u8ba9\u4f60\u7684\u65b0\u4ee3\u7801\u5de5\u4f5c\uff0c\u4f60\u603b\u662f\u53ef\u4ee5\u5220\u9664\u65b0\u4ee3\u7801\u548c\u53d6\u6d88\u6ce8\u91ca\u65e7\u4ee3\u7801\u6062\u590d\u5230\u4f60\u4ee5\u524d\u7684\u4ee3\u7801\u3002 \u6ce8\u91ca\u6389\u4ee3\u7801\u662f\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u5e38\u89c1\u7684\u4e8b\u60c5\uff0c\u56e0\u6b64\u8bb8\u591aIDE\u90fd\u652f\u6301\u6ce8\u91ca\u6389\u9009\u4e2d\u7684\u4ee3\u7801\u90e8\u5206\u3002\u4f7f\u7528\u65b9\u5f0f\u56e0 IDE \u800c\u5f02\u3002 For Visual Studio users \u4f60\u53ef\u4ee5\u901a\u8fc7\u7f16\u8f91\u83dc\u5355>\u9ad8\u7ea7>\u6ce8\u91ca\u9009\u62e9(\u6216\u53d6\u6d88\u6ce8\u91ca\u9009\u62e9)\u6765\u6ce8\u91ca\u6216\u53d6\u6d88\u6ce8\u91ca\u9009\u62e9\u3002 For Code::Blocks users \u4f60\u53ef\u4ee5\u901a\u8fc7\u7f16\u8f91\u83dc\u5355>\u6ce8\u91ca\u6765\u6ce8\u91ca\u6216\u53d6\u6d88\u6ce8\u91ca\u9009\u62e9\u3002 \u5c0f\u8d34\u58eb \u5982\u679c\u4f60\u53ea\u4f7f\u7528\u5355\u884c\u6ce8\u91ca\uff0c\u90a3\u4e48\u4f60\u603b\u662f\u53ef\u4ee5\u4f7f\u7528\u591a\u884c\u6ce8\u91ca\u6765\u6ce8\u91ca\u6389\u4ee3\u7801\u800c\u4e0d\u4ea7\u751f\u51b2\u7a81\u3002\u53cd\u4e4b\u5219\u4f1a\u6bd4\u8f83\u9ebb\u70e6\u3002 \u5982\u679c\u4f60\u786e\u5b9e\u9700\u8981\u6ce8\u91ca\u6389\u4e00\u4e2a\u5305\u542b\u591a\u884c\u6ce8\u91ca\u7684\u4ee3\u7801\u5757\uff0c\u4f60\u4e5f\u53ef\u4ee5\u8003\u8651\u4f7f\u7528 # if 0 \u9884\u5904\u7406\u5668\u6307\u4ee4 2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb \u5c0f\u7ed3 \u5728\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u7ea7\u522b\uff0c\u4f7f\u7528\u6ce8\u91ca\u6765\u63cf\u8ff0\u201c\u662f\u4ec0\u4e48\u201d\u3002 \u5728\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u5185\u90e8\uff0c\u4f7f\u7528\u6ce8\u91ca\u6765\u63cf\u8ff0\u201c\u600e\u4e48\u505a\u201d\u3002 \u5728\u8bed\u53e5\u7ea7\u522b\uff0c\u4f7f\u7528\u6ce8\u91ca\u6765\u63cf\u8ff0\u201c\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u505a\u201d\u3002","title":"1.2 - \u6ce8\u91ca"},{"location":"1-2-comments/#_1","text":"\u4f7f\u7528 // \u5f00\u5934\u53ef\u4ee5\u521b\u5efa\u5355\u884c\u6ce8\u91ca\uff0c\u8be5\u7b26\u53f7\u544a\u8bc9\u7f16\u8bd1\u5668\u5ffd\u7565\u8be5\u7b26\u53f7\u4e4b\u540e\u5230\u8be5\u884c\u672b\u5c3e\u7684\u5168\u90e8\u5185\u5bb9\u3002\u4f8b\u5982\uff1a 1 std :: cout << \"Hello world!\" ; // Everything from here to the end of the line is ignored \u4e00\u822c\u6765\u8bb2\uff0c\u5355\u884c\u6ce8\u91ca\u7528\u4e8e\u5bf9\u67d0\u884c\u4ee3\u7801\u8fdb\u884c\u5feb\u901f\u6ce8\u91ca\u3002 1 2 3 std :: cout << \"Hello world! \\n \" ; // std::cout lives in the iostream library std :: cout << \"It is very nice to meet you! \\n \" ; // these comments make the code hard to read std :: cout << \"Yeah! \\n \" ; // especially when lines are different lengths \u5c06\u6ce8\u91ca\u5199\u5728\u4ee3\u7801\u884c\u7684\u53f3\u4fa7\uff0c\u4f1a\u8ba9\u4ee3\u7801\u548c\u6ce8\u91ca\u7684\u53ef\u8bfb\u6027\u53d8\u5dee\uff0c\u5c24\u5176\u662f\u8fd9\u884c\u6bd4\u8f83\u957f\u7684\u60c5\u51b5\u3002\u5982\u679c\u8be5\u884c\u4e0d\u957f\uff0c\u6ce8\u91ca\u5c31\u653e\u5728\u540e\u9762\u5427\uff0c\u901a\u5e38\u653e\u7f6e\u5728\u4e0b\u4e00\u4e2a\u5236\u8868\u7b26\u7684\u4f4d\u7f6e\uff0c\u50cf\u8fd9\u6837\uff1a 1 2 3 std :: cout << \"Hello world! \\n \" ; // std::cout lives in the iostream library std :: cout << \"It is very nice to meet you! \\n \" ; // this is much easier to read std :: cout << \"Yeah! \\n \" ; // don't you think so? \u4f46\u662f\uff0c\u5982\u679c\u884c\u5f88\u957f\uff0c\u5c06\u6ce8\u91ca\u653e\u5728\u53f3\u8fb9\u4f1a\u4f7f\u8fd9\u4e00\u884c\u53d8\u5f97\u7279\u522b\u957f\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5355\u884c\u6ce8\u91ca\u901a\u5e38\u653e\u5728\u5b83\u6240\u6ce8\u91ca\u7684\u884c\u4e4b\u4e0a\uff1a 1 2 3 4 5 6 7 8 // std::cout lives in the iostream library std :: cout << \"Hello world! \\n \" ; // this is much easier to read std :: cout << \"It is very nice to meet you! \\n \" ; // don't you think so? std :: cout << \"Yeah! \\n \" ; \u4f5c\u8005\u6ce8 \u4e0a\u9762\u7684\u8bed\u53e5\u662f\u6211\u4eec\u7b2c\u4e00\u6b21\u9047\u5230\u201c\u4ee3\u7801\u7247\u6bb5\u201d\u3002\u201c\u4ee3\u7801\u7247\u6bb5\u201d\u4e0d\u662f\u5b8c\u6574\u7684\u7a0b\u5e8f\uff0c\u6240\u4ee5\u5b83\u4eec\u4e0d\u80fd\u88ab\u7f16\u8bd1\u3002\u5b83\u4eec\u7684\u5b58\u5728\u662f\u4e3a\u4e86\u4ee5\u7b80\u6d01\u7684\u65b9\u5f0f\u6f14\u793a\u7279\u5b9a\u7684\u6982\u5ff5\u3002 \u5982\u679c\u4f60\u60f3\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\u7247\u6bb5\uff0c\u5219\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5b8c\u6574\u7684\u7a0b\u5e8f\uff0c\u4ee5\u4fbf\u7f16\u8bd1\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u8be5\u7a0b\u5e8f\u770b\u8d77\u6765\u50cf\u8fd9\u6837: 1 2 3 4 5 6 7 8 #include int main () { // Replace this line with the snippet of code you'd like to compile return 0 ; }","title":"\u5355\u884c\u6ce8\u91ca"},{"location":"1-2-comments/#_2","text":"/* \u548c */ \u7b26\u53f7\u5bf9\u8868\u793aC\u98ce\u683c\u7684\u591a\u884c\u6ce8\u91ca\u3002\u5728\u8fd9\u4e24\u4e2a\u7b26\u53f7\u4e4b\u95f4\u7684\u4efb\u610f\u5185\u5bb9\u90fd\u4f1a\u88ab\u7f16\u8bd1\u5668\u5ffd\u7565 1 2 3 /* This is a multi-line comment. This line will be ignored. So will this one. */ \u56e0\u4e3a\u7b26\u53f7\u4e4b\u95f4\u7684\u6240\u6709\u5185\u5bb9\u90fd\u88ab\u5ffd\u7565\u4e86\uff0c\u4f60\u6709\u65f6\u4f1a\u770b\u5230\u7a0b\u5e8f\u5458\u201c\u7f8e\u5316\u201d\u4ed6\u4eec\u7684\u591a\u884c\u6ce8\u91ca\uff1a 1 2 3 4 /* This is a multi-line comment. * the matching asterisks to the left * can make this easier to read */ \u591a\u884c\u6837\u5f0f\u7684\u6ce8\u91ca\u4e0d\u80fd\u5d4c\u5957\u3002\u56e0\u6b64\uff0c\u4ee5\u4e0b\u60c5\u51b5\u5c06\u4ea7\u751f\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\uff1a 1 2 /* This is a multi-line /* comment */ this is not inside the comment */ // The above comment ends at the first */, not the second */ \u5f53\u7f16\u8bd1\u5668\u5c1d\u8bd5\u7f16\u8bd1\u5b83\u65f6\uff0c\u5b83\u5c06\u5ffd\u7565\u4ece\u7b2c\u4e00\u4e2a /* \u5230\u7b2c\u4e00\u4e2a */ \u4e2d\u7684\u6240\u6709\u5185\u5bb9\u3002\u56e0\u4e3a\u201c this is not inside the comment */ \u201d\u4e0d\u88ab\u8ba4\u4e3a\u662f\u6ce8\u91ca\u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u7f16\u8bd1\u5b83\u3002\u8fd9\u5c06\u4e0d\u53ef\u907f\u514d\u5730\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8bed\u6cd5\u9ad8\u4eae\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a\u6ce8\u91ca\u5177\u6709\u7279\u5b9a\u7684\u989c\u8272\uff0c\u6240\u4ee5\u4e0d\u5c5e\u4e8e\u6ce8\u91ca\u7684\u4e00\u90e8\u5206\u5f88\u5bb9\u6613\u88ab\u770b\u51fa\u6765\u3002 \u6ce8\u610f \u4e0d\u8981\u5728\u5176\u4ed6\u591a\u884c\u6ce8\u91ca\u4e2d\u4f7f\u7528\u591a\u884c\u6ce8\u91ca\u3002\u5c06\u5355\u884c\u6ce8\u91ca\u5c01\u88c5\u5728\u591a\u884c\u6ce8\u91ca\u4e2d\u662f\u53ef\u4ee5\u7684\u3002","title":"\u591a\u884c\u6ce8\u91ca"},{"location":"1-2-comments/#_3","text":"\u901a\u5e38\uff0c\u6ce8\u91ca\u5e94\u8be5\u7528\u4e8e\u5b8c\u6210\u4e09\u4ef6\u4e8b\u3002\u7b2c\u4e00\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u7ed9\u5b9a\u7684\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\uff0c\u6ce8\u91ca\u6700\u597d\u7528\u6765\u63cf\u8ff0\u8be5\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u7684\u529f\u80fd\u3002\u5b83\u4eec\u901a\u5e38\u653e\u5728\u6587\u4ef6\u6216\u5e93\u7684\u9876\u90e8\uff0c\u6216\u7d27\u6328\u7740\u51fd\u6570\u7684\u524d\u9762\u3002\u4f8b\u5982: 1 // This program calculates the student's final grade based on his test and homework scores. 1 // This function uses Newton's method to approximate the root of a given equation. 1 // The following lines generate a random item based on rarity, level, and a weight factor. \u6240\u6709\u8fd9\u4e9b\u6ce8\u91ca\u90fd\u53ef\u4ee5\u8ba9\u8bfb\u8005\u5728\u4e0d\u67e5\u770b\u5b9e\u9645\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u5f88\u597d\u5730\u4e86\u89e3\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u5b9e\u73b0\u4e86\u4ec0\u4e48\u529f\u80fd\u3002\u7528\u6237(\u53ef\u80fd\u662f\u5176\u4ed6\u4eba\uff0c\u5982\u679c\u4f60\u8bd5\u56fe\u91cd\u7528\u4ee5\u524d\u7f16\u5199\u7684\u4ee3\u7801\uff0c\u4e5f\u53ef\u80fd\u662f\u4f60\u81ea\u5df1)\u4e00\u773c\u5c31\u80fd\u770b\u51fa\u4ee3\u7801\u662f\u5426\u4e0e\u4ed6\u6216\u5979\u60f3\u8981\u5b9e\u73b0\u7684\u76ee\u6807\u76f8\u5173\u3002\u5728\u56e2\u961f\u4e2d\u5de5\u4f5c\u65f6\uff0c\u8fd9\u4e00\u70b9\u5c24\u5176\u91cd\u8981\uff0c\u56e0\u4e3a\u4e0d\u662f\u6bcf\u4e2a\u4eba\u90fd\u719f\u6089\u6240\u6709\u7684\u4ee3\u7801\u3002 \u7b2c\u4e8c\uff0c\u5728\u4e0a\u9762\u63cf\u8ff0\u7684\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u4e2d\uff0c\u6ce8\u91ca\u53ef\u4ee5\u7528\u6765\u63cf\u8ff0\u4ee3\u7801\u5c06\u5982\u4f55\u5b9e\u73b0\u5b83\u7684\u76ee\u6807\u3002 1 2 3 /* To calculate the final grade, we sum all the weighted midterm and homework scores and then divide by the number of scores to assign a percentage, which is used to calculate a letter grade. */ 1 2 3 4 5 6 // To generate a random item, we're going to do the following: // 1) Put all of the items of the desired rarity on a list // 2) Calculate a probability for each item based on level and weight factor // 3) Choose a random number // 4) Figure out which item that random number corresponds to // 5) Return the appropriate item \u8fd9\u4e9b\u6ce8\u91ca\u8ba9\u7528\u6237\u4e86\u89e3\u4ee3\u7801\u5c06\u5982\u4f55\u5b9e\u73b0\u5176\u76ee\u6807\uff0c\u800c\u4e0d\u5fc5\u4e86\u89e3\u6bcf\u4e00\u884c\u4ee3\u7801\u7684\u529f\u80fd\u3002 \u7b2c\u4e09\uff0c\u5728\u8bed\u53e5\u7ea7\u522b\uff0c\u6ce8\u91ca\u5e94\u8be5\u88ab\u7528\u6765\u63cf\u8ff0\u4ee3\u7801 \u4e3a\u4ec0\u4e48 \u505a\u67d0\u4e8b\uff0c\u800c\u4e0d\u662f\u88ab\u7528\u6765\u89e3\u91ca\u8be5\u884c\u4ee3\u7801\u662f\u5728 \u505a\u4ec0\u4e48\u4e8b \u3002\u5982\u679c\u4f60\u7f16\u5199\u7684\u4ee3\u7801\u975e\u5e38\u590d\u6742\uff0c\u4ee5\u81f3\u4e8e\u9700\u8981\u6ce8\u91ca\u6765\u89e3\u91ca\u8bed\u53e5\u5728\u505a\u4ec0\u4e48\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u91cd\u5199\u8bed\u53e5\uff0c\u800c\u4e0d\u662f\u4e3a\u5176\u6dfb\u52a0\u6ce8\u91ca\u3002 \u4e0b\u9762\u662f\u4e00\u4e9b\u5177\u4f53\u7684\u4f8b\u5b50\uff08\u6709\u597d\u6709\u574f\uff09\uff1a \u53cd\u9762\u6559\u6750\uff1a 1 2 // Set sight range to 0 sight = 0 ; \u7406\u7531\uff1a\u770b\u8bed\u53e5\u5c31\u77e5\u9053\u5b83\u662f\u5728\u7ed9 sight \u8d4b\u503c\uff0c\u65e0\u9700\u6ce8\u91ca\u3002 \u6b63\u786e\u793a\u4f8b\uff1a 1 2 // The player just drank a potion of blindness and can not see anything sight = 0 ; \u7406\u7531\uff1a\u8bf4\u660e\u4e3a\u4ec0\u4e48\u8981\u628a player \u7684 sight \u8bbe\u7f6e\u4e3a0\u3002 \u53cd\u9762\u6559\u6750\uff1a 1 2 // Calculate the cost of the items cost = quantity * 2 * storePrice ; \u7406\u7531\uff1a\u5f88\u663e\u7136\u662f\u5728\u8ba1\u7b97 cost \uff0c\u4f46\u662f\u6ca1\u6709\u89e3\u91ca\u4e3a\u4ec0\u4e48\u8981\u4e58\u4ee52\uff1f \u6b63\u786e\u793a\u4f8b\uff1a 1 2 // \u6211\u4eec\u9700\u8981\u5c06\u6570\u91cf\u4e58\u4ee52\u56e0\u4e3a\u5b83\u4eec\u662f\u6210\u5bf9\u8d2d\u4e70\u7684 cost = quantity * 2 * storePrice ; \u7406\u7531\uff1a\u8fd9\u6837\u5c31\u80fd\u7406\u89e3\u516c\u5f0f\u7684\u542b\u4e49\u4e86\u3002 \u7a0b\u5e8f\u5458\u5e38\u5e38\u4e0d\u5f97\u4e0d\u5728\u7528\u4e00\u79cd\u65b9\u6cd5\u89e3\u51b3\u95ee\u9898\uff0c\u8fd8\u662f\u7528\u53e6\u4e00\u79cd\u65b9\u6cd5\u89e3\u51b3\u95ee\u9898\u4e4b\u95f4\u505a\u51fa\u8270\u96be\u7684\u51b3\u5b9a\u3002\u6ce8\u91ca\u662f\u63d0\u9192\u4f60\u81ea\u5df1(\u6216\u544a\u8bc9\u522b\u4eba)\u505a\u51fa\u8fd9\u4e2a\u51b3\u5b9a\u800c\u4e0d\u662f\u53e6\u4e00\u4e2a\u51b3\u5b9a\u7684\u539f\u56e0\u7684\u597d\u65b9\u6cd5\u3002 \u6b63\u786e\u793a\u4f8b\uff1a 1 2 // We decided to use a linked list instead of an array because // arrays do insertion too slowly. 1 2 // We're going to use Newton's method to find the root of a number because // there is no deterministic way to solve these equations. \u6700\u540e\uff0c\u5e94\u8be5\u4ee5\u4e00\u79cd\u5bf9\u4e0d\u77e5\u9053\u4ee3\u7801\u662f\u505a\u4ec0\u4e48\u7684\u4eba\u6765\u8bf4\u6709\u610f\u4e49\u7684\u65b9\u5f0f\u7f16\u5199\u6ce8\u91ca\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u5458\u4f1a\u8bf4:\u201c\u8fd9\u5f88\u660e\u663e\uff01\u6211\u4e0d\u4f1a\u5fd8\u8bb0\u8fd9\u4ef6\u4e8b\u7684\u3002\u201d\u4f60\u731c\u600e\u4e48\u7740\uff1f\u8fd9\u5e76\u4e0d\u660e\u663e\uff0c\u4f60\u4f1a\u60ca\u8bb6\u4e8e\u81ea\u5df1\u6709\u591a\u5065\u5fd8\u3002\u4f60(\u6216\u5176\u4ed6\u4eba)\u4ee5\u540e\u4f1a\u611f\u8c22\u4f60\u7528\u4eba\u7c7b\u8bed\u8a00\u5199\u4e0b\u4ee3\u7801\u7684\u5185\u5bb9\u3001\u65b9\u5f0f\u548c\u539f\u56e0\u3002\u9605\u8bfb\u4e00\u884c\u884c\u4ee3\u7801\u5f88\u5bb9\u6613\uff0c\u4f46\u7406\u89e3\u5176\u8981\u5b8c\u6210\u7684\u76ee\u6807\u5374\u4e0d\u7b80\u5355\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u4f60\u7684\u4ee3\u7801\u505a\u5927\u91cf\u7684\u6ce8\u91ca\uff0c\u5199\u6ce8\u91ca\u65f6\u5c31\u50cf\u5bf9\u4e00\u4e2a\u4e0d\u77e5\u9053\u4ee3\u7801\u662f\u505a\u4ec0\u4e48\u7684\u4eba\u8bf4\u8bdd\u4e00\u6837\u3002\u4e0d\u8981\u4ee5\u4e3a\u4f60\u4f1a\u8bb0\u5f97\u4e3a\u4ec0\u4e48\u4f60\u505a\u4e86\u7279\u5b9a\u7684\u9009\u62e9\u3002 \u4f5c\u8005\u6ce8 \u5728\u672c\u7cfb\u5217\u6559\u7a0b\u7684\u5176\u4f59\u90e8\u5206\u4e2d\uff0c\u6211\u4eec\u5c06\u5728\u4ee3\u7801\u5757\u4e2d\u4f7f\u7528\u6ce8\u91ca\u6765\u8ba9\u4f60\u5173\u6ce8\u7279\u5b9a\u7684\u5185\u5bb9\uff0c\u6216\u8005\u5e2e\u52a9\u8bf4\u660e\u4ee3\u7801\u662f\u5982\u4f55\u5de5\u4f5c\u7684(\u540c\u65f6\u786e\u4fdd\u7a0b\u5e8f\u80fd\u591f\u7f16\u8bd1)\u3002\u7cbe\u660e\u7684\u8bfb\u8005\u4f1a\u6ce8\u610f\u5230\uff0c\u6309\u7167\u4e0a\u8ff0\u6807\u51c6\uff0c\u5927\u591a\u6570\u8bc4\u8bba\u90fd\u5f88\u7cdf\u7cd5\u3002:)\u5f53\u4f60\u9605\u8bfb\u5269\u4e0b\u7684\u6559\u7a0b\u65f6\uff0c\u8bf7\u8bb0\u4f4f\uff0c\u8fd9\u4e9b\u6ce8\u91ca\u662f\u4e3a\u6559\u80b2\u76ee\u7684\u670d\u52a1\u7684\uff0c\u800c\u4e0d\u662f\u8bd5\u56fe\u6f14\u793a\u597d\u7684\u6ce8\u91ca\u662f\u4ec0\u4e48\u6837\u5b50\u7684\u3002","title":"\u6b63\u786e\u5730\u4f7f\u7528\u6ce8\u91ca"},{"location":"1-2-comments/#_4","text":"\u5c06\u4e00\u884c\u6216\u591a\u884c\u4ee3\u7801\u8f6c\u6362\u4e3a\u6ce8\u91ca\u79f0\u4e3a\u6ce8\u91ca\u6389\u4ee3\u7801\u3002\u8fd9\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u7684\u65b9\u6cd5(\u4e34\u65f6)\u5c06\u90e8\u5206\u4ee3\u7801\u6392\u9664\u5728\u7f16\u8bd1\u540e\u7684\u7a0b\u5e8f\u4e2d\u3002 \u8981\u6ce8\u91ca\u6389\u4e00\u884c\u4ee3\u7801\uff0c\u53ea\u9700\u4f7f\u7528 // \u6837\u5f0f\u6ce8\u91ca\u5c06\u4e00\u884c\u4ee3\u7801\u4e34\u65f6\u53d8\u6210\u6ce8\u91ca: \u672a\u6ce8\u91ca\u6389\uff1a 1 std :: cout << 1 ; \u6ce8\u91ca\u6389\uff1a 1 // std::cout << 1; \u8981\u6ce8\u91ca\u6389\u4ee3\u7801\u5757\uff0c\u53ef\u4ee5\u5728\u591a\u884c\u4ee3\u7801\u4e0a\u4f7f\u7528 // \uff0c\u6216\u8005\u4f7f\u7528 /* */ \u6ce8\u91ca\u5c06\u4ee3\u7801\u5757\u4e34\u65f6\u8f6c\u6362\u4e3a\u6ce8\u91ca\u3002 \u672a\u6ce8\u91ca\u6389\uff1a 1 2 3 std :: cout << 1 ; std :: cout << 2 ; std :: cout << 3 ; \u6ce8\u91ca\u6389\uff1a 1 2 3 // std::cout << 1; // std::cout << 2; // std::cout << 3; \u6216 1 2 3 4 5 /* std::cout << 1; std::cout << 2; std::cout << 3; */ \u4f60\u9700\u8981\u8fd9\u4e48\u505a\u7684\u539f\u56e0\u6709\u5f88\u591a\uff1a \u4f60\u6b63\u5728\u5904\u7406\u4e00\u6bb5\u5c1a\u672a\u7f16\u8bd1\u7684\u65b0\u4ee3\u7801\uff0c\u9700\u8981\u8fd0\u884c\u8be5\u7a0b\u5e8f\u3002\u5982\u679c\u6709\u7f16\u8bd1\u5668\u9519\u8bef\uff0c\u7f16\u8bd1\u5668\u5c06\u4e0d\u5141\u8bb8\u7f16\u8bd1\u4ee3\u7801\u3002\u6ce8\u91ca\u6389\u4e0d\u80fd\u7f16\u8bd1\u7684\u4ee3\u7801\u5c06\u5141\u8bb8\u7a0b\u5e8f\u7f16\u8bd1\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u518d\u6b21\u8fd0\u884c\u3002\u51c6\u5907\u597d\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u53d6\u6d88\u5bf9\u4ee3\u7801\u7684\u6ce8\u91ca\uff0c\u5e76\u7ee7\u7eed\u5904\u7406\u5b83\u3002 \u4f60\u5df2\u7ecf\u7f16\u5199\u4e86\u53ef\u7f16\u8bd1\u4f46\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u7684\u65b0\u4ee3\u7801\uff0c\u5e76\u4e14\u76f4\u5230\u4ee5\u540e\u624d\u6709\u65f6\u95f4\u4fee\u590d\u5b83\u3002\u6ce8\u91ca\u6389\u635f\u574f\u7684\u4ee3\u7801\u5c06\u786e\u4fdd\u635f\u574f\u7684\u4ee3\u7801\u4e0d\u4f1a\u6267\u884c\u5e76\u5bfc\u81f4\u95ee\u9898\uff0c\u76f4\u5230\u4f60\u80fd\u591f\u4fee\u590d\u5b83\u3002 \u627e\u5230\u9519\u8bef\u7684\u6765\u6e90\u3002\u5982\u679c\u7a0b\u5e8f\u6ca1\u6709\u4ea7\u751f\u9884\u671f\u7684\u7ed3\u679c(\u6216\u5d29\u6e83)\uff0c\u6709\u65f6\u53ef\u4ee5\u7981\u7528\u90e8\u5206\u4ee3\u7801\uff0c\u770b\u770b\u662f\u5426\u53ef\u4ee5\u5206\u79bb\u51fa\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\u7684\u539f\u56e0\u3002\u5982\u679c\u6ce8\u91ca\u6389\u4e86\u4e00\u884c\u6216\u591a\u884c\u4ee3\u7801\u540e\u7a0b\u5e8f\u5f00\u59cb\u6309\u9884\u671f\u8fd0\u884c(\u6216\u505c\u6b62\u5d29\u6e83)\uff0c\u90a3\u4e48\u9519\u8bef\u53ef\u80fd\u4f4d\u4e8e\u6700\u540e\u6ce8\u91ca\u6389\u7684\u4ee3\u7801\u4e2d\u3002 \u4f60\u5e0c\u671b\u7528\u53e6\u4e00\u6bb5\u4ee3\u7801\u66ff\u6362\u4e00\u6bb5\u4ee3\u7801\uff0c\u4f46\u4f60\u6682\u65f6\u4e0d\u60f3\u5220\u9664\u539f\u4ee3\u7801\uff0c\u6b64\u65f6\u53ef\u4ee5\u5c06\u5176\u5148\u6ce8\u91ca\u6389\uff0c\u4fdd\u7559\u5728\u90a3\u91cc\u4ee5\u4f9b\u53c2\u8003\uff0c\u76f4\u5230\u65b0\u4ee3\u7801\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u4e3a\u6b62\u3002\u4e00\u65e6\u786e\u5b9a\u65b0\u4ee3\u7801\u6b63\u5e38\u5de5\u4f5c\uff0c\u5c31\u53ef\u4ee5\u5220\u9664\u6ce8\u91ca\u6389\u7684\u65e7\u4ee3\u7801\u3002\u5982\u679c\u4f60\u4e0d\u80fd\u8ba9\u4f60\u7684\u65b0\u4ee3\u7801\u5de5\u4f5c\uff0c\u4f60\u603b\u662f\u53ef\u4ee5\u5220\u9664\u65b0\u4ee3\u7801\u548c\u53d6\u6d88\u6ce8\u91ca\u65e7\u4ee3\u7801\u6062\u590d\u5230\u4f60\u4ee5\u524d\u7684\u4ee3\u7801\u3002 \u6ce8\u91ca\u6389\u4ee3\u7801\u662f\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u5e38\u89c1\u7684\u4e8b\u60c5\uff0c\u56e0\u6b64\u8bb8\u591aIDE\u90fd\u652f\u6301\u6ce8\u91ca\u6389\u9009\u4e2d\u7684\u4ee3\u7801\u90e8\u5206\u3002\u4f7f\u7528\u65b9\u5f0f\u56e0 IDE \u800c\u5f02\u3002","title":"\u5c06\u4ee3\u7801\u6ce8\u91ca\u6389"},{"location":"1-2-comments/#for-visual-studio-users","text":"\u4f60\u53ef\u4ee5\u901a\u8fc7\u7f16\u8f91\u83dc\u5355>\u9ad8\u7ea7>\u6ce8\u91ca\u9009\u62e9(\u6216\u53d6\u6d88\u6ce8\u91ca\u9009\u62e9)\u6765\u6ce8\u91ca\u6216\u53d6\u6d88\u6ce8\u91ca\u9009\u62e9\u3002","title":"For Visual Studio users"},{"location":"1-2-comments/#for-codeblocks-users","text":"\u4f60\u53ef\u4ee5\u901a\u8fc7\u7f16\u8f91\u83dc\u5355>\u6ce8\u91ca\u6765\u6ce8\u91ca\u6216\u53d6\u6d88\u6ce8\u91ca\u9009\u62e9\u3002 \u5c0f\u8d34\u58eb \u5982\u679c\u4f60\u53ea\u4f7f\u7528\u5355\u884c\u6ce8\u91ca\uff0c\u90a3\u4e48\u4f60\u603b\u662f\u53ef\u4ee5\u4f7f\u7528\u591a\u884c\u6ce8\u91ca\u6765\u6ce8\u91ca\u6389\u4ee3\u7801\u800c\u4e0d\u4ea7\u751f\u51b2\u7a81\u3002\u53cd\u4e4b\u5219\u4f1a\u6bd4\u8f83\u9ebb\u70e6\u3002 \u5982\u679c\u4f60\u786e\u5b9e\u9700\u8981\u6ce8\u91ca\u6389\u4e00\u4e2a\u5305\u542b\u591a\u884c\u6ce8\u91ca\u7684\u4ee3\u7801\u5757\uff0c\u4f60\u4e5f\u53ef\u4ee5\u8003\u8651\u4f7f\u7528 # if 0 \u9884\u5904\u7406\u5668\u6307\u4ee4 2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb","title":"For Code::Blocks users"},{"location":"1-2-comments/#_5","text":"\u5728\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u7ea7\u522b\uff0c\u4f7f\u7528\u6ce8\u91ca\u6765\u63cf\u8ff0\u201c\u662f\u4ec0\u4e48\u201d\u3002 \u5728\u5e93\u3001\u7a0b\u5e8f\u6216\u51fd\u6570\u5185\u90e8\uff0c\u4f7f\u7528\u6ce8\u91ca\u6765\u63cf\u8ff0\u201c\u600e\u4e48\u505a\u201d\u3002 \u5728\u8bed\u53e5\u7ea7\u522b\uff0c\u4f7f\u7528\u6ce8\u91ca\u6765\u63cf\u8ff0\u201c\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u505a\u201d\u3002","title":"\u5c0f\u7ed3"},{"location":"1-3-Introduction-to-objects-and-variables/","tags":["object","variable"],"text":"Key Takeaway \u5c3d\u7ba1\u5728\u8bed\u8a00\u5c42\u9762\uff0c\u4f7f\u7528\u5355\u4e00\u8bed\u53e5\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\u662f\u88ab\u5141\u8bb8\u7684\uff0c\u4f46\u662f\u5728\u5b9e\u8df5\u4e2d\u5e94\u5f53\u5c3d\u91cf\u907f\u514d\u8fd9\u6837\u53bb\u505a\uff08\u5373\u65f6\u5b83\u4eec\u5177\u6709\u76f8\u540c\u7684\u7c7b\u578b\uff09\u3002\u6b63\u786e\u7684\u505a\u6cd5\u662f\u4e00\u4e2a\u8bed\u53e5\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\uff08\u5e76\u4f7f\u7528\u5355\u884c\u6ce8\u91ca\u89e3\u91ca\u8be5\u53d8\u91cf\u7684\u7528\u9014\uff09\u3002 C++ \u53ea\u80fd\u901a\u8fc7\u5bf9\u8c61\u6765\u95f4\u63a5\u5730\u8bbf\u95ee\u5185\u5b58 \u5bf9\u8c61\u5373\u5b58\u653e\u5728\u5185\u5b58\u4e2d\u5177\u6709\u503c\u4ee5\u53ca\u5176\u4ed6\u5c5e\u6027\u7684\u6570\u636e C++ \u4e2d\u7684\u5bf9\u8c61\u6307\u7684\u662f\u53d8\u91cf\u548c\u6570\u636e\u7ed3\u6784\uff0c\u4f46\u4e0d\u5305\u62ec\u51fd\u6570 \u5bf9\u8c61\u53ef\u4ee5\u662f\u547d\u540d\u7684\u6216\u672a\u547d\u540d\u7684\uff0c\u547d\u540d\u7684\u5bf9\u8c61\u79f0\u4e3a\u53d8\u91cf \u53d8\u91cf\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u88ab\u5b9e\u4f8b\u5316\uff0c\u5373\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u3001\u4e3a\u5176\u5206\u914d\u5185\u5b58\u5e76\u5c06\u7ed3\u679c\u4fdd\u5b58\u5728\u53d8\u91cf\u4e2d \u6570\u636e\u7c7b\u578b\u7528\u4e8e\u544a\u8bc9\u7f16\u8bd1\u5668\u5b83\u8981\u5b58\u50a8\u6216\u89e3\u6790\u7684\u53d8\u91cf\u662f\u4ec0\u4e48\u683c\u5f0f \u5c3d\u91cf\u907f\u514d\u5728\u4e00\u884c\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\uff0c\u5bb9\u6613\u51fa\u73b0\u5fd8\u8bb0\u521d\u59cb\u5316\u7684\u60c5\u51b5 \u6570\u636e \u5728 1.1 \u7a0b\u5e8f\u7ed3\u6784\u548c\u8bed\u53e5 \u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u7ec4\u6210\u4e00\u4e2a\u7a0b\u5e8f\u6240\u6d89\u53ca\u7684\u5927\u591a\u6570\u8bed\u53e5\u5e76\u4e14\u5c06\u5b83\u4eec\u5c01\u88c5\u6210\u4e86\u51fd\u6570\u3002\u8fd9\u4e9b\u8bed\u53e5\u80fd\u591f\u6267\u884c\u4e00\u7cfb\u5217\u64cd\u4f5c\u5e76\u6309\u7167\uff08\u4f46\u613f\uff09\u9884\u671f\u8f93\u51fa\u6b63\u786e\u7684\u7ed3\u679c\u3002 \u4f46\u662f\uff0c\u7a0b\u5e8f\u7a76\u7adf\u662f\u5982\u4f55\u751f\u6210\u8fd9\u4e9b\u7ed3\u679c\u7684\u5462\uff1f\u5b83\u4eec\u662f\u901a\u8fc7\u64cd\u4f5c\uff08\u8bfb\u3001\u4fee\u6539\u3001\u5199\uff09\u6570\u636e\u6765\u5b8c\u6210\u8fd9\u4e00\u8fc7\u7a0b\u7684\u3002\u5728\u8ba1\u7b97\u673a\u9886\u57df\uff0c\u6570\u636e\u6307\u7684\u662f\u4efb\u4f55\u53ef\u4ee5\u88ab\u8ba1\u7b97\u673a\u79fb\u52a8\u3001\u5904\u7406\u548c\u5b58\u50a8\u7684\u4fe1\u606f\u3002 \u5173\u952e\u4fe1\u606f \u6240\u8c13\u7a0b\u5e8f\uff0c\u5373\u4e00\u7cfb\u5217\u7528\u4e8e\u64cd\u4f5c\u6570\u636e\u5e76\u8f93\u51fa\u9884\u671f\u7ed3\u679c\u7684\u6307\u4ee4 \u4e00\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u9014\u5f84\u6765\u83b7\u53d6\u6570\u636e\uff0c\u4f8b\u5982\u901a\u8fc7\u6587\u4ef6\u3001\u6570\u636e\u5e93\u3001\u7f51\u7edc\u3001\u7528\u6237\u8f93\u5165\u6765\u83b7\u53d6\uff0c\u7a0b\u5e8f\u5458\u751a\u81f3\u53ef\u4ee5\u76f4\u63a5\u5c06\u6570\u636e\u5d4c\u5165\u5728\u7a0b\u5e8f\u6e90\u7801\u4e2d\u3002\u5728\u524d\u9762\u8bfe\u7a0b\u4e2d\u7f16\u5199\u7684 \u201cHello world\u201d \u7a0b\u5e8f\u4e2d\uff0c\u201cHello world\u201d\u8fd9\u4e2a\u5b57\u7b26\u4e32\u4fbf\u662f\u76f4\u63a5\u5d4c\u5165\u5728\u7a0b\u5e8f\u4e2d\u7684\u3002\u968f\u540e\uff0c\u8be5\u7a0b\u5e8f\u5bf9\u8be5\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\uff0c\u5c06\u5176\u663e\u793a\u5728\u663e\u793a\u5668\u4e0a\u3002 \u6570\u636e\u5728\u8ba1\u7b97\u673a\u4e2d\u5b58\u50a8\u65f6\uff0c\u5f80\u5f80\u4ee5\u4e00\u79cd\u66f4\u9ad8\u6548\u66f4\u4fbf\u4e8e\u5904\u7406\u7684\u683c\u5f0f\u6765\u5b58\u50a8\uff08\u5f80\u5f80\u4e0d\u80fd\u591f\u88ab\u4eba\u7c7b\u76f4\u63a5\u9605\u8bfb\uff09\u3002\u56e0\u6b64\uff0c\u5f53 \u201cHello World\u201d \u7a0b\u5e8f\u88ab\u7f16\u8bd1\u540e\uff0c\u5b57\u7b26\u4e32 \u201cHello World\u201d \u4f1a\u88ab\u8f6c\u6362\u4e3a\u4e00\u79cd\u66f4\u4e3a\u9ad8\u6548\u7684\u683c\u5f0f\u6765\u5b58\u50a8\u5e76\u63d0\u4f9b\u7ed9\u7a0b\u5e8f\u4f7f\u7528\uff08\u4e8c\u8fdb\u5236\u683c\u5f0f\u2014\u2014\u6211\u4eec\u5c06\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\uff09 \u5bf9\u8c61\u548c\u53d8\u91cf \u6240\u6709\u8ba1\u7b97\u673a\u90fd\u6709\u5185\u5b58\uff0c\u5373 RAM \uff08\u968f\u673a\u8bbf\u95ee\u5185\u5b58\uff09\uff0c \u53ef\u4f9b\u7a0b\u5e8f\u4f7f\u7528\u3002\u4f60\u53ef\u4ee5\u5c06 RAM \u770b\u505a\u662f\u4e00\u7cfb\u5217\u5177\u6709\u6807\u53f7\u7684\u201c\u4fe1\u7bb1\u201d\uff0c\u5728\u7a0b\u5e8f\u8fd0\u884c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5b83\u4eec\u53ef\u4ee5\u5b58\u653e\u4e00\u7cfb\u5217\u7684\u6570\u636e\u7247\u6bb5\u3002\u4e00\u4e2a\u5355\u72ec\u7684\u5b58\u653e\u5728\u5185\u5b58\u4e2d\u7684\u6570\u636e\uff0c\u79f0\u4e4b\u4e3a\u503c\uff08value\uff09\u3002 \u5728\u4e00\u4e9b\u6bd4\u8f83\u53e4\u8001\u7684\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff08\u4f8b\u5982 Apple Basic\uff09\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u8fd9\u4e9b\u201d\u4fe1\u7bb1\u201c\uff08\u5bf9\u5e94\u7684\u8bed\u53e5\u7c7b\u4f3c\u4e8e \u83b7\u53d6\u6807\u53f7\u4e3a7532\u7684\u4fe1\u7bb1\u4e2d\u7684\u6570\u636e \uff09 \u5728 C++ \u4e2d\uff0c\u76f4\u63a5\u8bbf\u95ee\u5185\u5b58\u662f\u4e0d\u88ab\u5141\u8bb8\u7684\uff0c\u6211\u4eec\u53ea\u80fd\u591f\u901a\u8fc7\u67d0\u4e2a\u5bf9\u8c61\u95f4\u63a5\u5730\u8bbf\u95ee\u5185\u5b58\u3002\u6240\u8c13\u5bf9\u8c61\uff0c\u5373\u5b58\u653e\u5728\u67d0\u4e2a\u533a\u57df\uff08\u4e00\u822c\u6765\u8bb2\u4e3a\u5185\u5b58\uff09\uff0c\u5177\u6709\u503c\u4ee5\u53ca\u5176\u4ed6\u5c5e\u6027\uff08\u540e\u7eed\u8bfe\u7a0b\u4f1a\u7740\u91cd\u4ecb\u7ecd\uff09\u7684\u6570\u636e\u3002\u7f16\u8bd1\u5668\u548c\u64cd\u4f5c\u7cfb\u7edf\u4e3a\u5bf9\u8c61\u5206\u914d\u5185\u5b58\u7684\u8fc7\u7a0b\uff0c\u8d85\u51fa\u4e86\u672c\u8bfe\u7a0b\u7684\u8303\u56f4\u3002\u4f46\u662f\uff0c\u6211\u4eec\u9700\u8981\u77e5\u9053\u7684\u662f\uff0c \u83b7\u53d6\u6807\u53f7\u4e3a7532\u7684\u4fe1\u7bb1\u4e2d\u7684\u6570\u636e \u662f\u4e0d\u53ef\u884c\u7684\uff0c\u53d6\u800c\u4ee3\u4e4b\u7684\u5e94\u5f53\u662f \u83b7\u53d6\u67d0\u4e2a\u5bf9\u8c61\u6240\u4fdd\u5b58\u7684\u503c \u3002\u8fd9\u610f\u5473\u7740\uff0c\u6211\u4eec\u53ef\u4ee5\u4e13\u6ce8\u4e8e\u4f7f\u7528\u5bf9\u8c61\u6765\u5b58\u50a8\u548c\u8bfb\u53d6\u6570\u636e\uff0c\u800c\u4e0d\u5fc5\u62c5\u5fc3\u5b83\u4eec\u5b9e\u9645\u4f7f\u7528\u7684\u5185\u5b58\u3002 \u5bf9\u8c61\u53ef\u4ee5\u662f\u547d\u540d\u7684\uff0c\u6216\u672a\u547d\u540d\u7684\u3002\u4e00\u4e2a\u547d\u540d\u7684\u5bf9\u8c61\u88ab\u79f0\u4e3a\u53d8\u91cf\uff0c\u800c\u8be5\u5bf9\u8c61\u7684\u540d\u5b57\u672c\u8eab\uff0c\u5219\u79f0\u4e3a \u6807\u8bc6\u7b26(identifier) \u3002\u5728\u6211\u4eec\u7684\u7a0b\u5e8f\u4e2d\uff0c\u5927\u591a\u6570\u7684\u5bf9\u8c61\u90fd\u5c5e\u4e8e\u53d8\u91cf\u3002 \u4f5c\u8005\u6ce8 \u5728\u4e00\u822c\u7684\u7f16\u7a0b\u4e2d\uff0c \u5bf9\u8c61 \u8fd9\u4e2a\u672f\u8bed\u901a\u5e38\u6307\u4ee3\u7684\u662f\u67d0\u4e00\u4e2a\u53d8\u91cf\u3001\u6570\u636e\u7ed3\u6784\u6216\u51fd\u6570\u3002\u5728 C++ \u4e2d\uff0c \u5bf9\u8c61 \u8fd9\u4e2a\u672f\u8bed\u7684\u542b\u4e49\u8981\u66f4\u52a0\u5c40\u9650\u4e00\u4e9b\uff0c\u5b83\u5e76\u4e0d\u5305\u62ec\u51fd\u6570\u3002 \u53d8\u91cf\u7684\u5b9e\u4f8b\u5316 \u4e3a\u4e86\u521b\u5efa\u53d8\u91cf\uff0c\u6211\u4eec\u9700\u8981\u4e00\u79cd\u88ab\u79f0\u4e3a\u201d\u5b9a\u4e49\u201c\u7684\u7279\u6b8a\u7c7b\u578b\u7684\u58f0\u660e\u8bed\u53e5\uff08\u7a0d\u540e\u4f1a\u6f84\u6e05\u58f0\u660e\u548c\u5b9a\u4e49\u4e4b\u95f4\u7684\u533a\u522b\uff09\u3002 \u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u540d\u4e3ax\u7684\u53d8\u91cf\uff0c\u6848\u4f8b\u5982\u4e0b\uff1a 1 int x ; // define a variable named x, of type int \u5728\u7f16\u8bd1\u9636\u6bb5\uff0c\u5f53\u7f16\u8bd1\u5668\u89e3\u6790\u5230\u8be5\u8bed\u53e5\u65f6\uff0c\u5b83\u4f1a\u8bb0\u5f55\u4e0b\u6b64\u65f6\u5b9a\u4e49\u7684\u4e00\u4e2a\u540d\u4e3ax\u3001\u7c7b\u578b\u4e3a int \u7684\u53d8\u91cf\u3002\uff08\u7a0d\u540e\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u7c7b\u578b\u7cfb\u7edf\uff09\u3002\u4ece\u6b64\u4ee5\u540e\uff08\u6709\u4e00\u5b9a\u7684\u9650\u5236\u6761\u4ef6\uff0c\u7a0d\u540e\u4ecb\u7ecd\uff09\uff0c\u6bcf\u5f53\u7f16\u8bd1\u5668\u770b\u5230\u6807\u8bc6\u7b26 x\uff0c\u5b83\u5c31\u77e5\u9053\u53bb\u5f15\u7528\u8fd9\u4e2a\u53d8\u91cf\u3002 \u5728\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u5019\uff08\u79f0\u4e3a\u8fd0\u884c\u65f6\uff09\uff0c\u8be5\u53d8\u91cf\u4f1a\u88ab \u5b9e\u4f8b\u5316 \u3002\u5b9e\u4f8b\u5316\u8fd9\u4e2a\u8bcd\u53ea\u662f\u542c\u8d77\u6765\u6709\u70b9\u7384\u4e4e\u7f62\u4e86\uff0c\u5b83\u7684\u542b\u4e49\u5f88\u7b80\u5355\u2014\u2014\u521b\u5efa\u5bf9\u8c61\u5e76\u4e3a\u5176\u5206\u914d\u4e00\u4e2a\u5185\u5b58\u5730\u5740\u3002\u53d8\u91cf\u5728\u4f7f\u7528\u524d\uff08\u5b58\u50a8\u503c\u524d\uff09\u5fc5\u987b\u5b9e\u4f8b\u5316\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u5047\u8bbe\u53d8\u91cfx\u5b9e\u4f8b\u5316\u540e\u5bf9\u5e94\u7684\u5185\u5b58\u5730\u5740\u4e3a140\uff0c\u4e4b\u540e\u7a0b\u5e8f\u5728\u4efb\u4f55\u65f6\u5019\u4f7f\u7528\u8be5\u53d8\u91cfx\u65f6\uff0c\u5b83\u90fd\u4f1a\u53d6\u8bbf\u95ee\u5185\u5b58\u5730\u5740140\u3002\u4e00\u4e2a\u5b9e\u4f8b\u5316\u4e4b\u540e\u7684\u5bf9\u8c61\uff0c\u901a\u5e38\u88ab\u79f0\u4e3a \u5b9e\u4f8b(instance) \u3002 \u6570\u636e\u7c7b\u578b \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u4e86\uff0c\u53d8\u91cf\u5c31\u662f\u67d0\u4e2a\u547d\u540d\u4e4b\u540e\u7684\u5b58\u50a8\u533a\u57df\uff0c\u5b83\u53ef\u4ee5\u5b58\u653e\u6570\u636e\uff08\u5982\u4f55\u5b58\u653e\u5c06\u5728\u540e\u7eed\u7684\u7ae0\u8282\u8ba8\u8bba\uff09\u3002\u6570\u636e\u7c7b\u578b\uff08\u4e00\u822c\u7b80\u79f0\u4e3a\u7c7b\u578b\uff09\u53ef\u4ee5\u544a\u77e5\u7f16\u8bd1\u5668\u5b83\u8981\u5b58\u50a8\u7684\u8be5\u6570\u636e\u7684\u503c\u662f\u4ec0\u4e48\u7c7b\u578b\uff08\u4f8b\u5982\uff0c\u6570\u5b57\uff0c\u5b57\u6bcd\uff0c\u6587\u672c\u7b49\u7b49\uff09\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf x \u7684\u7c7b\u578b\u662f int\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u8be5\u53d8\u91cf x \u8868\u793a\u7684\u662f\u4e00\u4e2a\u6574\u578b\u7684\u503c\u3002\u6574\u578b\u8868\u793a\u8be5\u6570\u5b57\u4e0d\u9700\u8981\u5c0f\u6570\u90e8\u5206\u5c31\u53ef\u4ee5\u8868\u793a\uff0c\u4f8b\u5982 4\uff0c27\uff0c0\uff0c-2 \u6216\u8005 -12\u3002\u957f\u8bdd\u77ed\u8bf4\uff0c\u8fd9\u91cc x \u662f\u4e00\u4e2a\u6574\u578b\u53d8\u91cf\u3002 \u5728 C++ \u4e2d\uff0c\u53d8\u91cf\u7684\u7c7b\u578b\u5fc5\u987b\u5728 \u7f16\u8bd1\u65f6 \u786e\u5b9a\u3002\u6b64\u5916\uff0c\u8be5\u7c7b\u578b\u5728\u4e0b\u4e00\u6b21\u7f16\u8bd1\u524d\uff0c\u4e0d\u80fd\u53d1\u751f\u6539\u53d8\u3002\u8fd9\u610f\u5473\u7740\uff0c\u8be5\u6574\u578b\u53d8\u91cf\u53ea\u80fd\u59cb\u7ec8\u5b58\u653e\u4e00\u4e2a\u6574\u578b\u7684\u503c\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u5b83\u5b58\u653e\u5176\u4ed6\u7c7b\u578b\u7684\u503c\uff0c\u4f60\u5fc5\u987b\u4f7f\u7528\u53e6\u5916\u7684\u53d8\u91cf\u3002 \u6574\u578b\u53ea\u662f C++ \u539f\u751f\u652f\u6301\u7684\u591a\u79cd\u7c7b\u578b\u4e2d\u7684\u4e00\u79cd\u3002\u4e3a\u4e86\u66f4\u597d\u7684\u89e3\u91ca\uff0c\u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff0c\u8fd9\u91cc\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b\u578b\u4e3a double \u7684\u53d8\u91cf\uff1a 1 double width ; // define a variable named width, of type double C++ \u5141\u8bb8\u7528\u6237\u521b\u5efa\u81ea\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\u3002\u6211\u4eec\u4f1a\u5728\u5c06\u6765\u7684\u8bfe\u7a0b\u4e2d\u5927\u91cf\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\uff0c\u8fd9\u662f C++ \u8bed\u8a00\u5f3a\u5927\u7684\u539f\u56e0\u4e4b\u4e00\u3002 \u5728\u4ecb\u7ecd\u6027\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u53ea\u4f7f\u7528\u6574\u578b\u53d8\u91cf\uff0c\u56e0\u5b83\u4eec\u66f4\u52a0\u7b80\u5355\uff0c\u4f46\u662f\u6211\u4eec\u5f88\u5feb\u5c31\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u63a2\u7d22\u5927\u91cf\u7684\u5176\u4ed6 C++ \u652f\u6301\u7684\u6570\u636e\u7c7b\u578b\u3002 \u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf \u5728\u4e00\u4e2a\u8bed\u53e5\u4e2d\u5b9a\u4e49\u591a\u4e2a\u76f8\u540c\u7c7b\u578b\u7684\u53d8\u91cf\u5f53\u7136\u4e5f\u662f\u53ef\u4ee5\u7684\uff0c\u53ea\u9700\u8981\u7528\u9017\u53f7\u5c06\u53d8\u91cf\u540d\u5206\u5272\u5f00\u6765\u5373\u53ef\u3002\u4e0b\u9762\u4e24\u4e2a\u4ee3\u7801\u7247\u6bb5\u7684\u6548\u679c\u662f\u4e00\u6837\u7684\uff1a 1 2 int a ; int b ; 1 int a , b ; \u5982\u679c\u5c5e\u4e8e\u4e0a\u8ff0\u65b9\u5f0f\u6765\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\uff0c\u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u4f1a\u72af\u4e0b\u4e24\u79cd\u9519\u8bef\uff08\u8fd9\u6ca1\u6709\u4ec0\u4e48\u5927\u4e0d\u4e86\u7684\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u4f1a\u6355\u83b7\u8fd9\u4e9b\u9519\u8bef\u5e76\u8981\u6c42\u4f60\u8fdb\u884c\u4fee\u590d\uff09\uff1a \u7b2c\u4e00\u79cd\u9519\u8bef\u662f\u4e3a\u6bcf\u4e2a\u53d8\u91cf\u90fd\u6dfb\u52a0\u7c7b\u578b\u8bf4\u660e\u3002 1 2 3 int a , int b ; // wrong (compiler error) int a , b ; // correct \u7b2c\u4e8c\u79cd\u9519\u8bef\u662f\u5728\u4e00\u4e2a\u8bed\u53e5\u4e2d\u5b9a\u4e49\u4e24\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u8fd9\u662f\u4e0d\u88ab\u5141\u8bb8\u7684\u3002\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u53d8\u91cf\u5fc5\u987b\u5206\u522b\u5b9a\u4e49\u3002 1 2 3 4 5 6 7 int a , double b ; // wrong (compiler error) int a ; double b ; // correct (but not recommended) // correct and recommended (easier to read) int a ; double b ; \u6700\u4f73\u5b9e\u8df5 \u5c3d\u7ba1\u5728\u8bed\u8a00\u5c42\u9762\uff0c\u4f7f\u7528\u5355\u4e00\u8bed\u53e5\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\u662f\u88ab\u5141\u8bb8\u7684\uff0c\u4f46\u662f\u5728\u5b9e\u8df5\u4e2d\u5e94\u5f53\u5c3d\u91cf\u907f\u514d\u8fd9\u6837\u53bb\u505a\uff08\u5373\u65f6\u5b83\u4eec\u5177\u6709\u76f8\u540c\u7684\u7c7b\u578b\uff09\u3002\u6b63\u786e\u7684\u505a\u6cd5\u662f\u4e00\u4e2a\u8bed\u53e5\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\uff08\u5e76\u4f7f\u7528\u5355\u884c\u6ce8\u91ca\u89e3\u91ca\u8be5\u53d8\u91cf\u7684\u7528\u9014\uff09\u3002 \u5c0f\u7ed3 \u5728 C++ \u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u53d8\u91cf\u6765\u8bbf\u95ee\u5185\u5b58\u3002\u53d8\u91cf\u5177\u6709\u6807\u8bc6\u7b26\u3001\u7c7b\u578b\u548c\u503c\uff08\u4ee5\u53ca\u5176\u4ed6\u4e00\u4e9b\u4e0e\u6b64\u5904\u4e0d\u76f8\u5173\u7684\u5c5e\u6027\uff09\u3002\u53d8\u91cf\u7c7b\u578b\u7528\u4e8e\u786e\u5b9a\u5185\u5b58\u4e2d\u7684\u503c\u5e94\u8be5\u88ab\u5982\u4f55\u89e3\u6790\u3002 \u5728\u4e0b\u4e00\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5982\u4f55\u4e3a\u53d8\u91cf\u8d4b\u503c\u5e76\u4f7f\u7528\u5b83\u4eec\u3002","title":"1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf"},{"location":"1-3-Introduction-to-objects-and-variables/#_1","text":"\u5728 1.1 \u7a0b\u5e8f\u7ed3\u6784\u548c\u8bed\u53e5 \u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u7ec4\u6210\u4e00\u4e2a\u7a0b\u5e8f\u6240\u6d89\u53ca\u7684\u5927\u591a\u6570\u8bed\u53e5\u5e76\u4e14\u5c06\u5b83\u4eec\u5c01\u88c5\u6210\u4e86\u51fd\u6570\u3002\u8fd9\u4e9b\u8bed\u53e5\u80fd\u591f\u6267\u884c\u4e00\u7cfb\u5217\u64cd\u4f5c\u5e76\u6309\u7167\uff08\u4f46\u613f\uff09\u9884\u671f\u8f93\u51fa\u6b63\u786e\u7684\u7ed3\u679c\u3002 \u4f46\u662f\uff0c\u7a0b\u5e8f\u7a76\u7adf\u662f\u5982\u4f55\u751f\u6210\u8fd9\u4e9b\u7ed3\u679c\u7684\u5462\uff1f\u5b83\u4eec\u662f\u901a\u8fc7\u64cd\u4f5c\uff08\u8bfb\u3001\u4fee\u6539\u3001\u5199\uff09\u6570\u636e\u6765\u5b8c\u6210\u8fd9\u4e00\u8fc7\u7a0b\u7684\u3002\u5728\u8ba1\u7b97\u673a\u9886\u57df\uff0c\u6570\u636e\u6307\u7684\u662f\u4efb\u4f55\u53ef\u4ee5\u88ab\u8ba1\u7b97\u673a\u79fb\u52a8\u3001\u5904\u7406\u548c\u5b58\u50a8\u7684\u4fe1\u606f\u3002 \u5173\u952e\u4fe1\u606f \u6240\u8c13\u7a0b\u5e8f\uff0c\u5373\u4e00\u7cfb\u5217\u7528\u4e8e\u64cd\u4f5c\u6570\u636e\u5e76\u8f93\u51fa\u9884\u671f\u7ed3\u679c\u7684\u6307\u4ee4 \u4e00\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u9014\u5f84\u6765\u83b7\u53d6\u6570\u636e\uff0c\u4f8b\u5982\u901a\u8fc7\u6587\u4ef6\u3001\u6570\u636e\u5e93\u3001\u7f51\u7edc\u3001\u7528\u6237\u8f93\u5165\u6765\u83b7\u53d6\uff0c\u7a0b\u5e8f\u5458\u751a\u81f3\u53ef\u4ee5\u76f4\u63a5\u5c06\u6570\u636e\u5d4c\u5165\u5728\u7a0b\u5e8f\u6e90\u7801\u4e2d\u3002\u5728\u524d\u9762\u8bfe\u7a0b\u4e2d\u7f16\u5199\u7684 \u201cHello world\u201d \u7a0b\u5e8f\u4e2d\uff0c\u201cHello world\u201d\u8fd9\u4e2a\u5b57\u7b26\u4e32\u4fbf\u662f\u76f4\u63a5\u5d4c\u5165\u5728\u7a0b\u5e8f\u4e2d\u7684\u3002\u968f\u540e\uff0c\u8be5\u7a0b\u5e8f\u5bf9\u8be5\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\uff0c\u5c06\u5176\u663e\u793a\u5728\u663e\u793a\u5668\u4e0a\u3002 \u6570\u636e\u5728\u8ba1\u7b97\u673a\u4e2d\u5b58\u50a8\u65f6\uff0c\u5f80\u5f80\u4ee5\u4e00\u79cd\u66f4\u9ad8\u6548\u66f4\u4fbf\u4e8e\u5904\u7406\u7684\u683c\u5f0f\u6765\u5b58\u50a8\uff08\u5f80\u5f80\u4e0d\u80fd\u591f\u88ab\u4eba\u7c7b\u76f4\u63a5\u9605\u8bfb\uff09\u3002\u56e0\u6b64\uff0c\u5f53 \u201cHello World\u201d \u7a0b\u5e8f\u88ab\u7f16\u8bd1\u540e\uff0c\u5b57\u7b26\u4e32 \u201cHello World\u201d \u4f1a\u88ab\u8f6c\u6362\u4e3a\u4e00\u79cd\u66f4\u4e3a\u9ad8\u6548\u7684\u683c\u5f0f\u6765\u5b58\u50a8\u5e76\u63d0\u4f9b\u7ed9\u7a0b\u5e8f\u4f7f\u7528\uff08\u4e8c\u8fdb\u5236\u683c\u5f0f\u2014\u2014\u6211\u4eec\u5c06\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\uff09","title":"\u6570\u636e"},{"location":"1-3-Introduction-to-objects-and-variables/#_2","text":"\u6240\u6709\u8ba1\u7b97\u673a\u90fd\u6709\u5185\u5b58\uff0c\u5373 RAM \uff08\u968f\u673a\u8bbf\u95ee\u5185\u5b58\uff09\uff0c \u53ef\u4f9b\u7a0b\u5e8f\u4f7f\u7528\u3002\u4f60\u53ef\u4ee5\u5c06 RAM \u770b\u505a\u662f\u4e00\u7cfb\u5217\u5177\u6709\u6807\u53f7\u7684\u201c\u4fe1\u7bb1\u201d\uff0c\u5728\u7a0b\u5e8f\u8fd0\u884c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5b83\u4eec\u53ef\u4ee5\u5b58\u653e\u4e00\u7cfb\u5217\u7684\u6570\u636e\u7247\u6bb5\u3002\u4e00\u4e2a\u5355\u72ec\u7684\u5b58\u653e\u5728\u5185\u5b58\u4e2d\u7684\u6570\u636e\uff0c\u79f0\u4e4b\u4e3a\u503c\uff08value\uff09\u3002 \u5728\u4e00\u4e9b\u6bd4\u8f83\u53e4\u8001\u7684\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff08\u4f8b\u5982 Apple Basic\uff09\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u8fd9\u4e9b\u201d\u4fe1\u7bb1\u201c\uff08\u5bf9\u5e94\u7684\u8bed\u53e5\u7c7b\u4f3c\u4e8e \u83b7\u53d6\u6807\u53f7\u4e3a7532\u7684\u4fe1\u7bb1\u4e2d\u7684\u6570\u636e \uff09 \u5728 C++ \u4e2d\uff0c\u76f4\u63a5\u8bbf\u95ee\u5185\u5b58\u662f\u4e0d\u88ab\u5141\u8bb8\u7684\uff0c\u6211\u4eec\u53ea\u80fd\u591f\u901a\u8fc7\u67d0\u4e2a\u5bf9\u8c61\u95f4\u63a5\u5730\u8bbf\u95ee\u5185\u5b58\u3002\u6240\u8c13\u5bf9\u8c61\uff0c\u5373\u5b58\u653e\u5728\u67d0\u4e2a\u533a\u57df\uff08\u4e00\u822c\u6765\u8bb2\u4e3a\u5185\u5b58\uff09\uff0c\u5177\u6709\u503c\u4ee5\u53ca\u5176\u4ed6\u5c5e\u6027\uff08\u540e\u7eed\u8bfe\u7a0b\u4f1a\u7740\u91cd\u4ecb\u7ecd\uff09\u7684\u6570\u636e\u3002\u7f16\u8bd1\u5668\u548c\u64cd\u4f5c\u7cfb\u7edf\u4e3a\u5bf9\u8c61\u5206\u914d\u5185\u5b58\u7684\u8fc7\u7a0b\uff0c\u8d85\u51fa\u4e86\u672c\u8bfe\u7a0b\u7684\u8303\u56f4\u3002\u4f46\u662f\uff0c\u6211\u4eec\u9700\u8981\u77e5\u9053\u7684\u662f\uff0c \u83b7\u53d6\u6807\u53f7\u4e3a7532\u7684\u4fe1\u7bb1\u4e2d\u7684\u6570\u636e \u662f\u4e0d\u53ef\u884c\u7684\uff0c\u53d6\u800c\u4ee3\u4e4b\u7684\u5e94\u5f53\u662f \u83b7\u53d6\u67d0\u4e2a\u5bf9\u8c61\u6240\u4fdd\u5b58\u7684\u503c \u3002\u8fd9\u610f\u5473\u7740\uff0c\u6211\u4eec\u53ef\u4ee5\u4e13\u6ce8\u4e8e\u4f7f\u7528\u5bf9\u8c61\u6765\u5b58\u50a8\u548c\u8bfb\u53d6\u6570\u636e\uff0c\u800c\u4e0d\u5fc5\u62c5\u5fc3\u5b83\u4eec\u5b9e\u9645\u4f7f\u7528\u7684\u5185\u5b58\u3002 \u5bf9\u8c61\u53ef\u4ee5\u662f\u547d\u540d\u7684\uff0c\u6216\u672a\u547d\u540d\u7684\u3002\u4e00\u4e2a\u547d\u540d\u7684\u5bf9\u8c61\u88ab\u79f0\u4e3a\u53d8\u91cf\uff0c\u800c\u8be5\u5bf9\u8c61\u7684\u540d\u5b57\u672c\u8eab\uff0c\u5219\u79f0\u4e3a \u6807\u8bc6\u7b26(identifier) \u3002\u5728\u6211\u4eec\u7684\u7a0b\u5e8f\u4e2d\uff0c\u5927\u591a\u6570\u7684\u5bf9\u8c61\u90fd\u5c5e\u4e8e\u53d8\u91cf\u3002 \u4f5c\u8005\u6ce8 \u5728\u4e00\u822c\u7684\u7f16\u7a0b\u4e2d\uff0c \u5bf9\u8c61 \u8fd9\u4e2a\u672f\u8bed\u901a\u5e38\u6307\u4ee3\u7684\u662f\u67d0\u4e00\u4e2a\u53d8\u91cf\u3001\u6570\u636e\u7ed3\u6784\u6216\u51fd\u6570\u3002\u5728 C++ \u4e2d\uff0c \u5bf9\u8c61 \u8fd9\u4e2a\u672f\u8bed\u7684\u542b\u4e49\u8981\u66f4\u52a0\u5c40\u9650\u4e00\u4e9b\uff0c\u5b83\u5e76\u4e0d\u5305\u62ec\u51fd\u6570\u3002","title":"\u5bf9\u8c61\u548c\u53d8\u91cf"},{"location":"1-3-Introduction-to-objects-and-variables/#_3","text":"\u4e3a\u4e86\u521b\u5efa\u53d8\u91cf\uff0c\u6211\u4eec\u9700\u8981\u4e00\u79cd\u88ab\u79f0\u4e3a\u201d\u5b9a\u4e49\u201c\u7684\u7279\u6b8a\u7c7b\u578b\u7684\u58f0\u660e\u8bed\u53e5\uff08\u7a0d\u540e\u4f1a\u6f84\u6e05\u58f0\u660e\u548c\u5b9a\u4e49\u4e4b\u95f4\u7684\u533a\u522b\uff09\u3002 \u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u540d\u4e3ax\u7684\u53d8\u91cf\uff0c\u6848\u4f8b\u5982\u4e0b\uff1a 1 int x ; // define a variable named x, of type int \u5728\u7f16\u8bd1\u9636\u6bb5\uff0c\u5f53\u7f16\u8bd1\u5668\u89e3\u6790\u5230\u8be5\u8bed\u53e5\u65f6\uff0c\u5b83\u4f1a\u8bb0\u5f55\u4e0b\u6b64\u65f6\u5b9a\u4e49\u7684\u4e00\u4e2a\u540d\u4e3ax\u3001\u7c7b\u578b\u4e3a int \u7684\u53d8\u91cf\u3002\uff08\u7a0d\u540e\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u7c7b\u578b\u7cfb\u7edf\uff09\u3002\u4ece\u6b64\u4ee5\u540e\uff08\u6709\u4e00\u5b9a\u7684\u9650\u5236\u6761\u4ef6\uff0c\u7a0d\u540e\u4ecb\u7ecd\uff09\uff0c\u6bcf\u5f53\u7f16\u8bd1\u5668\u770b\u5230\u6807\u8bc6\u7b26 x\uff0c\u5b83\u5c31\u77e5\u9053\u53bb\u5f15\u7528\u8fd9\u4e2a\u53d8\u91cf\u3002 \u5728\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u5019\uff08\u79f0\u4e3a\u8fd0\u884c\u65f6\uff09\uff0c\u8be5\u53d8\u91cf\u4f1a\u88ab \u5b9e\u4f8b\u5316 \u3002\u5b9e\u4f8b\u5316\u8fd9\u4e2a\u8bcd\u53ea\u662f\u542c\u8d77\u6765\u6709\u70b9\u7384\u4e4e\u7f62\u4e86\uff0c\u5b83\u7684\u542b\u4e49\u5f88\u7b80\u5355\u2014\u2014\u521b\u5efa\u5bf9\u8c61\u5e76\u4e3a\u5176\u5206\u914d\u4e00\u4e2a\u5185\u5b58\u5730\u5740\u3002\u53d8\u91cf\u5728\u4f7f\u7528\u524d\uff08\u5b58\u50a8\u503c\u524d\uff09\u5fc5\u987b\u5b9e\u4f8b\u5316\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u5047\u8bbe\u53d8\u91cfx\u5b9e\u4f8b\u5316\u540e\u5bf9\u5e94\u7684\u5185\u5b58\u5730\u5740\u4e3a140\uff0c\u4e4b\u540e\u7a0b\u5e8f\u5728\u4efb\u4f55\u65f6\u5019\u4f7f\u7528\u8be5\u53d8\u91cfx\u65f6\uff0c\u5b83\u90fd\u4f1a\u53d6\u8bbf\u95ee\u5185\u5b58\u5730\u5740140\u3002\u4e00\u4e2a\u5b9e\u4f8b\u5316\u4e4b\u540e\u7684\u5bf9\u8c61\uff0c\u901a\u5e38\u88ab\u79f0\u4e3a \u5b9e\u4f8b(instance) \u3002","title":"\u53d8\u91cf\u7684\u5b9e\u4f8b\u5316"},{"location":"1-3-Introduction-to-objects-and-variables/#_4","text":"\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u4e86\uff0c\u53d8\u91cf\u5c31\u662f\u67d0\u4e2a\u547d\u540d\u4e4b\u540e\u7684\u5b58\u50a8\u533a\u57df\uff0c\u5b83\u53ef\u4ee5\u5b58\u653e\u6570\u636e\uff08\u5982\u4f55\u5b58\u653e\u5c06\u5728\u540e\u7eed\u7684\u7ae0\u8282\u8ba8\u8bba\uff09\u3002\u6570\u636e\u7c7b\u578b\uff08\u4e00\u822c\u7b80\u79f0\u4e3a\u7c7b\u578b\uff09\u53ef\u4ee5\u544a\u77e5\u7f16\u8bd1\u5668\u5b83\u8981\u5b58\u50a8\u7684\u8be5\u6570\u636e\u7684\u503c\u662f\u4ec0\u4e48\u7c7b\u578b\uff08\u4f8b\u5982\uff0c\u6570\u5b57\uff0c\u5b57\u6bcd\uff0c\u6587\u672c\u7b49\u7b49\uff09\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf x \u7684\u7c7b\u578b\u662f int\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u8be5\u53d8\u91cf x \u8868\u793a\u7684\u662f\u4e00\u4e2a\u6574\u578b\u7684\u503c\u3002\u6574\u578b\u8868\u793a\u8be5\u6570\u5b57\u4e0d\u9700\u8981\u5c0f\u6570\u90e8\u5206\u5c31\u53ef\u4ee5\u8868\u793a\uff0c\u4f8b\u5982 4\uff0c27\uff0c0\uff0c-2 \u6216\u8005 -12\u3002\u957f\u8bdd\u77ed\u8bf4\uff0c\u8fd9\u91cc x \u662f\u4e00\u4e2a\u6574\u578b\u53d8\u91cf\u3002 \u5728 C++ \u4e2d\uff0c\u53d8\u91cf\u7684\u7c7b\u578b\u5fc5\u987b\u5728 \u7f16\u8bd1\u65f6 \u786e\u5b9a\u3002\u6b64\u5916\uff0c\u8be5\u7c7b\u578b\u5728\u4e0b\u4e00\u6b21\u7f16\u8bd1\u524d\uff0c\u4e0d\u80fd\u53d1\u751f\u6539\u53d8\u3002\u8fd9\u610f\u5473\u7740\uff0c\u8be5\u6574\u578b\u53d8\u91cf\u53ea\u80fd\u59cb\u7ec8\u5b58\u653e\u4e00\u4e2a\u6574\u578b\u7684\u503c\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u5b83\u5b58\u653e\u5176\u4ed6\u7c7b\u578b\u7684\u503c\uff0c\u4f60\u5fc5\u987b\u4f7f\u7528\u53e6\u5916\u7684\u53d8\u91cf\u3002 \u6574\u578b\u53ea\u662f C++ \u539f\u751f\u652f\u6301\u7684\u591a\u79cd\u7c7b\u578b\u4e2d\u7684\u4e00\u79cd\u3002\u4e3a\u4e86\u66f4\u597d\u7684\u89e3\u91ca\uff0c\u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff0c\u8fd9\u91cc\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b\u578b\u4e3a double \u7684\u53d8\u91cf\uff1a 1 double width ; // define a variable named width, of type double C++ \u5141\u8bb8\u7528\u6237\u521b\u5efa\u81ea\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\u3002\u6211\u4eec\u4f1a\u5728\u5c06\u6765\u7684\u8bfe\u7a0b\u4e2d\u5927\u91cf\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\uff0c\u8fd9\u662f C++ \u8bed\u8a00\u5f3a\u5927\u7684\u539f\u56e0\u4e4b\u4e00\u3002 \u5728\u4ecb\u7ecd\u6027\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u53ea\u4f7f\u7528\u6574\u578b\u53d8\u91cf\uff0c\u56e0\u5b83\u4eec\u66f4\u52a0\u7b80\u5355\uff0c\u4f46\u662f\u6211\u4eec\u5f88\u5feb\u5c31\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u63a2\u7d22\u5927\u91cf\u7684\u5176\u4ed6 C++ \u652f\u6301\u7684\u6570\u636e\u7c7b\u578b\u3002","title":"\u6570\u636e\u7c7b\u578b"},{"location":"1-3-Introduction-to-objects-and-variables/#_5","text":"\u5728\u4e00\u4e2a\u8bed\u53e5\u4e2d\u5b9a\u4e49\u591a\u4e2a\u76f8\u540c\u7c7b\u578b\u7684\u53d8\u91cf\u5f53\u7136\u4e5f\u662f\u53ef\u4ee5\u7684\uff0c\u53ea\u9700\u8981\u7528\u9017\u53f7\u5c06\u53d8\u91cf\u540d\u5206\u5272\u5f00\u6765\u5373\u53ef\u3002\u4e0b\u9762\u4e24\u4e2a\u4ee3\u7801\u7247\u6bb5\u7684\u6548\u679c\u662f\u4e00\u6837\u7684\uff1a 1 2 int a ; int b ; 1 int a , b ; \u5982\u679c\u5c5e\u4e8e\u4e0a\u8ff0\u65b9\u5f0f\u6765\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\uff0c\u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u4f1a\u72af\u4e0b\u4e24\u79cd\u9519\u8bef\uff08\u8fd9\u6ca1\u6709\u4ec0\u4e48\u5927\u4e0d\u4e86\u7684\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u4f1a\u6355\u83b7\u8fd9\u4e9b\u9519\u8bef\u5e76\u8981\u6c42\u4f60\u8fdb\u884c\u4fee\u590d\uff09\uff1a \u7b2c\u4e00\u79cd\u9519\u8bef\u662f\u4e3a\u6bcf\u4e2a\u53d8\u91cf\u90fd\u6dfb\u52a0\u7c7b\u578b\u8bf4\u660e\u3002 1 2 3 int a , int b ; // wrong (compiler error) int a , b ; // correct \u7b2c\u4e8c\u79cd\u9519\u8bef\u662f\u5728\u4e00\u4e2a\u8bed\u53e5\u4e2d\u5b9a\u4e49\u4e24\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u8fd9\u662f\u4e0d\u88ab\u5141\u8bb8\u7684\u3002\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u53d8\u91cf\u5fc5\u987b\u5206\u522b\u5b9a\u4e49\u3002 1 2 3 4 5 6 7 int a , double b ; // wrong (compiler error) int a ; double b ; // correct (but not recommended) // correct and recommended (easier to read) int a ; double b ; \u6700\u4f73\u5b9e\u8df5 \u5c3d\u7ba1\u5728\u8bed\u8a00\u5c42\u9762\uff0c\u4f7f\u7528\u5355\u4e00\u8bed\u53e5\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\u662f\u88ab\u5141\u8bb8\u7684\uff0c\u4f46\u662f\u5728\u5b9e\u8df5\u4e2d\u5e94\u5f53\u5c3d\u91cf\u907f\u514d\u8fd9\u6837\u53bb\u505a\uff08\u5373\u65f6\u5b83\u4eec\u5177\u6709\u76f8\u540c\u7684\u7c7b\u578b\uff09\u3002\u6b63\u786e\u7684\u505a\u6cd5\u662f\u4e00\u4e2a\u8bed\u53e5\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\uff08\u5e76\u4f7f\u7528\u5355\u884c\u6ce8\u91ca\u89e3\u91ca\u8be5\u53d8\u91cf\u7684\u7528\u9014\uff09\u3002","title":"\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf"},{"location":"1-3-Introduction-to-objects-and-variables/#_6","text":"\u5728 C++ \u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u53d8\u91cf\u6765\u8bbf\u95ee\u5185\u5b58\u3002\u53d8\u91cf\u5177\u6709\u6807\u8bc6\u7b26\u3001\u7c7b\u578b\u548c\u503c\uff08\u4ee5\u53ca\u5176\u4ed6\u4e00\u4e9b\u4e0e\u6b64\u5904\u4e0d\u76f8\u5173\u7684\u5c5e\u6027\uff09\u3002\u53d8\u91cf\u7c7b\u578b\u7528\u4e8e\u786e\u5b9a\u5185\u5b58\u4e2d\u7684\u503c\u5e94\u8be5\u88ab\u5982\u4f55\u89e3\u6790\u3002 \u5728\u4e0b\u4e00\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5982\u4f55\u4e3a\u53d8\u91cf\u8d4b\u503c\u5e76\u4f7f\u7528\u5b83\u4eec\u3002","title":"\u5c0f\u7ed3"},{"location":"1-4-Variable-assignment-and-initialization/","tags":["variable","initialization"],"text":"\u5728\u4e0a\u4e00\u8282\u8bfe 1.3 \u5bf9\u8c61\u548c\u53d8\u91cf \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u3002\u5728\u8fd9\u4e00\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5982\u4f55\u4e3a\u53d8\u91cf\u8d4b\u503c\u5e76\u4f7f\u7528\u5b83\u5b58\u50a8\u7684\u503c\u3002 \u9996\u5148\u56de\u5fc6\u4e00\u4e0b\uff0c\u4e0b\u9762\u8fd9\u4e2a\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c\u6211\u4eec\u5728\u5185\u5b58\u4e2d\u4e3a\u4e00\u4e2a\u540d\u4e3a x \u7684\u6574\u578b\u53d8\u91cf\u5206\u914d\u4e86\u5185\u5b58\uff0c\u7136\u540e\u7c7b\u4f3c\u5730\uff0c\u53c8\u521b\u5efa\u4e86\u4e24\u4e2a\u6574\u578b\u53d8\u91cf\uff0c\u5206\u522b\u53eb\u505a y \u548c z\uff1a 1 2 int x ; // define an integer variable named x int y , z ; // define two integer variables, named y and z \u53d8\u91cf\u8d4b\u503c \u53d8\u91cf\u521b\u5efa\u5b8c\u6bd5\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd0\u7b97\u7b26 = \u4e3a\u5176\u8d4b\u503c\uff08\u5728\u53e6\u5916\u4e00\u4e2a\u5355\u72ec\u7684\u8bed\u53e5\u4e2d\uff09\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u79f0\u4e3a\u62f7\u8d1d\u8d4b\u503c\uff08\u6216\u8d4b\u503c\uff09\u3002 1 2 3 4 int width ; // define an integer variable named width width = 5 ; // copy assignment of value 5 into variable width // variable width now has value 5 \u62f7\u8d1d\u8d4b\u503c\u540d\u5b57\u7684\u6765\u5386\uff0c\u6e90\u4e8e\u4e0a\u8ff0\u64cd\u4f5c\u5c06\u8d4b\u503c\u8fd0\u7b97\u7b26\u53f3\u4fa7\u7684\u503c\u62f7\u8d1d\u540e\u8d4b\u503c\u7ed9\u4e86\u5de6\u4fa7\u7684\u53d8\u91cf\u3002= \u79f0\u4e3a\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5148\u540e\u4e24\u6b21\u4f7f\u7528\u4e86\u8d4b\u503c\u64cd\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int width ; width = 5 ; // copy assignment of value 5 into variable width // variable width now has value 5 width = 7 ; // change value stored in variable width to 7 // variable width now has value 7 return 0 ; } \u5728\u6211\u4eec\u628a 7 \u8d4b\u503c\u7ed9\u53d8\u91cf width \u7684\u65f6\u5019\uff0c\u4e4b\u524d\u7684\u503c\uff085\uff09\u5c31\u88ab\u8986\u76d6\u6389\u4e86\u3002\u4e00\u822c\u7684\u53d8\u91cf\u53ea\u80fd\u591f\u5b58\u50a8\u4e00\u4e2a\u503c\u3002 \u6ce8\u610f \u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u72af\u7684\u9519\u8bef\u4e4b\u4e00\u5c31\u662f\u5c06\u8d4b\u503c\u8fd0\u7b97\u7b26( = )\u548c\u76f8\u7b49\u6bd4\u8f83\u8fd0\u7b97\u7b26( == )\u5f04\u6df7\u6dc6\u3002\u8d4b\u503c\u8fd0\u7b97\u7b26\u7528\u4e8e\u7ed9\u53d8\u91cf\u8d4b\u503c\uff0c\u800c\u76f8\u7b49\u6bd4\u8f83\u8fd0\u7b97\u7b26\u5219\u7528\u4e8e\u6821\u9a8c\u4e24\u4e2a\u64cd\u4f5c\u6570\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002 \u521d\u59cb\u5316 \u4e0a\u8ff0\u8d4b\u503c\u8fc7\u7a0b\u7684\u7f3a\u70b9\u4e4b\u4e00\uff0c\u662f\u5b83\u9700\u8981\u4e24\u6761\u8bed\u53e5\u624d\u80fd\u5b8c\u6210\uff1a\u9996\u5148\u5b9a\u4e49\u53d8\u91cf\uff0c\u7136\u540e\u4e3a\u53d8\u91cf\u8d4b\u503c\u3002 \u8fd9\u4e24\u6b65\u5176\u5b9e\u662f\u53ef\u4ee5\u5408\u5e76\u7684\u3002\u5728\u5b9a\u4e49\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u8fd9\u4e2a\u53d8\u91cf\u8bbe\u7f6e\u4e00\u4e2a\u521d\u59cb\u503c\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a\u521d\u59cb\u5316\u3002\u7528\u4e8e\u521d\u59cb\u5316\u53d8\u91cf\u7684\u8fd9\u4e2a\u503c\uff0c\u79f0\u4e3a initializer \u3002 C++ \u4e2d\u7684\u521d\u59cb\u5316\u662f\u51fa\u5947\u7684\u590d\u6742\uff0c\u8fd9\u91cc\u5411\u4f60\u5c55\u793a\u4e00\u4e2a\u7b80\u5316\u540e\u7684\u7248\u672c\u3002 \u5728 C++ \u4e2d\u6709 4 \u79cd\u57fa\u672c\u7684\u53d8\u91cf\u521d\u59cb\u5316\u65b9\u6cd5\uff1a 1 2 3 4 int a ; // \u65e0\u521d\u59cb\u5316\u503c int b = 5 ; // \u5728\u7b49\u53f7\u540e\u653e\u7f6e\u521d\u59cb\u5316\u503c int c ( 6 ); // \u5728\u62ec\u53f7\u4e2d\u653e\u7f6e\u521d\u59cb\u5316\u503c int d { 7 }; // \u5728\u82b1\u62ec\u53f7\u4e2d\u653e\u7f6e\u521d\u59cb\u5316\u503c \u4f60\u53ef\u80fd\u5df2\u7ecf\u6ce8\u610f\u5230\u4e86\u4e0a\u8ff0\u4ee3\u7801\u7247\u6bb5\u4e2d\u989d\u5916\u7684\u7a7a\u683c\uff08\u4f8b\u5982 int d{7}; \uff09\u3002\u662f\u5426\u4f7f\u7528\u8fd9\u4e9b\u7a7a\u683c\u6765\u589e\u5f3a\u53ef\u8bfb\u6027\uff0c\u5c5e\u4e8e\u4e00\u79cd\u4e2a\u4eba\u504f\u597d\u3002 \u9ed8\u8ba4\u521d\u59cb\u5316 \u5728\u4e0d\u63d0\u4f9b\u4efb\u4f55\u521d\u59cb\u5316\u503c\u7684\u65f6\u5019\uff08\u4f8b\u5982\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u53d8\u91cf a \uff09\uff0c\u5c06\u8fdb\u884c\u9ed8\u8ba4\u521d\u59cb\u5316\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u9ed8\u8ba4\u521d\u59cb\u5316\u4f1a\u5bfc\u81f4\u53d8\u91cf\u5b58\u653e\u4e00\u4e2a\u4e0d\u786e\u5b9a\u503c\u3002\u76f8\u5173\u5185\u5bb9\u6211\u4eec\u4f1a\u5728 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u62f7\u8d1d\u521d\u59cb\u5316 \u5982\u679c\u7b49\u53f7\u540e\u9762\u63d0\u4f9b\u4e86\u521d\u59cb\u5316\u503c\uff0c\u6b64\u65f6\u5c06\u8fdb\u884c\u62f7\u8d1d\u521d\u59cb\u5316\u3002\u62f7\u8d1d\u521d\u59cb\u5316\u7ee7\u627f\u81ea C \u8bed\u8a00\u3002 1 int width = 5 ; // copy initialization of value 5 into variable width \u548c\u62f7\u8d1d\u8d4b\u503c\u5f88\u7c7b\u4f3c\uff0c\u62f7\u8d1d\u521d\u59cb\u5316\u4f1a\u5c06\u7b49\u53f7\u53f3\u8fb9\u7684\u503c\u62f7\u8d1d\u5230\u5de6\u4fa7\u521b\u5efa\u7684\u53d8\u91cf\u3002 \u5bf9\u4e8e\u4e0a\u8ff0\u7b80\u5355\u7c7b\u578b int \u800c\u8a00\uff0c\u62f7\u8d1d\u521d\u59cb\u5316\u662f\u975e\u5e38\u9ad8\u6548\u7684\u3002\u7136\u800c\uff0c\u5728\u5904\u7406\u590d\u6742\u7c7b\u578b\u65f6\uff0c\u62f7\u8d1d\u521d\u59cb\u5316\u5219\u662f\u4f4e\u6548\u7684\u3002 \u76f4\u63a5\u521d\u59cb\u5316 \u5f53\u6211\u4eec\u5c06\u521d\u59cb\u5316\u503c\u7f6e\u4e8e\u62ec\u53f7\u5185\u65f6\uff0c\u5c06\u8fdb\u884c\u76f4\u63a5\u521d\u59cb\u5316\u3002 1 int width ( 5 ); // direct initialization of value 5 into variable width \u5bf9\u4e8e int \u8fd9\u6837\u7684\u7b80\u5355\u6570\u636e\u7c7b\u578b\u800c\u8a00\uff0c\u62f7\u8d1d\u521d\u59cb\u5316\u548c\u76f4\u63a5\u521d\u59cb\u5316\u672c\u8d28\u4e0a\u51e0\u4e4e\u6ca1\u4ec0\u4e48\u533a\u522b\u3002\u5bf9\u4e8e\u590d\u6742\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u76f4\u63a5\u521d\u59cb\u5316\u76f8\u5bf9\u4e8e\u62f7\u8d1d\u521d\u59cb\u5316\u5219\u66f4\u52a0\u9ad8\u6548\u3002 \u62ec\u53f7\u521d\u59cb\u5316 \u4e0d\u5e78\u7684\u662f\uff0c\u76f4\u63a5\u521d\u59cb\u5316\u5e76\u4e0d\u9002\u7528\u4e8e\u6240\u6709\u7c7b\u578b\uff08\u4f8b\u5982\u521d\u59cb\u5316\u4e00\u4e2a\u5305\u542b\u4e00\u4e2a\u5217\u8868\u7684\u5bf9\u8c61\uff09\u3002\u4e3a\u4e86\u63d0\u4f9b\u66f4\u4e3a\u4e00\u81f4\u7684\u521d\u59cb\u5316\u673a\u5236\uff0cC++ \u652f\u6301\u4e86\u57fa\u4e8e\u82b1\u62ec\u53f7\u7684 \u62ec\u53f7\u521d\u59cb\u5316 \uff08\u4e5f\u79f0\u4e3a \u7edf\u4e00\u521d\u59cb\u5316 \u6216 \u5217\u8868\u521d\u59cb\u5316 \uff09\u3002 \u62ec\u53f7\u521d\u59cb\u5316\u6709\u4e09\u79cd\u5f62\u5f0f\u3002 1 2 3 int width { 5 }; // \u76f4\u63a5\u62ec\u53f7\u521d\u59cb\u5316\uff08\u63a8\u8350\uff09 int height = { 6 }; // \u62f7\u8d1d\u62ec\u53f7\u521d\u59cb\u5316 int depth {}; // \u503c\u521d\u59cb\u5316\uff08\u8be6\u89c1\u4e0b\u8282\uff09 \u76f4\u63a5\u62ec\u53f7\u521d\u59cb\u5316\u548c\u62f7\u8d1d\u62ec\u53f7\u521d\u59cb\u5316\u51e0\u4e4e\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u4f46\u901a\u5e38\u66f4\u63a8\u8350\u76f4\u63a5\u62ec\u53f7\u521d\u59cb\u5316\u3002 \u62ec\u53f7\u521d\u59cb\u5316\u7684\u4e00\u4e2a\u989d\u5916\u4f18\u70b9\u662f\uff0c \u62ec\u53f7\u521d\u59cb\u5316\u4e0d\u5141\u8bb8\u9690\u5f0f\u7f29\u7a84\u8f6c\u6362 \u7684\u53d1\u751f\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u62ec\u53f7\u521d\u59cb\u5316\u53bb\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u5982\u679c\u521d\u59cb\u5316\u503c\u5e76\u4e0d\u80fd\u88ab\u201d\u5b89\u5168\u7684\u201c\u5b58\u50a8\uff0c\u7f16\u8bd1\u5668\u4f1a\u629b\u51fa\u4e00\u4e2a\u544a\u8b66\u6216\u9519\u8bef\u3002\u4f8b\u5982\uff1a 1 int width { 4.5 }; // \u9519\u8bef: not all double values fit into an int \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c\u6211\u4eec\u5c1d\u8bd5\u5c06\u4e00\u4e2a\u5305\u542b\u4e86\u5c0f\u6570\u90e8\u5206\u7684\u503c 4.5 \u8d4b\u503c\u7ed9\u4e00\u4e2a\u6574\u578b\u53d8\u91cf\uff08\u53ea\u80fd\u5b58\u50a8\u6574\u6570\u90e8\u5206\uff09\u3002\u62f7\u8d1d\u548c\u76f4\u63a5\u521d\u59cb\u5316\u90fd\u4f1a\u4e22\u5f03\u5c0f\u6570\u90e8\u5206\uff0c\u6700\u7ec8\u7ed3\u679c\u5c31\u662f\u6b63\u6570\u90e8\u5206 4 \u88ab\u5b58\u50a8\u5728\u4e86\u53d8\u91cf width \u4e2d\u3002\u800c\u5bf9\u4e8e\u62ec\u53f7\u521d\u59cb\u5316\uff0c\u4e0a\u8ff0\u64cd\u4f5c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\uff08\u901a\u5e38\u6765\u8bb2\u8fd9\u662f\u4ef6\u597d\u4e8b\uff0c\u56e0\u4e3a\u5f88\u5c11\u6709\u6545\u610f\u4e22\u5931\u6570\u636e\u7cbe\u5ea6\u7684\u9700\u6c42\uff09\u3002\u4e0d\u4f1a\u9020\u6210\u6f5c\u5728\u6570\u636e\u4e22\u5931\u7684\u7c7b\u578b\u8f6c\u6362\u5219\u662f\u5141\u8bb8\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u53ea\u8981\u6761\u4ef6\u5141\u8bb8\uff0c\u5e94\u8be5\u5c3d\u53ef\u80fd\u4f7f\u7528\u62ec\u53f7\u521d\u59cb\u5316\u3002 Q & A Q\uff1bC++ \u63d0\u4f9b\u4e86\u62f7\u8d1d\u521d\u59cb\u5316\u3001\u76f4\u63a5\u521d\u59cb\u5316\u548c\u62ec\u53f7\u521d\u59cb\u5316\u3002\u90a3\u4e48\u662f\u5426\u5b58\u5728\u76f4\u63a5\u8d4b\u503c\u548c\u62ec\u53f7\u8d4b\u503c\u5462\uff1f A\uff1aC++ \u5e76\u4e0d\u652f\u6301\u76f4\u63a5\u8d4b\u503c\u548c\u62ec\u53f7\u8d4b\u503c\u8bed\u6cd5 \u503c\u521d\u59cb\u5316\u548c\u96f6\u521d\u59cb\u5316 \u5982\u679c\u4f7f\u7528\u7a7a\u7684\u82b1\u62ec\u53f7\u6765\u521d\u59cb\u5316\u53d8\u91cf\uff0c\u5219\u4f1a\u8fdb\u884c\u503c\u521d\u59cb\u5316\uff0c\u503c\u521d\u59cb\u5316\u4f1a\u628a\u53d8\u91cf\u521d\u59cb\u503c\u8bbe\u7f6e\u4e3a 0\uff08\u6216\u8005\u7a7a\uff0c\u5982\u679c\u7a7a\u503c\u5bf9\u4e8e\u7ed9\u5b9a\u7c7b\u578b\u662f\u5408\u9002\u7684\u521d\u503c\u7684\u8bdd\uff09\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u521d\u59cb\u5316\u4e3a\u5168 0\uff0c\u4e5f\u79f0\u4e4b\u4e3a\u96f6\u521d\u59cb\u5316\u3002 1 int width {}; // zero initialization to value 0 Q & A Q: \u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u4f7f\u7528 { 0 } \uff0c\u662f\u4ec0\u4e48\u65f6\u5019\u4f7f\u7528 {}? A: \u5982\u679c\u4f60\u4f7f\u7528\u7684\u7684\u786e\u662f 0 \u8fd9\u4e2a\u5177\u4f53\u503c\uff0c\u90a3\u4e48\u5c31\u5e94\u5f53\u660e\u786e\u5730\u5c06\u5176\u521d\u59cb\u5316\u4e3a 0 1 2 int x { 0 }; // explicit initialization to value 0 std::cout << x; // we're using that zero value A: \u5982\u679c\u521d\u59cb\u5316\u7684\u503c\u53ea\u662f\u4e34\u65f6\u503c\u4e14\u5373\u5c06\u88ab\u66ff\u6362\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u503c\u521d\u59cb\u5316\u3002 1 2 int x {}; // value initialization std::cin >> x; // we're immediately replacing that value \u4e60\u60ef\u6027\u5730\u521d\u59cb\u5316\u53d8\u91cf \u53d8\u91cf\u5728\u521b\u5efa\u65f6\uff0c\u5c31\u5e94\u8be5\u521d\u59cb\u5316\u3002\u4e5f\u8bb8\u4f60\u5c06\u6765\u4f1a\u9047\u5230\u67d0\u79cd\u7279\u4f8b\uff08\u4f8b\u5982\u67d0\u6bb5\u6027\u80fd\u53ca\u5176\u5173\u952e\u7684\u4ee3\u7801\u4e2d\u6240\u5305\u542b\u7684\u5927\u91cf\u53d8\u91cf\uff09\u5141\u8bb8\u4f60\u4e0d\u53bb\u521d\u59cb\u5316\u53d8\u91cf\uff0c\u524d\u63d0\u662f\u4f60\u662f\u6709\u610f\u800c\u4e3a\u4e4b\u7684\uff08\u907f\u514d\u56e0\u4e3a\u5fd8\u8bb0\u4e86\u800c\u6ca1\u6709\u521d\u59cb\u5316\u53d8\u91cf\u7684\u8fd9\u79cd\u60c5\u51b5\uff09 \u5173\u4e8e\u8fd9\u4e2a\u8bdd\u9898\u7684\u8ba8\u8bba\uff0cBjarne Stroustrup (C++\u4e4b\u7236) \u548c Herb Sutter (C++ \u5927\u5e08) \u63d0\u4f9b\u4e86\u4ed6\u4eec\u7684\u89c1\u89e3\uff0c\u8be6\u7ec6\u5185\u5bb9\u8bf7\u53c2\u8003 \u8fd9\u91cc . \u6211\u4eec\u4f1a\u5728 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a \u4e2d\u8be6\u7ec6\u8ba8\u8bba\u5f53\u6211\u4eec\u8bd5\u56fe\u4f7f\u7528\u4e00\u4e2a\u6ca1\u6709\u88ab\u201d\u6070\u5f53\u5b9a\u4e49\u201c\u7684\u53d8\u91cf\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002 !!! question Q&A 1 \u5728\u521b\u5efa\u53d8\u91cf\u65f6\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\u3002 \u521d\u59cb\u5316\u591a\u4e2a\u53d8\u91cf \u5728\u4e0a\u4e00\u8282\u8bfe\u4e2d\u6211\u4eec\u63d0\u5230\uff0cC++\u652f\u6301\u4f7f\u7528\u4e00\u4e2a\u8bed\u53e5\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\uff08\u901a\u8fc7\u9017\u53f7\u5206\u5272\u53d8\u91cf\u540d\uff09\uff1a 1 int a , b ; \u540c\u65f6\u6211\u4eec\u4e5f\u63d0\u5230\uff0c\u6700\u4f73\u5b9e\u8df5\u5176\u5b9e\u662f\u907f\u514d\u4f7f\u7528\u8be5\u8bed\u6cd5\u3002\u4e0d\u8fc7\uff0c\u7531\u4e8e\u6211\u4eec\u96be\u514d\u8fd8\u662f\u4f1a\u9047\u5230\u6b64\u7c7b\u98ce\u683c\u7684\u4ee3\u7801\uff0c\u5bf9\u5b83\u591a\u4e9b\u4e86\u89e3\u4e5f\u672a\u5c1d\u4e0d\u662f\u4ef6\u597d\u4e8b\u3002\u540c\u65f6\u4e5f\u80fd\u591f\u518d\u6b21\u63d0\u9192\u4f60\u907f\u514d\u4f7f\u7528\u8fd9\u79cd\u8bed\u6cd5\u3002 \u4f60\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u521d\u59cb\u5316\u591a\u4e2a\u53d8\u91cf\uff1a 1 2 3 int a = 5 , b = 6 ; // copy initialization int c ( 7 ), d ( 8 ); // direct initialization int e { 9 }, f { 10 }; // brace initialization (preferred) \u4e0d\u5e78\u7684\u662f\uff0c\u8fd9\u91cc\u6709\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u5751\u3002\u5f53\u7a0b\u5e8f\u5458\u9519\u8bef\u5730\u4f7f\u7528\u4e86\u4e00\u4e2a\u521d\u59cb\u5316\u8bed\u53e5\u5e76\u671f\u671b\u5b83\u80fd\u591f\u521d\u59cb\u5316\u591a\u4e2a\u53d8\u91cf\u65f6\uff0c\u9519\u8bef\u4fbf\u53ef\u80fd\u53d1\u751f\uff1a 1 2 3 int a , b = 5 ; // wrong (a is not initialized!) int a = 5 , b = 5 ; // correct \u5728\u7b2c\u4e00\u884c\u8bed\u53e5\u4e2d\uff0c\u53d8\u91cf a \u5e76\u6ca1\u6709\u88ab\u521d\u59cb\u5316\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5bf9\u6b64\u53d1\u51fa\u63d0\u9192\uff0c\u4e5f\u53ef\u80fd\u4e0d\u4f1a\u3002\u5982\u679c\u7f16\u8bd1\u5668\u5ffd\u7565\u4e86\u8be5\u95ee\u9898\uff0c\u90a3\u4e48\u4f60\u7684\u7a0b\u5e8f\u5c06\u53ef\u80fd\u56e0\u6b64\u95f4\u6b47\u6027\u5730\u5d29\u6e83\u6216\u8f93\u51fa\u9519\u8bef\u7ed3\u679c\u3002\u6211\u4eec\u7a0d\u540e\u5c31\u4f1a\u4ecb\u7ecd\u4f7f\u7528\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\u4f1a\u5e26\u6765\u54ea\u4e9b\u95ee\u9898\u3002 \u6709\u4e00\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8bb0\u5fc6\u5e76\u907f\u514d\u4e0a\u8ff0\u95ee\u9898\uff0c\u5982\u679c\u4f60\u8003\u8651\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\u6216\u8005\u62ec\u53f7\u521d\u59cb\u5316\u7684\u573a\u666f\uff0c\u4e0a\u8ff0\u5199\u6cd5\u663e\u800c\u6613\u89c1\u662f\u6709\u95ee\u9898\u7684\uff1a 1 2 int a , b ( 5 ); int c , d { 5 }; \u4f7f\u7528\u4e0a\u9762\u8fd9\u79cd\u5199\u6cd5\u6765\u8bf4\u660e\uff0c\u770b\u8d77\u6765\u66f4\u6e05\u695a\uff0c5 \u88ab\u7528\u4e8e\u521d\u59cb\u5316 b \u6216\u8005 d \uff0c\u800c\u4e0d\u662f a \u6216\u8005 c","title":"1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316"},{"location":"1-4-Variable-assignment-and-initialization/#_1","text":"\u53d8\u91cf\u521b\u5efa\u5b8c\u6bd5\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd0\u7b97\u7b26 = \u4e3a\u5176\u8d4b\u503c\uff08\u5728\u53e6\u5916\u4e00\u4e2a\u5355\u72ec\u7684\u8bed\u53e5\u4e2d\uff09\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u79f0\u4e3a\u62f7\u8d1d\u8d4b\u503c\uff08\u6216\u8d4b\u503c\uff09\u3002 1 2 3 4 int width ; // define an integer variable named width width = 5 ; // copy assignment of value 5 into variable width // variable width now has value 5 \u62f7\u8d1d\u8d4b\u503c\u540d\u5b57\u7684\u6765\u5386\uff0c\u6e90\u4e8e\u4e0a\u8ff0\u64cd\u4f5c\u5c06\u8d4b\u503c\u8fd0\u7b97\u7b26\u53f3\u4fa7\u7684\u503c\u62f7\u8d1d\u540e\u8d4b\u503c\u7ed9\u4e86\u5de6\u4fa7\u7684\u53d8\u91cf\u3002= \u79f0\u4e3a\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5148\u540e\u4e24\u6b21\u4f7f\u7528\u4e86\u8d4b\u503c\u64cd\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int width ; width = 5 ; // copy assignment of value 5 into variable width // variable width now has value 5 width = 7 ; // change value stored in variable width to 7 // variable width now has value 7 return 0 ; } \u5728\u6211\u4eec\u628a 7 \u8d4b\u503c\u7ed9\u53d8\u91cf width \u7684\u65f6\u5019\uff0c\u4e4b\u524d\u7684\u503c\uff085\uff09\u5c31\u88ab\u8986\u76d6\u6389\u4e86\u3002\u4e00\u822c\u7684\u53d8\u91cf\u53ea\u80fd\u591f\u5b58\u50a8\u4e00\u4e2a\u503c\u3002 \u6ce8\u610f \u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u72af\u7684\u9519\u8bef\u4e4b\u4e00\u5c31\u662f\u5c06\u8d4b\u503c\u8fd0\u7b97\u7b26( = )\u548c\u76f8\u7b49\u6bd4\u8f83\u8fd0\u7b97\u7b26( == )\u5f04\u6df7\u6dc6\u3002\u8d4b\u503c\u8fd0\u7b97\u7b26\u7528\u4e8e\u7ed9\u53d8\u91cf\u8d4b\u503c\uff0c\u800c\u76f8\u7b49\u6bd4\u8f83\u8fd0\u7b97\u7b26\u5219\u7528\u4e8e\u6821\u9a8c\u4e24\u4e2a\u64cd\u4f5c\u6570\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002","title":"\u53d8\u91cf\u8d4b\u503c"},{"location":"1-4-Variable-assignment-and-initialization/#_2","text":"\u4e0a\u8ff0\u8d4b\u503c\u8fc7\u7a0b\u7684\u7f3a\u70b9\u4e4b\u4e00\uff0c\u662f\u5b83\u9700\u8981\u4e24\u6761\u8bed\u53e5\u624d\u80fd\u5b8c\u6210\uff1a\u9996\u5148\u5b9a\u4e49\u53d8\u91cf\uff0c\u7136\u540e\u4e3a\u53d8\u91cf\u8d4b\u503c\u3002 \u8fd9\u4e24\u6b65\u5176\u5b9e\u662f\u53ef\u4ee5\u5408\u5e76\u7684\u3002\u5728\u5b9a\u4e49\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u8fd9\u4e2a\u53d8\u91cf\u8bbe\u7f6e\u4e00\u4e2a\u521d\u59cb\u503c\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a\u521d\u59cb\u5316\u3002\u7528\u4e8e\u521d\u59cb\u5316\u53d8\u91cf\u7684\u8fd9\u4e2a\u503c\uff0c\u79f0\u4e3a initializer \u3002 C++ \u4e2d\u7684\u521d\u59cb\u5316\u662f\u51fa\u5947\u7684\u590d\u6742\uff0c\u8fd9\u91cc\u5411\u4f60\u5c55\u793a\u4e00\u4e2a\u7b80\u5316\u540e\u7684\u7248\u672c\u3002 \u5728 C++ \u4e2d\u6709 4 \u79cd\u57fa\u672c\u7684\u53d8\u91cf\u521d\u59cb\u5316\u65b9\u6cd5\uff1a 1 2 3 4 int a ; // \u65e0\u521d\u59cb\u5316\u503c int b = 5 ; // \u5728\u7b49\u53f7\u540e\u653e\u7f6e\u521d\u59cb\u5316\u503c int c ( 6 ); // \u5728\u62ec\u53f7\u4e2d\u653e\u7f6e\u521d\u59cb\u5316\u503c int d { 7 }; // \u5728\u82b1\u62ec\u53f7\u4e2d\u653e\u7f6e\u521d\u59cb\u5316\u503c \u4f60\u53ef\u80fd\u5df2\u7ecf\u6ce8\u610f\u5230\u4e86\u4e0a\u8ff0\u4ee3\u7801\u7247\u6bb5\u4e2d\u989d\u5916\u7684\u7a7a\u683c\uff08\u4f8b\u5982 int d{7}; \uff09\u3002\u662f\u5426\u4f7f\u7528\u8fd9\u4e9b\u7a7a\u683c\u6765\u589e\u5f3a\u53ef\u8bfb\u6027\uff0c\u5c5e\u4e8e\u4e00\u79cd\u4e2a\u4eba\u504f\u597d\u3002","title":"\u521d\u59cb\u5316"},{"location":"1-4-Variable-assignment-and-initialization/#_3","text":"\u5728\u4e0d\u63d0\u4f9b\u4efb\u4f55\u521d\u59cb\u5316\u503c\u7684\u65f6\u5019\uff08\u4f8b\u5982\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u53d8\u91cf a \uff09\uff0c\u5c06\u8fdb\u884c\u9ed8\u8ba4\u521d\u59cb\u5316\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u9ed8\u8ba4\u521d\u59cb\u5316\u4f1a\u5bfc\u81f4\u53d8\u91cf\u5b58\u653e\u4e00\u4e2a\u4e0d\u786e\u5b9a\u503c\u3002\u76f8\u5173\u5185\u5bb9\u6211\u4eec\u4f1a\u5728 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"\u9ed8\u8ba4\u521d\u59cb\u5316"},{"location":"1-4-Variable-assignment-and-initialization/#_4","text":"\u5982\u679c\u7b49\u53f7\u540e\u9762\u63d0\u4f9b\u4e86\u521d\u59cb\u5316\u503c\uff0c\u6b64\u65f6\u5c06\u8fdb\u884c\u62f7\u8d1d\u521d\u59cb\u5316\u3002\u62f7\u8d1d\u521d\u59cb\u5316\u7ee7\u627f\u81ea C \u8bed\u8a00\u3002 1 int width = 5 ; // copy initialization of value 5 into variable width \u548c\u62f7\u8d1d\u8d4b\u503c\u5f88\u7c7b\u4f3c\uff0c\u62f7\u8d1d\u521d\u59cb\u5316\u4f1a\u5c06\u7b49\u53f7\u53f3\u8fb9\u7684\u503c\u62f7\u8d1d\u5230\u5de6\u4fa7\u521b\u5efa\u7684\u53d8\u91cf\u3002 \u5bf9\u4e8e\u4e0a\u8ff0\u7b80\u5355\u7c7b\u578b int \u800c\u8a00\uff0c\u62f7\u8d1d\u521d\u59cb\u5316\u662f\u975e\u5e38\u9ad8\u6548\u7684\u3002\u7136\u800c\uff0c\u5728\u5904\u7406\u590d\u6742\u7c7b\u578b\u65f6\uff0c\u62f7\u8d1d\u521d\u59cb\u5316\u5219\u662f\u4f4e\u6548\u7684\u3002","title":"\u62f7\u8d1d\u521d\u59cb\u5316"},{"location":"1-4-Variable-assignment-and-initialization/#_5","text":"\u5f53\u6211\u4eec\u5c06\u521d\u59cb\u5316\u503c\u7f6e\u4e8e\u62ec\u53f7\u5185\u65f6\uff0c\u5c06\u8fdb\u884c\u76f4\u63a5\u521d\u59cb\u5316\u3002 1 int width ( 5 ); // direct initialization of value 5 into variable width \u5bf9\u4e8e int \u8fd9\u6837\u7684\u7b80\u5355\u6570\u636e\u7c7b\u578b\u800c\u8a00\uff0c\u62f7\u8d1d\u521d\u59cb\u5316\u548c\u76f4\u63a5\u521d\u59cb\u5316\u672c\u8d28\u4e0a\u51e0\u4e4e\u6ca1\u4ec0\u4e48\u533a\u522b\u3002\u5bf9\u4e8e\u590d\u6742\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u76f4\u63a5\u521d\u59cb\u5316\u76f8\u5bf9\u4e8e\u62f7\u8d1d\u521d\u59cb\u5316\u5219\u66f4\u52a0\u9ad8\u6548\u3002","title":"\u76f4\u63a5\u521d\u59cb\u5316"},{"location":"1-4-Variable-assignment-and-initialization/#_6","text":"\u4e0d\u5e78\u7684\u662f\uff0c\u76f4\u63a5\u521d\u59cb\u5316\u5e76\u4e0d\u9002\u7528\u4e8e\u6240\u6709\u7c7b\u578b\uff08\u4f8b\u5982\u521d\u59cb\u5316\u4e00\u4e2a\u5305\u542b\u4e00\u4e2a\u5217\u8868\u7684\u5bf9\u8c61\uff09\u3002\u4e3a\u4e86\u63d0\u4f9b\u66f4\u4e3a\u4e00\u81f4\u7684\u521d\u59cb\u5316\u673a\u5236\uff0cC++ \u652f\u6301\u4e86\u57fa\u4e8e\u82b1\u62ec\u53f7\u7684 \u62ec\u53f7\u521d\u59cb\u5316 \uff08\u4e5f\u79f0\u4e3a \u7edf\u4e00\u521d\u59cb\u5316 \u6216 \u5217\u8868\u521d\u59cb\u5316 \uff09\u3002 \u62ec\u53f7\u521d\u59cb\u5316\u6709\u4e09\u79cd\u5f62\u5f0f\u3002 1 2 3 int width { 5 }; // \u76f4\u63a5\u62ec\u53f7\u521d\u59cb\u5316\uff08\u63a8\u8350\uff09 int height = { 6 }; // \u62f7\u8d1d\u62ec\u53f7\u521d\u59cb\u5316 int depth {}; // \u503c\u521d\u59cb\u5316\uff08\u8be6\u89c1\u4e0b\u8282\uff09 \u76f4\u63a5\u62ec\u53f7\u521d\u59cb\u5316\u548c\u62f7\u8d1d\u62ec\u53f7\u521d\u59cb\u5316\u51e0\u4e4e\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u4f46\u901a\u5e38\u66f4\u63a8\u8350\u76f4\u63a5\u62ec\u53f7\u521d\u59cb\u5316\u3002 \u62ec\u53f7\u521d\u59cb\u5316\u7684\u4e00\u4e2a\u989d\u5916\u4f18\u70b9\u662f\uff0c \u62ec\u53f7\u521d\u59cb\u5316\u4e0d\u5141\u8bb8\u9690\u5f0f\u7f29\u7a84\u8f6c\u6362 \u7684\u53d1\u751f\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u62ec\u53f7\u521d\u59cb\u5316\u53bb\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u5982\u679c\u521d\u59cb\u5316\u503c\u5e76\u4e0d\u80fd\u88ab\u201d\u5b89\u5168\u7684\u201c\u5b58\u50a8\uff0c\u7f16\u8bd1\u5668\u4f1a\u629b\u51fa\u4e00\u4e2a\u544a\u8b66\u6216\u9519\u8bef\u3002\u4f8b\u5982\uff1a 1 int width { 4.5 }; // \u9519\u8bef: not all double values fit into an int \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c\u6211\u4eec\u5c1d\u8bd5\u5c06\u4e00\u4e2a\u5305\u542b\u4e86\u5c0f\u6570\u90e8\u5206\u7684\u503c 4.5 \u8d4b\u503c\u7ed9\u4e00\u4e2a\u6574\u578b\u53d8\u91cf\uff08\u53ea\u80fd\u5b58\u50a8\u6574\u6570\u90e8\u5206\uff09\u3002\u62f7\u8d1d\u548c\u76f4\u63a5\u521d\u59cb\u5316\u90fd\u4f1a\u4e22\u5f03\u5c0f\u6570\u90e8\u5206\uff0c\u6700\u7ec8\u7ed3\u679c\u5c31\u662f\u6b63\u6570\u90e8\u5206 4 \u88ab\u5b58\u50a8\u5728\u4e86\u53d8\u91cf width \u4e2d\u3002\u800c\u5bf9\u4e8e\u62ec\u53f7\u521d\u59cb\u5316\uff0c\u4e0a\u8ff0\u64cd\u4f5c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\uff08\u901a\u5e38\u6765\u8bb2\u8fd9\u662f\u4ef6\u597d\u4e8b\uff0c\u56e0\u4e3a\u5f88\u5c11\u6709\u6545\u610f\u4e22\u5931\u6570\u636e\u7cbe\u5ea6\u7684\u9700\u6c42\uff09\u3002\u4e0d\u4f1a\u9020\u6210\u6f5c\u5728\u6570\u636e\u4e22\u5931\u7684\u7c7b\u578b\u8f6c\u6362\u5219\u662f\u5141\u8bb8\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u53ea\u8981\u6761\u4ef6\u5141\u8bb8\uff0c\u5e94\u8be5\u5c3d\u53ef\u80fd\u4f7f\u7528\u62ec\u53f7\u521d\u59cb\u5316\u3002 Q & A Q\uff1bC++ \u63d0\u4f9b\u4e86\u62f7\u8d1d\u521d\u59cb\u5316\u3001\u76f4\u63a5\u521d\u59cb\u5316\u548c\u62ec\u53f7\u521d\u59cb\u5316\u3002\u90a3\u4e48\u662f\u5426\u5b58\u5728\u76f4\u63a5\u8d4b\u503c\u548c\u62ec\u53f7\u8d4b\u503c\u5462\uff1f A\uff1aC++ \u5e76\u4e0d\u652f\u6301\u76f4\u63a5\u8d4b\u503c\u548c\u62ec\u53f7\u8d4b\u503c\u8bed\u6cd5","title":"\u62ec\u53f7\u521d\u59cb\u5316"},{"location":"1-4-Variable-assignment-and-initialization/#_7","text":"\u5982\u679c\u4f7f\u7528\u7a7a\u7684\u82b1\u62ec\u53f7\u6765\u521d\u59cb\u5316\u53d8\u91cf\uff0c\u5219\u4f1a\u8fdb\u884c\u503c\u521d\u59cb\u5316\uff0c\u503c\u521d\u59cb\u5316\u4f1a\u628a\u53d8\u91cf\u521d\u59cb\u503c\u8bbe\u7f6e\u4e3a 0\uff08\u6216\u8005\u7a7a\uff0c\u5982\u679c\u7a7a\u503c\u5bf9\u4e8e\u7ed9\u5b9a\u7c7b\u578b\u662f\u5408\u9002\u7684\u521d\u503c\u7684\u8bdd\uff09\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u521d\u59cb\u5316\u4e3a\u5168 0\uff0c\u4e5f\u79f0\u4e4b\u4e3a\u96f6\u521d\u59cb\u5316\u3002 1 int width {}; // zero initialization to value 0 Q & A Q: \u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u4f7f\u7528 { 0 } \uff0c\u662f\u4ec0\u4e48\u65f6\u5019\u4f7f\u7528 {}? A: \u5982\u679c\u4f60\u4f7f\u7528\u7684\u7684\u786e\u662f 0 \u8fd9\u4e2a\u5177\u4f53\u503c\uff0c\u90a3\u4e48\u5c31\u5e94\u5f53\u660e\u786e\u5730\u5c06\u5176\u521d\u59cb\u5316\u4e3a 0 1 2 int x { 0 }; // explicit initialization to value 0 std::cout << x; // we're using that zero value A: \u5982\u679c\u521d\u59cb\u5316\u7684\u503c\u53ea\u662f\u4e34\u65f6\u503c\u4e14\u5373\u5c06\u88ab\u66ff\u6362\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u503c\u521d\u59cb\u5316\u3002 1 2 int x {}; // value initialization std::cin >> x; // we're immediately replacing that value","title":"\u503c\u521d\u59cb\u5316\u548c\u96f6\u521d\u59cb\u5316"},{"location":"1-4-Variable-assignment-and-initialization/#_8","text":"\u53d8\u91cf\u5728\u521b\u5efa\u65f6\uff0c\u5c31\u5e94\u8be5\u521d\u59cb\u5316\u3002\u4e5f\u8bb8\u4f60\u5c06\u6765\u4f1a\u9047\u5230\u67d0\u79cd\u7279\u4f8b\uff08\u4f8b\u5982\u67d0\u6bb5\u6027\u80fd\u53ca\u5176\u5173\u952e\u7684\u4ee3\u7801\u4e2d\u6240\u5305\u542b\u7684\u5927\u91cf\u53d8\u91cf\uff09\u5141\u8bb8\u4f60\u4e0d\u53bb\u521d\u59cb\u5316\u53d8\u91cf\uff0c\u524d\u63d0\u662f\u4f60\u662f\u6709\u610f\u800c\u4e3a\u4e4b\u7684\uff08\u907f\u514d\u56e0\u4e3a\u5fd8\u8bb0\u4e86\u800c\u6ca1\u6709\u521d\u59cb\u5316\u53d8\u91cf\u7684\u8fd9\u79cd\u60c5\u51b5\uff09 \u5173\u4e8e\u8fd9\u4e2a\u8bdd\u9898\u7684\u8ba8\u8bba\uff0cBjarne Stroustrup (C++\u4e4b\u7236) \u548c Herb Sutter (C++ \u5927\u5e08) \u63d0\u4f9b\u4e86\u4ed6\u4eec\u7684\u89c1\u89e3\uff0c\u8be6\u7ec6\u5185\u5bb9\u8bf7\u53c2\u8003 \u8fd9\u91cc . \u6211\u4eec\u4f1a\u5728 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a \u4e2d\u8be6\u7ec6\u8ba8\u8bba\u5f53\u6211\u4eec\u8bd5\u56fe\u4f7f\u7528\u4e00\u4e2a\u6ca1\u6709\u88ab\u201d\u6070\u5f53\u5b9a\u4e49\u201c\u7684\u53d8\u91cf\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002 !!! question Q&A 1 \u5728\u521b\u5efa\u53d8\u91cf\u65f6\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\u3002","title":"\u4e60\u60ef\u6027\u5730\u521d\u59cb\u5316\u53d8\u91cf"},{"location":"1-4-Variable-assignment-and-initialization/#_9","text":"\u5728\u4e0a\u4e00\u8282\u8bfe\u4e2d\u6211\u4eec\u63d0\u5230\uff0cC++\u652f\u6301\u4f7f\u7528\u4e00\u4e2a\u8bed\u53e5\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\uff08\u901a\u8fc7\u9017\u53f7\u5206\u5272\u53d8\u91cf\u540d\uff09\uff1a 1 int a , b ; \u540c\u65f6\u6211\u4eec\u4e5f\u63d0\u5230\uff0c\u6700\u4f73\u5b9e\u8df5\u5176\u5b9e\u662f\u907f\u514d\u4f7f\u7528\u8be5\u8bed\u6cd5\u3002\u4e0d\u8fc7\uff0c\u7531\u4e8e\u6211\u4eec\u96be\u514d\u8fd8\u662f\u4f1a\u9047\u5230\u6b64\u7c7b\u98ce\u683c\u7684\u4ee3\u7801\uff0c\u5bf9\u5b83\u591a\u4e9b\u4e86\u89e3\u4e5f\u672a\u5c1d\u4e0d\u662f\u4ef6\u597d\u4e8b\u3002\u540c\u65f6\u4e5f\u80fd\u591f\u518d\u6b21\u63d0\u9192\u4f60\u907f\u514d\u4f7f\u7528\u8fd9\u79cd\u8bed\u6cd5\u3002 \u4f60\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u521d\u59cb\u5316\u591a\u4e2a\u53d8\u91cf\uff1a 1 2 3 int a = 5 , b = 6 ; // copy initialization int c ( 7 ), d ( 8 ); // direct initialization int e { 9 }, f { 10 }; // brace initialization (preferred) \u4e0d\u5e78\u7684\u662f\uff0c\u8fd9\u91cc\u6709\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u5751\u3002\u5f53\u7a0b\u5e8f\u5458\u9519\u8bef\u5730\u4f7f\u7528\u4e86\u4e00\u4e2a\u521d\u59cb\u5316\u8bed\u53e5\u5e76\u671f\u671b\u5b83\u80fd\u591f\u521d\u59cb\u5316\u591a\u4e2a\u53d8\u91cf\u65f6\uff0c\u9519\u8bef\u4fbf\u53ef\u80fd\u53d1\u751f\uff1a 1 2 3 int a , b = 5 ; // wrong (a is not initialized!) int a = 5 , b = 5 ; // correct \u5728\u7b2c\u4e00\u884c\u8bed\u53e5\u4e2d\uff0c\u53d8\u91cf a \u5e76\u6ca1\u6709\u88ab\u521d\u59cb\u5316\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5bf9\u6b64\u53d1\u51fa\u63d0\u9192\uff0c\u4e5f\u53ef\u80fd\u4e0d\u4f1a\u3002\u5982\u679c\u7f16\u8bd1\u5668\u5ffd\u7565\u4e86\u8be5\u95ee\u9898\uff0c\u90a3\u4e48\u4f60\u7684\u7a0b\u5e8f\u5c06\u53ef\u80fd\u56e0\u6b64\u95f4\u6b47\u6027\u5730\u5d29\u6e83\u6216\u8f93\u51fa\u9519\u8bef\u7ed3\u679c\u3002\u6211\u4eec\u7a0d\u540e\u5c31\u4f1a\u4ecb\u7ecd\u4f7f\u7528\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\u4f1a\u5e26\u6765\u54ea\u4e9b\u95ee\u9898\u3002 \u6709\u4e00\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8bb0\u5fc6\u5e76\u907f\u514d\u4e0a\u8ff0\u95ee\u9898\uff0c\u5982\u679c\u4f60\u8003\u8651\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\u6216\u8005\u62ec\u53f7\u521d\u59cb\u5316\u7684\u573a\u666f\uff0c\u4e0a\u8ff0\u5199\u6cd5\u663e\u800c\u6613\u89c1\u662f\u6709\u95ee\u9898\u7684\uff1a 1 2 int a , b ( 5 ); int c , d { 5 }; \u4f7f\u7528\u4e0a\u9762\u8fd9\u79cd\u5199\u6cd5\u6765\u8bf4\u660e\uff0c\u770b\u8d77\u6765\u66f4\u6e05\u695a\uff0c5 \u88ab\u7528\u4e8e\u521d\u59cb\u5316 b \u6216\u8005 d \uff0c\u800c\u4e0d\u662f a \u6216\u8005 c","title":"\u521d\u59cb\u5316\u591a\u4e2a\u53d8\u91cf"},{"location":"1-5-Introduction-to-iostream-cout-cin-and-endl/","tags":["iostream"],"text":"\u672c\u8282\u5c06\u91cd\u70b9\u4ecb\u7ecd std::cout \u3002\u6211\u4eec\u5728 Hello world! \u7a0b\u5e8f\u4e2d\u5df2\u7ecf\u4f7f\u7528\u5b83\u5411\u63a7\u5236\u53f0\u8f93\u51fa\u8fc7\u6587\u672c Hello world! \u3002\u6211\u4eec\u8fd8\u5c06\u4ecb\u7ecd\u5982\u4f55\u4ece\u7528\u6237\u83b7\u53d6\u8f93\u5165\uff0c\u4f7f\u7a0b\u5e8f\u5177\u5907\u4e0e\u7528\u6237\u4ea4\u4e92\u7684\u80fd\u529b\u3002 \u8f93\u5165\u8f93\u51fa\u5e93 \u8f93\u5165\u8f93\u51fa\u5e93 ( IO \u5e93) \u662f C++ \u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u7528\u4e8e\u5904\u7406\u57fa\u672c\u7684\u8f93\u5165\u548c\u8f93\u51fa\u3002\u6211\u4eec\u4f1a\u4f7f\u7528\u8be5\u5e93\u63d0\u4f9b\u7684\u529f\u80fd\u4ece\u952e\u76d8\u83b7\u53d6\u8f93\u5165\u5e76\u5411\u63a7\u5236\u53f0\u8f93\u51fa\u6570\u636e\u3002 iostream \u4e2d\u7684 io \u6307\u4ee3\u7684\u662f\u8f93\u5165\u8f93\u51fa\uff08 input/output \uff09\u3002 \u4e3a\u4e86\u4f7f\u7528 iostream \u5e93\u63d0\u4f9b\u7684\u529f\u80fd\uff0c\u6211\u4eec\u9700\u8981\u5728\u4efb\u4f55\u9700\u8981\u4f7f\u7528\u8be5\u5e93\u7684\u6e90\u6587\u4ef6\u7684\u9876\u90e8\u5305\u542b iostream \u7684\u5934\u6587\u4ef6\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 #include // rest of code that uses iostream functionality here std:: cout iostream \u5e93\u4e2d\u5305\u542b\u4e86\u4e00\u4e9b\u9884\u5b9a\u4e49\u7684\u53d8\u91cf\u4f9b\u6211\u4eec\u4f7f\u7528\uff0c\u5176\u4e2d\u6700\u6709\u7528\u7684\u5f53\u5c5e std::cout \uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u5411\u63a7\u5236\u53f0\u6253\u5370\u6587\u672c\uff0c cout \u4ee3\u8868\u7684\u542b\u4e49\u5c31\u662f\u5b57\u7b26\u8f93\u51fa\uff08character output\uff09\u3002 \u63d0\u9192\u4e00\u4e0b\uff0c\u4e0b\u9762\u65e2\u662f\u4e4b\u524d\u7684 Hello world \u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 #include // for std::cout int main () { std :: cout << \"Hello world!\" ; // print Hello world! to console return 0 ; } \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c iostream \u5df2\u7ecf\u88ab\u5305\u542b\u4e86\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u8bbf\u95ee std::cout \u3002\u5728\u4e3b\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 std::cout \uff0c\u5e76\u914d\u5408\u63d2\u5165\u8fd0\u7b97\u7b26\uff08 << \uff09\u5c06\u6587\u672c Hello world!\u53d1\u9001\u5230\u63a7\u5236\u53f0\u5e76\u6253\u5370\u51fa\u6765\u3002 std::cout \u4e0d\u4ec5\u53ef\u4ee5\u6253\u5370\u6587\u672c\uff0c\u8fd8\u53ef\u4ee5\u6253\u5370\u6570\u5b57\uff1a 1 2 3 4 5 6 7 8 #include // for std::cout int main () { std :: cout << 4 ; // print 4 to console return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 4 \u5b83\u8fd8\u53ef\u4ee5\u7528\u6765\u6253\u5370\u53d8\u91cf\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 #include // for std::cout int main () { int x { 5 }; // define integer variable x, initialized with value 5 std :: cout << x ; // print value of x (5) to console return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 5 \u4e3a\u4e86\u5728\u4e00\u884c\u4e2d\u6253\u5370\u591a\u4e2a\u5185\u5bb9\uff0c\u53ef\u4ee5\u591a\u6b21\u4f7f\u7528\u63d2\u5165\u8fd0\u7b97\u7b26\u5c06\u591a\u4e2a\u8981\u8f93\u51fa\u7684\u5185\u5bb9\u8fde\u63a5\u8d77\u6765\u5e76\u8f93\u51fa\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 #include // for std::cout int main () { std :: cout << \"Hello\" << \" world!\" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Hello world! \u53e6\u5916\u4e00\u4e2a\u4f8b\u5b50\u5982\u4e0b\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728\u4e00\u6761\u8bed\u53e5\u4e2d\u540c\u65f6\u6253\u5370\u4e86\u6587\u672c\u548c\u53d8\u91cf\u503c\uff1a 1 2 3 4 5 6 7 8 #include // for std::cout int main () { int x { 5 }; std :: cout << \"x is equal to: \" << x ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 x is equal to: 5 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 \u8ba8\u8bba std:: \u524d\u7f00\u7684\u4f5c\u7528 std::endl \u4f60\u89c9\u5f97\u4e0b\u9762\u7684\u4ee3\u7801\u6253\u5370\u7684\u7ed3\u679c\u662f\u4ec0\u4e48\u5462\uff1f 1 2 3 4 5 6 7 8 #include // for std::cout int main () { std :: cout << \"Hi!\" ; std :: cout << \"My name is Alex.\" ; return 0 ; } \u770b\u5230\u4e0b\u9762\u7684\u7ed3\u679c\u4f60\u4f1a\u89c9\u5f97\u5403\u60ca\u5417\uff1a 1 Hi!My name is Alex. \u663e\u7136\uff0c\u5c06\u4e00\u6761\u8bed\u53e5\u62c6\u5206\u4e3a\u4e24\u884c\uff0c\u5e76\u4e0d\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u7ed3\u679c\u3002 \u5982\u679c\u6211\u4eec\u5e0c\u671b\u5206\u884c\u6253\u5370\uff0c\u5219\u5fc5\u987b\u8981\u544a\u77e5\u63a7\u5236\u53f0\u5c06\u5149\u6807\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\u3002 \u4e00\u79cd\u65b9\u5f0f\u662f\u4f7f\u7528 std::endl \u3002\u5f53\u4f7f\u7528 std::cout \u8fdb\u884c\u8f93\u51fa\u65f6\uff0c std::endl \u4f1a\u6253\u5370\u4e00\u4e2a\u6362\u884c\u7b26\uff08\u4fc3\u4f7f\u5149\u6807\u88ab\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\u7684\u5f00\u5934\uff09\u3002\u56e0\u6b64\uff0c endl \u8868\u793a\u7684\u662f\u7ed3\u675f\u8be5\u884c\uff08end line\uff09\u3002 \u4f8b\u7a0b\uff1a 1 2 3 4 5 6 7 8 9 #include // for std::cout and std::endl int main () { std :: cout << \"Hi!\" << std :: endl ; // std::endl will cause the cursor to move to the next line of the console std :: cout << \"My name is Alex.\" << std :: endl ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 Hi! My name is Alex. \u5c0f\u8d34\u58eb \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u7b2c\u4e8c\u4e2a std::endl \u4ece\u6280\u672f\u4e0a\u8bb2\u5e76\u65e0\u5fc5\u8981\uff0c\u56e0\u4e3a\u5728\u6267\u884c\u5b8c\u8fd9\u884c\u4ee3\u7801\u540e\u7a0b\u5e8f\u4f1a\u7acb\u5373\u505c\u6b62\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u5176\u5b9e\u8fd8\u6709\u4e24\u4e2a\u6709\u7528\u7684\u76ee\u7684\uff1a\u9996\u5148\uff0c\u5b83\u53ef\u4ee5\u8868\u660e\u8be5\u884c\u7684\u5185\u5bb9\u5df2\u7ecf\u88ab\u201d\u5b8c\u6574\u8f93\u51fa\u201c \uff08\u4e0e\u4e4b\u76f8\u5bf9\u7684\u662f\u90e8\u5206\u8f93\u51fa\uff0c\u5373\u540e\u7eed\u4ee3\u7801\u4e2d\u8fd8\u6709\u9700\u8981\u8f93\u51fa\u7684\u90e8\u5206\uff09\u3002\u5176\u6b21\uff0c\u5982\u679c\u5c06\u6765\u6211\u4eec\u5728\u540e\u9762\u6dfb\u52a0\u5176\u4ed6\u989d\u5916\u7684\u8f93\u51fa\uff0c\u5c31\u4e0d\u9700\u8981\u4fee\u6539\u5df2\u6709\u7684\u4ee3\u7801\u4e86\u3002\u56e0\u6b64\u4e0d\u59a8\u52a0\u4e0a\u5b83\u3002 std:: endl vs \\n \u4f7f\u7528 std::endl \u6362\u884c\u7684\u6548\u7387\u7a0d\u5fae\u6709\u70b9\u4f4e\uff0c\u56e0\u4e3a\u5b83\u901a\u5e38\u9700\u8981\u5b8c\u6210\u4e24\u4ef6\u4e8b\uff1a\u5c06\u5149\u6807\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\uff0c\u7136\u540e\u786e\u4fdd\u8f93\u51fa\u7ed3\u679c\u9a6c\u4e0a\u663e\u793a\u5728\u5c4f\u5e55\u4e0a\uff08\u79f0\u4e3a\u5237\u65b0\u8f93\u51fa\uff09\u3002\u5f53\u4f7f\u7528 std::cout \u8fdb\u884c\u8f93\u51fa\u65f6\uff0c std::cout \u672c\u6765\u5c31\u4f1a\u5237\u65b0\u8f93\u51fa\uff08\u5373\u4f7f\u6ca1\u6709\u5237\u65b0\uff0c\u901a\u5e38\u4e5f\u4e0d\u4f1a\u4ea7\u751f\u4ec0\u4e48\u95ee\u9898\uff09\u3002\u56e0\u6b64\uff0c\u4f7f\u7528 std::endl \u6765\u5237\u65b0\u8f93\u51fa\u5c31\u6709\u4e9b\u591a\u4f59\u4e86\u3002 \u56e0\u6b64\uff0c\u4f7f\u7528\u6362\u884c\u5b57\u7b26( \\n )\u4e00\u822c\u6765\u8bb2\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u2018\\n\u2019 \u7b26\u53f7\u4f1a\u5c06\u5149\u6807\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\uff0c\u4f46\u662f\u5b83\u5e76\u4e0d\u4f1a\u8bf7\u6c42\u5237\u65b0\uff0c\u56e0\u6b64\u5728\u65e0\u9700\u7279\u522b\u5237\u65b0\u65f6\u53ef\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\u3002\u2018\\n\u2019 \u5b57\u7b26\u8fd8\u66f4\u6613\u8bfb\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u66f4\u7b80\u6d01\uff0c\u800c\u4e14\u8fd8\u53ef\u4ee5\u5d4c\u5165\u5728\u5df2\u6709\u7684\u6587\u672c\u4e2d\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u5c55\u73b0\u4e86\u4f7f\u7528 \u2018\\n\u2019 \u7684\u4e24\u79cd\u4e0d\u540c\u65b9\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 #include // for std::cout int main () { int x { 5 }; std :: cout << \"x is equal to: \" << x << '\\n' ; // Using '\\n' standalone std :: cout << \"And that's all, folks! \\n \" ; // Using '\\n' embedded into a double-quoted piece of text (note: no single quotes when used this way) return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 x is equal to: 5 And that's all, folks! \u6ce8\u610f\uff0c\u5728\u5355\u72ec\u4f7f\u7528\u6362\u884c\u7b26 \u2018\\n\u2019 \u7684\u65f6\u5019\uff0c\u5355\u5f15\u53f7\u662f\u5fc5\u987b\u7684\u3002\u5f53\u5d4c\u5165\u5728\u5176\u4ed6\u5df2\u7ecf\u88ab\u53cc\u5f15\u53f7\u5305\u88f9\u7684\u6587\u672c\u4e2d\u65f6\uff0c\u5219\u65e0\u9700\u518d\u4f7f\u7528\u5355\u5f15\u53f7\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u66f4\u8be6\u7ec6\u7684\u4ecb\u7ecd\u6362\u884c\u7b26 \u2018\\n\u2019 ( 4.11 - \u5b57\u7b26 )\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u8f93\u51fa\u65f6\u4f18\u5148\u4f7f\u7528 \u2018\\n\u2019 \u800c\u4e0d\u662f std::endl \u6ce8\u610f \u2018\\n\u2019\u4f7f\u7528\u7684\u662f\u53cd\u659c\u6760\uff08\u548c\u5176\u4ed6\u7279\u6b8a\u5b57\u7b26\u4e00\u6837\uff09\uff0c\u800c\u4e0d\u662f\u659c\u6760\u3002\u4f7f\u7528\u659c\u6760\uff08\u4f8b\u5982 '/n'\uff09\u53ef\u80fd\u4f1a\u5e26\u6765\u65e0\u6cd5\u9884\u6599\u7684\u7ed3\u679c\u3002 std:: cin std::cin \u662f isstream \u4e2d\u9884\u5b9a\u4e49\u7684\u53e6\u5916\u4e00\u4e2a\u53d8\u91cf\u3002\u4e0e\u7528\u4e8e\u8f93\u51fa\u7684 std::cout \u4e0d\u540c\uff0c std::cin (\u8868\u793a\u5b57\u7b26\u8f93\u5165\uff0c\u201ccharacter input\u201d) \u914d\u5408\u63d0\u53d6\u8fd0\u7b97\u7b26( >> )\uff0c\u53ef\u4ee5\u4ece\u952e\u76d8\u8bfb\u53d6\u8f93\u5165\u3002\u5f53\u7136\uff0c\u8f93\u5165\u7684\u7ed3\u679c\u5fc5\u987b\u5b58\u653e\u5728\u53d8\u91cf\u4e2d\u624d\u53ef\u4ee5\u88ab\u4f7f\u7528\u3002 1 2 3 4 5 6 7 8 9 10 11 12 #include // for std::cout and std::cin int main () { std :: cout << \"Enter a number: \" ; // ask user for a number int x { }; // define variable x to hold user input (and zero-initialize it) std :: cin >> x ; // get number from keyboard and store it in variable x std :: cout << \"You entered \" << x << '\\n' ; return 0 ; } \u8bf7\u5c1d\u8bd5\u81ea\u884c\u7f16\u8bd1\u548c\u8fd0\u884c\u4e0a\u8ff0\u7a0b\u5e8f\u3002\u5728\u6267\u884c\u7a0b\u5e8f\u65f6\uff0c\u7b2c\u4e94\u884c\u4ee3\u7801\u4f1a\u6253\u5370\u201cEnter a number: \u201c\uff0c\u5f53\u6267\u884c\u5230\u7b2c\u516b\u884c\u662f\uff0c\u7a0b\u5e8f\u4f1a\u7b49\u5f85\u7528\u6237\u8f93\u5165\u3002\u5f53\u4f60\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff08\u5e76\u6309\u4e0b\u56de\u8f66\uff09\u540e\uff0c\u5b83\u4f1a\u88ab\u8d4b\u503c\u7ed9\u53d8\u91cf x\u3002\u6700\u540e\uff0c\u7b2c\u5341\u884c\u4ee3\u7801\u4f1a\u6253\u5370 \u201cYou entered \u201d \u4ee5\u53ca\u4f60\u521a\u624d\u8f93\u5165\u7684\u6570\u5b57\u3002 \u4f8b\u5982\uff1a 1 2 Enter a number: 4 You entered 4 \u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u6bd4\u8f83\u7b80\u5355\u5730\u4ece\u7528\u6237\u83b7\u53d6\u8f93\u5165\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u5185\u5bb9\u4e2d\u5927\u91cf\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u3002\u6ce8\u610f\uff0c\u5728\u83b7\u53d6\u8f93\u5165\u65f6\u5e76\u4e0d\u9700\u8981\u6362\u884c\u7b26\uff0c\u56e0\u4e3a\u7528\u6237\u4f1a\u81ea\u884c\u6309\u4e0b\u56de\u8f66\u952e\uff0c\u6b64\u65f6\u5149\u6807\u4fbf\u4f1a\u88ab\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\u3002 \u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u754c\u9762\u5728\u8f93\u5165\u6570\u5b57\u540e\u9a6c\u4e0a\u5c31\u5173\u95ed\u4e86\uff0c\u8bf7\u53c2\u8003 0.8 -- A few common C++ problems \u6765\u83b7\u53d6\u5e2e\u52a9\u3002 \u4e0e\u8f93\u51fa\u7c7b\u4f3c\uff0c\u4e00\u884c\u4ee3\u7801\u4e5f\u53ef\u4ee5\u5b8c\u6210\u591a\u4e2a\u503c\u7684\u8f93\u5165\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // for std::cout and std::cin int main () { std :: cout << \"Enter two numbers separated by a space: \" ; int x { }; // define variable x to hold user input (and zero-initialize it) int y { }; // define variable y to hold user input (and zero-initialize it) std :: cin >> x >> y ; // get two numbers and store in variable x and y respectively std :: cout << \"You entered \" << x << \" and \" << y << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Enter two numbers separated by a space: 5 6 You entered 5 and 6 \u6700\u4f73\u5b9e\u8df5 \u53d8\u91cf\u5728\u7528\u4e8e\u63a5\u6536\u7528\u6237\u8f93\u5165\uff08\u4f8b\u5982\u901a\u8fc7 std::cin \uff09\u7684\u6570\u636e\u4e4b\u524d\uff0c\u662f\u5426\u5e94\u8be5\u8fdb\u884c\u521d\u59cb\u5316\u8fd9\u4e2a\u95ee\u9898\u6709\u5f88\u591a\u4e89\u8bba\uff0c\u56e0\u4e3a\u7528\u6237\u7684\u8f93\u5165\u4f1a\u8986\u76d6\u521d\u59cb\u5316\u7684\u503c\u3002\u4e0e\u6211\u4eec\u4e4b\u524d\u7684\u5efa\u8bae\u7c7b\u4f3c\uff0c\u53d8\u91cf\u5728\u4f7f\u7528\u524d\u90fd\u5fc5\u987b\u8981\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u56e0\u6b64\u8fd9\u91cc\u7684\u6700\u4f73\u5b9e\u8df5\u4ecd\u7136\u662f\uff0c\u53d8\u91cf\u8981\u5148\u8fdb\u884c\u521d\u59cb\u5316\u518d\u7528\u4e8e\u63a5\u6536\u7528\u6237\u8f93\u5165\u3002 \u6211\u4eec\u4f1a\u5728 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406 \u4e2d\u8ba8\u8bba std::cin \u5982\u4f55\u5e94\u5bf9\u975e\u6cd5\u8f93\u5165\u3002 \u6269\u5c55\u9605\u8bfb C++ \u7684\u8f93\u5165\u8f93\u51fa\u5e93\u5e76\u6ca1\u6709\u63d0\u4f9b\u4e00\u79cd\u65e0\u9700\u6309\u4e0b\u56de\u8f66\u5c31\u53ef\u4ee5\u83b7\u53d6\u8f93\u5165\u7ed3\u679c\u7684\u65b9\u6cd5\u3002\u5982\u679c\u4f60\u5e0c\u671b\u5b9e\u73b0\u8be5\u529f\u80fd\uff0c\u5c06\u4e0d\u5f97\u4e0d\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u3002\u5982\u679c\u662f\u5f00\u53d1\u63a7\u5236\u53f0\u5e94\u7528\u7a0b\u5e8f\uff0c\u6211\u4eec\u63a8\u8350\u5c1d\u8bd5 pdcurses \u3001 FXTUI \u6216 cpp-terminal \u3002\u53e6\u5916\uff0c\u5f88\u591a\u56fe\u5f62\u7528\u6237\u5e93\u754c\u9762\u5e93\u4e5f\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u7684\u529f\u80fd\u3002 \u5c0f\u7ed3 \u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u5e38\u4f1a\u6df7\u6dc6 std::cin \u3001 std::cout \u3001\u63d2\u5165\u8fd0\u7b97\u7b26 ( << ) \u548c\u63d0\u53d6\u8fd0\u7b97\u7b26 ( >> )\u3002\u4e0b\u9762\u8fd9\u4e9b\u6280\u5de7\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8bb0\u5fc6\uff1a std::cin \u548c std::cout \u603b\u662f\u4f4d\u4e8e\u8bed\u53e5\u7684\u5de6\u4fa7\u3002 std::cout \u7528\u4e8e\u8f93\u51fa (cout = character output) std::cin \u7528\u4e8e\u8f93\u5165 (cin = character input) << \u914d\u5408 std::cout \u4f7f\u7528\uff0c\u540c\u65f6\u4e5f\u8868\u660e\u4e86\u6570\u636e\u7684\u6d41\u52a8\u65b9\u5411 (\u5047\u8bbe std::cout \u8868\u793a\u7684\u662f\u63a7\u5236\u53f0\uff0c\u5219\u8f93\u51fa\u7684\u6570\u636e\u4ece\u53d8\u91cf\u6d41\u5411\u63a7\u5236\u53f0\uff09\u3002 std::cout << 4 \u5c31\u8868\u793a\u5c06 4 \u53d1\u9001\u5230\u63a7\u5236\u53f0\u3002 >> \u914d\u5408 std::cin \u4f7f\u7528\uff0c\u540c\u65f6\u4e5f\u8868\u660e\u4e86\u6570\u636e\u7684\u6d41\u52a8\u65b9\u5411 (\u5047\u8bbe std::cin \u8868\u793a\u7684\u662f\u952e\u76d8\uff0c\u5219\u8f93\u5165\u7684\u6570\u636e\u662f\u4ece\u952e\u76d8\u6d41\u5411\u53d8\u91cf\uff09\u3002 std::cin >> x \u5c31\u8868\u793a\u4ece\u952e\u76d8\u83b7\u53d6\u7684\u7528\u6237\u8f93\u5165\u88ab\u5b58\u653e\u5230\u53d8\u91cf x \u4e2d \u66f4\u591a\u5173\u4e8e\u8fd0\u7b97\u7b26\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u5c06\u5728 1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"1.5 - iostream \u7b80\u4ecb\uff1acout\uff0ccin \u548c endl"},{"location":"1-5-Introduction-to-iostream-cout-cin-and-endl/#_1","text":"\u8f93\u5165\u8f93\u51fa\u5e93 ( IO \u5e93) \u662f C++ \u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u7528\u4e8e\u5904\u7406\u57fa\u672c\u7684\u8f93\u5165\u548c\u8f93\u51fa\u3002\u6211\u4eec\u4f1a\u4f7f\u7528\u8be5\u5e93\u63d0\u4f9b\u7684\u529f\u80fd\u4ece\u952e\u76d8\u83b7\u53d6\u8f93\u5165\u5e76\u5411\u63a7\u5236\u53f0\u8f93\u51fa\u6570\u636e\u3002 iostream \u4e2d\u7684 io \u6307\u4ee3\u7684\u662f\u8f93\u5165\u8f93\u51fa\uff08 input/output \uff09\u3002 \u4e3a\u4e86\u4f7f\u7528 iostream \u5e93\u63d0\u4f9b\u7684\u529f\u80fd\uff0c\u6211\u4eec\u9700\u8981\u5728\u4efb\u4f55\u9700\u8981\u4f7f\u7528\u8be5\u5e93\u7684\u6e90\u6587\u4ef6\u7684\u9876\u90e8\u5305\u542b iostream \u7684\u5934\u6587\u4ef6\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 #include // rest of code that uses iostream functionality here","title":"\u8f93\u5165\u8f93\u51fa\u5e93"},{"location":"1-5-Introduction-to-iostream-cout-cin-and-endl/#std-cout","text":"iostream \u5e93\u4e2d\u5305\u542b\u4e86\u4e00\u4e9b\u9884\u5b9a\u4e49\u7684\u53d8\u91cf\u4f9b\u6211\u4eec\u4f7f\u7528\uff0c\u5176\u4e2d\u6700\u6709\u7528\u7684\u5f53\u5c5e std::cout \uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u5411\u63a7\u5236\u53f0\u6253\u5370\u6587\u672c\uff0c cout \u4ee3\u8868\u7684\u542b\u4e49\u5c31\u662f\u5b57\u7b26\u8f93\u51fa\uff08character output\uff09\u3002 \u63d0\u9192\u4e00\u4e0b\uff0c\u4e0b\u9762\u65e2\u662f\u4e4b\u524d\u7684 Hello world \u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 #include // for std::cout int main () { std :: cout << \"Hello world!\" ; // print Hello world! to console return 0 ; } \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c iostream \u5df2\u7ecf\u88ab\u5305\u542b\u4e86\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u8bbf\u95ee std::cout \u3002\u5728\u4e3b\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 std::cout \uff0c\u5e76\u914d\u5408\u63d2\u5165\u8fd0\u7b97\u7b26\uff08 << \uff09\u5c06\u6587\u672c Hello world!\u53d1\u9001\u5230\u63a7\u5236\u53f0\u5e76\u6253\u5370\u51fa\u6765\u3002 std::cout \u4e0d\u4ec5\u53ef\u4ee5\u6253\u5370\u6587\u672c\uff0c\u8fd8\u53ef\u4ee5\u6253\u5370\u6570\u5b57\uff1a 1 2 3 4 5 6 7 8 #include // for std::cout int main () { std :: cout << 4 ; // print 4 to console return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 4 \u5b83\u8fd8\u53ef\u4ee5\u7528\u6765\u6253\u5370\u53d8\u91cf\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 #include // for std::cout int main () { int x { 5 }; // define integer variable x, initialized with value 5 std :: cout << x ; // print value of x (5) to console return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 5 \u4e3a\u4e86\u5728\u4e00\u884c\u4e2d\u6253\u5370\u591a\u4e2a\u5185\u5bb9\uff0c\u53ef\u4ee5\u591a\u6b21\u4f7f\u7528\u63d2\u5165\u8fd0\u7b97\u7b26\u5c06\u591a\u4e2a\u8981\u8f93\u51fa\u7684\u5185\u5bb9\u8fde\u63a5\u8d77\u6765\u5e76\u8f93\u51fa\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 #include // for std::cout int main () { std :: cout << \"Hello\" << \" world!\" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Hello world! \u53e6\u5916\u4e00\u4e2a\u4f8b\u5b50\u5982\u4e0b\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728\u4e00\u6761\u8bed\u53e5\u4e2d\u540c\u65f6\u6253\u5370\u4e86\u6587\u672c\u548c\u53d8\u91cf\u503c\uff1a 1 2 3 4 5 6 7 8 #include // for std::cout int main () { int x { 5 }; std :: cout << \"x is equal to: \" << x ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 x is equal to: 5 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 \u8ba8\u8bba std:: \u524d\u7f00\u7684\u4f5c\u7528","title":"std:: cout"},{"location":"1-5-Introduction-to-iostream-cout-cin-and-endl/#stdendl","text":"\u4f60\u89c9\u5f97\u4e0b\u9762\u7684\u4ee3\u7801\u6253\u5370\u7684\u7ed3\u679c\u662f\u4ec0\u4e48\u5462\uff1f 1 2 3 4 5 6 7 8 #include // for std::cout int main () { std :: cout << \"Hi!\" ; std :: cout << \"My name is Alex.\" ; return 0 ; } \u770b\u5230\u4e0b\u9762\u7684\u7ed3\u679c\u4f60\u4f1a\u89c9\u5f97\u5403\u60ca\u5417\uff1a 1 Hi!My name is Alex. \u663e\u7136\uff0c\u5c06\u4e00\u6761\u8bed\u53e5\u62c6\u5206\u4e3a\u4e24\u884c\uff0c\u5e76\u4e0d\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u7ed3\u679c\u3002 \u5982\u679c\u6211\u4eec\u5e0c\u671b\u5206\u884c\u6253\u5370\uff0c\u5219\u5fc5\u987b\u8981\u544a\u77e5\u63a7\u5236\u53f0\u5c06\u5149\u6807\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\u3002 \u4e00\u79cd\u65b9\u5f0f\u662f\u4f7f\u7528 std::endl \u3002\u5f53\u4f7f\u7528 std::cout \u8fdb\u884c\u8f93\u51fa\u65f6\uff0c std::endl \u4f1a\u6253\u5370\u4e00\u4e2a\u6362\u884c\u7b26\uff08\u4fc3\u4f7f\u5149\u6807\u88ab\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\u7684\u5f00\u5934\uff09\u3002\u56e0\u6b64\uff0c endl \u8868\u793a\u7684\u662f\u7ed3\u675f\u8be5\u884c\uff08end line\uff09\u3002 \u4f8b\u7a0b\uff1a 1 2 3 4 5 6 7 8 9 #include // for std::cout and std::endl int main () { std :: cout << \"Hi!\" << std :: endl ; // std::endl will cause the cursor to move to the next line of the console std :: cout << \"My name is Alex.\" << std :: endl ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 Hi! My name is Alex. \u5c0f\u8d34\u58eb \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u7b2c\u4e8c\u4e2a std::endl \u4ece\u6280\u672f\u4e0a\u8bb2\u5e76\u65e0\u5fc5\u8981\uff0c\u56e0\u4e3a\u5728\u6267\u884c\u5b8c\u8fd9\u884c\u4ee3\u7801\u540e\u7a0b\u5e8f\u4f1a\u7acb\u5373\u505c\u6b62\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u5176\u5b9e\u8fd8\u6709\u4e24\u4e2a\u6709\u7528\u7684\u76ee\u7684\uff1a\u9996\u5148\uff0c\u5b83\u53ef\u4ee5\u8868\u660e\u8be5\u884c\u7684\u5185\u5bb9\u5df2\u7ecf\u88ab\u201d\u5b8c\u6574\u8f93\u51fa\u201c \uff08\u4e0e\u4e4b\u76f8\u5bf9\u7684\u662f\u90e8\u5206\u8f93\u51fa\uff0c\u5373\u540e\u7eed\u4ee3\u7801\u4e2d\u8fd8\u6709\u9700\u8981\u8f93\u51fa\u7684\u90e8\u5206\uff09\u3002\u5176\u6b21\uff0c\u5982\u679c\u5c06\u6765\u6211\u4eec\u5728\u540e\u9762\u6dfb\u52a0\u5176\u4ed6\u989d\u5916\u7684\u8f93\u51fa\uff0c\u5c31\u4e0d\u9700\u8981\u4fee\u6539\u5df2\u6709\u7684\u4ee3\u7801\u4e86\u3002\u56e0\u6b64\u4e0d\u59a8\u52a0\u4e0a\u5b83\u3002","title":"std::endl"},{"location":"1-5-Introduction-to-iostream-cout-cin-and-endl/#std-endl-vs-n","text":"\u4f7f\u7528 std::endl \u6362\u884c\u7684\u6548\u7387\u7a0d\u5fae\u6709\u70b9\u4f4e\uff0c\u56e0\u4e3a\u5b83\u901a\u5e38\u9700\u8981\u5b8c\u6210\u4e24\u4ef6\u4e8b\uff1a\u5c06\u5149\u6807\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\uff0c\u7136\u540e\u786e\u4fdd\u8f93\u51fa\u7ed3\u679c\u9a6c\u4e0a\u663e\u793a\u5728\u5c4f\u5e55\u4e0a\uff08\u79f0\u4e3a\u5237\u65b0\u8f93\u51fa\uff09\u3002\u5f53\u4f7f\u7528 std::cout \u8fdb\u884c\u8f93\u51fa\u65f6\uff0c std::cout \u672c\u6765\u5c31\u4f1a\u5237\u65b0\u8f93\u51fa\uff08\u5373\u4f7f\u6ca1\u6709\u5237\u65b0\uff0c\u901a\u5e38\u4e5f\u4e0d\u4f1a\u4ea7\u751f\u4ec0\u4e48\u95ee\u9898\uff09\u3002\u56e0\u6b64\uff0c\u4f7f\u7528 std::endl \u6765\u5237\u65b0\u8f93\u51fa\u5c31\u6709\u4e9b\u591a\u4f59\u4e86\u3002 \u56e0\u6b64\uff0c\u4f7f\u7528\u6362\u884c\u5b57\u7b26( \\n )\u4e00\u822c\u6765\u8bb2\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u2018\\n\u2019 \u7b26\u53f7\u4f1a\u5c06\u5149\u6807\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\uff0c\u4f46\u662f\u5b83\u5e76\u4e0d\u4f1a\u8bf7\u6c42\u5237\u65b0\uff0c\u56e0\u6b64\u5728\u65e0\u9700\u7279\u522b\u5237\u65b0\u65f6\u53ef\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\u3002\u2018\\n\u2019 \u5b57\u7b26\u8fd8\u66f4\u6613\u8bfb\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u66f4\u7b80\u6d01\uff0c\u800c\u4e14\u8fd8\u53ef\u4ee5\u5d4c\u5165\u5728\u5df2\u6709\u7684\u6587\u672c\u4e2d\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u5c55\u73b0\u4e86\u4f7f\u7528 \u2018\\n\u2019 \u7684\u4e24\u79cd\u4e0d\u540c\u65b9\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 #include // for std::cout int main () { int x { 5 }; std :: cout << \"x is equal to: \" << x << '\\n' ; // Using '\\n' standalone std :: cout << \"And that's all, folks! \\n \" ; // Using '\\n' embedded into a double-quoted piece of text (note: no single quotes when used this way) return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 x is equal to: 5 And that's all, folks! \u6ce8\u610f\uff0c\u5728\u5355\u72ec\u4f7f\u7528\u6362\u884c\u7b26 \u2018\\n\u2019 \u7684\u65f6\u5019\uff0c\u5355\u5f15\u53f7\u662f\u5fc5\u987b\u7684\u3002\u5f53\u5d4c\u5165\u5728\u5176\u4ed6\u5df2\u7ecf\u88ab\u53cc\u5f15\u53f7\u5305\u88f9\u7684\u6587\u672c\u4e2d\u65f6\uff0c\u5219\u65e0\u9700\u518d\u4f7f\u7528\u5355\u5f15\u53f7\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u66f4\u8be6\u7ec6\u7684\u4ecb\u7ecd\u6362\u884c\u7b26 \u2018\\n\u2019 ( 4.11 - \u5b57\u7b26 )\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u8f93\u51fa\u65f6\u4f18\u5148\u4f7f\u7528 \u2018\\n\u2019 \u800c\u4e0d\u662f std::endl \u6ce8\u610f \u2018\\n\u2019\u4f7f\u7528\u7684\u662f\u53cd\u659c\u6760\uff08\u548c\u5176\u4ed6\u7279\u6b8a\u5b57\u7b26\u4e00\u6837\uff09\uff0c\u800c\u4e0d\u662f\u659c\u6760\u3002\u4f7f\u7528\u659c\u6760\uff08\u4f8b\u5982 '/n'\uff09\u53ef\u80fd\u4f1a\u5e26\u6765\u65e0\u6cd5\u9884\u6599\u7684\u7ed3\u679c\u3002","title":"std:: endl vs \\n"},{"location":"1-5-Introduction-to-iostream-cout-cin-and-endl/#std-cin","text":"std::cin \u662f isstream \u4e2d\u9884\u5b9a\u4e49\u7684\u53e6\u5916\u4e00\u4e2a\u53d8\u91cf\u3002\u4e0e\u7528\u4e8e\u8f93\u51fa\u7684 std::cout \u4e0d\u540c\uff0c std::cin (\u8868\u793a\u5b57\u7b26\u8f93\u5165\uff0c\u201ccharacter input\u201d) \u914d\u5408\u63d0\u53d6\u8fd0\u7b97\u7b26( >> )\uff0c\u53ef\u4ee5\u4ece\u952e\u76d8\u8bfb\u53d6\u8f93\u5165\u3002\u5f53\u7136\uff0c\u8f93\u5165\u7684\u7ed3\u679c\u5fc5\u987b\u5b58\u653e\u5728\u53d8\u91cf\u4e2d\u624d\u53ef\u4ee5\u88ab\u4f7f\u7528\u3002 1 2 3 4 5 6 7 8 9 10 11 12 #include // for std::cout and std::cin int main () { std :: cout << \"Enter a number: \" ; // ask user for a number int x { }; // define variable x to hold user input (and zero-initialize it) std :: cin >> x ; // get number from keyboard and store it in variable x std :: cout << \"You entered \" << x << '\\n' ; return 0 ; } \u8bf7\u5c1d\u8bd5\u81ea\u884c\u7f16\u8bd1\u548c\u8fd0\u884c\u4e0a\u8ff0\u7a0b\u5e8f\u3002\u5728\u6267\u884c\u7a0b\u5e8f\u65f6\uff0c\u7b2c\u4e94\u884c\u4ee3\u7801\u4f1a\u6253\u5370\u201cEnter a number: \u201c\uff0c\u5f53\u6267\u884c\u5230\u7b2c\u516b\u884c\u662f\uff0c\u7a0b\u5e8f\u4f1a\u7b49\u5f85\u7528\u6237\u8f93\u5165\u3002\u5f53\u4f60\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff08\u5e76\u6309\u4e0b\u56de\u8f66\uff09\u540e\uff0c\u5b83\u4f1a\u88ab\u8d4b\u503c\u7ed9\u53d8\u91cf x\u3002\u6700\u540e\uff0c\u7b2c\u5341\u884c\u4ee3\u7801\u4f1a\u6253\u5370 \u201cYou entered \u201d \u4ee5\u53ca\u4f60\u521a\u624d\u8f93\u5165\u7684\u6570\u5b57\u3002 \u4f8b\u5982\uff1a 1 2 Enter a number: 4 You entered 4 \u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u6bd4\u8f83\u7b80\u5355\u5730\u4ece\u7528\u6237\u83b7\u53d6\u8f93\u5165\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u5185\u5bb9\u4e2d\u5927\u91cf\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u3002\u6ce8\u610f\uff0c\u5728\u83b7\u53d6\u8f93\u5165\u65f6\u5e76\u4e0d\u9700\u8981\u6362\u884c\u7b26\uff0c\u56e0\u4e3a\u7528\u6237\u4f1a\u81ea\u884c\u6309\u4e0b\u56de\u8f66\u952e\uff0c\u6b64\u65f6\u5149\u6807\u4fbf\u4f1a\u88ab\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\u3002 \u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u754c\u9762\u5728\u8f93\u5165\u6570\u5b57\u540e\u9a6c\u4e0a\u5c31\u5173\u95ed\u4e86\uff0c\u8bf7\u53c2\u8003 0.8 -- A few common C++ problems \u6765\u83b7\u53d6\u5e2e\u52a9\u3002 \u4e0e\u8f93\u51fa\u7c7b\u4f3c\uff0c\u4e00\u884c\u4ee3\u7801\u4e5f\u53ef\u4ee5\u5b8c\u6210\u591a\u4e2a\u503c\u7684\u8f93\u5165\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // for std::cout and std::cin int main () { std :: cout << \"Enter two numbers separated by a space: \" ; int x { }; // define variable x to hold user input (and zero-initialize it) int y { }; // define variable y to hold user input (and zero-initialize it) std :: cin >> x >> y ; // get two numbers and store in variable x and y respectively std :: cout << \"You entered \" << x << \" and \" << y << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Enter two numbers separated by a space: 5 6 You entered 5 and 6 \u6700\u4f73\u5b9e\u8df5 \u53d8\u91cf\u5728\u7528\u4e8e\u63a5\u6536\u7528\u6237\u8f93\u5165\uff08\u4f8b\u5982\u901a\u8fc7 std::cin \uff09\u7684\u6570\u636e\u4e4b\u524d\uff0c\u662f\u5426\u5e94\u8be5\u8fdb\u884c\u521d\u59cb\u5316\u8fd9\u4e2a\u95ee\u9898\u6709\u5f88\u591a\u4e89\u8bba\uff0c\u56e0\u4e3a\u7528\u6237\u7684\u8f93\u5165\u4f1a\u8986\u76d6\u521d\u59cb\u5316\u7684\u503c\u3002\u4e0e\u6211\u4eec\u4e4b\u524d\u7684\u5efa\u8bae\u7c7b\u4f3c\uff0c\u53d8\u91cf\u5728\u4f7f\u7528\u524d\u90fd\u5fc5\u987b\u8981\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u56e0\u6b64\u8fd9\u91cc\u7684\u6700\u4f73\u5b9e\u8df5\u4ecd\u7136\u662f\uff0c\u53d8\u91cf\u8981\u5148\u8fdb\u884c\u521d\u59cb\u5316\u518d\u7528\u4e8e\u63a5\u6536\u7528\u6237\u8f93\u5165\u3002 \u6211\u4eec\u4f1a\u5728 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406 \u4e2d\u8ba8\u8bba std::cin \u5982\u4f55\u5e94\u5bf9\u975e\u6cd5\u8f93\u5165\u3002 \u6269\u5c55\u9605\u8bfb C++ \u7684\u8f93\u5165\u8f93\u51fa\u5e93\u5e76\u6ca1\u6709\u63d0\u4f9b\u4e00\u79cd\u65e0\u9700\u6309\u4e0b\u56de\u8f66\u5c31\u53ef\u4ee5\u83b7\u53d6\u8f93\u5165\u7ed3\u679c\u7684\u65b9\u6cd5\u3002\u5982\u679c\u4f60\u5e0c\u671b\u5b9e\u73b0\u8be5\u529f\u80fd\uff0c\u5c06\u4e0d\u5f97\u4e0d\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u3002\u5982\u679c\u662f\u5f00\u53d1\u63a7\u5236\u53f0\u5e94\u7528\u7a0b\u5e8f\uff0c\u6211\u4eec\u63a8\u8350\u5c1d\u8bd5 pdcurses \u3001 FXTUI \u6216 cpp-terminal \u3002\u53e6\u5916\uff0c\u5f88\u591a\u56fe\u5f62\u7528\u6237\u5e93\u754c\u9762\u5e93\u4e5f\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u7684\u529f\u80fd\u3002","title":"std:: cin"},{"location":"1-5-Introduction-to-iostream-cout-cin-and-endl/#_2","text":"\u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u5e38\u4f1a\u6df7\u6dc6 std::cin \u3001 std::cout \u3001\u63d2\u5165\u8fd0\u7b97\u7b26 ( << ) \u548c\u63d0\u53d6\u8fd0\u7b97\u7b26 ( >> )\u3002\u4e0b\u9762\u8fd9\u4e9b\u6280\u5de7\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8bb0\u5fc6\uff1a std::cin \u548c std::cout \u603b\u662f\u4f4d\u4e8e\u8bed\u53e5\u7684\u5de6\u4fa7\u3002 std::cout \u7528\u4e8e\u8f93\u51fa (cout = character output) std::cin \u7528\u4e8e\u8f93\u5165 (cin = character input) << \u914d\u5408 std::cout \u4f7f\u7528\uff0c\u540c\u65f6\u4e5f\u8868\u660e\u4e86\u6570\u636e\u7684\u6d41\u52a8\u65b9\u5411 (\u5047\u8bbe std::cout \u8868\u793a\u7684\u662f\u63a7\u5236\u53f0\uff0c\u5219\u8f93\u51fa\u7684\u6570\u636e\u4ece\u53d8\u91cf\u6d41\u5411\u63a7\u5236\u53f0\uff09\u3002 std::cout << 4 \u5c31\u8868\u793a\u5c06 4 \u53d1\u9001\u5230\u63a7\u5236\u53f0\u3002 >> \u914d\u5408 std::cin \u4f7f\u7528\uff0c\u540c\u65f6\u4e5f\u8868\u660e\u4e86\u6570\u636e\u7684\u6d41\u52a8\u65b9\u5411 (\u5047\u8bbe std::cin \u8868\u793a\u7684\u662f\u952e\u76d8\uff0c\u5219\u8f93\u5165\u7684\u6570\u636e\u662f\u4ece\u952e\u76d8\u6d41\u5411\u53d8\u91cf\uff09\u3002 std::cin >> x \u5c31\u8868\u793a\u4ece\u952e\u76d8\u83b7\u53d6\u7684\u7528\u6237\u8f93\u5165\u88ab\u5b58\u653e\u5230\u53d8\u91cf x \u4e2d \u66f4\u591a\u5173\u4e8e\u8fd0\u7b97\u7b26\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u5c06\u5728 1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"\u5c0f\u7ed3"},{"location":"1-6-Uninitialized-variables-and-undefined-behavior/","tags":["undefined behavior","initialization"],"text":"\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf \u4e0d\u540c\u4e8e\u6709\u4e9b\u7f16\u7a0b\u8bed\u8a00\uff0cC/C++ \u5e76\u4e0d\u4f1a\u81ea\u52a8\u5730\u5c06\u5927\u90e8\u5206\u53d8\u91cf\u521d\u59cb\u5316\u4e3a\u67d0\u4e2a\u7ed9\u5b9a\u503c\uff08\u4f8b\u59820\uff09\u3002\u56e0\u6b64\uff0c\u5f53\u7f16\u8bd1\u5668\u4e3a\u53d8\u91cf\u5206\u914d\u5185\u5b58\u5730\u5740\u65f6\uff0c\u9ed8\u8ba4\u7684\u521d\u503c\u5c06\u4f1a\u662f\u4efb\u4f55\u6070\u597d\u4f4d\u4e8e\u8be5\u5730\u5740\u7684\u503c\uff08\u5783\u573e\u503c\uff09\u3002\u4e00\u4e2a\u6ca1\u6709\u88ab\u660e\u786e\u8d4b\u4e88\u6307\u5b9a\u521d\u503c\uff08\u901a\u8fc7\u521d\u59cb\u5316\u6216\u8d4b\u503c\uff09\u7684\u53d8\u91cf\u79f0\u4e3a\u672a\u521d\u59cb\u5316\u53d8\u91cf\u3002 \u4f5c\u8005\u6ce8 \u5f88\u591a\u8bfb\u8005\u60f3\u5f53\u7136\u5730\u8ba4\u4e3a\uff0c\u201d\u521d\u59cb\u5316\u7684\u201c\u548c\"\u672a\u521d\u59cb\u5316\u7684\"\u662f\u4e00\u5bf9\u4e25\u683c\u7684\u53cd\u4e49\u8bcd\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u3002\u521d\u59cb\u5316\u610f\u5473\u7740\u5bf9\u8c61\u5728\u88ab\u5b9a\u4e49\u7684\u65f6\u5019\u5373\u83b7\u5f97\u4e86\u4e00\u4e2a\u7ed9\u5b9a\u7684\u521d\u59cb\u503c\u3002\u800c\u672a\u521d\u59cb\u5316\u5219\u8868\u793a\uff0c\u5bf9\u8c61\u6ca1\u6709\u88ab\u660e\u786e\u5730\u6307\u5b9a\u4efb\u4f55\u503c\uff08\u901a\u8fc7\u5404\u79cd\u9014\u5f84\uff0c\u5305\u62ec\u8d4b\u503c\uff09\u3002\u56e0\u6b64\uff0c\u4e00\u4e2a\u5bf9\u8c61\u5982\u679c\u6ca1\u6709\u88ab\u201d\u521d\u59cb\u5316\u201c\uff0c\u4f46\u901a\u8fc7\u8d4b\u503c\u5f97\u5230\u4e86\u4e00\u4e2a\u521d\u503c\uff0c\u5b83\u4e5f\u4e0d\u518d\u662f\u201d\u672a\u521d\u59cb\u5316\u201c\u72b6\u6001\uff08\u56e0\u4e3a\u5b83\u5df2\u7ecf\u5f97\u5230\u4e86\u4e00\u4e2a\u503c\uff09 \u7b80\u5355\u603b\u7ed3\u4e00\u4e0b\uff1a - \u521d\u59cb\u5316\uff1a\u5bf9\u8c61\u5728\u88ab\u5b9a\u4e49\u7684\u540c\u65f6\u4e5f\u88ab\u8d4b\u4e88\u4e86\u521d\u503c - \u8d4b\u503c\uff1a\u5bf9\u8c61\u5728\u5b9a\u4e49\u540e\uff0c\u88ab\u8d4b\u4e88\u4e86\u4e00\u4e2a\u503c - \u672a\u521d\u59cb\u5316\uff1a\u5bf9\u8c61\u59cb\u7ec8\u6ca1\u6709\u80fd\u591f\u83b7\u5f97\u4e00\u4e2a\u660e\u786e\u6307\u5b9a\u7684\u521d\u503c\u3002 \u9898\u5916\u8bdd \u4e0d\u8fdb\u884c\u9ed8\u8ba4\u521d\u59cb\u5316\u8fd9\u4e00\u7b56\u7565\u662f\u4eceC\u8bed\u8a00\u7ee7\u627f\u800c\u6765\uff0c\u4e3b\u8981\u51fa\u4e8e\u6027\u80fd\u4f18\u5316\u8003\u8651\uff0c\u56e0\u4e3a\u5f53\u65f6\u7684\u8ba1\u7b97\u673a\u8fd0\u884c\u901f\u5ea6\u8fd8\u5f88\u6162\u3002\u4f60\u53ef\u4ee5\u60f3\u8c61\u4e00\u4e0b\uff0c\u5047\u8bbe\u6211\u4eec\u9700\u8981\u4ece\u6587\u4ef6\u8bfb\u53d6100000\u6761\u6570\u636e\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u4f60\u9700\u8981\u521b\u5efa100000\u4e2a\u53d8\u91cf\u5e76\u4f7f\u7528\u8bfb\u53d6\u7684\u6570\u636e\u6765\u586b\u5145\u5b83\u4eec\u3002 \u5982\u679c C++ \u9700\u8981\u5728\u521b\u5efa\u8fd9\u4e9b\u53d8\u91cf\u65f6\uff0c\u4f7f\u7528\u9ed8\u8ba4\u503c\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u8fd9\u5c31\u610f\u5473\u7740\u9700\u8981\u6267\u884c100000\u6b21\u521d\u59cb\u5316\uff08\u521d\u59cb\u5316\u901f\u5ea6\u5f88\u6162\uff09\uff0c\u5176\u5e26\u6765\u7684\u597d\u5904\u4e5f\u5e76\u4e0d\u5927\uff08\u6bd5\u7adf\u8fd9\u4e9b\u521d\u59cb\u5316\u5f88\u5feb\u5c06\u4f1a\u88ab\u771f\u5b9e\u503c\u8986\u76d6\uff09\u3002 \u73b0\u5982\u4eca\uff0c\u6211\u4eec\u5219\u5e94\u8be5\u7262\u8bb0\u5bf9\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u5176\u5e26\u6765\u7684\u597d\u5904\u5df2\u7ecf\u8fdc\u5927\u4e8e\u5176\u5f00\u9500\u3002\u5f53\u4f60\u719f\u7ec3\u638c\u63e1\u8fd9\u95e8\u8bed\u8a00\u540e\uff0c\u4e5f\u8bb8\u4e5f\u4f1a\u9047\u5230\u4e3a\u4e86\u4f18\u5316\u6027\u80fd\u800c\u653e\u5f03\u521d\u59cb\u5316\u7684\u60c5\u51b5\u3002\u4f46\u8fd9\u6837\u7684\u64cd\u4f5c\u4e00\u5b9a\u662f\u4f60\u6df1\u601d\u719f\u8651\u540e\u6709\u610f\u800c\u4e3a\u4e4b\u7684\u3002 \u4f7f\u7528\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\u7684\u503c\uff0c\u53ef\u80fd\u4f1a\u5e26\u6765\u96be\u4ee5\u9884\u6599\u7684\u7ed3\u679c\u3002\u4ee5\u5982\u4e0b\u4ee3\u7801\u4e3a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { // define an integer variable named x int x ; // this variable is uninitialized because we haven't given it a value // print the value of x to the screen std :: cout << x ; // who knows what we'll get, because x is uninitialized return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u8ba1\u7b97\u673a\u4f1a\u4e3ax\u5206\u914d\u67d0\u7a7a\u95f2\u7684\u5185\u5b58\u3002\u8be5\u5185\u5b58\u4e2d\u5b58\u653e\u7684\u503c\u4f1a\u901a\u8fc7 std::cout \u8f93\u51fa\u5230\u63a7\u5236\u53f0\u5e76\u6253\u5370\uff0c\u5176\u6253\u5370\u51fa\u7684\u7ed3\u679c\u53ef\u80fd\u662f\u67d0\u4e2a\u503c\uff08\u88ab\u5f53\u505a\u6574\u578b\u6765\u89e3\u6790\uff09\u3002\u4f46\u7a76\u7adf\u662f\u4ec0\u4e48\u503c\u5462\uff1f\u7b54\u6848\u662f\uff1a\u201d\u6ca1\u6709\u4eba\u77e5\u9053\u201c\uff0c\u6216\u8005\u8bf4\uff0c\u6bcf\u6b21\u8fd0\u884c\u7a0b\u5e8f\uff0c\u7b54\u6848\u90fd\u53ef\u80fd\u662f\u4e0d\u4e00\u6837\u7684\u3002\u4f5c\u8005\u5728\u4f7f\u7528 Visual Studio \u8fdb\u884c\u6d4b\u8bd5\u7684\u65f6\u5019\uff0c\u6253\u5370\u51fa\u7684\u503c\u662f 7177728 \uff0c\u518d\u6b21\u8fd0\u884c\u65f6\u6253\u5370\u7684\u503c\u5c31\u7f16\u7a0b\u4e86 5277592 \u3002\u4f60\u53ef\u4ee5\u81ea\u5df1\u5c1d\u8bd5\u5e76\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\uff08\u4e0d\u7528\u62c5\u5fc3\uff0c\u8fd9\u5e76\u4e0d\u4f1a\u635f\u574f\u4f60\u7684\u7535\u8111\uff09\u3002 \u6ce8\u610f \u6709\u4e00\u4e9b\u7f16\u8bd1\u5668\uff0c\u4f8b\u5982 Visual Studio\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fdebug\u7248\u672c\u914d\u7f6e\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u5c06\u5185\u5b58\u521d\u59cb\u5316\u4e3a\u67d0\u4e9b\u9884\u8bbe\u503c\u3002\u4f46\u662f\u5f53\u4f60\u4f7f\u7528release\u7248\u672c\u914d\u7f6e\u65f6\uff0c\u5219\u4e0d\u4f1a\u6709\u4e0a\u8ff0\u884c\u4e3a\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u60f3\u8981\u81ea\u884c\u5c1d\u8bd5\u4e0a\u8ff0\u7a0b\u5e8f\uff0c\u8bf7\u786e\u4fdd\u81ea\u5df1\u4f7f\u7528\u7684\u662frelease\u7248\u672c\u7684\u6784\u5efa\u914d\u7f6e (\u53c2\u8003 0.9 -- Configuring your compiler: Build configurations ). \u4f8b\u5982\uff0c\u5f53\u4f60\u5728Visual Studio\u4e2d\u4f7f\u7528debug\u914d\u7f6e\u7f16\u8bd1\u5e76\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u65f6\uff0c\u8f93\u51fa\u7ed3\u679c\u59cb\u7ec8\u4f1a\u662f -858993460\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u503c\uff08\u88ab\u89e3\u91ca\u4e3a\u6574\u578b\uff09\u662f Visual Studio \u5728 debug \u914d\u7f6e\u4e0b\u521d\u59cb\u5316\u5185\u5b58\u4f7f\u7528\u7684\u503c\u3002 \u5927\u591a\u6570\u7684\u73b0\u4ee3\u7f16\u8bd1\u5668\u90fd\u80fd\u591f\u68c0\u6d4b\u5230\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u88ab\u8d4b\u4e88\u4e86\u521d\u503c\u3002\u5982\u679c\u80fd\u591f\u68c0\u6d4b\u5230\uff0c\u5219\u5728\u7f16\u8bd1\u65f6\u4f1a\u4ea7\u751f\u4e00\u4e2a\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u5728 Visual Studio \u4e2d\u7f16\u8bd1\u4e0a\u8ff0\u7a0b\u5e8f\u65f6\uff0c\u4f1a\u8f93\u51fa\u5982\u4e0b\u7684\u544a\u8b66\u4fe1\u606f\uff1a 1 c:\\VCprojects\\test\\test.cpp(11) : warning C4700: uninitialized local variable 'x' used \u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u4e0d\u5141\u8bb8\u4f60\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\uff0c\u53ef\u4ee5\u91c7\u7528\u5982\u4e0b\u65b9\u5f0f\u7ed5\u8fc7\u9650\u5236\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include void doNothing ( int & ) // Don't worry about what & is for now, we're just using it to trick the compiler into thinking variable x is used { } int main () { // define an integer variable named x int x ; // this variable is uninitialized doNothing ( x ); // make the compiler think we're assigning a value to this variable // print the value of x to the screen (who knows what we'll get, because x is uninitialized) std :: cout << x ; return 0 ; } \u4f7f\u7528\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\u662f\u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u72af\u7684\u9519\u8bef\u4e4b\u4e00\uff0c\u4e0d\u5e78\u7684\u662f\uff0c\u8be5\u9519\u8bef\u4e5f\u5e38\u5e38\u4f1a\u57cb\u4e0b\u6700\u96be\u4ee5\u8c03\u8bd5\u7684\u95ee\u9898\uff08\u56e0\u4e3a\u7a0b\u5e8f\u53ef\u80fd\u53ef\u4ee5\u6b63\u786e\u8fd0\u884c\uff0c\u5982\u679c\u8be5\u5185\u5b58\u4e2d\u6070\u597d\u5305\u542b\u4e86\u4e00\u4e2a\u5408\u7406\u7684\u503c\uff0c\u4f8b\u59820\uff09\u3002 \u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728\u6700\u4f73\u5b9e\u8df5\u4e2d\u6211\u4eec\u5f3a\u8c03\uff0c\u4e00\u5b9a\u8981\u786e\u4fdd\u521d\u59cb\u5316\u53d8\u91cf\u3002 \u672a\u5b9a\u4e49\u884c\u4e3a \u4f7f\u7528\u672a\u5b9a\u4e49\u53d8\u91cf\u4e2d\u7684\u503c\uff0c\u662f\u6211\u4eec\u9047\u5230\u7684\u7b2c\u4e00\u79cd\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u4f8b\u5b50\u3002\u672a\u5b9a\u4e49\u884c\u4e3a\uff08\u5e38\u5e38\u7f29\u5199\u4e3a UB\uff09\u6307\u7684\u662f\u6267\u884c\u4ee3\u7801\u7684\u884c\u4e3a\u6ca1\u6709\u88ab C++ \u8bed\u8a00\u660e\u786e\u5b9a\u4e49\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cC++ \u8bed\u8a00\u5e76\u6ca1\u6709\u660e\u786e\u7684\u89c4\u5219\u80fd\u591f\u7528\u4e8e\u786e\u5b9a\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002\u4ece\u7ed3\u679c\u4e0a\u6765\u770b\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u4e86\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\uff0c\u5219\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 \u4ee3\u7801\u7684\u672a\u5b9a\u4e49\u884c\u4e3a\u53ef\u80fd\u8868\u73b0\u51fa\u5982\u4e0b\u75c7\u72b6\uff1a \u7a0b\u5e8f\u6bcf\u6b21\u8fd0\u884c\u90fd\u53ef\u80fd\u5f97\u5230\u4e0d\u540c\u7684\u7ed3\u679c\uff1b \u7a0b\u5e8f\u6bcf\u6b21\u8fd0\u884c\u90fd\u8f93\u51fa\u76f8\u540c\u7684\u9519\u8bef\u7ed3\u679c\uff1b \u7a0b\u5e8f\u6bcf\u6b21\u8fd0\u884c\u7684\u884c\u4e3a\u90fd\u4e0d\u4e00\u81f4 (\u6709\u65f6\u5019\u7ed3\u679c\u6b63\u786e\uff0c\u6709\u65f6\u5019\u4e0d\u6b63\u786e)\uff1b \u7a0b\u5e8f\u770b\u8d77\u6765\u80fd\u591f\u6b63\u786e\u8fd0\u884c\uff0c\u4f46\u662f\u4e00\u6bb5\u65f6\u95f4\u540e\u53c8\u4f1a\u8f93\u51fa\u9519\u8bef\u7684\u7ed3\u679c\uff1b \u7a0b\u5e8f\u5d29\u6e83\u9000\u51fa\uff0c\u8fd9\u79cd\u5d29\u6e83\u53ef\u80fd\u5728\u7a0b\u5e8f\u542f\u52a8\u540e\u7acb\u5373\u53d1\u751f\uff0c\u4e5f\u53ef\u80fd\u8fd0\u884c\u4e00\u6bb5\u65f6\u95f4\u540e\u53d1\u751f\uff1b \u7a0b\u5e8f\u7ecf\u67d0\u4e9b\u7f16\u8bd1\u5668\u7f16\u8bd1\u540e\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\uff0c\u4f46\u6709\u4e9b\u7f16\u8bd1\u5668\u5219\u4e0d\u884c\uff1b \u7a0b\u5e8f\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u76f4\u5230\u4f60\u4fee\u6539\u4e86\u67d0\u4e9b\u770b\u4e0a\u6beb\u4e0d\u76f8\u5e72\u7684\u4ee3\u7801\u3002 \u6216\u8005\uff0c\u7a0b\u5e8f\u751a\u81f3\u80fd\u591f\u6b63\u786e\u8fd0\u884c\u5e76\u5f97\u5230\u6b63\u786e\u7ed3\u679c\u3002\u6240\u8c13\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u5c31\u662f\u4f60\u6c38\u8fdc\u4e0d\u77e5\u9053\u4f60\u80fd\u5f97\u5230\u4ec0\u4e48\u6837\u7684\u7ed3\u679c\uff0c\u65e0\u6cd5\u9884\u6d4b\u6bcf\u6b21\u8fd0\u884c\u7684\u884c\u4e3a\uff0c\u4e5f\u65e0\u6cd5\u9884\u6d4b\u4f60\u505a\u51fa\u67d0\u4e9b\u4fee\u6539\u540e\u7684\u5f71\u54cd\u3002 \u5728 C++ \u4e2d\uff0c\u5982\u679c\u4f60\u4e0d\u591f\u6d88\u606f\uff0c\u90a3\u4e48\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u9014\u5f84\u6709\u5f88\u591a\u3002\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u9047\u5230\u5177\u4f53\u95ee\u9898\u65f6\u8fdb\u884c\u8bb2\u89e3\u3002\u8bf7\u6ce8\u610f\u8bb0\u5f55\u8fd9\u4e9b\u6848\u4f8b\u5e76\u5207\u8bb0\u8981\u907f\u514d\u5b83\u4eec\u3002 \u6cd5\u5219 \u6ce8\u610f\u907f\u514d\u53ef\u80fd\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u5404\u79cd\u60c5\u51b5\uff0c\u4f8b\u5982\u4f7f\u7528\u672a\u521d\u59cb\u5316\u53d8\u91cf\u3002 \u4f5c\u8005\u6ce8 \u8bfb\u8005\u8bc4\u8bba\u4e2d\u6700\u5e38\u89c1\u7684\u95ee\u9898\u4e4b\u4e00\u4fbf\u662f\uff1a\u201d\u4f60\u8bf4\u4e0d\u80fd\u8fd9\u6837\u505a\uff0c\u4f46\u6211\u504f\u504f\u8fd9\u6837\u505a\u4e86\uff0c\u800c\u4e14\u7a0b\u5e8f\u4e5f\u80fd\u591f\u6b63\u5e38\u8fd0\u884c\uff01\u4e3a\u4ec0\u4e48\uff1f\u201c \u5173\u4e8e\u8fd9\u4e2a\u95ee\u9898\u901a\u5e38\u6709\u4e24\u4e2a\u7b54\u6848\u3002\u6700\u5e38\u89c1\u7684\u7b54\u6848\u662f\uff0c\u4f60\u7684\u7a0b\u5e8f\u5b9e\u9645\u4e0a\u8868\u73b0\u51fa\u4e86\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u53ea\u4e0d\u8fc7\u8be5\u672a\u5b9a\u4e49\u884c\u4e3a\u6070\u5de7\uff08\u6682\u65f6\uff09\u8f93\u51fa\u4e86\u4f60\u671f\u671b\u7684\u7ed3\u679c\u3002\u6539\u5929\uff08\u6216\u8005\u4f7f\u7528\u5176\u4ed6\u7f16\u8bd1\u5668\u3001\u4f7f\u7528\u5176\u4ed6\u8ba1\u7b97\u673a\uff09\u518d\u8fd0\u884c\u65f6\uff0c\u53ef\u80fd\u672a\u5fc5\u5982\u6b64\u3002 \u8fd9\u4e2a\u95ee\u9898\u8fd8\u53ef\u4ee5\u8fd9\u6837\u56de\u7b54\uff1a\u6709\u4e9b\u7f16\u8bd1\u5668\u7684\u4f5c\u8005\uff0c\u5728\u5b9e\u73b0\u7279\u5b9a\u8bed\u8a00\u7684\u9700\u6c42\u65f6\uff0c\u8ba4\u4e3a\u67d0\u4e9b\u89c4\u5219\u8fc7\u4e8e\u4e25\u683c\u662f\u6ca1\u6709\u5fc5\u8981\u7684\u3002\u4f8b\u5982\uff0c\u6807\u51c6\u53ef\u80fd\u4f1a\u8bf4\uff0cX \u5fc5\u987b\u4f4d\u4e8e Y \u4e4b\u524d\u53d1\u751f\u3002\u4f46\u662f\u7f16\u8bd1\u5668\u4f5c\u8005\u786e\u8ba4\u4e3a\u8fd9\u4e48\u505a\u662f\u6ca1\u6709\u5fc5\u8981\u7684\uff0c\u5e76\u4e14\u4f7f\u5f97 Y \u80fd\u591f\u5728\u6ca1\u6709\u6267\u884cX\u7684\u524d\u63d0\u4e0b\u6b63\u786e\u5de5\u4f5c\u3002\u8fd9\u4e48\u505a\u4e0d\u4f1a\u5f71\u54cd\u5230\u6b63\u786e\u7684\u4ee3\u7801\uff0c\u4f46\u662f\u53ef\u80fd\u4f1a\u9020\u6210\u4e0d\u7b26\u5408\u89c4\u8303\u7684\u4ee3\u7801\u80fd\u591f\u6b63\u786e\u6267\u884c\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u8bf4\uff1a\u4f60\u4f7f\u7528\u7684\u7f16\u8bd1\u5668\u53ef\u80fd\u5e76\u6ca1\u6709\u5b8c\u5168\u9075\u5b88\u6807\u51c6\u3002\u8fd9\u79cd\u60c5\u51b5\u662f\u5b58\u5728\u7684\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5173\u95ed\u7f16\u8bd1\u5668\u7684\u6269\u5c55\u529f\u80fd\u6765\u907f\u514d\u8fd9\u4e9b\u95ee\u9898\uff0c\u5177\u4f53\u5185\u5bb9\u8bf7\u53c2\u8003 0.10 -- Configuring your compiler: Compiler extensions .","title":"1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a"},{"location":"1-6-Uninitialized-variables-and-undefined-behavior/#_1","text":"\u4e0d\u540c\u4e8e\u6709\u4e9b\u7f16\u7a0b\u8bed\u8a00\uff0cC/C++ \u5e76\u4e0d\u4f1a\u81ea\u52a8\u5730\u5c06\u5927\u90e8\u5206\u53d8\u91cf\u521d\u59cb\u5316\u4e3a\u67d0\u4e2a\u7ed9\u5b9a\u503c\uff08\u4f8b\u59820\uff09\u3002\u56e0\u6b64\uff0c\u5f53\u7f16\u8bd1\u5668\u4e3a\u53d8\u91cf\u5206\u914d\u5185\u5b58\u5730\u5740\u65f6\uff0c\u9ed8\u8ba4\u7684\u521d\u503c\u5c06\u4f1a\u662f\u4efb\u4f55\u6070\u597d\u4f4d\u4e8e\u8be5\u5730\u5740\u7684\u503c\uff08\u5783\u573e\u503c\uff09\u3002\u4e00\u4e2a\u6ca1\u6709\u88ab\u660e\u786e\u8d4b\u4e88\u6307\u5b9a\u521d\u503c\uff08\u901a\u8fc7\u521d\u59cb\u5316\u6216\u8d4b\u503c\uff09\u7684\u53d8\u91cf\u79f0\u4e3a\u672a\u521d\u59cb\u5316\u53d8\u91cf\u3002 \u4f5c\u8005\u6ce8 \u5f88\u591a\u8bfb\u8005\u60f3\u5f53\u7136\u5730\u8ba4\u4e3a\uff0c\u201d\u521d\u59cb\u5316\u7684\u201c\u548c\"\u672a\u521d\u59cb\u5316\u7684\"\u662f\u4e00\u5bf9\u4e25\u683c\u7684\u53cd\u4e49\u8bcd\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u3002\u521d\u59cb\u5316\u610f\u5473\u7740\u5bf9\u8c61\u5728\u88ab\u5b9a\u4e49\u7684\u65f6\u5019\u5373\u83b7\u5f97\u4e86\u4e00\u4e2a\u7ed9\u5b9a\u7684\u521d\u59cb\u503c\u3002\u800c\u672a\u521d\u59cb\u5316\u5219\u8868\u793a\uff0c\u5bf9\u8c61\u6ca1\u6709\u88ab\u660e\u786e\u5730\u6307\u5b9a\u4efb\u4f55\u503c\uff08\u901a\u8fc7\u5404\u79cd\u9014\u5f84\uff0c\u5305\u62ec\u8d4b\u503c\uff09\u3002\u56e0\u6b64\uff0c\u4e00\u4e2a\u5bf9\u8c61\u5982\u679c\u6ca1\u6709\u88ab\u201d\u521d\u59cb\u5316\u201c\uff0c\u4f46\u901a\u8fc7\u8d4b\u503c\u5f97\u5230\u4e86\u4e00\u4e2a\u521d\u503c\uff0c\u5b83\u4e5f\u4e0d\u518d\u662f\u201d\u672a\u521d\u59cb\u5316\u201c\u72b6\u6001\uff08\u56e0\u4e3a\u5b83\u5df2\u7ecf\u5f97\u5230\u4e86\u4e00\u4e2a\u503c\uff09 \u7b80\u5355\u603b\u7ed3\u4e00\u4e0b\uff1a - \u521d\u59cb\u5316\uff1a\u5bf9\u8c61\u5728\u88ab\u5b9a\u4e49\u7684\u540c\u65f6\u4e5f\u88ab\u8d4b\u4e88\u4e86\u521d\u503c - \u8d4b\u503c\uff1a\u5bf9\u8c61\u5728\u5b9a\u4e49\u540e\uff0c\u88ab\u8d4b\u4e88\u4e86\u4e00\u4e2a\u503c - \u672a\u521d\u59cb\u5316\uff1a\u5bf9\u8c61\u59cb\u7ec8\u6ca1\u6709\u80fd\u591f\u83b7\u5f97\u4e00\u4e2a\u660e\u786e\u6307\u5b9a\u7684\u521d\u503c\u3002 \u9898\u5916\u8bdd \u4e0d\u8fdb\u884c\u9ed8\u8ba4\u521d\u59cb\u5316\u8fd9\u4e00\u7b56\u7565\u662f\u4eceC\u8bed\u8a00\u7ee7\u627f\u800c\u6765\uff0c\u4e3b\u8981\u51fa\u4e8e\u6027\u80fd\u4f18\u5316\u8003\u8651\uff0c\u56e0\u4e3a\u5f53\u65f6\u7684\u8ba1\u7b97\u673a\u8fd0\u884c\u901f\u5ea6\u8fd8\u5f88\u6162\u3002\u4f60\u53ef\u4ee5\u60f3\u8c61\u4e00\u4e0b\uff0c\u5047\u8bbe\u6211\u4eec\u9700\u8981\u4ece\u6587\u4ef6\u8bfb\u53d6100000\u6761\u6570\u636e\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u4f60\u9700\u8981\u521b\u5efa100000\u4e2a\u53d8\u91cf\u5e76\u4f7f\u7528\u8bfb\u53d6\u7684\u6570\u636e\u6765\u586b\u5145\u5b83\u4eec\u3002 \u5982\u679c C++ \u9700\u8981\u5728\u521b\u5efa\u8fd9\u4e9b\u53d8\u91cf\u65f6\uff0c\u4f7f\u7528\u9ed8\u8ba4\u503c\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u8fd9\u5c31\u610f\u5473\u7740\u9700\u8981\u6267\u884c100000\u6b21\u521d\u59cb\u5316\uff08\u521d\u59cb\u5316\u901f\u5ea6\u5f88\u6162\uff09\uff0c\u5176\u5e26\u6765\u7684\u597d\u5904\u4e5f\u5e76\u4e0d\u5927\uff08\u6bd5\u7adf\u8fd9\u4e9b\u521d\u59cb\u5316\u5f88\u5feb\u5c06\u4f1a\u88ab\u771f\u5b9e\u503c\u8986\u76d6\uff09\u3002 \u73b0\u5982\u4eca\uff0c\u6211\u4eec\u5219\u5e94\u8be5\u7262\u8bb0\u5bf9\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u5176\u5e26\u6765\u7684\u597d\u5904\u5df2\u7ecf\u8fdc\u5927\u4e8e\u5176\u5f00\u9500\u3002\u5f53\u4f60\u719f\u7ec3\u638c\u63e1\u8fd9\u95e8\u8bed\u8a00\u540e\uff0c\u4e5f\u8bb8\u4e5f\u4f1a\u9047\u5230\u4e3a\u4e86\u4f18\u5316\u6027\u80fd\u800c\u653e\u5f03\u521d\u59cb\u5316\u7684\u60c5\u51b5\u3002\u4f46\u8fd9\u6837\u7684\u64cd\u4f5c\u4e00\u5b9a\u662f\u4f60\u6df1\u601d\u719f\u8651\u540e\u6709\u610f\u800c\u4e3a\u4e4b\u7684\u3002 \u4f7f\u7528\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\u7684\u503c\uff0c\u53ef\u80fd\u4f1a\u5e26\u6765\u96be\u4ee5\u9884\u6599\u7684\u7ed3\u679c\u3002\u4ee5\u5982\u4e0b\u4ee3\u7801\u4e3a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { // define an integer variable named x int x ; // this variable is uninitialized because we haven't given it a value // print the value of x to the screen std :: cout << x ; // who knows what we'll get, because x is uninitialized return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u8ba1\u7b97\u673a\u4f1a\u4e3ax\u5206\u914d\u67d0\u7a7a\u95f2\u7684\u5185\u5b58\u3002\u8be5\u5185\u5b58\u4e2d\u5b58\u653e\u7684\u503c\u4f1a\u901a\u8fc7 std::cout \u8f93\u51fa\u5230\u63a7\u5236\u53f0\u5e76\u6253\u5370\uff0c\u5176\u6253\u5370\u51fa\u7684\u7ed3\u679c\u53ef\u80fd\u662f\u67d0\u4e2a\u503c\uff08\u88ab\u5f53\u505a\u6574\u578b\u6765\u89e3\u6790\uff09\u3002\u4f46\u7a76\u7adf\u662f\u4ec0\u4e48\u503c\u5462\uff1f\u7b54\u6848\u662f\uff1a\u201d\u6ca1\u6709\u4eba\u77e5\u9053\u201c\uff0c\u6216\u8005\u8bf4\uff0c\u6bcf\u6b21\u8fd0\u884c\u7a0b\u5e8f\uff0c\u7b54\u6848\u90fd\u53ef\u80fd\u662f\u4e0d\u4e00\u6837\u7684\u3002\u4f5c\u8005\u5728\u4f7f\u7528 Visual Studio \u8fdb\u884c\u6d4b\u8bd5\u7684\u65f6\u5019\uff0c\u6253\u5370\u51fa\u7684\u503c\u662f 7177728 \uff0c\u518d\u6b21\u8fd0\u884c\u65f6\u6253\u5370\u7684\u503c\u5c31\u7f16\u7a0b\u4e86 5277592 \u3002\u4f60\u53ef\u4ee5\u81ea\u5df1\u5c1d\u8bd5\u5e76\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\uff08\u4e0d\u7528\u62c5\u5fc3\uff0c\u8fd9\u5e76\u4e0d\u4f1a\u635f\u574f\u4f60\u7684\u7535\u8111\uff09\u3002 \u6ce8\u610f \u6709\u4e00\u4e9b\u7f16\u8bd1\u5668\uff0c\u4f8b\u5982 Visual Studio\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fdebug\u7248\u672c\u914d\u7f6e\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u5c06\u5185\u5b58\u521d\u59cb\u5316\u4e3a\u67d0\u4e9b\u9884\u8bbe\u503c\u3002\u4f46\u662f\u5f53\u4f60\u4f7f\u7528release\u7248\u672c\u914d\u7f6e\u65f6\uff0c\u5219\u4e0d\u4f1a\u6709\u4e0a\u8ff0\u884c\u4e3a\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u60f3\u8981\u81ea\u884c\u5c1d\u8bd5\u4e0a\u8ff0\u7a0b\u5e8f\uff0c\u8bf7\u786e\u4fdd\u81ea\u5df1\u4f7f\u7528\u7684\u662frelease\u7248\u672c\u7684\u6784\u5efa\u914d\u7f6e (\u53c2\u8003 0.9 -- Configuring your compiler: Build configurations ). \u4f8b\u5982\uff0c\u5f53\u4f60\u5728Visual Studio\u4e2d\u4f7f\u7528debug\u914d\u7f6e\u7f16\u8bd1\u5e76\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u65f6\uff0c\u8f93\u51fa\u7ed3\u679c\u59cb\u7ec8\u4f1a\u662f -858993460\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u503c\uff08\u88ab\u89e3\u91ca\u4e3a\u6574\u578b\uff09\u662f Visual Studio \u5728 debug \u914d\u7f6e\u4e0b\u521d\u59cb\u5316\u5185\u5b58\u4f7f\u7528\u7684\u503c\u3002 \u5927\u591a\u6570\u7684\u73b0\u4ee3\u7f16\u8bd1\u5668\u90fd\u80fd\u591f\u68c0\u6d4b\u5230\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u88ab\u8d4b\u4e88\u4e86\u521d\u503c\u3002\u5982\u679c\u80fd\u591f\u68c0\u6d4b\u5230\uff0c\u5219\u5728\u7f16\u8bd1\u65f6\u4f1a\u4ea7\u751f\u4e00\u4e2a\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u5728 Visual Studio \u4e2d\u7f16\u8bd1\u4e0a\u8ff0\u7a0b\u5e8f\u65f6\uff0c\u4f1a\u8f93\u51fa\u5982\u4e0b\u7684\u544a\u8b66\u4fe1\u606f\uff1a 1 c:\\VCprojects\\test\\test.cpp(11) : warning C4700: uninitialized local variable 'x' used \u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u4e0d\u5141\u8bb8\u4f60\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\uff0c\u53ef\u4ee5\u91c7\u7528\u5982\u4e0b\u65b9\u5f0f\u7ed5\u8fc7\u9650\u5236\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include void doNothing ( int & ) // Don't worry about what & is for now, we're just using it to trick the compiler into thinking variable x is used { } int main () { // define an integer variable named x int x ; // this variable is uninitialized doNothing ( x ); // make the compiler think we're assigning a value to this variable // print the value of x to the screen (who knows what we'll get, because x is uninitialized) std :: cout << x ; return 0 ; } \u4f7f\u7528\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\u662f\u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u72af\u7684\u9519\u8bef\u4e4b\u4e00\uff0c\u4e0d\u5e78\u7684\u662f\uff0c\u8be5\u9519\u8bef\u4e5f\u5e38\u5e38\u4f1a\u57cb\u4e0b\u6700\u96be\u4ee5\u8c03\u8bd5\u7684\u95ee\u9898\uff08\u56e0\u4e3a\u7a0b\u5e8f\u53ef\u80fd\u53ef\u4ee5\u6b63\u786e\u8fd0\u884c\uff0c\u5982\u679c\u8be5\u5185\u5b58\u4e2d\u6070\u597d\u5305\u542b\u4e86\u4e00\u4e2a\u5408\u7406\u7684\u503c\uff0c\u4f8b\u59820\uff09\u3002 \u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728\u6700\u4f73\u5b9e\u8df5\u4e2d\u6211\u4eec\u5f3a\u8c03\uff0c\u4e00\u5b9a\u8981\u786e\u4fdd\u521d\u59cb\u5316\u53d8\u91cf\u3002","title":"\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf"},{"location":"1-6-Uninitialized-variables-and-undefined-behavior/#_2","text":"\u4f7f\u7528\u672a\u5b9a\u4e49\u53d8\u91cf\u4e2d\u7684\u503c\uff0c\u662f\u6211\u4eec\u9047\u5230\u7684\u7b2c\u4e00\u79cd\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u4f8b\u5b50\u3002\u672a\u5b9a\u4e49\u884c\u4e3a\uff08\u5e38\u5e38\u7f29\u5199\u4e3a UB\uff09\u6307\u7684\u662f\u6267\u884c\u4ee3\u7801\u7684\u884c\u4e3a\u6ca1\u6709\u88ab C++ \u8bed\u8a00\u660e\u786e\u5b9a\u4e49\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cC++ \u8bed\u8a00\u5e76\u6ca1\u6709\u660e\u786e\u7684\u89c4\u5219\u80fd\u591f\u7528\u4e8e\u786e\u5b9a\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002\u4ece\u7ed3\u679c\u4e0a\u6765\u770b\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u4e86\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\uff0c\u5219\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 \u4ee3\u7801\u7684\u672a\u5b9a\u4e49\u884c\u4e3a\u53ef\u80fd\u8868\u73b0\u51fa\u5982\u4e0b\u75c7\u72b6\uff1a \u7a0b\u5e8f\u6bcf\u6b21\u8fd0\u884c\u90fd\u53ef\u80fd\u5f97\u5230\u4e0d\u540c\u7684\u7ed3\u679c\uff1b \u7a0b\u5e8f\u6bcf\u6b21\u8fd0\u884c\u90fd\u8f93\u51fa\u76f8\u540c\u7684\u9519\u8bef\u7ed3\u679c\uff1b \u7a0b\u5e8f\u6bcf\u6b21\u8fd0\u884c\u7684\u884c\u4e3a\u90fd\u4e0d\u4e00\u81f4 (\u6709\u65f6\u5019\u7ed3\u679c\u6b63\u786e\uff0c\u6709\u65f6\u5019\u4e0d\u6b63\u786e)\uff1b \u7a0b\u5e8f\u770b\u8d77\u6765\u80fd\u591f\u6b63\u786e\u8fd0\u884c\uff0c\u4f46\u662f\u4e00\u6bb5\u65f6\u95f4\u540e\u53c8\u4f1a\u8f93\u51fa\u9519\u8bef\u7684\u7ed3\u679c\uff1b \u7a0b\u5e8f\u5d29\u6e83\u9000\u51fa\uff0c\u8fd9\u79cd\u5d29\u6e83\u53ef\u80fd\u5728\u7a0b\u5e8f\u542f\u52a8\u540e\u7acb\u5373\u53d1\u751f\uff0c\u4e5f\u53ef\u80fd\u8fd0\u884c\u4e00\u6bb5\u65f6\u95f4\u540e\u53d1\u751f\uff1b \u7a0b\u5e8f\u7ecf\u67d0\u4e9b\u7f16\u8bd1\u5668\u7f16\u8bd1\u540e\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\uff0c\u4f46\u6709\u4e9b\u7f16\u8bd1\u5668\u5219\u4e0d\u884c\uff1b \u7a0b\u5e8f\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u76f4\u5230\u4f60\u4fee\u6539\u4e86\u67d0\u4e9b\u770b\u4e0a\u6beb\u4e0d\u76f8\u5e72\u7684\u4ee3\u7801\u3002 \u6216\u8005\uff0c\u7a0b\u5e8f\u751a\u81f3\u80fd\u591f\u6b63\u786e\u8fd0\u884c\u5e76\u5f97\u5230\u6b63\u786e\u7ed3\u679c\u3002\u6240\u8c13\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u5c31\u662f\u4f60\u6c38\u8fdc\u4e0d\u77e5\u9053\u4f60\u80fd\u5f97\u5230\u4ec0\u4e48\u6837\u7684\u7ed3\u679c\uff0c\u65e0\u6cd5\u9884\u6d4b\u6bcf\u6b21\u8fd0\u884c\u7684\u884c\u4e3a\uff0c\u4e5f\u65e0\u6cd5\u9884\u6d4b\u4f60\u505a\u51fa\u67d0\u4e9b\u4fee\u6539\u540e\u7684\u5f71\u54cd\u3002 \u5728 C++ \u4e2d\uff0c\u5982\u679c\u4f60\u4e0d\u591f\u6d88\u606f\uff0c\u90a3\u4e48\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u9014\u5f84\u6709\u5f88\u591a\u3002\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u9047\u5230\u5177\u4f53\u95ee\u9898\u65f6\u8fdb\u884c\u8bb2\u89e3\u3002\u8bf7\u6ce8\u610f\u8bb0\u5f55\u8fd9\u4e9b\u6848\u4f8b\u5e76\u5207\u8bb0\u8981\u907f\u514d\u5b83\u4eec\u3002 \u6cd5\u5219 \u6ce8\u610f\u907f\u514d\u53ef\u80fd\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u5404\u79cd\u60c5\u51b5\uff0c\u4f8b\u5982\u4f7f\u7528\u672a\u521d\u59cb\u5316\u53d8\u91cf\u3002 \u4f5c\u8005\u6ce8 \u8bfb\u8005\u8bc4\u8bba\u4e2d\u6700\u5e38\u89c1\u7684\u95ee\u9898\u4e4b\u4e00\u4fbf\u662f\uff1a\u201d\u4f60\u8bf4\u4e0d\u80fd\u8fd9\u6837\u505a\uff0c\u4f46\u6211\u504f\u504f\u8fd9\u6837\u505a\u4e86\uff0c\u800c\u4e14\u7a0b\u5e8f\u4e5f\u80fd\u591f\u6b63\u5e38\u8fd0\u884c\uff01\u4e3a\u4ec0\u4e48\uff1f\u201c \u5173\u4e8e\u8fd9\u4e2a\u95ee\u9898\u901a\u5e38\u6709\u4e24\u4e2a\u7b54\u6848\u3002\u6700\u5e38\u89c1\u7684\u7b54\u6848\u662f\uff0c\u4f60\u7684\u7a0b\u5e8f\u5b9e\u9645\u4e0a\u8868\u73b0\u51fa\u4e86\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u53ea\u4e0d\u8fc7\u8be5\u672a\u5b9a\u4e49\u884c\u4e3a\u6070\u5de7\uff08\u6682\u65f6\uff09\u8f93\u51fa\u4e86\u4f60\u671f\u671b\u7684\u7ed3\u679c\u3002\u6539\u5929\uff08\u6216\u8005\u4f7f\u7528\u5176\u4ed6\u7f16\u8bd1\u5668\u3001\u4f7f\u7528\u5176\u4ed6\u8ba1\u7b97\u673a\uff09\u518d\u8fd0\u884c\u65f6\uff0c\u53ef\u80fd\u672a\u5fc5\u5982\u6b64\u3002 \u8fd9\u4e2a\u95ee\u9898\u8fd8\u53ef\u4ee5\u8fd9\u6837\u56de\u7b54\uff1a\u6709\u4e9b\u7f16\u8bd1\u5668\u7684\u4f5c\u8005\uff0c\u5728\u5b9e\u73b0\u7279\u5b9a\u8bed\u8a00\u7684\u9700\u6c42\u65f6\uff0c\u8ba4\u4e3a\u67d0\u4e9b\u89c4\u5219\u8fc7\u4e8e\u4e25\u683c\u662f\u6ca1\u6709\u5fc5\u8981\u7684\u3002\u4f8b\u5982\uff0c\u6807\u51c6\u53ef\u80fd\u4f1a\u8bf4\uff0cX \u5fc5\u987b\u4f4d\u4e8e Y \u4e4b\u524d\u53d1\u751f\u3002\u4f46\u662f\u7f16\u8bd1\u5668\u4f5c\u8005\u786e\u8ba4\u4e3a\u8fd9\u4e48\u505a\u662f\u6ca1\u6709\u5fc5\u8981\u7684\uff0c\u5e76\u4e14\u4f7f\u5f97 Y \u80fd\u591f\u5728\u6ca1\u6709\u6267\u884cX\u7684\u524d\u63d0\u4e0b\u6b63\u786e\u5de5\u4f5c\u3002\u8fd9\u4e48\u505a\u4e0d\u4f1a\u5f71\u54cd\u5230\u6b63\u786e\u7684\u4ee3\u7801\uff0c\u4f46\u662f\u53ef\u80fd\u4f1a\u9020\u6210\u4e0d\u7b26\u5408\u89c4\u8303\u7684\u4ee3\u7801\u80fd\u591f\u6b63\u786e\u6267\u884c\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u8bf4\uff1a\u4f60\u4f7f\u7528\u7684\u7f16\u8bd1\u5668\u53ef\u80fd\u5e76\u6ca1\u6709\u5b8c\u5168\u9075\u5b88\u6807\u51c6\u3002\u8fd9\u79cd\u60c5\u51b5\u662f\u5b58\u5728\u7684\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5173\u95ed\u7f16\u8bd1\u5668\u7684\u6269\u5c55\u529f\u80fd\u6765\u907f\u514d\u8fd9\u4e9b\u95ee\u9898\uff0c\u5177\u4f53\u5185\u5bb9\u8bf7\u53c2\u8003 0.10 -- Configuring your compiler: Compiler extensions .","title":"\u672a\u5b9a\u4e49\u884c\u4e3a"},{"location":"1-7-Keywords-and-naming-identifiers/","tags":["identifier"],"text":"Key Takeaway \u5173\u952e\u5b57 C++ \u9884\u7559\u4e86 92 \u4e2a\u8bcd\u81ea\u7528(\u5bf9\u4e8e C++20 \u800c\u8a00) \u3002\u8fd9\u4e9b\u8bcd\u79f0\u4e3a \u5173\u952e\u5b57 (\u6216\u4fdd\u7559\u5b57)\uff0c\u6bcf\u4e2a\u5173\u952e\u5b57\u5728C++\u4e2d\u90fd\u6709\u5176\u7279\u5b9a\u7684\u542b\u4e49\u3002 \u4e0b\u8868\u5217\u4e3e\u4e86 C++ 20 \u4e2d\u7684\u5168\u90e8\u5173\u952e\u5b57\uff1a \u5173\u952e\u5b57\u540e\u6709\u6807\u8bc6 C++20 \u7684\uff0c\u5c5e\u4e8e C++20 \u65b0\u589e\u7684\u5173\u952e\u5b57\u3002\u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u4e0d\u652f\u6301 C++ 20\uff08\u6216\u8005\u5176\u5177\u5907 C++20 \u529f\u80fd\u4f46\u9ed8\u8ba4\u662f\u5173\u95ed\u7684\uff09\uff0c\u8fd9\u4e9b\u5173\u952e\u5b57\u5c06\u4e0d\u5177\u5907\u5b9e\u9645\u7528\u9014\u3002 C++ \u8fd8\u5b9a\u4e49\u4e86\u4e00\u4e9b\u7279\u6b8a\u7684\u6807\u8bc6\u7b26\uff1a override \u3001 final \u3001 import \u548c module \u3002\u8fd9\u4e9b\u6807\u8bc6\u7b26\u5728\u7279\u5b9a\u4e0a\u4e0b\u6587\u4e2d\u5177\u6709\u7279\u6b8a\u7684\u542b\u4e49\uff0c\u4f46\u5b83\u4eec\u5e76\u4e0d\u662fC++\u4fdd\u7559\u5b57\u3002 \u4f60\u5df2\u7ecf\u63a5\u89e6\u8fc7\u5176\u4e2d\u7684\u4e00\u4e9b\u5173\u952e\u5b57\uff0c\u5305\u62ec int \u548c return \u3002\u8fd9\u4e9b\u5173\u952e\u5b57\u3001\u6807\u8bc6\u7b26\u4ee5\u53ca\u4e00\u7ec4\u8fd0\u7b97\u7b26\u5c31\u6784\u6210\u4e86 C++ \u8bed\u8a00\u7684\u5168\u90e8\uff08\u9884\u5904\u7406\u5668\u6307\u4ee4\u9664\u5916\uff09\u3002\u7531\u4e8e\u5173\u952e\u5b57\u548c\u7279\u6b8a\u7684\u6807\u8bc6\u7b26\u5177\u6709\u7279\u6b8a\u542b\u4e49\uff0c\u56e0\u6b64\u4f60\u7684 IDE \u901a\u5e38\u4f1a\u5c06\u5176\u6807\u8bb0\u4e3a\u4e0d\u540c\u7684\u989c\u8272\u4ee5\u4f7f\u5176\u66f4\u52a0\u9192\u76ee\u3002 \u5728\u5b8c\u6210\u4e86\u672c\u6559\u7a0b\u7684\u5b66\u4e60\u540e\uff0c\u4f60\u4f1a\u7406\u89e3\u4e0a\u8ff0\u5173\u952e\u5b57\u4e2d\u51e0\u4e4e\u5168\u90e8\u7684\u529f\u80fd\uff01 \u547d\u540d\u89c4\u5219 \u63d0\u9192\u4e00\u4e0b\uff0c\u53d8\u91cf\uff08\u6216\u51fd\u6570\u3001\u7c7b\u578b\u4ee5\u53ca\u5176\u4ed6\u4efb\u4f55\u5143\u7d20\uff09\u7684\u540d\u79f0\u90fd\u79f0\u4e3a\u6807\u8bc6\u7b26\u3002C++ \u5141\u8bb8\u4f60\u7075\u6d3b\u5730\u9009\u62e9\u6807\u8bc6\u7b26\u3002\u4e0d\u8fc7\uff0c\u5728\u9009\u62e9\u547d\u540d\u6807\u8bc6\u7b26\u65f6\uff0c\u4f60\u9700\u8981\u9075\u5faa\u4ee5\u4e0b\u89c4\u5219\uff1a \u6807\u8bc6\u7b26\u4e0d\u80fd\u662f\u5173\u952e\u5b57\uff0c\u5173\u952e\u5b57\u662f\u8bed\u8a00\u7684\u4fdd\u7559\u5b57\uff1b \u6807\u8bc6\u7b26\u53ea\u80fd\u4f7f\u7528\u5927\u5c0f\u5199\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6807\u8bc6\u7b26\u4e2d\u4e0d\u80fd\u5305\u542b\u7b26\u53f7\uff08\u9664\u4e86\u4e0b\u5212\u7ebf\uff09\u6216\u8005\u7a7a\u767d\u7b26\uff08\u7a7a\u683c\u6216\u5236\u8868\u7b26\uff09\uff1b \u6807\u8bc6\u7b26\u5fc5\u987b\u4ee5\u5b57\u6bcd\uff08\u5927\u5c0f\u5199\u5747\u53ef\uff09\u6216\u4e0b\u5212\u7ebf\u5f00\u5934\uff0c\u4e0d\u80fd\u591f\u4ee5\u6570\u5b57\u5f00\u5934\uff1b C++ \u662f\u5927\u5c0f\u5199\u654f\u611f\u7684\uff0c\u56e0\u6b64\u5b83\u4f1a\u533a\u5206\u5927\u5c0f\u5199\u5b57\u6bcd\u3002 nvalue \u548c nValue \u662f\u4e0d\u540c\u7684\uff0c\u548c NVALUE \u4e5f\u4e0d\u540c\uff0c \u547d\u540d\u6700\u4f73\u5b9e\u8df5 \u73b0\u5728\uff0c\u60f3\u5fc5\u4f60\u5df2\u7ecf\u77e5\u9053 \u5982\u4f55 \u4e3a\u53d8\u91cf\u547d\u540d\u4e86\uff0c\u63a5\u4e0b\u6765\u8ba9\u6211\u4eec\u804a\u804a\uff0c\u4f60 \u5e94\u8be5\u5982\u4f55 \u4e3a\u53d8\u91cf\uff08\u6216\u51fd\u6570\uff09\u547d\u540d\u3002 \u9996\u5148\uff0cC++ \u4e2d\u7ea6\u5b9a\u4fd7\u6210\u7684\u662f\uff0c\u53d8\u91cf\u540d\u5e94\u8be5\u4ee5\u5c0f\u5199\u5b57\u6bcd\u5f00\u5934\u3002\u5982\u679c\u53d8\u91cf\u540d\u662f\u4e00\u4e2a\u5355\u8bcd\uff0c\u5219\u6574\u4e2a\u5355\u8bcd\u90fd\u5e94\u8be5\u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd\u3002 1 2 3 4 5 int value ; // correct int Value ; // incorrect (should start with lower case letter) int VALUE ; // incorrect (should start with lower case letter) int VaLuE ; // incorrect (see your psychiatrist) ;) \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u51fd\u6570\u540d\u540c\u6837\u4e5f\u662f\u5c0f\u5199\u5b57\u6bcd\u5f00\u5934\u7684\uff08\u5c3d\u7ba1\u5728\u8fd9\u4e00\u70b9\u4e0a\u6709\u5f88\u591a\u4e0d\u540c\u610f\u89c1\uff09\u3002\u6211\u4eec\u4f1a\u9075\u5faa\u8fd9\u4e00\u60ef\u4f8b\uff0c\u56e0\u4e3a main \u51fd\u6570\u5c31\u662f\u5c0f\u5199\u5b57\u6bcd\u5f00\u5934\u7684\uff08\u800c\u4e14 main \u51fd\u6570\u662f\u4efb\u4f55\u7a0b\u5e8f\u90fd\u9700\u8981\u6709\u7684\uff09\u3002\u6b64\u5916\uff0cC++\u6807\u51c6\u5e93\u4e2d\u7684\u6240\u6709\u51fd\u6570\u4e5f\u90fd\u9075\u5faa\u8fd9\u4e00\u539f\u5219\u3002 \u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684\u547d\u540d\u6807\u8bc6\u7b26\u901a\u5e38\u7528\u4e8e\u547d\u540d\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u578b\uff08\u4f8b\u5982\u7ed3\u6784\u4f53\u3001\u7c7b\u548c\u679a\u4e3e\uff0c\u7a0d\u540e\u6211\u4eec\u4f1a\u4ecb\u7ecd\u8fd9\u4e9b\u7c7b\u578b\uff09\u3002 \u5982\u679c\u53d8\u91cf\u540d\u6216\u51fd\u6570\u540d\u7531\u591a\u4e2a\u5355\u8bcd\u7ec4\u6210\uff0c\u5e38\u89c1\u7684\u547d\u540d\u60ef\u4f8b\u6709\u4e24\u79cd\uff0c\u4e00\u79cd\u662f\u4f7f\u7528\u4e0b\u5212\u7ebf\u5206\u5272\u5355\u8bcd\uff0c\u79f0\u4e3a \u86c7\u5f62\u547d\u540d\u6cd5(snake case) \uff0c\u53e6\u5916\u4e00\u79cd\u662f\u4f7f\u7528\u5927\u5c0f\u5199\u95f4\u9694\u7684\u65b9\u6cd5\uff08\u6709\u65f6\u5019\u79f0\u4e3a\u9a7c\u5cf0\u547d\u540d\u6cd5\uff0c\u56e0\u4e3a\u5927\u5199\u5b57\u6bcd\u6bd4\u8f83\u7a81\u51fa\uff0c\u5c31\u50cf\u9a86\u9a7c\u7684\u9a7c\u5cf0\u4e00\u6837\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 int my_variable_name ; // correct (separated by underscores/snake_case) int my_function_name (); // correct (separated by underscores/snake_case) int myVariableName ; // correct (intercapped/CamelCase) int myFunctionName (); // correct (intercapped/CamelCase) int my variable name ; // invalid (whitespace not allowed) int my function name (); // invalid (whitespace not allowed) int MyVariableName ; // valid but incorrect (should start with lower case letter) int MyFunctionName (); // valid but incorrect (should start with lower case letter) \u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528\u9a7c\u5cf0\u547d\u540d\u6cd5\uff0c\u56e0\u4e3a\u5b83\u53ef\u8bfb\u6027\u66f4\u597d\uff08\u5bf9\u4e8e\u86c7\u5f62\u547d\u540d\u6cd5\u6765\u8bf4\uff0c\u6709\u65f6\u4e0b\u5212\u7ebf\u5bb9\u6613\u548c\u7a7a\u683c\u6df7\u6dc6\uff09\u3002\u4f46\u662f\uff0c\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u5f88\u5e38\u89c1\uff0cC++ \u7684\u6807\u51c6\u5e93\u5728\u547d\u540d\u53d8\u91cf\u548c\u51fd\u6570\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u7684\u5c31\u662f\u57fa\u4e8e\u4e0b\u5212\u7ebf\u7684\u86c7\u5f62\u547d\u540d\u6cd5\u3002\u6709\u65f6\u5019\uff0c\u4f60\u751a\u81f3\u4f1a\u9047\u5230\u6df7\u7528\u4e24\u79cd\u547d\u540d\u6cd5\u7684\u60c5\u51b5\uff0c\u86c7\u5f62\u547d\u540d\u6cd5\u7528\u4e8e\u53d8\u91cf\u540d\u800c\u9a7c\u5cf0\u547d\u540d\u6cd5\u5219\u7528\u4e8e\u51fd\u6570\u540d\u3002 \u5982\u679c\u4f60\u7684\u5de5\u4f5c\u662f\u5728\u5176\u4ed6\u4eba\u7684\u4ee3\u7801\u4e0a\u8fdb\u884c\u5f00\u53d1\uff0c\u90a3\u4e48\u8fd9\u4e9b\u5c31\u4e0d\u91cd\u8981\u4e86\u3002\u56e0\u4e3a\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6700\u597d\u7684\u505a\u6cd5\u662f\u9075\u5faa\u4ed6\u4eba\u7684\u547d\u540d\u89c4\u5219\u800c\u4e0d\u662f\u6b7b\u677f\u7684\u9075\u5faa\u4e0a\u8ff0\u539f\u5219\u3002 \u6cd5\u5219 \u5f53\u57fa\u4e8e\u5df2\u6709\u7684\u4ee3\u7801\u8fdb\u884c\u5f00\u53d1\u65f6\uff0c\u8bf7\u4f7f\u7528\u8be5\u4ee3\u7801\u7684\u547d\u540d\u7ea6\u5b9a\uff08\u65e2\u662f\u5b83\u7684\u98ce\u683c\u4e0d\u7b26\u5408\u6700\u4f73\u5b9e\u8df5\uff09\u3002\u5f53\u4f60\u7f16\u5199\u4e00\u4e2a\u65b0\u7684\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u8bf7\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\u3002 \u5176\u6b21\uff0c\u547d\u540d\u65f6\u5e94\u5f53\u907f\u514d\u4ee5\u4e0b\u5212\u7ebf\u5f00\u5934\uff0c\u4e0b\u5212\u7ebf\u5f00\u5934\u7684\u540d\u79f0\u5e38\u5e38\u88ab\u64cd\u4f5c\u7cfb\u7edf\u3001\u5e93\u6216\u7f16\u8bd1\u5668\u9884\u7559\u4f7f\u7528\u3002 \u7b2c\u4e09\uff0c\u547d\u540d\u5e94\u5f53\u80fd\u591f\u8868\u660e\u8be5\u53d8\u91cf\u5b58\u50a8\u6570\u636e\u7684\u542b\u4e49\uff08\u5c24\u5176\u662f\u5f53\u8ba1\u91cf\u5355\u4f4d\u4e0d\u660e\u663e\u7684\u65f6\u5019\uff09\u3002\u53d8\u91cf\u540d\u5e94\u8be5\u80fd\u591f\u5e2e\u52a9\u5176\u4ed6\u5bf9\u4f60\u4ee3\u7801\u4e0d\u751a\u4e86\u89e3\u7684\u5f00\u53d1\u8005\u5feb\u901f\u7406\u89e3\u5176\u7528\u9014\u3002\u751a\u81f3\uff0c\u5f53\u4f60\u5728\u4e09\u4e2a\u6708\u4e4b\u540e\u518d\u770b\u81ea\u5df1\u7684\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u4f60\u90fd\u6709\u53ef\u80fd\u770b\u4e0d\u61c2\u81ea\u5df1\u7684\u4ee3\u7801\uff0c\u90a3\u65f6\u5019\u4f60\u5c31\u4f1a\u5e86\u5e78\u81ea\u5df1\u8d77\u4e86\u6613\u4e8e\u7406\u89e3\u3001\u542b\u4e49\u6e05\u6670\u7684\u53d8\u91cf\u540d\u3002 \u4e0d\u8fc7\uff0c\u4e3a\u4e00\u4e9b\u65e0\u5173\u7d27\u8981\u7684\u53d8\u91cf\u53d6\u4e00\u4e2a\u8fc7\u5206\u590d\u6742\u7684\u540d\u79f0\u53cd\u5012\u6709\u788d\u4e8e\u7406\u89e3\uff0c\u5176\u5371\u5bb3\u4e0d\u4e9a\u4e8e\u7ed9\u91cd\u8981\u7684\u53d8\u91cf\u53d6\u4e00\u4e2a\u610f\u4e49\u6a21\u7cca\u7684\u540d\u5b57\u3002\u56e0\u6b64\uff0c\u547d\u540d\u7684\u4e00\u4e2a\u539f\u5219\u53ef\u4ee5\u662f\uff0c\u8ba9\u53d8\u91cf\u540d\u7684\u957f\u5ea6\u7b26\u5408\u5176\u88ab\u4f7f\u7528\u7684\u9891\u5ea6\u3002\u65e0\u5173\u7d27\u8981\u7684\u53d8\u91cf\u540d\u5e94\u8be5\u4f7f\u7528\u66f4\u77ed\u7684\u53d8\u91cf\u540d\uff08\u4f8b\u5982 i \uff09\u3002\u88ab\u5e7f\u6cdb\u4f7f\u7528\u7684\u53d8\u91cf\uff08\u4f8b\u5982\u67d0\u4e2a\u5728\u591a\u5904\u88ab\u8c03\u7528\u7684\u51fd\u6570\uff09\u5219\u503c\u5f97\u62e5\u6709\u4e00\u4e2a\u66f4\u957f\u3001\u66f4\u5177\u89e3\u91ca\u6027\u7684\u53d8\u91cf\u540d\uff08\u4f8b\u5982\uff0c openFileOnDisk \u4f1a\u6bd4 open \u66f4\u5408\u9002\uff09\u3002 \u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\uff0c\u90fd\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u7f29\u5199\u3002\u5c3d\u7ba1\u4f7f\u7528\u7f29\u5199\u53ef\u4ee5\u7a0d\u5fae\u63d0\u9ad8\u4f60\u6572\u4ee3\u7801\u7684\u901f\u5ea6\uff0c\u5b83\u4f1a\u5bfc\u81f4\u4f60\u7684\u4ee3\u7801\u96be\u4ee5\u88ab\u7406\u89e3\u3002\u65e2\u662f\u6709\u4e9b\u7f29\u5199\u5177\u6709\u660e\u786e\u7684\u542b\u4e49\uff0c\u4f46\u8bfb\u8005\u5728\u9047\u5230\u5b83\u7684\u65f6\u5019\u4ecd\u7136\u9700\u8981\u82b1\u4e9b\u65f6\u95f4\u6765\u641e\u6e05\u695a\u5b83\u7684\u542b\u4e49\u3002\u4ee3\u7801\u88ab\u9605\u8bfb\u7684\u65f6\u95f4\u8fdc\u957f\u4e8e\u5b83\u88ab\u7f16\u5199\u7684\u65f6\u95f4\uff0c\u4f60\u5728\u5199\u4ee3\u7801\u4e0a\u8282\u7701\u4e0b\u4e86\u7684\u65f6\u95f4\uff0c\u5c06\u6765\u90fd\u4f1a\u6d6a\u8d39\u5728\u5176\u4ed6\u9605\u8bfb\u8005\uff08\u751a\u81f3\u662f\u4f60\u81ea\u5df1\uff09\u7684\u8eab\u4e0a\u3002\u5982\u679c\u4f60\u5e0c\u671b\u52a0\u5feb\u7f16\u5199\u4ee3\u7801\u7684\u901f\u5ea6\uff0c\u8bf7\u4f7f\u7528\u7f16\u8f91\u5668\u7684\u81ea\u52a8\u8865\u5168\u529f\u80fd\u3002 \u6700\u540e\uff0c\u6e05\u6670\u660e\u786e\u7684\u6ce8\u91ca\u4e5f\u5927\u6709\u52a9\u76ca\u3002\u4f8b\u5982\uff0c\u53d8\u91cf numberOfChars \u7528\u4e8e\u5b58\u653e\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u7684\u4e2a\u6570\u3002\u4f46\u662f\uff0c\u5b57\u7b26\u4e32\u201cHello World!\u201d\u4e2d\u7a76\u7adf\u5305\u542b\u51e0\u4e2a\u5b57\u7b26\u5462\uff1f10\u4e2a\uff0c11\u4e2a\u8fd8\u662f12\u4e2a\uff1f\u8fd9\u53d6\u51b3\u4e8e\u4f60\u662f\u5426\u8003\u8651\u7a7a\u683c\u548c\u6807\u70b9\u7b26\u53f7\u3002\u4f46\u662f\uff0c\u5c06\u8be5\u53d8\u91cf\u547d\u540d\u4e3a numberOfCharsIncludingWhitespaceAndPunctuation \u5c31\u592a\u957f\u4e86\uff0c\u6700\u597d\u7684\u65b9\u5f0f\u662f\u5728\u5b9a\u4e49\u53d8\u91cf\u65f6\u4f7f\u7528\u6ce8\u91ca\u7684\u65b9\u5f0f\u5e2e\u52a9\u8bfb\u8005\u53bb\u7406\u89e3\u3002 1 2 // holds number of chars in a piece of text -- including whitespace and punctuation! int numberOfChars ;","title":"1.7 - \u5173\u952e\u5b57\u548c\u6807\u8bc6\u7b26"},{"location":"1-7-Keywords-and-naming-identifiers/#_1","text":"C++ \u9884\u7559\u4e86 92 \u4e2a\u8bcd\u81ea\u7528(\u5bf9\u4e8e C++20 \u800c\u8a00) \u3002\u8fd9\u4e9b\u8bcd\u79f0\u4e3a \u5173\u952e\u5b57 (\u6216\u4fdd\u7559\u5b57)\uff0c\u6bcf\u4e2a\u5173\u952e\u5b57\u5728C++\u4e2d\u90fd\u6709\u5176\u7279\u5b9a\u7684\u542b\u4e49\u3002 \u4e0b\u8868\u5217\u4e3e\u4e86 C++ 20 \u4e2d\u7684\u5168\u90e8\u5173\u952e\u5b57\uff1a \u5173\u952e\u5b57\u540e\u6709\u6807\u8bc6 C++20 \u7684\uff0c\u5c5e\u4e8e C++20 \u65b0\u589e\u7684\u5173\u952e\u5b57\u3002\u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u4e0d\u652f\u6301 C++ 20\uff08\u6216\u8005\u5176\u5177\u5907 C++20 \u529f\u80fd\u4f46\u9ed8\u8ba4\u662f\u5173\u95ed\u7684\uff09\uff0c\u8fd9\u4e9b\u5173\u952e\u5b57\u5c06\u4e0d\u5177\u5907\u5b9e\u9645\u7528\u9014\u3002 C++ \u8fd8\u5b9a\u4e49\u4e86\u4e00\u4e9b\u7279\u6b8a\u7684\u6807\u8bc6\u7b26\uff1a override \u3001 final \u3001 import \u548c module \u3002\u8fd9\u4e9b\u6807\u8bc6\u7b26\u5728\u7279\u5b9a\u4e0a\u4e0b\u6587\u4e2d\u5177\u6709\u7279\u6b8a\u7684\u542b\u4e49\uff0c\u4f46\u5b83\u4eec\u5e76\u4e0d\u662fC++\u4fdd\u7559\u5b57\u3002 \u4f60\u5df2\u7ecf\u63a5\u89e6\u8fc7\u5176\u4e2d\u7684\u4e00\u4e9b\u5173\u952e\u5b57\uff0c\u5305\u62ec int \u548c return \u3002\u8fd9\u4e9b\u5173\u952e\u5b57\u3001\u6807\u8bc6\u7b26\u4ee5\u53ca\u4e00\u7ec4\u8fd0\u7b97\u7b26\u5c31\u6784\u6210\u4e86 C++ \u8bed\u8a00\u7684\u5168\u90e8\uff08\u9884\u5904\u7406\u5668\u6307\u4ee4\u9664\u5916\uff09\u3002\u7531\u4e8e\u5173\u952e\u5b57\u548c\u7279\u6b8a\u7684\u6807\u8bc6\u7b26\u5177\u6709\u7279\u6b8a\u542b\u4e49\uff0c\u56e0\u6b64\u4f60\u7684 IDE \u901a\u5e38\u4f1a\u5c06\u5176\u6807\u8bb0\u4e3a\u4e0d\u540c\u7684\u989c\u8272\u4ee5\u4f7f\u5176\u66f4\u52a0\u9192\u76ee\u3002 \u5728\u5b8c\u6210\u4e86\u672c\u6559\u7a0b\u7684\u5b66\u4e60\u540e\uff0c\u4f60\u4f1a\u7406\u89e3\u4e0a\u8ff0\u5173\u952e\u5b57\u4e2d\u51e0\u4e4e\u5168\u90e8\u7684\u529f\u80fd\uff01","title":"\u5173\u952e\u5b57"},{"location":"1-7-Keywords-and-naming-identifiers/#_2","text":"\u63d0\u9192\u4e00\u4e0b\uff0c\u53d8\u91cf\uff08\u6216\u51fd\u6570\u3001\u7c7b\u578b\u4ee5\u53ca\u5176\u4ed6\u4efb\u4f55\u5143\u7d20\uff09\u7684\u540d\u79f0\u90fd\u79f0\u4e3a\u6807\u8bc6\u7b26\u3002C++ \u5141\u8bb8\u4f60\u7075\u6d3b\u5730\u9009\u62e9\u6807\u8bc6\u7b26\u3002\u4e0d\u8fc7\uff0c\u5728\u9009\u62e9\u547d\u540d\u6807\u8bc6\u7b26\u65f6\uff0c\u4f60\u9700\u8981\u9075\u5faa\u4ee5\u4e0b\u89c4\u5219\uff1a \u6807\u8bc6\u7b26\u4e0d\u80fd\u662f\u5173\u952e\u5b57\uff0c\u5173\u952e\u5b57\u662f\u8bed\u8a00\u7684\u4fdd\u7559\u5b57\uff1b \u6807\u8bc6\u7b26\u53ea\u80fd\u4f7f\u7528\u5927\u5c0f\u5199\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6807\u8bc6\u7b26\u4e2d\u4e0d\u80fd\u5305\u542b\u7b26\u53f7\uff08\u9664\u4e86\u4e0b\u5212\u7ebf\uff09\u6216\u8005\u7a7a\u767d\u7b26\uff08\u7a7a\u683c\u6216\u5236\u8868\u7b26\uff09\uff1b \u6807\u8bc6\u7b26\u5fc5\u987b\u4ee5\u5b57\u6bcd\uff08\u5927\u5c0f\u5199\u5747\u53ef\uff09\u6216\u4e0b\u5212\u7ebf\u5f00\u5934\uff0c\u4e0d\u80fd\u591f\u4ee5\u6570\u5b57\u5f00\u5934\uff1b C++ \u662f\u5927\u5c0f\u5199\u654f\u611f\u7684\uff0c\u56e0\u6b64\u5b83\u4f1a\u533a\u5206\u5927\u5c0f\u5199\u5b57\u6bcd\u3002 nvalue \u548c nValue \u662f\u4e0d\u540c\u7684\uff0c\u548c NVALUE \u4e5f\u4e0d\u540c\uff0c","title":"\u547d\u540d\u89c4\u5219"},{"location":"1-7-Keywords-and-naming-identifiers/#_3","text":"\u73b0\u5728\uff0c\u60f3\u5fc5\u4f60\u5df2\u7ecf\u77e5\u9053 \u5982\u4f55 \u4e3a\u53d8\u91cf\u547d\u540d\u4e86\uff0c\u63a5\u4e0b\u6765\u8ba9\u6211\u4eec\u804a\u804a\uff0c\u4f60 \u5e94\u8be5\u5982\u4f55 \u4e3a\u53d8\u91cf\uff08\u6216\u51fd\u6570\uff09\u547d\u540d\u3002 \u9996\u5148\uff0cC++ \u4e2d\u7ea6\u5b9a\u4fd7\u6210\u7684\u662f\uff0c\u53d8\u91cf\u540d\u5e94\u8be5\u4ee5\u5c0f\u5199\u5b57\u6bcd\u5f00\u5934\u3002\u5982\u679c\u53d8\u91cf\u540d\u662f\u4e00\u4e2a\u5355\u8bcd\uff0c\u5219\u6574\u4e2a\u5355\u8bcd\u90fd\u5e94\u8be5\u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd\u3002 1 2 3 4 5 int value ; // correct int Value ; // incorrect (should start with lower case letter) int VALUE ; // incorrect (should start with lower case letter) int VaLuE ; // incorrect (see your psychiatrist) ;) \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u51fd\u6570\u540d\u540c\u6837\u4e5f\u662f\u5c0f\u5199\u5b57\u6bcd\u5f00\u5934\u7684\uff08\u5c3d\u7ba1\u5728\u8fd9\u4e00\u70b9\u4e0a\u6709\u5f88\u591a\u4e0d\u540c\u610f\u89c1\uff09\u3002\u6211\u4eec\u4f1a\u9075\u5faa\u8fd9\u4e00\u60ef\u4f8b\uff0c\u56e0\u4e3a main \u51fd\u6570\u5c31\u662f\u5c0f\u5199\u5b57\u6bcd\u5f00\u5934\u7684\uff08\u800c\u4e14 main \u51fd\u6570\u662f\u4efb\u4f55\u7a0b\u5e8f\u90fd\u9700\u8981\u6709\u7684\uff09\u3002\u6b64\u5916\uff0cC++\u6807\u51c6\u5e93\u4e2d\u7684\u6240\u6709\u51fd\u6570\u4e5f\u90fd\u9075\u5faa\u8fd9\u4e00\u539f\u5219\u3002 \u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684\u547d\u540d\u6807\u8bc6\u7b26\u901a\u5e38\u7528\u4e8e\u547d\u540d\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u578b\uff08\u4f8b\u5982\u7ed3\u6784\u4f53\u3001\u7c7b\u548c\u679a\u4e3e\uff0c\u7a0d\u540e\u6211\u4eec\u4f1a\u4ecb\u7ecd\u8fd9\u4e9b\u7c7b\u578b\uff09\u3002 \u5982\u679c\u53d8\u91cf\u540d\u6216\u51fd\u6570\u540d\u7531\u591a\u4e2a\u5355\u8bcd\u7ec4\u6210\uff0c\u5e38\u89c1\u7684\u547d\u540d\u60ef\u4f8b\u6709\u4e24\u79cd\uff0c\u4e00\u79cd\u662f\u4f7f\u7528\u4e0b\u5212\u7ebf\u5206\u5272\u5355\u8bcd\uff0c\u79f0\u4e3a \u86c7\u5f62\u547d\u540d\u6cd5(snake case) \uff0c\u53e6\u5916\u4e00\u79cd\u662f\u4f7f\u7528\u5927\u5c0f\u5199\u95f4\u9694\u7684\u65b9\u6cd5\uff08\u6709\u65f6\u5019\u79f0\u4e3a\u9a7c\u5cf0\u547d\u540d\u6cd5\uff0c\u56e0\u4e3a\u5927\u5199\u5b57\u6bcd\u6bd4\u8f83\u7a81\u51fa\uff0c\u5c31\u50cf\u9a86\u9a7c\u7684\u9a7c\u5cf0\u4e00\u6837\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 int my_variable_name ; // correct (separated by underscores/snake_case) int my_function_name (); // correct (separated by underscores/snake_case) int myVariableName ; // correct (intercapped/CamelCase) int myFunctionName (); // correct (intercapped/CamelCase) int my variable name ; // invalid (whitespace not allowed) int my function name (); // invalid (whitespace not allowed) int MyVariableName ; // valid but incorrect (should start with lower case letter) int MyFunctionName (); // valid but incorrect (should start with lower case letter) \u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528\u9a7c\u5cf0\u547d\u540d\u6cd5\uff0c\u56e0\u4e3a\u5b83\u53ef\u8bfb\u6027\u66f4\u597d\uff08\u5bf9\u4e8e\u86c7\u5f62\u547d\u540d\u6cd5\u6765\u8bf4\uff0c\u6709\u65f6\u4e0b\u5212\u7ebf\u5bb9\u6613\u548c\u7a7a\u683c\u6df7\u6dc6\uff09\u3002\u4f46\u662f\uff0c\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u5f88\u5e38\u89c1\uff0cC++ \u7684\u6807\u51c6\u5e93\u5728\u547d\u540d\u53d8\u91cf\u548c\u51fd\u6570\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u7684\u5c31\u662f\u57fa\u4e8e\u4e0b\u5212\u7ebf\u7684\u86c7\u5f62\u547d\u540d\u6cd5\u3002\u6709\u65f6\u5019\uff0c\u4f60\u751a\u81f3\u4f1a\u9047\u5230\u6df7\u7528\u4e24\u79cd\u547d\u540d\u6cd5\u7684\u60c5\u51b5\uff0c\u86c7\u5f62\u547d\u540d\u6cd5\u7528\u4e8e\u53d8\u91cf\u540d\u800c\u9a7c\u5cf0\u547d\u540d\u6cd5\u5219\u7528\u4e8e\u51fd\u6570\u540d\u3002 \u5982\u679c\u4f60\u7684\u5de5\u4f5c\u662f\u5728\u5176\u4ed6\u4eba\u7684\u4ee3\u7801\u4e0a\u8fdb\u884c\u5f00\u53d1\uff0c\u90a3\u4e48\u8fd9\u4e9b\u5c31\u4e0d\u91cd\u8981\u4e86\u3002\u56e0\u4e3a\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6700\u597d\u7684\u505a\u6cd5\u662f\u9075\u5faa\u4ed6\u4eba\u7684\u547d\u540d\u89c4\u5219\u800c\u4e0d\u662f\u6b7b\u677f\u7684\u9075\u5faa\u4e0a\u8ff0\u539f\u5219\u3002 \u6cd5\u5219 \u5f53\u57fa\u4e8e\u5df2\u6709\u7684\u4ee3\u7801\u8fdb\u884c\u5f00\u53d1\u65f6\uff0c\u8bf7\u4f7f\u7528\u8be5\u4ee3\u7801\u7684\u547d\u540d\u7ea6\u5b9a\uff08\u65e2\u662f\u5b83\u7684\u98ce\u683c\u4e0d\u7b26\u5408\u6700\u4f73\u5b9e\u8df5\uff09\u3002\u5f53\u4f60\u7f16\u5199\u4e00\u4e2a\u65b0\u7684\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u8bf7\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\u3002 \u5176\u6b21\uff0c\u547d\u540d\u65f6\u5e94\u5f53\u907f\u514d\u4ee5\u4e0b\u5212\u7ebf\u5f00\u5934\uff0c\u4e0b\u5212\u7ebf\u5f00\u5934\u7684\u540d\u79f0\u5e38\u5e38\u88ab\u64cd\u4f5c\u7cfb\u7edf\u3001\u5e93\u6216\u7f16\u8bd1\u5668\u9884\u7559\u4f7f\u7528\u3002 \u7b2c\u4e09\uff0c\u547d\u540d\u5e94\u5f53\u80fd\u591f\u8868\u660e\u8be5\u53d8\u91cf\u5b58\u50a8\u6570\u636e\u7684\u542b\u4e49\uff08\u5c24\u5176\u662f\u5f53\u8ba1\u91cf\u5355\u4f4d\u4e0d\u660e\u663e\u7684\u65f6\u5019\uff09\u3002\u53d8\u91cf\u540d\u5e94\u8be5\u80fd\u591f\u5e2e\u52a9\u5176\u4ed6\u5bf9\u4f60\u4ee3\u7801\u4e0d\u751a\u4e86\u89e3\u7684\u5f00\u53d1\u8005\u5feb\u901f\u7406\u89e3\u5176\u7528\u9014\u3002\u751a\u81f3\uff0c\u5f53\u4f60\u5728\u4e09\u4e2a\u6708\u4e4b\u540e\u518d\u770b\u81ea\u5df1\u7684\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u4f60\u90fd\u6709\u53ef\u80fd\u770b\u4e0d\u61c2\u81ea\u5df1\u7684\u4ee3\u7801\uff0c\u90a3\u65f6\u5019\u4f60\u5c31\u4f1a\u5e86\u5e78\u81ea\u5df1\u8d77\u4e86\u6613\u4e8e\u7406\u89e3\u3001\u542b\u4e49\u6e05\u6670\u7684\u53d8\u91cf\u540d\u3002 \u4e0d\u8fc7\uff0c\u4e3a\u4e00\u4e9b\u65e0\u5173\u7d27\u8981\u7684\u53d8\u91cf\u53d6\u4e00\u4e2a\u8fc7\u5206\u590d\u6742\u7684\u540d\u79f0\u53cd\u5012\u6709\u788d\u4e8e\u7406\u89e3\uff0c\u5176\u5371\u5bb3\u4e0d\u4e9a\u4e8e\u7ed9\u91cd\u8981\u7684\u53d8\u91cf\u53d6\u4e00\u4e2a\u610f\u4e49\u6a21\u7cca\u7684\u540d\u5b57\u3002\u56e0\u6b64\uff0c\u547d\u540d\u7684\u4e00\u4e2a\u539f\u5219\u53ef\u4ee5\u662f\uff0c\u8ba9\u53d8\u91cf\u540d\u7684\u957f\u5ea6\u7b26\u5408\u5176\u88ab\u4f7f\u7528\u7684\u9891\u5ea6\u3002\u65e0\u5173\u7d27\u8981\u7684\u53d8\u91cf\u540d\u5e94\u8be5\u4f7f\u7528\u66f4\u77ed\u7684\u53d8\u91cf\u540d\uff08\u4f8b\u5982 i \uff09\u3002\u88ab\u5e7f\u6cdb\u4f7f\u7528\u7684\u53d8\u91cf\uff08\u4f8b\u5982\u67d0\u4e2a\u5728\u591a\u5904\u88ab\u8c03\u7528\u7684\u51fd\u6570\uff09\u5219\u503c\u5f97\u62e5\u6709\u4e00\u4e2a\u66f4\u957f\u3001\u66f4\u5177\u89e3\u91ca\u6027\u7684\u53d8\u91cf\u540d\uff08\u4f8b\u5982\uff0c openFileOnDisk \u4f1a\u6bd4 open \u66f4\u5408\u9002\uff09\u3002 \u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\uff0c\u90fd\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u7f29\u5199\u3002\u5c3d\u7ba1\u4f7f\u7528\u7f29\u5199\u53ef\u4ee5\u7a0d\u5fae\u63d0\u9ad8\u4f60\u6572\u4ee3\u7801\u7684\u901f\u5ea6\uff0c\u5b83\u4f1a\u5bfc\u81f4\u4f60\u7684\u4ee3\u7801\u96be\u4ee5\u88ab\u7406\u89e3\u3002\u65e2\u662f\u6709\u4e9b\u7f29\u5199\u5177\u6709\u660e\u786e\u7684\u542b\u4e49\uff0c\u4f46\u8bfb\u8005\u5728\u9047\u5230\u5b83\u7684\u65f6\u5019\u4ecd\u7136\u9700\u8981\u82b1\u4e9b\u65f6\u95f4\u6765\u641e\u6e05\u695a\u5b83\u7684\u542b\u4e49\u3002\u4ee3\u7801\u88ab\u9605\u8bfb\u7684\u65f6\u95f4\u8fdc\u957f\u4e8e\u5b83\u88ab\u7f16\u5199\u7684\u65f6\u95f4\uff0c\u4f60\u5728\u5199\u4ee3\u7801\u4e0a\u8282\u7701\u4e0b\u4e86\u7684\u65f6\u95f4\uff0c\u5c06\u6765\u90fd\u4f1a\u6d6a\u8d39\u5728\u5176\u4ed6\u9605\u8bfb\u8005\uff08\u751a\u81f3\u662f\u4f60\u81ea\u5df1\uff09\u7684\u8eab\u4e0a\u3002\u5982\u679c\u4f60\u5e0c\u671b\u52a0\u5feb\u7f16\u5199\u4ee3\u7801\u7684\u901f\u5ea6\uff0c\u8bf7\u4f7f\u7528\u7f16\u8f91\u5668\u7684\u81ea\u52a8\u8865\u5168\u529f\u80fd\u3002 \u6700\u540e\uff0c\u6e05\u6670\u660e\u786e\u7684\u6ce8\u91ca\u4e5f\u5927\u6709\u52a9\u76ca\u3002\u4f8b\u5982\uff0c\u53d8\u91cf numberOfChars \u7528\u4e8e\u5b58\u653e\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u7684\u4e2a\u6570\u3002\u4f46\u662f\uff0c\u5b57\u7b26\u4e32\u201cHello World!\u201d\u4e2d\u7a76\u7adf\u5305\u542b\u51e0\u4e2a\u5b57\u7b26\u5462\uff1f10\u4e2a\uff0c11\u4e2a\u8fd8\u662f12\u4e2a\uff1f\u8fd9\u53d6\u51b3\u4e8e\u4f60\u662f\u5426\u8003\u8651\u7a7a\u683c\u548c\u6807\u70b9\u7b26\u53f7\u3002\u4f46\u662f\uff0c\u5c06\u8be5\u53d8\u91cf\u547d\u540d\u4e3a numberOfCharsIncludingWhitespaceAndPunctuation \u5c31\u592a\u957f\u4e86\uff0c\u6700\u597d\u7684\u65b9\u5f0f\u662f\u5728\u5b9a\u4e49\u53d8\u91cf\u65f6\u4f7f\u7528\u6ce8\u91ca\u7684\u65b9\u5f0f\u5e2e\u52a9\u8bfb\u8005\u53bb\u7406\u89e3\u3002 1 2 // holds number of chars in a piece of text -- including whitespace and punctuation! int numberOfChars ;","title":"\u547d\u540d\u6700\u4f73\u5b9e\u8df5"},{"location":"1-8-Whitespace-and-basic-formatting/","tags":["formatting"],"text":"\u7a7a\u767d \u8fd9\u4e2a\u672f\u8bed\uff0c\u6307\u7684\u662f\u90a3\u4e9b\u51fa\u4e8e\u6392\u7248\u76ee\u7684\u800c\u4f7f\u7528\u7684\u7a7a\u767d\u5b57\u7b26\u3002\u5728 C++ \u4e2d\uff0c\u7a7a\u767d\u6307\u7684\u662f\u7a7a\u683c\u7b26\u3001\u5236\u8868\u7b26\u548c\u6362\u884c\u7b26\u3002C++ \u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u5ffd\u7565\u7a7a\u767d\uff08\u5904\u7406\u6587\u672c\u5b57\u9762\u91cf\u65f6\u662f\u4e00\u4e2a\u7279\u4f8b\uff09\u3002\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u8bf4 C++ \u662f\u4e00\u4e2a\u7a7a\u683c\u65e0\u5173\uff08whitespace-independent\uff09\u7684\u8bed\u8a00\u3002 \u56e0\u6b64\uff0c\u4e0b\u9762\u51e0\u884c\u4ee3\u7801\u7684\u6548\u679c\u5176\u5b9e\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 std :: cout << \"Hello world!\" ; std :: cout << \"Hello world!\" ; std :: cout << \"Hello world!\" ; std :: cout << \"Hello world!\" ; \u6700\u540e\u4e00\u4e2a\u8bed\u53e5\u5373\u4f7f\u88ab\u5206\u5272\u4e3a\u4e24\u884c\uff0c\u5b83\u4e5f\u80fd\u591f\u6b63\u5e38\u7f16\u8bd1\u3002 \u4e0b\u9762\u7684\u51fd\u6570\u6548\u679c\u4e5f\u5b8c\u5168\u4e00\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int add ( int x , int y ) { return x + y ; } int add ( int x , int y ) { return x + y ; } int add ( int x , int y ) { return x + y ; } int add ( int x , int y ) { return x + y ; } \u4e00\u4e2a\u4f8b\u5916\u662f\uff0cC++\u7f16\u8bd1\u5668\u5e76\u4e0d\u4f1a\u5ffd\u7565\u5f15\u53f7\u5305\u88f9\u7684\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u767d\uff0c\u4f8b\u5982 \"Hello world!\" \u3002 1 \"Hello world!\" \u4e0a\u9762\u7684\u5b57\u7b26\u4e32\u548c\u4e0b\u9762\u7684\u5b57\u7b26\u4e32\u662f\u4e0d\u4e00\u6837\u7684\u3002 1 \"Hello world!\" \u4e24\u4e2a\u5b57\u7b26\u4e32\u90fd\u4f1a\u88ab\u539f\u5c01\u4e0d\u52a8\u7684\u6253\u5370\u5904\u7406\u3002 \u5728\u5f15\u53f7\u5305\u88f9\u7684\u5b57\u7b26\u4e32\u4e2d\uff0c\u6362\u884c\u662f\u4e0d\u5408\u6cd5\u7684\uff1a 1 2 std :: cout << \"Hello world ! \"; // Not allowed! \u53ea\u4f7f\u7528\u7a7a\u767d\u5b57\u7b26\uff08\u7a7a\u683c\u3001\u6362\u884c\u548c\u56de\u8f66\uff09\u5206\u5272\u7684\u6587\u672c\uff0c\u6700\u7ec8\u5c06\u4f1a\u88ab\u8fde\u63a5\u5728\u4e00\u8d77\uff1a 1 2 std :: cout << \"Hello \" \"world!\" ; // prints \"Hello world!\" \u53e6\u5916\u4e00\u4e2a\u7279\u4f8b\u662f\uff0cC++ \u7684\u7f16\u8bd1\u5668\u4f1a\u5173\u6ce8\u5355\u884c\u6ce8\u91ca\uff08 // \uff09\u4e2d\u7684\u7a7a\u767d\u7b26\u3002\u5355\u884c\u6ce8\u91ca\u53ea\u4f1a\u5230\u8be5\u884c\u672b\u5c3e\u622a\u6b62\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u6709\u95ee\u9898\u7684\uff1a 1 2 std :: cout << \"Hello world!\" ; // Here is a single-line comment this is not part of the comment \u57fa\u672c\u4ee3\u7801\u683c\u5f0f \u548c\u5176\u4ed6\u8bed\u8a00\u4e0d\u540c\uff0cC++ \u5e76\u4e0d\u4f1a\u5f3a\u5236\u7a0b\u5e8f\u5458\u4f7f\u7528\u67d0\u79cd\u6392\u7248\u98ce\u683c\uff08\u8fd8\u8bb0\u5f97\u5417\uff1f\u76f8\u4fe1\u7a0b\u5e8f\u5458\uff01\uff09\u3002C++ \u4e2d\u6709\u5f88\u591a\u53ef\u4ee5\u4f7f\u7528\u7684\u6392\u7248\u65b9\u5f0f\uff0c\u5173\u4e8e\u54ea\u4e00\u79cd\u624d\u662f\u6700\u4f73\u7684\u65b9\u5f0f\uff0c\u4f60\u53ef\u80fd\u4f1a\u6709\u4e0d\u540c\u7684\u770b\u6cd5\u3002\u6211\u4eec\u7684\u57fa\u672c\u539f\u5219\u662f\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\u6700\u5f3a\u3001\u4e00\u6267\u884c\u6700\u597d\u7684\u90a3\u79cd\u6392\u7248\u98ce\u683c\uff0c\u5c31\u662f\u6700\u597d\u7684\u3002 \u672c\u6559\u7a0b\u5efa\u8bae\u7684\u6392\u7248\u89c4\u8303\u5982\u4e0b\uff1a 1.\u4f7f\u7528\u5236\u8868\u7b26\u548c\u7a7a\u683c\u8fdb\u884c\u7f29\u8fdb\u90fd\u662f\u5141\u8bb8\u7684 (\u5927\u591a\u6570\u7684 IDE \u90fd\u53ef\u4ee5\u8bbe\u7f6e\u81ea\u52a8\u5c06\u5236\u8868\u7b26\u8f6c\u6362\u4e3a\u89c4\u5b9a\u4e2a\u6570\u7684\u7a7a\u683c\uff09\u3002\u5f00\u53d1\u8005\u4f7f\u7528\u7a7a\u683c\u8fdb\u884c\u6392\u7248\u5c24\u5176\u5185\u5728\u7684\u903b\u8f91\uff0c\u6bd5\u7adf\u4f7f\u7528\u201d\u7a7a\u683c\u201c\u6765\u521b\u5efa\u4ee3\u7801\u4e4b\u95f4\u7684\u201d\u7a7a\u767d\u201c\u662f\u5929\u7ecf\u5730\u4e49\u7684\u3002\u4f7f\u7528\u7a7a\u683c\u6dfb\u52a0\u7684\u7a7a\u767d\uff0c\u4e0d\u8bba\u5728\u4ec0\u4e48\u7f16\u8f91\u5668\u4e0b\uff0c\u770b\u8d77\u6765\u90fd\u662f\u4e00\u81f4\u7684\u3002\u800c\u4f7f\u7528\u5236\u8868\u7b26\u8fdb\u884c\u7f29\u8fdb\u7684\u7a0b\u5e8f\u5458\u4f1a\u5bf9\u4e0a\u8ff0\u884c\u4e3a\u611f\u5230\u56f0\u60d1\uff0c\u5236\u8868\u7b26\u5c31\u662f\u4e3a\u4e86\u7f29\u8fdb\u800c\u5b58\u5728\u7684\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u7528\u5462\uff1f\u66f4\u4f55\u51b5\u4f60\u8fd8\u53ef\u4ee5\u81ea\u7531\u63a7\u5236\u5176\u7f29\u8fdb\u7684\u5bbd\u5ea6\u3002\u8fd9\u4e2a\u95ee\u9898\u6ca1\u6709\u6240\u8c13\u201d\u6b63\u786e\u201c\u7684\u7b54\u6848\uff0c\u4e89\u8bba\u5b83\u7684\u5bf9\u9519\u662f\u6ca1\u6709\u610f\u4e49\u7684\uff0c\u5c31\u597d\u6bd4\u4e89\u8bba\u7cbd\u5b50\u5e94\u8be5\u662f\u751c\u7684\u8fd8\u662f\u54b8\u7684\uff0c\u56e0\u4e3a\u8fd9\u5b8c\u5168\u53d6\u51b3\u4e8e\u4f60\u81ea\u5df1\u7684\u53e3\u5473\u3002 \u4e0d\u8bba\u90a3\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u5efa\u8bae\u4f60\u4f7f\u75284\u4e2a\u7a7a\u683c\u7684\u5bbd\u5ea6\u4f5c\u4e3a\u7f29\u8fdb\u7684\u5bbd\u5ea6\u3002\u6709\u4e9bIDE\u9ed8\u8ba4\u7684\u7f29\u8fdb\u5bbd\u5ea6\u662f3\u4e2a\u7a7a\u683c\uff0c\u5176\u5b9e\u4e5f\u662f\u53ef\u4ee5\u7684\u3002 2.\u51fd\u6570\u62ec\u53f7\u7684\u6392\u7248\u6709\u4e24\u79cd\u53ef\u4ee5\u63a5\u53d7\u7684\u65b9\u5f0f\u3002 Google C++ \u98ce\u683c\u6307\u5357\u5efa\u8bae\u7684\u98ce\u683c\u662f\u82b1\u62ec\u53f7\u4e0d\u6362\u884c\uff1a 1 2 int main () { } \u8fd9\u4e48\u505a\u7684\u4f9d\u636e\u662f\uff0c\u5b83\u80fd\u591f\u51cf\u5c11\u7eb5\u5411\u7a7a\u767d\u7684\u4ea7\u751f\uff08\u62ec\u53f7\u5355\u72ec\u5360\u7528\u4e00\u884c\u662f\u4e0d\u5212\u7b97\u7684\uff09\uff0c\u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u5728\u6709\u9650\u7684\u5c4f\u5e55\u4e2d\u663e\u793a\u66f4\u591a\u7684\u4ee3\u7801\uff0c\u8fd9\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u7684\u7406\u89e3\u4ee3\u7801\u3002 \u4e0d\u8fc7\uff0c\u6211\u4eec\u8fd8\u662f\u503e\u5411\u4e8e\u4f7f\u7528\u53e6\u5916\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u5373\u82b1\u62ec\u53f7\u53e6\u8d77\u4e00\u884c\uff1a 1 2 3 int main () { } \u8fd9\u4e48\u505a\u53ef\u4ee5\u589e\u5f3a\u53ef\u8bfb\u6027\uff0c\u56e0\u4e3a\u4e00\u5bf9\u82b1\u62ec\u53f7\u603b\u662f\u5177\u6709\u76f8\u540c\u7684\u7f29\u8fdb\uff0c\u8fd9\u4e48\u505a\u53ef\u4ee5\u51cf\u5c11\u72af\u9519\u7684\u51e0\u7387\u3002\u5982\u679c\u7f16\u8bd1\u5668\u62a5\u544a\u4e86\u62ec\u53f7\u4e0d\u5339\u914d\u7684\u9519\u8bef\uff0c\u90a3\u4f60\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u627e\u5230\u95ee\u9898\u6240\u5728\u3002 3.\u6bcf\u4e00\u5c42\u82b1\u62ec\u53f7\u4e2d\u7684\u8bed\u53e5\u90fd\u5fc5\u987b\u8fdb\u884c\u7f29\u8fdb\u4e00\u7ea7\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 int main () { std :: cout << \"Hello world! \\n \" ; // tabbed in one tab (4 spaces) std :: cout << \"Nice to meet you. \\n \" ; // tabbed in one tab (4 spaces) } 4.\u6bcf\u4e00\u884c\u4ee3\u7801\u90fd\u4e0d\u5e94\u8be5\u592a\u957f\u3002\u5e38\u89c1\u7684\u89c4\u5219\u662f\uff1a\u5355\u884c\u4e0d\u80fd\u8d85\u8fc780\u4e2a\u5b57\u7b26\u3002\u5982\u679c\u4e00\u884c\u4ee3\u7801\u592a\u957f\uff0c\u5b83\u5c31\u5e94\u5f53\u88ab\u5206\u5272\u4e3a\u591a\u884c\uff08\u5728\u5408\u9002\u7684\u4f4d\u7f6e\u5206\u5272\uff09\u3002\u5177\u4f53\u64cd\u4f5c\u65f6\u53ef\u4ee5\u5c06\u5404\u4e2a\u5b50\u884c\u989d\u5916\u7f29\u8fdb\u4e00\u7ea7\uff0c\u6216\u8005\uff0c\u5982\u679c\u5b50\u884c\u4e4b\u95f4\u975e\u5e38\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u5c06\u5b83\u4eec\u90fd\u5bf9\u9f50\u5230\u4e0a\u9762\u7684\u884c\uff08\u53ef\u8bfb\u6027\u66f4\u597d\uff09\u3002 1 2 3 4 5 6 7 8 9 10 int main () { std :: cout << \"This is a really, really, really, really, really, really, really, \" \"really long line \\n \" ; // one extra indentation for continuation line std :: cout << \"This is another really, really, really, really, really, really, really, \" \"really long line \\n \" ; // text aligned with the previous line for continuation line std :: cout << \"This one is short \\n \" ; } \u8fd9\u4e48\u505a\u53ef\u4ee5\u4f7f\u5f97\u6bcf\u884c\u4ee3\u7801\u90fd\u5177\u6709\u5f88\u597d\u7684\u53ef\u8bfb\u6027\u3002\u5728\u5bbd\u5c4f\u663e\u793a\u5668\u4e0a\uff0c\u8fd9\u4e48\u505a\u8fd8\u53ef\u4ee5\u5141\u8bb8\u4f60\u5c06\u4e24\u4e2a\u4ee3\u7801\u7a97\u53e3\u5e76\u6392\u663e\u793a\uff0c\u8f7b\u677e\u5730\u5bf9\u6bd4\u5b83\u4eec\u7684\u5dee\u5f02\u3002 \u6700\u4f73\u5b9e\u8df5 \u5355\u884c\u4ee3\u7801\u957f\u5ea6\u4e0d\u5e94\u8be5\u8d85\u8fc780\u4e2a\u5b57\u7b26\u3002 \u5c0f\u8d34\u58eb \u5f88\u591a\u7f16\u8f91\u5668\u90fd\u96c6\u6210\uff08\u6216\u901a\u8fc7\u63d2\u4ef6/\u6269\u5c55\uff09\u4e86 \u5217\u6307\u793a\u5668 \uff0c\u80fd\u591f\u663e\u793a\u51fa\u7279\u5b9a\u5217\u7684\u4f4d\u7f6e\uff08\u4f8b\u598280\u4e2a\u5b57\u7b26\u5904\uff09\u3002\u56e0\u6b64\u4f60\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5224\u65ad\u4f60\u7684\u4ee3\u7801\u884c\u662f\u4e0d\u662f\u8d85\u957f\u4e86\u3002\u641c\u7d22\u7f16\u8f91\u5668\u540d+\u201cColumn guide\u201d\u6765\u67e5\u8be2\u4f60\u7684\u7f16\u8f91\u5668\u662f\u5426\u652f\u6301\u8be5\u7279\u6027\u3002 \u8bd1\u6ce8\uff1a\u5728 VS Code \u4e2d\uff0c\u8fd9\u4e00\u7279\u6027\u53eb\u505a editor.rulers \uff0c\u53ef\u4ee5\u5728\u8bbe\u7f6e\u4e2d\u914d\u7f6e\u5176\u51fa\u73b0\u7684\u4f4d\u7f6e\uff0c\u4f8b\u5982 editor.rulers=[80] 5.\u5982\u679c\u4e00\u4e2a\u8f83\u957f\u7684\u884c\u88ab\u57fa\u4e8e\u8fd0\u7b97\u7b26\u5206\u5272\u4e3a\u4e86\u591a\u4e2a\u77ed\u884c\uff0c\u90a3\u4e48\u8fd0\u7b97\u7b26\u5e94\u8be5\u653e\u7f6e\u5728\u4e0b\u4e00\u884c\u7684\u5f00\u5934\u800c\u4e0d\u662f\u4e0a\u4e00\u884c\u7684\u7ed3\u5c3e\uff1a 1 2 3 std :: cout << 3 + 4 + 5 + 6 * 7 * 8 ; \u8fd9\u4e48\u505a\u53ef\u4ee5\u660e\u786e\u7684\u8868\u660e\u4e0b\u4e00\u884c\u662f\u627f\u63a5\u81ea\u4e0a\u4e00\u884c\u7684\uff0c\u800c\u4e14\u4f60\u53ef\u4ee5\u5c06\u8fd0\u7b97\u7b26\u5de6\u5bf9\u9f50\uff0c\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002 6.\u4f7f\u7528\u7a7a\u683c\u5c06\u503c\u3001\u6ce8\u91ca\u5bf9\u9f50\uff0c\u6216\u5728\u4ee3\u7801\u6bb5\u4e4b\u95f4\u6dfb\u52a0\u7a7a\u683c\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002 \u53ef\u8bfb\u6027\u4e0d\u4f73\uff1a 1 2 3 4 cost = 57 ; pricePerItem = 24 ; value = 5 ; numberOfItems = 17 ; \u53ef\u8bfb\u6027\u4f73\uff1a 1 2 3 4 cost = 57 ; pricePerItem = 24 ; value = 5 ; numberOfItems = 17 ; \u53ef\u8bfb\u6027\u4e0d\u4f73\uff1a 1 2 3 std :: cout << \"Hello world! \\n \" ; // cout lives in the iostream library std :: cout << \"It is very nice to meet you! \\n \" ; // these comments make the code hard to read std :: cout << \"Yeah! \\n \" ; // especially when lines are different lengths \u53ef\u8bfb\u6027\u4f73\uff1a 1 2 3 std :: cout << \"Hello world! \\n \" ; // cout lives in the iostream library std :: cout << \"It is very nice to meet you! \\n \" ; // these comments are easier to read std :: cout << \"Yeah! \\n \" ; // especially when all lined up \u53ef\u8bfb\u6027\u4e0d\u4f73\uff1a 1 2 3 4 5 6 // cout lives in the iostream library std :: cout << \"Hello world! \\n \" ; // these comments make the code hard to read std :: cout << \"It is very nice to meet you! \\n \" ; // especially when all bunched together std :: cout << \"Yeah! \\n \" ; \u53ef\u8bfb\u6027\u4f73\uff1a 1 2 3 4 5 6 7 8 // cout lives in the iostream library std :: cout << \"Hello world! \\n \" ; // these comments are easier to read std :: cout << \"It is very nice to meet you! \\n \" ; // when separated by whitespace std :: cout << \"Yeah! \\n \" ; \u672c\u6559\u7a0b\u5c06\u59cb\u7ec8\u8d2f\u5f7b\u4e0a\u8ff0\u6392\u7248\u7ea6\u5b9a\uff0c\u5b83\u4eec\u7ec8\u5c06\u4f1a\u79f0\u4e3a\u4f60\u4e60\u60ef\u7684\u4e00\u90e8\u5206\u3002\u5728\u6211\u4eec\u4ecb\u7ecd\u4fe1\u7684\u8bed\u8a00\u7279\u6027\u7684\u65f6\u5019\uff0c\u4e5f\u4f1a\u5bf9\u5e94\u5730\u4ecb\u7ecd\u5176\u9002\u7528\u7684\u4ee3\u7801\u6392\u7248\u98ce\u683c\u3002 C++ \u7ed9\u4e0e\u4e86\u4f60\u9009\u62e9\u4efb\u4f55\u4f60\u559c\u6b22\u7684\u98ce\u683c\u7684\u6743\u5229\uff0c\u4f46\u662f\uff0c\u6211\u4eec\u4ecd\u7136\u5f3a\u70c8\u63a8\u8350\u4f60\u4f7f\u7528\u4e0e\u6211\u4eec\u76f8\u540c\u7684\u4ee3\u7801\u98ce\u683c\uff0c\u8fd9\u4e9b\u89c4\u5219\u90fd\u662f\u5343\u9524\u767e\u70bc\uff0c\u88ab\u65e0\u6570\u4eba\u6d4b\u8bd5\u540e\u5f97\u5230\u7684\u7ecf\u9a8c\uff0c\u5b83\u4eec\u80fd\u591f\u5728\u7f16\u7a0b\u8def\u4e0a\u52a9\u4f60\u4e00\u81c2\u4e4b\u529b\u3002\u5f53\u7136\uff0c\u5982\u679c\u4f60\u5728\u57fa\u4e8e\u4ed6\u4eba\u7684\u4ee3\u7801\u8fdb\u884c\u5de5\u4f5c\uff0c\u8bf7\u9002\u5e94\u5e76\u4f7f\u7528\u4ed6\u4eec\u7684\u4ee3\u7801\u98ce\u683c\uff0c\u98ce\u683c\u4e00\u81f4\u6027\u7684\u4f18\u5148\u7ea7\u8981\u9ad8\u4e8e\u5176\u4ed6\u3002 \u81ea\u52a8\u6392\u7248 \u5927\u591a\u6570\u7684\u73b0\u4ee3 IDE \u90fd\u53ef\u4ee5\u5e2e\u52a9\u4f60\u6392\u7248\u4f60\u7684\u4ee3\u7801\uff08\u4f8b\u5982\uff0c\u5728\u4f60\u521b\u5efa\u51fd\u6570\u7684\u65f6\u5019\uff0cIDE\u5c31\u4f1a\u81ea\u52a8\u5e2e\u4f60\u7f29\u8fdb\u51fd\u6570\u4f53\u5185\u7684\u8bed\u53e5\uff09\u3002 \u7136\u800c\uff0c\u5f53\u4f60\u6dfb\u52a0\u3001\u5220\u9664\u4ee3\u7801\uff0c\u6216\u8005\u4fee\u6539\u4e86IDE\u7684\u9ed8\u8ba4\u6392\u7248\u89c4\u5219\u540e\uff0c\u53c8\u6216\u8005\u4f60\u4ece\u5176\u4ed6\u5730\u65b9\u62f7\u8d1d\u4e86\u4ee3\u7801\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6392\u7248\u53ef\u80fd\u4f1a\u53d8\u5f97\u4e00\u56e2\u7cdf\u3002\u4fee\u590d\u90e8\u5206\u6216\u6574\u4e2a\u6587\u4ef6\u4e2d\u7684\u6392\u7248\u95ee\u9898\u662f\u975e\u5e38\u5934\u75bc\u7684\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u73b0\u4ee3DIE\u901a\u5e38\u90fd\u63d0\u4f9b\u4e86\u81ea\u52a8\u6392\u7248\u7684\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u5bf9\u4f60\u9009\u62e9\u7684\u90e8\u5206\u6216\u8005\u6574\u4e2a\u6587\u4ef6\u8fdb\u884c\u91cd\u65b0\u6392\u7248\u3002 For Visual Studio users \u5728 Visual Studio \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 Edit > Advanced > Format Document and Edit > Advanced > Format Selection \u6765\u914d\u7f6e\u81ea\u52a8\u6392\u7248\u3002 For Code::Blocks users \u5728 Code::Blocks \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u9f20\u6807\u53f3\u952e\uff0c\u9009\u62e9 Format use AStyle \u6765\u4f7f\u7528\u3002 \u4e3a\u4e86\u4f7f\u7528\u65b9\u4fbf\uff0c\u63a8\u8350\u4e3a\u81ea\u52a8\u6392\u7248\u529f\u80fd\u7ed1\u5b9a\u4e00\u4e2a\u952e\u76d8\u5feb\u6377\u952e\u3002 \u5c0f\u8d34\u58eb \u4f7f\u7528\u81ea\u52a8\u6392\u7248\u529f\u80fd\uff0c\u662f\u4fdd\u6301\u4ee3\u7801\u98ce\u683c\u4e00\u81f4\u6027\u7684\u597d\u529e\u6cd5\u3002","title":"1.8 - \u7a7a\u767d\u7b26\u548c\u57fa\u672c\u6392\u7248"},{"location":"1-8-Whitespace-and-basic-formatting/#_1","text":"\u548c\u5176\u4ed6\u8bed\u8a00\u4e0d\u540c\uff0cC++ \u5e76\u4e0d\u4f1a\u5f3a\u5236\u7a0b\u5e8f\u5458\u4f7f\u7528\u67d0\u79cd\u6392\u7248\u98ce\u683c\uff08\u8fd8\u8bb0\u5f97\u5417\uff1f\u76f8\u4fe1\u7a0b\u5e8f\u5458\uff01\uff09\u3002C++ \u4e2d\u6709\u5f88\u591a\u53ef\u4ee5\u4f7f\u7528\u7684\u6392\u7248\u65b9\u5f0f\uff0c\u5173\u4e8e\u54ea\u4e00\u79cd\u624d\u662f\u6700\u4f73\u7684\u65b9\u5f0f\uff0c\u4f60\u53ef\u80fd\u4f1a\u6709\u4e0d\u540c\u7684\u770b\u6cd5\u3002\u6211\u4eec\u7684\u57fa\u672c\u539f\u5219\u662f\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\u6700\u5f3a\u3001\u4e00\u6267\u884c\u6700\u597d\u7684\u90a3\u79cd\u6392\u7248\u98ce\u683c\uff0c\u5c31\u662f\u6700\u597d\u7684\u3002 \u672c\u6559\u7a0b\u5efa\u8bae\u7684\u6392\u7248\u89c4\u8303\u5982\u4e0b\uff1a 1.\u4f7f\u7528\u5236\u8868\u7b26\u548c\u7a7a\u683c\u8fdb\u884c\u7f29\u8fdb\u90fd\u662f\u5141\u8bb8\u7684 (\u5927\u591a\u6570\u7684 IDE \u90fd\u53ef\u4ee5\u8bbe\u7f6e\u81ea\u52a8\u5c06\u5236\u8868\u7b26\u8f6c\u6362\u4e3a\u89c4\u5b9a\u4e2a\u6570\u7684\u7a7a\u683c\uff09\u3002\u5f00\u53d1\u8005\u4f7f\u7528\u7a7a\u683c\u8fdb\u884c\u6392\u7248\u5c24\u5176\u5185\u5728\u7684\u903b\u8f91\uff0c\u6bd5\u7adf\u4f7f\u7528\u201d\u7a7a\u683c\u201c\u6765\u521b\u5efa\u4ee3\u7801\u4e4b\u95f4\u7684\u201d\u7a7a\u767d\u201c\u662f\u5929\u7ecf\u5730\u4e49\u7684\u3002\u4f7f\u7528\u7a7a\u683c\u6dfb\u52a0\u7684\u7a7a\u767d\uff0c\u4e0d\u8bba\u5728\u4ec0\u4e48\u7f16\u8f91\u5668\u4e0b\uff0c\u770b\u8d77\u6765\u90fd\u662f\u4e00\u81f4\u7684\u3002\u800c\u4f7f\u7528\u5236\u8868\u7b26\u8fdb\u884c\u7f29\u8fdb\u7684\u7a0b\u5e8f\u5458\u4f1a\u5bf9\u4e0a\u8ff0\u884c\u4e3a\u611f\u5230\u56f0\u60d1\uff0c\u5236\u8868\u7b26\u5c31\u662f\u4e3a\u4e86\u7f29\u8fdb\u800c\u5b58\u5728\u7684\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u7528\u5462\uff1f\u66f4\u4f55\u51b5\u4f60\u8fd8\u53ef\u4ee5\u81ea\u7531\u63a7\u5236\u5176\u7f29\u8fdb\u7684\u5bbd\u5ea6\u3002\u8fd9\u4e2a\u95ee\u9898\u6ca1\u6709\u6240\u8c13\u201d\u6b63\u786e\u201c\u7684\u7b54\u6848\uff0c\u4e89\u8bba\u5b83\u7684\u5bf9\u9519\u662f\u6ca1\u6709\u610f\u4e49\u7684\uff0c\u5c31\u597d\u6bd4\u4e89\u8bba\u7cbd\u5b50\u5e94\u8be5\u662f\u751c\u7684\u8fd8\u662f\u54b8\u7684\uff0c\u56e0\u4e3a\u8fd9\u5b8c\u5168\u53d6\u51b3\u4e8e\u4f60\u81ea\u5df1\u7684\u53e3\u5473\u3002 \u4e0d\u8bba\u90a3\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u5efa\u8bae\u4f60\u4f7f\u75284\u4e2a\u7a7a\u683c\u7684\u5bbd\u5ea6\u4f5c\u4e3a\u7f29\u8fdb\u7684\u5bbd\u5ea6\u3002\u6709\u4e9bIDE\u9ed8\u8ba4\u7684\u7f29\u8fdb\u5bbd\u5ea6\u662f3\u4e2a\u7a7a\u683c\uff0c\u5176\u5b9e\u4e5f\u662f\u53ef\u4ee5\u7684\u3002 2.\u51fd\u6570\u62ec\u53f7\u7684\u6392\u7248\u6709\u4e24\u79cd\u53ef\u4ee5\u63a5\u53d7\u7684\u65b9\u5f0f\u3002 Google C++ \u98ce\u683c\u6307\u5357\u5efa\u8bae\u7684\u98ce\u683c\u662f\u82b1\u62ec\u53f7\u4e0d\u6362\u884c\uff1a 1 2 int main () { } \u8fd9\u4e48\u505a\u7684\u4f9d\u636e\u662f\uff0c\u5b83\u80fd\u591f\u51cf\u5c11\u7eb5\u5411\u7a7a\u767d\u7684\u4ea7\u751f\uff08\u62ec\u53f7\u5355\u72ec\u5360\u7528\u4e00\u884c\u662f\u4e0d\u5212\u7b97\u7684\uff09\uff0c\u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u5728\u6709\u9650\u7684\u5c4f\u5e55\u4e2d\u663e\u793a\u66f4\u591a\u7684\u4ee3\u7801\uff0c\u8fd9\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u7684\u7406\u89e3\u4ee3\u7801\u3002 \u4e0d\u8fc7\uff0c\u6211\u4eec\u8fd8\u662f\u503e\u5411\u4e8e\u4f7f\u7528\u53e6\u5916\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u5373\u82b1\u62ec\u53f7\u53e6\u8d77\u4e00\u884c\uff1a 1 2 3 int main () { } \u8fd9\u4e48\u505a\u53ef\u4ee5\u589e\u5f3a\u53ef\u8bfb\u6027\uff0c\u56e0\u4e3a\u4e00\u5bf9\u82b1\u62ec\u53f7\u603b\u662f\u5177\u6709\u76f8\u540c\u7684\u7f29\u8fdb\uff0c\u8fd9\u4e48\u505a\u53ef\u4ee5\u51cf\u5c11\u72af\u9519\u7684\u51e0\u7387\u3002\u5982\u679c\u7f16\u8bd1\u5668\u62a5\u544a\u4e86\u62ec\u53f7\u4e0d\u5339\u914d\u7684\u9519\u8bef\uff0c\u90a3\u4f60\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u627e\u5230\u95ee\u9898\u6240\u5728\u3002 3.\u6bcf\u4e00\u5c42\u82b1\u62ec\u53f7\u4e2d\u7684\u8bed\u53e5\u90fd\u5fc5\u987b\u8fdb\u884c\u7f29\u8fdb\u4e00\u7ea7\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 int main () { std :: cout << \"Hello world! \\n \" ; // tabbed in one tab (4 spaces) std :: cout << \"Nice to meet you. \\n \" ; // tabbed in one tab (4 spaces) } 4.\u6bcf\u4e00\u884c\u4ee3\u7801\u90fd\u4e0d\u5e94\u8be5\u592a\u957f\u3002\u5e38\u89c1\u7684\u89c4\u5219\u662f\uff1a\u5355\u884c\u4e0d\u80fd\u8d85\u8fc780\u4e2a\u5b57\u7b26\u3002\u5982\u679c\u4e00\u884c\u4ee3\u7801\u592a\u957f\uff0c\u5b83\u5c31\u5e94\u5f53\u88ab\u5206\u5272\u4e3a\u591a\u884c\uff08\u5728\u5408\u9002\u7684\u4f4d\u7f6e\u5206\u5272\uff09\u3002\u5177\u4f53\u64cd\u4f5c\u65f6\u53ef\u4ee5\u5c06\u5404\u4e2a\u5b50\u884c\u989d\u5916\u7f29\u8fdb\u4e00\u7ea7\uff0c\u6216\u8005\uff0c\u5982\u679c\u5b50\u884c\u4e4b\u95f4\u975e\u5e38\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u5c06\u5b83\u4eec\u90fd\u5bf9\u9f50\u5230\u4e0a\u9762\u7684\u884c\uff08\u53ef\u8bfb\u6027\u66f4\u597d\uff09\u3002 1 2 3 4 5 6 7 8 9 10 int main () { std :: cout << \"This is a really, really, really, really, really, really, really, \" \"really long line \\n \" ; // one extra indentation for continuation line std :: cout << \"This is another really, really, really, really, really, really, really, \" \"really long line \\n \" ; // text aligned with the previous line for continuation line std :: cout << \"This one is short \\n \" ; } \u8fd9\u4e48\u505a\u53ef\u4ee5\u4f7f\u5f97\u6bcf\u884c\u4ee3\u7801\u90fd\u5177\u6709\u5f88\u597d\u7684\u53ef\u8bfb\u6027\u3002\u5728\u5bbd\u5c4f\u663e\u793a\u5668\u4e0a\uff0c\u8fd9\u4e48\u505a\u8fd8\u53ef\u4ee5\u5141\u8bb8\u4f60\u5c06\u4e24\u4e2a\u4ee3\u7801\u7a97\u53e3\u5e76\u6392\u663e\u793a\uff0c\u8f7b\u677e\u5730\u5bf9\u6bd4\u5b83\u4eec\u7684\u5dee\u5f02\u3002 \u6700\u4f73\u5b9e\u8df5 \u5355\u884c\u4ee3\u7801\u957f\u5ea6\u4e0d\u5e94\u8be5\u8d85\u8fc780\u4e2a\u5b57\u7b26\u3002 \u5c0f\u8d34\u58eb \u5f88\u591a\u7f16\u8f91\u5668\u90fd\u96c6\u6210\uff08\u6216\u901a\u8fc7\u63d2\u4ef6/\u6269\u5c55\uff09\u4e86 \u5217\u6307\u793a\u5668 \uff0c\u80fd\u591f\u663e\u793a\u51fa\u7279\u5b9a\u5217\u7684\u4f4d\u7f6e\uff08\u4f8b\u598280\u4e2a\u5b57\u7b26\u5904\uff09\u3002\u56e0\u6b64\u4f60\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5224\u65ad\u4f60\u7684\u4ee3\u7801\u884c\u662f\u4e0d\u662f\u8d85\u957f\u4e86\u3002\u641c\u7d22\u7f16\u8f91\u5668\u540d+\u201cColumn guide\u201d\u6765\u67e5\u8be2\u4f60\u7684\u7f16\u8f91\u5668\u662f\u5426\u652f\u6301\u8be5\u7279\u6027\u3002 \u8bd1\u6ce8\uff1a\u5728 VS Code \u4e2d\uff0c\u8fd9\u4e00\u7279\u6027\u53eb\u505a editor.rulers \uff0c\u53ef\u4ee5\u5728\u8bbe\u7f6e\u4e2d\u914d\u7f6e\u5176\u51fa\u73b0\u7684\u4f4d\u7f6e\uff0c\u4f8b\u5982 editor.rulers=[80] 5.\u5982\u679c\u4e00\u4e2a\u8f83\u957f\u7684\u884c\u88ab\u57fa\u4e8e\u8fd0\u7b97\u7b26\u5206\u5272\u4e3a\u4e86\u591a\u4e2a\u77ed\u884c\uff0c\u90a3\u4e48\u8fd0\u7b97\u7b26\u5e94\u8be5\u653e\u7f6e\u5728\u4e0b\u4e00\u884c\u7684\u5f00\u5934\u800c\u4e0d\u662f\u4e0a\u4e00\u884c\u7684\u7ed3\u5c3e\uff1a 1 2 3 std :: cout << 3 + 4 + 5 + 6 * 7 * 8 ; \u8fd9\u4e48\u505a\u53ef\u4ee5\u660e\u786e\u7684\u8868\u660e\u4e0b\u4e00\u884c\u662f\u627f\u63a5\u81ea\u4e0a\u4e00\u884c\u7684\uff0c\u800c\u4e14\u4f60\u53ef\u4ee5\u5c06\u8fd0\u7b97\u7b26\u5de6\u5bf9\u9f50\uff0c\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002 6.\u4f7f\u7528\u7a7a\u683c\u5c06\u503c\u3001\u6ce8\u91ca\u5bf9\u9f50\uff0c\u6216\u5728\u4ee3\u7801\u6bb5\u4e4b\u95f4\u6dfb\u52a0\u7a7a\u683c\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002 \u53ef\u8bfb\u6027\u4e0d\u4f73\uff1a 1 2 3 4 cost = 57 ; pricePerItem = 24 ; value = 5 ; numberOfItems = 17 ; \u53ef\u8bfb\u6027\u4f73\uff1a 1 2 3 4 cost = 57 ; pricePerItem = 24 ; value = 5 ; numberOfItems = 17 ; \u53ef\u8bfb\u6027\u4e0d\u4f73\uff1a 1 2 3 std :: cout << \"Hello world! \\n \" ; // cout lives in the iostream library std :: cout << \"It is very nice to meet you! \\n \" ; // these comments make the code hard to read std :: cout << \"Yeah! \\n \" ; // especially when lines are different lengths \u53ef\u8bfb\u6027\u4f73\uff1a 1 2 3 std :: cout << \"Hello world! \\n \" ; // cout lives in the iostream library std :: cout << \"It is very nice to meet you! \\n \" ; // these comments are easier to read std :: cout << \"Yeah! \\n \" ; // especially when all lined up \u53ef\u8bfb\u6027\u4e0d\u4f73\uff1a 1 2 3 4 5 6 // cout lives in the iostream library std :: cout << \"Hello world! \\n \" ; // these comments make the code hard to read std :: cout << \"It is very nice to meet you! \\n \" ; // especially when all bunched together std :: cout << \"Yeah! \\n \" ; \u53ef\u8bfb\u6027\u4f73\uff1a 1 2 3 4 5 6 7 8 // cout lives in the iostream library std :: cout << \"Hello world! \\n \" ; // these comments are easier to read std :: cout << \"It is very nice to meet you! \\n \" ; // when separated by whitespace std :: cout << \"Yeah! \\n \" ; \u672c\u6559\u7a0b\u5c06\u59cb\u7ec8\u8d2f\u5f7b\u4e0a\u8ff0\u6392\u7248\u7ea6\u5b9a\uff0c\u5b83\u4eec\u7ec8\u5c06\u4f1a\u79f0\u4e3a\u4f60\u4e60\u60ef\u7684\u4e00\u90e8\u5206\u3002\u5728\u6211\u4eec\u4ecb\u7ecd\u4fe1\u7684\u8bed\u8a00\u7279\u6027\u7684\u65f6\u5019\uff0c\u4e5f\u4f1a\u5bf9\u5e94\u5730\u4ecb\u7ecd\u5176\u9002\u7528\u7684\u4ee3\u7801\u6392\u7248\u98ce\u683c\u3002 C++ \u7ed9\u4e0e\u4e86\u4f60\u9009\u62e9\u4efb\u4f55\u4f60\u559c\u6b22\u7684\u98ce\u683c\u7684\u6743\u5229\uff0c\u4f46\u662f\uff0c\u6211\u4eec\u4ecd\u7136\u5f3a\u70c8\u63a8\u8350\u4f60\u4f7f\u7528\u4e0e\u6211\u4eec\u76f8\u540c\u7684\u4ee3\u7801\u98ce\u683c\uff0c\u8fd9\u4e9b\u89c4\u5219\u90fd\u662f\u5343\u9524\u767e\u70bc\uff0c\u88ab\u65e0\u6570\u4eba\u6d4b\u8bd5\u540e\u5f97\u5230\u7684\u7ecf\u9a8c\uff0c\u5b83\u4eec\u80fd\u591f\u5728\u7f16\u7a0b\u8def\u4e0a\u52a9\u4f60\u4e00\u81c2\u4e4b\u529b\u3002\u5f53\u7136\uff0c\u5982\u679c\u4f60\u5728\u57fa\u4e8e\u4ed6\u4eba\u7684\u4ee3\u7801\u8fdb\u884c\u5de5\u4f5c\uff0c\u8bf7\u9002\u5e94\u5e76\u4f7f\u7528\u4ed6\u4eec\u7684\u4ee3\u7801\u98ce\u683c\uff0c\u98ce\u683c\u4e00\u81f4\u6027\u7684\u4f18\u5148\u7ea7\u8981\u9ad8\u4e8e\u5176\u4ed6\u3002","title":"\u57fa\u672c\u4ee3\u7801\u683c\u5f0f"},{"location":"1-8-Whitespace-and-basic-formatting/#_2","text":"\u5927\u591a\u6570\u7684\u73b0\u4ee3 IDE \u90fd\u53ef\u4ee5\u5e2e\u52a9\u4f60\u6392\u7248\u4f60\u7684\u4ee3\u7801\uff08\u4f8b\u5982\uff0c\u5728\u4f60\u521b\u5efa\u51fd\u6570\u7684\u65f6\u5019\uff0cIDE\u5c31\u4f1a\u81ea\u52a8\u5e2e\u4f60\u7f29\u8fdb\u51fd\u6570\u4f53\u5185\u7684\u8bed\u53e5\uff09\u3002 \u7136\u800c\uff0c\u5f53\u4f60\u6dfb\u52a0\u3001\u5220\u9664\u4ee3\u7801\uff0c\u6216\u8005\u4fee\u6539\u4e86IDE\u7684\u9ed8\u8ba4\u6392\u7248\u89c4\u5219\u540e\uff0c\u53c8\u6216\u8005\u4f60\u4ece\u5176\u4ed6\u5730\u65b9\u62f7\u8d1d\u4e86\u4ee3\u7801\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6392\u7248\u53ef\u80fd\u4f1a\u53d8\u5f97\u4e00\u56e2\u7cdf\u3002\u4fee\u590d\u90e8\u5206\u6216\u6574\u4e2a\u6587\u4ef6\u4e2d\u7684\u6392\u7248\u95ee\u9898\u662f\u975e\u5e38\u5934\u75bc\u7684\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u73b0\u4ee3DIE\u901a\u5e38\u90fd\u63d0\u4f9b\u4e86\u81ea\u52a8\u6392\u7248\u7684\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u5bf9\u4f60\u9009\u62e9\u7684\u90e8\u5206\u6216\u8005\u6574\u4e2a\u6587\u4ef6\u8fdb\u884c\u91cd\u65b0\u6392\u7248\u3002 For Visual Studio users \u5728 Visual Studio \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 Edit > Advanced > Format Document and Edit > Advanced > Format Selection \u6765\u914d\u7f6e\u81ea\u52a8\u6392\u7248\u3002 For Code::Blocks users \u5728 Code::Blocks \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u9f20\u6807\u53f3\u952e\uff0c\u9009\u62e9 Format use AStyle \u6765\u4f7f\u7528\u3002 \u4e3a\u4e86\u4f7f\u7528\u65b9\u4fbf\uff0c\u63a8\u8350\u4e3a\u81ea\u52a8\u6392\u7248\u529f\u80fd\u7ed1\u5b9a\u4e00\u4e2a\u952e\u76d8\u5feb\u6377\u952e\u3002 \u5c0f\u8d34\u58eb \u4f7f\u7528\u81ea\u52a8\u6392\u7248\u529f\u80fd\uff0c\u662f\u4fdd\u6301\u4ee3\u7801\u98ce\u683c\u4e00\u81f4\u6027\u7684\u597d\u529e\u6cd5\u3002","title":"\u81ea\u52a8\u6392\u7248"},{"location":"1-9-Introduction-to-literals-and-operators/","tags":["literals","operator"],"text":"\u5b57\u9762\u91cf \u8003\u8651\u4e0b\u9762\u4e24\u6761\u8bed\u53e5\uff1a 1 2 std :: cout << \"Hello world!\" ; int x { 5 }; \u8fd9\u91cc\u7684 \u201cHello world!\u201d \u548c 5 \u6709\u4ec0\u4e48\u5171\u540c\u70b9\uff1f\u5b83\u4eec\u90fd\u662f\u5b57\u9762\u91cf\u3002 literals \uff08\u4e5f\u79f0\u4e3a\u5b57\u9762\u91cf\u5e38\u91cf\uff09\u6307\u7684\u662f\u88ab\u76f4\u63a5\u63d2\u5165\u5230\u6e90\u7801\u4e2d\u7684\u5e38\u91cf\u3002 \u5b57\u9762\u91cf\u548c\u53d8\u91cf\u90fd\u5177\u6709\u4e00\u4e2a\u503c\uff08\u53ca\u5176\u7c7b\u578b\uff09\u3002\u4f46\u662f\uff0c\u5b57\u9762\u91cf\u7684\u503c\u662f\u56fa\u5b9a\u7684\uff0c\u800c\u4e14\u4e0d\u80fd\u88ab\u4fee\u6539\uff08\u6240\u4ee5\u6210\u4e3a\u5e38\u91cf\uff09\uff0c\u800c\u53d8\u91cf\u7684\u503c\u662f\u53ef\u4ee5\u6539\u53d8\u7684\uff0c\u4e0d\u8bba\u662f\u901a\u8fc7\u521d\u59cb\u5316\u8fd8\u662f\u901a\u8fc7\u8d4b\u503c\u3002 \u64cd\u4f5c\u7b26 \u6570\u5b66\u4e2d\u7684\u8fd0\u7b97\u6307\u7684\u662f\u4e00\u4e2a\u8ba1\u7b97\u64cd\u4f5c\uff0c\u5b83\u6d89\u53ca\u52300\u4e2a\u6216\u591a\u4e2a\u8f93\u5165\u503c\uff08\u79f0\u4e3a \u64cd\u4f5c\u6570 \uff09\uff0c\u5e76\u80fd\u591f\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u503c\uff08\u79f0\u4e3a\u8f93\u51fa\u503c\uff09\u3002\u8fd9\u4e2a\u7279\u5b9a\u7684\u8ba1\u7b97\u64cd\u4f5c\uff0c\u901a\u8fc7\u67d0\u79cd\u7ed3\u6784\uff08\u901a\u5e38\u662f\u4e00\u4e2a\u6216\u4e00\u5bf9\u7b26\u53f7\uff09\u6765\u6307\u5b9a\uff0c\u8fd9\u79cd\u7ed3\u6784\u79f0\u4e4b\u4e3a \u8fd0\u7b97\u7b26(operator) \u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u90fd\u77e5\u9053 2 + 3 = 5 \u3002\u5728\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4e2d\uff0c2\u548c3\u79f0\u4e4b\u4e3a\u8fd0\u7b97\u7b26\uff0c\u800c\u7b26\u53f7 + \u5219\u79f0\u4e3a\u8fd0\u7b97\u7b26\uff0c\u8be5\u8fd0\u7b97\u7b26\u544a\u8bc9\u6211\u4eec\u6b64\u65f6\u5c06\u5bf9\u4e24\u4e2a\u64cd\u4f5c\u6570\u6267\u884c\u4e00\u4e2a\u52a0\u6cd5\u64cd\u4f5c\uff0c\u5e76\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u503c 5\u3002 \u4f5c\u8005\u6ce8 \u5bf9\u4e8e\u7b26\u53f7\u7c7b\u578b\u7684\u8fd0\u7b97\u7b26\uff0c\u5e38\u7528\u7684\u672f\u8bed\u8868\u793a\u65b9\u6cd5\u662f\u5c06\u8fd0\u7b97\u7b26\u672c\u8eab\u5199\u5728operator\u4e4b\u540e\uff0c\u5176\u539f\u56e0\u4f1a\u968f\u7740\u6211\u4eec\u6df1\u5165\u4e86\u89e3\u8fd0\u7b97\u7b26\u540e\u53d8\u5f97\u6e05\u6670\u3002 \u4f8b\u5982\uff0c\u52a0\u6cd5\u8fd0\u7b97\u7b26\u4f1a\u5199\u4f5c operator+ \uff0c\u63d0\u53d6\u8fd0\u7b97\u7b26\u5219\u5199\u4f5c operator>> . \u5bf9\u4e8e\u6570\u5b66\u4e2d\u5e38\u7528\u6807\u51c6\u8fd0\u7b97\u7b26\u4f60\u5e94\u5f53\u5df2\u7ecf\u975e\u5e38\u719f\u6089\u4e86\uff0c\u5305\u62ec\u52a0( + )\u3001\u51cf( - )\u3001\u4e58( * )\u3001\u9664( / )\u3002\u5728 C++ \u4e2d\uff0c\u8d4b\u503c\u53f7( = )\u4e5f\u662f\u4e00\u4e2a\u8fd0\u7b97\u7b26\uff0c\u6b64\u5916\u8fd8\u6709 \u63d2\u5165\u8fd0\u7b97\u7b26 << \u548c\u63d0\u53d6\u8fd0\u7b97\u7b26 >> \u3002\u6709\u4e9b\u8fd0\u7b97\u7b26\u5305\u542b\u591a\u4e2a\u7b26\u53f7\uff0c\u4f8b\u5982\u76f8\u7b49\u6bd4\u8f83\u8fd0\u7b97\u7b26( == )\u3002\u8fd8\u6709\u4e00\u4e9b\u8fd0\u7b97\u7b26\u5219\u662f\u4ee5\u5355\u8bcd\u7684\u5f62\u5f0f\u51fa\u73b0\u7684\uff08\u4f8b\u5982\uff1a new \uff0c delete \u548c throw \uff09\u3002 \u4e00\u4e2a\u8fd0\u7b97\u7b26\u4f7f\u7528\u7684\u64cd\u4f5c\u6570\u7684\u4e2a\u6570\uff0c\u79f0\u4e3a\u8be5\u8fd0\u7b97\u7b26\u7684 \u5143\u6570(arity) \u3002C++ \u4e2d\u7684\u8fd0\u7b97\u7b26\u5177\u6709\u4e09\u79cd\u4e0d\u540c\u7684\u5143\u6570\uff1a \u4e00\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e00\u4e2a\u64cd\u4f5c\u6570\u3002 - \u662f\u4e00\u4e2a\u4e00\u5143\u8fd0\u7b97\u7b26\u7684\u5178\u578b\uff0c\u4f8b\u5982\uff0c -5 \u3002\u5b83\u4f7f\u7528\u4e00\u4e2a\u5b57\u9762\u91cf\u64cd\u4f5c\u65705\uff0c\u5e76\u5c06\u5176\u7b26\u53f7\u53cd\u8f6c\uff0c\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u8f93\u51fa\u91cf -5\u3002 \u4e8c\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e24\u4e2a\u64cd\u4f5c\u6570\uff08\u5de6\u53f3\u64cd\u4f5c\u6570\uff09\u3002\u4f8b\u5982 3 + 4 \uff0c\u8fd0\u7b97\u7b26 + \u5c06\u5de6\u64cd\u4f5c\u6570 3 \u548c\u53f3\u64cd\u4f5c\u6570 4\u6267\u884c\u52a0\u6cd5\u8fd0\u7b97\uff0c\u8f93\u51fa\u7ed3\u679c 7\u3002\u63d2\u5165\u8fd0\u7b97\u7b26( << ) \u548c\u63d0\u53d6\u8fd0\u7b97\u7b26( >> )\u4e5f\u662f\u4e8c\u5143\u8fd0\u7b97\u7b26\u3002\u5b83\u7684\u5de6\u4fa7\u4e3a std::cout \u6216 std::cin \uff0c\u53f3\u4fa7\u662f\u8981\u8f93\u51fa\u7684\u503c\u6216\u63a5\u6536\u8f93\u5165\u7684\u53d8\u91cf\u3002 \u4e09\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e09\u4e2a\u64cd\u4f5c\u6570\u3002C++ \u4e2d\u53ea\u6709\u4e00\u79cd\u4e09\u5143\u8fd0\u7b97\u7b26\uff0c\u7a0d\u540e\u6211\u4eec\u4f1a\u4ecb\u7ecd\u3002 \u6ce8\u610f\uff0c\u6709\u4e9b\u8fd0\u7b97\u7b26\u5728\u4e0d\u540c\u7684\u4f7f\u7528\u6761\u4ef6\u4e0b\u5177\u6709\u4e0d\u540c\u7684\u542b\u4e49\u3002\u4f8b\u5982 - \u5c31\u6709\u4e24\u79cd\u4e0d\u540c\u7684\u542b\u4e49\u3002\u5b83\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u5143\u8fd0\u7b97\u7b26\u53d6\u4e00\u4e2a\u503c\u7684\u76f8\u53cd\u6570\uff08\u4f8b\u5982\u5c065\u8f6c\u6362\u4e3a-5\uff0c\u6216\u8005\u53cd\u8fc7\u6765\uff09\uff0c\u6216\u8005\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u4e8c\u5143\u8fd0\u7b97\u7b26\u51cf\u53f7\u4f7f\u7528\uff08\u4f8b\u5982 4 - 3 \uff09\u3002 \u8fd0\u7b97\u7b26\u94fe \u8fd0\u7b97\u7b26\u53ef\u4ee5\u8fdb\u884c\u94fe\u5f0f\u64cd\u4f5c\uff0c\u8fd9\u6837\u4e00\u6765\uff0c\u4e00\u4e2a\u8fd0\u7b97\u7b26\u4fbf\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5176\u4ed6\u8fd0\u7b97\u7b26\u7684\u8f93\u51fa\u7ed3\u679c\u3002\u4f8b\u5982\uff1a 2 * 3 + 4 \uff0c\u4e58\u6cd5\u9996\u5148\u6267\u884c\u5e76\u5f97\u5230\u7ed3\u679c6\uff0c\u7136\u540e\u5b83\u5c31\u6210\u4e3a\u4e86\u52a0\u53f7\u7684\u505a\u64cd\u4f5c\u6570\u3002\u63a5\u4e0b\u6765\u6267\u884c\u52a0\u6cd5\u5f97\u5230\u65b0\u503c10\u3002 \u5728\u8bb2\u89e3\u8fd0\u7b97\u7b26\u65f6\uff0c\u6211\u4eec\u4f1a\u6df1\u5165\u63a2\u8ba8\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u3002\u73b0\u5728\u4f60\u53ea\u9700\u8981\u77e5\u9053\uff0c\u5b83\u7684\u4f18\u5148\u7ea7\u548c\u6570\u5b66\u4e2d\u7684\u4f18\u5148\u7ea7\u662f\u4e00\u6837\u7684\uff1a\u62ec\u53f7\u4f18\u5148\u7ea7\u6700\u9ad8\uff0c\u7136\u540e\u662f\u6307\u6570\u8fd0\u7b97\u7b26\uff0c\u7136\u540e\u662f\u4e58\u9664\uff0c\u518d\u7136\u540e\u662f\u52a0\u51cf\u3002\u8be5\u4f18\u5148\u7ea7\u987a\u5e8f\u5e38\u5e38\u7f29\u5199\u4e3a PEMDAS \uff08\u6216\u8005\u5c55\u5f00\u4e3a\u4e0b\u9762\u8fd9\u79cd\u5e2e\u52a9\u8bb0\u5fc6\u7684\u5f62\u5f0f\uff1a\u201cPlease Excuse My Dear Aunt Sally\u201d\uff09 \u4f5c\u8005\u6ce8 \u5728\u6709\u4e9b\u56fd\u5bb6\uff0cPEMDAS \u8868\u8ff0\u4e3a PEDMAS\u3001BEDMAS\u3001BODMAS \u6216 BIDMAS\u3002","title":"1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26"},{"location":"1-9-Introduction-to-literals-and-operators/#_1","text":"\u8003\u8651\u4e0b\u9762\u4e24\u6761\u8bed\u53e5\uff1a 1 2 std :: cout << \"Hello world!\" ; int x { 5 }; \u8fd9\u91cc\u7684 \u201cHello world!\u201d \u548c 5 \u6709\u4ec0\u4e48\u5171\u540c\u70b9\uff1f\u5b83\u4eec\u90fd\u662f\u5b57\u9762\u91cf\u3002 literals \uff08\u4e5f\u79f0\u4e3a\u5b57\u9762\u91cf\u5e38\u91cf\uff09\u6307\u7684\u662f\u88ab\u76f4\u63a5\u63d2\u5165\u5230\u6e90\u7801\u4e2d\u7684\u5e38\u91cf\u3002 \u5b57\u9762\u91cf\u548c\u53d8\u91cf\u90fd\u5177\u6709\u4e00\u4e2a\u503c\uff08\u53ca\u5176\u7c7b\u578b\uff09\u3002\u4f46\u662f\uff0c\u5b57\u9762\u91cf\u7684\u503c\u662f\u56fa\u5b9a\u7684\uff0c\u800c\u4e14\u4e0d\u80fd\u88ab\u4fee\u6539\uff08\u6240\u4ee5\u6210\u4e3a\u5e38\u91cf\uff09\uff0c\u800c\u53d8\u91cf\u7684\u503c\u662f\u53ef\u4ee5\u6539\u53d8\u7684\uff0c\u4e0d\u8bba\u662f\u901a\u8fc7\u521d\u59cb\u5316\u8fd8\u662f\u901a\u8fc7\u8d4b\u503c\u3002","title":"\u5b57\u9762\u91cf"},{"location":"1-9-Introduction-to-literals-and-operators/#_2","text":"\u6570\u5b66\u4e2d\u7684\u8fd0\u7b97\u6307\u7684\u662f\u4e00\u4e2a\u8ba1\u7b97\u64cd\u4f5c\uff0c\u5b83\u6d89\u53ca\u52300\u4e2a\u6216\u591a\u4e2a\u8f93\u5165\u503c\uff08\u79f0\u4e3a \u64cd\u4f5c\u6570 \uff09\uff0c\u5e76\u80fd\u591f\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u503c\uff08\u79f0\u4e3a\u8f93\u51fa\u503c\uff09\u3002\u8fd9\u4e2a\u7279\u5b9a\u7684\u8ba1\u7b97\u64cd\u4f5c\uff0c\u901a\u8fc7\u67d0\u79cd\u7ed3\u6784\uff08\u901a\u5e38\u662f\u4e00\u4e2a\u6216\u4e00\u5bf9\u7b26\u53f7\uff09\u6765\u6307\u5b9a\uff0c\u8fd9\u79cd\u7ed3\u6784\u79f0\u4e4b\u4e3a \u8fd0\u7b97\u7b26(operator) \u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u90fd\u77e5\u9053 2 + 3 = 5 \u3002\u5728\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4e2d\uff0c2\u548c3\u79f0\u4e4b\u4e3a\u8fd0\u7b97\u7b26\uff0c\u800c\u7b26\u53f7 + \u5219\u79f0\u4e3a\u8fd0\u7b97\u7b26\uff0c\u8be5\u8fd0\u7b97\u7b26\u544a\u8bc9\u6211\u4eec\u6b64\u65f6\u5c06\u5bf9\u4e24\u4e2a\u64cd\u4f5c\u6570\u6267\u884c\u4e00\u4e2a\u52a0\u6cd5\u64cd\u4f5c\uff0c\u5e76\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u503c 5\u3002 \u4f5c\u8005\u6ce8 \u5bf9\u4e8e\u7b26\u53f7\u7c7b\u578b\u7684\u8fd0\u7b97\u7b26\uff0c\u5e38\u7528\u7684\u672f\u8bed\u8868\u793a\u65b9\u6cd5\u662f\u5c06\u8fd0\u7b97\u7b26\u672c\u8eab\u5199\u5728operator\u4e4b\u540e\uff0c\u5176\u539f\u56e0\u4f1a\u968f\u7740\u6211\u4eec\u6df1\u5165\u4e86\u89e3\u8fd0\u7b97\u7b26\u540e\u53d8\u5f97\u6e05\u6670\u3002 \u4f8b\u5982\uff0c\u52a0\u6cd5\u8fd0\u7b97\u7b26\u4f1a\u5199\u4f5c operator+ \uff0c\u63d0\u53d6\u8fd0\u7b97\u7b26\u5219\u5199\u4f5c operator>> . \u5bf9\u4e8e\u6570\u5b66\u4e2d\u5e38\u7528\u6807\u51c6\u8fd0\u7b97\u7b26\u4f60\u5e94\u5f53\u5df2\u7ecf\u975e\u5e38\u719f\u6089\u4e86\uff0c\u5305\u62ec\u52a0( + )\u3001\u51cf( - )\u3001\u4e58( * )\u3001\u9664( / )\u3002\u5728 C++ \u4e2d\uff0c\u8d4b\u503c\u53f7( = )\u4e5f\u662f\u4e00\u4e2a\u8fd0\u7b97\u7b26\uff0c\u6b64\u5916\u8fd8\u6709 \u63d2\u5165\u8fd0\u7b97\u7b26 << \u548c\u63d0\u53d6\u8fd0\u7b97\u7b26 >> \u3002\u6709\u4e9b\u8fd0\u7b97\u7b26\u5305\u542b\u591a\u4e2a\u7b26\u53f7\uff0c\u4f8b\u5982\u76f8\u7b49\u6bd4\u8f83\u8fd0\u7b97\u7b26( == )\u3002\u8fd8\u6709\u4e00\u4e9b\u8fd0\u7b97\u7b26\u5219\u662f\u4ee5\u5355\u8bcd\u7684\u5f62\u5f0f\u51fa\u73b0\u7684\uff08\u4f8b\u5982\uff1a new \uff0c delete \u548c throw \uff09\u3002 \u4e00\u4e2a\u8fd0\u7b97\u7b26\u4f7f\u7528\u7684\u64cd\u4f5c\u6570\u7684\u4e2a\u6570\uff0c\u79f0\u4e3a\u8be5\u8fd0\u7b97\u7b26\u7684 \u5143\u6570(arity) \u3002C++ \u4e2d\u7684\u8fd0\u7b97\u7b26\u5177\u6709\u4e09\u79cd\u4e0d\u540c\u7684\u5143\u6570\uff1a \u4e00\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e00\u4e2a\u64cd\u4f5c\u6570\u3002 - \u662f\u4e00\u4e2a\u4e00\u5143\u8fd0\u7b97\u7b26\u7684\u5178\u578b\uff0c\u4f8b\u5982\uff0c -5 \u3002\u5b83\u4f7f\u7528\u4e00\u4e2a\u5b57\u9762\u91cf\u64cd\u4f5c\u65705\uff0c\u5e76\u5c06\u5176\u7b26\u53f7\u53cd\u8f6c\uff0c\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u8f93\u51fa\u91cf -5\u3002 \u4e8c\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e24\u4e2a\u64cd\u4f5c\u6570\uff08\u5de6\u53f3\u64cd\u4f5c\u6570\uff09\u3002\u4f8b\u5982 3 + 4 \uff0c\u8fd0\u7b97\u7b26 + \u5c06\u5de6\u64cd\u4f5c\u6570 3 \u548c\u53f3\u64cd\u4f5c\u6570 4\u6267\u884c\u52a0\u6cd5\u8fd0\u7b97\uff0c\u8f93\u51fa\u7ed3\u679c 7\u3002\u63d2\u5165\u8fd0\u7b97\u7b26( << ) \u548c\u63d0\u53d6\u8fd0\u7b97\u7b26( >> )\u4e5f\u662f\u4e8c\u5143\u8fd0\u7b97\u7b26\u3002\u5b83\u7684\u5de6\u4fa7\u4e3a std::cout \u6216 std::cin \uff0c\u53f3\u4fa7\u662f\u8981\u8f93\u51fa\u7684\u503c\u6216\u63a5\u6536\u8f93\u5165\u7684\u53d8\u91cf\u3002 \u4e09\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e09\u4e2a\u64cd\u4f5c\u6570\u3002C++ \u4e2d\u53ea\u6709\u4e00\u79cd\u4e09\u5143\u8fd0\u7b97\u7b26\uff0c\u7a0d\u540e\u6211\u4eec\u4f1a\u4ecb\u7ecd\u3002 \u6ce8\u610f\uff0c\u6709\u4e9b\u8fd0\u7b97\u7b26\u5728\u4e0d\u540c\u7684\u4f7f\u7528\u6761\u4ef6\u4e0b\u5177\u6709\u4e0d\u540c\u7684\u542b\u4e49\u3002\u4f8b\u5982 - \u5c31\u6709\u4e24\u79cd\u4e0d\u540c\u7684\u542b\u4e49\u3002\u5b83\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u5143\u8fd0\u7b97\u7b26\u53d6\u4e00\u4e2a\u503c\u7684\u76f8\u53cd\u6570\uff08\u4f8b\u5982\u5c065\u8f6c\u6362\u4e3a-5\uff0c\u6216\u8005\u53cd\u8fc7\u6765\uff09\uff0c\u6216\u8005\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u4e8c\u5143\u8fd0\u7b97\u7b26\u51cf\u53f7\u4f7f\u7528\uff08\u4f8b\u5982 4 - 3 \uff09\u3002","title":"\u64cd\u4f5c\u7b26"},{"location":"1-9-Introduction-to-literals-and-operators/#_3","text":"\u8fd0\u7b97\u7b26\u53ef\u4ee5\u8fdb\u884c\u94fe\u5f0f\u64cd\u4f5c\uff0c\u8fd9\u6837\u4e00\u6765\uff0c\u4e00\u4e2a\u8fd0\u7b97\u7b26\u4fbf\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5176\u4ed6\u8fd0\u7b97\u7b26\u7684\u8f93\u51fa\u7ed3\u679c\u3002\u4f8b\u5982\uff1a 2 * 3 + 4 \uff0c\u4e58\u6cd5\u9996\u5148\u6267\u884c\u5e76\u5f97\u5230\u7ed3\u679c6\uff0c\u7136\u540e\u5b83\u5c31\u6210\u4e3a\u4e86\u52a0\u53f7\u7684\u505a\u64cd\u4f5c\u6570\u3002\u63a5\u4e0b\u6765\u6267\u884c\u52a0\u6cd5\u5f97\u5230\u65b0\u503c10\u3002 \u5728\u8bb2\u89e3\u8fd0\u7b97\u7b26\u65f6\uff0c\u6211\u4eec\u4f1a\u6df1\u5165\u63a2\u8ba8\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u3002\u73b0\u5728\u4f60\u53ea\u9700\u8981\u77e5\u9053\uff0c\u5b83\u7684\u4f18\u5148\u7ea7\u548c\u6570\u5b66\u4e2d\u7684\u4f18\u5148\u7ea7\u662f\u4e00\u6837\u7684\uff1a\u62ec\u53f7\u4f18\u5148\u7ea7\u6700\u9ad8\uff0c\u7136\u540e\u662f\u6307\u6570\u8fd0\u7b97\u7b26\uff0c\u7136\u540e\u662f\u4e58\u9664\uff0c\u518d\u7136\u540e\u662f\u52a0\u51cf\u3002\u8be5\u4f18\u5148\u7ea7\u987a\u5e8f\u5e38\u5e38\u7f29\u5199\u4e3a PEMDAS \uff08\u6216\u8005\u5c55\u5f00\u4e3a\u4e0b\u9762\u8fd9\u79cd\u5e2e\u52a9\u8bb0\u5fc6\u7684\u5f62\u5f0f\uff1a\u201cPlease Excuse My Dear Aunt Sally\u201d\uff09 \u4f5c\u8005\u6ce8 \u5728\u6709\u4e9b\u56fd\u5bb6\uff0cPEMDAS \u8868\u8ff0\u4e3a PEDMAS\u3001BEDMAS\u3001BODMAS \u6216 BIDMAS\u3002","title":"\u8fd0\u7b97\u7b26\u94fe"},{"location":"1-x-Chapter-1-summary-and-quiz/","tags":["summary"],"text":"\u7ae0\u8282\u56de\u987e \u8bed\u53e5 \u662f\u4e00\u79cd\u6307\u4ee4\uff0c\u8ba9\u7a0b\u5e8f\u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\u3002\u8bed\u53e5\u901a\u5e38\u4ee5\u5206\u53f7\u7ed3\u5c3e\u3002 \u51fd\u6570 \u662f\u4e00\u7ec4\u5e8f\u5217\u6267\u884c\u7684\u8bed\u53e5\u3002\u6bcf\u4e2a C++\u7a0b\u5e8f\u90fd\u5fc5\u987b\u5305\u542b\u4e00\u4e2a main \u51fd\u6570\u3002\u5f53\u4f60\u6267\u884c\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u9996\u5148\u4f1a\u4ece main \u51fd\u6570\u5f00\u59cb\u6267\u884c\u3002 \u8fd9\u4e9b\u63a7\u5236 C++ \u5143\u7d20\u7ec4\u7ec7\u5f62\u5f0f\u7684\u89c4\u5219\uff0c\u79f0\u4e3a \u8bed\u6cd5 \u3002\u5f53\u4f60\u8fdd\u53cd\u8bed\u8a00\u7684\u8bed\u6cd5\u89c4\u5219\u65f6\uff0c\u5c31\u4f1a\u4ea7\u751f\u8bed\u6cd5\u9519\u8bef\u3002 \u6ce8\u91ca \u5141\u8bb8\u4f60\u5728\u4ee3\u7801\u4e2d\u7559\u4e0b\u7b14\u8bb0\u3002C++ \u652f\u6301\u4e24\u79cd\u7c7b\u578b\u7684\u6ce8\u91ca\u3002\u884c\u5185\u6ce8\u91ca\u4ee5 // \u5f00\u5934\u3002\u6bb5\u843d\u6ce8\u91ca\u5219\u4ee5 /* \u5f00\u5934\uff0c\u4ee5 */ \u7ed3\u675f\u3002\u8bf7\u4e0d\u8981\u5d4c\u5957\u6ce8\u91ca\u3002 \u4f60\u53ef\u4ee5\u901a\u8fc7\u6ce8\u91ca\u6765\u4e34\u65f6\u5c4f\u853d\u4e00\u884c\u6216\u4e00\u6bb5\u4ee3\u7801\uff0c\u4e0a\u8ff0\u64cd\u4f5c\u79f0\u4e3a\u6ce8\u91ca\u6389\uff08commenting out\uff09\u4ee3\u7801\u3002 \u6570\u636e \u662f\u4e00\u7cfb\u5217\u7b26\u53f7\u5e76\u53ef\u4ee5\u88ab\u89e3\u6790\u4e3a\u67d0\u79cd\u542b\u4e49\u3002\u4e00\u6bb5\u88ab\u5b58\u653e\u5728\u5185\u5b58\u4e2d\u7684\u6570\u636e\u79f0\u4e3a \u503c \u3002 \u53d8\u91cf \u662f\u4e00\u6bb5\u547d\u540d\u7684\u5185\u5b58\uff0c\u73b0\u5728\u6211\u4eec\u4fbf\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5b58\u653e\u503c\u4e86\u3002\u53d8\u91cf\u7684\u540d\u79f0\u4e3a \u6807\u8bc6\u7b26(identifier) \u3002\u521b\u5efa\u53d8\u91cf\u9700\u8981\u901a\u8fc7\u58f0\u660e\u8bed\u53e5\u3002\u5f53\u7a0b\u5e8f\u5f00\u59cb\u8fd0\u884c\u7684\u65f6\u5019\uff0c\u53d8\u91cf\u5c31\u4f1a\u88ab \u5b9e\u4f8b\u5316 \uff0c\u5373\u5b83\u4f1a\u88ab\u5173\u8054\u5230\u67d0\u4e2a\u5185\u5b58\u5730\u5740 \u3002 \u6570\u636e\u7c7b\u578b \u53ef\u4ee5\u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u4f55\u5c06\u4e00\u6bb5\u6570\u636e\u89e3\u91ca\u4e3a\u5177\u6709\u542b\u4e49\u7684\u503c\u3002\u6574\u578b\u6307\u7684\u662f\u4e0d\u5305\u542b\u5c0f\u6570\u90e8\u5206\u7684\u503c\uff0c\u4f8b\u5982 4\u300127\u30010\u3001-2 \u6216-12\u3002 \u62f7\u8d1d\u8d4b\u503c (\u901a\u8fc7 operator= ) \u53ef\u4ee5\u88ab\u7528\u6765\u5411\u67d0\u4e2a\u5df2\u7ecf\u88ab\u521b\u5efa\u7684\u53d8\u91cf\u8d4b\u503c\u3002 \u521d\u59cb\u5316 \u7528\u4e8e\u5728\u53d8\u91cf\u88ab\u521b\u5efa\u7684\u540c\u65f6\u4e3a\u5176\u6307\u5b9a\u3002C++ \u652f\u6301\u4e09\u79cd\u7c7b\u578b\u7684\u521d\u59cb\u5316\uff1a \u62f7\u8d1d\u521d\u59cb\u5316 \u3001 \u76f4\u63a5\u521d\u59cb\u5316 \u548c \u7edf\u4e00\u521d\u59cb\u5316 \uff08\u4e5f\u79f0\u4e3a \u5217\u8868\u521d\u59cb\u5316 \u6216 \u62ec\u53f7\u521d\u59cb\u5316 \uff09\u3002 \u4f60\u5e94\u8be5\u4f18\u5148\u4f7f\u7528\u7edf\u4e00\u521d\u59cb\u5316\u800c\u4e0d\u662f\u5176\u4ed6\u521d\u59cb\u5316\u65b9\u5f0f\uff0c\u6b64\u5916\uff0c\u4f7f\u7528\u521d\u59cb\u5316\u4e5f\u4f18\u4e8e\u8d4b\u503c\u3002 \u5c3d\u7ba1\u5728\u4e00\u4e2a\u8bed\u53e5\u4e2d\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\uff0c\u4f46\u662f\u6700\u597d\u8fd8\u662f\u6bcf\u884c\u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cf\u3002 std:: cout \u548c operator<< \u53ef\u4ee5\u5c06\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u4f5c\u4e3a\u6587\u672c\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002 std:: endl \u4f1a\u8f93\u51fa\u4e00\u4e2a\u6362\u884c\u7b26\uff0c\u63a7\u5236\u63a7\u5236\u53f0\u5149\u6807\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\u3002 std:: cin \u548c operator>> \u53ef\u4ee5\u4ece\u952e\u76d8\u83b7\u53d6\u8f93\u5165\u503c\u3002 \u6ca1\u6709\u88ab\u6307\u5b9a\u503c\u7684\u53d8\u91cf\uff0c\u79f0\u4e3a\u672a\u521d\u59cb\u5316\u53d8\u91cf\u3002\u5c1d\u8bd5\u83b7\u53d6\u672a\u521d\u59cb\u5316\u53d8\u91cf\u7684\u503c\uff0c\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \uff0c\u672a\u5b9a\u4e49\u884c\u4e3a\u6709\u591a\u79cd\u8868\u73b0\u5f62\u5f0f\u3002 C++ \u9884\u7559\u4e86\u4e00\u7ec4\u540d\u79f0\uff0c\u79f0\u4e3a \u5173\u952e\u5b57 \u3002\u8fd9\u4e9b\u5173\u952e\u5b57\u5728 C++\u4e2d\u5177\u6709\u7279\u6b8a\u7684\u542b\u4e49\uff0c\u4e0d\u80fd\u88ab\u7528\u4f5c\u53d8\u91cf\u540d\u3002 \u5b57\u9762\u91cf \u6307\u7684\u662f\u76f4\u63a5\u63d2\u5165\u6e90\u7801\u4e2d\u7684\u56fa\u5b9a\u503c\u3002\u4f8b\u5982 5 \u548c \u201cHello world!\u201d\u3002 \u8fd0\u7b97\u6307\u7684\u662f\u4e00\u6b21\u6570\u5b66\u8ba1\u7b97\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u6d89\u53ca\u52300\u4e2a\u6216\u591a\u4e2a\u8f93\u5165\u503c\uff0c\u79f0\u4e3a \u64cd\u4f5c\u6570 \u3002\u5177\u4f53\u7684\u8fd0\u7b97\u901a\u8fc7\u8fd0\u7b97\u7b26\u6765\u6307\u5b9a\u3002\u8fd0\u7b97\u7684\u7ed3\u679c\u4f1a\u4ea7\u751f\u4e00\u4e2a\u8f93\u51fa\u503c\u3002 \u4e00\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e00\u4e2a\u64cd\u4f5c\u6570\u3002 \u4e8c\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e24\u4e2a\u64cd\u4f5c\u6570\uff0c\u901a\u5e38\u79f0\u4e3a\u5de6\u64cd\u4f5c\u6570\u548c\u53f3\u64cd\u4f5c\u6570\u3002 \u4e09\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e09\u4e2a\u64cd\u4f5c\u6570\u3002 \u8868\u8fbe\u5f0f \u662f\u4e00\u7ec4\u5b57\u9762\u91cf\u3001\u53d8\u91cf\u3001\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u8c03\u7528\u7684\u7ec4\u5408\uff0c\u5b83\u7ecf\u8fc7\u6c42\u503c\u540e\uff0c\u4f1a\u5f97\u5230\u4e00\u4e2a\u5355\u4e00\u503c\u3002\u8ba1\u7b97\u5f97\u5230\u8be5\u5355\u4e00\u503c\u7684\u8fc7\u7a0b\u79f0\u4e4b\u4e3a \u6c42\u503c(evaluation) \uff0c\u800c\u8be5\u503c\u79f0\u4e3a\u8868\u8fbe\u5f0f\u7684 \u7ed3\u679c \u3002 \u901a\u8fc7\u5728\u8868\u8fbe\u5f0f\u7ed3\u5c3e\u6dfb\u52a0\u5206\u53f7\uff0c\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a \u8868\u8fbe\u5f0f\u8bed\u53e5 \u3002 \u7f16\u7a0b\u4e0d\u662f\u4ef6\u5bb9\u6613\u4e8b\uff0c\u4f60\u7684\u7a0b\u5e8f\u7684\u7b2c\u4e00\u4e2a\u7248\u672c\u4e0d\u53ef\u80fd\u662f\u5b8c\u7f8e\u7684\u3002\u8ba9\u7a0b\u5e8f\u80fd\u591f\u8fd0\u884c\u8d77\u6765\uff0c\u662f\u7f16\u7a0b\u7684\u9996\u8981\u4efb\u52a1\uff0c\u7136\u540e\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f18\u5316\u4f7f\u5176\u53d8\u5f97\u66f4\u597d\u3002","title":"1.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - C++\u57fa\u7840"},{"location":"1-x-Chapter-1-summary-and-quiz/#_1","text":"\u8bed\u53e5 \u662f\u4e00\u79cd\u6307\u4ee4\uff0c\u8ba9\u7a0b\u5e8f\u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\u3002\u8bed\u53e5\u901a\u5e38\u4ee5\u5206\u53f7\u7ed3\u5c3e\u3002 \u51fd\u6570 \u662f\u4e00\u7ec4\u5e8f\u5217\u6267\u884c\u7684\u8bed\u53e5\u3002\u6bcf\u4e2a C++\u7a0b\u5e8f\u90fd\u5fc5\u987b\u5305\u542b\u4e00\u4e2a main \u51fd\u6570\u3002\u5f53\u4f60\u6267\u884c\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u9996\u5148\u4f1a\u4ece main \u51fd\u6570\u5f00\u59cb\u6267\u884c\u3002 \u8fd9\u4e9b\u63a7\u5236 C++ \u5143\u7d20\u7ec4\u7ec7\u5f62\u5f0f\u7684\u89c4\u5219\uff0c\u79f0\u4e3a \u8bed\u6cd5 \u3002\u5f53\u4f60\u8fdd\u53cd\u8bed\u8a00\u7684\u8bed\u6cd5\u89c4\u5219\u65f6\uff0c\u5c31\u4f1a\u4ea7\u751f\u8bed\u6cd5\u9519\u8bef\u3002 \u6ce8\u91ca \u5141\u8bb8\u4f60\u5728\u4ee3\u7801\u4e2d\u7559\u4e0b\u7b14\u8bb0\u3002C++ \u652f\u6301\u4e24\u79cd\u7c7b\u578b\u7684\u6ce8\u91ca\u3002\u884c\u5185\u6ce8\u91ca\u4ee5 // \u5f00\u5934\u3002\u6bb5\u843d\u6ce8\u91ca\u5219\u4ee5 /* \u5f00\u5934\uff0c\u4ee5 */ \u7ed3\u675f\u3002\u8bf7\u4e0d\u8981\u5d4c\u5957\u6ce8\u91ca\u3002 \u4f60\u53ef\u4ee5\u901a\u8fc7\u6ce8\u91ca\u6765\u4e34\u65f6\u5c4f\u853d\u4e00\u884c\u6216\u4e00\u6bb5\u4ee3\u7801\uff0c\u4e0a\u8ff0\u64cd\u4f5c\u79f0\u4e3a\u6ce8\u91ca\u6389\uff08commenting out\uff09\u4ee3\u7801\u3002 \u6570\u636e \u662f\u4e00\u7cfb\u5217\u7b26\u53f7\u5e76\u53ef\u4ee5\u88ab\u89e3\u6790\u4e3a\u67d0\u79cd\u542b\u4e49\u3002\u4e00\u6bb5\u88ab\u5b58\u653e\u5728\u5185\u5b58\u4e2d\u7684\u6570\u636e\u79f0\u4e3a \u503c \u3002 \u53d8\u91cf \u662f\u4e00\u6bb5\u547d\u540d\u7684\u5185\u5b58\uff0c\u73b0\u5728\u6211\u4eec\u4fbf\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5b58\u653e\u503c\u4e86\u3002\u53d8\u91cf\u7684\u540d\u79f0\u4e3a \u6807\u8bc6\u7b26(identifier) \u3002\u521b\u5efa\u53d8\u91cf\u9700\u8981\u901a\u8fc7\u58f0\u660e\u8bed\u53e5\u3002\u5f53\u7a0b\u5e8f\u5f00\u59cb\u8fd0\u884c\u7684\u65f6\u5019\uff0c\u53d8\u91cf\u5c31\u4f1a\u88ab \u5b9e\u4f8b\u5316 \uff0c\u5373\u5b83\u4f1a\u88ab\u5173\u8054\u5230\u67d0\u4e2a\u5185\u5b58\u5730\u5740 \u3002 \u6570\u636e\u7c7b\u578b \u53ef\u4ee5\u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u4f55\u5c06\u4e00\u6bb5\u6570\u636e\u89e3\u91ca\u4e3a\u5177\u6709\u542b\u4e49\u7684\u503c\u3002\u6574\u578b\u6307\u7684\u662f\u4e0d\u5305\u542b\u5c0f\u6570\u90e8\u5206\u7684\u503c\uff0c\u4f8b\u5982 4\u300127\u30010\u3001-2 \u6216-12\u3002 \u62f7\u8d1d\u8d4b\u503c (\u901a\u8fc7 operator= ) \u53ef\u4ee5\u88ab\u7528\u6765\u5411\u67d0\u4e2a\u5df2\u7ecf\u88ab\u521b\u5efa\u7684\u53d8\u91cf\u8d4b\u503c\u3002 \u521d\u59cb\u5316 \u7528\u4e8e\u5728\u53d8\u91cf\u88ab\u521b\u5efa\u7684\u540c\u65f6\u4e3a\u5176\u6307\u5b9a\u3002C++ \u652f\u6301\u4e09\u79cd\u7c7b\u578b\u7684\u521d\u59cb\u5316\uff1a \u62f7\u8d1d\u521d\u59cb\u5316 \u3001 \u76f4\u63a5\u521d\u59cb\u5316 \u548c \u7edf\u4e00\u521d\u59cb\u5316 \uff08\u4e5f\u79f0\u4e3a \u5217\u8868\u521d\u59cb\u5316 \u6216 \u62ec\u53f7\u521d\u59cb\u5316 \uff09\u3002 \u4f60\u5e94\u8be5\u4f18\u5148\u4f7f\u7528\u7edf\u4e00\u521d\u59cb\u5316\u800c\u4e0d\u662f\u5176\u4ed6\u521d\u59cb\u5316\u65b9\u5f0f\uff0c\u6b64\u5916\uff0c\u4f7f\u7528\u521d\u59cb\u5316\u4e5f\u4f18\u4e8e\u8d4b\u503c\u3002 \u5c3d\u7ba1\u5728\u4e00\u4e2a\u8bed\u53e5\u4e2d\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\uff0c\u4f46\u662f\u6700\u597d\u8fd8\u662f\u6bcf\u884c\u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cf\u3002 std:: cout \u548c operator<< \u53ef\u4ee5\u5c06\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u4f5c\u4e3a\u6587\u672c\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002 std:: endl \u4f1a\u8f93\u51fa\u4e00\u4e2a\u6362\u884c\u7b26\uff0c\u63a7\u5236\u63a7\u5236\u53f0\u5149\u6807\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\u3002 std:: cin \u548c operator>> \u53ef\u4ee5\u4ece\u952e\u76d8\u83b7\u53d6\u8f93\u5165\u503c\u3002 \u6ca1\u6709\u88ab\u6307\u5b9a\u503c\u7684\u53d8\u91cf\uff0c\u79f0\u4e3a\u672a\u521d\u59cb\u5316\u53d8\u91cf\u3002\u5c1d\u8bd5\u83b7\u53d6\u672a\u521d\u59cb\u5316\u53d8\u91cf\u7684\u503c\uff0c\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \uff0c\u672a\u5b9a\u4e49\u884c\u4e3a\u6709\u591a\u79cd\u8868\u73b0\u5f62\u5f0f\u3002 C++ \u9884\u7559\u4e86\u4e00\u7ec4\u540d\u79f0\uff0c\u79f0\u4e3a \u5173\u952e\u5b57 \u3002\u8fd9\u4e9b\u5173\u952e\u5b57\u5728 C++\u4e2d\u5177\u6709\u7279\u6b8a\u7684\u542b\u4e49\uff0c\u4e0d\u80fd\u88ab\u7528\u4f5c\u53d8\u91cf\u540d\u3002 \u5b57\u9762\u91cf \u6307\u7684\u662f\u76f4\u63a5\u63d2\u5165\u6e90\u7801\u4e2d\u7684\u56fa\u5b9a\u503c\u3002\u4f8b\u5982 5 \u548c \u201cHello world!\u201d\u3002 \u8fd0\u7b97\u6307\u7684\u662f\u4e00\u6b21\u6570\u5b66\u8ba1\u7b97\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u6d89\u53ca\u52300\u4e2a\u6216\u591a\u4e2a\u8f93\u5165\u503c\uff0c\u79f0\u4e3a \u64cd\u4f5c\u6570 \u3002\u5177\u4f53\u7684\u8fd0\u7b97\u901a\u8fc7\u8fd0\u7b97\u7b26\u6765\u6307\u5b9a\u3002\u8fd0\u7b97\u7684\u7ed3\u679c\u4f1a\u4ea7\u751f\u4e00\u4e2a\u8f93\u51fa\u503c\u3002 \u4e00\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e00\u4e2a\u64cd\u4f5c\u6570\u3002 \u4e8c\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e24\u4e2a\u64cd\u4f5c\u6570\uff0c\u901a\u5e38\u79f0\u4e3a\u5de6\u64cd\u4f5c\u6570\u548c\u53f3\u64cd\u4f5c\u6570\u3002 \u4e09\u5143 \u8fd0\u7b97\u7b26\u4f7f\u7528\u4e09\u4e2a\u64cd\u4f5c\u6570\u3002 \u8868\u8fbe\u5f0f \u662f\u4e00\u7ec4\u5b57\u9762\u91cf\u3001\u53d8\u91cf\u3001\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u8c03\u7528\u7684\u7ec4\u5408\uff0c\u5b83\u7ecf\u8fc7\u6c42\u503c\u540e\uff0c\u4f1a\u5f97\u5230\u4e00\u4e2a\u5355\u4e00\u503c\u3002\u8ba1\u7b97\u5f97\u5230\u8be5\u5355\u4e00\u503c\u7684\u8fc7\u7a0b\u79f0\u4e4b\u4e3a \u6c42\u503c(evaluation) \uff0c\u800c\u8be5\u503c\u79f0\u4e3a\u8868\u8fbe\u5f0f\u7684 \u7ed3\u679c \u3002 \u901a\u8fc7\u5728\u8868\u8fbe\u5f0f\u7ed3\u5c3e\u6dfb\u52a0\u5206\u53f7\uff0c\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a \u8868\u8fbe\u5f0f\u8bed\u53e5 \u3002 \u7f16\u7a0b\u4e0d\u662f\u4ef6\u5bb9\u6613\u4e8b\uff0c\u4f60\u7684\u7a0b\u5e8f\u7684\u7b2c\u4e00\u4e2a\u7248\u672c\u4e0d\u53ef\u80fd\u662f\u5b8c\u7f8e\u7684\u3002\u8ba9\u7a0b\u5e8f\u80fd\u591f\u8fd0\u884c\u8d77\u6765\uff0c\u662f\u7f16\u7a0b\u7684\u9996\u8981\u4efb\u52a1\uff0c\u7136\u540e\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f18\u5316\u4f7f\u5176\u53d8\u5f97\u66f4\u597d\u3002","title":"\u7ae0\u8282\u56de\u987e"},{"location":"10-1-Introduction-to-program-defined-user-defined-types/","tags":["type"],"text":"Key Takeaway \u81ea\u5b9a\u4e49\u7c7b\u578b\u6307\u4ee3\u6240\u6709\u975eC++\u6838\u5fc3\u7c7b\u578b\uff0c\u5305\u62ec\u6807\u51c6\u5e93 \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u624d\u6307\u4ee3\u7f16\u5199\u5f53\u524d\u7a0b\u5e8f\u65f6\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u578b \u7c7b\u578b\u5b9a\u4e49\u5728\u67d0\u79cd\u7a0b\u5ea6\u4e0a\u662f\u4e0d\u53d7\u9650\u4e8e\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u7684\uff1a\u7c7b\u578b\u7684\u5b9a\u4e49\u53ef\u4ee5\u51fa\u73b0\u5728\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\u3002\u56e0\u4e3a\u7f16\u8bd1\u5668\u5fc5\u987b\u770b\u5230\u5b8c\u6574\u7c7b\u578b\u5b9a\u4e49\u624d\u80fd\u5de5\u4f5c\uff08\u786e\u5b9a\u5185\u5b58\u5206\u914d\u5927\u5c0f\uff09\u3002\u4f46\u662f\u540c\u4e00\u4e2a\u6587\u4ef6\u4e2d\u53ea\u80fd\u51fa\u73b0\u4e00\u6b21\uff0c\u8fd9\u4e2a\u662f\u6c38\u6052\u7684\u771f\u7406 \u57fa\u672c\u6570\u636e\u7c7b\u578b\u662f C++ \u8bed\u8a00\u7684\u6838\u5fc3\u90e8\u5206\uff0c\u8bbe\u8ba1\u5b83\u4eec\u7684\u76ee\u7684\u5c31\u662f\u4e3a\u4e86\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u62ff\u6765\u5c31\u7528\u3002\u4f8b\u5982\uff0c\u5f53\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a int \u6216\u8005 double \u7c7b\u578b\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53ea\u9700\u8981\uff1a 1 2 int x ; // define variable of fundamental type 'int' double d ; // define variable of fundamental type 'double' \u5bf9\u4e8e\u90a3\u4e9b\u7531\u57fa\u672c\u6570\u636e\u7c7b\u578b\u901a\u8fc7\u7b80\u5355\u6269\u5c55\u800c\u5f97\u5230\u7684 \u590d\u5408\u6570\u636e\u7c7b\u578b \u4e5f\u662f\u4e00\u6837\u7684\uff08\u5305\u62ec\u51fd\u6570\u3001\u6307\u9488\u3001\u5f15\u7528\u548c\u6570\u7ec4\uff09\uff1a 1 2 3 4 void fcn ( int ) {}; // define a function of type void()(int) int * ptr ; // define variable of compound type 'pointer to int' int & ref ; // define variable of compound type 'reference to int' int arr [ 5 ]; // define an array of 5 integers of type int[5] (we'll cover this in a future chapter) \u8fd9\u4e9b\u7ec4\u5408\u53ef\u4ee5\u6b63\u786e\u5de5\u4f5c\u662f\u56e0\u4e3aC++\u8bed\u8a00\u5df2\u7ecf\u77e5\u6653\u8fd9\u4e9b\u6570\u636e\u7c7b\u578b\u540d\uff08\u548c\u7b26\u53f7\uff09\u7684\u542b\u4e49\u2014\u2014\u56e0\u6b64\u6211\u4eec\u4e0d\u5fc5\u63d0\u4f9b\u6216\u5bfc\u5165\u5176\u5b9a\u4e49\u3002 \u800c\u5bf9\u4e8e\u4f7f\u7528\u7c7b\u578b\u522b\u540d\uff08 8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d \uff09\u7684\u60c5\u51b5\u6765\u8bf4\uff0c\u7c7b\u578b\u522b\u540d\u4f7f\u6211\u4eec\u53ef\u4ee5\u4e3a\u4e00\u4e2a\u5df2\u77e5\u7c7b\u578b\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u540d\u5b57\uff0c\u56e0\u6b64\u7c7b\u578b\u522b\u540d\u5b9e\u9645\u4e0a\u5411\u7a0b\u5e8f\u5f15\u5165\u4e86\u4e00\u4e2a\u65b0\u7684\u6807\u8bc6\u7b26\uff0c\u6240\u4ee5\u7c7b\u578b\u522b\u540d\u5fc5\u987b\u5148\u5b9a\u4e49\u540e\u4f7f\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include using length = int ; // define a type alias with identifier 'length' int main () { length x { 5 }; // we can use 'length' here since we defined it above std :: cout << x ; return 0 ; } \u5982\u679c\u6ca1\u6709\u5bf9 length \u5b9a\u4e49\uff0c\u7f16\u8bd1\u5668\u5c06\u65e0\u6cd5\u77e5\u9053\u5728\u6211\u4eec\u4f7f\u7528\u5b83\u5b9a\u4e49\u53d8\u91cf\u65f6\u77e5\u6653\u5176\u542b\u4e49\u3002\u5bf9 length \u7684\u5b9a\u4e49\u5e76\u6ca1\u6709\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\u2014\u2014\u5b83\u53ea\u662f\u544a\u8bc9\u7f16\u8bd1\u5668 length \u4f60\u7a0d\u540e\u53ef\u4ee5\u4f7f\u7528\u5b83\u5b9a\u4e49\u53d8\u91cf\u3002 \u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u662f\u4ec0\u4e48\uff1f \u5728 9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b \u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u66fe\u7ecf\u4ecb\u7ecd\u8fc7\u5982\u4f55\u5b58\u50a8\u4e00\u4e2a\u5206\u6570\u3002\u5bf9\u4e8e\u5206\u6570\u6765\u8bf4\uff0c\u5b83\u5305\u62ec\u5206\u5b50\u548c\u5206\u6bcd\u4e24\u4e2a\u90e8\u5206\uff0c\u800c\u4e14\u5b83\u4eec\u4e4b\u95f4\u662f\u5b58\u5728\u6982\u5ff5\u6027\u8054\u7cfb\u7684\u3002\u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba\u4f7f\u7528\u4e24\u4e2a\u6574\u578b\u5206\u522b\u8868\u793a\u5206\u5b50\u5206\u6bcd\u53ef\u80fd\u5e26\u6765\u7684\u95ee\u9898\u3002 \u5982\u679cC++\u6709\u80fd\u591f\u8868\u793a\u5206\u6570\u7684\u5185\u5efa\u7c7b\u578b\u5c31\u597d\u4e86\u2014\u2014\u4f46\u53ef\u60dc\u5e76\u6ca1\u6709\u3002\u8fd8\u6709\u6570\u767e\u79cd\u5176\u4ed6\u53ef\u80fd\u6709\u7528\u7684\u7c7b\u578bC++\u6ca1\u6709\u5305\u542b\uff0c\u56e0\u4e3a\u5b83\u4e0d\u53ef\u80fd\u9884\u6d4b\u4eba\u4eec\u53ef\u80fd\u9700\u8981\u7684\u6240\u6709\u4e1c\u897f(\u66f4\u4e0d\u7528\u8bf4\u5b9e\u73b0\u548c\u6d4b\u8bd5\u90a3\u4e9b\u4e1c\u897f\u4e86)\u3002 \u76f8\u53cd\uff0cC++\u63d0\u4f9b\u4e86\u89e3\u51b3\u8be5\u95ee\u9898\u7684\u53e6\u5916\u4e00\u79cd\u9014\u5f84\uff1a\u5141\u8bb8\u6211\u4eec\u5728\u7a0b\u5e8f\u4e2d\u521b\u5efa\u5168\u65b0\u7684\u81ea\u5b9a\u4e49\u7c7b\u578b\uff01\u8fd9\u6837\u7684\u7c7b\u578b\u901a\u5e38\u79f0\u4e3a\u7528\u6237\u5b9a\u4e49\u7c7b\u578b(\u5c3d\u7ba1\u6211\u4eec\u8ba4\u4e3a\u672f\u8bed\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u66f4\u597d\u2014\u2014\u6211\u4eec\u5c06\u5728\u672c\u8bfe\u540e\u9762\u8ba8\u8bba\u5176\u533a\u522b)\u3002C++\u6709\u4e24\u7c7b\u590d\u5408\u7c7b\u578b\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a\u679a\u4e3e\u7c7b\u578b(\u5305\u62ec \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u548c \u6709\u4f5c\u7528\u57df\u679a\u4e3e )\u548c\u7c7b\u7c7b\u578b(\u5305\u62ec\u7ed3\u6784\u4f53\u3001\u7c7b\u548c\u8054\u5408\u4f53)\u3002 \u5b9a\u4e49\u4e00\u4e2a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b \u5c31\u50cf \u7c7b\u578b\u522b\u540d \u4e00\u6837\uff0c\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u4e5f\u5fc5\u987b\u5148\u5b9a\u4e49\u540e\u4f7f\u7528\u3002\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7684\u5b9a\u4e49\u79f0\u4e3a\u7c7b\u578b\u5b9a\u4e49\u3002 \u5c3d\u7ba1\u7ed3\u6784\u4f53\u8fd8\u5c1a\u672a\u4ecb\u7ecd\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u5148\u4ece\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u4e86\u89e3\u5230\uff0c\u81ea\u5b9a\u4e49\u7c7b\u578b Fraction \u548c\u8be5\u7c7b\u578b\u5bf9\u8c61\u662f\u5982\u4f55\u521d\u59cb\u5316\u7684\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // Define a program-defined type named Fraction so the compiler understands what a Fraction is // (we'll explain what a struct is and how to use them later in this chapter) // This only defines what a Fraction type looks like, it doesn't create one struct Fraction { int numerator {}; int denominator {}; }; // Now we can make use of our Fraction type int main () { Fraction f { 3 , 4 }; // this actually instantiates a Fraction object named f return 0 ; } \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 struct \u5173\u952e\u5b57\u6765\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a Fraction \u7684\u65b0\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b(\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\uff0c\u56e0\u6b64\u5b83\u53ef\u4ee5\u5728\u6587\u4ef6\u7684\u5176\u4ed6\u5730\u65b9\u4f7f\u7528)\u3002\u8fd9\u4e0d\u4f1a\u5206\u914d\u4efb\u4f55\u5185\u5b58\u2014\u2014\u5b83\u53ea\u662f\u544a\u8bc9\u7f16\u8bd1\u5668 Fraction \u662f\u4ec0\u4e48\u6837\u5b50\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u7a0d\u540e\u518d\u4e3a Fraction \u7c7b\u578b\u7684\u5bf9\u8c61\u5206\u914d\u5185\u5b58\u3002\u7136\u540e\uff0c\u5728 main() \u5185\u90e8\uff0c\u5b9e\u4f8b\u5316(\u5e76\u521d\u59cb\u5316)\u4e00\u4e2a\u540d\u4e3a f \u7684 Fraction \u7c7b\u578b\u53d8\u91cf\u3002 \u7c7b\u578b\u5b9a\u4e49\u603b\u662f\u4ee5\u5206\u53f7\u7ed3\u5c3e\u3002\u6ca1\u6709\u5728\u7c7b\u578b\u5b9a\u4e49\u7684\u672b\u5c3e\u5305\u542b\u5206\u53f7\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u7a0b\u5e8f\u5458\u9519\u8bef\uff0c\u800c\u4e14\u8fd9\u4e2a\u9519\u8bef\u5f88\u96be\u8c03\u8bd5\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u5728\u7c7b\u578b\u5b9a\u4e49\u540e\u9762\u7684\u884c\u62a5\u9519\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u4ece\u4e0a\u9762\u4f8b\u5b50\u7684 Fraction \u5b9a\u4e49(\u7b2c8\u884c)\u672b\u5c3e\u5220\u9664\u5206\u53f7\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u56e0\u4e3a main() \u7684\u5b9a\u4e49(\u7b2c11\u884c)\u62a5\u9519\u3002 \u6ce8\u610f \u4e0d\u8981\u5fd8\u8bb0\u4ee5\u5206\u53f7\u7ed3\u675f\u7c7b\u578b\u5b9a\u4e49\uff0c\u5426\u5219\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u5728\u4e0b\u4e00\u884c\u4ee3\u7801\u4e2d\u51fa\u9519\u3002 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe( 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u4e2d\u5c55\u793a\u66f4\u591a\u5b9a\u4e49\u548c\u4f7f\u7528\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7684\u4f8b\u5b50\uff0c\u5e76\u4e14\u6211\u4eec\u5c06\u4ece 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 \u5f00\u59cb\u4ecb\u7ecd\u7ed3\u6784\u4f53\u3002 \u547d\u540d\u4e00\u4e2a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b \u6309\u7167\u60ef\u4f8b\uff0c\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u540d\u5b57\u5e94\u8be5\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff0c\u800c\u4e14\u4e0d\u8981\u6dfb\u52a0\u4efb\u4f55\u7684\u540e\u7f00(\u4f8b\u5982\uff1a Fraction \uff0c\u800c\u4e0d\u662f fraction \uff0c fraction_t \u6216\u8005 Fraction_t )\u3002 \u6700\u4f73\u5b9e\u8df5 \u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u59cb\u547d\u540d\u81ea\u5b9a\u4e49\u7c7b\u578b\uff0c\u4e0d\u8981\u4f7f\u7528\u540e\u7f00\u3002 \u7531\u4e8e\u7c7b\u578b\u540d\u548c\u53d8\u91cf\u540d\u4e4b\u95f4\u7684\u76f8\u4f3c\u6027\uff0c\u65b0\u624b\u7a0b\u5e8f\u5458\u65f6\u5e38\u4f1a\u89c9\u5f97\u4e0b\u9762\u8fd9\u6837\u7684\u53d8\u91cf\u5b9a\u4e49\u4ee4\u4eba\u56f0\u60d1: 1 Fraction fraction {}; // \u5b9e\u4f8b\u5316\u4e00\u4e2a\u540d\u4e3a fraction \u7684 Fraction \u7c7b\u578b\u7684\u5bf9\u8c61 \u4f46\u8fd9\u4e0e\u5176\u4ed6\u7c7b\u578b\u7684\u53d8\u91cf\u5b9a\u4e49\u5e76\u6ca1\u6709\u4ec0\u4e48\u533a\u522b\uff1a\u7c7b\u578b( Fraction )\u5728\u524d(\u56e0\u4e3a Fraction \u662f\u5927\u5199\u7684\uff0c\u6211\u4eec\u77e5\u9053\u5b83\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7c7b\u578b)\uff0c\u7136\u540e\u662f\u53d8\u91cf\u540d( fraction )\uff0c\u7136\u540e\u662f\u4e00\u4e2a\u53ef\u9009\u7684\u521d\u59cb\u5316\u503c\u3002\u56e0\u4e3aC++\u662f\u533a\u5206\u5927\u5c0f\u5199\u7684\uff0c\u6240\u4ee5\u8fd9\u91cc\u4e0d\u5b58\u5728\u547d\u540d\u51b2\u7a81! \u5728\u591a\u6587\u4ef6\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b \u6bcf\u4e2a\u4f7f\u7528\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7684\u4ee3\u7801\u6587\u4ef6\u5728\u4f7f\u7528\u4e4b\u524d\u90fd\u9700\u8981\u770b\u5230\u5b8c\u6574\u7684\u7c7b\u578b\u5b9a\u4e49\u3002 \u524d\u5411\u58f0\u660e \u662f\u4e0d\u591f\u7684\u3002\u770b\u5230\u5b8c\u6574\u5b9a\u4e49\u662f\u5fc5\u9700\u7684\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u9700\u8981\u77e5\u9053\u8981\u4e3a\u8be5\u7c7b\u578b\u7684\u5bf9\u8c61\u5206\u914d\u591a\u5c11\u5185\u5b58\u3002 \u4e3a\u4e86\u80fd\u5c06\u5b9a\u4e49\u5f15\u5165\u9700\u8981\u4f7f\u7528\u8be5\u7c7b\u578b\u5b9a\u4e49\u7684\u6587\u4ef6\uff0c\u81ea\u5b9a\u4e49\u7c7b\u578b\u901a\u5e38\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u4f7f\u7528 #included \u5bfc\u5165\u4efb\u4f55\u9700\u8981\u8be5\u5b9a\u4e49\u7684\u6587\u4ef6\u3002\u8fd9\u4e9b\u5934\u6587\u4ef6\u901a\u5e38\u548c\u7c7b\u578b\u5177\u6709\u76f8\u540c\u7684\u540d\u5b57\uff08\u4f8b\u5982 Fraction \u5e94\u8be5\u5b9a\u4e49\u5728 Fraction.h \u4e2d\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u53ea\u5728\u4e00\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\u4f7f\u7528\u7684\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u5e94\u8be5\u5728\u8be5\u4ee3\u7801\u6587\u4ef6\u4e2d\u5c3d\u53ef\u80fd\u9760\u8fd1\u7b2c\u4e00\u4e2a\u4f7f\u7528\u70b9\u5b9a\u4e49\u3002 \u5728\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\u4f7f\u7528\u7684\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u5e94\u8be5\u5728\u4e0e\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u540c\u540d\u7684\u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\uff0c\u7136\u540e\u6839\u636e\u9700\u8981\u5728\u4ee3\u7801\u6587\u4ef6\u4e2d\u4f7f\u7528 #include \u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5982\u679c\u6211\u4eec\u628a\u6211\u4eec\u7684 Fraction \u7c7b\u578b\u79fb\u52a8\u5230\u4e00\u4e2a\u5934\u6587\u4ef6(\u540d\u4e3a Fraction.h )\uff0c\u8fd9\u6837\u5b83\u5c31\u53ef\u4ee5\u5305\u542b\u5728\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d: Fraction.h 1 2 3 4 5 6 7 8 9 10 11 12 13 #ifndef FRACTION_H #define FRACTION_H // Define a new type named Fraction // This only defines what a Fraction looks like, it doesn't create one // Note that this is a full definition, not a forward declaration struct Fraction { int numerator {}; int denominator {}; }; #endif Fraction.cpp 1 2 3 4 5 6 7 8 9 #include \"Fraction.h\" // include our Fraction definition in this code file // Now we can make use of our Fraction type int main () { Fraction f { 3 , 4 }; // this actually creates a Fraction object named f return 0 ; } \u7c7b\u578b\u5b9a\u4e49\u90e8\u5206\u4e0a\u8c41\u514d\u4e8e\u5355\u4e00\u5b9a\u4e49\u539f\u5219 \u5728\u8bfe\u7a0b 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 \u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \uff0c\u8be5\u89c4\u5219\u8981\u6c42\u6bcf\u4e2a\u51fd\u6570\u548c\u5168\u5c40\u53d8\u91cf\u5728 \u6bcf\u4e2a\u7a0b\u5e8f \u4e2d\u53ea\u6709\u4e00\u4e2a\u5b9a\u4e49\u3002\u8981\u5728\u4e0d\u5305\u542b\u5b9a\u4e49\u7684\u6587\u4ef6\u4e2d\u4f7f\u7528\u7ed9\u5b9a\u51fd\u6570\u6216\u5168\u5c40\u53d8\u91cf\uff0c\u9700\u8981 \u524d\u5411\u58f0\u660e (\u901a\u5e38\u901a\u8fc7\u5934\u6587\u4ef6\u5b9e\u73b0)\u3002\u8fd9\u662f\u53ef\u884c\u7684\uff0c\u56e0\u4e3a\u5f53\u6d89\u53ca\u5230\u51fd\u6570\u548c\u975econstexpr\u53d8\u91cf\u65f6\uff0c\u7f16\u8bd1\u5668\u53ea\u9700\u8981\u58f0\u660e\u5c31\u53ef\u4ee5 \uff0c\u7136\u540e\u94fe\u63a5\u5668\u4f1a\u5c06\u6240\u9700\u7684\u4e1c\u897f\u90fd\u94fe\u63a5\u8d77\u6765\u3002 \u4f46\u662f\uff0c\u8fd9\u4e2a\u505a\u6cd5\u5e76\u4e0d\u9002\u7528\u4e8e\u7c7b\u578b\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u901a\u5e38\u9700\u8981\u770b\u5230\u5b8c\u6574\u7684\u7c7b\u578b\u5b9a\u4e49\u624d\u80fd\u4f7f\u7528\u7ed9\u5b9a\u7684\u7c7b\u578b\u3002\u6211\u4eec\u5fc5\u987b\u80fd\u591f\u5c06\u5b8c\u6574\u7684\u7c7b\u578b\u5b9a\u4e49\u5bfc\u5165\u5230\u6bcf\u4e2a\u9700\u8981\u5b83\u7684\u4ee3\u7801\u6587\u4ef6\u3002 \u4e3a\u6b64\uff0c \u7c7b\u578b\u5b9a\u4e49\u5728\u67d0\u79cd\u7a0b\u5ea6\u4e0a\u662f\u4e0d\u53d7\u9650\u4e8e\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u7684\uff1a\u7c7b\u578b\u7684\u5b9a\u4e49\u53ef\u4ee5\u51fa\u73b0\u5728\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\u3002 \u5176\u5b9e\u4f60\u5df2\u7ecf\u5728\u4e0d\u7ecf\u610f\u4e4b\u95f4\u8fd9\u4e48\u505a\u4e86\uff1a\u5982\u679c\u7a0b\u5e8f\u6709\u4e24\u4e2a\u4ee3\u7801\u6587\u4ef6\uff0c\u4e14\u90fd #include \uff0c\u90a3\u4e48\u6211\u4eec\u5b9e\u9645\u4e0a\u5c06\u6240\u6709\u8f93\u5165/\u8f93\u51fa\u7c7b\u578b\u5b9a\u4e49\u90fd\u5bfc\u5165\u5230\u4e86\u8fd9\u4e24\u4e2a\u6587\u4ef6\u4e2d\u3002 \u6709\u4e24\u70b9\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0c\u7c7b\u578b\u5b9a\u4e49\u5728\u540c\u4e00\u4e2a\u6587\u4ef6\u4e2d\u53ea\u80fd\u51fa\u73b0\u4e00\u6b21(\u8fd9\u901a\u5e38\u4e0d\u662f\u95ee\u9898\uff0c\u56e0\u4e3a \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e \u53ef\u4ee5\u9632\u6b62\u8be5\u95ee\u9898\u51fa\u73b0)\u3002\u5176\u6b21\uff0c\u7ed9\u5b9a\u7c7b\u578b\u7684\u6240\u6709\u7c7b\u578b\u5b9a\u4e49\u5fc5\u987b\u76f8\u540c\uff0c\u5426\u5219\u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u547d\u540d\u6cd5\uff1a\u7528\u6237\u5b9a\u4e49\u7c7b\u578b vs \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b \u672f\u8bed\u201c\u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u201d\uff08\u81ea\u5b9a\u4e49\u7c7b\u578b\uff09\u6709\u65f6\u4f1a\u51fa\u73b0\u5728\u65e5\u5e38\u5bf9\u8bdd\u4e2d\uff0c\u4e5f\u4f1a\u5728C++\u8bed\u8a00\u6807\u51c6\u4e2d\u63d0\u5230(\u4f46\u6ca1\u6709\u5b9a\u4e49)\u3002\u5728\u975e\u6b63\u5f0f\u7684\u5bf9\u8bdd\u4e2d\uff0c\u8fd9\u4e2a\u672f\u8bed\u5f80\u5f80\u610f\u5473\u7740\u201c\u4f60\u81ea\u5df1\u5b9a\u4e49\u7684\u7c7b\u578b\u201d(\u5982\u4e0a\u9762\u7684\u5206\u6570\u7c7b\u578b\u4f8b\u5b50)\u3002 \u6709\u65f6\u8fd9\u4e5f\u5305\u62ec\u7c7b\u578b\u522b\u540d \u3002 \u7136\u800c\uff0c\u5728C++\u8bed\u8a00\u6807\u51c6\u7684\u884c\u6587\u8bed\u5883\u4e2d\uff0c\u4efb\u4f55\u6ca1\u6709\u5b9a\u4e49\u4e3a\u6838\u5fc3C++\u8bed\u8a00\u4e00\u90e8\u5206\u7684\u7c7b\u578b(\u6362\u53e5\u8bdd\u8bf4\uff0c\u975e\u57fa\u672c\u7c7b\u578b)\u90fd\u662f\u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u3002\u56e0\u6b64\uff0c\u5728C++\u6807\u51c6\u5e93\u4e2d\u5b9a\u4e49\u7684\u7c7b\u578b(\u4f8b\u5982 std::string )\u7406\u8bba\u4e0a\u4e5f\u7b97\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u5c31\u50cf\u4f60\u81ea\u5df1\u5b9a\u4e49\u7684\u4efb\u4f55\u7c7b\u578b\u4e00\u6837\u3002 \u4e3a\u4e86\u533a\u522b\uff0cC++ 20\u8bed\u8a00\u6807\u51c6\u5c06\u672f\u8bed\u201c\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u201d\u5b9a\u4e49\u4e3a\u4ec5\u6307\u4ee3\u4f60\u81ea\u5df1\u5b9a\u4e49\u7684\u7c7b\u578b\u3002\u5f53\u6211\u4eec\u8ba8\u8bba\u8fd9\u4e9b\u7c7b\u578b\u65f6\uff0c\u66f4\u559c\u6b22\u4f7f\u7528\u8fd9\u4e2a\u672f\u8bed\uff0c\u56e0\u4e3a\u5b83\u66f4\u52a0\u7cbe\u786e\u3002 \u7c7b\u578b \u542b\u4e49 \u4f8b\u5b50 \u57fa\u672c\u7c7b\u578b \u5185\u5efa\u4e8e C++ \u8bed\u8a00\u7684\u6838\u5fc3\u90e8\u5206 int , std::nullptr_t \u7528\u6237\u5b9a\u4e49\u7c7b\u578b \u975e\u57fa\u7840\u6570\u636e\u7c7b\u578b(\u975e\u6b63\u5f0f\u573a\u5408\u6307\u4ee3\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b) std::string , Fraction \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b \u7528\u6237\u81ea\u5b9a\u4e49\u7684\u7c7b\u6216\u679a\u4e3e\u7c7b\u578b Fraction","title":"10.1 - \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7b80\u4ecb"},{"location":"10-1-Introduction-to-program-defined-user-defined-types/#_1","text":"\u5728 9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b \u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u66fe\u7ecf\u4ecb\u7ecd\u8fc7\u5982\u4f55\u5b58\u50a8\u4e00\u4e2a\u5206\u6570\u3002\u5bf9\u4e8e\u5206\u6570\u6765\u8bf4\uff0c\u5b83\u5305\u62ec\u5206\u5b50\u548c\u5206\u6bcd\u4e24\u4e2a\u90e8\u5206\uff0c\u800c\u4e14\u5b83\u4eec\u4e4b\u95f4\u662f\u5b58\u5728\u6982\u5ff5\u6027\u8054\u7cfb\u7684\u3002\u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba\u4f7f\u7528\u4e24\u4e2a\u6574\u578b\u5206\u522b\u8868\u793a\u5206\u5b50\u5206\u6bcd\u53ef\u80fd\u5e26\u6765\u7684\u95ee\u9898\u3002 \u5982\u679cC++\u6709\u80fd\u591f\u8868\u793a\u5206\u6570\u7684\u5185\u5efa\u7c7b\u578b\u5c31\u597d\u4e86\u2014\u2014\u4f46\u53ef\u60dc\u5e76\u6ca1\u6709\u3002\u8fd8\u6709\u6570\u767e\u79cd\u5176\u4ed6\u53ef\u80fd\u6709\u7528\u7684\u7c7b\u578bC++\u6ca1\u6709\u5305\u542b\uff0c\u56e0\u4e3a\u5b83\u4e0d\u53ef\u80fd\u9884\u6d4b\u4eba\u4eec\u53ef\u80fd\u9700\u8981\u7684\u6240\u6709\u4e1c\u897f(\u66f4\u4e0d\u7528\u8bf4\u5b9e\u73b0\u548c\u6d4b\u8bd5\u90a3\u4e9b\u4e1c\u897f\u4e86)\u3002 \u76f8\u53cd\uff0cC++\u63d0\u4f9b\u4e86\u89e3\u51b3\u8be5\u95ee\u9898\u7684\u53e6\u5916\u4e00\u79cd\u9014\u5f84\uff1a\u5141\u8bb8\u6211\u4eec\u5728\u7a0b\u5e8f\u4e2d\u521b\u5efa\u5168\u65b0\u7684\u81ea\u5b9a\u4e49\u7c7b\u578b\uff01\u8fd9\u6837\u7684\u7c7b\u578b\u901a\u5e38\u79f0\u4e3a\u7528\u6237\u5b9a\u4e49\u7c7b\u578b(\u5c3d\u7ba1\u6211\u4eec\u8ba4\u4e3a\u672f\u8bed\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u66f4\u597d\u2014\u2014\u6211\u4eec\u5c06\u5728\u672c\u8bfe\u540e\u9762\u8ba8\u8bba\u5176\u533a\u522b)\u3002C++\u6709\u4e24\u7c7b\u590d\u5408\u7c7b\u578b\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a\u679a\u4e3e\u7c7b\u578b(\u5305\u62ec \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u548c \u6709\u4f5c\u7528\u57df\u679a\u4e3e )\u548c\u7c7b\u7c7b\u578b(\u5305\u62ec\u7ed3\u6784\u4f53\u3001\u7c7b\u548c\u8054\u5408\u4f53)\u3002","title":"\u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u662f\u4ec0\u4e48\uff1f"},{"location":"10-1-Introduction-to-program-defined-user-defined-types/#_2","text":"\u5c31\u50cf \u7c7b\u578b\u522b\u540d \u4e00\u6837\uff0c\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u4e5f\u5fc5\u987b\u5148\u5b9a\u4e49\u540e\u4f7f\u7528\u3002\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7684\u5b9a\u4e49\u79f0\u4e3a\u7c7b\u578b\u5b9a\u4e49\u3002 \u5c3d\u7ba1\u7ed3\u6784\u4f53\u8fd8\u5c1a\u672a\u4ecb\u7ecd\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u5148\u4ece\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u4e86\u89e3\u5230\uff0c\u81ea\u5b9a\u4e49\u7c7b\u578b Fraction \u548c\u8be5\u7c7b\u578b\u5bf9\u8c61\u662f\u5982\u4f55\u521d\u59cb\u5316\u7684\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // Define a program-defined type named Fraction so the compiler understands what a Fraction is // (we'll explain what a struct is and how to use them later in this chapter) // This only defines what a Fraction type looks like, it doesn't create one struct Fraction { int numerator {}; int denominator {}; }; // Now we can make use of our Fraction type int main () { Fraction f { 3 , 4 }; // this actually instantiates a Fraction object named f return 0 ; } \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 struct \u5173\u952e\u5b57\u6765\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a Fraction \u7684\u65b0\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b(\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\uff0c\u56e0\u6b64\u5b83\u53ef\u4ee5\u5728\u6587\u4ef6\u7684\u5176\u4ed6\u5730\u65b9\u4f7f\u7528)\u3002\u8fd9\u4e0d\u4f1a\u5206\u914d\u4efb\u4f55\u5185\u5b58\u2014\u2014\u5b83\u53ea\u662f\u544a\u8bc9\u7f16\u8bd1\u5668 Fraction \u662f\u4ec0\u4e48\u6837\u5b50\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u7a0d\u540e\u518d\u4e3a Fraction \u7c7b\u578b\u7684\u5bf9\u8c61\u5206\u914d\u5185\u5b58\u3002\u7136\u540e\uff0c\u5728 main() \u5185\u90e8\uff0c\u5b9e\u4f8b\u5316(\u5e76\u521d\u59cb\u5316)\u4e00\u4e2a\u540d\u4e3a f \u7684 Fraction \u7c7b\u578b\u53d8\u91cf\u3002 \u7c7b\u578b\u5b9a\u4e49\u603b\u662f\u4ee5\u5206\u53f7\u7ed3\u5c3e\u3002\u6ca1\u6709\u5728\u7c7b\u578b\u5b9a\u4e49\u7684\u672b\u5c3e\u5305\u542b\u5206\u53f7\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u7a0b\u5e8f\u5458\u9519\u8bef\uff0c\u800c\u4e14\u8fd9\u4e2a\u9519\u8bef\u5f88\u96be\u8c03\u8bd5\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u5728\u7c7b\u578b\u5b9a\u4e49\u540e\u9762\u7684\u884c\u62a5\u9519\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u4ece\u4e0a\u9762\u4f8b\u5b50\u7684 Fraction \u5b9a\u4e49(\u7b2c8\u884c)\u672b\u5c3e\u5220\u9664\u5206\u53f7\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u56e0\u4e3a main() \u7684\u5b9a\u4e49(\u7b2c11\u884c)\u62a5\u9519\u3002 \u6ce8\u610f \u4e0d\u8981\u5fd8\u8bb0\u4ee5\u5206\u53f7\u7ed3\u675f\u7c7b\u578b\u5b9a\u4e49\uff0c\u5426\u5219\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u5728\u4e0b\u4e00\u884c\u4ee3\u7801\u4e2d\u51fa\u9519\u3002 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe( 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u4e2d\u5c55\u793a\u66f4\u591a\u5b9a\u4e49\u548c\u4f7f\u7528\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7684\u4f8b\u5b50\uff0c\u5e76\u4e14\u6211\u4eec\u5c06\u4ece 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 \u5f00\u59cb\u4ecb\u7ecd\u7ed3\u6784\u4f53\u3002","title":"\u5b9a\u4e49\u4e00\u4e2a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b"},{"location":"10-1-Introduction-to-program-defined-user-defined-types/#_3","text":"\u6309\u7167\u60ef\u4f8b\uff0c\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u540d\u5b57\u5e94\u8be5\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff0c\u800c\u4e14\u4e0d\u8981\u6dfb\u52a0\u4efb\u4f55\u7684\u540e\u7f00(\u4f8b\u5982\uff1a Fraction \uff0c\u800c\u4e0d\u662f fraction \uff0c fraction_t \u6216\u8005 Fraction_t )\u3002 \u6700\u4f73\u5b9e\u8df5 \u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u59cb\u547d\u540d\u81ea\u5b9a\u4e49\u7c7b\u578b\uff0c\u4e0d\u8981\u4f7f\u7528\u540e\u7f00\u3002 \u7531\u4e8e\u7c7b\u578b\u540d\u548c\u53d8\u91cf\u540d\u4e4b\u95f4\u7684\u76f8\u4f3c\u6027\uff0c\u65b0\u624b\u7a0b\u5e8f\u5458\u65f6\u5e38\u4f1a\u89c9\u5f97\u4e0b\u9762\u8fd9\u6837\u7684\u53d8\u91cf\u5b9a\u4e49\u4ee4\u4eba\u56f0\u60d1: 1 Fraction fraction {}; // \u5b9e\u4f8b\u5316\u4e00\u4e2a\u540d\u4e3a fraction \u7684 Fraction \u7c7b\u578b\u7684\u5bf9\u8c61 \u4f46\u8fd9\u4e0e\u5176\u4ed6\u7c7b\u578b\u7684\u53d8\u91cf\u5b9a\u4e49\u5e76\u6ca1\u6709\u4ec0\u4e48\u533a\u522b\uff1a\u7c7b\u578b( Fraction )\u5728\u524d(\u56e0\u4e3a Fraction \u662f\u5927\u5199\u7684\uff0c\u6211\u4eec\u77e5\u9053\u5b83\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7c7b\u578b)\uff0c\u7136\u540e\u662f\u53d8\u91cf\u540d( fraction )\uff0c\u7136\u540e\u662f\u4e00\u4e2a\u53ef\u9009\u7684\u521d\u59cb\u5316\u503c\u3002\u56e0\u4e3aC++\u662f\u533a\u5206\u5927\u5c0f\u5199\u7684\uff0c\u6240\u4ee5\u8fd9\u91cc\u4e0d\u5b58\u5728\u547d\u540d\u51b2\u7a81!","title":"\u547d\u540d\u4e00\u4e2a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b"},{"location":"10-1-Introduction-to-program-defined-user-defined-types/#_4","text":"\u6bcf\u4e2a\u4f7f\u7528\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7684\u4ee3\u7801\u6587\u4ef6\u5728\u4f7f\u7528\u4e4b\u524d\u90fd\u9700\u8981\u770b\u5230\u5b8c\u6574\u7684\u7c7b\u578b\u5b9a\u4e49\u3002 \u524d\u5411\u58f0\u660e \u662f\u4e0d\u591f\u7684\u3002\u770b\u5230\u5b8c\u6574\u5b9a\u4e49\u662f\u5fc5\u9700\u7684\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u9700\u8981\u77e5\u9053\u8981\u4e3a\u8be5\u7c7b\u578b\u7684\u5bf9\u8c61\u5206\u914d\u591a\u5c11\u5185\u5b58\u3002 \u4e3a\u4e86\u80fd\u5c06\u5b9a\u4e49\u5f15\u5165\u9700\u8981\u4f7f\u7528\u8be5\u7c7b\u578b\u5b9a\u4e49\u7684\u6587\u4ef6\uff0c\u81ea\u5b9a\u4e49\u7c7b\u578b\u901a\u5e38\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u4f7f\u7528 #included \u5bfc\u5165\u4efb\u4f55\u9700\u8981\u8be5\u5b9a\u4e49\u7684\u6587\u4ef6\u3002\u8fd9\u4e9b\u5934\u6587\u4ef6\u901a\u5e38\u548c\u7c7b\u578b\u5177\u6709\u76f8\u540c\u7684\u540d\u5b57\uff08\u4f8b\u5982 Fraction \u5e94\u8be5\u5b9a\u4e49\u5728 Fraction.h \u4e2d\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u53ea\u5728\u4e00\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\u4f7f\u7528\u7684\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u5e94\u8be5\u5728\u8be5\u4ee3\u7801\u6587\u4ef6\u4e2d\u5c3d\u53ef\u80fd\u9760\u8fd1\u7b2c\u4e00\u4e2a\u4f7f\u7528\u70b9\u5b9a\u4e49\u3002 \u5728\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\u4f7f\u7528\u7684\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u5e94\u8be5\u5728\u4e0e\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u540c\u540d\u7684\u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\uff0c\u7136\u540e\u6839\u636e\u9700\u8981\u5728\u4ee3\u7801\u6587\u4ef6\u4e2d\u4f7f\u7528 #include \u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5982\u679c\u6211\u4eec\u628a\u6211\u4eec\u7684 Fraction \u7c7b\u578b\u79fb\u52a8\u5230\u4e00\u4e2a\u5934\u6587\u4ef6(\u540d\u4e3a Fraction.h )\uff0c\u8fd9\u6837\u5b83\u5c31\u53ef\u4ee5\u5305\u542b\u5728\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d: Fraction.h 1 2 3 4 5 6 7 8 9 10 11 12 13 #ifndef FRACTION_H #define FRACTION_H // Define a new type named Fraction // This only defines what a Fraction looks like, it doesn't create one // Note that this is a full definition, not a forward declaration struct Fraction { int numerator {}; int denominator {}; }; #endif Fraction.cpp 1 2 3 4 5 6 7 8 9 #include \"Fraction.h\" // include our Fraction definition in this code file // Now we can make use of our Fraction type int main () { Fraction f { 3 , 4 }; // this actually creates a Fraction object named f return 0 ; }","title":"\u5728\u591a\u6587\u4ef6\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b"},{"location":"10-1-Introduction-to-program-defined-user-defined-types/#_5","text":"\u5728\u8bfe\u7a0b 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 \u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \uff0c\u8be5\u89c4\u5219\u8981\u6c42\u6bcf\u4e2a\u51fd\u6570\u548c\u5168\u5c40\u53d8\u91cf\u5728 \u6bcf\u4e2a\u7a0b\u5e8f \u4e2d\u53ea\u6709\u4e00\u4e2a\u5b9a\u4e49\u3002\u8981\u5728\u4e0d\u5305\u542b\u5b9a\u4e49\u7684\u6587\u4ef6\u4e2d\u4f7f\u7528\u7ed9\u5b9a\u51fd\u6570\u6216\u5168\u5c40\u53d8\u91cf\uff0c\u9700\u8981 \u524d\u5411\u58f0\u660e (\u901a\u5e38\u901a\u8fc7\u5934\u6587\u4ef6\u5b9e\u73b0)\u3002\u8fd9\u662f\u53ef\u884c\u7684\uff0c\u56e0\u4e3a\u5f53\u6d89\u53ca\u5230\u51fd\u6570\u548c\u975econstexpr\u53d8\u91cf\u65f6\uff0c\u7f16\u8bd1\u5668\u53ea\u9700\u8981\u58f0\u660e\u5c31\u53ef\u4ee5 \uff0c\u7136\u540e\u94fe\u63a5\u5668\u4f1a\u5c06\u6240\u9700\u7684\u4e1c\u897f\u90fd\u94fe\u63a5\u8d77\u6765\u3002 \u4f46\u662f\uff0c\u8fd9\u4e2a\u505a\u6cd5\u5e76\u4e0d\u9002\u7528\u4e8e\u7c7b\u578b\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u901a\u5e38\u9700\u8981\u770b\u5230\u5b8c\u6574\u7684\u7c7b\u578b\u5b9a\u4e49\u624d\u80fd\u4f7f\u7528\u7ed9\u5b9a\u7684\u7c7b\u578b\u3002\u6211\u4eec\u5fc5\u987b\u80fd\u591f\u5c06\u5b8c\u6574\u7684\u7c7b\u578b\u5b9a\u4e49\u5bfc\u5165\u5230\u6bcf\u4e2a\u9700\u8981\u5b83\u7684\u4ee3\u7801\u6587\u4ef6\u3002 \u4e3a\u6b64\uff0c \u7c7b\u578b\u5b9a\u4e49\u5728\u67d0\u79cd\u7a0b\u5ea6\u4e0a\u662f\u4e0d\u53d7\u9650\u4e8e\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u7684\uff1a\u7c7b\u578b\u7684\u5b9a\u4e49\u53ef\u4ee5\u51fa\u73b0\u5728\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\u3002 \u5176\u5b9e\u4f60\u5df2\u7ecf\u5728\u4e0d\u7ecf\u610f\u4e4b\u95f4\u8fd9\u4e48\u505a\u4e86\uff1a\u5982\u679c\u7a0b\u5e8f\u6709\u4e24\u4e2a\u4ee3\u7801\u6587\u4ef6\uff0c\u4e14\u90fd #include \uff0c\u90a3\u4e48\u6211\u4eec\u5b9e\u9645\u4e0a\u5c06\u6240\u6709\u8f93\u5165/\u8f93\u51fa\u7c7b\u578b\u5b9a\u4e49\u90fd\u5bfc\u5165\u5230\u4e86\u8fd9\u4e24\u4e2a\u6587\u4ef6\u4e2d\u3002 \u6709\u4e24\u70b9\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0c\u7c7b\u578b\u5b9a\u4e49\u5728\u540c\u4e00\u4e2a\u6587\u4ef6\u4e2d\u53ea\u80fd\u51fa\u73b0\u4e00\u6b21(\u8fd9\u901a\u5e38\u4e0d\u662f\u95ee\u9898\uff0c\u56e0\u4e3a \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e \u53ef\u4ee5\u9632\u6b62\u8be5\u95ee\u9898\u51fa\u73b0)\u3002\u5176\u6b21\uff0c\u7ed9\u5b9a\u7c7b\u578b\u7684\u6240\u6709\u7c7b\u578b\u5b9a\u4e49\u5fc5\u987b\u76f8\u540c\uff0c\u5426\u5219\u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002","title":"\u7c7b\u578b\u5b9a\u4e49\u90e8\u5206\u4e0a\u8c41\u514d\u4e8e\u5355\u4e00\u5b9a\u4e49\u539f\u5219"},{"location":"10-1-Introduction-to-program-defined-user-defined-types/#vs","text":"\u672f\u8bed\u201c\u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u201d\uff08\u81ea\u5b9a\u4e49\u7c7b\u578b\uff09\u6709\u65f6\u4f1a\u51fa\u73b0\u5728\u65e5\u5e38\u5bf9\u8bdd\u4e2d\uff0c\u4e5f\u4f1a\u5728C++\u8bed\u8a00\u6807\u51c6\u4e2d\u63d0\u5230(\u4f46\u6ca1\u6709\u5b9a\u4e49)\u3002\u5728\u975e\u6b63\u5f0f\u7684\u5bf9\u8bdd\u4e2d\uff0c\u8fd9\u4e2a\u672f\u8bed\u5f80\u5f80\u610f\u5473\u7740\u201c\u4f60\u81ea\u5df1\u5b9a\u4e49\u7684\u7c7b\u578b\u201d(\u5982\u4e0a\u9762\u7684\u5206\u6570\u7c7b\u578b\u4f8b\u5b50)\u3002 \u6709\u65f6\u8fd9\u4e5f\u5305\u62ec\u7c7b\u578b\u522b\u540d \u3002 \u7136\u800c\uff0c\u5728C++\u8bed\u8a00\u6807\u51c6\u7684\u884c\u6587\u8bed\u5883\u4e2d\uff0c\u4efb\u4f55\u6ca1\u6709\u5b9a\u4e49\u4e3a\u6838\u5fc3C++\u8bed\u8a00\u4e00\u90e8\u5206\u7684\u7c7b\u578b(\u6362\u53e5\u8bdd\u8bf4\uff0c\u975e\u57fa\u672c\u7c7b\u578b)\u90fd\u662f\u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u3002\u56e0\u6b64\uff0c\u5728C++\u6807\u51c6\u5e93\u4e2d\u5b9a\u4e49\u7684\u7c7b\u578b(\u4f8b\u5982 std::string )\u7406\u8bba\u4e0a\u4e5f\u7b97\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u5c31\u50cf\u4f60\u81ea\u5df1\u5b9a\u4e49\u7684\u4efb\u4f55\u7c7b\u578b\u4e00\u6837\u3002 \u4e3a\u4e86\u533a\u522b\uff0cC++ 20\u8bed\u8a00\u6807\u51c6\u5c06\u672f\u8bed\u201c\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u201d\u5b9a\u4e49\u4e3a\u4ec5\u6307\u4ee3\u4f60\u81ea\u5df1\u5b9a\u4e49\u7684\u7c7b\u578b\u3002\u5f53\u6211\u4eec\u8ba8\u8bba\u8fd9\u4e9b\u7c7b\u578b\u65f6\uff0c\u66f4\u559c\u6b22\u4f7f\u7528\u8fd9\u4e2a\u672f\u8bed\uff0c\u56e0\u4e3a\u5b83\u66f4\u52a0\u7cbe\u786e\u3002 \u7c7b\u578b \u542b\u4e49 \u4f8b\u5b50 \u57fa\u672c\u7c7b\u578b \u5185\u5efa\u4e8e C++ \u8bed\u8a00\u7684\u6838\u5fc3\u90e8\u5206 int , std::nullptr_t \u7528\u6237\u5b9a\u4e49\u7c7b\u578b \u975e\u57fa\u7840\u6570\u636e\u7c7b\u578b(\u975e\u6b63\u5f0f\u573a\u5408\u6307\u4ee3\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b) std::string , Fraction \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b \u7528\u6237\u81ea\u5b9a\u4e49\u7684\u7c7b\u6216\u679a\u4e3e\u7c7b\u578b Fraction","title":"\u547d\u540d\u6cd5\uff1a\u7528\u6237\u5b9a\u4e49\u7c7b\u578b vs \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b"},{"location":"10-10-class-templates/","tags":["class template"],"text":"Key Takeaway \u5728 [[8-13-Function-templates|8.13 - \u51fd\u6570\u6a21\u677f] \u4e2d\u6211\u4eec\u63d0\u5230\uff0c\u4e3a\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u5206\u522b\u521b\u5efa\uff08\u91cd\u8f7d\uff09\u4e0d\u540c\u7684\u51fd\u6570\u662f\u4e00\u9879\u56f0\u96be\u4e14\u6ca1\u5fc5\u8981\u7684\u5de5\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include // function to calculate the greater of two int values int max ( int x , int y ) { return ( x > y ) ? x : y ; } // almost identical function to calculate the greater of two double values // the only difference is the type information double max ( double x , double y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max ( 5 , 6 ); // calls max(int, int) std :: cout << '\\n' ; std :: cout << max ( 1.2 , 3.4 ); // calls max(double, double) return 0 ; } \u8be5\u95ee\u9898\u7684\u6700\u7ec8\u89e3\u51b3\u65b9\u6848\u662f\u521b\u5efa\u4e00\u4e2a \u51fd\u6570\u6a21\u677f \u5e76\u8ba9\u7f16\u8bd1\u5668\u6839\u636e\u6240\u9700\u7684\u7c7b\u578b\u4e3a\u4f60\u5b9e\u4f8b\u5316\u5bf9\u5e94\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // a single function template for max template < typename T > T max ( T x , T y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max ( 5 , 6 ); // instantiates and calls max(int, int) std :: cout << '\\n' ; std :: cout << max ( 1.2 , 3.4 ); // instantiates and calls max(double, double) return 0 ; } \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u5728 8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316 \u4e2d\u4ecb\u7ecd\u4e86\u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316\u3002 \u805a\u5408\u7c7b\u578b\u4e5f\u9762\u4e34\u7c7b\u4f3c\u7684\u95ee\u9898 \u5bf9\u4e8e\u805a\u5408\u7c7b\u578b(\u5305\u62ec\u7ed3\u6784/\u7c7b/\u8054\u5408\u548c\u6570\u7ec4)\uff0c\u6211\u4eec\u4e5f\u9047\u5230\u4e86\u7c7b\u4f3c\u7684\u95ee\u9898\u3002 \u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5176\u4e2d\u9700\u8981\u5904\u7406\u4e00\u5bf9 int \u503c\uff0c\u5e76\u9700\u8981\u786e\u5b9a\u8fd9\u4e24\u4e2a\u6570\u5b57\u4e2d\u54ea\u4e00\u4e2a\u66f4\u5927\u3002\u6211\u4eec\u53ef\u4ee5\u5199\u8fd9\u6837\u4e00\u4e2a\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include struct Pair { int first {}; int second {}; }; constexpr int max ( Pair p ) // pass by value because Pair is small { return ( p . first > p . second ? p . first : p . second ); } int main () { Pair p1 { 5 , 6 }; std :: cout << max ( p1 ) << \" is larger \\n \" ; return 0 ; } \u4e00\u6bb5\u65f6\u95f4\u540e\uff0c\u6211\u4eec\u53d1\u73b0\u6211\u4eec\u9700\u8981\u5904\u7406\u4e00\u5bf9 double \u7c7b\u578b\u7684\u503c\uff0c\u90a3\u4e48\u6211\u4eec\u5fc5\u987b\u66f4\u65b0\u7a0b\u5e8f\uff0c\u6dfb\u52a0\u53e6\u5916\u4e00\u4e2a Pair \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include struct Pair { int first {}; int second {}; }; struct Pair // compile error: erroneous redefinition of Pair { double first {}; double second {}; }; constexpr int max ( Pair p ) { return ( p . first > p . second ? p . first : p . second ); } constexpr double max ( Pair p ) // compile error: overloaded function differs only by return type { return ( p . first > p . second ? p . first : p . second ); } int main () { Pair p1 { 5 , 6 }; std :: cout << max ( p1 ) << \" is larger \\n \" ; Pair p2 { 1.2 , 3.4 }; std :: cout << max ( p2 ) << \" is larger \\n \" ; return 0 ; } \u5f88\u9057\u61be\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u662f\u4e0d\u80fd\u7f16\u8bd1\u7684\uff0c\u800c\u4e14\u8fd9\u91cc\u6709\u5f88\u591a\u95ee\u9898\uff1a \u9996\u5148\uff0c\u548c\u51fd\u6570\u4e0d\u540c\uff0c\u7c7b\u578b\u662f\u4e0d\u80fd\u88ab \u91cd\u8f7d \u7684\u3002\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u5c06\u7b2c\u4e8c\u4e2a double \u6210\u5458\u7248\u672c\u7684 Pair \u770b\u505a\u662f\u7b2c\u4e00\u4e2a Pair \u7684\u91cd\u590d\u5b9a\u4e49\u3002\u7b2c\u4e8c\uff0c\u5c3d\u7ba1\u51fd\u6570\u53ef\u4ee5\u88ab\u91cd\u8f7d\uff0c\u4f46\u4e24\u4e2a max(Pair) \u51fd\u6570\u53ea\u6709\u8fd4\u56de\u503c\u4e0d\u540c\uff0c\u800c\u51fd\u6570\u91cd\u8f7d\u4e0d\u80fd\u4ec5\u4ec5\u901a\u8fc7\u4e0d\u540c\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u533a\u5206\u3002\u7b2c\u4e09\uff0c\u4ee3\u7801\u6709\u5f88\u591a\u5197\u4f59\u3002\u6bcf\u4e2a Pair \u7ed3\u6784\u4f53\u90fd\u662f\u76f8\u540c\u7684\uff08\u53ea\u662f\u6570\u636e\u7c7b\u578b\u4e0d\u540c\uff09\uff0c max(Pair) \u51fd\u6570\u4e5f\u662f (\u53ea\u6709\u8fd4\u56de\u503c\u4e0d\u540c)\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ed9 Pair \u8d77\u4e0d\u540c\u7684\u540d\u5b57\u6765\u89e3\u51b3\u524d\u4e24\u4e2a\u95ee\u9898\uff08\u4f8b\u5982 Pairint \u548c Pairdouble )\u3002\u4f46\u662f\u6211\u4eec\u5fc5\u987b\u8bb0\u4f4f\u5168\u90e8\u7684\u547d\u540d\u65b9\u6848\uff0c\u5e76\u4e3a\u6211\u4eec\u60f3\u8981\u7684\u6bcf\u4e00\u4e2a\u989d\u5916\u7684\u7c7b\u578b\u514b\u9686\u4e00\u5806\u4ee3\u7801\uff0c\u8fd9\u5e76\u4e0d\u80fd\u89e3\u51b3\u5197\u4f59\u95ee\u9898\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u6709\u66f4\u597d\u7684\u529e\u6cd5\uff01 \u4f5c\u8005\u6ce8 \u5728\u7ee7\u7eed\u9605\u8bfb\u4e4b\u524d\uff0c\u5982\u679c\u4f60\u8fd8\u4e0d\u719f\u6089\u51fd\u6570\u6a21\u677f\u3001\u6a21\u677f\u7c7b\u578b\u6216\u8005\u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u63a8\u8350\u590d\u4e60\u4e00\u4e0b 8.13 - \u51fd\u6570\u6a21\u677f \u548c 8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316 \u3002 \u7c7b\u6a21\u677f \u5c31\u50cf\u51fd\u6570\u6a21\u677f\u662f\u5b9e\u4f8b\u5316\u51fd\u6570\u7684\u6a21\u677f\u5b9a\u4e49\u4e00\u6837\uff0c\u7c7b\u6a21\u677f\u662f\u5b9e\u4f8b\u5316\u7c7b\u7c7b\u578b\u7684\u6a21\u677f\u5b9a\u4e49\u3002 \u63d0\u9192 \u6240\u8c13 \u201c \u7c7b\u7c7b\u578b \u201d \u6307\u7684\u662f\u7ed3\u6784\u4f53\u3001\u7c7b\u6216\u8054\u5408\u4f53\u3002\u867d\u7136\u6211\u4eec\u4ee5\u7ed3\u6784\u4f53\u7684\u7c7b\u6a21\u677f\u8fdb\u884c\u6f14\u793a\uff0c\u4f46\u662f\u5b83\u540c\u6837\u9002\u7528\u4e8e\u7c7b\u3002 \u56de\u5fc6\u4e00\u4e0b\uff0c\u8fd9\u91cc\u6709\u4e00\u4e2a\u6574\u578b\u6210\u5458\u7c7b\u578b\u7684 Pair \u7ed3\u6784\u4f53\u5b9a\u4e49\uff1a 1 2 3 4 5 struct Pair { int first {}; int second {}; }; \u8ba9\u6211\u4eec\u4f7f\u7528\u7c7b\u6a21\u677f\u91cd\u5199\u4e00\u4e0b\u8be5\u7ed3\u6784\u4f53\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include template < typename T > struct Pair { T first {}; T second {}; }; int main () { Pair < int > p1 { 5 , 6 }; // instantiates Pair and creates object p1 std :: cout << p1 . first << ' ' << p1 . second << '\\n' ; Pair < double > p2 { 1.2 , 3.4 }; // instantiates Pair and creates object p2 std :: cout << p2 . first << ' ' << p2 . second << '\\n' ; Pair < double > p3 { 7.8 , 9.0 }; // creates object p3 using prior definition for Pair std :: cout << p3 . first << ' ' << p3 . second << '\\n' ; return 0 ; } \u5c31\u50cf\u51fd\u6570\u6a21\u677f\u4e00\u6837\uff0c\u6211\u4eec\u4ece\u6a21\u677f\u5f62\u53c2\u58f0\u660e\u5f00\u59cb\u5b9a\u4e49\u7c7b\u6a21\u677f\uff0c\u5373\u5148\u662f template \u5173\u952e\u5b57\u3002\u63a5\u4e0b\u6765\uff0c\u5728\u5c16\u62ec\u53f7( <> )\u4e2d\u6307\u5b9a\u7c7b\u6a21\u677f\u5c06\u4f7f\u7528\u7684\u6240\u6709\u6a21\u677f\u7c7b\u578b\u3002\u5bf9\u4e8e\u6211\u4eec\u9700\u8981\u7684\u6bcf\u4e2a\u6a21\u677f\u7c7b\u578b\uff0c\u9700\u8981\u4f7f\u7528\u5173\u952e\u5b57 typename (\u9996\u9009)\u6216 class (\u975e\u9996\u9009)\uff0c\u540e\u9762\u8ddf\u7740\u6a21\u677f\u7c7b\u578b\u7684\u540d\u79f0(\u4f8b\u5982\u3002\u201cT\u201d)\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u7684\u4e24\u4e2a\u6210\u5458\u90fd\u662f\u76f8\u540c\u7684\u7c7b\u578b\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u9700\u8981\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\u3002 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u50cf\u5f80\u5e38\u4e00\u6837\u5b9a\u4e49\u7ed3\u6784\uff0c\u53ea\u4e0d\u8fc7\u6211\u4eec\u53ef\u4ee5\u5728\u9700\u8981\u6a21\u677f\u7c7b\u578b\u7684\u5730\u65b9\u4f7f\u7528\u6a21\u677f\u7c7b\u578b( T )\uff0c\u4ee5\u4fbf\u7a0d\u540e\u7528\u5b9e\u9645\u7c7b\u578b\u66ff\u6362\u6a21\u677f\u7c7b\u578b\u3002\u5c31\u662f\u8fd9\u6837\uff01\u7c7b\u6a21\u677f\u7684\u5b9a\u4e49\u5c31\u5b8c\u6210\u4e86\u3002 \u5728 main \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9700\u8981\u8981\u7684\u4efb\u4f55\u7c7b\u578b\u6765\u5b9e\u4f8b\u5316 Pair \u5bf9\u8c61\u3002\u9996\u5148\uff0c\u6211\u4eec\u5b9e\u4f8b\u5316\u4e00\u4e2a Pair \u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u56e0\u4e3a Pair \u7684\u7c7b\u578b\u5b9a\u4e49\u8fd8\u4e0d\u5b58\u5728\uff0c\u7f16\u8bd1\u5668\u4f7f\u7528\u7c7b\u6a21\u677f\u5b9e\u4f8b\u5316\u4e00\u4e2a\u540d\u4e3a Pair \u7684\u7ed3\u6784\u4f53\u7c7b\u578b\u5b9a\u4e49\uff0c\u5176\u4e2d\u6240\u6709\u51fa\u73b0\u7684\u6a21\u677f\u7c7b\u578b T \u90fd\u88ab\u7c7b\u578b int \u66ff\u6362\u3002 \u63a5\u4e0b\u6765\uff0c\u518d\u5b9e\u4f8b\u5316\u4e00\u4e2a Pair \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u6b64\u65f6\u4f1a\u5b9e\u4f8b\u5316\u4e00\u4e2a Pair \u7c7b\u578b\u7684\u7ed3\u6784\u4f53\u5b9a\u4e49\uff0c\u5176\u4e2d T \u4f1a\u88ab\u66ff\u6362\u4e3a double \u3002\u5bf9\u4e8e p3 \u6765\u8bf4\uff0c Pair \u5df2\u7ecf\u88ab\u5b9e\u4f8b\u5316\u4e86\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u4f7f\u7528\u4e4b\u524d\u7684\u5b9a\u4e49\u3002 \u63a5\u4e0b\u6765\u662f\u4e0e\u4e0a\u9762\u76f8\u540c\u7684\u4f8b\u5b50\uff0c\u663e\u793a\u4e86\u7f16\u8bd1\u5668\u5728\u6240\u6709\u6a21\u677f\u5b9e\u4f8b\u5316\u5b8c\u6210\u540e\u5b9e\u9645\u7f16\u8bd1\u7684\u5185\u5bb9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include // A declaration for our Pair class template // (we don't need the definition any more since it's not used) template < typename T > struct Pair ; // Explicitly define what Pair looks like template <> // tells the compiler this is a template type with no template parameters struct Pair < int > { int first {}; int second {}; }; // Explicitly define what Pair looks like template <> // tells the compiler this is a template type with no template parameters struct Pair < double > { double first {}; double second {}; }; int main () { Pair < int > p1 { 5 , 6 }; // instantiates Pair and creates object p1 std :: cout << p1 . first << ' ' << p1 . second << '\\n' ; Pair < double > p2 { 1.2 , 3.4 }; // instantiates Pair and creates object p2 std :: cout << p2 . first << ' ' << p2 . second << '\\n' ; Pair < double > p3 { 7.8 , 9.0 }; // creates object p3 using prior definition for Pair std :: cout << p3 . first << ' ' << p3 . second << '\\n' ; return 0 ; } \u4f60\u53ef\u4ee5\u76f4\u63a5\u7f16\u8bd1\u6b64\u793a\u4f8b\uff0c\u5b83\u80fd\u591f\u6309\u9884\u671f\u5de5\u4f5c! \u6269\u5c55\u9605\u8bfb \u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86\u4e00\u4e2a\u79f0\u4e3a \u7c7b\u6a21\u677f\u7279\u5316 \u7684\u7279\u6027(\u4f1a\u5728 19.4 -- Class template specialization \u4e2d\u4ecb\u7ecd)\u3002\u76ee\u524d\u6211\u4eec\u8fd8\u4e0d\u9700\u8981\u638c\u63e1\u8be5\u7279\u6027\u7684\u77e5\u8bc6\u3002 \u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u7c7b\u6a21\u677f \u73b0\u5728\u8ba9\u6211\u4eec\u56de\u5230\u8ba9 max() \u51fd\u6570\u914d\u5408\u4e0d\u540c\u7c7b\u578b\u5de5\u4f5c\u7684\u6311\u6218\u3002\u56e0\u4e3a\u7f16\u8bd1\u5668\u5c06 Pair \u548c Pair \u770b\u505a\u5355\u72ec\u7684\u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6309\u5f62\u53c2\u7c7b\u578b\u533a\u5206\u7684\u91cd\u8f7d\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 constexpr int max ( Pair < int > p ) { return ( p . first > p . second ? p . first : p . second ); } constexpr double max ( Pair < double > p ) // okay: overloaded function differentiated by parameter type { return ( p . first > p . second ? p . first : p . second ); } \u867d\u7136\u4e0a\u8ff0\u4ee3\u7801\u53ef\u4ee5\u7f16\u8bd1\u4e86\uff0c\u4f46\u662f\u5b83\u6ca1\u6709\u89e3\u51b3\u4ee3\u7801\u5197\u4f59\u7684\u95ee\u9898\u3002\u6211\u4eec\u5b9e\u9645\u4e0a\u5e0c\u671b\u4e00\u4e2a\u51fd\u6570\u80fd\u591f\u63a5\u53d7\u4efb\u4f55\u7c7b\u578b\u7684\u53c2\u6570\u3002\u6362\u8a00\u4e4b\u6211\u4eec\u5e0c\u671b\u51fd\u6570\u53ef\u4ee5\u4f7f\u7528 Pair \u7c7b\u578b\u7684\u5f62\u53c2\uff0c\u5176\u4e2d T \u662f\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\u53c2\u6570\u3002\u56e0\u6b64\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u51fd\u6570\u6a21\u677f\u6765\u5b8c\u6210\u76f8\u5e94\u7684\u5de5\u4f5c\uff01 \u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u4f8b\u5b50\uff0c max() \u88ab\u5b9e\u73b0\u4e3a\u4e00\u4e2a\u51fd\u6570\u6a21\u677f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include template < typename T > struct Pair { T first {}; T second {}; }; template < typename T > constexpr T max ( Pair < T > p ) { return ( p . first > p . second ? p . first : p . second ); } int main () { Pair < int > p1 { 5 , 6 }; std :: cout << max < int > ( p1 ) << \" is larger \\n \" ; // explicit call to max Pair < double > p2 { 1.2 , 3.4 }; std :: cout << max ( p2 ) << \" is larger \\n \" ; // call to max using template argument deduction (prefer this) return 0 ; } max() \u51fd\u6570\u6a21\u677f\u975e\u5e38\u76f4\u767d\u3002\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u5411\u5176\u4f20\u9012 Pair \uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u5fc5\u987b\u4e86\u89e3 T \u662f\u4ec0\u4e48\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u5728\u51fd\u6570\u5f00\u5934\u51fa\u6dfb\u52a0\u6a21\u677f\u53c2\u6570\u58f0\u660e\uff0c\u5b9a\u4e49\u6a21\u677f\u7c7b\u578b T \u3002\u968f\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528 T \u4f5c\u4e3a\u8fd4\u56de\u7c7b\u578b\uff0c\u4ee5\u53ca\u6a21\u677f\u53c2\u6570 Pair \u3002 \u56e0\u4e3a max() \u51fd\u6570\u7684\u8c03\u7528\u57fa\u4e8e Pair \u7c7b\u578b \u5b9e\u53c2 \uff0c\u7f16\u8bd1\u5668\u4f1a\u57fa\u4e8e\u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316\u51fd\u6570 int max(Pair) \uff0c\u6a21\u677f\u4e2d\u7684 T \u88ab\u66ff\u6362\u4e3a\u4e86 int \u3002\u7f16\u8bd1\u5668\u5b9e\u9645\u4e0a\u5b9e\u4f8b\u5316\u4e86\u8fd9\u6837\u4e00\u4e2a\u51fd\u6570\uff1a 1 2 3 4 5 template <> constexpr int max ( Pair < int > p ) { return ( p . first > p . second ? p . first : p . second ); } \u4e0e\u6240\u6709\u5bf9\u51fd\u6570\u6a21\u677f\u7684\u8c03\u7528\u4e00\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u663e\u5f0f\u5730\u6307\u5b9a\u6a21\u677f\u7c7b\u578b\u5b9e\u53c2(\u4f8b\u5982 max(p1) )\uff0c\u4e5f\u53ef\u4ee5\u9690\u5f0f\u5730\u4f7f\u7528(\u4f8b\u5982 max(p2) )\uff0c\u5e76\u8ba9\u7f16\u8bd1\u5668\u4f7f\u7528 \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad \u6765\u786e\u5b9a\u6a21\u677f\u7c7b\u578b\u5b9e\u53c2\u5e94\u8be5\u662f\u4ec0\u4e48\u7c7b\u578b\u7684\u3002 \u540c\u65f6\u4f7f\u7528\u6a21\u677f\u7c7b\u578b\u548c\u975e\u6a21\u677f\u7c7b\u578b\u7684\u7c7b\u6a21\u677f \u7c7b\u6a21\u677f\u7684\u6210\u5458\u53ef\u4ee5\u4f7f\u7528\u6a21\u677f\u7c7b\u578b\uff0c\u4e5f\u53ef\u4ee5\u540c\u65f6\u6709\u53e6\u5916\u4e00\u90e8\u5206\u6210\u5458\u4f7f\u7528\u666e\u901a\u7c7b\u578b\uff08\u975e\u6a21\u677f\uff09\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 template < typename T > struct Foo { T first {}; // first will have whatever type T is replaced with int second {}; // second will always have type int, regardless of what type T is }; \u4e0a\u8ff0\u4ee3\u7801\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\uff1a first \u7684\u7c7b\u578b\u7531 T \u6307\u5b9a\uff0c\u800c second \u7c7b\u578b\u5219\u603b\u662f int \u3002 \u5177\u6709\u591a\u4e2a\u6a21\u677f\u7c7b\u578b\u7684\u7c7b\u6a21\u677f \u7c7b\u6a21\u677f\u4e5f\u53ef\u4ee5\u6709\u591a\u4e2a\u6a21\u677f\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b Pair \u7c7b\u7684\u4e24\u4e2a\u6210\u5458\u80fd\u591f\u5177\u6709\u4e0d\u540c\u7684\u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4e24\u79cd\u6a21\u677f\u7c7b\u578b\u5b9a\u4e49 Pair \u7c7b\u6a21\u677f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include template < typename T , typename U > struct Pair { T first {}; U second {}; }; template < typename T , typename U > void print ( Pair < T , U > p ) { std :: cout << '[' << p . first << \", \" << p . second << ']' ; } int main () { Pair < int , double > p1 { 1 , 2.3 }; // a pair holding an int and a double Pair < double , int > p2 { 4.5 , 6 }; // a pair holding a double and an int Pair < int , int > p3 { 7 , 8 }; // a pair holding two ints print ( p2 ); return 0 ; } \u8981\u5b9a\u4e49\u591a\u4e2a\u6a21\u677f\u7c7b\u578b\uff0c\u5728\u6a21\u677f\u53c2\u6570\u58f0\u660e\u4e2d\uff0c\u7528\u9017\u53f7\u5206\u9694\u6bcf\u4e2a\u6240\u9700\u7684\u6a21\u677f\u7c7b\u578b\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u79cd\u4e0d\u540c\u7684\u6a21\u677f\u7c7b\u578b\uff0c\u4e00\u79cd\u540d\u4e3a T \uff0c\u53e6\u4e00\u79cd\u540d\u4e3a U \u3002 T \u548c U \u7684\u5b9e\u9645\u6a21\u677f\u7c7b\u578b\u5b9e\u53c2\u53ef\u4ee5\u4e0d\u540c(\u5c31\u50cf\u4e0a\u9762\u7684 p1 \u548c p2 \u4e00\u6837)\uff0c\u4e5f\u53ef\u4ee5\u76f8\u540c(\u5c31\u50cf p3 \u4e00\u6837)\u3002 std::pair \u56e0\u4e3a\u5728C++\u4e2d\u4f7f\u7528\u4e00\u5bf9\u6570\u636e\u662f\u5f88\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u56e0\u6b64C++\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a std::pair \u7684\u7c7b\u6a21\u677f(\u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d) \uff0c\u5b83\u7684\u53e6\u4e00\u548c\u4f7f\u7528\u4e86\u591a\u4e2a\u6a21\u677f\u7c7b\u578b\u7684 Pair \u7c7b\u4e00\u6837\u3002\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 std::pair \u6765\u66ff\u6362\u6211\u4eec\u5b9a\u4e49\u7684 pair \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include template < typename T , typename U > void print ( std :: pair < T , U > p ) { std :: cout << '[' << p . first << \", \" << p . second << ']' ; } int main () { std :: pair < int , double > p1 { 1 , 2.3 }; // a pair holding an int and a double std :: pair < double , int > p2 { 4.5 , 6 }; // a pair holding a double and an int std :: pair < int , int > p3 { 7 , 8 }; // a pair holding two ints print ( p2 ); return 0 ; } \u672c\u8282\u8bfe\u6211\u4eec\u5b9a\u4e49\u4e86\u81ea\u5df1\u7684 Pair \u7c7b\u4ee5\u89e3\u91ca\u5176\u5de5\u4f5c\u539f\u7406\uff0c\u4f46\u662f\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\uff0c\u6211\u4eec\u63a8\u8350\u4f7f\u7528 std::pair \u3002 \u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528\u7c7b\u6a21\u677f \u5c31\u50cf\u51fd\u6570\u6a21\u677f\u4e00\u6837\uff0c\u7c7b\u6a21\u677f\u901a\u5e38\u662f\u5728\u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\uff0c\u56e0\u6b64\u5b83\u4eec\u53ef\u4ee5\u88ab\u5305\u542b\u5230\u4efb\u4f55\u9700\u8981\u5b83\u4eec\u7684\u4ee3\u7801\u6587\u4ef6\u4e2d\u3002\u6a21\u677f\u5b9a\u4e49\u548c\u7c7b\u578b\u5b9a\u4e49\u90fd\u4e0d\u53d7 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u7684\u7ea6\u675f\uff0c\u6240\u4ee5\u8fd9\u4e0d\u4f1a\u5bfc\u81f4\u95ee\u9898\uff1a pair.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #ifndef PAIR_H #define PAIR_H template < typename T > struct Pair { T first {}; T second {}; }; template < typename T > constexpr T max ( Pair < T > p ) { return ( p . first > p . second ? p . first : p . second ); } #endif foo.cpp 1 2 3 4 5 6 7 8 #include \"pair.h\" #include void foo () { Pair < int > p1 { 1 , 2 }; std :: cout << max ( p1 ) << \" is larger \\n \" ; } main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include \"pair.h\" #include void foo (); // forward declaration for function foo() int main () { Pair < double > p2 { 3.4 , 5.6 }; std :: cout << max ( p2 ) << \" is larger \\n \" ; foo (); return 0 ; }","title":"10.10 - \u7c7b\u6a21\u677f"},{"location":"10-10-class-templates/#_1","text":"\u5bf9\u4e8e\u805a\u5408\u7c7b\u578b(\u5305\u62ec\u7ed3\u6784/\u7c7b/\u8054\u5408\u548c\u6570\u7ec4)\uff0c\u6211\u4eec\u4e5f\u9047\u5230\u4e86\u7c7b\u4f3c\u7684\u95ee\u9898\u3002 \u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5176\u4e2d\u9700\u8981\u5904\u7406\u4e00\u5bf9 int \u503c\uff0c\u5e76\u9700\u8981\u786e\u5b9a\u8fd9\u4e24\u4e2a\u6570\u5b57\u4e2d\u54ea\u4e00\u4e2a\u66f4\u5927\u3002\u6211\u4eec\u53ef\u4ee5\u5199\u8fd9\u6837\u4e00\u4e2a\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include struct Pair { int first {}; int second {}; }; constexpr int max ( Pair p ) // pass by value because Pair is small { return ( p . first > p . second ? p . first : p . second ); } int main () { Pair p1 { 5 , 6 }; std :: cout << max ( p1 ) << \" is larger \\n \" ; return 0 ; } \u4e00\u6bb5\u65f6\u95f4\u540e\uff0c\u6211\u4eec\u53d1\u73b0\u6211\u4eec\u9700\u8981\u5904\u7406\u4e00\u5bf9 double \u7c7b\u578b\u7684\u503c\uff0c\u90a3\u4e48\u6211\u4eec\u5fc5\u987b\u66f4\u65b0\u7a0b\u5e8f\uff0c\u6dfb\u52a0\u53e6\u5916\u4e00\u4e2a Pair \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include struct Pair { int first {}; int second {}; }; struct Pair // compile error: erroneous redefinition of Pair { double first {}; double second {}; }; constexpr int max ( Pair p ) { return ( p . first > p . second ? p . first : p . second ); } constexpr double max ( Pair p ) // compile error: overloaded function differs only by return type { return ( p . first > p . second ? p . first : p . second ); } int main () { Pair p1 { 5 , 6 }; std :: cout << max ( p1 ) << \" is larger \\n \" ; Pair p2 { 1.2 , 3.4 }; std :: cout << max ( p2 ) << \" is larger \\n \" ; return 0 ; } \u5f88\u9057\u61be\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u662f\u4e0d\u80fd\u7f16\u8bd1\u7684\uff0c\u800c\u4e14\u8fd9\u91cc\u6709\u5f88\u591a\u95ee\u9898\uff1a \u9996\u5148\uff0c\u548c\u51fd\u6570\u4e0d\u540c\uff0c\u7c7b\u578b\u662f\u4e0d\u80fd\u88ab \u91cd\u8f7d \u7684\u3002\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u5c06\u7b2c\u4e8c\u4e2a double \u6210\u5458\u7248\u672c\u7684 Pair \u770b\u505a\u662f\u7b2c\u4e00\u4e2a Pair \u7684\u91cd\u590d\u5b9a\u4e49\u3002\u7b2c\u4e8c\uff0c\u5c3d\u7ba1\u51fd\u6570\u53ef\u4ee5\u88ab\u91cd\u8f7d\uff0c\u4f46\u4e24\u4e2a max(Pair) \u51fd\u6570\u53ea\u6709\u8fd4\u56de\u503c\u4e0d\u540c\uff0c\u800c\u51fd\u6570\u91cd\u8f7d\u4e0d\u80fd\u4ec5\u4ec5\u901a\u8fc7\u4e0d\u540c\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u533a\u5206\u3002\u7b2c\u4e09\uff0c\u4ee3\u7801\u6709\u5f88\u591a\u5197\u4f59\u3002\u6bcf\u4e2a Pair \u7ed3\u6784\u4f53\u90fd\u662f\u76f8\u540c\u7684\uff08\u53ea\u662f\u6570\u636e\u7c7b\u578b\u4e0d\u540c\uff09\uff0c max(Pair) \u51fd\u6570\u4e5f\u662f (\u53ea\u6709\u8fd4\u56de\u503c\u4e0d\u540c)\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ed9 Pair \u8d77\u4e0d\u540c\u7684\u540d\u5b57\u6765\u89e3\u51b3\u524d\u4e24\u4e2a\u95ee\u9898\uff08\u4f8b\u5982 Pairint \u548c Pairdouble )\u3002\u4f46\u662f\u6211\u4eec\u5fc5\u987b\u8bb0\u4f4f\u5168\u90e8\u7684\u547d\u540d\u65b9\u6848\uff0c\u5e76\u4e3a\u6211\u4eec\u60f3\u8981\u7684\u6bcf\u4e00\u4e2a\u989d\u5916\u7684\u7c7b\u578b\u514b\u9686\u4e00\u5806\u4ee3\u7801\uff0c\u8fd9\u5e76\u4e0d\u80fd\u89e3\u51b3\u5197\u4f59\u95ee\u9898\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u6709\u66f4\u597d\u7684\u529e\u6cd5\uff01 \u4f5c\u8005\u6ce8 \u5728\u7ee7\u7eed\u9605\u8bfb\u4e4b\u524d\uff0c\u5982\u679c\u4f60\u8fd8\u4e0d\u719f\u6089\u51fd\u6570\u6a21\u677f\u3001\u6a21\u677f\u7c7b\u578b\u6216\u8005\u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u63a8\u8350\u590d\u4e60\u4e00\u4e0b 8.13 - \u51fd\u6570\u6a21\u677f \u548c 8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316 \u3002","title":"\u805a\u5408\u7c7b\u578b\u4e5f\u9762\u4e34\u7c7b\u4f3c\u7684\u95ee\u9898"},{"location":"10-10-class-templates/#_2","text":"\u5c31\u50cf\u51fd\u6570\u6a21\u677f\u662f\u5b9e\u4f8b\u5316\u51fd\u6570\u7684\u6a21\u677f\u5b9a\u4e49\u4e00\u6837\uff0c\u7c7b\u6a21\u677f\u662f\u5b9e\u4f8b\u5316\u7c7b\u7c7b\u578b\u7684\u6a21\u677f\u5b9a\u4e49\u3002 \u63d0\u9192 \u6240\u8c13 \u201c \u7c7b\u7c7b\u578b \u201d \u6307\u7684\u662f\u7ed3\u6784\u4f53\u3001\u7c7b\u6216\u8054\u5408\u4f53\u3002\u867d\u7136\u6211\u4eec\u4ee5\u7ed3\u6784\u4f53\u7684\u7c7b\u6a21\u677f\u8fdb\u884c\u6f14\u793a\uff0c\u4f46\u662f\u5b83\u540c\u6837\u9002\u7528\u4e8e\u7c7b\u3002 \u56de\u5fc6\u4e00\u4e0b\uff0c\u8fd9\u91cc\u6709\u4e00\u4e2a\u6574\u578b\u6210\u5458\u7c7b\u578b\u7684 Pair \u7ed3\u6784\u4f53\u5b9a\u4e49\uff1a 1 2 3 4 5 struct Pair { int first {}; int second {}; }; \u8ba9\u6211\u4eec\u4f7f\u7528\u7c7b\u6a21\u677f\u91cd\u5199\u4e00\u4e0b\u8be5\u7ed3\u6784\u4f53\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include template < typename T > struct Pair { T first {}; T second {}; }; int main () { Pair < int > p1 { 5 , 6 }; // instantiates Pair and creates object p1 std :: cout << p1 . first << ' ' << p1 . second << '\\n' ; Pair < double > p2 { 1.2 , 3.4 }; // instantiates Pair and creates object p2 std :: cout << p2 . first << ' ' << p2 . second << '\\n' ; Pair < double > p3 { 7.8 , 9.0 }; // creates object p3 using prior definition for Pair std :: cout << p3 . first << ' ' << p3 . second << '\\n' ; return 0 ; } \u5c31\u50cf\u51fd\u6570\u6a21\u677f\u4e00\u6837\uff0c\u6211\u4eec\u4ece\u6a21\u677f\u5f62\u53c2\u58f0\u660e\u5f00\u59cb\u5b9a\u4e49\u7c7b\u6a21\u677f\uff0c\u5373\u5148\u662f template \u5173\u952e\u5b57\u3002\u63a5\u4e0b\u6765\uff0c\u5728\u5c16\u62ec\u53f7( <> )\u4e2d\u6307\u5b9a\u7c7b\u6a21\u677f\u5c06\u4f7f\u7528\u7684\u6240\u6709\u6a21\u677f\u7c7b\u578b\u3002\u5bf9\u4e8e\u6211\u4eec\u9700\u8981\u7684\u6bcf\u4e2a\u6a21\u677f\u7c7b\u578b\uff0c\u9700\u8981\u4f7f\u7528\u5173\u952e\u5b57 typename (\u9996\u9009)\u6216 class (\u975e\u9996\u9009)\uff0c\u540e\u9762\u8ddf\u7740\u6a21\u677f\u7c7b\u578b\u7684\u540d\u79f0(\u4f8b\u5982\u3002\u201cT\u201d)\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u7684\u4e24\u4e2a\u6210\u5458\u90fd\u662f\u76f8\u540c\u7684\u7c7b\u578b\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u9700\u8981\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\u3002 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u50cf\u5f80\u5e38\u4e00\u6837\u5b9a\u4e49\u7ed3\u6784\uff0c\u53ea\u4e0d\u8fc7\u6211\u4eec\u53ef\u4ee5\u5728\u9700\u8981\u6a21\u677f\u7c7b\u578b\u7684\u5730\u65b9\u4f7f\u7528\u6a21\u677f\u7c7b\u578b( T )\uff0c\u4ee5\u4fbf\u7a0d\u540e\u7528\u5b9e\u9645\u7c7b\u578b\u66ff\u6362\u6a21\u677f\u7c7b\u578b\u3002\u5c31\u662f\u8fd9\u6837\uff01\u7c7b\u6a21\u677f\u7684\u5b9a\u4e49\u5c31\u5b8c\u6210\u4e86\u3002 \u5728 main \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9700\u8981\u8981\u7684\u4efb\u4f55\u7c7b\u578b\u6765\u5b9e\u4f8b\u5316 Pair \u5bf9\u8c61\u3002\u9996\u5148\uff0c\u6211\u4eec\u5b9e\u4f8b\u5316\u4e00\u4e2a Pair \u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u56e0\u4e3a Pair \u7684\u7c7b\u578b\u5b9a\u4e49\u8fd8\u4e0d\u5b58\u5728\uff0c\u7f16\u8bd1\u5668\u4f7f\u7528\u7c7b\u6a21\u677f\u5b9e\u4f8b\u5316\u4e00\u4e2a\u540d\u4e3a Pair \u7684\u7ed3\u6784\u4f53\u7c7b\u578b\u5b9a\u4e49\uff0c\u5176\u4e2d\u6240\u6709\u51fa\u73b0\u7684\u6a21\u677f\u7c7b\u578b T \u90fd\u88ab\u7c7b\u578b int \u66ff\u6362\u3002 \u63a5\u4e0b\u6765\uff0c\u518d\u5b9e\u4f8b\u5316\u4e00\u4e2a Pair \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u6b64\u65f6\u4f1a\u5b9e\u4f8b\u5316\u4e00\u4e2a Pair \u7c7b\u578b\u7684\u7ed3\u6784\u4f53\u5b9a\u4e49\uff0c\u5176\u4e2d T \u4f1a\u88ab\u66ff\u6362\u4e3a double \u3002\u5bf9\u4e8e p3 \u6765\u8bf4\uff0c Pair \u5df2\u7ecf\u88ab\u5b9e\u4f8b\u5316\u4e86\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u4f7f\u7528\u4e4b\u524d\u7684\u5b9a\u4e49\u3002 \u63a5\u4e0b\u6765\u662f\u4e0e\u4e0a\u9762\u76f8\u540c\u7684\u4f8b\u5b50\uff0c\u663e\u793a\u4e86\u7f16\u8bd1\u5668\u5728\u6240\u6709\u6a21\u677f\u5b9e\u4f8b\u5316\u5b8c\u6210\u540e\u5b9e\u9645\u7f16\u8bd1\u7684\u5185\u5bb9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include // A declaration for our Pair class template // (we don't need the definition any more since it's not used) template < typename T > struct Pair ; // Explicitly define what Pair looks like template <> // tells the compiler this is a template type with no template parameters struct Pair < int > { int first {}; int second {}; }; // Explicitly define what Pair looks like template <> // tells the compiler this is a template type with no template parameters struct Pair < double > { double first {}; double second {}; }; int main () { Pair < int > p1 { 5 , 6 }; // instantiates Pair and creates object p1 std :: cout << p1 . first << ' ' << p1 . second << '\\n' ; Pair < double > p2 { 1.2 , 3.4 }; // instantiates Pair and creates object p2 std :: cout << p2 . first << ' ' << p2 . second << '\\n' ; Pair < double > p3 { 7.8 , 9.0 }; // creates object p3 using prior definition for Pair std :: cout << p3 . first << ' ' << p3 . second << '\\n' ; return 0 ; } \u4f60\u53ef\u4ee5\u76f4\u63a5\u7f16\u8bd1\u6b64\u793a\u4f8b\uff0c\u5b83\u80fd\u591f\u6309\u9884\u671f\u5de5\u4f5c! \u6269\u5c55\u9605\u8bfb \u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86\u4e00\u4e2a\u79f0\u4e3a \u7c7b\u6a21\u677f\u7279\u5316 \u7684\u7279\u6027(\u4f1a\u5728 19.4 -- Class template specialization \u4e2d\u4ecb\u7ecd)\u3002\u76ee\u524d\u6211\u4eec\u8fd8\u4e0d\u9700\u8981\u638c\u63e1\u8be5\u7279\u6027\u7684\u77e5\u8bc6\u3002","title":"\u7c7b\u6a21\u677f"},{"location":"10-10-class-templates/#_3","text":"\u73b0\u5728\u8ba9\u6211\u4eec\u56de\u5230\u8ba9 max() \u51fd\u6570\u914d\u5408\u4e0d\u540c\u7c7b\u578b\u5de5\u4f5c\u7684\u6311\u6218\u3002\u56e0\u4e3a\u7f16\u8bd1\u5668\u5c06 Pair \u548c Pair \u770b\u505a\u5355\u72ec\u7684\u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6309\u5f62\u53c2\u7c7b\u578b\u533a\u5206\u7684\u91cd\u8f7d\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 constexpr int max ( Pair < int > p ) { return ( p . first > p . second ? p . first : p . second ); } constexpr double max ( Pair < double > p ) // okay: overloaded function differentiated by parameter type { return ( p . first > p . second ? p . first : p . second ); } \u867d\u7136\u4e0a\u8ff0\u4ee3\u7801\u53ef\u4ee5\u7f16\u8bd1\u4e86\uff0c\u4f46\u662f\u5b83\u6ca1\u6709\u89e3\u51b3\u4ee3\u7801\u5197\u4f59\u7684\u95ee\u9898\u3002\u6211\u4eec\u5b9e\u9645\u4e0a\u5e0c\u671b\u4e00\u4e2a\u51fd\u6570\u80fd\u591f\u63a5\u53d7\u4efb\u4f55\u7c7b\u578b\u7684\u53c2\u6570\u3002\u6362\u8a00\u4e4b\u6211\u4eec\u5e0c\u671b\u51fd\u6570\u53ef\u4ee5\u4f7f\u7528 Pair \u7c7b\u578b\u7684\u5f62\u53c2\uff0c\u5176\u4e2d T \u662f\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\u53c2\u6570\u3002\u56e0\u6b64\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u51fd\u6570\u6a21\u677f\u6765\u5b8c\u6210\u76f8\u5e94\u7684\u5de5\u4f5c\uff01 \u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u4f8b\u5b50\uff0c max() \u88ab\u5b9e\u73b0\u4e3a\u4e00\u4e2a\u51fd\u6570\u6a21\u677f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include template < typename T > struct Pair { T first {}; T second {}; }; template < typename T > constexpr T max ( Pair < T > p ) { return ( p . first > p . second ? p . first : p . second ); } int main () { Pair < int > p1 { 5 , 6 }; std :: cout << max < int > ( p1 ) << \" is larger \\n \" ; // explicit call to max Pair < double > p2 { 1.2 , 3.4 }; std :: cout << max ( p2 ) << \" is larger \\n \" ; // call to max using template argument deduction (prefer this) return 0 ; } max() \u51fd\u6570\u6a21\u677f\u975e\u5e38\u76f4\u767d\u3002\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u5411\u5176\u4f20\u9012 Pair \uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u5fc5\u987b\u4e86\u89e3 T \u662f\u4ec0\u4e48\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u5728\u51fd\u6570\u5f00\u5934\u51fa\u6dfb\u52a0\u6a21\u677f\u53c2\u6570\u58f0\u660e\uff0c\u5b9a\u4e49\u6a21\u677f\u7c7b\u578b T \u3002\u968f\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528 T \u4f5c\u4e3a\u8fd4\u56de\u7c7b\u578b\uff0c\u4ee5\u53ca\u6a21\u677f\u53c2\u6570 Pair \u3002 \u56e0\u4e3a max() \u51fd\u6570\u7684\u8c03\u7528\u57fa\u4e8e Pair \u7c7b\u578b \u5b9e\u53c2 \uff0c\u7f16\u8bd1\u5668\u4f1a\u57fa\u4e8e\u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316\u51fd\u6570 int max(Pair) \uff0c\u6a21\u677f\u4e2d\u7684 T \u88ab\u66ff\u6362\u4e3a\u4e86 int \u3002\u7f16\u8bd1\u5668\u5b9e\u9645\u4e0a\u5b9e\u4f8b\u5316\u4e86\u8fd9\u6837\u4e00\u4e2a\u51fd\u6570\uff1a 1 2 3 4 5 template <> constexpr int max ( Pair < int > p ) { return ( p . first > p . second ? p . first : p . second ); } \u4e0e\u6240\u6709\u5bf9\u51fd\u6570\u6a21\u677f\u7684\u8c03\u7528\u4e00\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u663e\u5f0f\u5730\u6307\u5b9a\u6a21\u677f\u7c7b\u578b\u5b9e\u53c2(\u4f8b\u5982 max(p1) )\uff0c\u4e5f\u53ef\u4ee5\u9690\u5f0f\u5730\u4f7f\u7528(\u4f8b\u5982 max(p2) )\uff0c\u5e76\u8ba9\u7f16\u8bd1\u5668\u4f7f\u7528 \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad \u6765\u786e\u5b9a\u6a21\u677f\u7c7b\u578b\u5b9e\u53c2\u5e94\u8be5\u662f\u4ec0\u4e48\u7c7b\u578b\u7684\u3002","title":"\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u7c7b\u6a21\u677f"},{"location":"10-10-class-templates/#_4","text":"\u7c7b\u6a21\u677f\u7684\u6210\u5458\u53ef\u4ee5\u4f7f\u7528\u6a21\u677f\u7c7b\u578b\uff0c\u4e5f\u53ef\u4ee5\u540c\u65f6\u6709\u53e6\u5916\u4e00\u90e8\u5206\u6210\u5458\u4f7f\u7528\u666e\u901a\u7c7b\u578b\uff08\u975e\u6a21\u677f\uff09\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 template < typename T > struct Foo { T first {}; // first will have whatever type T is replaced with int second {}; // second will always have type int, regardless of what type T is }; \u4e0a\u8ff0\u4ee3\u7801\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\uff1a first \u7684\u7c7b\u578b\u7531 T \u6307\u5b9a\uff0c\u800c second \u7c7b\u578b\u5219\u603b\u662f int \u3002","title":"\u540c\u65f6\u4f7f\u7528\u6a21\u677f\u7c7b\u578b\u548c\u975e\u6a21\u677f\u7c7b\u578b\u7684\u7c7b\u6a21\u677f"},{"location":"10-10-class-templates/#_5","text":"\u7c7b\u6a21\u677f\u4e5f\u53ef\u4ee5\u6709\u591a\u4e2a\u6a21\u677f\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b Pair \u7c7b\u7684\u4e24\u4e2a\u6210\u5458\u80fd\u591f\u5177\u6709\u4e0d\u540c\u7684\u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4e24\u79cd\u6a21\u677f\u7c7b\u578b\u5b9a\u4e49 Pair \u7c7b\u6a21\u677f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include template < typename T , typename U > struct Pair { T first {}; U second {}; }; template < typename T , typename U > void print ( Pair < T , U > p ) { std :: cout << '[' << p . first << \", \" << p . second << ']' ; } int main () { Pair < int , double > p1 { 1 , 2.3 }; // a pair holding an int and a double Pair < double , int > p2 { 4.5 , 6 }; // a pair holding a double and an int Pair < int , int > p3 { 7 , 8 }; // a pair holding two ints print ( p2 ); return 0 ; } \u8981\u5b9a\u4e49\u591a\u4e2a\u6a21\u677f\u7c7b\u578b\uff0c\u5728\u6a21\u677f\u53c2\u6570\u58f0\u660e\u4e2d\uff0c\u7528\u9017\u53f7\u5206\u9694\u6bcf\u4e2a\u6240\u9700\u7684\u6a21\u677f\u7c7b\u578b\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u79cd\u4e0d\u540c\u7684\u6a21\u677f\u7c7b\u578b\uff0c\u4e00\u79cd\u540d\u4e3a T \uff0c\u53e6\u4e00\u79cd\u540d\u4e3a U \u3002 T \u548c U \u7684\u5b9e\u9645\u6a21\u677f\u7c7b\u578b\u5b9e\u53c2\u53ef\u4ee5\u4e0d\u540c(\u5c31\u50cf\u4e0a\u9762\u7684 p1 \u548c p2 \u4e00\u6837)\uff0c\u4e5f\u53ef\u4ee5\u76f8\u540c(\u5c31\u50cf p3 \u4e00\u6837)\u3002","title":"\u5177\u6709\u591a\u4e2a\u6a21\u677f\u7c7b\u578b\u7684\u7c7b\u6a21\u677f"},{"location":"10-10-class-templates/#stdpair","text":"\u56e0\u4e3a\u5728C++\u4e2d\u4f7f\u7528\u4e00\u5bf9\u6570\u636e\u662f\u5f88\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u56e0\u6b64C++\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a std::pair \u7684\u7c7b\u6a21\u677f(\u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d) \uff0c\u5b83\u7684\u53e6\u4e00\u548c\u4f7f\u7528\u4e86\u591a\u4e2a\u6a21\u677f\u7c7b\u578b\u7684 Pair \u7c7b\u4e00\u6837\u3002\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 std::pair \u6765\u66ff\u6362\u6211\u4eec\u5b9a\u4e49\u7684 pair \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include template < typename T , typename U > void print ( std :: pair < T , U > p ) { std :: cout << '[' << p . first << \", \" << p . second << ']' ; } int main () { std :: pair < int , double > p1 { 1 , 2.3 }; // a pair holding an int and a double std :: pair < double , int > p2 { 4.5 , 6 }; // a pair holding a double and an int std :: pair < int , int > p3 { 7 , 8 }; // a pair holding two ints print ( p2 ); return 0 ; } \u672c\u8282\u8bfe\u6211\u4eec\u5b9a\u4e49\u4e86\u81ea\u5df1\u7684 Pair \u7c7b\u4ee5\u89e3\u91ca\u5176\u5de5\u4f5c\u539f\u7406\uff0c\u4f46\u662f\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\uff0c\u6211\u4eec\u63a8\u8350\u4f7f\u7528 std::pair \u3002","title":"std::pair"},{"location":"10-10-class-templates/#_6","text":"\u5c31\u50cf\u51fd\u6570\u6a21\u677f\u4e00\u6837\uff0c\u7c7b\u6a21\u677f\u901a\u5e38\u662f\u5728\u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\uff0c\u56e0\u6b64\u5b83\u4eec\u53ef\u4ee5\u88ab\u5305\u542b\u5230\u4efb\u4f55\u9700\u8981\u5b83\u4eec\u7684\u4ee3\u7801\u6587\u4ef6\u4e2d\u3002\u6a21\u677f\u5b9a\u4e49\u548c\u7c7b\u578b\u5b9a\u4e49\u90fd\u4e0d\u53d7 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u7684\u7ea6\u675f\uff0c\u6240\u4ee5\u8fd9\u4e0d\u4f1a\u5bfc\u81f4\u95ee\u9898\uff1a pair.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #ifndef PAIR_H #define PAIR_H template < typename T > struct Pair { T first {}; T second {}; }; template < typename T > constexpr T max ( Pair < T > p ) { return ( p . first > p . second ? p . first : p . second ); } #endif foo.cpp 1 2 3 4 5 6 7 8 #include \"pair.h\" #include void foo () { Pair < int > p1 { 1 , 2 }; std :: cout << max ( p1 ) << \" is larger \\n \" ; } main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include \"pair.h\" #include void foo (); // forward declaration for function foo() int main () { Pair < double > p2 { 3.4 , 5.6 }; std :: cout << max ( p2 ) << \" is larger \\n \" ; foo (); return 0 ; }","title":"\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528\u7c7b\u6a21\u677f"},{"location":"10-11-class-template-argument-deduction-and-deduction%20-guides/","tags":["class template","CTAD","C++17"],"text":"Key Takeaway \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad (CTAD) (C++17) \u4eceC++ 17\u5f00\u59cb\uff0c\u5f53\u4ece\u7c7b\u6a21\u677f\u5b9e\u4f8b\u5316\u4e00\u4e2a\u5bf9\u8c61\u65f6\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u4ece\u5bf9\u8c61\u7684\u521d\u59cb\u5316\u5f0f\u7684\u7c7b\u578b\u63a8\u65ad\u51fa\u6a21\u677f\u7c7b\u578b( \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad \uff0c\u7b80\u79f0CTAD)\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 #include // for std::pair int main () { std :: pair < int , int > p1 { 1 , 2 }; // explicitly specify class template std::pair (C++11 onward) std :: pair p2 { 1 , 2 }; // CTAD used to deduce std::pair from the initializers (C++17) return 0 ; } CTAD \u53ea\u6709\u5728\u7c7b\u6a21\u677f\u5217\u8868\u4e2d\u6ca1\u6709\u63d0\u4f9b\u4efb\u4f55\u53c2\u6570\u65f6\u624d\u4f1a\u8fdb\u884c\uff0c\u56e0\u6b64\u4e0b\u9762\u4ee3\u7801\u4e2d\u7684\u4e24\u79cd\u65b9\u5f0f\u90fd\u662f\u9519\u8bef\u7684\uff1a 1 2 3 4 5 6 7 8 9 #include // for std::pair int main () { std :: pair <> p1 { 1 , 2 }; // \u9519\u8bef: \u6a21\u677f\u53c2\u6570\u592a\u5c11, \u4e24\u4e2a\u53c2\u6570\u90fd\u4e0d\u4f1a\u63a8\u65ad std :: pair < int > p2 { 3 , 4 }; // \u9519\u8bef: \u6a21\u677f\u53c2\u6570\u592a\u5c11,\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e0d\u4f1a\u88ab\u63a8\u65ad return 0 ; } \u4f5c\u8005\u6ce8 \u672c\u7f51\u7ad9\u4eca\u540e\u7684\u8bb8\u591a\u8bfe\u7a0b\u90fd\u5229\u7528\u4e86CTAD\u3002\u5982\u679c\u4f7f\u7528C++14\u6807\u51c6\u7f16\u8bd1\u8fd9\u4e9b\u793a\u4f8b\uff0c\u5c06\u4f1a\u5f97\u5230\u4e00\u4e2a\u5173\u4e8e\u7f3a\u5c11\u6a21\u677f\u53c2\u6570\u7684\u9519\u8bef\u3002\u60a8\u9700\u8981\u663e\u5f0f\u5730\u5c06\u8fd9\u4e9b\u53c2\u6570\u6dfb\u52a0\u5230\u793a\u4f8b\u4e2d\uff0c\u4ee5\u4f7f\u5176\u80fd\u591f\u7f16\u8bd1\u3002 \u6a21\u677f\u53c2\u6570\u63a8\u65ad\u6307\u5357 (C++17) \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0cCTAD\u53ef\u4ee5\u5f00\u7bb1\u5373\u7528\u3002\u7136\u800c\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u9700\u8981\u4e00\u4e9b\u989d\u5916\u7684\u5e2e\u52a9\uff0c\u4ee5\u7406\u89e3\u5982\u4f55\u6b63\u786e\u5730\u63a8\u5bfc\u6a21\u677f\u5b9e\u53c2\u3002 \u4f60\u53ef\u80fd\u4f1a\u60ca\u8bb6\u5730\u53d1\u73b0\u4e0b\u9762\u7684\u7a0b\u5e8f(\u5b83\u51e0\u4e4e\u4e0e\u4e0a\u9762\u4f7f\u7528 std::pair \u7684\u4f8b\u5b50\u76f8\u540c)\u4e0d\u80fd\u5728C++ 17\u4e2d\u7f16\u8bd1\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // define our own Pair type template < typename T , typename U > struct Pair { T first {}; U second {}; }; int main () { Pair < int , int > p1 { 1 , 2 }; // ok: we're explicitly specifying the template arguments Pair p2 { 1 , 2 }; // compile error in C++17 return 0 ; } \u5982\u679c\u4f60\u5728C++17\u4e2d\u7f16\u8bd1\u5b83\uff0c\u4f60\u53ef\u80fd\u4f1a\u5f97\u5230\u4e00\u4e9b\u5173\u4e8e\u201c\u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u5bfc\u5931\u8d25\u201d\u6216\u201c\u65e0\u6cd5\u63a8\u5bfc\u6a21\u677f\u5b9e\u53c2\u201d\u6216\u201c\u6ca1\u6709\u53ef\u884c\u7684\u6784\u9020\u51fd\u6570\u6216\u63a8\u5bfc\u6307\u5357\u201d\u7684\u9519\u8bef\u3002\u8fd9\u662f\u56e0\u4e3a\u5728C++17\u4e2d\uff0cCTAD\u4e0d\u77e5\u9053\u5982\u4f55\u63a8\u5bfc\u805a\u5408\u7c7b\u6a21\u677f\u7684\u6a21\u677f\u5b9e\u53c2\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u7f16\u8bd1\u5668\u63d0\u4f9b\u6307\u5357\uff0c\u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u4f55\u63a8\u65ad\u7ed9\u5b9a\u7c7b\u6a21\u677f\u7684\u6a21\u677f\u5b9e\u53c2\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u4e3a\u7f16\u8bd1\u5668\u63d0\u4f9b\u4e86\u63a8\u65ad\u6307\u5357\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 template < typename T , typename U > struct Pair { T first {}; U second {}; }; // \u6b64\u5904\u4e3a Pair \u63d0\u4f9b\u63a8\u65ad\u6307\u5357 // \u4f7f\u7528\u53c2\u6570 T \u548c U \u521d\u59cb\u5316\u7684Pair\u5bf9\u8c61\u5e94\u8be5\u63a8\u65ad\u4e3a Pair template < typename T , typename U > Pair ( T , U ) -> Pair < T , U > ; int main () { Pair < int , int > p1 { 1 , 2 }; // \u663e\u5f0f\u6307\u5b9a\u7c7b\u6a21\u677fPair (C++11 onward) Pair p2 { 1 , 2 }; // CTAD \u4f7f\u7528\u521d\u59cb\u5316\u503c\u63a8\u65ad Pair (C++17) return 0 ; } \u4e0a\u9762\u7684\u4f8b\u5b50\u53ef\u4ee5\u5728C++17\u4e2d\u6210\u529f\u7f16\u8bd1\u3002 \u6b64\u5904 Pair \u7c7b\u7684\u7c7b\u578b\u63a8\u65ad\u6307\u5357\u5f88\u7b80\u5355\uff0c\u8ba9\u6211\u4eec\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u5427\u3002 1 2 3 4 // \u6b64\u5904\u4e3a Pair \u63d0\u4f9b\u63a8\u65ad\u6307\u5357 // \u4f7f\u7528\u53c2\u6570 T \u548c U \u521d\u59cb\u5316\u7684Pair\u5bf9\u8c61\u5e94\u8be5\u63a8\u65ad\u4e3a Pair template < typename T , typename U > Pair ( T , U ) -> Pair < T , U > ; \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u548c Pair \u7c7b\u4e00\u6837\u7684\u6a21\u677f\u5b9a\u4e49\uff0c\u56e0\u4e3a\u63a8\u65ad\u5b9a\u4e49\u7684\u76ee\u7684\u5c31\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u4f55\u63a8\u65ad Pair \u7c7b\u578b\u3002\u63a5\u4e0b\u6765\uff0c\u5728\u7bad\u5934\u7b26\u53f7\u7684\u53f3\u4fa7\uff0c\u6211\u4eec\u63d0\u4f9b\u4e86\u7528\u4e8e\u5e2e\u52a9\u7f16\u8bd1\u5668\u8fdb\u884c\u63a8\u65ad\u7684\u7c7b\u578b\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5e0c\u671b\u7f16\u8bd1\u5668\u80fd\u591f\u4e3a Pair \u7c7b\u578b\u7684\u5bf9\u8c61\u8fdb\u884c\u6a21\u677f\u7c7b\u578b\u63a8\u65ad\u3002\u6700\u540e\uff0c\u5728\u7bad\u5934\u7b26\u53f7\u7684\u5de6\u4fa7\uff0c\u6211\u4eec\u544a\u8bc9\u7f16\u8bd1\u5668\u5e94\u8be5\u5173\u6ce8\u4ec0\u4e48\u6837\u7684\u58f0\u660e\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7f16\u8bd1\u5668\u88ab\u8981\u6c42\u5173\u6ce8\u540d\u4e3a Pair \u4e14\u6709\u4e24\u4e2a\u5f62\u53c2(\u4e00\u4e2a\u662f T \u7c7b\u578b\uff0c\u4e00\u4e2a\u662f U \u7c7b\u578b)\u7684\u58f0\u660e\u3002\u8fd9\u91cc\u4e5f\u53ef\u4ee5\u5199\u4f5c Pair(T t, U u) ( t \u548c u \u662f\u53c2\u6570\u7684\u540d\u5b57\uff0c\u4f46\u662f\u56e0\u4e3a\u6211\u4eec\u4e0d\u9700\u8981\u4f7f\u7528 t \u548c u \uff0c\u6240\u4ee5\u4e5f\u6ca1\u5fc5\u8981\u7ed9\u5b83\u4eec\u4e00\u4e2a\u540d\u5b57)\u3002 \u4e0a\u8ff0\u64cd\u4f5c\u7efc\u5408\u8d77\u6765\uff0c\u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u679c\u770b\u5230\u6709\u4e00\u4e2a Pair \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u4e14\u6709\u4e24\u4e2a \u5b9e\u53c2 \uff0c\u5219\u5e94\u8be5\u5c06\u5176\u63a8\u65ad\u4e3a\u7c7b\u578b Pair \u3002 \u6240\u4ee5\u5f53\u7f16\u8bd1\u5668\u770b\u5230 Pair p2{ 1, 2 }; \u65f6\uff0c\u5b83\u4f1a\u8bf4\uff1a\u201c\u5662\uff0c\u8fd9\u662f\u4e00\u4e2a Pair \u5bf9\u8c61\uff0c\u6709\u4e24\u4e2a\u5b9e\u53c2 int \u548c int \uff0c\u6240\u4ee5\u57fa\u4e8e\u63a8\u65ad\u6307\u5357\uff0c\u6211\u5e94\u8be5\u5c06\u5176\u63a8\u65ad\u4e3a\u7c7b\u578b Pair \u201d \u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u63a5\u53d7\u5355\u4e00\u6a21\u677f\u7c7b\u578b\u7684 Pair \u7684\u7c7b\u4f3c\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 template < typename T > struct Pair { T first {}; T second {}; }; // Here's a deduction guide for our Pair // pair objects initialized with arguments of type T and T should deduce to Pair template < typename T > Pair ( T , T ) -> Pair < T > ; int main () { Pair < int > p1 { 1 , 2 }; // explicitly specify class template Pair (C++11 onward) Pair p2 { 1 , 2 }; // CTAD used to deduce Pair from the initializers (C++17) return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7c7b\u578b\u63a8\u65ad\u6307\u5357\u5c06 Pair(T, T) ( Pair \u7c7b\u578b\u4e14\u6709\u4e24\u4e2a\u7c7b\u578b\u4e3aT\u7684\u53c2\u6570 T ) \u63a8\u65ad\u4e3a\u7c7b\u578b Pair \u3002 \u4f5c\u8005\u6ce8 \u5173\u4e8e\u7c7b\u578b\u63a8\u65ad\u6307\u5357\u7684\u4e00\u4e9b\u5907\u6ce8\u3002 \u9996\u5148\uff0c std::pair (\u4ee5\u53ca\u5176\u4ed6\u6807\u51c6\u5e93\u6a21\u677f\u7c7b\u578b)\u662f\u6709\u9884\u5b9a\u4e49\u7684\u7c7b\u578b\u63a8\u65ad\u6307\u5357\u7684\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u4f7f\u7528 std::pair \u7684\u4f8b\u5b50\u662f\u53ef\u4ee5\u5728 C++17 \u4e2d\u76f4\u63a5\u7f16\u8bd1\u7684\u3002 \u5176\u6b21\uff0cC++20 \u4e3a\u7f16\u8bd1\u5668\u63d0\u4f9b\u4e86\u4e3a\u805a\u5408\u7c7b\u578b\u81ea\u52a8\u751f\u6210\u7c7b\u578b\u63a8\u65ad\u6307\u5357\u7684\u80fd\u529b\uff0c\u56e0\u6b64\u524d\u9762\u4f7f\u7528 Pair \u7684\u4f8b\u5b50\u53ef\u4ee5\u5728C++20\u7f16\u8bd1\u5668\u4e2d\u7f16\u8bd1\u3002\u8fd9\u91cc\u5047\u8bbe\u4f60\u7684\u7f16\u8bd1\u5668\u652f\u6301P1816\u7279\u6027\uff0c\u5728\u7f16\u5199\u672c\u6587\u65f6\uff0cgcc\u548cVisual Studio\u652f\u6301P1816\u7279\u6027\uff0c\u800cClang\u4e0d\u652f\u6301\u3002","title":"10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD"},{"location":"10-11-class-template-argument-deduction-and-deduction%20-guides/#ctad-c17","text":"\u4eceC++ 17\u5f00\u59cb\uff0c\u5f53\u4ece\u7c7b\u6a21\u677f\u5b9e\u4f8b\u5316\u4e00\u4e2a\u5bf9\u8c61\u65f6\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u4ece\u5bf9\u8c61\u7684\u521d\u59cb\u5316\u5f0f\u7684\u7c7b\u578b\u63a8\u65ad\u51fa\u6a21\u677f\u7c7b\u578b( \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad \uff0c\u7b80\u79f0CTAD)\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 #include // for std::pair int main () { std :: pair < int , int > p1 { 1 , 2 }; // explicitly specify class template std::pair (C++11 onward) std :: pair p2 { 1 , 2 }; // CTAD used to deduce std::pair from the initializers (C++17) return 0 ; } CTAD \u53ea\u6709\u5728\u7c7b\u6a21\u677f\u5217\u8868\u4e2d\u6ca1\u6709\u63d0\u4f9b\u4efb\u4f55\u53c2\u6570\u65f6\u624d\u4f1a\u8fdb\u884c\uff0c\u56e0\u6b64\u4e0b\u9762\u4ee3\u7801\u4e2d\u7684\u4e24\u79cd\u65b9\u5f0f\u90fd\u662f\u9519\u8bef\u7684\uff1a 1 2 3 4 5 6 7 8 9 #include // for std::pair int main () { std :: pair <> p1 { 1 , 2 }; // \u9519\u8bef: \u6a21\u677f\u53c2\u6570\u592a\u5c11, \u4e24\u4e2a\u53c2\u6570\u90fd\u4e0d\u4f1a\u63a8\u65ad std :: pair < int > p2 { 3 , 4 }; // \u9519\u8bef: \u6a21\u677f\u53c2\u6570\u592a\u5c11,\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e0d\u4f1a\u88ab\u63a8\u65ad return 0 ; } \u4f5c\u8005\u6ce8 \u672c\u7f51\u7ad9\u4eca\u540e\u7684\u8bb8\u591a\u8bfe\u7a0b\u90fd\u5229\u7528\u4e86CTAD\u3002\u5982\u679c\u4f7f\u7528C++14\u6807\u51c6\u7f16\u8bd1\u8fd9\u4e9b\u793a\u4f8b\uff0c\u5c06\u4f1a\u5f97\u5230\u4e00\u4e2a\u5173\u4e8e\u7f3a\u5c11\u6a21\u677f\u53c2\u6570\u7684\u9519\u8bef\u3002\u60a8\u9700\u8981\u663e\u5f0f\u5730\u5c06\u8fd9\u4e9b\u53c2\u6570\u6dfb\u52a0\u5230\u793a\u4f8b\u4e2d\uff0c\u4ee5\u4f7f\u5176\u80fd\u591f\u7f16\u8bd1\u3002","title":"\u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad (CTAD)\u00a0(C++17)"},{"location":"10-11-class-template-argument-deduction-and-deduction%20-guides/#c17","text":"\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0cCTAD\u53ef\u4ee5\u5f00\u7bb1\u5373\u7528\u3002\u7136\u800c\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u9700\u8981\u4e00\u4e9b\u989d\u5916\u7684\u5e2e\u52a9\uff0c\u4ee5\u7406\u89e3\u5982\u4f55\u6b63\u786e\u5730\u63a8\u5bfc\u6a21\u677f\u5b9e\u53c2\u3002 \u4f60\u53ef\u80fd\u4f1a\u60ca\u8bb6\u5730\u53d1\u73b0\u4e0b\u9762\u7684\u7a0b\u5e8f(\u5b83\u51e0\u4e4e\u4e0e\u4e0a\u9762\u4f7f\u7528 std::pair \u7684\u4f8b\u5b50\u76f8\u540c)\u4e0d\u80fd\u5728C++ 17\u4e2d\u7f16\u8bd1\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // define our own Pair type template < typename T , typename U > struct Pair { T first {}; U second {}; }; int main () { Pair < int , int > p1 { 1 , 2 }; // ok: we're explicitly specifying the template arguments Pair p2 { 1 , 2 }; // compile error in C++17 return 0 ; } \u5982\u679c\u4f60\u5728C++17\u4e2d\u7f16\u8bd1\u5b83\uff0c\u4f60\u53ef\u80fd\u4f1a\u5f97\u5230\u4e00\u4e9b\u5173\u4e8e\u201c\u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u5bfc\u5931\u8d25\u201d\u6216\u201c\u65e0\u6cd5\u63a8\u5bfc\u6a21\u677f\u5b9e\u53c2\u201d\u6216\u201c\u6ca1\u6709\u53ef\u884c\u7684\u6784\u9020\u51fd\u6570\u6216\u63a8\u5bfc\u6307\u5357\u201d\u7684\u9519\u8bef\u3002\u8fd9\u662f\u56e0\u4e3a\u5728C++17\u4e2d\uff0cCTAD\u4e0d\u77e5\u9053\u5982\u4f55\u63a8\u5bfc\u805a\u5408\u7c7b\u6a21\u677f\u7684\u6a21\u677f\u5b9e\u53c2\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u7f16\u8bd1\u5668\u63d0\u4f9b\u6307\u5357\uff0c\u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u4f55\u63a8\u65ad\u7ed9\u5b9a\u7c7b\u6a21\u677f\u7684\u6a21\u677f\u5b9e\u53c2\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u4e3a\u7f16\u8bd1\u5668\u63d0\u4f9b\u4e86\u63a8\u65ad\u6307\u5357\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 template < typename T , typename U > struct Pair { T first {}; U second {}; }; // \u6b64\u5904\u4e3a Pair \u63d0\u4f9b\u63a8\u65ad\u6307\u5357 // \u4f7f\u7528\u53c2\u6570 T \u548c U \u521d\u59cb\u5316\u7684Pair\u5bf9\u8c61\u5e94\u8be5\u63a8\u65ad\u4e3a Pair template < typename T , typename U > Pair ( T , U ) -> Pair < T , U > ; int main () { Pair < int , int > p1 { 1 , 2 }; // \u663e\u5f0f\u6307\u5b9a\u7c7b\u6a21\u677fPair (C++11 onward) Pair p2 { 1 , 2 }; // CTAD \u4f7f\u7528\u521d\u59cb\u5316\u503c\u63a8\u65ad Pair (C++17) return 0 ; } \u4e0a\u9762\u7684\u4f8b\u5b50\u53ef\u4ee5\u5728C++17\u4e2d\u6210\u529f\u7f16\u8bd1\u3002 \u6b64\u5904 Pair \u7c7b\u7684\u7c7b\u578b\u63a8\u65ad\u6307\u5357\u5f88\u7b80\u5355\uff0c\u8ba9\u6211\u4eec\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u5427\u3002 1 2 3 4 // \u6b64\u5904\u4e3a Pair \u63d0\u4f9b\u63a8\u65ad\u6307\u5357 // \u4f7f\u7528\u53c2\u6570 T \u548c U \u521d\u59cb\u5316\u7684Pair\u5bf9\u8c61\u5e94\u8be5\u63a8\u65ad\u4e3a Pair template < typename T , typename U > Pair ( T , U ) -> Pair < T , U > ; \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u548c Pair \u7c7b\u4e00\u6837\u7684\u6a21\u677f\u5b9a\u4e49\uff0c\u56e0\u4e3a\u63a8\u65ad\u5b9a\u4e49\u7684\u76ee\u7684\u5c31\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u4f55\u63a8\u65ad Pair \u7c7b\u578b\u3002\u63a5\u4e0b\u6765\uff0c\u5728\u7bad\u5934\u7b26\u53f7\u7684\u53f3\u4fa7\uff0c\u6211\u4eec\u63d0\u4f9b\u4e86\u7528\u4e8e\u5e2e\u52a9\u7f16\u8bd1\u5668\u8fdb\u884c\u63a8\u65ad\u7684\u7c7b\u578b\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5e0c\u671b\u7f16\u8bd1\u5668\u80fd\u591f\u4e3a Pair \u7c7b\u578b\u7684\u5bf9\u8c61\u8fdb\u884c\u6a21\u677f\u7c7b\u578b\u63a8\u65ad\u3002\u6700\u540e\uff0c\u5728\u7bad\u5934\u7b26\u53f7\u7684\u5de6\u4fa7\uff0c\u6211\u4eec\u544a\u8bc9\u7f16\u8bd1\u5668\u5e94\u8be5\u5173\u6ce8\u4ec0\u4e48\u6837\u7684\u58f0\u660e\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7f16\u8bd1\u5668\u88ab\u8981\u6c42\u5173\u6ce8\u540d\u4e3a Pair \u4e14\u6709\u4e24\u4e2a\u5f62\u53c2(\u4e00\u4e2a\u662f T \u7c7b\u578b\uff0c\u4e00\u4e2a\u662f U \u7c7b\u578b)\u7684\u58f0\u660e\u3002\u8fd9\u91cc\u4e5f\u53ef\u4ee5\u5199\u4f5c Pair(T t, U u) ( t \u548c u \u662f\u53c2\u6570\u7684\u540d\u5b57\uff0c\u4f46\u662f\u56e0\u4e3a\u6211\u4eec\u4e0d\u9700\u8981\u4f7f\u7528 t \u548c u \uff0c\u6240\u4ee5\u4e5f\u6ca1\u5fc5\u8981\u7ed9\u5b83\u4eec\u4e00\u4e2a\u540d\u5b57)\u3002 \u4e0a\u8ff0\u64cd\u4f5c\u7efc\u5408\u8d77\u6765\uff0c\u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u679c\u770b\u5230\u6709\u4e00\u4e2a Pair \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u4e14\u6709\u4e24\u4e2a \u5b9e\u53c2 \uff0c\u5219\u5e94\u8be5\u5c06\u5176\u63a8\u65ad\u4e3a\u7c7b\u578b Pair \u3002 \u6240\u4ee5\u5f53\u7f16\u8bd1\u5668\u770b\u5230 Pair p2{ 1, 2 }; \u65f6\uff0c\u5b83\u4f1a\u8bf4\uff1a\u201c\u5662\uff0c\u8fd9\u662f\u4e00\u4e2a Pair \u5bf9\u8c61\uff0c\u6709\u4e24\u4e2a\u5b9e\u53c2 int \u548c int \uff0c\u6240\u4ee5\u57fa\u4e8e\u63a8\u65ad\u6307\u5357\uff0c\u6211\u5e94\u8be5\u5c06\u5176\u63a8\u65ad\u4e3a\u7c7b\u578b Pair \u201d \u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u63a5\u53d7\u5355\u4e00\u6a21\u677f\u7c7b\u578b\u7684 Pair \u7684\u7c7b\u4f3c\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 template < typename T > struct Pair { T first {}; T second {}; }; // Here's a deduction guide for our Pair // pair objects initialized with arguments of type T and T should deduce to Pair template < typename T > Pair ( T , T ) -> Pair < T > ; int main () { Pair < int > p1 { 1 , 2 }; // explicitly specify class template Pair (C++11 onward) Pair p2 { 1 , 2 }; // CTAD used to deduce Pair from the initializers (C++17) return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7c7b\u578b\u63a8\u65ad\u6307\u5357\u5c06 Pair(T, T) ( Pair \u7c7b\u578b\u4e14\u6709\u4e24\u4e2a\u7c7b\u578b\u4e3aT\u7684\u53c2\u6570 T ) \u63a8\u65ad\u4e3a\u7c7b\u578b Pair \u3002 \u4f5c\u8005\u6ce8 \u5173\u4e8e\u7c7b\u578b\u63a8\u65ad\u6307\u5357\u7684\u4e00\u4e9b\u5907\u6ce8\u3002 \u9996\u5148\uff0c std::pair (\u4ee5\u53ca\u5176\u4ed6\u6807\u51c6\u5e93\u6a21\u677f\u7c7b\u578b)\u662f\u6709\u9884\u5b9a\u4e49\u7684\u7c7b\u578b\u63a8\u65ad\u6307\u5357\u7684\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u4f7f\u7528 std::pair \u7684\u4f8b\u5b50\u662f\u53ef\u4ee5\u5728 C++17 \u4e2d\u76f4\u63a5\u7f16\u8bd1\u7684\u3002 \u5176\u6b21\uff0cC++20 \u4e3a\u7f16\u8bd1\u5668\u63d0\u4f9b\u4e86\u4e3a\u805a\u5408\u7c7b\u578b\u81ea\u52a8\u751f\u6210\u7c7b\u578b\u63a8\u65ad\u6307\u5357\u7684\u80fd\u529b\uff0c\u56e0\u6b64\u524d\u9762\u4f7f\u7528 Pair \u7684\u4f8b\u5b50\u53ef\u4ee5\u5728C++20\u7f16\u8bd1\u5668\u4e2d\u7f16\u8bd1\u3002\u8fd9\u91cc\u5047\u8bbe\u4f60\u7684\u7f16\u8bd1\u5668\u652f\u6301P1816\u7279\u6027\uff0c\u5728\u7f16\u5199\u672c\u6587\u65f6\uff0cgcc\u548cVisual Studio\u652f\u6301P1816\u7279\u6027\uff0c\u800cClang\u4e0d\u652f\u6301\u3002","title":"\u6a21\u677f\u53c2\u6570\u63a8\u65ad\u6307\u5357 (C++17)"},{"location":"10-2-unscoped-enumerations/","tags":["enum","unscoped enumerations"],"text":"Key Takeaway \u4f7f\u7528 enum \u5173\u952e\u5b57\u5b9a\u4e49\u7684\u679a\u4e3e\u662f \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \uff0c\u5b83\u7684\u679a\u4e3e\u503c\u88ab\u66b4\u9732\u5728\u548c\u679a\u4e3e\u7c7b\u578b\u5b9a\u4e49\u7684\u4f5c\u7528\u57df\u4e2d\uff0c\u56e0\u6b64\u66f4\u9002\u5408\u88ab\u76f4\u63a5\u5b9a\u4e49\u5728\u4f7f\u7528\u5b83\u7684\u7c7b\u4e2d \u4e00\u4e2a\u679a\u4e3e\u7c7b\u578b\u7684\u679a\u4e3e\u503c\u4e0d\u80fd\u7528\u4e8e\u53e6\u4e00\u4e2a\u679a\u4e3e\u7c7b\u578b\u7684\u5bf9\u8c61 C++ \u4e2d\u5185\u7f6e\u4e86\u5f88\u591a\u6709\u7528\u7684\u57fa\u7840\u6570\u636e\u7c7b\u578b\u548c\u7b26\u5408\u7c7b\u578b\uff08\u5206\u522b\u5728 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb \u548c 9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b \u4e2d\u8fdb\u884c\u4e86\u4ecb\u7ecd\uff09\u3002\u4f46\u662f\u8fd9\u4e9b\u5185\u7f6e\u6570\u636e\u7c7b\u578b\u5e76\u4e0d\u662f\u603b\u80fd\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u8981\u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u7684\u7a0b\u5e8f\u9700\u8981\u8ffd\u8e2a\u4e00\u4e2a\u82f9\u679c\u7684\u989c\u8272\u662f\u7ea2\u8272\u3001\u9ec4\u8272\u8fd8\u662f\u7eff\u8272\u3002\u4ea6\u6216\u8005\u6211\u4eec\u9700\u8981\u63cf\u8ff0\u4e00\u4ef6T\u6064\u7684\u53ef\u9009\u989c\u8272\uff08\u901a\u8fc7\u4e00\u4e2a\u989c\u8272\u5217\u8868\uff09\u3002\u5982\u679c\u53ea\u80fd\u4f7f\u7528\u57fa\u7840\u7c7b\u578b\u7684\u8bdd\uff0c\u4f60\u4f1a\u600e\u4e48\u505a\uff1f \u4f60\u53ef\u80fd\u4f1a\u628a\u989c\u8272\u5b58\u50a8\u4e3a\u6574\u578b\u503c\uff0c\u5e76\u9690\u5f0f\u5730\u5c06\u5176\u5173\u8054\u5230\u5bf9\u5e94\u7684\u989c\u8272(0 = red , 1 = green, 2 = blue)\uff1a 1 2 3 4 5 6 7 int main () { int appleColor { 0 }; // my apple is red int shirtColor { 1 }; // my shirt is green return 0 ; } \u4f46\u662f\u8fd9\u4e48\u505a\u5e76\u4e0d\u76f4\u89c2\uff0c\u800c\u4e14\u6211\u4eec\u8ba8\u8bba\u8fc7\u4e3a\u4ec0\u4e48\u4f7f\u7528\u9b54\u9b3c\u6570\u5b57\u662f\u4e0d\u5229\u7684\uff08 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \uff09\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7b26\u53f7\u5e38\u91cf\u6765\u907f\u514d\u9b54\u9b3c\u6570\u5b57\u7684\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 constexpr int red { 0 }; constexpr int green { 1 }; constexpr int blue { 2 }; int main () { int appleColor { red }; int shirtColor { green }; return 0 ; } \u5c3d\u7ba1\u53ef\u8bfb\u6027\u53d8\u597d\u4e86\u4e00\u4e9b\uff0c\u7a0b\u5e8f\u5458\u4ecd\u7136\u9700\u8981\u7406\u89e3 appleColor \u548c shirtColor ( int \u7c7b\u578b)\u5b58\u653e\u7684\u662f\u53e6\u5916\u4e00\u4e9b\u8868\u793a\u989c\u8272\u7684\u7b26\u53f7\u5e38\u91cf\uff08\u800c\u8fd9\u4e9b\u5e38\u91cf\u5f88\u53ef\u80fd\u88ab\u5b9a\u4e49\u5728\u5176\u4ed6\u5730\u65b9\uff0c\u751a\u81f3\u662f\u5176\u4ed6\u6587\u4ef6\u4e2d\uff09\u3002 \u4f7f\u7528 \u522b\u540d \u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u53ef\u8bfb\u6027\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 using Color = int ; // define a type alias named Color // The following color values should be used for a Color constexpr Color red { 0 }; constexpr Color green { 1 }; constexpr Color blue { 2 }; int main () { Color appleColor { red }; Color shirtColor { green }; return 0 ; } \u53ef\u8bfb\u6027\u66f4\u597d\u4e86\u3002\u4f46\u662f\u7a0b\u5e8f\u5458\u4ecd\u7136\u9700\u8981\u7406\u89e3\uff0c\u8fd9\u4e9b\u8868\u793a\u989c\u8272\u7684\u7b26\u53f7\u5e38\u91cf\u9700\u8981\u914d\u5408 Color \u7c7b\u578b\u7684\u53d8\u91cf\u6765\u4f7f\u7528\u3002\u4e0d\u8fc7\u81f3\u5c11\u73b0\u5728\u8fd9\u4e9b\u7c7b\u578b\u5177\u6709\u4e00\u4e2a\u552f\u4e00\u7684\u522b\u540d Color \uff0c\u6211\u4eec\u53ef\u4ee5\u641c\u7d22\u5b83\u5e76\u627e\u5230\u76f8\u5173\u7684\u7b26\u53f7\u5e38\u91cf\u5b9a\u4e49\u3002 \u4f46\u662f\uff0c\u56e0\u4e3a Color \u53ea\u662f int \u7684\u522b\u540d\uff0c\u6240\u4ee5\u6211\u4eec\u4ecd\u7136\u65e0\u6cd5\u4fdd\u8bc1\u8fd9\u4e9b\u7b26\u53f7\u5e38\u91cf\u88ab\u6b63\u786e\u5730\u4f7f\u7528\u3002\u6211\u4eec\u4ecd\u7136\u7531\u53ef\u80fd\u72af\u5982\u4e0b\u7684\u9519\u8bef\u3002 1 Color eyeColor { 8 }; // \u8bed\u6cd5\u6b63\u786e\uff0c\u4f46\u8bed\u4e49\u9519\u8bef\uff088\u4e0d\u662f\u4e00\u4e2a\u88ab\u5b9a\u4e49\u7684\u989c\u8272\u503c\uff09 \u4e0d\u4ec5\u5982\u6b64\uff0c\u5f53\u6211\u4eec\u8c03\u8bd5\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u8fd9\u4e9b\u53d8\u91cf\u90fd\u4f1a\u663e\u793a\u4e3a\u6574\u578b\u503c\uff0c\u800c\u4e0d\u662f\u5176\u7b26\u53f7\u542b\u4e49 ( red )\uff0c\u8fd9\u65e0\u7591\u4f1a\u8ba9debug\u53d8\u5f97\u66f4\u52a0\u56f0\u96be\u3002 \u5e78\u8fd0\u5730\u662f\uff0c\u6211\u4eec\u8fd8\u6709\u66f4\u597d\u7684\u529e\u6cd5\u3002 \u679a\u4e3e \u679a\u4e3e(\u4e5f\u79f0\u4e3a\u679a\u4e3e\u7c7b\u578b\u548c enum ) \u662f\u4e00\u79cd\u590d\u5408\u7c7b\u578b\u3002\u5728\u679a\u4e3e\u7c7b\u578b\u4e2d\uff0c\u6bcf\u4e2a\u53ef\u80fd\u7684\u503c\u90fd\u88ab\u5b9a\u4e49\u4e3a\u4e00\u4e2a \u7b26\u53f7\u5e38\u91cf \uff08\u79f0\u4e3a\u679a\u4e3e\u503c\uff09\u3002 \u56e0\u4e3a\u679a\u4e3e\u5c5e\u4e8e\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\uff08 10.1 - \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7b80\u4ecb \uff09\uff0c\u6240\u4ee5\u679a\u4e3e\u7c7b\u578b\u5728\u4f7f\u7528\u524d\u5fc5\u987b\u5148\u8fdb\u884c\u5b9a\u4e49\u3002 C++ \u652f\u6301\u4e24\u79cd\u7c7b\u578b\u7684\u679a\u4e3e\uff1a 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u548c 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u901a\u8fc7 enum \u5173\u952e\u5b57\u5b9a\u4e49\u3002 \u679a\u4e3e\u7c7b\u578b\u901a\u8fc7\u5b9e\u4f8b\u6765\u5b66\u4e60\u6548\u679c\u66f4\u597d\u3002\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u6765\u8868\u793a\u989c\u8272\u503c\u5427\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // \u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\uff0c\u540d\u4e3a Color enum Color { // \u679a\u4e3e\u503c\u5b9a\u4e49\u5728\u8fd9\u91cc // \u8fd9\u4e9b\u7b26\u53f7\u5e38\u91cf\u5b9a\u4e49\u4e86\u6240\u6709\u53ef\u80fd\u7684\u503c // \u679a\u4e3e\u503c\u901a\u8fc7\u9017\u53f7\u5206\u5272\uff0c\u800c\u4e0d\u662f\u5206\u53f7 red , green , blue , // \u7ed3\u5c3e\u7684\u9017\u53f7\u53ef\u6709\u53ef\u65e0\uff0c\u4f46\u662f\u63a8\u8350\u5199\u4e0a }; // \u679a\u4e3e\u5b9a\u4e49\u5fc5\u987b\u4ee5\u5206\u53f7\u7ed3\u5c3e int main () { // \u5b9a\u4e49\u4e00\u4e9bColor\u7c7b\u578b\u7684\u53d8\u91cf Color apple { red }; // my apple is red Color shirt { green }; // my shirt is green Color cup { blue }; // my cup is blue Color socks { white }; // \u9519\u8bef: white \u4e0d\u662f\u53ef\u7528\u7684\u679a\u4e3e\u503c Color hat { 2 }; // \u9519\u8bef: 2 \u9875\u4e0d\u662fColor\u7684\u679a\u4e3e\u503c return 0 ; } \u6211\u4eec\u9996\u5148\u4f7f\u7528 enum \u5173\u952e\u5b57\u6765\u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u6211\u4eec\u6b63\u5728\u5b9a\u4e49\u4e00\u4e2a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\uff0c\u6211\u4eec\u5c06\u5176\u547d\u540d\u4e3a Color \u3002 \u968f\u540e\uff0c\u5728\u82b1\u62ec\u53f7\u91cc\u9762\u5b9a\u4e49 Color \u7c7b\u578b\u7684\u679a\u4e3e\u503c\uff1a red \uff0c green \u548c blue \u3002\u8fd9\u4e9b\u679a\u4e3e\u503c\u6307\u5b9a\u4e86 Color \u7c7b\u578b\u80fd\u591f\u8868\u793a\u6216\u5b58\u50a8\u7684\u6240\u6709\u53ef\u80fd\u503c\u3002\u6bcf\u4e2a\u679a\u4e3e\u503c\u9700\u8981\u901a\u8fc7\u9017\u53f7\uff08\u800c\u4e0d\u662f\u5206\u53f7\uff09\u9694\u5f00\u2014\u2014\u6700\u540e\u4e00\u4e2a\u679a\u4e3e\u503c\u540e\u9762\u53ef\u4ee5\u6709\u9017\u53f7\u4e5f\u53ef\u4ee5\u6ca1\u6709\uff0c\u52a0\u4e0a\u9017\u53f7\u770b\u8d77\u6765\u4e00\u81f4\u6027\u66f4\u597d\u3002 Color \u7684\u5b9a\u4e49\u4ee5\u5206\u53f7\u7ed3\u5c3e\u3002\u81f3\u6b64\uff0c\u679a\u4e3e\u7c7b\u578b Color \u5c31\u5b9a\u4e49\u5b8c\u6210\u4e86\uff01 \u5728 main() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u521d\u59cb\u5316\u4e86\u4e09\u4e2a Color \u7c7b\u578b\u7684\u53d8\u91cf\uff1a apple \u88ab\u521d\u59cb\u5316\u4e3a red \uff0c shirt \u521d\u59cb\u5316\u4e3a green \uff0c cup \u521d\u59cb\u5316\u4e3a blue \u3002 \u7a0b\u5e8f\u4f1a\u4e3a\u8fd9\u4e09\u4e2a\u5bf9\u8c61\u5206\u914d\u5185\u5b58\u3002\u6ce8\u610f\uff0c\u5f53\u521d\u59cb\u5316\u679a\u4e3e\u7c7b\u578b\u65f6\uff0c\u6240\u7528\u7684\u521d\u59cb\u5316\u503c\u5fc5\u987b\u662f\u8be5\u7c7b\u578b\u5b9a\u4e49\u8fc7\u7684\u679a\u4e3e\u503c\u3002\u53d8\u91cf socks \u548c hat \u5e76\u4e0d\u80fd\u6210\u529f\u5b9a\u4e49\uff0c\u56e0\u4e3a white \u548c 2 \u5e76\u4e0d\u662f Color \u7684\u679a\u4e3e\u503c\u3002 \u4f5c\u8005\u6ce8 \u547d\u540d\u89c4\u5219\u5c0f\u7ed3: - \u679a\u4e3e\u548c\u679a\u4e3e\u7c7b\u578b\u6307\u7684\u662f\u7c7b\u578b\u672c\u8eab(\u4f8b\u5982 Color ) - \u679a\u4e3e\u503c\u6307\u7684\u662f\u679a\u4e3e\u4e2d\u53ef\u80fd\u503c\u6240\u5bf9\u5e94\u7684\u7b26\u53f7\u5e38\u91cf(\u4f8b\u5982 red ) \u679a\u4e3e\u7c7b\u578b\u548c\u679a\u4e3e\u503c\u7684\u547d\u540d \u6309\u7167\u60ef\u4f8b\uff0c\u679a\u4e3e\u7c7b\u578b\u7684\u540d\u5b57\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff08\u89c4\u5219\u9002\u7528\u4e8e\u6240\u6709\u81ea\u5b9a\u4e49\u7c7b\u578b\uff09\u3002 \u6ce8\u610f \u679a\u4e3e\u7c7b\u578b\u4e0d\u4e00\u5b9a\u8981\u6709\u540d\u5b57\uff0c\u4f46\u662f\u73b0\u4ee3C++\u63a8\u8350\u907f\u514d\u4f7f\u7528\u533f\u540d\u7684\u679a\u4e3e\u3002 \u679a\u4e3e\u503c\u5fc5\u987b\u6709\u540d\u5b57\u3002\u53ef\u60dc\u7684\u662f\uff0c\u5bf9\u4e8e\u679a\u4e3e\u503c\u7684\u540d\u5b57\u6682\u65f6\u8fd8\u6ca1\u6709\u7ea6\u5b9a\u4fd7\u6210\u7684\u547d\u540d\u89c4\u8303\u3002\u901a\u5e38\u7684\u9009\u62e9\u6709\uff1a\u5c0f\u5199\u5b57\u6bcd\u5f00\u5934\uff08\u4f8b\u5982 red \uff09\uff0c\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff08\u4f8b\u5982 Red \uff0c\u5168\u5927\u5199\uff08\u4f8b\u5982 RED \uff09\u3001\u5168\u5927\u5199\u4e14\u6dfb\u52a0\u5c06\u679a\u4e3e\u7c7b\u578b\u4f5c\u4e3a\u524d\u7f00\uff08\u4f8b\u5982 COLOR_RED \uff09\uff0c\u518d\u6216\u8005\u4f7f\u7528 k \u4f5c\u4e3a\u524d\u7f00\u5e76\u914d\u5408\u5927\u5c0f\u5199\u4ea4\u66ff(kColorRed)\u3002 \u73b0\u4ee3C++\u6307\u5357\u901a\u5e38\u5efa\u8bae\u907f\u514d\u4f7f\u7528\u5168\u5927\u5199\u7684\u547d\u540d\u7ea6\u5b9a\uff0c\u56e0\u4e3a\u5168\u5927\u5199\u901a\u5e38\u7528\u4e8e\u9884\u5904\u7406\u5668\u5b8f\uff0c\u53ef\u80fd\u4f1a\u53d1\u751f\u51b2\u7a81\u3002\u6211\u4eec\u8fd8\u5efa\u8bae\u907f\u514d\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684\u7ea6\u5b9a\uff0c\u56e0\u4e3a\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684\u540d\u79f0\u901a\u5e38\u4e3a\u81ea\u5b9a\u4e49\u7c7b\u578b\u4fdd\u7559\u3002 \u6700\u4f73\u5b9e\u8df5 \u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u547d\u540d\u679a\u4e3e\u7c7b\u578b\u3002\u4ee5\u5c0f\u5199\u5b57\u6bcd\u5f00\u5934\u547d\u540d\u679a\u4e3e\u503c\u3002 \u679a\u4e3e\u7c7b\u578b\u662f\u53ef\u533a\u5206\u7c7b\u578b \u4f60\u521b\u5efa\u7684\u6bcf\u4e2a\u679a\u4e3e\u7c7b\u578b\u90fd\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u4e0d\u540c\u7684\u7c7b\u578b\uff0c\u8fd9\u610f\u5473\u7740\u7f16\u8bd1\u5668\u53ef\u4ee5\u5c06\u5176\u4e0e\u5176\u4ed6\u7c7b\u578b\u533a\u5206\u5f00\u6765(\u4e0d\u50cf typedefs \u6216\u7c7b\u578b\u522b\u540d\uff0c\u5b83\u4eec\u662f\u4e00\u79cd\u522b\u540d\uff0c\u4e0e\u5176\u5bf9\u5e94\u7684\u7c7b\u578b\u5e76\u6ca1\u6709\u533a\u522b)\u3002 \u679a\u4e3e\u7c7b\u578b\u662f\u4e0d\u540c\u7684\uff0c\u56e0\u6b64\u4e00\u79cd\u679a\u4e3e\u7c7b\u578b\u4e2d\u679a\u4e3e\u503c\u4e5f\u4e0d\u80fd\u7528\u4e8e\u53e6\u4e00\u79cd\u679a\u4e3e\u7c7b\u578b\u7684\u5bf9\u8c61: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 enum Pet { cat , dog , pig , whale , }; enum Color { black , red , blue , }; int main () { Pet myPet { black }; // \u7f16\u8bd1\u9519\u8bef: black \u4e0d\u662f Pet \u7684\u679a\u4e3e\u503c Color shirt { pig }; // \u7f16\u8bd1\u9519\u8bef: pig \u4e0d\u662f Color \u7684\u679a\u4e3e\u503c return 0 ; } \u4f60\u53ef\u80fd\u4e5f\u4e0d\u60f3\u8981\u4e00\u4ef6\u201dpig\u201c\u886c\u886b\u3002 \u4f7f\u7528\u679a\u4e3e \u56e0\u4e3a\u679a\u4e3e\u5668\u662f\u63cf\u8ff0\u6027\u7684\uff0c\u5b83\u4eec\u5bf9\u4e8e\u589e\u5f3a\u4ee3\u7801\u6587\u6863\u6027\u548c\u53ef\u8bfb\u6027\u975e\u5e38\u6709\u7528\u3002\u5f53\u4f60\u7684\u76f8\u5173\u5e38\u91cf\u7684\u96c6\u5408\u5f88\u5c0f\uff0c\u5e76\u4e14\u5bf9\u8c61\u6bcf\u6b21\u53ea\u9700\u8981\u4fdd\u5b58\u5176\u4e2d\u4e00\u4e2a\u503c\u65f6\uff0c\u679a\u4e3e\u7c7b\u578b\u662f\u6700\u5408\u9002\u7684\u3002 \u661f\u671f\u51e0\u3001\u65b9\u5411\u6216\u8005\u4e00\u526f\u724c\u4e2d\u7684\u82b1\u8272\u90fd\u662f\u5e38\u89c1\u7684\u679a\u4e3e\u5b9a\u4e49\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 enum DaysOfWeek { sunday , monday , tuesday , wednesday , thursday , friday , saturday , }; enum CardinalDirections { north , east , south , west , }; enum CardSuits { clubs , diamonds , hearts , spades , }; \u6709\u65f6\u5019\u88ab\u8c03\u51fd\u6570\u9700\u8981\u50cf\u4e3b\u8c03\u51fd\u6570\u8fd4\u56de\u72b6\u6001\u7801\u4ee5\u8868\u660e\u81ea\u5df1\u662f\u5426\u6267\u884c\u6210\u529f\u3002\u4f20\u7edf\u505a\u6cd5\u662f\u4f7f\u7528\u8d1f\u503c\u6765\u4ee3\u8868\u4e0d\u540c\u7684\u9519\u8bef\u7801\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 int readFileContents () { if ( ! openFile ()) return -1 ; if ( ! readFile ()) return -2 ; if ( ! parseFile ()) return -3 ; return 0 ; // success } \u4f46\u662f\uff0c\u8fd9\u4e9b\u9b54\u9b3c\u6570\u5b57\u542b\u4e49\u975e\u5e38\u6a21\u7cca\u3002\u66f4\u597d\u7684\u529e\u6cd5\u662f\u4f7f\u7528\u679a\u4e3e\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 enum FileReadResult { readResultSuccess , readResultErrorFileOpen , readResultErrorFileRead , readResultErrorFileParse , }; FileReadResult readFileContents () { if ( ! openFile ()) return readResultErrorFileOpen ; if ( ! readFile ()) return readResultErrorFileRead ; if ( ! parseFile ()) return readResultErrorFileParse ; return readResultSuccess ; } \u7136\u540e\u8c03\u7528\u8005\u53ef\u4ee5\u5c06\u8fd4\u56de\u503c\u548c\u7279\u5b9a\u7684\u679a\u4e3e\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u76f8\u5bf9\u4e8e\u76f4\u63a5\u548c\u6574\u578b\u6570\u6bd4\u8f83\uff0c\u8fd9\u79cd\u65b9\u6cd5\u53ef\u8bfb\u6027\u66f4\u597d\u3002 1 2 3 4 5 6 7 8 if ( readFileContents () == readResultSuccess ) { // do something } else { // print error message } \u679a\u4e3e\u7c7b\u578b\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6807\u8bc6\u7b80\u5355\u6e38\u620f\u4e2d\u7684\u9053\u5177\u3001\u602a\u7269\u6216\u5730\u5f62\u7c7b\u578b\u3002\u57fa\u672c\u4e0a\uff0c\u4efb\u4f55\u76f8\u5173\u5bf9\u8c61\u7684\u5c0f\u96c6\u5408\u90fd\u53ef\u4ee5\u4f7f\u7528\u679a\u4e3e\u8868\u793a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 enum ItemType { sword , torch , potion , }; int main () { ItemType holding { torch }; return 0 ; } \u679a\u4e3e\u7c7b\u578b\u4e5f\u53ef\u4ee5\u7528\u4e8e\u51fd\u6570\u5f62\u53c2\uff0c\u8868\u793a\u7528\u6237\u7684\u8f93\u5165\u7684\u4e0d\u540c\u9009\u9879\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 enum SortOrder { alphabetical , alphabeticalReverse , numerical , }; void sortData ( SortOrder order ) { if ( order == alphabetical ) // sort data in forwards alphabetical order else if ( order == alphabeticalReverse ) // sort data in backwards alphabetical order else if ( order == numerical ) // sort data numerically } \u5f88\u591a\u7f16\u7a0b\u8bed\u8a00\u4f1a\u4f7f\u7528\u679a\u4e3e\u6765\u5b9a\u4e49\u5e03\u5c14\u7c7b\u578b\u2014\u2014\u6bd5\u7adf\u5e03\u5c14\u7c7b\u578b\u672c\u8d28\u4e0a\u5c31\u662f\u4e00\u4e2a\u5177\u6709\u4e24\u4e2a\u679a\u4e3e\u503c\u7684\u679a\u4e3e\u7c7b\u578b\uff1a false \u548c true \uff01\u4f46\u662f\uff0cC++ \u4e2d\u7684 true \u548c false \u662f\u5173\u952e\u5b57\u800c\u4e0d\u662f\u679a\u4e3e\u503c\u3002 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u4f5c\u7528\u57df \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u4e4b\u6240\u4ee5\u88ab\u8d4b\u4e88\u8fd9\u6837\u7684\u540d\u5b57\uff0c \u662f\u56e0\u4e3a\u5b83\u4f1a\u5c06\u5176\u5b9a\u4e49\u7684\u679a\u4e3e\u503c\u66b4\u9732\u5728\u548c\u81ea\u5df1\u4e00\u6837\u7684\u4f5c\u7528\u57df\u4e2d \uff08\u800c\u4e0d\u662f\u50cf\u547d\u540d\u7a7a\u95f4\u90a3\u6837\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u4f5c\u7528\u57df\u7a7a\u95f4\uff09\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 enum Color // \u8be5\u679a\u4e3e\u7c7b\u578b\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u5b9a\u4e49 { red , // \u6240\u4ee5 red \u4e5f\u4f4d\u4e8e\u5168\u5c40\u4f5c\u7528\u57df green , blue , }; int main () { Color apple { red }; // apple \u662f red \u7684 return 0 ; } Color \u679a\u4e3e\u7c7b\u578b\u88ab\u5b9a\u4e49\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\uff0c\u56e0\u6b64\u5b83\u6240\u6709\u679a\u4e3e\u503c( red \u3001 green \u548c blue ) \u90fd\u4f1a\u88ab\u653e\u5230\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u3002\u8fd9\u65e0\u7591\u4f1a\u6c61\u67d3\u5168\u5c40\u4f5c\u7528\u57df\uff0c\u800c\u4e14\u663e\u8457\u63d0\u9ad8\u4e86\u547d\u540d\u51b2\u7a81\u7684\u53ef\u80fd\u6027\u3002 \u540c\u6837\u5730\uff0c\u7531\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u76f8\u540c\u7684\u679a\u4e3e\u503c\u540d\u79f0\u4e0d\u80fd\u7528\u5728\u4e0d\u540c\u7684\u679a\u4e3e\u7c7b\u578b\u4e2d\uff08\u5982\u679c\u5b83\u4eec\u5728\u4e00\u4e2a\u4f5c\u7528\u57df\u4e2d\u88ab\u5b9a\u4e49\u7684\u8bdd\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 enum Color { red , green , blue , // blue \u5728\u5168\u5c40\u4f5c\u7528\u57df }; enum Feeling { happy , tired , blue , // \u9519\u8bef\uff1a\u547d\u540d\u51b2\u7a81 }; int main () { Color apple { red }; // my apple is red Feeling me { happy }; // I'm happy right now (even though my program doesn't compile) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4e24\u4e2a \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u90fd\u88ab\u5b9a\u4e49\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\uff0c\u4f46\u662f\u5b83\u4eec\u90fd\u5305\u542b\u4e86\u4e00\u4e2a\u540c\u540d\u7684\u679a\u4e3e\u503c blue \u3002\u8fd9\u65e0\u7591\u4f1a\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\uff0c\u7a0b\u5e8f\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\u3002 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u8fd8\u4e3a\u5b83\u4eec\u7684\u679a\u4e3e\u503c\u63d0\u4f9b\u547d\u540d\u4f5c\u7528(\u975e\u5e38\u7c7b\u4f3c\u4e8e \u547d\u540d\u7a7a\u95f4 \u4e3a\u5176\u4e2d\u58f0\u660e\u7684\u53d8\u91cf\u540d\u63d0\u4f9b\u547d\u540d\u4f5c\u7528\u57df\u533a\u57df)\u3002\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u6309\u4ee5\u4e0b\u65b9\u5f0f\u8bbf\u95ee\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7684\u679a\u4e3e\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 enum Color { red , green , blue , // blue \u4f4d\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4 }; int main () { Color apple { red }; // \u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 Color raspberry { Color :: red }; // \u4e5f\u53ef\u4ee5\u901a\u8fc7Color\u8bbf\u95ee return 0 ; } \u4e0d\u8fc7\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u679a\u4e3e\u503c\u4e0d\u4f1a\u901a\u8fc7 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u6765\u8bbf\u95ee\u3002 \u907f\u514d\u679a\u4e3e\u503c\u7684\u547d\u540d\u51b2\u7a81 \u6709\u5f88\u591a\u5e38\u89c1\u7684\u65b9\u6cd5\u53ef\u4ee5\u907f\u514d\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u503c\u7684\u547d\u540d\u51b2\u7a81\u3002\u4e00\u79cd\u9009\u62e9\u662f\u5728\u547d\u540d\u679a\u4e3e\u503c\u524d\uff0c\u52a0\u4e0a\u679a\u4e3e\u672c\u8eab\u7684\u540d\u79f0\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 enum Color { color_red , color_blue , color_green , }; enum Feeling { feeling_happy , feeling_tired , feeling_blue , // no longer has a naming collision with color_blue }; int main () { Color paint { color_blue }; Feeling me { feeling_blue }; return 0 ; } \u867d\u7136\u8fd9\u4ecd\u7136\u4f1a\u6c61\u67d3\u547d\u540d\u7a7a\u95f4\uff0c\u4f46\u901a\u8fc7\u4f7f\u540d\u79f0\u66f4\u957f\u3001\u66f4\u552f\u4e00\uff0c\u51cf\u5c11\u4e86\u547d\u540d\u51b2\u7a81\u7684\u673a\u4f1a\u3002 \u4e00\u4e2a\u66f4\u597d\u7684\u9009\u62e9\u662f\u5c06\u679a\u4e3e\u7c7b\u578b\u653e\u5728\u80fd\u591f\u63d0\u4f9b\u5355\u72ec\u4f5c\u7528\u57df\u7684\u4e1c\u897f\u4e2d\uff0c\u4f8b\u5982\u547d\u540d\u7a7a\u95f4: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 namespace color { // The names Color, red, blue, and green are defined inside namespace color enum Color { red , green , blue , }; } namespace feeling { enum Feeling { happy , tired , blue , // feeling::blue doesn't collide with color::blue }; } int main () { color :: Color paint { color :: blue }; feeling :: Feeling me { feeling :: blue }; return 0 ; } \u4f46\u8fd9\u610f\u5473\u7740\u6211\u4eec\u73b0\u5728\u5fc5\u987b\u5728\u679a\u4e3e\u548c\u679a\u4e3e\u503c\u540d\u79f0\u524d\u9762\u52a0\u4e0a\u4f5c\u7528\u57df\u540d\u79f0\u3002 \u6269\u5c55\u9605\u8bfb \u7c7b\u4e5f\u80fd\u591f\u63d0\u4f9b\u4f5c\u7528\u57df\uff0c\u901a\u5e38\u5c06\u4e0e\u7c7b\u76f8\u5173\u7684\u679a\u4e3e\u7c7b\u578b\u76f4\u63a5\u5b9a\u4e49\u5728\u7c7b\u5185\u3002\u6211\u4eec\u4f1a\u5728 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b \u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002 \u53e6\u5916\u4e00\u4e2a\u63a5\u8fd1\u65b9\u6848\u662f\u4f7f\u7528 \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e \u4f1a\u5728 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u6700\u4f73\u5b9e\u8df5 \u6700\u597d\u5c06\u679a\u4e3e\u653e\u5728\u4e00\u4e2a\u547d\u540d\u7684\u4f5c\u7528\u57df\u4e2d(\u4f8b\u5982\u547d\u540d\u7a7a\u95f4\u6216\u7c7b)\uff0c\u8fd9\u6837\u679a\u4e3e\u503c\u5c31\u4e0d\u4f1a\u6c61\u67d3\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u3002 \u6bd4\u8f83\u679a\u4e3e\u503c \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7b49\u4e8e\u53f7\u6216\u4e0d\u7b49\u53f7 ( operator== \u548c operator!= ) \u6765\u5224\u65ad\u4e24\u4e2a\u679a\u4e3e\u503c\u662f\u5426\u76f8\u540c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include enum Color { red , green , blue , }; int main () { Color shirt { blue }; if ( shirt == blue ) // if the shirt is blue std :: cout << \"Your shirt is blue!\" ; else std :: cout << \"Your shirt is not blue!\" ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 if \u8bed\u53e5\u6765\u6d4b\u8bd5 shirt \u662f\u5426\u7b49\u4e8e blue \u3002\u8fd9\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u57fa\u4e8e\u679a\u4e3e\u6240\u6301\u6709\u7684\u679a\u4e3e\u503c\u6765\u6309\u7167\u6761\u4ef6\u6267\u884c\u7a0b\u5e8f\u7684\u65b9\u6cd5\u3002 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u66f4\u591a\u5730\u4f7f\u7528\u5b83\u3002","title":"10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b"},{"location":"10-2-unscoped-enumerations/#_1","text":"\u679a\u4e3e(\u4e5f\u79f0\u4e3a\u679a\u4e3e\u7c7b\u578b\u548c enum ) \u662f\u4e00\u79cd\u590d\u5408\u7c7b\u578b\u3002\u5728\u679a\u4e3e\u7c7b\u578b\u4e2d\uff0c\u6bcf\u4e2a\u53ef\u80fd\u7684\u503c\u90fd\u88ab\u5b9a\u4e49\u4e3a\u4e00\u4e2a \u7b26\u53f7\u5e38\u91cf \uff08\u79f0\u4e3a\u679a\u4e3e\u503c\uff09\u3002 \u56e0\u4e3a\u679a\u4e3e\u5c5e\u4e8e\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\uff08 10.1 - \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7b80\u4ecb \uff09\uff0c\u6240\u4ee5\u679a\u4e3e\u7c7b\u578b\u5728\u4f7f\u7528\u524d\u5fc5\u987b\u5148\u8fdb\u884c\u5b9a\u4e49\u3002 C++ \u652f\u6301\u4e24\u79cd\u7c7b\u578b\u7684\u679a\u4e3e\uff1a 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u548c 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09","title":"\u679a\u4e3e"},{"location":"10-2-unscoped-enumerations/#_2","text":"\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u901a\u8fc7 enum \u5173\u952e\u5b57\u5b9a\u4e49\u3002 \u679a\u4e3e\u7c7b\u578b\u901a\u8fc7\u5b9e\u4f8b\u6765\u5b66\u4e60\u6548\u679c\u66f4\u597d\u3002\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u6765\u8868\u793a\u989c\u8272\u503c\u5427\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // \u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\uff0c\u540d\u4e3a Color enum Color { // \u679a\u4e3e\u503c\u5b9a\u4e49\u5728\u8fd9\u91cc // \u8fd9\u4e9b\u7b26\u53f7\u5e38\u91cf\u5b9a\u4e49\u4e86\u6240\u6709\u53ef\u80fd\u7684\u503c // \u679a\u4e3e\u503c\u901a\u8fc7\u9017\u53f7\u5206\u5272\uff0c\u800c\u4e0d\u662f\u5206\u53f7 red , green , blue , // \u7ed3\u5c3e\u7684\u9017\u53f7\u53ef\u6709\u53ef\u65e0\uff0c\u4f46\u662f\u63a8\u8350\u5199\u4e0a }; // \u679a\u4e3e\u5b9a\u4e49\u5fc5\u987b\u4ee5\u5206\u53f7\u7ed3\u5c3e int main () { // \u5b9a\u4e49\u4e00\u4e9bColor\u7c7b\u578b\u7684\u53d8\u91cf Color apple { red }; // my apple is red Color shirt { green }; // my shirt is green Color cup { blue }; // my cup is blue Color socks { white }; // \u9519\u8bef: white \u4e0d\u662f\u53ef\u7528\u7684\u679a\u4e3e\u503c Color hat { 2 }; // \u9519\u8bef: 2 \u9875\u4e0d\u662fColor\u7684\u679a\u4e3e\u503c return 0 ; } \u6211\u4eec\u9996\u5148\u4f7f\u7528 enum \u5173\u952e\u5b57\u6765\u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u6211\u4eec\u6b63\u5728\u5b9a\u4e49\u4e00\u4e2a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\uff0c\u6211\u4eec\u5c06\u5176\u547d\u540d\u4e3a Color \u3002 \u968f\u540e\uff0c\u5728\u82b1\u62ec\u53f7\u91cc\u9762\u5b9a\u4e49 Color \u7c7b\u578b\u7684\u679a\u4e3e\u503c\uff1a red \uff0c green \u548c blue \u3002\u8fd9\u4e9b\u679a\u4e3e\u503c\u6307\u5b9a\u4e86 Color \u7c7b\u578b\u80fd\u591f\u8868\u793a\u6216\u5b58\u50a8\u7684\u6240\u6709\u53ef\u80fd\u503c\u3002\u6bcf\u4e2a\u679a\u4e3e\u503c\u9700\u8981\u901a\u8fc7\u9017\u53f7\uff08\u800c\u4e0d\u662f\u5206\u53f7\uff09\u9694\u5f00\u2014\u2014\u6700\u540e\u4e00\u4e2a\u679a\u4e3e\u503c\u540e\u9762\u53ef\u4ee5\u6709\u9017\u53f7\u4e5f\u53ef\u4ee5\u6ca1\u6709\uff0c\u52a0\u4e0a\u9017\u53f7\u770b\u8d77\u6765\u4e00\u81f4\u6027\u66f4\u597d\u3002 Color \u7684\u5b9a\u4e49\u4ee5\u5206\u53f7\u7ed3\u5c3e\u3002\u81f3\u6b64\uff0c\u679a\u4e3e\u7c7b\u578b Color \u5c31\u5b9a\u4e49\u5b8c\u6210\u4e86\uff01 \u5728 main() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u521d\u59cb\u5316\u4e86\u4e09\u4e2a Color \u7c7b\u578b\u7684\u53d8\u91cf\uff1a apple \u88ab\u521d\u59cb\u5316\u4e3a red \uff0c shirt \u521d\u59cb\u5316\u4e3a green \uff0c cup \u521d\u59cb\u5316\u4e3a blue \u3002 \u7a0b\u5e8f\u4f1a\u4e3a\u8fd9\u4e09\u4e2a\u5bf9\u8c61\u5206\u914d\u5185\u5b58\u3002\u6ce8\u610f\uff0c\u5f53\u521d\u59cb\u5316\u679a\u4e3e\u7c7b\u578b\u65f6\uff0c\u6240\u7528\u7684\u521d\u59cb\u5316\u503c\u5fc5\u987b\u662f\u8be5\u7c7b\u578b\u5b9a\u4e49\u8fc7\u7684\u679a\u4e3e\u503c\u3002\u53d8\u91cf socks \u548c hat \u5e76\u4e0d\u80fd\u6210\u529f\u5b9a\u4e49\uff0c\u56e0\u4e3a white \u548c 2 \u5e76\u4e0d\u662f Color \u7684\u679a\u4e3e\u503c\u3002 \u4f5c\u8005\u6ce8 \u547d\u540d\u89c4\u5219\u5c0f\u7ed3: - \u679a\u4e3e\u548c\u679a\u4e3e\u7c7b\u578b\u6307\u7684\u662f\u7c7b\u578b\u672c\u8eab(\u4f8b\u5982 Color ) - \u679a\u4e3e\u503c\u6307\u7684\u662f\u679a\u4e3e\u4e2d\u53ef\u80fd\u503c\u6240\u5bf9\u5e94\u7684\u7b26\u53f7\u5e38\u91cf(\u4f8b\u5982 red )","title":"\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b"},{"location":"10-2-unscoped-enumerations/#_3","text":"\u6309\u7167\u60ef\u4f8b\uff0c\u679a\u4e3e\u7c7b\u578b\u7684\u540d\u5b57\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff08\u89c4\u5219\u9002\u7528\u4e8e\u6240\u6709\u81ea\u5b9a\u4e49\u7c7b\u578b\uff09\u3002 \u6ce8\u610f \u679a\u4e3e\u7c7b\u578b\u4e0d\u4e00\u5b9a\u8981\u6709\u540d\u5b57\uff0c\u4f46\u662f\u73b0\u4ee3C++\u63a8\u8350\u907f\u514d\u4f7f\u7528\u533f\u540d\u7684\u679a\u4e3e\u3002 \u679a\u4e3e\u503c\u5fc5\u987b\u6709\u540d\u5b57\u3002\u53ef\u60dc\u7684\u662f\uff0c\u5bf9\u4e8e\u679a\u4e3e\u503c\u7684\u540d\u5b57\u6682\u65f6\u8fd8\u6ca1\u6709\u7ea6\u5b9a\u4fd7\u6210\u7684\u547d\u540d\u89c4\u8303\u3002\u901a\u5e38\u7684\u9009\u62e9\u6709\uff1a\u5c0f\u5199\u5b57\u6bcd\u5f00\u5934\uff08\u4f8b\u5982 red \uff09\uff0c\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff08\u4f8b\u5982 Red \uff0c\u5168\u5927\u5199\uff08\u4f8b\u5982 RED \uff09\u3001\u5168\u5927\u5199\u4e14\u6dfb\u52a0\u5c06\u679a\u4e3e\u7c7b\u578b\u4f5c\u4e3a\u524d\u7f00\uff08\u4f8b\u5982 COLOR_RED \uff09\uff0c\u518d\u6216\u8005\u4f7f\u7528 k \u4f5c\u4e3a\u524d\u7f00\u5e76\u914d\u5408\u5927\u5c0f\u5199\u4ea4\u66ff(kColorRed)\u3002 \u73b0\u4ee3C++\u6307\u5357\u901a\u5e38\u5efa\u8bae\u907f\u514d\u4f7f\u7528\u5168\u5927\u5199\u7684\u547d\u540d\u7ea6\u5b9a\uff0c\u56e0\u4e3a\u5168\u5927\u5199\u901a\u5e38\u7528\u4e8e\u9884\u5904\u7406\u5668\u5b8f\uff0c\u53ef\u80fd\u4f1a\u53d1\u751f\u51b2\u7a81\u3002\u6211\u4eec\u8fd8\u5efa\u8bae\u907f\u514d\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684\u7ea6\u5b9a\uff0c\u56e0\u4e3a\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684\u540d\u79f0\u901a\u5e38\u4e3a\u81ea\u5b9a\u4e49\u7c7b\u578b\u4fdd\u7559\u3002 \u6700\u4f73\u5b9e\u8df5 \u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u547d\u540d\u679a\u4e3e\u7c7b\u578b\u3002\u4ee5\u5c0f\u5199\u5b57\u6bcd\u5f00\u5934\u547d\u540d\u679a\u4e3e\u503c\u3002","title":"\u679a\u4e3e\u7c7b\u578b\u548c\u679a\u4e3e\u503c\u7684\u547d\u540d"},{"location":"10-2-unscoped-enumerations/#_4","text":"\u4f60\u521b\u5efa\u7684\u6bcf\u4e2a\u679a\u4e3e\u7c7b\u578b\u90fd\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u4e0d\u540c\u7684\u7c7b\u578b\uff0c\u8fd9\u610f\u5473\u7740\u7f16\u8bd1\u5668\u53ef\u4ee5\u5c06\u5176\u4e0e\u5176\u4ed6\u7c7b\u578b\u533a\u5206\u5f00\u6765(\u4e0d\u50cf typedefs \u6216\u7c7b\u578b\u522b\u540d\uff0c\u5b83\u4eec\u662f\u4e00\u79cd\u522b\u540d\uff0c\u4e0e\u5176\u5bf9\u5e94\u7684\u7c7b\u578b\u5e76\u6ca1\u6709\u533a\u522b)\u3002 \u679a\u4e3e\u7c7b\u578b\u662f\u4e0d\u540c\u7684\uff0c\u56e0\u6b64\u4e00\u79cd\u679a\u4e3e\u7c7b\u578b\u4e2d\u679a\u4e3e\u503c\u4e5f\u4e0d\u80fd\u7528\u4e8e\u53e6\u4e00\u79cd\u679a\u4e3e\u7c7b\u578b\u7684\u5bf9\u8c61: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 enum Pet { cat , dog , pig , whale , }; enum Color { black , red , blue , }; int main () { Pet myPet { black }; // \u7f16\u8bd1\u9519\u8bef: black \u4e0d\u662f Pet \u7684\u679a\u4e3e\u503c Color shirt { pig }; // \u7f16\u8bd1\u9519\u8bef: pig \u4e0d\u662f Color \u7684\u679a\u4e3e\u503c return 0 ; } \u4f60\u53ef\u80fd\u4e5f\u4e0d\u60f3\u8981\u4e00\u4ef6\u201dpig\u201c\u886c\u886b\u3002","title":"\u679a\u4e3e\u7c7b\u578b\u662f\u53ef\u533a\u5206\u7c7b\u578b"},{"location":"10-2-unscoped-enumerations/#_5","text":"\u56e0\u4e3a\u679a\u4e3e\u5668\u662f\u63cf\u8ff0\u6027\u7684\uff0c\u5b83\u4eec\u5bf9\u4e8e\u589e\u5f3a\u4ee3\u7801\u6587\u6863\u6027\u548c\u53ef\u8bfb\u6027\u975e\u5e38\u6709\u7528\u3002\u5f53\u4f60\u7684\u76f8\u5173\u5e38\u91cf\u7684\u96c6\u5408\u5f88\u5c0f\uff0c\u5e76\u4e14\u5bf9\u8c61\u6bcf\u6b21\u53ea\u9700\u8981\u4fdd\u5b58\u5176\u4e2d\u4e00\u4e2a\u503c\u65f6\uff0c\u679a\u4e3e\u7c7b\u578b\u662f\u6700\u5408\u9002\u7684\u3002 \u661f\u671f\u51e0\u3001\u65b9\u5411\u6216\u8005\u4e00\u526f\u724c\u4e2d\u7684\u82b1\u8272\u90fd\u662f\u5e38\u89c1\u7684\u679a\u4e3e\u5b9a\u4e49\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 enum DaysOfWeek { sunday , monday , tuesday , wednesday , thursday , friday , saturday , }; enum CardinalDirections { north , east , south , west , }; enum CardSuits { clubs , diamonds , hearts , spades , }; \u6709\u65f6\u5019\u88ab\u8c03\u51fd\u6570\u9700\u8981\u50cf\u4e3b\u8c03\u51fd\u6570\u8fd4\u56de\u72b6\u6001\u7801\u4ee5\u8868\u660e\u81ea\u5df1\u662f\u5426\u6267\u884c\u6210\u529f\u3002\u4f20\u7edf\u505a\u6cd5\u662f\u4f7f\u7528\u8d1f\u503c\u6765\u4ee3\u8868\u4e0d\u540c\u7684\u9519\u8bef\u7801\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 int readFileContents () { if ( ! openFile ()) return -1 ; if ( ! readFile ()) return -2 ; if ( ! parseFile ()) return -3 ; return 0 ; // success } \u4f46\u662f\uff0c\u8fd9\u4e9b\u9b54\u9b3c\u6570\u5b57\u542b\u4e49\u975e\u5e38\u6a21\u7cca\u3002\u66f4\u597d\u7684\u529e\u6cd5\u662f\u4f7f\u7528\u679a\u4e3e\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 enum FileReadResult { readResultSuccess , readResultErrorFileOpen , readResultErrorFileRead , readResultErrorFileParse , }; FileReadResult readFileContents () { if ( ! openFile ()) return readResultErrorFileOpen ; if ( ! readFile ()) return readResultErrorFileRead ; if ( ! parseFile ()) return readResultErrorFileParse ; return readResultSuccess ; } \u7136\u540e\u8c03\u7528\u8005\u53ef\u4ee5\u5c06\u8fd4\u56de\u503c\u548c\u7279\u5b9a\u7684\u679a\u4e3e\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u76f8\u5bf9\u4e8e\u76f4\u63a5\u548c\u6574\u578b\u6570\u6bd4\u8f83\uff0c\u8fd9\u79cd\u65b9\u6cd5\u53ef\u8bfb\u6027\u66f4\u597d\u3002 1 2 3 4 5 6 7 8 if ( readFileContents () == readResultSuccess ) { // do something } else { // print error message } \u679a\u4e3e\u7c7b\u578b\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6807\u8bc6\u7b80\u5355\u6e38\u620f\u4e2d\u7684\u9053\u5177\u3001\u602a\u7269\u6216\u5730\u5f62\u7c7b\u578b\u3002\u57fa\u672c\u4e0a\uff0c\u4efb\u4f55\u76f8\u5173\u5bf9\u8c61\u7684\u5c0f\u96c6\u5408\u90fd\u53ef\u4ee5\u4f7f\u7528\u679a\u4e3e\u8868\u793a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 enum ItemType { sword , torch , potion , }; int main () { ItemType holding { torch }; return 0 ; } \u679a\u4e3e\u7c7b\u578b\u4e5f\u53ef\u4ee5\u7528\u4e8e\u51fd\u6570\u5f62\u53c2\uff0c\u8868\u793a\u7528\u6237\u7684\u8f93\u5165\u7684\u4e0d\u540c\u9009\u9879\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 enum SortOrder { alphabetical , alphabeticalReverse , numerical , }; void sortData ( SortOrder order ) { if ( order == alphabetical ) // sort data in forwards alphabetical order else if ( order == alphabeticalReverse ) // sort data in backwards alphabetical order else if ( order == numerical ) // sort data numerically } \u5f88\u591a\u7f16\u7a0b\u8bed\u8a00\u4f1a\u4f7f\u7528\u679a\u4e3e\u6765\u5b9a\u4e49\u5e03\u5c14\u7c7b\u578b\u2014\u2014\u6bd5\u7adf\u5e03\u5c14\u7c7b\u578b\u672c\u8d28\u4e0a\u5c31\u662f\u4e00\u4e2a\u5177\u6709\u4e24\u4e2a\u679a\u4e3e\u503c\u7684\u679a\u4e3e\u7c7b\u578b\uff1a false \u548c true \uff01\u4f46\u662f\uff0cC++ \u4e2d\u7684 true \u548c false \u662f\u5173\u952e\u5b57\u800c\u4e0d\u662f\u679a\u4e3e\u503c\u3002","title":"\u4f7f\u7528\u679a\u4e3e"},{"location":"10-2-unscoped-enumerations/#_6","text":"\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u4e4b\u6240\u4ee5\u88ab\u8d4b\u4e88\u8fd9\u6837\u7684\u540d\u5b57\uff0c \u662f\u56e0\u4e3a\u5b83\u4f1a\u5c06\u5176\u5b9a\u4e49\u7684\u679a\u4e3e\u503c\u66b4\u9732\u5728\u548c\u81ea\u5df1\u4e00\u6837\u7684\u4f5c\u7528\u57df\u4e2d \uff08\u800c\u4e0d\u662f\u50cf\u547d\u540d\u7a7a\u95f4\u90a3\u6837\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u4f5c\u7528\u57df\u7a7a\u95f4\uff09\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 enum Color // \u8be5\u679a\u4e3e\u7c7b\u578b\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u5b9a\u4e49 { red , // \u6240\u4ee5 red \u4e5f\u4f4d\u4e8e\u5168\u5c40\u4f5c\u7528\u57df green , blue , }; int main () { Color apple { red }; // apple \u662f red \u7684 return 0 ; } Color \u679a\u4e3e\u7c7b\u578b\u88ab\u5b9a\u4e49\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\uff0c\u56e0\u6b64\u5b83\u6240\u6709\u679a\u4e3e\u503c( red \u3001 green \u548c blue ) \u90fd\u4f1a\u88ab\u653e\u5230\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u3002\u8fd9\u65e0\u7591\u4f1a\u6c61\u67d3\u5168\u5c40\u4f5c\u7528\u57df\uff0c\u800c\u4e14\u663e\u8457\u63d0\u9ad8\u4e86\u547d\u540d\u51b2\u7a81\u7684\u53ef\u80fd\u6027\u3002 \u540c\u6837\u5730\uff0c\u7531\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u76f8\u540c\u7684\u679a\u4e3e\u503c\u540d\u79f0\u4e0d\u80fd\u7528\u5728\u4e0d\u540c\u7684\u679a\u4e3e\u7c7b\u578b\u4e2d\uff08\u5982\u679c\u5b83\u4eec\u5728\u4e00\u4e2a\u4f5c\u7528\u57df\u4e2d\u88ab\u5b9a\u4e49\u7684\u8bdd\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 enum Color { red , green , blue , // blue \u5728\u5168\u5c40\u4f5c\u7528\u57df }; enum Feeling { happy , tired , blue , // \u9519\u8bef\uff1a\u547d\u540d\u51b2\u7a81 }; int main () { Color apple { red }; // my apple is red Feeling me { happy }; // I'm happy right now (even though my program doesn't compile) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4e24\u4e2a \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u90fd\u88ab\u5b9a\u4e49\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\uff0c\u4f46\u662f\u5b83\u4eec\u90fd\u5305\u542b\u4e86\u4e00\u4e2a\u540c\u540d\u7684\u679a\u4e3e\u503c blue \u3002\u8fd9\u65e0\u7591\u4f1a\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\uff0c\u7a0b\u5e8f\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\u3002 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u8fd8\u4e3a\u5b83\u4eec\u7684\u679a\u4e3e\u503c\u63d0\u4f9b\u547d\u540d\u4f5c\u7528(\u975e\u5e38\u7c7b\u4f3c\u4e8e \u547d\u540d\u7a7a\u95f4 \u4e3a\u5176\u4e2d\u58f0\u660e\u7684\u53d8\u91cf\u540d\u63d0\u4f9b\u547d\u540d\u4f5c\u7528\u57df\u533a\u57df)\u3002\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u6309\u4ee5\u4e0b\u65b9\u5f0f\u8bbf\u95ee\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7684\u679a\u4e3e\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 enum Color { red , green , blue , // blue \u4f4d\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4 }; int main () { Color apple { red }; // \u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 Color raspberry { Color :: red }; // \u4e5f\u53ef\u4ee5\u901a\u8fc7Color\u8bbf\u95ee return 0 ; } \u4e0d\u8fc7\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u679a\u4e3e\u503c\u4e0d\u4f1a\u901a\u8fc7 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u6765\u8bbf\u95ee\u3002","title":"\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u4f5c\u7528\u57df"},{"location":"10-2-unscoped-enumerations/#_7","text":"\u6709\u5f88\u591a\u5e38\u89c1\u7684\u65b9\u6cd5\u53ef\u4ee5\u907f\u514d\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u503c\u7684\u547d\u540d\u51b2\u7a81\u3002\u4e00\u79cd\u9009\u62e9\u662f\u5728\u547d\u540d\u679a\u4e3e\u503c\u524d\uff0c\u52a0\u4e0a\u679a\u4e3e\u672c\u8eab\u7684\u540d\u79f0\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 enum Color { color_red , color_blue , color_green , }; enum Feeling { feeling_happy , feeling_tired , feeling_blue , // no longer has a naming collision with color_blue }; int main () { Color paint { color_blue }; Feeling me { feeling_blue }; return 0 ; } \u867d\u7136\u8fd9\u4ecd\u7136\u4f1a\u6c61\u67d3\u547d\u540d\u7a7a\u95f4\uff0c\u4f46\u901a\u8fc7\u4f7f\u540d\u79f0\u66f4\u957f\u3001\u66f4\u552f\u4e00\uff0c\u51cf\u5c11\u4e86\u547d\u540d\u51b2\u7a81\u7684\u673a\u4f1a\u3002 \u4e00\u4e2a\u66f4\u597d\u7684\u9009\u62e9\u662f\u5c06\u679a\u4e3e\u7c7b\u578b\u653e\u5728\u80fd\u591f\u63d0\u4f9b\u5355\u72ec\u4f5c\u7528\u57df\u7684\u4e1c\u897f\u4e2d\uff0c\u4f8b\u5982\u547d\u540d\u7a7a\u95f4: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 namespace color { // The names Color, red, blue, and green are defined inside namespace color enum Color { red , green , blue , }; } namespace feeling { enum Feeling { happy , tired , blue , // feeling::blue doesn't collide with color::blue }; } int main () { color :: Color paint { color :: blue }; feeling :: Feeling me { feeling :: blue }; return 0 ; } \u4f46\u8fd9\u610f\u5473\u7740\u6211\u4eec\u73b0\u5728\u5fc5\u987b\u5728\u679a\u4e3e\u548c\u679a\u4e3e\u503c\u540d\u79f0\u524d\u9762\u52a0\u4e0a\u4f5c\u7528\u57df\u540d\u79f0\u3002 \u6269\u5c55\u9605\u8bfb \u7c7b\u4e5f\u80fd\u591f\u63d0\u4f9b\u4f5c\u7528\u57df\uff0c\u901a\u5e38\u5c06\u4e0e\u7c7b\u76f8\u5173\u7684\u679a\u4e3e\u7c7b\u578b\u76f4\u63a5\u5b9a\u4e49\u5728\u7c7b\u5185\u3002\u6211\u4eec\u4f1a\u5728 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b \u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002 \u53e6\u5916\u4e00\u4e2a\u63a5\u8fd1\u65b9\u6848\u662f\u4f7f\u7528 \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e \u4f1a\u5728 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u6700\u4f73\u5b9e\u8df5 \u6700\u597d\u5c06\u679a\u4e3e\u653e\u5728\u4e00\u4e2a\u547d\u540d\u7684\u4f5c\u7528\u57df\u4e2d(\u4f8b\u5982\u547d\u540d\u7a7a\u95f4\u6216\u7c7b)\uff0c\u8fd9\u6837\u679a\u4e3e\u503c\u5c31\u4e0d\u4f1a\u6c61\u67d3\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u3002","title":"\u907f\u514d\u679a\u4e3e\u503c\u7684\u547d\u540d\u51b2\u7a81"},{"location":"10-2-unscoped-enumerations/#_8","text":"\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7b49\u4e8e\u53f7\u6216\u4e0d\u7b49\u53f7 ( operator== \u548c operator!= ) \u6765\u5224\u65ad\u4e24\u4e2a\u679a\u4e3e\u503c\u662f\u5426\u76f8\u540c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include enum Color { red , green , blue , }; int main () { Color shirt { blue }; if ( shirt == blue ) // if the shirt is blue std :: cout << \"Your shirt is blue!\" ; else std :: cout << \"Your shirt is not blue!\" ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 if \u8bed\u53e5\u6765\u6d4b\u8bd5 shirt \u662f\u5426\u7b49\u4e8e blue \u3002\u8fd9\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u57fa\u4e8e\u679a\u4e3e\u6240\u6301\u6709\u7684\u679a\u4e3e\u503c\u6765\u6309\u7167\u6761\u4ef6\u6267\u884c\u7a0b\u5e8f\u7684\u65b9\u6cd5\u3002 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u66f4\u591a\u5730\u4f7f\u7528\u5b83\u3002","title":"\u6bd4\u8f83\u679a\u4e3e\u503c"},{"location":"10-3-unscoped-enumeration-input-and-output/","tags":["enum","unscoped enumerations"],"text":"Key Takeaway \u5728\u4e0a\u8282\u8bfe\u4e2d( 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b )\u6211\u4eec\u63d0\u5230\uff0c\u679a\u4e3e\u662f\u4e00\u79cd \u7b26\u53f7\u5e38\u91cf \u3002\u4f46\u662f\u6211\u4eec\u6ca1\u8bf4\u7684\u662f\uff0c\u679a\u4e3e\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u6574\u578b\u7b26\u53f7\u5e38\u91cf\u3002\u56e0\u6b64\uff0c\u679a\u4e3e\u7c7b\u578b\u4e2d\u4fdd\u5b58\u7684\u5176\u5b9e\u662f\u4e00\u4e2a\u6574\u578b\u503c\u3002 \u5728\u8fd9\u4e00\u70b9\u4e0a\uff0c\u679a\u4e3e\u7c7b\u578b\u7c7b\u4f3c\u4e8e\u5b57\u7b26 ( 4.11 - \u5b57\u7b26 )\u3002\u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 char ch { 'A' }; char \u5b9e\u9645\u4e0a\u5c31\u662f\u4e00\u4e2a1\u4e2a\u5b57\u8282\u7684\u6574\u578b\u6570\uff0c\u5b57\u7b26 A \u4f1a\u88ab\u8f6c\u6362\u4e3a\u6574\u578b\u6570\uff08\u8f6c\u6362\u4e3a65\uff09\u5e76\u50a8\u5b58\u3002 \u5f53\u6211\u4eec\u5b9a\u4e49\u679a\u4e3e\u7684\u65f6\u5019\uff0c\u6bcf\u4e2a\u679a\u4e3e\u503c\u4f1a\u6839\u636e\u5176\u6240\u5728\u4f4d\u7f6e\u88ab\u81ea\u52a8\u8d4b\u503c\u4e00\u4e2a\u6574\u578b\u503c\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u7b2c\u4e00\u4e2a\u679a\u4e3e\u503c\u4f1a\u88ab\u8d4b\u503c\u4e3a0\uff0c\u540e\u9762\u7684\u679a\u4e3e\u503c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u6bcf\u4e2a\u90fd\u6bd4\u524d\u4e00\u4e2a\u503c\u5927\u4e00\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include enum Color { black , // assigned 0 red , // assigned 1 blue , // assigned 2 green , // assigned 3 white , // assigned 4 cyan , // assigned 5 yellow , // assigned 6 magenta , // assigned 7 }; int main () { Color shirt { blue }; // This actually stores the integral value 2 return 0 ; } \u6211\u4eec\u4e5f\u53ef\u4ee5\u663e\u5f0f\u5730\u5b9a\u4e49\u679a\u4e3e\u6570\u7684\u503c\uff0c\u800c\u4e14\u8fd9\u4e9b\u6574\u6570\u503c\u53ef\u4ee5\u662f\u6574\u6570\u6216\u8d1f\u6570\uff0c\u5e76\u4e14\u53ef\u4ee5\u4e0e\u5176\u4ed6\u679a\u4e3e\u503c\u5171\u4eab\u76f8\u540c\u7684\u503c\u3002\u4efb\u4f55\u672a\u5b9a\u4e49\u7684\u679a\u4e3e\u6570\u90fd\u88ab\u8d4b\u4e88\u4e00\u4e2a\u6bd4\u524d\u4e00\u4e2a\u679a\u4e3e\u6570\u59271\u7684\u503c\u3002 1 2 3 4 5 6 7 8 9 enum Animal { cat = -3 , dog , // assigned -2 pig , // assigned -1 horse = 5 , giraffe = 5 , // shares same value as horse chicken , // assigned 6 }; \u6ce8\u610f\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c horse \u548c giraffe \u5177\u6709\u76f8\u540c\u7684\u503c\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4e9b\u679a\u4e3e\u503c\u662f\u65e0\u533a\u522b\u7684\u2014\u2014 horse \u548c giraffe \u662f\u53ef\u4ee5\u4e92\u6362\u7684\u3002\u5c3d\u7ba1C++\u5141\u8bb8\u4f60\u8fd9\u4e48\u505a\uff0c\u4f46\u662f\u6211\u4eec\u5e94\u8be5\u5c3d\u91cf\u907f\u514d\u5c06\u4e00\u4e2a\u679a\u4e3e\u7c7b\u578b\u4e2d\u7684\u4e24\u4e2a\u679a\u4e3e\u503c\u8d4b\u503c\u4e3a\u76f8\u540c\u7684\u6570\u3002 \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u7ed9\u679a\u4e3e\u503c\u663e\u5f0f\u8d4b\u503c\uff0c\u9664\u975e\u6709\u4ee4\u4eba\u4fe1\u670d\u7684\u7406\u7531\u8fd9\u6837\u505a\u3002 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u4f1a\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u6574\u578b\u503c \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include enum Color { black , // assigned 0 red , // assigned 1 blue , // assigned 2 green , // assigned 3 white , // assigned 4 cyan , // assigned 5 yellow , // assigned 6 magenta , // assigned 7 }; int main () { Color shirt { blue }; std :: cout << \"Your shirt is \" << shirt ; // what does this do? return 0 ; } \u56e0\u4e3a\u679a\u4e3e\u7c7b\u578b\u4fdd\u5b58\u7684\u662f\u6574\u578b\u503c\uff0c\u6240\u4ee5\u5982\u4f60\u6240\u671f\u5f85\u7684\u90a3\u6837\uff0c\u4e0a\u9762\u7684\u7a0b\u5e8f\u6253\u5370\uff1a 1 Your shirt is 2 \u5f53\u679a\u4e3e\u7c7b\u578b\u7528\u4e8e\u51fd\u6570\u8c03\u7528\u6216\u4e0e\u8fd0\u7b97\u7b26\u4e00\u8d77\u4f7f\u7528\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u9996\u5148\u5c1d\u8bd5\u67e5\u627e\u4e0e\u679a\u4e3e\u7c7b\u578b\u5339\u914d\u7684\u51fd\u6570\u6216\u8fd0\u7b97\u7b26\u3002\u4f8b\u5982\uff0c\u5f53\u7f16\u8bd1\u5668\u8bd5\u56fe\u7f16\u8bd1 std::cout << shirt \u65f6\uff0c\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u67e5\u770b operator<< \u662f\u5426\u77e5\u9053\u5982\u4f55\u5c06 Color \u7c7b\u578b\u7684\u5bf9\u8c61(\u56e0\u4e3a shirt \u7684\u7c7b\u578b\u662f Color )\u6253\u5370\u5230 std::cout \u3002\u5b83\u4e0d\u77e5\u9053\u3002 \u5982\u679c\u7f16\u8bd1\u5668\u65e0\u6cd5\u627e\u5230\u5339\u914d\u7684\u8fd0\u7b97\u7b26\uff0c\u5219\u5b83\u4f1a\u5c06 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u6574\u578b\u3002\u56e0\u4e3a std::cout \u77e5\u9053\u5982\u4f55\u6253\u5370\u4e00\u4e2a\u6574\u578b\uff0c\u6240\u4ee5 shirt \u4f1a\u88ab\u8f6c\u6362\u4e3a\u6574\u578b\u5e76\u6253\u5370\u51fa2\u3002 \u6253\u5370\u679a\u4e3e\u503c\u7684\u540d\u5b57 \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5c06\u679a\u4e3e\u8f6c\u6362\u4e3a\u6574\u578b\u6253\u5370\u51fa\u6765\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c\u3002\u6211\u4eec\u80af\u5b9a\u662f\u5e0c\u671b\u80fd\u591f\u5c06\u679a\u4e3e\u503c\u7684\u672c\u540d\u6253\u5370\u51fa\u6765 (\u4f8b\u5982 blue )\u3002\u4f46\u662f\uff0c\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e00\u70b9\u6211\u4eec\u9700\u8981\u4e00\u4e9b\u65b9\u6cd5\u5c06\u679a\u4e3e\u5728\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u6253\u5370\u51fa\u6765( \"blue\" )\u3002 \u622a\u81f3C++ 20, C++\u8fd8\u6ca1\u6709\u63d0\u4f9b\u4efb\u4f55\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u81ea\u5df1\u5bfb\u627e\u89e3\u51b3\u65b9\u6848\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u8fd9\u5e76\u4e0d\u96be\u3002\u5b9e\u73b0\u8fd9\u4e00\u70b9\u7684\u5178\u578b\u65b9\u6cd5\u662f\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u679a\u4e3e\u7c7b\u578b\u4f5c\u4e3a\u53c2\u6570\uff0c\u7136\u540e\u8f93\u51fa\u76f8\u5e94\u7684\u5b57\u7b26\u4e32(\u6216\u5c06\u5b57\u7b26\u4e32\u8fd4\u56de\u7ed9\u8c03\u7528\u8005)\u3002 \u5178\u578b\u7684\u65b9\u6cd5\u662f\u5224\u65ad\u5f53\u524d\u9700\u8981\u6253\u5370\u7684\u662f\u54ea\u4e2a\u7684\u679a\u4e3e\u503c\uff1a 1 2 3 4 5 6 7 8 // Using if-else for this is inefficient void printColor ( Color color ) { if ( color == black ) std :: cout << \"black\" ; else if ( color == red ) std :: cout << \"red\" ; else if ( color == blue ) std :: cout << \"blue\" ; else std :: cout << \"???\" ; } \u4f46\u662f\uff0c\u4e3a\u6b64\u4f7f\u7528\u4e00\u7cfb\u5217if-else\u8bed\u53e5\u662f\u4f4e\u6548\u7684\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u591a\u6b21\u6bd4\u8f83\u624d\u80fd\u627e\u5230\u5339\u914d\u3002\u5b8c\u6210\u540c\u6837\u4efb\u52a1\u7684\u4e00\u79cd\u66f4\u6709\u6548\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 switch \u8bed\u53e5\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8fd8\u5c06\u4ee5 std::string \u7684\u5f62\u5f0f\u8fd4\u56de Color \uff0c\u4ee5\u7ed9\u8c03\u7528\u8005\u66f4\u591a\u7684\u7075\u6d3b\u6027\u6765\u5bf9\u540d\u79f0\u505a\u4efb\u4f55\u60f3\u505a\u7684\u4e8b\u60c5(\u5305\u62ec\u6253\u5370\u5b83): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include #include enum Color { black , red , blue , }; // We'll show a better version of this for C++17 below std :: string getColor ( Color color ) { switch ( color ) { case black : return \"black\" ; case red : return \"red\" ; case blue : return \"blue\" ; default : return \"???\" ; } } int main () { Color shirt { blue }; std :: cout << \"Your shirt is \" << getColor ( shirt ) << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Your shirt is blue \u8fd9\u4e48\u505a\u6bd4\u4f7f\u7528\u591a\u4e2aif-else\u5224\u65ad\u66f4\u9ad8\u6548\uff08\u56e0\u4e3aswitch\u8bed\u53e5\u6bd4if\u66f4\u9ad8\u6548\uff09\uff0c\u800c\u4e14\u53ef\u8bfb\u6027\u4e5f\u66f4\u597d\u3002\u4f46\u662f\uff0c\u8fd9\u4e2a\u7248\u672c\u7684\u4ee3\u7801\u4e5f\u4e0d\u662f\u5f88\u9ad8\u6548\uff0c\u56e0\u4e3a\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u90fd\u5fc5\u987b\u521b\u5efa\u5e76\u8fd4\u56de\u4e00\u4e2a std::string (\u5f00\u9500\u5f88\u5927)\u3002 \u5728 C++17 \u4e2d\uff0c\u66f4\u9ad8\u6548\u7684\u505a\u6cd5\u662f\u5c06 std::string \u66ff\u6362\u4e3a std::string_view \u3002 std::string_view \u4f1a\u4ee5\u67d0\u79cd\u62f7\u8d1d\u5f00\u9500\u66f4\u5c0f\u7684\u65b9\u5f0f\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include #include // C++17 enum Color { black , red , blue , }; constexpr std :: string_view getColor ( Color color ) // C++17 { switch ( color ) { case black : return \"black\" ; case red : return \"red\" ; case blue : return \"blue\" ; default : return \"???\" ; } } int main () { Color shirt { blue }; std :: cout << \"Your shirt is \" << getColor ( shirt ) << '\\n' ; return 0 ; } COPY \u76f8\u5173\u5185\u5bb9 Constexpr \u8fd4\u56de\u7c7b\u578b\u5728 6.14 - Constexpr \u548c consteval \u51fd\u6570 \u4e2d\u4ecb\u7ecd\u3002 \u8ba9 operator<< \u77e5\u9053\u5982\u4f55\u6253\u5370\u679a\u4e3e\u503c \u5c3d\u7ba1\u4e0a\u9762\u7684\u4f8b\u5b50\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\uff0c\u4f46\u662f\u6211\u4eec\u5fc5\u987b\u8981\u80fd\u591f\u8bb0\u4f4f\u54ea\u4e2a\u51fd\u6570\u662f\u7528\u6765\u83b7\u53d6\u679a\u4e3e\u503c\u540d\u5b57\u7684\u3002\u5c3d\u7ba1\u591a\u6570\u60c5\u51b5\u4e0b\u8fd9\u4e5f\u5e76\u4e0d\u9ebb\u70e6\uff0c\u4f46\u662f\u5982\u679c\u679a\u4e3e\u7c7b\u578b\u5f88\u591a\u7684\u65f6\u5019\uff0c\u8fd8\u662f\u5f88\u53ef\u80fd\u4f1a\u5e26\u6765\u95ee\u9898\u3002\u4f7f\u7528\u8fd0\u7b97\u7b26\u91cd\u8f7d\uff0c\u6211\u4eec\u53ef\u4ee5\u6559\u4f1a\u8fd0\u7b97\u7b26 operator<< \u5982\u4f55\u6253\u5370\u7a0b\u5e8f\u5b9a\u4e49\u679a\u4e3e\u7c7b\u578b\uff01\u7531\u4e8e\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u5148\u5c06\u4e0b\u9762\u7684\u4ee3\u7801\u5f53\u505a\u4e00\u79cd\u9b54\u6cd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include enum Color { black , red , blue , }; // Teach operator<< how to print a Color // Consider this magic for now since we haven't explained any of the concepts it uses yet // std::ostream is the type of std::cout // The return type and parameter type are references (to prevent copies from being made)! std :: ostream & operator << ( std :: ostream & out , Color color ) { switch ( color ) { case black : out << \"black\" ; break ; case red : out << \"red\" ; break ; case blue : out << \"blue\" ; break ; default : out << \"???\" ; break ; } return out ; } int main () { Color shirt { blue }; std :: cout << \"Your shirt is \" << shirt ; // it works! return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Your shirt is blue \u6269\u5c55\u9605\u8bfb \u5982\u679c\u4f60\u597d\u5947\u7684\u8bdd\uff0c\u8fd9\u91cc\u7b80\u5355\u4ecb\u7ecd\u4e00\u4e0b\u4e0a\u9762\u4ee3\u7801\u7684\u5de5\u4f5c\u539f\u7406\u3002\u5f53\u6211\u4eec\u5c1d\u8bd5\u901a\u8fc7 std::cout \u548c operator<< \u6253\u5370 shirt \u65f6\uff0c\u7f16\u8bd1\u5668\u53d1\u73b0\u88ab\u91cd\u8f7d\u7684 operator<< \u53ef\u4ee5\u914d\u5408 Color \u7c7b\u578b\u7684\u5bf9\u8c61\u5de5\u4f5c\u3002\u4e8e\u662f\u91cd\u8f7d\u7684 operator<< \u88ab\u8c03\u7528\uff0c\u53c2\u6570 out \u662f std::cout \uff0c\u53c2\u6570 color \u5219\u662f shirt \u3002\u56e0\u4e3a out \u662f std::cout \u7684\u5f15\u7528\uff0c\u6240\u4ee5 out << \"blue\" \u5c31\u662f\u5c06 \"blue\" \u9001\u5230 std::cout \u6253\u5370\u3002 \u6211\u4eec\u4f1a\u5728 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 \u4e2d\u4ecb\u7ecdIO\u8fd0\u7b97\u7b26\u7684\u91cd\u8f7d\u3002\u76ee\u524d\u4f60\u53ef\u4ee5\u5148\u62f7\u8d1d\u8fd9\u4efd\u4ee3\u7801\u5e76\u7528\u4e8e\u4f60\u81ea\u5df1\u7684\u679a\u4e3e\u7c7b\u578b\u3002 \u679a\u4e3e\u7684\u5927\u5c0f\u548c\u57fa\u7c7b\u578b \u679a\u4e3e\u7c7b\u578b\u88ab\u8ba4\u4e3a\u662f\u6574\u6570\u7c7b\u578b\u5bb6\u65cf\u7684\u4e00\u90e8\u5206\uff0c\u7531\u7f16\u8bd1\u5668\u51b3\u5b9a\u4e3a\u679a\u4e3e\u53d8\u91cf\u5206\u914d\u591a\u5c11\u5185\u5b58\u3002C++\u6807\u51c6\u89c4\u5b9a\uff0c\u679a\u4e3e\u7684\u5927\u5c0f\u9700\u8981\u5927\u5230\u8db3\u4ee5\u8868\u793a\u6240\u6709\u679a\u4e3e\u503c\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5b83\u5c06\u4f7f\u679a\u4e3e\u53d8\u91cf\u7684\u5927\u5c0f\u4e0e\u6807\u51c6\u7684 int \u76f8\u540c\u3002 \u4f46\u662f\uff0c\u53ef\u4ee5\u6307\u5b9a\u4e0d\u540c\u7684\u57fa\u7840\u7c7b\u578b\u3002\u4f8b\u5982\uff0c \u5982\u679c\u4f60\u5728\u5f00\u53d1\u67d0\u4e2a\u5e26\u5bbd\u654f\u611f\u7684\u7a0b\u5e8f(\u4f8b\u5982\u901a\u8fc7\u7f51\u7edc\u53d1\u9001\u6570\u636e)\uff0c\u90a3\u4e48\u6b64\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u6307\u5b9a\u4e00\u4e2a\u66f4\u5c0f\u7684\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 // Use an 8-bit unsigned integer as the enum base enum Color : std :: uint8_t { black , red , blue , }; \u7531\u4e8e\u679a\u4e3e\u6570\u901a\u5e38\u4e0d\u7528\u4e8e\u7b97\u672f\u6216\u4e0e\u6574\u6570\u6bd4\u8f83\uff0c\u5982\u679c\u9700\u8981\uff0c\u4f7f\u7528\u65e0\u7b26\u53f7\u6574\u6570\u901a\u5e38\u662f\u5b89\u5168\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u4ec5\u5728\u5fc5\u8981\u65f6\u6307\u5b9a\u679a\u4e3e\u7684\u57fa\u7c7b\u578b \u6574\u578b\u8f6c\u6362\u4e3a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e \u867d\u7136\u7f16\u8bd1\u5668\u53ef\u4ee5\u9690\u5f0f\u5730\u5c06 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u4f46\u5b83\u5c06\u4e0d\u80fd\u9690\u5f0f\u5730\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u3002\u4ee5\u4e0b\u64cd\u4f5c\u5c06\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include enum Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { Pet pet { 2 }; // compile error: integer value 2 won't implicitly convert to a Pet pet = 3 ; // compile error: integer value 3 won't implicitly convert to a Pet return 0 ; } \u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002 \u9996\u5148\uff0c\u53ef\u4ee5\u4f7f\u7528 static_cast \u5f3a\u5236\u7f16\u8bd1\u5668\u5c06\u4e00\u4e2a\u6574\u6570\u8f6c\u6362\u4e3a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include enum Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { Pet pet { static_cast < Pet > ( 2 ) }; // convert integer 2 to a Pet pet = static_cast < Pet > ( 3 ); // our pig evolved into a whale! return 0 ; } \u7a0d\u540e\u6211\u4eec\u4f1a\u4e3e\u4e00\u4e2a\u4f7f\u7528\u8be5\u65b9\u6cd5\u7684\u4f8b\u5b50\u3002 \u6b64\u5916\uff0c\u5728 C++17 \u4e2d\uff0c \u5982\u679c\u4e00\u4e2a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u6709\u4e00\u4e2a\u57fa\u7c7b\u578b\uff0c\u5219\u7f16\u8bd1\u5668\u5141\u8bb8\u4f60\u4f7f\u7528\u6574\u578b\u521d\u59cb\u5316\uff08\u975e\u8d4b\u503c\uff09\u8be5\u679a\u4e3e\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include enum Pet : int // we've specified a base { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { Pet pet { 2 }; // ok: can initialize with integer pet = 3 ; // compile error: can not assign with integer return 0 ; } \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u503c\u7684\u8f93\u5165 \u7531\u4e8e Pet \u662f\u4e00\u4e2a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\uff0c\u6240\u4ee5C++\u5e76\u4e0d\u77e5\u9053\u5982\u4f55\u4ece std::cin \u8f93\u5165\u4e00\u4e2a Pet \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include enum Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { Pet pet { pig }; std :: cin >> pet ; // compile error, std::cin doesn't know how to input a Pet return 0 ; } \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u8bfb\u5165\u4e00\u4e2a\u6574\u578b\u6570\uff0c\u7136\u540e\u4f7f\u7528 static_cast \u5c06\u5176 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u4e3a\u5408\u9002\u7684\u679a\u4e3e\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include enum Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { std :: cout << \"Enter a pet (0=cat, 1=dog, 2=pig, 3=whale): \" ; int input {}; std :: cin >> input ; // input an integer Pet pet { static_cast < Pet > ( input ) }; // static_cast our integer to a Pet return 0 ; } \u6269\u5c55\u9605\u8bfb \u7c7b\u4f3c\u4e8e\u91cd\u8f7d operator<< \u4f7f\u5176\u80fd\u591f\u8f93\u51fa\u679a\u4e3e\u7c7b\u578b\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u91cd\u8f7d operator>> \u4f7f\u5176\u6307\u5b9a\u5982\u4f55\u8f93\u5165\u4e00\u4e2a\u679a\u4e3e\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include enum Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; // Consider this magic for now // We pass pet by reference so we can have the function modify its value std :: istream & operator >> ( std :: istream & in , Pet & pet ) { int input {}; in >> input ; // input an integer pet = static_cast < Pet > ( input ); return in ; } int main () { std :: cout << \"Enter a pet (0=cat, 1=dog, 2=pig, 3=whale): \" ; Pet pet {}; std :: cin >> pet ; // input our pet using std::cin std :: cout << pet << '\\n' ; // prove that it worked return 0 ; } \u540c\u6837\u5730\uff0c\u4f60\u53ef\u4ee5\u5148\u4f7f\u7528\u8fd9\u6bb5\u795e\u5947\u7684\u4ee3\u7801(\u56e0\u4e3a\u6211\u4eec\u8fd8\u6ca1\u6709\u89e3\u91ca\u5b83\u80cc\u540e\u7684\u6982\u5ff5)\uff0c\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5b83\u7684\u539f\u7406\u3002","title":"10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa"},{"location":"10-3-unscoped-enumeration-input-and-output/#_1","text":"\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include enum Color { black , // assigned 0 red , // assigned 1 blue , // assigned 2 green , // assigned 3 white , // assigned 4 cyan , // assigned 5 yellow , // assigned 6 magenta , // assigned 7 }; int main () { Color shirt { blue }; std :: cout << \"Your shirt is \" << shirt ; // what does this do? return 0 ; } \u56e0\u4e3a\u679a\u4e3e\u7c7b\u578b\u4fdd\u5b58\u7684\u662f\u6574\u578b\u503c\uff0c\u6240\u4ee5\u5982\u4f60\u6240\u671f\u5f85\u7684\u90a3\u6837\uff0c\u4e0a\u9762\u7684\u7a0b\u5e8f\u6253\u5370\uff1a 1 Your shirt is 2 \u5f53\u679a\u4e3e\u7c7b\u578b\u7528\u4e8e\u51fd\u6570\u8c03\u7528\u6216\u4e0e\u8fd0\u7b97\u7b26\u4e00\u8d77\u4f7f\u7528\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u9996\u5148\u5c1d\u8bd5\u67e5\u627e\u4e0e\u679a\u4e3e\u7c7b\u578b\u5339\u914d\u7684\u51fd\u6570\u6216\u8fd0\u7b97\u7b26\u3002\u4f8b\u5982\uff0c\u5f53\u7f16\u8bd1\u5668\u8bd5\u56fe\u7f16\u8bd1 std::cout << shirt \u65f6\uff0c\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u67e5\u770b operator<< \u662f\u5426\u77e5\u9053\u5982\u4f55\u5c06 Color \u7c7b\u578b\u7684\u5bf9\u8c61(\u56e0\u4e3a shirt \u7684\u7c7b\u578b\u662f Color )\u6253\u5370\u5230 std::cout \u3002\u5b83\u4e0d\u77e5\u9053\u3002 \u5982\u679c\u7f16\u8bd1\u5668\u65e0\u6cd5\u627e\u5230\u5339\u914d\u7684\u8fd0\u7b97\u7b26\uff0c\u5219\u5b83\u4f1a\u5c06 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u6574\u578b\u3002\u56e0\u4e3a std::cout \u77e5\u9053\u5982\u4f55\u6253\u5370\u4e00\u4e2a\u6574\u578b\uff0c\u6240\u4ee5 shirt \u4f1a\u88ab\u8f6c\u6362\u4e3a\u6574\u578b\u5e76\u6253\u5370\u51fa2\u3002","title":"\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u4f1a\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u6574\u578b\u503c"},{"location":"10-3-unscoped-enumeration-input-and-output/#_2","text":"\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5c06\u679a\u4e3e\u8f6c\u6362\u4e3a\u6574\u578b\u6253\u5370\u51fa\u6765\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c\u3002\u6211\u4eec\u80af\u5b9a\u662f\u5e0c\u671b\u80fd\u591f\u5c06\u679a\u4e3e\u503c\u7684\u672c\u540d\u6253\u5370\u51fa\u6765 (\u4f8b\u5982 blue )\u3002\u4f46\u662f\uff0c\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e00\u70b9\u6211\u4eec\u9700\u8981\u4e00\u4e9b\u65b9\u6cd5\u5c06\u679a\u4e3e\u5728\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u6253\u5370\u51fa\u6765( \"blue\" )\u3002 \u622a\u81f3C++ 20, C++\u8fd8\u6ca1\u6709\u63d0\u4f9b\u4efb\u4f55\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u81ea\u5df1\u5bfb\u627e\u89e3\u51b3\u65b9\u6848\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u8fd9\u5e76\u4e0d\u96be\u3002\u5b9e\u73b0\u8fd9\u4e00\u70b9\u7684\u5178\u578b\u65b9\u6cd5\u662f\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u679a\u4e3e\u7c7b\u578b\u4f5c\u4e3a\u53c2\u6570\uff0c\u7136\u540e\u8f93\u51fa\u76f8\u5e94\u7684\u5b57\u7b26\u4e32(\u6216\u5c06\u5b57\u7b26\u4e32\u8fd4\u56de\u7ed9\u8c03\u7528\u8005)\u3002 \u5178\u578b\u7684\u65b9\u6cd5\u662f\u5224\u65ad\u5f53\u524d\u9700\u8981\u6253\u5370\u7684\u662f\u54ea\u4e2a\u7684\u679a\u4e3e\u503c\uff1a 1 2 3 4 5 6 7 8 // Using if-else for this is inefficient void printColor ( Color color ) { if ( color == black ) std :: cout << \"black\" ; else if ( color == red ) std :: cout << \"red\" ; else if ( color == blue ) std :: cout << \"blue\" ; else std :: cout << \"???\" ; } \u4f46\u662f\uff0c\u4e3a\u6b64\u4f7f\u7528\u4e00\u7cfb\u5217if-else\u8bed\u53e5\u662f\u4f4e\u6548\u7684\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u591a\u6b21\u6bd4\u8f83\u624d\u80fd\u627e\u5230\u5339\u914d\u3002\u5b8c\u6210\u540c\u6837\u4efb\u52a1\u7684\u4e00\u79cd\u66f4\u6709\u6548\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 switch \u8bed\u53e5\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8fd8\u5c06\u4ee5 std::string \u7684\u5f62\u5f0f\u8fd4\u56de Color \uff0c\u4ee5\u7ed9\u8c03\u7528\u8005\u66f4\u591a\u7684\u7075\u6d3b\u6027\u6765\u5bf9\u540d\u79f0\u505a\u4efb\u4f55\u60f3\u505a\u7684\u4e8b\u60c5(\u5305\u62ec\u6253\u5370\u5b83): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include #include enum Color { black , red , blue , }; // We'll show a better version of this for C++17 below std :: string getColor ( Color color ) { switch ( color ) { case black : return \"black\" ; case red : return \"red\" ; case blue : return \"blue\" ; default : return \"???\" ; } } int main () { Color shirt { blue }; std :: cout << \"Your shirt is \" << getColor ( shirt ) << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Your shirt is blue \u8fd9\u4e48\u505a\u6bd4\u4f7f\u7528\u591a\u4e2aif-else\u5224\u65ad\u66f4\u9ad8\u6548\uff08\u56e0\u4e3aswitch\u8bed\u53e5\u6bd4if\u66f4\u9ad8\u6548\uff09\uff0c\u800c\u4e14\u53ef\u8bfb\u6027\u4e5f\u66f4\u597d\u3002\u4f46\u662f\uff0c\u8fd9\u4e2a\u7248\u672c\u7684\u4ee3\u7801\u4e5f\u4e0d\u662f\u5f88\u9ad8\u6548\uff0c\u56e0\u4e3a\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u90fd\u5fc5\u987b\u521b\u5efa\u5e76\u8fd4\u56de\u4e00\u4e2a std::string (\u5f00\u9500\u5f88\u5927)\u3002 \u5728 C++17 \u4e2d\uff0c\u66f4\u9ad8\u6548\u7684\u505a\u6cd5\u662f\u5c06 std::string \u66ff\u6362\u4e3a std::string_view \u3002 std::string_view \u4f1a\u4ee5\u67d0\u79cd\u62f7\u8d1d\u5f00\u9500\u66f4\u5c0f\u7684\u65b9\u5f0f\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include #include // C++17 enum Color { black , red , blue , }; constexpr std :: string_view getColor ( Color color ) // C++17 { switch ( color ) { case black : return \"black\" ; case red : return \"red\" ; case blue : return \"blue\" ; default : return \"???\" ; } } int main () { Color shirt { blue }; std :: cout << \"Your shirt is \" << getColor ( shirt ) << '\\n' ; return 0 ; } COPY \u76f8\u5173\u5185\u5bb9 Constexpr \u8fd4\u56de\u7c7b\u578b\u5728 6.14 - Constexpr \u548c consteval \u51fd\u6570 \u4e2d\u4ecb\u7ecd\u3002","title":"\u6253\u5370\u679a\u4e3e\u503c\u7684\u540d\u5b57"},{"location":"10-3-unscoped-enumeration-input-and-output/#operator","text":"\u5c3d\u7ba1\u4e0a\u9762\u7684\u4f8b\u5b50\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\uff0c\u4f46\u662f\u6211\u4eec\u5fc5\u987b\u8981\u80fd\u591f\u8bb0\u4f4f\u54ea\u4e2a\u51fd\u6570\u662f\u7528\u6765\u83b7\u53d6\u679a\u4e3e\u503c\u540d\u5b57\u7684\u3002\u5c3d\u7ba1\u591a\u6570\u60c5\u51b5\u4e0b\u8fd9\u4e5f\u5e76\u4e0d\u9ebb\u70e6\uff0c\u4f46\u662f\u5982\u679c\u679a\u4e3e\u7c7b\u578b\u5f88\u591a\u7684\u65f6\u5019\uff0c\u8fd8\u662f\u5f88\u53ef\u80fd\u4f1a\u5e26\u6765\u95ee\u9898\u3002\u4f7f\u7528\u8fd0\u7b97\u7b26\u91cd\u8f7d\uff0c\u6211\u4eec\u53ef\u4ee5\u6559\u4f1a\u8fd0\u7b97\u7b26 operator<< \u5982\u4f55\u6253\u5370\u7a0b\u5e8f\u5b9a\u4e49\u679a\u4e3e\u7c7b\u578b\uff01\u7531\u4e8e\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u5148\u5c06\u4e0b\u9762\u7684\u4ee3\u7801\u5f53\u505a\u4e00\u79cd\u9b54\u6cd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include enum Color { black , red , blue , }; // Teach operator<< how to print a Color // Consider this magic for now since we haven't explained any of the concepts it uses yet // std::ostream is the type of std::cout // The return type and parameter type are references (to prevent copies from being made)! std :: ostream & operator << ( std :: ostream & out , Color color ) { switch ( color ) { case black : out << \"black\" ; break ; case red : out << \"red\" ; break ; case blue : out << \"blue\" ; break ; default : out << \"???\" ; break ; } return out ; } int main () { Color shirt { blue }; std :: cout << \"Your shirt is \" << shirt ; // it works! return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Your shirt is blue \u6269\u5c55\u9605\u8bfb \u5982\u679c\u4f60\u597d\u5947\u7684\u8bdd\uff0c\u8fd9\u91cc\u7b80\u5355\u4ecb\u7ecd\u4e00\u4e0b\u4e0a\u9762\u4ee3\u7801\u7684\u5de5\u4f5c\u539f\u7406\u3002\u5f53\u6211\u4eec\u5c1d\u8bd5\u901a\u8fc7 std::cout \u548c operator<< \u6253\u5370 shirt \u65f6\uff0c\u7f16\u8bd1\u5668\u53d1\u73b0\u88ab\u91cd\u8f7d\u7684 operator<< \u53ef\u4ee5\u914d\u5408 Color \u7c7b\u578b\u7684\u5bf9\u8c61\u5de5\u4f5c\u3002\u4e8e\u662f\u91cd\u8f7d\u7684 operator<< \u88ab\u8c03\u7528\uff0c\u53c2\u6570 out \u662f std::cout \uff0c\u53c2\u6570 color \u5219\u662f shirt \u3002\u56e0\u4e3a out \u662f std::cout \u7684\u5f15\u7528\uff0c\u6240\u4ee5 out << \"blue\" \u5c31\u662f\u5c06 \"blue\" \u9001\u5230 std::cout \u6253\u5370\u3002 \u6211\u4eec\u4f1a\u5728 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 \u4e2d\u4ecb\u7ecdIO\u8fd0\u7b97\u7b26\u7684\u91cd\u8f7d\u3002\u76ee\u524d\u4f60\u53ef\u4ee5\u5148\u62f7\u8d1d\u8fd9\u4efd\u4ee3\u7801\u5e76\u7528\u4e8e\u4f60\u81ea\u5df1\u7684\u679a\u4e3e\u7c7b\u578b\u3002","title":"\u8ba9\u00a0operator<<\u00a0\u77e5\u9053\u5982\u4f55\u6253\u5370\u679a\u4e3e\u503c"},{"location":"10-3-unscoped-enumeration-input-and-output/#_3","text":"\u679a\u4e3e\u7c7b\u578b\u88ab\u8ba4\u4e3a\u662f\u6574\u6570\u7c7b\u578b\u5bb6\u65cf\u7684\u4e00\u90e8\u5206\uff0c\u7531\u7f16\u8bd1\u5668\u51b3\u5b9a\u4e3a\u679a\u4e3e\u53d8\u91cf\u5206\u914d\u591a\u5c11\u5185\u5b58\u3002C++\u6807\u51c6\u89c4\u5b9a\uff0c\u679a\u4e3e\u7684\u5927\u5c0f\u9700\u8981\u5927\u5230\u8db3\u4ee5\u8868\u793a\u6240\u6709\u679a\u4e3e\u503c\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5b83\u5c06\u4f7f\u679a\u4e3e\u53d8\u91cf\u7684\u5927\u5c0f\u4e0e\u6807\u51c6\u7684 int \u76f8\u540c\u3002 \u4f46\u662f\uff0c\u53ef\u4ee5\u6307\u5b9a\u4e0d\u540c\u7684\u57fa\u7840\u7c7b\u578b\u3002\u4f8b\u5982\uff0c \u5982\u679c\u4f60\u5728\u5f00\u53d1\u67d0\u4e2a\u5e26\u5bbd\u654f\u611f\u7684\u7a0b\u5e8f(\u4f8b\u5982\u901a\u8fc7\u7f51\u7edc\u53d1\u9001\u6570\u636e)\uff0c\u90a3\u4e48\u6b64\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u6307\u5b9a\u4e00\u4e2a\u66f4\u5c0f\u7684\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 // Use an 8-bit unsigned integer as the enum base enum Color : std :: uint8_t { black , red , blue , }; \u7531\u4e8e\u679a\u4e3e\u6570\u901a\u5e38\u4e0d\u7528\u4e8e\u7b97\u672f\u6216\u4e0e\u6574\u6570\u6bd4\u8f83\uff0c\u5982\u679c\u9700\u8981\uff0c\u4f7f\u7528\u65e0\u7b26\u53f7\u6574\u6570\u901a\u5e38\u662f\u5b89\u5168\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u4ec5\u5728\u5fc5\u8981\u65f6\u6307\u5b9a\u679a\u4e3e\u7684\u57fa\u7c7b\u578b","title":"\u679a\u4e3e\u7684\u5927\u5c0f\u548c\u57fa\u7c7b\u578b"},{"location":"10-3-unscoped-enumeration-input-and-output/#_4","text":"\u867d\u7136\u7f16\u8bd1\u5668\u53ef\u4ee5\u9690\u5f0f\u5730\u5c06 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u4f46\u5b83\u5c06\u4e0d\u80fd\u9690\u5f0f\u5730\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u3002\u4ee5\u4e0b\u64cd\u4f5c\u5c06\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include enum Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { Pet pet { 2 }; // compile error: integer value 2 won't implicitly convert to a Pet pet = 3 ; // compile error: integer value 3 won't implicitly convert to a Pet return 0 ; } \u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002 \u9996\u5148\uff0c\u53ef\u4ee5\u4f7f\u7528 static_cast \u5f3a\u5236\u7f16\u8bd1\u5668\u5c06\u4e00\u4e2a\u6574\u6570\u8f6c\u6362\u4e3a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include enum Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { Pet pet { static_cast < Pet > ( 2 ) }; // convert integer 2 to a Pet pet = static_cast < Pet > ( 3 ); // our pig evolved into a whale! return 0 ; } \u7a0d\u540e\u6211\u4eec\u4f1a\u4e3e\u4e00\u4e2a\u4f7f\u7528\u8be5\u65b9\u6cd5\u7684\u4f8b\u5b50\u3002 \u6b64\u5916\uff0c\u5728 C++17 \u4e2d\uff0c \u5982\u679c\u4e00\u4e2a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u6709\u4e00\u4e2a\u57fa\u7c7b\u578b\uff0c\u5219\u7f16\u8bd1\u5668\u5141\u8bb8\u4f60\u4f7f\u7528\u6574\u578b\u521d\u59cb\u5316\uff08\u975e\u8d4b\u503c\uff09\u8be5\u679a\u4e3e\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include enum Pet : int // we've specified a base { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { Pet pet { 2 }; // ok: can initialize with integer pet = 3 ; // compile error: can not assign with integer return 0 ; }","title":"\u6574\u578b\u8f6c\u6362\u4e3a\u65e0\u4f5c\u7528\u57df\u679a\u4e3e"},{"location":"10-3-unscoped-enumeration-input-and-output/#_5","text":"\u7531\u4e8e Pet \u662f\u4e00\u4e2a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\uff0c\u6240\u4ee5C++\u5e76\u4e0d\u77e5\u9053\u5982\u4f55\u4ece std::cin \u8f93\u5165\u4e00\u4e2a Pet \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include enum Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { Pet pet { pig }; std :: cin >> pet ; // compile error, std::cin doesn't know how to input a Pet return 0 ; } \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u8bfb\u5165\u4e00\u4e2a\u6574\u578b\u6570\uff0c\u7136\u540e\u4f7f\u7528 static_cast \u5c06\u5176 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u4e3a\u5408\u9002\u7684\u679a\u4e3e\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include enum Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { std :: cout << \"Enter a pet (0=cat, 1=dog, 2=pig, 3=whale): \" ; int input {}; std :: cin >> input ; // input an integer Pet pet { static_cast < Pet > ( input ) }; // static_cast our integer to a Pet return 0 ; } \u6269\u5c55\u9605\u8bfb \u7c7b\u4f3c\u4e8e\u91cd\u8f7d operator<< \u4f7f\u5176\u80fd\u591f\u8f93\u51fa\u679a\u4e3e\u7c7b\u578b\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u91cd\u8f7d operator>> \u4f7f\u5176\u6307\u5b9a\u5982\u4f55\u8f93\u5165\u4e00\u4e2a\u679a\u4e3e\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include enum Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; // Consider this magic for now // We pass pet by reference so we can have the function modify its value std :: istream & operator >> ( std :: istream & in , Pet & pet ) { int input {}; in >> input ; // input an integer pet = static_cast < Pet > ( input ); return in ; } int main () { std :: cout << \"Enter a pet (0=cat, 1=dog, 2=pig, 3=whale): \" ; Pet pet {}; std :: cin >> pet ; // input our pet using std::cin std :: cout << pet << '\\n' ; // prove that it worked return 0 ; } \u540c\u6837\u5730\uff0c\u4f60\u53ef\u4ee5\u5148\u4f7f\u7528\u8fd9\u6bb5\u795e\u5947\u7684\u4ee3\u7801(\u56e0\u4e3a\u6211\u4eec\u8fd8\u6ca1\u6709\u89e3\u91ca\u5b83\u80cc\u540e\u7684\u6982\u5ff5)\uff0c\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5b83\u7684\u539f\u7406\u3002","title":"\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u503c\u7684\u8f93\u5165"},{"location":"10-4-scoped-enumerations-enum-classes/","tags":["enum","scoped enumerations","C++20","C++17"],"text":"Key Takeaway \u5c3d\u7ba1 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u5728C++\u4e2d\u662f\u53ef\u533a\u5206\u7c7b\u578b\uff0c\u4f46\u5b83\u4eec\u5e76\u4e0d\u662f \u7c7b\u578b\u5b89\u5168 \u7684\uff0c\u800c\u4e14\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u5b83\u5141\u8bb8\u4f60\u505a\u51fa\u4e00\u4e9b\u4e0d\u5408\u7406\u7684\u64cd\u4f5c\u3002\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include int main () { enum Color { red , blue , }; enum Fruit { banana , apple , }; Color color { red }; Fruit fruit { banana }; if ( color == fruit ) // \u7f16\u8bd1\u5668\u4f1a\u5c06 color \u548c fruit \u5f53\u505a\u6574\u578b\u8fdb\u884c\u6bd4\u8f83 std :: cout << \"color and fruit are equal \\n \" ; // \u5e76\u8ba4\u4e3a\u5b83\u4eec\u76f8\u7b49 else std :: cout << \"color and fruit are not equal \\n \" ; return 0 ; } \u6253\u5370\uff1a 1 color and fruit are equal \u5f53 color \u548c fruit \u8fdb\u884c\u6bd4\u8f83\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u9996\u5148\u5224\u65ad\u5b83\u662f\u5426\u77e5\u9053\u5982\u4f55\u6bd4\u8f83 Color \u548c Fruit \u3002\u5b83\u5e76\u4e0d\u77e5\u9053\u3002\u7136\u540e\uff0c\u5b83\u5c31\u4f1a\u5c1d\u8bd5\u5c06 Color \u6216 Fruit \u8f6c\u6362\u4e3a\u6574\u578b\u5e76\u7ee7\u7eed\u67e5\u770b\u662f\u6709\u5339\u914d\u7684\u6bd4\u8f83\u65b9\u6cd5\u3002\u6700\u7ec8\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c06\u4e8c\u8005\u90fd\u8f6c\u6362\u4e3a\u6574\u578b\u5e76\u8fdb\u884c\u6bd4\u8f83\u3002\u56e0\u4e3a color \u548c fruit \u7684\u679a\u4e3e\u503c\u90fd\u8f6c\u6362\u4e3a0\uff0c\u6240\u4ee5\u5b83\u4eec\u662f\u76f8\u7b49\u7684\u3002 \u8fd9\u663e\u7136\u4e0d\u5408\u7406\uff0c\u56e0\u4e3a color \u548c fruit \u6765\u81ea\u4e24\u4e2a\u4e0d\u540c\u7684\u679a\u4e3e\u7c7b\u578b\uff0c\u5b83\u4eec\u672c\u6765\u662f\u4e0d\u5177\u6709\u53ef\u6bd4\u6027\u7684\u3002\u5bf9\u4e8e\u4e00\u822c\u7684\u679a\u4e3e\u6765\u8bf4\uff0c\u6211\u4eec\u5e76\u6ca1\u6709\u7b80\u5355\u7684\u529e\u6cd5\u653e\u7f6e\u6b64\u7c7b\u4e8b\u60c5\u53d1\u751f\u3002 \u56e0\u4e3a\u4e0a\u8ff0\u95ee\u9898\u7684\u5b58\u5728\uff0c\u4ee5\u53ca \u547d\u540d\u7a7a\u95f4 \u6c61\u67d3\u95ee\u9898\uff0cC++\u8bbe\u8ba1\u8005\u51b3\u5b9a\u63d0\u4f9b\u4e00\u4e2a\u5e72\u51c0\u5229\u843d\u5730\u89e3\u51b3\u65b9\u6848\u3002 \u6709\u4f5c\u7528\u57df\u679a\u4e3e \u8fd9\u4e2a\u89e3\u51b3\u65b9\u6848\u5c31\u662f \u6709\u4f5c\u7528\u57df\u679a\u4e3e (\u5728C++\u4e2d\u901a\u5e38\u79f0\u4e3a \u679a\u4e3e\u7c7b \uff0c\u81f3\u4e8e\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u53eb\u5b83\uff0c\u5f88\u5feb\u5c31\u4f1a\u4e0d\u8a00\u81ea\u660e\u4e86)\u3002 \u6709\u4f5c\u7528\u57df\u679a\u4e3e\u548c\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\uff08 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \uff09\u7528\u6cd5\u7c7b\u4f3c\uff0c\u4e0d\u8fc7\u5b83\u4eec\u6709\u4e24\u4e2a\u4e3b\u8981\u7684\u533a\u522b\uff1a\u5176\u4e00\uff0c\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u662f\u5f3a\u7c7b\u578b\u7684(\u5b83\u4e0d\u4f1a\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u6574\u578b) \uff0c\u5176\u4e8c\u5b83\u5177\u6709\u5f3a\u4f5c\u7528\u57df\u7ea6\u675f (\u5b83\u7684\u679a\u4e3e\u503c \u53ea\u4f1a \u88ab\u653e\u7f6e\u5728\u679a\u4e3e\u7c7b\u7684\u4f5c\u7528\u57df\u4e2d)\u3002 \u521b\u5efa\u6709\u4f5c\u7528\u57df\u679a\u4e3e\uff0c\u9700\u8981\u4f7f\u7528\u5173\u952e\u5b57 enum class \u3002\u5b9a\u4e49\u7684\u5176\u4ed6\u90e8\u5206\u5219\u548c\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u4e00\u6837\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include int main () { enum class Color // \"enum class\" \u5b9a\u4e49\u4e86\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b { red , // red \u662f Color \u4f5c\u7528\u57df\u7684\u6210\u5458 blue , }; enum class Fruit { banana , // banana \u662f Fruit \u4f5c\u7528\u57df\u7684\u6210\u5458 apple , }; Color color { Color :: red }; // \u6ce8\u610f: red \u4e0d\u80fd\u591f\u88ab\u76f4\u63a5\u8bbf\u95ee\uff0c\u5fc5\u987b\u4f7f\u7528 Color::red Fruit fruit { Fruit :: banana }; // note: banana \u4e0d\u80fd\u591f\u88ab\u76f4\u63a5\u8bbf\u95ee, \u5fc5\u987b\u4f7f\u7528 Fruit::banana if ( color == fruit ) // \u7f16\u8bd1\u9519\u8bef\uff1a\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053\u5982\u4f55\u6bd4\u8f83 Color \u548c Fruit std :: cout << \"color and fruit are equal \\n \" ; else std :: cout << \"color and fruit are not equal \\n \" ; return 0 ; } \u7f16\u8bd1\u5668\u4f1a\u62a5\u544a\u7a0b\u5e8f\u7684\u7b2c19\u884c\u5b58\u5728\u9519\u8bef\uff0c\u56e0\u4e3a\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u4e0d\u80fd\u591f\u88ab\u8f6c\u6362\u4e3a\u4efb\u4f55\u53ef\u6bd4\u8f83\u7684\u5176\u4ed6\u7c7b\u578b\u3002 \u9898\u5916\u8bdd class \u5173\u952e\u5b57 (\u4ee5\u53ca static \u5173\u952e\u5b57)\u662f C++ \u4e2d\u6700\u88ab\u201c\u6ee5\u7528\u201d\u7684\u5173\u952e\u5b57\uff0c\u5b83\u5728\u4e0d\u540c\u8bed\u5883\u4e0b\u5177\u6709\u4e0d\u540c\u7684\u542b\u4e49\u3002\u5c3d\u7ba1\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u4f7f\u7528\u4e86 class \u5173\u952e\u5b57\uff0c\u4f46\u662f\u5b83\u4eec\u5e76\u4e0d\u662f\u7c7b\u7c7b\u578b\uff08\u7c7b\u7c7b\u578b\u5305\u62ec\u7ed3\u6784\u4f53\u3001\u7c7b\u548c\u8054\u5408\u4f53\uff09\u3002 \u6709\u4f5c\u7528\u57df\u679a\u4e3e\u4f1a\u5b9a\u4e49\u81ea\u5df1\u7684\u4f5c\u7528\u57df \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u4f1a\u5c06\u5176\u679a\u4e3e\u503c\u548c\u81ea\u5df1\u653e\u7f6e\u5728\u76f8\u540c\u7684\u4f5c\u7528\u57df\u4e2d\uff0c\u800c\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u4f1a\u5c06\u5b83\u7684\u679a\u4e3e\u503c\u653e\u7f6e\u5728\u81ea\u5df1\u5b9a\u4e49\u7684\u4f5c\u7528\u57df\u4e2d\u3002\u6362\u8a00\u4e4b\uff0c\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u540c\u65f6\u662f\u5176\u679a\u4e3e\u503c\u7684\u547d\u540d\u7a7a\u95f4\u3002\u8fd9\u79cd\u81ea\u5e26\u7684\u4f5c\u7528\u57df\u6709\u52a9\u4e8e\u907f\u514d\u547d\u540d\u7a7a\u95f4\u6c61\u67d3\u548c\u6f5c\u5728\u7684\u547d\u540d\u51b2\u7a81\u3002 \u8981\u8bbf\u95ee\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u503c\uff0c\u5c31\u50cf\u5b83\u4f4d\u4e8e\u4e0e\u4f5c\u7528\u57df\u679a\u4e3e\u540c\u540d\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\u4e00\u6837: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { enum class Color // \"enum class\" defines this as a scoped enum rather than an unscoped enum { red , // red is considered part of Color's scope region blue , }; std :: cout << red << '\\n' ; // compile error: red not defined in this scope region std :: cout << Color :: red << '\\n' ; // compile error: std::cout doesn't know how to print this (will not implicitly convert to int) Color color { Color :: blue }; // okay return 0 ; } \u56e0\u4e3a\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u4e3a\u679a\u4e3e\u503c\u63d0\u4f9b\u4e86\u5b83\u4eec\u81ea\u5df1\u7684\u9690\u5f0f\u540d\u79f0\u7a7a\u95f4\uff0c\u6240\u4ee5\u6ca1\u6709\u5fc5\u8981\u5c06\u4f5c\u7528\u57df\u679a\u4e3e\u653e\u5728\u53e6\u4e00\u4e2a\u4f5c\u7528\u57df(\u6bd4\u5982\u540d\u79f0\u7a7a\u95f4)\u4e2d\uff0c\u9664\u975e\u6709\u5176\u4ed6\u4ee4\u4eba\u4fe1\u670d\u7684\u7406\u7531\u8fd9\u6837\u505a\uff0c\u56e0\u4e3a\u8fd9\u6837\u505a\u662f\u591a\u4f59\u7684\u3002 \u6709\u4f5c\u7528\u57df\u679a\u4e3e\u4e0d\u4f1a\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u6574\u578b \u548c\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u4e0d\u540c\uff0c\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u4e0d\u4f1a\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u6574\u578b\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u662f\u6709\u76ca\u5730\uff0c\u56e0\u4e3a\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u8fd9\u6837\u7684\u8f6c\u6362\u90fd\u4e0d\u5408\u7406\uff0c\u6240\u4ee5\u8fd9\u6709\u52a9\u4e8e\u5e2e\u6211\u4eec\u907f\u514d\u8bed\u4e49\u9519\u8bef\uff0c\u4f8b\u5982\u6bd4\u8f83\u4e24\u4e2a\u4e0d\u540c\u679a\u4e3e\u7c7b\u578b\u4e2d\u7684\u679a\u4e3e\u503c\u6216\u8005\u4f7f\u7528\u679a\u4e3e\u503c\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\uff08 red + 5 \uff09\u3002 \u6ce8\u610f\uff0c\u4f60\u53ef\u4ee5\u6bd4\u8f83\u6765\u81ea\u76f8\u540c\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u4e2d\u7684\u679a\u4e3e\u503c(\u56e0\u4e3a\u5b83\u4eec\u5177\u6709\u76f8\u540c\u7684\u7c7b\u578b)\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { enum class Color { red , blue , }; Color shirt { Color :: red }; if ( shirt == Color :: red ) // Color \u548c Color \u7684\u6bd4\u8f83\u662f\u53ef\u4ee5\u7684 std :: cout << \"The shirt is red! \\n \" ; else if ( shirt == Color :: blue ) std :: cout << \"The shirt is blue! \\n \" ; return 0 ; } \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5c06\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u503c\u4f5c\u4e3a\u6574\u6570\u5904\u7406\u4e5f\u662f\u6709\u7528\u7684\u3002\u5728\u8fd9\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 static_cast \u663e\u5f0f\u5730\u5c06\u679a\u4e3e\u503c\u8f6c\u6362\u4e3a\u6574\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { enum class Color { red , blue , }; Color color { Color :: blue }; std :: cout << color << '\\n' ; // won't work, because there's no implicit conversion to int std :: cout << static_cast < int > ( color ) << '\\n' ; // will print 1 return 0 ; } \u53cd\u8fc7\u6765\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06\u4e00\u4e2a\u6574\u578b\u8f6c\u6362\u4e3a\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u503c\uff0c\u8fd9\u5728\u5904\u7406\u8f93\u5165\u65f6\u5c24\u5176\u6709\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include enum class Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { std :: cout << \"Enter a pet (0=cat, 1=dog, 2=pig, 3=whale): \" ; int input {}; std :: cin >> input ; // input an integer Pet pet { static_cast < Pet > ( input ) }; // static_cast our integer to a Pet return 0 ; } \u5728 C++17 \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u6574\u578b\u6570\uff08\u65e0\u9700\u7c7b\u578b\u8f6c\u6362\uff09\u76f4\u63a5\u521d\u59cb\u5316\u6709\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u800c\u4e14\u4e0d\u50cf\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\uff0c\u4f60\u4e0d\u9700\u8981\u6307\u660e\u4e00\u4e2a\u57fa\u7c7b\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u63a8\u8350\u4f7f\u7528\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u800c\u975e\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\uff0c\u9664\u975e\u4f60\u6709\u8db3\u591f\u7684\u7406\u7531\u4e0d\u8fd9\u4e48\u505a\u3002 \u5c3d\u7ba1\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u63d0\u4f9b\u4e86\u5f88\u591a\u597d\u7684\u529f\u80fd\uff0c\u4f46\u662f\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u5728C++\u4e2d\u4ecd\u7136\u975e\u5e38\u5e38\u89c1\uff0c\u56e0\u4e3a\u5728\u6709\u4e9b\u573a\u5408\u4e0b\u6211\u4eec\u5e0c\u671b\u679a\u4e3e\u503c\u80fd\u591f\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u6574\u578b\uff08\u603b\u662f\u4f7f\u7528 static_cast \u8fdb\u884c\u8f6c\u6362\u6709\u4e9b\u9ebb\u70e6\uff09\uff0c\u800c\u4e14\u6709\u65f6\u6211\u4eec\u4e5f\u4e0d\u9700\u8981\u989d\u5916\u7684\u547d\u540d\u7a7a\u95f4\u5206\u9694\u3002 \u7b80\u5316\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u503c\u5230\u6574\u578b\u7684\u8f6c\u6362(\u8fdb\u9636\u8bdd\u9898) \u6709\u4f5c\u7528\u57df\u679a\u4e3e\u5f88\u597d\uff0c\u4f46\u5b83\u4e0d\u80fd\u591f\u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u8fd9\u4e00\u7279\u70b9\u6709\u65f6\u53ef\u80fd\u4f1a\u662f\u4e00\u4e2a\u75db\u70b9\u3002\u5982\u679c\u9700\u8981\u7ecf\u5e38\u5c06\u4f5c\u7528\u57df\u679a\u4e3e\u8f6c\u6362\u4e3a\u6574\u6570(\u4f8b\u5982\uff0c\u5728\u5e0c\u671b\u4f7f\u7528\u4f5c\u7528\u57df\u679a\u4e3e\u4f5c\u4e3a\u6570\u7ec4\u4e0b\u6807\u7684\u60c5\u51b5\u4e0b)\uff0c\u90a3\u4e48\u6bcf\u6b21\u9700\u8981\u8f6c\u6362\u65f6\u90fd\u5fc5\u987b\u4f7f\u7528 static_cast \uff0c\u8fd9\u65e0\u7591\u4f1a\u4f7f\u4ee3\u7801\u770b\u8d77\u6765\u975e\u5e38\u6df7\u4e71 \u5982\u679c\u4f60\u6709\u8fd9\u6837\u7684\u9700\u6c42\uff0c\u5e0c\u671b\u4f7f\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u503c\u80fd\u591f\u66f4\u5bb9\u6613\u5730\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u90a3\u4e48\u53ef\u4ee5\u5c1d\u8bd5\u91cd\u8f7d\u4e00\u5143 operator+ \u6765\u6267\u884c\u8fd9\u79cd\u8f6c\u6362\u3002\u6211\u4eec\u8fd8\u6ca1\u6709\u89e3\u91ca\u8fd9\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u6240\u4ee5\u73b0\u5728\u5c31\u628a\u5b83\u5f53\u6210\u9b54\u672f\u5427\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include enum class Animals { chicken , // 0 dog , // 1 cat , // 2 elephant , // 3 duck , // 4 snake , // 5 maxAnimals , }; // Overload the unary + operator to convert Animals to the underlying type // adapted from https://stackoverflow.com/a/42198760, thanks to Pixelchemist for the idea constexpr auto operator + ( Animals a ) noexcept { return static_cast < std :: underlying_type_t < Animals >> ( a ); } int main () { std :: cout << + Animals :: elephant << '\\n' ; // convert Animals::elephant to an integer using unary operator+ return 0 ; } \u6253\u5370\uff1a 1 3 \u6b64\u65b9\u6cd5\u53ef\u907f\u514d\u4e0d\u7ecf\u610f\u5730\u9690\u5f0f\u8f6c\u6362\uff0c\u540c\u65f6\u8fd8\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u5730\u663e\u793a\u8f6c\u6362\u65b9\u6cd5\u3002 using enum \u8bed\u53e5 \uff08C++20\uff09 C++20 \u5f15\u5165\u4e86 using enum \u8bed\u53e5\uff0c\u5b83\u53ef\u4ee5\u5c06\u6bcf\u4e2a\u679a\u4e3e\u4f5c\u7528\u57df\u4e2d\u7684\u679a\u4e3e\u503c\u5168\u90e8\u5bfc\u5165\u5230\u5f53\u524d\u4f5c\u7528\u57df\u3002\u5f53\u914d\u5408 \u679a\u4e3e\u7c7b \u4f7f\u7528\u65f6\uff0c\u53ef\u4ee5\u907f\u514d\u4f7f\u7528\u679a\u4e3e\u7c7b\u524d\u7f00\u8bbf\u95ee\u679a\u4e3e\u503c\u3002 \u8fd9\u4e2a\u7279\u6027\u975e\u5e38\u6709\u7528\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u91cd\u590d\u8f93\u5165\u5927\u91cf\u76f8\u540c\u524d\u7f00\u7684\u60c5\u51b5\u4e0b\uff0c\u4f8b\u5982\u5728\u4e0b\u9762\u7684switch\u8bed\u53e5\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include enum class Color { black , red , blue , }; constexpr std :: string_view getColor ( Color color ) { using enum Color ; // bring all Color enumerators into current scope (C++20) // We can now access the enumerators of Color without using a Color:: prefix switch ( color ) { case black : return \"black\" ; // note: black instead of Color::black case red : return \"red\" ; case blue : return \"blue\" ; default : return \"???\" ; } } int main () { Color shirt { Color :: blue }; std :: cout << \"Your shirt is \" << getColor ( shirt ) << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c Color \u662f\u4e00\u4e2a\u679a\u4e3e\u7c7b\uff0c\u6240\u4ee5\u6211\u4eec\u901a\u5e38\u9700\u8981\u4f7f\u7528\u5b8c\u5168\u9650\u5b9a\u540d\u6765\u8bbf\u95ee\u5176\u679a\u4e3e\u503c (\u4f8b\u5982 Color::blue )\u3002\u4f46\u662f\uff0c\u5728 getColor() \u51fd\u6570\u4e2d, \u7531\u4e8e\u6211\u4eec\u4f7f\u7528\u4e86 using enum Color; \uff0c\u56e0\u6b64\u53ef\u4ee5\u7701\u7565 Color:: \u524d\u7f00\u76f4\u63a5\u8bbf\u95ee\u679a\u4e3e\u503c\u3002 \u8fd9\u6837\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728switch\u8bed\u53e5\u4e2d\u8282\u7701\u5927\u91cf\u91cd\u590d\u3001\u5197\u4f59\u7684\u8f93\u5165\u3002","title":"10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09"},{"location":"10-4-scoped-enumerations-enum-classes/#_1","text":"\u8fd9\u4e2a\u89e3\u51b3\u65b9\u6848\u5c31\u662f \u6709\u4f5c\u7528\u57df\u679a\u4e3e (\u5728C++\u4e2d\u901a\u5e38\u79f0\u4e3a \u679a\u4e3e\u7c7b \uff0c\u81f3\u4e8e\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u53eb\u5b83\uff0c\u5f88\u5feb\u5c31\u4f1a\u4e0d\u8a00\u81ea\u660e\u4e86)\u3002 \u6709\u4f5c\u7528\u57df\u679a\u4e3e\u548c\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\uff08 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \uff09\u7528\u6cd5\u7c7b\u4f3c\uff0c\u4e0d\u8fc7\u5b83\u4eec\u6709\u4e24\u4e2a\u4e3b\u8981\u7684\u533a\u522b\uff1a\u5176\u4e00\uff0c\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u662f\u5f3a\u7c7b\u578b\u7684(\u5b83\u4e0d\u4f1a\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u6574\u578b) \uff0c\u5176\u4e8c\u5b83\u5177\u6709\u5f3a\u4f5c\u7528\u57df\u7ea6\u675f (\u5b83\u7684\u679a\u4e3e\u503c \u53ea\u4f1a \u88ab\u653e\u7f6e\u5728\u679a\u4e3e\u7c7b\u7684\u4f5c\u7528\u57df\u4e2d)\u3002 \u521b\u5efa\u6709\u4f5c\u7528\u57df\u679a\u4e3e\uff0c\u9700\u8981\u4f7f\u7528\u5173\u952e\u5b57 enum class \u3002\u5b9a\u4e49\u7684\u5176\u4ed6\u90e8\u5206\u5219\u548c\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u4e00\u6837\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include int main () { enum class Color // \"enum class\" \u5b9a\u4e49\u4e86\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b { red , // red \u662f Color \u4f5c\u7528\u57df\u7684\u6210\u5458 blue , }; enum class Fruit { banana , // banana \u662f Fruit \u4f5c\u7528\u57df\u7684\u6210\u5458 apple , }; Color color { Color :: red }; // \u6ce8\u610f: red \u4e0d\u80fd\u591f\u88ab\u76f4\u63a5\u8bbf\u95ee\uff0c\u5fc5\u987b\u4f7f\u7528 Color::red Fruit fruit { Fruit :: banana }; // note: banana \u4e0d\u80fd\u591f\u88ab\u76f4\u63a5\u8bbf\u95ee, \u5fc5\u987b\u4f7f\u7528 Fruit::banana if ( color == fruit ) // \u7f16\u8bd1\u9519\u8bef\uff1a\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053\u5982\u4f55\u6bd4\u8f83 Color \u548c Fruit std :: cout << \"color and fruit are equal \\n \" ; else std :: cout << \"color and fruit are not equal \\n \" ; return 0 ; } \u7f16\u8bd1\u5668\u4f1a\u62a5\u544a\u7a0b\u5e8f\u7684\u7b2c19\u884c\u5b58\u5728\u9519\u8bef\uff0c\u56e0\u4e3a\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u4e0d\u80fd\u591f\u88ab\u8f6c\u6362\u4e3a\u4efb\u4f55\u53ef\u6bd4\u8f83\u7684\u5176\u4ed6\u7c7b\u578b\u3002 \u9898\u5916\u8bdd class \u5173\u952e\u5b57 (\u4ee5\u53ca static \u5173\u952e\u5b57)\u662f C++ \u4e2d\u6700\u88ab\u201c\u6ee5\u7528\u201d\u7684\u5173\u952e\u5b57\uff0c\u5b83\u5728\u4e0d\u540c\u8bed\u5883\u4e0b\u5177\u6709\u4e0d\u540c\u7684\u542b\u4e49\u3002\u5c3d\u7ba1\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u4f7f\u7528\u4e86 class \u5173\u952e\u5b57\uff0c\u4f46\u662f\u5b83\u4eec\u5e76\u4e0d\u662f\u7c7b\u7c7b\u578b\uff08\u7c7b\u7c7b\u578b\u5305\u62ec\u7ed3\u6784\u4f53\u3001\u7c7b\u548c\u8054\u5408\u4f53\uff09\u3002","title":"\u6709\u4f5c\u7528\u57df\u679a\u4e3e"},{"location":"10-4-scoped-enumerations-enum-classes/#_2","text":"\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u4f1a\u5c06\u5176\u679a\u4e3e\u503c\u548c\u81ea\u5df1\u653e\u7f6e\u5728\u76f8\u540c\u7684\u4f5c\u7528\u57df\u4e2d\uff0c\u800c\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u4f1a\u5c06\u5b83\u7684\u679a\u4e3e\u503c\u653e\u7f6e\u5728\u81ea\u5df1\u5b9a\u4e49\u7684\u4f5c\u7528\u57df\u4e2d\u3002\u6362\u8a00\u4e4b\uff0c\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u540c\u65f6\u662f\u5176\u679a\u4e3e\u503c\u7684\u547d\u540d\u7a7a\u95f4\u3002\u8fd9\u79cd\u81ea\u5e26\u7684\u4f5c\u7528\u57df\u6709\u52a9\u4e8e\u907f\u514d\u547d\u540d\u7a7a\u95f4\u6c61\u67d3\u548c\u6f5c\u5728\u7684\u547d\u540d\u51b2\u7a81\u3002 \u8981\u8bbf\u95ee\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u503c\uff0c\u5c31\u50cf\u5b83\u4f4d\u4e8e\u4e0e\u4f5c\u7528\u57df\u679a\u4e3e\u540c\u540d\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\u4e00\u6837: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { enum class Color // \"enum class\" defines this as a scoped enum rather than an unscoped enum { red , // red is considered part of Color's scope region blue , }; std :: cout << red << '\\n' ; // compile error: red not defined in this scope region std :: cout << Color :: red << '\\n' ; // compile error: std::cout doesn't know how to print this (will not implicitly convert to int) Color color { Color :: blue }; // okay return 0 ; } \u56e0\u4e3a\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u4e3a\u679a\u4e3e\u503c\u63d0\u4f9b\u4e86\u5b83\u4eec\u81ea\u5df1\u7684\u9690\u5f0f\u540d\u79f0\u7a7a\u95f4\uff0c\u6240\u4ee5\u6ca1\u6709\u5fc5\u8981\u5c06\u4f5c\u7528\u57df\u679a\u4e3e\u653e\u5728\u53e6\u4e00\u4e2a\u4f5c\u7528\u57df(\u6bd4\u5982\u540d\u79f0\u7a7a\u95f4)\u4e2d\uff0c\u9664\u975e\u6709\u5176\u4ed6\u4ee4\u4eba\u4fe1\u670d\u7684\u7406\u7531\u8fd9\u6837\u505a\uff0c\u56e0\u4e3a\u8fd9\u6837\u505a\u662f\u591a\u4f59\u7684\u3002","title":"\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u4f1a\u5b9a\u4e49\u81ea\u5df1\u7684\u4f5c\u7528\u57df"},{"location":"10-4-scoped-enumerations-enum-classes/#_3","text":"\u548c\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u4e0d\u540c\uff0c\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u4e0d\u4f1a\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u6574\u578b\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u662f\u6709\u76ca\u5730\uff0c\u56e0\u4e3a\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u8fd9\u6837\u7684\u8f6c\u6362\u90fd\u4e0d\u5408\u7406\uff0c\u6240\u4ee5\u8fd9\u6709\u52a9\u4e8e\u5e2e\u6211\u4eec\u907f\u514d\u8bed\u4e49\u9519\u8bef\uff0c\u4f8b\u5982\u6bd4\u8f83\u4e24\u4e2a\u4e0d\u540c\u679a\u4e3e\u7c7b\u578b\u4e2d\u7684\u679a\u4e3e\u503c\u6216\u8005\u4f7f\u7528\u679a\u4e3e\u503c\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\uff08 red + 5 \uff09\u3002 \u6ce8\u610f\uff0c\u4f60\u53ef\u4ee5\u6bd4\u8f83\u6765\u81ea\u76f8\u540c\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u4e2d\u7684\u679a\u4e3e\u503c(\u56e0\u4e3a\u5b83\u4eec\u5177\u6709\u76f8\u540c\u7684\u7c7b\u578b)\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { enum class Color { red , blue , }; Color shirt { Color :: red }; if ( shirt == Color :: red ) // Color \u548c Color \u7684\u6bd4\u8f83\u662f\u53ef\u4ee5\u7684 std :: cout << \"The shirt is red! \\n \" ; else if ( shirt == Color :: blue ) std :: cout << \"The shirt is blue! \\n \" ; return 0 ; } \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5c06\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u503c\u4f5c\u4e3a\u6574\u6570\u5904\u7406\u4e5f\u662f\u6709\u7528\u7684\u3002\u5728\u8fd9\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 static_cast \u663e\u5f0f\u5730\u5c06\u679a\u4e3e\u503c\u8f6c\u6362\u4e3a\u6574\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { enum class Color { red , blue , }; Color color { Color :: blue }; std :: cout << color << '\\n' ; // won't work, because there's no implicit conversion to int std :: cout << static_cast < int > ( color ) << '\\n' ; // will print 1 return 0 ; } \u53cd\u8fc7\u6765\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06\u4e00\u4e2a\u6574\u578b\u8f6c\u6362\u4e3a\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u503c\uff0c\u8fd9\u5728\u5904\u7406\u8f93\u5165\u65f6\u5c24\u5176\u6709\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include enum class Pet { cat , // assigned 0 dog , // assigned 1 pig , // assigned 2 whale , // assigned 3 }; int main () { std :: cout << \"Enter a pet (0=cat, 1=dog, 2=pig, 3=whale): \" ; int input {}; std :: cin >> input ; // input an integer Pet pet { static_cast < Pet > ( input ) }; // static_cast our integer to a Pet return 0 ; } \u5728 C++17 \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u6574\u578b\u6570\uff08\u65e0\u9700\u7c7b\u578b\u8f6c\u6362\uff09\u76f4\u63a5\u521d\u59cb\u5316\u6709\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u800c\u4e14\u4e0d\u50cf\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\uff0c\u4f60\u4e0d\u9700\u8981\u6307\u660e\u4e00\u4e2a\u57fa\u7c7b\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u63a8\u8350\u4f7f\u7528\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u800c\u975e\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\uff0c\u9664\u975e\u4f60\u6709\u8db3\u591f\u7684\u7406\u7531\u4e0d\u8fd9\u4e48\u505a\u3002 \u5c3d\u7ba1\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u63d0\u4f9b\u4e86\u5f88\u591a\u597d\u7684\u529f\u80fd\uff0c\u4f46\u662f\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u5728C++\u4e2d\u4ecd\u7136\u975e\u5e38\u5e38\u89c1\uff0c\u56e0\u4e3a\u5728\u6709\u4e9b\u573a\u5408\u4e0b\u6211\u4eec\u5e0c\u671b\u679a\u4e3e\u503c\u80fd\u591f\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u6574\u578b\uff08\u603b\u662f\u4f7f\u7528 static_cast \u8fdb\u884c\u8f6c\u6362\u6709\u4e9b\u9ebb\u70e6\uff09\uff0c\u800c\u4e14\u6709\u65f6\u6211\u4eec\u4e5f\u4e0d\u9700\u8981\u989d\u5916\u7684\u547d\u540d\u7a7a\u95f4\u5206\u9694\u3002","title":"\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u4e0d\u4f1a\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u6574\u578b"},{"location":"10-4-scoped-enumerations-enum-classes/#_4","text":"\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u5f88\u597d\uff0c\u4f46\u5b83\u4e0d\u80fd\u591f\u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u8fd9\u4e00\u7279\u70b9\u6709\u65f6\u53ef\u80fd\u4f1a\u662f\u4e00\u4e2a\u75db\u70b9\u3002\u5982\u679c\u9700\u8981\u7ecf\u5e38\u5c06\u4f5c\u7528\u57df\u679a\u4e3e\u8f6c\u6362\u4e3a\u6574\u6570(\u4f8b\u5982\uff0c\u5728\u5e0c\u671b\u4f7f\u7528\u4f5c\u7528\u57df\u679a\u4e3e\u4f5c\u4e3a\u6570\u7ec4\u4e0b\u6807\u7684\u60c5\u51b5\u4e0b)\uff0c\u90a3\u4e48\u6bcf\u6b21\u9700\u8981\u8f6c\u6362\u65f6\u90fd\u5fc5\u987b\u4f7f\u7528 static_cast \uff0c\u8fd9\u65e0\u7591\u4f1a\u4f7f\u4ee3\u7801\u770b\u8d77\u6765\u975e\u5e38\u6df7\u4e71 \u5982\u679c\u4f60\u6709\u8fd9\u6837\u7684\u9700\u6c42\uff0c\u5e0c\u671b\u4f7f\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u503c\u80fd\u591f\u66f4\u5bb9\u6613\u5730\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u90a3\u4e48\u53ef\u4ee5\u5c1d\u8bd5\u91cd\u8f7d\u4e00\u5143 operator+ \u6765\u6267\u884c\u8fd9\u79cd\u8f6c\u6362\u3002\u6211\u4eec\u8fd8\u6ca1\u6709\u89e3\u91ca\u8fd9\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u6240\u4ee5\u73b0\u5728\u5c31\u628a\u5b83\u5f53\u6210\u9b54\u672f\u5427\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include enum class Animals { chicken , // 0 dog , // 1 cat , // 2 elephant , // 3 duck , // 4 snake , // 5 maxAnimals , }; // Overload the unary + operator to convert Animals to the underlying type // adapted from https://stackoverflow.com/a/42198760, thanks to Pixelchemist for the idea constexpr auto operator + ( Animals a ) noexcept { return static_cast < std :: underlying_type_t < Animals >> ( a ); } int main () { std :: cout << + Animals :: elephant << '\\n' ; // convert Animals::elephant to an integer using unary operator+ return 0 ; } \u6253\u5370\uff1a 1 3 \u6b64\u65b9\u6cd5\u53ef\u907f\u514d\u4e0d\u7ecf\u610f\u5730\u9690\u5f0f\u8f6c\u6362\uff0c\u540c\u65f6\u8fd8\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u5730\u663e\u793a\u8f6c\u6362\u65b9\u6cd5\u3002","title":"\u7b80\u5316\u6709\u4f5c\u7528\u57df\u679a\u4e3e\u503c\u5230\u6574\u578b\u7684\u8f6c\u6362(\u8fdb\u9636\u8bdd\u9898)"},{"location":"10-4-scoped-enumerations-enum-classes/#using-enum-c20","text":"C++20 \u5f15\u5165\u4e86 using enum \u8bed\u53e5\uff0c\u5b83\u53ef\u4ee5\u5c06\u6bcf\u4e2a\u679a\u4e3e\u4f5c\u7528\u57df\u4e2d\u7684\u679a\u4e3e\u503c\u5168\u90e8\u5bfc\u5165\u5230\u5f53\u524d\u4f5c\u7528\u57df\u3002\u5f53\u914d\u5408 \u679a\u4e3e\u7c7b \u4f7f\u7528\u65f6\uff0c\u53ef\u4ee5\u907f\u514d\u4f7f\u7528\u679a\u4e3e\u7c7b\u524d\u7f00\u8bbf\u95ee\u679a\u4e3e\u503c\u3002 \u8fd9\u4e2a\u7279\u6027\u975e\u5e38\u6709\u7528\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u91cd\u590d\u8f93\u5165\u5927\u91cf\u76f8\u540c\u524d\u7f00\u7684\u60c5\u51b5\u4e0b\uff0c\u4f8b\u5982\u5728\u4e0b\u9762\u7684switch\u8bed\u53e5\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include enum class Color { black , red , blue , }; constexpr std :: string_view getColor ( Color color ) { using enum Color ; // bring all Color enumerators into current scope (C++20) // We can now access the enumerators of Color without using a Color:: prefix switch ( color ) { case black : return \"black\" ; // note: black instead of Color::black case red : return \"red\" ; case blue : return \"blue\" ; default : return \"???\" ; } } int main () { Color shirt { Color :: blue }; std :: cout << \"Your shirt is \" << getColor ( shirt ) << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c Color \u662f\u4e00\u4e2a\u679a\u4e3e\u7c7b\uff0c\u6240\u4ee5\u6211\u4eec\u901a\u5e38\u9700\u8981\u4f7f\u7528\u5b8c\u5168\u9650\u5b9a\u540d\u6765\u8bbf\u95ee\u5176\u679a\u4e3e\u503c (\u4f8b\u5982 Color::blue )\u3002\u4f46\u662f\uff0c\u5728 getColor() \u51fd\u6570\u4e2d, \u7531\u4e8e\u6211\u4eec\u4f7f\u7528\u4e86 using enum Color; \uff0c\u56e0\u6b64\u53ef\u4ee5\u7701\u7565 Color:: \u524d\u7f00\u76f4\u63a5\u8bbf\u95ee\u679a\u4e3e\u503c\u3002 \u8fd9\u6837\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728switch\u8bed\u53e5\u4e2d\u8282\u7701\u5927\u91cf\u91cd\u590d\u3001\u5197\u4f59\u7684\u8f93\u5165\u3002","title":"using enum\u00a0\u8bed\u53e5\u00a0\uff08C++20\uff09"},{"location":"10-5-Introduction-to-structs-members-and-member-selection/","tags":["struct","members","member selection"],"text":"Key Takeaway \u5728\u7f16\u7a0b\u4e2d\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u4f7f\u7528\u591a\u4e2a\u53d8\u91cf\u6765\u8868\u793a\u66f4\u590d\u6742\u7684\u5bf9\u8c61\uff0c\u6b63\u5982\u5728\u4e4b\u524d\u7ae0\u8282\u4e2d( 9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b )\u4ecb\u7ecd\u7684\u90a3\u6837\uff0c\u5206\u6570\u662f\u7531\u4e24\u4e2a\u5177\u6709\u5173\u8054\u7684\u6570\u5b66\u5bf9\u8c61\u2014\u2014\u5206\u5b50\u5206\u6bcd\u7ec4\u6210\u7684\u3002 \u6216\u8005\uff0c\u5047\u8bbe\u6211\u4eec\u60f3\u8981\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5176\u4e2d\u9700\u8981\u5b58\u50a8\u5173\u4e8e\u516c\u53f8\u5458\u5de5\u7684\u4fe1\u606f\u3002\u4e3a\u6b64\u6211\u4eec\u9700\u8981\u8ffd\u8e2a\u8bf8\u5982\u5458\u5de5\u59d3\u540d\u3001\u5934\u8854\u3001\u5e74\u9f84\u3001\u5458\u5de5id\u3001\u7ecf\u7406id\u3001\u5de5\u8d44\u3001\u751f\u65e5\u3001\u96c7\u4f63\u65e5\u671f\u7b49\u5c5e\u6027\u3002 1 2 3 4 5 6 7 8 9 10 11 12 std :: string name ; std :: string title ; int age ; int id ; int managerId ; double wage ; int birthdayYear ; int birthdayMonth ; int birthdayDay ; int hireYear ; int hireMonth ; int hireDay ; \u7136\u800c\uff0c\u8fd9\u79cd\u65b9\u5f0f\u5b58\u5728\u8bb8\u591a\u95ee\u9898\u3002\u9996\u5148\uff0c\u6211\u4eec\u4e0d\u6e05\u695a\u8fd9\u4e9b\u53d8\u91cf\u662f\u5426\u771f\u7684\u76f8\u5173(\u4f60\u5fc5\u987b\u9605\u8bfb\u6ce8\u91ca\uff0c\u6216\u8005\u67e5\u770b\u5b83\u4eec\u5728\u4e0a\u4e0b\u6587\u4e2d\u662f\u5982\u4f55\u4f7f\u7528\u7684)\u3002\u5176\u6b21\uff0c\u73b0\u5728\u670912\u4e2a\u53d8\u91cf\u9700\u8981\u7ba1\u7406\u3002\u5982\u679c\u6211\u4eec\u60f3\u8981\u5c06\u8fd9\u4e2a\u96c7\u5458\u4f20\u9012\u7ed9\u4e00\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u5fc5\u987b\u4f20\u901212\u4e2a\u53c2\u6570(\u5e76\u4e14\u987a\u5e8f\u6b63\u786e)\uff0c\u8fd9\u5c06\u4f7f\u6211\u4eec\u7684\u51fd\u6570\u539f\u578b\u548c\u51fd\u6570\u8c03\u7528\u53d8\u5f97\u6df7\u4e71\u3002\u65e2\u7136\u51fd\u6570\u53ea\u80fd\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u90a3\u4e48\u51fd\u6570\u5982\u4f55\u8fd4\u56de\u4e00\u4e2a\u96c7\u5458\u5462? \u5982\u679c\u6211\u4eec\u60f3\u8981\u4e00\u4e2a\u4ee5\u4e0a\u7684\u5458\u5de5\uff0c\u6211\u4eec\u9700\u8981\u4e3a\u6bcf\u4e2a\u989d\u5916\u7684\u5458\u5de5\u5b9a\u4e4912\u4e2a\u4ee5\u4e0a\u7684\u53d8\u91cf(\u6bcf\u4e2a\u53d8\u91cf\u90fd\u9700\u8981\u4e00\u4e2a\u60df\u4e00\u7684\u540d\u79f0)\uff01\u8fd9\u663e\u7136\u662f\u65e0\u6cd5\u6269\u5c55\u7684\u3002\u6211\u4eec\u771f\u6b63\u9700\u8981\u7684\u662f\u67d0\u79cd\u65b9\u6cd5\u6765\u7ec4\u7ec7\u6240\u6709\u8fd9\u4e9b\u76f8\u5173\u7684\u6570\u636e\u5757\uff0c\u4f7f\u5b83\u4eec\u66f4\u5bb9\u6613\u7ba1\u7406\u3002 \u5e78\u8fd0\u7684\u662f\uff0cC++ \u63d0\u4f9b\u4e86\u4e24\u4e2a\u7b26\u5408\u7c7b\u578b\u7528\u4e8e\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\uff1a\u7ed3\u6784\u4f53\u548c\u7c7b\u3002\u7ed3\u6784\u4f53\u662f\u4e00\u79cd\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b( 10.1 - \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7b80\u4ecb ) \uff0c\u4f7f\u7528\u7ed3\u6784\u4f53\u6211\u4eec\u53ef\u4ee5\u628a\u591a\u4e2a\u53d8\u91cf\u805a\u5408\u6210\u4e00\u4e2a\u5355\u72ec\u7684\u7c7b\u578b\u3002\u7a0d\u540e\u4f60\u5c06\u4f1a\u770b\u5230\uff0c\u8fd9\u4f7f\u5f97\u7ba1\u7406\u591a\u4e2a\u76f8\u5173\u8054\u53d8\u91cf\u53d8\u5f97\u975e\u5e38\u7b80\u5355\uff01 \u5b9a\u4e49\u7ed3\u6784\u4f53 \u56e0\u4e3a\u7ed3\u6784\u662f\u7a0b\u5e8f\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u6240\u4ee5\u5728\u5f00\u59cb\u4f7f\u7528\u7ed3\u6784\u7c7b\u578b\u4e4b\u524d\uff0c\u6211\u4eec\u5fc5\u987b\u5148\u544a\u8bc9\u7f16\u8bd1\u5668\u7ed3\u6784\u7c7b\u578b\u662f\u4ec0\u4e48\u6837\u5b50\u7684\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5316\u5458\u5de5\u7684\u7ed3\u6784\u5b9a\u4e49\u793a\u4f8b\uff1a 1 2 3 4 5 6 struct Employee { int id {}; int age {}; double wage {}; }; struct \u5173\u952e\u5b57\u7528\u4e8e\u544a\u8bc9\u7f16\u8bd1\u5668\u6b64\u5904\u8981\u5b9a\u4e49\u4e00\u4e2a\u7ed3\u6784\u4f53\u2014\u2014\u540d\u4e3a Employee \uff08\u56e0\u4e3a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7684\u547d\u540d\u901a\u5e38\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff09\u3002 \u968f\u540e\uff0c\u5728\u5927\u62ec\u53f7\u4e2d\uff0c\u5b9a\u4e49\u4e86 Employee \u6240\u9700\u8981\u7684\u5404\u4e2a\u53d8\u91cf\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6bcf\u4e2a Employee \u521b\u5efa\u4e863\u4e2a\u53d8\u91cf\uff1a int id \u3001 int age \u548c double wage \u3002\u8fd9\u4e9b\u53d8\u91cf\u662f\u7ed3\u6784\u4f53\u7684\u4e00\u90e8\u5206\uff0c\u79f0\u4e3a\u6570\u636e\u6210\u5458\uff08\u6216\u8005 \u6210\u5458\u53d8\u91cf \uff09\u3002 \u5c0f\u8d34\u58eb \u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f1a\u7ecf\u5e38\u7528\u5230\u201c\u6210\u5458\u201d\u8fd9\u4e2a\u8bcd\uff0c\u6240\u4ee5\u4e00\u5b9a\u8981\u8bb0\u4f4f\u5b83\u7684\u610f\u601d\u3002 \u6b63\u5982\u53ef\u4ee5\u4f7f\u7528\u7a7a\u7684\u5927\u62ec\u53f7\u5bf9\u4e00\u822c\u53d8\u91cf\u8fdb\u884c \u521d\u59cb\u5316 \u4e00\u6837 ( 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 ) \uff0c\u7a7a\u7684\u5927\u62ec\u53f7\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5728\u521b\u5efa Employee \u65f6\u5bf9\u5176\u6210\u5458\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 ( 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 )\u3002 \u6700\u540e\uff0c\u5728\u5b9a\u4e49\u7ed3\u675f\u5904\u6dfb\u52a0\u5206\u53f7\u3002 \u63d0\u9192\u4e00\u4e0b\uff0c\u8fd9\u91cc\u7684 Employee \u53ea\u662f\u4e00\u4e2a\u7c7b\u578b\u5b9a\u4e49\u2014\u2014\u5e76\u6ca1\u6709\u5b9e\u9645\u521b\u5efa\u4efb\u4f55\u5bf9\u8c61\u3002 \u5b9a\u4e49\u7ed3\u6784\u4f53\u5bf9\u8c61 \u4e3a\u4e86\u4f7f\u7528 Employee \u7c7b\u578b\uff0c\u5b9a\u4e49\u4e00\u4e2a Employee \u7c7b\u578b\u7684\u53d8\u91cf\u5373\u53ef\uff1a 1 Employee joe ; // Employee is the type, joe is the variable name \u6b64\u5904\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a joe \u7684 Employee \u7c7b\u578b\u53d8\u91cf\u3002\u5b9a\u4e49 joe \u65f6\uff0c\u5c06\u521b\u5efa\u4e00\u4e2a Employee \u5bf9\u8c61\uff0c\u5e76\u6309\u987a\u5e8f\u521b\u5efa\u5176\u4e2d\u76843\u4e2a\u6570\u636e\u6210\u5458(\u7136\u540e\u521d\u59cb\u5316\u503c)\u3002 \u5c31\u50cf\u4efb\u4f55\u5176\u4ed6\u7c7b\u578b\u4e00\u6837\uff0c\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2a\u6b64\u7c7b\u578b\u53d8\u91cf: 1 2 Employee joe ; // create an Employee struct for Joe Employee frank ; // create an Employee struct for Frank \u8bbf\u95ee\u6210\u5458 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c joe \u6307\u7684\u662f\u6574\u4e2a\u7ed3\u6784\u4f53\u5bf9\u8c61(\u5176\u4e2d\u5305\u542b\u6210\u5458\u53d8\u91cf)\u3002\u8981\u8bbf\u95ee\u7279\u5b9a\u7684\u6210\u5458\u53d8\u91cf\uff0c\u9700\u8981\u5728\u7ed3\u6784\u53d8\u91cf\u540d\u548c\u6210\u5458\u540d\u4e4b\u95f4\u4f7f\u7528 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 \u3002\u4f8b\u5982\uff0c\u8981\u8bbf\u95ee joe \u7684\u5e74\u9f84\u6210\u5458\uff0c\u9700\u8981\u5199\u4f5c joe.age \u3002 \u7ed3\u6784\u4f53\u6210\u5458\u53d8\u91cf\u5c31\u50cf\u666e\u901a\u53d8\u91cf\u4e00\u6837\u5de5\u4f5c\uff0c\u56e0\u6b64\u53ef\u4ee5\u5bf9\u5b83\u4eec\u8fdb\u884c\u4e00\u822c\u7684\u64cd\u4f5c\uff0c\u5305\u62ec\u8d4b\u503c\u3001\u8fd0\u7b97\u3001\u6bd4\u8f83\u7b49\u2026\u2026 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe ; joe . age = 32 ; // use member selection operator (.) to select the age member of variable joe std :: cout << joe . age << '\\n' ; // print joe's age return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 32 \u4f7f\u7528\u7ed3\u6784\u4f53\u7684\u6700\u5927\u6709\u65f6\u5728\u4e8e\uff0c\u4f60\u53ea\u9700\u8981\u4e3a\u6bcf\u4e2a\u7ed3\u6784\u4f53\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf(\u6210\u5458\u540d\u662f\u7ed3\u6784\u4f53\u5b9a\u4e49\u4e2d\u56fa\u5b9a\u7684\u90e8\u5206)\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9e\u4f8b\u5316\u4e86\u4e24\u4e2a Employee \u5bf9\u8c61\uff1a joe \u548c frank \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe ; joe . id = 14 ; joe . age = 32 ; joe . wage = 60000.0 ; Employee frank ; frank . id = 15 ; frank . age = 28 ; frank . wage = 45000.0 ; int totalAge { joe . age + frank . age }; if ( joe . wage > frank . wage ) std :: cout << \"Joe makes more than Frank \\n \" ; else if ( joe . wage < frank . wage ) std :: cout << \"Joe makes less than Frank \\n \" ; else std :: cout << \"Joe and Frank make the same amount \\n \" ; // Frank got a promotion frank . wage += 5000.0 ; // Today is Joe's birthday ++ joe . age ; // use pre-increment to increment Joe's age by 1 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f88\u5bb9\u6613\u5224\u65ad\u54ea\u4e9b\u6210\u5458\u53d8\u91cf\u5c5e\u4e8e Joe \uff0c\u54ea\u4e9b\u5c5e\u4e8e Frank \u3002\u8fd9\u63d0\u4f9b\u4e86\u6bd4\u5355\u4e2a\u53d8\u91cf\u66f4\u9ad8\u5c42\u6b21\u7684\u7ec4\u7ec7\u65b9\u6cd5\u3002\u6b64\u5916\uff0c\u7531\u4e8e Joe \u7684\u6210\u5458\u548c Frank \u7684\u6210\u5458\u5177\u6709\u76f8\u540c\u7684\u540d\u79f0\uff0c\u5f53\u4f60\u6709\u591a\u4e2a\u76f8\u540c\u7ed3\u6784\u7c7b\u578b\u7684\u53d8\u91cf\u65f6\uff0c\u8fd9\u5c06\u63d0\u4f9b\u4e00\u81f4\u6027\u3002 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u7ee7\u7eed\u63a2\u7d22\u7ed3\u6784\u4f53\uff0c\u4ee5\u53ca\u5982\u4f55\u521d\u59cb\u5316\u5b83\u4eec\u3002","title":"10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9"},{"location":"10-5-Introduction-to-structs-members-and-member-selection/#_1","text":"\u56e0\u4e3a\u7ed3\u6784\u662f\u7a0b\u5e8f\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u6240\u4ee5\u5728\u5f00\u59cb\u4f7f\u7528\u7ed3\u6784\u7c7b\u578b\u4e4b\u524d\uff0c\u6211\u4eec\u5fc5\u987b\u5148\u544a\u8bc9\u7f16\u8bd1\u5668\u7ed3\u6784\u7c7b\u578b\u662f\u4ec0\u4e48\u6837\u5b50\u7684\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5316\u5458\u5de5\u7684\u7ed3\u6784\u5b9a\u4e49\u793a\u4f8b\uff1a 1 2 3 4 5 6 struct Employee { int id {}; int age {}; double wage {}; }; struct \u5173\u952e\u5b57\u7528\u4e8e\u544a\u8bc9\u7f16\u8bd1\u5668\u6b64\u5904\u8981\u5b9a\u4e49\u4e00\u4e2a\u7ed3\u6784\u4f53\u2014\u2014\u540d\u4e3a Employee \uff08\u56e0\u4e3a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7684\u547d\u540d\u901a\u5e38\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff09\u3002 \u968f\u540e\uff0c\u5728\u5927\u62ec\u53f7\u4e2d\uff0c\u5b9a\u4e49\u4e86 Employee \u6240\u9700\u8981\u7684\u5404\u4e2a\u53d8\u91cf\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6bcf\u4e2a Employee \u521b\u5efa\u4e863\u4e2a\u53d8\u91cf\uff1a int id \u3001 int age \u548c double wage \u3002\u8fd9\u4e9b\u53d8\u91cf\u662f\u7ed3\u6784\u4f53\u7684\u4e00\u90e8\u5206\uff0c\u79f0\u4e3a\u6570\u636e\u6210\u5458\uff08\u6216\u8005 \u6210\u5458\u53d8\u91cf \uff09\u3002 \u5c0f\u8d34\u58eb \u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f1a\u7ecf\u5e38\u7528\u5230\u201c\u6210\u5458\u201d\u8fd9\u4e2a\u8bcd\uff0c\u6240\u4ee5\u4e00\u5b9a\u8981\u8bb0\u4f4f\u5b83\u7684\u610f\u601d\u3002 \u6b63\u5982\u53ef\u4ee5\u4f7f\u7528\u7a7a\u7684\u5927\u62ec\u53f7\u5bf9\u4e00\u822c\u53d8\u91cf\u8fdb\u884c \u521d\u59cb\u5316 \u4e00\u6837 ( 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 ) \uff0c\u7a7a\u7684\u5927\u62ec\u53f7\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5728\u521b\u5efa Employee \u65f6\u5bf9\u5176\u6210\u5458\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 ( 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 )\u3002 \u6700\u540e\uff0c\u5728\u5b9a\u4e49\u7ed3\u675f\u5904\u6dfb\u52a0\u5206\u53f7\u3002 \u63d0\u9192\u4e00\u4e0b\uff0c\u8fd9\u91cc\u7684 Employee \u53ea\u662f\u4e00\u4e2a\u7c7b\u578b\u5b9a\u4e49\u2014\u2014\u5e76\u6ca1\u6709\u5b9e\u9645\u521b\u5efa\u4efb\u4f55\u5bf9\u8c61\u3002","title":"\u5b9a\u4e49\u7ed3\u6784\u4f53"},{"location":"10-5-Introduction-to-structs-members-and-member-selection/#_2","text":"\u4e3a\u4e86\u4f7f\u7528 Employee \u7c7b\u578b\uff0c\u5b9a\u4e49\u4e00\u4e2a Employee \u7c7b\u578b\u7684\u53d8\u91cf\u5373\u53ef\uff1a 1 Employee joe ; // Employee is the type, joe is the variable name \u6b64\u5904\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a joe \u7684 Employee \u7c7b\u578b\u53d8\u91cf\u3002\u5b9a\u4e49 joe \u65f6\uff0c\u5c06\u521b\u5efa\u4e00\u4e2a Employee \u5bf9\u8c61\uff0c\u5e76\u6309\u987a\u5e8f\u521b\u5efa\u5176\u4e2d\u76843\u4e2a\u6570\u636e\u6210\u5458(\u7136\u540e\u521d\u59cb\u5316\u503c)\u3002 \u5c31\u50cf\u4efb\u4f55\u5176\u4ed6\u7c7b\u578b\u4e00\u6837\uff0c\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2a\u6b64\u7c7b\u578b\u53d8\u91cf: 1 2 Employee joe ; // create an Employee struct for Joe Employee frank ; // create an Employee struct for Frank","title":"\u5b9a\u4e49\u7ed3\u6784\u4f53\u5bf9\u8c61"},{"location":"10-5-Introduction-to-structs-members-and-member-selection/#_3","text":"\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c joe \u6307\u7684\u662f\u6574\u4e2a\u7ed3\u6784\u4f53\u5bf9\u8c61(\u5176\u4e2d\u5305\u542b\u6210\u5458\u53d8\u91cf)\u3002\u8981\u8bbf\u95ee\u7279\u5b9a\u7684\u6210\u5458\u53d8\u91cf\uff0c\u9700\u8981\u5728\u7ed3\u6784\u53d8\u91cf\u540d\u548c\u6210\u5458\u540d\u4e4b\u95f4\u4f7f\u7528 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 \u3002\u4f8b\u5982\uff0c\u8981\u8bbf\u95ee joe \u7684\u5e74\u9f84\u6210\u5458\uff0c\u9700\u8981\u5199\u4f5c joe.age \u3002 \u7ed3\u6784\u4f53\u6210\u5458\u53d8\u91cf\u5c31\u50cf\u666e\u901a\u53d8\u91cf\u4e00\u6837\u5de5\u4f5c\uff0c\u56e0\u6b64\u53ef\u4ee5\u5bf9\u5b83\u4eec\u8fdb\u884c\u4e00\u822c\u7684\u64cd\u4f5c\uff0c\u5305\u62ec\u8d4b\u503c\u3001\u8fd0\u7b97\u3001\u6bd4\u8f83\u7b49\u2026\u2026 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe ; joe . age = 32 ; // use member selection operator (.) to select the age member of variable joe std :: cout << joe . age << '\\n' ; // print joe's age return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 32 \u4f7f\u7528\u7ed3\u6784\u4f53\u7684\u6700\u5927\u6709\u65f6\u5728\u4e8e\uff0c\u4f60\u53ea\u9700\u8981\u4e3a\u6bcf\u4e2a\u7ed3\u6784\u4f53\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf(\u6210\u5458\u540d\u662f\u7ed3\u6784\u4f53\u5b9a\u4e49\u4e2d\u56fa\u5b9a\u7684\u90e8\u5206)\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9e\u4f8b\u5316\u4e86\u4e24\u4e2a Employee \u5bf9\u8c61\uff1a joe \u548c frank \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe ; joe . id = 14 ; joe . age = 32 ; joe . wage = 60000.0 ; Employee frank ; frank . id = 15 ; frank . age = 28 ; frank . wage = 45000.0 ; int totalAge { joe . age + frank . age }; if ( joe . wage > frank . wage ) std :: cout << \"Joe makes more than Frank \\n \" ; else if ( joe . wage < frank . wage ) std :: cout << \"Joe makes less than Frank \\n \" ; else std :: cout << \"Joe and Frank make the same amount \\n \" ; // Frank got a promotion frank . wage += 5000.0 ; // Today is Joe's birthday ++ joe . age ; // use pre-increment to increment Joe's age by 1 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f88\u5bb9\u6613\u5224\u65ad\u54ea\u4e9b\u6210\u5458\u53d8\u91cf\u5c5e\u4e8e Joe \uff0c\u54ea\u4e9b\u5c5e\u4e8e Frank \u3002\u8fd9\u63d0\u4f9b\u4e86\u6bd4\u5355\u4e2a\u53d8\u91cf\u66f4\u9ad8\u5c42\u6b21\u7684\u7ec4\u7ec7\u65b9\u6cd5\u3002\u6b64\u5916\uff0c\u7531\u4e8e Joe \u7684\u6210\u5458\u548c Frank \u7684\u6210\u5458\u5177\u6709\u76f8\u540c\u7684\u540d\u79f0\uff0c\u5f53\u4f60\u6709\u591a\u4e2a\u76f8\u540c\u7ed3\u6784\u7c7b\u578b\u7684\u53d8\u91cf\u65f6\uff0c\u8fd9\u5c06\u63d0\u4f9b\u4e00\u81f4\u6027\u3002 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u7ee7\u7eed\u63a2\u7d22\u7ed3\u6784\u4f53\uff0c\u4ee5\u53ca\u5982\u4f55\u521d\u59cb\u5316\u5b83\u4eec\u3002","title":"\u8bbf\u95ee\u6210\u5458"},{"location":"10-6-struct-aggregate-initialization/","tags":["struct","aggregate initialization","C++20"],"text":"Key Takeaway \u6570\u636e\u6210\u5458\u9ed8\u8ba4\u4e0d\u4f1a\u88ab\u521d\u59cb\u5316\uff0c\u9664\u975e\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\u3002 \u5982\u679c\u805a\u5408\u7c7b\u578b\u88ab\u521d\u59cb\u5316\u4e86\uff0c\u90a3\u4e48\u5373\u4fbf\u6ca1\u6709\u5bf9\u6bcf\u4e2a\u6210\u5458\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u5219\u8be5\u6210\u5458\u4f1a\u4f7f\u7528\u81ea\u5df1\u7684\u521d\u59cb\u5316\u503c\uff08\u53ef\u4ee5\u662f0\uff0c\u4e5f\u53ef\u4ee5\u662f\u5176\u4ed6\u6307\u5b9a\u503c\uff0c\u7531\u5176\u81ea\u8eab\u7684\u62ec\u53f7\u521d\u59cb\u5316\u51b3\u5b9a\uff09 \u5217\u8868\u521d\u59cb\u5316\u805a\u5408\u7c7b\u578b\u65f6\uff0c\u6309\u7167 \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \u8fdb\u884c\uff0c\u6b64\u65f6\u5982\u679c\u63d2\u5165\u65b0\u58f0\u660e\u7684\u6210\u5458\u5230\u524d\u9762\uff0c\u5219\u5bf9\u5e94\u7684\u5217\u8868\u5fc5\u987b\u79fb\u52a8\uff0c\u800c\u7f16\u8bd1\u5668\u5e76\u4e0d\u4f1a\u68c0\u6d4b\u5230\u8fd9\u7c7b\u95ee\u9898\u3002 \u5728\u5411\u805a\u5408\u4e2d\u6dfb\u52a0\u65b0\u6210\u5458\u65f6\uff0c\u6700\u5b89\u5168\u7684\u505a\u6cd5\u662f\u5c06\u5176\u6dfb\u52a0\u5230\u5b9a\u4e49\u5217\u8868\u7684\u5e95\u90e8\uff0c\u8fd9\u6837\u5176\u4ed6\u6210\u5458\u7684\u521d\u59cb\u5316\u5f0f\u5c31\u4e0d\u9700\u8981\u8c03\u6574\u987a\u5e8f\u3002 \u5728\u4e0a\u8282\u8bfe\u4e2d( 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 )\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u5982\u4f55\u5b9a\u4e49\u3001\u521d\u59cb\u5316\u4e00\u4e2a\u7ed3\u6784\u4f53\u4ee5\u53ca\u5982\u4f55\u8bbf\u95ee\u5176\u6210\u5458\u3002\u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5982\u4f55\u521d\u59cb\u5316\u4e00\u4e2a\u7ed3\u6784\u4f53\u3002 \u6570\u636e\u6210\u5458\u9ed8\u8ba4\u4e0d\u4f1a\u88ab\u521d\u59cb\u5316 \u548c\u666e\u901a\u53d8\u91cf\u7c7b\u4f3c\uff0c\u6570\u636e\u6210\u5458\u5e76\u4e0d\u4f1a\u88ab\u9ed8\u8ba4\u521d\u59cb\u5316\u3002\u8003\u8651\u4e0b\u9762\u7684\u7ed3\u6784\u4f53\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include struct Employee { int id ; // note: no initializer here int age ; double wage ; }; int main () { Employee joe ; // note: no initializer here either std :: cout << joe . id << '\\n' ; return 0 ; } \u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u63d0\u4f9b\u4efb\u4f55\u521d\u59cb\u5316\u503c\uff0c\u6240\u4ee5\u5f53\u5b9e\u4f8b\u5316 joe \u65f6\uff0c joe.id \u3001 joe.age \u548c joe.wage \u5904\u4e8e\u672a\u521d\u59cb\u5316\u72b6\u6001\u3002\u5f53\u6211\u4eec\u8bd5\u56fe\u6253\u5370 joe.id \u7684\u503c\u65f6\uff0c\u5c06\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4f46\u662f\uff0c\u5728\u5411\u4f60\u5c55\u793a\u5982\u4f55\u521d\u59cb\u5316\u7ed3\u6784\u4e4b\u524d\uff0c\u8ba9\u6211\u4eec\u5148\u7ed5\u8def\u8d70\u4e00\u5c0f\u6bb5\u8def\u3002 \u4ec0\u4e48\u662f\u805a\u5408\uff1f \u5728\u7f16\u7a0b\u9886\u57df\uff0c \u805a\u5408\u6570\u636e\u7c7b\u578b \u6307\u7684\u662f==\u4efb\u4f55\u5305\u542b\u591a\u4e2a\u6570\u636e\u6210\u5458\u7684\u7c7b\u578b==\u3002\u67d0\u4e9b\u7c7b\u578b\u7684\u805a\u5408\u7c7b\u578b\u5141\u8bb8\u6210\u5458\u5177\u6709\u4e0d\u540c\u7684\u7c7b\u578b(\u4f8b\u5982\u7ed3\u6784)\uff0c\u800c\u5176\u4ed6\u7c7b\u578b\u7684\u805a\u5408\u8981\u6c42\u6240\u6709\u6210\u5458\u5fc5\u987b\u5177\u6709\u5355\u4e00\u7c7b\u578b(\u4f8b\u5982\u6570\u7ec4)\u3002 \u5728C++\u4e2d\uff0c\u805a\u5408\u6570\u636e\u7c7b\u578b\u7684\u5b9a\u4e49\u66f4\u5c40\u9650\u4e5f\u66f4\u590d\u6742\u3002 \u6269\u5c55\u9605\u8bfb C++ \u4e2d\u7684\u805a\u5408\u6570\u636e\u7c7b\u578b\u5fc5\u987b\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\uff1a \u662f\u4e00\u4e2a \u7c7b\u7c7b\u578b (\u5305\u62ec struct, class \u6216 union) \u6216\u6570\u7ec4 (\u5305\u62ec\u5185\u7f6e\u6570\u7ec4\u6216 std::array )\uff1b \u6ca1\u6709\u79c1\u6709\u6216\u53d7\u4fdd\u62a4\u7684\u975e\u9759\u6001\u6570\u636e\u6210\u5458\uff1b \u6ca1\u6709\u7528\u6237\u58f0\u660e\u7684\u6216\u7ee7\u627f\u7684 \u6784\u9020\u51fd\u6570 \uff1b \u6ca1\u6709 \u57fa\u7c7b \uff1b \u6ca1\u6709 \u865a\u51fd\u6570 \uff1b \u5148\u628aC++\u805a\u5408\u7684\u7cbe\u786e\u5b9a\u4e49\u653e\u5728\u4e00\u8fb9\uff0c\u5728\u8fd9\u4e00\u70b9\u4e0a\u9700\u8981\u7406\u89e3\u7684\u91cd\u8981\u4e8b\u60c5\u662f\uff0c \u53ea\u6709\u6570\u636e\u6210\u5458\u7684\u7ed3\u6784(\u8fd9\u662f\u6211\u4eec\u5728\u8fd9\u4e9b\u8bfe\u7a0b\u4e2d\u521b\u5efa\u7684\u552f\u4e00\u7c7b\u578b\u7684\u7ed3\u6784)\u662f\u805a\u5408\u6570\u636e\u7c7b\u578b\u3002\u6570\u7ec4(\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u7ae0\u8ba8\u8bba)\u4e5f\u662f\u805a\u5408\u3002 \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 \u56e0\u4e3a\u666e\u901a\u53d8\u91cf\u53ea\u80fd\u4fdd\u5b58\u4e00\u4e2a\u503c\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u9700\u8981\u63d0\u4f9b\u4e00\u4e2a\u521d\u59cb\u5316\u503c\uff1a 1 int x { 5 }; \u4f46\u662f\uff0c\u7ed3\u6784\u4f53\u4e2d\u6709\u591a\u4e2a\u6210\u5458\uff1a 1 2 3 4 5 6 struct Employee { int id {}; int age {}; double wage {}; }; \u5f53\u5b9a\u4e49\u4e00\u4e2a\u5177\u6709\u7ed3\u6784\u7c7b\u578b\u7684\u5bf9\u8c61\u65f6\uff0c\u6211\u4eec\u80fd\u591f\u5728\u521d\u59cb\u5316\u7ed3\u6784\u4f53\u65f6\u540c\u65f6\u521d\u59cb\u5316\u591a\u4e2a\u6210\u5458\u7684\u65b9\u6cd5\uff1a 1 Employee joe ; // \u5982\u4f55\u521d\u59cb\u5316 joe.id, joe.age \u548c joe.wage? \u805a\u5408\u6570\u636e\u7c7b\u578b\u4f7f\u7528\u4e00\u79cd\u79f0\u4e3a \u805a\u5408\u521d\u59cb\u5316 \u7684\u521d\u59cb\u5316\u5f62\u5f0f\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u76f4\u63a5\u521d\u59cb\u5316\u805a\u5408\u7684\u6210\u5458\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u63d0\u4f9b\u4e00\u4e2a \u521d\u59cb\u5316\u503c\u5217\u8868 \u4f5c\u4e3a\u521d\u59cb\u5316\u503c\u3002\u521d\u59cb\u5316\u503c\u5217\u8868\u5c31\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u521d\u59cb\u5316\u503c\uff0c\u4ee5\u9017\u53f7\u5206\u5272\u7684\u503c\u5217\u8868\u3002 \u5c31\u50cf\u666e\u901a\u53d8\u91cf\u53ef\u4ee5\u88ab \u62f7\u8d1d\u521d\u59cb\u5316 \u3001 \u76f4\u63a5\u521d\u59cb\u5316 \u6216 \u5217\u8868\u521d\u59cb\u5316 \u4e00\u6837\uff0c \u805a\u5408\u521d\u59cb\u5316\u4e5f\u6709\u4e09\u79cd\u5f62\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee frank = { 1 , 32 , 60000.0 }; // \u62f7\u8d1d\u5217\u8868\u521d\u59cb\u5316\uff0c\u4f7f\u7528\u5927\u62ec\u53f7 Employee robert ( 3 , 45 , 62500.0 ); // \u4f7f\u7528\u5c0f\u62ec\u53f7\u7684\u76f4\u63a5\u521d\u59cb\u5316(C++20) Employee joe { 2 , 28 , 45000.0 }; // \u4f7f\u7528\u5927\u62ec\u53f7\u5217\u8868\u7684\u5217\u8868\u521d\u59cb\u5316\uff08\u63a8\u8350\uff09 return 0 ; } \u4e0a\u9762\u8fd9\u4e09\u79cd\u521d\u59cb\u5316\u5f62\u5f0f\u90fd\u4f1a\u8fdb\u884c \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \uff0c\u5373\u7ed3\u6784\u4f53\u6210\u5458\u4f1a\u6309\u7167\u5176\u58f0\u660e\u7684\u987a\u5e8f\u8fdb\u884c\u521d\u59cb\u5316\u3002\u56e0\u6b64\uff0c Employee joe { 2, 28, 45000.0 }; \u9996\u5148\u521d\u59cb\u5316 joe.id \u4e3a2\uff0c\u7136\u540e\u5c06 joe.age \u521d\u59cb\u5316\u4e3a28\uff0c\u6700\u540e\u5c06 joe.wage \u521d\u59cb\u5316\u4e3a 45000.0 \u3002 \u6700\u4f73\u5b9e\u8df5 \u63a8\u8350\u4f7f\u7528\u62ec\u53f7\u5217\u8868\u5f62\u5f0f\uff08\u975e\u62f7\u8d1d\uff09\u8fdb\u884c\u805a\u5408\u521d\u59cb\u5316\u3002 \u521d\u59cb\u5316\u503c\u5217\u8868\u4e2d\u7f3a\u5931\u7684\u503c \u5982\u679c\u805a\u5408\u6570\u636e\u7c7b\u578b\u88ab\u521d\u59cb\u5316\uff0c\u4f46\u521d\u59cb\u5316\u503c\u5217\u8868\u4e2d\u7684\u503c\u4e2a\u6570\u5c11\u4e8e\u6210\u5458\u4e2a\u6570\uff0c\u5219\u5269\u4f59\u7684\u6210\u5458\u4f1a\u88ab \u503c\u521d\u59cb\u5316 \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 2 , 28 }; // joe.wage \u88ab\u503c\u521d\u59cb\u5316\u4e3a 0.0 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c joe.id \u88ab\u521d\u59cb\u5316\u4e3a2\uff0c joe.age \u88ab\u521d\u59cb\u5316\u4e3a28\uff0c\u56e0\u4e3a joe.wage \u6ca1\u6709\u88ab\u663e\u5f0f\u6307\u5b9a\u521d\u59cb\u5316\u503c\uff0c\u5219\u88ab\u503c\u521d\u59cb\u5316\u4e3a0.0\u3002 \u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u7a7a\u7684\u521d\u59cb\u5316\u5217\u8868\u5bf9\u7ed3\u6784\u7684\u6240\u6709\u6210\u5458\u8fdb\u884c\u503c\u521d\u59cb\u5316\uff1a 1 Employee joe {}; // value-initialize all members \u8bd1\u8005\u6ce8 \u5173\u4e8e\u9ed8\u8ba4\u521d\u59cb\u5316\u8bf7\u53c2\u8003 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 \u3002 const \u7ed3\u6784\u4f53 \u7ed3\u6784\u4f53\u7c7b\u578b\u4e5f\u53ef\u4ee5\u662fconst\u7684\uff0c\u800c\u4e14\u548c\u666e\u901aconst\u53d8\u91cf\u4e00\u6837\u5fc5\u987b\u88ab\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 struct Rectangle { double length {}; double width {}; }; int main () { const Rectangle unit { 1.0 , 1.0 }; const Rectangle zero { }; // value-initialize all members return 0 ; } \u6307\u5b9a\u521d\u59cb\u5316\uff08 C++20 \uff09 \u4f7f\u7528\u5217\u8868\u521d\u59cb\u5316\u7ed3\u6784\u4f53\u65f6\uff0c\u662f\u6309\u7167\u6210\u5458\u58f0\u660e\u7684\u987a\u5e8f\u521d\u59cb\u5316\u7684\u3002 1 2 3 4 5 6 7 8 9 10 struct Foo { int a {}; int c {}; }; int main () { Foo f { 1 , 3 }; // f.a = 1, f.c = 3 } \u90a3\u4e48\u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u5982\u679c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u6210\u5458\uff0c\u800c\u4e14\u5e76\u6ca1\u6709\u628a\u5b83\u58f0\u660e\u6210\u6700\u540e\u4e00\u4e2a\uff1a 1 2 3 4 5 6 7 8 9 10 11 struct Foo { int a {}; int b {}; // just added int c {}; }; int main () { Foo f { 1 , 3 }; // now, f.a = 1, f.b = 3, f.c = 0 } \u90a3\u4e48\u4f60\u6240\u6709\u4f7f\u7528\u521d\u59cb\u5316\u503c\u5217\u8868\u7684\u5730\u65b9\uff0c\u90fd\u8981\u5bf9\u5e94\u7684\u79fb\u52a8\u3002\u66f4\u7cdf\u7684\u662f\uff0c\u7f16\u8bd1\u5668\u5e76\u4e0d\u4f1a\u68c0\u6d4b\u5230\u8be5\u95ee\u9898\uff0c\u6bd5\u7adf\u5728\u8bed\u6cd5\u5c42\u9762\u5b83\u662f\u6709\u6548\u7684\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cC++20 \u4e3a\u7ed3\u6784\u4f53\u6210\u5458\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b0\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u2014\u2014 \u6307\u5b9a\u521d\u59cb\u5316 \u3002\u6307\u5b9a\u521d\u59cb\u5316\u5141\u8bb8\u6211\u4eec\u663e\u793a\u5730\u5c06\u521d\u59cb\u5316\u503c\u6620\u5c04\u5230\u6240\u9700\u521d\u59cb\u5316\u7684\u6210\u5458\u3002\u4f46\u662f\uff0c\u5217\u8868\u7684\u987a\u5e8f\u4ecd\u7136\u8981\u6c42\u6309\u7167\u6210\u5458\u5b9a\u4e49\u7684\u987a\u5e8f\u5b9a\u4e49\uff0c\u5426\u5219\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002\u6ca1\u6709\u5728\u5217\u8868\u4e2d\u51fa\u73b0\u7684\u6210\u5458\u4ecd\u7136\u8fdb\u884c\u503c\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct Foo { int a { }; int b { }; int c { }; }; int main () { Foo f1 { . a { 1 }, . c { 3 } }; // ok: f.a = 1, f.b = 0 (\u672a\u51fa\u73b0\uff0c\u503c\u521d\u59cb\u5316), f.c = 3 Foo f2 { . b { 2 }, . a { 1 } }; // \u9519\u8bef: \u521d\u59cb\u5316\u987a\u5e8f\u548c\u6210\u5458\u58f0\u660e\u987a\u5e8f\u4e0d\u5339\u914d return 0 ; } \u6307\u5b9a\u521d\u59cb\u5316\u5f88\u4e0d\u9519\uff0c\u56e0\u4e3a\u5b83\u4eec\u63d0\u4f9b\u4e86\u67d0\u79cd\u7a0b\u5ea6\u7684\u81ea\u6ce8\u91ca\uff0c\u5e76\u6709\u52a9\u4e8e\u786e\u4fdd\u4f60\u4e0d\u4f1a\u65e0\u610f\u4e2d\u6df7\u6dc6\u521d\u59cb\u5316\u503c\u7684\u987a\u5e8f\u3002\u4f46\u662f\uff0c\u6307\u5b9a\u521d\u59cb\u5316\u5f0f\u4e5f\u4f1a\u4f7f\u521d\u59cb\u5316\u5f0f\u5217\u8868\u53d8\u5f97\u975e\u5e38\u6df7\u4e71\uff0c\u56e0\u6b64\u6211\u4eec\u76ee\u524d\u4e0d\u5efa\u8bae\u5c06\u5176\u4f5c\u4e3a\u6700\u4f73\u5b9e\u8df5\u4f7f\u7528\u3002 \u53e6\u5916\uff0c\u7531\u4e8e\u6ca1\u6709\u5f3a\u5236\u89c4\u5b9a\u5728\u521d\u59cb\u5316\u805a\u5408\u7684\u6240\u6709\u5730\u65b9\u90fd\u4e00\u81f4\u4f7f\u7528\u6307\u5b9a\u521d\u59cb\u5316\u5f0f\uff0c\u56e0\u6b64\u907f\u514d\u5411\u73b0\u6709\u805a\u5408\u5b9a\u4e49\u7684\u4e2d\u95f4\u6dfb\u52a0\u65b0\u6210\u5458\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\uff0c\u4ee5\u907f\u514d\u521d\u59cb\u5316\u5f0f\u8f6c\u79fb\u7684\u98ce\u9669\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u5411\u805a\u5408\u4e2d\u6dfb\u52a0\u65b0\u6210\u5458\u65f6\uff0c\u6700\u5b89\u5168\u7684\u505a\u6cd5\u662f\u5c06\u5176\u6dfb\u52a0\u5230\u5b9a\u4e49\u5217\u8868\u7684\u5e95\u90e8\uff0c\u8fd9\u6837\u5176\u4ed6\u6210\u5458\u7684\u521d\u59cb\u5316\u5f0f\u5c31\u4e0d\u9700\u8981\u8c03\u6574\u987a\u5e8f\u3002 \u4f7f\u7528\u521d\u59cb\u5316\u503c\u5217\u8868\u8fdb\u884c\u8d4b\u503c \u5982\u4e0a\u4e00\u8bfe\u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u7ed3\u6784\u4f53\u7684\u6210\u5458\u5355\u72ec\u8d4b\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 32 , 60000.0 }; joe . age = 33 ; // Joe had a birthday joe . wage = 66000.0 ; // and got a raise return 0 ; } \u5982\u679c\u8981\u64cd\u4f5c\u7684\u662f\u4e2a\u522b\u6210\u5458\uff0c\u90a3\u662f\u6ca1\u6709\u95ee\u9898\u7684\u3002\u4f46\u662f\u4e00\u65e6\u8981\u64cd\u4f5c\u5f88\u591a\u6210\u5458\u65f6\uff0c\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u9ebb\u70e6\u3002\u7c7b\u4f3c\u4e8e\u53ef\u4ee5\u7528 \u521d\u59cb\u5316\u503c\u5217\u8868 \u521d\u59cb\u5316\u7ed3\u6784\u4f53\u4e00\u6837\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u521d\u59cb\u5316\u503c\u5217\u8868\u8fdb\u884c\u8d4b\u503c\uff08\u8fdb\u884c \u6210\u5458\u4f9d\u6b21\u8d4b\u503c \uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 32 , 60000.0 }; joe = { joe . id , 33 , 66000.0 }; // Joe had a birthday and got a raise return 0 ; } \u6ce8\u610f\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u5e0c\u671b\u6539\u53d8 joe.id \u7684\u503c\uff0c\u6240\u4ee5\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 joe.id \u4f5c\u4e3a\u5360\u4f4d\u7b26\uff0c\u5c06 joe.id \u8d4b\u503c\u7ed9 joe.id \u3002\u867d\u7136\u4e0d\u592a\u4f18\u96c5\u3002 \u4f7f\u7528\u6307\u5b9a\u521d\u59cb\u5316\u8d4b\u503c\uff08C++20\uff09 \u6307\u5b9a\u521d\u59cb\u5316\u4e5f\u53ef\u4ee5\u7528\u6765\u8d4b\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 32 , 60000.0 }; joe = { . id = joe . id , . age = 33 , . wage = 66000.0 }; // Joe had a birthday and got a raise return 0 ; } \u4efb\u4f55\u6ca1\u6709\u5728\u8fd9\u79cd\u8d4b\u503c\u4e2d\u6307\u5b9a\u7684\u6210\u5458\u90fd\u5c06\u88ab\u8d4b\u503c\u7ed9\u7528\u4e8e \u503c\u521d\u59cb\u5316 \u7684\u503c\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4e0d\u4e3a joe.id \u63d0\u4f9b\u6307\u5b9a\u521d\u59cb\u5316\u503c\uff0c\u5219 joe.id \u4f1a\u88ab\u8d4b\u503c\u4e3a0\u3002","title":"10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316"},{"location":"10-6-struct-aggregate-initialization/#_1","text":"\u548c\u666e\u901a\u53d8\u91cf\u7c7b\u4f3c\uff0c\u6570\u636e\u6210\u5458\u5e76\u4e0d\u4f1a\u88ab\u9ed8\u8ba4\u521d\u59cb\u5316\u3002\u8003\u8651\u4e0b\u9762\u7684\u7ed3\u6784\u4f53\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include struct Employee { int id ; // note: no initializer here int age ; double wage ; }; int main () { Employee joe ; // note: no initializer here either std :: cout << joe . id << '\\n' ; return 0 ; } \u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u63d0\u4f9b\u4efb\u4f55\u521d\u59cb\u5316\u503c\uff0c\u6240\u4ee5\u5f53\u5b9e\u4f8b\u5316 joe \u65f6\uff0c joe.id \u3001 joe.age \u548c joe.wage \u5904\u4e8e\u672a\u521d\u59cb\u5316\u72b6\u6001\u3002\u5f53\u6211\u4eec\u8bd5\u56fe\u6253\u5370 joe.id \u7684\u503c\u65f6\uff0c\u5c06\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4f46\u662f\uff0c\u5728\u5411\u4f60\u5c55\u793a\u5982\u4f55\u521d\u59cb\u5316\u7ed3\u6784\u4e4b\u524d\uff0c\u8ba9\u6211\u4eec\u5148\u7ed5\u8def\u8d70\u4e00\u5c0f\u6bb5\u8def\u3002","title":"\u6570\u636e\u6210\u5458\u9ed8\u8ba4\u4e0d\u4f1a\u88ab\u521d\u59cb\u5316"},{"location":"10-6-struct-aggregate-initialization/#_2","text":"\u5728\u7f16\u7a0b\u9886\u57df\uff0c \u805a\u5408\u6570\u636e\u7c7b\u578b \u6307\u7684\u662f==\u4efb\u4f55\u5305\u542b\u591a\u4e2a\u6570\u636e\u6210\u5458\u7684\u7c7b\u578b==\u3002\u67d0\u4e9b\u7c7b\u578b\u7684\u805a\u5408\u7c7b\u578b\u5141\u8bb8\u6210\u5458\u5177\u6709\u4e0d\u540c\u7684\u7c7b\u578b(\u4f8b\u5982\u7ed3\u6784)\uff0c\u800c\u5176\u4ed6\u7c7b\u578b\u7684\u805a\u5408\u8981\u6c42\u6240\u6709\u6210\u5458\u5fc5\u987b\u5177\u6709\u5355\u4e00\u7c7b\u578b(\u4f8b\u5982\u6570\u7ec4)\u3002 \u5728C++\u4e2d\uff0c\u805a\u5408\u6570\u636e\u7c7b\u578b\u7684\u5b9a\u4e49\u66f4\u5c40\u9650\u4e5f\u66f4\u590d\u6742\u3002 \u6269\u5c55\u9605\u8bfb C++ \u4e2d\u7684\u805a\u5408\u6570\u636e\u7c7b\u578b\u5fc5\u987b\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\uff1a \u662f\u4e00\u4e2a \u7c7b\u7c7b\u578b (\u5305\u62ec struct, class \u6216 union) \u6216\u6570\u7ec4 (\u5305\u62ec\u5185\u7f6e\u6570\u7ec4\u6216 std::array )\uff1b \u6ca1\u6709\u79c1\u6709\u6216\u53d7\u4fdd\u62a4\u7684\u975e\u9759\u6001\u6570\u636e\u6210\u5458\uff1b \u6ca1\u6709\u7528\u6237\u58f0\u660e\u7684\u6216\u7ee7\u627f\u7684 \u6784\u9020\u51fd\u6570 \uff1b \u6ca1\u6709 \u57fa\u7c7b \uff1b \u6ca1\u6709 \u865a\u51fd\u6570 \uff1b \u5148\u628aC++\u805a\u5408\u7684\u7cbe\u786e\u5b9a\u4e49\u653e\u5728\u4e00\u8fb9\uff0c\u5728\u8fd9\u4e00\u70b9\u4e0a\u9700\u8981\u7406\u89e3\u7684\u91cd\u8981\u4e8b\u60c5\u662f\uff0c \u53ea\u6709\u6570\u636e\u6210\u5458\u7684\u7ed3\u6784(\u8fd9\u662f\u6211\u4eec\u5728\u8fd9\u4e9b\u8bfe\u7a0b\u4e2d\u521b\u5efa\u7684\u552f\u4e00\u7c7b\u578b\u7684\u7ed3\u6784)\u662f\u805a\u5408\u6570\u636e\u7c7b\u578b\u3002\u6570\u7ec4(\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u7ae0\u8ba8\u8bba)\u4e5f\u662f\u805a\u5408\u3002","title":"\u4ec0\u4e48\u662f\u805a\u5408\uff1f"},{"location":"10-6-struct-aggregate-initialization/#_3","text":"\u56e0\u4e3a\u666e\u901a\u53d8\u91cf\u53ea\u80fd\u4fdd\u5b58\u4e00\u4e2a\u503c\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u9700\u8981\u63d0\u4f9b\u4e00\u4e2a\u521d\u59cb\u5316\u503c\uff1a 1 int x { 5 }; \u4f46\u662f\uff0c\u7ed3\u6784\u4f53\u4e2d\u6709\u591a\u4e2a\u6210\u5458\uff1a 1 2 3 4 5 6 struct Employee { int id {}; int age {}; double wage {}; }; \u5f53\u5b9a\u4e49\u4e00\u4e2a\u5177\u6709\u7ed3\u6784\u7c7b\u578b\u7684\u5bf9\u8c61\u65f6\uff0c\u6211\u4eec\u80fd\u591f\u5728\u521d\u59cb\u5316\u7ed3\u6784\u4f53\u65f6\u540c\u65f6\u521d\u59cb\u5316\u591a\u4e2a\u6210\u5458\u7684\u65b9\u6cd5\uff1a 1 Employee joe ; // \u5982\u4f55\u521d\u59cb\u5316 joe.id, joe.age \u548c joe.wage? \u805a\u5408\u6570\u636e\u7c7b\u578b\u4f7f\u7528\u4e00\u79cd\u79f0\u4e3a \u805a\u5408\u521d\u59cb\u5316 \u7684\u521d\u59cb\u5316\u5f62\u5f0f\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u76f4\u63a5\u521d\u59cb\u5316\u805a\u5408\u7684\u6210\u5458\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u63d0\u4f9b\u4e00\u4e2a \u521d\u59cb\u5316\u503c\u5217\u8868 \u4f5c\u4e3a\u521d\u59cb\u5316\u503c\u3002\u521d\u59cb\u5316\u503c\u5217\u8868\u5c31\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u521d\u59cb\u5316\u503c\uff0c\u4ee5\u9017\u53f7\u5206\u5272\u7684\u503c\u5217\u8868\u3002 \u5c31\u50cf\u666e\u901a\u53d8\u91cf\u53ef\u4ee5\u88ab \u62f7\u8d1d\u521d\u59cb\u5316 \u3001 \u76f4\u63a5\u521d\u59cb\u5316 \u6216 \u5217\u8868\u521d\u59cb\u5316 \u4e00\u6837\uff0c \u805a\u5408\u521d\u59cb\u5316\u4e5f\u6709\u4e09\u79cd\u5f62\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee frank = { 1 , 32 , 60000.0 }; // \u62f7\u8d1d\u5217\u8868\u521d\u59cb\u5316\uff0c\u4f7f\u7528\u5927\u62ec\u53f7 Employee robert ( 3 , 45 , 62500.0 ); // \u4f7f\u7528\u5c0f\u62ec\u53f7\u7684\u76f4\u63a5\u521d\u59cb\u5316(C++20) Employee joe { 2 , 28 , 45000.0 }; // \u4f7f\u7528\u5927\u62ec\u53f7\u5217\u8868\u7684\u5217\u8868\u521d\u59cb\u5316\uff08\u63a8\u8350\uff09 return 0 ; } \u4e0a\u9762\u8fd9\u4e09\u79cd\u521d\u59cb\u5316\u5f62\u5f0f\u90fd\u4f1a\u8fdb\u884c \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \uff0c\u5373\u7ed3\u6784\u4f53\u6210\u5458\u4f1a\u6309\u7167\u5176\u58f0\u660e\u7684\u987a\u5e8f\u8fdb\u884c\u521d\u59cb\u5316\u3002\u56e0\u6b64\uff0c Employee joe { 2, 28, 45000.0 }; \u9996\u5148\u521d\u59cb\u5316 joe.id \u4e3a2\uff0c\u7136\u540e\u5c06 joe.age \u521d\u59cb\u5316\u4e3a28\uff0c\u6700\u540e\u5c06 joe.wage \u521d\u59cb\u5316\u4e3a 45000.0 \u3002 \u6700\u4f73\u5b9e\u8df5 \u63a8\u8350\u4f7f\u7528\u62ec\u53f7\u5217\u8868\u5f62\u5f0f\uff08\u975e\u62f7\u8d1d\uff09\u8fdb\u884c\u805a\u5408\u521d\u59cb\u5316\u3002","title":"\u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316"},{"location":"10-6-struct-aggregate-initialization/#_4","text":"\u5982\u679c\u805a\u5408\u6570\u636e\u7c7b\u578b\u88ab\u521d\u59cb\u5316\uff0c\u4f46\u521d\u59cb\u5316\u503c\u5217\u8868\u4e2d\u7684\u503c\u4e2a\u6570\u5c11\u4e8e\u6210\u5458\u4e2a\u6570\uff0c\u5219\u5269\u4f59\u7684\u6210\u5458\u4f1a\u88ab \u503c\u521d\u59cb\u5316 \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 2 , 28 }; // joe.wage \u88ab\u503c\u521d\u59cb\u5316\u4e3a 0.0 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c joe.id \u88ab\u521d\u59cb\u5316\u4e3a2\uff0c joe.age \u88ab\u521d\u59cb\u5316\u4e3a28\uff0c\u56e0\u4e3a joe.wage \u6ca1\u6709\u88ab\u663e\u5f0f\u6307\u5b9a\u521d\u59cb\u5316\u503c\uff0c\u5219\u88ab\u503c\u521d\u59cb\u5316\u4e3a0.0\u3002 \u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u7a7a\u7684\u521d\u59cb\u5316\u5217\u8868\u5bf9\u7ed3\u6784\u7684\u6240\u6709\u6210\u5458\u8fdb\u884c\u503c\u521d\u59cb\u5316\uff1a 1 Employee joe {}; // value-initialize all members \u8bd1\u8005\u6ce8 \u5173\u4e8e\u9ed8\u8ba4\u521d\u59cb\u5316\u8bf7\u53c2\u8003 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 \u3002","title":"\u521d\u59cb\u5316\u503c\u5217\u8868\u4e2d\u7f3a\u5931\u7684\u503c"},{"location":"10-6-struct-aggregate-initialization/#const","text":"\u7ed3\u6784\u4f53\u7c7b\u578b\u4e5f\u53ef\u4ee5\u662fconst\u7684\uff0c\u800c\u4e14\u548c\u666e\u901aconst\u53d8\u91cf\u4e00\u6837\u5fc5\u987b\u88ab\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 struct Rectangle { double length {}; double width {}; }; int main () { const Rectangle unit { 1.0 , 1.0 }; const Rectangle zero { }; // value-initialize all members return 0 ; }","title":"const \u7ed3\u6784\u4f53"},{"location":"10-6-struct-aggregate-initialization/#c20","text":"\u4f7f\u7528\u5217\u8868\u521d\u59cb\u5316\u7ed3\u6784\u4f53\u65f6\uff0c\u662f\u6309\u7167\u6210\u5458\u58f0\u660e\u7684\u987a\u5e8f\u521d\u59cb\u5316\u7684\u3002 1 2 3 4 5 6 7 8 9 10 struct Foo { int a {}; int c {}; }; int main () { Foo f { 1 , 3 }; // f.a = 1, f.c = 3 } \u90a3\u4e48\u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u5982\u679c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u6210\u5458\uff0c\u800c\u4e14\u5e76\u6ca1\u6709\u628a\u5b83\u58f0\u660e\u6210\u6700\u540e\u4e00\u4e2a\uff1a 1 2 3 4 5 6 7 8 9 10 11 struct Foo { int a {}; int b {}; // just added int c {}; }; int main () { Foo f { 1 , 3 }; // now, f.a = 1, f.b = 3, f.c = 0 } \u90a3\u4e48\u4f60\u6240\u6709\u4f7f\u7528\u521d\u59cb\u5316\u503c\u5217\u8868\u7684\u5730\u65b9\uff0c\u90fd\u8981\u5bf9\u5e94\u7684\u79fb\u52a8\u3002\u66f4\u7cdf\u7684\u662f\uff0c\u7f16\u8bd1\u5668\u5e76\u4e0d\u4f1a\u68c0\u6d4b\u5230\u8be5\u95ee\u9898\uff0c\u6bd5\u7adf\u5728\u8bed\u6cd5\u5c42\u9762\u5b83\u662f\u6709\u6548\u7684\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cC++20 \u4e3a\u7ed3\u6784\u4f53\u6210\u5458\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b0\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u2014\u2014 \u6307\u5b9a\u521d\u59cb\u5316 \u3002\u6307\u5b9a\u521d\u59cb\u5316\u5141\u8bb8\u6211\u4eec\u663e\u793a\u5730\u5c06\u521d\u59cb\u5316\u503c\u6620\u5c04\u5230\u6240\u9700\u521d\u59cb\u5316\u7684\u6210\u5458\u3002\u4f46\u662f\uff0c\u5217\u8868\u7684\u987a\u5e8f\u4ecd\u7136\u8981\u6c42\u6309\u7167\u6210\u5458\u5b9a\u4e49\u7684\u987a\u5e8f\u5b9a\u4e49\uff0c\u5426\u5219\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002\u6ca1\u6709\u5728\u5217\u8868\u4e2d\u51fa\u73b0\u7684\u6210\u5458\u4ecd\u7136\u8fdb\u884c\u503c\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct Foo { int a { }; int b { }; int c { }; }; int main () { Foo f1 { . a { 1 }, . c { 3 } }; // ok: f.a = 1, f.b = 0 (\u672a\u51fa\u73b0\uff0c\u503c\u521d\u59cb\u5316), f.c = 3 Foo f2 { . b { 2 }, . a { 1 } }; // \u9519\u8bef: \u521d\u59cb\u5316\u987a\u5e8f\u548c\u6210\u5458\u58f0\u660e\u987a\u5e8f\u4e0d\u5339\u914d return 0 ; } \u6307\u5b9a\u521d\u59cb\u5316\u5f88\u4e0d\u9519\uff0c\u56e0\u4e3a\u5b83\u4eec\u63d0\u4f9b\u4e86\u67d0\u79cd\u7a0b\u5ea6\u7684\u81ea\u6ce8\u91ca\uff0c\u5e76\u6709\u52a9\u4e8e\u786e\u4fdd\u4f60\u4e0d\u4f1a\u65e0\u610f\u4e2d\u6df7\u6dc6\u521d\u59cb\u5316\u503c\u7684\u987a\u5e8f\u3002\u4f46\u662f\uff0c\u6307\u5b9a\u521d\u59cb\u5316\u5f0f\u4e5f\u4f1a\u4f7f\u521d\u59cb\u5316\u5f0f\u5217\u8868\u53d8\u5f97\u975e\u5e38\u6df7\u4e71\uff0c\u56e0\u6b64\u6211\u4eec\u76ee\u524d\u4e0d\u5efa\u8bae\u5c06\u5176\u4f5c\u4e3a\u6700\u4f73\u5b9e\u8df5\u4f7f\u7528\u3002 \u53e6\u5916\uff0c\u7531\u4e8e\u6ca1\u6709\u5f3a\u5236\u89c4\u5b9a\u5728\u521d\u59cb\u5316\u805a\u5408\u7684\u6240\u6709\u5730\u65b9\u90fd\u4e00\u81f4\u4f7f\u7528\u6307\u5b9a\u521d\u59cb\u5316\u5f0f\uff0c\u56e0\u6b64\u907f\u514d\u5411\u73b0\u6709\u805a\u5408\u5b9a\u4e49\u7684\u4e2d\u95f4\u6dfb\u52a0\u65b0\u6210\u5458\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\uff0c\u4ee5\u907f\u514d\u521d\u59cb\u5316\u5f0f\u8f6c\u79fb\u7684\u98ce\u9669\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u5411\u805a\u5408\u4e2d\u6dfb\u52a0\u65b0\u6210\u5458\u65f6\uff0c\u6700\u5b89\u5168\u7684\u505a\u6cd5\u662f\u5c06\u5176\u6dfb\u52a0\u5230\u5b9a\u4e49\u5217\u8868\u7684\u5e95\u90e8\uff0c\u8fd9\u6837\u5176\u4ed6\u6210\u5458\u7684\u521d\u59cb\u5316\u5f0f\u5c31\u4e0d\u9700\u8981\u8c03\u6574\u987a\u5e8f\u3002","title":"\u6307\u5b9a\u521d\u59cb\u5316\uff08\u00a0C++20 \uff09"},{"location":"10-6-struct-aggregate-initialization/#_5","text":"\u5982\u4e0a\u4e00\u8bfe\u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u7ed3\u6784\u4f53\u7684\u6210\u5458\u5355\u72ec\u8d4b\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 32 , 60000.0 }; joe . age = 33 ; // Joe had a birthday joe . wage = 66000.0 ; // and got a raise return 0 ; } \u5982\u679c\u8981\u64cd\u4f5c\u7684\u662f\u4e2a\u522b\u6210\u5458\uff0c\u90a3\u662f\u6ca1\u6709\u95ee\u9898\u7684\u3002\u4f46\u662f\u4e00\u65e6\u8981\u64cd\u4f5c\u5f88\u591a\u6210\u5458\u65f6\uff0c\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u9ebb\u70e6\u3002\u7c7b\u4f3c\u4e8e\u53ef\u4ee5\u7528 \u521d\u59cb\u5316\u503c\u5217\u8868 \u521d\u59cb\u5316\u7ed3\u6784\u4f53\u4e00\u6837\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u521d\u59cb\u5316\u503c\u5217\u8868\u8fdb\u884c\u8d4b\u503c\uff08\u8fdb\u884c \u6210\u5458\u4f9d\u6b21\u8d4b\u503c \uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 32 , 60000.0 }; joe = { joe . id , 33 , 66000.0 }; // Joe had a birthday and got a raise return 0 ; } \u6ce8\u610f\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u5e0c\u671b\u6539\u53d8 joe.id \u7684\u503c\uff0c\u6240\u4ee5\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 joe.id \u4f5c\u4e3a\u5360\u4f4d\u7b26\uff0c\u5c06 joe.id \u8d4b\u503c\u7ed9 joe.id \u3002\u867d\u7136\u4e0d\u592a\u4f18\u96c5\u3002","title":"\u4f7f\u7528\u521d\u59cb\u5316\u503c\u5217\u8868\u8fdb\u884c\u8d4b\u503c"},{"location":"10-6-struct-aggregate-initialization/#c20_1","text":"\u6307\u5b9a\u521d\u59cb\u5316\u4e5f\u53ef\u4ee5\u7528\u6765\u8d4b\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 32 , 60000.0 }; joe = { . id = joe . id , . age = 33 , . wage = 66000.0 }; // Joe had a birthday and got a raise return 0 ; } \u4efb\u4f55\u6ca1\u6709\u5728\u8fd9\u79cd\u8d4b\u503c\u4e2d\u6307\u5b9a\u7684\u6210\u5458\u90fd\u5c06\u88ab\u8d4b\u503c\u7ed9\u7528\u4e8e \u503c\u521d\u59cb\u5316 \u7684\u503c\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4e0d\u4e3a joe.id \u63d0\u4f9b\u6307\u5b9a\u521d\u59cb\u5316\u503c\uff0c\u5219 joe.id \u4f1a\u88ab\u8d4b\u503c\u4e3a0\u3002","title":"\u4f7f\u7528\u6307\u5b9a\u521d\u59cb\u5316\u8d4b\u503c\uff08C++20\uff09"},{"location":"10-7-default-member-initialization/","tags":["struct","initialization"],"text":"Key Takeaway \u5b9e\u4f8b\u5316\u805a\u5408\u7c7b\u578b\u5bf9\u8c61\u65f6\u5982\u679c\u6ca1\u6709\u63d0\u4f9b\u521d\u59cb\u5316\u503c\uff0c\u5219\u8868\u793a\u6ca1\u6709\u521d\u59cb\u5316\u8be5\u805a\u5408\u7c7b\u578b\u3002\u6b64\u65f6\u5b83\u7684\u6210\u5458\u4f1a\u8fdb\u884c\u9ed8\u8ba4\u521d\u59cb\u5316\u3002\u6ca1\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\u7684\u6210\u5458\u5219\u4e3a\u672a\u521d\u59cb\u5316\u72b6\u6001\u3002 \u521d\u59cb\u5316\u805a\u5408\u7c7b\u578b\u5bf9\u8c61\u65f6\uff0c\u5982\u679c\u521d\u59cb\u5316\u503c\u5c11\u4e8e\u6210\u5458\uff0c\u5219\u5269\u4f59\u7684\u6210\u5458\u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\uff0c\u5982\u679c\u6ca1\u6709\u63d0\u4f9b\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u5219\u503c\u521d\u59cb\u5316\u4e3a0. \u5728\u5b9a\u4e49\u7ed3\u6784\u4f53\uff08\u6216\u7c7b\uff09\u7c7b\u578b\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u6210\u5458\u90fd\u63d0\u4f9b\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\u4f5c\u4e3a\u5176\u5b9a\u4e49\u7684\u4e00\u90e8\u5206\u3002 \u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \uff0c\u800c\u8fd9\u4e2a\u521d\u59cb\u5316\u503c\u79f0\u4e3a \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u503c \u3002 \u4e3e\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 struct Something { int x ; // no initialization value (bad) int y {}; // value-initialized by default int z { 2 }; // explicit default value }; int main () { Something s1 ; // s1.x is uninitialized, s1.y is 0, and s1.z is 2 return 0 ; } \u5728\u4e0a\u9762 Something \u7684\u5b9a\u4e49\u4e2d\uff0c x \u5e76\u6ca1\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\u3001 y \u8fdb\u884c\u9ed8\u8ba4\u7684\u503c\u521d\u59cb\u5316\u800c z \u5219\u88ab\u521d\u59cb\u5316\u4e3a2\u3002 \u5982\u679c\u5728 \u5b9e\u4f8b\u5316 Something \u7c7b\u578b\u7684\u5bf9\u8c61\u65f6\u6ca1\u6709\u63d0\u4f9b\u663e\u5f0f\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f1a\u4f7f\u7528\u6210\u5458\u7684\u8fd9\u4e9b\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c\u3002 s1 \u5bf9\u8c61\u6ca1\u6709\u521d\u59cb\u5316\u503c\uff0c\u6240\u4ee5 s1 \u7684\u6210\u5458\u4eec\u4f1a\u88ab\u521d\u59cb\u5316\u4e3a\u5b83\u4eec\u7684\u9ed8\u8ba4\u503c\u3002 s1.x \u5219\u56e0\u4e3a\u6ca1\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\u800c\u4fdd\u6301\u672a\u521d\u59cb\u5316\u72b6\u6001\u3002 s1.y \u8fdb\u884c\u9ed8\u8ba4\u503c\u521d\u59cb\u5316\uff0c\u5f97\u5230\u521d\u503c0\uff0c\u800c s1.z \u5219\u88ab\u521d\u59cb\u5316\u4e3a 2 \u3002 \u6ce8\u610f\uff0c\u5373\u4fbf\u6211\u4eec\u5e76\u6ca1\u6709\u4e3a s1.z \u63d0\u4f9b\u663e\u5f0f\u5730\u521d\u59cb\u5316\u503c\uff0c\u5b83\u4ecd\u7136\u88ab\u521d\u59cb\u5316\u4e3a\u4e00\u4e2a\u975e\u96f6\u503c\uff0c\u56e0\u4e3a\u6211\u4eec\u4e3a\u5176\u63d0\u4f9b\u4e86\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c\u3002 \u5173\u952e\u4fe1\u606f \u4f7f\u7528\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u5f0f(\u6216\u6211\u4eec\u5c06\u5728\u540e\u9762\u4ecb\u7ecd\u7684\u5176\u4ed6\u673a\u5236)\uff0c\u5373\u4f7f\u6ca1\u6709\u63d0\u4f9b\u663e\u5f0f\u521d\u59cb\u5316\u5f0f\uff0c\u7ed3\u6784\u548c\u7c7b\u4e5f\u53ef\u4ee5\u81ea\u6211\u521d\u59cb\u5316\uff01 \u663e\u5f0f\u521d\u59cb\u5316\u503c\u4f18\u5148\u7ea7\u9ad8\u4e8e\u9ed8\u8ba4\u521d\u59cb\u5316\u503c \u5217\u8868\u521d\u59cb\u5316\u5f0f\u4e2d\u7684\u663e\u5f0f\u503c\u603b\u662f\u4f18\u5148\u4e8e\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 struct Something { int x ; // no default initialization value (bad) int y {}; // value-initialized by default int z { 2 }; // explicit default value }; int main () { Something s2 { 5 , 6 , 7 }; // use explicit initializers for s2.x, s2.y, and s2.z (no default values are used) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cs2 \u7684\u6bcf\u4e2a\u6210\u5458\u90fd\u5177\u6709\u663e\u5f0f\u5730\u521d\u59cb\u5316\u503c\uff0c\u6240\u4ee5\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u5e76\u4e0d\u751f\u6548\u3002 s2.x \u3001 s2.y \u548c s2.z \u5206\u522b\u88ab\u521d\u59cb\u5316\u4e3a 5 \u3001 6 \u548c 7 \u3002 \u5b58\u5728\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\u4e14\u521d\u59cb\u5316\u5217\u8868\u6ca1\u6709\u63d0\u4f9b\u521d\u59cb\u5316\u503c\u7684\u60c5\u51b5 \u5728\u4e0a\u4e00\u8282\u8bfe\u4e2d\uff08 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 \uff09\u6211\u4eec\u63d0\u5230\uff0c \u5982\u679c\u805a\u5408\u6570\u636e\u7ed3\u6784\u88ab\u521d\u59cb\u5316\u4e86\uff0c\u4f46\u521d\u59cb\u5316\u503c\u7684\u4e2a\u6570\u6bd4\u5b9e\u9645\u6210\u5458\u8981\u5c11\uff0c\u5219\u5269\u4e0b\u7684\u6210\u5458\u53d8\u91cf\u5219\u4f1a\u8fdb\u884c\u503c\u521d\u59cb\u5316\u3002\u4f46\u662f\uff0c\u5982\u679c\u8be5\u6210\u5458\u5177\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f1a\u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 struct Something { int x ; // no default initialization value (bad) int y {}; // value-initialized by default int z { 2 }; // explicit default value }; int main () { Something s3 {}; // value initialize s3.x, use default values for s3.y and s3.z return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c s3 \u7684\u521d\u59cb\u5316\u503c\u5217\u8868\u662f\u4e00\u4e2a\u7a7a\u5217\u8868\uff0c\u5373\u6ca1\u6709\u63d0\u4f9b\u4efb\u4f55\u7684\u663e\u5f0f\u521d\u59cb\u5316\u503c\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u63d0\u4f9b\u4e86\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f7f\u7528\u8be5\u503c\uff0c\u5426\u5219\u5219\u4f7f\u7528 \u503c\u521d\u59cb\u5316 \u3002\u56e0\u6b64 s3.x (\u6ca1\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c) \u4f1a\u88ab\u503c\u521d\u59cb\u5316\u4e3a0\u3002 s3.y \u7684\u503c\u88ab\u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a0\uff0c\u800c s3.z \u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a 2\u3002 \u590d\u4e60\uff1a\u521d\u59cb\u5316\u7684\u53ef\u80fd\u6027 \u5982\u679c\u805a\u5408\u5bf9\u8c61\u5b9a\u4e49\u65f6\u6709 \u521d\u59cb\u5316\u503c\u5217\u8868 \uff08\u5b9e\u4f8b\u5316\u7684\u540c\u65f6\u521d\u59cb\u5316\uff09\uff1a \u5982\u679c\u5177\u6709\u663e\u5f0f\u7684\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f7f\u7528\u8be5\u503c\uff1b \u5982\u679c\u6709\u7f3a\u5931\u7684\u521d\u59cb\u5316\u503c\uff0c\u4f46\u5b58\u5728\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c\u8fdb\u884c \u9ed8\u8ba4\u521d\u59cb\u5316 \uff1b \u5982\u679c\u6709\u7f3a\u5931\u7684\u521d\u59cb\u5316\u503c\uff0c\u4f46\u662f\u6ca1\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f7f\u7528 \u503c\u521d\u59cb\u5316 \u3002 \u5982\u679c\u805a\u5408\u5bf9\u8c61\u5b9a\u4e49\u65f6\u6ca1\u6709\u521d\u59cb\u5316\u503c\u5217\u8868\uff08\u5b9e\u4f8b\u5316\u65f6\u672a\u8fdb\u884c\u521d\u59cb\u5316\uff09\uff1a \u5982\u679c\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c\u8fdb\u884c\u9ed8\u8ba4\u521d\u59cb\u5316\uff1b \u5982\u679c\u4e0d\u5b58\u5728\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u5219\u5bf9\u5e94\u6210\u5458\u4fdd\u6301\u672a\u521d\u59cb\u5316\u72b6\u6001\u3002 \u6210\u5458\u603b\u662f\u6309\u7167\u58f0\u660e\u7684\u987a\u5e8f\u521d\u59cb\u5316\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5bf9\u4e0a\u8ff0\u5404\u79cd\u53ef\u80fd\u6027\u8fdb\u884c\u4e86\u603b\u7ed3\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 struct Something { int x ; // no default initialization value (bad) int y {}; // value-initialized by default int z { 2 }; // explicit default value }; int main () { Something s1 ; // No initializer list: s1.x is uninitialized, s1.y and s1.z use defaults Something s2 { 5 , 6 , 7 }; // Explicit initializers: s2.x, s2.y, and s2.z use explicit values (no default values are used) Something s3 {}; // Missing initializers: s3.x is value initialized, s3.y and s3.z use defaults return 0 ; } \u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5c24\u5176\u8981\u6ce8\u610f\u7684\u662f s1.x \u3002\u56e0\u4e3a s1 \u6ca1\u6709\u521d\u59cb\u5316\u503c\u5217\u8868\uff0c\u800c\u4e14 x \u4e5f\u6ca1\u6709\u9ed8\u8ba4\u7684\u6210\u5458\u521d\u59cb\u5316\u503c\uff0c\u6240\u4ee5\u5b83\u5904\u4e8e\u672a\u521d\u59cb\u5316\u72b6\u6001\uff08\u8fd9\u662f\u4e0d\u5bf9\u7684\uff0c\u56e0\u4e3a\u53d8\u91cf\u4e00\u5b9a\u8981\u521d\u59cb\u5316\u624d\u597d\uff09\u3002 \u5e94\u5f53\u59cb\u7ec8\u4e3a\u6210\u5458\u63d0\u4f9b\u9ed8\u8ba4\u521d\u59cb\u5316\u503c \u4e3a\u4e86\u907f\u514d\u51fa\u73b0\u672a\u521d\u59cb\u5316\u7684\u6210\u5458\uff0c\u9700\u786e\u4fdd\u6bcf\u4e2a\u6210\u5458\u90fd\u6709\u4e00\u4e2a\u9ed8\u8ba4\u503c(\u8981\u4e48\u662f\u663e\u5f0f\u9ed8\u8ba4\u503c\uff0c\u8981\u4e48\u662f\u7a7a\u7684\u82b1\u62ec\u53f7\u5bf9)\u3002\u8fd9\u6837\uff0c\u65e0\u8bba\u662f\u5426\u63d0\u4f9b\u521d\u59cb\u5316\u5217\u8868\uff0c\u6210\u5458\u90fd\u5c06\u88ab\u521d\u59cb\u5316\u4e3a\u67d0\u4e2a\u503c\u3002 \u8003\u8651\u4e0b\u9762\u4f8b\u5b50\u4e2d\u4e2d\u7684\u7ed3\u6784\u4f53\uff0c\u5b83\u7684\u6240\u6709\u6210\u5458\u90fd\u6709\u9ed8\u8ba4\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 struct Fraction { int numerator { }; // we should use { 0 } here, but for the sake of example we'll use value initialization instead int denominator { 1 }; }; int main () { Fraction f1 ; // f1.numerator value initialized to 0, f1.denominator defaulted to 1 Fraction f2 {}; // f2.numerator value initialized to 0, f2.denominator defaulted to 1 Fraction f3 { 6 }; // f3.numerator initialized to 6, f3.denominator defaulted to 1 Fraction f4 { 5 , 8 }; // f4.numerator initialized to 5, f4.denominator initialized to 8 return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u7684\u6210\u5458\u90fd\u4f1a\u88ab\u521d\u59cb\u5316\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e3a\u6240\u6709\u6210\u5458\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002\u8fd9\u786e\u4fdd\u4e86\u5373\u4f7f\u53d8\u91cf\u5b9a\u4e49\u4e0d\u5305\u542b\u521d\u59cb\u5316\u5217\u8868\uff0c\u6210\u5458\u4e5f\u4f1a\u88ab\u521d\u59cb\u5316\u3002 \u9ed8\u8ba4\u521d\u59cb\u5316 vs \u805a\u5408\u7c7b\u578b\u7684\u503c\u521d\u59cb\u5316 \u56de\u987e\u4e00\u4e0b\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u4e24\u4e2a\u521d\u59cb\u5316\u65b9\u5f0f\uff1a 1 2 Fraction f1 ; // f1.numerator value initialized to 0, f1.denominator defaulted to 1 Fraction f2 {}; // f2.numerator value initialized to 0, f2.denominator defaulted to 1 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c f1 \u662f\u9ed8\u8ba4\u521d\u59cb\u5316\uff0c\u800c f2 \u662f\u503c\u521d\u59cb\u5316\u3002\u5c3d\u7ba1\u7ed3\u679c\u662f\u4e00\u6837\u7684 ( numerator \u521d\u59cb\u5316\u4e3a0\u800c denominator \u521d\u59cb\u5316\u4e3a1)\u3002\u6240\u4ee5\uff0c\u5e94\u8be5\u4f7f\u7528\u54ea\u79cd\u65b9\u5f0f\u5462\uff1f \u503c\u521d\u59cb\u5316( f2 )\u662f\u66f4\u5b89\u5168\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u786e\u4fdd\u5373\u4f7f\u6210\u5458\u6ca1\u6709\u521d\u59cb\u5316\u503c\u4e5f\u80fd\u901a\u8fc7\u503c\u521d\u59cb\u5316\u88ab\u521d\u59cb\u5316(\u5c3d\u7ba1\u6211\u4eec\u5e94\u8be5\u4e3a\u6bcf\u4e2a\u6210\u5458\u90fd\u63d0\u4f9b\u9ed8\u8ba4\u503c\uff0c\u8be5\u65b9\u6cd5\u53ef\u4ee5\u9632\u6b62\u6211\u4eec\u9057\u6f0f)\u3002 \u503c\u521d\u59cb\u5316\u8fd8\u6709\u4e00\u4e2a\u597d\u5904\u2014\u2014\u5b83\u4e0e\u521d\u59cb\u5316\u5176\u4ed6\u7c7b\u578b\u7684\u5bf9\u8c61\u7684\u65b9\u5f0f\u4e00\u81f4\u3002\u4e00\u81f4\u6027\u6709\u52a9\u4e8e\u9632\u6b62\u9519\u8bef\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u6ca1\u6709\u4e3a\u805a\u5408\u63d0\u4f9b\u663e\u5f0f\u521d\u59cb\u5316\u503c\uff0c\u5219\u9996\u9009\u503c\u521d\u59cb\u5316(\u5e26\u7a7a\u5927\u62ec\u53f7\u521d\u59cb\u5316\u5668)\u800c\u4e0d\u662f\u9ed8\u8ba4\u521d\u59cb\u5316(\u4e0d\u5e26\u5927\u62ec\u53f7)\u3002 \u4e5f\u5c31\u662f\u8bf4\uff0c\u7a0b\u5e8f\u5458\u5bf9\u7c7b\u7c7b\u578b\u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\u800c\u4e0d\u662f\u503c\u521d\u59cb\u5316\u662f\u5f88\u5e38\u89c1\u7684\u3002\u8fd9\u90e8\u5206\u662f\u7531\u4e8e\u5386\u53f2\u539f\u56e0(\u56e0\u4e3a\u503c\u521d\u59cb\u5316\u76f4\u5230C++ 11\u624d\u5f15\u5165)\uff0c\u90e8\u5206\u662f\u56e0\u4e3a\u5728\u7c7b\u4f3c\u7684\u60c5\u51b5\u4e0b(\u5bf9\u4e8e\u975e\u805a\u5408)\uff0c\u9ed8\u8ba4\u521d\u59cb\u5316\u66f4\u9ad8\u6548(\u6211\u4eec\u5728 13.5 -\u6784\u9020\u51fd\u6570 \u4e2d\u8ba8\u8bba\u4e86\u8fd9\u79cd\u60c5\u51b5)\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u4e0d\u4f1a\u5728\u8fd9\u4e9b\u6559\u7a0b\u4e2d\u5f3a\u5236\u4f7f\u7528\u7ed3\u6784\u548c\u7c7b\u7684\u503c\u521d\u59cb\u5316\uff0c\u4f46\u6211\u4eec\u5f3a\u70c8\u5efa\u8bae\u8fd9\u6837\u505a\u3002","title":"10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316"},{"location":"10-7-default-member-initialization/#_1","text":"\u5217\u8868\u521d\u59cb\u5316\u5f0f\u4e2d\u7684\u663e\u5f0f\u503c\u603b\u662f\u4f18\u5148\u4e8e\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 struct Something { int x ; // no default initialization value (bad) int y {}; // value-initialized by default int z { 2 }; // explicit default value }; int main () { Something s2 { 5 , 6 , 7 }; // use explicit initializers for s2.x, s2.y, and s2.z (no default values are used) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cs2 \u7684\u6bcf\u4e2a\u6210\u5458\u90fd\u5177\u6709\u663e\u5f0f\u5730\u521d\u59cb\u5316\u503c\uff0c\u6240\u4ee5\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u5e76\u4e0d\u751f\u6548\u3002 s2.x \u3001 s2.y \u548c s2.z \u5206\u522b\u88ab\u521d\u59cb\u5316\u4e3a 5 \u3001 6 \u548c 7 \u3002","title":"\u663e\u5f0f\u521d\u59cb\u5316\u503c\u4f18\u5148\u7ea7\u9ad8\u4e8e\u9ed8\u8ba4\u521d\u59cb\u5316\u503c"},{"location":"10-7-default-member-initialization/#_2","text":"\u5728\u4e0a\u4e00\u8282\u8bfe\u4e2d\uff08 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 \uff09\u6211\u4eec\u63d0\u5230\uff0c \u5982\u679c\u805a\u5408\u6570\u636e\u7ed3\u6784\u88ab\u521d\u59cb\u5316\u4e86\uff0c\u4f46\u521d\u59cb\u5316\u503c\u7684\u4e2a\u6570\u6bd4\u5b9e\u9645\u6210\u5458\u8981\u5c11\uff0c\u5219\u5269\u4e0b\u7684\u6210\u5458\u53d8\u91cf\u5219\u4f1a\u8fdb\u884c\u503c\u521d\u59cb\u5316\u3002\u4f46\u662f\uff0c\u5982\u679c\u8be5\u6210\u5458\u5177\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f1a\u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 struct Something { int x ; // no default initialization value (bad) int y {}; // value-initialized by default int z { 2 }; // explicit default value }; int main () { Something s3 {}; // value initialize s3.x, use default values for s3.y and s3.z return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c s3 \u7684\u521d\u59cb\u5316\u503c\u5217\u8868\u662f\u4e00\u4e2a\u7a7a\u5217\u8868\uff0c\u5373\u6ca1\u6709\u63d0\u4f9b\u4efb\u4f55\u7684\u663e\u5f0f\u521d\u59cb\u5316\u503c\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u63d0\u4f9b\u4e86\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f7f\u7528\u8be5\u503c\uff0c\u5426\u5219\u5219\u4f7f\u7528 \u503c\u521d\u59cb\u5316 \u3002\u56e0\u6b64 s3.x (\u6ca1\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c) \u4f1a\u88ab\u503c\u521d\u59cb\u5316\u4e3a0\u3002 s3.y \u7684\u503c\u88ab\u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a0\uff0c\u800c s3.z \u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a 2\u3002","title":"\u5b58\u5728\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\u4e14\u521d\u59cb\u5316\u5217\u8868\u6ca1\u6709\u63d0\u4f9b\u521d\u59cb\u5316\u503c\u7684\u60c5\u51b5"},{"location":"10-7-default-member-initialization/#_3","text":"\u5982\u679c\u805a\u5408\u5bf9\u8c61\u5b9a\u4e49\u65f6\u6709 \u521d\u59cb\u5316\u503c\u5217\u8868 \uff08\u5b9e\u4f8b\u5316\u7684\u540c\u65f6\u521d\u59cb\u5316\uff09\uff1a \u5982\u679c\u5177\u6709\u663e\u5f0f\u7684\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f7f\u7528\u8be5\u503c\uff1b \u5982\u679c\u6709\u7f3a\u5931\u7684\u521d\u59cb\u5316\u503c\uff0c\u4f46\u5b58\u5728\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c\u8fdb\u884c \u9ed8\u8ba4\u521d\u59cb\u5316 \uff1b \u5982\u679c\u6709\u7f3a\u5931\u7684\u521d\u59cb\u5316\u503c\uff0c\u4f46\u662f\u6ca1\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f7f\u7528 \u503c\u521d\u59cb\u5316 \u3002 \u5982\u679c\u805a\u5408\u5bf9\u8c61\u5b9a\u4e49\u65f6\u6ca1\u6709\u521d\u59cb\u5316\u503c\u5217\u8868\uff08\u5b9e\u4f8b\u5316\u65f6\u672a\u8fdb\u884c\u521d\u59cb\u5316\uff09\uff1a \u5982\u679c\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c\u8fdb\u884c\u9ed8\u8ba4\u521d\u59cb\u5316\uff1b \u5982\u679c\u4e0d\u5b58\u5728\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u5219\u5bf9\u5e94\u6210\u5458\u4fdd\u6301\u672a\u521d\u59cb\u5316\u72b6\u6001\u3002 \u6210\u5458\u603b\u662f\u6309\u7167\u58f0\u660e\u7684\u987a\u5e8f\u521d\u59cb\u5316\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5bf9\u4e0a\u8ff0\u5404\u79cd\u53ef\u80fd\u6027\u8fdb\u884c\u4e86\u603b\u7ed3\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 struct Something { int x ; // no default initialization value (bad) int y {}; // value-initialized by default int z { 2 }; // explicit default value }; int main () { Something s1 ; // No initializer list: s1.x is uninitialized, s1.y and s1.z use defaults Something s2 { 5 , 6 , 7 }; // Explicit initializers: s2.x, s2.y, and s2.z use explicit values (no default values are used) Something s3 {}; // Missing initializers: s3.x is value initialized, s3.y and s3.z use defaults return 0 ; } \u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5c24\u5176\u8981\u6ce8\u610f\u7684\u662f s1.x \u3002\u56e0\u4e3a s1 \u6ca1\u6709\u521d\u59cb\u5316\u503c\u5217\u8868\uff0c\u800c\u4e14 x \u4e5f\u6ca1\u6709\u9ed8\u8ba4\u7684\u6210\u5458\u521d\u59cb\u5316\u503c\uff0c\u6240\u4ee5\u5b83\u5904\u4e8e\u672a\u521d\u59cb\u5316\u72b6\u6001\uff08\u8fd9\u662f\u4e0d\u5bf9\u7684\uff0c\u56e0\u4e3a\u53d8\u91cf\u4e00\u5b9a\u8981\u521d\u59cb\u5316\u624d\u597d\uff09\u3002","title":"\u590d\u4e60\uff1a\u521d\u59cb\u5316\u7684\u53ef\u80fd\u6027"},{"location":"10-7-default-member-initialization/#_4","text":"\u4e3a\u4e86\u907f\u514d\u51fa\u73b0\u672a\u521d\u59cb\u5316\u7684\u6210\u5458\uff0c\u9700\u786e\u4fdd\u6bcf\u4e2a\u6210\u5458\u90fd\u6709\u4e00\u4e2a\u9ed8\u8ba4\u503c(\u8981\u4e48\u662f\u663e\u5f0f\u9ed8\u8ba4\u503c\uff0c\u8981\u4e48\u662f\u7a7a\u7684\u82b1\u62ec\u53f7\u5bf9)\u3002\u8fd9\u6837\uff0c\u65e0\u8bba\u662f\u5426\u63d0\u4f9b\u521d\u59cb\u5316\u5217\u8868\uff0c\u6210\u5458\u90fd\u5c06\u88ab\u521d\u59cb\u5316\u4e3a\u67d0\u4e2a\u503c\u3002 \u8003\u8651\u4e0b\u9762\u4f8b\u5b50\u4e2d\u4e2d\u7684\u7ed3\u6784\u4f53\uff0c\u5b83\u7684\u6240\u6709\u6210\u5458\u90fd\u6709\u9ed8\u8ba4\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 struct Fraction { int numerator { }; // we should use { 0 } here, but for the sake of example we'll use value initialization instead int denominator { 1 }; }; int main () { Fraction f1 ; // f1.numerator value initialized to 0, f1.denominator defaulted to 1 Fraction f2 {}; // f2.numerator value initialized to 0, f2.denominator defaulted to 1 Fraction f3 { 6 }; // f3.numerator initialized to 6, f3.denominator defaulted to 1 Fraction f4 { 5 , 8 }; // f4.numerator initialized to 5, f4.denominator initialized to 8 return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u7684\u6210\u5458\u90fd\u4f1a\u88ab\u521d\u59cb\u5316\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e3a\u6240\u6709\u6210\u5458\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002\u8fd9\u786e\u4fdd\u4e86\u5373\u4f7f\u53d8\u91cf\u5b9a\u4e49\u4e0d\u5305\u542b\u521d\u59cb\u5316\u5217\u8868\uff0c\u6210\u5458\u4e5f\u4f1a\u88ab\u521d\u59cb\u5316\u3002","title":"\u5e94\u5f53\u59cb\u7ec8\u4e3a\u6210\u5458\u63d0\u4f9b\u9ed8\u8ba4\u521d\u59cb\u5316\u503c"},{"location":"10-7-default-member-initialization/#vs","text":"\u56de\u987e\u4e00\u4e0b\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u4e24\u4e2a\u521d\u59cb\u5316\u65b9\u5f0f\uff1a 1 2 Fraction f1 ; // f1.numerator value initialized to 0, f1.denominator defaulted to 1 Fraction f2 {}; // f2.numerator value initialized to 0, f2.denominator defaulted to 1 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c f1 \u662f\u9ed8\u8ba4\u521d\u59cb\u5316\uff0c\u800c f2 \u662f\u503c\u521d\u59cb\u5316\u3002\u5c3d\u7ba1\u7ed3\u679c\u662f\u4e00\u6837\u7684 ( numerator \u521d\u59cb\u5316\u4e3a0\u800c denominator \u521d\u59cb\u5316\u4e3a1)\u3002\u6240\u4ee5\uff0c\u5e94\u8be5\u4f7f\u7528\u54ea\u79cd\u65b9\u5f0f\u5462\uff1f \u503c\u521d\u59cb\u5316( f2 )\u662f\u66f4\u5b89\u5168\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u786e\u4fdd\u5373\u4f7f\u6210\u5458\u6ca1\u6709\u521d\u59cb\u5316\u503c\u4e5f\u80fd\u901a\u8fc7\u503c\u521d\u59cb\u5316\u88ab\u521d\u59cb\u5316(\u5c3d\u7ba1\u6211\u4eec\u5e94\u8be5\u4e3a\u6bcf\u4e2a\u6210\u5458\u90fd\u63d0\u4f9b\u9ed8\u8ba4\u503c\uff0c\u8be5\u65b9\u6cd5\u53ef\u4ee5\u9632\u6b62\u6211\u4eec\u9057\u6f0f)\u3002 \u503c\u521d\u59cb\u5316\u8fd8\u6709\u4e00\u4e2a\u597d\u5904\u2014\u2014\u5b83\u4e0e\u521d\u59cb\u5316\u5176\u4ed6\u7c7b\u578b\u7684\u5bf9\u8c61\u7684\u65b9\u5f0f\u4e00\u81f4\u3002\u4e00\u81f4\u6027\u6709\u52a9\u4e8e\u9632\u6b62\u9519\u8bef\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u6ca1\u6709\u4e3a\u805a\u5408\u63d0\u4f9b\u663e\u5f0f\u521d\u59cb\u5316\u503c\uff0c\u5219\u9996\u9009\u503c\u521d\u59cb\u5316(\u5e26\u7a7a\u5927\u62ec\u53f7\u521d\u59cb\u5316\u5668)\u800c\u4e0d\u662f\u9ed8\u8ba4\u521d\u59cb\u5316(\u4e0d\u5e26\u5927\u62ec\u53f7)\u3002 \u4e5f\u5c31\u662f\u8bf4\uff0c\u7a0b\u5e8f\u5458\u5bf9\u7c7b\u7c7b\u578b\u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\u800c\u4e0d\u662f\u503c\u521d\u59cb\u5316\u662f\u5f88\u5e38\u89c1\u7684\u3002\u8fd9\u90e8\u5206\u662f\u7531\u4e8e\u5386\u53f2\u539f\u56e0(\u56e0\u4e3a\u503c\u521d\u59cb\u5316\u76f4\u5230C++ 11\u624d\u5f15\u5165)\uff0c\u90e8\u5206\u662f\u56e0\u4e3a\u5728\u7c7b\u4f3c\u7684\u60c5\u51b5\u4e0b(\u5bf9\u4e8e\u975e\u805a\u5408)\uff0c\u9ed8\u8ba4\u521d\u59cb\u5316\u66f4\u9ad8\u6548(\u6211\u4eec\u5728 13.5 -\u6784\u9020\u51fd\u6570 \u4e2d\u8ba8\u8bba\u4e86\u8fd9\u79cd\u60c5\u51b5)\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u4e0d\u4f1a\u5728\u8fd9\u4e9b\u6559\u7a0b\u4e2d\u5f3a\u5236\u4f7f\u7528\u7ed3\u6784\u548c\u7c7b\u7684\u503c\u521d\u59cb\u5316\uff0c\u4f46\u6211\u4eec\u5f3a\u70c8\u5efa\u8bae\u8fd9\u6837\u505a\u3002","title":"\u9ed8\u8ba4\u521d\u59cb\u5316 vs \u805a\u5408\u7c7b\u578b\u7684\u503c\u521d\u59cb\u5316"},{"location":"10-8-struct-passing-and-miscellany/","tags":["struct"],"text":"Key Takeaway \u5047\u8bbe\u4e00\u4e2a\u5458\u5de5\u75313\u4e2a\u677e\u6563\u53d8\u91cf\u8868\u793a\uff1a 1 2 3 4 5 6 7 8 int main () { int id { 1 }; int age { 24 }; double wage { 52400.0 }; return 0 ; } \u5982\u679c\u6211\u4eec\u60f3\u628a\u8fd9\u4e2a\u96c7\u5458\u4f20\u9012\u7ed9\u4e00\u4e2a\u51fd\u6570\uff0c\u5219\u987b\u4f20\u9012\u4e09\u4e2a\u53d8\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void printEmployee ( int id , int age , double wage ) { std :: cout << \"ID: \" << id << '\\n' ; std :: cout << \"Age: \" << age << '\\n' ; std :: cout << \"Wage: \" << wage << '\\n' ; } int main () { int id { 1 }; int age { 24 }; double wage { 52400.0 }; printEmployee ( id , age , wage ); return 0 ; } \u867d\u7136\u4f20\u90123\u4e2a\u53d8\u91cf\u5e76\u4e0d\u662f\u90a3\u4e48\u7cdf\u7cd5\uff0c\u4f46\u8003\u8651\u4e00\u4e2a\u670910\u621612\u4e2a\u6210\u5458\u7684\u7ed3\u6784\u4f53\u3002\u72ec\u7acb\u5730\u4f20\u9012\u6bcf\u4e2a\u53d8\u91cf\u65e2\u8017\u65f6\u53c8\u5bb9\u6613\u51fa\u9519\u3002\u6b64\u5916\uff0c\u5982\u679c\u6211\u4eec\u5411\u96c7\u5458\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u5c5e\u6027(\u4f8b\u5982\u540d\u79f0)\uff0c\u6211\u4eec\u73b0\u5728\u5fc5\u987b\u4fee\u6539\u6240\u6709\u51fd\u6570\u58f0\u660e\u3001\u5b9a\u4e49\u548c\u51fd\u6570\u8c03\u7528\uff0c\u4ee5\u63a5\u53d7\u65b0\u7684\u5f62\u53c2\u548c\u5b9e\u53c2\uff01 \u6309\u5f15\u7528\u4f20\u9012\u7ed3\u6784\u4f53 \u4f7f\u7528\u7ed3\u6784\u4f53\u800c\u4e0d\u662f\u5355\u4e2a\u53d8\u91cf\u7684\u4e00\u5927\u4f18\u70b9\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6574\u4e2a\u7ed3\u6784\u4f20\u9012\u7ed9\u9700\u8981\u4e0e\u6210\u5458\u4e00\u8d77\u5de5\u4f5c\u7684\u51fd\u6570\u3002\u7ed3\u6784\u4f53\u901a\u5e38\u901a\u8fc7(const)\u5f15\u7528\u4f20\u9012\uff0c\u4ee5\u907f\u514d\u62f7\u8d1d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include struct Employee { int id {}; int age {}; double wage {}; }; void printEmployee ( const Employee & employee ) // note pass by reference here { std :: cout << \"ID: \" << employee . id << '\\n' ; std :: cout << \"Age: \" << employee . age << '\\n' ; std :: cout << \"Wage: \" << employee . wage << '\\n' ; } int main () { Employee joe { 14 , 32 , 24.15 }; Employee frank { 15 , 28 , 18.27 }; // Print Joe's information printEmployee ( joe ); std :: cout << '\\n' ; // Print Frank's information printEmployee ( frank ); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06 Employee \u4f5c\u4e3a\u6574\u4f53\u4f20\u9012\u7ed9 printEmployee() (\u4f20\u9012\u4e86\u91cf\u8bcd\uff0c\u4e00\u6b21\u662f joe \u4e00\u6b21\u662f frank )\u3002 \u4e0a\u9762\u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 3 4 5 6 7 ID: 14 Age: 32 Wage: 24.15 ID: 15 Age: 28 Wage: 18.27 \u56e0\u4e3a\u4f20\u9012\u7684\u662f\u6574\u4e2a\u7ed3\u6784\u5bf9\u8c61(\u800c\u4e0d\u662f\u5355\u4e2a\u6210\u5458)\uff0c\u6240\u4ee5\u65e0\u8bba\u7ed3\u6784\u5bf9\u8c61\u6709\u591a\u5c11\u6210\u5458\uff0c\u6211\u4eec\u53ea\u9700\u8981\u4e00\u4e2a\u5f62\u53c2\u3002\u800c\u4e14\uff0c\u5728\u5c06\u6765\uff0c\u5982\u679c\u9700\u8981\u5411 Employee \u7ed3\u6784\u4f53\u4e2d\u6dfb\u52a0\u65b0\u6210\u5458\uff0c\u4e5f\u4e0d\u5fc5\u66f4\u6539\u51fd\u6570\u58f0\u660e\u6216\u51fd\u6570\u8c03\u7528\uff01\u65b0\u6210\u5458\u5c06\u81ea\u52a8\u88ab\u5305\u62ec\u5728\u5185\u3002 \u8fd4\u56de\u7ed3\u6784\u4f53 \u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u51fd\u6570\u9700\u8981\u8fd4\u56de\u4e09\u7ef4\u7b1b\u5361\u5c14\u7a7a\u95f4\u4e2d\u7684\u4e00\u4e2a\u70b9\u3002\u8fd9\u6837\u7684\u70b9\u67093\u4e2a\u5c5e\u6027\uff1a x \u5750\u6807\u3001 y \u5750\u6807\u548c z \u5750\u6807\u3002\u4f46\u662f\u51fd\u6570\u53ea\u80fd\u8fd4\u56de\u4e00\u4e2a\u503c\u3002\u6b64\u65f6\u5e94\u8be5\u5982\u4f55\u5c063\u4e2a\u5750\u6807\u5168\u90e8\u8fd4\u56de\u7ed9\u7528\u6237? \u6700\u5e38\u89c1\u7684\u529e\u6cd5\u662f\u8fd4\u56de\u4e00\u4e2a\u7ed3\u6784\u4f53\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include struct Point3d { double x { 0.0 }; double y { 0.0 }; double z { 0.0 }; }; Point3d getZeroPoint () { // We can create a variable and return the variable (we'll improve this below) Point3d temp { 0.0 , 0.0 , 0.0 }; return temp ; } int main () { Point3d zero { getZeroPoint () }; if ( zero . x == 0.0 && zero . y == 0.0 && zero . z == 0.0 ) std :: cout << \"The point is zero \\n \" ; else std :: cout << \"The point is not zero \\n \" ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 The point is zero \u7ed3\u6784\u4f53\u901a\u5e38 \u6309\u503c\u8fd4\u56de \uff0c\u8fd9\u6837\u5c31\u4e0d\u4f1a\u4ea7\u751f \u60ac\u5782 \u5f15\u7528\u3002 \u8fd4\u56de\u533f\u540d\u7ed3\u6784\u4f53 getZeroPoint() \u51fd\u6570\u4e2d\u521b\u5efa\u4e86\u4e00\u4e2a\u5bf9\u8c61( temp )\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u76f4\u63a5\u8fd4\u56de\uff1a 1 2 3 4 5 6 Point3d getZeroPoint () { // We can create a variable and return the variable (we'll improve this below) Point3d temp { 0.0 , 0.0 , 0.0 }; return temp ; } \u8fd9\u4e2a\u547d\u540d\u5bf9\u8c61 ( temp ) \u5bf9\u53ef\u8bfb\u6027\u5e76\u6ca1\u6709\u4ec0\u4e48\u5e2e\u52a9\u3002 \u6211\u4eec\u53ef\u4ee5\u5bf9\u51fd\u6570\u7a0d\u52a0\u4fee\u6539\uff0c\u4f7f\u5176\u8fd4\u56de\u4e00\u4e2a\u4e34\u65f6\u7684\u533f\u540d\u5bf9\u8c61\uff1a 1 2 3 4 Point3d getZeroPoint () { return Point3d { 0.0 , 0.0 , 0.0 }; // return an unnamed Point3d } \u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u521b\u5efa\u4e86\u4e00\u4e2a\u4e34\u65f6\u7684 Point3d \u5bf9\u8c61\u5e76\u5c06\u5176 \u6309\u503c\u8fd4\u56de \uff08\u62f7\u8d1d\u8fd4\u56de\uff09\u7ed9\u8c03\u7528\u8005\u3002\u8be5\u5bf9\u8c61\u4f1a\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u6ce8\u610f\u8fd9\u662f\u591a\u4e48\u7684\u7b80\u6d01(\u4e00\u884c\u76f8\u6bd4\u4e24\u884c\uff0c\u5e76\u4e14\u4e0d\u9700\u8981\u5224\u65ad temp \u662f\u5426\u4f7f\u7528\u4e86\u591a\u6b21)\u3002 \u5728\u51fd\u6570\u6709\u663e\u5f0f\u8fd4\u56de\u7c7b\u578b\u7684\u60c5\u51b5\u4e0b(\u4f8b\u5982: Point3d )\u800c\u4e0d\u662f\u4f7f\u7528\u7c7b\u578b\u6f14\u7ece(\u4e00\u4e2a auto \u8fd4\u56de\u7c7b\u578b)\uff0c\u6211\u4eec\u751a\u81f3\u53ef\u4ee5\u5728return\u8bed\u53e5\u4e2d\u7701\u7565\u7c7b\u578b: 1 2 3 4 5 6 Point3d getZeroPoint () { // We already specified the type at the function declaration // so we don't need to do so here again return { 0.0 , 0.0 , 0.0 }; // return an unnamed Point3d } \u8fd8\u8981\u6ce8\u610f\uff0c\u56e0\u4e3a\u5728\u672c\u4f8b\u4e2d\u6211\u4eec\u8fd4\u56de\u7684\u90fd\u662f\u96f6\u503c\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7a7a\u5927\u62ec\u53f7\u6765\u8fd4\u56de\u4e00\u4e2a\u503c\u521d\u59cb\u5316\u7684 Point3d \uff1a 1 2 3 4 5 Point3d getZeroPoint () { // We can use empty curly braces to value-initialize all members return {}; } \u5305\u542b\u7a0b\u5e8f\u81ea\u5b9a\u4e49\u7c7b\u578b\u6210\u5458\u7684\u7ed3\u6784\u4f53 \u5728C++\u4e2d\uff0c\u7ed3\u6784\u4f53(\u548c\u7c7b)\u7684\u6210\u5458\u53ef\u4ee5\u662f\u5176\u4ed6\u7a0b\u5e8f\u5b9a\u4e49\u7684\u7c7b\u578b\u3002\u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\u3002 \u9996\u5148\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\uff08\u5728\u5168\u5c40\u4f5c\u7528\u57df\uff09\uff0c\u7136\u540e\u5c06\u5176\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7684\u6210\u5458\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include struct Employee { int id {}; int age {}; double wage {}; }; struct Company { int numberOfEmployees {}; Employee CEO {}; // Employee is a struct within the Company struct }; int main () { Company myCompany { 7 , { 1 , 32 , 55000.0 } }; // Nested initialization list to initialize Employee std :: cout << myCompany . CEO . wage ; // print the CEO's wage } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a Employee \u7ed3\u6784\u4f53\uff0c\u7136\u540e\u5c06\u5176\u4f5c\u4e3a\u53e6\u4e00\u4e2a Company \u7ed3\u6784\u4f53\u7684\u6210\u5458\u3002 \u5f53 Company \u521d\u59cb\u5316\u65f6\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u7684 \u521d\u59cb\u5316\u503c\u5217\u8868 \u521d\u59cb\u5316 Employee \u3002\u5982\u679c\u6211\u4eec\u60f3\u77e5\u9053\u67d0\u4e2aCEO\u7684\u85aa\u6c34\u662f\u591a\u5c11\uff0c\u5219\u9700\u8981\u4f7f\u7528\u4e24\u6b21 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 \uff1a myCompany.CEO.wage; \u5176\u6b21\uff0c\u7c7b\u578b\u4e5f\u53ef\u4ee5\u88ab\u5d4c\u5957\u5b9a\u4e49\u5728\u5176\u4ed6\u7c7b\u578b\u4e2d\uff0c\u641c\u6613\u5982\u679c Employee \u53ea\u4f1a\u4f5c\u4e3a Company \u7684\u6210\u5458\u4f7f\u7528\uff0c\u5219\u53ef\u4ee5\u5c06\u5176\u5b9a\u4e49\u5728 Company \u7ed3\u6784\u4f53\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include struct Company { struct Employee // accessed via Company::Employee { int id {}; int age {}; double wage {}; }; int numberOfEmployees {}; Employee CEO {}; // Employee is a struct within the Company struct }; int main () { Company myCompany { 7 , { 1 , 32 , 55000.0 } }; // Nested initialization list to initialize Employee std :: cout << myCompany . CEO . wage ; // print the CEO's wage } \u8fd9\u79cd\u5f62\u5f0f\u901a\u5e38\u4f1a\u5728\u7c7b\u4e2d\u4f7f\u7528\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u4ecb\u7ecd 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b \u3002 \u7ed3\u6784\u4f53\u5927\u5c0f\u548c\u6570\u636e\u7ed3\u6784\u5bf9\u9f50 \u901a\u5e38\u6211\u4eec\u4f1a\u8ba4\u4e3a\u7ed3\u6784\u4f53\u7684\u5927\u5c0f\u5c31\u662f\u5176\u6240\u6709\u6210\u5458\u5927\u5c0f\u7684\u603b\u548c\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u4e00\u5b9a\uff01 \u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include struct Foo { short a {}; int b {}; double c {}; }; int main () { std :: cout << \"The size of Foo is \" << sizeof ( Foo ) << '\\n' ; return 0 ; } \u5728\u5f88\u591a\u5e73\u53f0\u4e0a\uff0cshort \u662f 2 \u5b57\u8282\u3001int \u662f 4 \u5b57\u8282\u800c double \u662f 8 \u5b57\u8282\uff0c\u6240\u4ee5\u6211\u4eec\u4f1a\u8ba4\u4e3a sizeof(Foo) \u7684\u957f\u5ea6\u4e3a 2 + 4 + 8 = 14 \u5b57\u8282\u3002\u4f46\u662f\uff0c\u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\u6253\u5370\u7ed3\u679c\u4e3a\u3002 1 The size of Foo is 16 \u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u8ba1\u7b97\u7684\u7ed3\u679c\uff0c\u53ea\u662f\u7ed3\u6784\u4f53\u6700\u5c0f\u7684\u5927\u5c0f\uff0c\u5373\u7ed3\u6784\u4f53\u7684\u5927\u5c0f\u7684\u6700\u5c0f\u503c\u4e3a\u5176\u6240\u6709\u6210\u5458\u7684\u5927\u5c0f\u7684\u603b\u548c\u3002\u4f46\u662f\u5b83\u4e5f\u53ef\u4ee5\u5360\u636e\u66f4\u591a\u7a7a\u95f4\u3002\u5904\u4e8e\u6027\u80fd\u4f18\u5316\u7684\u8003\u8651\uff0c\u7f16\u8bd1\u5668\u6709\u65f6\u4f1a\u5728\u7ed3\u6784\u4f53\u4e2d\u6dfb\u52a0\u4e00\u4e9b\u201c\u95f4\u9699\u201d\uff08\u79f0\u4e3a \u7ed3\u6784\u4f53\u586b\u5145 \uff09 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c Foo \u7ed3\u6784\u4f53\u4f1a\u88ab\u7f16\u8bd1\u5668\u63d2\u5165\u4e00\u4e2a2\u5b57\u8282\u7684\u4e0d\u53ef\u89c1\u7684\u586b\u5145\uff08\u4f4d\u4e8e\u6210\u5458a\u4e4b\u540e\uff09\uff0c\u4f7f\u5f97\u7ed3\u6784\u4f53\u7684\u5927\u5c0f\u4ece14\u53d8\u4e3a16\u3002 \u6269\u5c55\u9605\u8bfb \u7f16\u8bd1\u5668\u4e4b\u6240\u4ee5\u5bf9\u7ed3\u6784\u4f53\u8fdb\u884c\u586b\u5145\u7684\u539f\u56e0\uff0c\u8d85\u51fa\u4e86\u672c\u6559\u7a0b\u7684\u8303\u56f4\uff0c\u5982\u679c\u60a8\u6709\u5174\u8da3\u53ef\u4ee5\u9605\u8bfb\u7ef4\u57fa\u767e\u79d1\u4e0a\u5173\u4e8e \u6570\u636e\u7ed3\u6784\u5bf9\u9f50 \u7684\u5185\u5bb9\u3002\u8be5\u77e5\u8bc6\u70b9\u5c5e\u4e8e\u6269\u5c55\u9605\u8bfb\u5185\u5bb9\uff0c\u5b83\u5e76\u4e0d\u662f\u638c\u63e1\u7ed3\u6784\u4f53\u6216C++\u5fc5\u5907\u7684\u77e5\u8bc6\uff01 \u8fd9\u5b9e\u9645\u4e0a\u4f1a\u5bf9\u7ed3\u6784\u7684\u5927\u5c0f\u4ea7\u751f\u76f8\u5f53\u5927\u7684\u5f71\u54cd\uff0c\u5982\u4e0b\u9762\u7684\u7a0b\u5e8f\u6240\u793a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include struct Foo1 { short a {}; short qq {}; // note: qq is defined here int b {}; double c {}; }; struct Foo2 { short a {}; int b {}; double c {}; short qq {}; // note: qq is defined here }; int main () { std :: cout << \"The size of Foo1 is \" << sizeof ( Foo1 ) << '\\n' ; std :: cout << \"The size of Foo2 is \" << sizeof ( Foo2 ) << '\\n' ; return 0 ; } \u6ce8\u610f\uff0c\u5c3d\u7ba1 Foo1 \u548c Foo2 \u5177\u6709\u76f8\u540c\u7684\u6210\u5458\uff0c\u4f46\u662f\u5176\u4e2d\u6210\u5458 qq \u58f0\u660e\u7684\u4f4d\u7f6e\u662f\u4e0d\u540c\u7684\uff0c\u7a0b\u5e8f\u6253\u5370\uff1a 1 2 The size of Foo1 is 16 The size of Foo2 is 24 \u7ed3\u6784\u4f53\u662f\u91cd\u8981\u7684\u7a0b\u5e8f\u7ec4\u6210\u90e8\u5206 \u867d\u7136\u7ed3\u6784\u672c\u8eab\u5f88\u6709\u7528\uff0c\u4f46\u7c7b(\u7c7b\u662fC++\u548c\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u6838\u5fc3)\u76f4\u63a5\u6784\u5efa\u5728\u6211\u4eec\u8fd9\u91cc\u4ecb\u7ecd\u7684\u6982\u5ff5\u4e4b\u4e0a\u3002\u5728\u5f88\u597d\u5730\u7406\u89e3\u7ed3\u6784\u4f53(\u7279\u522b\u662f\u6570\u636e\u6210\u5458\u3001\u6210\u5458\u9009\u62e9\u548c\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316)\u4e4b\u540e\uff0c\u5b66\u4e60\u7c7b\u4e5f\u4f1a\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\u3002","title":"10.8 - \u7ed3\u6784\u4f53\u4f20\u9012\u53ca\u5176\u4ed6"},{"location":"10-8-struct-passing-and-miscellany/#_1","text":"\u4f7f\u7528\u7ed3\u6784\u4f53\u800c\u4e0d\u662f\u5355\u4e2a\u53d8\u91cf\u7684\u4e00\u5927\u4f18\u70b9\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6574\u4e2a\u7ed3\u6784\u4f20\u9012\u7ed9\u9700\u8981\u4e0e\u6210\u5458\u4e00\u8d77\u5de5\u4f5c\u7684\u51fd\u6570\u3002\u7ed3\u6784\u4f53\u901a\u5e38\u901a\u8fc7(const)\u5f15\u7528\u4f20\u9012\uff0c\u4ee5\u907f\u514d\u62f7\u8d1d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include struct Employee { int id {}; int age {}; double wage {}; }; void printEmployee ( const Employee & employee ) // note pass by reference here { std :: cout << \"ID: \" << employee . id << '\\n' ; std :: cout << \"Age: \" << employee . age << '\\n' ; std :: cout << \"Wage: \" << employee . wage << '\\n' ; } int main () { Employee joe { 14 , 32 , 24.15 }; Employee frank { 15 , 28 , 18.27 }; // Print Joe's information printEmployee ( joe ); std :: cout << '\\n' ; // Print Frank's information printEmployee ( frank ); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06 Employee \u4f5c\u4e3a\u6574\u4f53\u4f20\u9012\u7ed9 printEmployee() (\u4f20\u9012\u4e86\u91cf\u8bcd\uff0c\u4e00\u6b21\u662f joe \u4e00\u6b21\u662f frank )\u3002 \u4e0a\u9762\u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 3 4 5 6 7 ID: 14 Age: 32 Wage: 24.15 ID: 15 Age: 28 Wage: 18.27 \u56e0\u4e3a\u4f20\u9012\u7684\u662f\u6574\u4e2a\u7ed3\u6784\u5bf9\u8c61(\u800c\u4e0d\u662f\u5355\u4e2a\u6210\u5458)\uff0c\u6240\u4ee5\u65e0\u8bba\u7ed3\u6784\u5bf9\u8c61\u6709\u591a\u5c11\u6210\u5458\uff0c\u6211\u4eec\u53ea\u9700\u8981\u4e00\u4e2a\u5f62\u53c2\u3002\u800c\u4e14\uff0c\u5728\u5c06\u6765\uff0c\u5982\u679c\u9700\u8981\u5411 Employee \u7ed3\u6784\u4f53\u4e2d\u6dfb\u52a0\u65b0\u6210\u5458\uff0c\u4e5f\u4e0d\u5fc5\u66f4\u6539\u51fd\u6570\u58f0\u660e\u6216\u51fd\u6570\u8c03\u7528\uff01\u65b0\u6210\u5458\u5c06\u81ea\u52a8\u88ab\u5305\u62ec\u5728\u5185\u3002","title":"\u6309\u5f15\u7528\u4f20\u9012\u7ed3\u6784\u4f53"},{"location":"10-8-struct-passing-and-miscellany/#_2","text":"\u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u51fd\u6570\u9700\u8981\u8fd4\u56de\u4e09\u7ef4\u7b1b\u5361\u5c14\u7a7a\u95f4\u4e2d\u7684\u4e00\u4e2a\u70b9\u3002\u8fd9\u6837\u7684\u70b9\u67093\u4e2a\u5c5e\u6027\uff1a x \u5750\u6807\u3001 y \u5750\u6807\u548c z \u5750\u6807\u3002\u4f46\u662f\u51fd\u6570\u53ea\u80fd\u8fd4\u56de\u4e00\u4e2a\u503c\u3002\u6b64\u65f6\u5e94\u8be5\u5982\u4f55\u5c063\u4e2a\u5750\u6807\u5168\u90e8\u8fd4\u56de\u7ed9\u7528\u6237? \u6700\u5e38\u89c1\u7684\u529e\u6cd5\u662f\u8fd4\u56de\u4e00\u4e2a\u7ed3\u6784\u4f53\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include struct Point3d { double x { 0.0 }; double y { 0.0 }; double z { 0.0 }; }; Point3d getZeroPoint () { // We can create a variable and return the variable (we'll improve this below) Point3d temp { 0.0 , 0.0 , 0.0 }; return temp ; } int main () { Point3d zero { getZeroPoint () }; if ( zero . x == 0.0 && zero . y == 0.0 && zero . z == 0.0 ) std :: cout << \"The point is zero \\n \" ; else std :: cout << \"The point is not zero \\n \" ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 The point is zero \u7ed3\u6784\u4f53\u901a\u5e38 \u6309\u503c\u8fd4\u56de \uff0c\u8fd9\u6837\u5c31\u4e0d\u4f1a\u4ea7\u751f \u60ac\u5782 \u5f15\u7528\u3002","title":"\u8fd4\u56de\u7ed3\u6784\u4f53"},{"location":"10-8-struct-passing-and-miscellany/#_3","text":"getZeroPoint() \u51fd\u6570\u4e2d\u521b\u5efa\u4e86\u4e00\u4e2a\u5bf9\u8c61( temp )\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u76f4\u63a5\u8fd4\u56de\uff1a 1 2 3 4 5 6 Point3d getZeroPoint () { // We can create a variable and return the variable (we'll improve this below) Point3d temp { 0.0 , 0.0 , 0.0 }; return temp ; } \u8fd9\u4e2a\u547d\u540d\u5bf9\u8c61 ( temp ) \u5bf9\u53ef\u8bfb\u6027\u5e76\u6ca1\u6709\u4ec0\u4e48\u5e2e\u52a9\u3002 \u6211\u4eec\u53ef\u4ee5\u5bf9\u51fd\u6570\u7a0d\u52a0\u4fee\u6539\uff0c\u4f7f\u5176\u8fd4\u56de\u4e00\u4e2a\u4e34\u65f6\u7684\u533f\u540d\u5bf9\u8c61\uff1a 1 2 3 4 Point3d getZeroPoint () { return Point3d { 0.0 , 0.0 , 0.0 }; // return an unnamed Point3d } \u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u521b\u5efa\u4e86\u4e00\u4e2a\u4e34\u65f6\u7684 Point3d \u5bf9\u8c61\u5e76\u5c06\u5176 \u6309\u503c\u8fd4\u56de \uff08\u62f7\u8d1d\u8fd4\u56de\uff09\u7ed9\u8c03\u7528\u8005\u3002\u8be5\u5bf9\u8c61\u4f1a\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u6ce8\u610f\u8fd9\u662f\u591a\u4e48\u7684\u7b80\u6d01(\u4e00\u884c\u76f8\u6bd4\u4e24\u884c\uff0c\u5e76\u4e14\u4e0d\u9700\u8981\u5224\u65ad temp \u662f\u5426\u4f7f\u7528\u4e86\u591a\u6b21)\u3002 \u5728\u51fd\u6570\u6709\u663e\u5f0f\u8fd4\u56de\u7c7b\u578b\u7684\u60c5\u51b5\u4e0b(\u4f8b\u5982: Point3d )\u800c\u4e0d\u662f\u4f7f\u7528\u7c7b\u578b\u6f14\u7ece(\u4e00\u4e2a auto \u8fd4\u56de\u7c7b\u578b)\uff0c\u6211\u4eec\u751a\u81f3\u53ef\u4ee5\u5728return\u8bed\u53e5\u4e2d\u7701\u7565\u7c7b\u578b: 1 2 3 4 5 6 Point3d getZeroPoint () { // We already specified the type at the function declaration // so we don't need to do so here again return { 0.0 , 0.0 , 0.0 }; // return an unnamed Point3d } \u8fd8\u8981\u6ce8\u610f\uff0c\u56e0\u4e3a\u5728\u672c\u4f8b\u4e2d\u6211\u4eec\u8fd4\u56de\u7684\u90fd\u662f\u96f6\u503c\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7a7a\u5927\u62ec\u53f7\u6765\u8fd4\u56de\u4e00\u4e2a\u503c\u521d\u59cb\u5316\u7684 Point3d \uff1a 1 2 3 4 5 Point3d getZeroPoint () { // We can use empty curly braces to value-initialize all members return {}; }","title":"\u8fd4\u56de\u533f\u540d\u7ed3\u6784\u4f53"},{"location":"10-8-struct-passing-and-miscellany/#_4","text":"\u5728C++\u4e2d\uff0c\u7ed3\u6784\u4f53(\u548c\u7c7b)\u7684\u6210\u5458\u53ef\u4ee5\u662f\u5176\u4ed6\u7a0b\u5e8f\u5b9a\u4e49\u7684\u7c7b\u578b\u3002\u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\u3002 \u9996\u5148\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\uff08\u5728\u5168\u5c40\u4f5c\u7528\u57df\uff09\uff0c\u7136\u540e\u5c06\u5176\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7684\u6210\u5458\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include struct Employee { int id {}; int age {}; double wage {}; }; struct Company { int numberOfEmployees {}; Employee CEO {}; // Employee is a struct within the Company struct }; int main () { Company myCompany { 7 , { 1 , 32 , 55000.0 } }; // Nested initialization list to initialize Employee std :: cout << myCompany . CEO . wage ; // print the CEO's wage } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a Employee \u7ed3\u6784\u4f53\uff0c\u7136\u540e\u5c06\u5176\u4f5c\u4e3a\u53e6\u4e00\u4e2a Company \u7ed3\u6784\u4f53\u7684\u6210\u5458\u3002 \u5f53 Company \u521d\u59cb\u5316\u65f6\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u7684 \u521d\u59cb\u5316\u503c\u5217\u8868 \u521d\u59cb\u5316 Employee \u3002\u5982\u679c\u6211\u4eec\u60f3\u77e5\u9053\u67d0\u4e2aCEO\u7684\u85aa\u6c34\u662f\u591a\u5c11\uff0c\u5219\u9700\u8981\u4f7f\u7528\u4e24\u6b21 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 \uff1a myCompany.CEO.wage; \u5176\u6b21\uff0c\u7c7b\u578b\u4e5f\u53ef\u4ee5\u88ab\u5d4c\u5957\u5b9a\u4e49\u5728\u5176\u4ed6\u7c7b\u578b\u4e2d\uff0c\u641c\u6613\u5982\u679c Employee \u53ea\u4f1a\u4f5c\u4e3a Company \u7684\u6210\u5458\u4f7f\u7528\uff0c\u5219\u53ef\u4ee5\u5c06\u5176\u5b9a\u4e49\u5728 Company \u7ed3\u6784\u4f53\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include struct Company { struct Employee // accessed via Company::Employee { int id {}; int age {}; double wage {}; }; int numberOfEmployees {}; Employee CEO {}; // Employee is a struct within the Company struct }; int main () { Company myCompany { 7 , { 1 , 32 , 55000.0 } }; // Nested initialization list to initialize Employee std :: cout << myCompany . CEO . wage ; // print the CEO's wage } \u8fd9\u79cd\u5f62\u5f0f\u901a\u5e38\u4f1a\u5728\u7c7b\u4e2d\u4f7f\u7528\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u4ecb\u7ecd 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b \u3002","title":"\u5305\u542b\u7a0b\u5e8f\u81ea\u5b9a\u4e49\u7c7b\u578b\u6210\u5458\u7684\u7ed3\u6784\u4f53"},{"location":"10-8-struct-passing-and-miscellany/#_5","text":"\u901a\u5e38\u6211\u4eec\u4f1a\u8ba4\u4e3a\u7ed3\u6784\u4f53\u7684\u5927\u5c0f\u5c31\u662f\u5176\u6240\u6709\u6210\u5458\u5927\u5c0f\u7684\u603b\u548c\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u4e00\u5b9a\uff01 \u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include struct Foo { short a {}; int b {}; double c {}; }; int main () { std :: cout << \"The size of Foo is \" << sizeof ( Foo ) << '\\n' ; return 0 ; } \u5728\u5f88\u591a\u5e73\u53f0\u4e0a\uff0cshort \u662f 2 \u5b57\u8282\u3001int \u662f 4 \u5b57\u8282\u800c double \u662f 8 \u5b57\u8282\uff0c\u6240\u4ee5\u6211\u4eec\u4f1a\u8ba4\u4e3a sizeof(Foo) \u7684\u957f\u5ea6\u4e3a 2 + 4 + 8 = 14 \u5b57\u8282\u3002\u4f46\u662f\uff0c\u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\u6253\u5370\u7ed3\u679c\u4e3a\u3002 1 The size of Foo is 16 \u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u8ba1\u7b97\u7684\u7ed3\u679c\uff0c\u53ea\u662f\u7ed3\u6784\u4f53\u6700\u5c0f\u7684\u5927\u5c0f\uff0c\u5373\u7ed3\u6784\u4f53\u7684\u5927\u5c0f\u7684\u6700\u5c0f\u503c\u4e3a\u5176\u6240\u6709\u6210\u5458\u7684\u5927\u5c0f\u7684\u603b\u548c\u3002\u4f46\u662f\u5b83\u4e5f\u53ef\u4ee5\u5360\u636e\u66f4\u591a\u7a7a\u95f4\u3002\u5904\u4e8e\u6027\u80fd\u4f18\u5316\u7684\u8003\u8651\uff0c\u7f16\u8bd1\u5668\u6709\u65f6\u4f1a\u5728\u7ed3\u6784\u4f53\u4e2d\u6dfb\u52a0\u4e00\u4e9b\u201c\u95f4\u9699\u201d\uff08\u79f0\u4e3a \u7ed3\u6784\u4f53\u586b\u5145 \uff09 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c Foo \u7ed3\u6784\u4f53\u4f1a\u88ab\u7f16\u8bd1\u5668\u63d2\u5165\u4e00\u4e2a2\u5b57\u8282\u7684\u4e0d\u53ef\u89c1\u7684\u586b\u5145\uff08\u4f4d\u4e8e\u6210\u5458a\u4e4b\u540e\uff09\uff0c\u4f7f\u5f97\u7ed3\u6784\u4f53\u7684\u5927\u5c0f\u4ece14\u53d8\u4e3a16\u3002 \u6269\u5c55\u9605\u8bfb \u7f16\u8bd1\u5668\u4e4b\u6240\u4ee5\u5bf9\u7ed3\u6784\u4f53\u8fdb\u884c\u586b\u5145\u7684\u539f\u56e0\uff0c\u8d85\u51fa\u4e86\u672c\u6559\u7a0b\u7684\u8303\u56f4\uff0c\u5982\u679c\u60a8\u6709\u5174\u8da3\u53ef\u4ee5\u9605\u8bfb\u7ef4\u57fa\u767e\u79d1\u4e0a\u5173\u4e8e \u6570\u636e\u7ed3\u6784\u5bf9\u9f50 \u7684\u5185\u5bb9\u3002\u8be5\u77e5\u8bc6\u70b9\u5c5e\u4e8e\u6269\u5c55\u9605\u8bfb\u5185\u5bb9\uff0c\u5b83\u5e76\u4e0d\u662f\u638c\u63e1\u7ed3\u6784\u4f53\u6216C++\u5fc5\u5907\u7684\u77e5\u8bc6\uff01 \u8fd9\u5b9e\u9645\u4e0a\u4f1a\u5bf9\u7ed3\u6784\u7684\u5927\u5c0f\u4ea7\u751f\u76f8\u5f53\u5927\u7684\u5f71\u54cd\uff0c\u5982\u4e0b\u9762\u7684\u7a0b\u5e8f\u6240\u793a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include struct Foo1 { short a {}; short qq {}; // note: qq is defined here int b {}; double c {}; }; struct Foo2 { short a {}; int b {}; double c {}; short qq {}; // note: qq is defined here }; int main () { std :: cout << \"The size of Foo1 is \" << sizeof ( Foo1 ) << '\\n' ; std :: cout << \"The size of Foo2 is \" << sizeof ( Foo2 ) << '\\n' ; return 0 ; } \u6ce8\u610f\uff0c\u5c3d\u7ba1 Foo1 \u548c Foo2 \u5177\u6709\u76f8\u540c\u7684\u6210\u5458\uff0c\u4f46\u662f\u5176\u4e2d\u6210\u5458 qq \u58f0\u660e\u7684\u4f4d\u7f6e\u662f\u4e0d\u540c\u7684\uff0c\u7a0b\u5e8f\u6253\u5370\uff1a 1 2 The size of Foo1 is 16 The size of Foo2 is 24","title":"\u7ed3\u6784\u4f53\u5927\u5c0f\u548c\u6570\u636e\u7ed3\u6784\u5bf9\u9f50"},{"location":"10-8-struct-passing-and-miscellany/#_6","text":"\u867d\u7136\u7ed3\u6784\u672c\u8eab\u5f88\u6709\u7528\uff0c\u4f46\u7c7b(\u7c7b\u662fC++\u548c\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u6838\u5fc3)\u76f4\u63a5\u6784\u5efa\u5728\u6211\u4eec\u8fd9\u91cc\u4ecb\u7ecd\u7684\u6982\u5ff5\u4e4b\u4e0a\u3002\u5728\u5f88\u597d\u5730\u7406\u89e3\u7ed3\u6784\u4f53(\u7279\u522b\u662f\u6570\u636e\u6210\u5458\u3001\u6210\u5458\u9009\u62e9\u548c\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316)\u4e4b\u540e\uff0c\u5b66\u4e60\u7c7b\u4e5f\u4f1a\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\u3002","title":"\u7ed3\u6784\u4f53\u662f\u91cd\u8981\u7684\u7a0b\u5e8f\u7ec4\u6210\u90e8\u5206"},{"location":"10-9-member-selection-with-pointers-and-references/","tags":["struct"],"text":"Key Takeaway \u7ed3\u6784\u4f53\u548c\u7ed3\u6784\u4f53\u5f15\u7528\u7684\u6210\u5458\u9009\u62e9 \u5728 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 \u4e2d\u6211\u4eec\u63d0\u5230\uff0c\u8bbf\u95ee\u7ed3\u6784\u4f53\u7684\u6210\u5458\u53ef\u4ee5\u4f7f\u7528 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 34 , 65000.0 }; // Use member selection operator (.) to select a member from struct object ++ joe . age ; // Joe had a birthday joe . wage = 68000.0 ; // Joe got a promotion return 0 ; } \u56e0\u4e3a\u5bf9\u8c61\u7684\u5f15\u7528\u5176\u5b9e\u5c31\u662f\u5bf9\u8c61\u672c\u8eab\uff0c\u6240\u4ee5\u6211\u4eec\u4e5f\u53ef\u4ee5\u5bf9\u5f15\u7528\u4f7f\u7528\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26\u6765\u8bbf\u95ee\u7ed3\u6784\u4f53\u6210\u5458\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include struct Employee { int id {}; int age {}; double wage {}; }; void printEmployee ( const Employee & e ) { // Use member selection opeartor (.) to select member from reference to struct std :: cout << \"Id: \" << e . id << '\\n' ; std :: cout << \" Age: \" << e . age << '\\n' ; std :: cout << \" Wage: \" << e . wage << '\\n' ; } int main () { Employee joe { 1 , 34 , 65000.0 }; ++ joe . age ; joe . wage = 68000.0 ; printEmployee ( joe ); return 0 ; } \u7ed3\u6784\u4f53\u6307\u9488\u7684\u6210\u5458\u9009\u62e9 \u4e0d\u8fc7\uff0c\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26\u5e76\u4e0d\u80fd\u88ab\u7528\u4e8e\u6307\u5411\u7ed3\u6784\u4f53\u7684\u6307\u9488\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 34 , 65000.0 }; ++ joe . age ; joe . wage = 68000.0 ; Employee * ptr { & joe }; std :: cout << ptr . id << '\\n' ; // Compile error: can't use operator. with pointers return 0 ; } \u5bf9\u4e8e\u666e\u901a\u53d8\u91cf\u548c\u5f15\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5b83\u3002\u800c\u5bf9\u4e8e\u4fdd\u5b58\u7740\u5730\u5740\u7684\u6307\u9488\u6765\u8bf4\uff0c\u6211\u4eec\u5fc5\u987b\u5728\u8bbf\u95ee\u5bf9\u8c61\u524d\uff0c\u9996\u5148\u5bf9\u5730\u5740\u89e3\u5f15\u7528\u4ee5\u83b7\u5f97\u8be5\u5bf9\u8c61\u3002 \u6240\u4ee5\uff0c\u901a\u8fc7\u7ed3\u6784\u4f53\u8bbf\u95ee\u6210\u5458\u7684\u4e00\u79cd\u65b9\u5f0f\u5e94\u8be5\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 34 , 65000.0 }; ++ joe . age ; joe . wage = 68000.0 ; Employee * ptr { & joe }; std :: cout << ( * ptr ). id << '\\n' ; // Not great but works: First dereference ptr, then use member selection return 0 ; } \u4f46\u662f\uff0c\u8fd9\u5e76\u4e0d\u4f18\u96c5\uff0c\u5c24\u5176\u662f\u8fd9\u91cc\u5fc5\u987b\u4f7f\u7528\u62ec\u53f7\u4ee5\u786e\u4fdd\u6b63\u786e\u7684\u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u3002 \u4e3a\u4e86\u80fd\u591f\u63d0\u4f9b\u4e00\u4e2a\u66f4\u7b80\u6d01\u7684\u8bed\u6cd5\uff0cC++\u63d0\u4f9b\u4e86\u6307\u9488\u8fd0\u7b97\u7b26\u8fdb\u884c\u6210\u5458\u9009\u62e9\uff08\u6709\u65f6\u5019\u79f0\u4e3a\u7bad\u5934\u8fd0\u7b97\u7b26\uff09\u4f7f\u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u5bf9\u8c61\u6307\u9488\u6765\u8bbf\u95ee\u5176\u6210\u5458\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 34 , 65000.0 }; ++ joe . age ; joe . wage = 68000.0 ; Employee * ptr { & joe }; std :: cout << ptr -> id << '\\n' ; // Better: use -> to select member from pointer to object return 0 ; } \u6307\u9488\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26\u548c\u666e\u901a\u7684\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26\u7528\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u662f\u5b83\u4f1a\u9996\u5148\u8fdb\u884c\u9690\u5f0f\u7684\u89e3\u5f15\u7528\u3002\u8be5\u7bad\u5934\u8fd0\u7b97\u7b26\u4e0d\u4ec5\u66f4\u5bb9\u6613\u5199\uff0c\u4e5f\u66f4\u4e0d\u5bb9\u6613\u51fa\u9519\uff0c\u56e0\u4e3a\u5b83\u4f1a\u9690\u5f0f\u8fdb\u884c\u89e3\u5f15\u7528\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u9700\u8981\u64cd\u5fc3\u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u7684\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u5728\u901a\u8fc7\u7ed3\u6784\u4f53\u6307\u9488\u8bbf\u95ee\u5176\u6210\u5458\u65f6\uff0c\u5e94\u8be5\u603b\u662f\u4f7f\u7528\u7bad\u5934\u8fd0\u7b97\u7b26\u6765\u5b8c\u6210\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u4f7f\u7528\u6307\u9488\u8bbf\u95ee\u6210\u5458\u7684\u503c\u65f6\uff0c\u4f7f\u7528 -> ;\u800c\u4e0d\u662f . \u540c\u65f6\u4f7f\u7528\u6307\u9488\u548c\u975e\u6307\u9488\u6210\u5458\u8bbf\u95ee \u6210\u5458\u9009\u62e9\u64cd\u4f5c\u7b26\u59cb\u7ec8\u5e94\u7528\u4e8e\u5f53\u524d\u9009\u5b9a\u7684\u53d8\u91cf\u3002\u5982\u679c\u6307\u9488\u548c\u666e\u901a\u6210\u5458\u53d8\u91cf\u6df7\u5408\u4f7f\u7528\uff0c\u5219\u6210\u5458\u9009\u62e9 . \u548c -> \u662f\u6309\u987a\u5e8f\u4f7f\u7528\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include struct Paw { int claws {}; }; struct Animal { std :: string name {}; Paw paw {}; }; int main () { Animal puma { \"Puma\" , { 5 } }; Animal * ptr { & puma }; // ptr is a pointer, use -> // paw is not a pointer, use . std :: cout << ( ptr -> paw ). claws << '\\n' ; return 0 ; } \u6ce8\u610f (ptr->paw).claws \u4e2d\u7684\u62ec\u53f7\u662f\u53ef\u9009\u7684\uff0c\u56e0\u4e3a operator-> and operator. \u90fd\u662f\u4ece\u5de6\u5411\u53f3\u8fdb\u884c\u8fd0\u7b97\u7684\uff0c\u4f46\u662f\u6dfb\u52a0\u62ec\u53f7\u53ef\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002","title":"10.9 - \u57fa\u4e8e\u6307\u9488\u548c\u5f15\u7528\u7684\u6210\u5458\u9009\u62e9"},{"location":"10-9-member-selection-with-pointers-and-references/#_1","text":"\u5728 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 \u4e2d\u6211\u4eec\u63d0\u5230\uff0c\u8bbf\u95ee\u7ed3\u6784\u4f53\u7684\u6210\u5458\u53ef\u4ee5\u4f7f\u7528 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 34 , 65000.0 }; // Use member selection operator (.) to select a member from struct object ++ joe . age ; // Joe had a birthday joe . wage = 68000.0 ; // Joe got a promotion return 0 ; } \u56e0\u4e3a\u5bf9\u8c61\u7684\u5f15\u7528\u5176\u5b9e\u5c31\u662f\u5bf9\u8c61\u672c\u8eab\uff0c\u6240\u4ee5\u6211\u4eec\u4e5f\u53ef\u4ee5\u5bf9\u5f15\u7528\u4f7f\u7528\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26\u6765\u8bbf\u95ee\u7ed3\u6784\u4f53\u6210\u5458\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include struct Employee { int id {}; int age {}; double wage {}; }; void printEmployee ( const Employee & e ) { // Use member selection opeartor (.) to select member from reference to struct std :: cout << \"Id: \" << e . id << '\\n' ; std :: cout << \" Age: \" << e . age << '\\n' ; std :: cout << \" Wage: \" << e . wage << '\\n' ; } int main () { Employee joe { 1 , 34 , 65000.0 }; ++ joe . age ; joe . wage = 68000.0 ; printEmployee ( joe ); return 0 ; }","title":"\u7ed3\u6784\u4f53\u548c\u7ed3\u6784\u4f53\u5f15\u7528\u7684\u6210\u5458\u9009\u62e9"},{"location":"10-9-member-selection-with-pointers-and-references/#_2","text":"\u4e0d\u8fc7\uff0c\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26\u5e76\u4e0d\u80fd\u88ab\u7528\u4e8e\u6307\u5411\u7ed3\u6784\u4f53\u7684\u6307\u9488\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 34 , 65000.0 }; ++ joe . age ; joe . wage = 68000.0 ; Employee * ptr { & joe }; std :: cout << ptr . id << '\\n' ; // Compile error: can't use operator. with pointers return 0 ; } \u5bf9\u4e8e\u666e\u901a\u53d8\u91cf\u548c\u5f15\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5b83\u3002\u800c\u5bf9\u4e8e\u4fdd\u5b58\u7740\u5730\u5740\u7684\u6307\u9488\u6765\u8bf4\uff0c\u6211\u4eec\u5fc5\u987b\u5728\u8bbf\u95ee\u5bf9\u8c61\u524d\uff0c\u9996\u5148\u5bf9\u5730\u5740\u89e3\u5f15\u7528\u4ee5\u83b7\u5f97\u8be5\u5bf9\u8c61\u3002 \u6240\u4ee5\uff0c\u901a\u8fc7\u7ed3\u6784\u4f53\u8bbf\u95ee\u6210\u5458\u7684\u4e00\u79cd\u65b9\u5f0f\u5e94\u8be5\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 34 , 65000.0 }; ++ joe . age ; joe . wage = 68000.0 ; Employee * ptr { & joe }; std :: cout << ( * ptr ). id << '\\n' ; // Not great but works: First dereference ptr, then use member selection return 0 ; } \u4f46\u662f\uff0c\u8fd9\u5e76\u4e0d\u4f18\u96c5\uff0c\u5c24\u5176\u662f\u8fd9\u91cc\u5fc5\u987b\u4f7f\u7528\u62ec\u53f7\u4ee5\u786e\u4fdd\u6b63\u786e\u7684\u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u3002 \u4e3a\u4e86\u80fd\u591f\u63d0\u4f9b\u4e00\u4e2a\u66f4\u7b80\u6d01\u7684\u8bed\u6cd5\uff0cC++\u63d0\u4f9b\u4e86\u6307\u9488\u8fd0\u7b97\u7b26\u8fdb\u884c\u6210\u5458\u9009\u62e9\uff08\u6709\u65f6\u5019\u79f0\u4e3a\u7bad\u5934\u8fd0\u7b97\u7b26\uff09\u4f7f\u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u5bf9\u8c61\u6307\u9488\u6765\u8bbf\u95ee\u5176\u6210\u5458\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee joe { 1 , 34 , 65000.0 }; ++ joe . age ; joe . wage = 68000.0 ; Employee * ptr { & joe }; std :: cout << ptr -> id << '\\n' ; // Better: use -> to select member from pointer to object return 0 ; } \u6307\u9488\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26\u548c\u666e\u901a\u7684\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26\u7528\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u662f\u5b83\u4f1a\u9996\u5148\u8fdb\u884c\u9690\u5f0f\u7684\u89e3\u5f15\u7528\u3002\u8be5\u7bad\u5934\u8fd0\u7b97\u7b26\u4e0d\u4ec5\u66f4\u5bb9\u6613\u5199\uff0c\u4e5f\u66f4\u4e0d\u5bb9\u6613\u51fa\u9519\uff0c\u56e0\u4e3a\u5b83\u4f1a\u9690\u5f0f\u8fdb\u884c\u89e3\u5f15\u7528\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u9700\u8981\u64cd\u5fc3\u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u7684\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u5728\u901a\u8fc7\u7ed3\u6784\u4f53\u6307\u9488\u8bbf\u95ee\u5176\u6210\u5458\u65f6\uff0c\u5e94\u8be5\u603b\u662f\u4f7f\u7528\u7bad\u5934\u8fd0\u7b97\u7b26\u6765\u5b8c\u6210\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u4f7f\u7528\u6307\u9488\u8bbf\u95ee\u6210\u5458\u7684\u503c\u65f6\uff0c\u4f7f\u7528 -> ;\u800c\u4e0d\u662f .","title":"\u7ed3\u6784\u4f53\u6307\u9488\u7684\u6210\u5458\u9009\u62e9"},{"location":"10-9-member-selection-with-pointers-and-references/#_3","text":"\u6210\u5458\u9009\u62e9\u64cd\u4f5c\u7b26\u59cb\u7ec8\u5e94\u7528\u4e8e\u5f53\u524d\u9009\u5b9a\u7684\u53d8\u91cf\u3002\u5982\u679c\u6307\u9488\u548c\u666e\u901a\u6210\u5458\u53d8\u91cf\u6df7\u5408\u4f7f\u7528\uff0c\u5219\u6210\u5458\u9009\u62e9 . \u548c -> \u662f\u6309\u987a\u5e8f\u4f7f\u7528\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include struct Paw { int claws {}; }; struct Animal { std :: string name {}; Paw paw {}; }; int main () { Animal puma { \"Puma\" , { 5 } }; Animal * ptr { & puma }; // ptr is a pointer, use -> // paw is not a pointer, use . std :: cout << ( ptr -> paw ). claws << '\\n' ; return 0 ; } \u6ce8\u610f (ptr->paw).claws \u4e2d\u7684\u62ec\u53f7\u662f\u53ef\u9009\u7684\uff0c\u56e0\u4e3a operator-> and operator. \u90fd\u662f\u4ece\u5de6\u5411\u53f3\u8fdb\u884c\u8fd0\u7b97\u7684\uff0c\u4f46\u662f\u6dfb\u52a0\u62ec\u53f7\u53ef\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002","title":"\u540c\u65f6\u4f7f\u7528\u6307\u9488\u548c\u975e\u6307\u9488\u6210\u5458\u8bbf\u95ee"},{"location":"10-x-Chapter-10-comprehensive-quiz/","tags":["summary"],"text":"10.1 - \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7b80\u4ecb \u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u662f\u4e00\u79cd\u81ea\u5b9a\u4e49\u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u521b\u5efa\u5b83\u7528\u4e8e\u67d0\u4e9b\u76ee\u7684\u3002\u57fa\u4e8e\u679a\u4e3e\u7c7b\u578b\u548cclass\u7c7b\u578b\uff08\u5305\u62ec\u7ed3\u6784\u4f53\u3001\u7c7b\u548c\u8054\u5408\uff09\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u9700\u8981\u7684\u81ea\u5b9a\u4e49\u7c7b\u578b\u3002\u81ea\u5b9a\u4e49\u7c7b\u578b\u5728\u4f7f\u7528\u524d\u5fc5\u987b\u5148\u5b9a\u4e49\uff0c\u5b9a\u4e49\u5b83\u7684\u8fc7\u7a0b\u5219\u79f0\u4e3a \u7c7b\u578b\u5b9a\u4e49 \u3002\u7c7b\u578b\u5b9a\u4e49\u4e0d\u9700\u8981\u9075\u5faa \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u3002 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b 10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa \u679a\u4e3e\uff08\u79f0\u4e3a\u679a\u4e3e\u7c7b\u578b\u6216enum\uff09\u662f\u4e00\u79cd\u590d\u5408\u6570\u636e\u7c7b\u578b\uff0c\u5b83\u7684\u6bcf\u4e2a\u53ef\u80fd\u503c\u90fd\u88ab\u5b9a\u4e49\u4e3a \u7b26\u53f7\u5e38\u91cf \uff08\u79f0\u4e3a\u679a\u4e3e\u503c\uff09\u3002\u679a\u4e3e\u503c\u662f\u4e00\u79cd\u786e\u5207\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4e5f\u5c31\u662f\u8bf4\u7f16\u8bd1\u5668\u53ef\u4ee5\u533a\u5206\u5b83\u548c\u5176\u4ed6\u7c7b\u578b\uff08\u4e0d\u50cf \u7c7b\u578b\u522b\u540d \u90a3\u6837\uff09\u3002 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u4f1a\u5c06\u81ea\u5df1\u7684\u679a\u4e3e\u503c\u540d\u79f0\u5bfc\u5165\u5230\u4e0e\u81ea\u5df1\u76f8\u540c\u7684\u4f5c\u7528\u57df\u4e2d(\u800c\u4e0d\u662f\u50cf\u547d\u540d\u7a7a\u95f4\u90a3\u6837\u521b\u5efa\u65b0\u7684\u4f5c\u7528\u57df\u533a\u57df)\u3002\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u4f1a\u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a\u6574\u578b\u6570\u3002 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e \u4e0e\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7684\u5de5\u4f5c\u539f\u7406\u7c7b\u4f3c\uff0c\u4f46\u5b83\u4eec\u662f\u5f3a\u7c7b\u578b\u7684(\u5b83\u4eec\u4e0d\u4f1a\u9690\u5f0f\u8f6c\u6362\u4e3a\u6574\u6570)\u548c\u5f3a\u4f5c\u7528\u57df\u7684(\u679a\u4e3e\u6570\u53ea\u653e\u5728\u679a\u4e3e\u7684\u4f5c\u7528\u57df\u533a\u57df\u4e2d)\u3002 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 \u7ed3\u6784\u4f53\u662f\u4e00\u79cd\u81ea\u5b9a\u4e49\u7c7b\u578b\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u5c06\u591a\u4e2a\u53d8\u91cf\u6253\u5305\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u7c7b\u578b\u3002\u53d8\u91cf\u662f\u7ed3\u6784\u4f53\uff08\u6216\u7c7b\uff09\u7684\u4e00\u90e8\u5206\uff0c\u79f0\u4e3a\u6570\u636e\u6210\u5458\u6216 \u6210\u5458\u53d8\u91cf \u3002\u8bbf\u95ee\u6210\u5458\u51fd\u6570\u9700\u8981\u5728\u7ed3\u6784\u4f53\u53d8\u91cf\u540d\uff08\u666e\u901a\u7ed3\u6784\u4f53\u6216\u7ed3\u6784\u4f53\u5f15\u7528\uff09\u548c\u6210\u5458\u540d\u4e4b\u95f4\u4f7f\u7528 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 \uff08 operator . \uff09\uff0c\u6216\u8005\u4f7f\u7528\u6307\u9488\u8fd0\u7b97\u7b26( operator-> ) \uff08\u5bf9\u4e8e\u7ed3\u6784\u4f53\u6307\u9488\u800c\u8a00\uff09\u3002 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 \u5728\u4e00\u822c\u7f16\u7a0b\u9886\u57df\uff0c \u805a\u5408\u6570\u636e\u7c7b\u578b \uff08\u901a\u5e38\u79f0\u4e3a\u805a\u5408\uff09\u662f\u6307\u4efb\u4f55\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u6570\u636e\u6210\u5458\u7684\u7c7b\u578b\u3002\u5728 C++ \u4e2d\uff0c\u53ea\u5305\u542b\u6570\u636e\u6210\u5458\u7684\u6570\u7ec4\u548c\u7ed3\u6784\u4f53\u662f\u805a\u5408\u3002 \u805a\u5408\u4f7f\u7528\u4e00\u79cd\u88ab\u79f0\u4e3a \u805a\u5408\u521d\u59cb\u5316 \u7684\u521d\u59cb\u5316\u5f62\u5f0f\uff0c\u4f7f\u7528\u805a\u5408\u521d\u59cb\u5316\u53ef\u4ee5\u76f4\u63a5\u521d\u59cb\u5316\u805a\u5408\u7684\u6210\u5458\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u9700\u8981\u63d0\u4f9b\u4e00\u4e2a\u521d\u59cb\u5316\u5217\u8868\u4f5c\u4e3a\u521d\u59cb\u5316\u503c\uff0c\u800c\u6240\u8c13\u521d\u59cb\u5316\u5217\u8868\u5c31\u662f\u4e00\u4e2a\u4ee5\u9017\u53f7\u5206\u5272\u503c\u7684\u5217\u8868\u3002\u805a\u5408\u521d\u59cb\u5316\u4f7f\u7528 \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \u65b9\u5f0f\uff0c\u5373\u7ed3\u6784\u4f53\u7684\u6bcf\u4e2a\u6210\u5458\u4f9d\u7167\u5176\u58f0\u660e\u987a\u5e8f\u8fdb\u884c\u521d\u59cb\u5316\u3002 \u5728 C++20 \u4e2d\uff0c\u4f7f\u7528 \u6307\u5b9a\u521d\u59cb\u5316 \u53ef\u4ee5\u663e\u5f0f\u5730\u6307\u5b9a\u5c06\u521d\u59cb\u5316\u503c\u6620\u5c04\u5230\u54ea\u4e2a\u6210\u5458\u3002\u7ed3\u6784\u4f53\u6210\u5458\u5fc5\u987b\u6309\u7167\u58f0\u660e\u7684\u987a\u5e8f\u521d\u59cb\u5316\uff0c\u5426\u5219\u4f1a\u62a5\u9519\u3002 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 - \u5b9a\u4e49\u7ed3\u6784(\u6216\u7c7b)\u7c7b\u578b\u65f6\uff0c\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u6210\u5458\u63d0\u4f9b\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u4f5c\u4e3a\u7c7b\u578b\u5b9a\u4e49\u7684\u4e00\u90e8\u5206\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \uff0c\u521d\u59cb\u5316\u503c\u79f0\u4e3a \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c \u3002 10.8 - \u7ed3\u6784\u4f53\u4f20\u9012\u53ca\u5176\u4ed6 \u51fa\u4e8e\u6027\u80fd\u539f\u56e0\uff0c\u7f16\u8bd1\u5668\u6709\u65f6\u4f1a\u5728\u7ed3\u6784\u4e2d\u6dfb\u52a0\u95f4\u9699(\u8fd9\u79f0\u4e3a\u586b\u5145)\uff0c\u56e0\u6b64\u7ed3\u6784\u4f53\u7684\u5927\u5c0f\u53ef\u80fd\u5927\u4e8e\u5176\u6210\u5458\u7684\u5927\u5c0f\u4e4b\u548c\u3002 10.9 - \u57fa\u4e8e\u6307\u9488\u548c\u5f15\u7528\u7684\u6210\u5458\u9009\u62e9 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD \u7c7b\u6a21\u677f \u662f\u5b9e\u4f8b\u5316\u7c7b\u7c7b\u578b(\u7ed3\u6784\u3001\u7c7b\u6216\u8054\u5408)\u7684\u6a21\u677f\u5b9a\u4e49\u3002 \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad(CTAD) \u662f C++17\u7684\u4e00\u4e2a\u7279\u6027\uff0c\u5b83\u5141\u8bb8\u7f16\u8bd1\u5668\u4ece\u521d\u59cb\u5316\u5f0f\u4e2d\u63a8\u65ad\u6a21\u677f\u7c7b\u578b\u5b9e\u53c2\u3002","title":"10.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u679a\u4e3e\u548c\u7ed3\u6784\u4f53"},{"location":"10-x-Chapter-10-comprehensive-quiz/#-","text":"10.8 - \u7ed3\u6784\u4f53\u4f20\u9012\u53ca\u5176\u4ed6 \u51fa\u4e8e\u6027\u80fd\u539f\u56e0\uff0c\u7f16\u8bd1\u5668\u6709\u65f6\u4f1a\u5728\u7ed3\u6784\u4e2d\u6dfb\u52a0\u95f4\u9699(\u8fd9\u79f0\u4e3a\u586b\u5145)\uff0c\u56e0\u6b64\u7ed3\u6784\u4f53\u7684\u5927\u5c0f\u53ef\u80fd\u5927\u4e8e\u5176\u6210\u5458\u7684\u5927\u5c0f\u4e4b\u548c\u3002 10.9 - \u57fa\u4e8e\u6307\u9488\u548c\u5f15\u7528\u7684\u6210\u5458\u9009\u62e9 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD \u7c7b\u6a21\u677f \u662f\u5b9e\u4f8b\u5316\u7c7b\u7c7b\u578b(\u7ed3\u6784\u3001\u7c7b\u6216\u8054\u5408)\u7684\u6a21\u677f\u5b9a\u4e49\u3002 \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad(CTAD) \u662f C++17\u7684\u4e00\u4e2a\u7279\u6027\uff0c\u5b83\u5141\u8bb8\u7f16\u8bd1\u5668\u4ece\u521d\u59cb\u5316\u5f0f\u4e2d\u63a8\u65ad\u6a21\u677f\u7c7b\u578b\u5b9e\u53c2\u3002","title":"- \u5b9a\u4e49\u7ed3\u6784(\u6216\u7c7b)\u7c7b\u578b\u65f6\uff0c\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u6210\u5458\u63d0\u4f9b\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u4f5c\u4e3a\u7c7b\u578b\u5b9a\u4e49\u7684\u4e00\u90e8\u5206\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\uff0c\u521d\u59cb\u5316\u503c\u79f0\u4e3a\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c\u3002"},{"location":"10-y-using-a-language-reference/","tags":["summary"],"text":"\u6839\u636e\u4f60\u5b66\u4e60C++\u6240\u5904\u7684\u9636\u6bb5\uff0c\u672c\u7f51\u7ad9\u53ef\u80fd\u662f\u4f60\u5728\u67d0\u4e2a\u9636\u6bb5\u5b66\u4e60C++\u7684\u552f\u4e00\u8d44\u6599\u3002LearnCpp.com \u4ee5\u4e00\u79cd\u65b0\u624b\u53cb\u597d\u7684\u65b9\u5f0f\u5411\u4f60\u4ecb\u7ecd\u4e86C++\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4f46\u662f\u5b83\u4e0d\u53ef\u80fd\u6db5\u76d6C++\u7684\u65b9\u65b9\u9762\u9762\u3002\u5f53\u4f60\u5f00\u59cb\u63a2\u7d22\u672c\u6559\u7a0b\u4ee5\u5916\u7684\u8bdd\u9898\u65f6\uff0c\u4f60\u9700\u8981\u5b66\u4f1a\u5982\u4f55\u5229\u7528\u5176\u4ed6\u7684\u8d44\u6e90\u6765\u8fdb\u884c\u5b66\u4e60\u3002 Stack Overflow \u662f\u4e00\u4e2a\u95ee\u7b54\u7f51\u7ad9\uff0c\u5728\u8fd9\u91cc\u4f60\u53ef\u4ee5\u95ee\u95ee\u9898(\u4f60\u53ef\u4ee5\u5728\u8fd9\u91cc\u627e\u5230\u4e0e\u4f60\u6709\u76f8\u540c\u95ee\u9898\u7684\u4eba\u5e76\u4e14\u9605\u8bfb\u8fd9\u4e9b\u95ee\u9898\u7684\u7b54\u6848)\u3002\u4e0d\u8fc7\uff0c\u591a\u6570\u60c5\u51b5\u4e0b\u6211\u4eec\u8fd8\u662f\u5efa\u8bae\u4f60\u5148\u53bb\u770b\u770bC++\u8bed\u8a00\u7684\u53c2\u8003\u624b\u518c\u3002\u548c\u6559\u7a0b\u4e0d\u540c\uff0c\u6559\u7a0b\u901a\u5e38\u4e13\u6ce8\u4e8e\u67d0\u4e9b\u91cd\u8981\u8bdd\u9898\u5e76\u4f7f\u7528\u901a\u4fd7\u7684\u8bed\u8a00\u5411\u4f60\u89e3\u91ca\u76f8\u5173\u7684\u6982\u5ff5\uff0c\u4f7f\u5f97\u8bed\u8a00\u5b66\u4e60\u66f4\u5bb9\u6613\u3002\u4f46\u662f\u53c2\u8003\u624b\u518c\u4e0d\u540c\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u4f7f\u7528\u6b63\u5f0f\u7684\u672f\u8bed\uff0c\u7cbe\u786e\u5730\u63cf\u8ff0C++\u4e2d\u7684\u6982\u5ff5\u3002\u56e0\u6b64\uff0c\u53c2\u8003\u624b\u518c\u901a\u5e38\u5f88\u5168\u9762\u3001\u7cbe\u786e\uff0c\u4f46\u6bd4\u8f83\u96be\u4ee5\u7406\u89e3\u3002 \u672c\u8282\u8bfe\u6211\u4eec\u4f1a\u5411\u4f60\u4ecb\u7ecd\u5982\u4f55\u9ad8\u6548\u5730\u4f7f\u7528 cppreference \uff0c\u8fd9\u662f\u4e00\u4e2a\u6d41\u884c\u7684C++\u6807\u51c6\u53c2\u8003\u6587\u6863\u7f51\u7ad9\uff0c\u672c\u6559\u7a0b\u81ea\u59cb\u81f3\u7ec8\u591a\u6b21\u53c2\u8003\u4e86\u8be5\u7f51\u7ad9\u7684\u5185\u5bb9\u3002\u6211\u4eec\u4f1a\u4f7f\u7528\u4e09\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u5411\u4f60\u5c55\u793a\u5982\u4f55\u53bb\u4f7f\u7528\u8be5\u53c2\u8003\u7f51\u7ad9\u3002 \u603b\u89c8 Cppreference \u7684\u9996\u9875\u662f\u4e00\u4e2a \u603b\u89c8\u9875\u9762 \u5176\u4e2d\u5217\u51fa\u4e86\u8bed\u8a00\u7684\u6838\u5fc3\u529f\u80fd\u548c\u6807\u51c6\u5e93\uff1a \u867d\u7136\u5728\u8fd9\u4e2a\u9875\u9762\u4e2d\uff0c\u4f60\u53ef\u4ee5\u770b\u5230cppreference\u63d0\u4f9b\u7684\u6240\u6709\u5185\u5bb9\u7684\u76ee\u5f55\uff0c\u4f46\u901a\u5e38\u4f7f\u7528\u641c\u7d22\u529f\u80fd\u6216\u641c\u7d22\u5f15\u64ce\u66f4\u5bb9\u6613\u627e\u5230\u4f60\u9700\u8981\u7684\u5185\u5bb9\u3002\u5f53\u4f60\u5b8c\u6210\u4e86LearnCpp.com\u4e0a\u7684\u6559\u7a0b\u4e4b\u540e\uff0c\u8be5\u9875\u9762\u662f\u4f60\u7edd\u4f73\u7684\u4e0b\u4e00\u7ad9\u76ee\u7684\u5730\u3002\u5728\u8fd9\u91cc\u4f60\u53ef\u4ee5\u6df1\u5165\u7814\u7a76\u6807\u51c6\u5e93\uff0c\u5e76\u67e5\u770bC++\u8fd8\u63d0\u4f9b\u4e86\u54ea\u4e9b\u4f60\u8fd8\u4e0d\u77e5\u9053\u7684\u529f\u80fd\u3002 \u8868\u7684\u4e0a\u534a\u90e8\u5206\u663e\u793a\u4e86C++\u5f53\u524d\u7684\u7279\u6027\uff0c\u800c\u4e0b\u534a\u90e8\u5206\u5219\u5c55\u793a\u4e86\u6280\u672f\u89c4\u8303\uff0c\u8fd9\u4e9b\u7279\u6027\u53ef\u80fd\u4f1a\u5728\u672a\u6765\u7684\u7248\u672c\u4e2d\u6dfb\u52a0\u5230C++\uff0c\u4e5f\u53ef\u80fd\u4e0d\u4f1a\u6dfb\u52a0\u5230C++\uff0c\u6216\u8005\u5df2\u7ecf\u90e8\u5206\u88ab\u8bed\u8a00\u63a5\u53d7\u3002\u5982\u679c\u60a8\u60f3\u4e86\u89e3\u5373\u5c06\u63a8\u51fa\u7684\u65b0\u529f\u80fd\uff0c\u8fd9\u5c06\u975e\u5e38\u6709\u7528\u3002 \u4eceC++ 11\u5f00\u59cb\uff0ccppreference\u4f1a\u7528\u5f15\u5165\u8be5\u529f\u80fd\u7684\u6807\u51c6\u7248\u672c\u6765\u6807\u8bb0\u5bf9\u5e94\u7684\u529f\u80fd\u3002\u6807\u51c6\u7248\u672c\u662f\u4e0a\u9762\u56fe\u7247\u4e2d\u4e00\u4e9b\u94fe\u63a5\u65c1\u8fb9\u7684\u7eff\u8272\u5c0f\u6570\u5b57\u3002\u90a3\u4e9b\u6ca1\u6709\u6807\u8bb0\u6807\u51c6\u7248\u672c\u7684\u529f\u80fd\uff0c\u5219\u662f\u5728 C++ 98/03 \u4e2d\u5c31\u5df2\u7ecf\u6709\u7684\u529f\u80fd\u3002\u6211\u4eec\u4e0d\u4ec5\u4f1a\u5728\u603b\u89c8\u9875\u9762\u770b\u5230\u6807\u51c6\u7248\u672c\u53f7\uff0c\u5b83\u5728cppreference\u4e0a\u968f\u5904\u53ef\u89c1\uff0c\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u786e\u5207\u5730\u77e5\u9053\u5728\u7279\u5b9a\u7684C++\u7248\u672c\u4e2d\u4ec0\u4e48\u529f\u80fd\u53ef\u7528\uff0c\u4ec0\u4e48\u529f\u80fd\u4e0d\u53ef\u7528\u3002 \u63d0\u9192 C++ \u7684\u7248\u672c\u6709\uff1aC++98, C++03, C++11, C++14, C++17, C++20\u3002C++23 \u662f\u4e0b\u4e00\u4e2a\u8fed\u4ee3\u7684\u7248\u672c\u53f7\u3002 \u6ce8\u610f \u5982\u679c\u4f60\u4f7f\u7528\u641c\u7d22\u5f15\u64ce\uff0c\u800c\u67d0\u4e2a\u6280\u672f\u89c4\u8303\u521a\u521a\u88ab\u63a5\u53d7\u4e3a\u6807\u51c6\uff0c\u90a3\u4e48\u4f60\u53ef\u80fd\u4f1a\u94fe\u63a5\u5230\u67d0\u4e2a\u6280\u672f\u89c4\u8303\u800c\u4e0d\u662f\u5b98\u65b9\u53c2\u8003\uff0c\u8fd9\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002 \u5c0f\u8d34\u58eb cppreference\u4e0d\u4ec5\u662fC++\u7684\u53c2\u8003\uff0c\u4e5f\u662fC\u7684\u53c2\u8003\u3002\u56e0\u4e3aC++\u548cC\u5171\u4eab\u4e00\u4e9b\u51fd\u6570(\u53ef\u80fd\u6709\u6240\u4e0d\u540c)\uff0c\u4f60\u53ef\u80fd\u4f1a\u641c\u7d22\u540e\u88ab\u5f15\u5bfc\u5230C\u8bed\u8a00\u7684\u53c2\u8003\u4e2d\u3002\u5728cppreference\u9876\u90e8\u7684URL\u548c\u5bfc\u822a\u680f\u603b\u662f\u4f1a\u663e\u793a\u60a8\u6b63\u5728\u6d4f\u89c8\u7684\u662fC\u8fd8\u662fC++\u53c2\u8003\u3002 \u6848\u4f8b\u4e00\uff1a std::string::length \u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u9996\u5148\u7814\u7a76\u4e00\u4e2a\u5df2\u7ecf\u5b66\u4e60\u8fc7\u7684\u51fd\u6570\u2014\u2014 std::string::length \uff0c\u5b83\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002 \u5728cppreference\u7684\u53f3\u4e0a\u89d2\uff0c\u641c\u7d22\u201cstring\u201d\u3002\u6b64\u65f6\u505a\u4f1a\u663e\u793a\u4e00\u957f\u4e32\u7c7b\u578b\u548c\u51fd\u6570\uff0c\u76ee\u524d\u53ea\u6709\u6700\u4e0a\u9762\u7684\u662f\u76f8\u5173\u7684\u3002 \u5f53\u6211\u4eec\u4e5f\u53ef\u4ee5\u76f4\u63a5\u641c\u7d22\u201cstring length\u201d\uff0c\u4e0d\u8fc7\u4e3a\u4e86\u5411\u4f60\u66f4\u5168\u9762\u5730\u5c55\u793a\u5404\u79cd\u60c5\u51b5\uff0c\u8fd9\u91cc\u6211\u4eec\u6545\u610f\u820d\u8fd1\u6c42\u8fdc\u4e86\u3002\u70b9\u51fb \u201cStrings library\u201d \u4f1a\u5c06\u6211\u4eec\u5e26\u5230\u4e00\u4e2a\u9875\u9762\uff0c\u8be5\u9875\u9762\u5c55\u793a\u4e86C++\u652f\u6301\u7684\u5404\u79cd\u5b57\u7b26\u4e32\u529f\u80fd\u3002 \u5728 \u201c std::basic_string \u201d \u5c0f\u8282\u4e0b\u65b9\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e00\u7cfb\u5217\u7684 typedefs \uff0c\u5176\u4e2d\u5c31\u6709 std::string \u3002 \u70b9\u51fb \u201c std::string \u201d \u4f1a\u8df3\u8f6c\u5230 std::basic_string \u9875\u9762\u3002\u7f51\u7ad9\u4e2d\u5e76\u6ca1\u6709\u5173\u4e8e std::string \u7684\u9875\u9762\u56e0\u4e3a std::string \u662f typedef \u7684 std::basic_string \u3002\u5728 typedef \u5217\u8868\u4e2d\u53ef\u4ee5\u770b\u5230\u3002 \u8868\u793a\u8be5\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u5176\u7c7b\u578b\u90fd\u662f char \u3002\u5728\u8fd9\u91cc\u53ef\u4ee5\u770b\u5230\uff0cC++\u7684\u5b57\u7b26\u4e32\uff0c\u5176\u5b57\u7b26\u652f\u6301\u4e0d\u540c\u79cd\u7c7b\u7684\u5b57\u7b26\u3002\u5f53\u9700\u8981\u4f7f\u7528Unicode\u800c\u975eASCII\u65f6\uff0c\u8fd9\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002 \u8fd8\u662f\u8fd9\u4e2a\u9875\u9762\uff0c\u7ee7\u7eed\u5f80\u4e0b\u770b\uff0c\u6b64\u65f6\u53ef\u4ee5\u770b\u5230 \u6210\u5458\u51fd\u6570\u5217\u8868 Member functions (\u8be5\u7c7b\u578b\u652f\u6301\u7684\u65b9\u6cd5)\u3002\u5982\u679c\u4f60\u60f3\u77e5\u9053\u67d0\u4e2a\u7c7b\u578b\u53ef\u4ee5\u505a\u4ec0\u4e48\u6837\u7684\u64cd\u4f5c\uff0c\u76f4\u63a5\u770b\u8fd9\u4e2a\u8868\u5c31\u975e\u5e38\u6e05\u695a\u4e86\u3002\u5728\u8fd9\u4e2a\u8868\u4e2d\uff0c\u53ef\u4ee5\u627e\u5230 length \u65b9\u6cd5(\u548c size )\u3002 \u70b9\u51fb\u8be5\u65b9\u6cd5\u7684\u94fe\u63a5\uff0c\u9875\u9762\u8df3\u8f6c\u5230 length \u548c size \u7684\u4ecb\u7ecd \uff08\u8fd9\u4e24\u5176\u5b9e\u662f\u4e00\u56de\u4e8b\uff09\u3002 \u6bcf\u4e2a\u9875\u9762\u7684\u9876\u90e8\u9996\u5148\u5448\u73b0\u7684\u662f\u529f\u80fd\u548c\u8bed\u6cd5\u3001\u91cd\u8f7d\u6216\u58f0\u660e\u7684\u7b80\u77ed\u6458\u8981\u3002 \u8fd9\u4e2a\u9875\u9762\u7684\u6807\u9898\u663e\u793a\u4e86\u7c7b\u548c\u51fd\u6570\u7684\u540d\u79f0\u4ee5\u53ca\u6240\u6709\u6a21\u677f\u53c2\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u5ffd\u7565\u8fd9\u90e8\u5206\u3002\u5728\u6807\u9898\u4e0b\u9762\uff0c\u53ef\u4ee5\u627e\u5230\u6240\u6709\u7684\u91cd\u8f7d\u51fd\u6570(\u5177\u6709\u76f8\u540c\u540d\u79f0\u7684\u51fd\u6570\u7684\u4e0d\u540c\u7248\u672c)\u4ee5\u53ca\u5b83\u4eec\u5e94\u7528\u7684\u8bed\u8a00\u6807\u51c6\u3002 \u518d\u4e0b\u9762\uff0c\u662f\u51fd\u6570\u7684 \u5f62\u53c2 \u5217\u8868\uff0c\u4ee5\u53ca\u8fd4\u56de\u503c\u7c7b\u578b\u3002 \u56e0\u4e3a std::string::length \u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u51fd\u6570\uff0c\u6240\u4ee5\u8be5\u9875\u9762\u4e2d\u5e76\u6ca1\u6709\u5f88\u591a\u5185\u5bb9\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u9875\u9762\u4f1a\u5305\u542b\u4f7f\u7528\u4e86\u76f8\u5173\u7279\u6027\u7684\u4f8b\u5b50\uff0c\u4f8b\u5982\uff1a \u9664\u975e\u4f60\u5df2\u7ecf\u5b8c\u5168\u638c\u63e1C++\uff0c\u5426\u5219\u793a\u4f8b\u4e2d\u603b\u4f1a\u6709\u4f60\u8fd8\u6ca1\u89c1\u8fc7\u7684\u7279\u6027\u3002\u5982\u679c\u6709\u8db3\u591f\u591a\u7684\u4f8b\u5b50\uff0c\u4f60\u53ef\u80fd\u80fd\u591f\u7406\u89e3\u8db3\u591f\u591a\u7684\u5185\u5bb9\uff0c\u4ece\u800c\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u51fd\u6570\u53ca\u5176\u529f\u80fd\u3002\u5982\u679c\u793a\u4f8b\u592a\u590d\u6742\uff0c\u4e5f\u53ef\u4ee5\u5728\u5176\u4ed6\u5730\u65b9\u641c\u7d22\u793a\u4f8b\uff0c\u6216\u8005\u9605\u8bfb\u4e0d\u7406\u89e3\u90e8\u5206\u7684\u53c2\u8003\u8d44\u6599(\u53ef\u4ee5\u5355\u51fb\u793a\u4f8b\u4e2d\u7684\u51fd\u6570\u548c\u7c7b\u578b\u6765\u67e5\u770b\u5b83\u4eec\u7684\u529f\u80fd)\u3002 \u73b0\u5728\u6211\u4eec\u77e5\u9053\u4e86 std::string::length \u7684\u4f5c\u7528\uff0c\u4e0d\u8fc7\u8fd9\u6ca1\u4ec0\u4e48\u65b0\u9c9c\u7684\uff0c\u6bd5\u7adf\u6211\u4eec\u4e4b\u524d\u5c31\u5df2\u7ecf\u638c\u63e1\u8be5\u51fd\u6570\u4e86\u3002\u63a5\u4e0b\u6765\u770b\u770b\u65b0\u7684\u4e1c\u897f\u5427\uff01 \u6848\u4f8b\u4e8c\uff1a std::cin.ignore \u5728 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7 std::cin.ignore \uff0c\u5b83\u53ef\u4ee5\u5ffd\u7565\u6362\u884c\u4e4b\u524d\u7684\u5168\u90e8\u5185\u5bb9\u3002\u8fd9\u4e2a\u51fd\u6570\u7684\u5176\u4e2d\u4e00\u4e2a\u5f62\u53c2\u53c8\u957f\u53c8\u5570\u55e6\uff0c\u5f88\u96be\u88ab\u8bb0\u4f4f\u3002\u662f\u4ec0\u4e48\u6765\u7740\uff1f\u600e\u4e48\u7528\u6765\u7740\uff1f\u8ba9\u6211\u4eec\u67e5\u67e5\u770b\u3002 \u5728\u641c\u7d22\u6765\u8f93\u5165 \u201cstd::cin.ignore\u201d \uff1a std::cin, std::wcin - \u6211\u4eec\u8981\u627e\u7684\u662f .ignore \uff0c\u800c\u4e0d\u662f std::cin \uff1b std::basic_istream::ignore - \u8fd9\u662f\u5565\uff1f\u770b\u4e0d\u61c2\u5148\u8df3\u8fc7\u5427\uff1b std::ignore - \u4e0d\u662f\u8fd9\u4e2a\uff1b std::basic_istream - \u4e5f\u4e0d\u662f\u8fd9\u4e2a\u3002 \u6ca1\u641c\u5230\uff1f\u73b0\u5728\u548b\u6574\uff1f\u8ba9\u6211\u4eec\u5148\u8fdb\u5165 std::cin \uff0c\u7136\u540e\u987a\u85e4\u6478\u74dc\u5427\u3002\u8fd9\u4e2a\u9875\u9762\u4e0a\u4e5f\u6ca1\u6709\u660e\u663e\u4e0e\u6211\u4eec\u9700\u8981\u627e\u7684\u4e1c\u897f\u76f8\u5173\u7684\u5185\u5bb9\u3002\u5728\u9875\u9762\u7684\u9876\u90e8\uff0c\u53ef\u4ee5\u770b\u5230 std::cin \u548c std::wcin \u7684\u58f0\u660e\uff0c\u540c\u65f6\u5b83\u8fd8\u544a\u8bc9\u6211\u4eec\u4f7f\u7528 std::cin \u65f6\u5e94\u8be5\u5305\u542b\u54ea\u4e2a\u5934\u6587\u4ef6\uff1a \u53ef\u4ee5\u770b\u51fa std::cin \u662f std::istream \u7c7b\u578b\u7684\u4e00\u4e2a\u5bf9\u8c61\u3002\u63a5\u4e0b\u6765\u70b9\u51fb std::istream \u7684\u94fe\u63a5\uff1a \u7b49\u7b49\uff01 \u6211\u4e4b\u524d\u641c\u7d22 \u201c std::cin.ignore \u201d \u7684\u65f6\u5019\u662f\u641c\u5230\u8fc7 std::basic_istream \u7684\u3002\u539f\u6765 istream \u5c31\u662f basic_istream \u7684 typedef \u554a\u3002\u4e5f\u8bb8\u6211\u4eec\u5e76\u6ca1\u6709\u641c\u7d22\u9519\uff01 \u9875\u9762\u5411\u4e0b\u6ed1\u52a8\uff0c\u53ef\u4ee5\u770b\u5230\u5f88\u591a\u719f\u6089\u7684\u51fd\u6570\uff1a \u8fd9\u91cc\u5f88\u591a\u51fd\u6570\u6211\u4eec\u90fd\u7528\u8fc7\uff1a operator>> , get , getline , ignore \u3002\u53ef\u4ee5\u6d4f\u89c8\u4e00\u4e0b\u8fd8\u6709\u54ea\u4e9b\u51fd\u6570\uff0c\u7136\u540e\u70b9\u51fb ignore \uff0c\u8fd9\u5c31\u662f\u6211\u4eec\u8981\u627e\u7684\u51fd\u6570\u3002 \u5728\u9875\u9762\u7684\u9876\u90e8\u6709\u51fd\u6570\u7b7e\u540d\u3001\u51fd\u6570\u7b80\u4ecb\u53ca\u5176\u4e24\u4e2a\u53c2\u6570\u7684\u529f\u80fd\u63cf\u8ff0\u3002\u5f62\u53c2\u540e\u9762\u7684 = \u7b26\u53f7\u8868\u793a\u9ed8\u8ba4\u5b9e\u53c2(\u5728\u8bfe\u7a0b 8.12 -\u9ed8\u8ba4\u53c2\u6570 \u4e2d\u8ba8\u8bba\u8fc7)\u3002\u5982\u679c\u6ca1\u6709\u4e3a\u5177\u6709\u9ed8\u8ba4\u503c\u7684\u5f62\u53c2\u63d0\u4f9b\u5b9e\u53c2\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002 \u63a5\u4e0b\u6765\u7684\u65e0\u5e8f\u5217\u8868\u4e2d\u5305\u542b\u4e09\u6761\u5185\u5bb9\u3002\u53ef\u4ee5\u770b\u5230 std::numeric_limits::max() \u5728 std::cin.ignore \u4e2d\u662f\u7531\u7279\u6b8a\u542b\u4e49\u7684\uff0c\u5b83\u53ef\u4ee5\u5ffd\u7565\u5b57\u7b26\u8ba1\u6570\u68c0\u67e5\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c std::cin.ignore \u4f1a\u5ffd\u7565\u5206\u9694\u7b26\u524d\u6240\u6709\u7684\u5b57\u7b26\uff0c\u6216\u8005\u76f4\u5230\u8017\u5c3d\u9700\u8981\u5173\u6ce8\u7684\u5b57\u7b26\u4e2a\u6570\u3002 \u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u77e5\u9053\u8fd9\u4e2a\u51fd\u6570\u662f\u5e72\u561b\u7684\uff0c\u4f46\u662f\u5fd8\u8bb0\u4e86\u53c2\u6570\u548c\u8fd4\u56de\u503c\uff0c\u6b64\u65f6\u4f60\u4e0d\u9700\u8981\u9605\u8bfb\u51fd\u6570\u7684\u5b8c\u6574\u63cf\u8ff0\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f60\u53ea\u9700\u6d4f\u89c8\u4e00\u4e0b\u53c2\u6570\u5217\u8868\u548c\u8fd4\u56de\u503c\u5c31\u8db3\u591f\u4e86\u3002 \u8fd9\u91cc\u7684\u53c2\u6570\u4ecb\u7ecd\u5f88\u7b80\u6d01\u3002\u5b83\u5e76\u6ca1\u6709\u5305\u62ec std::numeric_limits::max() \u7684\u7279\u6b8a\u542b\u4e49\uff0c\u4f46\u662f\u8fd9\u4e9b\u7b80\u6d01\u7684\u53c2\u6570\u4ecb\u7ecd\u4e00\u822c\u60c5\u51b5\u4e0b\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u56de\u5fc6\u8d77\u4e00\u4e9b\u5173\u952e\u7684\u4fe1\u606f\u3002 \u6848\u4f8b\u4e09\uff1a\u8bed\u6cd5 \u9664\u4e86\u6807\u51c6\u5e93\uff0ccppreference\u8fd8\u8bb0\u5f55\u4e86C++\u8bed\u8a00\u7684\u8bed\u6cd5\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u6709\u6548\u7684\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include int getUserInput () { int i {}; std :: cin >> i ; return i ; } int main () { std :: cout << \"How many bananas did you eat today? \\n \" ; if ( int iBananasEaten { getUserInput () }; iBananasEaten <= 2 ) { std :: cout << \"Yummy \\n \" ; } else { std :: cout << iBananasEaten << \" is a lot! \\n \" ; } return 0 ; } \u4e3a\u4ec0\u4e48\u5728 if-statement \u7684\u6761\u4ef6\u4e2d\u6709\u4e00\u4e2a\u53d8\u91cf\u5b9a\u4e49\uff1f\u5728\u641c\u7d22\u5f15\u64ce\u4e2d\u641c\u7d22\u201ccppreference if statement\u201d\u6765\u4e86\u89e3\u5b83\u7684\u529f\u80fd\u3002\u641c\u7d22\u5f15\u64ce\u4f1a\u5c06\u6211\u4eec\u5e26\u5230 if statements \u3002\u5728\u9875\u9762\u5f00\u59cb\u7684\u5730\u65b9\u5c31\u6709\u5176\u76f8\u5173\u7684\u8bed\u6cd5\u8bf4\u660e\u3002 \u5728\u9875\u9762\u7684\u53f3\u534a\u90e8\u5206\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e0d\u540c\u8bed\u6cd5\u4e0eC++\u7248\u672c\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u5bf9\u4e8e if-statement \uff0c\u4eceC++17\u5f00\u59cb\uff0c\u5982\u679c\u4f60\u5ffd\u7565\u6240\u6709\u53ef\u9009\u7684\u90e8\u5206\uff0c\u90a3\u4e48\u5c31\u662f\u6211\u4eec\u5e38\u7528\u7684 if-statement \u7684\u5f62\u5f0f\u3002\u5728 condition \u4e4b\u524d\uff0c\u6709\u4e00\u4e2a\u53ef\u9009\u7684\u90e8\u5206\u79f0\u4e3a init-statement \uff0c\u770b\u8d77\u6765\u8fd9\u90e8\u5206\u5c31\u662f\u548c\u4e4b\u524d\u4f8b\u5b50\u4e2d\u7684\u4ee3\u7801\u662f\u76f8\u5173\u7684\u3002 1 2 if ( init - statement condition ) statement - true if ( init - statement condition ) statement - true else statement - false \u5728\u8bed\u6cd5\u53c2\u8003\u4e0b\u65b9\uff0c\u6709\u5bf9\u8bed\u6cd5\u7684\u6bcf\u4e2a\u90e8\u5206\u7684\u89e3\u91ca\uff0c\u5176\u4e2d\u5c31\u5305\u62ec init-statement \u2014\u2014\u5b83\u901a\u5e38\u662f\u4e00\u4e2a\u5e26\u6709\u521d\u59cb\u5316\u503c\u7684\u53d8\u91cf\u58f0\u660e\u3002 \u4e0b\u9762\u662f if-statements \u7684\u8bed\u6cd5\u89e3\u91ca\u548c\u4e00\u4e9b\u7b80\u5355\u7684\u4f8b\u5b50: \u6211\u4eec\u4e86\u89e3 if-statements \u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u4f46\u662f\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u6ca1\u6709\u5305\u542b init-statement \u7684\u7528\u6cd5\u3002\u6240\u4ee5\u63a5\u7740\u5411\u4e0b\u770b\uff0c\u627e\u627e\u770b\u6709\u6ca1\u6709\u4e13\u95e8\u4ecb\u7ecd\u5e26\u521d\u59cb\u5316\u503c\u7684 if-statements \u7684\u5185\u5bb9\u3002 \u8fd9\u91cc\u9996\u5148\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 init-statement \uff0c\u73b0\u5728\u6211\u4eec\u5c31\u77e5\u9053\u4e4b\u524d\u4ee3\u7801\u4e2d\u7684if\u8bed\u53e5\u662f\u600e\u4e48\u56de\u4e8b\u4e86\u3002\u5176\u5b9e\u5b83\u5c31\u662f\u5c06\u4e00\u822c\u7684\u53d8\u91cf\u58f0\u660e\u548cif\u8bed\u53e5\u5408\u5e76\u8d77\u6765\u7f62\u4e86\u3002 \u63a5\u4e0b\u6765\u7684\u53e5\u5b50\u5f88\u6709\u610f\u601d\uff0c\u5b83\u544a\u8bc9\u6211\u4eec init-statement \u58f0\u660e\u7684\u53d8\u91cf\uff0c\u5728if\u8bed\u53e5\u7684true\u548cfalse\u8bed\u53e5\u5757\u4e2d\u90fd\u80fd\u4f7f\u7528\u3002\u5982\u679c\u4f60\u4e4b\u524d\u5929\u771f\u7684\u8ba4\u4e3a\u8fd9\u4e2a\u53d8\u91cf\u53ea\u80fd\u5728true\u8bed\u53e5\u5757\u4e2d\u4f7f\u7528\u7684\u8bdd\uff0c\u8be5\u8bf4\u660e\u5e94\u8be5\u4f1a\u8ba9\u4f60\u770b\u5230\u6709\u70b9\u610f\u5916\u3002 init-statement \u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86\u4e00\u4e9b\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u8fc7\u7684\u7279\u6027\uff0c\u4f46\u6211\u4eec\u5e76\u4e0d\u9700\u8981\u770b\u61c2\u6240\u6709\u5185\u5bb9\u5c31\u53ef\u4ee5\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 init-statement \u3002\u6240\u4ee5\u76ee\u524d\u53ef\u4ee5\u5148\u8df3\u8fc7\u8ba9\u4eba\u56f0\u60d1\u7684\u5185\u5bb9\uff0c\u4e13\u6ce8\u4e8e\u6211\u4eec\u719f\u7cfb\u7684\u90e8\u5206\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // Iterators, we don't know them. Skip. if ( auto it = m . find ( 10 ); it != m . end ()) { return it -> second . size (); } // [10], what's that? Skip. if ( char buf [ 10 ]; std :: fgets ( buf , 10 , stdin )) { m [ 0 ] += buf ; } // std::lock_guard, we don't know that, but it's some type. We know what types are! if ( std :: lock_guard lock ( mx ); shared_flag ) { unsafe_ping (); shared_flag = false ; } // This is easy, that's an int! if ( int s ; int count = ReadBytesWithSignal ( & s )) { publish ( count ); raise ( s ); } // Whew, no thanks! if ( auto keywords = { \"if\" , \"for\" , \"while\" }; std :: any_of ( keywords . begin (), keywords . end (), [ & s ]( const char * kw ) { return s == kw ; })) { std :: cerr << \"Token must not be a keyword \\n \" ; } \u6700\u7b80\u5355\u7684\u4f8b\u5b50\u4f3c\u4e4e\u662f\u4f7f\u7528\u4e86 int \u7684\u90a3\u4e2a\uff0c\u4f46\u662f\u5b83\u7684\u5206\u53f7\u540e\u9762\u53c8\u662f\u4e00\u4e2a\u5b9a\u4e49\uff0c\u770b\u4e0a\u53bb\u6709\u70b9\u5947\u602a\u3002\u518d\u770b\u4e00\u4e2a std::lock_guard \u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 if ( std :: lock_guard lock ( mx ); shared_flag ) { unsafe_ping (); shared_flag = false ; } \u8fd9\u91cc\u5b9a\u4e49\u4e86\u53d8\u91cf( lock )\uff0c\u7136\u540e\u662f\u5206\u53f7\uff0c\u7136\u540e\u662f\u6761\u4ef6\u3002\u8fd9\u4e2a\u53cd\u5012\u662f\u6700\u63a5\u8fd1\u6211\u4eec\u4ee3\u7801\u7684\u4f8b\u5b50\u3002 \u5173\u4e8e cppreference \u51c6\u786e\u6027\u7684\u6ce8\u610f\u4e8b\u9879 \u5176\u5b9e\uff0ccppreference \u5e76\u4e0d\u662f\u4e00\u4e2a\u5b98\u65b9\u6587\u6863\u6e90\u2014\u2014\u76f8\u53cd\uff0c\u5b83\u662f\u4e00\u4e2awiki\u767e\u79d1\u5e94\u7528\u3002\u4f7f\u7528wiki\uff0c\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u6dfb\u52a0\u548c\u4fee\u6539\u5185\u5bb9\u2014\u2014\u5185\u5bb9\u6765\u81ea\u793e\u533a\u3002\u867d\u7136\u8fd9\u610f\u5473\u7740\u4eba\u4eec\u5f88\u5bb9\u6613\u6dfb\u52a0\u9519\u8bef\u7684\u4fe1\u606f\uff0c\u4f46\u9519\u8bef\u7684\u4fe1\u606f\u901a\u5e38\u4f1a\u5f88\u5feb\u88ab\u53d1\u73b0\u5e76\u5220\u9664\uff0c\u4f7fcppreference\u6210\u4e3a\u4e00\u4e2a\u53ef\u9760\u7684\u6765\u6e90\u3002 C++\u6807\u51c6 \u662f\u552f\u4e00\u7684\u5b98\u65b9\u6587\u6863( github )\u3002\u867d\u7136\u5b83\u662f\u6b63\u5f0f\u7684\u6587\u6863\uff0c\u4f46\u662f\u5b83\u5e76\u4e0d\u662f\u4e00\u4e2a\u6613\u7528\u7684\u53c2\u8003\u624b\u518c\u3002","title":"10.y - \u5b66\u4f1a\u4f7f\u7528C++\u53c2\u8003\u624b\u518c"},{"location":"10-y-using-a-language-reference/#_1","text":"Cppreference \u7684\u9996\u9875\u662f\u4e00\u4e2a \u603b\u89c8\u9875\u9762 \u5176\u4e2d\u5217\u51fa\u4e86\u8bed\u8a00\u7684\u6838\u5fc3\u529f\u80fd\u548c\u6807\u51c6\u5e93\uff1a \u867d\u7136\u5728\u8fd9\u4e2a\u9875\u9762\u4e2d\uff0c\u4f60\u53ef\u4ee5\u770b\u5230cppreference\u63d0\u4f9b\u7684\u6240\u6709\u5185\u5bb9\u7684\u76ee\u5f55\uff0c\u4f46\u901a\u5e38\u4f7f\u7528\u641c\u7d22\u529f\u80fd\u6216\u641c\u7d22\u5f15\u64ce\u66f4\u5bb9\u6613\u627e\u5230\u4f60\u9700\u8981\u7684\u5185\u5bb9\u3002\u5f53\u4f60\u5b8c\u6210\u4e86LearnCpp.com\u4e0a\u7684\u6559\u7a0b\u4e4b\u540e\uff0c\u8be5\u9875\u9762\u662f\u4f60\u7edd\u4f73\u7684\u4e0b\u4e00\u7ad9\u76ee\u7684\u5730\u3002\u5728\u8fd9\u91cc\u4f60\u53ef\u4ee5\u6df1\u5165\u7814\u7a76\u6807\u51c6\u5e93\uff0c\u5e76\u67e5\u770bC++\u8fd8\u63d0\u4f9b\u4e86\u54ea\u4e9b\u4f60\u8fd8\u4e0d\u77e5\u9053\u7684\u529f\u80fd\u3002 \u8868\u7684\u4e0a\u534a\u90e8\u5206\u663e\u793a\u4e86C++\u5f53\u524d\u7684\u7279\u6027\uff0c\u800c\u4e0b\u534a\u90e8\u5206\u5219\u5c55\u793a\u4e86\u6280\u672f\u89c4\u8303\uff0c\u8fd9\u4e9b\u7279\u6027\u53ef\u80fd\u4f1a\u5728\u672a\u6765\u7684\u7248\u672c\u4e2d\u6dfb\u52a0\u5230C++\uff0c\u4e5f\u53ef\u80fd\u4e0d\u4f1a\u6dfb\u52a0\u5230C++\uff0c\u6216\u8005\u5df2\u7ecf\u90e8\u5206\u88ab\u8bed\u8a00\u63a5\u53d7\u3002\u5982\u679c\u60a8\u60f3\u4e86\u89e3\u5373\u5c06\u63a8\u51fa\u7684\u65b0\u529f\u80fd\uff0c\u8fd9\u5c06\u975e\u5e38\u6709\u7528\u3002 \u4eceC++ 11\u5f00\u59cb\uff0ccppreference\u4f1a\u7528\u5f15\u5165\u8be5\u529f\u80fd\u7684\u6807\u51c6\u7248\u672c\u6765\u6807\u8bb0\u5bf9\u5e94\u7684\u529f\u80fd\u3002\u6807\u51c6\u7248\u672c\u662f\u4e0a\u9762\u56fe\u7247\u4e2d\u4e00\u4e9b\u94fe\u63a5\u65c1\u8fb9\u7684\u7eff\u8272\u5c0f\u6570\u5b57\u3002\u90a3\u4e9b\u6ca1\u6709\u6807\u8bb0\u6807\u51c6\u7248\u672c\u7684\u529f\u80fd\uff0c\u5219\u662f\u5728 C++ 98/03 \u4e2d\u5c31\u5df2\u7ecf\u6709\u7684\u529f\u80fd\u3002\u6211\u4eec\u4e0d\u4ec5\u4f1a\u5728\u603b\u89c8\u9875\u9762\u770b\u5230\u6807\u51c6\u7248\u672c\u53f7\uff0c\u5b83\u5728cppreference\u4e0a\u968f\u5904\u53ef\u89c1\uff0c\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u786e\u5207\u5730\u77e5\u9053\u5728\u7279\u5b9a\u7684C++\u7248\u672c\u4e2d\u4ec0\u4e48\u529f\u80fd\u53ef\u7528\uff0c\u4ec0\u4e48\u529f\u80fd\u4e0d\u53ef\u7528\u3002 \u63d0\u9192 C++ \u7684\u7248\u672c\u6709\uff1aC++98, C++03, C++11, C++14, C++17, C++20\u3002C++23 \u662f\u4e0b\u4e00\u4e2a\u8fed\u4ee3\u7684\u7248\u672c\u53f7\u3002 \u6ce8\u610f \u5982\u679c\u4f60\u4f7f\u7528\u641c\u7d22\u5f15\u64ce\uff0c\u800c\u67d0\u4e2a\u6280\u672f\u89c4\u8303\u521a\u521a\u88ab\u63a5\u53d7\u4e3a\u6807\u51c6\uff0c\u90a3\u4e48\u4f60\u53ef\u80fd\u4f1a\u94fe\u63a5\u5230\u67d0\u4e2a\u6280\u672f\u89c4\u8303\u800c\u4e0d\u662f\u5b98\u65b9\u53c2\u8003\uff0c\u8fd9\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002 \u5c0f\u8d34\u58eb cppreference\u4e0d\u4ec5\u662fC++\u7684\u53c2\u8003\uff0c\u4e5f\u662fC\u7684\u53c2\u8003\u3002\u56e0\u4e3aC++\u548cC\u5171\u4eab\u4e00\u4e9b\u51fd\u6570(\u53ef\u80fd\u6709\u6240\u4e0d\u540c)\uff0c\u4f60\u53ef\u80fd\u4f1a\u641c\u7d22\u540e\u88ab\u5f15\u5bfc\u5230C\u8bed\u8a00\u7684\u53c2\u8003\u4e2d\u3002\u5728cppreference\u9876\u90e8\u7684URL\u548c\u5bfc\u822a\u680f\u603b\u662f\u4f1a\u663e\u793a\u60a8\u6b63\u5728\u6d4f\u89c8\u7684\u662fC\u8fd8\u662fC++\u53c2\u8003\u3002","title":"\u603b\u89c8"},{"location":"10-y-using-a-language-reference/#stdstringlength","text":"\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u9996\u5148\u7814\u7a76\u4e00\u4e2a\u5df2\u7ecf\u5b66\u4e60\u8fc7\u7684\u51fd\u6570\u2014\u2014 std::string::length \uff0c\u5b83\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002 \u5728cppreference\u7684\u53f3\u4e0a\u89d2\uff0c\u641c\u7d22\u201cstring\u201d\u3002\u6b64\u65f6\u505a\u4f1a\u663e\u793a\u4e00\u957f\u4e32\u7c7b\u578b\u548c\u51fd\u6570\uff0c\u76ee\u524d\u53ea\u6709\u6700\u4e0a\u9762\u7684\u662f\u76f8\u5173\u7684\u3002 \u5f53\u6211\u4eec\u4e5f\u53ef\u4ee5\u76f4\u63a5\u641c\u7d22\u201cstring length\u201d\uff0c\u4e0d\u8fc7\u4e3a\u4e86\u5411\u4f60\u66f4\u5168\u9762\u5730\u5c55\u793a\u5404\u79cd\u60c5\u51b5\uff0c\u8fd9\u91cc\u6211\u4eec\u6545\u610f\u820d\u8fd1\u6c42\u8fdc\u4e86\u3002\u70b9\u51fb \u201cStrings library\u201d \u4f1a\u5c06\u6211\u4eec\u5e26\u5230\u4e00\u4e2a\u9875\u9762\uff0c\u8be5\u9875\u9762\u5c55\u793a\u4e86C++\u652f\u6301\u7684\u5404\u79cd\u5b57\u7b26\u4e32\u529f\u80fd\u3002 \u5728 \u201c std::basic_string \u201d \u5c0f\u8282\u4e0b\u65b9\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e00\u7cfb\u5217\u7684 typedefs \uff0c\u5176\u4e2d\u5c31\u6709 std::string \u3002 \u70b9\u51fb \u201c std::string \u201d \u4f1a\u8df3\u8f6c\u5230 std::basic_string \u9875\u9762\u3002\u7f51\u7ad9\u4e2d\u5e76\u6ca1\u6709\u5173\u4e8e std::string \u7684\u9875\u9762\u56e0\u4e3a std::string \u662f typedef \u7684 std::basic_string \u3002\u5728 typedef \u5217\u8868\u4e2d\u53ef\u4ee5\u770b\u5230\u3002 \u8868\u793a\u8be5\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u5176\u7c7b\u578b\u90fd\u662f char \u3002\u5728\u8fd9\u91cc\u53ef\u4ee5\u770b\u5230\uff0cC++\u7684\u5b57\u7b26\u4e32\uff0c\u5176\u5b57\u7b26\u652f\u6301\u4e0d\u540c\u79cd\u7c7b\u7684\u5b57\u7b26\u3002\u5f53\u9700\u8981\u4f7f\u7528Unicode\u800c\u975eASCII\u65f6\uff0c\u8fd9\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002 \u8fd8\u662f\u8fd9\u4e2a\u9875\u9762\uff0c\u7ee7\u7eed\u5f80\u4e0b\u770b\uff0c\u6b64\u65f6\u53ef\u4ee5\u770b\u5230 \u6210\u5458\u51fd\u6570\u5217\u8868 Member functions (\u8be5\u7c7b\u578b\u652f\u6301\u7684\u65b9\u6cd5)\u3002\u5982\u679c\u4f60\u60f3\u77e5\u9053\u67d0\u4e2a\u7c7b\u578b\u53ef\u4ee5\u505a\u4ec0\u4e48\u6837\u7684\u64cd\u4f5c\uff0c\u76f4\u63a5\u770b\u8fd9\u4e2a\u8868\u5c31\u975e\u5e38\u6e05\u695a\u4e86\u3002\u5728\u8fd9\u4e2a\u8868\u4e2d\uff0c\u53ef\u4ee5\u627e\u5230 length \u65b9\u6cd5(\u548c size )\u3002 \u70b9\u51fb\u8be5\u65b9\u6cd5\u7684\u94fe\u63a5\uff0c\u9875\u9762\u8df3\u8f6c\u5230 length \u548c size \u7684\u4ecb\u7ecd \uff08\u8fd9\u4e24\u5176\u5b9e\u662f\u4e00\u56de\u4e8b\uff09\u3002 \u6bcf\u4e2a\u9875\u9762\u7684\u9876\u90e8\u9996\u5148\u5448\u73b0\u7684\u662f\u529f\u80fd\u548c\u8bed\u6cd5\u3001\u91cd\u8f7d\u6216\u58f0\u660e\u7684\u7b80\u77ed\u6458\u8981\u3002 \u8fd9\u4e2a\u9875\u9762\u7684\u6807\u9898\u663e\u793a\u4e86\u7c7b\u548c\u51fd\u6570\u7684\u540d\u79f0\u4ee5\u53ca\u6240\u6709\u6a21\u677f\u53c2\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u5ffd\u7565\u8fd9\u90e8\u5206\u3002\u5728\u6807\u9898\u4e0b\u9762\uff0c\u53ef\u4ee5\u627e\u5230\u6240\u6709\u7684\u91cd\u8f7d\u51fd\u6570(\u5177\u6709\u76f8\u540c\u540d\u79f0\u7684\u51fd\u6570\u7684\u4e0d\u540c\u7248\u672c)\u4ee5\u53ca\u5b83\u4eec\u5e94\u7528\u7684\u8bed\u8a00\u6807\u51c6\u3002 \u518d\u4e0b\u9762\uff0c\u662f\u51fd\u6570\u7684 \u5f62\u53c2 \u5217\u8868\uff0c\u4ee5\u53ca\u8fd4\u56de\u503c\u7c7b\u578b\u3002 \u56e0\u4e3a std::string::length \u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u51fd\u6570\uff0c\u6240\u4ee5\u8be5\u9875\u9762\u4e2d\u5e76\u6ca1\u6709\u5f88\u591a\u5185\u5bb9\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u9875\u9762\u4f1a\u5305\u542b\u4f7f\u7528\u4e86\u76f8\u5173\u7279\u6027\u7684\u4f8b\u5b50\uff0c\u4f8b\u5982\uff1a \u9664\u975e\u4f60\u5df2\u7ecf\u5b8c\u5168\u638c\u63e1C++\uff0c\u5426\u5219\u793a\u4f8b\u4e2d\u603b\u4f1a\u6709\u4f60\u8fd8\u6ca1\u89c1\u8fc7\u7684\u7279\u6027\u3002\u5982\u679c\u6709\u8db3\u591f\u591a\u7684\u4f8b\u5b50\uff0c\u4f60\u53ef\u80fd\u80fd\u591f\u7406\u89e3\u8db3\u591f\u591a\u7684\u5185\u5bb9\uff0c\u4ece\u800c\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u51fd\u6570\u53ca\u5176\u529f\u80fd\u3002\u5982\u679c\u793a\u4f8b\u592a\u590d\u6742\uff0c\u4e5f\u53ef\u4ee5\u5728\u5176\u4ed6\u5730\u65b9\u641c\u7d22\u793a\u4f8b\uff0c\u6216\u8005\u9605\u8bfb\u4e0d\u7406\u89e3\u90e8\u5206\u7684\u53c2\u8003\u8d44\u6599(\u53ef\u4ee5\u5355\u51fb\u793a\u4f8b\u4e2d\u7684\u51fd\u6570\u548c\u7c7b\u578b\u6765\u67e5\u770b\u5b83\u4eec\u7684\u529f\u80fd)\u3002 \u73b0\u5728\u6211\u4eec\u77e5\u9053\u4e86 std::string::length \u7684\u4f5c\u7528\uff0c\u4e0d\u8fc7\u8fd9\u6ca1\u4ec0\u4e48\u65b0\u9c9c\u7684\uff0c\u6bd5\u7adf\u6211\u4eec\u4e4b\u524d\u5c31\u5df2\u7ecf\u638c\u63e1\u8be5\u51fd\u6570\u4e86\u3002\u63a5\u4e0b\u6765\u770b\u770b\u65b0\u7684\u4e1c\u897f\u5427\uff01","title":"\u6848\u4f8b\u4e00\uff1astd::string::length"},{"location":"10-y-using-a-language-reference/#stdcinignore","text":"\u5728 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7 std::cin.ignore \uff0c\u5b83\u53ef\u4ee5\u5ffd\u7565\u6362\u884c\u4e4b\u524d\u7684\u5168\u90e8\u5185\u5bb9\u3002\u8fd9\u4e2a\u51fd\u6570\u7684\u5176\u4e2d\u4e00\u4e2a\u5f62\u53c2\u53c8\u957f\u53c8\u5570\u55e6\uff0c\u5f88\u96be\u88ab\u8bb0\u4f4f\u3002\u662f\u4ec0\u4e48\u6765\u7740\uff1f\u600e\u4e48\u7528\u6765\u7740\uff1f\u8ba9\u6211\u4eec\u67e5\u67e5\u770b\u3002 \u5728\u641c\u7d22\u6765\u8f93\u5165 \u201cstd::cin.ignore\u201d \uff1a std::cin, std::wcin - \u6211\u4eec\u8981\u627e\u7684\u662f .ignore \uff0c\u800c\u4e0d\u662f std::cin \uff1b std::basic_istream::ignore - \u8fd9\u662f\u5565\uff1f\u770b\u4e0d\u61c2\u5148\u8df3\u8fc7\u5427\uff1b std::ignore - \u4e0d\u662f\u8fd9\u4e2a\uff1b std::basic_istream - \u4e5f\u4e0d\u662f\u8fd9\u4e2a\u3002 \u6ca1\u641c\u5230\uff1f\u73b0\u5728\u548b\u6574\uff1f\u8ba9\u6211\u4eec\u5148\u8fdb\u5165 std::cin \uff0c\u7136\u540e\u987a\u85e4\u6478\u74dc\u5427\u3002\u8fd9\u4e2a\u9875\u9762\u4e0a\u4e5f\u6ca1\u6709\u660e\u663e\u4e0e\u6211\u4eec\u9700\u8981\u627e\u7684\u4e1c\u897f\u76f8\u5173\u7684\u5185\u5bb9\u3002\u5728\u9875\u9762\u7684\u9876\u90e8\uff0c\u53ef\u4ee5\u770b\u5230 std::cin \u548c std::wcin \u7684\u58f0\u660e\uff0c\u540c\u65f6\u5b83\u8fd8\u544a\u8bc9\u6211\u4eec\u4f7f\u7528 std::cin \u65f6\u5e94\u8be5\u5305\u542b\u54ea\u4e2a\u5934\u6587\u4ef6\uff1a \u53ef\u4ee5\u770b\u51fa std::cin \u662f std::istream \u7c7b\u578b\u7684\u4e00\u4e2a\u5bf9\u8c61\u3002\u63a5\u4e0b\u6765\u70b9\u51fb std::istream \u7684\u94fe\u63a5\uff1a \u7b49\u7b49\uff01 \u6211\u4e4b\u524d\u641c\u7d22 \u201c std::cin.ignore \u201d \u7684\u65f6\u5019\u662f\u641c\u5230\u8fc7 std::basic_istream \u7684\u3002\u539f\u6765 istream \u5c31\u662f basic_istream \u7684 typedef \u554a\u3002\u4e5f\u8bb8\u6211\u4eec\u5e76\u6ca1\u6709\u641c\u7d22\u9519\uff01 \u9875\u9762\u5411\u4e0b\u6ed1\u52a8\uff0c\u53ef\u4ee5\u770b\u5230\u5f88\u591a\u719f\u6089\u7684\u51fd\u6570\uff1a \u8fd9\u91cc\u5f88\u591a\u51fd\u6570\u6211\u4eec\u90fd\u7528\u8fc7\uff1a operator>> , get , getline , ignore \u3002\u53ef\u4ee5\u6d4f\u89c8\u4e00\u4e0b\u8fd8\u6709\u54ea\u4e9b\u51fd\u6570\uff0c\u7136\u540e\u70b9\u51fb ignore \uff0c\u8fd9\u5c31\u662f\u6211\u4eec\u8981\u627e\u7684\u51fd\u6570\u3002 \u5728\u9875\u9762\u7684\u9876\u90e8\u6709\u51fd\u6570\u7b7e\u540d\u3001\u51fd\u6570\u7b80\u4ecb\u53ca\u5176\u4e24\u4e2a\u53c2\u6570\u7684\u529f\u80fd\u63cf\u8ff0\u3002\u5f62\u53c2\u540e\u9762\u7684 = \u7b26\u53f7\u8868\u793a\u9ed8\u8ba4\u5b9e\u53c2(\u5728\u8bfe\u7a0b 8.12 -\u9ed8\u8ba4\u53c2\u6570 \u4e2d\u8ba8\u8bba\u8fc7)\u3002\u5982\u679c\u6ca1\u6709\u4e3a\u5177\u6709\u9ed8\u8ba4\u503c\u7684\u5f62\u53c2\u63d0\u4f9b\u5b9e\u53c2\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002 \u63a5\u4e0b\u6765\u7684\u65e0\u5e8f\u5217\u8868\u4e2d\u5305\u542b\u4e09\u6761\u5185\u5bb9\u3002\u53ef\u4ee5\u770b\u5230 std::numeric_limits::max() \u5728 std::cin.ignore \u4e2d\u662f\u7531\u7279\u6b8a\u542b\u4e49\u7684\uff0c\u5b83\u53ef\u4ee5\u5ffd\u7565\u5b57\u7b26\u8ba1\u6570\u68c0\u67e5\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c std::cin.ignore \u4f1a\u5ffd\u7565\u5206\u9694\u7b26\u524d\u6240\u6709\u7684\u5b57\u7b26\uff0c\u6216\u8005\u76f4\u5230\u8017\u5c3d\u9700\u8981\u5173\u6ce8\u7684\u5b57\u7b26\u4e2a\u6570\u3002 \u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u77e5\u9053\u8fd9\u4e2a\u51fd\u6570\u662f\u5e72\u561b\u7684\uff0c\u4f46\u662f\u5fd8\u8bb0\u4e86\u53c2\u6570\u548c\u8fd4\u56de\u503c\uff0c\u6b64\u65f6\u4f60\u4e0d\u9700\u8981\u9605\u8bfb\u51fd\u6570\u7684\u5b8c\u6574\u63cf\u8ff0\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f60\u53ea\u9700\u6d4f\u89c8\u4e00\u4e0b\u53c2\u6570\u5217\u8868\u548c\u8fd4\u56de\u503c\u5c31\u8db3\u591f\u4e86\u3002 \u8fd9\u91cc\u7684\u53c2\u6570\u4ecb\u7ecd\u5f88\u7b80\u6d01\u3002\u5b83\u5e76\u6ca1\u6709\u5305\u62ec std::numeric_limits::max() \u7684\u7279\u6b8a\u542b\u4e49\uff0c\u4f46\u662f\u8fd9\u4e9b\u7b80\u6d01\u7684\u53c2\u6570\u4ecb\u7ecd\u4e00\u822c\u60c5\u51b5\u4e0b\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u56de\u5fc6\u8d77\u4e00\u4e9b\u5173\u952e\u7684\u4fe1\u606f\u3002","title":"\u6848\u4f8b\u4e8c\uff1astd::cin.ignore"},{"location":"10-y-using-a-language-reference/#_2","text":"\u9664\u4e86\u6807\u51c6\u5e93\uff0ccppreference\u8fd8\u8bb0\u5f55\u4e86C++\u8bed\u8a00\u7684\u8bed\u6cd5\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u6709\u6548\u7684\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include int getUserInput () { int i {}; std :: cin >> i ; return i ; } int main () { std :: cout << \"How many bananas did you eat today? \\n \" ; if ( int iBananasEaten { getUserInput () }; iBananasEaten <= 2 ) { std :: cout << \"Yummy \\n \" ; } else { std :: cout << iBananasEaten << \" is a lot! \\n \" ; } return 0 ; } \u4e3a\u4ec0\u4e48\u5728 if-statement \u7684\u6761\u4ef6\u4e2d\u6709\u4e00\u4e2a\u53d8\u91cf\u5b9a\u4e49\uff1f\u5728\u641c\u7d22\u5f15\u64ce\u4e2d\u641c\u7d22\u201ccppreference if statement\u201d\u6765\u4e86\u89e3\u5b83\u7684\u529f\u80fd\u3002\u641c\u7d22\u5f15\u64ce\u4f1a\u5c06\u6211\u4eec\u5e26\u5230 if statements \u3002\u5728\u9875\u9762\u5f00\u59cb\u7684\u5730\u65b9\u5c31\u6709\u5176\u76f8\u5173\u7684\u8bed\u6cd5\u8bf4\u660e\u3002 \u5728\u9875\u9762\u7684\u53f3\u534a\u90e8\u5206\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e0d\u540c\u8bed\u6cd5\u4e0eC++\u7248\u672c\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u5bf9\u4e8e if-statement \uff0c\u4eceC++17\u5f00\u59cb\uff0c\u5982\u679c\u4f60\u5ffd\u7565\u6240\u6709\u53ef\u9009\u7684\u90e8\u5206\uff0c\u90a3\u4e48\u5c31\u662f\u6211\u4eec\u5e38\u7528\u7684 if-statement \u7684\u5f62\u5f0f\u3002\u5728 condition \u4e4b\u524d\uff0c\u6709\u4e00\u4e2a\u53ef\u9009\u7684\u90e8\u5206\u79f0\u4e3a init-statement \uff0c\u770b\u8d77\u6765\u8fd9\u90e8\u5206\u5c31\u662f\u548c\u4e4b\u524d\u4f8b\u5b50\u4e2d\u7684\u4ee3\u7801\u662f\u76f8\u5173\u7684\u3002 1 2 if ( init - statement condition ) statement - true if ( init - statement condition ) statement - true else statement - false \u5728\u8bed\u6cd5\u53c2\u8003\u4e0b\u65b9\uff0c\u6709\u5bf9\u8bed\u6cd5\u7684\u6bcf\u4e2a\u90e8\u5206\u7684\u89e3\u91ca\uff0c\u5176\u4e2d\u5c31\u5305\u62ec init-statement \u2014\u2014\u5b83\u901a\u5e38\u662f\u4e00\u4e2a\u5e26\u6709\u521d\u59cb\u5316\u503c\u7684\u53d8\u91cf\u58f0\u660e\u3002 \u4e0b\u9762\u662f if-statements \u7684\u8bed\u6cd5\u89e3\u91ca\u548c\u4e00\u4e9b\u7b80\u5355\u7684\u4f8b\u5b50: \u6211\u4eec\u4e86\u89e3 if-statements \u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u4f46\u662f\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u6ca1\u6709\u5305\u542b init-statement \u7684\u7528\u6cd5\u3002\u6240\u4ee5\u63a5\u7740\u5411\u4e0b\u770b\uff0c\u627e\u627e\u770b\u6709\u6ca1\u6709\u4e13\u95e8\u4ecb\u7ecd\u5e26\u521d\u59cb\u5316\u503c\u7684 if-statements \u7684\u5185\u5bb9\u3002 \u8fd9\u91cc\u9996\u5148\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 init-statement \uff0c\u73b0\u5728\u6211\u4eec\u5c31\u77e5\u9053\u4e4b\u524d\u4ee3\u7801\u4e2d\u7684if\u8bed\u53e5\u662f\u600e\u4e48\u56de\u4e8b\u4e86\u3002\u5176\u5b9e\u5b83\u5c31\u662f\u5c06\u4e00\u822c\u7684\u53d8\u91cf\u58f0\u660e\u548cif\u8bed\u53e5\u5408\u5e76\u8d77\u6765\u7f62\u4e86\u3002 \u63a5\u4e0b\u6765\u7684\u53e5\u5b50\u5f88\u6709\u610f\u601d\uff0c\u5b83\u544a\u8bc9\u6211\u4eec init-statement \u58f0\u660e\u7684\u53d8\u91cf\uff0c\u5728if\u8bed\u53e5\u7684true\u548cfalse\u8bed\u53e5\u5757\u4e2d\u90fd\u80fd\u4f7f\u7528\u3002\u5982\u679c\u4f60\u4e4b\u524d\u5929\u771f\u7684\u8ba4\u4e3a\u8fd9\u4e2a\u53d8\u91cf\u53ea\u80fd\u5728true\u8bed\u53e5\u5757\u4e2d\u4f7f\u7528\u7684\u8bdd\uff0c\u8be5\u8bf4\u660e\u5e94\u8be5\u4f1a\u8ba9\u4f60\u770b\u5230\u6709\u70b9\u610f\u5916\u3002 init-statement \u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86\u4e00\u4e9b\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u8fc7\u7684\u7279\u6027\uff0c\u4f46\u6211\u4eec\u5e76\u4e0d\u9700\u8981\u770b\u61c2\u6240\u6709\u5185\u5bb9\u5c31\u53ef\u4ee5\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 init-statement \u3002\u6240\u4ee5\u76ee\u524d\u53ef\u4ee5\u5148\u8df3\u8fc7\u8ba9\u4eba\u56f0\u60d1\u7684\u5185\u5bb9\uff0c\u4e13\u6ce8\u4e8e\u6211\u4eec\u719f\u7cfb\u7684\u90e8\u5206\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // Iterators, we don't know them. Skip. if ( auto it = m . find ( 10 ); it != m . end ()) { return it -> second . size (); } // [10], what's that? Skip. if ( char buf [ 10 ]; std :: fgets ( buf , 10 , stdin )) { m [ 0 ] += buf ; } // std::lock_guard, we don't know that, but it's some type. We know what types are! if ( std :: lock_guard lock ( mx ); shared_flag ) { unsafe_ping (); shared_flag = false ; } // This is easy, that's an int! if ( int s ; int count = ReadBytesWithSignal ( & s )) { publish ( count ); raise ( s ); } // Whew, no thanks! if ( auto keywords = { \"if\" , \"for\" , \"while\" }; std :: any_of ( keywords . begin (), keywords . end (), [ & s ]( const char * kw ) { return s == kw ; })) { std :: cerr << \"Token must not be a keyword \\n \" ; } \u6700\u7b80\u5355\u7684\u4f8b\u5b50\u4f3c\u4e4e\u662f\u4f7f\u7528\u4e86 int \u7684\u90a3\u4e2a\uff0c\u4f46\u662f\u5b83\u7684\u5206\u53f7\u540e\u9762\u53c8\u662f\u4e00\u4e2a\u5b9a\u4e49\uff0c\u770b\u4e0a\u53bb\u6709\u70b9\u5947\u602a\u3002\u518d\u770b\u4e00\u4e2a std::lock_guard \u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 if ( std :: lock_guard lock ( mx ); shared_flag ) { unsafe_ping (); shared_flag = false ; } \u8fd9\u91cc\u5b9a\u4e49\u4e86\u53d8\u91cf( lock )\uff0c\u7136\u540e\u662f\u5206\u53f7\uff0c\u7136\u540e\u662f\u6761\u4ef6\u3002\u8fd9\u4e2a\u53cd\u5012\u662f\u6700\u63a5\u8fd1\u6211\u4eec\u4ee3\u7801\u7684\u4f8b\u5b50\u3002","title":"\u6848\u4f8b\u4e09\uff1a\u8bed\u6cd5"},{"location":"10-y-using-a-language-reference/#cppreference","text":"\u5176\u5b9e\uff0ccppreference \u5e76\u4e0d\u662f\u4e00\u4e2a\u5b98\u65b9\u6587\u6863\u6e90\u2014\u2014\u76f8\u53cd\uff0c\u5b83\u662f\u4e00\u4e2awiki\u767e\u79d1\u5e94\u7528\u3002\u4f7f\u7528wiki\uff0c\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u6dfb\u52a0\u548c\u4fee\u6539\u5185\u5bb9\u2014\u2014\u5185\u5bb9\u6765\u81ea\u793e\u533a\u3002\u867d\u7136\u8fd9\u610f\u5473\u7740\u4eba\u4eec\u5f88\u5bb9\u6613\u6dfb\u52a0\u9519\u8bef\u7684\u4fe1\u606f\uff0c\u4f46\u9519\u8bef\u7684\u4fe1\u606f\u901a\u5e38\u4f1a\u5f88\u5feb\u88ab\u53d1\u73b0\u5e76\u5220\u9664\uff0c\u4f7fcppreference\u6210\u4e3a\u4e00\u4e2a\u53ef\u9760\u7684\u6765\u6e90\u3002 C++\u6807\u51c6 \u662f\u552f\u4e00\u7684\u5b98\u65b9\u6587\u6863( github )\u3002\u867d\u7136\u5b83\u662f\u6b63\u5f0f\u7684\u6587\u6863\uff0c\u4f46\u662f\u5b83\u5e76\u4e0d\u662f\u4e00\u4e2a\u6613\u7528\u7684\u53c2\u8003\u624b\u518c\u3002","title":"\u5173\u4e8e cppreference \u51c6\u786e\u6027\u7684\u6ce8\u610f\u4e8b\u9879"},{"location":"11-1-Arrays-Part-I/","tags":["array"],"text":"Key Takeaway \u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5355\u4e2a\u5143\u7d20\uff0c\u9700\u8981\u4f7f\u7528\u6570\u7ec4\u540d\u3001\u4e0b\u6807\u64cd\u4f5c\u7b26( [] )\u548c\u4e0b\u6807(\u6216\u7d22\u5f15)\uff0c\u4e0b\u6807\u4ece0\u5f00\u59cb\u8ba1\u6570 int prime[5]{}; //\u521d\u59cb\u5316\u6570\u7ec4\u4e3a0 \u5728 C++ \u4e2d\u6570\u7ec4 \u4e0b\u6807(subscripts) \u5fc5\u987b\u662f\u6574\u578b\u7c7b\u578b\uff0c\u5305\u62ec char , short , int , long , long long \u7b49\uff0c\u5947\u602a\u7684\u662f\u5e03\u5c14\u7c7b\u578b\u7adf\u7136\u4e5f\u53ef\u4ee5\uff08 false \u5bf9\u5e94\u7d22\u5f15\u4e3a0\uff0c true \u5bf9\u5e94\u7d22\u5f15\u4e3a1\uff09\u3002 \u6570\u7ec4\u7684\u4e0b\u6807\u53ef\u4ee5\u662f \u5b57\u9762\u91cf \u3001\u53d8\u91cf\uff08 const \u6216\u975e const \u7c7b\u578b\uff09\u6216\u8005\u662f\u4e00\u4e2a\u6c42\u503c\u7ed3\u679c\u4e3a\u6574\u5f62\u7c7b\u578b\u7684\u8868\u8fbe\u5f0f\u3002 \u6570\u7ec4\u7684\u957f\u5ea6(\u65b9\u62ec\u53f7\u4e4b\u95f4)\u5fc5\u987b\u662f \u7f16\u8bd1\u65f6\u5e38\u91cf \uff0c\u975e const \u53d8\u91cf\u6216\u8005 \u8fd0\u884c\u65f6 \u5e38\u91cf \u6ce8\u610f\uff1a\u8fd9\u4e00\u7ae0\u76f8\u5bf9\u4e8e\u524d\u9762\u51e0\u7ae0\u4f1a\u66f4\u6709\u96be\u5ea6\u3002\u5982\u679c\u4f60\u611f\u6cc4\u6c14\uff0c\u4e00\u5b9a\u8981\u575a\u6301\u4f4f\u3002\u597d\u620f\u8fd8\u5728\u540e\u9762\u5462\uff01 \u5728 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u901a\u8fc7\u7ed3\u6784\u4f53\u5c06\u591a\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u805a\u5408\u4e3a\u4e00\u4e2a\u53d8\u91cf\u3002\u8fd9\u9879\u80fd\u529b\u5728\u6211\u4eec\u9700\u8981\u5bf9\u4e00\u4e2a\u5355\u72ec\u7684\u5bf9\u8c61\u8fdb\u884c\u5efa\u6a21\u65f6\u662f\u5f88\u6709\u7528\u7684\u3002\u4e0d\u8fc7\uff0c\u5f53\u6211\u4eec\u9700\u8981\u4fdd\u5b58\u4e00\u7cfb\u5217\u76f8\u5173\u7684\u5b9e\u4f8b\u65f6\uff0c\u8fd9\u4e2a\u7ed3\u6784\u4f53\u5c31\u6392\u4e0d\u4e0a\u7528\u573a\u4e86\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u7ed3\u6784\u4f53\u5e76\u4e0d\u662f C++ \u4e2d\u552f\u4e00\u80fd\u805a\u5408\u6570\u636e\u7684\u7c7b\u578b\u3002 \u6570\u7ec4 \u53ef\u4ee5\u5c06\u4e00\u7cfb\u5217\u5177\u6709\u76f8\u540c\u7c7b\u578b\u7684\u53d8\u91cf\u805a\u5408\u4e3a\u4e00\u4e2a\u53d8\u91cf\u3002 \u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u60a8\u60f3\u8981\u8bb0\u5f55\u4e00\u4e2a\u73ed\u7ea7 30 \u4e2a\u5b66\u751f\u7684\u8003\u8bd5\u6210\u7ee9\u3002\u5982\u679c\u6ca1\u6709\u6570\u7ec4\uff0c\u4f60\u5c06\u4e0d\u5f97\u4e0d\u5206\u914d 30 \u4e2a\u51e0\u4e4e\u76f8\u540c\u7684\u53d8\u91cf\uff01 1 2 3 4 5 6 // allocate 30 integer variables (each with a different name) int testScoreStudent1 {}; int testScoreStudent2 {}; int testScoreStudent3 {}; // ... int testScoreStudent30 {}; \u6570\u7ec4\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u7684\u6570\u7ec4\u5b9a\u4e49\u672c\u8d28\u4e0a\u4e0e\u4e0a\u9762\u7684\u4ee3\u7801\u662f\u7b49\u4ef7\u7684: 1 int testScore [ 30 ]{}; // allocate 30 integer variables in a fixed array \u5728\u6570\u7ec4\u53d8\u91cf\u58f0\u660e\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u65b9\u62ec\u53f7( [] )\u544a\u8bc9\u7f16\u8bd1\u5668\u8fd9\u662f\u4e00\u4e2a\u6570\u7ec4\u53d8\u91cf(\u800c\u4e0d\u662f\u666e\u901a\u7684\u53d8\u91cf)\uff0c\u4ee5\u53ca\u5206\u914d\u591a\u5c11\u53d8\u91cf(\u79f0\u4e3a \u6570\u7ec4\u957f\u5ea6 )\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a\u540d\u4e3a testScore \u7684\u56fa\u5b9a\u6570\u7ec4\uff0c\u957f\u5ea6\u4e3a30\u3002 \u56fa\u5b9a\u6570\u7ec4 (\u4e5f\u79f0\u4e3a \u56fa\u5b9a\u957f\u5ea6\u6570\u7ec4 \u6216 \u56fa\u5b9a\u5927\u5c0f\u6570\u7ec4 )\u662f\u5728\u7f16\u8bd1\u65f6\u5df2\u77e5\u957f\u5ea6\u7684\u6570\u7ec4\u3002 \u5f53testScore \u5b9e\u4f8b\u5316\u65f6\uff0c\u5c06\u5206\u914d30\u4e2a\u6574\u6570\u3002 \u6570\u7ec4\u5143\u7d20\u548c\u4e0b\u6807 \u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u53d8\u91cf\u90fd\u88ab\u79f0\u4e3a \u5143\u7d20 \u3002\u5143\u7d20\u6ca1\u6709\u81ea\u5df1\u7684\u540d\u79f0\u3002\u800c\u5982\u679c\u8981\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5355\u4e2a\u5143\u7d20\uff0c\u5219\u9700\u8981\u4f7f\u7528\u6570\u7ec4\u540d\u3001\u4e0b\u6807\u64cd\u4f5c\u7b26( [] )\u548c\u4e00\u4e2a\u88ab\u79f0\u4e3a\u4e0b\u6807(\u6216\u7d22\u5f15)\u7684\u5f62\u53c2\u6765\u544a\u8bc9\u7f16\u8bd1\u5668\u9700\u8981\u54ea\u4e2a\u5143\u7d20\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a\u6570\u7ec4\u7684\u53d6\u4e0b\u6807(subscripting)\u6216\u7d22\u5f15(indexing)\u64cd\u4f5c\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f testScore[0] \u3002\u7b2c\u4e8c\u4e2a\u662f testScore[1] \u3002\u7b2c\u5341\u4e2a\u662f testScore[9] \u3002 testScore \u6570\u7ec4\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u662f testScore[29] \u3002\u8fd9\u5f88\u597d\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u518d\u9700\u8981\u8ddf\u8e2a\u4e00\u5806\u4e0d\u540c\u7684(\u4f46\u76f8\u5173\u7684)\u540d\u79f0\u2014\u2014\u6211\u4eec\u53ea\u9700\u6539\u53d8\u4e0b\u6807\u6765\u8bbf\u95ee\u4e0d\u540c\u7684\u5143\u7d20\u3002 \u91cd\u8981 \uff1a\u4e0e\u65e5\u5e38\u751f\u6d3b\u4e2d\u6211\u4eec\u901a\u5e38\u4ece1\u5f00\u59cb\u8ba1\u6570\u4e0d\u540c\uff0c\u5728C++\u4e2d\uff0c\u6570\u7ec4\u603b\u662f\u4ece0\u5f00\u59cb\u8ba1\u6570\uff01 \u5bf9\u4e8e\u957f\u5ea6\u4e3a N \u7684\u6570\u7ec4\uff0c\u6570\u7ec4\u5143\u7d20\u7f16\u53f7\u4ece0\u5230N-1\u3002\u8fd9\u88ab\u79f0\u4e3a\u6570\u7ec4\u7684 \u8303\u56f4 \u3002 \u6570\u7ec4\u7a0b\u5e8f\u5b9e\u4f8b \u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u7a0b\u5e8f\uff0c\u5c06\u6570\u7ec4\u7684\u5b9a\u4e49\u548c\u7d22\u5f15\u653e\u5728\u4e00\u8d77: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { int prime [ 5 ]{}; // hold the first 5 prime numbers prime [ 0 ] = 2 ; // The first element has index 0 prime [ 1 ] = 3 ; prime [ 2 ] = 5 ; prime [ 3 ] = 7 ; prime [ 4 ] = 11 ; // The last element has index 4 (array length-1) std :: cout << \"The lowest prime number is: \" << prime [ 0 ] << '\\n' ; std :: cout << \"The sum of the first 5 primes is: \" << prime [ 0 ] + prime [ 1 ] + prime [ 2 ] + prime [ 3 ] + prime [ 4 ] << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 The lowest prime number is: 2 The sum of the first 5 primes is: 28 \u6570\u7ec4\u6570\u636e\u7c7b\u578b \u6570\u7ec4\u53ef\u4ee5\u7531\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u7ec4\u6210\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a double \u7c7b\u578b\u7684\u6570\u7ec4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { double batteryLifeInHours [ 3 ]{}; // \u5206\u914d 3 \u4e2a doubles batteryLifeInHours [ 0 ] = 2.0 ; batteryLifeInHours [ 1 ] = 3.0 ; batteryLifeInHours [ 2 ] = 4.3 ; std :: cout << \"The average battery life is \" << ( batteryLifeInHours [ 0 ] + batteryLifeInHours [ 1 ] + batteryLifeInHours [ 2 ]) / 3.0 << \" hour(s) \\n \" ; return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 The average battery life is 3.1 hour(s) \u6570\u7ec4\u4e5f\u53ef\u4ee5\u7531\u7ed3\u6784\u4f53\u7ec4\u6210\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 struct Rectangle { int length {}; int width {}; }; Rectangle rects [ 5 ]{}; // declare an array of 5 Rectangle \u8981\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7ed3\u6784\u4f53\u5143\u7d20\u7684\u6210\u5458\uff0c\u9996\u5148\u9009\u62e9\u60f3\u8981\u8bbf\u95ee\u7684\u6570\u7ec4\u5143\u7d20\uff0c\u7136\u540e\u4f7f\u7528\u6210\u5458\u9009\u62e9\u64cd\u4f5c\u7b26\u9009\u62e9\u60f3\u8981\u8bbf\u95ee\u7684\u6210\u5458\uff1a 1 rects [ 0 ]. length = 24 ; \u6570\u7ec4\u751a\u81f3\u53ef\u4ee5\u7531\u6570\u7ec4\u7ec4\u6210\uff0c\u8fd9\u4e2a\u4f1a\u5728\u4eca\u540e\u7684\u8bfe\u7a0b\u4e2d\u8ba8\u8bba\u3002 \u6570\u7ec4\u4e0b\u6807 \u5728 C++ \u4e2d\u6570\u7ec4 \u4e0b\u6807(subscripts) \u5fc5\u987b\u662f\u6574\u578b\u7c7b\u578b\uff0c\u5305\u62ec char , short , int , long , long long \u7b49\uff0c\u5947\u602a\u7684\u662f\u5e03\u5c14\u7c7b\u578b\u7adf\u7136\u4e5f\u53ef\u4ee5\uff08 false \u5bf9\u5e94\u7d22\u5f15\u4e3a0\uff0c true \u5bf9\u5e94\u7d22\u5f15\u4e3a1\uff09\u3002\u6570\u7ec4\u7684\u4e0b\u6807\u53ef\u4ee5\u662f \u5b57\u9762\u91cf \u3001\u53d8\u91cf\uff08 const \u6216\u975e const \u7c7b\u578b\uff09\u6216\u8005\u662f\u4e00\u4e2a\u6c42\u503c\u7ed3\u679c\u4e3a\u6574\u5f62\u7c7b\u578b\u7684\u8868\u8fbe\u5f0f\u3002 \u4e0b\u9762\u662f\u4e00\u4e9b\u6570\u7ec4\u4e0b\u6807\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 int array [ 5 ]{}; // \u58f0\u660e\u957f\u5ea6\u4e3a 5 \u7684\u6570\u7ec4 // \u4f7f\u7528\u5b57\u9762\u91cf\u7d22\u5f15: array [ 1 ] = 7 ; // ok // \u4f7f\u7528\u679a\u4e3e\u7d22\u5f15 enum Animals { animal_cat = 2 }; array [ animal_cat ] = 4 ; // ok // \u4f7f\u7528\u53d8\u91cf\uff08\u975e const\uff09\u4f5c\u4e3a\u7d22\u5f15 int index { 3 }; array [ index ] = 7 ; // ok // \u4f7f\u7528\u6c42\u503c\u7ed3\u679c\u4e3a\u6574\u5f62\u7c7b\u578b\u7684\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u7d22\u5f15 array [ 1 + 2 ] = 7 ; // ok \u58f0\u660e\u56fa\u5b9a\u957f\u5ea6\u6570\u7ec4 \u58f0\u660e\u56fa\u5b9a\u6570\u7ec4\u65f6\uff0c\u6570\u7ec4\u7684\u957f\u5ea6(\u65b9\u62ec\u53f7\u4e4b\u95f4)\u5fc5\u987b\u662f \u7f16\u8bd1\u65f6\u5e38\u91cf \u3002\u8fd9\u662f\u56e0\u4e3a\u56fa\u5b9a\u6570\u7ec4\u7684\u957f\u5ea6\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u77e5\u9053\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u58f0\u660e\u56fa\u5b9a\u6570\u7ec4\u7684\u4e0d\u540c\u65b9\u6cd5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // \u4f7f\u7528\u5b57\u9762\u91cf\u5e38\u91cf int numberOfLessonsPerDay [ 7 ]{}; // Ok //\u4f7f\u7528 constexpr \u7b26\u53f7\u5e38\u91cf constexpr int daysPerWeek { 7 }; int numberOfLessonsPerDay [ daysPerWeek ]{}; // Ok // \u4f7f\u7528\u679a\u4e3e enum DaysOfWeek { monday , tuesday , wednesday , thursday , friday , saturday , sunday , maxDaysOfWeek }; int numberOfLessonsPerDay [ maxDaysOfWeek ]{}; // Ok // \u4f7f\u7528\u5b8f #define DAYS_PER_WEEK 7 int numberOfLessonsPerDay [ DAYS_PER_WEEK ]{}; // \u53ef\u4ee5\uff0c\u4f46\u662f\u4e0d\u8981\u8fd9\u4e48\u505a\uff08\u5e94\u5f53\u4f7f\u7528 constexpr \u7b26\u53f7\u5e38\u91cf\uff09 \u6ce8\u610f\uff0c\u975e const \u53d8\u91cf\u6216\u8005 \u8fd0\u884c\u65f6 \u5e38\u91cf\u662f\u4e0d\u884c\u7684\uff1a 1 2 3 4 5 6 7 8 9 // \u4f7f\u7528\u975e const \u53d8\u91cf int daysPerWeek {}; std :: cin >> daysPerWeek ; int numberOfLessonsPerDay [ daysPerWeek ]{}; // Not ok -- daysPerWeek \u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf // \u4f7f\u7528\u8fd0\u884c\u65f6\u5e38\u91cf int temp { 5 }; const int daysPerWeek { temp }; // daysPerWeek \u76f4\u5230\u8fd0\u884c\u65f6\u5728\u76f4\u5230\uff0c\u6240\u4ee5\u662f\u8fd0\u884c\u65f6\u5e38\u91cf\u800c\u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf int numberOfLessonsPerDay [ daysPerWeek ]{}; // Not ok \u6ce8\u610f\uff0c\u5728\u6700\u540e\u4e24\u79cd\u60c5\u51b5\u4e0b\u4f1a\u4ea7\u751f\u9519\u8bef\uff0c\u56e0\u4e3a\u6570\u7ec4\u7684\u957f\u5ea6\u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u6709\u4e9b\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5141\u8bb8\u8fd9\u4e9b\u7c7b\u578b\u7684\u6570\u7ec4(\u51fa\u4e8e\u4e0eC99\u517c\u5bb9\u7684\u539f\u56e0)\uff0c\u4f46\u5b83\u4eec\u5728++\u4e2d\u662f\u65e0\u6548\u7684\uff0c\u4e0d\u5e94\u8be5\u5728C++\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u3002\u5982\u679c\u7f16\u8bd1\u5668\u5141\u8bb8\u8fd9\u4e9b\u6570\u7ec4\uff0c\u90a3\u4e48\u60a8\u53ef\u80fd\u5fd8\u8bb0\u7981\u7528\u7f16\u8bd1\u5668\u6269\u5c55\u3002 \u52a8\u6001\u6570\u7ec4 \u56e0\u4e3a\u56fa\u5b9a\u6570\u7ec4\u5728\u7f16\u8bd1\u65f6\u5206\u914d\u5185\u5b58\uff0c\u56e0\u6b64\u4f1a\u5e26\u6765\u4e24\u4e2a\u9650\u5236\uff1a \u56fa\u5b9a\u6570\u7ec4\u7684\u957f\u5ea6\u4e0d\u80fd\u57fa\u4e8e\u7528\u6237\u8f93\u5165\u6216\u8fd0\u884c\u65f6\u8ba1\u7b97\u7684\u5176\u4ed6\u503c\u6765\u8bbe\u5b9a\u3002 \u56fa\u5b9a\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u56fa\u5b9a\u7684\u4e0d\u80fd\u6539\u53d8\u3002 \u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4e9b\u9650\u5236\u4e0d\u5408\u7406\u7684\u3002\u5e78\u8fd0\u7684\u662f\uff0cC++\u652f\u6301\u7b2c\u4e8c\u79cd\u6570\u7ec4\uff0c\u79f0\u4e3a \u52a8\u6001\u6570\u7ec4 \u3002\u52a8\u6001\u6570\u7ec4\u7684\u957f\u5ea6\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u8bbe\u7f6e\uff0c\u4e5f\u53ef\u4ee5\u66f4\u6539\u5176\u957f\u5ea6\u3002\u7136\u800c\uff0c\u52a8\u6001\u6570\u7ec4\u7684\u5b9e\u4f8b\u5316\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\uff0c\u56e0\u6b64\u6211\u4eec\u5c06\u5728\u540e\u9762\u4ecb\u7ecd\u5b83\u4eec\u3002 \u5c0f\u7ed3 \u56fa\u5b9a\u6570\u7ec4\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5206\u914d\u548c\u4f7f\u7528\u76f8\u540c\u7c7b\u578b\u7684\u591a\u4e2a\u53d8\u91cf\uff0c\u4f46\u6570\u7ec4\u7684\u957f\u5ea6\u5728\u7f16\u8bd1\u65f6\u5fc5\u987b\u662f\u5df2\u77e5\u7684\u3002 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u56fa\u5b9a\u6570\u7ec4\u7684\u4e3b\u9898\u3002","title":"11.1 - \u6570\u7ec4\uff08\u7b2c\u4e00\u90e8\u5206\uff09"},{"location":"11-1-Arrays-Part-I/#_1","text":"\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u53d8\u91cf\u90fd\u88ab\u79f0\u4e3a \u5143\u7d20 \u3002\u5143\u7d20\u6ca1\u6709\u81ea\u5df1\u7684\u540d\u79f0\u3002\u800c\u5982\u679c\u8981\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5355\u4e2a\u5143\u7d20\uff0c\u5219\u9700\u8981\u4f7f\u7528\u6570\u7ec4\u540d\u3001\u4e0b\u6807\u64cd\u4f5c\u7b26( [] )\u548c\u4e00\u4e2a\u88ab\u79f0\u4e3a\u4e0b\u6807(\u6216\u7d22\u5f15)\u7684\u5f62\u53c2\u6765\u544a\u8bc9\u7f16\u8bd1\u5668\u9700\u8981\u54ea\u4e2a\u5143\u7d20\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a\u6570\u7ec4\u7684\u53d6\u4e0b\u6807(subscripting)\u6216\u7d22\u5f15(indexing)\u64cd\u4f5c\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f testScore[0] \u3002\u7b2c\u4e8c\u4e2a\u662f testScore[1] \u3002\u7b2c\u5341\u4e2a\u662f testScore[9] \u3002 testScore \u6570\u7ec4\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u662f testScore[29] \u3002\u8fd9\u5f88\u597d\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u518d\u9700\u8981\u8ddf\u8e2a\u4e00\u5806\u4e0d\u540c\u7684(\u4f46\u76f8\u5173\u7684)\u540d\u79f0\u2014\u2014\u6211\u4eec\u53ea\u9700\u6539\u53d8\u4e0b\u6807\u6765\u8bbf\u95ee\u4e0d\u540c\u7684\u5143\u7d20\u3002 \u91cd\u8981 \uff1a\u4e0e\u65e5\u5e38\u751f\u6d3b\u4e2d\u6211\u4eec\u901a\u5e38\u4ece1\u5f00\u59cb\u8ba1\u6570\u4e0d\u540c\uff0c\u5728C++\u4e2d\uff0c\u6570\u7ec4\u603b\u662f\u4ece0\u5f00\u59cb\u8ba1\u6570\uff01 \u5bf9\u4e8e\u957f\u5ea6\u4e3a N \u7684\u6570\u7ec4\uff0c\u6570\u7ec4\u5143\u7d20\u7f16\u53f7\u4ece0\u5230N-1\u3002\u8fd9\u88ab\u79f0\u4e3a\u6570\u7ec4\u7684 \u8303\u56f4 \u3002","title":"\u6570\u7ec4\u5143\u7d20\u548c\u4e0b\u6807"},{"location":"11-1-Arrays-Part-I/#_2","text":"\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u7a0b\u5e8f\uff0c\u5c06\u6570\u7ec4\u7684\u5b9a\u4e49\u548c\u7d22\u5f15\u653e\u5728\u4e00\u8d77: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { int prime [ 5 ]{}; // hold the first 5 prime numbers prime [ 0 ] = 2 ; // The first element has index 0 prime [ 1 ] = 3 ; prime [ 2 ] = 5 ; prime [ 3 ] = 7 ; prime [ 4 ] = 11 ; // The last element has index 4 (array length-1) std :: cout << \"The lowest prime number is: \" << prime [ 0 ] << '\\n' ; std :: cout << \"The sum of the first 5 primes is: \" << prime [ 0 ] + prime [ 1 ] + prime [ 2 ] + prime [ 3 ] + prime [ 4 ] << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 The lowest prime number is: 2 The sum of the first 5 primes is: 28","title":"\u6570\u7ec4\u7a0b\u5e8f\u5b9e\u4f8b"},{"location":"11-1-Arrays-Part-I/#_3","text":"\u6570\u7ec4\u53ef\u4ee5\u7531\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u7ec4\u6210\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a double \u7c7b\u578b\u7684\u6570\u7ec4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { double batteryLifeInHours [ 3 ]{}; // \u5206\u914d 3 \u4e2a doubles batteryLifeInHours [ 0 ] = 2.0 ; batteryLifeInHours [ 1 ] = 3.0 ; batteryLifeInHours [ 2 ] = 4.3 ; std :: cout << \"The average battery life is \" << ( batteryLifeInHours [ 0 ] + batteryLifeInHours [ 1 ] + batteryLifeInHours [ 2 ]) / 3.0 << \" hour(s) \\n \" ; return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 The average battery life is 3.1 hour(s) \u6570\u7ec4\u4e5f\u53ef\u4ee5\u7531\u7ed3\u6784\u4f53\u7ec4\u6210\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 struct Rectangle { int length {}; int width {}; }; Rectangle rects [ 5 ]{}; // declare an array of 5 Rectangle \u8981\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7ed3\u6784\u4f53\u5143\u7d20\u7684\u6210\u5458\uff0c\u9996\u5148\u9009\u62e9\u60f3\u8981\u8bbf\u95ee\u7684\u6570\u7ec4\u5143\u7d20\uff0c\u7136\u540e\u4f7f\u7528\u6210\u5458\u9009\u62e9\u64cd\u4f5c\u7b26\u9009\u62e9\u60f3\u8981\u8bbf\u95ee\u7684\u6210\u5458\uff1a 1 rects [ 0 ]. length = 24 ; \u6570\u7ec4\u751a\u81f3\u53ef\u4ee5\u7531\u6570\u7ec4\u7ec4\u6210\uff0c\u8fd9\u4e2a\u4f1a\u5728\u4eca\u540e\u7684\u8bfe\u7a0b\u4e2d\u8ba8\u8bba\u3002","title":"\u6570\u7ec4\u6570\u636e\u7c7b\u578b"},{"location":"11-1-Arrays-Part-I/#_4","text":"\u5728 C++ \u4e2d\u6570\u7ec4 \u4e0b\u6807(subscripts) \u5fc5\u987b\u662f\u6574\u578b\u7c7b\u578b\uff0c\u5305\u62ec char , short , int , long , long long \u7b49\uff0c\u5947\u602a\u7684\u662f\u5e03\u5c14\u7c7b\u578b\u7adf\u7136\u4e5f\u53ef\u4ee5\uff08 false \u5bf9\u5e94\u7d22\u5f15\u4e3a0\uff0c true \u5bf9\u5e94\u7d22\u5f15\u4e3a1\uff09\u3002\u6570\u7ec4\u7684\u4e0b\u6807\u53ef\u4ee5\u662f \u5b57\u9762\u91cf \u3001\u53d8\u91cf\uff08 const \u6216\u975e const \u7c7b\u578b\uff09\u6216\u8005\u662f\u4e00\u4e2a\u6c42\u503c\u7ed3\u679c\u4e3a\u6574\u5f62\u7c7b\u578b\u7684\u8868\u8fbe\u5f0f\u3002 \u4e0b\u9762\u662f\u4e00\u4e9b\u6570\u7ec4\u4e0b\u6807\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 int array [ 5 ]{}; // \u58f0\u660e\u957f\u5ea6\u4e3a 5 \u7684\u6570\u7ec4 // \u4f7f\u7528\u5b57\u9762\u91cf\u7d22\u5f15: array [ 1 ] = 7 ; // ok // \u4f7f\u7528\u679a\u4e3e\u7d22\u5f15 enum Animals { animal_cat = 2 }; array [ animal_cat ] = 4 ; // ok // \u4f7f\u7528\u53d8\u91cf\uff08\u975e const\uff09\u4f5c\u4e3a\u7d22\u5f15 int index { 3 }; array [ index ] = 7 ; // ok // \u4f7f\u7528\u6c42\u503c\u7ed3\u679c\u4e3a\u6574\u5f62\u7c7b\u578b\u7684\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u7d22\u5f15 array [ 1 + 2 ] = 7 ; // ok","title":"\u6570\u7ec4\u4e0b\u6807"},{"location":"11-1-Arrays-Part-I/#_5","text":"\u58f0\u660e\u56fa\u5b9a\u6570\u7ec4\u65f6\uff0c\u6570\u7ec4\u7684\u957f\u5ea6(\u65b9\u62ec\u53f7\u4e4b\u95f4)\u5fc5\u987b\u662f \u7f16\u8bd1\u65f6\u5e38\u91cf \u3002\u8fd9\u662f\u56e0\u4e3a\u56fa\u5b9a\u6570\u7ec4\u7684\u957f\u5ea6\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u77e5\u9053\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u58f0\u660e\u56fa\u5b9a\u6570\u7ec4\u7684\u4e0d\u540c\u65b9\u6cd5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // \u4f7f\u7528\u5b57\u9762\u91cf\u5e38\u91cf int numberOfLessonsPerDay [ 7 ]{}; // Ok //\u4f7f\u7528 constexpr \u7b26\u53f7\u5e38\u91cf constexpr int daysPerWeek { 7 }; int numberOfLessonsPerDay [ daysPerWeek ]{}; // Ok // \u4f7f\u7528\u679a\u4e3e enum DaysOfWeek { monday , tuesday , wednesday , thursday , friday , saturday , sunday , maxDaysOfWeek }; int numberOfLessonsPerDay [ maxDaysOfWeek ]{}; // Ok // \u4f7f\u7528\u5b8f #define DAYS_PER_WEEK 7 int numberOfLessonsPerDay [ DAYS_PER_WEEK ]{}; // \u53ef\u4ee5\uff0c\u4f46\u662f\u4e0d\u8981\u8fd9\u4e48\u505a\uff08\u5e94\u5f53\u4f7f\u7528 constexpr \u7b26\u53f7\u5e38\u91cf\uff09 \u6ce8\u610f\uff0c\u975e const \u53d8\u91cf\u6216\u8005 \u8fd0\u884c\u65f6 \u5e38\u91cf\u662f\u4e0d\u884c\u7684\uff1a 1 2 3 4 5 6 7 8 9 // \u4f7f\u7528\u975e const \u53d8\u91cf int daysPerWeek {}; std :: cin >> daysPerWeek ; int numberOfLessonsPerDay [ daysPerWeek ]{}; // Not ok -- daysPerWeek \u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf // \u4f7f\u7528\u8fd0\u884c\u65f6\u5e38\u91cf int temp { 5 }; const int daysPerWeek { temp }; // daysPerWeek \u76f4\u5230\u8fd0\u884c\u65f6\u5728\u76f4\u5230\uff0c\u6240\u4ee5\u662f\u8fd0\u884c\u65f6\u5e38\u91cf\u800c\u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf int numberOfLessonsPerDay [ daysPerWeek ]{}; // Not ok \u6ce8\u610f\uff0c\u5728\u6700\u540e\u4e24\u79cd\u60c5\u51b5\u4e0b\u4f1a\u4ea7\u751f\u9519\u8bef\uff0c\u56e0\u4e3a\u6570\u7ec4\u7684\u957f\u5ea6\u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u6709\u4e9b\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5141\u8bb8\u8fd9\u4e9b\u7c7b\u578b\u7684\u6570\u7ec4(\u51fa\u4e8e\u4e0eC99\u517c\u5bb9\u7684\u539f\u56e0)\uff0c\u4f46\u5b83\u4eec\u5728++\u4e2d\u662f\u65e0\u6548\u7684\uff0c\u4e0d\u5e94\u8be5\u5728C++\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u3002\u5982\u679c\u7f16\u8bd1\u5668\u5141\u8bb8\u8fd9\u4e9b\u6570\u7ec4\uff0c\u90a3\u4e48\u60a8\u53ef\u80fd\u5fd8\u8bb0\u7981\u7528\u7f16\u8bd1\u5668\u6269\u5c55\u3002","title":"\u58f0\u660e\u56fa\u5b9a\u957f\u5ea6\u6570\u7ec4"},{"location":"11-1-Arrays-Part-I/#_6","text":"\u56e0\u4e3a\u56fa\u5b9a\u6570\u7ec4\u5728\u7f16\u8bd1\u65f6\u5206\u914d\u5185\u5b58\uff0c\u56e0\u6b64\u4f1a\u5e26\u6765\u4e24\u4e2a\u9650\u5236\uff1a \u56fa\u5b9a\u6570\u7ec4\u7684\u957f\u5ea6\u4e0d\u80fd\u57fa\u4e8e\u7528\u6237\u8f93\u5165\u6216\u8fd0\u884c\u65f6\u8ba1\u7b97\u7684\u5176\u4ed6\u503c\u6765\u8bbe\u5b9a\u3002 \u56fa\u5b9a\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u56fa\u5b9a\u7684\u4e0d\u80fd\u6539\u53d8\u3002 \u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4e9b\u9650\u5236\u4e0d\u5408\u7406\u7684\u3002\u5e78\u8fd0\u7684\u662f\uff0cC++\u652f\u6301\u7b2c\u4e8c\u79cd\u6570\u7ec4\uff0c\u79f0\u4e3a \u52a8\u6001\u6570\u7ec4 \u3002\u52a8\u6001\u6570\u7ec4\u7684\u957f\u5ea6\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u8bbe\u7f6e\uff0c\u4e5f\u53ef\u4ee5\u66f4\u6539\u5176\u957f\u5ea6\u3002\u7136\u800c\uff0c\u52a8\u6001\u6570\u7ec4\u7684\u5b9e\u4f8b\u5316\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\uff0c\u56e0\u6b64\u6211\u4eec\u5c06\u5728\u540e\u9762\u4ecb\u7ecd\u5b83\u4eec\u3002","title":"\u52a8\u6001\u6570\u7ec4"},{"location":"11-1-Arrays-Part-I/#_7","text":"\u56fa\u5b9a\u6570\u7ec4\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5206\u914d\u548c\u4f7f\u7528\u76f8\u540c\u7c7b\u578b\u7684\u591a\u4e2a\u53d8\u91cf\uff0c\u4f46\u6570\u7ec4\u7684\u957f\u5ea6\u5728\u7f16\u8bd1\u65f6\u5fc5\u987b\u662f\u5df2\u77e5\u7684\u3002 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u56fa\u5b9a\u6570\u7ec4\u7684\u4e3b\u9898\u3002","title":"\u5c0f\u7ed3"},{"location":"11-10-C-style-string-symbolic-constants/","tags":["symbolic constants"],"text":"Key Takeaway \u521d\u59cb\u5316C\u5b57\u7b26\u4e32\u5e38\u91cf\u7684\u4e24\u79cd\u65b9\u6cd5\u2014\u2014\u56fa\u5b9a\u6570\u7ec4\u548c\u6307\u9488 char myName[]{ \"Alex\" }; // \u56fa\u5b9a\u6570\u7ec4 const char* myName{ \"Alex\" };//\u6307\u5411\u7b26\u53f7\u5e38\u91cf\u7684\u6307\u9488 \u7b26\u53f7\u5e38\u91cf\u65b9\u5f0f\u521d\u59cb\u5316\u7684C\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u53ef\u80fd\u4f1a\u4f4d\u4e8e\u5185\u5b58\u4e2d\u7684\u53ea\u8bfb\u533a\u57df\uff0c\u5e76\u4e14\u76f8\u540c\u7684\u5b57\u7b26\u4e32\u53d8\u91cf\u53ef\u80fd\u4f1a\u6307\u5411\u540c\u4e00\u5757\u5185\u5b58\uff0c\u56e0\u6b64\u6700\u597d\u5b9a\u4e49\u4e3aconst \u7b26\u53f7\u5e38\u91cf\u65b9\u5f0f\u521d\u59cb\u5316\u7684C\u98ce\u683c\u5b57\u7b26\u4e32\u4f4d\u4e8e\u53ea\u8bfb\u5185\u5b58\u4e2d\u65f6\uff0c\u5b83\u5177\u6709\u5168\u5c40\u4f5c\u7528\u57df\u3002 C \u98ce\u683c\u5b57\u7b26\u4e32\u5e38\u91cf \u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u521d\u59cb\u5316\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char myName []{ \"Alex\" }; // \u56fa\u5b9a\u6570\u7ec4 std :: cout << myName << '\\n' ; return 0 ; } C++ \u4e5f\u652f\u6301\u4f7f\u7528\u6307\u9488\u6307\u5411\u7b26\u53f7\u5e38\u91cf\u6765\u521b\u5efaC\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { const char * myName { \"Alex\" }; // pointer to symbolic constant std :: cout << myName << '\\n' ; return 0 ; } \u5c3d\u7ba1\u4ece\u5c42\u5e8f\u8fd0\u884c\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u4e0a\u9762\u4e24\u5957\u4ee3\u7801\u7684\u7ed3\u679c\u662f\u4e00\u6837\u7684\uff0c\u4f46C++\u5728\u5904\u7406\u4e0a\u8ff0\u4e24\u79cd\u4ee3\u7801\u65f6\uff0c\u5176\u5185\u5b58\u5206\u914d\u7684\u5904\u7406\u7ec6\u8282\u662f\u4e0d\u540c\u7684\u3002 \u5bf9\u4e8e\u56fa\u5b9a\u957f\u5ea6\u6570\u7ec4\u7684\u4f8b\u5b50\u6765\u8bf4\uff0c\u7a0b\u5e8f\u4f1a\u5728\u5185\u5b58\u4e2d\u5206\u914d\u4e00\u4e2a\u957f\u5ea6\u4e3a5\u7684\u6570\u7ec4\uff0c\u540c\u65f6\u5c06\u5185\u5b58\u521d\u59cb\u5316\u4e3a \u201c Alex\\0 \u201d\uff0c\u56e0\u4e3a\u8be5\u5185\u5b58\u5df2\u7ecf\u88ab\u5206\u914d\u7ed9\u8be5\u6570\u7ec4\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u81ea\u7531\u5730\u4fee\u6539\u6570\u7ec4\u7684\u5185\u5bb9\u3002\u8fd9\u4e2a\u6570\u7ec4\u4f1a\u88ab\u5f53\u505a\u4e00\u822c\u7684\u5c40\u90e8\u53d8\u91cf\u6765\u5bf9\u5f85\uff0c\u56e0\u6b64\u5f53\u5b83 \u79bb\u5f00\u4f5c\u7528\u57df \u65f6\uff0c\u5176\u6240\u4f7f\u7528\u7684\u5185\u5b58\u4f1a\u88ab\u91ca\u653e\u4ee5\u4fbf\u5176\u4ed6\u53d8\u91cf\u4f7f\u7528\u3002 \u800c\u5bf9\u4e8e\u7b26\u53f7\u5e38\u91cf\u7684\u4f8b\u5b50\u4e2d\u6765\u8bf4\uff0c\u7f16\u8bd1\u5668\u5982\u4f55\u5904\u7406\u53d6\u51b3\u4e8e\u5177\u4f53\u5b9e\u73b0\u3002 \u4e00\u822c \u6765\u8bb2\uff0c\u7f16\u8bd1\u5668\u4f1a\u628a\u201c Alex\\0 \u201d \u653e\u5728\u67d0\u5904\u53ea\u8bfb\u5185\u5b58\uff0c\u7136\u540e\u4f7f\u7528\u4e00\u4e2a\u6307\u9488\u6307\u5411\u5b83\u3002\u56e0\u4e3a\u8be5\u5185\u5b58\u662f\u53ea\u8bfb\u7684\uff0c\u6240\u4ee5\u6700\u597d\u5c06\u8be5\u5b57\u7b26\u4e32\u8bbe\u7f6e\u4e3aconst\u3002 \u7f16\u8bd1\u5668\u4f1a\u51fa\u4e8e\u6027\u80fd\u4f18\u5316\u7684\u8003\u91cf\uff0c\u5c06\u591a\u4e2a\u76f8\u540c\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u5408\u5e76\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u503c\u3002\u4f8b\u5982\uff1a 1 2 const char * name1 { \"Alex\" }; const char * name2 { \"Alex\" }; \u8fd9\u662f\u4e24\u4e2a\u5177\u6709\u76f8\u540c\u503c\u7684\u4e0d\u540c\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u3002\u7f16\u8bd1\u5668\u53ef\u4ee5\u9009\u62e9\u5c06\u5b83\u4eec\u7ec4\u5408\u6210\u4e00\u4e2a\u5171\u4eab\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u503c\uff0c name1 \u548c name2 \u90fd\u6307\u5411\u76f8\u540c\u7684\u5730\u5740\u3002\u56e0\u6b64\uff0c\u5982\u679c name1 \u4e0d\u662f const \uff0c\u90a3\u4e48\u5bf9 name1 \u8fdb\u884c\u66f4\u6539\u4e5f\u4f1a\u5f71\u54cd name2 (\u8fd9\u53ef\u80fd\u662f\u610f\u6599\u4e4b\u5916\u7684)\u3002 \u7531\u4e8e\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u88ab\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\u7684\u56fa\u5b9a\u4f4d\u7f6e\uff0c\u56e0\u6b64\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u5177\u6709\u9759\u6001\u6301\u7eed\u65f6\u95f4\uff0c\u800c\u4e0d\u662f\u81ea\u52a8\u6301\u7eed\u65f6\u95f4(\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4eec\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u9500\u6bc1\uff0c\u800c\u4e0d\u662f\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u5757\u7ed3\u675f\u65f6\u9500\u6bc1)\u3002\u8fd9\u610f\u5473\u7740\u5f53\u6211\u4eec\u4f7f\u7528\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u65f6\uff0c\u6211\u4eec\u4e0d\u5fc5\u62c5\u5fc3\u4f5c\u7528\u57df\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u8fd9\u4e48\u505a\u662f\u53ef\u4ee5\u7684\uff1a 1 2 3 4 const char * getName () { return \"Alex\" ; } \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c getName() \u5c06\u8fd4\u56de\u4e00\u4e2a\u6307\u5411C\u98ce\u683c\u5b57\u7b26\u4e32 \"Alex\" \u7684\u6307\u9488\u3002\u5982\u679c\u8be5\u51fd\u6570\u6309\u5730\u5740\u8fd4\u56de\u4efb\u4f55\u5176\u4ed6\u5c40\u90e8\u53d8\u91cf\uff0c\u5219\u8be5\u53d8\u91cf\u5c06\u5728 getName() '\u7ed3\u675f\u65f6\u9500\u6bc1\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u60ac\u7a7a\u6307\u9488\u7ed9\u8c03\u7528\u8005\u3002\u4f46\u662f\uff0c\u56e0\u4e3a\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u5177\u6709\u9759\u6001\u6301\u7eed\u65f6\u95f4\uff0c\u6240\u4ee5\u5f53 getName() \u7ec8\u6b62\u65f6\uff0c \"Alex\" \u4e0d\u4f1a\u88ab\u9500\u6bc1\uff0c\u56e0\u6b64\u8c03\u7528\u8005\u4ecd\u7136\u53ef\u4ee5\u8bbf\u95ee\u5b83\u3002 C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u5728\u5f88\u591a\u65e7\u7684\u6216\u5e95\u5c42\u4ee3\u7801\u4e2d\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u5360\u7528\u7684\u5185\u5b58\u975e\u5e38\u5c0f\u3002\u73b0\u4ee3\u4ee3\u7801\u5e94\u8be5\u66f4\u503e\u5411\u4e8e\u4f7f\u7528 std::string \u548c std::string_view \uff0c\u56e0\u4e3a\u5b83\u4eec\u63d0\u4f9b\u4e86\u5b89\u5168\u4e14\u6613\u7528\u7684\u5b57\u7b26\u4e32\u8bbf\u95ee\u3002 std::cout \u548c\u5b57\u7b26\u6307\u9488 \u5b66\u5230\u8fd9\u4e00\u7ae0\u7684\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u5df2\u7ecf\u6ce8\u610f\u5230\uff0c std::cout \u53ef\u4ee5\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6307\u9488\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int nArray [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; char cArray []{ \"Hello!\" }; const char * name { \"Alex\" }; std :: cout << nArray << '\\n' ; // nArray will decay to type int* std :: cout << cArray << '\\n' ; // cArray will decay to type char* std :: cout << name << '\\n' ; // name is already type char* return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u6253\u5370\u4e0b\u9762\u7684\u4fe1\u606f\uff1a 1 2 3 003AF738 Hello! Alex \u4e3a\u4ec0\u4e48\u6574\u578b\u6570\u7ec4\u6253\u5370\u7684\u5c31\u662f\u5730\u5740\uff0c\u800c\u5b57\u7b26\u6570\u7ec4\u5c31\u80fd\u6253\u5370\u5b57\u7b26\u4e32\uff1f \u8fd9\u662f\u56e0\u4e3a std::cout \u53ef\u4ee5\u63a8\u65ad\u4f60\u7684\u610f\u56fe\u3002\u5982\u679c\u4f60\u4f20\u5165\u4e86\u4e00\u4e2a\u975e\u5b57\u7b26\u7c7b\u578b\u7684\u6307\u9488\uff0c\u5b83\u4f1a\u6253\u5370\u8be5\u6307\u9488\u7684\u5185\u5bb9\uff08\u6307\u9488\u6301\u6709\u7684\u5730\u5740\u503c\uff09\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u4f20\u9012\u7684\u662f\u4e00\u4e2a char* \u6216 const char* \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u5b83\u4f1a\u5047\u5b9a\u4f60\u5e0c\u671b\u6253\u5370\u7684\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u56e0\u6b64\uff0c\u5b83\u6253\u5370\u7684\u662f\u6307\u9488\u6307\u5411\u7684\u5b57\u7b26\u4e32\u7684\u503c\u800c\u4e0d\u662f\u6307\u9488\u6301\u6709\u7684\u5730\u5740\u503c\u3002 \u5c3d\u7ba1\uff0c99%\u7684\u60c5\u51b5\u4e0b\u90fd\u662f\u6ca1\u95ee\u9898\u7684\uff0c\u4f46\u662f\u5176\u4ecd\u6709\u53ef\u80fd\u4ea7\u751f\u975e\u9884\u671f\u7684\u7ed3\u679c\u3002\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char c { 'Q' }; //\u6ce8\u610f\u8fd9\u91cc\u662f\u4e00\u4e2a\u5b57\u7b26 std :: cout << & c ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u5458\u6253\u7b97\u6253\u5370\u53d8\u91cfC\u7684\u5730\u5740\u3002\u7136\u800c\uff0c &c \u7684\u7c7b\u578b\u662f char* \uff0c\u6240\u4ee5 std::cout \u5c06\u5176\u4f5c\u4e3a\u5b57\u7b26\u4e32\u6253\u5370\uff01 \u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\uff0c\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 Q\u2560\u2560\u2560\u2560\u255c\u25614;\u00bf\u25a0A \u4e3a\u4ec0\u4e48\u8fd9\u6837\u5462\uff1f std::cout \u5047\u8bbe &c (\u7c7b\u578b\u4e3a char* )\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u6240\u4ee5\u5b83\u6253\u5370\u4e86\u201cQ\u201d\uff0c\u7136\u540e\u7ee7\u7eed\u6253\u5370\u63a5\u4e0b\u6765\u5185\u5b58\u4e2d\uff08\u4e00\u5806\u5783\u573e\u503c\uff09\u3002\u6700\u7ec8\uff0c\u5b83\u9047\u5230\u4e86\u4fdd\u5b580\u503c\u7684\u5185\u5b58\u4e2d\uff0c\u5e76\u5c06\u5176\u89e3\u91ca\u4e3a\u7a7a\u7ec8\u6b62\u7b26\u3002\u4f60\u770b\u5230\u7684\u53ef\u80fd\u4f1a\u4e0d\u540c\uff0c\u8fd9\u53d6\u51b3\u4e8e\u53d8\u91cfC\u540e\u9762\u7684\u5185\u5b58\u3002 \u8fd9\u79cd\u60c5\u51b5\u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\u4e0d\u592a\u53ef\u80fd\u53d1\u751f(\u56e0\u4e3a\u60a8\u4e0d\u592a\u53ef\u80fd\u771f\u7684\u60f3\u6253\u5370\u5185\u5b58\u5730\u5740)\uff0c\u4f46\u5b83\u8bf4\u660e\u4e86\u4e8b\u60c5\u662f\u5982\u4f55\u5728\u5e55\u540e\u5de5\u4f5c\u7684\uff0c\u4ee5\u53ca\u7a0b\u5e8f\u662f\u5982\u4f55\u5728\u4e0d\u7ecf\u610f\u95f4\u504f\u79bb\u8f68\u9053\u7684\u3002","title":"11.10 - C \u98ce\u683c\u5b57\u7b26\u4e32\u7b26\u53f7\u5e38\u91cf"},{"location":"11-10-C-style-string-symbolic-constants/#c","text":"\u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u521d\u59cb\u5316\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char myName []{ \"Alex\" }; // \u56fa\u5b9a\u6570\u7ec4 std :: cout << myName << '\\n' ; return 0 ; } C++ \u4e5f\u652f\u6301\u4f7f\u7528\u6307\u9488\u6307\u5411\u7b26\u53f7\u5e38\u91cf\u6765\u521b\u5efaC\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { const char * myName { \"Alex\" }; // pointer to symbolic constant std :: cout << myName << '\\n' ; return 0 ; } \u5c3d\u7ba1\u4ece\u5c42\u5e8f\u8fd0\u884c\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u4e0a\u9762\u4e24\u5957\u4ee3\u7801\u7684\u7ed3\u679c\u662f\u4e00\u6837\u7684\uff0c\u4f46C++\u5728\u5904\u7406\u4e0a\u8ff0\u4e24\u79cd\u4ee3\u7801\u65f6\uff0c\u5176\u5185\u5b58\u5206\u914d\u7684\u5904\u7406\u7ec6\u8282\u662f\u4e0d\u540c\u7684\u3002 \u5bf9\u4e8e\u56fa\u5b9a\u957f\u5ea6\u6570\u7ec4\u7684\u4f8b\u5b50\u6765\u8bf4\uff0c\u7a0b\u5e8f\u4f1a\u5728\u5185\u5b58\u4e2d\u5206\u914d\u4e00\u4e2a\u957f\u5ea6\u4e3a5\u7684\u6570\u7ec4\uff0c\u540c\u65f6\u5c06\u5185\u5b58\u521d\u59cb\u5316\u4e3a \u201c Alex\\0 \u201d\uff0c\u56e0\u4e3a\u8be5\u5185\u5b58\u5df2\u7ecf\u88ab\u5206\u914d\u7ed9\u8be5\u6570\u7ec4\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u81ea\u7531\u5730\u4fee\u6539\u6570\u7ec4\u7684\u5185\u5bb9\u3002\u8fd9\u4e2a\u6570\u7ec4\u4f1a\u88ab\u5f53\u505a\u4e00\u822c\u7684\u5c40\u90e8\u53d8\u91cf\u6765\u5bf9\u5f85\uff0c\u56e0\u6b64\u5f53\u5b83 \u79bb\u5f00\u4f5c\u7528\u57df \u65f6\uff0c\u5176\u6240\u4f7f\u7528\u7684\u5185\u5b58\u4f1a\u88ab\u91ca\u653e\u4ee5\u4fbf\u5176\u4ed6\u53d8\u91cf\u4f7f\u7528\u3002 \u800c\u5bf9\u4e8e\u7b26\u53f7\u5e38\u91cf\u7684\u4f8b\u5b50\u4e2d\u6765\u8bf4\uff0c\u7f16\u8bd1\u5668\u5982\u4f55\u5904\u7406\u53d6\u51b3\u4e8e\u5177\u4f53\u5b9e\u73b0\u3002 \u4e00\u822c \u6765\u8bb2\uff0c\u7f16\u8bd1\u5668\u4f1a\u628a\u201c Alex\\0 \u201d \u653e\u5728\u67d0\u5904\u53ea\u8bfb\u5185\u5b58\uff0c\u7136\u540e\u4f7f\u7528\u4e00\u4e2a\u6307\u9488\u6307\u5411\u5b83\u3002\u56e0\u4e3a\u8be5\u5185\u5b58\u662f\u53ea\u8bfb\u7684\uff0c\u6240\u4ee5\u6700\u597d\u5c06\u8be5\u5b57\u7b26\u4e32\u8bbe\u7f6e\u4e3aconst\u3002 \u7f16\u8bd1\u5668\u4f1a\u51fa\u4e8e\u6027\u80fd\u4f18\u5316\u7684\u8003\u91cf\uff0c\u5c06\u591a\u4e2a\u76f8\u540c\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u5408\u5e76\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u503c\u3002\u4f8b\u5982\uff1a 1 2 const char * name1 { \"Alex\" }; const char * name2 { \"Alex\" }; \u8fd9\u662f\u4e24\u4e2a\u5177\u6709\u76f8\u540c\u503c\u7684\u4e0d\u540c\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u3002\u7f16\u8bd1\u5668\u53ef\u4ee5\u9009\u62e9\u5c06\u5b83\u4eec\u7ec4\u5408\u6210\u4e00\u4e2a\u5171\u4eab\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u503c\uff0c name1 \u548c name2 \u90fd\u6307\u5411\u76f8\u540c\u7684\u5730\u5740\u3002\u56e0\u6b64\uff0c\u5982\u679c name1 \u4e0d\u662f const \uff0c\u90a3\u4e48\u5bf9 name1 \u8fdb\u884c\u66f4\u6539\u4e5f\u4f1a\u5f71\u54cd name2 (\u8fd9\u53ef\u80fd\u662f\u610f\u6599\u4e4b\u5916\u7684)\u3002 \u7531\u4e8e\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u88ab\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\u7684\u56fa\u5b9a\u4f4d\u7f6e\uff0c\u56e0\u6b64\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u5177\u6709\u9759\u6001\u6301\u7eed\u65f6\u95f4\uff0c\u800c\u4e0d\u662f\u81ea\u52a8\u6301\u7eed\u65f6\u95f4(\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4eec\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u9500\u6bc1\uff0c\u800c\u4e0d\u662f\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u5757\u7ed3\u675f\u65f6\u9500\u6bc1)\u3002\u8fd9\u610f\u5473\u7740\u5f53\u6211\u4eec\u4f7f\u7528\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u65f6\uff0c\u6211\u4eec\u4e0d\u5fc5\u62c5\u5fc3\u4f5c\u7528\u57df\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u8fd9\u4e48\u505a\u662f\u53ef\u4ee5\u7684\uff1a 1 2 3 4 const char * getName () { return \"Alex\" ; } \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c getName() \u5c06\u8fd4\u56de\u4e00\u4e2a\u6307\u5411C\u98ce\u683c\u5b57\u7b26\u4e32 \"Alex\" \u7684\u6307\u9488\u3002\u5982\u679c\u8be5\u51fd\u6570\u6309\u5730\u5740\u8fd4\u56de\u4efb\u4f55\u5176\u4ed6\u5c40\u90e8\u53d8\u91cf\uff0c\u5219\u8be5\u53d8\u91cf\u5c06\u5728 getName() '\u7ed3\u675f\u65f6\u9500\u6bc1\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u60ac\u7a7a\u6307\u9488\u7ed9\u8c03\u7528\u8005\u3002\u4f46\u662f\uff0c\u56e0\u4e3a\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u5177\u6709\u9759\u6001\u6301\u7eed\u65f6\u95f4\uff0c\u6240\u4ee5\u5f53 getName() \u7ec8\u6b62\u65f6\uff0c \"Alex\" \u4e0d\u4f1a\u88ab\u9500\u6bc1\uff0c\u56e0\u6b64\u8c03\u7528\u8005\u4ecd\u7136\u53ef\u4ee5\u8bbf\u95ee\u5b83\u3002 C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u5728\u5f88\u591a\u65e7\u7684\u6216\u5e95\u5c42\u4ee3\u7801\u4e2d\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u5360\u7528\u7684\u5185\u5b58\u975e\u5e38\u5c0f\u3002\u73b0\u4ee3\u4ee3\u7801\u5e94\u8be5\u66f4\u503e\u5411\u4e8e\u4f7f\u7528 std::string \u548c std::string_view \uff0c\u56e0\u4e3a\u5b83\u4eec\u63d0\u4f9b\u4e86\u5b89\u5168\u4e14\u6613\u7528\u7684\u5b57\u7b26\u4e32\u8bbf\u95ee\u3002","title":"C \u98ce\u683c\u5b57\u7b26\u4e32\u5e38\u91cf"},{"location":"11-10-C-style-string-symbolic-constants/#stdcout","text":"\u5b66\u5230\u8fd9\u4e00\u7ae0\u7684\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u5df2\u7ecf\u6ce8\u610f\u5230\uff0c std::cout \u53ef\u4ee5\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6307\u9488\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int nArray [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; char cArray []{ \"Hello!\" }; const char * name { \"Alex\" }; std :: cout << nArray << '\\n' ; // nArray will decay to type int* std :: cout << cArray << '\\n' ; // cArray will decay to type char* std :: cout << name << '\\n' ; // name is already type char* return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u6253\u5370\u4e0b\u9762\u7684\u4fe1\u606f\uff1a 1 2 3 003AF738 Hello! Alex \u4e3a\u4ec0\u4e48\u6574\u578b\u6570\u7ec4\u6253\u5370\u7684\u5c31\u662f\u5730\u5740\uff0c\u800c\u5b57\u7b26\u6570\u7ec4\u5c31\u80fd\u6253\u5370\u5b57\u7b26\u4e32\uff1f \u8fd9\u662f\u56e0\u4e3a std::cout \u53ef\u4ee5\u63a8\u65ad\u4f60\u7684\u610f\u56fe\u3002\u5982\u679c\u4f60\u4f20\u5165\u4e86\u4e00\u4e2a\u975e\u5b57\u7b26\u7c7b\u578b\u7684\u6307\u9488\uff0c\u5b83\u4f1a\u6253\u5370\u8be5\u6307\u9488\u7684\u5185\u5bb9\uff08\u6307\u9488\u6301\u6709\u7684\u5730\u5740\u503c\uff09\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u4f20\u9012\u7684\u662f\u4e00\u4e2a char* \u6216 const char* \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u5b83\u4f1a\u5047\u5b9a\u4f60\u5e0c\u671b\u6253\u5370\u7684\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u56e0\u6b64\uff0c\u5b83\u6253\u5370\u7684\u662f\u6307\u9488\u6307\u5411\u7684\u5b57\u7b26\u4e32\u7684\u503c\u800c\u4e0d\u662f\u6307\u9488\u6301\u6709\u7684\u5730\u5740\u503c\u3002 \u5c3d\u7ba1\uff0c99%\u7684\u60c5\u51b5\u4e0b\u90fd\u662f\u6ca1\u95ee\u9898\u7684\uff0c\u4f46\u662f\u5176\u4ecd\u6709\u53ef\u80fd\u4ea7\u751f\u975e\u9884\u671f\u7684\u7ed3\u679c\u3002\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char c { 'Q' }; //\u6ce8\u610f\u8fd9\u91cc\u662f\u4e00\u4e2a\u5b57\u7b26 std :: cout << & c ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u5458\u6253\u7b97\u6253\u5370\u53d8\u91cfC\u7684\u5730\u5740\u3002\u7136\u800c\uff0c &c \u7684\u7c7b\u578b\u662f char* \uff0c\u6240\u4ee5 std::cout \u5c06\u5176\u4f5c\u4e3a\u5b57\u7b26\u4e32\u6253\u5370\uff01 \u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\uff0c\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 Q\u2560\u2560\u2560\u2560\u255c\u25614;\u00bf\u25a0A \u4e3a\u4ec0\u4e48\u8fd9\u6837\u5462\uff1f std::cout \u5047\u8bbe &c (\u7c7b\u578b\u4e3a char* )\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u6240\u4ee5\u5b83\u6253\u5370\u4e86\u201cQ\u201d\uff0c\u7136\u540e\u7ee7\u7eed\u6253\u5370\u63a5\u4e0b\u6765\u5185\u5b58\u4e2d\uff08\u4e00\u5806\u5783\u573e\u503c\uff09\u3002\u6700\u7ec8\uff0c\u5b83\u9047\u5230\u4e86\u4fdd\u5b580\u503c\u7684\u5185\u5b58\u4e2d\uff0c\u5e76\u5c06\u5176\u89e3\u91ca\u4e3a\u7a7a\u7ec8\u6b62\u7b26\u3002\u4f60\u770b\u5230\u7684\u53ef\u80fd\u4f1a\u4e0d\u540c\uff0c\u8fd9\u53d6\u51b3\u4e8e\u53d8\u91cfC\u540e\u9762\u7684\u5185\u5b58\u3002 \u8fd9\u79cd\u60c5\u51b5\u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\u4e0d\u592a\u53ef\u80fd\u53d1\u751f(\u56e0\u4e3a\u60a8\u4e0d\u592a\u53ef\u80fd\u771f\u7684\u60f3\u6253\u5370\u5185\u5b58\u5730\u5740)\uff0c\u4f46\u5b83\u8bf4\u660e\u4e86\u4e8b\u60c5\u662f\u5982\u4f55\u5728\u5e55\u540e\u5de5\u4f5c\u7684\uff0c\u4ee5\u53ca\u7a0b\u5e8f\u662f\u5982\u4f55\u5728\u4e0d\u7ecf\u610f\u95f4\u504f\u79bb\u8f68\u9053\u7684\u3002","title":"std::cout \u548c\u5b57\u7b26\u6307\u9488"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/","tags":["new","delete","dynamic-memory"],"text":"Key Takeaway \u52a8\u6001\u5185\u5b58\u5206\u914d\u7684\u5fc5\u8981\u6027 C++ \u652f\u6301\u4e09\u79cd\u57fa\u672c\u7c7b\u578b\u7684\u5185\u5b58\u5206\u914d \uff0c\u4f60\u5e94\u8be5\u5df2\u7ecf\u89c1\u8fc7\u5176\u4e2d\u7684\u4e24\u79cd\u4e86\uff1a \u9759\u6001\u5185\u5b58\u5206\u914d \u53d1\u751f\u5728\u9759\u6001\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u521b\u5efa\u65f6\u3002\u5f53\u7a0b\u5e8f\u542f\u52a8\u65f6\uff0c\u6b64\u7c7b\u53d8\u91cf\u7684\u5185\u5b58\u4f1a\u88ab\u5206\u914d\u5e76\u6301\u7eed\u5230\u7a0b\u5e8f\u7ed3\u675f\uff0c\u8d2f\u7a7f\u7a0b\u5e8f\u7684\u6574\u4e2a\u751f\u547d\u5468\u671f\uff1b \u81ea\u52a8\u5185\u5b58\u5206\u914d \u53d1\u751f\u5728\u51fd\u6570\u5f62\u53c2\u548c\u5c40\u90e8\u53d8\u91cf\u521b\u5efa\u65f6\u3002\u8fd9\u4e9b\u53d8\u91cf\u7684\u5185\u5b58\u4f1a\u5728\u8fdb\u5165\u76f8\u5e94\u7684\u8bed\u53e5\u5757\u65f6\u88ab\u81ea\u52a8\u5206\u914d\uff0c\u800c\u5f53\u8bed\u53e5\u5757\u9000\u51fa\u65f6\u5219\u4f1a\u81ea\u52a8\u91ca\u653e\uff1b \u52a8\u6001\u5185\u5b58\u5206\u914d \u5219\u662f\u672c\u8282\u8bfe\u8981\u4ecb\u7ecd\u7684\u3002 \u9759\u6001\u5185\u5b58\u5206\u914d\u548c\u81ea\u52a8\u5185\u5b58\u5206\u914d\u6709\u4e24\u4e2a\u5171\u540c\u70b9\uff1a \u53d8\u91cf\u548c\u6570\u7ec4\u7684\u5927\u5c0f\u5fc5\u987b\u5728 \u7f16\u8bd1\u65f6 \u5c31\u5df2\u77e5\uff1b \u5185\u5b58\u7684\u5206\u914d\u548c\u91ca\u653e\u662f\u81ea\u52a8\u8fdb\u884c\u7684\uff08\u5728\u53d8\u91cf\u5b9e\u4f8b\u5316\u548c\u9500\u6bc1\u65f6\uff09\u3002 \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u662f\u5df2\u7ecf\u8db3\u591f\u4e86\u3002\u4f46\u662f\uff0c\u6709\u65f6\u5019\u8fd9\u4e24\u4e2a\u7279\u70b9\u4f1a\u5e26\u6765\u9650\u5236\uff0c\u901a\u5e38\u662f\u5728\u5904\u7406\u5916\u90e8(\u7528\u6237\u6216\u6587\u4ef6)\u8f93\u5165\u65f6\u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u80fd\u60f3\u4f7f\u7528\u4e00\u4e2a\u5b57\u7b26\u4e32\u6765\u4fdd\u5b58\u67d0\u4eba\u7684\u540d\u5b57\uff0c\u4f46\u5728\u7528\u6237\u8f93\u5165\u540d\u5b57\u4e4b\u524d\uff0c\u6211\u4eec\u5e76\u4e0d\u77e5\u9053\u540d\u5b57\u6709\u591a\u957f\u3002\u6216\u8005\u6211\u4eec\u53ef\u80fd\u60f3\u4ece\u78c1\u76d8\u8bfb\u5165\u4e00\u4e9b\u8bb0\u5f55\uff0c\u4f46\u4e8b\u5148\u4e0d\u77e5\u9053\u6709\u591a\u5c11\u8bb0\u5f55\u3002\u6216\u8005\u6211\u4eec\u6b63\u5728\u521b\u9020\u4e00\u6b3e\u602a\u7269\u6570\u91cf\u52a8\u6001\u53d8\u5316\u7684\u6e38\u620f(\u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\uff0c\u4e00\u4e9b\u602a\u7269\u4f1a\u6b7b\u4ea1\uff0c\u65b0\u602a\u7269\u4f1a\u51fa\u73b0)\u3002 \u5982\u679c\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u5c31\u58f0\u660e\u53d8\u91cf\u6216\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u90a3\u6211\u4eec\u80fd\u505a\u7684\u5c31\u662f\u5c1d\u8bd5\u731c\u6d4b\u9700\u8981\u7684\u53d8\u91cf\u7684\u5927\u5c0f\u7684\u6700\u5927\u503c\u5e76\u671f\u671b\u5b9e\u9645\u4f7f\u7528\u4e2d\u8be5\u5185\u5b58\u662f\u8db3\u591f\u7684\uff1a 1 2 3 4 char name [ 25 ]; // \u671f\u671b\u7528\u6237\u7684\u59d3\u540d\u4e0d\u8d85\u8fc725\u4e2a\u5b57\u7b26 Record record [ 500 ]; // \u5e0c\u671b\u6570\u636e\u7684\u8bb0\u5f55\u6570\u5c0f\u4e8e500\u6761 Monster monster [ 40 ]; // \u6700\u591a 40 \u4e2a\u602a\u7269 Polygon rendering [ 30000 ]; // 3d \u6e32\u67d3\u4e0d\u8d85\u8fc73\u4e07\u4e2a\u591a\u8fb9\u5f62 \u8fd9\u79cd\u89e3\u51b3\u65b9\u6848\u662f\u5f88\u5dee\u52b2\u7684\uff0c\u539f\u56e0\u6709\u56db\u6761\uff1a \u9996\u5148\uff0c\u5982\u679c\u5b9e\u9645\u6ca1\u6709\u4f7f\u7528\u8be5\u53d8\u91cf\uff0c\u5219\u4f1a\u5bfc\u81f4\u5185\u5b58\u6d6a\u8d39\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u4e3a\u6bcf\u4e2a\u540d\u79f0\u5206\u914d25\u4e2a\u5b57\u7b26\uff0c\u4f46\u540d\u79f0\u5e73\u5747\u53ea\u670912\u4e2a\u5b57\u7b26\u957f\uff0c\u90a3\u4e48\u7533\u8bf7\u7684\u5185\u5b58\u662f\u5b9e\u9645\u4f7f\u7528\u7684\u4e24\u500d\u591a\u3002\u6216\u8005\uff0c\u5bf9\u4e8e\u4e0a\u9762 rendering \u6570\u7ec4\u7684\u4f8b\u5b50\u6765\u8bf4\uff1a\u5982\u679c\u6e32\u67d3\u53ea\u4f7f\u752810000\u4e2a\u591a\u8fb9\u5f62\uff0c\u90a3\u4e48\u5c06\u670920000\u4e2a\u591a\u8fb9\u5f62\u7684\u5185\u5b58\u88ab\u989d\u5916\u7533\u8bf7\u5355\u6ca1\u6709\u88ab\u4f7f\u7528! \u7b2c\u4e8c\uff0c\u6211\u4eec\u5982\u4f55\u5224\u65ad\u54ea\u4e9b\u5185\u5b58\u4f4d\u5b9e\u9645\u4e0a\u88ab\u4f7f\u7528\u4e86\u5462\uff1f\u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0c\u8fd9\u5f88\u7b80\u5355\uff1a\u4ee5 \\0 \u5f00\u5934\u7684\u5b57\u7b26\u4e32\u663e\u7136\u6ca1\u6709\u88ab\u4f7f\u7528\u3002\u4f46\u662f\u5bf9\u4e8e monster[24] \u6765\u8bf4\u5462\uff1f\u5b83\u4ecd\u7136\u5728\u88ab\u4f7f\u7528\u5417\uff1f\u6211\u4eec\u9700\u8981\u67d0\u79cd\u65b9\u6cd5\u6765\u533a\u5206\u4ecd\u7136\u5728\u4f7f\u7528\u7684\u53d8\u91cf\u548c\u4e0d\u518d\u88ab\u4f7f\u7528\u7684\u53d8\u91cf\uff0c\u8fd9\u589e\u52a0\u4e86\u590d\u6742\u6027\uff0c\u5e76\u53ef\u80fd\u4f1a\u5360\u7528\u989d\u5916\u7684\u5185\u5b58\u3002 \u7b2c\u4e09\uff0c\u5927\u591a\u6570\u666e\u901a\u53d8\u91cf(\u5305\u62ec\u56fa\u5b9a\u6570\u7ec4)\u88ab\u5206\u914d\u5230\u79f0\u4e3a \u6808 \u7684\u5185\u5b58\u4e2d\u3002\u7a0b\u5e8f\u7684\u5806\u6808\u80fd\u591f\u4f7f\u7528\u7684\u5185\u5b58\u901a\u5e38\u975e\u5e38\u5c0f\u2014\u2014Visual Studio \u9ed8\u8ba4\u5806\u6808\u5927\u5c0f\u4e3a 1MB\u3002\u5982\u679c\u8d85\u8fc7\u8fd9\u4e2a\u5927\u5c0f\uff0c\u5c31\u4f1a\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5c31\u4f1a\u505c\u6b62\u7a0b\u5e8f\u3002 \u5728 Visual Studio \u4e2d\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\u65f6\u53d1\u751f\u7684\u60c5\u51b5\uff1a 1 2 3 4 int main () { int array [ 1000000 ]; // allocate 1 million integers (probably 4MB of memory) } \u5c06\u5185\u5b58\u9650\u5236\u4e3a1MB\u5bf9\u4e8e\u8bb8\u591a\u7a0b\u5e8f\u6765\u8bf4\u90fd\u662f\u6709\u95ee\u9898\u7684\uff0c\u7279\u522b\u662f\u90a3\u4e9b\u5904\u7406\u56fe\u5f62\u7684\u7a0b\u5e8f\u3002\u5728Visual Studio\u4e2d\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\u65f6\u53d1\u751f\u7684\u60c5\u51b5: \u7b2c\u56db\uff0c\u4e5f\u662f\u6700\u91cd\u8981\u7684\uff0c\u5b83\u4f1a\u5bfc\u81f4\u4eba\u4e3a\u7684\u9650\u5236\uff0c\u5e76\u53ef\u80fd\u5bfc\u81f4\u6ea2\u51fa\u3002\u5982\u679c\u7528\u6237\u8bd5\u56fe\u4ece\u78c1\u76d8\u8bfb\u5165600\u6761\u8bb0\u5f55\uff0c\u4f46\u6211\u4eec\u53ea\u5206\u914d\u4e86\u6700\u591a500\u6761\u8bb0\u5f55\u7684\u5185\u5b58\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u60c5\u51b5\uff1f\u6b64\u65f6\u8981\u4e48\u7ed9\u7528\u6237\u53d1\u9001\u4e00\u4e2a\u9519\u8bef\uff0c\u8981\u6c42\u5176\u53ea\u8bfb\u53d6500\u6761\u8bb0\u5f55\uff0c\u8981\u4e48(\u5728\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u6839\u672c\u4e0d\u5904\u7406\u8fd9\u79cd\u60c5\u51b5) record \u6570\u7ec4\u6ea2\u51fa\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u8fd9\u4e9b\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7\u52a8\u6001\u5185\u5b58\u5206\u914d\u8f7b\u677e\u89e3\u51b3\u3002 \u52a8\u6001\u5185\u5b58\u5206\u914d \u662f\u8fd0\u884c\u7a0b\u5e8f\u5728\u9700\u8981\u65f6\u5411\u64cd\u4f5c\u7cfb\u7edf\u8bf7\u6c42\u5185\u5b58\u7684\u4e00\u79cd\u65b9\u6cd5\u3002\u8fd9\u4e2a\u5185\u5b58\u4e0d\u662f\u6765\u81ea\u7a0b\u5e8f\u6709\u9650\u7684\u5806\u6808\u5185\u5b58\u2014\u2014\u76f8\u53cd\uff0c\u5b83\u662f\u4ece\u4e00\u4e2a\u66f4\u5927\u7684\u5185\u5b58\u6c60\u4e2d\u5206\u914d\u7684\uff0c\u7531\u64cd\u4f5c\u7cfb\u7edf\u7ba1\u7406\uff0c\u79f0\u4e3a \u5806 \u3002\u5728\u73b0\u4ee3\u673a\u5668\u4e0a\uff0c\u5806\u7684\u5927\u5c0f\u53ef\u4ee5\u8fbe\u5230\u5343\u5146\u5b57\u8282\u3002 \u5355\u4e00\u53d8\u91cf\u7684\u52a8\u6001\u5185\u5b58\u5206\u914d \u4e3a \u4e00\u4e2a \u53d8\u91cf\u5206\u914d\u5185\u5b58\uff0c\u6211\u4eec\u4f7f\u7528 new \u7684\u6807\u91cf\u5f62\u5f0f\uff1a 1 new int ; // dynamically allocate an integer (and discard the result) \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5411\u64cd\u4f5c\u7cfb\u7edf\u7533\u8bf7\u4e00\u4e2a\u6574\u578b\u7684\u5185\u5b58\u3002 new \u8fd0\u7b97\u7b26\u4f1a\u4f7f\u7528\u7533\u8bf7\u6765\u7684\u5185\u5b58\u521b\u5efa\u8fd9\u4e2a\u5bf9\u8c61\uff0c\u7136\u540e\u8fd4\u56de\u6307\u5411\u8be5\u5185\u5b58\u7684\u6307\u9488\u3002 \u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8fd4\u56de\u503c\u8d4b\u503c\u7ed9\u4e00\u4e2a\u6307\u9488\u53d8\u91cf\uff0c\u5e76\u901a\u8fc7\u5b83\u5728\u540e\u7eed\u7684\u7a0b\u5e8f\u4e2d\u8bbf\u95ee\u8fd9\u5757\u5185\u5b58\u3002 1 int * ptr { new int }; // dynamically allocate an integer and assign the address to ptr so we can access it later \u5728\u540e\u7eed\u7684\u7a0b\u5e8f\u4e2d\u901a\u8fc7 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u8bbf\u95ee\u8be5\u5185\u5b58\uff1a 1 * ptr = 7 ; // assign value of 7 to allocated memory \u5982\u679c\u4f60\u4e4b\u524d\u4e0d\u77e5\u9053\u6307\u9488\u6709\u4ec0\u4e48\u7528\uff0c\u90a3\u4e48\u73b0\u5728\u5e94\u8be5\u6e05\u695a\u4e86\uff0c\u81f3\u5c11\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u6ca1\u6709\u4e00\u4e2a\u6307\u9488\u6765\u4fdd\u5b58\u521a\u521a\u5206\u914d\u7684\u5185\u5b58\u5730\u5740\uff0c\u6211\u4eec\u5c31\u6ca1\u6709\u529e\u6cd5\u8bbf\u95ee\u521a\u521a\u5206\u914d\u7ed9\u6211\u4eec\u7684\u5185\u5b58! \u52a8\u6001\u5185\u5b58\u5206\u914d\u7684\u539f\u7406\u662f\u4ec0\u4e48\uff1f \u8ba1\u7b97\u673a\u4e2d\u7684\u5185\u5b58\u53ef\u4f9b\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u3002\u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u5c06\u7a0b\u5e8f\u52a0\u8f7d\u5230\u4e00\u5757\u5185\u5b58\u4e2d\u3002\u8fd9\u5757\u5185\u5b58\u88ab\u4f60\u7684\u7a0b\u5e8f\u5206\u5272\u6210\u51e0\u4e2a\u4e0d\u540c\u7684\u90e8\u5206\uff0c\u6bcf\u4e2a\u90e8\u5206\u4e5f\u90fd\u6709\u5176\u7279\u5b9a\u7684\u4f5c\u7528\u3002\u5176\u4e2d\u4e00\u5c0f\u90e8\u5206\u5305\u542b\u4f60\u7684\u7a0b\u5e8f\u4ee3\u7801\uff0c\u53e6\u5916\u4e00\u5c0f\u90e8\u5206\u5219\u7528\u4e8e\u7a0b\u5e8f\u6b63\u5e38\u8fd0\u884c\u65f6\u4f7f\u7528\uff08\u8bb0\u5f55\u51fd\u6570\u8c03\u7528\u3001\u521b\u5efa\u548c\u9500\u6bc1\u5168\u5c40\u53d8\u91cf\u548c\u4e34\u65f6\u53d8\u91cf\uff0c\u7b49\u7b49\uff09\u3002\u6211\u4eec\u4f1a\u5728\u540e\u9762\u8fdb\u884c\u66f4\u8be6\u7ec6\u4ecb\u7ecd\u3002\u4f46\u662f\uff0c\u8fd8\u6709\u5f88\u591a\u5f88\u591a\u7684\u5185\u5b58\u5c31\u53ea\u662f\u653e\u5728\u90a3\u91cc\uff0c\u7b49\u5f85\u4f60\u7684\u7a0b\u5e8f\u8bf7\u6c42\u4f7f\u7528\u3002 \u548c\u9759\u6001\u5185\u5b58\u3001\u81ea\u52a8\u5185\u5b58\u4e0d\u540c\u7684\u662f\uff0c\u7a0b\u5e8f\u81ea\u5df1\u9700\u8981\u8d1f\u8d23\u8bf7\u6c42\u548c\u91ca\u653e\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u3002 \u52a8\u6001\u5206\u914d\u53d8\u91cf\u7684\u521d\u59cb\u5316 \u5f53\u4f60\u52a8\u6001\u5206\u914d\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7 \u76f4\u63a5\u521d\u59cb\u5316 \u6216 \u7edf\u4e00\u521d\u59cb\u5316 \u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\uff1a 1 2 int * ptr1 { new int ( 5 ) }; // \u76f4\u63a5\u521d\u59cb\u5316 int * ptr2 { new int { 6 } }; // \u7edf\u4e00\u521d\u59cb\u5316 \u5220\u9664\u5355\u4e00\u53d8\u91cf \u5728\u4f7f\u7528\u5b8c\u52a8\u6001\u5206\u914d\u7684\u53d8\u91cf\u65f6\uff0c\u6211\u4eec\u9700\u8981\u663e\u5f0f\u5730\u544a\u8bc9C++\u91ca\u653e\u5185\u5b58\u4ee5\u4fbf\u91cd\u7528\u3002\u5bf9\u4e8e\u5355\u4e2a\u53d8\u91cf\uff0c\u8fd9\u662f\u901a\u8fc7\u6807\u91cf(\u975e\u6570\u7ec4)\u5f62\u5f0f\u7684 delete \u64cd\u4f5c\u7b26\u6765\u5b8c\u6210\u7684\uff1a 1 2 3 // assume ptr has previously been allocated with operator new delete ptr ; // return the memory pointed to by ptr to the operating system ptr = nullptr ; // set ptr to be a null pointer \u5220\u9664\u5185\u5b58\u662f\u4ec0\u4e48\u610f\u601d\uff1f delete \u8fd0\u7b97\u7b26\u5e76\u6ca1\u6709 \u5b9e\u9645\u4e0a \u5220\u9664\u4efb\u4f55\u4e1c\u897f\uff0c\u5b83\u53ea\u662f\u5c06\u6307\u9488\u6307\u5411\u7684\u5185\u5b58\u8fd8\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u7f62\u4e86\u3002\u64cd\u4f5c\u7cfb\u7edf\u968f\u5373\u4fbf\u53ef\u4ee5\u5c06\u8be5\u5185\u5b58\u5206\u914d\u7ed9\u5176\u4ed6\u5e94\u7528\u4f7f\u7528\uff08\u6216\u8005\u7ed9\u5f53\u524d\u5e94\u7528\u4f7f\u7528\u4e5f\u53ef\u4ee5\uff09\u3002 \u5c3d\u7ba1\u770b\u4e0a\u53bb\u6211\u4eec\u662f\u5728 delete \u4e00\u4e2a\u53d8\u91cf\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\uff01\u8be5\u6307\u9488\u53d8\u91cf\u4ecd\u7136\u5904\u4e8e\u8be5\u4f5c\u7528\u57df\uff0c\u800c\u4e14\u4e5f\u53ef\u4ee5\u88ab\u8d4b\u4e88\u65b0\u503c\u3002 \u6ce8\u610f\uff0c\u5220\u9664\u4e0d\u6307\u5411\u52a8\u6001\u5206\u914d\u5185\u5b58\u7684\u6307\u9488\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e0d\u597d\u7684\u4e8b\u60c5\u53d1\u751f\u3002 \u60ac\u5782\u6307\u9488 C++\u6ca1\u6709\u89c4\u5b9a\u88ab\u91ca\u653e\u7684\u5185\u5bb9\u5176\u5185\u5bb9\u5e94\u8be5\u662f\u4ec0\u4e48\uff0c\u4e5f\u6ca1\u6709\u89c4\u5b9a\u88ab\u5220\u9664\u7684\u6307\u9488\u7684\u503c\u5e94\u8be5\u662f\u4ec0\u4e48\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd4\u56de\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u7684\u5185\u5b58\u5c06\u5305\u542b\u5b83\u5728\u8fd4\u56de\u4e4b\u524d\u7684\u76f8\u540c\u503c\uff0c\u5e76\u4e14\u6307\u9488\u5c06\u6307\u5411\u73b0\u5728\u91ca\u653e\u7684\u5185\u5b58\u3002 \u6267\u884c\u5df2\u7ecf\u88ab\u91ca\u653e\u7684\u5185\u5b58\u7684\u6307\u9488\uff0c\u79f0\u4e3a \u60ac\u5782 \u6307\u9488\u3002\u95f4\u63a5\u8bbf\u95ee\u6216\u5220\u9664\u60ac\u5782\u6307\u9488\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int * ptr { new int }; // \u52a8\u6001\u5206\u914d\u4e00\u4e2a\u6574\u578b * ptr = 7 ; // \u5411\u8be5\u5185\u5b58\u8d4b\u4e00\u4e2a\u503c delete ptr ; // \u5185\u5b58\u8fd8\u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff0cptr\u73b0\u5728\u662f\u60ac\u5782\u6307\u9488 std :: cout << * ptr ; // \u95f4\u63a5\u8bbf\u95ee\u60ac\u5782\u6307\u9488\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a delete ptr ; // \u518d\u6b21\u91ca\u653e\u5df2\u7ecf\u88ab\u91ca\u653e\u7684\u5185\u5b58\u4e5f\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u4e4b\u524d\u5206\u914d\u7ed9\u5df2\u5206\u914d\u5185\u5b58\u7684\u503c7\u53ef\u80fd\u4ecd\u7136\u5b58\u5728\uff0c\u4f46\u8be5\u5185\u5b58\u5730\u5740\u7684\u503c\u53ef\u80fd\u5df2\u7ecf\u53d1\u751f\u4e86\u53d8\u5316\u3002\u5185\u5b58\u4e5f\u6709\u53ef\u80fd\u88ab\u5206\u914d\u7ed9\u53e6\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f(\u6216\u64cd\u4f5c\u7cfb\u7edf\u81ea\u5df1\u4f7f\u7528)\uff0c\u8bd5\u56fe\u8bbf\u95ee\u8be5\u5185\u5b58\u5c06\u5bfc\u81f4\u64cd\u4f5c\u7cfb\u7edf\u5173\u95ed\u7a0b\u5e8f\u3002 \u91ca\u653e\u5185\u5b58\u53ef\u80fd\u4f1a\u5bfc\u81f4\u591a\u4e2a\u60ac\u5782\u6307\u9488\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int * ptr { new int {} }; // \u52a8\u6001\u5206\u914d\u4e00\u4e2a\u6574\u578b int * otherPtr { ptr }; // otherPtr \u6307\u5411\u76f8\u540c\u7684\u5185\u5b58 delete ptr ; // \u5185\u5b58\u8fd8\u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff0cptr,otherPtr \u73b0\u5728\u662f\u60ac\u5782\u6307\u9488 ptr = nullptr ; // ptr \u73b0\u5728\u662fnullptr // \u4f46\u662f, otherPtr \u4ecd\u7136\u662f\u60ac\u5782\u6307\u9488 return 0 ; } \u901a\u8fc7\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u907f\u514d\u4e0a\u8ff0\u95ee\u9898\u3002 \u9996\u5148\uff0c\u5c3d\u91cf\u907f\u514d\u6709\u591a\u4e2a\u6307\u9488\u6307\u5411\u540c\u4e00\u5757\u52a8\u6001\u5185\u5b58\u3002\u5982\u679c\u8fd9\u662f\u4e0d\u53ef\u80fd\u7684\uff0c\u8981\u6e05\u695a\u54ea\u4e9b\u6307\u9488\u201c\u62e5\u6709\u201d\u5185\u5b58(\u5e76\u8d1f\u8d23\u5220\u9664\u5b83)\uff0c\u54ea\u4e9b\u6307\u9488\u53ea\u662f\u5728\u8bbf\u95ee\u5b83\u3002 \u5176\u6b21\uff0c\u5728\u5220\u9664\u6307\u9488\u65f6\uff0c\u5982\u679c\u8be5\u6307\u9488\u4e0d\u4f1a\u7acb\u5373\u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u5219\u5c06\u8be5\u6307\u9488\u8bbe\u7f6e\u4e3anullptr\u3002\u6211\u4eec\u5c06\u66f4\u591a\u5730\u8ba8\u8bba\u7a7a\u6307\u9488\uff0c\u4ee5\u53ca\u5b83\u4eec\u4e3a\u4ec0\u4e48\u6709\u7528\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c06\u5df2\u5220\u9664\u7684\u6307\u9488\u8bbe\u7f6e\u4e3anullptr\uff0c\u9664\u975e\u5b83\u4eec\u968f\u540e\u5c06\u7acb\u5373\u8d85\u51fa\u4f5c\u7528\u57df\u3002 new \u8fd0\u7b97\u7b26\u53ef\u80fd\u6267\u884c\u5931\u8d25 \u5f53\u5411\u64cd\u4f5c\u7cfb\u7edf\u8bf7\u6c42\u5185\u5b58\u65f6\uff0c\u5728\u6781\u5c11\u6570\u60c5\u51b5\u4e0b\uff0c\u64cd\u4f5c\u7cfb\u7edf\u53ef\u80fd\u5df2\u7ecf\u6ca1\u6709\u4efb\u4f55\u53ef\u7528\u7684\u5185\u5b58\u3002 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5982\u679c new \u5931\u8d25\uff0c\u5219\u629b\u51fa bad_alloc \u5f02\u5e38\u3002\u5982\u679c\u8fd9\u4e2a\u5f02\u5e38\u6ca1\u6709\u5f97\u5230\u6b63\u786e\u5904\u7406(\u5b83\u4e0d\u4f1a\u5f97\u5230\u6b63\u786e\u5904\u7406\uff0c\u56e0\u4e3a\u6211\u4eec\u8fd8\u6ca1\u6709\u8ba8\u8bba\u5f02\u5e38\u6216\u5f02\u5e38\u5904\u7406)\uff0c\u7a0b\u5e8f\u5c06\u7ec8\u6b62(\u5d29\u6e83)\uff0c\u5e76\u51fa\u73b0\u4e00\u4e2a\u672a\u5904\u7406\u7684\u5f02\u5e38\u9519\u8bef\u3002 \u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u8ba9 new \u629b\u51fa\u5f02\u5e38(\u6216\u8ba9\u7a0b\u5e8f\u5d29\u6e83)\u662f\u4e0d\u53ef\u53d6\u7684\uff0c\u56e0\u6b64\uff0c\u5982\u679c\u65e0\u6cd5\u5206\u914d\u5185\u5b58\uff0c\u53ef\u4ee5\u4f7f\u7528\u53e6\u4e00\u79cd\u5f62\u5f0f\u7684 new \u6765\u544a\u8bc9 new \u8fd4\u56de\u7a7a\u6307\u9488\u3002\u8fd9\u662f\u901a\u8fc7\u5728 new \u5173\u952e\u5b57\u548c\u5206\u914d\u7c7b\u578b\u4e4b\u95f4\u6dfb\u52a0\u5e38\u91cf std::nothrow \u6765\u5b9e\u73b0\u7684\uff1a 1 int * value { new ( std :: nothrow ) int }; // \u5982\u679c\u6ca1\u6709\u5206\u914d\u5230\u5185\u5b58\uff0c\u5219\u6307\u9488\u88ab\u7f6e\u4e3anull \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c new \u5206\u914d\u5185\u5b58\u5931\u8d25\uff0c\u5b83\u5c06\u8fd4\u56de\u4e00\u4e2a\u7a7a\u6307\u9488\u800c\u4e0d\u662f\u5df2\u5206\u914d\u5185\u5b58\u7684\u5730\u5740\u3002 \u6ce8\u610f\uff0c\u5982\u679c\u4f60\u5c1d\u8bd5\u901a\u8fc7\u6b64\u6307\u9488\u8fdb\u884c\u95f4\u63a5\u64cd\u4f5c\uff0c\u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a (\u6700\u6709\u53ef\u80fd\u7684\u662f\u7a0b\u5e8f\u5d29\u6e83)\u3002\u56e0\u6b64\uff0c\u6700\u4f73\u5b9e\u8df5\u662f\u5728\u4f7f\u7528\u5206\u914d\u7684\u5185\u5b58\u4e4b\u524d\u68c0\u67e5\u6240\u6709\u5185\u5b58\u8bf7\u6c42\uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u5b9e\u9645\u4e0a\u6210\u529f\u4e86\u3002 1 2 3 4 5 6 int * value { new ( std :: nothrow ) int {} }; // ask for an integer's worth of memory if ( ! value ) // handle case where new returned null { // Do error handling here std :: cerr << \"Could not allocate memory \\n \" ; } \u56e0\u4e3a\u901a\u8fc7 new \u7533\u8bf7\u5185\u5b58\u5f88\u5c11\u4f1a\u5931\u8d25(\u5728\u5f00\u53d1\u73af\u5883\u4e2d\u51e0\u4e4e\u4ece\u4e0d\u5931\u8d25)\uff0c\u6240\u4ee5\u7ecf\u5e38\u4f1a\u5fd8\u8bb0\u6267\u884c\u6b64\u68c0\u67e5\uff01 \u7a7a\u6307\u9488\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d \u7a7a\u6307\u9488 (\u8bbe\u7f6e\u4e3a nullptr \u7684\u6307\u9488) \u5728\u5904\u7406\u52a8\u6001\u5185\u5b58\u5206\u914d\u65f6\u5f88\u6709\u7528\u3002\u5728\u52a8\u6001\u5185\u5b58\u5206\u914d\u7684\u4e0a\u4e0b\u6587\u4e2d\uff0c\u7a7a\u6307\u9488\u4ee3\u8868\u7740\u201d\u6ca1\u6709\u5185\u5b58\u88ab\u5206\u914d\u7ed9\u8fd9\u4e2a\u6307\u9488\u201c\u3002\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u6765\u5206\u914d\u5185\u5b58\uff1a 1 2 3 // If ptr isn't already allocated, allocate it if ( ! ptr ) ptr = new int ; \u5220\u9664\u7a7a\u6307\u9488\u6ca1\u6548\u679c\u3002\u56e0\u6b64\u6ca1\u5fc5\u8981\u8fd9\u4e48\u505a\uff1a 1 2 if ( ptr ) delete ptr ; \u76f4\u63a5\u8fd9\u4e48\u505a\u5c31\u53ef\u4ee5\uff1a 1 delete ptr ; \u5982\u679c ptr \u662f\u975e\u7a7a\u7684\uff0c\u90a3\u4e48\u5b83\u6307\u5411\u7684\u5185\u5b58\u5c06\u88ab\u91ca\u653e\uff0c\u5982\u679c ptr \u662f\u7a7a\uff0c\u5219\u4ec0\u4e48\u90fd\u4e0d\u4f1a\u53d1\u751f\u3002 \u5185\u5b58\u6cc4\u6f0f \u9664\u975e\u663e\u5f0f\u5730\u91ca\u653e\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u6216\u7a0b\u5e8f\u7ed3\u675f(\u5e76\u4e14\u64cd\u4f5c\u7cfb\u7edf\u6e05\u7406\u5b83\uff0c\u5047\u8bbe\u60a8\u7684\u64cd\u4f5c\u7cfb\u7edf\u8fd9\u4e48\u505a)\uff0c\u5426\u5219\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u4e00\u76f4\u5904\u4e8e\u88ab\u5206\u914d\u72b6\u6001\u3002\u4f46\u662f\uff0c\u7528\u4e8e\u4fdd\u5b58\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u5730\u5740\u7684\u6307\u9488\u9075\u5faa\u672c\u5730\u53d8\u91cf\u7684\u6b63\u5e38\u4f5c\u7528\u57df\u89c4\u5219\u3002\u8fd9\u79cd\u4e0d\u5339\u914d\u4f1a\u4ea7\u751f\u6709\u8da3\u7684\u95ee\u9898\u3002 \u8003\u8651\u4e0b\u9762\u7684\u51fd\u6570\uff1a 1 2 3 4 void doSomething () { int * ptr { new int {} }; } \u8fd9\u4e2a\u51fd\u6570\u52a8\u6001\u5206\u914d\u4e86\u4e00\u4e2a\u6574\u6570\uff0c\u4f46\u5e76\u6ca1\u6709\u7528 delete \u91ca\u653e\u5b83\u3002\u56e0\u4e3a\u6307\u9488\u53d8\u91cf\u53ea\u662f\u666e\u901a\u53d8\u91cf\uff0c\u5f53\u51fd\u6570\u7ed3\u675f\u65f6\uff0c ptr \u79bb\u5f00\u4f5c\u7528\u57df \u3002\u7531\u4e8e ptr \u662f\u4fdd\u5b58\u52a8\u6001\u5206\u914d\u7684\u6574\u6570\u5730\u5740\u7684\u552f\u4e00\u53d8\u91cf\uff0c\u5f53 ptr \u88ab\u9500\u6bc1\u65f6\uff0c\u5bf9\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u5c31\u6ca1\u6709\u66f4\u591a\u7684\u5f15\u7528\u4e86\u3002\u8fd9\u610f\u5473\u7740\u7a0b\u5e8f\u73b0\u5728\u201c\u4e22\u5931\u201d\u4e86\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u7684\u5730\u5740\u3002\u56e0\u6b64\uff0c\u65e0\u6cd5\u518d\u5220\u9664\u8fd9\u4e2a\u52a8\u6001\u5206\u914d\u7684\u6574\u6570\u4e86\u3002 \u8fd9\u4e2a\u73b0\u8c61\u79f0\u4e3a \u5185\u5b58\u6cc4\u6f0f \u3002\u5f53\u7a0b\u5e8f\u5728\u5c06\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u8fd4\u56de\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u4e4b\u524d\u4e22\u5931\u4e86\u5b83\u7684\u5730\u5740\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u5185\u5b58\u6cc4\u6f0f\u3002\u5f53\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\u65f6\uff0c\u7a0b\u5e8f\u5c06\u65e0\u6cd5\u5220\u9664\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\uff0c\u56e0\u4e3a\u5b83\u4e0d\u518d\u77e5\u9053\u5185\u5b58\u5730\u5740\u4e86\u3002\u64cd\u4f5c\u7cfb\u7edf\u4e5f\u4e0d\u80fd\u4f7f\u7528\u8be5\u5185\u5b58\uff0c\u56e0\u4e3a\u8be5\u5185\u5b58\u88ab\u8ba4\u4e3a\u662f\u7a0b\u5e8f\u4ecd\u5728\u4f7f\u7528\u4e2d\u3002 \u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u5185\u5b58\u6cc4\u6f0f\u4f1a\u6d88\u8017\u6389\u53ef\u7528\u7684\u5185\u5b58\uff0c\u4e0d\u4ec5\u4f7f\u8fd9\u4e2a\u7a0b\u5e8f\u53ef\u7528\u7684\u5185\u5b58\u51cf\u5c11\uff0c\u800c\u4e14\u4f7f\u5176\u4ed6\u7a0b\u5e8f\u53ef\u7528\u7684\u5185\u5b58\u51cf\u5c11\u3002\u6709\u4e25\u91cd\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\u7684\u7a0b\u5e8f\u4f1a\u8017\u5c3d\u6240\u6709\u53ef\u7528\u5185\u5b58\uff0c\u5bfc\u81f4\u6574\u4e2a\u673a\u5668\u8fd0\u884c\u7f13\u6162\u751a\u81f3\u5d29\u6e83\u3002\u53ea\u6709\u5728\u7a0b\u5e8f\u7ec8\u6b62\u540e\uff0c\u64cd\u4f5c\u7cfb\u7edf\u624d\u80fd\u6e05\u7406\u548c\u201c\u56de\u6536\u201d\u6240\u6709\u6cc4\u6f0f\u7684\u5185\u5b58\u3002 \u5c3d\u7ba1\u6307\u9488\u8d85\u51fa\u4f5c\u7528\u57df\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\uff0c\u4f46\u8fd8\u6709\u5176\u4ed6\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u7684\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6301\u6709\u52a8\u6001\u5206\u914d\u5185\u5b58\u5730\u5740\u7684\u6307\u9488\u88ab\u5206\u914d\u4e86\u53e6\u4e00\u4e2a\u503c\uff0c\u4e5f\u4f1a\u53d1\u751f\u5185\u5b58\u6cc4\u6f0f\uff1a 1 2 3 int value = 5 ; int * ptr { new int {} }; // allocate memory ptr = & value ; // old address lost, memory leak results \u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728\u91cd\u65b0\u8d4b\u503c\u4e4b\u524d\u5220\u9664\u6307\u9488\u6765\u89e3\u51b3\uff1a 1 2 3 4 int value { 5 }; int * ptr { new int {} }; // allocate memory delete ptr ; // return memory back to operating system ptr = & value ; // reassign pointer to address of value \u7c7b\u4f3c\u5730\uff0c\u91cd\u590d\u5206\u914d\u5185\u5b58\u4e5f\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\uff1a 1 2 int * ptr { new int {} }; ptr = new int {}; // old address lost, memory leak results \u4ece\u7b2c\u4e8c\u6b21\u5206\u914d\u5185\u5b58\u65f6\u8fd4\u56de\u7684\u5730\u5740\u5c06\u8986\u76d6\u7b2c\u4e00\u4e2a\u5206\u914d\u7684\u5730\u5740\u3002\u56e0\u6b64\uff0c\u7b2c\u4e00\u5757\u5185\u5b58\u6cc4\u6f0f\u4e86\uff01 \u7c7b\u4f3c\u5730\uff0c\u53ef\u4ee5\u901a\u8fc7\u786e\u4fdd\u5728\u91cd\u65b0\u8d4b\u503c\u4e4b\u524d\u5220\u9664\u6307\u9488\u6765\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002 \u7ed3\u8bba \u64cd\u4f5c\u7b26 new \u548c delete \u5141\u8bb8\u6211\u4eec\u4e3a\u7a0b\u5e8f\u52a8\u6001\u5206\u914d\u5355\u4e2a\u53d8\u91cf\u3002 \u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u5177\u6709 \u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u5e76\u4e14\u5728\u91ca\u653e\u5b83\u6216\u7a0b\u5e8f\u7ec8\u6b62\u4e4b\u524d\u5c06\u4fdd\u6301\u5206\u914d\u72b6\u6001\u3002 \u6ce8\u610f\u4e0d\u8981\u901a\u8fc7\u60ac\u5782\u6216\u7a7a\u6307\u9488\u6267\u884c\u95f4\u63a5\u8bbf\u95ee\u64cd\u4f5c\u3002 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u770b\u770b\u5982\u4f55\u4f7f\u7528 new \u548c delete \u6765\u5206\u914d\u548c\u5220\u9664\u6570\u7ec4\u3002","title":"11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/#_1","text":"C++ \u652f\u6301\u4e09\u79cd\u57fa\u672c\u7c7b\u578b\u7684\u5185\u5b58\u5206\u914d \uff0c\u4f60\u5e94\u8be5\u5df2\u7ecf\u89c1\u8fc7\u5176\u4e2d\u7684\u4e24\u79cd\u4e86\uff1a \u9759\u6001\u5185\u5b58\u5206\u914d \u53d1\u751f\u5728\u9759\u6001\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u521b\u5efa\u65f6\u3002\u5f53\u7a0b\u5e8f\u542f\u52a8\u65f6\uff0c\u6b64\u7c7b\u53d8\u91cf\u7684\u5185\u5b58\u4f1a\u88ab\u5206\u914d\u5e76\u6301\u7eed\u5230\u7a0b\u5e8f\u7ed3\u675f\uff0c\u8d2f\u7a7f\u7a0b\u5e8f\u7684\u6574\u4e2a\u751f\u547d\u5468\u671f\uff1b \u81ea\u52a8\u5185\u5b58\u5206\u914d \u53d1\u751f\u5728\u51fd\u6570\u5f62\u53c2\u548c\u5c40\u90e8\u53d8\u91cf\u521b\u5efa\u65f6\u3002\u8fd9\u4e9b\u53d8\u91cf\u7684\u5185\u5b58\u4f1a\u5728\u8fdb\u5165\u76f8\u5e94\u7684\u8bed\u53e5\u5757\u65f6\u88ab\u81ea\u52a8\u5206\u914d\uff0c\u800c\u5f53\u8bed\u53e5\u5757\u9000\u51fa\u65f6\u5219\u4f1a\u81ea\u52a8\u91ca\u653e\uff1b \u52a8\u6001\u5185\u5b58\u5206\u914d \u5219\u662f\u672c\u8282\u8bfe\u8981\u4ecb\u7ecd\u7684\u3002 \u9759\u6001\u5185\u5b58\u5206\u914d\u548c\u81ea\u52a8\u5185\u5b58\u5206\u914d\u6709\u4e24\u4e2a\u5171\u540c\u70b9\uff1a \u53d8\u91cf\u548c\u6570\u7ec4\u7684\u5927\u5c0f\u5fc5\u987b\u5728 \u7f16\u8bd1\u65f6 \u5c31\u5df2\u77e5\uff1b \u5185\u5b58\u7684\u5206\u914d\u548c\u91ca\u653e\u662f\u81ea\u52a8\u8fdb\u884c\u7684\uff08\u5728\u53d8\u91cf\u5b9e\u4f8b\u5316\u548c\u9500\u6bc1\u65f6\uff09\u3002 \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u662f\u5df2\u7ecf\u8db3\u591f\u4e86\u3002\u4f46\u662f\uff0c\u6709\u65f6\u5019\u8fd9\u4e24\u4e2a\u7279\u70b9\u4f1a\u5e26\u6765\u9650\u5236\uff0c\u901a\u5e38\u662f\u5728\u5904\u7406\u5916\u90e8(\u7528\u6237\u6216\u6587\u4ef6)\u8f93\u5165\u65f6\u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u80fd\u60f3\u4f7f\u7528\u4e00\u4e2a\u5b57\u7b26\u4e32\u6765\u4fdd\u5b58\u67d0\u4eba\u7684\u540d\u5b57\uff0c\u4f46\u5728\u7528\u6237\u8f93\u5165\u540d\u5b57\u4e4b\u524d\uff0c\u6211\u4eec\u5e76\u4e0d\u77e5\u9053\u540d\u5b57\u6709\u591a\u957f\u3002\u6216\u8005\u6211\u4eec\u53ef\u80fd\u60f3\u4ece\u78c1\u76d8\u8bfb\u5165\u4e00\u4e9b\u8bb0\u5f55\uff0c\u4f46\u4e8b\u5148\u4e0d\u77e5\u9053\u6709\u591a\u5c11\u8bb0\u5f55\u3002\u6216\u8005\u6211\u4eec\u6b63\u5728\u521b\u9020\u4e00\u6b3e\u602a\u7269\u6570\u91cf\u52a8\u6001\u53d8\u5316\u7684\u6e38\u620f(\u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\uff0c\u4e00\u4e9b\u602a\u7269\u4f1a\u6b7b\u4ea1\uff0c\u65b0\u602a\u7269\u4f1a\u51fa\u73b0)\u3002 \u5982\u679c\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u5c31\u58f0\u660e\u53d8\u91cf\u6216\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u90a3\u6211\u4eec\u80fd\u505a\u7684\u5c31\u662f\u5c1d\u8bd5\u731c\u6d4b\u9700\u8981\u7684\u53d8\u91cf\u7684\u5927\u5c0f\u7684\u6700\u5927\u503c\u5e76\u671f\u671b\u5b9e\u9645\u4f7f\u7528\u4e2d\u8be5\u5185\u5b58\u662f\u8db3\u591f\u7684\uff1a 1 2 3 4 char name [ 25 ]; // \u671f\u671b\u7528\u6237\u7684\u59d3\u540d\u4e0d\u8d85\u8fc725\u4e2a\u5b57\u7b26 Record record [ 500 ]; // \u5e0c\u671b\u6570\u636e\u7684\u8bb0\u5f55\u6570\u5c0f\u4e8e500\u6761 Monster monster [ 40 ]; // \u6700\u591a 40 \u4e2a\u602a\u7269 Polygon rendering [ 30000 ]; // 3d \u6e32\u67d3\u4e0d\u8d85\u8fc73\u4e07\u4e2a\u591a\u8fb9\u5f62 \u8fd9\u79cd\u89e3\u51b3\u65b9\u6848\u662f\u5f88\u5dee\u52b2\u7684\uff0c\u539f\u56e0\u6709\u56db\u6761\uff1a \u9996\u5148\uff0c\u5982\u679c\u5b9e\u9645\u6ca1\u6709\u4f7f\u7528\u8be5\u53d8\u91cf\uff0c\u5219\u4f1a\u5bfc\u81f4\u5185\u5b58\u6d6a\u8d39\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u4e3a\u6bcf\u4e2a\u540d\u79f0\u5206\u914d25\u4e2a\u5b57\u7b26\uff0c\u4f46\u540d\u79f0\u5e73\u5747\u53ea\u670912\u4e2a\u5b57\u7b26\u957f\uff0c\u90a3\u4e48\u7533\u8bf7\u7684\u5185\u5b58\u662f\u5b9e\u9645\u4f7f\u7528\u7684\u4e24\u500d\u591a\u3002\u6216\u8005\uff0c\u5bf9\u4e8e\u4e0a\u9762 rendering \u6570\u7ec4\u7684\u4f8b\u5b50\u6765\u8bf4\uff1a\u5982\u679c\u6e32\u67d3\u53ea\u4f7f\u752810000\u4e2a\u591a\u8fb9\u5f62\uff0c\u90a3\u4e48\u5c06\u670920000\u4e2a\u591a\u8fb9\u5f62\u7684\u5185\u5b58\u88ab\u989d\u5916\u7533\u8bf7\u5355\u6ca1\u6709\u88ab\u4f7f\u7528! \u7b2c\u4e8c\uff0c\u6211\u4eec\u5982\u4f55\u5224\u65ad\u54ea\u4e9b\u5185\u5b58\u4f4d\u5b9e\u9645\u4e0a\u88ab\u4f7f\u7528\u4e86\u5462\uff1f\u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0c\u8fd9\u5f88\u7b80\u5355\uff1a\u4ee5 \\0 \u5f00\u5934\u7684\u5b57\u7b26\u4e32\u663e\u7136\u6ca1\u6709\u88ab\u4f7f\u7528\u3002\u4f46\u662f\u5bf9\u4e8e monster[24] \u6765\u8bf4\u5462\uff1f\u5b83\u4ecd\u7136\u5728\u88ab\u4f7f\u7528\u5417\uff1f\u6211\u4eec\u9700\u8981\u67d0\u79cd\u65b9\u6cd5\u6765\u533a\u5206\u4ecd\u7136\u5728\u4f7f\u7528\u7684\u53d8\u91cf\u548c\u4e0d\u518d\u88ab\u4f7f\u7528\u7684\u53d8\u91cf\uff0c\u8fd9\u589e\u52a0\u4e86\u590d\u6742\u6027\uff0c\u5e76\u53ef\u80fd\u4f1a\u5360\u7528\u989d\u5916\u7684\u5185\u5b58\u3002 \u7b2c\u4e09\uff0c\u5927\u591a\u6570\u666e\u901a\u53d8\u91cf(\u5305\u62ec\u56fa\u5b9a\u6570\u7ec4)\u88ab\u5206\u914d\u5230\u79f0\u4e3a \u6808 \u7684\u5185\u5b58\u4e2d\u3002\u7a0b\u5e8f\u7684\u5806\u6808\u80fd\u591f\u4f7f\u7528\u7684\u5185\u5b58\u901a\u5e38\u975e\u5e38\u5c0f\u2014\u2014Visual Studio \u9ed8\u8ba4\u5806\u6808\u5927\u5c0f\u4e3a 1MB\u3002\u5982\u679c\u8d85\u8fc7\u8fd9\u4e2a\u5927\u5c0f\uff0c\u5c31\u4f1a\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5c31\u4f1a\u505c\u6b62\u7a0b\u5e8f\u3002 \u5728 Visual Studio \u4e2d\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\u65f6\u53d1\u751f\u7684\u60c5\u51b5\uff1a 1 2 3 4 int main () { int array [ 1000000 ]; // allocate 1 million integers (probably 4MB of memory) } \u5c06\u5185\u5b58\u9650\u5236\u4e3a1MB\u5bf9\u4e8e\u8bb8\u591a\u7a0b\u5e8f\u6765\u8bf4\u90fd\u662f\u6709\u95ee\u9898\u7684\uff0c\u7279\u522b\u662f\u90a3\u4e9b\u5904\u7406\u56fe\u5f62\u7684\u7a0b\u5e8f\u3002\u5728Visual Studio\u4e2d\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\u65f6\u53d1\u751f\u7684\u60c5\u51b5: \u7b2c\u56db\uff0c\u4e5f\u662f\u6700\u91cd\u8981\u7684\uff0c\u5b83\u4f1a\u5bfc\u81f4\u4eba\u4e3a\u7684\u9650\u5236\uff0c\u5e76\u53ef\u80fd\u5bfc\u81f4\u6ea2\u51fa\u3002\u5982\u679c\u7528\u6237\u8bd5\u56fe\u4ece\u78c1\u76d8\u8bfb\u5165600\u6761\u8bb0\u5f55\uff0c\u4f46\u6211\u4eec\u53ea\u5206\u914d\u4e86\u6700\u591a500\u6761\u8bb0\u5f55\u7684\u5185\u5b58\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u60c5\u51b5\uff1f\u6b64\u65f6\u8981\u4e48\u7ed9\u7528\u6237\u53d1\u9001\u4e00\u4e2a\u9519\u8bef\uff0c\u8981\u6c42\u5176\u53ea\u8bfb\u53d6500\u6761\u8bb0\u5f55\uff0c\u8981\u4e48(\u5728\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u6839\u672c\u4e0d\u5904\u7406\u8fd9\u79cd\u60c5\u51b5) record \u6570\u7ec4\u6ea2\u51fa\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u8fd9\u4e9b\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7\u52a8\u6001\u5185\u5b58\u5206\u914d\u8f7b\u677e\u89e3\u51b3\u3002 \u52a8\u6001\u5185\u5b58\u5206\u914d \u662f\u8fd0\u884c\u7a0b\u5e8f\u5728\u9700\u8981\u65f6\u5411\u64cd\u4f5c\u7cfb\u7edf\u8bf7\u6c42\u5185\u5b58\u7684\u4e00\u79cd\u65b9\u6cd5\u3002\u8fd9\u4e2a\u5185\u5b58\u4e0d\u662f\u6765\u81ea\u7a0b\u5e8f\u6709\u9650\u7684\u5806\u6808\u5185\u5b58\u2014\u2014\u76f8\u53cd\uff0c\u5b83\u662f\u4ece\u4e00\u4e2a\u66f4\u5927\u7684\u5185\u5b58\u6c60\u4e2d\u5206\u914d\u7684\uff0c\u7531\u64cd\u4f5c\u7cfb\u7edf\u7ba1\u7406\uff0c\u79f0\u4e3a \u5806 \u3002\u5728\u73b0\u4ee3\u673a\u5668\u4e0a\uff0c\u5806\u7684\u5927\u5c0f\u53ef\u4ee5\u8fbe\u5230\u5343\u5146\u5b57\u8282\u3002","title":"\u52a8\u6001\u5185\u5b58\u5206\u914d\u7684\u5fc5\u8981\u6027"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/#_2","text":"\u4e3a \u4e00\u4e2a \u53d8\u91cf\u5206\u914d\u5185\u5b58\uff0c\u6211\u4eec\u4f7f\u7528 new \u7684\u6807\u91cf\u5f62\u5f0f\uff1a 1 new int ; // dynamically allocate an integer (and discard the result) \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5411\u64cd\u4f5c\u7cfb\u7edf\u7533\u8bf7\u4e00\u4e2a\u6574\u578b\u7684\u5185\u5b58\u3002 new \u8fd0\u7b97\u7b26\u4f1a\u4f7f\u7528\u7533\u8bf7\u6765\u7684\u5185\u5b58\u521b\u5efa\u8fd9\u4e2a\u5bf9\u8c61\uff0c\u7136\u540e\u8fd4\u56de\u6307\u5411\u8be5\u5185\u5b58\u7684\u6307\u9488\u3002 \u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8fd4\u56de\u503c\u8d4b\u503c\u7ed9\u4e00\u4e2a\u6307\u9488\u53d8\u91cf\uff0c\u5e76\u901a\u8fc7\u5b83\u5728\u540e\u7eed\u7684\u7a0b\u5e8f\u4e2d\u8bbf\u95ee\u8fd9\u5757\u5185\u5b58\u3002 1 int * ptr { new int }; // dynamically allocate an integer and assign the address to ptr so we can access it later \u5728\u540e\u7eed\u7684\u7a0b\u5e8f\u4e2d\u901a\u8fc7 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u8bbf\u95ee\u8be5\u5185\u5b58\uff1a 1 * ptr = 7 ; // assign value of 7 to allocated memory \u5982\u679c\u4f60\u4e4b\u524d\u4e0d\u77e5\u9053\u6307\u9488\u6709\u4ec0\u4e48\u7528\uff0c\u90a3\u4e48\u73b0\u5728\u5e94\u8be5\u6e05\u695a\u4e86\uff0c\u81f3\u5c11\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u6ca1\u6709\u4e00\u4e2a\u6307\u9488\u6765\u4fdd\u5b58\u521a\u521a\u5206\u914d\u7684\u5185\u5b58\u5730\u5740\uff0c\u6211\u4eec\u5c31\u6ca1\u6709\u529e\u6cd5\u8bbf\u95ee\u521a\u521a\u5206\u914d\u7ed9\u6211\u4eec\u7684\u5185\u5b58!","title":"\u5355\u4e00\u53d8\u91cf\u7684\u52a8\u6001\u5185\u5b58\u5206\u914d"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/#_3","text":"\u8ba1\u7b97\u673a\u4e2d\u7684\u5185\u5b58\u53ef\u4f9b\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u3002\u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u5c06\u7a0b\u5e8f\u52a0\u8f7d\u5230\u4e00\u5757\u5185\u5b58\u4e2d\u3002\u8fd9\u5757\u5185\u5b58\u88ab\u4f60\u7684\u7a0b\u5e8f\u5206\u5272\u6210\u51e0\u4e2a\u4e0d\u540c\u7684\u90e8\u5206\uff0c\u6bcf\u4e2a\u90e8\u5206\u4e5f\u90fd\u6709\u5176\u7279\u5b9a\u7684\u4f5c\u7528\u3002\u5176\u4e2d\u4e00\u5c0f\u90e8\u5206\u5305\u542b\u4f60\u7684\u7a0b\u5e8f\u4ee3\u7801\uff0c\u53e6\u5916\u4e00\u5c0f\u90e8\u5206\u5219\u7528\u4e8e\u7a0b\u5e8f\u6b63\u5e38\u8fd0\u884c\u65f6\u4f7f\u7528\uff08\u8bb0\u5f55\u51fd\u6570\u8c03\u7528\u3001\u521b\u5efa\u548c\u9500\u6bc1\u5168\u5c40\u53d8\u91cf\u548c\u4e34\u65f6\u53d8\u91cf\uff0c\u7b49\u7b49\uff09\u3002\u6211\u4eec\u4f1a\u5728\u540e\u9762\u8fdb\u884c\u66f4\u8be6\u7ec6\u4ecb\u7ecd\u3002\u4f46\u662f\uff0c\u8fd8\u6709\u5f88\u591a\u5f88\u591a\u7684\u5185\u5b58\u5c31\u53ea\u662f\u653e\u5728\u90a3\u91cc\uff0c\u7b49\u5f85\u4f60\u7684\u7a0b\u5e8f\u8bf7\u6c42\u4f7f\u7528\u3002 \u548c\u9759\u6001\u5185\u5b58\u3001\u81ea\u52a8\u5185\u5b58\u4e0d\u540c\u7684\u662f\uff0c\u7a0b\u5e8f\u81ea\u5df1\u9700\u8981\u8d1f\u8d23\u8bf7\u6c42\u548c\u91ca\u653e\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u3002","title":"\u52a8\u6001\u5185\u5b58\u5206\u914d\u7684\u539f\u7406\u662f\u4ec0\u4e48\uff1f"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/#_4","text":"\u5f53\u4f60\u52a8\u6001\u5206\u914d\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7 \u76f4\u63a5\u521d\u59cb\u5316 \u6216 \u7edf\u4e00\u521d\u59cb\u5316 \u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\uff1a 1 2 int * ptr1 { new int ( 5 ) }; // \u76f4\u63a5\u521d\u59cb\u5316 int * ptr2 { new int { 6 } }; // \u7edf\u4e00\u521d\u59cb\u5316","title":"\u52a8\u6001\u5206\u914d\u53d8\u91cf\u7684\u521d\u59cb\u5316"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/#_5","text":"\u5728\u4f7f\u7528\u5b8c\u52a8\u6001\u5206\u914d\u7684\u53d8\u91cf\u65f6\uff0c\u6211\u4eec\u9700\u8981\u663e\u5f0f\u5730\u544a\u8bc9C++\u91ca\u653e\u5185\u5b58\u4ee5\u4fbf\u91cd\u7528\u3002\u5bf9\u4e8e\u5355\u4e2a\u53d8\u91cf\uff0c\u8fd9\u662f\u901a\u8fc7\u6807\u91cf(\u975e\u6570\u7ec4)\u5f62\u5f0f\u7684 delete \u64cd\u4f5c\u7b26\u6765\u5b8c\u6210\u7684\uff1a 1 2 3 // assume ptr has previously been allocated with operator new delete ptr ; // return the memory pointed to by ptr to the operating system ptr = nullptr ; // set ptr to be a null pointer","title":"\u5220\u9664\u5355\u4e00\u53d8\u91cf"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/#_6","text":"delete \u8fd0\u7b97\u7b26\u5e76\u6ca1\u6709 \u5b9e\u9645\u4e0a \u5220\u9664\u4efb\u4f55\u4e1c\u897f\uff0c\u5b83\u53ea\u662f\u5c06\u6307\u9488\u6307\u5411\u7684\u5185\u5b58\u8fd8\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u7f62\u4e86\u3002\u64cd\u4f5c\u7cfb\u7edf\u968f\u5373\u4fbf\u53ef\u4ee5\u5c06\u8be5\u5185\u5b58\u5206\u914d\u7ed9\u5176\u4ed6\u5e94\u7528\u4f7f\u7528\uff08\u6216\u8005\u7ed9\u5f53\u524d\u5e94\u7528\u4f7f\u7528\u4e5f\u53ef\u4ee5\uff09\u3002 \u5c3d\u7ba1\u770b\u4e0a\u53bb\u6211\u4eec\u662f\u5728 delete \u4e00\u4e2a\u53d8\u91cf\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\uff01\u8be5\u6307\u9488\u53d8\u91cf\u4ecd\u7136\u5904\u4e8e\u8be5\u4f5c\u7528\u57df\uff0c\u800c\u4e14\u4e5f\u53ef\u4ee5\u88ab\u8d4b\u4e88\u65b0\u503c\u3002 \u6ce8\u610f\uff0c\u5220\u9664\u4e0d\u6307\u5411\u52a8\u6001\u5206\u914d\u5185\u5b58\u7684\u6307\u9488\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e0d\u597d\u7684\u4e8b\u60c5\u53d1\u751f\u3002","title":"\u5220\u9664\u5185\u5b58\u662f\u4ec0\u4e48\u610f\u601d\uff1f"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/#_7","text":"C++\u6ca1\u6709\u89c4\u5b9a\u88ab\u91ca\u653e\u7684\u5185\u5bb9\u5176\u5185\u5bb9\u5e94\u8be5\u662f\u4ec0\u4e48\uff0c\u4e5f\u6ca1\u6709\u89c4\u5b9a\u88ab\u5220\u9664\u7684\u6307\u9488\u7684\u503c\u5e94\u8be5\u662f\u4ec0\u4e48\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd4\u56de\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u7684\u5185\u5b58\u5c06\u5305\u542b\u5b83\u5728\u8fd4\u56de\u4e4b\u524d\u7684\u76f8\u540c\u503c\uff0c\u5e76\u4e14\u6307\u9488\u5c06\u6307\u5411\u73b0\u5728\u91ca\u653e\u7684\u5185\u5b58\u3002 \u6267\u884c\u5df2\u7ecf\u88ab\u91ca\u653e\u7684\u5185\u5b58\u7684\u6307\u9488\uff0c\u79f0\u4e3a \u60ac\u5782 \u6307\u9488\u3002\u95f4\u63a5\u8bbf\u95ee\u6216\u5220\u9664\u60ac\u5782\u6307\u9488\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int * ptr { new int }; // \u52a8\u6001\u5206\u914d\u4e00\u4e2a\u6574\u578b * ptr = 7 ; // \u5411\u8be5\u5185\u5b58\u8d4b\u4e00\u4e2a\u503c delete ptr ; // \u5185\u5b58\u8fd8\u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff0cptr\u73b0\u5728\u662f\u60ac\u5782\u6307\u9488 std :: cout << * ptr ; // \u95f4\u63a5\u8bbf\u95ee\u60ac\u5782\u6307\u9488\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a delete ptr ; // \u518d\u6b21\u91ca\u653e\u5df2\u7ecf\u88ab\u91ca\u653e\u7684\u5185\u5b58\u4e5f\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u4e4b\u524d\u5206\u914d\u7ed9\u5df2\u5206\u914d\u5185\u5b58\u7684\u503c7\u53ef\u80fd\u4ecd\u7136\u5b58\u5728\uff0c\u4f46\u8be5\u5185\u5b58\u5730\u5740\u7684\u503c\u53ef\u80fd\u5df2\u7ecf\u53d1\u751f\u4e86\u53d8\u5316\u3002\u5185\u5b58\u4e5f\u6709\u53ef\u80fd\u88ab\u5206\u914d\u7ed9\u53e6\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f(\u6216\u64cd\u4f5c\u7cfb\u7edf\u81ea\u5df1\u4f7f\u7528)\uff0c\u8bd5\u56fe\u8bbf\u95ee\u8be5\u5185\u5b58\u5c06\u5bfc\u81f4\u64cd\u4f5c\u7cfb\u7edf\u5173\u95ed\u7a0b\u5e8f\u3002 \u91ca\u653e\u5185\u5b58\u53ef\u80fd\u4f1a\u5bfc\u81f4\u591a\u4e2a\u60ac\u5782\u6307\u9488\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int * ptr { new int {} }; // \u52a8\u6001\u5206\u914d\u4e00\u4e2a\u6574\u578b int * otherPtr { ptr }; // otherPtr \u6307\u5411\u76f8\u540c\u7684\u5185\u5b58 delete ptr ; // \u5185\u5b58\u8fd8\u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff0cptr,otherPtr \u73b0\u5728\u662f\u60ac\u5782\u6307\u9488 ptr = nullptr ; // ptr \u73b0\u5728\u662fnullptr // \u4f46\u662f, otherPtr \u4ecd\u7136\u662f\u60ac\u5782\u6307\u9488 return 0 ; } \u901a\u8fc7\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u907f\u514d\u4e0a\u8ff0\u95ee\u9898\u3002 \u9996\u5148\uff0c\u5c3d\u91cf\u907f\u514d\u6709\u591a\u4e2a\u6307\u9488\u6307\u5411\u540c\u4e00\u5757\u52a8\u6001\u5185\u5b58\u3002\u5982\u679c\u8fd9\u662f\u4e0d\u53ef\u80fd\u7684\uff0c\u8981\u6e05\u695a\u54ea\u4e9b\u6307\u9488\u201c\u62e5\u6709\u201d\u5185\u5b58(\u5e76\u8d1f\u8d23\u5220\u9664\u5b83)\uff0c\u54ea\u4e9b\u6307\u9488\u53ea\u662f\u5728\u8bbf\u95ee\u5b83\u3002 \u5176\u6b21\uff0c\u5728\u5220\u9664\u6307\u9488\u65f6\uff0c\u5982\u679c\u8be5\u6307\u9488\u4e0d\u4f1a\u7acb\u5373\u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u5219\u5c06\u8be5\u6307\u9488\u8bbe\u7f6e\u4e3anullptr\u3002\u6211\u4eec\u5c06\u66f4\u591a\u5730\u8ba8\u8bba\u7a7a\u6307\u9488\uff0c\u4ee5\u53ca\u5b83\u4eec\u4e3a\u4ec0\u4e48\u6709\u7528\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c06\u5df2\u5220\u9664\u7684\u6307\u9488\u8bbe\u7f6e\u4e3anullptr\uff0c\u9664\u975e\u5b83\u4eec\u968f\u540e\u5c06\u7acb\u5373\u8d85\u51fa\u4f5c\u7528\u57df\u3002","title":"\u60ac\u5782\u6307\u9488"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/#new","text":"\u5f53\u5411\u64cd\u4f5c\u7cfb\u7edf\u8bf7\u6c42\u5185\u5b58\u65f6\uff0c\u5728\u6781\u5c11\u6570\u60c5\u51b5\u4e0b\uff0c\u64cd\u4f5c\u7cfb\u7edf\u53ef\u80fd\u5df2\u7ecf\u6ca1\u6709\u4efb\u4f55\u53ef\u7528\u7684\u5185\u5b58\u3002 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5982\u679c new \u5931\u8d25\uff0c\u5219\u629b\u51fa bad_alloc \u5f02\u5e38\u3002\u5982\u679c\u8fd9\u4e2a\u5f02\u5e38\u6ca1\u6709\u5f97\u5230\u6b63\u786e\u5904\u7406(\u5b83\u4e0d\u4f1a\u5f97\u5230\u6b63\u786e\u5904\u7406\uff0c\u56e0\u4e3a\u6211\u4eec\u8fd8\u6ca1\u6709\u8ba8\u8bba\u5f02\u5e38\u6216\u5f02\u5e38\u5904\u7406)\uff0c\u7a0b\u5e8f\u5c06\u7ec8\u6b62(\u5d29\u6e83)\uff0c\u5e76\u51fa\u73b0\u4e00\u4e2a\u672a\u5904\u7406\u7684\u5f02\u5e38\u9519\u8bef\u3002 \u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u8ba9 new \u629b\u51fa\u5f02\u5e38(\u6216\u8ba9\u7a0b\u5e8f\u5d29\u6e83)\u662f\u4e0d\u53ef\u53d6\u7684\uff0c\u56e0\u6b64\uff0c\u5982\u679c\u65e0\u6cd5\u5206\u914d\u5185\u5b58\uff0c\u53ef\u4ee5\u4f7f\u7528\u53e6\u4e00\u79cd\u5f62\u5f0f\u7684 new \u6765\u544a\u8bc9 new \u8fd4\u56de\u7a7a\u6307\u9488\u3002\u8fd9\u662f\u901a\u8fc7\u5728 new \u5173\u952e\u5b57\u548c\u5206\u914d\u7c7b\u578b\u4e4b\u95f4\u6dfb\u52a0\u5e38\u91cf std::nothrow \u6765\u5b9e\u73b0\u7684\uff1a 1 int * value { new ( std :: nothrow ) int }; // \u5982\u679c\u6ca1\u6709\u5206\u914d\u5230\u5185\u5b58\uff0c\u5219\u6307\u9488\u88ab\u7f6e\u4e3anull \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c new \u5206\u914d\u5185\u5b58\u5931\u8d25\uff0c\u5b83\u5c06\u8fd4\u56de\u4e00\u4e2a\u7a7a\u6307\u9488\u800c\u4e0d\u662f\u5df2\u5206\u914d\u5185\u5b58\u7684\u5730\u5740\u3002 \u6ce8\u610f\uff0c\u5982\u679c\u4f60\u5c1d\u8bd5\u901a\u8fc7\u6b64\u6307\u9488\u8fdb\u884c\u95f4\u63a5\u64cd\u4f5c\uff0c\u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a (\u6700\u6709\u53ef\u80fd\u7684\u662f\u7a0b\u5e8f\u5d29\u6e83)\u3002\u56e0\u6b64\uff0c\u6700\u4f73\u5b9e\u8df5\u662f\u5728\u4f7f\u7528\u5206\u914d\u7684\u5185\u5b58\u4e4b\u524d\u68c0\u67e5\u6240\u6709\u5185\u5b58\u8bf7\u6c42\uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u5b9e\u9645\u4e0a\u6210\u529f\u4e86\u3002 1 2 3 4 5 6 int * value { new ( std :: nothrow ) int {} }; // ask for an integer's worth of memory if ( ! value ) // handle case where new returned null { // Do error handling here std :: cerr << \"Could not allocate memory \\n \" ; } \u56e0\u4e3a\u901a\u8fc7 new \u7533\u8bf7\u5185\u5b58\u5f88\u5c11\u4f1a\u5931\u8d25(\u5728\u5f00\u53d1\u73af\u5883\u4e2d\u51e0\u4e4e\u4ece\u4e0d\u5931\u8d25)\uff0c\u6240\u4ee5\u7ecf\u5e38\u4f1a\u5fd8\u8bb0\u6267\u884c\u6b64\u68c0\u67e5\uff01","title":"new \u8fd0\u7b97\u7b26\u53ef\u80fd\u6267\u884c\u5931\u8d25"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/#_8","text":"\u7a7a\u6307\u9488 (\u8bbe\u7f6e\u4e3a nullptr \u7684\u6307\u9488) \u5728\u5904\u7406\u52a8\u6001\u5185\u5b58\u5206\u914d\u65f6\u5f88\u6709\u7528\u3002\u5728\u52a8\u6001\u5185\u5b58\u5206\u914d\u7684\u4e0a\u4e0b\u6587\u4e2d\uff0c\u7a7a\u6307\u9488\u4ee3\u8868\u7740\u201d\u6ca1\u6709\u5185\u5b58\u88ab\u5206\u914d\u7ed9\u8fd9\u4e2a\u6307\u9488\u201c\u3002\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u6765\u5206\u914d\u5185\u5b58\uff1a 1 2 3 // If ptr isn't already allocated, allocate it if ( ! ptr ) ptr = new int ; \u5220\u9664\u7a7a\u6307\u9488\u6ca1\u6548\u679c\u3002\u56e0\u6b64\u6ca1\u5fc5\u8981\u8fd9\u4e48\u505a\uff1a 1 2 if ( ptr ) delete ptr ; \u76f4\u63a5\u8fd9\u4e48\u505a\u5c31\u53ef\u4ee5\uff1a 1 delete ptr ; \u5982\u679c ptr \u662f\u975e\u7a7a\u7684\uff0c\u90a3\u4e48\u5b83\u6307\u5411\u7684\u5185\u5b58\u5c06\u88ab\u91ca\u653e\uff0c\u5982\u679c ptr \u662f\u7a7a\uff0c\u5219\u4ec0\u4e48\u90fd\u4e0d\u4f1a\u53d1\u751f\u3002","title":"\u7a7a\u6307\u9488\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/#_9","text":"\u9664\u975e\u663e\u5f0f\u5730\u91ca\u653e\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u6216\u7a0b\u5e8f\u7ed3\u675f(\u5e76\u4e14\u64cd\u4f5c\u7cfb\u7edf\u6e05\u7406\u5b83\uff0c\u5047\u8bbe\u60a8\u7684\u64cd\u4f5c\u7cfb\u7edf\u8fd9\u4e48\u505a)\uff0c\u5426\u5219\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u4e00\u76f4\u5904\u4e8e\u88ab\u5206\u914d\u72b6\u6001\u3002\u4f46\u662f\uff0c\u7528\u4e8e\u4fdd\u5b58\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u5730\u5740\u7684\u6307\u9488\u9075\u5faa\u672c\u5730\u53d8\u91cf\u7684\u6b63\u5e38\u4f5c\u7528\u57df\u89c4\u5219\u3002\u8fd9\u79cd\u4e0d\u5339\u914d\u4f1a\u4ea7\u751f\u6709\u8da3\u7684\u95ee\u9898\u3002 \u8003\u8651\u4e0b\u9762\u7684\u51fd\u6570\uff1a 1 2 3 4 void doSomething () { int * ptr { new int {} }; } \u8fd9\u4e2a\u51fd\u6570\u52a8\u6001\u5206\u914d\u4e86\u4e00\u4e2a\u6574\u6570\uff0c\u4f46\u5e76\u6ca1\u6709\u7528 delete \u91ca\u653e\u5b83\u3002\u56e0\u4e3a\u6307\u9488\u53d8\u91cf\u53ea\u662f\u666e\u901a\u53d8\u91cf\uff0c\u5f53\u51fd\u6570\u7ed3\u675f\u65f6\uff0c ptr \u79bb\u5f00\u4f5c\u7528\u57df \u3002\u7531\u4e8e ptr \u662f\u4fdd\u5b58\u52a8\u6001\u5206\u914d\u7684\u6574\u6570\u5730\u5740\u7684\u552f\u4e00\u53d8\u91cf\uff0c\u5f53 ptr \u88ab\u9500\u6bc1\u65f6\uff0c\u5bf9\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u5c31\u6ca1\u6709\u66f4\u591a\u7684\u5f15\u7528\u4e86\u3002\u8fd9\u610f\u5473\u7740\u7a0b\u5e8f\u73b0\u5728\u201c\u4e22\u5931\u201d\u4e86\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u7684\u5730\u5740\u3002\u56e0\u6b64\uff0c\u65e0\u6cd5\u518d\u5220\u9664\u8fd9\u4e2a\u52a8\u6001\u5206\u914d\u7684\u6574\u6570\u4e86\u3002 \u8fd9\u4e2a\u73b0\u8c61\u79f0\u4e3a \u5185\u5b58\u6cc4\u6f0f \u3002\u5f53\u7a0b\u5e8f\u5728\u5c06\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u8fd4\u56de\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u4e4b\u524d\u4e22\u5931\u4e86\u5b83\u7684\u5730\u5740\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u5185\u5b58\u6cc4\u6f0f\u3002\u5f53\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\u65f6\uff0c\u7a0b\u5e8f\u5c06\u65e0\u6cd5\u5220\u9664\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\uff0c\u56e0\u4e3a\u5b83\u4e0d\u518d\u77e5\u9053\u5185\u5b58\u5730\u5740\u4e86\u3002\u64cd\u4f5c\u7cfb\u7edf\u4e5f\u4e0d\u80fd\u4f7f\u7528\u8be5\u5185\u5b58\uff0c\u56e0\u4e3a\u8be5\u5185\u5b58\u88ab\u8ba4\u4e3a\u662f\u7a0b\u5e8f\u4ecd\u5728\u4f7f\u7528\u4e2d\u3002 \u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u5185\u5b58\u6cc4\u6f0f\u4f1a\u6d88\u8017\u6389\u53ef\u7528\u7684\u5185\u5b58\uff0c\u4e0d\u4ec5\u4f7f\u8fd9\u4e2a\u7a0b\u5e8f\u53ef\u7528\u7684\u5185\u5b58\u51cf\u5c11\uff0c\u800c\u4e14\u4f7f\u5176\u4ed6\u7a0b\u5e8f\u53ef\u7528\u7684\u5185\u5b58\u51cf\u5c11\u3002\u6709\u4e25\u91cd\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\u7684\u7a0b\u5e8f\u4f1a\u8017\u5c3d\u6240\u6709\u53ef\u7528\u5185\u5b58\uff0c\u5bfc\u81f4\u6574\u4e2a\u673a\u5668\u8fd0\u884c\u7f13\u6162\u751a\u81f3\u5d29\u6e83\u3002\u53ea\u6709\u5728\u7a0b\u5e8f\u7ec8\u6b62\u540e\uff0c\u64cd\u4f5c\u7cfb\u7edf\u624d\u80fd\u6e05\u7406\u548c\u201c\u56de\u6536\u201d\u6240\u6709\u6cc4\u6f0f\u7684\u5185\u5b58\u3002 \u5c3d\u7ba1\u6307\u9488\u8d85\u51fa\u4f5c\u7528\u57df\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\uff0c\u4f46\u8fd8\u6709\u5176\u4ed6\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u7684\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6301\u6709\u52a8\u6001\u5206\u914d\u5185\u5b58\u5730\u5740\u7684\u6307\u9488\u88ab\u5206\u914d\u4e86\u53e6\u4e00\u4e2a\u503c\uff0c\u4e5f\u4f1a\u53d1\u751f\u5185\u5b58\u6cc4\u6f0f\uff1a 1 2 3 int value = 5 ; int * ptr { new int {} }; // allocate memory ptr = & value ; // old address lost, memory leak results \u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728\u91cd\u65b0\u8d4b\u503c\u4e4b\u524d\u5220\u9664\u6307\u9488\u6765\u89e3\u51b3\uff1a 1 2 3 4 int value { 5 }; int * ptr { new int {} }; // allocate memory delete ptr ; // return memory back to operating system ptr = & value ; // reassign pointer to address of value \u7c7b\u4f3c\u5730\uff0c\u91cd\u590d\u5206\u914d\u5185\u5b58\u4e5f\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\uff1a 1 2 int * ptr { new int {} }; ptr = new int {}; // old address lost, memory leak results \u4ece\u7b2c\u4e8c\u6b21\u5206\u914d\u5185\u5b58\u65f6\u8fd4\u56de\u7684\u5730\u5740\u5c06\u8986\u76d6\u7b2c\u4e00\u4e2a\u5206\u914d\u7684\u5730\u5740\u3002\u56e0\u6b64\uff0c\u7b2c\u4e00\u5757\u5185\u5b58\u6cc4\u6f0f\u4e86\uff01 \u7c7b\u4f3c\u5730\uff0c\u53ef\u4ee5\u901a\u8fc7\u786e\u4fdd\u5728\u91cd\u65b0\u8d4b\u503c\u4e4b\u524d\u5220\u9664\u6307\u9488\u6765\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002","title":"\u5185\u5b58\u6cc4\u6f0f"},{"location":"11-11-Dynamic-memory-allocation-with-new-and-delete/#_10","text":"\u64cd\u4f5c\u7b26 new \u548c delete \u5141\u8bb8\u6211\u4eec\u4e3a\u7a0b\u5e8f\u52a8\u6001\u5206\u914d\u5355\u4e2a\u53d8\u91cf\u3002 \u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u5177\u6709 \u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u5e76\u4e14\u5728\u91ca\u653e\u5b83\u6216\u7a0b\u5e8f\u7ec8\u6b62\u4e4b\u524d\u5c06\u4fdd\u6301\u5206\u914d\u72b6\u6001\u3002 \u6ce8\u610f\u4e0d\u8981\u901a\u8fc7\u60ac\u5782\u6216\u7a7a\u6307\u9488\u6267\u884c\u95f4\u63a5\u8bbf\u95ee\u64cd\u4f5c\u3002 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u770b\u770b\u5982\u4f55\u4f7f\u7528 new \u548c delete \u6765\u5206\u914d\u548c\u5220\u9664\u6570\u7ec4\u3002","title":"\u7ed3\u8bba"},{"location":"11-12-Dynamically-allocating-arrays/","tags":["arrays"],"text":"Key Takeaway \u4f7f\u7528 new[] \u521b\u5efa\u52a8\u6001\u6570\u7ec4\uff0c\u5176\u957f\u5ea6\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6307\u5b9a\uff0c\u4e0d\u9700\u8981\u662f\u5e38\u6570\uff0c\u4f46\u5176\u7c7b\u578b\u5fc5\u987b\u662f\u80fd\u591f\u88ab\u8f6c\u6362\u4e3a std::size_t \u7684\u7c7b\u578b \u4f7f\u7528 new int[legth]{} \u5206\u914d\u5e76\u521d\u59cb\u5316\u6570\u7ec4\u4e3a\u51680\u3002\u4eceC++11\u5f00\u59cb\uff0c \u521d\u59cb\u5316\u503c\u5217\u8868 \u53ef\u4ee5\u7528\u4e8e\u52a8\u6001\u6570\u7ec4\uff1a new int[5]{ 9, 7, 5, 3, 1 } \u5220\u9664\u52a8\u6001\u6570\u7ec4\u7684\u5185\u5b58\u9700\u8981\u4f7f\u7528 delete [] \uff0c\u65e0\u9700\u6307\u5b9a\u5927\u5c0f \u9664\u4e86\u4e3a\u5355\u4e2a\u53d8\u91cf\u52a8\u6001\u5206\u914d\u5185\u5b58\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4e3a\u6570\u7ec4\u52a8\u6001\u5206\u914d\u5185\u5b58\u3002\u56fa\u5b9a\u6570\u7ec4\u7684\u5927\u5c0f\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u5df2\u77e5\uff0c\u800c\u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4\u5219\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u786e\u5b9a\u957f\u5ea6\u3002 \u4e3a\u4e86\u5206\u914d\u52a8\u6001\u6570\u7ec4\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528 new \u548c delete \u7684\u6570\u7ec4\u5f62\u5f0f( new[] \u548c delete[] )\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int main () { std :: cout << \"Enter a positive integer: \" ; int length {}; std :: cin >> length ; int * array { new int [ length ]{} }; // \u4f7f\u7528\u6570\u7ec4 new\uff0c\u6ce8\u610f\u8fd9\u91cc\u7684length\u4e0d\u9700\u8981\u662f\u5e38\u6570\uff01 std :: cout << \"I just allocated an array of integers of length \" << length << '\\n' ; array [ 0 ] = 5 ; // \u5143\u7d20 0 \u8d4b\u503c\u4e3a 5 delete [] array ; // \u4f7f\u7528\u6570\u7ec4 delete \u91ca\u653e\u5185\u5b58 // \u6b64\u5904\u4e0d\u9700\u8981\u5c06 array \u8bbe\u7f6e\u4e3a nullptr/0 \u56e0\u4e3a\u5b83\u9a6c\u4e0a\u5c31\u4f1a\u79bb\u5f00\u4f5c\u7528\u57df\u88ab\u9500\u6bc1 return 0 ; } \u56e0\u4e3a\u6211\u4eec\u5206\u914d\u7684\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u6240\u4ee5C++\u6307\u5b9a\u5e94\u8be5\u4f7f\u7528\u6570\u7ec4\u7248\u672c\u7684 new \u800c\u4e0d\u662f\u6807\u91cf\u7248\u672c\u7684 new \u3002\u5b9e\u9645\u4e0a\uff0c\u5373\u4f7f [] \u6ca1\u6709\u653e\u5728 new \u540e\u9762\uff0c\u8c03\u7528\u7684\u4e5f\u662f new [] \u3002 \u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4\u957f\u5ea6\uff0c\u5176\u7c7b\u578b\u5fc5\u987b\u662f\u80fd\u591f\u88ab\u8f6c\u6362\u4e3a std::size_t \u7684\u7c7b\u578b \u3002\u5728\u5b9e\u8df5\u4e2d\uff0c\u4f7f\u7528 int \u4f5c\u4e3a\u957f\u5ea6\u662f\u53ef\u4ee5\u7684\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a std::size_t \u3002 \u4f5c\u8005\u6ce8 \u6709\u4eba\u4f1a\u8bf4\uff0c\u56e0\u4e3a\u6570\u7ec4\u7248\u672c\u7684 new \u9700\u8981 size_t \u7c7b\u578b\u7684\u957f\u5ea6\u503c\uff0c\u6240\u4ee5\u6211\u4eec\u5e94\u8be5\u4f7f\u7528 size_t \u6216\u8005\u80fd\u591f\u88ab \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u6210 size_t \u7684\u53d8\u91cf\u7c7b\u578b\u3002 \u4f46\u662f\uff0c\u6211\u8ba4\u4e3a\u8fd9\u4e2a\u8bba\u70b9\u6ca1\u6709\u8bf4\u670d\u529b\uff0c\u539f\u56e0\u6709\u5f88\u591a\u3002\u9996\u5148\uff0c\u5b83\u8fdd\u80cc\u4e86\u4f7f\u7528\u6709\u7b26\u53f7\u6574\u6570\u800c\u4e0d\u662f\u65e0\u7b26\u53f7\u6574\u6570\u7684\u6700\u4f73\u5b9e\u8df5\u3002\u5176\u6b21\uff0c\u5f53\u4f7f\u7528\u6574\u6570\u957f\u5ea6\u521b\u5efa\u52a8\u6001\u6570\u7ec4\u65f6\uff0c\u6309\u7167\u60ef\u4f8b\u5e94\u8be5\u8fd9\u6837\u505a: 1 double * ptr { new double [ 5 ] }; 5 \u662f\u4e00\u4e2a\u6574\u578b \u5b57\u9762\u91cf \uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u9690\u5f0f\u8f6c\u6362\u4e3a size_t \u3002\u5728C++23\u4e4b\u524d\uff0c\u6211\u4eec\u6ca1\u6709\u529e\u6cd5\u5728\u4e0d\u4f7f\u7528 static_cast \u7684\u60c5\u51b5\u4e0b\u521b\u5efa\u4e00\u4e2a size_t \u5b57\u9762\u91cf\u3002\u5982\u679cC++\u7684\u8bbe\u8ba1\u8005\u8981\u6c42\u6211\u4eec\u5728\u8fd9\u91cc\u662f size_t \u7684\u8bdd\uff0c\u4ed6\u80af\u5b9a\u4f1a\u63d0\u4f9b\u4e00\u4e2a\u521b\u5efa size_t \u5b57\u9762\u7684\u65b9\u6cd5\u3002 \u6700\u5e38\u89c1\u7684\u53cd\u5bf9\u610f\u89c1\u662f\uff0c\u4e00\u4e9b\u8fc2\u8150\u7684\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5c06\u6b64\u6807\u8bb0\u4e3a\u6709\u7b26\u53f7/\u65e0\u7b26\u53f7\u8f6c\u6362\u9519\u8bef(\u56e0\u4e3a\u6211\u4eec\u603b\u662f\u5c06\u8b66\u544a\u89c6\u4e3a\u9519\u8bef)\u3002\u7136\u800c\uff0c\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5373\u4f7f\u542f\u7528\u4e86\u6b64\u7c7b\u8b66\u544a(' -Wconversion ')\uff0c GCC\u4e5f\u4e0d\u4f1a\u5c06\u5176\u6807\u8bb0\u4e3a\u6709\u7b26\u53f7/\u65e0\u7b26\u53f7\u8f6c\u6362\u9519\u8bef\u3002 \u867d\u7136\u4f7f\u7528 size_t \u4f5c\u4e3a\u52a8\u6001\u5206\u914d\u6570\u7ec4\u7684\u957f\u5ea6\u6ca1\u6709\u4ec0\u4e48\u9519\uff0c\u4f46\u5728\u672c\u7cfb\u5217\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u4e0d\u4f1a\u62d8\u4e8e\u8981\u6c42\u4f7f\u7528\u5b83\u3002 \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u6b64\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u662f\u4ece\u4e0d\u540c\u7684\u5730\u65b9\u5206\u914d\u7684\uff0c\u800c\u4e0d\u662f\u7528\u4e8e\u56fa\u5b9a\u6570\u7ec4\u7684\u5185\u5b58\uff0c\u56e0\u6b64\u6570\u7ec4\u7684\u5927\u5c0f\u53ef\u4ee5\u5f88\u5927\u3002\u4f60\u53ef\u4ee5\u8fd0\u884c\u4e0a\u9762\u7684\u7a0b\u5e8f\u5e76\u5206\u914d\u957f\u5ea6\u4e3a1,000,000(\u751a\u81f3\u53ef\u80fd\u662f100,000,000)\u7684\u6570\u7ec4\u3002\u8bd5\u4e00\u8bd5\uff01\u6b63\u56e0\u4e3a\u5982\u6b64\uff0c\u5728C++\u4e2d\u9700\u8981\u5206\u914d\u5927\u91cf\u5185\u5b58\u7684\u7a0b\u5e8f\u901a\u5e38\u9700\u8981\u5206\u914d\u52a8\u6001\u5185\u5b58\u3002 \u52a8\u6001\u5730\u5220\u9664\u6570\u7ec4 \u5220\u9664\u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4\u9700\u8981\u6570\u7ec4\u7248\u672c\u7684 delete \uff0c\u5373 delete[] \u3002 \u8fd9\u544a\u8bc9CPU\u5b83\u9700\u8981\u6e05\u7406\u591a\u4e2a\u53d8\u91cf\u800c\u4e0d\u662f\u5355\u4e2a\u53d8\u91cf\u3002\u65b0\u7a0b\u5e8f\u5458\u5728\u5904\u7406\u52a8\u6001\u5185\u5b58\u5206\u914d\u65f6\u6700\u5e38\u72af\u7684\u9519\u8bef\u4e4b\u4e00\u662f\u5728\u5220\u9664\u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4\u65f6\u4f7f\u7528delete\u800c\u4e0d\u662f delete[] \u3002\u5728\u6570\u7ec4\u4e0a\u4f7f\u7528\u6807\u91cf\u7248\u672c\u7684 delete \u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \uff0c\u5982\u6570\u636e\u635f\u574f\u3001\u5185\u5b58\u6cc4\u6f0f\u3001\u7a0b\u5e8f\u5d29\u6e83\u6216\u5176\u4ed6\u95ee\u9898\u3002 \u5173\u4e8e delete[] \u7684\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u662f\uff0c delete \u5982\u4f55\u77e5\u9053\u5e94\u8be5\u91ca\u653e\u591a\u5c11\u5185\u5b58\u5462\uff1f\u5b9e\u9645\u4e0a\uff0c new[] \u4f1a\u8d1f\u8d23\u8ffd\u8e2a\u4e3a\u53d8\u91cf\u5206\u914d\u7684\u5185\u5b58\u7684\u5927\u5c0f\uff0c\u6240\u4ee5 delete[] \u80fd\u591f\u5220\u9664\u8be5\u53d8\u91cf\u5b9e\u9645\u5206\u914d\u7684\u5185\u5b58\u3002\u4f46\u662f\uff0c\u8fd9\u6bb5\u5185\u5b58\u7684\u957f\u5ea6\u503c\u5e76\u4e0d\u80fd\u591f\u88ab\u5f00\u53d1\u8005\u83b7\u53d6\u3002 \u52a8\u6001\u6570\u7ec4\u548c\u56fa\u5b9a\u6570\u7ec4\u51e0\u4e4e\u662f\u4e00\u6837\u7684 \u5728 11.8 - \u6307\u9488\u548c\u6570\u7ec4 \u4e2d\uff0c\u6211\u4eec\u77e5\u9053\u56fa\u5b9a\u6570\u7ec4\u53d8\u91cf\u4f1a\u6301\u6709\u6307\u5411\u6570\u7ec4\u9996\u4e2a\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u3002\u540c\u65f6\u6211\u4eec\u8fd8\u77e5\u9053\uff0c\u56fa\u5b9a\u6570\u7ec4\u5728\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\uff0c\u4f1a\u9000\u5316\u4e3a\u6307\u9488\uff0c\u6b64\u65f6\u6570\u7ec4\u7684\u957f\u5ea6\u5c31\u65e0\u6cd5\u5728\u51fd\u6570\u5185\u88ab\u83b7\u53d6\u4e86\u3002(\u6b64\u65f6 sizeof() \u4e5f\u4e0d\u53ef\u7528\u4e86)\u3002 \u5bf9\u4e8e\u52a8\u6001\u6570\u7ec4\u6765\u8bf4\uff0c\u5b83\u751f\u6765\u5c31\u662f\u4e00\u4e2a\u6307\u5411\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u6307\u9488\u3002\u56e0\u6b64\uff0c\u5b83\u4e5f\u4e0d\u77e5\u9053\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u4f7f\u7528\u52a8\u6001\u6570\u7ec4\u548c\u56fa\u5b9a\u6570\u7ec4\u4e0a\u662f\u4e00\u81f4\u7684\uff0c\u4f46\u662f\u7a0b\u5e8f\u5458\u9700\u8981\u8d1f\u8d23\u91ca\u653e\u52a8\u6001\u6570\u7ec4\u7684\u5185\u5b58\u3002 \u521d\u59cb\u5316\u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4 \u5982\u679c\u4f60\u60f3\u8981\u5c06\u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4\u521d\u59cb\u5316\u4e3a0\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 int * array { new int [ length ]{} }; \u5728C++11\u4e4b\u524d\uff0c\u5e76\u6ca1\u6709\u5c06\u52a8\u6001\u6570\u7ec4\u521d\u59cb\u5316\u4e3a\u975e0\u7684\u7b80\u4fbf\u65b9\u6cd5\uff08 \u521d\u59cb\u5316\u503c\u5217\u8868 \u53ea\u5bf9\u56fa\u5b9a\u6570\u7ec4\u6709\u6548\uff09\u3002\u8fd9\u5c31\u610f\u5473\u7740\u4f60\u5fc5\u987b\u4f7f\u7528\u5faa\u73af\u4f9d\u6b21\u4e3a\u6bcf\u4e2a\u5143\u7d20\u8d4b\u503c\u624d\u884c\u3002 1 2 3 4 5 6 int * array = new int [ 5 ]; array [ 0 ] = 9 ; array [ 1 ] = 7 ; array [ 2 ] = 5 ; array [ 3 ] = 3 ; array [ 4 ] = 1 ; \u592a\u9ebb\u70e6\u4e86\uff01 \u4f46\u662f\uff0c\u4eceC++11\u5f00\u59cb\uff0c \u521d\u59cb\u5316\u503c\u5217\u8868 \u4e5f\u53ef\u4ee5\u7528\u4e8e\u521d\u59cb\u5316\u52a8\u6001\u6570\u7ec4\u4e86\uff01 1 2 3 4 int fixedArray [ 5 ] = { 9 , 7 , 5 , 3 , 1 }; // initialize a fixed array before C++11 int * array { new int [ 5 ]{ 9 , 7 , 5 , 3 , 1 } }; // initialize a dynamic array since C++11 // To prevent writing the type twice, we can use auto. This is often done for types with long names. auto * array { new int [ 5 ]{ 9 , 7 , 5 , 3 , 1 } }; \u6ce8\u610f\uff0c\u5728\u6570\u7ec4\u957f\u5ea6\u548c\u521d\u59cb\u5316\u503c\u5217\u8868\u4e4b\u95f4\u6ca1\u6709\u7b49\u53f7\u3002 \u4e3a\u4e86\u4fdd\u6301\u4e00\u81f4\uff0c\u56fa\u5b9a\u6570\u7ec4\u4e5f\u53ef\u4ee5\u4f7f\u7528 \u7edf\u4e00\u521d\u59cb\u5316 \u7684\u65b9\u5f0f\u8fdb\u884c\u521d\u59cb\u5316\u3002 1 2 int fixedArray []{ 9 , 7 , 5 , 3 , 1 }; // initialize a fixed array in C++11 char fixedArray []{ \"Hello, world!\" }; // initialize a fixed array in C++11 \u663e\u5f0f\u58f0\u660e\u6570\u7ec4\u7684\u5927\u5c0f\u662f\u53ef\u9009\u7684\u3002 \u8c03\u6574\u6570\u7ec4\u5927\u5c0f \u52a8\u6001\u5206\u914d\u6570\u7ec4\u5141\u8bb8\u4f60\u5728\u5206\u914d\u65f6\u8bbe\u7f6e\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u4f46\u662f\uff0cC++\u4e0d\u63d0\u4f9b\u8c03\u6574\u5df2\u7ecf\u5206\u914d\u7684\u6570\u7ec4\u5927\u5c0f\u7684\u5185\u7f6e\u65b9\u6cd5\u3002\u53ef\u4ee5\u901a\u8fc7\u52a8\u6001\u5206\u914d\u65b0\u6570\u7ec4\u3001\u590d\u5236\u5143\u7d20\u5e76\u5220\u9664\u65e7\u6570\u7ec4\u6765\u89e3\u51b3\u8fd9\u4e2a\u9650\u5236\u3002\u7136\u800c\uff0c\u8fd9\u5f88\u5bb9\u6613\u51fa\u9519\uff0c\u7279\u522b\u662f\u5f53\u5143\u7d20\u7c7b\u578b\u662f\u7c7b(\u7c7b\u6709\u7279\u6b8a\u7684\u89c4\u5219\u63a7\u5236\u5b83\u4eec\u7684\u521b\u5efa\u65b9\u5f0f)\u65f6\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u5e94\u8be5\u907f\u514d\u81ea\u5df1\u624b\u52a8\u8c03\u6574\u6570\u7ec4\u7684\u5927\u5c0f\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u5982\u679c\u4f60\u9700\u8981\u8be5\u529f\u80fd\uff0cC++\u63d0\u4f9b\u4e86\u652f\u6301\u8c03\u6574\u5927\u5c0f\u7684\u5bb9\u5668 std::vector \u3002\u53c2\u89c1\uff1a 11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb \u3002","title":"11.12 - \u52a8\u6001\u5206\u914d\u6570\u7ec4"},{"location":"11-12-Dynamically-allocating-arrays/#_1","text":"\u5220\u9664\u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4\u9700\u8981\u6570\u7ec4\u7248\u672c\u7684 delete \uff0c\u5373 delete[] \u3002 \u8fd9\u544a\u8bc9CPU\u5b83\u9700\u8981\u6e05\u7406\u591a\u4e2a\u53d8\u91cf\u800c\u4e0d\u662f\u5355\u4e2a\u53d8\u91cf\u3002\u65b0\u7a0b\u5e8f\u5458\u5728\u5904\u7406\u52a8\u6001\u5185\u5b58\u5206\u914d\u65f6\u6700\u5e38\u72af\u7684\u9519\u8bef\u4e4b\u4e00\u662f\u5728\u5220\u9664\u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4\u65f6\u4f7f\u7528delete\u800c\u4e0d\u662f delete[] \u3002\u5728\u6570\u7ec4\u4e0a\u4f7f\u7528\u6807\u91cf\u7248\u672c\u7684 delete \u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \uff0c\u5982\u6570\u636e\u635f\u574f\u3001\u5185\u5b58\u6cc4\u6f0f\u3001\u7a0b\u5e8f\u5d29\u6e83\u6216\u5176\u4ed6\u95ee\u9898\u3002 \u5173\u4e8e delete[] \u7684\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u662f\uff0c delete \u5982\u4f55\u77e5\u9053\u5e94\u8be5\u91ca\u653e\u591a\u5c11\u5185\u5b58\u5462\uff1f\u5b9e\u9645\u4e0a\uff0c new[] \u4f1a\u8d1f\u8d23\u8ffd\u8e2a\u4e3a\u53d8\u91cf\u5206\u914d\u7684\u5185\u5b58\u7684\u5927\u5c0f\uff0c\u6240\u4ee5 delete[] \u80fd\u591f\u5220\u9664\u8be5\u53d8\u91cf\u5b9e\u9645\u5206\u914d\u7684\u5185\u5b58\u3002\u4f46\u662f\uff0c\u8fd9\u6bb5\u5185\u5b58\u7684\u957f\u5ea6\u503c\u5e76\u4e0d\u80fd\u591f\u88ab\u5f00\u53d1\u8005\u83b7\u53d6\u3002","title":"\u52a8\u6001\u5730\u5220\u9664\u6570\u7ec4"},{"location":"11-12-Dynamically-allocating-arrays/#_2","text":"\u5728 11.8 - \u6307\u9488\u548c\u6570\u7ec4 \u4e2d\uff0c\u6211\u4eec\u77e5\u9053\u56fa\u5b9a\u6570\u7ec4\u53d8\u91cf\u4f1a\u6301\u6709\u6307\u5411\u6570\u7ec4\u9996\u4e2a\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u3002\u540c\u65f6\u6211\u4eec\u8fd8\u77e5\u9053\uff0c\u56fa\u5b9a\u6570\u7ec4\u5728\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\uff0c\u4f1a\u9000\u5316\u4e3a\u6307\u9488\uff0c\u6b64\u65f6\u6570\u7ec4\u7684\u957f\u5ea6\u5c31\u65e0\u6cd5\u5728\u51fd\u6570\u5185\u88ab\u83b7\u53d6\u4e86\u3002(\u6b64\u65f6 sizeof() \u4e5f\u4e0d\u53ef\u7528\u4e86)\u3002 \u5bf9\u4e8e\u52a8\u6001\u6570\u7ec4\u6765\u8bf4\uff0c\u5b83\u751f\u6765\u5c31\u662f\u4e00\u4e2a\u6307\u5411\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u6307\u9488\u3002\u56e0\u6b64\uff0c\u5b83\u4e5f\u4e0d\u77e5\u9053\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u4f7f\u7528\u52a8\u6001\u6570\u7ec4\u548c\u56fa\u5b9a\u6570\u7ec4\u4e0a\u662f\u4e00\u81f4\u7684\uff0c\u4f46\u662f\u7a0b\u5e8f\u5458\u9700\u8981\u8d1f\u8d23\u91ca\u653e\u52a8\u6001\u6570\u7ec4\u7684\u5185\u5b58\u3002","title":"\u52a8\u6001\u6570\u7ec4\u548c\u56fa\u5b9a\u6570\u7ec4\u51e0\u4e4e\u662f\u4e00\u6837\u7684"},{"location":"11-12-Dynamically-allocating-arrays/#_3","text":"\u5982\u679c\u4f60\u60f3\u8981\u5c06\u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4\u521d\u59cb\u5316\u4e3a0\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 int * array { new int [ length ]{} }; \u5728C++11\u4e4b\u524d\uff0c\u5e76\u6ca1\u6709\u5c06\u52a8\u6001\u6570\u7ec4\u521d\u59cb\u5316\u4e3a\u975e0\u7684\u7b80\u4fbf\u65b9\u6cd5\uff08 \u521d\u59cb\u5316\u503c\u5217\u8868 \u53ea\u5bf9\u56fa\u5b9a\u6570\u7ec4\u6709\u6548\uff09\u3002\u8fd9\u5c31\u610f\u5473\u7740\u4f60\u5fc5\u987b\u4f7f\u7528\u5faa\u73af\u4f9d\u6b21\u4e3a\u6bcf\u4e2a\u5143\u7d20\u8d4b\u503c\u624d\u884c\u3002 1 2 3 4 5 6 int * array = new int [ 5 ]; array [ 0 ] = 9 ; array [ 1 ] = 7 ; array [ 2 ] = 5 ; array [ 3 ] = 3 ; array [ 4 ] = 1 ; \u592a\u9ebb\u70e6\u4e86\uff01 \u4f46\u662f\uff0c\u4eceC++11\u5f00\u59cb\uff0c \u521d\u59cb\u5316\u503c\u5217\u8868 \u4e5f\u53ef\u4ee5\u7528\u4e8e\u521d\u59cb\u5316\u52a8\u6001\u6570\u7ec4\u4e86\uff01 1 2 3 4 int fixedArray [ 5 ] = { 9 , 7 , 5 , 3 , 1 }; // initialize a fixed array before C++11 int * array { new int [ 5 ]{ 9 , 7 , 5 , 3 , 1 } }; // initialize a dynamic array since C++11 // To prevent writing the type twice, we can use auto. This is often done for types with long names. auto * array { new int [ 5 ]{ 9 , 7 , 5 , 3 , 1 } }; \u6ce8\u610f\uff0c\u5728\u6570\u7ec4\u957f\u5ea6\u548c\u521d\u59cb\u5316\u503c\u5217\u8868\u4e4b\u95f4\u6ca1\u6709\u7b49\u53f7\u3002 \u4e3a\u4e86\u4fdd\u6301\u4e00\u81f4\uff0c\u56fa\u5b9a\u6570\u7ec4\u4e5f\u53ef\u4ee5\u4f7f\u7528 \u7edf\u4e00\u521d\u59cb\u5316 \u7684\u65b9\u5f0f\u8fdb\u884c\u521d\u59cb\u5316\u3002 1 2 int fixedArray []{ 9 , 7 , 5 , 3 , 1 }; // initialize a fixed array in C++11 char fixedArray []{ \"Hello, world!\" }; // initialize a fixed array in C++11 \u663e\u5f0f\u58f0\u660e\u6570\u7ec4\u7684\u5927\u5c0f\u662f\u53ef\u9009\u7684\u3002","title":"\u521d\u59cb\u5316\u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4"},{"location":"11-12-Dynamically-allocating-arrays/#_4","text":"\u52a8\u6001\u5206\u914d\u6570\u7ec4\u5141\u8bb8\u4f60\u5728\u5206\u914d\u65f6\u8bbe\u7f6e\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u4f46\u662f\uff0cC++\u4e0d\u63d0\u4f9b\u8c03\u6574\u5df2\u7ecf\u5206\u914d\u7684\u6570\u7ec4\u5927\u5c0f\u7684\u5185\u7f6e\u65b9\u6cd5\u3002\u53ef\u4ee5\u901a\u8fc7\u52a8\u6001\u5206\u914d\u65b0\u6570\u7ec4\u3001\u590d\u5236\u5143\u7d20\u5e76\u5220\u9664\u65e7\u6570\u7ec4\u6765\u89e3\u51b3\u8fd9\u4e2a\u9650\u5236\u3002\u7136\u800c\uff0c\u8fd9\u5f88\u5bb9\u6613\u51fa\u9519\uff0c\u7279\u522b\u662f\u5f53\u5143\u7d20\u7c7b\u578b\u662f\u7c7b(\u7c7b\u6709\u7279\u6b8a\u7684\u89c4\u5219\u63a7\u5236\u5b83\u4eec\u7684\u521b\u5efa\u65b9\u5f0f)\u65f6\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u5e94\u8be5\u907f\u514d\u81ea\u5df1\u624b\u52a8\u8c03\u6574\u6570\u7ec4\u7684\u5927\u5c0f\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u5982\u679c\u4f60\u9700\u8981\u8be5\u529f\u80fd\uff0cC++\u63d0\u4f9b\u4e86\u652f\u6301\u8c03\u6574\u5927\u5c0f\u7684\u5bb9\u5668 std::vector \u3002\u53c2\u89c1\uff1a 11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb \u3002","title":"\u8c03\u6574\u6570\u7ec4\u5927\u5c0f"},{"location":"11-13-For-each-loops/","tags":["for-each","C++11","C++20"],"text":"Key Takeaway \u5728 for-each \u5faa\u73af\u4e2d\u58f0\u660e\u5143\u7d20\u65f6\uff0c\u5982\u679c\u5143\u7d20\u662f\u975e\u57fa\u672c\u7c7b\u578b\uff0c\u5219\u5c06\u5176\u58f0\u660e\u4e3a\u5f15\u7528\u6216\u5e38\u6570\u5f15\u7528\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002 for-each \u5faa\u73af\u5fc5\u987b\u77e5\u9053\u88ab\u904d\u5386\u5bb9\u5668\u7684\u5927\u5c0f\uff0c\u6240\u4ee5\u5b83\u4e0d\u80fd\u914d\u5408\u9000\u5316\u4e3a\u6307\u9488\u7684\u6570\u7ec4\u6216\u52a8\u6001\u6570\u7ec4\u5de5\u4f5c For-each \u5faa\u73af\u4e0d\u4ec5\u80fd\u591f\u914d\u5408\u56fa\u5b9a\u6570\u7ec4\u4e00\u8d77\u4f7f\u7528\uff0c\u5b83\u53ef\u4ee5\u548c\u591a\u79cd\u7c7b\u5217\u8868\u7684\u6570\u636e\u7ed3\u6784\u4e00\u8d77\u5de5\u4f5c\uff0c\u4f8b\u5982vector(\u4f8b\u5982 std::vector )\u3001\u94fe\u8868\u3001\u6811\u548c\u6620\u5c04\u3002 \u901a\u8fc7\u653e\u7f6e\u5728\u5faa\u73af\u53d8\u91cf\u7684\u524d\u9762\u7684 init-statement \u53ef\u4ee5\u50cf\u666e\u901afor\u5faa\u73af\u4e00\u6837\u521b\u5efa\u4e00\u4e2a\u7d22\u5f15(C++20)\u5e76\u5728\u5faa\u73af\u4e2d\u5bf9\u5176\u8fdb\u884c\u9012\u589e\u3002\u5728C++20\u4e4b\u524d\uff0c\u5fc5\u987b\u5728\u5faa\u73af\u5916\u58f0\u660e\u8fd9\u7d22\u5f15\uff0c\u6ce8\u610f\u53ef\u80fd\u4f1a\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\u7684\u60c5\u51b5\u3002 \u5728 11.3 - \u6570\u7ec4\u548c\u5faa\u73af \u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include // std::size int main () { constexpr int scores []{ 84 , 92 , 76 , 81 , 56 }; constexpr int numStudents { std :: size ( scores ) }; int maxScore { 0 }; // keep track of our largest score for ( int student { 0 }; student < numStudents ; ++ student ) { if ( scores [ student ] > maxScore ) { maxScore = scores [ student ]; } } std :: cout << \"The best score was \" << maxScore << '\\n' ; return 0 ; } \u5c3d\u7ba1\u4f7f\u7528 for \u5faa\u73af\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u904d\u5386\u4e00\u4e2a\u6570\u7ec4\uff0c\u4f46\u662f\u5728\u771f\u6b63\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u8fd9\u79cd\u65b9\u5f0f\u5bb9\u6613\u51fa\u9519\uff0c\u800c\u4e14\u4e5f\u5bb9\u6613\u72af \u5dee\u4e00\u9519\u8bef \u3002 \u4f7f\u7528 for-each \u5faa\u73af\uff08\u4e5f\u79f0\u4e3a \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \uff09\u53ef\u4ee5\u66f4\u52a0\u7b80\u4fbf\u3001\u66f4\u52a0\u5b89\u5168\u5730\u904d\u5386\u6570\u7ec4\uff08\u6216\u5176\u4ed6\u7c7b\u4f3c\u7684\u6570\u636e\u7ed3\u6784\uff09\u4e2d\u7684\u5143\u7d20\u3002 For-each \u5faa\u73af for-each \u5faa\u73af\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a 1 2 for ( element_declaration : array ) statement ; \u5f53\u6267\u884c\u6b64\u8bed\u53e5\u65f6\uff0c\u5faa\u73af\u5c06\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5c06\u5f53\u524d\u6570\u7ec4\u5143\u7d20\u7684\u503c\u8d4b\u7ed9 element_declaration \u4e2d\u58f0\u660e\u7684\u53d8\u91cf\u3002\u4e3a\u4e86\u83b7\u5f97\u6700\u4f73\u7ed3\u679c\uff0c element_declaration \u5e94\u8be5\u5177\u6709\u4e0e\u6570\u7ec4\u5143\u7d20\u76f8\u540c\u7684\u7c7b\u578b\uff0c\u5426\u5219\u5c06\u53d1\u751f\u7c7b\u578b\u8f6c\u6362\u3002 \u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5b83\u4f7f\u7528 for-each \u5faa\u73af\u6765\u6253\u5370\u6590\u6ce2\u90a3\u5951\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { constexpr int fibonacci []{ 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 }; for ( int number : fibonacci ) // iterate over array fibonacci { std :: cout << number << ' ' ; // we access the array element for this iteration through variable number } std :: cout << '\\n' ; return 0 ; } \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 0 1 1 2 3 5 8 13 21 34 55 89 \u8ba9\u6211\u4eec\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\u4e0a\u9762\u4ee3\u7801\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u9996\u5148\uff0c\u6267\u884c for \u5faa\u73af\uff0c\u53d8\u91cf number \u7684\u503c\u9996\u5148\u88ab\u8bbe\u7f6e\u4e3a\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u4e5f\u5c31\u662f 0\u3002\u7a0b\u5e8f\u968f\u540e\u6253\u5370\u8be5\u503c\uff0c\u5373\u6253\u53700.\u7136\u540e\uff0cfor \u5faa\u73af\u518d\u6b21\u6267\u884c\uff0c number \u88ab\u8bbe\u7f6e\u4e3a\u6570\u7ec4\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff0c\u53731\u3002\u7136\u540e\u6253\u53701\u3002for \u5faa\u73af\u4e0d\u65ad\u5730\u6267\u884c\uff0c\u4f9d\u6b21\u6253\u5370\u51fa\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u76f4\u5230\u904d\u5386\u5b8c\u5168\u90e8\u6570\u7ec4\u3002\u7136\u540e\uff0c\u5faa\u73af\u622a\u6b62\u4e86\uff0c\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c\uff08\u8fd4\u56de0\u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff09\u3002 \u6ce8\u610f\uff0c\u8fd9\u91cc\u7684 number \u5e76\u4e0d\u662f\u4e00\u4e2a\u6570\u7ec4\u7d22\u5f15\u3002\u5b83\u672c\u8eab\u88ab\u8d4b\u503c\u4e3a\u5f53\u524d\u904d\u5386\u5230\u7684\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002 For each \u5faa\u73af\u548c auto \u5173\u952e\u5b57 \u56e0\u4e3a element_declaration \u5e94\u8be5\u4e0e\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5c5e\u4e8e\u540c\u4e00\u7c7b\u578b\uff0c\u90a3\u4e48\u6700\u7406\u60f3\u7684\u65b9\u5f0f\u5c31\u662f\u4f7f\u7528 auto \u5173\u952e\u5b57\u58f0\u660e\u8be5\u53d8\u91cf\u5e76\u8ba9C++\u4e3a\u6211\u4eec\u81ea\u52a8\u63a8\u65ad\u5176\u7c7b\u578b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { constexpr int fibonacci []{ 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 }; for ( auto number : fibonacci ) // type is auto, so number has its type deduced from the fibonacci array { std :: cout << number << ' ' ; } std :: cout << '\\n' ; return 0 ; } For-each \u5faa\u73af\u548c\u5f15\u7528 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c element \u88ab\u58f0\u660e\u4e3a\u4e00\u4e2a\u503c\u53d8\u91cf\uff1a 1 2 3 4 5 std :: string array []{ \"peter\" , \"likes\" , \"frozen\" , \"yogurt\" }; for ( auto element : array ) // element will be a copy of the current array element { std :: cout << element << ' ' ; } \u8fd9\u610f\u5473\u7740\u5728\u904d\u5386\u6570\u7ec4\u65f6\uff0c\u5f53\u524d\u5143\u7d20\u4f1a\u88ab\u62f7\u8d1d\u5230 element \u3002\u62f7\u8d1d\u6570\u7ec4\u5143\u7d20\u7684\u5f00\u9500\u4f1a\u5f88\u5927\uff0c\u800c\u4e14\u591a\u6570\u60c5\u51b5\u4e0b\u6211\u4eec\u5b9e\u9645\u4e0a\u60f3\u8981\u8bbf\u95ee\u539f\u672c\u7684\u5143\u7d20\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u58f0\u660e\u4e3a\u5f15\u7528\u7c7b\u578b\uff1a 1 2 3 4 5 std :: string array []{ \"peter\" , \"likes\" , \"frozen\" , \"yogurt\" }; for ( auto & element : array ) // The ampersand makes element a reference to the actual array element, preventing a copy from being made { std :: cout << element << ' ' ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c element \u662f\u5f53\u524d\u8fed\u4ee3\u6570\u7ec4\u5143\u7d20\u7684\u5f15\u7528\uff0c\u4ece\u800c\u907f\u514d\u4e86\u590d\u5236\u3002\u6b64\u5916\uff0c\u5bf9 element \u7684\u4efb\u4f55\u4fee\u6539\u90fd\u4f1a\u5f71\u54cd\u6b63\u5728\u8fed\u4ee3\u7684\u6570\u7ec4\uff0c\u5982\u679c element \u662f\u4e00\u4e2a\u666e\u901a\u53d8\u91cf\uff0c\u8fd9\u662f\u4e0d\u53ef\u80fd\u7684\u3002 \u5f53\u7136\uff0c\u5982\u679c\u4f60\u6253\u7b97\u4ee5\u53ea\u8bfb\u7684\u65b9\u5f0f\u4f7f\u7528\u5f15\u7528\uff0c\u5c06\u5b83\u8bbe\u4e3a const \u662f\u4e2a\u597d\u4e3b\u610f\uff1a 1 2 3 4 5 std :: string array []{ \"peter\" , \"likes\" , \"frozen\" , \"yogurt\" }; for ( const auto & element : array ) // element is a const reference to the currently iterated array element { std :: cout << element << ' ' ; } \u6700\u4f73\u5b9e\u8df5 \u5728 for-each \u5faa\u73af\u4e2d\u58f0\u660e\u5143\u7d20\u65f6\uff0c\u5982\u679c\u5143\u7d20\u662f\u975e\u57fa\u672c\u7c7b\u578b\uff0c\u5219\u5c06\u5176\u58f0\u660e\u4e3a\u5f15\u7528\u6216\u5e38\u6570\u5f15\u7528\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002 \u4f7f\u7528 for-each \u5faa\u73af\u91cd\u5199\u6700\u9ad8\u5206\u6570\u7684\u4f8b\u5b50 \u63a5\u4e0b\u6765\uff0c\u4f7f\u7528 for-each \u91cd\u5199\u5f00\u5934\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { constexpr int scores []{ 84 , 92 , 76 , 81 , 56 }; int maxScore { 0 }; // keep track of our largest score for ( auto score : scores ) // iterate over array scores, assigning each value in turn to variable score { if ( score > maxScore ) { maxScore = score ; } } std :: cout << \"The best score was \" << maxScore << '\\n' ; return 0 ; } \u6ce8\u610f\uff0c\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u4e0d\u518d\u9700\u8981\u901a\u8fc7\u4e0b\u6807\u7d22\u5f15\u6570\u7ec4\u6216\u83b7\u53d6\u6570\u7ec4\u5927\u5c0f\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u53d8\u91cf score \u76f4\u63a5\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u3002\u6570\u7ec4\u5fc5\u987b\u6709\u5927\u5c0f\u4fe1\u606f\uff0c\u9000\u5316\u4e3a\u6307\u9488\u7684\u6570\u7ec4\u4e0d\u80fd\u5728for-each\u5faa\u73af\u4e2d\u4f7f\u7528\u3002 For-each \u5faa\u73af\u548c\u975e\u6570\u7ec4\u53d8\u91cf For-each \u5faa\u73af\u4e0d\u4ec5\u80fd\u914d\u5408\u56fa\u5b9a\u6570\u7ec4\u4e00\u8d77\u4f7f\u7528\uff0c\u5b83\u53ef\u4ee5\u548c\u591a\u79cd\u7c7b\u5217\u8868\u7684\u6570\u636e\u7ed3\u6784\u4e00\u8d77\u5de5\u4f5c\uff0c\u4f8b\u5982vector(\u4f8b\u5982 std::vector )\u3001\u94fe\u8868\u3001\u6811\u548c\u6620\u5c04 \u3002 \u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u8fd9\u4e9b\u5185\u5bb9\uff0c\u6240\u4ee5\u4f60\u4e0d\u77e5\u9053\u4e5f\u6ca1\u5173\u7cfb\u3002\u4f60\u73b0\u5728\u53ea\u9700\u8981\u8bb0\u4f4f\uff0cfor-each\u5faa\u73af\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u3001\u901a\u7528\u7684\u904d\u5386\u65b9\u6cd5\uff08\u4e0d\u4ec5\u4ec5\u662f\u904d\u5386\u6570\u7ec4\u800c\u5df2\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include int main () { std :: vector fibonacci { 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 }; // note use of std::vector here rather than a fixed array // Before C++17 // std::vector fibonacci{ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 }; for ( auto number : fibonacci ) { std :: cout << number << ' ' ; } std :: cout << '\\n' ; return 0 ; } For-each \u4e0d\u80fd\u548c\u6307\u5411\u6570\u7ec4\u7684\u6307\u9488\u4e00\u8d77\u5de5\u4f5c \u4e3a\u4e86\u904d\u5386\u6570\u7ec4\uff0cfor-each\u9700\u8981\u77e5\u9053\u6570\u7ec4\u7684\u5927\u5c0f\u3002\u56e0\u4e3a\u5df2\u7ecf\u9000\u5316\u4e3a\u6307\u9488\u7684\u6570\u7ec4\u4e0d\u518d\u5305\u542b\u6570\u7ec4\u5927\u5c0f\u7684\u4fe1\u606f\uff0c\u56e0\u6b64for-each\u5faa\u73af\u5c06\u65e0\u6cd5\u914d\u5408\u5b83\u4eec\u4e00\u8d77\u5de5\u4f5c\uff01 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int sumArray ( const int array []) // array is a pointer { int sum { 0 }; for ( auto number : array ) // compile error, the size of array isn't known { sum += number ; } return sum ; } int main () { constexpr int array []{ 9 , 7 , 5 , 3 , 1 }; std :: cout << sumArray ( array ) << '\\n' ; // array decays into a pointer here return 0 ; } \u540c\u6837\uff0c\u7531\u4e8e\u540c\u6837\u7684\u539f\u56e0\uff0c\u52a8\u6001\u6570\u7ec4\u4e5f\u4e0d\u80fd\u7528\u4e8efor-each\u5faa\u73af\u3002 \u80fd\u591f\u83b7\u53d6\u5f53\u524d\u5143\u7d20\u7684\u7d22\u5f15\u5417\uff1f For-each \u5faa\u73af\u4e0d\u80fd\u591f\u63d0\u4f9b\u8bbf\u95ee\u6570\u7ec4\u7d22\u5f15\u7684\u76f4\u63a5\u65b9\u6cd5\u3002\u8fd9\u662f\u56e0\u4e3a\u5f88\u591a\u914d\u5408for-each\u5faa\u73af\u5de5\u4f5c\u7684\u6570\u636e\u7ed3\u6784\uff08\u4f8b\u5982\u94fe\u8868\uff09\u5e76\u4e0d\u80fd\u591f\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5143\u7d20\u3002 \u4eceC++20\u5f00\u59cb\uff0c\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af\u53ef\u4ee5\u4e0e\u4e00\u4e2a\u521d\u59cb\u5316\u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u5c31\u50cf\u666e\u901afor\u5faa\u73af\u4e2d\u7684\u521d\u59cb\u5316\u8bed\u53e5\u4e00\u6837\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 init-statement \u6765\u521b\u5efa\u624b\u52a8\u7d22\u5f15\u8ba1\u6570\u5668\uff0c\u800c\u4e0d\u4f1a\u7834\u574ffor\u5faa\u73af\u6240\u5728\u7684\u51fd\u6570\u3002 init-statement \u5e94\u8be5\u653e\u7f6e\u5728\u5faa\u73af\u53d8\u91cf\u7684\u524d\u9762\uff1a 1 2 for ( init - statement ; element_declaration : array ) statement ; \u5728\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u6709\u4e24\u4e2a\u901a\u8fc7\u7d22\u5f15\u5173\u8054\u7684\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u540d\u5b57\u4e3a\u201c names[3] \u201d\u7684\u5b66\u751f\u7684\u5206\u6570\u4e3a\u201c scores[3] \u201d\u3002\u6bcf\u5f53\u53d1\u73b0\u4e00\u4e2a\u65b0\u7684\u9ad8\u5206\u5b66\u751f\u65f6\uff0c\u6211\u4eec\u5c31\u6253\u5370\u51fa\u4ed6\u4eec\u7684\u540d\u5b57\u548c\u4e0e\u524d\u4e00\u4e2a\u9ad8\u5206\u7684\u5206\u5dee\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include int main () { std :: string names []{ \"Alex\" , \"Betty\" , \"Caroline\" , \"Dave\" , \"Emily\" }; // Names of the students constexpr int scores []{ 84 , 92 , 76 , 81 , 56 }; int maxScore { 0 }; for ( int i { 0 }; auto score : scores ) // i is the index of the current element { if ( score > maxScore ) { std :: cout << names [ i ] << \" beat the previous best score of \" << maxScore << \" by \" << ( score - maxScore ) << \" points! \\n \" ; maxScore = score ; } ++ i ; } std :: cout << \"The best score was \" << maxScore << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 Alex beat the previous best score of 0 by 84 points! Betty beat the previous best score of 84 by 8 points! The best score was 92 \u8fd9\u91cc\u7684 int i{ 0 }; \u662f init-statement \uff0c\u5b83\u4f1a\u5728\u5faa\u73af\u6267\u884c\u65f6\u6267\u884c\u4e00\u6b21\u3002\u5728\u6bcf\u6b21\u5faa\u73af\u65f6\uff0c i \u5c31\u88ab\u9012\u589e1\uff0c\u7c7b\u4f3c\u4e0e\u666e\u901a\u7684\u5faa\u73af\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u6211\u4eec\u5728\u5faa\u73af\u4e2d\u4f7f\u7528 continue \u7684\u8bdd\uff0c ++i \u4f1a\u88ab\u8df3\u8fc7\uff0c\u5bfc\u81f4\u975e\u9884\u671f\u7684\u7ed3\u679c\u3002\u5982\u679c\u4f60\u9700\u8981\u4f7f\u7528 continue \u7684\u8bdd\uff0c\u8bf7\u786e\u4fdd i \u7684\u9012\u589e\u5728 continue \u4e4b\u524d\u8fdb\u884c\u3002 \u5728C++20\u4e4b\u524d\uff0c\u7d22\u5f15\u53d8\u91cf i \u5fc5\u987b\u5728\u5faa\u73af\u4e4b\u5916\u58f0\u660e\uff0c\u4f46\u8fd9\u53ef\u80fd\u5bfc\u81f4\u540d\u79f0\u51b2\u7a81\uff0c\u4f8b\u5982\u5f53\u6211\u4eec\u60f3\u5728\u540e\u9762\u7684\u51fd\u6570\u4e2d\u5b9a\u4e49\u53e6\u4e00\u4e2a\u540d\u4e3a i \u7684\u53d8\u91cf\u65f6\u3002 \u7ed3\u8bba \u5f53\u9700\u8981\u6309\u6b63\u5411\u987a\u5e8f\u8bbf\u95ee\u6240\u6709\u6570\u7ec4\u5143\u7d20\u65f6\uff0cFor-each \u5faa\u73af\u4e3a\u904d\u5386\u6570\u7ec4\u63d0\u4f9b\u4e86\u4e00\u79cd\u9ad8\u7ea7\u8bed\u6cd5\u3002\u5728\u53ef\u4ee5\u4f7f\u7528\u5b83\u7684\u60c5\u51b5\u4e0b\uff0c\u5b83\u5e94\u8be5\u4f18\u4e8e\u6807\u51c6\u7684for\u5faa\u73af\u3002\u4e3a\u4e86\u9632\u6b62\u590d\u5236\u6bcf\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u5c06\u5143\u7d20\u58f0\u660e\u4e3a\u5f15\u7528\u3002","title":"11.13 - for-each \u5faa\u73af"},{"location":"11-13-For-each-loops/#for-each","text":"for-each \u5faa\u73af\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a 1 2 for ( element_declaration : array ) statement ; \u5f53\u6267\u884c\u6b64\u8bed\u53e5\u65f6\uff0c\u5faa\u73af\u5c06\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5c06\u5f53\u524d\u6570\u7ec4\u5143\u7d20\u7684\u503c\u8d4b\u7ed9 element_declaration \u4e2d\u58f0\u660e\u7684\u53d8\u91cf\u3002\u4e3a\u4e86\u83b7\u5f97\u6700\u4f73\u7ed3\u679c\uff0c element_declaration \u5e94\u8be5\u5177\u6709\u4e0e\u6570\u7ec4\u5143\u7d20\u76f8\u540c\u7684\u7c7b\u578b\uff0c\u5426\u5219\u5c06\u53d1\u751f\u7c7b\u578b\u8f6c\u6362\u3002 \u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5b83\u4f7f\u7528 for-each \u5faa\u73af\u6765\u6253\u5370\u6590\u6ce2\u90a3\u5951\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { constexpr int fibonacci []{ 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 }; for ( int number : fibonacci ) // iterate over array fibonacci { std :: cout << number << ' ' ; // we access the array element for this iteration through variable number } std :: cout << '\\n' ; return 0 ; } \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 0 1 1 2 3 5 8 13 21 34 55 89 \u8ba9\u6211\u4eec\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\u4e0a\u9762\u4ee3\u7801\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u9996\u5148\uff0c\u6267\u884c for \u5faa\u73af\uff0c\u53d8\u91cf number \u7684\u503c\u9996\u5148\u88ab\u8bbe\u7f6e\u4e3a\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u4e5f\u5c31\u662f 0\u3002\u7a0b\u5e8f\u968f\u540e\u6253\u5370\u8be5\u503c\uff0c\u5373\u6253\u53700.\u7136\u540e\uff0cfor \u5faa\u73af\u518d\u6b21\u6267\u884c\uff0c number \u88ab\u8bbe\u7f6e\u4e3a\u6570\u7ec4\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff0c\u53731\u3002\u7136\u540e\u6253\u53701\u3002for \u5faa\u73af\u4e0d\u65ad\u5730\u6267\u884c\uff0c\u4f9d\u6b21\u6253\u5370\u51fa\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u76f4\u5230\u904d\u5386\u5b8c\u5168\u90e8\u6570\u7ec4\u3002\u7136\u540e\uff0c\u5faa\u73af\u622a\u6b62\u4e86\uff0c\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c\uff08\u8fd4\u56de0\u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff09\u3002 \u6ce8\u610f\uff0c\u8fd9\u91cc\u7684 number \u5e76\u4e0d\u662f\u4e00\u4e2a\u6570\u7ec4\u7d22\u5f15\u3002\u5b83\u672c\u8eab\u88ab\u8d4b\u503c\u4e3a\u5f53\u524d\u904d\u5386\u5230\u7684\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002","title":"For-each \u5faa\u73af"},{"location":"11-13-For-each-loops/#for-each-auto","text":"\u56e0\u4e3a element_declaration \u5e94\u8be5\u4e0e\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5c5e\u4e8e\u540c\u4e00\u7c7b\u578b\uff0c\u90a3\u4e48\u6700\u7406\u60f3\u7684\u65b9\u5f0f\u5c31\u662f\u4f7f\u7528 auto \u5173\u952e\u5b57\u58f0\u660e\u8be5\u53d8\u91cf\u5e76\u8ba9C++\u4e3a\u6211\u4eec\u81ea\u52a8\u63a8\u65ad\u5176\u7c7b\u578b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { constexpr int fibonacci []{ 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 }; for ( auto number : fibonacci ) // type is auto, so number has its type deduced from the fibonacci array { std :: cout << number << ' ' ; } std :: cout << '\\n' ; return 0 ; }","title":"For each \u5faa\u73af\u548c auto\u5173\u952e\u5b57"},{"location":"11-13-For-each-loops/#for-each_1","text":"\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c element \u88ab\u58f0\u660e\u4e3a\u4e00\u4e2a\u503c\u53d8\u91cf\uff1a 1 2 3 4 5 std :: string array []{ \"peter\" , \"likes\" , \"frozen\" , \"yogurt\" }; for ( auto element : array ) // element will be a copy of the current array element { std :: cout << element << ' ' ; } \u8fd9\u610f\u5473\u7740\u5728\u904d\u5386\u6570\u7ec4\u65f6\uff0c\u5f53\u524d\u5143\u7d20\u4f1a\u88ab\u62f7\u8d1d\u5230 element \u3002\u62f7\u8d1d\u6570\u7ec4\u5143\u7d20\u7684\u5f00\u9500\u4f1a\u5f88\u5927\uff0c\u800c\u4e14\u591a\u6570\u60c5\u51b5\u4e0b\u6211\u4eec\u5b9e\u9645\u4e0a\u60f3\u8981\u8bbf\u95ee\u539f\u672c\u7684\u5143\u7d20\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u58f0\u660e\u4e3a\u5f15\u7528\u7c7b\u578b\uff1a 1 2 3 4 5 std :: string array []{ \"peter\" , \"likes\" , \"frozen\" , \"yogurt\" }; for ( auto & element : array ) // The ampersand makes element a reference to the actual array element, preventing a copy from being made { std :: cout << element << ' ' ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c element \u662f\u5f53\u524d\u8fed\u4ee3\u6570\u7ec4\u5143\u7d20\u7684\u5f15\u7528\uff0c\u4ece\u800c\u907f\u514d\u4e86\u590d\u5236\u3002\u6b64\u5916\uff0c\u5bf9 element \u7684\u4efb\u4f55\u4fee\u6539\u90fd\u4f1a\u5f71\u54cd\u6b63\u5728\u8fed\u4ee3\u7684\u6570\u7ec4\uff0c\u5982\u679c element \u662f\u4e00\u4e2a\u666e\u901a\u53d8\u91cf\uff0c\u8fd9\u662f\u4e0d\u53ef\u80fd\u7684\u3002 \u5f53\u7136\uff0c\u5982\u679c\u4f60\u6253\u7b97\u4ee5\u53ea\u8bfb\u7684\u65b9\u5f0f\u4f7f\u7528\u5f15\u7528\uff0c\u5c06\u5b83\u8bbe\u4e3a const \u662f\u4e2a\u597d\u4e3b\u610f\uff1a 1 2 3 4 5 std :: string array []{ \"peter\" , \"likes\" , \"frozen\" , \"yogurt\" }; for ( const auto & element : array ) // element is a const reference to the currently iterated array element { std :: cout << element << ' ' ; } \u6700\u4f73\u5b9e\u8df5 \u5728 for-each \u5faa\u73af\u4e2d\u58f0\u660e\u5143\u7d20\u65f6\uff0c\u5982\u679c\u5143\u7d20\u662f\u975e\u57fa\u672c\u7c7b\u578b\uff0c\u5219\u5c06\u5176\u58f0\u660e\u4e3a\u5f15\u7528\u6216\u5e38\u6570\u5f15\u7528\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002","title":"For-each \u5faa\u73af\u548c\u5f15\u7528"},{"location":"11-13-For-each-loops/#for-each_2","text":"\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528 for-each \u91cd\u5199\u5f00\u5934\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { constexpr int scores []{ 84 , 92 , 76 , 81 , 56 }; int maxScore { 0 }; // keep track of our largest score for ( auto score : scores ) // iterate over array scores, assigning each value in turn to variable score { if ( score > maxScore ) { maxScore = score ; } } std :: cout << \"The best score was \" << maxScore << '\\n' ; return 0 ; } \u6ce8\u610f\uff0c\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u4e0d\u518d\u9700\u8981\u901a\u8fc7\u4e0b\u6807\u7d22\u5f15\u6570\u7ec4\u6216\u83b7\u53d6\u6570\u7ec4\u5927\u5c0f\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u53d8\u91cf score \u76f4\u63a5\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u3002\u6570\u7ec4\u5fc5\u987b\u6709\u5927\u5c0f\u4fe1\u606f\uff0c\u9000\u5316\u4e3a\u6307\u9488\u7684\u6570\u7ec4\u4e0d\u80fd\u5728for-each\u5faa\u73af\u4e2d\u4f7f\u7528\u3002","title":"\u4f7f\u7528 for-each \u5faa\u73af\u91cd\u5199\u6700\u9ad8\u5206\u6570\u7684\u4f8b\u5b50"},{"location":"11-13-For-each-loops/#for-each_3","text":"For-each \u5faa\u73af\u4e0d\u4ec5\u80fd\u914d\u5408\u56fa\u5b9a\u6570\u7ec4\u4e00\u8d77\u4f7f\u7528\uff0c\u5b83\u53ef\u4ee5\u548c\u591a\u79cd\u7c7b\u5217\u8868\u7684\u6570\u636e\u7ed3\u6784\u4e00\u8d77\u5de5\u4f5c\uff0c\u4f8b\u5982vector(\u4f8b\u5982 std::vector )\u3001\u94fe\u8868\u3001\u6811\u548c\u6620\u5c04 \u3002 \u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u8fd9\u4e9b\u5185\u5bb9\uff0c\u6240\u4ee5\u4f60\u4e0d\u77e5\u9053\u4e5f\u6ca1\u5173\u7cfb\u3002\u4f60\u73b0\u5728\u53ea\u9700\u8981\u8bb0\u4f4f\uff0cfor-each\u5faa\u73af\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u3001\u901a\u7528\u7684\u904d\u5386\u65b9\u6cd5\uff08\u4e0d\u4ec5\u4ec5\u662f\u904d\u5386\u6570\u7ec4\u800c\u5df2\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include int main () { std :: vector fibonacci { 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 }; // note use of std::vector here rather than a fixed array // Before C++17 // std::vector fibonacci{ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 }; for ( auto number : fibonacci ) { std :: cout << number << ' ' ; } std :: cout << '\\n' ; return 0 ; }","title":"For-each \u5faa\u73af\u548c\u975e\u6570\u7ec4\u53d8\u91cf"},{"location":"11-13-For-each-loops/#for-each_4","text":"\u4e3a\u4e86\u904d\u5386\u6570\u7ec4\uff0cfor-each\u9700\u8981\u77e5\u9053\u6570\u7ec4\u7684\u5927\u5c0f\u3002\u56e0\u4e3a\u5df2\u7ecf\u9000\u5316\u4e3a\u6307\u9488\u7684\u6570\u7ec4\u4e0d\u518d\u5305\u542b\u6570\u7ec4\u5927\u5c0f\u7684\u4fe1\u606f\uff0c\u56e0\u6b64for-each\u5faa\u73af\u5c06\u65e0\u6cd5\u914d\u5408\u5b83\u4eec\u4e00\u8d77\u5de5\u4f5c\uff01 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int sumArray ( const int array []) // array is a pointer { int sum { 0 }; for ( auto number : array ) // compile error, the size of array isn't known { sum += number ; } return sum ; } int main () { constexpr int array []{ 9 , 7 , 5 , 3 , 1 }; std :: cout << sumArray ( array ) << '\\n' ; // array decays into a pointer here return 0 ; } \u540c\u6837\uff0c\u7531\u4e8e\u540c\u6837\u7684\u539f\u56e0\uff0c\u52a8\u6001\u6570\u7ec4\u4e5f\u4e0d\u80fd\u7528\u4e8efor-each\u5faa\u73af\u3002","title":"For-each \u4e0d\u80fd\u548c\u6307\u5411\u6570\u7ec4\u7684\u6307\u9488\u4e00\u8d77\u5de5\u4f5c"},{"location":"11-13-For-each-loops/#_1","text":"For-each \u5faa\u73af\u4e0d\u80fd\u591f\u63d0\u4f9b\u8bbf\u95ee\u6570\u7ec4\u7d22\u5f15\u7684\u76f4\u63a5\u65b9\u6cd5\u3002\u8fd9\u662f\u56e0\u4e3a\u5f88\u591a\u914d\u5408for-each\u5faa\u73af\u5de5\u4f5c\u7684\u6570\u636e\u7ed3\u6784\uff08\u4f8b\u5982\u94fe\u8868\uff09\u5e76\u4e0d\u80fd\u591f\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5143\u7d20\u3002 \u4eceC++20\u5f00\u59cb\uff0c\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af\u53ef\u4ee5\u4e0e\u4e00\u4e2a\u521d\u59cb\u5316\u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u5c31\u50cf\u666e\u901afor\u5faa\u73af\u4e2d\u7684\u521d\u59cb\u5316\u8bed\u53e5\u4e00\u6837\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 init-statement \u6765\u521b\u5efa\u624b\u52a8\u7d22\u5f15\u8ba1\u6570\u5668\uff0c\u800c\u4e0d\u4f1a\u7834\u574ffor\u5faa\u73af\u6240\u5728\u7684\u51fd\u6570\u3002 init-statement \u5e94\u8be5\u653e\u7f6e\u5728\u5faa\u73af\u53d8\u91cf\u7684\u524d\u9762\uff1a 1 2 for ( init - statement ; element_declaration : array ) statement ; \u5728\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u6709\u4e24\u4e2a\u901a\u8fc7\u7d22\u5f15\u5173\u8054\u7684\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u540d\u5b57\u4e3a\u201c names[3] \u201d\u7684\u5b66\u751f\u7684\u5206\u6570\u4e3a\u201c scores[3] \u201d\u3002\u6bcf\u5f53\u53d1\u73b0\u4e00\u4e2a\u65b0\u7684\u9ad8\u5206\u5b66\u751f\u65f6\uff0c\u6211\u4eec\u5c31\u6253\u5370\u51fa\u4ed6\u4eec\u7684\u540d\u5b57\u548c\u4e0e\u524d\u4e00\u4e2a\u9ad8\u5206\u7684\u5206\u5dee\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include int main () { std :: string names []{ \"Alex\" , \"Betty\" , \"Caroline\" , \"Dave\" , \"Emily\" }; // Names of the students constexpr int scores []{ 84 , 92 , 76 , 81 , 56 }; int maxScore { 0 }; for ( int i { 0 }; auto score : scores ) // i is the index of the current element { if ( score > maxScore ) { std :: cout << names [ i ] << \" beat the previous best score of \" << maxScore << \" by \" << ( score - maxScore ) << \" points! \\n \" ; maxScore = score ; } ++ i ; } std :: cout << \"The best score was \" << maxScore << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 Alex beat the previous best score of 0 by 84 points! Betty beat the previous best score of 84 by 8 points! The best score was 92 \u8fd9\u91cc\u7684 int i{ 0 }; \u662f init-statement \uff0c\u5b83\u4f1a\u5728\u5faa\u73af\u6267\u884c\u65f6\u6267\u884c\u4e00\u6b21\u3002\u5728\u6bcf\u6b21\u5faa\u73af\u65f6\uff0c i \u5c31\u88ab\u9012\u589e1\uff0c\u7c7b\u4f3c\u4e0e\u666e\u901a\u7684\u5faa\u73af\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u6211\u4eec\u5728\u5faa\u73af\u4e2d\u4f7f\u7528 continue \u7684\u8bdd\uff0c ++i \u4f1a\u88ab\u8df3\u8fc7\uff0c\u5bfc\u81f4\u975e\u9884\u671f\u7684\u7ed3\u679c\u3002\u5982\u679c\u4f60\u9700\u8981\u4f7f\u7528 continue \u7684\u8bdd\uff0c\u8bf7\u786e\u4fdd i \u7684\u9012\u589e\u5728 continue \u4e4b\u524d\u8fdb\u884c\u3002 \u5728C++20\u4e4b\u524d\uff0c\u7d22\u5f15\u53d8\u91cf i \u5fc5\u987b\u5728\u5faa\u73af\u4e4b\u5916\u58f0\u660e\uff0c\u4f46\u8fd9\u53ef\u80fd\u5bfc\u81f4\u540d\u79f0\u51b2\u7a81\uff0c\u4f8b\u5982\u5f53\u6211\u4eec\u60f3\u5728\u540e\u9762\u7684\u51fd\u6570\u4e2d\u5b9a\u4e49\u53e6\u4e00\u4e2a\u540d\u4e3a i \u7684\u53d8\u91cf\u65f6\u3002","title":"\u80fd\u591f\u83b7\u53d6\u5f53\u524d\u5143\u7d20\u7684\u7d22\u5f15\u5417\uff1f"},{"location":"11-13-For-each-loops/#_2","text":"\u5f53\u9700\u8981\u6309\u6b63\u5411\u987a\u5e8f\u8bbf\u95ee\u6240\u6709\u6570\u7ec4\u5143\u7d20\u65f6\uff0cFor-each \u5faa\u73af\u4e3a\u904d\u5386\u6570\u7ec4\u63d0\u4f9b\u4e86\u4e00\u79cd\u9ad8\u7ea7\u8bed\u6cd5\u3002\u5728\u53ef\u4ee5\u4f7f\u7528\u5b83\u7684\u60c5\u51b5\u4e0b\uff0c\u5b83\u5e94\u8be5\u4f18\u4e8e\u6807\u51c6\u7684for\u5faa\u73af\u3002\u4e3a\u4e86\u9632\u6b62\u590d\u5236\u6bcf\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u5c06\u5143\u7d20\u58f0\u660e\u4e3a\u5f15\u7528\u3002","title":"\u7ed3\u8bba"},{"location":"11-14-Void-pointers/","tags":["void"],"text":"void \u6307\u9488 \uff0c\u4e5f\u88ab\u79f0\u4e4b\u4e3a\u6cdb\u578b\u6307\u9488\uff0c\u662f\u4e00\u79cd\u53ef\u4ee5\u6307\u5411\u4efb\u610f\u7c7b\u578b\u5bf9\u8c61\u7684\u6307\u9488\uff01void\u6307\u9488\u7684\u58f0\u660e\u7c7b\u4f3c\u4e8e\u666e\u901a\u6307\u9488\uff0c\u4f7f\u7528 void \u5173\u952e\u5b57\u6807\u8bc6\u6307\u9488\u7c7b\u578b\uff1a 1 void * ptr ; // ptr is a void pointer void \u6307\u9488\u53ef\u4ee5\u6307\u5411\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u7684\u5bf9\u8c61\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int nValue ; float fValue ; struct Something { int n ; float f ; }; Something sValue ; void * ptr ; ptr = & nValue ; // valid ptr = & fValue ; // valid ptr = & sValue ; // valid \u4e0d\u8fc7\uff0c\u7531\u4e8evoid\u6307\u9488\u5e76\u4e0d\u77e5\u9053\u5b83\u6240\u6307\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u6240\u4ee5\u5806void\u6307\u9488\u8fdb\u884c \u89e3\u5f15\u7528 \u662f\u975e\u6cd5\u64cd\u4f5c\u3002void \u6307\u9488\u5fc5\u987b\u9996\u5148\u88ab\u8f6c\u6362\u4e3a\u5176\u4ed6\u7279\u5b9a\u7c7b\u578b\u7684\u6307\u9488\uff0c\u7136\u540e\u624d\u80fd\u8fdb\u884c\u89e3\u5f15\u7528\u3002 1 2 3 4 5 6 7 8 int value { 5 }; void * voidPtr { & value }; // std::cout << *voidPtr << '\\n'; // illegal: dereference of void pointer int * intPtr { static_cast < int *> ( voidPtr ) }; // \u9996\u5148\u5c06void\u6307\u9488\u8f6c\u6362\u4e3aint\u6307\u9488 std :: cout << * intPtr << '\\n' ; // \u7136\u540e\u89e3\u5f15\u7528\u5f97\u5230\u7ed3\u679c \u8fd0\u884c\u7ed3\u679c\uff1a 1 5 \u63a5\u4e0b\u6765\u7684\u95ee\u9898\u662f\uff1a\u5982\u679c void \u6307\u9488\u4e0d\u77e5\u9053\u5b83\u6240\u6307\u7684\u5bf9\u8c61\u662f\u4ec0\u4e48\u7c7b\u578b\u7684\u8bdd\uff0c\u6211\u4eec\u53c8\u662f\u5982\u4f55\u5f97\u5e94\u8be5\u5c06\u5b83\u8f6c\u6362\u6210\u4ec0\u4e48\u7c7b\u578b\u5462\uff1f\u5b9e\u9645\u4e0a\uff0c\u4f60\u5fc5\u987b\u81ea\u5df1\u53bb\u8ddf\u8e2a\u8fd9\u4e2a\u7c7b\u578b\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 void \u6307\u9488\u7684\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include #include enum class Type { tInt , // note: we can't use \"int\" here because it's a keyword, so we'll use \"tInt\" instead tFloat , tCString }; void printValue ( void * ptr , Type type ) { switch ( type ) { case Type :: tInt : std :: cout << * static_cast < int *> ( ptr ) << '\\n' ; // cast to int pointer and perform indirection break ; case Type :: tFloat : std :: cout << * static_cast < float *> ( ptr ) << '\\n' ; // cast to float pointer and perform indirection break ; case Type :: tCString : std :: cout << static_cast < char *> ( ptr ) << '\\n' ; // cast to char pointer (no indirection) // std::cout will treat char* as a C-style string // if we were to perform indirection through the result, then we'd just print the single char that ptr is pointing to break ; default : assert ( false && \"type not found\" ); break ; } } int main () { int nValue { 5 }; float fValue { 7.5f }; char szValue []{ \"Mollie\" }; printValue ( & nValue , Type :: tInt ); printValue ( & fValue , Type :: tFloat ); printValue ( szValue , Type :: tCString ); return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 5 7.5 Mollie void \u6307\u9488\u5176\u4ed6\u7ec6\u8282 void \u6307\u9488\u53ef\u4ee5\u88ab\u8bbe\u7f6e\u4e3a null \u503c\uff1a 1 void * ptr { nullptr }; // ptr is a void pointer that is currently a null pointer \u5c3d\u7ba1\u4e00\u4e9b\u7f16\u8bd1\u5668\u5141\u8bb8\u5220\u9664\u6307\u5411\u52a8\u6001\u5206\u914d\u5185\u5b58\u7684\u7a7a\u6307\u9488\uff0c\u4f46\u5e94\u8be5\u907f\u514d\u8fd9\u6837\u505a\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e0d\u80fd\u5728void\u6307\u9488\u4e0a\u505a\u6307\u9488\u7b97\u672f\u8fd0\u7b97\u3002\u8fd9\u662f\u56e0\u4e3a\u6307\u9488\u7b97\u672f\u8981\u6c42\u6307\u9488\u77e5\u9053\u5b83\u6240\u6307\u5411\u7684\u5bf9\u8c61\u7684\u5927\u5c0f\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u9002\u5f53\u5730\u589e\u52a0\u6216\u51cf\u5c11\u6307\u9488\u3002 \u6ce8\u610f\uff0c\u6ca1\u6709void\u5f15\u7528\u8fd9\u79cd\u4e1c\u897f\u3002\u8fd9\u662f\u56e0\u4e3avoid\u5f15\u7528\u7684\u7c7b\u578b\u662fvoid &\uff0c\u5e76\u4e14\u4e0d\u77e5\u9053\u5b83\u5f15\u7528\u7684\u662f\u4ec0\u4e48\u7c7b\u578b\u7684\u503c\u3002 \u5c0f\u7ed3 \u4e00\u822c\u6765\u8bf4\uff0c\u9664\u975e\u7edd\u5bf9\u5fc5\u8981\uff0c\u5426\u5219\u5e94\u8be5\u907f\u514d\u4f7f\u7528void\u6307\u9488\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ef\u4ee5\u907f\u514d\u7c7b\u578b\u68c0\u67e5\uff0c\u8fd9\u4f7f\u5f97\u4f60\u5728\u4e0d\u7ecf\u610f\u95f4\u72af\u7684\u9519\u8bef\u65e0\u6cd5\u88ab\u7f16\u8bd1\u5668\u53d1\u73b0\u3002\u4f8b\u5982\uff1a 1 2 int nValue { 5 }; printValue ( & nValue , Type :: tCString ); \u4f46\u8c01\u77e5\u9053\u7ed3\u679c\u4f1a\u600e\u6837\u5462\uff01 \u5c3d\u7ba1\u4e0a\u9762\u7684\u51fd\u6570\u770b\u8d77\u6765\u50cf\u662f\u4e00\u79cd\u8ba9\u4e00\u4e2a\u51fd\u6570\u5904\u7406\u591a\u4e2a\u6570\u636e\u7c7b\u578b\u7684\u7b80\u6d01\u65b9\u6cd5\uff0c\u4f46C++\u5b9e\u9645\u4e0a\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u597d\u7684\u65b9\u6cd5(\u901a\u8fc7\u51fd\u6570\u91cd\u8f7d)\u6765\u505a\u540c\u6837\u7684\u4e8b\u60c5\uff0c\u8be5\u65b9\u6cd5\u4fdd\u7559\u4e86\u7c7b\u578b\u68c0\u67e5\uff0c\u4ee5\u907f\u514d\u6ee5\u7528\u3002\u8bb8\u591a\u5176\u4ed6\u5730\u65b9\u66fe\u7ecf\u4f7f\u7528void\u6307\u9488\u6765\u5904\u7406\u591a\u4e2a\u6570\u636e\u7c7b\u578b\uff0c\u73b0\u5728\u53ef\u4ee5\u4f7f\u7528\u6a21\u677f\u6765\u66f4\u597d\u5730\u5b8c\u6210\uff0c\u6a21\u677f\u4e5f\u63d0\u4f9b\u4e86\u5f3a\u7c7b\u578b\u68c0\u67e5\u3002 \u4f46\u662f\uff0c\u5728\u975e\u5e38\u5076\u7136\u7684\u60c5\u51b5\u4e0b\uff0cvoid\u6307\u9488\u4e5f\u662f\u5f88\u6709\u7528\u7684\u3002\u4f46\u662f\u5728\u4f7f\u7528\u524d\u8981\u9996\u5148\u786e\u4fdd\u6ca1\u6709\u66f4\u597d(\u66f4\u5b89\u5168)\u7684\u65b9\u6cd5\u6765\u4f7f\u7528\u5176\u4ed6\u8bed\u8a00\u673a\u5236\u5b8c\u6210\u540c\u6837\u7684\u4e8b\u60c5\uff01","title":"11.14 \u2014 void \u6307\u9488"},{"location":"11-14-Void-pointers/#void","text":"void \u6307\u9488\u53ef\u4ee5\u88ab\u8bbe\u7f6e\u4e3a null \u503c\uff1a 1 void * ptr { nullptr }; // ptr is a void pointer that is currently a null pointer \u5c3d\u7ba1\u4e00\u4e9b\u7f16\u8bd1\u5668\u5141\u8bb8\u5220\u9664\u6307\u5411\u52a8\u6001\u5206\u914d\u5185\u5b58\u7684\u7a7a\u6307\u9488\uff0c\u4f46\u5e94\u8be5\u907f\u514d\u8fd9\u6837\u505a\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e0d\u80fd\u5728void\u6307\u9488\u4e0a\u505a\u6307\u9488\u7b97\u672f\u8fd0\u7b97\u3002\u8fd9\u662f\u56e0\u4e3a\u6307\u9488\u7b97\u672f\u8981\u6c42\u6307\u9488\u77e5\u9053\u5b83\u6240\u6307\u5411\u7684\u5bf9\u8c61\u7684\u5927\u5c0f\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u9002\u5f53\u5730\u589e\u52a0\u6216\u51cf\u5c11\u6307\u9488\u3002 \u6ce8\u610f\uff0c\u6ca1\u6709void\u5f15\u7528\u8fd9\u79cd\u4e1c\u897f\u3002\u8fd9\u662f\u56e0\u4e3avoid\u5f15\u7528\u7684\u7c7b\u578b\u662fvoid &\uff0c\u5e76\u4e14\u4e0d\u77e5\u9053\u5b83\u5f15\u7528\u7684\u662f\u4ec0\u4e48\u7c7b\u578b\u7684\u503c\u3002","title":"void \u6307\u9488\u5176\u4ed6\u7ec6\u8282"},{"location":"11-14-Void-pointers/#_1","text":"\u4e00\u822c\u6765\u8bf4\uff0c\u9664\u975e\u7edd\u5bf9\u5fc5\u8981\uff0c\u5426\u5219\u5e94\u8be5\u907f\u514d\u4f7f\u7528void\u6307\u9488\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ef\u4ee5\u907f\u514d\u7c7b\u578b\u68c0\u67e5\uff0c\u8fd9\u4f7f\u5f97\u4f60\u5728\u4e0d\u7ecf\u610f\u95f4\u72af\u7684\u9519\u8bef\u65e0\u6cd5\u88ab\u7f16\u8bd1\u5668\u53d1\u73b0\u3002\u4f8b\u5982\uff1a 1 2 int nValue { 5 }; printValue ( & nValue , Type :: tCString ); \u4f46\u8c01\u77e5\u9053\u7ed3\u679c\u4f1a\u600e\u6837\u5462\uff01 \u5c3d\u7ba1\u4e0a\u9762\u7684\u51fd\u6570\u770b\u8d77\u6765\u50cf\u662f\u4e00\u79cd\u8ba9\u4e00\u4e2a\u51fd\u6570\u5904\u7406\u591a\u4e2a\u6570\u636e\u7c7b\u578b\u7684\u7b80\u6d01\u65b9\u6cd5\uff0c\u4f46C++\u5b9e\u9645\u4e0a\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u597d\u7684\u65b9\u6cd5(\u901a\u8fc7\u51fd\u6570\u91cd\u8f7d)\u6765\u505a\u540c\u6837\u7684\u4e8b\u60c5\uff0c\u8be5\u65b9\u6cd5\u4fdd\u7559\u4e86\u7c7b\u578b\u68c0\u67e5\uff0c\u4ee5\u907f\u514d\u6ee5\u7528\u3002\u8bb8\u591a\u5176\u4ed6\u5730\u65b9\u66fe\u7ecf\u4f7f\u7528void\u6307\u9488\u6765\u5904\u7406\u591a\u4e2a\u6570\u636e\u7c7b\u578b\uff0c\u73b0\u5728\u53ef\u4ee5\u4f7f\u7528\u6a21\u677f\u6765\u66f4\u597d\u5730\u5b8c\u6210\uff0c\u6a21\u677f\u4e5f\u63d0\u4f9b\u4e86\u5f3a\u7c7b\u578b\u68c0\u67e5\u3002 \u4f46\u662f\uff0c\u5728\u975e\u5e38\u5076\u7136\u7684\u60c5\u51b5\u4e0b\uff0cvoid\u6307\u9488\u4e5f\u662f\u5f88\u6709\u7528\u7684\u3002\u4f46\u662f\u5728\u4f7f\u7528\u524d\u8981\u9996\u5148\u786e\u4fdd\u6ca1\u6709\u66f4\u597d(\u66f4\u5b89\u5168)\u7684\u65b9\u6cd5\u6765\u4f7f\u7528\u5176\u4ed6\u8bed\u8a00\u673a\u5236\u5b8c\u6210\u540c\u6837\u7684\u4e8b\u60c5\uff01","title":"\u5c0f\u7ed3"},{"location":"11-15-Pointers-to-pointers-and-dynamic-multidimensional-arrays/","tags":["pointer"],"text":"Key Takeaway \u672c\u8bfe\u7a0b\u662f\u53ef\u9009\u7684\uff0c\u9002\u7528\u4e8e\u60f3\u8981\u4e86\u89e3\u66f4\u591aC++\u77e5\u8bc6\u7684\u9ad8\u7ea7\u8bfb\u8005\u3002\u672a\u6765\u7684\u8bfe\u7a0b\u5e76\u4e0d\u4f1a\u5efa\u7acb\u5728\u8fd9\u8282\u8bfe\u4e0a\u3002 \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u6b63\u662f\u4f60\u6240\u671f\u671b\u7684\u90a3\u6837\uff1a\u5b83\u662f\u4fdd\u5b58\u53e6\u4e00\u4e2a\u6307\u9488\u5730\u5740\u7684\u6307\u9488\u3002 \u6307\u5411\u6307\u9488\u7684\u6307\u9488 \u6307\u5411int\u578b\u7684\u666e\u901a\u6307\u9488\u4f7f\u7528\u5355\u4e2a\u661f\u53f7\u6765\u58f0\u660e\uff1a 1 int * ptr ; // pointer to an int, one asterisk \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u4f7f\u7528\u4e24\u4e2a\u661f\u53f7\u6765\u58f0\u660e\uff1a 1 int ** ptrptr ; // pointer to a pointer to an int, two asterisks \u4e00\u4e2a\u6307\u5411int\u578b\u7684\u666e\u901a\u6307\u9488\u4f7f\u7528\u4e00\u4e2a\u661f\u53f7\u58f0\u660e\uff1a\u6307\u5411\u6307\u9488\u7684\u6307\u9488\u548c\u666e\u901a\u6307\u9488\u7684\u5de5\u4f5c\u65b9\u5f0f\u4e00\u6837\u2014\u2014\u4f60\u53ef\u4ee5\u89e3\u5f15\u7528\u5b83\u6765\u68c0\u7d22\u6307\u5411\u7684\u503c\u3002\u56e0\u4e3a\u8be5\u503c\u672c\u8eab\u662f\u4e00\u4e2a\u6307\u9488\uff0c\u6240\u4ee5\u53ef\u4ee5\u518d\u6b21\u89e3\u9664\u5f15\u7528\u4ee5\u83b7\u5f97\u5e95\u5c42\u503c\u3002\u8fd9\u4e9b\u89e3\u5f15\u7528\u53ef\u4ee5\u8fde\u7eed\u6267\u884c: 1 2 3 4 5 6 7 int value { 5 }; int * ptr { & value }; std :: cout << * ptr << '\\n' ; // Dereference pointer to int to get int value int ** ptrptr { & ptr }; std :: cout << ** ptrptr << '\\n' ; // dereference to get pointer to int, dereference again to get int value \u4e0a\u8ff0\u7a0b\u5e8f\u6253\u5370\uff1a 1 2 5 5 \u6ce8\u610f\uff1a\u6307\u9488\u7684\u6307\u9488\u4e0d\u80fd\u76f4\u63a5\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u503c\uff1a 1 2 int value { 5 }; int ** ptrptr { && value }; // not valid \u8fd9\u662f\u56e0\u4e3a\u53d6\u5730\u5740\u64cd\u4f5c\u7b26\u9700\u8981\u4e00\u4e2a \u5de6\u503c \u4f5c\u4e3a\u64cd\u4f5c\u6570\uff0c\u4f46\u662f &value \u662f\u4e00\u4e2a \u53f3\u503c \u3002 \u4f46\u662f\uff0c\u5b83\u53ef\u4ee5\u88ab\u8bbe\u7f6e\u4e3a\u7a7a\u6307\u9488\uff1a 1 int ** ptrptr { nullptr }; \u6307\u9488\u6570\u7ec4 \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u6709\u4e00\u4e9b\u7528\u9014\u3002\u6700\u5e38\u89c1\u7684\u7528\u6cd5\u662f\u52a8\u6001\u5206\u914d\u6307\u9488\u6570\u7ec4\uff1a 1 int ** array { new int * [ 10 ] }; // allocate an array of 10 int pointers \u4e0a\u8ff0\u4ee3\u7801\u548c\u4e00\u822c\u7684\u52a8\u6001\u6570\u7ec4\u884c\u4e3a\u4e00\u81f4\uff0c\u53ea\u4e0d\u8fc7\u6570\u7ec4\u5143\u7d20\u662f\u201c\u6574\u578b\u6307\u9488\u201d\u800c\u4e0d\u662f\u6574\u578b\u7f62\u4e86\u3002 \u4e8c\u7ef4\u52a8\u6001\u6570\u7ec4 \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u7684\u53e6\u5916\u4e00\u4e2a\u5e38\u89c1\u7528\u9014\u662f\u5206\u914d\u591a\u7ef4\u6570\u7ec4(\u53c2\u89c1 11.5 - \u591a\u7ef4\u6570\u7ec4 )\u3002 \u4e8c\u7ef4\u56fa\u5b9a\u6570\u7ec4\u7684\u58f0\u660e\u975e\u5e38\u7b80\u5355\uff1a 1 int array [ 10 ][ 5 ]; \u4f46\u662f\u52a8\u6001\u6570\u7ec4\u5374\u7a0d\u5fae\u6709\u4e9b\u590d\u6742\u3002\u4f60\u53ef\u80fd\u4f1a\u5c1d\u8bd5\u8fd9\u6837\u505a\uff1a 1 int ** array { new int [ 10 ][ 5 ] }; // won\u2019t work! \u4f46\u8fd9\u6837\u505a\u662f\u9519\u8bef\u7684\u3002 \u53f3\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5b9e\u73b0\uff0c\u5982\u679c\u5217\u662f\u56fa\u5b9a\u7684\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 2 int x { 7 }; // non-constant int ( * array )[ 5 ] { new int [ x ][ 5 ] }; // rightmost dimension must be constexpr \u4e3a\u786e\u4fdd\u6b63\u786e\u7684\u4f18\u5148\u7ea7\uff0c\u8fd9\u91cc\u7684\u62ec\u53f7\u662f\u5fc5\u987b\u7684\u3002\u8fd9\u91cc\u7279\u522b\u9002\u5408\u4f7f\u7528 \u7c7b\u578b\u63a8\u65ad \uff1a 1 2 int x { 7 }; // non-constant auto array { new int [ x ][ 5 ] }; // so much simpler! \u4e0d\u5e78\u7684\u662f\uff0c\u5982\u679c\u6700\u53f3\u8fb9\u7684\u6570\u7ec4\u7ef4\u5ea6\u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\uff0c\u8fd9\u4e2a\u76f8\u5bf9\u7b80\u5355\u7684\u89e3\u51b3\u65b9\u6848\u5c31\u4e0d\u8d77\u4f5c\u7528\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u64cd\u4f5c\u5c31\u66f4\u590d\u6742\u4e86\u3002\u9996\u5148\uff0c\u6211\u4eec\u5206\u914d\u4e00\u4e2a\u6307\u9488\u6570\u7ec4(\u5982\u4e0a\u6240\u8ff0)\u3002\u7136\u540e\u904d\u5386\u6307\u9488\u6570\u7ec4\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u5206\u914d\u4e00\u4e2a\u52a8\u6001\u6570\u7ec4\u3002\u6211\u4eec\u7684\u52a8\u6001\u4e8c\u7ef4\u6570\u7ec4\u662f\u52a8\u6001\u4e00\u7ef4\u6570\u7ec4\u7684\u52a8\u6001\u4e00\u7ef4\u6570\u7ec4! 1 2 3 int ** array { new int * [ 10 ] }; // allocate an array of 10 int pointers \u2014 these are our rows for ( int count { 0 }; count < 10 ; ++ count ) array [ count ] = new int [ 5 ]; // these are our columns \u8bbf\u95ee\u6570\u7ec4\u7684\u65b9\u5f0f\u548c\u5e73\u5e38\u4e00\u6837\uff1a 1 array [ 9 ][ 4 ] = 3 ; // This is the same as (array[9])[4] = 3; \u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\uff0c\u7531\u4e8e\u6bcf\u4e2a\u6570\u7ec4\u5217\u90fd\u662f\u72ec\u7acb\u52a8\u6001\u5206\u914d\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u52a8\u6001\u5206\u914d\u975e\u77e9\u5f62\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u4e09\u89d2\u5f62\u6570\u7ec4\uff1a 1 2 3 int ** array { new int * [ 10 ] }; // allocate an array of 10 int pointers \u2014 these are our rows for ( int count { 0 }; count < 10 ; ++ count ) array [ count ] = new int [ count + 1 ]; // these are our columns COPY In the above example, note that array[0] is an array of length 1, array[1] is an array of length 2, etc\u2026 Deallocating a dynamically allocated two-dimensional array using this method requires a loop as well: 1 2 3 for ( int count { 0 }; count < 10 ; ++ count ) delete [] array [ count ]; delete [] array ; // this needs to be done last COPY Note that we delete the array in the opposite order that we created it (elements first, then the array itself). If we delete array before the array columns, then we\u2019d have to access deallocated memory to delete the array columns. And that would result in undefined behavior. Because allocating and deallocating two-dimensional arrays is complex and easy to mess up, it\u2019s often easier to \u201cflatten\u201d a two-dimensional array (of size x by y) into a one-dimensional array of size x * y : 1 2 3 4 5 6 7 // Instead of this: int ** array { new int * [ 10 ] }; // allocate an array of 10 int pointers \u2014 these are our rows for ( int count { 0 }; count < 10 ; ++ count ) array [ count ] = new int [ 5 ]; // these are our columns // Do this int * array { new int [ 50 ] }; // a 10x5 array flattened into a single array COPY Simple math can then be used to convert a row and column index for a rectangular two-dimensional array into a single index for a one-dimensional array: 1 2 3 4 5 6 7 int getSingleIndex ( int row , int col , int numberOfColumnsInArray ) { return ( row * numberOfColumnsInArray ) + col ; } // set array[9,4] to 3 using our flattened array array [ getSingleIndex ( 9 , 4 , 5 )] = 3 ; COPY \u901a\u8fc7\u5730\u5740\u4f20\u9012\u6307\u9488 \u5c31\u50cf\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\u5f62\u53c2\u6765\u66f4\u6539\u4f20\u5165\u7684\u5e95\u5c42\u53c2\u6570\u7684\u5b9e\u9645\u503c\u4e00\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6307\u9488\u4f20\u9012\u7ed9\u4e00\u4e2a\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\uff0c\u7136\u540e\u4f7f\u7528\u8be5\u6307\u9488\u6765\u66f4\u6539\u5b83\u6240\u6307\u5411\u7684\u6307\u9488\u7684\u503c(\u8fd8\u4e0d\u660e\u767d\u5417?) However, if we want a function to be able to modify what a pointer argument points to, this is generally better done using a reference to a pointer instead. This is covered in lesson 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 \u6307\u9488\u7684\u6307\u9488\u7684\u6307\u9488 \u58f0\u660e\u6307\u5411\u6307\u9488\u7684\u6307\u9488\u7684\u6307\u9488\u4e5f\u662f\u53ef\u4ee5\u7684\uff1a 1 int *** ptrx3 ; \u8fd9\u6837\u53ef\u4ee5\u52a8\u6001\u5206\u914d\u4e00\u4e2a\u4e09\u7ef4\u6570\u7ec4\u3002\u7136\u800c\uff0c\u8fd9\u6837\u505a\u5c06\u9700\u8981\u4e00\u4e2a\u5faa\u73af\u4e2d\u7684\u5faa\u73af\uff0c\u5e76\u4e14\u8981\u5f97\u5230\u6b63\u786e\u7684\u7ed3\u679c\u662f\u6781\u5176\u590d\u6742\u7684\u3002 \u4f60\u751a\u81f3\u53ef\u4ee5\u58f0\u660e\u4e00\u4e2a\u56db\u91cd\u6307\u9488\uff1a 1 int **** ptrx4 ; \u53ea\u8981\u4f60\u613f\u610f\uff0c\u66f4\u9ad8\u5c42\u7684\u6307\u9488\u4e5f\u662f\u53ef\u4ee5\u7684\u3002 \u4f46\u662f\uff0c\u73b0\u5b9e\u4e2d\u5e76\u4e0d\u4f1a\u51fa\u73b0\u8fd9\u6837\u7684\u5b9a\u4e49\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u4f1a\u4f7f\u7528\u8fd9\u4e48\u591a\u5c42\u6b21\u7684\u95f4\u63a5\u5173\u7cfb\u3002 \u5c0f\u7ed3 \u6211\u4eec\u5efa\u8bae\u907f\u514d\u4f7f\u7528\u6307\u5411\u6307\u9488\u7684\u6307\u9488\uff0c\u9664\u975e\u6ca1\u6709\u5176\u4ed6\u53ef\u7528\u7684\u9009\u9879\uff0c\u56e0\u4e3a\u5b83\u4eec\u4f7f\u7528\u8d77\u6765\u5f88\u590d\u6742\uff0c\u800c\u4e14\u6709\u6f5c\u5728\u7684\u5371\u9669\u3002\u7528\u666e\u901a\u6307\u9488\u89e3\u9664\u5bf9\u7a7a\u6307\u9488\u6216\u60ac\u6d6e\u6307\u9488\u7684\u5f15\u7528\u975e\u5e38\u7b80\u5355\u2014\u2014\u4f7f\u7528\u6307\u5411\u6307\u9488\u7684\u6307\u9488\u66f4\u52a0\u5bb9\u6613\uff0c\u56e0\u4e3a\u5fc5\u987b\u6267\u884c\u53cc\u91cd\u89e3\u9664\u5f15\u7528\u624d\u80fd\u5f97\u5230\u5e95\u5c42\u503c\uff01","title":"11.15 \u2014 \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u548c\u591a\u7ef4\u6570\u7ec4"},{"location":"11-15-Pointers-to-pointers-and-dynamic-multidimensional-arrays/#_1","text":"\u6307\u5411int\u578b\u7684\u666e\u901a\u6307\u9488\u4f7f\u7528\u5355\u4e2a\u661f\u53f7\u6765\u58f0\u660e\uff1a 1 int * ptr ; // pointer to an int, one asterisk \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u4f7f\u7528\u4e24\u4e2a\u661f\u53f7\u6765\u58f0\u660e\uff1a 1 int ** ptrptr ; // pointer to a pointer to an int, two asterisks \u4e00\u4e2a\u6307\u5411int\u578b\u7684\u666e\u901a\u6307\u9488\u4f7f\u7528\u4e00\u4e2a\u661f\u53f7\u58f0\u660e\uff1a\u6307\u5411\u6307\u9488\u7684\u6307\u9488\u548c\u666e\u901a\u6307\u9488\u7684\u5de5\u4f5c\u65b9\u5f0f\u4e00\u6837\u2014\u2014\u4f60\u53ef\u4ee5\u89e3\u5f15\u7528\u5b83\u6765\u68c0\u7d22\u6307\u5411\u7684\u503c\u3002\u56e0\u4e3a\u8be5\u503c\u672c\u8eab\u662f\u4e00\u4e2a\u6307\u9488\uff0c\u6240\u4ee5\u53ef\u4ee5\u518d\u6b21\u89e3\u9664\u5f15\u7528\u4ee5\u83b7\u5f97\u5e95\u5c42\u503c\u3002\u8fd9\u4e9b\u89e3\u5f15\u7528\u53ef\u4ee5\u8fde\u7eed\u6267\u884c: 1 2 3 4 5 6 7 int value { 5 }; int * ptr { & value }; std :: cout << * ptr << '\\n' ; // Dereference pointer to int to get int value int ** ptrptr { & ptr }; std :: cout << ** ptrptr << '\\n' ; // dereference to get pointer to int, dereference again to get int value \u4e0a\u8ff0\u7a0b\u5e8f\u6253\u5370\uff1a 1 2 5 5 \u6ce8\u610f\uff1a\u6307\u9488\u7684\u6307\u9488\u4e0d\u80fd\u76f4\u63a5\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u503c\uff1a 1 2 int value { 5 }; int ** ptrptr { && value }; // not valid \u8fd9\u662f\u56e0\u4e3a\u53d6\u5730\u5740\u64cd\u4f5c\u7b26\u9700\u8981\u4e00\u4e2a \u5de6\u503c \u4f5c\u4e3a\u64cd\u4f5c\u6570\uff0c\u4f46\u662f &value \u662f\u4e00\u4e2a \u53f3\u503c \u3002 \u4f46\u662f\uff0c\u5b83\u53ef\u4ee5\u88ab\u8bbe\u7f6e\u4e3a\u7a7a\u6307\u9488\uff1a 1 int ** ptrptr { nullptr };","title":"\u6307\u5411\u6307\u9488\u7684\u6307\u9488"},{"location":"11-15-Pointers-to-pointers-and-dynamic-multidimensional-arrays/#_2","text":"\u6307\u5411\u6307\u9488\u7684\u6307\u9488\u6709\u4e00\u4e9b\u7528\u9014\u3002\u6700\u5e38\u89c1\u7684\u7528\u6cd5\u662f\u52a8\u6001\u5206\u914d\u6307\u9488\u6570\u7ec4\uff1a 1 int ** array { new int * [ 10 ] }; // allocate an array of 10 int pointers \u4e0a\u8ff0\u4ee3\u7801\u548c\u4e00\u822c\u7684\u52a8\u6001\u6570\u7ec4\u884c\u4e3a\u4e00\u81f4\uff0c\u53ea\u4e0d\u8fc7\u6570\u7ec4\u5143\u7d20\u662f\u201c\u6574\u578b\u6307\u9488\u201d\u800c\u4e0d\u662f\u6574\u578b\u7f62\u4e86\u3002","title":"\u6307\u9488\u6570\u7ec4"},{"location":"11-15-Pointers-to-pointers-and-dynamic-multidimensional-arrays/#_3","text":"\u6307\u5411\u6307\u9488\u7684\u6307\u9488\u7684\u53e6\u5916\u4e00\u4e2a\u5e38\u89c1\u7528\u9014\u662f\u5206\u914d\u591a\u7ef4\u6570\u7ec4(\u53c2\u89c1 11.5 - \u591a\u7ef4\u6570\u7ec4 )\u3002 \u4e8c\u7ef4\u56fa\u5b9a\u6570\u7ec4\u7684\u58f0\u660e\u975e\u5e38\u7b80\u5355\uff1a 1 int array [ 10 ][ 5 ]; \u4f46\u662f\u52a8\u6001\u6570\u7ec4\u5374\u7a0d\u5fae\u6709\u4e9b\u590d\u6742\u3002\u4f60\u53ef\u80fd\u4f1a\u5c1d\u8bd5\u8fd9\u6837\u505a\uff1a 1 int ** array { new int [ 10 ][ 5 ] }; // won\u2019t work! \u4f46\u8fd9\u6837\u505a\u662f\u9519\u8bef\u7684\u3002 \u53f3\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5b9e\u73b0\uff0c\u5982\u679c\u5217\u662f\u56fa\u5b9a\u7684\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 2 int x { 7 }; // non-constant int ( * array )[ 5 ] { new int [ x ][ 5 ] }; // rightmost dimension must be constexpr \u4e3a\u786e\u4fdd\u6b63\u786e\u7684\u4f18\u5148\u7ea7\uff0c\u8fd9\u91cc\u7684\u62ec\u53f7\u662f\u5fc5\u987b\u7684\u3002\u8fd9\u91cc\u7279\u522b\u9002\u5408\u4f7f\u7528 \u7c7b\u578b\u63a8\u65ad \uff1a 1 2 int x { 7 }; // non-constant auto array { new int [ x ][ 5 ] }; // so much simpler! \u4e0d\u5e78\u7684\u662f\uff0c\u5982\u679c\u6700\u53f3\u8fb9\u7684\u6570\u7ec4\u7ef4\u5ea6\u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\uff0c\u8fd9\u4e2a\u76f8\u5bf9\u7b80\u5355\u7684\u89e3\u51b3\u65b9\u6848\u5c31\u4e0d\u8d77\u4f5c\u7528\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u64cd\u4f5c\u5c31\u66f4\u590d\u6742\u4e86\u3002\u9996\u5148\uff0c\u6211\u4eec\u5206\u914d\u4e00\u4e2a\u6307\u9488\u6570\u7ec4(\u5982\u4e0a\u6240\u8ff0)\u3002\u7136\u540e\u904d\u5386\u6307\u9488\u6570\u7ec4\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u5206\u914d\u4e00\u4e2a\u52a8\u6001\u6570\u7ec4\u3002\u6211\u4eec\u7684\u52a8\u6001\u4e8c\u7ef4\u6570\u7ec4\u662f\u52a8\u6001\u4e00\u7ef4\u6570\u7ec4\u7684\u52a8\u6001\u4e00\u7ef4\u6570\u7ec4! 1 2 3 int ** array { new int * [ 10 ] }; // allocate an array of 10 int pointers \u2014 these are our rows for ( int count { 0 }; count < 10 ; ++ count ) array [ count ] = new int [ 5 ]; // these are our columns \u8bbf\u95ee\u6570\u7ec4\u7684\u65b9\u5f0f\u548c\u5e73\u5e38\u4e00\u6837\uff1a 1 array [ 9 ][ 4 ] = 3 ; // This is the same as (array[9])[4] = 3; \u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\uff0c\u7531\u4e8e\u6bcf\u4e2a\u6570\u7ec4\u5217\u90fd\u662f\u72ec\u7acb\u52a8\u6001\u5206\u914d\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u52a8\u6001\u5206\u914d\u975e\u77e9\u5f62\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u4e09\u89d2\u5f62\u6570\u7ec4\uff1a 1 2 3 int ** array { new int * [ 10 ] }; // allocate an array of 10 int pointers \u2014 these are our rows for ( int count { 0 }; count < 10 ; ++ count ) array [ count ] = new int [ count + 1 ]; // these are our columns COPY In the above example, note that array[0] is an array of length 1, array[1] is an array of length 2, etc\u2026 Deallocating a dynamically allocated two-dimensional array using this method requires a loop as well: 1 2 3 for ( int count { 0 }; count < 10 ; ++ count ) delete [] array [ count ]; delete [] array ; // this needs to be done last COPY Note that we delete the array in the opposite order that we created it (elements first, then the array itself). If we delete array before the array columns, then we\u2019d have to access deallocated memory to delete the array columns. And that would result in undefined behavior. Because allocating and deallocating two-dimensional arrays is complex and easy to mess up, it\u2019s often easier to \u201cflatten\u201d a two-dimensional array (of size x by y) into a one-dimensional array of size x * y : 1 2 3 4 5 6 7 // Instead of this: int ** array { new int * [ 10 ] }; // allocate an array of 10 int pointers \u2014 these are our rows for ( int count { 0 }; count < 10 ; ++ count ) array [ count ] = new int [ 5 ]; // these are our columns // Do this int * array { new int [ 50 ] }; // a 10x5 array flattened into a single array COPY Simple math can then be used to convert a row and column index for a rectangular two-dimensional array into a single index for a one-dimensional array: 1 2 3 4 5 6 7 int getSingleIndex ( int row , int col , int numberOfColumnsInArray ) { return ( row * numberOfColumnsInArray ) + col ; } // set array[9,4] to 3 using our flattened array array [ getSingleIndex ( 9 , 4 , 5 )] = 3 ; COPY","title":"\u4e8c\u7ef4\u52a8\u6001\u6570\u7ec4"},{"location":"11-15-Pointers-to-pointers-and-dynamic-multidimensional-arrays/#_4","text":"\u5c31\u50cf\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\u5f62\u53c2\u6765\u66f4\u6539\u4f20\u5165\u7684\u5e95\u5c42\u53c2\u6570\u7684\u5b9e\u9645\u503c\u4e00\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6307\u9488\u4f20\u9012\u7ed9\u4e00\u4e2a\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\uff0c\u7136\u540e\u4f7f\u7528\u8be5\u6307\u9488\u6765\u66f4\u6539\u5b83\u6240\u6307\u5411\u7684\u6307\u9488\u7684\u503c(\u8fd8\u4e0d\u660e\u767d\u5417?) However, if we want a function to be able to modify what a pointer argument points to, this is generally better done using a reference to a pointer instead. This is covered in lesson 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2","title":"\u901a\u8fc7\u5730\u5740\u4f20\u9012\u6307\u9488"},{"location":"11-15-Pointers-to-pointers-and-dynamic-multidimensional-arrays/#_5","text":"\u58f0\u660e\u6307\u5411\u6307\u9488\u7684\u6307\u9488\u7684\u6307\u9488\u4e5f\u662f\u53ef\u4ee5\u7684\uff1a 1 int *** ptrx3 ; \u8fd9\u6837\u53ef\u4ee5\u52a8\u6001\u5206\u914d\u4e00\u4e2a\u4e09\u7ef4\u6570\u7ec4\u3002\u7136\u800c\uff0c\u8fd9\u6837\u505a\u5c06\u9700\u8981\u4e00\u4e2a\u5faa\u73af\u4e2d\u7684\u5faa\u73af\uff0c\u5e76\u4e14\u8981\u5f97\u5230\u6b63\u786e\u7684\u7ed3\u679c\u662f\u6781\u5176\u590d\u6742\u7684\u3002 \u4f60\u751a\u81f3\u53ef\u4ee5\u58f0\u660e\u4e00\u4e2a\u56db\u91cd\u6307\u9488\uff1a 1 int **** ptrx4 ; \u53ea\u8981\u4f60\u613f\u610f\uff0c\u66f4\u9ad8\u5c42\u7684\u6307\u9488\u4e5f\u662f\u53ef\u4ee5\u7684\u3002 \u4f46\u662f\uff0c\u73b0\u5b9e\u4e2d\u5e76\u4e0d\u4f1a\u51fa\u73b0\u8fd9\u6837\u7684\u5b9a\u4e49\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u4f1a\u4f7f\u7528\u8fd9\u4e48\u591a\u5c42\u6b21\u7684\u95f4\u63a5\u5173\u7cfb\u3002","title":"\u6307\u9488\u7684\u6307\u9488\u7684\u6307\u9488"},{"location":"11-15-Pointers-to-pointers-and-dynamic-multidimensional-arrays/#_6","text":"\u6211\u4eec\u5efa\u8bae\u907f\u514d\u4f7f\u7528\u6307\u5411\u6307\u9488\u7684\u6307\u9488\uff0c\u9664\u975e\u6ca1\u6709\u5176\u4ed6\u53ef\u7528\u7684\u9009\u9879\uff0c\u56e0\u4e3a\u5b83\u4eec\u4f7f\u7528\u8d77\u6765\u5f88\u590d\u6742\uff0c\u800c\u4e14\u6709\u6f5c\u5728\u7684\u5371\u9669\u3002\u7528\u666e\u901a\u6307\u9488\u89e3\u9664\u5bf9\u7a7a\u6307\u9488\u6216\u60ac\u6d6e\u6307\u9488\u7684\u5f15\u7528\u975e\u5e38\u7b80\u5355\u2014\u2014\u4f7f\u7528\u6307\u5411\u6307\u9488\u7684\u6307\u9488\u66f4\u52a0\u5bb9\u6613\uff0c\u56e0\u4e3a\u5fc5\u987b\u6267\u884c\u53cc\u91cd\u89e3\u9664\u5f15\u7528\u624d\u80fd\u5f97\u5230\u5e95\u5c42\u503c\uff01","title":"\u5c0f\u7ed3"},{"location":"11-16-An-introduction-to-std-array/","tags":["std::array","C++17"],"text":"Key Takeaway - \u5728\u4e0a\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86 \u56fa\u5b9a\u6570\u7ec4 \u548c \u52a8\u6001\u6570\u7ec4 \u3002\u5c3d\u7ba1\u8fd9\u4e24\u79cd\u6570\u7ec4\u90fd\u662fC++\u81ea\u5e26\u7684\uff0c\u4f46\u662f\u5b83\u4eec\u7f3a\u70b9\u53ef\u4e0d\u5c11\uff1a\u56fa\u5b9a\u6570\u7ec4\u4f1a\u9000\u5316\u4e3a\u6307\u9488\uff0c\u9000\u5316\u548c\u5c06\u4f1a\u4e22\u5931\u6570\u7ec4\u7684\u957f\u5ea6\u4fe1\u606f\u3002\u52a8\u6001\u6570\u7ec4\u5728\u91ca\u653e\u65f6\u5bb9\u6613\u51fa\u73b0\u95ee\u9898\uff0c\u800c\u4e14\u5728\u91cd\u65b0\u8c03\u6574\u5927\u5c0f\u65f6\u4e5f\u5f88\u96be\u505a\u5230\u4e0d\u51fa\u9519\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\uff0cC++\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86 std::array \u548c std::vector \u4e24\u4e2a\u5bb9\u5668\u7c7b\uff0c\u4f7f\u5f97\u6570\u7ec4\u7ba1\u7406\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u4e86\u3002\u6211\u4eec\u4f1a\u5728\u672c\u8282\u8bfe\u4e2d\u4ecb\u7ecd std::array \uff0c\u7136\u540e\u5728\u4e0b\u8282\u8bfe\u4e2d\u4ecb\u7ecd std::vector \u3002 std::array std::array \u5b9e\u73b0\u4e86\u56fa\u5b9a\u6570\u7ec4\u7684\u529f\u80fd\uff0c\u4e14\u5728\u4f20\u5165\u51fd\u6570\u65f6\u4e0d\u4f1a\u9000\u5316\u4e3a\u6307\u9488\u3002 std::array \u88ab\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d\u4e14\u4f4d\u4e8e std \u547d\u540d\u7a7a\u95f4 \u5185\u3002 \u58f0\u660e std::array \u53d8\u91cf\u5f88\u7b80\u5355\uff1a 1 2 3 #include std :: array < int , 3 > myArray ; // \u58f0\u660e\u4e00\u4e2a\u957f\u5ea6\u4e3a3\u7684\u6574\u578b\u6570\u7ec4 \u548c\u539f\u751f\u7684\u56fa\u5b9a\u6570\u7ec4\u5b9e\u73b0\u7c7b\u4f3c\uff0c std::array \u7684\u957f\u5ea6\u5fc5\u987b\u5728 \u7f16\u8bd1\u65f6 \u786e\u5b9a\u3002 std::array \u53ef\u4ee5\u901a\u8fc7 \u521d\u59cb\u5316\u503c\u5217\u8868 \u6216 \u5217\u8868\u521d\u59cb\u5316 \u7684\u65b9\u5f0f\u8fdb\u884c\u521d\u59cb\u5316\uff1a 1 2 std :: array < int , 5 > myArray = { 9 , 7 , 5 , 3 , 1 }; // \u521d\u59cb\u5316\u503c\u5217\u8868 std :: array < int , 5 > myArray2 { 9 , 7 , 5 , 3 , 1 }; // \u5217\u8868\u521d\u59cb\u5316 \u548cC++\u81ea\u5e26\u7684\u56fa\u5b9a\u6570\u7ec4\u4e0d\u540c\u7684\u662f\uff0c\u5728\u4f7f\u7528 std::array \u7684\u65f6\u5019\uff0c\u4f60\u4e0d\u80fd\u7701\u7565\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u5373\u4f7f\u63d0\u4f9b\u4e86\u521d\u59cb\u5316\u503c\u4e5f\u4e0d\u884c\uff1a 1 2 std :: array < int , > myArray { 9 , 7 , 5 , 3 , 1 }; // \u4e0d\u5408\u6cd5\uff0c\u5fc5\u987b\u63d0\u4f9b\u957f\u5ea6 std :: array < int > myArray { 9 , 7 , 5 , 3 , 1 }; // \u4e0d\u5408\u6cd5\uff0c\u5fc5\u987b\u63d0\u4f9b\u957f\u5ea6 \u4e0d\u8fc7\uff0c\u4eceC++17\u5f00\u59cb\uff0c std::array \u4e5f\u652f\u6301\u7701\u7565\u7c7b\u578b\u548c\u5927\u5c0f\u4e86\u3002\u5982\u679c\u6570\u7ec4\u88ab\u663e\u5f0f\u5730\u521d\u59cb\u5316\uff0c\u5219\u7c7b\u578b\u548c\u5927\u5c0f\u53ef\u4ee5\u4e00\u8d77\u88ab\u5ffd\u7565\u3002 1 2 std :: array myArray { 9 , 7 , 5 , 3 , 1 }; // \u901a\u8fc7\u7c7b\u578b\u63a8\u65ad\u5f97\u5230 std::array std :: array myArray { 9.7 , 7.31 }; // \u901a\u8fc7\u7c7b\u578b\u63a8\u65ad\u5f97\u5230 std::array \u6211\u4eec\u63a8\u8350\u4f7f\u7528\u4e0a\u9762\u8fd9\u79cd\u5ffd\u7565\u7c7b\u578b\u548c\u5927\u5c0f\u7684\u58f0\u660e\u65b9\u5f0f\uff0c\u9664\u975e\u4f60\u7684\u7f16\u8bd1\u5668\u4e0d\u652f\u6301C++17\uff0c\u90a3\u4e48\u4f60\u5c31\u5fc5\u987b\u663e\u5f0f\u5730\u6307\u5b9a\u4e86\u7c7b\u578b\u548c\u5927\u5c0f\u3002 1 2 3 4 5 // std::array myArray { 9, 7, 5, 3, 1 }; // Since C++17 std :: array < int , 5 > myArray { 9 , 7 , 5 , 3 , 1 }; // Before C++17 // std::array myArray { 9.7, 7.31 }; // Since C++17 std :: array < double , 2 > myArray { 9.7 , 7.31 }; // Before C++17 \u5728 C++20 \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u5143\u7d20\u7c7b\u578b\uff0c\u4f46\u662f\u5ffd\u7565\u6570\u7ec4\u957f\u5ea6\u3002\u8fd9\u4e48\u505a\u7684\u65f6\u5019\uff0c\u521b\u5efa std::array \u65f6\u770b\u8d77\u6765\u66f4\u50cf\u662f\u521b\u5efa\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u6570\u7ec4\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 std::to_array \u6765\u521b\u5efa\u6570\u7ec4\uff08\u6307\u5b9a\u5143\u7d20\u7c7b\u578b\u4f46\u81ea\u52a8\u63a8\u65ad\u957f\u5ea6\uff09\uff1a 1 2 3 auto myArray1 { std :: to_array < int , 5 > ({ 9 , 7 , 5 , 3 , 1 }) }; // Specify type and size auto myArray2 { std :: to_array < int > ({ 9 , 7 , 5 , 3 , 1 }) }; // Specify type only, deduce size auto myArray3 { std :: to_array ({ 9 , 7 , 5 , 3 , 1 }) }; // Deduce type and size \u4e0d\u5e78\u7684\u662f\uff0c\u4f7f\u7528 std::to_array \u521b\u5efa\u6570\u7ec4\u65f6\u7684\u5f00\u9500\u6bd4\u76f4\u63a5\u4f7f\u7528 std::array \u521b\u5efa\u6570\u7ec4\u5f00\u9500\u66f4\u5927\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u5c06\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u6570\u7ec4\u7684\u5143\u7d20\u62f7\u8d1d\u5230 std::array \u3002\u51fa\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u5982\u679c\u9700\u8981\u5728\u5faa\u73af\u4e2d\u591a\u6b21\u521b\u5efa\u6570\u7ec4\u65f6\uff0c\u5e94\u8be5\u907f\u514d\u4f7f\u7528 std::to_array \u3002 \u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 \u521d\u59cb\u5316\u503c\u5217\u8868 \u4e3a\u6570\u7ec4\u8d4b\u503c\u3002 1 2 3 4 std :: array < int , 5 > myArray ; myArray = { 0 , 1 , 2 , 3 , 4 }; // okay myArray = { 9 , 8 , 7 }; // okay, elements 3 and 4 are set to zero! myArray = { 0 , 1 , 2 , 3 , 4 , 5 }; // not allowed, too many elements in initializer list! \u6076\u610f\u4f7f\u7528\u4e0b\u6807\u8fd0\u7b97\u7b26\u8bbf\u95ee std::array \u6570\u7ec4\u4e2d\u7684\u503c\uff1a 1 2 std :: cout << myArray [ 1 ] << '\\n' ; myArray [ 2 ] = 6 ; \u548c\u5185\u7f6e\u7684\u6570\u7ec4\u7c7b\u4f3c\uff0c\u4e0b\u6807\u8fd0\u7b97\u7b26\u5e76\u6ca1\u6709\u5b9e\u73b0\u8d8a\u754c\u68c0\u67e5\u3002\u5f53\u4f20\u5165\u975e\u6cd5\u7d22\u5f15\u65f6\uff0c\u4f1a\u53d1\u751f\u5f88\u4e0d\u597d\u7684\u4e8b\u60c5\u3002 std::array \u8fd8\u652f\u6301\u53e6\u5916\u4e00\u79cd\u5143\u7d20\u8bbf\u95ee\u65b9\u5f0f\uff08\u4f7f\u7528 at() \u51fd\u6570\uff09\uff0c\u5b83\u4f1a\u5728\uff08 \u8fd0\u884c\u65f6 \uff09\u8fdb\u884c\u8d8a\u754c\u68c0\u67e5\uff1a 1 2 3 std :: array myArray { 9 , 7 , 5 , 3 , 1 }; myArray . at ( 1 ) = 6 ; // array element 1 is valid, sets array element 1 to value 6 myArray . at ( 9 ) = 10 ; // array element 9 is invalid, will throw a runtime error \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u8c03\u7528 myArray.at(1) \u65f6\u4f1a\u786e\u4fdd\u6240\u4ee51\u662f\u5408\u6cd5\u7684\u3002\u7531\u4e8e1\u786e\u5b9e\u662f\u5408\u6cd5\u7d22\u5f15\uff0c\u6240\u4ee5\u5b83\u4f1a\u8fd4\u56de\u5bf9\u5e94\u7684\u5143\u7d20\u7684\u5f15\u7528\u3002\u6211\u4eec\u5c066\u8d4b\u503c\u7ed9\u8be5\u5f15\u7528\u3002\u4e0d\u8fc7\uff0c\u5f53\u6211\u4eec\u8c03\u7528 myArray.at(9) \u65f6\u5c31\u4f1a\u5931\u8d25\uff0c\u56e0\u4e3a\u7d22\u5f159\u8d8a\u754c\u4e86\u3002\u5f53\u8d8a\u754c\u53d1\u751f\u65f6\uff0c at() \u51fd\u6570\u4f1a\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\uff08\u5177\u4f53\u6765\u8bf4\u5b83\u4f1a\u629b\u51fa\u4e00\u4e2a std::out_of_range \u5f02\u5e38\uff09\uff0c\u800c\u4e0d\u4f1a\u8fd4\u56de\u5bf9\u5e94\u7684\u7d22\u5f15\u3002\u56e0\u4e3a\u9700\u8981\u8fdb\u884c\u8d8a\u754c\u68c0\u67e5\uff0c\u6240\u4ee5 at() \u7684\u901f\u5ea6\u4f1a\u6bd4\u4e0b\u6807\u8fd0\u7b97\u7b26\u6162\uff0c\u4f46\u5b83\u66f4\u5b89\u5168\u3002 std::array \u4f1a\u5728 \u79bb\u5f00\u4f5c\u7528\u57df \u65f6\u6e05\u7406\u81ea\u5df1\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u8fdb\u884c\u624b\u52a8\u6e05\u7406\u3002 \u6570\u7ec4\u5927\u5c0f\u548c\u6392\u5e8f size() \u51fd\u6570\u53ef\u4ee5\u7528\u6765\u83b7\u53d6 std::array \u7684\u957f\u5ea6\uff1a 1 2 std :: array myArray { 9.0 , 7.2 , 5.4 , 3.6 , 1.8 }; std :: cout << \"length: \" << myArray . size () << '\\n' ; \u7a0b\u5e8f\u6253\u5370\uff1a 1 length: 5 \u56e0\u4e3a std::array \u4e0d\u4f1a\u5728\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\u9000\u5316\u4e3a\u6307\u9488\uff0c\u6240\u4ee5 size() \u5728\u51fd\u6570\u5185\u90e8\u4e5f\u53ef\u4ee5\u8c03\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include void printLength ( const std :: array < double , 5 >& myArray ) { std :: cout << \"length: \" << myArray . size () << '\\n' ; } int main () { std :: array myArray { 9.0 , 7.2 , 5.4 , 3.6 , 1.8 }; printLength ( myArray ); return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 length: 5 \u6ce8\u610f\uff0c\u5728\u6807\u51c6\u5e93\u4e2d size \u4e00\u8bcd\u8868\u793a\u6570\u7ec4\u7684\u957f\u5ea6\u2014\u2014\u4e0d\u8981\u628a\u5b83\u548c sizeof() \u641e\u6df7\u4e86\uff0c\u5f53\u5bf9\u539f\u751f\u7684\u56fa\u5b9a\u6570\u7ec4\u4f7f\u7528 sizeof \u65f6\uff0c\u8fd4\u56de\u7684\u662f\u8be5\u6570\u7ec4\u5360\u7528\u7684\u5b9e\u9645\u5185\u5b58\u7684\u5927\u5c0f\uff08\u5143\u7d20\u5927\u5c0f\u4e58\u4ee5\u6570\u7ec4\u957f\u5ea6\uff09\u3002\u662f\u7684\uff0c\u8fd9\u8fb9\u547d\u540d\u7684\u4e00\u81f4\u6027\u4fdd\u6301\u7684\u4e0d\u597d\u3002 \u8fd8\u8981\u6ce8\u610f\uff0c\u5f53 \u6309\u5f15\u7528\u4f20\u9012 std::array \u65f6\u3002\u53ef\u4ee5\u9632\u6b62\u7f16\u8bd1\u5668\u521b\u5efa std::array \u7684\u62f7\u8d1d\uff08\u6709\u52a9\u4e8e\u63d0\u5347\u6027\u80fd\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u6309\u5f15\u7528\u4f20\u9012\u6216\u6309const\u5f15\u7528\u4f20\u9012\u7684\u65b9\u5f0f\u5c06 std::array \u4f20\u5165\u51fd\u6570\u3002 \u56e0\u4e3a std::array \u7684\u957f\u5ea6\u603b\u662f\u5df2\u77e5\u7684\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u4f7f\u7528 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u6765\u904d\u5386\uff1a 1 2 3 4 std :: array myArray { 9 , 7 , 5 , 3 , 1 }; for ( int element : myArray ) std :: cout << element << ' ' ; \u4f7f\u7528 std::sort \u53ef\u4ee5\u5bf9 std::array \u8fdb\u884c\u6392\u5e8f\uff0c\u8be5\u51fd\u6570\u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // for std::sort #include #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; std :: sort ( myArray . begin (), myArray . end ()); // \u4ece\u524d\u5411\u540e\u6392\u5e8f // std::sort(myArray.rbegin(), myArray.rend()); // \u4ece\u540e\u5411\u524d\u6392\u5e8f for ( int element : myArray ) std :: cout << element << ' ' ; std :: cout << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 1 3 5 7 9 \u5411\u51fd\u6570\u4f20\u9012\u4e0d\u540c\u957f\u5ea6\u7684 std::array \u5bf9\u4e8e std::array \u6765\u8bf4\uff0c\u5143\u7d20\u7684\u7c7b\u578b\u548c\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u6570\u7ec4\u7c7b\u578b\u7684\u4e00\u90e8\u5206\u3002\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u4f7f\u7528 std::array \u4f5c\u4e3a\u51fd\u6570 \u5f62\u53c2 \u65f6\uff0c\u5fc5\u987b\u6307\u660e\u5143\u7d20\u7c7b\u578b\u548c\u6570\u7ec4\u957f\u5ea6 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include void printArray ( const std :: array < int , 5 >& myArray ) { for ( auto element : myArray ) std :: cout << element << ' ' ; std :: cout << '\\n' ; } int main () { std :: array myArray5 { 9.0 , 7.2 , 5.4 , 3.6 , 1.8 }; // type deduced as std::array printArray ( myArray5 ); // error: printArray expects a std::array return 0 ; } \u8fd9\u4e48\u505a\u7684\u5f0a\u7aef\u5728\u4e8e\uff0c\u8be5\u51fd\u6570\u4fbf\u53ea\u80fd\u4f7f\u7528\u7279\u5b9a\u5143\u7d20\u7c7b\u578b\u548c\u6570\u7ec4\u957f\u5ea6\u7684\u6570\u7ec4\u4f5c\u4e3a \u5b9e\u53c2 \uff0c\u4f46\u662f\u5982\u679c\u6211\u4eec\u5e0c\u671b\u51fd\u6570\u80fd\u591f\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u5143\u7d20\u4e14\u957f\u5ea6\u4e0d\u540c\u7684\u6570\u7ec4\u65f6\uff0c\u5e94\u8be5\u600e\u4e48\u505a\u5462\uff1f\u96be\u9053\u8981\u521b\u5efa\u591a\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\u6765\u5904\u7406\u5417\uff1f\u8fd9\u663e\u7136\u4f1a\u5e26\u6765\u5f88\u591a\u5f88\u591a\u7684\u5197\u4f59\u4ee3\u7801\u3002 \u5e78\u8fd0\u5730\u662f\uff0cC++\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u6a21\u677f\u6765\u5b9e\u73b0\u4e0a\u8ff0\u529f\u80fd\u3002 \u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6a21\u677f\u5316\u51fd\u6570\u5e76\u5c06\u6240\u6709\u7684\u7c7b\u578b\u4fe1\u606f\u53c2\u6570\u5316\uff0cC++\u4f1a\u6839\u636e\u6a21\u677f\u5728\u9700\u8981\u65f6\u81ea\u52a8\u521b\u5efa\u201c\u771f\u5b9e\u201d\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include #include // printArray is a template function template < typename T , std :: size_t size > // parameterize the element type and size void printArray ( const std :: array < T , size >& myArray ) { for ( auto element : myArray ) std :: cout << element << ' ' ; std :: cout << '\\n' ; } int main () { std :: array myArray5 { 9.0 , 7.2 , 5.4 , 3.6 , 1.8 }; printArray ( myArray5 ); std :: array myArray7 { 9.0 , 7.2 , 5.4 , 3.6 , 1.8 , 1.2 , 0.7 }; printArray ( myArray7 ); return 0 ; } \u76f8\u5173\u5185\u5bb9 \u51fd\u6570\u6a21\u677f\u4f1a\u5728 8.13 - \u51fd\u6570\u6a21\u677f \u4e2d\u4ecb\u7ecd\u3002 \u57fa\u4e8e size_type \u5bf9 std::array \u8fdb\u884c\u4eba\u5de5\u7d22\u5f15 \u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\uff1a\u4e0b\u9762\u7684\u4ee3\u7801\u6709\u4ec0\u4e48\u95ee\u9898\uff1f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; // Iterate through the array and print the value of the elements for ( int i { 0 }; i < myArray . size (); ++ i ) std :: cout << myArray [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u8fd9\u4e2a\u95ee\u9898\u7684\u7b54\u6848\u662f\uff1a\u4ee3\u7801\u4e2d\u53ef\u80fd\u4f1a\u51fa\u73b0 signed/unsigned \u4e0d\u5339\u914d\u7684\u95ee\u9898\u3002 size() \u51fd\u6570\u548c\u4e0b\u6807\u7d22\u5f15\u4f7f\u7528\u4e86\u4e00\u79cd\u88ab\u79f0\u4e3a size_type \u7684\u7c7b\u578b\uff0c\u8be5\u7c7b\u578b\u5728C++\u6807\u51c6\u4e2d\u88ab\u5b9a\u4e49\u4e3a\u65e0\u7b26\u53f7\u6574\u578b\u7c7b\u578b\u3002\u800c\u5728\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c\u5faa\u73af\u7d22\u5f15 i \u662f\u4e00\u4e2a\u6709\u7b26\u53f7\u6574\u578b\u3002\u56e0\u6b64\u5728\u8fdb\u884c i < myArray.size() \u6bd4\u8f83\u65f6\u4f1a\u51fa\u73b0\u7c7b\u578b\u4e0d\u5339\u914d\u7684\u95ee\u9898\u3002 \u6709\u8da3\u7684\u662f\uff0c size_type \u8fd8\u4e0d\u662f\u4e00\u4e2a\u5168\u5c40\u7c7b\u578b\uff08\u4f8b\u5982\uff0c int \u6216 std::size_t \uff09\uff0c\u5b83\u5b9e\u9645\u4e0a\u88ab\u5b9a\u4e49\u5728 std::array \u5185\u90e8 (C++\u5141\u8bb8\u5d4c\u5957\u5b9a\u4e49)\u3002\u8fd9\u5c31\u610f\u5473\u7740\uff0c\u5f53\u4f60\u9700\u8981\u4f7f\u7528 size_type \u65f6\uff0c\u5fc5\u987b\u6dfb\u52a0\u5b8c\u6574\u7684\u524d\u7f00(\u53ef\u4ee5\u5c06 std::array \u770b\u505a\u662f\u5b83\u7684\u547d\u540d\u7a7a\u95f4)\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c \u201csize_type\u201d \u7684\u5b8c\u6574\u524d\u7f00\u662f std::array::size_type ! \u6240\u4ee5\uff0c\u4e0a\u9762\u4ee3\u7801\u7684\u6b63\u786e\u5199\u6cd5\u5e94\u8be5\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; // std::array::size_type is the return type of size()! for ( std :: array < int , 5 >:: size_type i { 0 }; i < myArray . size (); ++ i ) std :: cout << myArray [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u53ef\u8bfb\u6027\u5e76\u4e0d\u597d\u3002\u5e78\u8fd0\u5730\u662f\uff0c std::array::size_type \u5b9e\u9645\u4e0a\u662f std::size_t \u7684\u522b\u540d\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 std::size_t \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include // std::size_t #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; for ( std :: size_t i { 0 }; i < myArray . size (); ++ i ) std :: cout << myArray [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u66f4\u597d\u7684\u529e\u6cd5\u662f==\u907f\u514d\u5bf9 std::array \u8fdb\u884c\u4eba\u5de5\u7d22\u5f15\uff0c\u800c\u662f\u5e94\u8be5\u4f7f\u7528 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u6216 \u8fed\u4ee3\u5668 \u3002== \u4e00\u5b9a\u8981\u8bb0\u4f4f\uff0c\u5f53\u65e0\u7b26\u53f7\u6574\u578b\u5230\u8fbe\u5b83\u4eec\u7684\u6700\u5927\u503c\u540e\uff0c\u4f1a\u4ea7\u751f\u53cd\u8f6c\u3002\u5e38\u89c1\u7684\u9519\u8bef\u662f\u5bf9\u4e00\u4e2a\u6240\u6709\u8fdb\u884c\u9012\u51cf\uff0c\u5230\u8fbe0\u540e\u4ecd\u7136\u9012\u51cf\u5219\u4f1a\u5bfc\u81f4\u65e0\u7b26\u53f7\u6574\u578b\u53cd\u8f6c\u4e3a\u6700\u5927\u503c\u3002\u5728for\u5faa\u73af\u7684\u8bfe\u7a0b\u4e2d\u4f60\u5df2\u7ecf\u89c1\u8fc7\u4e0b\u9762\u7684\u4f8b\u5b50\u4e86\uff0c\u8fd9\u91cc\u518d\u590d\u4e60\u4e00\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; // \u9006\u5e8f\u6253\u5370\u7d20\u7ec4 // \u56e0\u4e3ai\u4e0d\u9700\u8981\u521d\u59cb\u5316\u4e3a0\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528auto // Bad: for ( auto i { myArray . size () - 1 }; i >= 0 ; -- i ) std :: cout << myArray [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u4e0a\u9762\u7684\u4ee3\u7801\u4f1a\u5f97\u5230\u4e00\u4e2a\u6b7b\u5faa\u73af\uff0c\u5f53 i \u53cd\u8f6c\u7684\u65f6\u5019\uff0c\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u8fd9\u91cc\u5b9e\u9645\u4e0a\u5b58\u5728\u4e24\u4e2a\u95ee\u9898\uff0c\u5982\u679c myArray \u662f\u7a7a\u7684\uff0c\u5219 size() \u4f1a\u8fd4\u56de 0 (\u5bf9\u4e8e std::array \u6765\u8bf4\u662f\u53ef\u4ee5\u7684)\uff0c\u6b64\u65f6 myArray.size() - 1 \u5c31\u4f1a\u53cd\u8f6c\u3002\u53e6\u5916\u4e00\u4e2a\u95ee\u9898\u5728\u4e8e\uff0c\u4e0d\u8bba\u6570\u7ec4\u4e2d\u6709\u591a\u5c11\u5143\u7d20\uff0c i >= 0 \u603b\u662f\u4e3a true \uff0c\u56e0\u4e3a\u65e0\u7b26\u53f7\u6574\u578b\u5c31\u4e0d\u53ef\u80fd\u5c0f\u4e8e0\u3002 \u53ef\u884c\u7684\u53cd\u5411for\u5faa\u73af\u5e94\u8be5\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; // Print the array in reverse order. for ( auto i { myArray . size () }; i -- > 0 ; ) std :: cout << myArray [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6211\u4eec\u5728\u6761\u4ef6\u8bed\u53e5\u4e2d\uff0c\u4f7f\u7528\u540e\u7f00 -- \u5bf9\u7d22\u5f15\u8fdb\u884c\u9012\u51cf\u3002\u8be5\u6761\u4ef6\u5224\u65ad\u4f1a\u5728\u6bcf\u6b21\u8fed\u4ee3\u524d\u8fdb\u884c\uff0c\u5305\u62ec\u7b2c\u4e00\u6b21\u8fed\u4ee3\u3002\u5728\u7b2c\u4e00\u6b21\u8fed\u4ee3\u65f6\uff0c i \u662f myArray.size() - 1 \uff0c\u56e0\u4e3a i \u5728\u6761\u4ef6\u4e2d\u9012\u51cf\u3002\u5f53 i \u7b49\u4e8e0\u65f6\uff0c\u5373\u5c06\u53d1\u751f\u53cd\u8f6c\uff0c\u4f46\u662f\u6b64\u65f6\u6761\u4ef6\u8bed\u53e5\u5df2\u7ecf\u4e0d\u662f true \u4e86\uff0c\u6240\u4ee5\u5faa\u73af\u4f1a\u505c\u6b62\u3002\u5b9e\u9645\u4e0a\uff0c i \u7684\u786e\u4f1a\u5728\u6700\u540e\u4e00\u4e2a -- \u65f6\u53cd\u8f6c\uff0c\u4e0d\u8fc7\u6b64\u65f6\u5faa\u73af\u5df2\u7ecf\u505c\u6b62\uff0c\u8be5\u503c\u5e76\u4e0d\u4f1a\u88ab\u5b9e\u9645\u4f7f\u7528\u3002 \u7ed3\u6784\u4f53\u6570\u7ec4 std::array \u663e\u7136\u4e0d\u53ef\u80fd\u53ea\u80fd\u5b58\u653e\u6570\u503c\u5143\u7d20\u3002\u4efb\u4f55\u80fd\u591f\u5728\u666e\u901a\u6570\u7ec4\u4e2d\u4f7f\u7528\u7684\u6570\u636e\u7c7b\u578b\uff0c\u540c\u6837\u4e5f\u80fd\u591f\u5728 std::array \u4e2d\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u7ed3\u6784\u4f53\u5143\u7d20\u7684 std::array \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include struct House { int number {}; int stories {}; int roomsPerStory {}; }; int main () { std :: array < House , 3 > houses {}; houses [ 0 ] = { 13 , 4 , 30 }; houses [ 1 ] = { 14 , 3 , 10 }; houses [ 2 ] = { 15 , 3 , 40 }; for ( const auto & house : houses ) { std :: cout << \"House number \" << house . number << \" has \" << ( house . stories * house . roomsPerStory ) << \" rooms \\n \" ; } return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 House number 13 has 120 rooms House number 14 has 30 rooms House number 15 has 120 rooms \u4e0d\u8fc7\uff0c\u5f53\u6211\u4eec\u9700\u8981\u521d\u59cb\u5316\u4e00\u4e2a\u6570\u7ec4\uff0c\u4e14\u6570\u7ec4\u5143\u7d20\u7684\u521d\u59cb\u5316\u4e5f\u9700\u8981\u4e00\u4e2a\u5217\u8868\u503c\u65f6\uff08\u6bd4\u5982\u7ed3\u6784\u4f53\u6570\u7ec4\uff09\uff0c\u4ee3\u7801\u4f1a\u770b\u8d77\u6765\u602a\u602a\u7684\u3002\u521d\u59cb\u5316\u6b64\u7c7b std::array \u65f6\uff0c\u4f60\u53ef\u80fd\u4f1a\u5c1d\u8bd5\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 // Doesn't work. std :: array < House , 3 > houses { { 13 , 4 , 30 }, { 14 , 3 , 10 }, { 15 , 3 , 40 } }; \u4f46\u8fd9\u79cd\u65b9\u6cd5\u5b9e\u9645\u4e0a\u662f\u65e0\u6548\u7684\u3002 \u56e0\u4e3a std::array \u7684\u5b9a\u4e49\u4e2d\u5305\u542b\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u6570\u7ec4\u6210\u5458 \uff08\u5176\u5177\u4f53\u540d\u5b57\u53d6\u51b3\u4e8e\u5b9e\u73b0\uff09\u3002\u6240\u4ee5\u5f53\u6211\u4eec\u50cf\u4e0a\u9762\u90a3\u6837\u521d\u59cb\u5316 houses \u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u6309\u7167\u4e0b\u9762\u7684\u65b9\u5f0f\u7406\u89e3\u8be5\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 // Doesn't work. std :: array < House , 3 > houses { // \u521d\u59cb\u5316 houses { 13 , 4 , 30 }, // \u521d\u59cb\u5316 std::array \u7ed3\u6784\u4e2d\u7684C\u98ce\u683c\u6570\u7ec4\u6210\u5458 { 14 , 3 , 10 }, // ? { 15 , 3 , 40 } // ? }; \u7f16\u8bd1\u5668\u4f1a\u5c06 { 13, 4, 30 } \u89e3\u91ca\u4e3a\u6210\u5458\u6570\u7ec4\u7684\u521d\u59cb\u5316\u503c\u3002\u5176\u6548\u679c\u76f8\u5f53\u4e8e\u4f7f\u7528\u8be5\u521d\u59cb\u5316\u503c\u521d\u59cb\u5316\u4e86\u4e0b\u6807\u4e3a\u7684\u7ed3\u6784\u4f53\uff0c\u5269\u4f59\u7684\u7ed3\u6784\u4f53\u5143\u7d20\u5219\u8fdb\u884c\u4e86 0\u521d\u59cb\u5316 \u3002\u968f\u540e\uff0c\u7f16\u8bd1\u5668\u4f1a\u53d1\u73b0\u6211\u4eec\u63d0\u4f9b\u4e86\u4e24\u4e2a\u989d\u5916\u7684\u521d\u59cb\u5316\u503c( { 14, 3, 10 } \u548c { 15, 3, 40 } ) \uff0c\u6240\u4ee5\u5b83\u4f1a\u4ea7\u751f\u4e00\u4e2a\u9519\u8bef\u4fe1\u606f\uff0c\u63d0\u9192\u5f00\u53d1\u8005\u521d\u59cb\u5316\u503c\u8fc7\u591a\u3002 \u6b63\u786e\u7684\u65b9\u6cd5\u662f\u518d\u6dfb\u52a0\u4e00\u5c42\u5927\u62ec\u53f7\uff1a 1 2 3 4 5 6 7 8 // This works as expected std :: array < House , 3 > houses { // initializer for houses { // \u989d\u5916\u7684\u5927\u62ec\u53f7\u8868\u660e\u63a5\u4e0b\u6765\u521d\u59cb\u5316\u6570\u7ec4\u6210\u5458 { 13 , 4 , 30 }, // initializer for array element 0 { 14 , 3 , 10 }, // initializer for array element 1 { 15 , 3 , 40 }, // initializer for array element 2 } }; \u6ce8\u610f\uff0c\u8be5\u5927\u62ec\u53f7\u662f\u5fc5\u8981\u7684\uff08\u8868\u660e\u5f00\u59cb\u521d\u59cb\u5316 std::array \u4e2d\u7684\u6570\u7ec4\u6210\u5458\uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5bf9\u6bcf\u4e2a\u7ed3\u6784\u4f53\u6210\u5458\u8fdb\u884c\u521d\u59cb\u5316\u4e86\u3002 \u8fd9\u5c31\u89e3\u91ca\u4e86\u4e3a\u4ec0\u4e48\u5728\u4f7f\u7528 std::array \u65f6\uff0c\u521d\u59cb\u5316\u503c\u5fc5\u987b\u591a\u4e00\u5c42\u5927\u62ec\u53f7\u3002 \u5c0f\u7ed3 std::array \u53ef\u4ee5\u5f88\u597d\u5730\u4ee3\u66ff\u5185\u7f6e\u7684\u56fa\u5b9a\u6570\u7ec4\u3002\u5b83\u6548\u7387\u5f88\u9ad8\uff0c\u552f\u4e00\u7684\u7f3a\u70b9\u5c31\u662f\u5b83\u7684\u8bed\u6cd5\u7a0d\u5fae\u6709\u70b9\u522b\u626d\uff1a\u4f60\u5fc5\u987b\u6307\u5b9a\u6570\u7ec4\u7684\u957f\u5ea6\uff08\u9664\u975e\u4f60\u7701\u7565\u7c7b\u578b\uff0c\u4e0d\u7136\u7f16\u8bd1\u5668\u4e0d\u4f1a\u57fa\u4e8e\u521d\u59cb\u5316\u503c\u81ea\u52a8\u8ba1\u7b97\u957f\u5ea6\uff0c\u4f46\u662f\u8fd9\u5e76\u4e0d\u603b\u662f\u53ef\u884c\uff09\uff0c\u800c\u4e14\u5728\u7d22\u5f15\u65f6\u8fd8\u5b58\u5728\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u4e0d\u5339\u914d\u7684\u95ee\u9898\u3002\u4e0d\u8fc7\uff0c\u7455\u4e0d\u63a9\u745c\uff0c\u6211\u4eec\u8fd8\u662f\u63a8\u8350\u4f7f\u7528 std::array \u6765\u4ee3\u66ff\u5185\u5efa\u6570\u7ec4\u3002","title":"11.16 \u2014 \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb"},{"location":"11-16-An-introduction-to-std-array/#stdarray","text":"std::array \u5b9e\u73b0\u4e86\u56fa\u5b9a\u6570\u7ec4\u7684\u529f\u80fd\uff0c\u4e14\u5728\u4f20\u5165\u51fd\u6570\u65f6\u4e0d\u4f1a\u9000\u5316\u4e3a\u6307\u9488\u3002 std::array \u88ab\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d\u4e14\u4f4d\u4e8e std \u547d\u540d\u7a7a\u95f4 \u5185\u3002 \u58f0\u660e std::array \u53d8\u91cf\u5f88\u7b80\u5355\uff1a 1 2 3 #include std :: array < int , 3 > myArray ; // \u58f0\u660e\u4e00\u4e2a\u957f\u5ea6\u4e3a3\u7684\u6574\u578b\u6570\u7ec4 \u548c\u539f\u751f\u7684\u56fa\u5b9a\u6570\u7ec4\u5b9e\u73b0\u7c7b\u4f3c\uff0c std::array \u7684\u957f\u5ea6\u5fc5\u987b\u5728 \u7f16\u8bd1\u65f6 \u786e\u5b9a\u3002 std::array \u53ef\u4ee5\u901a\u8fc7 \u521d\u59cb\u5316\u503c\u5217\u8868 \u6216 \u5217\u8868\u521d\u59cb\u5316 \u7684\u65b9\u5f0f\u8fdb\u884c\u521d\u59cb\u5316\uff1a 1 2 std :: array < int , 5 > myArray = { 9 , 7 , 5 , 3 , 1 }; // \u521d\u59cb\u5316\u503c\u5217\u8868 std :: array < int , 5 > myArray2 { 9 , 7 , 5 , 3 , 1 }; // \u5217\u8868\u521d\u59cb\u5316 \u548cC++\u81ea\u5e26\u7684\u56fa\u5b9a\u6570\u7ec4\u4e0d\u540c\u7684\u662f\uff0c\u5728\u4f7f\u7528 std::array \u7684\u65f6\u5019\uff0c\u4f60\u4e0d\u80fd\u7701\u7565\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u5373\u4f7f\u63d0\u4f9b\u4e86\u521d\u59cb\u5316\u503c\u4e5f\u4e0d\u884c\uff1a 1 2 std :: array < int , > myArray { 9 , 7 , 5 , 3 , 1 }; // \u4e0d\u5408\u6cd5\uff0c\u5fc5\u987b\u63d0\u4f9b\u957f\u5ea6 std :: array < int > myArray { 9 , 7 , 5 , 3 , 1 }; // \u4e0d\u5408\u6cd5\uff0c\u5fc5\u987b\u63d0\u4f9b\u957f\u5ea6 \u4e0d\u8fc7\uff0c\u4eceC++17\u5f00\u59cb\uff0c std::array \u4e5f\u652f\u6301\u7701\u7565\u7c7b\u578b\u548c\u5927\u5c0f\u4e86\u3002\u5982\u679c\u6570\u7ec4\u88ab\u663e\u5f0f\u5730\u521d\u59cb\u5316\uff0c\u5219\u7c7b\u578b\u548c\u5927\u5c0f\u53ef\u4ee5\u4e00\u8d77\u88ab\u5ffd\u7565\u3002 1 2 std :: array myArray { 9 , 7 , 5 , 3 , 1 }; // \u901a\u8fc7\u7c7b\u578b\u63a8\u65ad\u5f97\u5230 std::array std :: array myArray { 9.7 , 7.31 }; // \u901a\u8fc7\u7c7b\u578b\u63a8\u65ad\u5f97\u5230 std::array \u6211\u4eec\u63a8\u8350\u4f7f\u7528\u4e0a\u9762\u8fd9\u79cd\u5ffd\u7565\u7c7b\u578b\u548c\u5927\u5c0f\u7684\u58f0\u660e\u65b9\u5f0f\uff0c\u9664\u975e\u4f60\u7684\u7f16\u8bd1\u5668\u4e0d\u652f\u6301C++17\uff0c\u90a3\u4e48\u4f60\u5c31\u5fc5\u987b\u663e\u5f0f\u5730\u6307\u5b9a\u4e86\u7c7b\u578b\u548c\u5927\u5c0f\u3002 1 2 3 4 5 // std::array myArray { 9, 7, 5, 3, 1 }; // Since C++17 std :: array < int , 5 > myArray { 9 , 7 , 5 , 3 , 1 }; // Before C++17 // std::array myArray { 9.7, 7.31 }; // Since C++17 std :: array < double , 2 > myArray { 9.7 , 7.31 }; // Before C++17 \u5728 C++20 \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u5143\u7d20\u7c7b\u578b\uff0c\u4f46\u662f\u5ffd\u7565\u6570\u7ec4\u957f\u5ea6\u3002\u8fd9\u4e48\u505a\u7684\u65f6\u5019\uff0c\u521b\u5efa std::array \u65f6\u770b\u8d77\u6765\u66f4\u50cf\u662f\u521b\u5efa\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u6570\u7ec4\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 std::to_array \u6765\u521b\u5efa\u6570\u7ec4\uff08\u6307\u5b9a\u5143\u7d20\u7c7b\u578b\u4f46\u81ea\u52a8\u63a8\u65ad\u957f\u5ea6\uff09\uff1a 1 2 3 auto myArray1 { std :: to_array < int , 5 > ({ 9 , 7 , 5 , 3 , 1 }) }; // Specify type and size auto myArray2 { std :: to_array < int > ({ 9 , 7 , 5 , 3 , 1 }) }; // Specify type only, deduce size auto myArray3 { std :: to_array ({ 9 , 7 , 5 , 3 , 1 }) }; // Deduce type and size \u4e0d\u5e78\u7684\u662f\uff0c\u4f7f\u7528 std::to_array \u521b\u5efa\u6570\u7ec4\u65f6\u7684\u5f00\u9500\u6bd4\u76f4\u63a5\u4f7f\u7528 std::array \u521b\u5efa\u6570\u7ec4\u5f00\u9500\u66f4\u5927\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u5c06\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u6570\u7ec4\u7684\u5143\u7d20\u62f7\u8d1d\u5230 std::array \u3002\u51fa\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u5982\u679c\u9700\u8981\u5728\u5faa\u73af\u4e2d\u591a\u6b21\u521b\u5efa\u6570\u7ec4\u65f6\uff0c\u5e94\u8be5\u907f\u514d\u4f7f\u7528 std::to_array \u3002 \u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 \u521d\u59cb\u5316\u503c\u5217\u8868 \u4e3a\u6570\u7ec4\u8d4b\u503c\u3002 1 2 3 4 std :: array < int , 5 > myArray ; myArray = { 0 , 1 , 2 , 3 , 4 }; // okay myArray = { 9 , 8 , 7 }; // okay, elements 3 and 4 are set to zero! myArray = { 0 , 1 , 2 , 3 , 4 , 5 }; // not allowed, too many elements in initializer list! \u6076\u610f\u4f7f\u7528\u4e0b\u6807\u8fd0\u7b97\u7b26\u8bbf\u95ee std::array \u6570\u7ec4\u4e2d\u7684\u503c\uff1a 1 2 std :: cout << myArray [ 1 ] << '\\n' ; myArray [ 2 ] = 6 ; \u548c\u5185\u7f6e\u7684\u6570\u7ec4\u7c7b\u4f3c\uff0c\u4e0b\u6807\u8fd0\u7b97\u7b26\u5e76\u6ca1\u6709\u5b9e\u73b0\u8d8a\u754c\u68c0\u67e5\u3002\u5f53\u4f20\u5165\u975e\u6cd5\u7d22\u5f15\u65f6\uff0c\u4f1a\u53d1\u751f\u5f88\u4e0d\u597d\u7684\u4e8b\u60c5\u3002 std::array \u8fd8\u652f\u6301\u53e6\u5916\u4e00\u79cd\u5143\u7d20\u8bbf\u95ee\u65b9\u5f0f\uff08\u4f7f\u7528 at() \u51fd\u6570\uff09\uff0c\u5b83\u4f1a\u5728\uff08 \u8fd0\u884c\u65f6 \uff09\u8fdb\u884c\u8d8a\u754c\u68c0\u67e5\uff1a 1 2 3 std :: array myArray { 9 , 7 , 5 , 3 , 1 }; myArray . at ( 1 ) = 6 ; // array element 1 is valid, sets array element 1 to value 6 myArray . at ( 9 ) = 10 ; // array element 9 is invalid, will throw a runtime error \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u8c03\u7528 myArray.at(1) \u65f6\u4f1a\u786e\u4fdd\u6240\u4ee51\u662f\u5408\u6cd5\u7684\u3002\u7531\u4e8e1\u786e\u5b9e\u662f\u5408\u6cd5\u7d22\u5f15\uff0c\u6240\u4ee5\u5b83\u4f1a\u8fd4\u56de\u5bf9\u5e94\u7684\u5143\u7d20\u7684\u5f15\u7528\u3002\u6211\u4eec\u5c066\u8d4b\u503c\u7ed9\u8be5\u5f15\u7528\u3002\u4e0d\u8fc7\uff0c\u5f53\u6211\u4eec\u8c03\u7528 myArray.at(9) \u65f6\u5c31\u4f1a\u5931\u8d25\uff0c\u56e0\u4e3a\u7d22\u5f159\u8d8a\u754c\u4e86\u3002\u5f53\u8d8a\u754c\u53d1\u751f\u65f6\uff0c at() \u51fd\u6570\u4f1a\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\uff08\u5177\u4f53\u6765\u8bf4\u5b83\u4f1a\u629b\u51fa\u4e00\u4e2a std::out_of_range \u5f02\u5e38\uff09\uff0c\u800c\u4e0d\u4f1a\u8fd4\u56de\u5bf9\u5e94\u7684\u7d22\u5f15\u3002\u56e0\u4e3a\u9700\u8981\u8fdb\u884c\u8d8a\u754c\u68c0\u67e5\uff0c\u6240\u4ee5 at() \u7684\u901f\u5ea6\u4f1a\u6bd4\u4e0b\u6807\u8fd0\u7b97\u7b26\u6162\uff0c\u4f46\u5b83\u66f4\u5b89\u5168\u3002 std::array \u4f1a\u5728 \u79bb\u5f00\u4f5c\u7528\u57df \u65f6\u6e05\u7406\u81ea\u5df1\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u8fdb\u884c\u624b\u52a8\u6e05\u7406\u3002","title":"std::array"},{"location":"11-16-An-introduction-to-std-array/#_1","text":"size() \u51fd\u6570\u53ef\u4ee5\u7528\u6765\u83b7\u53d6 std::array \u7684\u957f\u5ea6\uff1a 1 2 std :: array myArray { 9.0 , 7.2 , 5.4 , 3.6 , 1.8 }; std :: cout << \"length: \" << myArray . size () << '\\n' ; \u7a0b\u5e8f\u6253\u5370\uff1a 1 length: 5 \u56e0\u4e3a std::array \u4e0d\u4f1a\u5728\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\u9000\u5316\u4e3a\u6307\u9488\uff0c\u6240\u4ee5 size() \u5728\u51fd\u6570\u5185\u90e8\u4e5f\u53ef\u4ee5\u8c03\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include void printLength ( const std :: array < double , 5 >& myArray ) { std :: cout << \"length: \" << myArray . size () << '\\n' ; } int main () { std :: array myArray { 9.0 , 7.2 , 5.4 , 3.6 , 1.8 }; printLength ( myArray ); return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 length: 5 \u6ce8\u610f\uff0c\u5728\u6807\u51c6\u5e93\u4e2d size \u4e00\u8bcd\u8868\u793a\u6570\u7ec4\u7684\u957f\u5ea6\u2014\u2014\u4e0d\u8981\u628a\u5b83\u548c sizeof() \u641e\u6df7\u4e86\uff0c\u5f53\u5bf9\u539f\u751f\u7684\u56fa\u5b9a\u6570\u7ec4\u4f7f\u7528 sizeof \u65f6\uff0c\u8fd4\u56de\u7684\u662f\u8be5\u6570\u7ec4\u5360\u7528\u7684\u5b9e\u9645\u5185\u5b58\u7684\u5927\u5c0f\uff08\u5143\u7d20\u5927\u5c0f\u4e58\u4ee5\u6570\u7ec4\u957f\u5ea6\uff09\u3002\u662f\u7684\uff0c\u8fd9\u8fb9\u547d\u540d\u7684\u4e00\u81f4\u6027\u4fdd\u6301\u7684\u4e0d\u597d\u3002 \u8fd8\u8981\u6ce8\u610f\uff0c\u5f53 \u6309\u5f15\u7528\u4f20\u9012 std::array \u65f6\u3002\u53ef\u4ee5\u9632\u6b62\u7f16\u8bd1\u5668\u521b\u5efa std::array \u7684\u62f7\u8d1d\uff08\u6709\u52a9\u4e8e\u63d0\u5347\u6027\u80fd\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u6309\u5f15\u7528\u4f20\u9012\u6216\u6309const\u5f15\u7528\u4f20\u9012\u7684\u65b9\u5f0f\u5c06 std::array \u4f20\u5165\u51fd\u6570\u3002 \u56e0\u4e3a std::array \u7684\u957f\u5ea6\u603b\u662f\u5df2\u77e5\u7684\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u4f7f\u7528 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u6765\u904d\u5386\uff1a 1 2 3 4 std :: array myArray { 9 , 7 , 5 , 3 , 1 }; for ( int element : myArray ) std :: cout << element << ' ' ; \u4f7f\u7528 std::sort \u53ef\u4ee5\u5bf9 std::array \u8fdb\u884c\u6392\u5e8f\uff0c\u8be5\u51fd\u6570\u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // for std::sort #include #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; std :: sort ( myArray . begin (), myArray . end ()); // \u4ece\u524d\u5411\u540e\u6392\u5e8f // std::sort(myArray.rbegin(), myArray.rend()); // \u4ece\u540e\u5411\u524d\u6392\u5e8f for ( int element : myArray ) std :: cout << element << ' ' ; std :: cout << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 1 3 5 7 9","title":"\u6570\u7ec4\u5927\u5c0f\u548c\u6392\u5e8f"},{"location":"11-16-An-introduction-to-std-array/#stdarray_1","text":"\u5bf9\u4e8e std::array \u6765\u8bf4\uff0c\u5143\u7d20\u7684\u7c7b\u578b\u548c\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u6570\u7ec4\u7c7b\u578b\u7684\u4e00\u90e8\u5206\u3002\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u4f7f\u7528 std::array \u4f5c\u4e3a\u51fd\u6570 \u5f62\u53c2 \u65f6\uff0c\u5fc5\u987b\u6307\u660e\u5143\u7d20\u7c7b\u578b\u548c\u6570\u7ec4\u957f\u5ea6 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include void printArray ( const std :: array < int , 5 >& myArray ) { for ( auto element : myArray ) std :: cout << element << ' ' ; std :: cout << '\\n' ; } int main () { std :: array myArray5 { 9.0 , 7.2 , 5.4 , 3.6 , 1.8 }; // type deduced as std::array printArray ( myArray5 ); // error: printArray expects a std::array return 0 ; } \u8fd9\u4e48\u505a\u7684\u5f0a\u7aef\u5728\u4e8e\uff0c\u8be5\u51fd\u6570\u4fbf\u53ea\u80fd\u4f7f\u7528\u7279\u5b9a\u5143\u7d20\u7c7b\u578b\u548c\u6570\u7ec4\u957f\u5ea6\u7684\u6570\u7ec4\u4f5c\u4e3a \u5b9e\u53c2 \uff0c\u4f46\u662f\u5982\u679c\u6211\u4eec\u5e0c\u671b\u51fd\u6570\u80fd\u591f\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u5143\u7d20\u4e14\u957f\u5ea6\u4e0d\u540c\u7684\u6570\u7ec4\u65f6\uff0c\u5e94\u8be5\u600e\u4e48\u505a\u5462\uff1f\u96be\u9053\u8981\u521b\u5efa\u591a\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\u6765\u5904\u7406\u5417\uff1f\u8fd9\u663e\u7136\u4f1a\u5e26\u6765\u5f88\u591a\u5f88\u591a\u7684\u5197\u4f59\u4ee3\u7801\u3002 \u5e78\u8fd0\u5730\u662f\uff0cC++\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u6a21\u677f\u6765\u5b9e\u73b0\u4e0a\u8ff0\u529f\u80fd\u3002 \u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6a21\u677f\u5316\u51fd\u6570\u5e76\u5c06\u6240\u6709\u7684\u7c7b\u578b\u4fe1\u606f\u53c2\u6570\u5316\uff0cC++\u4f1a\u6839\u636e\u6a21\u677f\u5728\u9700\u8981\u65f6\u81ea\u52a8\u521b\u5efa\u201c\u771f\u5b9e\u201d\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include #include // printArray is a template function template < typename T , std :: size_t size > // parameterize the element type and size void printArray ( const std :: array < T , size >& myArray ) { for ( auto element : myArray ) std :: cout << element << ' ' ; std :: cout << '\\n' ; } int main () { std :: array myArray5 { 9.0 , 7.2 , 5.4 , 3.6 , 1.8 }; printArray ( myArray5 ); std :: array myArray7 { 9.0 , 7.2 , 5.4 , 3.6 , 1.8 , 1.2 , 0.7 }; printArray ( myArray7 ); return 0 ; } \u76f8\u5173\u5185\u5bb9 \u51fd\u6570\u6a21\u677f\u4f1a\u5728 8.13 - \u51fd\u6570\u6a21\u677f \u4e2d\u4ecb\u7ecd\u3002","title":"\u5411\u51fd\u6570\u4f20\u9012\u4e0d\u540c\u957f\u5ea6\u7684 std::array"},{"location":"11-16-An-introduction-to-std-array/#size_type-stdarray","text":"\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\uff1a\u4e0b\u9762\u7684\u4ee3\u7801\u6709\u4ec0\u4e48\u95ee\u9898\uff1f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; // Iterate through the array and print the value of the elements for ( int i { 0 }; i < myArray . size (); ++ i ) std :: cout << myArray [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u8fd9\u4e2a\u95ee\u9898\u7684\u7b54\u6848\u662f\uff1a\u4ee3\u7801\u4e2d\u53ef\u80fd\u4f1a\u51fa\u73b0 signed/unsigned \u4e0d\u5339\u914d\u7684\u95ee\u9898\u3002 size() \u51fd\u6570\u548c\u4e0b\u6807\u7d22\u5f15\u4f7f\u7528\u4e86\u4e00\u79cd\u88ab\u79f0\u4e3a size_type \u7684\u7c7b\u578b\uff0c\u8be5\u7c7b\u578b\u5728C++\u6807\u51c6\u4e2d\u88ab\u5b9a\u4e49\u4e3a\u65e0\u7b26\u53f7\u6574\u578b\u7c7b\u578b\u3002\u800c\u5728\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c\u5faa\u73af\u7d22\u5f15 i \u662f\u4e00\u4e2a\u6709\u7b26\u53f7\u6574\u578b\u3002\u56e0\u6b64\u5728\u8fdb\u884c i < myArray.size() \u6bd4\u8f83\u65f6\u4f1a\u51fa\u73b0\u7c7b\u578b\u4e0d\u5339\u914d\u7684\u95ee\u9898\u3002 \u6709\u8da3\u7684\u662f\uff0c size_type \u8fd8\u4e0d\u662f\u4e00\u4e2a\u5168\u5c40\u7c7b\u578b\uff08\u4f8b\u5982\uff0c int \u6216 std::size_t \uff09\uff0c\u5b83\u5b9e\u9645\u4e0a\u88ab\u5b9a\u4e49\u5728 std::array \u5185\u90e8 (C++\u5141\u8bb8\u5d4c\u5957\u5b9a\u4e49)\u3002\u8fd9\u5c31\u610f\u5473\u7740\uff0c\u5f53\u4f60\u9700\u8981\u4f7f\u7528 size_type \u65f6\uff0c\u5fc5\u987b\u6dfb\u52a0\u5b8c\u6574\u7684\u524d\u7f00(\u53ef\u4ee5\u5c06 std::array \u770b\u505a\u662f\u5b83\u7684\u547d\u540d\u7a7a\u95f4)\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c \u201csize_type\u201d \u7684\u5b8c\u6574\u524d\u7f00\u662f std::array::size_type ! \u6240\u4ee5\uff0c\u4e0a\u9762\u4ee3\u7801\u7684\u6b63\u786e\u5199\u6cd5\u5e94\u8be5\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; // std::array::size_type is the return type of size()! for ( std :: array < int , 5 >:: size_type i { 0 }; i < myArray . size (); ++ i ) std :: cout << myArray [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u53ef\u8bfb\u6027\u5e76\u4e0d\u597d\u3002\u5e78\u8fd0\u5730\u662f\uff0c std::array::size_type \u5b9e\u9645\u4e0a\u662f std::size_t \u7684\u522b\u540d\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 std::size_t \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include // std::size_t #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; for ( std :: size_t i { 0 }; i < myArray . size (); ++ i ) std :: cout << myArray [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u66f4\u597d\u7684\u529e\u6cd5\u662f==\u907f\u514d\u5bf9 std::array \u8fdb\u884c\u4eba\u5de5\u7d22\u5f15\uff0c\u800c\u662f\u5e94\u8be5\u4f7f\u7528 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u6216 \u8fed\u4ee3\u5668 \u3002== \u4e00\u5b9a\u8981\u8bb0\u4f4f\uff0c\u5f53\u65e0\u7b26\u53f7\u6574\u578b\u5230\u8fbe\u5b83\u4eec\u7684\u6700\u5927\u503c\u540e\uff0c\u4f1a\u4ea7\u751f\u53cd\u8f6c\u3002\u5e38\u89c1\u7684\u9519\u8bef\u662f\u5bf9\u4e00\u4e2a\u6240\u6709\u8fdb\u884c\u9012\u51cf\uff0c\u5230\u8fbe0\u540e\u4ecd\u7136\u9012\u51cf\u5219\u4f1a\u5bfc\u81f4\u65e0\u7b26\u53f7\u6574\u578b\u53cd\u8f6c\u4e3a\u6700\u5927\u503c\u3002\u5728for\u5faa\u73af\u7684\u8bfe\u7a0b\u4e2d\u4f60\u5df2\u7ecf\u89c1\u8fc7\u4e0b\u9762\u7684\u4f8b\u5b50\u4e86\uff0c\u8fd9\u91cc\u518d\u590d\u4e60\u4e00\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; // \u9006\u5e8f\u6253\u5370\u7d20\u7ec4 // \u56e0\u4e3ai\u4e0d\u9700\u8981\u521d\u59cb\u5316\u4e3a0\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528auto // Bad: for ( auto i { myArray . size () - 1 }; i >= 0 ; -- i ) std :: cout << myArray [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u4e0a\u9762\u7684\u4ee3\u7801\u4f1a\u5f97\u5230\u4e00\u4e2a\u6b7b\u5faa\u73af\uff0c\u5f53 i \u53cd\u8f6c\u7684\u65f6\u5019\uff0c\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u8fd9\u91cc\u5b9e\u9645\u4e0a\u5b58\u5728\u4e24\u4e2a\u95ee\u9898\uff0c\u5982\u679c myArray \u662f\u7a7a\u7684\uff0c\u5219 size() \u4f1a\u8fd4\u56de 0 (\u5bf9\u4e8e std::array \u6765\u8bf4\u662f\u53ef\u4ee5\u7684)\uff0c\u6b64\u65f6 myArray.size() - 1 \u5c31\u4f1a\u53cd\u8f6c\u3002\u53e6\u5916\u4e00\u4e2a\u95ee\u9898\u5728\u4e8e\uff0c\u4e0d\u8bba\u6570\u7ec4\u4e2d\u6709\u591a\u5c11\u5143\u7d20\uff0c i >= 0 \u603b\u662f\u4e3a true \uff0c\u56e0\u4e3a\u65e0\u7b26\u53f7\u6574\u578b\u5c31\u4e0d\u53ef\u80fd\u5c0f\u4e8e0\u3002 \u53ef\u884c\u7684\u53cd\u5411for\u5faa\u73af\u5e94\u8be5\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: array myArray { 7 , 3 , 1 , 9 , 5 }; // Print the array in reverse order. for ( auto i { myArray . size () }; i -- > 0 ; ) std :: cout << myArray [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6211\u4eec\u5728\u6761\u4ef6\u8bed\u53e5\u4e2d\uff0c\u4f7f\u7528\u540e\u7f00 -- \u5bf9\u7d22\u5f15\u8fdb\u884c\u9012\u51cf\u3002\u8be5\u6761\u4ef6\u5224\u65ad\u4f1a\u5728\u6bcf\u6b21\u8fed\u4ee3\u524d\u8fdb\u884c\uff0c\u5305\u62ec\u7b2c\u4e00\u6b21\u8fed\u4ee3\u3002\u5728\u7b2c\u4e00\u6b21\u8fed\u4ee3\u65f6\uff0c i \u662f myArray.size() - 1 \uff0c\u56e0\u4e3a i \u5728\u6761\u4ef6\u4e2d\u9012\u51cf\u3002\u5f53 i \u7b49\u4e8e0\u65f6\uff0c\u5373\u5c06\u53d1\u751f\u53cd\u8f6c\uff0c\u4f46\u662f\u6b64\u65f6\u6761\u4ef6\u8bed\u53e5\u5df2\u7ecf\u4e0d\u662f true \u4e86\uff0c\u6240\u4ee5\u5faa\u73af\u4f1a\u505c\u6b62\u3002\u5b9e\u9645\u4e0a\uff0c i \u7684\u786e\u4f1a\u5728\u6700\u540e\u4e00\u4e2a -- \u65f6\u53cd\u8f6c\uff0c\u4e0d\u8fc7\u6b64\u65f6\u5faa\u73af\u5df2\u7ecf\u505c\u6b62\uff0c\u8be5\u503c\u5e76\u4e0d\u4f1a\u88ab\u5b9e\u9645\u4f7f\u7528\u3002","title":"\u57fa\u4e8esize_type \u5bf9 std::array \u8fdb\u884c\u4eba\u5de5\u7d22\u5f15"},{"location":"11-16-An-introduction-to-std-array/#_2","text":"std::array \u663e\u7136\u4e0d\u53ef\u80fd\u53ea\u80fd\u5b58\u653e\u6570\u503c\u5143\u7d20\u3002\u4efb\u4f55\u80fd\u591f\u5728\u666e\u901a\u6570\u7ec4\u4e2d\u4f7f\u7528\u7684\u6570\u636e\u7c7b\u578b\uff0c\u540c\u6837\u4e5f\u80fd\u591f\u5728 std::array \u4e2d\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u7ed3\u6784\u4f53\u5143\u7d20\u7684 std::array \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include struct House { int number {}; int stories {}; int roomsPerStory {}; }; int main () { std :: array < House , 3 > houses {}; houses [ 0 ] = { 13 , 4 , 30 }; houses [ 1 ] = { 14 , 3 , 10 }; houses [ 2 ] = { 15 , 3 , 40 }; for ( const auto & house : houses ) { std :: cout << \"House number \" << house . number << \" has \" << ( house . stories * house . roomsPerStory ) << \" rooms \\n \" ; } return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 House number 13 has 120 rooms House number 14 has 30 rooms House number 15 has 120 rooms \u4e0d\u8fc7\uff0c\u5f53\u6211\u4eec\u9700\u8981\u521d\u59cb\u5316\u4e00\u4e2a\u6570\u7ec4\uff0c\u4e14\u6570\u7ec4\u5143\u7d20\u7684\u521d\u59cb\u5316\u4e5f\u9700\u8981\u4e00\u4e2a\u5217\u8868\u503c\u65f6\uff08\u6bd4\u5982\u7ed3\u6784\u4f53\u6570\u7ec4\uff09\uff0c\u4ee3\u7801\u4f1a\u770b\u8d77\u6765\u602a\u602a\u7684\u3002\u521d\u59cb\u5316\u6b64\u7c7b std::array \u65f6\uff0c\u4f60\u53ef\u80fd\u4f1a\u5c1d\u8bd5\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 // Doesn't work. std :: array < House , 3 > houses { { 13 , 4 , 30 }, { 14 , 3 , 10 }, { 15 , 3 , 40 } }; \u4f46\u8fd9\u79cd\u65b9\u6cd5\u5b9e\u9645\u4e0a\u662f\u65e0\u6548\u7684\u3002 \u56e0\u4e3a std::array \u7684\u5b9a\u4e49\u4e2d\u5305\u542b\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u6570\u7ec4\u6210\u5458 \uff08\u5176\u5177\u4f53\u540d\u5b57\u53d6\u51b3\u4e8e\u5b9e\u73b0\uff09\u3002\u6240\u4ee5\u5f53\u6211\u4eec\u50cf\u4e0a\u9762\u90a3\u6837\u521d\u59cb\u5316 houses \u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u6309\u7167\u4e0b\u9762\u7684\u65b9\u5f0f\u7406\u89e3\u8be5\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 // Doesn't work. std :: array < House , 3 > houses { // \u521d\u59cb\u5316 houses { 13 , 4 , 30 }, // \u521d\u59cb\u5316 std::array \u7ed3\u6784\u4e2d\u7684C\u98ce\u683c\u6570\u7ec4\u6210\u5458 { 14 , 3 , 10 }, // ? { 15 , 3 , 40 } // ? }; \u7f16\u8bd1\u5668\u4f1a\u5c06 { 13, 4, 30 } \u89e3\u91ca\u4e3a\u6210\u5458\u6570\u7ec4\u7684\u521d\u59cb\u5316\u503c\u3002\u5176\u6548\u679c\u76f8\u5f53\u4e8e\u4f7f\u7528\u8be5\u521d\u59cb\u5316\u503c\u521d\u59cb\u5316\u4e86\u4e0b\u6807\u4e3a\u7684\u7ed3\u6784\u4f53\uff0c\u5269\u4f59\u7684\u7ed3\u6784\u4f53\u5143\u7d20\u5219\u8fdb\u884c\u4e86 0\u521d\u59cb\u5316 \u3002\u968f\u540e\uff0c\u7f16\u8bd1\u5668\u4f1a\u53d1\u73b0\u6211\u4eec\u63d0\u4f9b\u4e86\u4e24\u4e2a\u989d\u5916\u7684\u521d\u59cb\u5316\u503c( { 14, 3, 10 } \u548c { 15, 3, 40 } ) \uff0c\u6240\u4ee5\u5b83\u4f1a\u4ea7\u751f\u4e00\u4e2a\u9519\u8bef\u4fe1\u606f\uff0c\u63d0\u9192\u5f00\u53d1\u8005\u521d\u59cb\u5316\u503c\u8fc7\u591a\u3002 \u6b63\u786e\u7684\u65b9\u6cd5\u662f\u518d\u6dfb\u52a0\u4e00\u5c42\u5927\u62ec\u53f7\uff1a 1 2 3 4 5 6 7 8 // This works as expected std :: array < House , 3 > houses { // initializer for houses { // \u989d\u5916\u7684\u5927\u62ec\u53f7\u8868\u660e\u63a5\u4e0b\u6765\u521d\u59cb\u5316\u6570\u7ec4\u6210\u5458 { 13 , 4 , 30 }, // initializer for array element 0 { 14 , 3 , 10 }, // initializer for array element 1 { 15 , 3 , 40 }, // initializer for array element 2 } }; \u6ce8\u610f\uff0c\u8be5\u5927\u62ec\u53f7\u662f\u5fc5\u8981\u7684\uff08\u8868\u660e\u5f00\u59cb\u521d\u59cb\u5316 std::array \u4e2d\u7684\u6570\u7ec4\u6210\u5458\uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5bf9\u6bcf\u4e2a\u7ed3\u6784\u4f53\u6210\u5458\u8fdb\u884c\u521d\u59cb\u5316\u4e86\u3002 \u8fd9\u5c31\u89e3\u91ca\u4e86\u4e3a\u4ec0\u4e48\u5728\u4f7f\u7528 std::array \u65f6\uff0c\u521d\u59cb\u5316\u503c\u5fc5\u987b\u591a\u4e00\u5c42\u5927\u62ec\u53f7\u3002","title":"\u7ed3\u6784\u4f53\u6570\u7ec4"},{"location":"11-16-An-introduction-to-std-array/#_3","text":"std::array \u53ef\u4ee5\u5f88\u597d\u5730\u4ee3\u66ff\u5185\u7f6e\u7684\u56fa\u5b9a\u6570\u7ec4\u3002\u5b83\u6548\u7387\u5f88\u9ad8\uff0c\u552f\u4e00\u7684\u7f3a\u70b9\u5c31\u662f\u5b83\u7684\u8bed\u6cd5\u7a0d\u5fae\u6709\u70b9\u522b\u626d\uff1a\u4f60\u5fc5\u987b\u6307\u5b9a\u6570\u7ec4\u7684\u957f\u5ea6\uff08\u9664\u975e\u4f60\u7701\u7565\u7c7b\u578b\uff0c\u4e0d\u7136\u7f16\u8bd1\u5668\u4e0d\u4f1a\u57fa\u4e8e\u521d\u59cb\u5316\u503c\u81ea\u52a8\u8ba1\u7b97\u957f\u5ea6\uff0c\u4f46\u662f\u8fd9\u5e76\u4e0d\u603b\u662f\u53ef\u884c\uff09\uff0c\u800c\u4e14\u5728\u7d22\u5f15\u65f6\u8fd8\u5b58\u5728\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u4e0d\u5339\u914d\u7684\u95ee\u9898\u3002\u4e0d\u8fc7\uff0c\u7455\u4e0d\u63a9\u745c\uff0c\u6211\u4eec\u8fd8\u662f\u63a8\u8350\u4f7f\u7528 std::array \u6765\u4ee3\u66ff\u5185\u5efa\u6570\u7ec4\u3002","title":"\u5c0f\u7ed3"},{"location":"11-17-An-introduction-to-std-vector/","tags":["vector","C++17"],"text":"Key Takeaway \u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86 std::array \uff0c\u5b83\u63d0\u4f9b\u4e86C++\u5185\u5efa\u56fa\u5b9a\u6570\u7ec4\u76f8\u540c\u7684\u529f\u80fd\uff0c\u4f46\u662f\u66f4\u5b89\u5168\u4e5f\u66f4\u597d\u7528\u3002 \u7c7b\u4f3c\u5730\uff0cC++ \u6807\u51c6\u5e93\u4e5f\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u529f\u80fd\uff0c\u4f7f\u5f97\u4f7f\u7528\u52a8\u6001\u6570\u7ec4\u53ef\u4ee5\u66f4\u52a0\u5b89\u5168\u548c\u6613\u7528\u3002\u8fd9\u4e2a\u529f\u80fd\u5c31\u662f std::vector \u3002 \u4e0d\u540c\u4e8e std::array \u5c3d\u53ef\u80fd\u4eff\u7167\u56fa\u5b9a\u6570\u7ec4\u7684\u529f\u80fd\uff0c std::vector \u5219\u63d0\u4f9b\u4e86\u5f88\u591a\u6570\u7ec4\u7684\u989d\u5916\u529f\u80fd\uff0c\u56e0\u6b64 std::vector \u4e5f\u6210\u4e3a\u4e86C++\u4e2d\u6700\u6709\u7528\u7684\u5de5\u5177\u4e4b\u4e00\u3002 std::vector \u7b80\u4ecb std::vector \u5728C++03\u4e2d\u88ab\u5f15\u5165\uff0c\u5b83\u63d0\u4f9b\u4e86 dynamic-array \u7684\u529f\u80fd\u5e76\u4e14\u53ef\u4ee5\u81ea\u6211\u7ba1\u7406\u5185\u5b58\u7684\u7533\u8bf7\u548c\u91ca\u653e\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u4f60\u53ef\u4ee5\u5728 \u8fd0\u884c\u65f6 \u521b\u5efa\u6570\u7ec4\u5e76\u6307\u5b9a\u5176\u957f\u5ea6\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528 new \u548c delete \u6765\u624b\u52a8\u7ba1\u7406\u5185\u5b58\u7684\u7533\u8bf7\u91ca\u653e\u3002 std::vector \u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d\u3002 \u58f0\u660e\u4e00\u4e2a std::vector \u5f88\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 #include // \u5728\u58f0\u660e\u65f6\u65e0\u9700\u6307\u5b9a\u957f\u5ea6 std :: vector < int > array ; std :: vector < int > array2 = { 9 , 7 , 5 , 3 , 1 }; // \u4f7f\u7528\u521d\u59cb\u5316\u503c\u5217\u8868\u8fdb\u884c\u521d\u59cb\u5316(\u5728C++11\u4e4b\u524d) std :: vector < int > array3 { 9 , 7 , 5 , 3 , 1 }; // \u4f7f\u7528\u7edf\u4e00\u521d\u59cb\u5316 //\u548cstd::array\u4e00\u6837\uff0c\u4eceC++17\u5f00\u59cb\uff0c\u7c7b\u578b\u53ef\u4ee5\u7701\u7565 std :: vector array4 { 9 , 7 , 5 , 3 , 1 }; // deduced to std::vector \u6ce8\u610f\uff0c\u4e0d\u8bba\u662f\u521d\u59cb\u5316\u4e0e\u5426\uff0c\u4f60\u90fd\u4e0d\u9700\u8981\u5728\u7f16\u8bd1\u65f6\u6307\u5b9a\u6570\u7ec4\u957f\u5ea6\u3002\u8fd9\u662f\u56e0\u4e3a std::vector \u53ef\u4ee5\u5728\u9700\u8981\u65f6\u81ea\u884c\u5206\u914d\u5185\u5b58\u3002 \u548c std::array \u4e00\u6837\uff0c\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u53ef\u4ee5\u4f7f\u7528 \u4e0b\u6807\u8fd0\u7b97\u7b26 \uff08\u4e0d\u8fdb\u884c\u8d8a\u754c\u68c0\u67e5\uff09\u6216 at() \u51fd\u6570\uff08\u8fdb\u884c\u8d8a\u754c\u68c0\u67e5\uff09\u8bbf\u95ee\uff1a 1 2 array [ 6 ] = 2 ; // no bounds checking array . at ( 7 ) = 3 ; // does bounds checking \u5bf9\u4e8e\u4e0a\u9762\u4e24\u79cd\u60c5\u51b5\uff0c\u5982\u679c\u4f60\u8bbf\u95ee\u8d85\u51fa\u6570\u7ec4\u7ed3\u5c3e\u7684\u5143\u7d20\uff0cvector\u5e76\u4e0d\u4f1a\u81ea\u52a8\u8c03\u6574\u957f\u5ea6\u3002 \u5728 C++11 \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 \u521d\u59cb\u5316\u503c\u5217\u8868 \u4e3a std::vector \u8d4b\u503c\uff1a 1 2 array = { 0 , 1 , 2 , 3 , 4 }; // okay, array length is now 5 array = { 9 , 8 , 7 }; // okay, array length is now 3 \u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0cvector\u53ef\u4ee5\u6839\u636e\u5143\u7d20\u7684\u4e2a\u6570\u8c03\u6574\u5176\u957f\u5ea6\u3002 \u81ea\u6211\u6e05\u7406\uff0c\u907f\u514d\u5185\u5b58\u6cc4\u6f0f \u5f53\u4e00\u4e2avector\u53d8\u91cf\u8d85\u51fa\u4f5c\u7528\u57df\u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u91ca\u653e\u5b83\u6240\u4f7f\u7528\u7684\u5185\u5b58(\u5982\u679c\u6709\u5fc5\u8981)\u3002\u8fd9\u4e0d\u4ec5\u65b9\u4fbf(\u56e0\u4e3a\u60a8\u4e0d\u5fc5\u81ea\u5df1\u505a)\uff0c\u8fd8\u6709\u52a9\u4e8e\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\u3002\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\uff1a 1 2 3 4 5 6 7 8 9 10 11 void doSomething ( bool earlyExit ) { int * array { new int [ 5 ] { 9 , 7 , 5 , 3 , 1 } }; // \u4f7f\u7528new\u5206\u914d\u5185\u5b58 if ( earlyExit ) return ; // \u9000\u51fa\u51fd\u6570\u800c\u4e0d\u4f1a\u91ca\u653e\u5185\u5b58 // do stuff here delete [] array ; // \u5e76\u4e0d\u4f1a\u88ab\u8c03\u7528 } COPY \u5982\u679c earlyExit \u88ab\u8bbe\u7f6e\u4e3a true \uff0c\u5219\u6570\u7ec4\u7684\u5185\u5b58\u4e0d\u4f1a\u88ab\u91ca\u653e\uff0c\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\u3002 \u4f46\u662f\uff0c\u5982\u679c array \u662f std::vector \u7684\u8bdd\uff0c\u5c31\u4e0d\u4f1a\u51fa\u73b0\u95ee\u9898\uff0c\u56e0\u4e3a array \u4f1a\u5728 \u79bb\u5f00\u4f5c\u7528\u57df \u65f6\u81ea\u52a8\u91ca\u653e\u5185\u5b58(\u4e0d\u8bba\u51fd\u6570\u662f\u5426\u6b63\u786e\u9000\u51fa)\u3002\u8fd9\u4e5f\u8bf4\u660e\u4f7f\u7528 std::vector \u6bd4\u81ea\u5df1\u7ba1\u7406\u5185\u5b58\u8981\u66f4\u52a0\u5b89\u5168\u3002 Vectors \u80fd\u591f\u4fdd\u5b58\u5176\u957f\u5ea6\u4fe1\u606f \u4e0d\u540c\u4e8e\u5185\u5efa\u7684\u52a8\u6001\u6570\u7ec4\uff08\u4e0d\u77e5\u9053\u5176\u6307\u5411\u6570\u7ec4\u7684\u7a0b\u5ea6\uff09\uff0c std::vector \u80fd\u591f\u4fdd\u5b58\u5176\u957f\u5ea6\u3002\u4f7f\u7528 size() \u51fd\u6570\u5c31\u53ef\u4ee5\u5f97\u77e5 vector \u7684\u957f\u5ea6\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include void printLength ( const std :: vector < int >& array ) { std :: cout << \"The length is: \" << array . size () << '\\n' ; } int main () { std :: vector array { 9 , 7 , 5 , 3 , 1 }; printLength ( array ); std :: vector < int > empty {}; printLength ( empty ); return 0 ; } \u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 The length is: 5 The length is: 0 \u548c std::array \u4e00\u6837\uff0c size() \u8fd4\u56de\u7684\u662f size_type \u7c7b\u578b\u7684\u503c (\u5b8c\u6574\u7684\u5f62\u5f0f\u4e3a std::vector::size_type )\uff0c\u5b83\u662f\u65e0\u7b26\u53f7\u6574\u578b\u6570\u3002 \u8c03\u6574 vector \u5927\u5c0f \u8c03\u6574\u5185\u7f6e\u7684\u52a8\u6001\u6570\u7ec4\u7684\u5927\u5c0f\u662f\u5f88\u590d\u6742\u7684\uff0c\u800c\u8c03\u6574 std::vector \u7684\u5927\u5c0f\u5219\u5f88\u7b80\u5355\uff0c\u8c03\u7528 resize() \u5373\u53ef\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: vector array { 0 , 1 , 2 }; array . resize ( 5 ); // set size to 5 std :: cout << \"The length is: \" << array . size () << '\\n' ; for ( int i : array ) std :: cout << i << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6253\u5370\uff1a 1 2 The length is: 5 0 1 2 0 0 \u6709\u4e24\u4ef6\u4e8b\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0c\u5f53\u8c03\u6574 vector \u7684\u5927\u5c0f\u65f6\uff0c\u5df2\u7ecf\u5b58\u5728\u7684\u5143\u7d20\u4f1a\u88ab\u4fdd\u5b58\u4e0b\u6765\u3002\u7b2c\u4e8c\uff0c\u65b0\u7684\u5143\u7d20\u4f1a\u88ab\u521d\u59cb\u5316\u4e3a\u8be5\u7c7b\u578b\u7684\u9ed8\u8ba4\u503c\uff08\u6574\u578b\u662f0\uff09\u3002 Vectors \u7684\u5927\u5c0f\u4e5f\u53ef\u4ee5\u7f29\u5c0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: vector array { 0 , 1 , 2 , 3 , 4 }; array . resize ( 3 ); // set length to 3 std :: cout << \"The length is: \" << array . size () << '\\n' ; for ( int i : array ) std :: cout << i << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 The length is: 3 0 1 2 \u8c03\u6574vector\u5927\u5c0f\u7684\u5f00\u9500\u662f\u5f88\u5927\u7684\uff0c\u56e0\u6b64\u5e94\u8be5\u5c3d\u91cf\u5c11\u505a\u3002\u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a\u5177\u6709\u7279\u5b9a\u6570\u91cf\u5143\u7d20\u7684\u5411\u91cf\uff0c\u4f46\u5728\u58f0\u660e\u65f6\u4e0d\u77e5\u9053\u5143\u7d20\u7684\u503c\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u9ed8\u8ba4\u5143\u7d20\u7684\u5411\u91cf\uff0c\u5982\u4e0b\u6240\u793a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include int main () { // \u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\uff0c\u521b\u5efa\u4e00\u4e2a\u5305\u542b5\u4e2a\u5143\u7d20\u7684vector // \u6bcf\u4e2a\u5143\u7d20\u90fd\u88ab\u521d\u59cb\u5316\u4e3a0\u3002\u5982\u679c\u4f60\u4f7f\u7528\u62ec\u53f7\u521d\u59cb\u5316\u7684\u8bdd\uff0c\u5219v // vector\u4f1a\u5305\u542b\u4e00\u4e2a\u5143\u7d20\uff0c\u503c\u4e3a5 std :: vector < int > array ( 5 ); std :: cout << \"The length is: \" << array . size () << '\\n' ; for ( int i : array ) std :: cout << i << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6253\u5370\uff1a 1 2 The length is: 5 0 0 0 0 0 \u5c0f\u8d34\u58eb \u6211\u4eec\u4f1a\u5728 16.7 - std::initializer_list \u4e2d\u4ecb\u7ecd \u76f4\u63a5\u521d\u59cb\u5316 \u548c \u62ec\u53f7\u521d\u59cb\u5316 \u7684\u4e0d\u540c\u3002\u7ecf\u9a8c\u6cd5\u5219\u662f\uff1a\u5bf9\u4e8e\u4f60\u4e0d\u60f3\u521d\u59cb\u5316\u7684\u5217\u8868\u578b\u7c7b\u578b\uff0c\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\u3002 \u538b\u7f29\u5e03\u5c14\u503c std::vector \u8fd8\u6709\u53e6\u5916\u4e00\u4e2a\u5f88\u9177\u7684\u5c0f\u7279\u6027\uff0c\u5b83\u4f1a\u628a8\u4e2a\u5e03\u5c14\u7c7b\u578b\u7684\u5143\u7d20\u538b\u7f29\u4e3a\u4e00\u4e2a\u5b57\u8282\uff01\u8fd9\u4e00\u5207\u90fd\u662f\u5728\u5e55\u540e\u5b8c\u6210\u7684\uff0c\u5bf9\u4f60\u4f7f\u7528 std::vector \u5e76\u6ca1\u6709\u5f71\u54cd\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: vector < bool > array { true , false , false , true , true }; std :: cout << \"The length is: \" << array . size () << '\\n' ; for ( int i : array ) std :: cout << i << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 The length is: 5 1 0 0 1 1 \u66f4\u591a\u5185\u5bb9 \u6ce8\u610f\uff0c\u672c\u8282\u8bfe\u53ea\u662f\u5bf9 std::vector \u8fdb\u884c\u7b80\u4ecb\uff0c\u5728 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a \u4e2d\uff0c\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5b83\u7684\u4e00\u4e9b\u989d\u5916\u7684\u529f\u80fd\uff0c\u5305\u62ec\u957f\u5ea6\u548c\u5bb9\u91cf\u7684\u533a\u522b\uff0c\u5e76\u4e14\u8be6\u7ec6\u4ecb\u7ecd std::vector \u662f\u5982\u4f55\u7ba1\u7406\u5185\u5b58\u7684\u3002 \u7ed3\u8bba \u56e0\u4e3a std::vector \u7c7b\u578b\u662f\u53d8\u91cf\u53ef\u4ee5\u81ea\u5df1\u7ba1\u7406\u5185\u5b58\uff08\u6709\u52a9\u4e8e\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\uff09\u5e76\u4e14\u80fd\u591f\u8bb0\u5f55\u81ea\u8eab\u957f\u5ea6\uff0c\u6240\u4ee5\u6211\u4eec\u63a8\u8350\u5728\u5927\u591a\u6570\u9700\u8981\u4f7f\u7528\u52a8\u6001\u6570\u7ec4\u7684\u65f6\u5019\uff0c\u4f7f\u7528 std::vector \u3002","title":"11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb"},{"location":"11-17-An-introduction-to-std-vector/#stdvector","text":"std::vector \u5728C++03\u4e2d\u88ab\u5f15\u5165\uff0c\u5b83\u63d0\u4f9b\u4e86 dynamic-array \u7684\u529f\u80fd\u5e76\u4e14\u53ef\u4ee5\u81ea\u6211\u7ba1\u7406\u5185\u5b58\u7684\u7533\u8bf7\u548c\u91ca\u653e\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u4f60\u53ef\u4ee5\u5728 \u8fd0\u884c\u65f6 \u521b\u5efa\u6570\u7ec4\u5e76\u6307\u5b9a\u5176\u957f\u5ea6\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528 new \u548c delete \u6765\u624b\u52a8\u7ba1\u7406\u5185\u5b58\u7684\u7533\u8bf7\u91ca\u653e\u3002 std::vector \u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d\u3002 \u58f0\u660e\u4e00\u4e2a std::vector \u5f88\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 #include // \u5728\u58f0\u660e\u65f6\u65e0\u9700\u6307\u5b9a\u957f\u5ea6 std :: vector < int > array ; std :: vector < int > array2 = { 9 , 7 , 5 , 3 , 1 }; // \u4f7f\u7528\u521d\u59cb\u5316\u503c\u5217\u8868\u8fdb\u884c\u521d\u59cb\u5316(\u5728C++11\u4e4b\u524d) std :: vector < int > array3 { 9 , 7 , 5 , 3 , 1 }; // \u4f7f\u7528\u7edf\u4e00\u521d\u59cb\u5316 //\u548cstd::array\u4e00\u6837\uff0c\u4eceC++17\u5f00\u59cb\uff0c\u7c7b\u578b\u53ef\u4ee5\u7701\u7565 std :: vector array4 { 9 , 7 , 5 , 3 , 1 }; // deduced to std::vector \u6ce8\u610f\uff0c\u4e0d\u8bba\u662f\u521d\u59cb\u5316\u4e0e\u5426\uff0c\u4f60\u90fd\u4e0d\u9700\u8981\u5728\u7f16\u8bd1\u65f6\u6307\u5b9a\u6570\u7ec4\u957f\u5ea6\u3002\u8fd9\u662f\u56e0\u4e3a std::vector \u53ef\u4ee5\u5728\u9700\u8981\u65f6\u81ea\u884c\u5206\u914d\u5185\u5b58\u3002 \u548c std::array \u4e00\u6837\uff0c\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u53ef\u4ee5\u4f7f\u7528 \u4e0b\u6807\u8fd0\u7b97\u7b26 \uff08\u4e0d\u8fdb\u884c\u8d8a\u754c\u68c0\u67e5\uff09\u6216 at() \u51fd\u6570\uff08\u8fdb\u884c\u8d8a\u754c\u68c0\u67e5\uff09\u8bbf\u95ee\uff1a 1 2 array [ 6 ] = 2 ; // no bounds checking array . at ( 7 ) = 3 ; // does bounds checking \u5bf9\u4e8e\u4e0a\u9762\u4e24\u79cd\u60c5\u51b5\uff0c\u5982\u679c\u4f60\u8bbf\u95ee\u8d85\u51fa\u6570\u7ec4\u7ed3\u5c3e\u7684\u5143\u7d20\uff0cvector\u5e76\u4e0d\u4f1a\u81ea\u52a8\u8c03\u6574\u957f\u5ea6\u3002 \u5728 C++11 \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 \u521d\u59cb\u5316\u503c\u5217\u8868 \u4e3a std::vector \u8d4b\u503c\uff1a 1 2 array = { 0 , 1 , 2 , 3 , 4 }; // okay, array length is now 5 array = { 9 , 8 , 7 }; // okay, array length is now 3 \u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0cvector\u53ef\u4ee5\u6839\u636e\u5143\u7d20\u7684\u4e2a\u6570\u8c03\u6574\u5176\u957f\u5ea6\u3002","title":"std::vector \u7b80\u4ecb"},{"location":"11-17-An-introduction-to-std-vector/#_1","text":"\u5f53\u4e00\u4e2avector\u53d8\u91cf\u8d85\u51fa\u4f5c\u7528\u57df\u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u91ca\u653e\u5b83\u6240\u4f7f\u7528\u7684\u5185\u5b58(\u5982\u679c\u6709\u5fc5\u8981)\u3002\u8fd9\u4e0d\u4ec5\u65b9\u4fbf(\u56e0\u4e3a\u60a8\u4e0d\u5fc5\u81ea\u5df1\u505a)\uff0c\u8fd8\u6709\u52a9\u4e8e\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\u3002\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\uff1a 1 2 3 4 5 6 7 8 9 10 11 void doSomething ( bool earlyExit ) { int * array { new int [ 5 ] { 9 , 7 , 5 , 3 , 1 } }; // \u4f7f\u7528new\u5206\u914d\u5185\u5b58 if ( earlyExit ) return ; // \u9000\u51fa\u51fd\u6570\u800c\u4e0d\u4f1a\u91ca\u653e\u5185\u5b58 // do stuff here delete [] array ; // \u5e76\u4e0d\u4f1a\u88ab\u8c03\u7528 } COPY \u5982\u679c earlyExit \u88ab\u8bbe\u7f6e\u4e3a true \uff0c\u5219\u6570\u7ec4\u7684\u5185\u5b58\u4e0d\u4f1a\u88ab\u91ca\u653e\uff0c\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\u3002 \u4f46\u662f\uff0c\u5982\u679c array \u662f std::vector \u7684\u8bdd\uff0c\u5c31\u4e0d\u4f1a\u51fa\u73b0\u95ee\u9898\uff0c\u56e0\u4e3a array \u4f1a\u5728 \u79bb\u5f00\u4f5c\u7528\u57df \u65f6\u81ea\u52a8\u91ca\u653e\u5185\u5b58(\u4e0d\u8bba\u51fd\u6570\u662f\u5426\u6b63\u786e\u9000\u51fa)\u3002\u8fd9\u4e5f\u8bf4\u660e\u4f7f\u7528 std::vector \u6bd4\u81ea\u5df1\u7ba1\u7406\u5185\u5b58\u8981\u66f4\u52a0\u5b89\u5168\u3002","title":"\u81ea\u6211\u6e05\u7406\uff0c\u907f\u514d\u5185\u5b58\u6cc4\u6f0f"},{"location":"11-17-An-introduction-to-std-vector/#vectors","text":"\u4e0d\u540c\u4e8e\u5185\u5efa\u7684\u52a8\u6001\u6570\u7ec4\uff08\u4e0d\u77e5\u9053\u5176\u6307\u5411\u6570\u7ec4\u7684\u7a0b\u5ea6\uff09\uff0c std::vector \u80fd\u591f\u4fdd\u5b58\u5176\u957f\u5ea6\u3002\u4f7f\u7528 size() \u51fd\u6570\u5c31\u53ef\u4ee5\u5f97\u77e5 vector \u7684\u957f\u5ea6\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include void printLength ( const std :: vector < int >& array ) { std :: cout << \"The length is: \" << array . size () << '\\n' ; } int main () { std :: vector array { 9 , 7 , 5 , 3 , 1 }; printLength ( array ); std :: vector < int > empty {}; printLength ( empty ); return 0 ; } \u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 The length is: 5 The length is: 0 \u548c std::array \u4e00\u6837\uff0c size() \u8fd4\u56de\u7684\u662f size_type \u7c7b\u578b\u7684\u503c (\u5b8c\u6574\u7684\u5f62\u5f0f\u4e3a std::vector::size_type )\uff0c\u5b83\u662f\u65e0\u7b26\u53f7\u6574\u578b\u6570\u3002","title":"Vectors \u80fd\u591f\u4fdd\u5b58\u5176\u957f\u5ea6\u4fe1\u606f"},{"location":"11-17-An-introduction-to-std-vector/#vector","text":"\u8c03\u6574\u5185\u7f6e\u7684\u52a8\u6001\u6570\u7ec4\u7684\u5927\u5c0f\u662f\u5f88\u590d\u6742\u7684\uff0c\u800c\u8c03\u6574 std::vector \u7684\u5927\u5c0f\u5219\u5f88\u7b80\u5355\uff0c\u8c03\u7528 resize() \u5373\u53ef\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: vector array { 0 , 1 , 2 }; array . resize ( 5 ); // set size to 5 std :: cout << \"The length is: \" << array . size () << '\\n' ; for ( int i : array ) std :: cout << i << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6253\u5370\uff1a 1 2 The length is: 5 0 1 2 0 0 \u6709\u4e24\u4ef6\u4e8b\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0c\u5f53\u8c03\u6574 vector \u7684\u5927\u5c0f\u65f6\uff0c\u5df2\u7ecf\u5b58\u5728\u7684\u5143\u7d20\u4f1a\u88ab\u4fdd\u5b58\u4e0b\u6765\u3002\u7b2c\u4e8c\uff0c\u65b0\u7684\u5143\u7d20\u4f1a\u88ab\u521d\u59cb\u5316\u4e3a\u8be5\u7c7b\u578b\u7684\u9ed8\u8ba4\u503c\uff08\u6574\u578b\u662f0\uff09\u3002 Vectors \u7684\u5927\u5c0f\u4e5f\u53ef\u4ee5\u7f29\u5c0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: vector array { 0 , 1 , 2 , 3 , 4 }; array . resize ( 3 ); // set length to 3 std :: cout << \"The length is: \" << array . size () << '\\n' ; for ( int i : array ) std :: cout << i << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 The length is: 3 0 1 2 \u8c03\u6574vector\u5927\u5c0f\u7684\u5f00\u9500\u662f\u5f88\u5927\u7684\uff0c\u56e0\u6b64\u5e94\u8be5\u5c3d\u91cf\u5c11\u505a\u3002\u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a\u5177\u6709\u7279\u5b9a\u6570\u91cf\u5143\u7d20\u7684\u5411\u91cf\uff0c\u4f46\u5728\u58f0\u660e\u65f6\u4e0d\u77e5\u9053\u5143\u7d20\u7684\u503c\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u9ed8\u8ba4\u5143\u7d20\u7684\u5411\u91cf\uff0c\u5982\u4e0b\u6240\u793a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include int main () { // \u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\uff0c\u521b\u5efa\u4e00\u4e2a\u5305\u542b5\u4e2a\u5143\u7d20\u7684vector // \u6bcf\u4e2a\u5143\u7d20\u90fd\u88ab\u521d\u59cb\u5316\u4e3a0\u3002\u5982\u679c\u4f60\u4f7f\u7528\u62ec\u53f7\u521d\u59cb\u5316\u7684\u8bdd\uff0c\u5219v // vector\u4f1a\u5305\u542b\u4e00\u4e2a\u5143\u7d20\uff0c\u503c\u4e3a5 std :: vector < int > array ( 5 ); std :: cout << \"The length is: \" << array . size () << '\\n' ; for ( int i : array ) std :: cout << i << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6253\u5370\uff1a 1 2 The length is: 5 0 0 0 0 0 \u5c0f\u8d34\u58eb \u6211\u4eec\u4f1a\u5728 16.7 - std::initializer_list \u4e2d\u4ecb\u7ecd \u76f4\u63a5\u521d\u59cb\u5316 \u548c \u62ec\u53f7\u521d\u59cb\u5316 \u7684\u4e0d\u540c\u3002\u7ecf\u9a8c\u6cd5\u5219\u662f\uff1a\u5bf9\u4e8e\u4f60\u4e0d\u60f3\u521d\u59cb\u5316\u7684\u5217\u8868\u578b\u7c7b\u578b\uff0c\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\u3002","title":"\u8c03\u6574 vector \u5927\u5c0f"},{"location":"11-17-An-introduction-to-std-vector/#_2","text":"std::vector \u8fd8\u6709\u53e6\u5916\u4e00\u4e2a\u5f88\u9177\u7684\u5c0f\u7279\u6027\uff0c\u5b83\u4f1a\u628a8\u4e2a\u5e03\u5c14\u7c7b\u578b\u7684\u5143\u7d20\u538b\u7f29\u4e3a\u4e00\u4e2a\u5b57\u8282\uff01\u8fd9\u4e00\u5207\u90fd\u662f\u5728\u5e55\u540e\u5b8c\u6210\u7684\uff0c\u5bf9\u4f60\u4f7f\u7528 std::vector \u5e76\u6ca1\u6709\u5f71\u54cd\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: vector < bool > array { true , false , false , true , true }; std :: cout << \"The length is: \" << array . size () << '\\n' ; for ( int i : array ) std :: cout << i << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 The length is: 5 1 0 0 1 1","title":"\u538b\u7f29\u5e03\u5c14\u503c"},{"location":"11-17-An-introduction-to-std-vector/#_3","text":"\u6ce8\u610f\uff0c\u672c\u8282\u8bfe\u53ea\u662f\u5bf9 std::vector \u8fdb\u884c\u7b80\u4ecb\uff0c\u5728 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a \u4e2d\uff0c\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5b83\u7684\u4e00\u4e9b\u989d\u5916\u7684\u529f\u80fd\uff0c\u5305\u62ec\u957f\u5ea6\u548c\u5bb9\u91cf\u7684\u533a\u522b\uff0c\u5e76\u4e14\u8be6\u7ec6\u4ecb\u7ecd std::vector \u662f\u5982\u4f55\u7ba1\u7406\u5185\u5b58\u7684\u3002","title":"\u66f4\u591a\u5185\u5bb9"},{"location":"11-17-An-introduction-to-std-vector/#_4","text":"\u56e0\u4e3a std::vector \u7c7b\u578b\u662f\u53d8\u91cf\u53ef\u4ee5\u81ea\u5df1\u7ba1\u7406\u5185\u5b58\uff08\u6709\u52a9\u4e8e\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\uff09\u5e76\u4e14\u80fd\u591f\u8bb0\u5f55\u81ea\u8eab\u957f\u5ea6\uff0c\u6240\u4ee5\u6211\u4eec\u63a8\u8350\u5728\u5927\u591a\u6570\u9700\u8981\u4f7f\u7528\u52a8\u6001\u6570\u7ec4\u7684\u65f6\u5019\uff0c\u4f7f\u7528 std::vector \u3002","title":"\u7ed3\u8bba"},{"location":"11-18-Introduction-to-iterators/","tags":["iterator"],"text":"Key Takeaway \u6570\u7ec4\uff08\u6216\u5176\u4ed6\u6570\u636e\u7ed3\u6784\uff09\u7684\u904d\u5386\u662f\u7f16\u7a0b\u4e2d\u4e00\u9879\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u4ecb\u7ecd\u8fc7\u591a\u79cd\u904d\u5386\u65b9\u6cd5\uff1a\u4f7f\u7528\u5faa\u73af\u548c\u7d22\u5f15( for-loops \u548c while loops )\uff0c\u4f7f\u7528\u6307\u9488\u548c\u6307\u9488\u8fd0\u7b97\uff0c\u4f7f\u7528 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include #include #include int main () { // In C++17, the type of variable data is deduced to std::array // If you get an error compiling this example, see the warning below std :: array data { 0 , 1 , 2 , 3 , 4 , 5 , 6 }; std :: size_t length { std :: size ( data ) }; // while-loop with explicit index std :: size_t index { 0 }; while ( index < length ) { std :: cout << data [ index ] << ' ' ; ++ index ; } std :: cout << '\\n' ; // for-loop with explicit index for ( index = 0 ; index < length ; ++ index ) { std :: cout << data [ index ] << ' ' ; } std :: cout << '\\n' ; // for-loop with pointer (Note: ptr can't be const, because we increment it) for ( auto ptr { & data [ 0 ] }; ptr != ( & data [ 0 ] + length ); ++ ptr ) { std :: cout << * ptr << ' ' ; } std :: cout << '\\n' ; // ranged-based for loop for ( int i : data ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u6ce8\u610f \u6ce8\u610f\uff0c\u672c\u8282\u8bfe\u7684\u4f8b\u5b50\u4f7f\u7528\u4e86C++17\u7684\u7279\u6027\uff1a \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad \u3002\u5373\u901a\u8fc7\u53d8\u91cf\u7684\u521d\u59cb\u5316\u503c\u63a8\u65ad\u6a21\u677f\u5b9e\u53c2\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53\u7f16\u8bd1\u5668\u770b\u5230 std::array data{ 0, 1, 2, 3, 4, 5, 6 }; \u65f6\uff0c\u5b83\u4f1a\u63a8\u65ad\u51fa\u6211\u4eec\u5e0c\u671b\u4f7f\u7528 std::array data { 0, 1, 2, 3, 4, 5, 6 }; \u3002 \u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u4e0d\u652f\u6301C++17\uff0c\u4f60\u4f1a\u5f97\u5230\u8fd9\u6837\u7684\u7f16\u8bd1\u9519\u8bef\uff1a\u201cmissing template arguments before \u2018data\u2019\u201d\u3002\u6b64\u65f6\u4f60\u6700\u597d\u6309\u7167 0.12 -- Configuring your compiler: Choosing a language standard \u4ecb\u7ecd\u7684\u65b9\u6cd5\u6253\u5f00C++17\u652f\u6301\u3002\u5982\u679c\u5b9e\u5728\u4e0d\u884c\uff0c\u4f60\u53ef\u4ee5\u5c06\u8fd9\u4e00\u884c\u66ff\u6362\u4e3a\u663e\u5f0f\u7684\u58f0\u660e(e.g. replace std::array data{ 0, 1, 2, 3, 4, 5, 6 }; with std::array data { 0, 1, 2, 3, 4, 5, 6 }; \u5982\u679c\u6211\u4eec\u53ea\u9700\u8981\u901a\u8fc7\u7d22\u5f15\u83b7\u53d6\u5143\u7d20\u7684\u8bdd\uff08\u800c\u4e0d\u9700\u8981\u4f7f\u7528\u7d22\u5f15\uff09\uff0c\u90a3\u4e48\u4f7f\u7528\u57fa\u4e8e\u7d22\u5f15\u7684\u904d\u5386\u9700\u8981\u5f88\u591a\u4e0d\u5fc5\u8981\u7684\u952e\u76d8\u8f93\u5165\u3002\u800c\u4e14\uff0c\u8be5\u65b9\u6cd5\u4e5f\u4ec5\u4ec5\u9002\u7528\u4e8e\u652f\u6301\u5143\u7d20\u76f4\u63a5\u8bbf\u95ee\u7684\u5bb9\u5668\uff08\u6570\u7ec4\u652f\u6301\uff0c\u4f46\u662f\u5176\u4ed6\u7c7b\u578b\u7684\u5bb9\u5668\u4f8b\u5982\u94fe\u8868\u5c31\u4e0d\u652f\u6301\uff09\u3002 \u4f7f\u7528\u6307\u9488\u904d\u5386\u66f4\u662f\u5570\u55e6\uff0c\u800c\u4e14\u5bf9\u4e8e\u4e0d\u4e86\u89e3\u6307\u9488\u8fd0\u7b97\u89c4\u5219\u7684\u4eba\u6765\u8bf4\uff0c\u53ef\u8bfb\u6027\u975e\u5e38\u5dee\u3002\u6307\u9488\u904d\u5386\u4e5f\u53ea\u9002\u7528\u4e8e\u5143\u7d20\u5728\u5185\u5b58\u4e2d\u8fde\u7eed\u5b58\u5728\u7684\u60c5\u51b5\uff08\u6570\u7ec4\u662f\u8fd9\u6837\u7684\uff0c\u4f46\u662f\u5176\u4ed6\u7c7b\u578b\u7684\u5bb9\u5668\u53ef\u4e0d\u662f\uff0c\u4f8b\u5982\u94fe\u8868\u3001\u6811\u548c\u6620\u5c04\uff09\u3002 \u6269\u5c55\u9605\u8bfb \u6307\u9488(\u5728\u4e0d\u4f7f\u7528\u6307\u9488\u8fd0\u7b97\u65f6)\u4e5f\u53ef\u4ee5\u7528\u4e8e\u904d\u5386\u4e00\u4e9b\u975e\u987a\u5e8f\u578b\u6570\u636e\u7ed3\u6784\u3002\u5728\u4e00\u4e2a\u94fe\u8868\u4e2d\uff0c\u6bcf\u4e2a\u5143\u7d20\u90fd\u901a\u8fc7\u6307\u9488\u6307\u5411\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u6cbf\u7740\u6307\u9488\u94fe\u904d\u5386\u5404\u4e2a\u5143\u7d20\u3002 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u5c31\u6709\u610f\u601d\u4e86\uff0c\u5b83\u904d\u5386\u5bb9\u5668\u7684\u5177\u4f53\u673a\u5236\u9690\u85cf\u4e8e\u5e55\u540e\u2014\u2014\u800c\u4e14\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e\u5404\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\uff08\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6811\u3001\u6620\u5c04\u7b49\uff09\u3002\u5b83\u7684\u539f\u7406\u662f\u4ec0\u4e48\u5462\uff1f\u5b83\u4f7f\u7528\u4e86\u8fed\u4ee3\u5668\u3002 \u8fed\u4ee3\u5668 \u8fed\u4ee3\u5668 \u662f\u4e00\u4e2a\u7528\u4e8e\u904d\u5386\u5bb9\u5668\uff08\u4f8b\u5982\u6570\u7ec4\u4e2d\u7684\u503c\u6216\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\uff09\u7684\u5bf9\u8c61\uff0c\u5b83\u5728\u79fb\u52a8\u7684\u8fc7\u7a0b\u4e2d\u63d0\u4f9b\u4e86\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8bbf\u95ee\u7684\u80fd\u529b\u3002 \u5bb9\u5668\u53ef\u4ee5\u63d0\u4f9b\u4e0d\u540c\u7c7b\u578b\u7684\u8fed\u4ee3\u5668\u3002\u4f8b\u5982\uff0c\u6570\u7ec4\u5bb9\u5668\u4f1a\u63d0\u4f9b\u5411\u524d\u904d\u5386\u6574\u4e2a\u6570\u7ec4\u7684\u524d\u5411\u8fed\u4ee3\u5668\u548c\u7528\u4e8e\u9006\u5e8f\u904d\u5386\u6570\u7ec4\u7684\u9006\u5e8f\u8fed\u4ee3\u5668\u3002 \u4e00\u65e6\u521b\u5efa\u4e86\u5408\u9002\u7684\u8fed\u4ee3\u5668\uff0c\u7a0b\u5e8f\u5458\u5c31\u53ef\u4ee5\u4f7f\u7528\u8be5\u8fed\u4ee3\u5668\u63d0\u4f9b\u7684\u63a5\u53e3\u6765\u904d\u5386\u548c\u8bbf\u95ee\u5bb9\u5668\u4e2d\u7684\u5143\u7d20\uff0c\u800c\u4e0d\u9700\u8981\u64cd\u5fc3\u5177\u4f53\u7684\u904d\u5386\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff0c\u4e5f\u4e0d\u9700\u8981\u64cd\u5fc3\u5bb9\u5668\u4e2d\u7684\u6570\u636e\u662f\u5982\u4f55\u5b58\u50a8\u7684\u3002\u540c\u65f6\uff0c\u56e0\u4e3aC++\u4e2d\u7684\u8fed\u4ee3\u5668\u901a\u5e38\u63d0\u4f9b\u4e86\u76f8\u540c\u7684\u63a5\u53e3\u7528\u4e8e\u904d\u5386\uff08 ++ \u64cd\u4f5c\u7b26\u7528\u4e8e\u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5143\u7d20\uff09\u548c\u8bbf\u95ee\uff08 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u7528\u4e8e\u8bbf\u95ee\u5143\u7d20\uff09\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u81f4\u7684\u5ba2\u6237\u7aef\u4ee3\u7801\u6765\u904d\u5386\u5404\u79cd\u5404\u6837\u7684\u5bb9\u5668\u3002 \u6307\u9488\u4f5c\u4e3a\u8fed\u4ee3\u5668 \u5176\u5b9e\uff0c\u6700\u7b80\u5355\u7684\u8fed\u4ee3\u5668\u5c31\u662f\u6307\u9488\u3002\u6307\u9488\uff08\u4f7f\u7528\u6307\u9488\u8fd0\u7b97\uff09\u53ef\u4ee5\u7528\u4e8e\u904d\u5386\u987a\u5e8f\u5b58\u653e\u5728\u5185\u5b58\u4e2d\u7684\u6570\u636e\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5148\u590d\u4e60\u4e00\u4e0b\u6307\u9488\u548c\u6307\u9488\u8fd0\u7b97\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include int main () { std :: array data { 0 , 1 , 2 , 3 , 4 , 5 , 6 }; auto begin { & data [ 0 ] }; // note that this points to one spot beyond the last element auto end { begin + std :: size ( data ) }; // for-loop with pointer for ( auto ptr { begin }; ptr != end ; ++ ptr ) // ++ to move to next element { std :: cout << * ptr << ' ' ; // Indirection to get value of current element } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\uff1a 1 0 1 2 3 4 5 6 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u53d8\u91cf\uff1a begin (\u6307\u5411\u5bb9\u5668\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20)\u548c end (\u6807\u8bb0\u4e3a\u7ed3\u675f\u70b9)\u3002\u5bf9\u4e8e\u6570\u7ec4\u4e2d\u6765\u8bf4\uff0c\u6211\u4eec\u901a\u5e38\u5047\u5b9a\u6570\u7ec4\u591a\u5305\u542b\u4e00\u4e2a\u5143\u7d20\uff0c\u800c\u7ed3\u675f\u6807\u8bb0\u5e94\u8bbe\u7f6e\u4e3a\u8be5\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002 \u7136\u540e\uff0c\u6307\u9488\u4ece begin \u904d\u5386\u5230 end \uff0c\u800c\u4e14\u6240\u5904\u4f4d\u7f6e\u7684\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u8bbf\u95ee\u3002 \u6ce8\u610f \u4f60\u53ef\u80fd\u4f1a\u5c1d\u8bd5\u4f7f\u7528\u53d6\u5730\u5740\u548c\u6570\u7ec4\u8bed\u6cd5\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\u83b7\u53d6\u7ed3\u675f\u6807\u8bb0\uff1a 1 int * end { & data [ std :: size ( data )] }; \u8fd9\u4e48\u505a\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \uff0c\u56e0\u4e3a\u5bf9\u4e8e\u4e0b\u6807\u6765\u8bf4\uff0c data[std::size(data)] \u6240\u8bbf\u95ee\u7684\u5143\u7d20\u5df2\u7ecf\u8d8a\u754c\u4e86\u3002 \u5e94\u8be5\u8fd9\u4e48\u505a 1 int * end { data . data () + std :: size ( data ) }; // data() \u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5143\u7d20 \u6807\u51c6\u5e93\u8fed\u4ee3\u5668 \u904d\u5386\u662f\u5982\u6b64\u7684\u666e\u904d\uff0c\u6240\u4ee5\u6807\u51c6\u5e93\u4e2d\u6240\u6709\u7684\u5bb9\u5668\u90fd\u652f\u6301\u8fed\u4ee3\u3002\u800c\u4e14\u4e0d\u9700\u8981\u4f7f\u7528\u6307\u9488\u5e76\u8ba1\u7b97\u8d77\u6b62\u70b9\u7684\u4f4d\u7f6e\uff0c\u800c\u662f\u53ef\u4ee5\u51fd\u6570 begin() \u548c end() \u76f4\u63a5\u83b7\u53d6\u8fed\u4ee3\u5668\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include int main () { std :: array array { 1 , 2 , 3 }; // \u901a\u8fc7begin\u548cend\u51fd\u6570\u76f4\u63a5\u83b7\u53d6\u8d77\u6b62\u70b9 auto begin { array . begin () }; auto end { array . end () }; for ( auto p { begin }; p != end ; ++ p ) // ++ \u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5143\u7d20 { std :: cout << * p << ' ' ; // \u95f4\u63a5\u8bbf\u95ee\u5f53\u524d\u5143\u7d20 } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 1 2 3 iterator \u5934\u6587\u4ef6\u8fd8\u5305\u542b\u4e86\u4e24\u4e2a\u6cdb\u578b\u51fd\u6570 std::begin \u548c std::end \uff0c\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u8fed\u4ee3\u5668\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include #include // For std::begin and std::end int main () { std :: array array { 1 , 2 , 3 }; // Use std::begin and std::end to get the begin and end points. auto begin { std :: begin ( array ) }; auto end { std :: end ( array ) }; for ( auto p { begin }; p != end ; ++ p ) // ++ to move to next element { std :: cout << * p << ' ' ; // Indirection to get value of current element } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u4e00\u6837\uff1a 1 1 2 3 \u73b0\u5728\u4e0d\u8981\u62c5\u5fc3\u8fed\u4ee3\u5668\u7684\u7c7b\u578b\uff0c\u6211\u4eec\u5c06\u5728\u540e\u9762\u7684\u7ae0\u8282\u4e2d\u518d\u6b21\u4ecb\u7ecd\u8fed\u4ee3\u5668\u3002\u91cd\u8981\u7684\u662f\uff0c\u8fed\u4ee3\u5668\u8d1f\u8d23\u5904\u7406\u904d\u5386\u5bb9\u5668\u6240\u9700\u7684\u5404\u9879\u7ec6\u8282\u3002\u6211\u4eec\u53ea\u9700\u8981\u56db\u4e2a\u4e1c\u897f\uff1a\u8d77\u70b9\u3001\u7ec8\u70b9\u3001\u5c06\u8fed\u4ee3\u5668\u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5143\u7d20(\u6216\u7ed3\u675f)\u7684\u64cd\u4f5c\u7b26++\u548c\u83b7\u53d6\u5f53\u524d\u5143\u7d20\u503c\u7684\u64cd\u4f5c\u7b26*\u3002 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u6240\u6709\u5177\u6709 begin() \u548c end() \u6210\u5458\u51fd\u6570\u7684\u7c7b\u578b\uff0c\u6216\u8005\u53ef\u4ee5\u914d\u5408 std::begin() \u548c std::end() \u4f7f\u7528\u7684\u7c7b\u578b\uff0c\u90fd\u53ef\u4ee5\u7528\u4e8e \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include int main () { std :: array array { 1 , 2 , 3 }; // This does exactly the same as the loop we used before. for ( int i : array ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u5b9e\u9645\u4e0a\uff0c\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af\u4f1a\u5728\u80cc\u5730\u91cc\u8c03\u7528\u88ab\u904d\u5386\u7c7b\u578b\u7684 begin() \u548c end() \u3002 std::array \u662f\u5177\u6709 begin \u548c end \u6210\u5458\u51fd\u6570\u7684\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u88ab\u7528\u4e8e\u57fa\u4e8e\u8303\u56f4\u7684\u5faa\u73af\u3002C\u8bed\u8a00\u98ce\u683c\u7684\u56fa\u5b9a\u6570\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7 std::begin \u548c std::end \u6765\u83b7\u53d6\u8d77\u6b62\u70b9\uff0c\u6240\u4ee5\u5b83\u4e5f\u53ef\u4ee5\u88ab\u7528\u4e8e\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af\u3002\u52a8\u6001\u6570\u7ec4\u5c31\u4e0d\u597d\u4f7f\u4e86\uff0c\u56e0\u4e3a std::end \u51fd\u6570\u5bf9\u5b83\u65e0\u6548\uff08\u7c7b\u578b\u4fe1\u606f\u4e0d\u5305\u542b\u6570\u7ec4\u957f\u5ea6\uff09\u3002 \u7a0d\u540e\u4f60\u5c06\u5b66\u4e60\u5982\u4f55\u5411\u7c7b\u578b\u4e2d\u6dfb\u52a0\u51fd\u6570\uff0c\u4ee5\u4fbf\u5b83\u4eec\u4e5f\u53ef\u4ee5\u88ab\u7528\u4e8e\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af\u3002 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u5e76\u4e0d\u662f\u552f\u4e00\u4f7f\u7528\u8fed\u4ee3\u5668\u7684\u5730\u65b9\u3002\u8fed\u4ee3\u5668\u4e5f\u7528\u5728 std::sort \u548c\u5176\u4ed6\u7b97\u6cd5\u4e2d\u3002\u5728\u4e86\u89e3\u8fed\u4ee3\u5668\u4e4b\u540e\u4f60\u4f1a\u53d1\u73b0\uff0c\u6807\u51c6\u5e93\u4e2d\u5927\u91cf\u5730\u4f7f\u7528\u4e86\u8fed\u4ee3\u5668\u3002 \u65e0\u6548\u8fed\u4ee3\u5668\uff08\u60ac\u5782\u8fed\u4ee3\u5668\uff09 Much like pointers and references, iterators can be left \u201cdangling\u201d if the elements being iterated over change address or are destroyed. When this happens, we say the iterator has been invalidated. Accessing an invalidated iterator produces undefined behavior. \u5c31\u50cf\u6307\u9488\u548c\u5f15\u7528\u4e00\u6837\uff0c\u5982\u679c\u8fed\u4ee3\u5668\u7684\u5143\u7d20\u5730\u5740\u88ab\u4fee\u6539\u6216\u672c\u8eab\u88ab\u9500\u6bc1\u65f6\uff0c\u8fed\u4ee3\u5668\u53ef\u80fd\u4f1a\u5904\u4e8e \u60ac\u5782 \u72b6\u6001\u3002\u5f53\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\u65f6\uff0c\u6211\u4eec\u8bf4\u8fed\u4ee3\u5668\u5df2\u5931\u6548\u3002\u8bbf\u95ee\u65e0\u6548\u7684\u8fed\u4ee3\u5668\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e00\u4e9b\u4fee\u6539\u5bb9\u5668\u7684\u64cd\u4f5c(\u4f8b\u5982\u5411 std::vector \u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20)\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5bb9\u5668\u4e2d\u7684\u5143\u7d20\u5730\u5740\u6539\u53d8\u3002\u5f53\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\u65f6\uff0c\u6307\u5411\u8fd9\u4e9b\u5143\u7d20\u7684\u73b0\u6709\u8fed\u4ee3\u5668\u5c06\u5931\u6548\u3002\u597d\u7684C++\u53c2\u8003\u6587\u6863\u5e94\u8be5\u8bf4\u660e\u54ea\u4e9b\u5bb9\u5668\u64cd\u4f5c\u53ef\u80fd\u6216\u5c06\u4f7f\u8fed\u4ee3\u5668\u5931\u6548\u3002 \u201cIterator invalidation\u201d section of std::vector on cppreference \u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u3002 \u5931\u6548\u7684\u4f8b\u5b50\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include int main () { std :: vector v { 1 , 2 , 3 , 4 , 5 , 6 , 7 }; auto it { v . begin () }; ++ it ; // \u79fb\u52a8\u5230\u7b2c\u4e8c\u4e2a\u5143\u7d20 std :: cout << * it << '\\n' ; // ok: \u6253\u53702 v . erase ( it ); // erase \u5f53\u524d\u904d\u5386\u5230\u7684\u5143\u7d20 // erase() \u4f7f\u5f97\u88ab\u6307\u5411\u88ab\u5220\u9664\u5143\u7d20\u7684\u8fed\u4ee3\u5668\u5931\u6548 // \u6240\u4ee5\u8fed\u4ee3\u5668\"it\" \u73b0\u5728\u662f\u65e0\u6548\u7684 ++ it ; // \u672a\u5b9a\u4e49\u884c\u4e3a std :: cout << * it << '\\n' ; // \u672a\u5b9a\u4e49\u884c\u4e3a return 0 ; }","title":"11.18 \u2014 \u8fed\u4ee3\u5668\u7b80\u4ecb"},{"location":"11-18-Introduction-to-iterators/#_1","text":"\u8fed\u4ee3\u5668 \u662f\u4e00\u4e2a\u7528\u4e8e\u904d\u5386\u5bb9\u5668\uff08\u4f8b\u5982\u6570\u7ec4\u4e2d\u7684\u503c\u6216\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\uff09\u7684\u5bf9\u8c61\uff0c\u5b83\u5728\u79fb\u52a8\u7684\u8fc7\u7a0b\u4e2d\u63d0\u4f9b\u4e86\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8bbf\u95ee\u7684\u80fd\u529b\u3002 \u5bb9\u5668\u53ef\u4ee5\u63d0\u4f9b\u4e0d\u540c\u7c7b\u578b\u7684\u8fed\u4ee3\u5668\u3002\u4f8b\u5982\uff0c\u6570\u7ec4\u5bb9\u5668\u4f1a\u63d0\u4f9b\u5411\u524d\u904d\u5386\u6574\u4e2a\u6570\u7ec4\u7684\u524d\u5411\u8fed\u4ee3\u5668\u548c\u7528\u4e8e\u9006\u5e8f\u904d\u5386\u6570\u7ec4\u7684\u9006\u5e8f\u8fed\u4ee3\u5668\u3002 \u4e00\u65e6\u521b\u5efa\u4e86\u5408\u9002\u7684\u8fed\u4ee3\u5668\uff0c\u7a0b\u5e8f\u5458\u5c31\u53ef\u4ee5\u4f7f\u7528\u8be5\u8fed\u4ee3\u5668\u63d0\u4f9b\u7684\u63a5\u53e3\u6765\u904d\u5386\u548c\u8bbf\u95ee\u5bb9\u5668\u4e2d\u7684\u5143\u7d20\uff0c\u800c\u4e0d\u9700\u8981\u64cd\u5fc3\u5177\u4f53\u7684\u904d\u5386\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff0c\u4e5f\u4e0d\u9700\u8981\u64cd\u5fc3\u5bb9\u5668\u4e2d\u7684\u6570\u636e\u662f\u5982\u4f55\u5b58\u50a8\u7684\u3002\u540c\u65f6\uff0c\u56e0\u4e3aC++\u4e2d\u7684\u8fed\u4ee3\u5668\u901a\u5e38\u63d0\u4f9b\u4e86\u76f8\u540c\u7684\u63a5\u53e3\u7528\u4e8e\u904d\u5386\uff08 ++ \u64cd\u4f5c\u7b26\u7528\u4e8e\u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5143\u7d20\uff09\u548c\u8bbf\u95ee\uff08 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u7528\u4e8e\u8bbf\u95ee\u5143\u7d20\uff09\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u81f4\u7684\u5ba2\u6237\u7aef\u4ee3\u7801\u6765\u904d\u5386\u5404\u79cd\u5404\u6837\u7684\u5bb9\u5668\u3002","title":"\u8fed\u4ee3\u5668"},{"location":"11-18-Introduction-to-iterators/#_2","text":"\u5176\u5b9e\uff0c\u6700\u7b80\u5355\u7684\u8fed\u4ee3\u5668\u5c31\u662f\u6307\u9488\u3002\u6307\u9488\uff08\u4f7f\u7528\u6307\u9488\u8fd0\u7b97\uff09\u53ef\u4ee5\u7528\u4e8e\u904d\u5386\u987a\u5e8f\u5b58\u653e\u5728\u5185\u5b58\u4e2d\u7684\u6570\u636e\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5148\u590d\u4e60\u4e00\u4e0b\u6307\u9488\u548c\u6307\u9488\u8fd0\u7b97\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include int main () { std :: array data { 0 , 1 , 2 , 3 , 4 , 5 , 6 }; auto begin { & data [ 0 ] }; // note that this points to one spot beyond the last element auto end { begin + std :: size ( data ) }; // for-loop with pointer for ( auto ptr { begin }; ptr != end ; ++ ptr ) // ++ to move to next element { std :: cout << * ptr << ' ' ; // Indirection to get value of current element } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\uff1a 1 0 1 2 3 4 5 6 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u53d8\u91cf\uff1a begin (\u6307\u5411\u5bb9\u5668\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20)\u548c end (\u6807\u8bb0\u4e3a\u7ed3\u675f\u70b9)\u3002\u5bf9\u4e8e\u6570\u7ec4\u4e2d\u6765\u8bf4\uff0c\u6211\u4eec\u901a\u5e38\u5047\u5b9a\u6570\u7ec4\u591a\u5305\u542b\u4e00\u4e2a\u5143\u7d20\uff0c\u800c\u7ed3\u675f\u6807\u8bb0\u5e94\u8bbe\u7f6e\u4e3a\u8be5\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002 \u7136\u540e\uff0c\u6307\u9488\u4ece begin \u904d\u5386\u5230 end \uff0c\u800c\u4e14\u6240\u5904\u4f4d\u7f6e\u7684\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u8bbf\u95ee\u3002 \u6ce8\u610f \u4f60\u53ef\u80fd\u4f1a\u5c1d\u8bd5\u4f7f\u7528\u53d6\u5730\u5740\u548c\u6570\u7ec4\u8bed\u6cd5\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\u83b7\u53d6\u7ed3\u675f\u6807\u8bb0\uff1a 1 int * end { & data [ std :: size ( data )] }; \u8fd9\u4e48\u505a\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \uff0c\u56e0\u4e3a\u5bf9\u4e8e\u4e0b\u6807\u6765\u8bf4\uff0c data[std::size(data)] \u6240\u8bbf\u95ee\u7684\u5143\u7d20\u5df2\u7ecf\u8d8a\u754c\u4e86\u3002 \u5e94\u8be5\u8fd9\u4e48\u505a 1 int * end { data . data () + std :: size ( data ) }; // data() \u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5143\u7d20","title":"\u6307\u9488\u4f5c\u4e3a\u8fed\u4ee3\u5668"},{"location":"11-18-Introduction-to-iterators/#_3","text":"\u904d\u5386\u662f\u5982\u6b64\u7684\u666e\u904d\uff0c\u6240\u4ee5\u6807\u51c6\u5e93\u4e2d\u6240\u6709\u7684\u5bb9\u5668\u90fd\u652f\u6301\u8fed\u4ee3\u3002\u800c\u4e14\u4e0d\u9700\u8981\u4f7f\u7528\u6307\u9488\u5e76\u8ba1\u7b97\u8d77\u6b62\u70b9\u7684\u4f4d\u7f6e\uff0c\u800c\u662f\u53ef\u4ee5\u51fd\u6570 begin() \u548c end() \u76f4\u63a5\u83b7\u53d6\u8fed\u4ee3\u5668\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include int main () { std :: array array { 1 , 2 , 3 }; // \u901a\u8fc7begin\u548cend\u51fd\u6570\u76f4\u63a5\u83b7\u53d6\u8d77\u6b62\u70b9 auto begin { array . begin () }; auto end { array . end () }; for ( auto p { begin }; p != end ; ++ p ) // ++ \u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5143\u7d20 { std :: cout << * p << ' ' ; // \u95f4\u63a5\u8bbf\u95ee\u5f53\u524d\u5143\u7d20 } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 1 2 3 iterator \u5934\u6587\u4ef6\u8fd8\u5305\u542b\u4e86\u4e24\u4e2a\u6cdb\u578b\u51fd\u6570 std::begin \u548c std::end \uff0c\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u8fed\u4ee3\u5668\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include #include // For std::begin and std::end int main () { std :: array array { 1 , 2 , 3 }; // Use std::begin and std::end to get the begin and end points. auto begin { std :: begin ( array ) }; auto end { std :: end ( array ) }; for ( auto p { begin }; p != end ; ++ p ) // ++ to move to next element { std :: cout << * p << ' ' ; // Indirection to get value of current element } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u4e00\u6837\uff1a 1 1 2 3 \u73b0\u5728\u4e0d\u8981\u62c5\u5fc3\u8fed\u4ee3\u5668\u7684\u7c7b\u578b\uff0c\u6211\u4eec\u5c06\u5728\u540e\u9762\u7684\u7ae0\u8282\u4e2d\u518d\u6b21\u4ecb\u7ecd\u8fed\u4ee3\u5668\u3002\u91cd\u8981\u7684\u662f\uff0c\u8fed\u4ee3\u5668\u8d1f\u8d23\u5904\u7406\u904d\u5386\u5bb9\u5668\u6240\u9700\u7684\u5404\u9879\u7ec6\u8282\u3002\u6211\u4eec\u53ea\u9700\u8981\u56db\u4e2a\u4e1c\u897f\uff1a\u8d77\u70b9\u3001\u7ec8\u70b9\u3001\u5c06\u8fed\u4ee3\u5668\u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5143\u7d20(\u6216\u7ed3\u675f)\u7684\u64cd\u4f5c\u7b26++\u548c\u83b7\u53d6\u5f53\u524d\u5143\u7d20\u503c\u7684\u64cd\u4f5c\u7b26*\u3002","title":"\u6807\u51c6\u5e93\u8fed\u4ee3\u5668"},{"location":"11-18-Introduction-to-iterators/#for","text":"\u6240\u6709\u5177\u6709 begin() \u548c end() \u6210\u5458\u51fd\u6570\u7684\u7c7b\u578b\uff0c\u6216\u8005\u53ef\u4ee5\u914d\u5408 std::begin() \u548c std::end() \u4f7f\u7528\u7684\u7c7b\u578b\uff0c\u90fd\u53ef\u4ee5\u7528\u4e8e \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include int main () { std :: array array { 1 , 2 , 3 }; // This does exactly the same as the loop we used before. for ( int i : array ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u5b9e\u9645\u4e0a\uff0c\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af\u4f1a\u5728\u80cc\u5730\u91cc\u8c03\u7528\u88ab\u904d\u5386\u7c7b\u578b\u7684 begin() \u548c end() \u3002 std::array \u662f\u5177\u6709 begin \u548c end \u6210\u5458\u51fd\u6570\u7684\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u88ab\u7528\u4e8e\u57fa\u4e8e\u8303\u56f4\u7684\u5faa\u73af\u3002C\u8bed\u8a00\u98ce\u683c\u7684\u56fa\u5b9a\u6570\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7 std::begin \u548c std::end \u6765\u83b7\u53d6\u8d77\u6b62\u70b9\uff0c\u6240\u4ee5\u5b83\u4e5f\u53ef\u4ee5\u88ab\u7528\u4e8e\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af\u3002\u52a8\u6001\u6570\u7ec4\u5c31\u4e0d\u597d\u4f7f\u4e86\uff0c\u56e0\u4e3a std::end \u51fd\u6570\u5bf9\u5b83\u65e0\u6548\uff08\u7c7b\u578b\u4fe1\u606f\u4e0d\u5305\u542b\u6570\u7ec4\u957f\u5ea6\uff09\u3002 \u7a0d\u540e\u4f60\u5c06\u5b66\u4e60\u5982\u4f55\u5411\u7c7b\u578b\u4e2d\u6dfb\u52a0\u51fd\u6570\uff0c\u4ee5\u4fbf\u5b83\u4eec\u4e5f\u53ef\u4ee5\u88ab\u7528\u4e8e\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af\u3002 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u5e76\u4e0d\u662f\u552f\u4e00\u4f7f\u7528\u8fed\u4ee3\u5668\u7684\u5730\u65b9\u3002\u8fed\u4ee3\u5668\u4e5f\u7528\u5728 std::sort \u548c\u5176\u4ed6\u7b97\u6cd5\u4e2d\u3002\u5728\u4e86\u89e3\u8fed\u4ee3\u5668\u4e4b\u540e\u4f60\u4f1a\u53d1\u73b0\uff0c\u6807\u51c6\u5e93\u4e2d\u5927\u91cf\u5730\u4f7f\u7528\u4e86\u8fed\u4ee3\u5668\u3002","title":"\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af"},{"location":"11-18-Introduction-to-iterators/#_4","text":"Much like pointers and references, iterators can be left \u201cdangling\u201d if the elements being iterated over change address or are destroyed. When this happens, we say the iterator has been invalidated. Accessing an invalidated iterator produces undefined behavior. \u5c31\u50cf\u6307\u9488\u548c\u5f15\u7528\u4e00\u6837\uff0c\u5982\u679c\u8fed\u4ee3\u5668\u7684\u5143\u7d20\u5730\u5740\u88ab\u4fee\u6539\u6216\u672c\u8eab\u88ab\u9500\u6bc1\u65f6\uff0c\u8fed\u4ee3\u5668\u53ef\u80fd\u4f1a\u5904\u4e8e \u60ac\u5782 \u72b6\u6001\u3002\u5f53\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\u65f6\uff0c\u6211\u4eec\u8bf4\u8fed\u4ee3\u5668\u5df2\u5931\u6548\u3002\u8bbf\u95ee\u65e0\u6548\u7684\u8fed\u4ee3\u5668\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e00\u4e9b\u4fee\u6539\u5bb9\u5668\u7684\u64cd\u4f5c(\u4f8b\u5982\u5411 std::vector \u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20)\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5bb9\u5668\u4e2d\u7684\u5143\u7d20\u5730\u5740\u6539\u53d8\u3002\u5f53\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\u65f6\uff0c\u6307\u5411\u8fd9\u4e9b\u5143\u7d20\u7684\u73b0\u6709\u8fed\u4ee3\u5668\u5c06\u5931\u6548\u3002\u597d\u7684C++\u53c2\u8003\u6587\u6863\u5e94\u8be5\u8bf4\u660e\u54ea\u4e9b\u5bb9\u5668\u64cd\u4f5c\u53ef\u80fd\u6216\u5c06\u4f7f\u8fed\u4ee3\u5668\u5931\u6548\u3002 \u201cIterator invalidation\u201d section of std::vector on cppreference \u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u3002 \u5931\u6548\u7684\u4f8b\u5b50\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include int main () { std :: vector v { 1 , 2 , 3 , 4 , 5 , 6 , 7 }; auto it { v . begin () }; ++ it ; // \u79fb\u52a8\u5230\u7b2c\u4e8c\u4e2a\u5143\u7d20 std :: cout << * it << '\\n' ; // ok: \u6253\u53702 v . erase ( it ); // erase \u5f53\u524d\u904d\u5386\u5230\u7684\u5143\u7d20 // erase() \u4f7f\u5f97\u88ab\u6307\u5411\u88ab\u5220\u9664\u5143\u7d20\u7684\u8fed\u4ee3\u5668\u5931\u6548 // \u6240\u4ee5\u8fed\u4ee3\u5668\"it\" \u73b0\u5728\u662f\u65e0\u6548\u7684 ++ it ; // \u672a\u5b9a\u4e49\u884c\u4e3a std :: cout << * it << '\\n' ; // \u672a\u5b9a\u4e49\u884c\u4e3a return 0 ; }","title":"\u65e0\u6548\u8fed\u4ee3\u5668\uff08\u60ac\u5782\u8fed\u4ee3\u5668\uff09"},{"location":"11-19-Introduction-to-standard-library-algorithms/","tags":["algorithms","C++20"],"text":"Key Takeaway \u4e2d\u63d0\u4f9b\u4e86\u5f88\u591a\u597d\u7528\u7684\u7b97\u6cd5\uff0c\u7528\u4e8e\u5bf9\u5bb9\u5668\u8fdb\u884c\u67e5\u627e\u3001\u641c\u7d22\u548c\u8ba1\u6570\u3002 std::find \u51fd\u6570\u7528\u4e8e\u67e5\u627e\u67d0\u4e2a\u503c\u5728\u5143\u7d20\u4e2d\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\u3002 std::find \u6709\u4e09\u4e2a\u53c2\u6570\uff1a\u5e8f\u5217\u4e2d\u8d77\u59cb\u5143\u7d20\u7684\u8fed\u4ee3\u5668\u3001\u7ec8\u70b9\u5143\u7d20\u7684\u8fed\u4ee3\u5668\u3001\u9700\u8981\u641c\u7d22\u7684\u503c\u3002\u8be5\u51fd\u6570\u4f1a\u8fd4\u56de\u6307\u5411\u76ee\u6807\u5143\u7d20\u7684\u8fed\u4ee3\u5668\uff08\u5982\u679c\u627e\u5230\u7684\u8bdd\uff09\uff0c\u6216\u8005\u6307\u5411\u5bb9\u5668\u7684\u672b\u5c3e\uff08\u5982\u679c\u6ca1\u627e\u7684\u8bdd\uff09\u3002 std::find_if \u5728\u5bb9\u5668\u4e2d\u67e5\u627e\u662f\u5426\u6709\u4e00\u4e2a\u6ee1\u8db3\u67d0\u79cd\u6761\u4ef6\u7684\u503c\uff08\u4f8b\u5982\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u67d0\u4e2a\u7279\u5b9a\u7684\u5b50\u4e32\uff09 std::count \u548c std::count_if \u7528\u4e8e\u641c\u7d22\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u5e76\u5bf9\u5176\u51fa\u73b0\u6b21\u6570\u8fdb\u884c\u7edf\u8ba1\u3002 std::sort \u9ed8\u8ba4\u5bf9\u6570\u7ec4\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\uff0c\u4f46\u662f\u53ef\u4ee5\u63a5\u53d7\u7b2c\u4e09\u4e2a\u53c2\u6570\uff0c\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219 std::for_each \u63a5\u53d7\u4e00\u4e2a\u5217\u8868\u4f5c\u4e3a\u8f93\u5165\uff0c\u7136\u540e\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u51fd\u6570\u3002\u652f\u6301\u5e76\u884c\u5904\u7406\u3002 \u65b0\u624b\u7a0b\u5e8f\u5458\u53ef\u80fd\u4f1a\u753b\u4e0a\u5927\u91cf\u7684\u65f6\u95f4\u6765\u7f16\u5199\u5faa\u73af\u4ee3\u7801\u5904\u7406\u5f88\u591a\u7b80\u5355\u7684\u4efb\u52a1\uff0c\u4f8b\u5982\u6392\u5e8f\u3001\u8ba1\u6570\u6216\u8005\u662f\u641c\u7d22\u6570\u7ec4\u3002\u8fd9\u4e9b\u5faa\u73af\u53ef\u80fd\u5f88\u5bb9\u6613\u5e26\u6765\u95ee\u9898\uff0c\u4e00\u65b9\u9762\u672c\u8eab\u7f16\u5199\u6570\u7ec4\u5faa\u73af\u5c31\u5bb9\u6613\u4ea7\u751f\u95ee\u9898\uff0c\u53e6\u5916\u4e00\u65b9\u9762\u53ef\u7ef4\u62a4\u6027\u4e5f\u5f88\u4e0d\u597d\uff0c\u56e0\u4e3a\u5faa\u73af\u4ee3\u7801\u901a\u5e38\u6bd4\u8f83\u96be\u4ee5\u7406\u89e3\u3002 \u56e0\u4e3a\u641c\u7d22\u3001\u7edf\u8ba1\u548c\u6392\u5e8f\u662f\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u6240\u4ee5C++\u6807\u51c6\u5e93\u4e3a\u6b64\u63d0\u4f9b\u4e86\u975e\u5e38\u591a\u7684\u51fd\u6570\uff0c\u4ee5\u4fbf\u4f60\u4f7f\u7528\u5be5\u5be5\u51e0\u53e5\u4ee3\u7801\u5c31\u53ef\u4ee5\u5b8c\u6210\u8fd9\u4e9b\u4efb\u52a1\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u6807\u51c6\u5e93\u7684\u51fd\u6570\u4e45\u7ecf\u68c0\u9a8c\u3001\u9ad8\u6548\u3001\u53ef\u4ee5\u914d\u5408\u591a\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u5bb9\u5668\u4f7f\u7528\uff0c\u800c\u4e14\u5b83\u4eec\u4e2d\u591a\u5c11\u90fd\u652f\u6301\u5e76\u884c\uff08\u4f7f\u7528\u591a\u4e2aCPU\u7ebf\u7a0b\u540c\u65f6\u5de5\u4f5c\u4ee5\u4fbf\u66f4\u5feb\u901f\u7684\u5b8c\u6210\u4efb\u52a1\uff09\u3002 \u6807\u51c6\u7b97\u6cd5\u5e93\u63d0\u4f9b\u7684\u529f\u80fd\u5927\u4f53\u53ef\u4ee5\u5206\u4e3a\u4ee5\u4e0b\u4e09\u7c7b\uff1a \u67e5\u9a8c\u7c7b\uff08Inspectors\uff09\u2014\u2014 \u7528\u4e8e\u67e5\u770b\uff08\u4e0d\u4fee\u6539\uff09\u5bb9\u5668\u4e2d\u7684\u6570\u636e\u3002\u5305\u62ec\u641c\u7d22\u548c\u7edf\u8ba1\u3002 \u4fee\u6539\u7c7b\uff08Mutators\uff09\u2014\u2014 \u7528\u4e8e\u4fee\u6539\u5bb9\u5668\u4e2d\u7684\u6570\u636e\u3002\u4f8b\u5982\u6392\u5e8f\u548c\u6d17\u724c\u3002 \u8f85\u52a9\u7c7b\uff08Facilitators\uff09\u2014\u2014 \u7528\u4e8e\u57fa\u4e8e\u6570\u636e\u751f\u6210\u7ed3\u679c\u3002\u4f8b\u5982\u6570\u503c\u76f8\u4e58\u6216\u8005\u786e\u5b9a\u5143\u7d20\u5bf9\u7684\u6392\u5e8f\u987a\u5e8f\u3002 \u8fd9\u4e9b\u7b97\u6cd5\u90fd\u88ab\u5b9a\u4e49\u5728 algorithms \u5e93\u4e2d\u3002\u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5176\u4e2d\u6700\u5e38\u7528\u7684\u4e00\u4e9b\u7b97\u6cd5\u2014\u2014\u4f46\u662f\u5b9e\u9645\u4e0a\u8be5\u7b97\u6cd5\u5e93\u63d0\u4f9b\u7684\u7b97\u6cd5\u8fdc\u8fdc\u4e0d\u6b62\u8fd9\u4e9b\uff0c\u5f3a\u70c8\u5efa\u8bae\u4f60\u70b9\u51fb\u94fe\u63a5\u53bb\u5230\u53c2\u8003\u624b\u518c\uff0c\u770b\u770b\u5b83\u7684\u5168\u90e8\u5185\u5bb9\u3002 \u6ce8\u610f\uff1a\u6240\u6709\u8fd9\u4e9b\u7b97\u6cd5\u90fd\u4f7f\u7528\u4e86 \u8fed\u4ee3\u5668 \uff0c\u5982\u679c\u4f60\u8fd8\u4e0d\u719f\u6089\u8fed\u4ee3\u5668\u7684\u8bdd\uff0c\u518d\u53bb\u5b66\u4e60\u4e00\u4e0b 11.18 \u2014 \u8fed\u4ee3\u5668\u7b80\u4ecb \u5427\u3002 \u4f7f\u7528 std::find \u67e5\u627e\u7279\u5b9a\u503c\u7684\u5143\u7d20 std::find \u51fd\u6570\u7528\u4e8e\u67e5\u627e\u67d0\u4e2a\u503c\u5728\u5143\u7d20\u4e2d\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\u3002 std::find \u6709\u4e09\u4e2a\u53c2\u6570\uff1a\u5e8f\u5217\u4e2d\u8d77\u59cb\u5143\u7d20\u7684\u8fed\u4ee3\u5668\u3001\u7ec8\u70b9\u5143\u7d20\u7684\u8fed\u4ee3\u5668\u3001\u9700\u8981\u641c\u7d22\u7684\u503c\u3002\u8be5\u51fd\u6570\u4f1a\u8fd4\u56de\u6307\u5411\u76ee\u6807\u5143\u7d20\u7684\u8fed\u4ee3\u5668\uff08\u5982\u679c\u627e\u5230\u7684\u8bdd\uff09\uff0c\u6216\u8005\u6307\u5411\u5bb9\u5668\u7684\u672b\u5c3e\uff08\u5982\u679c\u6ca1\u627e\u7684\u8bdd\uff09\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include #include int main () { std :: array arr { 13 , 90 , 99 , 5 , 40 , 80 }; std :: cout << \"Enter a value to search for and replace with: \" ; int search {}; int replace {}; std :: cin >> search >> replace ; // \u5ffd\u7565\u8f93\u5165\u5408\u6cd5\u6027\u68c0\u67e5 // std::find \u8fd4\u56de\u6307\u5411\u627e\u5230\u5143\u7d20\u7684\u8fed\u4ee3\u5668\uff08\u6216\u8005\u662f\u6307\u5411\u5bb9\u5668\u672b\u5c3e\uff09 // \u6211\u4eec\u5c06\u8be5\u5143\u7d20\u5b58\u653e\u5230\u4e00\u4e2a\u53d8\u91cf\u4e2d\uff0c\u901a\u8fc7\u7c7b\u578b\u63a8\u65ad\u6765\u83b7\u53d6\u8fed\u4ee3\u5668\u7684\u7c7b\u578b auto found { std :: find ( arr . begin (), arr . end (), search ) }; // \u6ca1\u6709\u627e\u5230\u5219\u8fd4\u56de\u6307\u5411\u672b\u5c3e\u7684\u8fed\u4ee3\u5668 // \u53ef\u4ee5\u4f7f\u7528end()\u51fd\u6570\u83b7\u53d6\u672b\u5c3e\u5e76\u548c\u8fed\u4ee3\u5668\u8fdb\u884c\u6bd4\u8f83 if ( found == arr . end ()) { std :: cout << \"Could not find \" << search << '\\n' ; } else { // \u66ff\u6362 * found = replace ; } for ( int i : arr ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u5143\u7d20\u5b58\u5728\u7684\u4f8b\u5b50\uff1a 1 2 Enter a value to search for and replace with: 5 234 13 90 99 234 40 80 \u5143\u7d20\u4e0d\u5b58\u5728\u7684\u4f8b\u5b50\uff1a 1 2 3 Enter a value to search for and replace with: 0 234 Could not find 0 13 90 99 5 40 80 \u4f7f\u7528 std::find_if \u67e5\u627e\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20 \u6709\u7684\u65f6\u5019\u6211\u4eec\u9700\u8981\u5728\u5bb9\u5668\u4e2d\u67e5\u627e\u662f\u5426\u6709\u4e00\u4e2a\u6ee1\u8db3\u67d0\u79cd\u6761\u4ef6\u7684\u503c\uff08\u4f8b\u5982\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u67d0\u4e2a\u7279\u5b9a\u7684\u5b50\u4e32\uff09\uff0c\u800c\u4e0d\u662f\u67d0\u4e2a\u5177\u4f53\u7684\u503c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c std::find_if \u5f88\u597d\u7528\u3002 std::find_if \u51fd\u6570\u7684\u5de5\u4f5c\u65b9\u5f0f\u548c std::find \u5f88\u7c7b\u4f3c\uff0c\u4f46\u662f\u6211\u4eec\u4e0d\u9700\u8981\u4f20\u9012\u4e00\u4e2a\u5177\u4f53\u7684\u503c\uff0c\u800c\u662f\u4f20\u9012\u4e00\u4e2a \u53ef\u8c03\u7528\u5bf9\u8c61 \uff0c\u4f8b\u5982\u51fd\u6570\u6307\u9488\uff08\u6216\u8005 lambda\u8868\u8fbe\u5f0f \uff09\u3002 \u5f53\u5143\u7d20\u88ab\u4f9d\u6b21\u904d\u5386\u65f6\uff0c std::find_if \u4f1a\u8c03\u7528\u51fd\u6570\uff08\u901a\u8fc7\u5b9e\u53c2\u4f20\u5165\u7684\u51fd\u6570\uff09\uff0c\u800c\u8be5\u51fd\u6570\u4f1a\u8fd4\u56de true \u6216 false \u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86 std::find_if \u6765\u67e5\u627e\u5305\u542b nut \u5b50\u4e32\u7684\u5143\u7d20\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include #include #include // Our function will return true if the element matches bool containsNut ( std :: string_view str ) { // std::string_view::find returns std::string_view::npos if it doesn't find // the substring. Otherwise it returns the index where the substring occurs // in str. return ( str . find ( \"nut\" ) != std :: string_view :: npos ); } int main () { std :: array < std :: string_view , 4 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" }; // Scan our array to see if any elements contain the \"nut\" substring auto found { std :: find_if ( arr . begin (), arr . end (), containsNut ) }; if ( found == arr . end ()) { std :: cout << \"No nuts \\n \" ; } else { std :: cout << \"Found \" << * found << '\\n' ; } return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Found walnut \u5982\u679c\u6211\u4eec\u81ea\u5df1\u7f16\u5199\u4e0a\u9762\u7684\u793a\u4f8b\uff0c\u5219\u81f3\u5c11\u9700\u8981\u4e09\u4e2a\u5faa\u73af(\u4e00\u4e2a\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u4e24\u4e2a\u5faa\u73af\u5339\u914d\u5b50\u5b57\u7b26\u4e32)\u3002\u6807\u51c6\u5e93\u51fd\u6570\u5141\u8bb8\u6211\u4eec\u5728\u77ed\u77ed\u51e0\u884c\u4ee3\u7801\u4e2d\u5b8c\u6210\u76f8\u540c\u7684\u4e8b\u60c5\uff01 \u4f7f\u7528 std::count \u548c std::count_if \u8fdb\u884c\u8ba1\u6570 std::count \u548c std::count_if \u7528\u4e8e\u641c\u7d22\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u5e76\u5bf9\u5176\u51fa\u73b0\u6b21\u6570\u8fdb\u884c\u7edf\u8ba1\u3002 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u7edf\u8ba1\u6709\u591a\u5c11\u4e2a\u5143\u7d20\u5305\u542b\"nut\"\u5b50\u4e32\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include #include #include bool containsNut ( std :: string_view str ) { return ( str . find ( \"nut\" ) != std :: string_view :: npos ); } int main () { std :: array < std :: string_view , 5 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" , \"peanut\" }; auto nuts { std :: count_if ( arr . begin (), arr . end (), containsNut ) }; std :: cout << \"Counted \" << nuts << \" nut(s) \\n \" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Counted 2 nut(s) \u4f7f\u7528 std::sort \u8fdb\u884c\u81ea\u5b9a\u4e49\u6392\u5e8f \u6211\u4eec\u66fe\u4f7f\u7528 std::sort \u5bf9\u6570\u7ec4\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\uff0c\u4f46\u5b9e\u9645\u4e0a std::sort \u8fd8\u53ef\u4ee5\u505a\u5f88\u591a\u4e8b\u3002\u4e00\u4e2a\u7248\u672c\u7684 std::sort \u53ef\u4ee5\u63a5\u53d7\u7b2c\u4e09\u4e2a\u53c2\u6570\uff0c\u4f7f\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u51fd\u6570\u5b9a\u4e49\u5982\u4f55\u6392\u5e8f\u3002\u8be5\u51fd\u6570\u9700\u8981\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\u7528\u4e8e\u6bd4\u8f83\uff0c\u5982\u679c\u7b2c\u4e00\u4e2a\u6570\u5e94\u8be5\u88ab\u6392\u5728\u7b2c\u4e8c\u4e2a\u6570\u4e4b\u524d\uff0c\u5e94\u8be5\u8fd4\u56de true \u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c std::sort \u4f1a\u6309\u7167\u5347\u5e8f\u8fdb\u884c\u6392\u5e8f\u3002 \u4e0b\u9762\uff0c\u6211\u4eec\u4f7f\u7528 std::sort \u5bf9\u6570\u7ec4\u8fdb\u884c\u964d\u5e8f\u6392\u5e8f\uff08\u901a\u8fc7\u81ea\u5b9a\u4e49\u6bd4\u8f83\u51fd\u6570 greater \u5b9e\u73b0\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include #include bool greater ( int a , int b ) { // Order @a before @b if @a is greater than @b. return ( a > b ); } int main () { std :: array arr { 13 , 90 , 99 , 5 , 40 , 80 }; // Pass greater to std::sort std :: sort ( arr . begin (), arr . end (), greater ); for ( int i : arr ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 99 90 80 40 13 5 \u540c\u6837\uff0c\u6211\u4eec\u4e0d\u7528\u81ea\u5df1\u7f16\u5199\u5faa\u73af\uff0c\u53ea\u9700\u51e0\u884c\u4ee3\u7801\u5c31\u53ef\u4ee5\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f! \u6211\u4eec\u7684 greater \u51fd\u6570\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff0c\u4f46\u6211\u4eec\u6ca1\u6709\u4f20\u9012\u7ed9\u5b83\u4efb\u4f55\u53c2\u6570\uff0c\u90a3\u4e48\u5b83\u4eec\u4ece\u4f55\u800c\u6765\u5462?\u5f53\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u6ca1\u6709\u5706\u62ec\u53f7 () \u7684\u51fd\u6570\u65f6\uff0c\u5b83\u53ea\u662f\u4e00\u4e2a\u51fd\u6570\u6307\u9488\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u3002\u4f60\u53ef\u80fd\u8fd8\u8bb0\u5f97\uff0c\u5f53\u6211\u4eec\u8bd5\u56fe\u6253\u5370\u4e00\u4e2a\u6ca1\u6709\u62ec\u53f7\u7684\u51fd\u6570\u65f6\uff0c std::cout \u8f93\u51fa\u7684\u662f\"1\"\u3002 std::sort \u4f1a\u4f7f\u7528\u8fd9\u4e2a\u6307\u9488\u8c03\u7528\u51fd\u6570\u5e76\u5c06\u6570\u7ec4\u4e2d\u7684\u4e24\u4e2a\u5143\u7d20\u4f5c\u4e3a\u5b9e\u53c2\u3002\u6211\u4eec\u4e0d\u77e5\u9053\u54ea\u4e9b\u5143\u7d20\u4f1a\u88ab greater \u4f7f\u7528\uff0c\u8fd9\u53d6\u51b3\u4e8e\u5e95\u5c42\u7684\u6392\u5e8f\u7b97\u6cd5\u3002\u6211\u4eec\u5c06\u5728\u540e\u9762\u7684\u7ae0\u8282\u4e2d\u8be6\u7ec6\u8ba8\u8bba\u51fd\u6570\u6307\u9488\u3002 \u5c0f\u8d34\u58eb \u56e0\u4e3a\u964d\u5e8f\u6392\u5e8f\u4e5f\u975e\u5e38\u5e38\u89c1\uff0c\u6240\u4ee5C++\u63d0\u4f9b\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7c7b\u578b( std::greater )\u7528\u4e8e\u964d\u5e8f\u6392\u5e8f(\u5b83\u5b9a\u4e49\u5728 functional \u5934\u6587\u4ef6\u4e2d)\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u66ff\u6362\uff1a 1 std :: sort ( arr . begin (), arr . end (), greater ); // \u8c03\u7528\u81ea\u5b9a\u4e49\u7684 greater \u51fd\u6570 \u4e3a 1 2 3 std :: sort ( arr . begin (), arr . end (), std :: greater {}); // \u4f7f\u7528\u6807\u51c6\u5e93\u7684 greater \u6bd4\u8f83\u51fd\u6570 // \u5728 C++17 \u4e4b\u524d\uff0c\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a std::greater \u4f7f\u7528\u7684\u5143\u7d20\u7c7b\u578b std :: sort ( arr . begin (), arr . end (), std :: greater < int > {}); // \u4f7f\u7528\u6807\u51c6\u5e93\u7684 greater \u6bd4\u8f83\u51fd\u6570 \u6ce8\u610f\uff0c std::greater{} \u9700\u8981\u4f7f\u7528\u5927\u62ec\u53f7\u521d\u59cb\u5316\uff0c\u56e0\u4e3a\u5b83\u5e76\u4e0d\u662f\u4e00\u4e2a\u53ef\u8c03\u7528\u7684\u51fd\u6570\u3002\u5b83\u662f\u4e00\u4e2a\u7c7b\u578b\uff0c\u4e3a\u4e86\u4f7f\u7528\u5b83\uff0c\u6211\u4eec\u5fc5\u987b\u5c06\u5176\u5b9e\u4f8b\u5316\u4e3a\u4e00\u4e2a\u5bf9\u8c61\u3002\u82b1\u62ec\u53f7\u521d\u59cb\u5316\u518d\u8fd9\u91cc\u4f1a\u5b9e\u4f8b\u5316\u4e00\u4e2a\u533f\u540d\u5bf9\u8c61\uff08\u6700\u7ec8\u4f1a\u88ab\u4f20\u9012\u7ed9 std::sort \uff09\u3002 \u6269\u5c55\u9605\u8bfb \u8fd9\u91cc\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u4e0b std::sort \u662f\u5982\u4f55\u4f7f\u7528\u6bd4\u8f83\u51fd\u6570\u7684\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4fee\u6539\u4e00\u4e0b 11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f \u4e2d\u5b9e\u73b0\u7684\u9009\u62e9\u6392\u5e8f\u7b97\u6cd5\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include #include void sort ( int * begin , int * end ) { for ( auto startElement { begin }; startElement != end ; ++ startElement ) { auto smallestElement { startElement }; // std::next returns a pointer to the next element, just like (startElement + 1) would. for ( auto currentElement { std :: next ( startElement ) }; currentElement != end ; ++ currentElement ) { if ( * currentElement < * smallestElement ) { smallestElement = currentElement ; } } std :: swap ( * startElement , * smallestElement ); } } int main () { int array []{ 2 , 1 , 9 , 4 , 5 }; sort ( std :: begin ( array ), std :: end ( array )); for ( auto i : array ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u5230\u8fd9\u91cc\u4f4d\u7f6e\uff0c\u8fd8\u6ca1\u6709\u4efb\u4f55\u65b0\u4e1c\u897f\uff0c sort \u4e5f\u603b\u662f\u5bf9\u5143\u7d20\u8fdb\u884c\u4ece\u4f4e\u5230\u9ad8\u7684\u6392\u5e8f\u3002\u4e3a\u4e86\u6dfb\u52a0\u6bd4\u8f83\u51fd\u6570\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u65b0\u7684\u7c7b\u578b std::function \u7528\u4e8e\u4fdd\u5b58\u4e00\u4e2a2\u4e2a\u6574\u578b\u53c2\u6570\u4e14\u8fd4\u56de\u5e03\u5c14\u7c7b\u578b\u7684\u51fd\u6570\u3002\u4f60\u53ef\u4ee5\u6682\u65f6\u5c06\u5176\u7406\u89e3\u4e3a\u67d0\u79cd\u9ed1\u79d1\u6280\uff0c\u6211\u4eec\u7a0d\u540e\u4f1a\u5728 \u7b2c\u5341\u4e8c\u7ae0 \u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002 1 void sort ( int * begin , int * end , std :: function < bool ( int , int ) > compare ) \u6211\u4eec\u73b0\u5728\u53ef\u4ee5\u5c06\u4e00\u4e2a greater \u51fd\u6570\u4f20\u9012\u7ed9 sort \uff0c\u4f46\u662f sort \u53c8\u5e94\u8be5\u5982\u4f55\u4f7f\u7528\u5b83\u5462\uff1f\u6211\u4eec\u9700\u8981\u5c06\u4e0b\u9762\u4ee3\u7801 1 if ( * currentElement < * smallestElement ) \u66ff\u6362\u4e3a\uff1a 1 if ( compare ( * currentElement , * smallestElement )) \u63a5\u4e0b\u6765\uff0c sort \u7684\u8c03\u7528\u8005\u5c31\u53ef\u4ee5\u51b3\u5b9a\u5982\u4f55\u6bd4\u8f83\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include // std::function #include #include #include // sort accepts a comparison function void sort ( int * begin , int * end , std :: function < bool ( int , int ) > compare ) { for ( auto startElement { begin }; startElement != end ; ++ startElement ) { auto smallestElement { startElement }; for ( auto currentElement { std :: next ( startElement ) }; currentElement != end ; ++ currentElement ) { // the comparison function is used to check if the current element should be ordered // before the currently \"smallest\" element. if ( compare ( * currentElement , * smallestElement )) { smallestElement = currentElement ; } } std :: swap ( * startElement , * smallestElement ); } } int main () { int array []{ 2 , 1 , 9 , 4 , 5 }; // use std::greater to sort in descending order // (We have to use the global namespace selector to prevent a collision // between our sort function and std::sort.) :: sort ( std :: begin ( array ), std :: end ( array ), std :: greater {}); for ( auto i : array ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u4f7f\u7528 std::for_each \u5bf9\u5bb9\u5668\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c std::for_each \u63a5\u53d7\u4e00\u4e2a\u5217\u8868\u4f5c\u4e3a\u8f93\u5165\uff0c\u7136\u540e\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u51fd\u6570\u3002\u5f53\u6211\u4eec\u9700\u8981\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u8fdb\u884c\u76f8\u540c\u7684\u64cd\u4f5c\u65f6\u8fd9\u5c31\u975e\u5e38\u6709\u7528\uff0c \u5728\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 std::for_each \u51fd\u6570\u5c06\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u7ffb\u500d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include #include void doubleNumber ( int & i ) { i *= 2 ; } int main () { std :: array arr { 1 , 2 , 3 , 4 }; std :: for_each ( arr . begin (), arr . end (), doubleNumber ); for ( int i : arr ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 4 6 8 \u4e0a\u8ff0\u529f\u80fd\u5728\u65b0\u624b\u7a0b\u5e8f\u5458\u770b\u6765\uff0c\u4f3c\u4e4e\u6ca1\u4ec0\u4e48\u7528\uff0c\u56e0\u4e3a\u76f8\u540c\u7684\u64cd\u4f5c\u5b8c\u5168\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u6765\u5b8c\u6210\uff0c\u800c\u4e14\u4ee3\u7801\u4f1a\u66f4\u7b80\u77ed\u3002\u4f46\u662f std::for_each \u6709\u5b83\u7684\u4f18\u52bf\u3002\u8ba9\u6211\u4eec\u6bd4\u8f83\u4e00\u4e0b\u4e24\u79cd\u65b9\u5f0f\u3002 1 2 3 4 5 6 7 std :: ranges :: for_each ( arr , doubleNumber ); // \u4ece C++20\u5f00\u59cb\uff0c\u6211\u4eec\u4e0d\u5fc5\u518d\u4f7f\u7528 begin() \u548c end(). // std::for_each(arr.begin(), arr.end(), doubleNumber); // \u5728 C++20 \u4e4b\u524d\u7684\u5f62\u5f0f for ( auto & i : arr ) { doubleNumber ( i ); } \u4f7f\u7528 std::for_each \u65f6\uff0c\u7a0b\u5e8f\u7684\u76ee\u7684\u662f\u975e\u5e38\u6e05\u6670\u7684\u2014\u2014\u5bf9 arr \u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8c03\u7528 doubleNumber \u3002 \u5728for\u5faa\u73af\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5fc5\u987b\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf i \uff0c\u5728\u7a0b\u5e8f\u5458\u75b2\u60eb\u6216\u8005\u5206\u5fc3\u7684\u65f6\u5019\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5e26\u6765\u4e00\u4e9b\u95ee\u9898\u3002\u4e00\u65b9\u9762\uff0c\u5982\u679c\u6211\u4eec\u4e0d\u4f7f\u7528auto\u7684\u8bdd\uff0c\u6b64\u5904\u4f1a\u53d1\u751f \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u3002\u6211\u4eec\u4e5f\u53ef\u80fd\u4f1a\u5fd8\u8bb0\u4f7f\u7528 & \uff0c\u8fd9\u6837\u7684\u8bdd doubleNumber \u51fd\u6570\u5c06\u65e0\u6cd5\u4fee\u6539\u6570\u7ec4\u5143\u7d20\u3002\u6211\u4eec\u8fd8\u53ef\u80fd\u4f1a\u8bef\u5c06\u5176\u4ed6\u53d8\u91cf\u4f20\u9012\u7ed9 doubleNumber \u3002\u800c\u8fd9\u4e9b\u95ee\u9898\u5728\u4f7f\u7528 std::for_each \u65f6\u90fd\u4e0d\u4f1a\u53d1\u751f\u3002 \u4e0d\u4ec5\u5982\u6b64\uff0c std::for_each \u8fd8\u53ef\u4ee5\u8df3\u8fc7\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u4f8b\u5982 std::next \u5c31\u53ef\u4ee5\u5b9e\u73b0\u8be5\u529f\u80fd\u3002 1 2 std :: for_each ( std :: next ( arr . begin ()), arr . end (), doubleNumber ); // Now arr is [1, 4, 6, 8]. The first element wasn't doubled. \u8fd9\u5728 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u4e2d\u662f\u65e0\u6cd5\u5b9e\u73b0\u7684\u3002 \u548c\u5f88\u591a\u7b97\u6cd5\u4e00\u6837\uff0c std::for_each \u4e5f\u53ef\u4ee5\u901a\u8fc7\u5e76\u884c\u7684\u65b9\u5f0f\u6765\u52a0\u901f\u5904\u7406\uff0c\u8fd9\u5728\u5e94\u5bf9\u5927\u578b\u9879\u76ee\u6216\u5927\u6570\u636e\u65f6\u6548\u7387\u8981\u6bd4 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u9ad8\u5f97\u591a\u3002 \u6267\u884c\u987a\u5e8f \u6ce8\u610f\uff0c\u7b97\u6cd5\u5e93\u4e2d\u7684\u5927\u591a\u6570\u7b97\u6cd5\u90fd\u4e0d\u4fdd\u8bc1\u7279\u5b9a\u7684\u6267\u884c\u987a\u5e8f\u3002\u5bf9\u4e8e\u8fd9\u6837\u7684\u7b97\u6cd5\uff0c\u8bf7\u6ce8\u610f\u786e\u4fdd\u4f20\u5165\u7684\u4efb\u4f55\u51fd\u6570\u90fd\u4e0d\u4f9d\u8d56\u67d0\u79cd\u987a\u5e8f\u6267\u884c\uff0c\u56e0\u4e3a\u5176\u5728\u6bcf\u4e2a\u7f16\u8bd1\u5668\u4e0a\u8c03\u7528\u7684\u987a\u5e8f\u53ef\u80fd\u4e0d\u76f8\u540c\u3002 \u4e0b\u9762\u8fd9\u4e9b\u51fd\u6570\u662f\u53ef\u4ee5\u4fdd\u8bc1\u6267\u884c\u987a\u5e8f\u7684\uff1a std::for_each \u3001 std::copy \u3001 std::copy_backward \u3001 std::move \u548c std::move_backward \u3002 \u6700\u4f73\u5b9e\u8df5 \u9664\u975e\u6709\u7279\u6b8a\u8bf4\u660e\uff0c\u5426\u5219\u4e0d\u8981\u5047\u8bbe\u6807\u51c6\u5e93\u4e2d\u7684\u7b97\u6cd5\u5177\u6709\u7279\u5b9a\u7684\u6267\u884c\u987a\u5e8f\u3002 std::for_each , std::copy , std::copy_backward , std::move , and std::move_backward \u662f\u987a\u5e8f\u6267\u884c\u7684\u3002 C++20 \u4e2d\u7684\u8303\u56f4 \u5728\u4f7f\u7528\u7b97\u6cd5\u65f6\u603b\u662f\u8981\u4f20\u5165 arr.begin() \u548c arr.end() \u5176\u5b9e\u633a\u70e6\u4eba\u7684\u3002\u4e0d\u8fc7\u5728C++20\u4e2d\uff0c\u5c31\u4e0d\u9700\u8981\u8fd9\u6837\u505a\u4e86\uff0c\u76f4\u63a5\u4f7f\u7528 arr \u5373\u53ef\u3002\u8fd9\u65e0\u7591\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002 \u7ed3\u8bba \u7b97\u6cd5\u5e93\u6709\u5927\u91cf\u6709\u7528\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u4f60\u7684\u4ee3\u7801\u66f4\u7b80\u5355\u3001\u66f4\u5065\u58ee\u3002\u5728\u8fd9\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u53ea\u6d89\u53ca\u4e00\u4e2a\u5f88\u5c0f\u7684\u5b50\u96c6\uff0c\u4f46\u662f\u56e0\u4e3a\u8fd9\u4e9b\u51fd\u6570\u4e2d\u7684\u5927\u591a\u6570\u5de5\u4f5c\u65b9\u5f0f\u975e\u5e38\u76f8\u4f3c\uff0c\u4e00\u65e6\u4f60\u77e5\u9053\u4e86\u5176\u4e2d\u4e00\u4e9b\u51fd\u6570\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u4f60\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u4e2d\u7684\u5927\u591a\u6570\u3002 \u6700\u4f73\u5b9e\u8df5 \u5efa\u8bae\u4f7f\u7528\u7b97\u6cd5\u5e93\u4e2d\u7684\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u7f16\u5199\u81ea\u5df1\u7684\u51fd\u6570\u6765\u5b8c\u6210\u76f8\u540c\u7684\u4efb\u52a1","title":"11.19 \u2014 \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb"},{"location":"11-19-Introduction-to-standard-library-algorithms/#stdfind","text":"std::find \u51fd\u6570\u7528\u4e8e\u67e5\u627e\u67d0\u4e2a\u503c\u5728\u5143\u7d20\u4e2d\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\u3002 std::find \u6709\u4e09\u4e2a\u53c2\u6570\uff1a\u5e8f\u5217\u4e2d\u8d77\u59cb\u5143\u7d20\u7684\u8fed\u4ee3\u5668\u3001\u7ec8\u70b9\u5143\u7d20\u7684\u8fed\u4ee3\u5668\u3001\u9700\u8981\u641c\u7d22\u7684\u503c\u3002\u8be5\u51fd\u6570\u4f1a\u8fd4\u56de\u6307\u5411\u76ee\u6807\u5143\u7d20\u7684\u8fed\u4ee3\u5668\uff08\u5982\u679c\u627e\u5230\u7684\u8bdd\uff09\uff0c\u6216\u8005\u6307\u5411\u5bb9\u5668\u7684\u672b\u5c3e\uff08\u5982\u679c\u6ca1\u627e\u7684\u8bdd\uff09\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include #include int main () { std :: array arr { 13 , 90 , 99 , 5 , 40 , 80 }; std :: cout << \"Enter a value to search for and replace with: \" ; int search {}; int replace {}; std :: cin >> search >> replace ; // \u5ffd\u7565\u8f93\u5165\u5408\u6cd5\u6027\u68c0\u67e5 // std::find \u8fd4\u56de\u6307\u5411\u627e\u5230\u5143\u7d20\u7684\u8fed\u4ee3\u5668\uff08\u6216\u8005\u662f\u6307\u5411\u5bb9\u5668\u672b\u5c3e\uff09 // \u6211\u4eec\u5c06\u8be5\u5143\u7d20\u5b58\u653e\u5230\u4e00\u4e2a\u53d8\u91cf\u4e2d\uff0c\u901a\u8fc7\u7c7b\u578b\u63a8\u65ad\u6765\u83b7\u53d6\u8fed\u4ee3\u5668\u7684\u7c7b\u578b auto found { std :: find ( arr . begin (), arr . end (), search ) }; // \u6ca1\u6709\u627e\u5230\u5219\u8fd4\u56de\u6307\u5411\u672b\u5c3e\u7684\u8fed\u4ee3\u5668 // \u53ef\u4ee5\u4f7f\u7528end()\u51fd\u6570\u83b7\u53d6\u672b\u5c3e\u5e76\u548c\u8fed\u4ee3\u5668\u8fdb\u884c\u6bd4\u8f83 if ( found == arr . end ()) { std :: cout << \"Could not find \" << search << '\\n' ; } else { // \u66ff\u6362 * found = replace ; } for ( int i : arr ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u5143\u7d20\u5b58\u5728\u7684\u4f8b\u5b50\uff1a 1 2 Enter a value to search for and replace with: 5 234 13 90 99 234 40 80 \u5143\u7d20\u4e0d\u5b58\u5728\u7684\u4f8b\u5b50\uff1a 1 2 3 Enter a value to search for and replace with: 0 234 Could not find 0 13 90 99 5 40 80","title":"\u4f7f\u7528 std::find \u67e5\u627e\u7279\u5b9a\u503c\u7684\u5143\u7d20"},{"location":"11-19-Introduction-to-standard-library-algorithms/#stdfind_if","text":"\u6709\u7684\u65f6\u5019\u6211\u4eec\u9700\u8981\u5728\u5bb9\u5668\u4e2d\u67e5\u627e\u662f\u5426\u6709\u4e00\u4e2a\u6ee1\u8db3\u67d0\u79cd\u6761\u4ef6\u7684\u503c\uff08\u4f8b\u5982\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u67d0\u4e2a\u7279\u5b9a\u7684\u5b50\u4e32\uff09\uff0c\u800c\u4e0d\u662f\u67d0\u4e2a\u5177\u4f53\u7684\u503c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c std::find_if \u5f88\u597d\u7528\u3002 std::find_if \u51fd\u6570\u7684\u5de5\u4f5c\u65b9\u5f0f\u548c std::find \u5f88\u7c7b\u4f3c\uff0c\u4f46\u662f\u6211\u4eec\u4e0d\u9700\u8981\u4f20\u9012\u4e00\u4e2a\u5177\u4f53\u7684\u503c\uff0c\u800c\u662f\u4f20\u9012\u4e00\u4e2a \u53ef\u8c03\u7528\u5bf9\u8c61 \uff0c\u4f8b\u5982\u51fd\u6570\u6307\u9488\uff08\u6216\u8005 lambda\u8868\u8fbe\u5f0f \uff09\u3002 \u5f53\u5143\u7d20\u88ab\u4f9d\u6b21\u904d\u5386\u65f6\uff0c std::find_if \u4f1a\u8c03\u7528\u51fd\u6570\uff08\u901a\u8fc7\u5b9e\u53c2\u4f20\u5165\u7684\u51fd\u6570\uff09\uff0c\u800c\u8be5\u51fd\u6570\u4f1a\u8fd4\u56de true \u6216 false \u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86 std::find_if \u6765\u67e5\u627e\u5305\u542b nut \u5b50\u4e32\u7684\u5143\u7d20\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include #include #include // Our function will return true if the element matches bool containsNut ( std :: string_view str ) { // std::string_view::find returns std::string_view::npos if it doesn't find // the substring. Otherwise it returns the index where the substring occurs // in str. return ( str . find ( \"nut\" ) != std :: string_view :: npos ); } int main () { std :: array < std :: string_view , 4 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" }; // Scan our array to see if any elements contain the \"nut\" substring auto found { std :: find_if ( arr . begin (), arr . end (), containsNut ) }; if ( found == arr . end ()) { std :: cout << \"No nuts \\n \" ; } else { std :: cout << \"Found \" << * found << '\\n' ; } return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Found walnut \u5982\u679c\u6211\u4eec\u81ea\u5df1\u7f16\u5199\u4e0a\u9762\u7684\u793a\u4f8b\uff0c\u5219\u81f3\u5c11\u9700\u8981\u4e09\u4e2a\u5faa\u73af(\u4e00\u4e2a\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u4e24\u4e2a\u5faa\u73af\u5339\u914d\u5b50\u5b57\u7b26\u4e32)\u3002\u6807\u51c6\u5e93\u51fd\u6570\u5141\u8bb8\u6211\u4eec\u5728\u77ed\u77ed\u51e0\u884c\u4ee3\u7801\u4e2d\u5b8c\u6210\u76f8\u540c\u7684\u4e8b\u60c5\uff01","title":"\u4f7f\u7528 std::find_if \u67e5\u627e\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20"},{"location":"11-19-Introduction-to-standard-library-algorithms/#stdcount-stdcount_if","text":"std::count \u548c std::count_if \u7528\u4e8e\u641c\u7d22\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u5e76\u5bf9\u5176\u51fa\u73b0\u6b21\u6570\u8fdb\u884c\u7edf\u8ba1\u3002 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u7edf\u8ba1\u6709\u591a\u5c11\u4e2a\u5143\u7d20\u5305\u542b\"nut\"\u5b50\u4e32\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include #include #include bool containsNut ( std :: string_view str ) { return ( str . find ( \"nut\" ) != std :: string_view :: npos ); } int main () { std :: array < std :: string_view , 5 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" , \"peanut\" }; auto nuts { std :: count_if ( arr . begin (), arr . end (), containsNut ) }; std :: cout << \"Counted \" << nuts << \" nut(s) \\n \" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Counted 2 nut(s)","title":"\u4f7f\u7528 std::count \u548c std::count_if \u8fdb\u884c\u8ba1\u6570"},{"location":"11-19-Introduction-to-standard-library-algorithms/#stdsort","text":"\u6211\u4eec\u66fe\u4f7f\u7528 std::sort \u5bf9\u6570\u7ec4\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\uff0c\u4f46\u5b9e\u9645\u4e0a std::sort \u8fd8\u53ef\u4ee5\u505a\u5f88\u591a\u4e8b\u3002\u4e00\u4e2a\u7248\u672c\u7684 std::sort \u53ef\u4ee5\u63a5\u53d7\u7b2c\u4e09\u4e2a\u53c2\u6570\uff0c\u4f7f\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u51fd\u6570\u5b9a\u4e49\u5982\u4f55\u6392\u5e8f\u3002\u8be5\u51fd\u6570\u9700\u8981\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\u7528\u4e8e\u6bd4\u8f83\uff0c\u5982\u679c\u7b2c\u4e00\u4e2a\u6570\u5e94\u8be5\u88ab\u6392\u5728\u7b2c\u4e8c\u4e2a\u6570\u4e4b\u524d\uff0c\u5e94\u8be5\u8fd4\u56de true \u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c std::sort \u4f1a\u6309\u7167\u5347\u5e8f\u8fdb\u884c\u6392\u5e8f\u3002 \u4e0b\u9762\uff0c\u6211\u4eec\u4f7f\u7528 std::sort \u5bf9\u6570\u7ec4\u8fdb\u884c\u964d\u5e8f\u6392\u5e8f\uff08\u901a\u8fc7\u81ea\u5b9a\u4e49\u6bd4\u8f83\u51fd\u6570 greater \u5b9e\u73b0\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include #include bool greater ( int a , int b ) { // Order @a before @b if @a is greater than @b. return ( a > b ); } int main () { std :: array arr { 13 , 90 , 99 , 5 , 40 , 80 }; // Pass greater to std::sort std :: sort ( arr . begin (), arr . end (), greater ); for ( int i : arr ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 99 90 80 40 13 5 \u540c\u6837\uff0c\u6211\u4eec\u4e0d\u7528\u81ea\u5df1\u7f16\u5199\u5faa\u73af\uff0c\u53ea\u9700\u51e0\u884c\u4ee3\u7801\u5c31\u53ef\u4ee5\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f! \u6211\u4eec\u7684 greater \u51fd\u6570\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff0c\u4f46\u6211\u4eec\u6ca1\u6709\u4f20\u9012\u7ed9\u5b83\u4efb\u4f55\u53c2\u6570\uff0c\u90a3\u4e48\u5b83\u4eec\u4ece\u4f55\u800c\u6765\u5462?\u5f53\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u6ca1\u6709\u5706\u62ec\u53f7 () \u7684\u51fd\u6570\u65f6\uff0c\u5b83\u53ea\u662f\u4e00\u4e2a\u51fd\u6570\u6307\u9488\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u3002\u4f60\u53ef\u80fd\u8fd8\u8bb0\u5f97\uff0c\u5f53\u6211\u4eec\u8bd5\u56fe\u6253\u5370\u4e00\u4e2a\u6ca1\u6709\u62ec\u53f7\u7684\u51fd\u6570\u65f6\uff0c std::cout \u8f93\u51fa\u7684\u662f\"1\"\u3002 std::sort \u4f1a\u4f7f\u7528\u8fd9\u4e2a\u6307\u9488\u8c03\u7528\u51fd\u6570\u5e76\u5c06\u6570\u7ec4\u4e2d\u7684\u4e24\u4e2a\u5143\u7d20\u4f5c\u4e3a\u5b9e\u53c2\u3002\u6211\u4eec\u4e0d\u77e5\u9053\u54ea\u4e9b\u5143\u7d20\u4f1a\u88ab greater \u4f7f\u7528\uff0c\u8fd9\u53d6\u51b3\u4e8e\u5e95\u5c42\u7684\u6392\u5e8f\u7b97\u6cd5\u3002\u6211\u4eec\u5c06\u5728\u540e\u9762\u7684\u7ae0\u8282\u4e2d\u8be6\u7ec6\u8ba8\u8bba\u51fd\u6570\u6307\u9488\u3002 \u5c0f\u8d34\u58eb \u56e0\u4e3a\u964d\u5e8f\u6392\u5e8f\u4e5f\u975e\u5e38\u5e38\u89c1\uff0c\u6240\u4ee5C++\u63d0\u4f9b\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7c7b\u578b( std::greater )\u7528\u4e8e\u964d\u5e8f\u6392\u5e8f(\u5b83\u5b9a\u4e49\u5728 functional \u5934\u6587\u4ef6\u4e2d)\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u66ff\u6362\uff1a 1 std :: sort ( arr . begin (), arr . end (), greater ); // \u8c03\u7528\u81ea\u5b9a\u4e49\u7684 greater \u51fd\u6570 \u4e3a 1 2 3 std :: sort ( arr . begin (), arr . end (), std :: greater {}); // \u4f7f\u7528\u6807\u51c6\u5e93\u7684 greater \u6bd4\u8f83\u51fd\u6570 // \u5728 C++17 \u4e4b\u524d\uff0c\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a std::greater \u4f7f\u7528\u7684\u5143\u7d20\u7c7b\u578b std :: sort ( arr . begin (), arr . end (), std :: greater < int > {}); // \u4f7f\u7528\u6807\u51c6\u5e93\u7684 greater \u6bd4\u8f83\u51fd\u6570 \u6ce8\u610f\uff0c std::greater{} \u9700\u8981\u4f7f\u7528\u5927\u62ec\u53f7\u521d\u59cb\u5316\uff0c\u56e0\u4e3a\u5b83\u5e76\u4e0d\u662f\u4e00\u4e2a\u53ef\u8c03\u7528\u7684\u51fd\u6570\u3002\u5b83\u662f\u4e00\u4e2a\u7c7b\u578b\uff0c\u4e3a\u4e86\u4f7f\u7528\u5b83\uff0c\u6211\u4eec\u5fc5\u987b\u5c06\u5176\u5b9e\u4f8b\u5316\u4e3a\u4e00\u4e2a\u5bf9\u8c61\u3002\u82b1\u62ec\u53f7\u521d\u59cb\u5316\u518d\u8fd9\u91cc\u4f1a\u5b9e\u4f8b\u5316\u4e00\u4e2a\u533f\u540d\u5bf9\u8c61\uff08\u6700\u7ec8\u4f1a\u88ab\u4f20\u9012\u7ed9 std::sort \uff09\u3002 \u6269\u5c55\u9605\u8bfb \u8fd9\u91cc\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u4e0b std::sort \u662f\u5982\u4f55\u4f7f\u7528\u6bd4\u8f83\u51fd\u6570\u7684\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4fee\u6539\u4e00\u4e0b 11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f \u4e2d\u5b9e\u73b0\u7684\u9009\u62e9\u6392\u5e8f\u7b97\u6cd5\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include #include void sort ( int * begin , int * end ) { for ( auto startElement { begin }; startElement != end ; ++ startElement ) { auto smallestElement { startElement }; // std::next returns a pointer to the next element, just like (startElement + 1) would. for ( auto currentElement { std :: next ( startElement ) }; currentElement != end ; ++ currentElement ) { if ( * currentElement < * smallestElement ) { smallestElement = currentElement ; } } std :: swap ( * startElement , * smallestElement ); } } int main () { int array []{ 2 , 1 , 9 , 4 , 5 }; sort ( std :: begin ( array ), std :: end ( array )); for ( auto i : array ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u5230\u8fd9\u91cc\u4f4d\u7f6e\uff0c\u8fd8\u6ca1\u6709\u4efb\u4f55\u65b0\u4e1c\u897f\uff0c sort \u4e5f\u603b\u662f\u5bf9\u5143\u7d20\u8fdb\u884c\u4ece\u4f4e\u5230\u9ad8\u7684\u6392\u5e8f\u3002\u4e3a\u4e86\u6dfb\u52a0\u6bd4\u8f83\u51fd\u6570\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u65b0\u7684\u7c7b\u578b std::function \u7528\u4e8e\u4fdd\u5b58\u4e00\u4e2a2\u4e2a\u6574\u578b\u53c2\u6570\u4e14\u8fd4\u56de\u5e03\u5c14\u7c7b\u578b\u7684\u51fd\u6570\u3002\u4f60\u53ef\u4ee5\u6682\u65f6\u5c06\u5176\u7406\u89e3\u4e3a\u67d0\u79cd\u9ed1\u79d1\u6280\uff0c\u6211\u4eec\u7a0d\u540e\u4f1a\u5728 \u7b2c\u5341\u4e8c\u7ae0 \u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002 1 void sort ( int * begin , int * end , std :: function < bool ( int , int ) > compare ) \u6211\u4eec\u73b0\u5728\u53ef\u4ee5\u5c06\u4e00\u4e2a greater \u51fd\u6570\u4f20\u9012\u7ed9 sort \uff0c\u4f46\u662f sort \u53c8\u5e94\u8be5\u5982\u4f55\u4f7f\u7528\u5b83\u5462\uff1f\u6211\u4eec\u9700\u8981\u5c06\u4e0b\u9762\u4ee3\u7801 1 if ( * currentElement < * smallestElement ) \u66ff\u6362\u4e3a\uff1a 1 if ( compare ( * currentElement , * smallestElement )) \u63a5\u4e0b\u6765\uff0c sort \u7684\u8c03\u7528\u8005\u5c31\u53ef\u4ee5\u51b3\u5b9a\u5982\u4f55\u6bd4\u8f83\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include // std::function #include #include #include // sort accepts a comparison function void sort ( int * begin , int * end , std :: function < bool ( int , int ) > compare ) { for ( auto startElement { begin }; startElement != end ; ++ startElement ) { auto smallestElement { startElement }; for ( auto currentElement { std :: next ( startElement ) }; currentElement != end ; ++ currentElement ) { // the comparison function is used to check if the current element should be ordered // before the currently \"smallest\" element. if ( compare ( * currentElement , * smallestElement )) { smallestElement = currentElement ; } } std :: swap ( * startElement , * smallestElement ); } } int main () { int array []{ 2 , 1 , 9 , 4 , 5 }; // use std::greater to sort in descending order // (We have to use the global namespace selector to prevent a collision // between our sort function and std::sort.) :: sort ( std :: begin ( array ), std :: end ( array ), std :: greater {}); for ( auto i : array ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; }","title":"\u4f7f\u7528 std::sort \u8fdb\u884c\u81ea\u5b9a\u4e49\u6392\u5e8f"},{"location":"11-19-Introduction-to-standard-library-algorithms/#stdfor_each","text":"std::for_each \u63a5\u53d7\u4e00\u4e2a\u5217\u8868\u4f5c\u4e3a\u8f93\u5165\uff0c\u7136\u540e\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u51fd\u6570\u3002\u5f53\u6211\u4eec\u9700\u8981\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u8fdb\u884c\u76f8\u540c\u7684\u64cd\u4f5c\u65f6\u8fd9\u5c31\u975e\u5e38\u6709\u7528\uff0c \u5728\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 std::for_each \u51fd\u6570\u5c06\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u7ffb\u500d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include #include void doubleNumber ( int & i ) { i *= 2 ; } int main () { std :: array arr { 1 , 2 , 3 , 4 }; std :: for_each ( arr . begin (), arr . end (), doubleNumber ); for ( int i : arr ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 4 6 8 \u4e0a\u8ff0\u529f\u80fd\u5728\u65b0\u624b\u7a0b\u5e8f\u5458\u770b\u6765\uff0c\u4f3c\u4e4e\u6ca1\u4ec0\u4e48\u7528\uff0c\u56e0\u4e3a\u76f8\u540c\u7684\u64cd\u4f5c\u5b8c\u5168\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u6765\u5b8c\u6210\uff0c\u800c\u4e14\u4ee3\u7801\u4f1a\u66f4\u7b80\u77ed\u3002\u4f46\u662f std::for_each \u6709\u5b83\u7684\u4f18\u52bf\u3002\u8ba9\u6211\u4eec\u6bd4\u8f83\u4e00\u4e0b\u4e24\u79cd\u65b9\u5f0f\u3002 1 2 3 4 5 6 7 std :: ranges :: for_each ( arr , doubleNumber ); // \u4ece C++20\u5f00\u59cb\uff0c\u6211\u4eec\u4e0d\u5fc5\u518d\u4f7f\u7528 begin() \u548c end(). // std::for_each(arr.begin(), arr.end(), doubleNumber); // \u5728 C++20 \u4e4b\u524d\u7684\u5f62\u5f0f for ( auto & i : arr ) { doubleNumber ( i ); } \u4f7f\u7528 std::for_each \u65f6\uff0c\u7a0b\u5e8f\u7684\u76ee\u7684\u662f\u975e\u5e38\u6e05\u6670\u7684\u2014\u2014\u5bf9 arr \u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8c03\u7528 doubleNumber \u3002 \u5728for\u5faa\u73af\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5fc5\u987b\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf i \uff0c\u5728\u7a0b\u5e8f\u5458\u75b2\u60eb\u6216\u8005\u5206\u5fc3\u7684\u65f6\u5019\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5e26\u6765\u4e00\u4e9b\u95ee\u9898\u3002\u4e00\u65b9\u9762\uff0c\u5982\u679c\u6211\u4eec\u4e0d\u4f7f\u7528auto\u7684\u8bdd\uff0c\u6b64\u5904\u4f1a\u53d1\u751f \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u3002\u6211\u4eec\u4e5f\u53ef\u80fd\u4f1a\u5fd8\u8bb0\u4f7f\u7528 & \uff0c\u8fd9\u6837\u7684\u8bdd doubleNumber \u51fd\u6570\u5c06\u65e0\u6cd5\u4fee\u6539\u6570\u7ec4\u5143\u7d20\u3002\u6211\u4eec\u8fd8\u53ef\u80fd\u4f1a\u8bef\u5c06\u5176\u4ed6\u53d8\u91cf\u4f20\u9012\u7ed9 doubleNumber \u3002\u800c\u8fd9\u4e9b\u95ee\u9898\u5728\u4f7f\u7528 std::for_each \u65f6\u90fd\u4e0d\u4f1a\u53d1\u751f\u3002 \u4e0d\u4ec5\u5982\u6b64\uff0c std::for_each \u8fd8\u53ef\u4ee5\u8df3\u8fc7\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u4f8b\u5982 std::next \u5c31\u53ef\u4ee5\u5b9e\u73b0\u8be5\u529f\u80fd\u3002 1 2 std :: for_each ( std :: next ( arr . begin ()), arr . end (), doubleNumber ); // Now arr is [1, 4, 6, 8]. The first element wasn't doubled. \u8fd9\u5728 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u4e2d\u662f\u65e0\u6cd5\u5b9e\u73b0\u7684\u3002 \u548c\u5f88\u591a\u7b97\u6cd5\u4e00\u6837\uff0c std::for_each \u4e5f\u53ef\u4ee5\u901a\u8fc7\u5e76\u884c\u7684\u65b9\u5f0f\u6765\u52a0\u901f\u5904\u7406\uff0c\u8fd9\u5728\u5e94\u5bf9\u5927\u578b\u9879\u76ee\u6216\u5927\u6570\u636e\u65f6\u6548\u7387\u8981\u6bd4 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u9ad8\u5f97\u591a\u3002","title":"\u4f7f\u7528 std::for_each \u5bf9\u5bb9\u5668\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c"},{"location":"11-19-Introduction-to-standard-library-algorithms/#_1","text":"\u6ce8\u610f\uff0c\u7b97\u6cd5\u5e93\u4e2d\u7684\u5927\u591a\u6570\u7b97\u6cd5\u90fd\u4e0d\u4fdd\u8bc1\u7279\u5b9a\u7684\u6267\u884c\u987a\u5e8f\u3002\u5bf9\u4e8e\u8fd9\u6837\u7684\u7b97\u6cd5\uff0c\u8bf7\u6ce8\u610f\u786e\u4fdd\u4f20\u5165\u7684\u4efb\u4f55\u51fd\u6570\u90fd\u4e0d\u4f9d\u8d56\u67d0\u79cd\u987a\u5e8f\u6267\u884c\uff0c\u56e0\u4e3a\u5176\u5728\u6bcf\u4e2a\u7f16\u8bd1\u5668\u4e0a\u8c03\u7528\u7684\u987a\u5e8f\u53ef\u80fd\u4e0d\u76f8\u540c\u3002 \u4e0b\u9762\u8fd9\u4e9b\u51fd\u6570\u662f\u53ef\u4ee5\u4fdd\u8bc1\u6267\u884c\u987a\u5e8f\u7684\uff1a std::for_each \u3001 std::copy \u3001 std::copy_backward \u3001 std::move \u548c std::move_backward \u3002 \u6700\u4f73\u5b9e\u8df5 \u9664\u975e\u6709\u7279\u6b8a\u8bf4\u660e\uff0c\u5426\u5219\u4e0d\u8981\u5047\u8bbe\u6807\u51c6\u5e93\u4e2d\u7684\u7b97\u6cd5\u5177\u6709\u7279\u5b9a\u7684\u6267\u884c\u987a\u5e8f\u3002 std::for_each , std::copy , std::copy_backward , std::move , and std::move_backward \u662f\u987a\u5e8f\u6267\u884c\u7684\u3002","title":"\u6267\u884c\u987a\u5e8f"},{"location":"11-19-Introduction-to-standard-library-algorithms/#c20","text":"\u5728\u4f7f\u7528\u7b97\u6cd5\u65f6\u603b\u662f\u8981\u4f20\u5165 arr.begin() \u548c arr.end() \u5176\u5b9e\u633a\u70e6\u4eba\u7684\u3002\u4e0d\u8fc7\u5728C++20\u4e2d\uff0c\u5c31\u4e0d\u9700\u8981\u8fd9\u6837\u505a\u4e86\uff0c\u76f4\u63a5\u4f7f\u7528 arr \u5373\u53ef\u3002\u8fd9\u65e0\u7591\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002","title":"C++20 \u4e2d\u7684\u8303\u56f4"},{"location":"11-19-Introduction-to-standard-library-algorithms/#_2","text":"\u7b97\u6cd5\u5e93\u6709\u5927\u91cf\u6709\u7528\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u4f60\u7684\u4ee3\u7801\u66f4\u7b80\u5355\u3001\u66f4\u5065\u58ee\u3002\u5728\u8fd9\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u53ea\u6d89\u53ca\u4e00\u4e2a\u5f88\u5c0f\u7684\u5b50\u96c6\uff0c\u4f46\u662f\u56e0\u4e3a\u8fd9\u4e9b\u51fd\u6570\u4e2d\u7684\u5927\u591a\u6570\u5de5\u4f5c\u65b9\u5f0f\u975e\u5e38\u76f8\u4f3c\uff0c\u4e00\u65e6\u4f60\u77e5\u9053\u4e86\u5176\u4e2d\u4e00\u4e9b\u51fd\u6570\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u4f60\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u4e2d\u7684\u5927\u591a\u6570\u3002 \u6700\u4f73\u5b9e\u8df5 \u5efa\u8bae\u4f7f\u7528\u7b97\u6cd5\u5e93\u4e2d\u7684\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u7f16\u5199\u81ea\u5df1\u7684\u51fd\u6570\u6765\u5b8c\u6210\u76f8\u540c\u7684\u4efb\u52a1","title":"\u7ed3\u8bba"},{"location":"11-2-Arrays-Part-II/","tags":["array","C++17","C++20"],"text":"Key Takeaway \u4f7f\u7528 list-initialization \u65b9\u5f0f\u521d\u59cb\u5316\u6570\u7ec4 int prime[5]{ 2,3, 5,7, 11 }; \u521d\u59cb\u5316\u5217\u8868\u4e2d\u7684\u5143\u7d20\u4e2a\u6570\u8d85\u8fc7\u6570\u7ec4\u80fd\u5bb9\u7eb3\u7684\u4e2a\u6570\u65f6\u4f1a\u62a5\u9519\uff0c\u5c11\u4e8e\u65f6\u5269\u4e0b\u7684\u4f1a\u88ab\u521d\u59cb\u5316\u4e3a 0 \u521d\u59cb\u5316\u4e00\u4e2a\u5168 0 \u6570\u7ec4\uff1a int prime[5]{}; \u5982\u679c\u7701\u7565\u521d\u59cb\u5316\u5217\u8868\uff0c\u5219\u6570\u7ec4\u5143\u7d20\u4e3a\u672a\u521d\u59cb\u5316\u72b6\u6001\uff0c\u9664\u975e\u5b83\u4eec\u662f class \u7c7b\u578b\u7684\u3002 \u4e0d\u8bba\u6570\u7ec4\u5143\u7d20\u53ef\u4ee5\u5728\u6ca1\u6709\u521d\u59cb\u5316\u5217\u8868\u65f6\u4f1a\u4e0d\u4f1a\u88ab\u9ed8\u8ba4\u521d\u59cb\u5316\uff0c\u90fd\u5e94\u5f53\u663e\u5f0f\u5730\u521d\u59cb\u5316\u6570\u7ec4\u3002 \u4f7f\u7528\u5217\u8868\u521d\u59cb\u5316\u5bf9\u6570\u7ec4\u521d\u59cb\u5316\u65f6\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u81ea\u52a8\u8ba1\u7b97\u6570\u7ec4\u957f\u5ea6\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u58f0\u660e\u6570\u7ec4\u7684\u957f\u5ea6\u3002 \u53ef\u4ee5\u6dfb\u52a0\u4e86\u4e00\u4e2a\u540d\u4e3a max_XXX \u7684\u989d\u5916\u679a\u4e3e\u503c\u3002\u6b64\u679a\u4e3e\u503c\u5728\u6570\u7ec4\u58f0\u660e\u671f\u95f4\u4f7f\u7528\uff0c\u4ee5\u786e\u4fdd\u6570\u7ec4\u5177\u6709\u9002\u5f53\u7684\u957f\u5ea6(\u56e0\u4e3a\u6570\u7ec4\u957f\u5ea6\u5e94\u8be5\u6bd4\u6700\u5927\u7d22\u5f15\u5927 1)\u3002\u8fd9\u5bf9\u4e8e\u4ee3\u7801\u53ef\u8bfb\u6027\u975e\u5e38\u91cd\u8981\uff0c\u5f53\u6211\u4eec\u6dfb\u52a0\u65b0\u7684\u679a\u4e3e\u503c\u65f6\uff0c\u6570\u7ec4\u5c06\u81ea\u52a8\u8c03\u6574\u5927\u5c0f\uff1a \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e \uff08\u679a\u4e3e\u7c7b\uff09\u5e76\u4e0d\u80fd\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u6574\u578b \u4f7f\u7528\u666e\u901a\u679a\u4e3e\u7c7b\u578b\u5e76\u5c06\u5176\u653e\u7f6e\u5728\u67d0\u4e2a\u4f5c\u7528\u57df\u4e2d\uff0c\u6bd4\u4f7f\u7528\u679a\u4e3e\u7c7b\u66f4\u597d\u3002 \u6570\u7ec4\u662f\u4f20\u5730\u5740\u4e0d\u662f\u4f20\u503c\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u60f3\u786e\u4fdd\u4e00\u4e2a\u51fd\u6570\u4e0d\u4f1a\u4fee\u6539\u4f20\u9012\u7ed9\u5b83\u7684\u6570\u7ec4\u5143\u7d20\uff0c\u53ef\u4ee5\u5c06\u6570\u7ec4\u8bbe\u7f6e\u4e3a const \u5934\u6587\u4ef6\u4e2d\u7684 std: size() \u51fd\u6570\u53ef\u4ee5\u7528\u6765\u5224\u65ad\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u4f46\u4e0d\u80fd\u7528\u4e8e\u6570\u7ec4\u88ab\u5f53\u505a\u53c2\u6570\u4f20\u9012\u7684\u60c5\u5f62\uff08C++17\uff09 sizeof \u64cd\u4f5c\u7b26\u53ef\u4ee5\u7528\u4e8e\u6570\u7ec4\uff0c\u5b83\u4f1a\u8fd4\u56de \u6570\u7ec4\u7684\u5927\u5c0f \uff08\u6570\u7ec4\u957f\u5ea6\u4e58\u4ee5\u6570\u7ec4\u5143\u7d20\u7684\u5927\u5c0f\uff09\uff0c\u4f7f\u7528\u6570\u7ec4\u5927\u5c0f\u9664\u4ee5\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u5927\u5c0f\u53ef\u4ee5\u5f97\u5230\u6570\u7ec4\u957f\u5ea6 \u5c06 sizeof \u5e94\u7528\u4e8e\u88ab\u4f20\u5165\u51fd\u6570\u7684\u6570\u7ec4\u65f6\uff0c\u4e0d\u4f1a\u50cf std:: size() \u4e00\u6837\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\uff0c\u53d6\u800c\u4ee3\u4e4b\u7684\u662f\uff0c\u5b83\u4f1a\u8fd4\u56de \u6307\u9488\u7684\u957f\u5ea6 \u3002 \u4e66\u63a5\u4e0a\u6587\uff08 11.1 - \u6570\u7ec4\uff08\u7b2c\u4e00\u90e8\u5206\uff09 \uff09\u3002 \u56fa\u5b9a\u6570\u7ec4\u521d\u59cb\u5316 \u6570\u7ec4\u5143\u7d20\u4f1a\u88ab\u5f53\u505a\u666e\u901a\u53d8\u91cf\u6765\u5bf9\u5f85\uff0c\u56e0\u6b64\uff0c\u5b83\u4eec\u5728\u521b\u5efa\u65f6\u9ed8\u8ba4\u662f\u4e0d\u521d\u59cb\u5316\u7684\u3002 \u201c\u521d\u59cb\u5316\u201d\u6570\u7ec4\u7684\u65b9\u5f0f\u4e4b\u4e00\uff0c\u662f\u9010\u4e2a\u5143\u7d20\u8fdb\u884c\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 int prime [ 5 ]; // hold the first 5 prime numbers prime [ 0 ] = 2 ; prime [ 1 ] = 3 ; prime [ 2 ] = 5 ; prime [ 3 ] = 7 ; prime [ 4 ] = 11 ; \u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u5f88\u9ebb\u70e6\uff0c\u51e0\u4e4e\u4e0d\u53ef\u80fd\u7528\u6765\u521d\u59cb\u5316\u975e\u5e38\u957f\u7684\u6570\u7ec4\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u4e0e\u5176\u8bf4\u8fd9\u662f\u5728\u521d\u59cb\u5316\uff0c\u5012\u4e0d\u5982\u8bf4\u662f\u5728\u8d4b\u503c\u3002\u5982\u679c\u6570\u7ec4\u662f const \u7c7b\u578b\u7684\u8bdd\uff0c\u8d4b\u503c\u662f\u884c\u4e0d\u901a\u7684\u3002 \u5e78\u597d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 \u521d\u59cb\u5316\u503c\u5217\u8868(initializer list) \u6765\u521d\u59cb\u5316\u6574\u4e2a\u6570\u7ec4\u3002\u4e0b\u9762\u4f8b\u5b50\u4e2d\u521d\u59cb\u5316\u7684\u6570\u7ec4\u548c\u4e0a\u4e00\u4e2a\u4f8b\u5b50\u7684\u201d\u521d\u59cb\u5316\u201c\u7ed3\u679c\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff1a 1 int prime [ 5 ]{ 2 , 3 , 5 , 7 , 11 }; // use initializer list to initialize the fixed array \u5982\u679c\u5217\u8868\u4e2d\u521d\u59cb\u5316\u503c\u7684\u6570\u91cf\u8d85\u8fc7\u4e86\u6570\u7ec4\u6240\u80fd\u5bb9\u7eb3\u7684\u6570\u91cf\uff0c\u7f16\u8bd1\u5668\u5c06\u4f1a\u62a5\u544a\u4e00\u4e2a\u9519\u8bef\u3002 \u4f46\u662f\uff0c\u5982\u679c\u5217\u8868\u4e2d\u521d\u59cb\u5316\u5668\u7684\u6570\u91cf\u5c11\u4e8e\u6570\u7ec4\u6240\u80fd\u5bb9\u7eb3\u7684\u6570\u91cf\uff0c\u5219\u5269\u4f59\u7684\u5143\u7d20\u5c06\u88ab\u521d\u59cb\u5316\u4e3a 0 (\u6216\u8005\uff0c 0 \u4f1a\u88ab\u8f6c\u6362\u5176\u4ed6\u975e\u6574\u578b\u7684\u503c\u2014\u2014\u4f8b\u5982\uff0c\u5bf9\u4e8e double \u7c7b\u578b\uff0c 0 \u4f1a\u8f6c\u6362\u4e3a 0.0 )\u3002\u8fd9\u88ab\u79f0\u4e3a \u96f6\u521d\u59cb\u5316(zero initialization) \u3002 \u8bf7\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int array [ 5 ]{ 7 , 4 , 5 }; // only initialize first 3 elements std :: cout << array [ 0 ] << '\\n' ; std :: cout << array [ 1 ] << '\\n' ; std :: cout << array [ 2 ] << '\\n' ; std :: cout << array [ 3 ] << '\\n' ; std :: cout << array [ 4 ] << '\\n' ; return 0 ; } \u6253\u5370\uff1a 1 2 3 4 5 7 4 5 0 0 \u56e0\u6b64\uff0c\u8981\u5c06\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u521d\u59cb\u5316\u4e3a 0\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a: 1 2 3 4 5 6 7 8 // Initialize all elements to 0 int array [ 5 ]{ }; // Initialize all elements to 0.0 double array [ 5 ]{ }; // Initialize all elements to an empty string std :: string array [ 5 ]{ }; \u5982\u679c\u7701\u7565\u521d\u59cb\u5316\u5217\u8868\uff0c\u5219\u6570\u7ec4\u5143\u7d20\u4e3a\u672a\u521d\u59cb\u5316\u72b6\u6001\uff0c\u9664\u975e\u5b83\u4eec\u662f class \u7c7b\u578b\u7684\u3002 1 2 3 4 5 6 7 8 // \u672a\u521d\u59cb\u5316 int array [ 5 ]; // \u672a\u521d\u59cb\u5316 double array [ 5 ]; // \u521d\u59cb\u5316\u4e3a\u7a7a\u5b57\u7b26\u4e32 std :: string array [ 5 ]; \u6700\u4f73\u5b9e\u8df5 \u4e0d\u8bba\u6570\u7ec4\u5143\u7d20\u53ef\u4ee5\u5728\u6ca1\u6709\u521d\u59cb\u5316\u5217\u8868\u65f6\u4f1a\u4e0d\u4f1a\u88ab\u9ed8\u8ba4\u521d\u59cb\u5316\uff0c\u90fd\u5e94\u5f53\u663e\u5f0f\u5730\u521d\u59cb\u5316\u6570\u7ec4\u3002 \u7701\u7565\u6570\u7ec4\u957f\u5ea6 \u5982\u679c\u60a8\u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u521d\u59cb\u5316\u4e00\u4e2a\u56fa\u5b9a\u957f\u5ea6\u6570\u7ec4\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u81ea\u52a8\u8ba1\u7b97\u51fa\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u7701\u7565\u6570\u7ec4\u957f\u5ea6\u7684\u58f0\u660e\u3002 \u4e0b\u9762\u4e24\u884c\u662f\u7b49\u4ef7\u7684: 1 2 int array [ 5 ]{ 0 , 1 , 2 , 3 , 4 }; // explicitly define the length of the array int array []{ 0 , 1 , 2 , 3 , 4 }; // let the initializer list set length of the array \u8fd9\u4e48\u505a\u7684\u597d\u5904\u53ef\u4e0d\u4ec5\u4ec5\u662f\u65b9\u4fbf\uff0c\u540c\u65f6\u53ef\u4ee5\u907f\u514d\u4f60\u5728\u4fee\u6539\u4e86\u5143\u7d20\u540e\u8fd8\u8981\u66f4\u65b0\u6570\u7ec4\u957f\u5ea6\u58f0\u660e\u3002 \u6570\u7ec4\u548c\u679a\u4e3e \u5bf9\u4e8e\u6570\u7ec4\u6765\u8bf4\uff0c\u8df5\u884c\u4ee3\u7801\u5373\u6587\u6863\u7684\u6700\u5927\u969c\u788d\uff0c\u5c31\u662f\u6574\u6570\u7d22\u5f15\u65e0\u6cd5\u5411\u7a0b\u5e8f\u5458\u63d0\u4f9b\u5173\u4e8e\u7d22\u5f15\u542b\u4e49\u7684\u4efb\u4f55\u4fe1\u606f\u3002\u5047\u8bbe\u4e00\u4e2a\u6709 5 \u4e2a\u5b66\u751f\u7684\u73ed\u7ea7: 1 2 3 constexpr int numberOfStudents { 5 }; int testScores [ numberOfStudents ]{}; testScores [ 2 ] = 76 ; testScores[2] \u8868\u793a\u7684\u662f\u8c01\u7684\u5206\u6570\uff1f\u6211\u4eec\u770b\u4e0d\u51fa\u6765\uff01 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u529e\u6cd5\u662f\u4f7f\u7528\u679a\u4e3e\u7c7b\u578b\uff0c\u6bcf\u4e2a\u679a\u4e3e\u53d8\u91cf\u5bf9\u5e94\u4e00\u4e2a\u6570\u7ec4\u7d22\u5f15\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 enum StudentNames { kenny , // 0 kyle , // 1 stan , // 2 butters , // 3 cartman , // 4 max_students // 5 }; int main () { int testScores [ max_students ]{}; // allocate 5 integers testScores [ stan ] = 76 ; return 0 ; } \u8fd9\u6837\uff0c\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u6240\u4ee3\u8868\u7684\u542b\u4e49\u5c31\u66f4\u52a0\u6e05\u6670\u4e86\u3002\u6ce8\u610f\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e2a\u540d\u4e3a max_students \u7684\u989d\u5916\u679a\u4e3e\u503c\u3002\u6b64\u679a\u4e3e\u503c\u5728\u6570\u7ec4\u58f0\u660e\u671f\u95f4\u4f7f\u7528\uff0c\u4ee5\u786e\u4fdd\u6570\u7ec4\u5177\u6709\u9002\u5f53\u7684\u957f\u5ea6(\u56e0\u4e3a\u6570\u7ec4\u957f\u5ea6\u5e94\u8be5\u6bd4\u6700\u5927\u7d22\u5f15\u5927 1)\u3002\u8fd9\u5bf9\u4e8e\u4ee3\u7801\u53ef\u8bfb\u6027\u975e\u5e38\u91cd\u8981\uff0c\u5f53\u6211\u4eec\u6dfb\u52a0\u65b0\u7684\u679a\u4e3e\u503c\u65f6\uff0c\u6570\u7ec4\u5c06\u81ea\u52a8\u8c03\u6574\u5927\u5c0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 enum StudentNames { kenny , // 0 kyle , // 1 stan , // 2 butters , // 3 cartman , // 4 wendy , // 5 max_students // 6 }; int main () { int testScores [ max_students ]{}; // allocate 6 integers testScores [ stan ] = 76 ; // still works return 0 ; } \u6ce8\u610f\uff0c\u8fd9\u4e2a\u6280\u5de7\u53ea\u6709\u5728\u4f60\u4e0d\u9700\u8981\u624b\u52a8\u4fee\u6539\u679a\u4e3e\u503c\u5bf9\u5e94\u7684\u6570\u503c\u65f6\u624d\u6709\u7528\u3002 \u6570\u7ec4\u548c\u679a\u4e3e\u7c7b \u679a\u4e3e\u7c7b(enum class) \u5e76\u4e0d\u80fd\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u6574\u578b\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u5c1d\u8bd5\u7f16\u5199\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 enum class StudentNames { kenny , // 0 kyle , // 1 stan , // 2 butters , // 3 cartman , // 4 wendy , // 5 max_students // 6 }; int main () { int testScores [ StudentNames :: max_students ]{}; // allocate 6 integers testScores [ StudentNames :: stan ] = 76 ; return 0 ; } \u4f60\u4f1a\u5f97\u5230\u4e00\u4e2a\u7f16\u8bd1\u9519\u8bef\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06\u679a\u4e3e\u503c\u8f6c\u6362\u4e3a\u6574\u578b\u3002 1 2 3 4 5 6 7 int main () { int testScores [ static_cast < int > ( StudentNames :: max_students )]{}; // allocate 6 integers testScores [ static_cast < int > ( StudentNames :: stan )] = 76 ; return 0 ; } \u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u663e\u7136\u5f88\u9ebb\u70e6\u3002\u6240\u4ee5\uff0c\u4f7f\u7528\u666e\u901a\u679a\u4e3e\u7c7b\u578b\u5e76\u5c06\u5176\u653e\u7f6e\u5728\u67d0\u4e2a\u4f5c\u7528\u57df\u4e2d\uff0c\u6bd4\u4f7f\u7528\u679a\u4e3e\u7c7b\u66f4\u597d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 namespace StudentNames { enum StudentNames { kenny , // 0 kyle , // 1 stan , // 2 butters , // 3 cartman , // 4 wendy , // 5 max_students // 6 }; } int main () { int testScores [ StudentNames :: max_students ]{}; // allocate 6 integers testScores [ StudentNames :: stan ] = 76 ; return 0 ; } \u5c06\u6570\u7ec4\u4f20\u9012\u7ed9\u51fd\u6570 \u867d\u7136\u4e4d\u4e00\u770b\u5411\u51fd\u6570\u4f20\u9012\u6570\u7ec4\u548c\u4f20\u9012\u666e\u901a\u53d8\u91cf\u597d\u50cf\u6ca1\u4ec0\u4e48\u533a\u522b\uff0c\u4f46\u5728\u672c\u8d28\u4e0a\uff0cC++\u5bf9\u4f20\u9012\u6570\u7ec4\u7684\u5904\u7406\u662f\u4e0d\u540c\u7684\u3002 \u666e\u901a\u53d8\u91cf\u901a\u8fc7 \u6309\u503c\u4f20\u9012 \u7684\u65b9\u5f0f\u4f20\u5165\u51fd\u6570\u2014\u2014C++\u5c06\u5b9e\u53c2\u7684\u503c\u590d\u5236\u5230\u51fd\u6570\u5f62\u53c2\u4e2d\u3002\u56e0\u4e3a\u5f62\u53c2\u662f\u4e00\u4e2a\u526f\u672c\uff0c\u6539\u53d8\u5f62\u53c2\u7684\u503c\u5e76\u4e0d\u4f1a\u6539\u53d8\u539f\u59cb\u5b9e\u53c2\u7684\u503c\u3002 \u4f46\u662f\uff0c \u7531\u4e8e\u590d\u5236\u5927\u578b\u6570\u7ec4\u7684\u5f00\u9500\u975e\u5e38\u5927\uff0c\u6240\u4ee5 C++\u5728\u5c06\u6570\u7ec4\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\u4e0d\u4f1a\u590d\u5236\u6570\u7ec4\u3002\u76f8\u53cd\uff0c\u5b83\u4f1a\u4f20\u9012 \u6570\u7ec4\u672c\u8eab \u3002\u8fd9\u6837\u505a\u7684\u526f\u4f5c\u7528\u662f\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u66f4\u6539\u6570\u7ec4\u5143\u7d20\u7684\u503c\uff01 \u4e0b\u9762\u7684\u4f8b\u5b50\u8bf4\u660e\u4e86\u8fd9\u4e2a\u6982\u5ff5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include void passValue ( int value ) // value \u662f\u5b9e\u53c2\u7684\u526f\u672c { value = 99 ; // \u4fee\u6539 value \u7684\u503c\u4e0d\u4f1a\u5f71\u54cd\u539f\u503c } void passArray ( int prime [ 5 ]) // prime \u662f\u6570\u7ec4\u672c\u8eab { prime [ 0 ] = 11 ; // \u4fee\u6539 prime \u5143\u7d20\u7684\u503c\u4f1a\u5b9e\u9645\u4fee\u6539\u6570\u7ec4 prime [ 1 ] = 7 ; prime [ 2 ] = 5 ; prime [ 3 ] = 3 ; prime [ 4 ] = 2 ; } int main () { int value { 1 }; std :: cout << \"before passValue: \" << value << '\\n' ; passValue ( value ); std :: cout << \"after passValue: \" << value << '\\n' ; int prime [ 5 ]{ 2 , 3 , 5 , 7 , 11 }; std :: cout << \"before passArray: \" << prime [ 0 ] << \" \" << prime [ 1 ] << \" \" << prime [ 2 ] << \" \" << prime [ 3 ] << \" \" << prime [ 4 ] << '\\n' ; passArray ( prime ); std :: cout << \"after passArray: \" << prime [ 0 ] << \" \" << prime [ 1 ] << \" \" << prime [ 2 ] << \" \" << prime [ 3 ] << \" \" << prime [ 4 ] << '\\n' ; return 0 ; } 1 2 3 4 before passValue: 1 after passValue: 1 before passArray: 2 3 5 7 11 after passArray: 11 7 5 3 2 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c value \u6ca1\u6709\u5728 main() \u88ab\u4e2d\u6539\u53d8\uff0c\u56e0\u4e3a passValue() \u51fd\u6570\u4e2d\u7684\u53c2\u6570\u503c\u662f main() \u51fd\u6570\u4e2d\u53d8\u91cf\u503c\u7684\u526f\u672c\uff0c\u800c\u4e0d\u662f\u5b9e\u9645\u7684\u53d8\u91cf\u3002\u7136\u800c\uff0c\u7531\u4e8e passArray() \u51fd\u6570\u4e2d\u7684\u6570\u7ec4\u662f\u6570\u7ec4\u672c\u8eab\uff0c passArray() \u80fd\u591f\u76f4\u63a5\u6539\u53d8\u5143\u7d20\u7684\u503c\uff01 \u4e0a\u8ff0\u73b0\u8c61\u80cc\u540e\u7684\u539f\u56e0\uff0c\u548c C++ \u5b9e\u73b0\u6570\u7ec4\u7684\u65b9\u5f0f\u6709\u5173\uff0c\u6211\u4eec\u4f1a\u5728 11.8 - \u6307\u9488\u548c\u6570\u7ec4 \u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002\u73b0\u5728\u4f60\u53ef\u4ee5\u6682\u65f6\u8ba4\u4e3a\u8fd9\u662f\u8bed\u8a00\u4e2d\u7684\u4e00\u4e2a\u201d\u602a\u5f02\u201c\u7279\u6027\u3002 \u6ce8\u610f\uff0c\u5982\u679c\u4f60\u60f3\u786e\u4fdd\u4e00\u4e2a\u51fd\u6570\u4e0d\u4f1a\u4fee\u6539\u4f20\u9012\u7ed9\u5b83\u7684\u6570\u7ec4\u5143\u7d20\uff0c\u4f60\u53ef\u4ee5\u5c06\u6570\u7ec4\u8bbe\u7f6e\u4e3a const : 1 2 3 4 5 6 7 8 9 10 // \u5373\u4f7f prime \u662f\u5b9e\u9645\u4e0a\u6570\u7ec4\uff0c\u5728\u51fd\u6570\u4e2d\u5b83\u4e5f\u4f1a\u88ab\u770b\u505a\u662f\u5e38\u91cf\u3002 void passArray ( const int prime [ 5 ]) { // so each of these lines will cause a compile error! prime [ 0 ] = 11 ; prime [ 1 ] = 7 ; prime [ 2 ] = 5 ; prime [ 3 ] = 3 ; prime [ 4 ] = 2 ; } \u786e\u5b9a\u6570\u7ec4\u957f\u5ea6 \u5934\u6587\u4ef6\u4e2d\u7684 std: size() \u51fd\u6570\u53ef\u4ee5\u7528\u6765\u5224\u65ad\u6570\u7ec4\u7684\u957f\u5ea6\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include // for std::size int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << \"The array has: \" << std :: size ( array ) << \" elements \\n \" ; return 0 ; } \u6253\u5370\uff1a 1 The array has: 8 elements \u6ce8\u610f\uff0c\u7531\u4e8e C++ \u51fd\u6570\u4f20\u9012\u6570\u7ec4\u7684\u7279\u6b8a\u6027\uff0c\u4e0a\u9762\u7684\u65b9\u6cd5\u4e0d\u80fd\u7528\u4e8e\u6570\u7ec4\u88ab\u4f20\u9012\u5230\u51fd\u6570\u4e2d\u7684\u60c5\u5f62\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include void printSize ( int array []) { std :: cout << std :: size ( array ) << '\\n' ; // Error } int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << std :: size ( array ) << '\\n' ; // will print the size of the array printSize ( array ); return 0 ; } std:: size() \u4e5f\u53ef\u4ee5\u7528\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\u5bf9\u8c61\uff08\u4f8b\u5982 std:: array \u548c std:: vector \uff09\uff0c\u800c\u4e14\u5982\u679c\u4f60\u5bf9\u88ab\u4f20\u5165\u51fd\u6570\u7684\u6570\u7ec4\u4f7f\u7528 std: size() \u65f6\uff0c\u4f1a\u4ea7\u751f\u7f16\u8bd1\u62a5\u9519\u3002\u6ce8\u610f\uff0c std:: size \u4f1a\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u65e0\u7b26\u53f7\u503c\uff0c\u5982\u679c\u4f60\u9700\u8981\u4f7f\u7528\u6709\u7b26\u53f7\u503c\uff0c\u5219\u8981\u4e48\u5c06\u5176\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u503c\uff0c\u6216\u8005\u4f7f\u7528 C++20 \u4e2d\u63d0\u4f9b\u7684 std:: ssize() (signed size)\u3002 \u8bd1\u8005\u6ce8 \u4e3a\u4ec0\u4e48 C++20 \u8981\u5f15\u5165 std: ssize() std:: size() \u662f\u5728 C++17 \u4e2d\u5f15\u5165\u7684\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u65e7\u7248\u672c\u7684\u7f16\u8bd1\u5668\uff0c\u90a3\u4e48\u53ea\u597d\u4f7f\u7528 sizeof \u8fd0\u7b97\u7b26\u3002 sizeof \u8fd0\u7b97\u7b26\u4f7f\u7528\u8d77\u6765\u4e0d\u5982 std: size() \u65b9\u4fbf\uff0c\u800c\u4e14\u6709\u4e9b\u5730\u65b9\u8981\u7279\u522b\u6ce8\u610f\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u517c\u5bb9 C++17 \u7684\u7f16\u8bd1\u5668\uff0c \u53ef\u4ee5\u76f4\u63a5\u8df3\u5230 \u6570\u7ec4\u8bbf\u95ee\u8d8a\u754c \u4e00\u8282\u3002 sizeof \u64cd\u4f5c\u7b26\u53ef\u4ee5\u7528\u4e8e\u6570\u7ec4\uff0c\u5b83\u4f1a\u8fd4\u56de \u6570\u7ec4\u7684\u5927\u5c0f \uff08\u6570\u7ec4\u957f\u5ea6\u4e58\u4ee5\u6570\u7ec4\u5143\u7d20\u7684\u5927\u5c0f\uff09\u3002 1 2 3 4 5 6 7 8 9 10 #include int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << sizeof ( array ) << '\\n' ; // will print the size of the array multiplied by the size of an int std :: cout << sizeof ( int ) << '\\n' ; return 0 ; } \u5728\u6574\u578b\u5927\u5c0f\u4e3a 4 \u5b57\u8282\u7684\u673a\u5668\u4e0a\uff1a 1 2 32 4 \u7ecf\u5178\u6280\u5de7\uff1a\u5c06\u6570\u7ec4\u7684\u5927\u5c0f\u9664\u4ee5\u6570\u7ec4\u5143\u7d20\u7684\u5927\u5c0f\uff0c\u5c31\u53ef\u4ee5\u5f97\u5230\u6570\u7ec4\u7684\u957f\u5ea6\u3002 1 2 3 4 5 6 7 8 9 #include int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << \"The array has: \" << sizeof ( array ) / sizeof ( array [ 0 ]) << \" elements \\n \" ; return 0 ; } \u6253\u5370\uff1a 1 The array has: 8 elements \u8fd9\u662f\u600e\u4e48\u56de\u4e8b\uff1f\u9996\u5148\uff0c\u8bf7\u6ce8\u610f\uff0c\u6574\u4e2a\u6570\u7ec4\u7684\u5927\u5c0f\u7b49\u4e8e\u6570\u7ec4\u7684\u957f\u5ea6\u4e58\u4ee5\u5143\u7d20\u7684\u5927\u5c0f\uff0c\u5373 array size = array length * element size \u3002 \u516c\u5f0f\u53d8\u5f62\u4e00\u4e0b\u5c31\u53ef\u4ee5\u5f97\u5230\uff1a array length = array size / element size \u3002 sizeof(array) \u5373\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u800c sizeof(array[0]) \u5219\u662f\u6570\u7ec4\u5143\u7d20\u7684\u5927\u5c0f\u3002\u5e26\u5165\u516c\u5f0f\u5f97\u5230\u6570\u7ec4 length = sizeof(array) / sizeof(array[0]) \u3002\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u6570\u7ec4\u5143\u7d20 0 \u4f5c\u4e3a\u6765\u8ba1\u7b97\u6570\u7ec4\u5143\u7d20\u7684\u5927\u5c0f\uff0c\u56e0\u4e3a\u65e0\u8bba\u6570\u7ec4\u957f\u5ea6\u662f\u591a\u5c11\uff0c\u7b2c\u4e00\u4e2a\u5143\u7d20\u603b\u662f\u5b58\u5728\u7684\u3002 \u5c06 sizeof \u5e94\u7528\u4e8e\u88ab\u4f20\u5165\u51fd\u6570\u7684\u6570\u7ec4\u65f6\uff0c\u4e0d\u4f1a\u50cf std:: size() \u4e00\u6837\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\uff0c\u53d6\u800c\u4ee3\u4e4b\u7684\u662f\uff0c\u5b83\u4f1a\u8fd4\u56de \u6307\u9488\u7684\u957f\u5ea6 \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include void printSize ( int array []) { std :: cout << sizeof ( array ) / sizeof ( array [ 0 ]) << '\\n' ; } int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << sizeof ( array ) / sizeof ( array [ 0 ]) << '\\n' ; printSize ( array ); return 0 ; } \u540c\u6837\u5047\u8bbe 8 \u5b57\u8282\u6307\u9488\u548c 4 \u5b57\u8282\u6574\u6570\uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 8 2 \u4f5c\u8005\u6ce8 \u5982\u679c\u8bd5\u56fe\u5bf9\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u6570\u7ec4\u4f7f\u7528 sizeof()\uff0c\u6b63\u786e\u914d\u7f6e\u7684\u7f16\u8bd1\u5668\u5e94\u8be5\u6253\u5370\u4e00\u4e2a\u8b66\u544a\u3002 \u5728 main() \u4e2d\u8ba1\u7b97\u6570\u7ec4\u957f\u5ea6\u662f\u6b63\u786e\u7684\uff0c\u4f46\u662f printSize() \u4e2d\u7684 sizeof() \u8fd4\u56de\u4e86 8 (\u6307\u9488\u7684\u5927\u5c0f)\uff0c 8 \u9664\u4ee5 4 \u7b49\u4e8e 2 \u3002 \u56e0\u6b64\uff0c\u5728\u6570\u7ec4\u4e0a\u4f7f\u7528 sizeof() \u65f6\u8981\u5c0f\u5fc3! \u6ce8\u610f\uff1a\u5728\u901a\u5e38\u7684\u7528\u6cd5\u4e2d\uff0c\u672f\u8bed\u201c\u6570\u7ec4\u5927\u5c0f\u201d\u548c\u201c\u6570\u7ec4\u957f\u5ea6\u201d\u90fd\u662f\u6700\u5e38\u7528\u4e8e\u6307\u6570\u7ec4\u7684\u957f\u5ea6(\u6570\u7ec4\u7684\u5927\u5c0f\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u662f\u6ca1\u6709\u7528\u7684\uff0c\u9664\u4e86\u6211\u4eec\u4e0a\u9762\u5c55\u793a\u7684\u6280\u5de7)\u3002 \u6570\u7ec4\u8bbf\u95ee\u8d8a\u754c \u8bb0\u4f4f\uff0c\u957f\u5ea6\u4e3a N \u7684\u6570\u7ec4\uff0c\u5176\u4e0b\u6807\u4ece 0 \u5230 N-1 \u3002\u5982\u679c\u4f60\u8bd5\u56fe\u8bbf\u95ee\u7684\u6570\u7ec4\u4e0b\u6807\uff0c\u5728\u8fd9\u4e2a\u8303\u56f4\u4e4b\u5916\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f \u8003\u8651\u4ee5\u4e0b\u7a0b\u5e8f: 1 2 3 4 5 6 7 int main () { int prime [ 5 ]{}; // hold the first 5 prime numbers prime [ 5 ] = 13 ; return 0 ; } \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u6570\u7ec4\u957f\u5ea6\u4e3a 5 \uff0c\u4f46\u6211\u4eec\u8bd5\u56fe\u5c06\u7b2c 6 \u4e2a\u5143\u7d20(\u7d22\u5f15 5)\u5199\u5165 prime \u6570\u7ec4\u3002 C++ \u4e0d\u4f1a\u68c0\u67e5\u4e0b\u6807\u662f\u5426\u5408\u6cd5\u3002\u6240\u4ee5\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c13 \u7684\u503c\u4f1a\u88ab\u63d2\u5165\u5230\u5185\u5b58\u4e2d\uff08\u7b2c 6 \u4e2a\u5143\u7d20\u4f4d\u7f6e\u7684\u5185\u5b58\u53ef\u7528\u7684\u8bdd\uff09\u3002\u5f53\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\u65f6\uff0c\u60a8\u5c06\u5f97\u5230\u672a\u5b9a\u4e49\u7684\u884c\u4e3a\u2014\u2014\u4f8b\u5982\uff0c\u8fd9\u53ef\u80fd\u4f1a\u8986\u76d6\u53e6\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u6216\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002 \u5c3d\u7ba1\u4e0d\u5e38\u89c1\uff0c\u4f46\u5728 C++\u4e2d\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8d1f\u6570\u7d22\u5f15\uff0c\u8fd9\u4e5f\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002 \u6cd5\u5219 \u5f53\u4f7f\u7528\u6570\u7ec4\u65f6\uff0c\u786e\u4fdd\u4f60\u7684\u7d22\u5f15\u5728\u6709\u6548\u8303\u56f4\u5185","title":"11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09"},{"location":"11-2-Arrays-Part-II/#_1","text":"\u6570\u7ec4\u5143\u7d20\u4f1a\u88ab\u5f53\u505a\u666e\u901a\u53d8\u91cf\u6765\u5bf9\u5f85\uff0c\u56e0\u6b64\uff0c\u5b83\u4eec\u5728\u521b\u5efa\u65f6\u9ed8\u8ba4\u662f\u4e0d\u521d\u59cb\u5316\u7684\u3002 \u201c\u521d\u59cb\u5316\u201d\u6570\u7ec4\u7684\u65b9\u5f0f\u4e4b\u4e00\uff0c\u662f\u9010\u4e2a\u5143\u7d20\u8fdb\u884c\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 int prime [ 5 ]; // hold the first 5 prime numbers prime [ 0 ] = 2 ; prime [ 1 ] = 3 ; prime [ 2 ] = 5 ; prime [ 3 ] = 7 ; prime [ 4 ] = 11 ; \u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u5f88\u9ebb\u70e6\uff0c\u51e0\u4e4e\u4e0d\u53ef\u80fd\u7528\u6765\u521d\u59cb\u5316\u975e\u5e38\u957f\u7684\u6570\u7ec4\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u4e0e\u5176\u8bf4\u8fd9\u662f\u5728\u521d\u59cb\u5316\uff0c\u5012\u4e0d\u5982\u8bf4\u662f\u5728\u8d4b\u503c\u3002\u5982\u679c\u6570\u7ec4\u662f const \u7c7b\u578b\u7684\u8bdd\uff0c\u8d4b\u503c\u662f\u884c\u4e0d\u901a\u7684\u3002 \u5e78\u597d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 \u521d\u59cb\u5316\u503c\u5217\u8868(initializer list) \u6765\u521d\u59cb\u5316\u6574\u4e2a\u6570\u7ec4\u3002\u4e0b\u9762\u4f8b\u5b50\u4e2d\u521d\u59cb\u5316\u7684\u6570\u7ec4\u548c\u4e0a\u4e00\u4e2a\u4f8b\u5b50\u7684\u201d\u521d\u59cb\u5316\u201c\u7ed3\u679c\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff1a 1 int prime [ 5 ]{ 2 , 3 , 5 , 7 , 11 }; // use initializer list to initialize the fixed array \u5982\u679c\u5217\u8868\u4e2d\u521d\u59cb\u5316\u503c\u7684\u6570\u91cf\u8d85\u8fc7\u4e86\u6570\u7ec4\u6240\u80fd\u5bb9\u7eb3\u7684\u6570\u91cf\uff0c\u7f16\u8bd1\u5668\u5c06\u4f1a\u62a5\u544a\u4e00\u4e2a\u9519\u8bef\u3002 \u4f46\u662f\uff0c\u5982\u679c\u5217\u8868\u4e2d\u521d\u59cb\u5316\u5668\u7684\u6570\u91cf\u5c11\u4e8e\u6570\u7ec4\u6240\u80fd\u5bb9\u7eb3\u7684\u6570\u91cf\uff0c\u5219\u5269\u4f59\u7684\u5143\u7d20\u5c06\u88ab\u521d\u59cb\u5316\u4e3a 0 (\u6216\u8005\uff0c 0 \u4f1a\u88ab\u8f6c\u6362\u5176\u4ed6\u975e\u6574\u578b\u7684\u503c\u2014\u2014\u4f8b\u5982\uff0c\u5bf9\u4e8e double \u7c7b\u578b\uff0c 0 \u4f1a\u8f6c\u6362\u4e3a 0.0 )\u3002\u8fd9\u88ab\u79f0\u4e3a \u96f6\u521d\u59cb\u5316(zero initialization) \u3002 \u8bf7\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int array [ 5 ]{ 7 , 4 , 5 }; // only initialize first 3 elements std :: cout << array [ 0 ] << '\\n' ; std :: cout << array [ 1 ] << '\\n' ; std :: cout << array [ 2 ] << '\\n' ; std :: cout << array [ 3 ] << '\\n' ; std :: cout << array [ 4 ] << '\\n' ; return 0 ; } \u6253\u5370\uff1a 1 2 3 4 5 7 4 5 0 0 \u56e0\u6b64\uff0c\u8981\u5c06\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u521d\u59cb\u5316\u4e3a 0\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a: 1 2 3 4 5 6 7 8 // Initialize all elements to 0 int array [ 5 ]{ }; // Initialize all elements to 0.0 double array [ 5 ]{ }; // Initialize all elements to an empty string std :: string array [ 5 ]{ }; \u5982\u679c\u7701\u7565\u521d\u59cb\u5316\u5217\u8868\uff0c\u5219\u6570\u7ec4\u5143\u7d20\u4e3a\u672a\u521d\u59cb\u5316\u72b6\u6001\uff0c\u9664\u975e\u5b83\u4eec\u662f class \u7c7b\u578b\u7684\u3002 1 2 3 4 5 6 7 8 // \u672a\u521d\u59cb\u5316 int array [ 5 ]; // \u672a\u521d\u59cb\u5316 double array [ 5 ]; // \u521d\u59cb\u5316\u4e3a\u7a7a\u5b57\u7b26\u4e32 std :: string array [ 5 ]; \u6700\u4f73\u5b9e\u8df5 \u4e0d\u8bba\u6570\u7ec4\u5143\u7d20\u53ef\u4ee5\u5728\u6ca1\u6709\u521d\u59cb\u5316\u5217\u8868\u65f6\u4f1a\u4e0d\u4f1a\u88ab\u9ed8\u8ba4\u521d\u59cb\u5316\uff0c\u90fd\u5e94\u5f53\u663e\u5f0f\u5730\u521d\u59cb\u5316\u6570\u7ec4\u3002","title":"\u56fa\u5b9a\u6570\u7ec4\u521d\u59cb\u5316"},{"location":"11-2-Arrays-Part-II/#_2","text":"\u5982\u679c\u60a8\u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u521d\u59cb\u5316\u4e00\u4e2a\u56fa\u5b9a\u957f\u5ea6\u6570\u7ec4\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u81ea\u52a8\u8ba1\u7b97\u51fa\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u7701\u7565\u6570\u7ec4\u957f\u5ea6\u7684\u58f0\u660e\u3002 \u4e0b\u9762\u4e24\u884c\u662f\u7b49\u4ef7\u7684: 1 2 int array [ 5 ]{ 0 , 1 , 2 , 3 , 4 }; // explicitly define the length of the array int array []{ 0 , 1 , 2 , 3 , 4 }; // let the initializer list set length of the array \u8fd9\u4e48\u505a\u7684\u597d\u5904\u53ef\u4e0d\u4ec5\u4ec5\u662f\u65b9\u4fbf\uff0c\u540c\u65f6\u53ef\u4ee5\u907f\u514d\u4f60\u5728\u4fee\u6539\u4e86\u5143\u7d20\u540e\u8fd8\u8981\u66f4\u65b0\u6570\u7ec4\u957f\u5ea6\u58f0\u660e\u3002","title":"\u7701\u7565\u6570\u7ec4\u957f\u5ea6"},{"location":"11-2-Arrays-Part-II/#_3","text":"\u5bf9\u4e8e\u6570\u7ec4\u6765\u8bf4\uff0c\u8df5\u884c\u4ee3\u7801\u5373\u6587\u6863\u7684\u6700\u5927\u969c\u788d\uff0c\u5c31\u662f\u6574\u6570\u7d22\u5f15\u65e0\u6cd5\u5411\u7a0b\u5e8f\u5458\u63d0\u4f9b\u5173\u4e8e\u7d22\u5f15\u542b\u4e49\u7684\u4efb\u4f55\u4fe1\u606f\u3002\u5047\u8bbe\u4e00\u4e2a\u6709 5 \u4e2a\u5b66\u751f\u7684\u73ed\u7ea7: 1 2 3 constexpr int numberOfStudents { 5 }; int testScores [ numberOfStudents ]{}; testScores [ 2 ] = 76 ; testScores[2] \u8868\u793a\u7684\u662f\u8c01\u7684\u5206\u6570\uff1f\u6211\u4eec\u770b\u4e0d\u51fa\u6765\uff01 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u529e\u6cd5\u662f\u4f7f\u7528\u679a\u4e3e\u7c7b\u578b\uff0c\u6bcf\u4e2a\u679a\u4e3e\u53d8\u91cf\u5bf9\u5e94\u4e00\u4e2a\u6570\u7ec4\u7d22\u5f15\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 enum StudentNames { kenny , // 0 kyle , // 1 stan , // 2 butters , // 3 cartman , // 4 max_students // 5 }; int main () { int testScores [ max_students ]{}; // allocate 5 integers testScores [ stan ] = 76 ; return 0 ; } \u8fd9\u6837\uff0c\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u6240\u4ee3\u8868\u7684\u542b\u4e49\u5c31\u66f4\u52a0\u6e05\u6670\u4e86\u3002\u6ce8\u610f\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e2a\u540d\u4e3a max_students \u7684\u989d\u5916\u679a\u4e3e\u503c\u3002\u6b64\u679a\u4e3e\u503c\u5728\u6570\u7ec4\u58f0\u660e\u671f\u95f4\u4f7f\u7528\uff0c\u4ee5\u786e\u4fdd\u6570\u7ec4\u5177\u6709\u9002\u5f53\u7684\u957f\u5ea6(\u56e0\u4e3a\u6570\u7ec4\u957f\u5ea6\u5e94\u8be5\u6bd4\u6700\u5927\u7d22\u5f15\u5927 1)\u3002\u8fd9\u5bf9\u4e8e\u4ee3\u7801\u53ef\u8bfb\u6027\u975e\u5e38\u91cd\u8981\uff0c\u5f53\u6211\u4eec\u6dfb\u52a0\u65b0\u7684\u679a\u4e3e\u503c\u65f6\uff0c\u6570\u7ec4\u5c06\u81ea\u52a8\u8c03\u6574\u5927\u5c0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 enum StudentNames { kenny , // 0 kyle , // 1 stan , // 2 butters , // 3 cartman , // 4 wendy , // 5 max_students // 6 }; int main () { int testScores [ max_students ]{}; // allocate 6 integers testScores [ stan ] = 76 ; // still works return 0 ; } \u6ce8\u610f\uff0c\u8fd9\u4e2a\u6280\u5de7\u53ea\u6709\u5728\u4f60\u4e0d\u9700\u8981\u624b\u52a8\u4fee\u6539\u679a\u4e3e\u503c\u5bf9\u5e94\u7684\u6570\u503c\u65f6\u624d\u6709\u7528\u3002","title":"\u6570\u7ec4\u548c\u679a\u4e3e"},{"location":"11-2-Arrays-Part-II/#_4","text":"\u679a\u4e3e\u7c7b(enum class) \u5e76\u4e0d\u80fd\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u6574\u578b\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u5c1d\u8bd5\u7f16\u5199\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 enum class StudentNames { kenny , // 0 kyle , // 1 stan , // 2 butters , // 3 cartman , // 4 wendy , // 5 max_students // 6 }; int main () { int testScores [ StudentNames :: max_students ]{}; // allocate 6 integers testScores [ StudentNames :: stan ] = 76 ; return 0 ; } \u4f60\u4f1a\u5f97\u5230\u4e00\u4e2a\u7f16\u8bd1\u9519\u8bef\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06\u679a\u4e3e\u503c\u8f6c\u6362\u4e3a\u6574\u578b\u3002 1 2 3 4 5 6 7 int main () { int testScores [ static_cast < int > ( StudentNames :: max_students )]{}; // allocate 6 integers testScores [ static_cast < int > ( StudentNames :: stan )] = 76 ; return 0 ; } \u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u663e\u7136\u5f88\u9ebb\u70e6\u3002\u6240\u4ee5\uff0c\u4f7f\u7528\u666e\u901a\u679a\u4e3e\u7c7b\u578b\u5e76\u5c06\u5176\u653e\u7f6e\u5728\u67d0\u4e2a\u4f5c\u7528\u57df\u4e2d\uff0c\u6bd4\u4f7f\u7528\u679a\u4e3e\u7c7b\u66f4\u597d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 namespace StudentNames { enum StudentNames { kenny , // 0 kyle , // 1 stan , // 2 butters , // 3 cartman , // 4 wendy , // 5 max_students // 6 }; } int main () { int testScores [ StudentNames :: max_students ]{}; // allocate 6 integers testScores [ StudentNames :: stan ] = 76 ; return 0 ; }","title":"\u6570\u7ec4\u548c\u679a\u4e3e\u7c7b"},{"location":"11-2-Arrays-Part-II/#_5","text":"\u867d\u7136\u4e4d\u4e00\u770b\u5411\u51fd\u6570\u4f20\u9012\u6570\u7ec4\u548c\u4f20\u9012\u666e\u901a\u53d8\u91cf\u597d\u50cf\u6ca1\u4ec0\u4e48\u533a\u522b\uff0c\u4f46\u5728\u672c\u8d28\u4e0a\uff0cC++\u5bf9\u4f20\u9012\u6570\u7ec4\u7684\u5904\u7406\u662f\u4e0d\u540c\u7684\u3002 \u666e\u901a\u53d8\u91cf\u901a\u8fc7 \u6309\u503c\u4f20\u9012 \u7684\u65b9\u5f0f\u4f20\u5165\u51fd\u6570\u2014\u2014C++\u5c06\u5b9e\u53c2\u7684\u503c\u590d\u5236\u5230\u51fd\u6570\u5f62\u53c2\u4e2d\u3002\u56e0\u4e3a\u5f62\u53c2\u662f\u4e00\u4e2a\u526f\u672c\uff0c\u6539\u53d8\u5f62\u53c2\u7684\u503c\u5e76\u4e0d\u4f1a\u6539\u53d8\u539f\u59cb\u5b9e\u53c2\u7684\u503c\u3002 \u4f46\u662f\uff0c \u7531\u4e8e\u590d\u5236\u5927\u578b\u6570\u7ec4\u7684\u5f00\u9500\u975e\u5e38\u5927\uff0c\u6240\u4ee5 C++\u5728\u5c06\u6570\u7ec4\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\u4e0d\u4f1a\u590d\u5236\u6570\u7ec4\u3002\u76f8\u53cd\uff0c\u5b83\u4f1a\u4f20\u9012 \u6570\u7ec4\u672c\u8eab \u3002\u8fd9\u6837\u505a\u7684\u526f\u4f5c\u7528\u662f\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u66f4\u6539\u6570\u7ec4\u5143\u7d20\u7684\u503c\uff01 \u4e0b\u9762\u7684\u4f8b\u5b50\u8bf4\u660e\u4e86\u8fd9\u4e2a\u6982\u5ff5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include void passValue ( int value ) // value \u662f\u5b9e\u53c2\u7684\u526f\u672c { value = 99 ; // \u4fee\u6539 value \u7684\u503c\u4e0d\u4f1a\u5f71\u54cd\u539f\u503c } void passArray ( int prime [ 5 ]) // prime \u662f\u6570\u7ec4\u672c\u8eab { prime [ 0 ] = 11 ; // \u4fee\u6539 prime \u5143\u7d20\u7684\u503c\u4f1a\u5b9e\u9645\u4fee\u6539\u6570\u7ec4 prime [ 1 ] = 7 ; prime [ 2 ] = 5 ; prime [ 3 ] = 3 ; prime [ 4 ] = 2 ; } int main () { int value { 1 }; std :: cout << \"before passValue: \" << value << '\\n' ; passValue ( value ); std :: cout << \"after passValue: \" << value << '\\n' ; int prime [ 5 ]{ 2 , 3 , 5 , 7 , 11 }; std :: cout << \"before passArray: \" << prime [ 0 ] << \" \" << prime [ 1 ] << \" \" << prime [ 2 ] << \" \" << prime [ 3 ] << \" \" << prime [ 4 ] << '\\n' ; passArray ( prime ); std :: cout << \"after passArray: \" << prime [ 0 ] << \" \" << prime [ 1 ] << \" \" << prime [ 2 ] << \" \" << prime [ 3 ] << \" \" << prime [ 4 ] << '\\n' ; return 0 ; } 1 2 3 4 before passValue: 1 after passValue: 1 before passArray: 2 3 5 7 11 after passArray: 11 7 5 3 2 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c value \u6ca1\u6709\u5728 main() \u88ab\u4e2d\u6539\u53d8\uff0c\u56e0\u4e3a passValue() \u51fd\u6570\u4e2d\u7684\u53c2\u6570\u503c\u662f main() \u51fd\u6570\u4e2d\u53d8\u91cf\u503c\u7684\u526f\u672c\uff0c\u800c\u4e0d\u662f\u5b9e\u9645\u7684\u53d8\u91cf\u3002\u7136\u800c\uff0c\u7531\u4e8e passArray() \u51fd\u6570\u4e2d\u7684\u6570\u7ec4\u662f\u6570\u7ec4\u672c\u8eab\uff0c passArray() \u80fd\u591f\u76f4\u63a5\u6539\u53d8\u5143\u7d20\u7684\u503c\uff01 \u4e0a\u8ff0\u73b0\u8c61\u80cc\u540e\u7684\u539f\u56e0\uff0c\u548c C++ \u5b9e\u73b0\u6570\u7ec4\u7684\u65b9\u5f0f\u6709\u5173\uff0c\u6211\u4eec\u4f1a\u5728 11.8 - \u6307\u9488\u548c\u6570\u7ec4 \u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002\u73b0\u5728\u4f60\u53ef\u4ee5\u6682\u65f6\u8ba4\u4e3a\u8fd9\u662f\u8bed\u8a00\u4e2d\u7684\u4e00\u4e2a\u201d\u602a\u5f02\u201c\u7279\u6027\u3002 \u6ce8\u610f\uff0c\u5982\u679c\u4f60\u60f3\u786e\u4fdd\u4e00\u4e2a\u51fd\u6570\u4e0d\u4f1a\u4fee\u6539\u4f20\u9012\u7ed9\u5b83\u7684\u6570\u7ec4\u5143\u7d20\uff0c\u4f60\u53ef\u4ee5\u5c06\u6570\u7ec4\u8bbe\u7f6e\u4e3a const : 1 2 3 4 5 6 7 8 9 10 // \u5373\u4f7f prime \u662f\u5b9e\u9645\u4e0a\u6570\u7ec4\uff0c\u5728\u51fd\u6570\u4e2d\u5b83\u4e5f\u4f1a\u88ab\u770b\u505a\u662f\u5e38\u91cf\u3002 void passArray ( const int prime [ 5 ]) { // so each of these lines will cause a compile error! prime [ 0 ] = 11 ; prime [ 1 ] = 7 ; prime [ 2 ] = 5 ; prime [ 3 ] = 3 ; prime [ 4 ] = 2 ; }","title":"\u5c06\u6570\u7ec4\u4f20\u9012\u7ed9\u51fd\u6570"},{"location":"11-2-Arrays-Part-II/#_6","text":" \u5934\u6587\u4ef6\u4e2d\u7684 std: size() \u51fd\u6570\u53ef\u4ee5\u7528\u6765\u5224\u65ad\u6570\u7ec4\u7684\u957f\u5ea6\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include // for std::size int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << \"The array has: \" << std :: size ( array ) << \" elements \\n \" ; return 0 ; } \u6253\u5370\uff1a 1 The array has: 8 elements \u6ce8\u610f\uff0c\u7531\u4e8e C++ \u51fd\u6570\u4f20\u9012\u6570\u7ec4\u7684\u7279\u6b8a\u6027\uff0c\u4e0a\u9762\u7684\u65b9\u6cd5\u4e0d\u80fd\u7528\u4e8e\u6570\u7ec4\u88ab\u4f20\u9012\u5230\u51fd\u6570\u4e2d\u7684\u60c5\u5f62\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include void printSize ( int array []) { std :: cout << std :: size ( array ) << '\\n' ; // Error } int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << std :: size ( array ) << '\\n' ; // will print the size of the array printSize ( array ); return 0 ; } std:: size() \u4e5f\u53ef\u4ee5\u7528\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\u5bf9\u8c61\uff08\u4f8b\u5982 std:: array \u548c std:: vector \uff09\uff0c\u800c\u4e14\u5982\u679c\u4f60\u5bf9\u88ab\u4f20\u5165\u51fd\u6570\u7684\u6570\u7ec4\u4f7f\u7528 std: size() \u65f6\uff0c\u4f1a\u4ea7\u751f\u7f16\u8bd1\u62a5\u9519\u3002\u6ce8\u610f\uff0c std:: size \u4f1a\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u65e0\u7b26\u53f7\u503c\uff0c\u5982\u679c\u4f60\u9700\u8981\u4f7f\u7528\u6709\u7b26\u53f7\u503c\uff0c\u5219\u8981\u4e48\u5c06\u5176\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u503c\uff0c\u6216\u8005\u4f7f\u7528 C++20 \u4e2d\u63d0\u4f9b\u7684 std:: ssize() (signed size)\u3002 \u8bd1\u8005\u6ce8 \u4e3a\u4ec0\u4e48 C++20 \u8981\u5f15\u5165 std: ssize() std:: size() \u662f\u5728 C++17 \u4e2d\u5f15\u5165\u7684\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u65e7\u7248\u672c\u7684\u7f16\u8bd1\u5668\uff0c\u90a3\u4e48\u53ea\u597d\u4f7f\u7528 sizeof \u8fd0\u7b97\u7b26\u3002 sizeof \u8fd0\u7b97\u7b26\u4f7f\u7528\u8d77\u6765\u4e0d\u5982 std: size() \u65b9\u4fbf\uff0c\u800c\u4e14\u6709\u4e9b\u5730\u65b9\u8981\u7279\u522b\u6ce8\u610f\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u517c\u5bb9 C++17 \u7684\u7f16\u8bd1\u5668\uff0c \u53ef\u4ee5\u76f4\u63a5\u8df3\u5230 \u6570\u7ec4\u8bbf\u95ee\u8d8a\u754c \u4e00\u8282\u3002 sizeof \u64cd\u4f5c\u7b26\u53ef\u4ee5\u7528\u4e8e\u6570\u7ec4\uff0c\u5b83\u4f1a\u8fd4\u56de \u6570\u7ec4\u7684\u5927\u5c0f \uff08\u6570\u7ec4\u957f\u5ea6\u4e58\u4ee5\u6570\u7ec4\u5143\u7d20\u7684\u5927\u5c0f\uff09\u3002 1 2 3 4 5 6 7 8 9 10 #include int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << sizeof ( array ) << '\\n' ; // will print the size of the array multiplied by the size of an int std :: cout << sizeof ( int ) << '\\n' ; return 0 ; } \u5728\u6574\u578b\u5927\u5c0f\u4e3a 4 \u5b57\u8282\u7684\u673a\u5668\u4e0a\uff1a 1 2 32 4 \u7ecf\u5178\u6280\u5de7\uff1a\u5c06\u6570\u7ec4\u7684\u5927\u5c0f\u9664\u4ee5\u6570\u7ec4\u5143\u7d20\u7684\u5927\u5c0f\uff0c\u5c31\u53ef\u4ee5\u5f97\u5230\u6570\u7ec4\u7684\u957f\u5ea6\u3002 1 2 3 4 5 6 7 8 9 #include int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << \"The array has: \" << sizeof ( array ) / sizeof ( array [ 0 ]) << \" elements \\n \" ; return 0 ; } \u6253\u5370\uff1a 1 The array has: 8 elements \u8fd9\u662f\u600e\u4e48\u56de\u4e8b\uff1f\u9996\u5148\uff0c\u8bf7\u6ce8\u610f\uff0c\u6574\u4e2a\u6570\u7ec4\u7684\u5927\u5c0f\u7b49\u4e8e\u6570\u7ec4\u7684\u957f\u5ea6\u4e58\u4ee5\u5143\u7d20\u7684\u5927\u5c0f\uff0c\u5373 array size = array length * element size \u3002 \u516c\u5f0f\u53d8\u5f62\u4e00\u4e0b\u5c31\u53ef\u4ee5\u5f97\u5230\uff1a array length = array size / element size \u3002 sizeof(array) \u5373\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u800c sizeof(array[0]) \u5219\u662f\u6570\u7ec4\u5143\u7d20\u7684\u5927\u5c0f\u3002\u5e26\u5165\u516c\u5f0f\u5f97\u5230\u6570\u7ec4 length = sizeof(array) / sizeof(array[0]) \u3002\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u6570\u7ec4\u5143\u7d20 0 \u4f5c\u4e3a\u6765\u8ba1\u7b97\u6570\u7ec4\u5143\u7d20\u7684\u5927\u5c0f\uff0c\u56e0\u4e3a\u65e0\u8bba\u6570\u7ec4\u957f\u5ea6\u662f\u591a\u5c11\uff0c\u7b2c\u4e00\u4e2a\u5143\u7d20\u603b\u662f\u5b58\u5728\u7684\u3002 \u5c06 sizeof \u5e94\u7528\u4e8e\u88ab\u4f20\u5165\u51fd\u6570\u7684\u6570\u7ec4\u65f6\uff0c\u4e0d\u4f1a\u50cf std:: size() \u4e00\u6837\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\uff0c\u53d6\u800c\u4ee3\u4e4b\u7684\u662f\uff0c\u5b83\u4f1a\u8fd4\u56de \u6307\u9488\u7684\u957f\u5ea6 \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include void printSize ( int array []) { std :: cout << sizeof ( array ) / sizeof ( array [ 0 ]) << '\\n' ; } int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << sizeof ( array ) / sizeof ( array [ 0 ]) << '\\n' ; printSize ( array ); return 0 ; } \u540c\u6837\u5047\u8bbe 8 \u5b57\u8282\u6307\u9488\u548c 4 \u5b57\u8282\u6574\u6570\uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 8 2 \u4f5c\u8005\u6ce8 \u5982\u679c\u8bd5\u56fe\u5bf9\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u6570\u7ec4\u4f7f\u7528 sizeof()\uff0c\u6b63\u786e\u914d\u7f6e\u7684\u7f16\u8bd1\u5668\u5e94\u8be5\u6253\u5370\u4e00\u4e2a\u8b66\u544a\u3002 \u5728 main() \u4e2d\u8ba1\u7b97\u6570\u7ec4\u957f\u5ea6\u662f\u6b63\u786e\u7684\uff0c\u4f46\u662f printSize() \u4e2d\u7684 sizeof() \u8fd4\u56de\u4e86 8 (\u6307\u9488\u7684\u5927\u5c0f)\uff0c 8 \u9664\u4ee5 4 \u7b49\u4e8e 2 \u3002 \u56e0\u6b64\uff0c\u5728\u6570\u7ec4\u4e0a\u4f7f\u7528 sizeof() \u65f6\u8981\u5c0f\u5fc3! \u6ce8\u610f\uff1a\u5728\u901a\u5e38\u7684\u7528\u6cd5\u4e2d\uff0c\u672f\u8bed\u201c\u6570\u7ec4\u5927\u5c0f\u201d\u548c\u201c\u6570\u7ec4\u957f\u5ea6\u201d\u90fd\u662f\u6700\u5e38\u7528\u4e8e\u6307\u6570\u7ec4\u7684\u957f\u5ea6(\u6570\u7ec4\u7684\u5927\u5c0f\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u662f\u6ca1\u6709\u7528\u7684\uff0c\u9664\u4e86\u6211\u4eec\u4e0a\u9762\u5c55\u793a\u7684\u6280\u5de7)\u3002","title":"\u786e\u5b9a\u6570\u7ec4\u957f\u5ea6"},{"location":"11-2-Arrays-Part-II/#_7","text":"\u8bb0\u4f4f\uff0c\u957f\u5ea6\u4e3a N \u7684\u6570\u7ec4\uff0c\u5176\u4e0b\u6807\u4ece 0 \u5230 N-1 \u3002\u5982\u679c\u4f60\u8bd5\u56fe\u8bbf\u95ee\u7684\u6570\u7ec4\u4e0b\u6807\uff0c\u5728\u8fd9\u4e2a\u8303\u56f4\u4e4b\u5916\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f \u8003\u8651\u4ee5\u4e0b\u7a0b\u5e8f: 1 2 3 4 5 6 7 int main () { int prime [ 5 ]{}; // hold the first 5 prime numbers prime [ 5 ] = 13 ; return 0 ; } \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u6570\u7ec4\u957f\u5ea6\u4e3a 5 \uff0c\u4f46\u6211\u4eec\u8bd5\u56fe\u5c06\u7b2c 6 \u4e2a\u5143\u7d20(\u7d22\u5f15 5)\u5199\u5165 prime \u6570\u7ec4\u3002 C++ \u4e0d\u4f1a\u68c0\u67e5\u4e0b\u6807\u662f\u5426\u5408\u6cd5\u3002\u6240\u4ee5\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c13 \u7684\u503c\u4f1a\u88ab\u63d2\u5165\u5230\u5185\u5b58\u4e2d\uff08\u7b2c 6 \u4e2a\u5143\u7d20\u4f4d\u7f6e\u7684\u5185\u5b58\u53ef\u7528\u7684\u8bdd\uff09\u3002\u5f53\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\u65f6\uff0c\u60a8\u5c06\u5f97\u5230\u672a\u5b9a\u4e49\u7684\u884c\u4e3a\u2014\u2014\u4f8b\u5982\uff0c\u8fd9\u53ef\u80fd\u4f1a\u8986\u76d6\u53e6\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u6216\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002 \u5c3d\u7ba1\u4e0d\u5e38\u89c1\uff0c\u4f46\u5728 C++\u4e2d\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8d1f\u6570\u7d22\u5f15\uff0c\u8fd9\u4e5f\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002 \u6cd5\u5219 \u5f53\u4f7f\u7528\u6570\u7ec4\u65f6\uff0c\u786e\u4fdd\u4f60\u7684\u7d22\u5f15\u5728\u6709\u6548\u8303\u56f4\u5185","title":"\u6570\u7ec4\u8bbf\u95ee\u8d8a\u754c"},{"location":"11-3-Arrays-and-loops/","tags":["array","loop"],"text":"Key Takeaway \u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u5982\u679c\u9700\u8981\u8ba1\u7b97\u4e00\u4e2a\u73ed\u7ea7\u5b66\u751f\u7684\u5e73\u5747\u6210\u7ee9\u7684\u8bdd\uff0c\u4f7f\u7528\u591a\u4e2a\u53d8\u91cf\u7684\u8bdd\u53ef\u4ee5\u8fd9\u6837\u505a: 1 2 3 4 5 6 7 8 9 int numStudents { 5 }; int score0 { 84 }; int score1 { 92 }; int score2 { 76 }; int score3 { 81 }; int score4 { 56 }; int totalScore { score0 + score1 + score2 + score3 + score4 }; auto averageScore { static_cast < double > ( totalScore ) / numStudents }; \u771f\u662f\u4e0d\u5c11\u53d8\u91cf\u554a\u2014\u2014\u800c\u8fd9\u4ec5\u4ec5\u662f\u8ba1\u7b975\u4e2a\u5b66\u751f\u7684\u5e73\u5747\u503c! \u60f3\u8c61\u4e00\u4e0b\u5982\u679c\u662f30\u4e2a\u5b66\u751f\u6216150\u4e2a\u5b66\u751f\uff0c\u90a3\u5f97\u662f\u591a\u5927\u7684\u5de5\u4f5c\u91cf\u554a\u3002 \u53e6\u5916\uff0c\u5982\u679c\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u5b66\u751f\uff0c\u5219\u5fc5\u987b\u58f0\u660e\u3001\u521d\u59cb\u5316\u4e00\u4e2a\u65b0\u53d8\u91cf\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230 totalScore \u8ba1\u7b97\u4e2d\u3002 \u6bcf\u6b21\u4fee\u6539\u65e7\u4ee3\u7801\u65f6\uff0c\u90fd\u6709\u5f15\u5165\u9519\u8bef\u7684\u98ce\u9669\u3002 \u4f7f\u7528\u6570\u7ec4\u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u597d\u7684\u89e3\u51b3\u65b9\u6848: 1 2 3 4 int scores []{ 84 , 92 , 76 , 81 , 56 }; int numStudents { static_cast < int > ( std :: size ( scores )) }; // \u9700\u8981 C++17 \u548c \u5934\u6587\u4ef6 int totalScore { scores [ 0 ] + scores [ 1 ] + scores [ 2 ] + scores [ 3 ] + scores [ 4 ] }; auto averageScore { static_cast < double > ( totalScore ) / numStudents }; \u8fd9\u4e48\u505a\u7684\u786e\u53ef\u4ee5\u5927\u5927\u51cf\u5c11\u58f0\u660e\u53d8\u91cf\u7684\u6570\u91cf\uff0c\u4f46 totalScore \u4ecd\u7136\u9700\u8981\u5355\u72ec\u5217\u51fa\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u3002\u5982\u4e0a\u6240\u8ff0\uff0c\u6539\u53d8\u5b66\u751f\u4eba\u6570\u610f\u5473\u7740\u9700\u8981\u624b\u52a8\u8c03\u6574\u603b\u6210\u7ee9\u516c\u5f0f\u3002 \u5982\u679c\u6709\u4e00\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5faa\u73af\u6211\u4eec\u7684\u6570\u7ec4\u5e76\u76f4\u63a5\u8ba1\u7b97 totalScore \u5c31\u597d\u4e86\u3002 \u6570\u7ec4\u7684\u5faa\u73af\u904d\u5386 \u5728\u4e0a\u4e00\u8282\u8bfe\u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u6570\u7ec4\u7684\u4e0b\u6807\u4e0d\u9700\u8981\u662f\u4e00\u4e2a\u5e38\u91cf\u503c\u2014\u2014\u5b83\u53ef\u4ee5\u662f\u4e00\u4e2a\u53d8\u91cf\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u53d8\u91cf\u4f5c\u4e3a\u6570\u7ec4\u7d22\u5f15\u6765\u5faa\u73af\u904d\u5386\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\uff0c\u5e76\u5bf9\u5b83\u4eec\u6267\u884c\u4e00\u4e9b\u8ba1\u7b97\u3002\u8fd9\u662f\u975e\u5e38\u5e38\u89c1\u7684\uff0c\u6570\u7ec4\u548c\u5faa\u73af\u51e0\u4e4e\u603b\u662f\u5f62\u5f71\u4e0d\u79bb\u5730\u51fa\u73b0\u3002\u4f7f\u7528\u5faa\u73af\u6765\u4f9d\u6b21\u8bbf\u95ee\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u901a\u5e38\u88ab\u79f0\u4e3a \u904d\u5386(iterating) \u6570\u7ec4\u3002 \u4f7f\u7528 for \u5faa\u73af\u6765\u91cd\u65b0\u7f16\u5199\u4e0a\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 constexpr int scores []{ 84 , 92 , 76 , 81 , 56 }; constexpr int numStudents { static_cast < int > ( std :: size ( scores )) }; // const int numStudents{ sizeof(scores) / sizeof(scores[0]) }; // use this instead if not C++17 capable int totalScore { 0 }; // use a loop to calculate totalScore for ( int student { 0 }; student < numStudents ; ++ student ) // \u8fd9\u91cc student{0} \u5176\u5b9e\u5c31\u662f i=0 totalScore += scores [ student ]; auto averageScore { static_cast < double > ( totalScore ) / numStudents }; \u8fd9\u79cd\u65b9\u6848\u5728\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u65b9\u9762\u90fd\u5f88\u7406\u60f3\u3002\u901a\u8fc7\u5faa\u73af\u6765\u9010\u4e2a\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\uff0c\u516c\u5f0f\u53ef\u4ee5\u6839\u636e\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u4e2a\u6570\u81ea\u52a8\u8c03\u6574\u3002\u8fd9\u610f\u5473\u7740\u5f53\u6709\u65b0\u7684\u5b66\u751f\u65f6\uff0c\u4e0d\u9700\u8981\u624b\u52a8\u6dfb\u52a0\u8be5\u5b66\u751f\u7684\u5206\u6570\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u5faa\u73af\u641c\u7d22\u6570\u7ec4\u7684\u4f8b\u5b50\uff0c\u4ee5\u786e\u5b9a\u73ed\u7ea7\u4e2d\u7684\u6700\u9ad8\u5206\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include // for std::size int main () { // scores are 0 (worst) to 100 (best) constexpr int scores []{ 84 , 92 , 76 , 81 , 56 }; constexpr int numStudents { static_cast < int > ( std :: size ( scores )) }; int maxScore { 0 }; // keep track of our largest score for ( int student { 0 }; student < numStudents ; ++ student ) { if ( scores [ student ] > maxScore ) { maxScore = scores [ student ]; } } std :: cout << \"The best score was \" << maxScore << '\\n' ; return 0 ; } \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u540d\u4e3a maxScore \u7684\u975e\u5faa\u73af\u53d8\u91cf\u6765\u8ddf\u8e2a\u5f53\u524d\u7684\u6700\u9ad8\u5206\u3002 maxScore \u88ab\u521d\u59cb\u5316\u4e3a 0 \u8868\u793a\u8fd8\u6ca1\u6709\u5b58\u50a8\u4efb\u4f55\u5206\u6570\u3002\u7136\u540e\uff0c\u904d\u5386\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u53d1\u73b0\u5206\u6570\u6bd4\u4e4b\u524d\u770b\u5230\u7684\u4efb\u4f55\u5206\u6570\u90fd\u9ad8\uff0c\u5219\u5c06 maxScore \u8bbe\u7f6e\u4e3a\u8be5\u503c\u3002\u56e0\u6b64\uff0c maxScore \u603b\u662f\u8868\u793a\u5f53\u524d\u7684\u6700\u5927\u503c\u3002\u5f53\u5230\u8fbe\u6570\u7ec4\u672b\u5c3e\u65f6\uff0c maxScore \u5c31\u662f\u6574\u4e2a\u6570\u7ec4\u4e2d\u7684\u6700\u9ad8\u5206\u3002 \u6570\u7ec4\u548c\u5faa\u73af \u6570\u7ec4\u7684\u5faa\u73af\u904d\u5386\u901a\u5e38\u88ab\u7528\u6765\u5b8c\u6210\u4e00\u4e0b\u4e09\u4ef6\u4e8b\uff1a \u8ba1\u7b97\u6570\u503c (\u4f8b\u5982\uff1a\u5e73\u5747\u503c\u6216\u603b\u548c)\uff1b \u67e5\u627e\u67d0\u4e2a\u503c\uff08\u4f8b\u5982\uff1a\u6700\u5927\u503c\u3001\u6700\u5c0f\u503c\uff09\uff1b \u6570\u7ec4\u6392\u5e8f\uff08\u4f8b\u5982\uff1a\u5347\u5e8f\u3001\u964d\u5e8f\uff09\u3002 \u5728\u8ba1\u7b97\u6570\u503c\u65f6\uff0c\u53d8\u91cf\u901a\u5e38\u7528\u4e8e\u4fdd\u5b58\u7528\u4e8e\u8ba1\u7b97\u6700\u7ec8\u503c\u7684\u4e2d\u95f4\u7ed3\u679c\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8ba1\u7b97\u4e00\u4e2a\u5e73\u5747\u5206\u6570\uff0c totalScore \u4fdd\u5b58\u5230\u76ee\u524d\u4e3a\u6b62\u6240\u68c0\u67e5\u7684\u6240\u6709\u5143\u7d20\u7684\u603b\u5206\u6570\u3002 \u5728\u67e5\u627e\u67d0\u4e2a\u503c\u662f\uff0c\u53d8\u91cf\u901a\u5e38\u7528\u6765\u4fdd\u5b58\u5230\u76ee\u524d\u4e3a\u6b62\u770b\u5230\u7684\u6700\u4f73\u5019\u9009\u503c(\u6216\u6700\u4f73\u5019\u9009\u503c\u7684\u7d22\u5f15)\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u6765\u67e5\u627e\u6700\u4f73\u5206\u6570\uff0c maxScore \u7528\u4e8e\u4fdd\u5b58\u5230\u76ee\u524d\u4e3a\u6b62\u9047\u5230\u7684\u6700\u9ad8\u5206\u6570\u3002 \u6570\u7ec4\u6392\u5e8f\u8981\u590d\u6742\u4e00\u4e9b\uff0c\u56e0\u4e3a\u5b83\u901a\u5e38\u6d89\u53ca\u5d4c\u5957\u5faa\u73af\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u4ecb\u7ecd\u6570\u7ec4\u6392\u5e8f\u3002 \u6570\u7ec4\u7684\u201d\u5dee\u4e00\u201c\u9519\u8bef \u5bf9\u6570\u7ec4\u4f7f\u7528\u5faa\u73af\u6700\u68d8\u624b\u7684\u95ee\u9898\u4e4b\u4e00\u662f\u786e\u5b9a\u6b63\u786e\u7684\u8fed\u4ee3\u6b21\u6570\uff0c\u5f88\u5bb9\u6613\u72af \u5dee\u4e00\u9519\u8bef(Off-by-one) \u3002\u8bbf\u95ee\u5927\u4e8e\u6570\u7ec4\u957f\u5ea6\u7684\u5143\u7d20\u53ef\u80fd\u4f1a\u4ea7\u751f\u53ef\u6015\u7684\u540e\u679c\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include int main () { constexpr int scores []{ 84 , 92 , 76 , 81 , 56 }; constexpr int numStudents { static_cast < int > ( std :: size ( scores )) }; int maxScore { 0 }; // keep track of our largest score for ( int student { 0 }; student <= numStudents ; ++ student ) { if ( scores [ student ] > maxScore ) { maxScore = scores [ student ]; } } std :: cout << \"The best score was \" << maxScore << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u7684\u95ee\u9898\u662f for \u5faa\u73af\u4e2d\u7684\u6761\u4ef6\u662f\u9519\u8bef\u7684\uff01\u6570\u7ec4\u53ea\u6709 5 \u4e2a\u5143\u7d20\uff0c\u7d22\u5f15\u4ece 0 \u5230 4 \u3002\u4f46\u662f\uff0c\u8fd9\u4e2a\u6570\u7ec4\u4ece 0 \u5faa\u73af\u5230 5 \u3002\u56e0\u6b64\uff0c\u5728\u6700\u540e\u4e00\u6b21\u8fed\u4ee3\u65f6\uff0c\u6570\u7ec4\u5c06\u6267\u884c\u5982\u4e0b\u8bed\u53e5: 1 2 3 4 if ( scores [ 5 ] > maxScore ) { maxScore = scores [ 5 ]; } \u4f46\u5b9e\u9645\u4e0a scores[5] \u662f\u672a\u5b9a\u4e49\u7684\uff01 \u8bbf\u95ee\u5b83\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u6700\u6709\u53ef\u80fd\u7684\u662f\u8bbf\u95ee scores[5] \u4f1a\u5f97\u5230\u4e00\u4e2a \u5783\u573e\u503c \uff0c\u6b64\u65f6 maxScore \u5c06\u662f\u9519\u8bef\u7684\u3002 \u7136\u800c\uff0c\u60f3\u8c61\u4e00\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u65e0\u610f\u4e2d\u7ed9 array[5] \u8d4b\u4e86\u4e00\u4e2a\u503c\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u6211\u4eec\u53ef\u80fd\u4f1a\u8986\u76d6\u53e6\u4e00\u4e2a\u53d8\u91cf(\u6216\u5b83\u7684\u4e00\u90e8\u5206)\uff0c\u6216\u8005\u53ef\u80fd\u7834\u574f\u67d0\u4e9b\u4e1c\u897f\u2014\u2014\u8fd9\u4e9b\u7c7b\u578b\u7684bug \u662f\u5f88\u96be\u5b9a\u4f4d\u7684\uff01 \u56e0\u6b64\uff0c\u5728\u5faa\u73af\u904d\u5386\u6570\u7ec4\u65f6\uff0c\u4e00\u5b9a\u8981\u53cd\u590d\u68c0\u67e5\u5faa\u73af\u6761\u4ef6\uff0c\u4ee5\u786e\u4fdd\u4e0d\u4f1a\u5f15\u5165 \u5dee\u4e00\u9519\u8bef \u3002","title":"11.3 - \u6570\u7ec4\u548c\u5faa\u73af"},{"location":"11-3-Arrays-and-loops/#_1","text":"\u5728\u4e0a\u4e00\u8282\u8bfe\u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u6570\u7ec4\u7684\u4e0b\u6807\u4e0d\u9700\u8981\u662f\u4e00\u4e2a\u5e38\u91cf\u503c\u2014\u2014\u5b83\u53ef\u4ee5\u662f\u4e00\u4e2a\u53d8\u91cf\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u53d8\u91cf\u4f5c\u4e3a\u6570\u7ec4\u7d22\u5f15\u6765\u5faa\u73af\u904d\u5386\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\uff0c\u5e76\u5bf9\u5b83\u4eec\u6267\u884c\u4e00\u4e9b\u8ba1\u7b97\u3002\u8fd9\u662f\u975e\u5e38\u5e38\u89c1\u7684\uff0c\u6570\u7ec4\u548c\u5faa\u73af\u51e0\u4e4e\u603b\u662f\u5f62\u5f71\u4e0d\u79bb\u5730\u51fa\u73b0\u3002\u4f7f\u7528\u5faa\u73af\u6765\u4f9d\u6b21\u8bbf\u95ee\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u901a\u5e38\u88ab\u79f0\u4e3a \u904d\u5386(iterating) \u6570\u7ec4\u3002 \u4f7f\u7528 for \u5faa\u73af\u6765\u91cd\u65b0\u7f16\u5199\u4e0a\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 constexpr int scores []{ 84 , 92 , 76 , 81 , 56 }; constexpr int numStudents { static_cast < int > ( std :: size ( scores )) }; // const int numStudents{ sizeof(scores) / sizeof(scores[0]) }; // use this instead if not C++17 capable int totalScore { 0 }; // use a loop to calculate totalScore for ( int student { 0 }; student < numStudents ; ++ student ) // \u8fd9\u91cc student{0} \u5176\u5b9e\u5c31\u662f i=0 totalScore += scores [ student ]; auto averageScore { static_cast < double > ( totalScore ) / numStudents }; \u8fd9\u79cd\u65b9\u6848\u5728\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u65b9\u9762\u90fd\u5f88\u7406\u60f3\u3002\u901a\u8fc7\u5faa\u73af\u6765\u9010\u4e2a\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\uff0c\u516c\u5f0f\u53ef\u4ee5\u6839\u636e\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u4e2a\u6570\u81ea\u52a8\u8c03\u6574\u3002\u8fd9\u610f\u5473\u7740\u5f53\u6709\u65b0\u7684\u5b66\u751f\u65f6\uff0c\u4e0d\u9700\u8981\u624b\u52a8\u6dfb\u52a0\u8be5\u5b66\u751f\u7684\u5206\u6570\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u5faa\u73af\u641c\u7d22\u6570\u7ec4\u7684\u4f8b\u5b50\uff0c\u4ee5\u786e\u5b9a\u73ed\u7ea7\u4e2d\u7684\u6700\u9ad8\u5206\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include // for std::size int main () { // scores are 0 (worst) to 100 (best) constexpr int scores []{ 84 , 92 , 76 , 81 , 56 }; constexpr int numStudents { static_cast < int > ( std :: size ( scores )) }; int maxScore { 0 }; // keep track of our largest score for ( int student { 0 }; student < numStudents ; ++ student ) { if ( scores [ student ] > maxScore ) { maxScore = scores [ student ]; } } std :: cout << \"The best score was \" << maxScore << '\\n' ; return 0 ; } \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u540d\u4e3a maxScore \u7684\u975e\u5faa\u73af\u53d8\u91cf\u6765\u8ddf\u8e2a\u5f53\u524d\u7684\u6700\u9ad8\u5206\u3002 maxScore \u88ab\u521d\u59cb\u5316\u4e3a 0 \u8868\u793a\u8fd8\u6ca1\u6709\u5b58\u50a8\u4efb\u4f55\u5206\u6570\u3002\u7136\u540e\uff0c\u904d\u5386\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u53d1\u73b0\u5206\u6570\u6bd4\u4e4b\u524d\u770b\u5230\u7684\u4efb\u4f55\u5206\u6570\u90fd\u9ad8\uff0c\u5219\u5c06 maxScore \u8bbe\u7f6e\u4e3a\u8be5\u503c\u3002\u56e0\u6b64\uff0c maxScore \u603b\u662f\u8868\u793a\u5f53\u524d\u7684\u6700\u5927\u503c\u3002\u5f53\u5230\u8fbe\u6570\u7ec4\u672b\u5c3e\u65f6\uff0c maxScore \u5c31\u662f\u6574\u4e2a\u6570\u7ec4\u4e2d\u7684\u6700\u9ad8\u5206\u3002","title":"\u6570\u7ec4\u7684\u5faa\u73af\u904d\u5386"},{"location":"11-3-Arrays-and-loops/#_2","text":"\u6570\u7ec4\u7684\u5faa\u73af\u904d\u5386\u901a\u5e38\u88ab\u7528\u6765\u5b8c\u6210\u4e00\u4e0b\u4e09\u4ef6\u4e8b\uff1a \u8ba1\u7b97\u6570\u503c (\u4f8b\u5982\uff1a\u5e73\u5747\u503c\u6216\u603b\u548c)\uff1b \u67e5\u627e\u67d0\u4e2a\u503c\uff08\u4f8b\u5982\uff1a\u6700\u5927\u503c\u3001\u6700\u5c0f\u503c\uff09\uff1b \u6570\u7ec4\u6392\u5e8f\uff08\u4f8b\u5982\uff1a\u5347\u5e8f\u3001\u964d\u5e8f\uff09\u3002 \u5728\u8ba1\u7b97\u6570\u503c\u65f6\uff0c\u53d8\u91cf\u901a\u5e38\u7528\u4e8e\u4fdd\u5b58\u7528\u4e8e\u8ba1\u7b97\u6700\u7ec8\u503c\u7684\u4e2d\u95f4\u7ed3\u679c\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8ba1\u7b97\u4e00\u4e2a\u5e73\u5747\u5206\u6570\uff0c totalScore \u4fdd\u5b58\u5230\u76ee\u524d\u4e3a\u6b62\u6240\u68c0\u67e5\u7684\u6240\u6709\u5143\u7d20\u7684\u603b\u5206\u6570\u3002 \u5728\u67e5\u627e\u67d0\u4e2a\u503c\u662f\uff0c\u53d8\u91cf\u901a\u5e38\u7528\u6765\u4fdd\u5b58\u5230\u76ee\u524d\u4e3a\u6b62\u770b\u5230\u7684\u6700\u4f73\u5019\u9009\u503c(\u6216\u6700\u4f73\u5019\u9009\u503c\u7684\u7d22\u5f15)\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u6765\u67e5\u627e\u6700\u4f73\u5206\u6570\uff0c maxScore \u7528\u4e8e\u4fdd\u5b58\u5230\u76ee\u524d\u4e3a\u6b62\u9047\u5230\u7684\u6700\u9ad8\u5206\u6570\u3002 \u6570\u7ec4\u6392\u5e8f\u8981\u590d\u6742\u4e00\u4e9b\uff0c\u56e0\u4e3a\u5b83\u901a\u5e38\u6d89\u53ca\u5d4c\u5957\u5faa\u73af\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u4ecb\u7ecd\u6570\u7ec4\u6392\u5e8f\u3002","title":"\u6570\u7ec4\u548c\u5faa\u73af"},{"location":"11-3-Arrays-and-loops/#_3","text":"\u5bf9\u6570\u7ec4\u4f7f\u7528\u5faa\u73af\u6700\u68d8\u624b\u7684\u95ee\u9898\u4e4b\u4e00\u662f\u786e\u5b9a\u6b63\u786e\u7684\u8fed\u4ee3\u6b21\u6570\uff0c\u5f88\u5bb9\u6613\u72af \u5dee\u4e00\u9519\u8bef(Off-by-one) \u3002\u8bbf\u95ee\u5927\u4e8e\u6570\u7ec4\u957f\u5ea6\u7684\u5143\u7d20\u53ef\u80fd\u4f1a\u4ea7\u751f\u53ef\u6015\u7684\u540e\u679c\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include int main () { constexpr int scores []{ 84 , 92 , 76 , 81 , 56 }; constexpr int numStudents { static_cast < int > ( std :: size ( scores )) }; int maxScore { 0 }; // keep track of our largest score for ( int student { 0 }; student <= numStudents ; ++ student ) { if ( scores [ student ] > maxScore ) { maxScore = scores [ student ]; } } std :: cout << \"The best score was \" << maxScore << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u7684\u95ee\u9898\u662f for \u5faa\u73af\u4e2d\u7684\u6761\u4ef6\u662f\u9519\u8bef\u7684\uff01\u6570\u7ec4\u53ea\u6709 5 \u4e2a\u5143\u7d20\uff0c\u7d22\u5f15\u4ece 0 \u5230 4 \u3002\u4f46\u662f\uff0c\u8fd9\u4e2a\u6570\u7ec4\u4ece 0 \u5faa\u73af\u5230 5 \u3002\u56e0\u6b64\uff0c\u5728\u6700\u540e\u4e00\u6b21\u8fed\u4ee3\u65f6\uff0c\u6570\u7ec4\u5c06\u6267\u884c\u5982\u4e0b\u8bed\u53e5: 1 2 3 4 if ( scores [ 5 ] > maxScore ) { maxScore = scores [ 5 ]; } \u4f46\u5b9e\u9645\u4e0a scores[5] \u662f\u672a\u5b9a\u4e49\u7684\uff01 \u8bbf\u95ee\u5b83\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u6700\u6709\u53ef\u80fd\u7684\u662f\u8bbf\u95ee scores[5] \u4f1a\u5f97\u5230\u4e00\u4e2a \u5783\u573e\u503c \uff0c\u6b64\u65f6 maxScore \u5c06\u662f\u9519\u8bef\u7684\u3002 \u7136\u800c\uff0c\u60f3\u8c61\u4e00\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u65e0\u610f\u4e2d\u7ed9 array[5] \u8d4b\u4e86\u4e00\u4e2a\u503c\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u6211\u4eec\u53ef\u80fd\u4f1a\u8986\u76d6\u53e6\u4e00\u4e2a\u53d8\u91cf(\u6216\u5b83\u7684\u4e00\u90e8\u5206)\uff0c\u6216\u8005\u53ef\u80fd\u7834\u574f\u67d0\u4e9b\u4e1c\u897f\u2014\u2014\u8fd9\u4e9b\u7c7b\u578b\u7684bug \u662f\u5f88\u96be\u5b9a\u4f4d\u7684\uff01 \u56e0\u6b64\uff0c\u5728\u5faa\u73af\u904d\u5386\u6570\u7ec4\u65f6\uff0c\u4e00\u5b9a\u8981\u53cd\u590d\u68c0\u67e5\u5faa\u73af\u6761\u4ef6\uff0c\u4ee5\u786e\u4fdd\u4e0d\u4f1a\u5f15\u5165 \u5dee\u4e00\u9519\u8bef \u3002","title":"\u6570\u7ec4\u7684\u201d\u5dee\u4e00\u201c\u9519\u8bef"},{"location":"11-4-Sorting-an-array-using-selection-sort/","tags":["array","sort"],"text":"Key Takeaway \u6392\u5e8f \u6570\u7ec4\u6392\u5e8f\u662f\u5c06\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u6309\u7279\u5b9a\u987a\u5e8f\u6392\u5217\u7684\u8fc7\u7a0b\u3002\u6392\u5e8f\u5728\u5f88\u591a\u65f6\u5019\u90fd\u662f\u6709\u7528\u7684\u3002\u4f8b\u5982\uff0c\u7535\u5b50\u90ae\u4ef6\u7a0b\u5e8f\u901a\u5e38\u9700\u8981\u6309\u7167\u6536\u4ef6\u65f6\u95f4\u987a\u5e8f\u663e\u793a\u7535\u5b50\u90ae\u4ef6\uff0c\u56e0\u4e3a\u6700\u65b0\u7684\u7535\u5b50\u90ae\u4ef6\u901a\u5e38\u88ab\u8ba4\u4e3a\u5f53\u524d\u9700\u8981\u5173\u6ce8\u7684\u3002\u5f53\u4f60\u6253\u5f00\u4f60\u7684\u8054\u7cfb\u4eba\u5217\u8868\u65f6\uff0c\u540d\u5b57\u901a\u5e38\u662f\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u5217\u7684\uff0c\u56e0\u4e3a\u8fd9\u6837\u66f4\u5bb9\u6613\u627e\u5230\u4f60\u8981\u627e\u7684\u540d\u5b57\u3002\u8fd9\u4e24\u4e2a\u4f8b\u5b50\u90fd\u6d89\u53ca\u5728\u663e\u793a\u6570\u636e\u4e4b\u524d\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\u3002 \u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u53ef\u4ee5\u63d0\u9ad8\u641c\u7d22\u6570\u7ec4\u7684\u6548\u7387\uff0c\u4e0d\u4ec5\u5bf9\u4eba\u7c7b\uff0c\u5bf9\u8ba1\u7b97\u673a\u4e5f\u662f\u5982\u6b64\u3002\u4f8b\u5982\uff0c\u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u60f3\u77e5\u9053\u4e00\u4e2a\u540d\u5b57\u662f\u5426\u51fa\u73b0\u5728\u4e00\u4e2a\u540d\u5b57\u5217\u8868\u4e2d\u3002\u4e3a\u4e86\u67e5\u770b\u540d\u79f0\u662f\u5426\u5728\u5217\u8868\u4e2d\uff0c\u6211\u4eec\u5fc5\u987b\u68c0\u67e5\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u4ee5\u786e\u5b9a\u540d\u79f0\u662f\u5426\u51fa\u73b0\u3002\u5bf9\u4e8e\u5305\u542b\u8bb8\u591a\u5143\u7d20\u7684\u6570\u7ec4\uff0c\u641c\u7d22\u6240\u6709\u5143\u7d20\u7684\u5f00\u9500\u53ef\u80fd\u5f88\u5927\u3002 \u4f46\u662f\uff0c\u73b0\u5728\u5047\u8bbe\u6211\u4eec\u7684\u540d\u79f0\u6570\u7ec4\u662f\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u5e8f\u7684\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u53ea\u9700\u8981\u641c\u7d22\u5230\u5728\u5b57\u6bcd\u987a\u5e8f\u4e0a\u6bd4\u6211\u4eec\u8981\u67e5\u627e\u7684\u540d\u79f0\u5927\u7684\u540d\u79f0\u3002\u8fd9\u65f6\uff0c\u5982\u679c\u6211\u4eec\u8fd8\u6ca1\u6709\u627e\u5230\u8fd9\u4e2a\u540d\u5b57\uff0c\u6211\u4eec\u5c31\u77e5\u9053\u5b83\u4e0d\u5b58\u5728\u4e8e\u6570\u7ec4\u7684\u5176\u4ed6\u90e8\u5206\uff0c\u56e0\u4e3a\u6211\u4eec\u5728\u6570\u7ec4\u4e2d\u6ca1\u6709\u770b\u5230\u7684\u6240\u6709\u540d\u5b57\u90fd\u4fdd\u8bc1\u5728\u5b57\u6bcd\u987a\u5e8f\u4e0a\u66f4\u5927! \u5176\u5b9e\u8fd8\u6709\u66f4\u597d\u7684\u7b97\u6cd5\u6765\u641c\u7d22\u6392\u5e8f\u6570\u7ec4\u3002\u4f7f\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u641c\u7d22\u4e00\u4e2a\u5305\u542b1,000,000\u4e2a\u5143\u7d20\u7684\u6392\u5e8f\u6570\u7ec4\uff0c\u53ea\u9700\u8981\u8fdb\u884c20\u6b21\u6bd4\u8f83!\u5f53\u7136\uff0c\u5176\u7f3a\u70b9\u662f\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u7684\u4ee3\u4ef7\u76f8\u5bf9\u8f83\u9ad8\uff0c\u800c\u4e14\u901a\u5e38\u4e0d\u503c\u5f97\u4e3a\u4e86\u5feb\u901f\u641c\u7d22\u800c\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u9664\u975e\u60a8\u8981\u591a\u6b21\u641c\u7d22\u5b83\u3002 \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u540e\u5c31\u4e0d\u9700\u8981\u518d\u641c\u7d22\u4e86\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u627e\u5230\u6700\u597d\u7684\u8003\u8bd5\u6210\u7ee9\u3002\u5982\u679c\u6570\u7ec4\u662f\u65e0\u5e8f\u7684\uff0c\u6211\u4eec\u5fc5\u987b\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u4ee5\u627e\u5230\u6700\u5927\u7684\u6d4b\u8bd5\u5206\u6570\u3002\u5982\u679c\u5217\u8868\u662f\u6392\u5e8f\u7684\uff0c\u6700\u597d\u7684\u6d4b\u8bd5\u5206\u6570\u5c06\u5728\u7b2c\u4e00\u4e2a\u6216\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e(\u53d6\u51b3\u4e8e\u6211\u4eec\u662f\u6309\u5347\u5e8f\u8fd8\u662f\u964d\u5e8f\u6392\u5e8f)\uff0c\u6240\u4ee5\u6211\u4eec\u6839\u672c\u4e0d\u9700\u8981\u641c\u7d22! \u6392\u5e8f\u7684\u539f\u7406 \u6392\u5e8f\u901a\u5e38\u662f\u901a\u8fc7\u53cd\u590d\u6bd4\u8f83\u6570\u7ec4\u5143\u7d20\u5bf9\u6765\u6267\u884c\u7684\uff0c\u5982\u679c\u5b83\u4eec\u6ee1\u8db3\u67d0\u4e9b\u9884\u5b9a\u4e49\u7684\u6761\u4ef6\uff0c\u5219\u4ea4\u6362\u5b83\u4eec\u3002\u6839\u636e\u4f7f\u7528\u7684\u6392\u5e8f\u7b97\u6cd5\u7684\u4e0d\u540c\uff0c\u6bd4\u8f83\u8fd9\u4e9b\u5143\u7d20\u7684\u987a\u5e8f\u4e5f\u4e0d\u540c\u3002\u6807\u51c6\u53d6\u51b3\u4e8e\u5217\u8868\u5c06\u5982\u4f55\u6392\u5e8f(\u4f8b\u5982\uff0c\u5347\u5e8f\u6216\u964d\u5e8f)\u3002 \u8981\u4ea4\u6362\u4e24\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528c++\u6807\u51c6\u5e93\u4e2d\u7684 std::swap() \u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5728 \u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include int main () { int x { 2 }; int y { 4 }; std :: cout << \"Before swap: x = \" << x << \", y = \" << y << '\\n' ; std :: swap ( x , y ); // swap the values of x and y std :: cout << \"After swap: x = \" << x << \", y = \" << y << '\\n' ; return 0 ; } \u7a0b\u5e8f\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Before swap: x = 2, y = 4 After swap: x = 4, y = 2 \u6ce8\u610f\uff0c\u4f7f\u7528 swap \u540e\uff0c x \u548c y \u7684\u503c\u5c31\u88ab\u4ea4\u6362\u4e86\uff01 \u9009\u62e9\u6392\u5e8f \u6709\u5f88\u591a\u65b9\u6cd5\u53ef\u4ee5\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002 \u9009\u62e9\u6392\u5e8f \u53ef\u80fd\u662f\u6700\u5bb9\u6613\u7406\u89e3\u7684\u6392\u5e8f\uff0c\u8fd9\u4f7f\u5b83\u5f88\u9002\u5408\u4f5c\u4e3a\u6559\u5b66\u793a\u4f8b\uff08\u5c3d\u7ba1\u5b83\u7684\u6392\u5e8f\u901f\u5ea6\u5e76\u4e0d\u5feb\uff09\u3002 \u9009\u62e9\u6392\u5e8f\u7684\u6b65\u9aa4\u5982\u4e0b\uff08\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\uff09\uff1a \u4ece\u7d22\u5f15 0\u5f00\u59cb\uff0c\u904d\u5386\u6570\u7ec4\u67e5\u627e\u6700\u5c0f\u7684\u503c\uff1b \u4ea4\u6362\u6700\u5c0f\u503c\u548c\u7d22\u5f15\u4f4d\u7f6e\u4e3a 0 \u7684\u5143\u7d20\uff1b \u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u7d22\u5f15\u91cd\u590d\u4e0a\u8ff0\u6b65\u9aa4\u3002 \u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u8981\u627e\u5230\u6570\u7ec4\u4e2d\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u7136\u540e\u628a\u5b83\u4ea4\u6362\u5230\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u3002\u7136\u540e\u6211\u4eec\u8981\u627e\u5230\u4e0b\u4e00\u4e2a\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u7136\u540e\u628a\u5b83\u4ea4\u6362\u5230\u7b2c\u4e8c\u4e2a\u4f4d\u7f6e\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5c06\u91cd\u590d\u8fdb\u884c\uff0c\u76f4\u5230\u8017\u5c3d\u6240\u6709\u5143\u7d20\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u5173\u4e8e5\u4e2a\u5143\u7d20\u7684\u7b97\u6cd5\u7684\u4f8b\u5b50\u3002\u8ba9\u6211\u4eec\u4ece\u4e00\u4e2a\u793a\u4f8b\u6570\u7ec4\u5f00\u59cb { 30, 50, 20, 10, 40 } \u4ece\u7d22\u5f15 0 \u5f00\u59cb\uff0c\u627e\u5230\u6700\u5c0f\u7684\u5143\u7d20 { 30, 50, 20, 10 , 40 } \u4ea4\u6362\u5230\u7d22\u5f15 0 \u7684\u4f4d\u7f6e { 10 , 50, 20, 30 , 40 } \u7b2c\u4e00\u4e2a\u5143\u7d20\u5df2\u7ecf\u6392\u5e8f\u5b8c\u6210\uff0c\u540e\u9762\u5c31\u4e0d\u7528\u7ba1\u4e86\u3002\u7136\u540e\u4ece\u7d22\u5f15 1 \u5f00\u59cb\uff0c\u7ee7\u7eed\u67e5\u627e\u6700\u5c0f\u7684\u5143\u7d20\uff1a { 10 , 50, 20 , 30, 40 } \u5c06\u627e\u5230\u7684\u503c\u4ea4\u6362\u5230\u7d22\u5f15 1 \u7684\u4f4d\u7f6e\uff1a { 10 , 20 , 50 , 30, 40 } \u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u5ffd\u7565\u524d\u4e24\u4e2a\u5143\u7d20\u3002\u627e\u5230\u4ece\u7d22\u5f152\u5f00\u59cb\u7684\u6700\u5c0f\u5143\u7d20\uff1a { 10 , 20 , 50, 30 , 40 } \u7136\u540e\u5c06\u5b83\u4e0e\u7d22\u5f152\u4e2d\u7684\u5143\u7d20\u4ea4\u6362\uff1a { 10 , 20 , 30 , 50 , 40 } \u627e\u5230\u4ece\u7d22\u5f153\u5f00\u59cb\u7684\u6700\u5c0f\u5143\u7d20\uff1a { 10 , 20 , 30 , 50, 40 } \u7136\u540e\u5c06\u5b83\u4e0e\u7d22\u5f153\u4e2d\u7684\u5143\u7d20\u4ea4\u6362\uff1a { 10 , 20 , 30 , 40 , 50 } \u6700\u540e\uff0c\u627e\u5230\u4ece\u7d22\u5f154\u5f00\u59cb\u7684\u6700\u5c0f\u5143\u7d20\uff1a { 10 , 20 , 30 , 40 , 50 } \u7136\u540e\u5c06\u5b83\u4e0e\u7d22\u5f154\u4e2d\u7684\u5143\u7d20\u4ea4\u6362(\u5b9e\u9645\u4e0a\u4ec0\u4e48\u4e5f\u6ca1\u505a)\uff1a { 10 , 20 , 30 , 40 , 50 } \u5b8c\u6210\uff01 { 10, 20, 30, 40, 50 } \u6ce8\u610f\uff0c\u6700\u540e\u4e00\u6b21\u6bd4\u8f83\u603b\u662f\u4e0e\u81ea\u8eab\u8fdb\u884c\u6bd4\u8f83(\u8fd9\u662f\u5197\u4f59\u7684)\uff0c\u56e0\u6b64\u6211\u4eec\u5b9e\u9645\u4e0a\u53ef\u4ee5\u5728\u6570\u7ec4\u7ed3\u675f\u524d\u505c\u6b621\u4e2a\u5143\u7d20\u3002 C++ \u4e2d\u7684\u9009\u62e9\u6392\u5e8f C++ \u7684\u5b9e\u73b0\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include #include #include int main () { int array []{ 30 , 50 , 20 , 10 , 40 }; constexpr int length { static_cast < int > ( std :: size ( array )) }; // Step through each element of the array // (except the last one, which will already be sorted by the time we get there) for ( int startIndex { 0 }; startIndex < length - 1 ; ++ startIndex ) { // smallestIndex is the index of the smallest element we\u2019ve encountered this iteration // Start by assuming the smallest element is the first element of this iteration int smallestIndex { startIndex }; // Then look for a smaller element in the rest of the array for ( int currentIndex { startIndex + 1 }; currentIndex < length ; ++ currentIndex ) { // If we've found an element that is smaller than our previously found smallest if ( array [ currentIndex ] < array [ smallestIndex ]) // then keep track of it smallestIndex = currentIndex ; } // smallestIndex is now the smallest element in the remaining array // swap our start element with our smallest element (this sorts it into the correct place) std :: swap ( array [ startIndex ], array [ smallestIndex ]); } // Now that the whole array is sorted, print our sorted array as proof it works for ( int index { 0 }; index < length ; ++ index ) std :: cout << array [ index ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7b97\u6cd5\u6700\u4ee4\u4eba\u56f0\u60d1\u7684\u90e8\u5206\u662f\u4e00\u4e2a\u5faa\u73af\u4e2d\u7684\u5faa\u73af(\u79f0\u4e3a \u5d4c\u5957\u5faa\u73af )\u3002\u5916\u5c42\u5faa\u73af(startIndex)\u9010\u4e2a\u904d\u5386\u6bcf\u4e2a\u5143\u7d20\u3002\u5bf9\u4e8e\u5916\u5c42\u5faa\u73af\u7684\u6bcf\u6b21\u8fed\u4ee3\uff0c\u4f7f\u7528\u5185\u90e8\u5faa\u73af(currentIndex)\u67e5\u627e\u5269\u4f59\u6570\u7ec4\u4e2d\u6700\u5c0f\u7684\u5143\u7d20(\u4ecestartIndex+1\u5f00\u59cb)\u3002smallstindex\u8ddf\u8e2a\u5185\u90e8\u5faa\u73af\u627e\u5230\u7684\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\u3002\u7136\u540e\u7528startIndex\u4ea4\u6362smallstindex\u3002\u6700\u540e\uff0c\u5916\u5c42\u5faa\u73af(startIndex)\u524d\u8fdb\u4e00\u4e2a\u5143\u7d20\uff0c\u7136\u540e\u91cd\u590d\u8fd9\u4e2a\u8fc7\u7a0b\u3002 \u63d0\u793a:\u5982\u679c\u60a8\u5728\u5f04\u6e05\u695a\u4e0a\u8ff0\u7a0b\u5e8f\u5982\u4f55\u5de5\u4f5c\u65b9\u9762\u6709\u56f0\u96be\uff0c\u90a3\u4e48\u5728\u4e00\u5f20\u7eb8\u4e0a\u5b8c\u6210\u4e00\u4e2a\u793a\u4f8b\u6848\u4f8b\u4f1a\u5f88\u6709\u5e2e\u52a9\u3002\u5728\u7eb8\u5f20\u7684\u9876\u90e8\u6c34\u5e73\u5199\u5165\u8d77\u59cb(\u672a\u6392\u5e8f)\u6570\u7ec4\u5143\u7d20\u3002\u753b\u7bad\u5934\u8868\u793astartIndex\u3001currentIndex\u548csmallstindex\u6b63\u5728\u7d22\u5f15\u54ea\u4e9b\u5143\u7d20\u3002\u624b\u52a8\u8ddf\u8e2a\u6574\u4e2a\u7a0b\u5e8f\uff0c\u5e76\u5728\u7d22\u5f15\u53d8\u5316\u65f6\u91cd\u65b0\u7ed8\u5236\u7bad\u5934\u3002\u5bf9\u4e8e\u5916\u5c42\u5faa\u73af\u7684\u6bcf\u6b21\u8fed\u4ee3\uff0c\u5f00\u59cb\u4e00\u4e2a\u65b0\u884c\uff0c\u663e\u793a\u6570\u7ec4\u7684\u5f53\u524d\u72b6\u6001\u3002 \u540d\u79f0\u7684\u6392\u5e8f\u4f7f\u7528\u76f8\u540c\u7684\u7b97\u6cd5\u3002\u53ea\u9700\u5c06\u6570\u7ec4\u7c7b\u578b\u4ece int \u6539\u4e3a std::string \uff0c\u5e76\u4f7f\u7528\u9002\u5f53\u7684\u503c\u521d\u59cb\u5316\u5373\u53ef\u3002 std::sort \u56e0\u4e3a\u6570\u7ec4\u7684\u6392\u5e8f\u975e\u5e38\u5e38\u89c1\uff0cC++\u6807\u51c6\u5e93\u4e2d\u5305\u542b\u4e86\u4e00\u4e2a\u540d\u4e3a std::sort \u7684\u6392\u5e8f\u51fd\u6570\u3002 std::sort \u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u50cf\u8fd9\u6837\u5728\u6570\u7ec4\u4e2d\u8c03\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // for std::sort #include #include // for std::size int main () { int array []{ 30 , 50 , 20 , 10 , 40 }; std :: sort ( std :: begin ( array ), std :: end ( array )); for ( int i { 0 }; i < static_cast < int > ( std :: size ( array )); ++ i ) std :: cout << array [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c std::sort \u6309\u5347\u5e8f\u6392\u5e8f\uff0c\u4f7f\u7528 operator< \u6bd4\u8f83\u5143\u7d20\u5bf9\uff0c\u5e76\u5728\u5fc5\u8981\u65f6\u4ea4\u6362\u5b83\u4eec(\u5f88\u50cf\u4e0a\u9762\u7684\u9009\u62e9\u6392\u5e8f\u793a\u4f8b\u6240\u505a\u7684)\u3002 \u6211\u4eec\u5c06\u5728\u4ee5\u540e\u7684\u7ae0\u8282\u4e2d\u66f4\u591a\u5730\u8ba8\u8bba std::sort \u3002","title":"11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f"},{"location":"11-4-Sorting-an-array-using-selection-sort/#_1","text":"\u6570\u7ec4\u6392\u5e8f\u662f\u5c06\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u6309\u7279\u5b9a\u987a\u5e8f\u6392\u5217\u7684\u8fc7\u7a0b\u3002\u6392\u5e8f\u5728\u5f88\u591a\u65f6\u5019\u90fd\u662f\u6709\u7528\u7684\u3002\u4f8b\u5982\uff0c\u7535\u5b50\u90ae\u4ef6\u7a0b\u5e8f\u901a\u5e38\u9700\u8981\u6309\u7167\u6536\u4ef6\u65f6\u95f4\u987a\u5e8f\u663e\u793a\u7535\u5b50\u90ae\u4ef6\uff0c\u56e0\u4e3a\u6700\u65b0\u7684\u7535\u5b50\u90ae\u4ef6\u901a\u5e38\u88ab\u8ba4\u4e3a\u5f53\u524d\u9700\u8981\u5173\u6ce8\u7684\u3002\u5f53\u4f60\u6253\u5f00\u4f60\u7684\u8054\u7cfb\u4eba\u5217\u8868\u65f6\uff0c\u540d\u5b57\u901a\u5e38\u662f\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u5217\u7684\uff0c\u56e0\u4e3a\u8fd9\u6837\u66f4\u5bb9\u6613\u627e\u5230\u4f60\u8981\u627e\u7684\u540d\u5b57\u3002\u8fd9\u4e24\u4e2a\u4f8b\u5b50\u90fd\u6d89\u53ca\u5728\u663e\u793a\u6570\u636e\u4e4b\u524d\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\u3002 \u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u53ef\u4ee5\u63d0\u9ad8\u641c\u7d22\u6570\u7ec4\u7684\u6548\u7387\uff0c\u4e0d\u4ec5\u5bf9\u4eba\u7c7b\uff0c\u5bf9\u8ba1\u7b97\u673a\u4e5f\u662f\u5982\u6b64\u3002\u4f8b\u5982\uff0c\u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u60f3\u77e5\u9053\u4e00\u4e2a\u540d\u5b57\u662f\u5426\u51fa\u73b0\u5728\u4e00\u4e2a\u540d\u5b57\u5217\u8868\u4e2d\u3002\u4e3a\u4e86\u67e5\u770b\u540d\u79f0\u662f\u5426\u5728\u5217\u8868\u4e2d\uff0c\u6211\u4eec\u5fc5\u987b\u68c0\u67e5\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u4ee5\u786e\u5b9a\u540d\u79f0\u662f\u5426\u51fa\u73b0\u3002\u5bf9\u4e8e\u5305\u542b\u8bb8\u591a\u5143\u7d20\u7684\u6570\u7ec4\uff0c\u641c\u7d22\u6240\u6709\u5143\u7d20\u7684\u5f00\u9500\u53ef\u80fd\u5f88\u5927\u3002 \u4f46\u662f\uff0c\u73b0\u5728\u5047\u8bbe\u6211\u4eec\u7684\u540d\u79f0\u6570\u7ec4\u662f\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u5e8f\u7684\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u53ea\u9700\u8981\u641c\u7d22\u5230\u5728\u5b57\u6bcd\u987a\u5e8f\u4e0a\u6bd4\u6211\u4eec\u8981\u67e5\u627e\u7684\u540d\u79f0\u5927\u7684\u540d\u79f0\u3002\u8fd9\u65f6\uff0c\u5982\u679c\u6211\u4eec\u8fd8\u6ca1\u6709\u627e\u5230\u8fd9\u4e2a\u540d\u5b57\uff0c\u6211\u4eec\u5c31\u77e5\u9053\u5b83\u4e0d\u5b58\u5728\u4e8e\u6570\u7ec4\u7684\u5176\u4ed6\u90e8\u5206\uff0c\u56e0\u4e3a\u6211\u4eec\u5728\u6570\u7ec4\u4e2d\u6ca1\u6709\u770b\u5230\u7684\u6240\u6709\u540d\u5b57\u90fd\u4fdd\u8bc1\u5728\u5b57\u6bcd\u987a\u5e8f\u4e0a\u66f4\u5927! \u5176\u5b9e\u8fd8\u6709\u66f4\u597d\u7684\u7b97\u6cd5\u6765\u641c\u7d22\u6392\u5e8f\u6570\u7ec4\u3002\u4f7f\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u641c\u7d22\u4e00\u4e2a\u5305\u542b1,000,000\u4e2a\u5143\u7d20\u7684\u6392\u5e8f\u6570\u7ec4\uff0c\u53ea\u9700\u8981\u8fdb\u884c20\u6b21\u6bd4\u8f83!\u5f53\u7136\uff0c\u5176\u7f3a\u70b9\u662f\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u7684\u4ee3\u4ef7\u76f8\u5bf9\u8f83\u9ad8\uff0c\u800c\u4e14\u901a\u5e38\u4e0d\u503c\u5f97\u4e3a\u4e86\u5feb\u901f\u641c\u7d22\u800c\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u9664\u975e\u60a8\u8981\u591a\u6b21\u641c\u7d22\u5b83\u3002 \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u540e\u5c31\u4e0d\u9700\u8981\u518d\u641c\u7d22\u4e86\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u627e\u5230\u6700\u597d\u7684\u8003\u8bd5\u6210\u7ee9\u3002\u5982\u679c\u6570\u7ec4\u662f\u65e0\u5e8f\u7684\uff0c\u6211\u4eec\u5fc5\u987b\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u4ee5\u627e\u5230\u6700\u5927\u7684\u6d4b\u8bd5\u5206\u6570\u3002\u5982\u679c\u5217\u8868\u662f\u6392\u5e8f\u7684\uff0c\u6700\u597d\u7684\u6d4b\u8bd5\u5206\u6570\u5c06\u5728\u7b2c\u4e00\u4e2a\u6216\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e(\u53d6\u51b3\u4e8e\u6211\u4eec\u662f\u6309\u5347\u5e8f\u8fd8\u662f\u964d\u5e8f\u6392\u5e8f)\uff0c\u6240\u4ee5\u6211\u4eec\u6839\u672c\u4e0d\u9700\u8981\u641c\u7d22!","title":"\u6392\u5e8f"},{"location":"11-4-Sorting-an-array-using-selection-sort/#_2","text":"\u6392\u5e8f\u901a\u5e38\u662f\u901a\u8fc7\u53cd\u590d\u6bd4\u8f83\u6570\u7ec4\u5143\u7d20\u5bf9\u6765\u6267\u884c\u7684\uff0c\u5982\u679c\u5b83\u4eec\u6ee1\u8db3\u67d0\u4e9b\u9884\u5b9a\u4e49\u7684\u6761\u4ef6\uff0c\u5219\u4ea4\u6362\u5b83\u4eec\u3002\u6839\u636e\u4f7f\u7528\u7684\u6392\u5e8f\u7b97\u6cd5\u7684\u4e0d\u540c\uff0c\u6bd4\u8f83\u8fd9\u4e9b\u5143\u7d20\u7684\u987a\u5e8f\u4e5f\u4e0d\u540c\u3002\u6807\u51c6\u53d6\u51b3\u4e8e\u5217\u8868\u5c06\u5982\u4f55\u6392\u5e8f(\u4f8b\u5982\uff0c\u5347\u5e8f\u6216\u964d\u5e8f)\u3002 \u8981\u4ea4\u6362\u4e24\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528c++\u6807\u51c6\u5e93\u4e2d\u7684 std::swap() \u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5728 \u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include int main () { int x { 2 }; int y { 4 }; std :: cout << \"Before swap: x = \" << x << \", y = \" << y << '\\n' ; std :: swap ( x , y ); // swap the values of x and y std :: cout << \"After swap: x = \" << x << \", y = \" << y << '\\n' ; return 0 ; } \u7a0b\u5e8f\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Before swap: x = 2, y = 4 After swap: x = 4, y = 2 \u6ce8\u610f\uff0c\u4f7f\u7528 swap \u540e\uff0c x \u548c y \u7684\u503c\u5c31\u88ab\u4ea4\u6362\u4e86\uff01","title":"\u6392\u5e8f\u7684\u539f\u7406"},{"location":"11-4-Sorting-an-array-using-selection-sort/#_3","text":"\u6709\u5f88\u591a\u65b9\u6cd5\u53ef\u4ee5\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002 \u9009\u62e9\u6392\u5e8f \u53ef\u80fd\u662f\u6700\u5bb9\u6613\u7406\u89e3\u7684\u6392\u5e8f\uff0c\u8fd9\u4f7f\u5b83\u5f88\u9002\u5408\u4f5c\u4e3a\u6559\u5b66\u793a\u4f8b\uff08\u5c3d\u7ba1\u5b83\u7684\u6392\u5e8f\u901f\u5ea6\u5e76\u4e0d\u5feb\uff09\u3002 \u9009\u62e9\u6392\u5e8f\u7684\u6b65\u9aa4\u5982\u4e0b\uff08\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\uff09\uff1a \u4ece\u7d22\u5f15 0\u5f00\u59cb\uff0c\u904d\u5386\u6570\u7ec4\u67e5\u627e\u6700\u5c0f\u7684\u503c\uff1b \u4ea4\u6362\u6700\u5c0f\u503c\u548c\u7d22\u5f15\u4f4d\u7f6e\u4e3a 0 \u7684\u5143\u7d20\uff1b \u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u7d22\u5f15\u91cd\u590d\u4e0a\u8ff0\u6b65\u9aa4\u3002 \u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u8981\u627e\u5230\u6570\u7ec4\u4e2d\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u7136\u540e\u628a\u5b83\u4ea4\u6362\u5230\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u3002\u7136\u540e\u6211\u4eec\u8981\u627e\u5230\u4e0b\u4e00\u4e2a\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u7136\u540e\u628a\u5b83\u4ea4\u6362\u5230\u7b2c\u4e8c\u4e2a\u4f4d\u7f6e\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5c06\u91cd\u590d\u8fdb\u884c\uff0c\u76f4\u5230\u8017\u5c3d\u6240\u6709\u5143\u7d20\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u5173\u4e8e5\u4e2a\u5143\u7d20\u7684\u7b97\u6cd5\u7684\u4f8b\u5b50\u3002\u8ba9\u6211\u4eec\u4ece\u4e00\u4e2a\u793a\u4f8b\u6570\u7ec4\u5f00\u59cb { 30, 50, 20, 10, 40 } \u4ece\u7d22\u5f15 0 \u5f00\u59cb\uff0c\u627e\u5230\u6700\u5c0f\u7684\u5143\u7d20 { 30, 50, 20, 10 , 40 } \u4ea4\u6362\u5230\u7d22\u5f15 0 \u7684\u4f4d\u7f6e { 10 , 50, 20, 30 , 40 } \u7b2c\u4e00\u4e2a\u5143\u7d20\u5df2\u7ecf\u6392\u5e8f\u5b8c\u6210\uff0c\u540e\u9762\u5c31\u4e0d\u7528\u7ba1\u4e86\u3002\u7136\u540e\u4ece\u7d22\u5f15 1 \u5f00\u59cb\uff0c\u7ee7\u7eed\u67e5\u627e\u6700\u5c0f\u7684\u5143\u7d20\uff1a { 10 , 50, 20 , 30, 40 } \u5c06\u627e\u5230\u7684\u503c\u4ea4\u6362\u5230\u7d22\u5f15 1 \u7684\u4f4d\u7f6e\uff1a { 10 , 20 , 50 , 30, 40 } \u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u5ffd\u7565\u524d\u4e24\u4e2a\u5143\u7d20\u3002\u627e\u5230\u4ece\u7d22\u5f152\u5f00\u59cb\u7684\u6700\u5c0f\u5143\u7d20\uff1a { 10 , 20 , 50, 30 , 40 } \u7136\u540e\u5c06\u5b83\u4e0e\u7d22\u5f152\u4e2d\u7684\u5143\u7d20\u4ea4\u6362\uff1a { 10 , 20 , 30 , 50 , 40 } \u627e\u5230\u4ece\u7d22\u5f153\u5f00\u59cb\u7684\u6700\u5c0f\u5143\u7d20\uff1a { 10 , 20 , 30 , 50, 40 } \u7136\u540e\u5c06\u5b83\u4e0e\u7d22\u5f153\u4e2d\u7684\u5143\u7d20\u4ea4\u6362\uff1a { 10 , 20 , 30 , 40 , 50 } \u6700\u540e\uff0c\u627e\u5230\u4ece\u7d22\u5f154\u5f00\u59cb\u7684\u6700\u5c0f\u5143\u7d20\uff1a { 10 , 20 , 30 , 40 , 50 } \u7136\u540e\u5c06\u5b83\u4e0e\u7d22\u5f154\u4e2d\u7684\u5143\u7d20\u4ea4\u6362(\u5b9e\u9645\u4e0a\u4ec0\u4e48\u4e5f\u6ca1\u505a)\uff1a { 10 , 20 , 30 , 40 , 50 } \u5b8c\u6210\uff01 { 10, 20, 30, 40, 50 } \u6ce8\u610f\uff0c\u6700\u540e\u4e00\u6b21\u6bd4\u8f83\u603b\u662f\u4e0e\u81ea\u8eab\u8fdb\u884c\u6bd4\u8f83(\u8fd9\u662f\u5197\u4f59\u7684)\uff0c\u56e0\u6b64\u6211\u4eec\u5b9e\u9645\u4e0a\u53ef\u4ee5\u5728\u6570\u7ec4\u7ed3\u675f\u524d\u505c\u6b621\u4e2a\u5143\u7d20\u3002","title":"\u9009\u62e9\u6392\u5e8f"},{"location":"11-4-Sorting-an-array-using-selection-sort/#c","text":"C++ \u7684\u5b9e\u73b0\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include #include #include int main () { int array []{ 30 , 50 , 20 , 10 , 40 }; constexpr int length { static_cast < int > ( std :: size ( array )) }; // Step through each element of the array // (except the last one, which will already be sorted by the time we get there) for ( int startIndex { 0 }; startIndex < length - 1 ; ++ startIndex ) { // smallestIndex is the index of the smallest element we\u2019ve encountered this iteration // Start by assuming the smallest element is the first element of this iteration int smallestIndex { startIndex }; // Then look for a smaller element in the rest of the array for ( int currentIndex { startIndex + 1 }; currentIndex < length ; ++ currentIndex ) { // If we've found an element that is smaller than our previously found smallest if ( array [ currentIndex ] < array [ smallestIndex ]) // then keep track of it smallestIndex = currentIndex ; } // smallestIndex is now the smallest element in the remaining array // swap our start element with our smallest element (this sorts it into the correct place) std :: swap ( array [ startIndex ], array [ smallestIndex ]); } // Now that the whole array is sorted, print our sorted array as proof it works for ( int index { 0 }; index < length ; ++ index ) std :: cout << array [ index ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7b97\u6cd5\u6700\u4ee4\u4eba\u56f0\u60d1\u7684\u90e8\u5206\u662f\u4e00\u4e2a\u5faa\u73af\u4e2d\u7684\u5faa\u73af(\u79f0\u4e3a \u5d4c\u5957\u5faa\u73af )\u3002\u5916\u5c42\u5faa\u73af(startIndex)\u9010\u4e2a\u904d\u5386\u6bcf\u4e2a\u5143\u7d20\u3002\u5bf9\u4e8e\u5916\u5c42\u5faa\u73af\u7684\u6bcf\u6b21\u8fed\u4ee3\uff0c\u4f7f\u7528\u5185\u90e8\u5faa\u73af(currentIndex)\u67e5\u627e\u5269\u4f59\u6570\u7ec4\u4e2d\u6700\u5c0f\u7684\u5143\u7d20(\u4ecestartIndex+1\u5f00\u59cb)\u3002smallstindex\u8ddf\u8e2a\u5185\u90e8\u5faa\u73af\u627e\u5230\u7684\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\u3002\u7136\u540e\u7528startIndex\u4ea4\u6362smallstindex\u3002\u6700\u540e\uff0c\u5916\u5c42\u5faa\u73af(startIndex)\u524d\u8fdb\u4e00\u4e2a\u5143\u7d20\uff0c\u7136\u540e\u91cd\u590d\u8fd9\u4e2a\u8fc7\u7a0b\u3002 \u63d0\u793a:\u5982\u679c\u60a8\u5728\u5f04\u6e05\u695a\u4e0a\u8ff0\u7a0b\u5e8f\u5982\u4f55\u5de5\u4f5c\u65b9\u9762\u6709\u56f0\u96be\uff0c\u90a3\u4e48\u5728\u4e00\u5f20\u7eb8\u4e0a\u5b8c\u6210\u4e00\u4e2a\u793a\u4f8b\u6848\u4f8b\u4f1a\u5f88\u6709\u5e2e\u52a9\u3002\u5728\u7eb8\u5f20\u7684\u9876\u90e8\u6c34\u5e73\u5199\u5165\u8d77\u59cb(\u672a\u6392\u5e8f)\u6570\u7ec4\u5143\u7d20\u3002\u753b\u7bad\u5934\u8868\u793astartIndex\u3001currentIndex\u548csmallstindex\u6b63\u5728\u7d22\u5f15\u54ea\u4e9b\u5143\u7d20\u3002\u624b\u52a8\u8ddf\u8e2a\u6574\u4e2a\u7a0b\u5e8f\uff0c\u5e76\u5728\u7d22\u5f15\u53d8\u5316\u65f6\u91cd\u65b0\u7ed8\u5236\u7bad\u5934\u3002\u5bf9\u4e8e\u5916\u5c42\u5faa\u73af\u7684\u6bcf\u6b21\u8fed\u4ee3\uff0c\u5f00\u59cb\u4e00\u4e2a\u65b0\u884c\uff0c\u663e\u793a\u6570\u7ec4\u7684\u5f53\u524d\u72b6\u6001\u3002 \u540d\u79f0\u7684\u6392\u5e8f\u4f7f\u7528\u76f8\u540c\u7684\u7b97\u6cd5\u3002\u53ea\u9700\u5c06\u6570\u7ec4\u7c7b\u578b\u4ece int \u6539\u4e3a std::string \uff0c\u5e76\u4f7f\u7528\u9002\u5f53\u7684\u503c\u521d\u59cb\u5316\u5373\u53ef\u3002","title":"C++ \u4e2d\u7684\u9009\u62e9\u6392\u5e8f"},{"location":"11-4-Sorting-an-array-using-selection-sort/#stdsort","text":"\u56e0\u4e3a\u6570\u7ec4\u7684\u6392\u5e8f\u975e\u5e38\u5e38\u89c1\uff0cC++\u6807\u51c6\u5e93\u4e2d\u5305\u542b\u4e86\u4e00\u4e2a\u540d\u4e3a std::sort \u7684\u6392\u5e8f\u51fd\u6570\u3002 std::sort \u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u50cf\u8fd9\u6837\u5728\u6570\u7ec4\u4e2d\u8c03\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // for std::sort #include #include // for std::size int main () { int array []{ 30 , 50 , 20 , 10 , 40 }; std :: sort ( std :: begin ( array ), std :: end ( array )); for ( int i { 0 }; i < static_cast < int > ( std :: size ( array )); ++ i ) std :: cout << array [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c std::sort \u6309\u5347\u5e8f\u6392\u5e8f\uff0c\u4f7f\u7528 operator< \u6bd4\u8f83\u5143\u7d20\u5bf9\uff0c\u5e76\u5728\u5fc5\u8981\u65f6\u4ea4\u6362\u5b83\u4eec(\u5f88\u50cf\u4e0a\u9762\u7684\u9009\u62e9\u6392\u5e8f\u793a\u4f8b\u6240\u505a\u7684)\u3002 \u6211\u4eec\u5c06\u5728\u4ee5\u540e\u7684\u7ae0\u8282\u4e2d\u66f4\u591a\u5730\u8ba8\u8bba std::sort \u3002","title":"std::sort"},{"location":"11-5-Multidimensional-Arrays/","tags":["array"],"text":"Key Takeaway \u4f7f\u7528 \u5d4c\u5957\u5927\u62ec\u53f7 \u521d\u59cb\u5316\u4e8c\u7ef4\u6570\u7ec4 \u5e26\u6709\u521d\u59cb\u5316\u5217\u8868\u7684\u4e8c\u7ef4\u6570\u7ec4\u53ea\u80fd\u5ffd\u7565\u6700\u5de6\u8fb9\u7684\u957f\u5ea6\u58f0\u660e \u6570\u7ec4\u7684\u5143\u7d20\u53ef\u4ee5\u662f\u4efb\u4f55\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6570\u7ec4\uff01\u6570\u7ec4\u7684\u6570\u7ec4\u88ab\u79f0\u4e3a \u591a\u7ef4\u6570\u7ec4 1 int array [ 3 ][ 5 ]; // a 3-element array of 5-element arrays \u56e0\u4e3a\u6570\u7ec4\u6709\u4e24\u4e2a\u4e0b\u6807\uff0c\u6240\u4ee5\u8fd9\u662f\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u3002 \u5728\u4e8c\u7ef4\u6570\u7ec4\u4e2d\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u7b2c\u4e00\u4e2a(\u5de6)\u4e0b\u6807\u89c6\u4e3a\u884c\uff0c\u800c\u5c06\u7b2c\u4e8c\u4e2a(\u53f3)\u4e0b\u6807\u89c6\u4e3a\u5217\u3002\u8fd9\u88ab\u79f0\u4e3a \u884c\u4e3b\u5e8f \u3002\u4e0a\u8ff0\u4e8c\u7ef4\u6570\u7ec4\u7684\u5e03\u5c40\u5982\u4e0b: 1 2 3 [0][0] [0][1] [0][2] [0][3] [0][4] // row 0 [1][0] [1][1] [1][2] [1][3] [1][4] // row 1 [2][0] [2][1] [2][2] [2][3] [2][4] // row 2 \u540c\u65f6\u4f7f\u7528\u4e24\u4e2a\u4e0b\u6807\u5c31\u53ef\u4ee5\u8bbf\u95ee\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff1a 1 array [ 2 ][ 3 ] = 7 ; \u521d\u59cb\u5316\u4e8c\u7ef4\u6570\u7ec4 \u8981\u521d\u59cb\u5316\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 \u5d4c\u5957\u5927\u62ec\u53f7 \uff0c\u6bcf\u7ec4\u6570\u5b57\u4ee3\u8868\u4e00\u884c\uff1a 1 2 3 4 5 6 int array [ 3 ][ 5 ] { { 1 , 2 , 3 , 4 , 5 }, // row 0 { 6 , 7 , 8 , 9 , 10 }, // row 1 { 11 , 12 , 13 , 14 , 15 } // row 2 }; \u5c3d\u7ba1\u6709\u4e9b\u7f16\u8bd1\u5668\u5141\u8bb8\u4f60\u7701\u7565\u5185\u5c42\u5927\u62ec\u53f7\uff0c\u4f46\u6211\u4eec\u5f3a\u70c8\u5efa\u8bae\u60a8\u65e0\u8bba\u5982\u4f55\u90fd\u5305\u542b\u5b83\u4eec\uff0c\u8fd9\u65e2\u662f\u4e3a\u4e86\u53ef\u8bfb\u6027\uff0c\u4e5f\u662f\u56e0\u4e3aC++\u4f1a\u7528 0 \u66ff\u6362\u7f3a\u5c11\u7684\u521d\u59cb\u5316\u503c\u3002 1 2 3 4 5 6 int array [ 3 ][ 5 ] { { 1 , 2 }, // row 0 = 1, 2, 0, 0, 0 { 6 , 7 , 8 }, // row 1 = 6, 7, 8, 0, 0 { 11 , 12 , 13 , 14 } // row 2 = 11, 12, 13, 14, 0 }; \u5e26\u6709\u521d\u59cb\u5316\u5217\u8868\u7684\u4e8c\u7ef4\u6570\u7ec4\u53ea\u80fd\u5ffd\u7565\u6700\u5de6\u8fb9\u7684\u957f\u5ea6\u58f0\u660e\uff1a 1 2 3 4 5 6 int array [][ 5 ] { { 1 , 2 , 3 , 4 , 5 }, { 6 , 7 , 8 , 9 , 10 }, { 11 , 12 , 13 , 14 , 15 } }; \u7f16\u8bd1\u5668\u53ef\u4ee5\u8ba1\u7b97\u51fa\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u4f46\u662f\uff0c\u4e0b\u9762\u8fd9\u6837\u662f\u4e0d\u5141\u8bb8\u7684\uff1a 1 2 3 4 5 int array [][] { { 1 , 2 , 3 , 4 }, { 5 , 6 , 7 , 8 } }; \u5c31\u50cf\u666e\u901a\u6570\u7ec4\u4e00\u6837\uff0c\u591a\u7ef4\u6570\u7ec4\u4ecd\u7136\u53ef\u4ee5\u521d\u59cb\u5316\u4e3a0\uff0c\u5982\u4e0b\u6240\u793a: 1 int array [ 3 ][ 5 ]{}; \u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20 \u8bbf\u95ee\u4e8c\u7ef4\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\u9700\u8981\u4e24\u4e2a\u5faa\u73af\uff1a\u4e00\u4e2a\u7528\u4e8e\u884c\uff0c\u4e00\u4e2a\u7528\u4e8e\u5217\u3002\u7531\u4e8e\u4e8c\u7ef4\u6570\u7ec4\u901a\u5e38\u662f\u9010\u884c\u8bbf\u95ee\u7684\uff0c\u6240\u4ee5 \u884c\u7d22\u5f15\u901a\u5e38\u7528\u4f5c\u5916\u90e8\u5faa\u73af \u3002 1 2 3 4 5 6 7 for ( int row { 0 }; row < numRows ; ++ row ) // step through the rows in the array { for ( int col { 0 }; col < numCols ; ++ col ) // step through each element in the row { std :: cout << array [ row ][ col ]; } } \u5728 C++11 \u4e2d\uff0c for-each \u5faa\u73af\u4e5f\u53ef\u4ee5\u7528\u4e8e\u591a\u7ef4\u6570\u7ec4\uff0c\u6211\u4eec\u4f1a\u5728 11.11 - for-each \u5faa\u73af \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u8d85\u8fc7\u4e8c\u7ef4\u7684\u591a\u7ef4\u6570\u7ec4 \u591a\u7ef4\u6570\u7ec4\u7684\u7ef4\u6570\u53ef\u4ee5\u8d85\u8fc72\u7ef4\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4e09\u7ef4\u6570\u7ec4\u7684\u58f0\u660e\uff1a 1 int array [ 5 ][ 4 ][ 3 ]; \u4e09\u7ef4\u6570\u7ec4\u6ca1\u529e\u6cd5\u4ee5\u7b26\u5408\u76f4\u89c9\u7684\u65b9\u5f0f\u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u6765\u521d\u59cb\u5316\uff0c\u6240\u4ee5\u6700\u597d\u5c06\u5176\u521d\u59cb\u5316\u4e3a0\u7136\u540e\u518d\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u663e\u5f0f\u5730\u521d\u59cb\u5316\u3002 \u8bbf\u95ee\u4e09\u7ef4\u6570\u7ec4\u5143\u7d20\u7684\u65b9\u6cd5\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u662f\u7c7b\u4f3c\u7684\uff1a 1 std :: cout << array [ 3 ][ 1 ][ 2 ]; \u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u7684\u4f8b\u5b50 \u8ba9\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u7684\u5b9e\u9645\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include int main () { constexpr int numRows { 10 }; constexpr int numCols { 10 }; // Declare a 10x10 array int product [ numRows ][ numCols ]{}; // Calculate a multiplication table for ( int row { 1 }; row < numRows ; ++ row ) { for ( int col { 1 }; col < numCols ; ++ col ) { product [ row ][ col ] = row * col ; } } // Print the table for ( int row { 1 }; row < numRows ; ++ row ) { for ( int col { 1 }; col < numCols ; ++ col ) { std :: cout << product [ row ][ col ] << '\\t' ; } std :: cout << '\\n' ; } return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u8ba1\u7b97\u5e76\u6253\u53701\u52309(\u5305\u62ec9)\u4e4b\u95f4\u6240\u6709\u503c\u7684\u4e58\u6cd5\u8868\u3002\u6ce8\u610f\uff0c\u5728\u6253\u5370\u8868\u65f6\uff0cfor\u5faa\u73af\u4ece1\u5f00\u59cb\uff0c\u800c\u4e0d\u662f\u4ece0\u5f00\u59cb\u3002\u8f93\u51fa\u5982\u4e0b: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 48 54 7 14 21 28 35 42 49 56 63 8 16 24 32 40 48 56 64 72 9 18 27 36 45 54 63 72 81 \u4e8c\u7ef4\u6570\u7ec4\u901a\u5e38\u7528\u4e8e\u57fa\u4e8e\u8d34\u56fe\u7684\u6e38\u620f\u4e2d\uff0c\u5176\u4e2d\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u4ee3\u8868\u4e00\u4e2a\u8d34\u56fe\u3002\u5b83\u4eec\u4e5f\u88ab\u7528\u4e8e3d\u8ba1\u7b97\u673a\u56fe\u5f62(\u4f5c\u4e3a\u77e9\u9635)\uff0c\u4ee5\u65cb\u8f6c\u3001\u7f29\u653e\u548c\u8868\u793a\u5f62\u72b6\u3002","title":"11.5 - \u591a\u7ef4\u6570\u7ec4"},{"location":"11-5-Multidimensional-Arrays/#_1","text":"\u8981\u521d\u59cb\u5316\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 \u5d4c\u5957\u5927\u62ec\u53f7 \uff0c\u6bcf\u7ec4\u6570\u5b57\u4ee3\u8868\u4e00\u884c\uff1a 1 2 3 4 5 6 int array [ 3 ][ 5 ] { { 1 , 2 , 3 , 4 , 5 }, // row 0 { 6 , 7 , 8 , 9 , 10 }, // row 1 { 11 , 12 , 13 , 14 , 15 } // row 2 }; \u5c3d\u7ba1\u6709\u4e9b\u7f16\u8bd1\u5668\u5141\u8bb8\u4f60\u7701\u7565\u5185\u5c42\u5927\u62ec\u53f7\uff0c\u4f46\u6211\u4eec\u5f3a\u70c8\u5efa\u8bae\u60a8\u65e0\u8bba\u5982\u4f55\u90fd\u5305\u542b\u5b83\u4eec\uff0c\u8fd9\u65e2\u662f\u4e3a\u4e86\u53ef\u8bfb\u6027\uff0c\u4e5f\u662f\u56e0\u4e3aC++\u4f1a\u7528 0 \u66ff\u6362\u7f3a\u5c11\u7684\u521d\u59cb\u5316\u503c\u3002 1 2 3 4 5 6 int array [ 3 ][ 5 ] { { 1 , 2 }, // row 0 = 1, 2, 0, 0, 0 { 6 , 7 , 8 }, // row 1 = 6, 7, 8, 0, 0 { 11 , 12 , 13 , 14 } // row 2 = 11, 12, 13, 14, 0 }; \u5e26\u6709\u521d\u59cb\u5316\u5217\u8868\u7684\u4e8c\u7ef4\u6570\u7ec4\u53ea\u80fd\u5ffd\u7565\u6700\u5de6\u8fb9\u7684\u957f\u5ea6\u58f0\u660e\uff1a 1 2 3 4 5 6 int array [][ 5 ] { { 1 , 2 , 3 , 4 , 5 }, { 6 , 7 , 8 , 9 , 10 }, { 11 , 12 , 13 , 14 , 15 } }; \u7f16\u8bd1\u5668\u53ef\u4ee5\u8ba1\u7b97\u51fa\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u4f46\u662f\uff0c\u4e0b\u9762\u8fd9\u6837\u662f\u4e0d\u5141\u8bb8\u7684\uff1a 1 2 3 4 5 int array [][] { { 1 , 2 , 3 , 4 }, { 5 , 6 , 7 , 8 } }; \u5c31\u50cf\u666e\u901a\u6570\u7ec4\u4e00\u6837\uff0c\u591a\u7ef4\u6570\u7ec4\u4ecd\u7136\u53ef\u4ee5\u521d\u59cb\u5316\u4e3a0\uff0c\u5982\u4e0b\u6240\u793a: 1 int array [ 3 ][ 5 ]{};","title":"\u521d\u59cb\u5316\u4e8c\u7ef4\u6570\u7ec4"},{"location":"11-5-Multidimensional-Arrays/#_2","text":"\u8bbf\u95ee\u4e8c\u7ef4\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\u9700\u8981\u4e24\u4e2a\u5faa\u73af\uff1a\u4e00\u4e2a\u7528\u4e8e\u884c\uff0c\u4e00\u4e2a\u7528\u4e8e\u5217\u3002\u7531\u4e8e\u4e8c\u7ef4\u6570\u7ec4\u901a\u5e38\u662f\u9010\u884c\u8bbf\u95ee\u7684\uff0c\u6240\u4ee5 \u884c\u7d22\u5f15\u901a\u5e38\u7528\u4f5c\u5916\u90e8\u5faa\u73af \u3002 1 2 3 4 5 6 7 for ( int row { 0 }; row < numRows ; ++ row ) // step through the rows in the array { for ( int col { 0 }; col < numCols ; ++ col ) // step through each element in the row { std :: cout << array [ row ][ col ]; } } \u5728 C++11 \u4e2d\uff0c for-each \u5faa\u73af\u4e5f\u53ef\u4ee5\u7528\u4e8e\u591a\u7ef4\u6570\u7ec4\uff0c\u6211\u4eec\u4f1a\u5728 11.11 - for-each \u5faa\u73af \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20"},{"location":"11-5-Multidimensional-Arrays/#_3","text":"\u591a\u7ef4\u6570\u7ec4\u7684\u7ef4\u6570\u53ef\u4ee5\u8d85\u8fc72\u7ef4\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4e09\u7ef4\u6570\u7ec4\u7684\u58f0\u660e\uff1a 1 int array [ 5 ][ 4 ][ 3 ]; \u4e09\u7ef4\u6570\u7ec4\u6ca1\u529e\u6cd5\u4ee5\u7b26\u5408\u76f4\u89c9\u7684\u65b9\u5f0f\u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u6765\u521d\u59cb\u5316\uff0c\u6240\u4ee5\u6700\u597d\u5c06\u5176\u521d\u59cb\u5316\u4e3a0\u7136\u540e\u518d\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u663e\u5f0f\u5730\u521d\u59cb\u5316\u3002 \u8bbf\u95ee\u4e09\u7ef4\u6570\u7ec4\u5143\u7d20\u7684\u65b9\u6cd5\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u662f\u7c7b\u4f3c\u7684\uff1a 1 std :: cout << array [ 3 ][ 1 ][ 2 ];","title":"\u8d85\u8fc7\u4e8c\u7ef4\u7684\u591a\u7ef4\u6570\u7ec4"},{"location":"11-5-Multidimensional-Arrays/#_4","text":"\u8ba9\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u7684\u5b9e\u9645\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include int main () { constexpr int numRows { 10 }; constexpr int numCols { 10 }; // Declare a 10x10 array int product [ numRows ][ numCols ]{}; // Calculate a multiplication table for ( int row { 1 }; row < numRows ; ++ row ) { for ( int col { 1 }; col < numCols ; ++ col ) { product [ row ][ col ] = row * col ; } } // Print the table for ( int row { 1 }; row < numRows ; ++ row ) { for ( int col { 1 }; col < numCols ; ++ col ) { std :: cout << product [ row ][ col ] << '\\t' ; } std :: cout << '\\n' ; } return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u8ba1\u7b97\u5e76\u6253\u53701\u52309(\u5305\u62ec9)\u4e4b\u95f4\u6240\u6709\u503c\u7684\u4e58\u6cd5\u8868\u3002\u6ce8\u610f\uff0c\u5728\u6253\u5370\u8868\u65f6\uff0cfor\u5faa\u73af\u4ece1\u5f00\u59cb\uff0c\u800c\u4e0d\u662f\u4ece0\u5f00\u59cb\u3002\u8f93\u51fa\u5982\u4e0b: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 48 54 7 14 21 28 35 42 49 56 63 8 16 24 32 40 48 56 64 72 9 18 27 36 45 54 63 72 81 \u4e8c\u7ef4\u6570\u7ec4\u901a\u5e38\u7528\u4e8e\u57fa\u4e8e\u8d34\u56fe\u7684\u6e38\u620f\u4e2d\uff0c\u5176\u4e2d\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u4ee3\u8868\u4e00\u4e2a\u8d34\u56fe\u3002\u5b83\u4eec\u4e5f\u88ab\u7528\u4e8e3d\u8ba1\u7b97\u673a\u56fe\u5f62(\u4f5c\u4e3a\u77e9\u9635)\uff0c\u4ee5\u65cb\u8f6c\u3001\u7f29\u653e\u548c\u8868\u793a\u5f62\u72b6\u3002","title":"\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u7684\u4f8b\u5b50"},{"location":"11-6-C-style-strings/","tags":["c-style","string"],"text":"Key Takeaway C\u8bed\u8a00\u5b57\u7b26\u4e32\u5c31\u662f\u4e00\u4e2a\u5b57\u7b26\u6570\u7ec4\uff0c\u6700\u540e\u63a5\u7740\u4e00\u4e2a\u7a7a\u5b57\u7b26\u3002 \u6253\u5370\u5b57\u7b26\u4e32\u65f6\uff0c\u9047\u5230\u7a7a\u5b57\u7b26\u4f1a\u505c\u6b62\uff0c\u5982\u679c\u7a7a\u5b57\u7b26\u88ab\u8986\u76d6\uff0c\u6253\u5370\u51fd\u6570\u5c31\u4f1a\u4e00\u76f4\u6253\u5370\u76f4\u5230\u9047\u5230\u4e0b\u4e00\u4e2a0 \u8c03\u7528 std::cin.getline(name, std::size(name)); \u53ef\u4ee5\u8bfb\u53d6\u6700\u591a 254 \u4e2a\u5b57\u7b26\u5230 name \uff08\u4e3a\u7a7a\u5b57\u7b26\u4fdd\u7559\u4e00\u4e2a\u7a7a\u95f4\uff01\uff09\u3002\u8d85\u51fa\u7684\u5b57\u7b26\u4f1a\u88ab\u5ffd\u7565\u6389\u3002\u901a\u8fc7\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u907f\u514d\u6570\u7ec4\u6ea2\u51fa\uff01 \u64cd\u4f5cC\u8bed\u8a00\u5b57\u7b26\u4e32\u7684\u5e38\u7528\u51fd\u6570\uff1a strcpy() -- \u5c06\u5b57\u7b26\u4e32\u62f7\u8d1d\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32 strncpy() -- \u5c06\u5b57\u7b26\u4e32\u62f7\u8d1d\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32(\u6307\u5b9abuffer\u5927\u5c0f)\uff0c\u4f46\u4e0d\u80fd\u786e\u4fddnull\u7ed3\u5c3e strcpy_s() \uff0c\u5b83\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u53c2\u6570\u6765\u5b9a\u4e49\u76ee\u6807\u7684\u5927\u5c0f\uff08C++11\uff09 strcat() -- \u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u62fc\u63a5\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u540e\u9762\uff08\u5371\u9669\u64cd\u4f5c\uff09 strncat() -- \u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u62fc\u63a5\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u540e\u9762 (\u4f1a\u68c0\u67e5buffer\u957f\u5ea6) strcmp() -- \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\uff08\u76f8\u7b49\u8fd4\u56de 0\uff09 strncmp() -- \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\uff08\u76f8\u7b49\u8fd4\u56de 0\uff09 \u4e0d\u8981\u4f7f\u7528 C \u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32 \u5728 4.17 - std::string \u7b80\u4ecb \u4e2d\uff0c\u6211\u4eec\u5c06\u5b57\u7b26\u5b9a\u4e49\u4e3a\u4e00\u7cfb\u5217\u5b57\u7b26\u7684\u96c6\u5408\uff0c\u4f8b\u5982 \u201cHello, world!\u201d\u3002\u5b57\u7b26\u4e32c++\u4e2d\u5904\u7406\u6587\u672c\u7684\u4e3b\u8981\u65b9\u5f0f\uff0c\u800c std::string \u53ef\u4ee5\u4f7fC++\u4e2d\u5904\u7406\u5b57\u7b26\u4e32\u53d8\u5f97\u7b80\u5355\u3002 \u73b0\u4ee3C++\u652f\u6301\u4e24\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u5b57\u7b26\u4e32\uff1a std::string (\u4f5c\u4e3a\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206)\u548cC\u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32(\u4eceC\u8bed\u8a00\u7ee7\u627f\u800c\u6765)\u3002\u4e8b\u5b9e\u8bc1\u660e\uff0c std::string \u662f\u4f7f\u7528c\u98ce\u683c\u5b57\u7b26\u4e32\u5b9e\u73b0\u7684\u3002\u5728\u8fd9\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u4e86\u89e3C\u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002 C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 \u5c31\u662f\u4e00\u4e2a\u4ee5\u7a7a\u5b57\u7b26( \\0 )\u7ed3\u5c3e\u7684\u5b57\u7b26\u6570\u7ec4\u3002 \u7a7a\u5b57\u7b26 \u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u5b57\u7b26( '\\0' \uff0c ascii \u7801\u4e3a 0) \uff0c\u7528\u4e8e\u8868\u793a\u5b57\u7b26\u7684\u7ed3\u5c3e\u3002\u6240\u4ee5C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u4e5f\u88ab\u79f0\u4e3a\u7a7a\u5b57\u7b26\u7ed3\u5c3e\u7684\u5b57\u7b26\u4e32\u3002 \u5b9a\u4e49C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u58f0\u660e\u4e00\u4e2a\u5b57\u7b26\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\u3002 1 char myString []{ \"string\" }; \u5c3d\u7ba1 \u201cstring\u201d \u53ea\u67096\u4e2a\u5b57\u6bcd\uff0cC++\u4f1a\u81ea\u52a8\u5728\u5176\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u7a7a\u5b57\u7b26\u8868\u660e\u7ed3\u5c3e\uff08\u4e0d\u9700\u8981\u7a0b\u5e8f\u5458\u81ea\u5df1\u64cd\u4f5c\uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c myString \u6570\u7ec4\u7684\u957f\u5ea6\u5b9e\u9645\u4e0a\u4e3a 7\uff01 \u6211\u4eec\u53ef\u4ee5\u5728\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e2d\u770b\u5230\u8fd9\u4e00\u70b9\u7684\u8bc1\u636e\uff0c\u5b83\u6253\u5370\u51fa\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u4ee5\u53ca\u6240\u6709\u5b57\u7b26\u7684ASCII\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include // for std::size int main () { char myString []{ \"string\" }; const int length { static_cast < int > ( std :: size ( myString )) }; // const int length{ sizeof(myString) / sizeof(myString[0]) }; // use instead if not C++17 capable std :: cout << myString << \" has \" << length << \" characters. \\n \" ; for ( int index { 0 }; index < length ; ++ index ) std :: cout << static_cast < int > ( myString [ index ]) << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 string has 7 characters. 115 116 114 105 110 103 0 0 \u662f\u6dfb\u52a0\u5230\u5b57\u7b26\u4e32\u672b\u5c3e\u7684\u7a7a\u7ed3\u675f\u7b26\u7684ASCII\u7801\u3002 \u5f53\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u58f0\u660e\u5b57\u7b26\u4e32\u65f6\uff0c\u6700\u597d\u4f7f\u7528 [] \uff0c\u5e76\u8ba9\u7f16\u8bd1\u5668\u8ba1\u7b97\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u8fd9\u6837\uff0c\u5982\u679c\u4ee5\u540e\u9700\u8981\u4fee\u6539\u5b57\u7b26\u4e32\uff0c\u5c31\u4e0d\u5fc5\u624b\u52a8\u8c03\u6574\u6570\u7ec4\u7684\u957f\u5ea6\u3002 \u9700\u8981\u6ce8\u610f\u7684\u4e00\u70b9\u662f\uff0cC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u9075\u5faa\u4e0e\u6570\u7ec4\u76f8\u540c\u7684\u89c4\u5219\u3002\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u5728\u521b\u5efa\u65f6\u521d\u59cb\u5316\u5b57\u7b26\u4e32\uff0c\u4f46\u4f60\u4e0d\u80fd\u5728\u4e4b\u540e\u4f7f\u7528\u8d4b\u503c\u64cd\u4f5c\u7b26\u7ed9\u5b83\u8d4b\u503c! 1 2 char myString []{ \"string\" }; // ok myString = \"rope\" ; // not ok! \u56e0\u4e3a C \u98ce\u683c\u5b57\u7b26\u4e32\u662f\u6570\u7ec4\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0b\u6807\u8fd0\u7b97\u7b26 [] \u6765\u6539\u53d8\u5b57\u7b26\u4e32\u4e2d\u7684\u5143\u7d20\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { char myString []{ \"string\" }; myString [ 1 ] = 'p' ; std :: cout << myString << '\\n' ; return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 spring \u5728\u4f7f\u7528 C \u98ce\u683c\u5b57\u7b26\u4e32\u65f6\uff0c std::cout \u4f1a\u4f9d\u6b21\u6253\u5370\u7a7a\u5b57\u7b26\u3002\u5982\u679c\u4f60\u4e0d\u5c0f\u5fc3\u8986\u76d6\u4e86\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u5b57\u7b26\uff08\u4f8b\u5982\uff1a\u7ed9 myString[6] \u8d4b\u503c\uff09\uff0c\u90a3\u4e48\u4f60\u5f97\u5230\u7684\u6253\u5370\u7ed3\u679c\u4e0d\u4ec5\u4ec5\u662f\u5b8c\u6574\u7684\u5b57\u7b26\u4e32\uff0c std::cout \u8fd8\u4f1a\u7ee7\u7eed\u6253\u5370\u76f8\u90bb\u5185\u5b58\u4e2d\u7684\u5185\u5bb9\uff0c\u76f4\u5230\u9047\u5230 0\u3002 \u6ce8\u610f\uff0c\u5b57\u7b26\u6570\u7ec4\u7684\u5927\u5c0f\u6bd4\u5b57\u7b26\u4e32\u5927\u662f\u53ef\u4ee5\u7684\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char name [ 20 ]{ \"Alex\" }; // only use 5 characters (4 letters + null terminator) std :: cout << \"My name is: \" << name << '\\n' ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u201cAlex\u201d \u4f1a\u88ab\u6253\u5370\u51fa\u6765\uff0c\u7136\u540e std::cout \u4f1a\u5728\u9047\u5230\u7a7a\u5b57\u7b26\u65f6\u505c\u6b62\u6253\u5370\u3002\u6570\u7ec4\u4e2d\u540e\u7eed\u5185\u5bb9\u4f1a\u88ab\u5ffd\u7565\u3002 C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u548c std::cin \u5728\u5f88\u591a\u60c5\u51b5\u4e0b\u6211\u4eec\u90fd\u4e0d\u77e5\u9053\u5b57\u7b26\u4e32\u4f1a\u6709\u591a\u957f\u3002\u4f8b\u5982\uff0c\u8003\u8651\u8fd9\u6837\u4e00\u4e2a\u95ee\u9898\uff1a\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u6211\u4eec\u9700\u8981\u8981\u6c42\u7528\u6237\u8f93\u5165\u4ed6\u4eec\u7684\u540d\u5b57\u3002\u4ed6\u4eec\u7684\u540d\u5b57\u6709\u591a\u957f\uff1f\u5728\u7528\u6237\u8f93\u5165\u88ab\u8bfb\u53d6\u4e4b\u524d\uff0c\u6211\u4eec\u90fd\u4e0d\u77e5\u9053\uff01 \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u6bd4\u5b9e\u9645\u9700\u8981\u5927\u7684\u591a\u7684\u6570\u7ec4\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { char name [ 255 ] {}; // declare array large enough to hold 254 characters + null terminator std :: cout << \"Enter your name: \" ; std :: cin >> name ; std :: cout << \"You entered: \" << name << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4e3aname\u5206\u914d\u4e86\u4e00\u4e2a255\u4e2a\u5b57\u7b26\u7684\u6570\u7ec4\u5e76\u5047\u8bbe\u7528\u6237\u4e0d\u4f1a\u8f93\u5165\u8fd9\u4e48\u591a\u5b57\u7b26\u3002\u5c3d\u7ba1\u8fd9\u5728C/C++\u7f16\u7a0b\u4e2d\u5f88\u5e38\u89c1\uff0c\u4f46\u8fd9\u662f\u4e00\u79cd\u7cdf\u7cd5\u7684\u7f16\u7a0b\u5b9e\u8df5\uff0c\u56e0\u4e3a\u5e76\u6ca1\u6709\u529e\u6cd5\u963b\u6b62\u7528\u6237\u8f93\u5165\u8d85\u8fc7254\u4e2a\u5b57\u7b26(\u4e0d\u7ba1\u662f\u65e0\u610f\u7684\uff0c\u8fd8\u662f\u6076\u610f\u7684)\u3002 \u4f7f\u7528 std::cin \u8bfb\u53d6C\u98ce\u683c\u5b57\u7b26\u4e32\u7684\u63a8\u8350\u65b9\u6cd5\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include #include // for std::size int main () { char name [ 255 ] {}; // declare array large enough to hold 254 characters + null terminator std :: cout << \"Enter your name: \" ; std :: cin . getline ( name , std :: size ( name )); std :: cout << \"You entered: \" << name << '\\n' ; return 0 ; } \u8c03\u7528 std::cin.getline(name, std::size(name)); \u53ef\u4ee5\u8bfb\u53d6\u6700\u591a 254 \u4e2a\u5b57\u7b26\u5230 name \uff08\u4e3a\u7a7a\u5b57\u7b26\u4fdd\u7559\u4e00\u4e2a\u7a7a\u95f4\uff01\uff09\u3002\u8d85\u51fa\u7684\u5b57\u7b26\u4f1a\u88ab\u5ffd\u7565\u6389\u3002\u901a\u8fc7\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u907f\u514d\u6570\u7ec4\u6ea2\u51fa\uff01 \u64cd\u4f5c C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 C++\u63d0\u4f9b\u4e86\u8bb8\u591a\u51fd\u6570\u6765\u64cd\u4f5cC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u5b83\u4eec\u662f \u5934\u6587\u4ef6\u7684\u4e00\u90e8\u5206\u3002\u5e38\u7528\u7684\u51fd\u6570\u5217\u4e3e\u5982\u4e0b\uff1a strcpy() \u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u66f4\u5e38\u89c1\u7684\u662f\uff0c\u5b83\u88ab\u7528\u6765\u7ed9\u4e00\u4e2a\u5b57\u7b26\u4e32\u8d4b\u503c: 1 2 3 4 5 6 7 8 9 10 11 12 #include #include int main () { char source []{ \"Copy this!\" }; char dest [ 50 ]; std :: strcpy ( dest , source ); std :: cout << dest << '\\n' ; // prints \"Copy this!\" return 0 ; } \u4e0d\u8fc7\uff0c\u5982\u679c\u4e0d\u5c0f\u5fc3\u7684\u8bdd\uff0c strcpy() \u5f88\u5bb9\u6613\u5bfc\u81f4\u6570\u7ec4\u7684\u6ea2\u51fa\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u76ee\u6807\u5730\u5740\u7684\u5927\u5c0f\u4e0d\u8db3\u4ee5\u5bb9\u7eb3\u5168\u90e8\u5b57\u7b26\u4e32\uff0c\u8fd9\u5c31\u4f1a\u5bfc\u81f4\u6570\u7ec4\u6ea2\u51fa\u3002 1 2 3 4 5 6 7 8 9 10 11 12 #include #include int main () { char source []{ \"Copy this!\" }; char dest [ 5 ]; // note that the length of dest is only 5 chars! std :: strcpy ( dest , source ); // overflow! std :: cout << dest << '\\n' ; return 0 ; } \u8bb8\u591a\u7a0b\u5e8f\u5458\u5efa\u8bae\u4f7f\u7528 strncpy() \uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u6307\u5b9a\u7f13\u51b2\u533a\u7684\u5927\u5c0f\uff0c\u5e76\u786e\u4fdd\u4e0d\u4f1a\u53d1\u751f\u6ea2\u51fa\u3002\u4e0d\u5e78\u7684\u662f\uff0c' strncpy() \u4e0d\u80fd\u786e\u4fdd\u5b57\u7b26\u4e32\u4ee5\u7a7a\u5b57\u7b26\u7ed3\u5c3e\uff0c\u8fd9\u4ecd\u7136\u4e3a\u6570\u7ec4\u6ea2\u51fa\u7559\u4e0b\u4e86\u9690\u60a3\u3002 \u5728C++ 11\u4e2d\uff0c\u9996\u9009 strcpy_s() \uff0c\u5b83\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u53c2\u6570\u6765\u5b9a\u4e49\u76ee\u6807\u7684\u5927\u5c0f\u3002\u7136\u800c\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u7684\u7f16\u8bd1\u5668\u90fd\u652f\u6301\u8fd9\u4e2a\u51fd\u6570\uff0c\u8981\u4f7f\u7528\u5b83\uff0c\u4f60\u5fc5\u987b\u5b9a\u4e49 STDC_WANT_LIB_EXT1 \u5e76\u5c06\u5176\u5b9a\u4e49\u4e3a 1 \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #define __STDC_WANT_LIB_EXT1__ 1 #include // for strcpy_s #include int main () { char source []{ \"Copy this!\" }; char dest [ 5 ]; // note that the length of dest is only 5 chars! strcpy_s ( dest , 5 , source ); // A runtime error will occur in debug mode std :: cout << dest << '\\n' ; return 0 ; } \u56e0\u4e3a\u5e76\u4e0d\u662f\u6240\u6709\u7684\u7f16\u8bd1\u5668\u90fd\u652f\u6301 strcpy_s() \uff0c\u6240\u4ee5 strlcpy() \u662f\u4e00\u4e2a\u5f88\u53d7\u6b22\u8fce\u7684\u9009\u62e9\u2014\u2014\u5c3d\u7ba1\u5b83\u4e0d\u662f\u6807\u51c6\u7684\uff0c\u56e0\u6b64\u5f88\u591a\u7f16\u8bd1\u5668\u90fd\u4e0d\u5305\u542b\u5b83\u3002\u5b83\u4e5f\u6709\u81ea\u5df1\u7684\u4e00\u7cfb\u5217\u95ee\u9898\u3002\u7b80\u800c\u8a00\u4e4b\uff0c\u5982\u679c\u60a8\u9700\u8981\u590d\u5236C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u8fd9\u91cc\u6ca1\u6709\u666e\u904d\u63a8\u8350\u7684\u89e3\u51b3\u65b9\u6848\u3002 \u53e6\u4e00\u4e2a\u6709\u7528\u7684\u51fd\u6570\u662f strlen() \u51fd\u6570\uff0c\u5b83\u8fd4\u56deC\u98ce\u683c\u5b57\u7b26\u4e32\u7684\u957f\u5ea6(\u6ca1\u6709\u7a7a\u5b57\u7b26)\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include #include // for std::size int main () { char name [ 20 ]{ \"Alex\" }; // only use 5 characters (4 letters + null terminator) std :: cout << \"My name is: \" << name << '\\n' ; std :: cout << name << \" has \" << std :: strlen ( name ) << \" letters. \\n \" ; std :: cout << name << \" has \" << std :: size ( name ) << \" characters in the array. \\n \" ; // use sizeof(name) / sizeof(name[0]) if not C++17 capable return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 My name is: Alex Alex has 4 letters. Alex has 20 characters in the array. \u6ce8\u610f\u8fa8\u6790 strlen() \u548c std::size() \u7684\u533a\u522b\u3002 strlen() \u4f1a\u8fd4\u56de\u7a7a\u5b57\u7b26\u524d\u6240\u6709\u5b57\u7b26\u7684\u4e2a\u6570\uff0c\u800c std::size (\u6216\u8005 sizeof() \u6280\u5de7)\u5219\u4f1a\u8fd4\u56de\u6574\u4e2a\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u4e0d\u7ba1\u6570\u7ec4\u5185\u90e8\u5b58\u653e\u4e86\u4ec0\u4e48\u3002 \u5176\u4ed6\u51fd\u6570\uff1a strcat() -- \u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u62fc\u63a5\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u540e\u9762\uff08\u5371\u9669\u64cd\u4f5c\uff09 strncat() -- \u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u62fc\u63a5\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u540e\u9762 (\u4f1a\u68c0\u67e5buffer\u957f\u5ea6) strcmp() -- \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\uff08\u76f8\u7b49\u8fd4\u56de 0\uff09 strncmp() -- \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\uff08\u76f8\u7b49\u8fd4\u56de 0\uff09 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u672c\u8bfe\u4e2d\u7684\u4e00\u4e9b\u6982\u5ff5\u7f16\u5199\u7684\u793a\u4f8b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include #include // for std::size int main () { // Ask the user to enter a string char buffer [ 255 ] {}; std :: cout << \"Enter a string: \" ; std :: cin . getline ( buffer , std :: size ( buffer )); int spacesFound { 0 }; int bufferLength { static_cast < int > ( std :: strlen ( buffer )) }; // Loop through all of the characters the user entered for ( int index { 0 }; index < bufferLength ; ++ index ) { // If the current character is a space, count it if ( buffer [ index ] == ' ' ) ++ spacesFound ; } std :: cout << \"You typed \" << spacesFound << \" spaces! \\n \" ; return 0 ; } \u6ce8\u610f\u6211\u4eec\u5728\u5faa\u73af\u5916\u4f7f\u7528 strlen(buffer) \uff0c\u8fd9\u6837\u5b83\u4e4b\u540e\u8ba1\u7b97\u4e00\u6b21\uff0c\u800c\u4e0d\u662f\u6bcf\u6b21\u5faa\u73af\u90fd\u53bb\u8ba1\u7b97\u3002 \u4e0d\u8981\u4f7f\u7528 C \u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32 \u4e86\u89e3C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u662f\u5f88\u91cd\u8981\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u5728\u5f88\u591a\u4ee3\u7801\u4e2d\u90fd\u6709\u4f7f\u7528\u3002\u7136\u800c\uff0c\u65e2\u7136\u5df2\u7ecf\u89e3\u91ca\u4e86\u5b83\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u5efa\u8bae\u4f60\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u5b83\u4eec\uff01\u9664\u975e\u6709\u7279\u5b9a\u7684\u3001\u4ee4\u4eba\u4fe1\u670d\u7684\u7406\u7531\u4f7f\u7528C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u5426\u5219\u8bf7\u4f7f\u7528std::string(\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d)\u7c7b\u578b\u7684\u5b57\u7b26\u4e32\uff0c\u5b83\u66f4\u7b80\u5355\u3001\u66f4\u5b89\u5168\u4e5f\u66f4\u7075\u6d3b\u3002\u5728\u6781\u5c11\u6570\u60c5\u51b5\u4e0b\uff0c\u4f60\u786e\u5b9e\u9700\u8981\u4f7f\u7528\u56fa\u5b9a\u7684\u7f13\u51b2\u533a\u5927\u5c0f\u548cC\u98ce\u683c\u7684\u5b57\u7b26\u4e32(\u4f8b\u5982\u5185\u5b58\u6709\u9650\u7684\u8bbe\u5907)\uff0c\u6211\u4eec\u5efa\u8bae\u4f7f\u7528\u4e00\u4e2a\u6d4b\u8bd5\u826f\u597d\u7684\u7b2c\u4e09\u65b9\u5b57\u7b26\u4e32\u5e93\uff0c\u6216 std::string_view \uff0c\u8fd9\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u8ba8\u8bba\u3002 \u6cd5\u5219 \u4f7f\u7528 std::string \u6216 std::string_view \u6765\u4ee3\u66ff C \u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002","title":"11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32"},{"location":"11-6-C-style-strings/#c","text":"C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 \u5c31\u662f\u4e00\u4e2a\u4ee5\u7a7a\u5b57\u7b26( \\0 )\u7ed3\u5c3e\u7684\u5b57\u7b26\u6570\u7ec4\u3002 \u7a7a\u5b57\u7b26 \u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u5b57\u7b26( '\\0' \uff0c ascii \u7801\u4e3a 0) \uff0c\u7528\u4e8e\u8868\u793a\u5b57\u7b26\u7684\u7ed3\u5c3e\u3002\u6240\u4ee5C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u4e5f\u88ab\u79f0\u4e3a\u7a7a\u5b57\u7b26\u7ed3\u5c3e\u7684\u5b57\u7b26\u4e32\u3002 \u5b9a\u4e49C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u58f0\u660e\u4e00\u4e2a\u5b57\u7b26\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\u3002 1 char myString []{ \"string\" }; \u5c3d\u7ba1 \u201cstring\u201d \u53ea\u67096\u4e2a\u5b57\u6bcd\uff0cC++\u4f1a\u81ea\u52a8\u5728\u5176\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u7a7a\u5b57\u7b26\u8868\u660e\u7ed3\u5c3e\uff08\u4e0d\u9700\u8981\u7a0b\u5e8f\u5458\u81ea\u5df1\u64cd\u4f5c\uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c myString \u6570\u7ec4\u7684\u957f\u5ea6\u5b9e\u9645\u4e0a\u4e3a 7\uff01 \u6211\u4eec\u53ef\u4ee5\u5728\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e2d\u770b\u5230\u8fd9\u4e00\u70b9\u7684\u8bc1\u636e\uff0c\u5b83\u6253\u5370\u51fa\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u4ee5\u53ca\u6240\u6709\u5b57\u7b26\u7684ASCII\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include // for std::size int main () { char myString []{ \"string\" }; const int length { static_cast < int > ( std :: size ( myString )) }; // const int length{ sizeof(myString) / sizeof(myString[0]) }; // use instead if not C++17 capable std :: cout << myString << \" has \" << length << \" characters. \\n \" ; for ( int index { 0 }; index < length ; ++ index ) std :: cout << static_cast < int > ( myString [ index ]) << ' ' ; std :: cout << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 string has 7 characters. 115 116 114 105 110 103 0 0 \u662f\u6dfb\u52a0\u5230\u5b57\u7b26\u4e32\u672b\u5c3e\u7684\u7a7a\u7ed3\u675f\u7b26\u7684ASCII\u7801\u3002 \u5f53\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u58f0\u660e\u5b57\u7b26\u4e32\u65f6\uff0c\u6700\u597d\u4f7f\u7528 [] \uff0c\u5e76\u8ba9\u7f16\u8bd1\u5668\u8ba1\u7b97\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u8fd9\u6837\uff0c\u5982\u679c\u4ee5\u540e\u9700\u8981\u4fee\u6539\u5b57\u7b26\u4e32\uff0c\u5c31\u4e0d\u5fc5\u624b\u52a8\u8c03\u6574\u6570\u7ec4\u7684\u957f\u5ea6\u3002 \u9700\u8981\u6ce8\u610f\u7684\u4e00\u70b9\u662f\uff0cC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u9075\u5faa\u4e0e\u6570\u7ec4\u76f8\u540c\u7684\u89c4\u5219\u3002\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u5728\u521b\u5efa\u65f6\u521d\u59cb\u5316\u5b57\u7b26\u4e32\uff0c\u4f46\u4f60\u4e0d\u80fd\u5728\u4e4b\u540e\u4f7f\u7528\u8d4b\u503c\u64cd\u4f5c\u7b26\u7ed9\u5b83\u8d4b\u503c! 1 2 char myString []{ \"string\" }; // ok myString = \"rope\" ; // not ok! \u56e0\u4e3a C \u98ce\u683c\u5b57\u7b26\u4e32\u662f\u6570\u7ec4\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0b\u6807\u8fd0\u7b97\u7b26 [] \u6765\u6539\u53d8\u5b57\u7b26\u4e32\u4e2d\u7684\u5143\u7d20\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { char myString []{ \"string\" }; myString [ 1 ] = 'p' ; std :: cout << myString << '\\n' ; return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 spring \u5728\u4f7f\u7528 C \u98ce\u683c\u5b57\u7b26\u4e32\u65f6\uff0c std::cout \u4f1a\u4f9d\u6b21\u6253\u5370\u7a7a\u5b57\u7b26\u3002\u5982\u679c\u4f60\u4e0d\u5c0f\u5fc3\u8986\u76d6\u4e86\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u5b57\u7b26\uff08\u4f8b\u5982\uff1a\u7ed9 myString[6] \u8d4b\u503c\uff09\uff0c\u90a3\u4e48\u4f60\u5f97\u5230\u7684\u6253\u5370\u7ed3\u679c\u4e0d\u4ec5\u4ec5\u662f\u5b8c\u6574\u7684\u5b57\u7b26\u4e32\uff0c std::cout \u8fd8\u4f1a\u7ee7\u7eed\u6253\u5370\u76f8\u90bb\u5185\u5b58\u4e2d\u7684\u5185\u5bb9\uff0c\u76f4\u5230\u9047\u5230 0\u3002 \u6ce8\u610f\uff0c\u5b57\u7b26\u6570\u7ec4\u7684\u5927\u5c0f\u6bd4\u5b57\u7b26\u4e32\u5927\u662f\u53ef\u4ee5\u7684\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char name [ 20 ]{ \"Alex\" }; // only use 5 characters (4 letters + null terminator) std :: cout << \"My name is: \" << name << '\\n' ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u201cAlex\u201d \u4f1a\u88ab\u6253\u5370\u51fa\u6765\uff0c\u7136\u540e std::cout \u4f1a\u5728\u9047\u5230\u7a7a\u5b57\u7b26\u65f6\u505c\u6b62\u6253\u5370\u3002\u6570\u7ec4\u4e2d\u540e\u7eed\u5185\u5bb9\u4f1a\u88ab\u5ffd\u7565\u3002","title":"C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32"},{"location":"11-6-C-style-strings/#c-stdcin","text":"\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\u6211\u4eec\u90fd\u4e0d\u77e5\u9053\u5b57\u7b26\u4e32\u4f1a\u6709\u591a\u957f\u3002\u4f8b\u5982\uff0c\u8003\u8651\u8fd9\u6837\u4e00\u4e2a\u95ee\u9898\uff1a\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u6211\u4eec\u9700\u8981\u8981\u6c42\u7528\u6237\u8f93\u5165\u4ed6\u4eec\u7684\u540d\u5b57\u3002\u4ed6\u4eec\u7684\u540d\u5b57\u6709\u591a\u957f\uff1f\u5728\u7528\u6237\u8f93\u5165\u88ab\u8bfb\u53d6\u4e4b\u524d\uff0c\u6211\u4eec\u90fd\u4e0d\u77e5\u9053\uff01 \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u6bd4\u5b9e\u9645\u9700\u8981\u5927\u7684\u591a\u7684\u6570\u7ec4\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { char name [ 255 ] {}; // declare array large enough to hold 254 characters + null terminator std :: cout << \"Enter your name: \" ; std :: cin >> name ; std :: cout << \"You entered: \" << name << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4e3aname\u5206\u914d\u4e86\u4e00\u4e2a255\u4e2a\u5b57\u7b26\u7684\u6570\u7ec4\u5e76\u5047\u8bbe\u7528\u6237\u4e0d\u4f1a\u8f93\u5165\u8fd9\u4e48\u591a\u5b57\u7b26\u3002\u5c3d\u7ba1\u8fd9\u5728C/C++\u7f16\u7a0b\u4e2d\u5f88\u5e38\u89c1\uff0c\u4f46\u8fd9\u662f\u4e00\u79cd\u7cdf\u7cd5\u7684\u7f16\u7a0b\u5b9e\u8df5\uff0c\u56e0\u4e3a\u5e76\u6ca1\u6709\u529e\u6cd5\u963b\u6b62\u7528\u6237\u8f93\u5165\u8d85\u8fc7254\u4e2a\u5b57\u7b26(\u4e0d\u7ba1\u662f\u65e0\u610f\u7684\uff0c\u8fd8\u662f\u6076\u610f\u7684)\u3002 \u4f7f\u7528 std::cin \u8bfb\u53d6C\u98ce\u683c\u5b57\u7b26\u4e32\u7684\u63a8\u8350\u65b9\u6cd5\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include #include // for std::size int main () { char name [ 255 ] {}; // declare array large enough to hold 254 characters + null terminator std :: cout << \"Enter your name: \" ; std :: cin . getline ( name , std :: size ( name )); std :: cout << \"You entered: \" << name << '\\n' ; return 0 ; } \u8c03\u7528 std::cin.getline(name, std::size(name)); \u53ef\u4ee5\u8bfb\u53d6\u6700\u591a 254 \u4e2a\u5b57\u7b26\u5230 name \uff08\u4e3a\u7a7a\u5b57\u7b26\u4fdd\u7559\u4e00\u4e2a\u7a7a\u95f4\uff01\uff09\u3002\u8d85\u51fa\u7684\u5b57\u7b26\u4f1a\u88ab\u5ffd\u7565\u6389\u3002\u901a\u8fc7\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u907f\u514d\u6570\u7ec4\u6ea2\u51fa\uff01","title":"C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u548c std::cin"},{"location":"11-6-C-style-strings/#c_1","text":"C++\u63d0\u4f9b\u4e86\u8bb8\u591a\u51fd\u6570\u6765\u64cd\u4f5cC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u5b83\u4eec\u662f \u5934\u6587\u4ef6\u7684\u4e00\u90e8\u5206\u3002\u5e38\u7528\u7684\u51fd\u6570\u5217\u4e3e\u5982\u4e0b\uff1a strcpy() \u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u66f4\u5e38\u89c1\u7684\u662f\uff0c\u5b83\u88ab\u7528\u6765\u7ed9\u4e00\u4e2a\u5b57\u7b26\u4e32\u8d4b\u503c: 1 2 3 4 5 6 7 8 9 10 11 12 #include #include int main () { char source []{ \"Copy this!\" }; char dest [ 50 ]; std :: strcpy ( dest , source ); std :: cout << dest << '\\n' ; // prints \"Copy this!\" return 0 ; } \u4e0d\u8fc7\uff0c\u5982\u679c\u4e0d\u5c0f\u5fc3\u7684\u8bdd\uff0c strcpy() \u5f88\u5bb9\u6613\u5bfc\u81f4\u6570\u7ec4\u7684\u6ea2\u51fa\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u76ee\u6807\u5730\u5740\u7684\u5927\u5c0f\u4e0d\u8db3\u4ee5\u5bb9\u7eb3\u5168\u90e8\u5b57\u7b26\u4e32\uff0c\u8fd9\u5c31\u4f1a\u5bfc\u81f4\u6570\u7ec4\u6ea2\u51fa\u3002 1 2 3 4 5 6 7 8 9 10 11 12 #include #include int main () { char source []{ \"Copy this!\" }; char dest [ 5 ]; // note that the length of dest is only 5 chars! std :: strcpy ( dest , source ); // overflow! std :: cout << dest << '\\n' ; return 0 ; } \u8bb8\u591a\u7a0b\u5e8f\u5458\u5efa\u8bae\u4f7f\u7528 strncpy() \uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u6307\u5b9a\u7f13\u51b2\u533a\u7684\u5927\u5c0f\uff0c\u5e76\u786e\u4fdd\u4e0d\u4f1a\u53d1\u751f\u6ea2\u51fa\u3002\u4e0d\u5e78\u7684\u662f\uff0c' strncpy() \u4e0d\u80fd\u786e\u4fdd\u5b57\u7b26\u4e32\u4ee5\u7a7a\u5b57\u7b26\u7ed3\u5c3e\uff0c\u8fd9\u4ecd\u7136\u4e3a\u6570\u7ec4\u6ea2\u51fa\u7559\u4e0b\u4e86\u9690\u60a3\u3002 \u5728C++ 11\u4e2d\uff0c\u9996\u9009 strcpy_s() \uff0c\u5b83\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u53c2\u6570\u6765\u5b9a\u4e49\u76ee\u6807\u7684\u5927\u5c0f\u3002\u7136\u800c\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u7684\u7f16\u8bd1\u5668\u90fd\u652f\u6301\u8fd9\u4e2a\u51fd\u6570\uff0c\u8981\u4f7f\u7528\u5b83\uff0c\u4f60\u5fc5\u987b\u5b9a\u4e49 STDC_WANT_LIB_EXT1 \u5e76\u5c06\u5176\u5b9a\u4e49\u4e3a 1 \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #define __STDC_WANT_LIB_EXT1__ 1 #include // for strcpy_s #include int main () { char source []{ \"Copy this!\" }; char dest [ 5 ]; // note that the length of dest is only 5 chars! strcpy_s ( dest , 5 , source ); // A runtime error will occur in debug mode std :: cout << dest << '\\n' ; return 0 ; } \u56e0\u4e3a\u5e76\u4e0d\u662f\u6240\u6709\u7684\u7f16\u8bd1\u5668\u90fd\u652f\u6301 strcpy_s() \uff0c\u6240\u4ee5 strlcpy() \u662f\u4e00\u4e2a\u5f88\u53d7\u6b22\u8fce\u7684\u9009\u62e9\u2014\u2014\u5c3d\u7ba1\u5b83\u4e0d\u662f\u6807\u51c6\u7684\uff0c\u56e0\u6b64\u5f88\u591a\u7f16\u8bd1\u5668\u90fd\u4e0d\u5305\u542b\u5b83\u3002\u5b83\u4e5f\u6709\u81ea\u5df1\u7684\u4e00\u7cfb\u5217\u95ee\u9898\u3002\u7b80\u800c\u8a00\u4e4b\uff0c\u5982\u679c\u60a8\u9700\u8981\u590d\u5236C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u8fd9\u91cc\u6ca1\u6709\u666e\u904d\u63a8\u8350\u7684\u89e3\u51b3\u65b9\u6848\u3002 \u53e6\u4e00\u4e2a\u6709\u7528\u7684\u51fd\u6570\u662f strlen() \u51fd\u6570\uff0c\u5b83\u8fd4\u56deC\u98ce\u683c\u5b57\u7b26\u4e32\u7684\u957f\u5ea6(\u6ca1\u6709\u7a7a\u5b57\u7b26)\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include #include // for std::size int main () { char name [ 20 ]{ \"Alex\" }; // only use 5 characters (4 letters + null terminator) std :: cout << \"My name is: \" << name << '\\n' ; std :: cout << name << \" has \" << std :: strlen ( name ) << \" letters. \\n \" ; std :: cout << name << \" has \" << std :: size ( name ) << \" characters in the array. \\n \" ; // use sizeof(name) / sizeof(name[0]) if not C++17 capable return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 My name is: Alex Alex has 4 letters. Alex has 20 characters in the array. \u6ce8\u610f\u8fa8\u6790 strlen() \u548c std::size() \u7684\u533a\u522b\u3002 strlen() \u4f1a\u8fd4\u56de\u7a7a\u5b57\u7b26\u524d\u6240\u6709\u5b57\u7b26\u7684\u4e2a\u6570\uff0c\u800c std::size (\u6216\u8005 sizeof() \u6280\u5de7)\u5219\u4f1a\u8fd4\u56de\u6574\u4e2a\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u4e0d\u7ba1\u6570\u7ec4\u5185\u90e8\u5b58\u653e\u4e86\u4ec0\u4e48\u3002 \u5176\u4ed6\u51fd\u6570\uff1a strcat() -- \u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u62fc\u63a5\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u540e\u9762\uff08\u5371\u9669\u64cd\u4f5c\uff09 strncat() -- \u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u62fc\u63a5\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u540e\u9762 (\u4f1a\u68c0\u67e5buffer\u957f\u5ea6) strcmp() -- \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\uff08\u76f8\u7b49\u8fd4\u56de 0\uff09 strncmp() -- \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\uff08\u76f8\u7b49\u8fd4\u56de 0\uff09 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u672c\u8bfe\u4e2d\u7684\u4e00\u4e9b\u6982\u5ff5\u7f16\u5199\u7684\u793a\u4f8b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include #include // for std::size int main () { // Ask the user to enter a string char buffer [ 255 ] {}; std :: cout << \"Enter a string: \" ; std :: cin . getline ( buffer , std :: size ( buffer )); int spacesFound { 0 }; int bufferLength { static_cast < int > ( std :: strlen ( buffer )) }; // Loop through all of the characters the user entered for ( int index { 0 }; index < bufferLength ; ++ index ) { // If the current character is a space, count it if ( buffer [ index ] == ' ' ) ++ spacesFound ; } std :: cout << \"You typed \" << spacesFound << \" spaces! \\n \" ; return 0 ; } \u6ce8\u610f\u6211\u4eec\u5728\u5faa\u73af\u5916\u4f7f\u7528 strlen(buffer) \uff0c\u8fd9\u6837\u5b83\u4e4b\u540e\u8ba1\u7b97\u4e00\u6b21\uff0c\u800c\u4e0d\u662f\u6bcf\u6b21\u5faa\u73af\u90fd\u53bb\u8ba1\u7b97\u3002","title":"\u64cd\u4f5c C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32"},{"location":"11-6-C-style-strings/#c_2","text":"\u4e86\u89e3C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u662f\u5f88\u91cd\u8981\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u5728\u5f88\u591a\u4ee3\u7801\u4e2d\u90fd\u6709\u4f7f\u7528\u3002\u7136\u800c\uff0c\u65e2\u7136\u5df2\u7ecf\u89e3\u91ca\u4e86\u5b83\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u5efa\u8bae\u4f60\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u5b83\u4eec\uff01\u9664\u975e\u6709\u7279\u5b9a\u7684\u3001\u4ee4\u4eba\u4fe1\u670d\u7684\u7406\u7531\u4f7f\u7528C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u5426\u5219\u8bf7\u4f7f\u7528std::string(\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d)\u7c7b\u578b\u7684\u5b57\u7b26\u4e32\uff0c\u5b83\u66f4\u7b80\u5355\u3001\u66f4\u5b89\u5168\u4e5f\u66f4\u7075\u6d3b\u3002\u5728\u6781\u5c11\u6570\u60c5\u51b5\u4e0b\uff0c\u4f60\u786e\u5b9e\u9700\u8981\u4f7f\u7528\u56fa\u5b9a\u7684\u7f13\u51b2\u533a\u5927\u5c0f\u548cC\u98ce\u683c\u7684\u5b57\u7b26\u4e32(\u4f8b\u5982\u5185\u5b58\u6709\u9650\u7684\u8bbe\u5907)\uff0c\u6211\u4eec\u5efa\u8bae\u4f7f\u7528\u4e00\u4e2a\u6d4b\u8bd5\u826f\u597d\u7684\u7b2c\u4e09\u65b9\u5b57\u7b26\u4e32\u5e93\uff0c\u6216 std::string_view \uff0c\u8fd9\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u8ba8\u8bba\u3002 \u6cd5\u5219 \u4f7f\u7528 std::string \u6216 std::string_view \u6765\u4ee3\u66ff C \u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002","title":"\u4e0d\u8981\u4f7f\u7528 C \u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32"},{"location":"11-7-std-string-view-part-2/","tags":["std::string_view","C++17"],"text":"Key Takeaway std:: string_view \u63d0\u4f9b\u4e86\u4e00\u4e2a\u89c2\u5bdf\u5b57\u7b26\u4e32\u7684\u89c6\u56fe\uff0c\u5b57\u7b26\u4e32\u672c\u8eab\u5b58\u653e\u5728\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d \u521b\u5efa std:: string_view \u65f6\u4e0d\u4f1a\u53d1\u751f\u590d\u5236\uff0c\u4f46\u662f\u4fee\u6539\u65f6\u4f1a\u5f71\u54cd\u5230\u6240\u6709\u7684\u5bf9\u8c61\u3002 remove_prefix \u548c remove_suffix \u5206\u522b\u7528\u4e8e\u4ece\u5b57\u7b26\u4e32\u89c6\u56fe\u7684\u5de6\u4fa7\u548c\u53f3\u4fa7\u5220\u9664\u5b57\u7b26\uff08\u4e0d\u4f1a\u5f71\u54cd\u5230\u539f\u5b57\u7b26\u4e32\uff09 \u8981\u5c06 std:: string_view \u8f6c\u6362\u4e3aC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u8f6c\u6362\u4e3a std:: string \u4f7f\u7528 str.c_str() \u83b7\u53d6C\u98ce\u683c\u5b57\u7b26\u4e32 \u5982\u679c\u6211\u4eec\u60f3\u5199\u4e00\u4e2a\u63a5\u53d7\u5b57\u7b26\u4e32\u5f62\u53c2\u7684\u51fd\u6570\uff0c\u5c06\u5f62\u53c2\u8bbe\u7f6e\u4e3a std:: string_view \u662f\u6700\u7075\u6d3b\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u9ad8\u6548\u5730\u914d\u5408C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u53c2\u6570(\u5305\u62ec\u5b57\u7b26\u4e32\u5b57\u9762\u91cf)\u3001 std:: string \u53c2\u6570(\u5c06\u9690\u5f0f\u8f6c\u6362\u4e3a' std:: string_view ')\u548c std:: string_view \u53c2\u6570\u6765\u5de5\u4f5c\uff1a \u4f18\u5148\u4f7f\u7528 std:: string_view ( \u6309\u503c\u4f20\u9012 )\u800c\u4e0d\u662f const std:: string& \uff0c\u9664\u975e\u4f60\u9700\u8981\u8c03\u7528\u5176\u4ed6\u8981\u6c42\u4f7f\u7528C\u98ce\u683c\u5b57\u7b26\u4e32\u6216 std::string \u7684\u51fd\u6570\u3002 \u786e\u4fdd std::string_view \u89c2\u5bdf\u7684\u5b57\u7b26\u4e32\u4e0d\u4f1a \u79bb\u5f00\u4f5c\u7528\u57df \uff0c\u4e5f\u4e0d\u4f1a\u88ab\u4fee\u6539\u3002 \u56e0\u4e3a std::string_view \u7684\u751f\u547d\u5468\u671f\u662f\u72ec\u7acb\u4e8e\u5b83\u201c\u89c2\u5bdf\u201d\u7684\u5b57\u7b26\u4e32\u7684\uff08\u4e5f\u5c31\u662f\u8bf4\u8be5\u5b57\u7b26\u4e32\u5bf9\u8c61\u53ef\u4ee5\u5728\u5148\u4e8e std::string_view \u88ab\u9500\u6bc1)\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u8bbf\u95ee std::string_view \u5c31\u4f1a\u9020\u6210 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u53ea\u6709\u5728 std::string_view \u6ca1\u6709\u88ab\u4fee\u6539\u7684\u60c5\u51b5\u4e0b\u4e14\u5b57\u7b26\u4e32\u4ee5\u7a7a\u7ed3\u675f\u7b26\u7ed3\u5c3e\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528 std::string_view::data() \u5982\u679c\u5b57\u7b26\u4e32\u6ca1\u6709\u4ee5\u7a7a\u7ed3\u675f\u7b26\u7ed3\u5c3e\uff0c\u5219 std::string_view::data() \u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 \u4f5c\u8005\u6ce8 \u672c\u8bfe\u7a0b\u7684\u90e8\u5206\u5185\u5bb9\u88ab\u79fb\u52a8\u5230\u4e86 4.18 - std:: string_view \u7b80\u4ecb \u3002\u56e0\u6b64\uff0c\u8fd9\u8282\u8bfe\u4e2d\u6709\u90e8\u5206\u5c1a\u672a\u6e05\u7406\u7684\u91cd\u590d\u90e8\u5206\u3002 \u5728\u4e0a\u4e00\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86 C \u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u4f46\u662f\u4f7f\u7528\u5b83\u4eec\u7684\u98ce\u9669\u5f88\u9ad8\u3002\u5c3d\u7ba1 C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u7684\u6027\u80fd\u5f88\u597d\uff0c\u4f46\u662f\u5b83\u4eec\u5e76\u4e0d\u6613\u7528\uff0c\u5b89\u5168\u6027\u4e5f\u4e0d\u5982 std:: string \u3002 \u4f46\u662f std:: string (\u5728 4.17 - std:: string \u7b80\u4ecb \u4e2d\u4ecb\u7ecd)\u4e5f\u6709\u5b83\u7684\u7f3a\u70b9\uff0c\u5c24\u5176\u662f\u5f53\u6211\u4eec\u9700\u8981\u5e38\u91cf\u5b57\u7b26\u4e32\u7684\u65f6\u5019\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include int main () { char text []{ \"hello\" }; std :: string str { text }; std :: string more { str }; std :: cout << text << ' ' << str << ' ' << more << '\\n' ; return 0 ; } \u548c\u671f\u671b\u7684\u4e00\u6837\uff0c\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 hello hello hello main \u51fd\u6570\u4f1a\u5c06\u5b57\u7b26\u4e32 \"hello\" \u590d\u5236 3 \u6b21\uff0c\u603b\u5171\u5f97\u5230 4 \u4e2a\u526f\u672c\u3002\u9996\u5148\u662f\u5b57\u7b26\u4e32\u5b57\u9762\u91cf \u201chello\u201d \uff0c\u5b83\u5728\u7f16\u8bd1\u65f6\u5df2\u77e5\uff0c\u5e76\u5b58\u50a8\u5728\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d\u3002\u5f53\u6211\u4eec\u521b\u5efa char[] \u65f6\uff0c\u4f1a\u521b\u5efa\u4e00\u4e2a\u526f\u672c\u3002\u63a5\u4e0b\u6765\u7684\u4e24\u4e2a std:: string \u5bf9\u8c61\u5206\u522b\u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u4e32\u526f\u672c\u3002\u56e0\u4e3a std:: string \u88ab\u8bbe\u8ba1\u4e3a\u53ef\u4fee\u6539\u7684\uff0c\u6bcf\u4e2a std:: string \u5fc5\u987b\u5305\u542b\u5b83\u81ea\u5df1\u7684\u5b57\u7b26\u4e32\u526f\u672c\uff0c\u8fd9\u6837\u7ed9\u5b9a\u7684 std:: string \u53ef\u4ee5\u88ab\u4fee\u6539\u800c\u4e0d\u5f71\u54cd\u4efb\u4f55\u5176\u4ed6\u7684 std:: string \u5bf9\u8c61\u3002 \u5373\u4f7f const std:: string \u4e5f\u5b58\u5728\u8fd9\u4e2a\u95ee\u9898\uff0c\u5373\u4f7f\u5b83\u4eec\u4e0d\u80fd\u4fee\u6539\u3002 std:: string_view \u60f3\u8c61\u8fd9\u6837\u4e00\u4e2a\u573a\u666f\uff0c\u4f60\u5750\u5728\u5bb6\u4e2d\uff0c\u4ece\u7a97\u6237\u671b\u5411\u8857\u9053\u7684\u65f6\u5019\u4f60\u770b\u5230\u4e00\u8f86\u6c7d\u8f66\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u5b83\uff0c\u4f46\u4f60\u5374\u65e0\u6cd5\u89e6\u6478\u6216\u8005\u79fb\u52a8\u5b83\u3002\u4f60\u7684\u7a97\u6237\u53ea\u662f\u4e3a\u4f60\u63d0\u4f9b\u4e86\u4e00\u4e2a\u53ef\u4ee5\u89c2\u5bdf\u6c7d\u8f66\u7684\u89c6\u56fe\uff0c\u800c\u8f66\u5219\u662f\u5b8c\u5168\u72ec\u7acb\u7684\u5bf9\u8c61\u3002 C++17 \u5f15\u5165\u4e86\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u53e6\u5916\u4e00\u79cd\u65b9\u5f0f\u2014\u2014 std:: string_view \uff0c\u5b83\u901a\u8fc7 \u5934\u6587\u4ef6\u63d0\u4f9b\u3002 \u548c std:: string \u4e0d\u540c\u7684\u662f\uff0c\u5b83\u4e0d\u4f1a\u4fdd\u5b58\u4e00\u4efd\u5b57\u7b26\u4e32\u7684\u526f\u672c\uff0c std:: string_view \u53ea\u662f\u63d0\u4f9b\u4e86\u4e00\u4e2a\u88ab\u5b9a\u4e49\u5728 \u5176\u4ed6\u5730\u65b9 \u7684\u5b57\u7b26\u4e32\u7684\u89c6\u56fe\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 std:: string_view \u91cd\u65b0\u7f16\u5199\u4e0a\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include int main () { std :: string_view text { \"hello\" }; // view the text \"hello\", which is stored in the binary std :: string_view str { text }; // view of the same \"hello\" std :: string_view more { str }; // view of the same \"hello\" std :: cout << text << ' ' << str << ' ' << more << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u662f\u76f8\u540c\u7684\uff0c\u4f46\u8fd9\u6b21\u4e0d\u518d\u9700\u8981\u521b\u5efa\u5b57\u7b26\u4e32 \u201chello\u201d \u7684\u526f\u672c\u3002\u5b57\u7b26\u4e32 \"hello\" \u5b58\u50a8\u5728\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d\uff0c\u4e0d\u4f1a\u5728\u8fd0\u884c\u65f6\u5206\u914d\u3002 text \u53ea\u662f\u5b57\u7b26\u4e32 \"hello\" \u4e0a\u7684\u4e00\u4e2a\u89c6\u56fe\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u521b\u5efa\u526f\u672c\u3002\u5f53\u6211\u4eec\u590d\u5236 std:: string_view \u65f6\uff0c\u65b0\u7684 std:: string_view \u89c2\u5bdf\u5230\u7684\u5b57\u7b26\u4e32\u4e0e\u88ab\u590d\u5236\u7684 std:: string_view \u662f\u76f8\u540c\u3002\u8fd9\u610f\u5473\u7740 str \u548c more \u90fd\u4e0d\u4f1a\u521b\u5efa\u4efb\u4f55\u526f\u672c\u3002\u5b83\u4eec\u662f\u73b0\u6709\u5b57\u7b26\u4e32 \"hello\" \u7684\u89c6\u56fe\u3002 std:: string_view \u4e0d\u4ec5\u4ec5\u5feb\uff0c\u800c\u4e14\u8fd8\u5177\u6709 std:: string \u7684\u5927\u90e8\u5206\u529f\u80fd\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include int main () { std :: string_view str { \"Trains are fast!\" }; std :: cout << str . length () << '\\n' ; // 16 std :: cout << str . substr ( 0 , str . find ( ' ' )) << '\\n' ; // Trains std :: cout << ( str == \"Trains are fast!\" ) << '\\n' ; // 1 // Since C++20 std :: cout << str . starts_with ( \"Boats\" ) << '\\n' ; // 0 std :: cout << str . ends_with ( \"fast!\" ) << '\\n' ; // 1 std :: cout << str << '\\n' ; // Trains are fast! return 0 ; } \u7531\u4e8e std:: string_view \u5e76\u4e0d\u4f1a\u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u526f\u672c\uff0c\u5f53\u6211\u4eec\u4fee\u6539 std:: string_view \u7684\u65f6\u5019\uff0c\u88ab\u89c2\u5bdf\u7684\u5b57\u7b26\u4e32\u4e5f\u4f1a\u88ab\u4fee\u6539\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { char arr []{ \"Gold\" }; std :: string_view str { arr }; std :: cout << str << '\\n' ; // Gold // Change 'd' to 'f' in arr arr [ 3 ] = 'f' ; std :: cout << str << '\\n' ; // Golf return 0 ; } \u6211\u4eec\u4fee\u6539\u4e86 arr \uff0c\u4f46\u662f str \u4e5f\u88ab\u4fee\u6539\u4e86\u3002\u8fd9\u662f\u56e0\u4e3a\u5b83\u4eec\u5b9e\u9645\u4e0a\u5171\u4eab\u7684\u662f\u540c\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u5982\u679c\u4f60\u9700\u8981\u4f7f\u7528 std:: string_view \uff0c\u90a3\u4e48\u6700\u597d\u4e0d\u8981\u53bb\u4fee\u6539\u5b83\u5bf9\u5e94\u7684\u5b57\u7b26\u4e32\uff0c\u907f\u514d\u9020\u6210\u56f0\u6270\u548c\u9519\u8bef\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528 std:: string_view \u4ee3\u66ff C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u3002 \u5bf9\u4e8e \u53ea\u8bfb \u5b57\u7b26\u4e32\uff0c\u4f18\u5148\u4f7f\u7528 std:: string_view \u800c\u4e0d\u662f std:: string \u3002\u9664\u975e\u4f60\u5df2\u7ecf\u5177\u6709\u4e86\u4e00\u4e2a std:: string \u3002 \u89c6\u56fe\u4fee\u6539\u51fd\u6570 \u56de\u5230\u4e4b\u524d\u6709\u5173\u201d\u7a97\u6237\u201c\u7684\u6bd4\u55bb\uff0c\u8003\u8651\u4e00\u4e2a\u6709\u201d\u7a97\u5e18\u201c\u7684\u7a97\u6237\u3002\u901a\u8fc7\u62c9\u4e0a\u5de6\u4fa7\u6216\u53f3\u4fa7\u7684\u7a97\u5e18\uff0c\u6211\u4eec\u53ef\u4ee5\u9650\u5236\u80fd\u591f\u4ece\u7a97\u6237\u770b\u5230\u7684\u8303\u56f4\uff0c\u4f46\u662f\u8fd9\u5e76\u4e0d\u4f1a\u5b9e\u9645\u6539\u53d8\u7a97\u6237\u5916\u7684\u7269\u4f53\uff0c\u6211\u4eec\u53ea\u662f\u51cf\u5c0f\u4e86\u89c6\u89d2\u800c\u5df2\u3002 \u7c7b\u4f3c\u5730\uff0c std:: string_view \u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\u7528\u4e8e\u64cd\u4f5c\u89c2\u5bdf\u5b57\u7b26\u4e32\u7684\u89c6\u56fe\u3002\u901a\u8fc7\u8fd9\u4e9b\u51fd\u6570\u6211\u4eec\u53ef\u4ee5\u4fee\u6539\u770b\u5230\u7684\u5b57\u7b26\u4e32\uff0c\u4f46\u4e0d\u4f1a\u4fee\u6539\u88ab\u89c2\u5bdf\u7684\u5b57\u7b26\u4e32\u672c\u8eab\u3002 remove_prefix \u7528\u4e8e\u4ece\u89c6\u56fe\u7684\u5de6\u4fa7\u5220\u9664\u5b57\u7b26\uff0c\u800c remove_suffix \u5219\u7528\u4e8e\u4ece\u89c6\u56fe\u7684\u53f3\u4fa7\u5220\u9664\u5b57\u7b26\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include int main () { std :: string_view str { \"Peach\" }; std :: cout << str << '\\n' ; // Ignore the first character. str . remove_prefix ( 1 ); std :: cout << str << '\\n' ; // Ignore the last 2 characters. str . remove_suffix ( 2 ); std :: cout << str << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 3 Peach each ea \u4e0e\u771f\u6b63\u7684\u7a97\u5e18\u4e0d\u540c\uff0c std:: string_view \u4e0d\u80fd\u88ab\u91cd\u65b0\u6253\u5f00\u3002\u4e00\u65e6\u4f60\u7f29\u5c0f\u4e86\u533a\u57df\uff0c\u91cd\u65b0\u6269\u5927\u5b83\u7684\u552f\u4e00\u65b9\u6cd5\u5c31\u662f\u901a\u8fc7\u91cd\u65b0\u5206\u914d\u6e90\u5b57\u7b26\u4e32\u7ed9\u5b83\u6765\u91cd\u7f6e\u89c6\u56fe\u3002 std::string_view \u53ef\u4ee5\u914d\u5408\u6ca1\u6709\u7a7a\u5b57\u7b26\u7ed3\u675f\u7b26\u7684\u5b57\u7b26\u4e32 C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u548c std:: string \u4e0d\u540c\uff0c std:: string_view \u4e0d\u4f7f\u7528\u7a7a\u7ed3\u675f\u7b26\u6765\u6807\u8bb0\u5b57\u7b26\u4e32\u7684\u7ed3\u675f\u3002\u76f8\u53cd\uff0c\u5b83\u77e5\u9053\u5b57\u7b26\u4e32\u5728\u54ea\u91cc\u7ed3\u675f\uff0c\u56e0\u4e3a\u5b83\u4f1a\u8ddf\u8e2a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include // For std::size #include int main () { // No null-terminator. char vowels []{ 'a' , 'e' , 'i' , 'o' , 'u' }; // vowels \u5e76\u6ca1\u6709\u5305\u542b\u7ed3\u675f\u7b26\uff0c\u6211\u4eec\u9700\u8981\u4f20\u9012\u957f\u5ea6 // \u56e0\u4e3a vowels \u662f\u6570\u7ec4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 std::size \u6765\u83b7\u53d6\u957f\u5ea6 std :: string_view str { vowels , std :: size ( vowels ) }; std :: cout << str << '\\n' ; // \u8fd9\u662f\u5b89\u5168\u7684\u3002std::cout \u77e5\u9053\u5982\u4f55\u6253\u5370 std::string_view. return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 aeiou \u5c06 std::string_view \u8f6c\u6362\u4e3a C \u98ce\u683c\u5b57\u7b26\u4e32 \u4e00\u4e9b\u65e7\u7684\u51fd\u6570(\u6bd4\u5982\u65e7\u7684 strlen \u51fd\u6570)\u4ecd\u7136\u671f\u671b\u4f7f\u7528C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002\u8981\u5c06 std:: string_view \u8f6c\u6362\u4e3aC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u8f6c\u6362\u4e3a std:: string \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include #include #include int main () { std :: string_view sv { \"balloon\" }; sv . remove_suffix ( 3 ); // Create a std::string from the std::string_view std :: string str { sv }; // Get the null-terminated C-style string. auto szNullTerminated { str . c_str () }; // Pass the null-terminated string to the function that we want to use. std :: cout << str << \" has \" << std :: strlen ( szNullTerminated ) << \" letter(s) \\n \" ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 ball has 4 letter(s) \u4e0d\u8fc7\uff0c\u6bcf\u6b21 std:: string_view \u9700\u8981\u88ab\u4f5c\u4e3aC\u98ce\u683c\u5b57\u7b26\u4e32\u4f20\u9012\u65f6\uff0c\u90fd\u9700\u8981\u521b\u5efa\u4e00\u4e2a std::string \uff0c\u8fd9\u4e48\u505a\u7684\u5f00\u9500\u662f\u975e\u5e38\u5927\u7684\uff0c\u56e0\u6b64\u5e94\u8be5\u5c3d\u91cf\u907f\u514d\u8fd9\u4e48\u505a\u3002 \u901a\u8fc7 const std::string& \u8fd8\u662f std::string_view \u6765\u4f20\u9012\u5b57\u7b26\u4e32\uff1f \u4e00\u4e2a\u7ecf\u5e38\u9047\u5230\u7684\u95ee\u9898\u662f\uff1a\u5e94\u8be5\u901a\u8fc7 const std::string& \u8fd8\u662f std::string_view \u4f20\u9012\u5b57\u7b26\u4e32\uff1f\u90a3\u79cd\u65b9\u5f0f\u6700\u597d\uff1f \u5982\u679c\u6211\u4eec\u60f3\u5199\u4e00\u4e2a\u63a5\u53d7\u5b57\u7b26\u4e32\u5f62\u53c2\u7684\u51fd\u6570\uff0c\u5c06\u5f62\u53c2\u8bbe\u7f6e\u4e3a std::string_view \u662f\u6700\u7075\u6d3b\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u9ad8\u6548\u5730\u914d\u5408C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u53c2\u6570(\u5305\u62ec\u5b57\u7b26\u4e32\u5b57\u9762\u91cf)\u3001 std::string \u53c2\u6570(\u5c06\u9690\u5f0f\u8f6c\u6362\u4e3a std::string_view ')\u548c std::string_view \u53c2\u6570\u6765\u5de5\u4f5c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include #include void printSV ( std :: string_view sv ) { std :: cout << sv << '\\n' ; } int main () { std :: string s { \"Hello, world\" }; std :: string_view sv { s }; printSV ( s ); // ok: pass std::string printSV ( sv ); // ok: pass std::string_view printSV ( \"Hello, world\" ); // ok: pass C-style string literal return 0 ; } \u6ce8\u610f\uff0c\u6211\u4eec\u901a\u8fc7\u503c\u4f20\u9012 std::string_view \uff0c\u800c\u4e0d\u662f\u901a\u8fc7 const \u5f15\u7528\u7c7b\u578b\u6765\u4f20\u9012\u3002\u8fd9\u662f\u56e0\u4e3a std::string_view \u7684\u590d\u5236\u901f\u5ea6\u901a\u5e38\u8f83\u5feb\uff0c\u800c \u6309\u503c\u4f20\u9012 \u5bf9\u4e8e\u590d\u5236\u5f00\u9500\u8f83\u5c0f\u7684\u7c7b\u578b\u6765\u8bf4\u662f\u6700\u4f18\u7684\u3002 \u6709\u4e00\u79cd\u60c5\u51b5\uff0c\u5c06\u5f62\u53c2\u8bbe\u7f6e\u4e3a const std::string& \u901a\u5e38\u66f4\u597d\uff1a\u5982\u679c\u4f60\u7684\u51fd\u6570\u9700\u8981\u8c03\u7528\u5176\u4ed6\u63a5\u53d7C\u98ce\u683c\u5b57\u7b26\u4e32\u6216 std::string \u5f62\u53c2\u7684\u51fd\u6570\uff0c\u90a3\u4e48 const std::string& \u53ef\u80fd\u662f\u66f4\u597d\u7684\u9009\u62e9\uff0c\u56e0\u4e3a std::string_view \u4e0d\u80fd\u4fdd\u8bc1\u4ee5\u7a7a\u5b57\u7b26\u7ed3\u675f(\u800c\u8fd9\u662fC\u98ce\u683c\u5b57\u7b26\u4e32\u51fd\u6570\u6240\u671f\u671b\u7684)\uff0c\u5e76\u4e14\u4e0d\u80fd\u6709\u6548\u5730\u8f6c\u6362\u56de std::string \u3002 \u6700\u4f73\u5b9e\u8df5 \u4f18\u5148\u4f7f\u7528 std:: string_view ( \u6309\u503c\u4f20\u9012 )\u800c\u4e0d\u662f const std:: string& \uff0c\u9664\u975e\u4f60\u9700\u8981\u8c03\u7528\u5176\u4ed6\u8981\u6c42\u4f7f\u7528C\u98ce\u683c\u5b57\u7b26\u4e32\u6216 std::string \u7684\u51fd\u6570\u3002 \u4f5c\u8005\u6ce8 \u540e\u9762\u7684\u4e00\u4e9b\u8bfe\u7a0b\u662f\u5728\u5f15\u5165 std::string_view \u4e4b\u524d\u7f16\u5199\u7684\uff0c\u6240\u4ee5\u4ecd\u7136\u4f7f\u7528\u7684\u662f const std::string& \uff08\u5b9e\u9645\u4e0a\u7528 std::string_view \u66f4\u597d\uff09\uff0c\u6211\u4eec\u6b63\u5728\u7740\u624b\u6e05\u7406\u76f8\u5173\u7684\u4ee3\u7801\u3002 \u6240\u6709\u6743\u95ee\u9898 std::string_view \u7684\u751f\u547d\u5468\u671f\u662f\u72ec\u7acb\u4e8e\u5b83\u201c\u89c2\u5bdf\u201d\u7684\u5b57\u7b26\u4e32\u7684\uff08\u4e5f\u5c31\u662f\u8bf4\u8be5\u5b57\u7b26\u4e32\u5bf9\u8c61\u53ef\u4ee5\u5728\u5148\u4e8e std::string_view \u88ab\u9500\u6bc1)\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u8bbf\u95ee std::string_view \u5c31\u4f1a\u9020\u6210 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 std::string_view \u8868\u793a\u7684\u5b57\u7b26\u4e32\u5fc5\u987b\u662f\u5728\u5176\u4ed6\u5730\u65b9\u5df2\u7ecf\u88ab\u521b\u5efa\u7684\u5b57\u7b26\u4e32\u3002\u5b83\u53ef\u80fd\u662f\u4e00\u4e2a\u968f\u7740\u7a0b\u5e8f\u542f\u52a8\u505c\u6b62\u800c\u521b\u5efa\u9500\u6bc1\u7684 \u5b57\u9762\u91cf \uff0c\u4e5f\u53ef\u80fd\u662f\u4e00\u4e2a std::string \uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5f53 str::string \u9500\u6bc1\u65f6\u8be5\u5b57\u7b26\u4e32\u4e5f\u5c31\u4e0d\u5b58\u5728\u4e86\u3002 std:: string_view \u81ea\u8eab\u4e0d\u80fd\u521b\u5efa\u4efb\u4f55\u7684\u5b57\u7b26\u4e32\uff0c\u5b83\u53ea\u662f\u89c2\u5bdf\u67d0\u4e2a\u5b57\u7b26\u4e32\u7684\u89c6\u56fe\u3002 \u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\u5c31\u51fa\u73b0\u4e86\u6240\u6709\u6743\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include #include #include std :: string_view askForName () { std :: cout << \"What's your name? \\n \" ; // Use a std::string, because std::cin needs to modify it. std :: string name {}; std :: cin >> name ; // We're switching to std::string_view for demonstrative purposes only. // If you already have a std::string, there's no reason to switch to // a std::string_view. std :: string_view view { name }; std :: cout << \"Hello \" << view << '\\n' ; return view ; } // name \u53d8\u91cf\u88ab\u9500\u6bc1\uff0c\u5bf9\u5e94\u7684\u5b57\u7b26\u4e32\u4e5f\u9500\u6bc1\u4e86 int main () { std :: string_view view { askForName () }; // view \u6b63\u5728\u4e3a\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u5b57\u7b26\u4e32\u63d0\u4f9b\u89c6\u56fe std :: cout << \"Your name is \" << view << '\\n' ; // \u672a\u5b9a\u4e49\u884c\u4e3a return 0 ; } 1 2 3 4 What's your name? nascardriver Hello nascardriver Your name is \ufffdP@\ufffdP@ \u5728 askForName() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u53d8\u91cf name \u5e76\u4e14\u901a\u8fc7 std::cin \u5bf9\u5176\u8fdb\u884c\u4e86\u8d4b\u503c\u3002\u7136\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u53d8\u91cf view \uff0c\u901a\u8fc7\u8fd9\u4e2a\u53d8\u91cf\u6211\u4eec\u53ef\u201c\u89c2\u5bdf\u201d\u4e0a\u8ff0\u5b57\u7b26\u4e32\u3002\u5728\u51fd\u6570\u7ed3\u675f\u65f6\uff0c\u6211\u4eec\u8fd4\u56de view \uff0c\u4f46\u662f\u8be5\u53d8\u91cf\u5b9e\u9645\u89c2\u5bdf\u7684\u5b57\u7b26\u4e32 ( name ) \u5df2\u7ecf\u88ab\u9500\u6bc1\u4e86\uff0c\u56e0\u6b64 view \u73b0\u5728\u6307\u5411\u7684\u662f\u4e00\u5757\u5df2\u7ecf\u88ab\u91ca\u653e\u7684\u5185\u5b58\u3002\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a \u60ac\u5782(dangling) std::string_view . \u5728 mian \u51fd\u6570\u4e2d\u8bbf\u95ee\u88ab\u8fd4\u56de\u7684 std::string_view \u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\uff0c\u4f1a\u6253\u5370\u5947\u602a\u7684\u5b57\u7b26\u3002 \u95ee\u9898\u53d1\u751f\u7684\u539f\u56e0\u5728\u4e8e\u4f60\u901a\u8fc7 std::string \u521b\u5efa\u4e86\u4e00\u4e2a std::string_view \uff0c\u7136\u540e\u53c8\u4fee\u6539\u4e86 std::string \u3002\u4fee\u6539 std::string \u4f1a\u5bfc\u81f4\u5185\u90e8\u5b57\u7b26\u4e32\u88ab\u9500\u6bc1\u6216\u88ab\u66ff\u6362\uff08\u5728\u4ee3\u7801\u7684\u5176\u4ed6\u90e8\u5206\uff09\u3002 std::string_view \u603b\u662f\u6307\u5411\u8be5\u5b57\u7b26\u4e32\uff0c \u4f46\u8be5\u5b57\u7b26\u4e32\u53ef\u80fd\u5df2\u7ecf\u4e0d\u662f\u5b83\u539f\u6765\u7684\u6a21\u6837\u6216\u8005\u5df2\u7ecf\u4e0d\u5b58\u5728\u4e86\u3002 \u6ce8\u610f \u786e\u4fdd std::string_view \u89c2\u5bdf\u7684\u5b57\u7b26\u4e32\u4e0d\u4f1a \u79bb\u5f00\u4f5c\u7528\u57df \uff0c\u4e5f\u4e0d\u4f1a\u88ab\u4fee\u6539\u3002 \u4f7f\u7528 data() \u51fd\u6570\u6765\u6253\u5f00\u201d\u7a97\u6237\u201c std::string_view \u89c2\u5bdf\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u901a\u8fc7 data() \u51fd\u6570\u6765\u8bbf\u95ee\uff0c\u5b83\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u4e00\u4e2aC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u5f0f\u63d0\u4f9b\u4e86\u4e00\u79cd\u5feb\u901f\u8bbf\u95ee\u88ab\u89c2\u5bdf\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3002\u4f46\u662f\u5b83\u53ea\u80fd\u5728 std::string_view \u89c6\u56fe\u6ca1\u6709\u88ab\u4fee\u6539\u7684\u65f6\u5019\u4f7f\u7528(\u4f8b\u5982\u901a\u8fc7 remove_prefix \u6216 remove_suffix \u4fee\u6539\u89c6\u56fe)\uff0c\uff0c\u8be5\u5b57\u7b26\u4e32\u662f\u6709\u7a7a\u7ed3\u675f\u7b26\u7684\u3002 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c std::strlen \u51fd\u6570\u4e0d\u80fd\u8bc6\u522b std:: string_view \uff0c\u56e0\u6b64\u6211\u4eec\u5fc5\u987b\u4f20\u5165 str.data() \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include // For std::strlen #include #include int main () { std :: string_view str { \"balloon\" }; std :: cout << str << '\\n' ; // We use std::strlen because it's simple, this could be any other function // that needs a null-terminated string. // It's okay to use data() because we haven't modified the view, and the // string is null-terminated. std :: cout << std :: strlen ( str . data ()) << '\\n' ; return 0 ; } 1 2 balloon 7 \u5f53 std:: string_view \u88ab\u4fee\u6539\u540e\uff0c data() \u5c31\u4e0d\u80fd\u5b8c\u6210\u6211\u4eec\u671f\u671b\u7684\u4efb\u52a1\u4e86\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5f53\u89c6\u56fe\u88ab\u4fee\u6539\u540e data() \u7684\u884c\u4e3a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include #include int main () { std :: string_view str { \"balloon\" }; // Remove the \"b\" str . remove_prefix ( 1 ); // remove the \"oon\" str . remove_suffix ( 3 ); // Remember that the above doesn't modify the string, it only changes // the region that str is observing. std :: cout << str << \" has \" << std :: strlen ( str . data ()) << \" letter(s) \\n \" ; std :: cout << \"str.data() is \" << str . data () << '\\n' ; std :: cout << \"str is \" << str << '\\n' ; return 0 ; } 1 2 3 all has 6 letter(s) str.data() is alloon str is all \u8fd9\u663e\u7136\u4e0d\u662f\u6211\u4eec\u671f\u671b\u7684\uff0c\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528 data() \u6765\u8bbf\u95ee\u4e00\u4e2a\u5df2\u7ecf\u88ab\u4fee\u6539\u7684 std::string_view \u3002\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u4fe1\u606f\u5728\u6211\u4eec\u4f7f\u7528 data() \u65f6\u5c31\u4e22\u5931\u4e86\uff0c std:: strlen \u548c std:: cout \u4f1a\u6301\u7eed\u8bfb\u53d6\u76f4\u5230\u9047\u5230\u7b2c\u4e00\u4e2a\u7a7a\u7ed3\u675f\u7b26\uff0c\u4e5f\u5c31\u662f\u201cballoon\u201d\u7684\u672b\u5c3e\u3002 \u6ce8\u610f \u53ea\u6709\u5728 std::string_view \u6ca1\u6709\u88ab\u4fee\u6539\u7684\u60c5\u51b5\u4e0b\u4e14\u5b57\u7b26\u4e32\u4ee5\u7a7a\u7ed3\u675f\u7b26\u7ed3\u5c3e\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528 std:: string_view:: data() \u5982\u679c\u5b57\u7b26\u4e32\u6ca1\u6709\u4ee5\u7a7a\u7ed3\u675f\u7b26\u7ed3\u5c3e\uff0c\u5219 std:: string_view:: data() \u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 \u4e0d\u5b8c\u6574\u7684\u5b9e\u73b0 \u4f5c\u4e3a\u4e00\u4e2a\u76f8\u5bf9\u8f83\u65b0\u7684\u7279\u6027\u6765\u8bf4\uff0c std::string_view \u7684\u5b9e\u73b0\u5b8c\u6210\u5ea6\u5e76\u4e0d\u5982\u9884\u671f\u3002 ```cpp hl_lines=\"5,6\" std::string s{ \"hello\" }; std::string_view v{ \"world\" }; // \u4e0d\u80fd\u5de5\u4f5c std::cout << (s + v) << '\\n'; std::cout << (v + s) << '\\n'; // \u53ef\u80fd\u4e0d\u5b89\u5168\uff0c\u4e5f\u53ef\u80fd\u5e76\u4e0d\u662f\u6211\u4eec\u5e0c\u671b\u7684\u505a\u6cd5\uff0c\u56e0\u4e3a\u6211\u4eec\u6b64\u65f6\u5c06std::string_view\u5f53\u505a // C\u98ce\u683c\u5b57\u7b26\u4e32\u6765\u4f7f\u7528 std::cout << (s + v.data()) << '\\n'; std::cout << (v.data() + s) << '\\n'; // \u53ef\u4ee5, \u53ef\u4ee5\u4f46\u662f\u4e0d\u4f18\u96c5\uff0c\u800c\u4e14\u6d6a\u8d39\u8d44\u6e90\uff0c\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u6784\u5efa\u4e00\u4e2a\u65b0\u7684 std::string. std::cout << (s + std::string{ v }) << '\\n'; std::cout << (std::string{ v } + s) << '\\n'; std::cout << (s + static_cast (v)) << '\\n'; std::cout << (static_cast (v) + s) << '\\n'; ``` \u6ca1\u9053\u7406\u4e0d\u80fd\u4f7f\u7528\u7b2c\u4e94\u884c\u548c\u7b2c\u516d\u884c\u7684\u65b9\u5f0f\uff0c\u8fd9\u4e2a\u7279\u6027\u53ef\u80fd\u4f1a\u5728\u672a\u6765\u7684C++\u7248\u672c\u4e2d\u88ab\u652f\u6301\u3002","title":"11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09"},{"location":"11-7-std-string-view-part-2/#std-string_view","text":"\u60f3\u8c61\u8fd9\u6837\u4e00\u4e2a\u573a\u666f\uff0c\u4f60\u5750\u5728\u5bb6\u4e2d\uff0c\u4ece\u7a97\u6237\u671b\u5411\u8857\u9053\u7684\u65f6\u5019\u4f60\u770b\u5230\u4e00\u8f86\u6c7d\u8f66\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u5b83\uff0c\u4f46\u4f60\u5374\u65e0\u6cd5\u89e6\u6478\u6216\u8005\u79fb\u52a8\u5b83\u3002\u4f60\u7684\u7a97\u6237\u53ea\u662f\u4e3a\u4f60\u63d0\u4f9b\u4e86\u4e00\u4e2a\u53ef\u4ee5\u89c2\u5bdf\u6c7d\u8f66\u7684\u89c6\u56fe\uff0c\u800c\u8f66\u5219\u662f\u5b8c\u5168\u72ec\u7acb\u7684\u5bf9\u8c61\u3002 C++17 \u5f15\u5165\u4e86\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u53e6\u5916\u4e00\u79cd\u65b9\u5f0f\u2014\u2014 std:: string_view \uff0c\u5b83\u901a\u8fc7 \u5934\u6587\u4ef6\u63d0\u4f9b\u3002 \u548c std:: string \u4e0d\u540c\u7684\u662f\uff0c\u5b83\u4e0d\u4f1a\u4fdd\u5b58\u4e00\u4efd\u5b57\u7b26\u4e32\u7684\u526f\u672c\uff0c std:: string_view \u53ea\u662f\u63d0\u4f9b\u4e86\u4e00\u4e2a\u88ab\u5b9a\u4e49\u5728 \u5176\u4ed6\u5730\u65b9 \u7684\u5b57\u7b26\u4e32\u7684\u89c6\u56fe\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 std:: string_view \u91cd\u65b0\u7f16\u5199\u4e0a\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include int main () { std :: string_view text { \"hello\" }; // view the text \"hello\", which is stored in the binary std :: string_view str { text }; // view of the same \"hello\" std :: string_view more { str }; // view of the same \"hello\" std :: cout << text << ' ' << str << ' ' << more << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u662f\u76f8\u540c\u7684\uff0c\u4f46\u8fd9\u6b21\u4e0d\u518d\u9700\u8981\u521b\u5efa\u5b57\u7b26\u4e32 \u201chello\u201d \u7684\u526f\u672c\u3002\u5b57\u7b26\u4e32 \"hello\" \u5b58\u50a8\u5728\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d\uff0c\u4e0d\u4f1a\u5728\u8fd0\u884c\u65f6\u5206\u914d\u3002 text \u53ea\u662f\u5b57\u7b26\u4e32 \"hello\" \u4e0a\u7684\u4e00\u4e2a\u89c6\u56fe\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u521b\u5efa\u526f\u672c\u3002\u5f53\u6211\u4eec\u590d\u5236 std:: string_view \u65f6\uff0c\u65b0\u7684 std:: string_view \u89c2\u5bdf\u5230\u7684\u5b57\u7b26\u4e32\u4e0e\u88ab\u590d\u5236\u7684 std:: string_view \u662f\u76f8\u540c\u3002\u8fd9\u610f\u5473\u7740 str \u548c more \u90fd\u4e0d\u4f1a\u521b\u5efa\u4efb\u4f55\u526f\u672c\u3002\u5b83\u4eec\u662f\u73b0\u6709\u5b57\u7b26\u4e32 \"hello\" \u7684\u89c6\u56fe\u3002 std:: string_view \u4e0d\u4ec5\u4ec5\u5feb\uff0c\u800c\u4e14\u8fd8\u5177\u6709 std:: string \u7684\u5927\u90e8\u5206\u529f\u80fd\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include int main () { std :: string_view str { \"Trains are fast!\" }; std :: cout << str . length () << '\\n' ; // 16 std :: cout << str . substr ( 0 , str . find ( ' ' )) << '\\n' ; // Trains std :: cout << ( str == \"Trains are fast!\" ) << '\\n' ; // 1 // Since C++20 std :: cout << str . starts_with ( \"Boats\" ) << '\\n' ; // 0 std :: cout << str . ends_with ( \"fast!\" ) << '\\n' ; // 1 std :: cout << str << '\\n' ; // Trains are fast! return 0 ; } \u7531\u4e8e std:: string_view \u5e76\u4e0d\u4f1a\u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u526f\u672c\uff0c\u5f53\u6211\u4eec\u4fee\u6539 std:: string_view \u7684\u65f6\u5019\uff0c\u88ab\u89c2\u5bdf\u7684\u5b57\u7b26\u4e32\u4e5f\u4f1a\u88ab\u4fee\u6539\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { char arr []{ \"Gold\" }; std :: string_view str { arr }; std :: cout << str << '\\n' ; // Gold // Change 'd' to 'f' in arr arr [ 3 ] = 'f' ; std :: cout << str << '\\n' ; // Golf return 0 ; } \u6211\u4eec\u4fee\u6539\u4e86 arr \uff0c\u4f46\u662f str \u4e5f\u88ab\u4fee\u6539\u4e86\u3002\u8fd9\u662f\u56e0\u4e3a\u5b83\u4eec\u5b9e\u9645\u4e0a\u5171\u4eab\u7684\u662f\u540c\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u5982\u679c\u4f60\u9700\u8981\u4f7f\u7528 std:: string_view \uff0c\u90a3\u4e48\u6700\u597d\u4e0d\u8981\u53bb\u4fee\u6539\u5b83\u5bf9\u5e94\u7684\u5b57\u7b26\u4e32\uff0c\u907f\u514d\u9020\u6210\u56f0\u6270\u548c\u9519\u8bef\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528 std:: string_view \u4ee3\u66ff C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u3002 \u5bf9\u4e8e \u53ea\u8bfb \u5b57\u7b26\u4e32\uff0c\u4f18\u5148\u4f7f\u7528 std:: string_view \u800c\u4e0d\u662f std:: string \u3002\u9664\u975e\u4f60\u5df2\u7ecf\u5177\u6709\u4e86\u4e00\u4e2a std:: string \u3002","title":"std:: string_view"},{"location":"11-7-std-string-view-part-2/#_1","text":"\u56de\u5230\u4e4b\u524d\u6709\u5173\u201d\u7a97\u6237\u201c\u7684\u6bd4\u55bb\uff0c\u8003\u8651\u4e00\u4e2a\u6709\u201d\u7a97\u5e18\u201c\u7684\u7a97\u6237\u3002\u901a\u8fc7\u62c9\u4e0a\u5de6\u4fa7\u6216\u53f3\u4fa7\u7684\u7a97\u5e18\uff0c\u6211\u4eec\u53ef\u4ee5\u9650\u5236\u80fd\u591f\u4ece\u7a97\u6237\u770b\u5230\u7684\u8303\u56f4\uff0c\u4f46\u662f\u8fd9\u5e76\u4e0d\u4f1a\u5b9e\u9645\u6539\u53d8\u7a97\u6237\u5916\u7684\u7269\u4f53\uff0c\u6211\u4eec\u53ea\u662f\u51cf\u5c0f\u4e86\u89c6\u89d2\u800c\u5df2\u3002 \u7c7b\u4f3c\u5730\uff0c std:: string_view \u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\u7528\u4e8e\u64cd\u4f5c\u89c2\u5bdf\u5b57\u7b26\u4e32\u7684\u89c6\u56fe\u3002\u901a\u8fc7\u8fd9\u4e9b\u51fd\u6570\u6211\u4eec\u53ef\u4ee5\u4fee\u6539\u770b\u5230\u7684\u5b57\u7b26\u4e32\uff0c\u4f46\u4e0d\u4f1a\u4fee\u6539\u88ab\u89c2\u5bdf\u7684\u5b57\u7b26\u4e32\u672c\u8eab\u3002 remove_prefix \u7528\u4e8e\u4ece\u89c6\u56fe\u7684\u5de6\u4fa7\u5220\u9664\u5b57\u7b26\uff0c\u800c remove_suffix \u5219\u7528\u4e8e\u4ece\u89c6\u56fe\u7684\u53f3\u4fa7\u5220\u9664\u5b57\u7b26\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include int main () { std :: string_view str { \"Peach\" }; std :: cout << str << '\\n' ; // Ignore the first character. str . remove_prefix ( 1 ); std :: cout << str << '\\n' ; // Ignore the last 2 characters. str . remove_suffix ( 2 ); std :: cout << str << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 3 Peach each ea \u4e0e\u771f\u6b63\u7684\u7a97\u5e18\u4e0d\u540c\uff0c std:: string_view \u4e0d\u80fd\u88ab\u91cd\u65b0\u6253\u5f00\u3002\u4e00\u65e6\u4f60\u7f29\u5c0f\u4e86\u533a\u57df\uff0c\u91cd\u65b0\u6269\u5927\u5b83\u7684\u552f\u4e00\u65b9\u6cd5\u5c31\u662f\u901a\u8fc7\u91cd\u65b0\u5206\u914d\u6e90\u5b57\u7b26\u4e32\u7ed9\u5b83\u6765\u91cd\u7f6e\u89c6\u56fe\u3002","title":"\u89c6\u56fe\u4fee\u6539\u51fd\u6570"},{"location":"11-7-std-string-view-part-2/#stdstring_view","text":"C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u548c std:: string \u4e0d\u540c\uff0c std:: string_view \u4e0d\u4f7f\u7528\u7a7a\u7ed3\u675f\u7b26\u6765\u6807\u8bb0\u5b57\u7b26\u4e32\u7684\u7ed3\u675f\u3002\u76f8\u53cd\uff0c\u5b83\u77e5\u9053\u5b57\u7b26\u4e32\u5728\u54ea\u91cc\u7ed3\u675f\uff0c\u56e0\u4e3a\u5b83\u4f1a\u8ddf\u8e2a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include // For std::size #include int main () { // No null-terminator. char vowels []{ 'a' , 'e' , 'i' , 'o' , 'u' }; // vowels \u5e76\u6ca1\u6709\u5305\u542b\u7ed3\u675f\u7b26\uff0c\u6211\u4eec\u9700\u8981\u4f20\u9012\u957f\u5ea6 // \u56e0\u4e3a vowels \u662f\u6570\u7ec4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 std::size \u6765\u83b7\u53d6\u957f\u5ea6 std :: string_view str { vowels , std :: size ( vowels ) }; std :: cout << str << '\\n' ; // \u8fd9\u662f\u5b89\u5168\u7684\u3002std::cout \u77e5\u9053\u5982\u4f55\u6253\u5370 std::string_view. return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 aeiou","title":"std::string_view \u53ef\u4ee5\u914d\u5408\u6ca1\u6709\u7a7a\u5b57\u7b26\u7ed3\u675f\u7b26\u7684\u5b57\u7b26\u4e32"},{"location":"11-7-std-string-view-part-2/#stdstring_view-c","text":"\u4e00\u4e9b\u65e7\u7684\u51fd\u6570(\u6bd4\u5982\u65e7\u7684 strlen \u51fd\u6570)\u4ecd\u7136\u671f\u671b\u4f7f\u7528C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002\u8981\u5c06 std:: string_view \u8f6c\u6362\u4e3aC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u8f6c\u6362\u4e3a std:: string \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include #include #include int main () { std :: string_view sv { \"balloon\" }; sv . remove_suffix ( 3 ); // Create a std::string from the std::string_view std :: string str { sv }; // Get the null-terminated C-style string. auto szNullTerminated { str . c_str () }; // Pass the null-terminated string to the function that we want to use. std :: cout << str << \" has \" << std :: strlen ( szNullTerminated ) << \" letter(s) \\n \" ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 ball has 4 letter(s) \u4e0d\u8fc7\uff0c\u6bcf\u6b21 std:: string_view \u9700\u8981\u88ab\u4f5c\u4e3aC\u98ce\u683c\u5b57\u7b26\u4e32\u4f20\u9012\u65f6\uff0c\u90fd\u9700\u8981\u521b\u5efa\u4e00\u4e2a std::string \uff0c\u8fd9\u4e48\u505a\u7684\u5f00\u9500\u662f\u975e\u5e38\u5927\u7684\uff0c\u56e0\u6b64\u5e94\u8be5\u5c3d\u91cf\u907f\u514d\u8fd9\u4e48\u505a\u3002","title":"\u5c06\u00a0std::string_view\u00a0\u8f6c\u6362\u4e3a C \u98ce\u683c\u5b57\u7b26\u4e32"},{"location":"11-7-std-string-view-part-2/#const-stdstring-stdstring_view","text":"\u4e00\u4e2a\u7ecf\u5e38\u9047\u5230\u7684\u95ee\u9898\u662f\uff1a\u5e94\u8be5\u901a\u8fc7 const std::string& \u8fd8\u662f std::string_view \u4f20\u9012\u5b57\u7b26\u4e32\uff1f\u90a3\u79cd\u65b9\u5f0f\u6700\u597d\uff1f \u5982\u679c\u6211\u4eec\u60f3\u5199\u4e00\u4e2a\u63a5\u53d7\u5b57\u7b26\u4e32\u5f62\u53c2\u7684\u51fd\u6570\uff0c\u5c06\u5f62\u53c2\u8bbe\u7f6e\u4e3a std::string_view \u662f\u6700\u7075\u6d3b\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u9ad8\u6548\u5730\u914d\u5408C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u53c2\u6570(\u5305\u62ec\u5b57\u7b26\u4e32\u5b57\u9762\u91cf)\u3001 std::string \u53c2\u6570(\u5c06\u9690\u5f0f\u8f6c\u6362\u4e3a std::string_view ')\u548c std::string_view \u53c2\u6570\u6765\u5de5\u4f5c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include #include void printSV ( std :: string_view sv ) { std :: cout << sv << '\\n' ; } int main () { std :: string s { \"Hello, world\" }; std :: string_view sv { s }; printSV ( s ); // ok: pass std::string printSV ( sv ); // ok: pass std::string_view printSV ( \"Hello, world\" ); // ok: pass C-style string literal return 0 ; } \u6ce8\u610f\uff0c\u6211\u4eec\u901a\u8fc7\u503c\u4f20\u9012 std::string_view \uff0c\u800c\u4e0d\u662f\u901a\u8fc7 const \u5f15\u7528\u7c7b\u578b\u6765\u4f20\u9012\u3002\u8fd9\u662f\u56e0\u4e3a std::string_view \u7684\u590d\u5236\u901f\u5ea6\u901a\u5e38\u8f83\u5feb\uff0c\u800c \u6309\u503c\u4f20\u9012 \u5bf9\u4e8e\u590d\u5236\u5f00\u9500\u8f83\u5c0f\u7684\u7c7b\u578b\u6765\u8bf4\u662f\u6700\u4f18\u7684\u3002 \u6709\u4e00\u79cd\u60c5\u51b5\uff0c\u5c06\u5f62\u53c2\u8bbe\u7f6e\u4e3a const std::string& \u901a\u5e38\u66f4\u597d\uff1a\u5982\u679c\u4f60\u7684\u51fd\u6570\u9700\u8981\u8c03\u7528\u5176\u4ed6\u63a5\u53d7C\u98ce\u683c\u5b57\u7b26\u4e32\u6216 std::string \u5f62\u53c2\u7684\u51fd\u6570\uff0c\u90a3\u4e48 const std::string& \u53ef\u80fd\u662f\u66f4\u597d\u7684\u9009\u62e9\uff0c\u56e0\u4e3a std::string_view \u4e0d\u80fd\u4fdd\u8bc1\u4ee5\u7a7a\u5b57\u7b26\u7ed3\u675f(\u800c\u8fd9\u662fC\u98ce\u683c\u5b57\u7b26\u4e32\u51fd\u6570\u6240\u671f\u671b\u7684)\uff0c\u5e76\u4e14\u4e0d\u80fd\u6709\u6548\u5730\u8f6c\u6362\u56de std::string \u3002 \u6700\u4f73\u5b9e\u8df5 \u4f18\u5148\u4f7f\u7528 std:: string_view ( \u6309\u503c\u4f20\u9012 )\u800c\u4e0d\u662f const std:: string& \uff0c\u9664\u975e\u4f60\u9700\u8981\u8c03\u7528\u5176\u4ed6\u8981\u6c42\u4f7f\u7528C\u98ce\u683c\u5b57\u7b26\u4e32\u6216 std::string \u7684\u51fd\u6570\u3002 \u4f5c\u8005\u6ce8 \u540e\u9762\u7684\u4e00\u4e9b\u8bfe\u7a0b\u662f\u5728\u5f15\u5165 std::string_view \u4e4b\u524d\u7f16\u5199\u7684\uff0c\u6240\u4ee5\u4ecd\u7136\u4f7f\u7528\u7684\u662f const std::string& \uff08\u5b9e\u9645\u4e0a\u7528 std::string_view \u66f4\u597d\uff09\uff0c\u6211\u4eec\u6b63\u5728\u7740\u624b\u6e05\u7406\u76f8\u5173\u7684\u4ee3\u7801\u3002","title":"\u901a\u8fc7 const std::string& \u8fd8\u662f std::string_view \u6765\u4f20\u9012\u5b57\u7b26\u4e32\uff1f"},{"location":"11-7-std-string-view-part-2/#_2","text":"std::string_view \u7684\u751f\u547d\u5468\u671f\u662f\u72ec\u7acb\u4e8e\u5b83\u201c\u89c2\u5bdf\u201d\u7684\u5b57\u7b26\u4e32\u7684\uff08\u4e5f\u5c31\u662f\u8bf4\u8be5\u5b57\u7b26\u4e32\u5bf9\u8c61\u53ef\u4ee5\u5728\u5148\u4e8e std::string_view \u88ab\u9500\u6bc1)\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u8bbf\u95ee std::string_view \u5c31\u4f1a\u9020\u6210 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 std::string_view \u8868\u793a\u7684\u5b57\u7b26\u4e32\u5fc5\u987b\u662f\u5728\u5176\u4ed6\u5730\u65b9\u5df2\u7ecf\u88ab\u521b\u5efa\u7684\u5b57\u7b26\u4e32\u3002\u5b83\u53ef\u80fd\u662f\u4e00\u4e2a\u968f\u7740\u7a0b\u5e8f\u542f\u52a8\u505c\u6b62\u800c\u521b\u5efa\u9500\u6bc1\u7684 \u5b57\u9762\u91cf \uff0c\u4e5f\u53ef\u80fd\u662f\u4e00\u4e2a std::string \uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5f53 str::string \u9500\u6bc1\u65f6\u8be5\u5b57\u7b26\u4e32\u4e5f\u5c31\u4e0d\u5b58\u5728\u4e86\u3002 std:: string_view \u81ea\u8eab\u4e0d\u80fd\u521b\u5efa\u4efb\u4f55\u7684\u5b57\u7b26\u4e32\uff0c\u5b83\u53ea\u662f\u89c2\u5bdf\u67d0\u4e2a\u5b57\u7b26\u4e32\u7684\u89c6\u56fe\u3002 \u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\u5c31\u51fa\u73b0\u4e86\u6240\u6709\u6743\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include #include #include std :: string_view askForName () { std :: cout << \"What's your name? \\n \" ; // Use a std::string, because std::cin needs to modify it. std :: string name {}; std :: cin >> name ; // We're switching to std::string_view for demonstrative purposes only. // If you already have a std::string, there's no reason to switch to // a std::string_view. std :: string_view view { name }; std :: cout << \"Hello \" << view << '\\n' ; return view ; } // name \u53d8\u91cf\u88ab\u9500\u6bc1\uff0c\u5bf9\u5e94\u7684\u5b57\u7b26\u4e32\u4e5f\u9500\u6bc1\u4e86 int main () { std :: string_view view { askForName () }; // view \u6b63\u5728\u4e3a\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u5b57\u7b26\u4e32\u63d0\u4f9b\u89c6\u56fe std :: cout << \"Your name is \" << view << '\\n' ; // \u672a\u5b9a\u4e49\u884c\u4e3a return 0 ; } 1 2 3 4 What's your name? nascardriver Hello nascardriver Your name is \ufffdP@\ufffdP@ \u5728 askForName() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u53d8\u91cf name \u5e76\u4e14\u901a\u8fc7 std::cin \u5bf9\u5176\u8fdb\u884c\u4e86\u8d4b\u503c\u3002\u7136\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u53d8\u91cf view \uff0c\u901a\u8fc7\u8fd9\u4e2a\u53d8\u91cf\u6211\u4eec\u53ef\u201c\u89c2\u5bdf\u201d\u4e0a\u8ff0\u5b57\u7b26\u4e32\u3002\u5728\u51fd\u6570\u7ed3\u675f\u65f6\uff0c\u6211\u4eec\u8fd4\u56de view \uff0c\u4f46\u662f\u8be5\u53d8\u91cf\u5b9e\u9645\u89c2\u5bdf\u7684\u5b57\u7b26\u4e32 ( name ) \u5df2\u7ecf\u88ab\u9500\u6bc1\u4e86\uff0c\u56e0\u6b64 view \u73b0\u5728\u6307\u5411\u7684\u662f\u4e00\u5757\u5df2\u7ecf\u88ab\u91ca\u653e\u7684\u5185\u5b58\u3002\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a \u60ac\u5782(dangling) std::string_view . \u5728 mian \u51fd\u6570\u4e2d\u8bbf\u95ee\u88ab\u8fd4\u56de\u7684 std::string_view \u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\uff0c\u4f1a\u6253\u5370\u5947\u602a\u7684\u5b57\u7b26\u3002 \u95ee\u9898\u53d1\u751f\u7684\u539f\u56e0\u5728\u4e8e\u4f60\u901a\u8fc7 std::string \u521b\u5efa\u4e86\u4e00\u4e2a std::string_view \uff0c\u7136\u540e\u53c8\u4fee\u6539\u4e86 std::string \u3002\u4fee\u6539 std::string \u4f1a\u5bfc\u81f4\u5185\u90e8\u5b57\u7b26\u4e32\u88ab\u9500\u6bc1\u6216\u88ab\u66ff\u6362\uff08\u5728\u4ee3\u7801\u7684\u5176\u4ed6\u90e8\u5206\uff09\u3002 std::string_view \u603b\u662f\u6307\u5411\u8be5\u5b57\u7b26\u4e32\uff0c \u4f46\u8be5\u5b57\u7b26\u4e32\u53ef\u80fd\u5df2\u7ecf\u4e0d\u662f\u5b83\u539f\u6765\u7684\u6a21\u6837\u6216\u8005\u5df2\u7ecf\u4e0d\u5b58\u5728\u4e86\u3002 \u6ce8\u610f \u786e\u4fdd std::string_view \u89c2\u5bdf\u7684\u5b57\u7b26\u4e32\u4e0d\u4f1a \u79bb\u5f00\u4f5c\u7528\u57df \uff0c\u4e5f\u4e0d\u4f1a\u88ab\u4fee\u6539\u3002","title":"\u6240\u6709\u6743\u95ee\u9898"},{"location":"11-7-std-string-view-part-2/#data","text":"std::string_view \u89c2\u5bdf\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u901a\u8fc7 data() \u51fd\u6570\u6765\u8bbf\u95ee\uff0c\u5b83\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u4e00\u4e2aC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u5f0f\u63d0\u4f9b\u4e86\u4e00\u79cd\u5feb\u901f\u8bbf\u95ee\u88ab\u89c2\u5bdf\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3002\u4f46\u662f\u5b83\u53ea\u80fd\u5728 std::string_view \u89c6\u56fe\u6ca1\u6709\u88ab\u4fee\u6539\u7684\u65f6\u5019\u4f7f\u7528(\u4f8b\u5982\u901a\u8fc7 remove_prefix \u6216 remove_suffix \u4fee\u6539\u89c6\u56fe)\uff0c\uff0c\u8be5\u5b57\u7b26\u4e32\u662f\u6709\u7a7a\u7ed3\u675f\u7b26\u7684\u3002 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c std::strlen \u51fd\u6570\u4e0d\u80fd\u8bc6\u522b std:: string_view \uff0c\u56e0\u6b64\u6211\u4eec\u5fc5\u987b\u4f20\u5165 str.data() \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include // For std::strlen #include #include int main () { std :: string_view str { \"balloon\" }; std :: cout << str << '\\n' ; // We use std::strlen because it's simple, this could be any other function // that needs a null-terminated string. // It's okay to use data() because we haven't modified the view, and the // string is null-terminated. std :: cout << std :: strlen ( str . data ()) << '\\n' ; return 0 ; } 1 2 balloon 7 \u5f53 std:: string_view \u88ab\u4fee\u6539\u540e\uff0c data() \u5c31\u4e0d\u80fd\u5b8c\u6210\u6211\u4eec\u671f\u671b\u7684\u4efb\u52a1\u4e86\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5f53\u89c6\u56fe\u88ab\u4fee\u6539\u540e data() \u7684\u884c\u4e3a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include #include int main () { std :: string_view str { \"balloon\" }; // Remove the \"b\" str . remove_prefix ( 1 ); // remove the \"oon\" str . remove_suffix ( 3 ); // Remember that the above doesn't modify the string, it only changes // the region that str is observing. std :: cout << str << \" has \" << std :: strlen ( str . data ()) << \" letter(s) \\n \" ; std :: cout << \"str.data() is \" << str . data () << '\\n' ; std :: cout << \"str is \" << str << '\\n' ; return 0 ; } 1 2 3 all has 6 letter(s) str.data() is alloon str is all \u8fd9\u663e\u7136\u4e0d\u662f\u6211\u4eec\u671f\u671b\u7684\uff0c\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528 data() \u6765\u8bbf\u95ee\u4e00\u4e2a\u5df2\u7ecf\u88ab\u4fee\u6539\u7684 std::string_view \u3002\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u4fe1\u606f\u5728\u6211\u4eec\u4f7f\u7528 data() \u65f6\u5c31\u4e22\u5931\u4e86\uff0c std:: strlen \u548c std:: cout \u4f1a\u6301\u7eed\u8bfb\u53d6\u76f4\u5230\u9047\u5230\u7b2c\u4e00\u4e2a\u7a7a\u7ed3\u675f\u7b26\uff0c\u4e5f\u5c31\u662f\u201cballoon\u201d\u7684\u672b\u5c3e\u3002 \u6ce8\u610f \u53ea\u6709\u5728 std::string_view \u6ca1\u6709\u88ab\u4fee\u6539\u7684\u60c5\u51b5\u4e0b\u4e14\u5b57\u7b26\u4e32\u4ee5\u7a7a\u7ed3\u675f\u7b26\u7ed3\u5c3e\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528 std:: string_view:: data() \u5982\u679c\u5b57\u7b26\u4e32\u6ca1\u6709\u4ee5\u7a7a\u7ed3\u675f\u7b26\u7ed3\u5c3e\uff0c\u5219 std:: string_view:: data() \u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u3002","title":"\u4f7f\u7528data()\u51fd\u6570\u6765\u6253\u5f00\u201d\u7a97\u6237\u201c"},{"location":"11-7-std-string-view-part-2/#_3","text":"\u4f5c\u4e3a\u4e00\u4e2a\u76f8\u5bf9\u8f83\u65b0\u7684\u7279\u6027\u6765\u8bf4\uff0c std::string_view \u7684\u5b9e\u73b0\u5b8c\u6210\u5ea6\u5e76\u4e0d\u5982\u9884\u671f\u3002 ```cpp hl_lines=\"5,6\" std::string s{ \"hello\" }; std::string_view v{ \"world\" }; // \u4e0d\u80fd\u5de5\u4f5c std::cout << (s + v) << '\\n'; std::cout << (v + s) << '\\n'; // \u53ef\u80fd\u4e0d\u5b89\u5168\uff0c\u4e5f\u53ef\u80fd\u5e76\u4e0d\u662f\u6211\u4eec\u5e0c\u671b\u7684\u505a\u6cd5\uff0c\u56e0\u4e3a\u6211\u4eec\u6b64\u65f6\u5c06std::string_view\u5f53\u505a // C\u98ce\u683c\u5b57\u7b26\u4e32\u6765\u4f7f\u7528 std::cout << (s + v.data()) << '\\n'; std::cout << (v.data() + s) << '\\n'; // \u53ef\u4ee5, \u53ef\u4ee5\u4f46\u662f\u4e0d\u4f18\u96c5\uff0c\u800c\u4e14\u6d6a\u8d39\u8d44\u6e90\uff0c\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u6784\u5efa\u4e00\u4e2a\u65b0\u7684 std::string. std::cout << (s + std::string{ v }) << '\\n'; std::cout << (std::string{ v } + s) << '\\n'; std::cout << (s + static_cast (v)) << '\\n'; std::cout << (static_cast (v) + s) << '\\n'; ``` \u6ca1\u9053\u7406\u4e0d\u80fd\u4f7f\u7528\u7b2c\u4e94\u884c\u548c\u7b2c\u516d\u884c\u7684\u65b9\u5f0f\uff0c\u8fd9\u4e2a\u7279\u6027\u53ef\u80fd\u4f1a\u5728\u672a\u6765\u7684C++\u7248\u672c\u4e2d\u88ab\u652f\u6301\u3002","title":"\u4e0d\u5b8c\u6574\u7684\u5b9e\u73b0"},{"location":"11-8-Pointers-and-arrays/","tags":["array","pointer"],"text":"Key Takeaway \u5728\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u6570\u7ec4\u65f6\uff0c\u6570\u7ec4\u4f1a \u9000\u5316 \uff08\u9690\u5f0f\u8f6c\u6362\uff09\u4e3a\u4e00\u4e2a\u6307\u9488 \u5f53\u5b9a\u4e49\u51fd\u6570\u7684\u53c2\u6570\u65f6\uff0c\u6700\u597d\u4f7f\u7528\u6307\u9488\u8bed\u6cd5( * )\u800c\u4e0d\u662f\u6570\u7ec4\u8bed\u6cd5( [] )\u3002 \u6570\u7ec4\u9000\u5316\u4e3a\u6307\u9488\u4f20\u9012\u5230\u51fd\u6570\u540e\uff0c\u4fee\u6539\u8be5\u6307\u9488\u4f1a\u4fee\u6539\u539f\u6570\u7ec4 \u4f5c\u4e3a\u7ed3\u6784\u6216\u7c7b\u7684\u4e00\u90e8\u5206\u7684\u6570\u7ec4\u4e0d\u4f1a\u5728\u5c06\u6574\u4e2a\u7ed3\u6784\u6216\u7c7b\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\u53d1\u751f\u9000\u5316\u3002\u5982\u679c\u6709\u9700\u8981\uff0c\u53ef\u4ee5\u7528\u8fd9\u79cd\u65b9\u5f0f\u9632\u6b62\u6570\u7ec4\u9000\u5316\u4e3a\u6307\u9488\u3002 \u5728 C++ \u4e2d\uff0c\u6307\u9488\u548c\u6570\u7ec4\u672c\u8d28\u4e0a\u662f\u76f8\u4e92\u8054\u7cfb\u7684\u3002 \u6570\u7ec4\u9000\u5316 \u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\uff1a 1 int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; // declare a fixed array of 5 integers \u5bf9\u4e8e\u6211\u4eec\u6765\u8bf4\uff0c\u8fd9\u662f\u4e00\u4e2a\u5305\u542b\u4e94\u4e2a\u6574\u6570\u7684\u6570\u7ec4\uff0c\u4f46\u662f\u5bf9\u4e8e\u7f16\u8bd1\u5668\u6765\u8bf4\uff0c\u5b83\u662f\u4e00\u4e2a int[5] \u7c7b\u578b\u7684\u6570\u7ec4\u3002\u6211\u4eec\u53ef\u4ee5\u77e5\u9053 array[0] , array[1] , array[2] , array[3] , \u548c array[4] \u7684\u503c (\u5206\u522b\u4e3a 9\u30017\u30015\u30013 \u548c 1 )\u3002 \u5728\u51e0\u4e4e\u6240\u6709\u60c5\u51b5\u4e0b\uff08\u4e24\u79cd\u4f8b\u5916\u60c5\u51b5\u7a0d\u540e\u4ecb\u7ecd\uff09\uff0c\u5f53\u6211\u4eec\u5728\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u6570\u7ec4\u65f6\uff0c\u6570\u7ec4\u4f1a \u9000\u5316 \uff08\u9690\u5f0f\u8f6c\u6362\uff09\u4e3a\u4e00\u4e2a\u6307\u9488\u3002\u53c2\u8003\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; // \u6253\u5370\u6570\u7ec4\u7684\u9996\u5730\u5740 std :: cout << \"Element 0 has address: \" << & array [ 0 ] << '\\n' ; // \u6253\u5370\u6570\u7ec4\u9000\u5316\u540e\u7684\u6307\u9488 std :: cout << \"The array decays to a pointer holding address: \" << array << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u5185\u5bb9\uff1a 1 2 Element 0 has address: 0042FD5C The array decays to a pointer holding address: 0042FD5C \u8ba4\u4e3a\u6570\u7ec4\u548c\u6307\u5411\u6570\u7ec4\u7684\u6307\u9488\u5b8c\u5168\u4e00\u6837\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9519\u8bef\u3002\u5b83\u4eec\u5e76\u4e0d\u662f\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6570\u7ec4\u7684\u7c7b\u578b\u662f\u201c int[5] \u201d\u5b83\u7684\u503c\u5c31\u662f\u6570\u7ec4\u7684\u5404\u4e2a\u5143\u7d20\u3002\u800c\u6307\u5411\u6570\u7ec4\u7684\u6307\u9488\uff0c\u5176\u7c7b\u578b\u4e3a\u201c int* \u201d\uff0c\u5176\u503c\u4e3a\u6570\u7ec4\u9996\u4e2a\u5143\u7d20\u7684\u5730\u5740\u3002 \u7a0d\u540e\u6211\u4eec\u5c31\u4f1a\u4ecb\u7ecd\u5b83\u4eec\u7684\u5177\u4f53\u533a\u522b\u4f53\u73b0\u5728\u54ea\u91cc\u3002 \u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4e5f\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u6765\u8bbf\u95ee\uff08\u4e0b\u8282\u8bfe\u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\uff09\uff0c\u4f46\u662f\u6211\u4eec\u4e0d\u80fd\u901a\u8fc7\u6307\u9488\u6765\u83b7\u53d6\u6570\u7ec4\u7684\u7c7b\u578b\u4fe1\u606f\u3002 \u7136\u800c\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u8fd8\u662f\u53ef\u4ee5\u4ee5\u76f8\u540c\u7684\u65b9\u5f0f\u5bf9\u5f85\u56fa\u5b9a\u6570\u7ec4\u548c\u6307\u9488\u3002 \u4f8b\u5982\uff0c\u53ef\u4ee5\u5bf9\u6570\u7ec4\u89e3\u5f15\u7528\u4ee5\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u503c: 1 2 3 4 5 6 7 int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; // Deferencing an array returns the first element (element 0) std :: cout << * array ; // will print 9! char name []{ \"Jason\" }; // C-style string (also an array) std :: cout << * name << '\\n' ; // will print 'J' \u6ce8\u610f\uff0c\u6211\u4eec\u6ca1\u6709\u5bf9\u6570\u7ec4\u672c\u8eab\u8fdb\u884c\u89e3\u5f15\u7528\u3002\u6570\u7ec4\u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a\u4e86\u6307\u9488( int* \u7c7b\u578b)\uff0c\u5e76\u5bf9\u6307\u9488\u89e3\u5f15\u7528\uff0c\u4ee5\u83b7\u5f97\u6307\u9488\u6240\u4fdd\u5b58\u7684\u5185\u5b58\u5730\u5740\u7684\u503c(\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u503c)\u3002 \u6211\u4eec\u8fd8\u53ef\u4ee5\u8ba9\u4e00\u4e2a\u6307\u9488\u6307\u5411\u6570\u7ec4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; std :: cout << * array << '\\n' ; // will print 9 int * ptr { array }; std :: cout << * ptr << '\\n' ; // will print 9 return 0 ; } \u6570\u7ec4\u9000\u5316\u4e3a\u4e86 int* \u7c7b\u578b\u7684\u6307\u9488\uff0c\u800c ptr \u4e5f\u662f\u6b64\u7c7b\u578b\u7684\u6307\u9488\u3002 \u6307\u9488\u548c\u56fa\u5b9a\u6570\u7ec4\u7684\u5dee\u5f02 \u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u56fa\u5b9a\u6570\u7ec4\u548c\u6307\u9488\u4e4b\u95f4\u7684\u7c7b\u578b\u5dee\u5f02\u4f1a\u5e26\u6765\u95ee\u9898\u3002\u8fd9\u4e9b\u95ee\u9898\u6709\u52a9\u4e8e\u6211\u4eec\u89e3\u91ca\u56fa\u5b9a\u6570\u7ec4\u548c\u6307\u9488\u7684\u4e0d\u540c\u3002 \u4e3b\u8981\u7684\u5dee\u5f02\u53d1\u751f\u5728\u4f7f\u7528 sizeof() \u64cd\u4f5c\u7b26\u65f6\u3002\u5f53\u7528\u4e8e\u56fa\u5b9a\u6570\u7ec4\u65f6\uff0c sizeof \u8fd4\u56de\u6574\u4e2a\u6570\u7ec4\u7684\u5927\u5c0f( array length * element size )\u3002\u5f53\u7528\u4e8e\u6307\u9488\u65f6\uff0c sizeof \u8fd4\u56de\u6307\u9488\u7684\u5927\u5c0f(\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d)\u3002\u4e0b\u9762\u7684\u7a0b\u5e8f\u8bf4\u660e\u4e86\u8fd9\u4e00\u70b9: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; std :: cout << sizeof ( array ) << '\\n' ; // will print sizeof(int) * array length int * ptr { array }; std :: cout << sizeof ( ptr ) << '\\n' ; // will print the size of a pointer return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 2 20 4 \u56fa\u5b9a\u6570\u7ec4\u77e5\u9053\u5b83\u6240\u6307\u5411\u7684\u6570\u7ec4\u6709\u591a\u957f\uff0c\u800c\u6307\u5411\u6570\u7ec4\u7684\u6307\u9488\u5e76\u4e0d\u77e5\u9053\u6570\u7ec4\u6709\u591a\u957f\u3002 \u7b2c\u4e8c\u4e2a\u533a\u522b\u53d1\u751f\u5728\u4f7f\u7528\u53d6\u5730\u5740\u64cd\u4f5c\u7b26( & )\u65f6\u3002\u5bf9\u6307\u9488\u4f7f\u7528\u53d6\u5730\u5740\u64cd\u4f5c\u7b26\uff0c\u4f1a\u5f97\u5230\u6307\u9488\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u3002\u5bf9\u6570\u7ec4\u4f7f\u7528\u53d6\u5730\u5740\u64cd\u4f5c\u7b26\uff0c\u5c06\u8fd4\u56de\u6307\u5411\u5730\u5740\u7684\u6307\u9488\uff0c\u8fd9\u4e2a\u6307\u9488\u6307\u5411\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u4f46\u662f\u7c7b\u578b\u4fe1\u606f\u4e0d\u540c(\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c &array \u7684\u7c7b\u578b\u662f int(*)[5] )\u3002\u4f60\u4e0d\u592a\u53ef\u80fd\u4f1a\u7528\u5230\u5b83\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; std :: cout << array << '\\n' ; // \u7c7b\u578b\u4e3a int[5], \u6253\u5370 009DF9D4 std :: cout << & array << '\\n' ; // \u7c7b\u578b\u4e3a int(*)[5], \u6253\u5370 009DF9D4 std :: cout << '\\n' ; int * ptr { array }; std :: cout << ptr << '\\n' ; // \u7c7b\u578b\u4e3a int*, \u6253\u5370 009DF9D4 std :: cout << & ptr << '\\n' ; // \u7c7b\u578b\u4e3a int**, \u6253\u5370 009DF9C8 return 0 ; } // h/t to reader PacMan for this example \u5c06\u56fa\u5b9a\u6570\u7ec4\u4f20\u9012\u7ed9\u51fd\u6570 \u5728 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u62f7\u8d1d\u5927\u578b\u6570\u7ec4\u7684\u5f00\u9500\u662f\u5f88\u5927\u7684\uff0cC++\u5728\u5c06\u6570\u7ec4\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\uff0c\u5e76\u4e0d\u4f1a\u5c06\u5176\u62f7\u8d1d\u4e00\u4efd\u3002\u5b9e\u9645\u4e0a\uff0c\u56fa\u5b9a\u6570\u7ec4\u5728\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\u4e5f\u4f1a\u9000\u5316\u4e3a\u6307\u9488\uff0c\u8be5\u6307\u9488\u4f1a\u88ab\u4f20\u9012\u7ed9\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void printSize ( int * array ) { // array is treated as a pointer here std :: cout << sizeof ( array ) << '\\n' ; // prints the size of a pointer, not the size of the array! } int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << sizeof ( array ) << '\\n' ; // will print sizeof(int) * array length printSize ( array ); // the array argument decays into a pointer here return 0 ; } 1 2 32 4 \u6ce8\u610f\uff0c\u5373\u4f7f\u5f62\u53c2\u58f0\u660e\u4e3a\u56fa\u5b9a\u6570\u7ec4\uff0c\u4e5f\u4f1a\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include // C++ will implicitly convert parameter array[] to *array void printSize ( int array []) { // array is treated as a pointer here, not a fixed array std :: cout << sizeof ( array ) << '\\n' ; // prints the size of a pointer, not the size of the array! } int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << sizeof ( array ) << '\\n' ; // will print sizeof(int) * array length printSize ( array ); // the array argument decays into a pointer here return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 32 4 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cC++ \u9690\u5f0f\u5730\u5c06\u4f7f\u7528\u6570\u7ec4\u8bed\u6cd5( [] )\u7684\u53c2\u6570\u8f6c\u6362\u4e3a\u6307\u9488\u8bed\u6cd5( * )\u3002\u8fd9\u610f\u5473\u7740\u4e0b\u9762\u4e24\u4e2a\u51fd\u6570\u7684\u58f0\u660e\u662f\u76f8\u540c\u7684\uff1a 1 2 void printSize ( int array []); void printSize ( int * array ); \u6709\u4e9b\u7a0b\u5e8f\u5458\u66f4\u559c\u6b22\u4f7f\u7528 [] \u8bed\u6cd5\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u6e05\u695a\u5730\u8868\u660e\u51fd\u6570\u9700\u8981\u4e00\u4e2a\u6570\u7ec4\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u6307\u9488\u3002\u7136\u800c\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7531\u4e8e\u6307\u9488\u5e76\u4e0d\u77e5\u9053\u6570\u7ec4\u6709\u591a\u5927\uff0c\u6240\u4ee5\u65e0\u8bba\u5982\u4f55\u90fd\u9700\u8981\u5c06\u6570\u7ec4\u5927\u5c0f\u4f5c\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u53c2\u6570\u6765\u4f20\u9012(\u5b57\u7b26\u4e32\u662f\u4e00\u4e2a\u7279\u4f8b\uff0c\u56e0\u4e3a\u5b83\u4eec\u4ee5 null \u7ed3\u5c3e)\u3002 \u6211\u4eec\u5efa\u8bae\u4f7f\u7528\u6307\u9488\u8bed\u6cd5\uff0c\u56e0\u4e3a\u5b83\u8868\u660e\u5f62\u53c2\u88ab\u89c6\u4e3a\u6307\u9488\uff0c\u800c\u4e0d\u662f\u56fa\u5b9a\u7684\u6570\u7ec4\u3002\u6b64\u5916\uff0c\u67d0\u4e9b\u64cd\u4f5c\uff08\u5982 sizeof() \uff09\u4f1a\u5c06\u8be5\u5f62\u53c2\u4f5c\u4e3a\u6307\u9488\u8fdb\u884c\u64cd\u4f5c\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u5b9a\u4e49\u51fd\u6570\u7684\u53c2\u6570\u65f6\uff0c\u6700\u597d\u4f7f\u7528\u6307\u9488\u8bed\u6cd5( * )\u800c\u4e0d\u662f\u6570\u7ec4\u8bed\u6cd5( [] )\u3002 \u4f20\u5730\u5740 \u6570\u7ec4\u5728\u4f20\u9012\u7ed9\u51fd\u6570\u65f6 \u9000\u5316 \u4e3a\u6307\u9488\u7684\u4e8b\u5b9e\uff0c\u89e3\u91ca\u4e86\u4e3a\u4ec0\u4e48\u5728\u51fd\u6570\u4e2d\u66f4\u6539\u6570\u7ec4\u4f1a\u66f4\u6539\u4f20\u5165\u7684\u5b9e\u9645\u6570\u7ec4\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include // parameter ptr contains a copy of the array's address void changeArray ( int * ptr ) { * ptr = 5 ; // so changing an array element changes the _actual_ array } int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << \"Element 0 has value: \" << array [ 0 ] << '\\n' ; changeArray ( array ); std :: cout << \"Element 0 has value: \" << array [ 0 ] << '\\n' ; return 0 ; } 1 2 Element 0 has value: 1 Element 0 has value: 5 \u5f53 changeArray() \u88ab\u8c03\u7528\u65f6\uff0c\u6570\u7ec4\u9000\u5316\u4e3a\u4e00\u4e2a\u6307\u9488\uff0c\u8be5\u6307\u9488\u7684\u503c(\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740)\u88ab\u590d\u5236\u5230 changeArray() \u51fd\u6570\u7684 ptr \u5f62\u53c2\u4e2d\u3002\u867d\u7136 ptr \u4e2d\u7684\u503c\u662f\u6570\u7ec4\u5730\u5740\u7684\u526f\u672c\uff0c\u4f46 ptr \u4ecd\u7136\u6307\u5411\u5b9e\u9645\u7684\u6570\u7ec4(\u4e0d\u662f\u526f\u672c!)\u56e0\u6b64\uff0c\u5f53\u5bf9 ptr \u89e3\u5f15\u7528\u65f6\uff0c\u8bbf\u95ee\u7684\u5143\u7d20\u5b9e\u9645\u4e0a\u662f\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20! \u7cbe\u660e\u7684\u8bfb\u8005\u4f1a\u6ce8\u610f\u5230\u8fd9\u79cd\u73b0\u8c61\u4e5f\u9002\u7528\u4e8e\u6307\u5411\u975e\u6570\u7ec4\u503c\u7684\u6307\u9488\u3002 \u7ed3\u6784\u4f53\u548c\u7c7b\u4e2d\u7684\u6570\u7ec4\u4e0d\u4f1a\u9000\u5316 \u6700\u540e\uff0c\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u4f5c\u4e3a\u7ed3\u6784\u6216\u7c7b\u7684\u4e00\u90e8\u5206\u7684\u6570\u7ec4\u4e0d\u4f1a\u5728\u5c06\u6574\u4e2a\u7ed3\u6784\u6216\u7c7b\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\u53d1\u751f\u9000\u5316\u3002\u5982\u679c\u6709\u9700\u8981\uff0c\u53ef\u4ee5\u7528\u8fd9\u79cd\u65b9\u5f0f\u9632\u6b62\u6570\u7ec4\u9000\u5316\u4e3a\u6307\u9488\u3002\u5728\u7f16\u5199\u5229\u7528\u6570\u7ec4\u7684\u7c7b\u65f6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u662f\u6709\u7528\u7684 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u6307\u9488\u7b97\u672f\uff0c\u5e76\u8ba8\u8bba\u6570\u7ec4\u7d22\u5f15\u7684\u5de5\u4f5c\u539f\u7406\u3002","title":"11.8 - \u6307\u9488\u548c\u6570\u7ec4"},{"location":"11-8-Pointers-and-arrays/#_1","text":"\u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\uff1a 1 int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; // declare a fixed array of 5 integers \u5bf9\u4e8e\u6211\u4eec\u6765\u8bf4\uff0c\u8fd9\u662f\u4e00\u4e2a\u5305\u542b\u4e94\u4e2a\u6574\u6570\u7684\u6570\u7ec4\uff0c\u4f46\u662f\u5bf9\u4e8e\u7f16\u8bd1\u5668\u6765\u8bf4\uff0c\u5b83\u662f\u4e00\u4e2a int[5] \u7c7b\u578b\u7684\u6570\u7ec4\u3002\u6211\u4eec\u53ef\u4ee5\u77e5\u9053 array[0] , array[1] , array[2] , array[3] , \u548c array[4] \u7684\u503c (\u5206\u522b\u4e3a 9\u30017\u30015\u30013 \u548c 1 )\u3002 \u5728\u51e0\u4e4e\u6240\u6709\u60c5\u51b5\u4e0b\uff08\u4e24\u79cd\u4f8b\u5916\u60c5\u51b5\u7a0d\u540e\u4ecb\u7ecd\uff09\uff0c\u5f53\u6211\u4eec\u5728\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u6570\u7ec4\u65f6\uff0c\u6570\u7ec4\u4f1a \u9000\u5316 \uff08\u9690\u5f0f\u8f6c\u6362\uff09\u4e3a\u4e00\u4e2a\u6307\u9488\u3002\u53c2\u8003\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; // \u6253\u5370\u6570\u7ec4\u7684\u9996\u5730\u5740 std :: cout << \"Element 0 has address: \" << & array [ 0 ] << '\\n' ; // \u6253\u5370\u6570\u7ec4\u9000\u5316\u540e\u7684\u6307\u9488 std :: cout << \"The array decays to a pointer holding address: \" << array << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u5185\u5bb9\uff1a 1 2 Element 0 has address: 0042FD5C The array decays to a pointer holding address: 0042FD5C \u8ba4\u4e3a\u6570\u7ec4\u548c\u6307\u5411\u6570\u7ec4\u7684\u6307\u9488\u5b8c\u5168\u4e00\u6837\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9519\u8bef\u3002\u5b83\u4eec\u5e76\u4e0d\u662f\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6570\u7ec4\u7684\u7c7b\u578b\u662f\u201c int[5] \u201d\u5b83\u7684\u503c\u5c31\u662f\u6570\u7ec4\u7684\u5404\u4e2a\u5143\u7d20\u3002\u800c\u6307\u5411\u6570\u7ec4\u7684\u6307\u9488\uff0c\u5176\u7c7b\u578b\u4e3a\u201c int* \u201d\uff0c\u5176\u503c\u4e3a\u6570\u7ec4\u9996\u4e2a\u5143\u7d20\u7684\u5730\u5740\u3002 \u7a0d\u540e\u6211\u4eec\u5c31\u4f1a\u4ecb\u7ecd\u5b83\u4eec\u7684\u5177\u4f53\u533a\u522b\u4f53\u73b0\u5728\u54ea\u91cc\u3002 \u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4e5f\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u6765\u8bbf\u95ee\uff08\u4e0b\u8282\u8bfe\u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\uff09\uff0c\u4f46\u662f\u6211\u4eec\u4e0d\u80fd\u901a\u8fc7\u6307\u9488\u6765\u83b7\u53d6\u6570\u7ec4\u7684\u7c7b\u578b\u4fe1\u606f\u3002 \u7136\u800c\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u8fd8\u662f\u53ef\u4ee5\u4ee5\u76f8\u540c\u7684\u65b9\u5f0f\u5bf9\u5f85\u56fa\u5b9a\u6570\u7ec4\u548c\u6307\u9488\u3002 \u4f8b\u5982\uff0c\u53ef\u4ee5\u5bf9\u6570\u7ec4\u89e3\u5f15\u7528\u4ee5\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u503c: 1 2 3 4 5 6 7 int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; // Deferencing an array returns the first element (element 0) std :: cout << * array ; // will print 9! char name []{ \"Jason\" }; // C-style string (also an array) std :: cout << * name << '\\n' ; // will print 'J' \u6ce8\u610f\uff0c\u6211\u4eec\u6ca1\u6709\u5bf9\u6570\u7ec4\u672c\u8eab\u8fdb\u884c\u89e3\u5f15\u7528\u3002\u6570\u7ec4\u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a\u4e86\u6307\u9488( int* \u7c7b\u578b)\uff0c\u5e76\u5bf9\u6307\u9488\u89e3\u5f15\u7528\uff0c\u4ee5\u83b7\u5f97\u6307\u9488\u6240\u4fdd\u5b58\u7684\u5185\u5b58\u5730\u5740\u7684\u503c(\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u503c)\u3002 \u6211\u4eec\u8fd8\u53ef\u4ee5\u8ba9\u4e00\u4e2a\u6307\u9488\u6307\u5411\u6570\u7ec4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; std :: cout << * array << '\\n' ; // will print 9 int * ptr { array }; std :: cout << * ptr << '\\n' ; // will print 9 return 0 ; } \u6570\u7ec4\u9000\u5316\u4e3a\u4e86 int* \u7c7b\u578b\u7684\u6307\u9488\uff0c\u800c ptr \u4e5f\u662f\u6b64\u7c7b\u578b\u7684\u6307\u9488\u3002","title":"\u6570\u7ec4\u9000\u5316"},{"location":"11-8-Pointers-and-arrays/#_2","text":"\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u56fa\u5b9a\u6570\u7ec4\u548c\u6307\u9488\u4e4b\u95f4\u7684\u7c7b\u578b\u5dee\u5f02\u4f1a\u5e26\u6765\u95ee\u9898\u3002\u8fd9\u4e9b\u95ee\u9898\u6709\u52a9\u4e8e\u6211\u4eec\u89e3\u91ca\u56fa\u5b9a\u6570\u7ec4\u548c\u6307\u9488\u7684\u4e0d\u540c\u3002 \u4e3b\u8981\u7684\u5dee\u5f02\u53d1\u751f\u5728\u4f7f\u7528 sizeof() \u64cd\u4f5c\u7b26\u65f6\u3002\u5f53\u7528\u4e8e\u56fa\u5b9a\u6570\u7ec4\u65f6\uff0c sizeof \u8fd4\u56de\u6574\u4e2a\u6570\u7ec4\u7684\u5927\u5c0f( array length * element size )\u3002\u5f53\u7528\u4e8e\u6307\u9488\u65f6\uff0c sizeof \u8fd4\u56de\u6307\u9488\u7684\u5927\u5c0f(\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d)\u3002\u4e0b\u9762\u7684\u7a0b\u5e8f\u8bf4\u660e\u4e86\u8fd9\u4e00\u70b9: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; std :: cout << sizeof ( array ) << '\\n' ; // will print sizeof(int) * array length int * ptr { array }; std :: cout << sizeof ( ptr ) << '\\n' ; // will print the size of a pointer return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 2 20 4 \u56fa\u5b9a\u6570\u7ec4\u77e5\u9053\u5b83\u6240\u6307\u5411\u7684\u6570\u7ec4\u6709\u591a\u957f\uff0c\u800c\u6307\u5411\u6570\u7ec4\u7684\u6307\u9488\u5e76\u4e0d\u77e5\u9053\u6570\u7ec4\u6709\u591a\u957f\u3002 \u7b2c\u4e8c\u4e2a\u533a\u522b\u53d1\u751f\u5728\u4f7f\u7528\u53d6\u5730\u5740\u64cd\u4f5c\u7b26( & )\u65f6\u3002\u5bf9\u6307\u9488\u4f7f\u7528\u53d6\u5730\u5740\u64cd\u4f5c\u7b26\uff0c\u4f1a\u5f97\u5230\u6307\u9488\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u3002\u5bf9\u6570\u7ec4\u4f7f\u7528\u53d6\u5730\u5740\u64cd\u4f5c\u7b26\uff0c\u5c06\u8fd4\u56de\u6307\u5411\u5730\u5740\u7684\u6307\u9488\uff0c\u8fd9\u4e2a\u6307\u9488\u6307\u5411\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u4f46\u662f\u7c7b\u578b\u4fe1\u606f\u4e0d\u540c(\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c &array \u7684\u7c7b\u578b\u662f int(*)[5] )\u3002\u4f60\u4e0d\u592a\u53ef\u80fd\u4f1a\u7528\u5230\u5b83\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { int array [ 5 ]{ 9 , 7 , 5 , 3 , 1 }; std :: cout << array << '\\n' ; // \u7c7b\u578b\u4e3a int[5], \u6253\u5370 009DF9D4 std :: cout << & array << '\\n' ; // \u7c7b\u578b\u4e3a int(*)[5], \u6253\u5370 009DF9D4 std :: cout << '\\n' ; int * ptr { array }; std :: cout << ptr << '\\n' ; // \u7c7b\u578b\u4e3a int*, \u6253\u5370 009DF9D4 std :: cout << & ptr << '\\n' ; // \u7c7b\u578b\u4e3a int**, \u6253\u5370 009DF9C8 return 0 ; } // h/t to reader PacMan for this example","title":"\u6307\u9488\u548c\u56fa\u5b9a\u6570\u7ec4\u7684\u5dee\u5f02"},{"location":"11-8-Pointers-and-arrays/#_3","text":"\u5728 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u62f7\u8d1d\u5927\u578b\u6570\u7ec4\u7684\u5f00\u9500\u662f\u5f88\u5927\u7684\uff0cC++\u5728\u5c06\u6570\u7ec4\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\uff0c\u5e76\u4e0d\u4f1a\u5c06\u5176\u62f7\u8d1d\u4e00\u4efd\u3002\u5b9e\u9645\u4e0a\uff0c\u56fa\u5b9a\u6570\u7ec4\u5728\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\u4e5f\u4f1a\u9000\u5316\u4e3a\u6307\u9488\uff0c\u8be5\u6307\u9488\u4f1a\u88ab\u4f20\u9012\u7ed9\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void printSize ( int * array ) { // array is treated as a pointer here std :: cout << sizeof ( array ) << '\\n' ; // prints the size of a pointer, not the size of the array! } int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << sizeof ( array ) << '\\n' ; // will print sizeof(int) * array length printSize ( array ); // the array argument decays into a pointer here return 0 ; } 1 2 32 4 \u6ce8\u610f\uff0c\u5373\u4f7f\u5f62\u53c2\u58f0\u660e\u4e3a\u56fa\u5b9a\u6570\u7ec4\uff0c\u4e5f\u4f1a\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include // C++ will implicitly convert parameter array[] to *array void printSize ( int array []) { // array is treated as a pointer here, not a fixed array std :: cout << sizeof ( array ) << '\\n' ; // prints the size of a pointer, not the size of the array! } int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << sizeof ( array ) << '\\n' ; // will print sizeof(int) * array length printSize ( array ); // the array argument decays into a pointer here return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 32 4 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cC++ \u9690\u5f0f\u5730\u5c06\u4f7f\u7528\u6570\u7ec4\u8bed\u6cd5( [] )\u7684\u53c2\u6570\u8f6c\u6362\u4e3a\u6307\u9488\u8bed\u6cd5( * )\u3002\u8fd9\u610f\u5473\u7740\u4e0b\u9762\u4e24\u4e2a\u51fd\u6570\u7684\u58f0\u660e\u662f\u76f8\u540c\u7684\uff1a 1 2 void printSize ( int array []); void printSize ( int * array ); \u6709\u4e9b\u7a0b\u5e8f\u5458\u66f4\u559c\u6b22\u4f7f\u7528 [] \u8bed\u6cd5\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u6e05\u695a\u5730\u8868\u660e\u51fd\u6570\u9700\u8981\u4e00\u4e2a\u6570\u7ec4\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u6307\u9488\u3002\u7136\u800c\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7531\u4e8e\u6307\u9488\u5e76\u4e0d\u77e5\u9053\u6570\u7ec4\u6709\u591a\u5927\uff0c\u6240\u4ee5\u65e0\u8bba\u5982\u4f55\u90fd\u9700\u8981\u5c06\u6570\u7ec4\u5927\u5c0f\u4f5c\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u53c2\u6570\u6765\u4f20\u9012(\u5b57\u7b26\u4e32\u662f\u4e00\u4e2a\u7279\u4f8b\uff0c\u56e0\u4e3a\u5b83\u4eec\u4ee5 null \u7ed3\u5c3e)\u3002 \u6211\u4eec\u5efa\u8bae\u4f7f\u7528\u6307\u9488\u8bed\u6cd5\uff0c\u56e0\u4e3a\u5b83\u8868\u660e\u5f62\u53c2\u88ab\u89c6\u4e3a\u6307\u9488\uff0c\u800c\u4e0d\u662f\u56fa\u5b9a\u7684\u6570\u7ec4\u3002\u6b64\u5916\uff0c\u67d0\u4e9b\u64cd\u4f5c\uff08\u5982 sizeof() \uff09\u4f1a\u5c06\u8be5\u5f62\u53c2\u4f5c\u4e3a\u6307\u9488\u8fdb\u884c\u64cd\u4f5c\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u5b9a\u4e49\u51fd\u6570\u7684\u53c2\u6570\u65f6\uff0c\u6700\u597d\u4f7f\u7528\u6307\u9488\u8bed\u6cd5( * )\u800c\u4e0d\u662f\u6570\u7ec4\u8bed\u6cd5( [] )\u3002","title":"\u5c06\u56fa\u5b9a\u6570\u7ec4\u4f20\u9012\u7ed9\u51fd\u6570"},{"location":"11-8-Pointers-and-arrays/#_4","text":"\u6570\u7ec4\u5728\u4f20\u9012\u7ed9\u51fd\u6570\u65f6 \u9000\u5316 \u4e3a\u6307\u9488\u7684\u4e8b\u5b9e\uff0c\u89e3\u91ca\u4e86\u4e3a\u4ec0\u4e48\u5728\u51fd\u6570\u4e2d\u66f4\u6539\u6570\u7ec4\u4f1a\u66f4\u6539\u4f20\u5165\u7684\u5b9e\u9645\u6570\u7ec4\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include // parameter ptr contains a copy of the array's address void changeArray ( int * ptr ) { * ptr = 5 ; // so changing an array element changes the _actual_ array } int main () { int array []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 }; std :: cout << \"Element 0 has value: \" << array [ 0 ] << '\\n' ; changeArray ( array ); std :: cout << \"Element 0 has value: \" << array [ 0 ] << '\\n' ; return 0 ; } 1 2 Element 0 has value: 1 Element 0 has value: 5 \u5f53 changeArray() \u88ab\u8c03\u7528\u65f6\uff0c\u6570\u7ec4\u9000\u5316\u4e3a\u4e00\u4e2a\u6307\u9488\uff0c\u8be5\u6307\u9488\u7684\u503c(\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740)\u88ab\u590d\u5236\u5230 changeArray() \u51fd\u6570\u7684 ptr \u5f62\u53c2\u4e2d\u3002\u867d\u7136 ptr \u4e2d\u7684\u503c\u662f\u6570\u7ec4\u5730\u5740\u7684\u526f\u672c\uff0c\u4f46 ptr \u4ecd\u7136\u6307\u5411\u5b9e\u9645\u7684\u6570\u7ec4(\u4e0d\u662f\u526f\u672c!)\u56e0\u6b64\uff0c\u5f53\u5bf9 ptr \u89e3\u5f15\u7528\u65f6\uff0c\u8bbf\u95ee\u7684\u5143\u7d20\u5b9e\u9645\u4e0a\u662f\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20! \u7cbe\u660e\u7684\u8bfb\u8005\u4f1a\u6ce8\u610f\u5230\u8fd9\u79cd\u73b0\u8c61\u4e5f\u9002\u7528\u4e8e\u6307\u5411\u975e\u6570\u7ec4\u503c\u7684\u6307\u9488\u3002","title":"\u4f20\u5730\u5740"},{"location":"11-8-Pointers-and-arrays/#_5","text":"\u6700\u540e\uff0c\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u4f5c\u4e3a\u7ed3\u6784\u6216\u7c7b\u7684\u4e00\u90e8\u5206\u7684\u6570\u7ec4\u4e0d\u4f1a\u5728\u5c06\u6574\u4e2a\u7ed3\u6784\u6216\u7c7b\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\u53d1\u751f\u9000\u5316\u3002\u5982\u679c\u6709\u9700\u8981\uff0c\u53ef\u4ee5\u7528\u8fd9\u79cd\u65b9\u5f0f\u9632\u6b62\u6570\u7ec4\u9000\u5316\u4e3a\u6307\u9488\u3002\u5728\u7f16\u5199\u5229\u7528\u6570\u7ec4\u7684\u7c7b\u65f6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u662f\u6709\u7528\u7684 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u6307\u9488\u7b97\u672f\uff0c\u5e76\u8ba8\u8bba\u6570\u7ec4\u7d22\u5f15\u7684\u5de5\u4f5c\u539f\u7406\u3002","title":"\u7ed3\u6784\u4f53\u548c\u7c7b\u4e2d\u7684\u6570\u7ec4\u4e0d\u4f1a\u9000\u5316"},{"location":"11-9-Pointer-arithmetic-and-array-indexing/","tags":["Pointer arithmetic"],"text":"Key Takeaway \u6307\u9488+1\uff0c\u79fb\u52a8\u7684\u5185\u5b58\u5730\u5740\u6570\u53d6\u51b3\u4e8e\u6307\u9488\u6307\u5411\u7684\u5bf9\u8c61\u7684\u5927\u5c0f\uff0c\u6bcf\u6b21\u79fb\u52a8\u4e00\u4e2a\u5bf9\u8c61\u7684\u957f\u5ea6\u3002 C++\u7684 algorithms \u5e93\u63d0\u4f9b\u4e86 std::count_if \uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u57fa\u4e8e\u67d0\u4e2a\u6761\u4ef6\u6765\u7edf\u8ba1\u5143\u7d20 std::begin \u548c std::end \u53ef\u4ee5\u8fd4\u56de\u6307\u5411\u56fa\u5b9a\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u8fed\u4ee3\u5668\u548c\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u540e\u9762\u7684\u4f2a\u5143\u7d20\u7684\u8fed\u4ee3\u5668 std::end \u8fd4\u56de\u7684\u8fed\u4ee3\u5668\u53ea\u4f5c\u4e3a\u6807\u8bb0\u6765\u4f7f\u7528\uff0c\u5bf9\u5176\u8fdb\u884c\u8bbf\u95ee\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \uff0c\u56e0\u4e3a\u5b83\u4e0d\u6307\u5411\u67d0\u4e2a\u5b9e\u9645\u7684\u5143\u7d20\u3002 \u6307\u9488\u8fd0\u7b97 \u5728 C++ \u4e2d\u4f60\u53ef\u4ee5\u5bf9\u6307\u9488\u8fdb\u884c\u52a0\u51cf\u8fd0\u7b97\u3002\u5982\u679c ptr \u6307\u5411\u4e00\u4e2a\u6574\u578b\uff0c\u90a3\u4e48 ptr + 1 \u5219\u4f1a\u6307\u5411\u4e0b\u5185\u5b58\u4e2d\u4e0b\u4e00\u4e2a\u6574\u578b\u5bf9\u8c61\u3002 ptr - 1 \u5219\u4f1a\u6307\u5411\u524d\u4e00\u4e2a\u6574\u5f62\u5bf9\u8c61\u3002 \u6ce8\u610f\uff0c ptr + 1 \u8fd4\u56de\u7684\u5e76\u4e0d\u662f ptr \u6307\u9488\u6307\u5411\u7684\u4e0b\u4e00\u4e2a\u5185\u5b58\u5730\u5740\uff0c\u800c\u662f\u6307\u9488\u6240\u6307\u5bf9\u8c61\u7684\u4e0b\u4e00\u4e2a\u540c\u7c7b\u578b\u5bf9\u8c61\u7684\u5730\u5740\u3002\u5982\u679c\u6307\u9488\u6307\u5411\u6574\u578b\uff08\u5047\u8bbe\u4e3a4\u5b57\u8282\uff09\uff0c\u90a3\u4e48 ptr+3 \u5219\u8fd4\u56de3\u4e2a\u6574\u578b\uff0812\u5b57\u8282\uff09\u540e\u7684\u5730\u5740\u3002\u5982\u679c ptr \u6307\u5411\u7684\u662f char \u7c7b\u578b\u7684\u5bf9\u8c61\uff08\u603b\u662f1\u5b57\u8282\uff09\uff0c\u5219 ptr + 3 \u4f1a\u8fd4\u56de ptr \u540e\u97623\u4e2a\u5b57\u8282\u7684\u5730\u5740\u3002 \u5728\u8ba1\u7b97\u6307\u9488\u7b97\u672f\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u65f6\uff0c\u7f16\u8bd1\u5668\u603b\u662f\u5c06\u6574\u6570\u64cd\u4f5c\u6570\u4e58\u4ee5\u6240\u6307\u5411\u5bf9\u8c61\u7684\u5927\u5c0f\u3002\u8fd9\u53eb\u505a \u6307\u9488\u7f29\u653e(scaling) \u3002 \u8003\u8651\u4ee5\u4e0b\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int value { 7 }; int * ptr { & value }; std :: cout << ptr << '\\n' ; std :: cout << ptr + 1 << '\\n' ; std :: cout << ptr + 2 << '\\n' ; std :: cout << ptr + 3 << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 0012FF7C 0012FF80 0012FF84 0012FF88 \u4e0a\u8ff0\u5730\u5740\u4e4b\u95f4\u76f8\u5dee4\u5b57\u8282(\u5341\u516d\u8fdb\u5236 7C + 4 = 80)\uff0c\u8fd9\u662f\u56e0\u4e3a\u5728\u7b14\u8005\u7535\u8111\u4e0a\u6574\u578b\u5927\u5c0f\u4e3a4\u5b57\u8282\u3002 \u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\u4f7f\u7528 short \u4ee3\u66ff\u4e86 int \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { short value { 7 }; short * ptr { & value }; std :: cout << ptr << '\\n' ; std :: cout << ptr + 1 << '\\n' ; std :: cout << ptr + 2 << '\\n' ; std :: cout << ptr + 3 << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 0012FF7C 0012FF7E 0012FF80 0012FF82 \u56e0\u4e3a short \u7684\u957f\u5ea6\u4e3a 2 \u5b57\u8282\uff0c\u6240\u4ee5\u5730\u5740\u4e4b\u95f4\u76f8\u5dee 2 \u5b57\u8282\u3002 \u6570\u7ec4\u5143\u7d20\u6309\u987a\u5e8f\u5206\u5e03\u5728\u5185\u5b58\u4e2d \u901a\u8fc7\u53d6\u5730\u5740\u8fd0\u7b97\u7b26( & )\uff0c\u6211\u4eec\u53ef\u4ee5\u68c0\u9a8c\u6570\u7ec4\u5143\u7d20\u662f\u5426\u662f\u6309\u7167\u987a\u5e8f\u6392\u5217\u5728\u5185\u5b58\u4e2d\u7684\uff0c0, 1, 2, \u2026 \u5143\u7d20\u5f7c\u6b64\u76f8\u90bb\uff0c\u6309\u7167\u987a\u5e8f\u6392\u5217\u5728\u5185\u5b58\u4e2d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { int array []{ 9 , 7 , 5 , 3 , 1 }; std :: cout << \"Element 0 is at address: \" << & array [ 0 ] << '\\n' ; std :: cout << \"Element 1 is at address: \" << & array [ 1 ] << '\\n' ; std :: cout << \"Element 2 is at address: \" << & array [ 2 ] << '\\n' ; std :: cout << \"Element 3 is at address: \" << & array [ 3 ] << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 Element 0 is at address: 0041FE9C Element 1 is at address: 0041FEA0 Element 2 is at address: 0041FEA4 Element 3 is at address: 0041FEA8 \u6ce8\u610f\uff0c\u6bcf\u4e2a\u5730\u5740\u4e4b\u95f4\u90fd\u95f4\u96944\u5b57\u8282\uff0c\u8fd9\u6b63\u662f\u7b14\u8005\u7535\u8111\u4e0a\u6574\u578b\u6570\u7684\u5927\u5c0f\u3002 \u6307\u9488\u7b97\u6570\u3001\u6570\u7ec4\u548c\u7d22\u5f15\u80cc\u540e\u7684\u9b54\u6cd5 \u4e0a\u9762\u5df2\u7ecf\u4ecb\u7ecd\u4e86\uff0c\u6570\u7ec4\u662f\u4e00\u7cfb\u5217\u987a\u5e8f\u6392\u5217\u7684\u5185\u5b58\u3002 \u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86\uff0c\u56fa\u5b9a\u6570\u7ec4\u53ef\u4ee5\u9000\u5316\u4e3a\u6307\u9488\uff0c\u8be5\u6307\u9488\u6307\u5411\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002 \u540c\u65f6\u6211\u4eec\u8fd8\u77e5\u9053\uff0c\u5bf9\u6307\u9488\u52a01\u53ef\u4ee5\u5f97\u5230\u8be5\u6307\u9488\u6240\u6307\u5730\u5740\u540e\u9762\u4e00\u4e2a\u5bf9\u8c61\u7684\u5730\u5740\u3002 \u4ee5\u4f60\uff0c\u5bf9\u6570\u7ec4\u52a01\u5c31\u53ef\u4ee5\u5f97\u5230\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff0c\u901a\u8fc7\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u8fdb\u884c\u9a8c\u8bc1\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int array []{ 9 , 7 , 5 , 3 , 1 }; std :: cout << & array [ 1 ] << '\\n' ; // print memory address of array element 1 std :: cout << array + 1 << '\\n' ; // print memory address of array pointer + 1 std :: cout << array [ 1 ] << '\\n' ; // prints 7 std :: cout << * ( array + 1 ) << '\\n' ; // prints 7 (note the parenthesis required here) return 0 ; } \u6ce8\u610f\uff0c\u5f53\u901a\u8fc7\u6307\u9488\u8fd0\u7b97\u7684\u7ed3\u679c\u8fdb\u884c\u95f4\u63a5\u64cd\u4f5c\u65f6\uff0c\u5fc5\u987b\u4f7f\u7528\u5706\u62ec\u53f7\u6765\u786e\u4fdd\u64cd\u4f5c\u7b26\u7684\u4f18\u5148\u7ea7\u662f\u6b63\u786e\u7684\uff0c\u56e0\u4e3a operator * \u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e operator + \u3002 \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 0017FB80 0017FB80 7 7 \u4e8b\u5b9e\u8bc1\u660e\uff0c\u5f53\u7f16\u8bd1\u5668\u770b\u5230\u4e0b\u6807\u64cd\u4f5c\u7b26( [] )\u65f6\uff0c\u5b83\u5b9e\u9645\u4e0a\u4f1a\u5c06\u5176\u8f6c\u6362\u4e3a\u6307\u9488\u52a0\u6cd5\u548c\u95f4\u63a5\u64cd\u4f5c\uff01 array[n] \u7b49\u540c\u4e8e *(array + n) \uff0c\u5176\u4e2d n \u4e3a\u6574\u6570\u3002\u4e0b\u6807\u64cd\u4f5c\u7b26 [] \u7684\u5b58\u5728\u65e2\u662f\u4e3a\u4e86\u7f8e\u89c2\uff0c\u4e5f\u662f\u4e3a\u4e86\u4fbf\u4e8e\u4f7f\u7528\u3002 \u4f7f\u7528\u6307\u9488\u904d\u5386\u6570\u7ec4 \u53ef\u4ee5\u4f7f\u7528\u6307\u9488\u548c\u6307\u9488\u7b97\u672f\u6765\u904d\u5386\u6570\u7ec4\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u5e38\u89c1(\u4f7f\u7528\u4e0b\u6807\u901a\u5e38\u66f4\u5bb9\u6613\u9605\u8bfb\u548c\u66f4\u5c11\u7684\u9519\u8bef)\uff0c\u4f46\u7684\u786e\u662f\u53ef\u884c\u7684\u3002\u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include #include // for std::size bool isVowel ( char ch ) { switch ( ch ) { case 'A' : case 'a' : case 'E' : case 'e' : case 'I' : case 'i' : case 'O' : case 'o' : case 'U' : case 'u' : return true ; default : return false ; } } int main () { char name []{ \"Mollie\" }; int arrayLength { static_cast < int > ( std :: size ( name )) }; int numVowels { 0 }; for ( char * ptr { name }; ptr != ( name + arrayLength ); ++ ptr ) { if ( isVowel ( * ptr )) { ++ numVowels ; } } std :: cout << name << \" has \" << numVowels << \" vowels. \\n \" ; return 0 ; } \u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f\u8fd9\u4e2a\u7a0b\u5e8f\u4f7f\u7528\u4e00\u4e2a\u6307\u9488\u6765\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u8bb0\u4f4f\u6570\u7ec4\u4f1a\u9000\u5316\u4e3a\u6307\u5411\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u6307\u9488\u3002\u56e0\u6b64\uff0c\u901a\u8fc7\u4f7f\u7528 name \u521d\u59cb\u5316 ptr \uff0c ptr \u5c06\u6307\u5411\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u5f53\u8c03\u7528 isVowel(*ptr) \u65f6\uff0c\u901a\u8fc7 ptr \u5bf9\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u95f4\u63a5\u64cd\u4f5c\uff0c\u5982\u679c\u5143\u7d20\u662f\u5143\u97f3\uff0c numVowels \u5c06\u9012\u589e\u3002\u7136\u540e\uff0cfor\u5faa\u73af\u4f7f\u7528 ++ \u64cd\u4f5c\u7b26\u5c06\u6307\u9488\u5411\u524d\u79fb\u52a8\u5230\u6570\u7ec4\u4e2d\u7684\u4e0b\u4e00\u4e2a\u5b57\u7b26\u3002\u5f53\u68c0\u67e5\u5b8c\u6240\u6709\u5b57\u7b26\u65f6\uff0cfor\u5faa\u73af\u7ec8\u6b62\u3002 \u4e0a\u8ff0\u7a0b\u5e8f\u4ea7\u751f\u7684\u7ed3\u679c\u662f\uff1a 1 Mollie has 3 vowels \u56e0\u4e3a\u8ba1\u6570\u662f\u975e\u5e38\u5e38\u89c1\u7684\u7b97\u6cd5\uff0c\u6240\u4ee5C++\u7684 algorithms \u5e93\u63d0\u4f9b\u4e86 std::count_if \uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u57fa\u4e8e\u67d0\u4e2a\u6761\u4ef6\u6765\u7edf\u8ba1\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u4e0a\u9762\u7684for\u5faa\u73af\u4f7f\u7528 std::count_if \u8fdb\u884c\u6539\u5199\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include #include #include // for std::begin and std::end bool isVowel ( char ch ) { switch ( ch ) { case 'A' : case 'a' : case 'E' : case 'e' : case 'I' : case 'i' : case 'O' : case 'o' : case 'U' : case 'u' : return true ; default : return false ; } } int main () { char name []{ \"Mollie\" }; // walk through all the elements of name and count how many calls to isVowel return true auto numVowels { std :: count_if ( std :: begin ( name ), std :: end ( name ), isVowel ) }; std :: cout << name << \" has \" << numVowels << \" vowels. \\n \" ; return 0 ; } std::begin \u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u8fed\u4ee3\u5668\uff08\u6307\u9488\uff09\uff0c\u800c std::end \u8fd4\u56de\u7684\u662f\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u540e\u4e00\u4e2a\u4f4d\u7f6e\u3002 std::end \u8fd4\u56de\u7684\u8fed\u4ee3\u5668\u53ea\u4f5c\u4e3a\u6807\u8bb0\u6765\u4f7f\u7528\uff0c\u5bf9\u5176\u8fdb\u884c\u8bbf\u95ee\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u56e0\u4e3a\u5b83\u4e0d\u6307\u5411\u67d0\u4e2a\u5b9e\u9645\u7684\u5143\u7d20\u3002 std::begin \u548c std::end \u53ea\u80fd\u7528\u4e8e\u5df2\u77e5\u957f\u5ea6\u7684\u6570\u7ec4\uff0c\u5982\u679c\u6570\u7ec4\u9000\u5316\u4e3a\u6307\u9488\uff0c\u5219\u9700\u8981\u624b\u52a8\u8ba1\u7b97begin\u548cend\u3002 1 2 3 4 5 // nameLength is the number of elements in the array. std :: count_if ( name , name + nameLength , isVowel ) // Don't do this. Accessing invalid indexes causes undefined behavior. // std::count_if(name, &name[nameLength], isVowel) \u6ce8\u610f\uff0c\u6211\u4eec\u8ba1\u7b97\u7684\u662f name + nameLength \uff0c\u800c\u4e0d\u662f name + nameLength - 1 \uff0c\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u7684\u4e0d\u662f\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u800c\u662f\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u540e\u9762\u7684\u4e00\u4e2a\u201d\u4f2a\u5143\u7d20\u201c\u3002 \u50cf\u8fd9\u6837\u8ba1\u7b97\u6570\u7ec4\u7684\u5f00\u59cb\u548c\u7ed3\u675f\uff0c\u9002\u7528\u4e8e\u6240\u6709\u9700\u8981\u5f00\u59cb\u548c\u7ed3\u675f\u53c2\u6570\u7684\u7b97\u6cd5\u3002","title":"11.9 - \u6307\u9488\u8fd0\u7b97\u548c\u6570\u7ec4\u7d22\u5f15"},{"location":"11-9-Pointer-arithmetic-and-array-indexing/#_1","text":"\u5728 C++ \u4e2d\u4f60\u53ef\u4ee5\u5bf9\u6307\u9488\u8fdb\u884c\u52a0\u51cf\u8fd0\u7b97\u3002\u5982\u679c ptr \u6307\u5411\u4e00\u4e2a\u6574\u578b\uff0c\u90a3\u4e48 ptr + 1 \u5219\u4f1a\u6307\u5411\u4e0b\u5185\u5b58\u4e2d\u4e0b\u4e00\u4e2a\u6574\u578b\u5bf9\u8c61\u3002 ptr - 1 \u5219\u4f1a\u6307\u5411\u524d\u4e00\u4e2a\u6574\u5f62\u5bf9\u8c61\u3002 \u6ce8\u610f\uff0c ptr + 1 \u8fd4\u56de\u7684\u5e76\u4e0d\u662f ptr \u6307\u9488\u6307\u5411\u7684\u4e0b\u4e00\u4e2a\u5185\u5b58\u5730\u5740\uff0c\u800c\u662f\u6307\u9488\u6240\u6307\u5bf9\u8c61\u7684\u4e0b\u4e00\u4e2a\u540c\u7c7b\u578b\u5bf9\u8c61\u7684\u5730\u5740\u3002\u5982\u679c\u6307\u9488\u6307\u5411\u6574\u578b\uff08\u5047\u8bbe\u4e3a4\u5b57\u8282\uff09\uff0c\u90a3\u4e48 ptr+3 \u5219\u8fd4\u56de3\u4e2a\u6574\u578b\uff0812\u5b57\u8282\uff09\u540e\u7684\u5730\u5740\u3002\u5982\u679c ptr \u6307\u5411\u7684\u662f char \u7c7b\u578b\u7684\u5bf9\u8c61\uff08\u603b\u662f1\u5b57\u8282\uff09\uff0c\u5219 ptr + 3 \u4f1a\u8fd4\u56de ptr \u540e\u97623\u4e2a\u5b57\u8282\u7684\u5730\u5740\u3002 \u5728\u8ba1\u7b97\u6307\u9488\u7b97\u672f\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u65f6\uff0c\u7f16\u8bd1\u5668\u603b\u662f\u5c06\u6574\u6570\u64cd\u4f5c\u6570\u4e58\u4ee5\u6240\u6307\u5411\u5bf9\u8c61\u7684\u5927\u5c0f\u3002\u8fd9\u53eb\u505a \u6307\u9488\u7f29\u653e(scaling) \u3002 \u8003\u8651\u4ee5\u4e0b\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int value { 7 }; int * ptr { & value }; std :: cout << ptr << '\\n' ; std :: cout << ptr + 1 << '\\n' ; std :: cout << ptr + 2 << '\\n' ; std :: cout << ptr + 3 << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 0012FF7C 0012FF80 0012FF84 0012FF88 \u4e0a\u8ff0\u5730\u5740\u4e4b\u95f4\u76f8\u5dee4\u5b57\u8282(\u5341\u516d\u8fdb\u5236 7C + 4 = 80)\uff0c\u8fd9\u662f\u56e0\u4e3a\u5728\u7b14\u8005\u7535\u8111\u4e0a\u6574\u578b\u5927\u5c0f\u4e3a4\u5b57\u8282\u3002 \u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\u4f7f\u7528 short \u4ee3\u66ff\u4e86 int \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { short value { 7 }; short * ptr { & value }; std :: cout << ptr << '\\n' ; std :: cout << ptr + 1 << '\\n' ; std :: cout << ptr + 2 << '\\n' ; std :: cout << ptr + 3 << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 0012FF7C 0012FF7E 0012FF80 0012FF82 \u56e0\u4e3a short \u7684\u957f\u5ea6\u4e3a 2 \u5b57\u8282\uff0c\u6240\u4ee5\u5730\u5740\u4e4b\u95f4\u76f8\u5dee 2 \u5b57\u8282\u3002","title":"\u6307\u9488\u8fd0\u7b97"},{"location":"11-9-Pointer-arithmetic-and-array-indexing/#_2","text":"\u901a\u8fc7\u53d6\u5730\u5740\u8fd0\u7b97\u7b26( & )\uff0c\u6211\u4eec\u53ef\u4ee5\u68c0\u9a8c\u6570\u7ec4\u5143\u7d20\u662f\u5426\u662f\u6309\u7167\u987a\u5e8f\u6392\u5217\u5728\u5185\u5b58\u4e2d\u7684\uff0c0, 1, 2, \u2026 \u5143\u7d20\u5f7c\u6b64\u76f8\u90bb\uff0c\u6309\u7167\u987a\u5e8f\u6392\u5217\u5728\u5185\u5b58\u4e2d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { int array []{ 9 , 7 , 5 , 3 , 1 }; std :: cout << \"Element 0 is at address: \" << & array [ 0 ] << '\\n' ; std :: cout << \"Element 1 is at address: \" << & array [ 1 ] << '\\n' ; std :: cout << \"Element 2 is at address: \" << & array [ 2 ] << '\\n' ; std :: cout << \"Element 3 is at address: \" << & array [ 3 ] << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 Element 0 is at address: 0041FE9C Element 1 is at address: 0041FEA0 Element 2 is at address: 0041FEA4 Element 3 is at address: 0041FEA8 \u6ce8\u610f\uff0c\u6bcf\u4e2a\u5730\u5740\u4e4b\u95f4\u90fd\u95f4\u96944\u5b57\u8282\uff0c\u8fd9\u6b63\u662f\u7b14\u8005\u7535\u8111\u4e0a\u6574\u578b\u6570\u7684\u5927\u5c0f\u3002","title":"\u6570\u7ec4\u5143\u7d20\u6309\u987a\u5e8f\u5206\u5e03\u5728\u5185\u5b58\u4e2d"},{"location":"11-9-Pointer-arithmetic-and-array-indexing/#_3","text":"\u4e0a\u9762\u5df2\u7ecf\u4ecb\u7ecd\u4e86\uff0c\u6570\u7ec4\u662f\u4e00\u7cfb\u5217\u987a\u5e8f\u6392\u5217\u7684\u5185\u5b58\u3002 \u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86\uff0c\u56fa\u5b9a\u6570\u7ec4\u53ef\u4ee5\u9000\u5316\u4e3a\u6307\u9488\uff0c\u8be5\u6307\u9488\u6307\u5411\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002 \u540c\u65f6\u6211\u4eec\u8fd8\u77e5\u9053\uff0c\u5bf9\u6307\u9488\u52a01\u53ef\u4ee5\u5f97\u5230\u8be5\u6307\u9488\u6240\u6307\u5730\u5740\u540e\u9762\u4e00\u4e2a\u5bf9\u8c61\u7684\u5730\u5740\u3002 \u4ee5\u4f60\uff0c\u5bf9\u6570\u7ec4\u52a01\u5c31\u53ef\u4ee5\u5f97\u5230\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff0c\u901a\u8fc7\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u8fdb\u884c\u9a8c\u8bc1\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int array []{ 9 , 7 , 5 , 3 , 1 }; std :: cout << & array [ 1 ] << '\\n' ; // print memory address of array element 1 std :: cout << array + 1 << '\\n' ; // print memory address of array pointer + 1 std :: cout << array [ 1 ] << '\\n' ; // prints 7 std :: cout << * ( array + 1 ) << '\\n' ; // prints 7 (note the parenthesis required here) return 0 ; } \u6ce8\u610f\uff0c\u5f53\u901a\u8fc7\u6307\u9488\u8fd0\u7b97\u7684\u7ed3\u679c\u8fdb\u884c\u95f4\u63a5\u64cd\u4f5c\u65f6\uff0c\u5fc5\u987b\u4f7f\u7528\u5706\u62ec\u53f7\u6765\u786e\u4fdd\u64cd\u4f5c\u7b26\u7684\u4f18\u5148\u7ea7\u662f\u6b63\u786e\u7684\uff0c\u56e0\u4e3a operator * \u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e operator + \u3002 \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 0017FB80 0017FB80 7 7 \u4e8b\u5b9e\u8bc1\u660e\uff0c\u5f53\u7f16\u8bd1\u5668\u770b\u5230\u4e0b\u6807\u64cd\u4f5c\u7b26( [] )\u65f6\uff0c\u5b83\u5b9e\u9645\u4e0a\u4f1a\u5c06\u5176\u8f6c\u6362\u4e3a\u6307\u9488\u52a0\u6cd5\u548c\u95f4\u63a5\u64cd\u4f5c\uff01 array[n] \u7b49\u540c\u4e8e *(array + n) \uff0c\u5176\u4e2d n \u4e3a\u6574\u6570\u3002\u4e0b\u6807\u64cd\u4f5c\u7b26 [] \u7684\u5b58\u5728\u65e2\u662f\u4e3a\u4e86\u7f8e\u89c2\uff0c\u4e5f\u662f\u4e3a\u4e86\u4fbf\u4e8e\u4f7f\u7528\u3002","title":"\u6307\u9488\u7b97\u6570\u3001\u6570\u7ec4\u548c\u7d22\u5f15\u80cc\u540e\u7684\u9b54\u6cd5"},{"location":"11-9-Pointer-arithmetic-and-array-indexing/#_4","text":"\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\u548c\u6307\u9488\u7b97\u672f\u6765\u904d\u5386\u6570\u7ec4\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u5e38\u89c1(\u4f7f\u7528\u4e0b\u6807\u901a\u5e38\u66f4\u5bb9\u6613\u9605\u8bfb\u548c\u66f4\u5c11\u7684\u9519\u8bef)\uff0c\u4f46\u7684\u786e\u662f\u53ef\u884c\u7684\u3002\u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include #include // for std::size bool isVowel ( char ch ) { switch ( ch ) { case 'A' : case 'a' : case 'E' : case 'e' : case 'I' : case 'i' : case 'O' : case 'o' : case 'U' : case 'u' : return true ; default : return false ; } } int main () { char name []{ \"Mollie\" }; int arrayLength { static_cast < int > ( std :: size ( name )) }; int numVowels { 0 }; for ( char * ptr { name }; ptr != ( name + arrayLength ); ++ ptr ) { if ( isVowel ( * ptr )) { ++ numVowels ; } } std :: cout << name << \" has \" << numVowels << \" vowels. \\n \" ; return 0 ; } \u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f\u8fd9\u4e2a\u7a0b\u5e8f\u4f7f\u7528\u4e00\u4e2a\u6307\u9488\u6765\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u8bb0\u4f4f\u6570\u7ec4\u4f1a\u9000\u5316\u4e3a\u6307\u5411\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u6307\u9488\u3002\u56e0\u6b64\uff0c\u901a\u8fc7\u4f7f\u7528 name \u521d\u59cb\u5316 ptr \uff0c ptr \u5c06\u6307\u5411\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u5f53\u8c03\u7528 isVowel(*ptr) \u65f6\uff0c\u901a\u8fc7 ptr \u5bf9\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u95f4\u63a5\u64cd\u4f5c\uff0c\u5982\u679c\u5143\u7d20\u662f\u5143\u97f3\uff0c numVowels \u5c06\u9012\u589e\u3002\u7136\u540e\uff0cfor\u5faa\u73af\u4f7f\u7528 ++ \u64cd\u4f5c\u7b26\u5c06\u6307\u9488\u5411\u524d\u79fb\u52a8\u5230\u6570\u7ec4\u4e2d\u7684\u4e0b\u4e00\u4e2a\u5b57\u7b26\u3002\u5f53\u68c0\u67e5\u5b8c\u6240\u6709\u5b57\u7b26\u65f6\uff0cfor\u5faa\u73af\u7ec8\u6b62\u3002 \u4e0a\u8ff0\u7a0b\u5e8f\u4ea7\u751f\u7684\u7ed3\u679c\u662f\uff1a 1 Mollie has 3 vowels \u56e0\u4e3a\u8ba1\u6570\u662f\u975e\u5e38\u5e38\u89c1\u7684\u7b97\u6cd5\uff0c\u6240\u4ee5C++\u7684 algorithms \u5e93\u63d0\u4f9b\u4e86 std::count_if \uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u57fa\u4e8e\u67d0\u4e2a\u6761\u4ef6\u6765\u7edf\u8ba1\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u4e0a\u9762\u7684for\u5faa\u73af\u4f7f\u7528 std::count_if \u8fdb\u884c\u6539\u5199\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include #include #include // for std::begin and std::end bool isVowel ( char ch ) { switch ( ch ) { case 'A' : case 'a' : case 'E' : case 'e' : case 'I' : case 'i' : case 'O' : case 'o' : case 'U' : case 'u' : return true ; default : return false ; } } int main () { char name []{ \"Mollie\" }; // walk through all the elements of name and count how many calls to isVowel return true auto numVowels { std :: count_if ( std :: begin ( name ), std :: end ( name ), isVowel ) }; std :: cout << name << \" has \" << numVowels << \" vowels. \\n \" ; return 0 ; } std::begin \u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u8fed\u4ee3\u5668\uff08\u6307\u9488\uff09\uff0c\u800c std::end \u8fd4\u56de\u7684\u662f\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u540e\u4e00\u4e2a\u4f4d\u7f6e\u3002 std::end \u8fd4\u56de\u7684\u8fed\u4ee3\u5668\u53ea\u4f5c\u4e3a\u6807\u8bb0\u6765\u4f7f\u7528\uff0c\u5bf9\u5176\u8fdb\u884c\u8bbf\u95ee\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u56e0\u4e3a\u5b83\u4e0d\u6307\u5411\u67d0\u4e2a\u5b9e\u9645\u7684\u5143\u7d20\u3002 std::begin \u548c std::end \u53ea\u80fd\u7528\u4e8e\u5df2\u77e5\u957f\u5ea6\u7684\u6570\u7ec4\uff0c\u5982\u679c\u6570\u7ec4\u9000\u5316\u4e3a\u6307\u9488\uff0c\u5219\u9700\u8981\u624b\u52a8\u8ba1\u7b97begin\u548cend\u3002 1 2 3 4 5 // nameLength is the number of elements in the array. std :: count_if ( name , name + nameLength , isVowel ) // Don't do this. Accessing invalid indexes causes undefined behavior. // std::count_if(name, &name[nameLength], isVowel) \u6ce8\u610f\uff0c\u6211\u4eec\u8ba1\u7b97\u7684\u662f name + nameLength \uff0c\u800c\u4e0d\u662f name + nameLength - 1 \uff0c\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u7684\u4e0d\u662f\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u800c\u662f\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u540e\u9762\u7684\u4e00\u4e2a\u201d\u4f2a\u5143\u7d20\u201c\u3002 \u50cf\u8fd9\u6837\u8ba1\u7b97\u6570\u7ec4\u7684\u5f00\u59cb\u548c\u7ed3\u675f\uff0c\u9002\u7528\u4e8e\u6240\u6709\u9700\u8981\u5f00\u59cb\u548c\u7ed3\u675f\u53c2\u6570\u7684\u7b97\u6cd5\u3002","title":"\u4f7f\u7528\u6307\u9488\u904d\u5386\u6570\u7ec4"},{"location":"11-x-Chapter-11-comprehensive-quiz/","tags":["summary"],"text":"11.1 - \u6570\u7ec4\uff08\u7b2c\u4e00\u90e8\u5206\uff09 \u4f7f\u7528\u6570\u7ec4\u53ef\u4ee5\u57fa\u4e8e\u4e00\u4e2a\u6807\u8bc6\u7b26\u6765\u5b58\u50a8\u548c\u8bbf\u95ee\u4e00\u7cfb\u5217\u76f8\u540c\u7c7b\u578b\u7684\u53d8\u91cf\u3002\u6570\u7ec4 \u5143\u6570 \u53ef\u4ee5\u901a\u8fc7\u4e0b\u6807\u8fd0\u7b97\u7b26( [] )\u6765\u8bbf\u95ee\u3002\u6ce8\u610f\uff0c\u8bbf\u95ee\u6570\u7ec4\u65f6\u6570\u7ec4\u4e0b\u6807\u4e0d\u80fd\u8d8a\u754c\u3002\u6570\u7ec4\u7684\u521d\u59cb\u5316\u53ef\u4ee5\u4f7f\u7528\u521d\u59cb\u5316\u503c\u5217\u8868\u6216 uniform-initialization \u3002 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 \u56fa\u5b9a\u6570\u7ec4\u5fc5\u987b\u5728 \u7f16\u8bd1\u65f6 \u786e\u5b9a\u957f\u5ea6\u3002\u56fa\u5b9a\u6570\u7ec4\u5728\u6c42\u503c\u6216\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\u901a\u5e38\u4f1a\u9000\u5316\u4e3a\u6307\u9488\u3002 11.3 - \u6570\u7ec4\u548c\u5faa\u73af 11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f \u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u904d\u5386\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f \u5dee\u4e00\u9519\u8bef \uff0c\u907f\u514d\u904d\u5386\u6570\u7ec4\u65f6\u8bbf\u95ee\u8d8a\u754c\u3002\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af\u5728\u6570\u7ec4\u6ca1\u6709\u9000\u5316\u4e3a\u6307\u9488\u65f6\u662f\u975e\u5e38\u6709\u7528\u7684\u3002 11.5 - \u591a\u7ef4\u6570\u7ec4 \u4f7f\u7528\u591a\u4e2a\u7d22\u5f15\u53ef\u4ee5\u521b\u5efa\u591a\u7ef4\u6570\u7ec4\u3002 11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 11.10 - C \u98ce\u683c\u5b57\u7b26\u4e32\u7b26\u53f7\u5e38\u91cf 11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 \u6570\u7ec4\u53ef\u4ee5\u88ab\u7528\u6765\u521b\u5efaC\u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002\u4e00\u822c\u6765\u8bb2\u4f60\u4e0d\u5e94\u8be5\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u521b\u5efa\u5b57\u7b26\u4e32\uff0c\u800c\u662f\u5e94\u8be5\u4f7f\u7528 std::string_view \u548c std::string \u3002 11.8 - \u6307\u9488\u548c\u6570\u7ec4 11.9 - \u6307\u9488\u8fd0\u7b97\u548c\u6570\u7ec4\u7d22\u5f15 \u6307\u9488\u662f\u90a3\u4e9b\u4fdd\u5b58\u7740\u53e6\u5916\u4e00\u4e2a\u53d8\u91cf\u7684\u5730\u5740\u7684\u53d8\u91cf\u3002 \u53d6\u5730\u5740\u64cd\u4f5c\u7b26& \u53ef\u4ee5\u7528\u4e8e\u83b7\u5f97\u67d0\u4e2a\u53d8\u91cf\u7684\u5730\u5740\u3002\u800c\u95f4\u63a5\u8fd0\u7b97\u7b26\uff08 \u89e3\u5f15\u7528 \uff09\u5219\u53ef\u4ee5\u7528\u4e8e\u53d6\u5f97\u6307\u9488\u6240\u6307\u5730\u5740\u4fdd\u5b58\u7684\u503c\u3002 \u7a7a\u6307\u9488\u6ca1\u6709\u6307\u5411\u4efb\u4f55\u5730\u5740\u3002\u6307\u9488\u53ef\u4ee5\u901a\u8fc7\u521d\u59cb\u5316\u6216\u8d4b\u503c\u4e3a nullptr \u7684\u65b9\u5f0f\u5b9a\u4e49\u4e3a\u7a7a\u6307\u9488\u3002\u5220\u9664\u7a7a\u6307\u9488\u662f\u6ca1\u6709\u95ee\u9898\u7684\u3002 \u6307\u5411\u6570\u7ec4\u7684\u6307\u9488\u5e76\u4e0d\u77e5\u9053\u8be5\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u591a\u5c11\u3002\u8fd9\u4e5f\u610f\u5473\u7740 sizeof() \u8fd0\u7b97\u7b26\u548c \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u65e0\u6cd5\u6b63\u786e\u5de5\u4f5c\u3002 11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d new \u548c delete \u8fd0\u7b97\u7b26\u53ef\u4ee5\u4e3a\u53d8\u91cf\u6216\u6570\u7ec4\u5206\u914d\u52a8\u6001\u5185\u5b58\u3002\u5c3d\u7ba1\u6982\u7387\u5f88\u4f4e\uff0c\u4f46\u64cd\u4f5c\u7b26 new \u662f\u6709\u53ef\u80fd\u4f1a\u5931\u8d25\u7684\uff08\u64cd\u4f5c\u7cfb\u7edf\u5185\u5b58\u8017\u5c3d\u65f6\uff09\u3002\u5982\u679c\u4f60\u7f16\u5199\u7684\u8f6f\u4ef6\u8fd0\u884c\u5728\u5185\u5b58\u6709\u9650\u7684\u5e73\u53f0\u4e0a\u65f6\uff0c\u8bf7\u786e\u4fdd\u68c0\u67e5 new \u64cd\u4f5c\u7b26\u662f\u5426\u6210\u529f\u6267\u884c\u3002 \u5f53\u9700\u8981\u5220\u9664\u6570\u7ec4\u65f6\uff0c\u8bf7\u4f7f\u7528\u6570\u7ec4\u5220\u9664( delete[] ) \u3002\u6307\u5411\u5df2\u7ecf\u88ab\u91ca\u653e\u7684\u5185\u5b58\u5730\u5740\u7684\u6307\u9488\uff0c\u79f0\u4e3a \u60ac\u5782 \u6307\u9488\u3002\u4f7f\u7528\u9519\u8bef\u7684\u5220\u9664\u8fd0\u7b97\u7b26 delete \u6216\u8005\u5bf9\u60ac\u5782\u6307\u9488\u89e3\u5f15\u7528\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u5f53\u6307\u5411\u8be5\u5185\u5b58\u7684\u6700\u540e\u4e00\u4e2a\u6307\u9488\u8d85\u51fa\u4f5c\u7528\u57df\u65f6\uff0c\u672a\u80fd\u5220\u9664\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002 11.12 - \u52a8\u6001\u5206\u914d\u6570\u7ec4 \u4e00\u822c\u7684\u53d8\u91cf\uff0c\u7cfb\u7edf\u4f1a\u5728\u4e00\u5757\u6709\u9650\u7684\u5185\u5b58\u533a\u2014\u2014\u6808\u4e0a\u4e3a\u5176\u5206\u914d\u5185\u5b58\u3002\u52a8\u6001\u5206\u914d\u5185\u5b58\u7684\u53d8\u91cf\u5219\u4f4d\u4e8e\u4e00\u5757\u901a\u7528\u7684\u5185\u5b58\u533a\u57df\u2014\u2014\u5806\u4e0a\u3002 \u6307\u5411const\u7c7b\u578b\u503c\u7684\u6307\u9488\u4f1a\u5c06\u5b83\u6240\u6307\u7684\u503c\u770b\u505a\u5e38\u91cf\u3002 1 2 int value { 5 }; const int * ptr { & value }; // this is okay, ptr is pointing to a \"const int\" const \u6307\u9488\u5219\u662f\u8868\u793a\u6307\u9488\u672c\u8eab\u7684\u5185\u5bb9\uff08\u6301\u6709\u7684\u5730\u5740\uff09\u5728\u521d\u59cb\u5316\u540e\u4e0d\u80fd\u88ab\u6539\u53d8\u3002 1 2 int value { 5 }; int * const ptr { & value }; // ptr is const, but *ptr is non-const \u5f15\u7528\u662f\u53d8\u91cf\u7684\u522b\u540d\u3002\u5f15\u7528\u7684\u58f0\u660e\u9700\u8981\u4f7f\u7528 & \u53f7\uff0c\u4f46\u662f\u5728\u8fd9\u4e2a\u8bed\u5883\u4e0b\u5e76\u4e0d\u8868\u793a \u89e3\u5f15\u7528 \u3002\u5f15\u7528\u5e26\u6709\u9690\u542b\u7684\u201c\u5e38\u91cf\u201d\u542b\u4e49\uff0c\u5b83\u4eec\u5fc5\u987b\u88ab\u521d\u59cb\u5316\uff0c\u6b64\u540e\u5c06\u4e0d\u80fd\u591f\u518d\u4e3a\u5176\u8d4b\u4e88\u65b0\u7684\u503c\u3002\u4f7f\u7528\u5f15\u7528\u53ef\u4ee5\u5728\u51fd\u6570\u4f20\u503c\u6216\u8fd4\u56de\u503c\u65f6\u907f\u514d\u62f7\u8d1d\u3002 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26( -> )\u7528\u4e8e\u9009\u62e9\u6307\u9488\u6240\u6307\u7ed3\u6784\u4f53\u4e2d\u7684\u67d0\u4e2a\u6210\u5458\u3002\u5b83\u7b49\u6548\u4e8e\u95f4\u63a5\u8fd0\u7b97\u7b26\u52a0\u4e0a\u4e00\u822c\u7684\u6210\u5458\u8bbf\u95ee\u7b26\uff08 . \uff09\u3002 11.14 - void \u6307\u9488 void \u7c7b\u578b\u7684\u6307\u9488\u53ef\u4ee5\u6307\u5411\u4efb\u4f55\u7c7b\u578b\u7684\u6570\u636e\uff0c\u4f46\u662f\u4e0d\u80fd\u76f4\u63a5\u5bf9\u5b83\u4eec\u4f7f\u7528\u95f4\u63a5\u8fd0\u7b97\u7b26\uff08 \u89e3\u5f15\u7528 \uff09\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06\u5b83\u8fd8\u539f\u4e3a\u539f\u672c\u7684\u6307\u9488\u7c7b\u578b\u3002\u4e0d\u8fc7\u8fd9\u53d6\u51b3\u4e8e\u4f60\u662f\u5426\u8fd8\u8bb0\u5f97\u5b83\u539f\u672c\u662f\u4ec0\u4e48\u7c7b\u578b\u3002 11.15 \u2014 \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u548c\u591a\u7ef4\u6570\u7ec4 \u901a\u8fc7\u6307\u9488\u7684\u6307\u9488\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u6307\u5411\u5176\u4ed6\u6307\u9488\u7684\u6307\u9488\u3002 11.16 \u2014 \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb std::array \u63d0\u4f9b\u4e86C++\u5185\u7f6e\u6570\u7ec4\u7684\u5168\u90e8\uff08\u751a\u81f3\uff09\u66f4\u591a\u7684\u529f\u80fd\uff0c\u800c\u4e14\u5b83\u4e0d\u4f1a\u9000\u5316\u4e3a\u6307\u9488\u3002\u901a\u5e38\u6765\u8bf4\u4f7f\u7528\u5b83\u6765\u66ff\u6362\u5185\u7f6e\u6570\u7ec4\u662f\u66f4\u52a0\u7684\u9009\u62e9\u3002 11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb std::vector \u63d0\u4f9b\u4e86\u52a8\u6001\u6570\u7ec4\u7684\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u81ea\u884c\u7ba1\u7406\u5185\u5b58\u5e76\u8bb0\u5f55\u6570\u7ec4\u7684\u5927\u5c0f\u3002\u901a\u5e38\u4f7f\u7528\u5b83\u6765\u66ff\u6362\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\u662f\u66f4\u52a0\u7684\u9009\u62e9\u3002 11.18 \u2014 \u8fed\u4ee3\u5668\u7b80\u4ecb \u7531\u4e8e \u8fed\u4ee3\u5668 \u7684\u5b58\u5728\uff0c\u6211\u4eec\u5176\u5b9e\u5e76\u4e0d\u9700\u8981\u53bb\u4e86\u89e3\u5bb9\u5668\u7684\u5177\u4f53\u5b9e\u73b0\u5c31\u53ef\u4ee5\u5b8c\u6210\u5faa\u73af\u904d\u5386\u3002 11.19 \u2014 \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb algorithms \u5e93\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u5f88\u591a\u8d81\u624b\u7684\u51fd\u6570\uff0c\u642d\u914d\u8fed\u4ee3\u5668\uff08\u4ee5\u53ca\u540e\u9762\u5b66\u5230\u7684lambda\u51fd\u6570\uff09\uff0c\u5b83\u4eec\u6784\u6210\u4e86C++\u8bed\u8a00\u975e\u5e38\u91cd\u8981\u7684\u4e00\u90e8\u5206\u3002","title":"11.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5206\u914d"},{"location":"12-1-function-pointers/","tags":["function"],"text":"Key Takeaway \u5728 9.6 - \u6307\u9488\u7b80\u4ecb \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u6307\u9488\uff0c\u6307\u9488\u662f\u4e00\u4e2a\u4fdd\u5b58\u7740\u5176\u4ed6\u5730\u5740\u53d8\u91cf\u7684\u53d8\u91cf\u3002 \u51fd\u6570\u6307\u9488 \u4e5f\u662f\u7c7b\u4f3c\u7684\uff0c\u53ea\u4e0d\u8fc7\u5b83\u6307\u5411\u7684\u4e0d\u662f\u53d8\u91cf\uff0c\u800c\u662f\u4e00\u4e2a\u51fd\u6570\uff01 \u8003\u8651\u4e0b\u9762\u51fd\u6570\uff1a 1 2 3 4 int foo () { return 5 ; } foo \u662f\u51fd\u6570\u540d\uff0c\u4f46\u662f\u51fd\u6570\u7684\u7c7b\u578b\u662f\u4ec0\u4e48\uff1f\u51fd\u6570\u6709\u5b83\u81ea\u5df1\u7684 \u5de6\u503c \u51fd\u6570\u7c7b\u578b\u2014\u2014\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570\u7c7b\u578b\u662f\u8fd4\u56de\u6574\u578b\u5e76\u4e14\u4e0d\u63a5\u53d7\u53c2\u6570\u3002\u548c\u53d8\u91cf\u7c7b\u4f3c\uff0c\u51fd\u6570\u4e5f\u5728\u5b58\u653e\u5728\u5185\u5b58\u4e2d\u3002 \u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u8df3\u8f6c\u5230\u88ab\u8c03\u7528\u51fd\u6570\u7684\u5730\u5740\u53bb\u6267\u884c\uff1a 1 2 3 4 5 6 7 8 9 10 11 int foo () // code for foo starts at memory address 0x002717f0 { return 5 ; } int main () { foo (); // jump to address 0x002717f0 return 0 ; } \u5728\u4f60\u7684\u7f16\u7a0b\u751f\u6daf\u4e2d\u53ef\u80fd\u4f1a\u72af\u8fd9\u6837\u7684\u9519\u8bef\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int foo () // code starts at memory address 0x002717f0 { return 5 ; } int main () { std :: cout << foo << '\\n' ; // we meant to call foo(), but instead we're printing foo itself! return 0 ; } \u672c\u6765\u6211\u4eec\u662f\u60f3\u8981\u8c03\u7528\u51fd\u6570foo\u5e76\u6253\u5370\u5b83\u7684\u8fd4\u56de\u503c\u7684\uff0c\u4f46\u662f\u5374\u65e0\u610f\u4e2d\u76f4\u63a5\u4f7f\u7528 std::cout \u6253\u5370\u51fd\u6570\u672c\u8eab\u3002\u6b64\u65f6\u4f1a\u5f97\u5230\u4ec0\u4e48\u6837\u7684\u7ed3\u679c\u5462\uff1f \u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\u4f1a\u8f93\u51fa\u5982\u4e0b\u7ed3\u679c\uff1a 1 0x002717f0 \u4f46\u662f\uff0c\u5728\u4f60\u7684\u7535\u8111\u4e0a\u53ef\u80fd\u4f1a\u6253\u5370\u5176\u4ed6\u503c\uff08\u4f8b\u5982 1\uff09\uff0c\u8fd9\u53d6\u51b3\u4e8e\u4f60\u7684\u7f16\u8bd1\u5668\u5982\u4f55\u5c06\u51fd\u6570\u6307\u9488\u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b\u3002\u5982\u679c\u4f60\u7684\u673a\u5668\u6ca1\u6709\u6253\u5370\u51fd\u6570\u5730\u5740\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u5c06\u51fd\u6570\u5f3a\u5236\u8f6c\u6362\u4e3a\u7a7a\u6307\u9488\u5e76\u6253\u5370\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int foo () // code starts at memory address 0x002717f0 { return 5 ; } int main () { std :: cout << reinterpret_cast < void *> ( foo ) << '\\n' ; // \u544a\u8bc9 C++ \u5c06foo\u89e3\u6790\u4e3a\u7a7a\u6307\u9488 return 0 ; } \u5c31\u50cf\u53ef\u4ee5\u58f0\u660e\u4e00\u4e2a\u6307\u5411\u666e\u901a\u53d8\u91cf\u7684\u975econst\u6307\u9488\u4e00\u6837\uff0c\u4e5f\u53ef\u4ee5\u58f0\u660e\u4e00\u4e2a\u6307\u5411\u51fd\u6570\u7684\u975econst\u6307\u9488\u3002\u5728\u672c\u8bfe\u7684\u5176\u4f59\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u7814\u7a76\u8fd9\u4e9b\u51fd\u6570\u6307\u9488\u53ca\u5176\u7528\u6cd5\u3002\u51fd\u6570\u6307\u9488\u662f\u4e00\u4e2a\u76f8\u5f53\u9ad8\u7ea7\u7684\u4e3b\u9898\uff0c\u5bf9\u4e8e\u53ea\u5e0c\u671b\u4e86\u89e3C++\u57fa\u7840\u77e5\u8bc6\u7684\u4eba\u6765\u8bf4\uff0c\u672c\u8bfe\u7684\u5176\u4f59\u90e8\u5206\u53ef\u4ee5\u5b89\u5168\u5730\u8df3\u8fc7\u6216\u7565\u8fc7\u3002 \u6307\u5411\u51fd\u6570\u7684\u6307\u9488 \u521b\u5efa\u975econst\u51fd\u6570\u6307\u9488\u7684\u8bed\u6cd5\u662fC++\u4e2d\u6700\u4e11\u964b\u7684\u8bed\u6cd5\u4e4b\u4e00\uff1a 1 2 // fcnPtr is a pointer to a function that takes no arguments and returns an integer int ( * fcnPtr )(); \u4e0a\u9762\u4ee3\u7801\u4e2d\u7684 fcnPtr \u4e0a\u4e00\u4e2a\u51fd\u6570\u6307\u9488\uff0c\u5b83\u6ca1\u6709\u5f62\u53c2\u4e14\u8fd4\u56de\u6574\u578b\u3002 fcnPtr \u53ef\u4ee5\u6267\u884c\u4efb\u4f55\u8be5\u7c7b\u578b\u7684\u51fd\u6570\u3002 *fcnPtr \u4e24\u8fb9\u7684\u62ec\u53f7\u662f\u5fc5\u987b\u7684\uff0c\u5b83\u53ef\u4ee5\u786e\u4fdd\u4f18\u5148\u7ea7\u662f\u6b63\u786e\u7684\uff0c\u5426\u5219 int* fcnPtr() \u5c31\u4f1a\u88ab\u89e3\u6790\u4e3a\u540d\u4e3a fcnPtr \uff0c\u4e0d\u63a5\u53d7\u53c2\u6570\uff0c\u8fd4\u56de\u6574\u578b\u6307\u9488\u7684\u51fd\u6570\u7684\u524d\u5411\u58f0\u660e\u3002 \u521b\u5efaconst\u51fd\u6570\u6307\u9488\uff0c\u9700\u8981\u5c06const\u5173\u952e\u5b57\u6dfb\u52a0\u5728\u661f\u53f7\u540e\u9762\uff1a 1 int ( * const fcnPtr )(); \u5982\u679c\u628aconst\u653e\u5728int\u524d\u9762\uff0c\u5219\u8868\u793a\u51fd\u6570\u6307\u9488\u6307\u5411\u7684\u51fd\u6570\u8fd4\u56de\u4e00\u4e2aconst\u6574\u578b\u3002 \u5c06\u51fd\u6570\u8d4b\u503c\u7ed9\u51fd\u6570\u6307\u9488 \u51fd\u6570\u6307\u9488\u53ef\u4ee5\u7528\u51fd\u6570\u521d\u59cb\u5316(\u975econst\u51fd\u6570\u6307\u9488\u53ef\u4ee5\u88ab\u8d4b\u503c\u4e3a\u51fd\u6570)\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u76f4\u63a5\u4f7f\u7528\u4e86 foo \uff0c\u5e76\u4e14\u5b83\u5df2\u7ecf\u88ab\u8f6c\u6362\u4e3a\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u3002\u4e0e\u6307\u5411\u53d8\u91cf\u7684\u6307\u9488\u4e00\u6837\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528 &foo \u6765\u83b7\u5f97\u6307\u5411 foo \u7684\u51fd\u6570\u6307\u9488\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 int foo () { return 5 ; } int goo () { return 6 ; } int main () { int ( * fcnPtr )(){ & foo }; // fcnPtr points to function foo fcnPtr = & goo ; // fcnPtr now points to function goo return 0 ; } \u4e0b\u9762\u4ee3\u7801\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u9519\u8bef\uff1a 1 fcnPtr = goo (); \u8be5\u884c\u4ee3\u7801\u5c06 goo() \u7684\u8fd4\u56de\u503c(\u7c7b\u578b\u4e3a int ) \u8d4b\u503c\u7ed9 fcnPtr (\u5b9e\u9645\u671f\u671b\u7684\u7c7b\u578b\u662f int(*)() )\uff0c\u8fd9\u5e76\u4e0d\u662f\u6211\u4eec\u7684\u672c\u610f\u3002\u6211\u4eec\u5176\u5b9e\u5e0c\u671b fcnPtr \u88ab\u8d4b\u503c\u4e3a goo \u7684\u5730\u5740\uff0c\u800c\u4e0d\u662f goo() \u7684 So no parentheses are needed. \u6ce8\u610f\uff0c\u51fd\u6570\u6307\u9488\u7684\u7c7b\u578b\uff08\u8fd4\u56de\u503c\u548c\u53c2\u6570\uff09\u5fc5\u987b\u548c\u51fd\u6570\u7c7b\u578b\u5339\u914d\uff0c\u4e00\u4e9b\u5b9e\u4f8b\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 // function prototypes int foo (); double goo (); int hoo ( int x ); // function pointer assignments int ( * fcnPtr1 )(){ & foo }; // \u5bf9 int ( * fcnPtr2 )(){ & goo }; // \u9519 -- \u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u6b63\u786e double ( * fcnPtr4 )(){ & goo }; // \u5bf9 fcnPtr1 = & hoo ; // \u9519 -- fcnPtr1 \u4e0d\u63a5\u53d7\u53c2\u6570\uff0c\u548c hoo() \u4e0d\u5339\u914d int ( * fcnPtr3 )( int ){ & hoo }; // \u5bf9 \u4e0e\u57fa\u672c\u7c7b\u578b\u4e0d\u540c\uff0cc++ \u4f1a\u5728\u9700\u8981\u65f6\u9690\u5f0f\u5730\u5c06\u51fd\u6570\u8f6c\u6362\u4e3a\u51fd\u6570\u6307\u9488(\u56e0\u6b64\u4f60==\u4e0d\u9700\u8981\u4f7f\u7528address-of\u64cd\u4f5c\u7b26(&)\u6765\u83b7\u53d6\u51fd\u6570\u7684\u5730\u5740==)\u3002\u4f46\u662f\uff0c\u5b83\u4e0d\u4f1a\u9690\u5f0f\u5730\u5c06\u51fd\u6570\u6307\u9488\u8f6c\u6362\u4e3a\u7a7a\u6307\u9488\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002 \u51fd\u6570\u6307\u9488\u53ef\u4ee5\u88ab\u521d\u59cb\u5316\u6216\u8d4b\u503c\u4e3a nullptr \uff1a 1 int ( * fcnptr )() { nullptr }; // \u5bf9 \u4f7f\u7528\u51fd\u6570\u6307\u9488\u8c03\u7528\u51fd\u6570 \u51fd\u6570\u6307\u9488\u7684\u53e6\u4e00\u4e2a\u4e3b\u8981\u529f\u80fd\u662f\u4f7f\u7528\u5b83\u6765\u8c03\u7528\u51fd\u6570\u3002\u901a\u8fc7\u51fd\u6570\u6307\u9488\u8c03\u7528\u51fd\u6570\u6709\u4e24\u4e2a\u65b9\u6cd5\uff0c\u4e00\u662f\u901a\u8fc7\u663e\u5f0f\u89e3\u5f15\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int foo ( int x ) { return x ; } int main () { int ( * fcnPtr )( int ){ & foo }; // Initialize fcnPtr with function foo ( * fcnPtr )( 5 ); // call function foo(5) through fcnPtr. return 0 ; } \u7b2c\u4e8c\u79cd\u65b9\u6cd5\u662f\u901a\u8fc7\u9690\u5f0f\u89e3\u5f15\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int foo ( int x ) { return x ; } int main () { int ( * fcnPtr )( int ){ & foo }; // Initialize fcnPtr with function foo fcnPtr ( 5 ); // call function foo(5) through fcnPtr. return 0 ; } \u5982\u4f60\u6240\u89c1\uff0c\u4f7f\u7528\u9690\u5f0f\u89e3\u5f15\u7528\u8c03\u7528\u51fd\u6570\u770b\u8d77\u6765\u548c\u6b63\u5e38\u7684\u51fd\u6570\u8c03\u7528\u4e00\u6a21\u4e00\u6837\uff0c\u56e0\u4e3a\u51fd\u6570\u540d\u672c\u6765\u5c31\u662f\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\uff01\u4f46\u662f\uff0c\u6709\u4e9b\u65e7\u7684\u7f16\u8bd1\u5668\u5e76\u4e0d\u652f\u6301\u8fd9\u79cd\u9690\u5f0f\u89e3\u5f15\u7528\u7684\u65b9\u5f0f\uff0c\u4f46\u662f\u73b0\u4ee3\u7f16\u8bd1\u5668\u90fd\u652f\u6301\u3002 \u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a\u9ed8\u8ba4\u5f62\u53c2\u4e0d\u9002\u7528\u4e8e\u901a\u8fc7\u51fd\u6570\u6307\u9488\u8c03\u7528\u7684\u51fd\u6570\u3002\u9ed8\u8ba4\u5f62\u53c2\u5728\u7f16\u8bd1\u65f6\u89e3\u6790(\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u4e3a\u9ed8\u8ba4\u5f62\u53c2\u63d0\u4f9b\u5b9e\u53c2\uff0c\u7f16\u8bd1\u5668\u5c06\u5728\u7f16\u8bd1\u4ee3\u7801\u65f6\u4e3a\u60a8\u63d0\u4f9b\u4e00\u4e2a\u5b9e\u53c2)\u3002\u4f46\u662f\uff0c\u51fd\u6570\u6307\u9488\u5728\u8fd0\u884c\u65f6\u89e3\u6790\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528\u51fd\u6570\u6307\u9488\u8fdb\u884c\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u65e0\u6cd5\u89e3\u6790\u9ed8\u8ba4\u5f62\u53c2\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5fc5\u987b\u663e\u5f0f\u5730\u4f20\u5165\u4efb\u4f55\u9ed8\u8ba4\u53c2\u6570\u7684\u503c\u3002 \u8fd8\u8981\u6ce8\u610f\uff0c\u56e0\u4e3a\u51fd\u6570\u6307\u9488\u53ef\u4ee5\u8bbe\u7f6e\u4e3anullptr\uff0c\u6240\u4ee5\u5728\u8c03\u7528\u51fd\u6570\u6307\u9488\u4e4b\u524d\uff0c\u6700\u597d\u901a\u8fc7\u65ad\u8a00\u6216\u6709\u6761\u4ef6\u6d4b\u8bd5\u5224\u65ad\u5b83\u662f\u5426\u662f\u7a7a\u6307\u9488\u3002\u5c31\u50cf\u666e\u901a\u6307\u9488\u4e00\u6837\uff0c\u5bf9\u7a7a\u51fd\u6570\u6307\u9488\u7684\u89e3\u5f15\u7528\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 int foo ( int x ) { return x ; } int main () { int ( * fcnPtr )( int ){ & foo }; // Initialize fcnPtr with function foo if ( fcnPtr ) // make sure fcnPtr isn't a null pointer fcnPtr ( 5 ); // otherwise this will lead to undefined behavior return 0 ; } \u5c06\u51fd\u6570\u4f5c\u4e3a\u5b9e\u53c2\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570 \u51fd\u6570\u6307\u9488\u6700\u5927\u7684\u7528\u9014\u5176\u5b9e\u662f\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\u3002\u8fd9\u79cd\u4f5c\u4e3a\u5b9e\u53c2\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\u7684\u51fd\u6570\uff0c\u79f0\u4e3a \u56de\u8c03\u51fd\u6570 \u3002 \u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u5047\u8bbe\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u6267\u884c\u4e00\u9879\u4efb\u52a1(\u4f8b\u5982\u5bf9\u6570\u7ec4\u6392\u5e8f)\uff0c\u4f46\u4f60\u5e0c\u671b\u7528\u6237\u80fd\u591f\u5b9a\u4e49\u5982\u4f55\u6267\u884c\u8be5\u4efb\u52a1\u7684\u7279\u5b9a\u90e8\u5206(\u4f8b\u5982\u6570\u7ec4\u662f\u6309\u5347\u5e8f\u8fd8\u662f\u964d\u5e8f\u6392\u5e8f)\u3002\u8ba9\u6211\u4eec\u4ed4\u7ec6\u770b\u770b\u8fd9\u4e2a\u95ee\u9898\u662f\u5982\u4f55\u5177\u4f53\u5e94\u7528\u4e8e\u6392\u5e8f\u7684\uff0c\u4f5c\u4e3a\u4e00\u4e2a\u53ef\u4ee5\u63a8\u5e7f\u5230\u5176\u4ed6\u7c7b\u4f3c\u95ee\u9898\u7684\u4f8b\u5b50\u3002 \u8bb8\u591a\u57fa\u4e8e\u6bd4\u8f83\u7684\u6392\u5e8f\u7b97\u6cd5\u90fd\u57fa\u4e8e\u7c7b\u4f3c\u7684\u6982\u5ff5\uff1a\u6392\u5e8f\u7b97\u6cd5\u904d\u5386\u5217\u8868\uff0c\u5bf9\u6570\u5b57\u5bf9\u8fdb\u884c\u6bd4\u8f83\uff0c\u5e76\u57fa\u4e8e\u8fd9\u4e9b\u6bd4\u8f83\u7684\u7ed3\u679c\u5bf9\u6570\u5b57\u8fdb\u884c\u91cd\u65b0\u6392\u5e8f\u3002\u56e0\u6b64\uff0c\u901a\u8fc7\u6539\u53d8\u6bd4\u8f83\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6539\u53d8\u7b97\u6cd5\u7684\u6392\u5e8f\u65b9\u5f0f\uff0c\u800c\u4e0d\u5f71\u54cd\u5176\u4ed6\u6392\u5e8f\u4ee3\u7801\u3002 \u4e0b\u9762\u662f\u4e4b\u524d\u7684\u9009\u62e9\u6392\u5e8f\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // for std::swap void SelectionSort ( int * array , int size ) { // Step through each element of the array for ( int startIndex { 0 }; startIndex < ( size - 1 ); ++ startIndex ) { // smallestIndex is the index of the smallest element we've encountered so far. int smallestIndex { startIndex }; // Look for smallest element remaining in the array (starting at startIndex+1) for ( int currentIndex { startIndex + 1 }; currentIndex < size ; ++ currentIndex ) { // If the current element is smaller than our previously found smallest if ( array [ smallestIndex ] > array [ currentIndex ]) // COMPARISON DONE HERE { // This is the new smallest number for this iteration smallestIndex = currentIndex ; } } // Swap our start element with our smallest element std :: swap ( array [ startIndex ], array [ smallestIndex ]); } } \u8ba9\u6211\u4eec\u7528\u4e00\u4e2a\u51fd\u6570\u6765\u66ff\u6362\u8fd9\u91cc\u7684\u6bd4\u8f83\u3002\u6bd4\u8f83\u51fd\u6570\u6bd4\u8f83\u4e24\u4e2a\u6574\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\u6765\u6307\u793a\u5143\u7d20\u662f\u5426\u5e94\u8be5\u4ea4\u6362\uff0c\u5b83\u770b\u8d77\u6765\u50cf\u8fd9\u6837: 1 2 3 4 bool ascending ( int x , int y ) { return x > y ; // swap if the first element is greater than the second } \u4f7f\u7528 ascending() \u51fd\u6570\u4fee\u6539\u540e\u7684\u6392\u5e8f\u7b97\u6cd5\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // for std::swap void SelectionSort ( int * array , int size ) { // Step through each element of the array for ( int startIndex { 0 }; startIndex < ( size - 1 ); ++ startIndex ) { // smallestIndex is the index of the smallest element we've encountered so far. int smallestIndex { startIndex }; // Look for smallest element remaining in the array (starting at startIndex+1) for ( int currentIndex { startIndex + 1 }; currentIndex < size ; ++ currentIndex ) { // If the current element is smaller than our previously found smallest if ( ascending ( array [ smallestIndex ], array [ currentIndex ])) // COMPARISON DONE HERE { // This is the new smallest number for this iteration smallestIndex = currentIndex ; } } // Swap our start element with our smallest element std :: swap ( array [ startIndex ], array [ smallestIndex ]); } } \u73b0\u5728\uff0c\u4e3a\u4e86\u8ba9\u8c03\u7528\u8005\u51b3\u5b9a\u5982\u4f55\u8fdb\u884c\u6392\u5e8f\uff0c\u6211\u4eec\u4e0d\u4f7f\u7528\u56fa\u5b9a\u7684\u6bd4\u8f83\u51fd\u6570\uff0c\u800c\u662f\u5141\u8bb8\u8c03\u7528\u8005\u63d0\u4f9b\u4ed6\u4eec\u81ea\u5df1\u7684\u6392\u5e8f\u51fd\u6570\uff01\u8fd9\u662f\u901a\u8fc7\u51fd\u6570\u6307\u9488\u5b8c\u6210\u7684\u3002 \u56e0\u4e3a\u8c03\u7528\u8005\u7684\u6bd4\u8f83\u51fd\u6570\u5c06\u6bd4\u8f83\u4e24\u4e2a\u6574\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u6240\u4ee5\u6307\u5411\u8fd9\u6837\u4e00\u4e2a\u51fd\u6570\u7684\u6307\u9488\u770b\u8d77\u6765\u50cf\u8fd9\u6837: 1 bool ( * comparisonFcn )( int , int ); \u56e0\u6b64\uff0c\u6211\u4eec\u5c06\u5141\u8bb8\u8c03\u7528\u8005\u5c06\u4e00\u4e2a\u6307\u5411\u4ed6\u4eec\u60f3\u8981\u7684\u6bd4\u8f83\u51fd\u6570\u7684\u6307\u9488\u4f5c\u4e3a\u7b2c\u4e09\u4e2a\u5f62\u53c2\u4f20\u9012\u7ed9\u6392\u5e8f\u4f8b\u7a0b\uff0c\u7136\u540e\u6211\u4eec\u5c06\u4f7f\u7528\u8c03\u7528\u8005\u7684\u51fd\u6570\u8fdb\u884c\u6bd4\u8f83\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u51fd\u6570\u6307\u9488\u5f62\u53c2\u8fdb\u884c\u7528\u6237\u5b9a\u4e49\u6bd4\u8f83\u7684\u9009\u62e9\u6392\u5e8f\u7684\u5b8c\u6574\u793a\u4f8b\uff0c\u4ee5\u53ca\u5982\u4f55\u8c03\u7528\u5b83\u7684\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 #include // for std::swap #include // Note our user-defined comparison is the third parameter void selectionSort ( int * array , int size , bool ( * comparisonFcn )( int , int )) { // Step through each element of the array for ( int startIndex { 0 }; startIndex < ( size - 1 ); ++ startIndex ) { // bestIndex is the index of the smallest/largest element we've encountered so far. int bestIndex { startIndex }; // Look for smallest/largest element remaining in the array (starting at startIndex+1) for ( int currentIndex { startIndex + 1 }; currentIndex < size ; ++ currentIndex ) { // If the current element is smaller/larger than our previously found smallest if ( comparisonFcn ( array [ bestIndex ], array [ currentIndex ])) // COMPARISON DONE HERE { // This is the new smallest/largest number for this iteration bestIndex = currentIndex ; } } // Swap our start element with our smallest/largest element std :: swap ( array [ startIndex ], array [ bestIndex ]); } } // Here is a comparison function that sorts in ascending order // (Note: it's exactly the same as the previous ascending() function) bool ascending ( int x , int y ) { return x > y ; // swap if the first element is greater than the second } // Here is a comparison function that sorts in descending order bool descending ( int x , int y ) { return x < y ; // swap if the second element is greater than the first } // This function prints out the values in the array void printArray ( int * array , int size ) { for ( int index { 0 }; index < size ; ++ index ) { std :: cout << array [ index ] << ' ' ; } std :: cout << '\\n' ; } int main () { int array [ 9 ]{ 3 , 7 , 9 , 5 , 6 , 1 , 8 , 2 , 4 }; // Sort the array in descending order using the descending() function selectionSort ( array , 9 , descending ); printArray ( array , 9 ); // Sort the array in ascending order using the ascending() function selectionSort ( array , 9 , ascending ); printArray ( array , 9 ); return 0 ; } \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 \u9177\u4e0d\u9177\uff1f\u6211\u4eec\u7ed9\u4e86\u8c03\u7528\u8005\u63a7\u5236\u9009\u62e9\u6392\u5e8f\u5982\u4f55\u5de5\u4f5c\u7684\u80fd\u529b\u3002 \u8c03\u7528\u8005\u751a\u81f3\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u201c\u5947\u602a\u7684\u201d\u6bd4\u8f83\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 bool evensFirst ( int x , int y ) { // if x is even and y is odd, x goes first (no swap needed) if (( x % 2 == 0 ) && ! ( y % 2 == 0 )) return false ; // if x is odd and y is even, y goes first (swap needed) if ( ! ( x % 2 == 0 ) && ( y % 2 == 0 )) return true ; // otherwise sort in ascending order return ascending ( x , y ); } int main () { int array [ 9 ]{ 3 , 7 , 9 , 5 , 6 , 1 , 8 , 2 , 4 }; selectionSort ( array , 9 , evensFirst ); printArray ( array , 9 ); return 0 ; } \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 4 6 8 1 3 5 7 9 \u5982\u4f60\u6240\u89c1\uff0c\u5728\u6b64\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\u51fd\u6570\u6307\u9488\u662f\u4e00\u79cd\u5f88\u597d\u65b9\u5f0f\uff0c\u8c03\u7528\u8005\u53ef\u4ee5\u5c06\u81ea\u5df1\u7684\u529f\u80fd\u201c\u6302\u94a9\u201d\u5230\u4f60\u4ee5\u524d\u7f16\u5199\u548c\u6d4b\u8bd5\u8fc7\u7684\u5185\u5bb9\u4e2d\uff0c\u8fd9\u6709\u52a9\u4e8e\u4fc3\u8fdb\u4ee3\u7801\u91cd\u7528\uff01\u4ee5\u524d\uff0c\u5982\u679c\u5e0c\u671b\u6309\u964d\u5e8f\u6392\u5217\u4e00\u4e2a\u6570\u7ec4\uff0c\u6309\u5347\u5e8f\u6392\u5217\u53e6\u4e00\u4e2a\u6570\u7ec4\uff0c\u5219\u9700\u8981\u591a\u4e2a\u7248\u672c\u7684\u6392\u5e8f\u4f8b\u7a0b\u3002\u73b0\u5728\u4f60\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7248\u672c\u5e76\u6309\u8c03\u7528\u8005\u5e0c\u671b\u7684\u4efb\u4f55\u65b9\u5f0f\u6392\u5e8f\uff01 \u6ce8\u610f\uff1a \u5982\u679c\u51fd\u6570\u5f62\u53c2\u662f\u51fd\u6570\u7c7b\u578b\u7684\uff0c\u5b83\u5c06\u88ab\u8f6c\u6362\u4e3a\u6307\u5411\u51fd\u6570\u7c7b\u578b\u7684\u6307\u9488 \u3002\u8fd9\u610f\u5473\u7740\uff1a 1 void selectionSort ( int * array , int size , bool ( * comparisonFcn )( int , int )) \u7b49\u4ef7\u4e8e\uff1a 1 void selectionSort ( int * array , int size , bool comparisonFcn ( int , int )) \u8fd9\u53ea\u9002\u7528\u4e8e\u51fd\u6570\u5f62\u53c2\uff0c\u800c\u4e0d\u9002\u7528\u4e8e\u72ec\u7acb\u7684\u51fd\u6570\u6307\u9488\uff0c\u56e0\u6b64\u4f7f\u7528\u6bd4\u8f83\u6709\u9650\u3002 \u63d0\u4f9b\u9ed8\u8ba4\u51fd\u6570 \u5982\u679c\u4f60\u8981\u5141\u8bb8\u8c03\u7528\u65b9\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\uff0c\u90a3\u4e48\u4e3a\u8c03\u7528\u65b9\u63d0\u4f9b\u4e00\u4e9b\u6807\u51c6\u51fd\u6570\u4ee5\u65b9\u4fbf\u4f7f\u7528\u901a\u5e38\u662f\u5f88\u6709\u7528\u7684\u3002\u4f8b\u5982\uff0c\u5728\u4e0a\u9762\u7684\u9009\u62e9\u6392\u5e8f\u793a\u4f8b\u4e2d\uff0c\u63d0\u4f9b ascending() \u548c descending() \u51fd\u6570\u4ee5\u53ca selectionSort() \u51fd\u6570\u5c06\u4f7f\u8c03\u7528\u8005\u7684\u5de5\u4f5c\u66f4\u52a0\u8f7b\u677e\uff0c\u56e0\u4e3a\u4ed6\u4eec\u4e0d\u5fc5\u6bcf\u6b21\u60f3\u8981\u4f7f\u7528 ascending() \u548c descending() \u51fd\u6570\u65f6\u90fd\u91cd\u5199\u5b83\u4eec\u3002 \u4f60\u751a\u81f3\u53ef\u4ee5\u8bbe\u7f6e\u5176\u4e2d\u4e00\u4e2a\u4f5c\u4e3a\u9ed8\u8ba4\u53c2\u6570\uff1a 1 2 // Default the sort to ascending sort void selectionSort ( int * array , int size , bool ( * comparisonFcn )( int , int ) = ascending ); \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u7528\u6237\u6b63\u5e38\u8c03\u7528 selectionSort \uff08\u4e0d\u4f7f\u7528\u51fd\u6570\u6307\u9488\uff09\uff0c comparisonFcn \u5f62\u53c2 \u9ed8\u8ba4\u662f\u5347\u5e8f\u7684\u3002\u4f60\u5fc5\u987b\u786e\u4fdd ascending \u51fd\u6570\u5728\u6b64\u4e4b\u524d\u88ab\u5b9a\u4e49\uff0c\u5426\u5219\u7f16\u8bd1\u5668\u5c06\u4f1a\u62a5\u544a ascending \u627e\u4e0d\u5230\u3002 \u4f7f\u7528\u7c7b\u578b\u522b\u540d\u8ba9\u51fd\u6570\u6307\u9488\u770b\u8d77\u6765\u66f4\u4f18\u96c5 \u5b9e\u8bdd\u5b9e\u8bf4\u2014\u2014\u51fd\u6570\u6307\u9488\u7684\u8bed\u6cd5\u7684\u786e\u5f88\u96be\u770b\u3002\u4e0d\u8fc7\uff0c \u7c7b\u578b\u522b\u540d \u53ef\u4ee5\u7528\u6765\u4f7f\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\u770b\u8d77\u6765\u66f4\u50cf\u5e38\u89c4\u53d8\u91cf \uff1a 1 using ValidateFunction = bool ( * )( int , int ); \u7c7b\u578b\u522b\u540d \u201cValidateFunction\u201d \u662f\u4e00\u4e2a\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\uff0c\u8be5\u6307\u9488\u63a5\u53d7\u4e24\u4e2a\u6574\u578b\u5f62\u53c2\u5e76\u8fd4\u56de\u4e00\u4e2abool\u7c7b\u578b\u503c\u3002 \u6b64\u65f6\u6211\u4eec\u5c31\u53ef\u4ee5\u4e0d\u8fd9\u6837\u505a\uff1a 1 bool validate ( int x , int y , bool ( * fcnPtr )( int , int )); // \u4e11\u964b \u4f60\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 bool validate ( int x , int y , ValidateFunction pfcn ) // \u4f18\u96c5 \u4f7f\u7528 std::function \u5b9a\u4e49\u548c\u5b58\u50a8\u51fd\u6570\u6307\u9488\u8fd8\u6709\u4e00\u4e2a\u529e\u6cd5\uff0c\u5373\u4f7f\u7528 std::function \uff0c\u5b83\u5b9a\u4e49\u5728\u6807\u51c6\u5e93 \u5934\u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u5c06\u51fd\u6570\u6307\u9488\u5b9a\u4e49\u4e3a\u4e00\u4e2a std::function \u5bf9\u8c61\uff1a 1 2 #include bool validate ( int x , int y , std :: function < bool ( int , int ) > fcn ); // std::function method that returns a bool and takes two int parameters \u5982\u4f60\u6240\u89c1\uff0c\u8fd4\u56de\u503c\u7c7b\u578b\u5b9a\u4e49\u5728\u5c16\u62ec\u53f7\u4e2d\uff0c\u53c2\u6570\u7c7b\u578b\u5219\u88ab\u5b9a\u4e49\u5728\u62ec\u53f7\u4e2d\u3002\u5982\u679c\u6ca1\u6709\u53c2\u6570\u7684\u8bdd\uff0c\u62ec\u53f7\u53ef\u4ee5\u7559\u7a7a\u3002 \u4f7f\u7528 std::function \u66f4\u65b0\u4e4b\u524d\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include int foo () { return 5 ; } int goo () { return 6 ; } int main () { std :: function < int () > fcnPtr { & foo }; // declare function pointer that returns an int and takes no parameters fcnPtr = & goo ; // fcnPtr now points to function goo std :: cout << fcnPtr () << '\\n' ; // call the function just like normal return 0 ; } \u7c7b\u578b\u522b\u540d\u4e5f\u53ef\u4ee5\u5e2e\u52a9 std::function \u63d0\u9ad8\u53ef\u8bfb\u6027\uff1a 1 2 using ValidateFunctionRaw = bool ( * )( int , int ); // type alias to raw function pointer using ValidateFunction = std :: function < bool ( int , int ) > ; // type alias to std::function \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c std::function \u53ea\u5141\u8bb8\u901a\u8fc7\u9690\u5f0f\u89e3\u5f15\u7528(e.g. fcnPtr() )\u8c03\u7528\u51fd\u6570\uff0c\u800c\u4e0d\u652f\u6301\u663e\u793a\u89e3\u5f15\u7528\u8c03\u7528\u51fd\u6570 (e.g. (*fcnPtr)() )\u3002 C++17 \u4e2d \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD \u53ef\u4ee5\u7528\u6765\u4ece\u521d\u59cb\u5316\u503c\u76f4\u63a5\u63a8\u65ad std::function \u7684\u7c7b\u578b\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5199 std::function fcnPtr{ &foo }; \u800c\u4e0d\u9700\u8981\u5199 std::function fcnPtr{ &foo }; \uff0c\u8ba9\u7f16\u8bd1\u5668\u81ea\u5df1\u63a8\u65ad\u6a21\u677f\u53c2\u6570\u3002\u4f46\u662f CTAD \u5e76\u4e0d\u80fd\u5728\u5b9a\u4e49\u7c7b\u578b\u522b\u540d\u65f6\u4f7f\u7528\uff0c\u56e0\u4e3a\u6b64\u65f6\u6211\u4eec\u5e76\u6ca1\u6709\u63d0\u4f9b\u521d\u59cb\u5316\u503c\u3002 \u51fd\u6570\u6307\u9488\u7684\u7c7b\u578b\u63a8\u65ad auto \u5173\u952e\u5b57\u53ef\u4ee5\u63a8\u65ad\u666e\u901a\u53d8\u91cf\u7c7b\u578b\uff0c auto \u5173\u952e\u5b57\u4e5f\u53ef\u4ee5\u63a8\u65ad\u51fd\u6570\u6307\u9488\u7c7b\u578b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int foo ( int x ) { return x ; } int main () { auto fcnPtr { & foo }; std :: cout << fcnPtr ( 5 ) << '\\n' ; return 0 ; } \u8fd9\u5b8c\u5168\u7b26\u5408\u60a8\u7684\u9884\u671f\uff0c\u800c\u4e14\u8bed\u6cd5\u975e\u5e38\u7b80\u6d01\u3002\u5f53\u7136\uff0c\u7f3a\u70b9\u662f\u5173\u4e8e\u51fd\u6570\u7684\u5f62\u53c2\u7c7b\u578b\u548c\u8fd4\u56de\u7c7b\u578b\u7684\u6240\u6709\u7ec6\u8282\u90fd\u662f\u9690\u85cf\u7684\uff0c\u56e0\u6b64\u5728\u8c03\u7528\u51fd\u6570\u6216\u4f7f\u7528\u5176\u8fd4\u56de\u503c\u65f6\u5f88\u5bb9\u6613\u51fa\u9519\u3002 \u5c0f\u7ed3 \u5982\u679c\u4f60\u5e0c\u671b\u5c06\u51fd\u6570\u5b58\u50a8\u5728\u6570\u7ec4(\u6216\u5176\u4ed6\u7ed3\u6784)\u4e2d\uff0c\u6216\u8005\u9700\u8981\u5c06\u51fd\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u65f6\uff0c\u51fd\u6570\u6307\u9488\u975e\u5e38\u6709\u7528\u3002\u56e0\u4e3a\u58f0\u660e\u51fd\u6570\u6307\u9488\u7684\u8bed\u6cd5\u5f88\u96be\u770b\u800c\u4e14\u5bb9\u6613\u51fa\u9519\uff0c\u6211\u4eec\u5efa\u8bae\u4f7f\u7528 std::function \u3002\u5728\u51fd\u6570\u6307\u9488\u7c7b\u578b\u53ea\u4f7f\u7528\u4e00\u6b21\u7684\u5730\u65b9(\u4f8b\u5982\u5355\u4e2a\u5f62\u53c2\u6216\u8fd4\u56de\u503c)\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 std::function \u3002\u5728\u591a\u6b21\u4f7f\u7528\u51fd\u6570\u6307\u9488\u7c7b\u578b\u7684\u5730\u65b9\uff0c\u521b\u5efa std::function \u7684\u7c7b\u578b\u522b\u540d\u5219\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002","title":"12.1 - \u51fd\u6570\u6307\u9488"},{"location":"12-1-function-pointers/#_1","text":"\u521b\u5efa\u975econst\u51fd\u6570\u6307\u9488\u7684\u8bed\u6cd5\u662fC++\u4e2d\u6700\u4e11\u964b\u7684\u8bed\u6cd5\u4e4b\u4e00\uff1a 1 2 // fcnPtr is a pointer to a function that takes no arguments and returns an integer int ( * fcnPtr )(); \u4e0a\u9762\u4ee3\u7801\u4e2d\u7684 fcnPtr \u4e0a\u4e00\u4e2a\u51fd\u6570\u6307\u9488\uff0c\u5b83\u6ca1\u6709\u5f62\u53c2\u4e14\u8fd4\u56de\u6574\u578b\u3002 fcnPtr \u53ef\u4ee5\u6267\u884c\u4efb\u4f55\u8be5\u7c7b\u578b\u7684\u51fd\u6570\u3002 *fcnPtr \u4e24\u8fb9\u7684\u62ec\u53f7\u662f\u5fc5\u987b\u7684\uff0c\u5b83\u53ef\u4ee5\u786e\u4fdd\u4f18\u5148\u7ea7\u662f\u6b63\u786e\u7684\uff0c\u5426\u5219 int* fcnPtr() \u5c31\u4f1a\u88ab\u89e3\u6790\u4e3a\u540d\u4e3a fcnPtr \uff0c\u4e0d\u63a5\u53d7\u53c2\u6570\uff0c\u8fd4\u56de\u6574\u578b\u6307\u9488\u7684\u51fd\u6570\u7684\u524d\u5411\u58f0\u660e\u3002 \u521b\u5efaconst\u51fd\u6570\u6307\u9488\uff0c\u9700\u8981\u5c06const\u5173\u952e\u5b57\u6dfb\u52a0\u5728\u661f\u53f7\u540e\u9762\uff1a 1 int ( * const fcnPtr )(); \u5982\u679c\u628aconst\u653e\u5728int\u524d\u9762\uff0c\u5219\u8868\u793a\u51fd\u6570\u6307\u9488\u6307\u5411\u7684\u51fd\u6570\u8fd4\u56de\u4e00\u4e2aconst\u6574\u578b\u3002","title":"\u6307\u5411\u51fd\u6570\u7684\u6307\u9488"},{"location":"12-1-function-pointers/#_2","text":"\u51fd\u6570\u6307\u9488\u53ef\u4ee5\u7528\u51fd\u6570\u521d\u59cb\u5316(\u975econst\u51fd\u6570\u6307\u9488\u53ef\u4ee5\u88ab\u8d4b\u503c\u4e3a\u51fd\u6570)\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u76f4\u63a5\u4f7f\u7528\u4e86 foo \uff0c\u5e76\u4e14\u5b83\u5df2\u7ecf\u88ab\u8f6c\u6362\u4e3a\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u3002\u4e0e\u6307\u5411\u53d8\u91cf\u7684\u6307\u9488\u4e00\u6837\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528 &foo \u6765\u83b7\u5f97\u6307\u5411 foo \u7684\u51fd\u6570\u6307\u9488\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 int foo () { return 5 ; } int goo () { return 6 ; } int main () { int ( * fcnPtr )(){ & foo }; // fcnPtr points to function foo fcnPtr = & goo ; // fcnPtr now points to function goo return 0 ; } \u4e0b\u9762\u4ee3\u7801\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u9519\u8bef\uff1a 1 fcnPtr = goo (); \u8be5\u884c\u4ee3\u7801\u5c06 goo() \u7684\u8fd4\u56de\u503c(\u7c7b\u578b\u4e3a int ) \u8d4b\u503c\u7ed9 fcnPtr (\u5b9e\u9645\u671f\u671b\u7684\u7c7b\u578b\u662f int(*)() )\uff0c\u8fd9\u5e76\u4e0d\u662f\u6211\u4eec\u7684\u672c\u610f\u3002\u6211\u4eec\u5176\u5b9e\u5e0c\u671b fcnPtr \u88ab\u8d4b\u503c\u4e3a goo \u7684\u5730\u5740\uff0c\u800c\u4e0d\u662f goo() \u7684 So no parentheses are needed. \u6ce8\u610f\uff0c\u51fd\u6570\u6307\u9488\u7684\u7c7b\u578b\uff08\u8fd4\u56de\u503c\u548c\u53c2\u6570\uff09\u5fc5\u987b\u548c\u51fd\u6570\u7c7b\u578b\u5339\u914d\uff0c\u4e00\u4e9b\u5b9e\u4f8b\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 // function prototypes int foo (); double goo (); int hoo ( int x ); // function pointer assignments int ( * fcnPtr1 )(){ & foo }; // \u5bf9 int ( * fcnPtr2 )(){ & goo }; // \u9519 -- \u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u6b63\u786e double ( * fcnPtr4 )(){ & goo }; // \u5bf9 fcnPtr1 = & hoo ; // \u9519 -- fcnPtr1 \u4e0d\u63a5\u53d7\u53c2\u6570\uff0c\u548c hoo() \u4e0d\u5339\u914d int ( * fcnPtr3 )( int ){ & hoo }; // \u5bf9 \u4e0e\u57fa\u672c\u7c7b\u578b\u4e0d\u540c\uff0cc++ \u4f1a\u5728\u9700\u8981\u65f6\u9690\u5f0f\u5730\u5c06\u51fd\u6570\u8f6c\u6362\u4e3a\u51fd\u6570\u6307\u9488(\u56e0\u6b64\u4f60==\u4e0d\u9700\u8981\u4f7f\u7528address-of\u64cd\u4f5c\u7b26(&)\u6765\u83b7\u53d6\u51fd\u6570\u7684\u5730\u5740==)\u3002\u4f46\u662f\uff0c\u5b83\u4e0d\u4f1a\u9690\u5f0f\u5730\u5c06\u51fd\u6570\u6307\u9488\u8f6c\u6362\u4e3a\u7a7a\u6307\u9488\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002 \u51fd\u6570\u6307\u9488\u53ef\u4ee5\u88ab\u521d\u59cb\u5316\u6216\u8d4b\u503c\u4e3a nullptr \uff1a 1 int ( * fcnptr )() { nullptr }; // \u5bf9","title":"\u5c06\u51fd\u6570\u8d4b\u503c\u7ed9\u51fd\u6570\u6307\u9488"},{"location":"12-1-function-pointers/#_3","text":"\u51fd\u6570\u6307\u9488\u7684\u53e6\u4e00\u4e2a\u4e3b\u8981\u529f\u80fd\u662f\u4f7f\u7528\u5b83\u6765\u8c03\u7528\u51fd\u6570\u3002\u901a\u8fc7\u51fd\u6570\u6307\u9488\u8c03\u7528\u51fd\u6570\u6709\u4e24\u4e2a\u65b9\u6cd5\uff0c\u4e00\u662f\u901a\u8fc7\u663e\u5f0f\u89e3\u5f15\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int foo ( int x ) { return x ; } int main () { int ( * fcnPtr )( int ){ & foo }; // Initialize fcnPtr with function foo ( * fcnPtr )( 5 ); // call function foo(5) through fcnPtr. return 0 ; } \u7b2c\u4e8c\u79cd\u65b9\u6cd5\u662f\u901a\u8fc7\u9690\u5f0f\u89e3\u5f15\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int foo ( int x ) { return x ; } int main () { int ( * fcnPtr )( int ){ & foo }; // Initialize fcnPtr with function foo fcnPtr ( 5 ); // call function foo(5) through fcnPtr. return 0 ; } \u5982\u4f60\u6240\u89c1\uff0c\u4f7f\u7528\u9690\u5f0f\u89e3\u5f15\u7528\u8c03\u7528\u51fd\u6570\u770b\u8d77\u6765\u548c\u6b63\u5e38\u7684\u51fd\u6570\u8c03\u7528\u4e00\u6a21\u4e00\u6837\uff0c\u56e0\u4e3a\u51fd\u6570\u540d\u672c\u6765\u5c31\u662f\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\uff01\u4f46\u662f\uff0c\u6709\u4e9b\u65e7\u7684\u7f16\u8bd1\u5668\u5e76\u4e0d\u652f\u6301\u8fd9\u79cd\u9690\u5f0f\u89e3\u5f15\u7528\u7684\u65b9\u5f0f\uff0c\u4f46\u662f\u73b0\u4ee3\u7f16\u8bd1\u5668\u90fd\u652f\u6301\u3002 \u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a\u9ed8\u8ba4\u5f62\u53c2\u4e0d\u9002\u7528\u4e8e\u901a\u8fc7\u51fd\u6570\u6307\u9488\u8c03\u7528\u7684\u51fd\u6570\u3002\u9ed8\u8ba4\u5f62\u53c2\u5728\u7f16\u8bd1\u65f6\u89e3\u6790(\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u4e3a\u9ed8\u8ba4\u5f62\u53c2\u63d0\u4f9b\u5b9e\u53c2\uff0c\u7f16\u8bd1\u5668\u5c06\u5728\u7f16\u8bd1\u4ee3\u7801\u65f6\u4e3a\u60a8\u63d0\u4f9b\u4e00\u4e2a\u5b9e\u53c2)\u3002\u4f46\u662f\uff0c\u51fd\u6570\u6307\u9488\u5728\u8fd0\u884c\u65f6\u89e3\u6790\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528\u51fd\u6570\u6307\u9488\u8fdb\u884c\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u65e0\u6cd5\u89e3\u6790\u9ed8\u8ba4\u5f62\u53c2\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5fc5\u987b\u663e\u5f0f\u5730\u4f20\u5165\u4efb\u4f55\u9ed8\u8ba4\u53c2\u6570\u7684\u503c\u3002 \u8fd8\u8981\u6ce8\u610f\uff0c\u56e0\u4e3a\u51fd\u6570\u6307\u9488\u53ef\u4ee5\u8bbe\u7f6e\u4e3anullptr\uff0c\u6240\u4ee5\u5728\u8c03\u7528\u51fd\u6570\u6307\u9488\u4e4b\u524d\uff0c\u6700\u597d\u901a\u8fc7\u65ad\u8a00\u6216\u6709\u6761\u4ef6\u6d4b\u8bd5\u5224\u65ad\u5b83\u662f\u5426\u662f\u7a7a\u6307\u9488\u3002\u5c31\u50cf\u666e\u901a\u6307\u9488\u4e00\u6837\uff0c\u5bf9\u7a7a\u51fd\u6570\u6307\u9488\u7684\u89e3\u5f15\u7528\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 int foo ( int x ) { return x ; } int main () { int ( * fcnPtr )( int ){ & foo }; // Initialize fcnPtr with function foo if ( fcnPtr ) // make sure fcnPtr isn't a null pointer fcnPtr ( 5 ); // otherwise this will lead to undefined behavior return 0 ; }","title":"\u4f7f\u7528\u51fd\u6570\u6307\u9488\u8c03\u7528\u51fd\u6570"},{"location":"12-1-function-pointers/#_4","text":"\u51fd\u6570\u6307\u9488\u6700\u5927\u7684\u7528\u9014\u5176\u5b9e\u662f\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\u3002\u8fd9\u79cd\u4f5c\u4e3a\u5b9e\u53c2\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\u7684\u51fd\u6570\uff0c\u79f0\u4e3a \u56de\u8c03\u51fd\u6570 \u3002 \u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u5047\u8bbe\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u6267\u884c\u4e00\u9879\u4efb\u52a1(\u4f8b\u5982\u5bf9\u6570\u7ec4\u6392\u5e8f)\uff0c\u4f46\u4f60\u5e0c\u671b\u7528\u6237\u80fd\u591f\u5b9a\u4e49\u5982\u4f55\u6267\u884c\u8be5\u4efb\u52a1\u7684\u7279\u5b9a\u90e8\u5206(\u4f8b\u5982\u6570\u7ec4\u662f\u6309\u5347\u5e8f\u8fd8\u662f\u964d\u5e8f\u6392\u5e8f)\u3002\u8ba9\u6211\u4eec\u4ed4\u7ec6\u770b\u770b\u8fd9\u4e2a\u95ee\u9898\u662f\u5982\u4f55\u5177\u4f53\u5e94\u7528\u4e8e\u6392\u5e8f\u7684\uff0c\u4f5c\u4e3a\u4e00\u4e2a\u53ef\u4ee5\u63a8\u5e7f\u5230\u5176\u4ed6\u7c7b\u4f3c\u95ee\u9898\u7684\u4f8b\u5b50\u3002 \u8bb8\u591a\u57fa\u4e8e\u6bd4\u8f83\u7684\u6392\u5e8f\u7b97\u6cd5\u90fd\u57fa\u4e8e\u7c7b\u4f3c\u7684\u6982\u5ff5\uff1a\u6392\u5e8f\u7b97\u6cd5\u904d\u5386\u5217\u8868\uff0c\u5bf9\u6570\u5b57\u5bf9\u8fdb\u884c\u6bd4\u8f83\uff0c\u5e76\u57fa\u4e8e\u8fd9\u4e9b\u6bd4\u8f83\u7684\u7ed3\u679c\u5bf9\u6570\u5b57\u8fdb\u884c\u91cd\u65b0\u6392\u5e8f\u3002\u56e0\u6b64\uff0c\u901a\u8fc7\u6539\u53d8\u6bd4\u8f83\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6539\u53d8\u7b97\u6cd5\u7684\u6392\u5e8f\u65b9\u5f0f\uff0c\u800c\u4e0d\u5f71\u54cd\u5176\u4ed6\u6392\u5e8f\u4ee3\u7801\u3002 \u4e0b\u9762\u662f\u4e4b\u524d\u7684\u9009\u62e9\u6392\u5e8f\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // for std::swap void SelectionSort ( int * array , int size ) { // Step through each element of the array for ( int startIndex { 0 }; startIndex < ( size - 1 ); ++ startIndex ) { // smallestIndex is the index of the smallest element we've encountered so far. int smallestIndex { startIndex }; // Look for smallest element remaining in the array (starting at startIndex+1) for ( int currentIndex { startIndex + 1 }; currentIndex < size ; ++ currentIndex ) { // If the current element is smaller than our previously found smallest if ( array [ smallestIndex ] > array [ currentIndex ]) // COMPARISON DONE HERE { // This is the new smallest number for this iteration smallestIndex = currentIndex ; } } // Swap our start element with our smallest element std :: swap ( array [ startIndex ], array [ smallestIndex ]); } } \u8ba9\u6211\u4eec\u7528\u4e00\u4e2a\u51fd\u6570\u6765\u66ff\u6362\u8fd9\u91cc\u7684\u6bd4\u8f83\u3002\u6bd4\u8f83\u51fd\u6570\u6bd4\u8f83\u4e24\u4e2a\u6574\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\u6765\u6307\u793a\u5143\u7d20\u662f\u5426\u5e94\u8be5\u4ea4\u6362\uff0c\u5b83\u770b\u8d77\u6765\u50cf\u8fd9\u6837: 1 2 3 4 bool ascending ( int x , int y ) { return x > y ; // swap if the first element is greater than the second } \u4f7f\u7528 ascending() \u51fd\u6570\u4fee\u6539\u540e\u7684\u6392\u5e8f\u7b97\u6cd5\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // for std::swap void SelectionSort ( int * array , int size ) { // Step through each element of the array for ( int startIndex { 0 }; startIndex < ( size - 1 ); ++ startIndex ) { // smallestIndex is the index of the smallest element we've encountered so far. int smallestIndex { startIndex }; // Look for smallest element remaining in the array (starting at startIndex+1) for ( int currentIndex { startIndex + 1 }; currentIndex < size ; ++ currentIndex ) { // If the current element is smaller than our previously found smallest if ( ascending ( array [ smallestIndex ], array [ currentIndex ])) // COMPARISON DONE HERE { // This is the new smallest number for this iteration smallestIndex = currentIndex ; } } // Swap our start element with our smallest element std :: swap ( array [ startIndex ], array [ smallestIndex ]); } } \u73b0\u5728\uff0c\u4e3a\u4e86\u8ba9\u8c03\u7528\u8005\u51b3\u5b9a\u5982\u4f55\u8fdb\u884c\u6392\u5e8f\uff0c\u6211\u4eec\u4e0d\u4f7f\u7528\u56fa\u5b9a\u7684\u6bd4\u8f83\u51fd\u6570\uff0c\u800c\u662f\u5141\u8bb8\u8c03\u7528\u8005\u63d0\u4f9b\u4ed6\u4eec\u81ea\u5df1\u7684\u6392\u5e8f\u51fd\u6570\uff01\u8fd9\u662f\u901a\u8fc7\u51fd\u6570\u6307\u9488\u5b8c\u6210\u7684\u3002 \u56e0\u4e3a\u8c03\u7528\u8005\u7684\u6bd4\u8f83\u51fd\u6570\u5c06\u6bd4\u8f83\u4e24\u4e2a\u6574\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u6240\u4ee5\u6307\u5411\u8fd9\u6837\u4e00\u4e2a\u51fd\u6570\u7684\u6307\u9488\u770b\u8d77\u6765\u50cf\u8fd9\u6837: 1 bool ( * comparisonFcn )( int , int ); \u56e0\u6b64\uff0c\u6211\u4eec\u5c06\u5141\u8bb8\u8c03\u7528\u8005\u5c06\u4e00\u4e2a\u6307\u5411\u4ed6\u4eec\u60f3\u8981\u7684\u6bd4\u8f83\u51fd\u6570\u7684\u6307\u9488\u4f5c\u4e3a\u7b2c\u4e09\u4e2a\u5f62\u53c2\u4f20\u9012\u7ed9\u6392\u5e8f\u4f8b\u7a0b\uff0c\u7136\u540e\u6211\u4eec\u5c06\u4f7f\u7528\u8c03\u7528\u8005\u7684\u51fd\u6570\u8fdb\u884c\u6bd4\u8f83\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u51fd\u6570\u6307\u9488\u5f62\u53c2\u8fdb\u884c\u7528\u6237\u5b9a\u4e49\u6bd4\u8f83\u7684\u9009\u62e9\u6392\u5e8f\u7684\u5b8c\u6574\u793a\u4f8b\uff0c\u4ee5\u53ca\u5982\u4f55\u8c03\u7528\u5b83\u7684\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 #include // for std::swap #include // Note our user-defined comparison is the third parameter void selectionSort ( int * array , int size , bool ( * comparisonFcn )( int , int )) { // Step through each element of the array for ( int startIndex { 0 }; startIndex < ( size - 1 ); ++ startIndex ) { // bestIndex is the index of the smallest/largest element we've encountered so far. int bestIndex { startIndex }; // Look for smallest/largest element remaining in the array (starting at startIndex+1) for ( int currentIndex { startIndex + 1 }; currentIndex < size ; ++ currentIndex ) { // If the current element is smaller/larger than our previously found smallest if ( comparisonFcn ( array [ bestIndex ], array [ currentIndex ])) // COMPARISON DONE HERE { // This is the new smallest/largest number for this iteration bestIndex = currentIndex ; } } // Swap our start element with our smallest/largest element std :: swap ( array [ startIndex ], array [ bestIndex ]); } } // Here is a comparison function that sorts in ascending order // (Note: it's exactly the same as the previous ascending() function) bool ascending ( int x , int y ) { return x > y ; // swap if the first element is greater than the second } // Here is a comparison function that sorts in descending order bool descending ( int x , int y ) { return x < y ; // swap if the second element is greater than the first } // This function prints out the values in the array void printArray ( int * array , int size ) { for ( int index { 0 }; index < size ; ++ index ) { std :: cout << array [ index ] << ' ' ; } std :: cout << '\\n' ; } int main () { int array [ 9 ]{ 3 , 7 , 9 , 5 , 6 , 1 , 8 , 2 , 4 }; // Sort the array in descending order using the descending() function selectionSort ( array , 9 , descending ); printArray ( array , 9 ); // Sort the array in ascending order using the ascending() function selectionSort ( array , 9 , ascending ); printArray ( array , 9 ); return 0 ; } \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 \u9177\u4e0d\u9177\uff1f\u6211\u4eec\u7ed9\u4e86\u8c03\u7528\u8005\u63a7\u5236\u9009\u62e9\u6392\u5e8f\u5982\u4f55\u5de5\u4f5c\u7684\u80fd\u529b\u3002 \u8c03\u7528\u8005\u751a\u81f3\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u201c\u5947\u602a\u7684\u201d\u6bd4\u8f83\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 bool evensFirst ( int x , int y ) { // if x is even and y is odd, x goes first (no swap needed) if (( x % 2 == 0 ) && ! ( y % 2 == 0 )) return false ; // if x is odd and y is even, y goes first (swap needed) if ( ! ( x % 2 == 0 ) && ( y % 2 == 0 )) return true ; // otherwise sort in ascending order return ascending ( x , y ); } int main () { int array [ 9 ]{ 3 , 7 , 9 , 5 , 6 , 1 , 8 , 2 , 4 }; selectionSort ( array , 9 , evensFirst ); printArray ( array , 9 ); return 0 ; } \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 4 6 8 1 3 5 7 9 \u5982\u4f60\u6240\u89c1\uff0c\u5728\u6b64\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\u51fd\u6570\u6307\u9488\u662f\u4e00\u79cd\u5f88\u597d\u65b9\u5f0f\uff0c\u8c03\u7528\u8005\u53ef\u4ee5\u5c06\u81ea\u5df1\u7684\u529f\u80fd\u201c\u6302\u94a9\u201d\u5230\u4f60\u4ee5\u524d\u7f16\u5199\u548c\u6d4b\u8bd5\u8fc7\u7684\u5185\u5bb9\u4e2d\uff0c\u8fd9\u6709\u52a9\u4e8e\u4fc3\u8fdb\u4ee3\u7801\u91cd\u7528\uff01\u4ee5\u524d\uff0c\u5982\u679c\u5e0c\u671b\u6309\u964d\u5e8f\u6392\u5217\u4e00\u4e2a\u6570\u7ec4\uff0c\u6309\u5347\u5e8f\u6392\u5217\u53e6\u4e00\u4e2a\u6570\u7ec4\uff0c\u5219\u9700\u8981\u591a\u4e2a\u7248\u672c\u7684\u6392\u5e8f\u4f8b\u7a0b\u3002\u73b0\u5728\u4f60\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7248\u672c\u5e76\u6309\u8c03\u7528\u8005\u5e0c\u671b\u7684\u4efb\u4f55\u65b9\u5f0f\u6392\u5e8f\uff01 \u6ce8\u610f\uff1a \u5982\u679c\u51fd\u6570\u5f62\u53c2\u662f\u51fd\u6570\u7c7b\u578b\u7684\uff0c\u5b83\u5c06\u88ab\u8f6c\u6362\u4e3a\u6307\u5411\u51fd\u6570\u7c7b\u578b\u7684\u6307\u9488 \u3002\u8fd9\u610f\u5473\u7740\uff1a 1 void selectionSort ( int * array , int size , bool ( * comparisonFcn )( int , int )) \u7b49\u4ef7\u4e8e\uff1a 1 void selectionSort ( int * array , int size , bool comparisonFcn ( int , int )) \u8fd9\u53ea\u9002\u7528\u4e8e\u51fd\u6570\u5f62\u53c2\uff0c\u800c\u4e0d\u9002\u7528\u4e8e\u72ec\u7acb\u7684\u51fd\u6570\u6307\u9488\uff0c\u56e0\u6b64\u4f7f\u7528\u6bd4\u8f83\u6709\u9650\u3002","title":"\u5c06\u51fd\u6570\u4f5c\u4e3a\u5b9e\u53c2\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570"},{"location":"12-1-function-pointers/#_5","text":"\u5982\u679c\u4f60\u8981\u5141\u8bb8\u8c03\u7528\u65b9\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\uff0c\u90a3\u4e48\u4e3a\u8c03\u7528\u65b9\u63d0\u4f9b\u4e00\u4e9b\u6807\u51c6\u51fd\u6570\u4ee5\u65b9\u4fbf\u4f7f\u7528\u901a\u5e38\u662f\u5f88\u6709\u7528\u7684\u3002\u4f8b\u5982\uff0c\u5728\u4e0a\u9762\u7684\u9009\u62e9\u6392\u5e8f\u793a\u4f8b\u4e2d\uff0c\u63d0\u4f9b ascending() \u548c descending() \u51fd\u6570\u4ee5\u53ca selectionSort() \u51fd\u6570\u5c06\u4f7f\u8c03\u7528\u8005\u7684\u5de5\u4f5c\u66f4\u52a0\u8f7b\u677e\uff0c\u56e0\u4e3a\u4ed6\u4eec\u4e0d\u5fc5\u6bcf\u6b21\u60f3\u8981\u4f7f\u7528 ascending() \u548c descending() \u51fd\u6570\u65f6\u90fd\u91cd\u5199\u5b83\u4eec\u3002 \u4f60\u751a\u81f3\u53ef\u4ee5\u8bbe\u7f6e\u5176\u4e2d\u4e00\u4e2a\u4f5c\u4e3a\u9ed8\u8ba4\u53c2\u6570\uff1a 1 2 // Default the sort to ascending sort void selectionSort ( int * array , int size , bool ( * comparisonFcn )( int , int ) = ascending ); \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u7528\u6237\u6b63\u5e38\u8c03\u7528 selectionSort \uff08\u4e0d\u4f7f\u7528\u51fd\u6570\u6307\u9488\uff09\uff0c comparisonFcn \u5f62\u53c2 \u9ed8\u8ba4\u662f\u5347\u5e8f\u7684\u3002\u4f60\u5fc5\u987b\u786e\u4fdd ascending \u51fd\u6570\u5728\u6b64\u4e4b\u524d\u88ab\u5b9a\u4e49\uff0c\u5426\u5219\u7f16\u8bd1\u5668\u5c06\u4f1a\u62a5\u544a ascending \u627e\u4e0d\u5230\u3002","title":"\u63d0\u4f9b\u9ed8\u8ba4\u51fd\u6570"},{"location":"12-1-function-pointers/#_6","text":"\u5b9e\u8bdd\u5b9e\u8bf4\u2014\u2014\u51fd\u6570\u6307\u9488\u7684\u8bed\u6cd5\u7684\u786e\u5f88\u96be\u770b\u3002\u4e0d\u8fc7\uff0c \u7c7b\u578b\u522b\u540d \u53ef\u4ee5\u7528\u6765\u4f7f\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\u770b\u8d77\u6765\u66f4\u50cf\u5e38\u89c4\u53d8\u91cf \uff1a 1 using ValidateFunction = bool ( * )( int , int ); \u7c7b\u578b\u522b\u540d \u201cValidateFunction\u201d \u662f\u4e00\u4e2a\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\uff0c\u8be5\u6307\u9488\u63a5\u53d7\u4e24\u4e2a\u6574\u578b\u5f62\u53c2\u5e76\u8fd4\u56de\u4e00\u4e2abool\u7c7b\u578b\u503c\u3002 \u6b64\u65f6\u6211\u4eec\u5c31\u53ef\u4ee5\u4e0d\u8fd9\u6837\u505a\uff1a 1 bool validate ( int x , int y , bool ( * fcnPtr )( int , int )); // \u4e11\u964b \u4f60\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 bool validate ( int x , int y , ValidateFunction pfcn ) // \u4f18\u96c5","title":"\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u8ba9\u51fd\u6570\u6307\u9488\u770b\u8d77\u6765\u66f4\u4f18\u96c5"},{"location":"12-1-function-pointers/#stdfunction","text":"\u5b9a\u4e49\u548c\u5b58\u50a8\u51fd\u6570\u6307\u9488\u8fd8\u6709\u4e00\u4e2a\u529e\u6cd5\uff0c\u5373\u4f7f\u7528 std::function \uff0c\u5b83\u5b9a\u4e49\u5728\u6807\u51c6\u5e93 \u5934\u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u5c06\u51fd\u6570\u6307\u9488\u5b9a\u4e49\u4e3a\u4e00\u4e2a std::function \u5bf9\u8c61\uff1a 1 2 #include bool validate ( int x , int y , std :: function < bool ( int , int ) > fcn ); // std::function method that returns a bool and takes two int parameters \u5982\u4f60\u6240\u89c1\uff0c\u8fd4\u56de\u503c\u7c7b\u578b\u5b9a\u4e49\u5728\u5c16\u62ec\u53f7\u4e2d\uff0c\u53c2\u6570\u7c7b\u578b\u5219\u88ab\u5b9a\u4e49\u5728\u62ec\u53f7\u4e2d\u3002\u5982\u679c\u6ca1\u6709\u53c2\u6570\u7684\u8bdd\uff0c\u62ec\u53f7\u53ef\u4ee5\u7559\u7a7a\u3002 \u4f7f\u7528 std::function \u66f4\u65b0\u4e4b\u524d\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include int foo () { return 5 ; } int goo () { return 6 ; } int main () { std :: function < int () > fcnPtr { & foo }; // declare function pointer that returns an int and takes no parameters fcnPtr = & goo ; // fcnPtr now points to function goo std :: cout << fcnPtr () << '\\n' ; // call the function just like normal return 0 ; } \u7c7b\u578b\u522b\u540d\u4e5f\u53ef\u4ee5\u5e2e\u52a9 std::function \u63d0\u9ad8\u53ef\u8bfb\u6027\uff1a 1 2 using ValidateFunctionRaw = bool ( * )( int , int ); // type alias to raw function pointer using ValidateFunction = std :: function < bool ( int , int ) > ; // type alias to std::function \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c std::function \u53ea\u5141\u8bb8\u901a\u8fc7\u9690\u5f0f\u89e3\u5f15\u7528(e.g. fcnPtr() )\u8c03\u7528\u51fd\u6570\uff0c\u800c\u4e0d\u652f\u6301\u663e\u793a\u89e3\u5f15\u7528\u8c03\u7528\u51fd\u6570 (e.g. (*fcnPtr)() )\u3002 C++17 \u4e2d \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD \u53ef\u4ee5\u7528\u6765\u4ece\u521d\u59cb\u5316\u503c\u76f4\u63a5\u63a8\u65ad std::function \u7684\u7c7b\u578b\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5199 std::function fcnPtr{ &foo }; \u800c\u4e0d\u9700\u8981\u5199 std::function fcnPtr{ &foo }; \uff0c\u8ba9\u7f16\u8bd1\u5668\u81ea\u5df1\u63a8\u65ad\u6a21\u677f\u53c2\u6570\u3002\u4f46\u662f CTAD \u5e76\u4e0d\u80fd\u5728\u5b9a\u4e49\u7c7b\u578b\u522b\u540d\u65f6\u4f7f\u7528\uff0c\u56e0\u4e3a\u6b64\u65f6\u6211\u4eec\u5e76\u6ca1\u6709\u63d0\u4f9b\u521d\u59cb\u5316\u503c\u3002","title":"\u4f7f\u7528 std::function"},{"location":"12-1-function-pointers/#_7","text":"auto \u5173\u952e\u5b57\u53ef\u4ee5\u63a8\u65ad\u666e\u901a\u53d8\u91cf\u7c7b\u578b\uff0c auto \u5173\u952e\u5b57\u4e5f\u53ef\u4ee5\u63a8\u65ad\u51fd\u6570\u6307\u9488\u7c7b\u578b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int foo ( int x ) { return x ; } int main () { auto fcnPtr { & foo }; std :: cout << fcnPtr ( 5 ) << '\\n' ; return 0 ; } \u8fd9\u5b8c\u5168\u7b26\u5408\u60a8\u7684\u9884\u671f\uff0c\u800c\u4e14\u8bed\u6cd5\u975e\u5e38\u7b80\u6d01\u3002\u5f53\u7136\uff0c\u7f3a\u70b9\u662f\u5173\u4e8e\u51fd\u6570\u7684\u5f62\u53c2\u7c7b\u578b\u548c\u8fd4\u56de\u7c7b\u578b\u7684\u6240\u6709\u7ec6\u8282\u90fd\u662f\u9690\u85cf\u7684\uff0c\u56e0\u6b64\u5728\u8c03\u7528\u51fd\u6570\u6216\u4f7f\u7528\u5176\u8fd4\u56de\u503c\u65f6\u5f88\u5bb9\u6613\u51fa\u9519\u3002","title":"\u51fd\u6570\u6307\u9488\u7684\u7c7b\u578b\u63a8\u65ad"},{"location":"12-1-function-pointers/#_8","text":"\u5982\u679c\u4f60\u5e0c\u671b\u5c06\u51fd\u6570\u5b58\u50a8\u5728\u6570\u7ec4(\u6216\u5176\u4ed6\u7ed3\u6784)\u4e2d\uff0c\u6216\u8005\u9700\u8981\u5c06\u51fd\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u65f6\uff0c\u51fd\u6570\u6307\u9488\u975e\u5e38\u6709\u7528\u3002\u56e0\u4e3a\u58f0\u660e\u51fd\u6570\u6307\u9488\u7684\u8bed\u6cd5\u5f88\u96be\u770b\u800c\u4e14\u5bb9\u6613\u51fa\u9519\uff0c\u6211\u4eec\u5efa\u8bae\u4f7f\u7528 std::function \u3002\u5728\u51fd\u6570\u6307\u9488\u7c7b\u578b\u53ea\u4f7f\u7528\u4e00\u6b21\u7684\u5730\u65b9(\u4f8b\u5982\u5355\u4e2a\u5f62\u53c2\u6216\u8fd4\u56de\u503c)\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 std::function \u3002\u5728\u591a\u6b21\u4f7f\u7528\u51fd\u6570\u6307\u9488\u7c7b\u578b\u7684\u5730\u65b9\uff0c\u521b\u5efa std::function \u7684\u7c7b\u578b\u522b\u540d\u5219\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002","title":"\u5c0f\u7ed3"},{"location":"12-2-the-stack-and-the-heap/","tags":["stack","heap"],"text":"Key Takeaway \u4e00\u4e2a\u7a0b\u5e8f\u7684\u5185\u5b58\u53ef\u4ee5\u88ab\u5206\u4e3a\u51e0\u4e2a\u4e0d\u540c\u7684\u533a\u57df\uff0c\u79f0\u4e3a \u5185\u5b58\u6bb5 \uff1a \u4ee3\u7801\u6bb5(code\u6bb5) (\u4e5f\u79f0\u4e3a text \u6bb5)\uff0c\u7f16\u8bd1\u540e\u7684\u7a0b\u5e8f\u5c31\u4f4d\u4e8e\u8be5\u6bb5\u3002\u4ee3\u7801\u6bb5\u901a\u5e38\u662f\u53ea\u8bfb\u7684\uff1b bss\u6bb5 (\u4e5f\u79f0\u4e3a\u672a\u521d\u59cb\u5316\u6570\u636e\u6bb5)\uff0c\u8fd9\u91cc\u5b58\u653e 0\u521d\u59cb\u5316 \u7684 \u5168\u5c40\u53d8\u91cf \u548c \u9759\u6001\u53d8\u91cf \uff1b \u6570\u636e\u6bb5 (\u4e5f\u79f0\u4e3a\u521d\u59cb\u5316\u6570\u636e\u6bb5)\uff0c\u8fd9\u91cc\u5b58\u653e\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\u548c\u9759\u6001\u53d8\u91cf\uff1b \u5806 \uff1a\u52a8\u6001\u53d8\u91cf\u7684\u5185\u5b58\u662f\u4ece\u5806\u4e2d\u5206\u914d\u7684\uff1b \u6808 \uff1a \u5f62\u53c2 \u3001\u5c40\u90e8\u53d8\u91cf\u548c\u5176\u4ed6\u51fd\u6570\u76f8\u5173\u7684\u4fe1\u606f\u90fd\u5b58\u653e\u5728\u8fd9\u91cc\u3002 \u672c\u8282\u8bfe\uff0c\u6211\u4eec\u5c06\u4e3b\u8981\u5173\u6ce8\u5806\u548c\u6808\uff0c\u5b83\u4eec\u662f\u8fd9\u51e0\u4e2a\u5185\u5b58\u6bb5\u4e2d\u6700\u201d\u6709\u610f\u601d\u201c\u7684\u3002 \u5806\u5185\u5b58\u6bb5 \u5806\u6bb5(\u4e5f\u79f0\u4e3a\u201c\u81ea\u7531\u5b58\u50a8\u533a\u201d)\u8ddf\u8e2a\u7528\u4e8e\u52a8\u6001\u5185\u5b58\u5206\u914d\u7684\u5185\u5b58\u3002\u6211\u4eec\u5df2\u7ecf\u5728 11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d \u4e2d\u4ecb\u7ecd\u4e86\u5806\uff0c\u6240\u4ee5\u8fd9\u91cc\u53ea\u662f\u590d\u4e60\u4e00\u4e0b\uff1a \u5728 C++ \u4e2d\uff0c\u5f53\u4f60\u4f7f\u7528 new \u5206\u914d\u5185\u5b58\u65f6\uff0c\u8be5\u5185\u5b58\u5c31\u4f1a\u4f4d\u4e8e\u5806\u6bb5\u3002 1 2 int * ptr { new int }; // ptr is assigned 4 bytes in the heap int * array { new int [ 10 ] }; // array is assigned 40 bytes in the heap \u8be5\u5185\u5b58\u7684\u5730\u5740\u901a\u8fc7\u64cd\u4f5c\u7b26new\u8fd4\u56de\uff0c\u7136\u540e\u53ef\u4ee5\u5b58\u50a8\u5728\u6307\u9488\u4e2d\u3002\u4f60\u4e0d\u5fc5\u62c5\u5fc3\u5b9a\u4f4d\u7a7a\u95f2\u5185\u5b58\u5e76\u5c06\u5176\u5206\u914d\u7ed9\u7528\u6237\u7684\u5e95\u5c42\u673a\u5236\u3002\u7136\u800c\uff0c\u4f60\u9700\u8981\u77e5\u9053\u7684\u662f\uff0c\u8fde\u7eed\u5185\u5b58\u8bf7\u6c42\u53ef\u80fd\u4e0d\u4f1a\u5206\u914d\u8fde\u7eed\u7684\u5185\u5b58\u5730\u5740\uff01 1 2 3 int * ptr1 { new int }; int * ptr2 { new int }; // ptr1 and ptr2 may not have sequential addresses \u5f53\u52a8\u6001\u5206\u914d\u7684\u53d8\u91cf\u88ab\u5220\u9664\u65f6\uff0c\u5185\u5b58\u88ab\u201c\u8fd4\u56de\u201d\u5230\u5806\u4e2d\uff0c\u7136\u540e\u53ef\u4ee5\u5728\u63a5\u6536\u5230\u672a\u6765\u7684\u5206\u914d\u8bf7\u6c42\u65f6\u91cd\u65b0\u5206\u914d\u3002\u8bf7\u8bb0\u4f4f\uff0c\u5220\u9664\u6307\u9488\u5e76\u4e0d\u5220\u9664\u53d8\u91cf\uff0c\u5b83\u53ea\u662f\u5c06\u76f8\u5173\u5730\u5740\u7684\u5185\u5b58\u8fd4\u56de\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u3002 \u5806\u6709\u4f18\u70b9\u548c\u7f3a\u70b9\uff1a \u5728\u5806\u4e0a\u5206\u914d\u5185\u5b58\u76f8\u5bf9\u5f88\u6162\uff1b \u5df2\u5206\u914d\u7684\u5185\u5b58\u4e00\u76f4\u4fdd\u6301\u5206\u914d\u72b6\u6001\uff0c\u76f4\u5230\u5b83\u88ab\u7279\u522b\u5730\u91ca\u653e(\u6ce8\u610f\u5185\u5b58\u6cc4\u6f0f)\u6216\u5e94\u7528\u7a0b\u5e8f\u7ed3\u675f(\u8fd9\u65f6\u64cd\u4f5c\u7cfb\u7edf\u5e94\u8be5\u6e05\u7406\u5b83)\uff1b \u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u5fc5\u987b\u901a\u8fc7\u6307\u9488\u8bbf\u95ee\u3002\u89e3\u5f15\u7528\u6307\u9488\u6bd4\u76f4\u63a5\u8bbf\u95ee\u53d8\u91cf\u6bd4\u8f83\u6162\uff1b \u56e0\u4e3a\u5806\u662f\u4e00\u4e2a\u5f88\u5927\u7684\u5185\u5b58\u6c60\uff0c\u6240\u4ee5\u53ef\u4ee5\u5728\u8fd9\u91cc\u5206\u914d\u5927\u578b\u6570\u7ec4\u3001\u7ed3\u6784\u6216\u7c7b\u3002 \u8c03\u7528\u6808 \u8c03\u7528\u6808 \u6240\u626e\u6f14\u7684\u89d2\u8272\u66f4\u52a0\u6709\u610f\u601d\u3002\u8c03\u7528\u6808\u4f1a\u8ffd\u8e2a\u6240\u6709\u6d3b\u52a8\u51fd\u6570\uff08\u5df2\u7ecf\u88ab\u8c03\u7528\u4f46\u5c1a\u672a\u7ed3\u675f\uff09\u4ece\u5f00\u59cb\u5230\u5f53\u524d\u65f6\u95f4\u70b9\u7684\u4fe1\u606f\uff0c\u5e76\u4e14\u8d1f\u8d23\u4e3a\u51fd\u6570 \u5f62\u53c2 \u548c\u5c40\u90e8\u53d8\u91cf\u5206\u914d\u5185\u5b58\u3002 \u8c03\u7528\u6808\u4f7f\u7528\u6570\u636e\u7ed3\u6784\u2014\u2014\u6808\u5b9e\u73b0\u3002\u6240\u4ee5\u5728\u8ba8\u8bba\u8c03\u7528\u6808\u7684\u5de5\u4f5c\u539f\u7406\u4e4b\u524d\uff0c\u6211\u4eec\u5fc5\u987b\u8981\u6307\u5b9a\u6808\u662f\u4e00\u79cd\u4ec0\u4e48\u6837\u7684\u6570\u636e\u7ed3\u6784\u3002 \u6570\u636e\u7ed3\u6784\u2014\u2014\u6808 \u6570\u636e\u7ed3\u6784 \u662f\u4e00\u79cd\u7ec4\u7ec7\u6570\u636e\u4ee5\u4fbf\u6709\u6548\u5730\u4f7f\u7528\u6570\u636e\u7684\u7f16\u7a0b\u673a\u5236\uff0c\u4f60\u5df2\u7ecf\u4e86\u89e3\u4e86\u51e0\u79cd\u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u6570\u7ec4\u548c\u7ed3\u6784\u4f53\u3002\u8fd9\u4e24\u79cd\u6570\u636e\u7ed3\u6784\u90fd\u63d0\u4f9b\u4e86\u6709\u6548\u5730\u5b58\u50a8\u6570\u636e\u548c\u8bbf\u95ee\u6570\u636e\u7684\u673a\u5236\u3002\u8fd8\u6709\u8bb8\u591a\u5728\u7f16\u7a0b\u4e2d\u7ecf\u5e38\u4f7f\u7528\u7684\u9644\u52a0\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u76f8\u5f53\u4e00\u90e8\u5206\u662f\u5728\u6807\u51c6\u5e93\u4e2d\u5b9e\u73b0\u7684\uff0c\u6808\u5c31\u662f\u5176\u4e2d\u4e4b\u4e00\u3002 \u60f3\u60f3\u81ea\u52a9\u9910\u5385\u91cc\u7684\u4e00\u53e0\u76d8\u5b50\u3002\u56e0\u4e3a\u6bcf\u4e2a\u76d8\u5b50\u90fd\u5f88\u91cd\uff0c\u800c\u4e14\u5b83\u4eec\u662f\u5806\u53e0\u7684\uff0c\u6240\u4ee5\u4f60\u5b9e\u9645\u4e0a\u53ea\u80fd\u505a\u4e09\u4ef6\u4e8b\u4e2d\u7684\u4e00\u4ef6: \u770b\u5230\u6700\u9876\u5c42\u76d8\u5b50\u7684\u8868\u9762\uff1b \u62ff\u8d70\u6700\u4e0a\u9762\u4e00\u4e2a\u76d8\u5b50\uff08\u6f0f\u51fa\u4e0b\u9762\u4e00\u4e2a\u76d8\u5b50\uff09\uff1b \u5c06\u4e00\u4e2a\u65b0\u7684\u76d8\u5b50\u653e\u5728\u6700\u4e0a\u9762\uff08\u906e\u853d\u4e0b\u9762\u4e00\u4e2a\u76d8\u5b50\uff09\u3002 \u5728\u8ba1\u7b97\u673a\u7f16\u7a0b\u4e2d\uff0c\u6808\u662f\u4e00\u79cd\u5bb9\u7eb3\u591a\u4e2a\u53d8\u91cf\u7684\u5bb9\u5668\u6570\u636e\u7ed3\u6784(\u5f88\u50cf\u6570\u7ec4)\u3002\u7136\u800c\uff0c\u6570\u7ec4\u5141\u8bb8\u4f60\u6309\u4efb\u4f55\u987a\u5e8f\u8bbf\u95ee\u548c\u4fee\u6539\u5143\u7d20(\u79f0\u4e3a \u968f\u673a\u8bbf\u95ee )\uff0c\u800c\u6808\u5219\u6709\u66f4\u591a\u7684\u9650\u5236\u3002\u53ef\u4ee5\u5728\u5806\u6808\u4e0a\u6267\u884c\u7684\u64cd\u4f5c\u5bf9\u5e94\u4e8e\u4e0a\u9762\u63d0\u5230\u7684\u4e09\u4ef6\u4e8b\uff1a \u67e5\u770b\u6808\u9876\u5143\u7d20(\u901a\u5e38\u901a\u8fc7 top() \u51fd\u6570\u6216 peek() )\uff1b \u83b7\u53d6\u5e76\u79fb\u9664\u6808\u9876\u5143\u7d20(\u901a\u8fc7 pop() \u5b8c\u6210)\uff1b \u5c06\u4e00\u4e2a\u65b0\u5143\u7d20\u653e\u7f6e\u5728\u6808\u9876(\u901a\u8fc7 push() \u5b8c\u6210)\u3002 \u6808\u662f\u4e00\u4e2a\u540e\u5165\u5148\u51fa(LIFO)\u7684\u6570\u636e\u7ed3\u6784\u3002\u6700\u540e\u88ab\u538b\u5165\u6808\u7684\u5143\u7d20\u4f1a\u7b2c\u4e00\u4e2a\u88ab\u5f39\u51fa\u3002\u5982\u679c\u4f60\u5c06\u4e00\u4e2a\u65b0\u7684\u76d8\u5b50\u653e\u5728\u76d8\u5b50\u5806\u9876\u4e0a\u7684\u8bdd\uff0c\u90a3\u4f60\u7b2c\u4e00\u4e2a\u62ff\u8d70\u7684\u76d8\u5b50\u4e5f\u53ea\u80fd\u662f\u5b83\u3002\u5f53\u65b0\u7684\u5143\u7d20\u88ab\u538b\u5165\u6808\u65f6\uff0c\u6808\u5c31\u4f1a\u589e\u957f\u3002\u5f53\u5143\u7d20\u88ab\u79fb\u9664\u65f6\uff0c\u5219\u6808\u4f1a\u6536\u7f29\u3002 \u4f8b\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u77ed\u7684\u5e8f\u5217\uff0c\u5c55\u793a\u4e86\u6808\u4e0a\u7684push\u548cpop\u662f\u5982\u4f55\u5de5\u4f5c\u7684: 1 2 3 4 5 6 7 8 9 10 11 Stack: empty Push 1 Stack: 1 Push 2 Stack: 1 2 Push 3 Stack: 1 2 3 Pop Stack: 1 2 Pop Stack: 1 \u5bf9\u4e8e\u8c03\u7528\u6808\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u76d8\u5b50\u7c7b\u6bd4\u662f\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u7c7b\u6bd4\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u505a\u4e00\u4e2a\u66f4\u597d\u7684\u7c7b\u6bd4\u3002\u8003\u8651\u4e00\u5806\u90ae\u7bb1\uff0c\u5b83\u4eec\u90fd\u5806\u53e0\u5728\u4e00\u8d77\u3002\u6bcf\u4e2a\u90ae\u7bb1\u53ea\u80fd\u88c5\u4e00\u4ef6\u7269\u54c1\uff0c\u800c\u4e14\u6240\u6709\u90ae\u7bb1\u90fd\u662f\u7a7a\u7684\u3002\u6b64\u5916\uff0c\u6bcf\u4e2a\u90ae\u7bb1\u90fd\u56fa\u5b9a\u5728\u5b83\u4e0b\u9762\u7684\u90ae\u7bb1\u4e0a\uff0c\u56e0\u6b64\u90ae\u7bb1\u7684\u6570\u91cf\u4e0d\u80fd\u66f4\u6539\u3002\u5982\u679c\u4e0d\u80fd\u66f4\u6539\u90ae\u7bb1\u7684\u6570\u91cf\uff0c\u5982\u4f55\u83b7\u5f97\u7c7b\u4f3c\u4e8e\u5806\u6808\u7684\u884c\u4e3a? \u9996\u5148\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u6807\u8bb0(\u6bd4\u5982\u4fbf\u5229\u8d34)\u6765\u8ddf\u8e2a\u6700\u4e0b\u9762\u7684\u7a7a\u90ae\u7bb1\u7684\u4f4d\u7f6e\u3002\u5728\u5f00\u59cb\u65f6\uff0c\u8fd9\u5c06\u662f\u6700\u4f4e\u7684\u90ae\u7bb1(\u5728\u5806\u6808\u7684\u5e95\u90e8)\u3002\u5f53\u6211\u4eec\u5c06\u4e00\u4e2a\u9879\u76ee\u63a8\u5165\u90ae\u7bb1\u5806\u6808\u65f6\uff0c\u6211\u4eec\u5c06\u5b83\u653e\u5165\u6709\u6807\u8bb0\u7684\u90ae\u7bb1\u4e2d(\u5373\u7b2c\u4e00\u4e2a\u7a7a\u90ae\u7bb1)\uff0c\u5e76\u5c06\u6807\u8bb0\u5411\u4e0a\u79fb\u52a8\u4e00\u4e2a\u90ae\u7bb1\u3002\u5f53\u6211\u4eec\u4ece\u5806\u6808\u4e2d\u5f39\u51fa\u4e00\u4e2a\u9879\u76ee\u65f6\uff0c\u6211\u4eec\u5c06\u6807\u8bb0\u5411\u4e0b\u79fb\u52a8\u4e00\u4e2a\u90ae\u7bb1(\u56e0\u6b64\u5b83\u6307\u5411\u9876\u90e8\u7684\u975e\u7a7a\u90ae\u7bb1)\uff0c\u5e76\u4ece\u8be5\u90ae\u7bb1\u4e2d\u5220\u9664\u8be5\u9879\u76ee\u3002\u4efb\u4f55\u4f4e\u4e8e\u6807\u8bb0\u7684\u4e1c\u897f\u90fd\u88ab\u8ba4\u4e3a\u662f\u201c\u5728\u6808\u4e0a\u201d\u3002\u6807\u8bb0\u5904\u6216\u6807\u8bb0\u4e0a\u65b9\u7684\u4efb\u4f55\u4e1c\u897f\u90fd\u4e0d\u5728\u6808\u4e0a\u3002 \u8c03\u7528\u6808\u5185\u5b58\u6bb5 \u8c03\u7528\u6808\u5185\u5b58\u6bb5\u4fdd\u5b58\u7528\u4e8e\u8c03\u7528\u6808\u7684\u5185\u5b58\u3002\u5f53\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\uff0c main() \u51fd\u6570\u88ab\u64cd\u4f5c\u7cfb\u7edf\u538b\u5230\u8c03\u7528\u6808\u4e0a\u3002\u7136\u540e\u7a0b\u5e8f\u5f00\u59cb\u6267\u884c\u3002 \u51fd\u6570\u8c03\u7528\u65f6\u5c06\u51fd\u6570\u538b\u5165\u8c03\u7528\u6808\u3002\u5f53\u5f53\u524d\u51fd\u6570\u7ed3\u675f\u65f6\uff0c\u8be5\u51fd\u6570\u4ece\u8c03\u7528\u6808\u4e2d\u5f39\u51fa\u3002\u56e0\u6b64\uff0c\u901a\u8fc7\u67e5\u770b\u538b\u5165\u8c03\u7528\u6808\u4e0a\u7684\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e3a\u5230\u8fbe\u5f53\u524d\u6267\u884c\u70b9\u800c\u8c03\u7528\u7684\u6240\u6709\u51fd\u6570\u3002 \u90ae\u7bb1\u7684\u6bd4\u55bb\u5f88\u597d\u5730\u63cf\u8ff0\u4e86\u8c03\u7528\u6808\u7684\u5de5\u4f5c\u539f\u7406\u3002\u6808\u672c\u8eab\u662f\u4e00\u6bb5\u56fa\u5b9a\u957f\u5ea6\u7684\u5185\u5b58\u3002\u90ae\u7bb1\u53ef\u4ee5\u88ab\u770b\u505a\u662f\u5185\u5b58\u5730\u5740\uff0c\u800c\u538b\u5165\u6216\u5f39\u51fa\u7684\u5185\u5bb9\u79f0\u4e3a \u6808\u5e27 \u3002\u6808\u5e27\u8ffd\u8e2a\u548c\u67d0\u4e2a\u51fd\u6570\u8c03\u7528\u76f8\u5173\u7684\u5168\u90e8\u6570\u636e\u3002\u6bd4\u55bb\u4e2d\u7684\u201c\u6807\u8bb0\u201d\u662f\u4e00\u4e2a\u88ab\u79f0\u4e3a\u5806\u6808\u6307\u9488(SP)\u7684\u5bc4\u5b58\u5668(CPU\u4e2d\u7684\u4e00\u5c0f\u5757\u5185\u5b58)\u3002\u5806\u6808\u6307\u9488\u7528\u4e8e\u8ffd\u8e2a\u5f53\u524d\u7684\u6808\u9876\u4f4d\u7f6e\u3002 \u6211\u4eec\u8fd8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\uff1a\u5f53\u4ece\u8c03\u7528\u6808\u4e2d\u5f39\u51fa\u5185\u5bb9\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5e76\u4e0d\u9700\u8981\u5b9e\u9645\u6e05\u7406\u6216\u6e05\u96f6\u88ab\u5f39\u51fa\u7684\u6808\u5e27\u2014\u2014\u53ea\u9700\u8981\u5c06\u6808\u9876\u6307\u9488\u5411\u4e0b\u79fb\u52a8\u5373\u53ef\u3002\u76f8\u5e94\u7684\u5185\u5bb9\u5df2\u7ecf\u4e0d\u88ab\u8ba4\u4e3a\u662f\u6808\u7684\u4e00\u90e8\u5206\u4e86\uff08\u6808\u6307\u9488\u4f1a\u5728\u8be5\u5730\u5740\u6216\u8be5\u5730\u5740\u7684\u4e0b\u65b9\uff09\uff0c\u6240\u4ee5\u5b83\u4e5f\u4e0d\u4f1a\u88ab\u5bfb\u5740\u3002\u5982\u679c\u7a0d\u540e\u6211\u4eec\u5c06\u65b0\u7684\u6808\u5e27\u538b\u5165\u4e86\u8be5\u5185\u5b58\u5730\u5740\uff0c\u5219\u5176\u5185\u5bb9\u6b63\u597d\u5c31\u88ab\u8986\u76d6\u6389\u4e86\u3002 \u8c03\u7528\u6808\u5b9e\u4f8b \u8ba9\u6211\u4eec\u66f4\u8be6\u7ec6\u5730\u7814\u7a76\u8c03\u7528\u6808\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u4e0b\u9762\u662f\u8c03\u7528\u51fd\u6570\u65f6\u7684\u6b65\u9aa4\uff1a \u7a0b\u5e8f\u4e2d\u53d1\u751f\u51fd\u6570\u8c03\u7528\uff1b \u6808\u5e27\u88ab\u6784\u5efa\u5e76\u538b\u5165\u8c03\u7528\u6808\u3002\u6808\u5e27\u5305\u62ec\uff1a \u51fd\u6570\u8c03\u7528\u540e\u6307\u4ee4\u7684\u5730\u5740\uff08\u8fd4\u56de\u5730\u5740\uff09\u3002CPU\u901a\u8fc7\u5b83\u77e5\u9053\u51fd\u6570\u9000\u51fa\u65f6\u5e94\u8be5\u8fd4\u56de\u5230\u7684\u5730\u5740\uff1b \u6240\u6709\u51fd\u6570\u7684\u5b9e\u53c2\uff1b \u6240\u6709\u5c40\u90e8\u53d8\u91cf\u7684\u5185\u5b58\uff1b \u4fdd\u5b58\u88ab\u51fd\u6570\u4fee\u6539\u4e14\u9700\u8981\u5728\u51fd\u6570\u8fd4\u56de\u65f6\u6062\u590d\u7684\u5bc4\u5b58\u5668 CPU \u8df3\u8f6c\u5230\u51fd\u6570\u8d77\u70b9\uff1b \u51fd\u6570\u4e2d\u7684\u6307\u4ee4\u5f00\u59cb\u88ab\u6267\u884c\u3002 \u5f53\u51fd\u6570\u7ed3\u675f\u65f6\uff0c\u4f1a\u6267\u884c\u4e0b\u9762\u7684\u6b65\u9aa4\uff1a \u5bc4\u5b58\u5668\u4ece\u8c03\u7528\u6808\u6062\u590d\uff1b \u6808\u5e27\u5f39\u51fa\u8c03\u7528\u6808\u3002\u6b64\u65f6\u4f1a\u91ca\u653e\u5c40\u90e8\u53d8\u91cf\u548c\u5b9e\u53c2\u7684\u5185\u5b58\uff1b \u5904\u7406\u8fd4\u56de\u503c\uff1b CPU\u7ee7\u7eed\u4ece\u8fd4\u56de\u5730\u5740\u6267\u884c\u3002 \u6839\u636e\u8ba1\u7b97\u673a\u7684\u4f53\u7cfb\u7ed3\u6784\u7684\u4e0d\u540c\uff0c\u53ef\u4ee5\u6709\u8bb8\u591a\u4e0d\u540c\u7684\u65b9\u6cd5\u5904\u7406\u8fd4\u56de\u503c\u3002\u6709\u4e9b\u4f53\u7cfb\u7ed3\u6784\u5c06\u8fd4\u56de\u503c\u4f5c\u4e3a\u5806\u6808\u6846\u67b6\u7684\u4e00\u90e8\u5206\uff0c\u5176\u4ed6\u5219\u4f7f\u7528CPU\u5bc4\u5b58\u5668\u3002 \u901a\u5e38\uff0c\u4e86\u89e3\u8c03\u7528\u5806\u6808\u5982\u4f55\u5de5\u4f5c\u7684\u6240\u6709\u7ec6\u8282\u5e76\u4e0d\u91cd\u8981\u3002\u4f46\u662f\uff0c\u4e86\u89e3\u51fd\u6570\u5728\u8c03\u7528\u65f6\u88ab\u6709\u6548\u5730\u63a8\u5165\u5806\u6808\uff0c\u5728\u8fd4\u56de\u65f6\u5f39\u51fa\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u4e86\u89e3\u9012\u5f52\u6240\u9700\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u4ee5\u53ca\u5728\u8c03\u8bd5\u65f6\u6709\u7528\u7684\u5176\u4ed6\u4e00\u4e9b\u6982\u5ff5\u3002 \u63d0\u793a\uff1a\u5728\u67d0\u4e9b\u4f53\u7cfb\u7ed3\u6784\u4e0a\uff0c\u8c03\u7528\u5806\u6808\u4ece\u5185\u5b58\u5730\u57400\u589e\u957f\u3002\u5728\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u5b83\u4f1a\u5411\u5185\u5b58\u5730\u57400\u589e\u957f\u3002\u56e0\u6b64\uff0c\u65b0\u63a8\u7684\u5806\u6808\u5e27\u53ef\u80fd\u6bd4\u4ee5\u524d\u7684\u6808\u5e27\u6709\u66f4\u9ad8\u6216\u66f4\u4f4e\u7684\u5185\u5b58\u5730\u5740\u3002 \u4e00\u4e2a\u7b80\u5355\u7684\u8c03\u7528\u6808\u4f8b\u5b50 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7b80\u5355\u7684\u5e94\u7528\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int foo ( int x ) { // b return x ; } // foo is popped off the call stack here int main () { // a foo ( 5 ); // foo is pushed on the call stack here // c return 0 ; } \u5728\u5bf9\u5e94\u8c03\u7528\u6808\u6807\u8bb0\u70b9\u5904\u770b\u8d77\u6765\u662f\u8fd9\u6837\u7684\uff1a a: 1 main() b: 1 2 foo() (including parameter x) main() c: 1 main() \u5806\u6808\u6ea2\u51fa \u6808\u7684\u5927\u5c0f\u662f\u6709\u9650\u7684\uff0c\u56e0\u6b64\u5b83\u53ea\u80fd\u4fdd\u5b58\u6709\u9650\u7684\u4fe1\u606f\u3002\u5bf9\u4e8eWindows\u4e0a\u7684Visual Studio \u6765\u8bf4\uff0c\u6808\u7684\u9ed8\u8ba4\u5927\u5c0f\u4e3a 1MB\u3002\u800c\u5bf9\u4e8eLinux\u4e0a\u7684 g++/Clang\uff0c\u6808\u7684\u9ed8\u8ba4\u5927\u5c0f\u4e3a8MB\u3002\u5982\u679c\u7a0b\u5e8f\u89c6\u56fe\u5728\u6808\u4e0a\u4fdd\u5b58\u8fc7\u591a\u7684\u4fe1\u606f\uff0c\u5c31\u4f1a\u5bfc\u81f4 \u5806\u6808\u6ea2\u51fa \u3002\u5f53\u6808\u4e0a\u7684\u6240\u6709\u5185\u5b58\u90fd\u88ab\u5206\u914d\u51fa\u53bb\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u5806\u6808\u6ea2\u51fa\u2014\u2014\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u518d\u6b21\u5206\u914d\u7684\u5185\u5b58\u5c31\u4f1a\u6ea2\u51fa\u5230\u5176\u4ed6\u5185\u5b58\u6bb5\u3002 \u5806\u6808\u6ea2\u51fa\u7684\u4e3b\u8981\u539f\u56e0\u662f\u5728\u6808\u4e0a\u5206\u914d\u4e86\u8fc7\u591a\u7684\u53d8\u91cf\uff0c\u6216\u8005\u6267\u884c\u4e86\u8fc7\u591a\u7684\u51fd\u6570\u8c03\u7528\uff08\u51fd\u6570A\u8c03\u7528\u51fd\u6570B\u8c03\u7528\u51fd\u6570C\u8c03\u7528\u51fd\u6570D\u7b49\u7b49\uff09\u3002\u5bf9\u4e8e\u73b0\u4ee3\u64cd\u4f5c\u7cfb\u7edf\u6765\u8bf4\uff0c\u5806\u6808\u6ea2\u51fa\u4f1a\u5bfc\u81f4\u64cd\u4f5c\u7cfb\u7edf\u62a5\u544a\u975e\u6cd5\u8bbf\u95ee\u5e76\u7ec8\u6b62\u7a0b\u5e8f\u3002 \u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\u5c31\u6709\u53ef\u80fd\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\u3002\u4f60\u53ef\u4ee5\u5728\u4f60\u7684\u7cfb\u7edf\u4e0a\u8bd5\u8bd5\u770b\u7a0b\u5e8f\u4f1a\u4e0d\u4f1a\u5d29\u6e83\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { int stack [ 10000000 ]; std :: cout << \"hi\" << stack [ 0 ]; // we'll use stack[0] here so the compiler won't optimize the array away return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u8bd5\u56fe\u5728\u5806\u6808\u4e0a\u5206\u914d\u4e00\u4e2a\u5de8\u5927\u7684(\u53ef\u80fd\u662f40MB)\u6570\u7ec4\u3002\u56e0\u4e3a\u5806\u6808\u4e0d\u591f\u5927\uff0c\u65e0\u6cd5\u5904\u7406\u8fd9\u4e2a\u6570\u7ec4\uff0c\u6570\u7ec4\u5206\u914d\u6ea2\u51fa\u5230\u7a0b\u5e8f\u4e0d\u5141\u8bb8\u4f7f\u7528\u7684\u5185\u5b58\u90e8\u5206\u3002 \u5728 Windows (Visual Studio) \u4e0a\uff0c\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 HelloWorld.exe (process 15916) exited with code -1073741571. -1073741571 \u7b49\u4e8e\u5341\u516d\u8fdb\u5236\u7684 c0000005 \uff0c\u5b83\u5728 Windows \u4e2d\u8868\u793a\u975e\u6cd5\u8bbf\u95ee\u3002\u6ce8\u610f\u201chi\u201d\u5e76\u6ca1\u6709\u88ab\u6253\u5370\u51fa\u6765\uff0c\u56e0\u4e3a\u8fd8\u6ca1\u5230\u8fd9\u4e00\u6b65\u7a0b\u5e8f\u5c31\u5d29\u6e83\u4e86\u3002 \u4e0b\u9762\u662f\u53e6\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5b83\u4f1a\u56e0\u4e3a\u4e0d\u540c\u7684\u539f\u56e0\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include void foo () { foo (); std :: cout << \"hi\" ; } int main () { foo (); return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6bcf\u6b21\u8c03\u7528\u51fd\u6570 foo() \u65f6\uff0c\u6808\u5e27\u90fd\u4f1a\u88ab\u63a8\u5165\u5806\u6808\u3002\u56e0\u4e3a foo() \u65e0\u9650\u5730\u8c03\u7528\u81ea\u8eab\uff0c\u6700\u7ec8\u6808\u5c06\u8017\u5c3d\u5185\u5b58\u5e76\u5bfc\u81f4\u6ea2\u51fa\u3002 \u6808\u7684\u4f18\u7f3a\u70b9\uff1a \u5728\u6808\u4e0a\u5206\u914d\u5185\u5b58\u76f8\u5bf9\u8f83\u5feb\uff1b \u5728\u6808\u4e0a\u5206\u914d\u7684\u5185\u5b58\u53ea\u8981\u5728\u4f5c\u7528\u57df\u5c31\u4f1a\u4e00\u76f4\u5728\u6808\u4e0a\uff0c\u5728\u5f39\u51fa\u6808\u65f6\u4f1a\u88ab\u91ca\u653e\uff1b \u6240\u6709\u5728\u6808\u4e0a\u5206\u914d\u7684\u5185\u5b58\u90fd\u662f\u5728\u7f16\u8bd1\u65f6\u5c31\u5df2\u77e5\u7684\uff0c\u56e0\u6b64\u8fd9\u4e9b\u5185\u5b58\u53ef\u4ee5\u901a\u8fc7\u53d8\u91cf\u76f4\u63a5\u8bbf\u95ee\uff1b \u56e0\u4e3a\u6808\u76f8\u5bf9\u6765\u8bf4\u5f88\u5c0f\uff0c\u6240\u4ee5\u5728\u6808\u4e0a\u5206\u914d\u5927\u91cf\u7684\u5185\u5b58\u662f\u4e0d\u660e\u667a\u7684\u3002\u8fd9\u5176\u4e2d\u5c31\u5305\u62ec\u521b\u5efa\u5f88\u5927\u7684\u5c40\u90e8\u53d8\u91cf\u6570\u7ec4\u6216\u5927\u578b\u7ed3\u6784\u4f53\u3002 \u4f5c\u8005\u6ce8 \u6b64\u8bc4\u8bba \u6709\u4e00\u4e9b\u9644\u52a0\u7684(\u7b80\u5316\u7684)\u4fe1\u606f\uff0c\u5173\u4e8e\u5806\u6808\u4e0a\u7684\u53d8\u91cf\u662f\u5982\u4f55\u5e03\u5c40\u7684\uff0c\u4ee5\u53ca\u5728\u8fd0\u884c\u65f6\u5982\u4f55\u63a5\u6536\u5b9e\u9645\u7684\u5185\u5b58\u5730\u5740\u3002","title":"12.2 - \u6808\u548c\u5806"},{"location":"12-2-the-stack-and-the-heap/#_1","text":"\u5806\u6bb5(\u4e5f\u79f0\u4e3a\u201c\u81ea\u7531\u5b58\u50a8\u533a\u201d)\u8ddf\u8e2a\u7528\u4e8e\u52a8\u6001\u5185\u5b58\u5206\u914d\u7684\u5185\u5b58\u3002\u6211\u4eec\u5df2\u7ecf\u5728 11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d \u4e2d\u4ecb\u7ecd\u4e86\u5806\uff0c\u6240\u4ee5\u8fd9\u91cc\u53ea\u662f\u590d\u4e60\u4e00\u4e0b\uff1a \u5728 C++ \u4e2d\uff0c\u5f53\u4f60\u4f7f\u7528 new \u5206\u914d\u5185\u5b58\u65f6\uff0c\u8be5\u5185\u5b58\u5c31\u4f1a\u4f4d\u4e8e\u5806\u6bb5\u3002 1 2 int * ptr { new int }; // ptr is assigned 4 bytes in the heap int * array { new int [ 10 ] }; // array is assigned 40 bytes in the heap \u8be5\u5185\u5b58\u7684\u5730\u5740\u901a\u8fc7\u64cd\u4f5c\u7b26new\u8fd4\u56de\uff0c\u7136\u540e\u53ef\u4ee5\u5b58\u50a8\u5728\u6307\u9488\u4e2d\u3002\u4f60\u4e0d\u5fc5\u62c5\u5fc3\u5b9a\u4f4d\u7a7a\u95f2\u5185\u5b58\u5e76\u5c06\u5176\u5206\u914d\u7ed9\u7528\u6237\u7684\u5e95\u5c42\u673a\u5236\u3002\u7136\u800c\uff0c\u4f60\u9700\u8981\u77e5\u9053\u7684\u662f\uff0c\u8fde\u7eed\u5185\u5b58\u8bf7\u6c42\u53ef\u80fd\u4e0d\u4f1a\u5206\u914d\u8fde\u7eed\u7684\u5185\u5b58\u5730\u5740\uff01 1 2 3 int * ptr1 { new int }; int * ptr2 { new int }; // ptr1 and ptr2 may not have sequential addresses \u5f53\u52a8\u6001\u5206\u914d\u7684\u53d8\u91cf\u88ab\u5220\u9664\u65f6\uff0c\u5185\u5b58\u88ab\u201c\u8fd4\u56de\u201d\u5230\u5806\u4e2d\uff0c\u7136\u540e\u53ef\u4ee5\u5728\u63a5\u6536\u5230\u672a\u6765\u7684\u5206\u914d\u8bf7\u6c42\u65f6\u91cd\u65b0\u5206\u914d\u3002\u8bf7\u8bb0\u4f4f\uff0c\u5220\u9664\u6307\u9488\u5e76\u4e0d\u5220\u9664\u53d8\u91cf\uff0c\u5b83\u53ea\u662f\u5c06\u76f8\u5173\u5730\u5740\u7684\u5185\u5b58\u8fd4\u56de\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u3002 \u5806\u6709\u4f18\u70b9\u548c\u7f3a\u70b9\uff1a \u5728\u5806\u4e0a\u5206\u914d\u5185\u5b58\u76f8\u5bf9\u5f88\u6162\uff1b \u5df2\u5206\u914d\u7684\u5185\u5b58\u4e00\u76f4\u4fdd\u6301\u5206\u914d\u72b6\u6001\uff0c\u76f4\u5230\u5b83\u88ab\u7279\u522b\u5730\u91ca\u653e(\u6ce8\u610f\u5185\u5b58\u6cc4\u6f0f)\u6216\u5e94\u7528\u7a0b\u5e8f\u7ed3\u675f(\u8fd9\u65f6\u64cd\u4f5c\u7cfb\u7edf\u5e94\u8be5\u6e05\u7406\u5b83)\uff1b \u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u5fc5\u987b\u901a\u8fc7\u6307\u9488\u8bbf\u95ee\u3002\u89e3\u5f15\u7528\u6307\u9488\u6bd4\u76f4\u63a5\u8bbf\u95ee\u53d8\u91cf\u6bd4\u8f83\u6162\uff1b \u56e0\u4e3a\u5806\u662f\u4e00\u4e2a\u5f88\u5927\u7684\u5185\u5b58\u6c60\uff0c\u6240\u4ee5\u53ef\u4ee5\u5728\u8fd9\u91cc\u5206\u914d\u5927\u578b\u6570\u7ec4\u3001\u7ed3\u6784\u6216\u7c7b\u3002","title":"\u5806\u5185\u5b58\u6bb5"},{"location":"12-2-the-stack-and-the-heap/#_2","text":"\u8c03\u7528\u6808 \u6240\u626e\u6f14\u7684\u89d2\u8272\u66f4\u52a0\u6709\u610f\u601d\u3002\u8c03\u7528\u6808\u4f1a\u8ffd\u8e2a\u6240\u6709\u6d3b\u52a8\u51fd\u6570\uff08\u5df2\u7ecf\u88ab\u8c03\u7528\u4f46\u5c1a\u672a\u7ed3\u675f\uff09\u4ece\u5f00\u59cb\u5230\u5f53\u524d\u65f6\u95f4\u70b9\u7684\u4fe1\u606f\uff0c\u5e76\u4e14\u8d1f\u8d23\u4e3a\u51fd\u6570 \u5f62\u53c2 \u548c\u5c40\u90e8\u53d8\u91cf\u5206\u914d\u5185\u5b58\u3002 \u8c03\u7528\u6808\u4f7f\u7528\u6570\u636e\u7ed3\u6784\u2014\u2014\u6808\u5b9e\u73b0\u3002\u6240\u4ee5\u5728\u8ba8\u8bba\u8c03\u7528\u6808\u7684\u5de5\u4f5c\u539f\u7406\u4e4b\u524d\uff0c\u6211\u4eec\u5fc5\u987b\u8981\u6307\u5b9a\u6808\u662f\u4e00\u79cd\u4ec0\u4e48\u6837\u7684\u6570\u636e\u7ed3\u6784\u3002","title":"\u8c03\u7528\u6808"},{"location":"12-2-the-stack-and-the-heap/#_3","text":"\u6570\u636e\u7ed3\u6784 \u662f\u4e00\u79cd\u7ec4\u7ec7\u6570\u636e\u4ee5\u4fbf\u6709\u6548\u5730\u4f7f\u7528\u6570\u636e\u7684\u7f16\u7a0b\u673a\u5236\uff0c\u4f60\u5df2\u7ecf\u4e86\u89e3\u4e86\u51e0\u79cd\u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u6570\u7ec4\u548c\u7ed3\u6784\u4f53\u3002\u8fd9\u4e24\u79cd\u6570\u636e\u7ed3\u6784\u90fd\u63d0\u4f9b\u4e86\u6709\u6548\u5730\u5b58\u50a8\u6570\u636e\u548c\u8bbf\u95ee\u6570\u636e\u7684\u673a\u5236\u3002\u8fd8\u6709\u8bb8\u591a\u5728\u7f16\u7a0b\u4e2d\u7ecf\u5e38\u4f7f\u7528\u7684\u9644\u52a0\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u76f8\u5f53\u4e00\u90e8\u5206\u662f\u5728\u6807\u51c6\u5e93\u4e2d\u5b9e\u73b0\u7684\uff0c\u6808\u5c31\u662f\u5176\u4e2d\u4e4b\u4e00\u3002 \u60f3\u60f3\u81ea\u52a9\u9910\u5385\u91cc\u7684\u4e00\u53e0\u76d8\u5b50\u3002\u56e0\u4e3a\u6bcf\u4e2a\u76d8\u5b50\u90fd\u5f88\u91cd\uff0c\u800c\u4e14\u5b83\u4eec\u662f\u5806\u53e0\u7684\uff0c\u6240\u4ee5\u4f60\u5b9e\u9645\u4e0a\u53ea\u80fd\u505a\u4e09\u4ef6\u4e8b\u4e2d\u7684\u4e00\u4ef6: \u770b\u5230\u6700\u9876\u5c42\u76d8\u5b50\u7684\u8868\u9762\uff1b \u62ff\u8d70\u6700\u4e0a\u9762\u4e00\u4e2a\u76d8\u5b50\uff08\u6f0f\u51fa\u4e0b\u9762\u4e00\u4e2a\u76d8\u5b50\uff09\uff1b \u5c06\u4e00\u4e2a\u65b0\u7684\u76d8\u5b50\u653e\u5728\u6700\u4e0a\u9762\uff08\u906e\u853d\u4e0b\u9762\u4e00\u4e2a\u76d8\u5b50\uff09\u3002 \u5728\u8ba1\u7b97\u673a\u7f16\u7a0b\u4e2d\uff0c\u6808\u662f\u4e00\u79cd\u5bb9\u7eb3\u591a\u4e2a\u53d8\u91cf\u7684\u5bb9\u5668\u6570\u636e\u7ed3\u6784(\u5f88\u50cf\u6570\u7ec4)\u3002\u7136\u800c\uff0c\u6570\u7ec4\u5141\u8bb8\u4f60\u6309\u4efb\u4f55\u987a\u5e8f\u8bbf\u95ee\u548c\u4fee\u6539\u5143\u7d20(\u79f0\u4e3a \u968f\u673a\u8bbf\u95ee )\uff0c\u800c\u6808\u5219\u6709\u66f4\u591a\u7684\u9650\u5236\u3002\u53ef\u4ee5\u5728\u5806\u6808\u4e0a\u6267\u884c\u7684\u64cd\u4f5c\u5bf9\u5e94\u4e8e\u4e0a\u9762\u63d0\u5230\u7684\u4e09\u4ef6\u4e8b\uff1a \u67e5\u770b\u6808\u9876\u5143\u7d20(\u901a\u5e38\u901a\u8fc7 top() \u51fd\u6570\u6216 peek() )\uff1b \u83b7\u53d6\u5e76\u79fb\u9664\u6808\u9876\u5143\u7d20(\u901a\u8fc7 pop() \u5b8c\u6210)\uff1b \u5c06\u4e00\u4e2a\u65b0\u5143\u7d20\u653e\u7f6e\u5728\u6808\u9876(\u901a\u8fc7 push() \u5b8c\u6210)\u3002 \u6808\u662f\u4e00\u4e2a\u540e\u5165\u5148\u51fa(LIFO)\u7684\u6570\u636e\u7ed3\u6784\u3002\u6700\u540e\u88ab\u538b\u5165\u6808\u7684\u5143\u7d20\u4f1a\u7b2c\u4e00\u4e2a\u88ab\u5f39\u51fa\u3002\u5982\u679c\u4f60\u5c06\u4e00\u4e2a\u65b0\u7684\u76d8\u5b50\u653e\u5728\u76d8\u5b50\u5806\u9876\u4e0a\u7684\u8bdd\uff0c\u90a3\u4f60\u7b2c\u4e00\u4e2a\u62ff\u8d70\u7684\u76d8\u5b50\u4e5f\u53ea\u80fd\u662f\u5b83\u3002\u5f53\u65b0\u7684\u5143\u7d20\u88ab\u538b\u5165\u6808\u65f6\uff0c\u6808\u5c31\u4f1a\u589e\u957f\u3002\u5f53\u5143\u7d20\u88ab\u79fb\u9664\u65f6\uff0c\u5219\u6808\u4f1a\u6536\u7f29\u3002 \u4f8b\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u77ed\u7684\u5e8f\u5217\uff0c\u5c55\u793a\u4e86\u6808\u4e0a\u7684push\u548cpop\u662f\u5982\u4f55\u5de5\u4f5c\u7684: 1 2 3 4 5 6 7 8 9 10 11 Stack: empty Push 1 Stack: 1 Push 2 Stack: 1 2 Push 3 Stack: 1 2 3 Pop Stack: 1 2 Pop Stack: 1 \u5bf9\u4e8e\u8c03\u7528\u6808\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u76d8\u5b50\u7c7b\u6bd4\u662f\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u7c7b\u6bd4\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u505a\u4e00\u4e2a\u66f4\u597d\u7684\u7c7b\u6bd4\u3002\u8003\u8651\u4e00\u5806\u90ae\u7bb1\uff0c\u5b83\u4eec\u90fd\u5806\u53e0\u5728\u4e00\u8d77\u3002\u6bcf\u4e2a\u90ae\u7bb1\u53ea\u80fd\u88c5\u4e00\u4ef6\u7269\u54c1\uff0c\u800c\u4e14\u6240\u6709\u90ae\u7bb1\u90fd\u662f\u7a7a\u7684\u3002\u6b64\u5916\uff0c\u6bcf\u4e2a\u90ae\u7bb1\u90fd\u56fa\u5b9a\u5728\u5b83\u4e0b\u9762\u7684\u90ae\u7bb1\u4e0a\uff0c\u56e0\u6b64\u90ae\u7bb1\u7684\u6570\u91cf\u4e0d\u80fd\u66f4\u6539\u3002\u5982\u679c\u4e0d\u80fd\u66f4\u6539\u90ae\u7bb1\u7684\u6570\u91cf\uff0c\u5982\u4f55\u83b7\u5f97\u7c7b\u4f3c\u4e8e\u5806\u6808\u7684\u884c\u4e3a? \u9996\u5148\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u6807\u8bb0(\u6bd4\u5982\u4fbf\u5229\u8d34)\u6765\u8ddf\u8e2a\u6700\u4e0b\u9762\u7684\u7a7a\u90ae\u7bb1\u7684\u4f4d\u7f6e\u3002\u5728\u5f00\u59cb\u65f6\uff0c\u8fd9\u5c06\u662f\u6700\u4f4e\u7684\u90ae\u7bb1(\u5728\u5806\u6808\u7684\u5e95\u90e8)\u3002\u5f53\u6211\u4eec\u5c06\u4e00\u4e2a\u9879\u76ee\u63a8\u5165\u90ae\u7bb1\u5806\u6808\u65f6\uff0c\u6211\u4eec\u5c06\u5b83\u653e\u5165\u6709\u6807\u8bb0\u7684\u90ae\u7bb1\u4e2d(\u5373\u7b2c\u4e00\u4e2a\u7a7a\u90ae\u7bb1)\uff0c\u5e76\u5c06\u6807\u8bb0\u5411\u4e0a\u79fb\u52a8\u4e00\u4e2a\u90ae\u7bb1\u3002\u5f53\u6211\u4eec\u4ece\u5806\u6808\u4e2d\u5f39\u51fa\u4e00\u4e2a\u9879\u76ee\u65f6\uff0c\u6211\u4eec\u5c06\u6807\u8bb0\u5411\u4e0b\u79fb\u52a8\u4e00\u4e2a\u90ae\u7bb1(\u56e0\u6b64\u5b83\u6307\u5411\u9876\u90e8\u7684\u975e\u7a7a\u90ae\u7bb1)\uff0c\u5e76\u4ece\u8be5\u90ae\u7bb1\u4e2d\u5220\u9664\u8be5\u9879\u76ee\u3002\u4efb\u4f55\u4f4e\u4e8e\u6807\u8bb0\u7684\u4e1c\u897f\u90fd\u88ab\u8ba4\u4e3a\u662f\u201c\u5728\u6808\u4e0a\u201d\u3002\u6807\u8bb0\u5904\u6216\u6807\u8bb0\u4e0a\u65b9\u7684\u4efb\u4f55\u4e1c\u897f\u90fd\u4e0d\u5728\u6808\u4e0a\u3002","title":"\u6570\u636e\u7ed3\u6784\u2014\u2014\u6808"},{"location":"12-2-the-stack-and-the-heap/#_4","text":"\u8c03\u7528\u6808\u5185\u5b58\u6bb5\u4fdd\u5b58\u7528\u4e8e\u8c03\u7528\u6808\u7684\u5185\u5b58\u3002\u5f53\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\uff0c main() \u51fd\u6570\u88ab\u64cd\u4f5c\u7cfb\u7edf\u538b\u5230\u8c03\u7528\u6808\u4e0a\u3002\u7136\u540e\u7a0b\u5e8f\u5f00\u59cb\u6267\u884c\u3002 \u51fd\u6570\u8c03\u7528\u65f6\u5c06\u51fd\u6570\u538b\u5165\u8c03\u7528\u6808\u3002\u5f53\u5f53\u524d\u51fd\u6570\u7ed3\u675f\u65f6\uff0c\u8be5\u51fd\u6570\u4ece\u8c03\u7528\u6808\u4e2d\u5f39\u51fa\u3002\u56e0\u6b64\uff0c\u901a\u8fc7\u67e5\u770b\u538b\u5165\u8c03\u7528\u6808\u4e0a\u7684\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e3a\u5230\u8fbe\u5f53\u524d\u6267\u884c\u70b9\u800c\u8c03\u7528\u7684\u6240\u6709\u51fd\u6570\u3002 \u90ae\u7bb1\u7684\u6bd4\u55bb\u5f88\u597d\u5730\u63cf\u8ff0\u4e86\u8c03\u7528\u6808\u7684\u5de5\u4f5c\u539f\u7406\u3002\u6808\u672c\u8eab\u662f\u4e00\u6bb5\u56fa\u5b9a\u957f\u5ea6\u7684\u5185\u5b58\u3002\u90ae\u7bb1\u53ef\u4ee5\u88ab\u770b\u505a\u662f\u5185\u5b58\u5730\u5740\uff0c\u800c\u538b\u5165\u6216\u5f39\u51fa\u7684\u5185\u5bb9\u79f0\u4e3a \u6808\u5e27 \u3002\u6808\u5e27\u8ffd\u8e2a\u548c\u67d0\u4e2a\u51fd\u6570\u8c03\u7528\u76f8\u5173\u7684\u5168\u90e8\u6570\u636e\u3002\u6bd4\u55bb\u4e2d\u7684\u201c\u6807\u8bb0\u201d\u662f\u4e00\u4e2a\u88ab\u79f0\u4e3a\u5806\u6808\u6307\u9488(SP)\u7684\u5bc4\u5b58\u5668(CPU\u4e2d\u7684\u4e00\u5c0f\u5757\u5185\u5b58)\u3002\u5806\u6808\u6307\u9488\u7528\u4e8e\u8ffd\u8e2a\u5f53\u524d\u7684\u6808\u9876\u4f4d\u7f6e\u3002 \u6211\u4eec\u8fd8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\uff1a\u5f53\u4ece\u8c03\u7528\u6808\u4e2d\u5f39\u51fa\u5185\u5bb9\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5e76\u4e0d\u9700\u8981\u5b9e\u9645\u6e05\u7406\u6216\u6e05\u96f6\u88ab\u5f39\u51fa\u7684\u6808\u5e27\u2014\u2014\u53ea\u9700\u8981\u5c06\u6808\u9876\u6307\u9488\u5411\u4e0b\u79fb\u52a8\u5373\u53ef\u3002\u76f8\u5e94\u7684\u5185\u5bb9\u5df2\u7ecf\u4e0d\u88ab\u8ba4\u4e3a\u662f\u6808\u7684\u4e00\u90e8\u5206\u4e86\uff08\u6808\u6307\u9488\u4f1a\u5728\u8be5\u5730\u5740\u6216\u8be5\u5730\u5740\u7684\u4e0b\u65b9\uff09\uff0c\u6240\u4ee5\u5b83\u4e5f\u4e0d\u4f1a\u88ab\u5bfb\u5740\u3002\u5982\u679c\u7a0d\u540e\u6211\u4eec\u5c06\u65b0\u7684\u6808\u5e27\u538b\u5165\u4e86\u8be5\u5185\u5b58\u5730\u5740\uff0c\u5219\u5176\u5185\u5bb9\u6b63\u597d\u5c31\u88ab\u8986\u76d6\u6389\u4e86\u3002","title":"\u8c03\u7528\u6808\u5185\u5b58\u6bb5"},{"location":"12-2-the-stack-and-the-heap/#_5","text":"\u8ba9\u6211\u4eec\u66f4\u8be6\u7ec6\u5730\u7814\u7a76\u8c03\u7528\u6808\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u4e0b\u9762\u662f\u8c03\u7528\u51fd\u6570\u65f6\u7684\u6b65\u9aa4\uff1a \u7a0b\u5e8f\u4e2d\u53d1\u751f\u51fd\u6570\u8c03\u7528\uff1b \u6808\u5e27\u88ab\u6784\u5efa\u5e76\u538b\u5165\u8c03\u7528\u6808\u3002\u6808\u5e27\u5305\u62ec\uff1a \u51fd\u6570\u8c03\u7528\u540e\u6307\u4ee4\u7684\u5730\u5740\uff08\u8fd4\u56de\u5730\u5740\uff09\u3002CPU\u901a\u8fc7\u5b83\u77e5\u9053\u51fd\u6570\u9000\u51fa\u65f6\u5e94\u8be5\u8fd4\u56de\u5230\u7684\u5730\u5740\uff1b \u6240\u6709\u51fd\u6570\u7684\u5b9e\u53c2\uff1b \u6240\u6709\u5c40\u90e8\u53d8\u91cf\u7684\u5185\u5b58\uff1b \u4fdd\u5b58\u88ab\u51fd\u6570\u4fee\u6539\u4e14\u9700\u8981\u5728\u51fd\u6570\u8fd4\u56de\u65f6\u6062\u590d\u7684\u5bc4\u5b58\u5668 CPU \u8df3\u8f6c\u5230\u51fd\u6570\u8d77\u70b9\uff1b \u51fd\u6570\u4e2d\u7684\u6307\u4ee4\u5f00\u59cb\u88ab\u6267\u884c\u3002 \u5f53\u51fd\u6570\u7ed3\u675f\u65f6\uff0c\u4f1a\u6267\u884c\u4e0b\u9762\u7684\u6b65\u9aa4\uff1a \u5bc4\u5b58\u5668\u4ece\u8c03\u7528\u6808\u6062\u590d\uff1b \u6808\u5e27\u5f39\u51fa\u8c03\u7528\u6808\u3002\u6b64\u65f6\u4f1a\u91ca\u653e\u5c40\u90e8\u53d8\u91cf\u548c\u5b9e\u53c2\u7684\u5185\u5b58\uff1b \u5904\u7406\u8fd4\u56de\u503c\uff1b CPU\u7ee7\u7eed\u4ece\u8fd4\u56de\u5730\u5740\u6267\u884c\u3002 \u6839\u636e\u8ba1\u7b97\u673a\u7684\u4f53\u7cfb\u7ed3\u6784\u7684\u4e0d\u540c\uff0c\u53ef\u4ee5\u6709\u8bb8\u591a\u4e0d\u540c\u7684\u65b9\u6cd5\u5904\u7406\u8fd4\u56de\u503c\u3002\u6709\u4e9b\u4f53\u7cfb\u7ed3\u6784\u5c06\u8fd4\u56de\u503c\u4f5c\u4e3a\u5806\u6808\u6846\u67b6\u7684\u4e00\u90e8\u5206\uff0c\u5176\u4ed6\u5219\u4f7f\u7528CPU\u5bc4\u5b58\u5668\u3002 \u901a\u5e38\uff0c\u4e86\u89e3\u8c03\u7528\u5806\u6808\u5982\u4f55\u5de5\u4f5c\u7684\u6240\u6709\u7ec6\u8282\u5e76\u4e0d\u91cd\u8981\u3002\u4f46\u662f\uff0c\u4e86\u89e3\u51fd\u6570\u5728\u8c03\u7528\u65f6\u88ab\u6709\u6548\u5730\u63a8\u5165\u5806\u6808\uff0c\u5728\u8fd4\u56de\u65f6\u5f39\u51fa\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u4e86\u89e3\u9012\u5f52\u6240\u9700\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u4ee5\u53ca\u5728\u8c03\u8bd5\u65f6\u6709\u7528\u7684\u5176\u4ed6\u4e00\u4e9b\u6982\u5ff5\u3002 \u63d0\u793a\uff1a\u5728\u67d0\u4e9b\u4f53\u7cfb\u7ed3\u6784\u4e0a\uff0c\u8c03\u7528\u5806\u6808\u4ece\u5185\u5b58\u5730\u57400\u589e\u957f\u3002\u5728\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u5b83\u4f1a\u5411\u5185\u5b58\u5730\u57400\u589e\u957f\u3002\u56e0\u6b64\uff0c\u65b0\u63a8\u7684\u5806\u6808\u5e27\u53ef\u80fd\u6bd4\u4ee5\u524d\u7684\u6808\u5e27\u6709\u66f4\u9ad8\u6216\u66f4\u4f4e\u7684\u5185\u5b58\u5730\u5740\u3002","title":"\u8c03\u7528\u6808\u5b9e\u4f8b"},{"location":"12-2-the-stack-and-the-heap/#_6","text":"\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7b80\u5355\u7684\u5e94\u7528\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int foo ( int x ) { // b return x ; } // foo is popped off the call stack here int main () { // a foo ( 5 ); // foo is pushed on the call stack here // c return 0 ; } \u5728\u5bf9\u5e94\u8c03\u7528\u6808\u6807\u8bb0\u70b9\u5904\u770b\u8d77\u6765\u662f\u8fd9\u6837\u7684\uff1a a: 1 main() b: 1 2 foo() (including parameter x) main() c: 1 main()","title":"\u4e00\u4e2a\u7b80\u5355\u7684\u8c03\u7528\u6808\u4f8b\u5b50"},{"location":"12-2-the-stack-and-the-heap/#_7","text":"\u6808\u7684\u5927\u5c0f\u662f\u6709\u9650\u7684\uff0c\u56e0\u6b64\u5b83\u53ea\u80fd\u4fdd\u5b58\u6709\u9650\u7684\u4fe1\u606f\u3002\u5bf9\u4e8eWindows\u4e0a\u7684Visual Studio \u6765\u8bf4\uff0c\u6808\u7684\u9ed8\u8ba4\u5927\u5c0f\u4e3a 1MB\u3002\u800c\u5bf9\u4e8eLinux\u4e0a\u7684 g++/Clang\uff0c\u6808\u7684\u9ed8\u8ba4\u5927\u5c0f\u4e3a8MB\u3002\u5982\u679c\u7a0b\u5e8f\u89c6\u56fe\u5728\u6808\u4e0a\u4fdd\u5b58\u8fc7\u591a\u7684\u4fe1\u606f\uff0c\u5c31\u4f1a\u5bfc\u81f4 \u5806\u6808\u6ea2\u51fa \u3002\u5f53\u6808\u4e0a\u7684\u6240\u6709\u5185\u5b58\u90fd\u88ab\u5206\u914d\u51fa\u53bb\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u5806\u6808\u6ea2\u51fa\u2014\u2014\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u518d\u6b21\u5206\u914d\u7684\u5185\u5b58\u5c31\u4f1a\u6ea2\u51fa\u5230\u5176\u4ed6\u5185\u5b58\u6bb5\u3002 \u5806\u6808\u6ea2\u51fa\u7684\u4e3b\u8981\u539f\u56e0\u662f\u5728\u6808\u4e0a\u5206\u914d\u4e86\u8fc7\u591a\u7684\u53d8\u91cf\uff0c\u6216\u8005\u6267\u884c\u4e86\u8fc7\u591a\u7684\u51fd\u6570\u8c03\u7528\uff08\u51fd\u6570A\u8c03\u7528\u51fd\u6570B\u8c03\u7528\u51fd\u6570C\u8c03\u7528\u51fd\u6570D\u7b49\u7b49\uff09\u3002\u5bf9\u4e8e\u73b0\u4ee3\u64cd\u4f5c\u7cfb\u7edf\u6765\u8bf4\uff0c\u5806\u6808\u6ea2\u51fa\u4f1a\u5bfc\u81f4\u64cd\u4f5c\u7cfb\u7edf\u62a5\u544a\u975e\u6cd5\u8bbf\u95ee\u5e76\u7ec8\u6b62\u7a0b\u5e8f\u3002 \u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\u5c31\u6709\u53ef\u80fd\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\u3002\u4f60\u53ef\u4ee5\u5728\u4f60\u7684\u7cfb\u7edf\u4e0a\u8bd5\u8bd5\u770b\u7a0b\u5e8f\u4f1a\u4e0d\u4f1a\u5d29\u6e83\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { int stack [ 10000000 ]; std :: cout << \"hi\" << stack [ 0 ]; // we'll use stack[0] here so the compiler won't optimize the array away return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u8bd5\u56fe\u5728\u5806\u6808\u4e0a\u5206\u914d\u4e00\u4e2a\u5de8\u5927\u7684(\u53ef\u80fd\u662f40MB)\u6570\u7ec4\u3002\u56e0\u4e3a\u5806\u6808\u4e0d\u591f\u5927\uff0c\u65e0\u6cd5\u5904\u7406\u8fd9\u4e2a\u6570\u7ec4\uff0c\u6570\u7ec4\u5206\u914d\u6ea2\u51fa\u5230\u7a0b\u5e8f\u4e0d\u5141\u8bb8\u4f7f\u7528\u7684\u5185\u5b58\u90e8\u5206\u3002 \u5728 Windows (Visual Studio) \u4e0a\uff0c\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 HelloWorld.exe (process 15916) exited with code -1073741571. -1073741571 \u7b49\u4e8e\u5341\u516d\u8fdb\u5236\u7684 c0000005 \uff0c\u5b83\u5728 Windows \u4e2d\u8868\u793a\u975e\u6cd5\u8bbf\u95ee\u3002\u6ce8\u610f\u201chi\u201d\u5e76\u6ca1\u6709\u88ab\u6253\u5370\u51fa\u6765\uff0c\u56e0\u4e3a\u8fd8\u6ca1\u5230\u8fd9\u4e00\u6b65\u7a0b\u5e8f\u5c31\u5d29\u6e83\u4e86\u3002 \u4e0b\u9762\u662f\u53e6\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5b83\u4f1a\u56e0\u4e3a\u4e0d\u540c\u7684\u539f\u56e0\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include void foo () { foo (); std :: cout << \"hi\" ; } int main () { foo (); return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6bcf\u6b21\u8c03\u7528\u51fd\u6570 foo() \u65f6\uff0c\u6808\u5e27\u90fd\u4f1a\u88ab\u63a8\u5165\u5806\u6808\u3002\u56e0\u4e3a foo() \u65e0\u9650\u5730\u8c03\u7528\u81ea\u8eab\uff0c\u6700\u7ec8\u6808\u5c06\u8017\u5c3d\u5185\u5b58\u5e76\u5bfc\u81f4\u6ea2\u51fa\u3002 \u6808\u7684\u4f18\u7f3a\u70b9\uff1a \u5728\u6808\u4e0a\u5206\u914d\u5185\u5b58\u76f8\u5bf9\u8f83\u5feb\uff1b \u5728\u6808\u4e0a\u5206\u914d\u7684\u5185\u5b58\u53ea\u8981\u5728\u4f5c\u7528\u57df\u5c31\u4f1a\u4e00\u76f4\u5728\u6808\u4e0a\uff0c\u5728\u5f39\u51fa\u6808\u65f6\u4f1a\u88ab\u91ca\u653e\uff1b \u6240\u6709\u5728\u6808\u4e0a\u5206\u914d\u7684\u5185\u5b58\u90fd\u662f\u5728\u7f16\u8bd1\u65f6\u5c31\u5df2\u77e5\u7684\uff0c\u56e0\u6b64\u8fd9\u4e9b\u5185\u5b58\u53ef\u4ee5\u901a\u8fc7\u53d8\u91cf\u76f4\u63a5\u8bbf\u95ee\uff1b \u56e0\u4e3a\u6808\u76f8\u5bf9\u6765\u8bf4\u5f88\u5c0f\uff0c\u6240\u4ee5\u5728\u6808\u4e0a\u5206\u914d\u5927\u91cf\u7684\u5185\u5b58\u662f\u4e0d\u660e\u667a\u7684\u3002\u8fd9\u5176\u4e2d\u5c31\u5305\u62ec\u521b\u5efa\u5f88\u5927\u7684\u5c40\u90e8\u53d8\u91cf\u6570\u7ec4\u6216\u5927\u578b\u7ed3\u6784\u4f53\u3002 \u4f5c\u8005\u6ce8 \u6b64\u8bc4\u8bba \u6709\u4e00\u4e9b\u9644\u52a0\u7684(\u7b80\u5316\u7684)\u4fe1\u606f\uff0c\u5173\u4e8e\u5806\u6808\u4e0a\u7684\u53d8\u91cf\u662f\u5982\u4f55\u5e03\u5c40\u7684\uff0c\u4ee5\u53ca\u5728\u8fd0\u884c\u65f6\u5982\u4f55\u63a5\u6536\u5b9e\u9645\u7684\u5185\u5b58\u5730\u5740\u3002","title":"\u5806\u6808\u6ea2\u51fa"},{"location":"12-3-std-vector-capacity-and-stack-behavior/","tags":["vector","stack"],"text":"Key Takeaway \u5728 11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86 std::vector \u4ee5\u53ca\u5982\u4f55\u5c06\u5176\u5f53\u505a\u52a8\u6001\u6570\u7ec4\u4f7f\u7528\uff08\u53ef\u4ee5\u8bb0\u5f55\u81ea\u8eab\u957f\u5ea6\u4e5f\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u5927\u5c0f\uff09\u3002 \u5c3d\u7ba1\u8fd9\u662f std::vector \u6700\u5e38\u7528\u7684\u529f\u80fd\uff0c std::vector \u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u5176\u4ed6\u7684\u5c5e\u6027\u548c\u529f\u80fd\u4f7f\u5176\u5728\u522b\u7684\u65b9\u9762\u4e5f\u5f88\u6709\u7528\u3002 \u957f\u5ea6\u548c\u5bb9\u91cf \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 int * array { new int [ 10 ] { 1 , 2 , 3 , 4 , 5 } }; \u6211\u4eec\u4f1a\u8bf4\u8fd9\u4e2a\u6570\u7ec4\u7684\u957f\u5ea6\u662f10\uff0c\u5373\u4f7f\u6211\u4eec\u53ea\u4f7f\u7528\u4e86\u5206\u914d\u7684\u5143\u7d20\u4e2d\u76845\u4e2a\u3002 \u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u53ea\u60f3\u904d\u5386\u5df2\u7ecf\u521d\u59cb\u5316\u7684\u5143\u7d20\uff0c\u800c\u4fdd\u7559\u672a\u4f7f\u7528\u7684\u5143\u7d20\u4ee5\u5907\u5c06\u6765\u6269\u5c55\u5462\uff1f\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u5206\u522b\u8ddf\u8e2a\u201c\u4f7f\u7528\u201d\u4e86\u591a\u5c11\u5143\u7d20\u548c\u5206\u914d\u4e86\u591a\u5c11\u5143\u7d20\u3002\u4e0e\u53ea\u8bb0\u4f4f\u5176\u957f\u5ea6\u7684\u5185\u7f6e\u6570\u7ec4\u6216 std::array \u4e0d\u540c\uff0c std::vector \u5305\u542b\u4e24\u4e2a\u72ec\u7acb\u7684\u5c5e\u6027\uff1a\u957f\u5ea6\u548c\u5bb9\u91cf\u3002\u5728 std::vector \u7684\u8bed\u5883\u4e2d\uff0c length \u662f\u6570\u7ec4\u4e2d\u4f7f\u7528\u7684\u5143\u7d20\u6570\u91cf\uff0c\u800c capacity \u662f\u5185\u5b58\u4e2d\u5206\u914d\u7684\u5143\u7d20\u6570\u91cf\u3002 \u8ba9\u6211\u4eec\u518d\u770b\u4e00\u4e0b\u4e4b\u524d\u7684\u4e00\u4e2a std::vector \u7684\u4e00\u4e2a\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: vector array { 0 , 1 , 2 }; array . resize ( 5 ); // set length to 5 std :: cout << \"The length is: \" << array . size () << '\\n' ; for ( auto element : array ) std :: cout << element << ' ' ; return 0 ; }; 1 2 The length is: 5 0 1 2 0 0 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 resize() \u51fd\u6570\u5c06vector\u7684\u957f\u5ea6\u8bbe\u7f6e\u4e3a5\u3002\u8fd9\u544a\u8bc9\u53d8\u91cf\u6570\u7ec4\u6211\u4eec\u6253\u7b97\u4f7f\u7528\u6570\u7ec4\u7684\u524d5\u4e2a\u5143\u7d20\uff0c\u6240\u4ee5\u5b83\u5e94\u8be5\u628a\u8fd9\u4e9b\u5143\u7d20\u5f53\u505a\u6d3b\u52a8\u7684\u3002\u7136\u800c\uff0c\u8fd9\u5e26\u6765\u4e86\u4e00\u4e2a\u6709\u8da3\u7684\u95ee\u9898\uff1a\u8fd9\u4e2a\u6570\u7ec4\u7684\u5bb9\u91cf\u662f\u591a\u5c11? \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 capacity() \u51fd\u6570\u67e5\u8be2 std::vector \u7684\u5bb9\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include #include int main () { std :: vector array { 0 , 1 , 2 }; array . resize ( 5 ); // set length to 5 std :: cout << \"The length is: \" << array . size () << '\\n' ; std :: cout << \"The capacity is: \" << array . capacity () << '\\n' ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\uff0c\u8f93\u51fa\u5982\u4e0b\uff1a 1 2 The length is: 5 The capacity is: 5 \u5728\u672c\u4f8b\u4e2d\uff0c resize() \u51fd\u6570\u4f7f std::vector \u6539\u53d8\u4e86\u5b83\u7684\u957f\u5ea6\u548c\u5bb9\u91cf\u3002\u6ce8\u610f\uff0c\u5bb9\u91cf\u4fdd\u8bc1\u81f3\u5c11\u4e0e\u6570\u7ec4\u957f\u5ea6\u4e00\u6837\u5927(\u4f46\u4e5f\u53ef\u4ee5\u66f4\u5927)\uff0c\u5426\u5219\u8bbf\u95ee\u6570\u7ec4\u672b\u5c3e\u7684\u5143\u7d20\u5c06\u5728\u5206\u914d\u7684\u5185\u5b58\u4e4b\u5916\uff01 \u957f\u5ea6\u548c\u5bb9\u91cf\u2014\u2014\u66f4\u591a\u7ec6\u8282 \u4e3a\u4ec0\u4e48\u8981\u533a\u5206\u957f\u5ea6\u548c\u5bb9\u91cf\uff1fvector\u4f1a\u5728\u9700\u8981\u7684\u65f6\u5019\u91cd\u65b0\u5206\u914d\u5b83\u7684\u5185\u5b58\uff0c\u4f46\u662f\u5b83\u5e0c\u671b\u5c3d\u91cf\u907f\u514d\u91cd\u65b0\u5206\u914d\uff0c\u56e0\u4e3a\u8c03\u6574\u6570\u7ec4\u7684\u5927\u5c0f\u8ba1\u7b97\u5f00\u9500\u5f88\u5927\u3002\u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include #include int main () { std :: vector array {}; array = { 0 , 1 , 2 , 3 , 4 }; // okay, array length = 5 std :: cout << \"length: \" << array . size () << \" capacity: \" << array . capacity () << '\\n' ; array = { 9 , 8 , 7 }; // okay, array length is now 3! std :: cout << \"length: \" << array . size () << \" capacity: \" << array . capacity () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 length: 5 capacity: 5 length: 3 capacity: 5 \u6ce8\u610f\uff0c\u867d\u7136\u6211\u4eec\u4e3avector\u5206\u914d\u4e86\u4e00\u4e2a\u66f4\u5c0f\u7684\u6570\u7ec4\uff0c\u4f46\u5b83\u5e76\u6ca1\u6709\u91cd\u65b0\u5206\u914d\u5b83\u7684\u5185\u5b58(\u5bb9\u91cf\u4ecd\u7136\u662f5)\u3002\u5b83\u53ea\u662f\u66f4\u6539\u4e86\u5b83\u7684\u957f\u5ea6\uff0c\u56e0\u6b64\u5b83\u77e5\u9053\u6b64\u65f6\u53ea\u6709\u524d3\u4e2a\u5143\u7d20\u6709\u6548\u3002 \u6570\u7ec4\u4e0b\u6807\u548c at() \u57fa\u4e8e\u957f\u5ea6\u800c\u975e\u5bb9\u91cf \u4e0b\u6807\u8fd0\u7b97\u7b26\u548c at() \u51fd\u6570\u662f\u57fa\u4e8evector\u7684\u957f\u5ea6\u800c\u975e\u5bb9\u91cf\u5de5\u4f5c\u7684\u3002\u5bf9\u4e8e\u524d\u4f8b\u4e2d\u7684array\uff0c\u5b83\u7684\u957f\u5ea6\u662f3\uff0c\u5bb9\u91cf\u662f5\u3002\u90a3\u4e48\u5982\u679c\u6211\u4eec\u8bbf\u95ee\u7d22\u5f154\u7684\u5143\u7d20\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f\u7ed3\u679c\u663e\u7136\u662f\u4f1a\u8bbf\u95ee\u5931\u8d25\uff0c\u56e0\u4e3a4\u5df2\u7ecf\u8d85\u8fc7\u4e86\u6570\u7ec4\u7684\u957f\u5ea6\u3002 \u6ce8\u610f\uff1avector\u5e76\u4e0d\u4f1a\u56e0\u4e3a\u4e0b\u6807\u8fd0\u7b97\u7b26\u548c at() \u7684\u8c03\u7528\u800c\u8c03\u6574\u5927\u5c0f\uff01 std::vector \u7684\u7c7b\u6808\u884c\u4e3a \u5982\u679c\u4e0b\u6807\u64cd\u4f5c\u7b26\u548c at() \u51fd\u6570\u662f\u57fa\u4e8e\u6570\u7ec4\u957f\u5ea6\u7684\uff0c\u5e76\u4e14\u5bb9\u91cf\u603b\u662f\u81f3\u5c11\u4e0e\u6570\u7ec4\u957f\u5ea6\u4e00\u6837\u5927\uff0c\u4e3a\u4ec0\u4e48\u8fd8\u8981\u62c5\u5fc3\u5bb9\u91cf\u5462\uff1f\u867d\u7136 std::vector \u53ef\u4ee5\u7528\u4f5c\u52a8\u6001\u6570\u7ec4\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u4f5c\u5806\u6808\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u75283\u4e2a\u4e0e\u6808\u64cd\u4f5c\u76f8\u5339\u914d\u7684\u51fd\u6570\uff1a push_back() \uff1a\u5143\u7d20\u538b\u6808\uff1b back( ) \uff1a\u8fd4\u56de\u6808\u9876\u5143\u7d20\uff1b pop_back() \uff1a\u4ece\u6808\u4e2d\u5f39\u51fa\u5143\u7d20\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include void printStack ( const std :: vector < int >& stack ) { for ( auto element : stack ) std :: cout << element << ' ' ; std :: cout << \"(cap \" << stack . capacity () << \" length \" << stack . size () << \") \\n \" ; } int main () { std :: vector < int > stack {}; printStack ( stack ); stack . push_back ( 5 ); // push_back() \u538b\u6808 printStack ( stack ); stack . push_back ( 3 ); printStack ( stack ); stack . push_back ( 2 ); printStack ( stack ); std :: cout << \"top: \" << stack . back () << '\\n' ; // back() \u8fd4\u56de\u6700\u540e\u4e00\u4e2a\u5143\u7d20 stack . pop_back (); // pop_back() \u5f39\u51fa\u5143\u7d20 printStack ( stack ); stack . pop_back (); printStack ( stack ); stack . pop_back (); printStack ( stack ); return 0 ; } COPY This prints: 1 2 3 4 5 6 7 8 (cap 0 length 0) 5 (cap 1 length 1) 5 3 (cap 2 length 2) 5 3 2 (cap 3 length 3) top: 2 5 3 (cap 3 length 2) 5 (cap 3 length 1) (cap 3 length 0) \u548c\u4e0b\u6807\u8fd0\u7b97\u7b26\u548c at() \u4e0d\u540c\uff0c\u6808\u64cd\u4f5c\u51fd\u6570\u4f1a\u5728\u9700\u8981\u65f6\u8c03\u6574 std::vector \u7684\u5927\u5c0f\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u3002vector \u7684\u5927\u5c0f\u88ab\u8c03\u6574\u4e863\u6b21(capacity \u4ece 0 \u5230 1\uff0c 1 \u5230 2\uff0c2 \u5230 3)\u3002 \u56e0\u4e3a\u8c03\u6574vector\u7684\u5927\u5c0f\u4ee3\u4ef7\u5f88\u5927\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 reserve() \u51fd\u6570\u9884\u5148\u544a\u8bc9vector\u5206\u914d\u4e00\u5b9a\u91cf\u7684\u5bb9\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include void printStack ( const std :: vector < int >& stack ) { for ( auto element : stack ) std :: cout << element << ' ' ; std :: cout << \"(cap \" << stack . capacity () << \" length \" << stack . size () << \") \\n \" ; } int main () { std :: vector < int > stack {}; stack . reserve ( 5 ); // \u5c06\u5bb9\u91cf\u8bbe\u7f6e\u4e3a5 printStack ( stack ); stack . push_back ( 5 ); printStack ( stack ); stack . push_back ( 3 ); printStack ( stack ); stack . push_back ( 2 ); printStack ( stack ); std :: cout << \"top: \" << stack . back () << '\\n' ; stack . pop_back (); printStack ( stack ); stack . pop_back (); printStack ( stack ); stack . pop_back (); printStack ( stack ); return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 3 4 5 6 7 8 (cap 5 length 0) 5 (cap 5 length 1) 5 3 (cap 5 length 2) 5 3 2 (cap 5 length 3) top: 2 5 3 (cap 5 length 2) 5 (cap 5 length 1) (cap 5 length 0) \u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5bb9\u91cf\u88ab\u9884\u8bbe\u4e3a5\uff0c\u5e76\u4e14\u5728\u7a0b\u5e8f\u7684\u751f\u547d\u5468\u671f\u4e2d\u6ca1\u6709\u53d8\u5316\u3002 Vectors \u53ef\u80fd\u4f1a\u5206\u914d\u591a\u4f59\u7684\u5bb9\u91cf \u5f53\u8c03\u6574\u4e00\u4e2avector\u7684\u5927\u5c0f\u65f6\uff0c\u8be5vector\u53ef\u80fd\u4f1a\u5206\u914d\u6bd4\u6240\u9700\u66f4\u591a\u7684\u5bb9\u91cf\u3002\u8fd9\u6837\u505a\u662f\u4e3a\u4e86\u4e3a\u5176\u4ed6\u5143\u7d20\u63d0\u4f9b\u4e00\u4e9b\u201c\u5598\u606f\u7684\u7a7a\u95f4\u201d\uff0c\u4ece\u800c\u6700\u5c0f\u5316\u6240\u9700\u7684\u8c03\u6574\u64cd\u4f5c\u7684\u6570\u91cf\u3002\u8ba9\u6211\u4eec\u6765\u770b\u770b\u8fd9\u4e2a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include int main () { std :: vector v { 0 , 1 , 2 , 3 , 4 }; std :: cout << \"size: \" << v . size () << \" cap: \" << v . capacity () << '\\n' ; v . push_back ( 5 ); // add another element std :: cout << \"size: \" << v . size () << \" cap: \" << v . capacity () << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u673a\u5668\u4e0a\u6253\u5370\uff1a 1 2 size: 5 cap: 5 size: 6 cap: 7 \u5f53\u6211\u4eec\u4f7f\u7528 push_back() \u6dfb\u52a0\u4e00\u4e2a\u65b0\u5143\u7d20\u65f6\uff0c\u6211\u4eec\u7684\u5411\u91cf\u53ea\u9700\u89816\u4e2a\u5143\u7d20\u7684\u7a7a\u95f4\uff0c\u4f46\u5206\u914d\u4e867\u4e2a\u7a7a\u95f4\u3002\u8fd9\u6837\u505a\u7684\u76ee\u7684\u662f\uff0c\u5982\u679c\u6211\u4eec\u8981 push_back() \u53e6\u4e00\u4e2a\u5143\u7d20\uff0c\u5b83\u4e0d\u9700\u8981\u7acb\u5373\u8c03\u6574\u5927\u5c0f\u3002 \u662f\u5426\u3001\u4f55\u65f6\u4ee5\u53ca\u5206\u914d\u591a\u5c11\u989d\u5916\u7684\u5bb9\u91cf\u53d6\u51b3\u4e8e\u7f16\u8bd1\u5668\u5b9e\u73b0\u8005\u3002","title":"12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a"},{"location":"12-3-std-vector-capacity-and-stack-behavior/#_1","text":"\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 int * array { new int [ 10 ] { 1 , 2 , 3 , 4 , 5 } }; \u6211\u4eec\u4f1a\u8bf4\u8fd9\u4e2a\u6570\u7ec4\u7684\u957f\u5ea6\u662f10\uff0c\u5373\u4f7f\u6211\u4eec\u53ea\u4f7f\u7528\u4e86\u5206\u914d\u7684\u5143\u7d20\u4e2d\u76845\u4e2a\u3002 \u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u53ea\u60f3\u904d\u5386\u5df2\u7ecf\u521d\u59cb\u5316\u7684\u5143\u7d20\uff0c\u800c\u4fdd\u7559\u672a\u4f7f\u7528\u7684\u5143\u7d20\u4ee5\u5907\u5c06\u6765\u6269\u5c55\u5462\uff1f\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u5206\u522b\u8ddf\u8e2a\u201c\u4f7f\u7528\u201d\u4e86\u591a\u5c11\u5143\u7d20\u548c\u5206\u914d\u4e86\u591a\u5c11\u5143\u7d20\u3002\u4e0e\u53ea\u8bb0\u4f4f\u5176\u957f\u5ea6\u7684\u5185\u7f6e\u6570\u7ec4\u6216 std::array \u4e0d\u540c\uff0c std::vector \u5305\u542b\u4e24\u4e2a\u72ec\u7acb\u7684\u5c5e\u6027\uff1a\u957f\u5ea6\u548c\u5bb9\u91cf\u3002\u5728 std::vector \u7684\u8bed\u5883\u4e2d\uff0c length \u662f\u6570\u7ec4\u4e2d\u4f7f\u7528\u7684\u5143\u7d20\u6570\u91cf\uff0c\u800c capacity \u662f\u5185\u5b58\u4e2d\u5206\u914d\u7684\u5143\u7d20\u6570\u91cf\u3002 \u8ba9\u6211\u4eec\u518d\u770b\u4e00\u4e0b\u4e4b\u524d\u7684\u4e00\u4e2a std::vector \u7684\u4e00\u4e2a\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: vector array { 0 , 1 , 2 }; array . resize ( 5 ); // set length to 5 std :: cout << \"The length is: \" << array . size () << '\\n' ; for ( auto element : array ) std :: cout << element << ' ' ; return 0 ; }; 1 2 The length is: 5 0 1 2 0 0 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 resize() \u51fd\u6570\u5c06vector\u7684\u957f\u5ea6\u8bbe\u7f6e\u4e3a5\u3002\u8fd9\u544a\u8bc9\u53d8\u91cf\u6570\u7ec4\u6211\u4eec\u6253\u7b97\u4f7f\u7528\u6570\u7ec4\u7684\u524d5\u4e2a\u5143\u7d20\uff0c\u6240\u4ee5\u5b83\u5e94\u8be5\u628a\u8fd9\u4e9b\u5143\u7d20\u5f53\u505a\u6d3b\u52a8\u7684\u3002\u7136\u800c\uff0c\u8fd9\u5e26\u6765\u4e86\u4e00\u4e2a\u6709\u8da3\u7684\u95ee\u9898\uff1a\u8fd9\u4e2a\u6570\u7ec4\u7684\u5bb9\u91cf\u662f\u591a\u5c11? \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 capacity() \u51fd\u6570\u67e5\u8be2 std::vector \u7684\u5bb9\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include #include int main () { std :: vector array { 0 , 1 , 2 }; array . resize ( 5 ); // set length to 5 std :: cout << \"The length is: \" << array . size () << '\\n' ; std :: cout << \"The capacity is: \" << array . capacity () << '\\n' ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\uff0c\u8f93\u51fa\u5982\u4e0b\uff1a 1 2 The length is: 5 The capacity is: 5 \u5728\u672c\u4f8b\u4e2d\uff0c resize() \u51fd\u6570\u4f7f std::vector \u6539\u53d8\u4e86\u5b83\u7684\u957f\u5ea6\u548c\u5bb9\u91cf\u3002\u6ce8\u610f\uff0c\u5bb9\u91cf\u4fdd\u8bc1\u81f3\u5c11\u4e0e\u6570\u7ec4\u957f\u5ea6\u4e00\u6837\u5927(\u4f46\u4e5f\u53ef\u4ee5\u66f4\u5927)\uff0c\u5426\u5219\u8bbf\u95ee\u6570\u7ec4\u672b\u5c3e\u7684\u5143\u7d20\u5c06\u5728\u5206\u914d\u7684\u5185\u5b58\u4e4b\u5916\uff01","title":"\u957f\u5ea6\u548c\u5bb9\u91cf"},{"location":"12-3-std-vector-capacity-and-stack-behavior/#_2","text":"\u4e3a\u4ec0\u4e48\u8981\u533a\u5206\u957f\u5ea6\u548c\u5bb9\u91cf\uff1fvector\u4f1a\u5728\u9700\u8981\u7684\u65f6\u5019\u91cd\u65b0\u5206\u914d\u5b83\u7684\u5185\u5b58\uff0c\u4f46\u662f\u5b83\u5e0c\u671b\u5c3d\u91cf\u907f\u514d\u91cd\u65b0\u5206\u914d\uff0c\u56e0\u4e3a\u8c03\u6574\u6570\u7ec4\u7684\u5927\u5c0f\u8ba1\u7b97\u5f00\u9500\u5f88\u5927\u3002\u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include #include int main () { std :: vector array {}; array = { 0 , 1 , 2 , 3 , 4 }; // okay, array length = 5 std :: cout << \"length: \" << array . size () << \" capacity: \" << array . capacity () << '\\n' ; array = { 9 , 8 , 7 }; // okay, array length is now 3! std :: cout << \"length: \" << array . size () << \" capacity: \" << array . capacity () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 length: 5 capacity: 5 length: 3 capacity: 5 \u6ce8\u610f\uff0c\u867d\u7136\u6211\u4eec\u4e3avector\u5206\u914d\u4e86\u4e00\u4e2a\u66f4\u5c0f\u7684\u6570\u7ec4\uff0c\u4f46\u5b83\u5e76\u6ca1\u6709\u91cd\u65b0\u5206\u914d\u5b83\u7684\u5185\u5b58(\u5bb9\u91cf\u4ecd\u7136\u662f5)\u3002\u5b83\u53ea\u662f\u66f4\u6539\u4e86\u5b83\u7684\u957f\u5ea6\uff0c\u56e0\u6b64\u5b83\u77e5\u9053\u6b64\u65f6\u53ea\u6709\u524d3\u4e2a\u5143\u7d20\u6709\u6548\u3002","title":"\u957f\u5ea6\u548c\u5bb9\u91cf\u2014\u2014\u66f4\u591a\u7ec6\u8282"},{"location":"12-3-std-vector-capacity-and-stack-behavior/#at","text":"\u4e0b\u6807\u8fd0\u7b97\u7b26\u548c at() \u51fd\u6570\u662f\u57fa\u4e8evector\u7684\u957f\u5ea6\u800c\u975e\u5bb9\u91cf\u5de5\u4f5c\u7684\u3002\u5bf9\u4e8e\u524d\u4f8b\u4e2d\u7684array\uff0c\u5b83\u7684\u957f\u5ea6\u662f3\uff0c\u5bb9\u91cf\u662f5\u3002\u90a3\u4e48\u5982\u679c\u6211\u4eec\u8bbf\u95ee\u7d22\u5f154\u7684\u5143\u7d20\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f\u7ed3\u679c\u663e\u7136\u662f\u4f1a\u8bbf\u95ee\u5931\u8d25\uff0c\u56e0\u4e3a4\u5df2\u7ecf\u8d85\u8fc7\u4e86\u6570\u7ec4\u7684\u957f\u5ea6\u3002 \u6ce8\u610f\uff1avector\u5e76\u4e0d\u4f1a\u56e0\u4e3a\u4e0b\u6807\u8fd0\u7b97\u7b26\u548c at() \u7684\u8c03\u7528\u800c\u8c03\u6574\u5927\u5c0f\uff01","title":"\u6570\u7ec4\u4e0b\u6807\u548c at()\u57fa\u4e8e\u957f\u5ea6\u800c\u975e\u5bb9\u91cf"},{"location":"12-3-std-vector-capacity-and-stack-behavior/#stdvector","text":"\u5982\u679c\u4e0b\u6807\u64cd\u4f5c\u7b26\u548c at() \u51fd\u6570\u662f\u57fa\u4e8e\u6570\u7ec4\u957f\u5ea6\u7684\uff0c\u5e76\u4e14\u5bb9\u91cf\u603b\u662f\u81f3\u5c11\u4e0e\u6570\u7ec4\u957f\u5ea6\u4e00\u6837\u5927\uff0c\u4e3a\u4ec0\u4e48\u8fd8\u8981\u62c5\u5fc3\u5bb9\u91cf\u5462\uff1f\u867d\u7136 std::vector \u53ef\u4ee5\u7528\u4f5c\u52a8\u6001\u6570\u7ec4\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u4f5c\u5806\u6808\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u75283\u4e2a\u4e0e\u6808\u64cd\u4f5c\u76f8\u5339\u914d\u7684\u51fd\u6570\uff1a push_back() \uff1a\u5143\u7d20\u538b\u6808\uff1b back( ) \uff1a\u8fd4\u56de\u6808\u9876\u5143\u7d20\uff1b pop_back() \uff1a\u4ece\u6808\u4e2d\u5f39\u51fa\u5143\u7d20\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include void printStack ( const std :: vector < int >& stack ) { for ( auto element : stack ) std :: cout << element << ' ' ; std :: cout << \"(cap \" << stack . capacity () << \" length \" << stack . size () << \") \\n \" ; } int main () { std :: vector < int > stack {}; printStack ( stack ); stack . push_back ( 5 ); // push_back() \u538b\u6808 printStack ( stack ); stack . push_back ( 3 ); printStack ( stack ); stack . push_back ( 2 ); printStack ( stack ); std :: cout << \"top: \" << stack . back () << '\\n' ; // back() \u8fd4\u56de\u6700\u540e\u4e00\u4e2a\u5143\u7d20 stack . pop_back (); // pop_back() \u5f39\u51fa\u5143\u7d20 printStack ( stack ); stack . pop_back (); printStack ( stack ); stack . pop_back (); printStack ( stack ); return 0 ; } COPY This prints: 1 2 3 4 5 6 7 8 (cap 0 length 0) 5 (cap 1 length 1) 5 3 (cap 2 length 2) 5 3 2 (cap 3 length 3) top: 2 5 3 (cap 3 length 2) 5 (cap 3 length 1) (cap 3 length 0) \u548c\u4e0b\u6807\u8fd0\u7b97\u7b26\u548c at() \u4e0d\u540c\uff0c\u6808\u64cd\u4f5c\u51fd\u6570\u4f1a\u5728\u9700\u8981\u65f6\u8c03\u6574 std::vector \u7684\u5927\u5c0f\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u3002vector \u7684\u5927\u5c0f\u88ab\u8c03\u6574\u4e863\u6b21(capacity \u4ece 0 \u5230 1\uff0c 1 \u5230 2\uff0c2 \u5230 3)\u3002 \u56e0\u4e3a\u8c03\u6574vector\u7684\u5927\u5c0f\u4ee3\u4ef7\u5f88\u5927\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 reserve() \u51fd\u6570\u9884\u5148\u544a\u8bc9vector\u5206\u914d\u4e00\u5b9a\u91cf\u7684\u5bb9\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include void printStack ( const std :: vector < int >& stack ) { for ( auto element : stack ) std :: cout << element << ' ' ; std :: cout << \"(cap \" << stack . capacity () << \" length \" << stack . size () << \") \\n \" ; } int main () { std :: vector < int > stack {}; stack . reserve ( 5 ); // \u5c06\u5bb9\u91cf\u8bbe\u7f6e\u4e3a5 printStack ( stack ); stack . push_back ( 5 ); printStack ( stack ); stack . push_back ( 3 ); printStack ( stack ); stack . push_back ( 2 ); printStack ( stack ); std :: cout << \"top: \" << stack . back () << '\\n' ; stack . pop_back (); printStack ( stack ); stack . pop_back (); printStack ( stack ); stack . pop_back (); printStack ( stack ); return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 3 4 5 6 7 8 (cap 5 length 0) 5 (cap 5 length 1) 5 3 (cap 5 length 2) 5 3 2 (cap 5 length 3) top: 2 5 3 (cap 5 length 2) 5 (cap 5 length 1) (cap 5 length 0) \u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5bb9\u91cf\u88ab\u9884\u8bbe\u4e3a5\uff0c\u5e76\u4e14\u5728\u7a0b\u5e8f\u7684\u751f\u547d\u5468\u671f\u4e2d\u6ca1\u6709\u53d8\u5316\u3002","title":"std::vector \u7684\u7c7b\u6808\u884c\u4e3a"},{"location":"12-3-std-vector-capacity-and-stack-behavior/#vectors","text":"\u5f53\u8c03\u6574\u4e00\u4e2avector\u7684\u5927\u5c0f\u65f6\uff0c\u8be5vector\u53ef\u80fd\u4f1a\u5206\u914d\u6bd4\u6240\u9700\u66f4\u591a\u7684\u5bb9\u91cf\u3002\u8fd9\u6837\u505a\u662f\u4e3a\u4e86\u4e3a\u5176\u4ed6\u5143\u7d20\u63d0\u4f9b\u4e00\u4e9b\u201c\u5598\u606f\u7684\u7a7a\u95f4\u201d\uff0c\u4ece\u800c\u6700\u5c0f\u5316\u6240\u9700\u7684\u8c03\u6574\u64cd\u4f5c\u7684\u6570\u91cf\u3002\u8ba9\u6211\u4eec\u6765\u770b\u770b\u8fd9\u4e2a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include int main () { std :: vector v { 0 , 1 , 2 , 3 , 4 }; std :: cout << \"size: \" << v . size () << \" cap: \" << v . capacity () << '\\n' ; v . push_back ( 5 ); // add another element std :: cout << \"size: \" << v . size () << \" cap: \" << v . capacity () << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u673a\u5668\u4e0a\u6253\u5370\uff1a 1 2 size: 5 cap: 5 size: 6 cap: 7 \u5f53\u6211\u4eec\u4f7f\u7528 push_back() \u6dfb\u52a0\u4e00\u4e2a\u65b0\u5143\u7d20\u65f6\uff0c\u6211\u4eec\u7684\u5411\u91cf\u53ea\u9700\u89816\u4e2a\u5143\u7d20\u7684\u7a7a\u95f4\uff0c\u4f46\u5206\u914d\u4e867\u4e2a\u7a7a\u95f4\u3002\u8fd9\u6837\u505a\u7684\u76ee\u7684\u662f\uff0c\u5982\u679c\u6211\u4eec\u8981 push_back() \u53e6\u4e00\u4e2a\u5143\u7d20\uff0c\u5b83\u4e0d\u9700\u8981\u7acb\u5373\u8c03\u6574\u5927\u5c0f\u3002 \u662f\u5426\u3001\u4f55\u65f6\u4ee5\u53ca\u5206\u914d\u591a\u5c11\u989d\u5916\u7684\u5bb9\u91cf\u53d6\u51b3\u4e8e\u7f16\u8bd1\u5668\u5b9e\u73b0\u8005\u3002","title":"Vectors \u53ef\u80fd\u4f1a\u5206\u914d\u591a\u4f59\u7684\u5bb9\u91cf"},{"location":"12-4-recursion/","tags":["recursion"],"text":"\u5728 C++ \u4e2d\uff0c\u81ea\u5df1\u8c03\u7528\u81ea\u5df1\u7684\u51fd\u6570\u79f0\u4e3a\u9012\u5f52\u51fd\u6570\u3002\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u662f\u4e00\u4e2a\u9012\u5f52\u51fd\u6570\u7684\u4f8b\u5b50\uff08\u4e0d\u4f73\u7684\u5199\u6cd5\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include void countDown ( int count ) { std :: cout << \"push \" << count << '\\n' ; countDown ( count -1 ); // countDown() calls itself recursively } int main () { countDown ( 5 ); return 0 ; } \u5f53 countDown(5) \u88ab\u8c03\u7528\u65f6\uff0c\u6253\u5370\u4e86 \u201cpush 5\u201d\u3002\u7136\u540e countDown(4) \u88ab\u8c03\u7528\uff0c\u5b83\u6253\u5370 \u201cpush 4\u201d \u5e76\u8c03\u7528 countDown(3) \u3002 countDown(3) \u4f1a\u6253\u5370 \u201cpush 3\u201d \u5e76\u8c03\u7528 countDown(2) \u3002 countDown(n) \u8c03\u7528\u7684\u4e00\u7cfb\u5217 countDown(n-1) \uff0c\u5f62\u6210\u7684\u9012\u5f52\u8c03\u7528\u7b49\u4ef7\u4e8e\u4e00\u4e2a\u6b7b\u5faa\u73af\u3002 \u5728 12.2 - \u6808\u548c\u5806 \u4e2d\u6211\u4eec\u5b66\u8fc7\uff0c\u6240\u6709\u7684\u51fd\u6570\u8c03\u7528\u90fd\u9700\u8981\u5c06\u76f8\u5e94\u7684\u6570\u636e\u653e\u5728\u8c03\u7528\u6808\u4e0a\u3002\u56e0\u4e3a countDown() \u51fd\u6570\u4ece\u4e0d\u8fd4\u56de\uff08\u53ea\u662f\u4e0d\u65ad\u5730\u8c03\u7528 countDown() )\uff0c\u56e0\u6b64\u76f8\u5173\u7684\u51fd\u6570\u4fe1\u606f\u4ece\u6765\u6ca1\u6709\u88ab\u4ece\u6808\u4e0a\u5f39\u51fa\u8fc7\u3002\u56e0\u6b64\uff0c\u5728\u5230\u8fbe\u67d0\u4e2a\u6781\u9650\u65f6\uff0c\u8ba1\u7b97\u673a\u5c31\u4f1a\u8017\u5c3d\u6808\u4e0a\u5185\u5b58\uff0c\u8fdb\u800c\u53d1\u751f\u5806\u6808\u6ea2\u51fa\uff0c\u7a0b\u5e8f\u5d29\u6e83\u3002\u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\uff0c\u7a0b\u5e8f\u4f1a\u5728count\u5230-11732\u65f6\u5d29\u6e83\uff01 \u9012\u5f52\u7684\u7ec8\u6b62\u6761\u4ef6 \u9012\u5f52\u51fd\u6570\u8c03\u7528\u7684\u5de5\u4f5c\u65b9\u5f0f\u901a\u5e38\u4e0e\u666e\u901a\u51fd\u6570\u8c03\u7528\u4e00\u6837\u3002\u7136\u800c\uff0c\u4e0a\u9762\u7684\u7a0b\u5e8f\u8bf4\u660e\u4e86\u9012\u5f52\u51fd\u6570\u6700\u91cd\u8981\u7684\u533a\u522b\uff1a\u9012\u5f52\u51fd\u6570\u5fc5\u987b\u5305\u542b\u9012\u5f52\u7ec8\u6b62\u6761\u4ef6\uff0c\u5426\u5219\u5b83\u4eec\u5c06\u201c\u6c38\u8fdc\u201d\u8fd0\u884c(\u5b9e\u9645\u4e0a\uff0c\u76f4\u5230\u8c03\u7528\u5806\u6808\u8017\u5c3d\u5185\u5b58)\u3002\u9012\u5f52\u7ec8\u6b62\u6761\u4ef6\u2014\u2014\u5f53\u6ee1\u8db3\u8be5\u6761\u4ef6\u65f6\uff0c\u5c06\u5bfc\u81f4\u9012\u5f52\u51fd\u6570\u505c\u6b62\u8c03\u7528\u81ea\u8eab\u3002 \u9012\u5f52\u7ec8\u6b62\u6761\u4ef6\u901a\u5e38\u6d89\u53ca\u4f7f\u7528if\u8bed\u53e5\u3002\u4e0b\u9762\u662f\u6211\u4eec\u7684\u51fd\u6570\u91cd\u65b0\u8bbe\u8ba1\u4e86\u4e00\u4e2a\u7ec8\u6b62\u6761\u4ef6(\u548c\u4e00\u4e9b\u989d\u5916\u7684\u8f93\u51fa)\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void countDown ( int count ) { std :: cout << \"push \" << count << '\\n' ; if ( count > 1 ) // termination condition countDown ( count -1 ); std :: cout << \"pop \" << count << '\\n' ; } int main () { countDown ( 5 ); return 0 ; } \u8fd0\u884c\u7a0b\u5e8f\uff0c countDown() \u4f1a\u5f00\u59cb\u6253\u5370\uff1a 1 2 3 4 5 push 5 push 4 push 3 push 2 push 1 \u5982\u679c\u4f60\u73b0\u5728\u67e5\u770b\u8c03\u7528\u6808\uff0c\u4f60\u4f1a\u770b\u5230\u4ee5\u4e0b\u5185\u5bb9\uff1a 1 2 3 4 5 6 countDown(1) countDown(2) countDown(3) countDown(4) countDown(5) main() \u7531\u4e8e\u7ec8\u6b62\u6761\u4ef6\u5b58\u5728\uff0c countDown(1) \u4e0d\u8c03\u7528 countDown(0) \u2014\u2014\u76f8\u53cd\uff0c\" if\u8bed\u53e5\"\u4e0d\u6267\u884c\uff0c\u56e0\u6b64\u5b83\u6253\u5370\" pop 1 \"\uff0c\u7136\u540e\u7ec8\u6b62\u3002\u6b64\u65f6\uff0c\u4ece\u6808\u4e2d\u5f39\u51fa countDown(1) \uff0c\u63a7\u5236\u8fd4\u56de countDown(2) \u3002 countDown(2) \u5728 countDown(1) \u88ab\u8c03\u7528\u540e\u7ee7\u7eed\u6267\u884c\uff0c\u56e0\u6b64\u5b83\u6253\u5370\" pop 2 \"\u7136\u540e\u7ec8\u6b62\u3002\u9012\u5f52\u51fd\u6570\u8c03\u7528\u968f\u540e\u4ece\u5806\u6808\u4e2d\u5f39\u51fa\uff0c\u76f4\u5230\u6240\u6709\u7684 countDown \u5b9e\u4f8b\u90fd\u88ab\u5220\u9664\u3002 \u56e0\u6b64\uff0c\u7a0b\u5e8f\u6253\u5370\u7684\u5b8c\u6574\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 push 5 push 4 push 3 push 2 push 1 pop 1 pop 2 pop 3 pop 4 pop 5 \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u201cpush\u201d\u8f93\u51fa\u662f\u6309\u524d\u5411\u987a\u5e8f\u53d1\u751f\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u53d1\u751f\u5728\u9012\u5f52\u51fd\u6570\u8c03\u7528\u4e4b\u524d\u3002\u201cpop\u201d\u8f93\u51fa\u4ee5\u76f8\u53cd\u7684\u987a\u5e8f\u51fa\u73b0\uff0c\u56e0\u4e3a\u5b83\u4eec\u51fa\u73b0\u5728\u9012\u5f52\u51fd\u6570\u8c03\u7528\u4e4b\u540e\uff0c\u56e0\u4e3a\u51fd\u6570\u4ece\u6808\u4e2d\u5f39\u51fa(\u5176\u53d1\u751f\u7684\u987a\u5e8f\u4e0e\u653e\u5165\u5b83\u4eec\u7684\u987a\u5e8f\u76f8\u53cd)\u3002 \u66f4\u6709\u7528\u7684\u4f8b\u5b50 \u73b0\u5728\u6211\u4eec\u5df2\u7ecf\u8ba8\u8bba\u4e86\u9012\u5f52\u51fd\u6570\u8c03\u7528\u7684\u57fa\u672c\u673a\u5236\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u53e6\u4e00\u4e2a\u66f4\u5178\u578b\u7684\u9012\u5f52\u51fd\u6570: 1 2 3 4 5 6 7 8 9 10 11 // return the sum of all the integers between 1 (inclusive) and sumto (inclusive) // returns 0 for negative numbers int sumTo ( int sumto ) { if ( sumto <= 0 ) return 0 ; // base case (termination condition) when user passed in an unexpected argument (0 or negative) if ( sumto == 1 ) return 1 ; // normal base case (termination condition) return sumTo ( sumto - 1 ) + sumto ; // recursive function call } \u4ec5\u901a\u8fc7\u89c2\u5bdf\u9012\u5f52\u7a0b\u5e8f\u901a\u5e38\u5f88\u96be\u7406\u89e3\u9012\u5f52\u8c03\u7528\u3002\u5f53\u6211\u4eec\u8c03\u7528\u5177\u6709\u7279\u5b9a\u503c\u7684\u9012\u5f52\u51fd\u6570\u65f6\uff0c\u770b\u770b\u4f1a\u53d1\u751f\u4ec0\u4e48\u901a\u5e38\u662f\u6709\u6307\u5bfc\u610f\u4e49\u7684\u3002\u8ba9\u6211\u4eec\u770b\u770b\u5f53\u6211\u4eec\u8c03\u7528\u8fd9\u4e2a\u53c2\u6570\u4e3a sumto = 5 \u7684\u51fd\u6570\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002 1 2 3 4 5 sumTo(5) called, 5 <= 1 is false, so we return sumTo(4) + 5. sumTo(4) called, 4 <= 1 is false, so we return sumTo(3) + 4. sumTo(3) called, 3 <= 1 is false, so we return sumTo(2) + 3. sumTo(2) called, 2 <= 1 is false, so we return sumTo(1) + 2. sumTo(1) called, 1 <= 1 is true, so we return 1. This is the termination condition. \u73b0\u5728\u6211\u4eec\u5c55\u5f00\u8c03\u7528\u6808(\u5728\u8fd4\u56de\u65f6\u5c06\u6bcf\u4e2a\u51fd\u6570\u4ece\u8c03\u7528\u6808\u4e2d\u5f39\u51fa): 1 2 3 4 5 sumTo(1) returns 1. sumTo(2) returns sumTo(1) + 2, which is 1 + 2 = 3. sumTo(3) returns sumTo(2) + 3, which is 3 + 3 = 6. sumTo(4) returns sumTo(3) + 4, which is 6 + 4 = 10. sumTo(5) returns sumTo(4) + 5, which is 10 + 5 = 15. \u8fd9\u91cc\u53ef\u4ee5\u5f88\u660e\u663e\u7684\u770b\u51fa\uff0c\u6bcf\u6b21\u76f8\u52a0\u503c\u4f1a\u9012\u589e1\uff0c\u7136\u540e\u518d\u52a0\u4e0a\u4f20\u5165\u7684\u503c\u3002 \u56e0\u4e3a\u5355\u770b\u9012\u5f52\u51fd\u6570\u53ef\u80fd\u5f88\u96be\u7406\u89e3\uff0c\u6240\u4ee5\u597d\u7684\u6ce8\u91ca\u7279\u522b\u91cd\u8981\u3002 \u6ce8\u610f\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 sumto - 1 \u800c\u4e0d\u662f --sumto \u3002\u8fd9\u662f\u56e0\u4e3a operator-- \u662f\u6709\u526f\u4f5c\u7528\u7684\uff0c\u800c\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u591a\u6b21\u4f7f\u7528\u4e00\u4e2a\u6709\u526f\u4f5c\u7528\u7684\u53d8\u91cf\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u4f7f\u7528 sumto - 1 \u53ef\u4ee5\u907f\u514d\u526f\u4f5c\u7528\uff0c\u4f7f\u5f97 sumto \u53ef\u4ee5\u5b89\u5168\u5730\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u591a\u6b21\u4f7f\u7528\u3002 \u9012\u5f52\u7b97\u6cd5 \u4f7f\u7528\u9012\u5f52\u51fd\u6570\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5982\u4e0b\uff1a\u9996\u5148\u627e\u5230\u95ee\u9898\u5b50\u96c6\u7684\u89e3(\u9012\u5f52\u5730)\uff0c\u7136\u540e\u4fee\u6539\u5b50\u95ee\u9898\u7684\u89e3\u4ee5\u5f97\u5230\u95ee\u9898\u7684\u89e3\u3002\u5728\u4e0a\u8ff0\u7b97\u6cd5\u4e2d\uff0c sumTo(value) \u9996\u5148\u6c42\u89e3 sumTo(value-1) \uff0c\u7136\u540e\u5c06\u53d8\u91cf value \u7684\u503c\u76f8\u52a0\uff0c\u6c42\u51fa sumTo(value) \u7684\u89e3\u3002 \u5728\u8bb8\u591a\u9012\u5f52\u7b97\u6cd5\u4e2d\uff0c\u4e00\u4e9b\u8f93\u5165\u4f1a\u5f97\u5230\u5f88\u7b80\u5355\u7684\u8f93\u51fa\u3002\u4f8b\u5982\uff0c sumTo(1) \u7684\u8f93\u51fa\u4e3a1(\u4f60\u53ef\u4ee5\u5728\u5934\u8111\u4e2d\u8ba1\u7b97)\uff0c\u5e76\u4e14\u6ca1\u6709\u5fc5\u8981\u518d\u8fdb\u4e00\u6b65\u9012\u5f52\u4e86\u3002\u8fd9\u79cd\u60c5\u51b5\u79f0\u4e3a\u2014\u2014\u57fa\u672c\u6761\u4ef6\u3002\u57fa\u672c\u6761\u4ef6\u901a\u5e38\u4f5c\u4e3a\u7b97\u6cd5\u7684\u7ec8\u6b62\u6761\u4ef6\u3002\u901a\u8fc7\u8003\u8651\u8f93\u51650\u30011\u3001\" \"\u3001\"\u6216null\u7684\u8f93\u51fa\uff0c\u901a\u5e38\u53ef\u4ee5\u8bc6\u522b\u4e3a\u57fa\u672c\u6761\u4ef6\u3002 \u6590\u6ce2\u90a3\u5951\u6570\u5217 \u6700\u8457\u540d\u7684\u6570\u5b66\u9012\u5f52\u7b97\u6cd5\u4e4b\u4e00\u662f\u6590\u6ce2\u90a3\u5951\u6570\u5217\u3002\u6590\u6ce2\u90a3\u5951\u6570\u5217\u51fa\u73b0\u5728\u81ea\u7136\u754c\u7684\u8bb8\u591a\u5730\u65b9\uff0c\u5982\u6811\u6728\u7684\u5206\u679d\u3001\u8d1d\u58f3\u7684\u87ba\u65cb\u5f62\u3001\u83e0\u841d\u7684\u679c\u5b9e\u3001\u4e0d\u5377\u66f2\u7684\u8568\u7c7b\u53f6\u5b50\u548c\u677e\u679c\u7684\u6392\u5217\u3002 \u8fd9\u662f\u4e00\u5f20\u6590\u6ce2\u90a3\u5951\u87ba\u65cb\u56fe: \u6bcf\u4e00\u4e2a\u6590\u6ce2\u90a3\u5951\u6570\u5217\u90fd\u662f\u8be5\u6570\u5217\u6240\u5728\u6b63\u65b9\u5f62\u7684\u8fb9\u957f\u3002 \u6590\u6ce2\u90a3\u5951\u6570\u5217\u5728\u6570\u5b66\u4e0a\u5b9a\u4e49\u4e3a: 1 2 3 4 5 F(n) = 0 if n = 0 1 if n = 1 f(n-1) + f(n-2) if n > 1 \u56e0\u6b64\uff0c\u7f16\u5199\u4e00\u4e2a(\u4e0d\u662f\u5f88\u9ad8\u6548\u7684)\u9012\u5f52\u51fd\u6570\u6765\u8ba1\u7b97\u7b2cn\u4e2a\u6590\u6ce2\u90a3\u5951\u6570\u662f\u76f8\u5f53\u7b80\u5355\u7684: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int fibonacci ( int count ) { if ( count == 0 ) return 0 ; // base case (termination condition) if ( count == 1 ) return 1 ; // base case (termination condition) return fibonacci ( count -1 ) + fibonacci ( count -2 ); } // And a main program to display the first 13 Fibonacci numbers int main () { for ( int count { 0 }; count < 13 ; ++ count ) std :: cout << fibonacci ( count ) << ' ' ; return 0 ; } \u8fd0\u884c\u8be5\u7a0b\u5e8f\u4f1a\u4ea7\u751f\u4ee5\u4e0b\u7ed3\u679c: 1 0 1 1 2 3 5 8 13 21 34 55 89 144 \u4f60\u4f1a\u6ce8\u610f\u5230\uff0c\u8fd9\u6b63\u662f\u6590\u6ce2\u90a3\u5951\u87ba\u65cb\u56fe\u4e2d\u51fa\u73b0\u7684\u6570\u5b57\u3002 \u8bb0\u5fc6\u5316\u7b97\u6cd5 \u4e0a\u9762\u7684\u9012\u5f52\u6590\u6ce2\u90a3\u5951\u7b97\u6cd5\u4e0d\u662f\u5f88\u9ad8\u6548\uff0c\u90e8\u5206\u539f\u56e0\u662f\u6bcf\u6b21\u8c03\u7528\u4e00\u4e2a\u6590\u6ce2\u90a3\u5951\u90fd\u4f1a\u5bfc\u81f4\u53e6\u5916\u4e24\u4e2a\u6590\u6ce2\u90a3\u5951\u8c03\u7528\u3002\u8fd9\u5c06\u4ea7\u751f\u6307\u6570\u7ea7\u7684\u51fd\u6570\u8c03\u7528\u6b21\u6570(\u5b9e\u9645\u4e0a\uff0c\u4e0a\u9762\u7684\u793a\u4f8b\u8c03\u7528 fibonacci() 1205\u6b21!)\u6709\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u51cf\u5c11\u5fc5\u8981\u7684\u8c03\u7528\u6b21\u6570\u3002\u4e00\u79cd\u662f\u53eb\u505a \u8bb0\u5fc6\u5316 \u7684\u6280\u672f\uff0c\u5c06\u5f00\u9500\u5f88\u5927\u7684\u51fd\u6570\u8c03\u7528\u7684\u7ed3\u679c\u7f13\u5b58\u8d77\u6765\uff0c\u4ee5\u4fbf\u5728\u518d\u6b21\u51fa\u73b0\u76f8\u540c\u7684\u8f93\u5165\u65f6\u8fd4\u56de\u7ed3\u679c\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u8bb0\u5fc6\u7248\u7684\u9012\u5f52\u6590\u6ce2\u90a3\u5951\u7b97\u6cd5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include // h/t to potterman28wxcv for a variant of this code int fibonacci ( int count ) { // We'll use a static std::vector to cache calculated results static std :: vector results { 0 , 1 }; // If we've already seen this count, then use the cache'd result if ( count < static_cast < int > ( std :: size ( results ))) return results [ count ]; // Otherwise calculate the new result and add it results . push_back ( fibonacci ( count - 1 ) + fibonacci ( count - 2 )); return results [ count ]; } // And a main program to display the first 13 Fibonacci numbers int main () { for ( int count { 0 }; count < 13 ; ++ count ) std :: cout << fibonacci ( count ) << ' ' ; return 0 ; } \u8fd9\u4e2a\u8bb0\u5fc6\u7248\u672c\u8fdb\u884c\u4e8635\u6b21\u51fd\u6570\u8c03\u7528\uff0c\u6bd4\u539f\u59cb\u7b97\u6cd5\u76841205\u6b21\u597d\u5f97\u591a\u3002 \u9012\u5f52\u548c\u8fed\u4ee3 \u5173\u4e8e\u9012\u5f52\u51fd\u6570\u7ecf\u5e38\u88ab\u95ee\u5230\u7684\u4e00\u4e2a\u95ee\u9898\u662f\uff0c\u201c\u5982\u679c\u53ef\u4ee5\u8fed\u4ee3\u5730\u6267\u884c\u8bb8\u591a\u76f8\u540c\u7684\u4efb\u52a1(\u4f7f\u7528for \u5faa\u73af\u6216while\u5faa\u73af)\uff0c\u4e3a\u4ec0\u4e48\u8fd8\u8981\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u5462?\u201d\u4e8b\u5b9e\u8bc1\u660e\uff0c\u4f60\u603b\u662f\u53ef\u4ee5\u8fed\u4ee3\u5730\u89e3\u51b3\u9012\u5f52\u95ee\u9898\u2014\u2014\u7136\u800c\uff0c\u5bf9\u4e8e\u590d\u6742\u7684\u95ee\u9898\uff0c\u9012\u5f52\u7248\u672c\u7684\u7f16\u5199(\u548c\u8bfb\u53d6)\u901a\u5e38\u8981\u7b80\u5355\u5f97\u591a\u3002\u4f8b\u5982\uff0c\u867d\u7136\u53ef\u4ee5\u8fed\u4ee3\u5730\u7f16\u5199Fibonacci\u51fd\u6570\uff0c\u4f46\u8fd9\u6709\u70b9\u56f0\u96be!(\u8bd5\u4e00\u8bd5!) \u8fed\u4ee3\u51fd\u6570(\u4f7f\u7528for\u5faa\u73af\u6216while\u5faa\u73af\u7684\u51fd\u6570)\u51e0\u4e4e\u603b\u662f\u6bd4\u9012\u5f52\u51fd\u6570\u66f4\u9ad8\u6548\u3002\u8fd9\u662f\u56e0\u4e3a\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u6808\u5e27\u7684\u63a8\u5165\u548c\u5f39\u51fa\u90fd\u4f1a\u4ea7\u751f\u4e00\u5b9a\u7684\u5f00\u9500\u3002\u8fed\u4ee3\u51fd\u6570\u907f\u514d\u4e86\u8fd9\u79cd\u5f00\u9500\u3002 \u8fd9\u5e76\u4e0d\u662f\u8bf4\u8fed\u4ee3\u51fd\u6570\u603b\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u6709\u65f6\u5019\uff0c\u51fd\u6570\u7684\u9012\u5f52\u5b9e\u73b0\u662f\u5982\u6b64\u7684\u7b80\u6d01\u548c\u5bb9\u6613\u9075\u5faa\uff0c\u4ee5\u81f3\u4e8e\u4e3a\u4e86\u53ef\u7ef4\u62a4\u6027\u7684\u597d\u5904\uff0c\u4ea7\u751f\u4e00\u70b9\u989d\u5916\u7684\u5f00\u9500\u662f\u503c\u5f97\u7684\uff0c\u7279\u522b\u662f\u5728\u7b97\u6cd5\u4e0d\u9700\u8981\u591a\u6b21\u9012\u5f52\u6765\u627e\u5230\u4e00\u4e2a\u89e3\u51b3\u65b9\u6848\u7684\u60c5\u51b5\u4e0b\u3002 \u5728\u6ee1\u8db3\u4e0b\u9762\u6761\u4ef6\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u9012\u5f52\u901a\u5e38\u662f\u66f4\u597d\u7684\u9009\u62e9\uff1a \u5f53\u9012\u5f52\u4ee3\u7801\u66f4\u5bb9\u6613\u7f16\u5199\u65f6\uff1b \u9012\u5f52\u7684\u6df1\u5ea6\u6709\u9650(\u4f8b\u5982\uff0c\u5b9e\u9645\u4e0a\u6ca1\u529e\u6cd5\u7f16\u5199\u5341\u4e07\u5c42\u9012\u5f52\u7684\u7b97\u6cd5\uff09\uff1b \u5f53\u8fed\u4ee3\u7248\u672c\u7684\u7b97\u6cd5\u9700\u8981\u624b\u5de5\u7ba1\u7406\u6808\u65f6\uff1b \u6027\u80fd\u8981\u6c42\u4e0d\u9ad8 \u4f46\u662f\uff0c\u5982\u679c\u9012\u5f52\u7b97\u6cd5\u66f4\u5bb9\u6613\u5b9e\u73b0\uff0c\u90a3\u4e48\u53ef\u4ee5\u5148\u9012\u5f52\u5730\u5f00\u59cb\uff0c\u7136\u540e\u518d\u4f18\u5316\u4e3a\u8fed\u4ee3\u7b97\u6cd5\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e00\u822c\u6765\u8bf4\uff0c\u8fed\u4ee3\u6bd4\u9012\u5f52\u66f4\u53d7\u6b22\u8fce\uff0c\u9664\u975e\u9012\u5f52\u786e\u5b9e\u6709\u610f\u4e49\u3002","title":"12.4 - \u9012\u5f52"},{"location":"12-4-recursion/#_1","text":"\u9012\u5f52\u51fd\u6570\u8c03\u7528\u7684\u5de5\u4f5c\u65b9\u5f0f\u901a\u5e38\u4e0e\u666e\u901a\u51fd\u6570\u8c03\u7528\u4e00\u6837\u3002\u7136\u800c\uff0c\u4e0a\u9762\u7684\u7a0b\u5e8f\u8bf4\u660e\u4e86\u9012\u5f52\u51fd\u6570\u6700\u91cd\u8981\u7684\u533a\u522b\uff1a\u9012\u5f52\u51fd\u6570\u5fc5\u987b\u5305\u542b\u9012\u5f52\u7ec8\u6b62\u6761\u4ef6\uff0c\u5426\u5219\u5b83\u4eec\u5c06\u201c\u6c38\u8fdc\u201d\u8fd0\u884c(\u5b9e\u9645\u4e0a\uff0c\u76f4\u5230\u8c03\u7528\u5806\u6808\u8017\u5c3d\u5185\u5b58)\u3002\u9012\u5f52\u7ec8\u6b62\u6761\u4ef6\u2014\u2014\u5f53\u6ee1\u8db3\u8be5\u6761\u4ef6\u65f6\uff0c\u5c06\u5bfc\u81f4\u9012\u5f52\u51fd\u6570\u505c\u6b62\u8c03\u7528\u81ea\u8eab\u3002 \u9012\u5f52\u7ec8\u6b62\u6761\u4ef6\u901a\u5e38\u6d89\u53ca\u4f7f\u7528if\u8bed\u53e5\u3002\u4e0b\u9762\u662f\u6211\u4eec\u7684\u51fd\u6570\u91cd\u65b0\u8bbe\u8ba1\u4e86\u4e00\u4e2a\u7ec8\u6b62\u6761\u4ef6(\u548c\u4e00\u4e9b\u989d\u5916\u7684\u8f93\u51fa)\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void countDown ( int count ) { std :: cout << \"push \" << count << '\\n' ; if ( count > 1 ) // termination condition countDown ( count -1 ); std :: cout << \"pop \" << count << '\\n' ; } int main () { countDown ( 5 ); return 0 ; } \u8fd0\u884c\u7a0b\u5e8f\uff0c countDown() \u4f1a\u5f00\u59cb\u6253\u5370\uff1a 1 2 3 4 5 push 5 push 4 push 3 push 2 push 1 \u5982\u679c\u4f60\u73b0\u5728\u67e5\u770b\u8c03\u7528\u6808\uff0c\u4f60\u4f1a\u770b\u5230\u4ee5\u4e0b\u5185\u5bb9\uff1a 1 2 3 4 5 6 countDown(1) countDown(2) countDown(3) countDown(4) countDown(5) main() \u7531\u4e8e\u7ec8\u6b62\u6761\u4ef6\u5b58\u5728\uff0c countDown(1) \u4e0d\u8c03\u7528 countDown(0) \u2014\u2014\u76f8\u53cd\uff0c\" if\u8bed\u53e5\"\u4e0d\u6267\u884c\uff0c\u56e0\u6b64\u5b83\u6253\u5370\" pop 1 \"\uff0c\u7136\u540e\u7ec8\u6b62\u3002\u6b64\u65f6\uff0c\u4ece\u6808\u4e2d\u5f39\u51fa countDown(1) \uff0c\u63a7\u5236\u8fd4\u56de countDown(2) \u3002 countDown(2) \u5728 countDown(1) \u88ab\u8c03\u7528\u540e\u7ee7\u7eed\u6267\u884c\uff0c\u56e0\u6b64\u5b83\u6253\u5370\" pop 2 \"\u7136\u540e\u7ec8\u6b62\u3002\u9012\u5f52\u51fd\u6570\u8c03\u7528\u968f\u540e\u4ece\u5806\u6808\u4e2d\u5f39\u51fa\uff0c\u76f4\u5230\u6240\u6709\u7684 countDown \u5b9e\u4f8b\u90fd\u88ab\u5220\u9664\u3002 \u56e0\u6b64\uff0c\u7a0b\u5e8f\u6253\u5370\u7684\u5b8c\u6574\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 push 5 push 4 push 3 push 2 push 1 pop 1 pop 2 pop 3 pop 4 pop 5 \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u201cpush\u201d\u8f93\u51fa\u662f\u6309\u524d\u5411\u987a\u5e8f\u53d1\u751f\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u53d1\u751f\u5728\u9012\u5f52\u51fd\u6570\u8c03\u7528\u4e4b\u524d\u3002\u201cpop\u201d\u8f93\u51fa\u4ee5\u76f8\u53cd\u7684\u987a\u5e8f\u51fa\u73b0\uff0c\u56e0\u4e3a\u5b83\u4eec\u51fa\u73b0\u5728\u9012\u5f52\u51fd\u6570\u8c03\u7528\u4e4b\u540e\uff0c\u56e0\u4e3a\u51fd\u6570\u4ece\u6808\u4e2d\u5f39\u51fa(\u5176\u53d1\u751f\u7684\u987a\u5e8f\u4e0e\u653e\u5165\u5b83\u4eec\u7684\u987a\u5e8f\u76f8\u53cd)\u3002","title":"\u9012\u5f52\u7684\u7ec8\u6b62\u6761\u4ef6"},{"location":"12-4-recursion/#_2","text":"\u73b0\u5728\u6211\u4eec\u5df2\u7ecf\u8ba8\u8bba\u4e86\u9012\u5f52\u51fd\u6570\u8c03\u7528\u7684\u57fa\u672c\u673a\u5236\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u53e6\u4e00\u4e2a\u66f4\u5178\u578b\u7684\u9012\u5f52\u51fd\u6570: 1 2 3 4 5 6 7 8 9 10 11 // return the sum of all the integers between 1 (inclusive) and sumto (inclusive) // returns 0 for negative numbers int sumTo ( int sumto ) { if ( sumto <= 0 ) return 0 ; // base case (termination condition) when user passed in an unexpected argument (0 or negative) if ( sumto == 1 ) return 1 ; // normal base case (termination condition) return sumTo ( sumto - 1 ) + sumto ; // recursive function call } \u4ec5\u901a\u8fc7\u89c2\u5bdf\u9012\u5f52\u7a0b\u5e8f\u901a\u5e38\u5f88\u96be\u7406\u89e3\u9012\u5f52\u8c03\u7528\u3002\u5f53\u6211\u4eec\u8c03\u7528\u5177\u6709\u7279\u5b9a\u503c\u7684\u9012\u5f52\u51fd\u6570\u65f6\uff0c\u770b\u770b\u4f1a\u53d1\u751f\u4ec0\u4e48\u901a\u5e38\u662f\u6709\u6307\u5bfc\u610f\u4e49\u7684\u3002\u8ba9\u6211\u4eec\u770b\u770b\u5f53\u6211\u4eec\u8c03\u7528\u8fd9\u4e2a\u53c2\u6570\u4e3a sumto = 5 \u7684\u51fd\u6570\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002 1 2 3 4 5 sumTo(5) called, 5 <= 1 is false, so we return sumTo(4) + 5. sumTo(4) called, 4 <= 1 is false, so we return sumTo(3) + 4. sumTo(3) called, 3 <= 1 is false, so we return sumTo(2) + 3. sumTo(2) called, 2 <= 1 is false, so we return sumTo(1) + 2. sumTo(1) called, 1 <= 1 is true, so we return 1. This is the termination condition. \u73b0\u5728\u6211\u4eec\u5c55\u5f00\u8c03\u7528\u6808(\u5728\u8fd4\u56de\u65f6\u5c06\u6bcf\u4e2a\u51fd\u6570\u4ece\u8c03\u7528\u6808\u4e2d\u5f39\u51fa): 1 2 3 4 5 sumTo(1) returns 1. sumTo(2) returns sumTo(1) + 2, which is 1 + 2 = 3. sumTo(3) returns sumTo(2) + 3, which is 3 + 3 = 6. sumTo(4) returns sumTo(3) + 4, which is 6 + 4 = 10. sumTo(5) returns sumTo(4) + 5, which is 10 + 5 = 15. \u8fd9\u91cc\u53ef\u4ee5\u5f88\u660e\u663e\u7684\u770b\u51fa\uff0c\u6bcf\u6b21\u76f8\u52a0\u503c\u4f1a\u9012\u589e1\uff0c\u7136\u540e\u518d\u52a0\u4e0a\u4f20\u5165\u7684\u503c\u3002 \u56e0\u4e3a\u5355\u770b\u9012\u5f52\u51fd\u6570\u53ef\u80fd\u5f88\u96be\u7406\u89e3\uff0c\u6240\u4ee5\u597d\u7684\u6ce8\u91ca\u7279\u522b\u91cd\u8981\u3002 \u6ce8\u610f\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 sumto - 1 \u800c\u4e0d\u662f --sumto \u3002\u8fd9\u662f\u56e0\u4e3a operator-- \u662f\u6709\u526f\u4f5c\u7528\u7684\uff0c\u800c\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u591a\u6b21\u4f7f\u7528\u4e00\u4e2a\u6709\u526f\u4f5c\u7528\u7684\u53d8\u91cf\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u4f7f\u7528 sumto - 1 \u53ef\u4ee5\u907f\u514d\u526f\u4f5c\u7528\uff0c\u4f7f\u5f97 sumto \u53ef\u4ee5\u5b89\u5168\u5730\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u591a\u6b21\u4f7f\u7528\u3002","title":"\u66f4\u6709\u7528\u7684\u4f8b\u5b50"},{"location":"12-4-recursion/#_3","text":"\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5982\u4e0b\uff1a\u9996\u5148\u627e\u5230\u95ee\u9898\u5b50\u96c6\u7684\u89e3(\u9012\u5f52\u5730)\uff0c\u7136\u540e\u4fee\u6539\u5b50\u95ee\u9898\u7684\u89e3\u4ee5\u5f97\u5230\u95ee\u9898\u7684\u89e3\u3002\u5728\u4e0a\u8ff0\u7b97\u6cd5\u4e2d\uff0c sumTo(value) \u9996\u5148\u6c42\u89e3 sumTo(value-1) \uff0c\u7136\u540e\u5c06\u53d8\u91cf value \u7684\u503c\u76f8\u52a0\uff0c\u6c42\u51fa sumTo(value) \u7684\u89e3\u3002 \u5728\u8bb8\u591a\u9012\u5f52\u7b97\u6cd5\u4e2d\uff0c\u4e00\u4e9b\u8f93\u5165\u4f1a\u5f97\u5230\u5f88\u7b80\u5355\u7684\u8f93\u51fa\u3002\u4f8b\u5982\uff0c sumTo(1) \u7684\u8f93\u51fa\u4e3a1(\u4f60\u53ef\u4ee5\u5728\u5934\u8111\u4e2d\u8ba1\u7b97)\uff0c\u5e76\u4e14\u6ca1\u6709\u5fc5\u8981\u518d\u8fdb\u4e00\u6b65\u9012\u5f52\u4e86\u3002\u8fd9\u79cd\u60c5\u51b5\u79f0\u4e3a\u2014\u2014\u57fa\u672c\u6761\u4ef6\u3002\u57fa\u672c\u6761\u4ef6\u901a\u5e38\u4f5c\u4e3a\u7b97\u6cd5\u7684\u7ec8\u6b62\u6761\u4ef6\u3002\u901a\u8fc7\u8003\u8651\u8f93\u51650\u30011\u3001\" \"\u3001\"\u6216null\u7684\u8f93\u51fa\uff0c\u901a\u5e38\u53ef\u4ee5\u8bc6\u522b\u4e3a\u57fa\u672c\u6761\u4ef6\u3002","title":"\u9012\u5f52\u7b97\u6cd5"},{"location":"12-4-recursion/#_4","text":"\u6700\u8457\u540d\u7684\u6570\u5b66\u9012\u5f52\u7b97\u6cd5\u4e4b\u4e00\u662f\u6590\u6ce2\u90a3\u5951\u6570\u5217\u3002\u6590\u6ce2\u90a3\u5951\u6570\u5217\u51fa\u73b0\u5728\u81ea\u7136\u754c\u7684\u8bb8\u591a\u5730\u65b9\uff0c\u5982\u6811\u6728\u7684\u5206\u679d\u3001\u8d1d\u58f3\u7684\u87ba\u65cb\u5f62\u3001\u83e0\u841d\u7684\u679c\u5b9e\u3001\u4e0d\u5377\u66f2\u7684\u8568\u7c7b\u53f6\u5b50\u548c\u677e\u679c\u7684\u6392\u5217\u3002 \u8fd9\u662f\u4e00\u5f20\u6590\u6ce2\u90a3\u5951\u87ba\u65cb\u56fe: \u6bcf\u4e00\u4e2a\u6590\u6ce2\u90a3\u5951\u6570\u5217\u90fd\u662f\u8be5\u6570\u5217\u6240\u5728\u6b63\u65b9\u5f62\u7684\u8fb9\u957f\u3002 \u6590\u6ce2\u90a3\u5951\u6570\u5217\u5728\u6570\u5b66\u4e0a\u5b9a\u4e49\u4e3a: 1 2 3 4 5 F(n) = 0 if n = 0 1 if n = 1 f(n-1) + f(n-2) if n > 1 \u56e0\u6b64\uff0c\u7f16\u5199\u4e00\u4e2a(\u4e0d\u662f\u5f88\u9ad8\u6548\u7684)\u9012\u5f52\u51fd\u6570\u6765\u8ba1\u7b97\u7b2cn\u4e2a\u6590\u6ce2\u90a3\u5951\u6570\u662f\u76f8\u5f53\u7b80\u5355\u7684: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int fibonacci ( int count ) { if ( count == 0 ) return 0 ; // base case (termination condition) if ( count == 1 ) return 1 ; // base case (termination condition) return fibonacci ( count -1 ) + fibonacci ( count -2 ); } // And a main program to display the first 13 Fibonacci numbers int main () { for ( int count { 0 }; count < 13 ; ++ count ) std :: cout << fibonacci ( count ) << ' ' ; return 0 ; } \u8fd0\u884c\u8be5\u7a0b\u5e8f\u4f1a\u4ea7\u751f\u4ee5\u4e0b\u7ed3\u679c: 1 0 1 1 2 3 5 8 13 21 34 55 89 144 \u4f60\u4f1a\u6ce8\u610f\u5230\uff0c\u8fd9\u6b63\u662f\u6590\u6ce2\u90a3\u5951\u87ba\u65cb\u56fe\u4e2d\u51fa\u73b0\u7684\u6570\u5b57\u3002","title":"\u6590\u6ce2\u90a3\u5951\u6570\u5217"},{"location":"12-4-recursion/#_5","text":"\u4e0a\u9762\u7684\u9012\u5f52\u6590\u6ce2\u90a3\u5951\u7b97\u6cd5\u4e0d\u662f\u5f88\u9ad8\u6548\uff0c\u90e8\u5206\u539f\u56e0\u662f\u6bcf\u6b21\u8c03\u7528\u4e00\u4e2a\u6590\u6ce2\u90a3\u5951\u90fd\u4f1a\u5bfc\u81f4\u53e6\u5916\u4e24\u4e2a\u6590\u6ce2\u90a3\u5951\u8c03\u7528\u3002\u8fd9\u5c06\u4ea7\u751f\u6307\u6570\u7ea7\u7684\u51fd\u6570\u8c03\u7528\u6b21\u6570(\u5b9e\u9645\u4e0a\uff0c\u4e0a\u9762\u7684\u793a\u4f8b\u8c03\u7528 fibonacci() 1205\u6b21!)\u6709\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u51cf\u5c11\u5fc5\u8981\u7684\u8c03\u7528\u6b21\u6570\u3002\u4e00\u79cd\u662f\u53eb\u505a \u8bb0\u5fc6\u5316 \u7684\u6280\u672f\uff0c\u5c06\u5f00\u9500\u5f88\u5927\u7684\u51fd\u6570\u8c03\u7528\u7684\u7ed3\u679c\u7f13\u5b58\u8d77\u6765\uff0c\u4ee5\u4fbf\u5728\u518d\u6b21\u51fa\u73b0\u76f8\u540c\u7684\u8f93\u5165\u65f6\u8fd4\u56de\u7ed3\u679c\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u8bb0\u5fc6\u7248\u7684\u9012\u5f52\u6590\u6ce2\u90a3\u5951\u7b97\u6cd5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include // h/t to potterman28wxcv for a variant of this code int fibonacci ( int count ) { // We'll use a static std::vector to cache calculated results static std :: vector results { 0 , 1 }; // If we've already seen this count, then use the cache'd result if ( count < static_cast < int > ( std :: size ( results ))) return results [ count ]; // Otherwise calculate the new result and add it results . push_back ( fibonacci ( count - 1 ) + fibonacci ( count - 2 )); return results [ count ]; } // And a main program to display the first 13 Fibonacci numbers int main () { for ( int count { 0 }; count < 13 ; ++ count ) std :: cout << fibonacci ( count ) << ' ' ; return 0 ; } \u8fd9\u4e2a\u8bb0\u5fc6\u7248\u672c\u8fdb\u884c\u4e8635\u6b21\u51fd\u6570\u8c03\u7528\uff0c\u6bd4\u539f\u59cb\u7b97\u6cd5\u76841205\u6b21\u597d\u5f97\u591a\u3002","title":"\u8bb0\u5fc6\u5316\u7b97\u6cd5"},{"location":"12-4-recursion/#_6","text":"\u5173\u4e8e\u9012\u5f52\u51fd\u6570\u7ecf\u5e38\u88ab\u95ee\u5230\u7684\u4e00\u4e2a\u95ee\u9898\u662f\uff0c\u201c\u5982\u679c\u53ef\u4ee5\u8fed\u4ee3\u5730\u6267\u884c\u8bb8\u591a\u76f8\u540c\u7684\u4efb\u52a1(\u4f7f\u7528for \u5faa\u73af\u6216while\u5faa\u73af)\uff0c\u4e3a\u4ec0\u4e48\u8fd8\u8981\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u5462?\u201d\u4e8b\u5b9e\u8bc1\u660e\uff0c\u4f60\u603b\u662f\u53ef\u4ee5\u8fed\u4ee3\u5730\u89e3\u51b3\u9012\u5f52\u95ee\u9898\u2014\u2014\u7136\u800c\uff0c\u5bf9\u4e8e\u590d\u6742\u7684\u95ee\u9898\uff0c\u9012\u5f52\u7248\u672c\u7684\u7f16\u5199(\u548c\u8bfb\u53d6)\u901a\u5e38\u8981\u7b80\u5355\u5f97\u591a\u3002\u4f8b\u5982\uff0c\u867d\u7136\u53ef\u4ee5\u8fed\u4ee3\u5730\u7f16\u5199Fibonacci\u51fd\u6570\uff0c\u4f46\u8fd9\u6709\u70b9\u56f0\u96be!(\u8bd5\u4e00\u8bd5!) \u8fed\u4ee3\u51fd\u6570(\u4f7f\u7528for\u5faa\u73af\u6216while\u5faa\u73af\u7684\u51fd\u6570)\u51e0\u4e4e\u603b\u662f\u6bd4\u9012\u5f52\u51fd\u6570\u66f4\u9ad8\u6548\u3002\u8fd9\u662f\u56e0\u4e3a\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u6808\u5e27\u7684\u63a8\u5165\u548c\u5f39\u51fa\u90fd\u4f1a\u4ea7\u751f\u4e00\u5b9a\u7684\u5f00\u9500\u3002\u8fed\u4ee3\u51fd\u6570\u907f\u514d\u4e86\u8fd9\u79cd\u5f00\u9500\u3002 \u8fd9\u5e76\u4e0d\u662f\u8bf4\u8fed\u4ee3\u51fd\u6570\u603b\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u6709\u65f6\u5019\uff0c\u51fd\u6570\u7684\u9012\u5f52\u5b9e\u73b0\u662f\u5982\u6b64\u7684\u7b80\u6d01\u548c\u5bb9\u6613\u9075\u5faa\uff0c\u4ee5\u81f3\u4e8e\u4e3a\u4e86\u53ef\u7ef4\u62a4\u6027\u7684\u597d\u5904\uff0c\u4ea7\u751f\u4e00\u70b9\u989d\u5916\u7684\u5f00\u9500\u662f\u503c\u5f97\u7684\uff0c\u7279\u522b\u662f\u5728\u7b97\u6cd5\u4e0d\u9700\u8981\u591a\u6b21\u9012\u5f52\u6765\u627e\u5230\u4e00\u4e2a\u89e3\u51b3\u65b9\u6848\u7684\u60c5\u51b5\u4e0b\u3002 \u5728\u6ee1\u8db3\u4e0b\u9762\u6761\u4ef6\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u9012\u5f52\u901a\u5e38\u662f\u66f4\u597d\u7684\u9009\u62e9\uff1a \u5f53\u9012\u5f52\u4ee3\u7801\u66f4\u5bb9\u6613\u7f16\u5199\u65f6\uff1b \u9012\u5f52\u7684\u6df1\u5ea6\u6709\u9650(\u4f8b\u5982\uff0c\u5b9e\u9645\u4e0a\u6ca1\u529e\u6cd5\u7f16\u5199\u5341\u4e07\u5c42\u9012\u5f52\u7684\u7b97\u6cd5\uff09\uff1b \u5f53\u8fed\u4ee3\u7248\u672c\u7684\u7b97\u6cd5\u9700\u8981\u624b\u5de5\u7ba1\u7406\u6808\u65f6\uff1b \u6027\u80fd\u8981\u6c42\u4e0d\u9ad8 \u4f46\u662f\uff0c\u5982\u679c\u9012\u5f52\u7b97\u6cd5\u66f4\u5bb9\u6613\u5b9e\u73b0\uff0c\u90a3\u4e48\u53ef\u4ee5\u5148\u9012\u5f52\u5730\u5f00\u59cb\uff0c\u7136\u540e\u518d\u4f18\u5316\u4e3a\u8fed\u4ee3\u7b97\u6cd5\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e00\u822c\u6765\u8bf4\uff0c\u8fed\u4ee3\u6bd4\u9012\u5f52\u66f4\u53d7\u6b22\u8fce\uff0c\u9664\u975e\u9012\u5f52\u786e\u5b9e\u6709\u610f\u4e49\u3002","title":"\u9012\u5f52\u548c\u8fed\u4ee3"},{"location":"12-5-command-line-arguments/","tags":["argument"],"text":"Key Takeaway \u547d\u4ee4\u884c\u53c2\u6570\u7684\u7528\u9014 \u5728 0.5 -- Introduction to the compiler, linker, and libraries \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u7f16\u8bd1\u548c\u94fe\u63a5\u540e\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u3002\u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u5219\u4f1a\u4ece main() \u51fd\u6570\u7684\u9876\u90e8\u5f00\u59cb\u8fd0\u884c\u3002 main() \u51fd\u6570\u5b9a\u4e49\u5982\u4e0b\uff1a 1 int main () \u6ce8\u610f\uff0c\u8fd9\u91cc\u7684 main() \u51fd\u6570\u6ca1\u6709 \u5f62\u53c2 \u3002\u4f46\u662f\u5f88\u591a\u7a0b\u5e8f\u5176\u5b9e\u9700\u8981\u57fa\u4e8e\u8f93\u5165\u6765\u5224\u65ad\u5982\u4f55\u5f00\u59cb\u5de5\u4f5c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a\u8bfb\u53d6\u56fe\u7247\u4f5c\u4e3a\u7f29\u7565\u56fe\u7684\u7a0b\u5e8f Thumbnail \u3002\u90a3\u4e48 Thumbnail \u5e94\u8be5\u8bfb\u53d6\u54ea\u4e2a\u56fe\u7247\u5e76\u5904\u7406\u5462\uff1f\u7528\u6237\u5fc5\u987b\u6709\u529e\u6cd5\u544a\u8bc9\u7a0b\u5e8f\u5e94\u8be5\u6253\u5f00\u54ea\u4e2a\u6587\u4ef6\uff0c\u6b64\u65f6\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // Program: Thumbnail #include #include int main () { std :: cout << \"Please enter an image filename to create a thumbnail for: \" ; std :: string filename {}; std :: cin >> filename ; // open image file // create thumbnail // output thumbnail } \u4f46\u662f\uff0c\u8fd9\u4e48\u505a\u6709\u4e00\u4e2a\u6f5c\u5728\u7684\u95ee\u9898\u3002\u6bcf\u6b21\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u90fd\u5fc5\u987b\u7b49\u5f85\u7528\u6237\u7684\u8f93\u5165\u624d\u80fd\u7ee7\u7eed\u5de5\u4f5c\u3002\u5982\u679c\u6211\u4eec\u6bcf\u6b21\u90fd\u662f\u624b\u52a8\u542f\u52a8\u8be5\u7a0b\u5e8f\u7684\u8bdd\uff0c\u8fd9\u4f3c\u4e4e\u4e5f\u6ca1\u4ec0\u4e48\u95ee\u9898\u3002\u4f46\u662f\u5f88\u591a\u65f6\u5019\u8fd9\u4e48\u505a\u662f\u4e0d\u5408\u9002\u7684\uff0c\u4f8b\u5982\u4f60\u9700\u8981\u5bf9\u591a\u4e2a\u6587\u4ef6\u6267\u884c\u8be5\u7a0b\u5e8f\u6216\u8005\u4ece\u53e6\u5916\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\u8c03\u7528\u8be5\u7a0b\u5e8f\u3002 \u8ba9\u6211\u4eec\u8fdb\u4e00\u6b65\u7814\u7a76\u8fd9\u4e9b\u6848\u4f8b\u3002 \u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u4f60\u5e0c\u671b\u4e3a\u7ed9\u5b9a\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u56fe\u50cf\u6587\u4ef6\u521b\u5efa\u7f29\u7565\u56fe\u3002\u4f60\u4f1a\u600e\u4e48\u505a?\u53ea\u8981\u76ee\u5f55\u4e2d\u6709\u56fe\u50cf\uff0c\u4f60\u5c31\u53ef\u4ee5\u591a\u6b21\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u624b\u52a8\u952e\u5165\u6bcf\u4e2a\u6587\u4ef6\u540d\u3002\u7136\u800c\uff0c\u5982\u679c\u6709\u6570\u767e\u4e2a\u56fe\u50cf\uff0c\u8fd9\u53ef\u80fd\u9700\u8981\u4e00\u6574\u5929\uff01\u4e00\u4e2a\u597d\u7684\u89e3\u51b3\u65b9\u6848\u662f\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\u5faa\u73af\u904d\u5386\u76ee\u5f55\u4e2d\u7684\u6bcf\u4e2a\u6587\u4ef6\u540d\uff0c\u4e3a\u6bcf\u4e2a\u6587\u4ef6\u8c03\u7528\u4e00\u6b21 Thumbnail \u3002 \u73b0\u5728\u8003\u8651\u8fd9\u6837\u4e00\u4e2a\u60c5\u51b5\uff0c\u4f60\u6b63\u5728\u8fd0\u8425\u4e00\u4e2a\u7f51\u7ad9\uff0c\u4f60\u60f3\u8ba9\u4f60\u7684\u7f51\u7ad9\u5728\u7528\u6237\u6bcf\u6b21\u4e0a\u4f20\u56fe\u7247\u5230\u4f60\u7684\u7f51\u7ad9\u65f6\u521b\u5efa\u4e00\u4e2a\u7f29\u7565\u56fe\u3002\u8fd9\u4e2a\u7a0b\u5e8f\u6ca1\u6709\u8bbe\u7f6e\u4e3a\u63a5\u53d7\u6765\u81ea\u7f51\u7edc\u7684\u8f93\u5165\uff0c\u90a3\u4e48\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e0a\u4f20\u8005\u5982\u4f55\u8f93\u5165\u6587\u4ef6\u540d\u5462?\u4e00\u4e2a\u597d\u7684\u89e3\u51b3\u65b9\u6848\u662f\u8ba9\u4f60\u7684web\u670d\u52a1\u5668\u5728\u4e0a\u4f20\u540e\u81ea\u52a8\u8c03\u7528 Thumbnail \u3002 \u5728\u8fd9\u4e24\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u786e\u5b9e\u9700\u8981\u4e00\u79cd\u65b9\u6cd5\uff0c\u8ba9\u4e00\u4e2a\u5916\u90e8\u7a0b\u5e8f\u5728 Thumbnail \u542f\u52a8\u65f6\u5c06\u6587\u4ef6\u540d\u4f5c\u4e3a\u8f93\u5165\u4f20\u9012\u7ed9 Thumbnail \u7a0b\u5e8f\uff0c\u800c\u4e0d\u662f\u8ba9 Thumbnail \u5728\u542f\u52a8\u540e\u7b49\u5f85\u7528\u6237\u8f93\u5165\u6587\u4ef6\u540d\u3002 \u547d\u4ee4\u884c\u53c2\u6570 \u662f\u53ef\u9009\u7684\u5b57\u7b26\u4e32\u53c2\u6570\uff0c\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u7531\u64cd\u4f5c\u7cfb\u7edf\u4f20\u9012\u7ed9\u5b83\u3002\u7136\u540e\u7a0b\u5e8f\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u4f5c\u4e3a\u8f93\u5165(\u6216\u5ffd\u7565\u5b83\u4eec)\u3002\u5c31\u50cf\u51fd\u6570\u5f62\u53c2\u4e3a\u51fd\u6570\u63d0\u4f9b\u4e86\u5411\u53e6\u4e00\u4e2a\u51fd\u6570\u63d0\u4f9b\u8f93\u5165\u7684\u65b9\u6cd5\u4e00\u6837\uff0c\u547d\u4ee4\u884c\u5b9e\u53c2\u4e3a\u4eba\u4eec\u6216\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u5411\u7a0b\u5e8f\u63d0\u4f9b\u8f93\u5165\u7684\u65b9\u6cd5\u3002 \u4f20\u9012\u547d\u4ee4\u884c\u53c2\u6570 \u53ef\u6267\u884c\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u540d\u79f0\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u3002\u4f8b\u5982\uff0c\u8981\u8fd0\u884cWindows\u673a\u5668\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u53ef\u6267\u884c\u6587\u4ef6\" WordCount \"\uff0c\u4f60\u53ef\u4ee5\u8f93\u5165: 1 WordCount \u5728UNIX\u7c7b\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u4f7f\u7528\uff1a 1 ./WordCount \u7ed9 WordCount \u4f20\u9012\u53c2\u6570\u5f88\u7b80\u5355\uff0c\u53ea\u8981\u5c06\u53c2\u6570\u653e\u5728\u7a0b\u5e8f\u540d\u540e\u9762\u5373\u53ef\uff1a 1 WordCount Myfile.txt \u5f53 WordCount \u6267\u884c\u65f6 Myfile.txt \u4f1a\u4f5c\u4e3a\u547d\u4ee4\u884c\u5b9e\u53c2\u4f20\u5165\uff0c\u5982\u679c\u7a0b\u5e8f\u9700\u8981\u591a\u4e2a\u53c2\u6570\uff0c\u5219\u9700\u8981\u5c06\u5b83\u4eec\u7528\u7a7a\u683c\u9694\u5f00\uff1a 1 WordCount Myfile.txt Myotherfile.txt \u5982\u679c\u4eceIDE\u8fd0\u884c\u7a0b\u5e8f\uff0cIDE\u5e94\u8be5\u63d0\u4f9b\u8f93\u5165\u547d\u4ee4\u884c\u53c2\u6570\u7684\u65b9\u6cd5\u3002 \u5728Microsoft Visual Studio\u4e2d\uff0c\u5728\u89e3\u51b3\u65b9\u6848\u8d44\u6e90\u7ba1\u7406\u5668\u4e2d\u53f3\u952e\u5355\u51fb\u9879\u76ee\uff0c\u7136\u540e\u9009\u62e9\u5c5e\u6027\u3002\u6253\u5f00\u201cConfiguration Properties\u201d\uff0c\u9009\u62e9\u201cDebugging\u201d\u3002\u5728\u53f3\u7a97\u683c\u4e2d\uff0c\u6709\u4e00\u884c\u79f0\u4e3a\u201c\u547d\u4ee4\u53c2\u6570\u201d\u3002\u4f60\u53ef\u4ee5\u5728\u8fd9\u91cc\u8f93\u5165\u547d\u4ee4\u884c\u53c2\u6570\u8fdb\u884c\u6d4b\u8bd5\uff0c\u5f53\u4f60\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u5b83\u4eec\u5c06\u81ea\u52a8\u4f20\u9012\u7ed9\u4f60\u7684\u7a0b\u5e8f\u3002 \u5728Code::Blocks\u4e2d\uff0c\u9009\u62e9\" Project -> Set program 's arguments \"\u3002 \u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570 \u73b0\u5728\u4f60\u5df2\u7ecf\u77e5\u9053\u5982\u4f55\u5411\u7a0b\u5e8f\u63d0\u4f9b\u547d\u4ee4\u884c\u53c2\u6570\u4e86\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u4eceC++\u7a0b\u5e8f\u4e2d\u8bbf\u95ee\u5b83\u4eec\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u79cd\u4e0d\u540c\u4e8e\u4ee5\u5f80\u7684 main() \u5f62\u5f0f\u3002 main() \u7684\u8fd9\u79cd\u65b0\u5f62\u5f0f\u6709\u4e24\u4e2a\u53c2\u6570(\u6309\u7167\u7ea6\u5b9a\u547d\u540d\u4e3a argc \u548c argv )\uff1a 1 int main ( int argc , char * argv []) \u6709\u65f6\u4f60\u4f1a\u770b\u5230\u5b83\u88ab\u5199\u6210\uff1a 1 int main ( int argc , char ** argv ) \u5c3d\u7ba1\u8fd9\u4e24\u79cd\u8868\u793a\u662f\u76f8\u540c\u7684\uff0c\u4f46\u6211\u4eec\u66f4\u559c\u6b22\u7b2c\u4e00\u79cd\u8868\u793a\uff0c\u56e0\u4e3a\u5b83\u66f4\u76f4\u89c2\uff0c\u5bb9\u6613\u7406\u89e3\u3002 argc \u662f\u4e00\u4e2a\u6574\u6570\u53c2\u6570\uff0c\u5305\u542b\u4f20\u9012\u7ed9\u7a0b\u5e8f\u7684\u53c2\u6570\u6570\u91cf(\u8bf7\u8003\u8651\uff1aargc = arg ument c ount)\u3002 argc \u6700\u5c0f\u662f1\uff0c\u56e0\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\u603b\u662f\u7a0b\u5e8f\u672c\u8eab\u7684\u540d\u79f0\u3002\u7528\u6237\u63d0\u4f9b\u7684\u6bcf\u4e2a\u547d\u4ee4\u884c\u53c2\u6570\u5c06\u5bfc\u81f4 argc \u589e\u52a01\u3002 argv \u662f\u5b58\u50a8\u5b9e\u9645\u53c2\u6570\u503c\u7684\u5730\u65b9(\u8bf7\u8003\u8651\uff1aargv = arg ument v values\uff0c\u867d\u7136\u5b83\u7684\u6b63\u786e\u540d\u79f0\u662f\u201c\u53c2\u6570\u5411\u91cf\u201d)\u3002\u5c3d\u7ba1argv\u7684\u58f0\u660e\u770b\u8d77\u6765\u5f88\u5413\u4eba\uff0c\u4f46argv\u5b9e\u9645\u4e0a\u53ea\u662f\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u6570\u7ec4\u3002\u8fd9\u4e2a\u6570\u7ec4\u7684\u957f\u5ea6\u662fargc\u3002 \u8ba9\u6211\u4eec\u5199\u4e00\u4e2a\u540d\u4e3a\u201cMyArgs\u201d\u7684\u5c0f\u7a0b\u5e8f\u6765\u6253\u5370\u6240\u6709\u547d\u4ee4\u884c\u53c2\u6570\u7684\u503c: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // Program: MyArgs #include int main ( int argc , char * argv []) { std :: cout << \"There are \" << argc << \" arguments: \\n \" ; // Loop through each argument and print its number and value for ( int count { 0 }; count < argc ; ++ count ) { std :: cout << count << ' ' << argv [ count ] << '\\n' ; } return 0 ; } \u73b0\u5728\uff0c\u5f53\u6211\u4eec\u7528\u547d\u4ee4\u884c\u53c2\u6570\u201cMyfile.txt\u201d\u548c\u201c100\u201d\u8c03\u7528\u8fd9\u4e2a\u7a0b\u5e8f(MyArgs)\u65f6\uff0c\u8f93\u51fa\u5c06\u5982\u4e0b\u6240\u793a: 1 2 3 4 There are 3 arguments: 0 C:\\MyArgs 1 Myfile.txt 2 100 \u53c2\u65700\u662f\u6b63\u5728\u8fd0\u884c\u7684\u5f53\u524d\u7a0b\u5e8f\u7684\u8def\u5f84\u548c\u540d\u79f0\u3002\u672c\u4f8b\u4e2d\u7684\u53c2\u65701\u548c2\u662f\u6211\u4eec\u4f20\u5165\u7684\u4e24\u4e2a\u547d\u4ee4\u884c\u53c2\u6570\u3002 \u5904\u7406\u6570\u503c\u53c2\u6570 \u547d\u4ee4\u884c\u53c2\u6570\u603b\u662f\u4f5c\u4e3a\u5b57\u7b26\u4e32\u4f20\u9012\uff0c\u5373\u4f7f\u63d0\u4f9b\u7684\u503c\u672c\u8d28\u4e0a\u662f\u6570\u503c\u3002\u82e5\u8981\u5c06\u547d\u4ee4\u884c\u53c2\u6570\u7528\u4f5c\u6570\u5b57\uff0c\u5fc5\u987b\u5c06\u5176\u4ece\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\u3002\u4e0d\u5e78\u7684\u662f\uff0c\u5728C++\u4e2d\uff0c\u5b57\u7b26\u4e32\u8f6c\u6570\u5b57\u6bd4\u4f60\u60f3\u8c61\u7684\u8981\u590d\u6742\u3002 \u5728C++\u4e2d\u9700\u8981\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include #include // for std::stringstream #include int main ( int argc , char * argv []) { if ( argc <= 1 ) { // On some operating systems, argv[0] can end up as an empty string instead of the program's name. // We'll conditionalize our response on whether argv[0] is empty or not. if ( argv [ 0 ]) std :: cout << \"Usage: \" << argv [ 0 ] << \" \" << '\\n' ; else std :: cout << \"Usage: \" << '\\n' ; return 1 ; } std :: stringstream convert { argv [ 1 ] }; // \u521b\u5efa\u4e00\u4e2a stringstream \u53d8\u91cf convert\uff0c\u4f7f\u7528 argv[1] \u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316 int myint {}; if ( ! ( convert >> myint )) // do the conversion myint = 0 ; // if conversion fails, set myint to a default value std :: cout << \"Got integer: \" << myint << '\\n' ; return 0 ; } \u8fd0\u884c\u8be5\u7a0b\u5e8f\u5e76\u5c06\"567\"\u4f5c\u4e3a\u53c2\u6570\uff1a 1 Got integer: 567 std::stringstream \u548c std::cin \u7684\u5de5\u4f5c\u65b9\u5f0f\u7c7b\u4f3c\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d argv[1] \u9996\u5148\u7528\u4e8e\u521d\u59cb\u5316 stringstream \uff0c\u6240\u4ee5\u7a0d\u540e\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 >> \u5c06\u5176\u503c\u63d0\u53d6\u5230\u4e00\u4e2a\u6574\u578b\u53d8\u91cf ( \u7c7b\u4f3c\u4e8e std::cin )\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u7ae0\u8282\u4e2d\u5bf9 std::stringstream \u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002 \u64cd\u4f5c\u7cfb\u7edf\u9996\u5148\u89e3\u6790\u547d\u4ee4\u884c \u5f53\u4f60\u5728\u547d\u4ee4\u884c(\u6216\u4eceIDE\u8fd0\u884c\u60a8\u7684\u7a0b\u5e8f)\u4e0a\u952e\u5165\u67d0\u4e9b\u5185\u5bb9\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u9700\u8981\u8d1f\u8d23\u89e3\u6790\u6216\u8f6c\u53d1\u76f8\u5e94\u7684\u8bf7\u6c42\u3002\u8fd9\u4e0d\u4ec5\u6d89\u53ca\u5230\u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u8fd8\u6d89\u53ca\u5230\u89e3\u6790\u4efb\u4f55\u53c2\u6570\uff0c\u4ee5\u786e\u5b9a\u5e94\u8be5\u5982\u4f55\u5904\u7406\u5b83\u4eec\u5e76\u5c06\u5b83\u4eec\u4f20\u9012\u7ed9\u5e94\u7528\u7a0b\u5e8f\u3002 \u4e00\u822c\u6765\u8bf4\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5bf9\u4e8e\u5982\u4f55\u5904\u7406\u53cc\u5f15\u53f7\u548c\u53cd\u659c\u6760\u7b49\u7279\u6b8a\u5b57\u7b26\u6709\u7279\u6b8a\u7684\u89c4\u5219\u3002 \u4f8b\u5982\uff1a 1 MyArgs Hello world! \u6253\u5370\uff1a 1 2 3 4 There are 3 arguments: 0 C:\\MyArgs 1 Hello 2 world! \u901a\u5e38\uff0c\u4ee5\u53cc\u5f15\u53f7\u4f20\u9012\u7684\u5b57\u7b26\u4e32\u88ab\u8ba4\u4e3a\u662f\u540c\u4e00\u5b57\u7b26\u4e32\u7684\u4e00\u90e8\u5206: 1 MyArgs \"Hello world!\" \u6253\u5370\uff1a 1 2 3 There are 2 arguments: 0 C:\\MyArgs 1 Hello world! \u5927\u591a\u6570\u64cd\u4f5c\u7cfb\u7edf\u90fd\u5141\u8bb8\u4f60\u901a\u8fc7\u8f6c\u8bd1\u53cc\u5f15\u53f7\u6765\u4f7f\u7528\u5b57\u9762\u53cc\u5f15\u53f7\uff1a 1 MyArgs \\\"Hello world!\\\" \u6253\u5370\uff1a 1 2 3 4 There are 3 arguments: 0 C:\\MyArgs 1 \"Hello 2 world!\" \u5176\u4ed6\u5b57\u7b26\u53ef\u80fd\u4e5f\u9700\u8981\u8f6c\u4e49\uff0c\u8fd9\u53d6\u51b3\u4e8e\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u5982\u4f55\u89e3\u91ca\u5b83\u4eec\u3002 \u5c0f\u7ed3 \u547d\u4ee4\u884c\u53c2\u6570\u4e3a\u7528\u6237\u6216\u5176\u4ed6\u7a0b\u5e8f\u5728\u542f\u52a8\u65f6\u5411\u7a0b\u5e8f\u4f20\u9012\u8f93\u5165\u6570\u636e\u63d0\u4f9b\u4e86\u4e00\u79cd\u5f88\u597d\u7684\u65b9\u5f0f\u3002\u5bf9\u4e8e\u4efb\u4f55\u7a0b\u5e8f\u9700\u8981\u7684\u6570\u636e\uff0c\u8bf7\u8003\u8651\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u5141\u8bb8\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u6765\u4f20\u9012\u8fd9\u4e9b\u8f93\u5165\u6570\u636e\u3002\u5982\u679c\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u8be5\u6570\u636e\u6ca1\u6709\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570 \u4f20\u5165\uff0c\u4f60\u4e5f\u662f\u53ef\u4ee5\u68c0\u6d4b\u5230\u7684\uff0c\u6b64\u65f6\u4f60\u53ef\u4ee5\u518d\u8981\u6c42\u7528\u6237\u8fdb\u884c\u8f93\u5165\u3002\u8fd9\u6837\uff0c\u7a0b\u5e8f\u5c31\u53ef\u4ee5\u4ee5\u4efb\u4f55\u4e00\u79cd\u65b9\u5f0f\u8fd0\u884c\u3002","title":"12.5 - \u547d\u4ee4\u884c\u53c2\u6570"},{"location":"12-5-command-line-arguments/#_1","text":"\u5728 0.5 -- Introduction to the compiler, linker, and libraries \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u7f16\u8bd1\u548c\u94fe\u63a5\u540e\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u3002\u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u5219\u4f1a\u4ece main() \u51fd\u6570\u7684\u9876\u90e8\u5f00\u59cb\u8fd0\u884c\u3002 main() \u51fd\u6570\u5b9a\u4e49\u5982\u4e0b\uff1a 1 int main () \u6ce8\u610f\uff0c\u8fd9\u91cc\u7684 main() \u51fd\u6570\u6ca1\u6709 \u5f62\u53c2 \u3002\u4f46\u662f\u5f88\u591a\u7a0b\u5e8f\u5176\u5b9e\u9700\u8981\u57fa\u4e8e\u8f93\u5165\u6765\u5224\u65ad\u5982\u4f55\u5f00\u59cb\u5de5\u4f5c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a\u8bfb\u53d6\u56fe\u7247\u4f5c\u4e3a\u7f29\u7565\u56fe\u7684\u7a0b\u5e8f Thumbnail \u3002\u90a3\u4e48 Thumbnail \u5e94\u8be5\u8bfb\u53d6\u54ea\u4e2a\u56fe\u7247\u5e76\u5904\u7406\u5462\uff1f\u7528\u6237\u5fc5\u987b\u6709\u529e\u6cd5\u544a\u8bc9\u7a0b\u5e8f\u5e94\u8be5\u6253\u5f00\u54ea\u4e2a\u6587\u4ef6\uff0c\u6b64\u65f6\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // Program: Thumbnail #include #include int main () { std :: cout << \"Please enter an image filename to create a thumbnail for: \" ; std :: string filename {}; std :: cin >> filename ; // open image file // create thumbnail // output thumbnail } \u4f46\u662f\uff0c\u8fd9\u4e48\u505a\u6709\u4e00\u4e2a\u6f5c\u5728\u7684\u95ee\u9898\u3002\u6bcf\u6b21\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u90fd\u5fc5\u987b\u7b49\u5f85\u7528\u6237\u7684\u8f93\u5165\u624d\u80fd\u7ee7\u7eed\u5de5\u4f5c\u3002\u5982\u679c\u6211\u4eec\u6bcf\u6b21\u90fd\u662f\u624b\u52a8\u542f\u52a8\u8be5\u7a0b\u5e8f\u7684\u8bdd\uff0c\u8fd9\u4f3c\u4e4e\u4e5f\u6ca1\u4ec0\u4e48\u95ee\u9898\u3002\u4f46\u662f\u5f88\u591a\u65f6\u5019\u8fd9\u4e48\u505a\u662f\u4e0d\u5408\u9002\u7684\uff0c\u4f8b\u5982\u4f60\u9700\u8981\u5bf9\u591a\u4e2a\u6587\u4ef6\u6267\u884c\u8be5\u7a0b\u5e8f\u6216\u8005\u4ece\u53e6\u5916\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\u8c03\u7528\u8be5\u7a0b\u5e8f\u3002 \u8ba9\u6211\u4eec\u8fdb\u4e00\u6b65\u7814\u7a76\u8fd9\u4e9b\u6848\u4f8b\u3002 \u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u4f60\u5e0c\u671b\u4e3a\u7ed9\u5b9a\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u56fe\u50cf\u6587\u4ef6\u521b\u5efa\u7f29\u7565\u56fe\u3002\u4f60\u4f1a\u600e\u4e48\u505a?\u53ea\u8981\u76ee\u5f55\u4e2d\u6709\u56fe\u50cf\uff0c\u4f60\u5c31\u53ef\u4ee5\u591a\u6b21\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u624b\u52a8\u952e\u5165\u6bcf\u4e2a\u6587\u4ef6\u540d\u3002\u7136\u800c\uff0c\u5982\u679c\u6709\u6570\u767e\u4e2a\u56fe\u50cf\uff0c\u8fd9\u53ef\u80fd\u9700\u8981\u4e00\u6574\u5929\uff01\u4e00\u4e2a\u597d\u7684\u89e3\u51b3\u65b9\u6848\u662f\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\u5faa\u73af\u904d\u5386\u76ee\u5f55\u4e2d\u7684\u6bcf\u4e2a\u6587\u4ef6\u540d\uff0c\u4e3a\u6bcf\u4e2a\u6587\u4ef6\u8c03\u7528\u4e00\u6b21 Thumbnail \u3002 \u73b0\u5728\u8003\u8651\u8fd9\u6837\u4e00\u4e2a\u60c5\u51b5\uff0c\u4f60\u6b63\u5728\u8fd0\u8425\u4e00\u4e2a\u7f51\u7ad9\uff0c\u4f60\u60f3\u8ba9\u4f60\u7684\u7f51\u7ad9\u5728\u7528\u6237\u6bcf\u6b21\u4e0a\u4f20\u56fe\u7247\u5230\u4f60\u7684\u7f51\u7ad9\u65f6\u521b\u5efa\u4e00\u4e2a\u7f29\u7565\u56fe\u3002\u8fd9\u4e2a\u7a0b\u5e8f\u6ca1\u6709\u8bbe\u7f6e\u4e3a\u63a5\u53d7\u6765\u81ea\u7f51\u7edc\u7684\u8f93\u5165\uff0c\u90a3\u4e48\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e0a\u4f20\u8005\u5982\u4f55\u8f93\u5165\u6587\u4ef6\u540d\u5462?\u4e00\u4e2a\u597d\u7684\u89e3\u51b3\u65b9\u6848\u662f\u8ba9\u4f60\u7684web\u670d\u52a1\u5668\u5728\u4e0a\u4f20\u540e\u81ea\u52a8\u8c03\u7528 Thumbnail \u3002 \u5728\u8fd9\u4e24\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u786e\u5b9e\u9700\u8981\u4e00\u79cd\u65b9\u6cd5\uff0c\u8ba9\u4e00\u4e2a\u5916\u90e8\u7a0b\u5e8f\u5728 Thumbnail \u542f\u52a8\u65f6\u5c06\u6587\u4ef6\u540d\u4f5c\u4e3a\u8f93\u5165\u4f20\u9012\u7ed9 Thumbnail \u7a0b\u5e8f\uff0c\u800c\u4e0d\u662f\u8ba9 Thumbnail \u5728\u542f\u52a8\u540e\u7b49\u5f85\u7528\u6237\u8f93\u5165\u6587\u4ef6\u540d\u3002 \u547d\u4ee4\u884c\u53c2\u6570 \u662f\u53ef\u9009\u7684\u5b57\u7b26\u4e32\u53c2\u6570\uff0c\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u7531\u64cd\u4f5c\u7cfb\u7edf\u4f20\u9012\u7ed9\u5b83\u3002\u7136\u540e\u7a0b\u5e8f\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u4f5c\u4e3a\u8f93\u5165(\u6216\u5ffd\u7565\u5b83\u4eec)\u3002\u5c31\u50cf\u51fd\u6570\u5f62\u53c2\u4e3a\u51fd\u6570\u63d0\u4f9b\u4e86\u5411\u53e6\u4e00\u4e2a\u51fd\u6570\u63d0\u4f9b\u8f93\u5165\u7684\u65b9\u6cd5\u4e00\u6837\uff0c\u547d\u4ee4\u884c\u5b9e\u53c2\u4e3a\u4eba\u4eec\u6216\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u5411\u7a0b\u5e8f\u63d0\u4f9b\u8f93\u5165\u7684\u65b9\u6cd5\u3002","title":"\u547d\u4ee4\u884c\u53c2\u6570\u7684\u7528\u9014"},{"location":"12-5-command-line-arguments/#_2","text":"\u53ef\u6267\u884c\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u540d\u79f0\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u3002\u4f8b\u5982\uff0c\u8981\u8fd0\u884cWindows\u673a\u5668\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u53ef\u6267\u884c\u6587\u4ef6\" WordCount \"\uff0c\u4f60\u53ef\u4ee5\u8f93\u5165: 1 WordCount \u5728UNIX\u7c7b\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u4f7f\u7528\uff1a 1 ./WordCount \u7ed9 WordCount \u4f20\u9012\u53c2\u6570\u5f88\u7b80\u5355\uff0c\u53ea\u8981\u5c06\u53c2\u6570\u653e\u5728\u7a0b\u5e8f\u540d\u540e\u9762\u5373\u53ef\uff1a 1 WordCount Myfile.txt \u5f53 WordCount \u6267\u884c\u65f6 Myfile.txt \u4f1a\u4f5c\u4e3a\u547d\u4ee4\u884c\u5b9e\u53c2\u4f20\u5165\uff0c\u5982\u679c\u7a0b\u5e8f\u9700\u8981\u591a\u4e2a\u53c2\u6570\uff0c\u5219\u9700\u8981\u5c06\u5b83\u4eec\u7528\u7a7a\u683c\u9694\u5f00\uff1a 1 WordCount Myfile.txt Myotherfile.txt \u5982\u679c\u4eceIDE\u8fd0\u884c\u7a0b\u5e8f\uff0cIDE\u5e94\u8be5\u63d0\u4f9b\u8f93\u5165\u547d\u4ee4\u884c\u53c2\u6570\u7684\u65b9\u6cd5\u3002 \u5728Microsoft Visual Studio\u4e2d\uff0c\u5728\u89e3\u51b3\u65b9\u6848\u8d44\u6e90\u7ba1\u7406\u5668\u4e2d\u53f3\u952e\u5355\u51fb\u9879\u76ee\uff0c\u7136\u540e\u9009\u62e9\u5c5e\u6027\u3002\u6253\u5f00\u201cConfiguration Properties\u201d\uff0c\u9009\u62e9\u201cDebugging\u201d\u3002\u5728\u53f3\u7a97\u683c\u4e2d\uff0c\u6709\u4e00\u884c\u79f0\u4e3a\u201c\u547d\u4ee4\u53c2\u6570\u201d\u3002\u4f60\u53ef\u4ee5\u5728\u8fd9\u91cc\u8f93\u5165\u547d\u4ee4\u884c\u53c2\u6570\u8fdb\u884c\u6d4b\u8bd5\uff0c\u5f53\u4f60\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u5b83\u4eec\u5c06\u81ea\u52a8\u4f20\u9012\u7ed9\u4f60\u7684\u7a0b\u5e8f\u3002 \u5728Code::Blocks\u4e2d\uff0c\u9009\u62e9\" Project -> Set program 's arguments \"\u3002","title":"\u4f20\u9012\u547d\u4ee4\u884c\u53c2\u6570"},{"location":"12-5-command-line-arguments/#_3","text":"\u73b0\u5728\u4f60\u5df2\u7ecf\u77e5\u9053\u5982\u4f55\u5411\u7a0b\u5e8f\u63d0\u4f9b\u547d\u4ee4\u884c\u53c2\u6570\u4e86\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u4eceC++\u7a0b\u5e8f\u4e2d\u8bbf\u95ee\u5b83\u4eec\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u79cd\u4e0d\u540c\u4e8e\u4ee5\u5f80\u7684 main() \u5f62\u5f0f\u3002 main() \u7684\u8fd9\u79cd\u65b0\u5f62\u5f0f\u6709\u4e24\u4e2a\u53c2\u6570(\u6309\u7167\u7ea6\u5b9a\u547d\u540d\u4e3a argc \u548c argv )\uff1a 1 int main ( int argc , char * argv []) \u6709\u65f6\u4f60\u4f1a\u770b\u5230\u5b83\u88ab\u5199\u6210\uff1a 1 int main ( int argc , char ** argv ) \u5c3d\u7ba1\u8fd9\u4e24\u79cd\u8868\u793a\u662f\u76f8\u540c\u7684\uff0c\u4f46\u6211\u4eec\u66f4\u559c\u6b22\u7b2c\u4e00\u79cd\u8868\u793a\uff0c\u56e0\u4e3a\u5b83\u66f4\u76f4\u89c2\uff0c\u5bb9\u6613\u7406\u89e3\u3002 argc \u662f\u4e00\u4e2a\u6574\u6570\u53c2\u6570\uff0c\u5305\u542b\u4f20\u9012\u7ed9\u7a0b\u5e8f\u7684\u53c2\u6570\u6570\u91cf(\u8bf7\u8003\u8651\uff1aargc = arg ument c ount)\u3002 argc \u6700\u5c0f\u662f1\uff0c\u56e0\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\u603b\u662f\u7a0b\u5e8f\u672c\u8eab\u7684\u540d\u79f0\u3002\u7528\u6237\u63d0\u4f9b\u7684\u6bcf\u4e2a\u547d\u4ee4\u884c\u53c2\u6570\u5c06\u5bfc\u81f4 argc \u589e\u52a01\u3002 argv \u662f\u5b58\u50a8\u5b9e\u9645\u53c2\u6570\u503c\u7684\u5730\u65b9(\u8bf7\u8003\u8651\uff1aargv = arg ument v values\uff0c\u867d\u7136\u5b83\u7684\u6b63\u786e\u540d\u79f0\u662f\u201c\u53c2\u6570\u5411\u91cf\u201d)\u3002\u5c3d\u7ba1argv\u7684\u58f0\u660e\u770b\u8d77\u6765\u5f88\u5413\u4eba\uff0c\u4f46argv\u5b9e\u9645\u4e0a\u53ea\u662f\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u6570\u7ec4\u3002\u8fd9\u4e2a\u6570\u7ec4\u7684\u957f\u5ea6\u662fargc\u3002 \u8ba9\u6211\u4eec\u5199\u4e00\u4e2a\u540d\u4e3a\u201cMyArgs\u201d\u7684\u5c0f\u7a0b\u5e8f\u6765\u6253\u5370\u6240\u6709\u547d\u4ee4\u884c\u53c2\u6570\u7684\u503c: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // Program: MyArgs #include int main ( int argc , char * argv []) { std :: cout << \"There are \" << argc << \" arguments: \\n \" ; // Loop through each argument and print its number and value for ( int count { 0 }; count < argc ; ++ count ) { std :: cout << count << ' ' << argv [ count ] << '\\n' ; } return 0 ; } \u73b0\u5728\uff0c\u5f53\u6211\u4eec\u7528\u547d\u4ee4\u884c\u53c2\u6570\u201cMyfile.txt\u201d\u548c\u201c100\u201d\u8c03\u7528\u8fd9\u4e2a\u7a0b\u5e8f(MyArgs)\u65f6\uff0c\u8f93\u51fa\u5c06\u5982\u4e0b\u6240\u793a: 1 2 3 4 There are 3 arguments: 0 C:\\MyArgs 1 Myfile.txt 2 100 \u53c2\u65700\u662f\u6b63\u5728\u8fd0\u884c\u7684\u5f53\u524d\u7a0b\u5e8f\u7684\u8def\u5f84\u548c\u540d\u79f0\u3002\u672c\u4f8b\u4e2d\u7684\u53c2\u65701\u548c2\u662f\u6211\u4eec\u4f20\u5165\u7684\u4e24\u4e2a\u547d\u4ee4\u884c\u53c2\u6570\u3002","title":"\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570"},{"location":"12-5-command-line-arguments/#_4","text":"\u547d\u4ee4\u884c\u53c2\u6570\u603b\u662f\u4f5c\u4e3a\u5b57\u7b26\u4e32\u4f20\u9012\uff0c\u5373\u4f7f\u63d0\u4f9b\u7684\u503c\u672c\u8d28\u4e0a\u662f\u6570\u503c\u3002\u82e5\u8981\u5c06\u547d\u4ee4\u884c\u53c2\u6570\u7528\u4f5c\u6570\u5b57\uff0c\u5fc5\u987b\u5c06\u5176\u4ece\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\u3002\u4e0d\u5e78\u7684\u662f\uff0c\u5728C++\u4e2d\uff0c\u5b57\u7b26\u4e32\u8f6c\u6570\u5b57\u6bd4\u4f60\u60f3\u8c61\u7684\u8981\u590d\u6742\u3002 \u5728C++\u4e2d\u9700\u8981\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include #include // for std::stringstream #include int main ( int argc , char * argv []) { if ( argc <= 1 ) { // On some operating systems, argv[0] can end up as an empty string instead of the program's name. // We'll conditionalize our response on whether argv[0] is empty or not. if ( argv [ 0 ]) std :: cout << \"Usage: \" << argv [ 0 ] << \" \" << '\\n' ; else std :: cout << \"Usage: \" << '\\n' ; return 1 ; } std :: stringstream convert { argv [ 1 ] }; // \u521b\u5efa\u4e00\u4e2a stringstream \u53d8\u91cf convert\uff0c\u4f7f\u7528 argv[1] \u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316 int myint {}; if ( ! ( convert >> myint )) // do the conversion myint = 0 ; // if conversion fails, set myint to a default value std :: cout << \"Got integer: \" << myint << '\\n' ; return 0 ; } \u8fd0\u884c\u8be5\u7a0b\u5e8f\u5e76\u5c06\"567\"\u4f5c\u4e3a\u53c2\u6570\uff1a 1 Got integer: 567 std::stringstream \u548c std::cin \u7684\u5de5\u4f5c\u65b9\u5f0f\u7c7b\u4f3c\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d argv[1] \u9996\u5148\u7528\u4e8e\u521d\u59cb\u5316 stringstream \uff0c\u6240\u4ee5\u7a0d\u540e\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 >> \u5c06\u5176\u503c\u63d0\u53d6\u5230\u4e00\u4e2a\u6574\u578b\u53d8\u91cf ( \u7c7b\u4f3c\u4e8e std::cin )\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u7ae0\u8282\u4e2d\u5bf9 std::stringstream \u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002","title":"\u5904\u7406\u6570\u503c\u53c2\u6570"},{"location":"12-5-command-line-arguments/#_5","text":"\u5f53\u4f60\u5728\u547d\u4ee4\u884c(\u6216\u4eceIDE\u8fd0\u884c\u60a8\u7684\u7a0b\u5e8f)\u4e0a\u952e\u5165\u67d0\u4e9b\u5185\u5bb9\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u9700\u8981\u8d1f\u8d23\u89e3\u6790\u6216\u8f6c\u53d1\u76f8\u5e94\u7684\u8bf7\u6c42\u3002\u8fd9\u4e0d\u4ec5\u6d89\u53ca\u5230\u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u8fd8\u6d89\u53ca\u5230\u89e3\u6790\u4efb\u4f55\u53c2\u6570\uff0c\u4ee5\u786e\u5b9a\u5e94\u8be5\u5982\u4f55\u5904\u7406\u5b83\u4eec\u5e76\u5c06\u5b83\u4eec\u4f20\u9012\u7ed9\u5e94\u7528\u7a0b\u5e8f\u3002 \u4e00\u822c\u6765\u8bf4\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5bf9\u4e8e\u5982\u4f55\u5904\u7406\u53cc\u5f15\u53f7\u548c\u53cd\u659c\u6760\u7b49\u7279\u6b8a\u5b57\u7b26\u6709\u7279\u6b8a\u7684\u89c4\u5219\u3002 \u4f8b\u5982\uff1a 1 MyArgs Hello world! \u6253\u5370\uff1a 1 2 3 4 There are 3 arguments: 0 C:\\MyArgs 1 Hello 2 world! \u901a\u5e38\uff0c\u4ee5\u53cc\u5f15\u53f7\u4f20\u9012\u7684\u5b57\u7b26\u4e32\u88ab\u8ba4\u4e3a\u662f\u540c\u4e00\u5b57\u7b26\u4e32\u7684\u4e00\u90e8\u5206: 1 MyArgs \"Hello world!\" \u6253\u5370\uff1a 1 2 3 There are 2 arguments: 0 C:\\MyArgs 1 Hello world! \u5927\u591a\u6570\u64cd\u4f5c\u7cfb\u7edf\u90fd\u5141\u8bb8\u4f60\u901a\u8fc7\u8f6c\u8bd1\u53cc\u5f15\u53f7\u6765\u4f7f\u7528\u5b57\u9762\u53cc\u5f15\u53f7\uff1a 1 MyArgs \\\"Hello world!\\\" \u6253\u5370\uff1a 1 2 3 4 There are 3 arguments: 0 C:\\MyArgs 1 \"Hello 2 world!\" \u5176\u4ed6\u5b57\u7b26\u53ef\u80fd\u4e5f\u9700\u8981\u8f6c\u4e49\uff0c\u8fd9\u53d6\u51b3\u4e8e\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u5982\u4f55\u89e3\u91ca\u5b83\u4eec\u3002","title":"\u64cd\u4f5c\u7cfb\u7edf\u9996\u5148\u89e3\u6790\u547d\u4ee4\u884c"},{"location":"12-5-command-line-arguments/#_6","text":"\u547d\u4ee4\u884c\u53c2\u6570\u4e3a\u7528\u6237\u6216\u5176\u4ed6\u7a0b\u5e8f\u5728\u542f\u52a8\u65f6\u5411\u7a0b\u5e8f\u4f20\u9012\u8f93\u5165\u6570\u636e\u63d0\u4f9b\u4e86\u4e00\u79cd\u5f88\u597d\u7684\u65b9\u5f0f\u3002\u5bf9\u4e8e\u4efb\u4f55\u7a0b\u5e8f\u9700\u8981\u7684\u6570\u636e\uff0c\u8bf7\u8003\u8651\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u5141\u8bb8\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u6765\u4f20\u9012\u8fd9\u4e9b\u8f93\u5165\u6570\u636e\u3002\u5982\u679c\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u8be5\u6570\u636e\u6ca1\u6709\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570 \u4f20\u5165\uff0c\u4f60\u4e5f\u662f\u53ef\u4ee5\u68c0\u6d4b\u5230\u7684\uff0c\u6b64\u65f6\u4f60\u53ef\u4ee5\u518d\u8981\u6c42\u7528\u6237\u8fdb\u884c\u8f93\u5165\u3002\u8fd9\u6837\uff0c\u7a0b\u5e8f\u5c31\u53ef\u4ee5\u4ee5\u4efb\u4f55\u4e00\u79cd\u65b9\u5f0f\u8fd0\u884c\u3002","title":"\u5c0f\u7ed3"},{"location":"12-6-Ellipsis-and-why-to-avoid-them/","tags":["ellipsis"],"text":"Key Takeaway In all of the functions we\u2019ve seen so far, the number of parameters a function will take must be known in advance (even if they have default values). However, there are certain cases where it can be useful to be able to pass a variable number of parameters to a function. C++ provides a special specifier known as ellipsis (aka \u201c\u2026\u201d) that allow us to do precisely this. Because ellipsis are rarely used, potentially dangerous, and we recommend avoiding their use, this section can be considered optional reading. Functions that use ellipsis take the form: return_type function_name(argument_list, ...) The argument_list is one or more normal function parameters. Note that functions that use ellipsis must have at least one non-ellipsis parameter. Any arguments passed to the function must match the argument_list parameters first. The ellipsis (which are represented as three periods in a row) must always be the last parameter in the function. The ellipsis capture any additional arguments (if there are any). Though it is not quite accurate, it is conceptually useful to think of the ellipsis as an array that holds any additional parameters beyond those in the argument_list. \u7701\u7565\u53f7\u5b9e\u4f8b The best way to learn about ellipsis is by example. So let\u2019s write a simple program that uses ellipsis. Let\u2019s say we want to write a function that calculates the average of a bunch of integers. We\u2019d do it like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include #include // needed to use ellipsis // The ellipsis must be the last parameter // count is how many additional arguments we're passing double findAverage ( int count , ...) { int sum { 0 }; // We access the ellipsis through a va_list, so let's declare one std :: va_list list ; // We initialize the va_list using va_start. The first argument is // the list to initialize. The second argument is the last non-ellipsis // parameter. va_start ( list , count ); // Loop through all the ellipsis values for ( int arg { 0 }; arg < count ; ++ arg ) { // We use va_arg to get values out of our ellipsis // The first argument is the va_list we're using // The second argument is the type of the value sum += va_arg ( list , int ); } // Cleanup the va_list when we're done. va_end ( list ); return static_cast < double > ( sum ) / count ; } int main () { std :: cout << findAverage ( 5 , 1 , 2 , 3 , 4 , 5 ) << '\\n' ; std :: cout << findAverage ( 6 , 1 , 2 , 3 , 4 , 5 , 6 ) << '\\n' ; return 0 ; } COPY This code prints: 1 2 3 3.5 As you can see, this function takes a variable number of parameters! Now, let\u2019s take a look at the components that make up this example. First, we have to include the cstdarg header. This header defines va_list, va_arg, va_start, and va_end, which are macros that we need to use to access the parameters that are part of the ellipsis. We then declare our function that uses the ellipsis. Remember that the argument list must be one or more fixed parameters. In this case, we\u2019re passing in a single integer that tells us how many numbers to average. The ellipsis always comes last. Note that the ellipsis parameter has no name! Instead, we access the values in the ellipsis through a special type known as va_list. It is conceptually useful to think of va_list as a pointer that points to the ellipsis array. First, we declare a va_list, which we\u2019ve called \u201clist\u201d for simplicity. The next thing we need to do is make list point to our ellipsis parameters. We do this by calling va_start(). va_start() takes two parameters: the va_list itself, and the name of the last non-ellipsis parameter in the function. Once va_start() has been called, va_list points to the first parameter in the ellipsis. To get the value of the parameter that va_list currently points to, we use va_arg(). va_arg() also takes two parameters: the va_list itself, and the type of the parameter we\u2019re trying to access. Note that va_arg() also moves the va_list to the next parameter in the ellipsis! Finally, to clean up when we are done, we call va_end(), with va_list as the parameter. Note that va_start() can be called again any time we want to reset the va_list to point to the first parameter in the ellipses again. \u4e3a\u4ec0\u4e48\u7701\u7565\u53f7\u4e0d\u5b89\u5168\uff1a\u7c7b\u578b\u68c0\u67e5\u88ab\u8df3\u8fc7\u4e86 Ellipsis offer the programmer a lot of flexibility to implement functions that can take a variable number of parameters. However, this flexibility comes with some downsides. With regular function parameters, the compiler uses type checking to ensure the types of the function arguments match the types of the function parameters (or can be implicitly converted so they match). This helps ensure you don\u2019t pass a function an integer when it was expecting a string, or vice versa. However, note that ellipsis parameters have no type declarations. When using ellipsis, the compiler completely suspends type checking for ellipsis parameters. This means it is possible to send arguments of any type to the ellipsis! However, the downside is that the compiler will no longer be able to warn you if you call the function with ellipsis arguments that do not make sense. When using the ellipsis, it is completely up to the caller to ensure the function is called with ellipsis arguments that the function can handle. Obviously that leaves quite a bit of room for error (especially if the caller wasn\u2019t the one who wrote the function). Let\u2019s look at an example of a mistake that is pretty subtle: 1 std :: cout << findAverage ( 6 , 1.0 , 2 , 3 , 4 , 5 , 6 ) << '\\n' ; COPY Although this may look harmless enough at first glance, note that the second argument (the first ellipsis argument) is a double instead of an integer. This compiles fine, and produces a somewhat surprising result: 1 1.78782e+008 which is a REALLY big number. How did this happen? As you have learned in previous lessons, a computer stores all data as a sequence of bits. A variable\u2019s type tells the computer how to translate that sequence of bits into a meaningful value. However, you just learned that the ellipsis throw away the variable\u2019s type! Consequently, the only way to get a meaningful value back from the ellipsis is to manually tell va_arg() what the expected type of the next parameter is. This is what the second parameter of va_arg() does. If the actual parameter type doesn\u2019t match the expected parameter type, bad things will usually happen. In the above findAverage program, we told va_arg() that our variables are all expected to have a type of int. Consequently, each call to va_arg() will return the next sequence of bits translated as an integer. In this case, the problem is that the double we passed in as the first ellipsis argument is 8 bytes, whereas va_arg(list, int) will only return 4 bytes of data with each call. Consequently, the first call to va_arg will only read the first 4 bytes of the double (producing a garbage result), and the second call to va_arg will read the second 4 bytes of the double (producing another garbage result). Thus, our overall result is garbage. Because type checking is suspended, the compiler won\u2019t even complain if we do something completely ridiculous, like this: 1 2 int value { 7 }; std :: cout << findAverage ( 6 , 1.0 , 2 , \"Hello, world!\" , 'G' , & value , & findAverage ) << '\\n' ; COPY Believe it or not, this actually compiles just fine, and produces the following result on the author\u2019s machine: 1 1.79766e+008 This result epitomizes the phrase, \u201cGarbage in, garbage out\u201d, which is a popular computer science phrase \u201cused primarily to call attention to the fact that computers, unlike humans, will unquestioningly process the most nonsensical of input data and produce nonsensical output\u201d ( Wikipedia ). So, in summary, type checking on the parameters is suspended, and we have to trust the caller to pass in the right type of parameters. If they don\u2019t, the compiler won\u2019t complain -- our program will just produce garbage (or maybe crash). \u4e3a\u4ec0\u4e48\u7701\u7565\u53f7\u4e0d\u5b89\u5168\uff1a\u7701\u7565\u53f7\u4e0d\u77e5\u9053\u4f20\u5165\u53c2\u6570\u7684\u4e2a\u6570 Not only do the ellipsis throw away the type of the parameters, it also throws away the number of parameters in the ellipsis. This means we have to devise our own solution for keeping track of the number of parameters passed into the ellipsis. Typically, this is done in one of three ways. \u65b9\u6cd51\uff1a\u4f20\u5165\u4e00\u4e2a\u957f\u5ea6\u53c2\u6570 Method #1 is to have one of the fixed parameters represent the number of optional parameters passed. This is the solution we use in the findAverage() example above. However, even here we run into trouble. For example, consider the following call: 1 std :: cout << findAverage ( 6 , 1 , 2 , 3 , 4 , 5 ) << '\\n' ; COPY On the author\u2019s machine at the time of writing, this produced the result: 699773 What happened? We told findAverage() we were going to provide 6 additional values, but we only gave it 5. Consequently, the first five values that va_arg() returns were the ones we passed in. The 6th value it returns was a garbage value somewhere in the stack. Consequently, we got a garbage answer. A more insidious case: 1 std :: cout << findAverage ( 6 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ) << '\\n' ; COPY This produces the answer 3.5, which may look correct at first glance, but omits the last number in the average, because we only told it we were going to provide 6 additional values (and then actually provided 7). These kind of mistakes can be very hard to catch. \u65b9\u6cd52\uff1a \u4f7f\u7528\u54e8\u5175\u503c Method #2 is to use a \u54e8\u5175 value. A sentinel is a special value that is used to terminate a loop when it is encountered. For example, with strings, the null terminator is used as a sentinel value to denote the end of the string. With ellipsis, the sentinel is typically passed in as the last parameter. Here\u2019s an example of findAverage() rewritten to use a sentinel value of -1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include #include // needed to use ellipsis // The ellipsis must be the last parameter double findAverage ( int first , ...) { // We have to deal with the first number specially int sum { first }; // We access the ellipsis through a va_list, so let's declare one std :: va_list list ; // We initialize the va_list using va_start. The first argument is // the list to initialize. The second argument is the last non-ellipsis // parameter. va_start ( list , first ); int count { 1 }; // Loop indefinitely while ( true ) { // We use va_arg to get values out of our ellipsis // The first argument is the va_list we're using // The second argument is the type of the value int arg { va_arg ( list , int ) }; // If this parameter is our sentinel value, stop looping if ( arg == -1 ) break ; sum += arg ; ++ count ; } // Cleanup the va_list when we're done. va_end ( list ); return static_cast < double > ( sum ) / count ; } int main () { std :: cout << findAverage ( 1 , 2 , 3 , 4 , 5 , -1 ) << '\\n' ; std :: cout << findAverage ( 1 , 2 , 3 , 4 , 5 , 6 , -1 ) << '\\n' ; return 0 ; } COPY Note that we no longer need to pass an explicit length as the first parameter. Instead, we pass a sentinel value as the last parameter. However, there are a couple of challenges here. First, C++ requires that we pass at least one fixed parameter. In the previous example, this was our count variable. In this example, the first value is actually part of the numbers to be averaged. So instead of treating the first value to be averaged as part of the ellipsis parameters, we explicitly declare it as a normal parameter. We then need special handling for it inside the function (in this case, we set sum to first instead of 0 to start). Second, this requires the user to pass in the sentinel as the last value. If the user forgets to pass in the sentinel value (or passes in the wrong value), the function will loop continuously until it runs into garbage that matches the sentinel (or crashes). Finally, note that we\u2019ve chosen -1 as our sentinel. That\u2019s fine if we only wanted to find the average of positive numbers, but what if we wanted to include negative numbers? Sentinel values only work well if there is a value that falls outside the valid set of values for the problem you are trying to solve. \u65b9\u6cd5 3: \u4f7f\u7528\u89e3\u7801\u7684\u5b57\u7b26\u4e32 Method #3 involves passing a \u201cdecoder string\u201d that tells the program how to interpret the parameters. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #include #include #include // needed to use ellipsis // The ellipsis must be the last parameter double findAverage ( std :: string_view decoder , ...) { double sum { 0 }; // We access the ellipsis through a va_list, so let's declare one std :: va_list list ; // We initialize the va_list using va_start. The first argument is // the list to initialize. The second argument is the last non-ellipsis // parameter. va_start ( list , decoder ); for ( auto codetype : decoder ) { switch ( codetype ) { case 'i' : sum += va_arg ( list , int ); break ; case 'd' : sum += va_arg ( list , double ); break ; } } // Cleanup the va_list when we're done. va_end ( list ); return sum / std :: size ( decoder ); } int main () { std :: cout << findAverage ( \"iiiii\" , 1 , 2 , 3 , 4 , 5 ) << '\\n' ; std :: cout << findAverage ( \"iiiiii\" , 1 , 2 , 3 , 4 , 5 , 6 ) << '\\n' ; std :: cout << findAverage ( \"iiddi\" , 1 , 2 , 3.5 , 4.5 , 5 ) << '\\n' ; return 0 ; } COPY In this example, we pass a string that encodes both the number of optional variables and their types. The cool thing is that this lets us deal with parameters of different types. However, this method has downsides as well: the decoder string can be a bit cryptic, and if the number or types of the optional parameters don\u2019t match the decoder string precisely, bad things can happen. For those of you coming from C, this is what printf does! \u5b89\u5168\u4f7f\u7528\u7701\u7565\u53f7\u7684\u5efa\u8bae First, if possible, do not use ellipsis at all! Oftentimes, other reasonable solutions are available, even if they require slightly more work. For example, in our findAverage() program, we could have passed in a dynamically sized array of integers instead. This would have provided both strong type checking (to make sure the caller doesn\u2019t try to do something nonsensical) while preserving the ability to pass a variable number of integers to be averaged. Second, if you do use ellipsis, do not mix expected argument types within your ellipsis if possible. Doing so vastly increases the possibility of the caller inadvertently passing in data of the wrong type and va_arg() producing a garbage result. Third, using a count parameter or decoder string as part of the argument list is generally safer than using a sentinel as an ellipsis parameter. This forces the user to pick an appropriate value for the count/decoder parameter, which ensures the ellipsis loop will terminate after a reasonable number of iterations even if it produces a garbage value. \u6269\u5c55\u9605\u8bfb To improve upon ellipses-like functionality, C++11 introduced parameter packs and variadic templates , which offers functionality similar to ellipses, but with strong type checking. However, significant usability challenges impeded adoption of this feature. In C++17, fold expressions were added, which significantly improves the usability of parameter packs, to the point where they are now a viable option. We hope to introduce lessons on these topics in a future site update.","title":"12.6 - \u7701\u7565\u53f7\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83"},{"location":"12-6-Ellipsis-and-why-to-avoid-them/#_1","text":"The best way to learn about ellipsis is by example. So let\u2019s write a simple program that uses ellipsis. Let\u2019s say we want to write a function that calculates the average of a bunch of integers. We\u2019d do it like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include #include // needed to use ellipsis // The ellipsis must be the last parameter // count is how many additional arguments we're passing double findAverage ( int count , ...) { int sum { 0 }; // We access the ellipsis through a va_list, so let's declare one std :: va_list list ; // We initialize the va_list using va_start. The first argument is // the list to initialize. The second argument is the last non-ellipsis // parameter. va_start ( list , count ); // Loop through all the ellipsis values for ( int arg { 0 }; arg < count ; ++ arg ) { // We use va_arg to get values out of our ellipsis // The first argument is the va_list we're using // The second argument is the type of the value sum += va_arg ( list , int ); } // Cleanup the va_list when we're done. va_end ( list ); return static_cast < double > ( sum ) / count ; } int main () { std :: cout << findAverage ( 5 , 1 , 2 , 3 , 4 , 5 ) << '\\n' ; std :: cout << findAverage ( 6 , 1 , 2 , 3 , 4 , 5 , 6 ) << '\\n' ; return 0 ; } COPY This code prints: 1 2 3 3.5 As you can see, this function takes a variable number of parameters! Now, let\u2019s take a look at the components that make up this example. First, we have to include the cstdarg header. This header defines va_list, va_arg, va_start, and va_end, which are macros that we need to use to access the parameters that are part of the ellipsis. We then declare our function that uses the ellipsis. Remember that the argument list must be one or more fixed parameters. In this case, we\u2019re passing in a single integer that tells us how many numbers to average. The ellipsis always comes last. Note that the ellipsis parameter has no name! Instead, we access the values in the ellipsis through a special type known as va_list. It is conceptually useful to think of va_list as a pointer that points to the ellipsis array. First, we declare a va_list, which we\u2019ve called \u201clist\u201d for simplicity. The next thing we need to do is make list point to our ellipsis parameters. We do this by calling va_start(). va_start() takes two parameters: the va_list itself, and the name of the last non-ellipsis parameter in the function. Once va_start() has been called, va_list points to the first parameter in the ellipsis. To get the value of the parameter that va_list currently points to, we use va_arg(). va_arg() also takes two parameters: the va_list itself, and the type of the parameter we\u2019re trying to access. Note that va_arg() also moves the va_list to the next parameter in the ellipsis! Finally, to clean up when we are done, we call va_end(), with va_list as the parameter. Note that va_start() can be called again any time we want to reset the va_list to point to the first parameter in the ellipses again.","title":"\u7701\u7565\u53f7\u5b9e\u4f8b"},{"location":"12-6-Ellipsis-and-why-to-avoid-them/#_2","text":"Ellipsis offer the programmer a lot of flexibility to implement functions that can take a variable number of parameters. However, this flexibility comes with some downsides. With regular function parameters, the compiler uses type checking to ensure the types of the function arguments match the types of the function parameters (or can be implicitly converted so they match). This helps ensure you don\u2019t pass a function an integer when it was expecting a string, or vice versa. However, note that ellipsis parameters have no type declarations. When using ellipsis, the compiler completely suspends type checking for ellipsis parameters. This means it is possible to send arguments of any type to the ellipsis! However, the downside is that the compiler will no longer be able to warn you if you call the function with ellipsis arguments that do not make sense. When using the ellipsis, it is completely up to the caller to ensure the function is called with ellipsis arguments that the function can handle. Obviously that leaves quite a bit of room for error (especially if the caller wasn\u2019t the one who wrote the function). Let\u2019s look at an example of a mistake that is pretty subtle: 1 std :: cout << findAverage ( 6 , 1.0 , 2 , 3 , 4 , 5 , 6 ) << '\\n' ; COPY Although this may look harmless enough at first glance, note that the second argument (the first ellipsis argument) is a double instead of an integer. This compiles fine, and produces a somewhat surprising result: 1 1.78782e+008 which is a REALLY big number. How did this happen? As you have learned in previous lessons, a computer stores all data as a sequence of bits. A variable\u2019s type tells the computer how to translate that sequence of bits into a meaningful value. However, you just learned that the ellipsis throw away the variable\u2019s type! Consequently, the only way to get a meaningful value back from the ellipsis is to manually tell va_arg() what the expected type of the next parameter is. This is what the second parameter of va_arg() does. If the actual parameter type doesn\u2019t match the expected parameter type, bad things will usually happen. In the above findAverage program, we told va_arg() that our variables are all expected to have a type of int. Consequently, each call to va_arg() will return the next sequence of bits translated as an integer. In this case, the problem is that the double we passed in as the first ellipsis argument is 8 bytes, whereas va_arg(list, int) will only return 4 bytes of data with each call. Consequently, the first call to va_arg will only read the first 4 bytes of the double (producing a garbage result), and the second call to va_arg will read the second 4 bytes of the double (producing another garbage result). Thus, our overall result is garbage. Because type checking is suspended, the compiler won\u2019t even complain if we do something completely ridiculous, like this: 1 2 int value { 7 }; std :: cout << findAverage ( 6 , 1.0 , 2 , \"Hello, world!\" , 'G' , & value , & findAverage ) << '\\n' ; COPY Believe it or not, this actually compiles just fine, and produces the following result on the author\u2019s machine: 1 1.79766e+008 This result epitomizes the phrase, \u201cGarbage in, garbage out\u201d, which is a popular computer science phrase \u201cused primarily to call attention to the fact that computers, unlike humans, will unquestioningly process the most nonsensical of input data and produce nonsensical output\u201d ( Wikipedia ). So, in summary, type checking on the parameters is suspended, and we have to trust the caller to pass in the right type of parameters. If they don\u2019t, the compiler won\u2019t complain -- our program will just produce garbage (or maybe crash).","title":"\u4e3a\u4ec0\u4e48\u7701\u7565\u53f7\u4e0d\u5b89\u5168\uff1a\u7c7b\u578b\u68c0\u67e5\u88ab\u8df3\u8fc7\u4e86"},{"location":"12-6-Ellipsis-and-why-to-avoid-them/#_3","text":"Not only do the ellipsis throw away the type of the parameters, it also throws away the number of parameters in the ellipsis. This means we have to devise our own solution for keeping track of the number of parameters passed into the ellipsis. Typically, this is done in one of three ways.","title":"\u4e3a\u4ec0\u4e48\u7701\u7565\u53f7\u4e0d\u5b89\u5168\uff1a\u7701\u7565\u53f7\u4e0d\u77e5\u9053\u4f20\u5165\u53c2\u6570\u7684\u4e2a\u6570"},{"location":"12-6-Ellipsis-and-why-to-avoid-them/#1","text":"Method #1 is to have one of the fixed parameters represent the number of optional parameters passed. This is the solution we use in the findAverage() example above. However, even here we run into trouble. For example, consider the following call: 1 std :: cout << findAverage ( 6 , 1 , 2 , 3 , 4 , 5 ) << '\\n' ; COPY On the author\u2019s machine at the time of writing, this produced the result: 699773 What happened? We told findAverage() we were going to provide 6 additional values, but we only gave it 5. Consequently, the first five values that va_arg() returns were the ones we passed in. The 6th value it returns was a garbage value somewhere in the stack. Consequently, we got a garbage answer. A more insidious case: 1 std :: cout << findAverage ( 6 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ) << '\\n' ; COPY This produces the answer 3.5, which may look correct at first glance, but omits the last number in the average, because we only told it we were going to provide 6 additional values (and then actually provided 7). These kind of mistakes can be very hard to catch.","title":"\u65b9\u6cd51\uff1a\u4f20\u5165\u4e00\u4e2a\u957f\u5ea6\u53c2\u6570"},{"location":"12-6-Ellipsis-and-why-to-avoid-them/#2","text":"Method #2 is to use a \u54e8\u5175 value. A sentinel is a special value that is used to terminate a loop when it is encountered. For example, with strings, the null terminator is used as a sentinel value to denote the end of the string. With ellipsis, the sentinel is typically passed in as the last parameter. Here\u2019s an example of findAverage() rewritten to use a sentinel value of -1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include #include // needed to use ellipsis // The ellipsis must be the last parameter double findAverage ( int first , ...) { // We have to deal with the first number specially int sum { first }; // We access the ellipsis through a va_list, so let's declare one std :: va_list list ; // We initialize the va_list using va_start. The first argument is // the list to initialize. The second argument is the last non-ellipsis // parameter. va_start ( list , first ); int count { 1 }; // Loop indefinitely while ( true ) { // We use va_arg to get values out of our ellipsis // The first argument is the va_list we're using // The second argument is the type of the value int arg { va_arg ( list , int ) }; // If this parameter is our sentinel value, stop looping if ( arg == -1 ) break ; sum += arg ; ++ count ; } // Cleanup the va_list when we're done. va_end ( list ); return static_cast < double > ( sum ) / count ; } int main () { std :: cout << findAverage ( 1 , 2 , 3 , 4 , 5 , -1 ) << '\\n' ; std :: cout << findAverage ( 1 , 2 , 3 , 4 , 5 , 6 , -1 ) << '\\n' ; return 0 ; } COPY Note that we no longer need to pass an explicit length as the first parameter. Instead, we pass a sentinel value as the last parameter. However, there are a couple of challenges here. First, C++ requires that we pass at least one fixed parameter. In the previous example, this was our count variable. In this example, the first value is actually part of the numbers to be averaged. So instead of treating the first value to be averaged as part of the ellipsis parameters, we explicitly declare it as a normal parameter. We then need special handling for it inside the function (in this case, we set sum to first instead of 0 to start). Second, this requires the user to pass in the sentinel as the last value. If the user forgets to pass in the sentinel value (or passes in the wrong value), the function will loop continuously until it runs into garbage that matches the sentinel (or crashes). Finally, note that we\u2019ve chosen -1 as our sentinel. That\u2019s fine if we only wanted to find the average of positive numbers, but what if we wanted to include negative numbers? Sentinel values only work well if there is a value that falls outside the valid set of values for the problem you are trying to solve.","title":"\u65b9\u6cd52\uff1a \u4f7f\u7528\u54e8\u5175\u503c"},{"location":"12-6-Ellipsis-and-why-to-avoid-them/#3","text":"Method #3 involves passing a \u201cdecoder string\u201d that tells the program how to interpret the parameters. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #include #include #include // needed to use ellipsis // The ellipsis must be the last parameter double findAverage ( std :: string_view decoder , ...) { double sum { 0 }; // We access the ellipsis through a va_list, so let's declare one std :: va_list list ; // We initialize the va_list using va_start. The first argument is // the list to initialize. The second argument is the last non-ellipsis // parameter. va_start ( list , decoder ); for ( auto codetype : decoder ) { switch ( codetype ) { case 'i' : sum += va_arg ( list , int ); break ; case 'd' : sum += va_arg ( list , double ); break ; } } // Cleanup the va_list when we're done. va_end ( list ); return sum / std :: size ( decoder ); } int main () { std :: cout << findAverage ( \"iiiii\" , 1 , 2 , 3 , 4 , 5 ) << '\\n' ; std :: cout << findAverage ( \"iiiiii\" , 1 , 2 , 3 , 4 , 5 , 6 ) << '\\n' ; std :: cout << findAverage ( \"iiddi\" , 1 , 2 , 3.5 , 4.5 , 5 ) << '\\n' ; return 0 ; } COPY In this example, we pass a string that encodes both the number of optional variables and their types. The cool thing is that this lets us deal with parameters of different types. However, this method has downsides as well: the decoder string can be a bit cryptic, and if the number or types of the optional parameters don\u2019t match the decoder string precisely, bad things can happen. For those of you coming from C, this is what printf does!","title":"\u65b9\u6cd5 3: \u4f7f\u7528\u89e3\u7801\u7684\u5b57\u7b26\u4e32"},{"location":"12-6-Ellipsis-and-why-to-avoid-them/#_4","text":"First, if possible, do not use ellipsis at all! Oftentimes, other reasonable solutions are available, even if they require slightly more work. For example, in our findAverage() program, we could have passed in a dynamically sized array of integers instead. This would have provided both strong type checking (to make sure the caller doesn\u2019t try to do something nonsensical) while preserving the ability to pass a variable number of integers to be averaged. Second, if you do use ellipsis, do not mix expected argument types within your ellipsis if possible. Doing so vastly increases the possibility of the caller inadvertently passing in data of the wrong type and va_arg() producing a garbage result. Third, using a count parameter or decoder string as part of the argument list is generally safer than using a sentinel as an ellipsis parameter. This forces the user to pick an appropriate value for the count/decoder parameter, which ensures the ellipsis loop will terminate after a reasonable number of iterations even if it produces a garbage value. \u6269\u5c55\u9605\u8bfb To improve upon ellipses-like functionality, C++11 introduced parameter packs and variadic templates , which offers functionality similar to ellipses, but with strong type checking. However, significant usability challenges impeded adoption of this feature. In C++17, fold expressions were added, which significantly improves the usability of parameter packs, to the point where they are now a viable option. We hope to introduce lessons on these topics in a future site update.","title":"\u5b89\u5168\u4f7f\u7528\u7701\u7565\u53f7\u7684\u5efa\u8bae"},{"location":"12-7-introduction-to-lambdas-anonymous-functions/","tags":["lambda","C++14","C++20"],"text":"Key Takeaway \u8bf7\u8003\u8651\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff0c\u6211\u4eec\u5728 11.19 \u2014 \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb \u4e2d\u4ecb\u7ecd\u8fc7\u5b83\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include #include #include // Our function will return true if the element matches bool containsNut ( std :: string_view str ) { // std::string_view::find returns std::string_view::npos if it doesn't find // the substring. Otherwise it returns the index where the substring occurs // in str. return ( str . find ( \"nut\" ) != std :: string_view :: npos ); } int main () { std :: array < std :: string_view , 4 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" }; // Scan our array to see if any elements contain the \"nut\" substring auto found { std :: find_if ( arr . begin (), arr . end (), containsNut ) }; if ( found == arr . end ()) { std :: cout << \"No nuts \\n \" ; } else { std :: cout << \"Found \" << * found << '\\n' ; } return 0 ; } \u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\u641c\u7d22\u5b57\u7b26\u4e32\uff0c\u627e\u5230\u7b2c\u4e00\u4e2a\u5305\u542b \u201cnut\u201d \u5b50\u4e32\u7684\u5143\u7d20\u3002\u56e0\u6b64\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 Found walnut \u867d\u7136\u4ee3\u7801\u53ef\u4ee5\u6b63\u786e\u6267\u884c\uff0c\u4f46\u662f\u8fd8\u6709\u53ef\u6539\u8fdb\u7684\u5730\u65b9\u3002 \u8fd9\u91cc\u95ee\u9898\u7684\u6838\u5fc3\u5728\u4e8e std::find_if \u8981\u6c42\u4f7f\u7528\u8005\u4f20\u9012\u7ed9\u5b83\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5fc5\u987b\u5b9a\u4e49\u4e00\u4e2a\u53ea\u4f7f\u7528\u4e00\u6b21\u7684\u51fd\u6570\uff0c\u8fd8\u5fc5\u987b\u7ed9\u5b83\u8d77\u4e2a\u540d\uff0c\u5e76\u5c06\u5176\u653e\u7f6e\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d(\u56e0\u4e3a\u51fd\u6570\u4e0d\u80fd\u5d4c\u5957!)\u3002\u8fd9\u4e2a\u51fd\u6570\u5f88\u77ed\uff0c\u4ece\u4e00\u884c\u4ee3\u7801\u4e2d\u51e0\u4e4e\u6bd4\u4ece\u540d\u79f0\u548c\u6ce8\u91ca\u4e2d\u66f4\u5bb9\u6613\u8fa8\u522b\u5b83\u7684\u529f\u80fd\u3002 lambda \u4f7f\u7528 lambda \u8868\u8fbe\u5f0f(\u6709\u65f6\u5019\u4e5f\u79f0lambda\u6216 \u95ed\u5305 \u6216\u533f\u540d\u51fd\u6570) \u53ef\u4ee5\u5728\u5176\u4ed6\u51fd\u6570\u4e2d\u5b9a\u4e49\u533f\u540d\u51fd\u6570\u3002\u80fd\u591f\u5d4c\u5957\u5b9a\u4e49\u662f\u5f88\u91cd\u8981\u7684\u7279\u6027\uff0c\u5b83\u53ef\u4ee5\u907f\u514d \u547d\u540d\u7a7a\u95f4 \u4e2d\u7684\u540d\u79f0\u6c61\u67d3\uff0c\u800c\u4e14\u53ef\u4ee5\u5c06\u51fd\u6570\u7684\u5b9a\u4e49\u5c3d\u53ef\u80fd\u9760\u8fd1\u4f7f\u7528\u5b83\u7684\u5730\u65b9\uff08\u907f\u514d\u989d\u5916\u7684\u4e0a\u4e0b\u6587\uff09\u3002 lambda \u7684\u8bed\u6cd5\u662fC++\u4e2d\u6700\u5947\u602a\u7684\u4e1c\u897f\u4e4b\u4e00\uff0c\u9700\u8981\u4e00\u70b9\u65f6\u95f4\u6765\u9002\u5e94\u3002\u533f\u540d\u51fd\u6570\u5f62\u5f0f\u662f: 1 2 3 4 [ captureClause ] ( parameters ) -> returnType { statements ; } \u95ed\u5305\u8bed\u53e5\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u5982\u679c\u4e0d\u9700\u8981\u6355\u83b7\u53d8\u91cf\u7684\u8bdd\uff1b \u5f62\u53c2 \u5217\u8868\u4e5f\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u5982\u679c\u4e0d\u9700\u8981\u5f62\u53c2\u7684\u8bdd\uff1b \u8fd4\u56de\u7c7b\u578b\u662f\u53ef\u9009\u7684\uff0c\u5982\u679c\u7701\u7565\u7684\u8bdd\uff0c\u4f1a\u5047\u5b9a\u4e3a auto (\u4f7f\u7528 \u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad )\u3002\u5c3d\u7ba1\u6211\u4eec\u4e4b\u524d\u8bf4\u8fc7\uff0c \u5e94\u8be5\u907f\u514d\u4f7f\u7528\u51fd\u6570\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u63a8\u65ad \uff0c\u4f46\u662f\u5728\u533f\u540d\u8868\u8fbe\u5f0f\u4e2d\u662f\u53ef\u4ee5\u7528\u7684\uff08\u56e0\u4e3a\u8fd9\u4e9b\u51fd\u6570\u901a\u5e38\u90fd\u975e\u5e38\u7b80\u5355\uff09\u3002 \u56e0\u4e3alambda \u6ca1\u6709\u51fd\u6570\u540d\uff0c\u6240\u4ee5\u4e0d\u5fc5\u4e3a\u5176\u8d77\u540d\u3002 \u9898\u5916\u8bdd \u4e00\u4e2a\u57fa\u672c\u7684\u533f\u540d\u51fd\u6570\u5b9a\u4e49\u770b\u4e0a\u53bb\u50cf\u662f\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 #include int main () { [] {}; // \u7701\u7565\u8fd4\u56de\u503c\u7c7b\u578b, \u65e0\u95ed\u5305\uff0c\u65e0\u53c2\u6570 return 0 ; } \u8ba9\u6211\u4eec\u7528\u533f\u540d\u51fd\u6570\u91cd\u5199\u4e0a\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include #include #include int main () { constexpr std :: array < std :: string_view , 4 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" }; // Define the function right where we use it. const auto found { std :: find_if ( arr . begin (), arr . end (), []( std :: string_view str ) // \u533f\u540d\u51fd\u6570\uff0c\u65e0\u95ed\u5305 { return ( str . find ( \"nut\" ) != std :: string_view :: npos ); }) }; if ( found == arr . end ()) { std :: cout << \"No nuts \\n \" ; } else { std :: cout << \"Found \" << * found << '\\n' ; } return 0 ; } \u548c\u4f7f\u7528\u51fd\u6570\u6307\u9488\u7684\u60c5\u51b5\u4e00\u6837\uff0c\u4ea7\u751f\u76f8\u540c\u7684\u7ed3\u679c: 1 Found walnut \u53ef\u4ee5\u770b\u5230\uff0clambda \u548c\u4e4b\u524d\u7684 containsNut \u51fd\u6570\u975e\u5e38\u7c7b\u578b\u3002\u5b83\u4eec\u7684\u53c2\u6570\u548c\u51fd\u6570\u4f53\u662f\u5b8c\u5168\u4e00\u6837\u7684\u3002\u8fd9\u4e2alambda\u6ca1\u6709\u95ed\u5305\uff08\u4e0b\u8282\u8bfe\u4ecb\u7ecd\uff09\uff0c\u56e0\u4e3a\u6ca1\u5fc5\u8981\u3002\u4e0d\u4ec5\u5982\u6b64\u6211\u4eec\u8fd8\u7701\u7565\u4e86\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u4f46\u662f\u7531\u4e8e operator!= \u8fd4\u56de bool \uff0c\u6240\u4ee5\u8be5lambda\u4e5f\u8fd4\u56de\u5e03\u5c14\u7c7b\u578b\u3002 lambda \u51fd\u6570\u7684\u7c7b\u578b \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728\u9700\u8981\u7684\u5730\u65b9\u5b9a\u4e49\u4e86\u533f\u540d\u51fd\u6570\uff0c\u5b83\u7684\u8fd9\u79cd\u7528\u6cd5\u6709\u65f6\u88ab\u79f0\u4e3a \u51fd\u6570\u5b57\u9762\u91cf \u3002 \u7136\u800c\uff0c\u5728\u540c\u4e00\u884c\u4e2d\u7f16\u5199lambda\u6709\u65f6\u4f1a\u964d\u4f4e\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u5c31\u50cf\u6211\u4eec\u53ef\u4ee5\u7528\u5b57\u9762\u503c(\u6216\u51fd\u6570\u6307\u9488)\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cf\u4ee5\u4f9b\u4ee5\u540e\u4f7f\u7528\u4e00\u6837\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u7528lambda\u5b9a\u4e49\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cf\uff0c\u7136\u540e\u5728\u4ee5\u540e\u4f7f\u7528\u5b83\u3002\u4e00\u4e2a\u6709\u597d\u540d\u5b57\u7684lambda\u51fd\u6570\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u5bb9\u6613\u9605\u8bfb\u3002 \u4f8b\u5982\uff0c\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 std::all_of \u6765\u68c0\u67e5\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u662f\u5426\u4e3a\u5076\u6570\uff1a 1 2 // Bad: We have to read the lambda to understand what's happening. return std :: all_of ( array . begin (), array . end (), []( int i ){ return (( i % 2 ) == 0 ); }); \u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u65b9\u5f0f\u63d0\u5347\u53ef\u8bfb\u6027\u3002 1 2 3 4 5 6 7 8 9 // Good: Instead, we can store the lambda in a named variable and pass it to the function. auto isEven { []( int i ) { return (( i % 2 ) == 0 ); } }; return std :: all_of ( array . begin (), array . end (), isEven ); \u6ce8\u610f\uff0c\u73b0\u5728\u6700\u540e\u4e00\u884c\u4ee3\u7801\u53ef\u4ee5\u5f88\u81ea\u7136\u5730\u88ab\u7406\u89e3\u4e3a\uff1a \u5224\u65ad\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\u662f\u5426\u90fd\u662f\u5076\u6570 \u4e0d\u8fc7\uff0c isEven \u7684\u7c7b\u578b\u662f\u4ec0\u4e48\u5462\uff1f \u4e8b\u5b9e\u8bc1\u660e\uff0clambdas\u6ca1\u6709\u53ef\u4ee5\u663e\u5f0f\u4f7f\u7528\u7684\u7c7b\u578b\u3002\u5f53\u6211\u4eec\u7f16\u5199lambda\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u4e3a\u5176\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684\u7c7b\u578b\uff0c\u4f46\u5bf9\u6211\u4eec\u5e76\u4e0d\u53ef\u89c1\u3002 \u6269\u5c55\u9605\u8bfb \u5b9e\u9645\u4e0a\uff0clambda\u5e76\u4e0d\u662f\u51fd\u6570(\u8fd9\u662f\u5b83\u4eec\u53ef\u4ee5\u907f\u514dC++\u4e0d\u652f\u6301\u5d4c\u5957\u51fd\u6570\u7684\u9650\u5236\u7684\u90e8\u5206\u539f\u56e0)\u3002\u5b83\u4eec\u662f\u4e00\u79cd\u53eb\u505afunctor\u7684\u7279\u6b8a\u5bf9\u8c61\u3002\u51fd\u5b50\u662f\u5305\u542b\u91cd\u8f7d\u7684 operator() \u7684\u5bf9\u8c61\uff0c\u4f7f\u5b83\u4eec\u50cf\u51fd\u6570\u4e00\u6837\u53ef\u8c03\u7528\u3002 \u5c3d\u7ba1\u6211\u4eec\u5e76\u4e0d\u77e5\u9053lambda\u7684\u7c7b\u578b\uff0c\u4f46\u662f\u6211\u4eec\u4ecd\u7136\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5176\u5b58\u653e\u5230\u67d0\u4e2a\u53d8\u91cf\u4e2d\u3002\u5982\u679c\u4e00\u4e2alambda\u6ca1\u6709\u95ed\u5305\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u666e\u901a\u7684\u51fd\u6570\u6307\u9488\u5b58\u653e\u5b83\u3002\u4f7f\u7528 std::function \u6216\u8005\u901a\u8fc7 auto \u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\u4e5f\u662f\u53ef\u4ee5\u7684(\u5373\u4fbf\u6b64\u65f6lambda\u6709\u95ed\u5305\u8bed\u53e5)\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include int main () { // A regular function pointer. Only works with an empty capture clause (empty []). double ( * addNumbers1 )( double , double ){ []( double a , double b ) { return ( a + b ); } }; addNumbers1 ( 1 , 2 ); // Using std::function. The lambda could have a non-empty capture clause (discussed next lesson). std :: function addNumbers2 { // note: pre-C++17, use std::function instead []( double a , double b ) { return ( a + b ); } }; addNumbers2 ( 3 , 4 ); // Using auto. Stores the lambda with its real type. auto addNumbers3 { []( double a , double b ) { return ( a + b ); } }; addNumbers3 ( 5 , 6 ); return 0 ; } \u4f7f\u7528lambda\u7684\u5b9e\u9645\u7c7b\u578b\u7684\u552f\u4e00\u65b9\u6cd5\u662f\u901a\u8fc7 auto \u3002\u4e0e std::function \u76f8\u6bd4\uff0c auto \u8fd8\u6709\u4e00\u4e2a\u597d\u5904\uff0c\u90a3\u5c31\u662f\u6ca1\u6709\u5f00\u9500\u3002 \u4e0d\u5e78\u7684\u662f\uff0c\u5728C++ 20\u4e4b\u524d\uff0c\u6211\u4eec\u4e0d\u80fd\u603b\u662f\u4f7f\u7528 auto \u3002\u5728\u5b9e\u9645lambda\u672a\u77e5\u7684\u60c5\u51b5\u4e0b(\u4f8b\u5982\uff0c\u56e0\u4e3a\u6211\u4eec\u5c06lambda\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u7531\u8c03\u7528\u8005\u51b3\u5b9a\u5c06\u4f20\u5165\u4ec0\u4e48lambda)\uff0c\u6211\u4eec\u4e0d\u80fd\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f7f\u7528 auto \u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528 std::function \u6765\u4ee3\u66ff\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include // We don't know what fn will be. std::function works with regular functions and lambdas. void repeat ( int repetitions , const std :: function < void ( int ) >& fn ) { for ( int i { 0 }; i < repetitions ; ++ i ) { fn ( i ); } } int main () { repeat ( 3 , []( int i ) { std :: cout << i << '\\n' ; }); return 0 ; } \u8f93\u51fa 1 2 3 0 1 2 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u6211\u4eec\u4e0d\u80fd\u4f7f\u7528 auto \u5173\u952e\u5b57\uff0c\u8fd9\u4e48\u505a\u7684\u8bdd\u51fd\u6570\u7684\u8c03\u7528\u8005\u5c31\u65e0\u6cd5\u77e5\u9053 fn \u5e94\u8be5\u6709\u4ec0\u4e48\u6837\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u3002\u8fd9\u4e2a\u9650\u5236\u5728C++20\u5f15\u5165 \u7f29\u5199\u51fd\u6570\u6a21\u677f \u4e4b\u540e\u5c31\u4e0d\u5b58\u5728\u4e86\u3002 \u6b64\u5916\uff0c\u56e0\u4e3a\u5b83\u4eec\u5b9e\u9645\u4e0a\u662f\u6a21\u677f\uff0c\u5177\u6709 auto \u53c2\u6570\u7684\u51fd\u6570\u4e0d\u80fd\u88ab\u5206\u79bb\u5230\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u4e2d\u3002 \u6cd5\u5219 \u9700\u8981\u7528 lambda \u521d\u59cb\u5316\u53d8\u91cf\u65f6\uff0c\u4f7f\u7528 auto \u5173\u952e\u5b57\u3002\u7f6a\u57df\u4e0d\u80fd\u7528 lambda \u521d\u59cb\u5316\u53d8\u91cf\u7684\u573a\u5408\uff0c\u4f7f\u7528 std::function \u3002 \u6cdb\u578blambda\u51fd\u6570 \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0clambda\u5f62\u53c2\u7684\u5de5\u4f5c\u89c4\u5219\u4e0e\u5e38\u89c4\u51fd\u6570\u5f62\u53c2\u76f8\u540c\u3002 \u4e00\u4e2a\u503c\u5f97\u6ce8\u610f\u7684\u4f8b\u5916\u662f\uff0c\u4eceC++ 14\u5f00\u59cb\uff0c\u6211\u4eec\u5141\u8bb8\u4f7f\u7528 auto \u4f5c\u4e3a\u5f62\u53c2\u7c7b\u578b(\u6ce8\u610f:\u5728c++ 20\u4e2d\uff0c\u5e38\u89c4\u51fd\u6570\u4e5f\u53ef\u4ee5\u4f7f\u7528 auto \u4f5c\u4e3a\u5f62\u53c2\u7c7b\u578b)\u3002\u5f53lambda\u6709\u4e00\u4e2a\u6216\u591a\u4e2a auto \u5f62\u53c2\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u4ece\u5bf9lambda\u7684\u8c03\u7528\u4e2d\u63a8\u65ad\u9700\u8981\u4ec0\u4e48\u5f62\u53c2\u7c7b\u578b\u3002 \u56e0\u4e3a\u5e26\u6709\u4e00\u4e2a\u6216\u591a\u4e2a auto \u53c2\u6570\u7684 lambda\u53ef\u4ee5\u6f5c\u5728\u5730\u4e0e\u5404\u79cd\u7c7b\u578b\u4e00\u8d77\u5de5\u4f5c\uff0c\u6240\u4ee5\u5b83\u4eec\u88ab\u79f0\u4e3a\u6cdb\u578blambda\u3002 \u6269\u5c55\u9605\u8bfb \u5f53\u5728lambda\u7684\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\u65f6\uff0c auto \u53ea\u662f\u6a21\u677f\u5f62\u53c2\u7684\u7f29\u5199\u3002 \u770b\u4e00\u4e2a\u6cdb\u578blambda\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include #include #include int main () { constexpr std :: array months { // pre-C++17 use std::array \"January\" , \"February\" , \"March\" , \"April\" , \"May\" , \"June\" , \"July\" , \"August\" , \"September\" , \"October\" , \"November\" , \"December\" }; // Search for two consecutive months that start with the same letter. const auto sameLetter { std :: adjacent_find ( months . begin (), months . end (), []( const auto & a , const auto & b ) { return ( a [ 0 ] == b [ 0 ]); }) }; // Make sure that two months were found. if ( sameLetter != months . end ()) { // std::next returns the next iterator after sameLetter std :: cout << * sameLetter << \" and \" << * std :: next ( sameLetter ) << \" start with the same letter \\n \" ; } return 0 ; } \u8f93\u51fa\uff1a 1 June and July start with the same letter \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 auto \u5f62\u53c2\u6765\u83b7\u53d6\u5b57\u7b26\u4e32\u7684 const \u5f15\u7528\u3002\u7531\u4e8e\u6240\u6709\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\u90fd\u53ef\u4ee5\u901a\u8fc7 operator[] \u8bbf\u95ee\u5355\u4e2a\u5b57\u7b26\u3002\u6240\u4ee5\u6211\u4eec\u65e0\u9700\u5173\u5fc3\u4f20\u5165\u5b57\u7b26\u4e32\u662f std::string \u3001C\u98ce\u683c\u5b57\u7b26\u4e32\u8fd8\u662f\u5176\u4ed6\u5b57\u7b26\u4e32\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u7f16\u5199\u4ecb\u7ecd\u8fd9\u4e9b\u7c7b\u578b\u7684lambda\u8868\u8fbe\u5f0f\uff0c\u5982\u679c\u5c06\u6765\u9700\u8981\u4fee\u6539 months \u7684\u7c7b\u578b\uff0c\u4e5f\u4e0d\u5fc5\u4fee\u6539lambda\u3002 \u4e0d\u8fc7\uff0c\u4f7f\u7528 auto \u5e76\u4e0d\u603b\u662f\u6700\u4f73\u7684\u9009\u62e9\uff0c\u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include #include #include int main () { constexpr std :: array months { // pre-C++17 use std::array \"January\" , \"February\" , \"March\" , \"April\" , \"May\" , \"June\" , \"July\" , \"August\" , \"September\" , \"October\" , \"November\" , \"December\" }; // Count how many months consist of 5 letters const auto fiveLetterMonths { std :: count_if ( months . begin (), months . end (), []( std :: string_view str ) { return ( str . length () == 5 ); }) }; std :: cout << \"There are \" << fiveLetterMonths << \" months with 5 letters \\n \" ; return 0 ; } \u8f93\u51fa\uff1a 1 There are 2 months with 5 letters \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u4f7f\u7528 auto \uff0c\u5219\u4f1a\u63a8\u65ad\u51fa const char* \u3002\u7531\u4e8e C \u98ce\u683c\u5b57\u7b26\u4e32\u5e76\u4e0d\u6613\u7528\uff08\u9664\u4e86 operator[] \uff09\u3002\u6240\u4ee5\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u6211\u4eec\u663e\u5f0f\u5730\u5b9a\u4e49\u53c2\u6570\u4e3a std::string_view \u7c7b\u578b\uff0c\u8fd9\u4f7f\u5f97\u6211\u4eec\u5728\u5904\u7406\u6570\u636e\u65f6\u53ef\u4ee5\u66f4\u5bb9\u6613\u3002(\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u67e5\u8be2string view \u7684\u957f\u5ea6\uff0c\u5373\u4fbf\u4f20\u5165\u7684\u662f\u4e00\u4e2aC\u98ce\u683c\u5b57\u7b26\u4e32\u6570\u7ec4)\u3002 \u6cdb\u578blambda\u548c\u9759\u6001\u53d8\u91cf \u9700\u8981\u6ce8\u610f\u7684\u4e00\u4ef6\u4e8b\u662f\uff0c\u7f16\u8bd1\u5668\u5c06\u4e3a auto \u89e3\u6790\u51fa\u7684\u6bcf\u4e2a\u4e0d\u540c\u7c7b\u578b\u751f\u6210\u552f\u4e00\u7684lambda\u8868\u8fbe\u5f0f\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u4e00\u4e2a\u6cdb\u578blambda\u5982\u4f55\u53d8\u6210\u4e24\u4e2a\u4e0d\u540c\u7684lambda\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include #include #include int main () { // Print a value and count how many times @print has been called. auto print { []( auto value ) { static int callCount { 0 }; std :: cout << callCount ++ << \": \" << value << '\\n' ; } }; print ( \"hello\" ); // 0: hello print ( \"world\" ); // 1: world print ( 1 ); // 0: 1 print ( 2 ); // 1: 2 print ( \"ding dong\" ); // 2: ding dong return 0 ; } \u8f93\u51fa\uff1a 1 2 3 4 5 0: hello 1: world 0: 1 1: 2 2: ding dong \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2alambda\uff0c\u7136\u540e\u4f7f\u7528\u4e24\u4e2a\u4e0d\u540c\u7c7b\u578b\u7684\u53c2\u6570\u5bf9\u5176\u8fdb\u884c\u4e86\u8c03\u7528\uff08\u4e00\u4e2a\u662f\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff0c\u53e6\u4e00\u4e2a\u662f\u6574\u578b\u53c2\u6570\uff09\u3002\u8fd9\u4f1a\u751f\u6210\u4e24\u4e2a\u4e0d\u540c\u7248\u672c\u7684lambda(\u4e00\u4e2a\u662f\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u5f62\u53c2\uff0c\u53e6\u4e00\u4e2a\u5e26\u6709\u6574\u6570\u5f62\u53c2)\u3002 \u5927\u591a\u6570\u65f6\u5019\uff0c\u8fd9\u662f\u65e0\u5173\u7d27\u8981\u7684\u3002\u4f46\u662f\uff0c\u8bf7\u6ce8\u610f\uff0c \u5982\u679c\u6cdb\u578blambda\u4f7f\u7528 \u9759\u6001\u53d8\u91cf \uff0c\u5219\u751f\u6210\u7684lambda\u4e4b\u95f4\u4e0d\u4f1a\u5171\u4eab\u8fd9\u4e9b\u53d8\u91cf\u3002 \u4ece\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u53ef\u4ee5\u770b\u51fa\u8fd9\u4e00\u70b9\uff0c\u5176\u4e2d\u6bcf\u79cd\u7c7b\u578b(\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u548c\u6574\u6570)\u90fd\u6709\u81ea\u5df1\u552f\u4e00\u7684\u8ba1\u6570\uff01\u867d\u7136\u6211\u4eec\u53ea\u5199\u4e86\u4e00\u6b21lambda\uff0c\u4f46\u662f\u751f\u6210\u4e86\u4e24\u4e2alambda\u2014\u2014\u5e76\u4e14\u6bcf\u4e2alambda\u90fd\u6709\u81ea\u5df1\u7684 callCount \u3002\u8981\u5728\u4e24\u4e2a\u751f\u6210\u7684lambda\u4e4b\u95f4\u6709\u4e00\u4e2a\u5171\u4eab\u8ba1\u6570\u5668\uff0c\u6211\u4eec\u5fc5\u987b\u5728lambda\u5916\u90e8\u5b9a\u4e49\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u6216\u4e00\u4e2a\u201c\u9759\u6001\u201d\u5c40\u90e8\u53d8\u91cf\u3002\u6b63\u5982\u5728\u524d\u9762\u7684\u8bfe\u7a0b\u4e2d\u4e86\u5b66\u5230\u7684\uff0c\u5168\u5c40\u548c\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u5bb9\u6613\u5e26\u6765\u95ee\u9898\uff0c\u800c\u4e14\u4f1a\u964d\u4f4e\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u5728\u4e0b\u4e00\u8bfe\u4e2d\u8ba8\u8bbalambda\u6355\u83b7\u4e4b\u540e\uff0c\u6211\u4eec\u5c06\u80fd\u591f\u907f\u514d\u8fd9\u4e9b\u53d8\u91cf\u3002 \u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad\u548c\u5c3e\u968f\u8fd4\u56de\u503c\u7c7b\u578b \u5982\u679c\u4f7f\u7528\u4e86 \u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad \uff0c\u5219 lambda \u7684\u8fd4\u56de\u503c\u7c7b\u578b\u9700\u8981\u4ece return \u8bed\u53e5\u8fdb\u884c\u63a8\u65ad\uff0c\u800c\u4e14\u6240\u6709\u7684\u8fd4\u56de\u7c7b\u578b\u5fc5\u987b\u76f8\u540c\uff08\u5426\u5219\u7f16\u8bd1\u5668\u5c06\u96be\u4ee5\u5224\u65ad\uff09\uff1a \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { auto divide { []( int x , int y , bool intDivision ) { // note: no specified return type if ( intDivision ) return x / y ; // return type is int else return static_cast < double > ( x ) / y ; // ERROR: return type doesn't match previous return type } }; std :: cout << divide ( 3 , 2 , true ) << '\\n' ; std :: cout << divide ( 3 , 2 , false ) << '\\n' ; return 0 ; } \u8fd9\u5c06\u4ea7\u751f\u4e00\u4e2a\u7f16\u8bd1\u9519\u8bef\uff0c\u56e0\u4e3a\u7b2c\u4e00\u4e2a\u8fd4\u56de\u8bed\u53e5\u7684\u8fd4\u56de\u7c7b\u578b(int)\u4e0e\u7b2c\u4e8c\u4e2a\u8fd4\u56de\u8bed\u53e5\u7684\u8fd4\u56de\u7c7b\u578b(double)\u4e0d\u5339\u914d\u3002 \u5728\u8fd4\u56de\u4e0d\u540c\u7c7b\u578b\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u6709\u4e24\u4e2a\u9009\u9879\uff1a \u662f\u5426\u663e\u5f0f\u5f3a\u5236\u8f6c\u6362\u4ee5\u4f7f\u6240\u6709\u8fd4\u56de\u7c7b\u578b\u5339\u914d\uff0c\u8fd8\u662f \u663e\u5f0f\u6307\u5b9alambda\u7684\u8fd4\u56de\u7c7b\u578b\uff0c\u5e76\u8ba9\u7f16\u8bd1\u5668\u6267\u884c\u9690\u5f0f\u8f6c\u6362\u3002 \u7b2c\u4e8c\u79cd\u60c5\u51b5\u901a\u5e38\u662f\u66f4\u597d\u7684\u9009\u62e9: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { // note: explicitly specifying this returns a double auto divide { []( int x , int y , bool intDivision ) -> double { if ( intDivision ) return x / y ; // will do an implicit conversion of result to double else return static_cast < double > ( x ) / y ; } }; std :: cout << divide ( 3 , 2 , true ) << '\\n' ; std :: cout << divide ( 3 , 2 , false ) << '\\n' ; return 0 ; } \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5982\u679c\u4eca\u540e\u4f60\u51b3\u5b9a\u4fee\u6539\u8fd4\u56de\u503c\u7684\u7c7b\u578b\uff0c\u90a3\u4e48\u4f60\u901a\u5e38\u53ea\u9700\u8981\u4fee\u6539lambda\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u5373\u53ef\uff0c\u800c\u4e0d\u5fc5\u4fee\u6539\u5176\u51fd\u6570\u5185\u5bb9\u3002 \u6807\u51c6\u5e93\u4e2d\u7684\u51fd\u6570\u5bf9\u8c61 \u5bf9\u4e8e\u5f88\u591a\u57fa\u672c\u64cd\u4f5c(\u4f8b\u5982\uff1aaddition, negation \u6216 comparison) \u6765\u8bf4\uff0c\u5176\u5b9e\u6211\u4eec\u4e0d\u9700\u8981\u81ea\u5df1\u7f16\u5199lambda\uff0c\u56e0\u4e3a\u6807\u51c6\u5e93\u4e2d\u63d0\u4f9b\u5f88\u591a\u53ef\u4ee5\u4f7f\u7528\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u3002\u5b83\u4eec\u90fd\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include #include bool greater ( int a , int b ) { // Order @a before @b if @a is greater than @b. return ( a > b ); } int main () { std :: array arr { 13 , 90 , 99 , 5 , 40 , 80 }; // Pass greater to std::sort std :: sort ( arr . begin (), arr . end (), greater ); for ( int i : arr ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\uff1a 1 99 90 80 40 13 5 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u5c06 greater \u51fd\u6570\u6539\u5199\u4e3alambda\uff0c\u800c\u662f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 std::greater \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include #include #include // for std::greater int main () { std :: array arr { 13 , 90 , 99 , 5 , 40 , 80 }; // Pass std::greater to std::sort std :: sort ( arr . begin (), arr . end (), std :: greater {}); // note: need curly braces to instantiate object for ( int i : arr ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\uff1a 1 99 90 80 40 13 5 \u5c0f\u7ed3 \u4e0e\u4f7f\u7528\u5faa\u73af\u7684\u89e3\u51b3\u65b9\u6848\u76f8\u6bd4\uff0c\u533f\u540d\u51fd\u6570\u548c\u7b97\u6cd5\u5e93\u53ef\u80fd\u7a0d\u663e\u5f97\u4e0d\u5fc5\u8981\u5730\u590d\u6742\u3002\u7136\u800c\uff0c\u8fd9\u79cd\u7ec4\u5408\u53ef\u4ee5\u5728\u77ed\u77ed\u51e0\u884c\u4ee3\u7801\u4e2d\u5b9e\u73b0\u4e00\u4e9b\u975e\u5e38\u5f3a\u5927\u7684\u64cd\u4f5c\uff0c\u5e76\u4e14\u6bd4\u81ea\u5df1\u7f16\u5199\u5faa\u73af\u66f4\u5177\u6709\u53ef\u8bfb\u6027\u3002\u6700\u91cd\u8981\u7684\u662f\uff0c\u7b97\u6cd5\u5e93\u5177\u6709\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\u5e76\u884c\u6027\uff0c\u8fd9\u662f\u5faa\u73af\u6240\u65e0\u6cd5\u83b7\u5f97\u7684\u3002\u66f4\u65b0\u4f7f\u7528\u5e93\u51fd\u6570\u7684\u6e90\u4ee3\u7801\u6bd4\u66f4\u65b0\u4f7f\u7528\u5faa\u73af\u7684\u4ee3\u7801\u66f4\u5bb9\u6613\u3002 \u533f\u540d\u51fd\u6570\u5f88\u597d\uff0c\u4f46\u5b83\u4eec\u4e0d\u80fd\u5728\u6240\u6709\u60c5\u51b5\u4e0b\u53d6\u4ee3\u5e38\u89c4\u51fd\u6570\u3002\u5bf9\u4e8e\u529f\u80fd\u590d\u6742\u4e14\u9700\u8981\u53ef\u91cd\u7528\u7684\u60c5\u5f62\uff0c\u9996\u9009\u5e38\u89c4\u51fd\u6570\u3002","title":"12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09"},{"location":"12-7-introduction-to-lambdas-anonymous-functions/#lambda","text":"\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f(\u6709\u65f6\u5019\u4e5f\u79f0lambda\u6216 \u95ed\u5305 \u6216\u533f\u540d\u51fd\u6570) \u53ef\u4ee5\u5728\u5176\u4ed6\u51fd\u6570\u4e2d\u5b9a\u4e49\u533f\u540d\u51fd\u6570\u3002\u80fd\u591f\u5d4c\u5957\u5b9a\u4e49\u662f\u5f88\u91cd\u8981\u7684\u7279\u6027\uff0c\u5b83\u53ef\u4ee5\u907f\u514d \u547d\u540d\u7a7a\u95f4 \u4e2d\u7684\u540d\u79f0\u6c61\u67d3\uff0c\u800c\u4e14\u53ef\u4ee5\u5c06\u51fd\u6570\u7684\u5b9a\u4e49\u5c3d\u53ef\u80fd\u9760\u8fd1\u4f7f\u7528\u5b83\u7684\u5730\u65b9\uff08\u907f\u514d\u989d\u5916\u7684\u4e0a\u4e0b\u6587\uff09\u3002 lambda \u7684\u8bed\u6cd5\u662fC++\u4e2d\u6700\u5947\u602a\u7684\u4e1c\u897f\u4e4b\u4e00\uff0c\u9700\u8981\u4e00\u70b9\u65f6\u95f4\u6765\u9002\u5e94\u3002\u533f\u540d\u51fd\u6570\u5f62\u5f0f\u662f: 1 2 3 4 [ captureClause ] ( parameters ) -> returnType { statements ; } \u95ed\u5305\u8bed\u53e5\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u5982\u679c\u4e0d\u9700\u8981\u6355\u83b7\u53d8\u91cf\u7684\u8bdd\uff1b \u5f62\u53c2 \u5217\u8868\u4e5f\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u5982\u679c\u4e0d\u9700\u8981\u5f62\u53c2\u7684\u8bdd\uff1b \u8fd4\u56de\u7c7b\u578b\u662f\u53ef\u9009\u7684\uff0c\u5982\u679c\u7701\u7565\u7684\u8bdd\uff0c\u4f1a\u5047\u5b9a\u4e3a auto (\u4f7f\u7528 \u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad )\u3002\u5c3d\u7ba1\u6211\u4eec\u4e4b\u524d\u8bf4\u8fc7\uff0c \u5e94\u8be5\u907f\u514d\u4f7f\u7528\u51fd\u6570\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u63a8\u65ad \uff0c\u4f46\u662f\u5728\u533f\u540d\u8868\u8fbe\u5f0f\u4e2d\u662f\u53ef\u4ee5\u7528\u7684\uff08\u56e0\u4e3a\u8fd9\u4e9b\u51fd\u6570\u901a\u5e38\u90fd\u975e\u5e38\u7b80\u5355\uff09\u3002 \u56e0\u4e3alambda \u6ca1\u6709\u51fd\u6570\u540d\uff0c\u6240\u4ee5\u4e0d\u5fc5\u4e3a\u5176\u8d77\u540d\u3002 \u9898\u5916\u8bdd \u4e00\u4e2a\u57fa\u672c\u7684\u533f\u540d\u51fd\u6570\u5b9a\u4e49\u770b\u4e0a\u53bb\u50cf\u662f\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 #include int main () { [] {}; // \u7701\u7565\u8fd4\u56de\u503c\u7c7b\u578b, \u65e0\u95ed\u5305\uff0c\u65e0\u53c2\u6570 return 0 ; } \u8ba9\u6211\u4eec\u7528\u533f\u540d\u51fd\u6570\u91cd\u5199\u4e0a\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include #include #include int main () { constexpr std :: array < std :: string_view , 4 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" }; // Define the function right where we use it. const auto found { std :: find_if ( arr . begin (), arr . end (), []( std :: string_view str ) // \u533f\u540d\u51fd\u6570\uff0c\u65e0\u95ed\u5305 { return ( str . find ( \"nut\" ) != std :: string_view :: npos ); }) }; if ( found == arr . end ()) { std :: cout << \"No nuts \\n \" ; } else { std :: cout << \"Found \" << * found << '\\n' ; } return 0 ; } \u548c\u4f7f\u7528\u51fd\u6570\u6307\u9488\u7684\u60c5\u51b5\u4e00\u6837\uff0c\u4ea7\u751f\u76f8\u540c\u7684\u7ed3\u679c: 1 Found walnut \u53ef\u4ee5\u770b\u5230\uff0clambda \u548c\u4e4b\u524d\u7684 containsNut \u51fd\u6570\u975e\u5e38\u7c7b\u578b\u3002\u5b83\u4eec\u7684\u53c2\u6570\u548c\u51fd\u6570\u4f53\u662f\u5b8c\u5168\u4e00\u6837\u7684\u3002\u8fd9\u4e2alambda\u6ca1\u6709\u95ed\u5305\uff08\u4e0b\u8282\u8bfe\u4ecb\u7ecd\uff09\uff0c\u56e0\u4e3a\u6ca1\u5fc5\u8981\u3002\u4e0d\u4ec5\u5982\u6b64\u6211\u4eec\u8fd8\u7701\u7565\u4e86\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u4f46\u662f\u7531\u4e8e operator!= \u8fd4\u56de bool \uff0c\u6240\u4ee5\u8be5lambda\u4e5f\u8fd4\u56de\u5e03\u5c14\u7c7b\u578b\u3002","title":"lambda"},{"location":"12-7-introduction-to-lambdas-anonymous-functions/#lambda_1","text":"\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728\u9700\u8981\u7684\u5730\u65b9\u5b9a\u4e49\u4e86\u533f\u540d\u51fd\u6570\uff0c\u5b83\u7684\u8fd9\u79cd\u7528\u6cd5\u6709\u65f6\u88ab\u79f0\u4e3a \u51fd\u6570\u5b57\u9762\u91cf \u3002 \u7136\u800c\uff0c\u5728\u540c\u4e00\u884c\u4e2d\u7f16\u5199lambda\u6709\u65f6\u4f1a\u964d\u4f4e\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u5c31\u50cf\u6211\u4eec\u53ef\u4ee5\u7528\u5b57\u9762\u503c(\u6216\u51fd\u6570\u6307\u9488)\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cf\u4ee5\u4f9b\u4ee5\u540e\u4f7f\u7528\u4e00\u6837\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u7528lambda\u5b9a\u4e49\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cf\uff0c\u7136\u540e\u5728\u4ee5\u540e\u4f7f\u7528\u5b83\u3002\u4e00\u4e2a\u6709\u597d\u540d\u5b57\u7684lambda\u51fd\u6570\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u5bb9\u6613\u9605\u8bfb\u3002 \u4f8b\u5982\uff0c\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 std::all_of \u6765\u68c0\u67e5\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u662f\u5426\u4e3a\u5076\u6570\uff1a 1 2 // Bad: We have to read the lambda to understand what's happening. return std :: all_of ( array . begin (), array . end (), []( int i ){ return (( i % 2 ) == 0 ); }); \u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u65b9\u5f0f\u63d0\u5347\u53ef\u8bfb\u6027\u3002 1 2 3 4 5 6 7 8 9 // Good: Instead, we can store the lambda in a named variable and pass it to the function. auto isEven { []( int i ) { return (( i % 2 ) == 0 ); } }; return std :: all_of ( array . begin (), array . end (), isEven ); \u6ce8\u610f\uff0c\u73b0\u5728\u6700\u540e\u4e00\u884c\u4ee3\u7801\u53ef\u4ee5\u5f88\u81ea\u7136\u5730\u88ab\u7406\u89e3\u4e3a\uff1a \u5224\u65ad\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\u662f\u5426\u90fd\u662f\u5076\u6570 \u4e0d\u8fc7\uff0c isEven \u7684\u7c7b\u578b\u662f\u4ec0\u4e48\u5462\uff1f \u4e8b\u5b9e\u8bc1\u660e\uff0clambdas\u6ca1\u6709\u53ef\u4ee5\u663e\u5f0f\u4f7f\u7528\u7684\u7c7b\u578b\u3002\u5f53\u6211\u4eec\u7f16\u5199lambda\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u4e3a\u5176\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684\u7c7b\u578b\uff0c\u4f46\u5bf9\u6211\u4eec\u5e76\u4e0d\u53ef\u89c1\u3002 \u6269\u5c55\u9605\u8bfb \u5b9e\u9645\u4e0a\uff0clambda\u5e76\u4e0d\u662f\u51fd\u6570(\u8fd9\u662f\u5b83\u4eec\u53ef\u4ee5\u907f\u514dC++\u4e0d\u652f\u6301\u5d4c\u5957\u51fd\u6570\u7684\u9650\u5236\u7684\u90e8\u5206\u539f\u56e0)\u3002\u5b83\u4eec\u662f\u4e00\u79cd\u53eb\u505afunctor\u7684\u7279\u6b8a\u5bf9\u8c61\u3002\u51fd\u5b50\u662f\u5305\u542b\u91cd\u8f7d\u7684 operator() \u7684\u5bf9\u8c61\uff0c\u4f7f\u5b83\u4eec\u50cf\u51fd\u6570\u4e00\u6837\u53ef\u8c03\u7528\u3002 \u5c3d\u7ba1\u6211\u4eec\u5e76\u4e0d\u77e5\u9053lambda\u7684\u7c7b\u578b\uff0c\u4f46\u662f\u6211\u4eec\u4ecd\u7136\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5176\u5b58\u653e\u5230\u67d0\u4e2a\u53d8\u91cf\u4e2d\u3002\u5982\u679c\u4e00\u4e2alambda\u6ca1\u6709\u95ed\u5305\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u666e\u901a\u7684\u51fd\u6570\u6307\u9488\u5b58\u653e\u5b83\u3002\u4f7f\u7528 std::function \u6216\u8005\u901a\u8fc7 auto \u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\u4e5f\u662f\u53ef\u4ee5\u7684(\u5373\u4fbf\u6b64\u65f6lambda\u6709\u95ed\u5305\u8bed\u53e5)\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include int main () { // A regular function pointer. Only works with an empty capture clause (empty []). double ( * addNumbers1 )( double , double ){ []( double a , double b ) { return ( a + b ); } }; addNumbers1 ( 1 , 2 ); // Using std::function. The lambda could have a non-empty capture clause (discussed next lesson). std :: function addNumbers2 { // note: pre-C++17, use std::function instead []( double a , double b ) { return ( a + b ); } }; addNumbers2 ( 3 , 4 ); // Using auto. Stores the lambda with its real type. auto addNumbers3 { []( double a , double b ) { return ( a + b ); } }; addNumbers3 ( 5 , 6 ); return 0 ; } \u4f7f\u7528lambda\u7684\u5b9e\u9645\u7c7b\u578b\u7684\u552f\u4e00\u65b9\u6cd5\u662f\u901a\u8fc7 auto \u3002\u4e0e std::function \u76f8\u6bd4\uff0c auto \u8fd8\u6709\u4e00\u4e2a\u597d\u5904\uff0c\u90a3\u5c31\u662f\u6ca1\u6709\u5f00\u9500\u3002 \u4e0d\u5e78\u7684\u662f\uff0c\u5728C++ 20\u4e4b\u524d\uff0c\u6211\u4eec\u4e0d\u80fd\u603b\u662f\u4f7f\u7528 auto \u3002\u5728\u5b9e\u9645lambda\u672a\u77e5\u7684\u60c5\u51b5\u4e0b(\u4f8b\u5982\uff0c\u56e0\u4e3a\u6211\u4eec\u5c06lambda\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u7531\u8c03\u7528\u8005\u51b3\u5b9a\u5c06\u4f20\u5165\u4ec0\u4e48lambda)\uff0c\u6211\u4eec\u4e0d\u80fd\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f7f\u7528 auto \u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528 std::function \u6765\u4ee3\u66ff\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include // We don't know what fn will be. std::function works with regular functions and lambdas. void repeat ( int repetitions , const std :: function < void ( int ) >& fn ) { for ( int i { 0 }; i < repetitions ; ++ i ) { fn ( i ); } } int main () { repeat ( 3 , []( int i ) { std :: cout << i << '\\n' ; }); return 0 ; } \u8f93\u51fa 1 2 3 0 1 2 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u6211\u4eec\u4e0d\u80fd\u4f7f\u7528 auto \u5173\u952e\u5b57\uff0c\u8fd9\u4e48\u505a\u7684\u8bdd\u51fd\u6570\u7684\u8c03\u7528\u8005\u5c31\u65e0\u6cd5\u77e5\u9053 fn \u5e94\u8be5\u6709\u4ec0\u4e48\u6837\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u3002\u8fd9\u4e2a\u9650\u5236\u5728C++20\u5f15\u5165 \u7f29\u5199\u51fd\u6570\u6a21\u677f \u4e4b\u540e\u5c31\u4e0d\u5b58\u5728\u4e86\u3002 \u6b64\u5916\uff0c\u56e0\u4e3a\u5b83\u4eec\u5b9e\u9645\u4e0a\u662f\u6a21\u677f\uff0c\u5177\u6709 auto \u53c2\u6570\u7684\u51fd\u6570\u4e0d\u80fd\u88ab\u5206\u79bb\u5230\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u4e2d\u3002 \u6cd5\u5219 \u9700\u8981\u7528 lambda \u521d\u59cb\u5316\u53d8\u91cf\u65f6\uff0c\u4f7f\u7528 auto \u5173\u952e\u5b57\u3002\u7f6a\u57df\u4e0d\u80fd\u7528 lambda \u521d\u59cb\u5316\u53d8\u91cf\u7684\u573a\u5408\uff0c\u4f7f\u7528 std::function \u3002","title":"lambda \u51fd\u6570\u7684\u7c7b\u578b"},{"location":"12-7-introduction-to-lambdas-anonymous-functions/#lambda_2","text":"\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0clambda\u5f62\u53c2\u7684\u5de5\u4f5c\u89c4\u5219\u4e0e\u5e38\u89c4\u51fd\u6570\u5f62\u53c2\u76f8\u540c\u3002 \u4e00\u4e2a\u503c\u5f97\u6ce8\u610f\u7684\u4f8b\u5916\u662f\uff0c\u4eceC++ 14\u5f00\u59cb\uff0c\u6211\u4eec\u5141\u8bb8\u4f7f\u7528 auto \u4f5c\u4e3a\u5f62\u53c2\u7c7b\u578b(\u6ce8\u610f:\u5728c++ 20\u4e2d\uff0c\u5e38\u89c4\u51fd\u6570\u4e5f\u53ef\u4ee5\u4f7f\u7528 auto \u4f5c\u4e3a\u5f62\u53c2\u7c7b\u578b)\u3002\u5f53lambda\u6709\u4e00\u4e2a\u6216\u591a\u4e2a auto \u5f62\u53c2\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u4ece\u5bf9lambda\u7684\u8c03\u7528\u4e2d\u63a8\u65ad\u9700\u8981\u4ec0\u4e48\u5f62\u53c2\u7c7b\u578b\u3002 \u56e0\u4e3a\u5e26\u6709\u4e00\u4e2a\u6216\u591a\u4e2a auto \u53c2\u6570\u7684 lambda\u53ef\u4ee5\u6f5c\u5728\u5730\u4e0e\u5404\u79cd\u7c7b\u578b\u4e00\u8d77\u5de5\u4f5c\uff0c\u6240\u4ee5\u5b83\u4eec\u88ab\u79f0\u4e3a\u6cdb\u578blambda\u3002 \u6269\u5c55\u9605\u8bfb \u5f53\u5728lambda\u7684\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\u65f6\uff0c auto \u53ea\u662f\u6a21\u677f\u5f62\u53c2\u7684\u7f29\u5199\u3002 \u770b\u4e00\u4e2a\u6cdb\u578blambda\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include #include #include int main () { constexpr std :: array months { // pre-C++17 use std::array \"January\" , \"February\" , \"March\" , \"April\" , \"May\" , \"June\" , \"July\" , \"August\" , \"September\" , \"October\" , \"November\" , \"December\" }; // Search for two consecutive months that start with the same letter. const auto sameLetter { std :: adjacent_find ( months . begin (), months . end (), []( const auto & a , const auto & b ) { return ( a [ 0 ] == b [ 0 ]); }) }; // Make sure that two months were found. if ( sameLetter != months . end ()) { // std::next returns the next iterator after sameLetter std :: cout << * sameLetter << \" and \" << * std :: next ( sameLetter ) << \" start with the same letter \\n \" ; } return 0 ; } \u8f93\u51fa\uff1a 1 June and July start with the same letter \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 auto \u5f62\u53c2\u6765\u83b7\u53d6\u5b57\u7b26\u4e32\u7684 const \u5f15\u7528\u3002\u7531\u4e8e\u6240\u6709\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\u90fd\u53ef\u4ee5\u901a\u8fc7 operator[] \u8bbf\u95ee\u5355\u4e2a\u5b57\u7b26\u3002\u6240\u4ee5\u6211\u4eec\u65e0\u9700\u5173\u5fc3\u4f20\u5165\u5b57\u7b26\u4e32\u662f std::string \u3001C\u98ce\u683c\u5b57\u7b26\u4e32\u8fd8\u662f\u5176\u4ed6\u5b57\u7b26\u4e32\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u7f16\u5199\u4ecb\u7ecd\u8fd9\u4e9b\u7c7b\u578b\u7684lambda\u8868\u8fbe\u5f0f\uff0c\u5982\u679c\u5c06\u6765\u9700\u8981\u4fee\u6539 months \u7684\u7c7b\u578b\uff0c\u4e5f\u4e0d\u5fc5\u4fee\u6539lambda\u3002 \u4e0d\u8fc7\uff0c\u4f7f\u7528 auto \u5e76\u4e0d\u603b\u662f\u6700\u4f73\u7684\u9009\u62e9\uff0c\u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include #include #include int main () { constexpr std :: array months { // pre-C++17 use std::array \"January\" , \"February\" , \"March\" , \"April\" , \"May\" , \"June\" , \"July\" , \"August\" , \"September\" , \"October\" , \"November\" , \"December\" }; // Count how many months consist of 5 letters const auto fiveLetterMonths { std :: count_if ( months . begin (), months . end (), []( std :: string_view str ) { return ( str . length () == 5 ); }) }; std :: cout << \"There are \" << fiveLetterMonths << \" months with 5 letters \\n \" ; return 0 ; } \u8f93\u51fa\uff1a 1 There are 2 months with 5 letters \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u4f7f\u7528 auto \uff0c\u5219\u4f1a\u63a8\u65ad\u51fa const char* \u3002\u7531\u4e8e C \u98ce\u683c\u5b57\u7b26\u4e32\u5e76\u4e0d\u6613\u7528\uff08\u9664\u4e86 operator[] \uff09\u3002\u6240\u4ee5\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u6211\u4eec\u663e\u5f0f\u5730\u5b9a\u4e49\u53c2\u6570\u4e3a std::string_view \u7c7b\u578b\uff0c\u8fd9\u4f7f\u5f97\u6211\u4eec\u5728\u5904\u7406\u6570\u636e\u65f6\u53ef\u4ee5\u66f4\u5bb9\u6613\u3002(\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u67e5\u8be2string view \u7684\u957f\u5ea6\uff0c\u5373\u4fbf\u4f20\u5165\u7684\u662f\u4e00\u4e2aC\u98ce\u683c\u5b57\u7b26\u4e32\u6570\u7ec4)\u3002","title":"\u6cdb\u578blambda\u51fd\u6570"},{"location":"12-7-introduction-to-lambdas-anonymous-functions/#lambda_3","text":"\u9700\u8981\u6ce8\u610f\u7684\u4e00\u4ef6\u4e8b\u662f\uff0c\u7f16\u8bd1\u5668\u5c06\u4e3a auto \u89e3\u6790\u51fa\u7684\u6bcf\u4e2a\u4e0d\u540c\u7c7b\u578b\u751f\u6210\u552f\u4e00\u7684lambda\u8868\u8fbe\u5f0f\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u4e00\u4e2a\u6cdb\u578blambda\u5982\u4f55\u53d8\u6210\u4e24\u4e2a\u4e0d\u540c\u7684lambda\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include #include #include int main () { // Print a value and count how many times @print has been called. auto print { []( auto value ) { static int callCount { 0 }; std :: cout << callCount ++ << \": \" << value << '\\n' ; } }; print ( \"hello\" ); // 0: hello print ( \"world\" ); // 1: world print ( 1 ); // 0: 1 print ( 2 ); // 1: 2 print ( \"ding dong\" ); // 2: ding dong return 0 ; } \u8f93\u51fa\uff1a 1 2 3 4 5 0: hello 1: world 0: 1 1: 2 2: ding dong \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2alambda\uff0c\u7136\u540e\u4f7f\u7528\u4e24\u4e2a\u4e0d\u540c\u7c7b\u578b\u7684\u53c2\u6570\u5bf9\u5176\u8fdb\u884c\u4e86\u8c03\u7528\uff08\u4e00\u4e2a\u662f\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff0c\u53e6\u4e00\u4e2a\u662f\u6574\u578b\u53c2\u6570\uff09\u3002\u8fd9\u4f1a\u751f\u6210\u4e24\u4e2a\u4e0d\u540c\u7248\u672c\u7684lambda(\u4e00\u4e2a\u662f\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u5f62\u53c2\uff0c\u53e6\u4e00\u4e2a\u5e26\u6709\u6574\u6570\u5f62\u53c2)\u3002 \u5927\u591a\u6570\u65f6\u5019\uff0c\u8fd9\u662f\u65e0\u5173\u7d27\u8981\u7684\u3002\u4f46\u662f\uff0c\u8bf7\u6ce8\u610f\uff0c \u5982\u679c\u6cdb\u578blambda\u4f7f\u7528 \u9759\u6001\u53d8\u91cf \uff0c\u5219\u751f\u6210\u7684lambda\u4e4b\u95f4\u4e0d\u4f1a\u5171\u4eab\u8fd9\u4e9b\u53d8\u91cf\u3002 \u4ece\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u53ef\u4ee5\u770b\u51fa\u8fd9\u4e00\u70b9\uff0c\u5176\u4e2d\u6bcf\u79cd\u7c7b\u578b(\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u548c\u6574\u6570)\u90fd\u6709\u81ea\u5df1\u552f\u4e00\u7684\u8ba1\u6570\uff01\u867d\u7136\u6211\u4eec\u53ea\u5199\u4e86\u4e00\u6b21lambda\uff0c\u4f46\u662f\u751f\u6210\u4e86\u4e24\u4e2alambda\u2014\u2014\u5e76\u4e14\u6bcf\u4e2alambda\u90fd\u6709\u81ea\u5df1\u7684 callCount \u3002\u8981\u5728\u4e24\u4e2a\u751f\u6210\u7684lambda\u4e4b\u95f4\u6709\u4e00\u4e2a\u5171\u4eab\u8ba1\u6570\u5668\uff0c\u6211\u4eec\u5fc5\u987b\u5728lambda\u5916\u90e8\u5b9a\u4e49\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u6216\u4e00\u4e2a\u201c\u9759\u6001\u201d\u5c40\u90e8\u53d8\u91cf\u3002\u6b63\u5982\u5728\u524d\u9762\u7684\u8bfe\u7a0b\u4e2d\u4e86\u5b66\u5230\u7684\uff0c\u5168\u5c40\u548c\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u5bb9\u6613\u5e26\u6765\u95ee\u9898\uff0c\u800c\u4e14\u4f1a\u964d\u4f4e\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u5728\u4e0b\u4e00\u8bfe\u4e2d\u8ba8\u8bbalambda\u6355\u83b7\u4e4b\u540e\uff0c\u6211\u4eec\u5c06\u80fd\u591f\u907f\u514d\u8fd9\u4e9b\u53d8\u91cf\u3002","title":"\u6cdb\u578blambda\u548c\u9759\u6001\u53d8\u91cf"},{"location":"12-7-introduction-to-lambdas-anonymous-functions/#_1","text":"\u5982\u679c\u4f7f\u7528\u4e86 \u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad \uff0c\u5219 lambda \u7684\u8fd4\u56de\u503c\u7c7b\u578b\u9700\u8981\u4ece return \u8bed\u53e5\u8fdb\u884c\u63a8\u65ad\uff0c\u800c\u4e14\u6240\u6709\u7684\u8fd4\u56de\u7c7b\u578b\u5fc5\u987b\u76f8\u540c\uff08\u5426\u5219\u7f16\u8bd1\u5668\u5c06\u96be\u4ee5\u5224\u65ad\uff09\uff1a \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { auto divide { []( int x , int y , bool intDivision ) { // note: no specified return type if ( intDivision ) return x / y ; // return type is int else return static_cast < double > ( x ) / y ; // ERROR: return type doesn't match previous return type } }; std :: cout << divide ( 3 , 2 , true ) << '\\n' ; std :: cout << divide ( 3 , 2 , false ) << '\\n' ; return 0 ; } \u8fd9\u5c06\u4ea7\u751f\u4e00\u4e2a\u7f16\u8bd1\u9519\u8bef\uff0c\u56e0\u4e3a\u7b2c\u4e00\u4e2a\u8fd4\u56de\u8bed\u53e5\u7684\u8fd4\u56de\u7c7b\u578b(int)\u4e0e\u7b2c\u4e8c\u4e2a\u8fd4\u56de\u8bed\u53e5\u7684\u8fd4\u56de\u7c7b\u578b(double)\u4e0d\u5339\u914d\u3002 \u5728\u8fd4\u56de\u4e0d\u540c\u7c7b\u578b\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u6709\u4e24\u4e2a\u9009\u9879\uff1a \u662f\u5426\u663e\u5f0f\u5f3a\u5236\u8f6c\u6362\u4ee5\u4f7f\u6240\u6709\u8fd4\u56de\u7c7b\u578b\u5339\u914d\uff0c\u8fd8\u662f \u663e\u5f0f\u6307\u5b9alambda\u7684\u8fd4\u56de\u7c7b\u578b\uff0c\u5e76\u8ba9\u7f16\u8bd1\u5668\u6267\u884c\u9690\u5f0f\u8f6c\u6362\u3002 \u7b2c\u4e8c\u79cd\u60c5\u51b5\u901a\u5e38\u662f\u66f4\u597d\u7684\u9009\u62e9: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { // note: explicitly specifying this returns a double auto divide { []( int x , int y , bool intDivision ) -> double { if ( intDivision ) return x / y ; // will do an implicit conversion of result to double else return static_cast < double > ( x ) / y ; } }; std :: cout << divide ( 3 , 2 , true ) << '\\n' ; std :: cout << divide ( 3 , 2 , false ) << '\\n' ; return 0 ; } \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5982\u679c\u4eca\u540e\u4f60\u51b3\u5b9a\u4fee\u6539\u8fd4\u56de\u503c\u7684\u7c7b\u578b\uff0c\u90a3\u4e48\u4f60\u901a\u5e38\u53ea\u9700\u8981\u4fee\u6539lambda\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u5373\u53ef\uff0c\u800c\u4e0d\u5fc5\u4fee\u6539\u5176\u51fd\u6570\u5185\u5bb9\u3002","title":"\u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad\u548c\u5c3e\u968f\u8fd4\u56de\u503c\u7c7b\u578b"},{"location":"12-7-introduction-to-lambdas-anonymous-functions/#_2","text":"\u5bf9\u4e8e\u5f88\u591a\u57fa\u672c\u64cd\u4f5c(\u4f8b\u5982\uff1aaddition, negation \u6216 comparison) \u6765\u8bf4\uff0c\u5176\u5b9e\u6211\u4eec\u4e0d\u9700\u8981\u81ea\u5df1\u7f16\u5199lambda\uff0c\u56e0\u4e3a\u6807\u51c6\u5e93\u4e2d\u63d0\u4f9b\u5f88\u591a\u53ef\u4ee5\u4f7f\u7528\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u3002\u5b83\u4eec\u90fd\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include #include bool greater ( int a , int b ) { // Order @a before @b if @a is greater than @b. return ( a > b ); } int main () { std :: array arr { 13 , 90 , 99 , 5 , 40 , 80 }; // Pass greater to std::sort std :: sort ( arr . begin (), arr . end (), greater ); for ( int i : arr ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\uff1a 1 99 90 80 40 13 5 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u5c06 greater \u51fd\u6570\u6539\u5199\u4e3alambda\uff0c\u800c\u662f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 std::greater \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include #include #include // for std::greater int main () { std :: array arr { 13 , 90 , 99 , 5 , 40 , 80 }; // Pass std::greater to std::sort std :: sort ( arr . begin (), arr . end (), std :: greater {}); // note: need curly braces to instantiate object for ( int i : arr ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8f93\u51fa\uff1a 1 99 90 80 40 13 5","title":"\u6807\u51c6\u5e93\u4e2d\u7684\u51fd\u6570\u5bf9\u8c61"},{"location":"12-7-introduction-to-lambdas-anonymous-functions/#_3","text":"\u4e0e\u4f7f\u7528\u5faa\u73af\u7684\u89e3\u51b3\u65b9\u6848\u76f8\u6bd4\uff0c\u533f\u540d\u51fd\u6570\u548c\u7b97\u6cd5\u5e93\u53ef\u80fd\u7a0d\u663e\u5f97\u4e0d\u5fc5\u8981\u5730\u590d\u6742\u3002\u7136\u800c\uff0c\u8fd9\u79cd\u7ec4\u5408\u53ef\u4ee5\u5728\u77ed\u77ed\u51e0\u884c\u4ee3\u7801\u4e2d\u5b9e\u73b0\u4e00\u4e9b\u975e\u5e38\u5f3a\u5927\u7684\u64cd\u4f5c\uff0c\u5e76\u4e14\u6bd4\u81ea\u5df1\u7f16\u5199\u5faa\u73af\u66f4\u5177\u6709\u53ef\u8bfb\u6027\u3002\u6700\u91cd\u8981\u7684\u662f\uff0c\u7b97\u6cd5\u5e93\u5177\u6709\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\u5e76\u884c\u6027\uff0c\u8fd9\u662f\u5faa\u73af\u6240\u65e0\u6cd5\u83b7\u5f97\u7684\u3002\u66f4\u65b0\u4f7f\u7528\u5e93\u51fd\u6570\u7684\u6e90\u4ee3\u7801\u6bd4\u66f4\u65b0\u4f7f\u7528\u5faa\u73af\u7684\u4ee3\u7801\u66f4\u5bb9\u6613\u3002 \u533f\u540d\u51fd\u6570\u5f88\u597d\uff0c\u4f46\u5b83\u4eec\u4e0d\u80fd\u5728\u6240\u6709\u60c5\u51b5\u4e0b\u53d6\u4ee3\u5e38\u89c4\u51fd\u6570\u3002\u5bf9\u4e8e\u529f\u80fd\u590d\u6742\u4e14\u9700\u8981\u53ef\u91cd\u7528\u7684\u60c5\u5f62\uff0c\u9996\u9009\u5e38\u89c4\u51fd\u6570\u3002","title":"\u5c0f\u7ed3"},{"location":"12-8-lambda-captures/","tags":["summary"],"text":"Key Takeaway \u6355\u83b7\u8bed\u53e5\u4ee5\u53ca\u6355\u83b7\u503c \u5728\u4e0a\u8282\u8bfe 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb ) \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u6574\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include #include #include int main () { std :: array < std :: string_view , 4 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" }; auto found { std :: find_if ( arr . begin (), arr . end (), []( std :: string_view str ) { return ( str . find ( \"nut\" ) != std :: string_view :: npos ); }) }; if ( found == arr . end ()) { std :: cout << \"No nuts \\n \" ; } else { std :: cout << \"Found \" << * found << '\\n' ; } return 0 ; } \u63a5\u4e0b\u6765\uff0c\u4fee\u6539\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u8ba9\u7528\u6237\u51b3\u5b9a\u9700\u8981\u641c\u7d22\u7684\u5b50\u4e32\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u5e76\u4e0d\u60f3\u4f60\u60f3\u8c61\u7684\u90a3\u4e48\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include #include #include #include int main () { std :: array < std :: string_view , 4 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" }; // Ask the user what to search for. std :: cout << \"search for: \" ; std :: string search {}; std :: cin >> search ; auto found { std :: find_if ( arr . begin (), arr . end (), []( std :: string_view str ) { // \u641c\u7d22 @search \u800c\u4e0d\u662f \"nut\". return ( str . find ( search ) != std :: string_view :: npos ); // \u9519\u8bef: search \u5e76\u4e0d\u80fd\u5728\u8fd9\u91cc\u88ab\u8bbf\u95ee }) }; if ( found == arr . end ()) { std :: cout << \"Not found \\n \" ; } else { std :: cout << \"Found \" << * found << '\\n' ; } return 0 ; } \u8fd9\u6bb5\u4ee3\u7801\u65e0\u6cd5\u7f16\u8bd1\u3002\u4e0e\u5d4c\u5957\u5757(\u5728\u5d4c\u5957\u5757\u8303\u56f4\u5185\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u5757\u4e2d\u5b9a\u4e49\u7684\u4efb\u4f55\u6807\u8bc6\u7b26)\u4e0d\u540c\uff0c lambdas\u53ea\u80fd\u8bbf\u95ee\u7279\u5b9a\u7c7b\u578b\u7684\u6807\u8bc6\u7b26\uff1a\u5168\u5c40\u53d8\u91cf\u3001\u7f16\u8bd1\u65f6\u5df2\u77e5\u7684\u5b9e\u4f53\u548c\u5177\u6709 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u7684\u5b9e\u4f53 \u3002 search \u4e0d\u6ee1\u8db3\u8fd9\u4e9b\u8981\u6c42\uff0c\u6240\u4ee5lambda\u65e0\u6cd5\u770b\u5230\u5b83\u3002\u8fd9\u5c31\u662f\u6355\u83b7\u8bed\u53e5\u7684\u4f5c\u7528\u3002 \u95ed\u5305 \u6355\u83b7\u8bed\u53e5\u7528\u4e8e\u4e3a\u5e2e\u52a9 lambda \u8bbf\u95ee\u5728\u5176\u5185\u90e8\u65e0\u6cd5\u8bbf\u95ee\u7684\u666e\u901a\u53d8\u91cf\u3002\u6211\u4eec\u8981\u505a\u7684\u5c31\u662f\u5c06\u9700\u8981\u8bbf\u95ee\u7684\u5b9e\u4f53\u5217\u4e3e\u5728\u6355\u83b7\u8bed\u53e5\u4e2d\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u8ba9lambda\u8bbf\u95ee search \u53d8\u91cf\uff0c\u6240\u4ee5\u5c06\u5176\u52a0\u5165\u6355\u83b7\u8bed\u53e5\u5373\u53ef\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include #include #include #include #include int main () { std :: array < std :: string_view , 4 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" }; std :: cout << \"search for: \" ; std :: string search {}; std :: cin >> search ; // Capture @search vvvvvv auto found { std :: find_if ( arr . begin (), arr . end (), [ search ]( std :: string_view str ) { return ( str . find ( search ) != std :: string_view :: npos ); }) }; if ( found == arr . end ()) { std :: cout << \"Not found \\n \" ; } else { std :: cout << \"Found \" << * found << '\\n' ; } return 0 ; } \u6ce8\u610f\uff0c\u8fd9\u6837\u7528\u6237\u5c31\u53ef\u4ee5\u5728\u6570\u7ec4\u4e2d\u641c\u7d22\u4efb\u4f55\u503c\u4e86\uff1a \u8f93\u51fa\uff1a 1 2 search for: nana Found banana \u95ed\u5305\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f \u867d\u7136\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684lambda\u770b\u8d77\u6765\u50cf\u662f\u76f4\u63a5\u8bbf\u95ee\u4e86main\u51fd\u6570\u4e2d\u7684 search \u53d8\u91cf\uff0c\u4f46\u4e8b\u5b9e\u5e76\u975e\u5982\u6b64\u3002\u867d\u7136 lambda \u53ef\u80fd\u770b\u8d77\u6765\u50cf\u5d4c\u5957\u5757\uff0c\u4f46\u5b83\u4eec\u7684\u5de5\u4f5c\u65b9\u5f0f\u7565\u6709\u4e0d\u540c(\u800c\u4e14\u8fd9\u79cd\u533a\u522b\u5f88\u91cd\u8981)\u3002 \u5f53\u6267\u884clambda\u5b9a\u4e49\u65f6\uff0c\u5bf9\u4e8elambda\u6355\u83b7\u7684\u6bcf\u4e2a\u53d8\u91cf\uff0c\u5c06\u5728lambda\u5185\u90e8\u751f\u6210\u8be5\u53d8\u91cf\u7684\u514b\u9686(\u5177\u6709\u76f8\u540c\u7684\u540d\u79f0)\u3002\u6b64\u65f6\uff0c\u8fd9\u4e9b\u514b\u9686\u53d8\u91cf\u662f\u4ece\u540c\u540d\u7684\u5916\u90e8\u4f5c\u7528\u57df\u53d8\u91cf\u521d\u59cb\u5316\u7684\u3002 \u56e0\u6b64\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53lambda\u5bf9\u8c61\u88ab\u521b\u5efa\u65f6\uff0clambda\u5c06\u83b7\u5f97\u81ea\u5df1\u7684\u514b\u9686\u53d8\u91cf search \u3002\u8fd9\u4e2a\u514b\u9686\u7684 search \u4e0e main \u51fd\u6570\u4e2d\u7684 search \u5177\u6709\u76f8\u540c\u7684\u503c\uff0c\u56e0\u6b64\u5b83\u7684\u884c\u4e3a\u5c31\u50cf\u8bbf\u95ee main \u51fd\u6570\u4e2d\u7684 search \u4e00\u6837\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u3002 \u867d\u7136\u8fd9\u4e9b\u514b\u9686\u7684\u53d8\u91cf\u5177\u6709\u76f8\u540c\u7684\u540d\u79f0\uff0c \u4f46\u5b83\u4eec\u4e0d\u4e00\u5b9a\u5177\u6709\u4e0e\u539f\u59cb\u53d8\u91cf\u76f8\u540c\u7684\u7c7b\u578b \u3002\u6211\u4eec\u5c06\u5728\u672c\u8bfe\u63a5\u4e0b\u6765\u7684\u7ae0\u8282\u4e2d\u63a2\u8ba8\u8fd9\u4e2a\u95ee\u9898\u3002 \u5173\u952e\u4fe1\u606f \u6355\u83b7\u7684\u53d8\u91cf\u662f\u5916\u5c42\u539f\u59cb\u53d8\u91cf\u7684\u4e00\u4efd\u514b\u9686\uff0c\u800c\u4e0d\u662f\u5b9e\u9645\u7684\u539f\u53d8\u91cf\u3002 \u6269\u5c55\u9605\u8bfb \u5c3d\u7ba1 lambda \u770b\u4e0a\u53bb\u50cf\u662f\u51fd\u6570\uff0c\u4f46\u5176\u5b9e\u5e76\u4e0d\u662f\uff0c\u5b83\u53ea\u662f\u53ef\u4ee5\u88ab\u50cf\u51fd\u6570\u4e00\u6837\u8c03\u7528\u7684\u5bf9\u8c61\u800c\u5df2(\u79f0\u4e3a functor \u2014\u2014 \u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u5982\u4f55\u5b9a\u4e49\u81ea\u5df1\u7684functor\u7684)\u3002 \u5f53\u7f16\u8bd1\u5668\u9047\u5230lambda\u5b9a\u4e49\u65f6\uff0c\u5b83\u4e3a\u8be5lambda\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5bf9\u8c61\u5b9a\u4e49\u3002\u6bcf\u4e2a\u6355\u83b7\u7684\u53d8\u91cf\u90fd\u4f1a\u6210\u4e3a\u5bf9\u8c61\u7684\u6570\u636e\u6210\u5458\u3002 \u5728\u8fd0\u884c\u65f6\uff0c\u5f53\u9047\u5230lambda\u5b9a\u4e49\u65f6\uff0c\u5c06\u5b9e\u4f8b\u5316lambda\u5bf9\u8c61\uff0c\u5e76\u5728\u6b64\u65f6\u521d\u59cb\u5316lambda\u7684\u6210\u5458\u3002 \u9ed8\u8ba4\u6355\u83b7\u7684\u662fconst\u503c \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u53d8\u91cf\u88ab\u6355\u83b7\u4e3a const \u503c\u3002\u8fd9\u610f\u5473\u7740\u5728\u521b\u5efalambda\u65f6\uff0clambda\u6355\u83b7\u5916\u90e8\u4f5c\u7528\u57df\u53d8\u91cf\u7684const\u526f\u672c\uff0c\u8fd9\u610f\u5473\u7740\u4e0d\u5141\u8bb8lambda\u4fee\u6539\u5b83\u4eec\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6355\u83b7\u53d8\u91cf ammo \u5e76\u5c1d\u8bd5\u9012\u51cf\u5b83\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include int main () { int ammo { 10 }; // \u5b9a\u4e49\u4e00\u4e2a lambda \u5e76\u5c06\u5176\u5b58\u653e\u5728 \"shoot\" \u4e2d auto shoot { [ ammo ]() { // \u975e\u6cd5\u64cd\u4f5c, ammo \u662fconst\u7684 -- ammo ; std :: cout << \"Pew! \" << ammo << \" shot(s) left. \\n \" ; } }; // Call the lambda shoot (); std :: cout << ammo << \" shot(s) left \\n \" ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7531\u4e8e\u6355\u83b7\u7684 ammo \u662f const \u53d8\u91cf\u3002\u6240\u4ee5\u5e76\u4e0d\u80fd\u88ab\u4fee\u6539\uff0c\u5426\u5219\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002 \u6355\u83b7\u53d8\u91cf\u8bbe\u7f6e\u4e3a\u53ef\u53d8\u503c \u4e3a\u4e86\u5141\u8bb8\u4fee\u6539\u88ab\u6355\u83b7\u7684\u53d8\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06lambda\u6807\u8bb0\u4e3a mutable \u3002\u5728\u6b64\u4e0a\u4e0b\u6587\u4e2d\uff0c mutable \u5173\u952e\u5b57\u4ece\u6309\u503c\u6355\u83b7\u7684\u5168\u90e8\u53d8\u91cf\u4e2d\u5220\u9664 const \u9650\u5b9a\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include int main () { int ammo { 10 }; auto shoot { // \u5728\u53c2\u6570\u5217\u8868\u540e\u9762\u6dfb\u52a0 mutable \u5173\u952e\u5b57 [ ammo ]() mutable { // \u73b0\u5728\u53ef\u4ee5\u4fee\u6539 ammo \u4e86 -- ammo ; std :: cout << \"Pew! \" << ammo << \" shot(s) left. \\n \" ; } }; shoot (); shoot (); std :: cout << ammo << \" shot(s) left \\n \" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 Pew! 9 shot(s) left. Pew! 8 shot(s) left. 10 shot(s) left \u867d\u7136\u4ee3\u7801\u53ef\u4ee5\u7f16\u8bd1\uff0c\u4f46\u662f\u5b58\u5728\u903b\u8f91\u9519\u8bef\u3002\u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\uff1f\u5f53lambda\u88ab\u8c03\u7528\u65f6\uff0c\u5b83\u6355\u83b7\u7684\u662f ammo \u7684\u526f\u672c\uff0c\u5c3d\u7ba1 lambda \u5c06 ammo \u4ece 10 \u9012\u51cf\u5230 9 \u518d\u5230 8 \uff0c\u4f46\u662f\u5b83\u9012\u51cf\u7684\u662f\u5176\u62f7\u8d1d\uff0c\u800c\u4e0d\u662f\u539f\u672c\u7684\u503c\u3002 \u6ce8\u610f\uff0c ammo \u7684\u503c\u4f1a\u5728\u591a\u6b21lambda\u8c03\u7528\u76f4\u63a5\u4fdd\u6301\uff01 \u6ce8\u610f \u56e0\u4e3a\u6355\u83b7\u7684\u53d8\u91cf\u662flambda\u5bf9\u8c61\u7684\u6210\u5458\uff0c\u6240\u4ee5\u5b83\u4eec\u7684\u503c\u8de8\u591a\u6b21\u8c03\u7528lambda! \u6355\u83b7\u53d8\u91cf\u7684\u5f15\u7528 \u5c31\u50cf\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u6765\u66f4\u6539\u53c2\u6570\u7684\u503c\u4e00\u6837\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u6765\u6355\u83b7\u53d8\u91cf\uff0c\u4ee5\u5141\u8bb8lambda\u4fee\u6539\u53c2\u6570\u7684\u503c\u3002 \u8981\u901a\u8fc7\u5f15\u7528\u6355\u83b7\u53d8\u91cf\uff0c\u9700\u8981\u5728\u6355\u83b7\u7684\u53d8\u91cf\u540d\u524d\u52a0\u4e0a & \u53f7\u3002\u4e0e\u901a\u8fc7\u503c\u6355\u83b7\u7684\u53d8\u91cf\u4e0d\u540c\uff0c\u901a\u8fc7\u5f15\u7528\u6355\u83b7\u7684\u53d8\u91cf\u662f\u975econst\u7684\uff0c\u9664\u975e\u5b83\u4eec\u6355\u83b7\u7684\u53d8\u91cf\u662f const \u3002\u5f53\u4f60\u901a\u5e38\u559c\u6b22\u901a\u8fc7\u5f15\u7528\u5c06\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\u65f6(\u4f8b\u5982\uff0c\u5bf9\u4e8e\u975e\u57fa\u672c\u7c7b\u578b)\uff0c\u901a\u8fc7\u5f15\u7528\u6355\u83b7\u5e94\u8be5\u4f18\u5148\u4e8e\u901a\u8fc7\u503c\u6355\u83b7\u3002 \u4fee\u6539\u4e4b\u524d\u7684\u4f8b\u5b50\uff0c\u6355\u83b7 ammo \u7684\u5f15\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int main () { int ammo { 10 }; auto shoot { // We don't need mutable anymore [ & ammo ]() { // &ammo means ammo is captured by reference // Changes to ammo will affect main's ammo -- ammo ; std :: cout << \"Pew! \" << ammo << \" shot(s) left. \\n \" ; } }; shoot (); std :: cout << ammo << \" shot(s) left \\n \" ; return 0 ; } \u7ed3\u679c\u5982\u6211\u4eec\u671f\u671b\u7684\u90a3\u6837\uff1a 1 2 Pew! 9 shot(s) left. 9 shot(s) left \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528\u6309\u5f15\u7528\u6355\u83b7\u6765\u7edf\u8ba1 std::sort \u5bf9\u6570\u7ec4\u6392\u5e8f\u65f6\u8fdb\u884c\u7684\u6bd4\u8f83\u6b21\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include #include #include struct Car { std :: string make {}; std :: string model {}; }; int main () { std :: array < Car , 3 > cars { { { \"Volkswagen\" , \"Golf\" }, { \"Toyota\" , \"Corolla\" }, { \"Honda\" , \"Civic\" } } }; int comparisons { 0 }; std :: sort ( cars . begin (), cars . end (), // Capture @comparisons by reference. [ & comparisons ]( const auto & a , const auto & b ) { // We captured comparisons by reference. We can modify it without \"mutable\". ++ comparisons ; // Sort the cars by their make. return ( a . make < b . make ); }); std :: cout << \"Comparisons: \" << comparisons << '\\n' ; for ( const auto & car : cars ) { std :: cout << car . make << ' ' << car . model << '\\n' ; } return 0 ; } \u53ef\u80fd\u7684\u8f93\u51fa\u662f\uff1a 1 2 3 4 Comparisons: 2 Honda Civic Toyota Corolla Volkswagen Golf \u6355\u83b7\u591a\u4e2a\u53d8\u91cf \u53ef\u4ee5\u901a\u8fc7\u7528\u9017\u53f7\u5206\u9694\u591a\u4e2a\u53d8\u91cf\u6765\u6355\u83b7\u5b83\u4eec\u3002\u8fd9\u5176\u4e2d\u53ef\u4ee5\u5305\u62ec\u901a\u8fc7\u503c\u6216\u5f15\u7528\u6355\u83b7\u7684\u53d8\u91cf\u7684\u7ec4\u5408\uff1a 1 2 3 4 5 6 int health { 33 }; int armor { 100 }; std :: vector < CEnemy > enemies {}; // Capture health and armor by value, and enemies by reference. [ health , armor , & enemies ](){}; \u9ed8\u8ba4\u6355\u83b7 \u5fc5\u987b\u663e\u5f0f\u5730\u5217\u51fa\u60f3\u8981\u6355\u83b7\u7684\u53d8\u91cf\u662f\u5f88\u9ebb\u70e6\u7684\u3002\u5982\u679c\u4fee\u6539lambda\uff0c\u53ef\u80fd\u4f1a\u5fd8\u8bb0\u6dfb\u52a0\u6216\u5220\u9664\u6355\u83b7\u7684\u53d8\u91cf\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u7f16\u8bd1\u5668\u7684\u5e2e\u52a9\u6765\u81ea\u52a8\u751f\u6210\u9700\u8981\u6355\u83b7\u7684\u53d8\u91cf\u5217\u8868\u3002 \u9ed8\u8ba4\u6355\u83b7(\u4e5f\u79f0\u4e3acapture-default)\u6355\u83b7lambda\u4e2d\u63d0\u5230\u7684\u6240\u6709\u53d8\u91cf\uff0c\u4f46\u4e0d\u4f1a\u6355\u83b7lambda\u4e2d\u672a\u63d0\u53ca\u7684\u53d8\u91cf\u3002 \u6309\u503c\u6355\u83b7\u6240\u6709\u4f7f\u7528\u7684\u53d8\u91cf\uff0c\u4f7f\u7528\u6355\u83b7\u503c = \uff1b \u6309\u5f15\u7528\u6355\u83b7\u6240\u6709\u4f7f\u7528\u7684\u53d8\u91cf\uff0c\u8bf7\u4f7f\u7528\u6355\u83b7\u503c & \u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u9ed8\u8ba4\u503c\u6355\u83b7\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include #include #include int main () { std :: array areas { 100 , 25 , 121 , 40 , 56 }; int width {}; int height {}; std :: cout << \"Enter width and height: \" ; std :: cin >> width >> height ; auto found { std :: find_if ( areas . begin (), areas . end (), [ = ]( int knownArea ) { // will default capture width and height by value return ( width * height == knownArea ); // because they're mentioned here }) }; if ( found == areas . end ()) { std :: cout << \"I don't know this area :( \\n \" ; } else { std :: cout << \"Area found :) \\n \" ; } return 0 ; } \u9ed8\u8ba4\u6355\u83b7\u53ef\u4ee5\u4e0e\u666e\u901a\u6355\u83b7\u6df7\u5408\u4f7f\u7528\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u503c\u6355\u83b7\u4e00\u4e9b\u53d8\u91cf\uff0c\u901a\u8fc7\u5f15\u7528\u6355\u83b7\u53e6\u4e00\u4e9b\u53d8\u91cf\uff0c\u4f46\u662f\u6bcf\u4e2a\u53d8\u91cf\u53ea\u80fd\u6355\u83b7\u4e00\u6b21\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 int health { 33 }; int armor { 100 }; std :: vector < CEnemy > enemies {}; // \u6309\u503c\u6355\u83b7 health \u548c armor \uff0c\u6309\u5f15\u7528\u6355\u83b7 enemies [ health , armor , & enemies ](){}; // \u6309\u5f15\u7528\u6355\u83b7 enemies\uff0c\u5269\u4e0b\u7684\u90fd\u6309\u503c\u6355\u83b7 [ = , & enemies ](){}; // \u6309\u503c\u6355\u83b7 armor\uff0c\u5269\u4e0b\u7684\u90fd\u6309\u5f15\u7528\u6355\u83b7 [ & , armor ](){}; // \u9519\u8bef\uff1a\u5df2\u7ecf\u8868\u660e\u6309\u5f15\u7528\u6355\u83b7\u6240\u6709\u53d8\u91cf [ & , & armor ](){}; // \u9519\u8bef\uff1a\u5df2\u7ecf\u8868\u660e\u6309\u503c\u6355\u83b7\u6240\u6709\u53d8\u91cf [ = , armor ](){}; // \u9519\u8bef\uff1aarmor \u51fa\u73b0\u4e86\u4e24\u6b21 [ armor , & health , & armor ](){}; // \u9519\u8bef\uff1a\u9ed8\u8ba4\u6355\u83b7\u5fc5\u987b\u653e\u5728\u6700\u524d\u9762 [ armor , & ](){}; \u5728\u95ed\u5305\u4e2d\u5b9a\u4e49\u65b0\u7684\u53d8\u91cf \u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5bf9\u6355\u83b7\u7684\u53d8\u91cf\u8fdb\u884c\u7a0d\u8bb8\u4fee\u6539\uff0c\u6216\u8005\u662f\u58f0\u660e\u4e00\u4e2a\u4ec5\u5728 lambda \u4f5c\u7528\u57df\u4e2d\u53ef\u89c1\u7684\u53d8\u91cf\uff0c\u6b64\u65f6\u6211\u4eec\u53ef\u4ee5\u5728\u95ed\u5305\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\u800c\u4e14\u4e0d\u5fc5\u6307\u660e\u5176\u7c7b\u578b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include #include #include int main () { std :: array areas { 100 , 25 , 121 , 40 , 56 }; int width {}; int height {}; std :: cout << \"Enter width and height: \" ; std :: cin >> width >> height ; // \u6211\u4eec\u9700\u8981\u9762\u79ef\uff0c\u4f46\u662f\u7528\u6237\u8f93\u5165\u7684\u662f\u957f\u5bbd\u3002 // \u6240\u4ee5\u9700\u8981\u5728\u641c\u7d22\u9762\u79ef\u524d\u9996\u5148\u8ba1\u7b97\u9762\u79ef auto found { std :: find_if ( areas . begin (), areas . end (), // \u58f0\u660e\u4e00\u4e2a\u4ec5 lambda \u53ef\u89c1\u7684\u53d8\u91cf // userArea \u4f1a\u88ab\u81ea\u52a8\u63a8\u65ad\u4e3a int. [ userArea { width * height }]( int knownArea ) { return ( userArea == knownArea ); }) }; if ( found == areas . end ()) { std :: cout << \"I don't know this area :( \\n \" ; } else { std :: cout << \"Area found :) \\n \" ; } return 0 ; } \u5f53lambda\u88ab\u5b9a\u4e49\u65f6\uff0c userArea \u53ea\u4f1a\u88ab\u8ba1\u7b97\u4e00\u6b21\u3002\u8ba1\u7b97\u7684\u9762\u79ef\u5b58\u50a8\u5728lambda\u5bf9\u8c61\u4e2d\uff0c\u6bcf\u6b21\u8c03\u7528\u90fd\u662f\u76f8\u540c\u7684\u3002\u5982\u679clambda\u662f\u53ef\u53d8\u7684\uff0c\u5e76\u4e14\u4fee\u6539\u4e86\u6355\u83b7\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u5219\u539f\u59cb\u503c\u5c06\u88ab\u8986\u76d6\u3002 \u6700\u4f73\u5b9e\u8df5 \u53ea\u6709\u5f53\u53d8\u91cf\u7684\u5b58\u5728\u65f6\u95f4\u5f88\u77ed\u4e14\u7c7b\u578b\u5f88\u660e\u663e\u65f6\uff0c\u624d\u5728\u6355\u83b7\u4e2d\u7684\u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u53d8\u91cf\u3002\u5426\u5219\uff0c\u6700\u597d\u5728lambda\u4e4b\u5916\u5b9a\u4e49\u53d8\u91cf\u5e76\u6355\u83b7\u5b83\u3002 \u60ac\u5782\u7684\u6355\u83b7\u53d8\u91cf \u53d8\u91cf\u5728\u5b9a\u4e49lambda\u7684\u5730\u65b9\u88ab\u6355\u83b7\u3002\u5982\u679c\u5f15\u7528\u6355\u83b7\u7684\u53d8\u91cf\u5728lambda\u4e4b\u524d\u88ab\u9500\u6bc1\uff0c\u5219lambda\u5c06\u5f97\u5230\u4e00\u4e2a \u60ac\u5782 \u5f15\u7528\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include // \u8fd4\u56de\u4e00\u4e2a lambda auto makeWalrus ( const std :: string & name ) { // \u6309\u5f15\u7528\u6355\u83b7 name \u5e76\u8fd4\u56de\u4e00\u4e2alambda return [ & ]() { std :: cout << \"I am a walrus, my name is \" << name << '\\n' ; // \u672a\u5b9a\u4e49\u884c\u4e3a }; } int main () { // \u521b\u5efa\u4e00\u4e2a\u540d\u4e3aroofus\u7684\u65b0\u7684 walrus // sayName \u662f makeWalrus \u8fd4\u56de\u7684 lambda auto sayName { makeWalrus ( \"Roofus\" ) }; // \u8c03\u7528 makeWalrus \u8fd4\u56de\u7684 lambda sayName (); return 0 ; } \u8c03\u7528 makeWalrus \u4f1a\u4ece\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u201cRoofus\u201d\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u7684 std::string \u3002 makeWalrus \u4e2d\u7684lambda\u4f1a\u6309\u5f15\u7528\u6355\u83b7\u8be5\u5b57\u7b26\u4e32\u3002\u5f53 makeWalrus \u8fd4\u56de\u65f6\uff0c\u8be5\u5b57\u7b26\u4e32\u4f1a\u9500\u6bc1\uff0c\u4f46\u662f lambda \u4ecd\u7136\u4f1a\u4f7f\u7528\u5b83\u7684\u5f15\u7528\u3002\u4e8e\u662f\u5f53\u6211\u4eec\u8c03\u7528 sayName \u65f6\uff0c\u5c31\u4f1a\u8bbf\u95ee\u8be5\u60ac\u5782\u5f15\u7528\uff0c\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u6ce8\u610f\uff0c\u5373\u4f7f name \u662f \u6309\u503c\u4f20\u9012 \u7ed9 makeWalrus \u7684\u3002\u53d8\u91cf name \u4ecd\u7136\u4f1a\u5728 makeWalrus \u7ed3\u675f\u65f6\u9500\u6bc1\uff0clambda\u6301\u6709\u7684\u4ecd\u7136\u662f\u60ac\u5782\u5f15\u7528\u3002 \u6ce8\u610f \u5728\u901a\u8fc7\u5f15\u7528\u6355\u83b7\u53d8\u91cf\u65f6\u8981\u683c\u5916\u5c0f\u5fc3\uff0c\u7279\u522b\u662f\u4f7f\u7528\u9ed8\u8ba4\u5f15\u7528\u6355\u83b7\u65f6\u3002\u6355\u83b7\u7684\u53d8\u91cf\u5fc5\u987b\u6bd4lambda\u5b58\u6d3b\u7684\u65f6\u95f4\u957f\u3002 \u5982\u679c\u6211\u4eec\u60f3\u8981\u5728\u4f7f\u7528lambda\u65f6\u4ecd\u7136\u4fdd\u8bc1 name \u53ef\u7528\uff0c\u5219\u9700\u8981\u5728\u95ed\u5305\u4e2d\u6309\u503c\u6355\u83b7\u8be5\u53d8\u91cf\uff08\u663e\u5f0f\u6307\u5b9a\u6216\u9ed8\u8ba4\u6309\u503c\u6355\u83b7\uff09\u3002 \u65e0\u610f\u95f4\u62f7\u8d1d\u53ef\u53d8\u7684 lambda \u56e0\u4e3alambda\u662f\u5bf9\u8c61\uff0c\u6240\u4ee5\u5b83\u4eec\u53ef\u4ee5\u88ab\u590d\u5236\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u95ee\u9898\u3002\u8003\u8651\u4ee5\u4e0b\u4ee3\u7801: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int main () { int i { 0 }; // Create a new lambda named count auto count { [ i ]() mutable { std :: cout << ++ i << '\\n' ; } }; count (); // invoke count auto otherCount { count }; // create a copy of count // invoke both count and the copy count (); otherCount (); return 0 ; } \u8f93\u51fa\uff1a 1 2 3 1 2 2 \u7a0b\u5e8f\u6ca1\u6709\u6253\u5370 1, 2, 3\uff0c\u800c\u662f\u6253\u5370\u4e86\u4e24\u4e2a2\u3002\u6211\u4eec\u521b\u5efa otherCount \u65f6\u62f7\u8d1d\u4e86 count \uff0c\u540c\u65f6\u4e5f\u62f7\u8d1d\u4e86 count \u5f53\u524d\u7684\u72b6\u6001\u3002 count \u7684 i \u662f 1\uff0c\u6240\u4ee5 otherCount \u7684 i \u4e5f\u662f 1\u3002\u56e0\u4e3a otherCount \u662f count \u7684\u62f7\u8d1d\uff0c\u6240\u4ee5\u5b83\u4eec\u6709\u5404\u81ea\u7684 i \u3002 \u518d\u770b\u4e00\u4e2a\u4e0d\u592a\u660e\u663e\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include void myInvoke ( const std :: function < void () >& fn ) { fn (); } int main () { int i { 0 }; // Increments and prints its local copy of @i. auto count { [ i ]() mutable { std :: cout << ++ i << '\\n' ; } }; myInvoke ( count ); myInvoke ( count ); myInvoke ( count ); return 0 ; } \u8f93\u51fa\uff1a 1 2 3 1 1 1 \u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u5b58\u5728\u76f8\u540c\u7684\u95ee\u9898\uff0c\u4f46\u662f\u66f4\u52a0\u9690\u853d\u3002\u5f53\u901a\u8fc7lambda\u521b\u5efa std::function \u65f6\uff0c std::function \u4f1a\u521b\u5efa lambda \u7684\u62f7\u8d1d\u3002\u56e0\u6b64\u5728\u8c03\u7528 fn() \u65f6\uff0c\u6267\u884c\u7684\u5176\u5b9e\u662flambda \u7684\u62f7\u8d1d\u800c\u4e0d\u662f\u5176\u672c\u8eab\u3002 \u5982\u679c\u6211\u4eec\u9700\u8981\u4f20\u9012\u4e00\u4e2a\u53ef\u53d8\u7684lambda\uff0c\u5e76\u4e14\u60f3\u8981\u907f\u514d\u65e0\u610f\u5730\u590d\u5236\uff0c\u6709\u4e24\u4e2a\u65b9\u6cd5\u3002\u4e00\u79cd\u662f\u4f7f\u7528\u975e\u6355\u83b7lambda\u6765\u4ee3\u66ff\u2014\u2014\u5728\u4e0a\u9762\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5220\u9664\u6355\u83b7\u5e76\u4f7f\u7528\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u6765\u8ddf\u8e2a\u72b6\u6001\u3002\u4f46\u662f\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u5f88\u96be\u8ddf\u8e2a\uff0c\u5e76\u4e14\u4f7f\u6211\u4eec\u7684\u4ee3\u7801\u53ef\u8bfb\u6027\u8f83\u5dee\u3002\u4e00\u4e2a\u66f4\u597d\u7684\u9009\u62e9\u662f\u4ece\u4e00\u5f00\u59cb\u5c31\u963b\u6b62lambda\u7684\u590d\u5236\u3002\u4f46\u662f\u7531\u4e8e\u6211\u4eec\u4e0d\u80fd\u5f71\u54cd std::function (\u6216\u5176\u4ed6\u6807\u51c6\u5e93\u51fd\u6570\u6216\u5bf9\u8c61)\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u90a3\u8981\u5982\u4f55\u624d\u80fd\u505a\u5230\u8fd9\u4e00\u70b9\u5462? \u5e78\u8fd0\u7684\u662f\uff0cC++ \u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u52a0\u65b9\u4fbf\u7684\u7c7b\u578b(\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d) \u2014\u2014\u79f0\u4e3a std::reference_wrapper \u3002\u8be5\u7c7b\u578b\u4f7f\u6211\u4eec\u53ef\u4ee5\u4f20\u5165\u4e00\u4e2a\u666e\u901a\u7c7b\u578b\uff0c\u4f46\u5f53\u505a\u5f15\u7528\u4f7f\u7528\u3002\u66f4\u597d\u5730\u662f\uff0c std::reference_wrapper \u53ef\u4ee5\u901a\u8fc7 std::ref() \u51fd\u6570\u521b\u5efa\u3002\u5982\u679c\u6211\u4eec\u5c06 lambda \u5305\u88c5\u5230 std::reference_wrapper \u4e2d\uff0c\u5219\u6bcf\u5f53\u6709\u4eba\u60f3\u62f7\u8d1d lambda \u65f6\uff0c\u5b83\u4eec\u4f1a\u62f7\u8d1d\u4e00\u4e2a\u5f15\u7528\u800c\u4e0d\u662f\u5bf9\u8c61\u672c\u8eab\u3002 \u4f7f\u7528 std::ref \u66f4\u65b0\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include void myInvoke ( const std :: function < void () >& fn ) { fn (); } int main () { int i { 0 }; // Increments and prints its local copy of @i. auto count { [ i ]() mutable { std :: cout << ++ i << '\\n' ; } }; // std::ref(count) \u4f7f\u5f97 count \u88ab\u5f53\u505a\u5f15\u7528\u5bf9\u5f85 // \u56e0\u6b64, \u4efb\u4f55\u5bf9\u5176\u7684\u62f7\u8d1d\u90fd\u5b9e\u9645\u62f7\u8d1d\u7684\u662f\u5f15\u7528\uff0c\u786e\u4fdd\u59cb\u7ec8\u53ea\u5b58\u5728\u4e00\u4e2a count myInvoke ( std :: ref ( count )); myInvoke ( std :: ref ( count )); myInvoke ( std :: ref ( count )); return 0 ; } \u8f93\u51fa\u7b26\u5408\u9884\u671f\uff1a 1 2 3 1 2 3 \u6ce8\u610f\uff0c invoke \u6309\u503c\u83b7\u53d6 fn \u7ed3\u679c\u4e5f\u662f\u4e00\u6837\u7684\u3002\u5982\u679c\u6211\u4eec\u901a\u8fc7 std::ref \u521b\u5efa\u5b83\uff0c\u5219 std::function \u4e0d\u4f1a\u521b\u5efa lambda \u7684\u526f\u672c\u3002 \u6cd5\u5219 \u6807\u51c6\u5e93\u51fd\u6570\u53ef\u80fd\u4f1a\u590d\u5236\u51fd\u6570\u5bf9\u8c61(\u63d0\u9192:lambda\u662f\u51fd\u6570\u5bf9\u8c61)\u3002\u5982\u679c\u4f60\u60f3\u63d0\u4f9b\u5e26\u6709\u53ef\u53d8\u6355\u83b7\u53d8\u91cf\u7684lambda\uff0c\u53ef\u4ee5\u4f7f\u7528 std::ref \u901a\u8fc7\u5f15\u7528\u4f20\u9012\u5b83\u4eec\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c3d\u91cf\u907f\u514d\u53ef\u53d8\u7684lambda\u3002\u4e0d\u53ef\u53d8lambda\u66f4\u5bb9\u6613\u7406\u89e3\uff0c\u4e0d\u4f1a\u51fa\u73b0\u4e0a\u8ff0\u95ee\u9898\uff0c\u4e5f\u4e0d\u4f1a\u5728\u6dfb\u52a0\u5e76\u884c\u6267\u884c\u65f6\u51fa\u73b0\u66f4\u5371\u9669\u7684\u95ee\u9898\u3002","title":"12.8 - lambda \u95ed\u5305"},{"location":"12-8-lambda-captures/#_1","text":"\u5728\u4e0a\u8282\u8bfe 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb ) \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u6574\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include #include #include int main () { std :: array < std :: string_view , 4 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" }; auto found { std :: find_if ( arr . begin (), arr . end (), []( std :: string_view str ) { return ( str . find ( \"nut\" ) != std :: string_view :: npos ); }) }; if ( found == arr . end ()) { std :: cout << \"No nuts \\n \" ; } else { std :: cout << \"Found \" << * found << '\\n' ; } return 0 ; } \u63a5\u4e0b\u6765\uff0c\u4fee\u6539\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u8ba9\u7528\u6237\u51b3\u5b9a\u9700\u8981\u641c\u7d22\u7684\u5b50\u4e32\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u5e76\u4e0d\u60f3\u4f60\u60f3\u8c61\u7684\u90a3\u4e48\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include #include #include #include int main () { std :: array < std :: string_view , 4 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" }; // Ask the user what to search for. std :: cout << \"search for: \" ; std :: string search {}; std :: cin >> search ; auto found { std :: find_if ( arr . begin (), arr . end (), []( std :: string_view str ) { // \u641c\u7d22 @search \u800c\u4e0d\u662f \"nut\". return ( str . find ( search ) != std :: string_view :: npos ); // \u9519\u8bef: search \u5e76\u4e0d\u80fd\u5728\u8fd9\u91cc\u88ab\u8bbf\u95ee }) }; if ( found == arr . end ()) { std :: cout << \"Not found \\n \" ; } else { std :: cout << \"Found \" << * found << '\\n' ; } return 0 ; } \u8fd9\u6bb5\u4ee3\u7801\u65e0\u6cd5\u7f16\u8bd1\u3002\u4e0e\u5d4c\u5957\u5757(\u5728\u5d4c\u5957\u5757\u8303\u56f4\u5185\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u5757\u4e2d\u5b9a\u4e49\u7684\u4efb\u4f55\u6807\u8bc6\u7b26)\u4e0d\u540c\uff0c lambdas\u53ea\u80fd\u8bbf\u95ee\u7279\u5b9a\u7c7b\u578b\u7684\u6807\u8bc6\u7b26\uff1a\u5168\u5c40\u53d8\u91cf\u3001\u7f16\u8bd1\u65f6\u5df2\u77e5\u7684\u5b9e\u4f53\u548c\u5177\u6709 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u7684\u5b9e\u4f53 \u3002 search \u4e0d\u6ee1\u8db3\u8fd9\u4e9b\u8981\u6c42\uff0c\u6240\u4ee5lambda\u65e0\u6cd5\u770b\u5230\u5b83\u3002\u8fd9\u5c31\u662f\u6355\u83b7\u8bed\u53e5\u7684\u4f5c\u7528\u3002","title":"\u6355\u83b7\u8bed\u53e5\u4ee5\u53ca\u6355\u83b7\u503c"},{"location":"12-8-lambda-captures/#_2","text":"\u6355\u83b7\u8bed\u53e5\u7528\u4e8e\u4e3a\u5e2e\u52a9 lambda \u8bbf\u95ee\u5728\u5176\u5185\u90e8\u65e0\u6cd5\u8bbf\u95ee\u7684\u666e\u901a\u53d8\u91cf\u3002\u6211\u4eec\u8981\u505a\u7684\u5c31\u662f\u5c06\u9700\u8981\u8bbf\u95ee\u7684\u5b9e\u4f53\u5217\u4e3e\u5728\u6355\u83b7\u8bed\u53e5\u4e2d\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u8ba9lambda\u8bbf\u95ee search \u53d8\u91cf\uff0c\u6240\u4ee5\u5c06\u5176\u52a0\u5165\u6355\u83b7\u8bed\u53e5\u5373\u53ef\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include #include #include #include #include int main () { std :: array < std :: string_view , 4 > arr { \"apple\" , \"banana\" , \"walnut\" , \"lemon\" }; std :: cout << \"search for: \" ; std :: string search {}; std :: cin >> search ; // Capture @search vvvvvv auto found { std :: find_if ( arr . begin (), arr . end (), [ search ]( std :: string_view str ) { return ( str . find ( search ) != std :: string_view :: npos ); }) }; if ( found == arr . end ()) { std :: cout << \"Not found \\n \" ; } else { std :: cout << \"Found \" << * found << '\\n' ; } return 0 ; } \u6ce8\u610f\uff0c\u8fd9\u6837\u7528\u6237\u5c31\u53ef\u4ee5\u5728\u6570\u7ec4\u4e2d\u641c\u7d22\u4efb\u4f55\u503c\u4e86\uff1a \u8f93\u51fa\uff1a 1 2 search for: nana Found banana","title":"\u95ed\u5305"},{"location":"12-8-lambda-captures/#_3","text":"\u867d\u7136\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684lambda\u770b\u8d77\u6765\u50cf\u662f\u76f4\u63a5\u8bbf\u95ee\u4e86main\u51fd\u6570\u4e2d\u7684 search \u53d8\u91cf\uff0c\u4f46\u4e8b\u5b9e\u5e76\u975e\u5982\u6b64\u3002\u867d\u7136 lambda \u53ef\u80fd\u770b\u8d77\u6765\u50cf\u5d4c\u5957\u5757\uff0c\u4f46\u5b83\u4eec\u7684\u5de5\u4f5c\u65b9\u5f0f\u7565\u6709\u4e0d\u540c(\u800c\u4e14\u8fd9\u79cd\u533a\u522b\u5f88\u91cd\u8981)\u3002 \u5f53\u6267\u884clambda\u5b9a\u4e49\u65f6\uff0c\u5bf9\u4e8elambda\u6355\u83b7\u7684\u6bcf\u4e2a\u53d8\u91cf\uff0c\u5c06\u5728lambda\u5185\u90e8\u751f\u6210\u8be5\u53d8\u91cf\u7684\u514b\u9686(\u5177\u6709\u76f8\u540c\u7684\u540d\u79f0)\u3002\u6b64\u65f6\uff0c\u8fd9\u4e9b\u514b\u9686\u53d8\u91cf\u662f\u4ece\u540c\u540d\u7684\u5916\u90e8\u4f5c\u7528\u57df\u53d8\u91cf\u521d\u59cb\u5316\u7684\u3002 \u56e0\u6b64\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53lambda\u5bf9\u8c61\u88ab\u521b\u5efa\u65f6\uff0clambda\u5c06\u83b7\u5f97\u81ea\u5df1\u7684\u514b\u9686\u53d8\u91cf search \u3002\u8fd9\u4e2a\u514b\u9686\u7684 search \u4e0e main \u51fd\u6570\u4e2d\u7684 search \u5177\u6709\u76f8\u540c\u7684\u503c\uff0c\u56e0\u6b64\u5b83\u7684\u884c\u4e3a\u5c31\u50cf\u8bbf\u95ee main \u51fd\u6570\u4e2d\u7684 search \u4e00\u6837\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u3002 \u867d\u7136\u8fd9\u4e9b\u514b\u9686\u7684\u53d8\u91cf\u5177\u6709\u76f8\u540c\u7684\u540d\u79f0\uff0c \u4f46\u5b83\u4eec\u4e0d\u4e00\u5b9a\u5177\u6709\u4e0e\u539f\u59cb\u53d8\u91cf\u76f8\u540c\u7684\u7c7b\u578b \u3002\u6211\u4eec\u5c06\u5728\u672c\u8bfe\u63a5\u4e0b\u6765\u7684\u7ae0\u8282\u4e2d\u63a2\u8ba8\u8fd9\u4e2a\u95ee\u9898\u3002 \u5173\u952e\u4fe1\u606f \u6355\u83b7\u7684\u53d8\u91cf\u662f\u5916\u5c42\u539f\u59cb\u53d8\u91cf\u7684\u4e00\u4efd\u514b\u9686\uff0c\u800c\u4e0d\u662f\u5b9e\u9645\u7684\u539f\u53d8\u91cf\u3002 \u6269\u5c55\u9605\u8bfb \u5c3d\u7ba1 lambda \u770b\u4e0a\u53bb\u50cf\u662f\u51fd\u6570\uff0c\u4f46\u5176\u5b9e\u5e76\u4e0d\u662f\uff0c\u5b83\u53ea\u662f\u53ef\u4ee5\u88ab\u50cf\u51fd\u6570\u4e00\u6837\u8c03\u7528\u7684\u5bf9\u8c61\u800c\u5df2(\u79f0\u4e3a functor \u2014\u2014 \u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u5982\u4f55\u5b9a\u4e49\u81ea\u5df1\u7684functor\u7684)\u3002 \u5f53\u7f16\u8bd1\u5668\u9047\u5230lambda\u5b9a\u4e49\u65f6\uff0c\u5b83\u4e3a\u8be5lambda\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5bf9\u8c61\u5b9a\u4e49\u3002\u6bcf\u4e2a\u6355\u83b7\u7684\u53d8\u91cf\u90fd\u4f1a\u6210\u4e3a\u5bf9\u8c61\u7684\u6570\u636e\u6210\u5458\u3002 \u5728\u8fd0\u884c\u65f6\uff0c\u5f53\u9047\u5230lambda\u5b9a\u4e49\u65f6\uff0c\u5c06\u5b9e\u4f8b\u5316lambda\u5bf9\u8c61\uff0c\u5e76\u5728\u6b64\u65f6\u521d\u59cb\u5316lambda\u7684\u6210\u5458\u3002","title":"\u95ed\u5305\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f"},{"location":"12-8-lambda-captures/#const","text":"\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u53d8\u91cf\u88ab\u6355\u83b7\u4e3a const \u503c\u3002\u8fd9\u610f\u5473\u7740\u5728\u521b\u5efalambda\u65f6\uff0clambda\u6355\u83b7\u5916\u90e8\u4f5c\u7528\u57df\u53d8\u91cf\u7684const\u526f\u672c\uff0c\u8fd9\u610f\u5473\u7740\u4e0d\u5141\u8bb8lambda\u4fee\u6539\u5b83\u4eec\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6355\u83b7\u53d8\u91cf ammo \u5e76\u5c1d\u8bd5\u9012\u51cf\u5b83\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include int main () { int ammo { 10 }; // \u5b9a\u4e49\u4e00\u4e2a lambda \u5e76\u5c06\u5176\u5b58\u653e\u5728 \"shoot\" \u4e2d auto shoot { [ ammo ]() { // \u975e\u6cd5\u64cd\u4f5c, ammo \u662fconst\u7684 -- ammo ; std :: cout << \"Pew! \" << ammo << \" shot(s) left. \\n \" ; } }; // Call the lambda shoot (); std :: cout << ammo << \" shot(s) left \\n \" ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7531\u4e8e\u6355\u83b7\u7684 ammo \u662f const \u53d8\u91cf\u3002\u6240\u4ee5\u5e76\u4e0d\u80fd\u88ab\u4fee\u6539\uff0c\u5426\u5219\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002","title":"\u9ed8\u8ba4\u6355\u83b7\u7684\u662fconst\u503c"},{"location":"12-8-lambda-captures/#_4","text":"\u4e3a\u4e86\u5141\u8bb8\u4fee\u6539\u88ab\u6355\u83b7\u7684\u53d8\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06lambda\u6807\u8bb0\u4e3a mutable \u3002\u5728\u6b64\u4e0a\u4e0b\u6587\u4e2d\uff0c mutable \u5173\u952e\u5b57\u4ece\u6309\u503c\u6355\u83b7\u7684\u5168\u90e8\u53d8\u91cf\u4e2d\u5220\u9664 const \u9650\u5b9a\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include int main () { int ammo { 10 }; auto shoot { // \u5728\u53c2\u6570\u5217\u8868\u540e\u9762\u6dfb\u52a0 mutable \u5173\u952e\u5b57 [ ammo ]() mutable { // \u73b0\u5728\u53ef\u4ee5\u4fee\u6539 ammo \u4e86 -- ammo ; std :: cout << \"Pew! \" << ammo << \" shot(s) left. \\n \" ; } }; shoot (); shoot (); std :: cout << ammo << \" shot(s) left \\n \" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 Pew! 9 shot(s) left. Pew! 8 shot(s) left. 10 shot(s) left \u867d\u7136\u4ee3\u7801\u53ef\u4ee5\u7f16\u8bd1\uff0c\u4f46\u662f\u5b58\u5728\u903b\u8f91\u9519\u8bef\u3002\u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\uff1f\u5f53lambda\u88ab\u8c03\u7528\u65f6\uff0c\u5b83\u6355\u83b7\u7684\u662f ammo \u7684\u526f\u672c\uff0c\u5c3d\u7ba1 lambda \u5c06 ammo \u4ece 10 \u9012\u51cf\u5230 9 \u518d\u5230 8 \uff0c\u4f46\u662f\u5b83\u9012\u51cf\u7684\u662f\u5176\u62f7\u8d1d\uff0c\u800c\u4e0d\u662f\u539f\u672c\u7684\u503c\u3002 \u6ce8\u610f\uff0c ammo \u7684\u503c\u4f1a\u5728\u591a\u6b21lambda\u8c03\u7528\u76f4\u63a5\u4fdd\u6301\uff01 \u6ce8\u610f \u56e0\u4e3a\u6355\u83b7\u7684\u53d8\u91cf\u662flambda\u5bf9\u8c61\u7684\u6210\u5458\uff0c\u6240\u4ee5\u5b83\u4eec\u7684\u503c\u8de8\u591a\u6b21\u8c03\u7528lambda!","title":"\u6355\u83b7\u53d8\u91cf\u8bbe\u7f6e\u4e3a\u53ef\u53d8\u503c"},{"location":"12-8-lambda-captures/#_5","text":"\u5c31\u50cf\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u6765\u66f4\u6539\u53c2\u6570\u7684\u503c\u4e00\u6837\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u6765\u6355\u83b7\u53d8\u91cf\uff0c\u4ee5\u5141\u8bb8lambda\u4fee\u6539\u53c2\u6570\u7684\u503c\u3002 \u8981\u901a\u8fc7\u5f15\u7528\u6355\u83b7\u53d8\u91cf\uff0c\u9700\u8981\u5728\u6355\u83b7\u7684\u53d8\u91cf\u540d\u524d\u52a0\u4e0a & \u53f7\u3002\u4e0e\u901a\u8fc7\u503c\u6355\u83b7\u7684\u53d8\u91cf\u4e0d\u540c\uff0c\u901a\u8fc7\u5f15\u7528\u6355\u83b7\u7684\u53d8\u91cf\u662f\u975econst\u7684\uff0c\u9664\u975e\u5b83\u4eec\u6355\u83b7\u7684\u53d8\u91cf\u662f const \u3002\u5f53\u4f60\u901a\u5e38\u559c\u6b22\u901a\u8fc7\u5f15\u7528\u5c06\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\u65f6(\u4f8b\u5982\uff0c\u5bf9\u4e8e\u975e\u57fa\u672c\u7c7b\u578b)\uff0c\u901a\u8fc7\u5f15\u7528\u6355\u83b7\u5e94\u8be5\u4f18\u5148\u4e8e\u901a\u8fc7\u503c\u6355\u83b7\u3002 \u4fee\u6539\u4e4b\u524d\u7684\u4f8b\u5b50\uff0c\u6355\u83b7 ammo \u7684\u5f15\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int main () { int ammo { 10 }; auto shoot { // We don't need mutable anymore [ & ammo ]() { // &ammo means ammo is captured by reference // Changes to ammo will affect main's ammo -- ammo ; std :: cout << \"Pew! \" << ammo << \" shot(s) left. \\n \" ; } }; shoot (); std :: cout << ammo << \" shot(s) left \\n \" ; return 0 ; } \u7ed3\u679c\u5982\u6211\u4eec\u671f\u671b\u7684\u90a3\u6837\uff1a 1 2 Pew! 9 shot(s) left. 9 shot(s) left \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528\u6309\u5f15\u7528\u6355\u83b7\u6765\u7edf\u8ba1 std::sort \u5bf9\u6570\u7ec4\u6392\u5e8f\u65f6\u8fdb\u884c\u7684\u6bd4\u8f83\u6b21\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include #include #include struct Car { std :: string make {}; std :: string model {}; }; int main () { std :: array < Car , 3 > cars { { { \"Volkswagen\" , \"Golf\" }, { \"Toyota\" , \"Corolla\" }, { \"Honda\" , \"Civic\" } } }; int comparisons { 0 }; std :: sort ( cars . begin (), cars . end (), // Capture @comparisons by reference. [ & comparisons ]( const auto & a , const auto & b ) { // We captured comparisons by reference. We can modify it without \"mutable\". ++ comparisons ; // Sort the cars by their make. return ( a . make < b . make ); }); std :: cout << \"Comparisons: \" << comparisons << '\\n' ; for ( const auto & car : cars ) { std :: cout << car . make << ' ' << car . model << '\\n' ; } return 0 ; } \u53ef\u80fd\u7684\u8f93\u51fa\u662f\uff1a 1 2 3 4 Comparisons: 2 Honda Civic Toyota Corolla Volkswagen Golf","title":"\u6355\u83b7\u53d8\u91cf\u7684\u5f15\u7528"},{"location":"12-8-lambda-captures/#_6","text":"\u53ef\u4ee5\u901a\u8fc7\u7528\u9017\u53f7\u5206\u9694\u591a\u4e2a\u53d8\u91cf\u6765\u6355\u83b7\u5b83\u4eec\u3002\u8fd9\u5176\u4e2d\u53ef\u4ee5\u5305\u62ec\u901a\u8fc7\u503c\u6216\u5f15\u7528\u6355\u83b7\u7684\u53d8\u91cf\u7684\u7ec4\u5408\uff1a 1 2 3 4 5 6 int health { 33 }; int armor { 100 }; std :: vector < CEnemy > enemies {}; // Capture health and armor by value, and enemies by reference. [ health , armor , & enemies ](){};","title":"\u6355\u83b7\u591a\u4e2a\u53d8\u91cf"},{"location":"12-8-lambda-captures/#_7","text":"\u5fc5\u987b\u663e\u5f0f\u5730\u5217\u51fa\u60f3\u8981\u6355\u83b7\u7684\u53d8\u91cf\u662f\u5f88\u9ebb\u70e6\u7684\u3002\u5982\u679c\u4fee\u6539lambda\uff0c\u53ef\u80fd\u4f1a\u5fd8\u8bb0\u6dfb\u52a0\u6216\u5220\u9664\u6355\u83b7\u7684\u53d8\u91cf\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u7f16\u8bd1\u5668\u7684\u5e2e\u52a9\u6765\u81ea\u52a8\u751f\u6210\u9700\u8981\u6355\u83b7\u7684\u53d8\u91cf\u5217\u8868\u3002 \u9ed8\u8ba4\u6355\u83b7(\u4e5f\u79f0\u4e3acapture-default)\u6355\u83b7lambda\u4e2d\u63d0\u5230\u7684\u6240\u6709\u53d8\u91cf\uff0c\u4f46\u4e0d\u4f1a\u6355\u83b7lambda\u4e2d\u672a\u63d0\u53ca\u7684\u53d8\u91cf\u3002 \u6309\u503c\u6355\u83b7\u6240\u6709\u4f7f\u7528\u7684\u53d8\u91cf\uff0c\u4f7f\u7528\u6355\u83b7\u503c = \uff1b \u6309\u5f15\u7528\u6355\u83b7\u6240\u6709\u4f7f\u7528\u7684\u53d8\u91cf\uff0c\u8bf7\u4f7f\u7528\u6355\u83b7\u503c & \u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u9ed8\u8ba4\u503c\u6355\u83b7\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include #include #include int main () { std :: array areas { 100 , 25 , 121 , 40 , 56 }; int width {}; int height {}; std :: cout << \"Enter width and height: \" ; std :: cin >> width >> height ; auto found { std :: find_if ( areas . begin (), areas . end (), [ = ]( int knownArea ) { // will default capture width and height by value return ( width * height == knownArea ); // because they're mentioned here }) }; if ( found == areas . end ()) { std :: cout << \"I don't know this area :( \\n \" ; } else { std :: cout << \"Area found :) \\n \" ; } return 0 ; } \u9ed8\u8ba4\u6355\u83b7\u53ef\u4ee5\u4e0e\u666e\u901a\u6355\u83b7\u6df7\u5408\u4f7f\u7528\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u503c\u6355\u83b7\u4e00\u4e9b\u53d8\u91cf\uff0c\u901a\u8fc7\u5f15\u7528\u6355\u83b7\u53e6\u4e00\u4e9b\u53d8\u91cf\uff0c\u4f46\u662f\u6bcf\u4e2a\u53d8\u91cf\u53ea\u80fd\u6355\u83b7\u4e00\u6b21\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 int health { 33 }; int armor { 100 }; std :: vector < CEnemy > enemies {}; // \u6309\u503c\u6355\u83b7 health \u548c armor \uff0c\u6309\u5f15\u7528\u6355\u83b7 enemies [ health , armor , & enemies ](){}; // \u6309\u5f15\u7528\u6355\u83b7 enemies\uff0c\u5269\u4e0b\u7684\u90fd\u6309\u503c\u6355\u83b7 [ = , & enemies ](){}; // \u6309\u503c\u6355\u83b7 armor\uff0c\u5269\u4e0b\u7684\u90fd\u6309\u5f15\u7528\u6355\u83b7 [ & , armor ](){}; // \u9519\u8bef\uff1a\u5df2\u7ecf\u8868\u660e\u6309\u5f15\u7528\u6355\u83b7\u6240\u6709\u53d8\u91cf [ & , & armor ](){}; // \u9519\u8bef\uff1a\u5df2\u7ecf\u8868\u660e\u6309\u503c\u6355\u83b7\u6240\u6709\u53d8\u91cf [ = , armor ](){}; // \u9519\u8bef\uff1aarmor \u51fa\u73b0\u4e86\u4e24\u6b21 [ armor , & health , & armor ](){}; // \u9519\u8bef\uff1a\u9ed8\u8ba4\u6355\u83b7\u5fc5\u987b\u653e\u5728\u6700\u524d\u9762 [ armor , & ](){};","title":"\u9ed8\u8ba4\u6355\u83b7"},{"location":"12-8-lambda-captures/#_8","text":"\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5bf9\u6355\u83b7\u7684\u53d8\u91cf\u8fdb\u884c\u7a0d\u8bb8\u4fee\u6539\uff0c\u6216\u8005\u662f\u58f0\u660e\u4e00\u4e2a\u4ec5\u5728 lambda \u4f5c\u7528\u57df\u4e2d\u53ef\u89c1\u7684\u53d8\u91cf\uff0c\u6b64\u65f6\u6211\u4eec\u53ef\u4ee5\u5728\u95ed\u5305\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\u800c\u4e14\u4e0d\u5fc5\u6307\u660e\u5176\u7c7b\u578b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include #include #include int main () { std :: array areas { 100 , 25 , 121 , 40 , 56 }; int width {}; int height {}; std :: cout << \"Enter width and height: \" ; std :: cin >> width >> height ; // \u6211\u4eec\u9700\u8981\u9762\u79ef\uff0c\u4f46\u662f\u7528\u6237\u8f93\u5165\u7684\u662f\u957f\u5bbd\u3002 // \u6240\u4ee5\u9700\u8981\u5728\u641c\u7d22\u9762\u79ef\u524d\u9996\u5148\u8ba1\u7b97\u9762\u79ef auto found { std :: find_if ( areas . begin (), areas . end (), // \u58f0\u660e\u4e00\u4e2a\u4ec5 lambda \u53ef\u89c1\u7684\u53d8\u91cf // userArea \u4f1a\u88ab\u81ea\u52a8\u63a8\u65ad\u4e3a int. [ userArea { width * height }]( int knownArea ) { return ( userArea == knownArea ); }) }; if ( found == areas . end ()) { std :: cout << \"I don't know this area :( \\n \" ; } else { std :: cout << \"Area found :) \\n \" ; } return 0 ; } \u5f53lambda\u88ab\u5b9a\u4e49\u65f6\uff0c userArea \u53ea\u4f1a\u88ab\u8ba1\u7b97\u4e00\u6b21\u3002\u8ba1\u7b97\u7684\u9762\u79ef\u5b58\u50a8\u5728lambda\u5bf9\u8c61\u4e2d\uff0c\u6bcf\u6b21\u8c03\u7528\u90fd\u662f\u76f8\u540c\u7684\u3002\u5982\u679clambda\u662f\u53ef\u53d8\u7684\uff0c\u5e76\u4e14\u4fee\u6539\u4e86\u6355\u83b7\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u5219\u539f\u59cb\u503c\u5c06\u88ab\u8986\u76d6\u3002 \u6700\u4f73\u5b9e\u8df5 \u53ea\u6709\u5f53\u53d8\u91cf\u7684\u5b58\u5728\u65f6\u95f4\u5f88\u77ed\u4e14\u7c7b\u578b\u5f88\u660e\u663e\u65f6\uff0c\u624d\u5728\u6355\u83b7\u4e2d\u7684\u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u53d8\u91cf\u3002\u5426\u5219\uff0c\u6700\u597d\u5728lambda\u4e4b\u5916\u5b9a\u4e49\u53d8\u91cf\u5e76\u6355\u83b7\u5b83\u3002","title":"\u5728\u95ed\u5305\u4e2d\u5b9a\u4e49\u65b0\u7684\u53d8\u91cf"},{"location":"12-8-lambda-captures/#_9","text":"\u53d8\u91cf\u5728\u5b9a\u4e49lambda\u7684\u5730\u65b9\u88ab\u6355\u83b7\u3002\u5982\u679c\u5f15\u7528\u6355\u83b7\u7684\u53d8\u91cf\u5728lambda\u4e4b\u524d\u88ab\u9500\u6bc1\uff0c\u5219lambda\u5c06\u5f97\u5230\u4e00\u4e2a \u60ac\u5782 \u5f15\u7528\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include // \u8fd4\u56de\u4e00\u4e2a lambda auto makeWalrus ( const std :: string & name ) { // \u6309\u5f15\u7528\u6355\u83b7 name \u5e76\u8fd4\u56de\u4e00\u4e2alambda return [ & ]() { std :: cout << \"I am a walrus, my name is \" << name << '\\n' ; // \u672a\u5b9a\u4e49\u884c\u4e3a }; } int main () { // \u521b\u5efa\u4e00\u4e2a\u540d\u4e3aroofus\u7684\u65b0\u7684 walrus // sayName \u662f makeWalrus \u8fd4\u56de\u7684 lambda auto sayName { makeWalrus ( \"Roofus\" ) }; // \u8c03\u7528 makeWalrus \u8fd4\u56de\u7684 lambda sayName (); return 0 ; } \u8c03\u7528 makeWalrus \u4f1a\u4ece\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u201cRoofus\u201d\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u7684 std::string \u3002 makeWalrus \u4e2d\u7684lambda\u4f1a\u6309\u5f15\u7528\u6355\u83b7\u8be5\u5b57\u7b26\u4e32\u3002\u5f53 makeWalrus \u8fd4\u56de\u65f6\uff0c\u8be5\u5b57\u7b26\u4e32\u4f1a\u9500\u6bc1\uff0c\u4f46\u662f lambda \u4ecd\u7136\u4f1a\u4f7f\u7528\u5b83\u7684\u5f15\u7528\u3002\u4e8e\u662f\u5f53\u6211\u4eec\u8c03\u7528 sayName \u65f6\uff0c\u5c31\u4f1a\u8bbf\u95ee\u8be5\u60ac\u5782\u5f15\u7528\uff0c\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u6ce8\u610f\uff0c\u5373\u4f7f name \u662f \u6309\u503c\u4f20\u9012 \u7ed9 makeWalrus \u7684\u3002\u53d8\u91cf name \u4ecd\u7136\u4f1a\u5728 makeWalrus \u7ed3\u675f\u65f6\u9500\u6bc1\uff0clambda\u6301\u6709\u7684\u4ecd\u7136\u662f\u60ac\u5782\u5f15\u7528\u3002 \u6ce8\u610f \u5728\u901a\u8fc7\u5f15\u7528\u6355\u83b7\u53d8\u91cf\u65f6\u8981\u683c\u5916\u5c0f\u5fc3\uff0c\u7279\u522b\u662f\u4f7f\u7528\u9ed8\u8ba4\u5f15\u7528\u6355\u83b7\u65f6\u3002\u6355\u83b7\u7684\u53d8\u91cf\u5fc5\u987b\u6bd4lambda\u5b58\u6d3b\u7684\u65f6\u95f4\u957f\u3002 \u5982\u679c\u6211\u4eec\u60f3\u8981\u5728\u4f7f\u7528lambda\u65f6\u4ecd\u7136\u4fdd\u8bc1 name \u53ef\u7528\uff0c\u5219\u9700\u8981\u5728\u95ed\u5305\u4e2d\u6309\u503c\u6355\u83b7\u8be5\u53d8\u91cf\uff08\u663e\u5f0f\u6307\u5b9a\u6216\u9ed8\u8ba4\u6309\u503c\u6355\u83b7\uff09\u3002","title":"\u60ac\u5782\u7684\u6355\u83b7\u53d8\u91cf"},{"location":"12-8-lambda-captures/#lambda","text":"\u56e0\u4e3alambda\u662f\u5bf9\u8c61\uff0c\u6240\u4ee5\u5b83\u4eec\u53ef\u4ee5\u88ab\u590d\u5236\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u95ee\u9898\u3002\u8003\u8651\u4ee5\u4e0b\u4ee3\u7801: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int main () { int i { 0 }; // Create a new lambda named count auto count { [ i ]() mutable { std :: cout << ++ i << '\\n' ; } }; count (); // invoke count auto otherCount { count }; // create a copy of count // invoke both count and the copy count (); otherCount (); return 0 ; } \u8f93\u51fa\uff1a 1 2 3 1 2 2 \u7a0b\u5e8f\u6ca1\u6709\u6253\u5370 1, 2, 3\uff0c\u800c\u662f\u6253\u5370\u4e86\u4e24\u4e2a2\u3002\u6211\u4eec\u521b\u5efa otherCount \u65f6\u62f7\u8d1d\u4e86 count \uff0c\u540c\u65f6\u4e5f\u62f7\u8d1d\u4e86 count \u5f53\u524d\u7684\u72b6\u6001\u3002 count \u7684 i \u662f 1\uff0c\u6240\u4ee5 otherCount \u7684 i \u4e5f\u662f 1\u3002\u56e0\u4e3a otherCount \u662f count \u7684\u62f7\u8d1d\uff0c\u6240\u4ee5\u5b83\u4eec\u6709\u5404\u81ea\u7684 i \u3002 \u518d\u770b\u4e00\u4e2a\u4e0d\u592a\u660e\u663e\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include void myInvoke ( const std :: function < void () >& fn ) { fn (); } int main () { int i { 0 }; // Increments and prints its local copy of @i. auto count { [ i ]() mutable { std :: cout << ++ i << '\\n' ; } }; myInvoke ( count ); myInvoke ( count ); myInvoke ( count ); return 0 ; } \u8f93\u51fa\uff1a 1 2 3 1 1 1 \u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u5b58\u5728\u76f8\u540c\u7684\u95ee\u9898\uff0c\u4f46\u662f\u66f4\u52a0\u9690\u853d\u3002\u5f53\u901a\u8fc7lambda\u521b\u5efa std::function \u65f6\uff0c std::function \u4f1a\u521b\u5efa lambda \u7684\u62f7\u8d1d\u3002\u56e0\u6b64\u5728\u8c03\u7528 fn() \u65f6\uff0c\u6267\u884c\u7684\u5176\u5b9e\u662flambda \u7684\u62f7\u8d1d\u800c\u4e0d\u662f\u5176\u672c\u8eab\u3002 \u5982\u679c\u6211\u4eec\u9700\u8981\u4f20\u9012\u4e00\u4e2a\u53ef\u53d8\u7684lambda\uff0c\u5e76\u4e14\u60f3\u8981\u907f\u514d\u65e0\u610f\u5730\u590d\u5236\uff0c\u6709\u4e24\u4e2a\u65b9\u6cd5\u3002\u4e00\u79cd\u662f\u4f7f\u7528\u975e\u6355\u83b7lambda\u6765\u4ee3\u66ff\u2014\u2014\u5728\u4e0a\u9762\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5220\u9664\u6355\u83b7\u5e76\u4f7f\u7528\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u6765\u8ddf\u8e2a\u72b6\u6001\u3002\u4f46\u662f\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u5f88\u96be\u8ddf\u8e2a\uff0c\u5e76\u4e14\u4f7f\u6211\u4eec\u7684\u4ee3\u7801\u53ef\u8bfb\u6027\u8f83\u5dee\u3002\u4e00\u4e2a\u66f4\u597d\u7684\u9009\u62e9\u662f\u4ece\u4e00\u5f00\u59cb\u5c31\u963b\u6b62lambda\u7684\u590d\u5236\u3002\u4f46\u662f\u7531\u4e8e\u6211\u4eec\u4e0d\u80fd\u5f71\u54cd std::function (\u6216\u5176\u4ed6\u6807\u51c6\u5e93\u51fd\u6570\u6216\u5bf9\u8c61)\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u90a3\u8981\u5982\u4f55\u624d\u80fd\u505a\u5230\u8fd9\u4e00\u70b9\u5462? \u5e78\u8fd0\u7684\u662f\uff0cC++ \u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u52a0\u65b9\u4fbf\u7684\u7c7b\u578b(\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d) \u2014\u2014\u79f0\u4e3a std::reference_wrapper \u3002\u8be5\u7c7b\u578b\u4f7f\u6211\u4eec\u53ef\u4ee5\u4f20\u5165\u4e00\u4e2a\u666e\u901a\u7c7b\u578b\uff0c\u4f46\u5f53\u505a\u5f15\u7528\u4f7f\u7528\u3002\u66f4\u597d\u5730\u662f\uff0c std::reference_wrapper \u53ef\u4ee5\u901a\u8fc7 std::ref() \u51fd\u6570\u521b\u5efa\u3002\u5982\u679c\u6211\u4eec\u5c06 lambda \u5305\u88c5\u5230 std::reference_wrapper \u4e2d\uff0c\u5219\u6bcf\u5f53\u6709\u4eba\u60f3\u62f7\u8d1d lambda \u65f6\uff0c\u5b83\u4eec\u4f1a\u62f7\u8d1d\u4e00\u4e2a\u5f15\u7528\u800c\u4e0d\u662f\u5bf9\u8c61\u672c\u8eab\u3002 \u4f7f\u7528 std::ref \u66f4\u65b0\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include void myInvoke ( const std :: function < void () >& fn ) { fn (); } int main () { int i { 0 }; // Increments and prints its local copy of @i. auto count { [ i ]() mutable { std :: cout << ++ i << '\\n' ; } }; // std::ref(count) \u4f7f\u5f97 count \u88ab\u5f53\u505a\u5f15\u7528\u5bf9\u5f85 // \u56e0\u6b64, \u4efb\u4f55\u5bf9\u5176\u7684\u62f7\u8d1d\u90fd\u5b9e\u9645\u62f7\u8d1d\u7684\u662f\u5f15\u7528\uff0c\u786e\u4fdd\u59cb\u7ec8\u53ea\u5b58\u5728\u4e00\u4e2a count myInvoke ( std :: ref ( count )); myInvoke ( std :: ref ( count )); myInvoke ( std :: ref ( count )); return 0 ; } \u8f93\u51fa\u7b26\u5408\u9884\u671f\uff1a 1 2 3 1 2 3 \u6ce8\u610f\uff0c invoke \u6309\u503c\u83b7\u53d6 fn \u7ed3\u679c\u4e5f\u662f\u4e00\u6837\u7684\u3002\u5982\u679c\u6211\u4eec\u901a\u8fc7 std::ref \u521b\u5efa\u5b83\uff0c\u5219 std::function \u4e0d\u4f1a\u521b\u5efa lambda \u7684\u526f\u672c\u3002 \u6cd5\u5219 \u6807\u51c6\u5e93\u51fd\u6570\u53ef\u80fd\u4f1a\u590d\u5236\u51fd\u6570\u5bf9\u8c61(\u63d0\u9192:lambda\u662f\u51fd\u6570\u5bf9\u8c61)\u3002\u5982\u679c\u4f60\u60f3\u63d0\u4f9b\u5e26\u6709\u53ef\u53d8\u6355\u83b7\u53d8\u91cf\u7684lambda\uff0c\u53ef\u4ee5\u4f7f\u7528 std::ref \u901a\u8fc7\u5f15\u7528\u4f20\u9012\u5b83\u4eec\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c3d\u91cf\u907f\u514d\u53ef\u53d8\u7684lambda\u3002\u4e0d\u53ef\u53d8lambda\u66f4\u5bb9\u6613\u7406\u89e3\uff0c\u4e0d\u4f1a\u51fa\u73b0\u4e0a\u8ff0\u95ee\u9898\uff0c\u4e5f\u4e0d\u4f1a\u5728\u6dfb\u52a0\u5e76\u884c\u6267\u884c\u65f6\u51fa\u73b0\u66f4\u5371\u9669\u7684\u95ee\u9898\u3002","title":"\u65e0\u610f\u95f4\u62f7\u8d1d\u53ef\u53d8\u7684 lambda"},{"location":"12-x-Chapter-12-comprehensive-quiz/","tags":["summary"],"text":"\u51fd\u6570\u7684 \u5b9e\u53c2 \u53ef\u4ee5\u901a\u8fc7\u4f20\u503c\u3001\u4f20\u5f15\u7528\u6216\u8005\u4f20\u5730\u5740\u7684\u65b9\u5f0f\u6765\u4f20\u9012\u3002 - \u5728\u4f20\u9012\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u679a\u4e3e\u91cf\u65f6\u4f7f\u7528 \u6309\u503c\u4f20\u9012 \uff1b - \u5728\u4f20\u9012\u7ed3\u6784\u4f53\u3001\u7c7b\u6216\u8005\u9700\u8981\u51fd\u6570\u8fdb\u884c\u4fee\u6539\u7684\u5b9e\u53c2\u65f6\uff0c\u4f7f\u7528 \u6309\u5f15\u7528\u4f20\u9012 \uff1b - \u5728\u4f20\u9012\u6307\u9488\u6216\u5185\u7f6e\u6570\u7ec4\u65f6\u4f7f\u7528 \u6309\u5730\u5740\u4f20\u9012 \u3002 \u5982\u679c\u53ef\u4ee5\uff0c\u5c3d\u91cf\u5728\u4f20\u9012\u5f15\u7528\u548c\u5730\u5740\u65f6\u4f7f\u7528 const \u3002 \u503c\u53ef\u4ee5\u901a\u8fc7\u6309\u503c\u8fd4\u56de\u3001\u6309\u5f15\u7528\u8fd4\u56de\u6216\u6309\u5730\u5740\u8fd4\u56de\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6309\u503c\u8fd4\u56de\u662f\u53ef\u4ee5\u7684\uff0c\u4e0d\u8fc7\u6709\u65f6\u5019 \u6309\u5f15\u7528\u8fd4\u56de \u548c \u6309\u5730\u5740\u8fd4\u56de \u4e5f\u5f88\u6709\u7528\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u52a8\u6001\u5206\u914d\u7684\u6570\u636e\u3001\u7ed3\u6784\u4f53\u6216\u7c7b\u65f6\u3002\u5728\u4f7f\u7528\u6309\u5f15\u7528\u8fd4\u56de\u6216\u6309\u5730\u5740\u8fd4\u56de\u65f6\uff0c\u8bb0\u5f97\u8981\u786e\u4fdd\u8fd4\u56de\u7684\u5185\u5bb9\u4e0d\u4f1a \u79bb\u5f00\u4f5c\u7528\u57df \u3002 12.1 - \u51fd\u6570\u6307\u9488 \u51fd\u6570\u6307\u9488\u5141\u8bb8\u6211\u4eec\u5c06\u4e00\u4e2a\u51fd\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u3002\u8fd9\u6709\u52a9\u4e8e\u8c03\u7528\u8005\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u884c\u4e3a\uff0c\u4f8b\u5982\u5217\u8868\u7684\u6392\u5e8f\u7b97\u6cd5\u3002 12.2 - \u6808\u548c\u5806 \u52a8\u6001\u5185\u5b58\u5c06\u5728\u5806\u4e0a\u8fdb\u884c\u5206\u914d\u3002 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a \u51fd\u6570\u8c03\u7528\u65f6\u4f1a\u5728\u8c03\u7528\u6808\u4e2d\u5806\u6808\u8ddf\u8e2a\u4ece\u7a0b\u5e8f\u5f00\u59cb\u5230\u5f53\u524d\u6267\u884c\u70b9\u7684\u6240\u6709\u6d3b\u52a8\u51fd\u6570(\u90a3\u4e9b\u5df2\u88ab\u8c03\u7528\u4f46\u5c1a\u672a\u7ec8\u6b62\u7684\u51fd\u6570)\u3002\u5c40\u90e8\u53d8\u91cf\u4f1a\u5728\u6808\u4e0a\u5206\u914d\uff0c\u4f46\u662f\u6808\u7684\u7a7a\u95f4\u662f\u6709\u9650\u7684\u3002 std::vector \u53ef\u4ee5\u7528\u6765\u6a21\u62df\u6808\u7684\u884c\u4e3a\u3002 12.4 - \u9012\u5f52 \u81ea\u5df1\u8c03\u7528\u81ea\u5df1\u7684\u51fd\u6570\uff0c\u79f0\u4e3a\u9012\u5f52\u51fd\u6570\u3002\u6240\u6709\u7684\u9012\u5f52\u51fd\u6570\u90fd\u5fc5\u987b\u6709\u7ec8\u6b62\u6761\u4ef6\u3002 12.5 - \u547d\u4ee4\u884c\u53c2\u6570 \u547d\u4ee4\u884c\u53c2\u6570\u5141\u8bb8\u7528\u6237\u6216\u5176\u4ed6\u7a0b\u5e8f\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u4e3a\u5176\u4f20\u5165\u6570\u636e\u3002\u547d\u4ee4\u884c\u53c2\u6570\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u90fd\u662fC\u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u671f\u671b\u4f20\u9012\u7684\u662f\u6570\u503c\uff0c\u5219\u8981\u81ea\u884c\u8f6c\u6362\u3002 12.6 - \u7701\u7565\u53f7\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 \u7701\u7565\u53f7\u7528\u4e8e\u4f20\u9012\u4e0d\u5b9a\u957f\u53c2\u6570\u3002\u4f46\u662f\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4e0d\u4f1a\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\uff0c\u4e5f\u4e0d\u77e5\u9053\u4f20\u9012\u4e86\u591a\u5c11\u53c2\u6570\u3002\u7a0b\u5e8f\u5fc5\u987b\u81ea\u884c\u8ffd\u8e2a\u76f8\u5173\u7ec6\u8282\u3002 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb Lambda \u51fd\u6570\u662f\u90a3\u4e9b\u53ef\u4ee5\u5d4c\u5165\u5728\u5176\u4ed6\u51fd\u6570\u4e2d\u7684\u51fd\u6570\u3002\u5b83\u4eec\u4e0d\u9700\u8981\u51fd\u6570\u540d\uff0c\u800c\u4e14\u53ef\u4ee5\u5728\u548c algorithms \u5e93\u7ed3\u5408\u4f7f\u7528\u65f6\u975e\u5e38\u6709\u7528\u3002","title":"12.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570"},{"location":"13-1-welcome-to-object-oriented-programming/","tags":["object"],"text":"Key Takeaway \u5728 1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf \u4e2d\uff0c\u6211\u4eec\u5c06C++\u4e2d\u7684\u5bf9\u8c61\u5b9a\u4e49\u4e3a\uff1a\u201c\u4e00\u6bb5\u53ef\u4ee5\u88ab\u7528\u6765\u5b58\u50a8\u503c\u7684\u5185\u5b58\u201d\u3002\u4e00\u4e2a\u5177\u6709\u540d\u5b57\u7684\u5bf9\u8c61\u79f0\u4e3a\u53d8\u91cf\u3002 \u5728\u4f20\u7edf\u7f16\u7a0b\u9886\u57df\uff08\u672c\u8282\u8bfe\u4e4b\u524d\u4ecb\u7ecd\u7684\u5185\u5bb9\u90fd\u5c5e\u4e8e\u4f20\u7edf\u7f16\u7a0b\u9886\u57df\uff09\uff0c\u7a0b\u5e8f\u57fa\u672c\u4e0a\u5c31\u662f\u4e00\u7cfb\u5217\u8ba1\u7b97\u673a\u6307\u4ee4\uff0c\u5b83\u4eec\u5b9a\u4e49\u4e86\u6570\u636e\uff08\u901a\u8fc7\u5bf9\u8c61\uff09\uff0c\u7136\u540e\u64cd\u4f5c\u8fd9\u4e9b\u6570\u636e\uff08\u901a\u8fc7\u8bed\u53e5\u548c\u51fd\u6570\uff09\u3002\u6570\u636e\u548c\u51fd\u6570\u5171\u540c\u4f5c\u7528\u540c\u4e8e\u8ba1\u7b97\u6240\u9700\u7684\u7ed3\u679c\uff0c\u4f46\u5b83\u4eec\u5404\u81ea\u662f\u72ec\u7acb\u7684\u4e2a\u4f53\u3002\u56e0\u4e3a\u8fd9\u79cd\u72ec\u7acb\u6027\uff0c\u4f20\u7edf\u7684\u7f16\u7a0b\u6280\u672f\u901a\u5e38\u65e0\u6cd5\u4e3a\u73b0\u5b9e\u4e16\u754c\u4e2d\u7684\u4e8b\u52a1\u63d0\u4f9b\u76f4\u89c2\u7684\u8868\u793a\u6cd5\u3002\u7a0b\u5e8f\u5458\u9700\u8981\u5c06\u5c5e\u6027\uff08\u53d8\u91cf\uff09\u548c\u884c\u4e3a\uff08\u51fd\u6570\uff09\u6070\u5f53\u7684\u8054\u7cfb\u8d77\u6765\uff0c\u624d\u80fd\u786e\u4fdd\u7a0b\u5e8f\u6b63\u786e\u5de5\u4f5c\u3002\u5176\u4ee3\u7801\u770b\u4e0a\u53bb\u591a\u662f\u4e0b\u9762\u8fd9\u6837\uff1a 1 driveTo ( you , work ); \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u53c8\u662f\u4ec0\u4e48\u5462\uff1f\u5c31\u50cf\u8bb8\u591a\u4e8b\u60c5\u4e00\u6837\uff0c\u4e5f\u8bb8\u901a\u8fc7\u7c7b\u6bd4\u6700\u5bb9\u6613\u7406\u89e3\u3002\u770b\u770b\u4f60\u5468\u56f4\u7684\u4e8b\u7269\u2014\u2014\u4f60\u770b\u5230\u7684\u4efb\u4f55\u4e1c\u897f\u53ef\u4ee5\u88ab\u79f0\u4e4b\u4e3a\u5bf9\u8c61\uff1a\u4e66\u7c4d\u3001\u5efa\u7b51\u3001\u98df\u7269\u751a\u81f3\u662f\u4f60\u81ea\u5df1\u3002\u5bf9\u8c61\u901a\u5e38\u5305\u542b\u4e24\u4e2a\u4e3b\u8981\u7684\u7ec4\u6210\u90e8\u5206\uff1a1. \u4e00\u7ec4\u76f8\u5173\u7684\u5c5e\u6027\uff08\u4f8b\u5982\uff1a\u91cd\u91cf\u3001\u989c\u8272\u3001\u5c3a\u5bf8\u3001\u4f53\u79ef\u3001\u5f62\u72b6\u7b49\u7b49\uff09\u30022. \u5b83\u53ef\u4ee5\u8868\u73b0\u51fa\u7684\u4e00\u4e9b\u884c\u4e3a\uff08\u4f8b\u5982\uff1a\u88ab\u6253\u5f00\u3001\u52a0\u70ed\u4e1c\u897f\u7b49\u7b49\uff09\u3002\u8fd9\u4e9b\u5c5e\u6027\u548c\u884c\u4e3a\u662f\u5bc6\u4e0d\u53ef\u5206\u7684\u3002 \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\u4e3a\u7a0b\u5e8f\u5458\u63d0\u4f9b\u4e86\u4e00\u79cd\u5c06\u5c5e\u6027\u548c\u884c\u4e3a\u6253\u5305\u6210\u4e00\u4e2a\u81ea\u5305\u542b\u3001\u53ef\u91cd\u7528\u201c\u7ec4\u4ef6\u201d\u7684\u80fd\u529b\u3002\u5176\u4ee3\u7801\u98ce\u683c\u770b\u4e0a\u53bb\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 you . driveTo ( work ); \u4e0a\u9762\u7684\u4ee3\u7801\u4e0d\u4ec5\u8bfb\u8d77\u6765\u66f4\u6e05\u695a\uff0c\u8fd8\u66f4\u6e05\u695a\u76ee\u6807\u662f\u4ec0\u4e48( you )\uff0c\u8c03\u7528\u4e86\u4ec0\u4e48\u884c\u4e3a(\u5f00\u8f66\u5230\u67d0\u4e2a\u5730\u65b9)\u3002\u6211\u4eec\u4e0d\u518d\u662f\u4e13\u6ce8\u4e8e\u7f16\u5199\u51fd\u6570\uff0c\u800c\u662f\u4e13\u6ce8\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u4e86\u4e00\u7cfb\u5217\u88ab\u59a5\u5584\u5b9a\u4e49\u7684\u884c\u4e3a\u7684\u5bf9\u8c61\u3002\u8fd9\u5c31\u662f\u8fd9\u79cd\u8303\u5f0f\u88ab\u79f0\u4e3a\u201c\u9762\u5411\u5bf9\u8c61\u201d\u7684\u539f\u56e0\u3002 \u8fd9\u4e48\u505a\u662f\u7a0b\u5e8f\u5458\u53ef\u4ee5\u4ee5\u66f4\u6a21\u5757\u5316\u7684\u65b9\u5f0f\u7f16\u5199\u7a0b\u5e8f\uff0c\u7a0b\u5e8f\u4e5f\u66f4\u5bb9\u6613\u7f16\u5199\u548c\u7406\u89e3\uff0c\u8fd8\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7a0b\u5ea6\u7684\u4ee3\u7801\u91cd\u7528\u6027\u3002\u901a\u8fc7\u5b9a\u4e49\u5982\u4f55\u4e0e\u5bf9\u8c61\u4ea4\u4e92\uff0c\u4ee5\u53ca\u5bf9\u8c61\u5982\u4f55\u4e0e\u5176\u4ed6\u5bf9\u8c61\u4ea4\u4e92\uff0c\u8fd9\u4e9b\u5bf9\u8c61\u8fd8\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u76f4\u89c2\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6570\u636e\u3002 \u6ce8\u610f\uff0c\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u5e76\u4e0d\u662f\u8981\u53d6\u4ee3\u4f20\u7edf\u7684\u7f16\u7a0b\u65b9\u5f0f\u3002\u5b83\u53ea\u662f\u6211\u4eec\u5904\u7406\u590d\u6742\u95ee\u9898\u65f6\u7684\u53c8\u4e00\u79cd\u5de5\u5177\u7f62\u4e86\u3002 \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u8fd8\u5e26\u6765\u4e86\u5176\u4ed6\u4e00\u4e9b\u6709\u7528\u7684\u6982\u5ff5\uff1a \u7ee7\u627f \u3001 \u5c01\u88c5 \u3001 \u62bd\u8c61 \u548c \u591a\u6001\u6027 \u3002\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u51e0\u7ae0\u6559\u7a0b\u4e2d\u6db5\u76d6\u6240\u6709\u8fd9\u4e9b\u6982\u5ff5\u3002\u9762\u5411\u5bf9\u8c61\u5305\u542b\u4e86\u5f88\u591a\u65b0\u7684\u5185\u5bb9\uff0c\u4f46\u662f\u4e00\u65e6\u4f60\u5bf9\u5b83\u719f\u6089\u8d77\u6765\u5e76\u5bf9\u5b83\u4ea7\u751f\u5174\u8da3\uff0c\u4f60\u53ef\u80fd\u518d\u4e5f\u4e0d\u60f3\u56de\u5230\u7eaf\u7cb9\u7684\u4f20\u7edf\u7f16\u7a0b\u4e2d\u53bb\u4e86\u3002 \u6ce8\u610f\uff0c\u672f\u8bed\"\u5bf9\u8c61\"\u4e00\u8bcd\u4f3c\u4e4e\u5df2\u6709\u4e9b\u4e0d\u582a\u8d1f\u91cd\uff0c\u540c\u65f6\u4e5f\u5e26\u6765\u4e86\u4e0d\u5c0f\u7684\u6df7\u4e71\u3002\u5728\u4f20\u7edf\u7f16\u7a0b\u4e2d\uff0c\u5bf9\u8c61\u662f\u5b58\u50a8\u503c\u7684\u4e00\u5757\u5185\u5b58\u3002\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c\u201c\u5bf9\u8c61\u201d\u610f\u5473\u7740\u5b83\u65e2\u662f\u4f20\u7edf\u7f16\u7a0b\u610f\u4e49\u4e0a\u7684\u5bf9\u8c61\uff0c\u4e5f\u662f\u5c5e\u6027\u548c\u884c\u4e3a\u7684\u7ec4\u5408\u3002\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u5f53\u6211\u4eec\u4f7f\u7528\u672f\u8bed\u201c\u5bf9\u8c61\u201d\u65f6\uff0c\u6211\u4eec\u5c06\u6307\u9762\u5411\u5bf9\u8c61\u610f\u4e49\u4e0a\u7684\u201c\u5bf9\u8c61\u201d\u3002","title":"13.1 - \u6b22\u8fce\u6765\u5230\u9762\u5411\u5bf9\u8c61\u7684\u4e16\u754c"},{"location":"13-10-the-hidden-this-pointer/","tags":["this"],"text":"\u65b0\u624b\u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u5458\u7ecf\u5e38\u95ee\u7684\u4e00\u4e2a\u5173\u4e8e\u7c7b\u7684\u95ee\u9898\u662f\uff0c\u201c\u5f53\u8c03\u7528\u6210\u5458\u51fd\u6570\u65f6\uff0cC++\u5982\u4f55\u786e\u5b9a\u8c03\u7528\u5b83\u7684\u5bf9\u8c61\uff1f\u201c \u5b9e\u9645\u4e0a\uff0cC++ \u4f7f\u7528\u4e00\u4e2a\u540d\u4e3a \u201cthis\u201d\u7684\u9690\u85cf\u6307\u9488\u6765\u8ddf\u8e2a\u8c03\u7528\u6210\u5458\u7684\u5bf9\u8c61\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4fdd\u5b58\u6574\u6570\u5e76\u63d0\u4f9b\u6784\u9020\u51fd\u6570\u548c\u8bbf\u95ee\u51fd\u6570\u7684\u7b80\u5355\u7c7b\u3002\u6ce8\u610f\uff0c\u8fd9\u4e2a\u7c7b\u4e0d\u9700\u8981\u6790\u6784\u51fd\u6570\uff0c\u56e0\u4e3aC++\u81ea\u52a8\u6e05\u7406\u6574\u6570\u6210\u5458\u53d8\u91cf\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Simple { private : int m_id ; public : Simple ( int id ) : m_id { id } { } void setID ( int id ) { m_id = id ; } int getID () { return m_id ; } }; \u4e0b\u9762\u4f8b\u5b50\u663e\u793a\u4e86Simple\u7684\u4f7f\u7528\u65b9\u6cd5\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { Simple simple { 1 }; simple . setID ( 2 ); std :: cout << simple . getID () << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 \u5f53\u6211\u4eec\u8c03\u7528 simple.setID(2); \u65f6\uff0cC++ \u5c31\u80fd\u591f\u77e5\u9053\u8c03\u7528\u7684\u662f simple \u7684 setID() \uff0c\u800c m_id \u5219\u6307\u7684\u662f simple.m_id \u3002\u63a5\u4e0b\u6765\u8ba9\u6211\u4eec\u770b\u770b\u8fd9\u80cc\u540e\u7684\u5de5\u4f5c\u539f\u7406\u3002 \u9690\u85cf\u7684 *this \u6307\u9488 \u89c2\u5bdf\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u8fd9\u4e00\u884c\u4ee3\u7801\uff1a 1 simple . setID ( 2 ); \u5c3d\u7ba1\u51fd\u6570 setID() \u51fd\u6570\u770b\u4e0a\u53bb\u53ea\u63a5\u53d7\u4e86\u4e00\u4e2a\u53c2\u6570\uff0c\u800c\u5b9e\u9645\u4e0a\u5b83\u63a5\u53d7\u4e86\u4e24\u4e2a\u53c2\u6570\uff01\u5f53\u7a0b\u5e8f\u7f16\u8bd1\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c06\u4e0a\u8ff0\u4ee3\u7801\u8f6c\u53d8\u6210\u4e0b\u9762\u7684\u5f62\u5f0f \uff1a 1 setID ( & simple , 2 ); // \u6ce8\u610f\uff0c\u4f5c\u4e3a\u5bf9\u8c61\u7684\u524d\u7f00\u73b0\u5728\u53d8\u6210\u4e86\u51fd\u6570\u7684\u5b9e\u53c2\uff01 \u6ce8\u610f\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u5df2\u7ecf\u53d8\u6210\u4e86\u6807\u51c6\u7684\u51fd\u6570\u8c03\u7528\uff0csimple \u5bf9\u8c61(\u4ee5\u524d\u662f\u4e00\u4e2a\u5bf9\u8c61\u524d\u7f00)\u5219\u901a\u8fc7 \u6309\u5730\u5740\u4f20\u9012 \u7684\u65b9\u5f0f\u4f20\u5165\u4e86\u51fd\u6570\u3002 \u4f46\u8fd9\u53ea\u662f\u7b54\u6848\u7684\u4e00\u534a\uff0c\u7531\u4e8e\u51fd\u6570\u8c03\u7528\u73b0\u5728\u589e\u52a0\u4e86\u4e00\u4e2a\u5b9e\u53c2\uff0c\u56e0\u6b64\u9700\u8981\u4fee\u6539\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u624d\u80fd\u63a5\u53d7(\u5e76\u4f7f\u7528)\u8fd9\u4e2a\u5b9e\u53c2\u4f5c\u4e3a\u5f62\u53c2\u3002\u56e0\u6b64\uff0c\u4e0b\u9762\u7684\u6210\u5458\u51fd\u6570: 1 void setID ( int id ) { m_id = id ; } \u4f1a\u88ab\u7f16\u8bd1\u5668\u8f6c\u6362\u4e3a\u4e0b\u9762\u7684\u5f62\u5f0f\uff1a 1 void setID ( Simple * const this , int id ) { this -> m_id = id ; } \u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u666e\u901a\u7684\u6210\u5458\u51fd\u6570\u65f6\uff0c\u4f1a\u9690\u5f0f\u5730\u4e3a\u5176\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u53c2\u6570 this \u3002 this \u6307\u9488\u662f\u4e00\u4e2a\u9690\u85cf\u7684\u5e38\u91cf\u6307\u9488\uff0c\u5b83\u4fdd\u5b58\u7740\u8be5\u51fd\u6570\u5bf9\u5e94\u5bf9\u8c61\u7684\u5730\u5740\u3002 \u6b64\u5916\uff0c\u8fd8\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u3002\u5728\u6210\u5458\u51fd\u6570\u4e2d\uff0c\u5728\u6210\u5458\u51fd\u6570\u4e2d\uff0c\u4efb\u4f55\u7c7b\u6210\u5458\uff08\u5305\u62ec\u51fd\u6570\u548c\u53d8\u91cf\uff09\u4e5f\u9700\u8981\u88ab\u66f4\u65b0\u4e3a\u5f15\u7528\u5bf9\u8c61\u7684\u5f62\u5f0f\u3002\u8981\u5b8c\u6210\u8fd9\u4e00\u70b9\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4e3a\u5b83\u4eec\u6dfb\u52a0 this-> \u524d\u7f00\u5373\u53ef\u3002\u56e0\u6b64\u5728\u51fd\u6570\u4f53\u4e2d\uff0c m_id (\u6210\u5458\u53d8\u91cf)\u88ab\u8f6c\u53d8\u6210 this->m_id \u3002\u7531\u4e8e this \u6307\u9488\u6307\u5411\u7684\u662f simple \u5bf9\u8c61\u7684\u5730\u5740\uff0c\u56e0\u6b64 this->m_id \u4f1a\u88ab\u89e3\u6790\u4e3a simple.m_id \u3002 \u603b\u7ed3\u4e00\u4e0b\uff1a \u5f53\u8c03\u7528 simple.setID(2) \u65f6\uff0c\u7f16\u8bd1\u5668\u5b9e\u9645\u8c03\u7528\u7684\u662f setID(&simple, 2) \uff1b \u5728 setID() \u4e2d\uff0cthis \u6307\u9488\u4fdd\u5b58\u7740 simple \u5bf9\u8c61\u7684\u5730\u5740\uff1b setID() \u51fd\u6570\u4e2d\u7684\u4efb\u4f55\u6210\u5458\u90fd\u4f1a\u88ab\u6dfb\u52a0 \u201c this-> \u201d \u524d\u7f00\uff0c\u6240\u4ee5\uff0c\u5bf9\u4e8e m_id = id \u8fd9\u6761\u8bed\u53e5\uff0c\u7f16\u8bd1\u5668\u5b9e\u9645\u4e0a\u6267\u884c\u7684\u662f this->m_id = id \uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u5373\u5c06 simple.m_id \u7684\u503c\u66f4\u65b0\u4e3a id \u3002 \u597d\u6d88\u606f\u662f\uff0c\u6240\u6709\u8fd9\u4e9b\u90fd\u662f\u81ea\u52a8\u53d1\u751f\u7684\uff0c\u4f60\u662f\u5426\u8bb0\u5f97\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u5e76\u4e0d\u91cd\u8981\u3002\u4f60\u53ea\u9700\u8bb0\u4f4f\uff0c\u6240\u6709\u666e\u901a\u6210\u5458\u51fd\u6570\u90fd\u6709\u4e00\u4e2a\u201cthis\u201d\u6307\u9488\uff0c\u8be5\u6307\u9488\u6307\u5411\u8c03\u7528\u51fd\u6570\u7684\u5bf9\u8c61\u3002 \u201cthis\u201d \u6307\u9488\u603b\u662f\u6307\u5411\u64cd\u4f5c\u5bf9\u8c61\u81ea\u5df1 \u65b0\u7a0b\u5e8f\u5458\u6709\u65f6\u4f1a\u5bf9\u5b58\u5728\u591a\u5c11\u4e2a\u201cthis\u201d\u6307\u9488\u611f\u5230\u56f0\u60d1\u3002\u6bcf\u4e2a\u6210\u5458\u51fd\u6570\u90fd\u6709\u4e00\u4e2a\u201cthis\u201d\u6307\u9488\u5f62\u53c2\uff0c\u5b83\u88ab\u8bbe\u7f6e\u4e3a\u6b63\u5728\u64cd\u4f5c\u7684\u5bf9\u8c61\u7684\u5730\u5740\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 int main () { Simple A { 1 }; // this = &A inside the Simple constructor Simple B { 2 }; // this = &B inside the Simple constructor A . setID ( 3 ); // this = &A inside member function setID B . setID ( 4 ); // this = &B inside member function setID return 0 ; } \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0cthis\u6307\u9488\u4ea4\u66ff\u5730\u6307\u4ee3\u5bf9\u8c61A\u6216\u8005B\u7684\u5730\u5740\uff0c\u53d6\u51b3\u4e8e\u8c03\u7528\u6210\u5458\u51fd\u6570\u7684\u662fA\u8fd8\u662fB\u3002 \u56e0\u4e3a\u201cthis\u201d\u53ea\u662f\u4e00\u4e2a\u51fd\u6570\u5f62\u53c2\uff0c\u5b83\u4e0d\u4f1a\u7ed9\u7c7b\u589e\u52a0\u4efb\u4f55\u5185\u5b58\u4f7f\u7528\u91cf(\u53ea\u662f\u7ed9\u6210\u5458\u51fd\u6570\u8c03\u7528\u589e\u52a0\u5185\u5b58\u4f7f\u7528\u91cf\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u5f62\u53c2\u9700\u8981\u4f20\u9012\u7ed9\u51fd\u6570\u5e76\u5b58\u50a8\u5728\u5185\u5b58\u4e2d)\u3002 \u663e\u5f0f\u5730\u4f7f\u7528 this \u6307\u9488 \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f60\u4e0d\u9700\u8981\u663e\u5f0f\u5f15\u7528\u201cthis\u201d\u6307\u9488\u3002\u7136\u800c\uff0c\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\u76f4\u63a5\u4f7f\u7528 this \u6307\u9488\u4e5f\u662f\u6709\u7528\u7684\u3002 \u9996\u5148\uff0c\u5982\u679c\u6784\u9020\u51fd\u6570(\u6216\u6210\u5458\u51fd\u6570)\u7684\u5f62\u53c2\u4e0e\u6210\u5458\u53d8\u91cf\u540c\u540d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\" this \"\u6765\u6d88\u9664\u6b67\u4e49\uff1a 1 2 3 4 5 6 7 8 9 10 11 class Something { private : int data ; public : Something ( int data ) { this -> data = data ; // this->data is the member, data is the local parameter } }; \u4e0a\u9762\u4ee3\u7801\u4e2d\u7684\u6784\u9020\u51fd\u6570\uff0c\u5176 \u5f62\u53c2 \u548c\u7c7b\u6210\u5458\u53d8\u91cf\u540c\u540d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c data \u5c06\u8868\u793a\u5f62\u53c2\uff0c\u6240\u4ee5\u4f60\u5fc5\u987b\u4f7f\u7528 \u201cthis->data\u201d \u6765\u8868\u793a\u6210\u5458\u53d8\u91cf\u3002\u5c3d\u7ba1\u8fd9\u4e48\u505a\u4e5f\u662f\u53ef\u4ee5\u88ab\u63a5\u53d7\u7684\u7f16\u7a0b\u98ce\u683c\uff0c\u4f46\u662f\u4f7f\u7528 m_ \u4f5c\u4e3a\u6210\u5458\u53d8\u91cf\u7684\u524d\u7f00\u5c31\u53ef\u4ee5\u4ece\u6e90\u5934\u4e0a\u6d88\u9664\u8fd9\u79cd\u6b67\u4e49\u3002 \u94fe\u5f0f\u8c03\u7528\u6210\u5458\u51fd\u6570 \u53e6\u4e00\u65b9\u9762\uff0c\u6709\u65f6\u5019\u8ba9\u7c7b\u6210\u5458\u51fd\u6570\u8fd4\u56de\u5bf9\u8c61\u672c\u8eab\u4e5f\u662f\u5f88\u6709\u7528\u7684\u3002\u8fd9\u4e48\u505a\u7684\u4e00\u4e2a\u4e3b\u8981\u539f\u56e0\u5c31\u662f\u53ef\u4ee5\u5141\u8bb8\u51fd\u6570\u7684\u94fe\u5f0f\u8c03\u7528\uff0c\u4f7f\u5f97\u591a\u4e2a\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u4f5c\u7528\u4e8e\u4e00\u4e2a\u76f8\u540c\u7684\u5bf9\u8c61\uff01\u5176\u5b9e\u4f60\u5df2\u7ecf\u591a\u6b21\u4f7f\u7528\u8be5\u7279\u6027\u4e86\u3002\u5bf9\u4e8e\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u6765\u8bf4\uff0c std::cout \u8f93\u51fa\u4e86\u4e0d\u6b62\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002 1 std :: cout << \"Hello, \" << userName ; \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c std::cout \u5176\u5b9e\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u800c\u8fd0\u7b97\u7b26 << \u662f\u8be5\u5bf9\u8c61\u7684\u4e00\u4e2a\u6210\u5458\u51fd\u6570\u3002\u7f16\u8bd1\u5668\u4f1a\u6309\u7167\u4e0b\u9762\u7684\u65b9\u5f0f\u5bf9\u4e0a\u8ff0\u4ee3\u7801\u6c42\u503c\uff1a 1 ( std :: cout << \"Hello, \" ) << userName ; \u9996\u5148\uff0c << \u4f7f\u7528 std::cout \u548c\u5b57\u7b26\u4e32\u5b57\u9762\u91cf \u201cHello, \u201d \u5c06 \u201cHello, \u201d \u6253\u5370\u5230\u7ec8\u7aef\u3002\u4e0d\u8fc7\uff0c\u8fd9\u53ea\u662f\u4e0a\u8ff0\u8868\u8fbe\u5f0f\u7684\u4e00\u90e8\u5206\uff0c << \u8fd8\u9700\u8981\u8fd4\u56de\u4e00\u4e2a\u503c\uff08\u6216void\uff09\u3002\u5982\u679c\u5b83\u8fd4\u56de\u7684\u662f void\uff0c\u5219\u7ed3\u679c\u4f1a\u53d8\u4e3a\u4e0b\u9762\u7684\u5f62\u5f0f\uff1a 1 ( void ) << userName ; \u8fd9\u663e\u7136\u4e0d\u5408\u903b\u8f91\uff08\u7f16\u8bd1\u5668\u4f1a\u629b\u51fa\u9519\u8bef\uff09\u3002\u5b9e\u9645\u4e0a\uff0c << \u7684\u8fd4\u56de\u503c\u662f *this \uff0c\u5728\u4e0a\u9762\u7684\u8bed\u5883\u4e0b\uff0cthis \u5c31\u662f std::cout \u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u5f53\u7b2c\u4e00\u4e2a << \u6267\u884c\u5b8c\u6210\u540e\uff0c\u8bed\u53e5\u7b49\u4ef7\u4e8e\u5982\u4e0b\u5f62\u5f0f\uff1a 1 ( std :: cout ) << userName ; \u4e0a\u8ff0\u4ee3\u7801\u8fdb\u800c\u4f1a\u6253\u5370\u7528\u6237\u540d\u3002 \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ea\u9700\u8981\u6307\u5b9a\u5bf9\u8c61\u4e00\u6b21\uff08\u6b64\u4f8b\u4e2d\u4e3a std::cout )\uff0c\u7136\u540e\u6bcf\u6b21\u51fd\u6570\u8c03\u7528\u90fd\u53ef\u4ee5\u628a\u5b83\u4f20\u9012\u7ed9\u63a5\u4e0b\u6765\u7684\u51fd\u6570\uff0c\u662f\u6211\u4eec\u53ef\u4ee5\u94fe\u5f0f\u6267\u884c\u591a\u4e2a\u6307\u4ee4\u3002 \u5728\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u4e2d\u6211\u4eec\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u4e0a\u8ff0\u884c\u4e3a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 class Calc { private : int m_value { 0 }; public : void add ( int value ) { m_value += value ; } void sub ( int value ) { m_value -= value ; } void mult ( int value ) { m_value *= value ; } int getValue () { return m_value ; } }; \u5982\u679c\u4f60\u5e0c\u671b\u5148\u52a05\uff0c\u518d\u51cf3\uff0c\u7136\u540e\u518d\u4e58\u4ee54\uff0c\u90a3\u4e48\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { Calc calc {}; calc . add ( 5 ); // returns void calc . sub ( 3 ); // returns void calc . mult ( 4 ); // returns void std :: cout << calc . getValue () << '\\n' ; return 0 ; } \u4e0d\u8fc7\uff0c\u5982\u679c\u6bcf\u4e2a\u51fd\u6570\u90fd\u8fd4\u56de *this \u7684\u8bdd\uff0c\u5728\u53ef\u4ee5\u94fe\u5f0f\u5730\u8c03\u7528\u5b83\u4eec\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u53ef\u94fe\u5f0f\u8c03\u7528\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class Calc { private : int m_value {}; public : Calc & add ( int value ) { m_value += value ; return * this ; } Calc & sub ( int value ) { m_value -= value ; return * this ; } Calc & mult ( int value ) { m_value *= value ; return * this ; } int getValue () { return m_value ; } }; \u6ce8\u610f\uff0c\u51fd\u6570 add() \uff0c sub() \u548c mult() \u73b0\u5728\u90fd\u8fd4\u56de *this \u3002\u8fd9\u6837\u4e00\u6765\u5c31\u53ef\u4ee5\u5411\u4e0b\u9762\u8fd9\u6837\u8c03\u7528\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { Calc calc {}; calc . add ( 5 ). sub ( 3 ). mult ( 4 ); std :: cout << calc . getValue () << '\\n' ; return 0 ; } \u4e09\u884c\u4ee3\u7801\u88ab\u538b\u7f29\u6210\u4e86\u4e00\u884c\uff01\u6211\u4eec\u518d\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002 \u9996\u5148\uff0c\u8c03\u7528 calc.add(5) \u5c06 5 \u52a0\u5230 m_value \uff0c\u7136\u540e\u8fd4\u56de *this \uff0c\u5b83\u662f calc \u7684\u5f15\u7528\uff0c\u6240\u4ee5 calc \u4f1a\u88ab\u7528\u4e8e\u8c03\u7528\u63a5\u4e0b\u6765\u7684\u51fd\u6570\u3002\u518d\u63a5\u4e0b\u6765\uff0c\u5bf9 calc.sub(3) \u6c42\u503c\uff0c\u5b83\u5c06 3 \u4ece m_value \u51cf\u53bb\u540e\uff0c\u7ee7\u7eed\u8fd4\u56de calc \u3002\u6700\u540e\uff0c calc.mult(4) \u5c06 m_value \u4e58\u4ee5\u56db\u5e76\u8fd4\u56de calc \uff0c\u7531\u4e8e\u4e0d\u9700\u8981\u7ee7\u7eed\u8c03\u7528\u51fd\u6570\uff0c\u8fd9\u4e2a\u8fd4\u56de\u503c\u4e5f\u5c31\u88ab\u5ffd\u7565\u6389\u4e86\u3002 \u56e0\u4e3a\u6bcf\u4e2a\u51fd\u6570\u90fd\u4f1a\u4fee\u6539\u5bf9\u8c61 calc \uff0c\u6240\u4ee5 calc \u7684\u6210\u5458 m_value \u7684\u503c\u6b64\u65f6\u4e3a (((0 + 5) - 3) * 4) \uff0c\u53738\u3002 \u5c0f\u7ed3 \u201cthis\u201d \u6307\u9488\u662f\u4e00\u4e2a\u88ab\u7f16\u8bd1\u5668\u9690\u5f0f\u6dfb\u52a0\u5230\u975e\u9759\u6001\u6210\u5458\u51fd\u6570\u7684\u9690\u85cf\u53c2\u6570\u3002\u591a\u6570\u60c5\u51b5\u4e0b\u4f60\u5e76\u4e0d\u4f1a\u76f4\u63a5\u8bbf\u95ee\u5b83\uff0c\u4f46\u662f\u5f53\u6709\u9700\u8981\u65f6\u4f60\u4f1a\u7528\u7684\u7740\u7684\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cthis\u662f\u4e00\u4e2a\u5e38\u91cf\u6307\u9488\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5b83\u4fee\u6539\u5b83\u6240\u6307\u7684\u5bf9\u8c61\uff0c\u4f46\u662f\u4f60\u4e0d\u80fd\u8ba9\u5b83\u6307\u5411\u522b\u7684\u5bf9\u8c61\u3002 \u5982\u679c\u6211\u4eec\u8ba9\u539f\u672c\u8fd4\u56de void \u7684\u51fd\u6570\u8fd4\u56de *this \uff0c\u5219\u53ef\u4ee5\u4f7f\u8fd9\u4e9b\u51fd\u6570\u80fd\u591f\u88ab\u94fe\u5f0f\u8c03\u7528\u3002 \u901a\u5e38\u5728\u91cd\u8f7d\u7c7b\u7684\u8fd0\u7b97\u7b26\u65f6\u4f1a\u7528\u5230\u8fd9\u4e2a\u7279\u6027 \uff0814\u7ae0\u4e2d\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u8fd0\u7b97\u7b26\u91cd\u8f7d\uff09\u3002","title":"13.10 - \u9690\u85cf\u7684this\u6307\u9488"},{"location":"13-10-the-hidden-this-pointer/#this","text":"\u89c2\u5bdf\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u8fd9\u4e00\u884c\u4ee3\u7801\uff1a 1 simple . setID ( 2 ); \u5c3d\u7ba1\u51fd\u6570 setID() \u51fd\u6570\u770b\u4e0a\u53bb\u53ea\u63a5\u53d7\u4e86\u4e00\u4e2a\u53c2\u6570\uff0c\u800c\u5b9e\u9645\u4e0a\u5b83\u63a5\u53d7\u4e86\u4e24\u4e2a\u53c2\u6570\uff01\u5f53\u7a0b\u5e8f\u7f16\u8bd1\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c06\u4e0a\u8ff0\u4ee3\u7801\u8f6c\u53d8\u6210\u4e0b\u9762\u7684\u5f62\u5f0f \uff1a 1 setID ( & simple , 2 ); // \u6ce8\u610f\uff0c\u4f5c\u4e3a\u5bf9\u8c61\u7684\u524d\u7f00\u73b0\u5728\u53d8\u6210\u4e86\u51fd\u6570\u7684\u5b9e\u53c2\uff01 \u6ce8\u610f\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u5df2\u7ecf\u53d8\u6210\u4e86\u6807\u51c6\u7684\u51fd\u6570\u8c03\u7528\uff0csimple \u5bf9\u8c61(\u4ee5\u524d\u662f\u4e00\u4e2a\u5bf9\u8c61\u524d\u7f00)\u5219\u901a\u8fc7 \u6309\u5730\u5740\u4f20\u9012 \u7684\u65b9\u5f0f\u4f20\u5165\u4e86\u51fd\u6570\u3002 \u4f46\u8fd9\u53ea\u662f\u7b54\u6848\u7684\u4e00\u534a\uff0c\u7531\u4e8e\u51fd\u6570\u8c03\u7528\u73b0\u5728\u589e\u52a0\u4e86\u4e00\u4e2a\u5b9e\u53c2\uff0c\u56e0\u6b64\u9700\u8981\u4fee\u6539\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u624d\u80fd\u63a5\u53d7(\u5e76\u4f7f\u7528)\u8fd9\u4e2a\u5b9e\u53c2\u4f5c\u4e3a\u5f62\u53c2\u3002\u56e0\u6b64\uff0c\u4e0b\u9762\u7684\u6210\u5458\u51fd\u6570: 1 void setID ( int id ) { m_id = id ; } \u4f1a\u88ab\u7f16\u8bd1\u5668\u8f6c\u6362\u4e3a\u4e0b\u9762\u7684\u5f62\u5f0f\uff1a 1 void setID ( Simple * const this , int id ) { this -> m_id = id ; } \u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u666e\u901a\u7684\u6210\u5458\u51fd\u6570\u65f6\uff0c\u4f1a\u9690\u5f0f\u5730\u4e3a\u5176\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u53c2\u6570 this \u3002 this \u6307\u9488\u662f\u4e00\u4e2a\u9690\u85cf\u7684\u5e38\u91cf\u6307\u9488\uff0c\u5b83\u4fdd\u5b58\u7740\u8be5\u51fd\u6570\u5bf9\u5e94\u5bf9\u8c61\u7684\u5730\u5740\u3002 \u6b64\u5916\uff0c\u8fd8\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u3002\u5728\u6210\u5458\u51fd\u6570\u4e2d\uff0c\u5728\u6210\u5458\u51fd\u6570\u4e2d\uff0c\u4efb\u4f55\u7c7b\u6210\u5458\uff08\u5305\u62ec\u51fd\u6570\u548c\u53d8\u91cf\uff09\u4e5f\u9700\u8981\u88ab\u66f4\u65b0\u4e3a\u5f15\u7528\u5bf9\u8c61\u7684\u5f62\u5f0f\u3002\u8981\u5b8c\u6210\u8fd9\u4e00\u70b9\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4e3a\u5b83\u4eec\u6dfb\u52a0 this-> \u524d\u7f00\u5373\u53ef\u3002\u56e0\u6b64\u5728\u51fd\u6570\u4f53\u4e2d\uff0c m_id (\u6210\u5458\u53d8\u91cf)\u88ab\u8f6c\u53d8\u6210 this->m_id \u3002\u7531\u4e8e this \u6307\u9488\u6307\u5411\u7684\u662f simple \u5bf9\u8c61\u7684\u5730\u5740\uff0c\u56e0\u6b64 this->m_id \u4f1a\u88ab\u89e3\u6790\u4e3a simple.m_id \u3002 \u603b\u7ed3\u4e00\u4e0b\uff1a \u5f53\u8c03\u7528 simple.setID(2) \u65f6\uff0c\u7f16\u8bd1\u5668\u5b9e\u9645\u8c03\u7528\u7684\u662f setID(&simple, 2) \uff1b \u5728 setID() \u4e2d\uff0cthis \u6307\u9488\u4fdd\u5b58\u7740 simple \u5bf9\u8c61\u7684\u5730\u5740\uff1b setID() \u51fd\u6570\u4e2d\u7684\u4efb\u4f55\u6210\u5458\u90fd\u4f1a\u88ab\u6dfb\u52a0 \u201c this-> \u201d \u524d\u7f00\uff0c\u6240\u4ee5\uff0c\u5bf9\u4e8e m_id = id \u8fd9\u6761\u8bed\u53e5\uff0c\u7f16\u8bd1\u5668\u5b9e\u9645\u4e0a\u6267\u884c\u7684\u662f this->m_id = id \uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u5373\u5c06 simple.m_id \u7684\u503c\u66f4\u65b0\u4e3a id \u3002 \u597d\u6d88\u606f\u662f\uff0c\u6240\u6709\u8fd9\u4e9b\u90fd\u662f\u81ea\u52a8\u53d1\u751f\u7684\uff0c\u4f60\u662f\u5426\u8bb0\u5f97\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u5e76\u4e0d\u91cd\u8981\u3002\u4f60\u53ea\u9700\u8bb0\u4f4f\uff0c\u6240\u6709\u666e\u901a\u6210\u5458\u51fd\u6570\u90fd\u6709\u4e00\u4e2a\u201cthis\u201d\u6307\u9488\uff0c\u8be5\u6307\u9488\u6307\u5411\u8c03\u7528\u51fd\u6570\u7684\u5bf9\u8c61\u3002","title":"\u9690\u85cf\u7684 *this \u6307\u9488"},{"location":"13-10-the-hidden-this-pointer/#this_1","text":"\u65b0\u7a0b\u5e8f\u5458\u6709\u65f6\u4f1a\u5bf9\u5b58\u5728\u591a\u5c11\u4e2a\u201cthis\u201d\u6307\u9488\u611f\u5230\u56f0\u60d1\u3002\u6bcf\u4e2a\u6210\u5458\u51fd\u6570\u90fd\u6709\u4e00\u4e2a\u201cthis\u201d\u6307\u9488\u5f62\u53c2\uff0c\u5b83\u88ab\u8bbe\u7f6e\u4e3a\u6b63\u5728\u64cd\u4f5c\u7684\u5bf9\u8c61\u7684\u5730\u5740\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 int main () { Simple A { 1 }; // this = &A inside the Simple constructor Simple B { 2 }; // this = &B inside the Simple constructor A . setID ( 3 ); // this = &A inside member function setID B . setID ( 4 ); // this = &B inside member function setID return 0 ; } \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0cthis\u6307\u9488\u4ea4\u66ff\u5730\u6307\u4ee3\u5bf9\u8c61A\u6216\u8005B\u7684\u5730\u5740\uff0c\u53d6\u51b3\u4e8e\u8c03\u7528\u6210\u5458\u51fd\u6570\u7684\u662fA\u8fd8\u662fB\u3002 \u56e0\u4e3a\u201cthis\u201d\u53ea\u662f\u4e00\u4e2a\u51fd\u6570\u5f62\u53c2\uff0c\u5b83\u4e0d\u4f1a\u7ed9\u7c7b\u589e\u52a0\u4efb\u4f55\u5185\u5b58\u4f7f\u7528\u91cf(\u53ea\u662f\u7ed9\u6210\u5458\u51fd\u6570\u8c03\u7528\u589e\u52a0\u5185\u5b58\u4f7f\u7528\u91cf\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u5f62\u53c2\u9700\u8981\u4f20\u9012\u7ed9\u51fd\u6570\u5e76\u5b58\u50a8\u5728\u5185\u5b58\u4e2d)\u3002","title":"\u201cthis\u201d \u6307\u9488\u603b\u662f\u6307\u5411\u64cd\u4f5c\u5bf9\u8c61\u81ea\u5df1"},{"location":"13-10-the-hidden-this-pointer/#this_2","text":"\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f60\u4e0d\u9700\u8981\u663e\u5f0f\u5f15\u7528\u201cthis\u201d\u6307\u9488\u3002\u7136\u800c\uff0c\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\u76f4\u63a5\u4f7f\u7528 this \u6307\u9488\u4e5f\u662f\u6709\u7528\u7684\u3002 \u9996\u5148\uff0c\u5982\u679c\u6784\u9020\u51fd\u6570(\u6216\u6210\u5458\u51fd\u6570)\u7684\u5f62\u53c2\u4e0e\u6210\u5458\u53d8\u91cf\u540c\u540d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\" this \"\u6765\u6d88\u9664\u6b67\u4e49\uff1a 1 2 3 4 5 6 7 8 9 10 11 class Something { private : int data ; public : Something ( int data ) { this -> data = data ; // this->data is the member, data is the local parameter } }; \u4e0a\u9762\u4ee3\u7801\u4e2d\u7684\u6784\u9020\u51fd\u6570\uff0c\u5176 \u5f62\u53c2 \u548c\u7c7b\u6210\u5458\u53d8\u91cf\u540c\u540d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c data \u5c06\u8868\u793a\u5f62\u53c2\uff0c\u6240\u4ee5\u4f60\u5fc5\u987b\u4f7f\u7528 \u201cthis->data\u201d \u6765\u8868\u793a\u6210\u5458\u53d8\u91cf\u3002\u5c3d\u7ba1\u8fd9\u4e48\u505a\u4e5f\u662f\u53ef\u4ee5\u88ab\u63a5\u53d7\u7684\u7f16\u7a0b\u98ce\u683c\uff0c\u4f46\u662f\u4f7f\u7528 m_ \u4f5c\u4e3a\u6210\u5458\u53d8\u91cf\u7684\u524d\u7f00\u5c31\u53ef\u4ee5\u4ece\u6e90\u5934\u4e0a\u6d88\u9664\u8fd9\u79cd\u6b67\u4e49\u3002","title":"\u663e\u5f0f\u5730\u4f7f\u7528 this \u6307\u9488"},{"location":"13-10-the-hidden-this-pointer/#_1","text":"\u53e6\u4e00\u65b9\u9762\uff0c\u6709\u65f6\u5019\u8ba9\u7c7b\u6210\u5458\u51fd\u6570\u8fd4\u56de\u5bf9\u8c61\u672c\u8eab\u4e5f\u662f\u5f88\u6709\u7528\u7684\u3002\u8fd9\u4e48\u505a\u7684\u4e00\u4e2a\u4e3b\u8981\u539f\u56e0\u5c31\u662f\u53ef\u4ee5\u5141\u8bb8\u51fd\u6570\u7684\u94fe\u5f0f\u8c03\u7528\uff0c\u4f7f\u5f97\u591a\u4e2a\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u4f5c\u7528\u4e8e\u4e00\u4e2a\u76f8\u540c\u7684\u5bf9\u8c61\uff01\u5176\u5b9e\u4f60\u5df2\u7ecf\u591a\u6b21\u4f7f\u7528\u8be5\u7279\u6027\u4e86\u3002\u5bf9\u4e8e\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u6765\u8bf4\uff0c std::cout \u8f93\u51fa\u4e86\u4e0d\u6b62\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002 1 std :: cout << \"Hello, \" << userName ; \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c std::cout \u5176\u5b9e\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u800c\u8fd0\u7b97\u7b26 << \u662f\u8be5\u5bf9\u8c61\u7684\u4e00\u4e2a\u6210\u5458\u51fd\u6570\u3002\u7f16\u8bd1\u5668\u4f1a\u6309\u7167\u4e0b\u9762\u7684\u65b9\u5f0f\u5bf9\u4e0a\u8ff0\u4ee3\u7801\u6c42\u503c\uff1a 1 ( std :: cout << \"Hello, \" ) << userName ; \u9996\u5148\uff0c << \u4f7f\u7528 std::cout \u548c\u5b57\u7b26\u4e32\u5b57\u9762\u91cf \u201cHello, \u201d \u5c06 \u201cHello, \u201d \u6253\u5370\u5230\u7ec8\u7aef\u3002\u4e0d\u8fc7\uff0c\u8fd9\u53ea\u662f\u4e0a\u8ff0\u8868\u8fbe\u5f0f\u7684\u4e00\u90e8\u5206\uff0c << \u8fd8\u9700\u8981\u8fd4\u56de\u4e00\u4e2a\u503c\uff08\u6216void\uff09\u3002\u5982\u679c\u5b83\u8fd4\u56de\u7684\u662f void\uff0c\u5219\u7ed3\u679c\u4f1a\u53d8\u4e3a\u4e0b\u9762\u7684\u5f62\u5f0f\uff1a 1 ( void ) << userName ; \u8fd9\u663e\u7136\u4e0d\u5408\u903b\u8f91\uff08\u7f16\u8bd1\u5668\u4f1a\u629b\u51fa\u9519\u8bef\uff09\u3002\u5b9e\u9645\u4e0a\uff0c << \u7684\u8fd4\u56de\u503c\u662f *this \uff0c\u5728\u4e0a\u9762\u7684\u8bed\u5883\u4e0b\uff0cthis \u5c31\u662f std::cout \u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u5f53\u7b2c\u4e00\u4e2a << \u6267\u884c\u5b8c\u6210\u540e\uff0c\u8bed\u53e5\u7b49\u4ef7\u4e8e\u5982\u4e0b\u5f62\u5f0f\uff1a 1 ( std :: cout ) << userName ; \u4e0a\u8ff0\u4ee3\u7801\u8fdb\u800c\u4f1a\u6253\u5370\u7528\u6237\u540d\u3002 \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ea\u9700\u8981\u6307\u5b9a\u5bf9\u8c61\u4e00\u6b21\uff08\u6b64\u4f8b\u4e2d\u4e3a std::cout )\uff0c\u7136\u540e\u6bcf\u6b21\u51fd\u6570\u8c03\u7528\u90fd\u53ef\u4ee5\u628a\u5b83\u4f20\u9012\u7ed9\u63a5\u4e0b\u6765\u7684\u51fd\u6570\uff0c\u662f\u6211\u4eec\u53ef\u4ee5\u94fe\u5f0f\u6267\u884c\u591a\u4e2a\u6307\u4ee4\u3002 \u5728\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u4e2d\u6211\u4eec\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u4e0a\u8ff0\u884c\u4e3a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 class Calc { private : int m_value { 0 }; public : void add ( int value ) { m_value += value ; } void sub ( int value ) { m_value -= value ; } void mult ( int value ) { m_value *= value ; } int getValue () { return m_value ; } }; \u5982\u679c\u4f60\u5e0c\u671b\u5148\u52a05\uff0c\u518d\u51cf3\uff0c\u7136\u540e\u518d\u4e58\u4ee54\uff0c\u90a3\u4e48\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { Calc calc {}; calc . add ( 5 ); // returns void calc . sub ( 3 ); // returns void calc . mult ( 4 ); // returns void std :: cout << calc . getValue () << '\\n' ; return 0 ; } \u4e0d\u8fc7\uff0c\u5982\u679c\u6bcf\u4e2a\u51fd\u6570\u90fd\u8fd4\u56de *this \u7684\u8bdd\uff0c\u5728\u53ef\u4ee5\u94fe\u5f0f\u5730\u8c03\u7528\u5b83\u4eec\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u53ef\u94fe\u5f0f\u8c03\u7528\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class Calc { private : int m_value {}; public : Calc & add ( int value ) { m_value += value ; return * this ; } Calc & sub ( int value ) { m_value -= value ; return * this ; } Calc & mult ( int value ) { m_value *= value ; return * this ; } int getValue () { return m_value ; } }; \u6ce8\u610f\uff0c\u51fd\u6570 add() \uff0c sub() \u548c mult() \u73b0\u5728\u90fd\u8fd4\u56de *this \u3002\u8fd9\u6837\u4e00\u6765\u5c31\u53ef\u4ee5\u5411\u4e0b\u9762\u8fd9\u6837\u8c03\u7528\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { Calc calc {}; calc . add ( 5 ). sub ( 3 ). mult ( 4 ); std :: cout << calc . getValue () << '\\n' ; return 0 ; } \u4e09\u884c\u4ee3\u7801\u88ab\u538b\u7f29\u6210\u4e86\u4e00\u884c\uff01\u6211\u4eec\u518d\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002 \u9996\u5148\uff0c\u8c03\u7528 calc.add(5) \u5c06 5 \u52a0\u5230 m_value \uff0c\u7136\u540e\u8fd4\u56de *this \uff0c\u5b83\u662f calc \u7684\u5f15\u7528\uff0c\u6240\u4ee5 calc \u4f1a\u88ab\u7528\u4e8e\u8c03\u7528\u63a5\u4e0b\u6765\u7684\u51fd\u6570\u3002\u518d\u63a5\u4e0b\u6765\uff0c\u5bf9 calc.sub(3) \u6c42\u503c\uff0c\u5b83\u5c06 3 \u4ece m_value \u51cf\u53bb\u540e\uff0c\u7ee7\u7eed\u8fd4\u56de calc \u3002\u6700\u540e\uff0c calc.mult(4) \u5c06 m_value \u4e58\u4ee5\u56db\u5e76\u8fd4\u56de calc \uff0c\u7531\u4e8e\u4e0d\u9700\u8981\u7ee7\u7eed\u8c03\u7528\u51fd\u6570\uff0c\u8fd9\u4e2a\u8fd4\u56de\u503c\u4e5f\u5c31\u88ab\u5ffd\u7565\u6389\u4e86\u3002 \u56e0\u4e3a\u6bcf\u4e2a\u51fd\u6570\u90fd\u4f1a\u4fee\u6539\u5bf9\u8c61 calc \uff0c\u6240\u4ee5 calc \u7684\u6210\u5458 m_value \u7684\u503c\u6b64\u65f6\u4e3a (((0 + 5) - 3) * 4) \uff0c\u53738\u3002","title":"\u94fe\u5f0f\u8c03\u7528\u6210\u5458\u51fd\u6570"},{"location":"13-10-the-hidden-this-pointer/#_2","text":"\u201cthis\u201d \u6307\u9488\u662f\u4e00\u4e2a\u88ab\u7f16\u8bd1\u5668\u9690\u5f0f\u6dfb\u52a0\u5230\u975e\u9759\u6001\u6210\u5458\u51fd\u6570\u7684\u9690\u85cf\u53c2\u6570\u3002\u591a\u6570\u60c5\u51b5\u4e0b\u4f60\u5e76\u4e0d\u4f1a\u76f4\u63a5\u8bbf\u95ee\u5b83\uff0c\u4f46\u662f\u5f53\u6709\u9700\u8981\u65f6\u4f60\u4f1a\u7528\u7684\u7740\u7684\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cthis\u662f\u4e00\u4e2a\u5e38\u91cf\u6307\u9488\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5b83\u4fee\u6539\u5b83\u6240\u6307\u7684\u5bf9\u8c61\uff0c\u4f46\u662f\u4f60\u4e0d\u80fd\u8ba9\u5b83\u6307\u5411\u522b\u7684\u5bf9\u8c61\u3002 \u5982\u679c\u6211\u4eec\u8ba9\u539f\u672c\u8fd4\u56de void \u7684\u51fd\u6570\u8fd4\u56de *this \uff0c\u5219\u53ef\u4ee5\u4f7f\u8fd9\u4e9b\u51fd\u6570\u80fd\u591f\u88ab\u94fe\u5f0f\u8c03\u7528\u3002 \u901a\u5e38\u5728\u91cd\u8f7d\u7c7b\u7684\u8fd0\u7b97\u7b26\u65f6\u4f1a\u7528\u5230\u8fd9\u4e2a\u7279\u6027 \uff0814\u7ae0\u4e2d\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u8fd0\u7b97\u7b26\u91cd\u8f7d\uff09\u3002","title":"\u5c0f\u7ed3"},{"location":"13-11-class-code-and-header-files/","tags":["class"],"text":"Key Takeaway \u7c7b\u5185\u7684\u51fd\u6570\u662f\u5185\u8054\u7684\uff0c\u4e0d\u53d7 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u7b2c\u4e8c\u6761\u7684\u9650\u5236\uff08\u5b9a\u4e49\u5728\u7a0b\u5e8f\u4e2d\u53ea\u80fd\u51fa\u73b0\u4e00\u6b21\uff09\uff0c\u6240\u4ee5\u53ef\u4ee5\u88ab\u5b9a\u4e49\u5230\u5934\u6587\u4ef6\u4e2d\u3002 \u88ab\u5206\u79bb\u5230\u7c7b\u5916\u7684\u51fd\u6570\u5b9a\u4e49\u662f\u666e\u901a\u51fd\u6570\uff0c\u53d7\u9650\u4e8e\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\uff0c\u56e0\u6b64\u53ea\u80fd\u88ab\u5b9a\u4e49\u5728\u6e90\u6587\u4ef6\u4e2d\u3002 \u6210\u5458\u51fd\u6570\u7684\u9ed8\u8ba4\u53c2\u6570\u5e94\u8be5\u88ab\u58f0\u660e\u5728\u7c7b\u5b9a\u4e49\u4e2d\uff08\u5728\u5934\u6587\u4ef6\u5185\uff09\uff0c\u8fd9\u4e9b\u53c2\u6570\u53ef\u4ee5\u88ab\u4efb\u4f55\u5305\u542b\u4e86\u8be5\u5934\u6587\u4ef6\u7684\u4ee3\u7801\u770b\u5230 \u5728\u7c7b\u5b9a\u4e49\u7684\u5916\u90e8\u5b9a\u4e49\u7c7b\u6210\u5458\u51fd\u6570 \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u7f16\u5199\u7684\u7c7b\u90fd\u5f88\u7b80\u5355\uff0c\u6240\u4ee5\u53ef\u4ee5\u5728\u7c7b\u7684\u5b9a\u4e49\u4e2d\u76f4\u63a5\u5b9e\u73b0\u6210\u5458\u51fd\u6570\u3002 \u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e0b\u5217 Date \u7c7b\u6765\u8bf4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 class Date { private : int m_year ; int m_month ; int m_day ; public : Date ( int year , int month , int day ) { setDate ( year , month , day ); } void setDate ( int year , int month , int day ) { m_year = year ; m_month = month ; m_day = day ; } int getYear () { return m_year ; } int getMonth () { return m_month ; } int getDay () { return m_day ; } }; \u4e0d\u8fc7\uff0c\u968f\u7740\u7c7b\u8d8a\u6765\u8d8a\u590d\u6742\uff0c\u5c06\u6240\u6709\u7684\u6210\u5458\u51fd\u6570\u90fd\u76f4\u63a5\u5b9a\u4e49\u5728\u7c7b\u4e2d\u4f1a\u5bfc\u81f4\u7c7b\u96be\u4ee5\u7ef4\u62a4\u3002\u4f7f\u7528\u4e00\u4e2a\u5df2\u7ecf\u5b9a\u4e49\u597d\u7684\u7c7b\u65f6\uff0c\u53ea\u9700\u8981\u5173\u6ce8\u5176\u516c\u5171\u63a5\u53e3\uff08 \u516c\u6709\u6210\u5458 \u51fd\u6570\uff09\u5373\u53ef\uff0c\u800c\u4e0d\u9700\u8981\u4e86\u89e3\u7c7b\u7684\u5e95\u5c42\u5de5\u4f5c\u539f\u7406\u3002\u6210\u5458\u51fd\u6570\u7684\u5b9e\u73b0\u7ec6\u8282\u53ea\u4f1a\u5bf9\u6211\u4eec\u9020\u6210\u5e72\u6270\u3002 \u5e78\u8fd0\u5730\u662f\uff0cC++ \u652f\u6301\u5c06\u7c7b\u7684\u201c\u58f0\u660e\u90e8\u5206\u201d\u548c\u5b9e\u73b0\u90e8\u5206\u5206\u79bb\u3002\u8fd9\u662f\u901a\u8fc7\u5728\u7c7b\u5b9a\u4e49\u4e4b\u5916\u5b9a\u4e49\u7c7b\u6210\u5458\u51fd\u6570\u6765\u5b9e\u73b0\u7684\u3002\u8981\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u53ea\u9700\u5c06\u7c7b\u7684\u6210\u5458\u51fd\u6570\u6309\u7167\u666e\u901a\u51fd\u6570\u90a3\u6837\u5b9a\u4e49\uff0c\u4f46\u5fc5\u987b\u5728\u51fd\u6570\u524d\u6dfb\u52a0 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 (\u4e0e\u547d\u540d\u7a7a\u95f4\u76f8\u540c)\u3002 \u5bf9\u4e8e\u4e0b\u9762\u8fd9\u4e2a Data \u7c7b\u5b9a\u4e49\u6765\u8bf4\uff0c\u5176\u6784\u9020\u51fd\u6570\u548c setDate() \u51fd\u6570\u88ab\u5b9a\u4e49\u5728\u4e86\u7c7b\u5916\u90e8\u3002\u6ce8\u610f\uff0c\u51fd\u6570\u7684\u539f\u578b\u4ecd\u7136\u5728\u7c7b\u5b9a\u4e49\u4e2d\uff0c\u4f46\u662f\u5176\u5b9e\u9645\u5b9e\u73b0\u88ab\u79fb\u52a8\u5230\u4e86\u7c7b\u5916\u90e8\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 class Date { private : int m_year ; int m_month ; int m_day ; public : Date ( int year , int month , int day ); void SetDate ( int year , int month , int day ); int getYear () { return m_year ; } int getMonth () { return m_month ; } int getDay () { return m_day ; } }; // Date constructor Date :: Date ( int year , int month , int day ) { SetDate ( year , month , day ); } // Date member function void Date :: SetDate ( int year , int month , int day ) { m_month = month ; m_day = day ; m_year = year ; } \u5f88\u7b80\u5355\u5427\u3002\u5bf9\u4e8e\u8fd9\u4e2a\u7c7b\u6765\u8bf4\uff0c\u56e0\u4e3a\u8bbf\u95ee\u51fd\u6570\u901a\u5e38\u53ea\u6709\u4e00\u884c\uff0c\u6240\u4ee5\u5b83\u4eec\u901a\u5e38\u88ab\u4fdd\u7559\u5728\u7c7b\u5b9a\u4e49\u4e2d\u3002 \u5728\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4e00\u4e2a\u5177 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u6784\u9020\u51fd\u6570\u88ab\u5b9a\u4e49\u5728\u4e86\u5916\u90e8\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Calc { private : int m_value = 0 ; public : Calc ( int value = 0 ) : m_value { value } {} Calc & add ( int value ) { m_value += value ; return * this ; } Calc & sub ( int value ) { m_value -= value ; return * this ; } Calc & mult ( int value ) { m_value *= value ; return * this ; } int getValue () { return m_value ; } }; \u6539\u5199\u4e3a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 class Calc { private : int m_value = 0 ; public : Calc ( int value = 0 ); Calc & add ( int value ); Calc & sub ( int value ); Calc & mult ( int value ); int getValue () { return m_value ; } }; Calc :: Calc ( int value ) : m_value { value } { } Calc & Calc :: add ( int value ) { m_value += value ; return * this ; } Calc & Calc :: sub ( int value ) { m_value -= value ; return * this ; } Calc & Calc :: mult ( int value ) { m_value *= value ; return * this ; } \u5c06\u7c7b\u7684\u5b9a\u4e49\u653e\u7f6e\u4e8e\u5934\u6587\u4ef6\u4e2d \u5728 2.11 - \u5934\u6587\u4ef6 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u51fd\u6570\u7684\u58f0\u660e\u53ef\u4ee5\u88ab\u653e\u7f6e\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u591a\u4e2a\u6587\u4ef6\u6216\u8005\u9879\u76ee\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u3002\u5bf9\u4e8e\u7c7b\u6765\u8bf4\u4e5f\u662f\u8fd9\u6837\u7684\u3002\u7c7b\u7684\u5b9a\u4e49\u53ef\u4ee5\u88ab\u653e\u7f6e\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u505a\u540c\u6837\u6709\u52a9\u4e8e\u5728\u591a\u4e2a\u6587\u4ef6\u6216\u9879\u76ee\u4e2d\u4f7f\u7528\u8be5\u7c7b\u3002\u4e00\u822c\u6765\u8bb2\uff0c\u7c7b\u7684\u5b9a\u4e49\u4f1a\u88ab\u653e\u7f6e\u4e0e\u5176\u540c\u540d\u7684\u5934\u6587\u4ef6\u4e2d\uff0c\u800c\u5176\u6210\u5458\u51fd\u6570\u5219\u88ab\u5b9a\u4e49\u5728\u4e0e\u7c7b\u540c\u540d\u7684 .cpp \u6587\u4ef6\u4e2d\u3002 \u4e0b\u9762\u8fd9\u4e2a\u7c7b\u7684\u5b9a\u4e49\u88ab\u5206\u6563\u5728\u4e86\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u4e2d\uff1a Date.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #ifndef DATE_H #define DATE_H class Date { private : int m_year ; int m_month ; int m_day ; public : Date ( int year , int month , int day ); void SetDate ( int year , int month , int day ); int getYear () { return m_year ; } int getMonth () { return m_month ; } int getDay () { return m_day ; } }; #endif Date.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include \"Date.h\" // Date constructor Date :: Date ( int year , int month , int day ) { SetDate ( year , month , day ); } // Date member function void Date :: SetDate ( int year , int month , int day ) { m_month = month ; m_day = day ; m_year = year ; } \u73b0\u5728\uff0c\u4efb\u4f55\u5176\u4ed6\u7684\u9700\u8981\u4f7f\u7528 Date \u7c7b\u7684\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u53ea\u6709 #include \"Date.h\" \u5373\u53ef\u3002\u6ce8\u610f\uff0c Date.cpp \u5fc5\u987b\u88ab\u7f16\u8bd1\u5230\u4efb\u4f55\u4f7f\u7528\u4e86 Date.h \u7684\u9879\u76ee\u4e2d\uff0c\u8fd9\u6837\u94fe\u63a5\u5668\u624d\u80fd\u77e5\u6653 Date \u7684\u5b9e\u73b0\u3002 \u5c06\u7c7b\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u96be\u9053\u4e0d\u4f1a\u8fdd\u53cd\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u5417\uff1f \u5e76\u4e0d\u4f1a\u3002\u5982\u679c\u4f60\u7684\u5934\u6587\u4ef6\u5305\u542b\u4e86\u5408\u9002\u7684 \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e \uff0c\u90a3\u4e48\u7c7b\u7684\u5b9a\u4e49\u5e76\u4e0d\u4f1a\u88ab\u591a\u6b21\u5305\u542b\u5230\u4e00\u4e2a\u6587\u4ef6\u4e2d\u3002 \u7c7b\u578b(\u5305\u62ec\u7c7b)\u4e0d\u53d7 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u7b2c\u4e8c\u6761\u9650\u5236\uff08\u5373\u5728\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u4e00\u4e2a\u53d8\u91cf\u6216\u666e\u901a\u51fd\u6570\u53ea\u80fd\u591f\u6709\u4e00\u4e2a\u5b9a\u4e49\uff09\u3002\u56e0\u6b64\uff0c\u5c06\u7c7b\u5b9a\u4e49 #include \u7c7b\u5b9a\u4e49\u5230\u5230\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\u5e76\u4e0d\u5b58\u5728\u95ee\u9898(\u5982\u679c\u5b58\u5728\u95ee\u9898\uff0c\u7c7b\u5c31\u6ca1\u6709\u591a\u5927\u7528\u5904\u4e86)\u3002 \u5c06\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u96be\u9053\u4e0d\u4f1a\u8fdd\u53cd\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u5417\uff1f \u89c6\u60c5\u51b5\u800c\u5b9a\u3002\u5728\u7c7b\u5b9a\u4e49\u4e2d\u5b9a\u4e49\u7684\u6210\u5458\u51fd\u6570\u88ab\u8ba4\u4e3a\u662f\u9690\u5f0f\u5185\u8054\u7684\u3002 \u5185\u8054\u51fd\u6570 \u4e0d\u53d7\u5355\u5b9a\u4e49\u89c4\u5219\u4e2d\u6bcf\u4e2a\u7a0b\u5e8f\u4e00\u4e2a\u5b9a\u4e49\u90e8\u5206\u7684\u9650\u5236\u3002\u8fd9\u610f\u5473\u7740\u5728\u7c7b\u5b9a\u4e49\u672c\u8eab\u4e2d\u5b9a\u4e49\u666e\u901a\u6210\u5458\u51fd\u6570(\u5982\u8bbf\u95ee\u51fd\u6570)\u6ca1\u6709\u95ee\u9898\u3002 \u5728\u7c7b\u5b9a\u4e49\u4e4b\u5916\u5b9a\u4e49\u7684\u6210\u5458\u51fd\u6570\u88ab\u89c6\u4e3a\u666e\u901a\u51fd\u6570\uff0c\u5e76\u4e14\u670d\u4ece\u5355\u5b9a\u4e49\u89c4\u5219\u4e2d\u6bcf\u4e2a\u7a0b\u5e8f\u90e8\u5206\u7684\u4e00\u4e2a\u5b9a\u4e49\u3002 \u56e0\u6b64\uff0c\u8fd9\u4e9b\u51fd\u6570\u5e94\u8be5\u5b9a\u4e49\u5728\u4ee3\u7801\u6587\u4ef6\u4e2d\uff0c\u800c\u4e0d\u662f\u5728\u5934\u6587\u4ef6\u4e2d\u3002\u4e00\u4e2a\u4f8b\u5916\u662f\u6a21\u677f\u51fd\u6570\uff0c\u5b83\u4eec\u4e5f\u662f\u9690\u5f0f\u5185\u8054\u7684\u3002 \u4f55\u65f6\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u6216\u6e90\u6587\u4ef6\uff1f\u53c8\u4f55\u65f6\u5b9a\u4e49\u5728\u7c7b\u5185\u6216\u7c7b\u5916\uff1f \u60a8\u53ef\u80fd\u4f1a\u503e\u5411\u4e8e\u5c06\u6240\u6709\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u653e\u5728\u7c7b\u5185\u90e8\u7684\u5934\u6587\u4ef6\u4e2d\u3002\u867d\u7136\u8fd9\u6837\u505a\u53ef\u4ee5\u7f16\u8bd1\uff0c\u4f46\u662f\u8fd9\u6837\u505a\u6709\u4e00\u4e9b\u7f3a\u70b9\u3002\u9996\u5148\uff0c\u5982\u4e0a\u6240\u8ff0\uff0c\u8fd9\u4f1a\u4f7f\u7c7b\u5b9a\u4e49\u53d8\u5f97\u6df7\u4e71\u3002\u5176\u6b21\uff0c\u5982\u679c\u66f4\u6539\u4e86\u5934\u6587\u4ef6\u4e2d\u7684\u4efb\u4f55\u4ee3\u7801\uff0c\u5219\u9700\u8981\u91cd\u65b0\u7f16\u8bd1\u5305\u542b\u8be5\u5934\u6587\u4ef6\u7684\u6bcf\u4e2a\u6587\u4ef6\u3002\u8fd9\u53ef\u80fd\u4f1a\u4ea7\u751f\u8fde\u9501\u53cd\u5e94\uff0c\u4e00\u4e2a\u5fae\u5c0f\u7684\u66f4\u6539\u5c31\u4f1a\u5bfc\u81f4\u6574\u4e2a\u7a0b\u5e8f\u9700\u8981\u91cd\u65b0\u7f16\u8bd1(\u8fd9\u53ef\u80fd\u4f1a\u5f88\u6162)\u3002\u5982\u679c\u60a8\u66f4\u6539\u4e86.cpp\u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\uff0c\u5219\u53ea\u9700\u8981\u91cd\u65b0\u7f16\u8bd1 .cpp \u6587\u4ef6\uff01 \u56e0\u6b64\u6211\u4eec\u63a8\u8350\u4e0b\u9762\u7684\u505a\u6cd5\uff1a \u5bf9\u4e8e\u53ea\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528\u800c\u4e0d\u9700\u8981\u91cd\u7528\u7684\u7c7b\u6765\u8bf4\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u4f7f\u7528\u5b83\u4eec\u7684 .cpp \u6587\u4ef6\u4e2d\u5b9a\u4e49\uff1b \u5bf9\u4e8e\u4f1a\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528\u6216\u8005\u671f\u671b\u88ab\u91cd\u7528\u7684\u7c7b\u6765\u8bf4\uff0c\u5c06\u5b83\u4eec\u5b9a\u4e49\u5728\u540c\u540d\u5934\u6587\u4ef6\u4e2d\u3002 \u5bf9\u4e8e\u7b80\u5355\u7684\u6210\u5458\u51fd\u6570\uff08\u7b80\u5355\u7684\u6784\u9020\u51fd\u6570\u3001\u6790\u6784\u51fd\u6570\u6216\u6210\u5458\u8bbf\u95ee\u51fd\u6570\uff09\u6765\u8bf4\uff0c\u5b83\u4eec\u53ef\u4ee5\u88ab\u5b9a\u4e49\u5728\u7c7b\u4e2d\uff1b \u5bf9\u4e8e\u8f83\u590d\u6742\u7684\u6210\u5458\u51fd\u6570\u6765\u8bf4\uff0c\u5b83\u4eec\u5e94\u8be5\u88ab\u5b9a\u4e49\u5728\u4e0e\u7c7b\u540c\u540d\u7684 cpp \u6587\u4ef6\u4e2d\u3002 \u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\uff0c\u5927\u591a\u6570\u7684\u7c7b\u90fd\u5c06\u88ab\u5b9a\u4e49\u5728 .cpp \u6587\u4ef6\u4e2d\uff0c\u540c\u65f6\u6240\u6709\u51fd\u6570\u90fd\u4f1a\u88ab\u76f4\u63a5\u5b9a\u4e49\u5728\u7c7b\u5185\u3002\u8fd9\u53ea\u662f\u4e3a\u4e86\u65b9\u4fbf\uff0c\u540c\u65f6\u53ef\u4ee5\u4fdd\u6301\u793a\u4f8b\u7b80\u77ed\u3002\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u5c06\u7c7b\u653e\u5728\u5b83\u4eec\u81ea\u5df1\u7684\u4ee3\u7801\u548c\u5934\u6587\u4ef6\u4e2d\u8981\u5e38\u89c1\u5f97\u591a\uff0c\u4f60\u5e94\u8be5\u4e60\u60ef\u4e8e\u8fd9\u6837\u505a\u3002 \u9ed8\u8ba4\u53c2\u6570 \u6210\u5458\u51fd\u6570\u7684\u9ed8\u8ba4\u53c2\u6570\u5e94\u8be5\u88ab\u58f0\u660e\u5728\u7c7b\u5b9a\u4e49\u4e2d\uff08\u5728\u5934\u6587\u4ef6\u5185\uff09\uff0c\u8fd9\u4e9b\u53c2\u6570\u53ef\u4ee5\u88ab\u4efb\u4f55\u5305\u542b\u4e86\u8be5\u5934\u6587\u4ef6\u7684\u4ee3\u7801\u770b\u5230\u3002 \u5e93 \u5206\u79bb\u7c7b\u5b9a\u4e49\u548c\u7c7b\u5b9e\u73b0\u5bf9\u4e8e\u5e93\u6765\u8bf4\u662f\u975e\u5e38\u5e38\u89c1\u7684\u3002\u5728\u7f16\u5199\u7a0b\u5e8f\u65f6\uff0c\u7a0b\u5e8f\u4e2d\u4f1a\u5b58\u5728\u5f88\u591a\u88ab #included \u6807\u51c6\u5e93\u7684\u5934\u6587\u4ef6\uff0c\u5982 iostream \u3001 string \u3001 vector \u3001 array \u7b49\u3002\u6ce8\u610f\uff0c\u4f60\u4e0d\u9700\u8981\u5728\u4f60\u7684\u9879\u76ee\u4e2d\u6dfb\u52a0 iostream.cpp , string.cpp , vector.cpp \u6216 array.cpp \u3002\u4f60\u7684\u7a0b\u5e8f\u9700\u8981\u6765\u81ea\u5934\u6587\u4ef6\u7684\u58f0\u660e\uff0c\u4ee5\u4fbf\u7f16\u8bd1\u5668\u9a8c\u8bc1\u4f60\u5199\u7684\u7a0b\u5e8f\u662f\u8bed\u6cd5\u6b63\u786e\u7684\u3002\u4f46\u662f\uff0c\u5c5e\u4e8eC++\u6807\u51c6\u5e93\u7684\u7c7b\u7684 \u5b9e\u73b0 \u5305\u542b\u5728\u9884\u7f16\u8bd1\u6587\u4ef6\u4e2d\uff0c\u8be5\u6587\u4ef6\u4f1a\u5728\u94fe\u63a5\u9636\u6bb5\u88ab\u94fe\u63a5\u8fdb\u53bb\u3002\u4f60\u6c38\u8fdc\u770b\u4e0d\u5230\u4ee3\u7801\u3002 \u9664\u4e86\u4e00\u4e9b\u5f00\u653e\u6e90\u7801\u8f6f\u4ef6(\u5176\u4e2d\u63d0\u4f9b .h \u548c .cpp \u6587\u4ef6)\u4e4b\u5916\uff0c\u5927\u591a\u6570\u7b2c\u4e09\u65b9\u5e93\u53ea\u63d0\u4f9b\u5934\u6587\u4ef6\u4ee5\u53ca\u9884\u7f16\u8bd1\u7684\u5e93\u6587\u4ef6\u3002\u8fd9\u6709\u51e0\u4e2a\u539f\u56e0\uff1a 1. \u94fe\u63a5\u9884\u7f16\u8bd1\u5e93\u6bd4\u6bcf\u6b21\u9700\u8981\u65f6\u91cd\u65b0\u7f16\u8bd1\u5b83\u66f4\u5feb\uff1b 2. \u9884\u7f16\u8bd1\u5e93\u7684\u4e00\u4e2a\u526f\u672c\u53ef\u4ee5\u88ab\u8bb8\u591a\u5e94\u7528\u7a0b\u5e8f\u5171\u4eab\uff0c\u800c\u7f16\u8bd1\u540e\u7684\u4ee3\u7801\u88ab\u7f16\u8bd1\u5230\u6bcf\u4e2a\u4f7f\u7528\u5b83\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4e2d(\u81a8\u80c0\u6587\u4ef6\u5927\u5c0f)\uff1b 3. \u77e5\u8bc6\u4ea7\u6743\u539f\u56e0(\u4f60\u4e0d\u5e0c\u671b\u522b\u4eba\u7a83\u53d6\u4f60\u7684\u4ee3\u7801)\u3002 \u5c06\u81ea\u5df1\u7684\u6587\u4ef6\u5206\u79bb\u4e3a\u58f0\u660e(\u5934\u6587\u4ef6)\u548c\u5b9e\u73b0(\u4ee3\u7801\u6587\u4ef6)\u4e0d\u4ec5\u662f\u4e00\u79cd\u5f88\u597d\u7684\u5f62\u5f0f\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u521b\u5efa\u81ea\u5df1\u7684\u81ea\u5b9a\u4e49\u5e93\u66f4\u5bb9\u6613\u3002\u521b\u5efa\u81ea\u5df1\u7684\u5e93\u8d85\u51fa\u4e86\u672c\u6559\u7a0b\u7684\u8303\u56f4\uff0c\u4f46\u662f\u5c06\u58f0\u660e\u548c\u5b9e\u73b0\u5206\u79bb\u662f\u8fd9\u6837\u505a\u7684\u5148\u51b3\u6761\u4ef6\u3002","title":"13.11 - \u7c7b\u4ee3\u7801\u548c\u5934\u6587\u4ef6"},{"location":"13-11-class-code-and-header-files/#_1","text":"\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u7f16\u5199\u7684\u7c7b\u90fd\u5f88\u7b80\u5355\uff0c\u6240\u4ee5\u53ef\u4ee5\u5728\u7c7b\u7684\u5b9a\u4e49\u4e2d\u76f4\u63a5\u5b9e\u73b0\u6210\u5458\u51fd\u6570\u3002 \u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e0b\u5217 Date \u7c7b\u6765\u8bf4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 class Date { private : int m_year ; int m_month ; int m_day ; public : Date ( int year , int month , int day ) { setDate ( year , month , day ); } void setDate ( int year , int month , int day ) { m_year = year ; m_month = month ; m_day = day ; } int getYear () { return m_year ; } int getMonth () { return m_month ; } int getDay () { return m_day ; } }; \u4e0d\u8fc7\uff0c\u968f\u7740\u7c7b\u8d8a\u6765\u8d8a\u590d\u6742\uff0c\u5c06\u6240\u6709\u7684\u6210\u5458\u51fd\u6570\u90fd\u76f4\u63a5\u5b9a\u4e49\u5728\u7c7b\u4e2d\u4f1a\u5bfc\u81f4\u7c7b\u96be\u4ee5\u7ef4\u62a4\u3002\u4f7f\u7528\u4e00\u4e2a\u5df2\u7ecf\u5b9a\u4e49\u597d\u7684\u7c7b\u65f6\uff0c\u53ea\u9700\u8981\u5173\u6ce8\u5176\u516c\u5171\u63a5\u53e3\uff08 \u516c\u6709\u6210\u5458 \u51fd\u6570\uff09\u5373\u53ef\uff0c\u800c\u4e0d\u9700\u8981\u4e86\u89e3\u7c7b\u7684\u5e95\u5c42\u5de5\u4f5c\u539f\u7406\u3002\u6210\u5458\u51fd\u6570\u7684\u5b9e\u73b0\u7ec6\u8282\u53ea\u4f1a\u5bf9\u6211\u4eec\u9020\u6210\u5e72\u6270\u3002 \u5e78\u8fd0\u5730\u662f\uff0cC++ \u652f\u6301\u5c06\u7c7b\u7684\u201c\u58f0\u660e\u90e8\u5206\u201d\u548c\u5b9e\u73b0\u90e8\u5206\u5206\u79bb\u3002\u8fd9\u662f\u901a\u8fc7\u5728\u7c7b\u5b9a\u4e49\u4e4b\u5916\u5b9a\u4e49\u7c7b\u6210\u5458\u51fd\u6570\u6765\u5b9e\u73b0\u7684\u3002\u8981\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u53ea\u9700\u5c06\u7c7b\u7684\u6210\u5458\u51fd\u6570\u6309\u7167\u666e\u901a\u51fd\u6570\u90a3\u6837\u5b9a\u4e49\uff0c\u4f46\u5fc5\u987b\u5728\u51fd\u6570\u524d\u6dfb\u52a0 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 (\u4e0e\u547d\u540d\u7a7a\u95f4\u76f8\u540c)\u3002 \u5bf9\u4e8e\u4e0b\u9762\u8fd9\u4e2a Data \u7c7b\u5b9a\u4e49\u6765\u8bf4\uff0c\u5176\u6784\u9020\u51fd\u6570\u548c setDate() \u51fd\u6570\u88ab\u5b9a\u4e49\u5728\u4e86\u7c7b\u5916\u90e8\u3002\u6ce8\u610f\uff0c\u51fd\u6570\u7684\u539f\u578b\u4ecd\u7136\u5728\u7c7b\u5b9a\u4e49\u4e2d\uff0c\u4f46\u662f\u5176\u5b9e\u9645\u5b9e\u73b0\u88ab\u79fb\u52a8\u5230\u4e86\u7c7b\u5916\u90e8\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 class Date { private : int m_year ; int m_month ; int m_day ; public : Date ( int year , int month , int day ); void SetDate ( int year , int month , int day ); int getYear () { return m_year ; } int getMonth () { return m_month ; } int getDay () { return m_day ; } }; // Date constructor Date :: Date ( int year , int month , int day ) { SetDate ( year , month , day ); } // Date member function void Date :: SetDate ( int year , int month , int day ) { m_month = month ; m_day = day ; m_year = year ; } \u5f88\u7b80\u5355\u5427\u3002\u5bf9\u4e8e\u8fd9\u4e2a\u7c7b\u6765\u8bf4\uff0c\u56e0\u4e3a\u8bbf\u95ee\u51fd\u6570\u901a\u5e38\u53ea\u6709\u4e00\u884c\uff0c\u6240\u4ee5\u5b83\u4eec\u901a\u5e38\u88ab\u4fdd\u7559\u5728\u7c7b\u5b9a\u4e49\u4e2d\u3002 \u5728\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4e00\u4e2a\u5177 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u6784\u9020\u51fd\u6570\u88ab\u5b9a\u4e49\u5728\u4e86\u5916\u90e8\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Calc { private : int m_value = 0 ; public : Calc ( int value = 0 ) : m_value { value } {} Calc & add ( int value ) { m_value += value ; return * this ; } Calc & sub ( int value ) { m_value -= value ; return * this ; } Calc & mult ( int value ) { m_value *= value ; return * this ; } int getValue () { return m_value ; } }; \u6539\u5199\u4e3a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 class Calc { private : int m_value = 0 ; public : Calc ( int value = 0 ); Calc & add ( int value ); Calc & sub ( int value ); Calc & mult ( int value ); int getValue () { return m_value ; } }; Calc :: Calc ( int value ) : m_value { value } { } Calc & Calc :: add ( int value ) { m_value += value ; return * this ; } Calc & Calc :: sub ( int value ) { m_value -= value ; return * this ; } Calc & Calc :: mult ( int value ) { m_value *= value ; return * this ; }","title":"\u5728\u7c7b\u5b9a\u4e49\u7684\u5916\u90e8\u5b9a\u4e49\u7c7b\u6210\u5458\u51fd\u6570"},{"location":"13-11-class-code-and-header-files/#_2","text":"\u5728 2.11 - \u5934\u6587\u4ef6 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u51fd\u6570\u7684\u58f0\u660e\u53ef\u4ee5\u88ab\u653e\u7f6e\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u591a\u4e2a\u6587\u4ef6\u6216\u8005\u9879\u76ee\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u3002\u5bf9\u4e8e\u7c7b\u6765\u8bf4\u4e5f\u662f\u8fd9\u6837\u7684\u3002\u7c7b\u7684\u5b9a\u4e49\u53ef\u4ee5\u88ab\u653e\u7f6e\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u505a\u540c\u6837\u6709\u52a9\u4e8e\u5728\u591a\u4e2a\u6587\u4ef6\u6216\u9879\u76ee\u4e2d\u4f7f\u7528\u8be5\u7c7b\u3002\u4e00\u822c\u6765\u8bb2\uff0c\u7c7b\u7684\u5b9a\u4e49\u4f1a\u88ab\u653e\u7f6e\u4e0e\u5176\u540c\u540d\u7684\u5934\u6587\u4ef6\u4e2d\uff0c\u800c\u5176\u6210\u5458\u51fd\u6570\u5219\u88ab\u5b9a\u4e49\u5728\u4e0e\u7c7b\u540c\u540d\u7684 .cpp \u6587\u4ef6\u4e2d\u3002 \u4e0b\u9762\u8fd9\u4e2a\u7c7b\u7684\u5b9a\u4e49\u88ab\u5206\u6563\u5728\u4e86\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u4e2d\uff1a Date.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #ifndef DATE_H #define DATE_H class Date { private : int m_year ; int m_month ; int m_day ; public : Date ( int year , int month , int day ); void SetDate ( int year , int month , int day ); int getYear () { return m_year ; } int getMonth () { return m_month ; } int getDay () { return m_day ; } }; #endif Date.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include \"Date.h\" // Date constructor Date :: Date ( int year , int month , int day ) { SetDate ( year , month , day ); } // Date member function void Date :: SetDate ( int year , int month , int day ) { m_month = month ; m_day = day ; m_year = year ; } \u73b0\u5728\uff0c\u4efb\u4f55\u5176\u4ed6\u7684\u9700\u8981\u4f7f\u7528 Date \u7c7b\u7684\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u53ea\u6709 #include \"Date.h\" \u5373\u53ef\u3002\u6ce8\u610f\uff0c Date.cpp \u5fc5\u987b\u88ab\u7f16\u8bd1\u5230\u4efb\u4f55\u4f7f\u7528\u4e86 Date.h \u7684\u9879\u76ee\u4e2d\uff0c\u8fd9\u6837\u94fe\u63a5\u5668\u624d\u80fd\u77e5\u6653 Date \u7684\u5b9e\u73b0\u3002","title":"\u5c06\u7c7b\u7684\u5b9a\u4e49\u653e\u7f6e\u4e8e\u5934\u6587\u4ef6\u4e2d"},{"location":"13-11-class-code-and-header-files/#_3","text":"\u5e76\u4e0d\u4f1a\u3002\u5982\u679c\u4f60\u7684\u5934\u6587\u4ef6\u5305\u542b\u4e86\u5408\u9002\u7684 \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e \uff0c\u90a3\u4e48\u7c7b\u7684\u5b9a\u4e49\u5e76\u4e0d\u4f1a\u88ab\u591a\u6b21\u5305\u542b\u5230\u4e00\u4e2a\u6587\u4ef6\u4e2d\u3002 \u7c7b\u578b(\u5305\u62ec\u7c7b)\u4e0d\u53d7 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u7b2c\u4e8c\u6761\u9650\u5236\uff08\u5373\u5728\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u4e00\u4e2a\u53d8\u91cf\u6216\u666e\u901a\u51fd\u6570\u53ea\u80fd\u591f\u6709\u4e00\u4e2a\u5b9a\u4e49\uff09\u3002\u56e0\u6b64\uff0c\u5c06\u7c7b\u5b9a\u4e49 #include \u7c7b\u5b9a\u4e49\u5230\u5230\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\u5e76\u4e0d\u5b58\u5728\u95ee\u9898(\u5982\u679c\u5b58\u5728\u95ee\u9898\uff0c\u7c7b\u5c31\u6ca1\u6709\u591a\u5927\u7528\u5904\u4e86)\u3002","title":"\u5c06\u7c7b\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u96be\u9053\u4e0d\u4f1a\u8fdd\u53cd\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u5417\uff1f"},{"location":"13-11-class-code-and-header-files/#_4","text":"\u89c6\u60c5\u51b5\u800c\u5b9a\u3002\u5728\u7c7b\u5b9a\u4e49\u4e2d\u5b9a\u4e49\u7684\u6210\u5458\u51fd\u6570\u88ab\u8ba4\u4e3a\u662f\u9690\u5f0f\u5185\u8054\u7684\u3002 \u5185\u8054\u51fd\u6570 \u4e0d\u53d7\u5355\u5b9a\u4e49\u89c4\u5219\u4e2d\u6bcf\u4e2a\u7a0b\u5e8f\u4e00\u4e2a\u5b9a\u4e49\u90e8\u5206\u7684\u9650\u5236\u3002\u8fd9\u610f\u5473\u7740\u5728\u7c7b\u5b9a\u4e49\u672c\u8eab\u4e2d\u5b9a\u4e49\u666e\u901a\u6210\u5458\u51fd\u6570(\u5982\u8bbf\u95ee\u51fd\u6570)\u6ca1\u6709\u95ee\u9898\u3002 \u5728\u7c7b\u5b9a\u4e49\u4e4b\u5916\u5b9a\u4e49\u7684\u6210\u5458\u51fd\u6570\u88ab\u89c6\u4e3a\u666e\u901a\u51fd\u6570\uff0c\u5e76\u4e14\u670d\u4ece\u5355\u5b9a\u4e49\u89c4\u5219\u4e2d\u6bcf\u4e2a\u7a0b\u5e8f\u90e8\u5206\u7684\u4e00\u4e2a\u5b9a\u4e49\u3002 \u56e0\u6b64\uff0c\u8fd9\u4e9b\u51fd\u6570\u5e94\u8be5\u5b9a\u4e49\u5728\u4ee3\u7801\u6587\u4ef6\u4e2d\uff0c\u800c\u4e0d\u662f\u5728\u5934\u6587\u4ef6\u4e2d\u3002\u4e00\u4e2a\u4f8b\u5916\u662f\u6a21\u677f\u51fd\u6570\uff0c\u5b83\u4eec\u4e5f\u662f\u9690\u5f0f\u5185\u8054\u7684\u3002","title":"\u5c06\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u96be\u9053\u4e0d\u4f1a\u8fdd\u53cd\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u5417\uff1f"},{"location":"13-11-class-code-and-header-files/#_5","text":"\u60a8\u53ef\u80fd\u4f1a\u503e\u5411\u4e8e\u5c06\u6240\u6709\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u653e\u5728\u7c7b\u5185\u90e8\u7684\u5934\u6587\u4ef6\u4e2d\u3002\u867d\u7136\u8fd9\u6837\u505a\u53ef\u4ee5\u7f16\u8bd1\uff0c\u4f46\u662f\u8fd9\u6837\u505a\u6709\u4e00\u4e9b\u7f3a\u70b9\u3002\u9996\u5148\uff0c\u5982\u4e0a\u6240\u8ff0\uff0c\u8fd9\u4f1a\u4f7f\u7c7b\u5b9a\u4e49\u53d8\u5f97\u6df7\u4e71\u3002\u5176\u6b21\uff0c\u5982\u679c\u66f4\u6539\u4e86\u5934\u6587\u4ef6\u4e2d\u7684\u4efb\u4f55\u4ee3\u7801\uff0c\u5219\u9700\u8981\u91cd\u65b0\u7f16\u8bd1\u5305\u542b\u8be5\u5934\u6587\u4ef6\u7684\u6bcf\u4e2a\u6587\u4ef6\u3002\u8fd9\u53ef\u80fd\u4f1a\u4ea7\u751f\u8fde\u9501\u53cd\u5e94\uff0c\u4e00\u4e2a\u5fae\u5c0f\u7684\u66f4\u6539\u5c31\u4f1a\u5bfc\u81f4\u6574\u4e2a\u7a0b\u5e8f\u9700\u8981\u91cd\u65b0\u7f16\u8bd1(\u8fd9\u53ef\u80fd\u4f1a\u5f88\u6162)\u3002\u5982\u679c\u60a8\u66f4\u6539\u4e86.cpp\u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\uff0c\u5219\u53ea\u9700\u8981\u91cd\u65b0\u7f16\u8bd1 .cpp \u6587\u4ef6\uff01 \u56e0\u6b64\u6211\u4eec\u63a8\u8350\u4e0b\u9762\u7684\u505a\u6cd5\uff1a \u5bf9\u4e8e\u53ea\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528\u800c\u4e0d\u9700\u8981\u91cd\u7528\u7684\u7c7b\u6765\u8bf4\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u4f7f\u7528\u5b83\u4eec\u7684 .cpp \u6587\u4ef6\u4e2d\u5b9a\u4e49\uff1b \u5bf9\u4e8e\u4f1a\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528\u6216\u8005\u671f\u671b\u88ab\u91cd\u7528\u7684\u7c7b\u6765\u8bf4\uff0c\u5c06\u5b83\u4eec\u5b9a\u4e49\u5728\u540c\u540d\u5934\u6587\u4ef6\u4e2d\u3002 \u5bf9\u4e8e\u7b80\u5355\u7684\u6210\u5458\u51fd\u6570\uff08\u7b80\u5355\u7684\u6784\u9020\u51fd\u6570\u3001\u6790\u6784\u51fd\u6570\u6216\u6210\u5458\u8bbf\u95ee\u51fd\u6570\uff09\u6765\u8bf4\uff0c\u5b83\u4eec\u53ef\u4ee5\u88ab\u5b9a\u4e49\u5728\u7c7b\u4e2d\uff1b \u5bf9\u4e8e\u8f83\u590d\u6742\u7684\u6210\u5458\u51fd\u6570\u6765\u8bf4\uff0c\u5b83\u4eec\u5e94\u8be5\u88ab\u5b9a\u4e49\u5728\u4e0e\u7c7b\u540c\u540d\u7684 cpp \u6587\u4ef6\u4e2d\u3002 \u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\uff0c\u5927\u591a\u6570\u7684\u7c7b\u90fd\u5c06\u88ab\u5b9a\u4e49\u5728 .cpp \u6587\u4ef6\u4e2d\uff0c\u540c\u65f6\u6240\u6709\u51fd\u6570\u90fd\u4f1a\u88ab\u76f4\u63a5\u5b9a\u4e49\u5728\u7c7b\u5185\u3002\u8fd9\u53ea\u662f\u4e3a\u4e86\u65b9\u4fbf\uff0c\u540c\u65f6\u53ef\u4ee5\u4fdd\u6301\u793a\u4f8b\u7b80\u77ed\u3002\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u5c06\u7c7b\u653e\u5728\u5b83\u4eec\u81ea\u5df1\u7684\u4ee3\u7801\u548c\u5934\u6587\u4ef6\u4e2d\u8981\u5e38\u89c1\u5f97\u591a\uff0c\u4f60\u5e94\u8be5\u4e60\u60ef\u4e8e\u8fd9\u6837\u505a\u3002","title":"\u4f55\u65f6\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u6216\u6e90\u6587\u4ef6\uff1f\u53c8\u4f55\u65f6\u5b9a\u4e49\u5728\u7c7b\u5185\u6216\u7c7b\u5916\uff1f"},{"location":"13-11-class-code-and-header-files/#_6","text":"\u6210\u5458\u51fd\u6570\u7684\u9ed8\u8ba4\u53c2\u6570\u5e94\u8be5\u88ab\u58f0\u660e\u5728\u7c7b\u5b9a\u4e49\u4e2d\uff08\u5728\u5934\u6587\u4ef6\u5185\uff09\uff0c\u8fd9\u4e9b\u53c2\u6570\u53ef\u4ee5\u88ab\u4efb\u4f55\u5305\u542b\u4e86\u8be5\u5934\u6587\u4ef6\u7684\u4ee3\u7801\u770b\u5230\u3002","title":"\u9ed8\u8ba4\u53c2\u6570"},{"location":"13-11-class-code-and-header-files/#_7","text":"\u5206\u79bb\u7c7b\u5b9a\u4e49\u548c\u7c7b\u5b9e\u73b0\u5bf9\u4e8e\u5e93\u6765\u8bf4\u662f\u975e\u5e38\u5e38\u89c1\u7684\u3002\u5728\u7f16\u5199\u7a0b\u5e8f\u65f6\uff0c\u7a0b\u5e8f\u4e2d\u4f1a\u5b58\u5728\u5f88\u591a\u88ab #included \u6807\u51c6\u5e93\u7684\u5934\u6587\u4ef6\uff0c\u5982 iostream \u3001 string \u3001 vector \u3001 array \u7b49\u3002\u6ce8\u610f\uff0c\u4f60\u4e0d\u9700\u8981\u5728\u4f60\u7684\u9879\u76ee\u4e2d\u6dfb\u52a0 iostream.cpp , string.cpp , vector.cpp \u6216 array.cpp \u3002\u4f60\u7684\u7a0b\u5e8f\u9700\u8981\u6765\u81ea\u5934\u6587\u4ef6\u7684\u58f0\u660e\uff0c\u4ee5\u4fbf\u7f16\u8bd1\u5668\u9a8c\u8bc1\u4f60\u5199\u7684\u7a0b\u5e8f\u662f\u8bed\u6cd5\u6b63\u786e\u7684\u3002\u4f46\u662f\uff0c\u5c5e\u4e8eC++\u6807\u51c6\u5e93\u7684\u7c7b\u7684 \u5b9e\u73b0 \u5305\u542b\u5728\u9884\u7f16\u8bd1\u6587\u4ef6\u4e2d\uff0c\u8be5\u6587\u4ef6\u4f1a\u5728\u94fe\u63a5\u9636\u6bb5\u88ab\u94fe\u63a5\u8fdb\u53bb\u3002\u4f60\u6c38\u8fdc\u770b\u4e0d\u5230\u4ee3\u7801\u3002 \u9664\u4e86\u4e00\u4e9b\u5f00\u653e\u6e90\u7801\u8f6f\u4ef6(\u5176\u4e2d\u63d0\u4f9b .h \u548c .cpp \u6587\u4ef6)\u4e4b\u5916\uff0c\u5927\u591a\u6570\u7b2c\u4e09\u65b9\u5e93\u53ea\u63d0\u4f9b\u5934\u6587\u4ef6\u4ee5\u53ca\u9884\u7f16\u8bd1\u7684\u5e93\u6587\u4ef6\u3002\u8fd9\u6709\u51e0\u4e2a\u539f\u56e0\uff1a 1. \u94fe\u63a5\u9884\u7f16\u8bd1\u5e93\u6bd4\u6bcf\u6b21\u9700\u8981\u65f6\u91cd\u65b0\u7f16\u8bd1\u5b83\u66f4\u5feb\uff1b 2. \u9884\u7f16\u8bd1\u5e93\u7684\u4e00\u4e2a\u526f\u672c\u53ef\u4ee5\u88ab\u8bb8\u591a\u5e94\u7528\u7a0b\u5e8f\u5171\u4eab\uff0c\u800c\u7f16\u8bd1\u540e\u7684\u4ee3\u7801\u88ab\u7f16\u8bd1\u5230\u6bcf\u4e2a\u4f7f\u7528\u5b83\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4e2d(\u81a8\u80c0\u6587\u4ef6\u5927\u5c0f)\uff1b 3. \u77e5\u8bc6\u4ea7\u6743\u539f\u56e0(\u4f60\u4e0d\u5e0c\u671b\u522b\u4eba\u7a83\u53d6\u4f60\u7684\u4ee3\u7801)\u3002 \u5c06\u81ea\u5df1\u7684\u6587\u4ef6\u5206\u79bb\u4e3a\u58f0\u660e(\u5934\u6587\u4ef6)\u548c\u5b9e\u73b0(\u4ee3\u7801\u6587\u4ef6)\u4e0d\u4ec5\u662f\u4e00\u79cd\u5f88\u597d\u7684\u5f62\u5f0f\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u521b\u5efa\u81ea\u5df1\u7684\u81ea\u5b9a\u4e49\u5e93\u66f4\u5bb9\u6613\u3002\u521b\u5efa\u81ea\u5df1\u7684\u5e93\u8d85\u51fa\u4e86\u672c\u6559\u7a0b\u7684\u8303\u56f4\uff0c\u4f46\u662f\u5c06\u58f0\u660e\u548c\u5b9e\u73b0\u5206\u79bb\u662f\u8fd9\u6837\u505a\u7684\u5148\u51b3\u6761\u4ef6\u3002","title":"\u5e93"},{"location":"13-12-const-class-objects-and-member-functions/","tags":["class","const class"],"text":"Key Takeaway const \u7c7b\u578b\u7684\u5bf9\u8c61\u53ea\u80fd\u8c03\u7528 const \u6210\u5458\u51fd\u6570\u3002 const \u6210\u5458\u51fd\u6570\u53ef\u4ee5\u88ab const \u6216 \u975econst \u7c7b\u578b\u7684\u5bf9\u8c61\u8c03\u7528 \u901a\u8fc7\u5b66\u4e60 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \uff0c\u6211\u4eec\u77e5\u9053\u57fa\u672c\u6570\u636e\u7c7b\u578b( int , double , char \u7b49)\uff0c\u53ef\u4ee5\u901a\u8fc7 const \u5173\u952e\u5b57\u88ab\u5b9a\u4e49\u4e3aconst\u7c7b\u578b\u3002\u540c\u65f6\u6240\u6709\u7684const\u53d8\u91cf\u90fd\u5fc5\u987b\u5728\u521b\u5efa\u65f6\u88ab\u521d\u59cb\u5316\u3002 \u5bf9\u4e8e const \u7c7b\u578b\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u521d\u59cb\u5316\u53ef\u4ee5\u901a\u8fc7 \u62f7\u8d1d\u521d\u59cb\u5316 \u3001 \u76f4\u63a5\u521d\u59cb\u5316 \u6216 \u7edf\u4e00\u521d\u59cb\u5316 \u6765\u5b8c\u6210\uff1a 1 2 3 const int value1 = 5 ; // \u62f7\u8d1d\u521d\u59cb\u5316 const int value2 ( 7 ); // \u76f4\u63a5\u521d\u59cb\u5316 const int value3 { 9 }; // \u7edf\u4e00\u521d\u59cb\u5316 (C++11) const \u7c7b \u540c\u6837\u7684\uff0c\u88ab \u5b9e\u4f8b\u5316 \u7684\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u901a\u8fc7const\u5173\u952e\u5b57\u88ab\u521b\u5efa\u4e3a const \u7c7b\u578b\u3002\u521d\u59cb\u5316\u5219\u662f\u901a\u8fc7 \u6784\u9020\u51fd\u6570 \u5b8c\u6210\u7684\uff1a 1 2 3 const Date date1 ; // \u4f7f\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316 const Date date2 ( 2020 , 10 , 16 ); // \u4f7f\u7528\u5e26\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u8fdb\u884c\u521d\u59cb\u5316 const Date date3 { 2020 , 10 , 16 }; // \u4f7f\u7528\u5e26\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u8fdb\u884c\u521d\u59cb\u5316(C++11) const \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u4e00\u65e6\u901a\u8fc7\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\uff0c\u5176\u4efb\u4f55\u6210\u5458\u53d8\u91cf\u90fd\u4e0d\u518d\u53ef\u4ee5\u88ab\u4fee\u6539\uff0c\u5426\u5219\u663e\u7136\u8fdd\u80cc\u4e86\u5176\u201c\u5e38\u91cf\u6027\u201d\u3002\u4e0d\u8bba\u662f\u76f4\u63a5\u4fee\u6539 \u516c\u6709\u6210\u5458 \u8fd8\u662f\u901a\u8fc7\u516c\u6709\u6210\u5458\u51fd\u6570\u4e3a\u6210\u5458\u53d8\u91cf\u8d4b\u503c\u90fd\u662f\u4e0d\u53ef\u4ee5\u7684\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Something { public : int m_value {}; Something () : m_value { 0 } { } void setValue ( int value ) { m_value = value ; } int getValue () { return m_value ; } }; int main () { const Something something {}; // calls default constructor something . m_value = 5 ; // compiler error: violates const something . setValue ( 5 ); // compiler error: violates const return 0 ; } \u4e0a\u9762\u6d89\u53ca\u53d8\u91cf something \u7684\u4e24\u884c\u90fd\u662f\u975e\u6cd5\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u8981\u4e48\u8bd5\u56fe\u76f4\u63a5\u66f4\u6539\u6210\u5458\u53d8\u91cf\uff0c\u8981\u4e48\u8c03\u7528\u8bd5\u56fe\u66f4\u6539\u6210\u5458\u53d8\u91cf\u7684\u6210\u5458\u51fd\u6570\uff0c\u4ece\u800c\u8fdd\u53cd\u4e86 something \u7684\u5e38\u91cf\u6027\u3002 \u4e0e\u666e\u901a\u53d8\u91cf\u4e00\u6837\uff0c\u5f53\u9700\u8981\u786e\u4fdd\u7c7b\u5bf9\u8c61\u5728\u521b\u5efa\u540e\u4e0d\u518d\u88ab\u4fee\u6539\u65f6\uff0c\u53ef\u4ee5\u5c06\u5b83\u4eec\u8bbe\u7f6e\u4e3aconst\u3002 const \u6210\u5458\u51fd\u6570 \u8003\u8651\u4e0b\u9762\u8fd9\u884c\u4ee3\u7801\uff1a 1 std :: cout << something . getValue (); \u4e0a\u8ff0\u4ee3\u7801\u53ef\u80fd\u4f1a\u8ba9\u4f60\u611f\u5230\u5403\u60ca\uff0c\u5b83\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\uff0c\u5373\u4f7f getValue() \u6ca1\u6709\u53bb\u6539\u53d8\u4efb\u4f55\u6210\u5458\u53d8\u91cf\uff01\u5b9e\u9645\u4e0a\uff0c\u4f5c\u4e3a const \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u5b83\u53ea\u80fd\u663e\u5f0f\u5730\u8c03\u7528 const \u7c7b\u578b\u7684\u6210\u5458\u51fd\u6570\uff0c\u800c getValue() \u5e76\u4e0d\u662f const\u7c7b\u578b\u7684\u3002 const \u6210\u5458\u51fd\u6570 \u662f\u4e00\u7c7b\u6210\u5458\u51fd\u6570\uff0c\u5b83\u4fdd\u8bc1\u81ea\u5df1\u4e0d\u4f1a\u4fee\u6539\u5bf9\u8c61\uff0c\u4e5f\u4e0d\u4f1a\u8c03\u7528\u4efb\u4f55\u975econst\u7c7b\u578b\u7684\u6210\u5458\u51fd\u6570\uff08\u56e0\u4e3a\u5b83\u4eec\u53ef\u80fd\u4f1a\u4fee\u6539\u5bf9\u8c61\uff09\u3002 \u8981\u5c06 getValue() \u58f0\u660e\u4e3a const \u6210\u5458\u51fd\u6570\uff0c\u53ea\u9700\u8981\u5728\u51fd\u6570\u539f\u578b\u4e2d\u4f7f\u7528 const \u5173\u952e\u5b57\u5373\u53ef\uff08\u653e\u5728\u53c2\u6570\u5217\u8868\u540e\uff0c\u51fd\u6570\u4f53\u4e4b\u524d\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 class Something { public : int m_value {}; Something () : m_value { 0 } { } void resetValue () { m_value = 0 ; } void setValue ( int value ) { m_value = value ; } int getValue () const { return m_value ; } // \u6ce8\u610f\u53c2\u6570\u5217\u8868\u4e4b\u540e\uff0c\u51fd\u6570\u4f53\u4e4b\u524d\u7684 const \u5173\u952e\u5b57 }; \u73b0\u5728\uff0c getValue() \u5df2\u7ecf\u662f const \u6210\u5458\u51fd\u6570\u4e86\uff0c\u6b64\u65f6\u5b83\u5c31\u53ef\u4ee5\u88ab\u5176\u4ed6 const \u5bf9\u8c61\u8c03\u7528\u3002 \u5bf9\u4e8e\u5b9a\u4e49\u5728\u7c7b\u5916\u7684\u6210\u5458\u51fd\u6570\u6765\u8bf4\uff0c\u4e0d\u4ec5\u51fd\u6570\u539f\u578b\u4e2d\u9700\u8981const\u5173\u952e\u5b57\uff0c\u51fd\u6570\u5b9a\u4e49\u65f6\u4e5f\u5fc5\u987b\u6dfb\u52a0 const \u5173\u952e\u5b57\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Something { public : int m_value {}; Something () : m_value { 0 } { } void resetValue () { m_value = 0 ; } void setValue ( int value ) { m_value = value ; } int getValue () const ; // \u6ce8\u610f\u6b64\u5904\u7684 const \u5173\u952e\u5b57 }; int Something :: getValue () const // \u8fd9\u91cc\u4e5f\u9700\u8981 { return m_value ; } \u4e0d\u4ec5\u5982\u6b64\uff0c\u4efb\u4f55 const \u6210\u5458\u51fd\u6570\u5982\u679c\u5c1d\u8bd5\u4fee\u6539\u6210\u5458\u53d8\u91cf\u6216\u8005\u8c03\u7528\u975econst\u7c7b\u578b\u7684\u51fd\u6570\uff0c\u90fd\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u4fdd\u5b58\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 class Something { public : int m_value {}; void resetValue () const { m_value = 0 ; } // compile error, const functions can't change member variables. }; \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c resetValue() \u88ab\u6807\u8bb0\u4e3a const \u6210\u5458\u51fd\u6570\uff0c\u4f46\u662f\u5b83\u5c1d\u8bd5\u4fee\u6539 m_value \u3002\u56e0\u6b64\u5bfc\u81f4\u4e86\u7f16\u8bd1\u9519\u8bef\u3002 \u6ce8\u610f\uff0c\u6784\u9020\u51fd\u6570\u4e0d\u80fd\u88ab\u6807\u8bb0\u4e3aconst\u3002\u8fd9\u662f\u56e0\u4e3a\u6784\u9020\u51fd\u6570\u9700\u8981\u80fd\u591f\u521d\u59cb\u5316\u5b83\u4eec\u7684\u6210\u5458\u53d8\u91cf\uff0c\u800cconst\u6784\u9020\u51fd\u6570\u4e0d\u80fd\u8fd9\u6837\u505a\u3002\u56e0\u6b64\uff0c\u8be5\u8bed\u8a00\u4e0d\u5141\u8bb8\u4f7f\u7528const\u6784\u9020\u51fd\u6570\u3002 const\u6210\u5458\u51fd\u6570\u4e5f\u53ef\u4ee5\u7531\u975econst\u5bf9\u8c61\u8c03\u7528\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c06\u4efb\u4f55\u4e0d\u9700\u8981\u4fee\u6539\u5bf9\u8c61\u72b6\u6001\u7684\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u4e3a const\uff0c\u8fd9\u6837\u5b83\u5c31\u53ef\u4ee5\u88ab const \u7c7b\u578b\u7684\u5bf9\u8c61\u8c03\u7528\u3002 \u901a\u8fc7 const \u7c7b\u578b\u5f15\u7528\u4f20\u9012 const \u5bf9\u8c61 \u5c3d\u7ba1\u5b9e\u4f8b\u5316const\u7c7b\u5bf9\u8c61\u662f\u521b\u5efaconst\u5bf9\u8c61\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u4f46\u83b7\u5f97const\u5bf9\u8c61\u66f4\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u901a\u8fc7const\u5f15\u7528\u5c06\u5bf9\u8c61\u4f20\u9012\u7ed9\u51fd\u6570\u3002 \u5728 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u901a\u8fc7\u5e38\u91cf\u5f15\u7528\u4f20\u9012\u7c7b\u53c2\u6570\u65f6\u7684\u7279\u6027\u3002\u56de\u5fc6\u4e00\u4e0b\uff0c \u6309\u503c\u4f20\u9012 \u5bf9\u8c61\u4f1a\u5bfc\u81f4\u5bf9\u8c61\u88ab\u8d4b\u503c\uff08\u6548\u7387\u4f4e\uff09\u2014\u2014\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e76\u4e0d\u9700\u8981\u4e00\u4efd\u62f7\u8d1d\uff0c\u4f7f\u7528\u539f\u59cb\u5bf9\u8c61\u7684\u5f15\u7528\u5c31\u53ef\u4ee5\u4e86\uff0c\u800c\u4e14\u7531\u4e8e\u907f\u514d\u4e86\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\uff0c\u6027\u80fd\u81ea\u7136\u4e5f\u66f4\u597d\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528 const \u7c7b\u578b\u7684\u5f15\u7528\u6765\u786e\u4fdd\u5b9e\u53c2\u4e0d\u88ab\u4fee\u6539\uff0c\u800c\u4e14\u4f7f\u5f97\u51fd\u6570\u53ef\u4ee5\u914d\u5408 \u53f3\u503c \u6765\u4f7f\u7528\uff08\u4f8b\u5982\u5b57\u9762\u91cf\uff09\uff0c\u56e0\u4e3a\u53f3\u503c\u53ea\u80fd\u901a\u8fc7const\u5f15\u7528\u4f20\u9012\u3002 \u4f60\u80fd\u770b\u51fa\u4e0b\u9762\u4ee3\u7801\u4e2d\u7684\u95ee\u9898\u5417\uff1f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include class Date { private : int m_year {}; int m_month {}; int m_day {}; public : Date ( int year , int month , int day ) { setDate ( year , month , day ); } void setDate ( int year , int month , int day ) { m_year = year ; m_month = month ; m_day = day ; } int getYear () { return m_year ; } int getMonth () { return m_month ; } int getDay () { return m_day ; } }; // \u6ce8\u610f\uff1a\u6211\u4eec\u901a\u8fc7\u4f20 const \u5f15\u7528\u6765\u907f\u514d\u62f7\u8d1d data void printDate ( const Date & date ) { std :: cout << date . getYear () << '/' << date . getMonth () << '/' << date . getDay () << '\\n' ; } int main () { Date date { 2016 , 10 , 16 }; printDate ( date ); return 0 ; } \u8fd9\u91cc\u7684\u95ee\u9898\u5728\u4e8e\uff0c\u5728\u51fd\u6570 printDate \u51fd\u6570\u4e2d\uff0c date \u88ab\u5f53\u505aconst\u5bf9\u8c61\uff0c\u56e0\u6b64\u57fa\u4e8econst\u5bf9\u8c61 date \u8c03\u7528 getYear() \u3001 getMonth() \u548c getDay() \u8fd9\u4e9b\u975econst\u6210\u5458\u51fd\u6570\u7684\u65f6\u5019\uff0c\u5c31\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u9700\u8981\u5c06 getYear() \u3001 getMonth() \u548c getDay() \u5b9a\u4e49\u4e3a const\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 class Date { private : int m_year {}; int m_month {}; int m_day {}; public : Date ( int year , int month , int day ) { setDate ( year , month , day ); } // setDate() cannot be const, modifies member variables void setDate ( int year , int month , int day ) { m_year = year ; m_month = month ; m_day = day ; } // The following getters can all be made const int getYear () const { return m_year ; } int getMonth () const { return m_month ; } int getDay () const { return m_day ; } }; \u8fd9\u6837\u4e00\u6765 printDate() \u51fd\u6570\u4e2d\u7684 const date \u5c31\u53ef\u4ee5\u6b63\u5e38\u8c03\u7528 getYear() \u3001 getMonth() \u548c getDay() \u4e86\u3002 const \u6210\u5458\u4e0d\u80fd\u8fd4\u56de\u975econst\u5f15\u7528\u6210\u5458 \u5982\u679c\u6210\u5458\u51fd\u6570\u662f const \u7684\uff0c\u5219 *this \u6307\u9488\u4e5f\u662fconst\u7684\uff0c\u8fd9\u5c31\u610f\u5473\u7740\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6240\u6709\u7c7b\u6210\u5458\u90fd\u4f1a\u88ab\u5f53\u505a\u662fconst\u7684\u3002\u56e0\u6b64\u4e00\u4e2a const \u6210\u5458\u51fd\u6570\u4e0d\u80fd\u8fd4\u56de\u975econst\u5f15\u7528\u6210\u5458\u3002const\u6210\u5458\u51fd\u6570\u53ea\u80fd\u8fd4\u56deconst\u5f15\u7528\u6210\u5458\u3002 \u5728\u4e0b\u4e00\u4e2a\u7ae0\u8282\u6211\u4eec\u4f1a\u770b\u5230\u5177\u4f53\u7684\u4f8b\u5b50\u3002 \u91cd\u8f7d const \u548c \u975e const \u51fd\u6570 \u6700\u540e\uff0c\u5c3d\u7ba1\u4e0d\u5e38\u7528\uff0c\u4f46\u6211\u4eec\u7684\u786e\u53ef\u4ee5\u901a\u8fc7\u91cd\u8f7d\u83b7\u5f97\u540c\u4e00\u51fd\u6570\u7684const\u548c\u975econst\u7248\u672c\u3002\u56e0\u4e3aconst\u9650\u5b9a\u7b26\u88ab\u8ba4\u4e3a\u662f\u51fd\u6570\u7b7e\u540d\u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u5f53\u4e24\u4e2a\u51fd\u6570\u5177\u6709\u4e0d\u540c\u7684 const \u7b7e\u540d\u7684\u51fd\u6570\u4f1a\u88ab\u8ba4\u4e3a\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include class Something { private : std :: string m_value {}; public : Something ( const std :: string & value = \"\" ) : m_value { value } {} const std :: string & getValue () const { return m_value ; } // getValue() for const objects (returns const reference) std :: string & getValue () { return m_value ; } // getValue() for non-const objects (returns non-const reference) }; \u5f53\u4f7f\u7528const\u5bf9\u8c61\u8c03\u7528\u6210\u5458\u51fd\u6570\u65f6\uff0c\u8c03\u7528\u7684\u4fbf\u662f const \u7248\u672c\u7684\u51fd\u6570\u3002\u5bf9\u4e8e\u975econst\u6210\u5458\u800c\u8a00\uff0c\u5219\u8c03\u7528\u7684\u662f\u975econst\u6210\u5458\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { Something something ; something . getValue () = \"Hi\" ; // calls non-const getValue(); const Something something2 ; something2 . getValue (); // calls const getValue(); return 0 ; } \u5f53\u8fd4\u56de\u503c\u9700\u8981\u4e0d\u540c\u7684\u201c\u5e38\u91cf\u6027\u201d\u65f6\uff08\u5373\u5e38\u91cf\u6216\u975e\u5e38\u91cf\uff09\uff0c\u5219\u9700\u8981\u91cd\u8f7dconst\u548c\u975econst\u7248\u672c\u7684\u51fd\u6570\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c getValue() \u7684\u975econst\u7248\u672c\u53ea\u80fd\u914d\u5408\u975econst\u5bf9\u8c61\u6765\u4f7f\u7528\uff0c\u4f46\u662f\u5b83\u66f4\u7075\u6d3b\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u8bfb m_value \uff0c\u6216\u8005\u5199 m_value (\u901a\u8fc7\u8d4b\u503c\u5b57\u7b26\u4e32\" Hi \"\u6765\u5b9e\u73b0)\u3002 getValue() \u7684const\u7248\u672c\u65e2\u53ef\u4ee5\u5904\u7406const\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u5904\u7406\u975econst\u5bf9\u8c61\uff0c\u4f46\u8fd4\u56deconst\u5f15\u7528\uff0c\u4ee5\u786e\u4fdd\u4e0d\u80fd\u4fee\u6539const\u5bf9\u8c61\u7684\u6570\u636e\u3002 \u5c0f\u7ed3 \u56e0\u4e3a\u901a\u8fc7const\u5f15\u7528\u4f20\u9012\u5bf9\u8c61\u662f\u5e38\u89c1\u7684\uff0c\u6240\u4ee5\u7c7b\u5e94\u8be5\u662fconst\u53cb\u597d\u7684\u3002\u8fd9\u610f\u5473\u7740\u5e94\u8be5\u5c06\u4efb\u4f55\u4e0d\u4fee\u6539\u7c7b\u5bf9\u8c61\u7684\u72b6\u6001\u7684\u6210\u5458\u51fd\u6570\u8bbe\u7f6e\u4e3aconst\uff01","title":"13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570"},{"location":"13-12-const-class-objects-and-member-functions/#const","text":"\u540c\u6837\u7684\uff0c\u88ab \u5b9e\u4f8b\u5316 \u7684\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u901a\u8fc7const\u5173\u952e\u5b57\u88ab\u521b\u5efa\u4e3a const \u7c7b\u578b\u3002\u521d\u59cb\u5316\u5219\u662f\u901a\u8fc7 \u6784\u9020\u51fd\u6570 \u5b8c\u6210\u7684\uff1a 1 2 3 const Date date1 ; // \u4f7f\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316 const Date date2 ( 2020 , 10 , 16 ); // \u4f7f\u7528\u5e26\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u8fdb\u884c\u521d\u59cb\u5316 const Date date3 { 2020 , 10 , 16 }; // \u4f7f\u7528\u5e26\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u8fdb\u884c\u521d\u59cb\u5316(C++11) const \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u4e00\u65e6\u901a\u8fc7\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\uff0c\u5176\u4efb\u4f55\u6210\u5458\u53d8\u91cf\u90fd\u4e0d\u518d\u53ef\u4ee5\u88ab\u4fee\u6539\uff0c\u5426\u5219\u663e\u7136\u8fdd\u80cc\u4e86\u5176\u201c\u5e38\u91cf\u6027\u201d\u3002\u4e0d\u8bba\u662f\u76f4\u63a5\u4fee\u6539 \u516c\u6709\u6210\u5458 \u8fd8\u662f\u901a\u8fc7\u516c\u6709\u6210\u5458\u51fd\u6570\u4e3a\u6210\u5458\u53d8\u91cf\u8d4b\u503c\u90fd\u662f\u4e0d\u53ef\u4ee5\u7684\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Something { public : int m_value {}; Something () : m_value { 0 } { } void setValue ( int value ) { m_value = value ; } int getValue () { return m_value ; } }; int main () { const Something something {}; // calls default constructor something . m_value = 5 ; // compiler error: violates const something . setValue ( 5 ); // compiler error: violates const return 0 ; } \u4e0a\u9762\u6d89\u53ca\u53d8\u91cf something \u7684\u4e24\u884c\u90fd\u662f\u975e\u6cd5\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u8981\u4e48\u8bd5\u56fe\u76f4\u63a5\u66f4\u6539\u6210\u5458\u53d8\u91cf\uff0c\u8981\u4e48\u8c03\u7528\u8bd5\u56fe\u66f4\u6539\u6210\u5458\u53d8\u91cf\u7684\u6210\u5458\u51fd\u6570\uff0c\u4ece\u800c\u8fdd\u53cd\u4e86 something \u7684\u5e38\u91cf\u6027\u3002 \u4e0e\u666e\u901a\u53d8\u91cf\u4e00\u6837\uff0c\u5f53\u9700\u8981\u786e\u4fdd\u7c7b\u5bf9\u8c61\u5728\u521b\u5efa\u540e\u4e0d\u518d\u88ab\u4fee\u6539\u65f6\uff0c\u53ef\u4ee5\u5c06\u5b83\u4eec\u8bbe\u7f6e\u4e3aconst\u3002","title":"const \u7c7b"},{"location":"13-12-const-class-objects-and-member-functions/#const_1","text":"\u8003\u8651\u4e0b\u9762\u8fd9\u884c\u4ee3\u7801\uff1a 1 std :: cout << something . getValue (); \u4e0a\u8ff0\u4ee3\u7801\u53ef\u80fd\u4f1a\u8ba9\u4f60\u611f\u5230\u5403\u60ca\uff0c\u5b83\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\uff0c\u5373\u4f7f getValue() \u6ca1\u6709\u53bb\u6539\u53d8\u4efb\u4f55\u6210\u5458\u53d8\u91cf\uff01\u5b9e\u9645\u4e0a\uff0c\u4f5c\u4e3a const \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u5b83\u53ea\u80fd\u663e\u5f0f\u5730\u8c03\u7528 const \u7c7b\u578b\u7684\u6210\u5458\u51fd\u6570\uff0c\u800c getValue() \u5e76\u4e0d\u662f const\u7c7b\u578b\u7684\u3002 const \u6210\u5458\u51fd\u6570 \u662f\u4e00\u7c7b\u6210\u5458\u51fd\u6570\uff0c\u5b83\u4fdd\u8bc1\u81ea\u5df1\u4e0d\u4f1a\u4fee\u6539\u5bf9\u8c61\uff0c\u4e5f\u4e0d\u4f1a\u8c03\u7528\u4efb\u4f55\u975econst\u7c7b\u578b\u7684\u6210\u5458\u51fd\u6570\uff08\u56e0\u4e3a\u5b83\u4eec\u53ef\u80fd\u4f1a\u4fee\u6539\u5bf9\u8c61\uff09\u3002 \u8981\u5c06 getValue() \u58f0\u660e\u4e3a const \u6210\u5458\u51fd\u6570\uff0c\u53ea\u9700\u8981\u5728\u51fd\u6570\u539f\u578b\u4e2d\u4f7f\u7528 const \u5173\u952e\u5b57\u5373\u53ef\uff08\u653e\u5728\u53c2\u6570\u5217\u8868\u540e\uff0c\u51fd\u6570\u4f53\u4e4b\u524d\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 class Something { public : int m_value {}; Something () : m_value { 0 } { } void resetValue () { m_value = 0 ; } void setValue ( int value ) { m_value = value ; } int getValue () const { return m_value ; } // \u6ce8\u610f\u53c2\u6570\u5217\u8868\u4e4b\u540e\uff0c\u51fd\u6570\u4f53\u4e4b\u524d\u7684 const \u5173\u952e\u5b57 }; \u73b0\u5728\uff0c getValue() \u5df2\u7ecf\u662f const \u6210\u5458\u51fd\u6570\u4e86\uff0c\u6b64\u65f6\u5b83\u5c31\u53ef\u4ee5\u88ab\u5176\u4ed6 const \u5bf9\u8c61\u8c03\u7528\u3002 \u5bf9\u4e8e\u5b9a\u4e49\u5728\u7c7b\u5916\u7684\u6210\u5458\u51fd\u6570\u6765\u8bf4\uff0c\u4e0d\u4ec5\u51fd\u6570\u539f\u578b\u4e2d\u9700\u8981const\u5173\u952e\u5b57\uff0c\u51fd\u6570\u5b9a\u4e49\u65f6\u4e5f\u5fc5\u987b\u6dfb\u52a0 const \u5173\u952e\u5b57\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Something { public : int m_value {}; Something () : m_value { 0 } { } void resetValue () { m_value = 0 ; } void setValue ( int value ) { m_value = value ; } int getValue () const ; // \u6ce8\u610f\u6b64\u5904\u7684 const \u5173\u952e\u5b57 }; int Something :: getValue () const // \u8fd9\u91cc\u4e5f\u9700\u8981 { return m_value ; } \u4e0d\u4ec5\u5982\u6b64\uff0c\u4efb\u4f55 const \u6210\u5458\u51fd\u6570\u5982\u679c\u5c1d\u8bd5\u4fee\u6539\u6210\u5458\u53d8\u91cf\u6216\u8005\u8c03\u7528\u975econst\u7c7b\u578b\u7684\u51fd\u6570\uff0c\u90fd\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u4fdd\u5b58\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 class Something { public : int m_value {}; void resetValue () const { m_value = 0 ; } // compile error, const functions can't change member variables. }; \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c resetValue() \u88ab\u6807\u8bb0\u4e3a const \u6210\u5458\u51fd\u6570\uff0c\u4f46\u662f\u5b83\u5c1d\u8bd5\u4fee\u6539 m_value \u3002\u56e0\u6b64\u5bfc\u81f4\u4e86\u7f16\u8bd1\u9519\u8bef\u3002 \u6ce8\u610f\uff0c\u6784\u9020\u51fd\u6570\u4e0d\u80fd\u88ab\u6807\u8bb0\u4e3aconst\u3002\u8fd9\u662f\u56e0\u4e3a\u6784\u9020\u51fd\u6570\u9700\u8981\u80fd\u591f\u521d\u59cb\u5316\u5b83\u4eec\u7684\u6210\u5458\u53d8\u91cf\uff0c\u800cconst\u6784\u9020\u51fd\u6570\u4e0d\u80fd\u8fd9\u6837\u505a\u3002\u56e0\u6b64\uff0c\u8be5\u8bed\u8a00\u4e0d\u5141\u8bb8\u4f7f\u7528const\u6784\u9020\u51fd\u6570\u3002 const\u6210\u5458\u51fd\u6570\u4e5f\u53ef\u4ee5\u7531\u975econst\u5bf9\u8c61\u8c03\u7528\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c06\u4efb\u4f55\u4e0d\u9700\u8981\u4fee\u6539\u5bf9\u8c61\u72b6\u6001\u7684\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u4e3a const\uff0c\u8fd9\u6837\u5b83\u5c31\u53ef\u4ee5\u88ab const \u7c7b\u578b\u7684\u5bf9\u8c61\u8c03\u7528\u3002","title":"const \u6210\u5458\u51fd\u6570"},{"location":"13-12-const-class-objects-and-member-functions/#const-const","text":"\u5c3d\u7ba1\u5b9e\u4f8b\u5316const\u7c7b\u5bf9\u8c61\u662f\u521b\u5efaconst\u5bf9\u8c61\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u4f46\u83b7\u5f97const\u5bf9\u8c61\u66f4\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u901a\u8fc7const\u5f15\u7528\u5c06\u5bf9\u8c61\u4f20\u9012\u7ed9\u51fd\u6570\u3002 \u5728 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u901a\u8fc7\u5e38\u91cf\u5f15\u7528\u4f20\u9012\u7c7b\u53c2\u6570\u65f6\u7684\u7279\u6027\u3002\u56de\u5fc6\u4e00\u4e0b\uff0c \u6309\u503c\u4f20\u9012 \u5bf9\u8c61\u4f1a\u5bfc\u81f4\u5bf9\u8c61\u88ab\u8d4b\u503c\uff08\u6548\u7387\u4f4e\uff09\u2014\u2014\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e76\u4e0d\u9700\u8981\u4e00\u4efd\u62f7\u8d1d\uff0c\u4f7f\u7528\u539f\u59cb\u5bf9\u8c61\u7684\u5f15\u7528\u5c31\u53ef\u4ee5\u4e86\uff0c\u800c\u4e14\u7531\u4e8e\u907f\u514d\u4e86\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\uff0c\u6027\u80fd\u81ea\u7136\u4e5f\u66f4\u597d\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528 const \u7c7b\u578b\u7684\u5f15\u7528\u6765\u786e\u4fdd\u5b9e\u53c2\u4e0d\u88ab\u4fee\u6539\uff0c\u800c\u4e14\u4f7f\u5f97\u51fd\u6570\u53ef\u4ee5\u914d\u5408 \u53f3\u503c \u6765\u4f7f\u7528\uff08\u4f8b\u5982\u5b57\u9762\u91cf\uff09\uff0c\u56e0\u4e3a\u53f3\u503c\u53ea\u80fd\u901a\u8fc7const\u5f15\u7528\u4f20\u9012\u3002 \u4f60\u80fd\u770b\u51fa\u4e0b\u9762\u4ee3\u7801\u4e2d\u7684\u95ee\u9898\u5417\uff1f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include class Date { private : int m_year {}; int m_month {}; int m_day {}; public : Date ( int year , int month , int day ) { setDate ( year , month , day ); } void setDate ( int year , int month , int day ) { m_year = year ; m_month = month ; m_day = day ; } int getYear () { return m_year ; } int getMonth () { return m_month ; } int getDay () { return m_day ; } }; // \u6ce8\u610f\uff1a\u6211\u4eec\u901a\u8fc7\u4f20 const \u5f15\u7528\u6765\u907f\u514d\u62f7\u8d1d data void printDate ( const Date & date ) { std :: cout << date . getYear () << '/' << date . getMonth () << '/' << date . getDay () << '\\n' ; } int main () { Date date { 2016 , 10 , 16 }; printDate ( date ); return 0 ; } \u8fd9\u91cc\u7684\u95ee\u9898\u5728\u4e8e\uff0c\u5728\u51fd\u6570 printDate \u51fd\u6570\u4e2d\uff0c date \u88ab\u5f53\u505aconst\u5bf9\u8c61\uff0c\u56e0\u6b64\u57fa\u4e8econst\u5bf9\u8c61 date \u8c03\u7528 getYear() \u3001 getMonth() \u548c getDay() \u8fd9\u4e9b\u975econst\u6210\u5458\u51fd\u6570\u7684\u65f6\u5019\uff0c\u5c31\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u9700\u8981\u5c06 getYear() \u3001 getMonth() \u548c getDay() \u5b9a\u4e49\u4e3a const\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 class Date { private : int m_year {}; int m_month {}; int m_day {}; public : Date ( int year , int month , int day ) { setDate ( year , month , day ); } // setDate() cannot be const, modifies member variables void setDate ( int year , int month , int day ) { m_year = year ; m_month = month ; m_day = day ; } // The following getters can all be made const int getYear () const { return m_year ; } int getMonth () const { return m_month ; } int getDay () const { return m_day ; } }; \u8fd9\u6837\u4e00\u6765 printDate() \u51fd\u6570\u4e2d\u7684 const date \u5c31\u53ef\u4ee5\u6b63\u5e38\u8c03\u7528 getYear() \u3001 getMonth() \u548c getDay() \u4e86\u3002","title":"\u901a\u8fc7 const \u7c7b\u578b\u5f15\u7528\u4f20\u9012 const \u5bf9\u8c61"},{"location":"13-12-const-class-objects-and-member-functions/#const-const_1","text":"\u5982\u679c\u6210\u5458\u51fd\u6570\u662f const \u7684\uff0c\u5219 *this \u6307\u9488\u4e5f\u662fconst\u7684\uff0c\u8fd9\u5c31\u610f\u5473\u7740\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6240\u6709\u7c7b\u6210\u5458\u90fd\u4f1a\u88ab\u5f53\u505a\u662fconst\u7684\u3002\u56e0\u6b64\u4e00\u4e2a const \u6210\u5458\u51fd\u6570\u4e0d\u80fd\u8fd4\u56de\u975econst\u5f15\u7528\u6210\u5458\u3002const\u6210\u5458\u51fd\u6570\u53ea\u80fd\u8fd4\u56deconst\u5f15\u7528\u6210\u5458\u3002 \u5728\u4e0b\u4e00\u4e2a\u7ae0\u8282\u6211\u4eec\u4f1a\u770b\u5230\u5177\u4f53\u7684\u4f8b\u5b50\u3002","title":"const \u6210\u5458\u4e0d\u80fd\u8fd4\u56de\u975econst\u5f15\u7528\u6210\u5458"},{"location":"13-12-const-class-objects-and-member-functions/#const-const_2","text":"\u6700\u540e\uff0c\u5c3d\u7ba1\u4e0d\u5e38\u7528\uff0c\u4f46\u6211\u4eec\u7684\u786e\u53ef\u4ee5\u901a\u8fc7\u91cd\u8f7d\u83b7\u5f97\u540c\u4e00\u51fd\u6570\u7684const\u548c\u975econst\u7248\u672c\u3002\u56e0\u4e3aconst\u9650\u5b9a\u7b26\u88ab\u8ba4\u4e3a\u662f\u51fd\u6570\u7b7e\u540d\u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u5f53\u4e24\u4e2a\u51fd\u6570\u5177\u6709\u4e0d\u540c\u7684 const \u7b7e\u540d\u7684\u51fd\u6570\u4f1a\u88ab\u8ba4\u4e3a\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include class Something { private : std :: string m_value {}; public : Something ( const std :: string & value = \"\" ) : m_value { value } {} const std :: string & getValue () const { return m_value ; } // getValue() for const objects (returns const reference) std :: string & getValue () { return m_value ; } // getValue() for non-const objects (returns non-const reference) }; \u5f53\u4f7f\u7528const\u5bf9\u8c61\u8c03\u7528\u6210\u5458\u51fd\u6570\u65f6\uff0c\u8c03\u7528\u7684\u4fbf\u662f const \u7248\u672c\u7684\u51fd\u6570\u3002\u5bf9\u4e8e\u975econst\u6210\u5458\u800c\u8a00\uff0c\u5219\u8c03\u7528\u7684\u662f\u975econst\u6210\u5458\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { Something something ; something . getValue () = \"Hi\" ; // calls non-const getValue(); const Something something2 ; something2 . getValue (); // calls const getValue(); return 0 ; } \u5f53\u8fd4\u56de\u503c\u9700\u8981\u4e0d\u540c\u7684\u201c\u5e38\u91cf\u6027\u201d\u65f6\uff08\u5373\u5e38\u91cf\u6216\u975e\u5e38\u91cf\uff09\uff0c\u5219\u9700\u8981\u91cd\u8f7dconst\u548c\u975econst\u7248\u672c\u7684\u51fd\u6570\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c getValue() \u7684\u975econst\u7248\u672c\u53ea\u80fd\u914d\u5408\u975econst\u5bf9\u8c61\u6765\u4f7f\u7528\uff0c\u4f46\u662f\u5b83\u66f4\u7075\u6d3b\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u8bfb m_value \uff0c\u6216\u8005\u5199 m_value (\u901a\u8fc7\u8d4b\u503c\u5b57\u7b26\u4e32\" Hi \"\u6765\u5b9e\u73b0)\u3002 getValue() \u7684const\u7248\u672c\u65e2\u53ef\u4ee5\u5904\u7406const\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u5904\u7406\u975econst\u5bf9\u8c61\uff0c\u4f46\u8fd4\u56deconst\u5f15\u7528\uff0c\u4ee5\u786e\u4fdd\u4e0d\u80fd\u4fee\u6539const\u5bf9\u8c61\u7684\u6570\u636e\u3002","title":"\u91cd\u8f7d const \u548c \u975e const \u51fd\u6570"},{"location":"13-12-const-class-objects-and-member-functions/#_1","text":"\u56e0\u4e3a\u901a\u8fc7const\u5f15\u7528\u4f20\u9012\u5bf9\u8c61\u662f\u5e38\u89c1\u7684\uff0c\u6240\u4ee5\u7c7b\u5e94\u8be5\u662fconst\u53cb\u597d\u7684\u3002\u8fd9\u610f\u5473\u7740\u5e94\u8be5\u5c06\u4efb\u4f55\u4e0d\u4fee\u6539\u7c7b\u5bf9\u8c61\u7684\u72b6\u6001\u7684\u6210\u5458\u51fd\u6570\u8bbe\u7f6e\u4e3aconst\uff01","title":"\u5c0f\u7ed3"},{"location":"13-13-static-member-variables/","tags":["class","static member variable"],"text":"\u91cd\u6e29 static \u5173\u952e\u5b57 \u5728 6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf \u4e2d\u6211\u4eec\u5b66\u4e60\u4e86 static \u5173\u952e\u5b57\uff0c\u5b83\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u5e76\u786e\u4fdd\u5b83\u4e0d\u4f1a\u5728 \u79bb\u5f00\u4f5c\u7528\u57df \u540e\u88ab\u9500\u6bc1\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int generateID () { static int s_id { 0 }; return ++ s_id ; } int main () { std :: cout << generateID () << '\\n' ; std :: cout << generateID () << '\\n' ; std :: cout << generateID () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 1 2 3 \u6ce8\u610f\uff0c s_id \u7684\u503c\u5728\u591a\u6b21\u51fd\u6570\u8c03\u7528\u95f4\u5f97\u4ee5\u4fdd\u7559\u3002 static \u5173\u952e\u5b57\u5728\u7528\u4e8e \u5168\u5c40\u53d8\u91cf \u65f6\uff0c\u8fd8\u6709\u53e6\u5916\u7684\u542b\u4e49\u2014\u2014 \u5b83\u4f1a\u8d4b\u4e88\u8be5\u53d8\u91cf \u5185\u90e8\u94fe\u63a5 \u5c5e\u6027\uff08\u4e0d\u80fd\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u6587\u4ef6\u5916\u4f7f\u7528\uff09\u3002\u56e0\u4e3a\u5168\u5c40\u53d8\u91cf\u5e94\u8be5\u88ab\u675c\u7edd\uff0c\u6240\u4ee5 static \u7684\u8fd9\u65b9\u9762\u5e94\u7528\u5e76\u4e0d\u5e38\u89c1\u3002 \u9759\u6001\u6210\u5458\u53d8\u91cf static \u5173\u952e\u5b57\u5728\u5e94\u7528\u4e8e\u7c7b\u6210\u5458\u65f6\uff0c\u6709\u4e24\u4e2a\u989d\u5916\u7684\u7528\u9014\uff1a \u9759\u6001\u6210\u5458\u53d8\u91cf \u548c \u9759\u6001\u6210\u5458\u51fd\u6570 \u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e24\u79cd static \u7528\u6cd5\u90fd\u975e\u5e38\u7b80\u5355\u76f4\u63a5\u3002\u6211\u4eec\u4f1a\u5728\u672c\u7ae0\u4ecb\u7ecd\u9759\u6001\u6210\u5458\u53d8\u91cf\uff0c\u7136\u540e\u5728\u4e0b\u4e00\u7ae0\u4ecb\u7ecd\u9759\u6001\u6210\u5458\u51fd\u6570\u3002 \u5728\u5f00\u59cb\u5b66\u4e60\u4e3a\u6210\u5458\u53d8\u91cf\u6dfb\u52a0 static \u5173\u952e\u5b57\u4e4b\u524d\uff0c\u8bf7\u5148\u8003\u8651\u4e0b\u9762\u7684\u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include class Something { public : int m_value { 1 }; }; int main () { Something first ; Something second ; first . m_value = 2 ; std :: cout << first . m_value << '\\n' ; std :: cout << second . m_value << '\\n' ; return 0 ; } \u5f53\u6211\u4eec\u5b9e\u4f8b\u5316\u5bf9\u8c61\u7684\u65f6\u5019\uff0c\u5bf9\u8c61\u5404\u81ea\u521b\u5efa\u5176\u6210\u5458\u51fd\u6570\u7684\u4e00\u4efd\u62f7\u8d1d\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a Something \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u6700\u7ec8\u6211\u4eec\u4f1a\u5f97\u5230\u4e24\u4efd m_value : first.m_value \u548c second.m_value \u3002\u5b83\u4eec\u4e24\u4e2a\u662f\u5404\u81ea\u72ec\u7acb\u7684\uff0c\u56e0\u6b64\u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 2 1 \u4f7f\u7528 static \u5173\u952e\u5b57\u53ef\u4ee5\u521b\u5efa\u9759\u6001\u6210\u5458\u53d8\u91cf\u3002\u548c\u666e\u901a\u6210\u5458\u53d8\u91cf\u4e0d\u540c\u7684\u662f\uff0c\u9759\u6001\u6210\u5458\u53d8\u91cf\u5728\u540c\u4e00\u4e2a\u7c7b\u7684\u5bf9\u8c61\u95f4\u662f\u5171\u4eab\u7684\u3002\u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include class Something { public : static int s_value ; }; int Something :: s_value { 1 }; int main () { Something first ; Something second ; first . s_value = 2 ; std :: cout << first . s_value << '\\n' ; std :: cout << second . s_value << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 2 2 \u56e0\u4e3a s_value \u662f\u9759\u6001\u6210\u5458\u53d8\u91cf\uff0c\u6240\u4ee5 s_value \u5728\u5404\u4e2a\u5bf9\u8c61\u95f4\u662f\u5171\u4eab\u7684\u3002\u5176\u7ed3\u679c\u5c31\u662f\uff0c first.s_value is \u4e0e second.s_value \u5b9e\u9645\u4e0a\u662f\u540c\u4e00\u4e2a\u3002\u56e0\u6b64\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 first \u8bbe\u7f6e s_value \u7684\u503c\uff0c\u5e76\u901a\u8fc7 second \u8bbf\u95ee\u5b83\u3002 \u9759\u6001\u6210\u5458\u53d8\u91cf\u5e76\u4e0d\u548c\u7c7b\u5bf9\u8c61\u5173\u8054 \u5c3d\u7ba1\u4f60\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8c61\u6765\u8bbf\u95ee\u9759\u6001\u6210\u5458(\u4f8b\u5982\uff1a first.s_value \u548c second.s_value )\uff0c\u4f46\u5b9e\u9645\u4e0a\u8fd9\u4e9b\u9759\u6001\u6210\u5458\u5728\u5bf9\u8c61\u88ab\u5b9e\u4f8b\u5316\u524d\u5c31\u5b58\u5728\u4e86\u3002\u5b83\u4eec\u66f4\u50cf\u662f\u5168\u5c40\u53d8\u91cf\uff0c\u4f1a\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u521b\u5efa\uff0c\u5728\u7a0b\u5e8f\u9000\u51fa\u65f6\u9500\u6bc1\u3002 \u56e0\u6b64\uff0c\u6700\u597d\u8ba4\u4e3a\u9759\u6001\u6210\u5458\u662f\u5c5e\u4e8e\u7c7b\u672c\u8eab\u7684\uff0c\u800c\u4e0d\u662f\u7c7b\u7684\u67d0\u4e2a\u5b9e\u4f8b\u5bf9\u8c61\u3002\u56e0\u4e3a s_value \u72ec\u7acb\u4e8e\u4efb\u4f55\u7c7b\u5bf9\u8c61\u800c\u5b58\u5728\uff0c\u6240\u4ee5\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7c7b\u540d\u548c \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 (\u5728\u672c\u4f8b\u4e2d\u4e3a Something::s_value )\u8bbf\u95ee\u5b83: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include class Something { public : static int s_value ; // \u58f0\u660e\u9759\u6001\u6210\u5458\u53d8\u91cf }; int Something :: s_value { 1 }; // \u5b9a\u4e49\u9759\u6001\u6210\u5458\u53d8\u91cf int main () { // \u6ce8\u610f\uff1a\u6ca1\u6709\u5b9e\u4f8b\u5316\u4efb\u4f55\u5bf9\u8c61 Something :: s_value = 2 ; std :: cout << Something :: s_value << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c s_value \u662f\u4f7f\u7528\u7c7b\u540d\u8fdb\u884c\u8bbf\u95ee\u7684\uff0c\u800c\u6ca1\u6709\u901a\u8fc7\u4efb\u4f55\u8be5\u7c7b\u7684\u53d8\u91cf\u53bb\u8bbf\u95ee\u3002\u6211\u4eec\u751a\u81f3\u90fd\u8fd8\u6ca1\u6709\u5b9e\u4f8b\u5316\u4efb\u4f55\u8be5\u7c7b\u7684\u5bf9\u8c61\uff0c\u4f46\u662f\u4ecd\u7136\u53ef\u4ee5\u901a\u8fc7 Something::s_value \u6765\u8bbf\u95ee\u5b83\u3002\u8fd9\u79cd\u65b9\u5f0f\u662f\u66f4\u4e3a\u63a8\u8350\u7684\u4f7f\u7528\u9759\u6001\u6210\u5458\u7684\u65b9\u6cd5\u3002 \u6700\u4f73\u5b9e\u8df5 \u901a\u8fc7\u7c7b\u540d\uff08\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\uff09\u6765\u8bbf\u95ee\u9759\u6001\u6210\u5458\u53d8\u91cf\u800c\u4e0d\u662f\u901a\u8fc7\u5bf9\u8c61\u6765\u8bbf\u95ee\uff08\u4f7f\u7528\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26\uff09\u3002 \u5b9a\u4e49\u548c\u521d\u59cb\u5316\u9759\u6001\u6210\u5458\u53d8\u91cf \u5f53\u6211\u4eec\u5728\u7c7b\u5185\u90e8 \u58f0\u660e \u9759\u6001\u6210\u5458\u53d8\u91cf\u65f6\uff0c\u6211\u4eec\u662f\u5728\u544a\u8bc9\u7f16\u8bd1\u5668\u9759\u6001\u6210\u5458\u53d8\u91cf\u7684\u5b58\u5728\uff0c\u800c\u4e0d\u662f\u771f\u6b63\u5b9a\u4e49\u5b83(\u7c7b\u4f3c \u524d\u5411\u58f0\u660e )\u3002\u56e0\u4e3a\u9759\u6001\u6210\u5458\u53d8\u91cf\u4e0d\u662f\u5355\u4e2a\u7c7b\u5bf9\u8c61\u7684\u4e00\u90e8\u5206(\u5b83\u4eec\u4e0e\u5168\u5c40\u53d8\u91cf\u5904\u7406\u76f8\u4f3c\uff0c\u5e76\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u521d\u59cb\u5316)\uff0c\u6240\u4ee5\u5fc5\u987b\u5728\u7c7b\u5916\u90e8\u7684\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u663e\u5f0f\u5730\u5b9a\u4e49\u9759\u6001\u6210\u5458\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u8fd9\u4e00\u884c\u6765\u5b9e\u73b0\u9759\u6001\u6210\u5458\u7684\u5b9a\u4e49 1 int Something :: s_value { 1 }; // \u5b9a\u4e49\u9759\u6001\u6210\u5458\u53d8\u91cf \u8fd9\u4e00\u884c\u6709\u4e24\u4e2a\u76ee\u7684\uff1a\u5b9e\u4f8b\u5316\u9759\u6001\u6210\u5458\u53d8\u91cf(\u5c31\u50cf\u5168\u5c40\u53d8\u91cf\u4e00\u6837)\uff0c\u5e76\u53ef\u9009\u5730\u521d\u59cb\u5316\u5b83\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u63d0\u4f9b\u4e86\u521d\u59cb\u5316\u503c1\u3002\u5982\u679c\u6ca1\u6709\u63d0\u4f9b\u521d\u59cb\u5316\u503c\uff0cC++\u5c06\u8be5\u503c\u521d\u59cb\u5316\u4e3a0\u3002 \u6ce8\u610f\uff0c\u9759\u6001\u6210\u5458\u7684 \u5b9a\u4e49 \u4e0d\u53d7 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u7684\u9650\u5236\uff0c\u60a8\u53ef\u4ee5\u5b9a\u4e49\u548c\u521d\u59cb\u5316\u53d8\u91cf\uff0c\u5373\u4f7f\u5b83\u5728\u7c7b\u4e2d\u58f0\u660e\u4e3aprivate(\u6216protected)\u3002 \u5982\u679c\u7c7b\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u9759\u6001\u6210\u5458\u5b9a\u4e49\u901a\u5e38\u653e\u5728\u7c7b\u7684\u76f8\u5173\u4ee3\u7801\u6587\u4ef6\u4e2d(\u4f8b\u5982 Something.cpp )\u3002\u5982\u679c\u7c7b\u5b9a\u4e49\u5728 .cpp \u6587\u4ef6\u4e2d\uff0c\u5219\u9759\u6001\u6210\u5458\u5b9a\u4e49\u901a\u5e38\u76f4\u63a5\u653e\u5728\u7c7b\u7684\u4e0b\u9762\u3002\u4e0d\u8981\u5c06\u9759\u6001\u6210\u5458\u5b9a\u4e49\u653e\u5728\u5934\u6587\u4ef6\u4e2d(\u5f88\u50cf\u5168\u5c40\u53d8\u91cf\uff0c\u5982\u679c\u5934\u6587\u4ef6\u88ab\u5305\u542b\u4e0d\u6b62\u4e00\u6b21\uff0c\u6700\u7ec8\u5c06\u5f97\u5230\u591a\u4e2a\u5b9a\u4e49\uff0c\u8fd9\u5c06\u5bfc\u81f4\u94fe\u63a5\u5668\u9519\u8bef)\u3002 \u9759\u6001\u6210\u5458\u53d8\u91cf\u7684\u5185\u8054\u521d\u59cb\u5316 \u6709\u4e00\u4e9b\u6377\u5f84\u53ef\u4ee5\u5b9e\u73b0\u4e0a\u8ff0\u76ee\u6807\u3002\u9996\u5148\uff0c\u5f53\u9759\u6001\u6210\u5458\u662fconst\u6574\u578b\u7c7b\u578b(\u5305\u62ecchar\u548cbool)\u6216const enum\u65f6\uff0c\u53ef\u4ee5\u5728\u7c7b\u5b9a\u4e49\u4e2d\u521d\u59cb\u5316\u9759\u6001\u6210\u5458: 1 2 3 4 5 class Whatever { public : static const int s_value { 4 }; // a static const int can be declared and initialized directly }; \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u9759\u6001\u6210\u5458\u53d8\u91cf\u662f const \u7684\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u663e\u5f0f\u5730\u5b9a\u4e49\u5b83\u3002 \u5176\u6b21\uff0c static constexpr \u6210\u5458\u53ef\u4ee5\u5728\u7c7b\u5b9a\u4e49\u4e2d\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 #include class Whatever { public : static constexpr double s_value { 2.2 }; // ok static constexpr std :: array < int , 3 > s_array { 1 , 2 , 3 }; // this even works for classes that support constexpr initialization }; \u6700\u540e\uff0c\u5bf9\u4e8e C++17 \u6765\u8bf4\uff0c\u975e const \u9759\u6001\u6210\u5458\u4e5f\u53ef\u4ee5\u5728\u7c7b\u5b9a\u4e49\u4e2d\u5185\u8054\u5730\u521d\u59cb\u5316\uff1a 1 2 3 4 5 class Whatever { public : static inline int s_value { 4 }; // a static inline int can be declared and initialized directly (C++17) }; \u9759\u6001\u6210\u5458\u53d8\u91cf\u6848\u4f8b \u4e3a\u4ec0\u4e48\u6211\u4eec\u8981\u5728\u7c7b\u4e2d\u4f7f\u7528\u9759\u6001\u53d8\u91cf\u5462\uff1f\u4e00\u4e2a\u6bd4\u8f83\u6709\u7528\u7684\u4f8b\u5b50\u662f\u4e3a\u7c7b\u7684\u6bcf\u4e2a\u5b9e\u4f8b\u8bbe\u7f6e\u4e00\u4e2a\u552f\u4e00\u7684ID\uff0c\u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include class Something { private : static inline int s_idGenerator { 1 }; // C++17 // static int s_idGenerator; // \u5728 C++14 \u6216\u4e4b\u524d\u7684\u7248\u672c\u4e2d\u4f7f\u7528\u8fd9\u4e00\u884c\u4ee3\u7801 int m_id { }; public : Something () : m_id { s_idGenerator ++ } // \u4ece id \u751f\u6210\u5668\u83b7\u53d6\u4e0b\u4e00\u4e2a id {} int getID () const { return m_id ; } }; // For C++14 or older, we have to initialize the non-const static member outside the class definition // Note that we're defining and initializing s_idGenerator even though it is declared as private above. // This is okay since the definition isn't subject to access controls. // int Something::s_idGenerator { 1 }; // start our ID generator with value 1 (uncomment for C++14 or older) int main () { Something first ; Something second ; Something third ; std :: cout << first . getID () << '\\n' ; std :: cout << second . getID () << '\\n' ; std :: cout << third . getID () << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 1 2 3 \u56e0\u4e3a s_idGenerator \u7531\u6240\u6709 Something \u5bf9\u8c61\u5171\u4eab\uff0c\u6240\u4ee5\u5f53\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Something \u5bf9\u8c61\u65f6\uff0c\u6784\u9020\u51fd\u6570\u4ece s_idGenerator \u4e2d\u83b7\u53d6\u5f53\u524d\u503c\uff0c\u5c06\u5176\u9012\u589e\u540e\u4f5c\u4e3a\u4e0b\u4e00\u4e2a\u5bf9\u8c61\u7684ID\u3002\u8fd9\u4fdd\u8bc1\u4e86\u6bcf\u4e2a\u5b9e\u4f8b\u5316\u7684 Something \u5bf9\u8c61\u53ef\u4ee5\u83b7\u53d6\u552f\u4e00\u7684id(\u6309\u521b\u5efa\u987a\u5e8f\u9012\u589e)\u3002\u8fd9\u5728\u8c03\u8bd5\u6570\u7ec4\u4e2d\u7684\u591a\u4e2a\u9879\u65f6\u975e\u5e38\u6709\u5e2e\u52a9\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u6cd5\u6765\u533a\u5206\u5177\u6709\u76f8\u540c\u7c7b\u7c7b\u578b\u7684\u591a\u4e2a\u5bf9\u8c61! \u5f53\u7c7b\u9700\u8981\u521b\u5efa\u7c7b\u5185\u90e8\u7684\u67e5\u627e\u8868(\u4f8b\u5982\uff0c\u7528\u4e8e\u5b58\u50a8\u4e00\u7ec4\u9884\u8ba1\u7b97\u503c\u7684\u6570\u7ec4)\u65f6\uff0c\u9759\u6001\u6210\u5458\u53d8\u91cf\u4e5f\u5f88\u6709\u7528\u3002\u901a\u8fc7\u5c06\u67e5\u627e\u8868\u8bbe\u7f6e\u4e3a\u9759\u6001\u7684\uff0c\u6240\u6709\u5bf9\u8c61\u53ea\u5b58\u5728\u4e00\u4e2a\u526f\u672c\uff0c\u800c\u4e0d\u662f\u4e3a\u6bcf\u4e2a\u5b9e\u4f8b\u5316\u7684\u5bf9\u8c61\u90fd\u521b\u5efa\u4e00\u4e2a\u526f\u672c\u3002\u8fd9\u53ef\u4ee5\u8282\u7701\u5927\u91cf\u7684\u5185\u5b58\u3002","title":"13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf"},{"location":"13-13-static-member-variables/#static","text":"\u5728 6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf \u4e2d\u6211\u4eec\u5b66\u4e60\u4e86 static \u5173\u952e\u5b57\uff0c\u5b83\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u5e76\u786e\u4fdd\u5b83\u4e0d\u4f1a\u5728 \u79bb\u5f00\u4f5c\u7528\u57df \u540e\u88ab\u9500\u6bc1\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int generateID () { static int s_id { 0 }; return ++ s_id ; } int main () { std :: cout << generateID () << '\\n' ; std :: cout << generateID () << '\\n' ; std :: cout << generateID () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 1 2 3 \u6ce8\u610f\uff0c s_id \u7684\u503c\u5728\u591a\u6b21\u51fd\u6570\u8c03\u7528\u95f4\u5f97\u4ee5\u4fdd\u7559\u3002 static \u5173\u952e\u5b57\u5728\u7528\u4e8e \u5168\u5c40\u53d8\u91cf \u65f6\uff0c\u8fd8\u6709\u53e6\u5916\u7684\u542b\u4e49\u2014\u2014 \u5b83\u4f1a\u8d4b\u4e88\u8be5\u53d8\u91cf \u5185\u90e8\u94fe\u63a5 \u5c5e\u6027\uff08\u4e0d\u80fd\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u6587\u4ef6\u5916\u4f7f\u7528\uff09\u3002\u56e0\u4e3a\u5168\u5c40\u53d8\u91cf\u5e94\u8be5\u88ab\u675c\u7edd\uff0c\u6240\u4ee5 static \u7684\u8fd9\u65b9\u9762\u5e94\u7528\u5e76\u4e0d\u5e38\u89c1\u3002","title":"\u91cd\u6e29 static \u5173\u952e\u5b57"},{"location":"13-13-static-member-variables/#_1","text":"static \u5173\u952e\u5b57\u5728\u5e94\u7528\u4e8e\u7c7b\u6210\u5458\u65f6\uff0c\u6709\u4e24\u4e2a\u989d\u5916\u7684\u7528\u9014\uff1a \u9759\u6001\u6210\u5458\u53d8\u91cf \u548c \u9759\u6001\u6210\u5458\u51fd\u6570 \u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e24\u79cd static \u7528\u6cd5\u90fd\u975e\u5e38\u7b80\u5355\u76f4\u63a5\u3002\u6211\u4eec\u4f1a\u5728\u672c\u7ae0\u4ecb\u7ecd\u9759\u6001\u6210\u5458\u53d8\u91cf\uff0c\u7136\u540e\u5728\u4e0b\u4e00\u7ae0\u4ecb\u7ecd\u9759\u6001\u6210\u5458\u51fd\u6570\u3002 \u5728\u5f00\u59cb\u5b66\u4e60\u4e3a\u6210\u5458\u53d8\u91cf\u6dfb\u52a0 static \u5173\u952e\u5b57\u4e4b\u524d\uff0c\u8bf7\u5148\u8003\u8651\u4e0b\u9762\u7684\u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include class Something { public : int m_value { 1 }; }; int main () { Something first ; Something second ; first . m_value = 2 ; std :: cout << first . m_value << '\\n' ; std :: cout << second . m_value << '\\n' ; return 0 ; } \u5f53\u6211\u4eec\u5b9e\u4f8b\u5316\u5bf9\u8c61\u7684\u65f6\u5019\uff0c\u5bf9\u8c61\u5404\u81ea\u521b\u5efa\u5176\u6210\u5458\u51fd\u6570\u7684\u4e00\u4efd\u62f7\u8d1d\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a Something \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u6700\u7ec8\u6211\u4eec\u4f1a\u5f97\u5230\u4e24\u4efd m_value : first.m_value \u548c second.m_value \u3002\u5b83\u4eec\u4e24\u4e2a\u662f\u5404\u81ea\u72ec\u7acb\u7684\uff0c\u56e0\u6b64\u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 2 1 \u4f7f\u7528 static \u5173\u952e\u5b57\u53ef\u4ee5\u521b\u5efa\u9759\u6001\u6210\u5458\u53d8\u91cf\u3002\u548c\u666e\u901a\u6210\u5458\u53d8\u91cf\u4e0d\u540c\u7684\u662f\uff0c\u9759\u6001\u6210\u5458\u53d8\u91cf\u5728\u540c\u4e00\u4e2a\u7c7b\u7684\u5bf9\u8c61\u95f4\u662f\u5171\u4eab\u7684\u3002\u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include class Something { public : static int s_value ; }; int Something :: s_value { 1 }; int main () { Something first ; Something second ; first . s_value = 2 ; std :: cout << first . s_value << '\\n' ; std :: cout << second . s_value << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 2 2 \u56e0\u4e3a s_value \u662f\u9759\u6001\u6210\u5458\u53d8\u91cf\uff0c\u6240\u4ee5 s_value \u5728\u5404\u4e2a\u5bf9\u8c61\u95f4\u662f\u5171\u4eab\u7684\u3002\u5176\u7ed3\u679c\u5c31\u662f\uff0c first.s_value is \u4e0e second.s_value \u5b9e\u9645\u4e0a\u662f\u540c\u4e00\u4e2a\u3002\u56e0\u6b64\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 first \u8bbe\u7f6e s_value \u7684\u503c\uff0c\u5e76\u901a\u8fc7 second \u8bbf\u95ee\u5b83\u3002","title":"\u9759\u6001\u6210\u5458\u53d8\u91cf"},{"location":"13-13-static-member-variables/#_2","text":"\u5c3d\u7ba1\u4f60\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8c61\u6765\u8bbf\u95ee\u9759\u6001\u6210\u5458(\u4f8b\u5982\uff1a first.s_value \u548c second.s_value )\uff0c\u4f46\u5b9e\u9645\u4e0a\u8fd9\u4e9b\u9759\u6001\u6210\u5458\u5728\u5bf9\u8c61\u88ab\u5b9e\u4f8b\u5316\u524d\u5c31\u5b58\u5728\u4e86\u3002\u5b83\u4eec\u66f4\u50cf\u662f\u5168\u5c40\u53d8\u91cf\uff0c\u4f1a\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u521b\u5efa\uff0c\u5728\u7a0b\u5e8f\u9000\u51fa\u65f6\u9500\u6bc1\u3002 \u56e0\u6b64\uff0c\u6700\u597d\u8ba4\u4e3a\u9759\u6001\u6210\u5458\u662f\u5c5e\u4e8e\u7c7b\u672c\u8eab\u7684\uff0c\u800c\u4e0d\u662f\u7c7b\u7684\u67d0\u4e2a\u5b9e\u4f8b\u5bf9\u8c61\u3002\u56e0\u4e3a s_value \u72ec\u7acb\u4e8e\u4efb\u4f55\u7c7b\u5bf9\u8c61\u800c\u5b58\u5728\uff0c\u6240\u4ee5\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7c7b\u540d\u548c \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 (\u5728\u672c\u4f8b\u4e2d\u4e3a Something::s_value )\u8bbf\u95ee\u5b83: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include class Something { public : static int s_value ; // \u58f0\u660e\u9759\u6001\u6210\u5458\u53d8\u91cf }; int Something :: s_value { 1 }; // \u5b9a\u4e49\u9759\u6001\u6210\u5458\u53d8\u91cf int main () { // \u6ce8\u610f\uff1a\u6ca1\u6709\u5b9e\u4f8b\u5316\u4efb\u4f55\u5bf9\u8c61 Something :: s_value = 2 ; std :: cout << Something :: s_value << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c s_value \u662f\u4f7f\u7528\u7c7b\u540d\u8fdb\u884c\u8bbf\u95ee\u7684\uff0c\u800c\u6ca1\u6709\u901a\u8fc7\u4efb\u4f55\u8be5\u7c7b\u7684\u53d8\u91cf\u53bb\u8bbf\u95ee\u3002\u6211\u4eec\u751a\u81f3\u90fd\u8fd8\u6ca1\u6709\u5b9e\u4f8b\u5316\u4efb\u4f55\u8be5\u7c7b\u7684\u5bf9\u8c61\uff0c\u4f46\u662f\u4ecd\u7136\u53ef\u4ee5\u901a\u8fc7 Something::s_value \u6765\u8bbf\u95ee\u5b83\u3002\u8fd9\u79cd\u65b9\u5f0f\u662f\u66f4\u4e3a\u63a8\u8350\u7684\u4f7f\u7528\u9759\u6001\u6210\u5458\u7684\u65b9\u6cd5\u3002 \u6700\u4f73\u5b9e\u8df5 \u901a\u8fc7\u7c7b\u540d\uff08\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\uff09\u6765\u8bbf\u95ee\u9759\u6001\u6210\u5458\u53d8\u91cf\u800c\u4e0d\u662f\u901a\u8fc7\u5bf9\u8c61\u6765\u8bbf\u95ee\uff08\u4f7f\u7528\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26\uff09\u3002","title":"\u9759\u6001\u6210\u5458\u53d8\u91cf\u5e76\u4e0d\u548c\u7c7b\u5bf9\u8c61\u5173\u8054"},{"location":"13-13-static-member-variables/#_3","text":"\u5f53\u6211\u4eec\u5728\u7c7b\u5185\u90e8 \u58f0\u660e \u9759\u6001\u6210\u5458\u53d8\u91cf\u65f6\uff0c\u6211\u4eec\u662f\u5728\u544a\u8bc9\u7f16\u8bd1\u5668\u9759\u6001\u6210\u5458\u53d8\u91cf\u7684\u5b58\u5728\uff0c\u800c\u4e0d\u662f\u771f\u6b63\u5b9a\u4e49\u5b83(\u7c7b\u4f3c \u524d\u5411\u58f0\u660e )\u3002\u56e0\u4e3a\u9759\u6001\u6210\u5458\u53d8\u91cf\u4e0d\u662f\u5355\u4e2a\u7c7b\u5bf9\u8c61\u7684\u4e00\u90e8\u5206(\u5b83\u4eec\u4e0e\u5168\u5c40\u53d8\u91cf\u5904\u7406\u76f8\u4f3c\uff0c\u5e76\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u521d\u59cb\u5316)\uff0c\u6240\u4ee5\u5fc5\u987b\u5728\u7c7b\u5916\u90e8\u7684\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u663e\u5f0f\u5730\u5b9a\u4e49\u9759\u6001\u6210\u5458\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u8fd9\u4e00\u884c\u6765\u5b9e\u73b0\u9759\u6001\u6210\u5458\u7684\u5b9a\u4e49 1 int Something :: s_value { 1 }; // \u5b9a\u4e49\u9759\u6001\u6210\u5458\u53d8\u91cf \u8fd9\u4e00\u884c\u6709\u4e24\u4e2a\u76ee\u7684\uff1a\u5b9e\u4f8b\u5316\u9759\u6001\u6210\u5458\u53d8\u91cf(\u5c31\u50cf\u5168\u5c40\u53d8\u91cf\u4e00\u6837)\uff0c\u5e76\u53ef\u9009\u5730\u521d\u59cb\u5316\u5b83\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u63d0\u4f9b\u4e86\u521d\u59cb\u5316\u503c1\u3002\u5982\u679c\u6ca1\u6709\u63d0\u4f9b\u521d\u59cb\u5316\u503c\uff0cC++\u5c06\u8be5\u503c\u521d\u59cb\u5316\u4e3a0\u3002 \u6ce8\u610f\uff0c\u9759\u6001\u6210\u5458\u7684 \u5b9a\u4e49 \u4e0d\u53d7 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u7684\u9650\u5236\uff0c\u60a8\u53ef\u4ee5\u5b9a\u4e49\u548c\u521d\u59cb\u5316\u53d8\u91cf\uff0c\u5373\u4f7f\u5b83\u5728\u7c7b\u4e2d\u58f0\u660e\u4e3aprivate(\u6216protected)\u3002 \u5982\u679c\u7c7b\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u9759\u6001\u6210\u5458\u5b9a\u4e49\u901a\u5e38\u653e\u5728\u7c7b\u7684\u76f8\u5173\u4ee3\u7801\u6587\u4ef6\u4e2d(\u4f8b\u5982 Something.cpp )\u3002\u5982\u679c\u7c7b\u5b9a\u4e49\u5728 .cpp \u6587\u4ef6\u4e2d\uff0c\u5219\u9759\u6001\u6210\u5458\u5b9a\u4e49\u901a\u5e38\u76f4\u63a5\u653e\u5728\u7c7b\u7684\u4e0b\u9762\u3002\u4e0d\u8981\u5c06\u9759\u6001\u6210\u5458\u5b9a\u4e49\u653e\u5728\u5934\u6587\u4ef6\u4e2d(\u5f88\u50cf\u5168\u5c40\u53d8\u91cf\uff0c\u5982\u679c\u5934\u6587\u4ef6\u88ab\u5305\u542b\u4e0d\u6b62\u4e00\u6b21\uff0c\u6700\u7ec8\u5c06\u5f97\u5230\u591a\u4e2a\u5b9a\u4e49\uff0c\u8fd9\u5c06\u5bfc\u81f4\u94fe\u63a5\u5668\u9519\u8bef)\u3002","title":"\u5b9a\u4e49\u548c\u521d\u59cb\u5316\u9759\u6001\u6210\u5458\u53d8\u91cf"},{"location":"13-13-static-member-variables/#_4","text":"\u6709\u4e00\u4e9b\u6377\u5f84\u53ef\u4ee5\u5b9e\u73b0\u4e0a\u8ff0\u76ee\u6807\u3002\u9996\u5148\uff0c\u5f53\u9759\u6001\u6210\u5458\u662fconst\u6574\u578b\u7c7b\u578b(\u5305\u62ecchar\u548cbool)\u6216const enum\u65f6\uff0c\u53ef\u4ee5\u5728\u7c7b\u5b9a\u4e49\u4e2d\u521d\u59cb\u5316\u9759\u6001\u6210\u5458: 1 2 3 4 5 class Whatever { public : static const int s_value { 4 }; // a static const int can be declared and initialized directly }; \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u9759\u6001\u6210\u5458\u53d8\u91cf\u662f const \u7684\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u663e\u5f0f\u5730\u5b9a\u4e49\u5b83\u3002 \u5176\u6b21\uff0c static constexpr \u6210\u5458\u53ef\u4ee5\u5728\u7c7b\u5b9a\u4e49\u4e2d\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 #include class Whatever { public : static constexpr double s_value { 2.2 }; // ok static constexpr std :: array < int , 3 > s_array { 1 , 2 , 3 }; // this even works for classes that support constexpr initialization }; \u6700\u540e\uff0c\u5bf9\u4e8e C++17 \u6765\u8bf4\uff0c\u975e const \u9759\u6001\u6210\u5458\u4e5f\u53ef\u4ee5\u5728\u7c7b\u5b9a\u4e49\u4e2d\u5185\u8054\u5730\u521d\u59cb\u5316\uff1a 1 2 3 4 5 class Whatever { public : static inline int s_value { 4 }; // a static inline int can be declared and initialized directly (C++17) };","title":"\u9759\u6001\u6210\u5458\u53d8\u91cf\u7684\u5185\u8054\u521d\u59cb\u5316"},{"location":"13-13-static-member-variables/#_5","text":"\u4e3a\u4ec0\u4e48\u6211\u4eec\u8981\u5728\u7c7b\u4e2d\u4f7f\u7528\u9759\u6001\u53d8\u91cf\u5462\uff1f\u4e00\u4e2a\u6bd4\u8f83\u6709\u7528\u7684\u4f8b\u5b50\u662f\u4e3a\u7c7b\u7684\u6bcf\u4e2a\u5b9e\u4f8b\u8bbe\u7f6e\u4e00\u4e2a\u552f\u4e00\u7684ID\uff0c\u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include class Something { private : static inline int s_idGenerator { 1 }; // C++17 // static int s_idGenerator; // \u5728 C++14 \u6216\u4e4b\u524d\u7684\u7248\u672c\u4e2d\u4f7f\u7528\u8fd9\u4e00\u884c\u4ee3\u7801 int m_id { }; public : Something () : m_id { s_idGenerator ++ } // \u4ece id \u751f\u6210\u5668\u83b7\u53d6\u4e0b\u4e00\u4e2a id {} int getID () const { return m_id ; } }; // For C++14 or older, we have to initialize the non-const static member outside the class definition // Note that we're defining and initializing s_idGenerator even though it is declared as private above. // This is okay since the definition isn't subject to access controls. // int Something::s_idGenerator { 1 }; // start our ID generator with value 1 (uncomment for C++14 or older) int main () { Something first ; Something second ; Something third ; std :: cout << first . getID () << '\\n' ; std :: cout << second . getID () << '\\n' ; std :: cout << third . getID () << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 1 2 3 \u56e0\u4e3a s_idGenerator \u7531\u6240\u6709 Something \u5bf9\u8c61\u5171\u4eab\uff0c\u6240\u4ee5\u5f53\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Something \u5bf9\u8c61\u65f6\uff0c\u6784\u9020\u51fd\u6570\u4ece s_idGenerator \u4e2d\u83b7\u53d6\u5f53\u524d\u503c\uff0c\u5c06\u5176\u9012\u589e\u540e\u4f5c\u4e3a\u4e0b\u4e00\u4e2a\u5bf9\u8c61\u7684ID\u3002\u8fd9\u4fdd\u8bc1\u4e86\u6bcf\u4e2a\u5b9e\u4f8b\u5316\u7684 Something \u5bf9\u8c61\u53ef\u4ee5\u83b7\u53d6\u552f\u4e00\u7684id(\u6309\u521b\u5efa\u987a\u5e8f\u9012\u589e)\u3002\u8fd9\u5728\u8c03\u8bd5\u6570\u7ec4\u4e2d\u7684\u591a\u4e2a\u9879\u65f6\u975e\u5e38\u6709\u5e2e\u52a9\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u6cd5\u6765\u533a\u5206\u5177\u6709\u76f8\u540c\u7c7b\u7c7b\u578b\u7684\u591a\u4e2a\u5bf9\u8c61! \u5f53\u7c7b\u9700\u8981\u521b\u5efa\u7c7b\u5185\u90e8\u7684\u67e5\u627e\u8868(\u4f8b\u5982\uff0c\u7528\u4e8e\u5b58\u50a8\u4e00\u7ec4\u9884\u8ba1\u7b97\u503c\u7684\u6570\u7ec4)\u65f6\uff0c\u9759\u6001\u6210\u5458\u53d8\u91cf\u4e5f\u5f88\u6709\u7528\u3002\u901a\u8fc7\u5c06\u67e5\u627e\u8868\u8bbe\u7f6e\u4e3a\u9759\u6001\u7684\uff0c\u6240\u6709\u5bf9\u8c61\u53ea\u5b58\u5728\u4e00\u4e2a\u526f\u672c\uff0c\u800c\u4e0d\u662f\u4e3a\u6bcf\u4e2a\u5b9e\u4f8b\u5316\u7684\u5bf9\u8c61\u90fd\u521b\u5efa\u4e00\u4e2a\u526f\u672c\u3002\u8fd9\u53ef\u4ee5\u8282\u7701\u5927\u91cf\u7684\u5185\u5b58\u3002","title":"\u9759\u6001\u6210\u5458\u53d8\u91cf\u6848\u4f8b"},{"location":"13-14-static-member-functions/","tags":["class","static membe function"],"text":"\u9759\u6001\u6210\u5458\u51fd\u6570 \u5728\u4e0a\u4e00\u8282\u8bfe 13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf \u4e2d\uff0c\u6211\u4eec\u77e5\u9053\u4e86 \u9759\u6001\u6210\u5458\u53d8\u91cf \u662f\u5c5e\u4e8e\u7c7b\u800c\u4e0d\u662f\u7c7b\u5bf9\u8c61\u7684\u3002\u5982\u679c\u9759\u6001\u6210\u5458\u53d8\u91cf\u662f \u516c\u6709\u6210\u5458 \u7684\u8bdd\u6211\u4eec\u81ea\u7136\u53ef\u4ee5\u901a\u8fc7\u7c7b\u540d \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u6765\u8bbf\u95ee\u5b83\uff0c\u4f46\u662f\u5982\u679c\u5b83\u662f \u79c1\u6709\u6210\u5458 \u7684\u8bdd\uff0c\u6211\u4eec\u5e94\u8be5\u5982\u4f55\u8bbf\u95ee\u5b83\u5462\uff1f\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 class Something { private : static int s_value ; }; int Something :: s_value { 1 }; // \u521d\u59cb\u5316\u3002\u5373\u4f7f s_value \u662f\u79c1\u6709\u7684\u4e5f\u53ef\u4ee5\u8fd9\u6837\u505a\uff0c\u56e0\u4e3a\u6b64\u5904\u662f\u5728\u201c\u5b9a\u4e49\u201d int main () { // \u5e94\u8be5\u5982\u4f55\u8bbf\u95ee\u79c1\u6709\u6210\u5458 Something::s_value \u5462\uff1f } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4e0d\u80fd\u5728 main() \u51fd\u6570\u4e2d\u76f4\u63a5\u8bbf\u95ee Something::s_value \uff0c\u56e0\u4e3a\u5b83\u662f\u79c1\u6709\u6210\u5458\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u79c1\u6709\u6210\u5458\u53d8\u91cf\u9700\u8981\u901a\u8fc7\u516c\u6709\u6210\u5458\u51fd\u6570\u6765\u8bbf\u95ee\u3002\u5c3d\u7ba1\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u666e\u901a\u7684\u516c\u6709\u6210\u5458\u51fd\u6570\u6765\u8bbf\u95ee s_value \uff0c\u4f46\u5f88\u663e\u7136\u6211\u4eec\u5fc5\u987b\u5b9e\u4f8b\u5316\u4e00\u4e2a\u5bf9\u8c61\u624d\u80fd\u591f\u8c03\u7528\u8be5\u6210\u5458\u51fd\u6570\uff01\u66f4\u597d\u7684\u529e\u6cd5\u662f\u5c06\u8be5\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u4e3a\u9759\u6001\u7684\u3002 \u548c\u9759\u6001\u6210\u5458\u51fd\u6570\u4e00\u6837\uff0c\u9759\u6001\u6210\u5458\u51fd\u6570\u4e0d\u5c5e\u4e8e\u4efb\u4f55\u5bf9\u8c61\u3002\u4f7f\u7528\u9759\u6001\u6210\u5458\u51fd\u6570\u6765\u8bbf\u95ee s_value \u7684\u65b9\u6cd5\u89c1\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include class Something { private : static int s_value ; public : static int getValue () { return s_value ; } // static member function }; int Something :: s_value { 1 }; // initializer int main () { std :: cout << Something :: getValue () << '\\n' ; } \u56e0\u4e3a\u9759\u6001\u6210\u5458\u51fd\u6570\u4e0d\u5c5e\u4e8e\u4efb\u4f55\u7279\u5b9a\u5bf9\u8c61\uff0c\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528\u7c7b\u540d\u548c\u4f5c\u7528\u57df\u89e3\u6790\u64cd\u4f5c\u7b26\u76f4\u63a5\u8c03\u7528\u5b83\u4eec\u3002\u4e0e\u9759\u6001\u6210\u5458\u53d8\u91cf\u4e00\u6837\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8c61\u8c03\u7528\u5b83\u4eec\uff0c\u4f46\u4e0d\u5efa\u8bae\u8fd9\u6837\u505a\u3002 \u9759\u6001\u6210\u5458\u53d8\u91cf\u6ca1\u6709 *this \u6307\u9488 \u9759\u6001\u6210\u5458\u51fd\u6570\u6709\u4e24\u4e2a\u6709\u8da3\u7684\u7279\u6027\u503c\u5f97\u6ce8\u610f\u3002\u9996\u5148\uff0c\u56e0\u4e3a\u9759\u6001\u6210\u5458\u51fd\u6570\u4e0d\u5c5e\u4e8e\u67d0\u4e2a\u5bf9\u8c61\uff0c\u6240\u4ee5\u5b83\u4eec\u6ca1\u6709 this \u6307\u9488\uff01\u4ed4\u7ec6\u60f3\u60f3\uff0c\u8fd9\u662f\u6709\u9053\u7406\u7684\u2014\u2014 this \u6307\u9488\u603b\u662f\u6307\u5411\u8be5\u6210\u5458\u51fd\u6570\u6b63\u5728\u64cd\u4f5c\u7684\u5bf9\u8c61\u3002\u9759\u6001\u6210\u5458\u51fd\u6570\u4e0d\u80fd\u4f5c\u7528\u4e8e\u5bf9\u8c61\uff0c\u56e0\u6b64\u4e0d\u9700\u8981 this \u6307\u9488\u3002 \u5176\u6b21\uff0c\u9759\u6001\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5176\u4ed6\u9759\u6001\u6210\u5458(\u53d8\u91cf\u6216\u51fd\u6570)\uff0c\u4f46\u4e0d\u80fd\u8bbf\u95ee\u975e\u9759\u6001\u6210\u5458\u3002\u8fd9\u662f\u56e0\u4e3a\u975e\u9759\u6001\u6210\u5458\u5fc5\u987b\u5c5e\u4e8e\u7c7b\u5bf9\u8c61\uff0c\u800c\u9759\u6001\u6210\u5458\u51fd\u6570\u6ca1\u6709\u53ef\u4f7f\u7528\u7684\u7c7b\u5bf9\u8c61! \u53c8\u4e00\u4e2a\u4f8b\u5b50 \u9759\u6001\u6210\u5458\u51fd\u6570\u4e5f\u53ef\u4ee5\u5728\u7c7b\u58f0\u660e\u4e4b\u5916\u5b9a\u4e49\u3002\u8fd9\u4e0e\u666e\u901a\u6210\u5458\u51fd\u6570\u7684\u5de5\u4f5c\u65b9\u5f0f\u76f8\u540c\u3002 \u4e00\u4e2a\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include class IDGenerator { private : static int s_nextID ; // \u9759\u6001\u6210\u5458\u7684\u58f0\u660e public : static int getNextID (); // \u9759\u6001\u51fd\u6570\u7684\u58f0\u660e }; // \u5728\u7c7b\u5916\u5b9a\u4e49\u9759\u6001\u6210\u5458\u3002\u6ce8\u610f\uff0c\u6211\u4eec\u5728\u6b64\u5904\u6ca1\u6709\u4f7f\u7528 static \u5173\u952e\u5b57\u3002 // ID \u4ece 1 \u5f00\u59cb int IDGenerator :: s_nextID { 1 }; // \u7c7b\u5916\u5b9a\u4e49\u9759\u6001\u6210\u5458\u51fd\u6570\u3002\u6ce8\u610f\uff0c\u6211\u4eec\u5728\u6b64\u5904\u6ca1\u6709\u4f7f\u7528 static \u5173\u952e\u5b57\u3002 int IDGenerator::getNextID () { return s_nextID ++ ; } int main () { for ( int count { 0 }; count < 5 ; ++ count ) std :: cout << \"The next ID is: \" << IDGenerator :: getNextID () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 4 5 The next ID is: 1 The next ID is: 2 The next ID is: 3 The next ID is: 4 The next ID is: 5 \u6ce8\u610f\uff0c\u56e0\u4e3a\u8be5\u7c7b\u4e2d\u7684\u6240\u6709\u6570\u636e\u548c\u51fd\u6570\u90fd\u662f\u9759\u6001\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u9700\u8981\u5b9e\u4f8b\u5316\u8be5\u7c7b\u7684\u5bf9\u8c61\u6765\u4f7f\u7528\u5b83\u7684\u529f\u80fd\uff01\u8be5\u7c7b\u5229\u7528\u4e00\u4e2a\u9759\u6001\u6210\u5458\u53d8\u91cf\u6765\u4fdd\u5b58\u4e0b\u4e00\u4e2a\u8981\u5206\u914d\u7684ID\u7684\u503c\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u9759\u6001\u6210\u5458\u51fd\u6570\u6765\u8fd4\u56de\u8be5ID\u5e76\u4f7f\u5176\u9012\u589e\u3002 \u6709\u5173\u5168\u9759\u6001\u6210\u5458\u7c7b\u7684\u4e00\u4e9b\u8b66\u793a \u5bf9\u4e8e\u4e00\u4e2a\u6240\u6709\u6210\u5458\u90fd\u662f\u9759\u6001\u6210\u5458\u7684\u7c7b\u65f6\u8981\u5c0f\u5fc3\u3002\u5c3d\u7ba1\u8fd9\u79cd\u201c\u7eaf\u9759\u6001\u7c7b\u201d(\u4e5f\u79f0\u4e3a\u201c\u5355\u6001\u7c7b\u201d)\u53ef\u80fd\u5f88\u6709\u7528\uff0c\u4f46\u5b83\u4eec\u4e5f\u6709\u4e00\u4e9b\u6f5c\u5728\u7684\u7f3a\u70b9\u3002 \u9996\u5148\uff0c\u56e0\u4e3a\u6240\u6709\u9759\u6001\u6210\u5458\u90fd\u53ea\u5b9e\u4f8b\u5316\u4e00\u6b21\uff0c\u6240\u4ee5\u4e0d\u53ef\u80fd\u6709\u7eaf\u9759\u6001\u7c7b\u7684\u591a\u4e2a\u526f\u672c(\u9664\u975e\u514b\u9686\u8be5\u7c7b\u5e76\u91cd\u547d\u540d\u5b83)\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u9700\u8981\u4e24\u4e2a\u72ec\u7acb\u7684 IDGenerator \u5bf9\u8c61\uff0c\u8fd9\u5bf9\u4e8e\u5355\u4e2a\u7eaf\u9759\u6001\u7c7b\u6765\u8bf4\u662f\u4e0d\u53ef\u80fd\u7684\u3002 \u5176\u6b21\uff0c\u5728\u5173\u4e8e\u5168\u5c40\u53d8\u91cf\u7684\u4e00\u8bfe\u4e2d\uff0c\u4f60\u5df2\u7ecf\u4e86\u89e3\u5230\u4e86\u5168\u5c40\u53d8\u91cf\u7684\u5371\u9669\u6027\uff0c\u56e0\u4e3a\u4efb\u4f55\u4e00\u6bb5\u4ee3\u7801\u90fd\u53ef\u80fd\u6539\u53d8\u5168\u5c40\u53d8\u91cf\u7684\u503c\uff0c\u5e76\u6700\u7ec8\u7834\u574f\u53e6\u4e00\u6bb5\u770b\u8d77\u6765\u4e0d\u76f8\u5173\u7684\u4ee3\u7801\u3002\u5bf9\u4e8e\u7eaf\u9759\u6001\u7c7b\u4e5f\u662f\u5982\u6b64\u3002\u56e0\u4e3a\u6240\u6709\u6210\u5458\u90fd\u5c5e\u4e8e\u7c7b(\u800c\u4e0d\u662f\u7c7b\u7684\u5bf9\u8c61)\uff0c\u800c\u4e14\u7c7b\u58f0\u660e\u901a\u5e38\u5177\u6709\u5168\u5c40\u4f5c\u7528\u57df\uff0c\u6240\u4ee5\u7eaf\u9759\u6001\u7c7b\u672c\u8d28\u4e0a\u76f8\u5f53\u4e8e\u5728\u5168\u5c40\u53ef\u8bbf\u95ee\u7684\u540d\u79f0\u7a7a\u95f4\u4e2d\u58f0\u660e\u51fd\u6570\u548c\u5168\u5c40\u53d8\u91cf\uff0c\u5e76\u5177\u6709\u5168\u5c40\u53d8\u91cf\u6240\u5177\u6709\u7684\u6240\u6709\u5fc5\u8981\u7f3a\u70b9\u3002 C++ \u4e0d\u652f\u6301\u9759\u6001\u6784\u9020\u51fd\u6570 \u65e2\u7136\u53ef\u4ee5\u901a\u8fc7\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u666e\u901a\u6210\u5458\u53d8\u91cf\uff0c\u90a3\u4e48\u4e5f\u5e94\u8be5\u80fd\u591f\u901a\u8fc7\u9759\u6001\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u9759\u6001\u6210\u5458\u53d8\u91cf\u3002\u867d\u7136\u4e00\u4e9b\u73b0\u4ee3\u8bed\u8a00\u786e\u5b9e\u51fa\u4e8e\u8fd9\u4e2a\u76ee\u7684\u6dfb\u52a0\u4e86\u5bf9\u9759\u6001\u6784\u9020\u51fd\u6570\u7684\u652f\u6301\uff0c\u4f46\u4e0d\u5e78\u7684\u662fC++\u4e0d\u662f\u5176\u4e2d\u4e4b\u4e00\u3002 \u5982\u679c\u53ef\u4ee5\u76f4\u63a5\u521d\u59cb\u5316\u9759\u6001\u53d8\u91cf\uff0c\u5219\u4e0d\u9700\u8981\u6784\u9020\u51fd\u6570\uff1a\u53ef\u4ee5\u5728\u5b9a\u4e49\u70b9\u521d\u59cb\u5316\u9759\u6001\u6210\u5458\u53d8\u91cf(\u5373\u4f7f\u5b83\u662f\u79c1\u6709\u7684)\u3002\u6211\u4eec\u5728\u4e0a\u9762\u7684IDGenerator\u793a\u4f8b\u4e2d\u6b63\u662f\u8fd9\u6837\u505a\u7684\u3002\u8fd9\u662f\u53e6\u4e00\u4e2a\u4f8b\u5b50: 1 2 3 4 5 6 7 class MyClass { public : static std :: vector < char > s_mychars ; }; std :: vector < char > MyClass :: s_mychars { 'a' , 'e' , 'i' , 'o' , 'u' }; // \u5728\u5b9a\u4e49\u65f6\u521d\u59cb\u5316\u9759\u6001\u53d8\u91cf \u5982\u679c\u9700\u8981\u6267\u884c\u4ee3\u7801(\u4f8b\u5982\u5faa\u73af)\u624d\u80fd\u521d\u59cb\u5316\u9759\u6001\u6210\u5458\u53d8\u91cf\u7684\u8bdd\uff0c\u6709\u8bb8\u591a\u4e0d\u540c\u7684\u3001\u7b28\u62d9\u7684\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u6709\u4e00\u79cd\u5904\u7406\u65b9\u6cd5\u9002\u7528\u4e8e\u6240\u6709\u53d8\u91cf(\u9759\u6001\u6216\u975e\u9759\u6001)\uff0c\u5373\u4f7f\u7528lambda\u51fd\u6570\u5e76\u7acb\u5373\u8c03\u7528\u5b83\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class MyClass { public : static std :: vector < char > s_mychars ; }; std :: vector < char > MyClass :: s_mychars { []{ // The parameter list of lambdas without parameters can be omitted. // Inside the lambda we can declare another vector and use a loop. std :: vector < char > v {}; for ( char ch { 'a' }; ch <= 'z' ; ++ ch ) { v . push_back ( ch ); } return v ; }() // Call the lambda right away }; \u4e0b\u9762\u7684\u4ee3\u7801\u7ed9\u51fa\u4e86\u4e00\u4e2a\u884c\u4e3a\u66f4\u50cf\u5e38\u89c4\u6784\u9020\u51fd\u6570\u7684\u65b9\u6cd5\u3002\u4f46\u662f\uff0c\u60a8\u53ef\u80fd\u6c38\u8fdc\u90fd\u4e0d\u9700\u8981\u5b83\uff0c\u6240\u4ee5\u5982\u679c\u60a8\u613f\u610f\uff0c\u53ef\u4ee5\u968f\u610f\u8df3\u8fc7\u672c\u8282\u7684\u5176\u4f59\u90e8\u5206\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class MyClass { public : static std :: vector < char > s_mychars ; class init_static // we're defining a nested class named init_static { public : init_static () // the init constructor will initialize our static variable { for ( char ch { 'a' }; ch <= 'z' ; ++ ch ) { s_mychars . push_back ( ch ); } } } ; private : static init_static s_initializer ; // we'll use this static object to ensure the init_static constructor is called }; std :: vector < char > MyClass :: s_mychars {}; // define our static member variable MyClass :: init_static MyClass :: s_initializer {}; // define our static initializer, which will call the init_static constructor, which will initialize s_mychars \u5f53\u5b9a\u4e49\u9759\u6001\u6210\u5458 s_initializer \u65f6\uff0c\u5c06\u8c03\u7528 init_static() \u9ed8\u8ba4\u6784\u9020\u51fd\u6570(\u56e0\u4e3a s_initializer \u662f init_static \u7c7b\u578b)\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u4efb\u4f55\u9759\u6001\u6210\u5458\u53d8\u91cf\u3002\u8fd9\u4e2a\u89e3\u51b3\u65b9\u6848\u7684\u4f18\u70b9\u662f\uff0c\u6240\u6709\u521d\u59cb\u5316\u4ee3\u7801\u90fd\u9690\u85cf\u5728\u5e26\u6709\u9759\u6001\u6210\u5458\u7684\u539f\u59cb\u7c7b\u4e2d\u3002 \u5c0f\u7ed3 \u9759\u6001\u6210\u5458\u51fd\u6570\u53ef\u7528\u4e8e\u914d\u5408\u7c7b\u4e2d\u7684\u9759\u6001\u6210\u5458\u53d8\u91cf\u6765\u5de5\u4f5c\u3002\u7c7b\u5bf9\u8c61\u4e0d\u9700\u8981\u8c03\u7528\u5b83\u4eec\u3002 \u4e00\u4e2a\u7c7b\u7684\u6240\u6709\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u90fd\u662f\u9759\u6001\u7684\u3002\u7136\u800c\uff0c\u6b64\u7c7b\u7c7b\u672c\u8d28\u4e0a\u76f8\u5f53\u4e8e\u5728\u5168\u5c40\u53ef\u8bbf\u95ee\u7684\u540d\u79f0\u7a7a\u95f4\u4e2d\u58f0\u660e\u51fd\u6570\u548c\u5168\u5c40\u53d8\u91cf\uff0c\u901a\u5e38\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u5b83\u4eec\uff0c\u9664\u975e\u6709\u7279\u522b\u597d\u7684\u7406\u7531\u4f7f\u7528\u5b83\u4eec\u3002","title":"13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570"},{"location":"13-14-static-member-functions/#_1","text":"\u5728\u4e0a\u4e00\u8282\u8bfe 13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf \u4e2d\uff0c\u6211\u4eec\u77e5\u9053\u4e86 \u9759\u6001\u6210\u5458\u53d8\u91cf \u662f\u5c5e\u4e8e\u7c7b\u800c\u4e0d\u662f\u7c7b\u5bf9\u8c61\u7684\u3002\u5982\u679c\u9759\u6001\u6210\u5458\u53d8\u91cf\u662f \u516c\u6709\u6210\u5458 \u7684\u8bdd\u6211\u4eec\u81ea\u7136\u53ef\u4ee5\u901a\u8fc7\u7c7b\u540d \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u6765\u8bbf\u95ee\u5b83\uff0c\u4f46\u662f\u5982\u679c\u5b83\u662f \u79c1\u6709\u6210\u5458 \u7684\u8bdd\uff0c\u6211\u4eec\u5e94\u8be5\u5982\u4f55\u8bbf\u95ee\u5b83\u5462\uff1f\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 class Something { private : static int s_value ; }; int Something :: s_value { 1 }; // \u521d\u59cb\u5316\u3002\u5373\u4f7f s_value \u662f\u79c1\u6709\u7684\u4e5f\u53ef\u4ee5\u8fd9\u6837\u505a\uff0c\u56e0\u4e3a\u6b64\u5904\u662f\u5728\u201c\u5b9a\u4e49\u201d int main () { // \u5e94\u8be5\u5982\u4f55\u8bbf\u95ee\u79c1\u6709\u6210\u5458 Something::s_value \u5462\uff1f } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4e0d\u80fd\u5728 main() \u51fd\u6570\u4e2d\u76f4\u63a5\u8bbf\u95ee Something::s_value \uff0c\u56e0\u4e3a\u5b83\u662f\u79c1\u6709\u6210\u5458\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u79c1\u6709\u6210\u5458\u53d8\u91cf\u9700\u8981\u901a\u8fc7\u516c\u6709\u6210\u5458\u51fd\u6570\u6765\u8bbf\u95ee\u3002\u5c3d\u7ba1\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u666e\u901a\u7684\u516c\u6709\u6210\u5458\u51fd\u6570\u6765\u8bbf\u95ee s_value \uff0c\u4f46\u5f88\u663e\u7136\u6211\u4eec\u5fc5\u987b\u5b9e\u4f8b\u5316\u4e00\u4e2a\u5bf9\u8c61\u624d\u80fd\u591f\u8c03\u7528\u8be5\u6210\u5458\u51fd\u6570\uff01\u66f4\u597d\u7684\u529e\u6cd5\u662f\u5c06\u8be5\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u4e3a\u9759\u6001\u7684\u3002 \u548c\u9759\u6001\u6210\u5458\u51fd\u6570\u4e00\u6837\uff0c\u9759\u6001\u6210\u5458\u51fd\u6570\u4e0d\u5c5e\u4e8e\u4efb\u4f55\u5bf9\u8c61\u3002\u4f7f\u7528\u9759\u6001\u6210\u5458\u51fd\u6570\u6765\u8bbf\u95ee s_value \u7684\u65b9\u6cd5\u89c1\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include class Something { private : static int s_value ; public : static int getValue () { return s_value ; } // static member function }; int Something :: s_value { 1 }; // initializer int main () { std :: cout << Something :: getValue () << '\\n' ; } \u56e0\u4e3a\u9759\u6001\u6210\u5458\u51fd\u6570\u4e0d\u5c5e\u4e8e\u4efb\u4f55\u7279\u5b9a\u5bf9\u8c61\uff0c\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528\u7c7b\u540d\u548c\u4f5c\u7528\u57df\u89e3\u6790\u64cd\u4f5c\u7b26\u76f4\u63a5\u8c03\u7528\u5b83\u4eec\u3002\u4e0e\u9759\u6001\u6210\u5458\u53d8\u91cf\u4e00\u6837\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8c61\u8c03\u7528\u5b83\u4eec\uff0c\u4f46\u4e0d\u5efa\u8bae\u8fd9\u6837\u505a\u3002","title":"\u9759\u6001\u6210\u5458\u51fd\u6570"},{"location":"13-14-static-member-functions/#this","text":"\u9759\u6001\u6210\u5458\u51fd\u6570\u6709\u4e24\u4e2a\u6709\u8da3\u7684\u7279\u6027\u503c\u5f97\u6ce8\u610f\u3002\u9996\u5148\uff0c\u56e0\u4e3a\u9759\u6001\u6210\u5458\u51fd\u6570\u4e0d\u5c5e\u4e8e\u67d0\u4e2a\u5bf9\u8c61\uff0c\u6240\u4ee5\u5b83\u4eec\u6ca1\u6709 this \u6307\u9488\uff01\u4ed4\u7ec6\u60f3\u60f3\uff0c\u8fd9\u662f\u6709\u9053\u7406\u7684\u2014\u2014 this \u6307\u9488\u603b\u662f\u6307\u5411\u8be5\u6210\u5458\u51fd\u6570\u6b63\u5728\u64cd\u4f5c\u7684\u5bf9\u8c61\u3002\u9759\u6001\u6210\u5458\u51fd\u6570\u4e0d\u80fd\u4f5c\u7528\u4e8e\u5bf9\u8c61\uff0c\u56e0\u6b64\u4e0d\u9700\u8981 this \u6307\u9488\u3002 \u5176\u6b21\uff0c\u9759\u6001\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5176\u4ed6\u9759\u6001\u6210\u5458(\u53d8\u91cf\u6216\u51fd\u6570)\uff0c\u4f46\u4e0d\u80fd\u8bbf\u95ee\u975e\u9759\u6001\u6210\u5458\u3002\u8fd9\u662f\u56e0\u4e3a\u975e\u9759\u6001\u6210\u5458\u5fc5\u987b\u5c5e\u4e8e\u7c7b\u5bf9\u8c61\uff0c\u800c\u9759\u6001\u6210\u5458\u51fd\u6570\u6ca1\u6709\u53ef\u4f7f\u7528\u7684\u7c7b\u5bf9\u8c61!","title":"\u9759\u6001\u6210\u5458\u53d8\u91cf\u6ca1\u6709*this\u6307\u9488"},{"location":"13-14-static-member-functions/#_2","text":"\u9759\u6001\u6210\u5458\u51fd\u6570\u4e5f\u53ef\u4ee5\u5728\u7c7b\u58f0\u660e\u4e4b\u5916\u5b9a\u4e49\u3002\u8fd9\u4e0e\u666e\u901a\u6210\u5458\u51fd\u6570\u7684\u5de5\u4f5c\u65b9\u5f0f\u76f8\u540c\u3002 \u4e00\u4e2a\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include class IDGenerator { private : static int s_nextID ; // \u9759\u6001\u6210\u5458\u7684\u58f0\u660e public : static int getNextID (); // \u9759\u6001\u51fd\u6570\u7684\u58f0\u660e }; // \u5728\u7c7b\u5916\u5b9a\u4e49\u9759\u6001\u6210\u5458\u3002\u6ce8\u610f\uff0c\u6211\u4eec\u5728\u6b64\u5904\u6ca1\u6709\u4f7f\u7528 static \u5173\u952e\u5b57\u3002 // ID \u4ece 1 \u5f00\u59cb int IDGenerator :: s_nextID { 1 }; // \u7c7b\u5916\u5b9a\u4e49\u9759\u6001\u6210\u5458\u51fd\u6570\u3002\u6ce8\u610f\uff0c\u6211\u4eec\u5728\u6b64\u5904\u6ca1\u6709\u4f7f\u7528 static \u5173\u952e\u5b57\u3002 int IDGenerator::getNextID () { return s_nextID ++ ; } int main () { for ( int count { 0 }; count < 5 ; ++ count ) std :: cout << \"The next ID is: \" << IDGenerator :: getNextID () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 4 5 The next ID is: 1 The next ID is: 2 The next ID is: 3 The next ID is: 4 The next ID is: 5 \u6ce8\u610f\uff0c\u56e0\u4e3a\u8be5\u7c7b\u4e2d\u7684\u6240\u6709\u6570\u636e\u548c\u51fd\u6570\u90fd\u662f\u9759\u6001\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u9700\u8981\u5b9e\u4f8b\u5316\u8be5\u7c7b\u7684\u5bf9\u8c61\u6765\u4f7f\u7528\u5b83\u7684\u529f\u80fd\uff01\u8be5\u7c7b\u5229\u7528\u4e00\u4e2a\u9759\u6001\u6210\u5458\u53d8\u91cf\u6765\u4fdd\u5b58\u4e0b\u4e00\u4e2a\u8981\u5206\u914d\u7684ID\u7684\u503c\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u9759\u6001\u6210\u5458\u51fd\u6570\u6765\u8fd4\u56de\u8be5ID\u5e76\u4f7f\u5176\u9012\u589e\u3002","title":"\u53c8\u4e00\u4e2a\u4f8b\u5b50"},{"location":"13-14-static-member-functions/#_3","text":"\u5bf9\u4e8e\u4e00\u4e2a\u6240\u6709\u6210\u5458\u90fd\u662f\u9759\u6001\u6210\u5458\u7684\u7c7b\u65f6\u8981\u5c0f\u5fc3\u3002\u5c3d\u7ba1\u8fd9\u79cd\u201c\u7eaf\u9759\u6001\u7c7b\u201d(\u4e5f\u79f0\u4e3a\u201c\u5355\u6001\u7c7b\u201d)\u53ef\u80fd\u5f88\u6709\u7528\uff0c\u4f46\u5b83\u4eec\u4e5f\u6709\u4e00\u4e9b\u6f5c\u5728\u7684\u7f3a\u70b9\u3002 \u9996\u5148\uff0c\u56e0\u4e3a\u6240\u6709\u9759\u6001\u6210\u5458\u90fd\u53ea\u5b9e\u4f8b\u5316\u4e00\u6b21\uff0c\u6240\u4ee5\u4e0d\u53ef\u80fd\u6709\u7eaf\u9759\u6001\u7c7b\u7684\u591a\u4e2a\u526f\u672c(\u9664\u975e\u514b\u9686\u8be5\u7c7b\u5e76\u91cd\u547d\u540d\u5b83)\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u9700\u8981\u4e24\u4e2a\u72ec\u7acb\u7684 IDGenerator \u5bf9\u8c61\uff0c\u8fd9\u5bf9\u4e8e\u5355\u4e2a\u7eaf\u9759\u6001\u7c7b\u6765\u8bf4\u662f\u4e0d\u53ef\u80fd\u7684\u3002 \u5176\u6b21\uff0c\u5728\u5173\u4e8e\u5168\u5c40\u53d8\u91cf\u7684\u4e00\u8bfe\u4e2d\uff0c\u4f60\u5df2\u7ecf\u4e86\u89e3\u5230\u4e86\u5168\u5c40\u53d8\u91cf\u7684\u5371\u9669\u6027\uff0c\u56e0\u4e3a\u4efb\u4f55\u4e00\u6bb5\u4ee3\u7801\u90fd\u53ef\u80fd\u6539\u53d8\u5168\u5c40\u53d8\u91cf\u7684\u503c\uff0c\u5e76\u6700\u7ec8\u7834\u574f\u53e6\u4e00\u6bb5\u770b\u8d77\u6765\u4e0d\u76f8\u5173\u7684\u4ee3\u7801\u3002\u5bf9\u4e8e\u7eaf\u9759\u6001\u7c7b\u4e5f\u662f\u5982\u6b64\u3002\u56e0\u4e3a\u6240\u6709\u6210\u5458\u90fd\u5c5e\u4e8e\u7c7b(\u800c\u4e0d\u662f\u7c7b\u7684\u5bf9\u8c61)\uff0c\u800c\u4e14\u7c7b\u58f0\u660e\u901a\u5e38\u5177\u6709\u5168\u5c40\u4f5c\u7528\u57df\uff0c\u6240\u4ee5\u7eaf\u9759\u6001\u7c7b\u672c\u8d28\u4e0a\u76f8\u5f53\u4e8e\u5728\u5168\u5c40\u53ef\u8bbf\u95ee\u7684\u540d\u79f0\u7a7a\u95f4\u4e2d\u58f0\u660e\u51fd\u6570\u548c\u5168\u5c40\u53d8\u91cf\uff0c\u5e76\u5177\u6709\u5168\u5c40\u53d8\u91cf\u6240\u5177\u6709\u7684\u6240\u6709\u5fc5\u8981\u7f3a\u70b9\u3002","title":"\u6709\u5173\u5168\u9759\u6001\u6210\u5458\u7c7b\u7684\u4e00\u4e9b\u8b66\u793a"},{"location":"13-14-static-member-functions/#c","text":"\u65e2\u7136\u53ef\u4ee5\u901a\u8fc7\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u666e\u901a\u6210\u5458\u53d8\u91cf\uff0c\u90a3\u4e48\u4e5f\u5e94\u8be5\u80fd\u591f\u901a\u8fc7\u9759\u6001\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u9759\u6001\u6210\u5458\u53d8\u91cf\u3002\u867d\u7136\u4e00\u4e9b\u73b0\u4ee3\u8bed\u8a00\u786e\u5b9e\u51fa\u4e8e\u8fd9\u4e2a\u76ee\u7684\u6dfb\u52a0\u4e86\u5bf9\u9759\u6001\u6784\u9020\u51fd\u6570\u7684\u652f\u6301\uff0c\u4f46\u4e0d\u5e78\u7684\u662fC++\u4e0d\u662f\u5176\u4e2d\u4e4b\u4e00\u3002 \u5982\u679c\u53ef\u4ee5\u76f4\u63a5\u521d\u59cb\u5316\u9759\u6001\u53d8\u91cf\uff0c\u5219\u4e0d\u9700\u8981\u6784\u9020\u51fd\u6570\uff1a\u53ef\u4ee5\u5728\u5b9a\u4e49\u70b9\u521d\u59cb\u5316\u9759\u6001\u6210\u5458\u53d8\u91cf(\u5373\u4f7f\u5b83\u662f\u79c1\u6709\u7684)\u3002\u6211\u4eec\u5728\u4e0a\u9762\u7684IDGenerator\u793a\u4f8b\u4e2d\u6b63\u662f\u8fd9\u6837\u505a\u7684\u3002\u8fd9\u662f\u53e6\u4e00\u4e2a\u4f8b\u5b50: 1 2 3 4 5 6 7 class MyClass { public : static std :: vector < char > s_mychars ; }; std :: vector < char > MyClass :: s_mychars { 'a' , 'e' , 'i' , 'o' , 'u' }; // \u5728\u5b9a\u4e49\u65f6\u521d\u59cb\u5316\u9759\u6001\u53d8\u91cf \u5982\u679c\u9700\u8981\u6267\u884c\u4ee3\u7801(\u4f8b\u5982\u5faa\u73af)\u624d\u80fd\u521d\u59cb\u5316\u9759\u6001\u6210\u5458\u53d8\u91cf\u7684\u8bdd\uff0c\u6709\u8bb8\u591a\u4e0d\u540c\u7684\u3001\u7b28\u62d9\u7684\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u6709\u4e00\u79cd\u5904\u7406\u65b9\u6cd5\u9002\u7528\u4e8e\u6240\u6709\u53d8\u91cf(\u9759\u6001\u6216\u975e\u9759\u6001)\uff0c\u5373\u4f7f\u7528lambda\u51fd\u6570\u5e76\u7acb\u5373\u8c03\u7528\u5b83\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class MyClass { public : static std :: vector < char > s_mychars ; }; std :: vector < char > MyClass :: s_mychars { []{ // The parameter list of lambdas without parameters can be omitted. // Inside the lambda we can declare another vector and use a loop. std :: vector < char > v {}; for ( char ch { 'a' }; ch <= 'z' ; ++ ch ) { v . push_back ( ch ); } return v ; }() // Call the lambda right away }; \u4e0b\u9762\u7684\u4ee3\u7801\u7ed9\u51fa\u4e86\u4e00\u4e2a\u884c\u4e3a\u66f4\u50cf\u5e38\u89c4\u6784\u9020\u51fd\u6570\u7684\u65b9\u6cd5\u3002\u4f46\u662f\uff0c\u60a8\u53ef\u80fd\u6c38\u8fdc\u90fd\u4e0d\u9700\u8981\u5b83\uff0c\u6240\u4ee5\u5982\u679c\u60a8\u613f\u610f\uff0c\u53ef\u4ee5\u968f\u610f\u8df3\u8fc7\u672c\u8282\u7684\u5176\u4f59\u90e8\u5206\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class MyClass { public : static std :: vector < char > s_mychars ; class init_static // we're defining a nested class named init_static { public : init_static () // the init constructor will initialize our static variable { for ( char ch { 'a' }; ch <= 'z' ; ++ ch ) { s_mychars . push_back ( ch ); } } } ; private : static init_static s_initializer ; // we'll use this static object to ensure the init_static constructor is called }; std :: vector < char > MyClass :: s_mychars {}; // define our static member variable MyClass :: init_static MyClass :: s_initializer {}; // define our static initializer, which will call the init_static constructor, which will initialize s_mychars \u5f53\u5b9a\u4e49\u9759\u6001\u6210\u5458 s_initializer \u65f6\uff0c\u5c06\u8c03\u7528 init_static() \u9ed8\u8ba4\u6784\u9020\u51fd\u6570(\u56e0\u4e3a s_initializer \u662f init_static \u7c7b\u578b)\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u4efb\u4f55\u9759\u6001\u6210\u5458\u53d8\u91cf\u3002\u8fd9\u4e2a\u89e3\u51b3\u65b9\u6848\u7684\u4f18\u70b9\u662f\uff0c\u6240\u6709\u521d\u59cb\u5316\u4ee3\u7801\u90fd\u9690\u85cf\u5728\u5e26\u6709\u9759\u6001\u6210\u5458\u7684\u539f\u59cb\u7c7b\u4e2d\u3002","title":"C++ \u4e0d\u652f\u6301\u9759\u6001\u6784\u9020\u51fd\u6570"},{"location":"13-14-static-member-functions/#_4","text":"\u9759\u6001\u6210\u5458\u51fd\u6570\u53ef\u7528\u4e8e\u914d\u5408\u7c7b\u4e2d\u7684\u9759\u6001\u6210\u5458\u53d8\u91cf\u6765\u5de5\u4f5c\u3002\u7c7b\u5bf9\u8c61\u4e0d\u9700\u8981\u8c03\u7528\u5b83\u4eec\u3002 \u4e00\u4e2a\u7c7b\u7684\u6240\u6709\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u90fd\u662f\u9759\u6001\u7684\u3002\u7136\u800c\uff0c\u6b64\u7c7b\u7c7b\u672c\u8d28\u4e0a\u76f8\u5f53\u4e8e\u5728\u5168\u5c40\u53ef\u8bbf\u95ee\u7684\u540d\u79f0\u7a7a\u95f4\u4e2d\u58f0\u660e\u51fd\u6570\u548c\u5168\u5c40\u53d8\u91cf\uff0c\u901a\u5e38\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u5b83\u4eec\uff0c\u9664\u975e\u6709\u7279\u522b\u597d\u7684\u7406\u7531\u4f7f\u7528\u5b83\u4eec\u3002","title":"\u5c0f\u7ed3"},{"location":"13-15-friend-functions-and-classes/","tags":["class","friend"],"text":"\u672c\u7ae0\u4e2d\u5f88\u5927\u90e8\u5206\u7bc7\u5e45\u4e2d\u6211\u4eec\u90fd\u5728\u5f3a\u8c03\u5c01\u88c5\uff08\u6570\u636e\u79c1\u6709\uff09\u7684\u91cd\u8981\u6027\u3002\u4e0d\u8fc7\uff0c\u6709\u65f6\u5019\u6211\u4eec\u4e5f\u4f1a\u53d1\u73b0\uff0c\u6709\u4e9b\u7c7b\u548c\u51fd\u6570\u4f1a\u548c\u5176\u4ed6\u5176\u4ed6\u5916\u90e8\u7c7b\u5bc6\u5207\u914d\u5408\u5de5\u4f5c\u3002\u4f8b\u5982\uff0c\u6709\u4e00\u4e2a\u7c7b\u8d1f\u8d23\u5b58\u653e\u6570\u636e\uff0c\u800c\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\uff08\u6216\u53e6\u5916\u4e00\u4e2a\u7c7b\uff09\u7528\u4e8e\u5c06\u6570\u636e\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\u3002\u5c3d\u7ba1\u5b58\u50a8\u7c7b\u548c\u663e\u793a\u4ee3\u7801\u51fa\u4e8e\u4fbf\u4e8e\u7ef4\u62a4\u7684\u76ee\u7684\u800c\u88ab\u5206\u5272\u5f00\u6765\uff0c\u4f46\u662f\u663e\u793a\u4ee3\u7801\u5b9e\u9645\u4e0a\u975e\u5e38\u7d27\u5bc6\u5730\u4f9d\u9760\u7740\u5b58\u50a8\u7c7b\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u5c06\u5b58\u50a8\u7c7b\u7684\u7ec6\u8282\u5bf9\u663e\u793a\u4ee3\u7801\u9690\u85cf\u8d77\u6765\u5e76\u65e0\u592a\u591a\u5e2e\u52a9\u3002 \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u6709\u4e24\u79cd\u9009\u62e9\uff1a \u8ba9\u663e\u793a\u4ee3\u7801\u4f7f\u7528\u5b58\u50a8\u7c7b\u7684\u516c\u6709\u51fd\u6570\u3002\u7136\u800c\uff0c\u8fd9\u4e48\u505a\u6709\u51e0\u4e2a\u6f5c\u5728\u7684\u7f3a\u70b9\u3002\u9996\u5148\uff0c\u5fc5\u987b\u5b9a\u4e49\u8fd9\u4e9b\u516c\u5171\u6210\u5458\u51fd\u6570\uff0c\u8fd9\u9700\u8981\u65f6\u95f4\uff0c\u800c\u4e14\u53ef\u80fd\u4f1a\u6253\u4e71\u5b58\u50a8\u7c7b\u7684\u63a5\u53e3\u3002\u5176\u6b21\uff0c\u5b58\u50a8\u7c7b\u53ef\u80fd\u5fc5\u987b\u66b4\u9732\u4e00\u4e9b\u63a5\u53e3\u7ed9\u663e\u793a\u4ee3\u7801\uff0c\u4f46\u662f\u8fd9\u4e9b\u4ee3\u7801\u5e76\u4e0d\u5e94\u8be5\u66b4\u9732\u7ed9\u5176\u4ed6\u6a21\u5757\uff0c\u6211\u4eec\u4e0d\u5e0c\u671b\u5176\u4ed6\u4efb\u4f55\u4eba\u90fd\u80fd\u8bbf\u95ee\u8fd9\u4e9b\u51fd\u6570\u3002\u6ca1\u6709\u529e\u6cd5\u6307\u5b9a\u8fd9\u4e2a\u51fd\u6570\u53ea\u80fd\u88ab\u663e\u793a\u7c7b\u4f7f\u7528\u3002 \u6216\u8005\uff0c\u4f7f\u7528\u53cb\u5143\u7c7b\u548c\u53cb\u5143\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u8ba9\u663e\u793a\u4ee3\u7801\u8bbf\u95ee\u5b58\u50a8\u7c7b\u7684\u79c1\u6709\u7ec6\u8282\u3002\u8fd9\u5141\u8bb8\u663e\u793a\u4ee3\u7801\u76f4\u63a5\u8bbf\u95ee\u5b58\u50a8\u7c7b\u7684\u6240\u6709\u79c1\u6709\u6210\u5458\u548c\u51fd\u6570\uff0c\u540c\u65f6\u5c06\u5176\u4ed6\u6240\u6709\u4eba\u6392\u9664\u5728\u5916\uff01\u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u4ed4\u7ec6\u770b\u770b\u8fd9\u662f\u5982\u4f55\u505a\u5230\u7684\u3002 \u53cb\u5143\u51fd\u6570 \u53cb\u5143\u51fd\u6570 \u53ef\u4ee5\u50cf\u6210\u5458\u51fd\u6570\u4e00\u6837\u8bbf\u95ee\u4e00\u4e2a\u7c7b\u7684 \u79c1\u6709\u6210\u5458 \u3002\u4ece\u5404\u4e2a\u65b9\u9762\u6765\u770b\uff0c\u53cb\u5143\u51fd\u6570\u90fd\u548c\u666e\u901a\u51fd\u6570\u6ca1\u4ec0\u4e48\u533a\u522b\u3002\u53cb\u5143\u51fd\u6570\u53ef\u4ee5\u662f\u666e\u901a\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u662f\u5176\u4ed6\u7c7b\u7684\u6210\u5458\u51fd\u6570\u3002\u58f0\u660e\u4e00\u4e2a\u53cb\u5143\u51fd\u6570\uff0c\u53ea\u9700\u8981\u5728\u4f60\u5e0c\u671b\u6210\u4e3a\u53cb\u5143\u7684\u51fd\u6570\u539f\u578b\u524d\u6dfb\u52a0 friend \u5173\u952e\u5b57\u5373\u53ef\u3002\u5c06\u53cb\u5143\u51fd\u6570\u5b9a\u4e49\u5728private\u8fd8\u662fpublic\u4e0b\u6ca1\u4ec0\u4e48\u533a\u522b\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u53cb\u5143\u51fd\u6570\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 class Accumulator { private : int m_value { 0 }; public : void add ( int value ) { m_value += value ; } // \u4f7f reset() \u51fd\u6570\u6210\u4e3a\u8be5\u7c7b\u7684\u53cb\u5143\u51fd\u6570 friend void reset ( Accumulator & accumulator ); }; // reset() \u73b0\u5728\u662f Accumulator \u7c7b\u7684\u53cb\u5143 void reset ( Accumulator & accumulator ) { // \u53ef\u4ee5\u8bbf\u95ee Accumulator \u7684\u79c1\u6709\u6210\u5458 accumulator . m_value = 0 ; } int main () { Accumulator acc ; acc . add ( 5 ); // add 5 to the accumulator reset ( acc ); // reset the accumulator to 0 return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a\u540d\u4e3a reset() \u7684\u51fd\u6570\uff0c\u5b83\u63a5\u53d7 Accumulator \u7c7b\u7684\u5bf9\u8c61\uff0c\u5e76\u5c06 m_value \u7684\u503c\u8bbe\u7f6e\u4e3a0\u3002\u56e0\u4e3a reset() \u4e0d\u662f Accumulator \u7c7b\u7684\u6210\u5458\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c reset() \u4e0d\u80fd\u8bbf\u95ee Accumulator \u7684\u79c1\u6709\u6210\u5458\u3002\u4f46\u662f\uff0c\u56e0\u4e3a Accumulator \u5df2\u7ecf\u7279\u522b\u58f0\u660e\u4e86\u8fd9\u4e2a reset() \u51fd\u6570\u662f\u8fd9\u4e2a\u7c7b\u7684\u53cb\u5143\uff0c\u6240\u4ee5\u8ba9 reset() \u51fd\u6570\u8bbf\u95ee Accumulator \u7684\u79c1\u6709\u6210\u5458\u3002 \u6ce8\u610f\uff0c\u6211\u4eec\u5fc5\u987b\u5411 reset() \u4f20\u9012\u4e00\u4e2a Accumulator \u5bf9\u8c61\u3002\u8fd9\u662f\u56e0\u4e3a reset() \u4e0d\u662f\u6210\u5458\u51fd\u6570\u3002\u5b83\u6ca1\u6709 *this \u6307\u9488\uff0c\u4e5f\u6ca1\u6709 Accumulator \u5bf9\u8c61\u53ef\u4ee5\u7528\u6765\u64cd\u4f5c\uff0c\u9664\u975e\u7ed9\u5b9a\u4e00\u4e2a\u3002 \u518d\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include class Value { private : int m_value {}; public : Value ( int value ) : m_value { value } { } friend bool isEqual ( const Value & value1 , const Value & value2 ); }; bool isEqual ( const Value & value1 , const Value & value2 ) { return ( value1 . m_value == value2 . m_value ); } int main () { Value v1 { 5 }; Value v2 { 6 }; std :: cout << std :: boolalpha << isEqual ( v1 , v2 ); return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u6211\u4eec\u5c06\u51fd\u6570 isEqual() \u51fd\u6570\u58f0\u660e\u4e3a Value \u7c7b\u7684\u53cb\u5143\u3002 isEqual() \u63a5\u53d7\u4e24\u4e2a Value \u5bf9\u8c61\u4f5c\u4e3a \u5f62\u53c2 \u3002\u56e0\u4e3a isEqual() \u662f Value \u7684\u53cb\u5143\u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u8bbf\u95ee Value \u5bf9\u8c61\u7684\u6240\u6709\u79c1\u6709\u6210\u5458\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\uff0c\u5982\u679c\u76f8\u7b49\u5219\u8fd4\u56de true\u3002 \u867d\u7136\u4e0a\u9762\u7684\u4e24\u4e2a\u4f8b\u5b50\u90fd\u662f\u523b\u610f\u8bbe\u8ba1\u7684\uff0c\u4f46\u7b2c\u4e8c\u4e2a\u4f8b\u5b50\u4e0e\u6211\u4eec\u7a0d\u540e\u4f1a\u8ba8\u8bba\u8fd0\u7b97\u7b26\u91cd\u8f7d\u65f6\u9047\u5230\u7684\u60c5\u51b5\u975e\u5e38\u76f8\u4f3c! \u591a\u4e2a\u53cb\u5143 \u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u540c\u65f6\u6210\u4e3a\u591a\u4e2a\u7c7b\u7684\u53cb\u5143\u3002\u4f8b\u5982\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include class Humidity ; class Temperature { private : int m_temp {}; public : Temperature ( int temp = 0 ) : m_temp { temp } { } friend void printWeather ( const Temperature & temperature , const Humidity & humidity ); }; class Humidity { private : int m_humidity {}; public : Humidity ( int humidity = 0 ) : m_humidity { humidity } { } friend void printWeather ( const Temperature & temperature , const Humidity & humidity ); }; void printWeather ( const Temperature & temperature , const Humidity & humidity ) { std :: cout << \"The temperature is \" << temperature . m_temp << \" and the humidity is \" << humidity . m_humidity << '\\n' ; } int main () { Humidity hum { 10 }; Temperature temp { 12 }; printWeather ( temp , hum ); return 0 ; } \u5173\u4e8e\u8fd9\u4e2a\u793a\u4f8b\uff0c\u6709\u4e24\u70b9\u503c\u5f97\u6ce8\u610f\u3002\u9996\u5148\uff0c\u56e0\u4e3a printWeather \u662f\u8fd9\u4e24\u4e2a\u7c7b\u7684\u53cb\u5143\u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u8bbf\u95ee\u8fd9\u4e24\u4e2a\u7c7b\u7684\u5bf9\u8c61\u7684\u79c1\u6709\u6570\u636e\u3002\u5176\u6b21\uff0c\u6ce8\u610f\u793a\u4f8b\u4e2d\u9876\u90e8\u7684\u8fd9\u4e00\u884c\uff1a 1 class Humidity ; \u8fd9\u662f\u4e00\u4e2a\u7c7b\u539f\u578b\uff0c\u5b83\u544a\u8bc9\u7f16\u8bd1\u5668\u6211\u4eec\u5c06\u5728\u672a\u6765\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a Humidity \u7684\u7c7b\u3002\u5982\u679c\u6ca1\u6709\u8fd9\u4e00\u884c\uff0c\u7f16\u8bd1\u5668\u5728\u89e3\u6790 Temperature \u7c7b\u4e2d printWeather() \u7684\u539f\u578b\u65f6\u5c31\u4e0d\u77e5\u9053 Humidity \u662f\u4ec0\u4e48\u3002\u7c7b\u539f\u578b\u7684\u4f5c\u7528\u4e0e\u51fd\u6570\u539f\u578b\u76f8\u540c\u2014\u2014\u5b83\u4eec\u544a\u8bc9\u7f16\u8bd1\u5668\u67d0\u4e2a\u4e1c\u897f\u662f\u4ec0\u4e48\u6837\u5b50\u7684\uff0c\u4ee5\u4fbf\u73b0\u5728\u53ef\u4ee5\u4f7f\u7528\u5b83\uff0c\u4ee5\u540e\u53ef\u4ee5\u5b9a\u4e49\u5b83\u3002\u7136\u800c\uff0c\u4e0e\u51fd\u6570\u4e0d\u540c\u7684\u662f\uff0c\u7c7b\u6ca1\u6709\u8fd4\u56de\u7c7b\u578b\u6216\u53c2\u6570\uff0c\u56e0\u6b64\u7c7b\u539f\u578b\u603b\u662f\u7b80\u5355\u7684 class ClassName \uff0c\u5176\u4e2d ClassName \u662f\u7c7b\u7684\u540d\u79f0\u3002 \u53cb\u5143\u7c7b \u4e5f\u53ef\u4ee5\u8ba9\u6574\u4e2a\u7c7b\u6210\u4e3a\u53e6\u4e00\u4e2a\u7c7b\u7684\u53cb\u5143\u3002\u8fd9\u4f7f\u53cb\u5143\u7c7b\u7684\u6240\u6709\u6210\u5458\u90fd\u53ef\u4ee5\u8bbf\u95ee\u53e6\u4e00\u4e2a\u7c7b\u7684\u79c1\u6709\u6210\u5458\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include class Storage { private : int m_nValue {}; double m_dValue {}; public : Storage ( int nValue , double dValue ) : m_nValue { nValue }, m_dValue { dValue } { } // \u4f7f Display \u7c7b\u79f0\u4e3a Storage \u7684\u53cb\u5143 friend class Display ; }; class Display { private : bool m_displayIntFirst ; public : Display ( bool displayIntFirst ) : m_displayIntFirst { displayIntFirst } { } void displayItem ( const Storage & storage ) { if ( m_displayIntFirst ) std :: cout << storage . m_nValue << ' ' << storage . m_dValue << '\\n' ; else // display double first std :: cout << storage . m_dValue << ' ' << storage . m_nValue << '\\n' ; } }; int main () { Storage storage { 5 , 6.7 }; Display display { false }; display . displayItem ( storage ); return 0 ; } \u56e0\u4e3a Display \u7c7b\u662f Storage \u7684\u53cb\u5143\uff0c\u6240\u4ee5 Display \u7684\u4efb\u4f55\u6210\u5458\u90fd\u53ef\u4ee5 Storage \u7684\u79c1\u6709\u6210\u5458\u3002\u4e0a\u8ff0\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 6.7 5 \u6709\u5173\u53cb\u5143\u7c7b\uff0c\u8fd8\u6709\u4e00\u4e9b\u4e8b\u60c5\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0c\u5373\u4fbf Display \u662f Storage \u7684\u53cb\u5143\uff0c\u4f46\u662f Display \u5e76\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee Storage \u5bf9\u8c61\u7684 *this \u6307\u9488\u3002\u5176\u6b21\uff0c\u56e0\u4e3a Display \u662f Storage \u7684\u53cb\u5143\u7c7b\uff0c\u4f46\u662f\u8fd9\u4e0d\u4ee3\u8868 Storage \u4e5f\u662f Display \u7684\u53cb\u5143\u3002\u5982\u679c\u4f60\u5e0c\u671b\u4e24\u4e2a\u7c7b\u4e92\u4e3a\u53cb\u5143\uff0c\u963f\u4e48\u5fc5\u987b\u5206\u522b\u5728\u7c7b\u4e2d\u5c06\u5bf9\u65b9\u58f0\u660e\u4e3a\u53cb\u5143\u3002\u6700\u540e\uff0c\u5982\u679cA\u662fB\u7684\u53cb\u5143\uff0c\u800cB\u662fC\u7684\u53cb\u5143\uff0c\u4f46\u4e0d\u4ee3\u8868A\u662fC\u7684\u53cb\u5143\u3002 \u5728\u4f7f\u7528\u53cb\u51fd\u6570\u548c\u7c7b\u65f6\u8981\u5c0f\u5fc3\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u53cb\u51fd\u6570\u6216\u7c7b\u6253\u7834\u5c01\u88c5\u3002\u5982\u679c\u7c7b\u6539\u53d8\u4e86\uff0c\u53cb\u5143\u7684\u4e5f\u9700\u8981\u88ab\u8feb\u6539\u53d8\u3002\u56e0\u6b64\uff0c\u5c3d\u91cf\u51cf\u5c11\u5bf9\u53cb\u51fd\u6570\u548c\u53cb\u5143\u7c7b\u7684\u4f7f\u7528\u3002 \u53cb\u5143\u6210\u5458\u51fd\u6570 \u53ef\u4ee5\u5c06\u5355\u4e2a\u6210\u5458\u51fd\u6570\u8bbe\u4e3a\u53cb\u5143\uff0c\u800c\u4e0d\u662f\u5c06\u6574\u4e2a\u7c7b\u8bbe\u4e3a\u53cb\u5143\u3002\u8fd9\u4e0e\u5c06\u666e\u901a\u51fd\u6570\u8bbe\u4e3a\u53cb\u5143\u7c7b\u4f3c\uff0c\u53ea\u662f\u5728\u4f7f\u7528\u6210\u5458\u51fd\u6570\u7684\u540d\u79f0\u65f6\u5305\u542b\u4e86 className:: \u524d\u7f00\uff08\u4f8b\u5982 Display::displayItem )\u3002 \u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u6bd4\u9884\u671f\u7684\u8981\u590d\u6742\u4e00\u4e9b\u3002\u8ba9\u6211\u4eec\u6539\u5199\u4e00\u4e0b\u524d\u9762\u7684\u793a\u4f8b\uff0c\u4f7f Display::displayItem \u6210\u4e3a\u53cb\u5143\u6210\u5458\u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include class Display ; // Display \u7684\u524d\u5411\u58f0\u660e class Storage { private : int m_nValue {}; double m_dValue {}; public : Storage ( int nValue , double dValue ) : m_nValue { nValue }, m_dValue { dValue } { } // \u4f7f Display::displayItem \u6210\u5458\u51fd\u6570\u6210\u4e3a Storage \u7684\u53cb\u5143 friend void Display :: displayItem ( const Storage & storage ); // \u9519\u8bef: Storage \u6b64\u65f6\u5e76\u4e0d\u77e5\u9053 Display \u7c7b\u7684\u5b8c\u6574\u5b9a\u4e49\u3002 }; class Display { private : bool m_displayIntFirst {}; public : Display ( bool displayIntFirst ) : m_displayIntFirst { displayIntFirst } { } void displayItem ( const Storage & storage ) { if ( m_displayIntFirst ) std :: cout << storage . m_nValue << ' ' << storage . m_dValue << '\\n' ; else // display double first std :: cout << storage . m_dValue << ' ' << storage . m_nValue << '\\n' ; } }; \u5f88\u9057\u61be\uff0c\u8fd9\u4e48\u505a\u5e76\u4e0d\u80fd\u6b63\u786e\u5de5\u4f5c\u3002\u4e3a\u4e86\u8ba9\u4e00\u4e2a\u6210\u5458\u51fd\u6570\u79f0\u4e3a\u53cb\u5143\uff0c\u7f16\u8bd1\u5668\u5fc5\u987b\u770b\u5230\u8be5\u6210\u5458\u51fd\u6570\u7684\u5b8c\u6574\u5b9a\u4e49\u800c\u4e0d\u4ec5\u4ec5\u662f \u524d\u5411\u58f0\u660e \u3002\u56e0\u4e3a Storage \u8fd8\u6ca1\u6709\u770b\u5230 Display \u7684\u5b8c\u6574\u5b9a\u4e49\uff0c\u6240\u4ee5\u5f53\u6211\u4eec\u5728\u6b64\u5904\u58f0\u660e\u53cb\u5143\u51fd\u6570\u65f6\u4f1a\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ea\u8981\u5c06 Display \u7684\u5b9a\u4e49\u79fb\u52a8\u5230 Storage \u524d\u9762\u5373\u53ef\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include class Display { private : bool m_displayIntFirst {}; public : Display ( bool displayIntFirst ) : m_displayIntFirst { displayIntFirst } { } void displayItem ( const Storage & storage ) // error: compiler doesn't know what a Storage is { if ( m_displayIntFirst ) std :: cout << storage . m_nValue << ' ' << storage . m_dValue << '\\n' ; else // display double first std :: cout << storage . m_dValue << ' ' << storage . m_nValue << '\\n' ; } }; class Storage { private : int m_nValue {}; double m_dValue {}; public : Storage ( int nValue , double dValue ) : m_nValue { nValue }, m_dValue { dValue } { } // Make the Display::displayItem member function a friend of the Storage class friend void Display :: displayItem ( const Storage & storage ); // okay now }; \u4e0d\u8fc7\uff0c \u8fd9\u6837\u4e00\u6765\u4f1a\u5e26\u6765\u53e6\u5916\u7684\u95ee\u9898\uff0c\u56e0\u4e3a Display::displayItem() \u4f7f\u7528 Storage \u4f5c\u4e3a\u5f15\u7528\u53c2\u6570\u3002\u800c\u5f53\u6211\u4eec\u5c06 Storage \u7684\u5b9a\u4e49\u79fb\u52a8\u5230 Display \u540e\u9762\u65f6\uff0c \u7f16\u8bd1\u5668\u5c31\u4f1a\u62b1\u6028\u5b83\u4e0d\u77e5\u9053Storage \u662f\u4ec0\u4e48\uff0c\u6211\u4eec\u4e0d\u80fd\u518d\u8c03\u6574\u987a\u5e8f\u4e86\uff0c\u4e0d\u7136\u53c8\u4f1a\u51fa\u73b0\u4e4b\u524d\u7684\u95ee\u9898\u3002 \u5e78\u8fd0\u5730\u662f\uff0c\u53ea\u9700\u8981\u51e0\u6b65\u5c31\u53ef\u4ee5\u89e3\u51b3\u8be5\u95ee\u9898\u3002\u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u5bf9\u4e8e Storage \u7684\u524d\u5411\u58f0\u660e\u3002\u7b2c\u4e8c\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06 Display::displayItem() \u79fb\u52a8\u5230\u7c7b\u5916\u90e8\uff0c\u653e\u5728 Storage \u7c7b\u5b8c\u6574\u5b9a\u4e49\u7684\u540e\u3002 \u770b\u4e0a\u53bb\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 #include class Storage ; // Storage \u7684\u524d\u5411\u58f0\u660e class Display { private : bool m_displayIntFirst {}; public : Display ( bool displayIntFirst ) : m_displayIntFirst { displayIntFirst } { } void displayItem ( const Storage & storage ); // \u4e0a\u9762\u7684\u524d\u5411\u58f0\u660e\u5c31\u662f\u4e3a\u4e86\u8fd9\u4e00\u53e5\u58f0\u660e }; class Storage // Storage \u7c7b\u7684\u5b8c\u6574\u5b9a\u4e49 { private : int m_nValue {}; double m_dValue {}; public : Storage ( int nValue , double dValue ) : m_nValue { nValue }, m_dValue { dValue } { } // \u5c06 Display::displayItem \u58f0\u660e\u4e3a Storage \u7c7b\u7684\u53cb\u5143 (\u5fc5\u987b\u770b\u5230 Display \u7684\u5b8c\u6574\u5b9a\u4e49) friend void Display :: displayItem ( const Storage & storage ); }; // \u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49 Display::displayItem \u4e86\uff0c\u5b83\u5fc5\u987b\u770b\u5230 Storage \u7684\u5b8c\u6574\u5b9a\u4e49 void Display::displayItem ( const Storage & storage ) { if ( m_displayIntFirst ) std :: cout << storage . m_nValue << ' ' << storage . m_dValue << '\\n' ; else // display double first std :: cout << storage . m_dValue << ' ' << storage . m_nValue << '\\n' ; } int main () { Storage storage ( 5 , 6.7 ); Display display ( false ); display . displayItem ( storage ); return 0 ; } \u73b0\u5728\uff0c\u4ee3\u7801\u53ef\u4ee5\u6b63\u5e38\u7f16\u8bd1\u4e86\uff1a Storage \u7684\u524d\u5411\u58f0\u660e\u5bf9\u4e8e Display::displayItem() \u7684\u58f0\u660e\u6765\u8bf4\u8db3\u591f\u4e86\u3002 Display \u7684\u5b8c\u6574\u5b9a\u4e49\u53ef\u4ee5\u786e\u4fdd Display::displayItem() \u88ab\u5b9a\u4e49\u4e3a Storage \u7684\u53cb\u5143\u51fd\u6570\uff0c\u5982\u679c\u4f60\u8fd8\u662f\u611f\u5230\u56f0\u60d1\uff0c\u8bf7\u4ed4\u7ec6\u770b\u770b\u4e0a\u9762\u4ee3\u7801\u4e2d\u7684\u6ce8\u91ca\u3002 \u5982\u679c\u8fd9\u4ee4\u4f60\u611f\u5230\u56f0\u96be\u2014\u2014\u7684\u786e\u662f\u7684\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u8fd9\u4e48\u505a\u7684\u539f\u56e0\u53ea\u662f\u56e0\u4e3a\u6211\u4eec\u8bd5\u56fe\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u5b8c\u6210\u6240\u6709\u5de5\u4f5c\u3002\u66f4\u597d\u7684\u89e3\u51b3\u65b9\u6848\u662f\u5c06\u6bcf\u4e2a\u7c7b\u5b9a\u4e49\u653e\u5728\u5355\u72ec\u7684\u5934\u6587\u4ef6\u4e2d\uff0c\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u653e\u5728\u76f8\u5e94\u7684.cpp\u6587\u4ef6\u4e2d\u3002\u8fd9\u6837\uff0c\u6240\u6709\u7684\u7c7b\u5b9a\u4e49\u90fd\u5c06\u7acb\u5373\u5728.cpp\u6587\u4ef6\u4e2d\u53ef\u89c1\uff0c\u5e76\u4e14\u4e0d\u9700\u8981\u91cd\u65b0\u6392\u5217\u7c7b\u6216\u51fd\u6570! \u5c0f\u7ed3 \u53cb\u5143\u51fd\u6570\u6216\u53cb\u5143\u7c7b\u662f\u53ef\u4ee5\u8bbf\u95ee\u53e6\u4e00\u4e2a\u7c7b\u7684\u79c1\u6709\u6210\u5458\u7684\u51fd\u6570\u6216\u7c7b\uff0c\u5c31\u597d\u50cf\u5b83\u662f\u8be5\u7c7b\u7684\u6210\u5458\u4e00\u6837\u3002\u8fd9\u5141\u8bb8\u53cb\u5143\u51fd\u6570\u6216\u53cb\u5143\u7c7b\u4e0e\u53e6\u4e00\u4e2a\u7c7b\u7d27\u5bc6\u5730\u4e00\u8d77\u5de5\u4f5c\uff0c\u800c\u4e0d\u5fc5\u66b4\u9732\u4e00\u4e2a\u79c1\u6709\u6210\u5458(\u4f8b\u5982\u901a\u8fc7\u8bbf\u95ee\u51fd\u6570)\u3002 \u53cb\u5143\u901a\u5e38\u5728\u5b9a\u4e49\u91cd\u8f7d\u64cd\u4f5c\u7b26(\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u7ae0\u8ba8\u8bba)\u65f6\u4f7f\u7528\uff0c\u6216\u8005\uff08\u5c11\u6570\u65f6\u5019\uff09\u5728\u4e24\u4e2a\u6216\u591a\u4e2a\u7c7b\u9700\u8981\u4ee5\u4eb2\u5bc6\u7684\u65b9\u5f0f\u4e00\u8d77\u5de5\u4f5c\u65f6\u3002 \u6ce8\u610f\uff0c\u8981\u4f7f\u7279\u5b9a\u7684\u6210\u5458\u51fd\u6570\u6210\u4e3a\u53cb\u5143\uff0c\u9996\u5148\u9700\u8981\u770b\u5230\u6210\u5458\u51fd\u6570\u7c7b\u7684\u5b8c\u6574\u5b9a\u4e49\u3002","title":"13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b"},{"location":"13-15-friend-functions-and-classes/#_1","text":"\u53cb\u5143\u51fd\u6570 \u53ef\u4ee5\u50cf\u6210\u5458\u51fd\u6570\u4e00\u6837\u8bbf\u95ee\u4e00\u4e2a\u7c7b\u7684 \u79c1\u6709\u6210\u5458 \u3002\u4ece\u5404\u4e2a\u65b9\u9762\u6765\u770b\uff0c\u53cb\u5143\u51fd\u6570\u90fd\u548c\u666e\u901a\u51fd\u6570\u6ca1\u4ec0\u4e48\u533a\u522b\u3002\u53cb\u5143\u51fd\u6570\u53ef\u4ee5\u662f\u666e\u901a\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u662f\u5176\u4ed6\u7c7b\u7684\u6210\u5458\u51fd\u6570\u3002\u58f0\u660e\u4e00\u4e2a\u53cb\u5143\u51fd\u6570\uff0c\u53ea\u9700\u8981\u5728\u4f60\u5e0c\u671b\u6210\u4e3a\u53cb\u5143\u7684\u51fd\u6570\u539f\u578b\u524d\u6dfb\u52a0 friend \u5173\u952e\u5b57\u5373\u53ef\u3002\u5c06\u53cb\u5143\u51fd\u6570\u5b9a\u4e49\u5728private\u8fd8\u662fpublic\u4e0b\u6ca1\u4ec0\u4e48\u533a\u522b\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u53cb\u5143\u51fd\u6570\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 class Accumulator { private : int m_value { 0 }; public : void add ( int value ) { m_value += value ; } // \u4f7f reset() \u51fd\u6570\u6210\u4e3a\u8be5\u7c7b\u7684\u53cb\u5143\u51fd\u6570 friend void reset ( Accumulator & accumulator ); }; // reset() \u73b0\u5728\u662f Accumulator \u7c7b\u7684\u53cb\u5143 void reset ( Accumulator & accumulator ) { // \u53ef\u4ee5\u8bbf\u95ee Accumulator \u7684\u79c1\u6709\u6210\u5458 accumulator . m_value = 0 ; } int main () { Accumulator acc ; acc . add ( 5 ); // add 5 to the accumulator reset ( acc ); // reset the accumulator to 0 return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a\u540d\u4e3a reset() \u7684\u51fd\u6570\uff0c\u5b83\u63a5\u53d7 Accumulator \u7c7b\u7684\u5bf9\u8c61\uff0c\u5e76\u5c06 m_value \u7684\u503c\u8bbe\u7f6e\u4e3a0\u3002\u56e0\u4e3a reset() \u4e0d\u662f Accumulator \u7c7b\u7684\u6210\u5458\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c reset() \u4e0d\u80fd\u8bbf\u95ee Accumulator \u7684\u79c1\u6709\u6210\u5458\u3002\u4f46\u662f\uff0c\u56e0\u4e3a Accumulator \u5df2\u7ecf\u7279\u522b\u58f0\u660e\u4e86\u8fd9\u4e2a reset() \u51fd\u6570\u662f\u8fd9\u4e2a\u7c7b\u7684\u53cb\u5143\uff0c\u6240\u4ee5\u8ba9 reset() \u51fd\u6570\u8bbf\u95ee Accumulator \u7684\u79c1\u6709\u6210\u5458\u3002 \u6ce8\u610f\uff0c\u6211\u4eec\u5fc5\u987b\u5411 reset() \u4f20\u9012\u4e00\u4e2a Accumulator \u5bf9\u8c61\u3002\u8fd9\u662f\u56e0\u4e3a reset() \u4e0d\u662f\u6210\u5458\u51fd\u6570\u3002\u5b83\u6ca1\u6709 *this \u6307\u9488\uff0c\u4e5f\u6ca1\u6709 Accumulator \u5bf9\u8c61\u53ef\u4ee5\u7528\u6765\u64cd\u4f5c\uff0c\u9664\u975e\u7ed9\u5b9a\u4e00\u4e2a\u3002 \u518d\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include class Value { private : int m_value {}; public : Value ( int value ) : m_value { value } { } friend bool isEqual ( const Value & value1 , const Value & value2 ); }; bool isEqual ( const Value & value1 , const Value & value2 ) { return ( value1 . m_value == value2 . m_value ); } int main () { Value v1 { 5 }; Value v2 { 6 }; std :: cout << std :: boolalpha << isEqual ( v1 , v2 ); return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u6211\u4eec\u5c06\u51fd\u6570 isEqual() \u51fd\u6570\u58f0\u660e\u4e3a Value \u7c7b\u7684\u53cb\u5143\u3002 isEqual() \u63a5\u53d7\u4e24\u4e2a Value \u5bf9\u8c61\u4f5c\u4e3a \u5f62\u53c2 \u3002\u56e0\u4e3a isEqual() \u662f Value \u7684\u53cb\u5143\u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u8bbf\u95ee Value \u5bf9\u8c61\u7684\u6240\u6709\u79c1\u6709\u6210\u5458\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\uff0c\u5982\u679c\u76f8\u7b49\u5219\u8fd4\u56de true\u3002 \u867d\u7136\u4e0a\u9762\u7684\u4e24\u4e2a\u4f8b\u5b50\u90fd\u662f\u523b\u610f\u8bbe\u8ba1\u7684\uff0c\u4f46\u7b2c\u4e8c\u4e2a\u4f8b\u5b50\u4e0e\u6211\u4eec\u7a0d\u540e\u4f1a\u8ba8\u8bba\u8fd0\u7b97\u7b26\u91cd\u8f7d\u65f6\u9047\u5230\u7684\u60c5\u51b5\u975e\u5e38\u76f8\u4f3c!","title":"\u53cb\u5143\u51fd\u6570"},{"location":"13-15-friend-functions-and-classes/#_2","text":"\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u540c\u65f6\u6210\u4e3a\u591a\u4e2a\u7c7b\u7684\u53cb\u5143\u3002\u4f8b\u5982\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include class Humidity ; class Temperature { private : int m_temp {}; public : Temperature ( int temp = 0 ) : m_temp { temp } { } friend void printWeather ( const Temperature & temperature , const Humidity & humidity ); }; class Humidity { private : int m_humidity {}; public : Humidity ( int humidity = 0 ) : m_humidity { humidity } { } friend void printWeather ( const Temperature & temperature , const Humidity & humidity ); }; void printWeather ( const Temperature & temperature , const Humidity & humidity ) { std :: cout << \"The temperature is \" << temperature . m_temp << \" and the humidity is \" << humidity . m_humidity << '\\n' ; } int main () { Humidity hum { 10 }; Temperature temp { 12 }; printWeather ( temp , hum ); return 0 ; } \u5173\u4e8e\u8fd9\u4e2a\u793a\u4f8b\uff0c\u6709\u4e24\u70b9\u503c\u5f97\u6ce8\u610f\u3002\u9996\u5148\uff0c\u56e0\u4e3a printWeather \u662f\u8fd9\u4e24\u4e2a\u7c7b\u7684\u53cb\u5143\u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u8bbf\u95ee\u8fd9\u4e24\u4e2a\u7c7b\u7684\u5bf9\u8c61\u7684\u79c1\u6709\u6570\u636e\u3002\u5176\u6b21\uff0c\u6ce8\u610f\u793a\u4f8b\u4e2d\u9876\u90e8\u7684\u8fd9\u4e00\u884c\uff1a 1 class Humidity ; \u8fd9\u662f\u4e00\u4e2a\u7c7b\u539f\u578b\uff0c\u5b83\u544a\u8bc9\u7f16\u8bd1\u5668\u6211\u4eec\u5c06\u5728\u672a\u6765\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a Humidity \u7684\u7c7b\u3002\u5982\u679c\u6ca1\u6709\u8fd9\u4e00\u884c\uff0c\u7f16\u8bd1\u5668\u5728\u89e3\u6790 Temperature \u7c7b\u4e2d printWeather() \u7684\u539f\u578b\u65f6\u5c31\u4e0d\u77e5\u9053 Humidity \u662f\u4ec0\u4e48\u3002\u7c7b\u539f\u578b\u7684\u4f5c\u7528\u4e0e\u51fd\u6570\u539f\u578b\u76f8\u540c\u2014\u2014\u5b83\u4eec\u544a\u8bc9\u7f16\u8bd1\u5668\u67d0\u4e2a\u4e1c\u897f\u662f\u4ec0\u4e48\u6837\u5b50\u7684\uff0c\u4ee5\u4fbf\u73b0\u5728\u53ef\u4ee5\u4f7f\u7528\u5b83\uff0c\u4ee5\u540e\u53ef\u4ee5\u5b9a\u4e49\u5b83\u3002\u7136\u800c\uff0c\u4e0e\u51fd\u6570\u4e0d\u540c\u7684\u662f\uff0c\u7c7b\u6ca1\u6709\u8fd4\u56de\u7c7b\u578b\u6216\u53c2\u6570\uff0c\u56e0\u6b64\u7c7b\u539f\u578b\u603b\u662f\u7b80\u5355\u7684 class ClassName \uff0c\u5176\u4e2d ClassName \u662f\u7c7b\u7684\u540d\u79f0\u3002","title":"\u591a\u4e2a\u53cb\u5143"},{"location":"13-15-friend-functions-and-classes/#_3","text":"\u4e5f\u53ef\u4ee5\u8ba9\u6574\u4e2a\u7c7b\u6210\u4e3a\u53e6\u4e00\u4e2a\u7c7b\u7684\u53cb\u5143\u3002\u8fd9\u4f7f\u53cb\u5143\u7c7b\u7684\u6240\u6709\u6210\u5458\u90fd\u53ef\u4ee5\u8bbf\u95ee\u53e6\u4e00\u4e2a\u7c7b\u7684\u79c1\u6709\u6210\u5458\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include class Storage { private : int m_nValue {}; double m_dValue {}; public : Storage ( int nValue , double dValue ) : m_nValue { nValue }, m_dValue { dValue } { } // \u4f7f Display \u7c7b\u79f0\u4e3a Storage \u7684\u53cb\u5143 friend class Display ; }; class Display { private : bool m_displayIntFirst ; public : Display ( bool displayIntFirst ) : m_displayIntFirst { displayIntFirst } { } void displayItem ( const Storage & storage ) { if ( m_displayIntFirst ) std :: cout << storage . m_nValue << ' ' << storage . m_dValue << '\\n' ; else // display double first std :: cout << storage . m_dValue << ' ' << storage . m_nValue << '\\n' ; } }; int main () { Storage storage { 5 , 6.7 }; Display display { false }; display . displayItem ( storage ); return 0 ; } \u56e0\u4e3a Display \u7c7b\u662f Storage \u7684\u53cb\u5143\uff0c\u6240\u4ee5 Display \u7684\u4efb\u4f55\u6210\u5458\u90fd\u53ef\u4ee5 Storage \u7684\u79c1\u6709\u6210\u5458\u3002\u4e0a\u8ff0\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 6.7 5 \u6709\u5173\u53cb\u5143\u7c7b\uff0c\u8fd8\u6709\u4e00\u4e9b\u4e8b\u60c5\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0c\u5373\u4fbf Display \u662f Storage \u7684\u53cb\u5143\uff0c\u4f46\u662f Display \u5e76\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee Storage \u5bf9\u8c61\u7684 *this \u6307\u9488\u3002\u5176\u6b21\uff0c\u56e0\u4e3a Display \u662f Storage \u7684\u53cb\u5143\u7c7b\uff0c\u4f46\u662f\u8fd9\u4e0d\u4ee3\u8868 Storage \u4e5f\u662f Display \u7684\u53cb\u5143\u3002\u5982\u679c\u4f60\u5e0c\u671b\u4e24\u4e2a\u7c7b\u4e92\u4e3a\u53cb\u5143\uff0c\u963f\u4e48\u5fc5\u987b\u5206\u522b\u5728\u7c7b\u4e2d\u5c06\u5bf9\u65b9\u58f0\u660e\u4e3a\u53cb\u5143\u3002\u6700\u540e\uff0c\u5982\u679cA\u662fB\u7684\u53cb\u5143\uff0c\u800cB\u662fC\u7684\u53cb\u5143\uff0c\u4f46\u4e0d\u4ee3\u8868A\u662fC\u7684\u53cb\u5143\u3002 \u5728\u4f7f\u7528\u53cb\u51fd\u6570\u548c\u7c7b\u65f6\u8981\u5c0f\u5fc3\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u53cb\u51fd\u6570\u6216\u7c7b\u6253\u7834\u5c01\u88c5\u3002\u5982\u679c\u7c7b\u6539\u53d8\u4e86\uff0c\u53cb\u5143\u7684\u4e5f\u9700\u8981\u88ab\u8feb\u6539\u53d8\u3002\u56e0\u6b64\uff0c\u5c3d\u91cf\u51cf\u5c11\u5bf9\u53cb\u51fd\u6570\u548c\u53cb\u5143\u7c7b\u7684\u4f7f\u7528\u3002","title":"\u53cb\u5143\u7c7b"},{"location":"13-15-friend-functions-and-classes/#_4","text":"\u53ef\u4ee5\u5c06\u5355\u4e2a\u6210\u5458\u51fd\u6570\u8bbe\u4e3a\u53cb\u5143\uff0c\u800c\u4e0d\u662f\u5c06\u6574\u4e2a\u7c7b\u8bbe\u4e3a\u53cb\u5143\u3002\u8fd9\u4e0e\u5c06\u666e\u901a\u51fd\u6570\u8bbe\u4e3a\u53cb\u5143\u7c7b\u4f3c\uff0c\u53ea\u662f\u5728\u4f7f\u7528\u6210\u5458\u51fd\u6570\u7684\u540d\u79f0\u65f6\u5305\u542b\u4e86 className:: \u524d\u7f00\uff08\u4f8b\u5982 Display::displayItem )\u3002 \u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u6bd4\u9884\u671f\u7684\u8981\u590d\u6742\u4e00\u4e9b\u3002\u8ba9\u6211\u4eec\u6539\u5199\u4e00\u4e0b\u524d\u9762\u7684\u793a\u4f8b\uff0c\u4f7f Display::displayItem \u6210\u4e3a\u53cb\u5143\u6210\u5458\u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include class Display ; // Display \u7684\u524d\u5411\u58f0\u660e class Storage { private : int m_nValue {}; double m_dValue {}; public : Storage ( int nValue , double dValue ) : m_nValue { nValue }, m_dValue { dValue } { } // \u4f7f Display::displayItem \u6210\u5458\u51fd\u6570\u6210\u4e3a Storage \u7684\u53cb\u5143 friend void Display :: displayItem ( const Storage & storage ); // \u9519\u8bef: Storage \u6b64\u65f6\u5e76\u4e0d\u77e5\u9053 Display \u7c7b\u7684\u5b8c\u6574\u5b9a\u4e49\u3002 }; class Display { private : bool m_displayIntFirst {}; public : Display ( bool displayIntFirst ) : m_displayIntFirst { displayIntFirst } { } void displayItem ( const Storage & storage ) { if ( m_displayIntFirst ) std :: cout << storage . m_nValue << ' ' << storage . m_dValue << '\\n' ; else // display double first std :: cout << storage . m_dValue << ' ' << storage . m_nValue << '\\n' ; } }; \u5f88\u9057\u61be\uff0c\u8fd9\u4e48\u505a\u5e76\u4e0d\u80fd\u6b63\u786e\u5de5\u4f5c\u3002\u4e3a\u4e86\u8ba9\u4e00\u4e2a\u6210\u5458\u51fd\u6570\u79f0\u4e3a\u53cb\u5143\uff0c\u7f16\u8bd1\u5668\u5fc5\u987b\u770b\u5230\u8be5\u6210\u5458\u51fd\u6570\u7684\u5b8c\u6574\u5b9a\u4e49\u800c\u4e0d\u4ec5\u4ec5\u662f \u524d\u5411\u58f0\u660e \u3002\u56e0\u4e3a Storage \u8fd8\u6ca1\u6709\u770b\u5230 Display \u7684\u5b8c\u6574\u5b9a\u4e49\uff0c\u6240\u4ee5\u5f53\u6211\u4eec\u5728\u6b64\u5904\u58f0\u660e\u53cb\u5143\u51fd\u6570\u65f6\u4f1a\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ea\u8981\u5c06 Display \u7684\u5b9a\u4e49\u79fb\u52a8\u5230 Storage \u524d\u9762\u5373\u53ef\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include class Display { private : bool m_displayIntFirst {}; public : Display ( bool displayIntFirst ) : m_displayIntFirst { displayIntFirst } { } void displayItem ( const Storage & storage ) // error: compiler doesn't know what a Storage is { if ( m_displayIntFirst ) std :: cout << storage . m_nValue << ' ' << storage . m_dValue << '\\n' ; else // display double first std :: cout << storage . m_dValue << ' ' << storage . m_nValue << '\\n' ; } }; class Storage { private : int m_nValue {}; double m_dValue {}; public : Storage ( int nValue , double dValue ) : m_nValue { nValue }, m_dValue { dValue } { } // Make the Display::displayItem member function a friend of the Storage class friend void Display :: displayItem ( const Storage & storage ); // okay now }; \u4e0d\u8fc7\uff0c \u8fd9\u6837\u4e00\u6765\u4f1a\u5e26\u6765\u53e6\u5916\u7684\u95ee\u9898\uff0c\u56e0\u4e3a Display::displayItem() \u4f7f\u7528 Storage \u4f5c\u4e3a\u5f15\u7528\u53c2\u6570\u3002\u800c\u5f53\u6211\u4eec\u5c06 Storage \u7684\u5b9a\u4e49\u79fb\u52a8\u5230 Display \u540e\u9762\u65f6\uff0c \u7f16\u8bd1\u5668\u5c31\u4f1a\u62b1\u6028\u5b83\u4e0d\u77e5\u9053Storage \u662f\u4ec0\u4e48\uff0c\u6211\u4eec\u4e0d\u80fd\u518d\u8c03\u6574\u987a\u5e8f\u4e86\uff0c\u4e0d\u7136\u53c8\u4f1a\u51fa\u73b0\u4e4b\u524d\u7684\u95ee\u9898\u3002 \u5e78\u8fd0\u5730\u662f\uff0c\u53ea\u9700\u8981\u51e0\u6b65\u5c31\u53ef\u4ee5\u89e3\u51b3\u8be5\u95ee\u9898\u3002\u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u5bf9\u4e8e Storage \u7684\u524d\u5411\u58f0\u660e\u3002\u7b2c\u4e8c\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06 Display::displayItem() \u79fb\u52a8\u5230\u7c7b\u5916\u90e8\uff0c\u653e\u5728 Storage \u7c7b\u5b8c\u6574\u5b9a\u4e49\u7684\u540e\u3002 \u770b\u4e0a\u53bb\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 #include class Storage ; // Storage \u7684\u524d\u5411\u58f0\u660e class Display { private : bool m_displayIntFirst {}; public : Display ( bool displayIntFirst ) : m_displayIntFirst { displayIntFirst } { } void displayItem ( const Storage & storage ); // \u4e0a\u9762\u7684\u524d\u5411\u58f0\u660e\u5c31\u662f\u4e3a\u4e86\u8fd9\u4e00\u53e5\u58f0\u660e }; class Storage // Storage \u7c7b\u7684\u5b8c\u6574\u5b9a\u4e49 { private : int m_nValue {}; double m_dValue {}; public : Storage ( int nValue , double dValue ) : m_nValue { nValue }, m_dValue { dValue } { } // \u5c06 Display::displayItem \u58f0\u660e\u4e3a Storage \u7c7b\u7684\u53cb\u5143 (\u5fc5\u987b\u770b\u5230 Display \u7684\u5b8c\u6574\u5b9a\u4e49) friend void Display :: displayItem ( const Storage & storage ); }; // \u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49 Display::displayItem \u4e86\uff0c\u5b83\u5fc5\u987b\u770b\u5230 Storage \u7684\u5b8c\u6574\u5b9a\u4e49 void Display::displayItem ( const Storage & storage ) { if ( m_displayIntFirst ) std :: cout << storage . m_nValue << ' ' << storage . m_dValue << '\\n' ; else // display double first std :: cout << storage . m_dValue << ' ' << storage . m_nValue << '\\n' ; } int main () { Storage storage ( 5 , 6.7 ); Display display ( false ); display . displayItem ( storage ); return 0 ; } \u73b0\u5728\uff0c\u4ee3\u7801\u53ef\u4ee5\u6b63\u5e38\u7f16\u8bd1\u4e86\uff1a Storage \u7684\u524d\u5411\u58f0\u660e\u5bf9\u4e8e Display::displayItem() \u7684\u58f0\u660e\u6765\u8bf4\u8db3\u591f\u4e86\u3002 Display \u7684\u5b8c\u6574\u5b9a\u4e49\u53ef\u4ee5\u786e\u4fdd Display::displayItem() \u88ab\u5b9a\u4e49\u4e3a Storage \u7684\u53cb\u5143\u51fd\u6570\uff0c\u5982\u679c\u4f60\u8fd8\u662f\u611f\u5230\u56f0\u60d1\uff0c\u8bf7\u4ed4\u7ec6\u770b\u770b\u4e0a\u9762\u4ee3\u7801\u4e2d\u7684\u6ce8\u91ca\u3002 \u5982\u679c\u8fd9\u4ee4\u4f60\u611f\u5230\u56f0\u96be\u2014\u2014\u7684\u786e\u662f\u7684\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u8fd9\u4e48\u505a\u7684\u539f\u56e0\u53ea\u662f\u56e0\u4e3a\u6211\u4eec\u8bd5\u56fe\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u5b8c\u6210\u6240\u6709\u5de5\u4f5c\u3002\u66f4\u597d\u7684\u89e3\u51b3\u65b9\u6848\u662f\u5c06\u6bcf\u4e2a\u7c7b\u5b9a\u4e49\u653e\u5728\u5355\u72ec\u7684\u5934\u6587\u4ef6\u4e2d\uff0c\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u653e\u5728\u76f8\u5e94\u7684.cpp\u6587\u4ef6\u4e2d\u3002\u8fd9\u6837\uff0c\u6240\u6709\u7684\u7c7b\u5b9a\u4e49\u90fd\u5c06\u7acb\u5373\u5728.cpp\u6587\u4ef6\u4e2d\u53ef\u89c1\uff0c\u5e76\u4e14\u4e0d\u9700\u8981\u91cd\u65b0\u6392\u5217\u7c7b\u6216\u51fd\u6570!","title":"\u53cb\u5143\u6210\u5458\u51fd\u6570"},{"location":"13-15-friend-functions-and-classes/#_5","text":"\u53cb\u5143\u51fd\u6570\u6216\u53cb\u5143\u7c7b\u662f\u53ef\u4ee5\u8bbf\u95ee\u53e6\u4e00\u4e2a\u7c7b\u7684\u79c1\u6709\u6210\u5458\u7684\u51fd\u6570\u6216\u7c7b\uff0c\u5c31\u597d\u50cf\u5b83\u662f\u8be5\u7c7b\u7684\u6210\u5458\u4e00\u6837\u3002\u8fd9\u5141\u8bb8\u53cb\u5143\u51fd\u6570\u6216\u53cb\u5143\u7c7b\u4e0e\u53e6\u4e00\u4e2a\u7c7b\u7d27\u5bc6\u5730\u4e00\u8d77\u5de5\u4f5c\uff0c\u800c\u4e0d\u5fc5\u66b4\u9732\u4e00\u4e2a\u79c1\u6709\u6210\u5458(\u4f8b\u5982\u901a\u8fc7\u8bbf\u95ee\u51fd\u6570)\u3002 \u53cb\u5143\u901a\u5e38\u5728\u5b9a\u4e49\u91cd\u8f7d\u64cd\u4f5c\u7b26(\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u7ae0\u8ba8\u8bba)\u65f6\u4f7f\u7528\uff0c\u6216\u8005\uff08\u5c11\u6570\u65f6\u5019\uff09\u5728\u4e24\u4e2a\u6216\u591a\u4e2a\u7c7b\u9700\u8981\u4ee5\u4eb2\u5bc6\u7684\u65b9\u5f0f\u4e00\u8d77\u5de5\u4f5c\u65f6\u3002 \u6ce8\u610f\uff0c\u8981\u4f7f\u7279\u5b9a\u7684\u6210\u5458\u51fd\u6570\u6210\u4e3a\u53cb\u5143\uff0c\u9996\u5148\u9700\u8981\u770b\u5230\u6210\u5458\u51fd\u6570\u7c7b\u7684\u5b8c\u6574\u5b9a\u4e49\u3002","title":"\u5c0f\u7ed3"},{"location":"13-16-anonymous-objects/","tags":["class","friend"],"text":"\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ea\u9700\u8981\u4e00\u4e2a\u4e34\u65f6\u7684\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u8003\u8651\u4ee5\u4e0b\u60c5\u51b5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int add ( int x , int y ) { int sum { x + y }; return sum ; } int main () { std :: cout << add ( 5 , 3 ) << '\\n' ; return 0 ; } \u5728 add() \u51fd\u6570\u4e2d\uff0c\u53d8\u91cf sum \u53ea\u88ab\u5f53\u505a\u4e00\u4e2a\u4e34\u65f6\u7684\u5360\u4f4d\u7b26\u53d8\u91cf\u6765\u4f7f\u7528\u3002\u5b83\u5e76\u6ca1\u6709\u5176\u4ed6\u7279\u522b\u7684\u529f\u80fd\u2014\u2014\u5b83\u552f\u4e00\u7684\u529f\u80fd\u5c31\u662f\u4fdd\u5b58\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u5e76\u4f5c\u4e3a\u8fd4\u56de\u503c\u8fd4\u56de\u3002 \u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u533f\u540d\u5bf9\u8c61\u7f16\u5199\u4e00\u4e2a\u66f4\u52a0\u7b80\u5355\u7684 add() \u51fd\u6570\u3002 \u533f\u540d\u5bf9\u8c61 \u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u6ca1\u6709\u540d\u79f0\u7684\u503c\u3002\u56e0\u4e3a\u5b83\u4eec\u6ca1\u6709\u540d\u5b57\uff0c\u6240\u4ee5\u5728\u5b83\u4eec\u88ab\u521b\u5efa\u540e\uff0c\u6211\u4eec\u6ca1\u6709\u529e\u6cd5\u53bb\u5f15\u7528\u5b83\u4eec\u3002\u56e0\u6b64\uff0c\u533f\u540d\u5bf9\u8c61\u53ea\u80fd\u5177\u6709\u201c\u8868\u8fbe\u5f0f\u4f5c\u7528\u57df\u201d\uff0c\u5373\u5b83\u4eec\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u88ab\u521b\u5efa\u3001\u6c42\u503c\u548c\u9500\u6bc1\u3002 \u4e0b\u9762\u4f8b\u5b50\u4e2d\u7684 add() \u51fd\u6570\u4f7f\u7528\u4e86\u533f\u540d\u5bf9\u8c61\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int add ( int x , int y ) { return x + y ; // an anonymous object is created to hold and return the result of x + y } int main () { std :: cout << add ( 5 , 3 ) << '\\n' ; return 0 ; } \u5f53\u8868\u8fbe\u5f0f x + y \u6c42\u503c\u65f6\uff0c\u5b83\u7684\u7ed3\u679c\u4f1a\u88ab\u5b58\u653e\u5230\u4e00\u4e2a\u533f\u540d\u5bf9\u8c61\u4e2d\u3002\u7136\u540e\uff0c\u8be5\u5bf9\u8c61\u7684\u4e00\u4e2a\u62f7\u8d1d\u4f1a\u88ab\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\uff0c\u7136\u540e\u8fd9\u4e2a\u533f\u540d\u5bf9\u8c61\u5c31\u4f1a\u88ab\u9500\u6bc1\u3002 \u8fd9\u4e0d\u4ec5\u9002\u7528\u4e8e\u8fd4\u56de\u503c\uff0c\u4e5f\u9002\u7528\u4e8e\u51fd\u6570\u5f62\u53c2\u3002\u4f8b\u5982\uff0c\u4e0e\u5176\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include void printValue ( int value ) { std :: cout << value ; } int main () { int sum { 5 + 3 }; printValue ( sum ); return 0 ; } \u4e0d\u5982\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void printValue ( int value ) { std :: cout << value ; } int main () { printValue ( 5 + 3 ); return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8868\u8fbe\u5f0f 5+3 \u88ab\u6c42\u503c\u4e3a 8 \uff0c\u7136\u540e\u7ed3\u679c\u88ab\u5b58\u653e\u5728\u533f\u540d\u5bf9\u8c61\u65f6\u3002\u8fd9\u4e2a\u533f\u540d\u5bf9\u8c61\u7684\u62f7\u8d1d\u968f\u540e\u88ab\u4f20\u9012\u7ed9 printValue() \u51fd\u6570\uff0c\u7136\u540e\u88ab\u9500\u6bc1\u3002 \u6ce8\u610f\uff0c\u8fd9\u4f7f\u6211\u4eec\u7684\u4ee3\u7801\u53d8\u5f97\u975e\u5e38\u7b80\u6d01\u2014\u2014\u6211\u4eec\u4e0d\u5fc5\u7528\u53ea\u4f7f\u7528\u4e00\u6b21\u7684\u4e34\u65f6\u53d8\u91cf\u6765\u5f04\u4e71\u4ee3\u7801\u3002 \u533f\u540d\u7c7b\u5bf9\u8c61 \u5c3d\u7ba1\u6211\u4eec\u524d\u9762\u7684\u4f8b\u5b50\u90fd\u662f\u5185\u7f6e\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u662f\u6211\u4eec\u4e5f\u53ef\u4ee5\u6784\u9020\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u533f\u540d\u5bf9\u8c61\u3002\u65b9\u6cd5\u548c\u521b\u5efa\u666e\u901a\u5bf9\u8c61\u7c7b\u4f3c\uff0c\u53ea\u9700\u8981\u7701\u7565\u53d8\u91cf\u540d\u5373\u53ef\u3002 1 2 Cents cents { 5 }; // \u666e\u901a\u53d8\u91cf Cents { 7 }; // \u533f\u540d\u5bf9\u8c61 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c Cents{ 7 } \u4f1a\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u5bf9\u8c61\u5e76\u4f7f\u75287\u4f5c\u4e3a\u5176\u521d\u59cb\u503c\uff0c\u7136\u540e\u8be5\u5bf9\u8c61\u5c31\u88ab\u9500\u6bc1\u4e86\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u533f\u540d\u5bf9\u8c61\u770b\u4e0a\u53bb\u6ca1\u4ec0\u4e48\u7528\u3002\u8ba9\u6211\u4eec\u770b\u53e6\u4e00\u4e2a\u4f8b\u5b50\u5427\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; void print ( const Cents & cents ) { std :: cout << cents . getCents () << \" cents \\n \" ; } int main () { Cents cents { 6 }; print ( cents ); return 0 ; } \u6ce8\u610f\uff0c\u6b64\u793a\u4f8b\u4e0e\u524d\u9762\u4f7f\u7528\u6574\u6570\u7684\u793a\u4f8b\u975e\u5e38\u76f8\u4f3c\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u7684 main() \u51fd\u6570\u5411 print() \u51fd\u6570\u4f20\u9012\u4e86\u4e00\u4e2a Cents \u7c7b\u578b\u7684\u5bf9\u8c61(\u540d\u4e3acents)\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u533f\u540d\u5bf9\u8c61\u6765\u7b80\u5316\u8fd9\u4e2a\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; void print ( const Cents & cents ) { std :: cout << cents . getCents () << \" cents \\n \" ; } int main () { print ( Cents { 6 }); // Note: Now we're passing an anonymous Cents value return 0 ; } \u4f60\u53ef\u80fd\u5e0c\u671b\u7a0b\u5e8f\u6253\u5370\u5982\u4e0b\u5185\u5bb9\uff1a 1 6 cents \u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u518d\u770b\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; Cents add ( const Cents & c1 , const Cents & c2 ) { Cents sum { c1 . getCents () + c2 . getCents () }; return sum ; } int main () { Cents cents1 { 6 }; Cents cents2 { 8 }; Cents sum { add ( cents1 , cents2 ) }; std :: cout << \"I have \" << sum . getCents () << \" cents. \\n \" ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u5f88\u591a\u547d\u540d\u4e3aCents\u7684\u503c\u3002\u5728 add() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a sum \u7684 Cents \u5bf9\u8c61\uff0c\u6211\u4eec\u4f7f\u7528\u5b83\u4f5c\u4e3a\u4e2d\u95f4\u503c\uff0c\u5728\u8fd4\u56de\u00b7\u4e4b\u524d\u4fdd\u5b58\u5b83\u3002\u5728\u51fd\u6570 main() \u4e2d\uff0c\u6211\u4eec\u6709\u53e6\u4e00\u4e2a\u540d\u4e3a sum \u7684 Cents \u5bf9\u8c61\uff0c\u4e5f\u7528\u4f5c\u4e2d\u95f4\u503c\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u533f\u540d\u503c\u6765\u7b80\u5316\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; Cents add ( const Cents & c1 , const Cents & c2 ) { // \u5217\u8868\u521d\u59cb\u5316\u4f1a\u53c2\u8003\u51fd\u6570\u8fd4\u56de\u503c\u7684\u7c7b\u578b // \u7136\u540e\u521b\u5efa\u6240\u9700\u7684\u5bf9\u8c61 return { c1 . getCents () + c2 . getCents () }; // \u8fd4\u56de\u533f\u540d\u7684 Cents value } int main () { Cents cents1 { 6 }; Cents cents2 { 8 }; std :: cout << \"I have \" << add ( cents1 , cents2 ). getCents () << \" cents. \\n \" ; // print anonymous Cents value return 0 ; } \u8fd9\u4e2a\u7248\u672c\u7684 add() \u51fd\u6570\u4e0e\u4e0a\u9762\u7684\u51fd\u6570\u76f8\u540c\uff0c\u53ea\u662f\u5b83\u4f7f\u7528\u4e86\u4e00\u4e2a\u533f\u540d\u7684Cents\u503c\u800c\u4e0d\u662f\u4e00\u4e2a\u547d\u540d\u53d8\u91cf\u3002\u8fd8\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728 main() \u4e2d\uff0c\u6211\u4eec\u4e0d\u518d\u4f7f\u7528\u547d\u540d\u7684 sum \u53d8\u91cf\u4f5c\u4e3a\u4e34\u65f6\u5b58\u50a8\u3002\u76f8\u53cd\uff0c\u6211\u4eec\u4f1a\u533f\u540d\u4f7f\u7528 add() \u7684\u8fd4\u56de\u503c\uff01 \u56e0\u6b64\uff0c\u6211\u4eec\u7684\u7a0b\u5e8f\u66f4\u77ed\u3001\u66f4\u6e05\u6670\uff0c\u800c\u4e14\u901a\u5e38\u66f4\u5bb9\u6613\u7406\u89e3(\u4e00\u65e6\u4f60\u7406\u89e3\u4e86\u8fd9\u4e2a\u6982\u5ff5)\u3002 \u4e8b\u5b9e\u4e0a\uff0c\u56e0\u4e3acent1\u548ccent2\u53ea\u5728\u4e00\u4e2a\u5730\u65b9\u4f7f\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u8fdb\u4e00\u6b65\u533f\u540d\u5316: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; Cents add ( const Cents & c1 , const Cents & c2 ) { return { c1 . getCents () + c2 . getCents () }; // return anonymous Cents value } int main () { std :: cout << \"I have \" << add ( Cents { 6 }, Cents { 8 }). getCents () << \" cents. \\n \" ; // print anonymous Cents value return 0 ; } \u5c0f\u7ed3 \u5728C++\u4e2d\uff0c\u533f\u540d\u5bf9\u8c61\u4e3b\u8981\u7528\u4e8e\u4f20\u9012\u53c2\u6570\u6216\u8fd4\u56de\u503c\u800c\u4e0d\u5fc5\u4e3a\u6b64\u521b\u5efa\u5927\u91cf\u4e34\u65f6\u53d8\u91cf\u3002\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u4e5f\u662f\u533f\u540d\u5b8c\u6210\u7684(\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u5b83\u7684\u5730\u5740\u5fc5\u987b\u5206\u914d\u7ed9\u6307\u9488\uff0c\u5426\u5219\u6211\u4eec\u5c31\u6ca1\u6709\u529e\u6cd5\u5f15\u7528\u5b83)\u3002 \u53e6\u5916\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u56e0\u4e3a\u533f\u540d\u5bf9\u8c61\u5177\u6709\u8868\u8fbe\u5f0f\u4f5c\u7528\u57df\uff0c\u6240\u4ee5\u5b83\u4eec\u53ea\u80fd\u4f7f\u7528\u4e00\u6b21(\u9664\u975e\u7ed1\u5b9a\u5230\u4e00\u4e2a\u56fa\u5b9a\u7684 \u5de6\u503c\u5f15\u7528 \uff0c\u8fd9\u5c06\u5ef6\u957f\u4e34\u65f6\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\uff0c\u4ee5\u5339\u914d\u5f15\u7528\u7684\u751f\u547d\u5468\u671f)\u3002\u5982\u679c\u9700\u8981\u5728\u591a\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u5f15\u7528\u4e00\u4e2a\u503c\uff0c\u5219\u5e94\u8be5\u4f7f\u7528\u547d\u540d\u53d8\u91cf\u3002","title":"13.16 - \u533f\u540d\u5bf9\u8c61"},{"location":"13-16-anonymous-objects/#_1","text":"\u5c3d\u7ba1\u6211\u4eec\u524d\u9762\u7684\u4f8b\u5b50\u90fd\u662f\u5185\u7f6e\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u662f\u6211\u4eec\u4e5f\u53ef\u4ee5\u6784\u9020\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u533f\u540d\u5bf9\u8c61\u3002\u65b9\u6cd5\u548c\u521b\u5efa\u666e\u901a\u5bf9\u8c61\u7c7b\u4f3c\uff0c\u53ea\u9700\u8981\u7701\u7565\u53d8\u91cf\u540d\u5373\u53ef\u3002 1 2 Cents cents { 5 }; // \u666e\u901a\u53d8\u91cf Cents { 7 }; // \u533f\u540d\u5bf9\u8c61 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c Cents{ 7 } \u4f1a\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u5bf9\u8c61\u5e76\u4f7f\u75287\u4f5c\u4e3a\u5176\u521d\u59cb\u503c\uff0c\u7136\u540e\u8be5\u5bf9\u8c61\u5c31\u88ab\u9500\u6bc1\u4e86\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u533f\u540d\u5bf9\u8c61\u770b\u4e0a\u53bb\u6ca1\u4ec0\u4e48\u7528\u3002\u8ba9\u6211\u4eec\u770b\u53e6\u4e00\u4e2a\u4f8b\u5b50\u5427\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; void print ( const Cents & cents ) { std :: cout << cents . getCents () << \" cents \\n \" ; } int main () { Cents cents { 6 }; print ( cents ); return 0 ; } \u6ce8\u610f\uff0c\u6b64\u793a\u4f8b\u4e0e\u524d\u9762\u4f7f\u7528\u6574\u6570\u7684\u793a\u4f8b\u975e\u5e38\u76f8\u4f3c\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u7684 main() \u51fd\u6570\u5411 print() \u51fd\u6570\u4f20\u9012\u4e86\u4e00\u4e2a Cents \u7c7b\u578b\u7684\u5bf9\u8c61(\u540d\u4e3acents)\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u533f\u540d\u5bf9\u8c61\u6765\u7b80\u5316\u8fd9\u4e2a\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; void print ( const Cents & cents ) { std :: cout << cents . getCents () << \" cents \\n \" ; } int main () { print ( Cents { 6 }); // Note: Now we're passing an anonymous Cents value return 0 ; } \u4f60\u53ef\u80fd\u5e0c\u671b\u7a0b\u5e8f\u6253\u5370\u5982\u4e0b\u5185\u5bb9\uff1a 1 6 cents \u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u518d\u770b\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; Cents add ( const Cents & c1 , const Cents & c2 ) { Cents sum { c1 . getCents () + c2 . getCents () }; return sum ; } int main () { Cents cents1 { 6 }; Cents cents2 { 8 }; Cents sum { add ( cents1 , cents2 ) }; std :: cout << \"I have \" << sum . getCents () << \" cents. \\n \" ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u5f88\u591a\u547d\u540d\u4e3aCents\u7684\u503c\u3002\u5728 add() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a sum \u7684 Cents \u5bf9\u8c61\uff0c\u6211\u4eec\u4f7f\u7528\u5b83\u4f5c\u4e3a\u4e2d\u95f4\u503c\uff0c\u5728\u8fd4\u56de\u00b7\u4e4b\u524d\u4fdd\u5b58\u5b83\u3002\u5728\u51fd\u6570 main() \u4e2d\uff0c\u6211\u4eec\u6709\u53e6\u4e00\u4e2a\u540d\u4e3a sum \u7684 Cents \u5bf9\u8c61\uff0c\u4e5f\u7528\u4f5c\u4e2d\u95f4\u503c\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u533f\u540d\u503c\u6765\u7b80\u5316\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; Cents add ( const Cents & c1 , const Cents & c2 ) { // \u5217\u8868\u521d\u59cb\u5316\u4f1a\u53c2\u8003\u51fd\u6570\u8fd4\u56de\u503c\u7684\u7c7b\u578b // \u7136\u540e\u521b\u5efa\u6240\u9700\u7684\u5bf9\u8c61 return { c1 . getCents () + c2 . getCents () }; // \u8fd4\u56de\u533f\u540d\u7684 Cents value } int main () { Cents cents1 { 6 }; Cents cents2 { 8 }; std :: cout << \"I have \" << add ( cents1 , cents2 ). getCents () << \" cents. \\n \" ; // print anonymous Cents value return 0 ; } \u8fd9\u4e2a\u7248\u672c\u7684 add() \u51fd\u6570\u4e0e\u4e0a\u9762\u7684\u51fd\u6570\u76f8\u540c\uff0c\u53ea\u662f\u5b83\u4f7f\u7528\u4e86\u4e00\u4e2a\u533f\u540d\u7684Cents\u503c\u800c\u4e0d\u662f\u4e00\u4e2a\u547d\u540d\u53d8\u91cf\u3002\u8fd8\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728 main() \u4e2d\uff0c\u6211\u4eec\u4e0d\u518d\u4f7f\u7528\u547d\u540d\u7684 sum \u53d8\u91cf\u4f5c\u4e3a\u4e34\u65f6\u5b58\u50a8\u3002\u76f8\u53cd\uff0c\u6211\u4eec\u4f1a\u533f\u540d\u4f7f\u7528 add() \u7684\u8fd4\u56de\u503c\uff01 \u56e0\u6b64\uff0c\u6211\u4eec\u7684\u7a0b\u5e8f\u66f4\u77ed\u3001\u66f4\u6e05\u6670\uff0c\u800c\u4e14\u901a\u5e38\u66f4\u5bb9\u6613\u7406\u89e3(\u4e00\u65e6\u4f60\u7406\u89e3\u4e86\u8fd9\u4e2a\u6982\u5ff5)\u3002 \u4e8b\u5b9e\u4e0a\uff0c\u56e0\u4e3acent1\u548ccent2\u53ea\u5728\u4e00\u4e2a\u5730\u65b9\u4f7f\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u8fdb\u4e00\u6b65\u533f\u540d\u5316: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; Cents add ( const Cents & c1 , const Cents & c2 ) { return { c1 . getCents () + c2 . getCents () }; // return anonymous Cents value } int main () { std :: cout << \"I have \" << add ( Cents { 6 }, Cents { 8 }). getCents () << \" cents. \\n \" ; // print anonymous Cents value return 0 ; }","title":"\u533f\u540d\u7c7b\u5bf9\u8c61"},{"location":"13-16-anonymous-objects/#_2","text":"\u5728C++\u4e2d\uff0c\u533f\u540d\u5bf9\u8c61\u4e3b\u8981\u7528\u4e8e\u4f20\u9012\u53c2\u6570\u6216\u8fd4\u56de\u503c\u800c\u4e0d\u5fc5\u4e3a\u6b64\u521b\u5efa\u5927\u91cf\u4e34\u65f6\u53d8\u91cf\u3002\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u4e5f\u662f\u533f\u540d\u5b8c\u6210\u7684(\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u5b83\u7684\u5730\u5740\u5fc5\u987b\u5206\u914d\u7ed9\u6307\u9488\uff0c\u5426\u5219\u6211\u4eec\u5c31\u6ca1\u6709\u529e\u6cd5\u5f15\u7528\u5b83)\u3002 \u53e6\u5916\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u56e0\u4e3a\u533f\u540d\u5bf9\u8c61\u5177\u6709\u8868\u8fbe\u5f0f\u4f5c\u7528\u57df\uff0c\u6240\u4ee5\u5b83\u4eec\u53ea\u80fd\u4f7f\u7528\u4e00\u6b21(\u9664\u975e\u7ed1\u5b9a\u5230\u4e00\u4e2a\u56fa\u5b9a\u7684 \u5de6\u503c\u5f15\u7528 \uff0c\u8fd9\u5c06\u5ef6\u957f\u4e34\u65f6\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\uff0c\u4ee5\u5339\u914d\u5f15\u7528\u7684\u751f\u547d\u5468\u671f)\u3002\u5982\u679c\u9700\u8981\u5728\u591a\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u5f15\u7528\u4e00\u4e2a\u503c\uff0c\u5219\u5e94\u8be5\u4f7f\u7528\u547d\u540d\u53d8\u91cf\u3002","title":"\u5c0f\u7ed3"},{"location":"13-17-nested-types-in-classes/","tags":["class","friend"],"text":"\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7b80\u77ed\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include enum class FruitType { apple , banana , cherry }; class Fruit { private : FruitType m_type {}; int m_percentageEaten { 0 }; public : Fruit ( FruitType type ) : m_type { type } { } FruitType getType () const { return m_type ; } int getPercentageEaten () const { return m_percentageEaten ; } }; int main () { Fruit apple { FruitType :: apple }; if ( apple . getType () == FruitType :: apple ) std :: cout << \"I am an apple\" ; else std :: cout << \"I am not an apple\" ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u5e76\u6ca1\u6709\u4efb\u4f55\u95ee\u9898\u3002\u4f46\u662f\u56e0\u4e3a \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e \uff08\u679a\u4e3e\u7c7b\uff09 FruitType \u53ea\u4f1a\u5728 Fruit \u7c7b\u4e2d\u4f7f\u7528\uff0c\u6240\u4ee5\u5c06\u5b83\u5b9a\u4e49\u5728\u7c7b\u5916\u4f5c\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684\u679a\u4e3e\u7c7b\u591a\u5c11\u6709\u4e9b\u4e0d\u59a5\u3002 \u5d4c\u5957\u7c7b\u578b \u6570\u636e\u548c\u51fd\u6570\u53ef\u4ee5\u79f0\u4e3a\u7c7b\u7684\u6210\u5458\uff0c\u5728C++\u4e2d\uff0c\u7c7b\u578b\u4e5f\u53ef\u4ee5\u88ab\u5d4c\u5957\u5730\u5b9a\u4e49\u5728\u7c7b\u5185\u3002\u4e3a\u6b64\uff0c\u4f60\u53ea\u9700\u8981\u5c06\u7c7b\u578b\u5b9a\u4e49\u5728\u5185\u5408\u9002\u7684 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u4e0b\u65b9\u5373\u53ef\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u7c7b\u4f3c\u4e8e\u4e4b\u524d\u7684\u4f8b\u5b50\uff0c\u53ea\u4e0d\u8fc7\u679a\u4e3e\u88ab\u5b9a\u4e49\u5728\u4e86\u7c7b\u5185\uff08\u6b64\u5904\u4f7f\u7528\u4e86 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include class Fruit { public : // \u6ce8\u610f\uff1aFruitType \u88ab\u79fb\u52a8\u5230\u4e86\u7c7b\u4e2d\uff0c\u4f4d\u4e8epubic\u90e8\u5206 // \u540c\u65f6\u6211\u4eec\u5c06\u679a\u4e3e\u7c7b\u53d8\u6210\u4e86\u679a\u4e3e enum FruitType { apple , banana , cherry }; private : FruitType m_type {}; int m_percentageEaten { 0 }; public : Fruit ( FruitType type ) : m_type { type } { } FruitType getType () const { return m_type ; } int getPercentageEaten () const { return m_percentageEaten ; } }; int main () { // \u6ce8\u610f\uff0c\u6211\u4eec\u6b64\u5904\u662f\u901a\u8fc7 Fruit \u6765\u8bbf\u95ee FruitType Fruit apple { Fruit :: apple }; if ( apple . getType () == Fruit :: apple ) std :: cout << \"I am an apple\" ; else std :: cout << \"I am not an apple\" ; return 0 ; } \u9996\u5148\uff0c FruitType \u7684\u5b9a\u4e49\u73b0\u5728\u4f4d\u4e8e\u7c7b\u5185\u3002\u5176\u6b21\uff0c\u6211\u4eec\u5c06\u5176\u5b9a\u4e49\u4e3apublic\uff0c\u4ee5\u4fbf\u80fd\u591f\u4ece\u7c7b\u7684\u5916\u90e8\u8bbf\u95ee\u5b83\u3002 \u6b64\u65f6\u7684\u7c7b\u540d\u5b9e\u9645\u4e0a\u5145\u5f53\u7740\u6240\u6709\u5d4c\u5957\u7c7b\u578b\u7684 \u547d\u540d\u7a7a\u95f4 \uff0c\u548c\u4e48\u679a\u4e3e\u7c7b\u6709\u4e9b\u7c7b\u4f3c\u3002\u5728\u4e4b\u524d\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528\u4e86\u679a\u4e3e\u7c7b\uff0c\u6240\u4ee5\u5728\u4f7f\u7528\u679a\u4e3e\u65f6\u5fc5\u987b\u6dfb\u52a0 FruitType:: \u4f5c\u7528\u57df\u9650\u5b9a\u7b26\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a FruitType \u662f\u4e00\u4e2a\u666e\u901a\u7684\u679a\u4e3e\uff0c\u4f46\u662f\u5b83\u4f5c\u4e3a\u8be5\u7c7b\u7684\u4e00\u90e8\u5206\uff0c\u5728\u8bbf\u95ee\u65f6\u4e5f\u5fc5\u987b\u6dfb\u52a0 FruitType:: \u4f5c\u7528\u57df\u9650\u5b9a\u7b26\u3002 \u6ce8\u610f\uff0c\u56e0\u4e3a\u679a\u4e3e\u7c7b\u4e5f\u53ef\u4ee5\u5145\u5f53\u547d\u540d\u7a7a\u95f4\uff0c\u6240\u4ee5\u5982\u679c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u679a\u4e3e\u7c7b\u800c\u4e0d\u662f\u679a\u4e3e\uff0c\u5219\u5728\u8bbf\u95ee\u65f6\u5fc5\u987b\u6dfb\u52a0 Fruit::FruitType:: \u4f5c\u7528\u57df\u9650\u5b9a\u7b26\u3002\u8fd9\u5b9e\u5728\u662f\u6ca1\u6709\u4ee5\uff0c\u6240\u4ee5\u6211\u4eec\u9009\u62e9\u4f7f\u7528\u666e\u901a\u679a\u4e3e\u3002 \u5176\u4ed6\u53ef\u5d4c\u5957\u7c7b\u578b \u867d\u7136\u679a\u4e3e\u53ef\u80fd\u662f\u6700\u5e38\u88ab\u5d4c\u5957\u5728\u7c7b\u4e2d\u7684\u7c7b\u578b\uff0c\u4f46C++\u5141\u8bb8\u4f60\u5728\u7c7b\u4e2d\u5b9a\u4e49\u5176\u4ed6\u7c7b\u578b\uff0c\u4f8b\u5982\u7c7b\u578b\u5b9a\u4e49\u3001\u7c7b\u578b\u522b\u540d\uff0c\u751a\u81f3\u5176\u4ed6\u7c7b! \u4e0e\u7c7b\u7684\u4efb\u4f55\u666e\u901a\u6210\u5458\u4e00\u6837\uff0c\u5d4c\u5957\u7c7b\u5bf9\u5916\u56f4\u7c7b\u7684\u6210\u5458\u5177\u6709\u4e0e\u5916\u56f4\u7c7b\u76f8\u540c\u7684\u8bbf\u95ee\u6743\u9650\u3002\u7136\u800c\uff0c\u5d4c\u5957\u7c7b\u5bf9\u5916\u56f4\u7c7b\u7684\u201cthis\u201d\u6307\u9488\u6ca1\u6709\u4efb\u4f55\u7279\u6b8a\u7684\u8bbf\u95ee\u6743\u9650\u3002 \u5d4c\u5957\u7c7b\u578b\u7684\u53e6\u4e00\u4e2a\u9650\u5236\u662f\u5b83\u4eec\u4e0d\u80fd\u88ab \u524d\u5411\u58f0\u660e \u3002\u7136\u800c\uff0c\u5728\u5b9e\u8df5\u4e2d\u8fd9\u5f88\u5c11\u4f1a\u5e26\u6765\u95ee\u9898\uff0c\u56e0\u4e3a\u6574\u4e2a\u7c7b\u5b9a\u4e49(\u5305\u62ec\u5d4c\u5957\u7c7b\u578b)\u901a\u5e38\u53ef\u4ee5\u5728\u9700\u8981\u7684\u5730\u65b9\u4f7f\u7528\u88ab\u201c #include \u201d\u3002 \u5b9a\u4e49\u5d4c\u5957\u7c7b\u5e76\u4e0d\u5e38\u89c1\uff0c\u4f46C++\u6807\u51c6\u5e93\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u786e\u5b9e\u4f1a\u8fd9\u6837\u505a\uff0c\u6bd4\u5982\u8fed\u4ee3\u5668\u7c7b\u3002","title":"13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b"},{"location":"13-17-nested-types-in-classes/#_1","text":"\u6570\u636e\u548c\u51fd\u6570\u53ef\u4ee5\u79f0\u4e3a\u7c7b\u7684\u6210\u5458\uff0c\u5728C++\u4e2d\uff0c\u7c7b\u578b\u4e5f\u53ef\u4ee5\u88ab\u5d4c\u5957\u5730\u5b9a\u4e49\u5728\u7c7b\u5185\u3002\u4e3a\u6b64\uff0c\u4f60\u53ea\u9700\u8981\u5c06\u7c7b\u578b\u5b9a\u4e49\u5728\u5185\u5408\u9002\u7684 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u4e0b\u65b9\u5373\u53ef\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u7c7b\u4f3c\u4e8e\u4e4b\u524d\u7684\u4f8b\u5b50\uff0c\u53ea\u4e0d\u8fc7\u679a\u4e3e\u88ab\u5b9a\u4e49\u5728\u4e86\u7c7b\u5185\uff08\u6b64\u5904\u4f7f\u7528\u4e86 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include class Fruit { public : // \u6ce8\u610f\uff1aFruitType \u88ab\u79fb\u52a8\u5230\u4e86\u7c7b\u4e2d\uff0c\u4f4d\u4e8epubic\u90e8\u5206 // \u540c\u65f6\u6211\u4eec\u5c06\u679a\u4e3e\u7c7b\u53d8\u6210\u4e86\u679a\u4e3e enum FruitType { apple , banana , cherry }; private : FruitType m_type {}; int m_percentageEaten { 0 }; public : Fruit ( FruitType type ) : m_type { type } { } FruitType getType () const { return m_type ; } int getPercentageEaten () const { return m_percentageEaten ; } }; int main () { // \u6ce8\u610f\uff0c\u6211\u4eec\u6b64\u5904\u662f\u901a\u8fc7 Fruit \u6765\u8bbf\u95ee FruitType Fruit apple { Fruit :: apple }; if ( apple . getType () == Fruit :: apple ) std :: cout << \"I am an apple\" ; else std :: cout << \"I am not an apple\" ; return 0 ; } \u9996\u5148\uff0c FruitType \u7684\u5b9a\u4e49\u73b0\u5728\u4f4d\u4e8e\u7c7b\u5185\u3002\u5176\u6b21\uff0c\u6211\u4eec\u5c06\u5176\u5b9a\u4e49\u4e3apublic\uff0c\u4ee5\u4fbf\u80fd\u591f\u4ece\u7c7b\u7684\u5916\u90e8\u8bbf\u95ee\u5b83\u3002 \u6b64\u65f6\u7684\u7c7b\u540d\u5b9e\u9645\u4e0a\u5145\u5f53\u7740\u6240\u6709\u5d4c\u5957\u7c7b\u578b\u7684 \u547d\u540d\u7a7a\u95f4 \uff0c\u548c\u4e48\u679a\u4e3e\u7c7b\u6709\u4e9b\u7c7b\u4f3c\u3002\u5728\u4e4b\u524d\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528\u4e86\u679a\u4e3e\u7c7b\uff0c\u6240\u4ee5\u5728\u4f7f\u7528\u679a\u4e3e\u65f6\u5fc5\u987b\u6dfb\u52a0 FruitType:: \u4f5c\u7528\u57df\u9650\u5b9a\u7b26\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a FruitType \u662f\u4e00\u4e2a\u666e\u901a\u7684\u679a\u4e3e\uff0c\u4f46\u662f\u5b83\u4f5c\u4e3a\u8be5\u7c7b\u7684\u4e00\u90e8\u5206\uff0c\u5728\u8bbf\u95ee\u65f6\u4e5f\u5fc5\u987b\u6dfb\u52a0 FruitType:: \u4f5c\u7528\u57df\u9650\u5b9a\u7b26\u3002 \u6ce8\u610f\uff0c\u56e0\u4e3a\u679a\u4e3e\u7c7b\u4e5f\u53ef\u4ee5\u5145\u5f53\u547d\u540d\u7a7a\u95f4\uff0c\u6240\u4ee5\u5982\u679c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u679a\u4e3e\u7c7b\u800c\u4e0d\u662f\u679a\u4e3e\uff0c\u5219\u5728\u8bbf\u95ee\u65f6\u5fc5\u987b\u6dfb\u52a0 Fruit::FruitType:: \u4f5c\u7528\u57df\u9650\u5b9a\u7b26\u3002\u8fd9\u5b9e\u5728\u662f\u6ca1\u6709\u4ee5\uff0c\u6240\u4ee5\u6211\u4eec\u9009\u62e9\u4f7f\u7528\u666e\u901a\u679a\u4e3e\u3002","title":"\u5d4c\u5957\u7c7b\u578b"},{"location":"13-17-nested-types-in-classes/#_2","text":"\u867d\u7136\u679a\u4e3e\u53ef\u80fd\u662f\u6700\u5e38\u88ab\u5d4c\u5957\u5728\u7c7b\u4e2d\u7684\u7c7b\u578b\uff0c\u4f46C++\u5141\u8bb8\u4f60\u5728\u7c7b\u4e2d\u5b9a\u4e49\u5176\u4ed6\u7c7b\u578b\uff0c\u4f8b\u5982\u7c7b\u578b\u5b9a\u4e49\u3001\u7c7b\u578b\u522b\u540d\uff0c\u751a\u81f3\u5176\u4ed6\u7c7b! \u4e0e\u7c7b\u7684\u4efb\u4f55\u666e\u901a\u6210\u5458\u4e00\u6837\uff0c\u5d4c\u5957\u7c7b\u5bf9\u5916\u56f4\u7c7b\u7684\u6210\u5458\u5177\u6709\u4e0e\u5916\u56f4\u7c7b\u76f8\u540c\u7684\u8bbf\u95ee\u6743\u9650\u3002\u7136\u800c\uff0c\u5d4c\u5957\u7c7b\u5bf9\u5916\u56f4\u7c7b\u7684\u201cthis\u201d\u6307\u9488\u6ca1\u6709\u4efb\u4f55\u7279\u6b8a\u7684\u8bbf\u95ee\u6743\u9650\u3002 \u5d4c\u5957\u7c7b\u578b\u7684\u53e6\u4e00\u4e2a\u9650\u5236\u662f\u5b83\u4eec\u4e0d\u80fd\u88ab \u524d\u5411\u58f0\u660e \u3002\u7136\u800c\uff0c\u5728\u5b9e\u8df5\u4e2d\u8fd9\u5f88\u5c11\u4f1a\u5e26\u6765\u95ee\u9898\uff0c\u56e0\u4e3a\u6574\u4e2a\u7c7b\u5b9a\u4e49(\u5305\u62ec\u5d4c\u5957\u7c7b\u578b)\u901a\u5e38\u53ef\u4ee5\u5728\u9700\u8981\u7684\u5730\u65b9\u4f7f\u7528\u88ab\u201c #include \u201d\u3002 \u5b9a\u4e49\u5d4c\u5957\u7c7b\u5e76\u4e0d\u5e38\u89c1\uff0c\u4f46C++\u6807\u51c6\u5e93\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u786e\u5b9e\u4f1a\u8fd9\u6837\u505a\uff0c\u6bd4\u5982\u8fed\u4ee3\u5668\u7c7b\u3002","title":"\u5176\u4ed6\u53ef\u5d4c\u5957\u7c7b\u578b"},{"location":"13-18-timing-your-code/","tags":["class","friend"],"text":"\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5224\u65ad\u4e00\u4e2a\u65b9\u6cd5\u7684\u6027\u80fd\u662f\u5426\u6ee1\u8db3\u8981\u6c42\u3002\u6b64\u65f6\u5e94\u8be5\u5982\u4f55\u5224\u65ad\u5462\uff1f \u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u8ba1\u7b97\u4ee3\u7801\u7684\u8fd0\u884c\u65f6\u95f4\u3002C++ 11\u5728 chrono \u5e93\u4e2d\u63d0\u4f9b\u4e86\u4e00\u4e9b\u529f\u80fd\u7528\u4e8e\u5b9e\u73b0\u8ba1\u65f6\u3002\u7136\u800c\uff0c\u4f7f\u7528 chrono \u5e93\u6709\u70b9\u590d\u6742\uff0c\u4e0d\u8fc7\u6211\u4eec\u53ef\u4ee5\u5c06\u6240\u6709\u9700\u8981\u7684\u8ba1\u65f6\u529f\u80fd\u5c01\u88c5\u5230\u4e00\u4e2a\u7c7b\u4e2d\uff0c\u7136\u540e\u5728\u6211\u4eec\u81ea\u5df1\u7684\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u8be5\u7c7b\u3002 \u4e0b\u9762\u662f\u8ba1\u65f6\u7c7b\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include // for std::chrono functions class Timer { private : // \u7c7b\u578b\u522b\u540d\uff0c\u65b9\u4fbf\u8bbf\u95ee\u5d4c\u5957\u7c7b\u578b using Clock = std :: chrono :: steady_clock ; using Second = std :: chrono :: duration < double , std :: ratio < 1 > > ; std :: chrono :: time_point < Clock > m_beg { Clock :: now () }; //\u975e\u9759\u6001\u6210\u5458\u9ed8\u8ba4\u521d\u59cb\u5316 public : void reset () { m_beg = Clock :: now (); } double elapsed () const { return std :: chrono :: duration_cast < Second > ( Clock :: now () - m_beg ). count (); } }; \u5c31\u662f\u8fd9\u6837\uff01\u8981\u4f7f\u7528\u5b83\uff0c\u6211\u4eec\u9700\u8981\u5728\u4e3b\u51fd\u6570\u7684\u9876\u90e8\u5b9e\u4f8b\u5316\u4e00\u4e2a Timer \u5bf9\u8c61(\u6216\u8005\u5728\u4efb\u4f55\u9700\u8981\u8ba1\u65f6\u7684\u5730\u65b9)\uff0c\u7136\u540e\u5728\u6211\u4eec\u60f3\u77e5\u9053\u7a0b\u5e8f\u8fd0\u884c\u5230\u90a3\u4e2a\u70b9\u82b1\u4e86\u591a\u957f\u65f6\u95f4\u7684\u65f6\u5019\u8c03\u7528elapsed()\u6210\u5458\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { Timer t ; // Code to time goes here std :: cout << \"Time elapsed: \" << t . elapsed () << \" seconds \\n \" ; return 0 ; } \u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u5728\u4e00\u4e2a\u5b9e\u9645\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u5b83\uff0c\u6211\u4eec\u5bf9\u4e00\u4e2a\u5305\u542b10000\u4e2a\u5143\u7d20\u7684\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u9996\u5148\uff0c\u4f7f\u7528\u6211\u4eec\u5728\u524d\u4e00\u7ae0\u5f00\u53d1\u7684\u9009\u62e9\u6392\u5e8f\u7b97\u6cd5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 #include #include // for std::chrono functions #include // for std::size_t #include #include // for std::iota const int g_arrayElements { 10000 }; class Timer { private : // Type aliases to make accessing nested type easier using Clock = std :: chrono :: steady_clock ; using Second = std :: chrono :: duration < double , std :: ratio < 1 > > ; std :: chrono :: time_point < Clock > m_beg { Clock :: now () }; public : void reset () { m_beg = Clock :: now (); } double elapsed () const { return std :: chrono :: duration_cast < Second > ( Clock :: now () - m_beg ). count (); } }; void sortArray ( std :: array < int , g_arrayElements >& array ) { // Step through each element of the array // (except the last one, which will already be sorted by the time we get there) for ( std :: size_t startIndex { 0 }; startIndex < ( g_arrayElements - 1 ); ++ startIndex ) { // smallestIndex is the index of the smallest element we\u2019ve encountered this iteration // Start by assuming the smallest element is the first element of this iteration std :: size_t smallestIndex { startIndex }; // Then look for a smaller element in the rest of the array for ( std :: size_t currentIndex { startIndex + 1 }; currentIndex < g_arrayElements ; ++ currentIndex ) { // If we've found an element that is smaller than our previously found smallest if ( array [ currentIndex ] < array [ smallestIndex ]) { // then keep track of it smallestIndex = currentIndex ; } } // smallestIndex is now the smallest element in the remaining array // swap our start element with our smallest element (this sorts it into the correct place) std :: swap ( array [ startIndex ], array [ smallestIndex ]); } } int main () { std :: array < int , g_arrayElements > array ; std :: iota ( array . rbegin (), array . rend (), 1 ); // fill the array with values 10000 to 1 Timer t ; sortArray ( array ); std :: cout << \"Time taken: \" << t . elapsed () << \" seconds \\n \" ; return 0 ; } \u5728\u4f5c\u8005\u7684\u673a\u5668\u4e0a\uff0c\u4e09\u6b21\u8fd0\u884c\u4ea7\u751f\u7684\u65f6\u95f4\u5206\u522b\u4e3a0.0507\u30010.0506\u548c0.0498\u3002\u56e0\u6b64\u6392\u5e8f\u7684\u8017\u65f6\u5dee\u4e0d\u591a0.05\u79d2\u3002 \u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684 std::sort \u8fdb\u884c\u76f8\u540c\u7684\u6d4b\u8bd5\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include // for std::sort #include #include // for std::chrono functions #include // for std::size_t #include #include // for std::iota const int g_arrayElements { 10000 }; class Timer { private : // Type aliases to make accessing nested type easier using Clock = std :: chrono :: steady_clock ; using Second = std :: chrono :: duration < double , std :: ratio < 1 > > ; std :: chrono :: time_point < Clock > m_beg { Clock :: now () }; public : void reset () { m_beg = Clock :: now (); } double elapsed () const { return std :: chrono :: duration_cast < Second > ( Clock :: now () - m_beg ). count (); } }; int main () { std :: array < int , g_arrayElements > array ; std :: iota ( array . rbegin (), array . rend (), 1 ); // fill the array with values 10000 to 1 Timer t ; std :: ranges :: sort ( array ); // Since C++20 // If your compiler isn't C++20-capable // std::sort(array.begin(), array.end()); std :: cout << \"Time taken: \" << t . elapsed () << \" seconds \\n \" ; return 0 ; } \u5728\u4f5c\u8005\u7684\u673a\u5668\u4e0a\uff0c\u8fd0\u884c\u7ed3\u679c\u662f:0.000693,0.000692\u548c0.000699\u3002\u57fa\u672c\u4e0a\u57280.0007\u5de6\u53f3\u3002 \u6362\u53e5\u8bdd\u8bf4\uff0c\u5728\u672c\u4f8b\u4e2d\uff0c std::sort \u6bd4\u6211\u4eec\u81ea\u5df1\u7f16\u5199\u7684\u9009\u62e9\u6392\u5e8f\u5feb100\u500d! \u5173\u4e8e\u8ba1\u65f6\u7684\u51e0\u70b9\u6ce8\u610f\u4e8b\u9879 \u8ba1\u65f6\u5e76\u4e0d\u590d\u6742\uff0c\u4f46\u5176\u7ed3\u679c\u4f1a\u53d7\u5230\u5f88\u591a\u56e0\u7d20\u7684\u5f71\u54cd\uff0c\u77e5\u9053\u8fd9\u4e9b\u56e0\u7d20\u662f\u4ec0\u4e48\u5f88\u91cd\u8981\u3002 \u9996\u5148\uff0c\u786e\u4fdd\u4f60\u4f7f\u7528\u7684\u662frelease\u7248\u672c\uff0c\u800c\u4e0d\u662fdebug\u7248\u672c\u7684\u7a0b\u5e8f\u3002debug\u7248\u672c\u7684\u7a0b\u5e8f\u4f1a\u5173\u95ed\u4f18\u5316\uff0c\u800c\u4f18\u5316\u53ef\u80fd\u4f1a\u5bf9\u7ed3\u679c\u4ea7\u751f\u91cd\u5927\u5f71\u54cd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528debug\u7248\u672c\u65f6\uff0c\u5728\u4f5c\u8005\u7684\u673a\u5668\u4e0a\u8fd0\u884c\u4e0a\u9762\u7684 std::sort \u793a\u4f8b\u82b1\u4e860.0235\u79d2\u2014\u2014\u6162\u4e8633\u500d! \u5176\u6b21\uff0c\u8ba1\u65f6\u7ed3\u679c\u4f1a\u53d7\u5230\u7cfb\u7edf\u5728\u540e\u53f0\u53ef\u80fd\u6267\u884c\u7684\u5176\u4ed6\u64cd\u4f5c\u7684\u5f71\u54cd\u3002\u4e3a\u4e86\u8fbe\u5230\u6700\u597d\u7684\u6548\u679c\uff0c\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u6ca1\u6709\u505a\u4efb\u4f55CPU\u6216\u5185\u5b58\u5bc6\u96c6\u578b\u7684\u4e8b\u60c5(\u4f8b\u5982\u73a9\u6e38\u620f)\u6216\u786c\u76d8\u5bc6\u96c6\u578b\u7684\u4e8b\u60c5(\u4f8b\u5982\u641c\u7d22\u6587\u4ef6\u6216\u8fd0\u884c\u9632\u75c5\u6bd2\u626b\u63cf)\u3002 \u7136\u540e\u6d4b\u91cf\u81f3\u5c113\u6b21\u3002\u5982\u679c\u7ed3\u679c\u90fd\u76f8\u4f3c\uff0c\u53d6\u5e73\u5747\u503c\u3002\u5982\u679c\u6709\u4e00\u4e24\u4e2a\u7ed3\u679c\u4e0d\u540c\uff0c\u5219\u518d\u8fd0\u884c\u7a0b\u5e8f\u51e0\u6b21\uff0c\u76f4\u5230\u60a8\u66f4\u597d\u5730\u4e86\u89e3\u54ea\u4e9b\u662f\u5f02\u5e38\u503c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6709\u65f6\u5019\u4e00\u4e9b\u770b\u4e0a\u53bb\u65e0\u4f24\u5927\u96c5\u7684\u4e8b\u60c5\u7ffb\u5230\u4f1a\u4e25\u91cd\u5f71\u54cd\u7ed3\u679c\u7684\u6b63\u786e\u6027\u3002\u4f8b\u5982\uff0c\u4f60\u7684\u6d4f\u89c8\u5668\u4e4b\u524d\u6253\u5f00\u4e86\u4e00\u4e2a\u7f51\u7ad9\uff0c\u5f53\u8be5\u7f51\u7ad9\u5728\u540e\u53f0\u8f6e\u6362\u5e7f\u544a\u9875\u9762\u65f6\uff0c\u5b83\u9700\u8981\u89e3\u6790\u4e00\u5806javascript\uff0c\u6b64\u65f6\u53ef\u80fd\u4f1a\u6682\u65f6\u5c06\u4f60\u7684CPU\u5229\u7528\u7387\u63d0\u9ad8\u5230100%\u3002\u591a\u6b21\u8fd0\u884c\u6709\u52a9\u4e8e\u8bc6\u522b\u521d\u59cb\u8fd0\u884c\u662f\u5426\u53d7\u5230\u4e86\u6b64\u7c7b\u4e8b\u4ef6\u7684\u5f71\u54cd\u3002 \u7b2c\u4e09\uff0c\u5f53\u5728\u4e24\u7ec4\u4ee3\u7801\u4e4b\u95f4\u8fdb\u884c\u6bd4\u8f83\u65f6\uff0c\u8981\u8b66\u60d5\u5728\u8fd0\u884c\u4e4b\u95f4\u53ef\u80fd\u4f1a\u5f71\u54cd\u8ba1\u65f6\u7684\u53d8\u5316\u3002\u4f60\u7684\u7cfb\u7edf\u53ef\u80fd\u5728\u540e\u53f0\u542f\u52a8\u4e86\u53cd\u75c5\u6bd2\u626b\u63cf\uff0c\u6216\u8005\u4f60\u73b0\u5728\u6b63\u5728\u6d41\u5a92\u4f53\u97f3\u4e50\uff0c\u800c\u4f60\u4e4b\u524d\u6ca1\u6709\u3002\u968f\u673a\u5316\u4e5f\u4f1a\u5f71\u54cd\u65f6\u95f4\u3002\u5982\u679c\u6211\u4eec\u5bf9\u4e00\u4e2a\u5145\u6ee1\u968f\u673a\u6570\u7684\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u7ed3\u679c\u53ef\u80fd\u4f1a\u53d7\u5230\u968f\u673a\u5316\u7684\u5f71\u54cd\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e5f\u662f\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528\u968f\u673a\u5316\u7684\uff0c\u4f46\u8981\u786e\u4fdd\u4f7f\u7528\u56fa\u5b9a\u7684\u79cd\u5b50(\u4f8b\u5982\uff0c\u4e0d\u8981\u4f7f\u7528\u7cfb\u7edf\u65f6\u949f)\uff0c\u8fd9\u6837\u6bcf\u6b21\u8fd0\u884c\u7684\u968f\u673a\u5316\u90fd\u662f\u76f8\u540c\u7684\u3002\u53e6\u5916\uff0c\u786e\u4fdd\u4f60\u6ca1\u6709\u5bf9\u7b49\u5f85\u7528\u6237\u8f93\u5165\u8fdb\u884c\u8ba1\u65f6\uff0c\u56e0\u4e3a\u7528\u6237\u8f93\u5165\u6240\u9700\u7684\u65f6\u95f4\u4e0d\u5e94\u8be5\u662f\u4f60\u8ba1\u65f6\u8003\u8651\u7684\u4e00\u90e8\u5206\u3002","title":"13.18 - \u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6"},{"location":"13-18-timing-your-code/#_1","text":"\u8ba1\u65f6\u5e76\u4e0d\u590d\u6742\uff0c\u4f46\u5176\u7ed3\u679c\u4f1a\u53d7\u5230\u5f88\u591a\u56e0\u7d20\u7684\u5f71\u54cd\uff0c\u77e5\u9053\u8fd9\u4e9b\u56e0\u7d20\u662f\u4ec0\u4e48\u5f88\u91cd\u8981\u3002 \u9996\u5148\uff0c\u786e\u4fdd\u4f60\u4f7f\u7528\u7684\u662frelease\u7248\u672c\uff0c\u800c\u4e0d\u662fdebug\u7248\u672c\u7684\u7a0b\u5e8f\u3002debug\u7248\u672c\u7684\u7a0b\u5e8f\u4f1a\u5173\u95ed\u4f18\u5316\uff0c\u800c\u4f18\u5316\u53ef\u80fd\u4f1a\u5bf9\u7ed3\u679c\u4ea7\u751f\u91cd\u5927\u5f71\u54cd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528debug\u7248\u672c\u65f6\uff0c\u5728\u4f5c\u8005\u7684\u673a\u5668\u4e0a\u8fd0\u884c\u4e0a\u9762\u7684 std::sort \u793a\u4f8b\u82b1\u4e860.0235\u79d2\u2014\u2014\u6162\u4e8633\u500d! \u5176\u6b21\uff0c\u8ba1\u65f6\u7ed3\u679c\u4f1a\u53d7\u5230\u7cfb\u7edf\u5728\u540e\u53f0\u53ef\u80fd\u6267\u884c\u7684\u5176\u4ed6\u64cd\u4f5c\u7684\u5f71\u54cd\u3002\u4e3a\u4e86\u8fbe\u5230\u6700\u597d\u7684\u6548\u679c\uff0c\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u6ca1\u6709\u505a\u4efb\u4f55CPU\u6216\u5185\u5b58\u5bc6\u96c6\u578b\u7684\u4e8b\u60c5(\u4f8b\u5982\u73a9\u6e38\u620f)\u6216\u786c\u76d8\u5bc6\u96c6\u578b\u7684\u4e8b\u60c5(\u4f8b\u5982\u641c\u7d22\u6587\u4ef6\u6216\u8fd0\u884c\u9632\u75c5\u6bd2\u626b\u63cf)\u3002 \u7136\u540e\u6d4b\u91cf\u81f3\u5c113\u6b21\u3002\u5982\u679c\u7ed3\u679c\u90fd\u76f8\u4f3c\uff0c\u53d6\u5e73\u5747\u503c\u3002\u5982\u679c\u6709\u4e00\u4e24\u4e2a\u7ed3\u679c\u4e0d\u540c\uff0c\u5219\u518d\u8fd0\u884c\u7a0b\u5e8f\u51e0\u6b21\uff0c\u76f4\u5230\u60a8\u66f4\u597d\u5730\u4e86\u89e3\u54ea\u4e9b\u662f\u5f02\u5e38\u503c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6709\u65f6\u5019\u4e00\u4e9b\u770b\u4e0a\u53bb\u65e0\u4f24\u5927\u96c5\u7684\u4e8b\u60c5\u7ffb\u5230\u4f1a\u4e25\u91cd\u5f71\u54cd\u7ed3\u679c\u7684\u6b63\u786e\u6027\u3002\u4f8b\u5982\uff0c\u4f60\u7684\u6d4f\u89c8\u5668\u4e4b\u524d\u6253\u5f00\u4e86\u4e00\u4e2a\u7f51\u7ad9\uff0c\u5f53\u8be5\u7f51\u7ad9\u5728\u540e\u53f0\u8f6e\u6362\u5e7f\u544a\u9875\u9762\u65f6\uff0c\u5b83\u9700\u8981\u89e3\u6790\u4e00\u5806javascript\uff0c\u6b64\u65f6\u53ef\u80fd\u4f1a\u6682\u65f6\u5c06\u4f60\u7684CPU\u5229\u7528\u7387\u63d0\u9ad8\u5230100%\u3002\u591a\u6b21\u8fd0\u884c\u6709\u52a9\u4e8e\u8bc6\u522b\u521d\u59cb\u8fd0\u884c\u662f\u5426\u53d7\u5230\u4e86\u6b64\u7c7b\u4e8b\u4ef6\u7684\u5f71\u54cd\u3002 \u7b2c\u4e09\uff0c\u5f53\u5728\u4e24\u7ec4\u4ee3\u7801\u4e4b\u95f4\u8fdb\u884c\u6bd4\u8f83\u65f6\uff0c\u8981\u8b66\u60d5\u5728\u8fd0\u884c\u4e4b\u95f4\u53ef\u80fd\u4f1a\u5f71\u54cd\u8ba1\u65f6\u7684\u53d8\u5316\u3002\u4f60\u7684\u7cfb\u7edf\u53ef\u80fd\u5728\u540e\u53f0\u542f\u52a8\u4e86\u53cd\u75c5\u6bd2\u626b\u63cf\uff0c\u6216\u8005\u4f60\u73b0\u5728\u6b63\u5728\u6d41\u5a92\u4f53\u97f3\u4e50\uff0c\u800c\u4f60\u4e4b\u524d\u6ca1\u6709\u3002\u968f\u673a\u5316\u4e5f\u4f1a\u5f71\u54cd\u65f6\u95f4\u3002\u5982\u679c\u6211\u4eec\u5bf9\u4e00\u4e2a\u5145\u6ee1\u968f\u673a\u6570\u7684\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u7ed3\u679c\u53ef\u80fd\u4f1a\u53d7\u5230\u968f\u673a\u5316\u7684\u5f71\u54cd\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e5f\u662f\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528\u968f\u673a\u5316\u7684\uff0c\u4f46\u8981\u786e\u4fdd\u4f7f\u7528\u56fa\u5b9a\u7684\u79cd\u5b50(\u4f8b\u5982\uff0c\u4e0d\u8981\u4f7f\u7528\u7cfb\u7edf\u65f6\u949f)\uff0c\u8fd9\u6837\u6bcf\u6b21\u8fd0\u884c\u7684\u968f\u673a\u5316\u90fd\u662f\u76f8\u540c\u7684\u3002\u53e6\u5916\uff0c\u786e\u4fdd\u4f60\u6ca1\u6709\u5bf9\u7b49\u5f85\u7528\u6237\u8f93\u5165\u8fdb\u884c\u8ba1\u65f6\uff0c\u56e0\u4e3a\u7528\u6237\u8f93\u5165\u6240\u9700\u7684\u65f6\u95f4\u4e0d\u5e94\u8be5\u662f\u4f60\u8ba1\u65f6\u8003\u8651\u7684\u4e00\u90e8\u5206\u3002","title":"\u5173\u4e8e\u8ba1\u65f6\u7684\u51e0\u70b9\u6ce8\u610f\u4e8b\u9879"},{"location":"13-2-classes-and-class-members/","tags":["class"],"text":"Key Takeaway \u867d\u7136 C++ \u63d0\u4f9b\u4e86\u4e0d\u5c11\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982 char\u3001int\u3001long\u3001float\u3001double \u7b49\u7b49\uff09\uff0c\u5e94\u4ed8\u76f8\u5bf9\u7684\u7b80\u5355\u95ee\u9898\uff0c\u8fd9\u4e9b\u57fa\u672c\u6570\u636e\u7ed3\u6784\u4f3c\u4e4e\u5df2\u7ecf\u8db3\u591f\u4e86\uff0c\u4f46\u662f\u4f7f\u7528\u5b83\u4eec\u89e3\u51b3\u590d\u6742\u95ee\u9898\u7684\u8bdd\u5c31\u4f1a\u663e\u5f97\u529b\u4e0d\u4ece\u5fc3\u3002C++ \u6700\u6709\u7528\u7684\u4e00\u4e2a\u7279\u6027\u5c31\u662f\u5141\u8bb8\u7528\u6237\u9488\u5bf9\u8981\u89e3\u51b3\u7684\u95ee\u9898\u6765\u81ea\u5b9a\u4e49\u6570\u636e\u7c7b\u578b\u3002 \u679a\u4e3e\u7c7b\u578b \u548c \u7ed3\u6784\u4f53 \u662f\u4e24\u79cd\u6211\u4eec\u5df2\u7ecf\u4ecb\u7ecd\u8fc7\u7684\u81ea\u5b9a\u4e49\u6570\u636e\u7c7b\u578b\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u7ed3\u6784\u4f53\u6765\u5b58\u653e\u65e5\u671f\uff1a 1 2 3 4 5 6 struct DateStruct { int year {}; int month {}; int day {}; }; \u679a\u4e3e\u7c7b\u578b\u548c\u7eaf\u6570\u636e\u7ed3\u6784\u4f53\uff08\u53ea\u5305\u542b\u53d8\u91cf\u7684\u7ed3\u6784\u4f53\uff09\u5e38\u7528\u4e8e\u4f20\u7edf\u7684\u975e\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u9886\u57df\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ea\u80fd\u4fdd\u5b58\u6570\u636e\u3002\u6211\u4eec\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u521d\u59cb\u5316\u7ed3\u6784\u4f53\uff1a 1 DateStruct today { 2020 , 10 , 14 }; // use uniform initialization \u73b0\u5728\uff0c\u5982\u679c\u4f60\u60f3\u5c06\u7ed3\u6784\u4f53\u7684\u5185\u5bb9\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\uff0c\u90a3\u4e48\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u51fd\u6570\u6765\u5b8c\u6210\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include struct DateStruct { int year {}; int month {}; int day {}; }; void print ( const DateStruct & date ) { std :: cout << date . year << '/' << date . month << '/' << date . day ; } int main () { DateStruct today { 2020 , 10 , 14 }; // \u4f7f\u7528\u7edf\u4e00\u521d\u59cb\u5316 today . day = 16 ; // \u4f7f\u7528\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26\u9009\u62e9\u4e00\u4e2a\u7ed3\u6784\u4f53\u6210\u5458 print ( today ); return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2020/10/16 \u7c7b\uff08Classes\uff09 \u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u4e16\u754c\u4e2d\uff0c\u6211\u4eec\u4e0d\u4ec5\u9700\u8981\u81ea\u5b9a\u4e49\u7c7b\u578b\u80fd\u591f\u5305\u542b\u6570\u636e\uff0c\u5b83\u8fd8\u9700\u8981\u63d0\u4f9b\u80fd\u591f\u64cd\u4f5c\u8fd9\u4e9b\u6570\u636e\u7684\u51fd\u6570\uff08\u65b9\u6cd5\uff09\u3002\u5728C++\u4e2d\uff0c\u5b9a\u4e49\u8fd9\u6837\u7684\u6570\u636e\u53ef\u4ee5\u4f7f\u7528 class \u5173\u952e\u5b57\u3002\u8be5\u5173\u952e\u5b57\u53ef\u4ee5\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u88ab\u79f0\u4e3a\u7c7b\uff08class\uff09\u7684\u6570\u636e\u7c7b\u578b\u3002 \u5728 C++ \u4e2d\uff0c\u7c7b\u548c\u7ed3\u6784\u4f53\u672c\u8d28\u4e0a\u662f\u76f8\u540c\u7684\u3002\u5b9e\u9645\u4e0a\uff0c\u4e0b\u9762\u4ee3\u7801\u4e2d\u7684\u7ed3\u6784\u4f53\u548c\u7c7b\u51e0\u4e4e\u6ca1\u6709\u533a\u522b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct DateStruct { int year {}; int month {}; int day {}; }; class DateClass { public : int m_year {}; int m_month {}; int m_day {}; }; \u6ce8\u610f\uff0c\u4e0a\u9762\u4ee3\u7801\u4e2d\u7684\u552f\u4e00\u7684\u4e00\u5904\u660e\u663e\u4e0d\u540c\uff0c\u5c31\u662f\u7c7b\u4e2d\u7684public\u5173\u952e\u5b57\u3002\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u8ba8\u8bba\u8be5\u5173\u952e\u5b57\u7684\u4f5c\u7528\u3002 \u4e0e\u58f0\u660e\u7ed3\u6784\u4f53\u7c7b\u4f3c\uff0c\u7c7b\u7684\u58f0\u660e\u5e76\u4e0d\u4f1a\u5b9e\u9645\u5206\u914d\u4efb\u4f55\u5185\u5b58\uff0c\u5b83\u4ec5\u4ec5\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b\u5e94\u8be5\u201c\u662f\u4ec0\u4e48\u6837\u7684\u201d\u3002 \u6ce8\u610f \u4e0e\u5b9a\u4e49\u7ed3\u6784\u4f53\u7c7b\u4f3c\uff0c\u5b9a\u4e49\u7c7b\u7684\u65f6\u5019\u6700\u5bb9\u6613\u72af\u7684\u4e00\u4e2a\u9519\u8bef\u5c31\u662f\u5fd8\u8bb0\u7ed3\u5c3e\u5904\u7684\u5206\u53f7\u3002\u5fd8\u8bb0\u8fd9\u4e2a\u5206\u53f7\u4f1a\u5bfc\u81f4 \u4e0b\u4e00\u884c\u4ee3\u7801 \u7f16\u8bd1\u62a5\u9519\u3002\u73b0\u4ee3\u7f16\u8bd1\u5668\uff0c\u4f8b\u5982 Visual Studio 2010 \u4f1a\u5728\u4f60\u5fd8\u8bb0\u8be5\u5206\u53f7\u65f6\u63d0\u9192\u4f60\uff0c\u4f46\u662f\u4e00\u4e9b\u8f83\u65e9\u7684\u6216\u8005\u7b80\u5355\u7684\u7f16\u8bd1\u5668\u5e76\u4e0d\u4f1a\u63d0\u9192\u4f60\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u95ee\u9898\u96be\u4ee5\u88ab\u53d1\u73b0\u3002 \u7c7b\uff08\u548c\u7ed3\u6784\u4f53\uff09\u7684\u5b9a\u4e49\u7c7b\u4f3c\u4e8e \u84dd\u56fe \u2014\u2014\u5b83\u4eec\u63cf\u8ff0\u4e86\u4e00\u4e2a\u5bf9\u8c61\u5e94\u8be5\u201c\u662f\u4ec0\u4e48\u6837\u7684\u201d\uff0c\u4f46\u5b9a\u4e49\u5e76\u4e0d\u4f1a\u5b9e\u9645\u521b\u5efa\u5bf9\u8c61\u3002\u4e3a\u4e86\u521b\u5efa\u5bf9\u8c61\uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u8be5\u7c7b\u578b\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\uff1a 1 DateClass today { 2020 , 10 , 14 }; // declare a variable of class DateClass \u5c0f\u8d34\u58eb \u5728\u58f0\u660e\u7c7b\u7684\u65f6\u5019\u5c31\u8981\u5bf9\u5176\u6210\u5458\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u3002 \u6210\u5458\u51fd\u6570 \u9664\u4e86\u5b58\u653e\u6570\u636e\u4e4b\u5916\uff0c\u7c7b\uff08\u548c\u7ed3\u6784\u4f53\uff09\u540c\u6837\u8fd8\u53ef\u4ee5\u5305\u542b\u51fd\u6570\uff01\u5b9a\u4e49\u5728\u7c7b\u4e2d\u7684\u51fd\u6570\u79f0\u4e3a \u6210\u5458\u51fd\u6570 \uff08\u6216\u79f0\u4e3a\u65b9\u6cd5\uff09\u3002\u6210\u5458\u51fd\u6570\u7684\u5b9a\u4e49\u53ef\u4ee5\u88ab\u5305\u542b\u5728\u7c7b\u5185\uff0c\u4e5f\u53ef\u4ee5\u5b9a\u4e49\u5728\u7c7b\u5916\u3002\u6211\u4eec\u6682\u65f6\u5c06\u5176\u5b9a\u4e49\u5728\u7c7b\u5185\uff0c\u8fd9\u79cd\u5f62\u5f0f\u76f8\u5bf9\u66f4\u7b80\u5355\uff0c\u7a0d\u540e\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5982\u4f55\u5728\u7c7b\u5916\u5b9a\u4e49\u7c7b\u7684\u65b9\u6cd5\u3002 \u5177\u6709\u6210\u5458\u51fd\u6570\u7684Data\u7c7b\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class DateClass { public : int m_year {}; int m_month {}; int m_day {}; void print () // defines a member function named print() { std :: cout << m_year << '/' << m_month << '/' << m_day ; } }; \u548c\u7ed3\u6784\u4f53\u6210\u5458\u4e00\u6837\uff0c\u7c7b\u7684\u6210\u5458\uff08\u5305\u62ec\u53d8\u91cf\u548c\u51fd\u6570\uff09\u53ef\u4ee5\u901a\u8fc7 \u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26(.) \u6765\u8bbf\u95ee\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include class DateClass { public : int m_year {}; int m_month {}; int m_day {}; void print () { std :: cout << m_year << '/' << m_month << '/' << m_day ; } }; int main () { DateClass today { 2020 , 10 , 14 }; today . m_day = 16 ; // use member selection operator to select a member variable of the class today . print (); // use member selection operator to call a member function of the class return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 2020/10/16 \u89c2\u5bdf\u53ef\u4ee5\u53d1\u73b0\uff0c\u4e0a\u8ff0\u4ee3\u7801\u548c\u4e4b\u524d\u4f7f\u7528\u7ed3\u6784\u4f53\u7684\u7248\u672c\u8fd1\u4e4e\u662f\u4e00\u81f4\u7684\u3002 \u4e0d\u8fc7\uff0c\u5176\u4e2d\u8fd8\u662f\u6709\u51e0\u5904\u4e0d\u540c\u3002\u5728\u4f7f\u7528\u7ed3\u6784\u4f53\u7684\u7248\u672c\u4e2d\uff0c print() \u51fd\u6570\u9700\u8981\u5c06\u7ed3\u6784\u4f53\u672c\u8eab\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\uff0c\u5426\u5219 print() \u51fd\u6570\u5c31\u65e0\u4ece\u5224\u65ad\u6211\u4eec\u8981\u6253\u5370\u7684\u662f\u54ea\u4e2a\u7ed3\u6784\u4f53\u7684\u5185\u5bb9\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u5728\u51fd\u6570\u4e2d\u660e\u786e\u5730\u4f7f\u7528\u8be5\u53c2\u6570\u3002 \u6210\u5458\u51fd\u6570\u7684\u5de5\u4f5c\u65b9\u5f0f\u5219\u7565\u6709\u4e0d\u540c\uff1a\u6240\u6709\u6210\u5458\u51fd\u6570\u7684\u8c03\u7528\u5fc5\u987b\u57fa\u4e8e\u8be5\u7c7b\u7684\u4e00\u4e2a\u5177\u4f53\u5bf9\u8c61\u3002\u5f53\u6211\u4eec\u8c03\u7528 today.print() \u7684\u65f6\u5019\uff0c\u5b9e\u9645\u4e0a\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\u8c03\u7528 today \u5bf9\u8c61\u7684\u6210\u5458\u51fd\u6570 print() \u3002 \u8ba9\u6211\u4eec\u5728\u770b\u4e00\u773c\u6210\u5458\u51fd\u6570 print \u7684\u5b9a\u4e49\uff1a 1 2 3 4 void print () // defines a member function named print() { std :: cout << m_year << '/' << m_month << '/' << m_day ; } m_year \u3001 m_month \u548c m_day \u662f\u8c01\u7684\u53d8\u91cf\uff1f\u5b83\u4eec\u6307\u7684\u662f\u5176\u51fd\u6570\u8c03\u7528\u65f6\u5173\u8054\u7684\u5bf9\u8c61\u6240\u5c5e\u7684\u53d8\u91cf\u3002 \u56e0\u6b64\u5f53\u8c03\u7528 today.print() \u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c06 m_day \u89e3\u6790\u4e3a today.m_day \uff0c\u5c06 m_month \u89e3\u6790\u4e3a today.m_month \uff0c\u5c06 m_year \u89e3\u6790\u4e3a today.m_year \u3002\u5f53\u8c03\u7528 tomorrow.print() \u65f6\uff0c\u5219 m_day \u89e3\u6790\u4e3a tomorrow.m_day \u3002 \u53ef\u89c1\uff0c\u76f8\u5173\u8054\u7684\u5bf9\u8c61\u4f1a\u88ab\u9690\u5f0f\u5730\u4f20\u5165\u6210\u5458\u51fd\u6570\u3002\u56e0\u6b64\uff0c\u5b83\u901a\u5e38\u88ab\u79f0\u4e3a \u9690\u5f0f\u5bf9\u8c61 \u3002 \u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u9690\u5f0f\u5bf9\u8c61\u4f20\u9012\u7684\u5de5\u4f5c\u539f\u7406\u3002 \u5bf9\u4e8e\u975e\u6210\u5458\u51fd\u6570\u6765\u8bf4\uff0c\u6211\u4eec\u5fc5\u987b\u5c06\u5b83\u9700\u8981\u64cd\u4f5c\u7684\u6570\u636e\u4f20\u9012\u7ed9\u51fd\u6570\u3002\u800c\u5bf9\u4e8e\u6210\u5458\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5047\u5b9a\u5b83\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8fd9\u4e2a\u7c7b\u7684\u5bf9\u8c61\u3002 \u4e3a\u6210\u5458\u53d8\u91cf\u6dfb\u52a0 \u201c m_ \u201d \u524d\u7f00\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u533a\u5206\u6210\u5458\u53d8\u91cf\u3001\u51fd\u6570\u53c2\u6570\u548c\u5c40\u90e8\u53d8\u91cf\u3002\u8fd9\u4e48\u505a\u6709\u8bf8\u591a\u597d\u5904\uff1a\u9996\u5148\uff0c\u5f53\u6211\u4eec\u770b\u5230\u5bf9\u5177\u6709 \u201c m_ \u201d\u524d\u7f00\u7684\u53d8\u91cf\u8fdb\u884c\u8d4b\u503c\u65f6\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u6307\u5b9a\u8be5\u64cd\u4f5c\u4f1a\u6539\u53d8\u5f53\u524d\u7c7b\u5b9e\u4f8b\u7684\u72b6\u6001\u3002\u5176\u6b21\uff0c\u51fd\u6570 \u5f62\u53c2 \u6216\u5c40\u90e8\u53d8\u91cf\u88ab\u58f0\u660e\u5728\u5f53\u524d\u51fd\u6570\u4e2d\uff0c\u800c\u6210\u5458\u53d8\u91cf\u5219\u4e0d\u540c\uff0c\u5b83\u88ab\u5b9a\u4e49\u5728\u7c7b\u4e2d\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u77e5\u9053 m_ \u524d\u7f00\u7684\u53d8\u91cf\u7684\u5177\u4f53\u5b9a\u4e49\uff0c\u5c31\u9700\u8981\u53bb\u7c7b\u7684\u5b9a\u4e49\u4e2d\u67e5\u770b\uff0c\u800c\u4e0d\u662f\u5728\u5f53\u524d\u51fd\u6570\u4e2d\u67e5\u770b\u3002 \u6309\u7167\u7ba1\u7406\uff0c\u7c7b\u540d\u901a\u5e38\u662f\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u7c7b\u547d\u540d\u65f6\uff0c\u4f7f\u7528\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u3002 \u518d\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include #include class Employee { public : std :: string m_name {}; int m_id {}; double m_wage {}; // Print employee information to the screen void print () { std :: cout << \"Name: \" << m_name << \" Id: \" << m_id << \" Wage: $\" << m_wage << '\\n' ; } }; int main () { // Declare two employees Employee alex { \"Alex\" , 1 , 25.00 }; Employee joe { \"Joe\" , 2 , 22.25 }; // Print out the employee information alex . print (); joe . print (); return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 Name: Alex Id: 1 Wage: $25 Name: Joe Id: 2 Wage: $22.25 \u5bf9\u4e8e\u975e\u6210\u5458\u51fd\u6570\u6765\u8bf4\uff0c\u5b83\u4e0d\u80fd\u8c03\u7528\u88ab\u5b9a\u4e49\u5728\u5b83\u4e0b\u65b9\uff08\u5373\u540e\u5b9a\u4e49\uff09\u7684\u51fd\u6570\uff0c\u9664\u975e\u5b83\u80fd\u591f\u770b\u5230\u8be5\u51fd\u6570\u7684 \u524d\u5411\u58f0\u660e \uff1a 1 2 3 4 5 6 7 8 void x () { // You can't call y() from here unless the compiler has already seen a forward declaration for y() } void y () { } \u800c\u5bf9\u4e8e\u6210\u5458\u51fd\u6570\u6765\u8bf4\uff0c\u5219\u6ca1\u6709\u8be5\u9650\u5236\uff1a 1 2 3 4 5 6 class foo { public : void x () { y (); } // okay to call y() here, even though y() isn't defined until later in this class void y () { }; }; \u6210\u5458\u7c7b\u578b \u9664\u4e86\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u51fd\u6570\uff0c\u7c7b\u8fd8\u53ef\u4ee5\u5177\u6709 \u6210\u5458\u7c7b\u578b \u6216\u5d4c\u5957\u7c7b\u578b\uff08\u4e5f\u5305\u62ec \u7c7b\u578b\u522b\u540d \uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Employee { public : using IDType = int ; std :: string m_name {}; IDType m_id {}; double m_wage {}; // Print employee information to the screen void print () { std :: cout << \"Name: \" << m_name << \" Id: \" << m_id << \" Wage: $\" << m_wage << '\\n' ; } }; \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7c7b\u540d\u5b9e\u9645\u4e0a\u6210\u4e3a\u4e86\u5176\u5d4c\u5957\u7c7b\u578b\u7684 \u547d\u540d\u7a7a\u95f4 \u3002\u5728\u8be5\u7c7b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 IDType \uff0c\u800c\u5728\u7c7b\u5916\uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528 Employee::IDType \u6765\u8bbf\u95ee\u8be5\u7c7b\u578b\u3002 \u5982\u679c\u5728\u540e\u7eed\u7684\u5f00\u53d1\u4e2d\u6211\u4eec\u53d1\u73b0 int \u7c7b\u578b\u5df2\u7ecf\u65e0\u6cd5\u6ee1\u8db3\u9700\u8981\uff0c\u800c\u5fc5\u987b\u6539\u7528 std::string \uff0c\u6b64\u65f6\u6211\u4eec\u53ea\u9700\u8981\u66f4\u65b0\u8fd9\u4e2a\u7c7b\u578b\u522b\u540d\uff0c\u800c\u4e0d\u9700\u8981\u5c06\u6240\u6709\u7684 int \u90fd\u66ff\u6362\u4e3a std::string \u3002 \u5d4c\u5957\u7c7b\u578b\u4e0d\u80fd\u88ab\u524d\u5411\u58f0\u660e\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5d4c\u5957\u7c7b\u578b\u5e94\u8be5\u53ea\u5728\u5305\u542b\u5b83\u7684\u7c7b\u4e2d\u4f7f\u7528\u3002\u6ce8\u610f\uff0c\u56e0\u4e3a\u7c7b\u4e5f\u662f\u4e00\u79cd\u7c7b\u578b\uff0c\u6240\u4ee5\u7c7b\u53ef\u4ee5\u88ab\u5d4c\u5957\u5728\u7c7b\u4e2d\u2014\u2014\u8fd9\u4e2a\u505a\u6cd5\u5e76\u4e0d\u5e38\u89c1\u800c\u4e14\u901a\u5e38\u53ea\u6709\u9ad8\u7ea7\u7a0b\u5e8f\u5458\u624d\u4f1a\u53bb\u4f7f\u7528\u3002 \u5173\u4e8e C++ \u4e2d\u7ed3\u6784\u4f53\u7684\u4e00\u4e9b\u6ce8\u610f\u4e8b\u9879 \u5728C\u8bed\u8a00\u4e2d\uff0c\u7ed3\u6784\u4f53\u53ea\u6709\u6570\u636e\u6210\u5458\uff0c\u6ca1\u6709\u6210\u5458\u51fd\u6570\u3002\u5728c++\u4e2d\uff0c\u5728\u8bbe\u8ba1\u5b8c\u7c7b(\u4f7f\u7528class\u5173\u952e\u5b57)\u540e\uff0cBjarne Stroustrup\u82b1\u4e86\u4e00\u4e9b\u65f6\u95f4\u8003\u8651\u662f\u5426\u5e94\u8be5\u8d4b\u4e88\u7ed3\u6784\u4f53(\u7ee7\u627f\u81eaC)\u5177\u6709\u6210\u5458\u51fd\u6570\u7684\u80fd\u529b\u3002\u7ecf\u8fc7\u8003\u8651\uff0c\u4ed6\u51b3\u5b9a\u7ed3\u6784\u4f53\u5e94\u8be5\u80fd\u591f\u5177\u6709\u6210\u5458\u51fd\u6570\uff0c\u8fd9\u6837\u4e00\u6765\u53ef\u4ee5\u5728\u67d0\u79cd\u7a0b\u5ea6\u4e0a\u4e3a\u7c7b\u548c\u7ed3\u6784\u4f53\u6307\u5b9a\u7edf\u4e00\u7684\u89c4\u5219\u96c6\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u6211\u4eec\u4f7f\u7528class\u5173\u952e\u5b57\u7f16\u5199\u4e86\u4e0a\u9762\u7684\u7a0b\u5e8f\uff0c\u4f46\u5176\u5b9e\u53ef\u4ee5\u4f7f\u7528struct\u5173\u952e\u5b57\u6765\u4ee3\u66ff\u3002 \u8bb8\u591a\u5f00\u53d1\u8005(\u5305\u62ec\u6211\u81ea\u5df1)\u8ba4\u4e3a\u8fd9\u662f\u4e00\u4e2a\u4e0d\u6b63\u786e\u7684\u51b3\u5b9a\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u5bfc\u81f4\u5371\u9669\u7684\u5047\u8bbe\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4e00\u4e2a\u7c7b\u4f1a\u5728\u9500\u6bc1\u540e\u6e05\u7406\u5b83\u81ea\u5df1\u662f\u5408\u7406\u7684(\u4f8b\u5982\uff0c\u4e00\u4e2a\u5206\u914d\u5185\u5b58\u7684\u7c7b\u4f1a\u5728\u88ab\u9500\u6bc1\u4e4b\u524d\u91ca\u653e\u5b83\u6240\u4f7f\u7528\u7684\u5185\u5b58)\uff0c\u4f46\u5047\u8bbe\u4e00\u4e2a\u7ed3\u6784\u4f53\u4e5f\u4f1a\u8fd9\u4e48\u505a\u5219\u662f\u4e0d\u5b89\u5168\u7684\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5efa\u8bae\u5bf9\u53ea\u6709\u6570\u636e\u7684\u7ed3\u6784\u4f53\u4f7f\u7528struct\u5173\u952e\u5b57\uff0c\u800c\u5bf9\u5b9a\u4e49\u9700\u8981\u6570\u636e\u548c\u51fd\u6570\u6346\u7ed1\u5728\u4e00\u8d77\u7684\u5bf9\u8c61\u65f6\u4f7f\u7528class\u5173\u952e\u5b57\u3002 \u6700\u4f73\u5b9e\u8df5 struct \u5173\u952e\u5b57\u53ea\u9002\u7528\u4e8e\u4ec5\u5305\u542b\u6570\u636e\u7684\u7ed3\u6784\u3002\u800c\u5bf9\u4e8e\u540c\u65f6\u5305\u542b\u6570\u636e\u548c\u51fd\u6570\u7684\u5bf9\u8c61\uff0c\u8bf7\u4f7f\u7528class\u5173\u952e\u5b57\u3002 \u5176\u5b9e\u4f60\u5df2\u7ecf\u5728\u4e0d\u77e5\u60c5\u7684\u60c5\u51b5\u4e0b\u591a\u6b21\u4f7f\u7528class\u4e86\u3002 \u5b9e\u9645\u4e0a\uff0cC++\u6807\u51c6\u5e93\u91cc\u5145\u6ee1\u4e86\u5404\u5f0f\u5404\u6837\u7684\u7c7b\uff0c\u5b83\u4eec\u90fd\u53ef\u4ee5\u65b9\u4fbf\u4f60\u5de5\u4f5c\uff0c std::string \u3001 std::vector \u548c std::array \u90fd\u662fclass\uff01\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u521b\u5efa\u8fd9\u4e9b\u7c7b\u578b\u7684\u5bf9\u8c61\u65f6\uff0c\u4f60\u5c31\u5728\u5b9e\u4f8b\u5316\u4e00\u4e2a\u7c7b\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u800c\u5f53\u4f60\u4f7f\u7528\u8fd9\u4e9b\u5bf9\u8c61\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u8c03\u7528\u7684\u90fd\u662f\u5b83\u4eec\u7684\u6210\u5458\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include #include #include int main () { std :: string s { \"Hello, world!\" }; // instantiate a string class object std :: array < int , 3 > a { 1 , 2 , 3 }; // instantiate an array class object std :: vector < double > v { 1.1 , 2.2 , 3.3 }; // instantiate a vector class object std :: cout << \"length: \" << s . length () << '\\n' ; // call a member function return 0 ; } \u5c0f\u7ed3 class\u5173\u952e\u5b57\u5141\u8bb8\u6211\u4eec\u5728C++\u4e2d\u521b\u5efa\u4e00\u4e2a\u65e2\u53ef\u4ee5\u5305\u542b\u6210\u5458\u53d8\u91cf\u53c8\u53ef\u4ee5\u5305\u542b\u6210\u5458\u51fd\u6570\u7684\u81ea\u5b9a\u4e49\u7c7b\u578b\u3002\u7c7b\u6784\u6210\u4e86\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u57fa\u7840\uff0c\u6211\u4eec\u5c06\u7528\u672c\u7ae0\u7684\u5176\u4f59\u90e8\u5206\u548c\u5c06\u6765\u7684\u8bb8\u591a\u7ae0\u8282\u6765\u63a2\u7d22\u7c7b\u4e3a\u6211\u4eec\u63d0\u4f9b\u7684\u4e00\u5207\u80fd\u529b\u3002","title":"13.2 - \u7c7b\u548c\u7c7b\u6210\u5458"},{"location":"13-2-classes-and-class-members/#classes","text":"\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u4e16\u754c\u4e2d\uff0c\u6211\u4eec\u4e0d\u4ec5\u9700\u8981\u81ea\u5b9a\u4e49\u7c7b\u578b\u80fd\u591f\u5305\u542b\u6570\u636e\uff0c\u5b83\u8fd8\u9700\u8981\u63d0\u4f9b\u80fd\u591f\u64cd\u4f5c\u8fd9\u4e9b\u6570\u636e\u7684\u51fd\u6570\uff08\u65b9\u6cd5\uff09\u3002\u5728C++\u4e2d\uff0c\u5b9a\u4e49\u8fd9\u6837\u7684\u6570\u636e\u53ef\u4ee5\u4f7f\u7528 class \u5173\u952e\u5b57\u3002\u8be5\u5173\u952e\u5b57\u53ef\u4ee5\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u88ab\u79f0\u4e3a\u7c7b\uff08class\uff09\u7684\u6570\u636e\u7c7b\u578b\u3002 \u5728 C++ \u4e2d\uff0c\u7c7b\u548c\u7ed3\u6784\u4f53\u672c\u8d28\u4e0a\u662f\u76f8\u540c\u7684\u3002\u5b9e\u9645\u4e0a\uff0c\u4e0b\u9762\u4ee3\u7801\u4e2d\u7684\u7ed3\u6784\u4f53\u548c\u7c7b\u51e0\u4e4e\u6ca1\u6709\u533a\u522b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct DateStruct { int year {}; int month {}; int day {}; }; class DateClass { public : int m_year {}; int m_month {}; int m_day {}; }; \u6ce8\u610f\uff0c\u4e0a\u9762\u4ee3\u7801\u4e2d\u7684\u552f\u4e00\u7684\u4e00\u5904\u660e\u663e\u4e0d\u540c\uff0c\u5c31\u662f\u7c7b\u4e2d\u7684public\u5173\u952e\u5b57\u3002\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u8ba8\u8bba\u8be5\u5173\u952e\u5b57\u7684\u4f5c\u7528\u3002 \u4e0e\u58f0\u660e\u7ed3\u6784\u4f53\u7c7b\u4f3c\uff0c\u7c7b\u7684\u58f0\u660e\u5e76\u4e0d\u4f1a\u5b9e\u9645\u5206\u914d\u4efb\u4f55\u5185\u5b58\uff0c\u5b83\u4ec5\u4ec5\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b\u5e94\u8be5\u201c\u662f\u4ec0\u4e48\u6837\u7684\u201d\u3002 \u6ce8\u610f \u4e0e\u5b9a\u4e49\u7ed3\u6784\u4f53\u7c7b\u4f3c\uff0c\u5b9a\u4e49\u7c7b\u7684\u65f6\u5019\u6700\u5bb9\u6613\u72af\u7684\u4e00\u4e2a\u9519\u8bef\u5c31\u662f\u5fd8\u8bb0\u7ed3\u5c3e\u5904\u7684\u5206\u53f7\u3002\u5fd8\u8bb0\u8fd9\u4e2a\u5206\u53f7\u4f1a\u5bfc\u81f4 \u4e0b\u4e00\u884c\u4ee3\u7801 \u7f16\u8bd1\u62a5\u9519\u3002\u73b0\u4ee3\u7f16\u8bd1\u5668\uff0c\u4f8b\u5982 Visual Studio 2010 \u4f1a\u5728\u4f60\u5fd8\u8bb0\u8be5\u5206\u53f7\u65f6\u63d0\u9192\u4f60\uff0c\u4f46\u662f\u4e00\u4e9b\u8f83\u65e9\u7684\u6216\u8005\u7b80\u5355\u7684\u7f16\u8bd1\u5668\u5e76\u4e0d\u4f1a\u63d0\u9192\u4f60\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u95ee\u9898\u96be\u4ee5\u88ab\u53d1\u73b0\u3002 \u7c7b\uff08\u548c\u7ed3\u6784\u4f53\uff09\u7684\u5b9a\u4e49\u7c7b\u4f3c\u4e8e \u84dd\u56fe \u2014\u2014\u5b83\u4eec\u63cf\u8ff0\u4e86\u4e00\u4e2a\u5bf9\u8c61\u5e94\u8be5\u201c\u662f\u4ec0\u4e48\u6837\u7684\u201d\uff0c\u4f46\u5b9a\u4e49\u5e76\u4e0d\u4f1a\u5b9e\u9645\u521b\u5efa\u5bf9\u8c61\u3002\u4e3a\u4e86\u521b\u5efa\u5bf9\u8c61\uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u8be5\u7c7b\u578b\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\uff1a 1 DateClass today { 2020 , 10 , 14 }; // declare a variable of class DateClass \u5c0f\u8d34\u58eb \u5728\u58f0\u660e\u7c7b\u7684\u65f6\u5019\u5c31\u8981\u5bf9\u5176\u6210\u5458\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u3002","title":"\u7c7b\uff08Classes\uff09"},{"location":"13-2-classes-and-class-members/#_1","text":"\u9664\u4e86\u5b58\u653e\u6570\u636e\u4e4b\u5916\uff0c\u7c7b\uff08\u548c\u7ed3\u6784\u4f53\uff09\u540c\u6837\u8fd8\u53ef\u4ee5\u5305\u542b\u51fd\u6570\uff01\u5b9a\u4e49\u5728\u7c7b\u4e2d\u7684\u51fd\u6570\u79f0\u4e3a \u6210\u5458\u51fd\u6570 \uff08\u6216\u79f0\u4e3a\u65b9\u6cd5\uff09\u3002\u6210\u5458\u51fd\u6570\u7684\u5b9a\u4e49\u53ef\u4ee5\u88ab\u5305\u542b\u5728\u7c7b\u5185\uff0c\u4e5f\u53ef\u4ee5\u5b9a\u4e49\u5728\u7c7b\u5916\u3002\u6211\u4eec\u6682\u65f6\u5c06\u5176\u5b9a\u4e49\u5728\u7c7b\u5185\uff0c\u8fd9\u79cd\u5f62\u5f0f\u76f8\u5bf9\u66f4\u7b80\u5355\uff0c\u7a0d\u540e\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5982\u4f55\u5728\u7c7b\u5916\u5b9a\u4e49\u7c7b\u7684\u65b9\u6cd5\u3002 \u5177\u6709\u6210\u5458\u51fd\u6570\u7684Data\u7c7b\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class DateClass { public : int m_year {}; int m_month {}; int m_day {}; void print () // defines a member function named print() { std :: cout << m_year << '/' << m_month << '/' << m_day ; } }; \u548c\u7ed3\u6784\u4f53\u6210\u5458\u4e00\u6837\uff0c\u7c7b\u7684\u6210\u5458\uff08\u5305\u62ec\u53d8\u91cf\u548c\u51fd\u6570\uff09\u53ef\u4ee5\u901a\u8fc7 \u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26(.) \u6765\u8bbf\u95ee\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include class DateClass { public : int m_year {}; int m_month {}; int m_day {}; void print () { std :: cout << m_year << '/' << m_month << '/' << m_day ; } }; int main () { DateClass today { 2020 , 10 , 14 }; today . m_day = 16 ; // use member selection operator to select a member variable of the class today . print (); // use member selection operator to call a member function of the class return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 2020/10/16 \u89c2\u5bdf\u53ef\u4ee5\u53d1\u73b0\uff0c\u4e0a\u8ff0\u4ee3\u7801\u548c\u4e4b\u524d\u4f7f\u7528\u7ed3\u6784\u4f53\u7684\u7248\u672c\u8fd1\u4e4e\u662f\u4e00\u81f4\u7684\u3002 \u4e0d\u8fc7\uff0c\u5176\u4e2d\u8fd8\u662f\u6709\u51e0\u5904\u4e0d\u540c\u3002\u5728\u4f7f\u7528\u7ed3\u6784\u4f53\u7684\u7248\u672c\u4e2d\uff0c print() \u51fd\u6570\u9700\u8981\u5c06\u7ed3\u6784\u4f53\u672c\u8eab\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\uff0c\u5426\u5219 print() \u51fd\u6570\u5c31\u65e0\u4ece\u5224\u65ad\u6211\u4eec\u8981\u6253\u5370\u7684\u662f\u54ea\u4e2a\u7ed3\u6784\u4f53\u7684\u5185\u5bb9\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u5728\u51fd\u6570\u4e2d\u660e\u786e\u5730\u4f7f\u7528\u8be5\u53c2\u6570\u3002 \u6210\u5458\u51fd\u6570\u7684\u5de5\u4f5c\u65b9\u5f0f\u5219\u7565\u6709\u4e0d\u540c\uff1a\u6240\u6709\u6210\u5458\u51fd\u6570\u7684\u8c03\u7528\u5fc5\u987b\u57fa\u4e8e\u8be5\u7c7b\u7684\u4e00\u4e2a\u5177\u4f53\u5bf9\u8c61\u3002\u5f53\u6211\u4eec\u8c03\u7528 today.print() \u7684\u65f6\u5019\uff0c\u5b9e\u9645\u4e0a\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\u8c03\u7528 today \u5bf9\u8c61\u7684\u6210\u5458\u51fd\u6570 print() \u3002 \u8ba9\u6211\u4eec\u5728\u770b\u4e00\u773c\u6210\u5458\u51fd\u6570 print \u7684\u5b9a\u4e49\uff1a 1 2 3 4 void print () // defines a member function named print() { std :: cout << m_year << '/' << m_month << '/' << m_day ; } m_year \u3001 m_month \u548c m_day \u662f\u8c01\u7684\u53d8\u91cf\uff1f\u5b83\u4eec\u6307\u7684\u662f\u5176\u51fd\u6570\u8c03\u7528\u65f6\u5173\u8054\u7684\u5bf9\u8c61\u6240\u5c5e\u7684\u53d8\u91cf\u3002 \u56e0\u6b64\u5f53\u8c03\u7528 today.print() \u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c06 m_day \u89e3\u6790\u4e3a today.m_day \uff0c\u5c06 m_month \u89e3\u6790\u4e3a today.m_month \uff0c\u5c06 m_year \u89e3\u6790\u4e3a today.m_year \u3002\u5f53\u8c03\u7528 tomorrow.print() \u65f6\uff0c\u5219 m_day \u89e3\u6790\u4e3a tomorrow.m_day \u3002 \u53ef\u89c1\uff0c\u76f8\u5173\u8054\u7684\u5bf9\u8c61\u4f1a\u88ab\u9690\u5f0f\u5730\u4f20\u5165\u6210\u5458\u51fd\u6570\u3002\u56e0\u6b64\uff0c\u5b83\u901a\u5e38\u88ab\u79f0\u4e3a \u9690\u5f0f\u5bf9\u8c61 \u3002 \u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u9690\u5f0f\u5bf9\u8c61\u4f20\u9012\u7684\u5de5\u4f5c\u539f\u7406\u3002 \u5bf9\u4e8e\u975e\u6210\u5458\u51fd\u6570\u6765\u8bf4\uff0c\u6211\u4eec\u5fc5\u987b\u5c06\u5b83\u9700\u8981\u64cd\u4f5c\u7684\u6570\u636e\u4f20\u9012\u7ed9\u51fd\u6570\u3002\u800c\u5bf9\u4e8e\u6210\u5458\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5047\u5b9a\u5b83\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8fd9\u4e2a\u7c7b\u7684\u5bf9\u8c61\u3002 \u4e3a\u6210\u5458\u53d8\u91cf\u6dfb\u52a0 \u201c m_ \u201d \u524d\u7f00\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u533a\u5206\u6210\u5458\u53d8\u91cf\u3001\u51fd\u6570\u53c2\u6570\u548c\u5c40\u90e8\u53d8\u91cf\u3002\u8fd9\u4e48\u505a\u6709\u8bf8\u591a\u597d\u5904\uff1a\u9996\u5148\uff0c\u5f53\u6211\u4eec\u770b\u5230\u5bf9\u5177\u6709 \u201c m_ \u201d\u524d\u7f00\u7684\u53d8\u91cf\u8fdb\u884c\u8d4b\u503c\u65f6\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u6307\u5b9a\u8be5\u64cd\u4f5c\u4f1a\u6539\u53d8\u5f53\u524d\u7c7b\u5b9e\u4f8b\u7684\u72b6\u6001\u3002\u5176\u6b21\uff0c\u51fd\u6570 \u5f62\u53c2 \u6216\u5c40\u90e8\u53d8\u91cf\u88ab\u58f0\u660e\u5728\u5f53\u524d\u51fd\u6570\u4e2d\uff0c\u800c\u6210\u5458\u53d8\u91cf\u5219\u4e0d\u540c\uff0c\u5b83\u88ab\u5b9a\u4e49\u5728\u7c7b\u4e2d\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u77e5\u9053 m_ \u524d\u7f00\u7684\u53d8\u91cf\u7684\u5177\u4f53\u5b9a\u4e49\uff0c\u5c31\u9700\u8981\u53bb\u7c7b\u7684\u5b9a\u4e49\u4e2d\u67e5\u770b\uff0c\u800c\u4e0d\u662f\u5728\u5f53\u524d\u51fd\u6570\u4e2d\u67e5\u770b\u3002 \u6309\u7167\u7ba1\u7406\uff0c\u7c7b\u540d\u901a\u5e38\u662f\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u7c7b\u547d\u540d\u65f6\uff0c\u4f7f\u7528\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u3002 \u518d\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include #include class Employee { public : std :: string m_name {}; int m_id {}; double m_wage {}; // Print employee information to the screen void print () { std :: cout << \"Name: \" << m_name << \" Id: \" << m_id << \" Wage: $\" << m_wage << '\\n' ; } }; int main () { // Declare two employees Employee alex { \"Alex\" , 1 , 25.00 }; Employee joe { \"Joe\" , 2 , 22.25 }; // Print out the employee information alex . print (); joe . print (); return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 Name: Alex Id: 1 Wage: $25 Name: Joe Id: 2 Wage: $22.25 \u5bf9\u4e8e\u975e\u6210\u5458\u51fd\u6570\u6765\u8bf4\uff0c\u5b83\u4e0d\u80fd\u8c03\u7528\u88ab\u5b9a\u4e49\u5728\u5b83\u4e0b\u65b9\uff08\u5373\u540e\u5b9a\u4e49\uff09\u7684\u51fd\u6570\uff0c\u9664\u975e\u5b83\u80fd\u591f\u770b\u5230\u8be5\u51fd\u6570\u7684 \u524d\u5411\u58f0\u660e \uff1a 1 2 3 4 5 6 7 8 void x () { // You can't call y() from here unless the compiler has already seen a forward declaration for y() } void y () { } \u800c\u5bf9\u4e8e\u6210\u5458\u51fd\u6570\u6765\u8bf4\uff0c\u5219\u6ca1\u6709\u8be5\u9650\u5236\uff1a 1 2 3 4 5 6 class foo { public : void x () { y (); } // okay to call y() here, even though y() isn't defined until later in this class void y () { }; };","title":"\u6210\u5458\u51fd\u6570"},{"location":"13-2-classes-and-class-members/#_2","text":"\u9664\u4e86\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u51fd\u6570\uff0c\u7c7b\u8fd8\u53ef\u4ee5\u5177\u6709 \u6210\u5458\u7c7b\u578b \u6216\u5d4c\u5957\u7c7b\u578b\uff08\u4e5f\u5305\u62ec \u7c7b\u578b\u522b\u540d \uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Employee { public : using IDType = int ; std :: string m_name {}; IDType m_id {}; double m_wage {}; // Print employee information to the screen void print () { std :: cout << \"Name: \" << m_name << \" Id: \" << m_id << \" Wage: $\" << m_wage << '\\n' ; } }; \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7c7b\u540d\u5b9e\u9645\u4e0a\u6210\u4e3a\u4e86\u5176\u5d4c\u5957\u7c7b\u578b\u7684 \u547d\u540d\u7a7a\u95f4 \u3002\u5728\u8be5\u7c7b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 IDType \uff0c\u800c\u5728\u7c7b\u5916\uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528 Employee::IDType \u6765\u8bbf\u95ee\u8be5\u7c7b\u578b\u3002 \u5982\u679c\u5728\u540e\u7eed\u7684\u5f00\u53d1\u4e2d\u6211\u4eec\u53d1\u73b0 int \u7c7b\u578b\u5df2\u7ecf\u65e0\u6cd5\u6ee1\u8db3\u9700\u8981\uff0c\u800c\u5fc5\u987b\u6539\u7528 std::string \uff0c\u6b64\u65f6\u6211\u4eec\u53ea\u9700\u8981\u66f4\u65b0\u8fd9\u4e2a\u7c7b\u578b\u522b\u540d\uff0c\u800c\u4e0d\u9700\u8981\u5c06\u6240\u6709\u7684 int \u90fd\u66ff\u6362\u4e3a std::string \u3002 \u5d4c\u5957\u7c7b\u578b\u4e0d\u80fd\u88ab\u524d\u5411\u58f0\u660e\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5d4c\u5957\u7c7b\u578b\u5e94\u8be5\u53ea\u5728\u5305\u542b\u5b83\u7684\u7c7b\u4e2d\u4f7f\u7528\u3002\u6ce8\u610f\uff0c\u56e0\u4e3a\u7c7b\u4e5f\u662f\u4e00\u79cd\u7c7b\u578b\uff0c\u6240\u4ee5\u7c7b\u53ef\u4ee5\u88ab\u5d4c\u5957\u5728\u7c7b\u4e2d\u2014\u2014\u8fd9\u4e2a\u505a\u6cd5\u5e76\u4e0d\u5e38\u89c1\u800c\u4e14\u901a\u5e38\u53ea\u6709\u9ad8\u7ea7\u7a0b\u5e8f\u5458\u624d\u4f1a\u53bb\u4f7f\u7528\u3002","title":"\u6210\u5458\u7c7b\u578b"},{"location":"13-2-classes-and-class-members/#c","text":"\u5728C\u8bed\u8a00\u4e2d\uff0c\u7ed3\u6784\u4f53\u53ea\u6709\u6570\u636e\u6210\u5458\uff0c\u6ca1\u6709\u6210\u5458\u51fd\u6570\u3002\u5728c++\u4e2d\uff0c\u5728\u8bbe\u8ba1\u5b8c\u7c7b(\u4f7f\u7528class\u5173\u952e\u5b57)\u540e\uff0cBjarne Stroustrup\u82b1\u4e86\u4e00\u4e9b\u65f6\u95f4\u8003\u8651\u662f\u5426\u5e94\u8be5\u8d4b\u4e88\u7ed3\u6784\u4f53(\u7ee7\u627f\u81eaC)\u5177\u6709\u6210\u5458\u51fd\u6570\u7684\u80fd\u529b\u3002\u7ecf\u8fc7\u8003\u8651\uff0c\u4ed6\u51b3\u5b9a\u7ed3\u6784\u4f53\u5e94\u8be5\u80fd\u591f\u5177\u6709\u6210\u5458\u51fd\u6570\uff0c\u8fd9\u6837\u4e00\u6765\u53ef\u4ee5\u5728\u67d0\u79cd\u7a0b\u5ea6\u4e0a\u4e3a\u7c7b\u548c\u7ed3\u6784\u4f53\u6307\u5b9a\u7edf\u4e00\u7684\u89c4\u5219\u96c6\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u6211\u4eec\u4f7f\u7528class\u5173\u952e\u5b57\u7f16\u5199\u4e86\u4e0a\u9762\u7684\u7a0b\u5e8f\uff0c\u4f46\u5176\u5b9e\u53ef\u4ee5\u4f7f\u7528struct\u5173\u952e\u5b57\u6765\u4ee3\u66ff\u3002 \u8bb8\u591a\u5f00\u53d1\u8005(\u5305\u62ec\u6211\u81ea\u5df1)\u8ba4\u4e3a\u8fd9\u662f\u4e00\u4e2a\u4e0d\u6b63\u786e\u7684\u51b3\u5b9a\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u5bfc\u81f4\u5371\u9669\u7684\u5047\u8bbe\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4e00\u4e2a\u7c7b\u4f1a\u5728\u9500\u6bc1\u540e\u6e05\u7406\u5b83\u81ea\u5df1\u662f\u5408\u7406\u7684(\u4f8b\u5982\uff0c\u4e00\u4e2a\u5206\u914d\u5185\u5b58\u7684\u7c7b\u4f1a\u5728\u88ab\u9500\u6bc1\u4e4b\u524d\u91ca\u653e\u5b83\u6240\u4f7f\u7528\u7684\u5185\u5b58)\uff0c\u4f46\u5047\u8bbe\u4e00\u4e2a\u7ed3\u6784\u4f53\u4e5f\u4f1a\u8fd9\u4e48\u505a\u5219\u662f\u4e0d\u5b89\u5168\u7684\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5efa\u8bae\u5bf9\u53ea\u6709\u6570\u636e\u7684\u7ed3\u6784\u4f53\u4f7f\u7528struct\u5173\u952e\u5b57\uff0c\u800c\u5bf9\u5b9a\u4e49\u9700\u8981\u6570\u636e\u548c\u51fd\u6570\u6346\u7ed1\u5728\u4e00\u8d77\u7684\u5bf9\u8c61\u65f6\u4f7f\u7528class\u5173\u952e\u5b57\u3002 \u6700\u4f73\u5b9e\u8df5 struct \u5173\u952e\u5b57\u53ea\u9002\u7528\u4e8e\u4ec5\u5305\u542b\u6570\u636e\u7684\u7ed3\u6784\u3002\u800c\u5bf9\u4e8e\u540c\u65f6\u5305\u542b\u6570\u636e\u548c\u51fd\u6570\u7684\u5bf9\u8c61\uff0c\u8bf7\u4f7f\u7528class\u5173\u952e\u5b57\u3002 \u5176\u5b9e\u4f60\u5df2\u7ecf\u5728\u4e0d\u77e5\u60c5\u7684\u60c5\u51b5\u4e0b\u591a\u6b21\u4f7f\u7528class\u4e86\u3002 \u5b9e\u9645\u4e0a\uff0cC++\u6807\u51c6\u5e93\u91cc\u5145\u6ee1\u4e86\u5404\u5f0f\u5404\u6837\u7684\u7c7b\uff0c\u5b83\u4eec\u90fd\u53ef\u4ee5\u65b9\u4fbf\u4f60\u5de5\u4f5c\uff0c std::string \u3001 std::vector \u548c std::array \u90fd\u662fclass\uff01\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u521b\u5efa\u8fd9\u4e9b\u7c7b\u578b\u7684\u5bf9\u8c61\u65f6\uff0c\u4f60\u5c31\u5728\u5b9e\u4f8b\u5316\u4e00\u4e2a\u7c7b\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u800c\u5f53\u4f60\u4f7f\u7528\u8fd9\u4e9b\u5bf9\u8c61\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u8c03\u7528\u7684\u90fd\u662f\u5b83\u4eec\u7684\u6210\u5458\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include #include #include int main () { std :: string s { \"Hello, world!\" }; // instantiate a string class object std :: array < int , 3 > a { 1 , 2 , 3 }; // instantiate an array class object std :: vector < double > v { 1.1 , 2.2 , 3.3 }; // instantiate a vector class object std :: cout << \"length: \" << s . length () << '\\n' ; // call a member function return 0 ; }","title":"\u5173\u4e8e C++ \u4e2d\u7ed3\u6784\u4f53\u7684\u4e00\u4e9b\u6ce8\u610f\u4e8b\u9879"},{"location":"13-2-classes-and-class-members/#_3","text":"class\u5173\u952e\u5b57\u5141\u8bb8\u6211\u4eec\u5728C++\u4e2d\u521b\u5efa\u4e00\u4e2a\u65e2\u53ef\u4ee5\u5305\u542b\u6210\u5458\u53d8\u91cf\u53c8\u53ef\u4ee5\u5305\u542b\u6210\u5458\u51fd\u6570\u7684\u81ea\u5b9a\u4e49\u7c7b\u578b\u3002\u7c7b\u6784\u6210\u4e86\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u57fa\u7840\uff0c\u6211\u4eec\u5c06\u7528\u672c\u7ae0\u7684\u5176\u4f59\u90e8\u5206\u548c\u5c06\u6765\u7684\u8bb8\u591a\u7ae0\u8282\u6765\u63a2\u7d22\u7c7b\u4e3a\u6211\u4eec\u63d0\u4f9b\u7684\u4e00\u5207\u80fd\u529b\u3002","title":"\u5c0f\u7ed3"},{"location":"13-3-public-vs-private-access-specifiers/","tags":["access specifier"],"text":"Key Takeaway \u516c\u6709\u548c\u79c1\u6709\u6210\u5458 \u8003\u8651\u4e0b\u9762\u7684\u7ed3\u6784\u4f53\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 struct DateStruct // \u6210\u5458\u9ed8\u8ba4\u662f\u516c\u6709\u7684 { int month {}; // public by default, can be accessed by anyone int day {}; // public by default, can be accessed by anyone int year {}; // public by default, can be accessed by anyone }; int main () { DateStruct date ; date . month = 10 ; date . day = 14 ; date . year = 2020 ; return 0 ; } \u5728\u4e0a\u9762\u4f8b\u5b50\u7684 main() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a DateStruct \u7ed3\u6784\u4f53\uff0c\u7136\u540e\u76f4\u63a5\u8bbf\u95ee\u5b83\u7684\u6210\u5458\uff0c\u4ee5\u4fbf\u7ed9\u5b83\u4eec\u8d4b\u503c\u3002\u8fd9\u662f\u53ef\u884c\u7684\uff0c\u56e0\u4e3a\u7ed3\u6784\u4f53\u7684\u6240\u6709\u6210\u5458\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u90fd\u662f\u516c\u6709\u6210\u5458\u3002 \u516c\u6709\u6210\u5458 \u662f\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u7684\u7ed3\u6784\u6216\u7c7b\u7684\u6210\u5458\uff0c\u5305\u62ec\u4ece\u7ed3\u6784\u6216\u7c7b\u5916\u90e8\u7684\u4ee3\u7801\u8bbf\u95ee\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570 main() \u4f4d\u4e8e\u7ed3\u6784\u4f53\u5916\u90e8\uff0c\u4f46\u5b83\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u6210\u5458\u6708\u3001\u65e5\u548c\u5e74\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u516c\u6709\u6210\u5458\u3002 \u7ed3\u6784\u4f53\u6216\u8005\u7c7b\u5916\u90e8\u7684\u4ee3\u7801\u6709\u65f6\u79f0\u4e3a public\uff0cpublic\u53ea\u80fd\u591f\u8bbf\u95ee\u7ed3\u6784\u4f53\u6216\u7c7b\u7684\u516c\u6709\u6210\u5458\u3002 \u8bf7\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u51e0\u4e4e\u7c7b\u4f3c\u7684class\u7248\u672c\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class DateClass // members are private by default { int m_month {}; // private by default, can only be accessed by other members int m_day {}; // private by default, can only be accessed by other members int m_year {}; // private by default, can only be accessed by other members }; int main () { DateClass date ; date . m_month = 10 ; // error date . m_day = 14 ; // error date . m_year = 2020 ; // error return 0 ; } \u7f16\u8bd1\u8fd9\u4e2a\u7a0b\u5e8f\u4f1a\u62a5\u9519\u3002\u8fd9\u662f\u56e0\u4e3a\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u7c7b\u7684\u6240\u6709\u6210\u5458\u90fd\u662f\u79c1\u6709\u7684\u3002 \u79c1\u6709\u6210\u5458 \u53ea\u80fd\u88ab\u8be5\u7c7b\u7684\u5176\u4ed6\u6210\u5458\u8bbf\u95ee(\u4e0d\u80fd\u88abpublic\u8bbf\u95ee)\u3002\u56e0\u4e3a main() \u4e0d\u662f DateClass \u7684\u6210\u5458\uff0c\u6240\u4ee5\u5b83\u4e0d\u80fd\u8bbf\u95ee date \u7684\u79c1\u6709\u6210\u5458\u3002 \u8bbf\u95ee\u4fee\u9970\u7b26 \u5c3d\u7ba1\u7c7b\u6210\u5458\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u79c1\u6709\u7684\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528public\u5173\u952e\u5b57\u5c06\u5b83\u4eec\u8bbe\u4e3a\u516c\u6709\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class DateClass { public : // note use of public keyword here, and the colon int m_month {}; // public, can be accessed by anyone int m_day {}; // public, can be accessed by anyone int m_year {}; // public, can be accessed by anyone }; int main () { DateClass date ; date . m_month = 10 ; // okay because m_month is public date . m_day = 14 ; // okay because m_day is public date . m_year = 2020 ; // okay because m_year is public return 0 ; } \u56e0\u4e3a DateClass \u7684\u6210\u5458\u73b0\u5728\u662f\u516c\u6709\u7684\u4e86\uff0c\u56e0\u6b64\u5b83\u4eec\u53ef\u4ee5\u88ab main() \u8bbf\u95ee\u3002 public \u5173\u952e\u5b57\u53ca\u5176\u540e\u9762\u7684\u5206\u53f7\uff0c\u79f0\u4e3a \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \uff0c\u5b83\u53ef\u4ee5\u8bbe\u7f6e\u8c01\u53ef\u4ee5\u8bbf\u95ee\u5bf9\u5e94\u7684\u6210\u5458\u3002\u6bcf\u4e2a\u6210\u5458\u90fd\u53ef\u4ee5\u4ece\u5176\u524d\u9762\u7684\u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26\u83b7\u53d6\u5bf9\u5e94\u7684\u8bbf\u95ee\u7b49\u7ea7\uff08\u5982\u679c\u6ca1\u6709\u5bf9\u5e94\u7684\u4fee\u9970\u7b26\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u7684\u8bbf\u95ee\u4fee\u9970\u7b26\uff09\u3002 C++\u63d0\u4f9b\u4e863\u4e2a\u4e0d\u540c\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u5173\u952e\u5b57\uff1a public \u3001 private \u548c protected \u3002 public \u548c private \u5206\u522b\u7528\u6765\u4f7f\u5b83\u4eec\u540e\u9762\u7684\u6210\u5458\u6210\u4e3a \u516c\u6709\u6210\u5458 \u6216 \u79c1\u6709\u6210\u5458 \u3002\u7b2c\u4e09\u4e2a\u8bbf\u95ee\u8bf4\u660e\u7b26 protected \u7684\u5de5\u4f5c\u539f\u7406\u4e0e private \u975e\u5e38\u76f8\u4f3c\u3002\u6211\u4eec\u5c06\u5728\u8ba8\u8bba\u7ee7\u627f\u65f6\u8ba8\u8bba private \u548c protected \u7684\u533a\u522b\u3002 \u6df7\u5408\u4f7f\u7528\u8bbf\u95ee\u4fee\u9970\u7b26 \u4e00\u4e2a\u7c7b\u53ef\u4ee5(\u800c\u4e14\u51e0\u4e4e\u603b\u662f)\u4f7f\u7528\u591a\u4e2a\u8bbf\u95ee\u8bf4\u660e\u7b26\u6765\u8bbe\u7f6e\u5176\u6210\u5458\u7684\u8bbf\u95ee\u7ea7\u522b\u3002\u4e00\u4e2a\u7c7b\u4e2d\u53ef\u4ee5\u4f7f\u7528\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u7684\u6570\u91cf\u662f\u6ca1\u6709\u9650\u5236\u7684\u3002 \u4e00\u822c\u6765\u8bf4\uff0c\u6210\u5458\u53d8\u91cf\u901a\u5e38\u8bbe\u4e3a\u79c1\u6709\uff0c\u6210\u5458\u51fd\u6570\u901a\u5e38\u8bbe\u4e3a\u516c\u6709\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u4ed4\u7ec6\u7814\u7a76\u5176\u4e2d\u7684\u539f\u56e0\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c06\u6210\u5458\u53d8\u91cf\u8bbe\u4e3a\u79c1\u6709\uff0c\u5c06\u6210\u5458\u51fd\u6570\u8bbe\u4e3a\u516c\u5171\uff0c\u9664\u975e\u60a8\u6709\u5145\u5206\u7684\u7406\u7531\u4e0d\u8fd9\u6837\u505a\u3002 \u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u540c\u65f6\u4f7f\u7528\u79c1\u6709\u548c\u516c\u5171\u8bbf\u95ee\u7684\u7c7b\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include class DateClass // members are private by default { int m_month {}; // private by default, can only be accessed by other members int m_day {}; // private by default, can only be accessed by other members int m_year {}; // private by default, can only be accessed by other members public : void setDate ( int month , int day , int year ) // public, can be accessed by anyone { // setDate() can access the private members of the class because it is a member of the class itself m_month = month ; m_day = day ; m_year = year ; } void print () // public, can be accessed by anyone { std :: cout << m_month << '/' << m_day << '/' << m_year ; } }; int main () { DateClass date ; date . setDate ( 10 , 14 , 2020 ); // okay, because setDate() is public date . print (); // okay, because print() is public std :: cout << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 10/14/2020 \u6ce8\u610f\uff0c\u5c3d\u7ba1\u6211\u4eec\u4e0d\u80fd\u5728 main \u4e2d\u76f4\u63a5\u8bbf\u95ee\u53d8\u91cf m_month \u3001 m_day \u548c m_year \uff08\u56e0\u4e3a\u5b83\u4eec\u662f\u79c1\u6709\u7684\uff09\uff0c\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u516c\u6709\u6210\u5458\u51fd\u6570 setDate() \u548c print() \u95f4\u63a5\u5730\u8bbf\u95ee\u5b83\u4eec\uff01 \u7c7b\u4e2d\u516c\u6709\u6210\u5458\u7684\u96c6\u5408\u901a\u5e38\u79f0\u4e3a\u8be5\u7c7b\u7684\u516c\u5f00\u63a5\u53e3\u3002\u56e0\u4e3a\u53ea\u6709\u516c\u6709\u6210\u5458\u624d\u80fd\u5728\u7c7b\u5916\u88ab\u8bbf\u95ee\uff0c\u6240\u4ee5\u516c\u5f00\u63a5\u53e3\u5b9e\u9645\u4e0a\u5b9a\u4e49\u4e86\u7a0b\u5e8f\u5e94\u8be5\u5982\u4f55\u4f7f\u7528\u63a5\u53e3\u548c\u7c7b\u8fdb\u884c\u4ea4\u4e92\u3002\u6ce8\u610f\uff0c main() \u53ea\u80fd\u591f\u8bbe\u7f6e\u65e5\u671f\u548c\u6253\u5370\u65e5\u671f\u3002\u8be5\u7c7b\u53ef\u4ee5\u4fdd\u62a4\u81ea\u5df1\u7684\u6210\u5458\u88ab\u76f4\u63a5\u8bbf\u95ee\u6216\u4fee\u6539\u3002 \u6709\u7684\u7a0b\u5e8f\u5458\u559c\u6b22\u5c06\u79c1\u6709\u6210\u5458\u5b9a\u4e49\u5728\u516c\u6709\u6210\u5458\u524d\u9762\uff0c\u56e0\u4e3a\u516c\u6709\u6210\u5458\u901a\u5e38\u4f1a\u4f7f\u7528\u79c1\u6709\u6210\u5458\uff0c\u6240\u4ee5\u5c06\u79c1\u6709\u7684\u5b9a\u4e49\u5728\u524d\u662f\u6709\u9053\u7406\u7684\u3002\u4f46\u662f\uff0c\u53cd\u65b9\u7684\u89c2\u70b9\u4e5f\u5f88\u6709\u529b\uff0c\u4ed6\u4eec\u8ba4\u4e3a\u7528\u6237\u6839\u672c\u4e0d\u5173\u5fc3\u7c7b\u7684\u79c1\u6709\u6210\u5458\uff0c\u6240\u4ee5\u5e94\u8be5\u5c06\u516c\u6709\u6210\u5458\u5b9a\u4e49\u5728\u524d\u9762\u3002\u5176\u5b9e\u4e24\u79cd\u65b9\u6cd5\u90fd\u662f\u53ef\u4ee5\u7684\u3002 \u8bbf\u95ee\u63a7\u5236\u5728\u7c7b\u5c42\u9762\u5de5\u4f5c\uff08\u800c\u975e\u5bf9\u8c61\u5c42\u9762\uff09 \u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include class DateClass // members are private by default { int m_month {}; // private by default, can only be accessed by other members int m_day {}; // private by default, can only be accessed by other members int m_year {}; // private by default, can only be accessed by other members public : void setDate ( int month , int day , int year ) { m_month = month ; m_day = day ; m_year = year ; } void print () { std :: cout << m_month << '/' << m_day << '/' << m_year ; } // Note the addition of this function void copyFrom ( const DateClass & d ) { // Note that we can access the private members of d directly m_month = d . m_month ; m_day = d . m_day ; m_year = d . m_year ; } }; int main () { DateClass date ; date . setDate ( 10 , 14 , 2020 ); // okay, because setDate() is public DateClass copy {}; copy . copyFrom ( date ); // okay, because copyFrom() is public copy . print (); std :: cout << '\\n' ; return 0 ; } \u8be5\u4f8b\u4e2d\u5305\u542b\u4e00\u4e2a C++ \u4e2d\u5e38\u88ab\u5ffd\u7565\u6216\u8bef\u89e3\u7684\u7ec6\u8282\uff0c\u8bbf\u95ee\u63a7\u5236\u662f\u5de5\u4f5c\u5728\u7c7b\u7684\u5c42\u9762\uff0c\u800c\u4e0d\u662f\u5bf9\u8c61\u5c42\u9762\u3002 \u8fd9\u5c31\u610f\u5473\u7740\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee \u7c7b\u4e2d\u7684\u67d0\u4e2a\u79c1\u6709\u6210\u5458 \uff0c\u90a3\u4e48\u8be5\u7c7b\u6240\u6709\u5bf9\u8c61\u4e2d\u7684\u8be5\u79c1\u6709\u6210\u5458\u4e5f\u90fd\u80fd\u591f\u88ab\u8be5\u51fd\u6570\u8bbf\u95ee\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c copyFrom() \u662f\u7c7b DateClass \u7684\u6210\u5458\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee DateClass \u7684\u79c1\u6709\u6210\u5458\u3002\u56e0\u6b64\uff0c copyFrom() \u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u8bbf\u95ee\u8c03\u7528\u5b83\u7684\u5bf9\u8c61\uff08 copy \uff09\uff0c\u8fd8\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5b83\u7684\u5f62\u53c2\uff08 DataClass \u7c7b\u578b\u53d8\u91cf d \uff09\u7684\u79c1\u6709\u6210\u5458\u3002\u5982\u679c\u8be5\u5f62\u53c2d\u662f\u5176\u4ed6\u7c7b\u578b\u7684\uff0c\u5219\u5176\u79c1\u6709\u6210\u5458\u4e0d\u53ef\u4ee5\u88ab\u8bbf\u95ee\u3002 \u8fd9\u4e2a\u7279\u6027\u975e\u5e38\u6709\u7528\uff0c\u5c24\u5176\u662f\u5f53\u6211\u4eec\u9700\u8981\u62f7\u8d1d\u4e00\u4e2a\u5bf9\u8c61\u7ed9\u4e0e\u5b83\u540c\u5c5e\u4e00\u4e2a\u7c7b\u7684\u5bf9\u8c61\u65f6\u3002\u5728\u4e0b\u4e00\u7ae0\u4e2d\uff0c\u6211\u4eec\u8fd8\u4f1a\u8ba8\u8bba << \u8fd0\u7b97\u7b26\u7684\u91cd\u8f7d\uff0c\u5230\u65f6\u5019\u6211\u4eec\u8fd8\u4f1a\u91cd\u65b0\u4f53\u79ef\u8fd9\u4e2a\u95ee\u9898\u3002 \u7ed3\u6784\u4f53 vs \u7c7b \u8bbf\u95ee\u8bf4\u660e\u7b26\u4ecb\u7ecd\u5b8c\u6bd5\uff0c\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u8ba8\u8bbaC++\u4e2d\u7684\u7c7b\u548c\u7ed3\u6784\u4e4b\u95f4\u7684\u5b9e\u9645\u533a\u522b\u4e86\u3002\u7c7b\u7684\u6210\u5458\u9ed8\u8ba4\u4e3aprivate\u3002\u7ed3\u6784\u4f53\u7684\u7684\u6210\u5458\u5219\u9ed8\u8ba4\u4e3apublic\u3002 \u5c31\u662f\u8fd9\u6837! (\u597d\u5427\uff0c\u8fc2\u8150\u4e00\u70b9\uff0c\u5176\u5b9e\u8fd8\u6709\u4e00\u4e2a\u5c0f\u533a\u522b\u2014\u2014\u7ed3\u6784\u4f53\u9ed8\u8ba4\u4ece\u5176\u4ed6\u7c7b\u516c\u5f00\u7ee7\u627f\uff0c\u800c\u7c7b\u5219\u662f\u79c1\u6709\u7ee7\u627f\u3002\u6211\u4eec\u5c06\u5728\u4ee5\u540e\u7684\u7ae0\u8282\u4e2d\u8ba8\u8bba\u8fd9\u610f\u5473\u7740\u4ec0\u4e48\uff0c\u4f46\u8fd9\u4e00\u70b9\u5b9e\u9645\u4e0a\u662f\u65e0\u5173\u7d27\u8981\u7684\uff0c\u56e0\u4e3a\u65e0\u8bba\u5982\u4f55\u4f60\u90fd\u4e0d\u5e94\u8be5\u4f9d\u8d56\u5b83\u7684\u9ed8\u8ba4\u884c\u4e3a)\u3002","title":"13.3 - \u8bbf\u95ee\u4fee\u9970\u7b26 public \u548c private"},{"location":"13-3-public-vs-private-access-specifiers/#_1","text":"\u8003\u8651\u4e0b\u9762\u7684\u7ed3\u6784\u4f53\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 struct DateStruct // \u6210\u5458\u9ed8\u8ba4\u662f\u516c\u6709\u7684 { int month {}; // public by default, can be accessed by anyone int day {}; // public by default, can be accessed by anyone int year {}; // public by default, can be accessed by anyone }; int main () { DateStruct date ; date . month = 10 ; date . day = 14 ; date . year = 2020 ; return 0 ; } \u5728\u4e0a\u9762\u4f8b\u5b50\u7684 main() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a DateStruct \u7ed3\u6784\u4f53\uff0c\u7136\u540e\u76f4\u63a5\u8bbf\u95ee\u5b83\u7684\u6210\u5458\uff0c\u4ee5\u4fbf\u7ed9\u5b83\u4eec\u8d4b\u503c\u3002\u8fd9\u662f\u53ef\u884c\u7684\uff0c\u56e0\u4e3a\u7ed3\u6784\u4f53\u7684\u6240\u6709\u6210\u5458\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u90fd\u662f\u516c\u6709\u6210\u5458\u3002 \u516c\u6709\u6210\u5458 \u662f\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u7684\u7ed3\u6784\u6216\u7c7b\u7684\u6210\u5458\uff0c\u5305\u62ec\u4ece\u7ed3\u6784\u6216\u7c7b\u5916\u90e8\u7684\u4ee3\u7801\u8bbf\u95ee\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570 main() \u4f4d\u4e8e\u7ed3\u6784\u4f53\u5916\u90e8\uff0c\u4f46\u5b83\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u6210\u5458\u6708\u3001\u65e5\u548c\u5e74\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u516c\u6709\u6210\u5458\u3002 \u7ed3\u6784\u4f53\u6216\u8005\u7c7b\u5916\u90e8\u7684\u4ee3\u7801\u6709\u65f6\u79f0\u4e3a public\uff0cpublic\u53ea\u80fd\u591f\u8bbf\u95ee\u7ed3\u6784\u4f53\u6216\u7c7b\u7684\u516c\u6709\u6210\u5458\u3002 \u8bf7\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u51e0\u4e4e\u7c7b\u4f3c\u7684class\u7248\u672c\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class DateClass // members are private by default { int m_month {}; // private by default, can only be accessed by other members int m_day {}; // private by default, can only be accessed by other members int m_year {}; // private by default, can only be accessed by other members }; int main () { DateClass date ; date . m_month = 10 ; // error date . m_day = 14 ; // error date . m_year = 2020 ; // error return 0 ; } \u7f16\u8bd1\u8fd9\u4e2a\u7a0b\u5e8f\u4f1a\u62a5\u9519\u3002\u8fd9\u662f\u56e0\u4e3a\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u7c7b\u7684\u6240\u6709\u6210\u5458\u90fd\u662f\u79c1\u6709\u7684\u3002 \u79c1\u6709\u6210\u5458 \u53ea\u80fd\u88ab\u8be5\u7c7b\u7684\u5176\u4ed6\u6210\u5458\u8bbf\u95ee(\u4e0d\u80fd\u88abpublic\u8bbf\u95ee)\u3002\u56e0\u4e3a main() \u4e0d\u662f DateClass \u7684\u6210\u5458\uff0c\u6240\u4ee5\u5b83\u4e0d\u80fd\u8bbf\u95ee date \u7684\u79c1\u6709\u6210\u5458\u3002","title":"\u516c\u6709\u548c\u79c1\u6709\u6210\u5458"},{"location":"13-3-public-vs-private-access-specifiers/#_2","text":"\u5c3d\u7ba1\u7c7b\u6210\u5458\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u79c1\u6709\u7684\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528public\u5173\u952e\u5b57\u5c06\u5b83\u4eec\u8bbe\u4e3a\u516c\u6709\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class DateClass { public : // note use of public keyword here, and the colon int m_month {}; // public, can be accessed by anyone int m_day {}; // public, can be accessed by anyone int m_year {}; // public, can be accessed by anyone }; int main () { DateClass date ; date . m_month = 10 ; // okay because m_month is public date . m_day = 14 ; // okay because m_day is public date . m_year = 2020 ; // okay because m_year is public return 0 ; } \u56e0\u4e3a DateClass \u7684\u6210\u5458\u73b0\u5728\u662f\u516c\u6709\u7684\u4e86\uff0c\u56e0\u6b64\u5b83\u4eec\u53ef\u4ee5\u88ab main() \u8bbf\u95ee\u3002 public \u5173\u952e\u5b57\u53ca\u5176\u540e\u9762\u7684\u5206\u53f7\uff0c\u79f0\u4e3a \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \uff0c\u5b83\u53ef\u4ee5\u8bbe\u7f6e\u8c01\u53ef\u4ee5\u8bbf\u95ee\u5bf9\u5e94\u7684\u6210\u5458\u3002\u6bcf\u4e2a\u6210\u5458\u90fd\u53ef\u4ee5\u4ece\u5176\u524d\u9762\u7684\u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26\u83b7\u53d6\u5bf9\u5e94\u7684\u8bbf\u95ee\u7b49\u7ea7\uff08\u5982\u679c\u6ca1\u6709\u5bf9\u5e94\u7684\u4fee\u9970\u7b26\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u7684\u8bbf\u95ee\u4fee\u9970\u7b26\uff09\u3002 C++\u63d0\u4f9b\u4e863\u4e2a\u4e0d\u540c\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u5173\u952e\u5b57\uff1a public \u3001 private \u548c protected \u3002 public \u548c private \u5206\u522b\u7528\u6765\u4f7f\u5b83\u4eec\u540e\u9762\u7684\u6210\u5458\u6210\u4e3a \u516c\u6709\u6210\u5458 \u6216 \u79c1\u6709\u6210\u5458 \u3002\u7b2c\u4e09\u4e2a\u8bbf\u95ee\u8bf4\u660e\u7b26 protected \u7684\u5de5\u4f5c\u539f\u7406\u4e0e private \u975e\u5e38\u76f8\u4f3c\u3002\u6211\u4eec\u5c06\u5728\u8ba8\u8bba\u7ee7\u627f\u65f6\u8ba8\u8bba private \u548c protected \u7684\u533a\u522b\u3002","title":"\u8bbf\u95ee\u4fee\u9970\u7b26"},{"location":"13-3-public-vs-private-access-specifiers/#_3","text":"\u4e00\u4e2a\u7c7b\u53ef\u4ee5(\u800c\u4e14\u51e0\u4e4e\u603b\u662f)\u4f7f\u7528\u591a\u4e2a\u8bbf\u95ee\u8bf4\u660e\u7b26\u6765\u8bbe\u7f6e\u5176\u6210\u5458\u7684\u8bbf\u95ee\u7ea7\u522b\u3002\u4e00\u4e2a\u7c7b\u4e2d\u53ef\u4ee5\u4f7f\u7528\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u7684\u6570\u91cf\u662f\u6ca1\u6709\u9650\u5236\u7684\u3002 \u4e00\u822c\u6765\u8bf4\uff0c\u6210\u5458\u53d8\u91cf\u901a\u5e38\u8bbe\u4e3a\u79c1\u6709\uff0c\u6210\u5458\u51fd\u6570\u901a\u5e38\u8bbe\u4e3a\u516c\u6709\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u4ed4\u7ec6\u7814\u7a76\u5176\u4e2d\u7684\u539f\u56e0\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c06\u6210\u5458\u53d8\u91cf\u8bbe\u4e3a\u79c1\u6709\uff0c\u5c06\u6210\u5458\u51fd\u6570\u8bbe\u4e3a\u516c\u5171\uff0c\u9664\u975e\u60a8\u6709\u5145\u5206\u7684\u7406\u7531\u4e0d\u8fd9\u6837\u505a\u3002 \u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u540c\u65f6\u4f7f\u7528\u79c1\u6709\u548c\u516c\u5171\u8bbf\u95ee\u7684\u7c7b\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include class DateClass // members are private by default { int m_month {}; // private by default, can only be accessed by other members int m_day {}; // private by default, can only be accessed by other members int m_year {}; // private by default, can only be accessed by other members public : void setDate ( int month , int day , int year ) // public, can be accessed by anyone { // setDate() can access the private members of the class because it is a member of the class itself m_month = month ; m_day = day ; m_year = year ; } void print () // public, can be accessed by anyone { std :: cout << m_month << '/' << m_day << '/' << m_year ; } }; int main () { DateClass date ; date . setDate ( 10 , 14 , 2020 ); // okay, because setDate() is public date . print (); // okay, because print() is public std :: cout << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 10/14/2020 \u6ce8\u610f\uff0c\u5c3d\u7ba1\u6211\u4eec\u4e0d\u80fd\u5728 main \u4e2d\u76f4\u63a5\u8bbf\u95ee\u53d8\u91cf m_month \u3001 m_day \u548c m_year \uff08\u56e0\u4e3a\u5b83\u4eec\u662f\u79c1\u6709\u7684\uff09\uff0c\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u516c\u6709\u6210\u5458\u51fd\u6570 setDate() \u548c print() \u95f4\u63a5\u5730\u8bbf\u95ee\u5b83\u4eec\uff01 \u7c7b\u4e2d\u516c\u6709\u6210\u5458\u7684\u96c6\u5408\u901a\u5e38\u79f0\u4e3a\u8be5\u7c7b\u7684\u516c\u5f00\u63a5\u53e3\u3002\u56e0\u4e3a\u53ea\u6709\u516c\u6709\u6210\u5458\u624d\u80fd\u5728\u7c7b\u5916\u88ab\u8bbf\u95ee\uff0c\u6240\u4ee5\u516c\u5f00\u63a5\u53e3\u5b9e\u9645\u4e0a\u5b9a\u4e49\u4e86\u7a0b\u5e8f\u5e94\u8be5\u5982\u4f55\u4f7f\u7528\u63a5\u53e3\u548c\u7c7b\u8fdb\u884c\u4ea4\u4e92\u3002\u6ce8\u610f\uff0c main() \u53ea\u80fd\u591f\u8bbe\u7f6e\u65e5\u671f\u548c\u6253\u5370\u65e5\u671f\u3002\u8be5\u7c7b\u53ef\u4ee5\u4fdd\u62a4\u81ea\u5df1\u7684\u6210\u5458\u88ab\u76f4\u63a5\u8bbf\u95ee\u6216\u4fee\u6539\u3002 \u6709\u7684\u7a0b\u5e8f\u5458\u559c\u6b22\u5c06\u79c1\u6709\u6210\u5458\u5b9a\u4e49\u5728\u516c\u6709\u6210\u5458\u524d\u9762\uff0c\u56e0\u4e3a\u516c\u6709\u6210\u5458\u901a\u5e38\u4f1a\u4f7f\u7528\u79c1\u6709\u6210\u5458\uff0c\u6240\u4ee5\u5c06\u79c1\u6709\u7684\u5b9a\u4e49\u5728\u524d\u662f\u6709\u9053\u7406\u7684\u3002\u4f46\u662f\uff0c\u53cd\u65b9\u7684\u89c2\u70b9\u4e5f\u5f88\u6709\u529b\uff0c\u4ed6\u4eec\u8ba4\u4e3a\u7528\u6237\u6839\u672c\u4e0d\u5173\u5fc3\u7c7b\u7684\u79c1\u6709\u6210\u5458\uff0c\u6240\u4ee5\u5e94\u8be5\u5c06\u516c\u6709\u6210\u5458\u5b9a\u4e49\u5728\u524d\u9762\u3002\u5176\u5b9e\u4e24\u79cd\u65b9\u6cd5\u90fd\u662f\u53ef\u4ee5\u7684\u3002","title":"\u6df7\u5408\u4f7f\u7528\u8bbf\u95ee\u4fee\u9970\u7b26"},{"location":"13-3-public-vs-private-access-specifiers/#_4","text":"\u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include class DateClass // members are private by default { int m_month {}; // private by default, can only be accessed by other members int m_day {}; // private by default, can only be accessed by other members int m_year {}; // private by default, can only be accessed by other members public : void setDate ( int month , int day , int year ) { m_month = month ; m_day = day ; m_year = year ; } void print () { std :: cout << m_month << '/' << m_day << '/' << m_year ; } // Note the addition of this function void copyFrom ( const DateClass & d ) { // Note that we can access the private members of d directly m_month = d . m_month ; m_day = d . m_day ; m_year = d . m_year ; } }; int main () { DateClass date ; date . setDate ( 10 , 14 , 2020 ); // okay, because setDate() is public DateClass copy {}; copy . copyFrom ( date ); // okay, because copyFrom() is public copy . print (); std :: cout << '\\n' ; return 0 ; } \u8be5\u4f8b\u4e2d\u5305\u542b\u4e00\u4e2a C++ \u4e2d\u5e38\u88ab\u5ffd\u7565\u6216\u8bef\u89e3\u7684\u7ec6\u8282\uff0c\u8bbf\u95ee\u63a7\u5236\u662f\u5de5\u4f5c\u5728\u7c7b\u7684\u5c42\u9762\uff0c\u800c\u4e0d\u662f\u5bf9\u8c61\u5c42\u9762\u3002 \u8fd9\u5c31\u610f\u5473\u7740\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee \u7c7b\u4e2d\u7684\u67d0\u4e2a\u79c1\u6709\u6210\u5458 \uff0c\u90a3\u4e48\u8be5\u7c7b\u6240\u6709\u5bf9\u8c61\u4e2d\u7684\u8be5\u79c1\u6709\u6210\u5458\u4e5f\u90fd\u80fd\u591f\u88ab\u8be5\u51fd\u6570\u8bbf\u95ee\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c copyFrom() \u662f\u7c7b DateClass \u7684\u6210\u5458\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee DateClass \u7684\u79c1\u6709\u6210\u5458\u3002\u56e0\u6b64\uff0c copyFrom() \u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u8bbf\u95ee\u8c03\u7528\u5b83\u7684\u5bf9\u8c61\uff08 copy \uff09\uff0c\u8fd8\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5b83\u7684\u5f62\u53c2\uff08 DataClass \u7c7b\u578b\u53d8\u91cf d \uff09\u7684\u79c1\u6709\u6210\u5458\u3002\u5982\u679c\u8be5\u5f62\u53c2d\u662f\u5176\u4ed6\u7c7b\u578b\u7684\uff0c\u5219\u5176\u79c1\u6709\u6210\u5458\u4e0d\u53ef\u4ee5\u88ab\u8bbf\u95ee\u3002 \u8fd9\u4e2a\u7279\u6027\u975e\u5e38\u6709\u7528\uff0c\u5c24\u5176\u662f\u5f53\u6211\u4eec\u9700\u8981\u62f7\u8d1d\u4e00\u4e2a\u5bf9\u8c61\u7ed9\u4e0e\u5b83\u540c\u5c5e\u4e00\u4e2a\u7c7b\u7684\u5bf9\u8c61\u65f6\u3002\u5728\u4e0b\u4e00\u7ae0\u4e2d\uff0c\u6211\u4eec\u8fd8\u4f1a\u8ba8\u8bba << \u8fd0\u7b97\u7b26\u7684\u91cd\u8f7d\uff0c\u5230\u65f6\u5019\u6211\u4eec\u8fd8\u4f1a\u91cd\u65b0\u4f53\u79ef\u8fd9\u4e2a\u95ee\u9898\u3002","title":"\u8bbf\u95ee\u63a7\u5236\u5728\u7c7b\u5c42\u9762\u5de5\u4f5c\uff08\u800c\u975e\u5bf9\u8c61\u5c42\u9762\uff09"},{"location":"13-3-public-vs-private-access-specifiers/#vs","text":"\u8bbf\u95ee\u8bf4\u660e\u7b26\u4ecb\u7ecd\u5b8c\u6bd5\uff0c\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u8ba8\u8bbaC++\u4e2d\u7684\u7c7b\u548c\u7ed3\u6784\u4e4b\u95f4\u7684\u5b9e\u9645\u533a\u522b\u4e86\u3002\u7c7b\u7684\u6210\u5458\u9ed8\u8ba4\u4e3aprivate\u3002\u7ed3\u6784\u4f53\u7684\u7684\u6210\u5458\u5219\u9ed8\u8ba4\u4e3apublic\u3002 \u5c31\u662f\u8fd9\u6837! (\u597d\u5427\uff0c\u8fc2\u8150\u4e00\u70b9\uff0c\u5176\u5b9e\u8fd8\u6709\u4e00\u4e2a\u5c0f\u533a\u522b\u2014\u2014\u7ed3\u6784\u4f53\u9ed8\u8ba4\u4ece\u5176\u4ed6\u7c7b\u516c\u5f00\u7ee7\u627f\uff0c\u800c\u7c7b\u5219\u662f\u79c1\u6709\u7ee7\u627f\u3002\u6211\u4eec\u5c06\u5728\u4ee5\u540e\u7684\u7ae0\u8282\u4e2d\u8ba8\u8bba\u8fd9\u610f\u5473\u7740\u4ec0\u4e48\uff0c\u4f46\u8fd9\u4e00\u70b9\u5b9e\u9645\u4e0a\u662f\u65e0\u5173\u7d27\u8981\u7684\uff0c\u56e0\u4e3a\u65e0\u8bba\u5982\u4f55\u4f60\u90fd\u4e0d\u5e94\u8be5\u4f9d\u8d56\u5b83\u7684\u9ed8\u8ba4\u884c\u4e3a)\u3002","title":"\u7ed3\u6784\u4f53 vs \u7c7b"},{"location":"13-4-access-functions-and-encapsulation/","tags":["encapsulation"],"text":"Key Takeaway \u4e3a\u4ec0\u4e48\u8981\u5b9a\u4e49\u79c1\u6709\u6210\u5458\u53d8\u91cf \u4e66\u63a5\u4e0a\u6587\uff0c\u6211\u4eec\u63d0\u5230\uff0c\u7c7b\u6210\u5458\u53d8\u91cf\u901a\u5e38\u662f\u79c1\u6709\u7684\u3002\u6b63\u5728\u5b66\u4e60\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u5f00\u53d1\u4eba\u5458\u901a\u5e38\u5f88\u96be\u7406\u89e3\u8fd9\u4e48\u505a\u7684\u76ee\u7684\u3002\u5176\u5b9e\u6253\u4e2a\u6bd4\u65b9\u4f60\u5c31\u80fd\u591f\u660e\u767d\u4e86\u3002 \u751f\u6d3b\u4e2d\u6211\u4eec\u53ef\u4ee5\u63a5\u89e6\u5230\u8bb8\u591a\u7535\u5b50\u8bbe\u5907\u3002\u4f8b\u5982\u7535\u89c6\u9065\u63a7\u5668\uff0c\u4f60\u53ef\u4ee5\u7528\u5b83\u6765\u6253\u5f00/\u5173\u95ed\u7535\u89c6\u3002\u4f60\u53ef\u4ee5\u5f00\u8f66(\u6216\u8e0f\u677f\u8f66)\u53bb\u4e0a\u73ed\uff0c\u7528\u667a\u80fd\u624b\u673a\u62cd\u7167\u3002\u8fd9\u4e09\u79cd\u770b\u4f3c\u4e0d\u76f8\u5173\u7684\u4e1c\u897f\u5374\u6709\u7740\u4e00\u4e2a\u5171\u540c\u7684\u6a21\u5f0f\uff1a\u5b83\u4eec\u4e3a\u4f60\u63d0\u4f9b\u4e00\u4e2a\u7b80\u5355\u7684\u754c\u9762\u4f7f\u4f60\u53ef\u4ee5(\u4e00\u4e2a\u6309\u94ae\u3001\u4e00\u4e2a\u65b9\u5411\u76d8\u7b49\u7b49\u2026\u2026)\u6267\u884c\u4e00\u4e2a\u52a8\u4f5c\u3002\u7136\u800c\uff0c\u8fd9\u4e9b\u8bbe\u5907\u7684\u5b9e\u9645\u64cd\u4f5c\u65b9\u5f0f\u5374\u4e0d\u4e3a\u4f60\u6240\u77e5\u3002\u5f53\u4f60\u6309\u4e0b\u9065\u63a7\u5668\u4e0a\u7684\u6309\u94ae\u65f6\uff0c\u4f60\u4e0d\u9700\u8981\u77e5\u9053\u5b83\u662f\u5982\u4f55\u4e0e\u7535\u89c6\u901a\u4fe1\u7684\u3002\u5f53\u4f60\u8e29\u6cb9\u95e8\u65f6\uff0c\u4f60\u4e5f\u4e0d\u9700\u8981\u77e5\u9053\u5185\u71c3\u673a\u662f\u5982\u4f55\u8ba9\u8f6e\u5b50\u8f6c\u52a8\u7684\u3002\u5f53\u4f60\u62cd\u7167\u65f6\uff0c\u4f60\u4e0d\u9700\u8981\u77e5\u9053\u4f20\u611f\u5668\u662f\u5982\u4f55\u5c06\u5149\u7ebf\u805a\u96c6\u6210\u50cf\u7d20\u56fe\u50cf\u7684\u3002\u8fd9\u79cd\u63a5\u53e3\u548c\u5b9e\u73b0\u7684\u5206\u79bb\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u4e0d\u4e86\u89e3\u7269\u4f53\u5982\u4f55\u5de5\u4f5c\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u7269\u4f53\u3002\u8fd9\u6781\u5927\u5730\u964d\u4f4e\u4e86\u4f7f\u7528\u8fd9\u4e9b\u5bf9\u8c61\u7684\u590d\u6742\u6027\uff0c\u589e\u52a0\u4e86\u6211\u4eec\u80fd\u591f\u4e0e\u4e4b\u4ea4\u4e92\u7684\u5bf9\u8c61\u6570\u91cf\u3002 \u57fa\u4e8e\u540c\u6837\u7684\u539f\u56e0\uff0c\u5c06\u5b9e\u73b0\u548c\u63a5\u53e3\u5206\u79bb\u5728\u8f6f\u4ef6\u5de5\u7a0b\u4e2d\u4e5f\u662f\u975e\u5e38\u6709\u7528\u7684\u3002 \u5c01\u88c5 \u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c \u5c01\u88c5 \uff08\u6709\u65f6\u4e5f\u79f0\u4e3a\u4fe1\u606f\u5c4f\u853d\uff09\u662f\u6307\u5c06\u5bf9\u8c61\u7684\u5b9e\u73b0\u7ec6\u8282\u5bf9\u7528\u6237\u9690\u85cf\u8d77\u6765\u7684\u8fc7\u7a0b\u3002\u7528\u6237\u5728\u4f7f\u7528\u5bf9\u8c61\u7684\u65f6\u5019\u53ea\u9700\u8981\u901a\u8fc7\u516c\u5f00\u63a5\u53e3\u8bbf\u95ee\u5bf9\u8c61\u5373\u53ef\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u4e0d\u4e86\u89e3\u5bf9\u8c61\u5b9e\u73b0\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u8be5\u5bf9\u8c61\u3002 \u5728 C++ \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u6765\u5b9e\u73b0\u5c01\u88c5\u3002\u901a\u5e38\uff0c\u7c7b\u7684\u6240\u6709\u6210\u5458\u53d8\u91cf\u90fd\u662f\u79c1\u6709\u7684(\u9690\u85cf\u5b9e\u73b0\u7ec6\u8282)\uff0c\u800c\u5927\u591a\u6570\u6210\u5458\u51fd\u6570\u90fd\u662f\u516c\u6709\u7684(\u66b4\u9732\u63a5\u53e3)\u3002\u867d\u7136\u8981\u6c42\u7c7b\u7684\u7528\u6237\u4f7f\u7528\u516c\u5f00\u63a5\u53e3\u4e0e\u7c7b\u4ea4\u4e92\u6bd4\u76f4\u63a5\u63d0\u4f9b\u5bf9\u6210\u5458\u53d8\u91cf\u7684\u516c\u5171\u8bbf\u95ee\u66f4\u7e41\u7410\uff0c\u4f46\u8fd9\u6837\u505a\u5b9e\u9645\u4e0a\u76ca\u5904\u9887\u591a\uff0c\u6709\u52a9\u4e8e\u63d0\u9ad8\u7c7b\u7684\u53ef\u91cd\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002 \u6ce8\u610f\uff1a\u5c01\u88c5\u4e00\u8bcd\u6709\u65f6\u5019\u4e5f\u6307\u4ee3\u5bf9\u6570\u636e\u548c\u51fd\u6570\u7684\u6253\u5305\u3002\u8fd9\u91cc\u6211\u4eec\u53ea\u53d6\u5176\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\u7684\u542b\u4e49\u3002 \u597d\u5904 1 \uff1a\u7c7b\u5c01\u88c5\u53ef\u4ee5\u63d0\u9ad8\u5176\u6613\u7528\u6027\u5e76\u964d\u4f4e\u7a0b\u5e8f\u590d\u6742\u5ea6 \u5bf9\u4e8e\u4e00\u4e2a\u826f\u597d\u5c01\u88c5\u7684\u7c7b\u6765\u8bf4\uff0c\u4f60\u53ea\u9700\u5173\u5fc3\u8be5\u7c7b\u4e3a\u4f60\u63d0\u4f9b\u4e86\u54ea\u4e9b\u516c\u5171\u63a5\u53e3\uff0c\u8fd9\u4e9b\u63a5\u53e3\u9700\u8981\u4ec0\u4e48\u53c2\u6570\uff0c\u5b83\u4eec\u7684\u8fd4\u56de\u503c\u662f\u4ec0\u4e48\u3002\u800c\u8fd9\u4e2a\u7c7b\u7684\u5177\u4f53\u5185\u90e8\u5b9e\u73b0\u5219\u5b8c\u5168\u4e0d\u9700\u8981\u641e\u6e05\u695a\u3002\u4f8b\u5982\uff0c\u7c7b\u4e2d\u4fdd\u5b58\u4e86\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u540d\u5b57\u7684\u5217\u8868\uff0c\u800c\u8fd9\u4e2a\u5217\u8868\u7684\u5b9e\u73b0\u53ef\u4ee5\u662f\u5305\u542bC\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u7684\u52a8\u6001\u6570\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u662f std::array \u3001 std::vector \u3001 std::map \u3001 std::list \u6216\u8005\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u3002\u5728\u4f7f\u7528\u8be5\u7c7b\u7684\u5bf9\u8c61\u65f6\uff0c\u4e0d\u9700\u8981\u4e5f\u4e0d\u5173\u5fc3\u5b83\u4f7f\u7528\u4e86\u54ea\u4e2a\u6570\u636e\u7ed3\u6784\u6765\u4fdd\u5b58\u540d\u5b57\u3002\u8fd9\u65e0\u7591\u6781\u5927\u7a0b\u5ea6\u5730\u964d\u4f4e\u4e86\u4f60\u7684\u7a0b\u5e8f\u7684\u590d\u6742\u5ea6\uff0c\u4e5f\u5c31\u51cf\u5c11\u4e86\u72af\u9519\u8bef\u7684\u673a\u4f1a\u3002\u4e0e\u5176\u4ed6\u7684\u597d\u5904\u76f8\u6bd4\uff0c\u8fd9\u662f\u5c01\u88c5\u5e26\u6765\u7684\u6700\u5927\u597d\u5904\u3002 C++ \u6807\u51c6\u5e93\u4e2d\u7684\u6240\u6709\u7c7b\u90fd\u662f\u5c01\u88c5\u8fc7\u7684\u3002\u4f60\u53ef\u4ee5\u60f3\u8c61\u4e00\u4e0b\uff0c\u5982\u679c\u4f60\u9700\u8981\u4e86\u89e3 std::string , std::vector \u6216 std::cout \u7684\u5b9e\u73b0\u624d\u80fd\u4f7f\u7528\u5b83\u4eec\u7684\u8bdd\uff0c\u5c06\u662f\u591a\u4e48\u53ef\u6015\u7684\u4e00\u4ef6\u4e8b\uff01 \u597d\u5904 2 \uff1a\u7c7b\u5c01\u88c5\u53ef\u4ee5\u4fdd\u62a4\u5176\u6570\u636e\u4e0d\u88ab\u6ee5\u7528 \u5168\u5c40\u53d8\u91cf\u662f\u5371\u9669\u7684\uff0c\u56e0\u4e3a\u4f60\u4e0d\u80fd\u4e25\u683c\u63a7\u5236\u8c01\u53ef\u4ee5\u8bbf\u95ee\u5168\u5c40\u53d8\u91cf\uff0c\u6216\u8005\u4ed6\u4eec\u5e94\u8be5\u5982\u4f55\u4f7f\u7528\u5b83\u3002 \u516c\u6709\u6210\u5458 \u4e5f\u6709\u540c\u6837\u7684\u95ee\u9898\uff0c\u53ea\u662f\u7a0b\u5ea6\u7a0d\u8f7b\u800c\u5df2\u3002 \u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b\u3002\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u5f00\u59cb: 1 2 3 4 5 class MyString { char * m_string ; // we'll dynamically allocate our string here int m_length ; // we need to keep track of the string length }; \u8fd9\u4e24\u4e2a\u53d8\u91cf\u5b58\u5728\u5185\u5728\u7684\u8054\u7cfb: m_length \u5e94\u8be5\u603b\u662f\u7b49\u4e8e m_string \u6301\u6709\u7684\u5b57\u7b26\u4e32\u7684\u957f\u5ea6(\u8fd9\u79cd\u8054\u7cfb\u88ab\u79f0\u4e3a\u4e0d\u53d8\u91cf)\u3002\u5982\u679c m_length \u662fpublic \u7684\uff0c\u90a3\u4e48\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u5728\u4e0d\u6539\u53d8 m_string \u7684\u60c5\u51b5\u4e0b\u6539\u53d8\u5b57\u7b26\u4e32\u7684\u957f\u5ea6(\u53cd\u4e4b\u4ea6\u7136)\u3002\u8fd9\u5c06\u4f7f\u7c7b\u5904\u4e8e\u4e0d\u4e00\u81f4\u7684\u72b6\u6001\uff0c\u4ece\u800c\u53ef\u80fd\u5bfc\u81f4\u5404\u79cd\u5947\u602a\u7684\u95ee\u9898\u3002\u901a\u8fc7\u4f7f m_length \u548c m_string \u6210\u4e3a private \uff0c\u7528\u6237\u88ab\u8feb\u4f7f\u7528\u4efb\u4f55\u53ef\u7528\u7684\u516c\u5171\u6210\u5458\u51fd\u6570\u4e0e\u8be5\u7c7b\u8fdb\u884c\u4ea4\u4e92(\u5e76\u4e14\u8fd9\u4e9b\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u786e\u4fdd m_length \u548c m_string \u603b\u662f\u88ab\u9002\u5f53\u8bbe\u7f6e)\u3002 \u6211\u4eec\u8fd8\u53ef\u4ee5\u9632\u6b62\u7528\u6237\u5728\u4f7f\u7528\u6211\u4eec\u7684\u7c7b\u65f6\u72af\u9519\u8bef\u3002\u8003\u8651\u4e00\u4e2a\u5177\u6709\u516c\u5171\u6570\u7ec4\u6210\u5458\u53d8\u91cf\u7684\u7c7b: 1 2 3 4 5 class IntArray { public : int m_array [ 10 ]; }; \u5982\u679c\u7528\u6237\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u6570\u7ec4\uff0c\u4ed6\u4eec\u53ef\u80fd\u4f1a\u4f7f\u7528\u65e0\u6548\u7684\u7d22\u5f15\u4e0b\u6807\u6570\u7ec4\uff0c\u4ece\u800c\u5f15\u53d1\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\uff1a 1 2 3 4 5 int main () { IntArray array ; array . m_array [ 16 ] = 2 ; // invalid array index, now we overwrote memory that we don't own } \u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u5c06\u6570\u7ec4\u8bbe\u4e3a\u79c1\u6709\uff0c\u5219\u53ef\u4ee5\u5f3a\u5236\u8c03\u7528\u4e00\u4e2a\u9a8c\u8bc1\u7d22\u5f15\u662f\u5426\u6709\u6548\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // For std::size() class IntArray { private : int m_array [ 10 ]; // user can not access this directly any more public : void setValue ( int index , int value ) { // If the index is invalid, do nothing if ( index < 0 || index >= std :: size ( m_array )) return ; m_array [ index ] = value ; } }; \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4fdd\u62a4\u7a0b\u5e8f\u7684\u5b8c\u6574\u6027\u3002\u987a\u5e26\u4e00\u63d0\uff0c std::array \u548c std::vector \u7684 at() \u51fd\u6570\u6240\u505a\u7684\u4e8b\u60c5\u548c\u4e0a\u9762\u7684\u51fd\u6570\u975e\u5e38\u7c7b\u4f3c\uff01 \u597d\u5904 3 \uff1a\u5c01\u88c5\u8fc7\u7684\u7c7b\u66f4\u5bb9\u6613\u4fee\u6539 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7b80\u5355\u7684\u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include class Something { public : int m_value1 ; int m_value2 ; int m_value3 ; }; int main () { Something something ; something . m_value1 = 5 ; std :: cout << something . m_value1 << '\\n' ; } \u867d\u7136\u8fd9\u4e2a\u7a0b\u5e8f\u5de5\u4f5c\u5f97\u5f88\u597d\uff0c\u4f46\u5982\u679c\u7a0d\u540e\u6211\u4eec\u51b3\u5b9a\u91cd\u547d\u540d m_value1 \u6216\u6539\u53d8\u5b83\u7684\u7c7b\u578b\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u6211\u4eec\u4e0d\u4ec5\u7834\u574f\u4e86\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u800c\u4e14\u53ef\u80fd\u7834\u574f\u4e86\u5927\u591a\u6570\u4f7f\u7528 Something \u7c7b\u7684\u7a0b\u5e8f\uff01 \u5c01\u88c5\u4f7f\u6211\u4eec\u80fd\u591f\u5728\u4e0d\u7834\u574f\u6240\u6709\u4f7f\u7528\u7c7b\u7684\u7a0b\u5e8f\u7684\u60c5\u51b5\u4e0b\u6539\u53d8\u7c7b\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002 \u4e0b\u9762\u662f\u8fd9\u4e2a\u7c7b\u7684\u5c01\u88c5\u7248\u672c\uff0c\u5b83\u4f7f\u7528\u51fd\u6570\u8bbf\u95ee m_value1 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include class Something { private : int m_value1 ; int m_value2 ; int m_value3 ; public : void setValue1 ( int value ) { m_value1 = value ; } int getValue1 () { return m_value1 ; } }; int main () { Something something ; something . setValue1 ( 5 ); std :: cout << something . getValue1 () << '\\n' ; } \u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u6539\u53d8\u7c7b\u7684\u5b9e\u73b0\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include class Something { private : int m_value [ 3 ]; // note: we changed the implementation of this class! public : // We have to update any member functions to reflect the new implementation void setValue1 ( int value ) { m_value [ 0 ] = value ; } int getValue1 () { return m_value [ 0 ]; } }; int main () { // But our program still works just fine! Something something ; something . setValue1 ( 5 ); std :: cout << something . getValue1 () << '\\n' ; } \u8bf7\u6ce8\u610f\uff0c\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u66f4\u6539\u7c7b\u7684\u516c\u5171\u63a5\u53e3\u4e2d\u7684\u4efb\u4f55\u51fd\u6570\u7b7e\u540d(\u8fd4\u56de\u7c7b\u578b\u3001\u51fd\u6570\u540d\u6216\u5f62\u53c2)\uff0c\u6240\u4ee5\u4f7f\u7528\u8be5\u7c7b\u7684\u7528\u6237\u4e0d\u4f1a\u611f\u77e5\u5230\u4efb\u4f55\u6539\u53d8\u3002 \u540c\u6837\u5730\uff0c\u5982\u679c\u6709\u4eba\u5728\u665a\u4e0a\u6f5c\u5165\u4f60\u7684\u623f\u5b50\uff0c\u7528\u4e0d\u540c\u7684(\u4f46\u517c\u5bb9\u7684)\u6280\u672f\u66ff\u6362\u4e86\u4f60\u7684\u7535\u89c6\u9065\u63a7\u5668\u7684\u5185\u90e8\uff0c\u4f60\u5728\u4f7f\u7528\u65f6\u53ef\u80fd\u5b8c\u5168\u4e0d\u4f1a\u6ce8\u610f\u5230\uff01 \u597d\u5904 4\uff1a\u5c01\u88c5\u7684\u7c7b\u66f4\u5bb9\u6613\u67e5\u627e\u9519\u8bef \u6700\u540e\uff0c\u5c01\u88c5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u51fa\u73b0\u9519\u8bef\u65f6\u8c03\u8bd5\u7a0b\u5e8f\u3002\u901a\u5e38\uff0c\u5f53\u4e00\u4e2a\u7a0b\u5e8f\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u65f6\uff0c\u5176\u539f\u56e0\u53ef\u80fd\u662f\u56e0\u4e3a\u6211\u4eec\u7684\u4e00\u4e2a\u6210\u5458\u53d8\u91cf\u7684\u503c\u4e0d\u6b63\u786e\u3002\u5982\u679c\u6bcf\u4e2a\u4eba\u90fd\u80fd\u591f\u76f4\u63a5\u8bbf\u95ee\u53d8\u91cf\uff0c\u90a3\u4e48\u8ddf\u8e2a\u54ea\u6bb5\u4ee3\u7801\u4fee\u6539\u4e86\u53d8\u91cf\u5c31\u4f1a\u5f88\u56f0\u96be(\u53ef\u80fd\u662f\u7a0b\u5e8f\u4e2d\u7684\u4efb\u4f55\u4e00\u6bb5\u4ee3\u7801\uff0c\u4f60\u9700\u8981\u5c06\u5b83\u4eec\u5168\u90e8\u5206\u89e3\u4ee5\u627e\u51fa\u662f\u54ea\u4e00\u884c)\u3002\u4f46\u662f\uff0c\u5982\u679c\u6bcf\u4e2a\u4eba\u90fd\u5fc5\u987b\u8c03\u7528\u540c\u4e00\u4e2a\u516c\u5171\u51fd\u6570\u6765\u4fee\u6539\u4e00\u4e2a\u503c\uff0c\u90a3\u4e48\u4f60\u5c31\u53ef\u4ee5\u5c06\u8be5\u51fd\u6570\u8bbe\u4e3a\u65ad\u70b9\uff0c\u5e76\u89c2\u5bdf\u6bcf\u4e2a\u8c03\u7528\u8005\u66f4\u6539\u503c\u7684\u8fc7\u7a0b\uff0c\u76f4\u5230\u53d1\u73b0\u54ea\u91cc\u51fa\u4e86\u95ee\u9898\u3002 \u6210\u5458\u8bbf\u95ee\u51fd\u6570 \u5bf9\u4e8e\u67d0\u4e9b\u7c7b\u6765\u8bf4\uff08\u53d6\u51b3\u4e8e\u8fd9\u4e2a\u7c7b\u8981\u505a\u4ec0\u4e48\uff09\uff0c\u6709\u65f6\u5019\u6211\u4eec\u4f1a\u5e0c\u671b\u80fd\u591f\u76f4\u63a5\u8bfb\u53d6\u6216\u8bbe\u7f6e\u67d0\u4e2a \u79c1\u6709\u6210\u5458 \u53d8\u91cf\u7684\u503c\u3002 \u6210\u5458\u8bbf\u95ee\u51fd\u6570 \u662f\u4e00\u7c7b\u7528\u4e8e\u83b7\u53d6\u6216\u6539\u53d8\u7c7b\u79c1\u6709\u6210\u5458\u53d8\u91cf\u503c\u7684\u7cbe\u7b80\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e String \u7c7b\uff0c\u6210\u5458\u51fd\u6570\u770b\u4e0a\u53bb\u53ef\u80fd\u4f1a\u50cf\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 class MyString { private : char * m_string ; // we'll dynamically allocate our string here int m_length ; // we need to keep track of the string length public : int getLength () { return m_length ; } // access function to get value of m_length }; getLength() \u5c31\u662f\u4e0a\u4e00\u4e2a\u6210\u5458\u8bbf\u95ee\u51fd\u6570\uff0c\u5b83\u8fd4\u56de m_length \u7684\u503c\u3002 \u6210\u5458\u8bbf\u95ee\u51fd\u6570\u901a\u5e38\u6709\u4e24\u79cd\uff1a getters \u548c setters \u3002 Getters (\u6709\u65f6\u4e5f\u79f0\u4e3aaccessor) \u7528\u4e8e\u8fd4\u56de\u67d0\u4e2a\u79c1\u6709\u6210\u5458\u53d8\u91cf\u7684\u503c\u3002 Setters (\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a mutators ) \u5219\u7528\u4e8e\u4e3a\u79c1\u6709\u6210\u5458\u53d8\u91cf\u8bbe\u7f6e\u503c\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u7c7b\uff0c\u5b83\u7684\u6240\u6709\u6210\u5458\u90fd\u5177\u6709 getter \u548c setter \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Date { private : int m_month ; int m_day ; int m_year ; public : int getMonth () { return m_month ; } // getter for month void setMonth ( int month ) { m_month = month ; } // setter for month int getDay () { return m_day ; } // getter for day void setDay ( int day ) { m_day = day ; } // setter for day int getYear () { return m_year ; } // getter for year void setYear ( int year ) { m_year = year ; } // setter for year }; Date \u7c7b\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u5c01\u88c5\u8fc7\u7684\u7ed3\u6784\uff0c\u4f7f\u7528\u8005\u6709\u7406\u7531\u8ba4\u4e3a\u5b83\u4eec\u80fd\u591f\u8bfb\u53d6\u548c\u8bbe\u7f6e\u7c7b\u4e2d\u7684\u5e74\u6708\u65e5\u53d8\u91cf\u3002 MyString \u7c7b\u4e0d\u4ec5\u5305\u542b\u6570\u636e\u2014\u2014\u5b83\u6709\u66f4\u590d\u6742\u7684\u529f\u80fd\uff0c\u5e76\u4e14\u6709\u4e00\u4e2a\u9700\u8981\u7ef4\u62a4\u7684 \u4e0d\u53d8\u91cf \u3002\u6211\u4eec\u6ca1\u6709\u4e3a\u53d8\u91cf m_length \u63d0\u4f9b setter\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u5e0c\u671b\u7528\u6237\u80fd\u591f\u76f4\u63a5\u8bbe\u7f6e\u5b57\u7b26\u4e32\u7684(\u957f\u5ea6\u5e94\u8be5\u53ea\u5728\u5b57\u7b26\u4e32\u53d1\u751f\u66f4\u6539\u65f6\u8bbe\u7f6e)\u3002\u5728\u8fd9\u4e2a\u7c7b\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u76f4\u63a5\u83b7\u53d6\u5b57\u7b26\u4e32\u957f\u5ea6\u662f\u6709\u610f\u4e49\u7684\uff0c\u56e0\u6b64\u63d0\u4f9b\u4e86\u83b7\u53d6\u957f\u5ea6\u503c\u7684getter\u3002 getter\u5e94\u8be5\u63d0\u4f9b\u5bf9\u6570\u636e\u7684\u201c\u53ea\u8bfb\u201d\u8bbf\u95ee\u3002\u56e0\u6b64\uff0c\u6700\u4f73\u5b9e\u8df5\u662f\u5b83\u4eec\u5e94\u8be5\u901a\u8fc7\u503c\u6216const\u5f15\u7528(\u800c\u4e0d\u662f\u901a\u8fc7\u975econst\u5f15\u7528)\u8fd4\u56de\u3002\u8fd4\u56de\u975econst\u5f15\u7528\u7684getter\u5c06\u5141\u8bb8\u8c03\u7528\u8005\u4fee\u6539\u88ab\u5f15\u7528\u7684\u5b9e\u9645\u5bf9\u8c61\uff0c\u8fd9\u8fdd\u53cd\u4e86getter\u7684\u53ea\u8bfb\u7279\u6027(\u5e76\u8fdd\u53cd\u4e86\u5c01\u88c5)\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5f53getter\u8fd4\u56de\u4e00\u4e2a\u975econst\u5f15\u7528\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include class Foo { private : int m_value { 4 }; public : int & getValue () { return m_value ; } // returns a non-const reference }; int main () { Foo f ; // f.m_value is initialized to 4 f . getValue () = 5 ; // use the non-const reference to assign value 5 to m_value std :: cout << f . getValue (); // prints 5 return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 5 \u56e0\u4e3a getValue() \u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u975econst\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8be5\u5f15\u7528\u76f4\u63a5\u4fee\u6539\u88ab\u5176\u5f15\u7528\u7684\u5bf9\u8c61\uff08 m_value \uff09\uff01 \u6700\u4f73\u5b9e\u8df5 getter \u5e94\u8be5 \u6309\u503c\u8fd4\u56de \u6216\u8fd4\u56deconst\u5f15\u7528\u3002 \u6210\u5458\u8bbf\u95ee\u51fd\u6570\u7684\u76f8\u5173\u95ee\u9898 \u5728\u54ea\u4e9b\u60c5\u51b5\u4e0b\u5e94\u8be5\u4f7f\u7528\u8bbf\u95ee\u51fd\u6570\uff0c\u54ea\u4e9b\u60c5\u51b5\u4e0b\u5e94\u8be5\u907f\u514d\u8bbf\u95ee\u51fd\u6570\uff0c\u5bf9\u6b64\u6709\u76f8\u5f53\u591a\u7684\u8ba8\u8bba\u3002\u5c3d\u7ba1\u8bbf\u95ee\u51fd\u6570\u4e0d\u8fdd\u53cd\u5c01\u88c5\u539f\u5219\uff0c\u4f46\u4e00\u4e9b\u5f00\u53d1\u4eba\u5458\u8ba4\u4e3a\u4f7f\u7528\u8bbf\u95ee\u51fd\u6570\u4e0d\u662f\u826f\u597d\u7684OOP\u7c7b\u8bbe\u8ba1(\u8fd9\u4e2a\u4e3b\u9898\u53ef\u4ee5\u5199\u4e00\u672c\u4e66\u6765\u8ba8\u8bba)\u3002 \u5f53\u4e0b\uff0c\u6211\u4eec\u5c06\u63a8\u8350\u4e00\u79cd\u5b9e\u7528\u7684\u65b9\u6cd5\u3002\u5728\u521b\u5efa\u7c7b\u65f6\uff0c\u8bf7\u8003\u8651\u4ee5\u4e0b\u51e0\u70b9\uff1a \u5982\u679c\u4e0d\u9700\u8981\u4ece\u7c7b\u7684\u5916\u90e8\u8bbf\u95ee\u6210\u5458\uff0c\u4e0d\u8981\u4e3a\u8be5\u6210\u5458\u63d0\u4f9b\u8bbf\u95ee\u51fd\u6570\uff1b \u5982\u679c\u9700\u8981\u4ece\u5916\u90e8\u8bbf\u95ee\u6210\u5458\uff0c\u8003\u8651\u662f\u5426\u53ef\u4ee5\u901a\u8fc7\u67d0\u79cd\u884c\u4e3a\u6216\u52a8\u4f5c\u6765\u5b8c\u6210\uff08\u4f8b\u5982\uff0c\u4e0e\u5176\u63d0\u4f9b setAlive(bool) setter\uff0c\u4e0d\u5982\u5b9e\u73b0\u4e00\u4e2a kill() \u51fd\u6570\uff09 \u5982\u679c\u53ef\u4ee5\uff0c\u4ec5\u8003\u8651\u63d0\u4f9b\u4e00\u4e2agetter \u5c0f\u7ed3 \u6b63\u5982\u60a8\u6240\u770b\u5230\u7684\uff0c\u5b9e\u73b0\u5c01\u88c5\u5e76\u4e0d\u4f1a\u5f88\u9ebb\u70e6\uff0c\u5176\u5e26\u6765\u7684\u597d\u5904\u5374\u975e\u5e38\u7684\u591a\u3002\u5c01\u88c5\u7684\u4e3b\u8981\u597d\u5904\u662f\u5141\u8bb8\u7528\u6237\u5728\u4e0d\u4e86\u89e3\u7c7b\u7684\u5185\u90e8\u5b9e\u73b0\u7684\u60c5\u51b5\u4e0b\uff0c\u8f7b\u677e\u5730\u4f7f\u7528\u8be5\u7c7b\u3002\u00f7","title":"13.4 - \u6210\u5458\u8bbf\u95ee\u51fd\u6570\u4e0e\u5c01\u88c5"},{"location":"13-4-access-functions-and-encapsulation/#_1","text":"\u4e66\u63a5\u4e0a\u6587\uff0c\u6211\u4eec\u63d0\u5230\uff0c\u7c7b\u6210\u5458\u53d8\u91cf\u901a\u5e38\u662f\u79c1\u6709\u7684\u3002\u6b63\u5728\u5b66\u4e60\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u5f00\u53d1\u4eba\u5458\u901a\u5e38\u5f88\u96be\u7406\u89e3\u8fd9\u4e48\u505a\u7684\u76ee\u7684\u3002\u5176\u5b9e\u6253\u4e2a\u6bd4\u65b9\u4f60\u5c31\u80fd\u591f\u660e\u767d\u4e86\u3002 \u751f\u6d3b\u4e2d\u6211\u4eec\u53ef\u4ee5\u63a5\u89e6\u5230\u8bb8\u591a\u7535\u5b50\u8bbe\u5907\u3002\u4f8b\u5982\u7535\u89c6\u9065\u63a7\u5668\uff0c\u4f60\u53ef\u4ee5\u7528\u5b83\u6765\u6253\u5f00/\u5173\u95ed\u7535\u89c6\u3002\u4f60\u53ef\u4ee5\u5f00\u8f66(\u6216\u8e0f\u677f\u8f66)\u53bb\u4e0a\u73ed\uff0c\u7528\u667a\u80fd\u624b\u673a\u62cd\u7167\u3002\u8fd9\u4e09\u79cd\u770b\u4f3c\u4e0d\u76f8\u5173\u7684\u4e1c\u897f\u5374\u6709\u7740\u4e00\u4e2a\u5171\u540c\u7684\u6a21\u5f0f\uff1a\u5b83\u4eec\u4e3a\u4f60\u63d0\u4f9b\u4e00\u4e2a\u7b80\u5355\u7684\u754c\u9762\u4f7f\u4f60\u53ef\u4ee5(\u4e00\u4e2a\u6309\u94ae\u3001\u4e00\u4e2a\u65b9\u5411\u76d8\u7b49\u7b49\u2026\u2026)\u6267\u884c\u4e00\u4e2a\u52a8\u4f5c\u3002\u7136\u800c\uff0c\u8fd9\u4e9b\u8bbe\u5907\u7684\u5b9e\u9645\u64cd\u4f5c\u65b9\u5f0f\u5374\u4e0d\u4e3a\u4f60\u6240\u77e5\u3002\u5f53\u4f60\u6309\u4e0b\u9065\u63a7\u5668\u4e0a\u7684\u6309\u94ae\u65f6\uff0c\u4f60\u4e0d\u9700\u8981\u77e5\u9053\u5b83\u662f\u5982\u4f55\u4e0e\u7535\u89c6\u901a\u4fe1\u7684\u3002\u5f53\u4f60\u8e29\u6cb9\u95e8\u65f6\uff0c\u4f60\u4e5f\u4e0d\u9700\u8981\u77e5\u9053\u5185\u71c3\u673a\u662f\u5982\u4f55\u8ba9\u8f6e\u5b50\u8f6c\u52a8\u7684\u3002\u5f53\u4f60\u62cd\u7167\u65f6\uff0c\u4f60\u4e0d\u9700\u8981\u77e5\u9053\u4f20\u611f\u5668\u662f\u5982\u4f55\u5c06\u5149\u7ebf\u805a\u96c6\u6210\u50cf\u7d20\u56fe\u50cf\u7684\u3002\u8fd9\u79cd\u63a5\u53e3\u548c\u5b9e\u73b0\u7684\u5206\u79bb\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u4e0d\u4e86\u89e3\u7269\u4f53\u5982\u4f55\u5de5\u4f5c\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u7269\u4f53\u3002\u8fd9\u6781\u5927\u5730\u964d\u4f4e\u4e86\u4f7f\u7528\u8fd9\u4e9b\u5bf9\u8c61\u7684\u590d\u6742\u6027\uff0c\u589e\u52a0\u4e86\u6211\u4eec\u80fd\u591f\u4e0e\u4e4b\u4ea4\u4e92\u7684\u5bf9\u8c61\u6570\u91cf\u3002 \u57fa\u4e8e\u540c\u6837\u7684\u539f\u56e0\uff0c\u5c06\u5b9e\u73b0\u548c\u63a5\u53e3\u5206\u79bb\u5728\u8f6f\u4ef6\u5de5\u7a0b\u4e2d\u4e5f\u662f\u975e\u5e38\u6709\u7528\u7684\u3002","title":"\u4e3a\u4ec0\u4e48\u8981\u5b9a\u4e49\u79c1\u6709\u6210\u5458\u53d8\u91cf"},{"location":"13-4-access-functions-and-encapsulation/#_2","text":"\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c \u5c01\u88c5 \uff08\u6709\u65f6\u4e5f\u79f0\u4e3a\u4fe1\u606f\u5c4f\u853d\uff09\u662f\u6307\u5c06\u5bf9\u8c61\u7684\u5b9e\u73b0\u7ec6\u8282\u5bf9\u7528\u6237\u9690\u85cf\u8d77\u6765\u7684\u8fc7\u7a0b\u3002\u7528\u6237\u5728\u4f7f\u7528\u5bf9\u8c61\u7684\u65f6\u5019\u53ea\u9700\u8981\u901a\u8fc7\u516c\u5f00\u63a5\u53e3\u8bbf\u95ee\u5bf9\u8c61\u5373\u53ef\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u4e0d\u4e86\u89e3\u5bf9\u8c61\u5b9e\u73b0\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u8be5\u5bf9\u8c61\u3002 \u5728 C++ \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u6765\u5b9e\u73b0\u5c01\u88c5\u3002\u901a\u5e38\uff0c\u7c7b\u7684\u6240\u6709\u6210\u5458\u53d8\u91cf\u90fd\u662f\u79c1\u6709\u7684(\u9690\u85cf\u5b9e\u73b0\u7ec6\u8282)\uff0c\u800c\u5927\u591a\u6570\u6210\u5458\u51fd\u6570\u90fd\u662f\u516c\u6709\u7684(\u66b4\u9732\u63a5\u53e3)\u3002\u867d\u7136\u8981\u6c42\u7c7b\u7684\u7528\u6237\u4f7f\u7528\u516c\u5f00\u63a5\u53e3\u4e0e\u7c7b\u4ea4\u4e92\u6bd4\u76f4\u63a5\u63d0\u4f9b\u5bf9\u6210\u5458\u53d8\u91cf\u7684\u516c\u5171\u8bbf\u95ee\u66f4\u7e41\u7410\uff0c\u4f46\u8fd9\u6837\u505a\u5b9e\u9645\u4e0a\u76ca\u5904\u9887\u591a\uff0c\u6709\u52a9\u4e8e\u63d0\u9ad8\u7c7b\u7684\u53ef\u91cd\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002 \u6ce8\u610f\uff1a\u5c01\u88c5\u4e00\u8bcd\u6709\u65f6\u5019\u4e5f\u6307\u4ee3\u5bf9\u6570\u636e\u548c\u51fd\u6570\u7684\u6253\u5305\u3002\u8fd9\u91cc\u6211\u4eec\u53ea\u53d6\u5176\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\u7684\u542b\u4e49\u3002","title":"\u5c01\u88c5"},{"location":"13-4-access-functions-and-encapsulation/#1","text":"\u5bf9\u4e8e\u4e00\u4e2a\u826f\u597d\u5c01\u88c5\u7684\u7c7b\u6765\u8bf4\uff0c\u4f60\u53ea\u9700\u5173\u5fc3\u8be5\u7c7b\u4e3a\u4f60\u63d0\u4f9b\u4e86\u54ea\u4e9b\u516c\u5171\u63a5\u53e3\uff0c\u8fd9\u4e9b\u63a5\u53e3\u9700\u8981\u4ec0\u4e48\u53c2\u6570\uff0c\u5b83\u4eec\u7684\u8fd4\u56de\u503c\u662f\u4ec0\u4e48\u3002\u800c\u8fd9\u4e2a\u7c7b\u7684\u5177\u4f53\u5185\u90e8\u5b9e\u73b0\u5219\u5b8c\u5168\u4e0d\u9700\u8981\u641e\u6e05\u695a\u3002\u4f8b\u5982\uff0c\u7c7b\u4e2d\u4fdd\u5b58\u4e86\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u540d\u5b57\u7684\u5217\u8868\uff0c\u800c\u8fd9\u4e2a\u5217\u8868\u7684\u5b9e\u73b0\u53ef\u4ee5\u662f\u5305\u542bC\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u7684\u52a8\u6001\u6570\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u662f std::array \u3001 std::vector \u3001 std::map \u3001 std::list \u6216\u8005\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u3002\u5728\u4f7f\u7528\u8be5\u7c7b\u7684\u5bf9\u8c61\u65f6\uff0c\u4e0d\u9700\u8981\u4e5f\u4e0d\u5173\u5fc3\u5b83\u4f7f\u7528\u4e86\u54ea\u4e2a\u6570\u636e\u7ed3\u6784\u6765\u4fdd\u5b58\u540d\u5b57\u3002\u8fd9\u65e0\u7591\u6781\u5927\u7a0b\u5ea6\u5730\u964d\u4f4e\u4e86\u4f60\u7684\u7a0b\u5e8f\u7684\u590d\u6742\u5ea6\uff0c\u4e5f\u5c31\u51cf\u5c11\u4e86\u72af\u9519\u8bef\u7684\u673a\u4f1a\u3002\u4e0e\u5176\u4ed6\u7684\u597d\u5904\u76f8\u6bd4\uff0c\u8fd9\u662f\u5c01\u88c5\u5e26\u6765\u7684\u6700\u5927\u597d\u5904\u3002 C++ \u6807\u51c6\u5e93\u4e2d\u7684\u6240\u6709\u7c7b\u90fd\u662f\u5c01\u88c5\u8fc7\u7684\u3002\u4f60\u53ef\u4ee5\u60f3\u8c61\u4e00\u4e0b\uff0c\u5982\u679c\u4f60\u9700\u8981\u4e86\u89e3 std::string , std::vector \u6216 std::cout \u7684\u5b9e\u73b0\u624d\u80fd\u4f7f\u7528\u5b83\u4eec\u7684\u8bdd\uff0c\u5c06\u662f\u591a\u4e48\u53ef\u6015\u7684\u4e00\u4ef6\u4e8b\uff01","title":"\u597d\u5904 1 \uff1a\u7c7b\u5c01\u88c5\u53ef\u4ee5\u63d0\u9ad8\u5176\u6613\u7528\u6027\u5e76\u964d\u4f4e\u7a0b\u5e8f\u590d\u6742\u5ea6"},{"location":"13-4-access-functions-and-encapsulation/#2","text":"\u5168\u5c40\u53d8\u91cf\u662f\u5371\u9669\u7684\uff0c\u56e0\u4e3a\u4f60\u4e0d\u80fd\u4e25\u683c\u63a7\u5236\u8c01\u53ef\u4ee5\u8bbf\u95ee\u5168\u5c40\u53d8\u91cf\uff0c\u6216\u8005\u4ed6\u4eec\u5e94\u8be5\u5982\u4f55\u4f7f\u7528\u5b83\u3002 \u516c\u6709\u6210\u5458 \u4e5f\u6709\u540c\u6837\u7684\u95ee\u9898\uff0c\u53ea\u662f\u7a0b\u5ea6\u7a0d\u8f7b\u800c\u5df2\u3002 \u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b\u3002\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u5f00\u59cb: 1 2 3 4 5 class MyString { char * m_string ; // we'll dynamically allocate our string here int m_length ; // we need to keep track of the string length }; \u8fd9\u4e24\u4e2a\u53d8\u91cf\u5b58\u5728\u5185\u5728\u7684\u8054\u7cfb: m_length \u5e94\u8be5\u603b\u662f\u7b49\u4e8e m_string \u6301\u6709\u7684\u5b57\u7b26\u4e32\u7684\u957f\u5ea6(\u8fd9\u79cd\u8054\u7cfb\u88ab\u79f0\u4e3a\u4e0d\u53d8\u91cf)\u3002\u5982\u679c m_length \u662fpublic \u7684\uff0c\u90a3\u4e48\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u5728\u4e0d\u6539\u53d8 m_string \u7684\u60c5\u51b5\u4e0b\u6539\u53d8\u5b57\u7b26\u4e32\u7684\u957f\u5ea6(\u53cd\u4e4b\u4ea6\u7136)\u3002\u8fd9\u5c06\u4f7f\u7c7b\u5904\u4e8e\u4e0d\u4e00\u81f4\u7684\u72b6\u6001\uff0c\u4ece\u800c\u53ef\u80fd\u5bfc\u81f4\u5404\u79cd\u5947\u602a\u7684\u95ee\u9898\u3002\u901a\u8fc7\u4f7f m_length \u548c m_string \u6210\u4e3a private \uff0c\u7528\u6237\u88ab\u8feb\u4f7f\u7528\u4efb\u4f55\u53ef\u7528\u7684\u516c\u5171\u6210\u5458\u51fd\u6570\u4e0e\u8be5\u7c7b\u8fdb\u884c\u4ea4\u4e92(\u5e76\u4e14\u8fd9\u4e9b\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u786e\u4fdd m_length \u548c m_string \u603b\u662f\u88ab\u9002\u5f53\u8bbe\u7f6e)\u3002 \u6211\u4eec\u8fd8\u53ef\u4ee5\u9632\u6b62\u7528\u6237\u5728\u4f7f\u7528\u6211\u4eec\u7684\u7c7b\u65f6\u72af\u9519\u8bef\u3002\u8003\u8651\u4e00\u4e2a\u5177\u6709\u516c\u5171\u6570\u7ec4\u6210\u5458\u53d8\u91cf\u7684\u7c7b: 1 2 3 4 5 class IntArray { public : int m_array [ 10 ]; }; \u5982\u679c\u7528\u6237\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u6570\u7ec4\uff0c\u4ed6\u4eec\u53ef\u80fd\u4f1a\u4f7f\u7528\u65e0\u6548\u7684\u7d22\u5f15\u4e0b\u6807\u6570\u7ec4\uff0c\u4ece\u800c\u5f15\u53d1\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\uff1a 1 2 3 4 5 int main () { IntArray array ; array . m_array [ 16 ] = 2 ; // invalid array index, now we overwrote memory that we don't own } \u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u5c06\u6570\u7ec4\u8bbe\u4e3a\u79c1\u6709\uff0c\u5219\u53ef\u4ee5\u5f3a\u5236\u8c03\u7528\u4e00\u4e2a\u9a8c\u8bc1\u7d22\u5f15\u662f\u5426\u6709\u6548\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // For std::size() class IntArray { private : int m_array [ 10 ]; // user can not access this directly any more public : void setValue ( int index , int value ) { // If the index is invalid, do nothing if ( index < 0 || index >= std :: size ( m_array )) return ; m_array [ index ] = value ; } }; \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4fdd\u62a4\u7a0b\u5e8f\u7684\u5b8c\u6574\u6027\u3002\u987a\u5e26\u4e00\u63d0\uff0c std::array \u548c std::vector \u7684 at() \u51fd\u6570\u6240\u505a\u7684\u4e8b\u60c5\u548c\u4e0a\u9762\u7684\u51fd\u6570\u975e\u5e38\u7c7b\u4f3c\uff01","title":"\u597d\u5904 2 \uff1a\u7c7b\u5c01\u88c5\u53ef\u4ee5\u4fdd\u62a4\u5176\u6570\u636e\u4e0d\u88ab\u6ee5\u7528"},{"location":"13-4-access-functions-and-encapsulation/#3","text":"\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7b80\u5355\u7684\u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include class Something { public : int m_value1 ; int m_value2 ; int m_value3 ; }; int main () { Something something ; something . m_value1 = 5 ; std :: cout << something . m_value1 << '\\n' ; } \u867d\u7136\u8fd9\u4e2a\u7a0b\u5e8f\u5de5\u4f5c\u5f97\u5f88\u597d\uff0c\u4f46\u5982\u679c\u7a0d\u540e\u6211\u4eec\u51b3\u5b9a\u91cd\u547d\u540d m_value1 \u6216\u6539\u53d8\u5b83\u7684\u7c7b\u578b\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u6211\u4eec\u4e0d\u4ec5\u7834\u574f\u4e86\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u800c\u4e14\u53ef\u80fd\u7834\u574f\u4e86\u5927\u591a\u6570\u4f7f\u7528 Something \u7c7b\u7684\u7a0b\u5e8f\uff01 \u5c01\u88c5\u4f7f\u6211\u4eec\u80fd\u591f\u5728\u4e0d\u7834\u574f\u6240\u6709\u4f7f\u7528\u7c7b\u7684\u7a0b\u5e8f\u7684\u60c5\u51b5\u4e0b\u6539\u53d8\u7c7b\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002 \u4e0b\u9762\u662f\u8fd9\u4e2a\u7c7b\u7684\u5c01\u88c5\u7248\u672c\uff0c\u5b83\u4f7f\u7528\u51fd\u6570\u8bbf\u95ee m_value1 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include class Something { private : int m_value1 ; int m_value2 ; int m_value3 ; public : void setValue1 ( int value ) { m_value1 = value ; } int getValue1 () { return m_value1 ; } }; int main () { Something something ; something . setValue1 ( 5 ); std :: cout << something . getValue1 () << '\\n' ; } \u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u6539\u53d8\u7c7b\u7684\u5b9e\u73b0\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include class Something { private : int m_value [ 3 ]; // note: we changed the implementation of this class! public : // We have to update any member functions to reflect the new implementation void setValue1 ( int value ) { m_value [ 0 ] = value ; } int getValue1 () { return m_value [ 0 ]; } }; int main () { // But our program still works just fine! Something something ; something . setValue1 ( 5 ); std :: cout << something . getValue1 () << '\\n' ; } \u8bf7\u6ce8\u610f\uff0c\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u66f4\u6539\u7c7b\u7684\u516c\u5171\u63a5\u53e3\u4e2d\u7684\u4efb\u4f55\u51fd\u6570\u7b7e\u540d(\u8fd4\u56de\u7c7b\u578b\u3001\u51fd\u6570\u540d\u6216\u5f62\u53c2)\uff0c\u6240\u4ee5\u4f7f\u7528\u8be5\u7c7b\u7684\u7528\u6237\u4e0d\u4f1a\u611f\u77e5\u5230\u4efb\u4f55\u6539\u53d8\u3002 \u540c\u6837\u5730\uff0c\u5982\u679c\u6709\u4eba\u5728\u665a\u4e0a\u6f5c\u5165\u4f60\u7684\u623f\u5b50\uff0c\u7528\u4e0d\u540c\u7684(\u4f46\u517c\u5bb9\u7684)\u6280\u672f\u66ff\u6362\u4e86\u4f60\u7684\u7535\u89c6\u9065\u63a7\u5668\u7684\u5185\u90e8\uff0c\u4f60\u5728\u4f7f\u7528\u65f6\u53ef\u80fd\u5b8c\u5168\u4e0d\u4f1a\u6ce8\u610f\u5230\uff01","title":"\u597d\u5904 3 \uff1a\u5c01\u88c5\u8fc7\u7684\u7c7b\u66f4\u5bb9\u6613\u4fee\u6539"},{"location":"13-4-access-functions-and-encapsulation/#4","text":"\u6700\u540e\uff0c\u5c01\u88c5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u51fa\u73b0\u9519\u8bef\u65f6\u8c03\u8bd5\u7a0b\u5e8f\u3002\u901a\u5e38\uff0c\u5f53\u4e00\u4e2a\u7a0b\u5e8f\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u65f6\uff0c\u5176\u539f\u56e0\u53ef\u80fd\u662f\u56e0\u4e3a\u6211\u4eec\u7684\u4e00\u4e2a\u6210\u5458\u53d8\u91cf\u7684\u503c\u4e0d\u6b63\u786e\u3002\u5982\u679c\u6bcf\u4e2a\u4eba\u90fd\u80fd\u591f\u76f4\u63a5\u8bbf\u95ee\u53d8\u91cf\uff0c\u90a3\u4e48\u8ddf\u8e2a\u54ea\u6bb5\u4ee3\u7801\u4fee\u6539\u4e86\u53d8\u91cf\u5c31\u4f1a\u5f88\u56f0\u96be(\u53ef\u80fd\u662f\u7a0b\u5e8f\u4e2d\u7684\u4efb\u4f55\u4e00\u6bb5\u4ee3\u7801\uff0c\u4f60\u9700\u8981\u5c06\u5b83\u4eec\u5168\u90e8\u5206\u89e3\u4ee5\u627e\u51fa\u662f\u54ea\u4e00\u884c)\u3002\u4f46\u662f\uff0c\u5982\u679c\u6bcf\u4e2a\u4eba\u90fd\u5fc5\u987b\u8c03\u7528\u540c\u4e00\u4e2a\u516c\u5171\u51fd\u6570\u6765\u4fee\u6539\u4e00\u4e2a\u503c\uff0c\u90a3\u4e48\u4f60\u5c31\u53ef\u4ee5\u5c06\u8be5\u51fd\u6570\u8bbe\u4e3a\u65ad\u70b9\uff0c\u5e76\u89c2\u5bdf\u6bcf\u4e2a\u8c03\u7528\u8005\u66f4\u6539\u503c\u7684\u8fc7\u7a0b\uff0c\u76f4\u5230\u53d1\u73b0\u54ea\u91cc\u51fa\u4e86\u95ee\u9898\u3002","title":"\u597d\u5904 4\uff1a\u5c01\u88c5\u7684\u7c7b\u66f4\u5bb9\u6613\u67e5\u627e\u9519\u8bef"},{"location":"13-4-access-functions-and-encapsulation/#_3","text":"\u5bf9\u4e8e\u67d0\u4e9b\u7c7b\u6765\u8bf4\uff08\u53d6\u51b3\u4e8e\u8fd9\u4e2a\u7c7b\u8981\u505a\u4ec0\u4e48\uff09\uff0c\u6709\u65f6\u5019\u6211\u4eec\u4f1a\u5e0c\u671b\u80fd\u591f\u76f4\u63a5\u8bfb\u53d6\u6216\u8bbe\u7f6e\u67d0\u4e2a \u79c1\u6709\u6210\u5458 \u53d8\u91cf\u7684\u503c\u3002 \u6210\u5458\u8bbf\u95ee\u51fd\u6570 \u662f\u4e00\u7c7b\u7528\u4e8e\u83b7\u53d6\u6216\u6539\u53d8\u7c7b\u79c1\u6709\u6210\u5458\u53d8\u91cf\u503c\u7684\u7cbe\u7b80\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e String \u7c7b\uff0c\u6210\u5458\u51fd\u6570\u770b\u4e0a\u53bb\u53ef\u80fd\u4f1a\u50cf\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 class MyString { private : char * m_string ; // we'll dynamically allocate our string here int m_length ; // we need to keep track of the string length public : int getLength () { return m_length ; } // access function to get value of m_length }; getLength() \u5c31\u662f\u4e0a\u4e00\u4e2a\u6210\u5458\u8bbf\u95ee\u51fd\u6570\uff0c\u5b83\u8fd4\u56de m_length \u7684\u503c\u3002 \u6210\u5458\u8bbf\u95ee\u51fd\u6570\u901a\u5e38\u6709\u4e24\u79cd\uff1a getters \u548c setters \u3002 Getters (\u6709\u65f6\u4e5f\u79f0\u4e3aaccessor) \u7528\u4e8e\u8fd4\u56de\u67d0\u4e2a\u79c1\u6709\u6210\u5458\u53d8\u91cf\u7684\u503c\u3002 Setters (\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a mutators ) \u5219\u7528\u4e8e\u4e3a\u79c1\u6709\u6210\u5458\u53d8\u91cf\u8bbe\u7f6e\u503c\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u7c7b\uff0c\u5b83\u7684\u6240\u6709\u6210\u5458\u90fd\u5177\u6709 getter \u548c setter \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Date { private : int m_month ; int m_day ; int m_year ; public : int getMonth () { return m_month ; } // getter for month void setMonth ( int month ) { m_month = month ; } // setter for month int getDay () { return m_day ; } // getter for day void setDay ( int day ) { m_day = day ; } // setter for day int getYear () { return m_year ; } // getter for year void setYear ( int year ) { m_year = year ; } // setter for year }; Date \u7c7b\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u5c01\u88c5\u8fc7\u7684\u7ed3\u6784\uff0c\u4f7f\u7528\u8005\u6709\u7406\u7531\u8ba4\u4e3a\u5b83\u4eec\u80fd\u591f\u8bfb\u53d6\u548c\u8bbe\u7f6e\u7c7b\u4e2d\u7684\u5e74\u6708\u65e5\u53d8\u91cf\u3002 MyString \u7c7b\u4e0d\u4ec5\u5305\u542b\u6570\u636e\u2014\u2014\u5b83\u6709\u66f4\u590d\u6742\u7684\u529f\u80fd\uff0c\u5e76\u4e14\u6709\u4e00\u4e2a\u9700\u8981\u7ef4\u62a4\u7684 \u4e0d\u53d8\u91cf \u3002\u6211\u4eec\u6ca1\u6709\u4e3a\u53d8\u91cf m_length \u63d0\u4f9b setter\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u5e0c\u671b\u7528\u6237\u80fd\u591f\u76f4\u63a5\u8bbe\u7f6e\u5b57\u7b26\u4e32\u7684(\u957f\u5ea6\u5e94\u8be5\u53ea\u5728\u5b57\u7b26\u4e32\u53d1\u751f\u66f4\u6539\u65f6\u8bbe\u7f6e)\u3002\u5728\u8fd9\u4e2a\u7c7b\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u76f4\u63a5\u83b7\u53d6\u5b57\u7b26\u4e32\u957f\u5ea6\u662f\u6709\u610f\u4e49\u7684\uff0c\u56e0\u6b64\u63d0\u4f9b\u4e86\u83b7\u53d6\u957f\u5ea6\u503c\u7684getter\u3002 getter\u5e94\u8be5\u63d0\u4f9b\u5bf9\u6570\u636e\u7684\u201c\u53ea\u8bfb\u201d\u8bbf\u95ee\u3002\u56e0\u6b64\uff0c\u6700\u4f73\u5b9e\u8df5\u662f\u5b83\u4eec\u5e94\u8be5\u901a\u8fc7\u503c\u6216const\u5f15\u7528(\u800c\u4e0d\u662f\u901a\u8fc7\u975econst\u5f15\u7528)\u8fd4\u56de\u3002\u8fd4\u56de\u975econst\u5f15\u7528\u7684getter\u5c06\u5141\u8bb8\u8c03\u7528\u8005\u4fee\u6539\u88ab\u5f15\u7528\u7684\u5b9e\u9645\u5bf9\u8c61\uff0c\u8fd9\u8fdd\u53cd\u4e86getter\u7684\u53ea\u8bfb\u7279\u6027(\u5e76\u8fdd\u53cd\u4e86\u5c01\u88c5)\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5f53getter\u8fd4\u56de\u4e00\u4e2a\u975econst\u5f15\u7528\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include class Foo { private : int m_value { 4 }; public : int & getValue () { return m_value ; } // returns a non-const reference }; int main () { Foo f ; // f.m_value is initialized to 4 f . getValue () = 5 ; // use the non-const reference to assign value 5 to m_value std :: cout << f . getValue (); // prints 5 return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 5 \u56e0\u4e3a getValue() \u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u975econst\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8be5\u5f15\u7528\u76f4\u63a5\u4fee\u6539\u88ab\u5176\u5f15\u7528\u7684\u5bf9\u8c61\uff08 m_value \uff09\uff01 \u6700\u4f73\u5b9e\u8df5 getter \u5e94\u8be5 \u6309\u503c\u8fd4\u56de \u6216\u8fd4\u56deconst\u5f15\u7528\u3002","title":"\u6210\u5458\u8bbf\u95ee\u51fd\u6570"},{"location":"13-4-access-functions-and-encapsulation/#_4","text":"\u5728\u54ea\u4e9b\u60c5\u51b5\u4e0b\u5e94\u8be5\u4f7f\u7528\u8bbf\u95ee\u51fd\u6570\uff0c\u54ea\u4e9b\u60c5\u51b5\u4e0b\u5e94\u8be5\u907f\u514d\u8bbf\u95ee\u51fd\u6570\uff0c\u5bf9\u6b64\u6709\u76f8\u5f53\u591a\u7684\u8ba8\u8bba\u3002\u5c3d\u7ba1\u8bbf\u95ee\u51fd\u6570\u4e0d\u8fdd\u53cd\u5c01\u88c5\u539f\u5219\uff0c\u4f46\u4e00\u4e9b\u5f00\u53d1\u4eba\u5458\u8ba4\u4e3a\u4f7f\u7528\u8bbf\u95ee\u51fd\u6570\u4e0d\u662f\u826f\u597d\u7684OOP\u7c7b\u8bbe\u8ba1(\u8fd9\u4e2a\u4e3b\u9898\u53ef\u4ee5\u5199\u4e00\u672c\u4e66\u6765\u8ba8\u8bba)\u3002 \u5f53\u4e0b\uff0c\u6211\u4eec\u5c06\u63a8\u8350\u4e00\u79cd\u5b9e\u7528\u7684\u65b9\u6cd5\u3002\u5728\u521b\u5efa\u7c7b\u65f6\uff0c\u8bf7\u8003\u8651\u4ee5\u4e0b\u51e0\u70b9\uff1a \u5982\u679c\u4e0d\u9700\u8981\u4ece\u7c7b\u7684\u5916\u90e8\u8bbf\u95ee\u6210\u5458\uff0c\u4e0d\u8981\u4e3a\u8be5\u6210\u5458\u63d0\u4f9b\u8bbf\u95ee\u51fd\u6570\uff1b \u5982\u679c\u9700\u8981\u4ece\u5916\u90e8\u8bbf\u95ee\u6210\u5458\uff0c\u8003\u8651\u662f\u5426\u53ef\u4ee5\u901a\u8fc7\u67d0\u79cd\u884c\u4e3a\u6216\u52a8\u4f5c\u6765\u5b8c\u6210\uff08\u4f8b\u5982\uff0c\u4e0e\u5176\u63d0\u4f9b setAlive(bool) setter\uff0c\u4e0d\u5982\u5b9e\u73b0\u4e00\u4e2a kill() \u51fd\u6570\uff09 \u5982\u679c\u53ef\u4ee5\uff0c\u4ec5\u8003\u8651\u63d0\u4f9b\u4e00\u4e2agetter","title":"\u6210\u5458\u8bbf\u95ee\u51fd\u6570\u7684\u76f8\u5173\u95ee\u9898"},{"location":"13-4-access-functions-and-encapsulation/#_5","text":"\u6b63\u5982\u60a8\u6240\u770b\u5230\u7684\uff0c\u5b9e\u73b0\u5c01\u88c5\u5e76\u4e0d\u4f1a\u5f88\u9ebb\u70e6\uff0c\u5176\u5e26\u6765\u7684\u597d\u5904\u5374\u975e\u5e38\u7684\u591a\u3002\u5c01\u88c5\u7684\u4e3b\u8981\u597d\u5904\u662f\u5141\u8bb8\u7528\u6237\u5728\u4e0d\u4e86\u89e3\u7c7b\u7684\u5185\u90e8\u5b9e\u73b0\u7684\u60c5\u51b5\u4e0b\uff0c\u8f7b\u677e\u5730\u4f7f\u7528\u8be5\u7c7b\u3002\u00f7","title":"\u5c0f\u7ed3"},{"location":"13-5-constructors/","tags":["constructors"],"text":"\u5982\u679c\u4e00\u4e2a\u7c7b\u6216\u8005\u7ed3\u6784\u4f53\u7684\u6210\u5458\u90fd\u662f \u516c\u6709\u6210\u5458 \uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 \u805a\u5408\u521d\u59cb\u5316 \uff0c\u901a\u8fc7 \u5217\u8868\u521d\u59cb\u5316 \u5bf9\u7c7b\u8fdb\u884c\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 class Foo { public : int m_x {}; int m_y {}; }; int main () { Foo foo { 6 , 7 }; // list-initialization return 0 ; } \u4f46\u662f\uff0c\u4e00\u65e6\u5c06\u5176\u4e2d\u7684\u4efb\u4f55\u6210\u5458\u53d8\u91cf\u8bbe\u4e3a \u79c1\u6709\u6210\u5458 \uff0c\u5c31\u4e0d\u80fd\u518d\u80fd\u591f\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u521d\u59cb\u5316\u7c7b\u4e86\u3002\u9053\u7406\u5f88\u7b80\u5355\uff1a\u5982\u679c\u4f60\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u4e00\u4e2a\u53d8\u91cf(\u56e0\u4e3a\u5b83\u662f\u79c1\u6709\u7684)\uff0c\u4f60\u5c31\u4e0d\u5e94\u88ab\u5141\u8bb8\u76f4\u63a5\u521d\u59cb\u5316\u5b83\u3002 \u90a3\u4e48\uff0c\u5e94\u8be5\u5982\u4f55\u521d\u59cb\u5316\u5305\u542b\u79c1\u6709\u6210\u5458\u7684\u7c7b\u5462\uff1f\u7b54\u6848\u662f\u901a\u8fc7\u6784\u9020\u51fd\u6570\u3002 \u6784\u9020\u51fd\u6570 \u6784\u9020\u51fd\u6570 \u662f\u4e00\u7c7b\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u4f1a\u5728\u5bf9\u8c61\u521b\u5efa\u65f6\u88ab\u81ea\u52a8\u8c03\u7528\u3002\u6784\u9020\u51fd\u6570\u901a\u5e38\u7528\u4e8e\u4f7f\u7528\u7528\u6237\u63d0\u4f9b\u503c\u6765\u521d\u59cb\u5316\u7c7b\u7684\u6210\u5458\u53d8\u91cf\uff0c\u6216\u8005\u7528\u4e8e\u4e3a\u5bf9\u8c61\u7684\u4f7f\u7528\u505a\u597d\u76f8\u5e94\u7684\u51c6\u5907\u5de5\u4f5c\uff08\u4f8b\u5982\uff1a\u6253\u5f00\u6587\u4ef6\u6216\u6570\u636e\u5e93\uff09\u3002 \u6784\u9020\u51fd\u6570\u6267\u884c\u540e\uff0c\u5bf9\u8c61\u5e94\u8be5\u5904\u4e8e\u4e00\u79cd\u88ab\u826f\u597d\u5b9a\u4e49\u3001\u53ef\u4ee5\u4f7f\u7528\u7684\u72b6\u6001\u3002 \u548c\u666e\u901a\u7684\u6210\u5458\u51fd\u6570\u4e0d\u540c\uff0c\u6784\u9020\u51fd\u6570\u7684\u547d\u540d\u5fc5\u987b\u9075\u5faa\u4ee5\u4e0b\u539f\u5219\uff1a \u6784\u9020\u51fd\u6570\u7684\u540d\u5b57\u5fc5\u987b\u548c\u7c7b\u540d\u5b8c\u5168\u4e00\u81f4\uff08\u5305\u62ec\u5927\u5c0f\u5199\uff09\uff1b \u6784\u9020\u51fd\u6570\u4e0d\u8fd4\u56de\u4efb\u4f55\u7c7b\u578b\uff08\u751a\u81f3\u4e0d\u8fd4\u56de void \uff09 \u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u548c\u9ed8\u8ba4\u521d\u59cb\u5316 \u4e0d\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\uff08\u6216\u6240\u6709\u53c2\u6570\u90fd\u6709\u9ed8\u8ba4\u503c\uff09\u7684\u6784\u9020\u51fd\u6570\u79f0\u4e3a \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 \u3002\u5f53\u7528\u6237\u521d\u59cb\u5316\u5bf9\u8c61\u65f6\u4e0d\u63d0\u4f9b\u4efb\u4f55\u53c2\u6570\u65f6\uff0c\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u5c31\u4f1a\u88ab\u8c03\u7528\u3002 \u4e0b\u9762\u4f8b\u5b50\u4e2d\u7684\u7c7b\u5177\u6709\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : Fraction () // default constructor { m_numerator = 0 ; m_denominator = 1 ; } int getNumerator () { return m_numerator ; } int getDenominator () { return m_denominator ; } double getValue () { return static_cast < double > ( m_numerator ) / m_denominator ; } }; int main () { Fraction frac {}; // calls Fraction() default constructor std :: cout << frac . getNumerator () << '/' << frac . getDenominator () << '\\n' ; return 0 ; } Fraction \u7c7b\u7528\u4e8e\u8868\u793a\u4e00\u4e2a\u5206\u6570\uff08\u901a\u8fc7\u5206\u5b50\u548c\u5206\u6bcd\uff09\uff0c\u540c\u65f6\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a Fraction \u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff08\u4e0e\u51fd\u6570\u540c\u540d\uff09\u3002 \u5f53 Fraction frac{}; \u6267\u884c\u65f6\uff0c\u7f16\u8bd1\u5668\u5f97\u77e5\u6211\u4eec\u60f3\u8981\u521d\u59cb\u5316\u4e00\u4e2a Fraction \u7c7b\u578b\u7684\u5bf9\u8c61\uff08\u4e0d\u63d0\u4f9b\u4efb\u4f55\u53c2\u6570\uff09\u3002\u7136\u540e\uff0c\u5bf9\u8c61 frac \u7684\u521d\u59cb\u5316\u5c31\u5f00\u59cb\u4e86\uff0c\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u4e5f\u4f1a\u5728\u6b64\u65f6\u88ab\u8c03\u7528\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a\u9ed8\u8ba4\u521d\u59cb\u5316\uff08\u5c3d\u7ba1\u4e0d\u4e25\u8c28\uff09\u3002\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u8fd0\u884c\u65f6\u548c\u666e\u901a\u51fd\u6570\u4e00\u6837\uff08\u6b64\u5904\u5373\u5c060\u8d4b\u503c\u7ed9 m_numerator \uff0c\u5c06 m_denominator \u8d4b\u503c\u4e3a1 \uff09 \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 0/1 \u503c\u521d\u59cb\u5316 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 \u503c\u521d\u59cb\u5316 \u7684\u65b9\u5f0f\u521d\u59cb\u5316\u5bf9\u8c61\uff1a 1 Fraction frac {}; // Value initialization using empty set of braces \u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528 \u9ed8\u8ba4\u521d\u59cb\u5316 \u6765\u5b9e\u73b0\uff1a 1 Fraction frac ; // Default-initialization, calls default constructor \u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\u548c\u503c\u521d\u59cb\u5316\u6765\u521d\u59cb\u5316\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5176\u7ed3\u679c\u662f\u4e00\u81f4\u7684\uff1a\u5373\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u88ab\u8c03\u7528\u3002 \u6bd4\u8d77\u503c\u521d\u59cb\u5316\uff0c\u5f88\u591a\u7a0b\u5e8f\u5458\u66f4\u503e\u5411\u4e8e\u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\u3002\u8fd9\u662f\u56e0\u4e3a\u5728\u4f7f\u7528\u503c\u521d\u59cb\u5316\u65f6\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u9996\u5148\u5c06\u7c7b\u6210\u5458\u521d\u59cb\u5316\u4e3a0\uff0c\u7136\u540e\u624d\u8c03\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u4ece\u6548\u7387\u7684\u89d2\u5ea6\u6765\u8bb2\u66f4\u4f4e\u6548\u4e00\u4e9b\u3002 \u4e0d\u8fc7\uff0c\u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u4e5f\u6709\u4e0d\u597d\u7684\u5730\u65b9\uff1a\u4f60\u5fc5\u987b\u77e5\u9053\u7c7b\u662f\u5426\u5bf9\u4f1a\u521d\u59cb\u5316\u81ea\u8eab\u3002\u4f8b\u5982\uff0c\u8be5\u7c7b\u7684\u6240\u6709\u6210\u5458\u662f\u4e0d\u662f\u90fd\u5177\u6709\u521d\u59cb\u5316\u503c\uff0c\u6216\u8005\u662f\u5426\u5b58\u5728\u80fd\u591f\u5bf9\u6240\u6709\u6210\u5458\u8fdb\u884c\u521d\u59cb\u5316\u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002 \u5982\u679c\u4f60\u5df2\u7ecf\u77e5\u9053\u6709\u4e2a\u522b\u53d8\u91cf\u4e0d\u5177\u6709\u521d\u59cb\u5316\u503c\uff0c\u90a3\u4f60\u5c31\u8981\u614e\u91cd\u8003\u8651\uff0c\u6b64\u65f6\u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\u662f\u5426\u4f1a\u5e26\u6765\u95ee\u9898\u3002 \u4f8b\u5982\uff0c\u4e0b\u9762\u4ee3\u7801\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include class Fraction { private : // Removed initializers int m_numerator ; int m_denominator ; public : // Removed default-constructor int getNumerator () { return m_numerator ; } int getDenominator () { return m_denominator ; } double getValue () { return static_cast < double > ( m_numerator ) / m_denominator ; } }; int main () { Fraction frac ; // frac is uninitialized, accessing its members causes undefined behavior std :: cout << frac . getNumerator () << '/' << frac . getDenominator () << '\\n' ; return 0 ; } \u867d\u7136\u8fd9\u4e2a\u7c7b\u7684\u786e\u521d\u59cb\u5316\u4e86\u7c7b\u4e2d\u7684\u6240\u6709\u6210\u5458\uff0c\u4f46\u8981\u786e\u4fdd\u9879\u76ee\u4ee3\u7801\u4e2d\u7684\u6240\u6709\u7c7b\u90fd\u8fd9\u6837\u505a\u4e86\u662f\u5f88\u56f0\u96be\u7684\u3002 \u4f18\u5148\u5bf9\u7c7b\u5bf9\u8c61\u8fdb\u884c\u503c\u521d\u59cb\u5316\u662f\u7b80\u5355\u3001\u4e00\u81f4\u7684\uff0c\u5e76\u4e14\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6355\u83b7\u9519\u8bef\uff0c\u7279\u522b\u662f\u5728\u5b66\u4e60\u65f6\u3002 \u6700\u4f73\u5b9e\u8df5 \u521d\u59cb\u5316\u5bf9\u8c61\u65f6\uff0c\u63a8\u8350\u4f7f\u7528\u503c\u521d\u59cb\u5316\u800c\u975e\u9ed8\u8ba4\u521d\u59cb\u5316 \u4f7f\u7528\u5e26\u53c2\u6570\u7684\u51fd\u6570\u8fdb\u884c\u76f4\u63a5\u521d\u59cb\u5316\u548c\u5217\u8868\u521d\u59cb\u5316 \u5c3d\u7ba1\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u5e2e\u6211\u4eec\u786e\u4fdd\u7c7b\u6210\u5458\u5177\u6709\u5408\u7406\u7684\u521d\u503c\uff0c\u4f46\u662f\u66f4\u591a\u65f6\u5019\u6211\u4eec\u8fd8\u662f\u4f1a\u9700\u8981\u5728 \u5b9e\u4f8b\u5316 \u65f6\u6307\u5b9a\u4e00\u4e9b\u7279\u6b8a\u7684\u503c\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u6784\u9020\u51fd\u6570\u662f\u53ef\u4ee5\u88ab\u58f0\u660e\u4e3a\u5141\u8bb8\u63a5\u53d7\u53c2\u6570\u7684\u5f62\u5f0f\u7684\u3002\u4e0b\u9762\u4f8b\u5b50\u4e2d\u7684\u6784\u9020\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u6574\u578b\u53c2\u6570\u5206\u522b\u7528\u4e8e\u5bf9\u5206\u5b50\u5206\u6bcd\u8fdb\u884c\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : Fraction () // default constructor { m_numerator = 0 ; m_denominator = 1 ; } // Constructor with two parameters, one parameter having a default value Fraction ( int numerator , int denominator = 1 ) { assert ( denominator != 0 ); m_numerator = numerator ; m_denominator = denominator ; } int getNumerator () { return m_numerator ; } int getDenominator () { return m_denominator ; } double getValue () { return static_cast < double > ( m_numerator ) / m_denominator ; } }; \u6ce8\u610f\uff0c\u6211\u4eec\u73b0\u5728\u6709\u4e24\u4e2a\u6784\u9020\u51fd\u6570\uff1a\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff08\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u8c03\u7528\uff09\uff0c\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u5f62\u53c2\u3002C++\u652f\u6301 \u91cd\u8f7d \uff0c\u6240\u4ee5\u8fd9\u4e24\u4e2a\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u5728\u548c\u5e73\u5171\u5904\u3002\u4e8b\u5b9e\u4e0a\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5b9a\u4e49\u4efb\u610f\u6570\u91cf\u7684\u6784\u9020\u51fd\u6570\uff0c\u53ea\u8981\u6bcf\u4e2a\u6784\u9020\u51fd\u6570\u5177\u6709\u60df\u4e00\u7684\u7b7e\u540d(\u5f62\u53c2\u7684\u6570\u91cf\u548c\u7c7b\u578b)\u5373\u53ef\u3002 \u90a3\u4e48\uff0c\u6211\u4eec\u5e94\u8be5\u5982\u4f55\u8c03\u7528\u8fd9\u4e2a\u5177\u6709\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u5462\uff1f\u5f88\u7b80\u5355\uff0c\u4f7f\u7528 \u5217\u8868\u521d\u59cb\u5316 \u5373\u53ef\uff1a 1 2 Fraction fiveThirds { 5 , 3 }; // \u5217\u8868\u521d\u59cb\u5316\uff0c\u8c03\u7528 Fraction(int, int) Fraction threeQuarters ( 3 , 4 ); // \u76f4\u63a5\u521d\u59cb\u5316\uff0c\u4e5f\u662f\u8c03\u7528 Fraction(int, int) \u4e00\u5982\u65e2\u5f80\u5730\uff0c \u6211\u4eec\u66f4\u63a8\u8350\u5217\u8868\u521d\u59cb\u5316\u3002\u5728\u540e\u9762\u7684\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u4e5f\u4f1a\u9047\u5230\u9700\u8981\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\u7684\u60c5\u51b5(\u6a21\u677f\u548c std::initializer_list )\u3002\u8fd8\u6709\u53e6\u5916\u4e00\u79cd\u7279\u6b8a\u7684\u6784\u9020\u51fd\u6570\u4f1a\u4f7f\u7528\u62ec\u53f7\u521d\u59cb\u5316\uff08\u5217\u8868\u521d\u59cb\u5316\uff09\u505a\u4e00\u4e9b\u4e0d\u540c\u7684\u4e8b\u60c5\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\u3002 \u6ce8\u610f\uff0c\u7531\u4e8e\u6784\u9020\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a \u5f62\u53c2 \u5177\u6709\u9ed8\u8ba4\u503c\uff0c\u6240\u4ee5\u4e0b\u9762\u7684\u8c03\u7528\u65b9\u5f0f\u4e5f\u662f\u5408\u6cd5\u7684: 1 Fraction six { 6 }; // \u8c03\u7528 Fraction(int, int) \u6784\u9020\u51fd\u6570\uff0c\u7b2c\u4e8c\u4e2a\u5f62\u53c2\u4f1a\u4f7f\u7528\u9ed8\u8ba4\u503c1\u3002 \u6784\u9020\u51fd\u6570\u7684\u9ed8\u8ba4\u53c2\u6570\u548c\u666e\u901a\u51fd\u6570\u7684\u9ed8\u8ba4\u53c2\u6570\u6ca1\u6709\u4ec0\u4e48\u533a\u522b\uff0c\u6240\u4ee5\u5f53\u6211\u4eec\u8c03\u7528 six{ 6 } \u65f6\uff0c Fraction(int, int) \u51fd\u6570\u4f1a\u81ea\u52a8\u4f7f\u75281\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u5f62\u53c2\u7684\u503c\u3002 \u6700\u4f73\u5b9e\u8df5 \u63a8\u8350\u4f7f\u7528\u5927\u62ec\u53f7\u521d\u59cb\u5316\u6765\u521d\u59cb\u7c7b\u5bf9\u8c61\u3002 \u4f7f\u7528\u7b49\u4e8e\u53f7\u8fdb\u884c\u62f7\u8d1d\u521d\u59cb\u5316 \u5c31\u50cf\u57fa\u672c\u53d8\u91cf\u4e00\u6837\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 \u62f7\u8d1d\u521d\u59cb\u5316 \u6765\u521d\u59cb\u5316\u7c7b\uff1a 1 2 Fraction six = Fraction { 6 }; // Copy initialize a Fraction, will call Fraction(6, 1) Fraction seven = 7 ; // Copy initialize a Fraction. The compiler will try to find a way to convert 7 to a Fraction, which will invoke the Fraction(7, 1) constructor. \u4e0d\u8fc7\uff0c\u6700\u597d\u4e0d\u8981\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u6765\u521d\u59cb\u5316\u7c7b\uff0c\u5b83\u53ef\u80fd\u662f\u4f4e\u6548\u7684\u3002\u5c3d\u7ba1 \u76f4\u63a5\u521d\u59cb\u5316 \u3001 \u5217\u8868\u521d\u59cb\u5316 \u548c \u62f7\u8d1d\u521d\u59cb\u5316 \u5728\u521d\u59cb\u5316\u57fa\u672c\u6570\u636e\u7c7b\u578b\u65f6\u7684\u884c\u4e3a\u662f\u4e00\u6837\u7684\uff0c\u4f46\u662f\u62f7\u8d1d\u521d\u59cb\u5316\u5728\u914d\u5408\u7c7b\u7c7b\u578b\u4f7f\u7528\u65f6\u5176\u884c\u4e3a\u662f\u4e0d\u540c\u7684\uff08\u5c3d\u7ba1\u7ed3\u679c\u76f8\u540c\uff09\u3002\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u7ae0\u8282\u4e2d\u8fdb\u884c\u66f4\u4e3a\u8be6\u7ec6\u7684\u4ecb\u7ecd\u3002 \u51cf\u5c11\u6784\u9020\u51fd\u6570\u7684\u6570\u91cf \u50cf\u4e0a\u9762\u90a3\u6837\u4e3a Fraction \u58f0\u660e\u4e24\u4e2a\u6784\u9020\u51fd\u6570\u662f\u591a\u4f59\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) { assert ( denominator != 0 ); m_numerator = numerator ; m_denominator = denominator ; } int getNumerator () { return m_numerator ; } int getDenominator () { return m_denominator ; } double getValue () { return static_cast < double > ( m_numerator ) / m_denominator ; } }; \u5c3d\u7ba1\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u4ecd\u7136\u662f\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u4f46\u662f\u5b83\u73b0\u5728\u4e5f\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u57fa\u4e8e\u7528\u6237\u63d0\u4f9b\u7684\u53c2\u6570\u8fdb\u884c\u521d\u59cb\u5316\u3002 1 2 3 4 Fraction zero ; // will call Fraction(0, 1) Fraction zero {}; // will call Fraction(0, 1) Fraction six { 6 }; // will call Fraction(6, 1) Fraction fiveThirds { 5 , 3 }; // will call Fraction(5, 3) \u5728\u5b9e\u73b0\u6784\u9020\u51fd\u6570\u65f6\uff0c\u8981\u8003\u8651\u5982\u4f55\u901a\u8fc7\u9ed8\u8ba4\u503c\u6765\u51cf\u5c11\u6784\u9020\u51fd\u6570\u7684\u6570\u91cf\u3002 \u5173\u4e8e\u9ed8\u8ba4\u53c2\u6570\u7684\u6ce8\u610f\u4e8b\u9879 \u5b9a\u4e49\u548c\u8c03\u7528\u5177\u6709\u9ed8\u8ba4\u53c2\u6570\u51fd\u6570\u7684\u89c4\u5219 (\u53c2\u89c1\uff1a 8.12 - \u9ed8\u8ba4\u53c2\u6570 ) \u540c\u6837\u4e5f\u9002\u7528\u4e8e\u6784\u9020\u51fd\u6570\u3002\u7b80\u5355\u590d\u4e60\u4e00\u4e0b\uff0c\u5728\u5b9a\u4e49\u5177\u6709\u9ed8\u8ba4\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u65f6\uff0c\u9ed8\u8ba4\u53c2\u6570\u5fc5\u987b\u5728\u975e\u9ed8\u8ba4\u53c2\u6570\u7684\u540e\u9762\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u975e\u9ed8\u8ba4\u53c2\u6570\u4e0d\u80fd\u88ab\u5b9a\u4e49\u5728\u9ed8\u8ba4\u53c2\u6570\u540e\u3002 \u5bf9\u4e8e\u5177\u6709\u591a\u4e2a\u9ed8\u8ba4\u53c2\u6570\uff0c\u4e14\u53c2\u6570\u7c7b\u578b\u4e0d\u540c\u7684\u6784\u9020\u51fd\u6570\uff0c\u4f7f\u7528\u4e0d\u5f53\u4f1a\u5e26\u6765\u51fa\u4e4e\u610f\u6599\u7684\u9519\u8bef\u3002\u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Something { public : // Default constructor Something ( int n = 0 , double d = 1.2 ) // allows us to construct a Something(int, double), Something(int), or Something() { } }; int main () { Something s1 { 1 , 2.4 }; // calls Something(int, double) Something s2 { 1 }; // calls Something(int, double) Something s3 {}; // calls Something(int, double) Something s4 { 2.4 }; // \u65e0\u6cd5\u7f16\u8bd1\uff0c\u56e0\u4e3a\u6ca1\u6709\u80fd\u5339\u914d\u7684\u6784\u9020\u51fd\u6570 Something(double) return 0 ; } \u5bf9\u4e8e s4 \uff0c\u6211\u4eec\u5e0c\u671b\u4f7f\u7528\u4e00\u4e2a double \u7c7b\u578b\u7684\u53c2\u6570\u6784\u9020 Something \u3002\u4e0a\u9762\u7a0b\u5e8f\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\uff0c\u56e0\u4e3a\u89c4\u5219\u4e0d\u5141\u8bb8\u6211\u4eec\u8df3\u8fc7\u9ed8\u8ba4\u53c2\u6570\u524d\u9762\u7684\u9ed8\u8ba4\u53c2\u6570\u4e3a\u540e\u9762\u7684\u9ed8\u8ba4\u53c2\u6570\u63d0\u4f9b\u5177\u4f53\u503c\uff08\u8fd9\u91cc\u5373\u6700\u5de6\u8fb9\u7684 int \u7c7b\u578b\u7684 \u5f62\u53c2 \u4e0d\u80fd\u88ab\u8df3\u8fc7\uff09\u3002 \u5982\u679c\u60f3\u57fa\u4e8e\u4e00\u4e2a double \u7c7b\u578b\u7684\u53d8\u91cf\u6784\u9020 Something \uff0c\u5219\u5fc5\u987b\u518d\u5b9a\u4e49\u4e00\u4e2a\uff08\u975e\u9ed8\u8ba4\uff09\u6784\u9020\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class Something { public : // Default constructor Something ( int n = 0 , double d = 1.2 ) // allows us to construct a Something(int, double), Something(int), or Something() { } Something ( double d ) { } }; int main () { Something s1 { 1 , 2.4 }; // calls Something(int, double) Something s2 { 1 }; // calls Something(int, double) Something s3 {}; // calls Something(int, double) Something s4 { 2.4 }; // calls Something(double) return 0 ; } \u9690\u5f0f\u751f\u6210\u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570 \u5982\u679c\u4f60\u7684\u7c7b\u6ca1\u6709\u5b9a\u4e49\u6784\u9020\u51fd\u6570\uff0c\u5219C++\u53ef\u4ee5\u81ea\u52a8\u4e3a\u4f60\u751f\u6210\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u88ab\u79f0\u4e3a \u9690\u5f0f\u6784\u9020\u51fd\u6570 \u3002 \u8003\u8651\u4e0b\u9762\u7684\u7c7b\u5b9a\u4e49\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Date { private : int m_year { 1900 }; int m_month { 1 }; int m_day { 1 }; // \u7528\u6237\u6ca1\u6709\u63d0\u4f9b\u6784\u9020\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u81ea\u52a8\u751f\u6210\u9ed8\u8ba4\u6784\u9020\u51fd\u6570 }; int main () { Date date {}; return 0 ; } Date \u7c7b\u6ca1\u6709\u6784\u9020\u51fd\u6570\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u4f1a\u4e3a\u5176\u521b\u5efa\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u8be5\u9690\u5f0f\u6784\u9020\u51fd\u6570\uff0c\u5728\u4e0d\u63d0\u4f9b\u53c2\u6570\u7684\u60c5\u51b5\u4e0b\u521b\u5efa Date \u5bf9\u8c61\u3002 \u5f53\u9690\u5f0f\u6784\u9020\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u5177\u6709\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u503c\u7684\u6210\u5458\u4f1a\u88ab\u521d\u59cb\u5316(\u53c2\u8003\uff1a 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 \u548c 13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u5217\u8868 )\u3002 \u53ea\u8981\u4f60\u7684\u7c7b\u4e2d\u6709\u6784\u9020\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u5c31\u4e0d\u4f1a\u4e3a\u4f60\u521b\u5efa\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 class Date { private : int m_year { 1900 }; int m_month { 1 }; int m_day { 1 }; public : Date ( int year , int month , int day ) // normal non-default constructor { m_year = year ; m_month = month ; m_day = day ; } // No implicit constructor provided because we already defined our own constructor }; int main () { Date date {}; // error: Can't instantiate object because default constructor doesn't exist and the compiler won't generate one Date today { 2020 , 1 , 19 }; // today is initialized to Jan 19th, 2020 return 0 ; } \u5982\u679c\u4f60\u7684\u7c7b\u5177\u6709\u5176\u4ed6\u6784\u9020\u51fd\u6570\uff0c\u4f46\u662f\u4f60\u9700\u8981\u7c7b\u652f\u6301\u9ed8\u8ba4\u521d\u59cb\u5316\uff0c\u5219\u53ef\u4ee5\u4e3a\u6784\u9020\u51fd\u6570\u7684\u6bcf\u4e2a\u5f62\u53c2\u6dfb\u52a0\u9ed8\u8ba4\u503c\uff0c\u6216\u8005\u663e\u5f0f\u5730\u5b9a\u4e49\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002 \u5f53\u7136\uff0c\u8fd8\u6709\u7b2c\u4e09\u79cd\u65b9\u5f0f\uff1a\u4f60\u53ef\u4ee5\u4f7f\u7528 default \u5173\u952e\u5b57\u544a\u77e5\u7f16\u8bd1\u5668\u751f\u6210\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 class Date { private : int m_year { 1900 }; int m_month { 1 }; int m_day { 1 }; public : // \u8981\u6c42\u7f16\u8bd1\u5668\u751f\u6210\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u5373\u4f7f // \u7528\u6237\u5df2\u7ecf\u63d0\u4f9b\u4e86\u6784\u9020\u51fd\u6570 Date () = default ; Date ( int year , int month , int day ) // \u666e\u901a\u975e\u9ed8\u8ba4\u6784\u9020\u51fd\u6570 { m_year = year ; m_month = month ; m_day = day ; } }; int main () { Date date {}; // date is initialized to Jan 1st, 1900 Date today { 2020 , 10 , 14 }; // today is initialized to Oct 14th, 2020 return 0 ; } \u5199 default \u6bd4\u5199\u4e00\u4e2a\u7a7a\u7684\u51fd\u6570\u4f53\u66f4\u8d39\u4e8b\uff0c\u4f46\u662f\u5b83\u80fd\u591f\u66f4\u597d\u5730\u8868\u660e\u4f60\u7684\u610f\u56fe\uff08\u521b\u5efa\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff09\uff0c\u800c\u4e14\u4e5f\u66f4\u5b89\u5168\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5bf9\u6ca1\u6709\u521d\u59cb\u5316\u503c\u7684\u51fd\u6570\u8fdb\u884c0\u521d\u59cb\u5316\u3002 default \u5bf9\u4e8e\u5176\u4ed6\u7279\u6b8a\u6784\u9020\u51fd\u6570\u4e5f\u6709\u6548\uff0c\u7b49\u6211\u4eec\u9047\u5230\u65f6\u518d\u8ba8\u8bba\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u7c7b\u5df2\u7ecf\u62e5\u6709\u6784\u9020\u51fd\u6570\uff0c\u4f46\u4f60\u4ecd\u7136\u9700\u8981\u4e00\u4e2a\u4ec0\u4e48\u90fd\u4e0d\u505a\u7684\u6784\u9020\u51fd\u6570(\u4f8b\u5982\uff0c\u6240\u6709\u6210\u5458\u51fd\u6570\u90fd\u901a\u8fc7\u975e\u9759\u6001\u6210\u5458\u51fd\u6570\u521d\u59cb\u5316\u8fdb\u884c\u4e86\u521d\u59cb\u5316)\uff0c\u53ef\u4ee5\u4f7f\u7528 = default\u3002 \u5305\u542b\u7c7b\u7c7b\u578b\u6210\u5458\u7684\u7c7b class \u4e2d\u7684\u6210\u5458\u53d8\u91cf\uff0c\u4e5f\u53ef\u4ee5\u662f\u5176\u4ed6\u7c7b\u5bf9\u8c61\u3002 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5f53\u5916\u5c42\u5bf9\u8c61\u88ab\u521d\u59cb\u5316\u65f6\uff0c\u5185\u90e8\u7684\u8fd9\u4e9b\u5bf9\u8c61\u5176\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u4e5f\u4f1a\u88ab\u8c03\u7528\uff0c\u800c\u4e14\u4f1a\u5728\u6267\u884c\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u4e4b\u524d\u53d1\u751f\u3002 \u4e3e\u4f8b\u6765\u8bf4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include class A { public : A () { std :: cout << \"A \\n \" ; } }; class B { private : A m_a ; // A \u662f B \u7684\u4e00\u4e2a\u6210\u5458 public : B () { std :: cout << \"B \\n \" ; } }; int main () { B b ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 A B \u5f53\u53d8\u91cf b \u88ab\u6784\u5efa\u65f6\uff0c\u6784\u9020\u51fd\u6570 B() \u4f1a\u88ab\u8c03\u7528\uff0c\u5728 B() \u7684\u51fd\u6570\u4f53\u6267\u884c\u524d\uff0c m_a \u5c31\u5f00\u59cb\u521d\u59cb\u5316\u4e86\uff0c\u6b64\u65f6\u4f1a\u8c03\u7528 A \u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u5e76\u6253\u5370 \"A\"\u3002\u7136\u540e\u63a7\u5236\u6743\u8fd4\u8fd8\u7ed9\u6784\u9020\u51fd\u6570 B() \u5e76\u6267\u884c\u5176\u51fd\u6570\u4f53\u3002 \u8fd9\u4e48\u505a\u662f\u6709\u9053\u7406\u7684\uff0c\u56e0\u4e3a\u6784\u9020 B() \u662f\u6709\u53ef\u80fd\u4f1a\u53bb\u4f7f\u7528\u6210\u5458\u53d8\u91cf m_a \u7684\uff0c\u6240\u4ee5\u5b83\u5e94\u8be5\u9996\u5148\u88ab\u521d\u59cb\u5316\u3002 \u4e0a\u9762\u6700\u540e\u4e00\u4e2a\u4f8b\u5b50\u4e2d\uff0c m_a \u662f\u4e00\u4e2aclass\u7c7b\u578b\u7684\u53d8\u91cf\u3002 \u5bf9\u4e8eclass\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u5373\u4f7f\u6211\u4eec\u6ca1\u6709\u663e\u5f0f\u5730\u521d\u59cb\u5316\u5b83\u4eec\uff0c\u5b83\u4e5f\u4f1a\u88ab\u521d\u59cb\u5316\u3002 \u5728\u4e0b\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba\u5982\u4f55\u521d\u59cb\u5316\u8fd9\u4e9bclass\u7c7b\u578b\u7684\u6210\u5458\u53d8\u91cf\u3002 \u6784\u9020\u51fd\u6570\u5c0f\u7ed3 \u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u4f1a\u6709\u8fd9\u6837\u7684\u56f0\u60d1\uff1a\u6784\u9020\u51fd\u6570\u4f1a\u521b\u5efa\u5bf9\u8c61\u5417\uff1f\u5b9e\u9645\u4e0a\u6784\u9020\u51fd\u6570\u5e76\u4e0d\u4f1a\u521b\u5efa\u5bf9\u8c61\u2014\u2014\u7f16\u8bd1\u5668\u4f1a\u5728\u6784\u9020\u51fd\u6570\u8c03\u7528\u524d\u5c31\u4e3a\u5bf9\u8c61\u5206\u914d\u5185\u5b58\u3002 \u6784\u9020\u51fd\u6570\u5b9e\u9645\u4e0a\u6709\u4e24\u4e2a\u76ee\u7684\uff1a \u6784\u9020\u51fd\u6570\u53ef\u4ee5\u51b3\u5b9a\u8c01\u80fd\u591f\u521b\u5efa\u8fd9\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u53ea\u6709\u5f53\u80fd\u591f\u627e\u5230\u5339\u914d\u7684\u6784\u9020\u51fd\u6570\u65f6\uff0c\u8be5\u7c7b\u578b\u7684\u5bf9\u8c61\u624d\u80fd\u591f\u88ab\u521b\u5efa\u3002 \u6784\u9020\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u521d\u59cb\u5316\u5bf9\u8c61\u3002\u81f3\u4e8e\u6784\u9020\u51fd\u6570\u662f\u5426\u4f1a\u8fdb\u884c\u5b9e\u9645\u7684\u521d\u59cb\u5316\u5de5\u4f5c\uff0c\u8fd9\u53d6\u51b3\u4e8e\u7a0b\u5e8f\u5458\u3002\u6784\u9020\u51fd\u6570\u4e0d\u8fdb\u884c\u4efb\u4f55\u7684\u521d\u59cb\u5316\u5de5\u4f5c\uff0c\u4ece\u8bed\u6cd5\u5c42\u9762\u6765\u8bb2\u4e5f\u662f\u5408\u6cd5\u7684\u3002 \u7136\u800c\uff0c\u5c31\u50cf\u521d\u59cb\u5316\u6240\u6709\u5c40\u90e8\u53d8\u91cf\u662f\u6700\u4f73\u5b9e\u8df5\u4e00\u6837\uff0c\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u521d\u59cb\u5316\u6240\u6709\u6210\u5458\u53d8\u91cf\u4e5f\u662f\u6700\u4f73\u5b9e\u8df5\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u6784\u9020\u51fd\u6570\u6216\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u6765\u5b8c\u6210\u3002 \u6700\u4f73\u5b9e\u8df5 \u6c38\u8fdc\u8981\u8bb0\u5f97\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u5168\u90e8\u6210\u5458\u53d8\u91cf\u3002 \u6700\u540e\uff0c\u6784\u9020\u51fd\u6570\u4ec5\u7528\u4e8e\u521b\u5efa\u5bf9\u8c61\u65f6\u7684\u521d\u59cb\u5316\u3002\u4e0d\u8981\u4f7f\u7528\u6784\u9020\u51fd\u6570\u6765\u91cd\u65b0\u521d\u59cb\u5316\u73b0\u6709\u5bf9\u8c61\u3002\u867d\u7136\u53ef\u4ee5\u7f16\u8bd1\uff0c\u4f46\u7ed3\u679c\u4e0d\u4f1a\u662f\u4f60\u60f3\u8981\u7684(\u76f8\u53cd\uff0c\u7f16\u8bd1\u5668\u5c06\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff0c\u7136\u540e\u4e22\u5f03\u5b83)\u3002","title":"13.5 - \u6784\u9020\u51fd\u6570"},{"location":"13-5-constructors/#_1","text":"\u6784\u9020\u51fd\u6570 \u662f\u4e00\u7c7b\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u4f1a\u5728\u5bf9\u8c61\u521b\u5efa\u65f6\u88ab\u81ea\u52a8\u8c03\u7528\u3002\u6784\u9020\u51fd\u6570\u901a\u5e38\u7528\u4e8e\u4f7f\u7528\u7528\u6237\u63d0\u4f9b\u503c\u6765\u521d\u59cb\u5316\u7c7b\u7684\u6210\u5458\u53d8\u91cf\uff0c\u6216\u8005\u7528\u4e8e\u4e3a\u5bf9\u8c61\u7684\u4f7f\u7528\u505a\u597d\u76f8\u5e94\u7684\u51c6\u5907\u5de5\u4f5c\uff08\u4f8b\u5982\uff1a\u6253\u5f00\u6587\u4ef6\u6216\u6570\u636e\u5e93\uff09\u3002 \u6784\u9020\u51fd\u6570\u6267\u884c\u540e\uff0c\u5bf9\u8c61\u5e94\u8be5\u5904\u4e8e\u4e00\u79cd\u88ab\u826f\u597d\u5b9a\u4e49\u3001\u53ef\u4ee5\u4f7f\u7528\u7684\u72b6\u6001\u3002 \u548c\u666e\u901a\u7684\u6210\u5458\u51fd\u6570\u4e0d\u540c\uff0c\u6784\u9020\u51fd\u6570\u7684\u547d\u540d\u5fc5\u987b\u9075\u5faa\u4ee5\u4e0b\u539f\u5219\uff1a \u6784\u9020\u51fd\u6570\u7684\u540d\u5b57\u5fc5\u987b\u548c\u7c7b\u540d\u5b8c\u5168\u4e00\u81f4\uff08\u5305\u62ec\u5927\u5c0f\u5199\uff09\uff1b \u6784\u9020\u51fd\u6570\u4e0d\u8fd4\u56de\u4efb\u4f55\u7c7b\u578b\uff08\u751a\u81f3\u4e0d\u8fd4\u56de void \uff09","title":"\u6784\u9020\u51fd\u6570"},{"location":"13-5-constructors/#_2","text":"\u4e0d\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\uff08\u6216\u6240\u6709\u53c2\u6570\u90fd\u6709\u9ed8\u8ba4\u503c\uff09\u7684\u6784\u9020\u51fd\u6570\u79f0\u4e3a \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 \u3002\u5f53\u7528\u6237\u521d\u59cb\u5316\u5bf9\u8c61\u65f6\u4e0d\u63d0\u4f9b\u4efb\u4f55\u53c2\u6570\u65f6\uff0c\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u5c31\u4f1a\u88ab\u8c03\u7528\u3002 \u4e0b\u9762\u4f8b\u5b50\u4e2d\u7684\u7c7b\u5177\u6709\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : Fraction () // default constructor { m_numerator = 0 ; m_denominator = 1 ; } int getNumerator () { return m_numerator ; } int getDenominator () { return m_denominator ; } double getValue () { return static_cast < double > ( m_numerator ) / m_denominator ; } }; int main () { Fraction frac {}; // calls Fraction() default constructor std :: cout << frac . getNumerator () << '/' << frac . getDenominator () << '\\n' ; return 0 ; } Fraction \u7c7b\u7528\u4e8e\u8868\u793a\u4e00\u4e2a\u5206\u6570\uff08\u901a\u8fc7\u5206\u5b50\u548c\u5206\u6bcd\uff09\uff0c\u540c\u65f6\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a Fraction \u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff08\u4e0e\u51fd\u6570\u540c\u540d\uff09\u3002 \u5f53 Fraction frac{}; \u6267\u884c\u65f6\uff0c\u7f16\u8bd1\u5668\u5f97\u77e5\u6211\u4eec\u60f3\u8981\u521d\u59cb\u5316\u4e00\u4e2a Fraction \u7c7b\u578b\u7684\u5bf9\u8c61\uff08\u4e0d\u63d0\u4f9b\u4efb\u4f55\u53c2\u6570\uff09\u3002\u7136\u540e\uff0c\u5bf9\u8c61 frac \u7684\u521d\u59cb\u5316\u5c31\u5f00\u59cb\u4e86\uff0c\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u4e5f\u4f1a\u5728\u6b64\u65f6\u88ab\u8c03\u7528\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a\u9ed8\u8ba4\u521d\u59cb\u5316\uff08\u5c3d\u7ba1\u4e0d\u4e25\u8c28\uff09\u3002\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u8fd0\u884c\u65f6\u548c\u666e\u901a\u51fd\u6570\u4e00\u6837\uff08\u6b64\u5904\u5373\u5c060\u8d4b\u503c\u7ed9 m_numerator \uff0c\u5c06 m_denominator \u8d4b\u503c\u4e3a1 \uff09 \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 0/1","title":"\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u548c\u9ed8\u8ba4\u521d\u59cb\u5316"},{"location":"13-5-constructors/#_3","text":"\u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 \u503c\u521d\u59cb\u5316 \u7684\u65b9\u5f0f\u521d\u59cb\u5316\u5bf9\u8c61\uff1a 1 Fraction frac {}; // Value initialization using empty set of braces \u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528 \u9ed8\u8ba4\u521d\u59cb\u5316 \u6765\u5b9e\u73b0\uff1a 1 Fraction frac ; // Default-initialization, calls default constructor \u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\u548c\u503c\u521d\u59cb\u5316\u6765\u521d\u59cb\u5316\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5176\u7ed3\u679c\u662f\u4e00\u81f4\u7684\uff1a\u5373\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u88ab\u8c03\u7528\u3002 \u6bd4\u8d77\u503c\u521d\u59cb\u5316\uff0c\u5f88\u591a\u7a0b\u5e8f\u5458\u66f4\u503e\u5411\u4e8e\u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\u3002\u8fd9\u662f\u56e0\u4e3a\u5728\u4f7f\u7528\u503c\u521d\u59cb\u5316\u65f6\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u9996\u5148\u5c06\u7c7b\u6210\u5458\u521d\u59cb\u5316\u4e3a0\uff0c\u7136\u540e\u624d\u8c03\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u4ece\u6548\u7387\u7684\u89d2\u5ea6\u6765\u8bb2\u66f4\u4f4e\u6548\u4e00\u4e9b\u3002 \u4e0d\u8fc7\uff0c\u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u4e5f\u6709\u4e0d\u597d\u7684\u5730\u65b9\uff1a\u4f60\u5fc5\u987b\u77e5\u9053\u7c7b\u662f\u5426\u5bf9\u4f1a\u521d\u59cb\u5316\u81ea\u8eab\u3002\u4f8b\u5982\uff0c\u8be5\u7c7b\u7684\u6240\u6709\u6210\u5458\u662f\u4e0d\u662f\u90fd\u5177\u6709\u521d\u59cb\u5316\u503c\uff0c\u6216\u8005\u662f\u5426\u5b58\u5728\u80fd\u591f\u5bf9\u6240\u6709\u6210\u5458\u8fdb\u884c\u521d\u59cb\u5316\u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002 \u5982\u679c\u4f60\u5df2\u7ecf\u77e5\u9053\u6709\u4e2a\u522b\u53d8\u91cf\u4e0d\u5177\u6709\u521d\u59cb\u5316\u503c\uff0c\u90a3\u4f60\u5c31\u8981\u614e\u91cd\u8003\u8651\uff0c\u6b64\u65f6\u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\u662f\u5426\u4f1a\u5e26\u6765\u95ee\u9898\u3002 \u4f8b\u5982\uff0c\u4e0b\u9762\u4ee3\u7801\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include class Fraction { private : // Removed initializers int m_numerator ; int m_denominator ; public : // Removed default-constructor int getNumerator () { return m_numerator ; } int getDenominator () { return m_denominator ; } double getValue () { return static_cast < double > ( m_numerator ) / m_denominator ; } }; int main () { Fraction frac ; // frac is uninitialized, accessing its members causes undefined behavior std :: cout << frac . getNumerator () << '/' << frac . getDenominator () << '\\n' ; return 0 ; } \u867d\u7136\u8fd9\u4e2a\u7c7b\u7684\u786e\u521d\u59cb\u5316\u4e86\u7c7b\u4e2d\u7684\u6240\u6709\u6210\u5458\uff0c\u4f46\u8981\u786e\u4fdd\u9879\u76ee\u4ee3\u7801\u4e2d\u7684\u6240\u6709\u7c7b\u90fd\u8fd9\u6837\u505a\u4e86\u662f\u5f88\u56f0\u96be\u7684\u3002 \u4f18\u5148\u5bf9\u7c7b\u5bf9\u8c61\u8fdb\u884c\u503c\u521d\u59cb\u5316\u662f\u7b80\u5355\u3001\u4e00\u81f4\u7684\uff0c\u5e76\u4e14\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6355\u83b7\u9519\u8bef\uff0c\u7279\u522b\u662f\u5728\u5b66\u4e60\u65f6\u3002 \u6700\u4f73\u5b9e\u8df5 \u521d\u59cb\u5316\u5bf9\u8c61\u65f6\uff0c\u63a8\u8350\u4f7f\u7528\u503c\u521d\u59cb\u5316\u800c\u975e\u9ed8\u8ba4\u521d\u59cb\u5316","title":"\u503c\u521d\u59cb\u5316"},{"location":"13-5-constructors/#_4","text":"\u5c3d\u7ba1\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u5e2e\u6211\u4eec\u786e\u4fdd\u7c7b\u6210\u5458\u5177\u6709\u5408\u7406\u7684\u521d\u503c\uff0c\u4f46\u662f\u66f4\u591a\u65f6\u5019\u6211\u4eec\u8fd8\u662f\u4f1a\u9700\u8981\u5728 \u5b9e\u4f8b\u5316 \u65f6\u6307\u5b9a\u4e00\u4e9b\u7279\u6b8a\u7684\u503c\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u6784\u9020\u51fd\u6570\u662f\u53ef\u4ee5\u88ab\u58f0\u660e\u4e3a\u5141\u8bb8\u63a5\u53d7\u53c2\u6570\u7684\u5f62\u5f0f\u7684\u3002\u4e0b\u9762\u4f8b\u5b50\u4e2d\u7684\u6784\u9020\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u6574\u578b\u53c2\u6570\u5206\u522b\u7528\u4e8e\u5bf9\u5206\u5b50\u5206\u6bcd\u8fdb\u884c\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : Fraction () // default constructor { m_numerator = 0 ; m_denominator = 1 ; } // Constructor with two parameters, one parameter having a default value Fraction ( int numerator , int denominator = 1 ) { assert ( denominator != 0 ); m_numerator = numerator ; m_denominator = denominator ; } int getNumerator () { return m_numerator ; } int getDenominator () { return m_denominator ; } double getValue () { return static_cast < double > ( m_numerator ) / m_denominator ; } }; \u6ce8\u610f\uff0c\u6211\u4eec\u73b0\u5728\u6709\u4e24\u4e2a\u6784\u9020\u51fd\u6570\uff1a\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff08\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u8c03\u7528\uff09\uff0c\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u5f62\u53c2\u3002C++\u652f\u6301 \u91cd\u8f7d \uff0c\u6240\u4ee5\u8fd9\u4e24\u4e2a\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u5728\u548c\u5e73\u5171\u5904\u3002\u4e8b\u5b9e\u4e0a\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5b9a\u4e49\u4efb\u610f\u6570\u91cf\u7684\u6784\u9020\u51fd\u6570\uff0c\u53ea\u8981\u6bcf\u4e2a\u6784\u9020\u51fd\u6570\u5177\u6709\u60df\u4e00\u7684\u7b7e\u540d(\u5f62\u53c2\u7684\u6570\u91cf\u548c\u7c7b\u578b)\u5373\u53ef\u3002 \u90a3\u4e48\uff0c\u6211\u4eec\u5e94\u8be5\u5982\u4f55\u8c03\u7528\u8fd9\u4e2a\u5177\u6709\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u5462\uff1f\u5f88\u7b80\u5355\uff0c\u4f7f\u7528 \u5217\u8868\u521d\u59cb\u5316 \u5373\u53ef\uff1a 1 2 Fraction fiveThirds { 5 , 3 }; // \u5217\u8868\u521d\u59cb\u5316\uff0c\u8c03\u7528 Fraction(int, int) Fraction threeQuarters ( 3 , 4 ); // \u76f4\u63a5\u521d\u59cb\u5316\uff0c\u4e5f\u662f\u8c03\u7528 Fraction(int, int) \u4e00\u5982\u65e2\u5f80\u5730\uff0c \u6211\u4eec\u66f4\u63a8\u8350\u5217\u8868\u521d\u59cb\u5316\u3002\u5728\u540e\u9762\u7684\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u4e5f\u4f1a\u9047\u5230\u9700\u8981\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\u7684\u60c5\u51b5(\u6a21\u677f\u548c std::initializer_list )\u3002\u8fd8\u6709\u53e6\u5916\u4e00\u79cd\u7279\u6b8a\u7684\u6784\u9020\u51fd\u6570\u4f1a\u4f7f\u7528\u62ec\u53f7\u521d\u59cb\u5316\uff08\u5217\u8868\u521d\u59cb\u5316\uff09\u505a\u4e00\u4e9b\u4e0d\u540c\u7684\u4e8b\u60c5\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\u3002 \u6ce8\u610f\uff0c\u7531\u4e8e\u6784\u9020\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a \u5f62\u53c2 \u5177\u6709\u9ed8\u8ba4\u503c\uff0c\u6240\u4ee5\u4e0b\u9762\u7684\u8c03\u7528\u65b9\u5f0f\u4e5f\u662f\u5408\u6cd5\u7684: 1 Fraction six { 6 }; // \u8c03\u7528 Fraction(int, int) \u6784\u9020\u51fd\u6570\uff0c\u7b2c\u4e8c\u4e2a\u5f62\u53c2\u4f1a\u4f7f\u7528\u9ed8\u8ba4\u503c1\u3002 \u6784\u9020\u51fd\u6570\u7684\u9ed8\u8ba4\u53c2\u6570\u548c\u666e\u901a\u51fd\u6570\u7684\u9ed8\u8ba4\u53c2\u6570\u6ca1\u6709\u4ec0\u4e48\u533a\u522b\uff0c\u6240\u4ee5\u5f53\u6211\u4eec\u8c03\u7528 six{ 6 } \u65f6\uff0c Fraction(int, int) \u51fd\u6570\u4f1a\u81ea\u52a8\u4f7f\u75281\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u5f62\u53c2\u7684\u503c\u3002 \u6700\u4f73\u5b9e\u8df5 \u63a8\u8350\u4f7f\u7528\u5927\u62ec\u53f7\u521d\u59cb\u5316\u6765\u521d\u59cb\u7c7b\u5bf9\u8c61\u3002","title":"\u4f7f\u7528\u5e26\u53c2\u6570\u7684\u51fd\u6570\u8fdb\u884c\u76f4\u63a5\u521d\u59cb\u5316\u548c\u5217\u8868\u521d\u59cb\u5316"},{"location":"13-5-constructors/#_5","text":"\u5c31\u50cf\u57fa\u672c\u53d8\u91cf\u4e00\u6837\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 \u62f7\u8d1d\u521d\u59cb\u5316 \u6765\u521d\u59cb\u5316\u7c7b\uff1a 1 2 Fraction six = Fraction { 6 }; // Copy initialize a Fraction, will call Fraction(6, 1) Fraction seven = 7 ; // Copy initialize a Fraction. The compiler will try to find a way to convert 7 to a Fraction, which will invoke the Fraction(7, 1) constructor. \u4e0d\u8fc7\uff0c\u6700\u597d\u4e0d\u8981\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u6765\u521d\u59cb\u5316\u7c7b\uff0c\u5b83\u53ef\u80fd\u662f\u4f4e\u6548\u7684\u3002\u5c3d\u7ba1 \u76f4\u63a5\u521d\u59cb\u5316 \u3001 \u5217\u8868\u521d\u59cb\u5316 \u548c \u62f7\u8d1d\u521d\u59cb\u5316 \u5728\u521d\u59cb\u5316\u57fa\u672c\u6570\u636e\u7c7b\u578b\u65f6\u7684\u884c\u4e3a\u662f\u4e00\u6837\u7684\uff0c\u4f46\u662f\u62f7\u8d1d\u521d\u59cb\u5316\u5728\u914d\u5408\u7c7b\u7c7b\u578b\u4f7f\u7528\u65f6\u5176\u884c\u4e3a\u662f\u4e0d\u540c\u7684\uff08\u5c3d\u7ba1\u7ed3\u679c\u76f8\u540c\uff09\u3002\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u7ae0\u8282\u4e2d\u8fdb\u884c\u66f4\u4e3a\u8be6\u7ec6\u7684\u4ecb\u7ecd\u3002","title":"\u4f7f\u7528\u7b49\u4e8e\u53f7\u8fdb\u884c\u62f7\u8d1d\u521d\u59cb\u5316"},{"location":"13-5-constructors/#_6","text":"\u50cf\u4e0a\u9762\u90a3\u6837\u4e3a Fraction \u58f0\u660e\u4e24\u4e2a\u6784\u9020\u51fd\u6570\u662f\u591a\u4f59\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) { assert ( denominator != 0 ); m_numerator = numerator ; m_denominator = denominator ; } int getNumerator () { return m_numerator ; } int getDenominator () { return m_denominator ; } double getValue () { return static_cast < double > ( m_numerator ) / m_denominator ; } }; \u5c3d\u7ba1\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u4ecd\u7136\u662f\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u4f46\u662f\u5b83\u73b0\u5728\u4e5f\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u57fa\u4e8e\u7528\u6237\u63d0\u4f9b\u7684\u53c2\u6570\u8fdb\u884c\u521d\u59cb\u5316\u3002 1 2 3 4 Fraction zero ; // will call Fraction(0, 1) Fraction zero {}; // will call Fraction(0, 1) Fraction six { 6 }; // will call Fraction(6, 1) Fraction fiveThirds { 5 , 3 }; // will call Fraction(5, 3) \u5728\u5b9e\u73b0\u6784\u9020\u51fd\u6570\u65f6\uff0c\u8981\u8003\u8651\u5982\u4f55\u901a\u8fc7\u9ed8\u8ba4\u503c\u6765\u51cf\u5c11\u6784\u9020\u51fd\u6570\u7684\u6570\u91cf\u3002","title":"\u51cf\u5c11\u6784\u9020\u51fd\u6570\u7684\u6570\u91cf"},{"location":"13-5-constructors/#_7","text":"\u5b9a\u4e49\u548c\u8c03\u7528\u5177\u6709\u9ed8\u8ba4\u53c2\u6570\u51fd\u6570\u7684\u89c4\u5219 (\u53c2\u89c1\uff1a 8.12 - \u9ed8\u8ba4\u53c2\u6570 ) \u540c\u6837\u4e5f\u9002\u7528\u4e8e\u6784\u9020\u51fd\u6570\u3002\u7b80\u5355\u590d\u4e60\u4e00\u4e0b\uff0c\u5728\u5b9a\u4e49\u5177\u6709\u9ed8\u8ba4\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u65f6\uff0c\u9ed8\u8ba4\u53c2\u6570\u5fc5\u987b\u5728\u975e\u9ed8\u8ba4\u53c2\u6570\u7684\u540e\u9762\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u975e\u9ed8\u8ba4\u53c2\u6570\u4e0d\u80fd\u88ab\u5b9a\u4e49\u5728\u9ed8\u8ba4\u53c2\u6570\u540e\u3002 \u5bf9\u4e8e\u5177\u6709\u591a\u4e2a\u9ed8\u8ba4\u53c2\u6570\uff0c\u4e14\u53c2\u6570\u7c7b\u578b\u4e0d\u540c\u7684\u6784\u9020\u51fd\u6570\uff0c\u4f7f\u7528\u4e0d\u5f53\u4f1a\u5e26\u6765\u51fa\u4e4e\u610f\u6599\u7684\u9519\u8bef\u3002\u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Something { public : // Default constructor Something ( int n = 0 , double d = 1.2 ) // allows us to construct a Something(int, double), Something(int), or Something() { } }; int main () { Something s1 { 1 , 2.4 }; // calls Something(int, double) Something s2 { 1 }; // calls Something(int, double) Something s3 {}; // calls Something(int, double) Something s4 { 2.4 }; // \u65e0\u6cd5\u7f16\u8bd1\uff0c\u56e0\u4e3a\u6ca1\u6709\u80fd\u5339\u914d\u7684\u6784\u9020\u51fd\u6570 Something(double) return 0 ; } \u5bf9\u4e8e s4 \uff0c\u6211\u4eec\u5e0c\u671b\u4f7f\u7528\u4e00\u4e2a double \u7c7b\u578b\u7684\u53c2\u6570\u6784\u9020 Something \u3002\u4e0a\u9762\u7a0b\u5e8f\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\uff0c\u56e0\u4e3a\u89c4\u5219\u4e0d\u5141\u8bb8\u6211\u4eec\u8df3\u8fc7\u9ed8\u8ba4\u53c2\u6570\u524d\u9762\u7684\u9ed8\u8ba4\u53c2\u6570\u4e3a\u540e\u9762\u7684\u9ed8\u8ba4\u53c2\u6570\u63d0\u4f9b\u5177\u4f53\u503c\uff08\u8fd9\u91cc\u5373\u6700\u5de6\u8fb9\u7684 int \u7c7b\u578b\u7684 \u5f62\u53c2 \u4e0d\u80fd\u88ab\u8df3\u8fc7\uff09\u3002 \u5982\u679c\u60f3\u57fa\u4e8e\u4e00\u4e2a double \u7c7b\u578b\u7684\u53d8\u91cf\u6784\u9020 Something \uff0c\u5219\u5fc5\u987b\u518d\u5b9a\u4e49\u4e00\u4e2a\uff08\u975e\u9ed8\u8ba4\uff09\u6784\u9020\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class Something { public : // Default constructor Something ( int n = 0 , double d = 1.2 ) // allows us to construct a Something(int, double), Something(int), or Something() { } Something ( double d ) { } }; int main () { Something s1 { 1 , 2.4 }; // calls Something(int, double) Something s2 { 1 }; // calls Something(int, double) Something s3 {}; // calls Something(int, double) Something s4 { 2.4 }; // calls Something(double) return 0 ; }","title":"\u5173\u4e8e\u9ed8\u8ba4\u53c2\u6570\u7684\u6ce8\u610f\u4e8b\u9879"},{"location":"13-5-constructors/#_8","text":"\u5982\u679c\u4f60\u7684\u7c7b\u6ca1\u6709\u5b9a\u4e49\u6784\u9020\u51fd\u6570\uff0c\u5219C++\u53ef\u4ee5\u81ea\u52a8\u4e3a\u4f60\u751f\u6210\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u88ab\u79f0\u4e3a \u9690\u5f0f\u6784\u9020\u51fd\u6570 \u3002 \u8003\u8651\u4e0b\u9762\u7684\u7c7b\u5b9a\u4e49\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Date { private : int m_year { 1900 }; int m_month { 1 }; int m_day { 1 }; // \u7528\u6237\u6ca1\u6709\u63d0\u4f9b\u6784\u9020\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u81ea\u52a8\u751f\u6210\u9ed8\u8ba4\u6784\u9020\u51fd\u6570 }; int main () { Date date {}; return 0 ; } Date \u7c7b\u6ca1\u6709\u6784\u9020\u51fd\u6570\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u4f1a\u4e3a\u5176\u521b\u5efa\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u8be5\u9690\u5f0f\u6784\u9020\u51fd\u6570\uff0c\u5728\u4e0d\u63d0\u4f9b\u53c2\u6570\u7684\u60c5\u51b5\u4e0b\u521b\u5efa Date \u5bf9\u8c61\u3002 \u5f53\u9690\u5f0f\u6784\u9020\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u5177\u6709\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u503c\u7684\u6210\u5458\u4f1a\u88ab\u521d\u59cb\u5316(\u53c2\u8003\uff1a 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 \u548c 13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u5217\u8868 )\u3002 \u53ea\u8981\u4f60\u7684\u7c7b\u4e2d\u6709\u6784\u9020\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u5c31\u4e0d\u4f1a\u4e3a\u4f60\u521b\u5efa\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 class Date { private : int m_year { 1900 }; int m_month { 1 }; int m_day { 1 }; public : Date ( int year , int month , int day ) // normal non-default constructor { m_year = year ; m_month = month ; m_day = day ; } // No implicit constructor provided because we already defined our own constructor }; int main () { Date date {}; // error: Can't instantiate object because default constructor doesn't exist and the compiler won't generate one Date today { 2020 , 1 , 19 }; // today is initialized to Jan 19th, 2020 return 0 ; } \u5982\u679c\u4f60\u7684\u7c7b\u5177\u6709\u5176\u4ed6\u6784\u9020\u51fd\u6570\uff0c\u4f46\u662f\u4f60\u9700\u8981\u7c7b\u652f\u6301\u9ed8\u8ba4\u521d\u59cb\u5316\uff0c\u5219\u53ef\u4ee5\u4e3a\u6784\u9020\u51fd\u6570\u7684\u6bcf\u4e2a\u5f62\u53c2\u6dfb\u52a0\u9ed8\u8ba4\u503c\uff0c\u6216\u8005\u663e\u5f0f\u5730\u5b9a\u4e49\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002 \u5f53\u7136\uff0c\u8fd8\u6709\u7b2c\u4e09\u79cd\u65b9\u5f0f\uff1a\u4f60\u53ef\u4ee5\u4f7f\u7528 default \u5173\u952e\u5b57\u544a\u77e5\u7f16\u8bd1\u5668\u751f\u6210\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 class Date { private : int m_year { 1900 }; int m_month { 1 }; int m_day { 1 }; public : // \u8981\u6c42\u7f16\u8bd1\u5668\u751f\u6210\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u5373\u4f7f // \u7528\u6237\u5df2\u7ecf\u63d0\u4f9b\u4e86\u6784\u9020\u51fd\u6570 Date () = default ; Date ( int year , int month , int day ) // \u666e\u901a\u975e\u9ed8\u8ba4\u6784\u9020\u51fd\u6570 { m_year = year ; m_month = month ; m_day = day ; } }; int main () { Date date {}; // date is initialized to Jan 1st, 1900 Date today { 2020 , 10 , 14 }; // today is initialized to Oct 14th, 2020 return 0 ; } \u5199 default \u6bd4\u5199\u4e00\u4e2a\u7a7a\u7684\u51fd\u6570\u4f53\u66f4\u8d39\u4e8b\uff0c\u4f46\u662f\u5b83\u80fd\u591f\u66f4\u597d\u5730\u8868\u660e\u4f60\u7684\u610f\u56fe\uff08\u521b\u5efa\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff09\uff0c\u800c\u4e14\u4e5f\u66f4\u5b89\u5168\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5bf9\u6ca1\u6709\u521d\u59cb\u5316\u503c\u7684\u51fd\u6570\u8fdb\u884c0\u521d\u59cb\u5316\u3002 default \u5bf9\u4e8e\u5176\u4ed6\u7279\u6b8a\u6784\u9020\u51fd\u6570\u4e5f\u6709\u6548\uff0c\u7b49\u6211\u4eec\u9047\u5230\u65f6\u518d\u8ba8\u8bba\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u7c7b\u5df2\u7ecf\u62e5\u6709\u6784\u9020\u51fd\u6570\uff0c\u4f46\u4f60\u4ecd\u7136\u9700\u8981\u4e00\u4e2a\u4ec0\u4e48\u90fd\u4e0d\u505a\u7684\u6784\u9020\u51fd\u6570(\u4f8b\u5982\uff0c\u6240\u6709\u6210\u5458\u51fd\u6570\u90fd\u901a\u8fc7\u975e\u9759\u6001\u6210\u5458\u51fd\u6570\u521d\u59cb\u5316\u8fdb\u884c\u4e86\u521d\u59cb\u5316)\uff0c\u53ef\u4ee5\u4f7f\u7528 = default\u3002","title":"\u9690\u5f0f\u751f\u6210\u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570"},{"location":"13-5-constructors/#_9","text":"class \u4e2d\u7684\u6210\u5458\u53d8\u91cf\uff0c\u4e5f\u53ef\u4ee5\u662f\u5176\u4ed6\u7c7b\u5bf9\u8c61\u3002 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5f53\u5916\u5c42\u5bf9\u8c61\u88ab\u521d\u59cb\u5316\u65f6\uff0c\u5185\u90e8\u7684\u8fd9\u4e9b\u5bf9\u8c61\u5176\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u4e5f\u4f1a\u88ab\u8c03\u7528\uff0c\u800c\u4e14\u4f1a\u5728\u6267\u884c\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u4e4b\u524d\u53d1\u751f\u3002 \u4e3e\u4f8b\u6765\u8bf4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include class A { public : A () { std :: cout << \"A \\n \" ; } }; class B { private : A m_a ; // A \u662f B \u7684\u4e00\u4e2a\u6210\u5458 public : B () { std :: cout << \"B \\n \" ; } }; int main () { B b ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 A B \u5f53\u53d8\u91cf b \u88ab\u6784\u5efa\u65f6\uff0c\u6784\u9020\u51fd\u6570 B() \u4f1a\u88ab\u8c03\u7528\uff0c\u5728 B() \u7684\u51fd\u6570\u4f53\u6267\u884c\u524d\uff0c m_a \u5c31\u5f00\u59cb\u521d\u59cb\u5316\u4e86\uff0c\u6b64\u65f6\u4f1a\u8c03\u7528 A \u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u5e76\u6253\u5370 \"A\"\u3002\u7136\u540e\u63a7\u5236\u6743\u8fd4\u8fd8\u7ed9\u6784\u9020\u51fd\u6570 B() \u5e76\u6267\u884c\u5176\u51fd\u6570\u4f53\u3002 \u8fd9\u4e48\u505a\u662f\u6709\u9053\u7406\u7684\uff0c\u56e0\u4e3a\u6784\u9020 B() \u662f\u6709\u53ef\u80fd\u4f1a\u53bb\u4f7f\u7528\u6210\u5458\u53d8\u91cf m_a \u7684\uff0c\u6240\u4ee5\u5b83\u5e94\u8be5\u9996\u5148\u88ab\u521d\u59cb\u5316\u3002 \u4e0a\u9762\u6700\u540e\u4e00\u4e2a\u4f8b\u5b50\u4e2d\uff0c m_a \u662f\u4e00\u4e2aclass\u7c7b\u578b\u7684\u53d8\u91cf\u3002 \u5bf9\u4e8eclass\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u5373\u4f7f\u6211\u4eec\u6ca1\u6709\u663e\u5f0f\u5730\u521d\u59cb\u5316\u5b83\u4eec\uff0c\u5b83\u4e5f\u4f1a\u88ab\u521d\u59cb\u5316\u3002 \u5728\u4e0b\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba\u5982\u4f55\u521d\u59cb\u5316\u8fd9\u4e9bclass\u7c7b\u578b\u7684\u6210\u5458\u53d8\u91cf\u3002","title":"\u5305\u542b\u7c7b\u7c7b\u578b\u6210\u5458\u7684\u7c7b"},{"location":"13-5-constructors/#_10","text":"\u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u4f1a\u6709\u8fd9\u6837\u7684\u56f0\u60d1\uff1a\u6784\u9020\u51fd\u6570\u4f1a\u521b\u5efa\u5bf9\u8c61\u5417\uff1f\u5b9e\u9645\u4e0a\u6784\u9020\u51fd\u6570\u5e76\u4e0d\u4f1a\u521b\u5efa\u5bf9\u8c61\u2014\u2014\u7f16\u8bd1\u5668\u4f1a\u5728\u6784\u9020\u51fd\u6570\u8c03\u7528\u524d\u5c31\u4e3a\u5bf9\u8c61\u5206\u914d\u5185\u5b58\u3002 \u6784\u9020\u51fd\u6570\u5b9e\u9645\u4e0a\u6709\u4e24\u4e2a\u76ee\u7684\uff1a \u6784\u9020\u51fd\u6570\u53ef\u4ee5\u51b3\u5b9a\u8c01\u80fd\u591f\u521b\u5efa\u8fd9\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u53ea\u6709\u5f53\u80fd\u591f\u627e\u5230\u5339\u914d\u7684\u6784\u9020\u51fd\u6570\u65f6\uff0c\u8be5\u7c7b\u578b\u7684\u5bf9\u8c61\u624d\u80fd\u591f\u88ab\u521b\u5efa\u3002 \u6784\u9020\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u521d\u59cb\u5316\u5bf9\u8c61\u3002\u81f3\u4e8e\u6784\u9020\u51fd\u6570\u662f\u5426\u4f1a\u8fdb\u884c\u5b9e\u9645\u7684\u521d\u59cb\u5316\u5de5\u4f5c\uff0c\u8fd9\u53d6\u51b3\u4e8e\u7a0b\u5e8f\u5458\u3002\u6784\u9020\u51fd\u6570\u4e0d\u8fdb\u884c\u4efb\u4f55\u7684\u521d\u59cb\u5316\u5de5\u4f5c\uff0c\u4ece\u8bed\u6cd5\u5c42\u9762\u6765\u8bb2\u4e5f\u662f\u5408\u6cd5\u7684\u3002 \u7136\u800c\uff0c\u5c31\u50cf\u521d\u59cb\u5316\u6240\u6709\u5c40\u90e8\u53d8\u91cf\u662f\u6700\u4f73\u5b9e\u8df5\u4e00\u6837\uff0c\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u521d\u59cb\u5316\u6240\u6709\u6210\u5458\u53d8\u91cf\u4e5f\u662f\u6700\u4f73\u5b9e\u8df5\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u6784\u9020\u51fd\u6570\u6216\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u6765\u5b8c\u6210\u3002 \u6700\u4f73\u5b9e\u8df5 \u6c38\u8fdc\u8981\u8bb0\u5f97\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u5168\u90e8\u6210\u5458\u53d8\u91cf\u3002 \u6700\u540e\uff0c\u6784\u9020\u51fd\u6570\u4ec5\u7528\u4e8e\u521b\u5efa\u5bf9\u8c61\u65f6\u7684\u521d\u59cb\u5316\u3002\u4e0d\u8981\u4f7f\u7528\u6784\u9020\u51fd\u6570\u6765\u91cd\u65b0\u521d\u59cb\u5316\u73b0\u6709\u5bf9\u8c61\u3002\u867d\u7136\u53ef\u4ee5\u7f16\u8bd1\uff0c\u4f46\u7ed3\u679c\u4e0d\u4f1a\u662f\u4f60\u60f3\u8981\u7684(\u76f8\u53cd\uff0c\u7f16\u8bd1\u5668\u5c06\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff0c\u7136\u540e\u4e22\u5f03\u5b83)\u3002","title":"\u6784\u9020\u51fd\u6570\u5c0f\u7ed3"},{"location":"13-6-constructor-member-initializer-lists/","tags":["constructors","initialization"],"text":"\u5728\u4e0a\u4e00\u8282\u8bfe\u4e2d\uff0c\u4e3a\u4e86\u7b80\u5316\u95ee\u9898\uff0c\u6211\u4eec\u5728\u6784\u9020\u51fd\u6570\u4e2d\u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\u5bf9\u6210\u5458\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Something { private : int m_value1 {}; double m_value2 {}; char m_value3 {}; public : Something () { // \u90fd\u5c5e\u4e8e\u8d4b\u503c\uff0c\u800c\u4e0d\u662f\u521d\u59cb\u5316 m_value1 = 1 ; m_value2 = 2.2 ; m_value3 = 'c' ; } }; \u5f53\u6784\u9020\u51fd\u6570\u6267\u884c\u65f6\uff0c m_value1 \uff0c m_value2 \u548c m_value3 \u9996\u5148\u88ab\u521b\u5efa\u3002\u7136\u540e\u6267\u884c\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\uff0c\u5bf9\u6210\u5458\u53d8\u91cf\u8fdb\u884c\u8d4b\u503c\u3002\u4e0a\u9762\u7684\u8fc7\u7a0b\u548c\u4e4b\u524d\u7684\u975e\u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u662f\u975e\u5e38\u7c7b\u4f3c\uff1a 1 2 3 4 5 6 7 int m_value1 {}; double m_value2 {}; char m_value3 {}; m_value1 = 1 ; m_value2 = 2.2 ; m_value3 = 'c' ; \u5c3d\u7ba1\u5408\u4e4e\u8bed\u6cd5\uff0c\u4f46\u662f\u4e0a\u8ff0\u4ee3\u7801\u5e76\u4e0d\u5177\u6709\u826f\u597d\u7684\u7f16\u7a0b\u98ce\u683c\uff08\u800c\u4e14\u6548\u7387\u76f8\u8f83\u4e8e\u521d\u59cb\u5316\u662f\u4e0d\u4f73\u7684\uff09\u3002 \u4e0d\u8fc7\uff0c\u5728\u524d\u9762\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u63d0\u5230\u8fc7\uff0c\u6709\u4e9b\u7c7b\u578b\u7684\u53d8\u91cf\uff08\u4f8b\u5982const\u53d8\u91cf\u6216 \u5f15\u7528 \uff09\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 class Something { private : const int m_value ; public : Something () { m_value = 1 ; // \u9519\u8bef: const \u53d8\u91cf\u4e0d\u80fd\u88ab\u8d4b\u503c } }; \u4e0a\u8ff0\u4ee3\u7801\u7b49\u4ef7\u4e8e\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 const int m_value ; // error: const vars must be initialized with a value m_value = 5 ; // error: const vars can not be assigned to \u5728\u6784\u9020\u51fd\u6570\u5185\u90e8\u5bf9 const \u53d8\u91cf\u6216\u5f15\u7528\u8d4b\u503c\u663e\u7136\u4e0d\u662f\u4e00\u4e2a\u53ef\u884c\u7684\u529e\u6cd5\u3002 \u6210\u5458\u521d\u59cb\u5316\u5217\u8868 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cC++ \u63d0\u4f9b\u4e86\u4e00\u79cd\u5bf9\u6210\u5458\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\uff08\u800c\u4e0d\u662f\u5728\u521b\u5efa\u540e\u8d4b\u503c\uff09\u7684\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u3002\u8bf7\u4e0d\u8981\u628a\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u548c\u7528\u4e8e\u5bf9\u6570\u7ec4\u8fdb\u884c\u8d4b\u503c\u7684 \u521d\u59cb\u5316\u503c\u5217\u8868 \u641e\u6df7\u3002 \u5728 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u521d\u59cb\u5316\u53d8\u91cf\u7684\u65b9\u5f0f\u6709\u4e09\u79cd\uff1a \u62f7\u8d1d\u521d\u59cb\u5316 \u3001 \u76f4\u63a5\u521d\u59cb\u5316 \u548c \u7edf\u4e00\u521d\u59cb\u5316 \u3002 1 2 3 int value1 = 1 ; // copy initialization double value2 ( 2.2 ); // direct initialization char value3 { 'c' }; // uniform initialization \u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u8fdb\u884c\u521d\u59cb\u5316\u57fa\u672c\u4e0a\u548c \u76f4\u63a5\u521d\u59cb\u5316 \u6216 \u7edf\u4e00\u521d\u59cb\u5316 \u662f\u5b8c\u5168\u4e00\u81f4\u7684\u3002 \u4e3e\u4e2a\u4f8b\u5b50\u5c31\u66f4\u5bb9\u6613\u7406\u89e3\u4e86\u3002\u8fd8\u8bb0\u5f97\u4e4b\u524d\u4e3a\u53d8\u91cf\u8d4b\u503c\u7684\u6784\u9020\u51fd\u6570\u5417\uff1f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Something { private : int m_value1 {}; double m_value2 {}; char m_value3 {}; public : Something () { // \u90fd\u662f\u8d4b\u503c\uff0c\u800c\u4e0d\u662f\u521d\u59cb\u5316 m_value1 = 1 ; m_value2 = 2.2 ; m_value3 = 'c' ; } }; \u63a5\u4e0b\u6765\uff0c\u91cd\u5199\u51fd\u6570\uff0c\u4f7f\u7528\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u6765\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include class Something { private : int m_value1 {}; double m_value2 {}; char m_value3 {}; public : Something () : m_value1 { 1 }, m_value2 { 2.2 }, m_value3 { 'c' } // Initialize our member variables { // No need for assignment here } void print () { std :: cout << \"Something(\" << m_value1 << \", \" << m_value2 << \", \" << m_value3 << \") \\n \" ; } }; int main () { Something something {}; something . print (); return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Something(1, 2.2, c) \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u4f4d\u4e8e\u6784\u9020\u51fd\u6570\u53c2\u6570\u5217\u8868\u540e\uff0c\u4ee5\u5192\u53f7 : \u5f00\u5934\uff0c\u540e\u9762\u662f\u4e00\u7cfb\u5217\u53d8\u91cf\u548c\u5b83\u4eec\u7684\u521d\u59cb\u5316\u503c\uff08\u4f7f\u7528\u9017\u53f7\u5206\u9694\uff09\u3002 \u6ce8\u610f\uff0c\u51fd\u6570\u4f53\u5185\u4e0d\u518d\u9700\u8981\u5bf9\u53d8\u91cf\u8fdb\u884c\u8d4b\u503c\uff0c\u56e0\u4e3a\u521d\u59cb\u5316\u7684\u5de5\u4f5c\u5df2\u7ecf\u7531\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u5b8c\u6210\u3002\u540c\u65f6\u8fd8\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u540e\u9762\u5e76\u6ca1\u6709\u5206\u53f7\u3002 \u5f53\u7136\uff0c\u5982\u679c\u80fd\u591f\u901a\u8fc7\u6784\u9020\u51fd\u6570\u6765\u4f20\u9012\u521d\u59cb\u5316\u503c\u5219\u4f1a\u66f4\u6709\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include class Something { private : int m_value1 {}; double m_value2 {}; char m_value3 {}; public : Something ( int value1 , double value2 , char value3 = 'c' ) : m_value1 { value1 }, m_value2 { value2 }, m_value3 { value3 } // \u76f4\u63a5\u521d\u59cb\u5316\u6210\u5458\u53d8\u91cf { // No need for assignment here } void print () { std :: cout << \"Something(\" << m_value1 << \", \" << m_value2 << \", \" << m_value3 << \") \\n \" ; } }; int main () { Something something { 1 , 2.2 }; // value1 = 1, value2=2.2, value3 \u4f7f\u7528\u9ed8\u8ba4\u503c 'c' something . print (); return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Something(1, 2.2, c) \u6ce8\u610f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u9ed8\u8ba4\u5f62\u53c2\u6765\u63d0\u4f9b\u9ed8\u8ba4\u503c\uff0c\u9632\u6b62\u7528\u6237\u6ca1\u6709\u4f20\u9012\u53c2\u6570\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u5bf9\u6210\u5458\u8fdb\u884c\u521d\u59cb\u5316\u800c\u4e0d\u662f\u4f9d\u6b21\u4e3a\u5176\u8d4b\u503c\u3002 \u521d\u59cb\u5316 const \u7c7b\u578b\u6210\u5458\u53d8\u91cf \u7c7b\u53ef\u4ee5\u5305\u542b const \u7c7b\u578b\u7684\u6210\u5458\u53d8\u91cf\u3002const \u6210\u5458\u53d8\u91cf\u548c\u4e00\u822c\u7684 const \u53d8\u91cf\u6ca1\u4ec0\u4e48\u533a\u522b\u2014\u2014\u5b83\u4eec\u90fd\u5fc5\u987b\u88ab\u521d\u59cb\u5316\uff0c\u521d\u59cb\u5316\u540e\u5176\u503c\u5c06\u4e0d\u80fd\u88ab\u6539\u53d8\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6784\u9020\u51fd\u6570\u7684\u521d\u59cb\u5316\u5217\u8868\u5bf9 const \u7c7b\u578b\u7684\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\uff08\u548c\u975econst\u6210\u5458\u4e00\u6837\uff09\uff0c\u800c\u4e14\u521d\u59cb\u5316\u503c\u53ef\u4ee5\u662f\u5e38\u6570\u4e5f\u53ef\u4ee5\u4e0d\u662f\u3002 \u4e0b\u4f8b\u4e2d\u7684\u7c7b\uff0c\u5305\u542b\u4e00\u4e2a const \u7c7b\u578b\u7684\u53d8\u91cf\u3002\u6211\u4eec\u4f7f\u7528\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\uff08\u4f7f\u7528\u7528\u6237\u63d0\u4f9b\u7684\u975econst\u503c\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include class Something { private : const int m_value ; public : Something ( int x ) : m_value { x } // directly initialize our const member variable { } void print () { std :: cout << \"Something(\" << m_value << \") \\n \" ; } }; int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; Something s { x }; s . print (); return 0 ; } \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\uff1a 1 2 Enter an integer: 4 Something(4) \u6cd5\u5219 Const \u6210\u5458\u53d8\u91cf\u5fc5\u987b\u88ab\u521d\u59cb\u5316\u3002 \u4f7f\u7528\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u521d\u59cb\u5316\u6570\u7ec4\u7c7b\u578b\u6210\u5458 \u4e0b\u9762\u4f8b\u5b50\u4e2d\u7684\u7c7b\u5305\u542b\u4e00\u4e2a\u6570\u7ec4\u7c7b\u578b\u7684\u6210\u5458\uff1a 1 2 3 4 5 6 class Something { private : const int m_array [ 5 ]; }; \u5728 C++11 \u4e4b\u524d\uff0c\u4f60\u53ea\u80fd\u901a\u8fc7\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\uff0c\u5bf9\u6570\u7ec4\u6210\u5458\u8fdb\u884c0\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 class Something { private : const int m_array [ 5 ]; public : Something () : m_array {} // zero initialize the member array { } }; \u4e0d\u8fc7\uff0c\u4eceC++11\u5f00\u59cb\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 \u7edf\u4e00\u521d\u59cb\u5316 \u5bf9\u6570\u7ec4\u6210\u5458\u8fdb\u884c\u521d\u59cb\u5316\u4e86\uff1a 1 2 3 4 5 6 7 8 9 10 11 class Something { private : const int m_array [ 5 ]; public : Something () : m_array { 1 , 2 , 3 , 4 , 5 } // use uniform initialization to initialize our member array { } }; \u521d\u59cb\u5316\u7c7b\u7c7b\u578b\u7684\u6210\u5458\u53d8\u91cf \u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e5f\u53ef\u7528\u4e8e\u7c7b\u7c7b\u578b\u6210\u5458\u53d8\u91cf\u7684\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include class A { public : A ( int x = 0 ) { std :: cout << \"A \" << x << '\\n' ; } }; class B { private : A m_a {}; public : B ( int y ) : m_a { y - 1 } // call A(int) constructor to initialize member m_a { std :: cout << \"B \" << y << '\\n' ; } }; int main () { B b { 5 }; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 A 4 B 5 \u5f53\u6784\u9020\u53d8\u91cfb\u65f6\uff0c\u7528\u503c5\u8c03\u7528 B(int) \u6784\u9020\u51fd\u6570\u3002\u5728\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u6267\u884c\u4e4b\u524d\uff0c m_a \u88ab\u521d\u59cb\u5316\uff0c\u8c03\u7528\u503c\u4e3a4\u7684 A(int) \u6784\u9020\u51fd\u6570\uff0c\u6253\u5370\u51fa\u201cA 4\u201d\u3002\u7136\u540e\u63a7\u5236\u8fd4\u56de\u5230B\u6784\u9020\u51fd\u6570\uff0c\u6267\u884cB\u6784\u9020\u51fd\u6570\u7684\u4e3b\u4f53\uff0c\u6253\u5370\u201cB 5\u201d\u3002 \u521d\u59cb\u5316\u5217\u8868\u6392\u7248 C++ \u5141\u8bb8\u7a0b\u5e8f\u5458\u7075\u6d3b\u5730\u6392\u7248\u521d\u59cb\u5316\u503c\u5217\u8868\u3002\u867d\u7136\u4f60\u4e5f\u53ef\u6309\u7167\u81ea\u5df1\u7684\u559c\u597d\u6392\u7248\uff0c\u4f46\u662f\u6211\u4eec\u63a8\u8350\u4ee5\u4e0b\u7684\u65b9\u5f0f\uff1a \u5982\u679c\u51fd\u6570\u540d\u540c\u4e00\u884c\u4e2d\u53ef\u4ee5\u653e\u5f97\u4e0b\u521d\u59cb\u5316\u503c\u5217\u8868\uff0c\u5219\u5c06\u5b83\u4eec\u90fd\u653e\u5728\u8fd9\u4e00\u884c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class Something { private : int m_value1 {}; double m_value2 {}; char m_value3 {}; public : Something () : m_value1 { 1 }, m_value2 { 2.2 }, m_value3 { 'c' } // everything on one line { } }; \u5982\u679c\u653e\u4e0d\u4e0b\uff0c\u5219\u5728\u4e0b\u4e00\u884c\u4e2d\u7f29\u8fdb\u653e\u7f6e\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Something { private : int m_value1 ; double m_value2 ; char m_value3 ; public : Something ( int value1 , double value2 , char value3 = 'c' ) // \u51fd\u6570\u540d\u8fd9\u4e00\u884c\u5df2\u7ecf\u591f\u957f\u4e86 : m_value1 { value1 }, m_value2 { value2 }, m_value3 { value3 } // \u6240\u4ee5\u5c06\u521d\u59cb\u5316\u5217\u8868\u653e\u5728\u4e0b\u4e00\u884c { } }; \u5982\u679c\u8fd9\u6837\u4e5f\u4e0d\u80fd\u5c06\u521d\u59cb\u5316\u503c\u5217\u8868\u653e\u5728\u4e00\u884c\uff0c\u5219\u901a\u8fc7\u7a7a\u683c\u6392\u7248\uff0c\u6bcf\u884c\u4e00\u4e2a\u521d\u59cb\u5316\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class Something { private : int m_value1 {}; double m_value2 {}; char m_value3 {}; float m_value4 {}; public : Something ( int value1 , double value2 , char value3 = 'c' , float value4 = 34.6f ) // this line already has a lot of stuff on it : m_value1 { value1 } // one per line , m_value2 { value2 } , m_value3 { value3 } , m_value4 { value4 } { } }; \u521d\u59cb\u5316\u5217\u8868\u7684\u987a\u5e8f \u4ee4\u4eba\u60ca\u8bb6\u7684\u662f\uff0c\u521d\u59cb\u5316\u5217\u8868\u4e2d\u7684\u53d8\u91cf\u5e76\u4e0d\u4f1a\u6309\u7167\u5b83\u4eec\u5728\u521d\u59cb\u5316\u5217\u8868\u4e2d\u6307\u5b9a\u7684\u987a\u5e8f\u521d\u59cb\u5316\u3002\u5b9e\u9645\u4e0a\uff0c\u5b83\u4eec\u5c06\u6309\u7167\u5728\u53d8\u91cf\u5728\u7c7b\u4e2d\u58f0\u660e\u7684\u987a\u5e8f\u8fdb\u884c\u521d\u59cb\u5316\u3002 \u4e3a\u4e86\u53d6\u5f97\u6700\u4f73\u7684\u6548\u679c\uff0c\u8bf7\u9075\u5faa\u4ee5\u4e0b\u5efa\u8bae\uff1a \u521d\u59cb\u5316\u6210\u5458\u53d8\u91cf\uff0c\u4e0d\u8981\u4f7f\u5b83\u4eec\u4f9d\u8d56\u4e8e\u5176\u4ed6\u6210\u5458\u53d8\u91cf\u7684\u521d\u59cb\u5316(\u6362\u53e5\u8bdd\u8bf4\uff0c\u786e\u4fdd\u5373\u4f7f\u521d\u59cb\u5316\u987a\u5e8f\u4e0d\u540c\uff0c\u4e5f\u80fd\u6b63\u786e\u521d\u59cb\u5316\u6210\u5458\u53d8\u91cf)\u3002 \u6309\u7167\u5728\u7c7b\u4e2d\u58f0\u660e\u53d8\u91cf\u7684\u76f8\u540c\u987a\u5e8f\u7f16\u5199\u521d\u59cb\u5316\u5217\u8868\u3002\u53ea\u8981\u9075\u5faa\u4e86\u524d\u9762\u7684\u5efa\u8bae\uff0c\u8fd9\u4e0d\u662f\u4e25\u683c\u8981\u6c42\u7684\uff0c\u4f46\u662f\u5982\u679c\u4f60\u4e0d\u8fd9\u6837\u505a\uff0c\u5e76\u4e14\u6253\u5f00\u4e86\u6240\u6709\u7684\u8b66\u544a\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u4ea7\u751f\u8b66\u544a\u3002 \u5c0f\u7ed3 \u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u5bf9\u7c7b\u6210\u5458\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u800c\u4e0d\u662f\u4e3a\u5b83\u4eec\u8d4b\u503c\u3002\u8fd9\u662f\u521d\u59cb\u5316\u67d0\u4e9b\u7c7b\u578b\u53d8\u91cf\u7684(\u5982const\u6216reference\u6210\u5458)\u7684\u552f\u4e00\u65b9\u6cd5\uff0c\u800c\u4e14\u5b83\u6bd4\u5728\u6784\u9020\u51fd\u6570\u4f53\u4e2d\u8d4b\u503c\u6027\u80fd\u66f4\u597d\u3002\u6210\u5458\u521d\u59cb\u5316\u5668\u5217\u8868\u65e2\u9002\u7528\u4e8e\u57fa\u672c\u7c7b\u578b\uff0c\u4e5f\u9002\u7528\u4e8e\u4f5c\u4e3a\u7c7b\u672c\u8eab\u7684\u6210\u5458\u3002","title":"13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868"},{"location":"13-6-constructor-member-initializer-lists/#_1","text":"\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cC++ \u63d0\u4f9b\u4e86\u4e00\u79cd\u5bf9\u6210\u5458\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\uff08\u800c\u4e0d\u662f\u5728\u521b\u5efa\u540e\u8d4b\u503c\uff09\u7684\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u3002\u8bf7\u4e0d\u8981\u628a\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u548c\u7528\u4e8e\u5bf9\u6570\u7ec4\u8fdb\u884c\u8d4b\u503c\u7684 \u521d\u59cb\u5316\u503c\u5217\u8868 \u641e\u6df7\u3002 \u5728 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u521d\u59cb\u5316\u53d8\u91cf\u7684\u65b9\u5f0f\u6709\u4e09\u79cd\uff1a \u62f7\u8d1d\u521d\u59cb\u5316 \u3001 \u76f4\u63a5\u521d\u59cb\u5316 \u548c \u7edf\u4e00\u521d\u59cb\u5316 \u3002 1 2 3 int value1 = 1 ; // copy initialization double value2 ( 2.2 ); // direct initialization char value3 { 'c' }; // uniform initialization \u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u8fdb\u884c\u521d\u59cb\u5316\u57fa\u672c\u4e0a\u548c \u76f4\u63a5\u521d\u59cb\u5316 \u6216 \u7edf\u4e00\u521d\u59cb\u5316 \u662f\u5b8c\u5168\u4e00\u81f4\u7684\u3002 \u4e3e\u4e2a\u4f8b\u5b50\u5c31\u66f4\u5bb9\u6613\u7406\u89e3\u4e86\u3002\u8fd8\u8bb0\u5f97\u4e4b\u524d\u4e3a\u53d8\u91cf\u8d4b\u503c\u7684\u6784\u9020\u51fd\u6570\u5417\uff1f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Something { private : int m_value1 {}; double m_value2 {}; char m_value3 {}; public : Something () { // \u90fd\u662f\u8d4b\u503c\uff0c\u800c\u4e0d\u662f\u521d\u59cb\u5316 m_value1 = 1 ; m_value2 = 2.2 ; m_value3 = 'c' ; } }; \u63a5\u4e0b\u6765\uff0c\u91cd\u5199\u51fd\u6570\uff0c\u4f7f\u7528\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u6765\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include class Something { private : int m_value1 {}; double m_value2 {}; char m_value3 {}; public : Something () : m_value1 { 1 }, m_value2 { 2.2 }, m_value3 { 'c' } // Initialize our member variables { // No need for assignment here } void print () { std :: cout << \"Something(\" << m_value1 << \", \" << m_value2 << \", \" << m_value3 << \") \\n \" ; } }; int main () { Something something {}; something . print (); return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Something(1, 2.2, c) \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u4f4d\u4e8e\u6784\u9020\u51fd\u6570\u53c2\u6570\u5217\u8868\u540e\uff0c\u4ee5\u5192\u53f7 : \u5f00\u5934\uff0c\u540e\u9762\u662f\u4e00\u7cfb\u5217\u53d8\u91cf\u548c\u5b83\u4eec\u7684\u521d\u59cb\u5316\u503c\uff08\u4f7f\u7528\u9017\u53f7\u5206\u9694\uff09\u3002 \u6ce8\u610f\uff0c\u51fd\u6570\u4f53\u5185\u4e0d\u518d\u9700\u8981\u5bf9\u53d8\u91cf\u8fdb\u884c\u8d4b\u503c\uff0c\u56e0\u4e3a\u521d\u59cb\u5316\u7684\u5de5\u4f5c\u5df2\u7ecf\u7531\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u5b8c\u6210\u3002\u540c\u65f6\u8fd8\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u540e\u9762\u5e76\u6ca1\u6709\u5206\u53f7\u3002 \u5f53\u7136\uff0c\u5982\u679c\u80fd\u591f\u901a\u8fc7\u6784\u9020\u51fd\u6570\u6765\u4f20\u9012\u521d\u59cb\u5316\u503c\u5219\u4f1a\u66f4\u6709\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include class Something { private : int m_value1 {}; double m_value2 {}; char m_value3 {}; public : Something ( int value1 , double value2 , char value3 = 'c' ) : m_value1 { value1 }, m_value2 { value2 }, m_value3 { value3 } // \u76f4\u63a5\u521d\u59cb\u5316\u6210\u5458\u53d8\u91cf { // No need for assignment here } void print () { std :: cout << \"Something(\" << m_value1 << \", \" << m_value2 << \", \" << m_value3 << \") \\n \" ; } }; int main () { Something something { 1 , 2.2 }; // value1 = 1, value2=2.2, value3 \u4f7f\u7528\u9ed8\u8ba4\u503c 'c' something . print (); return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 Something(1, 2.2, c) \u6ce8\u610f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u9ed8\u8ba4\u5f62\u53c2\u6765\u63d0\u4f9b\u9ed8\u8ba4\u503c\uff0c\u9632\u6b62\u7528\u6237\u6ca1\u6709\u4f20\u9012\u53c2\u6570\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u5bf9\u6210\u5458\u8fdb\u884c\u521d\u59cb\u5316\u800c\u4e0d\u662f\u4f9d\u6b21\u4e3a\u5176\u8d4b\u503c\u3002","title":"\u6210\u5458\u521d\u59cb\u5316\u5217\u8868"},{"location":"13-6-constructor-member-initializer-lists/#const","text":"\u7c7b\u53ef\u4ee5\u5305\u542b const \u7c7b\u578b\u7684\u6210\u5458\u53d8\u91cf\u3002const \u6210\u5458\u53d8\u91cf\u548c\u4e00\u822c\u7684 const \u53d8\u91cf\u6ca1\u4ec0\u4e48\u533a\u522b\u2014\u2014\u5b83\u4eec\u90fd\u5fc5\u987b\u88ab\u521d\u59cb\u5316\uff0c\u521d\u59cb\u5316\u540e\u5176\u503c\u5c06\u4e0d\u80fd\u88ab\u6539\u53d8\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6784\u9020\u51fd\u6570\u7684\u521d\u59cb\u5316\u5217\u8868\u5bf9 const \u7c7b\u578b\u7684\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\uff08\u548c\u975econst\u6210\u5458\u4e00\u6837\uff09\uff0c\u800c\u4e14\u521d\u59cb\u5316\u503c\u53ef\u4ee5\u662f\u5e38\u6570\u4e5f\u53ef\u4ee5\u4e0d\u662f\u3002 \u4e0b\u4f8b\u4e2d\u7684\u7c7b\uff0c\u5305\u542b\u4e00\u4e2a const \u7c7b\u578b\u7684\u53d8\u91cf\u3002\u6211\u4eec\u4f7f\u7528\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\uff08\u4f7f\u7528\u7528\u6237\u63d0\u4f9b\u7684\u975econst\u503c\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include class Something { private : const int m_value ; public : Something ( int x ) : m_value { x } // directly initialize our const member variable { } void print () { std :: cout << \"Something(\" << m_value << \") \\n \" ; } }; int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; Something s { x }; s . print (); return 0 ; } \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\uff1a 1 2 Enter an integer: 4 Something(4) \u6cd5\u5219 Const \u6210\u5458\u53d8\u91cf\u5fc5\u987b\u88ab\u521d\u59cb\u5316\u3002","title":"\u521d\u59cb\u5316 const \u7c7b\u578b\u6210\u5458\u53d8\u91cf"},{"location":"13-6-constructor-member-initializer-lists/#_2","text":"\u4e0b\u9762\u4f8b\u5b50\u4e2d\u7684\u7c7b\u5305\u542b\u4e00\u4e2a\u6570\u7ec4\u7c7b\u578b\u7684\u6210\u5458\uff1a 1 2 3 4 5 6 class Something { private : const int m_array [ 5 ]; }; \u5728 C++11 \u4e4b\u524d\uff0c\u4f60\u53ea\u80fd\u901a\u8fc7\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\uff0c\u5bf9\u6570\u7ec4\u6210\u5458\u8fdb\u884c0\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 class Something { private : const int m_array [ 5 ]; public : Something () : m_array {} // zero initialize the member array { } }; \u4e0d\u8fc7\uff0c\u4eceC++11\u5f00\u59cb\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 \u7edf\u4e00\u521d\u59cb\u5316 \u5bf9\u6570\u7ec4\u6210\u5458\u8fdb\u884c\u521d\u59cb\u5316\u4e86\uff1a 1 2 3 4 5 6 7 8 9 10 11 class Something { private : const int m_array [ 5 ]; public : Something () : m_array { 1 , 2 , 3 , 4 , 5 } // use uniform initialization to initialize our member array { } };","title":"\u4f7f\u7528\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u521d\u59cb\u5316\u6570\u7ec4\u7c7b\u578b\u6210\u5458"},{"location":"13-6-constructor-member-initializer-lists/#_3","text":"\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e5f\u53ef\u7528\u4e8e\u7c7b\u7c7b\u578b\u6210\u5458\u53d8\u91cf\u7684\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include class A { public : A ( int x = 0 ) { std :: cout << \"A \" << x << '\\n' ; } }; class B { private : A m_a {}; public : B ( int y ) : m_a { y - 1 } // call A(int) constructor to initialize member m_a { std :: cout << \"B \" << y << '\\n' ; } }; int main () { B b { 5 }; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 A 4 B 5 \u5f53\u6784\u9020\u53d8\u91cfb\u65f6\uff0c\u7528\u503c5\u8c03\u7528 B(int) \u6784\u9020\u51fd\u6570\u3002\u5728\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u6267\u884c\u4e4b\u524d\uff0c m_a \u88ab\u521d\u59cb\u5316\uff0c\u8c03\u7528\u503c\u4e3a4\u7684 A(int) \u6784\u9020\u51fd\u6570\uff0c\u6253\u5370\u51fa\u201cA 4\u201d\u3002\u7136\u540e\u63a7\u5236\u8fd4\u56de\u5230B\u6784\u9020\u51fd\u6570\uff0c\u6267\u884cB\u6784\u9020\u51fd\u6570\u7684\u4e3b\u4f53\uff0c\u6253\u5370\u201cB 5\u201d\u3002","title":"\u521d\u59cb\u5316\u7c7b\u7c7b\u578b\u7684\u6210\u5458\u53d8\u91cf"},{"location":"13-6-constructor-member-initializer-lists/#_4","text":"C++ \u5141\u8bb8\u7a0b\u5e8f\u5458\u7075\u6d3b\u5730\u6392\u7248\u521d\u59cb\u5316\u503c\u5217\u8868\u3002\u867d\u7136\u4f60\u4e5f\u53ef\u6309\u7167\u81ea\u5df1\u7684\u559c\u597d\u6392\u7248\uff0c\u4f46\u662f\u6211\u4eec\u63a8\u8350\u4ee5\u4e0b\u7684\u65b9\u5f0f\uff1a \u5982\u679c\u51fd\u6570\u540d\u540c\u4e00\u884c\u4e2d\u53ef\u4ee5\u653e\u5f97\u4e0b\u521d\u59cb\u5316\u503c\u5217\u8868\uff0c\u5219\u5c06\u5b83\u4eec\u90fd\u653e\u5728\u8fd9\u4e00\u884c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class Something { private : int m_value1 {}; double m_value2 {}; char m_value3 {}; public : Something () : m_value1 { 1 }, m_value2 { 2.2 }, m_value3 { 'c' } // everything on one line { } }; \u5982\u679c\u653e\u4e0d\u4e0b\uff0c\u5219\u5728\u4e0b\u4e00\u884c\u4e2d\u7f29\u8fdb\u653e\u7f6e\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Something { private : int m_value1 ; double m_value2 ; char m_value3 ; public : Something ( int value1 , double value2 , char value3 = 'c' ) // \u51fd\u6570\u540d\u8fd9\u4e00\u884c\u5df2\u7ecf\u591f\u957f\u4e86 : m_value1 { value1 }, m_value2 { value2 }, m_value3 { value3 } // \u6240\u4ee5\u5c06\u521d\u59cb\u5316\u5217\u8868\u653e\u5728\u4e0b\u4e00\u884c { } }; \u5982\u679c\u8fd9\u6837\u4e5f\u4e0d\u80fd\u5c06\u521d\u59cb\u5316\u503c\u5217\u8868\u653e\u5728\u4e00\u884c\uff0c\u5219\u901a\u8fc7\u7a7a\u683c\u6392\u7248\uff0c\u6bcf\u884c\u4e00\u4e2a\u521d\u59cb\u5316\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class Something { private : int m_value1 {}; double m_value2 {}; char m_value3 {}; float m_value4 {}; public : Something ( int value1 , double value2 , char value3 = 'c' , float value4 = 34.6f ) // this line already has a lot of stuff on it : m_value1 { value1 } // one per line , m_value2 { value2 } , m_value3 { value3 } , m_value4 { value4 } { } };","title":"\u521d\u59cb\u5316\u5217\u8868\u6392\u7248"},{"location":"13-6-constructor-member-initializer-lists/#_5","text":"\u4ee4\u4eba\u60ca\u8bb6\u7684\u662f\uff0c\u521d\u59cb\u5316\u5217\u8868\u4e2d\u7684\u53d8\u91cf\u5e76\u4e0d\u4f1a\u6309\u7167\u5b83\u4eec\u5728\u521d\u59cb\u5316\u5217\u8868\u4e2d\u6307\u5b9a\u7684\u987a\u5e8f\u521d\u59cb\u5316\u3002\u5b9e\u9645\u4e0a\uff0c\u5b83\u4eec\u5c06\u6309\u7167\u5728\u53d8\u91cf\u5728\u7c7b\u4e2d\u58f0\u660e\u7684\u987a\u5e8f\u8fdb\u884c\u521d\u59cb\u5316\u3002 \u4e3a\u4e86\u53d6\u5f97\u6700\u4f73\u7684\u6548\u679c\uff0c\u8bf7\u9075\u5faa\u4ee5\u4e0b\u5efa\u8bae\uff1a \u521d\u59cb\u5316\u6210\u5458\u53d8\u91cf\uff0c\u4e0d\u8981\u4f7f\u5b83\u4eec\u4f9d\u8d56\u4e8e\u5176\u4ed6\u6210\u5458\u53d8\u91cf\u7684\u521d\u59cb\u5316(\u6362\u53e5\u8bdd\u8bf4\uff0c\u786e\u4fdd\u5373\u4f7f\u521d\u59cb\u5316\u987a\u5e8f\u4e0d\u540c\uff0c\u4e5f\u80fd\u6b63\u786e\u521d\u59cb\u5316\u6210\u5458\u53d8\u91cf)\u3002 \u6309\u7167\u5728\u7c7b\u4e2d\u58f0\u660e\u53d8\u91cf\u7684\u76f8\u540c\u987a\u5e8f\u7f16\u5199\u521d\u59cb\u5316\u5217\u8868\u3002\u53ea\u8981\u9075\u5faa\u4e86\u524d\u9762\u7684\u5efa\u8bae\uff0c\u8fd9\u4e0d\u662f\u4e25\u683c\u8981\u6c42\u7684\uff0c\u4f46\u662f\u5982\u679c\u4f60\u4e0d\u8fd9\u6837\u505a\uff0c\u5e76\u4e14\u6253\u5f00\u4e86\u6240\u6709\u7684\u8b66\u544a\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u4ea7\u751f\u8b66\u544a\u3002","title":"\u521d\u59cb\u5316\u5217\u8868\u7684\u987a\u5e8f"},{"location":"13-6-constructor-member-initializer-lists/#_6","text":"\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u5bf9\u7c7b\u6210\u5458\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u800c\u4e0d\u662f\u4e3a\u5b83\u4eec\u8d4b\u503c\u3002\u8fd9\u662f\u521d\u59cb\u5316\u67d0\u4e9b\u7c7b\u578b\u53d8\u91cf\u7684(\u5982const\u6216reference\u6210\u5458)\u7684\u552f\u4e00\u65b9\u6cd5\uff0c\u800c\u4e14\u5b83\u6bd4\u5728\u6784\u9020\u51fd\u6570\u4f53\u4e2d\u8d4b\u503c\u6027\u80fd\u66f4\u597d\u3002\u6210\u5458\u521d\u59cb\u5316\u5668\u5217\u8868\u65e2\u9002\u7528\u4e8e\u57fa\u672c\u7c7b\u578b\uff0c\u4e5f\u9002\u7528\u4e8e\u4f5c\u4e3a\u7c7b\u672c\u8eab\u7684\u6210\u5458\u3002","title":"\u5c0f\u7ed3"},{"location":"13-7-non-static-member-initialization/","tags":["static member","initialization"],"text":"\u5f53\u7f16\u5199\u5177\u6709\u591a\u4e2a\u6784\u9020\u51fd\u6570(\u5373\u5927\u591a\u6570\u6784\u9020\u51fd\u6570)\u7684\u7c7b\u65f6\uff0c\u5728\u6bcf\u4e2a\u6784\u9020\u51fd\u6570\u4e2d\u90fd\u4e3a\u6240\u6709\u6210\u5458\u6307\u5b9a\u9ed8\u8ba4\u503c\u4f1a\u4ea7\u751f\u5197\u4f59\u4ee3\u7801\uff0c\u800c\u4e14\u5982\u679c\u66f4\u65b0\u6210\u5458\u7684\u9ed8\u8ba4\u503c\uff0c\u5219\u9700\u8981\u4fee\u6539\u6240\u6709\u7684\u6784\u9020\u51fd\u6570\u3002 \u53ef\u4ee5\u76f4\u63a5\u7ed9\u666e\u901a\u7684\u7c7b\u6210\u5458\u53d8\u91cf(\u90a3\u4e9b\u4e0d\u4f7f\u7528static\u5173\u952e\u5b57\u7684\u53d8\u91cf)\u4e00\u4e2a\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include class Rectangle { private : double m_length { 1.0 }; // m_length \u5177\u6709\u9ed8\u8ba4\u503c 1.0 double m_width { 1.0 }; // m_width \u5177\u6709\u9ed8\u8ba4\u503c 1.0 public : void print () { std :: cout << \"length: \" << m_length << \", width: \" << m_width << '\\n' ; } }; int main () { Rectangle x {}; // x.m_length = 1.0, x.m_width = 1.0 x . print (); return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\uff1a 1 length: 1.0, width: 1.0 \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316(\u4e5f\u79f0\u4e3a\u7c7b\u5185\u6210\u5458\u521d\u59cb\u5316)\u4e3a\u6210\u5458\u53d8\u91cf\u63d0\u4f9b\u9ed8\u8ba4\u503c\uff0c\u5982\u679c\u6784\u9020\u51fd\u6570\u4e0d\u4e3a\u6210\u5458\u672c\u8eab\u63d0\u4f9b\u521d\u59cb\u5316\u503c(\u901a\u8fc7\u6210\u5458\u521d\u59cb\u5316\u5217\u8868)\uff0c\u5219\u6784\u9020\u51fd\u6570\u5c06\u4f7f\u7528\u8fd9\u4e9b\u9ed8\u8ba4\u503c\u3002 \u4e0d\u8fc7\uff0c\u51b3\u5b9a\u5bf9\u8c61\u521b\u5efa\u7684\u4ecd\u7136\u662f\u6784\u9020\u51fd\u6570\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include class Rectangle { private : double m_length { 1.0 }; double m_width { 1.0 }; public : // \u6ce8\u610f\uff1a\u6ca1\u6709\u9ed8\u8ba4\u6784\u9020\u51fd\u6570 Rectangle ( double length , double width ) : m_length { length }, m_width { width } { // m_length \u548c m_width \u4f1a\u88ab\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\uff08\u4e0d\u4f1a\u4f7f\u7528\u5b83\u4eec\u7684\u9ed8\u8ba4\u503c\uff09 } void print () { std :: cout << \"length: \" << m_length << \", width: \" << m_width << '\\n' ; } }; int main () { Rectangle x {}; // \u65e0\u6cd5\u7f16\u8bd1\uff0c\u56e0\u4e3a\u4e0d\u5b58\u5728\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u5373\u4f7f\u6210\u5458\u5177\u6709\u9ed8\u8ba4\u503c return 0 ; } \u5c3d\u7ba1\u6211\u4eec\u4e3a\u6240\u6709\u6210\u5458\u63d0\u4f9b\u4e86\u9ed8\u8ba4\u503c\uff0c\u4f46\u7531\u4e8e\u6ca1\u6709\u63d0\u4f9b\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u56e0\u6b64\u65e0\u6cd5\u521b\u5efa\u4e0d\u5e26\u53c2\u6570\u7684 Rectangle \u5bf9\u8c61\u3002 \u5982\u679c\u63d0\u4f9b\u4e86\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\uff0c\u4e14\u6784\u9020\u51fd\u6570\u901a\u8fc7\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u521d\u59cb\u5316\u6210\u5458\uff0c\u5219\u4f18\u5148\u4f7f\u7528\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u8bf4\u660e\u4e86\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include class Rectangle { private : double m_length { 1.0 }; double m_width { 1.0 }; public : Rectangle ( double length , double width ) : m_length { length }, m_width { width } { // m_length \u548c m_width \u4f1a\u88ab\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\uff08\u4e0d\u4f1a\u4f7f\u7528\u5b83\u4eec\u7684\u9ed8\u8ba4\u503c\uff09 } Rectangle ( double length ) : m_length { length } { // m_length \u88ab\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316 // m_width \u5219\u4f7f\u7528\u9ed8\u8ba4\u503c 1.0 } void print () { std :: cout << \"length: \" << m_length << \", width: \" << m_width << '\\n' ; } }; int main () { Rectangle x { 2.0 , 3.0 }; x . print (); Rectangle y { 4.0 }; y . print (); return 0 ; } 1 2 length: 2.0, width: 3.0 length: 4.0, width: 1.0 \u6ce8\u610f\uff0c\u4f7f\u7528\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u6765\u521d\u59cb\u5316\u6210\u5458\u9700\u8981\u4f7f\u7528\u7b49\u53f7\u6216\u5927\u62ec\u53f7(\u7edf\u4e00)\u521d\u59cb\u5316\u5f0f\u2014\u2014 \u5706\u62ec\u53f7\u521d\u59cb\u5316\u5f62\u5f0f \u5728\u8fd9\u91cc\u4e0d\u8d77\u4f5c\u7528: 1 2 3 4 5 6 class A { int m_a = 1 ; // ok (\u62f7\u8d1d\u521d\u59cb\u5316) int m_b { 2 }; // ok (\u5927\u62ec\u53f7\u521d\u59cb\u5316) int m_c ( 3 ); // \u65e0\u6548 (\u5706\u62ec\u53f7\u521d\u59cb\u5316) }; COPY \u6cd5\u5219 \u63a8\u8350\u4f7f\u7528\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u6765\u4e3a\u6210\u5458\u53d8\u91cf\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002","title":"13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316"},{"location":"13-8-overlapping-and-delegating-constructors/","tags":["overlapping","delegating","constructor"],"text":"Key Takeaway \u5206\u6e05\u695a\u521d\u59cb\u5316\u548c\u8d4b\u503c \u91cd\u53e0\u6784\u9020\u51fd\u6570 \u5f53\u5b9e\u4f8b\u5316\u4e00\u4e2a\u65b0\u5bf9\u8c61\u65f6\uff0c\u5c06\u9690\u5f0f\u8c03\u7528\u8be5\u5bf9\u8c61\u7684\u6784\u9020\u51fd\u6570\u3002\u5177\u6709\u591a\u4e2a\u5177\u6709\u91cd\u53e0\u529f\u80fd\u7684\u6784\u9020\u51fd\u6570\u7684\u7c7b\u5e76\u4e0d\u7f55\u89c1\u3002\u8003\u8651\u4ee5\u4e0b\u7c7b: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Foo { public : Foo () { // code to do A } Foo ( int value ) { // code to do A // code to do B } }; \u8fd9\u4e2a\u7c7b\u6709\u4e24\u4e2a\u6784\u9020\u51fd\u6570\uff1a\u4e00\u4e2a \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 \u4ee5\u53ca\u4e00\u4e2a\u63a5\u53d7\u4e00\u4e2a int \u7c7b\u578b\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u3002\u56e0\u4e3a \u201ccode to do A\u201d \u8fd9\u4e00\u90e8\u5206\u5728\u4e24\u4e2a\u6784\u9020\u51fd\u6570\u4e2d\u90fd\u5b58\u5728\uff0c\u5219\u53ef\u4ee5\u8bf4\u5b83\u4eec\u5728\u6bcf\u4e2a\u6784\u9020\u51fd\u6570\u4e2d\u662f\u91cd\u590d\u7684\u3002 \u8bfe\u7a0b\u5b66\u5230\u8fd9\u91cc\uff0c\u76f8\u6bd4\u4f60\u5df2\u7ecf\u4e86\u89e3\u5230\uff0c\u8981\u5c3d\u53ef\u80fd\u907f\u514d\u91cd\u590d\u4ee3\u7801\uff0c\u6240\u4ee5\u8ba9\u6211\u4eec\u770b\u770b\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u4e00\u4e9b\u65b9\u6cd5\u3002 \u663e\u800c\u6613\u89c1\u7684\u89e3\u51b3\u65b9\u6848\u5e76\u4e0d\u594f\u6548 \u663e\u800c\u6613\u89c1\u7684\u89e3\u51b3\u65b9\u6848\u662f\u8ba9 Foo(int) \u6784\u9020\u51fd\u6570\u8c03\u7528 Foo() \u6784\u9020\u51fd\u6570\u6765\u5b8c\u6210A\u90e8\u5206\u5de5\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Foo { public : Foo () { // code to do A } Foo ( int value ) { Foo (); // use the above constructor to do A (doesn't work) // code to do B } }; \u4e0d\u8fc7\uff0c\u5982\u679c\u4f60\u50cf\u4e0a\u9762\u8fd9\u6837\u5728\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u91cc\u9762\u8c03\u7528\u53e6\u5916\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u7684\u8bdd\uff0c\u7a0b\u5e8f\u867d\u7136\u53ef\u4ee5\u7f16\u8bd1\uff08\u4e5f\u8bb8\u4f1a\u4ea7\u751f\u544a\u8b66\uff09\uff0c\u4f46\u662f\u7edd\u5bf9\u4e0d\u4f1a\u50cf\u4f60\u60f3\u8c61\u7684\u90a3\u6837\u5de5\u4f5c\uff0c\u7136\u540e\u4f60\u53ef\u80fd\u9700\u8981\u82b1\u8d39\u5f88\u591a\u65f6\u95f4\u53bb\u5b9a\u4f4d\u95ee\u9898\u3002\u5b9e\u9645\u4e0a\uff0c\u6784\u9020\u51fd\u6570 Foo(); \u4f1a\u5b9e\u4f8b\u5316\u4e00\u4e2a\u65b0\u7684 Foo \u5bf9\u8c61\u3002 \u6784\u9020\u51fd\u6570\u59d4\u6d3e \u6784\u9020\u51fd\u6570\u53ef\u4ee5\u8c03\u7528\u6765\u81ea\u540c\u4e00\u7c7b\u7684\u5176\u4ed6\u6784\u9020\u51fd\u6570\uff0c\u79f0\u4e3a \u59d4\u6258\u6784\u9020\u51fd\u6570 (\u6216 \u6784\u9020\u51fd\u6570\u94fe )\u3002 \u8981\u8ba9\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u8c03\u7528\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u53ea\u9700\u5728\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u8c03\u7528\u5b83\u3002\u8fd9\u662f\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u7684\u4e00\u79cd\u60c5\u51b5\u3002\u5e94\u7528\u5230\u6211\u4eec\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Foo { private : public : Foo () { // code to do A } Foo ( int value ) : Foo {} // \u8c03\u7528 Foo() \u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u5b8c\u6210A { // code to do B } }; \u8fd9\u5b8c\u5168\u7b26\u5408\u60a8\u7684\u9884\u671f\u3002\u786e\u4fdd\u4ece\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u8c03\u7528\u6784\u9020\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u5728\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u4e2d\u8c03\u7528\u3002 \u4e0b\u9762\u662f\u4f7f\u7528\u59d4\u6258\u6784\u9020\u51fd\u6570\u51cf\u5c11\u5197\u4f59\u4ee3\u7801\u7684\u53e6\u4e00\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include #include class Employee { private : int m_id {}; std :: string m_name {}; public : Employee ( int id = 0 , std :: string_view name = \"\" ) : m_id { id }, m_name { name } { std :: cout << \"Employee \" << m_name << \" created. \\n \" ; } // Use a delegating constructor to minimize redundant code Employee ( std :: string_view name ) : Employee { 0 , name } { } }; \u8fd9\u4e2a\u7c7b\u6709\u4e24\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u5176\u4e2d\u4e00\u4e2a\u59d4\u6258\u7ed9 Employee(int, std::string_view) \u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5197\u4f59\u4ee3\u7801\u7684\u6570\u91cf\u88ab\u6700\u5c0f\u5316\u4e86(\u6211\u4eec\u53ea\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u4f53\u800c\u4e0d\u662f\u4e24\u4e2a)\u3002 \u5173\u4e8e\u59d4\u6258\u6784\u9020\u51fd\u6570\u7684\u4e00\u4e9b\u989d\u5916\u6ce8\u610f\u4e8b\u9879\u3002\u9996\u5148\uff0c\u59d4\u6258\u7ed9\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u7684\u6784\u9020\u51fd\u6570\u672c\u8eab\u4e0d\u5141\u8bb8\u8fdb\u884c\u4efb\u4f55\u6210\u5458\u521d\u59cb\u5316\u3002 \u6784\u9020\u51fd\u6570\u53ef\u4ee5\u59d4\u6258\u6216\u521d\u59cb\u5316\uff0c\u4f46\u4e0d\u80fd\u540c\u65f6\u59d4\u6258\u548c\u521d\u59cb\u5316\u3002 \u5176\u6b21\uff0c\u5982\u679c\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u59d4\u6258\u7ed9\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u800c\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u53c8\u59d4\u6258\u56de\u7b2c\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u3002\u8fd9\u5c06\u5f62\u6210\u4e00\u4e2a\u6b7b\u5faa\u73af\uff0c\u5e76\u5c06\u5bfc\u81f4\u7684\u7a0b\u5e8f\u8017\u5c3d\u5806\u6808\u7a7a\u95f4\u540e\u5d29\u6e83\u3002\u53ef\u4ee5\u901a\u8fc7\u786e\u4fdd\u6240\u6709\u6784\u9020\u51fd\u6570\u89e3\u6790\u4e3a\u975e\u59d4\u6258\u6784\u9020\u51fd\u6570\u6765\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u60a8\u6709\u591a\u4e2a\u5177\u6709\u76f8\u540c\u529f\u80fd\u7684\u6784\u9020\u51fd\u6570\uff0c\u8bf7\u4f7f\u7528\u59d4\u6258\u6784\u9020\u51fd\u6570\u4ee5\u907f\u514d\u91cd\u590d\u4ee3\u7801\u3002 \u4f7f\u7528\u666e\u901a\u6210\u5458\u51fd\u6570\u8fdb\u884c\u542f\u52a8\u914d\u7f6e \u56e0\u4e3a\u6784\u9020\u51fd\u6570\u53ea\u80fd\u7528\u4f5c\u521d\u59cb\u5316\u6216\u59d4\u6258\uff0c\u6240\u4ee5\u5982\u679c\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u9700\u8981\u6267\u884c\u4e00\u4e9b\u5e38\u89c1\u7684\u521d\u59cb\u5316\uff0c\u5c31\u4f1a\u5f88\u9ebb\u70e6\u3002\u8003\u8651\u4ee5\u4e0b\u7c7b: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Foo { private : const int m_value { 0 }; public : Foo () { // \u7528\u4e8e\u4efb\u52a1\u542f\u52a8\u914d\u7f6e\u7684\u4ee3\u7801(\u4f8b\u5982\u6253\u5f00\u6587\u4ef6\u6216\u6570\u636e\u5e93) } Foo ( int value ) : m_value { value } // \u5fc5\u987b\u521d\u59cb\u5316 const \u6210\u5458 m_value { // \u6b64\u65f6\u5e94\u8be5\u5982\u4f55\u6267\u884cFoo()\u4e2d\u7684\u542f\u52a8\u914d\u7f6e\u4ee3\u7801\u5462\uff1f } }; \u6784\u9020\u51fd\u6570 Foo(int) \u8981\u4e48\u521d\u59cb\u5316 m_value \uff0c\u8981\u4e48\u59d4\u6258\u7ed9 Foo() \u6267\u884c\u5176\u4e2d\u7684\u4ee3\u7801\uff0c\u4f46\u9c7c\u548c\u718a\u638c\u4e0d\u53ef\u517c\u5f97\u3002\u4f46\u662f\u5982\u679c\u4e00\u5b9a\u8981\u517c\u5f97\u5462\uff1f\u5f53\u7136\uff0c\u6700\u7b28\u7684\u7684\u529e\u6cd5\u5c31\u662f\u628a\u9700\u8981\u6267\u884c\u7684\u4ee3\u7801\u590d\u5236\u4e00\u4efd\u8fc7\u6765\u3002\u4f46\u8fd9\u65e0\u7591\u4f1a\u5e26\u6765\u5f88\u591a\u91cd\u590d\u4ee3\u7801\uff0c\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\u3002 \u5176\u5b9e\u6784\u9020\u51fd\u6570\u662f\u53ef\u4ee5\u8c03\u7528\u975e\u6784\u9020\u6210\u5458\u51fd\u6570\uff08\u548c\u975e\u6210\u5458\u51fd\u6570\uff09\u7684\uff0c\u56e0\u6b64\u66f4\u597d\u7684\u529e\u6cd5\u662f\u4f7f\u7528\u4e00\u4e2a\u666e\u901a\u51fd\u6570\u6765\u6267\u884c\u8fd9\u4e9b\u4ee3\u7801\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include class Foo { private : const int m_value { 0 }; void setup () // \u8bbe\u4e3a\u79c1\u6709\uff0c\u53ea\u6709\u6784\u9020\u51fd\u6570\u80fd\u591f\u65f6 { // code to do some common setup tasks (e.g. open a file or database) std :: cout << \"Setting things up... \\n \" ; } public : Foo () { setup (); } Foo ( int value ) : m_value { value } // m_value \u662f const \u7c7b\u578b\u5fc5\u987b\u521d\u59cb\u5316 { setup (); } }; int main () { Foo a ; Foo b { 5 }; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u3002\u6211\u4eec\u5b9a\u4e49 setup() \u6210\u5458\u51fd\u6570\u6765\u6267\u884c\u542f\u52a8\u914d\u7f6e\u76f8\u5173\u7684\u4ee3\u7801\uff0c\u7c7b\u4e2d\u7684\u4e24\u4e2a\u6784\u9020\u51fd\u6570\u90fd\u53ef\u4ee5\u8c03\u7528\u5b83\u3002\u6211\u4eec\u5c06\u8be5\u51fd\u6570\u5b9a\u4e49\u4e3a \u79c1\u6709\u6210\u5458 \uff0c\u6240\u4ee5\u53ea\u6709\u8fd9\u4e2a\u7c7b\u7684\u6210\u5458\u53ef\u4ee5\u8bbf\u95ee\u5b83\u3002 \u5f53\u7136\uff0c setup() \u5e76\u4e0d\u662f\u6784\u9020\u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u4e0d\u80fd\u521d\u59cb\u5316\u6210\u5458\u3002 \u5f53\u6784\u9020\u51fd\u6570\u8c03\u7528 setup() \u65f6\uff0c\u7c7b\u6210\u5458\u4eec\u90fd\u5df2\u7ecf\u88ab\u521b\u5efa\u4e86\uff08\u5982\u679c\u63d0\u4f9b\u4e86\u521d\u59cb\u5316\u503c\u5219\u751a\u81f3\u5df2\u7ecf\u5b8c\u6210\u4e86\u521d\u59cb\u5316\uff09\u3002 setup() \u51fd\u6570\u53ea\u80fd\u4e3a\u6210\u5458\u8d4b\u503c\u6216\u8fdb\u884c\u4e00\u4e9b\u5176\u4ed6\u64cd\u4f5c\uff0c\u4f46\u90fd\u53ea\u80fd\u901a\u8fc7\u4e00\u822c\u7684\u8bed\u53e5\u8fdb\u884c\uff08\u4f8b\u5982\u6253\u5f00\u6587\u4ef6\u6216\u6570\u636e\u5e93\uff09\u3002 setup() \u51fd\u6570\u4e0d\u80fd\u521b\u5efa\u6210\u5458\u7684 \u5de6\u503c\u5f15\u7528 \uff0c\u4e5f\u4e0d\u80fd\u4e3a const \u7c7b\u578b\u7684\u6210\u5458\u8d4b\u503c\uff08\u4e24\u8005\u90fd\u9700\u8981\u901a\u8fc7\u521d\u59cb\u5316\u5b8c\u6210\uff09\uff0c\u5f53\u7136\u4e5f\u4e0d\u80fd\u5411\u4efb\u4f55\u4e0d\u652f\u6301\u8d4b\u503c\u64cd\u4f5c\u7684\u6210\u5458\u8d4b\u503c\u3002 \u91cd\u7f6e\u5bf9\u8c61 \u6709\u65f6\u5019\u4f60\u53ef\u80fd\u4f1a\u60f3\u8981\u7f16\u5199\u4e00\u4e2a\u6210\u5458\u51fd\u6570\uff08\u4f8b\u5982 reset() \uff09\u5c06\u7c7b\u5bf9\u8c61\u91cd\u7f6e\u4e3a\u5176\u521d\u59cb\u72b6\u6001\u3002 \u56e0\u4e3a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u4e3a\u7c7b\u6210\u5458\u521d\u59cb\u5316\u6240\u9700\u7684\u503c\uff0c\u6240\u4ee5\u4f60\u53ef\u80fd\u4f1a\u5c1d\u8bd5\u5728 reset() \u51fd\u6570\u4e2d\u8c03\u7528\u6784\u9020\u51fd\u6570\u4ee5\u8fbe\u5230\u76ee\u7684\u3002\u4e0d\u8fc7\uff0c\u76f4\u63a5\u8c03\u7528\u6784\u9020\u51fd\u6570\u901a\u5e38\u4f1a\u5bfc\u81f4\u975e\u9884\u671f\u7684\u884c\u4e3a\uff0c\u5c31\u50cf\u4e4b\u524d\u5c55\u793a\u7684\u90a3\u6837\uff0c\u5b83\u5e76\u4e0d\u80fd\u6b63\u786e\u7684\u5de5\u4f5c\u3002 reset() \u51fd\u6570\u7684\u4e00\u822c\u5b9e\u73b0\u53ef\u80fd\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include class Foo { private : int m_a { 1 }; int m_b { 2 }; public : Foo () { } Foo ( int a , int b ) : m_a { a }, m_b { b } { } void print () { std :: cout << m_a << ' ' << m_b << '\\n' ; } void reset () { m_a = 1 ; m_b = 2 ; } }; int main () { Foo a { 3 , 4 }; a . reset (); a . print (); return 0 ; } \u867d\u7136\u8fd9\u79cd\u5b9e\u73b0\u53ef\u4ee5\u5de5\u4f5c\uff0c\u4f46\u662f\u8fdd\u53cd\u4e86DRY\u9a8c\u8bc1\uff0c\u56e0\u4e3a\u201c\u9ed8\u8ba4\u201d\u503c\u51fa\u73b0\u5728\u4e86\u4e24\u4e2a\u4f4d\u7f6e\uff1a\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u548c reset() \u51fd\u6570\u4f53\u4e2d\u3002\u4e0d\u8fc7\uff0c\u4e5f\u7684\u786e\u6ca1\u6709\u529e\u6cd5\u80fd\u591f\u8ba9 reset() \u76f4\u63a5\u83b7\u53d6\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u503c\u3002 \u4f46\u662f\uff0c\u5982\u679c\u4e00\u4e2a\u7c7b\u662f \u53ef\u8d4b\u503c\u7684 \uff08\u5373\u53ef\u4ee5\u8bbf\u95ee\u5176\u8d4b\u503c\u64cd\u4f5c\u7b26\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\uff0c\u7136\u540e\u5c06\u65b0\u5bf9\u8c61\u8d4b\u503c\u7ed9\u8001\u5bf9\u8c61\uff0c\u5b9e\u73b0\u91cd\u7f6e\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include class Foo { private : int m_a { 5 }; int m_b { 6 }; public : Foo () { } Foo ( int a , int b ) : m_a { a }, m_b { b } { } void print () { std :: cout << m_a << ' ' << m_b << '\\n' ; } void reset () { // consider this a bit of magic for now * this = Foo (); // create new Foo object, then use assignment to overwrite our implicit object } }; int main () { Foo a { 1 , 2 }; a . reset (); a . print (); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u7684 Foo \u5bf9\u8c61(\u5305\u542b\u9ed8\u8ba4\u521d\u503c)\u3002\u7136\u540e\u6211\u4eec\u5c06\u8fd9\u4e2a\u65b0 Foo \u5bf9\u8c61\u8d4b\u503c\u7ed9\u8c03\u7528 reset() \u6210\u5458\u51fd\u6570\u7684\u5bf9\u8c61 ( *this )\u3002\u6b64\u65f6\u7f16\u8bd1\u5668\u4f1a\u8fdb\u884c \u6210\u5458\u4f9d\u6b21\u62f7\u8d1d \u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 13.10 - \u9690\u85cf\u7684this\u6307\u9488 \u4e2d\u4ecb\u7ecdthis\u6307\u9488\uff0c\u7c7b\u5bf9\u8c61\u7684\u8d4b\u503c\u5219\u4f1a\u5728 14.15 -- Overloading the assignment operator \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570"},{"location":"13-8-overlapping-and-delegating-constructors/#_1","text":"\u5f53\u5b9e\u4f8b\u5316\u4e00\u4e2a\u65b0\u5bf9\u8c61\u65f6\uff0c\u5c06\u9690\u5f0f\u8c03\u7528\u8be5\u5bf9\u8c61\u7684\u6784\u9020\u51fd\u6570\u3002\u5177\u6709\u591a\u4e2a\u5177\u6709\u91cd\u53e0\u529f\u80fd\u7684\u6784\u9020\u51fd\u6570\u7684\u7c7b\u5e76\u4e0d\u7f55\u89c1\u3002\u8003\u8651\u4ee5\u4e0b\u7c7b: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Foo { public : Foo () { // code to do A } Foo ( int value ) { // code to do A // code to do B } }; \u8fd9\u4e2a\u7c7b\u6709\u4e24\u4e2a\u6784\u9020\u51fd\u6570\uff1a\u4e00\u4e2a \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 \u4ee5\u53ca\u4e00\u4e2a\u63a5\u53d7\u4e00\u4e2a int \u7c7b\u578b\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u3002\u56e0\u4e3a \u201ccode to do A\u201d \u8fd9\u4e00\u90e8\u5206\u5728\u4e24\u4e2a\u6784\u9020\u51fd\u6570\u4e2d\u90fd\u5b58\u5728\uff0c\u5219\u53ef\u4ee5\u8bf4\u5b83\u4eec\u5728\u6bcf\u4e2a\u6784\u9020\u51fd\u6570\u4e2d\u662f\u91cd\u590d\u7684\u3002 \u8bfe\u7a0b\u5b66\u5230\u8fd9\u91cc\uff0c\u76f8\u6bd4\u4f60\u5df2\u7ecf\u4e86\u89e3\u5230\uff0c\u8981\u5c3d\u53ef\u80fd\u907f\u514d\u91cd\u590d\u4ee3\u7801\uff0c\u6240\u4ee5\u8ba9\u6211\u4eec\u770b\u770b\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u4e00\u4e9b\u65b9\u6cd5\u3002","title":"\u91cd\u53e0\u6784\u9020\u51fd\u6570"},{"location":"13-8-overlapping-and-delegating-constructors/#_2","text":"\u663e\u800c\u6613\u89c1\u7684\u89e3\u51b3\u65b9\u6848\u662f\u8ba9 Foo(int) \u6784\u9020\u51fd\u6570\u8c03\u7528 Foo() \u6784\u9020\u51fd\u6570\u6765\u5b8c\u6210A\u90e8\u5206\u5de5\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Foo { public : Foo () { // code to do A } Foo ( int value ) { Foo (); // use the above constructor to do A (doesn't work) // code to do B } }; \u4e0d\u8fc7\uff0c\u5982\u679c\u4f60\u50cf\u4e0a\u9762\u8fd9\u6837\u5728\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u91cc\u9762\u8c03\u7528\u53e6\u5916\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u7684\u8bdd\uff0c\u7a0b\u5e8f\u867d\u7136\u53ef\u4ee5\u7f16\u8bd1\uff08\u4e5f\u8bb8\u4f1a\u4ea7\u751f\u544a\u8b66\uff09\uff0c\u4f46\u662f\u7edd\u5bf9\u4e0d\u4f1a\u50cf\u4f60\u60f3\u8c61\u7684\u90a3\u6837\u5de5\u4f5c\uff0c\u7136\u540e\u4f60\u53ef\u80fd\u9700\u8981\u82b1\u8d39\u5f88\u591a\u65f6\u95f4\u53bb\u5b9a\u4f4d\u95ee\u9898\u3002\u5b9e\u9645\u4e0a\uff0c\u6784\u9020\u51fd\u6570 Foo(); \u4f1a\u5b9e\u4f8b\u5316\u4e00\u4e2a\u65b0\u7684 Foo \u5bf9\u8c61\u3002","title":"\u663e\u800c\u6613\u89c1\u7684\u89e3\u51b3\u65b9\u6848\u5e76\u4e0d\u594f\u6548"},{"location":"13-8-overlapping-and-delegating-constructors/#_3","text":"\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u8c03\u7528\u6765\u81ea\u540c\u4e00\u7c7b\u7684\u5176\u4ed6\u6784\u9020\u51fd\u6570\uff0c\u79f0\u4e3a \u59d4\u6258\u6784\u9020\u51fd\u6570 (\u6216 \u6784\u9020\u51fd\u6570\u94fe )\u3002 \u8981\u8ba9\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u8c03\u7528\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u53ea\u9700\u5728\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u8c03\u7528\u5b83\u3002\u8fd9\u662f\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u7684\u4e00\u79cd\u60c5\u51b5\u3002\u5e94\u7528\u5230\u6211\u4eec\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Foo { private : public : Foo () { // code to do A } Foo ( int value ) : Foo {} // \u8c03\u7528 Foo() \u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u5b8c\u6210A { // code to do B } }; \u8fd9\u5b8c\u5168\u7b26\u5408\u60a8\u7684\u9884\u671f\u3002\u786e\u4fdd\u4ece\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u8c03\u7528\u6784\u9020\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u5728\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u4e2d\u8c03\u7528\u3002 \u4e0b\u9762\u662f\u4f7f\u7528\u59d4\u6258\u6784\u9020\u51fd\u6570\u51cf\u5c11\u5197\u4f59\u4ee3\u7801\u7684\u53e6\u4e00\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include #include class Employee { private : int m_id {}; std :: string m_name {}; public : Employee ( int id = 0 , std :: string_view name = \"\" ) : m_id { id }, m_name { name } { std :: cout << \"Employee \" << m_name << \" created. \\n \" ; } // Use a delegating constructor to minimize redundant code Employee ( std :: string_view name ) : Employee { 0 , name } { } }; \u8fd9\u4e2a\u7c7b\u6709\u4e24\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u5176\u4e2d\u4e00\u4e2a\u59d4\u6258\u7ed9 Employee(int, std::string_view) \u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5197\u4f59\u4ee3\u7801\u7684\u6570\u91cf\u88ab\u6700\u5c0f\u5316\u4e86(\u6211\u4eec\u53ea\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u4f53\u800c\u4e0d\u662f\u4e24\u4e2a)\u3002 \u5173\u4e8e\u59d4\u6258\u6784\u9020\u51fd\u6570\u7684\u4e00\u4e9b\u989d\u5916\u6ce8\u610f\u4e8b\u9879\u3002\u9996\u5148\uff0c\u59d4\u6258\u7ed9\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u7684\u6784\u9020\u51fd\u6570\u672c\u8eab\u4e0d\u5141\u8bb8\u8fdb\u884c\u4efb\u4f55\u6210\u5458\u521d\u59cb\u5316\u3002 \u6784\u9020\u51fd\u6570\u53ef\u4ee5\u59d4\u6258\u6216\u521d\u59cb\u5316\uff0c\u4f46\u4e0d\u80fd\u540c\u65f6\u59d4\u6258\u548c\u521d\u59cb\u5316\u3002 \u5176\u6b21\uff0c\u5982\u679c\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u59d4\u6258\u7ed9\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u800c\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u53c8\u59d4\u6258\u56de\u7b2c\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u3002\u8fd9\u5c06\u5f62\u6210\u4e00\u4e2a\u6b7b\u5faa\u73af\uff0c\u5e76\u5c06\u5bfc\u81f4\u7684\u7a0b\u5e8f\u8017\u5c3d\u5806\u6808\u7a7a\u95f4\u540e\u5d29\u6e83\u3002\u53ef\u4ee5\u901a\u8fc7\u786e\u4fdd\u6240\u6709\u6784\u9020\u51fd\u6570\u89e3\u6790\u4e3a\u975e\u59d4\u6258\u6784\u9020\u51fd\u6570\u6765\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u60a8\u6709\u591a\u4e2a\u5177\u6709\u76f8\u540c\u529f\u80fd\u7684\u6784\u9020\u51fd\u6570\uff0c\u8bf7\u4f7f\u7528\u59d4\u6258\u6784\u9020\u51fd\u6570\u4ee5\u907f\u514d\u91cd\u590d\u4ee3\u7801\u3002","title":"\u6784\u9020\u51fd\u6570\u59d4\u6d3e"},{"location":"13-8-overlapping-and-delegating-constructors/#_4","text":"\u56e0\u4e3a\u6784\u9020\u51fd\u6570\u53ea\u80fd\u7528\u4f5c\u521d\u59cb\u5316\u6216\u59d4\u6258\uff0c\u6240\u4ee5\u5982\u679c\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u9700\u8981\u6267\u884c\u4e00\u4e9b\u5e38\u89c1\u7684\u521d\u59cb\u5316\uff0c\u5c31\u4f1a\u5f88\u9ebb\u70e6\u3002\u8003\u8651\u4ee5\u4e0b\u7c7b: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Foo { private : const int m_value { 0 }; public : Foo () { // \u7528\u4e8e\u4efb\u52a1\u542f\u52a8\u914d\u7f6e\u7684\u4ee3\u7801(\u4f8b\u5982\u6253\u5f00\u6587\u4ef6\u6216\u6570\u636e\u5e93) } Foo ( int value ) : m_value { value } // \u5fc5\u987b\u521d\u59cb\u5316 const \u6210\u5458 m_value { // \u6b64\u65f6\u5e94\u8be5\u5982\u4f55\u6267\u884cFoo()\u4e2d\u7684\u542f\u52a8\u914d\u7f6e\u4ee3\u7801\u5462\uff1f } }; \u6784\u9020\u51fd\u6570 Foo(int) \u8981\u4e48\u521d\u59cb\u5316 m_value \uff0c\u8981\u4e48\u59d4\u6258\u7ed9 Foo() \u6267\u884c\u5176\u4e2d\u7684\u4ee3\u7801\uff0c\u4f46\u9c7c\u548c\u718a\u638c\u4e0d\u53ef\u517c\u5f97\u3002\u4f46\u662f\u5982\u679c\u4e00\u5b9a\u8981\u517c\u5f97\u5462\uff1f\u5f53\u7136\uff0c\u6700\u7b28\u7684\u7684\u529e\u6cd5\u5c31\u662f\u628a\u9700\u8981\u6267\u884c\u7684\u4ee3\u7801\u590d\u5236\u4e00\u4efd\u8fc7\u6765\u3002\u4f46\u8fd9\u65e0\u7591\u4f1a\u5e26\u6765\u5f88\u591a\u91cd\u590d\u4ee3\u7801\uff0c\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\u3002 \u5176\u5b9e\u6784\u9020\u51fd\u6570\u662f\u53ef\u4ee5\u8c03\u7528\u975e\u6784\u9020\u6210\u5458\u51fd\u6570\uff08\u548c\u975e\u6210\u5458\u51fd\u6570\uff09\u7684\uff0c\u56e0\u6b64\u66f4\u597d\u7684\u529e\u6cd5\u662f\u4f7f\u7528\u4e00\u4e2a\u666e\u901a\u51fd\u6570\u6765\u6267\u884c\u8fd9\u4e9b\u4ee3\u7801\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include class Foo { private : const int m_value { 0 }; void setup () // \u8bbe\u4e3a\u79c1\u6709\uff0c\u53ea\u6709\u6784\u9020\u51fd\u6570\u80fd\u591f\u65f6 { // code to do some common setup tasks (e.g. open a file or database) std :: cout << \"Setting things up... \\n \" ; } public : Foo () { setup (); } Foo ( int value ) : m_value { value } // m_value \u662f const \u7c7b\u578b\u5fc5\u987b\u521d\u59cb\u5316 { setup (); } }; int main () { Foo a ; Foo b { 5 }; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u3002\u6211\u4eec\u5b9a\u4e49 setup() \u6210\u5458\u51fd\u6570\u6765\u6267\u884c\u542f\u52a8\u914d\u7f6e\u76f8\u5173\u7684\u4ee3\u7801\uff0c\u7c7b\u4e2d\u7684\u4e24\u4e2a\u6784\u9020\u51fd\u6570\u90fd\u53ef\u4ee5\u8c03\u7528\u5b83\u3002\u6211\u4eec\u5c06\u8be5\u51fd\u6570\u5b9a\u4e49\u4e3a \u79c1\u6709\u6210\u5458 \uff0c\u6240\u4ee5\u53ea\u6709\u8fd9\u4e2a\u7c7b\u7684\u6210\u5458\u53ef\u4ee5\u8bbf\u95ee\u5b83\u3002 \u5f53\u7136\uff0c setup() \u5e76\u4e0d\u662f\u6784\u9020\u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u4e0d\u80fd\u521d\u59cb\u5316\u6210\u5458\u3002 \u5f53\u6784\u9020\u51fd\u6570\u8c03\u7528 setup() \u65f6\uff0c\u7c7b\u6210\u5458\u4eec\u90fd\u5df2\u7ecf\u88ab\u521b\u5efa\u4e86\uff08\u5982\u679c\u63d0\u4f9b\u4e86\u521d\u59cb\u5316\u503c\u5219\u751a\u81f3\u5df2\u7ecf\u5b8c\u6210\u4e86\u521d\u59cb\u5316\uff09\u3002 setup() \u51fd\u6570\u53ea\u80fd\u4e3a\u6210\u5458\u8d4b\u503c\u6216\u8fdb\u884c\u4e00\u4e9b\u5176\u4ed6\u64cd\u4f5c\uff0c\u4f46\u90fd\u53ea\u80fd\u901a\u8fc7\u4e00\u822c\u7684\u8bed\u53e5\u8fdb\u884c\uff08\u4f8b\u5982\u6253\u5f00\u6587\u4ef6\u6216\u6570\u636e\u5e93\uff09\u3002 setup() \u51fd\u6570\u4e0d\u80fd\u521b\u5efa\u6210\u5458\u7684 \u5de6\u503c\u5f15\u7528 \uff0c\u4e5f\u4e0d\u80fd\u4e3a const \u7c7b\u578b\u7684\u6210\u5458\u8d4b\u503c\uff08\u4e24\u8005\u90fd\u9700\u8981\u901a\u8fc7\u521d\u59cb\u5316\u5b8c\u6210\uff09\uff0c\u5f53\u7136\u4e5f\u4e0d\u80fd\u5411\u4efb\u4f55\u4e0d\u652f\u6301\u8d4b\u503c\u64cd\u4f5c\u7684\u6210\u5458\u8d4b\u503c\u3002","title":"\u4f7f\u7528\u666e\u901a\u6210\u5458\u51fd\u6570\u8fdb\u884c\u542f\u52a8\u914d\u7f6e"},{"location":"13-8-overlapping-and-delegating-constructors/#_5","text":"\u6709\u65f6\u5019\u4f60\u53ef\u80fd\u4f1a\u60f3\u8981\u7f16\u5199\u4e00\u4e2a\u6210\u5458\u51fd\u6570\uff08\u4f8b\u5982 reset() \uff09\u5c06\u7c7b\u5bf9\u8c61\u91cd\u7f6e\u4e3a\u5176\u521d\u59cb\u72b6\u6001\u3002 \u56e0\u4e3a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u4e3a\u7c7b\u6210\u5458\u521d\u59cb\u5316\u6240\u9700\u7684\u503c\uff0c\u6240\u4ee5\u4f60\u53ef\u80fd\u4f1a\u5c1d\u8bd5\u5728 reset() \u51fd\u6570\u4e2d\u8c03\u7528\u6784\u9020\u51fd\u6570\u4ee5\u8fbe\u5230\u76ee\u7684\u3002\u4e0d\u8fc7\uff0c\u76f4\u63a5\u8c03\u7528\u6784\u9020\u51fd\u6570\u901a\u5e38\u4f1a\u5bfc\u81f4\u975e\u9884\u671f\u7684\u884c\u4e3a\uff0c\u5c31\u50cf\u4e4b\u524d\u5c55\u793a\u7684\u90a3\u6837\uff0c\u5b83\u5e76\u4e0d\u80fd\u6b63\u786e\u7684\u5de5\u4f5c\u3002 reset() \u51fd\u6570\u7684\u4e00\u822c\u5b9e\u73b0\u53ef\u80fd\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include class Foo { private : int m_a { 1 }; int m_b { 2 }; public : Foo () { } Foo ( int a , int b ) : m_a { a }, m_b { b } { } void print () { std :: cout << m_a << ' ' << m_b << '\\n' ; } void reset () { m_a = 1 ; m_b = 2 ; } }; int main () { Foo a { 3 , 4 }; a . reset (); a . print (); return 0 ; } \u867d\u7136\u8fd9\u79cd\u5b9e\u73b0\u53ef\u4ee5\u5de5\u4f5c\uff0c\u4f46\u662f\u8fdd\u53cd\u4e86DRY\u9a8c\u8bc1\uff0c\u56e0\u4e3a\u201c\u9ed8\u8ba4\u201d\u503c\u51fa\u73b0\u5728\u4e86\u4e24\u4e2a\u4f4d\u7f6e\uff1a\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u548c reset() \u51fd\u6570\u4f53\u4e2d\u3002\u4e0d\u8fc7\uff0c\u4e5f\u7684\u786e\u6ca1\u6709\u529e\u6cd5\u80fd\u591f\u8ba9 reset() \u76f4\u63a5\u83b7\u53d6\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316\u503c\u3002 \u4f46\u662f\uff0c\u5982\u679c\u4e00\u4e2a\u7c7b\u662f \u53ef\u8d4b\u503c\u7684 \uff08\u5373\u53ef\u4ee5\u8bbf\u95ee\u5176\u8d4b\u503c\u64cd\u4f5c\u7b26\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\uff0c\u7136\u540e\u5c06\u65b0\u5bf9\u8c61\u8d4b\u503c\u7ed9\u8001\u5bf9\u8c61\uff0c\u5b9e\u73b0\u91cd\u7f6e\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include class Foo { private : int m_a { 5 }; int m_b { 6 }; public : Foo () { } Foo ( int a , int b ) : m_a { a }, m_b { b } { } void print () { std :: cout << m_a << ' ' << m_b << '\\n' ; } void reset () { // consider this a bit of magic for now * this = Foo (); // create new Foo object, then use assignment to overwrite our implicit object } }; int main () { Foo a { 1 , 2 }; a . reset (); a . print (); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u7684 Foo \u5bf9\u8c61(\u5305\u542b\u9ed8\u8ba4\u521d\u503c)\u3002\u7136\u540e\u6211\u4eec\u5c06\u8fd9\u4e2a\u65b0 Foo \u5bf9\u8c61\u8d4b\u503c\u7ed9\u8c03\u7528 reset() \u6210\u5458\u51fd\u6570\u7684\u5bf9\u8c61 ( *this )\u3002\u6b64\u65f6\u7f16\u8bd1\u5668\u4f1a\u8fdb\u884c \u6210\u5458\u4f9d\u6b21\u62f7\u8d1d \u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 13.10 - \u9690\u85cf\u7684this\u6307\u9488 \u4e2d\u4ecb\u7ecdthis\u6307\u9488\uff0c\u7c7b\u5bf9\u8c61\u7684\u8d4b\u503c\u5219\u4f1a\u5728 14.15 -- Overloading the assignment operator \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"\u91cd\u7f6e\u5bf9\u8c61"},{"location":"13-9-destructors/","tags":["destructors"],"text":"\u6790\u6784\u51fd\u6570 \u662f\u53e6\u5916\u4e00\u79cd\u96f7\u53d4\u7684\u7c7b\u6210\u5458\u51fd\u6570\uff0c\u5b83\u4f1a\u5728\u5bf9\u8c61\u9500\u6bc1\u65f6\u81ea\u52a8\u6267\u884c\u3002\u6784\u9020\u51fd\u6570\u51fd\u6570\u7528\u4e8e\u521d\u59cb\u5316\u5bf9\u8c61\uff0c\u800c\u6790\u6784\u51fd\u6570\u5219\u7528\u4e8e\u5bf9\u5bf9\u8c61\u8fdb\u884c\u201c\u6e05\u7406\u201d\u3002 \u5f53\u5bf9\u8c61\u6b63\u5e38\u5730 \u79bb\u5f00\u4f5c\u7528\u57df \u65f6\uff0c\u6216\u8005\u4f7f\u7528 delete \u5173\u952e\u5b57\u663e\u5f0f\u5730\u5220\u9664\u52a8\u6001\u5206\u914d\u7684\u5bf9\u8c61\u65f6\uff0c\u6790\u6784\u51fd\u6570(\u5982\u679c\u5b83\u5b58\u5728)\u5c31\u4f1a\u88ab\u81ea\u52a8\u8c03\u7528\uff0c\u5728\u91ca\u653e\u5185\u5b58\u524d\u6267\u884c\u5fc5\u8981\u7684\u6e05\u7406\u3002\u5bf9\u4e8e\u7b80\u5355\u7c7b(\u90a3\u4e9b\u53ea\u521d\u59cb\u5316\u666e\u901a\u6210\u5458\u53d8\u91cf\u503c\u7684\u7c7b)\uff0c\u4e0d\u9700\u8981\u6790\u6784\u51fd\u6570\uff0c\u56e0\u4e3aC++\u4f1a\u81ea\u52a8\u4e3a\u4f60\u6e05\u7406\u5185\u5b58\u3002 \u7136\u800c\uff0c\u5982\u679c\u4f60\u7684\u7c7b\u5bf9\u8c61\u6301\u6709\u67d0\u4e9b\u8d44\u6e90(\u4f8b\u5982\u52a8\u6001\u5185\u5b58\u3001\u6587\u4ef6\u6216\u6570\u636e\u5e93\u53e5\u67c4)\uff0c\u6216\u8005\u4f60\u9700\u8981\u5728\u5bf9\u8c61\u88ab\u9500\u6bc1\u4e4b\u524d\u8fdb\u884c\u67d0\u4e9b\u201c\u7ef4\u62a4\u201d\u64cd\u4f5c\uff0c\u6790\u6784\u51fd\u6570\u662f\u8fdb\u884c\u8fd9\u4e9b\u64cd\u4f5c\u7684\u597d\u5730\u65b9\uff0c\u56e0\u4e3a\u6790\u6784\u51fd\u6570\u7684\u8c03\u7528\u901a\u5e38\u662f\u5bf9\u8c61\u88ab\u9500\u6bc1\u4e4b\u524d\u53d1\u751f\u7684\u6700\u540e\u4e00\u4ef6\u4e8b\u3002 \u6790\u6784\u51fd\u6570\u547d\u540d \u548c\u6784\u9020\u51fd\u6570\u4e00\u6837\uff0c\u6790\u6784\u51fd\u6570\u4e5f\u6709\u5176\u7279\u6b8a\u7684\u547d\u540d\u89c4\u5219\uff1a \u6790\u6784\u51fd\u6570\u7684\u540d\u5fc5\u987b\u548c\u7c7b\u540d\u5b8c\u5168\u4e00\u81f4\uff0c\u5e76\u4e14\u5728\u6700\u524d\u9762\u6dfb\u52a0\u6ce2\u6d6a\u53f7(~)\uff1b \u6790\u6784\u51fd\u6570\u6ca1\u6709\u4efb\u4f55\u53c2\u6570\uff1b \u6790\u6784\u51fd\u6570\u4e0d\u8fd4\u56de\u4efb\u4f55\u7c7b\u578b\u3002 \u4e00\u4e2a\u7c7b\u53ea\u80fd\u6709\u4e00\u4e2a\u6790\u6784\u51fd\u6570\u3002 \u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6790\u6784\u51fd\u6570\u4e0d\u5e94\u8be5\u88ab\u663e\u5f0f\u5730\u8c03\u7528(\u56e0\u4e3a\u5f53\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\u4f1a\u81ea\u52a8\u8c03\u7528\u6790\u6784\u51fd\u6570)\uff0c\u56e0\u4e3a\u5f88\u5c11\u6709\u60c5\u51b5\u9700\u8981\u591a\u6b21\u6e05\u7406\u5bf9\u8c61\u3002\u4e0d\u8fc7\uff0c\u6790\u6784\u51fd\u6570\u53ef\u4ee5\u5b89\u5168\u5730\u8c03\u7528\u5176\u4ed6\u6210\u5458\u51fd\u6570\uff0c\u56e0\u4e3a\u76f4\u5230\u6790\u6784\u51fd\u6570\u6267\u884c\u4e4b\u540e\u624d\u5bf9\u8c61\u624d\u4f1a\u88ab\u9500\u6bc1\u3002 \u4e00\u4e2a\u6790\u6784\u51fd\u6570\u6848\u4f8b \u8ba9\u6211\u4eec\u6765\u770b\u770b\u4e00\u4e2a\u4f7f\u7528\u6790\u6784\u51fd\u6570\u7684\u7b80\u5355\u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include #include #include class IntArray { private : int * m_array {}; int m_length {}; public : IntArray ( int length ) // \u6784\u9020\u51fd\u6570 { assert ( length > 0 ); m_array = new int [ static_cast < std :: size_t > ( length )]{}; m_length = length ; } ~ IntArray () // \u6790\u6784\u51fd\u6570 { // \u5220\u9664\u4e4b\u524d\u5206\u914d\u7684\u6570\u7ec4 delete [] m_array ; } void setValue ( int index , int value ) { m_array [ index ] = value ; } int getValue ( int index ) { return m_array [ index ]; } int getLength () { return m_length ; } }; int main () { IntArray ar ( 10 ); // \u5206\u914d 10 \u4e2a\u6574\u578b for ( int count { 0 }; count < ar . getLength (); ++ count ) ar . setValue ( count , count + 1 ); std :: cout << \"The value of element 5 is: \" << ar . getValue ( 5 ) << '\\n' ; return 0 ; } // ar \u5728\u6b64\u5904\u88ab\u9500\u6bc1\uff0c\u6240\u4ee5\u6790\u6784\u51fd\u6570 ~IntArray() \u4f1a\u5728\u6b64\u65f6\u88ab\u8c03\u7528 \u5c0f\u8d34\u58eb \u5982\u679c\u4f60\u5728\u7f16\u8bd1\u4e0a\u9762\u7a0b\u5e8f\u65f6\u4ea7\u751f\u5982\u4e0b\u62a5\u9519\uff1a 1 2 3 error: 'class IntArray' has pointer data members [-Werror=effc++]| error: but does not override 'IntArray(const IntArray&)' [-Werror=effc++]| error: or 'operator=(const IntArray&)' [-Werror=effc++]| \u6b64\u65f6\u53ef\u4ee5\u5220\u9664\u7f16\u8bd1\u5668\u9009\u9879 \u201c-Weffc++\u201d\uff0c\u6216\u8005\u5728\u7c7b\u4e2d\u989d\u5916\u6dfb\u52a0\u4e0b\u9762\u4e24\u884c\u4ee3\u7801\uff1a 1 2 IntArray ( const IntArray & ) = delete ; IntArray & operator = ( const IntArray & ) = delete ; \u8be6\u7ec6\u4fe1\u606f\u53c2\u8003 14.14 -- Converting constructors, explicit, and delete \u8f93\u51fa\u7ed3\u679c\uff1a 1 The value of element 5 is: 6 \u5728 main() \u51fd\u6570\u7684\u7b2c\u4e00\u884c\uff0c\u6211\u4eec\u5b9e\u4f8b\u5316\u4e86\u4e00\u4e2a IntArray \u7c7b\u578b\u7684\u5bf9\u8c61 ar \uff0c\u5e76\u4e14\u4f20\u5165\u4e8610\u4f5c\u4e3a\u957f\u5ea6\u3002 \u6b64\u65f6\u4f1a\u8c03\u7528\u6784\u9020\u51fd\u6570\uff0c\u52a8\u6001\u5206\u914d\u6570\u7ec4\u6240\u9700\u7684\u5185\u5b58\u3002\u5185\u5b58\u5fc5\u987b\u52a8\u6001\u5206\u914d\u56e0\u4e3a\u5728 \u7f16\u8bd1\u65f6 \u65e0\u6cd5\u77e5\u9053\u6570\u7ec4\u7684\u957f\u5ea6\u3002 \u5728 main() \u672b\u5c3e\uff0c ar \u79bb\u5f00\u4f5c\u7528\u57df \uff0c\u8c03\u7528\u6790\u6784\u51fd\u6570 ~IntArray() \uff0c\u5220\u9664\u6784\u9020\u51fd\u6570\u4e2d\u4e3a\u6570\u7ec4\u7533\u8bf7\u7684\u5185\u5b58\u3002 \u63d0\u9192 \u5728 11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 \u4e2d\uff0c\u6211\u4eec\u8bb2\u8fc7\uff0c\u62ec\u53f7\u521d\u59cb\u5316\u5e94\u8be5\u88ab\u7528\u4e8e\u521d\u59cb\u5316\u57fa\u4e8e\u957f\u5ea6\u521b\u5efa\u7684\u6570\u7ec4\u3001\u5bb9\u5668\u548c\u5217\u8868\uff08\u76f8\u5bf9\u4e8e\u57fa\u4e8e\u4e00\u7ec4\u5143\u7d20\u521b\u5efa\uff09\u3002\u56e0\u6b64\uff0c\u521d\u59cb\u5316 IntArray \u65f6\u4f7f\u7528\u4e86 IntArray ar ( 10 ); \u3002 \u6784\u9020\u548c\u6790\u6784\u7684\u65f6\u673a \u548c\u4e4b\u524d\u63d0\u5230\u7684\u4e00\u6837\uff0c\u6784\u9020\u51fd\u6570\u4f1a\u5728\u5bf9\u8c61\u521b\u5efa\u65f6\u8c03\u7528\uff0c\u800c\u6790\u6784\u51fd\u6570\u5219\u662f\u5728\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\u8c03\u7528\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f1a\u5728\u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570\u4e2d\u6dfb\u52a0\u6253\u5370\u6765\u5c55\u793a\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include class Simple { private : int m_nID {}; public : Simple ( int nID ) : m_nID { nID } { std :: cout << \"Constructing Simple \" << nID << '\\n' ; } ~ Simple () { std :: cout << \"Destructing Simple\" << m_nID << '\\n' ; } int getID () { return m_nID ; } }; int main () { // \u5728\u6808\u4e0a\u5206\u914d\u4e00\u4e2a Simple \u5bf9\u8c61 Simple simple { 1 }; std :: cout << simple . getID () << '\\n' ; // \u52a8\u6001\u5206\u914d\u4e00\u4e2a Simple \u5bf9\u8c61 Simple * pSimple { new Simple { 2 } }; std :: cout << pSimple -> getID () << '\\n' ; // \u52a8\u6001\u5206\u914d\u7684 pSimple \u5fc5\u987b\u8981\u88abdelete delete pSimple ; return 0 ; } // simple \u5bf9\u8c61\u4f1a\u5728\u6b64\u5904\u79bb\u5f00\u4f5c\u7528\u57df \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 Constructing Simple 1 1 Constructing Simple 2 2 Destructing Simple 2 Destructing Simple 1 \u6ce8\u610f\uff0c\u201cSimple 1\u201d \u662f\u5728 \u201cSimple 2\u201d \u4e4b\u540e\u9500\u6bc1\u7684\uff0c\u56e0\u4e3a\u6211\u4eec\u5728 main \u51fd\u6570\u7ed3\u675f\u524d\u5220\u9664\u4e86 pSimple \uff0c\u800c simple \u5219\u662f\u5728 main() \u7ed3\u675f\u65f6\u624d\u9500\u6bc1\u3002 \u5168\u5c40\u53d8\u91cf\u4f1a\u5728main()\u51fd\u6570\u524d\u6784\u9020\u800c\u5728main()\u7ed3\u675f\u540e\u9500\u6bc1 \u3002 RAII \u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09 \u662f\u4e00\u79cd\u5c06\u8d44\u6e90\u4f7f\u7528\u4e0e\u5177\u6709\u81ea\u52a8\u6301\u7eed\u65f6\u95f4\u7684\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f(\u4f8b\u5982\uff0c\u975e\u52a8\u6001\u5206\u914d\u7684\u5bf9\u8c61)\u7ed1\u5b9a\u5728\u4e00\u8d77\u7684\u7f16\u7a0b\u6280\u672f\u3002\u5728C++\u4e2d\uff0cRAII\u662f\u901a\u8fc7\u5e26\u6709\u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570\u7684\u7c7b\u5b9e\u73b0\u7684\u3002\u8d44\u6e90(\u5982\u5185\u5b58\u3001\u6587\u4ef6\u6216\u6570\u636e\u5e93\u53e5\u67c4\u7b49)\u901a\u5e38\u662f\u5728\u5bf9\u8c61\u7684\u6784\u9020\u51fd\u6570\u4e2d\u83b7\u53d6\u7684(\u4e0d\u8fc7\uff0c\u5982\u679c\u9700\u8981\u7684\u8bdd\u4e5f\u53ef\u4ee5\u5728\u5bf9\u8c61\u521b\u5efa\u4e4b\u540e\u83b7\u53d6)\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u5728\u5bf9\u8c61\u5904\u4e8e\u6d3b\u52a8\u72b6\u6001\u65f6\u4f7f\u7528\u8be5\u8d44\u6e90\u3002\u5f53\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\uff0c\u8d44\u6e90\u5728\u6790\u6784\u51fd\u6570\u4e2d\u88ab\u91ca\u653e\u3002RAII\u7684\u4e3b\u8981\u4f18\u70b9\u662f\u5b83\u6709\u52a9\u4e8e\u9632\u6b62\u8d44\u6e90\u6cc4\u6f0f(\u4f8b\u5982\uff0c\u5185\u5b58\u6ca1\u6709\u88ab\u91ca\u653e)\uff0c\u56e0\u4e3a\u6240\u6709\u8d44\u6e90\u6301\u6709\u5bf9\u8c61\u90fd\u4f1a\u81ea\u52a8\u6e05\u7406\u3002 \u672c\u8282\u8bfe\u5f00\u59cb\u65f6\u7684 IntArray \u7c7b\u662f\u5b9e\u73b0RAII\u7684\u4e00\u4e2a\u4f8b\u5b50\u2014\u2014\u5728\u6784\u9020\u51fd\u6570\u4e2d\u5206\u914d\uff0c\u5728\u6790\u6784\u51fd\u6570\u4e2d\u91ca\u653e\u3002 std::string \u548c std::vector \u5219\u662f\u6807\u51c6\u5e93\u4e2d\u5b9e\u73b0RAII\u7684\u4f8b\u5b50\u2014\u2014\u52a8\u6001\u5185\u5b58\u5728\u521d\u59cb\u5316\u65f6\u83b7\u5f97\uff0c\u5728\u9500\u6bc1\u65f6\u81ea\u52a8\u6e05\u9664\u3002 \u6709\u5173 exit() \u51fd\u6570\u7684\u8b66\u793a \u6ce8\u610f\uff1a\u5f53\u4f7f\u7528 exit() \u51fd\u6570\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u5728\u4e0d\u8c03\u7528\u4efb\u4f55\u6784\u9020\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\u9000\u51fa\u3002\u5982\u679c\u4f60\u4f9d\u8d56\u4e8e\u6784\u9020\u51fd\u6570\u5728\u7a0b\u5e8f\u9000\u51fa\u65f6\u5b8c\u6210\u6e05\u7406\u5de5\u4f5c\uff08\u4f8b\u5982\uff0c\u5199\u65e5\u5fd7\u6216\u5199\u6570\u636e\u5e93\uff09\u3002 \u5c0f\u7ed3 \u5982\u4f60\u6240\u89c1\uff0c\u5f53\u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570\u88ab\u5b9e\u73b0\u540e\uff0c\u7c7b\u5c31\u53ef\u4ee5\u5728\u521d\u59cb\u5316\u81ea\u5df1\u5e76\u5728\u5176\u9500\u6bc1\u65f6\u5b8c\u6210\u6e05\u7406\u5de5\u4f5c\uff0c\u4e0d\u9700\u8981\u7a0b\u5e8f\u5458\u505a\u4efb\u4f55\u7279\u6b8a\u7684\u5de5\u4f5c\uff01\u8fd9\u964d\u4f4e\u4e86\u51fa\u9519\u7684\u53ef\u80fd\u6027\uff0c\u5e76\u4f7f\u7c7b\u66f4\u5bb9\u6613\u4f7f\u7528\u3002","title":"13.9 - \u6790\u6784\u51fd\u6570"},{"location":"13-9-destructors/#_1","text":"\u548c\u6784\u9020\u51fd\u6570\u4e00\u6837\uff0c\u6790\u6784\u51fd\u6570\u4e5f\u6709\u5176\u7279\u6b8a\u7684\u547d\u540d\u89c4\u5219\uff1a \u6790\u6784\u51fd\u6570\u7684\u540d\u5fc5\u987b\u548c\u7c7b\u540d\u5b8c\u5168\u4e00\u81f4\uff0c\u5e76\u4e14\u5728\u6700\u524d\u9762\u6dfb\u52a0\u6ce2\u6d6a\u53f7(~)\uff1b \u6790\u6784\u51fd\u6570\u6ca1\u6709\u4efb\u4f55\u53c2\u6570\uff1b \u6790\u6784\u51fd\u6570\u4e0d\u8fd4\u56de\u4efb\u4f55\u7c7b\u578b\u3002 \u4e00\u4e2a\u7c7b\u53ea\u80fd\u6709\u4e00\u4e2a\u6790\u6784\u51fd\u6570\u3002 \u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6790\u6784\u51fd\u6570\u4e0d\u5e94\u8be5\u88ab\u663e\u5f0f\u5730\u8c03\u7528(\u56e0\u4e3a\u5f53\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\u4f1a\u81ea\u52a8\u8c03\u7528\u6790\u6784\u51fd\u6570)\uff0c\u56e0\u4e3a\u5f88\u5c11\u6709\u60c5\u51b5\u9700\u8981\u591a\u6b21\u6e05\u7406\u5bf9\u8c61\u3002\u4e0d\u8fc7\uff0c\u6790\u6784\u51fd\u6570\u53ef\u4ee5\u5b89\u5168\u5730\u8c03\u7528\u5176\u4ed6\u6210\u5458\u51fd\u6570\uff0c\u56e0\u4e3a\u76f4\u5230\u6790\u6784\u51fd\u6570\u6267\u884c\u4e4b\u540e\u624d\u5bf9\u8c61\u624d\u4f1a\u88ab\u9500\u6bc1\u3002","title":"\u6790\u6784\u51fd\u6570\u547d\u540d"},{"location":"13-9-destructors/#_2","text":"\u8ba9\u6211\u4eec\u6765\u770b\u770b\u4e00\u4e2a\u4f7f\u7528\u6790\u6784\u51fd\u6570\u7684\u7b80\u5355\u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include #include #include class IntArray { private : int * m_array {}; int m_length {}; public : IntArray ( int length ) // \u6784\u9020\u51fd\u6570 { assert ( length > 0 ); m_array = new int [ static_cast < std :: size_t > ( length )]{}; m_length = length ; } ~ IntArray () // \u6790\u6784\u51fd\u6570 { // \u5220\u9664\u4e4b\u524d\u5206\u914d\u7684\u6570\u7ec4 delete [] m_array ; } void setValue ( int index , int value ) { m_array [ index ] = value ; } int getValue ( int index ) { return m_array [ index ]; } int getLength () { return m_length ; } }; int main () { IntArray ar ( 10 ); // \u5206\u914d 10 \u4e2a\u6574\u578b for ( int count { 0 }; count < ar . getLength (); ++ count ) ar . setValue ( count , count + 1 ); std :: cout << \"The value of element 5 is: \" << ar . getValue ( 5 ) << '\\n' ; return 0 ; } // ar \u5728\u6b64\u5904\u88ab\u9500\u6bc1\uff0c\u6240\u4ee5\u6790\u6784\u51fd\u6570 ~IntArray() \u4f1a\u5728\u6b64\u65f6\u88ab\u8c03\u7528 \u5c0f\u8d34\u58eb \u5982\u679c\u4f60\u5728\u7f16\u8bd1\u4e0a\u9762\u7a0b\u5e8f\u65f6\u4ea7\u751f\u5982\u4e0b\u62a5\u9519\uff1a 1 2 3 error: 'class IntArray' has pointer data members [-Werror=effc++]| error: but does not override 'IntArray(const IntArray&)' [-Werror=effc++]| error: or 'operator=(const IntArray&)' [-Werror=effc++]| \u6b64\u65f6\u53ef\u4ee5\u5220\u9664\u7f16\u8bd1\u5668\u9009\u9879 \u201c-Weffc++\u201d\uff0c\u6216\u8005\u5728\u7c7b\u4e2d\u989d\u5916\u6dfb\u52a0\u4e0b\u9762\u4e24\u884c\u4ee3\u7801\uff1a 1 2 IntArray ( const IntArray & ) = delete ; IntArray & operator = ( const IntArray & ) = delete ; \u8be6\u7ec6\u4fe1\u606f\u53c2\u8003 14.14 -- Converting constructors, explicit, and delete \u8f93\u51fa\u7ed3\u679c\uff1a 1 The value of element 5 is: 6 \u5728 main() \u51fd\u6570\u7684\u7b2c\u4e00\u884c\uff0c\u6211\u4eec\u5b9e\u4f8b\u5316\u4e86\u4e00\u4e2a IntArray \u7c7b\u578b\u7684\u5bf9\u8c61 ar \uff0c\u5e76\u4e14\u4f20\u5165\u4e8610\u4f5c\u4e3a\u957f\u5ea6\u3002 \u6b64\u65f6\u4f1a\u8c03\u7528\u6784\u9020\u51fd\u6570\uff0c\u52a8\u6001\u5206\u914d\u6570\u7ec4\u6240\u9700\u7684\u5185\u5b58\u3002\u5185\u5b58\u5fc5\u987b\u52a8\u6001\u5206\u914d\u56e0\u4e3a\u5728 \u7f16\u8bd1\u65f6 \u65e0\u6cd5\u77e5\u9053\u6570\u7ec4\u7684\u957f\u5ea6\u3002 \u5728 main() \u672b\u5c3e\uff0c ar \u79bb\u5f00\u4f5c\u7528\u57df \uff0c\u8c03\u7528\u6790\u6784\u51fd\u6570 ~IntArray() \uff0c\u5220\u9664\u6784\u9020\u51fd\u6570\u4e2d\u4e3a\u6570\u7ec4\u7533\u8bf7\u7684\u5185\u5b58\u3002 \u63d0\u9192 \u5728 11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 \u4e2d\uff0c\u6211\u4eec\u8bb2\u8fc7\uff0c\u62ec\u53f7\u521d\u59cb\u5316\u5e94\u8be5\u88ab\u7528\u4e8e\u521d\u59cb\u5316\u57fa\u4e8e\u957f\u5ea6\u521b\u5efa\u7684\u6570\u7ec4\u3001\u5bb9\u5668\u548c\u5217\u8868\uff08\u76f8\u5bf9\u4e8e\u57fa\u4e8e\u4e00\u7ec4\u5143\u7d20\u521b\u5efa\uff09\u3002\u56e0\u6b64\uff0c\u521d\u59cb\u5316 IntArray \u65f6\u4f7f\u7528\u4e86 IntArray ar ( 10 ); \u3002","title":"\u4e00\u4e2a\u6790\u6784\u51fd\u6570\u6848\u4f8b"},{"location":"13-9-destructors/#_3","text":"\u548c\u4e4b\u524d\u63d0\u5230\u7684\u4e00\u6837\uff0c\u6784\u9020\u51fd\u6570\u4f1a\u5728\u5bf9\u8c61\u521b\u5efa\u65f6\u8c03\u7528\uff0c\u800c\u6790\u6784\u51fd\u6570\u5219\u662f\u5728\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\u8c03\u7528\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f1a\u5728\u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570\u4e2d\u6dfb\u52a0\u6253\u5370\u6765\u5c55\u793a\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include class Simple { private : int m_nID {}; public : Simple ( int nID ) : m_nID { nID } { std :: cout << \"Constructing Simple \" << nID << '\\n' ; } ~ Simple () { std :: cout << \"Destructing Simple\" << m_nID << '\\n' ; } int getID () { return m_nID ; } }; int main () { // \u5728\u6808\u4e0a\u5206\u914d\u4e00\u4e2a Simple \u5bf9\u8c61 Simple simple { 1 }; std :: cout << simple . getID () << '\\n' ; // \u52a8\u6001\u5206\u914d\u4e00\u4e2a Simple \u5bf9\u8c61 Simple * pSimple { new Simple { 2 } }; std :: cout << pSimple -> getID () << '\\n' ; // \u52a8\u6001\u5206\u914d\u7684 pSimple \u5fc5\u987b\u8981\u88abdelete delete pSimple ; return 0 ; } // simple \u5bf9\u8c61\u4f1a\u5728\u6b64\u5904\u79bb\u5f00\u4f5c\u7528\u57df \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 Constructing Simple 1 1 Constructing Simple 2 2 Destructing Simple 2 Destructing Simple 1 \u6ce8\u610f\uff0c\u201cSimple 1\u201d \u662f\u5728 \u201cSimple 2\u201d \u4e4b\u540e\u9500\u6bc1\u7684\uff0c\u56e0\u4e3a\u6211\u4eec\u5728 main \u51fd\u6570\u7ed3\u675f\u524d\u5220\u9664\u4e86 pSimple \uff0c\u800c simple \u5219\u662f\u5728 main() \u7ed3\u675f\u65f6\u624d\u9500\u6bc1\u3002 \u5168\u5c40\u53d8\u91cf\u4f1a\u5728main()\u51fd\u6570\u524d\u6784\u9020\u800c\u5728main()\u7ed3\u675f\u540e\u9500\u6bc1 \u3002","title":"\u6784\u9020\u548c\u6790\u6784\u7684\u65f6\u673a"},{"location":"13-9-destructors/#raii","text":"\u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09 \u662f\u4e00\u79cd\u5c06\u8d44\u6e90\u4f7f\u7528\u4e0e\u5177\u6709\u81ea\u52a8\u6301\u7eed\u65f6\u95f4\u7684\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f(\u4f8b\u5982\uff0c\u975e\u52a8\u6001\u5206\u914d\u7684\u5bf9\u8c61)\u7ed1\u5b9a\u5728\u4e00\u8d77\u7684\u7f16\u7a0b\u6280\u672f\u3002\u5728C++\u4e2d\uff0cRAII\u662f\u901a\u8fc7\u5e26\u6709\u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570\u7684\u7c7b\u5b9e\u73b0\u7684\u3002\u8d44\u6e90(\u5982\u5185\u5b58\u3001\u6587\u4ef6\u6216\u6570\u636e\u5e93\u53e5\u67c4\u7b49)\u901a\u5e38\u662f\u5728\u5bf9\u8c61\u7684\u6784\u9020\u51fd\u6570\u4e2d\u83b7\u53d6\u7684(\u4e0d\u8fc7\uff0c\u5982\u679c\u9700\u8981\u7684\u8bdd\u4e5f\u53ef\u4ee5\u5728\u5bf9\u8c61\u521b\u5efa\u4e4b\u540e\u83b7\u53d6)\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u5728\u5bf9\u8c61\u5904\u4e8e\u6d3b\u52a8\u72b6\u6001\u65f6\u4f7f\u7528\u8be5\u8d44\u6e90\u3002\u5f53\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\uff0c\u8d44\u6e90\u5728\u6790\u6784\u51fd\u6570\u4e2d\u88ab\u91ca\u653e\u3002RAII\u7684\u4e3b\u8981\u4f18\u70b9\u662f\u5b83\u6709\u52a9\u4e8e\u9632\u6b62\u8d44\u6e90\u6cc4\u6f0f(\u4f8b\u5982\uff0c\u5185\u5b58\u6ca1\u6709\u88ab\u91ca\u653e)\uff0c\u56e0\u4e3a\u6240\u6709\u8d44\u6e90\u6301\u6709\u5bf9\u8c61\u90fd\u4f1a\u81ea\u52a8\u6e05\u7406\u3002 \u672c\u8282\u8bfe\u5f00\u59cb\u65f6\u7684 IntArray \u7c7b\u662f\u5b9e\u73b0RAII\u7684\u4e00\u4e2a\u4f8b\u5b50\u2014\u2014\u5728\u6784\u9020\u51fd\u6570\u4e2d\u5206\u914d\uff0c\u5728\u6790\u6784\u51fd\u6570\u4e2d\u91ca\u653e\u3002 std::string \u548c std::vector \u5219\u662f\u6807\u51c6\u5e93\u4e2d\u5b9e\u73b0RAII\u7684\u4f8b\u5b50\u2014\u2014\u52a8\u6001\u5185\u5b58\u5728\u521d\u59cb\u5316\u65f6\u83b7\u5f97\uff0c\u5728\u9500\u6bc1\u65f6\u81ea\u52a8\u6e05\u9664\u3002","title":"RAII"},{"location":"13-9-destructors/#exit","text":"\u6ce8\u610f\uff1a\u5f53\u4f7f\u7528 exit() \u51fd\u6570\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u5728\u4e0d\u8c03\u7528\u4efb\u4f55\u6784\u9020\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\u9000\u51fa\u3002\u5982\u679c\u4f60\u4f9d\u8d56\u4e8e\u6784\u9020\u51fd\u6570\u5728\u7a0b\u5e8f\u9000\u51fa\u65f6\u5b8c\u6210\u6e05\u7406\u5de5\u4f5c\uff08\u4f8b\u5982\uff0c\u5199\u65e5\u5fd7\u6216\u5199\u6570\u636e\u5e93\uff09\u3002","title":"\u6709\u5173 exit() \u51fd\u6570\u7684\u8b66\u793a"},{"location":"13-9-destructors/#_4","text":"\u5982\u4f60\u6240\u89c1\uff0c\u5f53\u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570\u88ab\u5b9e\u73b0\u540e\uff0c\u7c7b\u5c31\u53ef\u4ee5\u5728\u521d\u59cb\u5316\u81ea\u5df1\u5e76\u5728\u5176\u9500\u6bc1\u65f6\u5b8c\u6210\u6e05\u7406\u5de5\u4f5c\uff0c\u4e0d\u9700\u8981\u7a0b\u5e8f\u5458\u505a\u4efb\u4f55\u7279\u6b8a\u7684\u5de5\u4f5c\uff01\u8fd9\u964d\u4f4e\u4e86\u51fa\u9519\u7684\u53ef\u80fd\u6027\uff0c\u5e76\u4f7f\u7c7b\u66f4\u5bb9\u6613\u4f7f\u7528\u3002","title":"\u5c0f\u7ed3"},{"location":"13-x-Chapter-13-comprehensive-quiz/","tags":["summary"],"text":"13.2 - \u7c7b\u548c\u7c7b\u6210\u5458 \u4f7f\u7528\u7c7b\u53ef\u4ee5\u521b\u5efa\u81ea\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5e76\u4e14\u5c06\u6570\u636e\u548c\u51fd\u6570\u548c\u8be5\u6570\u636e\u7c7b\u578b\u7ed1\u5b9a\u5e76\u914d\u5408\u4f7f\u7528\u3002\u7c7b\u4e2d\u7684\u6570\u636e\u548c\u51fd\u6570\u79f0\u4e3a\u6210\u5458\uff0c\u800c\u9009\u62e9\u7c7b\u4e2d\u7684\u6210\u5458\u9700\u8981\u4f7f\u7528\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 . \uff08\u5728\u4f7f\u7528\u6307\u9488\u8bbf\u95ee\u65f6\uff0c\u4f7f\u7528 -> \u8fd0\u7b97\u7b26\uff09\u3002 13.3 - \u8bbf\u95ee\u4fee\u9970\u7b26 public \u548c private \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u5141\u8bb8\u7528\u6237\u9650\u5b9a\u7c7b\u6210\u5458\u53ef\u4ee5\u88ab\u8c01\u8bbf\u95ee\u3002 public \u6210\u5458\u53ef\u4ee5\u88ab\u4efb\u4f55\u4eba\u4f7f\u7528\u3002 private \u6210\u5458\u53ea\u80fd\u88ab\u7c7b\u4e2d\u7684\u5176\u4ed6\u6210\u5458\u8bbf\u95ee\u3002\u7a0d\u540e\u6211\u4eec\u8fd8\u4f1a\u4ecb\u7ecd protected \u6210\u5458\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u7c7b\u6210\u5458\u90fd\u662f private \u7684\uff0c\u800c\u7ed3\u6784\u4f53\u7684\u6210\u5458\u90fd\u662f public \u7684\u3002 13.4 - \u6210\u5458\u8bbf\u95ee\u51fd\u6570\u4e0e\u5c01\u88c5 \u5c01\u88c5 \u662f\u5c06\u6210\u5458\u8bbe\u4e3aprivate\u7684\u8fc7\u7a0b\uff0c\u8fd9\u6837\u5b83\u4eec\u5c31\u4e0d\u80fd\u88ab\u76f4\u63a5\u8bbf\u95ee\u3002\u8fd9\u6709\u52a9\u4e8e\u4fdd\u62a4\u7c7b\u4e0d\u88ab\u6ee5\u7528\u3002 13.5 - \u6784\u9020\u51fd\u6570 \u6784\u9020\u51fd\u6570 \u662f\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\u7684\u51fd\u6570\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u521d\u59cb\u5316\u8be5\u7c7b\u7684\u5bf9\u8c61\u3002\u4e0d\u5177\u6709\u4efb\u4f55 \u5f62\u53c2 \uff08\u6216\u5177\u6709\u5168\u90e8\u9ed8\u8ba4\u5f62\u53c2\uff09\u7684\u6784\u9020\u51fd\u6570\u662f\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002\u5f53\u7528\u6237\u6ca1\u6709\u63d0\u4f9b\u4efb\u4f55\u521d\u59cb\u5316\u503c\u7684\u65f6\u5019\uff0c\u9ed8\u8ba4\u51fd\u6570\u5c06\u4f1a\u88ab\u8c03\u7528\u3002\u4f60\u5e94\u8be5\u81f3\u5c11\u4e3a\u7c7b\u63d0\u4f9b\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u3002 13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u5217\u8868 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u5141\u8bb8\u7528\u6237\u5728\u6784\u9020\u51fd\u6570\u4e2d\u5bf9\u6210\u5458\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\uff08\u800c\u4e0d\u5fc5\u4e3a\u6bcf\u4e2a\u6210\u5458\u53d8\u91cf\u8d4b\u503c\uff09\u3002 13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u5141\u8bb8\u4f60\u5728\u58f0\u660e\u6210\u5458\u65f6\u4e3a\u53d8\u91cf\u76f4\u63a5\u6307\u5b9a\u9ed8\u8ba4\u503c\u3002 13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570 \u6784\u9020\u51fd\u6570 \u4e5f\u53ef\u4ee5\u8c03\u7528\u5176\u4ed6\u6784\u9020\u51fd\u6570\uff08\u79f0\u4e3a \u59d4\u6258\u6784\u9020\u51fd\u6570 \u6216\u6784\u9020\u51fd\u6570\u94fe\uff09\u3002 13.9 - \u6790\u6784\u51fd\u6570 \u6790\u6784\u51fd\u6570 \u662f\u53e6\u5916\u4e00\u7c7b\u7279\u6b8a\u7684\u6784\u9020\u51fd\u6570\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u8ba9\u7c7b\u5bf9\u81ea\u5df1\u8fdb\u884c\u6e05\u7406\u3002\u4efb\u4f55\u5185\u5b58\u56de\u6536\u6216\u9000\u51fa\u6d41\u7a0b\u90fd\u5e94\u8be5\u5728\u6790\u6784\u51fd\u6570\u4e2d\u6267\u884c\u3002 13.10 - \u9690\u85cf\u7684this\u6307\u9488 \u6240\u6709\u7684\u6210\u5458\u51fd\u6570\u90fd\u6709\u4e00\u4e2a\u9690\u85cf\u7684\u6307\u9488 this \uff0c this \u6307\u9488\u6307\u5411\u8fd9\u4e2a\u5bf9\u8c61\u672c\u8eab\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4f60\u5e76\u4e0d\u9700\u8981\u76f4\u63a5\u8bbf\u95ee\u8be5\u6307\u9488\uff0c\u4f46\u662f\u5076\u5c14\u4e5f\u4f1a\u6709\u8fd9\u6837\u7684\u9700\u6c42\u3002 13.11 - \u7c7b\u4ee3\u7801\u548c\u5934\u6587\u4ef6 \u4e00\u4e2a\u597d\u7684\u7f16\u7a0b\u5b9e\u8df5\u662f\u8fd9\u6837\u7684\uff1a\u5c06\u7c7b\u7684\u5b9a\u4e49\u653e\u7f6e\u5728\u5176\u540c\u540d\u7684\u5934\u6587\u4ef6\u5185\uff0c\u7136\u540e\u5c06\u7c7b\u7684\u6210\u5458\u5b9a\u4e49\u5728\u53e6\u4e00\u4e2a\u4e0e\u7c7b\u540c\u540d\u7684 .cpp \u6587\u4ef6\u4e2d\u3002 \u8fd9\u6837\u6709\u52a9\u4e8e\u907f\u514d\u5faa\u73af\u4f9d\u8d56\u3002 13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570 \u5982\u679c\u6210\u5458\u51fd\u6570\u4e0d\u4fee\u6539\u7c7b\u7684\u72b6\u6001\uff0c\u5219\u53ef\u4ee5\uff08\u4e5f\u5e94\u8be5\uff09\u88ab\u58f0\u660e\u4e3aconst\u7c7b\u578b\u3002const \u5bf9\u8c61\u4e5f\u53ea\u80fd\u8c03\u7528const\u6210\u5458\u51fd\u6570\u3002 13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf & 13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570 \u9759\u6001\u6210\u5458\u53d8\u91cf \u5728\u8be5\u7c7b\u7684\u6240\u6709\u5bf9\u8c61\u95f4\u5171\u4eab\u3002\u5b83\u4eec\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8c61\u6765\u8bbf\u95ee\uff0c\u5b83\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u6765\u8bbf\u95ee\u3002 \u9759\u6001\u6210\u5458\u51fd\u6570 \u6ca1\u6709 this \u6307\u9488\uff0c\u5b83\u4eec\u53ea\u80fd\u591f\u88ab\u9759\u6001\u6210\u5458\u53d8\u91cf\u8bbf\u95ee\u3002 13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b \u53cb\u5143\u51fd\u6570 \u88ab\u5f53\u505a\u7c7b\u6210\u5458\u51fd\u6570\u4f7f\u7528\uff08\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u7c7b\u7684\u79c1\u6709\u6570\u636e\uff09\u3002 \u53cb\u5143\u7c7b \u7684\u6240\u6709\u6210\u5458\u51fd\u6570\u90fd\u88ab\u770b\u505a\u662f\u53cb\u5143\u51fd\u6570\u3002 13.16 - \u533f\u540d\u5bf9\u8c61 \u5728\u8868\u8fbe\u5f0f\u6c42\u503c\uff0c\u6216\u8005\u4f20\u9012\u3001\u8fd4\u56de\u503c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u533f\u540d\u5bf9\u8c61\u3002 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b \u4f60\u4e5f\u53ef\u4ee5\u5728\u7c7b\u4e2d\u5d4c\u5957\u7c7b\u578b\u3002\u901a\u5e38\u662f\u679a\u4e3e\u7c7b\u578b\uff0c\u4e0d\u8fc7\u5982\u6709\u9700\u8981\u4e5f\u53ef\u4ee5\u662f\u5176\u4ed6\u7c7b\u578b\uff08\u6216\u8005\u5176\u4ed6\u7c7b\uff09\u3002","title":"13.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u9762\u5411\u5bf9\u8c61\u57fa\u7840"},{"location":"14-1-introduction-to-operator-overloading/","tags":["operator","overload"],"text":"Key Takeaway \u5728\u8bfe\u7a0b 8.9 - \u51fd\u6570\u91cd\u8f7d \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u51fd\u6570\u91cd\u8f7d\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u673a\u5236\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u540d\u79f0\u521b\u5efa\u548c\u89e3\u6790\u591a\u4e2a\u51fd\u6570\u8c03\u7528\uff0c\u53ea\u8981\u6bcf\u4e2a\u51fd\u6570\u90fd\u5177\u6709\u552f\u4e00\u7684\u51fd\u6570\u539f\u578b\u5373\u53ef\u3002\u8fd9\u6837\uff0c\u4f60\u5c31\u53ef\u4ee5\u521b\u5efa\u4e0e\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u4e00\u8d77\u4f7f\u7528\u7684\u51fd\u6570\u53d8\u4f53\uff0c\u800c\u65e0\u9700\u4e3a\u6bcf\u4e2a\u53d8\u4f53\u60f3\u51fa\u4e00\u4e2a\u72ec\u7279\u7684\u540d\u79f0\u3002 \u5728C++\u4e2d\uff0c\u64cd\u4f5c\u7b26\u4e5f\u88ab\u5b9e\u73b0\u4e3a\u51fd\u6570\u3002\u901a\u8fc7\u5728\u64cd\u4f5c\u7b26\u51fd\u6570\u4e0a\u4f7f\u7528\u51fd\u6570\u91cd\u8f7d\uff0c\u4f60\u53ef\u4ee5\u81ea\u5b9a\u4e49\u64cd\u4f5c\u7b26\u7684\u529f\u80fd\uff0c\u4f7f\u5176\u53ef\u4ee5\u4e0e\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u4e00\u8d77\u4f7f\u7528\uff08\u5305\u62ec\u4f60\u7f16\u5199\u7684\u7c7b\uff09\u3002\u4f7f\u7528\u51fd\u6570\u91cd\u8f7d\u6765\u91cd\u8f7d\u64cd\u4f5c\u7b26\u79f0\u4e3a \u64cd\u4f5c\u7b26\u91cd\u8f7d \u3002 \u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u68c0\u67e5\u4e0e\u64cd\u4f5c\u7b26\u91cd\u8f7d\u76f8\u5173\u7684\u4e3b\u9898\u3002 \u64cd\u4f5c\u7b26\u4f5c\u4e3a\u51fd\u6570 \u8003\u8651\u4ee5\u4e0b\u793a\u4f8b\uff1a 1 2 3 int x { 2 }; int y { 3 }; std :: cout << x + y << '\\\\\\\\\\\\\\\\ n ' ; \u7f16\u8bd1\u5668\u63d0\u4f9b\u4e86\u7528\u4e8e\u6574\u6570\u64cd\u4f5c\u6570\u7684\u52a0\u53f7\u8fd0\u7b97\u7b26\uff08 + \uff09\u7684\u5185\u7f6e\u7248\u672c \u2014\u2014 \u8be5\u51fd\u6570\u5c06\u6574\u6570 x \u548c y \u76f8\u52a0\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u7ed3\u679c\u3002\u5f53\u4f60\u770b\u5230\u8868\u8fbe\u5f0f x + y \u65f6\uff0c\u4f60\u53ef\u4ee5\u5c06\u5176\u5728\u5934\u8111\u4e2d\u7ffb\u8bd1\u4e3a\u51fd\u6570\u8c03\u7528 operator+(x, y) \uff08\u5176\u4e2d operator+ \u662f\u51fd\u6570\u7684\u540d\u79f0\uff09\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 double z { 2.0 }; double w { 3.0 }; std :: cout << w + z << '\\\\\\\\\\\\\\\\ n ' ; \u7f16\u8bd1\u5668\u4e5f\u9644\u5e26\u4e86\u53cc\u7cbe\u5ea6\u64cd\u4f5c\u6570\u7684\u52a0\u53f7\u8fd0\u7b97\u7b26\uff08 + \uff09\u7684\u5185\u7f6e\u7248\u672c\u3002\u8868\u8fbe\u5f0f w + z \u53d8\u4e3a\u51fd\u6570\u8c03\u7528 operator+(w, z) \uff0c\u5e76\u4f7f\u7528\u51fd\u6570\u91cd\u8f7d\u6765\u786e\u5b9a\u7f16\u8bd1\u5668\u5e94\u8be5\u8c03\u7528\u6b64\u51fd\u6570\u7684\u53cc\u7cbe\u5ea6\u7248\u672c\u800c\u4e0d\u662f\u6574\u6570\u7248\u672c\u3002 \u73b0\u5728\u8003\u8651\u5982\u679c\u6211\u4eec\u5c1d\u8bd5\u5c06\u4e24\u4e2a\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u7684\u5bf9\u8c61\u76f8\u52a0\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1a 1 2 3 Mystring string1 { \"Hello, \" }; Mystring string2 { \"World!\" }; std :: cout << string1 + string2 << '\\\\\\\\\\\\\\\\ n ' ; \u8fd0\u884c\u7ed3\u679c\u5982\u4f55\uff1f\u5b57\u7b26\u4e32\u201cHello\uff0cWorld\uff01\u201d\u672c\u5e94\u8be5\u5c06\u4f1a\u88ab\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\u3002\u4f46\u662f\uff0c\u7531\u4e8eMystring\u662f\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\uff0c\u7f16\u8bd1\u5668\u6ca1\u6709\u53ef\u7528\u4e8e Mystring \u7c7b\u578b\u64cd\u4f5c\u6570\u7684\u52a0\u53f7\u8fd0\u7b97\u7b26\u3002\u56e0\u6b64\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u544a\u9519\u8bef\u3002\u4e3a\u4e86\u4f7f\u5176\u6309\u7167\u6211\u4eec\u60f3\u8981\u7684\u65b9\u5f0f\u5de5\u4f5c\uff0c\u5219\u9700\u8981\u91cd\u8f7d\u51fd\u6570\u2014\u2014\u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u4f55\u5c06 + \u64cd\u4f5c\u7b26\u4e0e\u4e24\u4e2a Mystring \u7c7b\u578b\u7684\u64cd\u4f5c\u6570\u4e00\u8d77\u4f7f\u7528\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u770b\u5230\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9\u3002 \u89e3\u6790\u91cd\u8f7d\u64cd\u4f5c\u7b26 \u5728\u8ba1\u7b97\u5305\u542b\u64cd\u4f5c\u7b26\u7684\u8868\u8fbe\u5f0f\u65f6\uff0c\u7f16\u8bd1\u5668\u4f7f\u7528\u4ee5\u4e0b\u89c4\u5219\uff1a \u5982\u679c \u6240\u6709 \u64cd\u4f5c\u6570\u90fd\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u5219\u7f16\u8bd1\u5668\u5c06\u8c03\u7528\u5185\u7f6e\u4f8b\u7a0b\uff08\u5982\u679c\u5b58\u5728\uff09\u3002\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u7f16\u8bd1\u5668\u5c06\u751f\u6210\u7f16\u8bd1\u5668\u9519\u8bef\u3002 \u5982\u679c \u4efb\u4f55 \u64cd\u4f5c\u6570\u662f\u7528\u6237\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982\u4f60\u7684\u7c7b\u4e4b\u4e00\u6216\u679a\u4e3e\u7c7b\u578b\uff09\uff0c\u5219\u7f16\u8bd1\u5668\u5c06\u67e5\u770b\u8be5\u7c7b\u578b\u662f\u5426\u5177\u6709\u5339\u914d\u7684\u91cd\u8f7d\u64cd\u4f5c\u7b26\u51fd\u6570\u53ef\u4ee5\u8c03\u7528\u3002\u5982\u679c\u627e\u4e0d\u5230\uff0c\u5b83\u5c06\u5c1d\u8bd5\u5c06\u4e00\u4e2a\u6216\u591a\u4e2a\u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u64cd\u4f5c\u6570\u8f6c\u6362\u4e3a\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u4ee5\u4fbf\u5b83\u53ef\u4ee5\u4f7f\u7528\u5339\u914d\u7684\u5185\u7f6e\u64cd\u4f5c\u7b26\uff08\u901a\u8fc7\u91cd\u8f7d\u578b\u8f6c\u6362\uff0c\u6211\u4eec\u5c06\u5728\u672c\u7ae0\u7684\u540e\u9762\u4ecb\u7ecd\uff09\u3002\u5982\u679c\u5931\u8d25\uff0c\u5219\u5b83\u5c06\u751f\u6210\u7f16\u8bd1\u9519\u8bef\u3002 \u64cd\u4f5c\u7b26\u91cd\u8f7d\u7684\u9650\u5236\u662f\u4ec0\u4e48\uff1f \u9996\u5148\uff0cC++\u4e2d\u51e0\u4e4e\u53ef\u4ee5\u91cd\u8f7d\u6240\u6709\u73b0\u6709\u64cd\u4f5c\u7b26\u3002\u4f8b\u5916\u60c5\u51b5\u662f\uff1a\u6761\u4ef6\uff08 ?: \uff09\uff0csizeof\uff0c\u8303\u56f4\uff08 :: \uff09\uff0c\u6210\u5458\u9009\u62e9\u5668\uff08 . \uff09\uff0c\u6307\u9488\u6210\u5458\u9009\u62e9\u5668\uff08 -> \uff09\uff0ctypeid\u4ee5\u53ca\u8f6c\u6362\u64cd\u4f5c\u7b26\u3002 \u5176\u6b21\uff0c\u4f60\u53ea\u80fd\u91cd\u8f7d\u73b0\u6709\u7684\u64cd\u4f5c\u7b26\u3002\u4f60\u4e0d\u80fd\u521b\u5efa\u65b0\u64cd\u4f5c\u7b26\u6216\u91cd\u547d\u540d\u73b0\u6709\u64cd\u4f5c\u7b26\u3002\u4f8b\u5982\uff0c\u4f60\u4e0d\u80fd\u521b\u5efa\u4e00\u4e2a\u64cd\u4f5c\u7b26 ** \u6765\u6267\u884c\u5e42\u3002 \u7b2c\u4e09\uff0c\u91cd\u8f7d\u64cd\u4f5c\u7b26\u4e2d\u81f3\u5c11\u6709\u4e00\u4e2a\u64cd\u4f5c\u6570\u5fc5\u987b\u662f\u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u3002\u8fd9\u610f\u5473\u7740\u4f60\u4e0d\u80fd\u91cd\u8f7d\u52a0\u53f7\u64cd \u7b2c\u56db\uff0c\u4e0d\u53ef\u80fd\u6539\u53d8\u64cd\u4f5c\u7b26\u652f\u6301\u7684\u64cd\u4f5c\u6570\u7684\u6570\u91cf\u3002 \u6700\u540e\uff0c\u6240\u6709\u64cd\u4f5c\u7b26\u90fd\u4fdd\u6301\u5176\u9ed8\u8ba4\u7684\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u6027\uff08\u65e0\u8bba\u5b83\u4eec\u7528\u4e8e\u4ec0\u4e48\uff09\uff0c\u8fd9\u662f\u4e0d\u80fd\u6539\u53d8\u7684\u3002 \u4e00\u4e9b\u65b0\u7684\u7a0b\u5e8f\u5458\u8bd5\u56fe\u91cd\u8f7d\u4f4d\u8fd0\u7b97XOR\u64cd\u4f5c\u7b26\uff08 ^ \uff09\u6765\u5b9e\u73b0\u6307\u6570\u8fd0\u7b97\u3002\u4f46\u662f\uff0c\u5728C++\u4e2d\uff0c operator^ \u7684\u4f18\u5148\u7ea7\u4f4e\u4e8e\u57fa\u672c\u7b97\u672f\u8fd0\u7b97\u7b26\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u8868\u8fbe\u5f0f\u8bc4\u4f30\u4e0d\u6b63\u786e\u3002 \u5728\u57fa\u672c\u6570\u5b66\u4e2d\uff0c\u6307\u6570\u8fd0\u7b97\u4f18\u5148\u4e8e\u57fa\u672c\u7b97\u672f\u8fd0\u7b97\uff0c\u56e0\u6b64 4 + 3 ^ 2 \u89e3\u6790\u4e3a 4 + (3 ^ 2) => 4 + 9 => 13 \u3002\u4f46\u662f\uff0c\u5728C++\u4e2d\uff0c\u7b97\u672f\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e operator^ \uff0c\u56e0\u6b64 4 + 3 ^ 2 \u89e3\u6790\u4e3a\uff08 4 + 3\uff09^ 2 => 7 ^ 2 => 49 \u3002 \u8981\u6b63\u786e\u4f7f\u7528\u5b83\uff0c\u6bcf\u6b21\u90fd\u663e\u5f0f\u5730\u4e3a\u6307\u6570\u90e8\u5206\u6dfb\u52a0\u62ec\u53f7\uff08\u4f8b\u5982 4 + (3 ^ 2) \uff09\uff0c\u8fd9\u5e76\u4e0d\u76f4\u89c2\uff0c\u5e76\u4e14\u53ef\u80fd\u5b58\u5728\u9519\u8bef\u3002 \u56e0\u6b64\uff0c\u51fa\u4e8e\u8fd9\u4e2a\u4f18\u5148\u7ea7\u95ee\u9898\uff0c\u6700\u597d\u53ea\u4ee5\u7c7b\u4f3c\u4e8e\u5176\u539f\u59cb\u610f\u56fe\u7684\u65b9\u5f0f\u4f7f\u7528\u8fd0\u7b97\u7b26\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u91cd\u8f7d\u64cd\u4f5c\u7b26\u65f6\uff0c\u6700\u597d\u4f7f\u64cd\u4f5c\u7b26\u7684\u529f\u80fd\u5c3d\u53ef\u80fd\u63a5\u8fd1\u64cd\u4f5c\u7b26\u7684\u539f\u59cb\u610f\u56fe\u3002 \u6b64\u5916\uff0c\u7531\u4e8e\u64cd\u4f5c\u7b26\u6ca1\u6709\u63cf\u8ff0\u6027\u7684\u540d\u79f0\uff0c\u5b83\u4eec\u7684\u7528\u9014\u5e76\u4e0d\u603b\u662f\u5f88\u6e05\u695a\u3002\u4f8b\u5982\uff0c operator+ \u53ef\u80fd\u662f\u5b57\u7b26\u4e32\u7c7b\u8fde\u63a5\u5b57\u7b26\u4e32\u7684\u5408\u7406\u9009\u62e9\u3002\u4f46\u662f operator- \u5462\uff1f\u4f60\u671f\u671b\u5b83\u505a\u4ec0\u4e48\uff1f\u8fd9\u4e0d\u6e05\u695a\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u91cd\u8f7d\u64cd\u4f5c\u7b26\u7684\u542b\u4e49\u4e0d\u6e05\u6670\u3001\u4e0d\u76f4\u89c2\uff0c\u8bf7\u4f7f\u7528\u547d\u540d\u51fd\u6570\u4ee3\u66ff\u64cd\u4f5c\u7b26\u91cd\u8f7d\u3002 \u5728\u8fd9\u4e9b\u9650\u5236\u5185\uff0c\u4f60\u4ecd\u7136\u53ef\u4ee5\u627e\u5230\u8bb8\u591a\u6709\u7528\u7684\u529f\u80fd\u6765\u91cd\u8f7d\u4f60\u7684\u81ea\u5b9a\u4e49\u7c7b\uff01\u4f60\u53ef\u4ee5\u91cd\u8f7d + \u8fd0\u7b97\u7b26\u6765\u8fde\u63a5\u4f60\u7684\u7528\u6237\u5b9a\u4e49\u7684\u5b57\u7b26\u4e32\u7c7b\uff0c\u6216\u5c06\u4e24\u4e2a Fraction \u7c7b\u5bf9\u8c61\u76f8\u52a0\u3002\u4f60\u53ef\u4ee5\u91cd\u8f7d << \u8fd0\u7b97\u7b26\uff0c\u4ee5\u4fbf\u8f7b\u677e\u5c06\u4f60\u7684\u7c7b\u6253\u5370\u5230\u5c4f\u5e55\uff08\u6216\u6587\u4ef6\uff09\u3002\u4f60\u53ef\u4ee5\u91cd\u8f7d\u7b49\u5f0f\u8fd0\u7b97\u7b26\uff08 == \uff09\u6765\u6bd4\u8f83\u4e24\u4e2a\u7c7b\u5bf9\u8c61\u3002\u8fd9\u4f7f\u5f97\u8fd0\u7b97\u7b26\u91cd\u8f7d\u6210\u4e3aC ++\u4e2d\u6700\u6709\u7528\u7684\u529f\u80fd\u4e4b\u4e00 \u2014\u2014 \u56e0\u4e3a\u5b83\u5141\u8bb8\u4f60\u4ee5\u66f4\u76f4\u89c2\u7684\u65b9\u5f0f\u4f7f\u7528\u4f60\u7684\u7c7b\u3002 \u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u66f4\u6df1\u5165\u5730\u7814\u7a76\u91cd\u8f7d\u4e0d\u540c\u7c7b\u578b\u7684\u8fd0\u7b97\u7b26\u3002","title":"14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb"},{"location":"14-1-introduction-to-operator-overloading/#_1","text":"\u8003\u8651\u4ee5\u4e0b\u793a\u4f8b\uff1a 1 2 3 int x { 2 }; int y { 3 }; std :: cout << x + y << '\\\\\\\\\\\\\\\\ n ' ; \u7f16\u8bd1\u5668\u63d0\u4f9b\u4e86\u7528\u4e8e\u6574\u6570\u64cd\u4f5c\u6570\u7684\u52a0\u53f7\u8fd0\u7b97\u7b26\uff08 + \uff09\u7684\u5185\u7f6e\u7248\u672c \u2014\u2014 \u8be5\u51fd\u6570\u5c06\u6574\u6570 x \u548c y \u76f8\u52a0\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u7ed3\u679c\u3002\u5f53\u4f60\u770b\u5230\u8868\u8fbe\u5f0f x + y \u65f6\uff0c\u4f60\u53ef\u4ee5\u5c06\u5176\u5728\u5934\u8111\u4e2d\u7ffb\u8bd1\u4e3a\u51fd\u6570\u8c03\u7528 operator+(x, y) \uff08\u5176\u4e2d operator+ \u662f\u51fd\u6570\u7684\u540d\u79f0\uff09\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 double z { 2.0 }; double w { 3.0 }; std :: cout << w + z << '\\\\\\\\\\\\\\\\ n ' ; \u7f16\u8bd1\u5668\u4e5f\u9644\u5e26\u4e86\u53cc\u7cbe\u5ea6\u64cd\u4f5c\u6570\u7684\u52a0\u53f7\u8fd0\u7b97\u7b26\uff08 + \uff09\u7684\u5185\u7f6e\u7248\u672c\u3002\u8868\u8fbe\u5f0f w + z \u53d8\u4e3a\u51fd\u6570\u8c03\u7528 operator+(w, z) \uff0c\u5e76\u4f7f\u7528\u51fd\u6570\u91cd\u8f7d\u6765\u786e\u5b9a\u7f16\u8bd1\u5668\u5e94\u8be5\u8c03\u7528\u6b64\u51fd\u6570\u7684\u53cc\u7cbe\u5ea6\u7248\u672c\u800c\u4e0d\u662f\u6574\u6570\u7248\u672c\u3002 \u73b0\u5728\u8003\u8651\u5982\u679c\u6211\u4eec\u5c1d\u8bd5\u5c06\u4e24\u4e2a\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u7684\u5bf9\u8c61\u76f8\u52a0\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1a 1 2 3 Mystring string1 { \"Hello, \" }; Mystring string2 { \"World!\" }; std :: cout << string1 + string2 << '\\\\\\\\\\\\\\\\ n ' ; \u8fd0\u884c\u7ed3\u679c\u5982\u4f55\uff1f\u5b57\u7b26\u4e32\u201cHello\uff0cWorld\uff01\u201d\u672c\u5e94\u8be5\u5c06\u4f1a\u88ab\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\u3002\u4f46\u662f\uff0c\u7531\u4e8eMystring\u662f\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\uff0c\u7f16\u8bd1\u5668\u6ca1\u6709\u53ef\u7528\u4e8e Mystring \u7c7b\u578b\u64cd\u4f5c\u6570\u7684\u52a0\u53f7\u8fd0\u7b97\u7b26\u3002\u56e0\u6b64\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u544a\u9519\u8bef\u3002\u4e3a\u4e86\u4f7f\u5176\u6309\u7167\u6211\u4eec\u60f3\u8981\u7684\u65b9\u5f0f\u5de5\u4f5c\uff0c\u5219\u9700\u8981\u91cd\u8f7d\u51fd\u6570\u2014\u2014\u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u4f55\u5c06 + \u64cd\u4f5c\u7b26\u4e0e\u4e24\u4e2a Mystring \u7c7b\u578b\u7684\u64cd\u4f5c\u6570\u4e00\u8d77\u4f7f\u7528\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u770b\u5230\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9\u3002","title":"\u64cd\u4f5c\u7b26\u4f5c\u4e3a\u51fd\u6570"},{"location":"14-1-introduction-to-operator-overloading/#_2","text":"\u5728\u8ba1\u7b97\u5305\u542b\u64cd\u4f5c\u7b26\u7684\u8868\u8fbe\u5f0f\u65f6\uff0c\u7f16\u8bd1\u5668\u4f7f\u7528\u4ee5\u4e0b\u89c4\u5219\uff1a \u5982\u679c \u6240\u6709 \u64cd\u4f5c\u6570\u90fd\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u5219\u7f16\u8bd1\u5668\u5c06\u8c03\u7528\u5185\u7f6e\u4f8b\u7a0b\uff08\u5982\u679c\u5b58\u5728\uff09\u3002\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u7f16\u8bd1\u5668\u5c06\u751f\u6210\u7f16\u8bd1\u5668\u9519\u8bef\u3002 \u5982\u679c \u4efb\u4f55 \u64cd\u4f5c\u6570\u662f\u7528\u6237\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982\u4f60\u7684\u7c7b\u4e4b\u4e00\u6216\u679a\u4e3e\u7c7b\u578b\uff09\uff0c\u5219\u7f16\u8bd1\u5668\u5c06\u67e5\u770b\u8be5\u7c7b\u578b\u662f\u5426\u5177\u6709\u5339\u914d\u7684\u91cd\u8f7d\u64cd\u4f5c\u7b26\u51fd\u6570\u53ef\u4ee5\u8c03\u7528\u3002\u5982\u679c\u627e\u4e0d\u5230\uff0c\u5b83\u5c06\u5c1d\u8bd5\u5c06\u4e00\u4e2a\u6216\u591a\u4e2a\u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u64cd\u4f5c\u6570\u8f6c\u6362\u4e3a\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u4ee5\u4fbf\u5b83\u53ef\u4ee5\u4f7f\u7528\u5339\u914d\u7684\u5185\u7f6e\u64cd\u4f5c\u7b26\uff08\u901a\u8fc7\u91cd\u8f7d\u578b\u8f6c\u6362\uff0c\u6211\u4eec\u5c06\u5728\u672c\u7ae0\u7684\u540e\u9762\u4ecb\u7ecd\uff09\u3002\u5982\u679c\u5931\u8d25\uff0c\u5219\u5b83\u5c06\u751f\u6210\u7f16\u8bd1\u9519\u8bef\u3002","title":"\u89e3\u6790\u91cd\u8f7d\u64cd\u4f5c\u7b26"},{"location":"14-1-introduction-to-operator-overloading/#_3","text":"\u9996\u5148\uff0cC++\u4e2d\u51e0\u4e4e\u53ef\u4ee5\u91cd\u8f7d\u6240\u6709\u73b0\u6709\u64cd\u4f5c\u7b26\u3002\u4f8b\u5916\u60c5\u51b5\u662f\uff1a\u6761\u4ef6\uff08 ?: \uff09\uff0csizeof\uff0c\u8303\u56f4\uff08 :: \uff09\uff0c\u6210\u5458\u9009\u62e9\u5668\uff08 . \uff09\uff0c\u6307\u9488\u6210\u5458\u9009\u62e9\u5668\uff08 -> \uff09\uff0ctypeid\u4ee5\u53ca\u8f6c\u6362\u64cd\u4f5c\u7b26\u3002 \u5176\u6b21\uff0c\u4f60\u53ea\u80fd\u91cd\u8f7d\u73b0\u6709\u7684\u64cd\u4f5c\u7b26\u3002\u4f60\u4e0d\u80fd\u521b\u5efa\u65b0\u64cd\u4f5c\u7b26\u6216\u91cd\u547d\u540d\u73b0\u6709\u64cd\u4f5c\u7b26\u3002\u4f8b\u5982\uff0c\u4f60\u4e0d\u80fd\u521b\u5efa\u4e00\u4e2a\u64cd\u4f5c\u7b26 ** \u6765\u6267\u884c\u5e42\u3002 \u7b2c\u4e09\uff0c\u91cd\u8f7d\u64cd\u4f5c\u7b26\u4e2d\u81f3\u5c11\u6709\u4e00\u4e2a\u64cd\u4f5c\u6570\u5fc5\u987b\u662f\u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u3002\u8fd9\u610f\u5473\u7740\u4f60\u4e0d\u80fd\u91cd\u8f7d\u52a0\u53f7\u64cd \u7b2c\u56db\uff0c\u4e0d\u53ef\u80fd\u6539\u53d8\u64cd\u4f5c\u7b26\u652f\u6301\u7684\u64cd\u4f5c\u6570\u7684\u6570\u91cf\u3002 \u6700\u540e\uff0c\u6240\u6709\u64cd\u4f5c\u7b26\u90fd\u4fdd\u6301\u5176\u9ed8\u8ba4\u7684\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u6027\uff08\u65e0\u8bba\u5b83\u4eec\u7528\u4e8e\u4ec0\u4e48\uff09\uff0c\u8fd9\u662f\u4e0d\u80fd\u6539\u53d8\u7684\u3002 \u4e00\u4e9b\u65b0\u7684\u7a0b\u5e8f\u5458\u8bd5\u56fe\u91cd\u8f7d\u4f4d\u8fd0\u7b97XOR\u64cd\u4f5c\u7b26\uff08 ^ \uff09\u6765\u5b9e\u73b0\u6307\u6570\u8fd0\u7b97\u3002\u4f46\u662f\uff0c\u5728C++\u4e2d\uff0c operator^ \u7684\u4f18\u5148\u7ea7\u4f4e\u4e8e\u57fa\u672c\u7b97\u672f\u8fd0\u7b97\u7b26\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u8868\u8fbe\u5f0f\u8bc4\u4f30\u4e0d\u6b63\u786e\u3002 \u5728\u57fa\u672c\u6570\u5b66\u4e2d\uff0c\u6307\u6570\u8fd0\u7b97\u4f18\u5148\u4e8e\u57fa\u672c\u7b97\u672f\u8fd0\u7b97\uff0c\u56e0\u6b64 4 + 3 ^ 2 \u89e3\u6790\u4e3a 4 + (3 ^ 2) => 4 + 9 => 13 \u3002\u4f46\u662f\uff0c\u5728C++\u4e2d\uff0c\u7b97\u672f\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e operator^ \uff0c\u56e0\u6b64 4 + 3 ^ 2 \u89e3\u6790\u4e3a\uff08 4 + 3\uff09^ 2 => 7 ^ 2 => 49 \u3002 \u8981\u6b63\u786e\u4f7f\u7528\u5b83\uff0c\u6bcf\u6b21\u90fd\u663e\u5f0f\u5730\u4e3a\u6307\u6570\u90e8\u5206\u6dfb\u52a0\u62ec\u53f7\uff08\u4f8b\u5982 4 + (3 ^ 2) \uff09\uff0c\u8fd9\u5e76\u4e0d\u76f4\u89c2\uff0c\u5e76\u4e14\u53ef\u80fd\u5b58\u5728\u9519\u8bef\u3002 \u56e0\u6b64\uff0c\u51fa\u4e8e\u8fd9\u4e2a\u4f18\u5148\u7ea7\u95ee\u9898\uff0c\u6700\u597d\u53ea\u4ee5\u7c7b\u4f3c\u4e8e\u5176\u539f\u59cb\u610f\u56fe\u7684\u65b9\u5f0f\u4f7f\u7528\u8fd0\u7b97\u7b26\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u91cd\u8f7d\u64cd\u4f5c\u7b26\u65f6\uff0c\u6700\u597d\u4f7f\u64cd\u4f5c\u7b26\u7684\u529f\u80fd\u5c3d\u53ef\u80fd\u63a5\u8fd1\u64cd\u4f5c\u7b26\u7684\u539f\u59cb\u610f\u56fe\u3002 \u6b64\u5916\uff0c\u7531\u4e8e\u64cd\u4f5c\u7b26\u6ca1\u6709\u63cf\u8ff0\u6027\u7684\u540d\u79f0\uff0c\u5b83\u4eec\u7684\u7528\u9014\u5e76\u4e0d\u603b\u662f\u5f88\u6e05\u695a\u3002\u4f8b\u5982\uff0c operator+ \u53ef\u80fd\u662f\u5b57\u7b26\u4e32\u7c7b\u8fde\u63a5\u5b57\u7b26\u4e32\u7684\u5408\u7406\u9009\u62e9\u3002\u4f46\u662f operator- \u5462\uff1f\u4f60\u671f\u671b\u5b83\u505a\u4ec0\u4e48\uff1f\u8fd9\u4e0d\u6e05\u695a\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u91cd\u8f7d\u64cd\u4f5c\u7b26\u7684\u542b\u4e49\u4e0d\u6e05\u6670\u3001\u4e0d\u76f4\u89c2\uff0c\u8bf7\u4f7f\u7528\u547d\u540d\u51fd\u6570\u4ee3\u66ff\u64cd\u4f5c\u7b26\u91cd\u8f7d\u3002 \u5728\u8fd9\u4e9b\u9650\u5236\u5185\uff0c\u4f60\u4ecd\u7136\u53ef\u4ee5\u627e\u5230\u8bb8\u591a\u6709\u7528\u7684\u529f\u80fd\u6765\u91cd\u8f7d\u4f60\u7684\u81ea\u5b9a\u4e49\u7c7b\uff01\u4f60\u53ef\u4ee5\u91cd\u8f7d + \u8fd0\u7b97\u7b26\u6765\u8fde\u63a5\u4f60\u7684\u7528\u6237\u5b9a\u4e49\u7684\u5b57\u7b26\u4e32\u7c7b\uff0c\u6216\u5c06\u4e24\u4e2a Fraction \u7c7b\u5bf9\u8c61\u76f8\u52a0\u3002\u4f60\u53ef\u4ee5\u91cd\u8f7d << \u8fd0\u7b97\u7b26\uff0c\u4ee5\u4fbf\u8f7b\u677e\u5c06\u4f60\u7684\u7c7b\u6253\u5370\u5230\u5c4f\u5e55\uff08\u6216\u6587\u4ef6\uff09\u3002\u4f60\u53ef\u4ee5\u91cd\u8f7d\u7b49\u5f0f\u8fd0\u7b97\u7b26\uff08 == \uff09\u6765\u6bd4\u8f83\u4e24\u4e2a\u7c7b\u5bf9\u8c61\u3002\u8fd9\u4f7f\u5f97\u8fd0\u7b97\u7b26\u91cd\u8f7d\u6210\u4e3aC ++\u4e2d\u6700\u6709\u7528\u7684\u529f\u80fd\u4e4b\u4e00 \u2014\u2014 \u56e0\u4e3a\u5b83\u5141\u8bb8\u4f60\u4ee5\u66f4\u76f4\u89c2\u7684\u65b9\u5f0f\u4f7f\u7528\u4f60\u7684\u7c7b\u3002 \u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u66f4\u6df1\u5165\u5730\u7814\u7a76\u91cd\u8f7d\u4e0d\u540c\u7c7b\u578b\u7684\u8fd0\u7b97\u7b26\u3002","title":"\u64cd\u4f5c\u7b26\u91cd\u8f7d\u7684\u9650\u5236\u662f\u4ec0\u4e48\uff1f"},{"location":"14-10-overloading-the-parenthesis-operator/","tags":["overload","operator","parenthesis"],"text":"Key Takeaway All of the overloaded operators you have seen so far let you define the type of the operator\u2019s parameters, but not the number of parameters (which is fixed based on the type of the operator). For example, operator== always takes two parameters, whereas operator! always takes one. The parenthesis operator (operator()) is a particularly interesting operator in that it allows you to vary both the type AND number of parameters it takes. There are two things to keep in mind: first, the parenthesis operator must be implemented as a member function. Second, in non-object-oriented C++, the () operator is used to call functions. In the case of classes, operator() is just a normal operator that calls a function (named operator()) like any other overloaded operator. An example Let\u2019s take a look at an example that lends itself to overloading this operator: 1 2 3 4 5 class Matrix { private : double data [ 4 ][ 4 ]{}; }; COPY Matrices are a key component of linear algebra, and are often used to do geometric modeling and 3D computer graphics work. In this case, all you need to recognize is that the Matrix class is a 4 by 4 two-dimensional array of doubles. In the lesson on overloading the subscript operator , you learned that we could overload operator[] to provide direct access to a private one-dimensional array. However, in this case, we want access to a private two-dimensional array. Because operator[] is limited to a single parameter, it is not sufficient to let us index a two-dimensional array. However, because the () operator can take as many parameters as we want it to have, we can declare a version of operator() that takes two integer index parameters, and use it to access our two-dimensional array. Here is an example of this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include // for assert() class Matrix { private : double m_data [ 4 ][ 4 ]{}; public : double & operator ()( int row , int col ); double operator ()( int row , int col ) const ; // for const objects }; double & Matrix::operator ()( int row , int col ) { assert ( col >= 0 && col < 4 ); assert ( row >= 0 && row < 4 ); return m_data [ row ][ col ]; } double Matrix::operator ()( int row , int col ) const { assert ( col >= 0 && col < 4 ); assert ( row >= 0 && row < 4 ); return m_data [ row ][ col ]; } COPY Now we can declare a Matrix and access its elements like this: 1 2 3 4 5 6 7 8 9 10 #include int main () { Matrix matrix ; matrix ( 1 , 2 ) = 4.5 ; std :: cout << matrix ( 1 , 2 ) << '\\n' ; return 0 ; } COPY which produces the result: 4.5 Now, let\u2019s overload the () operator again, this time in a way that takes no parameters at all: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include // for assert() class Matrix { private : double m_data [ 4 ][ 4 ]{}; public : double & operator ()( int row , int col ); double operator ()( int row , int col ) const ; void operator ()(); }; double & Matrix::operator ()( int row , int col ) { assert ( col >= 0 && col < 4 ); assert ( row >= 0 && row < 4 ); return m_data [ row ][ col ]; } double Matrix::operator ()( int row , int col ) const { assert ( col >= 0 && col < 4 ); assert ( row >= 0 && row < 4 ); return m_data [ row ][ col ]; } void Matrix::operator ()() { // reset all elements of the matrix to 0.0 for ( int row { 0 }; row < 4 ; ++ row ) { for ( int col { 0 }; col < 4 ; ++ col ) { m_data [ row ][ col ] = 0.0 ; } } } COPY And here\u2019s our new example: 1 2 3 4 5 6 7 8 9 10 11 #include int main () { Matrix matrix {}; matrix ( 1 , 2 ) = 4.5 ; matrix (); // erase matrix std :: cout << matrix ( 1 , 2 ) << '\\n' ; return 0 ; } COPY which produces the result: 0 Because the () operator is so flexible, it can be tempting to use it for many different purposes. However, this is strongly discouraged, since the () symbol does not really give any indication of what the operator is doing. In our example above, it would be better to have written the erase functionality as a function called clear() or erase(), as matrix.erase() is easier to understand than matrix() (which could do anything!). Having fun with functors Operator() is also commonly overloaded to implement functors (or function object ), which are classes that operate like functions. The advantage of a functor over a normal function is that functors can store data in member variables (since they are classes). Here\u2019s a simple functor: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include class Accumulator { private : int m_counter { 0 }; public : int operator () ( int i ) { return ( m_counter += i ); } }; int main () { Accumulator acc {}; std :: cout << acc ( 10 ) << '\\n' ; // prints 10 std :: cout << acc ( 20 ) << '\\n' ; // prints 30 return 0 ; } COPY Note that using our Accumulator looks just like making a normal function call, but our Accumulator object is storing an accumulated value. You may wonder why we couldn\u2019t do the same thing with a normal function and a static local variable to preserve data between function calls. We could, but because functions only have one global instance, we\u2019d be limited to using it for one thing at a time. With functors, we can instantiate as many separate functor objects as we need and use them all simultaneously. Conclusion Operator() is sometimes overloaded with two parameters to index multidimensional arrays, or to retrieve a subset of a one dimensional array (with the two parameters defining the subset to return). Anything else is probably better written as a member function with a more descriptive name. Operator() is also often overloaded to create functors. Although simple functors (such as the example above) are fairly easily understood, functors are typically used in more advanced programming topics, and deserve their own lesson.","title":"14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26"},{"location":"14-10-overloading-the-parenthesis-operator/#conclusion","text":"Operator() is sometimes overloaded with two parameters to index multidimensional arrays, or to retrieve a subset of a one dimensional array (with the two parameters defining the subset to return). Anything else is probably better written as a member function with a more descriptive name. Operator() is also often overloaded to create functors. Although simple functors (such as the example above) are fairly easily understood, functors are typically used in more advanced programming topics, and deserve their own lesson.","title":"Conclusion"},{"location":"14-11-Overloading-typecasts/","tags":["overload","typecast"],"text":"Key Takeaway In lesson 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast , you learned that C++ allows you to convert one data type to another. The following example shows an int being converted into a double: 1 2 int n { 5 }; auto d { static_cast < double > ( n ) }; // int cast to a double COPY C++ already knows how to convert between the built-in data types. However, it does not know how to convert any of our user-defined classes. That\u2019s where overloading the typecast operators comes into play. User-defined conversions allow us to convert our class into another data type. Take a look at the following class: 1 2 3 4 5 6 7 8 9 10 11 12 13 class Cents { private : int m_cents ; public : Cents ( int cents = 0 ) : m_cents { cents } { } int getCents () const { return m_cents ; } void setCents ( int cents ) { m_cents = cents ; } }; COPY This class is pretty simple: it holds some number of cents as an integer, and provides access functions to get and set the number of cents. It also provides a constructor for converting an int into a Cents. If we can convert an int into a Cents, then doesn\u2019t it also make sense for us to be able to convert a Cents back into an int? In some cases, this might not be true, but in this case, it does make sense. In the following example, we have to use getCents() to convert our Cents variable back into an integer so we can print it using printInt(): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include void printInt ( int value ) { std :: cout << value ; } int main () { Cents cents { 7 }; printInt ( cents . getCents ()); // print 7 std :: cout << '\\n' ; return 0 ; } COPY If we have already written a lot of functions that take integers as parameters, our code will be littered with calls to getCents(), which makes it more messy than it needs to be. To make things easier, we can provide a user-defined conversion by overloading the int typecast. This will allow us to cast our Cents class directly into an int. The following example shows how this is done: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Cents { private : int m_cents ; public : Cents ( int cents = 0 ) : m_cents { cents } { } // Overloaded int cast operator int () const { return m_cents ; } int getCents () const { return m_cents ; } void setCents ( int cents ) { m_cents = cents ; } }; COPY There are three things to note: To overload the function that casts our class to an int, we write a new function in our class called operator int(). Note that there is a space between the word operator and the type we are casting to. User-defined conversions do not take parameters, as there is no way to pass arguments to them. User-defined conversions do not have a return type. C++ assumes you will be returning the correct type. Now in our example, we can call printInt() like this: 1 2 3 4 5 6 7 8 9 10 11 #include int main () { Cents cents { 7 }; printInt ( cents ); // print 7 std :: cout << '\\n' ; return 0 ; } COPY The compiler will first note that function printInt takes an integer parameter. Then it will note that variable cents is not an int. Finally, it will look to see if we\u2019ve provided a way to convert a Cents into an int. Since we have, it will call our operator int() function, which returns an int, and the returned int will be passed to printInt(). We can now also explicitly cast our Cents variable to an int: 1 2 Cents cents { 7 }; int c { static_cast < int > ( cents ) }; COPY You can provide user-defined conversions for any data type you wish, including your own user-defined data types! Here\u2019s a new class called Dollars that provides an overloaded Cents conversion: 1 2 3 4 5 6 7 8 9 10 11 12 13 class Dollars { private : int m_dollars ; public : Dollars ( int dollars = 0 ) : m_dollars { dollars } { } // Allow us to convert Dollars into Cents operator Cents () const { return Cents { m_dollars * 100 }; } }; COPY This allows us to convert a Dollars object directly into a Cents object! This allows you to do something like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include class Cents { private : int m_cents ; public : Cents ( int cents = 0 ) : m_cents { cents } { } // Overloaded int cast operator int () const { return m_cents ; } int getCents () const { return m_cents ; } void setCents ( int cents ) { m_cents = cents ; } }; class Dollars { private : int m_dollars ; public : Dollars ( int dollars = 0 ) : m_dollars { dollars } { } // Allow us to convert Dollars into Cents operator Cents () const { return Cents { m_dollars * 100 }; } }; void printCents ( Cents cents ) { std :: cout << cents ; // cents will be implicitly cast to an int here } int main () { Dollars dollars { 9 }; printCents ( dollars ); // dollars will be implicitly cast to a Cents here std :: cout << '\\n' ; return 0 ; } COPY Consequently, this program will print the value: 1 900 which makes sense, since 9 dollars is 900 cents!","title":"14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26"},{"location":"14-12-the-copy-constructor/","tags":["copy constructor","constructor"],"text":"Key Takeaway - \u521d\u59cb\u5316\u56de\u987e \u7531\u4e8e\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u51e0\u8282\u8bfe\u4e2d\u8ba8\u8bba\u521d\u59cb\u5316\uff0c\u6240\u4ee5\u6709\u5fc5\u8981\u5148\u56de\u987e\u4e00\u4e0b C++ \u652f\u6301\u7684\u51e0\u79cd\u521d\u59cb\u5316\u65b9\u5f0f\uff1a \u76f4\u63a5\u521d\u59cb\u5316 \uff08\u4f7f\u7528\u62ec\u53f7\uff09\u3001 \u7edf\u4e00\u521d\u59cb\u5316 \uff08\u5927\u62ec\u53f7\uff09\u548c \u62f7\u8d1d\u521d\u59cb\u5316 \uff08\u4f7f\u7528\u7b49\u4e8e\u53f7\uff09\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u540c\u65f6\u4f7f\u7528\u4e86\u4e0a\u8ff0\u4e09\u79cd\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } \u76f4\u63a5\u521d\u59cb\u5316\uff1a 1 2 int x ( 5 ); // \u76f4\u63a5\u521d\u59cb\u5316\u4e00\u4e2a\u6574\u578b Fraction fiveThirds ( 5 , 3 ); // \u76f4\u63a5\u521d\u59cb\u5316 Fraction\uff0c\u5373\u8c03\u7528\u6784\u9020\u51fd\u6570 Fraction(int, int) \u5728 C++11 \u4e2d\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7edf\u4e00\u521d\u59cb\u5316\uff1a 1 2 int x { 5 }; // \u7edf\u4e00\u521d\u59cb\u5316\u4e00\u4e2a\u6574\u578b Fraction fiveThirds { 5 , 3 }; // \u7edf\u4e00\u521d\u59cb\u5316 Fraction \uff0c\u4ecd\u7136\u662f\u8c03\u7528\u6784\u9020\u51fd\u6570 Fraction(int, int) \u6700\u540e\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u62f7\u8d1d\u521d\u59cb\u5316\uff1a 1 2 3 int x = 6 ; // \u62f7\u8d1d\u521d\u59cb\u5316\u4e00\u4e2a\u6574\u578b Fraction six = Fraction ( 6 ); // \u62f7\u8d1d\u521d\u59cb\u5316 Fraction, \u4f1a\u8c03\u7528 Fraction(6, 1) Fraction seven = 7 ; // \u62f7\u8d1d\u521d\u59cb\u5316 Fraction\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u5bfb\u627e\u5c06 7 \u8f6c\u6362\u4e3a Fraction \u7684\u65b9\u6cd5\uff0c\u56e0\u6b64\u4f1a\u8c03\u7528\u6784\u9020\u51fd\u6570 Fraction(7, 1) \u901a\u8fc7\u76f4\u63a5\u521d\u59cb\u5316\u6216\u8005\u7edf\u4e00\u521d\u59cb\u5316\uff0c\u5bf9\u8c61\u4f1a\u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u3002\u800c\u5f53\u4f7f\u7528\u62f7\u8d1d\u521d\u59cb\u5316\u65f6\uff0c\u95ee\u9898\u5c31\u6709\u4e00\u70b9\u590d\u6742\u4e86\u3002\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\u4ed4\u7ec6\u63a2\u8ba8\u62f7\u8d1d\u521d\u59cb\u5316\u3002\u4e3a\u4e86\u66f4\u597d\u7684\u6548\u679c\uff0c\u8ba9\u6211\u4eec\u5148\u770b\u770b\u53e6\u5916\u4e00\u4e2a\u8bdd\u9898\u3002 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } int main () { Fraction fiveThirds { 5 , 3 }; // \u7edf\u4e00\u521d\u59cb\u5316 Fraction, \u8c03\u7528 Fraction(int, int) Fraction fCopy { fiveThirds }; // \u7edf\u4e00\u521d\u59cb\u5316 Fraction -- \u8fd9\u91cc\u8c03\u7528\u7684\u662f\u54ea\u4e2a\u6784\u9020\u51fd\u6570\uff1f std :: cout << fCopy << '\\n' ; } \u5f53\u6211\u4eec\u7f16\u8bd1\u6267\u884c\u4e0a\u9762\u7a0b\u5e8f\u65f6\uff0c\u4e00\u5207\u6b63\u5e38\uff0c\u7a0b\u5e8f\u6253\u5370\uff1a 1 5 / 3 \u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u4e0a\u9762\u4ee3\u7801\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002 \u53d8\u91cf fiveThirds \u7684\u521d\u59cb\u5316\u4f7f\u7528\u4e86\u6807\u51c6\u7684\u7edf\u4e00\u521d\u59cb\u5316\u65b9\u5f0f\uff0c\u56e0\u6b64\u4f1a\u8c03\u7528\u6784\u9020\u51fd\u6570 Fraction(int, int) \uff0c\u6ca1\u4ec0\u4e48\u597d\u8bf4\u7684\u3002\u4f46\u662f\u4e0b\u4e00\u884c\u5462\uff1f fCopy \u663e\u7136\u4e5f\u662f\u5728\u521d\u59cb\u5316\uff0c\u800c\u4e14\u521d\u59cb\u5316\u4f1a\u8c03\u7528\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u90a3\u4e48\u4f60\u77e5\u9053\u5b83\u8c03\u7528\u7684\u662f\u4ec0\u4e48\u6784\u9020\u51fd\u6570\u5417\uff1f \u5b9e\u9645\u4e0a\uff0c\u8fd9\u884c\u4ee3\u7801\u4f1a\u8c03\u7528 Fraction \u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u662f\u4e00\u7c7b\u7279\u6b8a\u7684\u6784\u9020\u51fd\u6570\uff0c\u5b83\u4f1a\u901a\u8fc7\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u5bf9\u8c61\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\uff08\u76f8\u540c\u7c7b\u578b\uff09\u3002\u7c7b\u4f3c\u4e8e\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u5982\u679c\u4f60\u4e0d\u63d0\u4f9b\u4e00\u4e2a\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u7684\u8bdd\uff0cC++ \u4f1a\u4e3a\u4f60\u521b\u5efa\u4e00\u4e2apublic\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002\u7531\u4e8e\u7f16\u8bd1\u5668\u5e76\u4e0d\u5f88\u4e86\u89e3\u4f60\u7684\u7c7b\uff0c\u6240\u4ee5\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4f1a\u8fdb\u884c \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \u3002\u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316\u610f\u601d\u5c31\u662f\u6bcf\u4e2a\u6210\u5458\u90fd\u4f1a\u901a\u8fc7\u88ab\u62f7\u8d1d\u5bf9\u8c61\u4e2d\u7684\u5bf9\u5e94\u6210\u5458\u8fdb\u884c\u62f7\u8d1d\u521d\u59cb\u5316\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c fCopy.m_numerator \u4f1a\u901a\u8fc7 fiveThirds.m_numerator \u6765\u521d\u59cb\u5316\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002 \u548c\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u7c7b\u4f3c\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u663e\u5f0f\u5730\u5b9a\u4e49\u4e00\u4e2a\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u5b83\u7684\u5f62\u5f0f\u60f3\u5fc5\u4f60\u5e94\u8be5\u53ef\u4ee5\u731c\u5230\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : // \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } // \u62f7\u8d1d\u6784\u9020\u51fd\u6570 Fraction ( const Fraction & fraction ) : m_numerator { fraction . m_numerator }, m_denominator { fraction . m_denominator } // \u6ce8\u610f\uff1a\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u53c2\u6570 fraction \u7684\u6210\u5458\u53d8\u91cf\uff0c\u56e0\u4e3a\u5b83\u4eec\u90fd\u662f Fraction \u7c7b\u7684 { // no need to check for a denominator of 0 here since fraction must already be a valid Fraction std :: cout << \"Copy constructor called \\n \" ; // just to prove it works } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } int main () { Fraction fiveThirds { 5 , 3 }; // \u76f4\u63a5\u521d\u59cb\u5316\uff0c\u8c03\u7528 Fraction(int, int) \u6784\u9020\u51fd\u6570 Fraction fCopy { fiveThirds }; // \u76f4\u63a5\u521d\u59cb\u5316\u2014\u2014\u4f7f\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570 std :: cout << fCopy << '\\n' ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Copy constructor called 5/3 \u6211\u4eec\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u5b9a\u4e49\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4f7f\u7528\u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316\u65b9\u5f0f\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u5728\u529f\u80fd\u4e0a\u4e0e\u7f16\u8bd1\u5668\u9ed8\u8ba4\u521b\u5efa\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u76f8\u540c\uff0c\u53ea\u662f\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e2a\u8f93\u51fa\u8bed\u53e5\u6765\u8bc1\u660e\u6b63\u5728\u8c03\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002 \u4e0e\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u4e0d\u540c\u7684\u662f\uff0c\u5982\u679c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u80fd\u591f\u6ee1\u8db3\u4f60\u7684\u9700\u8981\uff0c\u90a3\u4e48\u5c3d\u7ba1\u4f7f\u7528\u5b83\u3002 \u6709\u4e00\u4e2a\u5730\u65b9\u9700\u8981\u6ce8\u610f\uff1a\u5728\u4e4b\u524d\u6211\u4eec\u5df2\u7ecf\u770b\u5230\u4e86\u4e00\u4e9b\u91cd\u8f7d\u64cd\u4f5c\u7b26 << \u7684\u4f8b\u5b50\uff0c\u5728\u8fd9\u4e9b\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u80fd\u591f\u8bbf\u95ee\u5f62\u53c2 f1 \u7684\u79c1\u6709\u6210\u5458\uff0c\u56e0\u4e3a\u8be5\u51fd\u6570\u662f Fraction \u7c7b\u7684\u53cb\u5143\u3002\u7c7b\u4f3c\u5730\uff0c\u7c7b\u7684\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u76f8\u540c\u7c7b\u5f62\u53c2\u7684\u79c1\u6709\u6210\u5458\u3002\u56e0\u4e3a Fraction \u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u63a5\u53d7\u540c\u7c7b\u578b\u7684\u5f62\u53c2(\u7528\u4e8e\u590d\u5236)\uff0c\u6240\u4ee5\u6211\u4eec\u80fd\u591f\u76f4\u63a5\u8bbf\u95ee\u5f62\u53c2 Fraction \u7684\u6210\u5458\u3002 \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u7684\u5f62\u53c2\u5fc5\u987b\u662f\u5f15\u7528\u7c7b\u578b \u590d\u5236\u6784\u9020\u51fd\u6570\u7684\u5f62\u53c2\u5fc5\u987b\u662f(const)\u5f15\u7528\u3002\u8fd9\u662f\u6709\u610f\u4e49\u7684\uff1a\u5982\u679c\u5b9e\u53c2\u662f\u6309\u503c\u4f20\u9012\u7684\uff0c\u90a3\u4e48\u6211\u4eec\u9700\u8981\u590d\u5236\u6784\u9020\u51fd\u6570\u5c06\u5b9e\u62f7\u8d1d\u5230\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u7684\u5f62\u53c2\u4e2d(\u8fd9\u5c06\u5bfc\u81f4\u65e0\u9650\u9012\u5f52)\u3002 \u963b\u6b62\u62f7\u8d1d \u901a\u8fc7\u5c06\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u8bbe\u4e3a \u79c1\u6709\u6210\u5458 \uff0c\u6211\u4eec\u53ef\u4ee5\u963b\u6b62\u7c7b\u7684\u62f7\u8d1d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include class Fraction { private : int m_numerator {}; int m_denominator {}; // \u79c1\u6709\u62f7\u8d1d\u6784\u9020\u51fd\u6570 Fraction ( const Fraction & fraction ) : m_numerator { fraction . m_numerator }, m_denominator { fraction . m_denominator } { // no need to check for a denominator of 0 here since fraction must already be a valid Fraction std :: cout << \"Copy constructor called \\n \" ; // just to prove it works } public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } int main () { Fraction fiveThirds { 5 , 3 }; // \u76f4\u63a5\u521d\u59cb\u5316 Fraction\uff0c\u8c03\u7528 Fraction(int, int) Fraction fCopy { fiveThirds }; // \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u662f\u79c1\u6709\u7684\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002 std :: cout << fCopy << '\\n' ; } \u7f16\u8bd1\u7a0b\u5e8f\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\uff0c\u56e0\u4e3a fCopy \u9700\u8981\u7528\u5230\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u4f46\u662f\u7531\u4e8e\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u662f\u79c1\u6709\u7684\uff0c\u5b83\u65e0\u6743\u8bbf\u95ee\u3002 \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u53ef\u80fd\u4f1a\u88ab\u7701\u7565 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } // Copy constructor Fraction ( const Fraction & fraction ) : m_numerator { fraction . m_numerator }, m_denominator { fraction . m_denominator } { // no need to check for a denominator of 0 here since fraction must already be a valid Fraction std :: cout << \"Copy constructor called \\n \" ; // just to prove it works } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } int main () { Fraction fiveThirds { Fraction { 5 , 3 } }; std :: cout << fiveThirds ; return 0 ; } \u8003\u8651\u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8fd0\u884c\u903b\u8f91\u3002\u9996\u5148\uff0c\u6211\u4eec\u76f4\u63a5\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u533f\u540d\u7684 Fraction \u5bf9\u8c61\uff08\u4f7f\u7528 Fraction(int, int) \u6784\u9020\u51fd\u6570\uff09\u3002\u968f\u540e\uff0c\u6211\u4eec\u5c06\u8be5\u533f\u540d\u5bf9\u8c61\u4f5c\u4e3a\u521d\u59cb\u5316\u503c\uff0c\u7528\u4e8e\u521d\u59cb\u5316 Fraction fiveThirds \u3002\u56e0\u4e3a\u533f\u540d\u5bf9\u8c61\u4e5f\u662f Fraction \u7c7b\u578b\u7684\uff0c\u6240\u4ee5\u4f1a\u8c03\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u8fd9\u4e48\u5206\u6790\u6ca1\u9519\u5427\uff1f \u7f16\u8bd1\u5e76\u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u4f60\u53ef\u4ee5\u4f1a\u8ba4\u4e3a\u5176\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 copy constructor called 5/3 \u800c\u5b9e\u9645\u4e0a\u7ed3\u679c\u66f4\u53ef\u80fd\u662f\uff1a 1 5/3 \u4e3a\u4ec0\u4e48\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u6ca1\u6709\u88ab\u8c03\u7528\uff1f \u6ce8\u610f\uff0c\u521d\u59cb\u5316\u533f\u540d\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528\u8be5\u5bf9\u8c61\u76f4\u63a5\u521d\u59cb\u5316\u5b9a\u4e49\u7684\u5bf9\u8c61\u9700\u8981\u4e24\u4e2a\u6b65\u9aa4(\u9996\u5148\u521b\u5efa\u533f\u540d\u5bf9\u8c61\uff0c\u7136\u8c03\u7528\u590d\u5236\u6784\u9020\u51fd\u6570)\u3002\u7136\u800c\uff0c\u521d\u59cb\u5316\u5df2\u5b9a\u4e49\u5bf9\u8c61\u7684\u6700\u7ec8\u7ed3\u679c\u57fa\u672c\u4e0a\u4e0e\u76f4\u63a5\u521d\u59cb\u5316\u76f8\u540c\uff0c\u540e\u8005\u53ea\u9700\u8981\u4e00\u4e2a\u6b65\u9aa4\u3002 \u56e0\u6b64\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u9009\u62e9\u4e0d\u8c03\u7528\u590d\u5236\u6784\u9020\u51fd\u6570\uff0c\u800c\u76f4\u63a5\u8fdb\u884c\u521d\u59cb\u5316\u3002\u4e3a\u4e86\u6027\u80fd\u76ee\u7684\u800c\u7701\u7565\u67d0\u4e9b\u590d\u5236(\u6216\u79fb\u52a8)\u6b65\u9aa4\u7684\u8fc7\u7a0b\u79f0\u4e3a \u7701\u7565 \u6240\u4ee5\u5c3d\u7ba1\u4ee3\u7801\u662f\u8fd9\u6837\u5199\u7684\uff1a 1 Fraction fiveThirds { Fraction { 5 , 3 } }; \u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5c06\u5176\u4f18\u5316\u4e3a\uff1a 1 Fraction fiveThirds { 5 , 3 }; \u4e0a\u9762\u7684\u5f62\u5f0f\u53ea\u9700\u8981\u4e00\u6b21\u6784\u9020\u51fd\u6570\u8c03\u7528( Fraction(int, int) )\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u7701\u7565\u53d1\u751f\u65f6\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u5176\u51fd\u6570\u4f53\u5185\u7684\u4efb\u4f55\u8bed\u53e5\u90fd\u4e0d\u4f1a\u88ab\u6267\u884c\uff0c\u5373\u4f7f\u5b83\u4eec\u4f1a\u4ea7\u751f \u526f\u4f5c\u7528 \uff08\u4f8b\u5982\u8f93\u51fa\u4fe1\u606f\u5230\u5c4f\u5e55\uff09\uff01 \u5728 C++17 \u4e4b\u524d\uff0c\u7f16\u8bd1\u5668\u88ab\u5141\u8bb8\uff08\u4f46\u4e0d\u662f\u5fc5\u987b\uff09\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6267\u884c\u62f7\u8d1d\u7701\u7565\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4ecd\u7136\u662f\u5fc5\u987b\u53ef\u8bbf\u95ee\u7684\uff08\u975e\u79c1\u6709\uff09\uff0c\u5373\u4f7f\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\u88ab\u8c03\u7528\u3002 \u5230\u4e86 C++17\uff0c\u6709\u4e9b\u62f7\u8d1d\u7701\u7565\u5df2\u7ecf\u662f\u5f3a\u5236\u7684\u4e86\uff08\u5305\u62ec\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u8fd9\u79cd\u60c5\u51b5\uff09\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u5f3a\u5236\u7701\u7565\u7684\u60c5\u51b5\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4e0d\u9700\u8981\u662f\u53ef\u8bbf\u95ee\u7684\uff0c\u751a\u81f3\u4e0d\u9700\u8981\u88ab\u5b9a\u4e49\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u4fdd\u8bc1\u4e0d\u8c03\u7528\u5b83\uff01","title":"14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570"},{"location":"14-12-the-copy-constructor/#_1","text":"\u7531\u4e8e\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u51e0\u8282\u8bfe\u4e2d\u8ba8\u8bba\u521d\u59cb\u5316\uff0c\u6240\u4ee5\u6709\u5fc5\u8981\u5148\u56de\u987e\u4e00\u4e0b C++ \u652f\u6301\u7684\u51e0\u79cd\u521d\u59cb\u5316\u65b9\u5f0f\uff1a \u76f4\u63a5\u521d\u59cb\u5316 \uff08\u4f7f\u7528\u62ec\u53f7\uff09\u3001 \u7edf\u4e00\u521d\u59cb\u5316 \uff08\u5927\u62ec\u53f7\uff09\u548c \u62f7\u8d1d\u521d\u59cb\u5316 \uff08\u4f7f\u7528\u7b49\u4e8e\u53f7\uff09\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u540c\u65f6\u4f7f\u7528\u4e86\u4e0a\u8ff0\u4e09\u79cd\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } \u76f4\u63a5\u521d\u59cb\u5316\uff1a 1 2 int x ( 5 ); // \u76f4\u63a5\u521d\u59cb\u5316\u4e00\u4e2a\u6574\u578b Fraction fiveThirds ( 5 , 3 ); // \u76f4\u63a5\u521d\u59cb\u5316 Fraction\uff0c\u5373\u8c03\u7528\u6784\u9020\u51fd\u6570 Fraction(int, int) \u5728 C++11 \u4e2d\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7edf\u4e00\u521d\u59cb\u5316\uff1a 1 2 int x { 5 }; // \u7edf\u4e00\u521d\u59cb\u5316\u4e00\u4e2a\u6574\u578b Fraction fiveThirds { 5 , 3 }; // \u7edf\u4e00\u521d\u59cb\u5316 Fraction \uff0c\u4ecd\u7136\u662f\u8c03\u7528\u6784\u9020\u51fd\u6570 Fraction(int, int) \u6700\u540e\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u62f7\u8d1d\u521d\u59cb\u5316\uff1a 1 2 3 int x = 6 ; // \u62f7\u8d1d\u521d\u59cb\u5316\u4e00\u4e2a\u6574\u578b Fraction six = Fraction ( 6 ); // \u62f7\u8d1d\u521d\u59cb\u5316 Fraction, \u4f1a\u8c03\u7528 Fraction(6, 1) Fraction seven = 7 ; // \u62f7\u8d1d\u521d\u59cb\u5316 Fraction\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u5bfb\u627e\u5c06 7 \u8f6c\u6362\u4e3a Fraction \u7684\u65b9\u6cd5\uff0c\u56e0\u6b64\u4f1a\u8c03\u7528\u6784\u9020\u51fd\u6570 Fraction(7, 1) \u901a\u8fc7\u76f4\u63a5\u521d\u59cb\u5316\u6216\u8005\u7edf\u4e00\u521d\u59cb\u5316\uff0c\u5bf9\u8c61\u4f1a\u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u3002\u800c\u5f53\u4f7f\u7528\u62f7\u8d1d\u521d\u59cb\u5316\u65f6\uff0c\u95ee\u9898\u5c31\u6709\u4e00\u70b9\u590d\u6742\u4e86\u3002\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\u4ed4\u7ec6\u63a2\u8ba8\u62f7\u8d1d\u521d\u59cb\u5316\u3002\u4e3a\u4e86\u66f4\u597d\u7684\u6548\u679c\uff0c\u8ba9\u6211\u4eec\u5148\u770b\u770b\u53e6\u5916\u4e00\u4e2a\u8bdd\u9898\u3002","title":"\u521d\u59cb\u5316\u56de\u987e"},{"location":"14-12-the-copy-constructor/#_2","text":"\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } int main () { Fraction fiveThirds { 5 , 3 }; // \u7edf\u4e00\u521d\u59cb\u5316 Fraction, \u8c03\u7528 Fraction(int, int) Fraction fCopy { fiveThirds }; // \u7edf\u4e00\u521d\u59cb\u5316 Fraction -- \u8fd9\u91cc\u8c03\u7528\u7684\u662f\u54ea\u4e2a\u6784\u9020\u51fd\u6570\uff1f std :: cout << fCopy << '\\n' ; } \u5f53\u6211\u4eec\u7f16\u8bd1\u6267\u884c\u4e0a\u9762\u7a0b\u5e8f\u65f6\uff0c\u4e00\u5207\u6b63\u5e38\uff0c\u7a0b\u5e8f\u6253\u5370\uff1a 1 5 / 3 \u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u4e0a\u9762\u4ee3\u7801\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002 \u53d8\u91cf fiveThirds \u7684\u521d\u59cb\u5316\u4f7f\u7528\u4e86\u6807\u51c6\u7684\u7edf\u4e00\u521d\u59cb\u5316\u65b9\u5f0f\uff0c\u56e0\u6b64\u4f1a\u8c03\u7528\u6784\u9020\u51fd\u6570 Fraction(int, int) \uff0c\u6ca1\u4ec0\u4e48\u597d\u8bf4\u7684\u3002\u4f46\u662f\u4e0b\u4e00\u884c\u5462\uff1f fCopy \u663e\u7136\u4e5f\u662f\u5728\u521d\u59cb\u5316\uff0c\u800c\u4e14\u521d\u59cb\u5316\u4f1a\u8c03\u7528\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u90a3\u4e48\u4f60\u77e5\u9053\u5b83\u8c03\u7528\u7684\u662f\u4ec0\u4e48\u6784\u9020\u51fd\u6570\u5417\uff1f \u5b9e\u9645\u4e0a\uff0c\u8fd9\u884c\u4ee3\u7801\u4f1a\u8c03\u7528 Fraction \u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u662f\u4e00\u7c7b\u7279\u6b8a\u7684\u6784\u9020\u51fd\u6570\uff0c\u5b83\u4f1a\u901a\u8fc7\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u5bf9\u8c61\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\uff08\u76f8\u540c\u7c7b\u578b\uff09\u3002\u7c7b\u4f3c\u4e8e\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u5982\u679c\u4f60\u4e0d\u63d0\u4f9b\u4e00\u4e2a\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u7684\u8bdd\uff0cC++ \u4f1a\u4e3a\u4f60\u521b\u5efa\u4e00\u4e2apublic\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002\u7531\u4e8e\u7f16\u8bd1\u5668\u5e76\u4e0d\u5f88\u4e86\u89e3\u4f60\u7684\u7c7b\uff0c\u6240\u4ee5\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4f1a\u8fdb\u884c \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \u3002\u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316\u610f\u601d\u5c31\u662f\u6bcf\u4e2a\u6210\u5458\u90fd\u4f1a\u901a\u8fc7\u88ab\u62f7\u8d1d\u5bf9\u8c61\u4e2d\u7684\u5bf9\u5e94\u6210\u5458\u8fdb\u884c\u62f7\u8d1d\u521d\u59cb\u5316\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c fCopy.m_numerator \u4f1a\u901a\u8fc7 fiveThirds.m_numerator \u6765\u521d\u59cb\u5316\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002 \u548c\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u7c7b\u4f3c\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u663e\u5f0f\u5730\u5b9a\u4e49\u4e00\u4e2a\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u5b83\u7684\u5f62\u5f0f\u60f3\u5fc5\u4f60\u5e94\u8be5\u53ef\u4ee5\u731c\u5230\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : // \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } // \u62f7\u8d1d\u6784\u9020\u51fd\u6570 Fraction ( const Fraction & fraction ) : m_numerator { fraction . m_numerator }, m_denominator { fraction . m_denominator } // \u6ce8\u610f\uff1a\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u53c2\u6570 fraction \u7684\u6210\u5458\u53d8\u91cf\uff0c\u56e0\u4e3a\u5b83\u4eec\u90fd\u662f Fraction \u7c7b\u7684 { // no need to check for a denominator of 0 here since fraction must already be a valid Fraction std :: cout << \"Copy constructor called \\n \" ; // just to prove it works } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } int main () { Fraction fiveThirds { 5 , 3 }; // \u76f4\u63a5\u521d\u59cb\u5316\uff0c\u8c03\u7528 Fraction(int, int) \u6784\u9020\u51fd\u6570 Fraction fCopy { fiveThirds }; // \u76f4\u63a5\u521d\u59cb\u5316\u2014\u2014\u4f7f\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570 std :: cout << fCopy << '\\n' ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Copy constructor called 5/3 \u6211\u4eec\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u5b9a\u4e49\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4f7f\u7528\u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316\u65b9\u5f0f\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u5728\u529f\u80fd\u4e0a\u4e0e\u7f16\u8bd1\u5668\u9ed8\u8ba4\u521b\u5efa\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u76f8\u540c\uff0c\u53ea\u662f\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e2a\u8f93\u51fa\u8bed\u53e5\u6765\u8bc1\u660e\u6b63\u5728\u8c03\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002 \u4e0e\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u4e0d\u540c\u7684\u662f\uff0c\u5982\u679c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u80fd\u591f\u6ee1\u8db3\u4f60\u7684\u9700\u8981\uff0c\u90a3\u4e48\u5c3d\u7ba1\u4f7f\u7528\u5b83\u3002 \u6709\u4e00\u4e2a\u5730\u65b9\u9700\u8981\u6ce8\u610f\uff1a\u5728\u4e4b\u524d\u6211\u4eec\u5df2\u7ecf\u770b\u5230\u4e86\u4e00\u4e9b\u91cd\u8f7d\u64cd\u4f5c\u7b26 << \u7684\u4f8b\u5b50\uff0c\u5728\u8fd9\u4e9b\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u80fd\u591f\u8bbf\u95ee\u5f62\u53c2 f1 \u7684\u79c1\u6709\u6210\u5458\uff0c\u56e0\u4e3a\u8be5\u51fd\u6570\u662f Fraction \u7c7b\u7684\u53cb\u5143\u3002\u7c7b\u4f3c\u5730\uff0c\u7c7b\u7684\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u76f8\u540c\u7c7b\u5f62\u53c2\u7684\u79c1\u6709\u6210\u5458\u3002\u56e0\u4e3a Fraction \u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u63a5\u53d7\u540c\u7c7b\u578b\u7684\u5f62\u53c2(\u7528\u4e8e\u590d\u5236)\uff0c\u6240\u4ee5\u6211\u4eec\u80fd\u591f\u76f4\u63a5\u8bbf\u95ee\u5f62\u53c2 Fraction \u7684\u6210\u5458\u3002 \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u7684\u5f62\u53c2\u5fc5\u987b\u662f\u5f15\u7528\u7c7b\u578b \u590d\u5236\u6784\u9020\u51fd\u6570\u7684\u5f62\u53c2\u5fc5\u987b\u662f(const)\u5f15\u7528\u3002\u8fd9\u662f\u6709\u610f\u4e49\u7684\uff1a\u5982\u679c\u5b9e\u53c2\u662f\u6309\u503c\u4f20\u9012\u7684\uff0c\u90a3\u4e48\u6211\u4eec\u9700\u8981\u590d\u5236\u6784\u9020\u51fd\u6570\u5c06\u5b9e\u62f7\u8d1d\u5230\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u7684\u5f62\u53c2\u4e2d(\u8fd9\u5c06\u5bfc\u81f4\u65e0\u9650\u9012\u5f52)\u3002","title":"\u62f7\u8d1d\u6784\u9020\u51fd\u6570"},{"location":"14-12-the-copy-constructor/#_3","text":"\u901a\u8fc7\u5c06\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u8bbe\u4e3a \u79c1\u6709\u6210\u5458 \uff0c\u6211\u4eec\u53ef\u4ee5\u963b\u6b62\u7c7b\u7684\u62f7\u8d1d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include class Fraction { private : int m_numerator {}; int m_denominator {}; // \u79c1\u6709\u62f7\u8d1d\u6784\u9020\u51fd\u6570 Fraction ( const Fraction & fraction ) : m_numerator { fraction . m_numerator }, m_denominator { fraction . m_denominator } { // no need to check for a denominator of 0 here since fraction must already be a valid Fraction std :: cout << \"Copy constructor called \\n \" ; // just to prove it works } public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } int main () { Fraction fiveThirds { 5 , 3 }; // \u76f4\u63a5\u521d\u59cb\u5316 Fraction\uff0c\u8c03\u7528 Fraction(int, int) Fraction fCopy { fiveThirds }; // \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u662f\u79c1\u6709\u7684\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002 std :: cout << fCopy << '\\n' ; } \u7f16\u8bd1\u7a0b\u5e8f\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\uff0c\u56e0\u4e3a fCopy \u9700\u8981\u7528\u5230\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u4f46\u662f\u7531\u4e8e\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u662f\u79c1\u6709\u7684\uff0c\u5b83\u65e0\u6743\u8bbf\u95ee\u3002","title":"\u963b\u6b62\u62f7\u8d1d"},{"location":"14-12-the-copy-constructor/#_4","text":"\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include class Fraction { private : int m_numerator {}; int m_denominator {}; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } // Copy constructor Fraction ( const Fraction & fraction ) : m_numerator { fraction . m_numerator }, m_denominator { fraction . m_denominator } { // no need to check for a denominator of 0 here since fraction must already be a valid Fraction std :: cout << \"Copy constructor called \\n \" ; // just to prove it works } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } int main () { Fraction fiveThirds { Fraction { 5 , 3 } }; std :: cout << fiveThirds ; return 0 ; } \u8003\u8651\u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8fd0\u884c\u903b\u8f91\u3002\u9996\u5148\uff0c\u6211\u4eec\u76f4\u63a5\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u533f\u540d\u7684 Fraction \u5bf9\u8c61\uff08\u4f7f\u7528 Fraction(int, int) \u6784\u9020\u51fd\u6570\uff09\u3002\u968f\u540e\uff0c\u6211\u4eec\u5c06\u8be5\u533f\u540d\u5bf9\u8c61\u4f5c\u4e3a\u521d\u59cb\u5316\u503c\uff0c\u7528\u4e8e\u521d\u59cb\u5316 Fraction fiveThirds \u3002\u56e0\u4e3a\u533f\u540d\u5bf9\u8c61\u4e5f\u662f Fraction \u7c7b\u578b\u7684\uff0c\u6240\u4ee5\u4f1a\u8c03\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u8fd9\u4e48\u5206\u6790\u6ca1\u9519\u5427\uff1f \u7f16\u8bd1\u5e76\u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u4f60\u53ef\u4ee5\u4f1a\u8ba4\u4e3a\u5176\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 copy constructor called 5/3 \u800c\u5b9e\u9645\u4e0a\u7ed3\u679c\u66f4\u53ef\u80fd\u662f\uff1a 1 5/3 \u4e3a\u4ec0\u4e48\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u6ca1\u6709\u88ab\u8c03\u7528\uff1f \u6ce8\u610f\uff0c\u521d\u59cb\u5316\u533f\u540d\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528\u8be5\u5bf9\u8c61\u76f4\u63a5\u521d\u59cb\u5316\u5b9a\u4e49\u7684\u5bf9\u8c61\u9700\u8981\u4e24\u4e2a\u6b65\u9aa4(\u9996\u5148\u521b\u5efa\u533f\u540d\u5bf9\u8c61\uff0c\u7136\u8c03\u7528\u590d\u5236\u6784\u9020\u51fd\u6570)\u3002\u7136\u800c\uff0c\u521d\u59cb\u5316\u5df2\u5b9a\u4e49\u5bf9\u8c61\u7684\u6700\u7ec8\u7ed3\u679c\u57fa\u672c\u4e0a\u4e0e\u76f4\u63a5\u521d\u59cb\u5316\u76f8\u540c\uff0c\u540e\u8005\u53ea\u9700\u8981\u4e00\u4e2a\u6b65\u9aa4\u3002 \u56e0\u6b64\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u9009\u62e9\u4e0d\u8c03\u7528\u590d\u5236\u6784\u9020\u51fd\u6570\uff0c\u800c\u76f4\u63a5\u8fdb\u884c\u521d\u59cb\u5316\u3002\u4e3a\u4e86\u6027\u80fd\u76ee\u7684\u800c\u7701\u7565\u67d0\u4e9b\u590d\u5236(\u6216\u79fb\u52a8)\u6b65\u9aa4\u7684\u8fc7\u7a0b\u79f0\u4e3a \u7701\u7565 \u6240\u4ee5\u5c3d\u7ba1\u4ee3\u7801\u662f\u8fd9\u6837\u5199\u7684\uff1a 1 Fraction fiveThirds { Fraction { 5 , 3 } }; \u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5c06\u5176\u4f18\u5316\u4e3a\uff1a 1 Fraction fiveThirds { 5 , 3 }; \u4e0a\u9762\u7684\u5f62\u5f0f\u53ea\u9700\u8981\u4e00\u6b21\u6784\u9020\u51fd\u6570\u8c03\u7528( Fraction(int, int) )\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u7701\u7565\u53d1\u751f\u65f6\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u5176\u51fd\u6570\u4f53\u5185\u7684\u4efb\u4f55\u8bed\u53e5\u90fd\u4e0d\u4f1a\u88ab\u6267\u884c\uff0c\u5373\u4f7f\u5b83\u4eec\u4f1a\u4ea7\u751f \u526f\u4f5c\u7528 \uff08\u4f8b\u5982\u8f93\u51fa\u4fe1\u606f\u5230\u5c4f\u5e55\uff09\uff01 \u5728 C++17 \u4e4b\u524d\uff0c\u7f16\u8bd1\u5668\u88ab\u5141\u8bb8\uff08\u4f46\u4e0d\u662f\u5fc5\u987b\uff09\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6267\u884c\u62f7\u8d1d\u7701\u7565\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4ecd\u7136\u662f\u5fc5\u987b\u53ef\u8bbf\u95ee\u7684\uff08\u975e\u79c1\u6709\uff09\uff0c\u5373\u4f7f\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\u88ab\u8c03\u7528\u3002 \u5230\u4e86 C++17\uff0c\u6709\u4e9b\u62f7\u8d1d\u7701\u7565\u5df2\u7ecf\u662f\u5f3a\u5236\u7684\u4e86\uff08\u5305\u62ec\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u8fd9\u79cd\u60c5\u51b5\uff09\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u5f3a\u5236\u7701\u7565\u7684\u60c5\u51b5\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4e0d\u9700\u8981\u662f\u53ef\u8bbf\u95ee\u7684\uff0c\u751a\u81f3\u4e0d\u9700\u8981\u88ab\u5b9a\u4e49\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u4fdd\u8bc1\u4e0d\u8c03\u7528\u5b83\uff01","title":"\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u53ef\u80fd\u4f1a\u88ab\u7701\u7565"},{"location":"14-13-copy-initialization/","tags":["copy initialization"],"text":"Key Takeaway - \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 int x = 5 ; \u8be5\u8bed\u53e5\u4f7f\u7528\u62f7\u8d1d\u521d\u59cb\u5316\u7684\u65b9\u5f0f\u5c06\u4e00\u4e2a\u65b0\u521b\u5efa\u7684\u6574\u578b\u53d8\u91cf x \u7684\u503c\u8bbe\u7f6e\u4e3a 5\u3002 \u800c\u5bf9\u4e8e\u7c7b\u6765\u8bf4\uff0c\u95ee\u9898\u5219\u4f1a\u53d8\u5f97\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\uff0c\u56e0\u4e3a\u7c7b\u5728\u521d\u59cb\u5316\u65f6\u9700\u8981\u4f7f\u7528\u6784\u9020\u51fd\u6570\u3002\u672c\u8282\u8bfe\u6211\u4eec\u4f1a\u4ecb\u7ecd\u4e0e\u7c7b\u7684\u62f7\u8d1d\u521d\u59cb\u5316\u76f8\u5173\u7684\u95ee\u9898\u3002 \u7c7b\u7684\u62f7\u8d1d\u521d\u59cb\u5316 \u8003\u8651\u4e0b\u9762\u7684 Fraction \u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include class Fraction { private : int m_numerator ; int m_denominator ; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator ( numerator ), m_denominator ( denominator ) { assert ( denominator != 0 ); } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } \u8003\u8651\u4e0b\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 int main () { Fraction six = Fraction ( 6 ); std :: cout << six ; return 0 ; } \u7f16\u8bd1\u8fd0\u884c\u4ee3\u7801\uff0c\u7ed3\u679c\u5982\u4f60\u6240\u4ee5\u60f3\u7684\u90a3\u6837\uff1a 1 6/1 \u8fd9\u79cd\u5f62\u5f0f\u7684\u62f7\u8d1d\u6784\u9020\uff0c\u5176\u6c42\u503c\u65b9\u5f0f\u548c\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u4e00\u6837\u7684\uff1a 1 Fraction six ( Fraction ( 6 )); \u5728\u4e0a\u4e00\u8282\u8bfe\u4e2d\uff08 14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \uff09\u6211\u4eec\u5b66\u5230\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u53ef\u80fd\u4f1a\u8c03\u7528 Fraction(int, int) \u4ee5\u53ca Fraction \u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff08\u53ef\u80fd\u4f1a\u88ab\u4f18\u5316\u6389\uff09\u3002\u4e0d\u8fc7\uff0c\u7531\u4e8e\u7701\u7565\u4e0d\u88ab\u4fdd\u8bc1\u4e00\u5b9a\u53d1\u751f\uff0c\u6240\u4ee5\u6700\u597d\u907f\u514d\u4f7f\u7528\u62f7\u8d1d\u521d\u59cb\u5316\u6765\u521d\u59cb\u5316\u7c7b\uff0c\u800c\u6539\u7528 \u7edf\u4e00\u521d\u59cb\u5316 \u3002 \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u4f7f\u7528\u62f7\u8d1d\u521d\u59cb\u5316\uff0c\u4f7f\u7528\u7edf\u4e00\u521d\u59cb\u5316 \u62f7\u8d1d\u521d\u59cb\u5316\u7684\u5176\u4ed6\u4f7f\u7528\u573a\u666f \u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u5730\u65b9\u4f7f\u7528\u4e86\u62f7\u8d1d\u521d\u59cb\u5316\uff0c\u5176\u4e2d\u6709\u4e24\u4e2a\u5730\u65b9\u503c\u5f97\u4e00\u63d0\u3002\u5f53 \u6309\u503c\u4f20\u9012 \u6216 \u6309\u503c\u8fd4\u56de \u7c7b\u65f6\uff0c\u4f1a\u4f7f\u7528\u62f7\u8d1d\u521d\u59cb\u5316\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include #include class Fraction { private : int m_numerator ; int m_denominator ; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator ( numerator ), m_denominator ( denominator ) { assert ( denominator != 0 ); } // Copy constructor Fraction ( const Fraction & copy ) : m_numerator ( copy . m_numerator ), m_denominator ( copy . m_denominator ) { // no need to check for a denominator of 0 here since copy must already be a valid Fraction std :: cout << \"Copy constructor called \\n \" ; // just to prove it works } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); int getNumerator () { return m_numerator ; } void setNumerator ( int numerator ) { m_numerator = numerator ; } }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } Fraction makeNegative ( Fraction f ) // ideally we should do this by const reference { f . setNumerator ( - f . getNumerator ()); return f ; } int main () { Fraction fiveThirds ( 5 , 3 ); std :: cout << makeNegative ( fiveThirds ); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570 makeNegative \u5c06 Fraction \u7c7b\u578b\u7684\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u540c\u65f6\u8fd4\u56de\u6309\u503c\u8fd4\u56de Fraction \u5bf9\u8c61\u3002\u7a0b\u5e8f\u6267\u884c\u540e\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 Copy constructor called Copy constructor called -5/3 \u7b2c\u4e00\u6b21\u8c03\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u662f\u5728 fiveThirds \u88ab\u4f20\u5165 makeNegative() \u4f5c\u4e3a\u5f62\u53c2 f \u7684\u65f6\u5019\u3002\u7b2c\u4e8c\u6b21\u8c03\u7528\u662f\u5728 makeNegative() \u6309\u503c\u8fd4\u56de\u65f6\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5b9e\u53c2\u548c\u8fd4\u56de\u503c\u90fd\u662f \u6309\u503c \u4f20\u9012\u6216\u8fd4\u56de\u7684\uff0c\u6240\u4ee5\u62f7\u8d1d\u6784\u9020\u4e0d\u4f1a\u88ab\u7701\u7565\u3002\u4f46\u662f\uff0c\u5728\u5176\u4ed6\u7684\u4e00\u4e9b\u573a\u5408\uff0c\u5982\u679c\u5b9e\u53c2\u548c\u8fd4\u56de\u503c\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\uff0c\u7f16\u8bd1\u5668\u4ecd\u7136\u53ef\u80fd\u7701\u7565\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include class Something { public : Something () = default ; Something ( const Something & ) { std :: cout << \"Copy constructor called \\n \" ; } }; Something foo () { return Something (); // copy constructor normally called here } Something goo () { Something s ; return s ; // copy constructor normally called here } int main () { std :: cout << \"Initializing s1 \\n \" ; Something s1 = foo (); // copy constructor normally called here std :: cout << \"Initializing s2 \\n \" ; Something s2 = goo (); // copy constructor normally called here } The above program would normally call the copy constructor 4 times -- however, due to copy elision, it\u2019s likely that your compiler will elide most or all of the cases. Visual Studio 2019 elides 3 (it doesn\u2019t elide the case where goo() is returned), and GCC elides all 4 \u4e0a\u9762\u7684\u7a0b\u5e8f\u901a\u5e38\u4f1a\u8c03\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u65704\u6b21\u2014\u2014\u7136\u800c\uff0c\u7531\u4e8e\u7701\u7565\uff0c\u7f16\u8bd1\u5668\u5f88\u53ef\u80fd\u4f1a\u7701\u7565\u5927\u90e8\u5206\u6216\u6240\u6709\u8c03\u7528\u3002Visual Studio 2019\u7701\u7565\u4e863\u4e2a(\u5b83\u6ca1\u6709\u7701\u7565\u8fd4\u56de goo() \u7684\u60c5\u51b5)\uff0c\u800cGCC\u7701\u7565\u4e86\u6240\u67094\u4e2a\u3002","title":"14.13 - \u62f7\u8d1d\u521d\u59cb\u5316"},{"location":"14-13-copy-initialization/#_1","text":"\u8003\u8651\u4e0b\u9762\u7684 Fraction \u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include class Fraction { private : int m_numerator ; int m_denominator ; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator ( numerator ), m_denominator ( denominator ) { assert ( denominator != 0 ); } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } \u8003\u8651\u4e0b\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 int main () { Fraction six = Fraction ( 6 ); std :: cout << six ; return 0 ; } \u7f16\u8bd1\u8fd0\u884c\u4ee3\u7801\uff0c\u7ed3\u679c\u5982\u4f60\u6240\u4ee5\u60f3\u7684\u90a3\u6837\uff1a 1 6/1 \u8fd9\u79cd\u5f62\u5f0f\u7684\u62f7\u8d1d\u6784\u9020\uff0c\u5176\u6c42\u503c\u65b9\u5f0f\u548c\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u4e00\u6837\u7684\uff1a 1 Fraction six ( Fraction ( 6 )); \u5728\u4e0a\u4e00\u8282\u8bfe\u4e2d\uff08 14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \uff09\u6211\u4eec\u5b66\u5230\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u53ef\u80fd\u4f1a\u8c03\u7528 Fraction(int, int) \u4ee5\u53ca Fraction \u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff08\u53ef\u80fd\u4f1a\u88ab\u4f18\u5316\u6389\uff09\u3002\u4e0d\u8fc7\uff0c\u7531\u4e8e\u7701\u7565\u4e0d\u88ab\u4fdd\u8bc1\u4e00\u5b9a\u53d1\u751f\uff0c\u6240\u4ee5\u6700\u597d\u907f\u514d\u4f7f\u7528\u62f7\u8d1d\u521d\u59cb\u5316\u6765\u521d\u59cb\u5316\u7c7b\uff0c\u800c\u6539\u7528 \u7edf\u4e00\u521d\u59cb\u5316 \u3002 \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u4f7f\u7528\u62f7\u8d1d\u521d\u59cb\u5316\uff0c\u4f7f\u7528\u7edf\u4e00\u521d\u59cb\u5316","title":"\u7c7b\u7684\u62f7\u8d1d\u521d\u59cb\u5316"},{"location":"14-13-copy-initialization/#_2","text":"\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u5730\u65b9\u4f7f\u7528\u4e86\u62f7\u8d1d\u521d\u59cb\u5316\uff0c\u5176\u4e2d\u6709\u4e24\u4e2a\u5730\u65b9\u503c\u5f97\u4e00\u63d0\u3002\u5f53 \u6309\u503c\u4f20\u9012 \u6216 \u6309\u503c\u8fd4\u56de \u7c7b\u65f6\uff0c\u4f1a\u4f7f\u7528\u62f7\u8d1d\u521d\u59cb\u5316\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include #include class Fraction { private : int m_numerator ; int m_denominator ; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator ( numerator ), m_denominator ( denominator ) { assert ( denominator != 0 ); } // Copy constructor Fraction ( const Fraction & copy ) : m_numerator ( copy . m_numerator ), m_denominator ( copy . m_denominator ) { // no need to check for a denominator of 0 here since copy must already be a valid Fraction std :: cout << \"Copy constructor called \\n \" ; // just to prove it works } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); int getNumerator () { return m_numerator ; } void setNumerator ( int numerator ) { m_numerator = numerator ; } }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } Fraction makeNegative ( Fraction f ) // ideally we should do this by const reference { f . setNumerator ( - f . getNumerator ()); return f ; } int main () { Fraction fiveThirds ( 5 , 3 ); std :: cout << makeNegative ( fiveThirds ); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570 makeNegative \u5c06 Fraction \u7c7b\u578b\u7684\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u540c\u65f6\u8fd4\u56de\u6309\u503c\u8fd4\u56de Fraction \u5bf9\u8c61\u3002\u7a0b\u5e8f\u6267\u884c\u540e\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 Copy constructor called Copy constructor called -5/3 \u7b2c\u4e00\u6b21\u8c03\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u662f\u5728 fiveThirds \u88ab\u4f20\u5165 makeNegative() \u4f5c\u4e3a\u5f62\u53c2 f \u7684\u65f6\u5019\u3002\u7b2c\u4e8c\u6b21\u8c03\u7528\u662f\u5728 makeNegative() \u6309\u503c\u8fd4\u56de\u65f6\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5b9e\u53c2\u548c\u8fd4\u56de\u503c\u90fd\u662f \u6309\u503c \u4f20\u9012\u6216\u8fd4\u56de\u7684\uff0c\u6240\u4ee5\u62f7\u8d1d\u6784\u9020\u4e0d\u4f1a\u88ab\u7701\u7565\u3002\u4f46\u662f\uff0c\u5728\u5176\u4ed6\u7684\u4e00\u4e9b\u573a\u5408\uff0c\u5982\u679c\u5b9e\u53c2\u548c\u8fd4\u56de\u503c\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\uff0c\u7f16\u8bd1\u5668\u4ecd\u7136\u53ef\u80fd\u7701\u7565\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include class Something { public : Something () = default ; Something ( const Something & ) { std :: cout << \"Copy constructor called \\n \" ; } }; Something foo () { return Something (); // copy constructor normally called here } Something goo () { Something s ; return s ; // copy constructor normally called here } int main () { std :: cout << \"Initializing s1 \\n \" ; Something s1 = foo (); // copy constructor normally called here std :: cout << \"Initializing s2 \\n \" ; Something s2 = goo (); // copy constructor normally called here } The above program would normally call the copy constructor 4 times -- however, due to copy elision, it\u2019s likely that your compiler will elide most or all of the cases. Visual Studio 2019 elides 3 (it doesn\u2019t elide the case where goo() is returned), and GCC elides all 4 \u4e0a\u9762\u7684\u7a0b\u5e8f\u901a\u5e38\u4f1a\u8c03\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u65704\u6b21\u2014\u2014\u7136\u800c\uff0c\u7531\u4e8e\u7701\u7565\uff0c\u7f16\u8bd1\u5668\u5f88\u53ef\u80fd\u4f1a\u7701\u7565\u5927\u90e8\u5206\u6216\u6240\u6709\u8c03\u7528\u3002Visual Studio 2019\u7701\u7565\u4e863\u4e2a(\u5b83\u6ca1\u6709\u7701\u7565\u8fd4\u56de goo() \u7684\u60c5\u51b5)\uff0c\u800cGCC\u7701\u7565\u4e86\u6240\u67094\u4e2a\u3002","title":"\u62f7\u8d1d\u521d\u59cb\u5316\u7684\u5176\u4ed6\u4f7f\u7528\u573a\u666f"},{"location":"14-14-converting-constructors-explicit-and-delete/","tags":["converting constructors","explicit","delete"],"text":"Key Takeaway - \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cC++\u4f1a\u628a\u6240\u6709\u7684\u6784\u9020\u51fd\u6570\u90fd\u5f53\u505a\u9690\u5f0f\u5730\u8f6c\u6362\u64cd\u4f5c\u7b26\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include #include class Fraction { private : int m_numerator ; int m_denominator ; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator ( numerator ), m_denominator ( denominator ) { assert ( denominator != 0 ); } // Copy constructor Fraction ( const Fraction & copy ) : m_numerator ( copy . m_numerator ), m_denominator ( copy . m_denominator ) { // no need to check for a denominator of 0 here since copy must already be a valid Fraction std :: cout << \"Copy constructor called \\n \" ; // just to prove it works } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); int getNumerator () { return m_numerator ; } void setNumerator ( int numerator ) { m_numerator = numerator ; } }; void printFraction ( const Fraction & f ) { std :: cout << f ; } std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } int main () { printFraction ( 6 ); return 0 ; } \u5c3d\u7ba1 printFraction() \u671f\u671b\u63a5\u53d7\u4e00\u4e2a Fraction \uff0c\u4f46\u5b9e\u9645\u4e0a\u6211\u4eec\u7ed9\u5b83\u4f20\u9012\u4e86\u4e00\u4e2a\u5b57\u9762\u91cf 6\u3002\u56e0\u4e3a Fraction \u7684\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u6574\u578b\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u9690\u5f0f\u5730\u5c06\u5b57\u9762\u91cf6\u8f6c\u6362\u4e3a\u4e00\u4e2a Fraction \u5bf9\u8c61\u3002\u5b83\u4f1a\u4f7f\u7528 Fraction(int, int) \u6765\u521d\u59cb\u5316 printFraction() \u7684\u5f62\u53c2 f \u3002 \u7a0b\u5e8f\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 6/1 \u5bf9\u4e8e\u6240\u6709\u7c7b\u578b\u7684\u521d\u59cb\u5316\uff0c\u8be5\u9690\u5f0f\u8f6c\u6362\u90fd\u53ef\u7528\u3002 \u80fd\u591f\u88ab\u7528\u6765\u8fdb\u884c \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u7684\u6784\u9020\u51fd\u6570\uff0c\u79f0\u4e3a \u8f6c\u6362\u6784\u9020\u51fd\u6570 \u3002 explicit \u5173\u952e\u5b57 \u867d\u7136\u5728 Fraction \u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8fdb\u884c\u9690\u5f0f\u8f6c\u6362\u662f\u6709\u610f\u4e49\u7684\uff0c\u4f46\u5728\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u8fd9\u53ef\u80fd\u662f\u4e0d\u53ef\u53d6\u7684\uff0c\u4e5f\u53ef\u80fd\u5bfc\u81f4\u610f\u5916\u7684\u884c\u4e3a: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include #include class MyString { private : std :: string m_string ; public : MyString ( int x ) // allocate string of size x { m_string . resize ( x ); } MyString ( const char * string ) // allocate string to hold string value { m_string = string ; } friend std :: ostream & operator << ( std :: ostream & out , const MyString & s ); }; std :: ostream & operator << ( std :: ostream & out , const MyString & s ) { out << s . m_string ; return out ; } void printString ( const MyString & s ) { std :: cout << s ; } int main () { MyString mine = 'x' ; // Will compile and use MyString(int) std :: cout << mine << '\\n' ; printString ( 'x' ); // Will compile and use MyString(int) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u5b57\u7b26\u6765\u521d\u59cb\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u56e0\u4e3a\u5b57\u7b26\u5b9e\u9645\u4e0a\u662f\u5c5e\u4e8e\u6574\u578b\u5bb6\u65cf\u7684\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u4f7f\u7528 MyString(int) \u6784\u9020\u51fd\u6570\u9690\u5f0f\u5730\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a MyString \u3002\u5f53\u7a0b\u5e8f\u6253\u5370 MyString \u65f6\uff0c\u5176\u7ed3\u679c\u4f1a\u51fa\u4e4e\u6211\u4eec\u7684\u610f\u6599\u3002\u7c7b\u4f3c\u7684\uff0c\u8c03\u7528 printString(\u2018x\u2019) \u4e5f\u4f1a\u5bfc\u81f4\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\uff0c\u7ed3\u679c\u4e5f\u662f\u7c7b\u4f3c\u7684\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u901a\u8fc7 explicit \u5173\u952e\u5b57\u6807\u8bb0\u6784\u9020\u51fd\u6570(\u548c\u8f6c\u6362\u51fd\u6570)\uff0c\u8be5\u5173\u952e\u5b57\u653e\u5728\u51fd\u6570\u540d\u524d\u9762\u3002explicit \u7684\u6784\u9020\u51fd\u6570\u548c\u8f6c\u6362\u51fd\u6570\u5c06\u4e0d\u4f1a\u7528\u4e8e\u9690\u5f0f\u8f6c\u6362\u6216\u62f7\u8d1d\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include #include class MyString { private : std :: string m_string ; public : // explicit keyword makes this constructor ineligible for implicit conversions explicit MyString ( int x ) // allocate string of size x { m_string . resize ( x ); } MyString ( const char * string ) // allocate string to hold string value { m_string = string ; } friend std :: ostream & operator << ( std :: ostream & out , const MyString & s ); }; std :: ostream & operator << ( std :: ostream & out , const MyString & s ) { out << s . m_string ; return out ; } void printString ( const MyString & s ) { std :: cout << s ; } int main () { MyString mine = 'x' ; // \u7f16\u8bd1\u9519\u8bef\uff0c\u56e0\u4e3a MyString(int) \u73b0\u5728\u662f explicit \u7684 std :: cout << mine ; printString ( 'x' ); // \u7f16\u8bd1\u9519\u8bef\uff0cMyString(int) \u4e0d\u80fd\u88ab\u7528\u4e8e\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 return 0 ; } \u4e0a\u9762\u7684\u7a0b\u5e8f\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\uff0c\u56e0\u4e3a MyString(int) \u88ab\u5b9a\u4e49\u4e3a explicit \uff0c\u800c\u4e14\u65e0\u6cd5\u627e\u5230\u4e00\u4e2a\u53ef\u7528\u7684\u80fd\u591f\u5c06 \u2018x\u2019 \u9690\u5f0f\u8f6c\u6362\u4e3a MyString \u7684\u6784\u9020\u51fd\u6570\u3002 \u4e0d\u8fc7\uff0c\u5c06\u6784\u9020\u51fd\u6570\u6807\u8bb0\u4e3a explict \u53ea\u80fd\u9632\u6b62\u9690\u5f0f\u7c7b\u8f6c\u6362\u3002\u663e\u793a\u7684\u7c7b\u578b\u8f6c\u6362\u4ecd\u7136\u662f\u53ef\u4ee5\u7684\uff1a 1 std :: cout << static_cast < MyString > ( 5 ); // Allowed: explicit cast of 5 to MyString(int) \u76f4\u63a5\u521d\u59cb\u5316 \u548c \u7edf\u4e00\u521d\u59cb\u5316 \u4ecd\u4f1a\u5bf9\u53c2\u6570\u8fdb\u884c\u8f6c\u6362\u4f7f\u5176\u5339\u914d\uff08\u7edf\u4e00\u521d\u59cb\u5316\u867d\u7136\u4e0d\u4f1a\u8fdb\u884c \u7f29\u7a84\u8f6c\u6362 \uff0c\u4f46\u662f\u53ef\u80fd\u4f1a\u8fdb\u884c\u5176\u4ed6\u7c7b\u578b\u7684\u8f6c\u6362\uff09\u3002 1 MyString str { 'x' }; // Allowed: initialization parameters may still be implicitly converted to match \u6700\u4f73\u5b9e\u8df5 \u8003\u8651\u5c06\u6784\u9020\u51fd\u6570\u548c\u7528\u6237\u5b9a\u4e49\u8f6c\u6362\u6210\u5458\u51fd\u6570\u8bbe\u7f6e\u4e3a\u663e\u5f0f\u7684\uff0c\u4ee5\u9632\u6b62\u9690\u5f0f\u8f6c\u6362\u9519\u8bef\u3002 delete \u5173\u952e\u5b57 \u5728 MyString \u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u5e0c\u671b\u5b8c\u5168\u7981\u6b62 x \u88ab\u8f6c\u6362\u4e3a MyString (\u65e0\u8bba\u662f\u9690\u5f0f\u7684\u8fd8\u662f\u663e\u5f0f\u7684\uff0c\u56e0\u4e3a\u7ed3\u679c\u7b26\u5408\u76f4\u89c9)\u3002\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u662f\u6dfb\u52a0 MyString(char) \u6784\u9020\u51fd\u6570\uff0c\u5e76\u5c06\u5176\u8bbe\u4e3a\u79c1\u6709: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include class MyString { private : std :: string m_string ; MyString ( char ) // objects of type MyString(char) can't be constructed from outside the class { } public : // explicit keyword makes this constructor ineligible for implicit conversions explicit MyString ( int x ) // allocate string of size x { m_string . resize ( x ); } MyString ( const char * string ) // allocate string to hold string value { m_string = string ; } friend std :: ostream & operator << ( std :: ostream & out , const MyString & s ); }; std :: ostream & operator << ( std :: ostream & out , const MyString & s ) { out << s . m_string ; return out ; } int main () { MyString mine ( 'x' ); // compile error, since MyString(char) is private std :: cout << mine ; return 0 ; } COPY \u4f46\u662f\uff0c\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u4ecd\u7136\u53ef\u4ee5\u5728\u7c7b\u5185\u90e8\u4f7f\u7528(\u79c1\u6709\u8bbf\u95ee\u53ea\u9632\u6b62\u975e\u6210\u5458\u8c03\u7528\u6b64\u51fd\u6570)\u3002 \u66f4\u597d\u7684\u89e3\u51b3\u65b9\u6cd5\u662f\u4f7f\u7528 delete \u5173\u952e\u5b57\u5220\u9664\u51fd\u6570: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include class MyString { private : std :: string m_string ; public : MyString ( char ) = delete ; // \u4efb\u4f55\u4f7f\u7528\u8be5\u51fd\u6570\u7684\u5730\u65b9\u90fd\u4f1a\u62a5\u9519 // explicit \u4f7f\u5f97\u8be5\u6784\u9020\u51fd\u6570\u4e0d\u80fd\u591f\u88ab\u7528\u4e8e\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 explicit MyString ( int x ) // allocate string of size x / { m_string . resize ( x ); } MyString ( const char * string ) // allocate string to hold string value { m_string = string ; } friend std :: ostream & operator << ( std :: ostream & out , const MyString & s ); }; std :: ostream & operator << ( std :: ostream & out , const MyString & s ) { out << s . m_string ; return out ; } int main () { MyString mine ( 'x' ); // compile error, since MyString(char) is deleted std :: cout << mine ; return 0 ; } \u5f53\u51fd\u6570\u88ab delete \u4e4b\u540e\uff0c\u4efb\u4f55\u5bf9\u8be5\u6784\u9020\u51fd\u6570\u7684\u4f7f\u7528\u90fd\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u62a5\u9519\u3002 \u6ce8\u610f\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u91cd\u8f7d\u8fd0\u7b97\u7b26\u4e5f\u53ef\u4ee5\u88ab delete \uff0c\u4ee5\u907f\u514d\u5bf9\u5e94\u7684\u64cd\u4f5c\u3002","title":"14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57"},{"location":"14-14-converting-constructors-explicit-and-delete/#explicit","text":"\u867d\u7136\u5728 Fraction \u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8fdb\u884c\u9690\u5f0f\u8f6c\u6362\u662f\u6709\u610f\u4e49\u7684\uff0c\u4f46\u5728\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u8fd9\u53ef\u80fd\u662f\u4e0d\u53ef\u53d6\u7684\uff0c\u4e5f\u53ef\u80fd\u5bfc\u81f4\u610f\u5916\u7684\u884c\u4e3a: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include #include class MyString { private : std :: string m_string ; public : MyString ( int x ) // allocate string of size x { m_string . resize ( x ); } MyString ( const char * string ) // allocate string to hold string value { m_string = string ; } friend std :: ostream & operator << ( std :: ostream & out , const MyString & s ); }; std :: ostream & operator << ( std :: ostream & out , const MyString & s ) { out << s . m_string ; return out ; } void printString ( const MyString & s ) { std :: cout << s ; } int main () { MyString mine = 'x' ; // Will compile and use MyString(int) std :: cout << mine << '\\n' ; printString ( 'x' ); // Will compile and use MyString(int) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u5b57\u7b26\u6765\u521d\u59cb\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u56e0\u4e3a\u5b57\u7b26\u5b9e\u9645\u4e0a\u662f\u5c5e\u4e8e\u6574\u578b\u5bb6\u65cf\u7684\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u4f7f\u7528 MyString(int) \u6784\u9020\u51fd\u6570\u9690\u5f0f\u5730\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a MyString \u3002\u5f53\u7a0b\u5e8f\u6253\u5370 MyString \u65f6\uff0c\u5176\u7ed3\u679c\u4f1a\u51fa\u4e4e\u6211\u4eec\u7684\u610f\u6599\u3002\u7c7b\u4f3c\u7684\uff0c\u8c03\u7528 printString(\u2018x\u2019) \u4e5f\u4f1a\u5bfc\u81f4\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\uff0c\u7ed3\u679c\u4e5f\u662f\u7c7b\u4f3c\u7684\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u901a\u8fc7 explicit \u5173\u952e\u5b57\u6807\u8bb0\u6784\u9020\u51fd\u6570(\u548c\u8f6c\u6362\u51fd\u6570)\uff0c\u8be5\u5173\u952e\u5b57\u653e\u5728\u51fd\u6570\u540d\u524d\u9762\u3002explicit \u7684\u6784\u9020\u51fd\u6570\u548c\u8f6c\u6362\u51fd\u6570\u5c06\u4e0d\u4f1a\u7528\u4e8e\u9690\u5f0f\u8f6c\u6362\u6216\u62f7\u8d1d\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include #include class MyString { private : std :: string m_string ; public : // explicit keyword makes this constructor ineligible for implicit conversions explicit MyString ( int x ) // allocate string of size x { m_string . resize ( x ); } MyString ( const char * string ) // allocate string to hold string value { m_string = string ; } friend std :: ostream & operator << ( std :: ostream & out , const MyString & s ); }; std :: ostream & operator << ( std :: ostream & out , const MyString & s ) { out << s . m_string ; return out ; } void printString ( const MyString & s ) { std :: cout << s ; } int main () { MyString mine = 'x' ; // \u7f16\u8bd1\u9519\u8bef\uff0c\u56e0\u4e3a MyString(int) \u73b0\u5728\u662f explicit \u7684 std :: cout << mine ; printString ( 'x' ); // \u7f16\u8bd1\u9519\u8bef\uff0cMyString(int) \u4e0d\u80fd\u88ab\u7528\u4e8e\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 return 0 ; } \u4e0a\u9762\u7684\u7a0b\u5e8f\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\uff0c\u56e0\u4e3a MyString(int) \u88ab\u5b9a\u4e49\u4e3a explicit \uff0c\u800c\u4e14\u65e0\u6cd5\u627e\u5230\u4e00\u4e2a\u53ef\u7528\u7684\u80fd\u591f\u5c06 \u2018x\u2019 \u9690\u5f0f\u8f6c\u6362\u4e3a MyString \u7684\u6784\u9020\u51fd\u6570\u3002 \u4e0d\u8fc7\uff0c\u5c06\u6784\u9020\u51fd\u6570\u6807\u8bb0\u4e3a explict \u53ea\u80fd\u9632\u6b62\u9690\u5f0f\u7c7b\u8f6c\u6362\u3002\u663e\u793a\u7684\u7c7b\u578b\u8f6c\u6362\u4ecd\u7136\u662f\u53ef\u4ee5\u7684\uff1a 1 std :: cout << static_cast < MyString > ( 5 ); // Allowed: explicit cast of 5 to MyString(int) \u76f4\u63a5\u521d\u59cb\u5316 \u548c \u7edf\u4e00\u521d\u59cb\u5316 \u4ecd\u4f1a\u5bf9\u53c2\u6570\u8fdb\u884c\u8f6c\u6362\u4f7f\u5176\u5339\u914d\uff08\u7edf\u4e00\u521d\u59cb\u5316\u867d\u7136\u4e0d\u4f1a\u8fdb\u884c \u7f29\u7a84\u8f6c\u6362 \uff0c\u4f46\u662f\u53ef\u80fd\u4f1a\u8fdb\u884c\u5176\u4ed6\u7c7b\u578b\u7684\u8f6c\u6362\uff09\u3002 1 MyString str { 'x' }; // Allowed: initialization parameters may still be implicitly converted to match \u6700\u4f73\u5b9e\u8df5 \u8003\u8651\u5c06\u6784\u9020\u51fd\u6570\u548c\u7528\u6237\u5b9a\u4e49\u8f6c\u6362\u6210\u5458\u51fd\u6570\u8bbe\u7f6e\u4e3a\u663e\u5f0f\u7684\uff0c\u4ee5\u9632\u6b62\u9690\u5f0f\u8f6c\u6362\u9519\u8bef\u3002","title":"explicit \u5173\u952e\u5b57"},{"location":"14-14-converting-constructors-explicit-and-delete/#delete","text":"\u5728 MyString \u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u5e0c\u671b\u5b8c\u5168\u7981\u6b62 x \u88ab\u8f6c\u6362\u4e3a MyString (\u65e0\u8bba\u662f\u9690\u5f0f\u7684\u8fd8\u662f\u663e\u5f0f\u7684\uff0c\u56e0\u4e3a\u7ed3\u679c\u7b26\u5408\u76f4\u89c9)\u3002\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u662f\u6dfb\u52a0 MyString(char) \u6784\u9020\u51fd\u6570\uff0c\u5e76\u5c06\u5176\u8bbe\u4e3a\u79c1\u6709: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include class MyString { private : std :: string m_string ; MyString ( char ) // objects of type MyString(char) can't be constructed from outside the class { } public : // explicit keyword makes this constructor ineligible for implicit conversions explicit MyString ( int x ) // allocate string of size x { m_string . resize ( x ); } MyString ( const char * string ) // allocate string to hold string value { m_string = string ; } friend std :: ostream & operator << ( std :: ostream & out , const MyString & s ); }; std :: ostream & operator << ( std :: ostream & out , const MyString & s ) { out << s . m_string ; return out ; } int main () { MyString mine ( 'x' ); // compile error, since MyString(char) is private std :: cout << mine ; return 0 ; } COPY \u4f46\u662f\uff0c\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u4ecd\u7136\u53ef\u4ee5\u5728\u7c7b\u5185\u90e8\u4f7f\u7528(\u79c1\u6709\u8bbf\u95ee\u53ea\u9632\u6b62\u975e\u6210\u5458\u8c03\u7528\u6b64\u51fd\u6570)\u3002 \u66f4\u597d\u7684\u89e3\u51b3\u65b9\u6cd5\u662f\u4f7f\u7528 delete \u5173\u952e\u5b57\u5220\u9664\u51fd\u6570: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include class MyString { private : std :: string m_string ; public : MyString ( char ) = delete ; // \u4efb\u4f55\u4f7f\u7528\u8be5\u51fd\u6570\u7684\u5730\u65b9\u90fd\u4f1a\u62a5\u9519 // explicit \u4f7f\u5f97\u8be5\u6784\u9020\u51fd\u6570\u4e0d\u80fd\u591f\u88ab\u7528\u4e8e\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 explicit MyString ( int x ) // allocate string of size x / { m_string . resize ( x ); } MyString ( const char * string ) // allocate string to hold string value { m_string = string ; } friend std :: ostream & operator << ( std :: ostream & out , const MyString & s ); }; std :: ostream & operator << ( std :: ostream & out , const MyString & s ) { out << s . m_string ; return out ; } int main () { MyString mine ( 'x' ); // compile error, since MyString(char) is deleted std :: cout << mine ; return 0 ; } \u5f53\u51fd\u6570\u88ab delete \u4e4b\u540e\uff0c\u4efb\u4f55\u5bf9\u8be5\u6784\u9020\u51fd\u6570\u7684\u4f7f\u7528\u90fd\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u62a5\u9519\u3002 \u6ce8\u610f\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u91cd\u8f7d\u8fd0\u7b97\u7b26\u4e5f\u53ef\u4ee5\u88ab delete \uff0c\u4ee5\u907f\u514d\u5bf9\u5e94\u7684\u64cd\u4f5c\u3002","title":"delete \u5173\u952e\u5b57"},{"location":"14-15-overloading-the-assignment-operator/","tags":["overload","assignment"],"text":"Key Takeaway - The assignment operator (operator=) is used to copy values from one object to another already existing object . Assignment vs Copy constructor The purpose of the copy constructor and the assignment operator are almost equivalent -- both copy one object to another. However, the copy constructor initializes new objects, whereas the assignment operator replaces the contents of existing objects. The difference between the copy constructor and the assignment operator causes a lot of confusion for new programmers, but it\u2019s really not all that difficult. Summarizing: If a new object has to be created before the copying can occur, the copy constructor is used (note: this includes passing or returning objects by value). If a new object does not have to be created before the copying can occur, the assignment operator is used. Overloading the assignment operator Overloading the assignment operator (operator=) is fairly straightforward, with one specific caveat that we\u2019ll get to. The assignment operator must be overloaded as a member function. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 #include #include class Fraction { private : int m_numerator { 0 }; int m_denominator { 1 }; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } // Copy constructor Fraction ( const Fraction & copy ) : m_numerator { copy . m_numerator }, m_denominator { copy . m_denominator } { // no need to check for a denominator of 0 here since copy must already be a valid Fraction std :: cout << \"Copy constructor called \\n \" ; // just to prove it works } // Overloaded assignment Fraction & operator = ( const Fraction & fraction ); friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } // A simplistic implementation of operator= (see better implementation below) Fraction & Fraction :: operator = ( const Fraction & fraction ) { // do the copy m_numerator = fraction . m_numerator ; m_denominator = fraction . m_denominator ; // return the existing object so we can chain this operator return * this ; } int main () { Fraction fiveThirds { 5 , 3 }; Fraction f ; f = fiveThirds ; // calls overloaded assignment std :: cout << f ; return 0 ; } COPY This prints: 5/3 This should all be pretty straightforward by now. Our overloaded operator= returns *this, so that we can chain multiple assignments together: 1 2 3 4 5 6 7 8 9 10 int main () { Fraction f1 { 5 , 3 }; Fraction f2 { 7 , 2 }; Fraction f3 { 9 , 5 }; f1 = f2 = f3 ; // chained assignment return 0 ; } COPY Issues due to self-assignment Here\u2019s where things start to get a little more interesting. C++ allows self-assignment: 1 2 3 4 5 6 7 int main () { Fraction f1 { 5 , 3 }; f1 = f1 ; // self assignment return 0 ; } COPY This will call f1.operator=(f1), and under the simplistic implementation above, all of the members will be assigned to themselves. In this particular example, the self-assignment causes each member to be assigned to itself, which has no overall impact, other than wasting time. In most cases, a self-assignment doesn\u2019t need to do anything at all! However, in cases where an assignment operator needs to dynamically assign memory, self-assignment can actually be dangerous: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 #include class MyString { private : char * m_data {}; int m_length {}; public : MyString ( const char * data = nullptr , int length = 0 ) : m_length { length } { if ( length ) { m_data = new char [ length ]; for ( int i { 0 }; i < length ; ++ i ) m_data [ i ] = data [ i ]; } } ~ MyString () { delete [] m_data ; } // Overloaded assignment MyString & operator = ( const MyString & str ); friend std :: ostream & operator << ( std :: ostream & out , const MyString & s ); }; std :: ostream & operator << ( std :: ostream & out , const MyString & s ) { out << s . m_data ; return out ; } // A simplistic implementation of operator= (do not use) MyString & MyString :: operator = ( const MyString & str ) { // if data exists in the current string, delete it if ( m_data ) delete [] m_data ; m_length = str . m_length ; // copy the data from str to the implicit object m_data = new char [ str . m_length ]; for ( int i { 0 }; i < str . m_length ; ++ i ) m_data [ i ] = str . m_data [ i ]; // return the existing object so we can chain this operator return * this ; } int main () { MyString alex ( \"Alex\" , 5 ); // Meet Alex MyString employee ; employee = alex ; // Alex is our newest employee std :: cout << employee ; // Say your name, employee return 0 ; } COPY First, run the program as it is. You\u2019ll see that the program prints \u201cAlex\u201d as it should. Now run the following program: 1 2 3 4 5 6 7 8 int main () { MyString alex { \"Alex\" , 5 }; // Meet Alex alex = alex ; // Alex is himself std :: cout << alex ; // Say your name, Alex return 0 ; } COPY You\u2019ll probably get garbage output. What happened? Consider what happens in the overloaded operator= when the implicit object AND the passed in parameter (str) are both variable alex. In this case, m_data is the same as str.m_data. The first thing that happens is that the function checks to see if the implicit object already has a string. If so, it needs to delete it, so we don\u2019t end up with a memory leak. In this case, m_data is allocated, so the function deletes m_data. But because str is the same as *this, the string that we wanted to copy has been deleted and m_data (and str.m_data) are dangling. Later on, we allocate new memory to m_data (and str.m_data). So when we subsequently copy the data from str.m_data into m_data, we\u2019re copying garbage, because str.m_data was never initialized. Detecting and handling self-assignment Fortunately, we can detect when self-assignment occurs. Here\u2019s an updated implementation of our overloaded operator= for the MyString class: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 MyString & MyString :: operator = ( const MyString & str ) { // self-assignment check if ( this == & str ) return * this ; // if data exists in the current string, delete it if ( m_data ) delete [] m_data ; m_length = str . m_length ; // copy the data from str to the implicit object m_data = new char [ str . m_length ]; for ( int i { 0 }; i < str . m_length ; ++ i ) m_data [ i ] = str . m_data [ i ]; // return the existing object so we can chain this operator return * this ; } COPY By checking if the address of our implicit object is the same as the address of the object being passed in as a parameter, we can have our assignment operator just return immediately without doing any other work. Because this is just a pointer comparison, it should be fast, and does not require operator== to be overloaded. When not to handle self-assignment Typically the self-assignment check is skipped for copy constructors. Because the object being copy constructed is newly created, the only case where the newly created object can be equal to the object being copied is when you try to initialize a newly defined object with itself: 1 someClass c { c }; COPY In such cases, your compiler should warn you that c is an uninitialized variable. Second, the self-assignment check may be omitted in classes that can naturally handle self-assignment. Consider this Fraction class assignment operator that has a self-assignment guard: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // A better implementation of operator= Fraction & Fraction :: operator = ( const Fraction & fraction ) { // self-assignment guard if ( this == & fraction ) return * this ; // do the copy m_numerator = fraction . m_numerator ; // can handle self-assignment m_denominator = fraction . m_denominator ; // can handle self-assignment // return the existing object so we can chain this operator return * this ; } COPY If the self-assignment guard did not exist, this function would still operate correctly during a self-assignment (because all of the operations done by the function can handle self-assignment properly). Because self-assignment is a rare event, some prominent C++ gurus recommend omitting the self-assignment guard even in classes that would benefit from it. We do not recommend this, as we believe it\u2019s a better practice to code defensively and then selectively optimize later. The copy and swap idiom A better way to handle self-assignment issues is via what\u2019s called the copy and swap idiom. There\u2019s a great writeup of how this idiom works on Stack Overflow . Default assignment operator Unlike other operators, the compiler will provide a default public assignment operator for your class if you do not provide one. This assignment operator does memberwise assignment (which is essentially the same as the memberwise initialization that default copy constructors do). Just like other constructors and operators, you can prevent assignments from being made by making your assignment operator private or using the delete keyword: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #include #include class Fraction { private : int m_numerator { 0 }; int m_denominator { 1 }; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { assert ( denominator != 0 ); } // Copy constructor Fraction ( const Fraction & copy ) = delete ; // Overloaded assignment Fraction & operator = ( const Fraction & fraction ) = delete ; // no copies through assignment! friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } int main () { Fraction fiveThirds { 5 , 3 }; Fraction f ; f = fiveThirds ; // compile error, operator= has been deleted std :: cout << f ; return 0 ; }","title":"14.15 - \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26"},{"location":"14-16-shallow-vs-deep-copying/","tags":["shallow copy","deep copy"],"text":"Key Takeaway - \u6d45\u62f7\u8d1d \u56e0\u4e3a C++ \u5e76\u4e0d\u4e86\u89e3\u4f60\u7684\u7c7b\uff0c\u6240\u4ee5\u9ed8\u8ba4\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4ee5\u53ca\u9ed8\u8ba4\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u4f1a\u4f7f\u7528 \u6210\u5458\u4f9d\u6b21\u62f7\u8d1d \uff08\u4e5f\u79f0\u4e3a \u6d45\u62f7\u8d1d \uff09\u7684\u65b9\u5f0f\u4fe1\u606f\u62f7\u8d1d\u3002\u4e5f\u5c31\u662f\u8bf4\uff0cC++ \u4f1a\u4f9d\u6b21\u62f7\u8d1d\u7c7b\u7684\u6bcf\u4e00\u4e2a\u6210\u5458\uff08\u4f7f\u7528\u7b49\u53f7\u662f\u8c03\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\u3001\u5728\u4f7f\u7528\u62f7\u8d1d\u6784\u9020\u65f6\uff0c\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\uff09\u3002\u5bf9\u4e8e\u7b80\u5355\u7c7b\uff08\u4f8b\u5982\uff0c\u4e0d\u5305\u542b\u52a8\u6001\u5185\u5b58\u7684\u7c7b\uff09\uff0c\u8fd9\u4e48\u505a\u975e\u5e38\u5408\u9002\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include class Fraction { private : int m_numerator { 0 }; int m_denominator { 1 }; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator } , m_denominator { denominator } { assert ( denominator != 0 ); } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } \u8fd9\u91cc\u9ed8\u8ba4\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u9ed8\u8ba4\u8d4b\u503c\u8fd0\u7b97\u7b26\u5f62\u5f0f\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include #include class Fraction { private : int m_numerator { 0 }; int m_denominator { 1 }; public : // \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator } , m_denominator { denominator } { assert ( denominator != 0 ); } // \u9690\u5f0f\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u7684\u4e00\u79cd\u53ef\u80fd\u5b9e\u73b0 Fraction ( const Fraction & f ) : m_numerator { f . m_numerator } , m_denominator { f . m_denominator } { } // \u9690\u5f0f\u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u4e00\u79cd\u53ef\u80fd\u5b9e\u73b0 Fraction & operator = ( const Fraction & fraction ) { // \u907f\u514d\u81ea\u6211\u8d4b\u503c if ( this == & fraction ) return * this ; // \u62f7\u8d1d m_numerator = fraction . m_numerator ; m_denominator = fraction . m_denominator ; // \u8fd4\u56de\u5bf9\u8c61\u672c\u8eab\uff0c\u4f7f\u64cd\u4f5c\u7b26\u53ef\u4ee5\u94fe\u5f0f\u8c03\u7528 return * this ; } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } }; \u6ce8\u610f\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u9ed8\u8ba4\u7248\u672c\u7684\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u5f88\u597d\u5730\u5b8c\u6210\u62f7\u8d1d\uff0c\u6211\u4eec\u5b8c\u5168\u6ca1\u6709\u5fc5\u8981\u7f16\u5199\u81ea\u5df1\u7684\u7248\u672c\u3002 \u4f46\u662f\uff0c\u5728\u8bbe\u8ba1\u6301\u6709\u52a8\u6001\u5185\u5b58\u7684\u7c7b\u65f6\uff0c\u6d45\u62f7\u8d1d\u4f1a\u5e26\u6765\u5f88\u591a\u95ee\u9898\uff01\u56e0\u4e3a\u6d45\u62f7\u8d1d\u5728\u62f7\u8d1d\u6307\u9488\u65f6\uff0c\u53ea\u662f\u62f7\u8d1d\u4e86\u6307\u9488\u7684\u5730\u5740\u2014\u2014\u5b83\u5e76\u6ca1\u6709\u5206\u914d\u65b0\u7684\u5730\u5740\u6216\u62f7\u8d1d\u6307\u9488\u6240\u6307\u7684\u5185\u5bb9\u3002 \u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include // for strlen() #include // for assert() class MyString { private : char * m_data {}; int m_length {}; public : MyString ( const char * source = \"\" ) { assert ( source ); // make sure source isn't a null string // Find the length of the string // Plus one character for a terminator m_length = std :: strlen ( source ) + 1 ; // Allocate a buffer equal to this length m_data = new char [ m_length ]; // Copy the parameter string into our internal buffer for ( int i { 0 }; i < m_length ; ++ i ) m_data [ i ] = source [ i ]; } ~ MyString () // destructor { // We need to deallocate our string delete [] m_data ; } char * getString () { return m_data ; } int getLength () { return m_length ; } }; \u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u7c7b\uff0c\u5b83\u5206\u914d\u4e86\u4e00\u6bb5\u5185\u5b58\u7528\u4e8e\u4fdd\u5b58\u4f20\u5165\u7684\u5b57\u7b26\u4e32\u3002\u6ce8\u610f\uff0c\u6211\u4eec\u5e76\u6ca1\u6709\u5b9a\u4e49 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u6216\u8005\u91cd\u8f7d\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002\u56e0\u6b64\uff0cC++\u4f1a\u63d0\u4f9b\u4e00\u4e2a\u9ed8\u8ba4\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u9ed8\u8ba4\u8d4b\u503c\u8fd0\u7b97\u7b26\u4ee5\u4fbf\u6267\u884c\u6d45\u62f7\u8d1d\u3002\u5176\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u770b\u4e0a\u53bb\u5e94\u8be5\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 MyString :: MyString ( const MyString & source ) : m_length { source . m_length } , m_data { source . m_data } { } \u6ce8\u610f\uff0c m_data \u53ea\u662f source.m_data \u7684\u4e00\u4e2a\u6d45\u62f7\u8d1d\u6307\u9488\uff0c\u6240\u4ee5\u5b83\u4eec\u6307\u5411\u540c\u4e00\u5757\u5185\u5b58\u3002 \u73b0\u5728\uff0c \u8003\u8651\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { MyString hello { \"Hello, world!\" }; { MyString copy { hello }; // \u4f7f\u7528\u9ed8\u8ba4\u62f7\u8d1d\u6784\u9020\u51fd\u6570 } // copy \u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u5728\u6b64\u5904\u4f1a\u9500\u6bc1\u3002\u6240\u4ee5\u5176\u6790\u6784\u51fd\u6570UI\u5220\u9664copy\u6301\u6709\u7684\u5b57\u7b26\u4e32\uff0c\u6240\u4ee5hello\u6b64\u65f6\u6301\u6709\u4e00\u4e2a\u60ac\u5782\u6307\u9488 std :: cout << hello . getString () << '\\n' ; // \u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a return 0 ; } \u867d\u7136\u8fd9\u6bb5\u4ee3\u7801\u770b\u8d77\u6765\u6ca1\u5565\u95ee\u9898\uff0c\u4f46\u5b83\u5305\u542b\u4e00\u4e2a\u6f5c\u5728\u7684\u95ee\u9898\uff0c\u5c06\u5bfc\u81f4\u7a0b\u5e8f\u51fa\u73b0 \u672a\u5b9a\u4e49\u884c\u4e3a \uff01 \u8ba9\u6211\u4eec\u9010\u884c\u5206\u6790\u8fd9\u4e2a\u4f8b\u5b50: 1 MyString hello { \"Hello, world!\" }; \u8fd9\u4e00\u884c\u6ca1\u4ec0\u4e48\u95ee\u9898\u3002\u5b83\u8c03\u7528 MyString \u6784\u9020\u51fd\u6570\uff0c\u5206\u914d\u4e00\u6bb5\u5185\u5b58\uff0c\u7136\u540e\u8ba9 hello.m_data \u6307\u5411\u5b83\uff0c\u7136\u540e\u62f7\u8d1d\u5b57\u7b26\u4e32\u201cHello, world!\u201d \u5230\u8be5\u5185\u5b58\u3002 1 MyString copy { hello }; // \u4f7f\u7528\u9ed8\u8ba4\u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u8fd9\u4e00\u884c\u4ee3\u7801\u770b\u4e0a\u53bb\u4e5f\u4eba\u755c\u65e0\u5bb3\uff0c\u4f46\u662f\u5b83\u5176\u5b9e\u662f\u95ee\u9898\u7684\u6839\u6e90\uff01\u8fd9\u884c\u4ee3\u7801\u5728\u6c42\u503c\u65f6\uff0cC++\u4f1a\u4f7f\u7528\u9ed8\u8ba4\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff08\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u63d0\u4f9b\u81ea\u5df1\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff09\u3002\u8be5\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4f1a\u6267\u884c\u6d45\u62f7\u8d1d\uff0c\u5c06 copy.m_data \u521d\u59cb\u5316\u4e3a hello.m_data \u6301\u6709\u7684\u5730\u5740\u3002\u56e0\u6b64\uff0c copy.m_data \u548c hello.m_data \u6307\u5411\u4e00\u5757\u76f8\u540c\u7684\u5185\u5b58\uff01 1 } // copy \u5728\u6b64\u5904\u9500\u6bc1 \u5f53 copy \u79bb\u5f00\u4f5c\u7528\u57df \u65f6\uff0c MyString \u7684\u6790\u6784\u51fd\u6570\u4f1a\u88ab\u8c03\u7528\u3002\u6790\u6784\u51fd\u6570\u4f1a\u5220\u9664 copy.m_data \u548c hello.m_data \u6240\u6307\u7684\u90a3\u6bb5\u5185\u5b58\uff01\u56e0\u6b64\uff0c\u5f53 copy \u88ab\u5220\u9664\u65f6\uff0c\u5176\u6790\u6784\u8fc7\u7a0b\u4e0d\u53ef\u907f\u514d\u5730\u5f71\u54cd\u4e86 hello \u3002\u53d8\u91cf copy \u968f\u540e\u5c31\u88ab\u9500\u6bc1\u4e86\uff0c\u4f46\u662f hello.m_data \u4ecd\u7136\u6307\u5411\u8fd9\u5757\u88ab\u5220\u9664\u7684\u5185\u5b58\uff08\u975e\u6cd5\u5185\u5b58\uff09\uff01 1 std :: cout << hello . getString () << '\\n' ; // \u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a \u73b0\u5728\u4f60\u80fd\u660e\u767d\u4e3a\u4ec0\u4e48\u7a0b\u5e8f\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u4e86\u5427\u3002\u6211\u4eec\u5df2\u7ecf\u5220\u9664\u4e86 hello \u6240\u6307\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u53c8\u5c1d\u8bd5\u6253\u5370\u8fd9\u5757\u5185\u5b58\u7684\u5185\u5bb9\u3002 \u8fd9\u4e2a\u95ee\u9898\u7684\u6839\u6e90\u662f\u7531\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u6267\u884c\u7684\u6d45\u62f7\u8d1d\u5bfc\u81f4\u7684\u2014\u2014\u5728\u221a\u6784\u9020\u51fd\u6570\u6216\u91cd\u8f7d\u8d4b\u503c\u64cd\u4f5c\u7b26\u4e2d\u5bf9\u6307\u9488\u503c\u8fdb\u884c\u6d45\u51e0\u4e4e\u603b\u662f\u4f1a\u5e26\u6765\u9ebb\u70e6\u3002 \u6df1\u62f7\u8d1d \u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\u7684\u529e\u6cd5\u662f\u5bf9\u4efb\u4f55\u975e\u7a7a\u6307\u9488\u8fdb\u884c\u6df1\u62f7\u8d1d\u3002 \u6df1\u62f7\u8d1d \u4f1a\u9996\u5148\u4e3a\u526f\u672c\u5206\u914d\u5185\u5b58\uff0c\u7136\u540e\u518d\u62f7\u8d1d\u5b9e\u9645\u7684\u503c\uff0c\u8fd9\u6837\u4e00\u6765\u526f\u672c\u6301\u6709\u7684\u5185\u5b58\u5c31\u548c\u6e90\u5185\u5b58\u4e0d\u540c\u4e86\u3002\u526f\u672c\u548c\u539f\u672c\u7531\u4e8e\u6301\u6709\u4e0d\u540c\u7684\u5185\u5b58\uff0c\u6240\u4ee5\u4fbf\u4e0d\u4f1a\u4e92\u76f8\u5f71\u54cd\u3002\u8981\u6267\u884c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u5219\u5fc5\u987b\u7f16\u5199\u6211\u4eec\u81ea\u5df1\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002 \u4ee5 MyString \u4e3a\u4e86\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u5982\u4f55\u8fdb\u884c\u6df1\u62f7\u8d1d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 // assumes m_data is initialized void MyString::deepCopy ( const MyString & source ) { // \u9996\u5148\u5e94\u8be5\u91ca\u653e\u5f53\u524d\u5b58\u653e\u7684\u5b57\u7b26\u4e32 delete [] m_data ; // \u56e0\u4e3a m_length \u4e0d\u662f\u6307\u9488\uff0c\u6240\u4ee5\u6d45\u62f7\u8d1d\u5373\u53ef m_length = source . m_length ; // m_data \u662f\u6307\u9488\uff0c\u6240\u4ee5\u5f53\u5b83\u4e0d\u662f\u7a7a\u6307\u9488\u662f\u5c31\u5fc5\u987b\u8fdb\u884c\u6df1\u62f7\u8d1d if ( source . m_data ) { // \u4e3a\u526f\u672c\u5206\u914d\u5185\u5b58 m_data = new char [ m_length ]; // \u62f7\u8d1d for ( int i { 0 }; i < m_length ; ++ i ) m_data [ i ] = source . m_data [ i ]; } else m_data = nullptr ; } // Copy constructor MyString :: MyString ( const MyString & source ) { deepCopy ( source ); } \u4ece\u4e0a\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u770b\u51fa\uff0c\u6df1\u62f7\u8d1d\u88ab\u6d45\u62f7\u8d1d\u8981\u590d\u6742\u4e0d\u5c11\uff01\u9996\u5148\uff0c\u6211\u4eec\u5fc5\u987b\u539f\u672c\u542b\u6709\u5b57\u7b26\u4e32\uff0811\u884c\uff09\u3002\u5982\u679c\u6709\u7684\u8bdd\uff0c\u5219\u5fc5\u987b\u4e3a\u526f\u672c\u5206\u914d\u8db3\u591f\u7684\u5185\u5b58\u7528\u4e8e\u4fdd\u5b58\u5b57\u7b26\u4e32\u7684\u526f\u672c\uff0814\u884c\uff09\u3002\u6700\u540e\uff0c\u6211\u4eec\u5fc5\u987b\u624b\u52a8\u62f7\u8d1d\u5b57\u7b26\u4e32\uff0817\u884c\u548c18\u884c\uff09\u3002 \u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 // Assignment operator MyString & MyString :: operator = ( const MyString & source ) { // check for self-assignment if ( this != & source ) { // now do the deep copy deepCopy ( source ); } return * this ; } \u6ce8\u610f\uff0c\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26\u548c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u975e\u5e38\u7c7b\u4f3c\uff0c\u4f46\u662f\u6709\u4e09\u5904\u660e\u663e\u7684\u4e0d\u540c\uff1a \u6dfb\u52a0\u4e86\u4e00\u4e2a\u81ea\u6211\u590d\u5236\u68c0\u67e5\uff1b \u8fd4\u56de *this \u6307\u9488\u4e00\u822c\u80fd\u591f\u94fe\u5f0f\u8c03\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\uff1b \u5fc5\u987b\u663e\u5f0f\u5730\u91ca\u653e\u5df2\u7ecf\u6301\u6709\u7684\u5b57\u7b26\u4e32\uff08\u907f\u514d m_data \u91cd\u65b0\u5206\u914d\u65f6\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\uff09 \u3002\u8be5\u64cd\u4f5c\u662f\u5728 deepCopy \u5728\u5b8c\u6210\u7684 \u5f53\u8c03\u7528\u91cd\u8f7d\u8d4b\u503c\u64cd\u4f5c\u7b26\u65f6\uff0c\u88ab\u8d4b\u503c\u7684\u9879\u53ef\u80fd\u5df2\u7ecf\u5305\u542b\u4e86\u4ee5\u524d\u7684\u503c\uff0c\u9700\u8981\u786e\u4fdd\u5728\u4e3a\u65b0\u503c\u5206\u914d\u5185\u5b58\u4e4b\u524d\u5c06\u5176\u6e05\u9664\u3002\u5bf9\u4e8e\u975e\u52a8\u6001\u5206\u914d\u7684\u53d8\u91cf(\u5927\u5c0f\u56fa\u5b9a)\uff0c\u6211\u4eec\u4e0d\u5fc5\u8d39\u5fc3\uff0c\u56e0\u4e3a\u65b0\u503c\u53ea\u662f\u8986\u76d6\u65e7\u503c\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u52a8\u6001\u5206\u914d\u7684\u53d8\u91cf\uff0c\u5728\u5206\u914d\u65b0\u5185\u5b58\u4e4b\u524d\u9700\u8981\u663e\u5f0f\u5730\u91ca\u653e\u65e7\u5185\u5b58\u3002\u5982\u679c\u6211\u4eec\u4e0d\u8fd9\u6837\u505a\uff0c\u4ee3\u7801\u4e0d\u4f1a\u5d29\u6e83\u4f46\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\u2014\u2014\u6bcf\u6b21\u8d4b\u503c\u90fd\u4f1a\u541e\u566c\u53ef\u7528\u7684\u5185\u5b58\u3002 \u66f4\u597d\u7684\u89e3\u51b3\u65b9\u6848 \u6807\u51c6\u5e93\u4e2d\u5904\u7406\u52a8\u6001\u5185\u5b58\u7684\u7c7b\uff0c\u5982 std::string \u548c std::vector \uff0c\u5b83\u4eec\u4f1a\u7ba1\u7406\u81ea\u5df1\u6240\u6709\u7684\u5185\u5b58\uff0c\u5e76\u5177\u6709\u6267\u884c\u6df1\u62f7\u8d1d\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002\u56e0\u6b64\uff0c\u4f60\u4e0d\u5fc5\u81ea\u5df1\u7ba1\u7406\u5b83\u4eec\u7684\u5185\u5b58\uff0c\u53ea\u9700\u50cf\u666e\u901a\u57fa\u672c\u53d8\u91cf\u4e00\u6837\u521d\u59cb\u5316\u6216\u5206\u914d\u5b83\u4eec\uff01\u8fd9\u4f7f\u5f97\u8fd9\u4e9b\u7c7b\u66f4\u5bb9\u6613\u4f7f\u7528\uff0c\u66f4\u4e0d\u5bb9\u6613\u51fa\u9519\uff0c\u800c\u4e14\u4f60\u4e0d\u5fc5\u82b1\u65f6\u95f4\u7f16\u5199\u81ea\u5df1\u7684\u91cd\u8f7d\u51fd\u6570! \u5c0f\u7ed3 \u9ed8\u8ba4\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u9ed8\u8ba4\u8d4b\u503c\u64cd\u4f5c\u7b26\u6267\u884c\u6d45\u590d\u5236\uff0c\u8fd9\u5bf9\u4e8e\u4e0d\u5305\u542b\u52a8\u6001\u5206\u914d\u53d8\u91cf\u7684\u7c7b\u6765\u8bf4\u6ca1\u6709\u95ee\u9898\uff1b \u5177\u6709\u52a8\u6001\u5206\u914d\u53d8\u91cf\u7684\u7c7b\u9700\u8981\u6709\u4e00\u4e2a\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\u6765\u6267\u884c\u6df1\u62f7\u8d1d\uff1b \u6700\u597d\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684\u7c7b\uff0c\u800c\u4e0d\u662f\u81ea\u5df1\u8fdb\u884c\u5185\u5b58\u7ba1\u7406\u3002","title":"14.16 - \u6d45\u62f7\u8d1d\u548c\u6df1\u62f7\u8d1d"},{"location":"14-16-shallow-vs-deep-copying/#_1","text":"\u56e0\u4e3a C++ \u5e76\u4e0d\u4e86\u89e3\u4f60\u7684\u7c7b\uff0c\u6240\u4ee5\u9ed8\u8ba4\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4ee5\u53ca\u9ed8\u8ba4\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u4f1a\u4f7f\u7528 \u6210\u5458\u4f9d\u6b21\u62f7\u8d1d \uff08\u4e5f\u79f0\u4e3a \u6d45\u62f7\u8d1d \uff09\u7684\u65b9\u5f0f\u4fe1\u606f\u62f7\u8d1d\u3002\u4e5f\u5c31\u662f\u8bf4\uff0cC++ \u4f1a\u4f9d\u6b21\u62f7\u8d1d\u7c7b\u7684\u6bcf\u4e00\u4e2a\u6210\u5458\uff08\u4f7f\u7528\u7b49\u53f7\u662f\u8c03\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\u3001\u5728\u4f7f\u7528\u62f7\u8d1d\u6784\u9020\u65f6\uff0c\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\uff09\u3002\u5bf9\u4e8e\u7b80\u5355\u7c7b\uff08\u4f8b\u5982\uff0c\u4e0d\u5305\u542b\u52a8\u6001\u5185\u5b58\u7684\u7c7b\uff09\uff0c\u8fd9\u4e48\u505a\u975e\u5e38\u5408\u9002\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include class Fraction { private : int m_numerator { 0 }; int m_denominator { 1 }; public : // Default constructor Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator } , m_denominator { denominator } { assert ( denominator != 0 ); } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ); }; std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } \u8fd9\u91cc\u9ed8\u8ba4\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u9ed8\u8ba4\u8d4b\u503c\u8fd0\u7b97\u7b26\u5f62\u5f0f\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include #include class Fraction { private : int m_numerator { 0 }; int m_denominator { 1 }; public : // \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator } , m_denominator { denominator } { assert ( denominator != 0 ); } // \u9690\u5f0f\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u7684\u4e00\u79cd\u53ef\u80fd\u5b9e\u73b0 Fraction ( const Fraction & f ) : m_numerator { f . m_numerator } , m_denominator { f . m_denominator } { } // \u9690\u5f0f\u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u4e00\u79cd\u53ef\u80fd\u5b9e\u73b0 Fraction & operator = ( const Fraction & fraction ) { // \u907f\u514d\u81ea\u6211\u8d4b\u503c if ( this == & fraction ) return * this ; // \u62f7\u8d1d m_numerator = fraction . m_numerator ; m_denominator = fraction . m_denominator ; // \u8fd4\u56de\u5bf9\u8c61\u672c\u8eab\uff0c\u4f7f\u64cd\u4f5c\u7b26\u53ef\u4ee5\u94fe\u5f0f\u8c03\u7528 return * this ; } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } }; \u6ce8\u610f\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u9ed8\u8ba4\u7248\u672c\u7684\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u5f88\u597d\u5730\u5b8c\u6210\u62f7\u8d1d\uff0c\u6211\u4eec\u5b8c\u5168\u6ca1\u6709\u5fc5\u8981\u7f16\u5199\u81ea\u5df1\u7684\u7248\u672c\u3002 \u4f46\u662f\uff0c\u5728\u8bbe\u8ba1\u6301\u6709\u52a8\u6001\u5185\u5b58\u7684\u7c7b\u65f6\uff0c\u6d45\u62f7\u8d1d\u4f1a\u5e26\u6765\u5f88\u591a\u95ee\u9898\uff01\u56e0\u4e3a\u6d45\u62f7\u8d1d\u5728\u62f7\u8d1d\u6307\u9488\u65f6\uff0c\u53ea\u662f\u62f7\u8d1d\u4e86\u6307\u9488\u7684\u5730\u5740\u2014\u2014\u5b83\u5e76\u6ca1\u6709\u5206\u914d\u65b0\u7684\u5730\u5740\u6216\u62f7\u8d1d\u6307\u9488\u6240\u6307\u7684\u5185\u5bb9\u3002 \u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include // for strlen() #include // for assert() class MyString { private : char * m_data {}; int m_length {}; public : MyString ( const char * source = \"\" ) { assert ( source ); // make sure source isn't a null string // Find the length of the string // Plus one character for a terminator m_length = std :: strlen ( source ) + 1 ; // Allocate a buffer equal to this length m_data = new char [ m_length ]; // Copy the parameter string into our internal buffer for ( int i { 0 }; i < m_length ; ++ i ) m_data [ i ] = source [ i ]; } ~ MyString () // destructor { // We need to deallocate our string delete [] m_data ; } char * getString () { return m_data ; } int getLength () { return m_length ; } }; \u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u7c7b\uff0c\u5b83\u5206\u914d\u4e86\u4e00\u6bb5\u5185\u5b58\u7528\u4e8e\u4fdd\u5b58\u4f20\u5165\u7684\u5b57\u7b26\u4e32\u3002\u6ce8\u610f\uff0c\u6211\u4eec\u5e76\u6ca1\u6709\u5b9a\u4e49 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u6216\u8005\u91cd\u8f7d\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002\u56e0\u6b64\uff0cC++\u4f1a\u63d0\u4f9b\u4e00\u4e2a\u9ed8\u8ba4\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u9ed8\u8ba4\u8d4b\u503c\u8fd0\u7b97\u7b26\u4ee5\u4fbf\u6267\u884c\u6d45\u62f7\u8d1d\u3002\u5176\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u770b\u4e0a\u53bb\u5e94\u8be5\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 MyString :: MyString ( const MyString & source ) : m_length { source . m_length } , m_data { source . m_data } { } \u6ce8\u610f\uff0c m_data \u53ea\u662f source.m_data \u7684\u4e00\u4e2a\u6d45\u62f7\u8d1d\u6307\u9488\uff0c\u6240\u4ee5\u5b83\u4eec\u6307\u5411\u540c\u4e00\u5757\u5185\u5b58\u3002 \u73b0\u5728\uff0c \u8003\u8651\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { MyString hello { \"Hello, world!\" }; { MyString copy { hello }; // \u4f7f\u7528\u9ed8\u8ba4\u62f7\u8d1d\u6784\u9020\u51fd\u6570 } // copy \u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u5728\u6b64\u5904\u4f1a\u9500\u6bc1\u3002\u6240\u4ee5\u5176\u6790\u6784\u51fd\u6570UI\u5220\u9664copy\u6301\u6709\u7684\u5b57\u7b26\u4e32\uff0c\u6240\u4ee5hello\u6b64\u65f6\u6301\u6709\u4e00\u4e2a\u60ac\u5782\u6307\u9488 std :: cout << hello . getString () << '\\n' ; // \u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a return 0 ; } \u867d\u7136\u8fd9\u6bb5\u4ee3\u7801\u770b\u8d77\u6765\u6ca1\u5565\u95ee\u9898\uff0c\u4f46\u5b83\u5305\u542b\u4e00\u4e2a\u6f5c\u5728\u7684\u95ee\u9898\uff0c\u5c06\u5bfc\u81f4\u7a0b\u5e8f\u51fa\u73b0 \u672a\u5b9a\u4e49\u884c\u4e3a \uff01 \u8ba9\u6211\u4eec\u9010\u884c\u5206\u6790\u8fd9\u4e2a\u4f8b\u5b50: 1 MyString hello { \"Hello, world!\" }; \u8fd9\u4e00\u884c\u6ca1\u4ec0\u4e48\u95ee\u9898\u3002\u5b83\u8c03\u7528 MyString \u6784\u9020\u51fd\u6570\uff0c\u5206\u914d\u4e00\u6bb5\u5185\u5b58\uff0c\u7136\u540e\u8ba9 hello.m_data \u6307\u5411\u5b83\uff0c\u7136\u540e\u62f7\u8d1d\u5b57\u7b26\u4e32\u201cHello, world!\u201d \u5230\u8be5\u5185\u5b58\u3002 1 MyString copy { hello }; // \u4f7f\u7528\u9ed8\u8ba4\u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u8fd9\u4e00\u884c\u4ee3\u7801\u770b\u4e0a\u53bb\u4e5f\u4eba\u755c\u65e0\u5bb3\uff0c\u4f46\u662f\u5b83\u5176\u5b9e\u662f\u95ee\u9898\u7684\u6839\u6e90\uff01\u8fd9\u884c\u4ee3\u7801\u5728\u6c42\u503c\u65f6\uff0cC++\u4f1a\u4f7f\u7528\u9ed8\u8ba4\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff08\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u63d0\u4f9b\u81ea\u5df1\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff09\u3002\u8be5\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4f1a\u6267\u884c\u6d45\u62f7\u8d1d\uff0c\u5c06 copy.m_data \u521d\u59cb\u5316\u4e3a hello.m_data \u6301\u6709\u7684\u5730\u5740\u3002\u56e0\u6b64\uff0c copy.m_data \u548c hello.m_data \u6307\u5411\u4e00\u5757\u76f8\u540c\u7684\u5185\u5b58\uff01 1 } // copy \u5728\u6b64\u5904\u9500\u6bc1 \u5f53 copy \u79bb\u5f00\u4f5c\u7528\u57df \u65f6\uff0c MyString \u7684\u6790\u6784\u51fd\u6570\u4f1a\u88ab\u8c03\u7528\u3002\u6790\u6784\u51fd\u6570\u4f1a\u5220\u9664 copy.m_data \u548c hello.m_data \u6240\u6307\u7684\u90a3\u6bb5\u5185\u5b58\uff01\u56e0\u6b64\uff0c\u5f53 copy \u88ab\u5220\u9664\u65f6\uff0c\u5176\u6790\u6784\u8fc7\u7a0b\u4e0d\u53ef\u907f\u514d\u5730\u5f71\u54cd\u4e86 hello \u3002\u53d8\u91cf copy \u968f\u540e\u5c31\u88ab\u9500\u6bc1\u4e86\uff0c\u4f46\u662f hello.m_data \u4ecd\u7136\u6307\u5411\u8fd9\u5757\u88ab\u5220\u9664\u7684\u5185\u5b58\uff08\u975e\u6cd5\u5185\u5b58\uff09\uff01 1 std :: cout << hello . getString () << '\\n' ; // \u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a \u73b0\u5728\u4f60\u80fd\u660e\u767d\u4e3a\u4ec0\u4e48\u7a0b\u5e8f\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u4e86\u5427\u3002\u6211\u4eec\u5df2\u7ecf\u5220\u9664\u4e86 hello \u6240\u6307\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u53c8\u5c1d\u8bd5\u6253\u5370\u8fd9\u5757\u5185\u5b58\u7684\u5185\u5bb9\u3002 \u8fd9\u4e2a\u95ee\u9898\u7684\u6839\u6e90\u662f\u7531\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u6267\u884c\u7684\u6d45\u62f7\u8d1d\u5bfc\u81f4\u7684\u2014\u2014\u5728\u221a\u6784\u9020\u51fd\u6570\u6216\u91cd\u8f7d\u8d4b\u503c\u64cd\u4f5c\u7b26\u4e2d\u5bf9\u6307\u9488\u503c\u8fdb\u884c\u6d45\u51e0\u4e4e\u603b\u662f\u4f1a\u5e26\u6765\u9ebb\u70e6\u3002","title":"\u6d45\u62f7\u8d1d"},{"location":"14-16-shallow-vs-deep-copying/#_2","text":"\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\u7684\u529e\u6cd5\u662f\u5bf9\u4efb\u4f55\u975e\u7a7a\u6307\u9488\u8fdb\u884c\u6df1\u62f7\u8d1d\u3002 \u6df1\u62f7\u8d1d \u4f1a\u9996\u5148\u4e3a\u526f\u672c\u5206\u914d\u5185\u5b58\uff0c\u7136\u540e\u518d\u62f7\u8d1d\u5b9e\u9645\u7684\u503c\uff0c\u8fd9\u6837\u4e00\u6765\u526f\u672c\u6301\u6709\u7684\u5185\u5b58\u5c31\u548c\u6e90\u5185\u5b58\u4e0d\u540c\u4e86\u3002\u526f\u672c\u548c\u539f\u672c\u7531\u4e8e\u6301\u6709\u4e0d\u540c\u7684\u5185\u5b58\uff0c\u6240\u4ee5\u4fbf\u4e0d\u4f1a\u4e92\u76f8\u5f71\u54cd\u3002\u8981\u6267\u884c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u5219\u5fc5\u987b\u7f16\u5199\u6211\u4eec\u81ea\u5df1\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002 \u4ee5 MyString \u4e3a\u4e86\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u5982\u4f55\u8fdb\u884c\u6df1\u62f7\u8d1d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 // assumes m_data is initialized void MyString::deepCopy ( const MyString & source ) { // \u9996\u5148\u5e94\u8be5\u91ca\u653e\u5f53\u524d\u5b58\u653e\u7684\u5b57\u7b26\u4e32 delete [] m_data ; // \u56e0\u4e3a m_length \u4e0d\u662f\u6307\u9488\uff0c\u6240\u4ee5\u6d45\u62f7\u8d1d\u5373\u53ef m_length = source . m_length ; // m_data \u662f\u6307\u9488\uff0c\u6240\u4ee5\u5f53\u5b83\u4e0d\u662f\u7a7a\u6307\u9488\u662f\u5c31\u5fc5\u987b\u8fdb\u884c\u6df1\u62f7\u8d1d if ( source . m_data ) { // \u4e3a\u526f\u672c\u5206\u914d\u5185\u5b58 m_data = new char [ m_length ]; // \u62f7\u8d1d for ( int i { 0 }; i < m_length ; ++ i ) m_data [ i ] = source . m_data [ i ]; } else m_data = nullptr ; } // Copy constructor MyString :: MyString ( const MyString & source ) { deepCopy ( source ); } \u4ece\u4e0a\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u770b\u51fa\uff0c\u6df1\u62f7\u8d1d\u88ab\u6d45\u62f7\u8d1d\u8981\u590d\u6742\u4e0d\u5c11\uff01\u9996\u5148\uff0c\u6211\u4eec\u5fc5\u987b\u539f\u672c\u542b\u6709\u5b57\u7b26\u4e32\uff0811\u884c\uff09\u3002\u5982\u679c\u6709\u7684\u8bdd\uff0c\u5219\u5fc5\u987b\u4e3a\u526f\u672c\u5206\u914d\u8db3\u591f\u7684\u5185\u5b58\u7528\u4e8e\u4fdd\u5b58\u5b57\u7b26\u4e32\u7684\u526f\u672c\uff0814\u884c\uff09\u3002\u6700\u540e\uff0c\u6211\u4eec\u5fc5\u987b\u624b\u52a8\u62f7\u8d1d\u5b57\u7b26\u4e32\uff0817\u884c\u548c18\u884c\uff09\u3002 \u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 // Assignment operator MyString & MyString :: operator = ( const MyString & source ) { // check for self-assignment if ( this != & source ) { // now do the deep copy deepCopy ( source ); } return * this ; } \u6ce8\u610f\uff0c\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26\u548c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u975e\u5e38\u7c7b\u4f3c\uff0c\u4f46\u662f\u6709\u4e09\u5904\u660e\u663e\u7684\u4e0d\u540c\uff1a \u6dfb\u52a0\u4e86\u4e00\u4e2a\u81ea\u6211\u590d\u5236\u68c0\u67e5\uff1b \u8fd4\u56de *this \u6307\u9488\u4e00\u822c\u80fd\u591f\u94fe\u5f0f\u8c03\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\uff1b \u5fc5\u987b\u663e\u5f0f\u5730\u91ca\u653e\u5df2\u7ecf\u6301\u6709\u7684\u5b57\u7b26\u4e32\uff08\u907f\u514d m_data \u91cd\u65b0\u5206\u914d\u65f6\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\uff09 \u3002\u8be5\u64cd\u4f5c\u662f\u5728 deepCopy \u5728\u5b8c\u6210\u7684 \u5f53\u8c03\u7528\u91cd\u8f7d\u8d4b\u503c\u64cd\u4f5c\u7b26\u65f6\uff0c\u88ab\u8d4b\u503c\u7684\u9879\u53ef\u80fd\u5df2\u7ecf\u5305\u542b\u4e86\u4ee5\u524d\u7684\u503c\uff0c\u9700\u8981\u786e\u4fdd\u5728\u4e3a\u65b0\u503c\u5206\u914d\u5185\u5b58\u4e4b\u524d\u5c06\u5176\u6e05\u9664\u3002\u5bf9\u4e8e\u975e\u52a8\u6001\u5206\u914d\u7684\u53d8\u91cf(\u5927\u5c0f\u56fa\u5b9a)\uff0c\u6211\u4eec\u4e0d\u5fc5\u8d39\u5fc3\uff0c\u56e0\u4e3a\u65b0\u503c\u53ea\u662f\u8986\u76d6\u65e7\u503c\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u52a8\u6001\u5206\u914d\u7684\u53d8\u91cf\uff0c\u5728\u5206\u914d\u65b0\u5185\u5b58\u4e4b\u524d\u9700\u8981\u663e\u5f0f\u5730\u91ca\u653e\u65e7\u5185\u5b58\u3002\u5982\u679c\u6211\u4eec\u4e0d\u8fd9\u6837\u505a\uff0c\u4ee3\u7801\u4e0d\u4f1a\u5d29\u6e83\u4f46\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\u2014\u2014\u6bcf\u6b21\u8d4b\u503c\u90fd\u4f1a\u541e\u566c\u53ef\u7528\u7684\u5185\u5b58\u3002","title":"\u6df1\u62f7\u8d1d"},{"location":"14-16-shallow-vs-deep-copying/#_3","text":"\u6807\u51c6\u5e93\u4e2d\u5904\u7406\u52a8\u6001\u5185\u5b58\u7684\u7c7b\uff0c\u5982 std::string \u548c std::vector \uff0c\u5b83\u4eec\u4f1a\u7ba1\u7406\u81ea\u5df1\u6240\u6709\u7684\u5185\u5b58\uff0c\u5e76\u5177\u6709\u6267\u884c\u6df1\u62f7\u8d1d\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002\u56e0\u6b64\uff0c\u4f60\u4e0d\u5fc5\u81ea\u5df1\u7ba1\u7406\u5b83\u4eec\u7684\u5185\u5b58\uff0c\u53ea\u9700\u50cf\u666e\u901a\u57fa\u672c\u53d8\u91cf\u4e00\u6837\u521d\u59cb\u5316\u6216\u5206\u914d\u5b83\u4eec\uff01\u8fd9\u4f7f\u5f97\u8fd9\u4e9b\u7c7b\u66f4\u5bb9\u6613\u4f7f\u7528\uff0c\u66f4\u4e0d\u5bb9\u6613\u51fa\u9519\uff0c\u800c\u4e14\u4f60\u4e0d\u5fc5\u82b1\u65f6\u95f4\u7f16\u5199\u81ea\u5df1\u7684\u91cd\u8f7d\u51fd\u6570!","title":"\u66f4\u597d\u7684\u89e3\u51b3\u65b9\u6848"},{"location":"14-16-shallow-vs-deep-copying/#_4","text":"\u9ed8\u8ba4\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u9ed8\u8ba4\u8d4b\u503c\u64cd\u4f5c\u7b26\u6267\u884c\u6d45\u590d\u5236\uff0c\u8fd9\u5bf9\u4e8e\u4e0d\u5305\u542b\u52a8\u6001\u5206\u914d\u53d8\u91cf\u7684\u7c7b\u6765\u8bf4\u6ca1\u6709\u95ee\u9898\uff1b \u5177\u6709\u52a8\u6001\u5206\u914d\u53d8\u91cf\u7684\u7c7b\u9700\u8981\u6709\u4e00\u4e2a\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\u6765\u6267\u884c\u6df1\u62f7\u8d1d\uff1b \u6700\u597d\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684\u7c7b\uff0c\u800c\u4e0d\u662f\u81ea\u5df1\u8fdb\u884c\u5185\u5b58\u7ba1\u7406\u3002","title":"\u5c0f\u7ed3"},{"location":"14-17-overloading-operators-and-function-templates/","tags":["shallow copy","deep copy"],"text":"Key Takeaway - In lesson 8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316 \uff0cwe discussed how the compiler will use function templates to instantiate functions, which are then compiled. We also noted that these functions may not compile, if the code in the function template tries to perform some operation that the actual type doesn\u2019t support (such as adding integer value 1 to a std::string ). In this lesson, we\u2019ll take a look at a few examples where our instantiated functions won\u2019t compile because our actual class types don\u2019t support those operators, and show how we can define those operators so that the instantiated functions will then compile. Operators, function calls, and function templates First, let\u2019s create a simple class: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } friend std :: ostream & operator << ( std :: ostream & ostr , const Cents & c ) { ostr << c . m_cents ; return ostr ; } }; COPY and define a max function template: 1 2 3 4 5 template < typename T > const T & max ( T x , T y ) { return ( x < y ) ? y : x ; } COPY Now, let\u2019s see what happens when we try to call max() with object of type Cents : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } friend std :: ostream & operator << ( std :: ostream & ostr , const Cents & c ) { ostr << c . m_cents ; return ostr ; } }; template < typename T > const T & max ( const T & x , const T & y ) { return ( x < y ) ? y : x ; } int main () { Cents nickel { 5 }; Cents dime { 10 }; Cents bigger = max ( nickel , dime ); std :: cout << bigger << \" is bigger \\n \" ; return 0 ; } C++ will create a template instance for max() that looks like this: 1 2 3 4 5 template <> const Cents & max ( const Cents & x , const Cents & y ) { return ( x < y ) ? y : x ; } And then it will try to compile this function. See the problem here? C++ has no idea how to evaluate x < y when x and y are of type Cents ! Consequently, this will produce a compile error. To get around this problem, simply overload operator< for any class we wish to use max with: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } friend bool operator < ( const Cents & c1 , const Cents & c2 ) { return ( c1 . m_cents < c2 . m_cents ); } friend std :: ostream & operator << ( std :: ostream & ostr , const Cents & c ) { ostr << c . m_cents ; return ostr ; } }; template < typename T > const T & max ( const T & x , const T & y ) { return ( x < y ) ? y : x ; } int main () { Cents nickel { 5 }; Cents dime { 10 }; Cents bigger = max ( nickel , dime ); std :: cout << bigger << \" is bigger \\n \" ; return 0 ; } This works as expected, and prints: 1 10 is bigger Another example Let\u2019s do one more example of a function template not working because of missing overloaded operators. The following function template will calculate the average of a number of objects in an array: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include template < typename T > T average ( const T * myArray , int numValues ) { T sum { 0 }; for ( int count { 0 }; count < numValues ; ++ count ) sum += myArray [ count ]; sum /= numValues ; return sum ; } int main () { int intArray [] { 5 , 3 , 2 , 1 , 4 }; std :: cout << average ( intArray , 5 ) << '\\n' ; double doubleArray [] { 3.12 , 3.45 , 9.23 , 6.34 }; std :: cout << average ( doubleArray , 4 ) << '\\n' ; return 0 ; } COPY This produces the values: 1 2 3 5.535 As you can see, it works great for built-in types! Now let\u2019s see what happens when we call this function on our Cents class: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include template < typename T > T average ( const T * myArray , int numValues ) { T sum { 0 }; for ( int count { 0 }; count < numValues ; ++ count ) sum += myArray [ count ]; sum /= numValues ; return sum ; } class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } }; int main () { Cents centsArray [] { Cents { 5 }, Cents { 10 }, Cents { 15 }, Cents { 14 } }; std :: cout << average ( centsArray , 4 ) << '\\n' ; return 0 ; } The compiler goes berserk and produces a ton of error messages! The first error message will be something like this: 1 error C2679: binary << : no operator found which takes a right-hand operand of type Cents (or there is no acceptable conversion) Remember that average() returns a Cents object, and we are trying to stream that object to std::cout using operator<< . However, we haven\u2019t defined the operator<< for our Cents class yet. Let\u2019s do that: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include template < typename T > T average ( const T * myArray , int numValues ) { T sum { 0 }; for ( int count { 0 }; count < numValues ; ++ count ) sum += myArray [ count ]; sum /= numValues ; return sum ; } class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } friend std :: ostream & operator << ( std :: ostream & out , const Cents & cents ) { out << cents . m_cents << \" cents \" ; return out ; } }; int main () { Cents centsArray [] { Cents { 5 }, Cents { 10 }, Cents { 15 }, Cents { 14 } }; std :: cout << average ( centsArray , 4 ) << '\\n' ; return 0 ; } If we compile again, we will get another error: 1 error C2676: binary += : Cents does not define this operator or a conversion to a type acceptable to the predefined operator This error is actually being caused by the function template instance created when we call average(const Cents*, int) . Remember that when we call a templated function, the compiler \u201cstencils\u201d out a copy of the function where the template type parameters (the placeholder types) have been replaced with the actual types in the function call. Here is the function template instance for average() when T is a Cents object: 1 2 3 4 5 6 7 8 9 10 template <> Cents average ( const Cents * myArray , int numValues ) { Cents sum { 0 }; for ( int count { 0 }; count < numValues ; ++ count ) sum += myArray [ count ]; sum /= numValues ; return sum ; } COPY The reason we are getting an error message is because of the following line: 1 sum += myArray [ count ]; COPY In this case, sum is a Cents object, but we have not defined operator+= for Cents objects! We will need to define this function in order for average() to be able to work with Cents . Looking forward, we can see that average() also uses the operator/= , so we will go ahead and define that as well: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include template < typename T > T average ( const T * myArray , int numValues ) { T sum { 0 }; for ( int count { 0 }; count < numValues ; ++ count ) sum += myArray [ count ]; sum /= numValues ; return sum ; } class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } friend std :: ostream & operator << ( std :: ostream & out , const Cents & cents ) { out << cents . m_cents << \" cents \" ; return out ; } Cents & operator += ( const Cents & cents ) { m_cents += cents . m_cents ; return * this ; } Cents & operator /= ( int x ) { m_cents /= x ; return * this ; } }; int main () { Cents centsArray [] { Cents { 5 }, Cents { 10 }, Cents { 15 }, Cents { 14 } }; std :: cout << average ( centsArray , 4 ) << '\\n' ; return 0 ; } COPY Finally, our code will compile and run! Here is the result: 1 11 cents Note that we didn\u2019t have to modify average() at all to make it work with objects of type Cents . We simply had to define the operators used to implement average() for the Cents class, and the compiler took care of the rest!","title":"14.17 - \u91cd\u8f7d\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u6a21\u677f"},{"location":"14-17-overloading-operators-and-function-templates/#another-example","text":"Let\u2019s do one more example of a function template not working because of missing overloaded operators. The following function template will calculate the average of a number of objects in an array: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include template < typename T > T average ( const T * myArray , int numValues ) { T sum { 0 }; for ( int count { 0 }; count < numValues ; ++ count ) sum += myArray [ count ]; sum /= numValues ; return sum ; } int main () { int intArray [] { 5 , 3 , 2 , 1 , 4 }; std :: cout << average ( intArray , 5 ) << '\\n' ; double doubleArray [] { 3.12 , 3.45 , 9.23 , 6.34 }; std :: cout << average ( doubleArray , 4 ) << '\\n' ; return 0 ; } COPY This produces the values: 1 2 3 5.535 As you can see, it works great for built-in types! Now let\u2019s see what happens when we call this function on our Cents class: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include template < typename T > T average ( const T * myArray , int numValues ) { T sum { 0 }; for ( int count { 0 }; count < numValues ; ++ count ) sum += myArray [ count ]; sum /= numValues ; return sum ; } class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } }; int main () { Cents centsArray [] { Cents { 5 }, Cents { 10 }, Cents { 15 }, Cents { 14 } }; std :: cout << average ( centsArray , 4 ) << '\\n' ; return 0 ; } The compiler goes berserk and produces a ton of error messages! The first error message will be something like this: 1 error C2679: binary << : no operator found which takes a right-hand operand of type Cents (or there is no acceptable conversion) Remember that average() returns a Cents object, and we are trying to stream that object to std::cout using operator<< . However, we haven\u2019t defined the operator<< for our Cents class yet. Let\u2019s do that: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include template < typename T > T average ( const T * myArray , int numValues ) { T sum { 0 }; for ( int count { 0 }; count < numValues ; ++ count ) sum += myArray [ count ]; sum /= numValues ; return sum ; } class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } friend std :: ostream & operator << ( std :: ostream & out , const Cents & cents ) { out << cents . m_cents << \" cents \" ; return out ; } }; int main () { Cents centsArray [] { Cents { 5 }, Cents { 10 }, Cents { 15 }, Cents { 14 } }; std :: cout << average ( centsArray , 4 ) << '\\n' ; return 0 ; } If we compile again, we will get another error: 1 error C2676: binary += : Cents does not define this operator or a conversion to a type acceptable to the predefined operator This error is actually being caused by the function template instance created when we call average(const Cents*, int) . Remember that when we call a templated function, the compiler \u201cstencils\u201d out a copy of the function where the template type parameters (the placeholder types) have been replaced with the actual types in the function call. Here is the function template instance for average() when T is a Cents object: 1 2 3 4 5 6 7 8 9 10 template <> Cents average ( const Cents * myArray , int numValues ) { Cents sum { 0 }; for ( int count { 0 }; count < numValues ; ++ count ) sum += myArray [ count ]; sum /= numValues ; return sum ; } COPY The reason we are getting an error message is because of the following line: 1 sum += myArray [ count ]; COPY In this case, sum is a Cents object, but we have not defined operator+= for Cents objects! We will need to define this function in order for average() to be able to work with Cents . Looking forward, we can see that average() also uses the operator/= , so we will go ahead and define that as well: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include template < typename T > T average ( const T * myArray , int numValues ) { T sum { 0 }; for ( int count { 0 }; count < numValues ; ++ count ) sum += myArray [ count ]; sum /= numValues ; return sum ; } class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } friend std :: ostream & operator << ( std :: ostream & out , const Cents & cents ) { out << cents . m_cents << \" cents \" ; return out ; } Cents & operator += ( const Cents & cents ) { m_cents += cents . m_cents ; return * this ; } Cents & operator /= ( int x ) { m_cents /= x ; return * this ; } }; int main () { Cents centsArray [] { Cents { 5 }, Cents { 10 }, Cents { 15 }, Cents { 14 } }; std :: cout << average ( centsArray , 4 ) << '\\n' ; return 0 ; } COPY Finally, our code will compile and run! Here is the result: 1 11 cents Note that we didn\u2019t have to modify average() at all to make it work with objects of type Cents . We simply had to define the operators used to implement average() for the Cents class, and the compiler took care of the rest!","title":"Another example"},{"location":"14-2-overloading-the-arithmetic-operators-using-friend-functions/","tags":["overload","friend","operator"],"text":"Key Takeaway Some of the most commonly used operators in C++ are the arithmetic operators -- that is, the plus operator (+), minus operator (-), multiplication operator (*), and division operator (/). Note that all of the arithmetic operators are binary operators -- meaning they take two operands -- one on each side of the operator. All four of these operators are overloaded in the exact same way. It turns out that there are three different ways to overload operators: the member function way, the friend function way, and the normal function way. In this lesson, we\u2019ll cover the friend function way (because it\u2019s more intuitive for most binary operators). Next lesson, we\u2019ll discuss the normal function way. Finally, in a later lesson in this chapter, we\u2019ll cover the member function way. And, of course, we\u2019ll also summarize when to use each in more detail. Overloading operators using friend functions Consider the following trivial class: 1 2 3 4 5 6 7 8 9 class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } int getCents () const { return m_cents ; } }; COPY The following example shows how to overload operator plus (+) in order to add two \u201cCents\u201d objects together: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } // add Cents + Cents using a friend function friend Cents operator + ( const Cents & c1 , const Cents & c2 ); int getCents () const { return m_cents ; } }; // note: this function is not a member function! Cents operator + ( const Cents & c1 , const Cents & c2 ) { // use the Cents constructor and operator+(int, int) // we can access m_cents directly because this is a friend function return Cents { c1 . m_cents + c2 . m_cents }; } int main () { Cents cents1 { 6 }; Cents cents2 { 8 }; Cents centsSum { cents1 + cents2 }; std :: cout << \"I have \" << centsSum . getCents () << \" cents. \\n \" ; return 0 ; } COPY This produces the result: I have 14 cents. Overloading the plus operator (+) is as simple as declaring a function named operator+, giving it two parameters of the type of the operands we want to add, picking an appropriate return type, and then writing the function. In the case of our Cents object, implementing our operator+() function is very simple. First, the parameter types: in this version of operator+, we are going to add two Cents objects together, so our function will take two objects of type Cents. Second, the return type: our operator+ is going to return a result of type Cents, so that\u2019s our return type. Finally, implementation: to add two Cents objects together, we really need to add the m_cents member from each Cents object. Because our overloaded operator+() function is a friend of the class, we can access the m_cents member of our parameters directly. Also, because m_cents is an integer, and C++ knows how to add integers together using the built-in version of the plus operator that works with integer operands, we can simply use the + operator to do the adding. Overloading the subtraction operator (-) is simple as well: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } // add Cents + Cents using a friend function friend Cents operator + ( const Cents & c1 , const Cents & c2 ); // subtract Cents - Cents using a friend function friend Cents operator - ( const Cents & c1 , const Cents & c2 ); int getCents () const { return m_cents ; } }; // note: this function is not a member function! Cents operator + ( const Cents & c1 , const Cents & c2 ) { // use the Cents constructor and operator+(int, int) // we can access m_cents directly because this is a friend function return Cents { c1 . m_cents + c2 . m_cents }; } // note: this function is not a member function! Cents operator - ( const Cents & c1 , const Cents & c2 ) { // use the Cents constructor and operator-(int, int) // we can access m_cents directly because this is a friend function return Cents ( c1 . m_cents - c2 . m_cents ); } int main () { Cents cents1 { 6 }; Cents cents2 { 2 }; Cents centsSum { cents1 - cents2 }; std :: cout << \"I have \" << centsSum . getCents () << \" cents. \\n \" ; return 0 ; } COPY Overloading the multiplication operator ( ) and the division operator (/) is as easy as defining functions for operator and operator/ respectively. Friend functions can be defined inside the class Even though friend functions are not members of the class, they can still be defined inside the class if desired: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } // add Cents + Cents using a friend function // This function is not considered a member of the class, even though the definition is inside the class friend Cents operator + ( const Cents & c1 , const Cents & c2 ) { // use the Cents constructor and operator+(int, int) // we can access m_cents directly because this is a friend function return Cents { c1 . m_cents + c2 . m_cents }; } int getCents () const { return m_cents ; } }; int main () { Cents cents1 { 6 }; Cents cents2 { 8 }; Cents centsSum { cents1 + cents2 }; std :: cout << \"I have \" << centsSum . getCents () << \" cents. \\n \" ; return 0 ; } COPY We generally don\u2019t recommend this, as non-trivial function definitions are better kept in a separate .cpp file, outside of the class definition. However, we will use this pattern in future tutorials to keep the examples concise. Overloading operators for operands of different types Often it is the case that you want your overloaded operators to work with operands that are different types. For example, if we have Cents(4), we may want to add the integer 6 to this to produce the result Cents(10). When C++ evaluates the expression x + y , x becomes the first parameter, and y becomes the second parameter. When x and y have the same type, it does not matter if you add x + y or y + x -- either way, the same version of operator+ gets called. However, when the operands have different types, x + y does not call the same function as y + x. For example, Cents(4) + 6 would call operator+(Cents, int), and 6 + Cents(4) would call operator+(int, Cents). Consequently, whenever we overload binary operators for operands of different types, we actually need to write two functions -- one for each case. Here is an example of that: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } // add Cents + int using a friend function friend Cents operator + ( const Cents & c1 , int value ); // add int + Cents using a friend function friend Cents operator + ( int value , const Cents & c1 ); int getCents () const { return m_cents ; } }; // note: this function is not a member function! Cents operator + ( const Cents & c1 , int value ) { // use the Cents constructor and operator+(int, int) // we can access m_cents directly because this is a friend function return { c1 . m_cents + value }; } // note: this function is not a member function! Cents operator + ( int value , const Cents & c1 ) { // use the Cents constructor and operator+(int, int) // we can access m_cents directly because this is a friend function return { c1 . m_cents + value }; } int main () { Cents c1 { Cents { 4 } + 6 }; Cents c2 { 6 + Cents { 4 } }; std :: cout << \"I have \" << c1 . getCents () << \" cents. \\n \" ; std :: cout << \"I have \" << c2 . getCents () << \" cents. \\n \" ; return 0 ; } COPY Note that both overloaded functions have the same implementation -- that\u2019s because they do the same thing, they just take their parameters in a different order. Another example Let\u2019s take a look at another example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 #include class MinMax { private : int m_min {}; // The min value seen so far int m_max {}; // The max value seen so far public : MinMax ( int min , int max ) : m_min { min }, m_max { max } { } int getMin () const { return m_min ; } int getMax () const { return m_max ; } friend MinMax operator + ( const MinMax & m1 , const MinMax & m2 ); friend MinMax operator + ( const MinMax & m , int value ); friend MinMax operator + ( int value , const MinMax & m ); }; MinMax operator + ( const MinMax & m1 , const MinMax & m2 ) { // Get the minimum value seen in m1 and m2 int min { m1 . m_min < m2 . m_min ? m1 . m_min : m2 . m_min }; // Get the maximum value seen in m1 and m2 int max { m1 . m_max > m2 . m_max ? m1 . m_max : m2 . m_max }; return { min , max }; } MinMax operator + ( const MinMax & m , int value ) { // Get the minimum value seen in m and value int min { m . m_min < value ? m . m_min : value }; // Get the maximum value seen in m and value int max { m . m_max > value ? m . m_max : value }; return { min , max }; } MinMax operator + ( int value , const MinMax & m ) { // call operator+(MinMax, int) return { m + value }; } int main () { MinMax m1 { 10 , 15 }; MinMax m2 { 8 , 11 }; MinMax m3 { 3 , 12 }; MinMax mFinal { m1 + m2 + 5 + 8 + m3 + 16 }; std :: cout << \"Result: (\" << mFinal . getMin () << \", \" << mFinal . getMax () << \") \\n \" ; return 0 ; } COPY The MinMax class keeps track of the minimum and maximum values that it has seen so far. We have overloaded the + operator 3 times, so that we can add two MinMax objects together, or add integers to MinMax objects. This example produces the result: Result: (3, 16) which you will note is the minimum and maximum values that we added to mFinal. Let\u2019s talk a little bit more about how \u201cMinMax mFinal { m1 + m2 + 5 + 8 + m3 + 16 }\u201d evaluates. Remember that operator+ evaluates from left to right, so m1 + m2 evaluates first. This becomes a call to operator+(m1, m2), which produces the return value MinMax(8, 15). Then MinMax(8, 15) + 5 evaluates next. This becomes a call to operator+(MinMax(8, 15), 5), which produces return value MinMax(5, 15). Then MinMax(5, 15) + 8 evaluates in the same way to produce MinMax(5, 15). Then MinMax(5, 15) + m3 evaluates to produce MinMax(3, 15). And finally, MinMax(3, 15) + 16 evaluates to MinMax(3, 16). This final result is then used to initialize mFinal. In other words, this expression evaluates as \u201cMinMax mFinal = (((((m1 + m2) + 5) + 8) + m3) + 16)\u201d, with each successive operation returning a MinMax object that becomes the left-hand operand for the following operator. Implementing operators using other operators In the above example, note that we defined operator+(int, MinMax) by calling operator+(MinMax, int) (which produces the same result). This allows us to reduce the implementation of operator+(int, MinMax) to a single line, making our code easier to maintain by minimizing redundancy and making the function simpler to understand. It is often possible to define overloaded operators by calling other overloaded operators. You should do so if and when doing so produces simpler code. In cases where the implementation is trivial (e.g. a single line) it may or may not be worth doing this.","title":"14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26"},{"location":"14-3-overloading-operators-using-normal-functions/","tags":["overload","operator"],"text":"Key Takeaway In the previous lesson, we overloaded operator+ as a friend function: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} // add Cents + Cents using a friend function friend Cents operator + ( const Cents & c1 , const Cents & c2 ); int getCents () const { return m_cents ; } }; // note: this function is not a member function! Cents operator + ( const Cents & c1 , const Cents & c2 ) { // use the Cents constructor and operator+(int, int) // we can access m_cents directly because this is a friend function return { c1 . m_cents + c2 . m_cents }; } int main () { Cents cents1 { 6 }; Cents cents2 { 8 }; Cents centsSum { cents1 + cents2 }; std :: cout << \"I have \" << centsSum . getCents () << \" cents. \\n \" ; return 0 ; } COPY Using a friend function to overload an operator is convenient because it gives you direct access to the internal members of the classes you\u2019re operating on. In the initial Cents example above, our friend function version of operator+ accessed member variable m_cents directly. However, if you don\u2019t need that access, you can write your overloaded operators as normal functions. Note that the Cents class above contains an access function (getCents()) that allows us to get at m_cents without having to have direct access to private members. Because of this, we can write our overloaded operator+ as a non-friend: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; // note: this function is not a member function nor a friend function! Cents operator + ( const Cents & c1 , const Cents & c2 ) { // use the Cents constructor and operator+(int, int) // we don't need direct access to private members here return Cents { c1 . getCents () + c2 . getCents () }; } int main () { Cents cents1 { 6 }; Cents cents2 { 8 }; Cents centsSum { cents1 + cents2 }; std :: cout << \"I have \" << centsSum . getCents () << \" cents. \\n \" ; return 0 ; } COPY Because the normal and friend functions work almost identically (they just have different levels of access to private members), we generally won\u2019t differentiate them. The one difference is that the friend function declaration inside the class serves as a prototype as well. With the normal function version, you\u2019ll have to provide your own function prototype. Cents.h: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #ifndef CENTS_H #define CENTS_H class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} int getCents () const { return m_cents ; } }; // Need to explicitly provide prototype for operator+ so uses of operator+ in other files know this overload exists Cents operator + ( const Cents & c1 , const Cents & c2 ); #endif COPY Cents.cpp: 1 2 3 4 5 6 7 8 9 #include \"Cents.h\" // note: this function is not a member function nor a friend function! Cents operator + ( const Cents & c1 , const Cents & c2 ) { // use the Cents constructor and operator+(int, int) // we don't need direct access to private members here return { c1 . getCents () + c2 . getCents () }; } COPY main.cpp: 1 2 3 4 5 6 7 8 9 10 11 12 #include \"Cents.h\" #include int main () { Cents cents1 { 6 }; Cents cents2 { 8 }; Cents centsSum { cents1 + cents2 }; // without the prototype in Cents.h, this would fail to compile std :: cout << \"I have \" << centsSum . getCents () << \" cents. \\n \" ; return 0 ; } COPY In general, a normal function should be preferred over a friend function if it\u2019s possible to do so with the existing member functions available (the less functions touching your classes\u2019s internals, the better). However, don\u2019t add additional access functions just to overload an operator as a normal function instead of a friend function! Best practice Prefer overloading operators as normal functions instead of friends if it\u2019s possible to do so without adding additional functions.","title":"14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26"},{"location":"14-4-overloading-the-IO-operators/","tags":["overload","operator"],"text":"Key Takeaway For classes that have multiple member variables, printing each of the individual variables on the screen can get tiresome fast. For example, consider the following class: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Point { private : double m_x {}; double m_y {}; double m_z {}; public : Point ( double x = 0.0 , double y = 0.0 , double z = 0.0 ) : m_x { x }, m_y { y }, m_z { z } { } double getX () const { return m_x ; } double getY () const { return m_y ; } double getZ () const { return m_z ; } }; COPY If you wanted to print an instance of this class to the screen, you\u2019d have to do something like this: 1 2 3 4 5 Point point { 5.0 , 6.0 , 7.0 }; std :: cout << \"Point(\" << point . getX () << \", \" << point . getY () << \", \" << point . getZ () << ')' ; COPY Of course, it makes more sense to do this as a reusable function. And in previous examples, you\u2019ve seen us create print() functions that work like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 class Point { private : double m_x {}; double m_y {}; double m_z {}; public : Point ( double x = 0.0 , double y = 0.0 , double z = 0.0 ) : m_x { x }, m_y { y }, m_z { z } { } double getX () const { return m_x ; } double getY () const { return m_y ; } double getZ () const { return m_z ; } void print () const { std :: cout << \"Point(\" << m_x << \", \" << m_y << \", \" << m_z << ')' ; } }; COPY While this is much better, it still has some downsides. Because print() returns void, it can\u2019t be called in the middle of an output statement. Instead, you have to do this: 1 2 3 4 5 6 7 8 int main () { const Point point { 5.0 , 6.0 , 7.0 }; std :: cout << \"My point is: \" ; point . print (); std :: cout << \" in Cartesian space. \\n \" ; } COPY It would be much easier if you could simply type: 1 2 Point point { 5.0 , 6.0 , 7.0 }; cout << \"My point is: \" << point << \" in Cartesian space. \\n \" ; COPY and get the same result. There would be no breaking up output across multiple statements, and no having to remember what you named the print function. Fortunately, by overloading the << operator, you can! Overloading operator<< Overloading operator<< is similar to overloading operator+ (they are both binary operators), except that the parameter types are different. Consider the expression std::cout << point . If the operator is <<, what are the operands? The left operand is the std::cout object, and the right operand is your Point class object. std::cout is actually an object of type std::ostream. Therefore, our overloaded function will look like this: 1 2 // std::ostream is the type for object std::cout friend std :: ostream & operator << ( std :: ostream & out , const Point & point ); COPY Implementation of operator<< for our Point class is fairly straightforward -- because C++ already knows how to output doubles using operator<<, and our members are all doubles, we can simply use operator<< to output the member variables of our Point. Here is the above Point class with the overloaded operator<<. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include class Point { private : double m_x {}; double m_y {}; double m_z {}; public : Point ( double x = 0.0 , double y = 0.0 , double z = 0.0 ) : m_x { x }, m_y { y }, m_z { z } { } friend std :: ostream & operator << ( std :: ostream & out , const Point & point ); }; std :: ostream & operator << ( std :: ostream & out , const Point & point ) { // Since operator<< is a friend of the Point class, we can access Point's members directly. out << \"Point(\" << point . m_x << \", \" << point . m_y << \", \" << point . m_z << ')' ; // actual output done here return out ; // return std::ostream so we can chain calls to operator<< } int main () { const Point point1 { 2.0 , 3.0 , 4.0 }; std :: cout << point1 << '\\n' ; return 0 ; } COPY This is pretty straightforward -- note how similar our output line is to the line in the print() function we wrote previously. The most notable difference is that std::cout has become parameter out (which will be a reference to std::cout when the function is called). The trickiest part here is the return type. With the arithmetic operators, we calculated and returned a single answer by value (because we were creating and returning a new result). However, if you try to return std::ostream by value, you\u2019ll get a compiler error. This happens because std::ostream specifically disallows being copied. In this case, we return the left hand parameter as a reference. This not only prevents a copy of std::ostream from being made, it also allows us to \u201cchain\u201d output commands together, such as std::cout << point << std::endl; You might have initially thought that since operator<< doesn\u2019t return a value to the caller, we should define the function as returning void. But consider what would happen if our operator<< returned void. When the compiler evaluates std::cout << point << '\\n' , due to the precedence/associativity rules, it evaluates this expression as (std::cout << point) << '\\n'; . std::cout << point would call our void-returning overloaded operator<< function, which returns void. Then the partially evaluated expression becomes: void << '\\n'; , which makes no sense! By returning the out parameter as the return type instead, (std::cout<< point) returns std::cout. Then our partially evaluated expression becomes: std::cout << '\\n'; , which then gets evaluated itself! Any time we want our overloaded binary operators to be chainable in such a manner, the left operand should be returned (by reference). Returning the left-hand parameter by reference is okay in this case -- since the left-hand parameter was passed in by the calling function, it must still exist when the called function returns. Therefore, we don\u2019t have to worry about referencing something that will go out of scope and get destroyed when the operator returns. Just to prove it works, consider the following example, which uses the Point class with the overloaded operator<< we wrote above: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include class Point { private : double m_x {}; double m_y {}; double m_z {}; public : Point ( double x = 0.0 , double y = 0.0 , double z = 0.0 ) : m_x { x }, m_y { y }, m_z { z } { } friend std :: ostream & operator << ( std :: ostream & out , const Point & point ); }; std :: ostream & operator << ( std :: ostream & out , const Point & point ) { // Since operator<< is a friend of the Point class, we can access Point's members directly. out << \"Point(\" << point . m_x << \", \" << point . m_y << \", \" << point . m_z << ')' ; return out ; } int main () { Point point1 { 2.0 , 3.5 , 4.0 }; Point point2 { 6.0 , 7.5 , 8.0 }; std :: cout << point1 << ' ' << point2 << '\\n' ; return 0 ; } COPY This produces the following result: Point(2, 3.5, 4) Point(6, 7.5, 8) Overloading operator>> It is also possible to overload the input operator. This is done in a manner analogous to overloading the output operator. The key thing you need to know is that std::cin is an object of type std::istream. Here\u2019s our Point class with an overloaded operator>>: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include class Point { private : double m_x {}; double m_y {}; double m_z {}; public : Point ( double x = 0.0 , double y = 0.0 , double z = 0.0 ) : m_x { x }, m_y { y }, m_z { z } { } friend std :: ostream & operator << ( std :: ostream & out , const Point & point ); friend std :: istream & operator >> ( std :: istream & in , Point & point ); }; std :: ostream & operator << ( std :: ostream & out , const Point & point ) { // Since operator<< is a friend of the Point class, we can access Point's members directly. out << \"Point(\" << point . m_x << \", \" << point . m_y << \", \" << point . m_z << ')' ; return out ; } std :: istream & operator >> ( std :: istream & in , Point & point ) { // Since operator>> is a friend of the Point class, we can access Point's members directly. // note that parameter point must be non-const so we can modify the class members with the input values in >> point . m_x ; in >> point . m_y ; in >> point . m_z ; return in ; } COPY Here\u2019s a sample program using both the overloaded operator<< and operator>>: 1 2 3 4 5 6 7 8 9 10 11 int main () { std :: cout << \"Enter a point: \" ; Point point ; std :: cin >> point ; std :: cout << \"You entered: \" << point << '\\n' ; return 0 ; } COPY Assuming the user enters 3.0 4.5 7.26 as input, the program produces the following result: You entered: Point(3, 4.5, 7.26) Conclusion Overloading operator<< and operator>> make it extremely easy to output your class to screen and accept user input from the console.","title":"14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26"},{"location":"14-5-overloading-operators-using-member-functions/","tags":["overload","operator"],"text":"Key Takeaway In lesson 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 , you learned how to overload the arithmetic operators using friend functions. You also learned you can overload operators as normal functions. Many operators can be overloaded in a different way: as a member function. Overloading operators using a member function is very similar to overloading operators using a friend function. When overloading an operator using a member function: The overloaded operator must be added as a member function of the left operand. The left operand becomes the implicit *this object All other operands become function parameters. As a reminder, here\u2019s how we overloaded operator+ using a friend function: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } // Overload Cents + int friend Cents operator + ( const Cents & cents , int value ); int getCents () const { return m_cents ; } }; // note: this function is not a member function! Cents operator + ( const Cents & cents , int value ) { return Cents ( cents . m_cents + value ); } int main () { Cents cents1 { 6 }; Cents cents2 { cents1 + 2 }; std :: cout << \"I have \" << cents2 . getCents () << \" cents. \\n \" ; return 0 ; } COPY Converting a friend overloaded operator to a member overloaded operator is easy: The overloaded operator is defined as a member instead of a friend (Cents::operator+ instead of friend operator+) The left parameter is removed, because that parameter now becomes the implicit *this object. Inside the function body, all references to the left parameter can be removed (e.g. cents.m_cents becomes m_cents, which implicitly references the *this object). Now, the same operator overloaded using the member function method: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } { } // Overload Cents + int Cents operator + ( int value ); int getCents () const { return m_cents ; } }; // note: this function is a member function! // the cents parameter in the friend version is now the implicit *this parameter Cents Cents :: operator + ( int value ) { return Cents { m_cents + value }; } int main () { Cents cents1 { 6 }; Cents cents2 { cents1 + 2 }; std :: cout << \"I have \" << cents2 . getCents () << \" cents. \\n \" ; return 0 ; } COPY Note that the usage of the operator does not change (in both cases, cents1 + 2 ), we\u2019ve simply defined the function differently. Our two-parameter friend function becomes a one-parameter member function, with the leftmost parameter in the friend version (cents) becoming the implicit *this parameter in the member function version. Let\u2019s take a closer look at how the expression cents1 + 2 evaluates. In the friend function version, the expression cents1 + 2 becomes function call operator+(cents1, 2). Note that there are two function parameters. This is straightforward. In the member function version, the expression cents1 + 2 becomes function call cents1.operator+(2) . Note that there is now only one explicit function parameter, and cents1 has become an object prefix. However, in lesson 13.10 -- The hidden \u201cthis\u201d pointer , we mentioned that the compiler implicitly converts an object prefix into a hidden leftmost parameter named *this. So in actuality, cents1.operator+(2) becomes operator+(\u00a2s1, 2) , which is almost identical to the friend version. Both cases produce the same result, just in slightly different ways. So if we can overload an operator as a friend or a member, which should we use? In order to answer that question, there\u2019s a few more things you\u2019ll need to know. Not everything can be overloaded as a friend function The assignment (=), subscript ([]), function call (()), and member selection (->) operators must be overloaded as member functions, because the language requires them to be. Not everything can be overloaded as a member function In lesson 14.4 -- Overloading the I/O operators , we overloaded operator<< for our Point class using the friend function method. Here\u2019s a reminder of how we did that: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include class Point { private : double m_x {}, m_y {}, m_z {}; public : Point ( double x = 0.0 , double y = 0.0 , double z = 0.0 ) : m_x { x }, m_y { y }, m_z { z } { } friend std :: ostream & operator << ( std :: ostream & out , const Point & point ); }; std :: ostream & operator << ( std :: ostream & out , const Point & point ) { // Since operator<< is a friend of the Point class, we can access Point's members directly. out << \"Point(\" << point . m_x << \", \" << point . m_y << \", \" << point . m_z << \")\" ; return out ; } int main () { Point point1 { 2.0 , 3.0 , 4.0 }; std :: cout << point1 ; return 0 ; } COPY However, we are not able to overload operator<< as a member function. Why not? Because the overloaded operator must be added as a member of the left operand. In this case, the left operand is an object of type std::ostream. std::ostream is fixed as part of the standard library. We can\u2019t modify the class declaration to add the overload as a member function of std::ostream. This necessitates that operator<< be overloaded as a normal function (preferred) or a friend. Similarly, although we can overload operator+(Cents, int) as a member function (as we did above), we can\u2019t overload operator+(int, Cents) as a member function, because int isn\u2019t a class we can add members to. Typically, we won\u2019t be able to use a member overload if the left operand is either not a class (e.g. int), or it is a class that we can\u2019t modify (e.g. std::ostream). When to use a normal, friend, or member function overload In most cases, the language leaves it up to you to determine whether you want to use the normal/friend or member function version of the overload. However, one of the two is usually a better choice than the other. When dealing with binary operators that don\u2019t modify the left operand (e.g. operator+), the normal or friend function version is typically preferred, because it works for all parameter types (even when the left operand isn\u2019t a class object, or is a class that is not modifiable). The normal or friend function version has the added benefit of \u201csymmetry\u201d, as all operands become explicit parameters (instead of the left operand becoming *this and the right operand becoming an explicit parameter). When dealing with binary operators that do modify the left operand (e.g. operator+=), the member function version is typically preferred. In these cases, the leftmost operand will always be a class type, and having the object being modified become the one pointed to by *this is natural. Because the rightmost operand becomes an explicit parameter, there\u2019s no confusion over who is getting modified and who is getting evaluated. Unary operators are usually overloaded as member functions as well, since the member version has no parameters. The following rules of thumb can help you determine which form is best for a given situation: If you\u2019re overloading assignment (=), subscript ([]), function call (()), or member selection (->), do so as a member function. If you\u2019re overloading a unary operator, do so as a member function. If you\u2019re overloading a binary operator that does not modify its left operand (e.g. operator+), do so as a normal function (preferred) or friend function. If you\u2019re overloading a binary operator that modifies its left operand, but you can\u2019t add members to the class definition of the left operand (e.g. operator<<, which has a left operand of type ostream), do so as a normal function (preferred) or friend function. If you\u2019re overloading a binary operator that modifies its left operand (e.g. operator+=), and you can modify the definition of the left operand, do so as a member function. [ ](https://www.learncpp.com/cpp-tutorial/overloading-unary-operators/)","title":"14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26"},{"location":"14-6-overloading-unary-operators-%2B--and-%21/","tags":["overload","unary","operator"],"text":"Key Takeaway Overloading unary operators Unlike the operators you\u2019ve seen so far, the positive (+), negative (-) and logical not (!) operators all are unary operators, which means they only operate on one operand. Because they only operate on the object they are applied to, typically unary operator overloads are implemented as member functions. All three operands are implemented in an identical manner. Let\u2019s take a look at how we\u2019d implement operator- on the Cents class we used in a previous example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include class Cents { private : int m_cents {}; public : Cents ( int cents ) : m_cents { cents } {} // Overload -Cents as a member function Cents operator - () const ; int getCents () const { return m_cents ; } }; // note: this function is a member function! Cents Cents :: operator - () const { return - m_cents ; // since return type is a Cents, this does an implicit conversion from int to Cents using the Cents(int) constructor } int main () { const Cents nickle { 5 }; std :: cout << \"A nickle of debt is worth \" << ( - nickle ). getCents () << \" cents \\n \" ; return 0 ; } COPY This should be straightforward. Our overloaded negative operator (-) is a unary operator implemented as a member function, so it takes no parameters (it operates on the *this object). It returns a Cents object that is the negation of the original Cents value. Because operator- does not modify the Cents object, we can (and should) make it a const function (so it can be called on const Cents objects). Note that there\u2019s no confusion between the negative operator- and the minus operator- since they have a different number of parameters. Here\u2019s another example. The ! operator is the logical negation operator -- if an expression evaluates to \u201ctrue\u201d, operator! will return false, and vice-versa. We commonly see this applied to boolean variables to test whether they are true or not: 1 2 3 4 if ( ! isHappy ) std :: cout << \"I am not happy! \\n \" ; else std :: cout << \"I am so happy! \\n \" ; COPY For integers, 0 evaluates to false, and anything else to true, so operator! as applied to integers will return true for an integer value of 0 and false otherwise. Extending the concept, we can say that operator! should evaluate to true if the state of the object is \u201cfalse\u201d, \u201czero\u201d, or whatever the default initialization state is. The following example shows an overload of both operator- and operator! for a user-defined Point class: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include class Point { private : double m_x {}; double m_y {}; double m_z {}; public : Point ( double x = 0.0 , double y = 0.0 , double z = 0.0 ) : m_x { x }, m_y { y }, m_z { z } { } // Convert a Point into its negative equivalent Point operator - () const ; // Return true if the point is set at the origin bool operator ! () const ; double getX () const { return m_x ; } double getY () const { return m_y ; } double getZ () const { return m_z ; } }; // Convert a Point into its negative equivalent Point Point :: operator - () const { return { - m_x , - m_y , - m_z }; } // Return true if the point is set at the origin, false otherwise bool Point :: operator ! () const { return ( m_x == 0.0 && m_y == 0.0 && m_z == 0.0 ); } int main () { Point point {}; // use default constructor to set to (0.0, 0.0, 0.0) if ( ! point ) std :: cout << \"point is set at the origin. \\n \" ; else std :: cout << \"point is not set at the origin. \\n \" ; return 0 ; } COPY The overloaded operator! for this class returns the Boolean value \u201ctrue\u201d if the Point is set to the default value at coordinate (0.0, 0.0, 0.0). Thus, the above code produces the result: point is set at the origin.","title":"14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c !"},{"location":"14-7-overloading-the-comparison-operators/","tags":["overload","unary","operator"],"text":"Key Takeaway In lesson 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 we discussed the six comparison operators. Overloading these comparison operators is comparatively simple (see what I did there?), as they follow the same patterns as we\u2019ve seen in overloading other operators. Because the comparison operators are all binary operators that do not modify their left operands, we will make our overloaded comparison operators friend functions. Here\u2019s an example Car class with an overloaded operator== and operator!=. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #include #include #include class Car { private : std :: string m_make ; std :: string m_model ; public : Car ( std :: string_view make , std :: string_view model ) : m_make { make }, m_model { model } { } friend bool operator == ( const Car & c1 , const Car & c2 ); friend bool operator != ( const Car & c1 , const Car & c2 ); }; bool operator == ( const Car & c1 , const Car & c2 ) { return ( c1 . m_make == c2 . m_make && c1 . m_model == c2 . m_model ); } bool operator != ( const Car & c1 , const Car & c2 ) { return ( c1 . m_make != c2 . m_make || c1 . m_model != c2 . m_model ); } int main () { Car corolla { \"Toyota\" , \"Corolla\" }; Car camry { \"Toyota\" , \"Camry\" }; if ( corolla == camry ) std :: cout << \"a Corolla and Camry are the same. \\n \" ; if ( corolla != camry ) std :: cout << \"a Corolla and Camry are not the same. \\n \" ; return 0 ; } COPY The code here should be straightforward. What about operator< and operator>? What would it mean for a Car to be greater or less than another Car? We typically don\u2019t think about cars this way. Since the results of operator< and operator> would not be immediately intuitive, it may be better to leave these operators undefined. \u6700\u4f73\u5b9e\u8df5 Only define overloaded operators that make intuitive sense for your class. However, there is one common exception to the above recommendation. What if we wanted to sort a list of Cars? In such a case, we might want to overload the comparison operators to return the member (or members) you\u2019re most likely to want to sort on. For example, an overloaded operator< for Cars might sort based on make and model alphabetically. Some of the container classes in the standard library (classes that hold sets of other classes) require an overloaded operator< so they can keep the elements sorted. Here\u2019s a different example overloading all 6 logical comparison operators: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 #include class Cents { private : int m_cents ; public : Cents ( int cents ) : m_cents { cents } {} friend bool operator == ( const Cents & c1 , const Cents & c2 ); friend bool operator != ( const Cents & c1 , const Cents & c2 ); friend bool operator < ( const Cents & c1 , const Cents & c2 ); friend bool operator > ( const Cents & c1 , const Cents & c2 ); friend bool operator <= ( const Cents & c1 , const Cents & c2 ); friend bool operator >= ( const Cents & c1 , const Cents & c2 ); }; bool operator == ( const Cents & c1 , const Cents & c2 ) { return c1 . m_cents == c2 . m_cents ; } bool operator != ( const Cents & c1 , const Cents & c2 ) { return c1 . m_cents != c2 . m_cents ; } bool operator < ( const Cents & c1 , const Cents & c2 ) { return c1 . m_cents < c2 . m_cents ; } bool operator > ( const Cents & c1 , const Cents & c2 ) { return c1 . m_cents > c2 . m_cents ; } bool operator <= ( const Cents & c1 , const Cents & c2 ) { return c1 . m_cents <= c2 . m_cents ; } bool operator >= ( const Cents & c1 , const Cents & c2 ) { return c1 . m_cents >= c2 . m_cents ; } int main () { Cents dime { 10 }; Cents nickel { 5 }; if ( nickel > dime ) std :: cout << \"a nickel is greater than a dime. \\n \" ; if ( nickel >= dime ) std :: cout << \"a nickel is greater than or equal to a dime. \\n \" ; if ( nickel < dime ) std :: cout << \"a dime is greater than a nickel. \\n \" ; if ( nickel <= dime ) std :: cout << \"a dime is greater than or equal to a nickel. \\n \" ; if ( nickel == dime ) std :: cout << \"a dime is equal to a nickel. \\n \" ; if ( nickel != dime ) std :: cout << \"a dime is not equal to a nickel. \\n \" ; return 0 ; } COPY This is also pretty straightforward. Minimizing comparative redundancy In the example above, note how similar the implementation of each of the overloaded comparison operators are. Overloaded comparison operators tend to have a high degree of redundancy, and the more complex the implementation, the more redundancy there will be. Fortunately, many of the comparison operators can be implemented using the other comparison operators: operator!= can be implemented as !(operator==) operator> can be implemented as operator< with the order of the parameters flipped operator>= can be implemented as !(operator<) operator<= can be implemented as !(operator>) This means that we only need to implement logic for operator== and operator<, and then the other four comparison operators can be defined in terms of those two! Here\u2019s an updated Cents example illustrating this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 #include class Cents { private : int m_cents ; public : Cents ( int cents ) : m_cents { cents } {} friend bool operator == ( const Cents & c1 , const Cents & c2 ); friend bool operator != ( const Cents & c1 , const Cents & c2 ); friend bool operator < ( const Cents & c1 , const Cents & c2 ); friend bool operator > ( const Cents & c1 , const Cents & c2 ); friend bool operator <= ( const Cents & c1 , const Cents & c2 ); friend bool operator >= ( const Cents & c1 , const Cents & c2 ); }; bool operator == ( const Cents & c1 , const Cents & c2 ) { return c1 . m_cents == c2 . m_cents ; } bool operator != ( const Cents & c1 , const Cents & c2 ) { return ! ( operator == ( c1 , c2 )); } bool operator < ( const Cents & c1 , const Cents & c2 ) { return c1 . m_cents < c2 . m_cents ; } bool operator > ( const Cents & c1 , const Cents & c2 ) { return operator < ( c2 , c1 ); } bool operator <= ( const Cents & c1 , const Cents & c2 ) { return ! ( operator > ( c1 , c2 )); } bool operator >= ( const Cents & c1 , const Cents & c2 ) { return ! ( operator < ( c1 , c2 )); } int main () { Cents dime { 10 }; Cents nickel { 5 }; if ( nickel > dime ) std :: cout << \"a nickel is greater than a dime. \\n \" ; if ( nickel >= dime ) std :: cout << \"a nickel is greater than or equal to a dime. \\n \" ; if ( nickel < dime ) std :: cout << \"a dime is greater than a nickel. \\n \" ; if ( nickel <= dime ) std :: cout << \"a dime is greater than or equal to a nickel. \\n \" ; if ( nickel == dime ) std :: cout << \"a dime is equal to a nickel. \\n \" ; if ( nickel != dime ) std :: cout << \"a dime is not equal to a nickel. \\n \" ; return 0 ; } COPY This way, if we ever need to change something, we only need to update operator== and operator< instead of all six comparison operators!","title":"14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26"},{"location":"14-7-overloading-the-comparison-operators/#minimizing-comparative-redundancy","text":"In the example above, note how similar the implementation of each of the overloaded comparison operators are. Overloaded comparison operators tend to have a high degree of redundancy, and the more complex the implementation, the more redundancy there will be. Fortunately, many of the comparison operators can be implemented using the other comparison operators: operator!= can be implemented as !(operator==) operator> can be implemented as operator< with the order of the parameters flipped operator>= can be implemented as !(operator<) operator<= can be implemented as !(operator>) This means that we only need to implement logic for operator== and operator<, and then the other four comparison operators can be defined in terms of those two! Here\u2019s an updated Cents example illustrating this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 #include class Cents { private : int m_cents ; public : Cents ( int cents ) : m_cents { cents } {} friend bool operator == ( const Cents & c1 , const Cents & c2 ); friend bool operator != ( const Cents & c1 , const Cents & c2 ); friend bool operator < ( const Cents & c1 , const Cents & c2 ); friend bool operator > ( const Cents & c1 , const Cents & c2 ); friend bool operator <= ( const Cents & c1 , const Cents & c2 ); friend bool operator >= ( const Cents & c1 , const Cents & c2 ); }; bool operator == ( const Cents & c1 , const Cents & c2 ) { return c1 . m_cents == c2 . m_cents ; } bool operator != ( const Cents & c1 , const Cents & c2 ) { return ! ( operator == ( c1 , c2 )); } bool operator < ( const Cents & c1 , const Cents & c2 ) { return c1 . m_cents < c2 . m_cents ; } bool operator > ( const Cents & c1 , const Cents & c2 ) { return operator < ( c2 , c1 ); } bool operator <= ( const Cents & c1 , const Cents & c2 ) { return ! ( operator > ( c1 , c2 )); } bool operator >= ( const Cents & c1 , const Cents & c2 ) { return ! ( operator < ( c1 , c2 )); } int main () { Cents dime { 10 }; Cents nickel { 5 }; if ( nickel > dime ) std :: cout << \"a nickel is greater than a dime. \\n \" ; if ( nickel >= dime ) std :: cout << \"a nickel is greater than or equal to a dime. \\n \" ; if ( nickel < dime ) std :: cout << \"a dime is greater than a nickel. \\n \" ; if ( nickel <= dime ) std :: cout << \"a dime is greater than or equal to a nickel. \\n \" ; if ( nickel == dime ) std :: cout << \"a dime is equal to a nickel. \\n \" ; if ( nickel != dime ) std :: cout << \"a dime is not equal to a nickel. \\n \" ; return 0 ; } COPY This way, if we ever need to change something, we only need to update operator== and operator< instead of all six comparison operators!","title":"Minimizing comparative redundancy"},{"location":"14-8-overloading-the-increment-and-decrement-operators/","tags":["overload","operator"],"text":"Key Takeaway Overloading the increment ( ++ ) and decrement ( -- ) operators is pretty straightforward, with one small exception. There are actually two versions of the increment and decrement operators: a prefix increment and decrement (e.g. ++x; --y; ) and a postfix increment and decrement (e.g. x++; y--; ). Because the increment and decrement operators are both unary operators and they modify their operands, they\u2019re best overloaded as member functions. We\u2019ll tackle the prefix versions first because they\u2019re the most straightforward. Overloading prefix increment and decrement** Prefix increment and decrement are overloaded exactly the same as any normal unary operator. We\u2019ll do this one by example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 #include class Digit { private : int m_digit ; public : Digit ( int digit = 0 ) : m_digit { digit } { } Digit & operator ++ (); Digit & operator -- (); friend std :: ostream & operator << ( std :: ostream & out , const Digit & d ); }; Digit & Digit :: operator ++ () { // If our number is already at 9, wrap around to 0 if ( m_digit == 9 ) m_digit = 0 ; // otherwise just increment to next number else ++ m_digit ; return * this ; } Digit & Digit :: operator -- () { // If our number is already at 0, wrap around to 9 if ( m_digit == 0 ) m_digit = 9 ; // otherwise just decrement to next number else -- m_digit ; return * this ; } std :: ostream & operator << ( std :: ostream & out , const Digit & d ) { out << d . m_digit ; return out ; } int main () { Digit digit ( 8 ); std :: cout << digit ; std :: cout << ++ digit ; std :: cout << ++ digit ; std :: cout << -- digit ; std :: cout << -- digit ; return 0 ; } COPY Our Digit class holds a number between 0 and 9. We\u2019ve overloaded increment and decrement so they increment/decrement the digit, wrapping around if the digit is incremented/decremented out range. This example prints: 1 89098 Note that we return *this. The overloaded increment and decrement operators return the current implicit object so multiple operators can be \u201cchained\u201d together. Overloading postfix increment and decrement** Normally, functions can be overloaded when they have the same name but a different number and/or different type of parameters. However, consider the case of the prefix and postfix increment and decrement operators. Both have the same name (eg. operator++), are unary, and take one parameter of the same type. So how it is possible to differentiate the two when overloading? The C++ language specification has a special case that provides the answer: the compiler looks to see if the overloaded operator has an int parameter. If the overloaded operator has an int parameter, the operator is a postfix overload. If the overloaded operator has no parameter, the operator is a prefix overload. Here is the above Digit class with both prefix and postfix overloads: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 class Digit { private : int m_digit ; public : Digit ( int digit = 0 ) : m_digit { digit } { } Digit & operator ++ (); // prefix has no parameter Digit & operator -- (); // prefix has no parameter Digit operator ++ ( int ); // postfix has an int parameter Digit operator -- ( int ); // postfix has an int parameter friend std :: ostream & operator << ( std :: ostream & out , const Digit & d ); }; // No parameter means this is prefix operator++ Digit & Digit :: operator ++ () { // If our number is already at 9, wrap around to 0 if ( m_digit == 9 ) m_digit = 0 ; // otherwise just increment to next number else ++ m_digit ; return * this ; } // No parameter means this is prefix operator-- Digit & Digit :: operator -- () { // If our number is already at 0, wrap around to 9 if ( m_digit == 0 ) m_digit = 9 ; // otherwise just decrement to next number else -- m_digit ; return * this ; } // int parameter means this is postfix operator++ Digit Digit :: operator ++ ( int ) { // Create a temporary variable with our current digit Digit temp { * this }; // Use prefix operator to increment this digit ++ ( * this ); // apply operator // return temporary result return temp ; // return saved state } // int parameter means this is postfix operator-- Digit Digit :: operator -- ( int ) { // Create a temporary variable with our current digit Digit temp { * this }; // Use prefix operator to decrement this digit -- ( * this ); // apply operator // return temporary result return temp ; // return saved state } std :: ostream & operator << ( std :: ostream & out , const Digit & d ) { out << d . m_digit ; return out ; } int main () { Digit digit ( 5 ); std :: cout << digit ; std :: cout << ++ digit ; // calls Digit::operator++(); std :: cout << digit ++ ; // calls Digit::operator++(int); std :: cout << digit ; std :: cout << -- digit ; // calls Digit::operator--(); std :: cout << digit -- ; // calls Digit::operator--(int); std :: cout << digit ; return 0 ; } COPY This prints 1 5667665 There are a few interesting things going on here. First, note that we\u2019ve distinguished the prefix from the postfix operators by providing an integer dummy parameter on the postfix version. Second, because the dummy parameter is not used in the function implementation, we have not even given it a name. This tells the compiler to treat this variable as a placeholder, which means it won\u2019t warn us that we declared a variable but never used it. Third, note that the prefix and postfix operators do the same job -- they both increment or decrement the object. The difference between the two is in the value they return. The overloaded prefix operators return the object after it has been incremented or decremented. Consequently, overloading these is fairly straightforward. We simply increment or decrement our member variables, and then return *this. The postfix operators, on the other hand, need to return the state of the object before it is incremented or decremented. This leads to a bit of a conundrum -- if we increment or decrement the object, we won\u2019t be able to return the state of the object before it was incremented or decremented. On the other hand, if we return the state of the object before we increment or decrement it, the increment or decrement will never be called. The typical way this problem is solved is to use a temporary variable that holds the value of the object before it is incremented or decremented. Then the object itself can be incremented or decremented. And finally, the temporary variable is returned to the caller. In this way, the caller receives a copy of the object before it was incremented or decremented, but the object itself is incremented or decremented. Note that this means the return value of the overloaded operator must be a non-reference, because we can\u2019t return a reference to a local variable that will be destroyed when the function exits. Also note that this means the postfix operators are typically less efficient than the prefix operators because of the added overhead of instantiating a temporary variable and returning by value instead of reference. Finally, note that we\u2019ve written the post-increment and post-decrement in such a way that it calls the pre-increment and pre-decrement to do most of the work. This cuts down on duplicate code, and makes our class easier to modify in the future.","title":"14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26"},{"location":"14-8-overloading-the-increment-and-decrement-operators/#overloading-prefix-increment-and-decrement","text":"Prefix increment and decrement are overloaded exactly the same as any normal unary operator. We\u2019ll do this one by example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 #include class Digit { private : int m_digit ; public : Digit ( int digit = 0 ) : m_digit { digit } { } Digit & operator ++ (); Digit & operator -- (); friend std :: ostream & operator << ( std :: ostream & out , const Digit & d ); }; Digit & Digit :: operator ++ () { // If our number is already at 9, wrap around to 0 if ( m_digit == 9 ) m_digit = 0 ; // otherwise just increment to next number else ++ m_digit ; return * this ; } Digit & Digit :: operator -- () { // If our number is already at 0, wrap around to 9 if ( m_digit == 0 ) m_digit = 9 ; // otherwise just decrement to next number else -- m_digit ; return * this ; } std :: ostream & operator << ( std :: ostream & out , const Digit & d ) { out << d . m_digit ; return out ; } int main () { Digit digit ( 8 ); std :: cout << digit ; std :: cout << ++ digit ; std :: cout << ++ digit ; std :: cout << -- digit ; std :: cout << -- digit ; return 0 ; } COPY Our Digit class holds a number between 0 and 9. We\u2019ve overloaded increment and decrement so they increment/decrement the digit, wrapping around if the digit is incremented/decremented out range. This example prints: 1 89098 Note that we return *this. The overloaded increment and decrement operators return the current implicit object so multiple operators can be \u201cchained\u201d together.","title":"Overloading prefix increment and decrement**"},{"location":"14-8-overloading-the-increment-and-decrement-operators/#overloading-postfix-increment-and-decrement","text":"Normally, functions can be overloaded when they have the same name but a different number and/or different type of parameters. However, consider the case of the prefix and postfix increment and decrement operators. Both have the same name (eg. operator++), are unary, and take one parameter of the same type. So how it is possible to differentiate the two when overloading? The C++ language specification has a special case that provides the answer: the compiler looks to see if the overloaded operator has an int parameter. If the overloaded operator has an int parameter, the operator is a postfix overload. If the overloaded operator has no parameter, the operator is a prefix overload. Here is the above Digit class with both prefix and postfix overloads: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 class Digit { private : int m_digit ; public : Digit ( int digit = 0 ) : m_digit { digit } { } Digit & operator ++ (); // prefix has no parameter Digit & operator -- (); // prefix has no parameter Digit operator ++ ( int ); // postfix has an int parameter Digit operator -- ( int ); // postfix has an int parameter friend std :: ostream & operator << ( std :: ostream & out , const Digit & d ); }; // No parameter means this is prefix operator++ Digit & Digit :: operator ++ () { // If our number is already at 9, wrap around to 0 if ( m_digit == 9 ) m_digit = 0 ; // otherwise just increment to next number else ++ m_digit ; return * this ; } // No parameter means this is prefix operator-- Digit & Digit :: operator -- () { // If our number is already at 0, wrap around to 9 if ( m_digit == 0 ) m_digit = 9 ; // otherwise just decrement to next number else -- m_digit ; return * this ; } // int parameter means this is postfix operator++ Digit Digit :: operator ++ ( int ) { // Create a temporary variable with our current digit Digit temp { * this }; // Use prefix operator to increment this digit ++ ( * this ); // apply operator // return temporary result return temp ; // return saved state } // int parameter means this is postfix operator-- Digit Digit :: operator -- ( int ) { // Create a temporary variable with our current digit Digit temp { * this }; // Use prefix operator to decrement this digit -- ( * this ); // apply operator // return temporary result return temp ; // return saved state } std :: ostream & operator << ( std :: ostream & out , const Digit & d ) { out << d . m_digit ; return out ; } int main () { Digit digit ( 5 ); std :: cout << digit ; std :: cout << ++ digit ; // calls Digit::operator++(); std :: cout << digit ++ ; // calls Digit::operator++(int); std :: cout << digit ; std :: cout << -- digit ; // calls Digit::operator--(); std :: cout << digit -- ; // calls Digit::operator--(int); std :: cout << digit ; return 0 ; } COPY This prints 1 5667665 There are a few interesting things going on here. First, note that we\u2019ve distinguished the prefix from the postfix operators by providing an integer dummy parameter on the postfix version. Second, because the dummy parameter is not used in the function implementation, we have not even given it a name. This tells the compiler to treat this variable as a placeholder, which means it won\u2019t warn us that we declared a variable but never used it. Third, note that the prefix and postfix operators do the same job -- they both increment or decrement the object. The difference between the two is in the value they return. The overloaded prefix operators return the object after it has been incremented or decremented. Consequently, overloading these is fairly straightforward. We simply increment or decrement our member variables, and then return *this. The postfix operators, on the other hand, need to return the state of the object before it is incremented or decremented. This leads to a bit of a conundrum -- if we increment or decrement the object, we won\u2019t be able to return the state of the object before it was incremented or decremented. On the other hand, if we return the state of the object before we increment or decrement it, the increment or decrement will never be called. The typical way this problem is solved is to use a temporary variable that holds the value of the object before it is incremented or decremented. Then the object itself can be incremented or decremented. And finally, the temporary variable is returned to the caller. In this way, the caller receives a copy of the object before it was incremented or decremented, but the object itself is incremented or decremented. Note that this means the return value of the overloaded operator must be a non-reference, because we can\u2019t return a reference to a local variable that will be destroyed when the function exits. Also note that this means the postfix operators are typically less efficient than the prefix operators because of the added overhead of instantiating a temporary variable and returning by value instead of reference. Finally, note that we\u2019ve written the post-increment and post-decrement in such a way that it calls the pre-increment and pre-decrement to do most of the work. This cuts down on duplicate code, and makes our class easier to modify in the future.","title":"Overloading postfix increment and decrement**"},{"location":"14-9-overloading-the-subscript-operator/","tags":["overload","operator","subscript"],"text":"Key Takeaway When working with arrays, we typically use the subscript operator ([]) to index specific elements of an array: 1 myArray [ 0 ] = 7 ; // put the value 7 in the first element of the array COPY However, consider the following IntList class, which has a member variable that is an array: 1 2 3 4 5 6 7 8 9 10 11 12 class IntList { private : int m_list [ 10 ]{}; }; int main () { IntList list {}; // how do we access elements from m_list? return 0 ; } COPY Because the m_list member variable is private, we can not access it directly from variable list. This means we have no way to directly get or set values in the m_list array. So how do we get or put elements into our list? Without operator overloading, the typical method would be to create access functions: 1 2 3 4 5 6 7 8 9 class IntList { private : int m_list [ 10 ]{}; public : void setItem ( int index , int value ) { m_list [ index ] = value ; } int getItem ( int index ) const { return m_list [ index ]; } }; COPY While this works, it\u2019s not particularly user friendly. Consider the following example: 1 2 3 4 5 6 7 int main () { IntList list {}; list . setItem ( 2 , 3 ); return 0 ; } COPY Are we setting element 2 to the value 3, or element 3 to the value 2? Without seeing the definition of setItem() , it\u2019s simply not clear. You could also just return the entire list and use operator[] to access the element: 1 2 3 4 5 6 7 8 class IntList { private : int m_list [ 10 ]{}; public : int * getList () { return m_list ; } }; COPY While this also works, it\u2019s syntactically odd: 1 2 3 4 5 6 7 int main () { IntList list {}; list . getList ()[ 2 ] = 3 ; return 0 ; } COPY Overloading operator[] However, a better solution in this case is to overload the subscript operator ([]) to allow access to the elements of m_list. The subscript operator is one of the operators that must be overloaded as a member function. An overloaded operator[] function will always take one parameter: the subscript that the user places between the hard braces. In our IntList case, we expect the user to pass in an integer index, and we\u2019ll return an integer value back as a result. 1 2 3 4 5 6 7 8 9 10 11 12 13 class IntList { private : int m_list [ 10 ]{}; public : int & operator [] ( int index ); }; int & IntList :: operator [] ( int index ) { return m_list [ index ]; } COPY Now, whenever we use the subscript operator ([]) on an object of our class, the compiler will return the corresponding element from the m_list member variable! This allows us to both get and set values of m_list directly: 1 2 3 4 5 IntList list {}; list [ 2 ] = 3 ; // set a value std :: cout << list [ 2 ] << '\\n' ; // get a value return 0 ; COPY This is both easy syntactically and from a comprehension standpoint. When list[2] evaluates, the compiler first checks to see if there\u2019s an overloaded operator[] function. If so, it passes the value inside the hard braces (in this case, 2) as an argument to the function. Note that although you can provide a default value for the function parameter, actually using operator[] without a subscript inside is not considered a valid syntax, so there\u2019s no point. Tip C++23 will add support for overloading operator[] with multiple subscripts. Why operator[] returns a reference Let\u2019s take a closer look at how list[2] = 3 evaluates. Because the subscript operator has a higher precedence than the assignment operator, list[2] evaluates first. list[2] calls operator[], which we\u2019ve defined to return a reference to list.m_list[2] . Because operator[] is returning a reference, it returns the actual list.m_list[2] array element. Our partially evaluated expression becomes list.m_list[2] = 3 , which is a straightforward integer assignment. In lesson 9.2 -- Value categories (lvalues and rvalues) , you learned that any value on the left hand side of an assignment statement must be an l-value (which is a variable that has an actual memory address). Because the result of operator[] can be used on the left hand side of an assignment (e.g. list[2] = 3 ), the return value of operator[] must be an l-value. As it turns out, references are always l-values, because you can only take a reference of variables that have memory addresses. So by returning a reference, the compiler is satisfied that we are returning an l-value. Consider what would happen if operator[] returned an integer by value instead of by reference. list[2] would call operator[], which would return the value of list.m_list[2]. For example, if m_list[2] had the value of 6, operator[] would return the value 6. list[2] = 3 would partially evaluate to 6 = 3 , which makes no sense! If you try to do this, the C++ compiler will complain: C:VCProjectsTest.cpp(386) : error C2106: '=' : left operand must be l-value Dealing with const objects In the above IntList example, operator[] is non-const, and we can use it as an l-value to change the state of non-const objects. However, what if our IntList object was const? In this case, we wouldn\u2019t be able to call the non-const version of operator[] because that would allow us to potentially change the state of a const object. The good news is that we can define a non-const and a const version of operator[] separately. The non-const version will be used with non-const objects, and the const version with const-objects. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include class IntList { private : int m_list [ 10 ]{ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 }; // give this class some initial state for this example public : int & operator [] ( int index ); int operator [] ( int index ) const ; // could also return const int& if returning a non-fundamental type }; int & IntList :: operator [] ( int index ) // for non-const objects: can be used for assignment { return m_list [ index ]; } int IntList :: operator [] ( int index ) const // for const objects: can only be used for access { return m_list [ index ]; } int main () { IntList list {}; list [ 2 ] = 3 ; // okay: calls non-const version of operator[] std :: cout << list [ 2 ] << '\\n' ; const IntList clist {}; clist [ 2 ] = 3 ; // compile error: calls const version of operator[], which returns by value. Cannot assign to this because it is an rvalue. std :: cout << clist [ 2 ] << '\\n' ; return 0 ; } COPY If we comment out the line clist[2] = 3 , the above program compiles and executes as expected. Error checking One other advantage of overloading the subscript operator is that we can make it safer than accessing arrays directly. Normally, when accessing arrays, the subscript operator does not check whether the index is valid. For example, the compiler will not complain about the following code: 1 2 int list [ 5 ]{}; list [ 7 ] = 3 ; // index 7 is out of bounds! COPY However, if we know the size of our array, we can make our overloaded subscript operator check to ensure the index is within bounds: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include // for assert() #include // for std::size() class IntList { private : int m_list [ 10 ]{}; public : int & operator [] ( int index ); }; int & IntList :: operator [] ( int index ) { assert ( index >= 0 && index < std :: size ( m_list )); return m_list [ index ]; } COPY In the above example, we have used the assert() function (included in the cassert header) to make sure our index is valid. If the expression inside the assert evaluates to false (which means the user passed in an invalid index), the program will terminate with an error message, which is much better than the alternative (corrupting memory). This is probably the most common method of doing error checking of this sort. Pointers to objects and overloaded operator[] don\u2019t mix If you try to call operator[] on a pointer to an object, C++ will assume you\u2019re trying to index an array of objects of that type. Consider the following example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include // for assert() #include // for std::size() class IntList { private : int m_list [ 10 ]{}; public : int & operator [] ( int index ); }; int & IntList :: operator [] ( int index ) { assert ( index >= 0 && index < std :: size ( m_list )); return m_list [ index ]; } int main () { IntList * list { new IntList {} }; list [ 2 ] = 3 ; // error: this will assume we're accessing index 2 of an array of IntLists delete list ; return 0 ; } COPY Because we can\u2019t assign an integer to an IntList, this won\u2019t compile. However, if assigning an integer was valid, this would compile and run, with undefined results. Rule Make sure you\u2019re not trying to call an overloaded operator[] on a pointer to an object. The proper syntax would be to dereference the pointer first (making sure to use parenthesis since operator[] has higher precedence than operator*), then call operator[]: 1 2 3 4 5 6 7 8 int main () { IntList * list { new IntList {} }; ( * list )[ 2 ] = 3 ; // get our IntList object, then call overloaded operator[] delete list ; return 0 ; } COPY This is ugly and error prone. Better yet, don\u2019t set pointers to your objects if you don\u2019t have to. The function parameter does not need to be an integer As mentioned above, C++ passes what the user types between the hard braces as an argument to the overloaded function. In most cases, this will be an integer value. However, this is not required -- and in fact, you can define that your overloaded operator[] take a value of any type you desire. You could define your overloaded operator[] to take a double, a std::string, or whatever else you like. As a ridiculous example, just so you can see that it works: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include // C++17 class Stupid { private : public : void operator [] ( std :: string_view index ); }; // It doesn't make sense to overload operator[] to print something // but it is the easiest way to show that the function parameter can be a non-integer void Stupid :: operator [] ( std :: string_view index ) { std :: cout << index ; } int main () { Stupid stupid {}; stupid [ \"Hello, world!\" ]; return 0 ; } COPY As you would expect, this prints: Hello, world! Overloading operator[] to take a std::string parameter can be useful when writing certain kinds of classes, such as those that use words as indices. Conclusion The subscript operator is typically overloaded to provide direct access to individual elements from an array (or other similar structure) contained within a class. Because strings are often implemented as arrays of characters, operator[] is often implemented in string classes to allow the user to access a single character of the string.","title":"14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26"},{"location":"14-x-Chapter-14-comprehensive-quiz/","tags":["summary"],"text":"14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb - \u8fd0\u7b97\u7b26 \u91cd\u8f7d \u662f\u51fd\u6570\u91cd\u8f7d\u7684\u4e00\u79cd\u53d8\u5f62\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u4e3a\u4f60\u7684\u7c7b\u91cd\u8f7d\u64cd\u4f5c\u7b26\u3002\u8fd0\u7b97\u7b26\u91cd\u8f7d\u5e94\u8be5\u5c3d\u91cf\u4fdd\u6301\u548c\u5176\u539f\u672c\u542b\u4e49\u7c7b\u4f3c\u7684\u529f\u80fd\u3002\u5982\u679c\u65b0\u7684\u8fd0\u7b97\u7b26\u7684\u542b\u4e49\u4e0d\u660e\u786e\u6216\u8fdd\u53cd\u76f4\u89c9\uff0c\u90a3\u6700\u597d\u8fd8\u662f\u4f7f\u7528\u4e00\u4e2a\u51fd\u6570\u6765\u4ee3\u66ff\u8fd0\u7b97\u7b26\u91cd\u8f7d\u3002 - \u8fd0\u7b97\u7b26\u53ef\u4ee5\u88ab\u91cd\u8f7d\u4e3a\u666e\u901a\u51fd\u6570\u3001 \u53cb\u5143\u51fd\u6570 \u6216\u8005\u6210\u5458\u51fd\u6570\u3002\u4e0b\u9762\u8fd9\u4e9b\u6cd5\u5219\u53ef\u4ee5\u5e2e\u52a9\u4f60\u51b3\u5b9a\u54ea\u79cd\u5f62\u5f0f\u66f4\u9002\u5408\u4f60\uff1a - \u5f53\u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26(=)\uff0c\u4e0b\u6807\u8fd0\u7b97\u7b26( [] )\u3001\u51fd\u6570\u8c03\u7528\u8fd0\u7b97\u7b26( () )\u6216\u8005\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26( -> )\u65f6\uff0c\u91cd\u8f7d\u4e3a \u6210\u5458\u51fd\u6570 \uff1b - \u5f53\u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26\u65f6\uff0c\u91cd\u8f7d\u4e3a \u6210\u5458\u51fd\u6570 \uff1b - \u5f53\u91cd\u8f7d\u9700\u8981\u4fee\u6539\u5176\u5de6\u64cd\u4f5c\u6570\u7684\u4e8c\u5143\u8fd0\u7b97\u7b26\uff08\u4f8b\u5982 += \uff09\u65f6\uff0c\u5982\u679c\u53ef\u4ee5\u5219\u5c3d\u91cf\u91cd\u8f7d\u4e3a \u6210\u5458\u51fd\u6570 \uff1b - \u5f53\u91cd\u8f7d\u4e0d\u9700\u8981\u4fee\u6539\u5176\u5de6\u64cd\u4f5c\u6570\u7684\u4e8c\u5143\u8fd0\u7b97\u7b26\uff08\u4f8b\u5982 + \uff09\u65f6\uff0c\u53ef\u4ee5\u91cd\u8f7d\u4e3a \u6210\u5458\u51fd\u6570 \u6216 \u53cb\u5143\u51fd\u6570 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26 - \u7c7b\u578b\u8f6c\u6362\u4e5f\u53ef\u4ee5\u91cd\u8f7d\uff0c\u7528\u4e8e\u63d0\u4f9b\u8f6c\u6362\u529f\u80fd\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u663e\u5f0f\u6216\u9690\u5f0f\u5730\u5c06\u7c7b\u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b\u3002 14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570 && 14.13 - \u62f7\u8d1d\u521d\u59cb\u5316 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u662f\u4e00\u7c7b\u7279\u6b8a\u7684\u6784\u9020\u51fd\u6570\uff0c\u7528\u4e8e\u4f7f\u7528\u76f8\u540c\u7c7b\u578b\u7684\u4e00\u4e2a\u5bf9\u8c61\u53bb\u521d\u59cb\u5316\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61\u3002\u5f53\u4f7f\u7528\u76f8\u540c\u7c7b\u578b\u7684\u4e00\u4e2a\u5bf9\u8c61\uff0c\u901a\u8fc7\u76f4\u63a5\u521d\u59cb\u5316\u6216 \u7edf\u4e00\u521d\u59cb\u5316 \u7684\u65b9\u5f0f\u521d\u59cb\u5316\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61\u65f6( Fraction f = Fraction(5,3) )\uff0c\u6216\u8005\u4f20\u9012\u3001\u8fd4\u56de\u503c\u65f6\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u5c31\u4f1a\u88ab\u8c03\u7528\u3002 - \u5982\u679c\u4f60\u4e0d\u63d0\u4f9b\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u4e3a\u4f60\u521b\u5efa\u4e00\u4e2a\u3002\u7f16\u8bd1\u5668\u63d0\u4f9b\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4f1a\u4f7f\u7528 \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \uff0c\u5373\u8be5\u5bf9\u8c61\u7684\u6bcf\u4e2a\u6210\u5458\u90fd\u4f1a\u4ece\u7528\u4e8e\u521d\u59cb\u5316\u5b83\u7684\u5bf9\u8c61\u7684\u6bcf\u4e2a\u6210\u5458\u4f9d\u6b21\u62f7\u8d1d\u3002\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u53ef\u80fd\u4f1a\u51fa\u4e8e\u4f18\u5316\u7684\u76ee\u7684\u88ab\u7701\u7565\uff0c\u5373\u4f7f\u5b83\u6709\u526f\u4f5c\u7528\uff0c\u6240\u4ee5\u4e0d\u8981\u671f\u671b\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4e00\u5b9a\u4f1a\u6267\u884c\u3002 14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57 - \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6784\u9020\u51fd\u6570\u4f1a\u88ab\u770b\u505a\u7c7b\u578b\u8f6c\u6362\u6784\u9020\u51fd\u6570\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u9690\u5f0f\u5730\u5c06\u5176\u4ed6\u7c7b\u578b\u7684\u5bf9\u8c61\u8f6c\u6362\u4e3a\u8fd9\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u4e8b\u60c5\u7684\u53d1\u751f\uff0c\u4f60\u53ef\u4ee5\u5728\u6784\u9020\u51fd\u6570\u524d\u9762\u6dfb\u52a0 explicit \u5173\u952e\u5b57\u3002\u4f60\u4e5f\u53ef\u4ee5\u5220\u9664\u7c7b\u4e2d\u7684\u51fd\u6570( =delete )\uff0c\u6bd4\u5982\u5220\u9664\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u6216\u91cd\u8f7d\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002\u5f53\u8c03\u7528\u88ab\u5220\u9664\u7684\u51fd\u6570\u65f6\uff0c\u4f1a\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002 14.15 - \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26 - \u8d4b\u503c\u8fd0\u7b97\u7b26\u53ef\u4ee5\u88ab\u91cd\u8f7d\u5e76\u7528\u4e8e\u7ed9\u4f60\u7684\u7c7b\u8d4b\u503c\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u63d0\u4f9b\u91cd\u8f7d\u7684\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u7f16\u8bd1\u5668\u5219\u4f1a\u81ea\u52a8\u5730\u4e3a\u4f60\u521b\u5efa\u4e00\u4e2a\u3002\u91cd\u8f7d\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u5e94\u8be5\u603b\u662f\u8fdb\u884c\u81ea\u6211\u8d4b\u503c\u68c0\u67e5\uff08\u9664\u975e\u5b83\u80fd\u591f\u81ea\u7136\u5730\u5904\u7406\uff0c\u6216\u8005\u4f60\u9009\u62e9\u4f7f\u7528 copy-swap\u4e60\u8bed ( what is the copy and swap idiom )\uff09\u3002 - \u65b0\u624b\u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u628a \u8d4b\u503c\u8fd0\u7b97\u7b26 \u548c \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u641e\u6df7\uff0c\u5176\u5b9e\u5b83\u4eec\u5f88\u597d\u533a\u5206\uff1a - \u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u5728\u62f7\u8d1d\u524d\uff0c\u5fc5\u987b\u5148\u88ab\u521b\u5efa\uff0c\u5219\u4f1a\u4f7f\u7528 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \uff08\u6ce8\u610f\uff1a\u5305\u62ec\u5bf9\u8c61\u7684\u6309\u503c\u4f20\u9012\u548c\u8fd4\u56de\uff09\uff1b - \u5982\u679c\u5728\u62f7\u8d1d\u524d\uff0c\u4e0d\u9700\u8981\u5148\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\uff0c\u5219\u4f1a\u4f7f\u7528 \u8d4b\u503c\u8fd0\u7b97\u7b26 \u3002 14.16 - \u6d45\u62f7\u8d1d\u548c\u6df1\u62f7\u8d1d - \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u4f1a\u63d0\u4f9b\u4e00\u4e2a\u80fd\u591f\u8fdb\u884c \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u6216\u8d4b\u503c\u8fd0\u7b97\u7b26\uff0c\u5176\u884c\u4e3a\u79f0\u4e3a \u6d45\u62f7\u8d1d \u3002\u800c\u5982\u679c\u4f60\u7684\u7c7b\u4e2d\u5305\u542b\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\uff0c\u6d45\u62f7\u8d1d\u53ef\u80fd\u4f1a\u5bfc\u81f4\u95ee\u9898\u7684\u53d1\u751f\uff0c\u56e0\u4e3a\u591a\u4e2a\u5bf9\u8c61\u6700\u7ec8\u53ef\u80fd\u4f1a\u6307\u5411\u540c\u4e00\u5757\u5185\u5b58\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f60\u5e94\u8be5\u663e\u5f0f\u5730\u5b9a\u4e49\u5bf9\u8c61\u7684\u62f7\u8d1d\uff0c\u5373\u8fdb\u884c \u6df1\u62f7\u8d1d \u3002\u5982\u679c\u4f60\u53ef\u4ee5\u907f\u514d\u81ea\u5df1\u8fdb\u884c\u5185\u5b58\u7ba1\u7406\uff0c\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684\u7c7b\uff0c\u90a3\u5c31\u66f4\u597d\u4e86\u3002","title":"14.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26\u91cd\u8f7d"},{"location":"16-1-object-relationships/","tags":["object"],"text":"Key Takeaway - \u751f\u6d3b\u4e2d\u5145\u6ee1\u4e86\u53cd\u590d\u51fa\u73b0\u7684\u6a21\u5f0f\u3001\u5173\u7cfb\u548c\u7b49\u7ea7\u5173\u7cfb\u3002\u901a\u8fc7\u63a2\u7d22\u548c\u7406\u89e3\u8fd9\u4e9b\u5173\u7cfb\uff0c\u6211\u4eec\u53ef\u4ee5\u6d1e\u5bdf\u73b0\u5b9e\u751f\u6d3b\u4e2d\u7684\u5bf9\u8c61\u662f\u5982\u4f55\u8fd0\u4f5c\u7684\u4ee5\u589e\u5f3a\u6211\u4eec\u5bf9\u8fd9\u4e9b\u5bf9\u8c61\u7684\u7406\u89e3\u3002 \u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u5929\u4f60\u8d70\u5728\u8857\u4e0a\uff0c\u770b\u5230\u4e00\u4e2a\u4eae\u9ec4\u8272\u7684\u7269\u4f53\u8fde\u63a5\u5728\u4e00\u4e2a\u7eff\u8272\u704c\u6728\u72b6\u7684\u7269\u4f53\u4e0a\u3002\u4f60\u53ef\u80fd\u4f1a\u8ba4\u51fa\u4eae\u9ec4\u8272\u7684\u4e1c\u897f\u662f\u82b1\uff0c\u800c\u7eff\u8272\u704c\u6728\u72b6\u7684\u4e1c\u897f\u662f\u690d\u7269\u3002\u5373\u4f7f\u4f60\u4ee5\u524d\u4ece\u672a\u89c1\u8fc7\u8fd9\u79cd\u7279\u6b8a\u7c7b\u578b\u7684\u690d\u7269\uff0c\u4f60\u4e5f\u77e5\u9053\u8fd9\u4e9b\u7eff\u8272\u7684\u4e1c\u897f\u662f\u6811\u53f6\uff0c\u5b83\u4eec\u6536\u96c6\u9633\u5149\u3002\u4f60\u77e5\u9053\u8fd9\u79cd\u82b1\u80fd\u5e2e\u52a9\u690d\u7269\u7e41\u6b96\u3002\u4f60\u8fd8\u4f1a\u77e5\u9053\uff0c\u5982\u679c\u4f60\u6740\u6b7b\u4e86\u690d\u7269\uff0c\u82b1\u4e5f\u4f1a\u6b7b\u53bb\u3002 \u4f46\u4f60\u4ee5\u524d\u4ece\u672a\u89c1\u8fc7\u8fd9\u79cd\u690d\u7269\uff0c\u4f60\u600e\u4e48\u77e5\u9053\u8fd9\u4e9b\u5462\uff1f\u4f60\u77e5\u9053\u8fd9\u4e00\u70b9\u662f\u56e0\u4e3a\u4f60\u7406\u89e3\u690d\u7269\u7684\u62bd\u8c61\u6982\u5ff5\uff0c\u5e76\u4e14\u8ba4\u8bc6\u5230\u8fd9\u4e2a\u690d\u7269\u662f\u8fd9\u4e2a\u62bd\u8c61\u6982\u5ff5\u7684\u4e00\u4e2a\u5b9e\u4f8b\u3002\u4f60\u77e5\u9053\u5927\u591a\u6570\u690d\u7269(\u90e8\u5206)\u662f\u7531\u53f6\u5b50\u7ec4\u6210\u7684\uff0c\u6709\u4e9b\u690d\u7269\u6709\u82b1\u3002\u4f60\u77e5\u9053\u53f6\u5b50\u4f1a\u4e0e\u9633\u5149\u76f8\u4e92\u4f5c\u7528(\u5373\u4f7f\u4f60\u4e0d\u77e5\u9053\u5177\u4f53\u662f\u5982\u4f55\u4f5c\u7528\u7684)\uff0c\u4f60\u4e5f\u77e5\u9053\u82b1\u7684\u5b58\u5728\u4f9d\u8d56\u4e8e\u690d\u7269\u3002\u56e0\u4e3a\u4f60\u77e5\u9053\u6240\u6709\u8fd9\u4e9b\u5173\u4e8e\u690d\u7269\u7684\u62bd\u8c61\u6982\u5ff5\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u63a8\u65ad\u51fa\u5f88\u591a\u5173\u4e8e\u8fd9\u79cd\u690d\u7269\u7684\u8fdb\u4e00\u6b65\u4fe1\u606f\u3002 \u7c7b\u4f3c\u5730\uff0c\u7f16\u7a0b\u4e5f\u5145\u6ee1\u4e86\u91cd\u590d\u7684\u6a21\u5f0f\u3001\u5173\u7cfb\u548c\u5c42\u6b21\u7ed3\u6784\u3002\u7279\u522b\u662f\u5f53\u6d89\u53ca\u5230\u5bf9\u8c61\u65f6\uff0c\u7ba1\u7406\u73b0\u5b9e\u751f\u6d3b\u5bf9\u8c61\u7684\u76f8\u540c\u6a21\u5f0f\u4e5f\u9002\u7528\u4e8e\u6211\u4eec\u81ea\u5df1\u5728\u7a0b\u5e8f\u4e2d\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u901a\u8fc7\u4ed4\u7ec6\u5206\u6790\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\uff0c\u5e76\u7f16\u5199\u66f4\u5177\u53ef\u6269\u5c55\u6027\u7684\u7c7b\u3002 \u5728\u524d\u51e0\u7ae0\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u63a2\u8ba8\u4e86\u4e00\u4e9b\u5173\u4e8e\u5faa\u73af\u6a21\u5f0f\u7684\u60f3\u6cd5\uff1a\u6211\u4eec\u521b\u5efa\u4e86\u5faa\u73af\u548c\u51fd\u6570\uff0c\u4ee5\u5141\u8bb8\u6211\u4eec\u591a\u6b21\u6267\u884c\u7279\u5b9a\u7684\u4efb\u52a1\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u521b\u5efa\u4e86\u81ea\u5df1\u7684\u679a\u4e3e\u3001\u7ed3\u6784\u548c\u7c7b\uff0c\u4ee5\u5141\u8bb8\u6211\u4eec\u5b9e\u4f8b\u5316\u7ed9\u5b9a\u7c7b\u578b\u7684\u5bf9\u8c61\u3002 \u6211\u4eec\u8fd8\u63a2\u8ba8\u4e86\u5c42\u6b21\u7ed3\u6784\u7684\u4e00\u4e9b\u57fa\u672c\u5f62\u5f0f\uff0c\u4f8b\u5982\u6570\u7ec4(\u5b83\u5141\u8bb8\u6211\u4eec\u5c06\u5143\u7d20\u5206\u7ec4\u5230\u66f4\u5927\u7684\u7ed3\u6784\u4e2d)\u548c\u9012\u5f52(\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u7684\u6d3e\u751f\u7248\u672c)\u3002 \u4f46\u662f\uff0c\u6211\u4eec\u8fd8\u6ca1\u6709\u8fc7\u591a\u5730\u5173\u6ce8\u5bf9\u8c61\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u7279\u522b\u662f\u4e0e\u7f16\u7a0b\u76f8\u5173\u7684\u5173\u7cfb\u3002 \u5bf9\u8c61\u4e4b\u95f4\u7684\u5173\u7cfb \u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\uff0c\u4e24\u4e2a\u5bf9\u8c61\u4e4b\u95f4\u53ef\u80fd\u5b58\u5728\u8bb8\u591a\u4e0d\u540c\u7c7b\u578b\u7684\u5173\u7cfb\uff0c\u6211\u4eec\u4f7f\u7528\u7279\u5b9a\u7684\u201c\u5173\u7cfb\u7c7b\u578b\u201d\u8bcd\u6c47\u6765\u63cf\u8ff0\u8fd9\u4e9b\u5173\u7cfb\u3002\u4f8b\u5982\uff1a\u6b63\u65b9\u5f62\u201d\u662f\uff08is-a\uff09\u201d\u4e00\u79cd\u5f62\u72b6\u3002\u6c7d\u8f66\u201c\u6709\uff08has-a\uff09\u201c\u65b9\u5411\u76d8\u3002\u8ba1\u7b97\u673a\u7a0b\u5e8f\u5458\u201c\u4f7f\u7528\uff08uses-a\uff09\u201d\u952e\u76d8\u3002\u4e00\u6735\u82b1\u201c\u4f9d\u8d56\uff08depends-on\uff09\u201d\u871c\u8702\u6388\u7c89\u3002\u5b66\u751f\u662f\u73ed\u7ea7\u7684\u201c\u6210\u5458\uff08member-of\uff09\u201d\u3002\u4f60\u7684\u5927\u8111\u4f5c\u4e3a\u4f60\u7684\u201c\u4e00\u90e8\u5206\uff08part-of\uff09\u201d\u5b58\u5728(\u81f3\u5c11\uff0c\u5982\u679c\u4f60\u5df2\u7ecf\u8d70\u5230\u8fd9\u4e00\u6b65\uff0c\u6211\u4eec\u53ef\u4ee5\u5408\u7406\u5730\u5047\u8bbe\u662f\u8fd9\u6837)\u3002 \u6240\u6709\u8fd9\u4e9b\u5173\u7cfb\u7c7b\u578b\u5728 C++ \u4e2d\u90fd\u6709\u6709\u7528\u7684\u7c7b\u6bd4\u3002 \u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u7d22\u5173\u7cfb\u7c7b\u578b\u201cpart-of\u201d\u3001\u201chas-a\u201d\u3001\u201cuses-a\u201d\u3001\u201cdepends-on\u201d\u548c\u201cmember-of\u201d\u7684\u7ec6\u5fae\u5dee\u522b\uff0c\u5e76\u5c55\u793a\u5b83\u4eec\u5728 C++ \u7c7b\u8bed\u5883\u4e2d\u662f\u5982\u4f55\u4f7f\u7528\u7684\u3002\u6211\u4eec\u8fd8\u5c06\u63a2\u8ba8\u4e00\u4e9b\u5176\u4ed6\u5730\u65b9\u4e0d\u592a\u9002\u5408\u7684\u76f8\u5173\u4e3b\u9898\u3002 \u7136\u540e\uff0c\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u4e24\u7ae0\u4e2d\u901a\u8fc7 C++ \u7684\u7ee7\u627f\u6a21\u578b\u548c\u865a\u51fd\u6570\u6765\u63a2\u7d22\u201cis-a\u201d\u5173\u7cfb\u3002\u662f\u7684\uff0c\u8fd9\u662f\u4e00\u4e2a\u5927\u95ee\u9898\u3002 \u597d\u4e86\uff0c\u80cc\u666f\u4ecb\u7ecd\u5230\u6b64\u4e3a\u6b62\u3002\u8ba9\u6211\u4eec\u5f00\u59cb\u5427\u3002","title":"16.1 - \u5bf9\u8c61\u5173\u7cfb"},{"location":"16-1-object-relationships/#_1","text":"\u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\uff0c\u4e24\u4e2a\u5bf9\u8c61\u4e4b\u95f4\u53ef\u80fd\u5b58\u5728\u8bb8\u591a\u4e0d\u540c\u7c7b\u578b\u7684\u5173\u7cfb\uff0c\u6211\u4eec\u4f7f\u7528\u7279\u5b9a\u7684\u201c\u5173\u7cfb\u7c7b\u578b\u201d\u8bcd\u6c47\u6765\u63cf\u8ff0\u8fd9\u4e9b\u5173\u7cfb\u3002\u4f8b\u5982\uff1a\u6b63\u65b9\u5f62\u201d\u662f\uff08is-a\uff09\u201d\u4e00\u79cd\u5f62\u72b6\u3002\u6c7d\u8f66\u201c\u6709\uff08has-a\uff09\u201c\u65b9\u5411\u76d8\u3002\u8ba1\u7b97\u673a\u7a0b\u5e8f\u5458\u201c\u4f7f\u7528\uff08uses-a\uff09\u201d\u952e\u76d8\u3002\u4e00\u6735\u82b1\u201c\u4f9d\u8d56\uff08depends-on\uff09\u201d\u871c\u8702\u6388\u7c89\u3002\u5b66\u751f\u662f\u73ed\u7ea7\u7684\u201c\u6210\u5458\uff08member-of\uff09\u201d\u3002\u4f60\u7684\u5927\u8111\u4f5c\u4e3a\u4f60\u7684\u201c\u4e00\u90e8\u5206\uff08part-of\uff09\u201d\u5b58\u5728(\u81f3\u5c11\uff0c\u5982\u679c\u4f60\u5df2\u7ecf\u8d70\u5230\u8fd9\u4e00\u6b65\uff0c\u6211\u4eec\u53ef\u4ee5\u5408\u7406\u5730\u5047\u8bbe\u662f\u8fd9\u6837)\u3002 \u6240\u6709\u8fd9\u4e9b\u5173\u7cfb\u7c7b\u578b\u5728 C++ \u4e2d\u90fd\u6709\u6709\u7528\u7684\u7c7b\u6bd4\u3002 \u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u7d22\u5173\u7cfb\u7c7b\u578b\u201cpart-of\u201d\u3001\u201chas-a\u201d\u3001\u201cuses-a\u201d\u3001\u201cdepends-on\u201d\u548c\u201cmember-of\u201d\u7684\u7ec6\u5fae\u5dee\u522b\uff0c\u5e76\u5c55\u793a\u5b83\u4eec\u5728 C++ \u7c7b\u8bed\u5883\u4e2d\u662f\u5982\u4f55\u4f7f\u7528\u7684\u3002\u6211\u4eec\u8fd8\u5c06\u63a2\u8ba8\u4e00\u4e9b\u5176\u4ed6\u5730\u65b9\u4e0d\u592a\u9002\u5408\u7684\u76f8\u5173\u4e3b\u9898\u3002 \u7136\u540e\uff0c\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u4e24\u7ae0\u4e2d\u901a\u8fc7 C++ \u7684\u7ee7\u627f\u6a21\u578b\u548c\u865a\u51fd\u6570\u6765\u63a2\u7d22\u201cis-a\u201d\u5173\u7cfb\u3002\u662f\u7684\uff0c\u8fd9\u662f\u4e00\u4e2a\u5927\u95ee\u9898\u3002 \u597d\u4e86\uff0c\u80cc\u666f\u4ecb\u7ecd\u5230\u6b64\u4e3a\u6b62\u3002\u8ba9\u6211\u4eec\u5f00\u59cb\u5427\u3002","title":"\u5bf9\u8c61\u4e4b\u95f4\u7684\u5173\u7cfb"},{"location":"16-2-composition/","tags":["object","composition"],"text":"Key Takeaway - \u5bf9\u8c61\u7ec4\u5408 \u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\uff0c\u590d\u6742\u7684\u4e1c\u897f\u5f80\u5f80\u90fd\u57fa\u4e8e\u66f4\u5c0f\u7684\u3001\u66f4\u7b80\u5355\u7684\u4e1c\u897f\u6784\u5efa\u3002\u4f8b\u5982\uff0c\u6c7d\u8f66\u662f\u57fa\u4e8e\u8f66\u67b6\u3001\u5f15\u8d77\u3001\u8f6e\u80ce\u3001\u53d8\u901f\u7bb1\u3001\u65b9\u5411\u76d8\u548c\u5f88\u591a\u5176\u4ed6\u90e8\u4ef6\u6784\u5efa\u7684\u3002\u4e2a\u4eba\u7535\u8111\u5219\u662f\u57fa\u4e8eCPU\u3001\u4e3b\u677f\u3001\u5185\u5b58\u7b49\u6784\u5efa\u7684\u3002\u751a\u81f3\u4f60\u81ea\u5df1\u4e5f\u53ef\u4ee5\u5206\u4e3a\u66f4\u5c0f\u7684\u90e8\u5206\uff1a\u5934\u3001\u8eab\u4f53\u3001\u817f\u3001\u624b\u81c2\u7b49\u7b49\u3002\u901a\u8fc7\u7b80\u5355\u5bf9\u8c61\u6784\u5efa\u590d\u6742\u5bf9\u8c61\u7684\u8fc7\u7a0b\u79f0\u4e3a\u5bf9\u8c61 \u7ec4\u5408 \u3002 \u4e00\u822c\u6765\u8bf4\uff0c\u5bf9\u8c61\u7ec4\u5408\u6a21\u578b\u4e2d\u7684\u5bf9\u8c61\u4e4b\u95f4\u662f\u201c\u6709\u4e00\u4e2a\uff08has-a\uff09\u201d\u7684\u5173\u7cfb\u3002\u6c7d\u8f66\u201c\u6709\u4e00\u4e2a\u201d\u53d8\u901f\u7bb1\u3002\u7535\u8111\u201c\u6709\u4e00\u4e2a\u201dCPU\uff0c\u4f60\u201c\u6709\u4e00\u4e2a\u201d\u5fc3\u810f\u3002\u590d\u6742\u5bf9\u8c61\u901a\u5e38\u88ab\u79f0\u4e3a \u6574\u4f53 \u3002\u800c\u7b80\u5355\u7684\u5bf9\u8c61\u901a\u5e38\u88ab\u79f0\u4e3a\u90e8\u4ef6\u6216\u7ec4\u4ef6\u3002 \u5728C++\u4e2d\uff0c\u4f60\u5df2\u7ecf\u770b\u5230\u7ed3\u6784\u548c\u7c7b\u53ef\u4ee5\u62e5\u6709\u5404\u79cd\u7c7b\u578b\u7684\u6570\u636e\u6210\u5458(\u4f8b\u5982\u57fa\u672c\u7c7b\u578b\u6216\u5176\u4ed6\u7c7b)\u3002\u5f53\u4f7f\u7528\u6570\u636e\u6210\u5458\u6784\u5efa\u7c7b\u65f6\uff0c\u5c31\u662f\u5728\u7528\u66f4\u7b80\u5355\u7684\u90e8\u5206\u6784\u9020\u590d\u6742\u7684\u5bf9\u8c61\uff0c\u8fd9\u5c31\u662f\u5bf9\u8c61\u7ec4\u5408\u3002\u56e0\u6b64\uff0c\u7ed3\u6784\u548c\u7c7b\u6709\u65f6\u88ab\u79f0\u4e3a \u7ec4\u5408\u7c7b\u578b \u3002 \u5bf9\u8c61\u7ec4\u5408\u5728C++\u4e2d\u5f88\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u5c06\u66f4\u7b80\u5355\u3001\u66f4\u5bb9\u6613\u7ba1\u7406\u7684\u90e8\u5206\u6765\u7ec4\u5408\u8d77\u6765\u521b\u5efa\u590d\u6742\u7684\u7c7b\u3002\u8fd9\u964d\u4f4e\u4e86\u590d\u6742\u6027\uff0c\u5e76\u4f7f\u6211\u4eec\u53ef\u4ee5\u66f4\u5feb\u5730\u7f16\u5199\u4ee3\u7801\uff0c\u4e5f\u66f4\u4e0d\u5bb9\u6613\u51fa\u9519\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u91cd\u7528\u5df2\u7ecf\u7f16\u5199\u3001\u6d4b\u8bd5\u548c\u9a8c\u8bc1\u8fc7\u7684\u4ee3\u7801\u3002 \u5bf9\u8c61\u7ec4\u5408\u7684\u7c7b\u578b \u5bf9\u8c61\u7ec4\u5408\u6709\u4e24\u79cd\u57fa\u672c\u7684\u5b50\u7c7b\u578b\uff1a\u7ec4\u5408\u548c\u805a\u5408\u3002\u8fd9\u8282\u8bfe\u6211\u4eec\u5c06\u8ba8\u8bba\u7ec4\u5408\uff0c\u4e0b\u8282\u8bfe\u6211\u4eec\u5c06\u8ba8\u8bba \u805a\u5408\u5173\u7cfb \u3002 \u5173\u4e8e\u672f\u8bed\u7684\u8bf4\u660e\uff1a\u672f\u8bed\u201c\u7ec4\u5408\u201d\u901a\u5e38\u540c\u65f6\u6307\u7ec4\u5408\u548c\u805a\u5408\uff0c\u800c\u4e0d\u4ec5\u4ec5\u6307\u7ec4\u5408\u5b50\u7c7b\u578b\u3002\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u5f53\u6211\u4eec\u63d0\u5230\u4e24\u8005\u65f6\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u672f\u8bed\u201c\u5bf9\u8c61\u7ec4\u5408\u201d\uff0c\u5f53\u6211\u4eec\u4e13\u95e8\u63d0\u5230\u7ec4\u5408\u5b50\u7c7b\u578b\u65f6\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u672f\u8bed\u201c\u7ec4\u5408\u201d\u3002 \u7ec4\u5408 \u5408\u683c\u7684\u7ec4\u5408\u5173\u7cfb\u9700\u8981\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\uff1a \u90e8\u5206(\u6210\u5458)\u662f\u5bf9\u8c61(\u7c7b)\u7684\u4e00\u90e8\u5206 \u90e8\u4ef6(\u6210\u5458)\u4e00\u6b21\u53ea\u80fd\u5c5e\u4e8e\u4e00\u4e2a\u5bf9\u8c61(\u7c7b) \u90e8\u5206(\u6210\u5458)\u7684\u5b58\u5728\u7531\u5bf9\u8c61(\u7c7b)\u7ba1\u7406 \u90e8\u5206(\u6210\u5458)\u4e0d\u77e5\u9053\u5bf9\u8c61(\u7c7b)\u7684\u5b58\u5728 \u73b0\u5b9e\u751f\u6d3b\u4e2d\u4e00\u4e2a\u5f88\u597d\u7684\u7ec4\u5408\u4f8b\u5b50\u662f\u4e00\u4e2a\u4eba\u7684\u8eab\u4f53\u548c\u5fc3\u810f\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u8ba9\u6211\u4eec\u66f4\u8be6\u7ec6\u5730\u7814\u7a76\u8fd9\u4e9b\u5185\u5bb9\u3002 \u7ec4\u5408\u5173\u7cfb\u662f\u201c\u90e8\u5206-\u6574\u4f53\u201d\u7684\u5173\u7cfb\uff0c\u5176\u4e2d\u90e8\u5206\u5fc5\u987b\u6784\u6210\u6574\u4e2a\u5bf9\u8c61\u7684\u4e00\u90e8\u5206\u3002\u4f8b\u5982\uff0c\u5fc3\u810f\u662f\u4e00\u4e2a\u4eba\u8eab\u4f53\u7684\u4e00\u90e8\u5206\u3002\u7ec4\u5408\u4e2d\u7684\u90e8\u5206\u53ea\u80fd\u662f\u5c5e\u4e8e \u4e00\u4e2a\u5bf9\u8c61 \u7684\u4e00\u90e8\u5206\u3002\u4f5c\u4e3a\u4e00\u4e2a\u4eba\u8eab\u4f53\u4e00\u90e8\u5206\u7684\u5fc3\u810f\u4e0d\u53ef\u80fd\u540c\u65f6\u662f\u53e6\u4e00\u4e2a\u4eba\u8eab\u4f53\u7684\u4e00\u90e8\u5206\u3002 \u5728\u7ec4\u5408\u5173\u7cfb\u4e2d\uff0c\u5bf9\u8c61\u8d1f\u8d23\u7ba1\u7406\u5404\u4e2a\u90e8\u4ef6\u7684\u5b58\u5728\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u8fd9\u610f\u5473\u7740\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u521b\u5efa\u8be5\u90e8\u4ef6\uff0c\u5728\u9500\u6bc1\u5bf9\u8c61\u65f6\u9500\u6bc1\u8be5\u90e8\u5206\u3002\u4f46\u66f4\u5e7f\u6cdb\u5730\u8bf4\uff0c\u5b83\u610f\u5473\u7740\u5bf9\u8c61\u4ee5\u4e00\u79cd\u4e0d\u9700\u8981\u5bf9\u8c61\u7684\u7528\u6237\u53c2\u4e0e\u7684\u65b9\u5f0f\u7ba1\u7406\u90e8\u4ef6\u7684\u751f\u547d\u5468\u671f\u3002\u4f8b\u5982\uff0c\u5f53\u4e00\u4e2a\u8eab\u4f53\u88ab\u521b\u9020\u51fa\u6765\u65f6\uff0c\u5fc3\u810f\u4e5f\u88ab\u521b\u9020\u51fa\u6765\u4e86\u3002\u5f53\u4e00\u4e2a\u4eba\u7684\u8eab\u4f53\u88ab\u6467\u6bc1\u65f6\uff0c\u4ed6\u7684\u5fc3\u4e5f\u4f1a\u88ab\u6467\u6bc1\u3002\u6b63\u56e0\u4e3a\u5982\u6b64\uff0c\u7ec4\u5408\u6709\u65f6\u88ab\u79f0\u4e3a\u201c\u6b7b\u4ea1\u5173\u7cfb\u201d\u3002 \u6700\u540e\uff0c\u90e8\u5206\u4e0d\u77e5\u9053\u6574\u4f53\u7684\u5b58\u5728\u3002\u4f60\u7684\u5fc3\u5728\u5e78\u798f\u5730\u8fd0\u8f6c\u7740\uff0c\u5374\u6ca1\u6709\u610f\u8bc6\u5230\u5b83\u662f\u4e00\u4e2a\u66f4\u5927\u7ed3\u6784\u7684\u4e00\u90e8\u5206\u3002\u6211\u4eec\u79f0\u4e4b\u4e3a\u5355\u5411\u5173\u7cfb\uff0c\u56e0\u4e3a\u8eab\u4f53\u4e86\u89e3\u5fc3\u810f\uff0c\u4f46\u5fc3\u810f\u4e0d\u4e86\u89e3\u8eab\u4f53\u3002 \u6ce8\u610f\uff0c\u7ec4\u5408\u5173\u7cfb\u4e0e\u90e8\u4ef6\u7684\u53ef\u79fb\u690d\u6027\u65e0\u5173\u3002\u5fc3\u810f\u53ef\u4ee5\u4ece\u4e00\u4e2a\u8eab\u4f53\u79fb\u690d\u5230\u53e6\u4e00\u4e2a\u8eab\u4e0a\u3002\u7136\u800c\uff0c\u5373\u4f7f\u5728\u79fb\u690d\u540e\uff0c\u5b83\u4ecd\u7136\u6ee1\u8db3\u7ec4\u5408\u7684\u5b9a\u4e49(\u5fc3\u810f\u73b0\u5728\u5c5e\u4e8e\u63a5\u53d7\u8005\uff0c\u9664\u975e\u518d\u6b21\u79fb\u690d\uff0c\u5426\u5219\u53ea\u80fd\u662f\u63a5\u53d7\u8005\u5bf9\u8c61\u7684\u4e00\u90e8\u5206)\u3002 \u6211\u4eec\u7684 Fraction \u7c7b\u5c31\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u7ec4\u5408\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 class Fraction { private : int m_numerator ; int m_denominator ; public : Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { } }; \u8fd9\u4e2a\u7c7b\u6709\u4e24\u4e2a\u6570\u636e\u6210\u5458\uff1a\u5206\u5b50\u548c\u5206\u6bcd\u3002\u5206\u5b50\u548c\u5206\u6bcd\u662f\u5206\u6570\u7684\u4e00\u90e8\u5206(\u5305\u542b\u5728\u5206\u6570\u4e2d)\u3002\u5b83\u4eec\u4e0d\u80fd\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u5206\u6570\u3002\u5206\u5b50\u548c\u5206\u6bcd\u5e76\u4e0d\u77e5\u9053\u5b83\u4eec\u662f\u5206\u6570\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u4eec\u53ea\u5305\u542b\u6574\u6570\u3002\u5728\u521b\u5efa Fraction \u5b9e\u4f8b\u65f6\uff0c\u5c06\u521b\u5efa\u5206\u5b50\u548c\u5206\u6bcd\u3002\u5f53\u5206\u6570\u5b9e\u4f8b\u88ab\u7834\u574f\u65f6\uff0c\u5206\u5b50\u548c\u5206\u6bcd\u4e5f\u4f1a\u88ab\u7834\u574f\u3002 \u5c3d\u7ba1\u5bf9\u8c61\u7ec4\u5408\u5173\u7cfb\u63cf\u8ff0\u7684\u662f\u201c\u5177\u6709\u201d\u7684\u5173\u7cfb\uff08\u4eba\u4f53\u6709\u4e00\u4e2a\u5fc3\u810f\u3001\u5206\u6570\u6709\u4e00\u4e2a\u5206\u6bcd\uff09\uff0c\u4f46\u662f\u66f4\u52a0\u7cbe\u786e\u7684\u8bf4\u6cd5\u5176\u5b9e\u662f\uff0c\u7ec4\u5408\u63cf\u8ff0\u7684\u662f\u201c\u90e8\u5206\u4e0e\u6574\u4f53\u201d\u7684\u5173\u7cfb\uff08\u5fc3\u810f\u662f\u4eba\u4f53\u7684\u4e00\u90e8\u5206\u3001\u5206\u5b50\u662f\u5206\u6570\u7684\u4e00\u90e8\u5206\uff09\u3002\u7ec4\u5408\u5173\u7cfb\u901a\u5e38\u7528\u4e8e\u5bf9\u4e00\u4e2a\u7269\u7406\u5173\u7cfb\u8fdb\u884c\u5efa\u6a21\uff0c\u5373\u4e00\u4e2a\u5bf9\u8c61\u88ab\u5305\u542b\u5728\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61\u4e2d\u3002 \u201c\u90e8\u5206\u201d\u5173\u7cfb\u53ef\u4ee5\u662f\u201c\u4e00\u5bf9\u4e00\u7684\u201d\uff0c\u4e5f\u53ef\u4ee5\u662f\u201c\u591a\u5bf9\u4e00\u7684\u201d\u2014\u2014\u4f8b\u5982\uff0c\u8eab\u4f53\u4e2d\u5305\u542b\u4e00\u4e2a\u5fc3\u810f\uff0c\u4f46\u662f\u8eab\u4f53\u53ef\u4ee5\u5305\u542b10\u4e2a\u624b\u6307\uff08\u6700\u7ec8\u88ab\u5b9e\u73b0\u4e3a\u6570\u7ec4\uff09\u3002 \u5b9e\u73b0\u7ec4\u5408\u5173\u7cfb \u7ec4\u5408\u662fC++\u4e2d\u6700\u5bb9\u6613\u5b9e\u73b0\u7684\u5173\u7cfb\u7c7b\u578b\u4e4b\u4e00\u3002\u5b83\u4eec\u901a\u5e38\u88ab\u521b\u5efa\u4e3a\u5177\u6709\u666e\u901a\u6570\u636e\u6210\u5458\u7684\u7ed3\u6784\u6216\u7c7b\u3002\u56e0\u4e3a\u8fd9\u4e9b\u6570\u636e\u6210\u5458\u76f4\u63a5\u4f5c\u4e3a\u7ed3\u6784/\u7c7b\u7684\u4e00\u90e8\u5206\u5b58\u5728\uff0c\u6240\u4ee5\u5b83\u4eec\u7684\u751f\u547d\u5468\u671f\u4e0e\u7c7b\u5b9e\u4f8b\u672c\u8eab\u7684\u751f\u547d\u5468\u671f\u7ed1\u5b9a\u5728\u4e00\u8d77\u3002 \u9700\u8981\u8fdb\u884c\u52a8\u6001\u5206\u914d\u6216\u56de\u6536\u7684\u7ec4\u5408\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\u6570\u636e\u6210\u5458\u5b9e\u73b0\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7ec4\u5408\u7c7b\u5e94\u8be5\u81ea\u5df1\u8d1f\u8d23\u6240\u6709\u5fc5\u8981\u7684\u5185\u5b58\u7ba1\u7406(\u800c\u4e0d\u662f\u7c7b\u7684\u7528\u6237)\u3002 \u4e00\u822c\u6765\u8bf4\uff0c\u5982\u679c\u4f60\u80fd\u4f7f\u7528\u590d\u5408\u8bbe\u8ba1\u4e00\u4e2a\u7c7b\uff0c\u4f60\u5c31\u5e94\u8be5\u4f7f\u7528\u7ec4\u5408\u5173\u7cfb\u8bbe\u8ba1\u4e00\u4e2a\u7c7b\u3002\u4f7f\u7528\u7ec4\u5408\u8bbe\u8ba1\u7684\u7c7b\u662f\u76f4\u63a5\u7684\u3001\u7075\u6d3b\u7684\u548c\u5065\u58ee\u7684(\u56e0\u4e3a\u5b83\u4eec\u5f88\u597d\u5730\u81ea\u884c\u6e05\u7406)\u3002 \u66f4\u591a\u7684\u4f8b\u5b50 \u8bb8\u591a\u6e38\u620f\u90fd\u6709\u5728\u68cb\u76d8\u3001\u5730\u56fe\u6216\u5c4f\u5e55\u4e0a\u79fb\u52a8\u7684\u751f\u7269\u6216\u7269\u4f53\u3002\u6240\u6709\u8fd9\u4e9b\u751f\u7269/\u7269\u4f53\u7684\u4e00\u4e2a\u5171\u540c\u70b9\u662f\uff0c\u5b83\u4eec\u90fd\u6709\u4e00\u4e2a\u4f4d\u7f6e\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u751f\u7269\u7c7b\uff0c\u5b83\u4f7f\u7528\u4e00\u4e2a Point \u7c7b\u6765\u4fdd\u5b58\u751f\u7269\u7684\u4f4d\u7f6e\u3002 \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u8bbe\u8ba1 Point \u7c7b\u3002\u751f\u7269\u5c06\u751f\u6d3b\u57282d\u4e16\u754c\u4e2d\uff0c\u6240\u4ee5 Point \u7c7b\u53ea\u9700\u8981\u6709\u4e24\u4e2a\u7ef4\u5ea6 x \u548c y \u3002\u6211\u4eec\u5047\u8bbe\u4e16\u754c\u662f\u7531\u79bb\u6563\u7684\u6b63\u65b9\u5f62\u7ec4\u6210\uff0c\u6240\u4ee5\u8fd9\u4e9b\u7ef4\u5ea6\u603b\u662f\u6574\u6570\u3002 Point2D.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #ifndef POINT2D_H #define POINT2D_H #include class Point2D { private : int m_x ; int m_y ; public : // \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 Point2D () : m_x { 0 }, m_y { 0 } { } // \u6709\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570 Point2D ( int x , int y ) : m_x { x }, m_y { y } { } // \u91cd\u8f7d\u7684\u8f93\u51fa\u64cd\u4f5c\u7b26 friend std :: ostream & operator << ( std :: ostream & out , const Point2D & point ) { out << '(' << point . m_x << \", \" << point . m_y << ')' ; return out ; } // \u6210\u5458\u8bbf\u95ee\u51fd\u6570 void setPoint ( int x , int y ) { m_x = x ; m_y = y ; } }; #endif \u6ce8\u610f\uff0c\u56e0\u4e3a\u6240\u6709\u51fd\u6570\u7684\u5b9e\u73b0\u90fd\u5728\u5934\u6587\u4ef6\u4e2d\uff08\u4e3a\u4e86\u4fdd\u6301\u4f8b\u5b50\u7b80\u77ed\uff09\uff0c\u6240\u4ee5\u5e76\u6ca1\u6709 Point2D.cpp \u6587\u4ef6\u3002 Point2d \u662f\u5b83\u7684\u90e8\u4ef6\u7684\u7684\u7ec4\u5408\uff1a\u4f4d\u7f6e\u4fe1\u606fx\u548cy\u90fd\u662f Point2D \u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u5b83\u4eec\u7684\u751f\u547d\u5468\u671f\u4e5f\u548c Point2D \u5b9e\u4f8b\u7ed1\u5b9a\u5728\u4e00\u8d77\u3002 \u73b0\u5728\uff0c\u5b9a\u4e49 Creature \u7c7b\u3002 Creature \u6709\u5982\u4e0b\u51e0\u4e2a\u5c5e\u6027\uff1a\u540d\u79f0\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4f4d\u7f6e\u5219\u662f\u4e00\u4e2a Point2D \u7c7b\u3002 Creature.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #ifndef CREATURE_H #define CREATURE_H #include #include #include \"Point2D.h\" class Creature { private : std :: string m_name ; //\u540d\u79f0 Point2D m_location ; //\u4f4d\u7f6e public : Creature ( const std :: string & name , const Point2D & location ) : m_name { name }, m_location { location } { } friend std :: ostream & operator << ( std :: ostream & out , const Creature & creature ) { out << creature . m_name << \" is at \" << creature . m_location ; return out ; } void moveTo ( int x , int y ) { m_location . setPoint ( x , y ); } }; #endif Creature \u540c\u6837\u4e5f\u662f\u5176\u5404\u90e8\u5206\u7684\u7ec4\u5408\u3002\u751f\u7269\u7684\u540d\u5b57\u548c\u4f4d\u7f6e\u5176\u58f0\u660e\u5468\u671f\u90fd\u7ed1\u5b9a\u5230\u4e86 Creature \u3002 \u6700\u7ec8\u7684\u7a0b\u5e8f\u662f\u8fd9\u6837\u7684\u3002 main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include #include #include \"Creature.h\" #include \"Point2D.h\" int main () { std :: cout << \"Enter a name for your creature: \" ; std :: string name ; std :: cin >> name ; Creature creature { name , { 4 , 7 } }; while ( true ) { // print the creature's name and location std :: cout << creature << '\\n' ; std :: cout << \"Enter new X location for creature (-1 to quit): \" ; int x { 0 }; std :: cin >> x ; if ( x == -1 ) break ; std :: cout << \"Enter new Y location for creature (-1 to quit): \" ; int y { 0 }; std :: cin >> y ; if ( y == -1 ) break ; creature . moveTo ( x , y ); } return 0 ; } \u8fd0\u884c\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 Enter a name for your creature: Marvin Marvin is at (4, 7) Enter new X location for creature (-1 to quit): 6 Enter new Y location for creature (-1 to quit): 12 Marvin is at (6, 12) Enter new X location for creature (-1 to quit): 3 Enter new Y location for creature (-1 to quit): 2 Marvin is at (3, 2) Enter new X location for creature (-1 to quit): -1 \u4e0d\u540c\u7c7b\u578b\u7684\u7ec4\u5408 \u5c3d\u7ba1\u5927\u591a\u6570\u7ec4\u5408\u4f1a\u5728\u7ec4\u5408\u5173\u7cfb\u5efa\u7acb\u65f6\uff0c\u76f4\u63a5\u521b\u5efa\u5b83\u4eec\u7684\u90e8\u5206\uff0c\u5e76\u5728\u7ec4\u5408\u88ab\u7834\u574f\u65f6\u76f4\u63a5\u9500\u6bc1\u5b83\u4eec\u7684\u90e8\u5206\uff0c\u4f46\u4e5f\u6709\u4e00\u4e9b\u4e0d\u540c\u7c7b\u578b\u7684\u7ec4\u5408\u4e0d\u6ee1\u8db3\u8fd9\u4e00\u89c4\u5219\u3002 \u4f8b\u5982\uff1a \u7ec4\u5408\u4e5f\u53ef\u4ee5\u5148\u4e0d\u521b\u5efa\u90e8\u5206\uff0c\u800c\u662f\u5728\u9700\u8981\u4f7f\u7528\u5b83\u4eec\u65f6\u5728\u521b\u5efa\u3002\u4f8b\u5982\uff0c\u5b57\u7b26\u4e32\u7c7b\u53ef\u4ee5\u5148\u4e0d\u52a8\u6001\u5206\u914d\u5b57\u7b26\u6570\u7ec4\uff0c\u5f53\u771f\u6b63\u6709\u6570\u636e\u8981\u5b58\u653e\u65f6\u518d\u521b\u5efa\uff1b \u7ec4\u5408\u4f7f\u7528\u7684\u90e8\u5206\u53ef\u4ee5\u662f\u88ab\u4f20\u5165\u7684\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u5b83\u81ea\u5df1\u521b\u5efa\u7684\uff1b \u7ec4\u5408\u4e5f\u53ef\u4ee5\u628a\u9500\u6bc1\u90e8\u5206\u7684\u5de5\u4f5c\u59d4\u6d3e\u7ed9\u5176\u4ed6\u5bf9\u8c61\uff08\u4f8b\u5982\uff0c\u5783\u573e\u56de\u6536\u7a0b\u5e8f\uff09\u3002 \u8fd9\u91cc\u7684\u5173\u952e\u70b9\u662f\uff0c\u7ec4\u5408\u5e94\u8be5\u7ba1\u7406\u5b83\u7684\u5404\u4e2a\u90e8\u5206\uff0c\u800c\u4e0d\u9700\u8981\u7ec4\u5408\u7684\u7528\u6237\u7ba1\u7406\u4efb\u4f55\u4e1c\u897f\u3002 \u7ec4\u5408\u548c\u7c7b\u6210\u5458 \u5f53\u6d89\u53ca\u5230\u5bf9\u8c61\u7ec4\u5408\u65f6\uff0c\u65b0\u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u95ee\u7684\u4e00\u4e2a\u95ee\u9898\u662f\uff0c\u201c\u4ec0\u4e48\u65f6\u5019\u6211\u5e94\u8be5\u4f7f\u7528\u7c7b\u6210\u5458\u800c\u4e0d\u662f\u76f4\u63a5\u4f7f\u7528\u67d0\u4e2a\u7279\u6027\uff1f\u201d\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4e0d\u4f7f\u7528 Point2D``\u7c7b\u6765\u5b9e\u73b0Creature \u7684\u4f4d\u7f6e\uff0c\u800c\u662f\u5411 Creature \u7c7b\u4e2d\u76f4\u63a5\u6dfb\u52a02\u4e2a\u6574\u6570\uff0c\u5e76\u7f16\u5199\u4ee3\u7801\u6765\u5904\u7406\u5b9a\u4f4d\u3002\u7136\u800c\uff0c\u8ba9 Point2D \u6210\u4e3a\u72ec\u7acb\u7684\u7c7b(\u5e76\u6210\u4e3a Creature \u7684\u7684\u6210\u5458)\u6709\u5f88\u591a\u597d\u5904\uff1a \u6bcf\u4e2a\u72ec\u7acb\u7684\u7c7b\u80fd\u591f\u5c3d\u53ef\u80fd\u5730\u4fdd\u6301\u7b80\u6d01\uff0c\u4e13\u6ce8\u4e8e\u5b83\u8981\u5b8c\u6210\u7684\u4efb\u52a1\u3002\u8fd9\u4f7f\u5f97\u8fd9\u4e9b\u7c7b\u66f4\u5bb9\u6613\u7f16\u5199\u3001\u4e5f\u66f4\u5bb9\u6613\u7406\u89e3\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ea\u4e13\u6ce8\u4e00\u4e2a\u529f\u80fd\u3002\u4f8b\u5982\uff0c Point2D \u53ea\u9700\u8981\u5173\u6ce8\u548c\u201c\u70b9\u201d\u76f8\u5173\u7684\u529f\u80fd\uff0c\u663e\u7136\u6709\u52a9\u4e8e\u4fdd\u6301\u7c7b\u7684\u7b80\u6d01\u3002 \u6bcf\u4e2a\u7c7b\u90fd\u662f\u81ea\u5305\u542b\u7684\uff0c\u6240\u4ee5\u5b83\u4eec\u80fd\u591f\u88ab\u590d\u7528\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5176\u4ed6\u5e94\u7528\u4e2d\u590d\u7528 Point2D \u8fd9\u4e2a\u7c7b\u3002\u800c\u4e14\uff0c\u5982\u679c creature \u9700\u8981\u53e6\u5916\u4e00\u4e2a\u70b9\uff08\u4f8b\u5982\uff0c\u8868\u793a\u8981\u53bb\u7684\u76ee\u6807\u70b9\uff09\uff0c\u6b64\u65f6\u53ea\u9700\u8981\u5411\u7c7b\u4e2d\u518d\u6dfb\u52a0\u4e00\u4e2a Point2D \u6210\u5458\u53d8\u91cf\u5373\u53ef\uff1b \u5916\u5c42\u7c7b\u53ef\u4ee5\u5229\u7528\u5176\u7c7b\u6210\u5458\u5b8c\u6210\u66f4\u590d\u6742\u7684\u4efb\u52a1\uff0c\u800c\u4e0d\u9700\u8981\u5728\u4e0d\u540c\u7684\u6210\u5458\u95f4\u534f\u8c03\u6570\u636e\u6d41\uff0c\u8fd9\u663e\u7136\u6709\u52a9\u4e8e\u964d\u4f4e\u5916\u5c42\u7c7b\u7684\u6574\u4f53\u590d\u6742\u5ea6\uff0c\u56e0\u4e3a\u5b83\u5c06\u4efb\u52a1\u59d4\u6d3e\u7ed9\u4e86\u5176\u6210\u5458\uff0c\u4e14\u8be5\u6210\u5458\u5df2\u7ecf\u77e5\u9053\u5e94\u8be5\u5982\u4f55\u5b8c\u6210\u76f8\u5173\u7684\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u5728\u9700\u8981\u79fb\u52a8 Creature \u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u5c06\u4efb\u52a1\u59d4\u6d3e\u7ed9 Point \u7c7b\uff0c\u800c\u8be5\u7c7b\u662f\u77e5\u9053\u5982\u4f55\u8bbe\u7f6e\u4e00\u4e2a\u70b9\u7684\u3002\u56e0\u6b64 Creature \u7c7b\u5728\u5b9e\u73b0\u7684\u65f6\u5019\uff0c\u5c31\u65e0\u9700\u64cd\u5fc3\u8fd9\u4e9b\u95ee\u9898\u4e86\u3002 \u5c0f\u8d34\u58eb \u6839\u636e\u7ecf\u9a8c\u6cd5\u5219\uff0c\u4e00\u4e2a\u7c7b\u5e94\u8be5\u88ab\u8bbe\u8ba1\u4e3a\u4ec5\u5b8c\u6210\u4e00\u4e2a\u4efb\u52a1\u3002\u8fd9\u4e2a\u4efb\u52a1\u8981\u4e48\u662f\u5b58\u50a8\u6216\u4fee\u6539\u67d0\u79cd\u6570\u636e\uff08\u4f8b\u5982\uff0c Point2D \u6216 std::string )\uff0c\u8981\u4e48\u662f\u534f\u8c03\u5b83\u7684\u6210\u5458\u5b8c\u6210\u67d0\u4e9b\u4efb\u52a1\u3002\u4f46\u7406\u60f3\u60c5\u51b5\u4e0b\u4e0d\u5e94\u8be5\u540c\u65f6\u505a\u4e0a\u9762\u4e24\u4ef6\u4e8b\u3002 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c Creature \u7684\u786e\u4e0d\u5e94\u8be5\u64cd\u5fc3 Points \u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff0c\u4e5f\u4e0d\u9700\u8981\u5173\u5fc3name\u662f\u5982\u4f55\u50a8\u5b58\u7684\u3002 Creature \u7684\u4efb\u52a1\u4e0d\u662f\u4e86\u89e3\u8fd9\u4e9b\u7ec6\u8282\uff0c\u800c\u662f\u5982\u4f55\u8981\u5173\u6ce8\u5982\u4f55\u534f\u8c03\u6570\u636e\u6d41\u5e76\u786e\u4fdd\u5176\u6bcf\u4e2a\u6210\u5458\u90fd\u77e5\u9053\u5b83\u4eec\u8981\u505a\u4ec0\u4e48\u3002\u800c\u5982\u4f55\u505a\uff0c\u5219\u7531\u6210\u5458\u81ea\u5df1\u6765\u8d1f\u8d23\u3002","title":"16.2 - \u7ec4\u5408\u5173\u7cfb"},{"location":"16-2-composition/#_1","text":"\u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\uff0c\u590d\u6742\u7684\u4e1c\u897f\u5f80\u5f80\u90fd\u57fa\u4e8e\u66f4\u5c0f\u7684\u3001\u66f4\u7b80\u5355\u7684\u4e1c\u897f\u6784\u5efa\u3002\u4f8b\u5982\uff0c\u6c7d\u8f66\u662f\u57fa\u4e8e\u8f66\u67b6\u3001\u5f15\u8d77\u3001\u8f6e\u80ce\u3001\u53d8\u901f\u7bb1\u3001\u65b9\u5411\u76d8\u548c\u5f88\u591a\u5176\u4ed6\u90e8\u4ef6\u6784\u5efa\u7684\u3002\u4e2a\u4eba\u7535\u8111\u5219\u662f\u57fa\u4e8eCPU\u3001\u4e3b\u677f\u3001\u5185\u5b58\u7b49\u6784\u5efa\u7684\u3002\u751a\u81f3\u4f60\u81ea\u5df1\u4e5f\u53ef\u4ee5\u5206\u4e3a\u66f4\u5c0f\u7684\u90e8\u5206\uff1a\u5934\u3001\u8eab\u4f53\u3001\u817f\u3001\u624b\u81c2\u7b49\u7b49\u3002\u901a\u8fc7\u7b80\u5355\u5bf9\u8c61\u6784\u5efa\u590d\u6742\u5bf9\u8c61\u7684\u8fc7\u7a0b\u79f0\u4e3a\u5bf9\u8c61 \u7ec4\u5408 \u3002 \u4e00\u822c\u6765\u8bf4\uff0c\u5bf9\u8c61\u7ec4\u5408\u6a21\u578b\u4e2d\u7684\u5bf9\u8c61\u4e4b\u95f4\u662f\u201c\u6709\u4e00\u4e2a\uff08has-a\uff09\u201d\u7684\u5173\u7cfb\u3002\u6c7d\u8f66\u201c\u6709\u4e00\u4e2a\u201d\u53d8\u901f\u7bb1\u3002\u7535\u8111\u201c\u6709\u4e00\u4e2a\u201dCPU\uff0c\u4f60\u201c\u6709\u4e00\u4e2a\u201d\u5fc3\u810f\u3002\u590d\u6742\u5bf9\u8c61\u901a\u5e38\u88ab\u79f0\u4e3a \u6574\u4f53 \u3002\u800c\u7b80\u5355\u7684\u5bf9\u8c61\u901a\u5e38\u88ab\u79f0\u4e3a\u90e8\u4ef6\u6216\u7ec4\u4ef6\u3002 \u5728C++\u4e2d\uff0c\u4f60\u5df2\u7ecf\u770b\u5230\u7ed3\u6784\u548c\u7c7b\u53ef\u4ee5\u62e5\u6709\u5404\u79cd\u7c7b\u578b\u7684\u6570\u636e\u6210\u5458(\u4f8b\u5982\u57fa\u672c\u7c7b\u578b\u6216\u5176\u4ed6\u7c7b)\u3002\u5f53\u4f7f\u7528\u6570\u636e\u6210\u5458\u6784\u5efa\u7c7b\u65f6\uff0c\u5c31\u662f\u5728\u7528\u66f4\u7b80\u5355\u7684\u90e8\u5206\u6784\u9020\u590d\u6742\u7684\u5bf9\u8c61\uff0c\u8fd9\u5c31\u662f\u5bf9\u8c61\u7ec4\u5408\u3002\u56e0\u6b64\uff0c\u7ed3\u6784\u548c\u7c7b\u6709\u65f6\u88ab\u79f0\u4e3a \u7ec4\u5408\u7c7b\u578b \u3002 \u5bf9\u8c61\u7ec4\u5408\u5728C++\u4e2d\u5f88\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u5c06\u66f4\u7b80\u5355\u3001\u66f4\u5bb9\u6613\u7ba1\u7406\u7684\u90e8\u5206\u6765\u7ec4\u5408\u8d77\u6765\u521b\u5efa\u590d\u6742\u7684\u7c7b\u3002\u8fd9\u964d\u4f4e\u4e86\u590d\u6742\u6027\uff0c\u5e76\u4f7f\u6211\u4eec\u53ef\u4ee5\u66f4\u5feb\u5730\u7f16\u5199\u4ee3\u7801\uff0c\u4e5f\u66f4\u4e0d\u5bb9\u6613\u51fa\u9519\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u91cd\u7528\u5df2\u7ecf\u7f16\u5199\u3001\u6d4b\u8bd5\u548c\u9a8c\u8bc1\u8fc7\u7684\u4ee3\u7801\u3002","title":"\u5bf9\u8c61\u7ec4\u5408"},{"location":"16-2-composition/#_2","text":"\u5bf9\u8c61\u7ec4\u5408\u6709\u4e24\u79cd\u57fa\u672c\u7684\u5b50\u7c7b\u578b\uff1a\u7ec4\u5408\u548c\u805a\u5408\u3002\u8fd9\u8282\u8bfe\u6211\u4eec\u5c06\u8ba8\u8bba\u7ec4\u5408\uff0c\u4e0b\u8282\u8bfe\u6211\u4eec\u5c06\u8ba8\u8bba \u805a\u5408\u5173\u7cfb \u3002 \u5173\u4e8e\u672f\u8bed\u7684\u8bf4\u660e\uff1a\u672f\u8bed\u201c\u7ec4\u5408\u201d\u901a\u5e38\u540c\u65f6\u6307\u7ec4\u5408\u548c\u805a\u5408\uff0c\u800c\u4e0d\u4ec5\u4ec5\u6307\u7ec4\u5408\u5b50\u7c7b\u578b\u3002\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u5f53\u6211\u4eec\u63d0\u5230\u4e24\u8005\u65f6\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u672f\u8bed\u201c\u5bf9\u8c61\u7ec4\u5408\u201d\uff0c\u5f53\u6211\u4eec\u4e13\u95e8\u63d0\u5230\u7ec4\u5408\u5b50\u7c7b\u578b\u65f6\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u672f\u8bed\u201c\u7ec4\u5408\u201d\u3002","title":"\u5bf9\u8c61\u7ec4\u5408\u7684\u7c7b\u578b"},{"location":"16-2-composition/#_3","text":"\u5408\u683c\u7684\u7ec4\u5408\u5173\u7cfb\u9700\u8981\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\uff1a \u90e8\u5206(\u6210\u5458)\u662f\u5bf9\u8c61(\u7c7b)\u7684\u4e00\u90e8\u5206 \u90e8\u4ef6(\u6210\u5458)\u4e00\u6b21\u53ea\u80fd\u5c5e\u4e8e\u4e00\u4e2a\u5bf9\u8c61(\u7c7b) \u90e8\u5206(\u6210\u5458)\u7684\u5b58\u5728\u7531\u5bf9\u8c61(\u7c7b)\u7ba1\u7406 \u90e8\u5206(\u6210\u5458)\u4e0d\u77e5\u9053\u5bf9\u8c61(\u7c7b)\u7684\u5b58\u5728 \u73b0\u5b9e\u751f\u6d3b\u4e2d\u4e00\u4e2a\u5f88\u597d\u7684\u7ec4\u5408\u4f8b\u5b50\u662f\u4e00\u4e2a\u4eba\u7684\u8eab\u4f53\u548c\u5fc3\u810f\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u8ba9\u6211\u4eec\u66f4\u8be6\u7ec6\u5730\u7814\u7a76\u8fd9\u4e9b\u5185\u5bb9\u3002 \u7ec4\u5408\u5173\u7cfb\u662f\u201c\u90e8\u5206-\u6574\u4f53\u201d\u7684\u5173\u7cfb\uff0c\u5176\u4e2d\u90e8\u5206\u5fc5\u987b\u6784\u6210\u6574\u4e2a\u5bf9\u8c61\u7684\u4e00\u90e8\u5206\u3002\u4f8b\u5982\uff0c\u5fc3\u810f\u662f\u4e00\u4e2a\u4eba\u8eab\u4f53\u7684\u4e00\u90e8\u5206\u3002\u7ec4\u5408\u4e2d\u7684\u90e8\u5206\u53ea\u80fd\u662f\u5c5e\u4e8e \u4e00\u4e2a\u5bf9\u8c61 \u7684\u4e00\u90e8\u5206\u3002\u4f5c\u4e3a\u4e00\u4e2a\u4eba\u8eab\u4f53\u4e00\u90e8\u5206\u7684\u5fc3\u810f\u4e0d\u53ef\u80fd\u540c\u65f6\u662f\u53e6\u4e00\u4e2a\u4eba\u8eab\u4f53\u7684\u4e00\u90e8\u5206\u3002 \u5728\u7ec4\u5408\u5173\u7cfb\u4e2d\uff0c\u5bf9\u8c61\u8d1f\u8d23\u7ba1\u7406\u5404\u4e2a\u90e8\u4ef6\u7684\u5b58\u5728\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u8fd9\u610f\u5473\u7740\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u521b\u5efa\u8be5\u90e8\u4ef6\uff0c\u5728\u9500\u6bc1\u5bf9\u8c61\u65f6\u9500\u6bc1\u8be5\u90e8\u5206\u3002\u4f46\u66f4\u5e7f\u6cdb\u5730\u8bf4\uff0c\u5b83\u610f\u5473\u7740\u5bf9\u8c61\u4ee5\u4e00\u79cd\u4e0d\u9700\u8981\u5bf9\u8c61\u7684\u7528\u6237\u53c2\u4e0e\u7684\u65b9\u5f0f\u7ba1\u7406\u90e8\u4ef6\u7684\u751f\u547d\u5468\u671f\u3002\u4f8b\u5982\uff0c\u5f53\u4e00\u4e2a\u8eab\u4f53\u88ab\u521b\u9020\u51fa\u6765\u65f6\uff0c\u5fc3\u810f\u4e5f\u88ab\u521b\u9020\u51fa\u6765\u4e86\u3002\u5f53\u4e00\u4e2a\u4eba\u7684\u8eab\u4f53\u88ab\u6467\u6bc1\u65f6\uff0c\u4ed6\u7684\u5fc3\u4e5f\u4f1a\u88ab\u6467\u6bc1\u3002\u6b63\u56e0\u4e3a\u5982\u6b64\uff0c\u7ec4\u5408\u6709\u65f6\u88ab\u79f0\u4e3a\u201c\u6b7b\u4ea1\u5173\u7cfb\u201d\u3002 \u6700\u540e\uff0c\u90e8\u5206\u4e0d\u77e5\u9053\u6574\u4f53\u7684\u5b58\u5728\u3002\u4f60\u7684\u5fc3\u5728\u5e78\u798f\u5730\u8fd0\u8f6c\u7740\uff0c\u5374\u6ca1\u6709\u610f\u8bc6\u5230\u5b83\u662f\u4e00\u4e2a\u66f4\u5927\u7ed3\u6784\u7684\u4e00\u90e8\u5206\u3002\u6211\u4eec\u79f0\u4e4b\u4e3a\u5355\u5411\u5173\u7cfb\uff0c\u56e0\u4e3a\u8eab\u4f53\u4e86\u89e3\u5fc3\u810f\uff0c\u4f46\u5fc3\u810f\u4e0d\u4e86\u89e3\u8eab\u4f53\u3002 \u6ce8\u610f\uff0c\u7ec4\u5408\u5173\u7cfb\u4e0e\u90e8\u4ef6\u7684\u53ef\u79fb\u690d\u6027\u65e0\u5173\u3002\u5fc3\u810f\u53ef\u4ee5\u4ece\u4e00\u4e2a\u8eab\u4f53\u79fb\u690d\u5230\u53e6\u4e00\u4e2a\u8eab\u4e0a\u3002\u7136\u800c\uff0c\u5373\u4f7f\u5728\u79fb\u690d\u540e\uff0c\u5b83\u4ecd\u7136\u6ee1\u8db3\u7ec4\u5408\u7684\u5b9a\u4e49(\u5fc3\u810f\u73b0\u5728\u5c5e\u4e8e\u63a5\u53d7\u8005\uff0c\u9664\u975e\u518d\u6b21\u79fb\u690d\uff0c\u5426\u5219\u53ea\u80fd\u662f\u63a5\u53d7\u8005\u5bf9\u8c61\u7684\u4e00\u90e8\u5206)\u3002 \u6211\u4eec\u7684 Fraction \u7c7b\u5c31\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u7ec4\u5408\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 class Fraction { private : int m_numerator ; int m_denominator ; public : Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { } }; \u8fd9\u4e2a\u7c7b\u6709\u4e24\u4e2a\u6570\u636e\u6210\u5458\uff1a\u5206\u5b50\u548c\u5206\u6bcd\u3002\u5206\u5b50\u548c\u5206\u6bcd\u662f\u5206\u6570\u7684\u4e00\u90e8\u5206(\u5305\u542b\u5728\u5206\u6570\u4e2d)\u3002\u5b83\u4eec\u4e0d\u80fd\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u5206\u6570\u3002\u5206\u5b50\u548c\u5206\u6bcd\u5e76\u4e0d\u77e5\u9053\u5b83\u4eec\u662f\u5206\u6570\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u4eec\u53ea\u5305\u542b\u6574\u6570\u3002\u5728\u521b\u5efa Fraction \u5b9e\u4f8b\u65f6\uff0c\u5c06\u521b\u5efa\u5206\u5b50\u548c\u5206\u6bcd\u3002\u5f53\u5206\u6570\u5b9e\u4f8b\u88ab\u7834\u574f\u65f6\uff0c\u5206\u5b50\u548c\u5206\u6bcd\u4e5f\u4f1a\u88ab\u7834\u574f\u3002 \u5c3d\u7ba1\u5bf9\u8c61\u7ec4\u5408\u5173\u7cfb\u63cf\u8ff0\u7684\u662f\u201c\u5177\u6709\u201d\u7684\u5173\u7cfb\uff08\u4eba\u4f53\u6709\u4e00\u4e2a\u5fc3\u810f\u3001\u5206\u6570\u6709\u4e00\u4e2a\u5206\u6bcd\uff09\uff0c\u4f46\u662f\u66f4\u52a0\u7cbe\u786e\u7684\u8bf4\u6cd5\u5176\u5b9e\u662f\uff0c\u7ec4\u5408\u63cf\u8ff0\u7684\u662f\u201c\u90e8\u5206\u4e0e\u6574\u4f53\u201d\u7684\u5173\u7cfb\uff08\u5fc3\u810f\u662f\u4eba\u4f53\u7684\u4e00\u90e8\u5206\u3001\u5206\u5b50\u662f\u5206\u6570\u7684\u4e00\u90e8\u5206\uff09\u3002\u7ec4\u5408\u5173\u7cfb\u901a\u5e38\u7528\u4e8e\u5bf9\u4e00\u4e2a\u7269\u7406\u5173\u7cfb\u8fdb\u884c\u5efa\u6a21\uff0c\u5373\u4e00\u4e2a\u5bf9\u8c61\u88ab\u5305\u542b\u5728\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61\u4e2d\u3002 \u201c\u90e8\u5206\u201d\u5173\u7cfb\u53ef\u4ee5\u662f\u201c\u4e00\u5bf9\u4e00\u7684\u201d\uff0c\u4e5f\u53ef\u4ee5\u662f\u201c\u591a\u5bf9\u4e00\u7684\u201d\u2014\u2014\u4f8b\u5982\uff0c\u8eab\u4f53\u4e2d\u5305\u542b\u4e00\u4e2a\u5fc3\u810f\uff0c\u4f46\u662f\u8eab\u4f53\u53ef\u4ee5\u5305\u542b10\u4e2a\u624b\u6307\uff08\u6700\u7ec8\u88ab\u5b9e\u73b0\u4e3a\u6570\u7ec4\uff09\u3002","title":"\u7ec4\u5408"},{"location":"16-2-composition/#_4","text":"\u7ec4\u5408\u662fC++\u4e2d\u6700\u5bb9\u6613\u5b9e\u73b0\u7684\u5173\u7cfb\u7c7b\u578b\u4e4b\u4e00\u3002\u5b83\u4eec\u901a\u5e38\u88ab\u521b\u5efa\u4e3a\u5177\u6709\u666e\u901a\u6570\u636e\u6210\u5458\u7684\u7ed3\u6784\u6216\u7c7b\u3002\u56e0\u4e3a\u8fd9\u4e9b\u6570\u636e\u6210\u5458\u76f4\u63a5\u4f5c\u4e3a\u7ed3\u6784/\u7c7b\u7684\u4e00\u90e8\u5206\u5b58\u5728\uff0c\u6240\u4ee5\u5b83\u4eec\u7684\u751f\u547d\u5468\u671f\u4e0e\u7c7b\u5b9e\u4f8b\u672c\u8eab\u7684\u751f\u547d\u5468\u671f\u7ed1\u5b9a\u5728\u4e00\u8d77\u3002 \u9700\u8981\u8fdb\u884c\u52a8\u6001\u5206\u914d\u6216\u56de\u6536\u7684\u7ec4\u5408\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\u6570\u636e\u6210\u5458\u5b9e\u73b0\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7ec4\u5408\u7c7b\u5e94\u8be5\u81ea\u5df1\u8d1f\u8d23\u6240\u6709\u5fc5\u8981\u7684\u5185\u5b58\u7ba1\u7406(\u800c\u4e0d\u662f\u7c7b\u7684\u7528\u6237)\u3002 \u4e00\u822c\u6765\u8bf4\uff0c\u5982\u679c\u4f60\u80fd\u4f7f\u7528\u590d\u5408\u8bbe\u8ba1\u4e00\u4e2a\u7c7b\uff0c\u4f60\u5c31\u5e94\u8be5\u4f7f\u7528\u7ec4\u5408\u5173\u7cfb\u8bbe\u8ba1\u4e00\u4e2a\u7c7b\u3002\u4f7f\u7528\u7ec4\u5408\u8bbe\u8ba1\u7684\u7c7b\u662f\u76f4\u63a5\u7684\u3001\u7075\u6d3b\u7684\u548c\u5065\u58ee\u7684(\u56e0\u4e3a\u5b83\u4eec\u5f88\u597d\u5730\u81ea\u884c\u6e05\u7406)\u3002","title":"\u5b9e\u73b0\u7ec4\u5408\u5173\u7cfb"},{"location":"16-2-composition/#_5","text":"\u8bb8\u591a\u6e38\u620f\u90fd\u6709\u5728\u68cb\u76d8\u3001\u5730\u56fe\u6216\u5c4f\u5e55\u4e0a\u79fb\u52a8\u7684\u751f\u7269\u6216\u7269\u4f53\u3002\u6240\u6709\u8fd9\u4e9b\u751f\u7269/\u7269\u4f53\u7684\u4e00\u4e2a\u5171\u540c\u70b9\u662f\uff0c\u5b83\u4eec\u90fd\u6709\u4e00\u4e2a\u4f4d\u7f6e\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u751f\u7269\u7c7b\uff0c\u5b83\u4f7f\u7528\u4e00\u4e2a Point \u7c7b\u6765\u4fdd\u5b58\u751f\u7269\u7684\u4f4d\u7f6e\u3002 \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u8bbe\u8ba1 Point \u7c7b\u3002\u751f\u7269\u5c06\u751f\u6d3b\u57282d\u4e16\u754c\u4e2d\uff0c\u6240\u4ee5 Point \u7c7b\u53ea\u9700\u8981\u6709\u4e24\u4e2a\u7ef4\u5ea6 x \u548c y \u3002\u6211\u4eec\u5047\u8bbe\u4e16\u754c\u662f\u7531\u79bb\u6563\u7684\u6b63\u65b9\u5f62\u7ec4\u6210\uff0c\u6240\u4ee5\u8fd9\u4e9b\u7ef4\u5ea6\u603b\u662f\u6574\u6570\u3002 Point2D.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #ifndef POINT2D_H #define POINT2D_H #include class Point2D { private : int m_x ; int m_y ; public : // \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 Point2D () : m_x { 0 }, m_y { 0 } { } // \u6709\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570 Point2D ( int x , int y ) : m_x { x }, m_y { y } { } // \u91cd\u8f7d\u7684\u8f93\u51fa\u64cd\u4f5c\u7b26 friend std :: ostream & operator << ( std :: ostream & out , const Point2D & point ) { out << '(' << point . m_x << \", \" << point . m_y << ')' ; return out ; } // \u6210\u5458\u8bbf\u95ee\u51fd\u6570 void setPoint ( int x , int y ) { m_x = x ; m_y = y ; } }; #endif \u6ce8\u610f\uff0c\u56e0\u4e3a\u6240\u6709\u51fd\u6570\u7684\u5b9e\u73b0\u90fd\u5728\u5934\u6587\u4ef6\u4e2d\uff08\u4e3a\u4e86\u4fdd\u6301\u4f8b\u5b50\u7b80\u77ed\uff09\uff0c\u6240\u4ee5\u5e76\u6ca1\u6709 Point2D.cpp \u6587\u4ef6\u3002 Point2d \u662f\u5b83\u7684\u90e8\u4ef6\u7684\u7684\u7ec4\u5408\uff1a\u4f4d\u7f6e\u4fe1\u606fx\u548cy\u90fd\u662f Point2D \u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u5b83\u4eec\u7684\u751f\u547d\u5468\u671f\u4e5f\u548c Point2D \u5b9e\u4f8b\u7ed1\u5b9a\u5728\u4e00\u8d77\u3002 \u73b0\u5728\uff0c\u5b9a\u4e49 Creature \u7c7b\u3002 Creature \u6709\u5982\u4e0b\u51e0\u4e2a\u5c5e\u6027\uff1a\u540d\u79f0\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4f4d\u7f6e\u5219\u662f\u4e00\u4e2a Point2D \u7c7b\u3002 Creature.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #ifndef CREATURE_H #define CREATURE_H #include #include #include \"Point2D.h\" class Creature { private : std :: string m_name ; //\u540d\u79f0 Point2D m_location ; //\u4f4d\u7f6e public : Creature ( const std :: string & name , const Point2D & location ) : m_name { name }, m_location { location } { } friend std :: ostream & operator << ( std :: ostream & out , const Creature & creature ) { out << creature . m_name << \" is at \" << creature . m_location ; return out ; } void moveTo ( int x , int y ) { m_location . setPoint ( x , y ); } }; #endif Creature \u540c\u6837\u4e5f\u662f\u5176\u5404\u90e8\u5206\u7684\u7ec4\u5408\u3002\u751f\u7269\u7684\u540d\u5b57\u548c\u4f4d\u7f6e\u5176\u58f0\u660e\u5468\u671f\u90fd\u7ed1\u5b9a\u5230\u4e86 Creature \u3002 \u6700\u7ec8\u7684\u7a0b\u5e8f\u662f\u8fd9\u6837\u7684\u3002 main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include #include #include \"Creature.h\" #include \"Point2D.h\" int main () { std :: cout << \"Enter a name for your creature: \" ; std :: string name ; std :: cin >> name ; Creature creature { name , { 4 , 7 } }; while ( true ) { // print the creature's name and location std :: cout << creature << '\\n' ; std :: cout << \"Enter new X location for creature (-1 to quit): \" ; int x { 0 }; std :: cin >> x ; if ( x == -1 ) break ; std :: cout << \"Enter new Y location for creature (-1 to quit): \" ; int y { 0 }; std :: cin >> y ; if ( y == -1 ) break ; creature . moveTo ( x , y ); } return 0 ; } \u8fd0\u884c\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 Enter a name for your creature: Marvin Marvin is at (4, 7) Enter new X location for creature (-1 to quit): 6 Enter new Y location for creature (-1 to quit): 12 Marvin is at (6, 12) Enter new X location for creature (-1 to quit): 3 Enter new Y location for creature (-1 to quit): 2 Marvin is at (3, 2) Enter new X location for creature (-1 to quit): -1","title":"\u66f4\u591a\u7684\u4f8b\u5b50"},{"location":"16-2-composition/#_6","text":"\u5c3d\u7ba1\u5927\u591a\u6570\u7ec4\u5408\u4f1a\u5728\u7ec4\u5408\u5173\u7cfb\u5efa\u7acb\u65f6\uff0c\u76f4\u63a5\u521b\u5efa\u5b83\u4eec\u7684\u90e8\u5206\uff0c\u5e76\u5728\u7ec4\u5408\u88ab\u7834\u574f\u65f6\u76f4\u63a5\u9500\u6bc1\u5b83\u4eec\u7684\u90e8\u5206\uff0c\u4f46\u4e5f\u6709\u4e00\u4e9b\u4e0d\u540c\u7c7b\u578b\u7684\u7ec4\u5408\u4e0d\u6ee1\u8db3\u8fd9\u4e00\u89c4\u5219\u3002 \u4f8b\u5982\uff1a \u7ec4\u5408\u4e5f\u53ef\u4ee5\u5148\u4e0d\u521b\u5efa\u90e8\u5206\uff0c\u800c\u662f\u5728\u9700\u8981\u4f7f\u7528\u5b83\u4eec\u65f6\u5728\u521b\u5efa\u3002\u4f8b\u5982\uff0c\u5b57\u7b26\u4e32\u7c7b\u53ef\u4ee5\u5148\u4e0d\u52a8\u6001\u5206\u914d\u5b57\u7b26\u6570\u7ec4\uff0c\u5f53\u771f\u6b63\u6709\u6570\u636e\u8981\u5b58\u653e\u65f6\u518d\u521b\u5efa\uff1b \u7ec4\u5408\u4f7f\u7528\u7684\u90e8\u5206\u53ef\u4ee5\u662f\u88ab\u4f20\u5165\u7684\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u5b83\u81ea\u5df1\u521b\u5efa\u7684\uff1b \u7ec4\u5408\u4e5f\u53ef\u4ee5\u628a\u9500\u6bc1\u90e8\u5206\u7684\u5de5\u4f5c\u59d4\u6d3e\u7ed9\u5176\u4ed6\u5bf9\u8c61\uff08\u4f8b\u5982\uff0c\u5783\u573e\u56de\u6536\u7a0b\u5e8f\uff09\u3002 \u8fd9\u91cc\u7684\u5173\u952e\u70b9\u662f\uff0c\u7ec4\u5408\u5e94\u8be5\u7ba1\u7406\u5b83\u7684\u5404\u4e2a\u90e8\u5206\uff0c\u800c\u4e0d\u9700\u8981\u7ec4\u5408\u7684\u7528\u6237\u7ba1\u7406\u4efb\u4f55\u4e1c\u897f\u3002","title":"\u4e0d\u540c\u7c7b\u578b\u7684\u7ec4\u5408"},{"location":"16-2-composition/#_7","text":"\u5f53\u6d89\u53ca\u5230\u5bf9\u8c61\u7ec4\u5408\u65f6\uff0c\u65b0\u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u95ee\u7684\u4e00\u4e2a\u95ee\u9898\u662f\uff0c\u201c\u4ec0\u4e48\u65f6\u5019\u6211\u5e94\u8be5\u4f7f\u7528\u7c7b\u6210\u5458\u800c\u4e0d\u662f\u76f4\u63a5\u4f7f\u7528\u67d0\u4e2a\u7279\u6027\uff1f\u201d\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4e0d\u4f7f\u7528 Point2D``\u7c7b\u6765\u5b9e\u73b0Creature \u7684\u4f4d\u7f6e\uff0c\u800c\u662f\u5411 Creature \u7c7b\u4e2d\u76f4\u63a5\u6dfb\u52a02\u4e2a\u6574\u6570\uff0c\u5e76\u7f16\u5199\u4ee3\u7801\u6765\u5904\u7406\u5b9a\u4f4d\u3002\u7136\u800c\uff0c\u8ba9 Point2D \u6210\u4e3a\u72ec\u7acb\u7684\u7c7b(\u5e76\u6210\u4e3a Creature \u7684\u7684\u6210\u5458)\u6709\u5f88\u591a\u597d\u5904\uff1a \u6bcf\u4e2a\u72ec\u7acb\u7684\u7c7b\u80fd\u591f\u5c3d\u53ef\u80fd\u5730\u4fdd\u6301\u7b80\u6d01\uff0c\u4e13\u6ce8\u4e8e\u5b83\u8981\u5b8c\u6210\u7684\u4efb\u52a1\u3002\u8fd9\u4f7f\u5f97\u8fd9\u4e9b\u7c7b\u66f4\u5bb9\u6613\u7f16\u5199\u3001\u4e5f\u66f4\u5bb9\u6613\u7406\u89e3\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ea\u4e13\u6ce8\u4e00\u4e2a\u529f\u80fd\u3002\u4f8b\u5982\uff0c Point2D \u53ea\u9700\u8981\u5173\u6ce8\u548c\u201c\u70b9\u201d\u76f8\u5173\u7684\u529f\u80fd\uff0c\u663e\u7136\u6709\u52a9\u4e8e\u4fdd\u6301\u7c7b\u7684\u7b80\u6d01\u3002 \u6bcf\u4e2a\u7c7b\u90fd\u662f\u81ea\u5305\u542b\u7684\uff0c\u6240\u4ee5\u5b83\u4eec\u80fd\u591f\u88ab\u590d\u7528\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5176\u4ed6\u5e94\u7528\u4e2d\u590d\u7528 Point2D \u8fd9\u4e2a\u7c7b\u3002\u800c\u4e14\uff0c\u5982\u679c creature \u9700\u8981\u53e6\u5916\u4e00\u4e2a\u70b9\uff08\u4f8b\u5982\uff0c\u8868\u793a\u8981\u53bb\u7684\u76ee\u6807\u70b9\uff09\uff0c\u6b64\u65f6\u53ea\u9700\u8981\u5411\u7c7b\u4e2d\u518d\u6dfb\u52a0\u4e00\u4e2a Point2D \u6210\u5458\u53d8\u91cf\u5373\u53ef\uff1b \u5916\u5c42\u7c7b\u53ef\u4ee5\u5229\u7528\u5176\u7c7b\u6210\u5458\u5b8c\u6210\u66f4\u590d\u6742\u7684\u4efb\u52a1\uff0c\u800c\u4e0d\u9700\u8981\u5728\u4e0d\u540c\u7684\u6210\u5458\u95f4\u534f\u8c03\u6570\u636e\u6d41\uff0c\u8fd9\u663e\u7136\u6709\u52a9\u4e8e\u964d\u4f4e\u5916\u5c42\u7c7b\u7684\u6574\u4f53\u590d\u6742\u5ea6\uff0c\u56e0\u4e3a\u5b83\u5c06\u4efb\u52a1\u59d4\u6d3e\u7ed9\u4e86\u5176\u6210\u5458\uff0c\u4e14\u8be5\u6210\u5458\u5df2\u7ecf\u77e5\u9053\u5e94\u8be5\u5982\u4f55\u5b8c\u6210\u76f8\u5173\u7684\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u5728\u9700\u8981\u79fb\u52a8 Creature \u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u5c06\u4efb\u52a1\u59d4\u6d3e\u7ed9 Point \u7c7b\uff0c\u800c\u8be5\u7c7b\u662f\u77e5\u9053\u5982\u4f55\u8bbe\u7f6e\u4e00\u4e2a\u70b9\u7684\u3002\u56e0\u6b64 Creature \u7c7b\u5728\u5b9e\u73b0\u7684\u65f6\u5019\uff0c\u5c31\u65e0\u9700\u64cd\u5fc3\u8fd9\u4e9b\u95ee\u9898\u4e86\u3002 \u5c0f\u8d34\u58eb \u6839\u636e\u7ecf\u9a8c\u6cd5\u5219\uff0c\u4e00\u4e2a\u7c7b\u5e94\u8be5\u88ab\u8bbe\u8ba1\u4e3a\u4ec5\u5b8c\u6210\u4e00\u4e2a\u4efb\u52a1\u3002\u8fd9\u4e2a\u4efb\u52a1\u8981\u4e48\u662f\u5b58\u50a8\u6216\u4fee\u6539\u67d0\u79cd\u6570\u636e\uff08\u4f8b\u5982\uff0c Point2D \u6216 std::string )\uff0c\u8981\u4e48\u662f\u534f\u8c03\u5b83\u7684\u6210\u5458\u5b8c\u6210\u67d0\u4e9b\u4efb\u52a1\u3002\u4f46\u7406\u60f3\u60c5\u51b5\u4e0b\u4e0d\u5e94\u8be5\u540c\u65f6\u505a\u4e0a\u9762\u4e24\u4ef6\u4e8b\u3002 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c Creature \u7684\u786e\u4e0d\u5e94\u8be5\u64cd\u5fc3 Points \u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff0c\u4e5f\u4e0d\u9700\u8981\u5173\u5fc3name\u662f\u5982\u4f55\u50a8\u5b58\u7684\u3002 Creature \u7684\u4efb\u52a1\u4e0d\u662f\u4e86\u89e3\u8fd9\u4e9b\u7ec6\u8282\uff0c\u800c\u662f\u5982\u4f55\u8981\u5173\u6ce8\u5982\u4f55\u534f\u8c03\u6570\u636e\u6d41\u5e76\u786e\u4fdd\u5176\u6bcf\u4e2a\u6210\u5458\u90fd\u77e5\u9053\u5b83\u4eec\u8981\u505a\u4ec0\u4e48\u3002\u800c\u5982\u4f55\u505a\uff0c\u5219\u7531\u6210\u5458\u81ea\u5df1\u6765\u8d1f\u8d23\u3002","title":"\u7ec4\u5408\u548c\u7c7b\u6210\u5458"},{"location":"16-3-aggregation/","tags":["object","aggregation"],"text":"Key Takeaway \u805a\u5408\u5173\u7cfb\u4e2d\u90e8\u5206\u53ef\u4ee5\u5c5e\u4e8e\u591a\u4e2a\u6574\u4f53\uff0c\u5176\u751f\u547d\u5468\u671f\u4e5f\u72ec\u7acb\u4e8e\u6574\u4f53 \u5b9e\u73b0\u805a\u5408\u5173\u7cfb\u662f\u57fa\u4e8e\u6307\u9488\u548c\u5f15\u7528\u7684\uff0c\u5176\u4ed6\u5730\u65b9\u90fd\u548c\u7ec4\u5408\u5173\u7cfb\u4e00\u6837 \u5728 16.2 - \u7ec4\u5408\u5173\u7cfb \u4e2d\u6211\u4eec\u6307\u51fa\uff0c\u5bf9\u8c61\u7ec4\u5408\u662f\u57fa\u4e8e\u7b80\u5355\u5bf9\u8c61\u521b\u5efa\u590d\u6742\u5bf9\u8c61\u7684\u4e00\u79cd\u65b9\u6cd5\u3002\u540c\u65f6\uff0c\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86\u4e00\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\u7ec4\u5408\u2014\u2014\u7ec4\u5408\u3002\u5728\u7ec4\u5408\u5173\u7cfb\u4e2d\uff0c\u6574\u4f53\u5bf9\u8c61\u8981\u8d1f\u8d23\u7ba1\u7406\u90e8\u5206\u5bf9\u8c61\u7684\u5b58\u5728\u6027\u3002 \u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ecb\u7ecd\u53e6\u5916\u4e00\u79cd\u5bf9\u8c61\u7ec4\u5408\u2014\u2014\u805a\u5408\u3002 \u805a\u5408 \u805a\u5408\u5173\u7cfb\u8981\u6ee1\u8db3\u5982\u4e0b\u51e0\u4e2a\u6761\u4ef6\uff1a \u90e8\u5206\uff08\u6210\u5458\uff09\u662f\u6574\u4f53\u5bf9\u8c61\u7684\u4e00\u90e8\u5206\uff1b \u90e8\u5206\uff08\u6210\u5458\uff09\u53ef\u4ee5\u4e0d\u53ea\u5c5e\u4e8e\u4e00\u4e2a\u6574\u4f53\u5bf9\u8c61\uff1b \u90e8\u5206\uff08\u6210\u5458\uff09\u7684\u751f\u547d\u5468\u671f\u4e0d\u7531\u6574\u4f53\u5bf9\u8c61\u7ba1\u7406\uff1b \u90e8\u5206\uff08\u6210\u5458\uff09\u4e0d\u9700\u8981\u77e5\u9053\u6574\u4f53\u5bf9\u8c61\u7684\u5b58\u5728\u6027\uff1b \u548c\u7ec4\u5408\u7c7b\u4f3c\uff0c\u805a\u5408\u4e5f\u662f\u4e00\u79cd\u201d\u90e8\u5206\u4e0e\u6574\u4f53\u201c\u7684\u5173\u7cfb\uff0c\u90e8\u5206\u88ab\u5305\u542b\u5728\u6574\u4f53\u4e2d\uff0c\u800c\u4e14\u8fd9\u662f\u4e00\u79cd\u5355\u5411\u7684\u5173\u7cfb\u3002\u4f46\u662f\uff0c\u548c\u7ec4\u5408\u4e0d\u540c\uff0c\u805a\u5408\u5bf9\u8c61\u53ef\u4ee5\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u6574\u4f53\uff0c\u800c\u4e14\u6574\u4f53\u5bf9\u8c61\u4e0d\u9700\u8981\u8d1f\u8d23\u7ba1\u7406\u90e8\u5206\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u3002\u5f53\u805a\u5408\u5173\u7cfb\u88ab\u521b\u5efa\u65f6\uff0c\u6574\u4f53\u4e0d\u8d1f\u8d23\u521b\u5efa\u90e8\u5206\u3002\u5f53\u805a\u5408\u5173\u7cfb\u88ab\u7834\u574f\u65f6\uff0c\u6574\u4f53\u4e5f\u4e0d\u8d1f\u8d23\u9500\u6bc1\u90e8\u5206\u3002 \u4e3e\u4f8b\u6765\u8bf4\uff0c\u8003\u8651\u67d0\u4e2a\u4eba\u548c\u5b83\u4f4f\u5740\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u4e00\u4e2a\u4eba\u6709\u4e00\u4e2a\u4f4f\u5740\u3002\u4f46\u662f\u4f4f\u5740\u53ef\u4ee5\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u4eba\uff0c\u4f8b\u5982\u4f60\u548c\u4f60\u7684\u5ba4\u53cb\uff0c\u6216\u8005\u662f\u5176\u4ed6\u4eba\u3002\u800c\u4e14\uff0c\u4f4f\u5740\u4e5f\u4e0d\u662f\u88ab\u4eba\u7ba1\u7406\u7684\u2014\u2014\u5176\u5730\u5740\u751a\u81f3\u65e9\u4e8e\u5c45\u4f4f\u4eba\u5c31\u5b58\u5728\u4e86\uff0c\u800c\u4e14\u4eba\u4e0d\u5728\u7684\u65f6\u5019\u5730\u5740\u4ecd\u7136\u4f1a\u7ee7\u7eed\u5b58\u5728\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4e0a\u9762\u63cf\u8ff0\u7684\u662f\u4e00\u79cd\u805a\u5408\u5173\u7cfb\u3002 \u53e6\u5916\uff0c\u8003\u8651\u6c7d\u8f66\u548c\u5f15\u64ce\u7684\u5173\u7cfb\u3002\u6c7d\u8f66\u7684\u5f15\u64ce\u5c5e\u4e8e\u6c7d\u8f66\u7684\u4e00\u90e8\u5206\u3002\u4f46\u662f\u5c3d\u7ba1\u5f15\u64ce\u662f\u6c7d\u8f66\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u4e5f\u53ef\u4ee5\u5c5e\u4e8e\u5176\u4ed6\u4eba\u6216\u7269\u3002\u4f8b\u5982\uff0c\u5f15\u64ce\u4e5f\u5c5e\u4e8e\u8f66\u4e3b\u3002\u6c7d\u8f66\u672c\u8eab\u5e76\u4e0d\u8d1f\u8d23\u521b\u5efa\u6216\u9500\u6bc1\u5f15\u64ce\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u5c3d\u7ba1\u8f66\u8f86\u77e5\u9053\u5b83\u4f1a\u6709\u4e00\u4e2a\u5f15\u64ce\uff0c\u4f46\u662f\u5f15\u64ce\u5374\u4e0d\u77e5\u9053\u5b83\u662f\u6c7d\u8f66\u7684\u4e00\u90e8\u5206\u3002 \u5f53\u6211\u4eec\u8ba8\u8bba\u5bf9\u7269\u7406\u5bf9\u8c61\u5efa\u6a21\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u201d\u9500\u6bc1\u201c\u4e00\u8bcd\u4f1a\u6bd4\u8f83\u5947\u602a\u3002\u6b64\u65f6\u6709\u7684\u4eba\u53ef\u80fd\u4f1a\u62ac\u6760\u8bf4\uff0c\u5982\u679c\u4e00\u4e2a\u9668\u77f3\u649e\u51fb\u5730\u7403\u6b63\u597d\u7838\u5230\u4e86\u4f60\u7684\u8f66\uff0c\u90a3\u5c82\u4e0d\u662f\u8f66\u548c\u5f15\u64ce\u4e00\u8d77\u88ab\u9500\u6bc1\u4e86\uff1f\u5f53\u7136\uff0c\u4e0d\u8fc7\u8fd9\u662f\u9668\u77f3\u7684\u9519\u3002\u8fd9\u91cc\u7684\u5173\u952e\u70b9\u5728\u4e8e\uff0c\u6c7d\u8f66\u4e0d\u8d1f\u8d23\u9500\u6bc1\u5176\u90e8\u4ef6\uff08\u5916\u529b\u662f\u53ef\u80fd\u7684\uff09\u3002 \u6211\u4eec\u53ef\u4ee5\u8bf4\uff0c\u805a\u5408\u6a21\u578b\u662f\u4e00\u79cd\u201d\u6709\u4e00\u4e2a\u201c\u7684\u5173\u7cfb\uff08\u9662\u7cfb\u6709\u4e00\u4e2a\u8001\u5e08\u3001\u6c7d\u8f66\u6709\u4e00\u4e2a\u5f15\u64ce\uff09\u3002 \u7c7b\u4f3c\u7ec4\u5408\u5173\u7cfb\uff0c\u805a\u5408\u4e2d\u7684\u90e8\u5206\u53ef\u4ee5\u662f\u4e00\u4e2a\u4e5f\u53ef\u4ee5\u662f\u591a\u4e2a\u3002 \u5b9e\u73b0\u805a\u5408\u5173\u7cfb \u56e0\u4e3a\u805a\u5408\u7c7b\u4f3c\u4e8e\u7ec4\u5408\uff0c\u5b83\u4eec\u90fd\u662f\u90e8\u5206-\u6574\u4f53\u5173\u7cfb\uff0c\u6240\u4ee5\u5b83\u4eec\u7684\u5b9e\u73b0\u51e0\u4e4e\u5b8c\u5168\u76f8\u540c\uff0c\u5b83\u4eec\u4e4b\u95f4\u7684\u533a\u522b\u4e3b\u8981\u662f\u8bed\u4e49\u4e0a\u7684\u3002\u5728\u7ec4\u5408\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u666e\u901a\u6210\u5458\u53d8\u91cf(\u6216\u7531\u7ec4\u5408\u7c7b\u5904\u7406\u5206\u914d\u548c\u56de\u6536\u8fc7\u7a0b\u7684\u6307\u9488)\u5c06\u90e8\u4ef6\u6dfb\u52a0\u5230\u7ec4\u5408\u4e2d\u3002 \u5728\u805a\u5408\u4e2d\uff0c\u6211\u4eec\u4ecd\u7136\u4f7f\u7528\u6210\u5458\u53d8\u91cf\u8868\u793a\u90e8\u5206\u5bf9\u8c61\u3002\u4f46\u662f\uff0c \u8fd9\u4e9b\u6210\u5458\u53d8\u91cf\u901a\u5e38\u662f\u5f15\u7528\u6216\u6307\u9488 \uff0c\u7528\u4e8e\u6307\u5411\u5728\u7c7b\u7684\u4f5c\u7528\u57df\u4e4b\u5916\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u805a\u5408\u901a\u5e38\u8981\u4e48\u63a5\u53d7\u5b83\u5c06\u8981\u6307\u5411\u7684\u5bf9\u8c61\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\u53c2\u6570\uff0c\u8981\u4e48\u4ee5\u7a7a\u5f00\u59cb\uff0c\u7136\u540e\u901a\u8fc7\u8bbf\u95ee\u51fd\u6570\u6216\u64cd\u4f5c\u7b26\u6dfb\u52a0\u5b50\u5bf9\u8c61\u3002 \u56e0\u4e3a\u8fd9\u4e9b\u90e8\u5206\u5bf9\u8c61\u5b58\u5728\u4e8e\u7c7b\u7684\u4f5c\u7528\u57df\u4e4b\u5916\uff0c\u6240\u4ee5\u5f53\u7c7b\u88ab\u9500\u6bc1\u65f6\uff0c\u6307\u9488\u6216\u5f15\u7528\u6210\u5458\u53d8\u91cf\u5c06\u88ab\u9500\u6bc1(\u4f46\u4e0d\u4f1a\u88ab\u5220\u9664)\u3002\u56e0\u6b64\uff0c\u8fd9\u4e9b\u90e8\u5206\u672c\u8eab\u4ecd\u7136\u5b58\u5728\u3002 \u8ba9\u6211\u4eec\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e2a\u201c\u6559\u5e08\u548c\u9662\u7cfb\u201d\u7684\u793a\u4f8b\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u505a\u4e00\u4e9b\u7b80\u5316\uff1a\u9996\u5148\uff0c\u9662\u7cfb\u5c06\u53ea\u5bb9\u7eb3\u4e00\u540d\u6559\u5e08\u3002\u5176\u6b21\uff0c\u8001\u5e08\u4e0d\u77e5\u9053\u4ed6\u4eec\u662f\u54ea\u4e2a\u9662\u7cfb\u7684\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include #include class Teacher { private : std :: string m_name {}; public : Teacher ( const std :: string & name ) : m_name { name } { } const std :: string & getName () const { return m_name ; } }; class Department { private : const Teacher & m_teacher ; // This dept holds only one teacher for simplicity, but it could hold many teachers public : Department ( const Teacher & teacher ) : m_teacher { teacher } { } }; int main () { // Create a teacher outside the scope of the Department Teacher bob { \"Bob\" }; // create a teacher { // Create a department and use the constructor parameter to pass // the teacher to it. Department department { bob }; } // department goes out of scope here and is destroyed // bob still exists here, but the department doesn't std :: cout << bob . getName () << \" still exists! \\n \" ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d bob \u7684\u521b\u5efa\u662f\u72ec\u7acb\u4e8e department \u7684\uff0c\u5b83\u901a\u8fc7\u6784\u9020\u51fd\u6570\u88ab\u4f20\u9012\u7ed9\u4e86 department \u3002\u5f53 department \u9500\u6bc1\u7684\u65f6\u5019\u5f15\u7528 m_teacher \u88ab\u9500\u6bc1\u4e86\uff0c\u4f46\u662f teacher \u672c\u8eab\u5e76\u6ca1\u6709\u88ab\u9500\u6bc1\uff0c\u5b83\u4ecd\u7136\u5b58\u5728\u5e76\u4e14\u5728 main() \u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u3002 \u5bf9\u8c61\u5efa\u6a21\u65f6\u5e94\u5f53\u9009\u62e9\u6b63\u786e\u7684\u5bf9\u8c61\u5173\u7cfb \u5c3d\u7ba1\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6559\u5e08\u4e0d\u77e5\u9053\u4ed6\u4eec\u5728\u4e3a\u54ea\u4e2a\u7cfb\u5de5\u4f5c\uff0c\u8fd9\u542c\u8d77\u6765\u6709\u70b9\u8822\uff0c\u4f46\u5728\u7ed9\u5b9a\u7684\u9879\u76ee\u4e0a\u4e0b\u6587\u4e2d\uff0c\u8fd9\u53ef\u80fd\u5b8c\u5168\u6ca1\u95ee\u9898\u3002\u5f53\u51b3\u5b9a\u5b9e\u73b0\u67d0\u79cd\u7c7b\u578b\u7684\u5173\u7cfb\u65f6\uff0c\u5b9e\u73b0\u6ee1\u8db3\u4f60\u9700\u6c42\u7684\u6700\u7b80\u5355\u7684\u5173\u7cfb\uff0c\u800c\u4e0d\u662f\u770b\u8d77\u6765\u6700\u7b26\u5408\u5b9e\u9645\u73af\u5883\u7684\u5173\u7cfb\u3002 \u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u8f66\u95f4\u6a21\u62df\u5668\uff0c\u5219\u5c06\u6c7d\u8f66\u548c\u5f15\u64ce\u4f5c\u4e3a\u4e00\u4e2a\u805a\u5408\u662f\u6070\u5f53\u7684\uff0c\u8fd9\u6837\u5f15\u64ce\u5c31\u53ef\u4ee5\u88ab\u4ece\u6c7d\u8f66\u4e2d\u79fb\u9664\u5e76\u653e\u5728\u67d0\u4e2a\u67b6\u5b50\u4e0a\u4ee5\u5907\u4ee5\u540e\u4f7f\u7528\u3002\u7136\u800c\uff0c\u5982\u679c\u4f60\u6b63\u5728\u7f16\u5199\u6a21\u62df\u8d5b\u8f66\u6e38\u620f\uff0c\u90a3\u53ef\u80fd\u5c06\u6c7d\u8f66\u548c\u5f15\u64ce\u4f5c\u4e3a\u7ec4\u5408\u5b9e\u73b0\u66f4\u6070\u5f53\uff0c\u56e0\u4e3a\u5728\u8be5\u8bed\u5883\u4e2d\uff0c\u5f15\u64ce\u4e0d\u4f1a\u72ec\u7acb\u5b58\u5728\u4e8e\u6c7d\u8f66\u4e4b\u5916\u3002 \u6700\u4f73\u5b9e\u8df5 \u5b9e\u73b0\u6ee1\u8db3\u7a0b\u5e8f\u9700\u8981\u7684\u6700\u7b80\u5355\u7684\u5173\u7cfb\u7c7b\u578b\uff0c\u800c\u4e0d\u662f\u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\u770b\u8d77\u6765\u6b63\u786e\u7684\u5173\u7cfb\u7c7b\u578b\u3002 \u5c0f\u7ed3\uff1a\u7ec4\u5408\u548c\u805a\u5408 \u7ec4\u5408: \u901a\u5e38\u4f7f\u7528\u666e\u901a\u7684\u6210\u5458\u53d8\u91cf\uff1b \u5982\u679c\u7c7b\u81ea\u5df1\u7ba1\u7406\u6210\u5458\u5bf9\u8c61\u7684\u5185\u5b58\u7533\u8bf7\u91ca\u653e\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\uff1b \u8d1f\u8d23\u90e8\u5206\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u9500\u6bc1\u3002 \u805a\u5408: \u901a\u5e38\u4f7f\u7528\u6307\u9488\u548c\u5f15\u7528\u6210\u5458\u5e76\u6307\u5411\u5b58\u5728\u4e8e\u6574\u4f53\u5bf9\u8c61\u4f5c\u7528\u57df\u4e4b\u5916\u7684\u5bf9\u8c61\uff1b \u4e0d\u8d1f\u8d23\u90e8\u5206\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u9500\u6bc1\u3002 \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7ec4\u5408\u548c\u805a\u5408\u7684\u6982\u5ff5\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u7c7b\u4e2d\u81ea\u7531\u6df7\u5408\u4f7f\u7528 \u3002\u7f16\u5199\u4e00\u4e2a\u8d1f\u8d23\u521b\u5efa/\u9500\u6bc1\u67d0\u4e9b\u90e8\u5206\u800c\u4e0d\u8d1f\u8d23\u5176\u4ed6\u90e8\u5206\u7684\u7c7b\u662f\u53ef\u4ee5\u7684\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u7684 Department \u7c7b\u53ef\u4ee5\u6709\u4e00\u4e2a name \u548c\u4e00\u4e2a Teacher \u6210\u5458\u3002\u8fd9\u4e2a name \u901a\u5e38\u662f\u4f5c\u4e3a\u7ec4\u5408\u5173\u7cfb\u6dfb\u52a0\u5230\u8be5\u7c7b\u4e2d\u7684\u5e76\u4e14\u548c Department \u4e00\u8d77\u521b\u5efa\u548c\u9500\u6bc1\u3002\u53e6\u4e00\u65b9\u9762\uff0c Teacher \u5c06\u901a\u8fc7\u805a\u5408\u7684\u65b9\u5f0f\u6dfb\u52a0\u5230 Department \u4e2d\uff0c\u5e76\u72ec\u7acb\u5730\u521b\u5efa/\u9500\u6bc1\u3002 \u867d\u7136\u805a\u5408\u53ef\u80fd\u975e\u5e38\u6709\u7528\uff0c\u4f46\u5b83\u4eec\u4e5f\u66f4\u5371\u9669\uff0c\u56e0\u4e3a\u805a\u5408\u4e0d\u5904\u7406\u5176\u90e8\u5206\u5bf9\u8c61\u7684\u56de\u6536\uff0c\u5176\u9500\u6bc1\u64cd\u4f5c\u88ab\u5206\u914d\u5916\u90e8\u53bb\u505a\u4e86\uff0c\u5982\u679c\u5916\u90e8\u4e0d\u518d\u6709\u6307\u5411\u8be5\u90e8\u5206\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u6216\u8005\u5b83\u53ea\u662f\u5fd8\u8bb0\u8fdb\u884c\u6e05\u7406(\u5047\u8bbe\u7c7b\u5c06\u5904\u7406\u6b64\u64cd\u4f5c)\uff0c\u90a3\u4e48\u5185\u5b58\u5c06\u4f1a\u6cc4\u6f0f\u3002 \u56e0\u6b64\uff0c\u5e94\u8be5\u591a\u7528\u7ec4\u5408\u5c11\u7528\u805a\u5408\u3002 \u63d0\u9192\u548c\u52d8\u8bef \u7531\u4e8e\u5404\u79cd\u5404\u6837\u7684\u5386\u53f2\u539f\u56e0\uff0c\u805a\u5408\u7684\u5b9a\u4e49\u5e76\u4e0d\u50cf\u7ec4\u5408\u7684\u5b9a\u4e49\u90a3\u6837\u7cbe\u786e\u2014\u2014\u56e0\u6b64\u4f60\u53ef\u80fd\u4f1a\u770b\u5230\u5176\u4ed6\u53c2\u8003\u6750\u6599\u5bf9\u5b83\u7684\u5b9a\u4e49\u4e0e\u6211\u4eec\u6240\u505a\u7684\u4e0d\u540c\u3002\u8fd9\u5e76\u4e0d\u8981\u7d27\uff0c\u4f60\u77e5\u9053\u5c31\u53ef\u4ee5\u4e86\u3002 \u6700\u540e\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a\u5728\u8bfe\u7a0b 10.5 -\u7ed3\u6784\u4f53\uff0c\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 \u4e2d\uff0c\u6211\u4eec\u5c06\u805a\u5408\u6570\u636e\u7c7b\u578b(\u5982\u7ed3\u6784\u548c\u7c7b)\u5b9a\u4e49\u4e3a\u5c06\u591a\u4e2a\u53d8\u91cf\u7ec4\u5408\u5728\u4e00\u8d77\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f60\u4eca\u540e\u8fd8\u53ef\u80fd\u63a5\u89e6\u5230 \u805a\u5408\u7c7b \u8fd9\u4e2a\u672f\u8bed\uff0c\u5b83\u88ab\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u7ed3\u6784\u6216\u7c7b\uff0c\u4f46\u6ca1\u6709\u63d0\u4f9b\u6784\u9020\u51fd\u6570\u3001\u6790\u6784\u51fd\u6570\u6216\u91cd\u8f7d\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u4e14\u6240\u6709\u6210\u5458\u90fd\u662f \u516c\u6709\u6210\u5458 \uff0c\u5b83\u4e5f\u4e0d\u4f7f\u7528\u7ee7\u627f\u2014\u2014\u5b83\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u4ec5\u5305\u542b\u6570\u636e\u7684\u7ed3\u6784\u4f53\u3002 std::reference_wrapper \u5728 Department / Teacher \u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728 Department \u4e2d\u4f7f\u7528\u5f15\u7528\u6765\u4fdd\u5b58 Teacher \u3002\u53ef\u4ee5\u8fd9\u4e48\u505a\u7684\u539f\u56e0\u662f\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u53ea\u6709\u4e00\u4e2a Teacher \uff0c\u4f46\u662f\u5982\u679c\u9662\u7cfb\u4e2d\u4e0d\u6b62\u4e00\u4e2a\u8001\u5e08\u5462\uff1f\u6b64\u65f6\u6211\u4eec\u9700\u8981\u5c06\u8001\u5e08\u5b58\u653e\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff08\u4f8b\u5982 std::vector \uff09\u3002\u4f46\u662f\u56fa\u5b9a\u6570\u7ec4\u548c\u5f88\u591a\u6807\u51c6\u5e93\u5bb9\u5668\u5e76\u4e0d\u652f\u6301\u4fdd\u5b58\u5f15\u7528\uff08\u56e0\u4e3a\u94fe\u8868\u5143\u7d20\u5fc5\u987b\u662f\u53ef\u8d4b\u503c\u7684\uff0c\u800c\u5f15\u7528\u4e0d\u53ef\u4ee5\u88ab\u8d4b\u503c\uff09\u3002 1 std :: vector < const Teacher &> m_teachers {}; // Illegal \u5982\u679c\u5f15\u7528\u4e0d\u884c\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\uff0c\u4e0d\u8fc7\u6b64\u65f6\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5b58\u653e\u6216\u4f7f\u7528\u7a7a\u6307\u9488\u7684\u95ee\u9898\u3002\u5728 Department / Teacher \u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4e0d\u5141\u8bb8\u4f7f\u7528\u7a7a\u6307\u9488\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528 std::reference_wrapper \u3002 \u672c\u8d28\u4e0a std::reference_wrapper \u662f\u4e00\u4e2a\u884c\u4e3a\u7c7b\u4f3c\u4e8e\u5f15\u7528\u7684\u7c7b\uff0c\u4f46\u662f\u5b83\u53ef\u4ee5\u88ab\u8d4b\u503c\u548c\u62f7\u8d1d\uff0c\u6240\u4ee5\u80fd\u591f\u914d\u5408 std::vector \u4f7f\u7528\u3002 \u597d\u6d88\u606f\u662f\u4f60\u5b8c\u5168\u4e0d\u9700\u8981\u4e86\u89e3\u8fd9\u80cc\u540e\u7684\u539f\u7406\uff0c\u4f7f\u7528\u5b83\u4f60\u53ea\u9700\u8981\u8bb0\u4f4f\u4e09\u4ef6\u4e8b\uff1a std::reference_wrapper \u4f4d\u4e8e \u5934\u6587\u4ef6\uff1b \u5728\u521b\u5efa std::reference_wrapper \u5305\u88c5\u7684\u5bf9\u8c61\u65f6\uff0c\u8be5\u5bf9\u8c61\u4e0d\u53ef\u4ee5\u662f \u533f\u540d\u5bf9\u8c61 (\u56e0\u4e3a\u533f\u540d\u5bf9\u8c61\u5177\u6709\u8868\u8fbe\u5f0f\u4f5c\u7528\u57df\uff0c\u6240\u4ee5\u4f1a\u5bfc\u81f4 \u60ac\u5782 \u5f15\u7528)\u3002 \u5f53\u9700\u8981\u4ece std::reference_wrapper \u4e2d\u83b7\u53d6\u539f\u5bf9\u8c61\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 get() \u6210\u5458\u65b9\u6cd5\u3002 \u4e0b\u9762\u7684\u4e2d\u6211\u4eec\u5728 std::vector \u4e2d\u4f7f\u7528\u4e86 std::reference_wrapper \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include // std::reference_wrapper #include #include #include int main () { std :: string tom { \"Tom\" }; std :: string berta { \"Berta\" }; std :: vector < std :: reference_wrapper < std :: string >> names { tom , berta }; // these strings are stored by reference, not value std :: string jim { \"Jim\" }; names . push_back ( jim ); for ( auto name : names ) { // Use the get() member function to get the referenced string. name . get () += \" Beam\" ; } std :: cout << jim << '\\n' ; // Jim Beam return 0 ; } \u5982\u679c\u8981\u521b\u5efaconst\u5f15\u7528\uff0c\u5728 std::string \u524d\u52a0\u4e0aconst\u5373\u53ef\uff1a 1 2 // Vector of const references to std::string std :: vector < std :: reference_wrapper < const std :: string >> names { tom , berta };","title":"16.3 - \u805a\u5408\u5173\u7cfb"},{"location":"16-3-aggregation/#_1","text":"\u805a\u5408\u5173\u7cfb\u8981\u6ee1\u8db3\u5982\u4e0b\u51e0\u4e2a\u6761\u4ef6\uff1a \u90e8\u5206\uff08\u6210\u5458\uff09\u662f\u6574\u4f53\u5bf9\u8c61\u7684\u4e00\u90e8\u5206\uff1b \u90e8\u5206\uff08\u6210\u5458\uff09\u53ef\u4ee5\u4e0d\u53ea\u5c5e\u4e8e\u4e00\u4e2a\u6574\u4f53\u5bf9\u8c61\uff1b \u90e8\u5206\uff08\u6210\u5458\uff09\u7684\u751f\u547d\u5468\u671f\u4e0d\u7531\u6574\u4f53\u5bf9\u8c61\u7ba1\u7406\uff1b \u90e8\u5206\uff08\u6210\u5458\uff09\u4e0d\u9700\u8981\u77e5\u9053\u6574\u4f53\u5bf9\u8c61\u7684\u5b58\u5728\u6027\uff1b \u548c\u7ec4\u5408\u7c7b\u4f3c\uff0c\u805a\u5408\u4e5f\u662f\u4e00\u79cd\u201d\u90e8\u5206\u4e0e\u6574\u4f53\u201c\u7684\u5173\u7cfb\uff0c\u90e8\u5206\u88ab\u5305\u542b\u5728\u6574\u4f53\u4e2d\uff0c\u800c\u4e14\u8fd9\u662f\u4e00\u79cd\u5355\u5411\u7684\u5173\u7cfb\u3002\u4f46\u662f\uff0c\u548c\u7ec4\u5408\u4e0d\u540c\uff0c\u805a\u5408\u5bf9\u8c61\u53ef\u4ee5\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u6574\u4f53\uff0c\u800c\u4e14\u6574\u4f53\u5bf9\u8c61\u4e0d\u9700\u8981\u8d1f\u8d23\u7ba1\u7406\u90e8\u5206\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u3002\u5f53\u805a\u5408\u5173\u7cfb\u88ab\u521b\u5efa\u65f6\uff0c\u6574\u4f53\u4e0d\u8d1f\u8d23\u521b\u5efa\u90e8\u5206\u3002\u5f53\u805a\u5408\u5173\u7cfb\u88ab\u7834\u574f\u65f6\uff0c\u6574\u4f53\u4e5f\u4e0d\u8d1f\u8d23\u9500\u6bc1\u90e8\u5206\u3002 \u4e3e\u4f8b\u6765\u8bf4\uff0c\u8003\u8651\u67d0\u4e2a\u4eba\u548c\u5b83\u4f4f\u5740\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u4e00\u4e2a\u4eba\u6709\u4e00\u4e2a\u4f4f\u5740\u3002\u4f46\u662f\u4f4f\u5740\u53ef\u4ee5\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u4eba\uff0c\u4f8b\u5982\u4f60\u548c\u4f60\u7684\u5ba4\u53cb\uff0c\u6216\u8005\u662f\u5176\u4ed6\u4eba\u3002\u800c\u4e14\uff0c\u4f4f\u5740\u4e5f\u4e0d\u662f\u88ab\u4eba\u7ba1\u7406\u7684\u2014\u2014\u5176\u5730\u5740\u751a\u81f3\u65e9\u4e8e\u5c45\u4f4f\u4eba\u5c31\u5b58\u5728\u4e86\uff0c\u800c\u4e14\u4eba\u4e0d\u5728\u7684\u65f6\u5019\u5730\u5740\u4ecd\u7136\u4f1a\u7ee7\u7eed\u5b58\u5728\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4e0a\u9762\u63cf\u8ff0\u7684\u662f\u4e00\u79cd\u805a\u5408\u5173\u7cfb\u3002 \u53e6\u5916\uff0c\u8003\u8651\u6c7d\u8f66\u548c\u5f15\u64ce\u7684\u5173\u7cfb\u3002\u6c7d\u8f66\u7684\u5f15\u64ce\u5c5e\u4e8e\u6c7d\u8f66\u7684\u4e00\u90e8\u5206\u3002\u4f46\u662f\u5c3d\u7ba1\u5f15\u64ce\u662f\u6c7d\u8f66\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u4e5f\u53ef\u4ee5\u5c5e\u4e8e\u5176\u4ed6\u4eba\u6216\u7269\u3002\u4f8b\u5982\uff0c\u5f15\u64ce\u4e5f\u5c5e\u4e8e\u8f66\u4e3b\u3002\u6c7d\u8f66\u672c\u8eab\u5e76\u4e0d\u8d1f\u8d23\u521b\u5efa\u6216\u9500\u6bc1\u5f15\u64ce\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u5c3d\u7ba1\u8f66\u8f86\u77e5\u9053\u5b83\u4f1a\u6709\u4e00\u4e2a\u5f15\u64ce\uff0c\u4f46\u662f\u5f15\u64ce\u5374\u4e0d\u77e5\u9053\u5b83\u662f\u6c7d\u8f66\u7684\u4e00\u90e8\u5206\u3002 \u5f53\u6211\u4eec\u8ba8\u8bba\u5bf9\u7269\u7406\u5bf9\u8c61\u5efa\u6a21\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u201d\u9500\u6bc1\u201c\u4e00\u8bcd\u4f1a\u6bd4\u8f83\u5947\u602a\u3002\u6b64\u65f6\u6709\u7684\u4eba\u53ef\u80fd\u4f1a\u62ac\u6760\u8bf4\uff0c\u5982\u679c\u4e00\u4e2a\u9668\u77f3\u649e\u51fb\u5730\u7403\u6b63\u597d\u7838\u5230\u4e86\u4f60\u7684\u8f66\uff0c\u90a3\u5c82\u4e0d\u662f\u8f66\u548c\u5f15\u64ce\u4e00\u8d77\u88ab\u9500\u6bc1\u4e86\uff1f\u5f53\u7136\uff0c\u4e0d\u8fc7\u8fd9\u662f\u9668\u77f3\u7684\u9519\u3002\u8fd9\u91cc\u7684\u5173\u952e\u70b9\u5728\u4e8e\uff0c\u6c7d\u8f66\u4e0d\u8d1f\u8d23\u9500\u6bc1\u5176\u90e8\u4ef6\uff08\u5916\u529b\u662f\u53ef\u80fd\u7684\uff09\u3002 \u6211\u4eec\u53ef\u4ee5\u8bf4\uff0c\u805a\u5408\u6a21\u578b\u662f\u4e00\u79cd\u201d\u6709\u4e00\u4e2a\u201c\u7684\u5173\u7cfb\uff08\u9662\u7cfb\u6709\u4e00\u4e2a\u8001\u5e08\u3001\u6c7d\u8f66\u6709\u4e00\u4e2a\u5f15\u64ce\uff09\u3002 \u7c7b\u4f3c\u7ec4\u5408\u5173\u7cfb\uff0c\u805a\u5408\u4e2d\u7684\u90e8\u5206\u53ef\u4ee5\u662f\u4e00\u4e2a\u4e5f\u53ef\u4ee5\u662f\u591a\u4e2a\u3002","title":"\u805a\u5408"},{"location":"16-3-aggregation/#_2","text":"\u56e0\u4e3a\u805a\u5408\u7c7b\u4f3c\u4e8e\u7ec4\u5408\uff0c\u5b83\u4eec\u90fd\u662f\u90e8\u5206-\u6574\u4f53\u5173\u7cfb\uff0c\u6240\u4ee5\u5b83\u4eec\u7684\u5b9e\u73b0\u51e0\u4e4e\u5b8c\u5168\u76f8\u540c\uff0c\u5b83\u4eec\u4e4b\u95f4\u7684\u533a\u522b\u4e3b\u8981\u662f\u8bed\u4e49\u4e0a\u7684\u3002\u5728\u7ec4\u5408\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u666e\u901a\u6210\u5458\u53d8\u91cf(\u6216\u7531\u7ec4\u5408\u7c7b\u5904\u7406\u5206\u914d\u548c\u56de\u6536\u8fc7\u7a0b\u7684\u6307\u9488)\u5c06\u90e8\u4ef6\u6dfb\u52a0\u5230\u7ec4\u5408\u4e2d\u3002 \u5728\u805a\u5408\u4e2d\uff0c\u6211\u4eec\u4ecd\u7136\u4f7f\u7528\u6210\u5458\u53d8\u91cf\u8868\u793a\u90e8\u5206\u5bf9\u8c61\u3002\u4f46\u662f\uff0c \u8fd9\u4e9b\u6210\u5458\u53d8\u91cf\u901a\u5e38\u662f\u5f15\u7528\u6216\u6307\u9488 \uff0c\u7528\u4e8e\u6307\u5411\u5728\u7c7b\u7684\u4f5c\u7528\u57df\u4e4b\u5916\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u805a\u5408\u901a\u5e38\u8981\u4e48\u63a5\u53d7\u5b83\u5c06\u8981\u6307\u5411\u7684\u5bf9\u8c61\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\u53c2\u6570\uff0c\u8981\u4e48\u4ee5\u7a7a\u5f00\u59cb\uff0c\u7136\u540e\u901a\u8fc7\u8bbf\u95ee\u51fd\u6570\u6216\u64cd\u4f5c\u7b26\u6dfb\u52a0\u5b50\u5bf9\u8c61\u3002 \u56e0\u4e3a\u8fd9\u4e9b\u90e8\u5206\u5bf9\u8c61\u5b58\u5728\u4e8e\u7c7b\u7684\u4f5c\u7528\u57df\u4e4b\u5916\uff0c\u6240\u4ee5\u5f53\u7c7b\u88ab\u9500\u6bc1\u65f6\uff0c\u6307\u9488\u6216\u5f15\u7528\u6210\u5458\u53d8\u91cf\u5c06\u88ab\u9500\u6bc1(\u4f46\u4e0d\u4f1a\u88ab\u5220\u9664)\u3002\u56e0\u6b64\uff0c\u8fd9\u4e9b\u90e8\u5206\u672c\u8eab\u4ecd\u7136\u5b58\u5728\u3002 \u8ba9\u6211\u4eec\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e2a\u201c\u6559\u5e08\u548c\u9662\u7cfb\u201d\u7684\u793a\u4f8b\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u505a\u4e00\u4e9b\u7b80\u5316\uff1a\u9996\u5148\uff0c\u9662\u7cfb\u5c06\u53ea\u5bb9\u7eb3\u4e00\u540d\u6559\u5e08\u3002\u5176\u6b21\uff0c\u8001\u5e08\u4e0d\u77e5\u9053\u4ed6\u4eec\u662f\u54ea\u4e2a\u9662\u7cfb\u7684\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include #include class Teacher { private : std :: string m_name {}; public : Teacher ( const std :: string & name ) : m_name { name } { } const std :: string & getName () const { return m_name ; } }; class Department { private : const Teacher & m_teacher ; // This dept holds only one teacher for simplicity, but it could hold many teachers public : Department ( const Teacher & teacher ) : m_teacher { teacher } { } }; int main () { // Create a teacher outside the scope of the Department Teacher bob { \"Bob\" }; // create a teacher { // Create a department and use the constructor parameter to pass // the teacher to it. Department department { bob }; } // department goes out of scope here and is destroyed // bob still exists here, but the department doesn't std :: cout << bob . getName () << \" still exists! \\n \" ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d bob \u7684\u521b\u5efa\u662f\u72ec\u7acb\u4e8e department \u7684\uff0c\u5b83\u901a\u8fc7\u6784\u9020\u51fd\u6570\u88ab\u4f20\u9012\u7ed9\u4e86 department \u3002\u5f53 department \u9500\u6bc1\u7684\u65f6\u5019\u5f15\u7528 m_teacher \u88ab\u9500\u6bc1\u4e86\uff0c\u4f46\u662f teacher \u672c\u8eab\u5e76\u6ca1\u6709\u88ab\u9500\u6bc1\uff0c\u5b83\u4ecd\u7136\u5b58\u5728\u5e76\u4e14\u5728 main() \u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u3002","title":"\u5b9e\u73b0\u805a\u5408\u5173\u7cfb"},{"location":"16-3-aggregation/#_3","text":"\u5c3d\u7ba1\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6559\u5e08\u4e0d\u77e5\u9053\u4ed6\u4eec\u5728\u4e3a\u54ea\u4e2a\u7cfb\u5de5\u4f5c\uff0c\u8fd9\u542c\u8d77\u6765\u6709\u70b9\u8822\uff0c\u4f46\u5728\u7ed9\u5b9a\u7684\u9879\u76ee\u4e0a\u4e0b\u6587\u4e2d\uff0c\u8fd9\u53ef\u80fd\u5b8c\u5168\u6ca1\u95ee\u9898\u3002\u5f53\u51b3\u5b9a\u5b9e\u73b0\u67d0\u79cd\u7c7b\u578b\u7684\u5173\u7cfb\u65f6\uff0c\u5b9e\u73b0\u6ee1\u8db3\u4f60\u9700\u6c42\u7684\u6700\u7b80\u5355\u7684\u5173\u7cfb\uff0c\u800c\u4e0d\u662f\u770b\u8d77\u6765\u6700\u7b26\u5408\u5b9e\u9645\u73af\u5883\u7684\u5173\u7cfb\u3002 \u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u8f66\u95f4\u6a21\u62df\u5668\uff0c\u5219\u5c06\u6c7d\u8f66\u548c\u5f15\u64ce\u4f5c\u4e3a\u4e00\u4e2a\u805a\u5408\u662f\u6070\u5f53\u7684\uff0c\u8fd9\u6837\u5f15\u64ce\u5c31\u53ef\u4ee5\u88ab\u4ece\u6c7d\u8f66\u4e2d\u79fb\u9664\u5e76\u653e\u5728\u67d0\u4e2a\u67b6\u5b50\u4e0a\u4ee5\u5907\u4ee5\u540e\u4f7f\u7528\u3002\u7136\u800c\uff0c\u5982\u679c\u4f60\u6b63\u5728\u7f16\u5199\u6a21\u62df\u8d5b\u8f66\u6e38\u620f\uff0c\u90a3\u53ef\u80fd\u5c06\u6c7d\u8f66\u548c\u5f15\u64ce\u4f5c\u4e3a\u7ec4\u5408\u5b9e\u73b0\u66f4\u6070\u5f53\uff0c\u56e0\u4e3a\u5728\u8be5\u8bed\u5883\u4e2d\uff0c\u5f15\u64ce\u4e0d\u4f1a\u72ec\u7acb\u5b58\u5728\u4e8e\u6c7d\u8f66\u4e4b\u5916\u3002 \u6700\u4f73\u5b9e\u8df5 \u5b9e\u73b0\u6ee1\u8db3\u7a0b\u5e8f\u9700\u8981\u7684\u6700\u7b80\u5355\u7684\u5173\u7cfb\u7c7b\u578b\uff0c\u800c\u4e0d\u662f\u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\u770b\u8d77\u6765\u6b63\u786e\u7684\u5173\u7cfb\u7c7b\u578b\u3002","title":"\u5bf9\u8c61\u5efa\u6a21\u65f6\u5e94\u5f53\u9009\u62e9\u6b63\u786e\u7684\u5bf9\u8c61\u5173\u7cfb"},{"location":"16-3-aggregation/#_4","text":"\u7ec4\u5408: \u901a\u5e38\u4f7f\u7528\u666e\u901a\u7684\u6210\u5458\u53d8\u91cf\uff1b \u5982\u679c\u7c7b\u81ea\u5df1\u7ba1\u7406\u6210\u5458\u5bf9\u8c61\u7684\u5185\u5b58\u7533\u8bf7\u91ca\u653e\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\uff1b \u8d1f\u8d23\u90e8\u5206\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u9500\u6bc1\u3002 \u805a\u5408: \u901a\u5e38\u4f7f\u7528\u6307\u9488\u548c\u5f15\u7528\u6210\u5458\u5e76\u6307\u5411\u5b58\u5728\u4e8e\u6574\u4f53\u5bf9\u8c61\u4f5c\u7528\u57df\u4e4b\u5916\u7684\u5bf9\u8c61\uff1b \u4e0d\u8d1f\u8d23\u90e8\u5206\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u9500\u6bc1\u3002 \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7ec4\u5408\u548c\u805a\u5408\u7684\u6982\u5ff5\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u7c7b\u4e2d\u81ea\u7531\u6df7\u5408\u4f7f\u7528 \u3002\u7f16\u5199\u4e00\u4e2a\u8d1f\u8d23\u521b\u5efa/\u9500\u6bc1\u67d0\u4e9b\u90e8\u5206\u800c\u4e0d\u8d1f\u8d23\u5176\u4ed6\u90e8\u5206\u7684\u7c7b\u662f\u53ef\u4ee5\u7684\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u7684 Department \u7c7b\u53ef\u4ee5\u6709\u4e00\u4e2a name \u548c\u4e00\u4e2a Teacher \u6210\u5458\u3002\u8fd9\u4e2a name \u901a\u5e38\u662f\u4f5c\u4e3a\u7ec4\u5408\u5173\u7cfb\u6dfb\u52a0\u5230\u8be5\u7c7b\u4e2d\u7684\u5e76\u4e14\u548c Department \u4e00\u8d77\u521b\u5efa\u548c\u9500\u6bc1\u3002\u53e6\u4e00\u65b9\u9762\uff0c Teacher \u5c06\u901a\u8fc7\u805a\u5408\u7684\u65b9\u5f0f\u6dfb\u52a0\u5230 Department \u4e2d\uff0c\u5e76\u72ec\u7acb\u5730\u521b\u5efa/\u9500\u6bc1\u3002 \u867d\u7136\u805a\u5408\u53ef\u80fd\u975e\u5e38\u6709\u7528\uff0c\u4f46\u5b83\u4eec\u4e5f\u66f4\u5371\u9669\uff0c\u56e0\u4e3a\u805a\u5408\u4e0d\u5904\u7406\u5176\u90e8\u5206\u5bf9\u8c61\u7684\u56de\u6536\uff0c\u5176\u9500\u6bc1\u64cd\u4f5c\u88ab\u5206\u914d\u5916\u90e8\u53bb\u505a\u4e86\uff0c\u5982\u679c\u5916\u90e8\u4e0d\u518d\u6709\u6307\u5411\u8be5\u90e8\u5206\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u6216\u8005\u5b83\u53ea\u662f\u5fd8\u8bb0\u8fdb\u884c\u6e05\u7406(\u5047\u8bbe\u7c7b\u5c06\u5904\u7406\u6b64\u64cd\u4f5c)\uff0c\u90a3\u4e48\u5185\u5b58\u5c06\u4f1a\u6cc4\u6f0f\u3002 \u56e0\u6b64\uff0c\u5e94\u8be5\u591a\u7528\u7ec4\u5408\u5c11\u7528\u805a\u5408\u3002","title":"\u5c0f\u7ed3\uff1a\u7ec4\u5408\u548c\u805a\u5408"},{"location":"16-3-aggregation/#_5","text":"\u7531\u4e8e\u5404\u79cd\u5404\u6837\u7684\u5386\u53f2\u539f\u56e0\uff0c\u805a\u5408\u7684\u5b9a\u4e49\u5e76\u4e0d\u50cf\u7ec4\u5408\u7684\u5b9a\u4e49\u90a3\u6837\u7cbe\u786e\u2014\u2014\u56e0\u6b64\u4f60\u53ef\u80fd\u4f1a\u770b\u5230\u5176\u4ed6\u53c2\u8003\u6750\u6599\u5bf9\u5b83\u7684\u5b9a\u4e49\u4e0e\u6211\u4eec\u6240\u505a\u7684\u4e0d\u540c\u3002\u8fd9\u5e76\u4e0d\u8981\u7d27\uff0c\u4f60\u77e5\u9053\u5c31\u53ef\u4ee5\u4e86\u3002 \u6700\u540e\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a\u5728\u8bfe\u7a0b 10.5 -\u7ed3\u6784\u4f53\uff0c\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 \u4e2d\uff0c\u6211\u4eec\u5c06\u805a\u5408\u6570\u636e\u7c7b\u578b(\u5982\u7ed3\u6784\u548c\u7c7b)\u5b9a\u4e49\u4e3a\u5c06\u591a\u4e2a\u53d8\u91cf\u7ec4\u5408\u5728\u4e00\u8d77\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f60\u4eca\u540e\u8fd8\u53ef\u80fd\u63a5\u89e6\u5230 \u805a\u5408\u7c7b \u8fd9\u4e2a\u672f\u8bed\uff0c\u5b83\u88ab\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u7ed3\u6784\u6216\u7c7b\uff0c\u4f46\u6ca1\u6709\u63d0\u4f9b\u6784\u9020\u51fd\u6570\u3001\u6790\u6784\u51fd\u6570\u6216\u91cd\u8f7d\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u4e14\u6240\u6709\u6210\u5458\u90fd\u662f \u516c\u6709\u6210\u5458 \uff0c\u5b83\u4e5f\u4e0d\u4f7f\u7528\u7ee7\u627f\u2014\u2014\u5b83\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u4ec5\u5305\u542b\u6570\u636e\u7684\u7ed3\u6784\u4f53\u3002","title":"\u63d0\u9192\u548c\u52d8\u8bef"},{"location":"16-3-aggregation/#stdreference_wrapper","text":"\u5728 Department / Teacher \u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728 Department \u4e2d\u4f7f\u7528\u5f15\u7528\u6765\u4fdd\u5b58 Teacher \u3002\u53ef\u4ee5\u8fd9\u4e48\u505a\u7684\u539f\u56e0\u662f\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u53ea\u6709\u4e00\u4e2a Teacher \uff0c\u4f46\u662f\u5982\u679c\u9662\u7cfb\u4e2d\u4e0d\u6b62\u4e00\u4e2a\u8001\u5e08\u5462\uff1f\u6b64\u65f6\u6211\u4eec\u9700\u8981\u5c06\u8001\u5e08\u5b58\u653e\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff08\u4f8b\u5982 std::vector \uff09\u3002\u4f46\u662f\u56fa\u5b9a\u6570\u7ec4\u548c\u5f88\u591a\u6807\u51c6\u5e93\u5bb9\u5668\u5e76\u4e0d\u652f\u6301\u4fdd\u5b58\u5f15\u7528\uff08\u56e0\u4e3a\u94fe\u8868\u5143\u7d20\u5fc5\u987b\u662f\u53ef\u8d4b\u503c\u7684\uff0c\u800c\u5f15\u7528\u4e0d\u53ef\u4ee5\u88ab\u8d4b\u503c\uff09\u3002 1 std :: vector < const Teacher &> m_teachers {}; // Illegal \u5982\u679c\u5f15\u7528\u4e0d\u884c\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\uff0c\u4e0d\u8fc7\u6b64\u65f6\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5b58\u653e\u6216\u4f7f\u7528\u7a7a\u6307\u9488\u7684\u95ee\u9898\u3002\u5728 Department / Teacher \u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4e0d\u5141\u8bb8\u4f7f\u7528\u7a7a\u6307\u9488\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528 std::reference_wrapper \u3002 \u672c\u8d28\u4e0a std::reference_wrapper \u662f\u4e00\u4e2a\u884c\u4e3a\u7c7b\u4f3c\u4e8e\u5f15\u7528\u7684\u7c7b\uff0c\u4f46\u662f\u5b83\u53ef\u4ee5\u88ab\u8d4b\u503c\u548c\u62f7\u8d1d\uff0c\u6240\u4ee5\u80fd\u591f\u914d\u5408 std::vector \u4f7f\u7528\u3002 \u597d\u6d88\u606f\u662f\u4f60\u5b8c\u5168\u4e0d\u9700\u8981\u4e86\u89e3\u8fd9\u80cc\u540e\u7684\u539f\u7406\uff0c\u4f7f\u7528\u5b83\u4f60\u53ea\u9700\u8981\u8bb0\u4f4f\u4e09\u4ef6\u4e8b\uff1a std::reference_wrapper \u4f4d\u4e8e \u5934\u6587\u4ef6\uff1b \u5728\u521b\u5efa std::reference_wrapper \u5305\u88c5\u7684\u5bf9\u8c61\u65f6\uff0c\u8be5\u5bf9\u8c61\u4e0d\u53ef\u4ee5\u662f \u533f\u540d\u5bf9\u8c61 (\u56e0\u4e3a\u533f\u540d\u5bf9\u8c61\u5177\u6709\u8868\u8fbe\u5f0f\u4f5c\u7528\u57df\uff0c\u6240\u4ee5\u4f1a\u5bfc\u81f4 \u60ac\u5782 \u5f15\u7528)\u3002 \u5f53\u9700\u8981\u4ece std::reference_wrapper \u4e2d\u83b7\u53d6\u539f\u5bf9\u8c61\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 get() \u6210\u5458\u65b9\u6cd5\u3002 \u4e0b\u9762\u7684\u4e2d\u6211\u4eec\u5728 std::vector \u4e2d\u4f7f\u7528\u4e86 std::reference_wrapper \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include // std::reference_wrapper #include #include #include int main () { std :: string tom { \"Tom\" }; std :: string berta { \"Berta\" }; std :: vector < std :: reference_wrapper < std :: string >> names { tom , berta }; // these strings are stored by reference, not value std :: string jim { \"Jim\" }; names . push_back ( jim ); for ( auto name : names ) { // Use the get() member function to get the referenced string. name . get () += \" Beam\" ; } std :: cout << jim << '\\n' ; // Jim Beam return 0 ; } \u5982\u679c\u8981\u521b\u5efaconst\u5f15\u7528\uff0c\u5728 std::string \u524d\u52a0\u4e0aconst\u5373\u53ef\uff1a 1 2 // Vector of const references to std::string std :: vector < std :: reference_wrapper < const std :: string >> names { tom , berta };","title":"std::reference_wrapper"},{"location":"16-4-association/","tags":["object","association"],"text":"Key Takeaway - \u5728\u524d\u9762\u7684\u4e24\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u4e24\u79cd\u5bf9\u8c61\u7ec4\u5408\u5173\u7cfb\u2014\u2014\u7ec4\u5408\u548c\u7ee7\u627f\u3002\u5bf9\u8c61\u7ec4\u5408\u7528\u4e8e\u901a\u8fc7\u4e00\u4e2a\u6216\u591a\u4e2a\u7b80\u5355\u5bf9\u8c61\u6784\u5efa\u590d\u6742\u5bf9\u8c61\u3002 \u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u770b\u770b\u4e24\u4e2a\u672c\u6765\u4e0d\u76f8\u5173\u7684\u5bf9\u8c61\u4e4b\u95f4\u7684\u4e00\u79cd\u8f83\u5f31\u7c7b\u578b\u7684\u5173\u7cfb\uff0c\u79f0\u4e3a\u5173\u8054\u3002\u4e0e\u5bf9\u8c61\u7ec4\u5408\u5173\u7cfb\u4e0d\u540c\uff0c\u5728\u5173\u8054\u4e2d\uff0c\u6ca1\u6709\u9690\u542b\u7684\u6574\u4f53/\u90e8\u5206\u5173\u7cfb\u3002 \u5173\u8054 \u5f62\u6210\u5173\u8054\u5173\u7cfb\u65f6\uff0c\u5bf9\u8c61\u548c\u5bf9\u8c61\u4e4b\u95f4\u9700\u8981\u6ee1\u8db3\u4e0b\u9762\u6761\u4ef6\uff1a \u5173\u8054\u7684\u5bf9\u8c61(\u6210\u5458)\u5728\u5176\u4ed6\u65b9\u9762\u4e0e\u5bf9\u8c61(\u7c7b)\u65e0\u5173\uff1b \u5173\u8054\u7684\u5bf9\u8c61(\u6210\u5458)\u53ef\u4ee5\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u5bf9\u8c61(\u7c7b)\uff1b \u5173\u8054\u5bf9\u8c61(\u6210\u5458)\u7684\u5b58\u5728\u6027\u4e0d\u7531\u5bf9\u8c61(\u7c7b)\u7ba1\u7406\uff1b \u5173\u8054\u7684\u5bf9\u8c61(\u6210\u5458)\u53ef\u80fd\u77e5\u9053\u4e5f\u53ef\u80fd\u4e0d\u77e5\u9053\u5bf9\u8c61(\u7c7b)\u7684\u5b58\u5728\u3002 \u5728\u7ec4\u5408\u6216\u805a\u5408\u4e2d\uff0c\u90e8\u5206\u662f\u6574\u4e2a\u5bf9\u8c61\u7684\u4e00\u90e8\u5206\uff0c\u800c\u5728\u5173\u8054\u4e2d\uff0c\u5173\u8054\u5bf9\u8c61\u5728\u5176\u4ed6\u65b9\u9762\u4e0e\u5bf9\u8c61\u65e0\u5173\u3002\u5c31\u50cf\u805a\u5408\u4e00\u6837\uff0c\u5173\u8054\u7684\u5bf9\u8c61\u53ef\u4ee5\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u5bf9\u8c61\uff0c\u5e76\u4e14\u4e0d\u53d7\u8fd9\u4e9b\u5bf9\u8c61\u7684\u7ba1\u7406\u3002\u7136\u800c\uff0c\u4e0e\u805a\u5408(\u805a\u5408\u7684\u5173\u7cfb\u603b\u662f\u5355\u5411\u7684)\u4e0d\u540c\uff0c\u5728\u5173\u8054\u4e2d\uff0c\u5173\u7cfb\u53ef\u80fd\u662f\u5355\u5411\u7684\u6216\u53cc\u5411\u7684(\u5176\u4e2d\u4e24\u4e2a\u5bf9\u8c61\u5f7c\u6b64\u610f\u8bc6\u5230\u5bf9\u65b9)\u3002 \u533b\u751f\u548c\u75c5\u4eba\u4e4b\u95f4\u7684\u5173\u7cfb\u5c31\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u4f8b\u5b50\u3002\u533b\u751f\u663e\u7136\u4e0e\u4ed6\u7684\u75c5\u4eba\u6709\u4e00\u79cd\u5173\u7cfb\uff0c\u4f46\u4ece\u6982\u5ff5\u4e0a\u8bb2\uff0c\u8fd9\u4e0d\u662f\u4e00\u4e2a\u90e8\u5206/\u6574\u4f53(\u5bf9\u8c61\u7ec4\u6210)\u5173\u7cfb\u3002\u4e00\u4e2a\u533b\u751f\u4e00\u5929\u53ef\u4ee5\u770b\u5f88\u591a\u75c5\u4eba\uff0c\u4e00\u4e2a\u75c5\u4eba\u53ef\u4ee5\u770b\u5f88\u591a\u533b\u751f(\u4e5f\u8bb8\u4ed6\u4eec\u60f3\u8981\u7b2c\u4e8c\u4e2a\u610f\u89c1\uff0c\u6216\u8005\u4ed6\u4eec\u6b63\u5728\u770b\u4e0d\u540c\u7c7b\u578b\u7684\u533b\u751f)\u3002\u8fd9\u4e24\u4e2a\u7269\u4f53\u7684\u5bff\u547d\u90fd\u4e0e\u5bf9\u65b9\u65e0\u5173\u3002 \u6211\u4eec\u53ef\u4ee5\u8bf4\u5173\u8054\u5efa\u6a21\u4e3a\u201c\u4f7f\u7528\u4e00\u4e2a\u201d\u5173\u7cfb\u3002\u533b\u751f\u201c\u4f7f\u7528\u201d\u75c5\u4eba(\u4ee5\u8d5a\u53d6\u6536\u5165)\u3002\u75c5\u4eba\u201c\u4f7f\u7528\u201d\u533b\u751f(\u4e3a\u4e86\u4ed6\u4eec\u9700\u8981\u7684\u4efb\u4f55\u5065\u5eb7\u76ee\u7684)\u3002 \u5b9e\u73b0\u5173\u8054\u5173\u7cfb \u56e0\u4e3a\u5173\u8054\u662f\u4e00\u79cd\u6bd4\u8f83\u5bbd\u6cdb\u7684\u5173\u7cfb\u7c7b\u578b\uff0c\u5b83\u4eec\u53ef\u4ee5\u4ee5\u8bb8\u591a\u4e0d\u540c\u7684\u65b9\u5f0f\u5b9e\u73b0\u3002\u7136\u800c\uff0c\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5173\u8054\u662f\u4f7f\u7528\u6307\u9488\u5b9e\u73b0\u7684\uff0c\u5bf9\u8c61\u6307\u5411\u5173\u8054\u5bf9\u8c61\u3002 \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u5b9e\u73b0\u4e00\u4e2a\u53cc\u5411\u7684\u533b\u751f/\u75c5\u4eba\u5173\u7cfb\uff0c\u56e0\u4e3a\u533b\u751f\u77e5\u9053\u8c01\u662f\u4ed6\u4eec\u7684\u75c5\u4eba\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 #include // reference_wrapper #include #include #include // Since Doctor and Patient have a circular dependency, we're going to forward declare Patient class Patient ; class Doctor { private : std :: string m_name {}; std :: vector < std :: reference_wrapper < const Patient >> m_patient {}; public : Doctor ( const std :: string & name ) : m_name { name } { } void addPatient ( Patient & patient ); // We'll implement this function below Patient since we need Patient to be defined at that point friend std :: ostream & operator << ( std :: ostream & out , const Doctor & doctor ); const std :: string & getName () const { return m_name ; } }; class Patient { private : std :: string m_name {}; std :: vector < std :: reference_wrapper < const Doctor >> m_doctor {}; // so that we can use it here // We're going to make addDoctor private because we don't want the public to use it. // They should use Doctor::addPatient() instead, which is publicly exposed void addDoctor ( const Doctor & doctor ) { m_doctor . push_back ( doctor ); } public : Patient ( const std :: string & name ) : m_name { name } { } // We'll implement this function below to parallel operator<<(std::ostream&, const Doctor&) friend std :: ostream & operator << ( std :: ostream & out , const Patient & patient ); const std :: string & getName () const { return m_name ; } // We'll friend Doctor::addPatient() so it can access the private function Patient::addDoctor() friend void Doctor::addPatient ( Patient & patient ); }; void Doctor::addPatient ( Patient & patient ) { // Our doctor will add this patient m_patient . push_back ( patient ); // and the patient will also add this doctor patient . addDoctor ( * this ); } std :: ostream & operator << ( std :: ostream & out , const Doctor & doctor ) { if ( doctor . m_patient . empty ()) { out << doctor . m_name << \" has no patients right now\" ; return out ; } out << doctor . m_name << \" is seeing patients: \" ; for ( const auto & patient : doctor . m_patient ) out << patient . get (). getName () << ' ' ; return out ; } std :: ostream & operator << ( std :: ostream & out , const Patient & patient ) { if ( patient . m_doctor . empty ()) { out << patient . getName () << \" has no doctors right now\" ; return out ; } out << patient . m_name << \" is seeing doctors: \" ; for ( const auto & doctor : patient . m_doctor ) out << doctor . get (). getName () << ' ' ; return out ; } int main () { // Create a Patient outside the scope of the Doctor Patient dave { \"Dave\" }; Patient frank { \"Frank\" }; Patient betsy { \"Betsy\" }; Doctor james { \"James\" }; Doctor scott { \"Scott\" }; james . addPatient ( dave ); scott . addPatient ( dave ); scott . addPatient ( betsy ); std :: cout << james << '\\n' ; std :: cout << scott << '\\n' ; std :: cout << dave << '\\n' ; std :: cout << frank << '\\n' ; std :: cout << betsy << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 2 3 4 5 James is seeing patients: Dave Scott is seeing patients: Dave Betsy Dave is seeing doctors: James Scott Frank has no doctors right now Betsy is seeing doctors: Scott \u4e00\u822c\u6765\u8bf4\uff0c\u5982\u679c\u53ef\u4ee5\u4f7f\u7528\u5355\u5411\u5173\u8054\uff0c\u5219\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u53cc\u5411\u5173\u8054\uff0c\u56e0\u4e3a\u5b83\u4eec\u589e\u52a0\u4e86\u590d\u6742\u6027\uff0c\u800c\u4e14\u5f88\u96be\u5728\u4e0d\u51fa\u9519\u3002 \u53cd\u8eab\u5173\u8054 \u6709\u65f6\u5bf9\u8c61\u53ef\u80fd\u4e0e\u76f8\u540c\u7c7b\u578b\u7684\u5176\u4ed6\u5bf9\u8c61\u6709\u5173\u7cfb\uff0c\u8fd9\u88ab\u79f0\u4e3a \u53cd\u8eab\u5173\u8054 \u3002\u53cd\u8eab\u5173\u8054\u7684\u4e00\u4e2a\u5f88\u597d\u7684\u4f8b\u5b50\u662f\u5927\u5b66\u8bfe\u7a0b\u548c\u5b83\u7684\u5148\u4fee\u8bfe\u7a0b(\u4e5f\u662f\u5927\u5b66\u8bfe\u7a0b)\u4e4b\u95f4\u7684\u5173\u7cfb\u3002 \u8003\u8651\u4e00\u4e2a\u7b80\u5316\u7684\u60c5\u51b5\uff0c\u5176\u4e2d\u4e00\u4e2a\u8bfe\u7a0b\u53ea\u80fd\u6709\u4e00\u4e2a\u5148\u4fee\u8bfe\u7a0b\u3002\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include class Course { private : std :: string m_name ; const Course * m_prerequisite ; public : Course ( const std :: string & name , const Course * prerequisite = nullptr ) : m_name { name }, m_prerequisite { prerequisite } { } }; \u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u7cfb\u5217\u7684\u5173\u8054(\u4e00\u95e8\u8bfe\u7a0b\u6709\u4e00\u4e2a\u5148\u51b3\u6761\u4ef6\uff0c\u53e6\u4e00\u95e8\u8bfe\u7a0b\u4e5f\u6709\u4e00\u4e2a\u5148\u51b3\u6761\u4ef6\uff0c\u7b49\u7b49\u2026\u2026) \u5173\u8054\u53ef\u4ee5\u662f\u95f4\u63a5\u7684 \u5728\u524d\u9762\u7684\u6240\u6709\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u90fd\u4f7f\u7528\u6307\u9488\u6216\u5f15\u7528\u76f4\u63a5\u5c06\u5bf9\u8c61\u8fde\u63a5\u5728\u4e00\u8d77\u3002\u7136\u800c\u5728\u5173\u8054\u4e2d\uff0c\u5e76\u4e0d\u4e00\u5b9a\u8981\u8fd9\u6837\u505a\u3002\u4efb\u4f55\u80fd\u591f\u5efa\u7acb\u4e24\u4e2a\u5bf9\u8c61\u4e4b\u95f4\u5173\u7cfb\u7684\u6570\u636e\u90fd\u53ef\u4ee5\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86 Driver \u7c7b\u5982\u4f55\u5728\u4e0d\u5b9e\u9645\u5305\u542b Car \u6307\u9488\u6216\u5f15\u7528\u6210\u5458\u7684\u60c5\u51b5\u4e0b\u4e0e Car \u5f62\u6210\u5355\u5411\u5173\u8054\u5173\u7cfb\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 #include #include class Car { private : std :: string m_name ; int m_id ; public : Car ( const std :: string & name , int id ) : m_name { name }, m_id { id } { } const std :: string & getName () const { return m_name ; } int getId () const { return m_id ; } }; // Our CarLot is essentially just a static array of Cars and a lookup function to retrieve them. // Because it's static, we don't need to allocate an object of type CarLot to use it class CarLot { private : static Car s_carLot [ 4 ]; public : CarLot () = delete ; // Ensure we don't try to create a CarLot static Car * getCar ( int id ) { for ( int count { 0 }; count < 4 ; ++ count ) { if ( s_carLot [ count ]. getId () == id ) { return & ( s_carLot [ count ]); } } return nullptr ; } }; Car CarLot :: s_carLot [ 4 ]{ { \"Prius\" , 4 }, { \"Corolla\" , 17 }, { \"Accord\" , 84 }, { \"Matrix\" , 62 } }; class Driver { private : std :: string m_name ; int m_carId ; // we're associated with the Car by ID rather than pointer public : Driver ( const std :: string & name , int carId ) : m_name { name }, m_carId { carId } { } const std :: string & getName () const { return m_name ; } int getCarId () const { return m_carId ; } }; int main () { Driver d { \"Franz\" , 17 }; // Franz is driving the car with ID 17 Car * car { CarLot :: getCar ( d . getCarId ()) }; // Get that car from the car lot if ( car ) std :: cout << d . getName () << \" is driving a \" << car -> getName () << '\\n' ; else std :: cout << d . getName () << \" couldn't find his car \\n \" ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c CarLot \u6301\u6709 car \u3002\u800c Driver \u4f5c\u4e3a\u672c\u8eab\u9700\u8981 car \u7684\u5bf9\u8c61\uff0c\u5374\u5e76\u6ca1\u6709\u6307\u5411 Car \u7684\u6307\u9488\u2014\u2014\u4f46\u662f\u5b83\u6709\u8f66\u7684ID\uff0c\u5b83\u53ef\u4ee5\u901a\u8fc7\u8be5ID\u5728\u9700\u8981\u65f6\u4ece CarLot \u4e2d\u83b7\u53d6 car \u3002 \u5728\u8fd9\u4e2a\u7279\u5b9a\u7684\u793a\u4f8b\u4e2d\uff0c\u8fd9\u6837\u505a\u6709\u70b9\u611a\u8822\uff0c\u56e0\u4e3a\u4ece CarLot \u4e2d\u83b7\u53d6 Car \u9700\u8981\u4f4e\u6548\u7684\u67e5\u627e(\u4e4b\u95f4\u76f4\u63a5\u4f7f\u7528\u6307\u9488\u8981\u5feb\u5f97\u591a)\u3002\u7136\u800c\uff0c\u901a\u8fc7\u552f\u4e00ID\u800c\u4e0d\u662f\u6307\u9488\u5f15\u7528\u4e1c\u897f\u6709\u4e00\u4e9b\u4f18\u70b9\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5f15\u7528\u5f53\u524d\u4e0d\u5728\u5185\u5b58\u4e2d\u7684\u5185\u5bb9(\u5b83\u4eec\u53ef\u80fd\u5728\u6587\u4ef6\u4e2d\uff0c\u6216\u8005\u5728\u6570\u636e\u5e93\u4e2d\uff0c\u5e76\u4e14\u53ef\u4ee5\u6309\u9700\u52a0\u8f7d)\u3002\u6b64\u5916\uff0c\u6307\u9488\u53ef\u80fd\u5360\u75284\u62168\u4e2a\u5b57\u8282\u2014\u2014\u5982\u679c\u7a7a\u95f4\u975e\u5e38\u6709\u9650\uff0c\u4e14\u60df\u4e00\u5bf9\u8c61\u7684\u6570\u91cf\u76f8\u5f53\u4f4e\uff0c\u90a3\u4e48\u901a\u8fc78\u4f4d\u621616\u4f4d\u6574\u6570\u5f15\u7528\u5b83\u4eec\u53ef\u4ee5\u8282\u7701\u5927\u91cf\u5185\u5b58\u3002 \u7ec4\u5408 vs \u805a\u5408 vs \u5173\u8054 \u4e0b\u9762\u662f\u4e00\u4e2a\u6c47\u603b\u8868\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8bb0\u4f4f\u7ec4\u5408\u3001\u805a\u5408\u548c\u5173\u8054\u4e4b\u95f4\u7684\u533a\u522b: \u5c5e\u6027 \u7ec4\u5408 \u805a\u5408 \u5173\u8054 \u5173\u7cfb\u7c7b\u578b \u6574\u4f53-\u90e8\u5206 \u6574\u4f53-\u90e8\u5206 \u672c\u4e0d\u76f8\u5173 \u6210\u5458\u662f\u5426\u53ef\u4ee5\u5c5e\u4e8e\u4e0d\u540c\u7684\u6574\u4f53\u5bf9\u8c61 No Yes Yes \u6210\u5458\u7684\u5b58\u5728\u6027\u662f\u5426\u7531\u6574\u4f53\u7ba1\u7406 Yes No No \u65b9\u5411 \u5355\u5411 \u5355\u5411 \u5355\u5411\u6216\u53cc\u5411 \u5173\u7cfb\u52a8\u8bcd Part-of Has-a Uses-a","title":"16.4 - \u5173\u8054\u5173\u7cfb"},{"location":"16-4-association/#_1","text":"\u5f62\u6210\u5173\u8054\u5173\u7cfb\u65f6\uff0c\u5bf9\u8c61\u548c\u5bf9\u8c61\u4e4b\u95f4\u9700\u8981\u6ee1\u8db3\u4e0b\u9762\u6761\u4ef6\uff1a \u5173\u8054\u7684\u5bf9\u8c61(\u6210\u5458)\u5728\u5176\u4ed6\u65b9\u9762\u4e0e\u5bf9\u8c61(\u7c7b)\u65e0\u5173\uff1b \u5173\u8054\u7684\u5bf9\u8c61(\u6210\u5458)\u53ef\u4ee5\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u5bf9\u8c61(\u7c7b)\uff1b \u5173\u8054\u5bf9\u8c61(\u6210\u5458)\u7684\u5b58\u5728\u6027\u4e0d\u7531\u5bf9\u8c61(\u7c7b)\u7ba1\u7406\uff1b \u5173\u8054\u7684\u5bf9\u8c61(\u6210\u5458)\u53ef\u80fd\u77e5\u9053\u4e5f\u53ef\u80fd\u4e0d\u77e5\u9053\u5bf9\u8c61(\u7c7b)\u7684\u5b58\u5728\u3002 \u5728\u7ec4\u5408\u6216\u805a\u5408\u4e2d\uff0c\u90e8\u5206\u662f\u6574\u4e2a\u5bf9\u8c61\u7684\u4e00\u90e8\u5206\uff0c\u800c\u5728\u5173\u8054\u4e2d\uff0c\u5173\u8054\u5bf9\u8c61\u5728\u5176\u4ed6\u65b9\u9762\u4e0e\u5bf9\u8c61\u65e0\u5173\u3002\u5c31\u50cf\u805a\u5408\u4e00\u6837\uff0c\u5173\u8054\u7684\u5bf9\u8c61\u53ef\u4ee5\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u5bf9\u8c61\uff0c\u5e76\u4e14\u4e0d\u53d7\u8fd9\u4e9b\u5bf9\u8c61\u7684\u7ba1\u7406\u3002\u7136\u800c\uff0c\u4e0e\u805a\u5408(\u805a\u5408\u7684\u5173\u7cfb\u603b\u662f\u5355\u5411\u7684)\u4e0d\u540c\uff0c\u5728\u5173\u8054\u4e2d\uff0c\u5173\u7cfb\u53ef\u80fd\u662f\u5355\u5411\u7684\u6216\u53cc\u5411\u7684(\u5176\u4e2d\u4e24\u4e2a\u5bf9\u8c61\u5f7c\u6b64\u610f\u8bc6\u5230\u5bf9\u65b9)\u3002 \u533b\u751f\u548c\u75c5\u4eba\u4e4b\u95f4\u7684\u5173\u7cfb\u5c31\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u4f8b\u5b50\u3002\u533b\u751f\u663e\u7136\u4e0e\u4ed6\u7684\u75c5\u4eba\u6709\u4e00\u79cd\u5173\u7cfb\uff0c\u4f46\u4ece\u6982\u5ff5\u4e0a\u8bb2\uff0c\u8fd9\u4e0d\u662f\u4e00\u4e2a\u90e8\u5206/\u6574\u4f53(\u5bf9\u8c61\u7ec4\u6210)\u5173\u7cfb\u3002\u4e00\u4e2a\u533b\u751f\u4e00\u5929\u53ef\u4ee5\u770b\u5f88\u591a\u75c5\u4eba\uff0c\u4e00\u4e2a\u75c5\u4eba\u53ef\u4ee5\u770b\u5f88\u591a\u533b\u751f(\u4e5f\u8bb8\u4ed6\u4eec\u60f3\u8981\u7b2c\u4e8c\u4e2a\u610f\u89c1\uff0c\u6216\u8005\u4ed6\u4eec\u6b63\u5728\u770b\u4e0d\u540c\u7c7b\u578b\u7684\u533b\u751f)\u3002\u8fd9\u4e24\u4e2a\u7269\u4f53\u7684\u5bff\u547d\u90fd\u4e0e\u5bf9\u65b9\u65e0\u5173\u3002 \u6211\u4eec\u53ef\u4ee5\u8bf4\u5173\u8054\u5efa\u6a21\u4e3a\u201c\u4f7f\u7528\u4e00\u4e2a\u201d\u5173\u7cfb\u3002\u533b\u751f\u201c\u4f7f\u7528\u201d\u75c5\u4eba(\u4ee5\u8d5a\u53d6\u6536\u5165)\u3002\u75c5\u4eba\u201c\u4f7f\u7528\u201d\u533b\u751f(\u4e3a\u4e86\u4ed6\u4eec\u9700\u8981\u7684\u4efb\u4f55\u5065\u5eb7\u76ee\u7684)\u3002","title":"\u5173\u8054"},{"location":"16-4-association/#_2","text":"\u56e0\u4e3a\u5173\u8054\u662f\u4e00\u79cd\u6bd4\u8f83\u5bbd\u6cdb\u7684\u5173\u7cfb\u7c7b\u578b\uff0c\u5b83\u4eec\u53ef\u4ee5\u4ee5\u8bb8\u591a\u4e0d\u540c\u7684\u65b9\u5f0f\u5b9e\u73b0\u3002\u7136\u800c\uff0c\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5173\u8054\u662f\u4f7f\u7528\u6307\u9488\u5b9e\u73b0\u7684\uff0c\u5bf9\u8c61\u6307\u5411\u5173\u8054\u5bf9\u8c61\u3002 \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u5b9e\u73b0\u4e00\u4e2a\u53cc\u5411\u7684\u533b\u751f/\u75c5\u4eba\u5173\u7cfb\uff0c\u56e0\u4e3a\u533b\u751f\u77e5\u9053\u8c01\u662f\u4ed6\u4eec\u7684\u75c5\u4eba\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 #include // reference_wrapper #include #include #include // Since Doctor and Patient have a circular dependency, we're going to forward declare Patient class Patient ; class Doctor { private : std :: string m_name {}; std :: vector < std :: reference_wrapper < const Patient >> m_patient {}; public : Doctor ( const std :: string & name ) : m_name { name } { } void addPatient ( Patient & patient ); // We'll implement this function below Patient since we need Patient to be defined at that point friend std :: ostream & operator << ( std :: ostream & out , const Doctor & doctor ); const std :: string & getName () const { return m_name ; } }; class Patient { private : std :: string m_name {}; std :: vector < std :: reference_wrapper < const Doctor >> m_doctor {}; // so that we can use it here // We're going to make addDoctor private because we don't want the public to use it. // They should use Doctor::addPatient() instead, which is publicly exposed void addDoctor ( const Doctor & doctor ) { m_doctor . push_back ( doctor ); } public : Patient ( const std :: string & name ) : m_name { name } { } // We'll implement this function below to parallel operator<<(std::ostream&, const Doctor&) friend std :: ostream & operator << ( std :: ostream & out , const Patient & patient ); const std :: string & getName () const { return m_name ; } // We'll friend Doctor::addPatient() so it can access the private function Patient::addDoctor() friend void Doctor::addPatient ( Patient & patient ); }; void Doctor::addPatient ( Patient & patient ) { // Our doctor will add this patient m_patient . push_back ( patient ); // and the patient will also add this doctor patient . addDoctor ( * this ); } std :: ostream & operator << ( std :: ostream & out , const Doctor & doctor ) { if ( doctor . m_patient . empty ()) { out << doctor . m_name << \" has no patients right now\" ; return out ; } out << doctor . m_name << \" is seeing patients: \" ; for ( const auto & patient : doctor . m_patient ) out << patient . get (). getName () << ' ' ; return out ; } std :: ostream & operator << ( std :: ostream & out , const Patient & patient ) { if ( patient . m_doctor . empty ()) { out << patient . getName () << \" has no doctors right now\" ; return out ; } out << patient . m_name << \" is seeing doctors: \" ; for ( const auto & doctor : patient . m_doctor ) out << doctor . get (). getName () << ' ' ; return out ; } int main () { // Create a Patient outside the scope of the Doctor Patient dave { \"Dave\" }; Patient frank { \"Frank\" }; Patient betsy { \"Betsy\" }; Doctor james { \"James\" }; Doctor scott { \"Scott\" }; james . addPatient ( dave ); scott . addPatient ( dave ); scott . addPatient ( betsy ); std :: cout << james << '\\n' ; std :: cout << scott << '\\n' ; std :: cout << dave << '\\n' ; std :: cout << frank << '\\n' ; std :: cout << betsy << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 2 3 4 5 James is seeing patients: Dave Scott is seeing patients: Dave Betsy Dave is seeing doctors: James Scott Frank has no doctors right now Betsy is seeing doctors: Scott \u4e00\u822c\u6765\u8bf4\uff0c\u5982\u679c\u53ef\u4ee5\u4f7f\u7528\u5355\u5411\u5173\u8054\uff0c\u5219\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u53cc\u5411\u5173\u8054\uff0c\u56e0\u4e3a\u5b83\u4eec\u589e\u52a0\u4e86\u590d\u6742\u6027\uff0c\u800c\u4e14\u5f88\u96be\u5728\u4e0d\u51fa\u9519\u3002","title":"\u5b9e\u73b0\u5173\u8054\u5173\u7cfb"},{"location":"16-4-association/#_3","text":"\u6709\u65f6\u5bf9\u8c61\u53ef\u80fd\u4e0e\u76f8\u540c\u7c7b\u578b\u7684\u5176\u4ed6\u5bf9\u8c61\u6709\u5173\u7cfb\uff0c\u8fd9\u88ab\u79f0\u4e3a \u53cd\u8eab\u5173\u8054 \u3002\u53cd\u8eab\u5173\u8054\u7684\u4e00\u4e2a\u5f88\u597d\u7684\u4f8b\u5b50\u662f\u5927\u5b66\u8bfe\u7a0b\u548c\u5b83\u7684\u5148\u4fee\u8bfe\u7a0b(\u4e5f\u662f\u5927\u5b66\u8bfe\u7a0b)\u4e4b\u95f4\u7684\u5173\u7cfb\u3002 \u8003\u8651\u4e00\u4e2a\u7b80\u5316\u7684\u60c5\u51b5\uff0c\u5176\u4e2d\u4e00\u4e2a\u8bfe\u7a0b\u53ea\u80fd\u6709\u4e00\u4e2a\u5148\u4fee\u8bfe\u7a0b\u3002\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include class Course { private : std :: string m_name ; const Course * m_prerequisite ; public : Course ( const std :: string & name , const Course * prerequisite = nullptr ) : m_name { name }, m_prerequisite { prerequisite } { } }; \u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u7cfb\u5217\u7684\u5173\u8054(\u4e00\u95e8\u8bfe\u7a0b\u6709\u4e00\u4e2a\u5148\u51b3\u6761\u4ef6\uff0c\u53e6\u4e00\u95e8\u8bfe\u7a0b\u4e5f\u6709\u4e00\u4e2a\u5148\u51b3\u6761\u4ef6\uff0c\u7b49\u7b49\u2026\u2026)","title":"\u53cd\u8eab\u5173\u8054"},{"location":"16-4-association/#_4","text":"\u5728\u524d\u9762\u7684\u6240\u6709\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u90fd\u4f7f\u7528\u6307\u9488\u6216\u5f15\u7528\u76f4\u63a5\u5c06\u5bf9\u8c61\u8fde\u63a5\u5728\u4e00\u8d77\u3002\u7136\u800c\u5728\u5173\u8054\u4e2d\uff0c\u5e76\u4e0d\u4e00\u5b9a\u8981\u8fd9\u6837\u505a\u3002\u4efb\u4f55\u80fd\u591f\u5efa\u7acb\u4e24\u4e2a\u5bf9\u8c61\u4e4b\u95f4\u5173\u7cfb\u7684\u6570\u636e\u90fd\u53ef\u4ee5\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86 Driver \u7c7b\u5982\u4f55\u5728\u4e0d\u5b9e\u9645\u5305\u542b Car \u6307\u9488\u6216\u5f15\u7528\u6210\u5458\u7684\u60c5\u51b5\u4e0b\u4e0e Car \u5f62\u6210\u5355\u5411\u5173\u8054\u5173\u7cfb\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 #include #include class Car { private : std :: string m_name ; int m_id ; public : Car ( const std :: string & name , int id ) : m_name { name }, m_id { id } { } const std :: string & getName () const { return m_name ; } int getId () const { return m_id ; } }; // Our CarLot is essentially just a static array of Cars and a lookup function to retrieve them. // Because it's static, we don't need to allocate an object of type CarLot to use it class CarLot { private : static Car s_carLot [ 4 ]; public : CarLot () = delete ; // Ensure we don't try to create a CarLot static Car * getCar ( int id ) { for ( int count { 0 }; count < 4 ; ++ count ) { if ( s_carLot [ count ]. getId () == id ) { return & ( s_carLot [ count ]); } } return nullptr ; } }; Car CarLot :: s_carLot [ 4 ]{ { \"Prius\" , 4 }, { \"Corolla\" , 17 }, { \"Accord\" , 84 }, { \"Matrix\" , 62 } }; class Driver { private : std :: string m_name ; int m_carId ; // we're associated with the Car by ID rather than pointer public : Driver ( const std :: string & name , int carId ) : m_name { name }, m_carId { carId } { } const std :: string & getName () const { return m_name ; } int getCarId () const { return m_carId ; } }; int main () { Driver d { \"Franz\" , 17 }; // Franz is driving the car with ID 17 Car * car { CarLot :: getCar ( d . getCarId ()) }; // Get that car from the car lot if ( car ) std :: cout << d . getName () << \" is driving a \" << car -> getName () << '\\n' ; else std :: cout << d . getName () << \" couldn't find his car \\n \" ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c CarLot \u6301\u6709 car \u3002\u800c Driver \u4f5c\u4e3a\u672c\u8eab\u9700\u8981 car \u7684\u5bf9\u8c61\uff0c\u5374\u5e76\u6ca1\u6709\u6307\u5411 Car \u7684\u6307\u9488\u2014\u2014\u4f46\u662f\u5b83\u6709\u8f66\u7684ID\uff0c\u5b83\u53ef\u4ee5\u901a\u8fc7\u8be5ID\u5728\u9700\u8981\u65f6\u4ece CarLot \u4e2d\u83b7\u53d6 car \u3002 \u5728\u8fd9\u4e2a\u7279\u5b9a\u7684\u793a\u4f8b\u4e2d\uff0c\u8fd9\u6837\u505a\u6709\u70b9\u611a\u8822\uff0c\u56e0\u4e3a\u4ece CarLot \u4e2d\u83b7\u53d6 Car \u9700\u8981\u4f4e\u6548\u7684\u67e5\u627e(\u4e4b\u95f4\u76f4\u63a5\u4f7f\u7528\u6307\u9488\u8981\u5feb\u5f97\u591a)\u3002\u7136\u800c\uff0c\u901a\u8fc7\u552f\u4e00ID\u800c\u4e0d\u662f\u6307\u9488\u5f15\u7528\u4e1c\u897f\u6709\u4e00\u4e9b\u4f18\u70b9\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5f15\u7528\u5f53\u524d\u4e0d\u5728\u5185\u5b58\u4e2d\u7684\u5185\u5bb9(\u5b83\u4eec\u53ef\u80fd\u5728\u6587\u4ef6\u4e2d\uff0c\u6216\u8005\u5728\u6570\u636e\u5e93\u4e2d\uff0c\u5e76\u4e14\u53ef\u4ee5\u6309\u9700\u52a0\u8f7d)\u3002\u6b64\u5916\uff0c\u6307\u9488\u53ef\u80fd\u5360\u75284\u62168\u4e2a\u5b57\u8282\u2014\u2014\u5982\u679c\u7a7a\u95f4\u975e\u5e38\u6709\u9650\uff0c\u4e14\u60df\u4e00\u5bf9\u8c61\u7684\u6570\u91cf\u76f8\u5f53\u4f4e\uff0c\u90a3\u4e48\u901a\u8fc78\u4f4d\u621616\u4f4d\u6574\u6570\u5f15\u7528\u5b83\u4eec\u53ef\u4ee5\u8282\u7701\u5927\u91cf\u5185\u5b58\u3002","title":"\u5173\u8054\u53ef\u4ee5\u662f\u95f4\u63a5\u7684"},{"location":"16-4-association/#vs-vs","text":"\u4e0b\u9762\u662f\u4e00\u4e2a\u6c47\u603b\u8868\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8bb0\u4f4f\u7ec4\u5408\u3001\u805a\u5408\u548c\u5173\u8054\u4e4b\u95f4\u7684\u533a\u522b: \u5c5e\u6027 \u7ec4\u5408 \u805a\u5408 \u5173\u8054 \u5173\u7cfb\u7c7b\u578b \u6574\u4f53-\u90e8\u5206 \u6574\u4f53-\u90e8\u5206 \u672c\u4e0d\u76f8\u5173 \u6210\u5458\u662f\u5426\u53ef\u4ee5\u5c5e\u4e8e\u4e0d\u540c\u7684\u6574\u4f53\u5bf9\u8c61 No Yes Yes \u6210\u5458\u7684\u5b58\u5728\u6027\u662f\u5426\u7531\u6574\u4f53\u7ba1\u7406 Yes No No \u65b9\u5411 \u5355\u5411 \u5355\u5411 \u5355\u5411\u6216\u53cc\u5411 \u5173\u7cfb\u52a8\u8bcd Part-of Has-a Uses-a","title":"\u7ec4\u5408 vs \u805a\u5408 vs \u5173\u8054"},{"location":"16-5-dependencies/","tags":["object","dependencies"],"text":"Key Takeaway - \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u63a2\u8ba8\u4e863\u79cd\u7c7b\u578b\u7684\u5173\u7cfb\uff1a\u7ec4\u5408\u3001\u805a\u5408\u548c\u5173\u8054\u3002\u6211\u4eec\u628a\u6700\u7b80\u5355\u7684\u4e00\u79cd\u5173\u7cfb\u653e\u5728\u6700\u540e\u4ecb\u7ecd\u2014\u2014\u4f9d\u8d56\u5173\u7cfb\u3002 \u5728\u65e5\u5e38\u4ea4\u8c08\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u672f\u8bed\u4f9d\u8d56\u6027\u6765\u8868\u793a\u4e00\u4e2a\u5bf9\u8c61\u4f9d\u8d56\u4e8e\u53e6\u4e00\u4e2a\u5bf9\u8c61\u6765\u5b8c\u6210\u7ed9\u5b9a\u7684\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u7684\u811a\u9aa8\u6298\u4e86\uff0c\u4f60\u5c31\u9700\u8981\u4f9d\u9760\u62d0\u6756\u6765\u8d70\u52a8\u3002\u82b1\u6735\u4f9d\u9760\u871c\u8702\u6388\u7c89\uff0c\u624d\u80fd\u7ed3\u51fa\u679c\u5b9e\u6216\u7e41\u6b96\u540e\u4ee3\u3002 \u5f53\u4e00\u4e2a\u5bf9\u8c61\u4e3a\u4e86\u5b8c\u6210\u67d0\u4e9b\u7279\u5b9a\u4efb\u52a1\u800c\u8c03\u7528\u53e6\u4e00\u4e2a\u5bf9\u8c61\u7684\u529f\u80fd\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u4f9d\u8d56\u5173\u7cfb\u3002\u8fd9\u662f\u4e00\u79cd\u6bd4\u5173\u8054\u5f31\u7684\u5173\u7cfb\uff0c\u4f46\u662f\uff0c\u5bf9\u6240\u4f9d\u8d56\u5bf9\u8c61\u7684\u4efb\u4f55\u66f4\u6539\u90fd\u53ef\u80fd\u7834\u574f(\u4f9d\u8d56\u7684)\u8c03\u7528\u8005\u7684\u529f\u80fd\u3002\u4f9d\u8d56\u5173\u7cfb\u603b\u662f\u5355\u5411\u7684\u5173\u7cfb\u3002 \u4e00\u4e2a\u60a8\u5df2\u7ecf\u89c1\u8fc7\u5f88\u591a\u6b21\u7684\u4f9d\u8d56\u7684\u597d\u4f8b\u5b50\u662f std::ostream \u3002\u6211\u4eec\u4f7f\u7528 std::ostream \u7684\u7c7b\u4f7f\u7528\u5b83\u662f\u4e3a\u4e86\u5b8c\u6210\u5411\u63a7\u5236\u53f0\u6253\u5370\u5185\u5bb9\u7684\u4efb\u52a1\uff0c\u800c\u4e0d\u662f\u4e3a\u4e86\u5176\u4ed6\u76ee\u7684\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include class Point { private : double m_x {}; double m_y {}; double m_z {}; public : Point ( double x = 0.0 , double y = 0.0 , double z = 0.0 ) : m_x { x }, m_y { y }, m_z { z } { } friend std :: ostream & operator << ( std :: ostream & out , const Point & point ); // Point has a dependency on std::ostream here }; std :: ostream & operator << ( std :: ostream & out , const Point & point ) { // Since operator<< is a friend of the Point class, we can access Point's members directly. out << \"Point(\" << point . m_x << \", \" << point . m_y << \", \" << point . m_z << ')' ; return out ; } int main () { Point point1 { 2.0 , 3.0 , 4.0 }; std :: cout << point1 ; // the program has a dependency on std::cout here return 0 ; } \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c Point \u4e0e std::ostream \u6ca1\u6709\u76f4\u63a5\u5173\u7cfb\uff0c\u4f46\u662f\u5b83\u4f9d\u8d56\u4e8e std::ostream \uff0c\u56e0\u4e3a\u64cd\u4f5c\u7b26 << \u4f7f\u7528 std::ostream \u5c06 Point \u6253\u5370\u5230\u63a7\u5236\u53f0\u3002 C++\u4e2d\u7684\u4f9d\u8d56\u548c\u5173\u8054 \u4f9d\u8d56\u5173\u7cfb\u548c\u5173\u8054\u4e4b\u95f4\u7684\u533a\u522b\u901a\u5e38\u5bb9\u6613\u6df7\u6dc6\u3002 \u5728C++\u4e2d\uff0c\u5173\u8054\u662f\u7c7b\u7ea7\u522b\u4e0a\u4e24\u4e2a\u7c7b\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u4e00\u4e2a\u7c7b\u4fdd\u6301\u4e0e\u5173\u8054\u7c7b\u7684\u76f4\u63a5\u6216\u95f4\u63a5\u201c\u94fe\u63a5\u201d\u4f5c\u4e3a\u6210\u5458\u3002\u4f8b\u5982\uff0c Doctor \u7c7b\u6709\u4e00\u4e2a\u6307\u5411\u5176 Patients \u6210\u5458\u7684\u6307\u9488\u6570\u7ec4\u3002\u4f60\u53ef\u4ee5\u95ee\u533b\u751f\u8c01\u662f\u4ed6\u7684\u75c5\u4eba\u3002 Driver \u4e2d\u6709\u4e00\u4e2a\u6574\u578b\u6210\u5458\uff0c\u8868\u793a driver \u5bf9\u8c61\u62e5\u6709\u7684 Car \u3002\u9a7e\u9a76\u5458\u603b\u662f\u77e5\u9053\u4e0e\u5b83\u76f8\u5173\u8054\u7684\u662f\u4ec0\u4e48\u8f66\u3002 \u4f9d\u8d56\u9879\u901a\u5e38\u4e0d\u662f\u7c7b\u5c42\u9762\u7684\u6982\u5ff5\u2014\u2014\u4e5f\u5c31\u662f\u8bf4\uff0c\u6240\u4f9d\u8d56\u7684\u5bf9\u8c61\u4e0d\u662f\u4f5c\u4e3a\u7c7b\u6210\u5458\u51fa\u73b0\u7684\u3002\u76f8\u53cd\uff0c\u6240\u4f9d\u8d56\u7684\u5bf9\u8c61\u901a\u5e38\u6839\u636e\u9700\u8981\u5b9e\u4f8b\u5316(\u5982\u6253\u5f00\u6587\u4ef6\u5199\u5165\u6570\u636e)\uff0c\u6216\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u5230\u51fd\u6570\u4e2d(\u5982\u4e0a\u9762\u91cd\u8f7d\u64cd\u4f5c\u7b26 << \u4e2d\u7684 std::ostream )\u3002 \u8f7b\u677e\u4e00\u523b \u4f9d\u8d56 (\u8f6c\u8f7d\u81ea xkcd )\uff1a \u5f53\u7136\uff0c\u6211\u4eec\u5176\u5b9e\u90fd\u77e5\u9053\uff0c\u8fd9\u5b9e\u9645\u4e0a\u662f\u4e00\u79cd \u53cd\u8eab\u5173\u8054 \u3002","title":"16.5 - \u4f9d\u8d56\u5173\u7cfb"},{"location":"16-5-dependencies/#c","text":"\u4f9d\u8d56\u5173\u7cfb\u548c\u5173\u8054\u4e4b\u95f4\u7684\u533a\u522b\u901a\u5e38\u5bb9\u6613\u6df7\u6dc6\u3002 \u5728C++\u4e2d\uff0c\u5173\u8054\u662f\u7c7b\u7ea7\u522b\u4e0a\u4e24\u4e2a\u7c7b\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u4e00\u4e2a\u7c7b\u4fdd\u6301\u4e0e\u5173\u8054\u7c7b\u7684\u76f4\u63a5\u6216\u95f4\u63a5\u201c\u94fe\u63a5\u201d\u4f5c\u4e3a\u6210\u5458\u3002\u4f8b\u5982\uff0c Doctor \u7c7b\u6709\u4e00\u4e2a\u6307\u5411\u5176 Patients \u6210\u5458\u7684\u6307\u9488\u6570\u7ec4\u3002\u4f60\u53ef\u4ee5\u95ee\u533b\u751f\u8c01\u662f\u4ed6\u7684\u75c5\u4eba\u3002 Driver \u4e2d\u6709\u4e00\u4e2a\u6574\u578b\u6210\u5458\uff0c\u8868\u793a driver \u5bf9\u8c61\u62e5\u6709\u7684 Car \u3002\u9a7e\u9a76\u5458\u603b\u662f\u77e5\u9053\u4e0e\u5b83\u76f8\u5173\u8054\u7684\u662f\u4ec0\u4e48\u8f66\u3002 \u4f9d\u8d56\u9879\u901a\u5e38\u4e0d\u662f\u7c7b\u5c42\u9762\u7684\u6982\u5ff5\u2014\u2014\u4e5f\u5c31\u662f\u8bf4\uff0c\u6240\u4f9d\u8d56\u7684\u5bf9\u8c61\u4e0d\u662f\u4f5c\u4e3a\u7c7b\u6210\u5458\u51fa\u73b0\u7684\u3002\u76f8\u53cd\uff0c\u6240\u4f9d\u8d56\u7684\u5bf9\u8c61\u901a\u5e38\u6839\u636e\u9700\u8981\u5b9e\u4f8b\u5316(\u5982\u6253\u5f00\u6587\u4ef6\u5199\u5165\u6570\u636e)\uff0c\u6216\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u5230\u51fd\u6570\u4e2d(\u5982\u4e0a\u9762\u91cd\u8f7d\u64cd\u4f5c\u7b26 << \u4e2d\u7684 std::ostream )\u3002","title":"C++\u4e2d\u7684\u4f9d\u8d56\u548c\u5173\u8054"},{"location":"16-5-dependencies/#_1","text":"\u4f9d\u8d56 (\u8f6c\u8f7d\u81ea xkcd )\uff1a \u5f53\u7136\uff0c\u6211\u4eec\u5176\u5b9e\u90fd\u77e5\u9053\uff0c\u8fd9\u5b9e\u9645\u4e0a\u662f\u4e00\u79cd \u53cd\u8eab\u5173\u8054 \u3002","title":"\u8f7b\u677e\u4e00\u523b"},{"location":"16-6-container-classes/","tags":["object","container classes"],"text":"Key Takeaway - \u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\u6211\u4eec\u7ecf\u5e38\u4f1a\u7528\u5230\u5bb9\u5668\u3002\u9ea6\u7247\u662f\u88c5\u5728\u76d2\u5b50\u91cc\u7684\uff0c\u4e66\u4e5f\u6709\u5c01\u9762\u548c\u88c5\u8ba2\uff0c\u4f60\u7684\u8f66\u5e93\u91cc\u5e94\u8be5\u4e5f\u88c5\u4e86\u4e0d\u5c11\u4e1c\u897f\u5427\u3002\u5982\u679c\u6ca1\u6709\u5bb9\u5668\uff0c\u4f7f\u7528\u8fd9\u4e9b\u4e1c\u897f\u7684\u65f6\u5019\u4e00\u5b9a\u4f1a\u975e\u5e38\u7684\u6df7\u4e71\u3002\u5bb9\u5668\u6700\u5927\u7684\u529f\u80fd\uff0c\u5c31\u662f\u5e2e\u52a9\u6211\u4eec\u7ec4\u7ec7\u548c\u5b58\u50a8\u5b58\u5728\u5bb9\u5668\u4e2d\u7684\u4e1c\u897f\u3002 \u7c7b\u4f3c\u7684\uff0c \u5bb9\u5668\u7c7b \u662f\u4e00\u79cd\u88ab\u8bbe\u8ba1\u7528\u6765\u5b58\u653e\u548c\u7ec4\u7ec7\u591a\u79cd\u5176\u4ed6\u7c7b\u578b\uff08\u53ef\u4ee5\u662f\u7c7b\u4e5f\u53ef\u4ee5\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff09\u5b9e\u4f8b\u7684\u7c7b\u3002\u5bb9\u5668\u7c7b\u7684\u79cd\u7c7b\u6709\u5f88\u591a\uff0c\u6bcf\u79cd\u90fd\u6709\u5176\u81ea\u5df1\u7684\u4f18\u52bf\u548c\u52a3\u52bf\uff0c\u4ee5\u53ca\u4f7f\u7528\u9650\u5236\u3002\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6700\u6709\u7528\u7684\u5bb9\u5668\u662f\u6570\u7ec4\uff0c\u4f60\u5728\u524d\u9762\u7684\u4f8b\u5b50\u4e2d\u4ee5\u53ca\u591a\u6b21\u4f7f\u7528\u5b83\u4e86\u3002\u5c3d\u7ba1C++ \u63d0\u4f9b\u4e86\u5185\u5efa\u7684\u6570\u7ec4\u529f\u80fd\uff0c\u7a0b\u5e8f\u5458\u5374\u5927\u591a\u4f7f\u7528\u6570\u7ec4\u5bb9\u5668\u7c7b\uff08 std::array \u6216 std::vector ) \u3002\u548c\u5185\u5efa\u7684\u6570\u7ec4\u4e0d\u540c\uff0c\u8fd9\u4e9b\u6570\u7ec4\u5bb9\u5668\u7c7b\u901a\u5e38\u53ef\u4ee5\u52a8\u6001\u5730\u8c03\u6574\u5176\u5927\u5c0f\uff08\u5f53\u8d44\u6e90\u88ab\u6dfb\u52a0\u6216\u5220\u9664\u65f6\uff09\u3001\u53ef\u4ee5\u5728\u4f20\u9012\u5230\u51fd\u6570\u65f6\u4fdd\u7559\u5176\u5927\u5c0f\u4fe1\u606f\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u8fb9\u754c\u68c0\u67e5\u3002\u8fd9\u4e9b\u529f\u80fd\u4e0d\u4f46\u4f7f\u5f97\u6570\u7ec4\u5bb9\u5668\u7c7b\u7b80\u5355\u6613\u7528\uff0c\u800c\u4e14\u5b89\u5168\u6027\u4e5f\u66f4\u597d\u3002 \u5bb9\u5668\u7c7b\u901a\u5e38\u4f1a\u5b9e\u73b0\u4e00\u7cfb\u5217\u6807\u51c6\u7684\u3001\u6700\u5c0f\u5316\u7684\u529f\u80fd\u3002\u5927\u591a\u6570\u5b9e\u73b0\u826f\u597d\u7684\u5bb9\u5668\u7c7b\u901a\u5e38\u5305\u62ec\u4e0b\u9762\u8fd9\u4e9b\u529f\u80fd\uff1a \u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u5bb9\u5668\uff08\u901a\u8fc7 \u6784\u9020\u51fd\u6570 \uff09\uff1b \u5411\u5bb9\u5668\u6dfb\u52a0\u4e00\u4e2a\u5bf9\u8c61\uff1b \u4ece\u5bb9\u5668\u4e2d\u5220\u9664\u4e00\u4e2a\u5bf9\u8c61\uff1b \u62a5\u544a\u5bb9\u5668\u4e2d\u5bf9\u8c61\u7684\u4e2a\u6570\uff1b \u6e05\u7a7a\u5bb9\u5668\uff1b \u63d0\u4f9b\u5b58\u50a8\u5bf9\u8c61\u7684\u8bbf\u95ee\u65b9\u5f0f\uff1b \u5143\u7d20\u6392\u5e8f\uff08\u53ef\u9009\u7684\uff09\u3002 \u6709\u4e9b\u5bb9\u5668\u4f1a\u5ffd\u7565\u4e0a\u8ff0\u529f\u80fd\u4e2d\u7684\u67d0\u4e9b\u3002\u4f8b\u5982\uff0c\u6570\u7ec4\u5bb9\u5668\u7c7b\u901a\u5e38\u4f1a\u5ffd\u7565\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\uff0c\u56e0\u4e3a\u6570\u7ec4\u6570\u636e\u7ed3\u6784\u5728\u8fdb\u884c\u63d2\u5165\u548c\u5220\u9664\u65f6\u6548\u7387\u5f88\u4f4e\uff0c\u6240\u4ee5\u7c7b\u7684\u8bbe\u8ba1\u8005\u4e0d\u9f13\u52b1\u6211\u4eec\u4f7f\u7528\u8fd9\u4e24\u79cd\u64cd\u4f5c\u3002 \u5bb9\u5668\u7c7b\u5b9e\u73b0\u4e86\u4e00\u79cd \u6210\u5458\u5173\u7cfb \u3002\u4f8b\u5982\uff0c\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u662f\u8be5\u6570\u7ec4\u7684\u4e00\u4e2a\u6210\u5458\uff08\u5c5e\u4e8e\uff09\u3002\u6ce8\u610f\uff0c\u6211\u4eec\u8fd9\u91cc\u8bf4\u7684\u6210\u5458\u662f\u5e73\u5e38\u610f\u4e49\u4e0a\u7684\u6210\u5458\uff0c\u800c\u4e0d\u662f\u6307 \u7c7b\u6210\u5458 \u3002 \u5bb9\u5668\u7c7b\u578b \u5bb9\u5668\u7c7b\u901a\u5e38\u6709\u4e24\u4e2a\u53d8\u79cd\u3002 \u503c\u5bb9\u5668 \u662f \u7ec4\u5408\u5173\u7cfb \uff0c\u5b83\u5b58\u653e\u4e86\u5176\u5143\u7d20\u7684\u62f7\u8d1d\uff08\u56e0\u6b64\u5fc5\u987b\u8d1f\u8d23\u521b\u5efa\u548c\u9500\u6bc1\u8fd9\u4e9b\u62f7\u8d1d\uff09\u3002 \u5f15\u7528\u5bb9\u5668 \u5219\u662f \u805a\u5408\u5173\u7cfb \uff0c\u5b83\u4fdd\u5b58\u4e86\u5143\u7d20\u7684\u6307\u9488\u6216\u5f15\u7528\uff08\u56e0\u6b64\u65e0\u9700\u64cd\u5fc3\u5b83\u4eec\u7684\u521b\u5efa\u548c\u9500\u6bc1\uff09\u3002 \u6570\u7ec4\u5bb9\u5668\u7c7b \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ece\u5934\u7f16\u5199\u4e00\u4e2a\u6574\u578b\u6570\u7ec4\u7c7b\u5e76\u5b9e\u73b0\u5b83\u5e94\u8be5\u63d0\u4f9b\u7684\u5927\u90e8\u5206\u529f\u80fd\u3002\u8be5\u6570\u7ec4\u7c7b\u662f\u4e00\u4e2a \u503c\u5bb9\u5668 \uff0c\u5b83\u4fdd\u5b58\u5143\u7d20\u7684\u62f7\u8d1d\u3002\u548c\u5b83\u4eec\u7684\u540d\u5b57\u6697\u793a\u7684\u90a3\u6837\uff0c\u8fd9\u4e2a\u5bb9\u5668\u4f1a\u4fdd\u5b58\u4e00\u4e2a\u6574\u578b\u6570\u6784\u6210\u7684\u6570\u7ec4\uff0c\u7c7b\u4f3c std::vector \u3002 \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u521b\u5efa IntArray.h \u6587\u4ef6\uff1a 1 2 3 4 5 6 7 8 #ifndef INTARRAY_H #define INTARRAY_H class IntArray { }; #endif IntArray \u9700\u8981\u8ffd\u8e2a\u4e24\u4e2a\u503c\uff1a\u6570\u636e\u548c\u6570\u7ec4\u5927\u5c0f\u3002\u56e0\u4e3a\u6211\u4eec\u5e0c\u671b\u6570\u7ec4\u80fd\u591f\u6539\u53d8\u5176\u5927\u5c0f\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u52a8\u6001\u5206\u914d\u5185\u5b58\uff0c\u56e0\u6b64\u5fc5\u987b\u4f7f\u7528\u6307\u9488\u6765\u4fdd\u5b58\u6570\u636e\u3002 1 2 3 4 5 6 7 8 9 10 11 #ifndef INTARRAY_H #define INTARRAY_H class IntArray { private : int m_length {}; int * m_data {}; }; #endif \u63a5\u4e0b\u6765\uff0c\u6dfb\u52a0\u7528\u4e8e\u521b\u5efa IntArrays \u7684\u6784\u9020\u51fd\u6570\u3002\u8fd9\u91cc\u6211\u4eec\u9700\u8981\u4e24\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u4e00\u4e2a\u521b\u5efa\u7a7a\u7684\u6570\u7ec4\uff0c\u4e00\u4e2a\u5219\u521b\u5efa\u9884\u5b9a\u4e49\u957f\u5ea6\u7684\u6570\u7ec4\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #ifndef INTARRAY_H #define INTARRAY_H #include // for assert() class IntArray { private : int m_length {}; int * m_data {}; public : IntArray () = default ; IntArray ( int length ) : m_length { length } { assert ( length >= 0 ); if ( length > 0 ) m_data = new int [ length ]{}; } }; #endif \u6211\u4eec\u8fd8\u9700\u8981\u521b\u5efa\u7528\u4e8e\u6e05\u7406\u6570\u7ec4\u7684\u51fd\u6570\u3002\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a \u6790\u6784\u51fd\u6570 \uff0c\u5b83\u7684\u529f\u80fd\u5c31\u662f\u91ca\u653e\u6570\u636e\u5360\u7528\u7684\u5185\u5b58\u3002\u7136\u540e\uff0c\u518d\u7f16\u5199\u4e00\u4e2a erase() \u51fd\u6570\u5c06\u6570\u7ec4\u7684\u5143\u7d20\u6e05\u9664\u5e76\u5c06\u957f\u5ea6\u8bbe\u7f6e\u4e3a0\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ~ IntArray () { delete [] m_data ; // \u8fd9\u91cc\u4e0d\u5fc5\u5c06 m_data \u8bbe\u7f6e\u4e3a null \u4e5f\u4e0d\u7528\u5c06 m_length \u8bbe\u7f6e\u4e3a0\uff0c\u56e0\u4e3a\u5bf9\u8c61\u9a6c\u4e0a\u5c31\u4f1a\u88ab\u9500\u6bc1 } void erase () { delete [] m_data ; // \u5fc5\u987b\u786e\u4fdd m_data \u662f nullptr \u5426\u5219\u5c06\u4f1a\u79f0\u4e3a\u60ac\u5782\u6307\u9488 m_data = nullptr ; m_length = 0 ; } \u73b0\u5728\uff0c \u91cd\u8f7d [] \u64cd\u4f5c\u7b26\u7528\u4e8e\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u5728\u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26\u7684\u65f6\u5019\uff0c\u5fc5\u987b\u786e\u4fdd\u7d22\u5f15\u662f\u5408\u6cd5\u7684\uff0c\u8be5\u64cd\u4f5c\u53ef\u4ee5\u4f7f\u7528 assert() \u51fd\u6570\u3002\u6211\u4eec\u8fd8\u9700\u8981\u6dfb\u52a0\u4e00\u4e2a\u51fd\u6570\u7528\u4e8e\u8fd4\u56de\u6570\u7ec4\u957f\u5ea6\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u7c7b\u770b\u4e0a\u53bb\u4f1a\u5411\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #ifndef INTARRAY_H #define INTARRAY_H #include // for assert() class IntArray { private : int m_length {}; int * m_data {}; public : IntArray () = default ; IntArray ( int length ) : m_length { length } { assert ( length >= 0 ); if ( length > 0 ) m_data = new int [ length ]{}; } ~ IntArray () { delete [] m_data ; // \u8fd9\u91cc\u4e0d\u5fc5\u5c06 m_data \u8bbe\u7f6e\u4e3a null \u4e5f\u4e0d\u7528\u5c06 m_length \u8bbe\u7f6e\u4e3a0\uff0c\u56e0\u4e3a\u5bf9\u8c61\u9a6c\u4e0a\u5c31\u4f1a\u88ab\u9500\u6bc1 } void erase () { delete [] m_data ; // \u5fc5\u987b\u786e\u4fdd m_data \u662f nullptr \u5426\u5219\u5c06\u4f1a\u79f0\u4e3a\u60ac\u5782\u6307\u9488 m_data = nullptr ; m_length = 0 ; } int & operator []( int index ) { assert ( index >= 0 && index < m_length ); return m_data [ index ]; } int getLength () const { return m_length ; } }; #endif \u81f3\u6b64\uff0c IntArray \u7c7b\u5df2\u7ecf\u5b9e\u73b0\u5b8c\u6210\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e86\u3002\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6709\u9884\u5b9a\u4e49\u957f\u5ea6\u7684 IntArrays \uff0c\u7136\u540e\u4f7f\u7528\u4e0b\u6807\u8fd0\u7b97\u7b26\u83b7\u53d6\u5143\u7d20\u7684\u503c\u3002 \u4f46\u662f\uff0c IntArray \u4ecd\u7136\u8fd8\u6709\u4e9b\u529f\u80fd\u6ca1\u6709\u88ab\u5b9e\u73b0\u3002\u6570\u7ec4\u5927\u5c0f\u6682\u65f6\u8fd8\u4e0d\u80fd\u6539\u53d8\u3001\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4e5f\u8fd8\u4e0d\u80fd\u88ab\u63d2\u5165\u6216\u5220\u9664\u3001\u4e0d\u4ec5\u5982\u6b64\uff0c\u6570\u7ec4\u5143\u7d20\u4e5f\u8fd8\u4e0d\u80fd\u88ab\u6392\u5e8f\u3002 \u9996\u5148\uff0c\u7f16\u5199\u4ee3\u7801\u4f7f\u5f97\u6570\u7ec4\u53ef\u4ee5\u6539\u53d8\u5176\u5c3a\u5bf8\u3002\u800c\u4e14\uff0c\u6211\u4eec\u4f1a\u5b9e\u73b0\u4e24\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u7b2c\u4e00\u4e2a\u51fd\u6570\u662f reallocate() \uff0c\u8fd9\u4e2a\u51fd\u6570\u5728\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\u7684\u65f6\u5019\uff0c\u4f1a\u9500\u6bc1\u6570\u7ec4\u4e2d\u7684\u5168\u90e8\u5143\u7d20\uff0c\u4f46\u662f\u5b83\u7684\u901f\u5ea6\u5f88\u5feb\u3002\u7b2c\u4e8c\u4e2a\u51fd\u6570\u662f resize() \uff0c\u5b83\u5728\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u4fdd\u6301\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u4f46\u662f\u8be5\u51fd\u6570\u7684\u6548\u7387\u4f1a\u4f4e\u4e00\u4e9b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 // reallocate \u4f1a\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\uff0c\u4f46\u662f\u5b83\u4f1a\u5728\u5de5\u4f5c\u65f6\u9500\u6bc1\u6240\u6709\u7684\u6570\u7ec4\u5143\u7d20\u3002\u8be5\u51fd\u6570\u7684\u901f\u5ea6\u5f88\u5feb void reallocate ( int newLength ) { // \u9996\u5148\uff0c\u5220\u9664\u5168\u90e8\u7684\u5143\u7d20 erase (); // \u5982\u679c\u8981\u521b\u5efa\u7a7a\u6570\u7ec4\uff0c\u6b64\u5904\u8fd4\u56de\u5373\u53ef if ( newLength <= 0 ) return ; // \u5206\u914d\u65b0\u7684\u5143\u7d20 m_data = new int [ newLength ]; m_length = newLength ; } // resize \u4f1a\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\uff0c\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4f1a\u88ab\u4fdd\u5b58\u4e0b\u6765\u3002\u8be5\u51fd\u6570\u7684\u901f\u5ea6\u6bd4\u8f83\u6162 void resize ( int newLength ) { // \u5982\u679c\u6570\u7ec4\u7684\u957f\u5ea6\u5df2\u7ecf\u6ee1\u8db3\u8981\u6c42\uff0c\u5b8c\u6210 if ( newLength == m_length ) return ; // \u5982\u679c\u9700\u8981\u5c06\u6570\u7ec4\u8c03\u6574\u4e3a\u4e00\u4e2a\u7a7a\u6570\u7ec4\uff0c\u5b8c\u6210\u5e76\u8fd4\u56de if ( newLength <= 0 ) { erase (); return ; } // \u5047\u8bbe newLength \u5927\u4e8e\u7b49\u4e8e1\u3002\u8be5\u7b97\u6cd5\u7684\u539f\u7406\u5982\u4e0b\uff1a // \u9996\u5148\uff0c\u5206\u914d\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\u3002\u7136\u540e\u5c06\u65e7\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u90fd\u62f7\u8d1d\u5230\u65b0\u7684\u6570\u7ec4\u4e2d\u3002\u5b8c\u6210\u540e\uff0c\u9500\u6bc1\u65e7\u6570\u7ec4\u3002 // \u7136\u540e\u8ba9 m_data \u6307\u5411\u65b0\u7684\u6570\u7ec4 // \u5206\u914d\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4 int * data { new int [ newLength ] }; // \u786e\u5b9a\u6709\u591a\u5c11\u4e2a\u5143\u7d20\u9700\u8981\u4ece\u65e7\u6570\u7ec4\u4e2d\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\u3002 // \u8fd9\u91cc\u6211\u4eec\u5e0c\u671b\u4ece\u65e7\u6570\u7ec4\u4e2d\u62f7\u8d1d\u5c3d\u53ef\u80fd\u591a\u7684\u5143\u7d20\uff08\u65b0\u6570\u7ec4\u4e0d\u4e00\u5b9a\u6bd4\u65e7\u6570\u7ec4\u5927\uff09 // \u62f7\u8d1d\u7684\u5143\u7d20\u4e2a\u6570\u662f\u65b0\u65e7\u4e24\u4e2a\u6570\u7ec4\u4e2d\u8f83\u5c0f\u7684\u4e00\u4e2a if ( m_length > 0 ) { int elementsToCopy { ( newLength > m_length ) ? m_length : newLength }; // Now copy the elements one by one for ( int index { 0 }; index < elementsToCopy ; ++ index ) data [ index ] = m_data [ index ]; } // \u5220\u9664\u65e7\u6570\u7ec4 delete [] m_data ; // \u5f00\u59cb\u4f7f\u7528\u65b0\u7684\u6570\u7ec4\u4f5c\u4e3a\u6570\u636e\uff01\u6700\u7b80\u5355\u7684\u529e\u6cd5\u5c31\u662f\u8ba9 m_data\u6307\u5411\u65b0\u6570\u7ec4\u7684\u5730\u5740\u3002 // \u7531\u4e8e\u8be5\u6570\u7ec4\u662f\u52a8\u6001\u5206\u914d\u7684\uff0c\u6240\u4ee5\u4e0d\u4f1a\u5728\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\u88ab\u9500\u6bc1 m_data = data ; m_length = newLength ; } \u8fd8\u633a\u590d\u6742\uff01 \u591a\u6570\u7684\u6570\u7ec4\u5bb9\u5668\u7c7b\u4f1a\u6b62\u6b65\u4e8e\u6b64\u3002\u4e0d\u8fc7\uff0c\u4e5f\u8bb8\u4f60\u60f3\u77e5\u9053\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u5e94\u8be5\u5982\u4f55\u5b9e\u73b0\uff0c\u6240\u4ee5\u6211\u4eec\u7ee7\u7eed\u5b9e\u73b0\u8fd9\u4e24\u4e2a\u529f\u80fd\u3002\u5176\u5b9e\u73b0\u65b9\u6cd5\u548c resize() \u975e\u5e38\u7c7b\u4f3c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 void insertBefore ( int value , int index ) { // \u68c0\u67e5\u6570\u7ec4\u7d22\u5f15\u5408\u6cd5\u6027 assert ( index >= 0 && index <= m_length ); // \u9996\u5148\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u5176\u957f\u5ea6\u6bd4\u65e7\u6570\u7ec4\u59271 int * data { new int [ m_length + 1 ] }; // \u62f7\u8d1d\u5230\u5f53\u524d\u7d22\u5f15\u7684\u6240\u6709\u5143\u7d20 for ( int before { 0 }; before < index ; ++ before ) data [ before ] = m_data [ before ]; // \u63d2\u5165\u65b0\u7684\u5143\u7d20\u5230\u65b0\u6570\u7ec4 data [ index ] = value ; // \u7ee7\u7eed\u62f7\u8d1d\u5269\u4f59\u7684\u5143\u7d20 for ( int after { index }; after < m_length ; ++ after ) data [ after + 1 ] = m_data [ after ]; // \u6700\u540e\uff0c\u5220\u9664\u65e7\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528\u65b0\u7684\u6570\u7ec4\u4f5c\u4e3a\u6570\u636e delete [] m_data ; m_data = data ; ++ m_length ; } void remove ( int index ) { // \u68c0\u67e5\u6570\u7ec4\u7d22\u5f15\u5408\u6cd5\u6027 assert ( index >= 0 && index < m_length ); // \u5982\u679c\u8fd9\u5df2\u7ecf\u662f\u6700\u540e\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20\uff0c\u5c06\u6570\u7ec4\u8bbe\u7f6e\u4e3a\u7a7a\u6570\u7ec4\u7136\u540e\u5b8c\u6210 if ( m_length == 1 ) { erase (); return ; } // \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5176\u5927\u5c0f\u6bd4\u65e7\u6570\u7ec4\u5c0f1 int * data { new int [ m_length -1 ] }; // \u62f7\u8d1d\u5230\u7d22\u5f15\u7684\u5168\u90e8\u5143\u7d20 for ( int before { 0 }; before < index ; ++ before ) data [ before ] = m_data [ before ]; // \u7ee7\u7eed\u62f7\u8d1d\u88ab\u5220\u9664\u5143\u7d20\u540e\u9762\u7684\u6240\u6709\u5143\u7d20 for ( int after { index + 1 }; after < m_length ; ++ after ) data [ after -1 ] = m_data [ after ]; // \u6700\u540e\uff0c\u5220\u9664\u65e7\u6570\u7ec4\u5e76\u4f7f\u7528\u65b0\u7684\u6570\u7ec4\u4f5c\u4e3a\u6570\u636e delete [] m_data ; m_data = data ; -- m_length ; } // A couple of additional functions just for convenience void insertAtBeginning ( int value ) { insertBefore ( value , 0 ); } void insertAtEnd ( int value ) { insertBefore ( value , m_length ); } IntArray \u5bb9\u5668\u7c7b\u7684\u5b8c\u6574\u5b9a\u4e49\u5982\u4e0b\uff1a IntArray.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 #ifndef INTARRAY_H #define INTARRAY_H #include // for assert() class IntArray { private : int m_length {}; int * m_data {}; public : IntArray () = default ; IntArray ( int length ) : m_length { length } { assert ( length >= 0 ); if ( length > 0 ) m_data = new int [ length ]{}; } ~ IntArray () { delete [] m_data ; // we don't need to set m_data to null or m_length to 0 here, since the object will be destroyed immediately after this function anyway } void erase () { delete [] m_data ; // We need to make sure we set m_data to nullptr here, otherwise it will // be left pointing at deallocated memory! m_data = nullptr ; m_length = 0 ; } int & operator []( int index ) { assert ( index >= 0 && index < m_length ); return m_data [ index ]; } // reallocate resizes the array. Any existing elements will be destroyed. This function operates quickly. void reallocate ( int newLength ) { // First we delete any existing elements erase (); // If our array is going to be empty now, return here if ( newLength <= 0 ) return ; // Then we have to allocate new elements m_data = new int [ newLength ]; m_length = newLength ; } // resize resizes the array. Any existing elements will be kept. This function operates slowly. void resize ( int newLength ) { // if the array is already the right length, we're done if ( newLength == m_length ) return ; // If we are resizing to an empty array, do that and return if ( newLength <= 0 ) { erase (); return ; } // Now we can assume newLength is at least 1 element. This algorithm // works as follows: First we are going to allocate a new array. Then we // are going to copy elements from the existing array to the new array. // Once that is done, we can destroy the old array, and make m_data // point to the new array. // First we have to allocate a new array int * data { new int [ newLength ] }; // Then we have to figure out how many elements to copy from the existing // array to the new array. We want to copy as many elements as there are // in the smaller of the two arrays. if ( m_length > 0 ) { int elementsToCopy { ( newLength > m_length ) ? m_length : newLength }; // Now copy the elements one by one for ( int index { 0 }; index < elementsToCopy ; ++ index ) data [ index ] = m_data [ index ]; } // Now we can delete the old array because we don't need it any more delete [] m_data ; // And use the new array instead! Note that this simply makes m_data point // to the same address as the new array we dynamically allocated. Because // data was dynamically allocated, it won't be destroyed when it goes out of scope. m_data = data ; m_length = newLength ; } void insertBefore ( int value , int index ) { // Sanity check our index value assert ( index >= 0 && index <= m_length ); // First create a new array one element larger than the old array int * data { new int [ m_length + 1 ] }; // Copy all of the elements up to the index for ( int before { 0 }; before < index ; ++ before ) data [ before ] = m_data [ before ]; // Insert our new element into the new array data [ index ] = value ; // Copy all of the values after the inserted element for ( int after { index }; after < m_length ; ++ after ) data [ after + 1 ] = m_data [ after ]; // Finally, delete the old array, and use the new array instead delete [] m_data ; m_data = data ; ++ m_length ; } void remove ( int index ) { // Sanity check our index value assert ( index >= 0 && index < m_length ); // If we're removing the last element in the array, we can just erase the array and return early if ( m_length == 1 ) { erase (); return ; } // First create a new array one element smaller than the old array int * data { new int [ m_length -1 ] }; // Copy all of the elements up to the index for ( int before { 0 }; before < index ; ++ before ) data [ before ] = m_data [ before ]; // Copy all of the values after the removed element for ( int after { index + 1 }; after < m_length ; ++ after ) data [ after -1 ] = m_data [ after ]; // Finally, delete the old array, and use the new array instead delete [] m_data ; m_data = data ; -- m_length ; } // A couple of additional functions just for convenience void insertAtBeginning ( int value ) { insertBefore ( value , 0 ); } void insertAtEnd ( int value ) { insertBefore ( value , m_length ); } int getLength () const { return m_length ; } }; #endif \u8bd5\u8bd5\u770b\uff0c\u80fd\u4e0d\u80fd\u6b63\u786e\u5de5\u4f5c\uff01 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include #include \"IntArray.h\" int main () { // Declare an array with 10 elements IntArray array ( 10 ); // Fill the array with numbers 1 through 10 for ( int i { 0 }; i < 10 ; ++ i ) array [ i ] = i + 1 ; // Resize the array to 8 elements array . resize ( 8 ); // Insert the number 20 before element with index 5 array . insertBefore ( 20 , 5 ); // Remove the element with index 3 array . remove ( 3 ); // Add 30 and 40 to the end and beginning array . insertAtEnd ( 30 ); array . insertAtBeginning ( 40 ); // Print out all the numbers for ( int i { 0 }; i < array . getLength (); ++ i ) std :: cout << array [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 40 1 2 3 5 20 6 7 8 30 \u867d\u7136\u7f16\u5199\u4e00\u4e2a\u5bb9\u5668\u7c7b\u662f\u975e\u5e38\u590d\u6742\u7684\u4e8b\u60c5\uff0c\u597d\u6d88\u606f\u662f\u4f60\u53ea\u9700\u8981\u7f16\u5199\u4e00\u6b21\u5373\u53ef\u3002\u4e00\u65e6\u67d0\u4e2a\u5bb9\u5668\u7c7b\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\u3002\u90a3\u4e48\u4f60\u63a5\u4e0b\u6765\u5c31\u53ef\u4ee5\u4e0d\u65ad\u5730\u4f7f\u7528\u5b83\u4e86\u3002 \u5c3d\u7ba1\u6211\u4eec\u7684 IntArray \u5bb9\u5668\u4e2d\u5b58\u653e\u7684\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b ( int )\uff0c\u6211\u4eec\u5176\u5b9e\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7528\u6237\u5b9a\u4e49\u7c7b\u578b(\u4f8b\u5982 Point \u7c7b). \u8fd8\u6709\u4e00\u4ef6\u4e8b\uff1a\u5982\u679c\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u7c7b\u53ef\u4ee5\u6ee1\u8db3\u4f60\u7684\u9700\u6c42\uff0c\u90a3\u4f60\u5e94\u8be5\u76f4\u63a5\u4f7f\u7528\u5b83\u800c\u4e0d\u662f\u521b\u5efa\u81ea\u5df1\u7684\u7c7b\u3002\u4f8b\u5982\uff0c\u4e0e\u5176\u4f7f\u7528 IntArray \uff0c\u4e0d\u5982\u4f7f\u7528 std::vector \u3002\u5b83\u662f\u7ecf\u8fc7\u68c0\u9a8c\u7684\uff0c\u6548\u7387\u5f88\u9ad8\uff0c\u5e76\u4e14\u4e0e\u6807\u51c6\u5e93\u4e2d\u7684\u5176\u4ed6\u7c7b\u53ef\u4ee5\u5f88\u597d\u5730\u914d\u5408\u3002\u4f46\u6709\u65f6\u4f60\u9700\u8981\u6807\u51c6\u5e93\u4e2d\u4e0d\u5b58\u5728\u7684\u4e13\u7528\u5bb9\u5668\u7c7b\uff0c\u56e0\u6b64\u6700\u597d\u77e5\u9053\u5728\u9700\u8981\u65f6\u5982\u4f55\u521b\u5efa\u81ea\u5df1\u7684\u5bb9\u5668\u7c7b\u3002\u5728\u4ecb\u7ecd\u4e86\u4e00\u4e9b\u66f4\u57fa\u672c\u7684\u4e3b\u9898\u4e4b\u540e\uff0c\u6211\u4eec\u5c06\u66f4\u591a\u5730\u8ba8\u8bba\u6807\u51c6\u5e93\u4e2d\u7684\u5bb9\u5668\u3002","title":"16.6 - \u5bb9\u5668\u7c7b"},{"location":"16-6-container-classes/#_1","text":"\u5bb9\u5668\u7c7b\u901a\u5e38\u6709\u4e24\u4e2a\u53d8\u79cd\u3002 \u503c\u5bb9\u5668 \u662f \u7ec4\u5408\u5173\u7cfb \uff0c\u5b83\u5b58\u653e\u4e86\u5176\u5143\u7d20\u7684\u62f7\u8d1d\uff08\u56e0\u6b64\u5fc5\u987b\u8d1f\u8d23\u521b\u5efa\u548c\u9500\u6bc1\u8fd9\u4e9b\u62f7\u8d1d\uff09\u3002 \u5f15\u7528\u5bb9\u5668 \u5219\u662f \u805a\u5408\u5173\u7cfb \uff0c\u5b83\u4fdd\u5b58\u4e86\u5143\u7d20\u7684\u6307\u9488\u6216\u5f15\u7528\uff08\u56e0\u6b64\u65e0\u9700\u64cd\u5fc3\u5b83\u4eec\u7684\u521b\u5efa\u548c\u9500\u6bc1\uff09\u3002","title":"\u5bb9\u5668\u7c7b\u578b"},{"location":"16-6-container-classes/#_2","text":"\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ece\u5934\u7f16\u5199\u4e00\u4e2a\u6574\u578b\u6570\u7ec4\u7c7b\u5e76\u5b9e\u73b0\u5b83\u5e94\u8be5\u63d0\u4f9b\u7684\u5927\u90e8\u5206\u529f\u80fd\u3002\u8be5\u6570\u7ec4\u7c7b\u662f\u4e00\u4e2a \u503c\u5bb9\u5668 \uff0c\u5b83\u4fdd\u5b58\u5143\u7d20\u7684\u62f7\u8d1d\u3002\u548c\u5b83\u4eec\u7684\u540d\u5b57\u6697\u793a\u7684\u90a3\u6837\uff0c\u8fd9\u4e2a\u5bb9\u5668\u4f1a\u4fdd\u5b58\u4e00\u4e2a\u6574\u578b\u6570\u6784\u6210\u7684\u6570\u7ec4\uff0c\u7c7b\u4f3c std::vector \u3002 \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u521b\u5efa IntArray.h \u6587\u4ef6\uff1a 1 2 3 4 5 6 7 8 #ifndef INTARRAY_H #define INTARRAY_H class IntArray { }; #endif IntArray \u9700\u8981\u8ffd\u8e2a\u4e24\u4e2a\u503c\uff1a\u6570\u636e\u548c\u6570\u7ec4\u5927\u5c0f\u3002\u56e0\u4e3a\u6211\u4eec\u5e0c\u671b\u6570\u7ec4\u80fd\u591f\u6539\u53d8\u5176\u5927\u5c0f\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u52a8\u6001\u5206\u914d\u5185\u5b58\uff0c\u56e0\u6b64\u5fc5\u987b\u4f7f\u7528\u6307\u9488\u6765\u4fdd\u5b58\u6570\u636e\u3002 1 2 3 4 5 6 7 8 9 10 11 #ifndef INTARRAY_H #define INTARRAY_H class IntArray { private : int m_length {}; int * m_data {}; }; #endif \u63a5\u4e0b\u6765\uff0c\u6dfb\u52a0\u7528\u4e8e\u521b\u5efa IntArrays \u7684\u6784\u9020\u51fd\u6570\u3002\u8fd9\u91cc\u6211\u4eec\u9700\u8981\u4e24\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u4e00\u4e2a\u521b\u5efa\u7a7a\u7684\u6570\u7ec4\uff0c\u4e00\u4e2a\u5219\u521b\u5efa\u9884\u5b9a\u4e49\u957f\u5ea6\u7684\u6570\u7ec4\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #ifndef INTARRAY_H #define INTARRAY_H #include // for assert() class IntArray { private : int m_length {}; int * m_data {}; public : IntArray () = default ; IntArray ( int length ) : m_length { length } { assert ( length >= 0 ); if ( length > 0 ) m_data = new int [ length ]{}; } }; #endif \u6211\u4eec\u8fd8\u9700\u8981\u521b\u5efa\u7528\u4e8e\u6e05\u7406\u6570\u7ec4\u7684\u51fd\u6570\u3002\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a \u6790\u6784\u51fd\u6570 \uff0c\u5b83\u7684\u529f\u80fd\u5c31\u662f\u91ca\u653e\u6570\u636e\u5360\u7528\u7684\u5185\u5b58\u3002\u7136\u540e\uff0c\u518d\u7f16\u5199\u4e00\u4e2a erase() \u51fd\u6570\u5c06\u6570\u7ec4\u7684\u5143\u7d20\u6e05\u9664\u5e76\u5c06\u957f\u5ea6\u8bbe\u7f6e\u4e3a0\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ~ IntArray () { delete [] m_data ; // \u8fd9\u91cc\u4e0d\u5fc5\u5c06 m_data \u8bbe\u7f6e\u4e3a null \u4e5f\u4e0d\u7528\u5c06 m_length \u8bbe\u7f6e\u4e3a0\uff0c\u56e0\u4e3a\u5bf9\u8c61\u9a6c\u4e0a\u5c31\u4f1a\u88ab\u9500\u6bc1 } void erase () { delete [] m_data ; // \u5fc5\u987b\u786e\u4fdd m_data \u662f nullptr \u5426\u5219\u5c06\u4f1a\u79f0\u4e3a\u60ac\u5782\u6307\u9488 m_data = nullptr ; m_length = 0 ; } \u73b0\u5728\uff0c \u91cd\u8f7d [] \u64cd\u4f5c\u7b26\u7528\u4e8e\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u5728\u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26\u7684\u65f6\u5019\uff0c\u5fc5\u987b\u786e\u4fdd\u7d22\u5f15\u662f\u5408\u6cd5\u7684\uff0c\u8be5\u64cd\u4f5c\u53ef\u4ee5\u4f7f\u7528 assert() \u51fd\u6570\u3002\u6211\u4eec\u8fd8\u9700\u8981\u6dfb\u52a0\u4e00\u4e2a\u51fd\u6570\u7528\u4e8e\u8fd4\u56de\u6570\u7ec4\u957f\u5ea6\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u7c7b\u770b\u4e0a\u53bb\u4f1a\u5411\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #ifndef INTARRAY_H #define INTARRAY_H #include // for assert() class IntArray { private : int m_length {}; int * m_data {}; public : IntArray () = default ; IntArray ( int length ) : m_length { length } { assert ( length >= 0 ); if ( length > 0 ) m_data = new int [ length ]{}; } ~ IntArray () { delete [] m_data ; // \u8fd9\u91cc\u4e0d\u5fc5\u5c06 m_data \u8bbe\u7f6e\u4e3a null \u4e5f\u4e0d\u7528\u5c06 m_length \u8bbe\u7f6e\u4e3a0\uff0c\u56e0\u4e3a\u5bf9\u8c61\u9a6c\u4e0a\u5c31\u4f1a\u88ab\u9500\u6bc1 } void erase () { delete [] m_data ; // \u5fc5\u987b\u786e\u4fdd m_data \u662f nullptr \u5426\u5219\u5c06\u4f1a\u79f0\u4e3a\u60ac\u5782\u6307\u9488 m_data = nullptr ; m_length = 0 ; } int & operator []( int index ) { assert ( index >= 0 && index < m_length ); return m_data [ index ]; } int getLength () const { return m_length ; } }; #endif \u81f3\u6b64\uff0c IntArray \u7c7b\u5df2\u7ecf\u5b9e\u73b0\u5b8c\u6210\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e86\u3002\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6709\u9884\u5b9a\u4e49\u957f\u5ea6\u7684 IntArrays \uff0c\u7136\u540e\u4f7f\u7528\u4e0b\u6807\u8fd0\u7b97\u7b26\u83b7\u53d6\u5143\u7d20\u7684\u503c\u3002 \u4f46\u662f\uff0c IntArray \u4ecd\u7136\u8fd8\u6709\u4e9b\u529f\u80fd\u6ca1\u6709\u88ab\u5b9e\u73b0\u3002\u6570\u7ec4\u5927\u5c0f\u6682\u65f6\u8fd8\u4e0d\u80fd\u6539\u53d8\u3001\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4e5f\u8fd8\u4e0d\u80fd\u88ab\u63d2\u5165\u6216\u5220\u9664\u3001\u4e0d\u4ec5\u5982\u6b64\uff0c\u6570\u7ec4\u5143\u7d20\u4e5f\u8fd8\u4e0d\u80fd\u88ab\u6392\u5e8f\u3002 \u9996\u5148\uff0c\u7f16\u5199\u4ee3\u7801\u4f7f\u5f97\u6570\u7ec4\u53ef\u4ee5\u6539\u53d8\u5176\u5c3a\u5bf8\u3002\u800c\u4e14\uff0c\u6211\u4eec\u4f1a\u5b9e\u73b0\u4e24\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u7b2c\u4e00\u4e2a\u51fd\u6570\u662f reallocate() \uff0c\u8fd9\u4e2a\u51fd\u6570\u5728\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\u7684\u65f6\u5019\uff0c\u4f1a\u9500\u6bc1\u6570\u7ec4\u4e2d\u7684\u5168\u90e8\u5143\u7d20\uff0c\u4f46\u662f\u5b83\u7684\u901f\u5ea6\u5f88\u5feb\u3002\u7b2c\u4e8c\u4e2a\u51fd\u6570\u662f resize() \uff0c\u5b83\u5728\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u4fdd\u6301\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u4f46\u662f\u8be5\u51fd\u6570\u7684\u6548\u7387\u4f1a\u4f4e\u4e00\u4e9b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 // reallocate \u4f1a\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\uff0c\u4f46\u662f\u5b83\u4f1a\u5728\u5de5\u4f5c\u65f6\u9500\u6bc1\u6240\u6709\u7684\u6570\u7ec4\u5143\u7d20\u3002\u8be5\u51fd\u6570\u7684\u901f\u5ea6\u5f88\u5feb void reallocate ( int newLength ) { // \u9996\u5148\uff0c\u5220\u9664\u5168\u90e8\u7684\u5143\u7d20 erase (); // \u5982\u679c\u8981\u521b\u5efa\u7a7a\u6570\u7ec4\uff0c\u6b64\u5904\u8fd4\u56de\u5373\u53ef if ( newLength <= 0 ) return ; // \u5206\u914d\u65b0\u7684\u5143\u7d20 m_data = new int [ newLength ]; m_length = newLength ; } // resize \u4f1a\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\uff0c\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4f1a\u88ab\u4fdd\u5b58\u4e0b\u6765\u3002\u8be5\u51fd\u6570\u7684\u901f\u5ea6\u6bd4\u8f83\u6162 void resize ( int newLength ) { // \u5982\u679c\u6570\u7ec4\u7684\u957f\u5ea6\u5df2\u7ecf\u6ee1\u8db3\u8981\u6c42\uff0c\u5b8c\u6210 if ( newLength == m_length ) return ; // \u5982\u679c\u9700\u8981\u5c06\u6570\u7ec4\u8c03\u6574\u4e3a\u4e00\u4e2a\u7a7a\u6570\u7ec4\uff0c\u5b8c\u6210\u5e76\u8fd4\u56de if ( newLength <= 0 ) { erase (); return ; } // \u5047\u8bbe newLength \u5927\u4e8e\u7b49\u4e8e1\u3002\u8be5\u7b97\u6cd5\u7684\u539f\u7406\u5982\u4e0b\uff1a // \u9996\u5148\uff0c\u5206\u914d\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\u3002\u7136\u540e\u5c06\u65e7\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u90fd\u62f7\u8d1d\u5230\u65b0\u7684\u6570\u7ec4\u4e2d\u3002\u5b8c\u6210\u540e\uff0c\u9500\u6bc1\u65e7\u6570\u7ec4\u3002 // \u7136\u540e\u8ba9 m_data \u6307\u5411\u65b0\u7684\u6570\u7ec4 // \u5206\u914d\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4 int * data { new int [ newLength ] }; // \u786e\u5b9a\u6709\u591a\u5c11\u4e2a\u5143\u7d20\u9700\u8981\u4ece\u65e7\u6570\u7ec4\u4e2d\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\u3002 // \u8fd9\u91cc\u6211\u4eec\u5e0c\u671b\u4ece\u65e7\u6570\u7ec4\u4e2d\u62f7\u8d1d\u5c3d\u53ef\u80fd\u591a\u7684\u5143\u7d20\uff08\u65b0\u6570\u7ec4\u4e0d\u4e00\u5b9a\u6bd4\u65e7\u6570\u7ec4\u5927\uff09 // \u62f7\u8d1d\u7684\u5143\u7d20\u4e2a\u6570\u662f\u65b0\u65e7\u4e24\u4e2a\u6570\u7ec4\u4e2d\u8f83\u5c0f\u7684\u4e00\u4e2a if ( m_length > 0 ) { int elementsToCopy { ( newLength > m_length ) ? m_length : newLength }; // Now copy the elements one by one for ( int index { 0 }; index < elementsToCopy ; ++ index ) data [ index ] = m_data [ index ]; } // \u5220\u9664\u65e7\u6570\u7ec4 delete [] m_data ; // \u5f00\u59cb\u4f7f\u7528\u65b0\u7684\u6570\u7ec4\u4f5c\u4e3a\u6570\u636e\uff01\u6700\u7b80\u5355\u7684\u529e\u6cd5\u5c31\u662f\u8ba9 m_data\u6307\u5411\u65b0\u6570\u7ec4\u7684\u5730\u5740\u3002 // \u7531\u4e8e\u8be5\u6570\u7ec4\u662f\u52a8\u6001\u5206\u914d\u7684\uff0c\u6240\u4ee5\u4e0d\u4f1a\u5728\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\u88ab\u9500\u6bc1 m_data = data ; m_length = newLength ; } \u8fd8\u633a\u590d\u6742\uff01 \u591a\u6570\u7684\u6570\u7ec4\u5bb9\u5668\u7c7b\u4f1a\u6b62\u6b65\u4e8e\u6b64\u3002\u4e0d\u8fc7\uff0c\u4e5f\u8bb8\u4f60\u60f3\u77e5\u9053\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u5e94\u8be5\u5982\u4f55\u5b9e\u73b0\uff0c\u6240\u4ee5\u6211\u4eec\u7ee7\u7eed\u5b9e\u73b0\u8fd9\u4e24\u4e2a\u529f\u80fd\u3002\u5176\u5b9e\u73b0\u65b9\u6cd5\u548c resize() \u975e\u5e38\u7c7b\u4f3c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 void insertBefore ( int value , int index ) { // \u68c0\u67e5\u6570\u7ec4\u7d22\u5f15\u5408\u6cd5\u6027 assert ( index >= 0 && index <= m_length ); // \u9996\u5148\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u5176\u957f\u5ea6\u6bd4\u65e7\u6570\u7ec4\u59271 int * data { new int [ m_length + 1 ] }; // \u62f7\u8d1d\u5230\u5f53\u524d\u7d22\u5f15\u7684\u6240\u6709\u5143\u7d20 for ( int before { 0 }; before < index ; ++ before ) data [ before ] = m_data [ before ]; // \u63d2\u5165\u65b0\u7684\u5143\u7d20\u5230\u65b0\u6570\u7ec4 data [ index ] = value ; // \u7ee7\u7eed\u62f7\u8d1d\u5269\u4f59\u7684\u5143\u7d20 for ( int after { index }; after < m_length ; ++ after ) data [ after + 1 ] = m_data [ after ]; // \u6700\u540e\uff0c\u5220\u9664\u65e7\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528\u65b0\u7684\u6570\u7ec4\u4f5c\u4e3a\u6570\u636e delete [] m_data ; m_data = data ; ++ m_length ; } void remove ( int index ) { // \u68c0\u67e5\u6570\u7ec4\u7d22\u5f15\u5408\u6cd5\u6027 assert ( index >= 0 && index < m_length ); // \u5982\u679c\u8fd9\u5df2\u7ecf\u662f\u6700\u540e\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20\uff0c\u5c06\u6570\u7ec4\u8bbe\u7f6e\u4e3a\u7a7a\u6570\u7ec4\u7136\u540e\u5b8c\u6210 if ( m_length == 1 ) { erase (); return ; } // \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5176\u5927\u5c0f\u6bd4\u65e7\u6570\u7ec4\u5c0f1 int * data { new int [ m_length -1 ] }; // \u62f7\u8d1d\u5230\u7d22\u5f15\u7684\u5168\u90e8\u5143\u7d20 for ( int before { 0 }; before < index ; ++ before ) data [ before ] = m_data [ before ]; // \u7ee7\u7eed\u62f7\u8d1d\u88ab\u5220\u9664\u5143\u7d20\u540e\u9762\u7684\u6240\u6709\u5143\u7d20 for ( int after { index + 1 }; after < m_length ; ++ after ) data [ after -1 ] = m_data [ after ]; // \u6700\u540e\uff0c\u5220\u9664\u65e7\u6570\u7ec4\u5e76\u4f7f\u7528\u65b0\u7684\u6570\u7ec4\u4f5c\u4e3a\u6570\u636e delete [] m_data ; m_data = data ; -- m_length ; } // A couple of additional functions just for convenience void insertAtBeginning ( int value ) { insertBefore ( value , 0 ); } void insertAtEnd ( int value ) { insertBefore ( value , m_length ); } IntArray \u5bb9\u5668\u7c7b\u7684\u5b8c\u6574\u5b9a\u4e49\u5982\u4e0b\uff1a IntArray.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 #ifndef INTARRAY_H #define INTARRAY_H #include // for assert() class IntArray { private : int m_length {}; int * m_data {}; public : IntArray () = default ; IntArray ( int length ) : m_length { length } { assert ( length >= 0 ); if ( length > 0 ) m_data = new int [ length ]{}; } ~ IntArray () { delete [] m_data ; // we don't need to set m_data to null or m_length to 0 here, since the object will be destroyed immediately after this function anyway } void erase () { delete [] m_data ; // We need to make sure we set m_data to nullptr here, otherwise it will // be left pointing at deallocated memory! m_data = nullptr ; m_length = 0 ; } int & operator []( int index ) { assert ( index >= 0 && index < m_length ); return m_data [ index ]; } // reallocate resizes the array. Any existing elements will be destroyed. This function operates quickly. void reallocate ( int newLength ) { // First we delete any existing elements erase (); // If our array is going to be empty now, return here if ( newLength <= 0 ) return ; // Then we have to allocate new elements m_data = new int [ newLength ]; m_length = newLength ; } // resize resizes the array. Any existing elements will be kept. This function operates slowly. void resize ( int newLength ) { // if the array is already the right length, we're done if ( newLength == m_length ) return ; // If we are resizing to an empty array, do that and return if ( newLength <= 0 ) { erase (); return ; } // Now we can assume newLength is at least 1 element. This algorithm // works as follows: First we are going to allocate a new array. Then we // are going to copy elements from the existing array to the new array. // Once that is done, we can destroy the old array, and make m_data // point to the new array. // First we have to allocate a new array int * data { new int [ newLength ] }; // Then we have to figure out how many elements to copy from the existing // array to the new array. We want to copy as many elements as there are // in the smaller of the two arrays. if ( m_length > 0 ) { int elementsToCopy { ( newLength > m_length ) ? m_length : newLength }; // Now copy the elements one by one for ( int index { 0 }; index < elementsToCopy ; ++ index ) data [ index ] = m_data [ index ]; } // Now we can delete the old array because we don't need it any more delete [] m_data ; // And use the new array instead! Note that this simply makes m_data point // to the same address as the new array we dynamically allocated. Because // data was dynamically allocated, it won't be destroyed when it goes out of scope. m_data = data ; m_length = newLength ; } void insertBefore ( int value , int index ) { // Sanity check our index value assert ( index >= 0 && index <= m_length ); // First create a new array one element larger than the old array int * data { new int [ m_length + 1 ] }; // Copy all of the elements up to the index for ( int before { 0 }; before < index ; ++ before ) data [ before ] = m_data [ before ]; // Insert our new element into the new array data [ index ] = value ; // Copy all of the values after the inserted element for ( int after { index }; after < m_length ; ++ after ) data [ after + 1 ] = m_data [ after ]; // Finally, delete the old array, and use the new array instead delete [] m_data ; m_data = data ; ++ m_length ; } void remove ( int index ) { // Sanity check our index value assert ( index >= 0 && index < m_length ); // If we're removing the last element in the array, we can just erase the array and return early if ( m_length == 1 ) { erase (); return ; } // First create a new array one element smaller than the old array int * data { new int [ m_length -1 ] }; // Copy all of the elements up to the index for ( int before { 0 }; before < index ; ++ before ) data [ before ] = m_data [ before ]; // Copy all of the values after the removed element for ( int after { index + 1 }; after < m_length ; ++ after ) data [ after -1 ] = m_data [ after ]; // Finally, delete the old array, and use the new array instead delete [] m_data ; m_data = data ; -- m_length ; } // A couple of additional functions just for convenience void insertAtBeginning ( int value ) { insertBefore ( value , 0 ); } void insertAtEnd ( int value ) { insertBefore ( value , m_length ); } int getLength () const { return m_length ; } }; #endif \u8bd5\u8bd5\u770b\uff0c\u80fd\u4e0d\u80fd\u6b63\u786e\u5de5\u4f5c\uff01 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include #include \"IntArray.h\" int main () { // Declare an array with 10 elements IntArray array ( 10 ); // Fill the array with numbers 1 through 10 for ( int i { 0 }; i < 10 ; ++ i ) array [ i ] = i + 1 ; // Resize the array to 8 elements array . resize ( 8 ); // Insert the number 20 before element with index 5 array . insertBefore ( 20 , 5 ); // Remove the element with index 3 array . remove ( 3 ); // Add 30 and 40 to the end and beginning array . insertAtEnd ( 30 ); array . insertAtBeginning ( 40 ); // Print out all the numbers for ( int i { 0 }; i < array . getLength (); ++ i ) std :: cout << array [ i ] << ' ' ; std :: cout << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 40 1 2 3 5 20 6 7 8 30 \u867d\u7136\u7f16\u5199\u4e00\u4e2a\u5bb9\u5668\u7c7b\u662f\u975e\u5e38\u590d\u6742\u7684\u4e8b\u60c5\uff0c\u597d\u6d88\u606f\u662f\u4f60\u53ea\u9700\u8981\u7f16\u5199\u4e00\u6b21\u5373\u53ef\u3002\u4e00\u65e6\u67d0\u4e2a\u5bb9\u5668\u7c7b\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\u3002\u90a3\u4e48\u4f60\u63a5\u4e0b\u6765\u5c31\u53ef\u4ee5\u4e0d\u65ad\u5730\u4f7f\u7528\u5b83\u4e86\u3002 \u5c3d\u7ba1\u6211\u4eec\u7684 IntArray \u5bb9\u5668\u4e2d\u5b58\u653e\u7684\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b ( int )\uff0c\u6211\u4eec\u5176\u5b9e\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7528\u6237\u5b9a\u4e49\u7c7b\u578b(\u4f8b\u5982 Point \u7c7b). \u8fd8\u6709\u4e00\u4ef6\u4e8b\uff1a\u5982\u679c\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u7c7b\u53ef\u4ee5\u6ee1\u8db3\u4f60\u7684\u9700\u6c42\uff0c\u90a3\u4f60\u5e94\u8be5\u76f4\u63a5\u4f7f\u7528\u5b83\u800c\u4e0d\u662f\u521b\u5efa\u81ea\u5df1\u7684\u7c7b\u3002\u4f8b\u5982\uff0c\u4e0e\u5176\u4f7f\u7528 IntArray \uff0c\u4e0d\u5982\u4f7f\u7528 std::vector \u3002\u5b83\u662f\u7ecf\u8fc7\u68c0\u9a8c\u7684\uff0c\u6548\u7387\u5f88\u9ad8\uff0c\u5e76\u4e14\u4e0e\u6807\u51c6\u5e93\u4e2d\u7684\u5176\u4ed6\u7c7b\u53ef\u4ee5\u5f88\u597d\u5730\u914d\u5408\u3002\u4f46\u6709\u65f6\u4f60\u9700\u8981\u6807\u51c6\u5e93\u4e2d\u4e0d\u5b58\u5728\u7684\u4e13\u7528\u5bb9\u5668\u7c7b\uff0c\u56e0\u6b64\u6700\u597d\u77e5\u9053\u5728\u9700\u8981\u65f6\u5982\u4f55\u521b\u5efa\u81ea\u5df1\u7684\u5bb9\u5668\u7c7b\u3002\u5728\u4ecb\u7ecd\u4e86\u4e00\u4e9b\u66f4\u57fa\u672c\u7684\u4e3b\u9898\u4e4b\u540e\uff0c\u6211\u4eec\u5c06\u66f4\u591a\u5730\u8ba8\u8bba\u6807\u51c6\u5e93\u4e2d\u7684\u5bb9\u5668\u3002","title":"\u6570\u7ec4\u5bb9\u5668\u7c7b"},{"location":"16-7-std-initializer_list/","tags":["object","initializer_list"],"text":"Key Takeaway - \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u6574\u578b\u6570\u7ec4\uff1a 1 int array [ 5 ]; \u5982\u679c\u6211\u4eec\u60f3\u7528\u503c\u521d\u59cb\u5316\u8fd9\u4e2a\u6570\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7 \u521d\u59cb\u5316\u503c\u5217\u8868 \u8bed\u6cd5\u6765\u5b9e\u73b0: 1 2 3 4 5 6 7 8 9 10 #include int main () { int array [] { 5 , 4 , 3 , 2 , 1 }; // \u521d\u59cb\u5316\u503c\u5217\u8868 for ( auto i : array ) std :: cout << i << ' ' ; return 0 ; } \u6253\u5370\uff1a 1 5 4 3 2 1 \u5bf9\u4e8e\u52a8\u6001\u6570\u7ec4\u540c\u6837\u9002\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { auto * array { new int [ 5 ]{ 5 , 4 , 3 , 2 , 1 } }; // \u521d\u59cb\u5316\u503c\u5217\u8868 for ( int count { 0 }; count < 5 ; ++ count ) std :: cout << array [ count ] << ' ' ; delete [] array ; return 0 ; } \u5728\u4e0a\u8282\u8bfe\u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86 \u5bb9\u5668\u7c7b \u7684\u6982\u5ff5\uff0c\u5e76\u4e14\u4ee5 IntArray \u7c7b\u4e3a\u4f8b\u4ecb\u7ecd\u4e86\u6574\u578b\u6570\u7ec4\u5bb9\u5668\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #include // for assert() #include class IntArray { private : int m_length {}; int * m_data {}; public : IntArray () = default ; IntArray ( int length ) : m_length { length } , m_data { new int [ length ]{} } { } ~ IntArray () { delete [] m_data ; // we don't need to set m_data to null or m_length to 0 here, since the object will be destroyed immediately after this function anyway } int & operator []( int index ) { assert ( index >= 0 && index < m_length ); return m_data [ index ]; } int getLength () const { return m_length ; } }; int main () { // \u5982\u679c\u6211\u4eec\u4f7f\u7528\u521d\u59cb\u5316\u503c\u5217\u8868\u5bf9IntArray\u8fdb\u884c\u521d\u59cb\u5316\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f IntArray array { 5 , 4 , 3 , 2 , 1 }; // \u65e0\u6cd5\u7f16\u8bd1 for ( int count { 0 }; count < 5 ; ++ count ) std :: cout << array [ count ] << ' ' ; return 0 ; } \u4e0a\u8ff0\u4ee3\u7801\u5e76\u4e0d\u80fd\u6210\u529f\u7f16\u8bd1\uff0c\u56e0\u4e3a IntArray \u7c7b\u5e76\u4e0d\u77e5\u9053\u5728\u9762\u5bf9\u4e00\u4e2a \u521d\u59cb\u5316\u503c\u5217\u8868 \u65f6\u5e94\u8be5\u505a\u4ec0\u4e48\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5fc5\u987b\u5bf9\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u9010\u4e00\u521d\u59cb\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int main () { IntArray array ( 5 ); array [ 0 ] = 5 ; array [ 1 ] = 4 ; array [ 2 ] = 3 ; array [ 3 ] = 2 ; array [ 4 ] = 1 ; for ( int count { 0 }; count < 5 ; ++ count ) std :: cout << array [ count ] << ' ' ; return 0 ; } \u5f88\u4e0d\u4f18\u96c5\u3002 \u4f7f\u7528 std::initializer_list \u521d\u59cb\u5316 \u5f53\u7f16\u8bd1\u5668\u770b\u5230\u521d\u59cb\u5316\u503c\u5217\u8868\u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u5730\u5c06\u521d\u59cb\u5316\u503c\u5217\u8868\u8f6c\u6362\u4e3a\u4e00\u4e2a std::initializer_list \u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u63a5\u6536 std::initializer_list \u7c7b\u578b\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u7684\u8bdd\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7\u521d\u59cb\u5316\u503c\u5217\u8868\u521b\u5efa\u5bf9\u8c61\u4e86\u3002 std::initializer_list \u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d\u3002 \u5173\u4e8e std::initializer_list \uff0c\u6709\u4e9b\u4e8b\u60c5\u4f60\u5fc5\u987b\u6307\u5b9a\u3002\u548c std::array \u6216\u8005 std::vector \u7c7b\u4f3c\uff0c\u4f60\u5fc5\u987b\u544a\u8bc9 std::initializer_list \u5217\u8868\u4e2d\u7684\u5143\u7d20\u7c7b\u578b\u662f\u4ec0\u4e48\uff0c\u9664\u975e\u4f60\u662f\u7acb\u5373\u5bf9\u5176\u521d\u59cb\u5316\u3002\u56e0\u6b64\u4f60\u5f88\u5c11\u4f1a\u770b\u5230 std::initializer_list \u7684\u6700\u7b80\u5f62\u5f0f\u3002\u591a\u6570\u60c5\u51b5\u4e0b\u4f60\u770b\u5230\u7684\u5e94\u8be5\u662f\u8fd9\u6837\u7684 std::initializer_list \u6216\u8fd9\u6837\u7684 std::initializer_list \u3002 \u7b2c\u4e8c\uff0c std::initializer_list \u6709\u4e00\u4e2a size() \u6210\u5458\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u5143\u7d20\u4e2a\u6570\u3002\u5f53\u6211\u4eec\u9700\u8981\u6307\u5b9a\u4f20\u5165\u7684\u521d\u59cb\u5316\u5217\u8868\u591a\u957f\u65f6\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u6709\u7528\u3002 \u4f7f\u7528 std::initializer_list \u5bf9 IntArray \u6539\u5199\u4e4b\u540e\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 #include // for assert() #include // for std::initializer_list #include class IntArray { private : int m_length {}; int * m_data {}; public : IntArray () = default ; IntArray ( int length ) : m_length { length } , m_data { new int [ length ]{} } { } IntArray ( std :: initializer_list < int > list ) // allow IntArray to be initialized via list initialization : IntArray ( static_cast < int > ( list . size ())) // use delegating constructor to set up initial array { // Now initialize our array from the list int count { 0 }; for ( auto element : list ) { m_data [ count ] = element ; ++ count ; } } ~ IntArray () { delete [] m_data ; // we don't need to set m_data to null or m_length to 0 here, since the object will be destroyed immediately after this function anyway } IntArray ( const IntArray & ) = delete ; // to avoid shallow copies IntArray & operator = ( const IntArray & list ) = delete ; // to avoid shallow copies int & operator []( int index ) { assert ( index >= 0 && index < m_length ); return m_data [ index ]; } int getLength () const { return m_length ; } }; int main () { IntArray array { 5 , 4 , 3 , 2 , 1 }; // initializer list for ( int count { 0 }; count < array . getLength (); ++ count ) std :: cout << array [ count ] << ' ' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u7b26\u5408\u9884\u671f\uff1a 1 5 4 3 2 1 \u5f88\u597d\uff01\u63a5\u4e0b\u6765\u8ba9\u6211\u4eec\u6df1\u5165\u7814\u7a76\u4e00\u4e0b\uff1a IntArray \u7684\u6784\u9020\u51fd\u6570\u63a5\u53d7 std::initializer_list \u4f5c\u4e3a \u5f62\u53c2 \u3002 1 2 3 4 5 6 7 8 9 10 11 IntArray ( std :: initializer_list < int > list ) // \u4f7f\u5f97 IntArray \u53ef\u4ee5\u8fdb\u884c\u5217\u8868\u521d\u59cb\u5316 : IntArray ( static_cast < int > ( list . size ())) // \u4f7f\u7528\u59d4\u6d3e\u6784\u9020\u51fd\u6570\u8bbe\u7f6e\u6570\u7ec4 { // \u4f7f\u7528\u5217\u8868\u5143\u7d20\u521d\u59cb\u5316\u6570\u7ec4 int count { 0 }; for ( int element : list ) { m_data [ count ] = element ; ++ count ; } } \u7b2c\u4e00\u884c\uff1a\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u5c16\u62ec\u53f7\u8868\u660e\u5217\u8868\u4e2d\u5143\u7d20\u7684\u7c7b\u578b\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u8fd9\u662f\u4e00\u4e2a IntArray \uff0c\u6240\u4ee5\u521d\u59cb\u5316\u503c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u81ea\u7136\u9700\u8981\u662f int \u3002\u6ce8\u610f\uff0c\u6211\u4eec\u5e76\u6ca1\u6709 \u6309const\u5f15\u7528\u4f20\u9012 \u3002\u548c std::string_view \u7c7b\u4f3c\uff0c std::initializer_list \u662f\u975e\u5e38\u8f7b\u91cf\u7ea7\u7684\uff0c\u6240\u4ee5\u62f7\u8d1d\u6bd4\u95f4\u63a5\u8bbf\u95ee\u5f00\u9500\u8fd8\u5c0f\u3002 \u7b2c\u4e8c\u884c\uff1a\u6211\u4eec\u5fc5\u987b\u5c06\u5185\u5b58\u5206\u914d\u7684\u4efb\u52a1\u59d4\u6d3e\u7ed9\u5176\u4ed6\u6784\u9020\u51fd\u6570\uff08\u901a\u8fc7\u59d4\u6d3e\u6784\u9020\u51cf\u5c11\u5197\u4f59\u4ee3\u7801\uff09\u3002\u8be5\u59d4\u6d3e\u6784\u9020\u51fd\u6570\u9700\u8981\u6307\u5b9a\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u6240\u4ee5\u6211\u4eec\u901a\u8fc7 list.size() \u83b7\u53d6\u957f\u5ea6\u5e76\u5411\u5b83\u4f20\u9012\u8be5\u4fe1\u606f\u3002\u6ce8\u610f list.size() \u8fd4\u56de\u7684\u662f size_t (\u65e0\u7b26\u53f7)\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u5c06\u5176\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u6574\u578b\u3002\u6211\u4eec\u4f7f\u7528 \u76f4\u63a5\u521d\u59cb\u5316 \u800c\u4e0d\u662f \u62ec\u53f7\u521d\u59cb\u5316 \u56e0\u4e3a\u62ec\u53f7\u521d\u59cb\u5316\u9700\u8981 \u5217\u8868\u6784\u9020\u51fd\u6570 \uff08list constructors\uff09\u3002\u5c3d\u7ba1\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u88ab\u6b63\u786e\u89e3\u6790\uff0c\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\u7684\u65b9\u5f0f\u521d\u59cb\u5316\u5177\u6709\u5217\u8868\u6784\u9020\u51fd\u6570\u7684\u7c7b\u662f\u66f4\u5b89\u5168\u7684\uff08\u5982\u679c\u4f60\u672c\u610f\u4e0d\u662f\u4f7f\u7528\u5217\u8868\u6784\u9020\u51fd\u6570\u7684\u8bdd\uff09\u3002 \u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u4e2d\u7684\u4ee3\u7801\u7528\u4e8e\u5c06\u521d\u59cb\u5316\u5217\u8868\u4e2d\u7684\u503c\u62f7\u8d1d\u5230 IntArray \u4e2d\u3002\u4ee4\u4eba\u8d39\u89e3\u7684\u662f\uff0c std::initializer_list \u5e76\u6ca1\u6709\u63d0\u4f9b\u4e0b\u6807\u8fd0\u7b97\u7b26\u7528\u4e8e\u8bbf\u95ee\u5143\u7d20\u3002\u8fd9\u4e2a\u95ee\u9898\u5728C++\u6807\u51c6\u59d4\u5458\u4f1a\u4e0a\u88ab\u63d0\u53ca\u5f88\u591a\u6b21\u4f46\u6bcf\u4e00\u6b21\u90fd\u4e0d\u4e86\u4e86\u4e4b\u4e86\u3002 \u4e0d\u8fc7\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u65b9\u6cd5\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u6700\u7b80\u5355\u7684\u662f\u4f7f\u7528 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \uff0c\u5b83\u53ef\u4ee5\u4e00\u6b21\u904d\u5386\u521d\u59cb\u5316\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u7136\u540e\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u5143\u7d20\u62f7\u8d1d\u5230\u6211\u4eec\u7684\u5185\u90e8\u6570\u7ec4\u4e2d\u3002 \u6ce8\u610f\uff1a\u521d\u59cb\u5316\u5668\u5217\u8868\u603b\u662f\u503e\u5411\u4e8e\u8c03\u7528\u5339\u914d\u7684 initializer_list \u6784\u9020\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u5176\u4ed6\u53ef\u80fd\u5339\u914d\u7684\u6784\u9020\u51fd\u6570\u3002\u56e0\u6b64\uff0c\u8fd9\u4e2a\u53d8\u91cf\u5b9a\u4e49: 1 IntArray array { 5 }; \u4f1a\u5339\u914d\u5230 IntArray(std::initializer_list) \u800c\u4e0d\u662f IntArray(int) \u3002\u5982\u679c\u4f60\u5e0c\u671b\u5728 \u5217\u8868\u6784\u9020\u51fd\u6570 \u5b58\u5728\u7684\u60c5\u51b5\u4e0b\u5339\u914d IntArray(int) \uff0c\u90a3\u4e48\u4f60\u9700\u8981\u4f7f\u7528 \u62f7\u8d1d\u521d\u59cb\u5316 \u6216\u8005 \u76f4\u63a5\u521d\u59cb\u5316 \u3002\u5bf9\u4e8e std::vector \u548c\u5176\u4ed6\u540c\u65f6\u5177\u6709\u5217\u8868\u6784\u9020\u51fd\u6570\u548c\u7c7b\u4f3c\u6784\u9020\u51fd\u6570\u7684 \u5bb9\u5668\u7c7b \u4e5f\u662f\u4e00\u6837\u7684\u3002 1 2 std :: vector < int > array ( 5 ); // Calls std::vector::vector(std::vector::size_type), 5\u4e2a\u5143\u7d20\u88ab\u521d\u59cb\u5316\u4e3a0 std :: vector < int > array { 5 }; // Calls std::vector::vector(std::initializer_list), \u4e00\u4e2a\u5143\u7d20 5 COPY \u4f7f\u7528 std::initializer_list \u5bf9\u7c7b\u8fdb\u884c\u8d4b\u503c \u91cd\u8f7d\u8d4b\u503c\u64cd\u4f5c\u7b26\u63a5\u53d7 std::initializer_list \u5f62\u53c2\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7 std::initializer_list \u6765\u4e3a\u7c7b\u8d4b\u503c\u4e86\u3002\u5de5\u4f5c\u539f\u7406\u4e0e\u4e0a\u9762\u7c7b\u4f3c\u3002\u5177\u4f53\u7684\u5b9e\u4f8b\u89c1quiz\u90e8\u5206\u3002\uff08\u89c1\u539f\u6587\uff09 \u6ce8\u610f\uff0c\u5982\u679c\u4f60\u5b9e\u73b0\u4e86\u4e00\u4e2a\u63a5\u53d7 std::initializer_list \u7684\u6784\u9020\u51fd\u6570\uff0c\u4f60\u5e94\u8be5\u786e\u4fdd\u4f60\u81f3\u5c11\u505a\u4e86\u4ee5\u4e0b\u5176\u4e2d\u4e4b\u4e00: \u63d0\u4f9b\u91cd\u8f7d\u7684 \u5217\u8868\u8d4b\u503c\u8fd0\u7b97\u7b26 \uff1b \u63d0\u4f9b\u6b63\u786e\u7684 \u6df1\u62f7\u8d1d \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \uff1b \u5220\u9664 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u3002 \u4e3a\u4ec0\u4e48\uff1f\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7c7b(\u4e0d\u6ee1\u8db3\u4e0a\u9762\u4e09\u4e2a\u6761\u4ef6)\u5728\u4f7f\u7528\u5217\u8868\u8d4b\u503c\u8bed\u53e5\u65f6\u53ef\u80fd\u51fa\u73b0\u7684\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 #include // for assert() #include // for std::initializer_list #include class IntArray { private : int m_length {}; int * m_data {}; public : IntArray () = default ; IntArray ( int length ) : m_length { length } , m_data { new int [ length ] {} } { } IntArray ( std :: initializer_list < int > list ) // allow IntArray to be initialized via list initialization : IntArray ( static_cast < int > ( list . size ())) // use delegating constructor to set up initial array { // Now initialize our array from the list int count { 0 }; for ( auto element : list ) { m_data [ count ] = element ; ++ count ; } } ~ IntArray () { delete [] m_data ; } // IntArray(const IntArray&) = delete; // to avoid shallow copies // IntArray& operator=(const IntArray& list) = delete; // to avoid shallow copies int & operator []( int index ) { assert ( index >= 0 && index < m_length ); return m_data [ index ]; } int getLength () const { return m_length ; } }; int main () { IntArray array {}; array = { 1 , 3 , 5 , 7 , 9 , 11 }; // Here's our list assignment statement for ( int count { 0 }; count < array . getLength (); ++ count ) std :: cout << array [ count ] << ' ' ; return 0 ; } \u9996\u5148\uff0c\u7f16\u8bd1\u5668\u4f1a\u6ce8\u610f\u5230\u5e26\u6709 std::initializer_list \u7684\u8d4b\u503c\u51fd\u6570\u4e0d\u5b58\u5728\u3002\u63a5\u4e0b\u6765\uff0c\u5b83\u5c06\u67e5\u627e\u5176\u4ed6\u53ef\u4ee5\u4f7f\u7528\u7684\u8d4b\u503c\u51fd\u6570\uff0c\u5e76\u53d1\u73b0 \u9690\u5f0f\u63d0\u4f9b \u7684 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u3002\u4f46\u662f\uff0c\u6b64\u51fd\u6570\u53ea\u5e94\u8be5\u5728\u521d\u59cb\u5316\u5668\u5217\u8868\u80fd\u591f\u88ab\u8f6c\u6362\u4e3a IntArray \u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002\u56e0\u4e3a {1,3,5,7,9,11} \u662f std::initializer_list \uff0c\u7f16\u8bd1\u5668\u5c06\u4f7f\u7528\u5217\u8868\u6784\u9020\u51fd\u6570\u5c06\u521d\u59cb\u5316\u5217\u8868\u8f6c\u6362\u4e3a\u4e34\u65f6\u7684 IntArray \u3002\u7136\u540e\u8c03\u7528\u9690\u5f0f\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u5c06\u4e34\u65f6\u7684 IntArray \u6d45\u62f7\u8d1d\u5230\u6570\u7ec4\u5bf9\u8c61\u4e2d\u3002 \u8fd9\u6837\u4e00\u6765\uff0c\u4e34\u65f6 IntArray \u7684 m_data \u548c array->m_data \u90fd\u6307\u5411\u540c\u4e00\u4e2a\u5730\u5740\uff08\u56e0\u4e3a \u6d45\u62f7\u8d1d \u7684\u539f\u56e0\uff09\u3002\u7ed3\u679c\u4f60\u5e94\u8be5\u4e5f\u80fd\u9884\u6599\u5230\u4e86\u3002 \u5728\u8d4b\u503c\u8bed\u53e5\u7684\u672b\u5c3e\uff0c\u4e34\u65f6\u7684 IntArray \u4f1a\u88ab\u9500\u6bc1\uff0c\u6b64\u65f6\u5b83\u7684\u6790\u6784\u51fd\u6570\u5c31\u4f1a\u5220\u9664 IntArray \u7684 m_data \u3002\u8fd9\u4f7f\u5f97 array->m_data \u79f0\u4e3a\u4e86\u4e00\u4e2a\u60ac\u5782\u6307\u9488\u3002\u5728\u540e\u7eed\u4f7f\u7528 array->m_data \u7684\u65f6\u5019\uff08\u4e0d\u8bba\u51fa\u4e8e\u4ec0\u4e48\u76ee\u7684\uff0c\u5305\u62ec\u6570\u7ec4 \u79bb\u5f00\u4f5c\u7528\u57df \u6216\u8005\u6790\u6784\u51fd\u6570\u5220\u9664 m_data \u65f6\uff09\uff0c\u90fd\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u63d0\u4f9b\u4e86\u5217\u8868\u6784\u9020\u51fd\u6570\uff0c\u8bf7\u540c\u65f6\u63d0\u4f9b\u5217\u8868\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002 \u5c0f\u7ed3 \u5b9e\u73b0\u4e00\u4e2a\u63a5\u53d7 std::initializer_list \u5f62\u53c2\u7684\u6784\u9020\u51fd\u6570\uff0c\u5141\u8bb8\u6211\u4eec\u5bf9\u81ea\u5b9a\u4e49\u7c7b\u4f7f\u7528\u5217\u8868\u521d\u59cb\u5316\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528 std::initializer_list \u6765\u5b9e\u73b0\u5176\u4ed6\u9700\u8981\u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u7684\u51fd\u6570\uff0c\u4f8b\u5982\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002","title":"16.7 - std::initializer_list"},{"location":"16-7-std-initializer_list/#stdinitializer_list","text":"\u5f53\u7f16\u8bd1\u5668\u770b\u5230\u521d\u59cb\u5316\u503c\u5217\u8868\u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u5730\u5c06\u521d\u59cb\u5316\u503c\u5217\u8868\u8f6c\u6362\u4e3a\u4e00\u4e2a std::initializer_list \u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u63a5\u6536 std::initializer_list \u7c7b\u578b\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u7684\u8bdd\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7\u521d\u59cb\u5316\u503c\u5217\u8868\u521b\u5efa\u5bf9\u8c61\u4e86\u3002 std::initializer_list \u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d\u3002 \u5173\u4e8e std::initializer_list \uff0c\u6709\u4e9b\u4e8b\u60c5\u4f60\u5fc5\u987b\u6307\u5b9a\u3002\u548c std::array \u6216\u8005 std::vector \u7c7b\u4f3c\uff0c\u4f60\u5fc5\u987b\u544a\u8bc9 std::initializer_list \u5217\u8868\u4e2d\u7684\u5143\u7d20\u7c7b\u578b\u662f\u4ec0\u4e48\uff0c\u9664\u975e\u4f60\u662f\u7acb\u5373\u5bf9\u5176\u521d\u59cb\u5316\u3002\u56e0\u6b64\u4f60\u5f88\u5c11\u4f1a\u770b\u5230 std::initializer_list \u7684\u6700\u7b80\u5f62\u5f0f\u3002\u591a\u6570\u60c5\u51b5\u4e0b\u4f60\u770b\u5230\u7684\u5e94\u8be5\u662f\u8fd9\u6837\u7684 std::initializer_list \u6216\u8fd9\u6837\u7684 std::initializer_list \u3002 \u7b2c\u4e8c\uff0c std::initializer_list \u6709\u4e00\u4e2a size() \u6210\u5458\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u5143\u7d20\u4e2a\u6570\u3002\u5f53\u6211\u4eec\u9700\u8981\u6307\u5b9a\u4f20\u5165\u7684\u521d\u59cb\u5316\u5217\u8868\u591a\u957f\u65f6\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u6709\u7528\u3002 \u4f7f\u7528 std::initializer_list \u5bf9 IntArray \u6539\u5199\u4e4b\u540e\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 #include // for assert() #include // for std::initializer_list #include class IntArray { private : int m_length {}; int * m_data {}; public : IntArray () = default ; IntArray ( int length ) : m_length { length } , m_data { new int [ length ]{} } { } IntArray ( std :: initializer_list < int > list ) // allow IntArray to be initialized via list initialization : IntArray ( static_cast < int > ( list . size ())) // use delegating constructor to set up initial array { // Now initialize our array from the list int count { 0 }; for ( auto element : list ) { m_data [ count ] = element ; ++ count ; } } ~ IntArray () { delete [] m_data ; // we don't need to set m_data to null or m_length to 0 here, since the object will be destroyed immediately after this function anyway } IntArray ( const IntArray & ) = delete ; // to avoid shallow copies IntArray & operator = ( const IntArray & list ) = delete ; // to avoid shallow copies int & operator []( int index ) { assert ( index >= 0 && index < m_length ); return m_data [ index ]; } int getLength () const { return m_length ; } }; int main () { IntArray array { 5 , 4 , 3 , 2 , 1 }; // initializer list for ( int count { 0 }; count < array . getLength (); ++ count ) std :: cout << array [ count ] << ' ' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u7b26\u5408\u9884\u671f\uff1a 1 5 4 3 2 1 \u5f88\u597d\uff01\u63a5\u4e0b\u6765\u8ba9\u6211\u4eec\u6df1\u5165\u7814\u7a76\u4e00\u4e0b\uff1a IntArray \u7684\u6784\u9020\u51fd\u6570\u63a5\u53d7 std::initializer_list \u4f5c\u4e3a \u5f62\u53c2 \u3002 1 2 3 4 5 6 7 8 9 10 11 IntArray ( std :: initializer_list < int > list ) // \u4f7f\u5f97 IntArray \u53ef\u4ee5\u8fdb\u884c\u5217\u8868\u521d\u59cb\u5316 : IntArray ( static_cast < int > ( list . size ())) // \u4f7f\u7528\u59d4\u6d3e\u6784\u9020\u51fd\u6570\u8bbe\u7f6e\u6570\u7ec4 { // \u4f7f\u7528\u5217\u8868\u5143\u7d20\u521d\u59cb\u5316\u6570\u7ec4 int count { 0 }; for ( int element : list ) { m_data [ count ] = element ; ++ count ; } } \u7b2c\u4e00\u884c\uff1a\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u5c16\u62ec\u53f7\u8868\u660e\u5217\u8868\u4e2d\u5143\u7d20\u7684\u7c7b\u578b\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u8fd9\u662f\u4e00\u4e2a IntArray \uff0c\u6240\u4ee5\u521d\u59cb\u5316\u503c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u81ea\u7136\u9700\u8981\u662f int \u3002\u6ce8\u610f\uff0c\u6211\u4eec\u5e76\u6ca1\u6709 \u6309const\u5f15\u7528\u4f20\u9012 \u3002\u548c std::string_view \u7c7b\u4f3c\uff0c std::initializer_list \u662f\u975e\u5e38\u8f7b\u91cf\u7ea7\u7684\uff0c\u6240\u4ee5\u62f7\u8d1d\u6bd4\u95f4\u63a5\u8bbf\u95ee\u5f00\u9500\u8fd8\u5c0f\u3002 \u7b2c\u4e8c\u884c\uff1a\u6211\u4eec\u5fc5\u987b\u5c06\u5185\u5b58\u5206\u914d\u7684\u4efb\u52a1\u59d4\u6d3e\u7ed9\u5176\u4ed6\u6784\u9020\u51fd\u6570\uff08\u901a\u8fc7\u59d4\u6d3e\u6784\u9020\u51cf\u5c11\u5197\u4f59\u4ee3\u7801\uff09\u3002\u8be5\u59d4\u6d3e\u6784\u9020\u51fd\u6570\u9700\u8981\u6307\u5b9a\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u6240\u4ee5\u6211\u4eec\u901a\u8fc7 list.size() \u83b7\u53d6\u957f\u5ea6\u5e76\u5411\u5b83\u4f20\u9012\u8be5\u4fe1\u606f\u3002\u6ce8\u610f list.size() \u8fd4\u56de\u7684\u662f size_t (\u65e0\u7b26\u53f7)\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u5c06\u5176\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u6574\u578b\u3002\u6211\u4eec\u4f7f\u7528 \u76f4\u63a5\u521d\u59cb\u5316 \u800c\u4e0d\u662f \u62ec\u53f7\u521d\u59cb\u5316 \u56e0\u4e3a\u62ec\u53f7\u521d\u59cb\u5316\u9700\u8981 \u5217\u8868\u6784\u9020\u51fd\u6570 \uff08list constructors\uff09\u3002\u5c3d\u7ba1\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u88ab\u6b63\u786e\u89e3\u6790\uff0c\u4f7f\u7528\u76f4\u63a5\u521d\u59cb\u5316\u7684\u65b9\u5f0f\u521d\u59cb\u5316\u5177\u6709\u5217\u8868\u6784\u9020\u51fd\u6570\u7684\u7c7b\u662f\u66f4\u5b89\u5168\u7684\uff08\u5982\u679c\u4f60\u672c\u610f\u4e0d\u662f\u4f7f\u7528\u5217\u8868\u6784\u9020\u51fd\u6570\u7684\u8bdd\uff09\u3002 \u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u4e2d\u7684\u4ee3\u7801\u7528\u4e8e\u5c06\u521d\u59cb\u5316\u5217\u8868\u4e2d\u7684\u503c\u62f7\u8d1d\u5230 IntArray \u4e2d\u3002\u4ee4\u4eba\u8d39\u89e3\u7684\u662f\uff0c std::initializer_list \u5e76\u6ca1\u6709\u63d0\u4f9b\u4e0b\u6807\u8fd0\u7b97\u7b26\u7528\u4e8e\u8bbf\u95ee\u5143\u7d20\u3002\u8fd9\u4e2a\u95ee\u9898\u5728C++\u6807\u51c6\u59d4\u5458\u4f1a\u4e0a\u88ab\u63d0\u53ca\u5f88\u591a\u6b21\u4f46\u6bcf\u4e00\u6b21\u90fd\u4e0d\u4e86\u4e86\u4e4b\u4e86\u3002 \u4e0d\u8fc7\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u65b9\u6cd5\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u6700\u7b80\u5355\u7684\u662f\u4f7f\u7528 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \uff0c\u5b83\u53ef\u4ee5\u4e00\u6b21\u904d\u5386\u521d\u59cb\u5316\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u7136\u540e\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u5143\u7d20\u62f7\u8d1d\u5230\u6211\u4eec\u7684\u5185\u90e8\u6570\u7ec4\u4e2d\u3002 \u6ce8\u610f\uff1a\u521d\u59cb\u5316\u5668\u5217\u8868\u603b\u662f\u503e\u5411\u4e8e\u8c03\u7528\u5339\u914d\u7684 initializer_list \u6784\u9020\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u5176\u4ed6\u53ef\u80fd\u5339\u914d\u7684\u6784\u9020\u51fd\u6570\u3002\u56e0\u6b64\uff0c\u8fd9\u4e2a\u53d8\u91cf\u5b9a\u4e49: 1 IntArray array { 5 }; \u4f1a\u5339\u914d\u5230 IntArray(std::initializer_list) \u800c\u4e0d\u662f IntArray(int) \u3002\u5982\u679c\u4f60\u5e0c\u671b\u5728 \u5217\u8868\u6784\u9020\u51fd\u6570 \u5b58\u5728\u7684\u60c5\u51b5\u4e0b\u5339\u914d IntArray(int) \uff0c\u90a3\u4e48\u4f60\u9700\u8981\u4f7f\u7528 \u62f7\u8d1d\u521d\u59cb\u5316 \u6216\u8005 \u76f4\u63a5\u521d\u59cb\u5316 \u3002\u5bf9\u4e8e std::vector \u548c\u5176\u4ed6\u540c\u65f6\u5177\u6709\u5217\u8868\u6784\u9020\u51fd\u6570\u548c\u7c7b\u4f3c\u6784\u9020\u51fd\u6570\u7684 \u5bb9\u5668\u7c7b \u4e5f\u662f\u4e00\u6837\u7684\u3002 1 2 std :: vector < int > array ( 5 ); // Calls std::vector::vector(std::vector::size_type), 5\u4e2a\u5143\u7d20\u88ab\u521d\u59cb\u5316\u4e3a0 std :: vector < int > array { 5 }; // Calls std::vector::vector(std::initializer_list), \u4e00\u4e2a\u5143\u7d20 5 COPY","title":"\u4f7f\u7528 std::initializer_list\u521d\u59cb\u5316"},{"location":"16-7-std-initializer_list/#stdinitializer_list_1","text":"\u91cd\u8f7d\u8d4b\u503c\u64cd\u4f5c\u7b26\u63a5\u53d7 std::initializer_list \u5f62\u53c2\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7 std::initializer_list \u6765\u4e3a\u7c7b\u8d4b\u503c\u4e86\u3002\u5de5\u4f5c\u539f\u7406\u4e0e\u4e0a\u9762\u7c7b\u4f3c\u3002\u5177\u4f53\u7684\u5b9e\u4f8b\u89c1quiz\u90e8\u5206\u3002\uff08\u89c1\u539f\u6587\uff09 \u6ce8\u610f\uff0c\u5982\u679c\u4f60\u5b9e\u73b0\u4e86\u4e00\u4e2a\u63a5\u53d7 std::initializer_list \u7684\u6784\u9020\u51fd\u6570\uff0c\u4f60\u5e94\u8be5\u786e\u4fdd\u4f60\u81f3\u5c11\u505a\u4e86\u4ee5\u4e0b\u5176\u4e2d\u4e4b\u4e00: \u63d0\u4f9b\u91cd\u8f7d\u7684 \u5217\u8868\u8d4b\u503c\u8fd0\u7b97\u7b26 \uff1b \u63d0\u4f9b\u6b63\u786e\u7684 \u6df1\u62f7\u8d1d \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \uff1b \u5220\u9664 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u3002 \u4e3a\u4ec0\u4e48\uff1f\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7c7b(\u4e0d\u6ee1\u8db3\u4e0a\u9762\u4e09\u4e2a\u6761\u4ef6)\u5728\u4f7f\u7528\u5217\u8868\u8d4b\u503c\u8bed\u53e5\u65f6\u53ef\u80fd\u51fa\u73b0\u7684\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 #include // for assert() #include // for std::initializer_list #include class IntArray { private : int m_length {}; int * m_data {}; public : IntArray () = default ; IntArray ( int length ) : m_length { length } , m_data { new int [ length ] {} } { } IntArray ( std :: initializer_list < int > list ) // allow IntArray to be initialized via list initialization : IntArray ( static_cast < int > ( list . size ())) // use delegating constructor to set up initial array { // Now initialize our array from the list int count { 0 }; for ( auto element : list ) { m_data [ count ] = element ; ++ count ; } } ~ IntArray () { delete [] m_data ; } // IntArray(const IntArray&) = delete; // to avoid shallow copies // IntArray& operator=(const IntArray& list) = delete; // to avoid shallow copies int & operator []( int index ) { assert ( index >= 0 && index < m_length ); return m_data [ index ]; } int getLength () const { return m_length ; } }; int main () { IntArray array {}; array = { 1 , 3 , 5 , 7 , 9 , 11 }; // Here's our list assignment statement for ( int count { 0 }; count < array . getLength (); ++ count ) std :: cout << array [ count ] << ' ' ; return 0 ; } \u9996\u5148\uff0c\u7f16\u8bd1\u5668\u4f1a\u6ce8\u610f\u5230\u5e26\u6709 std::initializer_list \u7684\u8d4b\u503c\u51fd\u6570\u4e0d\u5b58\u5728\u3002\u63a5\u4e0b\u6765\uff0c\u5b83\u5c06\u67e5\u627e\u5176\u4ed6\u53ef\u4ee5\u4f7f\u7528\u7684\u8d4b\u503c\u51fd\u6570\uff0c\u5e76\u53d1\u73b0 \u9690\u5f0f\u63d0\u4f9b \u7684 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u3002\u4f46\u662f\uff0c\u6b64\u51fd\u6570\u53ea\u5e94\u8be5\u5728\u521d\u59cb\u5316\u5668\u5217\u8868\u80fd\u591f\u88ab\u8f6c\u6362\u4e3a IntArray \u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002\u56e0\u4e3a {1,3,5,7,9,11} \u662f std::initializer_list \uff0c\u7f16\u8bd1\u5668\u5c06\u4f7f\u7528\u5217\u8868\u6784\u9020\u51fd\u6570\u5c06\u521d\u59cb\u5316\u5217\u8868\u8f6c\u6362\u4e3a\u4e34\u65f6\u7684 IntArray \u3002\u7136\u540e\u8c03\u7528\u9690\u5f0f\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u5c06\u4e34\u65f6\u7684 IntArray \u6d45\u62f7\u8d1d\u5230\u6570\u7ec4\u5bf9\u8c61\u4e2d\u3002 \u8fd9\u6837\u4e00\u6765\uff0c\u4e34\u65f6 IntArray \u7684 m_data \u548c array->m_data \u90fd\u6307\u5411\u540c\u4e00\u4e2a\u5730\u5740\uff08\u56e0\u4e3a \u6d45\u62f7\u8d1d \u7684\u539f\u56e0\uff09\u3002\u7ed3\u679c\u4f60\u5e94\u8be5\u4e5f\u80fd\u9884\u6599\u5230\u4e86\u3002 \u5728\u8d4b\u503c\u8bed\u53e5\u7684\u672b\u5c3e\uff0c\u4e34\u65f6\u7684 IntArray \u4f1a\u88ab\u9500\u6bc1\uff0c\u6b64\u65f6\u5b83\u7684\u6790\u6784\u51fd\u6570\u5c31\u4f1a\u5220\u9664 IntArray \u7684 m_data \u3002\u8fd9\u4f7f\u5f97 array->m_data \u79f0\u4e3a\u4e86\u4e00\u4e2a\u60ac\u5782\u6307\u9488\u3002\u5728\u540e\u7eed\u4f7f\u7528 array->m_data \u7684\u65f6\u5019\uff08\u4e0d\u8bba\u51fa\u4e8e\u4ec0\u4e48\u76ee\u7684\uff0c\u5305\u62ec\u6570\u7ec4 \u79bb\u5f00\u4f5c\u7528\u57df \u6216\u8005\u6790\u6784\u51fd\u6570\u5220\u9664 m_data \u65f6\uff09\uff0c\u90fd\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u63d0\u4f9b\u4e86\u5217\u8868\u6784\u9020\u51fd\u6570\uff0c\u8bf7\u540c\u65f6\u63d0\u4f9b\u5217\u8868\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002","title":"\u4f7f\u7528 std::initializer_list \u5bf9\u7c7b\u8fdb\u884c\u8d4b\u503c"},{"location":"16-7-std-initializer_list/#_1","text":"\u5b9e\u73b0\u4e00\u4e2a\u63a5\u53d7 std::initializer_list \u5f62\u53c2\u7684\u6784\u9020\u51fd\u6570\uff0c\u5141\u8bb8\u6211\u4eec\u5bf9\u81ea\u5b9a\u4e49\u7c7b\u4f7f\u7528\u5217\u8868\u521d\u59cb\u5316\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528 std::initializer_list \u6765\u5b9e\u73b0\u5176\u4ed6\u9700\u8981\u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u7684\u51fd\u6570\uff0c\u4f8b\u5982\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002","title":"\u5c0f\u7ed3"},{"location":"16-x-Chapter-16-comprehensive-quiz/","tags":["summary"],"text":"\u590d\u4e60 16.2 - \u7ec4\u5408\u5173\u7cfb \u4f7f\u7528\u7b80\u5355\u5bf9\u8c61\u6784\u5efa\u590d\u6742\u5bf9\u8c61\u7684\u8fc7\u7a0b\uff0c\u79f0\u4e3a \u5bf9\u8c61\u7ec4\u5408 \u3002\u5bf9\u8c61\u7ec4\u5408\u6709\u4e24\u79cd\uff1a \u7ec4\u5408 \u548c \u805a\u5408 \u3002 \u5f53\u4e00\u4e2a\u7c7b\u7684\u7c7b\u6210\u5458\u4e3a\u8be5\u7c7b\u7684 \u4e00\u90e8\u5206 \uff08part-of\uff09\u65f6\uff0c\u5c31\u4ea7\u751f\u4e86\u7ec4\u5408\u5173\u7cfb\u3002\u5728\u7ec4\u5408\u5173\u7cfb\u4e2d\uff0c\u7c7b\u8d1f\u8d23\u7ba1\u7406\u6210\u5458\u662f\u5426\u5b58\u5728\u3002\u4e00\u4e2a\u5408\u683c\u7684\u7ec4\u5408\u5173\u7cfb\u5e94\u8be5\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\uff1a \u90e8\u5206(\u6210\u5458) \u662f\u5bf9\u8c61\uff08\u7c7b\uff09\u7684\u4e00\u90e8\u5206\uff1b \u90e8\u5206(\u6210\u5458) \u540c\u65f6\u53ea\u80fd\u5c5e\u4e8e\u4e00\u4e2a\u5bf9\u8c61\uff08\u7c7b\uff09; \u90e8\u5206(\u6210\u5458) \u7684\u5b58\u5728\u4e0e\u5426\u53d7\u5230\u5bf9\u8c61\uff08\u7c7b\uff09\u7684\u7ba1\u7406\uff1b \u90e8\u5206(\u6210\u5458) \u5e76\u4e0d\u77e5\u9053\u5bf9\u8c61\uff08\u7c7b\uff09\u662f\u5426\u5b58\u5728\u3002 \u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u7ec4\u5408\u5173\u7cfb\u53ef\u4ee5\u4f7f\u7528\u4e00\u822c\u7684\u6210\u5458\u53d8\u91cf\u6765\u5b9e\u73b0\uff0c\u6216\u8005\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\uff0c\u4f46\u5176\u8868\u793a\u7684\u5185\u5b58\u7684\u7533\u8bf7\u548c\u91ca\u653e\u5fc5\u987b\u5b8c\u5168\u7531\u8be5\u7c7b\u6765\u7ba1\u7406\u3002\u5982\u679c\u4f60\u80fd\u591f\u5c06\u4e00\u4e2a\u7c7b\u5b9e\u73b0\u4e3a\u7ec4\u5408\u5173\u7cfb\uff0c\u90a3\u4f60\u5c31\u5e94\u8be5\u5c3d\u91cf\u8fd9\u4e48\u505a\u3002 16.3 - \u805a\u5408\u5173\u7cfb \u5f53\u67d0\u4e2a\u7c7b \u5177\u6709\uff08has\uff09 \u67d0\u4e2a\u6210\u5458\u662f\uff0c \u805a\u5408 \u5173\u7cfb\u5c31\u51fa\u73b0\u4e86\u3002\u5728\u805a\u5408\u5173\u7cfb\u4e2d\uff0c\u7c7b\u5e76\u4e0d\u8d1f\u8d23\u7ba1\u7406\u8be5\u6210\u5458\u7684\u5b58\u5728\u6027\u3002\u4e00\u4e2a\u5408\u683c\u7684\u805a\u5408\u5173\u7cfb\u5e94\u8be5\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\uff1a \u90e8\u5206(\u6210\u5458) \u662f\u5bf9\u8c61\uff08\u7c7b\uff09\u7684\u4e00\u90e8\u5206\uff1b \u90e8\u5206(\u6210\u5458) \u4e0d\u4ec5\u4ec5\u5c5e\u4e8e\u4e00\u4e2a\u5bf9\u8c61\uff08\u7c7b\uff09; \u90e8\u5206(\u6210\u5458) \u7684\u5b58\u5728\u4e0e\u5426\u4e0d\u4f1a\u53d7\u5230\u5bf9\u8c61\uff08\u7c7b\uff09\u7684\u7ba1\u7406\uff1b \u90e8\u5206(\u6210\u5458) \u5e76\u4e0d\u77e5\u9053\u5bf9\u8c61\uff08\u7c7b\uff09\u662f\u5426\u5b58\u5728\u3002 \u805a\u5408\u5173\u7cfb\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u548c\u5f15\u7528\u6765\u5b9e\u73b0\u3002 16.4 - \u5173\u8054\u5173\u7cfb \u5173\u8054 \u662f\u4e00\u79cd\u76f8\u5bf9\u66f4\u52a0\u677e\u6563\u7684\u5173\u7cfb\u3002\u7c7b\u4f7f\u7528\u4e86\u4e00\u4e2a\u4e0e\u5b83\u5e76\u65e0\u76f4\u63a5\u5173\u7cfb\u7684\u5bf9\u8c61\u3002\u53ea\u6709\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\uff0c\u624d\u80fd\u5c06\u4e24\u4e2a\u5bf9\u8c61\u79f0\u4e3a\u5173\u8054\u5173\u7cfb\uff1a \u5173\u8054\u5bf9\u8c61\uff08\u6210\u5458\uff09\u4e0e\u8be5\u5bf9\u8c61\uff08\u7c7b\uff09\u672c\u4e0d\u8be5\u76f8\u5173\uff1b \u5173\u8054\u5bf9\u8c61\uff08\u6210\u5458\uff09\u53ef\u4ee5\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u5bf9\u8c61\uff08\u7c7b\uff09\uff1b \u5173\u8054\u5bf9\u8c61\uff08\u6210\u5458\uff09\u5b58\u5728\u4e0e\u5426\u4e0d\u4f1a\u53d7\u5230\u5bf9\u8c61\uff08\u7c7b\uff09\u7684\u7ba1\u7406\uff1b \u5173\u8054\u5bf9\u8c61\uff08\u6210\u5458\uff09\u5bf9\u5bf9\u8c61\uff08\u6216\u7c7b\uff09\u7684\u5b58\u5728\uff0c\u53ef\u80fd\u77e5\u6653\u4e5f\u53ef\u80fd\u4e0d\u77e5\u6653\u3002 \u5173\u8054\u5173\u7cfb\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u6216\u5f15\u7528\u6765\u5b9e\u73b0\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u66f4\u52a0\u95f4\u63a5\u7684\u65b9\u5f0f\u5b9e\u73b0\uff08\u4f8b\u5982\u901a\u8fc7\u5173\u8054\u5bf9\u8c61\u7684\u5bf9\u5e94\u952e\u6216\u7d22\u5f15\u6765\u4f7f\u7528\u5b83\uff09\u3002 16.5 - \u4f9d\u8d56\u5173\u7cfb \u5f53\u4e00\u4e2a\u7c7b\u4f7f\u7528\u53e6\u4e00\u4e2a\u7c7b\u5b8c\u6210\u67d0\u9879\u4efb\u52a1\u65f6\uff0c\u5b83\u4eec\u4e4b\u95f4\u5c5e\u4e8e \u4f9d\u8d56 \u5173\u7cfb\u3002\u88ab\u4f9d\u8d56\u7684\u5bf9\u8c61\u901a\u5e38\u5e76\u4e0d\u662f\u4f7f\u7528\u5b83\u7684\u5bf9\u8c61\u7684\u4e00\u90e8\u5206\u3002\u88ab\u4f7f\u7528\u7684\u5bf9\u8c61\u901a\u5e38\u662f\u88ab\u4e34\u65f6\u521b\u5efa\u3001\u4f7f\u7528\u7136\u540e\u9500\u6bc1\uff0c\u6216\u8005\u662f\u901a\u8fc7\u6210\u5458\u51fd\u6570\u4f20\u5165\u5e76\u4f7f\u7528\u7684\u5916\u90e8\u8d44\u6e90\u3002 16.6 - \u5bb9\u5668\u7c7b \u5bb9\u5668\u7c7b(container class) \u6307\u7684\u662f\u4e3a\u5176\u4ed6\u4e00\u4e9b\u7c7b\u63d0\u4f9b\u5bb9\u5668\u6765\u5b58\u50a8\u5b83\u4eec\u7684\u7c7b\u3002 \u503c\u5bb9\u5668 \u548c\u5b83\u4fdd\u5b58\u7684\u6570\u636e\u526f\u672c\u5c5e\u4e8e\u7ec4\u5408\u5173\u7cfb\u3002 \u5f15\u7528\u5bb9\u5668 \u662f\u4e00\u79cd\u805a\u5408\u5173\u7cfb\uff0c\u5b83\u4fdd\u5b58\u4e86\u5bb9\u5668\u5916\u90e8\u5bf9\u8c61\u7684\u6307\u9488\u6216\u5f15\u7528\u3002 16.7 - std::initializer_list std::initializer_list \u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u6784\u9020\u51fd\u6570\u3001\u8d4b\u503c\u64cd\u4f5c\u7b26\u548c\u5176\u4ed6\u63a5\u53d7\u5217\u8868\u521d\u59cb\u5316\u53c2\u6570\u7684\u51fd\u6570\u3002 std::initailizer_list \u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d\u3002 \u5c5e\u6027\u7c7b\u578b \u7ec4\u5408 \u805a\u5408 \u5173\u8054 \u4f9d\u8d56 \u5173\u7cfb\u7c7b\u578b \u6574\u4f53/\u90e8\u5206 \u6574\u4f53/\u90e8\u5206 \u672c\u4e0d\u76f8\u5173 \u672c\u4e0d\u76f8\u5173 \u6210\u5458\u662f\u5426\u53ef\u4ee5\u5c5e\u4e8e\u591a\u4e2a\u7c7b No Yes Yes Yes \u6210\u5458\u5b58\u5728\u4e0e\u5426\u662f\u5426\u53d7\u7c7b\u7ba1\u7406 Yes No No No \u65b9\u5411 \u5355\u5411 \u5355\u5411 \u5355\u5411\u6216\u53cc\u5411 \u5355\u5411\u6216\u53cc\u5411 \u5173\u7cfb\u52a8\u8bcd Part-of Has-a Uses-a Depends-on","title":"16.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5bf9\u8c61\u5173\u7cfb"},{"location":"16-x-Chapter-16-comprehensive-quiz/#_1","text":"16.2 - \u7ec4\u5408\u5173\u7cfb \u4f7f\u7528\u7b80\u5355\u5bf9\u8c61\u6784\u5efa\u590d\u6742\u5bf9\u8c61\u7684\u8fc7\u7a0b\uff0c\u79f0\u4e3a \u5bf9\u8c61\u7ec4\u5408 \u3002\u5bf9\u8c61\u7ec4\u5408\u6709\u4e24\u79cd\uff1a \u7ec4\u5408 \u548c \u805a\u5408 \u3002 \u5f53\u4e00\u4e2a\u7c7b\u7684\u7c7b\u6210\u5458\u4e3a\u8be5\u7c7b\u7684 \u4e00\u90e8\u5206 \uff08part-of\uff09\u65f6\uff0c\u5c31\u4ea7\u751f\u4e86\u7ec4\u5408\u5173\u7cfb\u3002\u5728\u7ec4\u5408\u5173\u7cfb\u4e2d\uff0c\u7c7b\u8d1f\u8d23\u7ba1\u7406\u6210\u5458\u662f\u5426\u5b58\u5728\u3002\u4e00\u4e2a\u5408\u683c\u7684\u7ec4\u5408\u5173\u7cfb\u5e94\u8be5\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\uff1a \u90e8\u5206(\u6210\u5458) \u662f\u5bf9\u8c61\uff08\u7c7b\uff09\u7684\u4e00\u90e8\u5206\uff1b \u90e8\u5206(\u6210\u5458) \u540c\u65f6\u53ea\u80fd\u5c5e\u4e8e\u4e00\u4e2a\u5bf9\u8c61\uff08\u7c7b\uff09; \u90e8\u5206(\u6210\u5458) \u7684\u5b58\u5728\u4e0e\u5426\u53d7\u5230\u5bf9\u8c61\uff08\u7c7b\uff09\u7684\u7ba1\u7406\uff1b \u90e8\u5206(\u6210\u5458) \u5e76\u4e0d\u77e5\u9053\u5bf9\u8c61\uff08\u7c7b\uff09\u662f\u5426\u5b58\u5728\u3002 \u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u7ec4\u5408\u5173\u7cfb\u53ef\u4ee5\u4f7f\u7528\u4e00\u822c\u7684\u6210\u5458\u53d8\u91cf\u6765\u5b9e\u73b0\uff0c\u6216\u8005\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\uff0c\u4f46\u5176\u8868\u793a\u7684\u5185\u5b58\u7684\u7533\u8bf7\u548c\u91ca\u653e\u5fc5\u987b\u5b8c\u5168\u7531\u8be5\u7c7b\u6765\u7ba1\u7406\u3002\u5982\u679c\u4f60\u80fd\u591f\u5c06\u4e00\u4e2a\u7c7b\u5b9e\u73b0\u4e3a\u7ec4\u5408\u5173\u7cfb\uff0c\u90a3\u4f60\u5c31\u5e94\u8be5\u5c3d\u91cf\u8fd9\u4e48\u505a\u3002 16.3 - \u805a\u5408\u5173\u7cfb \u5f53\u67d0\u4e2a\u7c7b \u5177\u6709\uff08has\uff09 \u67d0\u4e2a\u6210\u5458\u662f\uff0c \u805a\u5408 \u5173\u7cfb\u5c31\u51fa\u73b0\u4e86\u3002\u5728\u805a\u5408\u5173\u7cfb\u4e2d\uff0c\u7c7b\u5e76\u4e0d\u8d1f\u8d23\u7ba1\u7406\u8be5\u6210\u5458\u7684\u5b58\u5728\u6027\u3002\u4e00\u4e2a\u5408\u683c\u7684\u805a\u5408\u5173\u7cfb\u5e94\u8be5\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\uff1a \u90e8\u5206(\u6210\u5458) \u662f\u5bf9\u8c61\uff08\u7c7b\uff09\u7684\u4e00\u90e8\u5206\uff1b \u90e8\u5206(\u6210\u5458) \u4e0d\u4ec5\u4ec5\u5c5e\u4e8e\u4e00\u4e2a\u5bf9\u8c61\uff08\u7c7b\uff09; \u90e8\u5206(\u6210\u5458) \u7684\u5b58\u5728\u4e0e\u5426\u4e0d\u4f1a\u53d7\u5230\u5bf9\u8c61\uff08\u7c7b\uff09\u7684\u7ba1\u7406\uff1b \u90e8\u5206(\u6210\u5458) \u5e76\u4e0d\u77e5\u9053\u5bf9\u8c61\uff08\u7c7b\uff09\u662f\u5426\u5b58\u5728\u3002 \u805a\u5408\u5173\u7cfb\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u548c\u5f15\u7528\u6765\u5b9e\u73b0\u3002 16.4 - \u5173\u8054\u5173\u7cfb \u5173\u8054 \u662f\u4e00\u79cd\u76f8\u5bf9\u66f4\u52a0\u677e\u6563\u7684\u5173\u7cfb\u3002\u7c7b\u4f7f\u7528\u4e86\u4e00\u4e2a\u4e0e\u5b83\u5e76\u65e0\u76f4\u63a5\u5173\u7cfb\u7684\u5bf9\u8c61\u3002\u53ea\u6709\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\uff0c\u624d\u80fd\u5c06\u4e24\u4e2a\u5bf9\u8c61\u79f0\u4e3a\u5173\u8054\u5173\u7cfb\uff1a \u5173\u8054\u5bf9\u8c61\uff08\u6210\u5458\uff09\u4e0e\u8be5\u5bf9\u8c61\uff08\u7c7b\uff09\u672c\u4e0d\u8be5\u76f8\u5173\uff1b \u5173\u8054\u5bf9\u8c61\uff08\u6210\u5458\uff09\u53ef\u4ee5\u540c\u65f6\u5c5e\u4e8e\u591a\u4e2a\u5bf9\u8c61\uff08\u7c7b\uff09\uff1b \u5173\u8054\u5bf9\u8c61\uff08\u6210\u5458\uff09\u5b58\u5728\u4e0e\u5426\u4e0d\u4f1a\u53d7\u5230\u5bf9\u8c61\uff08\u7c7b\uff09\u7684\u7ba1\u7406\uff1b \u5173\u8054\u5bf9\u8c61\uff08\u6210\u5458\uff09\u5bf9\u5bf9\u8c61\uff08\u6216\u7c7b\uff09\u7684\u5b58\u5728\uff0c\u53ef\u80fd\u77e5\u6653\u4e5f\u53ef\u80fd\u4e0d\u77e5\u6653\u3002 \u5173\u8054\u5173\u7cfb\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u6216\u5f15\u7528\u6765\u5b9e\u73b0\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u66f4\u52a0\u95f4\u63a5\u7684\u65b9\u5f0f\u5b9e\u73b0\uff08\u4f8b\u5982\u901a\u8fc7\u5173\u8054\u5bf9\u8c61\u7684\u5bf9\u5e94\u952e\u6216\u7d22\u5f15\u6765\u4f7f\u7528\u5b83\uff09\u3002 16.5 - \u4f9d\u8d56\u5173\u7cfb \u5f53\u4e00\u4e2a\u7c7b\u4f7f\u7528\u53e6\u4e00\u4e2a\u7c7b\u5b8c\u6210\u67d0\u9879\u4efb\u52a1\u65f6\uff0c\u5b83\u4eec\u4e4b\u95f4\u5c5e\u4e8e \u4f9d\u8d56 \u5173\u7cfb\u3002\u88ab\u4f9d\u8d56\u7684\u5bf9\u8c61\u901a\u5e38\u5e76\u4e0d\u662f\u4f7f\u7528\u5b83\u7684\u5bf9\u8c61\u7684\u4e00\u90e8\u5206\u3002\u88ab\u4f7f\u7528\u7684\u5bf9\u8c61\u901a\u5e38\u662f\u88ab\u4e34\u65f6\u521b\u5efa\u3001\u4f7f\u7528\u7136\u540e\u9500\u6bc1\uff0c\u6216\u8005\u662f\u901a\u8fc7\u6210\u5458\u51fd\u6570\u4f20\u5165\u5e76\u4f7f\u7528\u7684\u5916\u90e8\u8d44\u6e90\u3002 16.6 - \u5bb9\u5668\u7c7b \u5bb9\u5668\u7c7b(container class) \u6307\u7684\u662f\u4e3a\u5176\u4ed6\u4e00\u4e9b\u7c7b\u63d0\u4f9b\u5bb9\u5668\u6765\u5b58\u50a8\u5b83\u4eec\u7684\u7c7b\u3002 \u503c\u5bb9\u5668 \u548c\u5b83\u4fdd\u5b58\u7684\u6570\u636e\u526f\u672c\u5c5e\u4e8e\u7ec4\u5408\u5173\u7cfb\u3002 \u5f15\u7528\u5bb9\u5668 \u662f\u4e00\u79cd\u805a\u5408\u5173\u7cfb\uff0c\u5b83\u4fdd\u5b58\u4e86\u5bb9\u5668\u5916\u90e8\u5bf9\u8c61\u7684\u6307\u9488\u6216\u5f15\u7528\u3002 16.7 - std::initializer_list std::initializer_list \u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u6784\u9020\u51fd\u6570\u3001\u8d4b\u503c\u64cd\u4f5c\u7b26\u548c\u5176\u4ed6\u63a5\u53d7\u5217\u8868\u521d\u59cb\u5316\u53c2\u6570\u7684\u51fd\u6570\u3002 std::initailizer_list \u4f4d\u4e8e \u5934\u6587\u4ef6\u4e2d\u3002 \u5c5e\u6027\u7c7b\u578b \u7ec4\u5408 \u805a\u5408 \u5173\u8054 \u4f9d\u8d56 \u5173\u7cfb\u7c7b\u578b \u6574\u4f53/\u90e8\u5206 \u6574\u4f53/\u90e8\u5206 \u672c\u4e0d\u76f8\u5173 \u672c\u4e0d\u76f8\u5173 \u6210\u5458\u662f\u5426\u53ef\u4ee5\u5c5e\u4e8e\u591a\u4e2a\u7c7b No Yes Yes Yes \u6210\u5458\u5b58\u5728\u4e0e\u5426\u662f\u5426\u53d7\u7c7b\u7ba1\u7406 Yes No No No \u65b9\u5411 \u5355\u5411 \u5355\u5411 \u5355\u5411\u6216\u53cc\u5411 \u5355\u5411\u6216\u53cc\u5411 \u5173\u7cfb\u52a8\u8bcd Part-of Has-a Uses-a Depends-on","title":"\u590d\u4e60"},{"location":"17-1-introduction-to-inheritance/","tags":["inheritance"],"text":"Key Takeaway - \u5728\u4e0a\u4e00\u7ae0\u4e2d\u6211\u4eec\u8ba8\u8bba\u4e86 \u5bf9\u8c61\u7ec4\u5408 \u2014\u2014\u590d\u6742\u7684\u7c7b\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u7c7b\u548c\u7c7b\u578b\u7ec4\u6210\u3002\u5bf9\u8c61\u7ec4\u5408\u975e\u5e38\u9002\u5408\u6784\u5efa\u6574\u4f53\u548c\u90e8\u5206\u5177\u6709\u201c\u6709\u4e00\u4e2a\u201d\u8bed\u4e49\u5173\u7cfb\u7684\u5bf9\u8c61\u3002\u4e0d\u8fc7\uff0c\u5bf9\u8c61\u7ec4\u5408\u53ea\u662fC++\u63d0\u4f9b\u7684\u4e24\u79cd\u6784\u5efa\u590d\u6742\u7c7b\u7684\u65b9\u5f0f\u4e4b\u4e00\u3002\u53e6\u5916\u4e00\u79cd\u4e3b\u8981\u65b9\u5f0f\u662f \u7ee7\u627f \uff0c\u5b83\u8868\u793a\u4e24\u4e2a\u5bf9\u8c61\u4e4b\u95f4\u7684\u201c\u662f\u4e00\u4e2a\u201d\u5173\u7cfb\u3002 \u7ee7\u627f\u548c\u7ec4\u5408\u4e0d\u540c\uff0c\u7ec4\u5408\u8bed\u4e49\u6d89\u53ca\u5230\u4e00\u4e2a\u5bf9\u8c61\u5bf9\u53e6\u5916\u4e00\u4e9b\u5bf9\u8c61\u7684\u7ec4\u5408\u548c\u8fde\u63a5\uff0c\u7ee7\u627f\u5219\u662f\u65b0\u5bf9\u8c61\u901a\u8fc7\u76f4\u63a5\u83b7\u53d6\u5176\u4ed6\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u884c\u4e3a\u6765\u6269\u5c55\u6216\u7279\u6b8a\u5316(specializing)\u81ea\u5df1\u3002\u548c\u5bf9\u8c61\u7ec4\u5408\u7c7b\u4f3c\uff0c\u7ee7\u627f\u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\u4e5f\u968f\u5904\u53ef\u89c1\u2014\u2014\u4f60\u7ee7\u627f\u4e86\u7236\u6bcd\u7684\u57fa\u56e0\uff0c\u4ece\u4ed6\u4eec\u8eab\u4e0a\u83b7\u53d6\u4e86\u4e00\u4e9b\u7269\u7406\u7279\u5f81\uff0c\u4f46\u662f\u4f60\u540c\u65f6\u4e5f\u5177\u6709\u4e2a\u6027\u7684\u4e00\u9762\u3002\u79d1\u6280\u4ea7\u54c1\uff08\u8ba1\u7b97\u673a\u3001\u624b\u673a\u7b49\uff09\u4ece\u5b83\u4eec\u7684\u8001\u6b3e\u4e2d\u7ee7\u627f\u4e86\u4e00\u4e9b\u7279\u6027\uff08\u5f53\u9700\u8981\u5411\u540e\u517c\u5bb9\u7684\u65f6\u5019\uff09\u3002\u4f8b\u5982\uff0cIntel \u5954\u817e\u5904\u7406\u5668\u4ece 486 \u5904\u7406\u5668\u4e0a\u7ee7\u627f\u4e86\u5f88\u591a\u529f\u80fd\uff0c\u800c486\u4e5f\u4ece\u66f4\u65e9\u671f\u7684\u5904\u7406\u5668\u4e0a\u7ee7\u627f\u4e86\u4e00\u4e9b\u529f\u80fd\u3002C++ \u7ee7\u627f\u4e86\u5f88\u591a C \u8bed\u8a00\u7684\u7279\u5f81\uff0c\u800c C \u8bed\u8a00\u4e5f\u4ece\u66f4\u65e9\u671f\u7684\u7f16\u7a0b\u8bed\u8a00\u4e2d\u6709\u6240\u501f\u9274\u3002 \u4ee5\u82f9\u679c\u548c\u9999\u8549\u4e3a\u4f8b\u3002\u5c3d\u7ba1\u82f9\u679c\u548c\u9999\u8549\u662f\u4e0d\u540c\u7684\u6c34\u679c\uff0c\u4f46\u662f\u5b83\u4eec\u90fd\u662f \u6c34\u679c \u3002\u56e0\u4e3a\u5b83\u4eec\u90fd\u662f\u6c34\u679c\uff0c\u6240\u4ee5\u4ece\u903b\u8f91\u7684\u89d2\u5ea6\u6765\u8bb2\uff0c\u4efb\u4f55\u6709\u5173\u6c34\u679c\u7684\u4e3a\u771f\u7684\u8bba\u65ad\uff0c\u540c\u6837\u4e5f\u9002\u7528\u4e8e\u82f9\u679c\u548c\u9999\u8549\u3002\u4f8b\u5982\uff0c\u6c34\u679c\u6709\u540d\u5b57\u3001\u989c\u8272\u548c\u5927\u5c0f\u3002\u56e0\u6b64\uff0c\u82f9\u679c\u548c\u9999\u8549\u4e5f\u6709\u540d\u5b57\u3001\u989c\u8272\u548c\u5927\u5c0f\u3002\u6211\u4eec\u53ef\u4ee5\u8bf4\u82f9\u679c\u548c\u9999\u8549\u7ee7\u627f\u4e86\u6c34\u679c\u7684\u4e00\u4e9b\u5c5e\u6027\u3002\u6211\u4eec\u8fd8\u77e5\u9053\uff0c\u6c34\u679c\u9700\u8981\u7ecf\u5386\u4e00\u4e2a\u6210\u719f\u7684\u8fc7\u7a0b\uff0c\u6210\u719f\u4e4b\u540e\u7684\u6c34\u679c\u5c31\u53ef\u4ee5\u5403\u4e86\u3002\u56e0\u4e3a\u82f9\u679c\u548c\u9999\u8549\u662f\u6c34\u679c\uff0c\u6240\u4ee5\u5b83\u4eec\u4e5f\u7ee7\u627f\u4e86\u8fd9\u4e2a\u6210\u719f\u7684\u8fc7\u7a0b\u3002 \u5982\u679c\u901a\u8fc7\u56fe\u8868\u5f62\u5f0f\u6765\u5c55\u73b0\u82f9\u679c\u3001\u9999\u8549\u548c\u6c34\u679c\u7684\u5173\u7cfb\uff0c\u5e94\u8be5\u770b\u4e0a\u53bb\u662f\u8fd9\u4e2a\u6837\u5b50\u7684\uff1a \u4ece\u56fe\u4e2d\u53ef\u4ee5\u770b\u51fa\u4e00\u79cd\u5c42\u6b21\u7ed3\u6784\u3002 \u5c42\u6b21\u7ed3\u6784 \u5c42\u6b21\u7ed3\u6784\u662f\u4e00\u79cd\u5bf9\u8c61\u95f4\u7684\u5173\u7cfb\uff08\u57fa\u4e8e\u56fe\u8868\u8fdb\u884c\u5c55\u793a\uff09\u3002\u5927\u591a\u6570\u7684\u5c42\u6b21\u7ed3\u6784\u80fd\u591f\u4f53\u73b0\u968f\u7740\u65f6\u95f4\u53d8\u5316\u7684\u8fc7\u7a0b (386 -> 486 -> Pentium)\uff0c\u6216\u662f\u4ece\u5bbd\u6cdb\u6982\u5ff5\u9010\u6e10\u5206\u5316\u7684\u8fc7\u7a0b(\u6c34\u679c -> \u82f9\u679c -> \u86c7\u679c)\u3002\u5982\u679c\u4f60\u4e0a\u8fc7\u751f\u7269\u8bfe\u7684\u8bdd\uff0c\u90a3\u4e48\u5e94\u8be5\u4f1a\u4e86\u89e3\u8457\u540d\u7684\u201c \u57df \u3001\u754c\u3001 \u95e8 \u3001 \u7eb2 \u3001 \u76ee \u3001 \u79d1 \u3001 \u5c5e \u3001 \u79cd \u201c\uff0c\u8fd9\u79cd\u5c42\u6b21\u7ed3\u6784\uff08\u4ece\u4e00\u822c\u5230\u7279\u6b8a\uff09 \u8fd9\u662f\u53e6\u4e00\u4e2a\u5c42\u6b21\u7ed3\u6784\u7684\u4f8b\u5b50\uff1a\u6b63\u65b9\u5f62\u662f\u77e9\u5f62\uff0c\u77e9\u5f62\u662f\u56db\u8fb9\u5f62\uff0c\u56db\u8fb9\u5f62\u662f\u4e00\u79cd\u5f62\u72b6\u3002\u76f4\u89d2\u4e09\u89d2\u5f62\u662f\u4e09\u89d2\u5f62\uff0c\u4e09\u89d2\u5f62\u4e5f\u662f\u4e00\u79cd\u5f62\u72b6\u3002\u5c06\u5176\u653e\u5165\u5c42\u6b21\u56fe\u4e2d\uff0c\u770b\u8d77\u6765\u50cf\u8fd9\u6837: \u8fd9\u4e2a\u56fe\u4ece\u4e00\u822c\u7684(\u4e0a)\u5230\u7279\u6b8a\u7684(\u4e0b)\uff0c\u5c42\u6b21\u7ed3\u6784\u4e2d\u7684\u6bcf\u4e2a\u9879\u76ee\u7ee7\u627f\u4e0a\u9762\u9879\u76ee\u7684\u5c5e\u6027\u548c\u884c\u4e3a\u3002 \u5148\u7779\u4e3a\u5feb \u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba\u4f7f\u7528 C++ \u4e2d\u7ee7\u627f\u7684\u57fa\u672c\u65b9\u5f0f\u3002 \u5728\u4e0b\u4e00\u7ae0\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba\u7ee7\u627f\u5982\u4f55\u57fa\u4e8e \u865a\u51fd\u6570 \u6765 \u591a\u6001 \uff08\u9762\u5411\u5bf9\u8c61\u4e2d\u6700\u5e38\u89c1\u7684\u6982\u5ff5\u4e4b\u4e00\uff09\u3002 \u968f\u540e\uff0c\u6211\u4eec\u8fd8\u4f1a\u8ba8\u8bba\u4f7f\u7528\u7ee7\u627f\u800c\u5e26\u6765\u7684\u4e00\u4e9b\u4f18\u52bf\u548c\u52a3\u52bf\u3002","title":"17.1 - \u7ee7\u627f\u7b80\u4ecb"},{"location":"17-1-introduction-to-inheritance/#_1","text":"\u5c42\u6b21\u7ed3\u6784\u662f\u4e00\u79cd\u5bf9\u8c61\u95f4\u7684\u5173\u7cfb\uff08\u57fa\u4e8e\u56fe\u8868\u8fdb\u884c\u5c55\u793a\uff09\u3002\u5927\u591a\u6570\u7684\u5c42\u6b21\u7ed3\u6784\u80fd\u591f\u4f53\u73b0\u968f\u7740\u65f6\u95f4\u53d8\u5316\u7684\u8fc7\u7a0b (386 -> 486 -> Pentium)\uff0c\u6216\u662f\u4ece\u5bbd\u6cdb\u6982\u5ff5\u9010\u6e10\u5206\u5316\u7684\u8fc7\u7a0b(\u6c34\u679c -> \u82f9\u679c -> \u86c7\u679c)\u3002\u5982\u679c\u4f60\u4e0a\u8fc7\u751f\u7269\u8bfe\u7684\u8bdd\uff0c\u90a3\u4e48\u5e94\u8be5\u4f1a\u4e86\u89e3\u8457\u540d\u7684\u201c \u57df \u3001\u754c\u3001 \u95e8 \u3001 \u7eb2 \u3001 \u76ee \u3001 \u79d1 \u3001 \u5c5e \u3001 \u79cd \u201c\uff0c\u8fd9\u79cd\u5c42\u6b21\u7ed3\u6784\uff08\u4ece\u4e00\u822c\u5230\u7279\u6b8a\uff09 \u8fd9\u662f\u53e6\u4e00\u4e2a\u5c42\u6b21\u7ed3\u6784\u7684\u4f8b\u5b50\uff1a\u6b63\u65b9\u5f62\u662f\u77e9\u5f62\uff0c\u77e9\u5f62\u662f\u56db\u8fb9\u5f62\uff0c\u56db\u8fb9\u5f62\u662f\u4e00\u79cd\u5f62\u72b6\u3002\u76f4\u89d2\u4e09\u89d2\u5f62\u662f\u4e09\u89d2\u5f62\uff0c\u4e09\u89d2\u5f62\u4e5f\u662f\u4e00\u79cd\u5f62\u72b6\u3002\u5c06\u5176\u653e\u5165\u5c42\u6b21\u56fe\u4e2d\uff0c\u770b\u8d77\u6765\u50cf\u8fd9\u6837: \u8fd9\u4e2a\u56fe\u4ece\u4e00\u822c\u7684(\u4e0a)\u5230\u7279\u6b8a\u7684(\u4e0b)\uff0c\u5c42\u6b21\u7ed3\u6784\u4e2d\u7684\u6bcf\u4e2a\u9879\u76ee\u7ee7\u627f\u4e0a\u9762\u9879\u76ee\u7684\u5c5e\u6027\u548c\u884c\u4e3a\u3002","title":"\u5c42\u6b21\u7ed3\u6784"},{"location":"17-1-introduction-to-inheritance/#_2","text":"\u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba\u4f7f\u7528 C++ \u4e2d\u7ee7\u627f\u7684\u57fa\u672c\u65b9\u5f0f\u3002 \u5728\u4e0b\u4e00\u7ae0\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba\u7ee7\u627f\u5982\u4f55\u57fa\u4e8e \u865a\u51fd\u6570 \u6765 \u591a\u6001 \uff08\u9762\u5411\u5bf9\u8c61\u4e2d\u6700\u5e38\u89c1\u7684\u6982\u5ff5\u4e4b\u4e00\uff09\u3002 \u968f\u540e\uff0c\u6211\u4eec\u8fd8\u4f1a\u8ba8\u8bba\u4f7f\u7528\u7ee7\u627f\u800c\u5e26\u6765\u7684\u4e00\u4e9b\u4f18\u52bf\u548c\u52a3\u52bf\u3002","title":"\u5148\u7779\u4e3a\u5feb"},{"location":"17-2-basic-inheritance-in-c%2B%2B/","tags":["inheritance"],"text":"Key Takeaway - \u4ece\u62bd\u8c61\u5c42\u9762\u8ba8\u8bba\u8fc7\u7ee7\u627f\u4e4b\u540e\uff0c\u8ba9\u6211\u4eec\u6765\u770b\u770b C++ \u4e2d\u7684\u7ee7\u627f\u5427\u3002 C++\u7684\u7ee7\u627f\u53d1\u751f\u5728\u7c7b\u4e0e\u7c7b\u4e4b\u95f4\u3002\u5728\u7ee7\u627f\u5173\u7cfb\u4e2d\uff0c\u88ab\u7ee7\u627f\u7684\u7c7b\u79f0\u4e3a \u7236\u7c7b(parent class) \u3001 \u57fa\u7c7b(base class) \u6216 \u8d85\u7c7b(super class) \u3002\u800c\u7ee7\u627f\u7236\u7c7b\u7684\u7c7b\uff0c\u5219\u79f0\u4e3a \u5b50\u7c7b(child class / sub class) \u3001 \u6d3e\u751f\u7c7b \u3002 \u5728\u4e0a\u56fe\u4e2d\uff0c\u6c34\u679c\u662f\u7236\u7c7b\uff0c\u82f9\u679c\u548c\u9999\u8549\u90fd\u662f\u5b50\u7c7b\u3002 \u5728\u4e0a\u56fe\u4e2d\uff0c\u4e09\u89d2\u5f62\u65e2\u662f\u5b50\u7c7b\uff08 \u5f62\u72b6 \u7684\u5b50\u7c7b\uff09\uff0c\u4e5f\u662f\u7236\u7c7b\uff08\u5bf9\u76f4\u89d2\u4e09\u89d2\u5f62\u800c\u8a00\uff09\u3002 \u5b50\u7c7b\u4ece\u7236\u7c7b\u7ee7\u627f\u884c\u4e3a(\u6210\u5458\u51fd\u6570)\u548c\u5c5e\u6027(\u6210\u5458\u53d8\u91cf)(\u9700\u8981\u9075\u5b88\u8bbf\u95ee\u9650\u5236\uff0c\u6211\u4eec\u5c06\u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\u8ba8\u8bba)\u3002 \u8fd9\u4e9b\u53d8\u91cf\u548c\u51fd\u6570\u6210\u4e3a\u6d3e\u751f\u7c7b\u7684\u6210\u5458\u3002 \u56e0\u4e3a\u5b50\u7c7b\u4e5f\u662f\u4e00\u4e2a\u6807\u51c6\u7684\u7c7b\uff0c\u6240\u4ee5\u5b83\u4eec(\u5f53\u7136)\u53ef\u4ee5\u6709\u81ea\u5df1\u7684\u7c7b\u6210\u5458\u3002 Person \u7c7b \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528 Person \u8868\u793a\u4e00\u4e2a\u201d\u4eba\u7c7b\u201c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include class Person { // In this example, we're making our members public for simplicity public : std :: string m_name {}; int m_age {}; Person ( const std :: string & name = \"\" , int age = 0 ) : m_name { name }, m_age { age } { } const std :: string & getName () const { return m_name ; } int getAge () const { return m_age ; } }; \u56e0\u4e3a\u8fd9\u4e2a Person \u7c7b\u88ab\u8bbe\u8ba1\u7528\u6765\u8868\u793a\u4e00\u822c\u7684\u4eba\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u5b9a\u4e49\u4e86\u4efb\u4f55\u7c7b\u578b\u7684\u4eba\u90fd\u901a\u7528\u7684\u6210\u5458\u3002\u6bcf\u4e2a\u4eba(\u4e0d\u8bba\u6027\u522b\u3001\u804c\u4e1a\u7b49)\u90fd\u6709\u540d\u5b57\u548c\u5e74\u9f84\uff0c\u6240\u4ee5\u5728\u8fd9\u91cc\u8868\u793a\u51fa\u6765\u3002 \u6ce8\u610f\uff0c\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u6240\u6709\u53d8\u91cf\u548c\u51fd\u6570\u8bbe\u7f6e\u4e3a\u516c\u5171\u7684\u3002\u8fd9\u7eaf\u7cb9\u662f\u4e3a\u4e86\u4f7f\u8fd9\u4e9b\u793a\u4f8b\u4fdd\u6301\u7b80\u5355\u3002\u901a\u5e38\u6211\u4eec\u4f1a\u5c06\u53d8\u91cf\u8bbe\u4e3a\u79c1\u6709\u3002\u6211\u4eec\u5c06\u5728\u672c\u7ae0\u540e\u9762\u8ba8\u8bba\u8bbf\u95ee\u63a7\u5236\u4ee5\u53ca\u5b83\u4eec\u5982\u4f55\u4e0e\u7ee7\u627f\u4ea4\u4e92\u3002 BaseballPlayer \u7c7b \u5047\u8bbe\u6211\u4eec\u60f3\u8981\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\u6765\u8bb0\u5f55\u68d2\u7403\u8fd0\u52a8\u5458\u7684\u4fe1\u606f\u3002\u68d2\u7403\u8fd0\u52a8\u5458\u9700\u8981\u5305\u542b\u7279\u5b9a\u7684\u5173\u4e8e\u68d2\u7403\u8fd0\u52a8\u5458\u7684\u4fe1\u606f\u2014\u2014\u4f8b\u5982\uff0c\u7403\u5458\u7684\u51fb\u7403\u7387\u548c\u672c\u5792\u6253\u6570\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4e0d\u5b8c\u6574\u7684\u7248\u672c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class BaseballPlayer { // In this example, we're making our members public for simplicity public : double m_battingAverage {}; int m_homeRuns {}; BaseballPlayer ( double battingAverage = 0.0 , int homeRuns = 0 ) : m_battingAverage { battingAverage }, m_homeRuns { homeRuns } { } }; \u6211\u4eec\u540c\u65f6\u8fd8\u9700\u8981\u8bb0\u5f55\u8fd0\u52a8\u5458\u7684\u59d3\u540d\u548c\u5e74\u9f84\uff0c\u800c Person \u7c7b\u4e2d\u6b63\u597d\u5305\u542b\u8fd9\u4e9b\u4fe1\u606f\u3002 \u6211\u4eec\u6709\u4e09\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4e3a BaseballPlayer \u7c7b\u6dfb\u52a0\u59d3\u540d\u548c\u5e74\u9f84\uff1a \u76f4\u63a5\u5c06\u59d3\u540d\u548c\u5e74\u9f84\u4f5c\u4e3a\u6210\u5458\u6dfb\u52a0\u5230 BaseballPlayer \u7c7b\u4e2d\u3002\u8fd9\u53ef\u80fd\u662f\u6700\u7cdf\u7cd5\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u6211\u4eec\u590d\u5236\u7684\u662f Person \u7c7b\u4e2d\u5df2\u7ecf\u5b58\u5728\u7684\u4ee3\u7801\u3002\u5bf9 Person \u7684\u4efb\u4f55\u66f4\u65b0\u90fd\u5fc5\u987b\u5728 BaseballPlayer \u4e2d\u8fdb\u884c\u3002 \u4f7f\u7528\u7ec4\u5408\u5c06 Person \u6dfb\u52a0\u4e3a BaseballPlayer \u7684\u6210\u5458\u3002\u4f46\u6211\u4eec\u5fc5\u987b\u95ee\u81ea\u5df1\uff0c\u201c\u4e00\u4e2a\u68d2\u7403\u8fd0\u52a8\u5458\u6709\u4e00\u4e2a\u4eba\u5417\uff1f\u201d \u5e76\u4e0d\u662f\u3002\u6240\u4ee5\u8fd9\u4e0d\u662f\u6b63\u786e\u7684\u8303\u5f0f\u3002 \u8ba9 BaseballPlayer \u4ece Person \u7ee7\u627f\u8fd9\u4e9b\u5c5e\u6027\u3002\u8bb0\u4f4f\uff0c\u7ee7\u627f\u8868\u793a is-a \u5173\u7cfb\u3002\u68d2\u7403\u8fd0\u52a8\u5458\u662f\u4eba\u5417\uff1f\u5f53\u7136\u662f\u3002\u6240\u4ee5\u7ee7\u627f\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002 \u8ba9 BaseballPlayer \u6210\u4e3a\u6d3e\u751f\u7c7b \u8ba9 BaseballPlayer \u7ee7\u627f Person \u7c7b\u7684\u8bed\u6cd5\u5f88\u7b80\u5355\u3002\u5728\u5b8c\u6210 class BaseballPlayer \u58f0\u660e\u540e\uff0c\u4f7f\u7528\u4e00\u4e2a\u5192\u53f7\uff0c\u5916\u52a0 public \u5173\u952e\u5b57\uff0c\u63a5\u4e0a\u9700\u8981\u7ee7\u627f\u7684\u7c7b\u7684\u540d\u5b57\u5373\u53ef\u3002\u8fd9\u79cd\u65b9\u5f0f\u79f0\u4e3a \u516c\u5f00\u7ee7\u627f \u3002\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u518d\u8be6\u7ec6\u8ba8\u8bba\u516c\u5f00\u7ee7\u627f\u7684\u8bdd\u9898\u3002 1 2 3 4 5 6 7 8 9 10 11 12 // BaseballPlayer publicly inheriting Person class BaseballPlayer : public Person { public : double m_battingAverage {}; int m_homeRuns {}; BaseballPlayer ( double battingAverage = 0.0 , int homeRuns = 0 ) : m_battingAverage { battingAverage }, m_homeRuns { homeRuns } { } }; \u4f7f\u7528\u6d3e\u751f\u5173\u7cfb\u56fe\uff0c\u6211\u4eec\u7684\u7ee7\u627f\u770b\u8d77\u6765\u50cf\u8fd9\u6837\uff1a \u5f53 BaseballPlayer \u7ee7\u627f Person \u540e\uff0c BaseballPlayer \u5c31\u83b7\u53d6\u4e86 Person \u7684 \u6210\u5458\u53d8\u91cf \u548c \u6210\u5458\u51fd\u6570 \u3002\u6b64\u5916\uff0c BaseballPlayer \u8fd8\u5b9a\u4e49\u4e86\u4e24\u4e2a\u81ea\u5df1\u7279\u6709\u7684\u6210\u5458\uff1a m_battingAverage \u548c m_homeRuns \u3002\u8fd9\u4e48\u505a\u7684\u539f\u56e0\u5f88\u7b80\u5355\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u5c5e\u6027\u662f\u5c5e\u4e8e BaseballPlayer \u7684\uff0c\u800c\u4e0d\u662f\u4efb\u4f55 Person \u6240\u5171\u6709\u7684\u3002 \u56e0\u6b64\uff0c BaseballPlayer \u5bf9\u8c61\u6700\u7ec8\u4f1a\u5305\u542b4\u4e2a\u6210\u5458\u53d8\u91cf\uff1a m_battingAverage \u548c m_homeRuns \u662f BaseballPlayer \u7279\u6709\u7684\uff0c m_name \u548c m_age \u662f\u7ee7\u627f\u81ea Person \u7684\u3002 \u4e0a\u9762\u8bba\u8ff0\u5f88\u5bb9\u6613\u8bc1\u660e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include #include class Person { public : std :: string m_name {}; int m_age {}; Person ( const std :: string & name = \"\" , int age = 0 ) : m_name { name }, m_age { age } { } const std :: string & getName () const { return m_name ; } int getAge () const { return m_age ; } }; // BaseballPlayer publicly inheriting Person class BaseballPlayer : public Person { public : double m_battingAverage {}; int m_homeRuns {}; BaseballPlayer ( double battingAverage = 0.0 , int homeRuns = 0 ) : m_battingAverage { battingAverage }, m_homeRuns { homeRuns } { } }; int main () { // Create a new BaseballPlayer object BaseballPlayer joe {}; // Assign it a name (we can do this directly because m_name is public) joe . m_name = \"Joe\" ; // Print out the name std :: cout << joe . getName () << '\\n' ; // use the getName() function we've acquired from the Person base class return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 Joe \u4ee3\u7801\u53ef\u4ee5\u6b63\u786e\u5730\u7f16\u8bd1\u8fd0\u884c\uff0c\u56e0\u4e3a joe \u662f BaseballPlayer \uff0c\u800c\u6240\u6709\u7684 BaseballPlayer \u7c7b\u578b\u7684\u5bf9\u8c61\u90fd\u5177\u6709 m_name \u6210\u5458\u53d8\u91cf\u548c getName() \u6210\u5458\u51fd\u6570\uff0c\u5b83\u4eec\u662f\u4ece Person \u7c7b\u7ee7\u627f\u8fc7\u6765\u7684\u3002 Employee \u6d3e\u751f\u7c7b \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u518d\u7f16\u5199\u53e6\u5916\u4e00\u4e2a\u7c7b\u5e76\u7ee7\u627f Person \u3002\u8fd9\u6b21\uff0c\u6211\u4eec\u7f16\u5199\u4e00\u4e2a Employee \u7c7b\u3002\u96c7\u5458\u663e\u7136\u201d\u662f\u4e00\u4e2a\u201c\u4eba\uff0c\u6240\u4ee5\u4f7f\u5176\u7ee7\u627f Person \u7c7b\u4f3c\u662f\u5408\u9002\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // Employee publicly inherits from Person class Employee : public Person { public : double m_hourlySalary {}; long m_employeeID {}; Employee ( double hourlySalary = 0.0 , long employeeID = 0 ) : m_hourlySalary { hourlySalary }, m_employeeID { employeeID } { } void printNameAndSalary () const { std :: cout << m_name << \": \" << m_hourlySalary << '\\n' ; } }; Employee \u4ece Person \u7ee7\u627f\u4e86 m_name \u548c m_age (\u4ee5\u53ca\u4e24\u4e2a \u6210\u5458\u8bbf\u95ee\u51fd\u6570 )\uff0c\u540c\u65f6\u5b83\u8fd8\u6dfb\u52a0\u4e86\u4e24\u4e2a\u989d\u5916\u7684\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u51fd\u6570\u3002\u6ce8\u610f\uff0c printNameAndSalary() \u540c\u65f6\u4f7f\u7528\u4e86\u6765\u81ea\u4e24\u4e2a\u7c7b\u7684\u53d8\u91cf( Employee::m_hourlySalary \u548c Person::m_name )\u3002 \u6d3e\u751f\u7ed3\u6784\u56fe\u5982\u4e0b\uff1a \u5982\u56fe\u6240\u793a\uff0c Employee \u548c BaseballPlayer \u5e76\u6ca1\u6709\u76f4\u63a5\u5173\u7cfb\uff0c\u5373\u4f7f\u5b83\u4eec\u90fd\u6d3e\u751f\u81ea Person \u3002 \u57fa\u4e8e Employee \u7c7b\u7f16\u5199\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #include #include class Person { public : std :: string m_name {}; int m_age {}; const std :: string & getName () const { return m_name ; } int getAge () const { return m_age ; } Person ( const std :: string & name = \"\" , int age = 0 ) : m_name { name }, m_age { age } { } }; // Employee \u516c\u5f00\u7ee7\u627f\u4e86 Person class Employee : public Person { public : double m_hourlySalary {}; long m_employeeID {}; Employee ( double hourlySalary = 0.0 , long employeeID = 0 ) : m_hourlySalary { hourlySalary }, m_employeeID { employeeID } { } void printNameAndSalary () const { std :: cout << m_name << \": \" << m_hourlySalary << '\\n' ; } }; int main () { Employee frank { 20.25 , 12345 }; frank . m_name = \"Frank\" ; // \u53ef\u4ee5\u8fd9\u4e48\u505a\u662f\u56e0\u4e3a m_name \u662f public frank . printNameAndSalary (); return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 Frank: 20.25 \u7ee7\u627f\u94fe \u6d3e\u751f\u7c7b\u672c\u8eab\u4e5f\u662f\u53ef\u4ee5\u88ab\u7ee7\u627f\u7684\u3002\u800c\u4e14\u5728\u64cd\u4f5c\u548c\u4f7f\u7528\u4e0a\u90fd\u6ca1\u6709\u4ec0\u4e48\u7279\u522b\u4e4b\u5904\u3002 \u4f8b\u5982 Supervisor \u7c7b\u672c\u8eab\u662f Employee \u7c7b\u7684\u6d3e\u751f\u7c7b\uff0c Employee \u7c7b\u5219\u662f Person \u7c7b\u7684\u5b50\u7c7b\uff1a 1 2 3 4 5 6 class Supervisor : public Employee { public : // This Supervisor can oversee a max of 5 employees long m_overseesIDs [ 5 ]{}; }; \u6d3e\u751f\u7ed3\u6784\u56fe\u5982\u4e0b\uff1a \u6240\u6709\u7684 Supervisor \u5bf9\u8c61\u90fd\u7ee7\u627f Employee \u548c Person \u7684\u51fd\u6570\u548c\u53d8\u91cf\uff0c\u5e76\u6dfb\u52a0\u81ea\u5df1\u7684 m_overseesIDs \u6210\u5458\u53d8\u91cf\u3002 \u901a\u8fc7\u6784\u5efa\u8fd9\u6837\u7684\u7ee7\u627f\u94fe\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u7ec4\u53ef\u91cd\u7528\u7684\u7c7b\uff0c\u8fd9\u4e9b\u7c7b\u975e\u5e38\u901a\u7528(\u4f4d\u4e8e\u6d3e\u751f\u5c42\u6b21\u7ed3\u6784\u7684\u9876\u90e8)\uff0c\u5e76\u5728\u6bcf\u4e2a\u7ee7\u627f\u7ea7\u522b\u4e0a\u9010\u6e10\u53d8\u5f97\u66f4\u52a0\u5177\u4f53\u3002 \u4e3a\u4ec0\u4e48\u6b64\u7c7b\u7ee7\u627f\u662f\u6709\u7528\u7684\uff1f \u4ece\u57fa\u7c7b\u7ee7\u627f\u610f\u5473\u7740\u4e0d\u5fc5\u5728\u6d3e\u751f\u7c7b\u4e2d\u91cd\u65b0\u5b9a\u4e49\u57fa\u7c7b\u7684\u4fe1\u606f\u3002\u6211\u4eec\u901a\u8fc7\u7ee7\u627f\u81ea\u52a8\u63a5\u6536\u57fa\u7c7b\u7684\u6210\u5458\u51fd\u6570\u548c\u6210\u5458\u53d8\u91cf\uff0c\u7136\u540e\u7b80\u5355\u5730\u6dfb\u52a0\u6240\u9700\u7684\u9644\u52a0\u51fd\u6570\u6216\u6210\u5458\u53d8\u91cf\u3002\u8fd9\u4e0d\u4ec5\u8282\u7701\u4e86\u5de5\u4f5c\uff0c\u800c\u4e14\u8fd8\u610f\u5473\u7740\u5982\u679c\u6211\u4eec\u66f4\u65b0\u6216\u4fee\u6539\u57fa\u7c7b(\u4f8b\u5982\u6dfb\u52a0\u65b0\u51fd\u6570\uff0c\u6216\u4fee\u590dbug)\uff0c\u6211\u4eec\u6240\u6709\u7684\u6d3e\u751f\u7c7b\u5c06\u81ea\u52a8\u7ee7\u627f\u66f4\u6539! \u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u5411 Person \u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u529f\u80fd\uff0c Employee \u548c Supervisor \u90fd\u4f1a\u81ea\u52a8\u83b7\u5f97\u5bf9\u5b83\u7684\u8bbf\u95ee\u6743\u9650\u3002\u5982\u679c\u6211\u4eec\u5411 Employee \u6dfb\u52a0\u4e00\u4e2a\u65b0\u53d8\u91cf\uff0c Supervisor \u4e5f\u5c06\u83b7\u5f97\u5bf9\u5b83\u7684\u8bbf\u95ee\u6743\u3002\u8fd9\u5141\u8bb8\u6211\u4eec\u4ee5\u4e00\u79cd\u7b80\u5355\u3001\u76f4\u89c2\u548c\u4f4e\u7ef4\u62a4\u7684\u65b9\u5f0f\u6784\u9020\u65b0\u7c7b! \u7ed3\u8bba \u901a\u8fc7\u7ee7\u627f\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u5176\u4ed6\u7c7b\u4e2d\u83b7\u53d6\u5176\u6210\u5458\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u91cd\u7528\u3002\u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u63a2\u7d22\u7ee7\u627f\u7684\u5de5\u4f5c\u539f\u7406\u3002","title":"17.2 - C++\u7ee7\u627f\u57fa\u7840"},{"location":"17-2-basic-inheritance-in-c%2B%2B/#person","text":"\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528 Person \u8868\u793a\u4e00\u4e2a\u201d\u4eba\u7c7b\u201c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include class Person { // In this example, we're making our members public for simplicity public : std :: string m_name {}; int m_age {}; Person ( const std :: string & name = \"\" , int age = 0 ) : m_name { name }, m_age { age } { } const std :: string & getName () const { return m_name ; } int getAge () const { return m_age ; } }; \u56e0\u4e3a\u8fd9\u4e2a Person \u7c7b\u88ab\u8bbe\u8ba1\u7528\u6765\u8868\u793a\u4e00\u822c\u7684\u4eba\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u5b9a\u4e49\u4e86\u4efb\u4f55\u7c7b\u578b\u7684\u4eba\u90fd\u901a\u7528\u7684\u6210\u5458\u3002\u6bcf\u4e2a\u4eba(\u4e0d\u8bba\u6027\u522b\u3001\u804c\u4e1a\u7b49)\u90fd\u6709\u540d\u5b57\u548c\u5e74\u9f84\uff0c\u6240\u4ee5\u5728\u8fd9\u91cc\u8868\u793a\u51fa\u6765\u3002 \u6ce8\u610f\uff0c\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u6240\u6709\u53d8\u91cf\u548c\u51fd\u6570\u8bbe\u7f6e\u4e3a\u516c\u5171\u7684\u3002\u8fd9\u7eaf\u7cb9\u662f\u4e3a\u4e86\u4f7f\u8fd9\u4e9b\u793a\u4f8b\u4fdd\u6301\u7b80\u5355\u3002\u901a\u5e38\u6211\u4eec\u4f1a\u5c06\u53d8\u91cf\u8bbe\u4e3a\u79c1\u6709\u3002\u6211\u4eec\u5c06\u5728\u672c\u7ae0\u540e\u9762\u8ba8\u8bba\u8bbf\u95ee\u63a7\u5236\u4ee5\u53ca\u5b83\u4eec\u5982\u4f55\u4e0e\u7ee7\u627f\u4ea4\u4e92\u3002","title":"Person \u7c7b"},{"location":"17-2-basic-inheritance-in-c%2B%2B/#baseballplayer","text":"\u5047\u8bbe\u6211\u4eec\u60f3\u8981\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\u6765\u8bb0\u5f55\u68d2\u7403\u8fd0\u52a8\u5458\u7684\u4fe1\u606f\u3002\u68d2\u7403\u8fd0\u52a8\u5458\u9700\u8981\u5305\u542b\u7279\u5b9a\u7684\u5173\u4e8e\u68d2\u7403\u8fd0\u52a8\u5458\u7684\u4fe1\u606f\u2014\u2014\u4f8b\u5982\uff0c\u7403\u5458\u7684\u51fb\u7403\u7387\u548c\u672c\u5792\u6253\u6570\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4e0d\u5b8c\u6574\u7684\u7248\u672c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class BaseballPlayer { // In this example, we're making our members public for simplicity public : double m_battingAverage {}; int m_homeRuns {}; BaseballPlayer ( double battingAverage = 0.0 , int homeRuns = 0 ) : m_battingAverage { battingAverage }, m_homeRuns { homeRuns } { } }; \u6211\u4eec\u540c\u65f6\u8fd8\u9700\u8981\u8bb0\u5f55\u8fd0\u52a8\u5458\u7684\u59d3\u540d\u548c\u5e74\u9f84\uff0c\u800c Person \u7c7b\u4e2d\u6b63\u597d\u5305\u542b\u8fd9\u4e9b\u4fe1\u606f\u3002 \u6211\u4eec\u6709\u4e09\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4e3a BaseballPlayer \u7c7b\u6dfb\u52a0\u59d3\u540d\u548c\u5e74\u9f84\uff1a \u76f4\u63a5\u5c06\u59d3\u540d\u548c\u5e74\u9f84\u4f5c\u4e3a\u6210\u5458\u6dfb\u52a0\u5230 BaseballPlayer \u7c7b\u4e2d\u3002\u8fd9\u53ef\u80fd\u662f\u6700\u7cdf\u7cd5\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u6211\u4eec\u590d\u5236\u7684\u662f Person \u7c7b\u4e2d\u5df2\u7ecf\u5b58\u5728\u7684\u4ee3\u7801\u3002\u5bf9 Person \u7684\u4efb\u4f55\u66f4\u65b0\u90fd\u5fc5\u987b\u5728 BaseballPlayer \u4e2d\u8fdb\u884c\u3002 \u4f7f\u7528\u7ec4\u5408\u5c06 Person \u6dfb\u52a0\u4e3a BaseballPlayer \u7684\u6210\u5458\u3002\u4f46\u6211\u4eec\u5fc5\u987b\u95ee\u81ea\u5df1\uff0c\u201c\u4e00\u4e2a\u68d2\u7403\u8fd0\u52a8\u5458\u6709\u4e00\u4e2a\u4eba\u5417\uff1f\u201d \u5e76\u4e0d\u662f\u3002\u6240\u4ee5\u8fd9\u4e0d\u662f\u6b63\u786e\u7684\u8303\u5f0f\u3002 \u8ba9 BaseballPlayer \u4ece Person \u7ee7\u627f\u8fd9\u4e9b\u5c5e\u6027\u3002\u8bb0\u4f4f\uff0c\u7ee7\u627f\u8868\u793a is-a \u5173\u7cfb\u3002\u68d2\u7403\u8fd0\u52a8\u5458\u662f\u4eba\u5417\uff1f\u5f53\u7136\u662f\u3002\u6240\u4ee5\u7ee7\u627f\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002","title":"BaseballPlayer \u7c7b"},{"location":"17-2-basic-inheritance-in-c%2B%2B/#baseballplayer_1","text":"\u8ba9 BaseballPlayer \u7ee7\u627f Person \u7c7b\u7684\u8bed\u6cd5\u5f88\u7b80\u5355\u3002\u5728\u5b8c\u6210 class BaseballPlayer \u58f0\u660e\u540e\uff0c\u4f7f\u7528\u4e00\u4e2a\u5192\u53f7\uff0c\u5916\u52a0 public \u5173\u952e\u5b57\uff0c\u63a5\u4e0a\u9700\u8981\u7ee7\u627f\u7684\u7c7b\u7684\u540d\u5b57\u5373\u53ef\u3002\u8fd9\u79cd\u65b9\u5f0f\u79f0\u4e3a \u516c\u5f00\u7ee7\u627f \u3002\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u518d\u8be6\u7ec6\u8ba8\u8bba\u516c\u5f00\u7ee7\u627f\u7684\u8bdd\u9898\u3002 1 2 3 4 5 6 7 8 9 10 11 12 // BaseballPlayer publicly inheriting Person class BaseballPlayer : public Person { public : double m_battingAverage {}; int m_homeRuns {}; BaseballPlayer ( double battingAverage = 0.0 , int homeRuns = 0 ) : m_battingAverage { battingAverage }, m_homeRuns { homeRuns } { } }; \u4f7f\u7528\u6d3e\u751f\u5173\u7cfb\u56fe\uff0c\u6211\u4eec\u7684\u7ee7\u627f\u770b\u8d77\u6765\u50cf\u8fd9\u6837\uff1a \u5f53 BaseballPlayer \u7ee7\u627f Person \u540e\uff0c BaseballPlayer \u5c31\u83b7\u53d6\u4e86 Person \u7684 \u6210\u5458\u53d8\u91cf \u548c \u6210\u5458\u51fd\u6570 \u3002\u6b64\u5916\uff0c BaseballPlayer \u8fd8\u5b9a\u4e49\u4e86\u4e24\u4e2a\u81ea\u5df1\u7279\u6709\u7684\u6210\u5458\uff1a m_battingAverage \u548c m_homeRuns \u3002\u8fd9\u4e48\u505a\u7684\u539f\u56e0\u5f88\u7b80\u5355\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u5c5e\u6027\u662f\u5c5e\u4e8e BaseballPlayer \u7684\uff0c\u800c\u4e0d\u662f\u4efb\u4f55 Person \u6240\u5171\u6709\u7684\u3002 \u56e0\u6b64\uff0c BaseballPlayer \u5bf9\u8c61\u6700\u7ec8\u4f1a\u5305\u542b4\u4e2a\u6210\u5458\u53d8\u91cf\uff1a m_battingAverage \u548c m_homeRuns \u662f BaseballPlayer \u7279\u6709\u7684\uff0c m_name \u548c m_age \u662f\u7ee7\u627f\u81ea Person \u7684\u3002 \u4e0a\u9762\u8bba\u8ff0\u5f88\u5bb9\u6613\u8bc1\u660e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include #include class Person { public : std :: string m_name {}; int m_age {}; Person ( const std :: string & name = \"\" , int age = 0 ) : m_name { name }, m_age { age } { } const std :: string & getName () const { return m_name ; } int getAge () const { return m_age ; } }; // BaseballPlayer publicly inheriting Person class BaseballPlayer : public Person { public : double m_battingAverage {}; int m_homeRuns {}; BaseballPlayer ( double battingAverage = 0.0 , int homeRuns = 0 ) : m_battingAverage { battingAverage }, m_homeRuns { homeRuns } { } }; int main () { // Create a new BaseballPlayer object BaseballPlayer joe {}; // Assign it a name (we can do this directly because m_name is public) joe . m_name = \"Joe\" ; // Print out the name std :: cout << joe . getName () << '\\n' ; // use the getName() function we've acquired from the Person base class return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 Joe \u4ee3\u7801\u53ef\u4ee5\u6b63\u786e\u5730\u7f16\u8bd1\u8fd0\u884c\uff0c\u56e0\u4e3a joe \u662f BaseballPlayer \uff0c\u800c\u6240\u6709\u7684 BaseballPlayer \u7c7b\u578b\u7684\u5bf9\u8c61\u90fd\u5177\u6709 m_name \u6210\u5458\u53d8\u91cf\u548c getName() \u6210\u5458\u51fd\u6570\uff0c\u5b83\u4eec\u662f\u4ece Person \u7c7b\u7ee7\u627f\u8fc7\u6765\u7684\u3002","title":"\u8ba9 BaseballPlayer \u6210\u4e3a\u6d3e\u751f\u7c7b"},{"location":"17-2-basic-inheritance-in-c%2B%2B/#employee","text":"\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u518d\u7f16\u5199\u53e6\u5916\u4e00\u4e2a\u7c7b\u5e76\u7ee7\u627f Person \u3002\u8fd9\u6b21\uff0c\u6211\u4eec\u7f16\u5199\u4e00\u4e2a Employee \u7c7b\u3002\u96c7\u5458\u663e\u7136\u201d\u662f\u4e00\u4e2a\u201c\u4eba\uff0c\u6240\u4ee5\u4f7f\u5176\u7ee7\u627f Person \u7c7b\u4f3c\u662f\u5408\u9002\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // Employee publicly inherits from Person class Employee : public Person { public : double m_hourlySalary {}; long m_employeeID {}; Employee ( double hourlySalary = 0.0 , long employeeID = 0 ) : m_hourlySalary { hourlySalary }, m_employeeID { employeeID } { } void printNameAndSalary () const { std :: cout << m_name << \": \" << m_hourlySalary << '\\n' ; } }; Employee \u4ece Person \u7ee7\u627f\u4e86 m_name \u548c m_age (\u4ee5\u53ca\u4e24\u4e2a \u6210\u5458\u8bbf\u95ee\u51fd\u6570 )\uff0c\u540c\u65f6\u5b83\u8fd8\u6dfb\u52a0\u4e86\u4e24\u4e2a\u989d\u5916\u7684\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u51fd\u6570\u3002\u6ce8\u610f\uff0c printNameAndSalary() \u540c\u65f6\u4f7f\u7528\u4e86\u6765\u81ea\u4e24\u4e2a\u7c7b\u7684\u53d8\u91cf( Employee::m_hourlySalary \u548c Person::m_name )\u3002 \u6d3e\u751f\u7ed3\u6784\u56fe\u5982\u4e0b\uff1a \u5982\u56fe\u6240\u793a\uff0c Employee \u548c BaseballPlayer \u5e76\u6ca1\u6709\u76f4\u63a5\u5173\u7cfb\uff0c\u5373\u4f7f\u5b83\u4eec\u90fd\u6d3e\u751f\u81ea Person \u3002 \u57fa\u4e8e Employee \u7c7b\u7f16\u5199\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #include #include class Person { public : std :: string m_name {}; int m_age {}; const std :: string & getName () const { return m_name ; } int getAge () const { return m_age ; } Person ( const std :: string & name = \"\" , int age = 0 ) : m_name { name }, m_age { age } { } }; // Employee \u516c\u5f00\u7ee7\u627f\u4e86 Person class Employee : public Person { public : double m_hourlySalary {}; long m_employeeID {}; Employee ( double hourlySalary = 0.0 , long employeeID = 0 ) : m_hourlySalary { hourlySalary }, m_employeeID { employeeID } { } void printNameAndSalary () const { std :: cout << m_name << \": \" << m_hourlySalary << '\\n' ; } }; int main () { Employee frank { 20.25 , 12345 }; frank . m_name = \"Frank\" ; // \u53ef\u4ee5\u8fd9\u4e48\u505a\u662f\u56e0\u4e3a m_name \u662f public frank . printNameAndSalary (); return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 Frank: 20.25","title":"Employee \u6d3e\u751f\u7c7b"},{"location":"17-2-basic-inheritance-in-c%2B%2B/#_1","text":"\u6d3e\u751f\u7c7b\u672c\u8eab\u4e5f\u662f\u53ef\u4ee5\u88ab\u7ee7\u627f\u7684\u3002\u800c\u4e14\u5728\u64cd\u4f5c\u548c\u4f7f\u7528\u4e0a\u90fd\u6ca1\u6709\u4ec0\u4e48\u7279\u522b\u4e4b\u5904\u3002 \u4f8b\u5982 Supervisor \u7c7b\u672c\u8eab\u662f Employee \u7c7b\u7684\u6d3e\u751f\u7c7b\uff0c Employee \u7c7b\u5219\u662f Person \u7c7b\u7684\u5b50\u7c7b\uff1a 1 2 3 4 5 6 class Supervisor : public Employee { public : // This Supervisor can oversee a max of 5 employees long m_overseesIDs [ 5 ]{}; }; \u6d3e\u751f\u7ed3\u6784\u56fe\u5982\u4e0b\uff1a \u6240\u6709\u7684 Supervisor \u5bf9\u8c61\u90fd\u7ee7\u627f Employee \u548c Person \u7684\u51fd\u6570\u548c\u53d8\u91cf\uff0c\u5e76\u6dfb\u52a0\u81ea\u5df1\u7684 m_overseesIDs \u6210\u5458\u53d8\u91cf\u3002 \u901a\u8fc7\u6784\u5efa\u8fd9\u6837\u7684\u7ee7\u627f\u94fe\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u7ec4\u53ef\u91cd\u7528\u7684\u7c7b\uff0c\u8fd9\u4e9b\u7c7b\u975e\u5e38\u901a\u7528(\u4f4d\u4e8e\u6d3e\u751f\u5c42\u6b21\u7ed3\u6784\u7684\u9876\u90e8)\uff0c\u5e76\u5728\u6bcf\u4e2a\u7ee7\u627f\u7ea7\u522b\u4e0a\u9010\u6e10\u53d8\u5f97\u66f4\u52a0\u5177\u4f53\u3002","title":"\u7ee7\u627f\u94fe"},{"location":"17-2-basic-inheritance-in-c%2B%2B/#_2","text":"\u4ece\u57fa\u7c7b\u7ee7\u627f\u610f\u5473\u7740\u4e0d\u5fc5\u5728\u6d3e\u751f\u7c7b\u4e2d\u91cd\u65b0\u5b9a\u4e49\u57fa\u7c7b\u7684\u4fe1\u606f\u3002\u6211\u4eec\u901a\u8fc7\u7ee7\u627f\u81ea\u52a8\u63a5\u6536\u57fa\u7c7b\u7684\u6210\u5458\u51fd\u6570\u548c\u6210\u5458\u53d8\u91cf\uff0c\u7136\u540e\u7b80\u5355\u5730\u6dfb\u52a0\u6240\u9700\u7684\u9644\u52a0\u51fd\u6570\u6216\u6210\u5458\u53d8\u91cf\u3002\u8fd9\u4e0d\u4ec5\u8282\u7701\u4e86\u5de5\u4f5c\uff0c\u800c\u4e14\u8fd8\u610f\u5473\u7740\u5982\u679c\u6211\u4eec\u66f4\u65b0\u6216\u4fee\u6539\u57fa\u7c7b(\u4f8b\u5982\u6dfb\u52a0\u65b0\u51fd\u6570\uff0c\u6216\u4fee\u590dbug)\uff0c\u6211\u4eec\u6240\u6709\u7684\u6d3e\u751f\u7c7b\u5c06\u81ea\u52a8\u7ee7\u627f\u66f4\u6539! \u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u5411 Person \u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u529f\u80fd\uff0c Employee \u548c Supervisor \u90fd\u4f1a\u81ea\u52a8\u83b7\u5f97\u5bf9\u5b83\u7684\u8bbf\u95ee\u6743\u9650\u3002\u5982\u679c\u6211\u4eec\u5411 Employee \u6dfb\u52a0\u4e00\u4e2a\u65b0\u53d8\u91cf\uff0c Supervisor \u4e5f\u5c06\u83b7\u5f97\u5bf9\u5b83\u7684\u8bbf\u95ee\u6743\u3002\u8fd9\u5141\u8bb8\u6211\u4eec\u4ee5\u4e00\u79cd\u7b80\u5355\u3001\u76f4\u89c2\u548c\u4f4e\u7ef4\u62a4\u7684\u65b9\u5f0f\u6784\u9020\u65b0\u7c7b!","title":"\u4e3a\u4ec0\u4e48\u6b64\u7c7b\u7ee7\u627f\u662f\u6709\u7528\u7684\uff1f"},{"location":"17-2-basic-inheritance-in-c%2B%2B/#_3","text":"\u901a\u8fc7\u7ee7\u627f\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u5176\u4ed6\u7c7b\u4e2d\u83b7\u53d6\u5176\u6210\u5458\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u91cd\u7528\u3002\u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u63a2\u7d22\u7ee7\u627f\u7684\u5de5\u4f5c\u539f\u7406\u3002","title":"\u7ed3\u8bba"},{"location":"17-3-order-of-construction-of-derived-classes/","tags":["inheritance"],"text":"Key Takeaway - \u5728 17.2 - C++\u7ee7\u627f\u57fa\u7840 \u4e2d\u6211\u4eec\u4e86\u89e3\u5230\uff0c\u7c7b\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f\u5176\u4ed6\u7c7b\u6765\u83b7\u53d6\u5176\u6210\u5458\u53d8\u91cf\u3002\u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u63a2\u8ba8\u5f53\u6d3e\u751f\u7c7b\u88ab \u5b9e\u4f8b\u5316 \u65f6\uff0c\u76f8\u5173\u7c7b\u7684\u6784\u9020\u987a\u5e8f\u662f\u5982\u4f55\u7684\u3002 \u9996\u5148\uff0c\u5b9a\u4e49\u51e0\u4e2a\u7c7b\u4ee5\u4fbf\u66f4\u597d\u5730\u8bf4\u660e\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 class Base { public : int m_id {}; Base ( int id = 0 ) : m_id { id } { } int getId () const { return m_id ; } }; class Derived : public Base { public : double m_cost {}; Derived ( double cost = 0.0 ) : m_cost { cost } { } double getCost () const { return m_cost ; } }; \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c Derived \u6d3e\u751f\u81ea Base \u3002 \u56e0\u4e3a Derived \u7ee7\u627f\u4e86 Base \u7684\u53d8\u91cf\u548c\u51fd\u6570\uff0c\u6240\u4ee5\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a Base \u7684\u8fd9\u90e8\u5206\u5185\u5bb9\u90fd\u88ab \u62f7\u8d1d \u5230\u4e86 Derived \u4e2d\u3002\u5176\u5b9e\u4e0d\u7136\uff0c\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06 Derived \u5206\u4e3a\u4e24\u90e8\u5206\u6765\u770b\uff0c\u4e00\u90e8\u5206\u662f Derived \uff0c\u53e6\u4e00\u90e8\u5206\u662f Base \u3002 \u5bf9\u4e8e\u4e00\u4e2a\u666e\u901a\u7684(\u975e\u6d3e\u751f\u7684)\u7c7b\u65f6\u7684\u5b9e\u4f8b\u5316\uff0c\u76f8\u6bd4\u4f60\u5df2\u7ecf\u5f88\u4e86\u89e3\u4e86\uff1a 1 2 3 4 5 6 int main () { Base base ; return 0 ; } Base \u4e0d\u662f\u4e00\u4e2a \u6d3e\u751f\u7c7b \uff0c\u56e0\u4e3a\u5b83\u6ca1\u6709\u7ee7\u627f\u4efb\u4f55\u5176\u4ed6\u7684\u7c7b\u3002C++\u4f1a\u9996\u5148\u4e3a Base \u5206\u914d\u5185\u5b58\uff0c\u7136\u540e\u8c03\u7528\u5b83\u7684 \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 \u8fdb\u884c\u521d\u59cb\u5316\u3002 \u63a5\u4e0b\u6765\uff0c\u5b9e\u4f8b\u5316\u4e00\u4e2a\u6d3e\u751f\u7c7b\u770b\u770b\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1a 1 2 3 4 5 6 int main () { Derived derived ; return 0 ; } \u5728\u4f60\u7f16\u5199\u5e76\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u4e0d\u4f1a\u6ce8\u610f\u5230\u4efb\u4f55\u7684\u4e0d\u540c\uff08\u76f8\u6bd4\u4e8e\u524d\u9762\u5b9e\u4f8b\u5316\u975e\u6d3e\u751f\u7c7b\u7684\u4f8b\u5b50\uff09\u3002\u4f46\u662f\u5728\u5e95\u5c42\uff0c\u5b83\u4eec\u6240\u8fdb\u884c\u7684\u5de5\u4f5c\u662f\u4e0d\u540c\u7684\u3002\u6b63\u5982\u4e0a\u9762\u63d0\u5230\u7684\u90a3\u6837\uff0c Derived \u5b9e\u9645\u4e0a\u5305\u542b\u4e24\u90e8\u5206\uff1a Base \u90e8\u5206\u548c Derived \u90e8\u5206\u3002C++ \u6784\u5efa\u6d3e\u751f\u5bf9\u8c61\u65f6\u662f\u5206\u9636\u6bb5\u8fdb\u884c\u7684\u3002\u9996\u5148\uff0c\u6700\u4e3a\u57fa\u7840\u7684\u57fa\u7c7b\uff08\u7ee7\u627f\u4f53\u7cfb\u7684\u6700\u9876\u90e8\uff09\u4f1a\u9996\u5148\u88ab\u6784\u5efa\u3002\u7136\u540e\u6bcf\u4e00\u4e2a\u5b50\u7c7b\u4f1a\u6309\u7167\u5c42\u6b21\u7ed3\u6784\u4f9d\u6b21\u6784\u5efa\uff0c\u77e5\u9053\u6700\u540e\u4e00\u4e2a\u5b50\u7c7b\uff08\u7ee7\u627f\u4f53\u7cfb\u6700\u5e95\u7aef\uff09\u6784\u5efa\u5b8c\u6210\u3002 \u56e0\u6b64\uff0c Derived \u88ab\u5b9e\u4f8b\u5316\u65f6\uff0c\u9996\u5148\u88ab\u6784\u9020\u7684\u662f Base \u90e8\u5206\uff08\u4f7f\u7528 Base \u7684\u6784\u9020\u51fd\u6570\uff09\u3002\u4e00\u65e6 Base \u90e8\u5206\u88ab\u521b\u5efa\uff0c Derived \u7684\u90e8\u5206\u5c31\u4f1a\u5f00\u59cb\u6784\u5efa\uff08\u4f7f\u7528 Derived \u7684\u6784\u9020\u51fd\u6570\uff09\u3002\u6b64\u65f6\uff0c\u5df2\u7ecf\u6ca1\u6709\u5176\u4ed6\u5b50\u7c7b\u9700\u8981\u8fdb\u4e00\u6b65\u6784\u5efa\u4e86\uff0c\u5b9e\u4f8b\u5316\u8fc7\u7a0b\u5230\u6b64\u7ed3\u675f\u3002 \u4f7f\u7528\u4e0b\u9762\u4ee3\u7801\u53ef\u4ee5\u5c55\u793a\u4e0a\u8ff0\u8fc7\u7a0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include class Base { public : int m_id {}; Base ( int id = 0 ) : m_id { id } { std :: cout << \"Base \\n \" ; } int getId () const { return m_id ; } }; class Derived : public Base { public : double m_cost {}; Derived ( double cost = 0.0 ) : m_cost { cost } { std :: cout << \"Derived \\n \" ; } double getCost () const { return m_cost ; } }; int main () { std :: cout << \"Instantiating Base \\n \" ; Base base ; std :: cout << \"Instantiating Derived \\n \" ; Derived derived ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 Instantiating Base Base Instantiating Derived Base Derived \u5982\u4f60\u6240\u89c1\uff0c\u5f53\u6211\u4eec\u6784\u9020 Derived \u65f6\uff0c Derived \u4e2d\u7684\u7684 Base \u90e8\u5206\u9996\u5148\u88ab\u6784\u9020\u3002\u9053\u7406\u663e\u800c\u6613\u89c1\uff0c\u4ece\u903b\u8f91\u4e0a\u8bb2\uff0c\u5b69\u5b50\u79bb\u5f00\u7236\u6bcd\u5c31\u4e0d\u80fd\u5b58\u5728\u3002\u8fd9\u4e5f\u662f\u4e00\u79cd\u5b89\u5168\u7684\u505a\u4e8b\u65b9\u5f0f\uff1a\u5b50\u7c7b\u7ecf\u5e38\u4f7f\u7528\u6765\u81ea\u7236\u7c7b\u7684\u53d8\u91cf\u548c\u51fd\u6570\uff0c\u4f46\u7236\u7c7b\u5bf9\u5b50\u7c7b\u4e00\u65e0\u6240\u77e5\u3002\u9996\u5148\u5b9e\u4f8b\u5316\u7236\u7c7b\u53ef\u4ee5\u786e\u4fdd\u5728\u521b\u5efa\u6d3e\u751f\u7c7b\u5e76\u51c6\u5907\u4f7f\u7528\u5b83\u4eec\u65f6\uff0c\u8fd9\u4e9b\u53d8\u91cf\u5df2\u7ecf\u521d\u59cb\u5316\u3002 \u7ee7\u627f\u94fe\u7684\u6784\u9020\u987a\u5e8f \u6709\u65f6\u5019\u4f1a\u51fa\u73b0\u7236\u7c7b\u662f\u5176\u4ed6\u7c7b\u7684\u5b50\u7c7b\u7684\u60c5\u51b5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include class A { public : A () { std :: cout << \"A \\n \" ; } }; class B : public A { public : B () { std :: cout << \"B \\n \" ; } }; class C : public B { public : C () { std :: cout << \"C \\n \" ; } }; class D : public C { public : D () { std :: cout << \"D \\n \" ; } }; \u6211\u4eec\u53ea\u9700\u8981\u8bb0\u5f97\uff0cC++ \u603b\u662f\u9996\u5148\u6784\u5efa\u201d\u7b2c\u4e00\u4e2a\u201c\u6216\u201d\u6700\u57fa\u672c\u201c\u7684\u7c7b\u3002\u7136\u540e\u5b83\u4f1a\u6cbf\u7740\u7ee7\u627f\u6811\u9010\u7ea7\u6784\u9020\u5404\u4e2a\u6d3e\u751f\u7c7b\u3002 \u4e0b\u9762\u4ee3\u7801\u53ef\u4ee5\u5c55\u793a\u6d3e\u751f\u7c7b\u5b9e\u4f8b\u5316\u65f6\u7684\u6784\u9020\u987a\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int main () { std :: cout << \"Constructing A: \\n \" ; A a ; std :: cout << \"Constructing B: \\n \" ; B b ; std :: cout << \"Constructing C: \\n \" ; C c ; std :: cout << \"Constructing D: \\n \" ; D d ; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Constructing A: A Constructing B: A B Constructing C: A B C Constructing D: A B C D \u7ed3\u8bba C++\u662f\u5206\u9636\u6bb5\u6784\u9020\u6d3e\u751f\u7c7b\u7684\uff0c\u4ece\u6700\u57fa\u672c\u7684\u7c7b(\u5728\u7ee7\u627f\u6811\u7684\u9876\u90e8)\u5f00\u59cb\uff0c\u5230\u6700\u540e\u4e00\u4e2a\u5b50\u7c7b(\u5728\u7ee7\u627f\u6811\u7684\u5e95\u90e8)\u7ed3\u675f\u3002\u5728\u6784\u9020\u6bcf\u4e2a\u7c7b\u65f6\uff0c\u8c03\u7528\u8be5\u7c7b\u7684\u9002\u5f53\u6784\u9020\u51fd\u6570\u6765\u521d\u59cb\u5316\u5c5e\u4e8e\u8be5\u7c7b\u7684\u90e8\u5206\u3002 \u6ce8\u610f\uff0c\u672c\u8282\u4e2d\u7684\u793a\u4f8b\u7c7b\u90fd\u4f7f\u7528\u57fa\u7c7b\u9ed8\u8ba4\u6784\u9020\u51fd\u6570(\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1)\u3002\u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u7814\u7a76\u6784\u9020\u51fd\u6570\u5728\u6784\u9020\u6d3e\u751f\u7c7b\u8fc7\u7a0b\u4e2d\u7684\u4f5c\u7528(\u5305\u62ec\u5982\u4f55\u663e\u5f0f\u9009\u62e9\u6d3e\u751f\u7c7b\u8981\u4f7f\u7528\u7684\u57fa\u7c7b\u6784\u9020\u51fd\u6570)\u3002","title":"17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f"},{"location":"17-3-order-of-construction-of-derived-classes/#_1","text":"\u6709\u65f6\u5019\u4f1a\u51fa\u73b0\u7236\u7c7b\u662f\u5176\u4ed6\u7c7b\u7684\u5b50\u7c7b\u7684\u60c5\u51b5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include class A { public : A () { std :: cout << \"A \\n \" ; } }; class B : public A { public : B () { std :: cout << \"B \\n \" ; } }; class C : public B { public : C () { std :: cout << \"C \\n \" ; } }; class D : public C { public : D () { std :: cout << \"D \\n \" ; } }; \u6211\u4eec\u53ea\u9700\u8981\u8bb0\u5f97\uff0cC++ \u603b\u662f\u9996\u5148\u6784\u5efa\u201d\u7b2c\u4e00\u4e2a\u201c\u6216\u201d\u6700\u57fa\u672c\u201c\u7684\u7c7b\u3002\u7136\u540e\u5b83\u4f1a\u6cbf\u7740\u7ee7\u627f\u6811\u9010\u7ea7\u6784\u9020\u5404\u4e2a\u6d3e\u751f\u7c7b\u3002 \u4e0b\u9762\u4ee3\u7801\u53ef\u4ee5\u5c55\u793a\u6d3e\u751f\u7c7b\u5b9e\u4f8b\u5316\u65f6\u7684\u6784\u9020\u987a\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int main () { std :: cout << \"Constructing A: \\n \" ; A a ; std :: cout << \"Constructing B: \\n \" ; B b ; std :: cout << \"Constructing C: \\n \" ; C c ; std :: cout << \"Constructing D: \\n \" ; D d ; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Constructing A: A Constructing B: A B Constructing C: A B C Constructing D: A B C D","title":"\u7ee7\u627f\u94fe\u7684\u6784\u9020\u987a\u5e8f"},{"location":"17-3-order-of-construction-of-derived-classes/#_2","text":"C++\u662f\u5206\u9636\u6bb5\u6784\u9020\u6d3e\u751f\u7c7b\u7684\uff0c\u4ece\u6700\u57fa\u672c\u7684\u7c7b(\u5728\u7ee7\u627f\u6811\u7684\u9876\u90e8)\u5f00\u59cb\uff0c\u5230\u6700\u540e\u4e00\u4e2a\u5b50\u7c7b(\u5728\u7ee7\u627f\u6811\u7684\u5e95\u90e8)\u7ed3\u675f\u3002\u5728\u6784\u9020\u6bcf\u4e2a\u7c7b\u65f6\uff0c\u8c03\u7528\u8be5\u7c7b\u7684\u9002\u5f53\u6784\u9020\u51fd\u6570\u6765\u521d\u59cb\u5316\u5c5e\u4e8e\u8be5\u7c7b\u7684\u90e8\u5206\u3002 \u6ce8\u610f\uff0c\u672c\u8282\u4e2d\u7684\u793a\u4f8b\u7c7b\u90fd\u4f7f\u7528\u57fa\u7c7b\u9ed8\u8ba4\u6784\u9020\u51fd\u6570(\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1)\u3002\u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u7814\u7a76\u6784\u9020\u51fd\u6570\u5728\u6784\u9020\u6d3e\u751f\u7c7b\u8fc7\u7a0b\u4e2d\u7684\u4f5c\u7528(\u5305\u62ec\u5982\u4f55\u663e\u5f0f\u9009\u62e9\u6d3e\u751f\u7c7b\u8981\u4f7f\u7528\u7684\u57fa\u7c7b\u6784\u9020\u51fd\u6570)\u3002","title":"\u7ed3\u8bba"},{"location":"17-4-constructors-and-initialization-of-derived-classes/","tags":["inheritance"],"text":"Key Takeaway - \u5728\u524d\u9762\u4e24\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u63a2\u8ba8\u4e86C++\u4e2d\u7ee7\u627f\u7684\u4e00\u4e9b\u57fa\u7840\u77e5\u8bc6\u4ee5\u53ca\u6d3e\u751f\u7c7b\u521d\u59cb\u5316\u7684\u987a\u5e8f\u3002\u672c\u8282\u8bfe\uff0c\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u4e86\u89e3\u6784\u9020\u51fd\u6570\u5728\u6d3e\u751f\u7c7b\u521d\u59cb\u5316\u4e2d\u7684\u4f5c\u7528\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u4f7f\u7528\u4e0a\u4e00\u8bfe\u4e2d\u5f00\u53d1\u7684 Base \u548c Derived \u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 class Base { public : int m_id {}; Base ( int id = 0 ) : m_id { id } { } int getId () const { return m_id ; } }; class Derived : public Base { public : double m_cost {}; Derived ( double cost = 0.0 ) : m_cost { cost } { } double getCost () const { return m_cost ; } }; \u5bf9\u4e8e\u975e\u6d3e\u751f\u7c7b\u6765\u8bf4\uff0c\u6784\u9020\u51fd\u6570\u53ea\u9700\u8981\u5173\u5fc3\u5b83\u81ea\u5df1\u7684\u6210\u5458\u5373\u53ef\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e Base \u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u50cf\u8fd9\u6837\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff1a 1 2 3 4 5 6 int main () { Base base { 5 }; // use Base(int) constructor return 0 ; } \u4e0b\u9762\u662f\u5b9e\u4f8b\u5316base\u65f6\u5b9e\u9645\u53d1\u751f\u7684\u60c5\u51b5\uff1a \u4e3a base \u5206\u914d\u5185\u5b58\uff1b \u8c03\u7528\u5408\u9002\u7684\u6784\u9020\u51fd\u6570\uff1b \u4f7f\u7528 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u6765\u521d\u59cb\u5316\u53d8\u91cf\uff1b \u6784\u9020\u51fd\u6570\u6267\u884c\u5176\u51fd\u6570\u4f53\u5185\u8bed\u53e5\uff1b \u63a7\u5236\u6743\u8fd4\u8fd8\u7ed9\u8c03\u7528\u8005\u3002 \u5f88\u7b80\u5355\u3002 \u5bf9\u4e8e\u6d3e\u751f\u7c7b\uff0c\u4e8b\u60c5\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\uff1a 1 2 3 4 5 6 int main () { Derived derived { 1.3 }; // use Derived(double) constructor return 0 ; } \u5728\u6d3e\u751f\u7c7b\u5b9e\u4f8b\u5316\u65f6\u4f1a\u6709\u5982\u4e0b\u6b65\u9aa4\uff1a \u5206\u914d\u5185\u5b58(\u6ee1\u8db3 Base \u548c Derived \u7684\u9700\u8981)\uff1b \u8c03\u7528 Derived \u7684\u6784\u9020\u51fd\u6570\uff1b Base \u5bf9\u8c61\u4f1a\u4f7f\u7528\u5408\u9002\u7684 Base \u6784\u9020\u51fd\u6570\u9996\u5148\u521d\u59cb\u5316\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u6784\u9020\u51fd\u6570\uff0c\u5219\u8c03\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff1b \u4f7f\u7528 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u521d\u59cb\u5316\u53d8\u91cf\uff1b \u6784\u9020\u51fd\u6570\u6267\u884c\u5176\u51fd\u6570\u4f53\u5185\u8bed\u53e5\uff1b \u63a7\u5236\u6743\u8fd4\u8fd8\u7ed9\u8c03\u7528\u8005\u3002 \u4e24\u4e2a\u4f8b\u5b50\u4e2d\u7684\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\uff0c\u5728 Derived \u7684\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u505a\u4efb\u4f55\u4e8b\u4e4b\u524d\uff0c Base \u7684\u6784\u9020\u51fd\u6570\u9996\u5148\u4f1a\u88ab\u8c03\u7528\uff0c\u5b83\u4f1a\u521d\u59cb\u5316\u8be5\u5bf9\u8c61\u7684 Base \u90e8\u5206\uff0c\u7136\u540e\u5c06\u63a7\u5236\u6743\u8fd4\u8fd8\u7ed9 Derived \u6784\u9020\u51fd\u6570\uff0c\u7136\u540e Derived \u624d\u80fd\u53bb\u5b8c\u6210\u5b83\u81ea\u5df1\u7684\u5de5\u4f5c\u3002 \u521d\u59cb\u5316\u57fa\u7c7b\u6210\u5458 \u6211\u4eec\u6240\u7f16\u5199\u7684\u6d3e\u751f\u7c7b\u76ee\u524d\u7684\u7f3a\u70b9\u4e4b\u4e00\u662f\uff0c\u5728\u521b\u5efa\u6d3e\u751f\u5bf9\u8c61\u65f6\u6ca1\u6709\u529e\u6cd5\u521d\u59cb\u5316 m_id \u3002\u5982\u679c\u6211\u4eec\u60f3\u5728\u521b\u5efa\u6d3e\u751f\u5bf9\u8c61\u65f6\u540c\u65f6\u8bbe\u7f6e m_cost (\u6765\u81ea\u5bf9\u8c61\u7684 Derived \u90e8\u5206)\u548c m_id (\u6765\u81ea\u5bf9\u8c61\u7684 Base \u90e8\u5206)\uff0c\u8be5\u600e\u4e48\u529e? \u65b0\u624b\u7a0b\u5e8f\u5458\u4f1a\u5c1d\u8bd5\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Derived : public Base { public : double m_cost {}; Derived ( double cost = 0.0 , int id = 0 ) // does not work : m_cost { cost } , m_id { id } { } double getCost () const { return m_cost ; } }; \u60f3\u6cd5\u5f88\u597d\u4f46\u5e76\u4e0d\u5b8c\u5168\u6b63\u786e\u3002\u6211\u4eec\u7684\u786e\u9700\u8981\u4e3a\u6784\u9020\u51fd\u6570\u6dfb\u52a0\u989d\u5916\u7684\u53c2\u6570\uff0c\u5426\u5219C++\u65e0\u4ece\u77e5\u6653\u6211\u4eec\u5e0c\u671b\u7528\u4ec0\u4e48\u503c\u6765\u521d\u59cb\u5316 m_id \u3002 \u4f46\u662f\uff0cC++\u4e0d\u5141\u8bb8\u6211\u4eec\u5728\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u7684\u521d\u59cb\u5316\u7ee7\u627f\u6765\u7684\u6210\u5458\u53d8\u91cf\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6210\u5458\u53d8\u91cf\u7684\u503c\u53ea\u80fd\u5728\u5b83\u6240\u5c5e\u7c7b\u7684\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u8bbe\u7f6e\u3002 \u4e3a\u4ec0\u4e48C++\u8981\u8fd9\u6837\u505a\uff1f\u7b54\u6848\u4e0econst\u53d8\u91cf\u548c\u5f15\u7528\u53d8\u91cf\u6709\u5173\u3002\u8003\u8651\u4e00\u4e0b\u5982\u679c m_id \u662f const \u4f1a\u53d1\u751f\u4ec0\u4e48\u3002\u56e0\u4e3aconst\u53d8\u91cf\u5fc5\u987b\u5728\u521b\u5efa\u65f6\u7528\u4e00\u4e2a\u503c\u521d\u59cb\u5316\uff0c\u6240\u4ee5\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u5fc5\u987b\u5728\u521b\u5efa\u53d8\u91cf\u65f6\u8bbe\u7f6e\u5b83\u7684\u503c\u3002\u4f46\u662f\uff0c\u5f53\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u5b8c\u6210\u65f6\uff0c\u5c06\u6267\u884c\u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u3002\u7136\u540e\u6bcf\u4e2a\u6d3e\u751f\u7c7b\u90fd\u6709\u673a\u4f1a\u521d\u59cb\u5316\u8be5\u53d8\u91cf\uff0c\u53ef\u80fd\u4f1a\u6539\u53d8\u5b83\u7684\u503c!\u901a\u8fc7\u5c06\u53d8\u91cf\u7684\u521d\u59cb\u5316\u9650\u5236\u5728\u8fd9\u4e9b\u53d8\u91cf\u6240\u5c5e\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4e2d\uff0cC++\u9700\u8981\u786e\u4fdd\u6240\u6709\u53d8\u91cf\u53ea\u521d\u59cb\u5316\u4e00\u6b21\u3002 \u6700\u7ec8\u7684\u7ed3\u679c\u662f\u4e0a\u9762\u7684\u793a\u4f8b\u4e0d\u8d77\u4f5c\u7528\uff0c\u56e0\u4e3a m_id \u662f\u4ece Base \u7ee7\u627f\u7684\uff0c\u5e76\u4e14\u53ea\u6709\u975e\u7ee7\u627f\u7684\u53d8\u91cf\u53ef\u4ee5\u5728\u6210\u5458\u521d\u59cb\u5316\u5668\u5217\u8868\u4e2d\u521d\u59cb\u5316\u3002 \u4f46\u662f\uff0c\u7ee7\u627f\u7684\u53d8\u91cf\u4ecd\u7136\u53ef\u4ee5\u5728\u6784\u9020\u51fd\u6570\u4f53\u4e2d\u4f7f\u7528\u8d4b\u503c\u64cd\u4f5c\u66f4\u6539\u5176\u503c\u3002\u56e0\u6b64\uff0c\u65b0\u7a0b\u5e8f\u5458\u901a\u5e38\u4e5f\u4f1a\u8fd9\u6837\u505a: 1 2 3 4 5 6 7 8 9 10 11 12 13 class Derived : public Base { public : double m_cost {}; Derived ( double cost = 0.0 , int id = 0 ) : m_cost { cost } { m_id = id ; } double getCost () const { return m_cost ; } }; \u867d\u7136\u8fd9\u5728\u672c\u4f8b\u4e2d\u5b9e\u9645\u4e0a\u662f\u53ef\u884c\u7684\uff0c\u4f46\u5982\u679c m_id \u662f const \u6216\u5f15\u7528\uff0c\u5219\u4e0d\u53ef\u884c(\u56e0\u4e3a const \u503c\u548c\u5f15\u7528\u5fc5\u987b\u5728\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u521d\u59cb\u5316)\u3002\u5b83\u7684\u6548\u7387\u4e5f\u5f88\u4f4e\uff0c\u56e0\u4e3a m_id \u88ab\u5206\u914d\u4e86\u4e24\u6b21\u503c\uff1a\u4e00\u6b21\u662f\u5728\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\uff0c\u7136\u540e\u662f\u5728\u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u7684\u4e3b\u4f53\u4e2d\u3002\u6700\u540e\uff0c\u5982\u679c\u57fa\u7c7b\u5728\u6784\u9020\u8fc7\u7a0b\u4e2d\u9700\u8981\u8bbf\u95ee\u8fd9\u4e2a\u503c\u600e\u4e48\u529e\uff1f\u5b83\u6ca1\u6709\u529e\u6cd5\u8bbf\u95ee\u5b83\uff0c\u56e0\u4e3a\u5b83\u662f\u5728\u6267\u884c Derived \u6784\u9020\u51fd\u6570\u4e4b\u524d\u624d\u8bbe\u7f6e\u7684(\u8fd9\u57fa\u672c\u4e0a\u662f\u5728\u6700\u540e\u6267\u884c)\u3002 \u90a3\u4e48\uff0c\u6211\u4eec\u5e94\u8be5\u5982\u4f55\u5b50\u521b\u5efa Derived \u7c7b\u5bf9\u8c61\u65f6\u6b63\u786e\u521d\u59cb\u5316 m_id \u5462\uff1f \u5728\u4e0a\u9762\u7684\u8fd9\u4e9b\u4f8b\u5b50\u4e2d\uff0c\u5f53\u6211\u4eec\u521d\u59cb\u5316 Derived \u7c7b\u7684\u5bf9\u8c61\u65f6\uff0c Base \u7c7b\u7684\u90e8\u5206\u90fd\u662f\u901a\u8fc7\u5b83\u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u6765\u521b\u5efa\u7684\u3002\u4e3a\u4ec0\u4e48\u5b83\u603b\u662f\u4f1a\u8c03\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u5462\uff1f\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u8ba9\u4ed6\u4e0d\u8981\u8fd9\u4e48\u505a\u554a\uff01 \u6240\u5e78\uff0cC++ \u5141\u8bb8\u6211\u4eec\u663e\u5f0f\u5730\u6307\u5b9a\u521b\u5efa Base \u7c7b\u65f6\u5e94\u8be5\u4f7f\u7528\u7684\u6784\u9020\u51fd\u6570\uff01\u6211\u4eec\u53ea\u9700\u8981\u5728\u6d3e\u751f\u7c7b\u7684 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u4e2d\u8c03\u7528\u6240\u9700\u7684 Base \u7684\u6784\u9020\u51fd\u6570\u5c31\u53ef\u4ee5\u4e86\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 class Derived : public Base { public : double m_cost {}; Derived ( double cost = 0.0 , int id = 0 ) : Base { id } // Call Base(int) constructor with value id! , m_cost { cost } { } double getCost () const { return m_cost ; } }; \u518d\u6b21\u6267\u884c\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { Derived derived { 1.3 , 5 }; // use Derived(double, int) constructor std :: cout << \"Id: \" << derived . getId () << '\\n' ; std :: cout << \"Cost: \" << derived . getCost () << '\\n' ; return 0 ; } \u57fa\u7c7b\u7684\u6784\u9020\u51fd\u6570 Base(int) \u5c06\u4f1a\u88ab\u7528\u6765\u521d\u59cb\u5316\u6210\u5458 m_id \uff085\uff09\u7136\u540e\u6d3e\u751f\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4f1a\u88ab\u7528\u6765\u521d\u59cb\u5316 m_cost \u5230 1.3 \uff01 \u56e0\u6b64\uff0c\u7a0b\u5e8f\u4f1a\u6253\u5370\uff1a 1 2 Id: 5 Cost: 1.3 \u5177\u4f53\u6765\u8bf4\uff1a \u5185\u5b58\u5206\u914d\uff1b \u6784\u9020\u51fd\u6570 Derived(double, int) \u88ab\u8c03\u7528\uff0c cost = 1.3 \uff0c id = 5 \u3002 \u7f16\u8bd1\u5668\u67e5\u770b\u4f60\u662f\u5426\u6307\u5b9a\u4e86 Base \u7c7b\u7684\u6784\u9020\u51fd\u6570\u3002\u8fd9\u91cc\u6211\u4eec\u6307\u5b9a\u4e86\uff0c\u6240\u4ee5\u5b83\u4f1a\u8c03\u7528 Base(int) \u4e14 id = 5 \u3002 \u57fa\u7c7b\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u5c06 m_id \u8bbe\u7f6e\u4e3a5\uff1b \u57fa\u7c7b\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u6267\u884c\uff0c\u4ec0\u4e48\u90fd\u6ca1\u505a\uff1b \u57fa\u7c7b\u6784\u9020\u51fd\u6570\u8fd4\u56de\uff1b \u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u5c06 m_cost \u8bbe\u7f6e\u4e3a 1.3\uff1b \u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u6267\u884c\uff0c\u4ec0\u4e48\u90fd\u6ca1\u505a\uff1b \u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u8fd4\u56de\u3002 \u770b\u8d77\u6765\u6709\u70b9\u590d\u6742\uff0c\u4f46\u5b9e\u9645\u4e0a\u975e\u5e38\u7b80\u5355\u3002\u6240\u53d1\u751f\u7684\u4e00\u5207\u5c31\u662f Derived \u6784\u9020\u51fd\u6570\u8c03\u7528\u6307\u5b9a\u7684 Base \u6784\u9020\u51fd\u6570\u6765\u521d\u59cb\u5316\u5bf9\u8c61\u7684 Base \u90e8\u5206\u3002\u56e0\u4e3a m_id \u4f4d\u4e8e\u5bf9\u8c61\u7684 Base \u90e8\u5206\uff0c\u6240\u4ee5 Base \u6784\u9020\u51fd\u6570\u662f\u552f\u4e00\u53ef\u4ee5\u521d\u59cb\u5316\u8be5\u503c\u7684\u6784\u9020\u51fd\u6570\u3002 \u6ce8\u610f\uff0cBase\u6784\u9020\u51fd\u6570\u5728 Derived \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u7684\u4ec0\u4e48\u4f4d\u7f6e\u88ab\u8c03\u7528\u5e76\u4e0d\u91cd\u8981\u2014\u2014\u5b83\u603b\u662f\u9996\u5148\u6267\u884c\u3002 \u5c06\u6210\u5458\u8bbe\u4e3a\u79c1\u6709 \u65e2\u7136\u5df2\u7ecf\u77e5\u9053\u4e86\u5982\u4f55\u521d\u59cb\u5316\u57fa\u7c7b\u6210\u5458\uff0c\u5c31\u6ca1\u6709\u5fc5\u8981\u5c06\u6210\u5458\u53d8\u91cf\u4fdd\u6301\u4e3a public \u3002\u6211\u4eec\u518d\u6b21\u5c06\u6210\u5458\u53d8\u91cf\u8bbe\u4e3a\u79c1\u6709\uff0c\u56e0\u4e3a\u5b83\u4eec\u5e94\u8be5\u662f\u79c1\u6709\u7684\u3002 \u5feb\u901f\u56de\u987e\u4e00\u4e0b\uff0c\u516c\u5171\u6210\u5458\u53ef\u4ee5\u88ab\u4efb\u4f55\u4eba\u8bbf\u95ee\u3002\u79c1\u6709\u6210\u5458\u53ea\u80fd\u7531\u540c\u4e00\u7c7b\u7684\u6210\u5458\u51fd\u6570\u8bbf\u95ee\u3002\u6ce8\u610f\uff0c \u8fd9\u610f\u5473\u7740\u6d3e\u751f\u7c7b\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u57fa\u7c7b\u7684\u79c1\u6709\u6210\u5458 \uff01\u6d3e\u751f\u7c7b\u5c06\u9700\u8981\u4f7f\u7528\u8bbf\u95ee\u51fd\u6570\u6765\u8bbf\u95ee\u57fa\u7c7b\u7684\u79c1\u6709\u6210\u5458\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include class Base { private : // our member is now private int m_id {}; public : Base ( int id = 0 ) : m_id { id } { } int getId () const { return m_id ; } }; class Derived : public Base { private : // our member is now private double m_cost ; public : Derived ( double cost = 0.0 , int id = 0 ) : Base { id } // Call Base(int) constructor with value id! , m_cost { cost } { } double getCost () const { return m_cost ; } }; int main () { Derived derived { 1.3 , 5 }; // use Derived(double, int) constructor std :: cout << \"Id: \" << derived . getId () << '\\n' ; std :: cout << \"Cost: \" << derived . getCost () << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06 m_id \u548c m_cost \u8bbe\u4e3a\u79c1\u6709\u3002\u8fd9\u4e48\u505a\u5b8c\u5168\u6ca1\u6709\u95ee\u9898\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u76f8\u5173\u7684\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u8fd9\u4e9b\u6210\u5458\uff0c\u5e76\u901a\u8fc7\u516c\u6709\u7684 \u6210\u5458\u8bbf\u95ee\u51fd\u6570 \u6765\u8bbf\u95ee\u5b83\u4eec\u3002 \u6253\u5370\u7ed3\u679c\u5982\u6211\u4eec\u6240\u60f3\u7684\u90a3\u6837\uff1a 1 2 Id: 5 Cost: 1.3 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u66f4\u591a\u5730\u8ba8\u8bba\u8bbf\u95ee\u8bf4\u660e\u7b26\u3002 \u53e6\u5916\u4e00\u4e2a\u4f8b\u5b50 \u518d\u770b\u770b\u4e4b\u524d\u6211\u4eec\u4f7f\u7528\u8fc7\u7684\u4e00\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include #include class Person { public : std :: string m_name ; int m_age {}; Person ( const std :: string_view name = \"\" , int age = 0 ) : m_name { name }, m_age { age } { } const std :: string & getName () const { return m_name ; } int getAge () const { return m_age ; } }; // BaseballPlayer publicly inheriting Person class BaseballPlayer : public Person { public : double m_battingAverage {}; int m_homeRuns {}; BaseballPlayer ( double battingAverage = 0.0 , int homeRuns = 0 ) : m_battingAverage { battingAverage }, m_homeRuns { homeRuns } { } }; \u5728\u4e4b\u524d\u7684\u4f8b\u5b50\u4e2d\uff0c BaseballPlayer \u53ea\u4f1a\u521d\u59cb\u5316\u5b83\u81ea\u5df1\u7684\u6210\u5458\uff0c\u4e5f\u5e76\u6ca1\u6709\u6307\u5b9a Person \u7684\u6784\u9020\u51fd\u6570\u3002\u8fd9\u610f\u5473\u7740 BaseballPlayer \u5728\u521b\u5efa\u65f6\uff0c\u8c03\u7528\u7684\u90fd\u662f Person \u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u5b83\u4f1a\u5c06\u540d\u5b57\u521d\u59cb\u5316\u4e3a\u7a7a\u767d\u5e76\u5c06\u5e74\u9f84\u521d\u59cb\u5316\u4e3a0\u3002\u56e0\u4e3a\uff0c\u56e0\u4e3a\u5728\u521b\u5efa BaseballPlayer \u65f6\u7ed9\u5b83\u4eec\u4e00\u4e2a\u540d\u79f0\u548c\u5e74\u9f84\u662f\u6709\u610f\u4e49\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u5e94\u8be5\u4fee\u6539\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u6765\u6dfb\u52a0\u8fd9\u4e9b\u53c2\u6570\u3002 \u66f4\u65b0\u4ee3\u7801\uff0c\u8ba9\u7c7b\u4f7f\u7528\u79c1\u6709\u6210\u5458\uff0c\u540c\u65f6\u8ba9 BaseballPlayer \u7c7b\u8c03\u7528\u9002\u5f53\u7684 Person \u6784\u9020\u51fd\u6570\u6765\u521d\u59cb\u5316\u7ee7\u627f\u7684 Person \u6210\u5458\u53d8\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include #include class Person { private : std :: string m_name ; int m_age {}; public : Person ( const std :: string_view name = \"\" , int age = 0 ) : m_name { name }, m_age { age } { } const std :: string & getName () const { return m_name ; } int getAge () const { return m_age ; } }; // BaseballPlayer \u516c\u5f00\u7ee7\u627f Person class BaseballPlayer : public Person { private : double m_battingAverage {}; int m_homeRuns {}; public : BaseballPlayer ( const std :: string_view name = \"\" , int age = 0 , double battingAverage = 0.0 , int homeRuns = 0 ) : Person { name , age } // \u8c03\u7528 Person(const std::string_view, int) \u6765\u521d\u59cb\u5316\u76f8\u5e94\u7684\u6210\u5458 , m_battingAverage { battingAverage }, m_homeRuns { homeRuns } { } double getBattingAverage () const { return m_battingAverage ; } int getHomeRuns () const { return m_homeRuns ; } }; \u73b0\u5728\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { BaseballPlayer pedro { \"Pedro Cerrano\" , 32 , 0.342 , 42 }; std :: cout << pedro . getName () << '\\n' ; std :: cout << pedro . getAge () << '\\n' ; std :: cout << pedro . getBattingAverage () << '\\n' ; std :: cout << pedro . getHomeRuns () << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 2 3 4 Pedro Cerrano 32 0.342 42 \u53ef\u4ee5\u770b\u5230\uff0c\u57fa\u7c7b\u7684\u540d\u79f0\u548c\u5e74\u9f84\u5df2\u6b63\u786e\u521d\u59cb\u5316\uff0c\u6d3e\u751f\u7c7b\u7684\u672c\u5792\u6253\u6570\u548c\u51fb\u7403\u7387\u4e5f\u5df2\u521d\u59cb\u5316\u3002 \u7ee7\u627f\u94fe \u7ee7\u627f\u94fe\u4e2d\u7684\u7c7b\u4ee5\u5b8c\u5168\u76f8\u540c\u7684\u65b9\u5f0f\u5de5\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include class A { public : A ( int a ) { std :: cout << \"A: \" << a << '\\n' ; } }; class B : public A { public : B ( int a , double b ) : A { a } { std :: cout << \"B: \" << b << '\\n' ; } }; class C : public B { public : C ( int a , double b , char c ) : B { a , b } { std :: cout << \"C: \" << c << '\\n' ; } }; int main () { C c { 5 , 4.3 , 'R' }; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7c7bC\u662f\u7531\u7c7bB\u6d3e\u751f\u800c\u6765\u7684\uff0c\u800c\u7c7bB\u53c8\u662f\u7531\u7c7bA\u6d3e\u751f\u800c\u6765\u7684\u3002\u90a3\u4e48\u5f53\u6211\u4eec\u5b9e\u4f8b\u5316\u7c7bC\u7684\u5bf9\u8c61\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462? \u9996\u5148 main() \u4f1a\u8c03\u7528 C(int, double, char) \u3002C \u7684\u6784\u9020\u51fd\u6570\u4f1a\u8c03\u7528 B(int, double) \u3002B\u7684\u6784\u9020\u51fd\u6570\u8c03\u7528 A(int) \u3002\u56e0\u4e3aA\u6ca1\u6709\u7ee7\u627f\u4efb\u4f55\u7c7b\uff0c\u6240\u4ee5\u5b83\u4f1a\u88ab\u7b2c\u4e00\u4e2a\u6784\u9020\u3002A \u88ab\u6784\u9020\u540e\u6253\u5370\u4e86 5\uff0c\u7136\u540e\u63a7\u5236\u6743\u8fd4\u56de\u7ed9B\u3002B\u88ab\u6784\u9020\u65f6\u6253\u5370\u503c 4.3\uff0c\u7136\u540e\u8fd4\u56de\u63a7\u5236\u6743\u7ed9C\u3002C\u88ab\u6784\u9020\u65f6\u6253\u5370\u2018R\u2019\uff0c\u7136\u540e\u5c06\u63a7\u5236\u6743\u8fd4\u56de\u7ed9 main() \u3002\u6784\u9020\u7ed3\u675f\uff01 \u56e0\u6b64\u7a0b\u5e8f\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 A: 5 B: 4.3 C: R \u503c\u5f97\u4e00\u63d0\u7684\u662f\uff0c\u6784\u9020\u51fd\u6570\u53ea\u80fd\u8c03\u7528 \u76f4\u63a5 \u7236\u7c7b\uff08\u57fa\u7c7b\uff09\u4e2d\u7684\u6784\u9020\u51fd\u6570\u3002\u56e0\u6b64\uff0cC\u6784\u9020\u51fd\u6570\u4e0d\u80fd\u76f4\u63a5\u8c03\u7528\u6216\u5c06\u53c2\u6570\u4f20\u9012\u7ed9A\u6784\u9020\u51fd\u6570\u3002C\u6784\u9020\u51fd\u6570\u53ea\u80fd\u8c03\u7528B\u6784\u9020\u51fd\u6570(B\u6784\u9020\u51fd\u6570\u8d1f\u8d23\u8c03\u7528A\u6784\u9020\u51fd\u6570)\u3002 \u6790\u6784\u51fd\u6570 \u5f53\u6d3e\u751f\u7c7b\u88ab\u9500\u6bc1\u65f6\uff0c\u6bcf\u4e2a\u6790\u6784\u51fd\u6570\u5c06\u6309\u6784\u9020\u7684 \u9006\u987a\u5e8f\u8c03\u7528 \u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53C\u88ab\u9500\u6bc1\u65f6\uff0c\u9996\u5148\u8c03\u7528C\u6790\u6784\u51fd\u6570\uff0c\u7136\u540e\u662fB\u6790\u6784\u51fd\u6570\uff0c\u7136\u540e\u662fA\u6790\u6784\u51fd\u6570\u3002 \u5c0f\u7ed3 \u5728\u6784\u9020\u6d3e\u751f\u7c7b\u65f6\uff0c\u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u8d1f\u8d23\u786e\u5b9a\u8c03\u7528\u54ea\u4e2a\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u57fa\u7c7b\u6784\u9020\u51fd\u6570\uff0c\u5c06\u4f7f\u7528\u9ed8\u8ba4\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u627e\u4e0d\u5230\u7f3a\u7701\u57fa\u7c7b\u6784\u9020\u51fd\u6570(\u6216\u7f3a\u7701\u521b\u5efa\u57fa\u7c7b\u6784\u9020\u51fd\u6570)\uff0c\u7f16\u8bd1\u5668\u5c06\u62a5\u9519\u3002\u7136\u540e\u6309\u7167\u4ece\u6700\u57fa\u7c7b\u5230\u6700\u6d3e\u751f\u7c7b\u7684\u987a\u5e8f\u6784\u9020\u7c7b\u3002 \u81f3\u6b64\uff0c\u60a8\u5df2\u7ecf\u8db3\u591f\u4e86\u89e3C++\u7ee7\u627f\uff0c\u53ef\u4ee5\u521b\u5efa\u81ea\u5df1\u7684\u7ee7\u627f\u7c7b\u4e86!","title":"17.4 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u548c\u521d\u59cb\u5316"},{"location":"17-4-constructors-and-initialization-of-derived-classes/#_1","text":"\u6211\u4eec\u6240\u7f16\u5199\u7684\u6d3e\u751f\u7c7b\u76ee\u524d\u7684\u7f3a\u70b9\u4e4b\u4e00\u662f\uff0c\u5728\u521b\u5efa\u6d3e\u751f\u5bf9\u8c61\u65f6\u6ca1\u6709\u529e\u6cd5\u521d\u59cb\u5316 m_id \u3002\u5982\u679c\u6211\u4eec\u60f3\u5728\u521b\u5efa\u6d3e\u751f\u5bf9\u8c61\u65f6\u540c\u65f6\u8bbe\u7f6e m_cost (\u6765\u81ea\u5bf9\u8c61\u7684 Derived \u90e8\u5206)\u548c m_id (\u6765\u81ea\u5bf9\u8c61\u7684 Base \u90e8\u5206)\uff0c\u8be5\u600e\u4e48\u529e? \u65b0\u624b\u7a0b\u5e8f\u5458\u4f1a\u5c1d\u8bd5\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Derived : public Base { public : double m_cost {}; Derived ( double cost = 0.0 , int id = 0 ) // does not work : m_cost { cost } , m_id { id } { } double getCost () const { return m_cost ; } }; \u60f3\u6cd5\u5f88\u597d\u4f46\u5e76\u4e0d\u5b8c\u5168\u6b63\u786e\u3002\u6211\u4eec\u7684\u786e\u9700\u8981\u4e3a\u6784\u9020\u51fd\u6570\u6dfb\u52a0\u989d\u5916\u7684\u53c2\u6570\uff0c\u5426\u5219C++\u65e0\u4ece\u77e5\u6653\u6211\u4eec\u5e0c\u671b\u7528\u4ec0\u4e48\u503c\u6765\u521d\u59cb\u5316 m_id \u3002 \u4f46\u662f\uff0cC++\u4e0d\u5141\u8bb8\u6211\u4eec\u5728\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u7684\u521d\u59cb\u5316\u7ee7\u627f\u6765\u7684\u6210\u5458\u53d8\u91cf\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6210\u5458\u53d8\u91cf\u7684\u503c\u53ea\u80fd\u5728\u5b83\u6240\u5c5e\u7c7b\u7684\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u8bbe\u7f6e\u3002 \u4e3a\u4ec0\u4e48C++\u8981\u8fd9\u6837\u505a\uff1f\u7b54\u6848\u4e0econst\u53d8\u91cf\u548c\u5f15\u7528\u53d8\u91cf\u6709\u5173\u3002\u8003\u8651\u4e00\u4e0b\u5982\u679c m_id \u662f const \u4f1a\u53d1\u751f\u4ec0\u4e48\u3002\u56e0\u4e3aconst\u53d8\u91cf\u5fc5\u987b\u5728\u521b\u5efa\u65f6\u7528\u4e00\u4e2a\u503c\u521d\u59cb\u5316\uff0c\u6240\u4ee5\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u5fc5\u987b\u5728\u521b\u5efa\u53d8\u91cf\u65f6\u8bbe\u7f6e\u5b83\u7684\u503c\u3002\u4f46\u662f\uff0c\u5f53\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u5b8c\u6210\u65f6\uff0c\u5c06\u6267\u884c\u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u3002\u7136\u540e\u6bcf\u4e2a\u6d3e\u751f\u7c7b\u90fd\u6709\u673a\u4f1a\u521d\u59cb\u5316\u8be5\u53d8\u91cf\uff0c\u53ef\u80fd\u4f1a\u6539\u53d8\u5b83\u7684\u503c!\u901a\u8fc7\u5c06\u53d8\u91cf\u7684\u521d\u59cb\u5316\u9650\u5236\u5728\u8fd9\u4e9b\u53d8\u91cf\u6240\u5c5e\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4e2d\uff0cC++\u9700\u8981\u786e\u4fdd\u6240\u6709\u53d8\u91cf\u53ea\u521d\u59cb\u5316\u4e00\u6b21\u3002 \u6700\u7ec8\u7684\u7ed3\u679c\u662f\u4e0a\u9762\u7684\u793a\u4f8b\u4e0d\u8d77\u4f5c\u7528\uff0c\u56e0\u4e3a m_id \u662f\u4ece Base \u7ee7\u627f\u7684\uff0c\u5e76\u4e14\u53ea\u6709\u975e\u7ee7\u627f\u7684\u53d8\u91cf\u53ef\u4ee5\u5728\u6210\u5458\u521d\u59cb\u5316\u5668\u5217\u8868\u4e2d\u521d\u59cb\u5316\u3002 \u4f46\u662f\uff0c\u7ee7\u627f\u7684\u53d8\u91cf\u4ecd\u7136\u53ef\u4ee5\u5728\u6784\u9020\u51fd\u6570\u4f53\u4e2d\u4f7f\u7528\u8d4b\u503c\u64cd\u4f5c\u66f4\u6539\u5176\u503c\u3002\u56e0\u6b64\uff0c\u65b0\u7a0b\u5e8f\u5458\u901a\u5e38\u4e5f\u4f1a\u8fd9\u6837\u505a: 1 2 3 4 5 6 7 8 9 10 11 12 13 class Derived : public Base { public : double m_cost {}; Derived ( double cost = 0.0 , int id = 0 ) : m_cost { cost } { m_id = id ; } double getCost () const { return m_cost ; } }; \u867d\u7136\u8fd9\u5728\u672c\u4f8b\u4e2d\u5b9e\u9645\u4e0a\u662f\u53ef\u884c\u7684\uff0c\u4f46\u5982\u679c m_id \u662f const \u6216\u5f15\u7528\uff0c\u5219\u4e0d\u53ef\u884c(\u56e0\u4e3a const \u503c\u548c\u5f15\u7528\u5fc5\u987b\u5728\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u521d\u59cb\u5316)\u3002\u5b83\u7684\u6548\u7387\u4e5f\u5f88\u4f4e\uff0c\u56e0\u4e3a m_id \u88ab\u5206\u914d\u4e86\u4e24\u6b21\u503c\uff1a\u4e00\u6b21\u662f\u5728\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\uff0c\u7136\u540e\u662f\u5728\u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u7684\u4e3b\u4f53\u4e2d\u3002\u6700\u540e\uff0c\u5982\u679c\u57fa\u7c7b\u5728\u6784\u9020\u8fc7\u7a0b\u4e2d\u9700\u8981\u8bbf\u95ee\u8fd9\u4e2a\u503c\u600e\u4e48\u529e\uff1f\u5b83\u6ca1\u6709\u529e\u6cd5\u8bbf\u95ee\u5b83\uff0c\u56e0\u4e3a\u5b83\u662f\u5728\u6267\u884c Derived \u6784\u9020\u51fd\u6570\u4e4b\u524d\u624d\u8bbe\u7f6e\u7684(\u8fd9\u57fa\u672c\u4e0a\u662f\u5728\u6700\u540e\u6267\u884c)\u3002 \u90a3\u4e48\uff0c\u6211\u4eec\u5e94\u8be5\u5982\u4f55\u5b50\u521b\u5efa Derived \u7c7b\u5bf9\u8c61\u65f6\u6b63\u786e\u521d\u59cb\u5316 m_id \u5462\uff1f \u5728\u4e0a\u9762\u7684\u8fd9\u4e9b\u4f8b\u5b50\u4e2d\uff0c\u5f53\u6211\u4eec\u521d\u59cb\u5316 Derived \u7c7b\u7684\u5bf9\u8c61\u65f6\uff0c Base \u7c7b\u7684\u90e8\u5206\u90fd\u662f\u901a\u8fc7\u5b83\u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u6765\u521b\u5efa\u7684\u3002\u4e3a\u4ec0\u4e48\u5b83\u603b\u662f\u4f1a\u8c03\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u5462\uff1f\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u8ba9\u4ed6\u4e0d\u8981\u8fd9\u4e48\u505a\u554a\uff01 \u6240\u5e78\uff0cC++ \u5141\u8bb8\u6211\u4eec\u663e\u5f0f\u5730\u6307\u5b9a\u521b\u5efa Base \u7c7b\u65f6\u5e94\u8be5\u4f7f\u7528\u7684\u6784\u9020\u51fd\u6570\uff01\u6211\u4eec\u53ea\u9700\u8981\u5728\u6d3e\u751f\u7c7b\u7684 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u4e2d\u8c03\u7528\u6240\u9700\u7684 Base \u7684\u6784\u9020\u51fd\u6570\u5c31\u53ef\u4ee5\u4e86\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 class Derived : public Base { public : double m_cost {}; Derived ( double cost = 0.0 , int id = 0 ) : Base { id } // Call Base(int) constructor with value id! , m_cost { cost } { } double getCost () const { return m_cost ; } }; \u518d\u6b21\u6267\u884c\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { Derived derived { 1.3 , 5 }; // use Derived(double, int) constructor std :: cout << \"Id: \" << derived . getId () << '\\n' ; std :: cout << \"Cost: \" << derived . getCost () << '\\n' ; return 0 ; } \u57fa\u7c7b\u7684\u6784\u9020\u51fd\u6570 Base(int) \u5c06\u4f1a\u88ab\u7528\u6765\u521d\u59cb\u5316\u6210\u5458 m_id \uff085\uff09\u7136\u540e\u6d3e\u751f\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4f1a\u88ab\u7528\u6765\u521d\u59cb\u5316 m_cost \u5230 1.3 \uff01 \u56e0\u6b64\uff0c\u7a0b\u5e8f\u4f1a\u6253\u5370\uff1a 1 2 Id: 5 Cost: 1.3 \u5177\u4f53\u6765\u8bf4\uff1a \u5185\u5b58\u5206\u914d\uff1b \u6784\u9020\u51fd\u6570 Derived(double, int) \u88ab\u8c03\u7528\uff0c cost = 1.3 \uff0c id = 5 \u3002 \u7f16\u8bd1\u5668\u67e5\u770b\u4f60\u662f\u5426\u6307\u5b9a\u4e86 Base \u7c7b\u7684\u6784\u9020\u51fd\u6570\u3002\u8fd9\u91cc\u6211\u4eec\u6307\u5b9a\u4e86\uff0c\u6240\u4ee5\u5b83\u4f1a\u8c03\u7528 Base(int) \u4e14 id = 5 \u3002 \u57fa\u7c7b\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u5c06 m_id \u8bbe\u7f6e\u4e3a5\uff1b \u57fa\u7c7b\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u6267\u884c\uff0c\u4ec0\u4e48\u90fd\u6ca1\u505a\uff1b \u57fa\u7c7b\u6784\u9020\u51fd\u6570\u8fd4\u56de\uff1b \u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u7684\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868\u5c06 m_cost \u8bbe\u7f6e\u4e3a 1.3\uff1b \u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u6267\u884c\uff0c\u4ec0\u4e48\u90fd\u6ca1\u505a\uff1b \u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u8fd4\u56de\u3002 \u770b\u8d77\u6765\u6709\u70b9\u590d\u6742\uff0c\u4f46\u5b9e\u9645\u4e0a\u975e\u5e38\u7b80\u5355\u3002\u6240\u53d1\u751f\u7684\u4e00\u5207\u5c31\u662f Derived \u6784\u9020\u51fd\u6570\u8c03\u7528\u6307\u5b9a\u7684 Base \u6784\u9020\u51fd\u6570\u6765\u521d\u59cb\u5316\u5bf9\u8c61\u7684 Base \u90e8\u5206\u3002\u56e0\u4e3a m_id \u4f4d\u4e8e\u5bf9\u8c61\u7684 Base \u90e8\u5206\uff0c\u6240\u4ee5 Base \u6784\u9020\u51fd\u6570\u662f\u552f\u4e00\u53ef\u4ee5\u521d\u59cb\u5316\u8be5\u503c\u7684\u6784\u9020\u51fd\u6570\u3002 \u6ce8\u610f\uff0cBase\u6784\u9020\u51fd\u6570\u5728 Derived \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u7684\u4ec0\u4e48\u4f4d\u7f6e\u88ab\u8c03\u7528\u5e76\u4e0d\u91cd\u8981\u2014\u2014\u5b83\u603b\u662f\u9996\u5148\u6267\u884c\u3002","title":"\u521d\u59cb\u5316\u57fa\u7c7b\u6210\u5458"},{"location":"17-4-constructors-and-initialization-of-derived-classes/#_2","text":"\u65e2\u7136\u5df2\u7ecf\u77e5\u9053\u4e86\u5982\u4f55\u521d\u59cb\u5316\u57fa\u7c7b\u6210\u5458\uff0c\u5c31\u6ca1\u6709\u5fc5\u8981\u5c06\u6210\u5458\u53d8\u91cf\u4fdd\u6301\u4e3a public \u3002\u6211\u4eec\u518d\u6b21\u5c06\u6210\u5458\u53d8\u91cf\u8bbe\u4e3a\u79c1\u6709\uff0c\u56e0\u4e3a\u5b83\u4eec\u5e94\u8be5\u662f\u79c1\u6709\u7684\u3002 \u5feb\u901f\u56de\u987e\u4e00\u4e0b\uff0c\u516c\u5171\u6210\u5458\u53ef\u4ee5\u88ab\u4efb\u4f55\u4eba\u8bbf\u95ee\u3002\u79c1\u6709\u6210\u5458\u53ea\u80fd\u7531\u540c\u4e00\u7c7b\u7684\u6210\u5458\u51fd\u6570\u8bbf\u95ee\u3002\u6ce8\u610f\uff0c \u8fd9\u610f\u5473\u7740\u6d3e\u751f\u7c7b\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u57fa\u7c7b\u7684\u79c1\u6709\u6210\u5458 \uff01\u6d3e\u751f\u7c7b\u5c06\u9700\u8981\u4f7f\u7528\u8bbf\u95ee\u51fd\u6570\u6765\u8bbf\u95ee\u57fa\u7c7b\u7684\u79c1\u6709\u6210\u5458\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include class Base { private : // our member is now private int m_id {}; public : Base ( int id = 0 ) : m_id { id } { } int getId () const { return m_id ; } }; class Derived : public Base { private : // our member is now private double m_cost ; public : Derived ( double cost = 0.0 , int id = 0 ) : Base { id } // Call Base(int) constructor with value id! , m_cost { cost } { } double getCost () const { return m_cost ; } }; int main () { Derived derived { 1.3 , 5 }; // use Derived(double, int) constructor std :: cout << \"Id: \" << derived . getId () << '\\n' ; std :: cout << \"Cost: \" << derived . getCost () << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06 m_id \u548c m_cost \u8bbe\u4e3a\u79c1\u6709\u3002\u8fd9\u4e48\u505a\u5b8c\u5168\u6ca1\u6709\u95ee\u9898\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u76f8\u5173\u7684\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u8fd9\u4e9b\u6210\u5458\uff0c\u5e76\u901a\u8fc7\u516c\u6709\u7684 \u6210\u5458\u8bbf\u95ee\u51fd\u6570 \u6765\u8bbf\u95ee\u5b83\u4eec\u3002 \u6253\u5370\u7ed3\u679c\u5982\u6211\u4eec\u6240\u60f3\u7684\u90a3\u6837\uff1a 1 2 Id: 5 Cost: 1.3 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u66f4\u591a\u5730\u8ba8\u8bba\u8bbf\u95ee\u8bf4\u660e\u7b26\u3002","title":"\u5c06\u6210\u5458\u8bbe\u4e3a\u79c1\u6709"},{"location":"17-4-constructors-and-initialization-of-derived-classes/#_3","text":"\u518d\u770b\u770b\u4e4b\u524d\u6211\u4eec\u4f7f\u7528\u8fc7\u7684\u4e00\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include #include class Person { public : std :: string m_name ; int m_age {}; Person ( const std :: string_view name = \"\" , int age = 0 ) : m_name { name }, m_age { age } { } const std :: string & getName () const { return m_name ; } int getAge () const { return m_age ; } }; // BaseballPlayer publicly inheriting Person class BaseballPlayer : public Person { public : double m_battingAverage {}; int m_homeRuns {}; BaseballPlayer ( double battingAverage = 0.0 , int homeRuns = 0 ) : m_battingAverage { battingAverage }, m_homeRuns { homeRuns } { } }; \u5728\u4e4b\u524d\u7684\u4f8b\u5b50\u4e2d\uff0c BaseballPlayer \u53ea\u4f1a\u521d\u59cb\u5316\u5b83\u81ea\u5df1\u7684\u6210\u5458\uff0c\u4e5f\u5e76\u6ca1\u6709\u6307\u5b9a Person \u7684\u6784\u9020\u51fd\u6570\u3002\u8fd9\u610f\u5473\u7740 BaseballPlayer \u5728\u521b\u5efa\u65f6\uff0c\u8c03\u7528\u7684\u90fd\u662f Person \u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u5b83\u4f1a\u5c06\u540d\u5b57\u521d\u59cb\u5316\u4e3a\u7a7a\u767d\u5e76\u5c06\u5e74\u9f84\u521d\u59cb\u5316\u4e3a0\u3002\u56e0\u4e3a\uff0c\u56e0\u4e3a\u5728\u521b\u5efa BaseballPlayer \u65f6\u7ed9\u5b83\u4eec\u4e00\u4e2a\u540d\u79f0\u548c\u5e74\u9f84\u662f\u6709\u610f\u4e49\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u5e94\u8be5\u4fee\u6539\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u6765\u6dfb\u52a0\u8fd9\u4e9b\u53c2\u6570\u3002 \u66f4\u65b0\u4ee3\u7801\uff0c\u8ba9\u7c7b\u4f7f\u7528\u79c1\u6709\u6210\u5458\uff0c\u540c\u65f6\u8ba9 BaseballPlayer \u7c7b\u8c03\u7528\u9002\u5f53\u7684 Person \u6784\u9020\u51fd\u6570\u6765\u521d\u59cb\u5316\u7ee7\u627f\u7684 Person \u6210\u5458\u53d8\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include #include class Person { private : std :: string m_name ; int m_age {}; public : Person ( const std :: string_view name = \"\" , int age = 0 ) : m_name { name }, m_age { age } { } const std :: string & getName () const { return m_name ; } int getAge () const { return m_age ; } }; // BaseballPlayer \u516c\u5f00\u7ee7\u627f Person class BaseballPlayer : public Person { private : double m_battingAverage {}; int m_homeRuns {}; public : BaseballPlayer ( const std :: string_view name = \"\" , int age = 0 , double battingAverage = 0.0 , int homeRuns = 0 ) : Person { name , age } // \u8c03\u7528 Person(const std::string_view, int) \u6765\u521d\u59cb\u5316\u76f8\u5e94\u7684\u6210\u5458 , m_battingAverage { battingAverage }, m_homeRuns { homeRuns } { } double getBattingAverage () const { return m_battingAverage ; } int getHomeRuns () const { return m_homeRuns ; } }; \u73b0\u5728\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { BaseballPlayer pedro { \"Pedro Cerrano\" , 32 , 0.342 , 42 }; std :: cout << pedro . getName () << '\\n' ; std :: cout << pedro . getAge () << '\\n' ; std :: cout << pedro . getBattingAverage () << '\\n' ; std :: cout << pedro . getHomeRuns () << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 2 3 4 Pedro Cerrano 32 0.342 42 \u53ef\u4ee5\u770b\u5230\uff0c\u57fa\u7c7b\u7684\u540d\u79f0\u548c\u5e74\u9f84\u5df2\u6b63\u786e\u521d\u59cb\u5316\uff0c\u6d3e\u751f\u7c7b\u7684\u672c\u5792\u6253\u6570\u548c\u51fb\u7403\u7387\u4e5f\u5df2\u521d\u59cb\u5316\u3002","title":"\u53e6\u5916\u4e00\u4e2a\u4f8b\u5b50"},{"location":"17-4-constructors-and-initialization-of-derived-classes/#_4","text":"\u7ee7\u627f\u94fe\u4e2d\u7684\u7c7b\u4ee5\u5b8c\u5168\u76f8\u540c\u7684\u65b9\u5f0f\u5de5\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include class A { public : A ( int a ) { std :: cout << \"A: \" << a << '\\n' ; } }; class B : public A { public : B ( int a , double b ) : A { a } { std :: cout << \"B: \" << b << '\\n' ; } }; class C : public B { public : C ( int a , double b , char c ) : B { a , b } { std :: cout << \"C: \" << c << '\\n' ; } }; int main () { C c { 5 , 4.3 , 'R' }; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7c7bC\u662f\u7531\u7c7bB\u6d3e\u751f\u800c\u6765\u7684\uff0c\u800c\u7c7bB\u53c8\u662f\u7531\u7c7bA\u6d3e\u751f\u800c\u6765\u7684\u3002\u90a3\u4e48\u5f53\u6211\u4eec\u5b9e\u4f8b\u5316\u7c7bC\u7684\u5bf9\u8c61\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462? \u9996\u5148 main() \u4f1a\u8c03\u7528 C(int, double, char) \u3002C \u7684\u6784\u9020\u51fd\u6570\u4f1a\u8c03\u7528 B(int, double) \u3002B\u7684\u6784\u9020\u51fd\u6570\u8c03\u7528 A(int) \u3002\u56e0\u4e3aA\u6ca1\u6709\u7ee7\u627f\u4efb\u4f55\u7c7b\uff0c\u6240\u4ee5\u5b83\u4f1a\u88ab\u7b2c\u4e00\u4e2a\u6784\u9020\u3002A \u88ab\u6784\u9020\u540e\u6253\u5370\u4e86 5\uff0c\u7136\u540e\u63a7\u5236\u6743\u8fd4\u56de\u7ed9B\u3002B\u88ab\u6784\u9020\u65f6\u6253\u5370\u503c 4.3\uff0c\u7136\u540e\u8fd4\u56de\u63a7\u5236\u6743\u7ed9C\u3002C\u88ab\u6784\u9020\u65f6\u6253\u5370\u2018R\u2019\uff0c\u7136\u540e\u5c06\u63a7\u5236\u6743\u8fd4\u56de\u7ed9 main() \u3002\u6784\u9020\u7ed3\u675f\uff01 \u56e0\u6b64\u7a0b\u5e8f\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 A: 5 B: 4.3 C: R \u503c\u5f97\u4e00\u63d0\u7684\u662f\uff0c\u6784\u9020\u51fd\u6570\u53ea\u80fd\u8c03\u7528 \u76f4\u63a5 \u7236\u7c7b\uff08\u57fa\u7c7b\uff09\u4e2d\u7684\u6784\u9020\u51fd\u6570\u3002\u56e0\u6b64\uff0cC\u6784\u9020\u51fd\u6570\u4e0d\u80fd\u76f4\u63a5\u8c03\u7528\u6216\u5c06\u53c2\u6570\u4f20\u9012\u7ed9A\u6784\u9020\u51fd\u6570\u3002C\u6784\u9020\u51fd\u6570\u53ea\u80fd\u8c03\u7528B\u6784\u9020\u51fd\u6570(B\u6784\u9020\u51fd\u6570\u8d1f\u8d23\u8c03\u7528A\u6784\u9020\u51fd\u6570)\u3002","title":"\u7ee7\u627f\u94fe"},{"location":"17-4-constructors-and-initialization-of-derived-classes/#_5","text":"\u5f53\u6d3e\u751f\u7c7b\u88ab\u9500\u6bc1\u65f6\uff0c\u6bcf\u4e2a\u6790\u6784\u51fd\u6570\u5c06\u6309\u6784\u9020\u7684 \u9006\u987a\u5e8f\u8c03\u7528 \u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53C\u88ab\u9500\u6bc1\u65f6\uff0c\u9996\u5148\u8c03\u7528C\u6790\u6784\u51fd\u6570\uff0c\u7136\u540e\u662fB\u6790\u6784\u51fd\u6570\uff0c\u7136\u540e\u662fA\u6790\u6784\u51fd\u6570\u3002","title":"\u6790\u6784\u51fd\u6570"},{"location":"17-4-constructors-and-initialization-of-derived-classes/#_6","text":"\u5728\u6784\u9020\u6d3e\u751f\u7c7b\u65f6\uff0c\u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u8d1f\u8d23\u786e\u5b9a\u8c03\u7528\u54ea\u4e2a\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u57fa\u7c7b\u6784\u9020\u51fd\u6570\uff0c\u5c06\u4f7f\u7528\u9ed8\u8ba4\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u627e\u4e0d\u5230\u7f3a\u7701\u57fa\u7c7b\u6784\u9020\u51fd\u6570(\u6216\u7f3a\u7701\u521b\u5efa\u57fa\u7c7b\u6784\u9020\u51fd\u6570)\uff0c\u7f16\u8bd1\u5668\u5c06\u62a5\u9519\u3002\u7136\u540e\u6309\u7167\u4ece\u6700\u57fa\u7c7b\u5230\u6700\u6d3e\u751f\u7c7b\u7684\u987a\u5e8f\u6784\u9020\u7c7b\u3002 \u81f3\u6b64\uff0c\u60a8\u5df2\u7ecf\u8db3\u591f\u4e86\u89e3C++\u7ee7\u627f\uff0c\u53ef\u4ee5\u521b\u5efa\u81ea\u5df1\u7684\u7ee7\u627f\u7c7b\u4e86!","title":"\u5c0f\u7ed3"},{"location":"17-5-inheritance-and-access-specifiers/","tags":["inheritance","access-specifiers"],"text":"Key Takeaway \u6d3e\u751f\u7c7b\u4ece\u57fa\u7c7b\u7ee7\u627f\u4e86\u4e00\u4e9b\u5c5e\u6027\uff0c\u4ece\u5916\u90e8\u6765\u770b\uff0c\u8fd9\u4e9b\u7ee7\u627f\u6765\u7684\u5c5e\u6027\u5c31\u597d\u50cf\u662f\u6d3e\u751f\u7c7b\u7684\u4e00\u6837\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u3002\u5916\u90e8\u4ee3\u7801\u80fd\u5426\u901a\u8fc7\u6d3e\u751f\u7c7b\u8bbf\u95ee\u8fd9\u4e9b\u7ee7\u627f\u6765\u7684\u5c5e\u6027\u65f6\uff0c\u9700\u8981\u53c2\u8003\u7ee7\u627f\u7c7b\u578b\u4f7f\u7528\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u3002\uff08\u964d\u7ea7\u4e3a\u7ee7\u627f\u7b49\u7ea7\uff09 \u6d3e\u751f\u7c7b\u5bf9\u57fa\u7c7b\u6210\u5458\u7684\u8bbf\u95ee\u4e0d\u53d7\u7ee7\u627f\u7c7b\u578b\u5f71\u54cd\uff0c\u53ea\u53c2\u8003\u57fa\u7c7b\u7684\u8bbf\u95ee\u4fee\u9970\u7b26 \u5728\u672c\u7ae0\u524d\u9762\u7684\u8bfe\u7a0b\u4e2d\uff0c\u60a8\u5df2\u7ecf\u4e86\u89e3\u4e86\u57fa\u7ee7\u627f\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u5728\u6211\u4eec\u6240\u6709\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u90fd\u4f7f\u7528\u4e86\u516c\u5f00\u7ee7\u627f\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u7684\u6d3e\u751f\u7c7b\u516c\u5f00\u7ee7\u627f\u57fa\u7c7b\u3002 \u5728\u8fd9\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u4e86\u89e3\u516c\u5f00\u7ee7\u627f\uff0c\u4ee5\u53ca\u53e6\u5916\u4e24\u79cd\u7c7b\u578b\u7684\u7ee7\u627f(\u79c1\u6709\u7684\u548c\u53d7\u4fdd\u62a4\u7684)\u3002\u6211\u4eec\u8fd8\u5c06\u63a2\u8ba8\u4e0d\u540c\u7c7b\u578b\u7684\u7ee7\u627f\u5982\u4f55\u4e0e \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \uff0c\u4ee5\u5141\u8bb8\u6216\u9650\u5236\u5bf9\u6210\u5458\u7684\u8bbf\u95ee\u3002 \u81f3\u6b64\uff0c\u4f60\u5df2\u7ecf\u770b\u5230\u4e86\u79c1\u6709\u8bbf\u95ee\u8bf4\u660e\u7b26\u548c\u516c\u5171\u8bbf\u95ee\u8bf4\u660e\u7b26\uff0c\u5b83\u4eec\u51b3\u5b9a\u8c01\u53ef\u4ee5\u8bbf\u95ee\u7c7b\u7684\u6210\u5458\u3002\u5feb\u901f\u590d\u4e60\u4e00\u4e0b\uff0c \u516c\u6709\u6210\u5458 \u53ef\u4ee5\u88ab\u4efb\u4f55\u4eba\u8bbf\u95ee\u3002 \u79c1\u6709\u6210\u5458 \u53ea\u80fd\u7531\u540c\u4e00\u7c7b\u6216 \u53cb\u5143\u7c7b \u7684\u6210\u5458\u51fd\u6570\u8bbf\u95ee\u3002\u8fd9\u610f\u5473\u7740\u6d3e\u751f\u7c7b\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u57fa\u7c7b\u7684\u79c1\u6709\u6210\u5458\uff01 1 2 3 4 5 6 7 class Base { private : int m_private {}; // can only be accessed by Base members and friends (not derived classes) public : int m_public {}; // can be accessed by anybody }; \u975e\u5e38\u7b80\u5355\u5427\uff0c\u4f60\u5e94\u8be5\u5df2\u7ecf\u80fd\u591f\u5f88\u597d\u5730\u638c\u63e1\u5e76\u4f7f\u7528\u8fd9\u4e9b\u77e5\u8bc6\u4e86\u3002 protected \u8bbf\u95ee\u8bf4\u660e\u7b26 \u5f53\u914d\u5408\u7ee7\u627f\u7c7b\u5de5\u4f5c\u65f6\uff0c\u95ee\u9898\u53d8\u5f97\u590d\u6742\u4e86\u4e00\u4e9b\u3002 C++ \u63d0\u4f9b\u4e86\u7b2c\u4e09\u79cd\u8bbf\u95ee\u8bf4\u660e\u7b26\uff0c\u5b83\u53ea\u5728\u4f7f\u7528\u7ee7\u627f\u7684\u573a\u5408\u4e2d\u6709\u7528\u9014\u3002 protected \u8bbf\u95ee\u8bf4\u660e\u7b26\u5141\u8bb8\u6210\u5458\u88ab\u7c7b\u672c\u8eab\u3001\u53cb\u5143\u7c7b\u548c\u6d3e\u751f\u7c7b\u8bbf\u95ee\u3002\u4f46\u662f\uff0c \u53d7\u4fdd\u62a4\u6210\u5458 \u4e0d\u80fd\u88ab\u5916\u90e8\u7c7b\u8bbf\u95ee\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 class Base { public : int m_public {}; // \u53ef\u4ee5\u88ab\u4efb\u4f55\u4eba\u8bbf\u95ee protected : int m_protected {}; // \u53ef\u4ee5\u88ab Base\u3001\u53cb\u5143\u7c7b\u548c\u6d3e\u751f\u7c7b\u8bbf\u95ee private : int m_private {}; // \u53ea\u80fd\u88ab Base \u548c\u53cb\u5143\u8bbf\u95ee\uff08\u4e0d\u80fd\u88ab\u6d3e\u751f\u7c7b\u76f4\u63a5\u8bbf\u95ee\uff09 }; class Derived : public Base { public : Derived () { m_public = 1 ; // \u5141\u8bb8: \u53ef\u4ee5\u5728\u6d3e\u751f\u7c7b\u4e2d\u8bbf\u95ee\u57fa\u7c7b\u7684\u516c\u6709\u6210\u5458 m_protected = 2 ; // \u5141\u8bb8: \u53ef\u4ee5\u5728\u6d3e\u751f\u7c7b\u4e2d\u8bbf\u95ee\u57fa\u7c7b\u4e2d\u7684\u53d7\u4fdd\u62a4\u6210\u5458 m_private = 3 ; // \u4e0d\u5141\u8bb8\uff1a\u4e0d\u80fd\u5728\u6d3e\u751f\u7c7b\u4e2d\u8bbf\u95ee\u57fa\u7c7b\u7684\u79c1\u6709\u6210\u5458 } }; int main () { Base base ; base . m_public = 1 ; // \u5141\u8bb8: \u53ef\u4ee5\u4ece\u5916\u90e8\u7c7b\u8bbf\u95ee\u516c\u6709\u6210\u5458 base . m_protected = 2 ; // \u4e0d\u5141\u8bb8: \u4e0d\u80fd\u591f\u4ece\u5916\u90e8\u7c7b\u8bbf\u95ee\u516c\u6709\u6210\u5458 base . m_private = 3 ; // \u4e0d\u5141\u8bb8\uff1a\u4e0d\u80fd\u591f\u4ece\u5916\u90e8\u8bbf\u95ee\u79c1\u6709\u6210\u5458 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u53d7\u4fdd\u62a4\u7684\u57fa\u7c7b\u7684\u6210\u5458 m_protected \u53ef\u4ee5\u76f4\u63a5\u7531\u6d3e\u751f\u7c7b\u8bbf\u95ee\uff0c\u4f46\u4e0d\u80fd\u5728\u5916\u90e8\u88ab\u8bbf\u95ee\u3002 \u90a3\u4e48\uff0c\u5e94\u8be5\u5728\u4f55\u65f6\u4f7f\u7528 protected \u8bbf\u95ee\u8bf4\u660e\u7b26\uff1f \u57fa\u7c7b\u4e2d\u7684 \u53d7\u4fdd\u62a4\u6210\u5458 \u53ef\u4ee5\u76f4\u63a5\u88ab\u6d3e\u751f\u7c7b\u8bbf\u95ee\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u4f60\u4ee5\u540e\u66f4\u6539\u4e86\u53d7\u4fdd\u62a4\u5c5e\u6027\u7684\u4efb\u4f55\u5185\u5bb9(\u7c7b\u578b\u3001\u503c\u7684\u542b\u4e49\u7b49\u7b49)\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u540c\u65f6\u66f4\u6539\u57fa\u7c7b\u548c\u6240\u6709\u6d3e\u751f\u7c7b\u3002 \u56e0\u6b64\uff0c\u53d7\u4fdd\u62a4\u8bbf\u95ee\u8bf4\u660e\u7b26\u9002\u7528\u4e8e\u4f60\u4ece\u81ea\u5df1\u7f16\u5199\u7c7b\u4e2d\u7ee7\u627f\u5e76\u4e14\u6d3e\u751f\u7c7b\u6570\u91cf\u5e76\u4e0d\u591a\u7684\u60c5\u51b5\u3002\u8fd9\u6837\uff0c\u5982\u679c\u4f60\u5bf9\u57fa\u7c7b\u7684\u5b9e\u73b0\u8fdb\u884c\u4e86\u66f4\u6539\uff0c\u5e76\u4e14\u56e0\u6b64\u9700\u8981\u5bf9\u6d3e\u751f\u7c7b\u8fdb\u884c\u66f4\u65b0\uff0c\u90a3\u4e48\u53ef\u4ee5\u81ea\u5df1\u8fdb\u884c\u66f4\u65b0(\u5e76\u4e14\u4e0d\u4f1a\u82b1\u8d39\u592a\u957f\u65f6\u95f4\uff0c\u56e0\u4e3a\u6d3e\u751f\u7c7b\u7684\u6570\u91cf\u662f\u6709\u9650\u7684)\u3002 \u5c06\u6210\u5458\u8bbe\u4e3a\u79c1\u6709\u610f\u5473\u7740\u516c\u5171\u7c7b\u548c\u6d3e\u751f\u7c7b\u4e0d\u80fd\u76f4\u63a5\u5bf9\u57fa\u7c7b\u8fdb\u884c\u66f4\u6539\u3002\u8fd9\u6709\u5229\u4e8e\u5c06\u516c\u5171\u7c7b\u6216\u6d3e\u751f\u7c7b\u4e0e\u5b9e\u73b0\u66f4\u6539\u9694\u79bb\u5f00\u6765\uff0c\u5e76\u786e\u4fdd\u9002\u5f53\u5730\u7ef4\u62a4\u4e0d\u53d8\u91cf\u3002\u4f46\u662f\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u4f60\u7684\u7c7b\u53ef\u80fd\u9700\u8981\u4e00\u4e2a\u66f4\u5927\u7684\u516c\u5f00(\u6216\u53d7\u4fdd\u62a4\u7684)\u63a5\u53e3\u6765\u652f\u6301\u516c\u5171\u6216\u6d3e\u751f\u7c7b\u64cd\u4f5c\u6240\u9700\u7684\u6240\u6709\u529f\u80fd\uff0c\u8be5\u63a5\u53e3\u6709\u5176\u81ea\u8eab\u7684\u6784\u5efa\u3001\u6d4b\u8bd5\u548c\u7ef4\u62a4\u6210\u672c\u3002 \u4e00\u822c\u6765\u8bf4\uff0c\u5982\u679c\u53ef\u4ee5\u7684\u8bdd\uff0c\u6700\u597d\u5c06\u6210\u5458\u8bbe\u4e3a\u79c1\u6709\u7684\uff0c\u53ea\u6709\u51c6\u5907\u521b\u5efa\u6d3e\u751f\u7c7b\u4e14\u6784\u5efa\u548c\u7ef4\u62a4\u5230\u90a3\u4e9b\u79c1\u6709\u6210\u5458\u7684\u63a5\u53e3\u7684\u6210\u672c\u8fc7\u9ad8\u65f6\u624d\u4f7f\u7528\u53d7 protected \u8bbf\u95ee\u8bf4\u660e\u7b26\u3002 \u6269\u5c55\u9605\u8bfb \u4f18\u5148\u4f7f\u7528\u79c1\u6709\u6210\u5458\u800c\u4e0d\u662f\u53d7\u4fdd\u62a4\u6210\u5458\u3002 \u4e0d\u540c\u7c7b\u578b\u7684\u7ee7\u627f\u4ee5\u53ca\u5b83\u4eec\u5bf9\u6210\u5458\u8bbf\u95ee\u7684\u5f71\u54cd \u9996\u5148\uff0c\u7ee7\u627f\u7c7b\u578b\u6709\u4e09\u79cd\uff1a \u516c\u5f00\u7ee7\u627f \u3001 \u53d7\u4fdd\u62a4\u7ee7\u627f \u548c \u79c1\u6709\u7ee7\u627f \u3002 \u53ea\u9700\u5728\u7ee7\u627f\u7c7b\u65f6\u6307\u5b9a\u9700\u8981\u7684\u8bbf\u95ee\u7c7b\u578b\u5c31\u53ef\u4ee5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // Inherit from Base publicly class Pub : public Base { }; // Inherit from Base protectedly class Pro : protected Base { }; // Inherit from Base privately class Pri : private Base { }; class Def : Base // Defaults to private inheritance { }; \u5982\u679c\u4e0d\u9009\u62e9\u7ee7\u627f\u7c7b\u578b\uff0cC++\u9ed8\u8ba4\u4e3a\u79c1\u6709\u7ee7\u627f(\u5c31\u50cf\u5982\u679c\u4e0d\u6307\u5b9a\u5176\u4ed6\u7c7b\u578b\uff0c\u6210\u5458\u9ed8\u8ba4\u4e3a\u79c1\u6709\u8bbf\u95ee\u4e00\u6837)\u3002 \u8fd9\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e869\u79cd\u7ec4\u5408\uff1a3\u4e2a \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 (\u516c\u5171\u3001\u79c1\u6709\u548c\u53d7\u4fdd\u62a4)\u548c3\u79cd\u7ee7\u627f\u7c7b\u578b(\u516c\u5171\u3001\u79c1\u6709\u548c\u53d7\u4fdd\u62a4)\u3002 \u90a3\u4e48\u5b83\u4eec\u7684\u533a\u522b\u662f\u4ec0\u4e48\u5462\uff1f\u7b80\u800c\u8a00\u4e4b\uff0c\u5f53\u6210\u5458\u88ab\u7ee7\u627f\u65f6\uff0c\u7ee7\u627f\u6210\u5458\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u53ef\u4ee5\u6839\u636e\u6240\u4f7f\u7528\u7684\u7ee7\u627f\u7c7b\u578b\u66f4\u6539(\u4ec5\u5728\u6d3e\u751f\u7c7b\u4e2d)\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u57fa\u7c7b\u4e2d\u516c\u5171\u7684\u6216\u53d7\u4fdd\u62a4\u7684\u6210\u5458\uff0c\u5728\u6d3e\u751f\u7c7b\u4e2d\u53ef\u80fd\u662f\u5176\u4ed6\u7c7b\u578b\u7684\u3002 \u8fd9\u53ef\u80fd\u770b\u8d77\u6765\u6709\u70b9\u4ee4\u4eba\u56f0\u60d1\uff0c\u4e0d\u8fc7\u4e0d\u8981\u62c5\u5fc3\uff0c\u8fd9\u8282\u8bfe\u5269\u4e0b\u7684\u65f6\u95f4\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e2a\u95ee\u9898\u3002 \u5728\u6211\u4eec\u9010\u6b65\u5b66\u4e60\u8fd9\u4e9b\u4f8b\u5b50\u65f6\uff0c\u8bf7\u8bb0\u4f4f\u4ee5\u4e0b\u89c4\u5219: \u4e00\u4e2a\u7c7b\u603b\u662f\u80fd\u8bbf\u95ee\u5176\u81ea\u5df1\u7684\u6210\u5458\uff08\u975e\u7ee7\u627f\u800c\u6765\u7684\uff09\uff1b \u5916\u90e8\u5728\u8bbf\u95ee\u4e00\u4e2a\u7c7b\u7684\u6210\u5458\u65f6\uff0c\u57fa\u4e8e\u8be5\u6210\u5458\u7684\u8bbf\u95ee\u4fee\u9970\u7b26\u5224\u65ad\u662f\u5426\u80fd\u8bbf\u95ee\uff1b \u6d3e\u751f\u7c7b\u5728\u8bbf\u95ee\u7ee7\u627f\u6765\u7684\u6210\u5458\u65f6\uff0c\u57fa\u4e8e\u5b83\u4ece\u57fa\u7c7b\u7ee7\u627f\u6765\u7684\u8bbf\u95ee\u4fee\u9970\u7b26\u3002\u8fd9\u4e00\u70b9\u4f1a\u6839\u636e\u8bbf\u95ee\u8bf4\u660e\u7b26\u7684\u4e0d\u540c\u548c\u7ee7\u627f\u7c7b\u578b\u7684\u4e0d\u540c\u800c\u6539\u53d8\u3002 \u516c\u5f00\u7ee7\u627f \u516c\u5f00\u7ee7\u627f \u662f\u6700\u5e38\u7528\u7684\u7ee7\u627f\u7c7b\u578b\u3002\u4e8b\u5b9e\u4e0a\uff0c\u4f60\u5f88\u5c11\u4f1a\u770b\u5230\u6216\u4f7f\u7528\u5176\u4ed6\u7c7b\u578b\u7684\u7ee7\u627f\uff0c\u56e0\u6b64\u4f60\u7684\u4e3b\u8981\u5173\u6ce8\u70b9\u5e94\u8be5\u653e\u5728\u7406\u89e3\u8fd9\u4e00\u8282\u4e0a\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u516c\u5f00\u7ee7\u627f\u4e5f\u662f\u6700\u5bb9\u6613\u7406\u89e3\u7684\u3002\u5f53\u516c\u5f00\u7ee7\u627f\u57fa\u7c7b\u65f6\uff0c\u7ee7\u627f\u7684 \u516c\u6709\u6210\u5458 \u4fdd\u6301\u516c\u6709\uff0c\u7ee7\u627f\u7684 \u53d7\u4fdd\u62a4\u6210\u5458 \u4fdd\u6301\u53d7\u4fdd\u62a4\u3002\u7ee7\u627f\u7684\u79c1\u6709\u6210\u5458\u7531\u4e8e\u5728\u57fa\u7c7b\u4e2d\u662f\u79c1\u6709\u7684\u800c\u4e0d\u53ef\u8bbf\u95ee\uff0c\u56e0\u6b64\u4ecd\u7136\u4e0d\u53ef\u8bbf\u95ee\u3002 \u57fa\u7c7b\u4e2d\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u516c\u6709\u7ee7\u627f\u540e\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 Public Public Protected Protected Private Inaccessible \u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u6765\u8bf4\u660e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 class Base { public : int m_public {}; protected : int m_protected {}; private : int m_private {}; }; class Pub : public Base // \u6ce8\u610f: \u516c\u5f00\u7ee7\u627f { // \u516c\u6709\u7ee7\u627f\u610f\u5473\u7740: // \u516c\u6709\u7ee7\u627f\u7684\u6210\u5458\u4fdd\u6301\u516c\u6709 (m_public \u88ab\u5f53\u505a public \u7684) // \u516c\u6709\u7ee7\u627f\u6765\u7684\u53d7\u4fdd\u62a4\u6210\u5458\u4ecd\u7136\u662f\u53d7\u4fdd\u62a4\u7684( m_protected \u662f protected \u7684) // \u516c\u6709\u7ee7\u627f\u6765\u7684\u79c1\u6709\u6210\u5458\u662f\u4e0d\u53ef\u8bbf\u95ee\u7684( m_private \u4e0d\u53ef\u8bbf\u95ee) public : Pub () { m_public = 1 ; // okay: m_public \u53ef\u4ee5\u88ab\u8bbf\u95ee m_protected = 2 ; // okay: m_protected \u53ef\u4ee5\u88ab\u8bbf\u95ee m_private = 3 ; // not okay: m_private \u4e0d\u53ef\u4ee5\u88ab\u6d3e\u751f\u7c7b\u8bbf\u95ee } }; int main () { // \u5916\u90e8\u8bbf\u95ee\u65f6\u57fa\u4e8e\u88ab\u8bbf\u95ee\u7c7b\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u5224\u65ad Base base ; base . m_public = 1 ; // \u53ef\u4ee5: m_public \u5728 Base \u4e2d\u662f\u516c\u6709\u7684 base . m_protected = 2 ; //\u4e0d\u53ef\u4ee5 : m_protected \u5728 Base \u4e2d\u662f\u53d7\u4fdd\u62a4\u7684 base . m_private = 3 ; // \u4e0d\u53ef\u4ee5: m_private \u5728 Base \u4e2d\u662f\u79c1\u6709\u7684 Pub pub ; pub . m_public = 1 ; // \u53ef\u4ee5: m_public \u5728 Pub \u4e2d\u662f\u516c\u6709\u7684 pub . m_protected = 2 ; // \u4e0d\u53ef\u4ee5 : m_protected \u5728 Pub \u4e2d\u662f\u53d7\u4fdd\u62a4\u7684 pub . m_private = 3 ; // \u4e0d\u53ef\u4ee5: m_private \u5728 Pub \u4e2d\u662f\u4e0d\u53ef\u4ee5\u8bbf\u95ee\u7684 return 0 ; } \u8fd9\u4e0e\u4e0a\u9762\u6211\u4eec\u4ecb\u7ecd\u53d7\u4fdd\u62a4\u8bbf\u95ee\u8bf4\u660e\u7b26\u7684\u793a\u4f8b\u76f8\u540c\uff0c\u4e0d\u540c\u7684\u662f\uff0c\u6211\u4eec\u4e5f\u5b9e\u4f8b\u5316\u4e86\u6d3e\u751f\u7c7b\uff0c\u53ea\u662f\u4e3a\u4e86\u8868\u660e\u4f7f\u7528\u516c\u6709\u7ee7\u627f\uff0c\u57fa\u7c7b\u548c\u6d3e\u751f\u7c7b\u7684\u5de5\u4f5c\u65b9\u5f0f\u662f\u76f8\u540c\u7684\u3002 \u9664\u975e\u6709\u7279\u5b9a\u7684\u539f\u56e0\uff0c\u5426\u5219\u4f60\u5e94\u8be5\u4f7f\u7528\u516c\u5171\u7ee7\u627f\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u516c\u6709\u7ee7\u627f\uff0c\u9664\u975e\u4f60\u6709\u7279\u6b8a\u7684\u7406\u7531\u4e0d\u8fd9\u6837\u505a\u3002 \u53d7\u4fdd\u62a4\u7ee7\u627f \u53d7\u4fdd\u62a4\u7684\u7ee7\u627f\u662f\u6700\u4e0d\u5e38\u89c1\u7684\u7ee7\u627f\u65b9\u6cd5\u3002\u9664\u4e86\u5728\u975e\u5e38\u7279\u6b8a\u7684\u60c5\u51b5\u4e0b\uff0c\u5b83\u51e0\u4e4e\u4ece\u4e0d\u88ab\u4f7f\u7528\u3002\u901a\u8fc7\u53d7\u4fdd\u62a4\u7684\u7ee7\u627f\uff0c\u516c\u5171\u6210\u5458\u548c\u53d7\u4fdd\u62a4\u6210\u5458\u6210\u4e3a\u53d7\u4fdd\u62a4\u6210\u5458\uff0c\u800c\u79c1\u6709\u6210\u5458\u4fdd\u6301\u4e0d\u53ef\u8bbf\u95ee\u72b6\u6001\u3002 \u56e0\u4e3a\u8fd9\u79cd\u5f62\u5f0f\u7684\u7ee7\u627f\u975e\u5e38\u7f55\u89c1\uff0c\u6211\u4eec\u5c06\u8df3\u8fc7\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u53ea\u4f7f\u7528\u4e00\u4e2a\u8868\u8fdb\u884c\u603b\u7ed3: \u57fa\u7c7b\u4e2d\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u53d7\u4fdd\u62a4\u7ee7\u627f\u540e\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 Public Protected Protected Protected Private Inaccessible \u79c1\u6709\u7ee7\u627f \u4f7f\u7528\u79c1\u6709\u7ee7\u627f\uff0c\u57fa\u7c7b\u7684\u6240\u6709\u6210\u5458\u90fd\u88ab\u7ee7\u627f\u4e3a\u79c1\u6709\u3002\u8fd9\u610f\u5473\u7740\u79c1\u6709\u6210\u5458\u662f\u4e0d\u53ef\u8bbf\u95ee\u7684\uff0c\u53d7\u4fdd\u62a4\u7684\u548c\u516c\u5171\u6210\u5458\u4e5f\u53d8\u6210\u79c1\u6709\u3002 \u6ce8\u610f\uff0c\u8fd9\u4e0d\u4f1a\u5f71\u54cd\u6d3e\u751f\u7c7b\u8bbf\u95ee\u4ece\u5176\u7236\u7c7b\u7ee7\u627f\u7684\u6210\u5458\u7684\u65b9\u5f0f\uff01\u5b83\u53ea\u5f71\u54cd\u8bd5\u56fe\u901a\u8fc7\u6d3e\u751f\u7c7b\u8bbf\u95ee\u8fd9\u4e9b\u6210\u5458\u7684\u4ee3\u7801\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 class Base { public : int m_public {}; protected : int m_protected {}; private : int m_private {}; }; class Pri : private Base // note: private inheritance { // Private inheritance means: // Public inherited members become private (so m_public is treated as private) // Protected inherited members become private (so m_protected is treated as private) // Private inherited members stay inaccessible (so m_private is inaccessible) public : Pri () { m_public = 1 ; // okay: m_public is now private in Pri m_protected = 2 ; // okay: m_protected is now private in Pri m_private = 3 ; // not okay: derived classes can't access private members in the base class } }; int main () { // Outside access uses the access specifiers of the class being accessed. // In this case, the access specifiers of base. Base base ; base . m_public = 1 ; // okay: m_public is public in Base base . m_protected = 2 ; // not okay: m_protected is protected in Base base . m_private = 3 ; // not okay: m_private is private in Base Pri pri ; pri . m_public = 1 ; // not okay: m_public is now private in Pri pri . m_protected = 2 ; // not okay: m_protected is now private in Pri pri . m_private = 3 ; // not okay: m_private is inaccessible in Pri return 0 ; } \u603b\u7ed3\u5982\u4e0b\uff1a \u57fa\u7c7b\u4e2d\u7684\u8bbf\u95ee\u4fee\u9970\u7b26 \u79c1\u6709\u7ee7\u627f\u540e\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 Public Private Protected Private Private Inaccessible \u5f53\u6d3e\u751f\u7c7b\u4e0e\u57fa\u7c7b\u6ca1\u6709\u660e\u663e\u7684\u5173\u7cfb\uff0c\u4f46\u5728\u5185\u90e8\u4f7f\u7528\u57fa\u7c7b\u5b9e\u73b0\u65f6\uff0c\u79c1\u6709\u7ee7\u627f\u53ef\u80fd\u5f88\u6709\u7528\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u4e0d\u5e0c\u671b\u57fa\u7c7b\u7684\u516c\u5171\u63a5\u53e3\u901a\u8fc7\u6d3e\u751f\u7c7b\u7684\u5bf9\u8c61\u516c\u5f00(\u5982\u679c\u516c\u5f00\u7ee7\u627f\u5c31\u4f1a\u8fd9\u6837)\u3002 \u4e0d\u8fc7\uff0c\u5728\u5b9e\u8df5\u4e2d\uff0c\u5f88\u5c11\u4f7f\u7528\u79c1\u6709\u7ee7\u627f\u3002 \u6700\u540e\u4e00\u4e2a\u4f8b\u5b50 1 2 3 4 5 6 7 8 9 class Base { public : int m_public {}; protected : int m_protected {}; private : int m_private {}; }; Base \u5728\u8bbf\u95ee\u81ea\u5df1\u7684\u6210\u5458\u65f6\uff0c\u6ca1\u6709\u4efb\u4f55\u9650\u5236\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5916\u90e8\u4ee3\u7801\u53ea\u80fd\u8bbf\u95ee m_public \u3002\u6d3e\u751f\u7c7b\u53ef\u4ee5\u8bbf\u95ee m_public \u548c m_protected \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 class D2 : private Base // note: private inheritance { // \u79c1\u6709\u7ee7\u627f\uff1a // \u7ee7\u627f\u6765\u7684\u516c\u6709\u6210\u5458\u53d8\u4e3a\u79c1\u6709 // \u7ee7\u627f\u6765\u7684\u53d7\u4fdd\u62a4\u6210\u5458\u53d8\u4e3a\u79c1\u6709 // \u7ee7\u627f\u6765\u7684\u516c\u6709\u6210\u5458\u4ecd\u7136\u662f\u4e0d\u53ef\u8bbf\u95ee\u7684 public : int m_public2 {}; protected : int m_protected2 {}; private : int m_private2 {}; }; D2 \u53ef\u4ee5\u4e0d\u53d7\u9650\u5236\u5730\u8bbf\u95ee\u81ea\u5df1\u7684\u6210\u5458\u3002 D2 \u53ef\u4ee5\u8bbf\u95ee Base \u7684 m_public \u548c m_protected \u6210\u5458\uff0c\u4f46\u4e0d\u80fd\u8bbf\u95ee m_private \u6210\u5458\u3002\u56e0\u4e3a D2 \u79c1\u6709\u5730\u7ee7\u627f\u4e86 Base \uff0c\u5f53\u901a\u8fc7 D2 \u8bbf\u95ee\u65f6\uff0c m_public \u548c m_protected \u73b0\u5728\u88ab\u8ba4\u4e3a\u662f\u79c1\u6709\u7684\u3002\u8fd9\u610f\u5473\u7740\u5728\u4f7f\u7528 D2 \u5bf9\u8c61\u65f6\uff0c\u5916\u90e8\u4ee3\u7801\u4e0d\u80fd\u8bbf\u95ee\u8fd9\u4e9b\u53d8\u91cf\uff0c \u4e5f\u4e0d\u80fd\u8bbf\u95ee\u4ece D2 \u6d3e\u751f\u7684\u4efb\u4f55\u7c7b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 class D3 : public D2 { // \u516c\u6709\u7ee7\u627f\uff1a // \u7ee7\u627f\u6765\u7684\u516c\u6709\u6210\u5458\u4ecd\u7136\u516c\u6709 // \u7ee7\u627f\u6765\u7684\u53d7\u4fdd\u62a4\u6210\u5458\u4ecd\u7136\u53d7\u4fdd\u62a4 // \u7ee7\u627f\u6765\u7684\u79c1\u6709\u6210\u5458\u4ecd\u7136\u4e0d\u53ef\u8bbf\u95ee public : int m_public3 {}; protected : int m_protected3 {}; private : int m_private3 {}; }; COPY D3 can access its own members without restriction. D3 can access D2\u2019s m_public2 and m_protected2 members, but not m_private2. Because D3 inherited D2 publicly, m_public2 and m_protected2 keep their access specifiers when accessed through D3. D3 has no access to Base\u2019s m_private, which was already private in Base. Nor does it have access to Base\u2019s m_protected or m_public, both of which became private when D2 inherited them. D3 \u53ef\u4ee5\u4e0d\u53d7\u9650\u5236\u5730\u8bbf\u95ee\u81ea\u5df1\u7684\u6210\u5458\u3002 D3 \u53ef\u4ee5\u8bbf\u95ee D2 \u7684 m_public2 \u548c m_protected2 \u6210\u5458\uff0c\u4f46\u4e0d\u80fd\u8bbf\u95ee m_private2 \u6210\u5458\u3002\u56e0\u4e3a D3 \u516c\u5f00\u7ee7\u627f\u4e86 D2 \uff0c\u6240\u4ee5\u5f53\u901a\u8fc7 D3 \u8bbf\u95ee\u65f6\uff0c m_public2 \u548c m_protected2 \u4fdd\u7559\u4e86\u5b83\u4eec\u7684\u8bbf\u95ee\u4fee\u9970\u7b26\u3002 D3 \u4e0d\u80fd\u8bbf\u95ee Base \u7684 m_private \uff0c\u5b83\u5728 Base \u4e2d\u5df2\u7ecf\u662f\u79c1\u6709\u7684\u3002\u5b83\u4e5f\u4e0d\u80fd\u8bbf\u95ee Base \u7684 m_protected \u6216 m_public \uff0c\u5f53 D2 \u7ee7\u627f\u5b83\u4eec\u65f6\uff0c\u5b83\u4eec\u90fd\u53d8\u6210\u4e86\u79c1\u6709\u3002 \u5c0f\u7ed3 \u8bbf\u95ee\u8bf4\u660e\u7b26\u3001\u7ee7\u627f\u7c7b\u578b\u548c\u6d3e\u751f\u7c7b\u7684\u4ea4\u4e92\u65b9\u5f0f\u4f1a\u5bfc\u81f4\u5f88\u591a\u6df7\u4e71\u3002\u5c3d\u91cf\u628a\u4e8b\u60c5\u5f04\u6e05\u695a: \u9996\u5148\uff0c\u7c7b(\u53ca\u5176\u53cb\u5143)\u603b\u662f\u53ef\u4ee5\u8bbf\u95ee\u81ea\u5df1\u7684\u975e\u7ee7\u627f\u6210\u5458\u3002\u8bbf\u95ee\u8bf4\u660e\u7b26\u53ea\u5f71\u54cd\u5916\u90e8\u548c\u6d3e\u751f\u7c7b\u662f\u5426\u53ef\u4ee5\u8bbf\u95ee\u8fd9\u4e9b\u6210\u5458\u3002 \u5176\u6b21\uff0c\u5f53\u6d3e\u751f\u7c7b\u7ee7\u627f\u6210\u5458\u65f6\uff0c\u8fd9\u4e9b\u6210\u5458\u53ef\u80fd\u4f1a\u66f4\u6539\u6d3e\u751f\u7c7b\u4e2d\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u3002\u8fd9\u4e0d\u4f1a\u5f71\u54cd\u6d3e\u751f\u7c7b\u81ea\u5df1\u7684(\u975e\u7ee7\u627f\u7684)\u6210\u5458(\u5b83\u4eec\u6709\u81ea\u5df1\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26)\u3002\u5b83\u53ea\u5f71\u54cd\u5916\u90e8\u4eba\u5458\u548c\u4ece\u6d3e\u751f\u7c7b\u6d3e\u751f\u7684\u7c7b\u662f\u5426\u53ef\u4ee5\u8bbf\u95ee\u90a3\u4e9b\u7ee7\u627f\u7684\u6210\u5458\u3002 \u4e0b\u9762\u662f\u6240\u6709\u8bbf\u95ee\u8bf4\u660e\u7b26\u548c\u7ee7\u627f\u7c7b\u578b\u7ec4\u5408\u7684\u8868 \u57fa\u7c7b\u4e2d\u7684\u8bbf\u95ee\u4fee\u9970\u7b26 \u516c\u6709\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u4fee\u9970\u7b26 \u79c1\u6709\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u4fee\u9970\u7b26 \u53d7\u4fdd\u62a4\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u4fee\u9970\u7b26 Public Public Private Protected Protected Protected Private Protected Private Inaccessible Inaccessible Inaccessible \u6700\u540e\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5c3d\u7ba1\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ea\u5c55\u793a\u4e86\u4f7f\u7528\u6210\u5458\u53d8\u91cf\u7684\u4f8b\u5b50\uff0c\u4f46\u8fd9\u4e9b\u8bbf\u95ee\u89c4\u5219\u5bf9\u6240\u6709\u6210\u5458\u90fd\u6210\u7acb(\u4f8b\u5982\uff0c\u5728\u7c7b\u5185\u90e8\u58f0\u660e\u7684\u6210\u5458\u51fd\u6570\u548c\u7c7b\u578b)\u3002","title":"17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26"},{"location":"17-5-inheritance-and-access-specifiers/#protected","text":"\u5f53\u914d\u5408\u7ee7\u627f\u7c7b\u5de5\u4f5c\u65f6\uff0c\u95ee\u9898\u53d8\u5f97\u590d\u6742\u4e86\u4e00\u4e9b\u3002 C++ \u63d0\u4f9b\u4e86\u7b2c\u4e09\u79cd\u8bbf\u95ee\u8bf4\u660e\u7b26\uff0c\u5b83\u53ea\u5728\u4f7f\u7528\u7ee7\u627f\u7684\u573a\u5408\u4e2d\u6709\u7528\u9014\u3002 protected \u8bbf\u95ee\u8bf4\u660e\u7b26\u5141\u8bb8\u6210\u5458\u88ab\u7c7b\u672c\u8eab\u3001\u53cb\u5143\u7c7b\u548c\u6d3e\u751f\u7c7b\u8bbf\u95ee\u3002\u4f46\u662f\uff0c \u53d7\u4fdd\u62a4\u6210\u5458 \u4e0d\u80fd\u88ab\u5916\u90e8\u7c7b\u8bbf\u95ee\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 class Base { public : int m_public {}; // \u53ef\u4ee5\u88ab\u4efb\u4f55\u4eba\u8bbf\u95ee protected : int m_protected {}; // \u53ef\u4ee5\u88ab Base\u3001\u53cb\u5143\u7c7b\u548c\u6d3e\u751f\u7c7b\u8bbf\u95ee private : int m_private {}; // \u53ea\u80fd\u88ab Base \u548c\u53cb\u5143\u8bbf\u95ee\uff08\u4e0d\u80fd\u88ab\u6d3e\u751f\u7c7b\u76f4\u63a5\u8bbf\u95ee\uff09 }; class Derived : public Base { public : Derived () { m_public = 1 ; // \u5141\u8bb8: \u53ef\u4ee5\u5728\u6d3e\u751f\u7c7b\u4e2d\u8bbf\u95ee\u57fa\u7c7b\u7684\u516c\u6709\u6210\u5458 m_protected = 2 ; // \u5141\u8bb8: \u53ef\u4ee5\u5728\u6d3e\u751f\u7c7b\u4e2d\u8bbf\u95ee\u57fa\u7c7b\u4e2d\u7684\u53d7\u4fdd\u62a4\u6210\u5458 m_private = 3 ; // \u4e0d\u5141\u8bb8\uff1a\u4e0d\u80fd\u5728\u6d3e\u751f\u7c7b\u4e2d\u8bbf\u95ee\u57fa\u7c7b\u7684\u79c1\u6709\u6210\u5458 } }; int main () { Base base ; base . m_public = 1 ; // \u5141\u8bb8: \u53ef\u4ee5\u4ece\u5916\u90e8\u7c7b\u8bbf\u95ee\u516c\u6709\u6210\u5458 base . m_protected = 2 ; // \u4e0d\u5141\u8bb8: \u4e0d\u80fd\u591f\u4ece\u5916\u90e8\u7c7b\u8bbf\u95ee\u516c\u6709\u6210\u5458 base . m_private = 3 ; // \u4e0d\u5141\u8bb8\uff1a\u4e0d\u80fd\u591f\u4ece\u5916\u90e8\u8bbf\u95ee\u79c1\u6709\u6210\u5458 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u53d7\u4fdd\u62a4\u7684\u57fa\u7c7b\u7684\u6210\u5458 m_protected \u53ef\u4ee5\u76f4\u63a5\u7531\u6d3e\u751f\u7c7b\u8bbf\u95ee\uff0c\u4f46\u4e0d\u80fd\u5728\u5916\u90e8\u88ab\u8bbf\u95ee\u3002","title":"protected \u8bbf\u95ee\u8bf4\u660e\u7b26"},{"location":"17-5-inheritance-and-access-specifiers/#protected_1","text":"\u57fa\u7c7b\u4e2d\u7684 \u53d7\u4fdd\u62a4\u6210\u5458 \u53ef\u4ee5\u76f4\u63a5\u88ab\u6d3e\u751f\u7c7b\u8bbf\u95ee\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u4f60\u4ee5\u540e\u66f4\u6539\u4e86\u53d7\u4fdd\u62a4\u5c5e\u6027\u7684\u4efb\u4f55\u5185\u5bb9(\u7c7b\u578b\u3001\u503c\u7684\u542b\u4e49\u7b49\u7b49)\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u540c\u65f6\u66f4\u6539\u57fa\u7c7b\u548c\u6240\u6709\u6d3e\u751f\u7c7b\u3002 \u56e0\u6b64\uff0c\u53d7\u4fdd\u62a4\u8bbf\u95ee\u8bf4\u660e\u7b26\u9002\u7528\u4e8e\u4f60\u4ece\u81ea\u5df1\u7f16\u5199\u7c7b\u4e2d\u7ee7\u627f\u5e76\u4e14\u6d3e\u751f\u7c7b\u6570\u91cf\u5e76\u4e0d\u591a\u7684\u60c5\u51b5\u3002\u8fd9\u6837\uff0c\u5982\u679c\u4f60\u5bf9\u57fa\u7c7b\u7684\u5b9e\u73b0\u8fdb\u884c\u4e86\u66f4\u6539\uff0c\u5e76\u4e14\u56e0\u6b64\u9700\u8981\u5bf9\u6d3e\u751f\u7c7b\u8fdb\u884c\u66f4\u65b0\uff0c\u90a3\u4e48\u53ef\u4ee5\u81ea\u5df1\u8fdb\u884c\u66f4\u65b0(\u5e76\u4e14\u4e0d\u4f1a\u82b1\u8d39\u592a\u957f\u65f6\u95f4\uff0c\u56e0\u4e3a\u6d3e\u751f\u7c7b\u7684\u6570\u91cf\u662f\u6709\u9650\u7684)\u3002 \u5c06\u6210\u5458\u8bbe\u4e3a\u79c1\u6709\u610f\u5473\u7740\u516c\u5171\u7c7b\u548c\u6d3e\u751f\u7c7b\u4e0d\u80fd\u76f4\u63a5\u5bf9\u57fa\u7c7b\u8fdb\u884c\u66f4\u6539\u3002\u8fd9\u6709\u5229\u4e8e\u5c06\u516c\u5171\u7c7b\u6216\u6d3e\u751f\u7c7b\u4e0e\u5b9e\u73b0\u66f4\u6539\u9694\u79bb\u5f00\u6765\uff0c\u5e76\u786e\u4fdd\u9002\u5f53\u5730\u7ef4\u62a4\u4e0d\u53d8\u91cf\u3002\u4f46\u662f\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u4f60\u7684\u7c7b\u53ef\u80fd\u9700\u8981\u4e00\u4e2a\u66f4\u5927\u7684\u516c\u5f00(\u6216\u53d7\u4fdd\u62a4\u7684)\u63a5\u53e3\u6765\u652f\u6301\u516c\u5171\u6216\u6d3e\u751f\u7c7b\u64cd\u4f5c\u6240\u9700\u7684\u6240\u6709\u529f\u80fd\uff0c\u8be5\u63a5\u53e3\u6709\u5176\u81ea\u8eab\u7684\u6784\u5efa\u3001\u6d4b\u8bd5\u548c\u7ef4\u62a4\u6210\u672c\u3002 \u4e00\u822c\u6765\u8bf4\uff0c\u5982\u679c\u53ef\u4ee5\u7684\u8bdd\uff0c\u6700\u597d\u5c06\u6210\u5458\u8bbe\u4e3a\u79c1\u6709\u7684\uff0c\u53ea\u6709\u51c6\u5907\u521b\u5efa\u6d3e\u751f\u7c7b\u4e14\u6784\u5efa\u548c\u7ef4\u62a4\u5230\u90a3\u4e9b\u79c1\u6709\u6210\u5458\u7684\u63a5\u53e3\u7684\u6210\u672c\u8fc7\u9ad8\u65f6\u624d\u4f7f\u7528\u53d7 protected \u8bbf\u95ee\u8bf4\u660e\u7b26\u3002 \u6269\u5c55\u9605\u8bfb \u4f18\u5148\u4f7f\u7528\u79c1\u6709\u6210\u5458\u800c\u4e0d\u662f\u53d7\u4fdd\u62a4\u6210\u5458\u3002","title":"\u90a3\u4e48\uff0c\u5e94\u8be5\u5728\u4f55\u65f6\u4f7f\u7528protected\u8bbf\u95ee\u8bf4\u660e\u7b26\uff1f"},{"location":"17-5-inheritance-and-access-specifiers/#_1","text":"\u9996\u5148\uff0c\u7ee7\u627f\u7c7b\u578b\u6709\u4e09\u79cd\uff1a \u516c\u5f00\u7ee7\u627f \u3001 \u53d7\u4fdd\u62a4\u7ee7\u627f \u548c \u79c1\u6709\u7ee7\u627f \u3002 \u53ea\u9700\u5728\u7ee7\u627f\u7c7b\u65f6\u6307\u5b9a\u9700\u8981\u7684\u8bbf\u95ee\u7c7b\u578b\u5c31\u53ef\u4ee5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // Inherit from Base publicly class Pub : public Base { }; // Inherit from Base protectedly class Pro : protected Base { }; // Inherit from Base privately class Pri : private Base { }; class Def : Base // Defaults to private inheritance { }; \u5982\u679c\u4e0d\u9009\u62e9\u7ee7\u627f\u7c7b\u578b\uff0cC++\u9ed8\u8ba4\u4e3a\u79c1\u6709\u7ee7\u627f(\u5c31\u50cf\u5982\u679c\u4e0d\u6307\u5b9a\u5176\u4ed6\u7c7b\u578b\uff0c\u6210\u5458\u9ed8\u8ba4\u4e3a\u79c1\u6709\u8bbf\u95ee\u4e00\u6837)\u3002 \u8fd9\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e869\u79cd\u7ec4\u5408\uff1a3\u4e2a \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 (\u516c\u5171\u3001\u79c1\u6709\u548c\u53d7\u4fdd\u62a4)\u548c3\u79cd\u7ee7\u627f\u7c7b\u578b(\u516c\u5171\u3001\u79c1\u6709\u548c\u53d7\u4fdd\u62a4)\u3002 \u90a3\u4e48\u5b83\u4eec\u7684\u533a\u522b\u662f\u4ec0\u4e48\u5462\uff1f\u7b80\u800c\u8a00\u4e4b\uff0c\u5f53\u6210\u5458\u88ab\u7ee7\u627f\u65f6\uff0c\u7ee7\u627f\u6210\u5458\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u53ef\u4ee5\u6839\u636e\u6240\u4f7f\u7528\u7684\u7ee7\u627f\u7c7b\u578b\u66f4\u6539(\u4ec5\u5728\u6d3e\u751f\u7c7b\u4e2d)\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u57fa\u7c7b\u4e2d\u516c\u5171\u7684\u6216\u53d7\u4fdd\u62a4\u7684\u6210\u5458\uff0c\u5728\u6d3e\u751f\u7c7b\u4e2d\u53ef\u80fd\u662f\u5176\u4ed6\u7c7b\u578b\u7684\u3002 \u8fd9\u53ef\u80fd\u770b\u8d77\u6765\u6709\u70b9\u4ee4\u4eba\u56f0\u60d1\uff0c\u4e0d\u8fc7\u4e0d\u8981\u62c5\u5fc3\uff0c\u8fd9\u8282\u8bfe\u5269\u4e0b\u7684\u65f6\u95f4\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e2a\u95ee\u9898\u3002 \u5728\u6211\u4eec\u9010\u6b65\u5b66\u4e60\u8fd9\u4e9b\u4f8b\u5b50\u65f6\uff0c\u8bf7\u8bb0\u4f4f\u4ee5\u4e0b\u89c4\u5219: \u4e00\u4e2a\u7c7b\u603b\u662f\u80fd\u8bbf\u95ee\u5176\u81ea\u5df1\u7684\u6210\u5458\uff08\u975e\u7ee7\u627f\u800c\u6765\u7684\uff09\uff1b \u5916\u90e8\u5728\u8bbf\u95ee\u4e00\u4e2a\u7c7b\u7684\u6210\u5458\u65f6\uff0c\u57fa\u4e8e\u8be5\u6210\u5458\u7684\u8bbf\u95ee\u4fee\u9970\u7b26\u5224\u65ad\u662f\u5426\u80fd\u8bbf\u95ee\uff1b \u6d3e\u751f\u7c7b\u5728\u8bbf\u95ee\u7ee7\u627f\u6765\u7684\u6210\u5458\u65f6\uff0c\u57fa\u4e8e\u5b83\u4ece\u57fa\u7c7b\u7ee7\u627f\u6765\u7684\u8bbf\u95ee\u4fee\u9970\u7b26\u3002\u8fd9\u4e00\u70b9\u4f1a\u6839\u636e\u8bbf\u95ee\u8bf4\u660e\u7b26\u7684\u4e0d\u540c\u548c\u7ee7\u627f\u7c7b\u578b\u7684\u4e0d\u540c\u800c\u6539\u53d8\u3002","title":"\u4e0d\u540c\u7c7b\u578b\u7684\u7ee7\u627f\u4ee5\u53ca\u5b83\u4eec\u5bf9\u6210\u5458\u8bbf\u95ee\u7684\u5f71\u54cd"},{"location":"17-5-inheritance-and-access-specifiers/#_2","text":"\u516c\u5f00\u7ee7\u627f \u662f\u6700\u5e38\u7528\u7684\u7ee7\u627f\u7c7b\u578b\u3002\u4e8b\u5b9e\u4e0a\uff0c\u4f60\u5f88\u5c11\u4f1a\u770b\u5230\u6216\u4f7f\u7528\u5176\u4ed6\u7c7b\u578b\u7684\u7ee7\u627f\uff0c\u56e0\u6b64\u4f60\u7684\u4e3b\u8981\u5173\u6ce8\u70b9\u5e94\u8be5\u653e\u5728\u7406\u89e3\u8fd9\u4e00\u8282\u4e0a\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u516c\u5f00\u7ee7\u627f\u4e5f\u662f\u6700\u5bb9\u6613\u7406\u89e3\u7684\u3002\u5f53\u516c\u5f00\u7ee7\u627f\u57fa\u7c7b\u65f6\uff0c\u7ee7\u627f\u7684 \u516c\u6709\u6210\u5458 \u4fdd\u6301\u516c\u6709\uff0c\u7ee7\u627f\u7684 \u53d7\u4fdd\u62a4\u6210\u5458 \u4fdd\u6301\u53d7\u4fdd\u62a4\u3002\u7ee7\u627f\u7684\u79c1\u6709\u6210\u5458\u7531\u4e8e\u5728\u57fa\u7c7b\u4e2d\u662f\u79c1\u6709\u7684\u800c\u4e0d\u53ef\u8bbf\u95ee\uff0c\u56e0\u6b64\u4ecd\u7136\u4e0d\u53ef\u8bbf\u95ee\u3002 \u57fa\u7c7b\u4e2d\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u516c\u6709\u7ee7\u627f\u540e\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 Public Public Protected Protected Private Inaccessible \u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u6765\u8bf4\u660e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 class Base { public : int m_public {}; protected : int m_protected {}; private : int m_private {}; }; class Pub : public Base // \u6ce8\u610f: \u516c\u5f00\u7ee7\u627f { // \u516c\u6709\u7ee7\u627f\u610f\u5473\u7740: // \u516c\u6709\u7ee7\u627f\u7684\u6210\u5458\u4fdd\u6301\u516c\u6709 (m_public \u88ab\u5f53\u505a public \u7684) // \u516c\u6709\u7ee7\u627f\u6765\u7684\u53d7\u4fdd\u62a4\u6210\u5458\u4ecd\u7136\u662f\u53d7\u4fdd\u62a4\u7684( m_protected \u662f protected \u7684) // \u516c\u6709\u7ee7\u627f\u6765\u7684\u79c1\u6709\u6210\u5458\u662f\u4e0d\u53ef\u8bbf\u95ee\u7684( m_private \u4e0d\u53ef\u8bbf\u95ee) public : Pub () { m_public = 1 ; // okay: m_public \u53ef\u4ee5\u88ab\u8bbf\u95ee m_protected = 2 ; // okay: m_protected \u53ef\u4ee5\u88ab\u8bbf\u95ee m_private = 3 ; // not okay: m_private \u4e0d\u53ef\u4ee5\u88ab\u6d3e\u751f\u7c7b\u8bbf\u95ee } }; int main () { // \u5916\u90e8\u8bbf\u95ee\u65f6\u57fa\u4e8e\u88ab\u8bbf\u95ee\u7c7b\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u5224\u65ad Base base ; base . m_public = 1 ; // \u53ef\u4ee5: m_public \u5728 Base \u4e2d\u662f\u516c\u6709\u7684 base . m_protected = 2 ; //\u4e0d\u53ef\u4ee5 : m_protected \u5728 Base \u4e2d\u662f\u53d7\u4fdd\u62a4\u7684 base . m_private = 3 ; // \u4e0d\u53ef\u4ee5: m_private \u5728 Base \u4e2d\u662f\u79c1\u6709\u7684 Pub pub ; pub . m_public = 1 ; // \u53ef\u4ee5: m_public \u5728 Pub \u4e2d\u662f\u516c\u6709\u7684 pub . m_protected = 2 ; // \u4e0d\u53ef\u4ee5 : m_protected \u5728 Pub \u4e2d\u662f\u53d7\u4fdd\u62a4\u7684 pub . m_private = 3 ; // \u4e0d\u53ef\u4ee5: m_private \u5728 Pub \u4e2d\u662f\u4e0d\u53ef\u4ee5\u8bbf\u95ee\u7684 return 0 ; } \u8fd9\u4e0e\u4e0a\u9762\u6211\u4eec\u4ecb\u7ecd\u53d7\u4fdd\u62a4\u8bbf\u95ee\u8bf4\u660e\u7b26\u7684\u793a\u4f8b\u76f8\u540c\uff0c\u4e0d\u540c\u7684\u662f\uff0c\u6211\u4eec\u4e5f\u5b9e\u4f8b\u5316\u4e86\u6d3e\u751f\u7c7b\uff0c\u53ea\u662f\u4e3a\u4e86\u8868\u660e\u4f7f\u7528\u516c\u6709\u7ee7\u627f\uff0c\u57fa\u7c7b\u548c\u6d3e\u751f\u7c7b\u7684\u5de5\u4f5c\u65b9\u5f0f\u662f\u76f8\u540c\u7684\u3002 \u9664\u975e\u6709\u7279\u5b9a\u7684\u539f\u56e0\uff0c\u5426\u5219\u4f60\u5e94\u8be5\u4f7f\u7528\u516c\u5171\u7ee7\u627f\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u516c\u6709\u7ee7\u627f\uff0c\u9664\u975e\u4f60\u6709\u7279\u6b8a\u7684\u7406\u7531\u4e0d\u8fd9\u6837\u505a\u3002","title":"\u516c\u5f00\u7ee7\u627f"},{"location":"17-5-inheritance-and-access-specifiers/#_3","text":"\u53d7\u4fdd\u62a4\u7684\u7ee7\u627f\u662f\u6700\u4e0d\u5e38\u89c1\u7684\u7ee7\u627f\u65b9\u6cd5\u3002\u9664\u4e86\u5728\u975e\u5e38\u7279\u6b8a\u7684\u60c5\u51b5\u4e0b\uff0c\u5b83\u51e0\u4e4e\u4ece\u4e0d\u88ab\u4f7f\u7528\u3002\u901a\u8fc7\u53d7\u4fdd\u62a4\u7684\u7ee7\u627f\uff0c\u516c\u5171\u6210\u5458\u548c\u53d7\u4fdd\u62a4\u6210\u5458\u6210\u4e3a\u53d7\u4fdd\u62a4\u6210\u5458\uff0c\u800c\u79c1\u6709\u6210\u5458\u4fdd\u6301\u4e0d\u53ef\u8bbf\u95ee\u72b6\u6001\u3002 \u56e0\u4e3a\u8fd9\u79cd\u5f62\u5f0f\u7684\u7ee7\u627f\u975e\u5e38\u7f55\u89c1\uff0c\u6211\u4eec\u5c06\u8df3\u8fc7\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u53ea\u4f7f\u7528\u4e00\u4e2a\u8868\u8fdb\u884c\u603b\u7ed3: \u57fa\u7c7b\u4e2d\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u53d7\u4fdd\u62a4\u7ee7\u627f\u540e\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 Public Protected Protected Protected Private Inaccessible","title":"\u53d7\u4fdd\u62a4\u7ee7\u627f"},{"location":"17-5-inheritance-and-access-specifiers/#_4","text":"\u4f7f\u7528\u79c1\u6709\u7ee7\u627f\uff0c\u57fa\u7c7b\u7684\u6240\u6709\u6210\u5458\u90fd\u88ab\u7ee7\u627f\u4e3a\u79c1\u6709\u3002\u8fd9\u610f\u5473\u7740\u79c1\u6709\u6210\u5458\u662f\u4e0d\u53ef\u8bbf\u95ee\u7684\uff0c\u53d7\u4fdd\u62a4\u7684\u548c\u516c\u5171\u6210\u5458\u4e5f\u53d8\u6210\u79c1\u6709\u3002 \u6ce8\u610f\uff0c\u8fd9\u4e0d\u4f1a\u5f71\u54cd\u6d3e\u751f\u7c7b\u8bbf\u95ee\u4ece\u5176\u7236\u7c7b\u7ee7\u627f\u7684\u6210\u5458\u7684\u65b9\u5f0f\uff01\u5b83\u53ea\u5f71\u54cd\u8bd5\u56fe\u901a\u8fc7\u6d3e\u751f\u7c7b\u8bbf\u95ee\u8fd9\u4e9b\u6210\u5458\u7684\u4ee3\u7801\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 class Base { public : int m_public {}; protected : int m_protected {}; private : int m_private {}; }; class Pri : private Base // note: private inheritance { // Private inheritance means: // Public inherited members become private (so m_public is treated as private) // Protected inherited members become private (so m_protected is treated as private) // Private inherited members stay inaccessible (so m_private is inaccessible) public : Pri () { m_public = 1 ; // okay: m_public is now private in Pri m_protected = 2 ; // okay: m_protected is now private in Pri m_private = 3 ; // not okay: derived classes can't access private members in the base class } }; int main () { // Outside access uses the access specifiers of the class being accessed. // In this case, the access specifiers of base. Base base ; base . m_public = 1 ; // okay: m_public is public in Base base . m_protected = 2 ; // not okay: m_protected is protected in Base base . m_private = 3 ; // not okay: m_private is private in Base Pri pri ; pri . m_public = 1 ; // not okay: m_public is now private in Pri pri . m_protected = 2 ; // not okay: m_protected is now private in Pri pri . m_private = 3 ; // not okay: m_private is inaccessible in Pri return 0 ; } \u603b\u7ed3\u5982\u4e0b\uff1a \u57fa\u7c7b\u4e2d\u7684\u8bbf\u95ee\u4fee\u9970\u7b26 \u79c1\u6709\u7ee7\u627f\u540e\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 Public Private Protected Private Private Inaccessible \u5f53\u6d3e\u751f\u7c7b\u4e0e\u57fa\u7c7b\u6ca1\u6709\u660e\u663e\u7684\u5173\u7cfb\uff0c\u4f46\u5728\u5185\u90e8\u4f7f\u7528\u57fa\u7c7b\u5b9e\u73b0\u65f6\uff0c\u79c1\u6709\u7ee7\u627f\u53ef\u80fd\u5f88\u6709\u7528\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u4e0d\u5e0c\u671b\u57fa\u7c7b\u7684\u516c\u5171\u63a5\u53e3\u901a\u8fc7\u6d3e\u751f\u7c7b\u7684\u5bf9\u8c61\u516c\u5f00(\u5982\u679c\u516c\u5f00\u7ee7\u627f\u5c31\u4f1a\u8fd9\u6837)\u3002 \u4e0d\u8fc7\uff0c\u5728\u5b9e\u8df5\u4e2d\uff0c\u5f88\u5c11\u4f7f\u7528\u79c1\u6709\u7ee7\u627f\u3002","title":"\u79c1\u6709\u7ee7\u627f"},{"location":"17-5-inheritance-and-access-specifiers/#_5","text":"1 2 3 4 5 6 7 8 9 class Base { public : int m_public {}; protected : int m_protected {}; private : int m_private {}; }; Base \u5728\u8bbf\u95ee\u81ea\u5df1\u7684\u6210\u5458\u65f6\uff0c\u6ca1\u6709\u4efb\u4f55\u9650\u5236\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5916\u90e8\u4ee3\u7801\u53ea\u80fd\u8bbf\u95ee m_public \u3002\u6d3e\u751f\u7c7b\u53ef\u4ee5\u8bbf\u95ee m_public \u548c m_protected \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 class D2 : private Base // note: private inheritance { // \u79c1\u6709\u7ee7\u627f\uff1a // \u7ee7\u627f\u6765\u7684\u516c\u6709\u6210\u5458\u53d8\u4e3a\u79c1\u6709 // \u7ee7\u627f\u6765\u7684\u53d7\u4fdd\u62a4\u6210\u5458\u53d8\u4e3a\u79c1\u6709 // \u7ee7\u627f\u6765\u7684\u516c\u6709\u6210\u5458\u4ecd\u7136\u662f\u4e0d\u53ef\u8bbf\u95ee\u7684 public : int m_public2 {}; protected : int m_protected2 {}; private : int m_private2 {}; }; D2 \u53ef\u4ee5\u4e0d\u53d7\u9650\u5236\u5730\u8bbf\u95ee\u81ea\u5df1\u7684\u6210\u5458\u3002 D2 \u53ef\u4ee5\u8bbf\u95ee Base \u7684 m_public \u548c m_protected \u6210\u5458\uff0c\u4f46\u4e0d\u80fd\u8bbf\u95ee m_private \u6210\u5458\u3002\u56e0\u4e3a D2 \u79c1\u6709\u5730\u7ee7\u627f\u4e86 Base \uff0c\u5f53\u901a\u8fc7 D2 \u8bbf\u95ee\u65f6\uff0c m_public \u548c m_protected \u73b0\u5728\u88ab\u8ba4\u4e3a\u662f\u79c1\u6709\u7684\u3002\u8fd9\u610f\u5473\u7740\u5728\u4f7f\u7528 D2 \u5bf9\u8c61\u65f6\uff0c\u5916\u90e8\u4ee3\u7801\u4e0d\u80fd\u8bbf\u95ee\u8fd9\u4e9b\u53d8\u91cf\uff0c \u4e5f\u4e0d\u80fd\u8bbf\u95ee\u4ece D2 \u6d3e\u751f\u7684\u4efb\u4f55\u7c7b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 class D3 : public D2 { // \u516c\u6709\u7ee7\u627f\uff1a // \u7ee7\u627f\u6765\u7684\u516c\u6709\u6210\u5458\u4ecd\u7136\u516c\u6709 // \u7ee7\u627f\u6765\u7684\u53d7\u4fdd\u62a4\u6210\u5458\u4ecd\u7136\u53d7\u4fdd\u62a4 // \u7ee7\u627f\u6765\u7684\u79c1\u6709\u6210\u5458\u4ecd\u7136\u4e0d\u53ef\u8bbf\u95ee public : int m_public3 {}; protected : int m_protected3 {}; private : int m_private3 {}; }; COPY D3 can access its own members without restriction. D3 can access D2\u2019s m_public2 and m_protected2 members, but not m_private2. Because D3 inherited D2 publicly, m_public2 and m_protected2 keep their access specifiers when accessed through D3. D3 has no access to Base\u2019s m_private, which was already private in Base. Nor does it have access to Base\u2019s m_protected or m_public, both of which became private when D2 inherited them. D3 \u53ef\u4ee5\u4e0d\u53d7\u9650\u5236\u5730\u8bbf\u95ee\u81ea\u5df1\u7684\u6210\u5458\u3002 D3 \u53ef\u4ee5\u8bbf\u95ee D2 \u7684 m_public2 \u548c m_protected2 \u6210\u5458\uff0c\u4f46\u4e0d\u80fd\u8bbf\u95ee m_private2 \u6210\u5458\u3002\u56e0\u4e3a D3 \u516c\u5f00\u7ee7\u627f\u4e86 D2 \uff0c\u6240\u4ee5\u5f53\u901a\u8fc7 D3 \u8bbf\u95ee\u65f6\uff0c m_public2 \u548c m_protected2 \u4fdd\u7559\u4e86\u5b83\u4eec\u7684\u8bbf\u95ee\u4fee\u9970\u7b26\u3002 D3 \u4e0d\u80fd\u8bbf\u95ee Base \u7684 m_private \uff0c\u5b83\u5728 Base \u4e2d\u5df2\u7ecf\u662f\u79c1\u6709\u7684\u3002\u5b83\u4e5f\u4e0d\u80fd\u8bbf\u95ee Base \u7684 m_protected \u6216 m_public \uff0c\u5f53 D2 \u7ee7\u627f\u5b83\u4eec\u65f6\uff0c\u5b83\u4eec\u90fd\u53d8\u6210\u4e86\u79c1\u6709\u3002","title":"\u6700\u540e\u4e00\u4e2a\u4f8b\u5b50"},{"location":"17-5-inheritance-and-access-specifiers/#_6","text":"\u8bbf\u95ee\u8bf4\u660e\u7b26\u3001\u7ee7\u627f\u7c7b\u578b\u548c\u6d3e\u751f\u7c7b\u7684\u4ea4\u4e92\u65b9\u5f0f\u4f1a\u5bfc\u81f4\u5f88\u591a\u6df7\u4e71\u3002\u5c3d\u91cf\u628a\u4e8b\u60c5\u5f04\u6e05\u695a: \u9996\u5148\uff0c\u7c7b(\u53ca\u5176\u53cb\u5143)\u603b\u662f\u53ef\u4ee5\u8bbf\u95ee\u81ea\u5df1\u7684\u975e\u7ee7\u627f\u6210\u5458\u3002\u8bbf\u95ee\u8bf4\u660e\u7b26\u53ea\u5f71\u54cd\u5916\u90e8\u548c\u6d3e\u751f\u7c7b\u662f\u5426\u53ef\u4ee5\u8bbf\u95ee\u8fd9\u4e9b\u6210\u5458\u3002 \u5176\u6b21\uff0c\u5f53\u6d3e\u751f\u7c7b\u7ee7\u627f\u6210\u5458\u65f6\uff0c\u8fd9\u4e9b\u6210\u5458\u53ef\u80fd\u4f1a\u66f4\u6539\u6d3e\u751f\u7c7b\u4e2d\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u3002\u8fd9\u4e0d\u4f1a\u5f71\u54cd\u6d3e\u751f\u7c7b\u81ea\u5df1\u7684(\u975e\u7ee7\u627f\u7684)\u6210\u5458(\u5b83\u4eec\u6709\u81ea\u5df1\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26)\u3002\u5b83\u53ea\u5f71\u54cd\u5916\u90e8\u4eba\u5458\u548c\u4ece\u6d3e\u751f\u7c7b\u6d3e\u751f\u7684\u7c7b\u662f\u5426\u53ef\u4ee5\u8bbf\u95ee\u90a3\u4e9b\u7ee7\u627f\u7684\u6210\u5458\u3002 \u4e0b\u9762\u662f\u6240\u6709\u8bbf\u95ee\u8bf4\u660e\u7b26\u548c\u7ee7\u627f\u7c7b\u578b\u7ec4\u5408\u7684\u8868 \u57fa\u7c7b\u4e2d\u7684\u8bbf\u95ee\u4fee\u9970\u7b26 \u516c\u6709\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u4fee\u9970\u7b26 \u79c1\u6709\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u4fee\u9970\u7b26 \u53d7\u4fdd\u62a4\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u4fee\u9970\u7b26 Public Public Private Protected Protected Protected Private Protected Private Inaccessible Inaccessible Inaccessible \u6700\u540e\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5c3d\u7ba1\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ea\u5c55\u793a\u4e86\u4f7f\u7528\u6210\u5458\u53d8\u91cf\u7684\u4f8b\u5b50\uff0c\u4f46\u8fd9\u4e9b\u8bbf\u95ee\u89c4\u5219\u5bf9\u6240\u6709\u6210\u5458\u90fd\u6210\u7acb(\u4f8b\u5982\uff0c\u5728\u7c7b\u5185\u90e8\u58f0\u660e\u7684\u6210\u5458\u51fd\u6570\u548c\u7c7b\u578b)\u3002","title":"\u5c0f\u7ed3"},{"location":"17-6-adding-new-functionality-to-a-derived-class/","tags":["inheritance"],"text":"Key Takeaway - \u5728 17.1 - \u7ee7\u627f\u7b80\u4ecb \u4e2d\u6211\u4eec\u63d0\u5230\uff0c\u4f7f\u7528\u6d3e\u751f\u7c7b\u6700\u5927\u7684\u597d\u5904\u5728\u4e8e\u53ef\u4ee5\u91cd\u7528\u4ee3\u7801\u3002\u4f60\u53ef\u4ee5\u7ee7\u627f\u57fa\u7c7b\u7684\u529f\u80fd\u5e76\u5728\u6d3e\u751f\u7c7b\u4e2d\u6dfb\u52a0\u9700\u8981\u7684\u65b0\u529f\u80fd\u3001\u4fee\u6539\u5df2\u7ecf\u5b58\u5728\u7684\u529f\u80fd\u6216\u8005\u9690\u85cf\u4f60\u4e0d\u9700\u8981\u7684\u529f\u80fd\u3002\u5728\u63a5\u4e0b\u6765\u7684\u51e0\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u505a\u5230\u8fd9\u4e9b\u3002 \u9996\u5148\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include class Base { protected : int m_value {}; public : Base ( int value ) : m_value { value } { } void identify () const { std :: cout << \"I am a Base \\n \" ; } }; \u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7ee7\u627f\u81ea Base \u7684\u6d3e\u751f\u7c7b\u3002\u56e0\u4e3a\u6211\u4eec\u5e0c\u671b\u6d3e\u751f\u7c7b\u80fd\u591f\u5728\u6d3e\u751f\u5bf9\u8c61\u5b9e\u4f8b\u5316\u65f6\u8bbe\u7f6e m_value \u7684\u503c\uff0c\u6240\u4ee5\u9700\u8981\u5728 Derived \u7684 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u4e2d\u8c03\u7528 Base \u7684\u6784\u9020\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 class Derived : public Base { public : Derived ( int value ) : Base { value } { } }; \u5411\u6d3e\u751f\u7c7b\u4e2d\u6dfb\u52a0\u65b0\u7684\u529f\u80fd \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u8bbf\u95ee Base \u7c7b\u7684\u6e90\u4ee3\u7801\uff0c\u6240\u4ee5\u5982\u679c\u9700\u8981\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5411 Base \u6dfb\u52a0\u529f\u80fd\u3002 \u6709\u65f6\u5019\uff0c\u867d\u7136\u57fa\u7c7b\u7684\u4ee3\u7801\u53ef\u4ee5\u8bbf\u95ee\uff0c\u4f46\u6211\u4eec\u4e0d\u60f3\u4fee\u6539\u5b83\u3002\u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u60a8\u521a\u521a\u4ece\u7b2c\u4e09\u65b9\u4f9b\u5e94\u5546\u8d2d\u4e70\u4e86\u4e00\u4e2a\u4ee3\u7801\u5e93\uff0c\u4f46\u9700\u8981\u4e00\u4e9b\u989d\u5916\u7684\u529f\u80fd\u3002\u60a8\u53ef\u4ee5\u6dfb\u52a0\u5230\u539f\u59cb\u4ee3\u7801\u4e2d\uff0c\u4f46\u8fd9\u4e0d\u662f\u6700\u597d\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5982\u679c\u4f9b\u5e94\u5546\u8fdc\u7a0b\u53d1\u5e03\u4e86\u65b0\u7248\u672c\u600e\u4e48\u529e\uff1f\u6dfb\u52a0\u7684\u5185\u5bb9\u8981\u4e48\u88ab\u8986\u76d6\uff0c\u8981\u4e48\u5fc5\u987b\u624b\u52a8\u5c06\u5b83\u4eec\u8fc1\u79fb\u5230\u66f4\u65b0\u4e2d\uff0c\u8fd9\u65e2\u8017\u65f6\u53c8\u6709\u98ce\u9669\u3002 \u6216\u8005\uff0c\u6709\u65f6\u751a\u81f3\u4e0d\u53ef\u80fd\u4fee\u6539\u57fa\u7c7b\u3002\u8003\u8651\u6807\u51c6\u5e93\u4e2d\u7684\u4ee3\u7801\u3002\u6211\u4eec\u65e0\u6cd5\u4fee\u6539\u4f5c\u4e3a\u6807\u51c6\u5e93\u4e00\u90e8\u5206\u7684\u4ee3\u7801\u3002\u4f46\u662f\u6211\u4eec\u80fd\u591f\u4ece\u8fd9\u4e9b\u7c7b\u7ee7\u627f\uff0c\u7136\u540e\u5c06\u6211\u4eec\u81ea\u5df1\u7684\u529f\u80fd\u6dfb\u52a0\u5230\u6211\u4eec\u7684\u6d3e\u751f\u7c7b\u4e2d\u3002\u540c\u6837\u7684\u60c5\u51b5\u4e5f\u53d1\u751f\u5728\u7b2c\u4e09\u65b9\u5e93\u4e2d\uff0c\u5728\u90a3\u91cc\u4f60\u53ef\u4ee5\u8bbf\u95ee\u5934\u6587\u4ef6\uff0c\u4f46\u662f\u4ee3\u7801\u662f\u9884\u7f16\u8bd1\u7684\u3002 \u5728\u4efb\u4f55\u4e00\u79cd\u60c5\u51b5\u4e0b\uff0c\u6700\u597d\u7684\u7b54\u6848\u662f\u521b\u5efa\u4e00\u4e2a\u81ea\u5df1\u7684\u6d3e\u751f\u7c7b\uff0c\u5e76\u5411\u6d3e\u751f\u7c7b\u6dfb\u52a0\u4f60\u60f3\u8981\u7684\u529f\u80fd\u3002 \u57fa\u7c7b\u4e2d\u6ca1\u6709\u5305\u62ec\u7528\u4e8e\u516c\u5171\u4ee3\u7801\u8bbf\u95ee m_value \u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5728\u57fa\u7c7b\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u8bbf\u95ee\u51fd\u6570\u6765\u8865\u6551\u8fd9\u4e2a\u95ee\u9898\u2014\u2014\u4f46\u4e3a\u4e86\u4e3e\u4f8b\uff0c\u6211\u4eec\u5c06\u628a\u5b83\u6dfb\u52a0\u5230\u6d3e\u751f\u7c7b\u4e2d\u3002\u56e0\u4e3a m_value \u5728\u57fa\u7c7b\u4e2d\u88ab\u58f0\u660e\u4e3a\u53d7\u4fdd\u62a4\u7684\uff0c Derived \u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5b83\u3002 \u8981\u5411\u6d3e\u751f\u7c7b\u6dfb\u52a0\u65b0\u529f\u80fd\uff0c\u53ea\u9700\u5728\u6d3e\u751f\u7c7b\u4e2d\u50cf\u6b63\u5e38\u4e00\u6837\u58f0\u660e\u8be5\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 class Derived : public Base { public : Derived ( int value ) : Base { value } { } int getValue () const { return m_value ; } }; \u73b0\u5728 public \u5c06\u80fd\u591f\u5728 Derived \u7c7b\u578b\u7684\u5bf9\u8c61\u4e0a\u8c03\u7528 getValue() \u6765\u8bbf\u95ee m_value \u7684\u503c\u3002 1 2 3 4 5 6 7 int main () { Derived derived { 5 }; std :: cout << \"derived has value \" << derived . getValue () << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 derived has value 5 \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c Base \u5bf9\u8c61\u5e76\u4e0d\u80fd\u8bbf\u95ee Derived \u4e2d\u7684 getValue() \u51fd\u6570\uff0c\u6240\u4ee5\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u4e0d\u80fd\u5de5\u4f5c\u7684\uff1a 1 2 3 4 5 6 7 int main () { Base base { 5 }; std :: cout << \"base has value \" << base . getValue () << '\\n' ; return 0 ; } \u8fd9\u662f\u56e0\u4e3a Base \u7c7b\u4e2d\u5e76\u6ca1\u6709 getValue() \u51fd\u6570\uff0c\u5b83\u5c5e\u4e8e Derived \u7c7b\u3002\u56e0\u4e3a Derived \u662f Base \uff0c\u6240\u4ee5\u5b83\u80fd\u591f\u8bbf\u95ee Base \u7684\u5c5e\u6027\u3002\u4f46\u662f\uff0c Base \u4e0d\u80fd\u8bbf\u95ee Derived \u7684\u4efb\u4f55\u5c5e\u6027\u3002","title":"17.6 - \u4e3a\u6d3e\u751f\u7c7b\u6dfb\u52a0\u65b0\u529f\u80fd"},{"location":"17-6-adding-new-functionality-to-a-derived-class/#_1","text":"\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u8bbf\u95ee Base \u7c7b\u7684\u6e90\u4ee3\u7801\uff0c\u6240\u4ee5\u5982\u679c\u9700\u8981\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5411 Base \u6dfb\u52a0\u529f\u80fd\u3002 \u6709\u65f6\u5019\uff0c\u867d\u7136\u57fa\u7c7b\u7684\u4ee3\u7801\u53ef\u4ee5\u8bbf\u95ee\uff0c\u4f46\u6211\u4eec\u4e0d\u60f3\u4fee\u6539\u5b83\u3002\u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u60a8\u521a\u521a\u4ece\u7b2c\u4e09\u65b9\u4f9b\u5e94\u5546\u8d2d\u4e70\u4e86\u4e00\u4e2a\u4ee3\u7801\u5e93\uff0c\u4f46\u9700\u8981\u4e00\u4e9b\u989d\u5916\u7684\u529f\u80fd\u3002\u60a8\u53ef\u4ee5\u6dfb\u52a0\u5230\u539f\u59cb\u4ee3\u7801\u4e2d\uff0c\u4f46\u8fd9\u4e0d\u662f\u6700\u597d\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5982\u679c\u4f9b\u5e94\u5546\u8fdc\u7a0b\u53d1\u5e03\u4e86\u65b0\u7248\u672c\u600e\u4e48\u529e\uff1f\u6dfb\u52a0\u7684\u5185\u5bb9\u8981\u4e48\u88ab\u8986\u76d6\uff0c\u8981\u4e48\u5fc5\u987b\u624b\u52a8\u5c06\u5b83\u4eec\u8fc1\u79fb\u5230\u66f4\u65b0\u4e2d\uff0c\u8fd9\u65e2\u8017\u65f6\u53c8\u6709\u98ce\u9669\u3002 \u6216\u8005\uff0c\u6709\u65f6\u751a\u81f3\u4e0d\u53ef\u80fd\u4fee\u6539\u57fa\u7c7b\u3002\u8003\u8651\u6807\u51c6\u5e93\u4e2d\u7684\u4ee3\u7801\u3002\u6211\u4eec\u65e0\u6cd5\u4fee\u6539\u4f5c\u4e3a\u6807\u51c6\u5e93\u4e00\u90e8\u5206\u7684\u4ee3\u7801\u3002\u4f46\u662f\u6211\u4eec\u80fd\u591f\u4ece\u8fd9\u4e9b\u7c7b\u7ee7\u627f\uff0c\u7136\u540e\u5c06\u6211\u4eec\u81ea\u5df1\u7684\u529f\u80fd\u6dfb\u52a0\u5230\u6211\u4eec\u7684\u6d3e\u751f\u7c7b\u4e2d\u3002\u540c\u6837\u7684\u60c5\u51b5\u4e5f\u53d1\u751f\u5728\u7b2c\u4e09\u65b9\u5e93\u4e2d\uff0c\u5728\u90a3\u91cc\u4f60\u53ef\u4ee5\u8bbf\u95ee\u5934\u6587\u4ef6\uff0c\u4f46\u662f\u4ee3\u7801\u662f\u9884\u7f16\u8bd1\u7684\u3002 \u5728\u4efb\u4f55\u4e00\u79cd\u60c5\u51b5\u4e0b\uff0c\u6700\u597d\u7684\u7b54\u6848\u662f\u521b\u5efa\u4e00\u4e2a\u81ea\u5df1\u7684\u6d3e\u751f\u7c7b\uff0c\u5e76\u5411\u6d3e\u751f\u7c7b\u6dfb\u52a0\u4f60\u60f3\u8981\u7684\u529f\u80fd\u3002 \u57fa\u7c7b\u4e2d\u6ca1\u6709\u5305\u62ec\u7528\u4e8e\u516c\u5171\u4ee3\u7801\u8bbf\u95ee m_value \u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5728\u57fa\u7c7b\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u8bbf\u95ee\u51fd\u6570\u6765\u8865\u6551\u8fd9\u4e2a\u95ee\u9898\u2014\u2014\u4f46\u4e3a\u4e86\u4e3e\u4f8b\uff0c\u6211\u4eec\u5c06\u628a\u5b83\u6dfb\u52a0\u5230\u6d3e\u751f\u7c7b\u4e2d\u3002\u56e0\u4e3a m_value \u5728\u57fa\u7c7b\u4e2d\u88ab\u58f0\u660e\u4e3a\u53d7\u4fdd\u62a4\u7684\uff0c Derived \u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5b83\u3002 \u8981\u5411\u6d3e\u751f\u7c7b\u6dfb\u52a0\u65b0\u529f\u80fd\uff0c\u53ea\u9700\u5728\u6d3e\u751f\u7c7b\u4e2d\u50cf\u6b63\u5e38\u4e00\u6837\u58f0\u660e\u8be5\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 class Derived : public Base { public : Derived ( int value ) : Base { value } { } int getValue () const { return m_value ; } }; \u73b0\u5728 public \u5c06\u80fd\u591f\u5728 Derived \u7c7b\u578b\u7684\u5bf9\u8c61\u4e0a\u8c03\u7528 getValue() \u6765\u8bbf\u95ee m_value \u7684\u503c\u3002 1 2 3 4 5 6 7 int main () { Derived derived { 5 }; std :: cout << \"derived has value \" << derived . getValue () << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 derived has value 5 \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c Base \u5bf9\u8c61\u5e76\u4e0d\u80fd\u8bbf\u95ee Derived \u4e2d\u7684 getValue() \u51fd\u6570\uff0c\u6240\u4ee5\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u4e0d\u80fd\u5de5\u4f5c\u7684\uff1a 1 2 3 4 5 6 7 int main () { Base base { 5 }; std :: cout << \"base has value \" << base . getValue () << '\\n' ; return 0 ; } \u8fd9\u662f\u56e0\u4e3a Base \u7c7b\u4e2d\u5e76\u6ca1\u6709 getValue() \u51fd\u6570\uff0c\u5b83\u5c5e\u4e8e Derived \u7c7b\u3002\u56e0\u4e3a Derived \u662f Base \uff0c\u6240\u4ee5\u5b83\u80fd\u591f\u8bbf\u95ee Base \u7684\u5c5e\u6027\u3002\u4f46\u662f\uff0c Base \u4e0d\u80fd\u8bbf\u95ee Derived \u7684\u4efb\u4f55\u5c5e\u6027\u3002","title":"\u5411\u6d3e\u751f\u7c7b\u4e2d\u6dfb\u52a0\u65b0\u7684\u529f\u80fd"},{"location":"17-7-calling-inherited-functions-and-overriding-behavior/","tags":["inheritance","overriding"],"text":"Key Takeaway \u57fa\u4e8e\u6d3e\u751f\u7c7b\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u7f16\u8bd1\u5668\u6cbf\u7740\u7ee7\u627f\u94fe\u5411\u4e0a\u67e5\u627e\u5e76\u4f7f\u7528\u627e\u5230\u7684\u7b2c\u4e00\u4e2a \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6d3e\u751f\u7c7b\u7ee7\u627f\u57fa\u7c7b\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u884c\u4e3a\u3002\u5728\u8fd9\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u66f4\u8be6\u7ec6\u5730\u7814\u7a76\u5982\u4f55\u9009\u62e9\u6210\u5458\u51fd\u6570\uff0c\u4ee5\u53ca\u5982\u4f55\u5229\u7528\u5b83\u6765\u6539\u53d8\u6d3e\u751f\u7c7b\u4e2d\u7684\u884c\u4e3a\u3002 \u8c03\u7528\u57fa\u7c7b\u51fd\u6570 \u5f53\u4f7f\u7528\u6d3e\u751f\u7c7b\u5bf9\u8c61\u8c03\u7528\u6210\u5458\u51fd\u6570\u65f6\uff0c\u7f16\u8bd1\u5668\u9996\u5148\u67e5\u770b\u8be5\u6210\u5458\u662f\u5426\u5b58\u5728\u4e8e\u6d3e\u751f\u7c7b\u4e2d\u3002\u5982\u679c\u6ca1\u6709\uff0c\u5b83\u5f00\u59cb\u6cbf\u7740\u7ee7\u627f\u94fe\u5411\u4e0a\u67e5\u627e\uff0c\u5e76\u68c0\u67e5\u6210\u5458\u662f\u5426\u5df2\u7ecf\u5728\u4efb\u4f55\u7236\u7c7b\u4e2d\u5b9a\u4e49\u3002\u5b83\u4f7f\u7528\u5b83\u627e\u5230\u7684\u7b2c\u4e00\u4e2a\u3002 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include class Base { protected : int m_value {}; public : Base ( int value ) : m_value { value } { } void identify () const { std :: cout << \"I am a Base \\n \" ; } }; class Derived : public Base { public : Derived ( int value ) : Base { value } { } }; int main () { Base base { 5 }; base . identify (); Derived derived { 7 }; derived . identify (); return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 I am a Base I am a Base \u5f53\u8c03\u7528 derived.identify() \u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u67e5\u770b identify() \u51fd\u6570\u662f\u5426\u88ab\u5b9a\u4e49\u5728 Derived \u7c7b\u4e2d\u3002\u5b9e\u9645\u4e0a\u6ca1\u6709\u3002\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u7ee7\u7eed\u67e5\u770b\u8be5\u7c7b\u7684\u7236\u7c7b\uff08\u672c\u4f8b\u4e2d\u7684 Base \uff09\u3002 Base \u7c7b\u5b9a\u4e49\u4e86 identify() \u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u4f1a\u4f7f\u7528\u8be5\u51fd\u6570\u3002\u6362\u8a00\u4e4b\uff0c\u7531\u4e8e Derived::identify() \u4e0d\u5b58\u5728\uff0c\u6240\u4ee5\u5b9e\u9645\u8c03\u7528\u7684\u662f Base::identify() \u3002 \u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u57fa\u7c7b\u63d0\u4f9b\u7684\u51fd\u6570\u6ee1\u8db3\u9700\u8981\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u57fa\u7c7b\u7684\u51fd\u6570\u3002 \u91cd\u5b9a\u4e49\u884c\u4e3a\uff08\u51fd\u6570\uff09 \u4e0d\u8fc7\uff0c\u5982\u679c\u6211\u4eec\u5728\u6d3e\u751f\u7c7b\u4e2d\u5b9a\u4e49\u4e86 Derived::identify() \uff0c\u5219\u4f1a\u8c03\u7528\u8fd9\u4e2a\u7248\u672c\u7684\u51fd\u6570\u3002 \u4e5f\u5c31\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6d3e\u751f\u7c7b\u4e2d\u91cd\u5b9a\u4e49\u901a\u8fc7\u91cd\u65b0\u5b9a\u4e49\u51fd\u6570\u6765\u4f7f\u6d3e\u751f\u7c7b\u5177\u6709\u4e0d\u540c\u7684\u884c\u4e3a\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c derived.identify() \u6253\u5370 \u201cI am a Derived\u201d \u7684\u8bdd\u624d\u66f4\u5408\u7406\u3002\u6240\u4ee5\uff0c\u8ba9\u6211\u4eec\u5728\u6d3e\u751f\u7c7b\u4e2d\u4fee\u6539\u51fd\u6570 identify() \uff0c\u4f7f\u5176\u80fd\u591f\u5728\u6211\u4eec\u8c03\u7528 derived.identify() \u65f6\u6253\u5370\u6b63\u786e\u7684\u5185\u5bb9\u3002 \u8981\u4fee\u6539\u57fa\u7c7b\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u5728\u6d3e\u751f\u7c7b\u4e2d\u7684\u5de5\u4f5c\u65b9\u5f0f\uff0c\u53ea\u9700\u91cd\u65b0\u5b9a\u4e49\u6d3e\u751f\u7c7b\u4e2d\u7684\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include class Derived : public Base { public : Derived ( int value ) : Base { value } { } int getValue () const { return m_value ; } // \u5728\u6b64\u5904\u4fee\u6539\u51fd\u6570 void identify () const { std :: cout << \"I am a Derived \\n \" ; } }; \u7c7b\u4f3c\u7684\u4f8b\u5b50\uff0c\u4f46\u662f\u8c03\u7528\u4e86 Derived::identify() \uff1a 1 2 3 4 5 6 7 8 9 10 int main () { Base base { 5 }; base . identify (); Derived derived { 7 }; derived . identify (); return 0 ; } 1 2 I am a Base I am a Derived \u6ce8\u610f\uff0c\u5f53\u5728\u6d3e\u751f\u7c7b\u4e2d\u91cd\u65b0\u5b9a\u4e49\u51fd\u6570\u65f6\uff0c \u6d3e\u751f\u51fd\u6570\u4e0d\u4f1a\u7ee7\u627f\u57fa\u7c7b\u4e2d\u540c\u540d\u51fd\u6570\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u3002\u5b83\u4f7f\u7528\u6d3e\u751f\u7c7b\u4e2d\u5b9a\u4e49\u5b83\u7684\u4efb\u4f55\u8bbf\u95ee\u8bf4\u660e\u7b26 \u3002\u56e0\u6b64\uff0c\u5728\u57fa\u7c7b\u4e2d\u5b9a\u4e49\u4e3a private \u7684\u51fd\u6570\u53ef\u4ee5\u5728\u6d3e\u751f\u7c7b\u4e2d\u91cd\u65b0\u5b9a\u4e49\u4e3a public \uff0c\u53cd\u4e4b\u4ea6\u7136\uff01 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include class Base { private : void print () const { std :: cout << \"Base\" ; } }; class Derived : public Base { public : void print () const { std :: cout << \"Derived \" ; } }; int main () { Derived derived ; derived . print (); // calls derived::print(), which is public return 0 ; } \u6dfb\u52a0\u529f\u80fd \u6709\u65f6\uff0c\u6211\u4eec\u4e0d\u60f3\u5b8c\u5168\u66ff\u6362\u57fa\u7c7b\u7684\u6210\u5458\u51fd\u6570\uff0c\u800c\u662f\u60f3\u5411\u5b83\u6dfb\u52a0\u989d\u5916\u7684\u529f\u80fd\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6ce8\u610f Derived::identify() \u5b8c\u5168\u66ff\u6362\u4e86 Base::identify() \uff01\u8fd9\u53ef\u80fd\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u3002\u53ef\u4ee5\u8ba9\u6d3e\u751f\u51fd\u6570\u8c03\u7528\u540c\u540d\u51fd\u6570\u7684\u57fa\u672c\u7248\u672c(\u4ee5\u4fbf\u91cd\u7528\u4ee3\u7801)\uff0c\u7136\u540e\u5411\u5176\u6dfb\u52a0\u989d\u5916\u7684\u529f\u80fd\u3002 \u8981\u8ba9\u6d3e\u751f\u51fd\u6570\u8c03\u7528\u540c\u540d\u7684\u57fa\u51fd\u6570\uff0c\u53ea\u9700\u6267\u884c\u4e00\u4e2a\u666e\u901a\u51fd\u6570\u8c03\u7528\uff0c\u4f46\u5728\u51fd\u6570\u524d\u9762\u52a0\u4e0a \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 (\u57fa\u7c7b\u7684\u540d\u79f0\u548c\u4e24\u4e2a\u5192\u53f7)\u3002\u4e0b\u9762\u7684\u793a\u4f8b\u91cd\u65b0\u5b9a\u4e49\u4e86 Derived::identify() \uff0c\u5b83\u9996\u5148\u8c03\u7528 Base::identify() \uff0c\u7136\u540e\u6267\u884c\u81ea\u5df1\u7684\u9644\u52a0\u64cd\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include class Derived : public Base { public : Derived ( int value ) : Base { value } { } int getValue () const { return m_value ; } void identify () const { Base :: identify (); // call Base::identify() first std :: cout << \"I am a Derived \\n \" ; // then identify ourselves } }; \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { Base base { 5 }; base . identify (); Derived derived { 7 }; derived . identify (); return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 I am a Base I am a Base I am a Derived \u5f53 derived.identify() \u6267\u884cs\u65f6\uff0c\u5b83\u88ab\u89e3\u6790\u4e3a Derived::identify() \u3002\u4f46\u662f\u5b83\u505a\u7684\u7b2c\u4e00\u4ef6\u4e8b\u662f\u8c03\u7528 Base::identify() \uff0c\u8fdb\u800c\u6253\u5370 \u201cI am a Base\u201d\u3002\u5f53 Base::identify() \u8fd4\u56de\u65f6\uff0c Derived::identify() \u4f1a\u7ee7\u7eed\u6267\u884c\u5e76\u6253\u5370 \u201cI am a Derived\u201d\u3002 \u5f88\u7b80\u5355\u5427\u3002 \u4e3a\u4ec0\u4e48\u6211\u4eec\u9700\u8981\u4f7f\u7528 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u5462\uff1f\u5982\u679c\u50cf\u4e0b\u9762\u8fd9\u6837\u5b9a\u4e49 Derived::identify() \u4f1a\u600e\u6837\u5462\uff1f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include class Derived : public Base { public : Derived ( int value ) : Base { value } { } int getValue () const { return m_value ; } void identify () const { identify (); // Note: no scope resolution! cout << \"I am a Derived\" ; } }; \u4e0d\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u9650\u5b9a\u7b26\u800c\u8c03\u7528 identify() \u7684\u8bdd\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4f1a\u8c03\u7528\u5f53\u524d\u7c7b\u4e2d\u7684 identify() \uff0c\u8fd9\u6837\u8c03\u7528\u7684\u5c31\u662f Derived::identify() \uff0c\u4e8e\u662f Derived::identify() \u4f1a\u81ea\u5df1\u8c03\u7528\u81ea\u5df1\uff0c\u5bfc\u81f4\u6b7b\u5faa\u73af\uff01 \u5728\u5c1d\u8bd5\u8c03\u7528\u57fa\u7c7b\u4e2d\u7684\u53cb\u5143\u51fd\u6570\u65f6\uff0c\u6bd4\u5982\u64cd\u4f5c\u7b26 << \uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u70b9\u9ebb\u70e6\u3002 \u56e0\u4e3a\u57fa\u7c7b\u7684\u53cb\u51fd\u6570\u5b9e\u9645\u4e0a\u4e0d\u662f\u57fa\u7c7b\u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u4f7f\u7528\u8303\u56f4\u89e3\u6790\u9650\u5b9a\u7b26\u4e0d\u8d77\u4f5c\u7528 \u3002\u6211\u4eec\u9700\u8981\u4e00\u79cd\u65b9\u6cd5\u4f7f\u6211\u4eec\u7684\u6d3e\u751f\u7c7b\u4e34\u65f6\u770b\u8d77\u6765\u50cf\u57fa\u7c7b\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u8c03\u7528\u51fd\u6570\u7684\u6b63\u786e\u7248\u672c\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u8fd9\u5f88\u5bb9\u6613\u505a\u5230\uff0c\u53ea\u9700\u4f7f\u7528 static_cast \u8fdb\u884c\u8f6c\u6362\u5373\u53ef\u3002\u8bf7\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include class Base { private : int m_value {}; public : Base ( int value ) : m_value { value } { } friend std :: ostream & operator << ( std :: ostream & out , const Base & b ) { out << \"In Base \\n \" ; out << b . m_value << '\\n' ; return out ; } }; class Derived : public Base { public : Derived ( int value ) : Base { value } { } friend std :: ostream & operator << ( std :: ostream & out , const Derived & d ) { out << \"In Derived \\n \" ; // static_cast Derived to a Base object, so we call the right version of operator<< out << static_cast < const Base &> ( d ); return out ; } }; int main () { Derived derived { 7 }; std :: cout << derived << '\\n' ; return 0 ; } \u56e0\u4e3a Derived \u201d\u662f\u4e00\u4e2a\u201c Base \uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06 Derived \u8f6c\u6362\u4e3a Base \uff0c\u7136\u540e\u5c31\u53ef\u4ee5\u8c03\u7528 Base \u7684 << \u64cd\u4f5c\u7b26\u4e86\u3002 \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 In Derived In Base 7","title":"17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199"},{"location":"17-7-calling-inherited-functions-and-overriding-behavior/#_1","text":"\u5f53\u4f7f\u7528\u6d3e\u751f\u7c7b\u5bf9\u8c61\u8c03\u7528\u6210\u5458\u51fd\u6570\u65f6\uff0c\u7f16\u8bd1\u5668\u9996\u5148\u67e5\u770b\u8be5\u6210\u5458\u662f\u5426\u5b58\u5728\u4e8e\u6d3e\u751f\u7c7b\u4e2d\u3002\u5982\u679c\u6ca1\u6709\uff0c\u5b83\u5f00\u59cb\u6cbf\u7740\u7ee7\u627f\u94fe\u5411\u4e0a\u67e5\u627e\uff0c\u5e76\u68c0\u67e5\u6210\u5458\u662f\u5426\u5df2\u7ecf\u5728\u4efb\u4f55\u7236\u7c7b\u4e2d\u5b9a\u4e49\u3002\u5b83\u4f7f\u7528\u5b83\u627e\u5230\u7684\u7b2c\u4e00\u4e2a\u3002 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include class Base { protected : int m_value {}; public : Base ( int value ) : m_value { value } { } void identify () const { std :: cout << \"I am a Base \\n \" ; } }; class Derived : public Base { public : Derived ( int value ) : Base { value } { } }; int main () { Base base { 5 }; base . identify (); Derived derived { 7 }; derived . identify (); return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 I am a Base I am a Base \u5f53\u8c03\u7528 derived.identify() \u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u67e5\u770b identify() \u51fd\u6570\u662f\u5426\u88ab\u5b9a\u4e49\u5728 Derived \u7c7b\u4e2d\u3002\u5b9e\u9645\u4e0a\u6ca1\u6709\u3002\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u7ee7\u7eed\u67e5\u770b\u8be5\u7c7b\u7684\u7236\u7c7b\uff08\u672c\u4f8b\u4e2d\u7684 Base \uff09\u3002 Base \u7c7b\u5b9a\u4e49\u4e86 identify() \u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u4f1a\u4f7f\u7528\u8be5\u51fd\u6570\u3002\u6362\u8a00\u4e4b\uff0c\u7531\u4e8e Derived::identify() \u4e0d\u5b58\u5728\uff0c\u6240\u4ee5\u5b9e\u9645\u8c03\u7528\u7684\u662f Base::identify() \u3002 \u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u57fa\u7c7b\u63d0\u4f9b\u7684\u51fd\u6570\u6ee1\u8db3\u9700\u8981\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u57fa\u7c7b\u7684\u51fd\u6570\u3002","title":"\u8c03\u7528\u57fa\u7c7b\u51fd\u6570"},{"location":"17-7-calling-inherited-functions-and-overriding-behavior/#_2","text":"\u4e0d\u8fc7\uff0c\u5982\u679c\u6211\u4eec\u5728\u6d3e\u751f\u7c7b\u4e2d\u5b9a\u4e49\u4e86 Derived::identify() \uff0c\u5219\u4f1a\u8c03\u7528\u8fd9\u4e2a\u7248\u672c\u7684\u51fd\u6570\u3002 \u4e5f\u5c31\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6d3e\u751f\u7c7b\u4e2d\u91cd\u5b9a\u4e49\u901a\u8fc7\u91cd\u65b0\u5b9a\u4e49\u51fd\u6570\u6765\u4f7f\u6d3e\u751f\u7c7b\u5177\u6709\u4e0d\u540c\u7684\u884c\u4e3a\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c derived.identify() \u6253\u5370 \u201cI am a Derived\u201d \u7684\u8bdd\u624d\u66f4\u5408\u7406\u3002\u6240\u4ee5\uff0c\u8ba9\u6211\u4eec\u5728\u6d3e\u751f\u7c7b\u4e2d\u4fee\u6539\u51fd\u6570 identify() \uff0c\u4f7f\u5176\u80fd\u591f\u5728\u6211\u4eec\u8c03\u7528 derived.identify() \u65f6\u6253\u5370\u6b63\u786e\u7684\u5185\u5bb9\u3002 \u8981\u4fee\u6539\u57fa\u7c7b\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u5728\u6d3e\u751f\u7c7b\u4e2d\u7684\u5de5\u4f5c\u65b9\u5f0f\uff0c\u53ea\u9700\u91cd\u65b0\u5b9a\u4e49\u6d3e\u751f\u7c7b\u4e2d\u7684\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include class Derived : public Base { public : Derived ( int value ) : Base { value } { } int getValue () const { return m_value ; } // \u5728\u6b64\u5904\u4fee\u6539\u51fd\u6570 void identify () const { std :: cout << \"I am a Derived \\n \" ; } }; \u7c7b\u4f3c\u7684\u4f8b\u5b50\uff0c\u4f46\u662f\u8c03\u7528\u4e86 Derived::identify() \uff1a 1 2 3 4 5 6 7 8 9 10 int main () { Base base { 5 }; base . identify (); Derived derived { 7 }; derived . identify (); return 0 ; } 1 2 I am a Base I am a Derived \u6ce8\u610f\uff0c\u5f53\u5728\u6d3e\u751f\u7c7b\u4e2d\u91cd\u65b0\u5b9a\u4e49\u51fd\u6570\u65f6\uff0c \u6d3e\u751f\u51fd\u6570\u4e0d\u4f1a\u7ee7\u627f\u57fa\u7c7b\u4e2d\u540c\u540d\u51fd\u6570\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u3002\u5b83\u4f7f\u7528\u6d3e\u751f\u7c7b\u4e2d\u5b9a\u4e49\u5b83\u7684\u4efb\u4f55\u8bbf\u95ee\u8bf4\u660e\u7b26 \u3002\u56e0\u6b64\uff0c\u5728\u57fa\u7c7b\u4e2d\u5b9a\u4e49\u4e3a private \u7684\u51fd\u6570\u53ef\u4ee5\u5728\u6d3e\u751f\u7c7b\u4e2d\u91cd\u65b0\u5b9a\u4e49\u4e3a public \uff0c\u53cd\u4e4b\u4ea6\u7136\uff01 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include class Base { private : void print () const { std :: cout << \"Base\" ; } }; class Derived : public Base { public : void print () const { std :: cout << \"Derived \" ; } }; int main () { Derived derived ; derived . print (); // calls derived::print(), which is public return 0 ; }","title":"\u91cd\u5b9a\u4e49\u884c\u4e3a\uff08\u51fd\u6570\uff09"},{"location":"17-7-calling-inherited-functions-and-overriding-behavior/#_3","text":"\u6709\u65f6\uff0c\u6211\u4eec\u4e0d\u60f3\u5b8c\u5168\u66ff\u6362\u57fa\u7c7b\u7684\u6210\u5458\u51fd\u6570\uff0c\u800c\u662f\u60f3\u5411\u5b83\u6dfb\u52a0\u989d\u5916\u7684\u529f\u80fd\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6ce8\u610f Derived::identify() \u5b8c\u5168\u66ff\u6362\u4e86 Base::identify() \uff01\u8fd9\u53ef\u80fd\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u3002\u53ef\u4ee5\u8ba9\u6d3e\u751f\u51fd\u6570\u8c03\u7528\u540c\u540d\u51fd\u6570\u7684\u57fa\u672c\u7248\u672c(\u4ee5\u4fbf\u91cd\u7528\u4ee3\u7801)\uff0c\u7136\u540e\u5411\u5176\u6dfb\u52a0\u989d\u5916\u7684\u529f\u80fd\u3002 \u8981\u8ba9\u6d3e\u751f\u51fd\u6570\u8c03\u7528\u540c\u540d\u7684\u57fa\u51fd\u6570\uff0c\u53ea\u9700\u6267\u884c\u4e00\u4e2a\u666e\u901a\u51fd\u6570\u8c03\u7528\uff0c\u4f46\u5728\u51fd\u6570\u524d\u9762\u52a0\u4e0a \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 (\u57fa\u7c7b\u7684\u540d\u79f0\u548c\u4e24\u4e2a\u5192\u53f7)\u3002\u4e0b\u9762\u7684\u793a\u4f8b\u91cd\u65b0\u5b9a\u4e49\u4e86 Derived::identify() \uff0c\u5b83\u9996\u5148\u8c03\u7528 Base::identify() \uff0c\u7136\u540e\u6267\u884c\u81ea\u5df1\u7684\u9644\u52a0\u64cd\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include class Derived : public Base { public : Derived ( int value ) : Base { value } { } int getValue () const { return m_value ; } void identify () const { Base :: identify (); // call Base::identify() first std :: cout << \"I am a Derived \\n \" ; // then identify ourselves } }; \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { Base base { 5 }; base . identify (); Derived derived { 7 }; derived . identify (); return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 I am a Base I am a Base I am a Derived \u5f53 derived.identify() \u6267\u884cs\u65f6\uff0c\u5b83\u88ab\u89e3\u6790\u4e3a Derived::identify() \u3002\u4f46\u662f\u5b83\u505a\u7684\u7b2c\u4e00\u4ef6\u4e8b\u662f\u8c03\u7528 Base::identify() \uff0c\u8fdb\u800c\u6253\u5370 \u201cI am a Base\u201d\u3002\u5f53 Base::identify() \u8fd4\u56de\u65f6\uff0c Derived::identify() \u4f1a\u7ee7\u7eed\u6267\u884c\u5e76\u6253\u5370 \u201cI am a Derived\u201d\u3002 \u5f88\u7b80\u5355\u5427\u3002 \u4e3a\u4ec0\u4e48\u6211\u4eec\u9700\u8981\u4f7f\u7528 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u5462\uff1f\u5982\u679c\u50cf\u4e0b\u9762\u8fd9\u6837\u5b9a\u4e49 Derived::identify() \u4f1a\u600e\u6837\u5462\uff1f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include class Derived : public Base { public : Derived ( int value ) : Base { value } { } int getValue () const { return m_value ; } void identify () const { identify (); // Note: no scope resolution! cout << \"I am a Derived\" ; } }; \u4e0d\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u9650\u5b9a\u7b26\u800c\u8c03\u7528 identify() \u7684\u8bdd\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4f1a\u8c03\u7528\u5f53\u524d\u7c7b\u4e2d\u7684 identify() \uff0c\u8fd9\u6837\u8c03\u7528\u7684\u5c31\u662f Derived::identify() \uff0c\u4e8e\u662f Derived::identify() \u4f1a\u81ea\u5df1\u8c03\u7528\u81ea\u5df1\uff0c\u5bfc\u81f4\u6b7b\u5faa\u73af\uff01 \u5728\u5c1d\u8bd5\u8c03\u7528\u57fa\u7c7b\u4e2d\u7684\u53cb\u5143\u51fd\u6570\u65f6\uff0c\u6bd4\u5982\u64cd\u4f5c\u7b26 << \uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u70b9\u9ebb\u70e6\u3002 \u56e0\u4e3a\u57fa\u7c7b\u7684\u53cb\u51fd\u6570\u5b9e\u9645\u4e0a\u4e0d\u662f\u57fa\u7c7b\u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u4f7f\u7528\u8303\u56f4\u89e3\u6790\u9650\u5b9a\u7b26\u4e0d\u8d77\u4f5c\u7528 \u3002\u6211\u4eec\u9700\u8981\u4e00\u79cd\u65b9\u6cd5\u4f7f\u6211\u4eec\u7684\u6d3e\u751f\u7c7b\u4e34\u65f6\u770b\u8d77\u6765\u50cf\u57fa\u7c7b\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u8c03\u7528\u51fd\u6570\u7684\u6b63\u786e\u7248\u672c\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u8fd9\u5f88\u5bb9\u6613\u505a\u5230\uff0c\u53ea\u9700\u4f7f\u7528 static_cast \u8fdb\u884c\u8f6c\u6362\u5373\u53ef\u3002\u8bf7\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include class Base { private : int m_value {}; public : Base ( int value ) : m_value { value } { } friend std :: ostream & operator << ( std :: ostream & out , const Base & b ) { out << \"In Base \\n \" ; out << b . m_value << '\\n' ; return out ; } }; class Derived : public Base { public : Derived ( int value ) : Base { value } { } friend std :: ostream & operator << ( std :: ostream & out , const Derived & d ) { out << \"In Derived \\n \" ; // static_cast Derived to a Base object, so we call the right version of operator<< out << static_cast < const Base &> ( d ); return out ; } }; int main () { Derived derived { 7 }; std :: cout << derived << '\\n' ; return 0 ; } \u56e0\u4e3a Derived \u201d\u662f\u4e00\u4e2a\u201c Base \uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06 Derived \u8f6c\u6362\u4e3a Base \uff0c\u7136\u540e\u5c31\u53ef\u4ee5\u8c03\u7528 Base \u7684 << \u64cd\u4f5c\u7b26\u4e86\u3002 \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 In Derived In Base 7","title":"\u6dfb\u52a0\u529f\u80fd"},{"location":"17-8-Hiding-inherited-functionality/","tags":["inheritance","overriding"],"text":"Key Takeaway - \u4fee\u6539\u7ee7\u627f\u6210\u5458\u7684\u8bbf\u95ee\u7ea7\u522b C++\u5141\u8bb8\u6211\u4eec\u5728\u6d3e\u751f\u7c7b\u4e2d\u66f4\u6539\u7ee7\u627f\u6210\u5458\u7684 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u3002\u8fd9\u662f\u901a\u8fc7\u5728\u65b0\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u4e0b\u4f7f\u7528 using\u58f0\u660e\u6765\u6807\u8bc6\u5728\u6d3e\u751f\u7c7b\u4e2d\u66f4\u6539\u5176\u8bbf\u95ee\u6743\u9650\u7684(\u9650\u5b9a\u8303\u56f4\u7684)\u57fa\u7c7b\u6210\u5458\u6765\u5b9e\u73b0\u7684\u3002 \u8003\u8651\u4e0b\u9762\u7684 Base \u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include class Base { private : int m_value {}; public : Base ( int value ) : m_value { value } { } protected : void printValue () const { std :: cout << m_value ; } }; \u56e0\u4e3a Base::printValue() \u7c7b\u88ab\u58f0\u660e\u4e3a \u53d7\u4fdd\u62a4\u6210\u5458 \uff0c\u6240\u4ee5\u5b83\u53ea\u80fd\u88ab Base \u6216\u5b83\u7684\u6d3e\u751f\u7c7b\u8bbf\u95ee\u3002\u800c\u5916\u90e8\u4ee3\u7801\u662f\u4e0d\u80fd\u8bbf\u95ee\u5b83\u7684\u3002 \u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u628a printValue() \u7684\u8bbf\u95ee\u7ea7\u522b\u8c03\u6574\u4e3a \u516c\u6709\u6210\u5458 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class Derived : public Base { public : Derived ( int value ) : Base { value } { } // Base::printValue \u662f\u7ee7\u627f\u6765\u7684\u53d7\u4fdd\u62a4\u6210\u5458\uff0c\u6240\u4ee5\u5916\u90e8\u4ee3\u7801\u4e0d\u80fd\u8bbf\u95ee\u5b83 // \u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 using \u58f0\u660e\u5c06\u5176\u4fee\u6539\u4e3apublic\u7684 using Base :: printValue ; // note: \u6ce8\u610f\u8fd9\u91cc\u6ca1\u6709\u62ec\u53f7 }; \u8fd9\u610f\u5473\u7740\u8fd9\u6bb5\u4ee3\u7801\u73b0\u5728\u53ef\u4ee5\u5de5\u4f5c\u4e86\uff1a 1 2 3 4 5 6 7 8 int main () { Derived derived { 7 }; // printValue public in Derived, so this is okay derived . printValue (); // prints 7 return 0 ; } \u6211\u4eec\u53ea\u80fd\u4fee\u6539\u6d3e\u751f\u7c7b\u4e2d\u80fd\u591f\u8bbf\u95ee\u7684\u57fa\u7c7b\u6210\u5458\u6210\u5458\u7684\u8bbf\u95ee\u4fee\u9970\u7b26\u3002\u56e0\u6b64\uff0c\u79c1\u6709\u7684\u57fa\u6210\u5458\u65e0\u6cd5\u88ab\u4fee\u6539\u4e3a\u53d7\u4fdd\u62a4\u6216\u516c\u6709\uff0c\u56e0\u4e3a\u6d3e\u751f\u7c7b\u4e0d\u80fd\u8bbf\u95ee\u57fa\u7c7b\u7684\u79c1\u6709\u6210\u5458\u3002 \u9690\u85cf\u51fd\u6570 \u5728 C++ \u4e2d\uff0c\u9664\u4e86\u76f4\u63a5\u4fee\u6539\u4ee3\u7801\u4e4b\u5916\uff0c\u8fd8\u6709\u522b\u7684\u529e\u6cd5\u5220\u9664\u6216\u9650\u5236\u57fa\u7c7b\u4e2d\u51fd\u6570\u7684\u4f7f\u7528\u3002\u57fa\u7c7b\u4e2d\u7684\u7684\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u88ab\u9690\u85cf\u8d77\u6765\uff0c\u8fd9\u6837\u5b83\u5c31\u4e0d\u80fd\u5728\u6d3e\u751f\u7c7b\u4e2d\u88ab\u8bbf\u95ee\u3002\u8981\u8fbe\u5230\u8fd9\u4e2a\u76ee\u7684\uff0c\u53ea\u9700\u8981\u4fee\u6539\u8bbf\u95ee\u4fee\u9970\u7b26\u5373\u53ef\u3002 \u4f8b\u5982\uff0c\u53ef\u4ee5\u628a \u516c\u6709\u6210\u5458 \u4fee\u6539\u4e3a \u79c1\u6709\u6210\u5458 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include class Base { public : int m_value {}; }; class Derived : public Base { private : using Base :: m_value ; public : Derived ( int value ) // We can't initialize m_value, since it's a Base member (Base must initialize it) { // But we can assign it a value m_value = value ; } }; int main () { Derived derived { 7 }; // The following won't work because m_value has been redefined as private std :: cout << derived . m_value ; return 0 ; } \u6ce8\u610f\uff0c\u8fd9\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u8bbe\u8ba1\u7cdf\u7cd5\u7684\u57fa\u7c7b\uff0c\u5e76\u5c06\u5176\u6570\u636e\u826f\u597d\u5730\u5c01\u88c5\u5728\u6d3e\u751f\u7c7b\u4e2d\u3002 \u6216\u8005\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u79c1\u6709\u5730\u7ee7\u627f Base \uff0c\u9664\u4e86 \u516c\u5f00\u7ee7\u627f Base \u7684\u6210\u5458\u5e76\u901a\u8fc7\u91cd\u5199\u5176\u8bbf\u95ee\u8bf4\u660e\u7b26\u4f7f m_value \u53d8\u4e3a \u79c1\u6709\u6210\u5458 \u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5 \u901a\u8fc7 \u79c1\u6709\u7ee7\u627f \u7684\u65b9\u5f0f\u7ee7\u627f Base \uff0c\u8fd9\u6837\u4e00\u6765 Base \u7684\u6240\u6709\u6210\u5458\u5728\u4e00\u5f00\u59cb\u5c31\u88ab\u79c1\u6709\u5730\u7ee7\u627f\u3002 \u4f60\u4e5f\u53ef\u4ee5\u5728\u6d3e\u751f\u7c7b\u4e2d\u5c06\u6210\u5458\u51fd\u6570\u6807\u8bb0\u4e3a delete \uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u4e0d\u80fd\u901a\u8fc7\u6d3e\u751f\u5bf9\u8c61\u8c03\u7528: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include class Base { private : int m_value {}; public : Base ( int value ) : m_value { value } { } int getValue () const { return m_value ; } }; class Derived : public Base { public : Derived ( int value ) : Base { value } { } int getValue () = delete ; // mark this function as inaccessible }; int main () { Derived derived { 7 }; // The following won't work because getValue() has been deleted! std :: cout << derived . getValue (); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c getValue() \u51fd\u6570\u88ab\u6807\u8bb0\u4e3a delete \u3002\u8fd9\u610f\u5473\u7740\u8c03\u7528\u6d3e\u751f\u7c7b\u4e2d\u7684\u8be5\u51fd\u6570\u65f6\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\u3002 \u6ce8\u610f\uff0c\u57fa\u7c7b\u4e2d\u7684 getValue() \u51fd\u6570\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528 \u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\u8c03\u7528 Base::getValue() \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int main () { Derived derived { 7 }; // \u53ef\u4ee5\u76f4\u63a5\u8c03\u7528 Base::getValue() std :: cout << derived . Base :: getValue (); // \u5c06\u5b50\u7c7b\u8f6c\u6362\u4e3a\u57fa\u7c7b\u7136\u540e\u8c03\u7528 getValue() \u5c31\u4f1a\u89e3\u6790\u5230 Base::getValue() std :: cout << static_cast < Base &> ( derived ). getValue (); return 0 ; } \u5982\u679c\u4f7f\u7528\u5f3a\u5236\u8f6c\u6362\u65b9\u6cd5\uff0c\u5219\u5f3a\u5236\u8f6c\u6362\u4e3aBase&\u800c\u4e0d\u662fBase\uff0c\u4ee5\u907f\u514d\u751f\u6210 derived \u7684 Base \u90e8\u5206\u7684\u526f\u672c\u3002","title":"17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570"},{"location":"17-8-Hiding-inherited-functionality/#_1","text":"C++\u5141\u8bb8\u6211\u4eec\u5728\u6d3e\u751f\u7c7b\u4e2d\u66f4\u6539\u7ee7\u627f\u6210\u5458\u7684 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u3002\u8fd9\u662f\u901a\u8fc7\u5728\u65b0\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u4e0b\u4f7f\u7528 using\u58f0\u660e\u6765\u6807\u8bc6\u5728\u6d3e\u751f\u7c7b\u4e2d\u66f4\u6539\u5176\u8bbf\u95ee\u6743\u9650\u7684(\u9650\u5b9a\u8303\u56f4\u7684)\u57fa\u7c7b\u6210\u5458\u6765\u5b9e\u73b0\u7684\u3002 \u8003\u8651\u4e0b\u9762\u7684 Base \u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include class Base { private : int m_value {}; public : Base ( int value ) : m_value { value } { } protected : void printValue () const { std :: cout << m_value ; } }; \u56e0\u4e3a Base::printValue() \u7c7b\u88ab\u58f0\u660e\u4e3a \u53d7\u4fdd\u62a4\u6210\u5458 \uff0c\u6240\u4ee5\u5b83\u53ea\u80fd\u88ab Base \u6216\u5b83\u7684\u6d3e\u751f\u7c7b\u8bbf\u95ee\u3002\u800c\u5916\u90e8\u4ee3\u7801\u662f\u4e0d\u80fd\u8bbf\u95ee\u5b83\u7684\u3002 \u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u628a printValue() \u7684\u8bbf\u95ee\u7ea7\u522b\u8c03\u6574\u4e3a \u516c\u6709\u6210\u5458 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class Derived : public Base { public : Derived ( int value ) : Base { value } { } // Base::printValue \u662f\u7ee7\u627f\u6765\u7684\u53d7\u4fdd\u62a4\u6210\u5458\uff0c\u6240\u4ee5\u5916\u90e8\u4ee3\u7801\u4e0d\u80fd\u8bbf\u95ee\u5b83 // \u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 using \u58f0\u660e\u5c06\u5176\u4fee\u6539\u4e3apublic\u7684 using Base :: printValue ; // note: \u6ce8\u610f\u8fd9\u91cc\u6ca1\u6709\u62ec\u53f7 }; \u8fd9\u610f\u5473\u7740\u8fd9\u6bb5\u4ee3\u7801\u73b0\u5728\u53ef\u4ee5\u5de5\u4f5c\u4e86\uff1a 1 2 3 4 5 6 7 8 int main () { Derived derived { 7 }; // printValue public in Derived, so this is okay derived . printValue (); // prints 7 return 0 ; } \u6211\u4eec\u53ea\u80fd\u4fee\u6539\u6d3e\u751f\u7c7b\u4e2d\u80fd\u591f\u8bbf\u95ee\u7684\u57fa\u7c7b\u6210\u5458\u6210\u5458\u7684\u8bbf\u95ee\u4fee\u9970\u7b26\u3002\u56e0\u6b64\uff0c\u79c1\u6709\u7684\u57fa\u6210\u5458\u65e0\u6cd5\u88ab\u4fee\u6539\u4e3a\u53d7\u4fdd\u62a4\u6216\u516c\u6709\uff0c\u56e0\u4e3a\u6d3e\u751f\u7c7b\u4e0d\u80fd\u8bbf\u95ee\u57fa\u7c7b\u7684\u79c1\u6709\u6210\u5458\u3002","title":"\u4fee\u6539\u7ee7\u627f\u6210\u5458\u7684\u8bbf\u95ee\u7ea7\u522b"},{"location":"17-8-Hiding-inherited-functionality/#_2","text":"\u5728 C++ \u4e2d\uff0c\u9664\u4e86\u76f4\u63a5\u4fee\u6539\u4ee3\u7801\u4e4b\u5916\uff0c\u8fd8\u6709\u522b\u7684\u529e\u6cd5\u5220\u9664\u6216\u9650\u5236\u57fa\u7c7b\u4e2d\u51fd\u6570\u7684\u4f7f\u7528\u3002\u57fa\u7c7b\u4e2d\u7684\u7684\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u88ab\u9690\u85cf\u8d77\u6765\uff0c\u8fd9\u6837\u5b83\u5c31\u4e0d\u80fd\u5728\u6d3e\u751f\u7c7b\u4e2d\u88ab\u8bbf\u95ee\u3002\u8981\u8fbe\u5230\u8fd9\u4e2a\u76ee\u7684\uff0c\u53ea\u9700\u8981\u4fee\u6539\u8bbf\u95ee\u4fee\u9970\u7b26\u5373\u53ef\u3002 \u4f8b\u5982\uff0c\u53ef\u4ee5\u628a \u516c\u6709\u6210\u5458 \u4fee\u6539\u4e3a \u79c1\u6709\u6210\u5458 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include class Base { public : int m_value {}; }; class Derived : public Base { private : using Base :: m_value ; public : Derived ( int value ) // We can't initialize m_value, since it's a Base member (Base must initialize it) { // But we can assign it a value m_value = value ; } }; int main () { Derived derived { 7 }; // The following won't work because m_value has been redefined as private std :: cout << derived . m_value ; return 0 ; } \u6ce8\u610f\uff0c\u8fd9\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u8bbe\u8ba1\u7cdf\u7cd5\u7684\u57fa\u7c7b\uff0c\u5e76\u5c06\u5176\u6570\u636e\u826f\u597d\u5730\u5c01\u88c5\u5728\u6d3e\u751f\u7c7b\u4e2d\u3002 \u6216\u8005\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u79c1\u6709\u5730\u7ee7\u627f Base \uff0c\u9664\u4e86 \u516c\u5f00\u7ee7\u627f Base \u7684\u6210\u5458\u5e76\u901a\u8fc7\u91cd\u5199\u5176\u8bbf\u95ee\u8bf4\u660e\u7b26\u4f7f m_value \u53d8\u4e3a \u79c1\u6709\u6210\u5458 \u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5 \u901a\u8fc7 \u79c1\u6709\u7ee7\u627f \u7684\u65b9\u5f0f\u7ee7\u627f Base \uff0c\u8fd9\u6837\u4e00\u6765 Base \u7684\u6240\u6709\u6210\u5458\u5728\u4e00\u5f00\u59cb\u5c31\u88ab\u79c1\u6709\u5730\u7ee7\u627f\u3002 \u4f60\u4e5f\u53ef\u4ee5\u5728\u6d3e\u751f\u7c7b\u4e2d\u5c06\u6210\u5458\u51fd\u6570\u6807\u8bb0\u4e3a delete \uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u4e0d\u80fd\u901a\u8fc7\u6d3e\u751f\u5bf9\u8c61\u8c03\u7528: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include class Base { private : int m_value {}; public : Base ( int value ) : m_value { value } { } int getValue () const { return m_value ; } }; class Derived : public Base { public : Derived ( int value ) : Base { value } { } int getValue () = delete ; // mark this function as inaccessible }; int main () { Derived derived { 7 }; // The following won't work because getValue() has been deleted! std :: cout << derived . getValue (); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c getValue() \u51fd\u6570\u88ab\u6807\u8bb0\u4e3a delete \u3002\u8fd9\u610f\u5473\u7740\u8c03\u7528\u6d3e\u751f\u7c7b\u4e2d\u7684\u8be5\u51fd\u6570\u65f6\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\u3002 \u6ce8\u610f\uff0c\u57fa\u7c7b\u4e2d\u7684 getValue() \u51fd\u6570\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528 \u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\u8c03\u7528 Base::getValue() \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int main () { Derived derived { 7 }; // \u53ef\u4ee5\u76f4\u63a5\u8c03\u7528 Base::getValue() std :: cout << derived . Base :: getValue (); // \u5c06\u5b50\u7c7b\u8f6c\u6362\u4e3a\u57fa\u7c7b\u7136\u540e\u8c03\u7528 getValue() \u5c31\u4f1a\u89e3\u6790\u5230 Base::getValue() std :: cout << static_cast < Base &> ( derived ). getValue (); return 0 ; } \u5982\u679c\u4f7f\u7528\u5f3a\u5236\u8f6c\u6362\u65b9\u6cd5\uff0c\u5219\u5f3a\u5236\u8f6c\u6362\u4e3aBase&\u800c\u4e0d\u662fBase\uff0c\u4ee5\u907f\u514d\u751f\u6210 derived \u7684 Base \u90e8\u5206\u7684\u526f\u672c\u3002","title":"\u9690\u85cf\u51fd\u6570"},{"location":"17-9-multiple-inheritance/","tags":["inheritance","multiple-inheritance"],"text":"Key Takeaway - \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u4ecb\u7ecd\u7684\u6240\u6709\u7ee7\u627f\u793a\u4f8b\u90fd\u662f\u5355\u7ee7\u627f\u2014\u2014\u4e5f\u5c31\u662f\u8bf4\uff0c\u6bcf\u4e2a\u7ee7\u627f\u7684\u7c7b\u90fd\u6709\u4e14\u53ea\u6709\u4e00\u4e2a\u7236\u7c7b\u3002\u4e0d\u8fc7\uff0cC++\u5176\u5b9e\u8fd8\u652f\u6301\u591a\u91cd\u7ee7\u627f\u3002 \u591a\u91cd\u7ee7\u627f \u4f7f\u5f97\u6d3e\u751f\u7c7b\u53ef\u4ee5\u4ece\u591a\u4e2a\u7236\u7c7b\u7ee7\u627f\u6210\u5458\u3002 \u5047\u8bbe\u6211\u4eec\u60f3\u5199\u4e00\u4e2a\u7a0b\u5e8f\u6765\u8bb0\u5f55\u4e00\u7fa4\u8001\u5e08\u7684\u4fe1\u606f\u3002\u8001\u5e08\u201d\u662f\u4e00\u4e2a\u201c\u4eba\u3002\u7136\u800c\uff0c\u6559\u5e08\u4e5f\u662f\u96c7\u5458(\u5982\u679c\u4e3a\u81ea\u5df1\u5de5\u4f5c\uff0c\u4ed6\u4eec\u5c31\u662f\u81ea\u5df1\u7684\u96c7\u4e3b)\u3002\u591a\u91cd\u7ee7\u627f\u53ef\u7528\u4e8e\u521b\u5efa\u4ece Person \u548c Employee \u7ee7\u627f\u5c5e\u6027\u7684 Teacher \u7c7b\u3002\u8981\u4f7f\u7528\u591a\u91cd\u7ee7\u627f\uff0c\u53ea\u9700\u4e00\u6b21\u6307\u5b9a\u6bcf\u4e2a\u57fa\u7c7b(\u5c31\u50cf\u5355\u7ee7\u627f\u4e00\u6837)\uff0c\u7528\u9017\u53f7\u5206\u9694\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 #include #include class Person { private : std :: string m_name ; int m_age {}; public : Person ( std :: string_view name , int age ) : m_name { name }, m_age { age } { } const std :: string & getName () const { return m_name ; } int getAge () const { return m_age ; } }; class Employee { private : std :: string m_employer ; double m_wage {}; public : Employee ( std :: string_view employer , double wage ) : m_employer { employer }, m_wage { wage } { } const std :: string & getEmployer () const { return m_employer ; } double getWage () const { return m_wage ; } }; // Teacher publicly inherits Person and Employee class Teacher : public Person , public Employee { private : int m_teachesGrade {}; public : Teacher ( std :: string_view name , int age , std :: string_view employer , double wage , int teachesGrade ) : Person { name , age }, Employee { employer , wage }, m_teachesGrade { teachesGrade } { } }; int main () { Teacher t { \"Mary\" , 45 , \"Boo\" , 14.3 , 8 }; return 0 ; } Mixins mixin (\u4e5f\u62fc\u4f5c \u201cmix-in\u201d) \u662f\u4e00\u4e2a\u5c0f\u7684\u7c7b\uff0c\u5b83\u53ef\u4ee5\u88ab\u7c7b\u7ee7\u627f\u4ee5\u4fbf\u4e3a\u5176\u6dfb\u52a0\u4e00\u4e9b\u5c5e\u6027\u3002\u5b83\u7684\u540d\u5b57mixin\uff08\u6df7\u5165\uff09\u8bf4\u660e\uff0c\u5b83\u7684\u7528\u9014\u662f\u88ab\u6dfb\u52a0\u5230\u5176\u4ed6\u7c7b\u4e2d\uff0c\u800c\u4e0d\u662f\u5355\u72ec\u88ab\u5b9e\u4f8b\u5316\u3002 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c Box \u548c Label \u7c7b\u90fd\u662f mixin\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f\u5b83\u4eec\u6765\u521b\u5efa\u65b0\u7684 Button class. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 // h/t to reader Waldo for this example #include struct Point2D { int x ; int y ; }; class Box // mixin Box class { public : void setTopLeft ( Point2D point ) { m_topLeft = point ; } void setBottomRight ( Point2D point ) { m_bottomRight = point ; } private : Point2D m_topLeft {}; Point2D m_bottomRight {}; }; class Label // mixin Label class { public : void setText ( const std :: string_view str ) { m_text = str ; } void setFontSize ( int fontSize ) { m_fontSize = fontSize ; } private : std :: string m_text {}; int m_fontSize {}; }; class Button : public Box , public Label {}; int main () { Button button {}; button . Box :: setTopLeft ({ 1 , 1 }); button . Box :: setBottomRight ({ 10 , 10 }); button . Label :: setText ( \"Username: \" ); button . Label :: setFontSize ( 6 ); } COPY \u6269\u5c55\u9605\u8bfb \u56e0\u4e3amixin\u88ab\u8bbe\u8ba1\u4e3a\u5411\u6d3e\u751f\u7c7b\u6dfb\u52a0\u529f\u80fd\uff0c\u800c\u4e0d\u662f\u63d0\u4f9b\u63a5\u53e3\uff0c\u6240\u4ee5mixin\u901a\u5e38\u4e0d\u4f7f\u7528\u865a\u51fd\u6570(\u4e0b\u4e00\u7ae0\u5c06\u8ba8\u8bba)\u3002\u76f8\u53cd\uff0c\u5982\u679c\u9700\u8981\u81ea\u5b9a\u4e49mixin\u7c7b\u4ee5\u7279\u5b9a\u7684\u65b9\u5f0f\u5de5\u4f5c\uff0c\u5219\u901a\u5e38\u4f7f\u7528\u6a21\u677f\u3002\u7531\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0cmixin\u7c7b\u901a\u5e38\u662f\u6a21\u677f\u5316\u7684\u3002 \u4e5f\u8bb8\u4ee4\u4eba\u60ca\u8bb6\u7684\u662f\uff0c\u6d3e\u751f\u7c7b\u53ef\u4ee5\u4f7f\u7528\u6d3e\u751f\u7c7b\u4f5c\u4e3a\u6a21\u677f\u7c7b\u578b\u53c2\u6570\u4ecemixin\u57fa\u7c7b\u7ee7\u627f\u3002\u8fd9\u6837\u7684\u7ee7\u627f\u88ab\u79f0\u4e3a\u5947\u602a\u7684\u5faa\u73af\u6a21\u677f\u6a21\u5f0f(\u7b80\u79f0CRTP)\uff0c\u5b83\u770b\u8d77\u6765\u50cf\u8fd9\u6837: 1 2 3 4 5 6 7 8 9 10 11 12 // The Curiously Recurring Template Pattern (CRTP) template < class T > class Mixin { // Mixin can use template type parameter T to access members of Derived // via (static_cast(this)) }; class Derived : public Mixin < Derived > { }; \u4f7f\u7528 CRTP \u7684\u7b80\u5355\u4f8b\u5b50\u53ef\u4ee5\u770b \u8fd9\u91cc . \u591a\u91cd\u7ee7\u627f\u5e26\u6765\u7684\u95ee\u9898 \u867d\u7136\u591a\u91cd\u7ee7\u627f\u4f3c\u4e4e\u662f\u5355\u7ee7\u627f\u7684\u7b80\u5355\u6269\u5c55\uff0c\u4f46\u591a\u91cd\u7ee7\u627f\u5f15\u5165\u4e86\u8bb8\u591a\u95ee\u9898\uff0c\u8fd9\u4e9b\u95ee\u9898\u53ef\u80fd\u663e\u8457\u589e\u52a0\u7a0b\u5e8f\u7684\u590d\u6742\u6027\uff0c\u5e76\u4f7f\u5176\u6210\u4e3a\u7ef4\u62a4\u7684\u5669\u68a6\u3002\u8ba9\u6211\u4eec\u6765\u770b\u770b\u4e00\u4e9b\u5177\u4f53\u60c5\u51b5\u3002 \u9996\u5148\uff0c\u5f53\u591a\u4e2a\u57fa\u7c7b\u5305\u542b\u5177\u6709\u76f8\u540c\u540d\u79f0\u7684\u51fd\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u4ea7\u751f\u6b67\u4e49\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include class USBDevice { private : long m_id {}; public : USBDevice ( long id ) : m_id { id } { } long getID () const { return m_id ; } }; class NetworkDevice { private : long m_id {}; public : NetworkDevice ( long id ) : m_id { id } { } long getID () const { return m_id ; } }; class WirelessAdapter : public USBDevice , public NetworkDevice { public : WirelessAdapter ( long usbId , long networkId ) : USBDevice { usbId }, NetworkDevice { networkId } { } }; int main () { WirelessAdapter c54G { 5442 , 181742 }; std :: cout << c54G . getID (); // Which getID() do we call? return 0 ; } \u5f53 c54G.getID() \u7f16\u8bd1\u7684\u65f6\u5019\uff0c\u7f16\u8bd1\u5668\u4f1a\u67e5\u770b WirelessAdapter \u4e2d\u662f\u5426\u6709\u540d\u4e3a getID() \u7684\u51fd\u6570\u3002\u6ca1\u6709\u3002\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u67e5\u770b\u7236\u7c7b\u4e2d\u662f\u5426\u5177\u6709\u540d\u4e3a getID() \u7684\u51fd\u6570\u3002\u770b\u5230\u95ee\u9898\u4e86\u5417\uff1f\u95ee\u9898\u662f c54G \u5b9e\u9645\u4e0a\u5305\u542b\u4e24\u4e2a getID() \u51fd\u6570\uff1a\u4e00\u4e2a\u7ee7\u627f\u81ea USBDevice \uff0c \u53e6\u4e00\u4e2a\u7ee7\u627f\u81eaNetworkDevice \u3002\u56e0\u6b64\uff0c\u8fd9\u4e2a\u51fd\u6570\u8c03\u7528\u662f\u6a21\u7cca\u7684\uff0c\u5982\u679c\u8bd5\u56fe\u7f16\u8bd1\u5b83\uff0c\u4f60\u5c06\u6536\u5230\u4e00\u4e2a\u7f16\u8bd1\u5668\u9519\u8bef\u3002 1 2 3 4 5 6 7 int main () { WirelessAdapter c54G { 5442 , 181742 }; std :: cout << c54G . USBDevice :: getID (); return 0 ; } \u867d\u7136\u4e0a\u8ff0\u89e3\u51b3\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u4f46\u662f\u5f53\u60a8\u7684\u7c7b\u7ee7\u627f4\u4e2a\u62166\u4e2a\u57fa\u7c7b(\u8fd9\u4e9b\u57fa\u7c7b\u672c\u8eab\u7ee7\u627f\u5176\u4ed6\u7c7b)\u65f6\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u4e8b\u60c5\u4f1a\u53d8\u5f97\u591a\u4e48\u590d\u6742\u3002\u968f\u7740\u7ee7\u627f\u7684\u7c7b\u8d8a\u6765\u8d8a\u591a\uff0c\u547d\u540d\u51b2\u7a81\u7684\u53ef\u80fd\u6027\u5448\u6307\u6570\u7ea7\u589e\u957f\uff0c\u9700\u8981\u663e\u5f0f\u5730\u89e3\u51b3\u6bcf\u4e00\u4e2a\u547d\u540d\u51b2\u7a81\u3002 \u5176\u6b21\uff0c \u83f1\u5f62\u7ee7\u627f\u95ee\u9898 \u66f4\u52a0\u4e25\u91cd\uff0c\u7b14\u8005\u5c06\u5176\u79f0\u4e3a\u201d\u83f1\u5f62\u672b\u65e5\u201c\u3002\u5f53\u4e00\u4e2a\u7c7b\u4ece\u4e24\u4e2a\u7c7b\u7ee7\u627f\uff0c\u800c\u8fd9\u4e24\u4e2a\u7c7b\u53c8\u7ee7\u627f\u81ea\u4e00\u4e2a\u5171\u540c\u7684\u7c7b\u65f6\uff0c\u5c31\u4f1a\u51fa\u73b0\u83f1\u5f62\u7ee7\u627f\u95ee\u9898\u3002\u5176\u7ee7\u627f\u5c42\u6b21\u7ed3\u6784\u770b\u4e0a\u53bb\u50cf\u4e00\u4e2a\u83f1\u5f62\u3002 \u4f8b\u5982\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class PoweredDevice { }; class Scanner : public PoweredDevice { }; class Printer : public PoweredDevice { }; class Copier : public Scanner , public Printer { }; \u626b\u63cf\u4eea\u548c\u6253\u5370\u673a\u90fd\u662f\u7535\u5668\uff0c\u56e0\u6b64\u5b83\u4eec\u7ee7\u627f PoweredDevice \u3002\u7136\u800c\uff0c\u590d\u5370\u673a\u7ed3\u5408\u4e86\u626b\u63cf\u4eea\u548c\u6253\u5370\u673a\u7684\u529f\u80fd\u3002 \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f1a\u51fa\u73b0\u8bb8\u591a\u95ee\u9898\uff1a\u4f8b\u5982\uff0c\u590d\u5370\u673a\u4e2d\u662f\u5426\u5e94\u8be5\u6709\u4e24\u4efdPoweredDevice\uff0c\u4ee5\u53ca\u5982\u4f55\u89e3\u51b3\u67d0\u4e9b\u7c7b\u578b\u7684\u6a21\u7cca\u5f15\u7528\u3002\u867d\u7136\u8fd9\u4e9b\u95ee\u9898\u4e2d\u7684\u5927\u591a\u6570\u90fd\u53ef\u4ee5\u901a\u8fc7\u663e\u5f0f\u4f5c\u7528\u57df\u6765\u89e3\u51b3\uff0c\u4f46\u4e3a\u4e86\u5904\u7406\u589e\u52a0\u7684\u590d\u6742\u6027\u800c\u5411\u7c7b\u6dfb\u52a0\u7684\u7ef4\u62a4\u5f00\u9500\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5f00\u53d1\u65f6\u95f4\u6025\u5267\u589e\u52a0\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u7ae0\u4e2d\u8ba8\u8bba\u66f4\u591a\u89e3\u51b3\u83f1\u5f62\u7ee7\u627f\u95ee\u9898\u7684\u65b9\u6cd5( 18.8 -- Virtual base classes )\u3002 \u591a\u91cd\u7ee7\u627f\u662f\u5426\u5f0a\u5927\u4e8e\u5229\uff1f \u4e8b\u5b9e\u8bc1\u660e\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u91cd\u7ee7\u627f\u89e3\u51b3\u7684\u5927\u591a\u6570\u95ee\u9898\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5355\u4e00\u7ee7\u627f\u89e3\u51b3\u3002\u8bb8\u591a\u9762\u5411\u5bf9\u8c61\u8bed\u8a00(\u5982\u3002Smalltalk, PHP)\u751a\u81f3\u4e0d\u652f\u6301\u591a\u91cd\u7ee7\u627f\u3002\u8bb8\u591a\u76f8\u5bf9\u73b0\u4ee3\u7684\u8bed\u8a00\uff0c\u5982Java\u548cc#\uff0c\u5c06\u7c7b\u9650\u5236\u4e3a\u666e\u901a\u7c7b\u7684\u5355\u4e00\u7ee7\u627f\uff0c\u4f46\u5141\u8bb8\u63a5\u53e3\u7c7b\u7684\u591a\u91cd\u7ee7\u627f(\u6211\u4eec\u5c06\u5728\u540e\u9762\u8ba8\u8bba)\u3002\u5728\u8fd9\u4e9b\u8bed\u8a00\u4e2d\u7981\u6b62\u591a\u91cd\u7ee7\u627f\u7684\u80cc\u540e\u7684\u9a71\u52a8\u601d\u60f3\u662f\uff0c\u5b83\u53ea\u4f1a\u4f7f\u8bed\u8a00\u8fc7\u4e8e\u590d\u6742\uff0c\u6700\u7ec8\u5bfc\u81f4\u7684\u95ee\u9898\u6bd4\u89e3\u51b3\u7684\u95ee\u9898\u66f4\u591a\u3002 \u8bb8\u591a\u4f5c\u8005\u548c\u6709\u7ecf\u9a8c\u7684\u7a0b\u5e8f\u5458\u8ba4\u4e3a\uff0c\u5e94\u8be5\u4e0d\u60dc\u4e00\u5207\u4ee3\u4ef7\u907f\u514dC++\u4e2d\u7684\u591a\u91cd\u7ee7\u627f\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5e26\u6765\u8bb8\u591a\u6f5c\u5728\u7684\u95ee\u9898\u3002\u7b14\u8005\u5e76\u4e0d\u540c\u610f\u8fd9\u79cd\u89c2\u70b9\uff0c\u56e0\u4e3a\u5728\u67d0\u4e9b\u65f6\u5019\u548c\u60c5\u51b5\u4e0b\uff0c\u591a\u91cd\u7ee7\u627f\u662f\u8fdb\u884c\u7684\u6700\u4f73\u65b9\u5f0f\u3002\u7136\u800c\uff0c\u591a\u91cd\u7ee7\u627f\u5e94\u8be5\u975e\u5e38\u660e\u667a\u5730\u4f7f\u7528\u3002 \u6709\u8da3\u7684\u662f\uff0c\u4f60\u5df2\u7ecf\u5728\u4f7f\u7528\u57fa\u4e8e\u591a\u91cd\u7ee7\u627f\u7f16\u5199\u7684\u7c7b\u800c\u4e0d\u81ea\u77e5\uff0c std:iostream \u5e93\u5bf9\u8c61 std::cin \u548c std::cout \u90fd\u662f\u4f7f\u7528\u591a\u91cd\u7ee7\u627f\u5b9e\u73b0\u7684\uff01 \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u591a\u91cd\u7ee7\u627f\uff0c\u9664\u975e\u5907\u9009\u65b9\u6848\u4f7f\u95ee\u9898\u53d8\u5f97\u66f4\u590d\u6742\u3002","title":"17.9 - \u591a\u91cd\u7ee7\u627f"},{"location":"17-9-multiple-inheritance/#mixins","text":"mixin (\u4e5f\u62fc\u4f5c \u201cmix-in\u201d) \u662f\u4e00\u4e2a\u5c0f\u7684\u7c7b\uff0c\u5b83\u53ef\u4ee5\u88ab\u7c7b\u7ee7\u627f\u4ee5\u4fbf\u4e3a\u5176\u6dfb\u52a0\u4e00\u4e9b\u5c5e\u6027\u3002\u5b83\u7684\u540d\u5b57mixin\uff08\u6df7\u5165\uff09\u8bf4\u660e\uff0c\u5b83\u7684\u7528\u9014\u662f\u88ab\u6dfb\u52a0\u5230\u5176\u4ed6\u7c7b\u4e2d\uff0c\u800c\u4e0d\u662f\u5355\u72ec\u88ab\u5b9e\u4f8b\u5316\u3002 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c Box \u548c Label \u7c7b\u90fd\u662f mixin\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f\u5b83\u4eec\u6765\u521b\u5efa\u65b0\u7684 Button class. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 // h/t to reader Waldo for this example #include struct Point2D { int x ; int y ; }; class Box // mixin Box class { public : void setTopLeft ( Point2D point ) { m_topLeft = point ; } void setBottomRight ( Point2D point ) { m_bottomRight = point ; } private : Point2D m_topLeft {}; Point2D m_bottomRight {}; }; class Label // mixin Label class { public : void setText ( const std :: string_view str ) { m_text = str ; } void setFontSize ( int fontSize ) { m_fontSize = fontSize ; } private : std :: string m_text {}; int m_fontSize {}; }; class Button : public Box , public Label {}; int main () { Button button {}; button . Box :: setTopLeft ({ 1 , 1 }); button . Box :: setBottomRight ({ 10 , 10 }); button . Label :: setText ( \"Username: \" ); button . Label :: setFontSize ( 6 ); } COPY \u6269\u5c55\u9605\u8bfb \u56e0\u4e3amixin\u88ab\u8bbe\u8ba1\u4e3a\u5411\u6d3e\u751f\u7c7b\u6dfb\u52a0\u529f\u80fd\uff0c\u800c\u4e0d\u662f\u63d0\u4f9b\u63a5\u53e3\uff0c\u6240\u4ee5mixin\u901a\u5e38\u4e0d\u4f7f\u7528\u865a\u51fd\u6570(\u4e0b\u4e00\u7ae0\u5c06\u8ba8\u8bba)\u3002\u76f8\u53cd\uff0c\u5982\u679c\u9700\u8981\u81ea\u5b9a\u4e49mixin\u7c7b\u4ee5\u7279\u5b9a\u7684\u65b9\u5f0f\u5de5\u4f5c\uff0c\u5219\u901a\u5e38\u4f7f\u7528\u6a21\u677f\u3002\u7531\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0cmixin\u7c7b\u901a\u5e38\u662f\u6a21\u677f\u5316\u7684\u3002 \u4e5f\u8bb8\u4ee4\u4eba\u60ca\u8bb6\u7684\u662f\uff0c\u6d3e\u751f\u7c7b\u53ef\u4ee5\u4f7f\u7528\u6d3e\u751f\u7c7b\u4f5c\u4e3a\u6a21\u677f\u7c7b\u578b\u53c2\u6570\u4ecemixin\u57fa\u7c7b\u7ee7\u627f\u3002\u8fd9\u6837\u7684\u7ee7\u627f\u88ab\u79f0\u4e3a\u5947\u602a\u7684\u5faa\u73af\u6a21\u677f\u6a21\u5f0f(\u7b80\u79f0CRTP)\uff0c\u5b83\u770b\u8d77\u6765\u50cf\u8fd9\u6837: 1 2 3 4 5 6 7 8 9 10 11 12 // The Curiously Recurring Template Pattern (CRTP) template < class T > class Mixin { // Mixin can use template type parameter T to access members of Derived // via (static_cast(this)) }; class Derived : public Mixin < Derived > { }; \u4f7f\u7528 CRTP \u7684\u7b80\u5355\u4f8b\u5b50\u53ef\u4ee5\u770b \u8fd9\u91cc .","title":"Mixins"},{"location":"17-9-multiple-inheritance/#_1","text":"\u867d\u7136\u591a\u91cd\u7ee7\u627f\u4f3c\u4e4e\u662f\u5355\u7ee7\u627f\u7684\u7b80\u5355\u6269\u5c55\uff0c\u4f46\u591a\u91cd\u7ee7\u627f\u5f15\u5165\u4e86\u8bb8\u591a\u95ee\u9898\uff0c\u8fd9\u4e9b\u95ee\u9898\u53ef\u80fd\u663e\u8457\u589e\u52a0\u7a0b\u5e8f\u7684\u590d\u6742\u6027\uff0c\u5e76\u4f7f\u5176\u6210\u4e3a\u7ef4\u62a4\u7684\u5669\u68a6\u3002\u8ba9\u6211\u4eec\u6765\u770b\u770b\u4e00\u4e9b\u5177\u4f53\u60c5\u51b5\u3002 \u9996\u5148\uff0c\u5f53\u591a\u4e2a\u57fa\u7c7b\u5305\u542b\u5177\u6709\u76f8\u540c\u540d\u79f0\u7684\u51fd\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u4ea7\u751f\u6b67\u4e49\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include class USBDevice { private : long m_id {}; public : USBDevice ( long id ) : m_id { id } { } long getID () const { return m_id ; } }; class NetworkDevice { private : long m_id {}; public : NetworkDevice ( long id ) : m_id { id } { } long getID () const { return m_id ; } }; class WirelessAdapter : public USBDevice , public NetworkDevice { public : WirelessAdapter ( long usbId , long networkId ) : USBDevice { usbId }, NetworkDevice { networkId } { } }; int main () { WirelessAdapter c54G { 5442 , 181742 }; std :: cout << c54G . getID (); // Which getID() do we call? return 0 ; } \u5f53 c54G.getID() \u7f16\u8bd1\u7684\u65f6\u5019\uff0c\u7f16\u8bd1\u5668\u4f1a\u67e5\u770b WirelessAdapter \u4e2d\u662f\u5426\u6709\u540d\u4e3a getID() \u7684\u51fd\u6570\u3002\u6ca1\u6709\u3002\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u67e5\u770b\u7236\u7c7b\u4e2d\u662f\u5426\u5177\u6709\u540d\u4e3a getID() \u7684\u51fd\u6570\u3002\u770b\u5230\u95ee\u9898\u4e86\u5417\uff1f\u95ee\u9898\u662f c54G \u5b9e\u9645\u4e0a\u5305\u542b\u4e24\u4e2a getID() \u51fd\u6570\uff1a\u4e00\u4e2a\u7ee7\u627f\u81ea USBDevice \uff0c \u53e6\u4e00\u4e2a\u7ee7\u627f\u81eaNetworkDevice \u3002\u56e0\u6b64\uff0c\u8fd9\u4e2a\u51fd\u6570\u8c03\u7528\u662f\u6a21\u7cca\u7684\uff0c\u5982\u679c\u8bd5\u56fe\u7f16\u8bd1\u5b83\uff0c\u4f60\u5c06\u6536\u5230\u4e00\u4e2a\u7f16\u8bd1\u5668\u9519\u8bef\u3002 1 2 3 4 5 6 7 int main () { WirelessAdapter c54G { 5442 , 181742 }; std :: cout << c54G . USBDevice :: getID (); return 0 ; } \u867d\u7136\u4e0a\u8ff0\u89e3\u51b3\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u4f46\u662f\u5f53\u60a8\u7684\u7c7b\u7ee7\u627f4\u4e2a\u62166\u4e2a\u57fa\u7c7b(\u8fd9\u4e9b\u57fa\u7c7b\u672c\u8eab\u7ee7\u627f\u5176\u4ed6\u7c7b)\u65f6\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u4e8b\u60c5\u4f1a\u53d8\u5f97\u591a\u4e48\u590d\u6742\u3002\u968f\u7740\u7ee7\u627f\u7684\u7c7b\u8d8a\u6765\u8d8a\u591a\uff0c\u547d\u540d\u51b2\u7a81\u7684\u53ef\u80fd\u6027\u5448\u6307\u6570\u7ea7\u589e\u957f\uff0c\u9700\u8981\u663e\u5f0f\u5730\u89e3\u51b3\u6bcf\u4e00\u4e2a\u547d\u540d\u51b2\u7a81\u3002 \u5176\u6b21\uff0c \u83f1\u5f62\u7ee7\u627f\u95ee\u9898 \u66f4\u52a0\u4e25\u91cd\uff0c\u7b14\u8005\u5c06\u5176\u79f0\u4e3a\u201d\u83f1\u5f62\u672b\u65e5\u201c\u3002\u5f53\u4e00\u4e2a\u7c7b\u4ece\u4e24\u4e2a\u7c7b\u7ee7\u627f\uff0c\u800c\u8fd9\u4e24\u4e2a\u7c7b\u53c8\u7ee7\u627f\u81ea\u4e00\u4e2a\u5171\u540c\u7684\u7c7b\u65f6\uff0c\u5c31\u4f1a\u51fa\u73b0\u83f1\u5f62\u7ee7\u627f\u95ee\u9898\u3002\u5176\u7ee7\u627f\u5c42\u6b21\u7ed3\u6784\u770b\u4e0a\u53bb\u50cf\u4e00\u4e2a\u83f1\u5f62\u3002 \u4f8b\u5982\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class PoweredDevice { }; class Scanner : public PoweredDevice { }; class Printer : public PoweredDevice { }; class Copier : public Scanner , public Printer { }; \u626b\u63cf\u4eea\u548c\u6253\u5370\u673a\u90fd\u662f\u7535\u5668\uff0c\u56e0\u6b64\u5b83\u4eec\u7ee7\u627f PoweredDevice \u3002\u7136\u800c\uff0c\u590d\u5370\u673a\u7ed3\u5408\u4e86\u626b\u63cf\u4eea\u548c\u6253\u5370\u673a\u7684\u529f\u80fd\u3002 \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f1a\u51fa\u73b0\u8bb8\u591a\u95ee\u9898\uff1a\u4f8b\u5982\uff0c\u590d\u5370\u673a\u4e2d\u662f\u5426\u5e94\u8be5\u6709\u4e24\u4efdPoweredDevice\uff0c\u4ee5\u53ca\u5982\u4f55\u89e3\u51b3\u67d0\u4e9b\u7c7b\u578b\u7684\u6a21\u7cca\u5f15\u7528\u3002\u867d\u7136\u8fd9\u4e9b\u95ee\u9898\u4e2d\u7684\u5927\u591a\u6570\u90fd\u53ef\u4ee5\u901a\u8fc7\u663e\u5f0f\u4f5c\u7528\u57df\u6765\u89e3\u51b3\uff0c\u4f46\u4e3a\u4e86\u5904\u7406\u589e\u52a0\u7684\u590d\u6742\u6027\u800c\u5411\u7c7b\u6dfb\u52a0\u7684\u7ef4\u62a4\u5f00\u9500\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5f00\u53d1\u65f6\u95f4\u6025\u5267\u589e\u52a0\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u7ae0\u4e2d\u8ba8\u8bba\u66f4\u591a\u89e3\u51b3\u83f1\u5f62\u7ee7\u627f\u95ee\u9898\u7684\u65b9\u6cd5( 18.8 -- Virtual base classes )\u3002","title":"\u591a\u91cd\u7ee7\u627f\u5e26\u6765\u7684\u95ee\u9898"},{"location":"17-9-multiple-inheritance/#_2","text":"\u4e8b\u5b9e\u8bc1\u660e\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u91cd\u7ee7\u627f\u89e3\u51b3\u7684\u5927\u591a\u6570\u95ee\u9898\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5355\u4e00\u7ee7\u627f\u89e3\u51b3\u3002\u8bb8\u591a\u9762\u5411\u5bf9\u8c61\u8bed\u8a00(\u5982\u3002Smalltalk, PHP)\u751a\u81f3\u4e0d\u652f\u6301\u591a\u91cd\u7ee7\u627f\u3002\u8bb8\u591a\u76f8\u5bf9\u73b0\u4ee3\u7684\u8bed\u8a00\uff0c\u5982Java\u548cc#\uff0c\u5c06\u7c7b\u9650\u5236\u4e3a\u666e\u901a\u7c7b\u7684\u5355\u4e00\u7ee7\u627f\uff0c\u4f46\u5141\u8bb8\u63a5\u53e3\u7c7b\u7684\u591a\u91cd\u7ee7\u627f(\u6211\u4eec\u5c06\u5728\u540e\u9762\u8ba8\u8bba)\u3002\u5728\u8fd9\u4e9b\u8bed\u8a00\u4e2d\u7981\u6b62\u591a\u91cd\u7ee7\u627f\u7684\u80cc\u540e\u7684\u9a71\u52a8\u601d\u60f3\u662f\uff0c\u5b83\u53ea\u4f1a\u4f7f\u8bed\u8a00\u8fc7\u4e8e\u590d\u6742\uff0c\u6700\u7ec8\u5bfc\u81f4\u7684\u95ee\u9898\u6bd4\u89e3\u51b3\u7684\u95ee\u9898\u66f4\u591a\u3002 \u8bb8\u591a\u4f5c\u8005\u548c\u6709\u7ecf\u9a8c\u7684\u7a0b\u5e8f\u5458\u8ba4\u4e3a\uff0c\u5e94\u8be5\u4e0d\u60dc\u4e00\u5207\u4ee3\u4ef7\u907f\u514dC++\u4e2d\u7684\u591a\u91cd\u7ee7\u627f\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5e26\u6765\u8bb8\u591a\u6f5c\u5728\u7684\u95ee\u9898\u3002\u7b14\u8005\u5e76\u4e0d\u540c\u610f\u8fd9\u79cd\u89c2\u70b9\uff0c\u56e0\u4e3a\u5728\u67d0\u4e9b\u65f6\u5019\u548c\u60c5\u51b5\u4e0b\uff0c\u591a\u91cd\u7ee7\u627f\u662f\u8fdb\u884c\u7684\u6700\u4f73\u65b9\u5f0f\u3002\u7136\u800c\uff0c\u591a\u91cd\u7ee7\u627f\u5e94\u8be5\u975e\u5e38\u660e\u667a\u5730\u4f7f\u7528\u3002 \u6709\u8da3\u7684\u662f\uff0c\u4f60\u5df2\u7ecf\u5728\u4f7f\u7528\u57fa\u4e8e\u591a\u91cd\u7ee7\u627f\u7f16\u5199\u7684\u7c7b\u800c\u4e0d\u81ea\u77e5\uff0c std:iostream \u5e93\u5bf9\u8c61 std::cin \u548c std::cout \u90fd\u662f\u4f7f\u7528\u591a\u91cd\u7ee7\u627f\u5b9e\u73b0\u7684\uff01 \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u591a\u91cd\u7ee7\u627f\uff0c\u9664\u975e\u5907\u9009\u65b9\u6848\u4f7f\u95ee\u9898\u53d8\u5f97\u66f4\u590d\u6742\u3002","title":"\u591a\u91cd\u7ee7\u627f\u662f\u5426\u5f0a\u5927\u4e8e\u5229\uff1f"},{"location":"17-x-Chapter-17-comprehensive-quiz/","tags":["summary"],"text":"\u590d\u4e60 17.1 - \u7ee7\u627f\u7b80\u4ecb 17.2 - C++\u7ee7\u627f\u57fa\u7840 \u901a\u8fc7\u7ee7\u627f\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u5728\u4e24\u4e2a\u5bf9\u8c61\u4e4b\u95f4\u5b9e\u73b0\u201dis-a\u201c\uff08\u662f\u4e00\u4e2a\uff09\u7684\u5173\u7cfb\u3002\u88ab\u7ee7\u627f\u7684\u5bf9\u8c61\u79f0\u4e3a\u7236\u7c7b\u6216\u8005\u57fa\u7c7b\uff0c\u6216\u8005\u8d85\u7c7b\u3002\u800c\u7ee7\u627f\u5f97\u5230\u7684\u5bf9\u8c61\u5219\u79f0\u4e3a\u5b50\u7c7b\u3001\u6d3e\u751f\u7c7b\u3002 \u5f53\u4e00\u4e2a\u7c7b\u4ece\u53e6\u4e00\u4e2a\u7c7b\u6d3e\u751f\u51fa\u6765\u65f6\uff0c\u5b83\u4f1a\u83b7\u5f97\u57fa\u7c7b\u7684\u5168\u90e8\u6210\u5458\u3002 17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f 17.4 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u548c\u521d\u59cb\u5316 \u5f53\u6d3e\u751f\u7c7b\u88ab\u6784\u9020\u65f6\uff0c\u5176\u5c5e\u4e8e\u57fa\u7c7b\u7684\u90e8\u5206\u4f1a\u9996\u5148\u88ab\u6784\u5efa\uff0c\u7136\u540e\u6d3e\u751f\u7c7b\u90e8\u5206\u624d\u4f1a\u6784\u5efa\u3002\u66f4\u591a\u5177\u4f53\u4fe1\u606f\u5982\u4e0b\uff1a \u6d3e\u751f\u7c7b\u7684\u5185\u5b58\u662f\u53e6\u5916\u5206\u914d\u7684\uff08\u8db3\u591f\u5b58\u653e\u57fa\u7c7b\u90e8\u5206\u7684\u6570\u636e\u548c\u6d3e\u751f\u7c7b\u90e8\u5206\u7684\u6570\u636e\uff09\uff1b \u8c03\u7528\u6d3e\u751f\u7c7b\u5408\u9002\u7684\u6784\u9020\u51fd\u6570\uff1b \u9996\u5148\u4f1a\u4f7f\u7528\u5408\u9002\u7684\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u6784\u9020\u57fa\u7c7b\u5bf9\u8c61\u3002\u5982\u679c\u57fa\u7c7b\u5bf9\u8c61\u6ca1\u6709\u88ab\u663e\u5f0f\u6307\u5b9a\uff0c\u5219\u8c03\u7528\u7684\u662f\u9ed8\u8ba4\u7684\u6784\u9020\u51fd\u6570\uff1b \u6d3e\u751f\u7c7b\u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u6765\u521d\u59cb\u5316\u5176\u6210\u5458\u53d8\u91cf\uff1b \u6267\u884c\u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\uff1b \u63a7\u5236\u6743\u4ea4\u8fd8\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002 \u6790\u6784\u7684\u987a\u5e8f\u5219\u6b63\u53f7\u76f8\u53cd\uff0c\u5148\u6790\u6784\u6d3e\u751f\u7c7b\u518d\u6790\u6784\u57fa\u7c7b\u3002 17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 C++ \u652f\u63013\u4e2a\u8bbf\u95ee\u63a7\u5236\u8bf4\u660e\u7b26\uff1a \u516c\u6709\u6210\u5458 \u3001 \u79c1\u6709\u6210\u5458 \u548c \u53d7\u4fdd\u62a4\u6210\u5458 \u3002protected \u5173\u952e\u5b57\u4fee\u9970\u7684\u6210\u5458\uff0c\u5141\u8bb8\u8be5\u7c7b\u3001 \u53cb\u5143\u7c7b \u548c\u6d3e\u751f\u7c7b\u8bbf\u95ee\uff0c\u4f46\u662f\u4e0d\u80fd\u88ab\u5176\u4ed6\u7c7b\u8bbf\u95ee\u3002 \u7c7b\u4e5f\u53ef\u4ee5\u901a\u8fc7 \u516c\u5f00\u7ee7\u627f \u3001 \u79c1\u6709\u7ee7\u627f \u6216 \u53d7\u4fdd\u62a4\u7ee7\u627f \u3002\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7c7b\u7684\u7ee7\u627f\u90fd\u662f\u516c\u5f00\u7684\u3002 \u4e0b\u8868\u5217\u4e3e\u4e86\u4e0d\u540c\u8bbf\u95ee\u8bf4\u660e\u7b26\u548c\u7ee7\u627f\u7c7b\u578b\u7684\u7ec4\u5408\uff1a \u57fa\u7c7b\u4e2d\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u516c\u5f00\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u79c1\u6709\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u4fdd\u62a4\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u516c\u5f00 \u516c\u5f00 \u79c1\u6709 \u4fdd\u62a4 \u79c1\u6709 \u4e0d\u53ef\u8bbf\u95ee \u4e0d\u53ef\u8bbf\u95ee \u4e0d\u53ef\u8bbf\u95ee \u4fdd\u62a4 \u4fdd\u62a4 \u79c1\u6709 \u4fdd\u62a4 17.6 - \u4e3a\u6d3e\u751f\u7c7b\u6dfb\u52a0\u65b0\u529f\u80fd 17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570 \u6d3e\u751f\u7c7b\u4e2d\u53ef\u4ee5\u6dfb\u52a0\u65b0\u7684\u51fd\u6570\uff0c\u4fee\u6539\u57fa\u7c7b\u7684\u51fd\u6570\uff0c\u4fee\u6539\u7ee7\u627f\u7684\u6210\u5458\u7684\u8bbf\u95ee\u89c4\u5219\u6216\u8005\u9690\u85cf\u67d0\u4e9b\u51fd\u6570\u3002 17.9 - \u591a\u91cd\u7ee7\u627f \u5f53\u6d3e\u751f\u7c7b\u4ece\u591a\u4e2a\u7236\u7c7b\u8fdb\u884c\u57fa\u7840\u65f6\uff0c\u79f0\u4e3a[[\u591a\u91cd\u7ee7\u627f]]\u3002\u4e00\u822c\u6765\u8bf4\u5e94\u8be5\u907f\u514d\u591a\u91cd\u7ee7\u627f\uff0c\u9664\u975e\u5b83\u80fd\u591f\u6781\u5927\u5730\u964d\u4f4e\u8bbe\u8ba1\u590d\u6742\u5ea6\u3002","title":"17.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7ee7\u627f"},{"location":"17-x-Chapter-17-comprehensive-quiz/#_1","text":"17.1 - \u7ee7\u627f\u7b80\u4ecb 17.2 - C++\u7ee7\u627f\u57fa\u7840 \u901a\u8fc7\u7ee7\u627f\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u5728\u4e24\u4e2a\u5bf9\u8c61\u4e4b\u95f4\u5b9e\u73b0\u201dis-a\u201c\uff08\u662f\u4e00\u4e2a\uff09\u7684\u5173\u7cfb\u3002\u88ab\u7ee7\u627f\u7684\u5bf9\u8c61\u79f0\u4e3a\u7236\u7c7b\u6216\u8005\u57fa\u7c7b\uff0c\u6216\u8005\u8d85\u7c7b\u3002\u800c\u7ee7\u627f\u5f97\u5230\u7684\u5bf9\u8c61\u5219\u79f0\u4e3a\u5b50\u7c7b\u3001\u6d3e\u751f\u7c7b\u3002 \u5f53\u4e00\u4e2a\u7c7b\u4ece\u53e6\u4e00\u4e2a\u7c7b\u6d3e\u751f\u51fa\u6765\u65f6\uff0c\u5b83\u4f1a\u83b7\u5f97\u57fa\u7c7b\u7684\u5168\u90e8\u6210\u5458\u3002 17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f 17.4 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u548c\u521d\u59cb\u5316 \u5f53\u6d3e\u751f\u7c7b\u88ab\u6784\u9020\u65f6\uff0c\u5176\u5c5e\u4e8e\u57fa\u7c7b\u7684\u90e8\u5206\u4f1a\u9996\u5148\u88ab\u6784\u5efa\uff0c\u7136\u540e\u6d3e\u751f\u7c7b\u90e8\u5206\u624d\u4f1a\u6784\u5efa\u3002\u66f4\u591a\u5177\u4f53\u4fe1\u606f\u5982\u4e0b\uff1a \u6d3e\u751f\u7c7b\u7684\u5185\u5b58\u662f\u53e6\u5916\u5206\u914d\u7684\uff08\u8db3\u591f\u5b58\u653e\u57fa\u7c7b\u90e8\u5206\u7684\u6570\u636e\u548c\u6d3e\u751f\u7c7b\u90e8\u5206\u7684\u6570\u636e\uff09\uff1b \u8c03\u7528\u6d3e\u751f\u7c7b\u5408\u9002\u7684\u6784\u9020\u51fd\u6570\uff1b \u9996\u5148\u4f1a\u4f7f\u7528\u5408\u9002\u7684\u57fa\u7c7b\u6784\u9020\u51fd\u6570\u6784\u9020\u57fa\u7c7b\u5bf9\u8c61\u3002\u5982\u679c\u57fa\u7c7b\u5bf9\u8c61\u6ca1\u6709\u88ab\u663e\u5f0f\u6307\u5b9a\uff0c\u5219\u8c03\u7528\u7684\u662f\u9ed8\u8ba4\u7684\u6784\u9020\u51fd\u6570\uff1b \u6d3e\u751f\u7c7b\u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u6765\u521d\u59cb\u5316\u5176\u6210\u5458\u53d8\u91cf\uff1b \u6267\u884c\u6d3e\u751f\u7c7b\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\uff1b \u63a7\u5236\u6743\u4ea4\u8fd8\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002 \u6790\u6784\u7684\u987a\u5e8f\u5219\u6b63\u53f7\u76f8\u53cd\uff0c\u5148\u6790\u6784\u6d3e\u751f\u7c7b\u518d\u6790\u6784\u57fa\u7c7b\u3002 17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 C++ \u652f\u63013\u4e2a\u8bbf\u95ee\u63a7\u5236\u8bf4\u660e\u7b26\uff1a \u516c\u6709\u6210\u5458 \u3001 \u79c1\u6709\u6210\u5458 \u548c \u53d7\u4fdd\u62a4\u6210\u5458 \u3002protected \u5173\u952e\u5b57\u4fee\u9970\u7684\u6210\u5458\uff0c\u5141\u8bb8\u8be5\u7c7b\u3001 \u53cb\u5143\u7c7b \u548c\u6d3e\u751f\u7c7b\u8bbf\u95ee\uff0c\u4f46\u662f\u4e0d\u80fd\u88ab\u5176\u4ed6\u7c7b\u8bbf\u95ee\u3002 \u7c7b\u4e5f\u53ef\u4ee5\u901a\u8fc7 \u516c\u5f00\u7ee7\u627f \u3001 \u79c1\u6709\u7ee7\u627f \u6216 \u53d7\u4fdd\u62a4\u7ee7\u627f \u3002\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7c7b\u7684\u7ee7\u627f\u90fd\u662f\u516c\u5f00\u7684\u3002 \u4e0b\u8868\u5217\u4e3e\u4e86\u4e0d\u540c\u8bbf\u95ee\u8bf4\u660e\u7b26\u548c\u7ee7\u627f\u7c7b\u578b\u7684\u7ec4\u5408\uff1a \u57fa\u7c7b\u4e2d\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u516c\u5f00\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u79c1\u6709\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u4fdd\u62a4\u7ee7\u627f\u65f6\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26 \u516c\u5f00 \u516c\u5f00 \u79c1\u6709 \u4fdd\u62a4 \u79c1\u6709 \u4e0d\u53ef\u8bbf\u95ee \u4e0d\u53ef\u8bbf\u95ee \u4e0d\u53ef\u8bbf\u95ee \u4fdd\u62a4 \u4fdd\u62a4 \u79c1\u6709 \u4fdd\u62a4 17.6 - \u4e3a\u6d3e\u751f\u7c7b\u6dfb\u52a0\u65b0\u529f\u80fd 17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570 \u6d3e\u751f\u7c7b\u4e2d\u53ef\u4ee5\u6dfb\u52a0\u65b0\u7684\u51fd\u6570\uff0c\u4fee\u6539\u57fa\u7c7b\u7684\u51fd\u6570\uff0c\u4fee\u6539\u7ee7\u627f\u7684\u6210\u5458\u7684\u8bbf\u95ee\u89c4\u5219\u6216\u8005\u9690\u85cf\u67d0\u4e9b\u51fd\u6570\u3002 17.9 - \u591a\u91cd\u7ee7\u627f \u5f53\u6d3e\u751f\u7c7b\u4ece\u591a\u4e2a\u7236\u7c7b\u8fdb\u884c\u57fa\u7840\u65f6\uff0c\u79f0\u4e3a[[\u591a\u91cd\u7ee7\u627f]]\u3002\u4e00\u822c\u6765\u8bf4\u5e94\u8be5\u907f\u514d\u591a\u91cd\u7ee7\u627f\uff0c\u9664\u975e\u5b83\u80fd\u591f\u6781\u5927\u5730\u964d\u4f4e\u8bbe\u8ba1\u590d\u6742\u5ea6\u3002","title":"\u590d\u4e60"},{"location":"18-1-pointers-and-references-to-the-base-class-of-derived-objects/","tags":["pointers","references"],"text":"\u5728\u524d\u9762\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u901a\u8fc7 \u7ee7\u627f \u6765\u6d3e\u751f\u65b0\u7684\u7c7b\u3002\u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u7ee7\u627f\u4e2d\u6700\u91cd\u8981\u4e5f\u662f\u6700\u5f3a\u5927\u7684\u4e00\u9762\u2014\u2014\u865a\u51fd\u6570\u3002 \u5728\u6211\u4eec\u8ba8\u8bba\u865a\u51fd\u6570\u4e4b\u524d\uff0c\u5148\u6765\u4e86\u89e3\u4e00\u4e0b\uff0c\u4e3a\u4ec0\u4e48\u865a\u51fd\u6570\u662f\u6709\u7528\u7684\u3002 \u901a\u8fc7 17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f \u7684\u5b66\u4e60\uff0c\u6211\u4eec\u6307\u5b9a\uff0c\u5f53\u521b\u5efa\u4e00\u4e2a\u6d3e\u751f\u7c7b\u65f6\uff0c\u6d3e\u751f\u7c7b\u5b9e\u9645\u4e0a\u662f\u7531\u591a\u4e2a\u90e8\u5206\u7ec4\u6210\u7684\uff1a\u4e00\u90e8\u5206\u5c5e\u4e8e\u5176\u7236\u7c7b\uff0c\u53e6\u4e00\u90e8\u5206\u5c5e\u4e8e\u5b83\u81ea\u5df1\u3002 \u4f8b\u5982\uff0c\u8fd9\u91cc\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include class Base { protected : int m_value {}; public : Base ( int value ) : m_value { value } { } std :: string_view getName () const { return \"Base\" ; } int getValue () const { return m_value ; } }; class Derived : public Base { public : Derived ( int value ) : Base { value } { } std :: string_view getName () const { return \"Derived\" ; } int getValueDoubled () const { return m_value * 2 ; } }; \u5f53\u6211\u4eec\u521b\u5efa Derived \u5bf9\u8c61\u65f6\uff0c\u5b83\u5305\u542b Base \u7684\u90e8\u5206\uff08\u9996\u5148\u6784\u5efa\uff09\u4ee5\u53ca\u5c5e\u4e8e Derived \u7684\u90e8\u5206 (\u968f\u540e\u6784\u5efa)\u3002\u6ce8\u610f\uff0c\u7ee7\u627f\u5173\u7cfb\u9690\u542b\u7684\u662f\u5bf9\u8c61A\u201c\u662f\u4e00\u79cd\u201d\u5bf9\u8c61B\u7684\u542b\u4e49\u3002\u56e0\u4e3a Derived \u662f\u4e00\u4e2a Base \uff0c\u6240\u4ee5 Derived \u5305\u542b Base \u7684\u90e8\u5206\u662f\u5408\u7406\u7684\u3002 \u6307\u9488\uff0c\u5f15\u7528\u548c\u6d3e\u751f\u7c7b \u521b\u5efa\u4e00\u4e2a\u6307\u5411 Derived \u5bf9\u8c61\u7684\u6307\u9488\u548c\u5f15\u7528\u5f88\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { Derived derived { 5 }; std :: cout << \"derived is a \" << derived . getName () << \" and has value \" << derived . getValue () << '\\n' ; Derived & rDerived { derived }; std :: cout << \"rDerived is a \" << rDerived . getName () << \" and has value \" << rDerived . getValue () << '\\n' ; Derived * pDerived { & derived }; std :: cout << \"pDerived is a \" << pDerived -> getName () << \" and has value \" << pDerived -> getValue () << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 derived is a Derived and has value 5 rDerived is a Derived and has value 5 pDerived is a Derived and has value 5 \u4e0d\u8fc7\uff0c\u7531\u4e8e Derived \u4e2d\u5305\u542b\u5c5e\u4e8e Base \u7684\u90e8\u5206\uff0c\u90a3\u4e48\u6211\u4eec\u4e0d\u7981\u8981\u95ee\uff1aC++\u5141\u8bb8\u6211\u4eec\u5c06 Base \u7c7b\u578b\u6307\u9488\u6307\u5411 Derived \u5bf9\u8c61\u5417\uff1f\u7b54\u6848\u662f\u53ef\u4ee5\u7684\uff01 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { Derived derived { 5 }; // These are both legal! Base & rBase { derived }; Base * pBase { & derived }; std :: cout << \"derived is a \" << derived . getName () << \" and has value \" << derived . getValue () << '\\n' ; std :: cout << \"rBase is a \" << rBase . getName () << \" and has value \" << rBase . getValue () << '\\n' ; std :: cout << \"pBase is a \" << pBase -> getName () << \" and has value \" << pBase -> getValue () << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 derived is a Derived and has value 5 rBase is a Base and has value 5 pBase is a Base and has value 5 \u8f93\u51fa\u7ed3\u679c\u53ef\u80fd\u548c\u4f60\u60f3\u7684\u5e76\u4e0d\u4e00\u6837\uff01 \u4e8b\u5b9e\u8bc1\u660e\uff0c\u7531\u4e8e rBase \u548c pBase \u662f Base \u5f15\u7528\u548c\u6307\u9488\uff0c\u5b83\u4eec\u53ea\u80fd\u770b\u5230 Base \u7684\u6210\u5458(\u6216 Base \u7236\u7c7b\u4eec\u7684\u6210\u5458)\u3002\u56e0\u6b64\uff0c\u5373\u4f7f Derived::getName() \u906e\u853d(\u9690\u85cf)\u4e86 Derived \u5bf9\u8c61\u7684 Base::getName() \uff0c Base \u6307\u9488/\u5f15\u7528\u4e5f\u4e0d\u80fd\u770b\u5230 Derived::getName() \u3002\u56e0\u6b64\uff0c\u5b83\u4f1a\u8c03\u7528 Base::getName() \uff0c\u8fd9\u5c31\u662f rBase \u548c pBase \u6253\u5370\u5b83\u4eec\u662f Base \u800c\u4e0d\u662f Derived \u7684\u539f\u56e0\u3002 \u6ce8\u610f\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u4f60\u4e0d\u80fd\u901a\u8fc7 rBase \u6216\u8005 pBase \u6765\u8c03\u7528 Derived::getValueDoubled() \u3002\u56e0\u4e3a\u5b83\u4eec\u770b\u4e0d\u5230\u8be5\u51fd\u6570\u3002 \u8fd9\u662f\u53e6\u4e00\u4e2a\u7a0d\u5fae\u590d\u6742\u4e00\u70b9\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\u4e2d\u4f7f\u7528\u5b83\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 #include #include #include class Animal { protected : std :: string m_name ; // \u5c06\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u8bbe\u7f6e\u4e3a\u53d7\u4fdd\u62a4\u7684\uff0c\u56e0\u4e3a // \u6211\u4eec\u4e0d\u5e0c\u671b\u7528\u6237\u80fd\u591f\u76f4\u63a5\u521b\u5efa Animal \u5bf9\u8c61\uff0c // \u4f46\u662f\u6211\u4eec\u4ecd\u7136\u5e0c\u671b\u80fd\u591f\u4f7f\u7528\u5b83\u6765\u6d3e\u751f\u7c7b\u3002 Animal ( std :: string_view name ) : m_name { name } { } // To prevent slicing (covered later) Animal ( const Animal & ) = default ; Animal & operator = ( const Animal & ) = default ; public : std :: string_view getName () const { return m_name ; } std :: string_view speak () const { return \"???\" ; } }; class Cat : public Animal { public : Cat ( std :: string_view name ) : Animal { name } { } std :: string_view speak () const { return \"Meow\" ; } }; class Dog : public Animal { public : Dog ( std :: string_view name ) : Animal { name } { } std :: string_view speak () const { return \"Woof\" ; } }; int main () { const Cat cat { \"Fred\" }; std :: cout << \"cat is named \" << cat . getName () << \", and it says \" << cat . speak () << '\\n' ; const Dog dog { \"Garbo\" }; std :: cout << \"dog is named \" << dog . getName () << \", and it says \" << dog . speak () << '\\n' ; const Animal * pAnimal { & cat }; std :: cout << \"pAnimal is named \" << pAnimal -> getName () << \", and it says \" << pAnimal -> speak () << '\\n' ; pAnimal = & dog ; std :: cout << \"pAnimal is named \" << pAnimal -> getName () << \", and it says \" << pAnimal -> speak () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c 1 2 3 4 cat is named Fred, and it says Meow dog is named Garbo, and it says Woof pAnimal is named Fred, and it says ??? pAnimal is named Garbo, and it says ??? \u548c\u4e4b\u524d\u7684\u95ee\u9898\u7c7b\u4f3c\uff0c\u56e0\u4e3a pAnimal \u662f\u4e00\u4e2a Animal \u7c7b\u578b\u7684\u6307\u9488\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u770b\u5230 Animal \u7684\u90e8\u5206\u3002\u5176\u7ed3\u679c\u5c31\u662f pAnimal->speak() \u8c03\u7528\u7684\u662f Animal::speak() \u800c\u4e0d\u662f Dog::Speak() \u6216\u8005 Cat::speak() \u3002 \u4f7f\u7528\u6307\u5411\u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 \u73b0\u5728\u4f60\u53ef\u80fd\u4f1a\u8bf4\uff0c\u201c\u4e0a\u9762\u7684\u4f8b\u5b50\u770b\u8d77\u6765\u6709\u70b9\u50bb\u3002\u5f53\u6211\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u6d3e\u751f\u5bf9\u8c61\u65f6\uff0c\u4e3a\u4ec0\u4e48\u8981\u8bbe\u7f6e\u6307\u5411\u6d3e\u751f\u5bf9\u8c61\u57fa\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\u5462?\u201d\u4e8b\u5b9e\u8bc1\u660e\uff0c\u8fd9\u4e48\u505a\u5f88\u6709\u7528\u3002 \u9996\u5148\uff0c\u5047\u8bbe\u4f60\u60f3\u8981\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u6253\u5370\u52a8\u7269\u7684\u540d\u5b57\u548c\u58f0\u97f3\u3002\u5982\u679c\u4e0d\u4f7f\u7528\u6307\u5411\u57fa\u7c7b\u7684\u6307\u9488\uff0c\u4f60\u5c31\u5fc5\u987b\u4f7f\u7528\u91cd\u8f7d\u51fd\u6570\u6765\u7f16\u5199\u5b83\uff0c\u50cf\u8fd9\u6837: 1 2 3 4 5 6 7 8 9 10 //\u51fd\u6570\u91cd\u8f7d\uff08\u7b7e\u540d\u4e0d\u540c\uff09 void report ( const Cat & cat ) { std :: cout << cat . getName () << \" says \" << cat . speak () << '\\n' ; } void report ( const Dog & dog ) { std :: cout << dog . getName () << \" says \" << dog . speak () << '\\n' ; } \u597d\u50cf\u6ca1\u5565\u95ee\u9898\uff0c\u4f46\u60f3\u60f3\u5982\u679c\u6211\u4eec\u4e0d\u6b62\u6709\u4e24\u79cd\u52a8\u7269\u800c\u662f\u670930\u79cd\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002\u4f60\u5fc5\u987b\u7f16\u519930\u4e2a\u51e0\u4e4e\u76f8\u540c\u7684\u51fd\u6570\uff01\u53e6\u5916\uff0c\u5982\u679c\u4f60\u6dfb\u52a0\u4e86\u4e00\u79cd\u65b0\u7684\u52a8\u7269\u7c7b\u578b\uff0c\u4f60\u4e5f\u5fc5\u987b\u4e3a\u5b83\u5199\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u3002\u8003\u8651\u5230\u8fd9\u4e9b\u51fd\u6570\u7684\u552f\u4e00\u533a\u522b\u5c31\u662f\u53c2\u6570\u4e0d\u540c\uff0c\u8fd9\u662f\u65e0\u7591\u662f\u4e00\u79cd\u5de8\u5927\u7684\u6d6a\u8d39\u3002 \u4e0d\u8fc7\uff0c\u56e0\u4e3a Cat \u548c Dog \u90fd\u6d3e\u751f\u81ea Animal \uff0c Cat \u548c Dog \u4e2d\u4e00\u5b9a\u5177\u6709\u5c5e\u4e8e Animal \u7684\u90e8\u5206\u3002\u56e0\u6b64\uff0c\u5982\u679c\u80fd\u50cf\u4e0b\u9762\u8fd9\u4e48\u505a\u5c31\u597d\u4e86\uff1a 1 2 3 4 void report ( const Animal & rAnimal ) { std :: cout << rAnimal . getName () << \" says \" << rAnimal . speak () << '\\n' ; } \u8fd9\u5c06\u5141\u8bb8\u6211\u4eec\u4f20\u5165\u4ece Animal \u6d3e\u751f\u7684\u4efb\u4f55\u7c7b\uff0c\u751a\u81f3\u662f\u5728\u7f16\u5199\u51fd\u6570\u4e4b\u540e\u521b\u5efa\u7684\u7c7b\uff01\u4e0d\u9700\u8981\u4e3a\u6bcf\u4e2a\u6d3e\u751f\u7c7b\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u800c\u662f\u4e0e\u6240\u6709\u6d3e\u751f\u81ea Animal \u7684\u7c7b\u53ef\u4ee5\u5171\u7528\u7684\u4e00\u4e2a\u51fd\u6570\uff01 \u5f53\u7136\uff0c\u8fd9\u4e48\u505a\u6700\u5927\u7684\u95ee\u9898\u5728\u4e8e rAnimal \u662f Animal \u7684\u5f15\u7528\uff0c\u6240\u4ee5 rAnimal.speak() \u4f1a\u8c03\u7528 Animal::speak() \u800c\u4e0d\u662f\u6d3e\u751f\u7c7b\u4e2d\u7684 speak() \u3002 \u5176\u6b21\uff0c\u5047\u8bbe\u4f60\u67093\u53ea\u732b\u548c3\u53ea\u72d7\uff0c\u4f60\u60f3\u628a\u5b83\u4eec\u653e\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\u4ee5\u4fbf\u4e8e\u8bbf\u95ee\u3002\u56e0\u4e3a\u6570\u7ec4\u53ea\u80fd\u4fdd\u5b58\u4e00\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u6ca1\u6709\u6307\u5411\u57fa\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u4f60\u5fc5\u987b\u4e3a\u6bcf\u4e2a\u6d3e\u751f\u7c7b\u578b\u521b\u5efa\u4e0d\u540c\u7684\u6570\u7ec4\uff0c\u50cf\u8fd9\u6837: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include // Cat and Dog from the example above int main () { const auto & cats { std :: to_array < Cat > ({{ \"Fred\" }, { \"Misty\" }, { \"Zeke\" }}) }; const auto & dogs { std :: to_array < Dog > ({{ \"Garbo\" }, { \"Pooky\" }, { \"Truffle\" }}) }; // Before C++20 // const std::array cats{{ { \"Fred\" }, { \"Misty\" }, { \"Zeke\" } }}; // const std::array dogs{{ { \"Garbo\" }, { \"Pooky\" }, { \"Truffle\" } }}; for ( const auto & cat : cats ) { std :: cout << cat . getName () << \" says \" << cat . speak () << '\\n' ; } for ( const auto & dog : dogs ) { std :: cout << dog . getName () << \" says \" << dog . speak () << '\\n' ; } return 0 ; } \u5982\u679c\u670930\u79cd\u4e0d\u540c\u7684\u52a8\u7269\u5462\uff1f\u5c82\u4e0d\u662f\u8981\u5b9a\u4e4930\u4e2a\u6570\u7ec4\uff0c\u6bcf\u4e2a\u52a8\u7269\u90fd\u9700\u8981\u4e00\u4e2a\u6570\u7ec4\uff01 \u4e0d\u8fc7\uff0c\u56e0\u4e3a Cat \u548c Dog \u90fd\u6d3e\u751f\u81ea\u4e8e Animal \uff0c\u6240\u4ee5\u53ef\u4ee5\u8fd9\u6837\u505a: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include #include // Cat and Dog from the example above int main () { const Cat fred { \"Fred\" }; const Cat misty { \"Misty\" }; const Cat zeke { \"Zeke\" }; const Dog garbo { \"Garbo\" }; const Dog pooky { \"Pooky\" }; const Dog truffle { \"Truffle\" }; // Set up an array of pointers to animals, and set those pointers to our Cat and Dog objects // Note: to_array requires C++20 support (and at the time of writing, Visual Studio 2022 still doesn't support it correctly) const auto animals { std :: to_array < const Animal *> ({ & fred , & garbo , & misty , & pooky , & truffle , & zeke }) }; // Before C++20, with the array size being explicitly specified // const std::array animals{ &fred, &garbo, &misty, &pooky, &truffle, &zeke }; for ( const auto animal : animals ) { std :: cout << animal -> getName () << \" says \" << animal -> speak () << '\\n' ; } return 0 ; } \u5c3d\u7ba1\u4e0a\u8ff0\u4ee3\u7801\u53ef\u4ee5\u7f16\u8bd1\u548c\u6267\u884c\uff0c\u4f46\u53ef\u60dc\u7684\u662f\uff0c\u7531\u4e8e\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f Animal \u7c7b\u578b\u7684\u6307\u9488\uff0c\u90a3\u5c31\u610f\u5473\u7740 animal->speak() \u8c03\u7528\u7684\u4f1a\u662f Animal::speak() \u800c\u4e0d\u662f\u6d3e\u751f\u7c7b\u5404\u81ea\u7684 speak() \u51fd\u6570\u3002\u8f93\u51fa\u7ed3\u679c\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 Fred says ??? Garbo says ??? Misty says ??? Pooky says ??? Truffle says ??? Zeke says ??? \u867d\u7136\u8fd9\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8282\u7701\u5927\u91cf\u7684\u65f6\u95f4\u548c\u7cbe\u529b\uff0c\u4f46\u5b83\u4eec\u90fd\u6709\u76f8\u540c\u7684\u95ee\u9898\u3002\u57fa\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\u8c03\u7528\u51fd\u6570\u7684\u57fa\u7c7b\u7248\u672c\u800c\u4e0d\u662f\u6d3e\u751f\u7248\u672c\u3002\u8981\u662f\u6709\u529e\u6cd5\u8ba9\u8fd9\u4e9b\u57fa\u6307\u9488\u8c03\u7528\u51fd\u6570\u7684\u6d3e\u751f\u7248\u672c\u800c\u4e0d\u662f\u57fa\u7248\u672c\u5c31\u597d\u4e86\u2026 \u4f60\u731c\uff0c\u865a\u51fd\u6570\u662f\u505a\u4ec0\u4e48\u7528\u7684\uff1f\uff08\u4e0d\u4f1a\u90a3\u4e48\u5de7\u5427\uff09\u3002","title":"18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528"},{"location":"18-1-pointers-and-references-to-the-base-class-of-derived-objects/#_1","text":"\u521b\u5efa\u4e00\u4e2a\u6307\u5411 Derived \u5bf9\u8c61\u7684\u6307\u9488\u548c\u5f15\u7528\u5f88\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { Derived derived { 5 }; std :: cout << \"derived is a \" << derived . getName () << \" and has value \" << derived . getValue () << '\\n' ; Derived & rDerived { derived }; std :: cout << \"rDerived is a \" << rDerived . getName () << \" and has value \" << rDerived . getValue () << '\\n' ; Derived * pDerived { & derived }; std :: cout << \"pDerived is a \" << pDerived -> getName () << \" and has value \" << pDerived -> getValue () << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 derived is a Derived and has value 5 rDerived is a Derived and has value 5 pDerived is a Derived and has value 5 \u4e0d\u8fc7\uff0c\u7531\u4e8e Derived \u4e2d\u5305\u542b\u5c5e\u4e8e Base \u7684\u90e8\u5206\uff0c\u90a3\u4e48\u6211\u4eec\u4e0d\u7981\u8981\u95ee\uff1aC++\u5141\u8bb8\u6211\u4eec\u5c06 Base \u7c7b\u578b\u6307\u9488\u6307\u5411 Derived \u5bf9\u8c61\u5417\uff1f\u7b54\u6848\u662f\u53ef\u4ee5\u7684\uff01 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { Derived derived { 5 }; // These are both legal! Base & rBase { derived }; Base * pBase { & derived }; std :: cout << \"derived is a \" << derived . getName () << \" and has value \" << derived . getValue () << '\\n' ; std :: cout << \"rBase is a \" << rBase . getName () << \" and has value \" << rBase . getValue () << '\\n' ; std :: cout << \"pBase is a \" << pBase -> getName () << \" and has value \" << pBase -> getValue () << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 derived is a Derived and has value 5 rBase is a Base and has value 5 pBase is a Base and has value 5 \u8f93\u51fa\u7ed3\u679c\u53ef\u80fd\u548c\u4f60\u60f3\u7684\u5e76\u4e0d\u4e00\u6837\uff01 \u4e8b\u5b9e\u8bc1\u660e\uff0c\u7531\u4e8e rBase \u548c pBase \u662f Base \u5f15\u7528\u548c\u6307\u9488\uff0c\u5b83\u4eec\u53ea\u80fd\u770b\u5230 Base \u7684\u6210\u5458(\u6216 Base \u7236\u7c7b\u4eec\u7684\u6210\u5458)\u3002\u56e0\u6b64\uff0c\u5373\u4f7f Derived::getName() \u906e\u853d(\u9690\u85cf)\u4e86 Derived \u5bf9\u8c61\u7684 Base::getName() \uff0c Base \u6307\u9488/\u5f15\u7528\u4e5f\u4e0d\u80fd\u770b\u5230 Derived::getName() \u3002\u56e0\u6b64\uff0c\u5b83\u4f1a\u8c03\u7528 Base::getName() \uff0c\u8fd9\u5c31\u662f rBase \u548c pBase \u6253\u5370\u5b83\u4eec\u662f Base \u800c\u4e0d\u662f Derived \u7684\u539f\u56e0\u3002 \u6ce8\u610f\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u4f60\u4e0d\u80fd\u901a\u8fc7 rBase \u6216\u8005 pBase \u6765\u8c03\u7528 Derived::getValueDoubled() \u3002\u56e0\u4e3a\u5b83\u4eec\u770b\u4e0d\u5230\u8be5\u51fd\u6570\u3002 \u8fd9\u662f\u53e6\u4e00\u4e2a\u7a0d\u5fae\u590d\u6742\u4e00\u70b9\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\u4e2d\u4f7f\u7528\u5b83\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 #include #include #include class Animal { protected : std :: string m_name ; // \u5c06\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u8bbe\u7f6e\u4e3a\u53d7\u4fdd\u62a4\u7684\uff0c\u56e0\u4e3a // \u6211\u4eec\u4e0d\u5e0c\u671b\u7528\u6237\u80fd\u591f\u76f4\u63a5\u521b\u5efa Animal \u5bf9\u8c61\uff0c // \u4f46\u662f\u6211\u4eec\u4ecd\u7136\u5e0c\u671b\u80fd\u591f\u4f7f\u7528\u5b83\u6765\u6d3e\u751f\u7c7b\u3002 Animal ( std :: string_view name ) : m_name { name } { } // To prevent slicing (covered later) Animal ( const Animal & ) = default ; Animal & operator = ( const Animal & ) = default ; public : std :: string_view getName () const { return m_name ; } std :: string_view speak () const { return \"???\" ; } }; class Cat : public Animal { public : Cat ( std :: string_view name ) : Animal { name } { } std :: string_view speak () const { return \"Meow\" ; } }; class Dog : public Animal { public : Dog ( std :: string_view name ) : Animal { name } { } std :: string_view speak () const { return \"Woof\" ; } }; int main () { const Cat cat { \"Fred\" }; std :: cout << \"cat is named \" << cat . getName () << \", and it says \" << cat . speak () << '\\n' ; const Dog dog { \"Garbo\" }; std :: cout << \"dog is named \" << dog . getName () << \", and it says \" << dog . speak () << '\\n' ; const Animal * pAnimal { & cat }; std :: cout << \"pAnimal is named \" << pAnimal -> getName () << \", and it says \" << pAnimal -> speak () << '\\n' ; pAnimal = & dog ; std :: cout << \"pAnimal is named \" << pAnimal -> getName () << \", and it says \" << pAnimal -> speak () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c 1 2 3 4 cat is named Fred, and it says Meow dog is named Garbo, and it says Woof pAnimal is named Fred, and it says ??? pAnimal is named Garbo, and it says ??? \u548c\u4e4b\u524d\u7684\u95ee\u9898\u7c7b\u4f3c\uff0c\u56e0\u4e3a pAnimal \u662f\u4e00\u4e2a Animal \u7c7b\u578b\u7684\u6307\u9488\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u770b\u5230 Animal \u7684\u90e8\u5206\u3002\u5176\u7ed3\u679c\u5c31\u662f pAnimal->speak() \u8c03\u7528\u7684\u662f Animal::speak() \u800c\u4e0d\u662f Dog::Speak() \u6216\u8005 Cat::speak() \u3002","title":"\u6307\u9488\uff0c\u5f15\u7528\u548c\u6d3e\u751f\u7c7b"},{"location":"18-1-pointers-and-references-to-the-base-class-of-derived-objects/#_2","text":"\u73b0\u5728\u4f60\u53ef\u80fd\u4f1a\u8bf4\uff0c\u201c\u4e0a\u9762\u7684\u4f8b\u5b50\u770b\u8d77\u6765\u6709\u70b9\u50bb\u3002\u5f53\u6211\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u6d3e\u751f\u5bf9\u8c61\u65f6\uff0c\u4e3a\u4ec0\u4e48\u8981\u8bbe\u7f6e\u6307\u5411\u6d3e\u751f\u5bf9\u8c61\u57fa\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\u5462?\u201d\u4e8b\u5b9e\u8bc1\u660e\uff0c\u8fd9\u4e48\u505a\u5f88\u6709\u7528\u3002 \u9996\u5148\uff0c\u5047\u8bbe\u4f60\u60f3\u8981\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u6253\u5370\u52a8\u7269\u7684\u540d\u5b57\u548c\u58f0\u97f3\u3002\u5982\u679c\u4e0d\u4f7f\u7528\u6307\u5411\u57fa\u7c7b\u7684\u6307\u9488\uff0c\u4f60\u5c31\u5fc5\u987b\u4f7f\u7528\u91cd\u8f7d\u51fd\u6570\u6765\u7f16\u5199\u5b83\uff0c\u50cf\u8fd9\u6837: 1 2 3 4 5 6 7 8 9 10 //\u51fd\u6570\u91cd\u8f7d\uff08\u7b7e\u540d\u4e0d\u540c\uff09 void report ( const Cat & cat ) { std :: cout << cat . getName () << \" says \" << cat . speak () << '\\n' ; } void report ( const Dog & dog ) { std :: cout << dog . getName () << \" says \" << dog . speak () << '\\n' ; } \u597d\u50cf\u6ca1\u5565\u95ee\u9898\uff0c\u4f46\u60f3\u60f3\u5982\u679c\u6211\u4eec\u4e0d\u6b62\u6709\u4e24\u79cd\u52a8\u7269\u800c\u662f\u670930\u79cd\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002\u4f60\u5fc5\u987b\u7f16\u519930\u4e2a\u51e0\u4e4e\u76f8\u540c\u7684\u51fd\u6570\uff01\u53e6\u5916\uff0c\u5982\u679c\u4f60\u6dfb\u52a0\u4e86\u4e00\u79cd\u65b0\u7684\u52a8\u7269\u7c7b\u578b\uff0c\u4f60\u4e5f\u5fc5\u987b\u4e3a\u5b83\u5199\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u3002\u8003\u8651\u5230\u8fd9\u4e9b\u51fd\u6570\u7684\u552f\u4e00\u533a\u522b\u5c31\u662f\u53c2\u6570\u4e0d\u540c\uff0c\u8fd9\u662f\u65e0\u7591\u662f\u4e00\u79cd\u5de8\u5927\u7684\u6d6a\u8d39\u3002 \u4e0d\u8fc7\uff0c\u56e0\u4e3a Cat \u548c Dog \u90fd\u6d3e\u751f\u81ea Animal \uff0c Cat \u548c Dog \u4e2d\u4e00\u5b9a\u5177\u6709\u5c5e\u4e8e Animal \u7684\u90e8\u5206\u3002\u56e0\u6b64\uff0c\u5982\u679c\u80fd\u50cf\u4e0b\u9762\u8fd9\u4e48\u505a\u5c31\u597d\u4e86\uff1a 1 2 3 4 void report ( const Animal & rAnimal ) { std :: cout << rAnimal . getName () << \" says \" << rAnimal . speak () << '\\n' ; } \u8fd9\u5c06\u5141\u8bb8\u6211\u4eec\u4f20\u5165\u4ece Animal \u6d3e\u751f\u7684\u4efb\u4f55\u7c7b\uff0c\u751a\u81f3\u662f\u5728\u7f16\u5199\u51fd\u6570\u4e4b\u540e\u521b\u5efa\u7684\u7c7b\uff01\u4e0d\u9700\u8981\u4e3a\u6bcf\u4e2a\u6d3e\u751f\u7c7b\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u800c\u662f\u4e0e\u6240\u6709\u6d3e\u751f\u81ea Animal \u7684\u7c7b\u53ef\u4ee5\u5171\u7528\u7684\u4e00\u4e2a\u51fd\u6570\uff01 \u5f53\u7136\uff0c\u8fd9\u4e48\u505a\u6700\u5927\u7684\u95ee\u9898\u5728\u4e8e rAnimal \u662f Animal \u7684\u5f15\u7528\uff0c\u6240\u4ee5 rAnimal.speak() \u4f1a\u8c03\u7528 Animal::speak() \u800c\u4e0d\u662f\u6d3e\u751f\u7c7b\u4e2d\u7684 speak() \u3002 \u5176\u6b21\uff0c\u5047\u8bbe\u4f60\u67093\u53ea\u732b\u548c3\u53ea\u72d7\uff0c\u4f60\u60f3\u628a\u5b83\u4eec\u653e\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\u4ee5\u4fbf\u4e8e\u8bbf\u95ee\u3002\u56e0\u4e3a\u6570\u7ec4\u53ea\u80fd\u4fdd\u5b58\u4e00\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u6ca1\u6709\u6307\u5411\u57fa\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u4f60\u5fc5\u987b\u4e3a\u6bcf\u4e2a\u6d3e\u751f\u7c7b\u578b\u521b\u5efa\u4e0d\u540c\u7684\u6570\u7ec4\uff0c\u50cf\u8fd9\u6837: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include // Cat and Dog from the example above int main () { const auto & cats { std :: to_array < Cat > ({{ \"Fred\" }, { \"Misty\" }, { \"Zeke\" }}) }; const auto & dogs { std :: to_array < Dog > ({{ \"Garbo\" }, { \"Pooky\" }, { \"Truffle\" }}) }; // Before C++20 // const std::array cats{{ { \"Fred\" }, { \"Misty\" }, { \"Zeke\" } }}; // const std::array dogs{{ { \"Garbo\" }, { \"Pooky\" }, { \"Truffle\" } }}; for ( const auto & cat : cats ) { std :: cout << cat . getName () << \" says \" << cat . speak () << '\\n' ; } for ( const auto & dog : dogs ) { std :: cout << dog . getName () << \" says \" << dog . speak () << '\\n' ; } return 0 ; } \u5982\u679c\u670930\u79cd\u4e0d\u540c\u7684\u52a8\u7269\u5462\uff1f\u5c82\u4e0d\u662f\u8981\u5b9a\u4e4930\u4e2a\u6570\u7ec4\uff0c\u6bcf\u4e2a\u52a8\u7269\u90fd\u9700\u8981\u4e00\u4e2a\u6570\u7ec4\uff01 \u4e0d\u8fc7\uff0c\u56e0\u4e3a Cat \u548c Dog \u90fd\u6d3e\u751f\u81ea\u4e8e Animal \uff0c\u6240\u4ee5\u53ef\u4ee5\u8fd9\u6837\u505a: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include #include // Cat and Dog from the example above int main () { const Cat fred { \"Fred\" }; const Cat misty { \"Misty\" }; const Cat zeke { \"Zeke\" }; const Dog garbo { \"Garbo\" }; const Dog pooky { \"Pooky\" }; const Dog truffle { \"Truffle\" }; // Set up an array of pointers to animals, and set those pointers to our Cat and Dog objects // Note: to_array requires C++20 support (and at the time of writing, Visual Studio 2022 still doesn't support it correctly) const auto animals { std :: to_array < const Animal *> ({ & fred , & garbo , & misty , & pooky , & truffle , & zeke }) }; // Before C++20, with the array size being explicitly specified // const std::array animals{ &fred, &garbo, &misty, &pooky, &truffle, &zeke }; for ( const auto animal : animals ) { std :: cout << animal -> getName () << \" says \" << animal -> speak () << '\\n' ; } return 0 ; } \u5c3d\u7ba1\u4e0a\u8ff0\u4ee3\u7801\u53ef\u4ee5\u7f16\u8bd1\u548c\u6267\u884c\uff0c\u4f46\u53ef\u60dc\u7684\u662f\uff0c\u7531\u4e8e\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f Animal \u7c7b\u578b\u7684\u6307\u9488\uff0c\u90a3\u5c31\u610f\u5473\u7740 animal->speak() \u8c03\u7528\u7684\u4f1a\u662f Animal::speak() \u800c\u4e0d\u662f\u6d3e\u751f\u7c7b\u5404\u81ea\u7684 speak() \u51fd\u6570\u3002\u8f93\u51fa\u7ed3\u679c\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 Fred says ??? Garbo says ??? Misty says ??? Pooky says ??? Truffle says ??? Zeke says ??? \u867d\u7136\u8fd9\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8282\u7701\u5927\u91cf\u7684\u65f6\u95f4\u548c\u7cbe\u529b\uff0c\u4f46\u5b83\u4eec\u90fd\u6709\u76f8\u540c\u7684\u95ee\u9898\u3002\u57fa\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\u8c03\u7528\u51fd\u6570\u7684\u57fa\u7c7b\u7248\u672c\u800c\u4e0d\u662f\u6d3e\u751f\u7248\u672c\u3002\u8981\u662f\u6709\u529e\u6cd5\u8ba9\u8fd9\u4e9b\u57fa\u6307\u9488\u8c03\u7528\u51fd\u6570\u7684\u6d3e\u751f\u7248\u672c\u800c\u4e0d\u662f\u57fa\u7248\u672c\u5c31\u597d\u4e86\u2026 \u4f60\u731c\uff0c\u865a\u51fd\u6570\u662f\u505a\u4ec0\u4e48\u7528\u7684\uff1f\uff08\u4e0d\u4f1a\u90a3\u4e48\u5de7\u5427\uff09\u3002","title":"\u4f7f\u7528\u6307\u5411\u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528"},{"location":"18-10-dynamic-casting/","tags":["dynamic casting"],"text":"Key Takeaway \u65e9\u5728 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast \u4e2d\u6211\u4eec\u5c31\u8ba8\u8bba\u8fc7\u7c7b\u578b\u8f6c\u6362\u7684\u8bdd\u9898\uff0c\u5f53\u65f6\u6211\u4eec\u4f7f\u7528 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u2014\u2014 static_cast \u5c06\u53d8\u91cf\u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b\u3002 \u672c\u8282\u8bfe\u6211\u4eec\u4f1a\u8ba8\u8bba\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\u7684\u8f6c\u6362\uff1a \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u2014\u2014 dynamic_cast . dynamic_cast \u7684\u5fc5\u8981\u6027 \u5728\u5904\u7406 \u591a\u6001 \u65f6\u7ecf\u5e38\u4f1a\u9047\u5230\u8fd9\u6837\u7684\u60c5\u51b5\uff1a\u4f60\u6709\u4e00\u4e2a\u6307\u5411\u57fa\u7c7b\u7684\u6307\u9488\uff0c\u4f46\u9700\u8981\u8bbf\u95ee\u5b58\u5728\u4e8e\u6d3e\u751f\u7c7b\u4e2d\u7684\u4e00\u4e9b\u4fe1\u606f\u3002 \u8003\u8651\u4ee5\u4e0b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 #include #include class Base { protected : int m_value {}; public : Base ( int value ) : m_value { value } { } virtual ~ Base () = default ; }; class Derived : public Base { protected : std :: string m_name {}; public : Derived ( int value , const std :: string & name ) : Base { value }, m_name { name } { } const std :: string & getName () const { return m_name ; } }; Base * getObject ( bool returnDerived ) { if ( returnDerived ) return new Derived { 1 , \"Apple\" }; else return new Base { 2 }; } int main () { Base * b { getObject ( true ) }; // \u5728\u8fd9\u91cc\u5982\u4f55\u6253\u5370\u6d3e\u751f\u7c7b\u5bf9\u8c61\u7684\u540d\u5b57\u5462\uff1f\u6b64\u65f6\u53ea\u6709\u4e00\u4e2a\u57fa\u7c7b\u7684\u6307\u9488 delete b ; return 0 ; } \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u51fd\u6570 getObject() \u603b\u662f\u8fd4\u56de\u4e00\u4e2a Base \u6307\u9488\uff0c\u4f46\u8be5\u6307\u9488\u53ef\u4ee5\u6307\u5411 Base \u5bf9\u8c61\u6216 Derived \u5bf9\u8c61\u3002\u5728\u6307\u9488\u6307\u5411\u6d3e\u751f\u5bf9\u8c61\u7684\u60c5\u51b5\u4e0b\uff0c\u5982\u4f55\u8c03\u7528 Derived::getName() ? \u4e00\u79cd\u65b9\u6cd5\u662f\u5411 Base \u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3a getName() \u7684\u865a\u51fd\u6570(\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u7528 Base \u6307\u9488/\u5f15\u7528\u8c03\u7528\u5b83\uff0c\u5e76\u52a8\u6001\u89e3\u6790\u4e3a Derived::getName() )\u3002\u4f46\u662f\u5982\u679c\u4f60\u7528\u4e00\u4e2a\u6307\u5411 Base \u5bf9\u8c61\u7684 Base \u6307\u9488/\u5f15\u7528\u8c03\u7528\u5b83\uff0c\u8fd9\u4e2a\u51fd\u6570\u53c8\u5e94\u8be5\u8fd4\u56de\u4ec0\u4e48\u5462\uff1f\u8fd4\u56de\u4ec0\u4e48\u503c\u90fd\u662f\u6ca1\u6709\u5b9e\u9645\u610f\u4e49\u7684\uff08\u4e5f\u4e0d\u80fd\u4e3a\u7eaf\u865a\u56e0\u4e3a\u57fa\u7c7b\u8981\u80fd\u591f\u88ab\u5b9e\u4f8b\u5316\uff09\u3002\u8fd9\u4e2a\u51fd\u6570\u5bf9\u57fa\u7c7b\u662f\u6ca1\u6709\u610f\u4e49\u7684\uff0c\u53ea\u6709\u6d3e\u751f\u7c7b\u9700\u8981\u8003\u8651\u5b9e\u73b0\u8be5\u51fd\u6570\uff0c\u90a3\u4e48\u6211\u4eec\u4e3a\u4ec0\u4e48\u8981\u7528\u8fd9\u4e2a\u51fd\u6570\u6765\u201c\u6c61\u67d3\u201d\u57fa\u7c7b\u5462\uff1f \u6211\u4eec\u77e5\u9053\uff0cC++ \u5141\u8bb8\u4f60\u5c06 Derived \u6307\u9488\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a Base \u6307\u9488(\u5b9e\u9645\u4e0a\uff0c getObject() \u6b63\u662f\u8fd9\u6837\u505a\u7684)\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u6709\u65f6\u88ab\u79f0\u4e3a \u5411\u4e0a\u8f6c\u6362(upcasting|) \u3002\u90a3\u4e48\uff0c\u662f\u5426\u6709\u4e00\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06 Base \u6307\u9488\u8f6c\u6362\u56de Derived \u7c7b\u6307\u9488\u5462\uff1f\u8fd9\u6837\u7684\u8bdd\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8be5\u6307\u9488\u8c03\u7528 Derived::getName() \uff0c\u800c\u4e0d\u5fc5\u4f9d\u8d56\u865a\u51fd\u6570\u89e3\u6790\u3002 dynamic_cast C++ \u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a dynamic_cast \u7684\u5f3a\u5236\u8f6c\u6362\u64cd\u4f5c\u7b26\uff0c\u53ef\u7528\u4e8e\u6b64\u76ee\u7684\u3002\u5c3d\u7ba1\u52a8\u6001\u5f3a\u5236\u8f6c\u6362\u6709\u4e00\u4e9b\u4e0d\u540c\u7684\u529f\u80fd\uff0c\u4f46\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u52a8\u6001\u5f3a\u5236\u8f6c\u6362\u6700\u5e38\u89c1\u7684\u7528\u9014\u662f\u5c06\u57fa\u7c7b\u6307\u9488\u8f6c\u6362\u4e3a\u6d3e\u751f\u7c7b\u6307\u9488\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a \u5411\u4e0b\u8f6c\u6362\uff08downcasting\uff09 \u3002 \u4f7f\u7528 dynamic_cast \u548c static_cast \u7c7b\u4f3c\u3002\u57fa\u4e8e\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 dynamic_cast \u5c06 Base \u6307\u9488\u8f6c\u6362\u4e3a Derived \u6307\u9488\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int main () { Base * b { getObject ( true ) }; Derived * d { dynamic_cast < Derived *> ( b ) }; // \u4f7f\u7528\u52a8\u6001\u8f6c\u6362\u5c06 Base \u6307\u9488\u8f6c\u6362\u4e3a Derived \u6307\u9488 std :: cout << \"The name of the Derived is: \" << d -> getName () << '\\n' ; delete b ; return 0 ; } \u6253\u5370\uff1a 1 The name of the Derived is: Apple dynamic_cast \u5931\u8d25 \u4e0a\u9762\u7684\u4f8b\u5b50\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\uff0c\u56e0\u4e3a b \u5b9e\u9645\u4e0a\u6307\u5411\u4e00\u4e2a Derived \u5bf9\u8c61\uff0c\u56e0\u6b64\u5c06 b \u8f6c\u6362\u4e3a Derived \u6307\u9488\u662f\u6210\u529f\u7684\u3002 \u7136\u800c\uff0c\u6211\u4eec\u505a\u4e86\u4e00\u4e2a\u76f8\u5f53\u5371\u9669\u7684\u5047\u8bbe\uff1a b \u6307\u5411\u4e00\u4e2a\u6d3e\u751f\u5bf9\u8c61\u3002\u5982\u679c b \u4e0d\u6307\u5411\u6d3e\u751f\u5bf9\u8c61\u5462\uff1f\u8fd9\u5f88\u5bb9\u6613\u901a\u8fc7\u5c06 getObject() \u7684\u53c2\u6570\u4ece true \u66f4\u6539\u4e3a false \u6765\u6d4b\u8bd5\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c getObject() \u5c06\u8fd4\u56de\u4e00\u4e2a Base \u5bf9\u8c61\u7684 Base \u6307\u9488\u3002\u5f53\u6211\u4eec\u5c1d\u8bd5\u5c06 dynamic_cast \u8f6c\u6362\u4e3a\u6d3e\u751f\u7c7b\u578b\u65f6\uff0c\u5b83\u4f1a\u5931\u8d25\uff0c\u56e0\u4e3a\u65e0\u6cd5\u8fdb\u884c\u8f6c\u6362\u3002 \u5982\u679c dynamic_cast \u5931\u8d25\uff0c\u5219\u8f6c\u6362\u7ed3\u679c\u4f1a\u662f\u4e00\u4e2a\u7a7a\u6307\u9488\u3002 \u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u68c0\u67e5\u7a7a\u6307\u9488\u7684\u7ed3\u679c\uff0c\u6240\u4ee5\u6211\u4eec\u8bbf\u95ee d->getName() \uff0c\u5b83\u5c06\u5c1d\u8bd5\u89e3\u5f15\u7528\u7a7a\u6307\u9488\uff0c\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a (\u53ef\u80fd\u662f\u5d29\u6e83)\u3002 \u4e3a\u4e86\u4f7f\u8fd9\u4e2a\u7a0b\u5e8f\u5b89\u5168\uff0c\u9700\u8981\u786e\u4fdd dynamic_cast \u7684\u7ed3\u679c\u5b9e\u9645\u4e0a\u662f\u6210\u529f\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 int main () { Base * b { getObject ( true ) }; Derived * d { dynamic_cast < Derived *> ( b ) }; // use dynamic cast to convert Base pointer into Derived pointer if ( d ) // \u786e\u4fdd d \u662f\u975e\u7a7a std :: cout << \"The name of the Derived is: \" << d -> getName () << '\\n' ; delete b ; return 0 ; } \u6cd5\u5219 \u603b\u662f\u8981\u901a\u8fc7\u68c0\u67e5\u8fd4\u56de\u503c\u662f\u5426\u4e3a\u7a7a\u6307\u9488\u6765\u786e\u4fdd\u52a8\u6001\u8f6c\u6362\u6210\u529f\u3002 \u6ce8\u610f\uff0c\u7531\u4e8e dynamic_cast \u5728\u8fd0\u884c\u65f6\u8fdb\u884c\u4e00\u4e9b\u4e00\u81f4\u6027\u68c0\u67e5(\u4ee5\u786e\u4fdd\u53ef\u4ee5\u8fdb\u884c\u8f6c\u6362)\uff0c\u56e0\u6b64\u4f7f\u7528 dynamic_cast \u786e\u5b9e\u4f1a\u5bfc\u81f4\u6027\u80fd\u635f\u5931\u3002 \u6b64\u5916\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u60c5\u5f62\uff0c\u6b64\u65f6\u4f7f\u7528 dynamic_cast \u8fdb\u884c \u5411\u4e0b\u8f6c\u6362 \u662f\u4e0d\u80fd\u6210\u529f\u7684\uff1a \u53d7\u4fdd\u62a4\u7ee7\u627f \u548c \u79c1\u6709\u7ee7\u627f \u7684\u7c7b\uff1b \u5bf9\u4e8e\u6ca1\u6709\u58f0\u660e\u6216\u7ee7\u627f\u4efb\u4f55\u865a\u51fd\u6570(\u56e0\u6b64\u6ca1\u6709\u865a\u8868)\u7684\u7c7b\uff1b \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6d89\u53ca \u865a\u57fa\u7c7b \u7684\u60c5\u51b5 (\u53c2\u8003 \u8fd9\u91cc \u67e5\u770b\u4e00\u4e9b\u60c5\u51b5\u7684\u793a\u4f8b\uff0c\u4ee5\u53ca\u5982\u4f55\u89e3\u51b3\u5b83\u4eec)\u3002 \u4f7f\u7528 static_cast \u8fdb\u884c\u5411\u4e0b\u8f6c\u6362 \u4e8b\u5b9e\u8bc1\u660e\uff0c\u5411\u4e0b\u8f6c\u6362\u4e5f\u53ef\u4ee5\u4f7f\u7528 static_cast \u5b8c\u6210\u3002 \u4e3b\u8981\u7684\u533a\u522b\u662f static_cast \u4e0d\u8fdb\u884c\u8fd0\u884c\u65f6\u7c7b\u578b\u68c0\u67e5 \uff0c\u4ee5\u786e\u4fdd\u60a8\u6240\u505a\u7684\u5de5\u4f5c\u662f\u6709\u610f\u4e49\u7684\u3002\u8fd9\u4f7f\u5f97\u4f7f\u7528 static_cast \u66f4\u5feb\uff0c\u4f46\u66f4\u5371\u9669\u3002\u5982\u679c\u5c06 Base* \u8f6c\u6362\u4e3a Derived* \uff0c\u5373\u4f7f\u57fa\u7c7b\u6307\u9488\u6ca1\u6709\u6307\u5411\u6d3e\u751f\u7c7b\u5bf9\u8c61\uff0c\u5b83\u4e5f\u4f1a\u201c\u6210\u529f\u201d\u3002\u5f53\u60a8\u5c1d\u8bd5\u8bbf\u95ee\u7ed3\u679c\u6d3e\u751f\u6307\u9488(\u5b9e\u9645\u4e0a\u6307\u5411 Base \u5bf9\u8c61)\u65f6\uff0c\u8fd9\u5c06\u5bfc\u81f4\u672a\u5b9a\u4e49\u7684\u884c\u4e3a\u3002 \u5982\u679c\u4f60\u53ef\u4ee5\u4fdd\u8bc1\u5411\u4e0b\u5f3a\u5236\u8f6c\u6362\u7684\u6307\u9488\u5c06\u6210\u529f\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528 static_cast \u3002\u8fd9\u91cc\u6709\u4e00\u4e2a(\u4e0d\u662f\u5f88\u597d\u7684)\u65b9\u6cd5\uff0c\u901a\u8fc7\u865a\u51fd\u6570\u5224\u65ad\u6240\u6307\u7684\u5bf9\u8c61\u662f\u4e0d\u662f\u6b63\u786e\u7684\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 #include #include // Class identifier enum class ClassID { base , derived // Others can be added here later }; class Base { protected : int m_value {}; public : Base ( int value ) : m_value { value } { } virtual ~ Base () = default ; virtual ClassID getClassID () const { return ClassID :: base ; } }; class Derived : public Base { protected : std :: string m_name {}; public : Derived ( int value , const std :: string & name ) : Base { value }, m_name { name } { } const std :: string & getName () const { return m_name ; } virtual ClassID getClassID () const { return ClassID :: derived ; } }; Base * getObject ( bool bReturnDerived ) { if ( bReturnDerived ) return new Derived { 1 , \"Apple\" }; else return new Base { 2 }; } int main () { Base * b { getObject ( true ) }; if ( b -> getClassID () == ClassID :: derived ) { // We already proved b is pointing to a Derived object, so this should always succeed Derived * d { static_cast < Derived *> ( b ) }; std :: cout << \"The name of the Derived is: \" << d -> getName () << '\\n' ; } delete b ; return 0 ; } \u867d\u7136\u7684\u786e\u6709\u7528\u4f46\u662f\u5b9e\u73b0\u8d77\u6765\u8fd8\u662f\u6709\u70b9\u8d39\u4e8b\u7684(\u8fd8\u8981\u4ed8\u51fa\u8c03\u7528\u865a\u51fd\u6570\u548c\u5904\u7406\u7ed3\u679c\u7684\u4ee3\u4ef7)\uff0c\u90a3\u4e48\u8fd8\u4e0d\u5982\u4f7f\u7528\u76f4\u63a5\u4f7f\u7528 dynamic_cast \u3002 dynamic_cast \u548c\u5f15\u7528 \u5c3d\u7ba1\u4e0a\u9762\u7684\u6240\u6709\u793a\u4f8b\u90fd\u4ee5\u6307\u9488\u7684\u52a8\u6001\u5f3a\u5236\u8f6c\u6362(\u8fd9\u66f4\u5e38\u89c1)\u4e3a\u4f8b\uff0c\u4f46 dynamic_cast \u4e5f\u53ef\u4ee5\u7528\u4e8e\u5f15\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include #include class Base { protected : int m_value ; public : Base ( int value ) : m_value { value } { } virtual ~ Base () = default ; }; class Derived : public Base { protected : std :: string m_name ; public : Derived ( int value , const std :: string & name ) : Base { value }, m_name { name } { } const std :: string & getName () const { return m_name ; } }; int main () { Derived apple { 1 , \"Apple\" }; // create an apple Base & b { apple }; // set base reference to object Derived & d { dynamic_cast < Derived &> ( b ) }; // \u5bf9\u5f15\u7528\u8fdb\u884c\u52a8\u6001\u8f6c\u6362 std :: cout << \"The name of the Derived is: \" << d . getName () << '\\n' ; // \u4f7f\u7528 d \u8bbf\u95ee Derived::getName through d return 0 ; } \u56e0\u4e3aC++\u4e2d\u6ca1\u6709\u201c\u7a7a\u5f15\u7528\u201d\uff0c\u6240\u6709 dynamic_cast \u5728\u5931\u8d25\u65f6\u4e0d\u80fd\u8fd4\u56de\u7a7a\u5f15\u7528\u3002\u6240\u4ee5\uff0c\u5982\u679c\u5f15\u7528\u7684 dynamic_cast \u5931\u8d25\uff0c\u5219\u4f1a\u629b\u51fa std::bad_cast \u7c7b\u578b\u7684\u5f02\u5e38\u3002\u6211\u4eec\u5c06\u5728\u672c\u6559\u7a0b\u540e\u9762\u8ba8\u8bba\u5f02\u5e38\u3002 dynamic_cast vs static_cast \u65b0\u624b\u7a0b\u5e8f\u5458\u6709\u65f6\u4f1a\u5bf9\u4f55\u65f6\u4f7f\u7528 static_cast \u548c dynamic_cast \u611f\u5230\u56f0\u60d1\u3002\u7b54\u6848\u5f88\u7b80\u5355\uff1a\u9664\u975e\u662f\u5411\u4e0b\u7c7b\u578b\u8f6c\u6362\uff08\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cdynamic_cast\u901a\u5e38\u662f\u66f4\u597d\u7684\u9009\u62e9\uff09\uff0c\u5426\u5219\u4e00\u5f8b\u4f7f\u7528 static_cast\u3002\u4f46\u662f\uff0c\u4f60\u8fd8\u5e94\u8be5\u8003\u8651\u5b8c\u5168\u907f\u514d\u5f3a\u5236\u8f6c\u6362\uff0c\u800c\u53ea\u4f7f\u7528\u865a\u51fd\u6570\u3002 \u5411\u4e0b\u8f6c\u6362 vs \u865a\u51fd\u6570 \u6709\u4e9b\u5f00\u53d1\u4eba\u5458\u8ba4\u4e3a dynamic_cast \u662f\u90aa\u6076\u7684\uff0c\u662f\u7c7b\u6ca1\u6709\u88ab\u8bbe\u8ba1\u597d\u7684\u7279\u5f81\u3002\u76f8\u53cd\uff0c\u8fd9\u4e9b\u7a0b\u5e8f\u5458\u8ba4\u4e3a\u5e94\u8be5\u4f7f\u7528\u865a\u51fd\u6570\u3002 \u4e00\u822c\u6765\u8bf4\uff0c \u4f7f\u7528\u865a\u51fd\u6570\u5e94\u8be5\u4f18\u5148\u4e8e\u5411\u4e0b\u8f6c\u6362 \u3002\u7136\u800c\uff0c\u6709\u65f6\u5019\u4f7f\u7528\u5411\u4e0b\u8f6c\u6362\u4f1a\u662f\u66f4\u597d\u7684\u9009\u62e9\uff1a \u5f53\u4f60\u4e0d\u80fd\u4fee\u6539\u57fa\u7c7b\u6765\u6dfb\u52a0\u865a\u51fd\u6570\u65f6(\u4f8b\u5982\uff0c\u56e0\u4e3a\u57fa\u7c7b\u662f\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206) \u5f53\u4f60\u9700\u8981\u8bbf\u95ee\u7279\u5b9a\u4e8e\u6d3e\u751f\u7c7b\u7684\u4e1c\u897f\u65f6(\u4f8b\u5982\uff0c\u4e00\u4e2a\u53ea\u5b58\u5728\u4e8e\u6d3e\u751f\u7c7b\u4e2d\u7684\u8bbf\u95ee\u51fd\u6570) \u5728\u57fa\u7c7b\u4e2d\u6dfb\u52a0\u865a\u51fd\u6570\u662f\u6ca1\u6709\u610f\u4e49\u7684(\u4f8b\u5982\uff0c\u57fa\u7c7b\u6ca1\u6709\u9002\u5f53\u7684\u8fd4\u56de\u503c)\u3002\u5982\u679c\u4e0d\u9700\u8981\u5b9e\u4f8b\u5316\u57fa\u7c7b\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u7eaf\u865a\u51fd\u6570\u3002 \u5bf9 dynamic_cast \u548c RTTI \u7684\u4e00\u4e9b\u8b66\u793a \u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f(RTTI) \u662fC++\u7684\u4e00\u4e2a\u7279\u6027\uff0c\u5b83\u5728\u8fd0\u884c\u65f6\u4f1a\u66b4\u9732\u5bf9\u8c61\u6570\u636e\u7c7b\u578b\u7684\u4fe1\u606f\u3002 dynamic_cast \u5229\u7528\u4e86\u7279\u6027\u3002\u56e0\u4e3aRTTI\u6709\u76f8\u5f53\u5927\u7684\u7a7a\u95f4\u6027\u80fd\u5f00\u9500\uff0c\u4e00\u4e9b\u7f16\u8bd1\u5668\u5141\u8bb8\u4f60\u5173\u95edRTTI\u4f5c\u4e3a\u4e00\u79cd\u4f18\u5316\u65b9\u5f0f\u3002\u4e0d\u7528\u8bf4\uff0c\u5982\u679c\u8fd9\u6837\u505a\uff0c dynamic_cast \u5c06\u4e0d\u80fd\u6b63\u786e\u5de5\u4f5c\u3002","title":"18.10 - \u52a8\u6001\u7c7b\u578b\u8f6c\u6362"},{"location":"18-10-dynamic-casting/#dynamic_cast","text":"\u5728\u5904\u7406 \u591a\u6001 \u65f6\u7ecf\u5e38\u4f1a\u9047\u5230\u8fd9\u6837\u7684\u60c5\u51b5\uff1a\u4f60\u6709\u4e00\u4e2a\u6307\u5411\u57fa\u7c7b\u7684\u6307\u9488\uff0c\u4f46\u9700\u8981\u8bbf\u95ee\u5b58\u5728\u4e8e\u6d3e\u751f\u7c7b\u4e2d\u7684\u4e00\u4e9b\u4fe1\u606f\u3002 \u8003\u8651\u4ee5\u4e0b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 #include #include class Base { protected : int m_value {}; public : Base ( int value ) : m_value { value } { } virtual ~ Base () = default ; }; class Derived : public Base { protected : std :: string m_name {}; public : Derived ( int value , const std :: string & name ) : Base { value }, m_name { name } { } const std :: string & getName () const { return m_name ; } }; Base * getObject ( bool returnDerived ) { if ( returnDerived ) return new Derived { 1 , \"Apple\" }; else return new Base { 2 }; } int main () { Base * b { getObject ( true ) }; // \u5728\u8fd9\u91cc\u5982\u4f55\u6253\u5370\u6d3e\u751f\u7c7b\u5bf9\u8c61\u7684\u540d\u5b57\u5462\uff1f\u6b64\u65f6\u53ea\u6709\u4e00\u4e2a\u57fa\u7c7b\u7684\u6307\u9488 delete b ; return 0 ; } \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u51fd\u6570 getObject() \u603b\u662f\u8fd4\u56de\u4e00\u4e2a Base \u6307\u9488\uff0c\u4f46\u8be5\u6307\u9488\u53ef\u4ee5\u6307\u5411 Base \u5bf9\u8c61\u6216 Derived \u5bf9\u8c61\u3002\u5728\u6307\u9488\u6307\u5411\u6d3e\u751f\u5bf9\u8c61\u7684\u60c5\u51b5\u4e0b\uff0c\u5982\u4f55\u8c03\u7528 Derived::getName() ? \u4e00\u79cd\u65b9\u6cd5\u662f\u5411 Base \u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3a getName() \u7684\u865a\u51fd\u6570(\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u7528 Base \u6307\u9488/\u5f15\u7528\u8c03\u7528\u5b83\uff0c\u5e76\u52a8\u6001\u89e3\u6790\u4e3a Derived::getName() )\u3002\u4f46\u662f\u5982\u679c\u4f60\u7528\u4e00\u4e2a\u6307\u5411 Base \u5bf9\u8c61\u7684 Base \u6307\u9488/\u5f15\u7528\u8c03\u7528\u5b83\uff0c\u8fd9\u4e2a\u51fd\u6570\u53c8\u5e94\u8be5\u8fd4\u56de\u4ec0\u4e48\u5462\uff1f\u8fd4\u56de\u4ec0\u4e48\u503c\u90fd\u662f\u6ca1\u6709\u5b9e\u9645\u610f\u4e49\u7684\uff08\u4e5f\u4e0d\u80fd\u4e3a\u7eaf\u865a\u56e0\u4e3a\u57fa\u7c7b\u8981\u80fd\u591f\u88ab\u5b9e\u4f8b\u5316\uff09\u3002\u8fd9\u4e2a\u51fd\u6570\u5bf9\u57fa\u7c7b\u662f\u6ca1\u6709\u610f\u4e49\u7684\uff0c\u53ea\u6709\u6d3e\u751f\u7c7b\u9700\u8981\u8003\u8651\u5b9e\u73b0\u8be5\u51fd\u6570\uff0c\u90a3\u4e48\u6211\u4eec\u4e3a\u4ec0\u4e48\u8981\u7528\u8fd9\u4e2a\u51fd\u6570\u6765\u201c\u6c61\u67d3\u201d\u57fa\u7c7b\u5462\uff1f \u6211\u4eec\u77e5\u9053\uff0cC++ \u5141\u8bb8\u4f60\u5c06 Derived \u6307\u9488\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a Base \u6307\u9488(\u5b9e\u9645\u4e0a\uff0c getObject() \u6b63\u662f\u8fd9\u6837\u505a\u7684)\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u6709\u65f6\u88ab\u79f0\u4e3a \u5411\u4e0a\u8f6c\u6362(upcasting|) \u3002\u90a3\u4e48\uff0c\u662f\u5426\u6709\u4e00\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06 Base \u6307\u9488\u8f6c\u6362\u56de Derived \u7c7b\u6307\u9488\u5462\uff1f\u8fd9\u6837\u7684\u8bdd\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8be5\u6307\u9488\u8c03\u7528 Derived::getName() \uff0c\u800c\u4e0d\u5fc5\u4f9d\u8d56\u865a\u51fd\u6570\u89e3\u6790\u3002","title":"dynamic_cast \u7684\u5fc5\u8981\u6027"},{"location":"18-10-dynamic-casting/#dynamic_cast_1","text":"C++ \u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a dynamic_cast \u7684\u5f3a\u5236\u8f6c\u6362\u64cd\u4f5c\u7b26\uff0c\u53ef\u7528\u4e8e\u6b64\u76ee\u7684\u3002\u5c3d\u7ba1\u52a8\u6001\u5f3a\u5236\u8f6c\u6362\u6709\u4e00\u4e9b\u4e0d\u540c\u7684\u529f\u80fd\uff0c\u4f46\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u52a8\u6001\u5f3a\u5236\u8f6c\u6362\u6700\u5e38\u89c1\u7684\u7528\u9014\u662f\u5c06\u57fa\u7c7b\u6307\u9488\u8f6c\u6362\u4e3a\u6d3e\u751f\u7c7b\u6307\u9488\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a \u5411\u4e0b\u8f6c\u6362\uff08downcasting\uff09 \u3002 \u4f7f\u7528 dynamic_cast \u548c static_cast \u7c7b\u4f3c\u3002\u57fa\u4e8e\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 dynamic_cast \u5c06 Base \u6307\u9488\u8f6c\u6362\u4e3a Derived \u6307\u9488\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int main () { Base * b { getObject ( true ) }; Derived * d { dynamic_cast < Derived *> ( b ) }; // \u4f7f\u7528\u52a8\u6001\u8f6c\u6362\u5c06 Base \u6307\u9488\u8f6c\u6362\u4e3a Derived \u6307\u9488 std :: cout << \"The name of the Derived is: \" << d -> getName () << '\\n' ; delete b ; return 0 ; } \u6253\u5370\uff1a 1 The name of the Derived is: Apple","title":"dynamic_cast"},{"location":"18-10-dynamic-casting/#dynamic_cast_2","text":"\u4e0a\u9762\u7684\u4f8b\u5b50\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\uff0c\u56e0\u4e3a b \u5b9e\u9645\u4e0a\u6307\u5411\u4e00\u4e2a Derived \u5bf9\u8c61\uff0c\u56e0\u6b64\u5c06 b \u8f6c\u6362\u4e3a Derived \u6307\u9488\u662f\u6210\u529f\u7684\u3002 \u7136\u800c\uff0c\u6211\u4eec\u505a\u4e86\u4e00\u4e2a\u76f8\u5f53\u5371\u9669\u7684\u5047\u8bbe\uff1a b \u6307\u5411\u4e00\u4e2a\u6d3e\u751f\u5bf9\u8c61\u3002\u5982\u679c b \u4e0d\u6307\u5411\u6d3e\u751f\u5bf9\u8c61\u5462\uff1f\u8fd9\u5f88\u5bb9\u6613\u901a\u8fc7\u5c06 getObject() \u7684\u53c2\u6570\u4ece true \u66f4\u6539\u4e3a false \u6765\u6d4b\u8bd5\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c getObject() \u5c06\u8fd4\u56de\u4e00\u4e2a Base \u5bf9\u8c61\u7684 Base \u6307\u9488\u3002\u5f53\u6211\u4eec\u5c1d\u8bd5\u5c06 dynamic_cast \u8f6c\u6362\u4e3a\u6d3e\u751f\u7c7b\u578b\u65f6\uff0c\u5b83\u4f1a\u5931\u8d25\uff0c\u56e0\u4e3a\u65e0\u6cd5\u8fdb\u884c\u8f6c\u6362\u3002 \u5982\u679c dynamic_cast \u5931\u8d25\uff0c\u5219\u8f6c\u6362\u7ed3\u679c\u4f1a\u662f\u4e00\u4e2a\u7a7a\u6307\u9488\u3002 \u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u68c0\u67e5\u7a7a\u6307\u9488\u7684\u7ed3\u679c\uff0c\u6240\u4ee5\u6211\u4eec\u8bbf\u95ee d->getName() \uff0c\u5b83\u5c06\u5c1d\u8bd5\u89e3\u5f15\u7528\u7a7a\u6307\u9488\uff0c\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a (\u53ef\u80fd\u662f\u5d29\u6e83)\u3002 \u4e3a\u4e86\u4f7f\u8fd9\u4e2a\u7a0b\u5e8f\u5b89\u5168\uff0c\u9700\u8981\u786e\u4fdd dynamic_cast \u7684\u7ed3\u679c\u5b9e\u9645\u4e0a\u662f\u6210\u529f\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 int main () { Base * b { getObject ( true ) }; Derived * d { dynamic_cast < Derived *> ( b ) }; // use dynamic cast to convert Base pointer into Derived pointer if ( d ) // \u786e\u4fdd d \u662f\u975e\u7a7a std :: cout << \"The name of the Derived is: \" << d -> getName () << '\\n' ; delete b ; return 0 ; } \u6cd5\u5219 \u603b\u662f\u8981\u901a\u8fc7\u68c0\u67e5\u8fd4\u56de\u503c\u662f\u5426\u4e3a\u7a7a\u6307\u9488\u6765\u786e\u4fdd\u52a8\u6001\u8f6c\u6362\u6210\u529f\u3002 \u6ce8\u610f\uff0c\u7531\u4e8e dynamic_cast \u5728\u8fd0\u884c\u65f6\u8fdb\u884c\u4e00\u4e9b\u4e00\u81f4\u6027\u68c0\u67e5(\u4ee5\u786e\u4fdd\u53ef\u4ee5\u8fdb\u884c\u8f6c\u6362)\uff0c\u56e0\u6b64\u4f7f\u7528 dynamic_cast \u786e\u5b9e\u4f1a\u5bfc\u81f4\u6027\u80fd\u635f\u5931\u3002 \u6b64\u5916\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u60c5\u5f62\uff0c\u6b64\u65f6\u4f7f\u7528 dynamic_cast \u8fdb\u884c \u5411\u4e0b\u8f6c\u6362 \u662f\u4e0d\u80fd\u6210\u529f\u7684\uff1a \u53d7\u4fdd\u62a4\u7ee7\u627f \u548c \u79c1\u6709\u7ee7\u627f \u7684\u7c7b\uff1b \u5bf9\u4e8e\u6ca1\u6709\u58f0\u660e\u6216\u7ee7\u627f\u4efb\u4f55\u865a\u51fd\u6570(\u56e0\u6b64\u6ca1\u6709\u865a\u8868)\u7684\u7c7b\uff1b \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6d89\u53ca \u865a\u57fa\u7c7b \u7684\u60c5\u51b5 (\u53c2\u8003 \u8fd9\u91cc \u67e5\u770b\u4e00\u4e9b\u60c5\u51b5\u7684\u793a\u4f8b\uff0c\u4ee5\u53ca\u5982\u4f55\u89e3\u51b3\u5b83\u4eec)\u3002","title":"dynamic_cast \u5931\u8d25"},{"location":"18-10-dynamic-casting/#static_cast","text":"\u4e8b\u5b9e\u8bc1\u660e\uff0c\u5411\u4e0b\u8f6c\u6362\u4e5f\u53ef\u4ee5\u4f7f\u7528 static_cast \u5b8c\u6210\u3002 \u4e3b\u8981\u7684\u533a\u522b\u662f static_cast \u4e0d\u8fdb\u884c\u8fd0\u884c\u65f6\u7c7b\u578b\u68c0\u67e5 \uff0c\u4ee5\u786e\u4fdd\u60a8\u6240\u505a\u7684\u5de5\u4f5c\u662f\u6709\u610f\u4e49\u7684\u3002\u8fd9\u4f7f\u5f97\u4f7f\u7528 static_cast \u66f4\u5feb\uff0c\u4f46\u66f4\u5371\u9669\u3002\u5982\u679c\u5c06 Base* \u8f6c\u6362\u4e3a Derived* \uff0c\u5373\u4f7f\u57fa\u7c7b\u6307\u9488\u6ca1\u6709\u6307\u5411\u6d3e\u751f\u7c7b\u5bf9\u8c61\uff0c\u5b83\u4e5f\u4f1a\u201c\u6210\u529f\u201d\u3002\u5f53\u60a8\u5c1d\u8bd5\u8bbf\u95ee\u7ed3\u679c\u6d3e\u751f\u6307\u9488(\u5b9e\u9645\u4e0a\u6307\u5411 Base \u5bf9\u8c61)\u65f6\uff0c\u8fd9\u5c06\u5bfc\u81f4\u672a\u5b9a\u4e49\u7684\u884c\u4e3a\u3002 \u5982\u679c\u4f60\u53ef\u4ee5\u4fdd\u8bc1\u5411\u4e0b\u5f3a\u5236\u8f6c\u6362\u7684\u6307\u9488\u5c06\u6210\u529f\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528 static_cast \u3002\u8fd9\u91cc\u6709\u4e00\u4e2a(\u4e0d\u662f\u5f88\u597d\u7684)\u65b9\u6cd5\uff0c\u901a\u8fc7\u865a\u51fd\u6570\u5224\u65ad\u6240\u6307\u7684\u5bf9\u8c61\u662f\u4e0d\u662f\u6b63\u786e\u7684\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 #include #include // Class identifier enum class ClassID { base , derived // Others can be added here later }; class Base { protected : int m_value {}; public : Base ( int value ) : m_value { value } { } virtual ~ Base () = default ; virtual ClassID getClassID () const { return ClassID :: base ; } }; class Derived : public Base { protected : std :: string m_name {}; public : Derived ( int value , const std :: string & name ) : Base { value }, m_name { name } { } const std :: string & getName () const { return m_name ; } virtual ClassID getClassID () const { return ClassID :: derived ; } }; Base * getObject ( bool bReturnDerived ) { if ( bReturnDerived ) return new Derived { 1 , \"Apple\" }; else return new Base { 2 }; } int main () { Base * b { getObject ( true ) }; if ( b -> getClassID () == ClassID :: derived ) { // We already proved b is pointing to a Derived object, so this should always succeed Derived * d { static_cast < Derived *> ( b ) }; std :: cout << \"The name of the Derived is: \" << d -> getName () << '\\n' ; } delete b ; return 0 ; } \u867d\u7136\u7684\u786e\u6709\u7528\u4f46\u662f\u5b9e\u73b0\u8d77\u6765\u8fd8\u662f\u6709\u70b9\u8d39\u4e8b\u7684(\u8fd8\u8981\u4ed8\u51fa\u8c03\u7528\u865a\u51fd\u6570\u548c\u5904\u7406\u7ed3\u679c\u7684\u4ee3\u4ef7)\uff0c\u90a3\u4e48\u8fd8\u4e0d\u5982\u4f7f\u7528\u76f4\u63a5\u4f7f\u7528 dynamic_cast \u3002","title":"\u4f7f\u7528 static_cast\u8fdb\u884c\u5411\u4e0b\u8f6c\u6362"},{"location":"18-10-dynamic-casting/#dynamic_cast_3","text":"\u5c3d\u7ba1\u4e0a\u9762\u7684\u6240\u6709\u793a\u4f8b\u90fd\u4ee5\u6307\u9488\u7684\u52a8\u6001\u5f3a\u5236\u8f6c\u6362(\u8fd9\u66f4\u5e38\u89c1)\u4e3a\u4f8b\uff0c\u4f46 dynamic_cast \u4e5f\u53ef\u4ee5\u7528\u4e8e\u5f15\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include #include class Base { protected : int m_value ; public : Base ( int value ) : m_value { value } { } virtual ~ Base () = default ; }; class Derived : public Base { protected : std :: string m_name ; public : Derived ( int value , const std :: string & name ) : Base { value }, m_name { name } { } const std :: string & getName () const { return m_name ; } }; int main () { Derived apple { 1 , \"Apple\" }; // create an apple Base & b { apple }; // set base reference to object Derived & d { dynamic_cast < Derived &> ( b ) }; // \u5bf9\u5f15\u7528\u8fdb\u884c\u52a8\u6001\u8f6c\u6362 std :: cout << \"The name of the Derived is: \" << d . getName () << '\\n' ; // \u4f7f\u7528 d \u8bbf\u95ee Derived::getName through d return 0 ; } \u56e0\u4e3aC++\u4e2d\u6ca1\u6709\u201c\u7a7a\u5f15\u7528\u201d\uff0c\u6240\u6709 dynamic_cast \u5728\u5931\u8d25\u65f6\u4e0d\u80fd\u8fd4\u56de\u7a7a\u5f15\u7528\u3002\u6240\u4ee5\uff0c\u5982\u679c\u5f15\u7528\u7684 dynamic_cast \u5931\u8d25\uff0c\u5219\u4f1a\u629b\u51fa std::bad_cast \u7c7b\u578b\u7684\u5f02\u5e38\u3002\u6211\u4eec\u5c06\u5728\u672c\u6559\u7a0b\u540e\u9762\u8ba8\u8bba\u5f02\u5e38\u3002","title":"dynamic_cast \u548c\u5f15\u7528"},{"location":"18-10-dynamic-casting/#dynamic_cast-vs-static_cast","text":"\u65b0\u624b\u7a0b\u5e8f\u5458\u6709\u65f6\u4f1a\u5bf9\u4f55\u65f6\u4f7f\u7528 static_cast \u548c dynamic_cast \u611f\u5230\u56f0\u60d1\u3002\u7b54\u6848\u5f88\u7b80\u5355\uff1a\u9664\u975e\u662f\u5411\u4e0b\u7c7b\u578b\u8f6c\u6362\uff08\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cdynamic_cast\u901a\u5e38\u662f\u66f4\u597d\u7684\u9009\u62e9\uff09\uff0c\u5426\u5219\u4e00\u5f8b\u4f7f\u7528 static_cast\u3002\u4f46\u662f\uff0c\u4f60\u8fd8\u5e94\u8be5\u8003\u8651\u5b8c\u5168\u907f\u514d\u5f3a\u5236\u8f6c\u6362\uff0c\u800c\u53ea\u4f7f\u7528\u865a\u51fd\u6570\u3002","title":"dynamic_cast vs static_cast"},{"location":"18-10-dynamic-casting/#vs","text":"\u6709\u4e9b\u5f00\u53d1\u4eba\u5458\u8ba4\u4e3a dynamic_cast \u662f\u90aa\u6076\u7684\uff0c\u662f\u7c7b\u6ca1\u6709\u88ab\u8bbe\u8ba1\u597d\u7684\u7279\u5f81\u3002\u76f8\u53cd\uff0c\u8fd9\u4e9b\u7a0b\u5e8f\u5458\u8ba4\u4e3a\u5e94\u8be5\u4f7f\u7528\u865a\u51fd\u6570\u3002 \u4e00\u822c\u6765\u8bf4\uff0c \u4f7f\u7528\u865a\u51fd\u6570\u5e94\u8be5\u4f18\u5148\u4e8e\u5411\u4e0b\u8f6c\u6362 \u3002\u7136\u800c\uff0c\u6709\u65f6\u5019\u4f7f\u7528\u5411\u4e0b\u8f6c\u6362\u4f1a\u662f\u66f4\u597d\u7684\u9009\u62e9\uff1a \u5f53\u4f60\u4e0d\u80fd\u4fee\u6539\u57fa\u7c7b\u6765\u6dfb\u52a0\u865a\u51fd\u6570\u65f6(\u4f8b\u5982\uff0c\u56e0\u4e3a\u57fa\u7c7b\u662f\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206) \u5f53\u4f60\u9700\u8981\u8bbf\u95ee\u7279\u5b9a\u4e8e\u6d3e\u751f\u7c7b\u7684\u4e1c\u897f\u65f6(\u4f8b\u5982\uff0c\u4e00\u4e2a\u53ea\u5b58\u5728\u4e8e\u6d3e\u751f\u7c7b\u4e2d\u7684\u8bbf\u95ee\u51fd\u6570) \u5728\u57fa\u7c7b\u4e2d\u6dfb\u52a0\u865a\u51fd\u6570\u662f\u6ca1\u6709\u610f\u4e49\u7684(\u4f8b\u5982\uff0c\u57fa\u7c7b\u6ca1\u6709\u9002\u5f53\u7684\u8fd4\u56de\u503c)\u3002\u5982\u679c\u4e0d\u9700\u8981\u5b9e\u4f8b\u5316\u57fa\u7c7b\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u7eaf\u865a\u51fd\u6570\u3002","title":"\u5411\u4e0b\u8f6c\u6362 vs \u865a\u51fd\u6570"},{"location":"18-10-dynamic-casting/#dynamic_cast-rtti","text":"\u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f(RTTI) \u662fC++\u7684\u4e00\u4e2a\u7279\u6027\uff0c\u5b83\u5728\u8fd0\u884c\u65f6\u4f1a\u66b4\u9732\u5bf9\u8c61\u6570\u636e\u7c7b\u578b\u7684\u4fe1\u606f\u3002 dynamic_cast \u5229\u7528\u4e86\u7279\u6027\u3002\u56e0\u4e3aRTTI\u6709\u76f8\u5f53\u5927\u7684\u7a7a\u95f4\u6027\u80fd\u5f00\u9500\uff0c\u4e00\u4e9b\u7f16\u8bd1\u5668\u5141\u8bb8\u4f60\u5173\u95edRTTI\u4f5c\u4e3a\u4e00\u79cd\u4f18\u5316\u65b9\u5f0f\u3002\u4e0d\u7528\u8bf4\uff0c\u5982\u679c\u8fd9\u6837\u505a\uff0c dynamic_cast \u5c06\u4e0d\u80fd\u6b63\u786e\u5de5\u4f5c\u3002","title":"\u5bf9 dynamic_cast \u548c RTTI \u7684\u4e00\u4e9b\u8b66\u793a"},{"location":"18-11-printing-inherited-classes-using-operator%3C%3C/","tags":["inheritance","operator<<"],"text":"Key Takeaway \u53cb\u5143\u51fd\u6570 \u4e0d\u5c5e\u4e8e \u6210\u5458\u51fd\u6570 \uff0c\u56e0\u6b64\u4e0d\u80fd\u662f \u865a\u51fd\u6570 \u4e0d\u540c\u7c7b << \u9700\u8981\u4f20\u5165\u4e0d\u540c\u7c7b\u7684\u5bf9\u8c61\uff0c\u6240\u4ee5\u5373\u4f7f\u80fd\u5b9a\u4e49\u4e3a\u865a\u51fd\u6570\uff0c\u6d3e\u751f\u7c7b\u4e5f\u6ca1\u6cd5\u91cd\u5199\u5b83 \u53cb\u5143\u7684\u8fd0\u7b97\u7b26\u53ef\u4ee5\u5c06\u5b9e\u9645\u5de5\u4f5c\u59d4\u6d3e\u7ed9\u4e00\u4e2a\u666e\u901a\u7684\u6210\u5458\u51fd\u6570\uff08 \u865a\u51fd\u6570 \uff09\uff0c\u800c\u4e14\u65e0\u9700\u5728\u6d3e\u751f\u7c7b\u4e2d\u5b9e\u73b0\u8be5\u8fd0\u7b97\u7b26\uff0c\u53ea\u9700\u8981\u5b9e\u73b0\u8be5\u865a\u51fd\u6570\u7684\u91cd\u5199\u51fd\u6570\u5373\u53ef \u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\u4f7f\u7528\u4e86 \u865a\u51fd\u6570 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include class Base { public : virtual void print () const { std :: cout << \"Base\" ; } }; class Derived : public Base { public : void print () const override { std :: cout << \"Derived\" ; } }; int main () { Derived d {}; Base & b { d }; b . print (); // will call Derived::print() return 0 ; } b.print() \u4f1a\u8c03\u7528 Derived::print() (\u56e0\u4e3a b \u6307\u5411 Derived \u7c7b\u578b\u7684\u5bf9\u8c61 object\uff0c Base::print() \u662f\u4e00\u4e2a\u865a\u51fd\u6570\uff0c\u800c\u4e14 Derived::print() \u662f \u91cd\u5199 \u51fd\u6570)\u3002 \u867d\u7136\u8c03\u7528 print() \u8fd9\u6837\u7684\u6210\u5458\u51fd\u6570\u6765\u6267\u884c\u8f93\u51fa\u662f\u53ef\u4ee5\u7684\uff0c\u4f46\u8fd9\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u4e0d\u80fd\u5f88\u597d\u5730\u4e0e std::cout \u4e00\u8d77\u4f7f\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { Derived d {}; Base & b { d }; std :: cout << \"b is a \" ; b . print (); // \u663e\u5f97\u6742\u4e71\uff0c\u4e0e\u4e00\u822c\u7684\u8f93\u51fa\u8bed\u53e5\u683c\u683c\u4e0d\u5165 std :: cout << '\\n' ; return 0 ; } \u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u5b66\u4e60\u5982\u4f55\u4f7f\u7528 operator<< \u6253\u5370\u7ee7\u627f\u7c7b\u7684\u4fe1\u606f\uff0c\u4f7f\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u98ce\u683c\u6765\u4f7f\u7528\uff1a 1 std :: cout << \"b is a \" << b << '\\n' ; // much better \u6311\u6218 \u9996\u5148\u6211\u4eec\u4f7f\u7528 \u91cd\u8f7d \u7684\u65b9\u5f0f\u5b9e\u73b0 operator<< \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include class Base { public : virtual void print () const { std :: cout << \"Base\" ; } friend std :: ostream & operator << ( std :: ostream & out , const Base & b ) { out << \"Base\" ; return out ; } }; class Derived : public Base { public : void print () const override { std :: cout << \"Derived\" ; } friend std :: ostream & operator << ( std :: ostream & out , const Derived & d ) { out << \"Derived\" ; return out ; } }; int main () { Base b {}; std :: cout << b << '\\n' ; Derived d {}; std :: cout << d << '\\n' ; return 0 ; } \u7531\u4e8e\u4e0d\u9700\u8981 \u865a\u51fd\u6570 \u89e3\u6790\uff0c\u6240\u4ee5\u7a0b\u5e8f\u80fd\u591f\u6b63\u786e\u5730\u5de5\u4f5c\u5e76\u6253\u5370\uff1a 1 2 Base Derived \u518d\u8003\u8651\u4e0b\u9762\u7684 main() \uff1a 1 2 3 4 5 6 7 8 int main () { Derived d {}; Base & bref { d }; std :: cout << bref << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 Base \u663e\u7136\u8f93\u51fa\u7ed3\u679c\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u3002\u5f53\u914d\u5408 Base \u4f7f\u7528\u975e\u865a\u7684 operator<< \u65f6\uff0c std::cout << bref \u4f1a\u8c03\u7528\u57fa\u7c7b\u7684 operator<< \u3002 \u8fd9\u5c31\u662f\u6211\u4eec\u8981\u9762\u4e34\u7684\u6311\u6218\u3002 Operator << \u53ef\u4ee5\u662f\u865a\u51fd\u6570\u5417\uff1f \u5982\u679c\u95ee\u9898\u7684\u539f\u56e0\u5728\u4e8e operator<< \u4e0d\u662f\u865a\u51fd\u6570\uff0c\u90a3\u4e48\u53ef\u4ee5\u628a\u5b83\u8bbe\u4e3a virtual \u5417\uff1f \u4e0d\u884c\uff01\u800c\u4e14\u6709\u5f88\u591a\u539f\u56e0\uff1a \u9996\u5148\uff0c\u53ea\u6709\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u88ab\u865a\u62df\u5316\u2014\u2014\u8fd9\u662f\u6709\u610f\u4e49\u7684\uff0c\u56e0\u4e3a\u53ea\u6709\u7c7b\u53ef\u4ee5\u4ece\u5176\u4ed6\u7c7b\u7ee7\u627f\uff0c\u6ca1\u6709\u529e\u6cd5\u91cd\u5199\u5b58\u5728\u4e8e\u7c7b\u5916\u90e8\u7684\u51fd\u6570(\u53ef\u4ee5\u91cd\u8f7d\u975e\u6210\u5458\u51fd\u6570\uff0c\u4f46\u4e0d\u80fd\u91cd\u5199\u5b83\u4eec)\u3002\u56e0\u4e3a\u6211\u4eec\u901a\u5e38\u5c06\u64cd\u4f5c\u7b26 << \u5b9e\u73b0\u4e3a\u53cb\u5143\uff0c \u800c\u53cb\u5143\u4e0d\u88ab\u89c6\u4e3a\u6210\u5458\u51fd\u6570 \uff0c\u6240\u4ee5\u64cd\u4f5c\u7b26<<\u7684\u53cb\u5143\u7248\u672c\u4e0d\u7b26\u5408\u79f0\u4e3a\u865a\u51fd\u6570\u7684\u6761\u4ef6\u3002(\u8981\u4e86\u89e3\u4e3a\u4ec0\u4e48\u8981\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u5b9e\u73b0\u64cd\u4f5c\u7b26<<\uff0c\u8bf7\u770b 14.5 -\u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 )\u3002 \u5176\u6b21\uff0c\u5373\u4f7f\u6211\u4eec\u53ef\u4ee5\u628a operator<< \u5b9a\u4e49\u4e3a\u865a\u51fd\u6570\uff0c\u4e5f\u5b58\u5728 Base::operator<< \u548c Derived::operator<< \u7684\u51fd\u6570\u5f62\u53c2\u4e0d\u540c\u7684\u95ee\u9898(Base\u7248\u672c\u5c06\u63a5\u53d7Base\u5f62\u53c2\uff0c\u800cDerived\u7248\u672c\u5c06\u63a5\u53d7Derived\u5f62\u53c2)\u3002\u56e0\u6b64\uff0c Derived \u7248\u672c\u4e0d\u4f1a\u88ab\u8ba4\u4e3a\u662f Base \u7248\u672c\u7684 \u91cd\u5199 \uff0c\u56e0\u6b64\u4e0d\u7b26\u5408\u865a\u51fd\u6570\u89e3\u6790\u7684\u6761\u4ef6\u3002 \u90a3\u4e48\u5e94\u8be5\u600e\u4e48\u505a\u5462\uff1f \u89e3\u51b3\u529e\u6cd5 \u7b54\u6848\u975e\u5e38\u7b80\u5355\u3002 \u9996\u5148\uff0c\u5728\u7c7b\u4e2d\u6dfb\u52a0 \u53cb\u5143\u51fd\u6570 operator<< \u3002\u4f46\u662f\uff0c\u4e0d\u8981\u8ba9 operator<< \u81ea\u5df1\u5426\u5219\u5b9e\u9645\u7684\u6253\u5370\uff0c\u800c\u662f\u5c06\u8fd9\u6253\u5370\u5de5\u4f5c\u59d4\u6258\u7ed9\u4e00\u4e2a\u53ef\u4ee5\u88ab\u865a\u5316\u7684\u666e\u901a\u6210\u5458\u51fd\u6570\uff01 \u4ee5\u4e0b\u662f\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include class Base { public : // \u91cd\u8f7d\u7684 operator<< friend std :: ostream & operator << ( std :: ostream & out , const Base & b ) { // \u59d4\u6d3e\u7ed9\u6210\u5458\u51fd\u6570 print() return b . print ( out ); } // \u6211\u4eec\u4f9d\u8d56 print() \u51fd\u6570\u8fdb\u884c\u5b9e\u9645\u7684\u6253\u5370\u5de5\u4f5c // \u56e0\u4e3a print \u662f\u4e00\u4e2a\u666e\u901a\u7684\u6210\u5458\u51fd\u6570\uff0c\u6240\u4ee5\u53ef\u4ee5\u662f\u865a\u51fd\u6570 virtual std :: ostream & print ( std :: ostream & out ) const { out << \"Base\" ; return out ; } }; class Derived : public Base { public : // \u91cd\u5199\u7684 print \u51fd\u6570 std :: ostream & print ( std :: ostream & out ) const override { out << \"Derived\" ; return out ; } //\u4e0d\u9700\u8981\u5b9e\u73b0operator<< }; int main () { Base b {}; std :: cout << b << '\\n' ; Derived d {}; std :: cout << d << '\\n' ; // \u6ce8\u610f\uff0c\u8fd9\u4e00\u884c\u4ee3\u7801\u53ef\u4ee5\u6b63\u786e\u5de5\u4f5c\uff0c\u5373\u4f7f\u6d3e\u751f\u7c7b\u90fd\u6ca1\u6709\u5b9e\u73b0\u8be5\u8fd0\u7b97\u7b26 Base & bref { d }; std :: cout << bref << '\\n' ; return 0 ; } \u4e09\u6761\u8bed\u53e5\u90fd\u80fd\u6b63\u786e\u6267\u884c\uff1a 1 2 3 Base Derived Derived \u8ba9\u6211\u4eec\u66f4\u8be6\u7ec6\u5730\u7814\u7a76\u4e00\u4e0b\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9\u3002 \u9996\u5148\uff0c\u5bf9\u4e8e Base \u7684\u4f8b\u5b50\uff0c\u8c03\u7528 operator<< \u65f6\u4f1a\u8c03\u7528\u865a\u51fd\u6570 print() \u3002\u56e0\u4e3a\u7684 Base \u5f15\u7528\u53c2\u6570\u6307\u5411\u4e00\u4e2a Base \u5bf9\u8c61\uff0c b.print() \u89e3\u6790\u4e3a Base::print() \u5e76\u6267\u884c\u6253\u5370\u3002\u8fd9\u91cc\u6ca1\u4ec0\u4e48\u7279\u522b\u7684\u3002 \u5728\u5bf9\u4e8e Derived \u7684\u4f8b\u5b50\uff0c\u7f16\u8bd1\u5668\u9996\u5148\u67e5\u770b\u662f\u5426\u6709\u63a5\u53d7 Derived \u5bf9\u8c61\u7684 << \u3002\u6ca1\u6709\uff0c\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u5b9a\u4e49\uff0c\u63a5\u4e0b\u6765\uff0c\u7f16\u8bd1\u5668\u67e5\u770b\u662f\u5426\u6709\u63a5\u53d7 Base \u5bf9\u8c61\u7684 << \u3002\u6709\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u5c06 Derived \u5bf9\u8c61\u9690\u5f0f\u4e0a\u8f6c\u6362\u4e3a Base& \u5e76\u8c03\u7528\u51fd\u6570\uff0c\u7136\u540e\u8c03\u7528\u865a print() \uff0c\u89e3\u6790\u4e3a Derived::print() \u3002 \u6ce8\u610f\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u4e3a\u6bcf\u4e2a\u6d3e\u751f\u7c7b\u5b9a\u4e49 operator<< \uff0c\u5904\u7406 Base \u5bf9\u8c61\u7684\u7248\u672c\u5bf9 Base \u53ca\u5176\u6d3e\u751f\u7684\u4efb\u4f55\u7c7b\u53ef\u7528\uff01 \u7b2c\u4e09\u79cd\u60c5\u51b5\u662f\u524d\u4e24\u79cd\u60c5\u51b5\u7684\u6df7\u5408\u3002\u9996\u5148\uff0c\u7f16\u8bd1\u5668\u5c06\u53d8\u91cf bref \u4e0e\u5e26\u6709 Base \u53c2\u6570\u7684 operator<< \u5339\u914d\u3002\u5b83\u8c03\u7528\u4e86\u865a\u51fd\u6570 print() \u3002\u56e0\u4e3a Base \u5f15\u7528\u5b9e\u9645\u4e0a\u662f\u6307\u5411\u4e00\u4e2a Derived \u5bf9\u8c61\uff0c\u6240\u4ee5\u5b83\u89e3\u6790\u4e3a Derived::print() \uff0c\u6b63\u5982\u6211\u4eec\u671f\u671b\u7684\u90a3\u6837\u3002 \u95ee\u9898\u641e\u5b9a\uff01","title":"18.11 - \u4f7f\u7528 << \u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b"},{"location":"18-11-printing-inherited-classes-using-operator%3C%3C/#_1","text":"\u9996\u5148\u6211\u4eec\u4f7f\u7528 \u91cd\u8f7d \u7684\u65b9\u5f0f\u5b9e\u73b0 operator<< \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include class Base { public : virtual void print () const { std :: cout << \"Base\" ; } friend std :: ostream & operator << ( std :: ostream & out , const Base & b ) { out << \"Base\" ; return out ; } }; class Derived : public Base { public : void print () const override { std :: cout << \"Derived\" ; } friend std :: ostream & operator << ( std :: ostream & out , const Derived & d ) { out << \"Derived\" ; return out ; } }; int main () { Base b {}; std :: cout << b << '\\n' ; Derived d {}; std :: cout << d << '\\n' ; return 0 ; } \u7531\u4e8e\u4e0d\u9700\u8981 \u865a\u51fd\u6570 \u89e3\u6790\uff0c\u6240\u4ee5\u7a0b\u5e8f\u80fd\u591f\u6b63\u786e\u5730\u5de5\u4f5c\u5e76\u6253\u5370\uff1a 1 2 Base Derived \u518d\u8003\u8651\u4e0b\u9762\u7684 main() \uff1a 1 2 3 4 5 6 7 8 int main () { Derived d {}; Base & bref { d }; std :: cout << bref << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 Base \u663e\u7136\u8f93\u51fa\u7ed3\u679c\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u3002\u5f53\u914d\u5408 Base \u4f7f\u7528\u975e\u865a\u7684 operator<< \u65f6\uff0c std::cout << bref \u4f1a\u8c03\u7528\u57fa\u7c7b\u7684 operator<< \u3002 \u8fd9\u5c31\u662f\u6211\u4eec\u8981\u9762\u4e34\u7684\u6311\u6218\u3002","title":"\u6311\u6218"},{"location":"18-11-printing-inherited-classes-using-operator%3C%3C/#operator","text":"\u5982\u679c\u95ee\u9898\u7684\u539f\u56e0\u5728\u4e8e operator<< \u4e0d\u662f\u865a\u51fd\u6570\uff0c\u90a3\u4e48\u53ef\u4ee5\u628a\u5b83\u8bbe\u4e3a virtual \u5417\uff1f \u4e0d\u884c\uff01\u800c\u4e14\u6709\u5f88\u591a\u539f\u56e0\uff1a \u9996\u5148\uff0c\u53ea\u6709\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u88ab\u865a\u62df\u5316\u2014\u2014\u8fd9\u662f\u6709\u610f\u4e49\u7684\uff0c\u56e0\u4e3a\u53ea\u6709\u7c7b\u53ef\u4ee5\u4ece\u5176\u4ed6\u7c7b\u7ee7\u627f\uff0c\u6ca1\u6709\u529e\u6cd5\u91cd\u5199\u5b58\u5728\u4e8e\u7c7b\u5916\u90e8\u7684\u51fd\u6570(\u53ef\u4ee5\u91cd\u8f7d\u975e\u6210\u5458\u51fd\u6570\uff0c\u4f46\u4e0d\u80fd\u91cd\u5199\u5b83\u4eec)\u3002\u56e0\u4e3a\u6211\u4eec\u901a\u5e38\u5c06\u64cd\u4f5c\u7b26 << \u5b9e\u73b0\u4e3a\u53cb\u5143\uff0c \u800c\u53cb\u5143\u4e0d\u88ab\u89c6\u4e3a\u6210\u5458\u51fd\u6570 \uff0c\u6240\u4ee5\u64cd\u4f5c\u7b26<<\u7684\u53cb\u5143\u7248\u672c\u4e0d\u7b26\u5408\u79f0\u4e3a\u865a\u51fd\u6570\u7684\u6761\u4ef6\u3002(\u8981\u4e86\u89e3\u4e3a\u4ec0\u4e48\u8981\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u5b9e\u73b0\u64cd\u4f5c\u7b26<<\uff0c\u8bf7\u770b 14.5 -\u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 )\u3002 \u5176\u6b21\uff0c\u5373\u4f7f\u6211\u4eec\u53ef\u4ee5\u628a operator<< \u5b9a\u4e49\u4e3a\u865a\u51fd\u6570\uff0c\u4e5f\u5b58\u5728 Base::operator<< \u548c Derived::operator<< \u7684\u51fd\u6570\u5f62\u53c2\u4e0d\u540c\u7684\u95ee\u9898(Base\u7248\u672c\u5c06\u63a5\u53d7Base\u5f62\u53c2\uff0c\u800cDerived\u7248\u672c\u5c06\u63a5\u53d7Derived\u5f62\u53c2)\u3002\u56e0\u6b64\uff0c Derived \u7248\u672c\u4e0d\u4f1a\u88ab\u8ba4\u4e3a\u662f Base \u7248\u672c\u7684 \u91cd\u5199 \uff0c\u56e0\u6b64\u4e0d\u7b26\u5408\u865a\u51fd\u6570\u89e3\u6790\u7684\u6761\u4ef6\u3002 \u90a3\u4e48\u5e94\u8be5\u600e\u4e48\u505a\u5462\uff1f","title":"Operator << \u53ef\u4ee5\u662f\u865a\u51fd\u6570\u5417\uff1f"},{"location":"18-11-printing-inherited-classes-using-operator%3C%3C/#_2","text":"\u7b54\u6848\u975e\u5e38\u7b80\u5355\u3002 \u9996\u5148\uff0c\u5728\u7c7b\u4e2d\u6dfb\u52a0 \u53cb\u5143\u51fd\u6570 operator<< \u3002\u4f46\u662f\uff0c\u4e0d\u8981\u8ba9 operator<< \u81ea\u5df1\u5426\u5219\u5b9e\u9645\u7684\u6253\u5370\uff0c\u800c\u662f\u5c06\u8fd9\u6253\u5370\u5de5\u4f5c\u59d4\u6258\u7ed9\u4e00\u4e2a\u53ef\u4ee5\u88ab\u865a\u5316\u7684\u666e\u901a\u6210\u5458\u51fd\u6570\uff01 \u4ee5\u4e0b\u662f\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include class Base { public : // \u91cd\u8f7d\u7684 operator<< friend std :: ostream & operator << ( std :: ostream & out , const Base & b ) { // \u59d4\u6d3e\u7ed9\u6210\u5458\u51fd\u6570 print() return b . print ( out ); } // \u6211\u4eec\u4f9d\u8d56 print() \u51fd\u6570\u8fdb\u884c\u5b9e\u9645\u7684\u6253\u5370\u5de5\u4f5c // \u56e0\u4e3a print \u662f\u4e00\u4e2a\u666e\u901a\u7684\u6210\u5458\u51fd\u6570\uff0c\u6240\u4ee5\u53ef\u4ee5\u662f\u865a\u51fd\u6570 virtual std :: ostream & print ( std :: ostream & out ) const { out << \"Base\" ; return out ; } }; class Derived : public Base { public : // \u91cd\u5199\u7684 print \u51fd\u6570 std :: ostream & print ( std :: ostream & out ) const override { out << \"Derived\" ; return out ; } //\u4e0d\u9700\u8981\u5b9e\u73b0operator<< }; int main () { Base b {}; std :: cout << b << '\\n' ; Derived d {}; std :: cout << d << '\\n' ; // \u6ce8\u610f\uff0c\u8fd9\u4e00\u884c\u4ee3\u7801\u53ef\u4ee5\u6b63\u786e\u5de5\u4f5c\uff0c\u5373\u4f7f\u6d3e\u751f\u7c7b\u90fd\u6ca1\u6709\u5b9e\u73b0\u8be5\u8fd0\u7b97\u7b26 Base & bref { d }; std :: cout << bref << '\\n' ; return 0 ; } \u4e09\u6761\u8bed\u53e5\u90fd\u80fd\u6b63\u786e\u6267\u884c\uff1a 1 2 3 Base Derived Derived \u8ba9\u6211\u4eec\u66f4\u8be6\u7ec6\u5730\u7814\u7a76\u4e00\u4e0b\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9\u3002 \u9996\u5148\uff0c\u5bf9\u4e8e Base \u7684\u4f8b\u5b50\uff0c\u8c03\u7528 operator<< \u65f6\u4f1a\u8c03\u7528\u865a\u51fd\u6570 print() \u3002\u56e0\u4e3a\u7684 Base \u5f15\u7528\u53c2\u6570\u6307\u5411\u4e00\u4e2a Base \u5bf9\u8c61\uff0c b.print() \u89e3\u6790\u4e3a Base::print() \u5e76\u6267\u884c\u6253\u5370\u3002\u8fd9\u91cc\u6ca1\u4ec0\u4e48\u7279\u522b\u7684\u3002 \u5728\u5bf9\u4e8e Derived \u7684\u4f8b\u5b50\uff0c\u7f16\u8bd1\u5668\u9996\u5148\u67e5\u770b\u662f\u5426\u6709\u63a5\u53d7 Derived \u5bf9\u8c61\u7684 << \u3002\u6ca1\u6709\uff0c\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u5b9a\u4e49\uff0c\u63a5\u4e0b\u6765\uff0c\u7f16\u8bd1\u5668\u67e5\u770b\u662f\u5426\u6709\u63a5\u53d7 Base \u5bf9\u8c61\u7684 << \u3002\u6709\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u5c06 Derived \u5bf9\u8c61\u9690\u5f0f\u4e0a\u8f6c\u6362\u4e3a Base& \u5e76\u8c03\u7528\u51fd\u6570\uff0c\u7136\u540e\u8c03\u7528\u865a print() \uff0c\u89e3\u6790\u4e3a Derived::print() \u3002 \u6ce8\u610f\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u4e3a\u6bcf\u4e2a\u6d3e\u751f\u7c7b\u5b9a\u4e49 operator<< \uff0c\u5904\u7406 Base \u5bf9\u8c61\u7684\u7248\u672c\u5bf9 Base \u53ca\u5176\u6d3e\u751f\u7684\u4efb\u4f55\u7c7b\u53ef\u7528\uff01 \u7b2c\u4e09\u79cd\u60c5\u51b5\u662f\u524d\u4e24\u79cd\u60c5\u51b5\u7684\u6df7\u5408\u3002\u9996\u5148\uff0c\u7f16\u8bd1\u5668\u5c06\u53d8\u91cf bref \u4e0e\u5e26\u6709 Base \u53c2\u6570\u7684 operator<< \u5339\u914d\u3002\u5b83\u8c03\u7528\u4e86\u865a\u51fd\u6570 print() \u3002\u56e0\u4e3a Base \u5f15\u7528\u5b9e\u9645\u4e0a\u662f\u6307\u5411\u4e00\u4e2a Derived \u5bf9\u8c61\uff0c\u6240\u4ee5\u5b83\u89e3\u6790\u4e3a Derived::print() \uff0c\u6b63\u5982\u6211\u4eec\u671f\u671b\u7684\u90a3\u6837\u3002 \u95ee\u9898\u641e\u5b9a\uff01","title":"\u89e3\u51b3\u529e\u6cd5"},{"location":"18-2-virtual-functions-and-polymorphism/","tags":["virtual functions","polymorphism"],"text":"Key Takeaway \u865a\u51fd\u6570\u5c06\u89e3\u6790\u4e3a\u6700\u540e\u88ab\u6d3e\u751f\u7684\u7248\u672c\uff08\u5728\u539f\u59cb\u5bf9\u8c61\u548c\u5f15\u7528\u5bf9\u8c61\u4e4b\u95f4\u7684\u6d3e\u751f\u94fe\u4e0a\u67e5\u627e\uff09\uff0c\u5373\u4fbf\u4f7f\u7528\u57fa\u7c7b\u6307\u9488\u64cd\u4f5c\u4e5f\u662f\u4e00\u6837\uff08\u4e0d\u7136\u610f\u4e49\u4f55\u5728\uff09 \u9f99\u751f\u9f99\u51e4\u751f\u51e4\uff0c\u865a\u51fd\u6570\u7684\u91cd\u5199\u51fd\u6570\u865a\u51fd\u6570 \u89e3\u6790\u865a\u51fd\u6570\u8c03\u7528\u7684\u65f6\u95f4\u6bd4\u89e3\u6790\u5e38\u89c4\u51fd\u6570\u8c03\u7528\u7684\u65f6\u95f4\u957f\u3002\u6b64\u5916\uff0c\u7f16\u8bd1\u5668\u8fd8\u5fc5\u987b\u4e3a\u6bcf\u4e2a\u5177\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u865a\u51fd\u6570\u7684\u7c7b\u5bf9\u8c61\u5206\u914d\u4e00\u4e2a\u989d\u5916\u7684\u6307\u9488 \u5728 18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u4e00\u4e9b\u53ef\u4ee5\u4f7f\u7528\u57fa\u7c7b\u6307\u9488\u6216\u5f15\u7528\u7b80\u5316\u4ee3\u7801\u7684\u4f8b\u5b50\u3002\u4f46\u662f\uff0c\u5728\u8fd9\u4e9b\u4f8b\u5b50\u4e2d\uff0c\u6700\u5927\u7684\u95ee\u9898\u5c31\u5728\u4e8e\u57fa\u7c7b\u7684\u6307\u9488\u53ea\u80fd\u591f\u8c03\u7528\u57fa\u7c7b\u7248\u672c\u7684\u51fd\u6570\uff0c\u65e0\u6cd5\u8c03\u7528\u6d3e\u751f\u7c7b\u7684\u51fd\u6570\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include class Base { public : std :: string_view getName () const { return \"Base\" ; } }; class Derived : public Base { public : std :: string_view getName () const { return \"Derived\" ; } }; int main () { Derived derived ; Base & rBase { derived }; std :: cout << \"rBase is a \" << rBase . getName () << '\\n' ; return 0 ; } \u6253\u5370\uff1a rBase is a Base \u56e0\u4e3a rBase \u662f Base \u7c7b\u578b\u7684\u5f15\u7528\uff0c\u5b83\u4f1a\u8c03\u7528 Base::getName() \uff0c\u5373\u4fbf\u5b83\u5b9e\u9645\u4e0a\u5f15\u7528\u7684\u662f Derived \u7c7b\u578b\u4e2d\u7684 Base \u90e8\u5206\u3002 \u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528 \u865a\u51fd\u6570 \u6765\u89e3\u51b3\u8be5\u95ee\u9898\u3002 \u865a\u51fd\u6570\u548c\u591a\u6001 \u865a\u51fd\u6570 \u662f\u4e00\u7c7b\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5f53\u5b83\u88ab\u8c03\u7528\u65f6\uff0c\u5b9e\u9645\u89e3\u6790\u5230\u7684\u662f\u6700\u540e\u6d3e\u751f\u7c7b\u4e2d\u5bf9\u5e94\u7248\u672c\u7684\u51fd\u6570\uff08\u9700\u8981\u540c\u65f6\u5b58\u5728\u4e8e\u6d3e\u751f\u7c7b\u548c\u57fa\u7c7b\u4e2d\uff09\u3002\u8fd9\u4e2a\u673a\u5236\u79f0\u4e3a \u591a\u6001 \u3002\u5982\u679c\u6d3e\u751f\u7c7b\u4e2d\u6709\u5177\u6709\u76f8\u540c \u7b7e\u540d (\u51fd\u6570\u540d\u3001\u53c2\u6570\u7c7b\u578b\u3001\u662f\u5426\u4e3aconst\uff09\u5e76\u4e14\u8fd4\u56de\u503c\u7c7b\u578b\u4e00\u6837\u7684\u51fd\u6570\uff0c\u5219\u8fd9\u4e9b\u51fd\u6570\u79f0\u4e3a \u91cd\u5199\u51fd\u6570\uff08override\uff09 \u3002 \u5b9a\u4e49\u865a\u51fd\u6570\u65f6\uff0c\u53ea\u9700\u5728\u51fd\u6570\u58f0\u660e\u524d\u6dfb\u52a0\u201cvirtual\u201d\u5173\u952e\u5b57\u3002 \u4f7f\u7528\u865a\u51fd\u6570\u6539\u5199\u4e0a\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include class Base { public : virtual std :: string_view getName () const { return \"Base\" ; } // note addition of virtual keyword }; class Derived : public Base { public : virtual std :: string_view getName () const { return \"Derived\" ; } }; int main () { Derived derived ; Base & rBase { derived }; std :: cout << \"rBase is a \" << rBase . getName () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 rBase is a Derived \u56e0\u4e3a rBase \u662f Derived \u7c7b\u578b\u5bf9\u8c61\u4e2d Base \u90e8\u5206\u7684\u5f15\u7528\uff0c\u56e0\u6b64\u5f53 rBase.getName() \u88ab\u8c03\u7528\u65f6\uff0c\u5b83\u4f1a\u89e3\u6790\u4e3a Base::getName() \u3002\u4e0d\u8fc7\uff0c\u7531\u4e8e\u8be5\u51fd\u6570\u662f\u865a\u51fd\u6570\uff0c\u6240\u4ee5\u7a0b\u5e8f\u4f1a\u7ee7\u7eed\u6cbf\u7740\u6d3e\u751f\u7684\u65b9\u5411\u67e5\u627e\uff0c\u5982\u679c\u5728 Base \u548c Derived \u4e2d\u5b58\u5728\u8be5\u51fd\u6570\u8fdb\u4e00\u6b65\u6d3e\u751f\u7684\u7248\u672c\uff0c\u5219\u4f1a\u8c03\u7528\u8be5\u51fd\u6570\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u5b9e\u9645\u8c03\u7528\u7684\u662f Derived::getName() \uff01 \u518d\u770b\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include #include class A { public : virtual std :: string_view getName () const { return \"A\" ; } }; class B : public A { public : virtual std :: string_view getName () const { return \"B\" ; } }; class C : public B { public : virtual std :: string_view getName () const { return \"C\" ; } }; class D : public C { public : virtual std :: string_view getName () const { return \"D\" ; } }; int main () { C c ; A & rBase { c }; //C\u7c7b\u578b\u4e2d\u7684A\u57fa\u7c7b\u90e8\u5206 std :: cout << \"rBase is a \" << rBase . getName () << '\\n' ; return 0 ; } \u4f60\u89c9\u5f97\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4f1a\u662f\u4ec0\u4e48\uff1f \u6765\u770b\u770b\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u9996\u5148\uff0c\u5b9e\u4f8b\u5316\u4e00\u4e2aC\u7c7b\u578b\u7684\u5bf9\u8c61\u3002 rBase \u662f\u4e00\u4e2aA\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u5f15\u7528C\u7c7b\u578b\u5bf9\u8c61\u4e2d\u7684A\u7c7b\u578b\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u5f53 rBase.getName() \u88ab\u8c03\u7528\u65f6\uff0c\u5b83\u4f1a\u6c42\u503c\u5f97\u5230 A::getName() \u3002\u4f46\u662f\u7531\u4e8e A::getName() \u662f\u865a\u51fd\u6570\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u8c03\u7528A\u7c7b\u578b\u548cC\u7c7b\u578b\u4e2d\u6700\u540e\u88ab\u6d3e\u751f\u7684\u8be5\u51fd\u6570\u7248\u672c\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u663e\u7136\u662f C::getName() \u3002\u6ce8\u610f\uff0c\u5b83\u4e0d\u4f1a\u89e3\u6790\u5230 D::getName() \uff0c\u56e0\u4e3a\u539f\u59cb\u5bf9\u8c61\u662f C\uff0c\u800c\u4e0d\u662fD\uff0c\u6240\u4ee5D\u7684\u6210\u5458\u4e0d\u5728\u8003\u8651\u8303\u56f4\u5185\u3002 \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 rBase is a C \u66f4\u590d\u6742\u7684\u4f8b\u5b50 \u518d\u6765\u770b\u770b\u6211\u4eec\u5728\u4e0a\u4e00\u8bfe\u4e2d\u4f7f\u7528\u7684\u52a8\u7269\u7684\u4f8b\u5b50\u3002\u4e0b\u9762\u662f\u539f\u59cb\u7684\u4ee3\u7801\uff0c\u4ee5\u53ca\u4e00\u4e9b\u6d4b\u8bd5\u4ee3\u7801: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 #include #include #include class Animal { protected : std :: string m_name ; // We're making this constructor protected because // we don't want people creating Animal objects directly, // but we still want derived classes to be able to use it. Animal ( const std :: string & name ) : m_name { name } { } public : const std :: string & getName () const { return m_name ; } std :: string_view speak () const { return \"???\" ; } }; class Cat : public Animal { public : Cat ( const std :: string & name ) : Animal { name } { } std :: string_view speak () const { return \"Meow\" ; } }; class Dog : public Animal { public : Dog ( const std :: string & name ) : Animal { name } { } std :: string_view speak () const { return \"Woof\" ; } }; void report ( const Animal & animal ) { std :: cout << animal . getName () << \" says \" << animal . speak () << '\\n' ; } int main () { Cat cat { \"Fred\" }; Dog dog { \"Garbo\" }; report ( cat ); report ( dog ); return 0 ; } \u8fd0\u884c\u7ed3\u679c 1 2 Fred says ??? Garbo says ??? \u63a5\u4e0b\u6765\u7684\u4f8b\u5b50\u4e2d\uff0c speak() \u51fd\u6570\u88ab\u5b9a\u4e49\u4e3a\u865a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 #include #include #include class Animal { protected : std :: string m_name ; // We're making this constructor protected because // we don't want people creating Animal objects directly, // but we still want derived classes to be able to use it. Animal ( const std :: string & name ) : m_name { name } { } public : const std :: string & getName () const { return m_name ; } virtual std :: string_view speak () const { return \"???\" ; } //\u865a\u51fd\u6570 }; class Cat : public Animal { public : Cat ( const std :: string & name ) : Animal { name } { } virtual std :: string_view speak () const { return \"Meow\" ; } }; class Dog : public Animal { public : Dog ( const std :: string & name ) : Animal { name } { } virtual std :: string_view speak () const { return \"Woof\" ; } }; void report ( const Animal & animal ) { std :: cout << animal . getName () << \" says \" << animal . speak () << '\\n' ; } int main () { Cat cat { \"Fred\" }; Dog dog { \"Garbo\" }; report ( cat ); report ( dog ); return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Fred says Meow Garbo says Woof \u53ef\u4ee5\u6b63\u786e\u5de5\u4f5c\u4e86\u3002 \u5f53 animal.speak() \u6c42\u804c\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u6ce8\u610f\u5230 Animal::speak() \u662f\u4e00\u4e2a\u865a\u51fd\u6570\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c animal \u5f15\u7528\u7684\u662f Cat \u7c7b\u4e2d\u7684 Animal \u90e8\u5206\u3002\u7a0b\u5e8f\u4f1a\u5728Animal \u548c Cat \u4e4b\u95f4\u6240\u6709\u7684\u6d3e\u751f\u7c7b\u4e2d\u67e5\u770b\u662f\u5426\u6709\u8be5\u51fd\u6570\u8fdb\u4e00\u6b65\u88ab\u6d3e\u751f\u7684\u7248\u672c\u3002\u5728\u672c\u4f8b\u4e2d\uff0c Cat::speak() \u5c31\u4f1a\u88ab\u8c03\u7528\u3002\u5bf9\u4e8e Dog \u4e5f\u7c7b\u4f3c\uff0c\u7a0b\u5e8f\u89e3\u6790\u5230\u7684\u51fd\u6570\u662f Dog::speak() \u3002 \u6ce8\u610f\uff0c\u6211\u4eec\u6ca1\u6709\u5c06 Animal::getName() \u5b9a\u4e49\u4e3a\u865a\u51fd\u6570\u3002\u8fd9\u662f\u56e0\u4e3a getName() \u5e76\u6ca1\u6709\u5728\u4efb\u4f55\u6d3e\u751f\u7c7b\u4e2d\u88ab \u91cd\u5199 \uff0c\u6240\u4ee5\u6ca1\u6709\u5fc5\u8981\u8bbe\u7f6e\u4e3a\u865a\u51fd\u6570\u3002 \u540c\u6837\u5730\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u4e5f\u80fd\u591f\u6309\u7167\u6211\u4eec\u7684\u9700\u8981\u6b63\u5e38\u5de5\u4f5c\u4e86\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 Cat fred { \"Fred\" }; Cat misty { \"Misty\" }; Cat zeke { \"Zeke\" }; Dog garbo { \"Garbo\" }; Dog pooky { \"Pooky\" }; Dog truffle { \"Truffle\" }; // Set up an array of pointers to animals, and set those pointers to our Cat and Dog objects Animal * animals []{ & fred , & garbo , & misty , & pooky , & truffle , & zeke }; for ( const auto * animal : animals ) std :: cout << animal -> getName () << \" says \" << animal -> speak () << '\\n' ; \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 Fred says Meow Garbo says Woof Misty says Meow Pooky says Woof Truffle says Woof Zeke says Meow \u5c3d\u7ba1\u8fd9\u4e24\u4e2a\u4f8b\u5b50\u53ea\u4f7f\u7528\u4e86 Cat \u548c Dog \uff0c\u4f46\u6211\u4eec\u4ece Animal \u6d3e\u751f\u7684\u4efb\u4f55\u5176\u4ed6\u7c7b\u4e5f\u53ef\u4ee5\u4f7f\u7528 report() \u51fd\u6570\u548c Animal \u6570\u7ec4\uff0c\u65e0\u9700\u8fdb\u4e00\u6b65\u4fee\u6539\uff01\u8fd9\u53ef\u80fd\u662f\u865a\u51fd\u6570\u7684\u6700\u5927\u597d\u5904\u2014\u2014\u80fd\u591f\u4ee5\u8fd9\u6837\u4e00\u79cd\u65b9\u5f0f\u6784\u9020\u4ee3\u7801\uff0c\u5373\u65b0\u6d3e\u751f\u7684\u7c7b\u5c06\u81ea\u52a8\u4e0e\u65e7\u4ee3\u7801\u4e00\u8d77\u5de5\u4f5c\uff0c\u800c\u65e0\u9700\u4fee\u6539! \u6ce8\u610f\uff1a\u8981\u4f7f\u7528\u6d3e\u751f\u7c7b\u51fd\u6570\uff0c \u6d3e\u751f\u7c7b\u51fd\u6570\u7684\u7b7e\u540d\u5fc5\u987b\u4e0e\u57fa\u7c7b\u865a\u51fd\u6570\u7684\u7b7e\u540d\u5b8c\u5168\u5339\u914d \uff08\u8fd4\u56de\u503c\u4e0d\u662f\u7b7e\u540d\u7684\u4e00\u90e8\u5206\uff09\u3002\u5982\u679c\u6d3e\u751f\u7c7b\u51fd\u6570\u5177\u6709\u4e0d\u540c\u7684\u5f62\u53c2\u7c7b\u578b\uff0c\u5219\u7a0b\u5e8f\u4ecd\u53ef\u80fd\u7f16\u8bd1\uff0c\u4f46\u865a\u51fd\u6570\u5c06\u4e0d\u80fd\u6309\u9884\u671f\u89e3\u6790\u3002\u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5982\u4f55\u9632\u8303\u8fd9\u79cd\u60c5\u51b5\u3002 \u8fd8\u8981\u6ce8\u610f\uff0c \u5982\u679c\u4e00\u4e2a\u51fd\u6570\u88ab\u6807\u8bb0\u4e3a\u865a\u51fd\u6570\uff0c\u90a3\u4e48\u6240\u6709\u7684\u5339\u914d \u91cd\u5199 \u51fd\u6570\u4e5f\u88ab\u8ba4\u4e3a\u662f\u865a\u51fd\u6570 \uff0c\u5373\u4f7f\u5b83\u4eec\u6ca1\u6709\u88ab\u663e\u5f0f\u5730\u6807\u8bb0\u4e3a\u865a\u51fd\u6570\u3002 \u865a\u51fd\u6570\u7684\u8fd4\u56de\u503c \u6b63\u5e38\u60c5\u51b5\u4e0b\uff0c\u865a\u51fd\u6570\u7684\u51fd\u6570\u7684\u8fd4\u56de\u503c\u548c \u91cd\u5199 \u51fd\u6570\u7684\u8fd4\u56de\u503c\u5fc5\u987b\u8981\u5339\u914d\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 class Base { public : virtual int getValue () const { return 5 ; } }; class Derived : public Base { public : virtual double getValue () const { return 6.78 ; } }; \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c Derived::getValue() is \u5e76\u4e0d\u4f1a\u88ab\u8ba4\u4e3a\u662f\u5bf9 Base::getValue() \u7684\u91cd\u5199\uff0c \u56e0\u6b64\u7f16\u8bd1\u4f1a\u5931\u8d25 \u3002 \u4e0d\u8981\u5728\u6784\u9020\u51fd\u6570\u6216\u6790\u6784\u51fd\u6570\u4e2d\u8c03\u7528\u865a\u51fd\u6570 \u8fd9\u662f\u53e6\u4e00\u4e2a\u7ecf\u5e38\u4f24\u5230\u83dc\u9e1f\u4eec\u7684\u9677\u9631\u662f\u2014\u2014\u4e0d\u5e94\u8be5\u4ece\u6784\u9020\u51fd\u6570\u6216\u6790\u6784\u51fd\u6570\u8c03\u7528\u865a\u51fd\u6570\u3002 \u8bb0\u4f4f\uff0c\u5728\u521b\u5efa\u6d3e\u751f\u7c7b\u65f6\uff0c\u9996\u5148\u6784\u9020\u57fa\u7c7b\u90e8\u5206\u3002\u5982\u679c\u5728 Base \u6784\u9020\u51fd\u6570\u8c03\u7528\u865a\u51fd\u6570\uff0c\u800c\u7c7b\u7684\u6d3e\u751f\u90e8\u5206\u751a\u81f3\u8fd8\u6ca1\u6709\u521b\u5efa\uff0c\u5219\u65e0\u6cd5\u8c03\u7528\u51fd\u6570\u7684\u6d3e\u751f\u7248\u672c\uff0c\u56e0\u4e3a\u6ca1\u6709\u6d3e\u751f\u5bf9\u8c61\u4f9b\u6d3e\u751f\u51fd\u6570\u5904\u7406\u3002\u5728C++\u4e2d\uff0c\u5b83\u4f1a\u8c03\u7528Base\u7248\u672c\u3002\uff08 17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f \uff09 \u6790\u6784\u51fd\u6570\u4e5f\u5b58\u5728\u7c7b\u4f3c\u7684\u95ee\u9898\u3002 \u5982\u679c\u5728\u57fa\u7c7b\u6790\u6784\u51fd\u6570\u4e2d\u8c03\u7528\u865a\u51fd\u6570\uff0c\u5b83\u5c06\u59cb\u7ec8\u89e3\u6790\u4e3a\u8be5\u51fd\u6570\u7684\u57fa\u7c7b\u7248\u672c \uff0c\u56e0\u4e3a\u7c7b\u7684\u6d3e\u751f\u90e8\u5206\u5c06\u5df2\u7ecf\u88ab\u9500\u6bc1\u3002 \u6700\u4f73\u5b9e\u8df5 \u6c38\u8fdc\u4e0d\u8981\u5728\u6784\u9020\u51fd\u6570\u6216\u6790\u6784\u51fd\u6570\u4e2d\u8c03\u7528\u865a\u51fd\u6570 \u865a\u51fd\u6570\u7684\u7f3a\u70b9 \u65e2\u7136\u5927\u591a\u6570\u65f6\u5019\u4f60\u4f1a\u5e0c\u671b\u51fd\u6570\u662f\u865a\u51fd\u6570\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u8ba9\u6240\u6709\u7684\u51fd\u6570\u90fd\u662f\u865a\u7684\u5462\uff1f\u56e0\u4e3a\u8fd9\u4e48\u505a\u6548\u7387\u5f88\u4f4e\u2014\u2014 \u89e3\u6790\u865a\u51fd\u6570\u8c03\u7528\u7684\u65f6\u95f4\u6bd4\u89e3\u6790\u5e38\u89c4\u51fd\u6570\u8c03\u7528\u7684\u65f6\u95f4\u957f\u3002\u6b64\u5916\uff0c\u7f16\u8bd1\u5668\u8fd8\u5fc5\u987b\u4e3a\u6bcf\u4e2a\u5177\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u865a\u51fd\u6570\u7684\u7c7b\u5bf9\u8c61\u5206\u914d\u4e00\u4e2a\u989d\u5916\u7684\u6307\u9488 \u3002\u6211\u4eec\u5c06\u5728\u672c\u7ae0\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e2a\u95ee\u9898\u3002","title":"18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001"},{"location":"18-2-virtual-functions-and-polymorphism/#_1","text":"\u865a\u51fd\u6570 \u662f\u4e00\u7c7b\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5f53\u5b83\u88ab\u8c03\u7528\u65f6\uff0c\u5b9e\u9645\u89e3\u6790\u5230\u7684\u662f\u6700\u540e\u6d3e\u751f\u7c7b\u4e2d\u5bf9\u5e94\u7248\u672c\u7684\u51fd\u6570\uff08\u9700\u8981\u540c\u65f6\u5b58\u5728\u4e8e\u6d3e\u751f\u7c7b\u548c\u57fa\u7c7b\u4e2d\uff09\u3002\u8fd9\u4e2a\u673a\u5236\u79f0\u4e3a \u591a\u6001 \u3002\u5982\u679c\u6d3e\u751f\u7c7b\u4e2d\u6709\u5177\u6709\u76f8\u540c \u7b7e\u540d (\u51fd\u6570\u540d\u3001\u53c2\u6570\u7c7b\u578b\u3001\u662f\u5426\u4e3aconst\uff09\u5e76\u4e14\u8fd4\u56de\u503c\u7c7b\u578b\u4e00\u6837\u7684\u51fd\u6570\uff0c\u5219\u8fd9\u4e9b\u51fd\u6570\u79f0\u4e3a \u91cd\u5199\u51fd\u6570\uff08override\uff09 \u3002 \u5b9a\u4e49\u865a\u51fd\u6570\u65f6\uff0c\u53ea\u9700\u5728\u51fd\u6570\u58f0\u660e\u524d\u6dfb\u52a0\u201cvirtual\u201d\u5173\u952e\u5b57\u3002 \u4f7f\u7528\u865a\u51fd\u6570\u6539\u5199\u4e0a\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include class Base { public : virtual std :: string_view getName () const { return \"Base\" ; } // note addition of virtual keyword }; class Derived : public Base { public : virtual std :: string_view getName () const { return \"Derived\" ; } }; int main () { Derived derived ; Base & rBase { derived }; std :: cout << \"rBase is a \" << rBase . getName () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 rBase is a Derived \u56e0\u4e3a rBase \u662f Derived \u7c7b\u578b\u5bf9\u8c61\u4e2d Base \u90e8\u5206\u7684\u5f15\u7528\uff0c\u56e0\u6b64\u5f53 rBase.getName() \u88ab\u8c03\u7528\u65f6\uff0c\u5b83\u4f1a\u89e3\u6790\u4e3a Base::getName() \u3002\u4e0d\u8fc7\uff0c\u7531\u4e8e\u8be5\u51fd\u6570\u662f\u865a\u51fd\u6570\uff0c\u6240\u4ee5\u7a0b\u5e8f\u4f1a\u7ee7\u7eed\u6cbf\u7740\u6d3e\u751f\u7684\u65b9\u5411\u67e5\u627e\uff0c\u5982\u679c\u5728 Base \u548c Derived \u4e2d\u5b58\u5728\u8be5\u51fd\u6570\u8fdb\u4e00\u6b65\u6d3e\u751f\u7684\u7248\u672c\uff0c\u5219\u4f1a\u8c03\u7528\u8be5\u51fd\u6570\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u5b9e\u9645\u8c03\u7528\u7684\u662f Derived::getName() \uff01 \u518d\u770b\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include #include class A { public : virtual std :: string_view getName () const { return \"A\" ; } }; class B : public A { public : virtual std :: string_view getName () const { return \"B\" ; } }; class C : public B { public : virtual std :: string_view getName () const { return \"C\" ; } }; class D : public C { public : virtual std :: string_view getName () const { return \"D\" ; } }; int main () { C c ; A & rBase { c }; //C\u7c7b\u578b\u4e2d\u7684A\u57fa\u7c7b\u90e8\u5206 std :: cout << \"rBase is a \" << rBase . getName () << '\\n' ; return 0 ; } \u4f60\u89c9\u5f97\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4f1a\u662f\u4ec0\u4e48\uff1f \u6765\u770b\u770b\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u9996\u5148\uff0c\u5b9e\u4f8b\u5316\u4e00\u4e2aC\u7c7b\u578b\u7684\u5bf9\u8c61\u3002 rBase \u662f\u4e00\u4e2aA\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u5f15\u7528C\u7c7b\u578b\u5bf9\u8c61\u4e2d\u7684A\u7c7b\u578b\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u5f53 rBase.getName() \u88ab\u8c03\u7528\u65f6\uff0c\u5b83\u4f1a\u6c42\u503c\u5f97\u5230 A::getName() \u3002\u4f46\u662f\u7531\u4e8e A::getName() \u662f\u865a\u51fd\u6570\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u8c03\u7528A\u7c7b\u578b\u548cC\u7c7b\u578b\u4e2d\u6700\u540e\u88ab\u6d3e\u751f\u7684\u8be5\u51fd\u6570\u7248\u672c\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u663e\u7136\u662f C::getName() \u3002\u6ce8\u610f\uff0c\u5b83\u4e0d\u4f1a\u89e3\u6790\u5230 D::getName() \uff0c\u56e0\u4e3a\u539f\u59cb\u5bf9\u8c61\u662f C\uff0c\u800c\u4e0d\u662fD\uff0c\u6240\u4ee5D\u7684\u6210\u5458\u4e0d\u5728\u8003\u8651\u8303\u56f4\u5185\u3002 \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 rBase is a C","title":"\u865a\u51fd\u6570\u548c\u591a\u6001"},{"location":"18-2-virtual-functions-and-polymorphism/#_2","text":"\u518d\u6765\u770b\u770b\u6211\u4eec\u5728\u4e0a\u4e00\u8bfe\u4e2d\u4f7f\u7528\u7684\u52a8\u7269\u7684\u4f8b\u5b50\u3002\u4e0b\u9762\u662f\u539f\u59cb\u7684\u4ee3\u7801\uff0c\u4ee5\u53ca\u4e00\u4e9b\u6d4b\u8bd5\u4ee3\u7801: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 #include #include #include class Animal { protected : std :: string m_name ; // We're making this constructor protected because // we don't want people creating Animal objects directly, // but we still want derived classes to be able to use it. Animal ( const std :: string & name ) : m_name { name } { } public : const std :: string & getName () const { return m_name ; } std :: string_view speak () const { return \"???\" ; } }; class Cat : public Animal { public : Cat ( const std :: string & name ) : Animal { name } { } std :: string_view speak () const { return \"Meow\" ; } }; class Dog : public Animal { public : Dog ( const std :: string & name ) : Animal { name } { } std :: string_view speak () const { return \"Woof\" ; } }; void report ( const Animal & animal ) { std :: cout << animal . getName () << \" says \" << animal . speak () << '\\n' ; } int main () { Cat cat { \"Fred\" }; Dog dog { \"Garbo\" }; report ( cat ); report ( dog ); return 0 ; } \u8fd0\u884c\u7ed3\u679c 1 2 Fred says ??? Garbo says ??? \u63a5\u4e0b\u6765\u7684\u4f8b\u5b50\u4e2d\uff0c speak() \u51fd\u6570\u88ab\u5b9a\u4e49\u4e3a\u865a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 #include #include #include class Animal { protected : std :: string m_name ; // We're making this constructor protected because // we don't want people creating Animal objects directly, // but we still want derived classes to be able to use it. Animal ( const std :: string & name ) : m_name { name } { } public : const std :: string & getName () const { return m_name ; } virtual std :: string_view speak () const { return \"???\" ; } //\u865a\u51fd\u6570 }; class Cat : public Animal { public : Cat ( const std :: string & name ) : Animal { name } { } virtual std :: string_view speak () const { return \"Meow\" ; } }; class Dog : public Animal { public : Dog ( const std :: string & name ) : Animal { name } { } virtual std :: string_view speak () const { return \"Woof\" ; } }; void report ( const Animal & animal ) { std :: cout << animal . getName () << \" says \" << animal . speak () << '\\n' ; } int main () { Cat cat { \"Fred\" }; Dog dog { \"Garbo\" }; report ( cat ); report ( dog ); return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Fred says Meow Garbo says Woof \u53ef\u4ee5\u6b63\u786e\u5de5\u4f5c\u4e86\u3002 \u5f53 animal.speak() \u6c42\u804c\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u6ce8\u610f\u5230 Animal::speak() \u662f\u4e00\u4e2a\u865a\u51fd\u6570\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c animal \u5f15\u7528\u7684\u662f Cat \u7c7b\u4e2d\u7684 Animal \u90e8\u5206\u3002\u7a0b\u5e8f\u4f1a\u5728Animal \u548c Cat \u4e4b\u95f4\u6240\u6709\u7684\u6d3e\u751f\u7c7b\u4e2d\u67e5\u770b\u662f\u5426\u6709\u8be5\u51fd\u6570\u8fdb\u4e00\u6b65\u88ab\u6d3e\u751f\u7684\u7248\u672c\u3002\u5728\u672c\u4f8b\u4e2d\uff0c Cat::speak() \u5c31\u4f1a\u88ab\u8c03\u7528\u3002\u5bf9\u4e8e Dog \u4e5f\u7c7b\u4f3c\uff0c\u7a0b\u5e8f\u89e3\u6790\u5230\u7684\u51fd\u6570\u662f Dog::speak() \u3002 \u6ce8\u610f\uff0c\u6211\u4eec\u6ca1\u6709\u5c06 Animal::getName() \u5b9a\u4e49\u4e3a\u865a\u51fd\u6570\u3002\u8fd9\u662f\u56e0\u4e3a getName() \u5e76\u6ca1\u6709\u5728\u4efb\u4f55\u6d3e\u751f\u7c7b\u4e2d\u88ab \u91cd\u5199 \uff0c\u6240\u4ee5\u6ca1\u6709\u5fc5\u8981\u8bbe\u7f6e\u4e3a\u865a\u51fd\u6570\u3002 \u540c\u6837\u5730\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u4e5f\u80fd\u591f\u6309\u7167\u6211\u4eec\u7684\u9700\u8981\u6b63\u5e38\u5de5\u4f5c\u4e86\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 Cat fred { \"Fred\" }; Cat misty { \"Misty\" }; Cat zeke { \"Zeke\" }; Dog garbo { \"Garbo\" }; Dog pooky { \"Pooky\" }; Dog truffle { \"Truffle\" }; // Set up an array of pointers to animals, and set those pointers to our Cat and Dog objects Animal * animals []{ & fred , & garbo , & misty , & pooky , & truffle , & zeke }; for ( const auto * animal : animals ) std :: cout << animal -> getName () << \" says \" << animal -> speak () << '\\n' ; \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 Fred says Meow Garbo says Woof Misty says Meow Pooky says Woof Truffle says Woof Zeke says Meow \u5c3d\u7ba1\u8fd9\u4e24\u4e2a\u4f8b\u5b50\u53ea\u4f7f\u7528\u4e86 Cat \u548c Dog \uff0c\u4f46\u6211\u4eec\u4ece Animal \u6d3e\u751f\u7684\u4efb\u4f55\u5176\u4ed6\u7c7b\u4e5f\u53ef\u4ee5\u4f7f\u7528 report() \u51fd\u6570\u548c Animal \u6570\u7ec4\uff0c\u65e0\u9700\u8fdb\u4e00\u6b65\u4fee\u6539\uff01\u8fd9\u53ef\u80fd\u662f\u865a\u51fd\u6570\u7684\u6700\u5927\u597d\u5904\u2014\u2014\u80fd\u591f\u4ee5\u8fd9\u6837\u4e00\u79cd\u65b9\u5f0f\u6784\u9020\u4ee3\u7801\uff0c\u5373\u65b0\u6d3e\u751f\u7684\u7c7b\u5c06\u81ea\u52a8\u4e0e\u65e7\u4ee3\u7801\u4e00\u8d77\u5de5\u4f5c\uff0c\u800c\u65e0\u9700\u4fee\u6539! \u6ce8\u610f\uff1a\u8981\u4f7f\u7528\u6d3e\u751f\u7c7b\u51fd\u6570\uff0c \u6d3e\u751f\u7c7b\u51fd\u6570\u7684\u7b7e\u540d\u5fc5\u987b\u4e0e\u57fa\u7c7b\u865a\u51fd\u6570\u7684\u7b7e\u540d\u5b8c\u5168\u5339\u914d \uff08\u8fd4\u56de\u503c\u4e0d\u662f\u7b7e\u540d\u7684\u4e00\u90e8\u5206\uff09\u3002\u5982\u679c\u6d3e\u751f\u7c7b\u51fd\u6570\u5177\u6709\u4e0d\u540c\u7684\u5f62\u53c2\u7c7b\u578b\uff0c\u5219\u7a0b\u5e8f\u4ecd\u53ef\u80fd\u7f16\u8bd1\uff0c\u4f46\u865a\u51fd\u6570\u5c06\u4e0d\u80fd\u6309\u9884\u671f\u89e3\u6790\u3002\u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5982\u4f55\u9632\u8303\u8fd9\u79cd\u60c5\u51b5\u3002 \u8fd8\u8981\u6ce8\u610f\uff0c \u5982\u679c\u4e00\u4e2a\u51fd\u6570\u88ab\u6807\u8bb0\u4e3a\u865a\u51fd\u6570\uff0c\u90a3\u4e48\u6240\u6709\u7684\u5339\u914d \u91cd\u5199 \u51fd\u6570\u4e5f\u88ab\u8ba4\u4e3a\u662f\u865a\u51fd\u6570 \uff0c\u5373\u4f7f\u5b83\u4eec\u6ca1\u6709\u88ab\u663e\u5f0f\u5730\u6807\u8bb0\u4e3a\u865a\u51fd\u6570\u3002","title":"\u66f4\u590d\u6742\u7684\u4f8b\u5b50"},{"location":"18-2-virtual-functions-and-polymorphism/#_3","text":"\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0c\u865a\u51fd\u6570\u7684\u51fd\u6570\u7684\u8fd4\u56de\u503c\u548c \u91cd\u5199 \u51fd\u6570\u7684\u8fd4\u56de\u503c\u5fc5\u987b\u8981\u5339\u914d\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 class Base { public : virtual int getValue () const { return 5 ; } }; class Derived : public Base { public : virtual double getValue () const { return 6.78 ; } }; \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c Derived::getValue() is \u5e76\u4e0d\u4f1a\u88ab\u8ba4\u4e3a\u662f\u5bf9 Base::getValue() \u7684\u91cd\u5199\uff0c \u56e0\u6b64\u7f16\u8bd1\u4f1a\u5931\u8d25 \u3002","title":"\u865a\u51fd\u6570\u7684\u8fd4\u56de\u503c"},{"location":"18-2-virtual-functions-and-polymorphism/#_4","text":"\u8fd9\u662f\u53e6\u4e00\u4e2a\u7ecf\u5e38\u4f24\u5230\u83dc\u9e1f\u4eec\u7684\u9677\u9631\u662f\u2014\u2014\u4e0d\u5e94\u8be5\u4ece\u6784\u9020\u51fd\u6570\u6216\u6790\u6784\u51fd\u6570\u8c03\u7528\u865a\u51fd\u6570\u3002 \u8bb0\u4f4f\uff0c\u5728\u521b\u5efa\u6d3e\u751f\u7c7b\u65f6\uff0c\u9996\u5148\u6784\u9020\u57fa\u7c7b\u90e8\u5206\u3002\u5982\u679c\u5728 Base \u6784\u9020\u51fd\u6570\u8c03\u7528\u865a\u51fd\u6570\uff0c\u800c\u7c7b\u7684\u6d3e\u751f\u90e8\u5206\u751a\u81f3\u8fd8\u6ca1\u6709\u521b\u5efa\uff0c\u5219\u65e0\u6cd5\u8c03\u7528\u51fd\u6570\u7684\u6d3e\u751f\u7248\u672c\uff0c\u56e0\u4e3a\u6ca1\u6709\u6d3e\u751f\u5bf9\u8c61\u4f9b\u6d3e\u751f\u51fd\u6570\u5904\u7406\u3002\u5728C++\u4e2d\uff0c\u5b83\u4f1a\u8c03\u7528Base\u7248\u672c\u3002\uff08 17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f \uff09 \u6790\u6784\u51fd\u6570\u4e5f\u5b58\u5728\u7c7b\u4f3c\u7684\u95ee\u9898\u3002 \u5982\u679c\u5728\u57fa\u7c7b\u6790\u6784\u51fd\u6570\u4e2d\u8c03\u7528\u865a\u51fd\u6570\uff0c\u5b83\u5c06\u59cb\u7ec8\u89e3\u6790\u4e3a\u8be5\u51fd\u6570\u7684\u57fa\u7c7b\u7248\u672c \uff0c\u56e0\u4e3a\u7c7b\u7684\u6d3e\u751f\u90e8\u5206\u5c06\u5df2\u7ecf\u88ab\u9500\u6bc1\u3002 \u6700\u4f73\u5b9e\u8df5 \u6c38\u8fdc\u4e0d\u8981\u5728\u6784\u9020\u51fd\u6570\u6216\u6790\u6784\u51fd\u6570\u4e2d\u8c03\u7528\u865a\u51fd\u6570","title":"\u4e0d\u8981\u5728\u6784\u9020\u51fd\u6570\u6216\u6790\u6784\u51fd\u6570\u4e2d\u8c03\u7528\u865a\u51fd\u6570"},{"location":"18-2-virtual-functions-and-polymorphism/#_5","text":"\u65e2\u7136\u5927\u591a\u6570\u65f6\u5019\u4f60\u4f1a\u5e0c\u671b\u51fd\u6570\u662f\u865a\u51fd\u6570\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u8ba9\u6240\u6709\u7684\u51fd\u6570\u90fd\u662f\u865a\u7684\u5462\uff1f\u56e0\u4e3a\u8fd9\u4e48\u505a\u6548\u7387\u5f88\u4f4e\u2014\u2014 \u89e3\u6790\u865a\u51fd\u6570\u8c03\u7528\u7684\u65f6\u95f4\u6bd4\u89e3\u6790\u5e38\u89c4\u51fd\u6570\u8c03\u7528\u7684\u65f6\u95f4\u957f\u3002\u6b64\u5916\uff0c\u7f16\u8bd1\u5668\u8fd8\u5fc5\u987b\u4e3a\u6bcf\u4e2a\u5177\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u865a\u51fd\u6570\u7684\u7c7b\u5bf9\u8c61\u5206\u914d\u4e00\u4e2a\u989d\u5916\u7684\u6307\u9488 \u3002\u6211\u4eec\u5c06\u5728\u672c\u7ae0\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e2a\u95ee\u9898\u3002","title":"\u865a\u51fd\u6570\u7684\u7f3a\u70b9"},{"location":"18-3-the-override-and-final-specifiers-and-covariant-return-types/","tags":["override","final","covariant-return-types"],"text":"Key Takeaway \u6d3e\u751f\u7c7b\u865a\u51fd\u6570\u53ea\u6709\u5728\u5176\u7b7e\u540d\u548c\u8fd4\u56de\u7c7b\u578b\u5b8c\u5168\u5339\u914d\u7684\u60c5\u51b5\u4e0b\u624d\u88ab\u8ba4\u4e3a\u662f\u91cd\u5199\uff0c\u4f7f\u7528\u5173\u952e\u5b57 override \u53ef\u4ee5\u9632\u6b62\u9519\u8bef \u5728\u57fa\u7c7b\u7684\u865a\u51fd\u6570\u4e2d\u4f7f\u7528 virtual \u5173\u952e\u5b57\u3002\u5728\u6d3e\u751f\u7c7b\u4e2d\u5bf9\u5e94\u7684\u51fd\u6570\u4e2d\u4f7f\u7528 override \u5173\u952e\u5b57\uff08\u65e0\u9700\u4f7f\u7528 virtual \u5173\u952e\u5b57\uff09\u3002 \u56e0\u4e3a override \u4fee\u9970\u7b26\u610f\u5473\u7740\u8be5\u51fd\u6570\u662f\u865a\u51fd\u6570\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u4f7f\u7528 virtual \u5173\u952e\u5b57\u91cd\u590d\u6807\u8bb0\u5df2\u7ecf\u88ab\u6807\u8bb0\u4e3a override \u7684\u51fd\u6570 \u5982\u679c\u865a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f\u6307\u5411\u76f8\u540c\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u5176\u91cd\u5199\u51fd\u6570\u4e5f\u53ef\u4ee5\u8fd4\u56de\u6307\u5411\u6d3e\u751f\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528\u3002\u8fd9\u4e2a\u673a\u5236\u79f0\u4e3a \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b \u4e3a\u4e86\u89e3\u51b3\u7ee7\u627f\u65b9\u9762\u7684\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\uff0cC++\u63d0\u4f9b\u4e86\u4e24\u4e2a\u7279\u6b8a\u6807\u8bc6\u7b26\uff1a override \u548c final \u3002\u6ce8\u610f\uff0c \u8fd9\u4e9b\u6807\u8bc6\u7b26\u4e0d\u88ab\u8ba4\u4e3a\u662f\u5173\u952e\u5b57 \u2014\u2014\u5b83\u4eec\u662f\u5728\u67d0\u4e9b\u4e0a\u4e0b\u6587\u4e2d\u5177\u6709\u7279\u6b8a\u542b\u4e49\u7684\u666e\u901a\u6807\u8bc6\u7b26\u3002 final \u4f7f\u7528\u5f97\u4e0d\u591a\uff0c\u4f46 override \u662f\u4e00\u4e2a\u5f88\u597d\u7684\u529f\u80fd\uff0c\u5e94\u8be5\u7ecf\u5e38\u4f7f\u7528\u3002\u5728\u8fd9\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u5206\u522b\u4ecb\u7ecd\u4e8c\u8005\uff0c\u4ee5\u53ca\u865a\u51fd\u6570\u91cd\u5199\u8fd4\u56de\u7c7b\u578b\u5fc5\u987b\u5339\u914d\u7684\u89c4\u5219\u7684\u4e00\u4e2a\u4f8b\u5916\u3002 override \u4fee\u9970\u7b26 \u6b63\u5982\u6211\u4eec\u5728\u4e0a\u4e00\u8bfe\u4e2d\u63d0\u5230\u7684\uff0c \u6d3e\u751f\u7c7b\u865a\u51fd\u6570\u53ea\u6709\u5728\u5176\u7b7e\u540d\u548c\u8fd4\u56de\u7c7b\u578b\u5b8c\u5168\u5339\u914d\u7684\u60c5\u51b5\u4e0b\u624d\u88ab\u8ba4\u4e3a\u662f\u91cd\u5199 \u3002\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5bb9\u6613\u88ab\u5ffd\u7565\u7684\u95ee\u9898\uff0c\u5373\u6253\u7b97\u88ab\u91cd\u5199\u7684\u51fd\u6570\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\u88ab\u91cd\u5199\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include class A { public : virtual std :: string_view getName1 ( int x ) { return \"A\" ; } virtual std :: string_view getName2 ( int x ) { return \"A\" ; } }; class B : public A { public : virtual std :: string_view getName1 ( short int x ) { return \"B\" ; } // \u6ce8\u610f: \u53c2\u6570\u662f short int virtual std :: string_view getName2 ( int x ) const { return \"B\" ; } // \u6ce8\u610f: \u51fd\u6570\u662f const }; int main () { B b {}; A & rBase { b }; std :: cout << rBase . getName1 ( 1 ) << '\\n' ; std :: cout << rBase . getName2 ( 2 ) << '\\n' ; return 0 ; } \u56e0\u4e3a rBase \u662fB\u4e2dA\u90e8\u5206\u7684\u5f15\u7528\uff0c\u6240\u4ee5\u8fd9\u91cc\u7684\u672c\u610f\u662f\u4f7f\u7528\u865a\u51fd\u6570\u6765\u8bbf\u95ee B::getName1() \u548c B::getName2() \u3002\u4f46\u662f\uff0c\u7531\u4e8e B::getName1() \u7684\u53c2\u6570\u5e76\u4e0d\u540c\u6240\u4ee5\u6ca1\u6709\u88ab\u770b\u505a\u662f\u5bf9 A::getName1() \u7684\u91cd\u5199\u3002\u66f4\u9690\u853d\u7684\u662f\uff0c\u56e0\u4e3a B::getName2() \u662f const \u800c A::getName2() \u4e0d\u662f\uff0c\u6240\u4ee5 B::getName2() \u4e5f\u4e0d\u4f1a\u88ab\u770b\u505a\u662f A::getName2() \u7684\u91cd\u5199\u3002 \u56e0\u6b64\uff0c\u7a0b\u5e8f\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 A A \u5728\u672c\u4f8b\u4e2d\uff0c\u56e0\u4e3aA\u548cB\u53ea\u662f\u6253\u5370\u4e86\u5b83\u4eec\u5404\u81ea\u7684\u540d\u79f0\uff0c\u6240\u4ee5\u5f88\u5bb9\u6613\u53d1\u73b0\u9519\u8bef\uff0c\u5373\u56e0\u4e3a\u91cd\u5199\u6ca1\u6709\u751f\u6548\u800c\u8c03\u7528\u4e86\u9519\u8bef\u7684\u865a\u51fd\u6570\u3002\u7136\u800c\uff0c\u5728\u66f4\u590d\u6742\u7684\u7a0b\u5e8f\u4e2d\uff0c\u51fd\u6570\u7684\u884c\u4e3a\u6216\u8fd4\u56de\u503c\u6ca1\u6709\u6253\u5370\u51fa\u6765\uff0c\u8fd9\u6837\u7684\u95ee\u9898\u5f88\u96be\u8c03\u8bd5\u3002 \u4e3a\u4e86\u5e2e\u52a9\u89e3\u51b3\u672c\u8be5\u88ab\u91cd\u5199\u4f46\u5374\u6ca1\u6709\u88ab\u91cd\u5199\u7684\u51fd\u6570\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u5c06 override \u4fee\u9970\u7b26\u653e\u7f6e\u5728const\u5c06\u8981\u653e\u7f6e\u7684\u4f4d\u7f6e\uff0c\u4ece\u800c\u5c06 override \u5e94\u7528\u4e8e\u4efb\u4f55\u865a\u51fd\u6570\u3002\u5982\u679c\u57fa\u7c7b\u51fd\u6570\u6ca1\u6709\u91cd\u5199(\u6216\u5e94\u7528\u4e8e\u975e\u865a\u51fd\u6570)\uff0c\u7f16\u8bd1\u5668\u5c06\u4f1a\u9519\u8bef\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include class A { public : virtual std :: string_view getName1 ( int x ) { return \"A\" ; } virtual std :: string_view getName2 ( int x ) { return \"A\" ; } virtual std :: string_view getName3 ( int x ) { return \"A\" ; } }; class B : public A { public : std :: string_view getName1 ( short int x ) override { return \"B\" ; } // compile error, function is not an override std :: string_view getName2 ( int x ) const override { return \"B\" ; } // compile error, function is not an override std :: string_view getName3 ( int x ) override { return \"B\" ; } // okay, function is an override of A::getName3(int) }; int main () { return 0 ; } \u4e0a\u9762\u7684\u7a0b\u5e8f\u4f1a\u62a5\u544a\u4e24\u4e2a\u7f16\u8bd1\u9519\u8bef\uff1a\u7b2c\u4e00\u4e2a\u662f B::getName1() \u76f8\u5173\u9519\u8bef\uff0c\u7b2c\u4e8c\u4e2a\u662f B::getName2() \u76f8\u5173\u9519\u8bef\u3002\u56e0\u5b83\u4eec\u90fd\u6ca1\u6709\u6210\u529f\u5730\u91cd\u5199\u57fa\u7c7b\u4e2d\u7684\u51fd\u6570\u3002 B::getName3() \u91cd\u5199\u6210\u529f\uff0c\u6240\u4ee5\u6ca1\u6709\u95ee\u9898\u3002 \u56e0\u4e3a\u4f7f\u7528 override \u8bf4\u660e\u7b26\u4e0d\u4f1a\u5f71\u54cd\u6027\u80fd\uff0c\u800c\u4e14\u5b83\u6709\u52a9\u4e8e\u786e\u4fdd\u91cd\u5199\u5b9e\u9645\u53d1\u751f\uff0c\u6240\u4ee5\u5e94\u8be5\u4f7f\u7528\u8be5\u4fee\u9970\u7b26\u6807\u8bb0\u6240\u6709\u865a\u51fd\u6570\u3002\u6b64\u5916\uff0c\u56e0\u4e3a override \u4fee\u9970\u7b26\u610f\u5473\u7740\u8be5\u51fd\u6570\u662f\u865a\u51fd\u6570\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u4f7f\u7528 virtual \u5173\u952e\u5b57\u91cd\u590d\u6807\u8bb0\u5df2\u7ecf\u88ab\u6807\u8bb0\u4e3a override \u7684\u51fd\u6570\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u57fa\u7c7b\u7684\u865a\u51fd\u6570\u4e2d\u4f7f\u7528 virtual \u5173\u952e\u5b57\u3002\u5728\u6d3e\u751f\u7c7b\u4e2d\u5bf9\u5e94\u7684\u51fd\u6570\u4e2d\u4f7f\u7528 override \u5173\u952e\u5b57\uff08\u65e0\u9700\u4f7f\u7528 virtual \u5173\u952e\u5b57\uff09\u3002 final \u4fee\u9970\u7b26 \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u4e0d\u5e0c\u671b\u6709\u4eba\u80fd\u591f\u91cd\u5199\u865a\u51fd\u6570\u6216\u4e0d\u5e0c\u671b\u7c7b\u88ab\u7ee7\u627f\u3002 final \u4fee\u9970\u7b26\u53ef\u4ee5\u7528\u6765\u544a\u8bc9\u7f16\u8bd1\u5668\u786e\u4fdd\u8fd9\u4e00\u70b9\u3002\u5982\u679c\u7528\u6237\u8bd5\u56fe\u91cd\u5199\u88ab\u6807\u8bb0\u4e3a final \u7684\u51fd\u6570\u6216\u7ee7\u627f\u5df2\u6307\u5b9a\u4e3a final \u7684\u7c7b\uff0c\u7f16\u8bd1\u5668\u5c06\u7ed9\u51fa\u7f16\u8bd1\u9519\u8bef\u3002 \u5728\u6211\u4eec\u60f3\u8981\u9650\u5236\u7528\u6237\u91cd\u5199\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c final \u4fee\u9970\u7b26\u7528\u5728 override \u4fee\u9970\u7b26\u7684\u76f8\u540c\u4f4d\u7f6e\uff0c\u5982\u4e0b\u6240\u793a: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include class A { public : virtual std :: string_view getName () { return \"A\" ; } }; class B : public A { public : // note use of final specifier on following line -- that makes this function no longer overridable std :: string_view getName () override final { return \"B\" ; } // okay, \u91cd\u5199 A::getName() }; class C : public B { public : std :: string_view getName () override { return \"C\" ; } // \u7f16\u8bd1\u9519\u8bef: \u8bd5\u56fe\u91cd\u5199\u88ab\u6807\u8bb0\u4e3a final \u7684 B::getName() }; \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c B::getName() \u91cd\u5199\u4e86 A::getName() \uff0c\u8fd9\u91cc\u6ca1\u6709\u4efb\u4f55\u95ee\u9898\u3002\u4f46\u662f\u7531\u4e8e B::getName() \u5177\u6709 final \u4fee\u9970\u7b26\uff0c\u6240\u4ee5\u4efb\u4f55\u5bf9\u5b83\u7684\u91cd\u5199\u90fd\u662f\u4e0d\u5141\u8bb8\u7684\uff0c\u4f1a\u88ab\u8ba4\u4e3a\u662f\u9519\u8bef\u3002 C::getName() \u5bf9 B::getName() (\u4e0e\u8fd9\u91cc\u7684 override \u8bf4\u660e\u7b26\u5e76\u4e0d\u76f8\u5173\uff0c\u8fd9\u91cc\u53ea\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\u7f62\u4e86) \u6765\u8bf4\u662f\u91cd\u5199\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002 \u5728\u6211\u4eec\u60f3\u8981\u9632\u6b62\u4ece\u4e00\u4e2a\u7c7b\u7ee7\u627f\u7684\u60c5\u51b5\u4e0b\uff0c final \u4fee\u9970\u7b26\u5e94\u7528\u5728\u7c7b\u540d\u4e4b\u540e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include class A { public : virtual std :: string_view getName () { return \"A\" ; } }; class B final : public A // note use of final specifier here { public : std :: string_view getName () override { return \"B\" ; } }; class C : public B // compile error: cannot inherit from final class { public : std :: string_view getName () override { return \"C\" ; } }; \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7c7bB\u88ab\u58f0\u660e\u4e3afinal\u3002\u56e0\u6b64\uff0c\u5f53C\u8bd5\u56fe\u4eceB\u7ee7\u627f\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u7ed9\u51fa\u4e00\u4e2a\u7f16\u8bd1\u9519\u8bef\u3002 \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b \u6709\u4e00\u79cd\u60c5\u51b5\u4e0b\uff0c\u6d3e\u751f\u7c7b\u4e2d\u91cd\u5199\u7684\u865a\u51fd\u6570\uff0c\u53ef\u4ee5\u548c\u57fa\u7c7b\u4e2d\u7684\u865a\u51fd\u6570\u5177\u6709\u4e0d\u540c\u7684\u8fd4\u56de\u503c\uff0c\u4f46\u4ecd\u7136\u88ab\u8ba4\u4e3a\u662f\u6709\u6548\u7684\u91cd\u5199\u3002 \u5982\u679c\u865a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f\u6307\u5411\u76f8\u540c\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u5176\u91cd\u5199\u51fd\u6570\u4e5f\u53ef\u4ee5\u8fd4\u56de\u6307\u5411\u6d3e\u751f\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528\u3002\u8fd9\u4e2a\u673a\u5236\u79f0\u4e3a \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b \u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include #include class Base { public : // This version of getThis() returns a pointer to a Base class virtual Base * getThis () { std :: cout << \"called Base::getThis() \\n \" ; return this ; } void printType () { std :: cout << \"returned a Base \\n \" ; } }; class Derived : public Base { public : // Normally override functions have to return objects of the same type as the base function // However, because Derived is derived from Base, it's okay to return Derived* instead of Base* Derived * getThis () override { std :: cout << \"called Derived::getThis() \\n \" ; return this ; } void printType () { std :: cout << \"returned a Derived \\n \" ; } }; int main () { Derived d {}; Base * b { & d }; d . getThis () -> printType (); // \u8c03\u7528 Derived::getThis(), \u8fd4\u56de Derived*, \u8c03\u7528 Derived::printType b -> getThis () -> printType (); // \u8c03\u7528 Derived::getThis(), \u8fd4\u56de Base*, \u8c03\u7528 Base::printType return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 4 called Derived::getThis() returned a Derived called Derived::getThis() returned a Base \u5173\u4e8e\u534f\u53d8\u8fd4\u56de\u7c7b\u578b\u6709\u4e00\u4e2a\u6709\u8da3\u7684\u6ce8\u610f\u4e8b\u9879\uff1aC++\u4e0d\u652f\u6301\u52a8\u6001\u7c7b\u578b\uff0c\u56e0\u6b64\u4f60\u603b\u662f\u5f97\u5230\u4e0e\u6240\u8c03\u7528\u51fd\u6570\u7684\u5b9e\u9645\u7248\u672c\u76f8\u5339\u914d\u7684\u7c7b\u8fd4\u56de\u503c\u578b\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u8c03\u7528 d.getThis() \u3002\u56e0\u4e3a d \u662f\u6d3e\u751f\u7684\uff0c\u6240\u4ee5\u8c03\u7528 Derived::getThis() \uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a Derived* \u3002\u7136\u540e\u8fd9\u4e2a Derived* \u88ab\u7528\u6765\u8c03\u7528\u975e\u865a\u51fd\u6570 Derived::printType() \u3002 \u6362\u8a00\u4e4b\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u4f60\u4f7f\u7528 Derived \u7c7b\u578b\u7684\u5bf9\u8c61\u8c03\u7528 getThis() \uff0c\u90a3\u4f60\u5f97\u5230\u7684\u5c31\u662f Derived* \u7c7b\u578b\u7684\u8fd4\u56de\u503c\u3002 \u5982\u679c printType() \u662f\u865a\u51fd\u6570\u7684\u8bdd\u60c5\u51b5\u5219\u4e0d\u540c\uff0c\u6b64\u65f6 b->getThis() ( Base* \u7684\u5bf9\u8c61) \u5c31\u4f1a\u8fdb\u884c\u865a\u51fd\u6570\u89e3\u6790\uff0c\u8c03\u7528 Derived::printType() \u3002 \u534f\u53d8\u8fd4\u56de\u7c7b\u578b\u901a\u5e38\u7528\u4e8e\u8fd4\u56de\u4e00\u4e2a\u6307\u9488\u6216\u5f15\u7528\uff0c\u8be5\u6307\u9488\u6216\u5f15\u7528\u6240\u6307\u7684\u5bf9\u8c61\uff0c\u5305\u542b\u8be5\u6210\u5458\u51fd\u6570(\u4f8b\u5982 Base::getThis() \u8fd4\u56de Base* \uff0c Derived::getThis() \u8fd4\u56de Derived* )\u7684\u60c5\u51b5\u3002\u7136\u800c\uff0c\u5e76\u4e0d\u662f\u5b83\u552f\u4e00\u7684\u7528\u6cd5\uff0c\u5f53 \u91cd\u5199 \u6210\u5458\u51fd\u6570\u6240\u5c5e\u7684\u7c7b\u578b\uff0c\u4e0e\u57fa\u7c7b\u865a\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u5c5e\u4e8e\u6d3e\u751f\u5173\u7cfb\u65f6\uff0c\u90fd\u53ef\u4ee5\u4f7f\u7528\u534f\u53d8\u8fd4\u56de\u7c7b\u578b\u3002","title":"18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b"},{"location":"18-3-the-override-and-final-specifiers-and-covariant-return-types/#override","text":"\u6b63\u5982\u6211\u4eec\u5728\u4e0a\u4e00\u8bfe\u4e2d\u63d0\u5230\u7684\uff0c \u6d3e\u751f\u7c7b\u865a\u51fd\u6570\u53ea\u6709\u5728\u5176\u7b7e\u540d\u548c\u8fd4\u56de\u7c7b\u578b\u5b8c\u5168\u5339\u914d\u7684\u60c5\u51b5\u4e0b\u624d\u88ab\u8ba4\u4e3a\u662f\u91cd\u5199 \u3002\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5bb9\u6613\u88ab\u5ffd\u7565\u7684\u95ee\u9898\uff0c\u5373\u6253\u7b97\u88ab\u91cd\u5199\u7684\u51fd\u6570\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\u88ab\u91cd\u5199\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include class A { public : virtual std :: string_view getName1 ( int x ) { return \"A\" ; } virtual std :: string_view getName2 ( int x ) { return \"A\" ; } }; class B : public A { public : virtual std :: string_view getName1 ( short int x ) { return \"B\" ; } // \u6ce8\u610f: \u53c2\u6570\u662f short int virtual std :: string_view getName2 ( int x ) const { return \"B\" ; } // \u6ce8\u610f: \u51fd\u6570\u662f const }; int main () { B b {}; A & rBase { b }; std :: cout << rBase . getName1 ( 1 ) << '\\n' ; std :: cout << rBase . getName2 ( 2 ) << '\\n' ; return 0 ; } \u56e0\u4e3a rBase \u662fB\u4e2dA\u90e8\u5206\u7684\u5f15\u7528\uff0c\u6240\u4ee5\u8fd9\u91cc\u7684\u672c\u610f\u662f\u4f7f\u7528\u865a\u51fd\u6570\u6765\u8bbf\u95ee B::getName1() \u548c B::getName2() \u3002\u4f46\u662f\uff0c\u7531\u4e8e B::getName1() \u7684\u53c2\u6570\u5e76\u4e0d\u540c\u6240\u4ee5\u6ca1\u6709\u88ab\u770b\u505a\u662f\u5bf9 A::getName1() \u7684\u91cd\u5199\u3002\u66f4\u9690\u853d\u7684\u662f\uff0c\u56e0\u4e3a B::getName2() \u662f const \u800c A::getName2() \u4e0d\u662f\uff0c\u6240\u4ee5 B::getName2() \u4e5f\u4e0d\u4f1a\u88ab\u770b\u505a\u662f A::getName2() \u7684\u91cd\u5199\u3002 \u56e0\u6b64\uff0c\u7a0b\u5e8f\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 A A \u5728\u672c\u4f8b\u4e2d\uff0c\u56e0\u4e3aA\u548cB\u53ea\u662f\u6253\u5370\u4e86\u5b83\u4eec\u5404\u81ea\u7684\u540d\u79f0\uff0c\u6240\u4ee5\u5f88\u5bb9\u6613\u53d1\u73b0\u9519\u8bef\uff0c\u5373\u56e0\u4e3a\u91cd\u5199\u6ca1\u6709\u751f\u6548\u800c\u8c03\u7528\u4e86\u9519\u8bef\u7684\u865a\u51fd\u6570\u3002\u7136\u800c\uff0c\u5728\u66f4\u590d\u6742\u7684\u7a0b\u5e8f\u4e2d\uff0c\u51fd\u6570\u7684\u884c\u4e3a\u6216\u8fd4\u56de\u503c\u6ca1\u6709\u6253\u5370\u51fa\u6765\uff0c\u8fd9\u6837\u7684\u95ee\u9898\u5f88\u96be\u8c03\u8bd5\u3002 \u4e3a\u4e86\u5e2e\u52a9\u89e3\u51b3\u672c\u8be5\u88ab\u91cd\u5199\u4f46\u5374\u6ca1\u6709\u88ab\u91cd\u5199\u7684\u51fd\u6570\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u5c06 override \u4fee\u9970\u7b26\u653e\u7f6e\u5728const\u5c06\u8981\u653e\u7f6e\u7684\u4f4d\u7f6e\uff0c\u4ece\u800c\u5c06 override \u5e94\u7528\u4e8e\u4efb\u4f55\u865a\u51fd\u6570\u3002\u5982\u679c\u57fa\u7c7b\u51fd\u6570\u6ca1\u6709\u91cd\u5199(\u6216\u5e94\u7528\u4e8e\u975e\u865a\u51fd\u6570)\uff0c\u7f16\u8bd1\u5668\u5c06\u4f1a\u9519\u8bef\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include class A { public : virtual std :: string_view getName1 ( int x ) { return \"A\" ; } virtual std :: string_view getName2 ( int x ) { return \"A\" ; } virtual std :: string_view getName3 ( int x ) { return \"A\" ; } }; class B : public A { public : std :: string_view getName1 ( short int x ) override { return \"B\" ; } // compile error, function is not an override std :: string_view getName2 ( int x ) const override { return \"B\" ; } // compile error, function is not an override std :: string_view getName3 ( int x ) override { return \"B\" ; } // okay, function is an override of A::getName3(int) }; int main () { return 0 ; } \u4e0a\u9762\u7684\u7a0b\u5e8f\u4f1a\u62a5\u544a\u4e24\u4e2a\u7f16\u8bd1\u9519\u8bef\uff1a\u7b2c\u4e00\u4e2a\u662f B::getName1() \u76f8\u5173\u9519\u8bef\uff0c\u7b2c\u4e8c\u4e2a\u662f B::getName2() \u76f8\u5173\u9519\u8bef\u3002\u56e0\u5b83\u4eec\u90fd\u6ca1\u6709\u6210\u529f\u5730\u91cd\u5199\u57fa\u7c7b\u4e2d\u7684\u51fd\u6570\u3002 B::getName3() \u91cd\u5199\u6210\u529f\uff0c\u6240\u4ee5\u6ca1\u6709\u95ee\u9898\u3002 \u56e0\u4e3a\u4f7f\u7528 override \u8bf4\u660e\u7b26\u4e0d\u4f1a\u5f71\u54cd\u6027\u80fd\uff0c\u800c\u4e14\u5b83\u6709\u52a9\u4e8e\u786e\u4fdd\u91cd\u5199\u5b9e\u9645\u53d1\u751f\uff0c\u6240\u4ee5\u5e94\u8be5\u4f7f\u7528\u8be5\u4fee\u9970\u7b26\u6807\u8bb0\u6240\u6709\u865a\u51fd\u6570\u3002\u6b64\u5916\uff0c\u56e0\u4e3a override \u4fee\u9970\u7b26\u610f\u5473\u7740\u8be5\u51fd\u6570\u662f\u865a\u51fd\u6570\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u4f7f\u7528 virtual \u5173\u952e\u5b57\u91cd\u590d\u6807\u8bb0\u5df2\u7ecf\u88ab\u6807\u8bb0\u4e3a override \u7684\u51fd\u6570\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u57fa\u7c7b\u7684\u865a\u51fd\u6570\u4e2d\u4f7f\u7528 virtual \u5173\u952e\u5b57\u3002\u5728\u6d3e\u751f\u7c7b\u4e2d\u5bf9\u5e94\u7684\u51fd\u6570\u4e2d\u4f7f\u7528 override \u5173\u952e\u5b57\uff08\u65e0\u9700\u4f7f\u7528 virtual \u5173\u952e\u5b57\uff09\u3002","title":"override \u4fee\u9970\u7b26"},{"location":"18-3-the-override-and-final-specifiers-and-covariant-return-types/#final","text":"\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u4e0d\u5e0c\u671b\u6709\u4eba\u80fd\u591f\u91cd\u5199\u865a\u51fd\u6570\u6216\u4e0d\u5e0c\u671b\u7c7b\u88ab\u7ee7\u627f\u3002 final \u4fee\u9970\u7b26\u53ef\u4ee5\u7528\u6765\u544a\u8bc9\u7f16\u8bd1\u5668\u786e\u4fdd\u8fd9\u4e00\u70b9\u3002\u5982\u679c\u7528\u6237\u8bd5\u56fe\u91cd\u5199\u88ab\u6807\u8bb0\u4e3a final \u7684\u51fd\u6570\u6216\u7ee7\u627f\u5df2\u6307\u5b9a\u4e3a final \u7684\u7c7b\uff0c\u7f16\u8bd1\u5668\u5c06\u7ed9\u51fa\u7f16\u8bd1\u9519\u8bef\u3002 \u5728\u6211\u4eec\u60f3\u8981\u9650\u5236\u7528\u6237\u91cd\u5199\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c final \u4fee\u9970\u7b26\u7528\u5728 override \u4fee\u9970\u7b26\u7684\u76f8\u540c\u4f4d\u7f6e\uff0c\u5982\u4e0b\u6240\u793a: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include class A { public : virtual std :: string_view getName () { return \"A\" ; } }; class B : public A { public : // note use of final specifier on following line -- that makes this function no longer overridable std :: string_view getName () override final { return \"B\" ; } // okay, \u91cd\u5199 A::getName() }; class C : public B { public : std :: string_view getName () override { return \"C\" ; } // \u7f16\u8bd1\u9519\u8bef: \u8bd5\u56fe\u91cd\u5199\u88ab\u6807\u8bb0\u4e3a final \u7684 B::getName() }; \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c B::getName() \u91cd\u5199\u4e86 A::getName() \uff0c\u8fd9\u91cc\u6ca1\u6709\u4efb\u4f55\u95ee\u9898\u3002\u4f46\u662f\u7531\u4e8e B::getName() \u5177\u6709 final \u4fee\u9970\u7b26\uff0c\u6240\u4ee5\u4efb\u4f55\u5bf9\u5b83\u7684\u91cd\u5199\u90fd\u662f\u4e0d\u5141\u8bb8\u7684\uff0c\u4f1a\u88ab\u8ba4\u4e3a\u662f\u9519\u8bef\u3002 C::getName() \u5bf9 B::getName() (\u4e0e\u8fd9\u91cc\u7684 override \u8bf4\u660e\u7b26\u5e76\u4e0d\u76f8\u5173\uff0c\u8fd9\u91cc\u53ea\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\u7f62\u4e86) \u6765\u8bf4\u662f\u91cd\u5199\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002 \u5728\u6211\u4eec\u60f3\u8981\u9632\u6b62\u4ece\u4e00\u4e2a\u7c7b\u7ee7\u627f\u7684\u60c5\u51b5\u4e0b\uff0c final \u4fee\u9970\u7b26\u5e94\u7528\u5728\u7c7b\u540d\u4e4b\u540e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include class A { public : virtual std :: string_view getName () { return \"A\" ; } }; class B final : public A // note use of final specifier here { public : std :: string_view getName () override { return \"B\" ; } }; class C : public B // compile error: cannot inherit from final class { public : std :: string_view getName () override { return \"C\" ; } }; \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7c7bB\u88ab\u58f0\u660e\u4e3afinal\u3002\u56e0\u6b64\uff0c\u5f53C\u8bd5\u56fe\u4eceB\u7ee7\u627f\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u7ed9\u51fa\u4e00\u4e2a\u7f16\u8bd1\u9519\u8bef\u3002","title":"final \u4fee\u9970\u7b26"},{"location":"18-3-the-override-and-final-specifiers-and-covariant-return-types/#_1","text":"\u6709\u4e00\u79cd\u60c5\u51b5\u4e0b\uff0c\u6d3e\u751f\u7c7b\u4e2d\u91cd\u5199\u7684\u865a\u51fd\u6570\uff0c\u53ef\u4ee5\u548c\u57fa\u7c7b\u4e2d\u7684\u865a\u51fd\u6570\u5177\u6709\u4e0d\u540c\u7684\u8fd4\u56de\u503c\uff0c\u4f46\u4ecd\u7136\u88ab\u8ba4\u4e3a\u662f\u6709\u6548\u7684\u91cd\u5199\u3002 \u5982\u679c\u865a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f\u6307\u5411\u76f8\u540c\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u5176\u91cd\u5199\u51fd\u6570\u4e5f\u53ef\u4ee5\u8fd4\u56de\u6307\u5411\u6d3e\u751f\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528\u3002\u8fd9\u4e2a\u673a\u5236\u79f0\u4e3a \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b \u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include #include class Base { public : // This version of getThis() returns a pointer to a Base class virtual Base * getThis () { std :: cout << \"called Base::getThis() \\n \" ; return this ; } void printType () { std :: cout << \"returned a Base \\n \" ; } }; class Derived : public Base { public : // Normally override functions have to return objects of the same type as the base function // However, because Derived is derived from Base, it's okay to return Derived* instead of Base* Derived * getThis () override { std :: cout << \"called Derived::getThis() \\n \" ; return this ; } void printType () { std :: cout << \"returned a Derived \\n \" ; } }; int main () { Derived d {}; Base * b { & d }; d . getThis () -> printType (); // \u8c03\u7528 Derived::getThis(), \u8fd4\u56de Derived*, \u8c03\u7528 Derived::printType b -> getThis () -> printType (); // \u8c03\u7528 Derived::getThis(), \u8fd4\u56de Base*, \u8c03\u7528 Base::printType return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 4 called Derived::getThis() returned a Derived called Derived::getThis() returned a Base \u5173\u4e8e\u534f\u53d8\u8fd4\u56de\u7c7b\u578b\u6709\u4e00\u4e2a\u6709\u8da3\u7684\u6ce8\u610f\u4e8b\u9879\uff1aC++\u4e0d\u652f\u6301\u52a8\u6001\u7c7b\u578b\uff0c\u56e0\u6b64\u4f60\u603b\u662f\u5f97\u5230\u4e0e\u6240\u8c03\u7528\u51fd\u6570\u7684\u5b9e\u9645\u7248\u672c\u76f8\u5339\u914d\u7684\u7c7b\u8fd4\u56de\u503c\u578b\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u8c03\u7528 d.getThis() \u3002\u56e0\u4e3a d \u662f\u6d3e\u751f\u7684\uff0c\u6240\u4ee5\u8c03\u7528 Derived::getThis() \uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a Derived* \u3002\u7136\u540e\u8fd9\u4e2a Derived* \u88ab\u7528\u6765\u8c03\u7528\u975e\u865a\u51fd\u6570 Derived::printType() \u3002 \u6362\u8a00\u4e4b\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u4f60\u4f7f\u7528 Derived \u7c7b\u578b\u7684\u5bf9\u8c61\u8c03\u7528 getThis() \uff0c\u90a3\u4f60\u5f97\u5230\u7684\u5c31\u662f Derived* \u7c7b\u578b\u7684\u8fd4\u56de\u503c\u3002 \u5982\u679c printType() \u662f\u865a\u51fd\u6570\u7684\u8bdd\u60c5\u51b5\u5219\u4e0d\u540c\uff0c\u6b64\u65f6 b->getThis() ( Base* \u7684\u5bf9\u8c61) \u5c31\u4f1a\u8fdb\u884c\u865a\u51fd\u6570\u89e3\u6790\uff0c\u8c03\u7528 Derived::printType() \u3002 \u534f\u53d8\u8fd4\u56de\u7c7b\u578b\u901a\u5e38\u7528\u4e8e\u8fd4\u56de\u4e00\u4e2a\u6307\u9488\u6216\u5f15\u7528\uff0c\u8be5\u6307\u9488\u6216\u5f15\u7528\u6240\u6307\u7684\u5bf9\u8c61\uff0c\u5305\u542b\u8be5\u6210\u5458\u51fd\u6570(\u4f8b\u5982 Base::getThis() \u8fd4\u56de Base* \uff0c Derived::getThis() \u8fd4\u56de Derived* )\u7684\u60c5\u51b5\u3002\u7136\u800c\uff0c\u5e76\u4e0d\u662f\u5b83\u552f\u4e00\u7684\u7528\u6cd5\uff0c\u5f53 \u91cd\u5199 \u6210\u5458\u51fd\u6570\u6240\u5c5e\u7684\u7c7b\u578b\uff0c\u4e0e\u57fa\u7c7b\u865a\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u5c5e\u4e8e\u6d3e\u751f\u5173\u7cfb\u65f6\uff0c\u90fd\u53ef\u4ee5\u4f7f\u7528\u534f\u53d8\u8fd4\u56de\u7c7b\u578b\u3002","title":"\u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b"},{"location":"18-4-virtual-destructors-virtual-assignment-and-overriding-virtualization/","tags":["virtual-destructors","virtual-assignment","overriding-virtualization"],"text":"Key Takeaway \u6790\u6784\u51fd\u6570\u7684\u8c03\u7528\u987a\u5e8f\u548c\u6784\u9020\u51fd\u6570\u76f8\u53cd\uff0c\u800c\u4e14\u662f\u5411\u57fa\u7c7b\u65b9\u5411\u9010\u7ea7\u8c03\u7528\u3002 \u57fa\u7c7b\u7684\u6790\u6784\u51fd\u6570\u5fc5\u987b\u662f \u865a\u51fd\u6570 \u3002\u5982\u679c\u4e0d\u8fd9\u6837\u505a\uff0c\u5f53\u57fa\u4e8e\u6d3e\u751f\u7c7b\u4e2d\u57fa\u7c7b\u90e8\u5206\u7684\u6307\u9488\u6216\u5f15\u7528\u65f6\u9500\u6bc1\u5bf9\u8c61\u65f6\uff0c\u8c03\u7528\u7684\u662f\u57fa\u7c7b\u7684\u6790\u6784\u51fd\u6570\uff0c\u5b50\u7c7b\u6210\u5458\u4e0d\u80fd\u88ab\u6b63\u786e\u9500\u6bc1\u3002 \u6240\u6709\u7684\u6790\u6784\u51fd\u6570\u90fd\u8bbe\u7f6e\u4e3a\u865a\u53ef\u4ee5\u907f\u514d\u4e0a\u8ff0\u95ee\u9898\uff0c\u4f46\u662f\u4f1a\u6709\u6027\u80fd\u635f\u5931\uff08\u65f6\u95f4\u548c\u7a7a\u95f4\uff0c\u6bcf\u4e2a\u7c7b\u5bf9\u8c61\u90fd\u591a\u4e86\u4e00\u4e2a\u6307\u9488\uff09 \u5982\u679c\u4e00\u4e2a\u7c7b\u5141\u8bb8\u88ab\u5176\u4ed6\u7c7b\u7ee7\u627f\uff0c\u786e\u4fdd\u5176\u6790\u6784\u51fd\u6570\u662f\u865a\u51fd\u6570\u3002 \u5982\u679c\u4e00\u4e2a\u7c7b \u4e0d\u5141\u8bb8 \u88ab\u5176\u4ed6\u7c7b\u7ee7\u627f\uff0c\u5c06\u5176\u6807\u8bb0\u4e3a final \u3002\u8fd9\u5c06\u4ece\u6839\u672c\u4e0a\u9632\u6b62\u5176\u4ed6\u7c7b\u5bf9\u5b83\u7684\u7ee7\u627f\uff0c\u800c\u4e0d\u4f1a\u5bf9\u7c7b\u672c\u8eab\u65bd\u52a0\u5176\u4ed6\u9650\u5236 \u865a\u6784\u6790\u6784\u51fd\u6570 \u5c3d\u7ba1 C++ \u53ef\u4ee5\u63d0\u4f9b\u9ed8\u8ba4\u7684\u6784\u9020\u51fd\u6570\uff0c\u4f46\u6211\u4eec\u65f6\u5e38\u4e5f\u4f1a\u60f3\u8981\u63d0\u4f9b\u81ea\u5b9a\u4e49\u7684\u6790\u6784\u51fd\u6570\uff08\u5c24\u5176\u662f\u5f53\u7c7b\u9700\u8981\u91ca\u653e\u5185\u5b58\u7684\u60c5\u51b5\uff09\u3002==\u5f53\u4e00\u4e2a\u7c7b\u6d89\u53ca\u5230 \u7ee7\u627f \u7684\u65f6\u5019\uff0c\u5176\u6790\u6784\u51fd\u6570\u5e94\u8be5\u603b\u662f\u4e3a \u865a\u6790\u6784\u51fd\u6570 \u3002==\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include class Base { public : ~ Base () // note: \u4e0d\u662f\u865a\u51fd\u6570 { std :: cout << \"Calling ~Base() \\n \" ; } }; class Derived : public Base { private : int * m_array ; public : Derived ( int length ) : m_array { new int [ length ] } { } ~ Derived () // note: \u4e0d\u662f virtual (\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u53d1\u51fa\u544a\u8b66) { std :: cout << \"Calling ~Derived() \\n \" ; delete [] m_array ; } }; int main () { Derived * derived { new Derived ( 5 ) }; Base * base { derived }; delete base ; //\u8c03\u7528\u57fa\u7c7b\u7684\u6790\u6784\u51fd\u6570\uff0c\u540c\u65f6\u56e0\u4e3a\u5b83\u4e0d\u662f\u865a\u51fd\u6570\uff0c\u5219\u5b50\u7c7b\u7684\u6790\u6784\u51fd\u6570\u4e0d\u4f1a\u88ab\u8c03\u7528\uff0c\u5b50\u7c7b\u6210\u5458\u65e0\u6cd5\u6790\u6784 return 0 ; } \u6ce8\u610f\uff1a\u5982\u679c\u4f60\u5728\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\u65f6\uff0c\u7f16\u8bd1\u5668\u8b66\u544a\u4f60\u4f7f\u7528\u4e86\u975e\u865a\u7684\u6790\u6784\u51fd\u6570\u3002\u4f60\u9700\u8981\u5173\u95ed\u7f16\u8bd1\u5668\u9009\u9879\uff1a\u5c06\u544a\u8b66\u5f53\u505a\u9519\u8bef\u5bf9\u5f85\u3002 \u56e0\u4e3a base \u662f\u4e00\u4e2a Base \u7c7b\u578b\u7684\u6307\u9488\uff0c\u5219\u5f53 base \u88ab\u5220\u9664\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u67e5\u770b Base \u7684\u6790\u6784\u51fd\u6570\u662f\u5426\u4e3a\u865a\u51fd\u6570\u3002\u5982\u679c\u4e0d\u662f\u7684\u8bdd\uff0c\u5b83\u4f1a\u8ba4\u4e3a\u4f60\u9700\u8981\u8c03\u7528\u7684\u5c31\u662f Base \u7684\u6790\u6784\u51fd\u6570\u3002 \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u80fd\u591f\u8bc1\u660e\u8fd9\u4e00\u70b9\uff1a 1 Calling ~Base() \u4f46\u662f\uff0c\u5b9e\u9645\u4e0a\u6211\u4eec\u5e0c\u671b\u80fd\u591f\u8c03\u7528 Derived \u7684\u6790\u6784\u51fd\u6570\uff08\u8fdb\u800c\u8c03\u7528 Base \u7684\u6790\u6784\u51fd\u6570)\uff0c\u5426\u5219 m_array \u662f\u6ca1\u529e\u6cd5\u88ab\u5220\u9664\u7684\u3002\u4e3a\u6b64\u6211\u4eec\u9700\u8981\u5c06 Base \u7684\u6790\u6784\u51fd\u6570\u8bbe\u7f6e\u4e3a\u865a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include class Base { public : virtual ~ Base () // note: virtual { std :: cout << \"Calling ~Base() \\n \" ; } }; class Derived : public Base { private : int * m_array ; public : Derived ( int length ) : m_array { new int [ length ] } { } virtual ~ Derived () // note: virtual { std :: cout << \"Calling ~Derived() \\n \" ; delete [] m_array ; } }; int main () { Derived * derived { new Derived ( 5 ) }; Base * base { derived }; delete base ; return 0 ; } \u6b64\u65f6\uff0c\u7a0b\u5e8f\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Calling ~Derived() Calling ~Base() \u6cd5\u5219 \u5728\u5904\u7406\u6d89\u53ca\u7ee7\u627f\u7684\u7c7b\u65f6\uff0c\u5176\u6790\u6784\u51fd\u6570\u5fc5\u987b\u663e\u5f0f\u5b9a\u4e49\u4e3a\u865a\u51fd\u6570 \u548c\u666e\u901a\u7684\u865a\u51fd\u6570\u4e00\u6837\uff0c\u5982\u679c\u57fa\u7c7b\u7684\u51fd\u6570\u662f\u865a\u51fd\u6570\uff0c\u5176\u6d3e\u751f\u7c7b\u4e2d\u6240\u6709 \u91cd\u5199 \u51fd\u6570\u90fd\u88ab\u8ba4\u4e3a\u662f\u865a\u51fd\u6570\uff0c\u4e0d\u7ba1\u6709\u4e48\u6709\u6807\u8bb0\u4e3a virtual \u3002\u6240\u4ee5\u6ca1\u6709\u5fc5\u8981\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u7684\u6d3e\u751f\u7c7b\u6790\u6784\u51fd\u6570\u5e76\u5c06\u5176\u6807\u8bb0\u4e3a virtual \u3002 \u6ce8\u610f\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u57fa\u7c7b\u7684\u865a\u6784\u9020\u51fd\u6570\u662f\u7a7a\u7684\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9a\u4e49\uff1a 1 virtual ~ Base () = default ; // generate a virtual default destructor \u865a\u8d4b\u503c \u53ef\u4ee5\u5c06\u8d4b\u503c\u64cd\u4f5c\u7b26\u8bbe\u4e3a virtual \u3002\u7136\u800c\uff0c\u4e0e\u6790\u6784\u51fd\u6570\u7684\u60c5\u51b5(\u865a\u62df\u5316\u603b\u662f\u4e00\u4e2a\u597d\u4e3b\u610f)\u4e0d\u540c\uff0c\u865a\u62df\u5316\u8d4b\u503c\u64cd\u4f5c\u7b26\u4f1a\u5e26\u6765\u4e00\u5927\u5806\u9ebb\u70e6\uff0c\u5e76\u6d89\u53ca\u672c\u6559\u7a0b\u8303\u56f4\u4e4b\u5916\u7684\u4e00\u4e9b\u9ad8\u7ea7\u4e3b\u9898\u3002\u56e0\u6b64\uff0c\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u6211\u4eec\u5efa\u8bae\u4f60\u6682\u65f6\u4e0d\u8981\u4f7f\u7528\u865a\u8d4b\u503c\u3002 \u5ffd\u7565\u865a\u5316 \u6781\u5c11\u6570\u60c5\u51b5\u4e0b\u6211\u4eec\u9700\u8981\u5ffd\u7565\u51fd\u6570\u7684\u865a\u5316\uff0c\u4f8b\u5982\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class Base { public : virtual ~ Base () = default ; virtual const char * getName () const { return \"Base\" ; } }; class Derived : public Base { public : virtual const char * getName () const { return \"Derived\" ; } }; \u53ef\u80fd\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u9700\u8981\u6307\u5411 Derived \u5bf9\u8c61\u7684 Base \u6307\u9488\u80fd\u591f\u8c03\u7528 Base::getName() \u800c\u4e0d\u662f Derived::getName() \u3002\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { Derived derived ; const Base & base { derived }; // Calls Base::getName() instead of the virtualized Derived::getName() std :: cout << base . Base :: getName () << '\\n' ; return 0 ; } \u8fd9\u4e2a\u64cd\u4f5c\u5e76\u4e0d\u5e38\u7528\uff0c\u4f46\u662f\u77e5\u9053\u603b\u6bd4\u4e0d\u77e5\u9053\u597d\u3002 \u5e94\u8be5\u5c06\u6240\u6709\u7684\u6790\u6784\u51fd\u6570\u90fd\u8bbe\u7f6e\u4e3a\u865a\u51fd\u6570\u5417\uff1f \u8fd9\u662f\u65b0\u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u95ee\u7684\u95ee\u9898\u3002\u5982\u4e0a\u9762\u7684\u4f8b\u5b50\u6240\u8ff0\uff0c\u5982\u679c\u57fa\u7c7b\u6790\u6784\u51fd\u6570\u6ca1\u6709\u88ab\u6807\u8bb0\u4e3a\u865a\u51fd\u6570\uff0c\u90a3\u4e48\u5982\u679c\u7a0b\u5e8f\u5458\u7a0d\u540e\u5220\u9664\u6307\u5411\u6d3e\u751f\u5bf9\u8c61\u7684\u57fa\u7c7b\u6307\u9488\uff0c\u5219\u7a0b\u5e8f\u6709\u5185\u5b58\u6cc4\u6f0f\u7684\u98ce\u9669\u3002\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u5c06\u6240\u6709\u6790\u6784\u51fd\u6570\u6807\u8bb0\u4e3a\u865a\u51fd\u6570\u3002\u4f46\u662f\u6211\u4eec\u771f\u7684\u9700\u8981\u8fd9\u4e48\u505a\u5417\uff1f \u8bf4\u201c\u662f\u201d\u5f88\u5bb9\u6613\uff0c\u8fd9\u6837\u4ee5\u540e\u5c31\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55\u7c7b\u4f5c\u4e3a\u57fa\u7c7b\u4e86\u2014\u2014\u4f46\u662f\u8fd9\u6837\u505a\u4f1a\u6709\u6027\u80fd\u635f\u5931(\u5411\u7c7b\u7684\u6bcf\u4e2a\u5b9e\u4f8b\u6dfb\u52a0\u4e00\u4e2a\u865a\u62df\u6307\u9488)\u3002\u6240\u4ee5\u4f60\u5fc5\u987b\u6743\u8861\u8f7b\u91cd\uff0c\u5c24\u5176\u662f\u5b83\u662f\u5426\u7b26\u5408\u4f60\u7684\u610f\u56fe\u3002 \u8457\u540d\u7684C++\u5927\u5e08Herb Sutter\u63d0\u51fa\u4e86\u4e00\u79cd\u80fd\u591f\u907f\u514d\u7531\u975e\u865a\u6790\u6784\u51fd\u6570\u5bfc\u81f4\u7684\u5185\u5b58\u6cc4\u6f0f\u7684\u65b9\u6cd5\uff1a\u201c\u57fa\u7c7b\u6790\u6784\u51fd\u6570\u5e94\u8be5\u662f \u516c\u5171\u7684\u865a\u6790\u6784\u51fd\u6570 \uff0c\u6216\u8005\u662f \u53d7\u4fdd\u62a4\u7684\u975e\u865a\u6790\u6784\u51fd\u6570 \u3002\u201d \u6709\u53d7\u4fdd\u62a4\u6790\u6784\u51fd\u6570\u7684\u7c7b\u4e0d\u80fd\u901a\u8fc7\u6307\u9488\u5220\u9664\uff0c\u56e0\u6b64\uff0c\u5f53\u57fa\u7c7b\u5177\u6709\u975e\u865a\u6790\u6784\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u9632\u6b62\u901a\u8fc7\u57fa\u6307\u9488\u610f\u5916\u5220\u9664\u6d3e\u751f\u7c7b\u3002\u4e0d\u5e78\u7684\u662f\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u57fa\u7c7b\u4e0d\u80fd\u901a\u8fc7\u57fa\u7c7b\u6307\u9488\u5220\u9664\uff0c\u8fd9\u5b9e\u9645\u4e0a\u610f\u5473\u7740\u7c7b\u53ea\u80fd\u7531\u6d3e\u751f\u7c7b\u52a8\u6001\u5206\u914d\u6216\u5220\u9664\u3002\u8fd9\u4e5f\u4f7f\u5f97\u8fd9\u4e9b\u7c7b\u4e0d\u80fd\u4f7f\u7528\u667a\u80fd\u6307\u9488(\u4f8b\u5982 std::unique_ptr \u548c std::shared_ptr )\uff0c\u4ece\u800c\u9650\u5236\u4e86\u8be5\u89c4\u5219\u7684\u6709\u7528\u6027(\u6211\u4eec\u5c06\u5728\u540e\u9762\u7684\u7ae0\u8282\u8ba8\u8bba\u667a\u80fd\u6307\u9488)\u3002\u8fd9\u4e5f\u610f\u5473\u7740\u8fd9\u6837\u7684\u57fa\u7c7b\u4e0d\u80fd\u88ab\u5206\u914d\u5728\u6808\u4e0a\u3002\u4ee3\u4ef7\u6709\u70b9\u5927\uff01 \u65e2\u7136\u5df2\u7ecf\u5728\u8bed\u8a00\u4e2d\u5f15\u5165\u4e86 final \u4fee\u9970\u7b26\uff0c\u6211\u4eec\u7684\u5efa\u8bae\u5982\u4e0b: \u5982\u679c\u4e00\u4e2a\u7c7b\u5141\u8bb8\u88ab\u5176\u4ed6\u7c7b\u7ee7\u627f\uff0c\u786e\u4fdd\u5176\u6790\u6784\u51fd\u6570\u662f\u865a\u51fd\u6570\u3002 \u5982\u679c\u4e00\u4e2a\u7c7b \u4e0d\u5141\u8bb8 \u88ab\u5176\u4ed6\u7c7b\u7ee7\u627f\uff0c\u5c06\u5176\u6807\u8bb0\u4e3a final \u3002\u8fd9\u5c06\u4ece\u6839\u672c\u4e0a\u9632\u6b62\u5176\u4ed6\u7c7b\u5bf9\u5b83\u7684\u7ee7\u627f\uff0c\u800c\u4e0d\u4f1a\u5bf9\u7c7b\u672c\u8eab\u65bd\u52a0\u5176\u4ed6\u9650\u5236","title":"18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316"},{"location":"18-4-virtual-destructors-virtual-assignment-and-overriding-virtualization/#_1","text":"\u5c3d\u7ba1 C++ \u53ef\u4ee5\u63d0\u4f9b\u9ed8\u8ba4\u7684\u6784\u9020\u51fd\u6570\uff0c\u4f46\u6211\u4eec\u65f6\u5e38\u4e5f\u4f1a\u60f3\u8981\u63d0\u4f9b\u81ea\u5b9a\u4e49\u7684\u6790\u6784\u51fd\u6570\uff08\u5c24\u5176\u662f\u5f53\u7c7b\u9700\u8981\u91ca\u653e\u5185\u5b58\u7684\u60c5\u51b5\uff09\u3002==\u5f53\u4e00\u4e2a\u7c7b\u6d89\u53ca\u5230 \u7ee7\u627f \u7684\u65f6\u5019\uff0c\u5176\u6790\u6784\u51fd\u6570\u5e94\u8be5\u603b\u662f\u4e3a \u865a\u6790\u6784\u51fd\u6570 \u3002==\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include class Base { public : ~ Base () // note: \u4e0d\u662f\u865a\u51fd\u6570 { std :: cout << \"Calling ~Base() \\n \" ; } }; class Derived : public Base { private : int * m_array ; public : Derived ( int length ) : m_array { new int [ length ] } { } ~ Derived () // note: \u4e0d\u662f virtual (\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u53d1\u51fa\u544a\u8b66) { std :: cout << \"Calling ~Derived() \\n \" ; delete [] m_array ; } }; int main () { Derived * derived { new Derived ( 5 ) }; Base * base { derived }; delete base ; //\u8c03\u7528\u57fa\u7c7b\u7684\u6790\u6784\u51fd\u6570\uff0c\u540c\u65f6\u56e0\u4e3a\u5b83\u4e0d\u662f\u865a\u51fd\u6570\uff0c\u5219\u5b50\u7c7b\u7684\u6790\u6784\u51fd\u6570\u4e0d\u4f1a\u88ab\u8c03\u7528\uff0c\u5b50\u7c7b\u6210\u5458\u65e0\u6cd5\u6790\u6784 return 0 ; } \u6ce8\u610f\uff1a\u5982\u679c\u4f60\u5728\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\u65f6\uff0c\u7f16\u8bd1\u5668\u8b66\u544a\u4f60\u4f7f\u7528\u4e86\u975e\u865a\u7684\u6790\u6784\u51fd\u6570\u3002\u4f60\u9700\u8981\u5173\u95ed\u7f16\u8bd1\u5668\u9009\u9879\uff1a\u5c06\u544a\u8b66\u5f53\u505a\u9519\u8bef\u5bf9\u5f85\u3002 \u56e0\u4e3a base \u662f\u4e00\u4e2a Base \u7c7b\u578b\u7684\u6307\u9488\uff0c\u5219\u5f53 base \u88ab\u5220\u9664\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u67e5\u770b Base \u7684\u6790\u6784\u51fd\u6570\u662f\u5426\u4e3a\u865a\u51fd\u6570\u3002\u5982\u679c\u4e0d\u662f\u7684\u8bdd\uff0c\u5b83\u4f1a\u8ba4\u4e3a\u4f60\u9700\u8981\u8c03\u7528\u7684\u5c31\u662f Base \u7684\u6790\u6784\u51fd\u6570\u3002 \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u80fd\u591f\u8bc1\u660e\u8fd9\u4e00\u70b9\uff1a 1 Calling ~Base() \u4f46\u662f\uff0c\u5b9e\u9645\u4e0a\u6211\u4eec\u5e0c\u671b\u80fd\u591f\u8c03\u7528 Derived \u7684\u6790\u6784\u51fd\u6570\uff08\u8fdb\u800c\u8c03\u7528 Base \u7684\u6790\u6784\u51fd\u6570)\uff0c\u5426\u5219 m_array \u662f\u6ca1\u529e\u6cd5\u88ab\u5220\u9664\u7684\u3002\u4e3a\u6b64\u6211\u4eec\u9700\u8981\u5c06 Base \u7684\u6790\u6784\u51fd\u6570\u8bbe\u7f6e\u4e3a\u865a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include class Base { public : virtual ~ Base () // note: virtual { std :: cout << \"Calling ~Base() \\n \" ; } }; class Derived : public Base { private : int * m_array ; public : Derived ( int length ) : m_array { new int [ length ] } { } virtual ~ Derived () // note: virtual { std :: cout << \"Calling ~Derived() \\n \" ; delete [] m_array ; } }; int main () { Derived * derived { new Derived ( 5 ) }; Base * base { derived }; delete base ; return 0 ; } \u6b64\u65f6\uff0c\u7a0b\u5e8f\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Calling ~Derived() Calling ~Base() \u6cd5\u5219 \u5728\u5904\u7406\u6d89\u53ca\u7ee7\u627f\u7684\u7c7b\u65f6\uff0c\u5176\u6790\u6784\u51fd\u6570\u5fc5\u987b\u663e\u5f0f\u5b9a\u4e49\u4e3a\u865a\u51fd\u6570 \u548c\u666e\u901a\u7684\u865a\u51fd\u6570\u4e00\u6837\uff0c\u5982\u679c\u57fa\u7c7b\u7684\u51fd\u6570\u662f\u865a\u51fd\u6570\uff0c\u5176\u6d3e\u751f\u7c7b\u4e2d\u6240\u6709 \u91cd\u5199 \u51fd\u6570\u90fd\u88ab\u8ba4\u4e3a\u662f\u865a\u51fd\u6570\uff0c\u4e0d\u7ba1\u6709\u4e48\u6709\u6807\u8bb0\u4e3a virtual \u3002\u6240\u4ee5\u6ca1\u6709\u5fc5\u8981\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u7684\u6d3e\u751f\u7c7b\u6790\u6784\u51fd\u6570\u5e76\u5c06\u5176\u6807\u8bb0\u4e3a virtual \u3002 \u6ce8\u610f\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u57fa\u7c7b\u7684\u865a\u6784\u9020\u51fd\u6570\u662f\u7a7a\u7684\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9a\u4e49\uff1a 1 virtual ~ Base () = default ; // generate a virtual default destructor","title":"\u865a\u6784\u6790\u6784\u51fd\u6570"},{"location":"18-4-virtual-destructors-virtual-assignment-and-overriding-virtualization/#_2","text":"\u53ef\u4ee5\u5c06\u8d4b\u503c\u64cd\u4f5c\u7b26\u8bbe\u4e3a virtual \u3002\u7136\u800c\uff0c\u4e0e\u6790\u6784\u51fd\u6570\u7684\u60c5\u51b5(\u865a\u62df\u5316\u603b\u662f\u4e00\u4e2a\u597d\u4e3b\u610f)\u4e0d\u540c\uff0c\u865a\u62df\u5316\u8d4b\u503c\u64cd\u4f5c\u7b26\u4f1a\u5e26\u6765\u4e00\u5927\u5806\u9ebb\u70e6\uff0c\u5e76\u6d89\u53ca\u672c\u6559\u7a0b\u8303\u56f4\u4e4b\u5916\u7684\u4e00\u4e9b\u9ad8\u7ea7\u4e3b\u9898\u3002\u56e0\u6b64\uff0c\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u6211\u4eec\u5efa\u8bae\u4f60\u6682\u65f6\u4e0d\u8981\u4f7f\u7528\u865a\u8d4b\u503c\u3002","title":"\u865a\u8d4b\u503c"},{"location":"18-4-virtual-destructors-virtual-assignment-and-overriding-virtualization/#_3","text":"\u6781\u5c11\u6570\u60c5\u51b5\u4e0b\u6211\u4eec\u9700\u8981\u5ffd\u7565\u51fd\u6570\u7684\u865a\u5316\uff0c\u4f8b\u5982\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 class Base { public : virtual ~ Base () = default ; virtual const char * getName () const { return \"Base\" ; } }; class Derived : public Base { public : virtual const char * getName () const { return \"Derived\" ; } }; \u53ef\u80fd\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u9700\u8981\u6307\u5411 Derived \u5bf9\u8c61\u7684 Base \u6307\u9488\u80fd\u591f\u8c03\u7528 Base::getName() \u800c\u4e0d\u662f Derived::getName() \u3002\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { Derived derived ; const Base & base { derived }; // Calls Base::getName() instead of the virtualized Derived::getName() std :: cout << base . Base :: getName () << '\\n' ; return 0 ; } \u8fd9\u4e2a\u64cd\u4f5c\u5e76\u4e0d\u5e38\u7528\uff0c\u4f46\u662f\u77e5\u9053\u603b\u6bd4\u4e0d\u77e5\u9053\u597d\u3002","title":"\u5ffd\u7565\u865a\u5316"},{"location":"18-4-virtual-destructors-virtual-assignment-and-overriding-virtualization/#_4","text":"\u8fd9\u662f\u65b0\u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u95ee\u7684\u95ee\u9898\u3002\u5982\u4e0a\u9762\u7684\u4f8b\u5b50\u6240\u8ff0\uff0c\u5982\u679c\u57fa\u7c7b\u6790\u6784\u51fd\u6570\u6ca1\u6709\u88ab\u6807\u8bb0\u4e3a\u865a\u51fd\u6570\uff0c\u90a3\u4e48\u5982\u679c\u7a0b\u5e8f\u5458\u7a0d\u540e\u5220\u9664\u6307\u5411\u6d3e\u751f\u5bf9\u8c61\u7684\u57fa\u7c7b\u6307\u9488\uff0c\u5219\u7a0b\u5e8f\u6709\u5185\u5b58\u6cc4\u6f0f\u7684\u98ce\u9669\u3002\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u5c06\u6240\u6709\u6790\u6784\u51fd\u6570\u6807\u8bb0\u4e3a\u865a\u51fd\u6570\u3002\u4f46\u662f\u6211\u4eec\u771f\u7684\u9700\u8981\u8fd9\u4e48\u505a\u5417\uff1f \u8bf4\u201c\u662f\u201d\u5f88\u5bb9\u6613\uff0c\u8fd9\u6837\u4ee5\u540e\u5c31\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55\u7c7b\u4f5c\u4e3a\u57fa\u7c7b\u4e86\u2014\u2014\u4f46\u662f\u8fd9\u6837\u505a\u4f1a\u6709\u6027\u80fd\u635f\u5931(\u5411\u7c7b\u7684\u6bcf\u4e2a\u5b9e\u4f8b\u6dfb\u52a0\u4e00\u4e2a\u865a\u62df\u6307\u9488)\u3002\u6240\u4ee5\u4f60\u5fc5\u987b\u6743\u8861\u8f7b\u91cd\uff0c\u5c24\u5176\u662f\u5b83\u662f\u5426\u7b26\u5408\u4f60\u7684\u610f\u56fe\u3002 \u8457\u540d\u7684C++\u5927\u5e08Herb Sutter\u63d0\u51fa\u4e86\u4e00\u79cd\u80fd\u591f\u907f\u514d\u7531\u975e\u865a\u6790\u6784\u51fd\u6570\u5bfc\u81f4\u7684\u5185\u5b58\u6cc4\u6f0f\u7684\u65b9\u6cd5\uff1a\u201c\u57fa\u7c7b\u6790\u6784\u51fd\u6570\u5e94\u8be5\u662f \u516c\u5171\u7684\u865a\u6790\u6784\u51fd\u6570 \uff0c\u6216\u8005\u662f \u53d7\u4fdd\u62a4\u7684\u975e\u865a\u6790\u6784\u51fd\u6570 \u3002\u201d \u6709\u53d7\u4fdd\u62a4\u6790\u6784\u51fd\u6570\u7684\u7c7b\u4e0d\u80fd\u901a\u8fc7\u6307\u9488\u5220\u9664\uff0c\u56e0\u6b64\uff0c\u5f53\u57fa\u7c7b\u5177\u6709\u975e\u865a\u6790\u6784\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u9632\u6b62\u901a\u8fc7\u57fa\u6307\u9488\u610f\u5916\u5220\u9664\u6d3e\u751f\u7c7b\u3002\u4e0d\u5e78\u7684\u662f\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u57fa\u7c7b\u4e0d\u80fd\u901a\u8fc7\u57fa\u7c7b\u6307\u9488\u5220\u9664\uff0c\u8fd9\u5b9e\u9645\u4e0a\u610f\u5473\u7740\u7c7b\u53ea\u80fd\u7531\u6d3e\u751f\u7c7b\u52a8\u6001\u5206\u914d\u6216\u5220\u9664\u3002\u8fd9\u4e5f\u4f7f\u5f97\u8fd9\u4e9b\u7c7b\u4e0d\u80fd\u4f7f\u7528\u667a\u80fd\u6307\u9488(\u4f8b\u5982 std::unique_ptr \u548c std::shared_ptr )\uff0c\u4ece\u800c\u9650\u5236\u4e86\u8be5\u89c4\u5219\u7684\u6709\u7528\u6027(\u6211\u4eec\u5c06\u5728\u540e\u9762\u7684\u7ae0\u8282\u8ba8\u8bba\u667a\u80fd\u6307\u9488)\u3002\u8fd9\u4e5f\u610f\u5473\u7740\u8fd9\u6837\u7684\u57fa\u7c7b\u4e0d\u80fd\u88ab\u5206\u914d\u5728\u6808\u4e0a\u3002\u4ee3\u4ef7\u6709\u70b9\u5927\uff01 \u65e2\u7136\u5df2\u7ecf\u5728\u8bed\u8a00\u4e2d\u5f15\u5165\u4e86 final \u4fee\u9970\u7b26\uff0c\u6211\u4eec\u7684\u5efa\u8bae\u5982\u4e0b: \u5982\u679c\u4e00\u4e2a\u7c7b\u5141\u8bb8\u88ab\u5176\u4ed6\u7c7b\u7ee7\u627f\uff0c\u786e\u4fdd\u5176\u6790\u6784\u51fd\u6570\u662f\u865a\u51fd\u6570\u3002 \u5982\u679c\u4e00\u4e2a\u7c7b \u4e0d\u5141\u8bb8 \u88ab\u5176\u4ed6\u7c7b\u7ee7\u627f\uff0c\u5c06\u5176\u6807\u8bb0\u4e3a final \u3002\u8fd9\u5c06\u4ece\u6839\u672c\u4e0a\u9632\u6b62\u5176\u4ed6\u7c7b\u5bf9\u5b83\u7684\u7ee7\u627f\uff0c\u800c\u4e0d\u4f1a\u5bf9\u7c7b\u672c\u8eab\u65bd\u52a0\u5176\u4ed6\u9650\u5236","title":"\u5e94\u8be5\u5c06\u6240\u6709\u7684\u6790\u6784\u51fd\u6570\u90fd\u8bbe\u7f6e\u4e3a\u865a\u51fd\u6570\u5417\uff1f"},{"location":"18-5-early-binding-and-late-binding/","tags":["early-binding","late-binding"],"text":"Key Takeaway \u5728\u672c\u8282\u8bfe\u548c\u4e0b\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ed4\u7ec6\u7814\u7a76 \u865a\u51fd\u6570 \u7684\u5b9e\u73b0\u65b9\u5f0f\u3002\u5c3d\u7ba1\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4e8e\u4f60\u9ad8\u6548\u5730\u4f7f\u7528\u865a\u51fd\u6570\u6765\u8bf4\u5e76\u4e0d\u662f\u5fc5\u987b\u7684\uff0c\u4f46\u662f\u5b83\u771f\u7684\u5f88\u6709\u610f\u601d\u3002\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u628a\u8fd9\u4e9b\u5185\u5bb9\u5f53\u505a\u9009\u4fee\u8bfe\u3002 \u5f53C++\u7a0b\u5e8f\u88ab\u6267\u884c\u65f6\uff0c\u5b83\u662f\u6309\u987a\u5e8f\u6267\u884c\u7684\uff0c\u4ece main() \u7684\u9876\u90e8\u5f00\u59cb\u3002\u5f53\u9047\u5230\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u6267\u884c\u70b9\u8df3\u8f6c\u5230\u88ab\u8c03\u7528\u51fd\u6570\u7684\u5f00\u59cb\u3002CPU\u662f\u600e\u4e48\u77e5\u9053\u8981\u8fd9\u4e48\u505a\u7684? \u7f16\u8bd1\u7a0b\u5e8f\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06C++\u7a0b\u5e8f\u4e2d\u7684\u6bcf\u4e2a\u8bed\u53e5\u8f6c\u6362\u4e3a\u4e00\u884c\u6216\u591a\u884c\u673a\u5668\u8bed\u8a00\u3002\u673a\u5668\u8bed\u8a00\u7684\u6bcf\u4e00\u884c\u90fd\u6709\u81ea\u5df1\u552f\u4e00\u7684\u987a\u5e8f\u5730\u5740\u3002\u51fd\u6570\u4e5f\u4e0d\u4f8b\u5916\u2014\u2014\u5f53\u9047\u5230\u4e00\u4e2a\u51fd\u6570\u65f6\uff0c\u5b83\u88ab\u8f6c\u6362\u6210\u673a\u5668\u8bed\u8a00\u5e76\u7ed9\u51fa\u4e0b\u4e00\u4e2a\u53ef\u7528\u5730\u5740\u3002\u56e0\u6b64\uff0c\u6bcf\u4e2a\u51fd\u6570\u6700\u7ec8\u90fd\u6709\u4e00\u4e2a\u552f\u4e00\u7684\u5730\u5740\u3002 \u7ed1\u5b9a \u6307\u5b9a\u5c31\u662f\u5c06 \u6807\u8bc6\u7b26 \uff08\u53d8\u91cf\u540d\u6216\u51fd\u6570\u540d\uff09\u8f6c\u6362\u4e3a\u5730\u5740\u7684\u8fc7\u7a0b\u3002\u867d\u7136\u7ed1\u5b9a\u9002\u7528\u4e8e\u53d8\u91cf\u548c\u51fd\u6570\uff0c\u4f46\u662f\u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u91cd\u70b9\u8ba8\u8bba\u51fd\u6570\u7684\u7ed1\u5b9a\u3002 \u65e9\u7ed1\u5b9a Early binding \u7f16\u8bd1\u5668\u9047\u5230\u7684\u5927\u591a\u6570\u51fd\u6570\u8c03\u7528\u90fd\u662f\u51fd\u6570\u76f4\u63a5\u8c03\u7528\u3002\u76f4\u63a5\u8c03\u7528\u662f\u76f4\u63a5\u8c03\u7528\u51fd\u6570\u7684\u8bed\u53e5\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 #include void printValue ( int value ) { std :: cout << value ; } int main () { printValue ( 5 ); // This is a direct function call return 0 ; } \u76f4\u63a5\u51fd\u6570\u8c03\u7528\u53ef\u4ee5\u4f7f\u7528\u79f0\u4e3a\u65e9\u671f\u7ed1\u5b9a\u7684\u8fc7\u7a0b\u6765\u89e3\u51b3\u3002 \u65e9\u671f\u7ed1\u5b9a (\u4e5f\u79f0\u4e3a\u9759\u6001\u7ed1\u5b9a)\u610f\u5473\u7740\u7f16\u8bd1\u5668(\u6216\u94fe\u63a5\u5668)\u80fd\u591f\u76f4\u63a5\u5c06\u6807\u8bc6\u7b26\u540d(\u5982\u51fd\u6570\u540d\u6216\u53d8\u91cf\u540d)\u4e0e\u673a\u5668\u5730\u5740\u5173\u8054\u8d77\u6765 \u3002\u8bb0\u4f4f\uff0c\u6240\u6709\u51fd\u6570\u90fd\u6709\u552f\u4e00\u7684\u5730\u5740\u3002\u56e0\u6b64\uff0c\u5f53\u7f16\u8bd1\u5668(\u6216\u94fe\u63a5\u5668)\u9047\u5230\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u5b83\u4f1a\u7528\u4e00\u4e2a\u673a\u5668\u8bed\u8a00\u6307\u4ee4\u66ff\u6362\u51fd\u6570\u8c03\u7528\uff0c\u8be5\u6307\u4ee4\u544a\u8bc9CPU\u8df3\u8f6c\u5230\u51fd\u6570\u7684\u5730\u5740\u3002 \u8ba9\u6211\u4eec\u6765\u770b\u770b\u4e00\u4e2a\u4f7f\u7528\u65e9\u671f\u7ed1\u5b9a\u7684\u7b80\u5355\u8ba1\u7b97\u5668\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include int add ( int x , int y ) { return x + y ; } int subtract ( int x , int y ) { return x - y ; } int multiply ( int x , int y ) { return x * y ; } int main () { int x {}; std :: cout << \"Enter a number: \" ; std :: cin >> x ; int y {}; std :: cout << \"Enter another number: \" ; std :: cin >> y ; int op {}; do { std :: cout << \"Enter an operation (0=add, 1=subtract, 2=multiply): \" ; std :: cin >> op ; } while ( op < 0 || op > 2 ); int result {}; switch ( op ) { // call the target function directly using early binding case 0 : result = add ( x , y ); break ; case 1 : result = subtract ( x , y ); break ; case 2 : result = multiply ( x , y ); break ; } std :: cout << \"The answer is: \" << result << '\\n' ; return 0 ; } \u56e0\u4e3a add() \u3001 subtract() \u548c multiply() \u90fd\u662f\u76f4\u63a5\u51fd\u6570\u8c03\u7528\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u4f7f\u7528 \u65e9\u671f\u7ed1\u5b9a \u7684\u65b9\u5f0f\u6765\u89e3\u6790\u5b83\u4eec\u7684\u8c03\u7528\u3002\u7f16\u8bd1\u5668\u4f1a\u5c06 add() \u51fd\u6570\u7684\u8c03\u7528\u66ff\u6362\u4e3a\u4e00\u4e2a\u6307\u4ee4\uff0c\u544a\u8bc9CPU\u5982\u4f55\u8df3\u8f6c\u5230 add() \u51fd\u6570\u7684\u5730\u5740\u3002\u5bf9\u4e8e subtract() \u548c multiply() \u4e5f\u662f\u7c7b\u4f3c\u7684\u9053\u7406\u3002 \u5ef6\u8fdf\u7ed1\u5b9a Late Binding \u5728\u67d0\u4e9b\u7a0b\u5e8f\u4e2d\uff0c\u5728 \u8fd0\u884c\u65f6 (\u7a0b\u5e8f\u8fd0\u884c\u65f6)\u4e4b\u524d\u4e0d\u53ef\u80fd\u77e5\u9053\u5c06\u8c03\u7528\u54ea\u4e2a\u51fd\u6570\u3002\u8fd9\u88ab\u79f0\u4e3a \u540e\u671f\u7ed1\u5b9a (\u6216\u52a8\u6001\u7ed1\u5b9a)\u3002\u5728C++\u4e2d\uff0c\u83b7\u5f97\u540e\u671f\u7ed1\u5b9a\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 \u51fd\u6570\u6307\u9488 \u3002\u7b80\u5355\u56de\u987e\u4e00\u4e0b\u51fd\u6570\u6307\u9488\uff0c\u51fd\u6570\u6307\u9488\u662f\u4e00\u79cd\u6307\u5411\u51fd\u6570\u800c\u4e0d\u662f\u53d8\u91cf\u7684\u6307\u9488\u3002\u51fd\u6570\u6307\u9488\u6240\u6307\u5411\u7684\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u5728\u6307\u9488\u4e0a\u4f7f\u7528\u51fd\u6570\u8c03\u7528\u64cd\u4f5c\u7b26( () )\u6765\u8c03\u7528\u3002 \u4f8b\u5982\uff0c\u4e0b\u9762\u4ee3\u7801\u901a\u8fc7\u51fd\u6570\u6307\u9488\u6765\u8c03\u7528 add() \u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int add ( int x , int y ) { return x + y ; } int main () { // Create a function pointer and make it point to the add function int ( * pFcn )( int , int ) { add }; std :: cout << pFcn ( 5 , 3 ) << '\\n' ; // add 5 + 3 return 0 ; } \u901a\u8fc7\u51fd\u6570\u6307\u9488\u8c03\u7528\u51fd\u6570\u4e5f\u79f0\u4e3a\u95f4\u63a5\u51fd\u6570\u8c03\u7528\u3002\u4e0b\u9762\u7684\u8ba1\u7b97\u5668\u7a0b\u5e8f\u5728\u51fd\u6570\u4e0a\u4e0e\u4e0a\u9762\u7684\u8ba1\u7b97\u5668\u793a\u4f8b\u76f8\u540c\uff0c\u53ea\u4e0d\u8fc7\u5b83\u4f7f\u7528\u51fd\u6570\u6307\u9488\u800c\u4e0d\u662f\u76f4\u63a5\u7684\u51fd\u6570\u8c03\u7528: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 #include int add ( int x , int y ) { return x + y ; } int subtract ( int x , int y ) { return x - y ; } int multiply ( int x , int y ) { return x * y ; } int main () { int x {}; std :: cout << \"Enter a number: \" ; std :: cin >> x ; int y {}; std :: cout << \"Enter another number: \" ; std :: cin >> y ; int op {}; do { std :: cout << \"Enter an operation (0=add, 1=subtract, 2=multiply): \" ; std :: cin >> op ; } while ( op < 0 || op > 2 ); // Create a function pointer named pFcn (yes, the syntax is ugly) int ( * pFcn )( int , int ) { nullptr }; // Set pFcn to point to the function the user chose switch ( op ) { case 0 : pFcn = add ; break ; case 1 : pFcn = subtract ; break ; case 2 : pFcn = multiply ; break ; } // Call the function that pFcn is pointing to with x and y as parameters // This uses late binding std :: cout << \"The answer is: \" << pFcn ( x , y ) << '\\n' ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6ca1\u6709\u76f4\u63a5\u8c03\u7528 add() \u3001 subtract() \u6216 multiply() \uff0c\u800c\u662f\u8ba9\u51fd\u6570\u6307\u9488\u6307\u5411\u9700\u8981\u8c03\u7528\u7684\u51fd\u6570\u3002\u7136\u540e\u518d\u4f7f\u7528\u8be5\u51fd\u6570\u6307\u9488\u8c03\u7528\u51fd\u6570\u3002\u5f88\u663e\u7136\uff0c\u7f16\u8bd1\u5668\u6ca1\u529e\u6cd5\u901a\u8fc7\u65e9\u671f\u7ed1\u5b9a\u6765\u89e3\u6790\u51fd\u6570\u8c03\u7528 pFcn(x, y) \u56e0\u4e3a\u5728\u7f16\u8bd1\u65f6\u5b83\u5e76\u4e0d\u77e5\u9053 pFcn \u4f1a\u6307\u5411\u54ea\u4e2a\u51fd\u6570\u3002 \u540e\u671f\u7ed1\u5b9a\u7684\u6548\u7387\u7565\u4f4e\uff0c\u56e0\u4e3a\u5b83\u6d89\u53ca\u989d\u5916\u7684\u95f4\u63a5\u903b\u8f91\u5c42\u3002\u4f7f\u7528\u65e9\u671f\u7ed1\u5b9a\uff0cCPU\u53ef\u4ee5\u76f4\u63a5\u8df3\u8f6c\u5230\u51fd\u6570\u7684\u5730\u5740\u3002\u4f7f\u7528\u540e\u671f\u7ed1\u5b9a\uff0c\u7a0b\u5e8f\u5fc5\u987b\u8bfb\u53d6\u6307\u9488\u4e2d\u4fdd\u5b58\u7684\u5730\u5740\uff0c\u7136\u540e\u8df3\u8f6c\u5230\u8be5\u5730\u5740\u3002\u8fd9\u9700\u8981\u4e00\u4e2a\u989d\u5916\u7684\u6b65\u9aa4\uff0c\u4f7f\u5b83\u7a0d\u5fae\u6162\u4e00\u4e9b\u3002\u4f46\u662f\uff0c\u540e\u671f\u7ed1\u5b9a\u7684\u4f18\u70b9\u662f\u5b83\u6bd4\u65e9\u671f\u7ed1\u5b9a\u66f4\u7075\u6d3b\uff0c\u56e0\u4e3a\u76f4\u5230\u8fd0\u884c\u65f6\u624d\u9700\u8981\u51b3\u5b9a\u8c03\u7528\u4ec0\u4e48\u51fd\u6570\u3002 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u540e\u671f\u7ed1\u5b9a\u6765\u5b9e\u73b0\u865a\u51fd\u6570\u3002","title":"18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a"},{"location":"18-5-early-binding-and-late-binding/#early-binding","text":"\u7f16\u8bd1\u5668\u9047\u5230\u7684\u5927\u591a\u6570\u51fd\u6570\u8c03\u7528\u90fd\u662f\u51fd\u6570\u76f4\u63a5\u8c03\u7528\u3002\u76f4\u63a5\u8c03\u7528\u662f\u76f4\u63a5\u8c03\u7528\u51fd\u6570\u7684\u8bed\u53e5\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 #include void printValue ( int value ) { std :: cout << value ; } int main () { printValue ( 5 ); // This is a direct function call return 0 ; } \u76f4\u63a5\u51fd\u6570\u8c03\u7528\u53ef\u4ee5\u4f7f\u7528\u79f0\u4e3a\u65e9\u671f\u7ed1\u5b9a\u7684\u8fc7\u7a0b\u6765\u89e3\u51b3\u3002 \u65e9\u671f\u7ed1\u5b9a (\u4e5f\u79f0\u4e3a\u9759\u6001\u7ed1\u5b9a)\u610f\u5473\u7740\u7f16\u8bd1\u5668(\u6216\u94fe\u63a5\u5668)\u80fd\u591f\u76f4\u63a5\u5c06\u6807\u8bc6\u7b26\u540d(\u5982\u51fd\u6570\u540d\u6216\u53d8\u91cf\u540d)\u4e0e\u673a\u5668\u5730\u5740\u5173\u8054\u8d77\u6765 \u3002\u8bb0\u4f4f\uff0c\u6240\u6709\u51fd\u6570\u90fd\u6709\u552f\u4e00\u7684\u5730\u5740\u3002\u56e0\u6b64\uff0c\u5f53\u7f16\u8bd1\u5668(\u6216\u94fe\u63a5\u5668)\u9047\u5230\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u5b83\u4f1a\u7528\u4e00\u4e2a\u673a\u5668\u8bed\u8a00\u6307\u4ee4\u66ff\u6362\u51fd\u6570\u8c03\u7528\uff0c\u8be5\u6307\u4ee4\u544a\u8bc9CPU\u8df3\u8f6c\u5230\u51fd\u6570\u7684\u5730\u5740\u3002 \u8ba9\u6211\u4eec\u6765\u770b\u770b\u4e00\u4e2a\u4f7f\u7528\u65e9\u671f\u7ed1\u5b9a\u7684\u7b80\u5355\u8ba1\u7b97\u5668\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include int add ( int x , int y ) { return x + y ; } int subtract ( int x , int y ) { return x - y ; } int multiply ( int x , int y ) { return x * y ; } int main () { int x {}; std :: cout << \"Enter a number: \" ; std :: cin >> x ; int y {}; std :: cout << \"Enter another number: \" ; std :: cin >> y ; int op {}; do { std :: cout << \"Enter an operation (0=add, 1=subtract, 2=multiply): \" ; std :: cin >> op ; } while ( op < 0 || op > 2 ); int result {}; switch ( op ) { // call the target function directly using early binding case 0 : result = add ( x , y ); break ; case 1 : result = subtract ( x , y ); break ; case 2 : result = multiply ( x , y ); break ; } std :: cout << \"The answer is: \" << result << '\\n' ; return 0 ; } \u56e0\u4e3a add() \u3001 subtract() \u548c multiply() \u90fd\u662f\u76f4\u63a5\u51fd\u6570\u8c03\u7528\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u4f7f\u7528 \u65e9\u671f\u7ed1\u5b9a \u7684\u65b9\u5f0f\u6765\u89e3\u6790\u5b83\u4eec\u7684\u8c03\u7528\u3002\u7f16\u8bd1\u5668\u4f1a\u5c06 add() \u51fd\u6570\u7684\u8c03\u7528\u66ff\u6362\u4e3a\u4e00\u4e2a\u6307\u4ee4\uff0c\u544a\u8bc9CPU\u5982\u4f55\u8df3\u8f6c\u5230 add() \u51fd\u6570\u7684\u5730\u5740\u3002\u5bf9\u4e8e subtract() \u548c multiply() \u4e5f\u662f\u7c7b\u4f3c\u7684\u9053\u7406\u3002","title":"\u65e9\u7ed1\u5b9a Early binding"},{"location":"18-5-early-binding-and-late-binding/#late-binding","text":"\u5728\u67d0\u4e9b\u7a0b\u5e8f\u4e2d\uff0c\u5728 \u8fd0\u884c\u65f6 (\u7a0b\u5e8f\u8fd0\u884c\u65f6)\u4e4b\u524d\u4e0d\u53ef\u80fd\u77e5\u9053\u5c06\u8c03\u7528\u54ea\u4e2a\u51fd\u6570\u3002\u8fd9\u88ab\u79f0\u4e3a \u540e\u671f\u7ed1\u5b9a (\u6216\u52a8\u6001\u7ed1\u5b9a)\u3002\u5728C++\u4e2d\uff0c\u83b7\u5f97\u540e\u671f\u7ed1\u5b9a\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 \u51fd\u6570\u6307\u9488 \u3002\u7b80\u5355\u56de\u987e\u4e00\u4e0b\u51fd\u6570\u6307\u9488\uff0c\u51fd\u6570\u6307\u9488\u662f\u4e00\u79cd\u6307\u5411\u51fd\u6570\u800c\u4e0d\u662f\u53d8\u91cf\u7684\u6307\u9488\u3002\u51fd\u6570\u6307\u9488\u6240\u6307\u5411\u7684\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u5728\u6307\u9488\u4e0a\u4f7f\u7528\u51fd\u6570\u8c03\u7528\u64cd\u4f5c\u7b26( () )\u6765\u8c03\u7528\u3002 \u4f8b\u5982\uff0c\u4e0b\u9762\u4ee3\u7801\u901a\u8fc7\u51fd\u6570\u6307\u9488\u6765\u8c03\u7528 add() \u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int add ( int x , int y ) { return x + y ; } int main () { // Create a function pointer and make it point to the add function int ( * pFcn )( int , int ) { add }; std :: cout << pFcn ( 5 , 3 ) << '\\n' ; // add 5 + 3 return 0 ; } \u901a\u8fc7\u51fd\u6570\u6307\u9488\u8c03\u7528\u51fd\u6570\u4e5f\u79f0\u4e3a\u95f4\u63a5\u51fd\u6570\u8c03\u7528\u3002\u4e0b\u9762\u7684\u8ba1\u7b97\u5668\u7a0b\u5e8f\u5728\u51fd\u6570\u4e0a\u4e0e\u4e0a\u9762\u7684\u8ba1\u7b97\u5668\u793a\u4f8b\u76f8\u540c\uff0c\u53ea\u4e0d\u8fc7\u5b83\u4f7f\u7528\u51fd\u6570\u6307\u9488\u800c\u4e0d\u662f\u76f4\u63a5\u7684\u51fd\u6570\u8c03\u7528: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 #include int add ( int x , int y ) { return x + y ; } int subtract ( int x , int y ) { return x - y ; } int multiply ( int x , int y ) { return x * y ; } int main () { int x {}; std :: cout << \"Enter a number: \" ; std :: cin >> x ; int y {}; std :: cout << \"Enter another number: \" ; std :: cin >> y ; int op {}; do { std :: cout << \"Enter an operation (0=add, 1=subtract, 2=multiply): \" ; std :: cin >> op ; } while ( op < 0 || op > 2 ); // Create a function pointer named pFcn (yes, the syntax is ugly) int ( * pFcn )( int , int ) { nullptr }; // Set pFcn to point to the function the user chose switch ( op ) { case 0 : pFcn = add ; break ; case 1 : pFcn = subtract ; break ; case 2 : pFcn = multiply ; break ; } // Call the function that pFcn is pointing to with x and y as parameters // This uses late binding std :: cout << \"The answer is: \" << pFcn ( x , y ) << '\\n' ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6ca1\u6709\u76f4\u63a5\u8c03\u7528 add() \u3001 subtract() \u6216 multiply() \uff0c\u800c\u662f\u8ba9\u51fd\u6570\u6307\u9488\u6307\u5411\u9700\u8981\u8c03\u7528\u7684\u51fd\u6570\u3002\u7136\u540e\u518d\u4f7f\u7528\u8be5\u51fd\u6570\u6307\u9488\u8c03\u7528\u51fd\u6570\u3002\u5f88\u663e\u7136\uff0c\u7f16\u8bd1\u5668\u6ca1\u529e\u6cd5\u901a\u8fc7\u65e9\u671f\u7ed1\u5b9a\u6765\u89e3\u6790\u51fd\u6570\u8c03\u7528 pFcn(x, y) \u56e0\u4e3a\u5728\u7f16\u8bd1\u65f6\u5b83\u5e76\u4e0d\u77e5\u9053 pFcn \u4f1a\u6307\u5411\u54ea\u4e2a\u51fd\u6570\u3002 \u540e\u671f\u7ed1\u5b9a\u7684\u6548\u7387\u7565\u4f4e\uff0c\u56e0\u4e3a\u5b83\u6d89\u53ca\u989d\u5916\u7684\u95f4\u63a5\u903b\u8f91\u5c42\u3002\u4f7f\u7528\u65e9\u671f\u7ed1\u5b9a\uff0cCPU\u53ef\u4ee5\u76f4\u63a5\u8df3\u8f6c\u5230\u51fd\u6570\u7684\u5730\u5740\u3002\u4f7f\u7528\u540e\u671f\u7ed1\u5b9a\uff0c\u7a0b\u5e8f\u5fc5\u987b\u8bfb\u53d6\u6307\u9488\u4e2d\u4fdd\u5b58\u7684\u5730\u5740\uff0c\u7136\u540e\u8df3\u8f6c\u5230\u8be5\u5730\u5740\u3002\u8fd9\u9700\u8981\u4e00\u4e2a\u989d\u5916\u7684\u6b65\u9aa4\uff0c\u4f7f\u5b83\u7a0d\u5fae\u6162\u4e00\u4e9b\u3002\u4f46\u662f\uff0c\u540e\u671f\u7ed1\u5b9a\u7684\u4f18\u70b9\u662f\u5b83\u6bd4\u65e9\u671f\u7ed1\u5b9a\u66f4\u7075\u6d3b\uff0c\u56e0\u4e3a\u76f4\u5230\u8fd0\u884c\u65f6\u624d\u9700\u8981\u51b3\u5b9a\u8c03\u7528\u4ec0\u4e48\u51fd\u6570\u3002 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u540e\u671f\u7ed1\u5b9a\u6765\u5b9e\u73b0\u865a\u51fd\u6570\u3002","title":"\u5ef6\u8fdf\u7ed1\u5b9a Late Binding"},{"location":"18-6-the-virtual-table/","tags":["virtual-table"],"text":"Key Takeaway \u4e3a\u4e86\u5b9e\u73b0\u865a\u51fd\u6570\uff0cC++ \u4f7f\u7528\u4e86\u4e00\u79cd\u79f0\u4e3a \u865a\u8868 \u7684\u7279\u6b8a\u5f62\u5f0f\u7684 \u540e\u671f\u7ed1\u5b9a \u3002\u865a\u8868\u662f\u4e00\u4e2a\u51fd\u6570\u67e5\u627e\u8868\uff0c\u7528\u4e8e\u4ee5\u52a8\u6001/\u540e\u671f\u7ed1\u5b9a\u65b9\u5f0f\u89e3\u6790\u51fd\u6570\u8c03\u7528\u3002\u865a\u8868\u6709\u65f6\u4e5f\u79f0\u4e3a \u201cvtable\u201d\u3001\u201c\u865a\u51fd\u6570\u8868\u201d\u3001\u201c\u865a\u65b9\u6cd5\u8868\u201d\u6216\u201c\u8c03\u5ea6\u8868\u201d\u3002 \u56e0\u4e3a\u4f7f\u7528\u865a\u51fd\u6570\u5e76\u4e0d\u9700\u8981\u4e86\u89e3\u865a\u8868\u7684\u5de5\u4f5c\u65b9\u5f0f\uff0c\u6240\u4ee5\u53ef\u4ee5\u5c06\u672c\u8282\u89c6\u4e3a\u9009\u4fee \u865a\u8868\u5b9e\u9645\u4e0a\u975e\u5e38\u7b80\u5355\uff0c\u4e0d\u8fc7\u7528\u8bed\u8a00\u63cf\u8ff0\u6709\u70b9\u590d\u6742\u3002\u9996\u5148\uff0c\u6bcf\u4e2a\u4f7f\u7528\u865a\u51fd\u6570\u7684\u7c7b(\u6216\u4ece\u4f7f\u7528\u865a\u51fd\u6570\u7684\u7c7b\u6d3e\u751f\u800c\u6765\u7684\u7c7b)\u90fd\u6709\u81ea\u5df1\u7684\u865a\u8868\u3002\u8fd9\u4e2a\u8868\u53ea\u662f\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u65f6\u8bbe\u7f6e\u7684\u4e00\u4e2a\u9759\u6001\u6570\u7ec4\u3002\u865a\u8868\u4e3a\u7c7b\u7684\u5bf9\u8c61\u8c03\u7528\u7684\u6bcf\u4e2a\u865a\u51fd\u6570\u90fd\u5305\u542b\u4e00\u4e2a\u6761\u76ee\u3002\u8be5\u8868\u4e2d\u7684\u6bcf\u4e2a\u6761\u76ee\u90fd\u53ea\u662f\u4e00\u4e2a\u51fd\u6570\u6307\u9488\uff0c\u6307\u5411\u8be5\u7c7b\u53ef\u8bbf\u95ee\u7684\u6700\u540e\u6d3e\u751f\uff08most-derived\uff09\u7684\u51fd\u6570\u3002 \u5176\u6b21\uff0c\u7f16\u8bd1\u5668\u8fd8\u4f1a\u6dfb\u52a0\u4e00\u4e2a\u9690\u85cf\u7684\u6307\u9488\u4f5c\u4e3a\u57fa\u7c7b\u6210\u5458\uff0c\u79f0\u4e3a *__vptr \u3002 *__vptr \u4f1a\u5728\u7c7b\u5bf9\u8c61\u521b\u5efa\u65f6\u88ab\u81ea\u52a8\u8bbe\u7f6e\uff0c\u4f7f\u5176\u6307\u5411\u8be5\u7c7b\u7684\u865a\u8868\u3002\u548c *this \u6307\u9488\u4e0d\u540c\u7684\u662f\uff0c *this \u6307\u9488\u662f\u4e00\u4e2a\u51fd\u6570\u53c2\u6570\uff0c\u7528\u4e8e\u7f16\u8bd1\u5668\u89e3\u6790\u81ea\u5f15\u7528\u3002 \u800c *__vptr \u662f\u4e00\u4e2a\u5b9e\u9645\u7684\u6307\u9488\u3002\u56e0\u6b64\uff0c\u6240\u6709\u5177\u6709\u8be5\u6307\u9488\u7684\u5bf9\u8c61\u9700\u8981\u591a\u5206\u914d\u4e00\u4e2a\u6307\u9488\u5927\u5c0f\u7684\u5185\u5b58\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c *__vptr \u8fd8\u4f1a\u548c\u5176\u4ed6\u6210\u5458\u4e00\u6837\u88ab\u6d3e\u751f\u7ee7\u627f\uff0c\u8fd9\u4e00\u70b9\u975e\u5e38\u91cd\u8981 \u3002 \u4f46\u5c31\u76ee\u524d\u6765\u8bf4\uff0c\u4f60\u53ef\u80fd\u8fd8\u4e0d\u77e5\u9053\u8fd9\u4e9b\u4e1c\u897f\u662f\u5982\u4f55\u7ec4\u5408\u5728\u4e00\u8d77\u4f7f\u7528\u7684\uff0c\u6240\u4ee5\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class Base { public : virtual void function1 () {}; virtual void function2 () {}; }; class D1 : public Base { public : void function1 () override {}; }; class D2 : public Base { public : void function2 () override {}; }; \u4f8b\u5b50\u4e2d\u6709\u4e09\u4e2a\u7c7b\uff0c\u7f16\u8bd1\u5668\u4f1a\u521b\u5efa\u4e09\u4e2a \u865a\u8868 \uff1a Base \u3001 D1 \u3001 D2 \u5404\u4e00\u4e2a\u3002 \u7f16\u8bd1\u5668\u8fd8\u5411\u4f7f\u7528\u865a\u51fd\u6570\u7684 \u6700\u57fa\u7c7b \u6dfb\u52a0\u4e00\u4e2a\u9690\u85cf\u6307\u9488\u6210\u5458\u3002\u5c3d\u7ba1\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u4f46\u6211\u4eec\u5c06\u628a\u5b83\u653e\u5728\u4e0b\u4e00\u4e2a\u793a\u4f8b\u4e2d\uff0c\u53ea\u662f\u4e3a\u4e86\u663e\u793a\u6dfb\u52a0\u5b83\u7684\u4f4d\u7f6e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Base { public : VirtualTable * __vptr ; //\u81ea\u52a8\u6dfb\u52a0\u7684 virtual void function1 () {}; virtual void function2 () {}; }; class D1 : public Base { public : void function1 () override {}; }; class D2 : public Base { public : void function2 () override {}; }; \u5f53\u4e00\u4e2a\u7c7b\u5bf9\u8c61\u88ab\u521b\u5efa\u65f6\uff0c *__vptr \u88ab\u8bbe\u7f6e\u4e3a\u6307\u5411\u8be5\u7c7b\u7684\u865a\u8868 \u3002\u4f8b\u5982\uff0c\u5f53 Base \u7c7b\u578b\u7684\u5bf9\u8c61\u521b\u5efa\u65f6\uff0c *__vptr \u6307\u5411 Base \u7684\u865a\u8868\u3001\u5f53 D1 \u6216 D2 \u6784\u5efa\u65f6\uff0c *__vptr \u5219\u5206\u522b\u6307\u5411 D1 \u6216 D2 \u7684\u865a\u8868\u3002 \u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u8ba8\u8bba\u4e00\u4e0b\u5982\u4f55\u586b\u5145\u8fd9\u4e9b\u865a\u62df\u8868\u3002\u56e0\u4e3a\u8fd9\u91cc\u53ea\u6709\u4e24\u4e2a\u865a\u51fd\u6570\uff0c \u6bcf\u4e2a\u865a\u8868 \u5c06\u6709\u4e24\u4e2a\u6761\u76ee(\u4e00\u4e2a\u7528\u4e8e function1() \uff0c\u4e00\u4e2a\u7528\u4e8e function2() ))\u3002\u8bf7\u8bb0\u4f4f\uff0c\u5728\u586b\u5199\u8fd9\u4e9b\u865a\u8868\u65f6\uff0c\u6bcf\u4e2a\u6761\u76ee\u90fd\u7528\u8be5\u7c7b\u7c7b\u578b\u7684\u5bf9\u8c61\u53ef\u4ee5\u8c03\u7528\u7684\u6700\u540e\u6d3e\u751f\u7684\u51fd\u6570\u586b\u5199\u3002 Base \u5bf9\u8c61\u7684\u865a\u8868\u5f88\u7b80\u5355\uff0c\u4efb\u4f55 Base \u5bf9\u8c61\u90fd\u53ea\u80fd\u8bbf\u95ee Base \u7684\u6210\u5458\uff0c\u800c\u4e0d\u80fd\u8bbf\u95ee D1 \u6216\u8005 D2 \u3002\u8fd9\u6837\u4e00\u6765\uff0c\u865a\u8868\u4e2d\u6761\u76ee function1 \u6307\u5411 Base::function1() \u5e76\u4e14 function2 \u6307\u5411 Base::function2() . D1 \u7684\u865a\u8868\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\u3002D1 \u7c7b\u578b\u7684\u5bf9\u8c61\u53ef\u4ee5\u8bbf\u95ee D1 \u548c Base \u7684\u6210\u5458\uff0cD1 \u91cd\u5199 \u4e86 function1() \uff0c\u4f7f\u5f97 D1::function1() \u76f8\u8f83\u4e8e Base::function1() \u662f\u66f4\u665a\u88ab\u6d3e\u751f\u7684\u51fd\u6570\u3002\u56e0\u6b64\uff0c function1 \u6307\u5411 D1::function1() \u3002\u56e0\u4e3a D1 \u6ca1\u6709\u91cd\u5199 function2() \uff0c\u6240\u4ee5\u6761\u76ee function2 \u6307\u5411 Base::function2() \u3002 D2 \u7684\u865a\u8868\u7c7b\u4f3c\u4e8e D1 \u7684\u865a\u8868\u3002\u53ea\u4e0d\u8fc7 function1 \u6307\u5411 Base::function1() \u800c function2 \u6307\u5411 D2::function2() \u3002 Here\u2019s a picture of this graphically: \u5c3d\u7ba1\u770b\u4e0a\u53bb\u6709\u4e9b\u590d\u6742\uff0c\u6bcf\u4e2a\u7c7b\u7684 *__vptr \u90fd\u6307\u5411\u8be5\u7c7b\u7684 \u865a\u8868 \uff0c\u865a\u8868\u7684\u6761\u76ee\u6307\u5411\u8be5\u5bf9\u8c61\u80fd\u591f\u8c03\u7528\u7684\u6700\u540e\u88ab\u6d3e\u751f\u7684\u51fd\u6570\u3002 \u8003\u8651\u4e00\u4e0b\u5f53\u6211\u4eec\u521b\u5efa\u4e00\u4e2aD1\u7c7b\u578b\u7684\u5bf9\u8c61\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48: 1 2 3 4 int main () { D1 d1 ; } \u56e0\u4e3a d1 \u662f\u4e00\u4e2a D1 \u5bf9\u8c61\uff0c d1 \u7684 *__vptr \u6307\u5411 D1 \u7684\u865a\u8868\u3002 \u73b0\u5728\uff0c\u521b\u5efa\u4e00\u4e2a\u6307\u5411D1\u7684\u57fa\u7c7b\u6307\u9488\uff1a 1 2 3 4 5 6 7 int main () { D1 d1 ; Base * dPtr = & d1 ; return 0 ; } \u56e0\u4e3a dPtr \u662f\u4e00\u4e2a\u57fa\u7c7b\u6307\u9488\uff0c\u6240\u4ee5\u5b83\u6307\u5411\u7684\u662f d1 \u4e2d\u7684 Base \u90e8\u5206\u3002\u4e0d\u8fc7\uff0c\u7531\u4e8e __vptr \u4f4d\u4e8e Base \u90e8\u5206\uff0c\u6240\u4ee5 dPtr \u662f\u53ef\u4ee5\u8bbf\u95ee\u8be5\u6307\u9488\u7684\u3002\u6700\u7ec8\uff0c \u4e00\u5b9a\u8981\u6ce8\u610f dPtr->__vptr \u6307\u5411\u7684\u662f D1 \u7684\u865a\u8868\uff01 \u56e0\u6b64\uff0c\u5373\u4f7f dPtr \u662f Base \u7c7b\u578b\u6307\u9488\uff0c\u5b83\u4ecd\u7136\u80fd\u591f\u8bbf\u95ee D1\u2019\u7684\u865a\u8868(\u901a\u8fc7 __vptr )\u3002 \u90a3\u4e48\uff0c\u5f53\u6211\u4eec\u5c1d\u8bd5\u8c03\u7528 dPtr->function1() \u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f 1 2 3 4 5 6 7 8 int main () { D1 d1 ; Base * dPtr = & d1 ; dPtr -> function1 (); return 0 ; } \u9996\u5148\uff0c\u7a0b\u5e8f\u8bc6\u522b\u5230 function1() \u662f\u4e00\u4e2a \u865a\u51fd\u6570 \u3002\u5176\u6b21\uff0c\u7a0b\u5e8f\u4f7f\u7528 dPtr->__vptr \u83b7\u53d6 D1 \u7684\u865a\u51fd\u6570\u8868\u3002\u7136\u540e\uff0c\u5b83\u4f1a\u5728\u865a\u8868\u4e2d\u67e5\u627e function1() \u5bf9\u5e94\u7684\u7248\u672c\u2014\u2014\u5373 D1::function1() \u3002\u56e0\u6b64 dPtr->function1() \u4f1a\u89e3\u6790\u4e3a D1::function1() \uff01 \u4f60\u53ef\u80fd\u4f1a\u6709\u8fd9\u6837\u7684\u7591\u95ee\uff1a\u201c\u5982\u679c dPtr \u6307\u5411\u7684\u662f\u4e00\u4e2a Base \u7c7b\u7684\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f D1 \u7c7b\u578b\u7684\u5bf9\u8c61\u4f1a\u600e\u6837\uff1f\u5b83\u4ecd\u7136\u4f1a\u8c03\u7528 D1::function1() \u5417\uff1f\u7b54\u6848\u662f\u5426\u5b9a\u7684\u3002 1 2 3 4 5 6 7 8 int main () { Base b ; Base * bPtr = & b ; //\u6307\u5411\u4e00\u4e2aBase\u5bf9\u8c61 bPtr -> function1 (); return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c b \u88ab\u521b\u5efa\u540e\uff0c __vptr \u5c31\u88ab\u6307\u5411\u4e86 Base \u7684\u865a\u8868\u800c\u4e0d\u662fD1\u7684\u865a\u8868\u3002\u8fd9\u6837\u4e00\u6765\uff0c bPtr->__vptr \u4e5f\u540c\u6837\u6307\u5411 Base \u7684\u865a\u8868\u3002 Base \u865a\u8868\u7684 function1() \u6761\u76ee\u6307\u5411 Base::function1() \u3002\u56e0\u6b64 bPtr->function1() \u89e3\u6790\u5230 Base::function1() \uff0c\u56e0\u4e3a\u5b83\u662f\u80fd\u770b\u5230\u7684\u6700\u540e\u6d3e\u751f\u7684\u51fd\u6570\u3002 \u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u8868\uff0c\u7f16\u8bd1\u5668\u548c\u7a0b\u5e8f\u80fd\u591f\u786e\u4fdd\u51fd\u6570\u8c03\u7528\u89e3\u6790\u4e3a\u9002\u5f53\u7684\u865a\u51fd\u6570\uff0c\u5373\u4f7f\u4f60\u53ea\u4f7f\u7528\u6307\u5411\u57fa\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\uff01 \u8c03\u7528\u865a\u51fd\u6570\u6bd4\u8c03\u7528\u975e\u865a\u51fd\u6570\u8981\u6162\uff0c\u8fd9\u6709\u51e0\u4e2a\u539f\u56e0\uff1a\u9996\u5148\uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528 *__vptr \u6765\u83b7\u5f97\u9002\u5f53\u7684\u865a\u8868\u3002\u5176\u6b21\uff0c\u6211\u4eec\u5fc5\u987b\u5bf9\u865a\u62df\u8868\u5efa\u7acb\u7d22\u5f15\uff0c\u4ee5\u627e\u5230\u8981\u8c03\u7528\u7684\u51fd\u6570\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5fc5\u987b\u6267\u884c3\u4e2a\u64cd\u4f5c\u624d\u80fd\u627e\u5230\u8981\u8c03\u7528\u7684\u51fd\u6570\uff0c\u800c\u666e\u901a\u7684\u95f4\u63a5\u51fd\u6570\u8c03\u7528\u5219\u9700\u8981\u6267\u884c2\u4e2a\u64cd\u4f5c\uff0c\u76f4\u63a5\u51fd\u6570\u8c03\u7528\u5219\u9700\u8981\u6267\u884c1\u4e2a\u64cd\u4f5c\u3002\u7136\u800c\uff0c\u5728\u73b0\u4ee3\u8ba1\u7b97\u673a\u4e2d\uff0c\u8fd9\u589e\u52a0\u7684\u65f6\u95f4\u901a\u5e38\u662f\u76f8\u5f53\u5fae\u4e0d\u8db3\u9053\u7684\u3002 \u53e6\u5916\u9700\u8981\u63d0\u9192\u7684\u662f\uff0c\u4efb\u4f55\u4f7f\u7528\u865a\u51fd\u6570\u7684\u7c7b\u90fd\u6709 *__vptr \uff0c\u56e0\u6b64\u8be5\u7c7b\u7684\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u5c06\u589e\u52a0\u4e00\u4e2a\u6307\u9488\u3002\u865a\u51fd\u6570\u529f\u80fd\u5f3a\u5927\uff0c\u4f46\u5b83\u4eec\u786e\u5b9e\u6709\u6027\u80fd\u4ee3\u4ef7\u3002","title":"18.6 - \u865a\u51fd\u6570\u8868"},{"location":"18-7-pure-virtual-functions-abstract-base-classes-and-%20interface-classes/","tags":["pure-virtual-functions","abstract-base-classes","interface-classes"],"text":"Key Takeaway \u5305\u542b\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\u662f\u62bd\u8c61\u7c7b\uff0c\u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\uff0c\u5176\u6d3e\u751f\u7c7b\u5982\u679c\u5b9e\u73b0\u4e86\u6240\u6709\u7684\u865a\u51fd\u6570\u5219\u53d8\u6210\u6b63\u5e38\u7c7b \u5373\u4f7f\u7eaf\u865a\u51fd\u6570\u6709\u51fd\u6570\u4f53\uff0c\u4e5f\u662f\u7eaf\u865a\u51fd\u6570\uff0c\u800c\u4e14\u5b83\u7684\u5b9a\u4e49\u9700\u8981\u5355\u72ec\u5b9a\u4e49\uff0c\u800c\u4e0d\u80fd\u662f\u58f0\u660e\u65f6\u5b9a\u4e49 \u6709\u5b9e\u73b0\u7684\u7eaf\u865a\u51fd\u6570\u7528\u4e8e\u63d0\u4f9b\u9ed8\u8ba4\u5b9e\u73b0\uff0c\u6d3e\u751f\u7c7b\u5728\u5b9e\u73b0\u7eaf\u865a\u51fd\u6570\u65f6\u4e2d\u53ef\u4ee5\u663e\u5f0f\u5730\u8c03\u7528\u8be5\u7eaf\u865a\u51fd\u6570\u5b9e\u73b0 \u6790\u6784\u51fd\u6570\u53ef\u4ee5\u662f\u7eaf\u865a\u7684\uff0c\u4f46\u5fc5\u987b\u7ed9\u51fa\u4e00\u4e2a\u5b9a\u4e49\uff0c\u4ee5\u4fbf\u5728\u6d3e\u751f\u5bf9\u8c61\u6790\u6784\u65f6\u8c03\u7528\u5b83 \u63a5\u53e3\u7c7b\u4e5f\u6709\u865a\u8868\uff0c\u4f46\u662f\u7eaf\u865a\u51fd\u6570\u7684\u865a\u8868\u6761\u76ee\u8981\u4e48\u662f\u7a7a\u6307\u9488\u7684\u8981\u4e48\u662f\u6253\u5370\u62a5\u9519\u51fd\u6570 \u7eaf\u865a\uff08\u62bd\u8c61\uff09\u51fd\u6570\u548c\u7eaf\u865a\u57fa\u7c7b \u5230\u76ee\u524d\u4f4d\u7f6e\uff0c\u6211\u4eec\u770b\u5230\u7684 \u865a\u51fd\u6570 \u90fd\u662f\u7531\u51fd\u6570\u4f53\uff08\u51fd\u6570\u5b9a\u4e49\uff09\u7684\u3002\u4f46\u662f\uff0cC++ \u5141\u8bb8\u6211\u4eec\u521b\u5efa\u4e00\u79cd\u7279\u6b8a\u7684\u865a\u51fd\u6570\u2014\u2014 \u7eaf\u865a\u51fd\u6570 \uff08\u62bd\u8c61\u51fd\u6570\uff09\uff0c\u5b83\u6ca1\u6709\u51fd\u6570\u4f53\uff01\u7eaf\u865a\u51fd\u6570\u53ea\u662f\u4e00\u4e2a\u5360\u4f4d\u800c\u5df2\uff0c\u5b83\u5fc5\u987b\u5728 \u6d3e\u751f\u7c7b \u4e2d\u91cd\u5b9a\u4e49\u3002 \u521b\u5efa\u7eaf\u865a\u51fd\u6570\uff0c\u4e0d\u9700\u8981\u5b9a\u4e49\u51fd\u6570\u4f53\uff0c\u53ea\u9700\u8981\u5c06\u51fd\u6570\u8d4b\u503c\u4e3a0\u5373\u53ef\u3002 1 2 3 4 5 6 7 8 9 10 11 class Base { public : const char * sayHi () const { return \"Hi\" ; } // \u666e\u901a\u51fd\u6570 virtual const char * getName () const { return \"Base\" ; } // \u666e\u901a\u865a\u51fd\u6570 virtual int getValue () const = 0 ; // \u7eaf\u865a\u51fd\u6570 int doSomething () = 0 ; // \u7f16\u8bd1\u9519\u8bef: \u4e0d\u80fd\u7ed9\u975e\u865a\u51fd\u6570\u8d4b\u503c\u4e3a0 }; \u5f53\u6211\u4eec\u5411\u7c7b\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570\u65f6\uff0c\u5b9e\u9645\u4e0a\u662f\u5728\u8bf4\uff0c\u201c\u8981\u7531\u6d3e\u751f\u7c7b\u6765\u5b9e\u73b0\u8fd9\u4e2a\u51fd\u6570\u201d\u3002 \u4f7f\u7528\u7eaf\u865a\u51fd\u6570\u6709\u4e24\u4e2a\u4e3b\u8981\u540e\u679c\uff1a\u9996\u5148\uff0c\u4efb\u4f55\u5177\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\u90fd\u53d8\u6210\u4e86 \u62bd\u8c61\u7c7b \uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\uff01\u8003\u8651\u4e00\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a Base \u5b9e\u4f8b\u4f1a\u53d1\u751f\u4ec0\u4e48: 1 2 3 4 5 6 7 int main () { Base base ; // We can't instantiate an abstract base class, but for the sake of example, pretend this was allowed base . getValue (); // what would this do? return 0 ; } \u56e0\u4e3a\u6ca1\u6709 getValue() \u7684\u5b9a\u4e49\uff0c\u90a3\u4e48 base.getValue() \u5e94\u8be5\u5982\u4f55\u89e3\u6790\uff1f \u7b2c\u4e8c\uff0c\u4efb\u4f55\u6d3e\u751f\u7c7b\u90fd\u5fc5\u987b\u4e3a\u8fd9\u4e2a\u51fd\u6570\u5b9a\u4e49\u4e00\u4e2a\u4e3b\u4f53\uff0c\u5426\u5219\u6d3e\u751f\u7c7b\u4e5f\u4f1a\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b\u3002 \u4e00\u4e2a\u7eaf\u865a\u51fd\u6570\u7684\u4f8b\u5b50 \u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570\u7684\u4f8b\u5b50\u3002\u5728\u4e0a\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a\u7b80\u5355\u7684 Animal \u57fa\u7c7b\uff0c\u5e76\u4ece\u4e2d\u6d3e\u751f\u4e86 Cat \u548c Dog \u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include class Animal { protected : std :: string m_name ; // We're making this constructor protected because // we don't want people creating Animal objects directly, // but we still want derived classes to be able to use it. Animal ( const std :: string & name ) : m_name { name } { } public : std :: string getName () const { return m_name ; } virtual const char * speak () const { return \"???\" ; } virtual ~ Animal () = default ; //\u9ed8\u8ba4\u6790\u6784\u51fd\u6570\uff08\u865a\u51fd\u6570\uff09 }; class Cat : public Animal { public : Cat ( const std :: string & name ) : Animal { name } { } const char * speak () const override { return \"Meow\" ; } }; class Dog : public Animal { public : Dog ( const std :: string & name ) : Animal { name } { } const char * speak () const override { return \"Woof\" ; } }; \u6211\u4eec\u5c06\u6784\u9020\u51fd\u6570\u5b9a\u4e49\u4e3a \u53d7\u4fdd\u62a4\u6210\u5458 \u4ee5\u9632\u6b62\u5206\u914d Animal \u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u4f46\u662f\uff0c\u4ecd\u7136\u53ef\u4ee5\u521b\u5efa\u4e0d\u91cd\u65b0\u5b9a\u4e49 speak() \u7684\u6d3e\u751f\u7c7b\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include class Cow : public Animal { public : Cow ( const std :: string & name ) : Animal { name } { } // \u6ca1\u6709\u91cd\u65b0\u5b9a\u4e49 speak }; int main () { Cow cow { \"Betsy\" }; std :: cout << cow . getName () << \" says \" << cow . speak () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 Betsy says ??? \u53d1\u751f\u4e86\u4ec0\u4e48\u4e8b?\u6211\u4eec\u5fd8\u8bb0\u91cd\u65b0\u5b9a\u4e49\u51fd\u6570speak()\uff0c\u6240\u4ee5 cow.Speak() \u89e3\u6790\u4e3a Animal.speak() \uff0c\u8fd9\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u4e00\u4e2a\u66f4\u597d\u7684\u529e\u6cd5\u662f\u4f7f\u7528\u7eaf\u865a\u51fd\u6570\uff1a\u6211\u4eec\u901a\u8fc7\u5c06\u6784\u9020\u51fd\u6570\u5b9a\u4e49\u4e3a \u53d7\u4fdd\u62a4\u6210\u5458 \uff0c\u653e\u7f6e\u4eba\u4e3a\u5730\u5206\u914d Animal \u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u4f46\u662f\uff0c\u4ecd\u7136\u53ef\u4ee5\u521b\u5efa\u4e0d\u91cd\u65b0\u5b9a\u4e49 speak() \u7684\u6d3e\u751f\u7c7b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include class Animal // Animal \u662f\u4e00\u4e2a\u62bd\u8c61\u7c7b { protected : std :: string m_name ; public : Animal ( const std :: string & name ) : m_name { name } { } const std :: string & getName () const { return m_name ; } virtual const char * speak () const = 0 ; // \u7eaf\u865a\u51fd\u6570 virtual ~ Animal () = default ; }; \u8fd9\u91cc\u6709\u51e0\u70b9\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0c speak() \u73b0\u5728\u662f\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570\u3002\u8fd9\u610f\u5473\u7740 Animal \u73b0\u5728\u662f\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b\uff0c\u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u5c06\u6784\u9020\u51fd\u6570\u5b9a\u4e49\u4e3a\u53d7\u4fdd\u62a4\u6210\u5458(\u5c3d\u7ba1\u8fd9\u6837\u505a\u6ca1\u6709\u574f\u5904)\u3002\u7b2c\u4e8c\uff0c\u56e0\u4e3a\u6211\u4eec\u7684 Cow \u7c7b\u662f\u4ece Animal \u6d3e\u751f\u7684\uff0c\u4f46\u662f\u6211\u4eec\u6ca1\u6709\u5b9a\u4e49 Cow::speak() \uff0c\u6240\u4ee5 Cow \u4e5f\u662f\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b\u3002\u73b0\u5728\uff0c\u5f53\u6211\u4eec\u8bd5\u56fe\u7f16\u8bd1\u8fd9\u6bb5\u4ee3\u7801\u65f6: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include class Cow : public Animal { public : Cow ( const std :: string & name ) : Animal { name } { } // We forgot to redefine speak }; int main () { Cow cow { \"Betsy\" }; std :: cout << cow . getName () << \" says \" << cow . speak () << '\\n' ; return 0 ; } \u7f16\u8bd1\u5668\u4f1a\u7ed9\u6211\u4eec\u4e00\u4e2a\u8b66\u544a\uff0c\u56e0\u4e3a Cow \u662f\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b\uff0c\u6211\u4eec\u4e0d\u80fd\u521b\u5efa\u62bd\u8c61\u57fa\u7c7b\u7684\u5b9e\u4f8b(\u884c\u53f7\u662f\u9519\u8bef\u7684\uff0c\u56e0\u4e3a\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u7701\u7565\u4e86 Animal \u7c7b): 1 2 3 4 5 ( 33 ) : error C2259: 'Cow' : cannot instantiate abstract class ( 20 ) : note: see declaration of 'Cow' ( 33 ) : note: due to following members: ( 33 ) : note: 'const char *Animal::speak(void) const' : is abstract ( 15 ) : note: see declaration of 'Animal::speak' \u4e0a\u9762\u4fe1\u606f\u8868\u660e\uff0c\u53ea\u6709\u5f53 Cow \u4e3a speak() \u63d0\u4f9b\u4e86\u5b9a\u4e49\uff0c Cow \u624d\u80fd\u591f\u88ab\u5b9e\u4f8b\u5316\u3002 \u8ba9\u6211\u4eec\u7ee7\u7eed\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include class Animal // This Animal is an abstract base class { protected : std :: string m_name ; public : Animal ( const std :: string & name ) : m_name { name } { } const std :: string & getName () const { return m_name ; } virtual const char * speak () const = 0 ; // note that speak is now a pure virtual function virtual ~ Animal () = default ; }; class Cow : public Animal { public : Cow ( const std :: string & name ) : Animal ( name ) { } const char * speak () const override { return \"Moo\" ; } }; int main () { Cow cow { \"Betsy\" }; std :: cout << cow . getName () << \" says \" << cow . speak () << '\\n' ; return 0 ; } \u7f16\u8bd1\u5e76\u8fd0\u884c\u7a0b\u5e8f\uff1a 1 Betsy says Moo \u5f53\u6211\u4eec\u6709\u4e00\u4e2a\u60f3\u8981\u653e\u5728\u57fa\u7c7b\u4e2d\u7684\u51fd\u6570\uff0c\u4f46\u53ea\u6709\u6d3e\u751f\u7c7b\u77e5\u9053\u5b83\u5e94\u8be5\u8fd4\u56de\u4ec0\u4e48\u65f6\uff0c\u7eaf\u865a\u51fd\u6570\u662f\u6709\u7528\u7684 \u3002\u7eaf\u865a\u51fd\u6570\u4f7f\u5f97\u57fa\u7c7b\u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\uff0c\u6d3e\u751f\u7c7b\u5728\u88ab\u5b9e\u4f8b\u5316\u4e4b\u524d\u5fc5\u987b\u5b9a\u4e49\u8fd9\u4e9b\u51fd\u6570\u3002\u8fd9\u6709\u52a9\u4e8e\u786e\u4fdd\u6d3e\u751f\u7c7b\u4e0d\u4f1a\u5fd8\u8bb0\u91cd\u65b0\u5b9a\u4e49\u57fa\u7c7b\u5e0c\u671b\u5b83\u4eec\u91cd\u65b0\u5b9a\u4e49\u7684\u51fd\u6570\u3002 \u5c31\u50cf\u666e\u901a\u865a\u51fd\u6570\u4e00\u6837\uff0c\u7eaf\u865a\u51fd\u6570\u53ef\u4ee5\u4f7f\u7528\u57fa\u7c7b\u7684\u5f15\u7528(\u6216\u6307\u9488)\u6765\u8c03\u7528: 1 2 3 4 5 6 7 8 9 int main () { Cow cow { \"Betsy\" }; Animal & a { cow }; std :: cout << a . speak (); // resolves to Cow::speak(), prints \"Moo\" return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c a.speak() \u901a\u8fc7\u865a\u51fd\u6570\u89e3\u6790\u89e3\u6790\u4e3a Cow::speak() \u3002 \u7531\u4e8e\u5e26\u6709\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\u5177\u6709\u865a\u51fd\u6570\uff0c\u6240\u4ee5\u4e0d\u8981\u5fd8\u8bb0\u5c06\u6790\u6784\u51fd\u6570\u4e5f\u8bbe\u7f6e\u4e3a\u865a\u51fd\u6570\u3002 \u6709\u51fd\u6570\u4f53\uff08\u5b9a\u4e49\uff09\u7684\u7eaf\u865a\u51fd\u6570 \u4e8b\u5b9e\u8bc1\u660e\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u5177\u6709\u51fd\u6570\u4f53\u7684\u7eaf\u865a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include class Animal // This Animal is an abstract base class { protected : std :: string m_name ; public : Animal ( const std :: string & name ) : m_name { name } { } std :: string getName () { return m_name ; } virtual const char * speak () const = 0 ; // = 0 \u610f\u5473\u7740\u8fd9\u662f\u7eaf\u865a\u51fd\u6570 virtual ~ Animal () = default ; }; const char * Animal :: speak () const // \u5373\u4f7f\u8be5\u51fd\u6570\u6709\u5b9a\u4e49 { return \"buzz\" ; } \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c speak() \u4ecd\u7136\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570\u56e0\u4e3a\u201c= 0\u201d(\u5373\u4f7f\u5df2\u7ecf\u7ed9\u4e86\u5b83\u4e00\u4e2a\u5b9a\u4e49)\uff0c Animal \u4ecd\u7136\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b(\u56e0\u6b64\u4e0d\u80fd\u5b9e\u4f8b\u5316)\u3002\u4efb\u4f55\u7ee7\u627f\u81ea Animal \u7684\u7c7b\u90fd\u9700\u8981\u4e3a speak() \u63d0\u4f9b\u81ea\u5df1\u7684\u5b9a\u4e49\uff0c\u5426\u5219\u4e5f\u4f1a\u88ab\u8ba4\u4e3a\u662f\u62bd\u8c61\u57fa\u7c7b\u3002 \u5728\u4e3a\u7eaf\u865a\u51fd\u6570\u63d0\u4f9b\u5b9a\u4e49\u65f6\uff0c\u5fc5\u987b\u5355\u72ec\u63d0\u4f9b\u5b9a\u4e49(\u4e0d\u80fd\u662f \u5185\u8054\u51fd\u6570 )\u3002 \u5c0f\u8d34\u58eb \u5bf9\u4e8e Visual Studio \u7528\u6237\u7684\u63d0\u793a Visual Studio \u9519\u8bef\u5730\u5141\u8bb8\u7eaf\u865a\u51fd\u6570\u5728\u58f0\u660e\u65f6\u5b9a\u4e49\uff1b 1 2 3 4 5 // wrong! virtual const char * speak () const = 0 { return \"buzz\" ; } \u8fd9\u5176\u5b9e\u662f\u9519\u8bef\u7684\uff0c\u4f46\u662f\u65e0\u6cd5\u5173\u95ed\u3002 \u5f53\u60a8\u5e0c\u671b\u57fa\u7c7b\u4e3a\u51fd\u6570\u63d0\u4f9b\u9ed8\u8ba4\u5b9e\u73b0\uff0c\u4f46\u4ecd\u7136\u5f3a\u5236\u4efb\u4f55\u6d3e\u751f\u7c7b\u63d0\u4f9b\u5b83\u4eec\u81ea\u5df1\u7684\u5b9e\u73b0\u65f6\uff0c\u6b64\u8303\u4f8b\u53ef\u80fd\u5f88\u6709\u7528\u3002\u4f46\u662f\uff0c\u5982\u679c\u6d3e\u751f\u7c7b\u5bf9\u57fa\u7c7b\u63d0\u4f9b\u7684\u9ed8\u8ba4\u5b9e\u73b0\u611f\u5230\u6ee1\u610f\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u57fa\u7c7b\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 #include #include class Animal // Animal \u662f\u62bd\u8c61\u57fa\u7c7b { protected : std :: string m_name ; public : Animal ( const std :: string & name ) : m_name ( name ) { } const std :: string & getName () const { return m_name ; } virtual const char * speak () const = 0 ; // speak \u662f\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570 virtual ~ Animal () = default ; }; const char * Animal::speak () const { return \"buzz\" ; // \u9ed8\u8ba4\u5b9e\u73b0 } class Dragonfly : public Animal { public : Dragonfly ( const std :: string & name ) : Animal { name } { } const char * speak () const override // \u63d0\u4f9b\u7eaf\u865a\u51fd\u6570\u5b9a\u4e49\u540e\u8be5\u7c7b\u5df2\u7ecf\u4e0d\u662f\u62bd\u8c61\u7c7b\u4e86 { return Animal :: speak (); // \u4f7f\u7528 Animal \u7684\u9ed8\u8ba4\u5b9e\u73b0 } }; int main () { Dragonfly dfly { \"Sally\" }; std :: cout << dfly . getName () << \" says \" << dfly . speak () << '\\n' ; return 0 ; } \u8f93\u51fa\uff1a 1 Sally says buzz \u8fd9\u4e2a\u529f\u80fd\u5e76\u4e0d\u5e38\u7528\u3002 \u6790\u6784\u51fd\u6570\u53ef\u4ee5\u662f\u7eaf\u865a\u7684\uff0c\u4f46\u5fc5\u987b\u7ed9\u51fa\u4e00\u4e2a\u5b9a\u4e49\uff0c\u4ee5\u4fbf\u5728\u6d3e\u751f\u5bf9\u8c61\u6790\u6784\u65f6\u8c03\u7528\u5b83\u3002 \u63a5\u53e3\u7c7b \u63a5\u53e3\u7c7b \u6ca1\u6709\u6210\u5458\u53d8\u91cf\uff0c\u800c\u4e14 \u6240\u6709 \u51fd\u6570\u90fd\u662f \u7eaf\u865a\u51fd\u6570 \uff01\u6362\u8a00\u4e4b\uff0c\u8fd9\u4e2a\u7c7b\u5c31\u662f\u4e00\u4e2a\u7eaf\u5b9a\u4e49\uff0c\u5e76\u6ca1\u6709\u5b9e\u9645\u5b9e\u73b0\u3002\u5f53\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u51fa\u6d3e\u751f\u7c7b\u5fc5\u987b\u5b9e\u73b0\u7684\u529f\u80fd\u65f6\uff0c\u5e76\u4e14\u5c06\u8fd9\u4e9b\u529f\u80fd\u90fd\u7559\u7ed9\u6d3e\u751f\u7c7b\u5b9e\u73b0\u65f6\uff0c\u4f7f\u7528\u63a5\u53e3\u7c7b\u662f\u5f88\u6709\u7528\u7684\u3002 \u63a5\u53e3\u7c7b\u547d\u540d\u901a\u5e38\u4ee5 I \u5f00\u5934\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u63a5\u53e3\u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 class IErrorLog { public : virtual bool openLog ( const char * filename ) = 0 ; virtual bool closeLog () = 0 ; virtual bool writeError ( const char * errorMessage ) = 0 ; virtual ~ IErrorLog () {} // make a virtual destructor in case we delete an IErrorLog pointer, so the proper derived destructor is called }; \u4efb\u4f55\u4ece IErrorLog \u7ee7\u627f\u7684\u7c7b\u90fd\u5fc5\u987b\u5b9e\u73b0\u8fd9\u4e09\u4e2a\u51fd\u6570\uff0c\u4ee5\u4fbf\u88ab\u80fd\u591f\u88ab\u5b9e\u4f8b\u5316\u3002\u4f60\u53ef\u4ee5\u6d3e\u751f\u4e00\u4e2a\u540d\u4e3a FileErrorLog \u7684\u7c7b\uff0c\u5176\u4e2d openLog() \u6253\u5f00\u78c1\u76d8\u4e0a\u7684\u6587\u4ef6\uff0c closeLog() \u5173\u95ed\u6587\u4ef6\uff0c writeError() \u5c06\u6d88\u606f\u5199\u5165\u6587\u4ef6\u3002\u7136\u540e\u53ef\u6d3e\u751f\u53e6\u4e00\u4e2a\u540d\u4e3a ScreenErrorLog \u7684\u7c7b\uff0c\u5176\u4e2d openLog() \u548c closeLog() \u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\uff0c\u800c writeError() \u5728\u5c4f\u5e55\u4e0a\u7684\u5f39\u51fa\u6d88\u606f\u6846\u4e2d\u6253\u5370\u6d88\u606f\u3002 \u73b0\u5728\uff0c\u5047\u8bbe\u4f60\u9700\u8981\u7f16\u5199\u4e00\u4e9b\u4ee3\u7801\u5e76\u9700\u8981\u4f7f\u7528\u9519\u8bef\u65e5\u5fd7\u529f\u80fd\u3002\u5982\u679c\u4f60\u7f16\u5199\u7684\u4ee3\u7801\u76f4\u63a5\u5305\u4f7f\u7528 FileErrorLog \u6216 ScreenErrorLog \uff0c\u90a3\u4e48\u4f60\u5c06 \u53ea\u80fd \u4f7f\u7528\u8fd9\u79cd\u6307\u5b9a\u7684\u9519\u8bef\u65e5\u5fd7\u7c7b\u578b(\u5728\u4e0d\u4fee\u6539\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b)\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u51fd\u6570\u8feb\u4f7f mySqrt() \u7684\u8c03\u7528\u8005\u4f7f\u7528 FileErrorLog \uff0c\u4f46\u8c03\u7528\u8005\u53ef\u80fd\u5e76\u4e0d\u60f3\u4f7f\u7528\u8fd9\u79cd\u9519\u8bef\u65e5\u5fd7\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // for sqrt() double mySqrt ( double value , FileErrorLog & log ) //\u6ce8\u610f\u53c2\u6570\u4f7f\u7528\u7684\u662fFileErrorLog\u7c7b\u578b { if ( value < 0.0 ) { log . writeError ( \"Tried to take square root of value less than 0\" ); return 0.0 ; } else { return std :: sqrt ( value ); } } \u66f4\u52a0\u7075\u6d3b\u7684\u529e\u6cd5\u662f\u5c06\u8be5\u51fd\u6570\u5b9e\u73b0\u4e3a\u4f7f\u7528 IErrorLog \u7c7b\u578b\u7684\u53c2\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include // for sqrt() double mySqrt ( double value , IErrorLog & log ) { if ( value < 0.0 ) { log . writeError ( \"Tried to take square root of value less than 0\" ); return 0.0 ; } else { return std :: sqrt ( value ); } } \u8fd9\u6837\u4e00\u6765\uff0c\u8c03\u7528\u8005\u53ef\u4ee5\u4f20\u5165\u7b26\u5408 IErrorLog \u63a5\u53e3\u7684\u4efb\u4f55\u7c7b\u3002\u5982\u679c\u5b83\u4eec\u5e0c\u671b\u5c06\u9519\u8bef\u65e5\u5fd7\u5199\u5230\u6587\u4ef6\uff0c\u5219\u53ef\u4ee5\u4f20\u5165 FileErrorLog \u7684\u5b9e\u4f8b\u3002\u5982\u679c\u60f3\u9519\u8bef\u4fe1\u606f\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\uff0c\u5219\u53ef\u4ee5\u4f20\u5165\u4e00\u4e2a ScreenErrorLog \u7684\u5b9e\u4f8b\u3002\u5982\u679c\u4ed6\u4eec\u60f3\u505a\u4e00\u4e9b\u4f60\u751a\u81f3\u6ca1\u6709\u60f3\u8fc7\u7684\u4e8b\u60c5\uff0c\u6bd4\u5982\u5728\u51fa\u73b0\u9519\u8bef\u65f6\u5411\u67d0\u4eba\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\uff0c\u5219\u53ef\u4ee5\u4ece IErrorLog (\u4f8b\u5982EmailErrorLog)\u6d3e\u751f\u4e00\u4e2a\u65b0\u7c7b\u5e76\u4f7f\u7528\u5b83\u7684\u5b9e\u4f8b\uff01\u901a\u8fc7\u4f7f\u7528IErrorLog\uff0c\u60a8\u7684\u51fd\u6570\u53d8\u5f97\u66f4\u52a0\u72ec\u7acb\u548c\u7075\u6d3b\u3002 \u4e0d\u8981\u5fd8\u8bb0\u4e3a\u63a5\u53e3\u7c7b\u5b9a\u4e49\u865a\u6790\u6784\u51fd\u6570\uff0c\u4ee5\u4fbf\u5728 \u5220\u9664\u6307\u5411\u63a5\u53e3\u7684\u6307\u9488 \u65f6\u8c03\u7528\u9002\u5f53\u7684\u6d3e\u751f\u6790\u6784\u51fd\u6570\u3002 \u63a5\u53e3\u7c7b\u975e\u5e38\u5e38\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u6613\u4e8e\u4f7f\u7528\u3001\u6613\u4e8e\u6269\u5c55\u548c\u6613\u4e8e\u7ef4\u62a4\u3002\u4e8b\u5b9e\u4e0a\uff0c\u4e00\u4e9b\u73b0\u4ee3\u8bed\u8a00\uff0c\u5982Java\u548cC#\uff0c\u5df2\u7ecf\u6dfb\u52a0\u4e86 interface \u5173\u952e\u5b57\uff0c\u5141\u8bb8\u7a0b\u5e8f\u5458\u76f4\u63a5\u5b9a\u4e49\u63a5\u53e3\u7c7b\uff0c\u800c\u4e0d\u5fc5\u663e\u5f0f\u5730\u5c06\u6240\u6709\u6210\u5458\u51fd\u6570\u6807\u8bb0\u4e3a\u62bd\u8c61\u3002\u6b64\u5916\uff0c\u5c3d\u7ba1Java(\u7248\u672c8\u4e4b\u524d\u7684\u7248\u672c)\u548cC#\u4e0d\u5141\u8bb8\u5bf9\u666e\u901a\u7c7b\u4f7f\u7528\u591a\u91cd\u7ee7\u627f\uff0c\u4f46\u5b83\u4eec\u5141\u8bb8\u60a8\u6839\u636e\u9700\u8981\u5bf9\u4efb\u610f\u591a\u7684\u63a5\u53e3\u8fdb\u884c\u591a\u91cd\u7ee7\u627f\u3002\u56e0\u4e3a\u63a5\u53e3\u6ca1\u6709\u6570\u636e\u4e5f\u6ca1\u6709\u51fd\u6570\u4f53\uff0c\u6240\u4ee5\u5b83\u4eec\u907f\u514d\u4e86\u8bb8\u591a\u4f20\u7edf\u7684\u591a\u91cd\u7ee7\u627f\u95ee\u9898\uff0c\u540c\u65f6\u4ecd\u7136\u63d0\u4f9b\u4e86\u5f88\u5927\u7684\u7075\u6d3b\u6027\u3002 \u7eaf\u865a\u51fd\u6570\u548c\u865a\u8868 \u62bd\u8c61\u7c7b\u4ecd\u7136\u6709\u865a\u8868\uff0c\u56e0\u4e3a\u5982\u679c\u60a8\u6709\u6307\u5411\u62bd\u8c61\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u8fd9\u4e9b\u865a\u8868\u4ecd\u7136\u6709\u7528 \u3002\u5e26\u6709\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\u7684\u865a\u8868\u9879\u901a\u5e38\u8981\u4e48\u5305\u542b\u7a7a\u6307\u9488\uff0c\u8981\u4e48\u6307\u5411\u6253\u5370\u9519\u8bef\u7684\u6cdb\u578b\u51fd\u6570(\u6709\u65f6\u8be5\u51fd\u6570\u88ab\u547d\u540d\u4e3a __purecall )\u3002","title":"18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b"},{"location":"18-7-pure-virtual-functions-abstract-base-classes-and-%20interface-classes/#_1","text":"\u5230\u76ee\u524d\u4f4d\u7f6e\uff0c\u6211\u4eec\u770b\u5230\u7684 \u865a\u51fd\u6570 \u90fd\u662f\u7531\u51fd\u6570\u4f53\uff08\u51fd\u6570\u5b9a\u4e49\uff09\u7684\u3002\u4f46\u662f\uff0cC++ \u5141\u8bb8\u6211\u4eec\u521b\u5efa\u4e00\u79cd\u7279\u6b8a\u7684\u865a\u51fd\u6570\u2014\u2014 \u7eaf\u865a\u51fd\u6570 \uff08\u62bd\u8c61\u51fd\u6570\uff09\uff0c\u5b83\u6ca1\u6709\u51fd\u6570\u4f53\uff01\u7eaf\u865a\u51fd\u6570\u53ea\u662f\u4e00\u4e2a\u5360\u4f4d\u800c\u5df2\uff0c\u5b83\u5fc5\u987b\u5728 \u6d3e\u751f\u7c7b \u4e2d\u91cd\u5b9a\u4e49\u3002 \u521b\u5efa\u7eaf\u865a\u51fd\u6570\uff0c\u4e0d\u9700\u8981\u5b9a\u4e49\u51fd\u6570\u4f53\uff0c\u53ea\u9700\u8981\u5c06\u51fd\u6570\u8d4b\u503c\u4e3a0\u5373\u53ef\u3002 1 2 3 4 5 6 7 8 9 10 11 class Base { public : const char * sayHi () const { return \"Hi\" ; } // \u666e\u901a\u51fd\u6570 virtual const char * getName () const { return \"Base\" ; } // \u666e\u901a\u865a\u51fd\u6570 virtual int getValue () const = 0 ; // \u7eaf\u865a\u51fd\u6570 int doSomething () = 0 ; // \u7f16\u8bd1\u9519\u8bef: \u4e0d\u80fd\u7ed9\u975e\u865a\u51fd\u6570\u8d4b\u503c\u4e3a0 }; \u5f53\u6211\u4eec\u5411\u7c7b\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570\u65f6\uff0c\u5b9e\u9645\u4e0a\u662f\u5728\u8bf4\uff0c\u201c\u8981\u7531\u6d3e\u751f\u7c7b\u6765\u5b9e\u73b0\u8fd9\u4e2a\u51fd\u6570\u201d\u3002 \u4f7f\u7528\u7eaf\u865a\u51fd\u6570\u6709\u4e24\u4e2a\u4e3b\u8981\u540e\u679c\uff1a\u9996\u5148\uff0c\u4efb\u4f55\u5177\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\u90fd\u53d8\u6210\u4e86 \u62bd\u8c61\u7c7b \uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\uff01\u8003\u8651\u4e00\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a Base \u5b9e\u4f8b\u4f1a\u53d1\u751f\u4ec0\u4e48: 1 2 3 4 5 6 7 int main () { Base base ; // We can't instantiate an abstract base class, but for the sake of example, pretend this was allowed base . getValue (); // what would this do? return 0 ; } \u56e0\u4e3a\u6ca1\u6709 getValue() \u7684\u5b9a\u4e49\uff0c\u90a3\u4e48 base.getValue() \u5e94\u8be5\u5982\u4f55\u89e3\u6790\uff1f \u7b2c\u4e8c\uff0c\u4efb\u4f55\u6d3e\u751f\u7c7b\u90fd\u5fc5\u987b\u4e3a\u8fd9\u4e2a\u51fd\u6570\u5b9a\u4e49\u4e00\u4e2a\u4e3b\u4f53\uff0c\u5426\u5219\u6d3e\u751f\u7c7b\u4e5f\u4f1a\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b\u3002","title":"\u7eaf\u865a\uff08\u62bd\u8c61\uff09\u51fd\u6570\u548c\u7eaf\u865a\u57fa\u7c7b"},{"location":"18-7-pure-virtual-functions-abstract-base-classes-and-%20interface-classes/#_2","text":"\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570\u7684\u4f8b\u5b50\u3002\u5728\u4e0a\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a\u7b80\u5355\u7684 Animal \u57fa\u7c7b\uff0c\u5e76\u4ece\u4e2d\u6d3e\u751f\u4e86 Cat \u548c Dog \u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include class Animal { protected : std :: string m_name ; // We're making this constructor protected because // we don't want people creating Animal objects directly, // but we still want derived classes to be able to use it. Animal ( const std :: string & name ) : m_name { name } { } public : std :: string getName () const { return m_name ; } virtual const char * speak () const { return \"???\" ; } virtual ~ Animal () = default ; //\u9ed8\u8ba4\u6790\u6784\u51fd\u6570\uff08\u865a\u51fd\u6570\uff09 }; class Cat : public Animal { public : Cat ( const std :: string & name ) : Animal { name } { } const char * speak () const override { return \"Meow\" ; } }; class Dog : public Animal { public : Dog ( const std :: string & name ) : Animal { name } { } const char * speak () const override { return \"Woof\" ; } }; \u6211\u4eec\u5c06\u6784\u9020\u51fd\u6570\u5b9a\u4e49\u4e3a \u53d7\u4fdd\u62a4\u6210\u5458 \u4ee5\u9632\u6b62\u5206\u914d Animal \u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u4f46\u662f\uff0c\u4ecd\u7136\u53ef\u4ee5\u521b\u5efa\u4e0d\u91cd\u65b0\u5b9a\u4e49 speak() \u7684\u6d3e\u751f\u7c7b\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include class Cow : public Animal { public : Cow ( const std :: string & name ) : Animal { name } { } // \u6ca1\u6709\u91cd\u65b0\u5b9a\u4e49 speak }; int main () { Cow cow { \"Betsy\" }; std :: cout << cow . getName () << \" says \" << cow . speak () << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 Betsy says ??? \u53d1\u751f\u4e86\u4ec0\u4e48\u4e8b?\u6211\u4eec\u5fd8\u8bb0\u91cd\u65b0\u5b9a\u4e49\u51fd\u6570speak()\uff0c\u6240\u4ee5 cow.Speak() \u89e3\u6790\u4e3a Animal.speak() \uff0c\u8fd9\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u4e00\u4e2a\u66f4\u597d\u7684\u529e\u6cd5\u662f\u4f7f\u7528\u7eaf\u865a\u51fd\u6570\uff1a\u6211\u4eec\u901a\u8fc7\u5c06\u6784\u9020\u51fd\u6570\u5b9a\u4e49\u4e3a \u53d7\u4fdd\u62a4\u6210\u5458 \uff0c\u653e\u7f6e\u4eba\u4e3a\u5730\u5206\u914d Animal \u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u4f46\u662f\uff0c\u4ecd\u7136\u53ef\u4ee5\u521b\u5efa\u4e0d\u91cd\u65b0\u5b9a\u4e49 speak() \u7684\u6d3e\u751f\u7c7b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include class Animal // Animal \u662f\u4e00\u4e2a\u62bd\u8c61\u7c7b { protected : std :: string m_name ; public : Animal ( const std :: string & name ) : m_name { name } { } const std :: string & getName () const { return m_name ; } virtual const char * speak () const = 0 ; // \u7eaf\u865a\u51fd\u6570 virtual ~ Animal () = default ; }; \u8fd9\u91cc\u6709\u51e0\u70b9\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0c speak() \u73b0\u5728\u662f\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570\u3002\u8fd9\u610f\u5473\u7740 Animal \u73b0\u5728\u662f\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b\uff0c\u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u5c06\u6784\u9020\u51fd\u6570\u5b9a\u4e49\u4e3a\u53d7\u4fdd\u62a4\u6210\u5458(\u5c3d\u7ba1\u8fd9\u6837\u505a\u6ca1\u6709\u574f\u5904)\u3002\u7b2c\u4e8c\uff0c\u56e0\u4e3a\u6211\u4eec\u7684 Cow \u7c7b\u662f\u4ece Animal \u6d3e\u751f\u7684\uff0c\u4f46\u662f\u6211\u4eec\u6ca1\u6709\u5b9a\u4e49 Cow::speak() \uff0c\u6240\u4ee5 Cow \u4e5f\u662f\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b\u3002\u73b0\u5728\uff0c\u5f53\u6211\u4eec\u8bd5\u56fe\u7f16\u8bd1\u8fd9\u6bb5\u4ee3\u7801\u65f6: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include class Cow : public Animal { public : Cow ( const std :: string & name ) : Animal { name } { } // We forgot to redefine speak }; int main () { Cow cow { \"Betsy\" }; std :: cout << cow . getName () << \" says \" << cow . speak () << '\\n' ; return 0 ; } \u7f16\u8bd1\u5668\u4f1a\u7ed9\u6211\u4eec\u4e00\u4e2a\u8b66\u544a\uff0c\u56e0\u4e3a Cow \u662f\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b\uff0c\u6211\u4eec\u4e0d\u80fd\u521b\u5efa\u62bd\u8c61\u57fa\u7c7b\u7684\u5b9e\u4f8b(\u884c\u53f7\u662f\u9519\u8bef\u7684\uff0c\u56e0\u4e3a\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u7701\u7565\u4e86 Animal \u7c7b): 1 2 3 4 5 ( 33 ) : error C2259: 'Cow' : cannot instantiate abstract class ( 20 ) : note: see declaration of 'Cow' ( 33 ) : note: due to following members: ( 33 ) : note: 'const char *Animal::speak(void) const' : is abstract ( 15 ) : note: see declaration of 'Animal::speak' \u4e0a\u9762\u4fe1\u606f\u8868\u660e\uff0c\u53ea\u6709\u5f53 Cow \u4e3a speak() \u63d0\u4f9b\u4e86\u5b9a\u4e49\uff0c Cow \u624d\u80fd\u591f\u88ab\u5b9e\u4f8b\u5316\u3002 \u8ba9\u6211\u4eec\u7ee7\u7eed\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include class Animal // This Animal is an abstract base class { protected : std :: string m_name ; public : Animal ( const std :: string & name ) : m_name { name } { } const std :: string & getName () const { return m_name ; } virtual const char * speak () const = 0 ; // note that speak is now a pure virtual function virtual ~ Animal () = default ; }; class Cow : public Animal { public : Cow ( const std :: string & name ) : Animal ( name ) { } const char * speak () const override { return \"Moo\" ; } }; int main () { Cow cow { \"Betsy\" }; std :: cout << cow . getName () << \" says \" << cow . speak () << '\\n' ; return 0 ; } \u7f16\u8bd1\u5e76\u8fd0\u884c\u7a0b\u5e8f\uff1a 1 Betsy says Moo \u5f53\u6211\u4eec\u6709\u4e00\u4e2a\u60f3\u8981\u653e\u5728\u57fa\u7c7b\u4e2d\u7684\u51fd\u6570\uff0c\u4f46\u53ea\u6709\u6d3e\u751f\u7c7b\u77e5\u9053\u5b83\u5e94\u8be5\u8fd4\u56de\u4ec0\u4e48\u65f6\uff0c\u7eaf\u865a\u51fd\u6570\u662f\u6709\u7528\u7684 \u3002\u7eaf\u865a\u51fd\u6570\u4f7f\u5f97\u57fa\u7c7b\u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\uff0c\u6d3e\u751f\u7c7b\u5728\u88ab\u5b9e\u4f8b\u5316\u4e4b\u524d\u5fc5\u987b\u5b9a\u4e49\u8fd9\u4e9b\u51fd\u6570\u3002\u8fd9\u6709\u52a9\u4e8e\u786e\u4fdd\u6d3e\u751f\u7c7b\u4e0d\u4f1a\u5fd8\u8bb0\u91cd\u65b0\u5b9a\u4e49\u57fa\u7c7b\u5e0c\u671b\u5b83\u4eec\u91cd\u65b0\u5b9a\u4e49\u7684\u51fd\u6570\u3002 \u5c31\u50cf\u666e\u901a\u865a\u51fd\u6570\u4e00\u6837\uff0c\u7eaf\u865a\u51fd\u6570\u53ef\u4ee5\u4f7f\u7528\u57fa\u7c7b\u7684\u5f15\u7528(\u6216\u6307\u9488)\u6765\u8c03\u7528: 1 2 3 4 5 6 7 8 9 int main () { Cow cow { \"Betsy\" }; Animal & a { cow }; std :: cout << a . speak (); // resolves to Cow::speak(), prints \"Moo\" return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c a.speak() \u901a\u8fc7\u865a\u51fd\u6570\u89e3\u6790\u89e3\u6790\u4e3a Cow::speak() \u3002 \u7531\u4e8e\u5e26\u6709\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\u5177\u6709\u865a\u51fd\u6570\uff0c\u6240\u4ee5\u4e0d\u8981\u5fd8\u8bb0\u5c06\u6790\u6784\u51fd\u6570\u4e5f\u8bbe\u7f6e\u4e3a\u865a\u51fd\u6570\u3002","title":"\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570\u7684\u4f8b\u5b50"},{"location":"18-7-pure-virtual-functions-abstract-base-classes-and-%20interface-classes/#_3","text":"\u4e8b\u5b9e\u8bc1\u660e\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u5177\u6709\u51fd\u6570\u4f53\u7684\u7eaf\u865a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include class Animal // This Animal is an abstract base class { protected : std :: string m_name ; public : Animal ( const std :: string & name ) : m_name { name } { } std :: string getName () { return m_name ; } virtual const char * speak () const = 0 ; // = 0 \u610f\u5473\u7740\u8fd9\u662f\u7eaf\u865a\u51fd\u6570 virtual ~ Animal () = default ; }; const char * Animal :: speak () const // \u5373\u4f7f\u8be5\u51fd\u6570\u6709\u5b9a\u4e49 { return \"buzz\" ; } \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c speak() \u4ecd\u7136\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570\u56e0\u4e3a\u201c= 0\u201d(\u5373\u4f7f\u5df2\u7ecf\u7ed9\u4e86\u5b83\u4e00\u4e2a\u5b9a\u4e49)\uff0c Animal \u4ecd\u7136\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b(\u56e0\u6b64\u4e0d\u80fd\u5b9e\u4f8b\u5316)\u3002\u4efb\u4f55\u7ee7\u627f\u81ea Animal \u7684\u7c7b\u90fd\u9700\u8981\u4e3a speak() \u63d0\u4f9b\u81ea\u5df1\u7684\u5b9a\u4e49\uff0c\u5426\u5219\u4e5f\u4f1a\u88ab\u8ba4\u4e3a\u662f\u62bd\u8c61\u57fa\u7c7b\u3002 \u5728\u4e3a\u7eaf\u865a\u51fd\u6570\u63d0\u4f9b\u5b9a\u4e49\u65f6\uff0c\u5fc5\u987b\u5355\u72ec\u63d0\u4f9b\u5b9a\u4e49(\u4e0d\u80fd\u662f \u5185\u8054\u51fd\u6570 )\u3002 \u5c0f\u8d34\u58eb \u5bf9\u4e8e Visual Studio \u7528\u6237\u7684\u63d0\u793a Visual Studio \u9519\u8bef\u5730\u5141\u8bb8\u7eaf\u865a\u51fd\u6570\u5728\u58f0\u660e\u65f6\u5b9a\u4e49\uff1b 1 2 3 4 5 // wrong! virtual const char * speak () const = 0 { return \"buzz\" ; } \u8fd9\u5176\u5b9e\u662f\u9519\u8bef\u7684\uff0c\u4f46\u662f\u65e0\u6cd5\u5173\u95ed\u3002 \u5f53\u60a8\u5e0c\u671b\u57fa\u7c7b\u4e3a\u51fd\u6570\u63d0\u4f9b\u9ed8\u8ba4\u5b9e\u73b0\uff0c\u4f46\u4ecd\u7136\u5f3a\u5236\u4efb\u4f55\u6d3e\u751f\u7c7b\u63d0\u4f9b\u5b83\u4eec\u81ea\u5df1\u7684\u5b9e\u73b0\u65f6\uff0c\u6b64\u8303\u4f8b\u53ef\u80fd\u5f88\u6709\u7528\u3002\u4f46\u662f\uff0c\u5982\u679c\u6d3e\u751f\u7c7b\u5bf9\u57fa\u7c7b\u63d0\u4f9b\u7684\u9ed8\u8ba4\u5b9e\u73b0\u611f\u5230\u6ee1\u610f\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u57fa\u7c7b\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 #include #include class Animal // Animal \u662f\u62bd\u8c61\u57fa\u7c7b { protected : std :: string m_name ; public : Animal ( const std :: string & name ) : m_name ( name ) { } const std :: string & getName () const { return m_name ; } virtual const char * speak () const = 0 ; // speak \u662f\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570 virtual ~ Animal () = default ; }; const char * Animal::speak () const { return \"buzz\" ; // \u9ed8\u8ba4\u5b9e\u73b0 } class Dragonfly : public Animal { public : Dragonfly ( const std :: string & name ) : Animal { name } { } const char * speak () const override // \u63d0\u4f9b\u7eaf\u865a\u51fd\u6570\u5b9a\u4e49\u540e\u8be5\u7c7b\u5df2\u7ecf\u4e0d\u662f\u62bd\u8c61\u7c7b\u4e86 { return Animal :: speak (); // \u4f7f\u7528 Animal \u7684\u9ed8\u8ba4\u5b9e\u73b0 } }; int main () { Dragonfly dfly { \"Sally\" }; std :: cout << dfly . getName () << \" says \" << dfly . speak () << '\\n' ; return 0 ; } \u8f93\u51fa\uff1a 1 Sally says buzz \u8fd9\u4e2a\u529f\u80fd\u5e76\u4e0d\u5e38\u7528\u3002 \u6790\u6784\u51fd\u6570\u53ef\u4ee5\u662f\u7eaf\u865a\u7684\uff0c\u4f46\u5fc5\u987b\u7ed9\u51fa\u4e00\u4e2a\u5b9a\u4e49\uff0c\u4ee5\u4fbf\u5728\u6d3e\u751f\u5bf9\u8c61\u6790\u6784\u65f6\u8c03\u7528\u5b83\u3002","title":"\u6709\u51fd\u6570\u4f53\uff08\u5b9a\u4e49\uff09\u7684\u7eaf\u865a\u51fd\u6570"},{"location":"18-7-pure-virtual-functions-abstract-base-classes-and-%20interface-classes/#_4","text":"\u63a5\u53e3\u7c7b \u6ca1\u6709\u6210\u5458\u53d8\u91cf\uff0c\u800c\u4e14 \u6240\u6709 \u51fd\u6570\u90fd\u662f \u7eaf\u865a\u51fd\u6570 \uff01\u6362\u8a00\u4e4b\uff0c\u8fd9\u4e2a\u7c7b\u5c31\u662f\u4e00\u4e2a\u7eaf\u5b9a\u4e49\uff0c\u5e76\u6ca1\u6709\u5b9e\u9645\u5b9e\u73b0\u3002\u5f53\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u51fa\u6d3e\u751f\u7c7b\u5fc5\u987b\u5b9e\u73b0\u7684\u529f\u80fd\u65f6\uff0c\u5e76\u4e14\u5c06\u8fd9\u4e9b\u529f\u80fd\u90fd\u7559\u7ed9\u6d3e\u751f\u7c7b\u5b9e\u73b0\u65f6\uff0c\u4f7f\u7528\u63a5\u53e3\u7c7b\u662f\u5f88\u6709\u7528\u7684\u3002 \u63a5\u53e3\u7c7b\u547d\u540d\u901a\u5e38\u4ee5 I \u5f00\u5934\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u63a5\u53e3\u7c7b\uff1a 1 2 3 4 5 6 7 8 9 10 class IErrorLog { public : virtual bool openLog ( const char * filename ) = 0 ; virtual bool closeLog () = 0 ; virtual bool writeError ( const char * errorMessage ) = 0 ; virtual ~ IErrorLog () {} // make a virtual destructor in case we delete an IErrorLog pointer, so the proper derived destructor is called }; \u4efb\u4f55\u4ece IErrorLog \u7ee7\u627f\u7684\u7c7b\u90fd\u5fc5\u987b\u5b9e\u73b0\u8fd9\u4e09\u4e2a\u51fd\u6570\uff0c\u4ee5\u4fbf\u88ab\u80fd\u591f\u88ab\u5b9e\u4f8b\u5316\u3002\u4f60\u53ef\u4ee5\u6d3e\u751f\u4e00\u4e2a\u540d\u4e3a FileErrorLog \u7684\u7c7b\uff0c\u5176\u4e2d openLog() \u6253\u5f00\u78c1\u76d8\u4e0a\u7684\u6587\u4ef6\uff0c closeLog() \u5173\u95ed\u6587\u4ef6\uff0c writeError() \u5c06\u6d88\u606f\u5199\u5165\u6587\u4ef6\u3002\u7136\u540e\u53ef\u6d3e\u751f\u53e6\u4e00\u4e2a\u540d\u4e3a ScreenErrorLog \u7684\u7c7b\uff0c\u5176\u4e2d openLog() \u548c closeLog() \u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\uff0c\u800c writeError() \u5728\u5c4f\u5e55\u4e0a\u7684\u5f39\u51fa\u6d88\u606f\u6846\u4e2d\u6253\u5370\u6d88\u606f\u3002 \u73b0\u5728\uff0c\u5047\u8bbe\u4f60\u9700\u8981\u7f16\u5199\u4e00\u4e9b\u4ee3\u7801\u5e76\u9700\u8981\u4f7f\u7528\u9519\u8bef\u65e5\u5fd7\u529f\u80fd\u3002\u5982\u679c\u4f60\u7f16\u5199\u7684\u4ee3\u7801\u76f4\u63a5\u5305\u4f7f\u7528 FileErrorLog \u6216 ScreenErrorLog \uff0c\u90a3\u4e48\u4f60\u5c06 \u53ea\u80fd \u4f7f\u7528\u8fd9\u79cd\u6307\u5b9a\u7684\u9519\u8bef\u65e5\u5fd7\u7c7b\u578b(\u5728\u4e0d\u4fee\u6539\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b)\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u51fd\u6570\u8feb\u4f7f mySqrt() \u7684\u8c03\u7528\u8005\u4f7f\u7528 FileErrorLog \uff0c\u4f46\u8c03\u7528\u8005\u53ef\u80fd\u5e76\u4e0d\u60f3\u4f7f\u7528\u8fd9\u79cd\u9519\u8bef\u65e5\u5fd7\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // for sqrt() double mySqrt ( double value , FileErrorLog & log ) //\u6ce8\u610f\u53c2\u6570\u4f7f\u7528\u7684\u662fFileErrorLog\u7c7b\u578b { if ( value < 0.0 ) { log . writeError ( \"Tried to take square root of value less than 0\" ); return 0.0 ; } else { return std :: sqrt ( value ); } } \u66f4\u52a0\u7075\u6d3b\u7684\u529e\u6cd5\u662f\u5c06\u8be5\u51fd\u6570\u5b9e\u73b0\u4e3a\u4f7f\u7528 IErrorLog \u7c7b\u578b\u7684\u53c2\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include // for sqrt() double mySqrt ( double value , IErrorLog & log ) { if ( value < 0.0 ) { log . writeError ( \"Tried to take square root of value less than 0\" ); return 0.0 ; } else { return std :: sqrt ( value ); } } \u8fd9\u6837\u4e00\u6765\uff0c\u8c03\u7528\u8005\u53ef\u4ee5\u4f20\u5165\u7b26\u5408 IErrorLog \u63a5\u53e3\u7684\u4efb\u4f55\u7c7b\u3002\u5982\u679c\u5b83\u4eec\u5e0c\u671b\u5c06\u9519\u8bef\u65e5\u5fd7\u5199\u5230\u6587\u4ef6\uff0c\u5219\u53ef\u4ee5\u4f20\u5165 FileErrorLog \u7684\u5b9e\u4f8b\u3002\u5982\u679c\u60f3\u9519\u8bef\u4fe1\u606f\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\uff0c\u5219\u53ef\u4ee5\u4f20\u5165\u4e00\u4e2a ScreenErrorLog \u7684\u5b9e\u4f8b\u3002\u5982\u679c\u4ed6\u4eec\u60f3\u505a\u4e00\u4e9b\u4f60\u751a\u81f3\u6ca1\u6709\u60f3\u8fc7\u7684\u4e8b\u60c5\uff0c\u6bd4\u5982\u5728\u51fa\u73b0\u9519\u8bef\u65f6\u5411\u67d0\u4eba\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\uff0c\u5219\u53ef\u4ee5\u4ece IErrorLog (\u4f8b\u5982EmailErrorLog)\u6d3e\u751f\u4e00\u4e2a\u65b0\u7c7b\u5e76\u4f7f\u7528\u5b83\u7684\u5b9e\u4f8b\uff01\u901a\u8fc7\u4f7f\u7528IErrorLog\uff0c\u60a8\u7684\u51fd\u6570\u53d8\u5f97\u66f4\u52a0\u72ec\u7acb\u548c\u7075\u6d3b\u3002 \u4e0d\u8981\u5fd8\u8bb0\u4e3a\u63a5\u53e3\u7c7b\u5b9a\u4e49\u865a\u6790\u6784\u51fd\u6570\uff0c\u4ee5\u4fbf\u5728 \u5220\u9664\u6307\u5411\u63a5\u53e3\u7684\u6307\u9488 \u65f6\u8c03\u7528\u9002\u5f53\u7684\u6d3e\u751f\u6790\u6784\u51fd\u6570\u3002 \u63a5\u53e3\u7c7b\u975e\u5e38\u5e38\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u6613\u4e8e\u4f7f\u7528\u3001\u6613\u4e8e\u6269\u5c55\u548c\u6613\u4e8e\u7ef4\u62a4\u3002\u4e8b\u5b9e\u4e0a\uff0c\u4e00\u4e9b\u73b0\u4ee3\u8bed\u8a00\uff0c\u5982Java\u548cC#\uff0c\u5df2\u7ecf\u6dfb\u52a0\u4e86 interface \u5173\u952e\u5b57\uff0c\u5141\u8bb8\u7a0b\u5e8f\u5458\u76f4\u63a5\u5b9a\u4e49\u63a5\u53e3\u7c7b\uff0c\u800c\u4e0d\u5fc5\u663e\u5f0f\u5730\u5c06\u6240\u6709\u6210\u5458\u51fd\u6570\u6807\u8bb0\u4e3a\u62bd\u8c61\u3002\u6b64\u5916\uff0c\u5c3d\u7ba1Java(\u7248\u672c8\u4e4b\u524d\u7684\u7248\u672c)\u548cC#\u4e0d\u5141\u8bb8\u5bf9\u666e\u901a\u7c7b\u4f7f\u7528\u591a\u91cd\u7ee7\u627f\uff0c\u4f46\u5b83\u4eec\u5141\u8bb8\u60a8\u6839\u636e\u9700\u8981\u5bf9\u4efb\u610f\u591a\u7684\u63a5\u53e3\u8fdb\u884c\u591a\u91cd\u7ee7\u627f\u3002\u56e0\u4e3a\u63a5\u53e3\u6ca1\u6709\u6570\u636e\u4e5f\u6ca1\u6709\u51fd\u6570\u4f53\uff0c\u6240\u4ee5\u5b83\u4eec\u907f\u514d\u4e86\u8bb8\u591a\u4f20\u7edf\u7684\u591a\u91cd\u7ee7\u627f\u95ee\u9898\uff0c\u540c\u65f6\u4ecd\u7136\u63d0\u4f9b\u4e86\u5f88\u5927\u7684\u7075\u6d3b\u6027\u3002","title":"\u63a5\u53e3\u7c7b"},{"location":"18-7-pure-virtual-functions-abstract-base-classes-and-%20interface-classes/#_5","text":"\u62bd\u8c61\u7c7b\u4ecd\u7136\u6709\u865a\u8868\uff0c\u56e0\u4e3a\u5982\u679c\u60a8\u6709\u6307\u5411\u62bd\u8c61\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u8fd9\u4e9b\u865a\u8868\u4ecd\u7136\u6709\u7528 \u3002\u5e26\u6709\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\u7684\u865a\u8868\u9879\u901a\u5e38\u8981\u4e48\u5305\u542b\u7a7a\u6307\u9488\uff0c\u8981\u4e48\u6307\u5411\u6253\u5370\u9519\u8bef\u7684\u6cdb\u578b\u51fd\u6570(\u6709\u65f6\u8be5\u51fd\u6570\u88ab\u547d\u540d\u4e3a __purecall )\u3002","title":"\u7eaf\u865a\u51fd\u6570\u548c\u865a\u8868"},{"location":"18-8-virtual-base-classes/","tags":["virtual-base-classes"],"text":"Key Takeaway \u5728\u4e0a\u4e00\u7ae0 17.9 - \u591a\u91cd\u7ee7\u627f \u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u8c08\u8bba\u4e86 \u83f1\u5f62\u7ee7\u627f\u95ee\u9898 \u3002\u672c\u7ae0\u6211\u4eec\u4f1a\u7ee7\u7eed\u8be5\u8bdd\u9898\u3002 \u6ce8\u610f\uff1a\u672c\u8282\u662f\u9ad8\u7ea7\u4e3b\u9898\uff0c\u53ef\u4ee5\u4f5c\u4e3a\u9009\u4fee\u3002 \u83f1\u5f62\u7ee7\u627f\u95ee\u9898 \u4e0b\u9762\u662f\u6211\u4eec\u4e0a\u4e00\u8bfe\u4e2d\u7684\u4f8b\u5b50(\u591a\u4e86\u4e00\u4e9b\u6784\u9020\u51fd\u6570)\uff0c\u8bf4\u660e\u83f1\u5f62\u7ee7\u627f\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include class PoweredDevice { public : PoweredDevice ( int power ) { std :: cout << \"PoweredDevice: \" << power << '\\n' ; } }; class Scanner : public PoweredDevice { public : Scanner ( int scanner , int power ) : PoweredDevice { power } { std :: cout << \"Scanner: \" << scanner << '\\n' ; } }; class Printer : public PoweredDevice { public : Printer ( int printer , int power ) : PoweredDevice { power } { std :: cout << \"Printer: \" << printer << '\\n' ; } }; class Copier : public Scanner , public Printer { public : Copier ( int scanner , int printer , int power ) : Scanner { scanner , power }, Printer { printer , power } { } }; \u4e5f\u8bb8\u4f60\u4f1a\u8ba4\u4e3a\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u5f97\u5230\u4e0b\u9762\u8fd9\u6837\u7684\u7ee7\u627f\u7ed3\u6784\uff1a \u5982\u679c\u8981\u521b\u5efa\u4e00\u4e2a Copier \u7c7b\u5bf9\u8c61\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u6700\u7ec8\u4f1a\u5f97\u5230 PoweredDevice \u7c7b\u7684\u4e24\u4e2a\u526f\u672c\u2014\u2014\u4e00\u4e2a\u6765\u81ea Printer \uff0c\u4e00\u4e2a\u6765\u81ea Scanner \u3002\u5b83\u6709\u4ee5\u4e0b\u7ed3\u6784: \u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7b80\u77ed\u7684\u793a\u4f8b\u6765\u6f14\u793a\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 int main () { Copier copier { 1 , 2 , 3 }; return 0 ; } 1 2 3 4 PoweredDevice: 3 Scanner: 1 PoweredDevice: 3 Printer: 2 \u770b\u5230\u4e86\u5417\uff1f PoweredDevice \u88ab\u6784\u5efa\u4e86\u4e24\u6b21\u3002 \u867d\u7136\u8fd9\u901a\u5e38\u662f\u671f\u671b\u7684\u7ed3\u679c\uff0c\u4f46\u5728\u6709\u65f6\u4f60\u53ef\u80fd\u53ea\u60f3\u8ba9 Scanner \u548c Printer \u5171\u4eab PoweredDevice \u7684\u4e00\u4e2a\u526f\u672c\u3002 \u865a\u57fa\u7c7b \u8981\u5171\u4eab\u57fa\u7c7b\uff0c\u53ea\u9700\u5728\u6d3e\u751f\u7c7b\u7684\u7ee7\u627f\u5217\u8868\u4e2d\u63d2\u5165\" virtual \"\u5173\u952e\u5b57\u3002\u8fd9\u5c06\u521b\u5efa\u6240\u8c13\u7684 \u865a\u57fa\u7c7b \uff0c\u8fd9\u610f\u5473\u7740\u53ea\u6709\u4e00\u4e2a\u57fa\u7c7b\u5bf9\u8c61\u3002\u57fa\u7c7b\u5bf9\u8c61\u5728\u7ee7\u627f\u6811\u4e2d\u7684\u6240\u6709\u5bf9\u8c61\u4e4b\u95f4\u5171\u4eab\uff0c\u5e76\u4e14\u53ea\u6784\u9020\u4e00\u6b21\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b(\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u6ca1\u6709\u6784\u9020\u51fd\u6570)\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528 virtual \u5173\u952e\u5b57\u521b\u5efa\u5171\u4eab\u57fa\u7c7b: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class PoweredDevice { }; class Scanner : virtual public PoweredDevice { }; class Printer : virtual public PoweredDevice { }; class Copier : public Scanner , public Printer { }; \u73b0\u5728\uff0c\u5f53\u4f60\u521b\u5efa\u4e00\u4e2a Copier \u7c7b\u5bf9\u8c61\u65f6\uff0c\u6bcf\u4e2a Copier \u5c06\u53ea\u83b7\u5f97\u4e00\u4e2a\u7531 Scanner \u548c Printer \u5171\u4eab\u7684 PoweredDevice \u526f\u672c\u3002 \u4f46\u662f\uff0c\u8fd9\u53c8\u5bfc\u81f4\u4e86\u53e6\u4e00\u4e2a\u95ee\u9898\uff1a\u5982\u679c Scanner \u548c Printer \u5171\u4eab\u4e00\u4e2a PoweredDevice \u57fa\u7c7b\uff0c\u90a3\u4e48\u8c01\u8d1f\u8d23\u521b\u5efa\u5b83\u5462\uff1f\u4e8b\u5b9e\u8bc1\u660e\uff0c\u7b54\u6848\u662f Copier \u3002 Copier \u7684\u6784\u9020\u51fd\u6570\u8d1f\u8d23\u521b\u5efa PoweredDevice \u3002\u56e0\u6b64\uff0c Copier \u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u975e\u76f4\u63a5\u7236\u7c7b(non-immediate-parent\u6784\u9020\u51fd\u6570\u4e00\u6b21\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include class PoweredDevice { public : PoweredDevice ( int power ) { std :: cout << \"PoweredDevice: \" << power << '\\n' ; } }; class Scanner : virtual public PoweredDevice // note: PoweredDevice is now a virtual base class { public : Scanner ( int scanner , int power ) : PoweredDevice { power } // this line is required to create Scanner objects, but ignored in this case { std :: cout << \"Scanner: \" << scanner << '\\n' ; } }; class Printer : virtual public PoweredDevice // note: PoweredDevice is now a virtual base class { public : Printer ( int printer , int power ) : PoweredDevice { power } // this line is required to create Printer objects, but ignored in this case { std :: cout << \"Printer: \" << printer << '\\n' ; } }; class Copier : public Scanner , public Printer { public : Copier ( int scanner , int printer , int power ) : PoweredDevice { power }, // PoweredDevice is constructed here Scanner { scanner , power }, Printer { printer , power } { } }; \u518d\u770b\u524d\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 int main () { Copier copier { 1 , 2 , 3 }; return 0 ; } \u7ed3\u679c\uff1a 1 2 3 PoweredDevice: 3 Scanner: 1 Printer: 2 \u5982\u60a8\u6240\u89c1\uff0c PoweredDevice \u53ea\u6784\u9020\u4e00\u6b21\u3002 \u6709\u51e0\u4e2a\u5bb9\u6613\u5ffd\u89c6\u7684\u7ec6\u8282\uff1a \u9996\u5148\uff0c\u865a\u57fa\u7c7b\u603b\u662f\u5728\u975e\u865a\u57fa\u7c7b\u4e4b\u524d\u521b\u5efa\uff0c\u8fd9\u786e\u4fdd\u4e86\u6240\u6709\u57fa\u7c7b\u90fd\u5728\u5b83\u4eec\u7684\u6d3e\u751f\u7c7b\u4e4b\u524d\u521b\u5efa\u3002 \u5176\u6b21\uff0c\u8bf7\u6ce8\u610f Scanner \u548c Printer \u6784\u9020\u51fd\u6570\u4ecd\u7136\u6709\u5bf9 PoweredDevice \u6784\u9020\u51fd\u6570\u7684\u8c03\u7528\u3002\u5728\u521b\u5efa Copier \u7684\u5b9e\u4f8b\u65f6\uff0c\u8fd9\u4e9b\u6784\u9020\u51fd\u6570\u8c03\u7528\u5c06\u88ab\u5ffd\u7565\uff0c\u56e0\u4e3a Copier \u8d1f\u8d23\u521b\u5efa PoweredDevice \uff0c\u800c\u4e0d\u662f Scanner \u6216 Printer \u3002\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u8981\u521b\u5efa Scanner \u6216 Printer \u7684\u5b9e\u4f8b\uff0c\u5c31\u4f1a\u4f7f\u7528\u8fd9\u4e9b\u6784\u9020\u51fd\u6570\u8c03\u7528\uff0c\u5e76\u5e94\u7528\u6b63\u5e38\u7684\u7ee7\u627f\u89c4\u5219\u3002 \u7b2c\u4e09\uff0c\u5982\u679c\u4e00\u4e2a\u7c7b\u7ee7\u627f\u4e86\u4e00\u4e2a\u6216\u591a\u4e2a\u5177\u6709\u865a\u7236\u7c7b\u7684\u7c7b\uff0c\u5219\u6700\u540e\u88ab\u6d3e\u751f\u7684\u7c7b\u4f1a\u8d1f\u8d23\u6784\u9020\u865a\u57fa\u7c7b\u3002\u5728\u672c\u4f8b\u4e2d\uff0c Copier \u7ee7\u627f\u4e86 Printer \u548c Scanner \uff0c\u5b83\u4eec\u90fd\u6709\u4e00\u4e2a PoweredDevice \u865a\u57fa\u7c7b\u3002\u6700\u6700\u540e\u88ab\u6d3e\u751f\u7684\u7c7b\uff0c\u8d1f\u8d23 PoweredDevice \u7684\u521b\u5efa\u3002\u6ce8\u610f\uff0c\u5373\u4f7f\u5728\u5355\u4e00\u7ee7\u627f\u7684\u60c5\u51b5\u4e0b\u4e5f\u662f\u8fd9\u6837\uff1a\u5982\u679c Copier \u4ece Printer \u5355\u72ec\u7ee7\u627f\uff0c\u800c Printer \u4ece PoweredDevice \u865a\u7ee7\u627f\uff0c\u90a3\u4e48 Copier \u4ecd\u7136\u8d1f\u8d23\u521b\u5efa PoweredDevice \u3002 \u7b2c\u56db\uff0c\u6240\u6709\u7ee7\u627f\u865a\u57fa\u7c7b\u7684\u7c7b\u90fd\u5c06\u6709\u4e00\u4e2a\u865a\u8868\uff0c\u5373\u4f7f\u5b83\u4eec\u901a\u5e38\u6ca1\u6709\u865a\u8868\uff0c\u56e0\u6b64\u7c7b\u7684\u5b9e\u4f8b\u5176\u5927\u5c0f\u4f1a\u589e\u52a0\u4e00\u4e2a\u6307\u9488\u3002 \u56e0\u4e3a Scanner \u548c Printer \u5b9e\u9645\u4e0a\u662f\u4ece PoweredDevice \u6d3e\u751f\u7684\uff0c\u6240\u4ee5 Copier \u53ea\u662f\u4e00\u4e2a PoweredDevice \u5b50\u5bf9\u8c61(subobject)\u3002 Scanner \u548c Printer \u90fd\u9700\u8981\u77e5\u9053\u5982\u4f55\u627e\u5230\u5355\u4e2a PoweredDevice \u5b50\u5bf9\u8c61\uff0c\u8fd9\u6837\u5b83\u4eec\u624d\u80fd\u8bbf\u95ee\u5b83\u7684\u6210\u5458(\u56e0\u4e3a\u5b83\u4eec\u6bd5\u7adf\u662f\u4ece\u5b83\u6d3e\u751f\u7684)\u3002\u8fd9\u901a\u5e38\u662f\u901a\u8fc7\u4e00\u4e9b\u865a\u8868\u64cd\u4f5c\u6765\u5b8c\u6210\u7684(\u5b83\u5b9e\u9645\u4e0a\u5b58\u50a8\u4e86\u4ece\u6bcf\u4e2a\u5b50\u7c7b\u5230 PoweredDevice \u5b50\u5bf9\u8c61\u7684\u504f\u79fb\u91cf)\u3002","title":"18.8 - \u865a\u57fa\u7c7b"},{"location":"18-8-virtual-base-classes/#_1","text":"\u4e0b\u9762\u662f\u6211\u4eec\u4e0a\u4e00\u8bfe\u4e2d\u7684\u4f8b\u5b50(\u591a\u4e86\u4e00\u4e9b\u6784\u9020\u51fd\u6570)\uff0c\u8bf4\u660e\u83f1\u5f62\u7ee7\u627f\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include class PoweredDevice { public : PoweredDevice ( int power ) { std :: cout << \"PoweredDevice: \" << power << '\\n' ; } }; class Scanner : public PoweredDevice { public : Scanner ( int scanner , int power ) : PoweredDevice { power } { std :: cout << \"Scanner: \" << scanner << '\\n' ; } }; class Printer : public PoweredDevice { public : Printer ( int printer , int power ) : PoweredDevice { power } { std :: cout << \"Printer: \" << printer << '\\n' ; } }; class Copier : public Scanner , public Printer { public : Copier ( int scanner , int printer , int power ) : Scanner { scanner , power }, Printer { printer , power } { } }; \u4e5f\u8bb8\u4f60\u4f1a\u8ba4\u4e3a\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u5f97\u5230\u4e0b\u9762\u8fd9\u6837\u7684\u7ee7\u627f\u7ed3\u6784\uff1a \u5982\u679c\u8981\u521b\u5efa\u4e00\u4e2a Copier \u7c7b\u5bf9\u8c61\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u6700\u7ec8\u4f1a\u5f97\u5230 PoweredDevice \u7c7b\u7684\u4e24\u4e2a\u526f\u672c\u2014\u2014\u4e00\u4e2a\u6765\u81ea Printer \uff0c\u4e00\u4e2a\u6765\u81ea Scanner \u3002\u5b83\u6709\u4ee5\u4e0b\u7ed3\u6784: \u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7b80\u77ed\u7684\u793a\u4f8b\u6765\u6f14\u793a\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 int main () { Copier copier { 1 , 2 , 3 }; return 0 ; } 1 2 3 4 PoweredDevice: 3 Scanner: 1 PoweredDevice: 3 Printer: 2 \u770b\u5230\u4e86\u5417\uff1f PoweredDevice \u88ab\u6784\u5efa\u4e86\u4e24\u6b21\u3002 \u867d\u7136\u8fd9\u901a\u5e38\u662f\u671f\u671b\u7684\u7ed3\u679c\uff0c\u4f46\u5728\u6709\u65f6\u4f60\u53ef\u80fd\u53ea\u60f3\u8ba9 Scanner \u548c Printer \u5171\u4eab PoweredDevice \u7684\u4e00\u4e2a\u526f\u672c\u3002","title":"\u83f1\u5f62\u7ee7\u627f\u95ee\u9898"},{"location":"18-8-virtual-base-classes/#_2","text":"\u8981\u5171\u4eab\u57fa\u7c7b\uff0c\u53ea\u9700\u5728\u6d3e\u751f\u7c7b\u7684\u7ee7\u627f\u5217\u8868\u4e2d\u63d2\u5165\" virtual \"\u5173\u952e\u5b57\u3002\u8fd9\u5c06\u521b\u5efa\u6240\u8c13\u7684 \u865a\u57fa\u7c7b \uff0c\u8fd9\u610f\u5473\u7740\u53ea\u6709\u4e00\u4e2a\u57fa\u7c7b\u5bf9\u8c61\u3002\u57fa\u7c7b\u5bf9\u8c61\u5728\u7ee7\u627f\u6811\u4e2d\u7684\u6240\u6709\u5bf9\u8c61\u4e4b\u95f4\u5171\u4eab\uff0c\u5e76\u4e14\u53ea\u6784\u9020\u4e00\u6b21\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b(\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u6ca1\u6709\u6784\u9020\u51fd\u6570)\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528 virtual \u5173\u952e\u5b57\u521b\u5efa\u5171\u4eab\u57fa\u7c7b: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class PoweredDevice { }; class Scanner : virtual public PoweredDevice { }; class Printer : virtual public PoweredDevice { }; class Copier : public Scanner , public Printer { }; \u73b0\u5728\uff0c\u5f53\u4f60\u521b\u5efa\u4e00\u4e2a Copier \u7c7b\u5bf9\u8c61\u65f6\uff0c\u6bcf\u4e2a Copier \u5c06\u53ea\u83b7\u5f97\u4e00\u4e2a\u7531 Scanner \u548c Printer \u5171\u4eab\u7684 PoweredDevice \u526f\u672c\u3002 \u4f46\u662f\uff0c\u8fd9\u53c8\u5bfc\u81f4\u4e86\u53e6\u4e00\u4e2a\u95ee\u9898\uff1a\u5982\u679c Scanner \u548c Printer \u5171\u4eab\u4e00\u4e2a PoweredDevice \u57fa\u7c7b\uff0c\u90a3\u4e48\u8c01\u8d1f\u8d23\u521b\u5efa\u5b83\u5462\uff1f\u4e8b\u5b9e\u8bc1\u660e\uff0c\u7b54\u6848\u662f Copier \u3002 Copier \u7684\u6784\u9020\u51fd\u6570\u8d1f\u8d23\u521b\u5efa PoweredDevice \u3002\u56e0\u6b64\uff0c Copier \u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u975e\u76f4\u63a5\u7236\u7c7b(non-immediate-parent\u6784\u9020\u51fd\u6570\u4e00\u6b21\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include class PoweredDevice { public : PoweredDevice ( int power ) { std :: cout << \"PoweredDevice: \" << power << '\\n' ; } }; class Scanner : virtual public PoweredDevice // note: PoweredDevice is now a virtual base class { public : Scanner ( int scanner , int power ) : PoweredDevice { power } // this line is required to create Scanner objects, but ignored in this case { std :: cout << \"Scanner: \" << scanner << '\\n' ; } }; class Printer : virtual public PoweredDevice // note: PoweredDevice is now a virtual base class { public : Printer ( int printer , int power ) : PoweredDevice { power } // this line is required to create Printer objects, but ignored in this case { std :: cout << \"Printer: \" << printer << '\\n' ; } }; class Copier : public Scanner , public Printer { public : Copier ( int scanner , int printer , int power ) : PoweredDevice { power }, // PoweredDevice is constructed here Scanner { scanner , power }, Printer { printer , power } { } }; \u518d\u770b\u524d\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 int main () { Copier copier { 1 , 2 , 3 }; return 0 ; } \u7ed3\u679c\uff1a 1 2 3 PoweredDevice: 3 Scanner: 1 Printer: 2 \u5982\u60a8\u6240\u89c1\uff0c PoweredDevice \u53ea\u6784\u9020\u4e00\u6b21\u3002 \u6709\u51e0\u4e2a\u5bb9\u6613\u5ffd\u89c6\u7684\u7ec6\u8282\uff1a \u9996\u5148\uff0c\u865a\u57fa\u7c7b\u603b\u662f\u5728\u975e\u865a\u57fa\u7c7b\u4e4b\u524d\u521b\u5efa\uff0c\u8fd9\u786e\u4fdd\u4e86\u6240\u6709\u57fa\u7c7b\u90fd\u5728\u5b83\u4eec\u7684\u6d3e\u751f\u7c7b\u4e4b\u524d\u521b\u5efa\u3002 \u5176\u6b21\uff0c\u8bf7\u6ce8\u610f Scanner \u548c Printer \u6784\u9020\u51fd\u6570\u4ecd\u7136\u6709\u5bf9 PoweredDevice \u6784\u9020\u51fd\u6570\u7684\u8c03\u7528\u3002\u5728\u521b\u5efa Copier \u7684\u5b9e\u4f8b\u65f6\uff0c\u8fd9\u4e9b\u6784\u9020\u51fd\u6570\u8c03\u7528\u5c06\u88ab\u5ffd\u7565\uff0c\u56e0\u4e3a Copier \u8d1f\u8d23\u521b\u5efa PoweredDevice \uff0c\u800c\u4e0d\u662f Scanner \u6216 Printer \u3002\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u8981\u521b\u5efa Scanner \u6216 Printer \u7684\u5b9e\u4f8b\uff0c\u5c31\u4f1a\u4f7f\u7528\u8fd9\u4e9b\u6784\u9020\u51fd\u6570\u8c03\u7528\uff0c\u5e76\u5e94\u7528\u6b63\u5e38\u7684\u7ee7\u627f\u89c4\u5219\u3002 \u7b2c\u4e09\uff0c\u5982\u679c\u4e00\u4e2a\u7c7b\u7ee7\u627f\u4e86\u4e00\u4e2a\u6216\u591a\u4e2a\u5177\u6709\u865a\u7236\u7c7b\u7684\u7c7b\uff0c\u5219\u6700\u540e\u88ab\u6d3e\u751f\u7684\u7c7b\u4f1a\u8d1f\u8d23\u6784\u9020\u865a\u57fa\u7c7b\u3002\u5728\u672c\u4f8b\u4e2d\uff0c Copier \u7ee7\u627f\u4e86 Printer \u548c Scanner \uff0c\u5b83\u4eec\u90fd\u6709\u4e00\u4e2a PoweredDevice \u865a\u57fa\u7c7b\u3002\u6700\u6700\u540e\u88ab\u6d3e\u751f\u7684\u7c7b\uff0c\u8d1f\u8d23 PoweredDevice \u7684\u521b\u5efa\u3002\u6ce8\u610f\uff0c\u5373\u4f7f\u5728\u5355\u4e00\u7ee7\u627f\u7684\u60c5\u51b5\u4e0b\u4e5f\u662f\u8fd9\u6837\uff1a\u5982\u679c Copier \u4ece Printer \u5355\u72ec\u7ee7\u627f\uff0c\u800c Printer \u4ece PoweredDevice \u865a\u7ee7\u627f\uff0c\u90a3\u4e48 Copier \u4ecd\u7136\u8d1f\u8d23\u521b\u5efa PoweredDevice \u3002 \u7b2c\u56db\uff0c\u6240\u6709\u7ee7\u627f\u865a\u57fa\u7c7b\u7684\u7c7b\u90fd\u5c06\u6709\u4e00\u4e2a\u865a\u8868\uff0c\u5373\u4f7f\u5b83\u4eec\u901a\u5e38\u6ca1\u6709\u865a\u8868\uff0c\u56e0\u6b64\u7c7b\u7684\u5b9e\u4f8b\u5176\u5927\u5c0f\u4f1a\u589e\u52a0\u4e00\u4e2a\u6307\u9488\u3002 \u56e0\u4e3a Scanner \u548c Printer \u5b9e\u9645\u4e0a\u662f\u4ece PoweredDevice \u6d3e\u751f\u7684\uff0c\u6240\u4ee5 Copier \u53ea\u662f\u4e00\u4e2a PoweredDevice \u5b50\u5bf9\u8c61(subobject)\u3002 Scanner \u548c Printer \u90fd\u9700\u8981\u77e5\u9053\u5982\u4f55\u627e\u5230\u5355\u4e2a PoweredDevice \u5b50\u5bf9\u8c61\uff0c\u8fd9\u6837\u5b83\u4eec\u624d\u80fd\u8bbf\u95ee\u5b83\u7684\u6210\u5458(\u56e0\u4e3a\u5b83\u4eec\u6bd5\u7adf\u662f\u4ece\u5b83\u6d3e\u751f\u7684)\u3002\u8fd9\u901a\u5e38\u662f\u901a\u8fc7\u4e00\u4e9b\u865a\u8868\u64cd\u4f5c\u6765\u5b8c\u6210\u7684(\u5b83\u5b9e\u9645\u4e0a\u5b58\u50a8\u4e86\u4ece\u6bcf\u4e2a\u5b50\u7c7b\u5230 PoweredDevice \u5b50\u5bf9\u8c61\u7684\u504f\u79fb\u91cf)\u3002","title":"\u865a\u57fa\u7c7b"},{"location":"18-9-object-slicing/","tags":["virtual-base-classes"],"text":"Key Takeaway \u6309\u503c\u4f20\u9012 \u5bf9\u8c61\u65f6\uff0c\u5982\u679c\u5f62\u53c2\u662f\u57fa\u7c7b\uff0c\u5b9e\u53c2\u662f\u6d3e\u751f\u7c7b\uff0c\u5219\u4f1a\u88ab\u5207\u7247\uff0c\u5bfc\u81f4\u6d3e\u751f\u7c7b\u4e2d\u865a\u51fd\u6570\u65e0\u6cd5\u8c03\u7528\u3002\u6700\u4fdd\u9669\u7684\u662f \u6309\u5f15\u7528\u4f20\u9012 \uff0c\u6b64\u65f6\u5373\u4f7f\u8f6c\u6362\u6210\u4e86\u57fa\u7c7b\u7684\u5f15\u7528\uff0c\u4e5f\u662f\u4e0d\u59a8\u788d\u865a\u51fd\u6570\u5de5\u4f5c\u3002 \u786e\u4fdd\u51fd\u6570\u5f62\u53c2\u662f\u5f15\u7528(\u6216\u6307\u9488)\uff0c\u5e76\u5728\u6d3e\u751f\u7c7b\u4e2d\u5c3d\u91cf\u907f\u514d\u4efb\u4f55\u5f62\u5f0f\u7684\u503c\u4f20\u9012\u3002 \u8ba9\u6211\u4eec\u56de\u5230\u6211\u4eec\u4e4b\u524d\u7684\u4e00\u4e2a\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include class Base { protected : int m_value {}; public : Base ( int value ) : m_value { value } { } virtual const char * getName () const { return \"Base\" ; } int getValue () const { return m_value ; } }; class Derived : public Base { public : Derived ( int value ) : Base { value } { } const char * getName () const override { return \"Derived\" ; } }; int main () { Derived derived { 5 }; std :: cout << \"derived is a \" << derived . getName () << \" and has value \" << derived . getValue () << '\\n' ; Base & ref { derived }; std :: cout << \"ref is a \" << ref . getName () << \" and has value \" << ref . getValue () << '\\n' ; Base * ptr { & derived }; std :: cout << \"ptr is a \" << ptr -> getName () << \" and has value \" << ptr -> getValue () << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c ref \u5f15\u7528\u548c ptr \u6307\u5411 derived \uff0c\u5b83\u6709\u4e00\u4e2a Base \u90e8\u5206\u548c\u4e00\u4e2a derived \u90e8\u5206\u3002\u56e0\u4e3a ref \u548c ptr \u662f Base \u7c7b\u578b\uff0c\u6240\u4ee5 ref \u548c ptr \u53ea\u80fd\u770b\u5230 derived \u7684 Base \u90e8\u5206\u2014\u2014 derived \u7684 derived \u90e8\u5206\u4ecd\u7136\u5b58\u5728\uff0c\u4f46\u4e0d\u80fd\u901a\u8fc7 ref \u6216 ptr \u770b\u5230\u3002\u4f46\u662f\uff0c\u901a\u8fc7\u4f7f\u7528\u865a\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbf\u95ee\u51fd\u6570\u7684\u6700\u540e\u6d3e\u751f\u7684\u7248\u672c\u3002\u56e0\u6b64\uff0c\u4e0a\u9762\u7684\u7a0b\u5e8f\u8f93\u51fa: 1 2 3 derived is a Derived and has value 5 ref is a Derived and has value 5 ptr is a Derived and has value 5 \u4f46\u662f\uff0c\u5982\u679c Base \u4e0d\u5f15\u7528\u6216\u6307\u9488\u6307\u5411 Derived \u5bf9\u8c61\uff0c\u800c\u662f\u7b80\u5355\u5730\u5c06 Derived \u5bf9\u8c61 \u8d4b\u503c \u7ed9 Base \u5bf9\u8c61\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u60c5\u51b5\u5462? 1 2 3 4 5 6 7 8 int main () { Derived derived { 5 }; Base base { derived }; // what happens here? std :: cout << \"base is a \" << base . getName () << \" and has value \" << base . getValue () << '\\n' ; return 0 ; } derived \u5305\u542b\u4e00\u4e2a Base \u90e8\u5206\u548c Derived \u90e8\u5206\u3002\u5f53\u5c06 Derived \u5bf9\u8c61\u8d4b\u503c\u7ed9 Base \u5bf9\u8c61\u65f6\uff0c\u53ea\u4f1a\u590d\u5236\u6d3e\u751f\u5bf9\u8c61\u4e2d\u7684 Base \u90e8\u5206\uff0c\u800c Derived \u5219\u4e0d\u4f1a\u88ab\u8d4b\u503c\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c base \u63a5\u6536\u4e86 derived \u7684 base \u90e8\u5206\u7684\u526f\u672c\uff0c\u4f46\u4e0d\u4f1a\u83b7\u53d6 derived \u90e8\u5206\u7684\u526f\u672c\uff0c\u4e5f\u5c31\u662f\u8bf4 Derived \u90e8\u5206\u5b9e\u9645\u4e0a\u5df2\u7ecf\u88ab\u201c\u5207\u6389\u4e86\u201d\u3002\u56e0\u6b64\uff0c\u5c06\u6d3e\u751f\u7c7b\u5bf9\u8c61\u8d4b\u503c\u7ed9\u57fa\u7c7b\u5bf9\u8c61\u79f0\u4e3a \u5bf9\u8c61\u5207\u7247 (\u6216\u7b80\u79f0\u5207\u7247)\u3002 \u56e0\u4e3a base \u53d8\u91cf\u6ca1\u6709 Derived \u90e8\u5206\uff0c base.getName() \u53ea\u80fd\u89e3\u6790\u4e3a base::getName() \u3002 \u4e0a\u9762\u7684\u7a0b\u5e8f\u6253\u5370\u51fa\uff1a 1 base is a Base and has value 5 \u4f7f\u7528\u5f97\u5f53\u7684\u8bdd\uff0c\u5bf9\u8c61\u5207\u7247\u662f\u5f88\u6709\u7528\u7684\u3002\u7136\u800c\uff0c\u5982\u679c\u4f7f\u7528\u4e0d\u5f53\uff0c\u5207\u7247\u4f1a\u4ee5\u4e0d\u540c\u7684\u65b9\u5f0f\u5bfc\u81f4\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\u3002\u8ba9\u6211\u4eec\u6765\u770b\u770b\u5176\u4e2d\u7684\u4e00\u4e9b\u6848\u4f8b\u3002 \u5207\u7247\u548c\u51fd\u6570 \u73b0\u5728\uff0c\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u4e0a\u9762\u7684\u4f8b\u5b50\u6709\u70b9\u50bb\u3002\u6bd5\u7adf\uff0c\u4e3a\u4ec0\u4e48\u8981\u8fd9\u6837\u628a derived \u8d4b\u503c\u7ed9 base \u5462\uff1f\u4e00\u822c\u5f88\u5c11\u4f1a\u8fd9\u4e48\u505a\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u51fd\u6570\u6765\u8bf4\uff0c\u5bf9\u8c61\u5207\u7247\u662f\u53ef\u80fd\u5728\u4e0d\u7ecf\u610f\u95f4\u53d1\u751f\u7684\u3002 \u8003\u8651\u4e0b\u9762\u7684\u51fd\u6570\uff1a 1 2 3 4 void printName ( const Base base ) // \u6ce8\u610f: base \u6309\u503c\u4f20\u9012\uff0c\u800c\u4e0d\u662f\u5f15\u7528 { std :: cout << \"I am a \" << base . getName () << '\\n' ; } \u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u51fd\u6570\uff0c\u5e26\u6709\u4e00\u4e2a \u6309\u503c\u4f20\u9012 \u7684 const base \u5bf9\u8c61\u5f62\u53c2\u3002\u5982\u679c\u6211\u4eec\u50cf\u8fd9\u6837\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 int main () { Derived d { 5 }; printName ( d ); // oops, \u6ca1\u6709\u610f\u8bc6\u5230\u5728\u5b9e\u9645\u8c03\u7528\u65f6\u662f\u6309\u503c\u4f20\u9012\u7684 return 0 ; } \u5728\u7f16\u5199\u8fd9\u4e2a\u7a0b\u5e8f\u65f6\uff0c\u60a8\u53ef\u80fd\u6ca1\u6709\u6ce8\u610f\u5230 base \u662f\u4e00\u4e2a\u503c\u5f62\u53c2\uff0c\u800c\u4e0d\u662f\u5f15\u7528\u3002\u56e0\u6b64\uff0c\u5f53 printName(d) \u8c03\u7528\u65f6\uff0c\u867d\u7136\u6211\u4eec\u53ef\u80fd\u671f\u671b base.getName() \u8c03\u7528\u865a\u51fd\u6570 getName() \u5e76\u6253\u5370\u201cI am a Derived\u201d\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u3002\u76f8\u53cd\uff0c Derived \u5bf9\u8c61 d \u88ab\u5207\u7247\uff0c\u53ea\u6709 Base \u90e8\u5206\u88ab\u590d\u5236\u5230 base \u53c2\u6570\u4e2d\u3002\u5f53 base.getName() \u6267\u884c\u65f6\uff0c\u5373\u4f7f\u5bf9 getName() \u51fd\u6570\u8fdb\u884c\u4e86\u865a\u5316\uff0c\u4e5f\u6ca1\u6709\u7c7b\u7684 Derived \u90e8\u5206\u4f9b\u5b83\u89e3\u6790\u3002\u56e0\u6b64\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u8f93\u51fa\uff1a 1 I am a Base \u8fd9\u91cc\u53ef\u4ee5\u660e\u663e\u770b\u5230\u95ee\u9898\uff0c\u4f46\u5982\u679c\u51fd\u6570\u6ca1\u6709\u50cf\u8fd9\u6837\u5b9e\u9645\u6253\u5370\u4efb\u4f55\u4fe1\u606f\uff0c\u90a3\u4e48\u8be5\u95ee\u9898\u4f1a\u975e\u5e38\u96be\u4ee5\u5b9a\u4f4d\u3002 \u5f53\u7136\uff0c \u901a\u8fc7\u5c06\u51fd\u6570\u5f62\u53c2\u4f5c\u4e3a\u5f15\u7528\u800c\u4e0d\u662f\u6309\u503c\u4f20\u9012\uff0c\u5c31\u53ef\u4ee5\u907f\u514d\u5207\u7247(\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u6309\u5f15\u7528\u800c\u4e0d\u662f\u6309\u503c\u4f20\u9012\u7c7b\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\u7684\u53e6\u4e00\u4e2a\u539f\u56e0)\u3002 1 2 3 4 5 6 7 8 9 10 11 12 void printName ( const Base & base ) // note: base now passed by reference { std :: cout << \"I am a \" << base . getName () << '\\n' ; } int main () { Derived d { 5 }; printName ( d ); return 0 ; } COPY This prints: 1 I am a Derived vector \u5207\u7247 \u65b0\u7a0b\u5e8f\u5458\u65f6\u5e38\u5728\u4f7f\u7528 std::vector \u5b9e\u73b0\u591a\u6001\u65f6\u9047\u5230\u95ee\u9898\u3002\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { std :: vector < Base > v {}; v . push_back ( Base { 5 }); // \u6dfb\u52a0\u4e00\u4e2a Base \u5bf9\u8c61\u5230 vector v . push_back ( Derived { 6 }); // \u6dfb\u52a0\u4e00\u4e2a Derived \u5bf9\u8c61\u5230 vector // \u6253\u5370 vector \u4e2d\u7684\u6240\u6709\u5143\u7d20 for ( const auto & element : v ) std :: cout << \"I am a \" << element . getName () << \" with value \" << element . getValue () << '\\n' ; return 0 ; } \u7f16\u8bd1\u8fd0\u884c\u7a0b\u5e8f\uff0c\u8f93\u51fa\uff1a 1 2 I am a Base with value 5 I am a Base with value 6 \u548c\u4e4b\u524d\u7684\u4f8b\u5b50\u7c7b\u4f3c\uff0c\u56e0\u4e3a std::vector \u88ab\u58f0\u660e\u4e3a\u4e86 Base \u7c7b\u578b\u7684\u5bb9\u5668\uff0c\u5f53\u6dfb\u52a0 Derived(6) \u65f6\uff0c\u5b83\u88ab\u5207\u7247\u4e86\u3002 \u4fee\u590d\u8fd9\u4e2a\u95ee\u9898\u6709\u70b9\u9ebb\u70e6\u3002\u8bb8\u591a\u65b0\u7a0b\u5e8f\u5458\u6700\u5148\u60f3\u5230\u521b\u5efa\u4e00\u4e2a\u7c7b\u578b\u4e3a\u57fa\u7c7b\u5f15\u7528\u7684 std::vector \uff0c\u50cf\u8fd9\u6837\uff1a 1 std :: vector < Base &> v {}; \u53ef\u60dc\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u6839\u672c\u5c31\u4e0d\u80fd\u7f16\u8bd1\u3002 std::vector \u4e2d\u7684\u5bf9\u8c61\u662f\u5fc5\u987b\u53ef\u4ee5\u8d4b\u503c\u7684\uff0c\u663e\u7136 \u5de6\u503c\u5f15\u7528 \u4e0d\u6ee1\u8db3\u8981\u6c42\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u552f\u4e00\u529e\u6cd5\u662f\u5c06\u7c7b\u578b\u5b9a\u4e49\u4e3a\u57fa\u7c7b\u7684\u6307\u9488\u7c7b\u578b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include int main () { std :: vector < Base *> v {}; Base b { 5 }; // b \u548c d \u4e0d\u80fd\u662f\u533f\u540d\u5bf9\u8c61\uff08\u6307\u9488\u4e0d\u80fd\u6307\u5411\u533f\u540d\u5bf9\u8c61\uff09 Derived d { 6 }; v . push_back ( & b ); // add a Base object to our vector v . push_back ( & d ); // add a Derived object to our vector // Print out all of the elements in our vector for ( const auto * element : v ) std :: cout << \"I am a \" << element -> getName () << \" with value \" << element -> getValue () << '\\n' ; return 0 ; } \u6253\u5370\uff1a 1 2 I am a Base with value 5 I am a Derived with value 6 \u641e\u5b9a\uff01\u5bf9\u6b64\u6709\u51e0\u70b9\u8fd8\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0cnullptr \u73b0\u5728\u4e5f\u53ef\u4ee5\u88ab\u5b58\u8fdb\u53bb\uff0c\u8fd9\u53ef\u80fd\u662f\u4f60\u60f3\u8981\u7684\u7ed3\u679c\u4e5f\u53ef\u80fd\u4e0d\u662f\u3002\u5176\u6b21\uff0c\u4f60\u73b0\u5728\u5fc5\u987b\u5904\u7406\u6307\u9488\u8bed\u4e49\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5f88\u9ebb\u70e6\u3002\u597d\u7684\u65b9\u9762\u662f\uff0c\u8fd9\u6837\u4e00\u6765\u5c31\u53ef\u4ee5\u4f7f\u7528\u52a8\u6001\u5185\u5b58\u5206\u914d\uff0c\u5982\u679c\u5bf9\u8c61\u9700\u8981\u8d85\u51fa\u4f5c\u7528\u57df\uff0c\u8fd9\u662f\u5f88\u6709\u7528\u7684\u3002 \u53e6\u4e00\u4e2a\u9009\u62e9\u662f\u4f7f\u7528 std::reference_wrapper \uff0c\u5b83\u662f\u4e00\u4e2a\u7c7b\uff0c\u53ef\u6a21\u62df\u51fa\u53ef\u8d4b\u503c\u7684\u5f15\u7528\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include // for std::reference_wrapper #include #include class Base { protected : int m_value {}; public : Base ( int value ) : m_value { value } { } virtual const char * getName () const { return \"Base\" ; } int getValue () const { return m_value ; } }; class Derived : public Base { public : Derived ( int value ) : Base { value } { } const char * getName () const override { return \"Derived\" ; } }; int main () { std :: vector < std :: reference_wrapper < Base >> v {}; // \u5b58\u653e Base \u7684\u53ef\u8d4b\u503c\u5f15\u7528\u7684\u5bb9\u5668 Base b { 5 }; // b and d can't be anonymous objects Derived d { 6 }; v . push_back ( b ); // add a Base object to our vector v . push_back ( d ); // add a Derived object to our vector // Print out all of the elements in our vector // we use .get() to get our element out of the std::reference_wrapper for ( const auto & element : v ) // element has type const std::reference_wrapper& std :: cout << \"I am a \" << element . get (). getName () << \" with value \" << element . get (). getValue () << '\\n' ; return 0 ; } \u7f1d\u5408\u602a\u5bf9\u8c61 Frankenobject \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u770b\u5230\u4e86\u5bf9\u8c61\u5207\u7247\u53ef\u80fd\u5bfc\u81f4\u9519\u8bef\u7684\u60c5\u51b5\uff08\u7531\u4e8e\u6d3e\u751f\u7c7b\u88ab\u5207\u6389\u6240\u5f15\u8d77\uff09\u3002\u73b0\u5728\u8ba9\u6211\u4eec\u6765\u770b\u53e6\u4e00\u79cd\u5371\u9669\u7684\u60c5\u51b5\uff0c\u5176\u4e2d\u6d3e\u751f\u5bf9\u8c61\u4ecd\u7136\u5b58\u5728\uff01 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { Derived d1 { 5 }; Derived d2 { 6 }; Base & b { d2 }; b = d1 ; // \u95ee\u9898\u6240\u5728\uff08\u901a\u8fc7b\u7ed9d2\u8d4b\u503c\u4e3ad1\uff0c\u6ce8\u610f\u8fd9\u91cc\u4e0d\u662f\u7ed9\u5f15\u7528\u91cd\u65b0\u8d4b\u503c\uff09 return 0 ; } \u51fd\u6570\u7684\u524d\u4e09\u884c\u975e\u5e38\u7b80\u5355\u3002\u521b\u5efa\u4e24\u4e2a Derived \u5bf9\u8c61\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u7b2c\u4e8c\u4e2a\u5bf9\u8c61\u7684 Base \u7c7b\u578b\u5f15\u7528\u3002 \u7b2c\u56db\u884c\u662f\u51fa\u9519\u7684\u5730\u65b9\u3002\u56e0\u4e3a b \u6307\u5411 d2 \uff0c\u6211\u4eec\u628a d1 \u8d4b\u503c\u7ed9 b \uff08\u76f8\u5f53\u4e8e d2=d1 \uff09\uff0c\u4f60\u53ef\u80fd\u8ba4\u4e3a\u7ed3\u679c\u662f d1 \u4f1a\u88ab\u590d\u5236\u5230 d2 \u4e2d\u3002\u4f46\u662f b \u662f Base , \u800cC++\u4e3a\u7c7b\u63d0\u4f9b\u7684\u64cd\u4f5c\u7b26=\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4e0d\u662f\u865a\u51fd\u6570\u7684\u3002\u56e0\u6b64\uff0c\u53ea\u6709 d1 \u7684 Base \u90e8\u5206\u88ab\u590d\u5236\u5230 d2 \u4e2d\u3002 \u5c31\u662f d2 \u73b0\u5728\u6709 d1 \u7684\u57fa\u90e8\u5206\u548c d2 \u7684\u6d3e\u751f\u90e8\u5206\u3002\u5728\u8fd9\u4e2a\u7279\u5b9a\u7684\u4f8b\u5b50\u4e2d\uff0c\u8fd9\u4e0d\u662f\u95ee\u9898(\u56e0\u4e3a Derived \u7c7b\u672c\u8eab\u6ca1\u6709\u6570\u636e)\uff0c\u4f46\u662f\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f60\u5b9e\u9645\u521b\u5efa\u4e00\u4e2a \u7f1d\u5408\u602a\u5bf9\u8c61 \u2014\u2014\u7531\u591a\u4e2a\u5bf9\u8c61\u7684\u90e8\u5206\u7ec4\u6210\u3002\u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u6ca1\u6709\u4ec0\u4e48\u7b80\u5355\u7684\u65b9\u6cd5\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u7684\u53d1\u751f(\u53ea\u80fd\u5c3d\u91cf\u907f\u514d\u8fd9\u6837\u7684\u8d4b\u503c)\u3002 \u7ed3\u8bba \u5c3d\u7ba1C++\u652f\u6301\u901a\u8fc7\u5bf9\u8c61\u5207\u7247\u5c06\u6d3e\u751f\u5bf9\u8c61\u5206\u914d\u7ed9\u57fa\u5bf9\u8c61\uff0c\u4f46\u8fd9\u4e48\u505a\u591a\u6570\u60c5\u51b5\u4e0b\u4f1a\u5e26\u6765\u95ee\u9898\uff0c\u56e0\u6b64\u5e94\u8be5\u5c3d\u91cf\u907f\u514d\u5207\u7247\u3002 \u786e\u4fdd\u51fd\u6570\u5f62\u53c2\u662f\u5f15\u7528(\u6216\u6307\u9488)\uff0c\u5e76\u5728\u6d3e\u751f\u7c7b\u4e2d\u5c3d\u91cf\u907f\u514d\u4efb\u4f55\u5f62\u5f0f\u7684\u503c\u4f20\u9012\u3002","title":"18.9 - \u5bf9\u8c61\u5207\u7247"},{"location":"18-9-object-slicing/#_1","text":"\u73b0\u5728\uff0c\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u4e0a\u9762\u7684\u4f8b\u5b50\u6709\u70b9\u50bb\u3002\u6bd5\u7adf\uff0c\u4e3a\u4ec0\u4e48\u8981\u8fd9\u6837\u628a derived \u8d4b\u503c\u7ed9 base \u5462\uff1f\u4e00\u822c\u5f88\u5c11\u4f1a\u8fd9\u4e48\u505a\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u51fd\u6570\u6765\u8bf4\uff0c\u5bf9\u8c61\u5207\u7247\u662f\u53ef\u80fd\u5728\u4e0d\u7ecf\u610f\u95f4\u53d1\u751f\u7684\u3002 \u8003\u8651\u4e0b\u9762\u7684\u51fd\u6570\uff1a 1 2 3 4 void printName ( const Base base ) // \u6ce8\u610f: base \u6309\u503c\u4f20\u9012\uff0c\u800c\u4e0d\u662f\u5f15\u7528 { std :: cout << \"I am a \" << base . getName () << '\\n' ; } \u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u51fd\u6570\uff0c\u5e26\u6709\u4e00\u4e2a \u6309\u503c\u4f20\u9012 \u7684 const base \u5bf9\u8c61\u5f62\u53c2\u3002\u5982\u679c\u6211\u4eec\u50cf\u8fd9\u6837\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 int main () { Derived d { 5 }; printName ( d ); // oops, \u6ca1\u6709\u610f\u8bc6\u5230\u5728\u5b9e\u9645\u8c03\u7528\u65f6\u662f\u6309\u503c\u4f20\u9012\u7684 return 0 ; } \u5728\u7f16\u5199\u8fd9\u4e2a\u7a0b\u5e8f\u65f6\uff0c\u60a8\u53ef\u80fd\u6ca1\u6709\u6ce8\u610f\u5230 base \u662f\u4e00\u4e2a\u503c\u5f62\u53c2\uff0c\u800c\u4e0d\u662f\u5f15\u7528\u3002\u56e0\u6b64\uff0c\u5f53 printName(d) \u8c03\u7528\u65f6\uff0c\u867d\u7136\u6211\u4eec\u53ef\u80fd\u671f\u671b base.getName() \u8c03\u7528\u865a\u51fd\u6570 getName() \u5e76\u6253\u5370\u201cI am a Derived\u201d\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u3002\u76f8\u53cd\uff0c Derived \u5bf9\u8c61 d \u88ab\u5207\u7247\uff0c\u53ea\u6709 Base \u90e8\u5206\u88ab\u590d\u5236\u5230 base \u53c2\u6570\u4e2d\u3002\u5f53 base.getName() \u6267\u884c\u65f6\uff0c\u5373\u4f7f\u5bf9 getName() \u51fd\u6570\u8fdb\u884c\u4e86\u865a\u5316\uff0c\u4e5f\u6ca1\u6709\u7c7b\u7684 Derived \u90e8\u5206\u4f9b\u5b83\u89e3\u6790\u3002\u56e0\u6b64\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u8f93\u51fa\uff1a 1 I am a Base \u8fd9\u91cc\u53ef\u4ee5\u660e\u663e\u770b\u5230\u95ee\u9898\uff0c\u4f46\u5982\u679c\u51fd\u6570\u6ca1\u6709\u50cf\u8fd9\u6837\u5b9e\u9645\u6253\u5370\u4efb\u4f55\u4fe1\u606f\uff0c\u90a3\u4e48\u8be5\u95ee\u9898\u4f1a\u975e\u5e38\u96be\u4ee5\u5b9a\u4f4d\u3002 \u5f53\u7136\uff0c \u901a\u8fc7\u5c06\u51fd\u6570\u5f62\u53c2\u4f5c\u4e3a\u5f15\u7528\u800c\u4e0d\u662f\u6309\u503c\u4f20\u9012\uff0c\u5c31\u53ef\u4ee5\u907f\u514d\u5207\u7247(\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u6309\u5f15\u7528\u800c\u4e0d\u662f\u6309\u503c\u4f20\u9012\u7c7b\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\u7684\u53e6\u4e00\u4e2a\u539f\u56e0)\u3002 1 2 3 4 5 6 7 8 9 10 11 12 void printName ( const Base & base ) // note: base now passed by reference { std :: cout << \"I am a \" << base . getName () << '\\n' ; } int main () { Derived d { 5 }; printName ( d ); return 0 ; } COPY This prints: 1 I am a Derived","title":"\u5207\u7247\u548c\u51fd\u6570"},{"location":"18-9-object-slicing/#vector","text":"\u65b0\u7a0b\u5e8f\u5458\u65f6\u5e38\u5728\u4f7f\u7528 std::vector \u5b9e\u73b0\u591a\u6001\u65f6\u9047\u5230\u95ee\u9898\u3002\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { std :: vector < Base > v {}; v . push_back ( Base { 5 }); // \u6dfb\u52a0\u4e00\u4e2a Base \u5bf9\u8c61\u5230 vector v . push_back ( Derived { 6 }); // \u6dfb\u52a0\u4e00\u4e2a Derived \u5bf9\u8c61\u5230 vector // \u6253\u5370 vector \u4e2d\u7684\u6240\u6709\u5143\u7d20 for ( const auto & element : v ) std :: cout << \"I am a \" << element . getName () << \" with value \" << element . getValue () << '\\n' ; return 0 ; } \u7f16\u8bd1\u8fd0\u884c\u7a0b\u5e8f\uff0c\u8f93\u51fa\uff1a 1 2 I am a Base with value 5 I am a Base with value 6 \u548c\u4e4b\u524d\u7684\u4f8b\u5b50\u7c7b\u4f3c\uff0c\u56e0\u4e3a std::vector \u88ab\u58f0\u660e\u4e3a\u4e86 Base \u7c7b\u578b\u7684\u5bb9\u5668\uff0c\u5f53\u6dfb\u52a0 Derived(6) \u65f6\uff0c\u5b83\u88ab\u5207\u7247\u4e86\u3002 \u4fee\u590d\u8fd9\u4e2a\u95ee\u9898\u6709\u70b9\u9ebb\u70e6\u3002\u8bb8\u591a\u65b0\u7a0b\u5e8f\u5458\u6700\u5148\u60f3\u5230\u521b\u5efa\u4e00\u4e2a\u7c7b\u578b\u4e3a\u57fa\u7c7b\u5f15\u7528\u7684 std::vector \uff0c\u50cf\u8fd9\u6837\uff1a 1 std :: vector < Base &> v {}; \u53ef\u60dc\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u6839\u672c\u5c31\u4e0d\u80fd\u7f16\u8bd1\u3002 std::vector \u4e2d\u7684\u5bf9\u8c61\u662f\u5fc5\u987b\u53ef\u4ee5\u8d4b\u503c\u7684\uff0c\u663e\u7136 \u5de6\u503c\u5f15\u7528 \u4e0d\u6ee1\u8db3\u8981\u6c42\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u552f\u4e00\u529e\u6cd5\u662f\u5c06\u7c7b\u578b\u5b9a\u4e49\u4e3a\u57fa\u7c7b\u7684\u6307\u9488\u7c7b\u578b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include int main () { std :: vector < Base *> v {}; Base b { 5 }; // b \u548c d \u4e0d\u80fd\u662f\u533f\u540d\u5bf9\u8c61\uff08\u6307\u9488\u4e0d\u80fd\u6307\u5411\u533f\u540d\u5bf9\u8c61\uff09 Derived d { 6 }; v . push_back ( & b ); // add a Base object to our vector v . push_back ( & d ); // add a Derived object to our vector // Print out all of the elements in our vector for ( const auto * element : v ) std :: cout << \"I am a \" << element -> getName () << \" with value \" << element -> getValue () << '\\n' ; return 0 ; } \u6253\u5370\uff1a 1 2 I am a Base with value 5 I am a Derived with value 6 \u641e\u5b9a\uff01\u5bf9\u6b64\u6709\u51e0\u70b9\u8fd8\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0cnullptr \u73b0\u5728\u4e5f\u53ef\u4ee5\u88ab\u5b58\u8fdb\u53bb\uff0c\u8fd9\u53ef\u80fd\u662f\u4f60\u60f3\u8981\u7684\u7ed3\u679c\u4e5f\u53ef\u80fd\u4e0d\u662f\u3002\u5176\u6b21\uff0c\u4f60\u73b0\u5728\u5fc5\u987b\u5904\u7406\u6307\u9488\u8bed\u4e49\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5f88\u9ebb\u70e6\u3002\u597d\u7684\u65b9\u9762\u662f\uff0c\u8fd9\u6837\u4e00\u6765\u5c31\u53ef\u4ee5\u4f7f\u7528\u52a8\u6001\u5185\u5b58\u5206\u914d\uff0c\u5982\u679c\u5bf9\u8c61\u9700\u8981\u8d85\u51fa\u4f5c\u7528\u57df\uff0c\u8fd9\u662f\u5f88\u6709\u7528\u7684\u3002 \u53e6\u4e00\u4e2a\u9009\u62e9\u662f\u4f7f\u7528 std::reference_wrapper \uff0c\u5b83\u662f\u4e00\u4e2a\u7c7b\uff0c\u53ef\u6a21\u62df\u51fa\u53ef\u8d4b\u503c\u7684\u5f15\u7528\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include // for std::reference_wrapper #include #include class Base { protected : int m_value {}; public : Base ( int value ) : m_value { value } { } virtual const char * getName () const { return \"Base\" ; } int getValue () const { return m_value ; } }; class Derived : public Base { public : Derived ( int value ) : Base { value } { } const char * getName () const override { return \"Derived\" ; } }; int main () { std :: vector < std :: reference_wrapper < Base >> v {}; // \u5b58\u653e Base \u7684\u53ef\u8d4b\u503c\u5f15\u7528\u7684\u5bb9\u5668 Base b { 5 }; // b and d can't be anonymous objects Derived d { 6 }; v . push_back ( b ); // add a Base object to our vector v . push_back ( d ); // add a Derived object to our vector // Print out all of the elements in our vector // we use .get() to get our element out of the std::reference_wrapper for ( const auto & element : v ) // element has type const std::reference_wrapper& std :: cout << \"I am a \" << element . get (). getName () << \" with value \" << element . get (). getValue () << '\\n' ; return 0 ; }","title":"vector \u5207\u7247"},{"location":"18-9-object-slicing/#frankenobject","text":"\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u770b\u5230\u4e86\u5bf9\u8c61\u5207\u7247\u53ef\u80fd\u5bfc\u81f4\u9519\u8bef\u7684\u60c5\u51b5\uff08\u7531\u4e8e\u6d3e\u751f\u7c7b\u88ab\u5207\u6389\u6240\u5f15\u8d77\uff09\u3002\u73b0\u5728\u8ba9\u6211\u4eec\u6765\u770b\u53e6\u4e00\u79cd\u5371\u9669\u7684\u60c5\u51b5\uff0c\u5176\u4e2d\u6d3e\u751f\u5bf9\u8c61\u4ecd\u7136\u5b58\u5728\uff01 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { Derived d1 { 5 }; Derived d2 { 6 }; Base & b { d2 }; b = d1 ; // \u95ee\u9898\u6240\u5728\uff08\u901a\u8fc7b\u7ed9d2\u8d4b\u503c\u4e3ad1\uff0c\u6ce8\u610f\u8fd9\u91cc\u4e0d\u662f\u7ed9\u5f15\u7528\u91cd\u65b0\u8d4b\u503c\uff09 return 0 ; } \u51fd\u6570\u7684\u524d\u4e09\u884c\u975e\u5e38\u7b80\u5355\u3002\u521b\u5efa\u4e24\u4e2a Derived \u5bf9\u8c61\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u7b2c\u4e8c\u4e2a\u5bf9\u8c61\u7684 Base \u7c7b\u578b\u5f15\u7528\u3002 \u7b2c\u56db\u884c\u662f\u51fa\u9519\u7684\u5730\u65b9\u3002\u56e0\u4e3a b \u6307\u5411 d2 \uff0c\u6211\u4eec\u628a d1 \u8d4b\u503c\u7ed9 b \uff08\u76f8\u5f53\u4e8e d2=d1 \uff09\uff0c\u4f60\u53ef\u80fd\u8ba4\u4e3a\u7ed3\u679c\u662f d1 \u4f1a\u88ab\u590d\u5236\u5230 d2 \u4e2d\u3002\u4f46\u662f b \u662f Base , \u800cC++\u4e3a\u7c7b\u63d0\u4f9b\u7684\u64cd\u4f5c\u7b26=\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4e0d\u662f\u865a\u51fd\u6570\u7684\u3002\u56e0\u6b64\uff0c\u53ea\u6709 d1 \u7684 Base \u90e8\u5206\u88ab\u590d\u5236\u5230 d2 \u4e2d\u3002 \u5c31\u662f d2 \u73b0\u5728\u6709 d1 \u7684\u57fa\u90e8\u5206\u548c d2 \u7684\u6d3e\u751f\u90e8\u5206\u3002\u5728\u8fd9\u4e2a\u7279\u5b9a\u7684\u4f8b\u5b50\u4e2d\uff0c\u8fd9\u4e0d\u662f\u95ee\u9898(\u56e0\u4e3a Derived \u7c7b\u672c\u8eab\u6ca1\u6709\u6570\u636e)\uff0c\u4f46\u662f\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f60\u5b9e\u9645\u521b\u5efa\u4e00\u4e2a \u7f1d\u5408\u602a\u5bf9\u8c61 \u2014\u2014\u7531\u591a\u4e2a\u5bf9\u8c61\u7684\u90e8\u5206\u7ec4\u6210\u3002\u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u6ca1\u6709\u4ec0\u4e48\u7b80\u5355\u7684\u65b9\u6cd5\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u7684\u53d1\u751f(\u53ea\u80fd\u5c3d\u91cf\u907f\u514d\u8fd9\u6837\u7684\u8d4b\u503c)\u3002","title":"\u7f1d\u5408\u602a\u5bf9\u8c61 Frankenobject"},{"location":"18-9-object-slicing/#_2","text":"\u5c3d\u7ba1C++\u652f\u6301\u901a\u8fc7\u5bf9\u8c61\u5207\u7247\u5c06\u6d3e\u751f\u5bf9\u8c61\u5206\u914d\u7ed9\u57fa\u5bf9\u8c61\uff0c\u4f46\u8fd9\u4e48\u505a\u591a\u6570\u60c5\u51b5\u4e0b\u4f1a\u5e26\u6765\u95ee\u9898\uff0c\u56e0\u6b64\u5e94\u8be5\u5c3d\u91cf\u907f\u514d\u5207\u7247\u3002 \u786e\u4fdd\u51fd\u6570\u5f62\u53c2\u662f\u5f15\u7528(\u6216\u6307\u9488)\uff0c\u5e76\u5728\u6d3e\u751f\u7c7b\u4e2d\u5c3d\u91cf\u907f\u514d\u4efb\u4f55\u5f62\u5f0f\u7684\u503c\u4f20\u9012\u3002","title":"\u7ed3\u8bba"},{"location":"18-x-Chapter-18-comprehensive-quiz/","tags":["summary"],"text":"\u590d\u4e60 18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 C++ \u5141\u8bb8\u4f60\u5c06\u57fa\u7c7b\u7c7b\u578b\u7684\u6307\u9488\u6216\u5f15\u7528\u6307\u5411\u6d3e\u751f\u7c7b\u7684\u5bf9\u8c61\u3002\u5f53\u6211\u4eec\u9700\u8981\u7f16\u5199\u4ee3\u7801\u4f7f\u5176\u80fd\u591f\u914d\u5408\u4ece\u540c\u4e00\u4e2a\u57fa\u7c7b\u7ee7\u627f\u7684\u6d3e\u751f\u7c7b\u5de5\u4f5c\u65f6\uff0c\u8fd9\u4e2a\u7279\u6027\u975e\u5e38\u6709\u7528\uff1b 18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001 \u5982\u679c\u6ca1\u6709\u865a\u51fd\u6570\u7684\u5e2e\u52a9\uff0c\u6307\u5411\u6d3e\u751f\u7c7b\u7684\u57fa\u7c7b\u6307\u9488\u548c\u5f15\u7528\u5c31\u53ea\u80fd\u8bbf\u95ee\u57fa\u7c7b\u7684\u6210\u5458\u53d8\u91cf\u548c\u57fa\u7c7b\u4e2d\u5bf9\u5e94\u7684\u51fd\u6570\uff1b \u865a\u51fd\u6570\u662f\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\u7684\u51fd\u6570\uff0c\u5b83\u4f1a\u89e3\u6790\u4e3a\u57fa\u7c7b\u548c\u6d3e\u751f\u7c7b\u4e2d\u5171\u540c\u5b58\u5728\u7684\u6700\u540e\u6d3e\u751f\u7684\u51fd\u6570\uff08\u79f0\u4e3a \u91cd\u8f7d \uff09\u3002\u4e3a\u4e86\u5b9e\u73b0\u91cd\u8f7d\u3002\u6d3e\u751f\u7c7b\u7684\u51fd\u6570\u548c\u57fa\u7c7b\u4e2d\u7684\u865a\u51fd\u6570\u5fc5\u987b\u5177\u6709\u76f8\u540c\u7684\u51fd\u6570\u7b7e\u540d\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u3002\u4e00\u4e2a\u4f8b\u5916\u7684\u60c5\u51b5\u662f \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b \uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u91cd\u8f7d\u51fd\u6570\u7684\u8fd4\u56de\u503c\u53ef\u4ee5\u8fd4\u56de\u6d3e\u751f\u7c7b\u7c7b\u578b\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u800c\u4e0d\u5fc5\u548c\u57fa\u7c7b\u4e2d\u7684\u51fd\u6570\u4e00\u6837\u4e5f\u8fd4\u56de\u57fa\u7c7b\u7c7b\u578b\u7684\u6307\u9488\u548c\u5f15\u7528\uff1b 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b \u5982\u679c\u4e00\u4e2a\u51fd\u6570\u7684\u76ee\u7684\u662f\u88ab\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\u91cd\u8f7d\uff0c\u6211\u4eec\u5e94\u8be5\u4f7f\u7528 override \u6765\u4fee\u9970\u5b83\u4ee5\u4fbf\u786e\u4fdd\u5b83\u88ab\u91cd\u8f7d\uff1b final \u5173\u952e\u5b57\u53ef\u4ee5\u7528\u6765\u6307\u660e\u67d0\u4e2a\u51fd\u6570\u4e0d\u53ef\u4ee5\u88ab\u91cd\u8f7d\u6216\u67d0\u4e2a\u7c7b\u4e0d\u80fd\u88ab\u7ee7\u627f\uff1b 18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316 \u5982\u679c\u4e00\u4e2a\u7c7b\u7684\u529f\u80fd\u662f\u4f5c\u4e3a\u57fa\u7c7b\u4f7f\u7528\uff0c\u90a3\u5e94\u8be5\u5c06\u5176\u6790\u6784\u51fd\u6570\u8bbe\u4e3a\u865a\u51fd\u6570\uff0c\u8fd9\u6837\u624d\u80fd\u786e\u4fdd\u4e00\u4e2a\u6307\u5411\u6d3e\u751f\u7c7b\u7684\u57fa\u7c7b\u7c7b\u578b\u7684\u6307\u9488\u88ab delete \u7684\u65f6\u5019\uff0c\u80fd\u591f\u8c03\u7528\u6b63\u786e\u7684\u6790\u6784\u51fd\u6570\uff08\u5b50\u7c7b\u7684\u6790\u6784\u51fd\u6570\uff09\uff1b \u4f7f\u7528 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u53ef\u4ee5\u89c4\u907f\u865a\u51fd\u6570\u89e3\u6790\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u663e\u5f0f\u5730\u6307\u5b9a\u4f60\u5e0c\u671b\u4f7f\u7528\u54ea\u4e2a\u7c7b\u4e2d\u7684\u51fd\u6570\uff1a\u4f8b\u5982\uff1a base.Base::getName() \uff1b 18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a \u5f53\u7f16\u8bd1\u5668\u89e3\u6790\u5230\u51fd\u6570\u7684\u76f4\u63a5\u8c03\u7528\u65f6\uff0c\u4f1a\u8fdb\u884c \u65e9\u671f\u7ed1\u5b9a \u3002\u7f16\u8bd1\u5668\u6216\u8005\u94fe\u63a5\u5668\u53ef\u4ee5\u76f4\u63a5\u89e3\u6790\u8be5\u51fd\u6570\u8c03\u7528\u3002 \u540e\u671f\u7ed1\u5b9a \u5219\u53d1\u751f\u5728\u901a\u8fc7\u51fd\u6570\u6307\u9488\u8fdb\u884c\u8c03\u7528\u65f6\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5b9e\u9645\u7684\u51fd\u6570\u8c03\u7528\u53ea\u6709\u5728\u8fd0\u884c\u65f6\u624d\u80fd\u786e\u5b9a\u3002\u865a\u51fd\u6570\u4f7f\u7528 \u540e\u671f\u7ed1\u5b9a \u548c\u865a\u51fd\u6570\u8868\u6765\u786e\u5b9a\u5b9e\u9645\u8c03\u7528\u7684\u51fd\u6570\u3002 18.6 - \u865a\u51fd\u6570\u8868 \u4f7f\u7528\u865a\u51fd\u6570\u662f\u6709\u5f00\u9500\u7684\uff1a\u865a\u51fd\u6570\u7684\u8c03\u7528\u66f4\u8017\u65f6\uff0c\u800c\u4e14\u865a\u51fd\u6570\u8868\u4e5f\u4f1a\u4f7f\u6bcf\u4e00\u4e2a\u5305\u542b\u865a\u51fd\u6570\u7684\u5bf9\u8c61\u5176\u5927\u5c0f\u589e\u52a0\u4e00\u4e2a\u6307\u9488\u7684\u5927\u5c0f\u3002 18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b \u865a\u51fd\u6570\u53ef\u4ee5\u88ab\u5b9a\u4e49\u4e3a \u7eaf\u865a\u51fd\u6570 \u6216\u62bd\u8c61\u51fd\u6570\uff0c\u53ea\u9700\u5728\u5176\u51fd\u6570\u539f\u578b\u7684\u7ed3\u5c3e\u6dfb\u52a0\u201d=0\u201c\u5373\u53ef\u3002\u5305\u542b\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\u79f0\u4e3a \u62bd\u8c61\u7c7b \uff0c\u5b83 \u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\u3002\u4e00\u4e2a\u7ee7\u627f\u4e86\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\uff0c\u5fc5\u987b\u5b9e\u73b0\u8fd9\u4e9b\u865a\u51fd\u6570\uff0c\u5426\u5219\u8be5\u7c7b\u4e5f\u662f\u62bd\u8c61\u7c7b\u3002\u7eaf\u865a\u51fd\u6570\u4e5f\u53ef\u4ee5\u6709\u51fd\u6570\u4f53\uff0c\u4f46\u5373\u4fbf\u8fd9\u6837\u5b83\u4ecd\u7136\u662f\u62bd\u8c61\u7684\u3002 \u63a5\u53e3\u7c7b \u662f\u90a3\u4e9b\u6ca1\u6709\u6210\u5458\u53d8\u91cf\uff0c\u4e14\u6240\u6709\u51fd\u6570\u5747\u4e3a \u7eaf\u865a\u51fd\u6570 \u7684\u7c7b\uff0c\u901a\u5e38\u5728\u547d\u540d\u5b83\u4eec\u7684\u65f6\u5019\u4f1a\u4f7f\u7528\u5927\u5199\u5b57\u6bcdI\u5f00\u5934\u3002 18.8 - \u865a\u57fa\u7c7b \u865a\u57fa\u7c7b \u662f\u4e00\u79cd\u57fa\u7c7b\uff0c\u4e0d\u8bba\u5b83\u88ab\u7ee7\u627f\u591a\u5c11\u6b21\uff0c\u5b83\u59cb\u7ec8\u53ea\u4f1a\u88ab\u6784\u9020\u4e00\u6b21\u3002 18.9 - \u5bf9\u8c61\u5207\u7247 \u5f53\u4e00\u4e2a\u6d3e\u751f\u7c7b\u88ab\u8d4b\u503c\u7ed9\u4e00\u4e2a\u57fa\u7c7b\u5bf9\u8c61\u65f6\uff0c\u57fa\u7c7b\u53ea\u4f1a\u5f97\u5230\u4e00\u4efd\u5305\u542b\u4e86\u6d3e\u751f\u7c7b\u4e2d\u57fa\u7c7b\u90e8\u5206\u7684\u62f7\u8d1d\u3002\u8fd9\u4e2a\u73b0\u8c61\u88ab\u79f0\u4e3a \u5bf9\u8c61\u5207\u7247 \u3002 \u6240\u4ee5\u51fd\u6570 \u6309\u503c\u4f20\u9012 \u6d3e\u751f\u5bf9\u8c61\u65f6\u4f1a\u51fa\u73b0\u5207\u7247\uff0c\u5bfc\u81f4\u6d3e\u751f\u7c7b\u865a\u51fd\u6570\u6ca1\u529e\u6cd5\u88ab\u5b9e\u9645\u8c03\u7528 18.10 - \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u53ef\u4ee5\u7528\u6765\u5c06\u4e00\u4e2a\u6307\u5411\u57fa\u7c7b\u5bf9\u8c61\u7684\u6307\u9488\u8f6c\u6362\u4e3a\u6307\u5411\u6d3e\u751f\u7c7b\u5bf9\u8c61\u7684\u6307\u9488\uff0c\u5373 downcasting \u3002\u5982\u679c\u8f6c\u6362\u5931\u8d25\uff0c\u5219\u4f1a\u8fd4\u56de\u4e00\u4e2a\u7a7a\u6307\u9488\u3002 18.11 - \u4f7f\u7528<<\u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b \u4e3a\u6d3e\u751f\u7c7b\u91cd\u8f7d << \u8fd0\u7b97\u7b26\u7684\u6700\u7b80\u5355\u7684\u529e\u6cd5\uff0c\u662f\u91cd\u8f7d\u6700\u57fa\u7840\u7684\u57fa\u7c7b\u7684 << \u8fd0\u7b97\u7b26\uff0c\u7136\u540e\u8c03\u7528\u865a\u6210\u5458\u51fd\u6570\u6765\u8fdb\u884c\u6253\u5370\u3002","title":"18.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u865a\u51fd\u6570"},{"location":"18-x-Chapter-18-comprehensive-quiz/#_1","text":"18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 C++ \u5141\u8bb8\u4f60\u5c06\u57fa\u7c7b\u7c7b\u578b\u7684\u6307\u9488\u6216\u5f15\u7528\u6307\u5411\u6d3e\u751f\u7c7b\u7684\u5bf9\u8c61\u3002\u5f53\u6211\u4eec\u9700\u8981\u7f16\u5199\u4ee3\u7801\u4f7f\u5176\u80fd\u591f\u914d\u5408\u4ece\u540c\u4e00\u4e2a\u57fa\u7c7b\u7ee7\u627f\u7684\u6d3e\u751f\u7c7b\u5de5\u4f5c\u65f6\uff0c\u8fd9\u4e2a\u7279\u6027\u975e\u5e38\u6709\u7528\uff1b 18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001 \u5982\u679c\u6ca1\u6709\u865a\u51fd\u6570\u7684\u5e2e\u52a9\uff0c\u6307\u5411\u6d3e\u751f\u7c7b\u7684\u57fa\u7c7b\u6307\u9488\u548c\u5f15\u7528\u5c31\u53ea\u80fd\u8bbf\u95ee\u57fa\u7c7b\u7684\u6210\u5458\u53d8\u91cf\u548c\u57fa\u7c7b\u4e2d\u5bf9\u5e94\u7684\u51fd\u6570\uff1b \u865a\u51fd\u6570\u662f\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\u7684\u51fd\u6570\uff0c\u5b83\u4f1a\u89e3\u6790\u4e3a\u57fa\u7c7b\u548c\u6d3e\u751f\u7c7b\u4e2d\u5171\u540c\u5b58\u5728\u7684\u6700\u540e\u6d3e\u751f\u7684\u51fd\u6570\uff08\u79f0\u4e3a \u91cd\u8f7d \uff09\u3002\u4e3a\u4e86\u5b9e\u73b0\u91cd\u8f7d\u3002\u6d3e\u751f\u7c7b\u7684\u51fd\u6570\u548c\u57fa\u7c7b\u4e2d\u7684\u865a\u51fd\u6570\u5fc5\u987b\u5177\u6709\u76f8\u540c\u7684\u51fd\u6570\u7b7e\u540d\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u3002\u4e00\u4e2a\u4f8b\u5916\u7684\u60c5\u51b5\u662f \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b \uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u91cd\u8f7d\u51fd\u6570\u7684\u8fd4\u56de\u503c\u53ef\u4ee5\u8fd4\u56de\u6d3e\u751f\u7c7b\u7c7b\u578b\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u800c\u4e0d\u5fc5\u548c\u57fa\u7c7b\u4e2d\u7684\u51fd\u6570\u4e00\u6837\u4e5f\u8fd4\u56de\u57fa\u7c7b\u7c7b\u578b\u7684\u6307\u9488\u548c\u5f15\u7528\uff1b 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b \u5982\u679c\u4e00\u4e2a\u51fd\u6570\u7684\u76ee\u7684\u662f\u88ab\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\u91cd\u8f7d\uff0c\u6211\u4eec\u5e94\u8be5\u4f7f\u7528 override \u6765\u4fee\u9970\u5b83\u4ee5\u4fbf\u786e\u4fdd\u5b83\u88ab\u91cd\u8f7d\uff1b final \u5173\u952e\u5b57\u53ef\u4ee5\u7528\u6765\u6307\u660e\u67d0\u4e2a\u51fd\u6570\u4e0d\u53ef\u4ee5\u88ab\u91cd\u8f7d\u6216\u67d0\u4e2a\u7c7b\u4e0d\u80fd\u88ab\u7ee7\u627f\uff1b 18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316 \u5982\u679c\u4e00\u4e2a\u7c7b\u7684\u529f\u80fd\u662f\u4f5c\u4e3a\u57fa\u7c7b\u4f7f\u7528\uff0c\u90a3\u5e94\u8be5\u5c06\u5176\u6790\u6784\u51fd\u6570\u8bbe\u4e3a\u865a\u51fd\u6570\uff0c\u8fd9\u6837\u624d\u80fd\u786e\u4fdd\u4e00\u4e2a\u6307\u5411\u6d3e\u751f\u7c7b\u7684\u57fa\u7c7b\u7c7b\u578b\u7684\u6307\u9488\u88ab delete \u7684\u65f6\u5019\uff0c\u80fd\u591f\u8c03\u7528\u6b63\u786e\u7684\u6790\u6784\u51fd\u6570\uff08\u5b50\u7c7b\u7684\u6790\u6784\u51fd\u6570\uff09\uff1b \u4f7f\u7528 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u53ef\u4ee5\u89c4\u907f\u865a\u51fd\u6570\u89e3\u6790\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u663e\u5f0f\u5730\u6307\u5b9a\u4f60\u5e0c\u671b\u4f7f\u7528\u54ea\u4e2a\u7c7b\u4e2d\u7684\u51fd\u6570\uff1a\u4f8b\u5982\uff1a base.Base::getName() \uff1b 18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a \u5f53\u7f16\u8bd1\u5668\u89e3\u6790\u5230\u51fd\u6570\u7684\u76f4\u63a5\u8c03\u7528\u65f6\uff0c\u4f1a\u8fdb\u884c \u65e9\u671f\u7ed1\u5b9a \u3002\u7f16\u8bd1\u5668\u6216\u8005\u94fe\u63a5\u5668\u53ef\u4ee5\u76f4\u63a5\u89e3\u6790\u8be5\u51fd\u6570\u8c03\u7528\u3002 \u540e\u671f\u7ed1\u5b9a \u5219\u53d1\u751f\u5728\u901a\u8fc7\u51fd\u6570\u6307\u9488\u8fdb\u884c\u8c03\u7528\u65f6\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5b9e\u9645\u7684\u51fd\u6570\u8c03\u7528\u53ea\u6709\u5728\u8fd0\u884c\u65f6\u624d\u80fd\u786e\u5b9a\u3002\u865a\u51fd\u6570\u4f7f\u7528 \u540e\u671f\u7ed1\u5b9a \u548c\u865a\u51fd\u6570\u8868\u6765\u786e\u5b9a\u5b9e\u9645\u8c03\u7528\u7684\u51fd\u6570\u3002 18.6 - \u865a\u51fd\u6570\u8868 \u4f7f\u7528\u865a\u51fd\u6570\u662f\u6709\u5f00\u9500\u7684\uff1a\u865a\u51fd\u6570\u7684\u8c03\u7528\u66f4\u8017\u65f6\uff0c\u800c\u4e14\u865a\u51fd\u6570\u8868\u4e5f\u4f1a\u4f7f\u6bcf\u4e00\u4e2a\u5305\u542b\u865a\u51fd\u6570\u7684\u5bf9\u8c61\u5176\u5927\u5c0f\u589e\u52a0\u4e00\u4e2a\u6307\u9488\u7684\u5927\u5c0f\u3002 18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b \u865a\u51fd\u6570\u53ef\u4ee5\u88ab\u5b9a\u4e49\u4e3a \u7eaf\u865a\u51fd\u6570 \u6216\u62bd\u8c61\u51fd\u6570\uff0c\u53ea\u9700\u5728\u5176\u51fd\u6570\u539f\u578b\u7684\u7ed3\u5c3e\u6dfb\u52a0\u201d=0\u201c\u5373\u53ef\u3002\u5305\u542b\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\u79f0\u4e3a \u62bd\u8c61\u7c7b \uff0c\u5b83 \u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\u3002\u4e00\u4e2a\u7ee7\u627f\u4e86\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\uff0c\u5fc5\u987b\u5b9e\u73b0\u8fd9\u4e9b\u865a\u51fd\u6570\uff0c\u5426\u5219\u8be5\u7c7b\u4e5f\u662f\u62bd\u8c61\u7c7b\u3002\u7eaf\u865a\u51fd\u6570\u4e5f\u53ef\u4ee5\u6709\u51fd\u6570\u4f53\uff0c\u4f46\u5373\u4fbf\u8fd9\u6837\u5b83\u4ecd\u7136\u662f\u62bd\u8c61\u7684\u3002 \u63a5\u53e3\u7c7b \u662f\u90a3\u4e9b\u6ca1\u6709\u6210\u5458\u53d8\u91cf\uff0c\u4e14\u6240\u6709\u51fd\u6570\u5747\u4e3a \u7eaf\u865a\u51fd\u6570 \u7684\u7c7b\uff0c\u901a\u5e38\u5728\u547d\u540d\u5b83\u4eec\u7684\u65f6\u5019\u4f1a\u4f7f\u7528\u5927\u5199\u5b57\u6bcdI\u5f00\u5934\u3002 18.8 - \u865a\u57fa\u7c7b \u865a\u57fa\u7c7b \u662f\u4e00\u79cd\u57fa\u7c7b\uff0c\u4e0d\u8bba\u5b83\u88ab\u7ee7\u627f\u591a\u5c11\u6b21\uff0c\u5b83\u59cb\u7ec8\u53ea\u4f1a\u88ab\u6784\u9020\u4e00\u6b21\u3002 18.9 - \u5bf9\u8c61\u5207\u7247 \u5f53\u4e00\u4e2a\u6d3e\u751f\u7c7b\u88ab\u8d4b\u503c\u7ed9\u4e00\u4e2a\u57fa\u7c7b\u5bf9\u8c61\u65f6\uff0c\u57fa\u7c7b\u53ea\u4f1a\u5f97\u5230\u4e00\u4efd\u5305\u542b\u4e86\u6d3e\u751f\u7c7b\u4e2d\u57fa\u7c7b\u90e8\u5206\u7684\u62f7\u8d1d\u3002\u8fd9\u4e2a\u73b0\u8c61\u88ab\u79f0\u4e3a \u5bf9\u8c61\u5207\u7247 \u3002 \u6240\u4ee5\u51fd\u6570 \u6309\u503c\u4f20\u9012 \u6d3e\u751f\u5bf9\u8c61\u65f6\u4f1a\u51fa\u73b0\u5207\u7247\uff0c\u5bfc\u81f4\u6d3e\u751f\u7c7b\u865a\u51fd\u6570\u6ca1\u529e\u6cd5\u88ab\u5b9e\u9645\u8c03\u7528 18.10 - \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u53ef\u4ee5\u7528\u6765\u5c06\u4e00\u4e2a\u6307\u5411\u57fa\u7c7b\u5bf9\u8c61\u7684\u6307\u9488\u8f6c\u6362\u4e3a\u6307\u5411\u6d3e\u751f\u7c7b\u5bf9\u8c61\u7684\u6307\u9488\uff0c\u5373 downcasting \u3002\u5982\u679c\u8f6c\u6362\u5931\u8d25\uff0c\u5219\u4f1a\u8fd4\u56de\u4e00\u4e2a\u7a7a\u6307\u9488\u3002 18.11 - \u4f7f\u7528<<\u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b \u4e3a\u6d3e\u751f\u7c7b\u91cd\u8f7d << \u8fd0\u7b97\u7b26\u7684\u6700\u7b80\u5355\u7684\u529e\u6cd5\uff0c\u662f\u91cd\u8f7d\u6700\u57fa\u7840\u7684\u57fa\u7c7b\u7684 << \u8fd0\u7b97\u7b26\uff0c\u7136\u540e\u8c03\u7528\u865a\u6210\u5458\u51fd\u6570\u6765\u8fdb\u884c\u6253\u5370\u3002","title":"\u590d\u4e60"},{"location":"19-x-Chapter-19-comprehensive-quiz/","tags":["summary"],"text":"\u4f7f\u7528[[template|\u6a21\u677f]]\u53ef\u4ee5\u57fa\u4e8e \u5360\u4f4d\u7b26\u7c7b\u578b \u6765\u5b9a\u4e49\u51fd\u6570\u6216\u8005\u7c7b\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5b9a\u4e49\u51fa\u914d\u5408\u4e0d\u540c\u7c7b\u578b\u5de5\u4f5c\uff0c\u4f46\u662f\u7ed3\u6784\u5b8c\u5168\u4e00\u81f4\u7684\u51fd\u6570\u6216\u8005\u7c7b\u3002\u88ab\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u6216\u8005\u7c7b\uff0c\u79f0\u4e3a \u51fd\u6570\u5b9e\u4f8b \u6216 \u7c7b\u5b9e\u4f8b \u6240\u6709\u7684\u6a21\u677f\u51fd\u6570\u6216\u6a21\u677f\u7c7b\uff0c\u90fd\u5fc5\u987b\u4ee5[[template-parameter-declaration|\u6a21\u677f\u53c2\u6570\u58f0\u660e]]\u5f00\u5934\u3002\u5728\u6a21\u677f\u53c2\u6570\u58f0\u660e\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u6307\u5b9a\u6a21\u677f\u7684\u7c7b\u578b\u53c2\u6570\u6216\u8868\u8fbe\u5f0f\u53c2\u6570\u3002 \u6a21\u677f\u7c7b\u578b\u53c2\u6570 \u5176\u5b9e\u5c31\u662f \u5360\u4f4d\u7b26\u7c7b\u578b \uff0c\u901a\u5e38\u4f1a\u5199\u4f5c T \uff0c T1 \uff0c T2 \u6216\u8005\u5176\u4ed6\u5355\u72ec\u5b57\u6bcd\uff08\u4f8b\u5982 S \uff09\u3002 \u8868\u8fbe\u5f0f\u53c2\u6570 \u901a\u5e38\u662f\u6574\u578b\u7c7b\u578b\uff0c\u4f46\u662f\u4e5f\u53ef\u4ee5\u662f\u6307\u9488\u6216\u8005\u5f15\u7528\u7c7b\u578b\uff08\u6307\u5411\u67d0\u4e2a\u51fd\u6570\uff0c\u5bf9\u8c61\u6216\u6210\u5458\u51fd\u6570\uff09\u3002 \u6a21\u677f\u7c7b\u53ca\u5176\u6210\u5458\u51fd\u6570\u5b9a\u4e49\u5e76\u4e0d\u80fd\u50cf\u5176\u4ed6\u4e00\u822c\u7c7b\u90a3\u6837\u8fdb\u884c\u5206\u79bb\u2014\u2014\u4f60\u4e0d\u80fd\u5c06\u7c7b\u7684\u5b9a\u4e49\u653e\u5728\u5934\u6587\u4ef6\u4e2d\u7136\u540e\u5c06\u6210\u5458\u51fd\u6570\u7684\u5b9a\u4e49\u653e\u5728 .cpp \u6587\u4ef6\u4e2d\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6700\u597d\u5c06\u5b83\u4eec\u90fd\u5b9a\u4e49\u5728\u4e00\u4e2a\u5934\u6587\u4ef6\u4e2d\uff0c\u6a21\u677f\u7c7b\u7684\u5b9a\u4e49\u5728\u524d\uff0c\u5176\u6210\u5458\u51fd\u6570\u7684\u5b9a\u4e49\u5728\u540e\u3002 \u6a21\u677f\u7279\u5316 can be used when we want to override the default behavior from the templated function or class for a specific type. If all types are overridden, this is called full specialization. Classes also support partial specialization, where only some of the templated parameters are specialized. Functions can not be partially specialized. C++\u6807\u51c6\u5e93\u4e2d\u7684\u5f88\u591a\u7c7b\u90fd\u4f7f\u7528\u4e86\u6a21\u677f\uff0c\u5305\u62ec std::array \u548c std::vector \u3002\u6a21\u677f\u7ecf\u5e38\u88ab\u7528\u6765\u5b9e\u73b0\u5bb9\u5668\u7c7b\uff0c\u8fd9\u6837\u7684\u5bb9\u5668\u7c7b\u5c31\u53ef\u4ee5\u914d\u5408\u4efb\u610f\u7c7b\u578b\u7684\u5bf9\u8c61\u6765\u4f7f\u7528\u3002","title":"19.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6a21\u677f\u548c\u7c7b"},{"location":"2-1-Introduction-to-functions/","tags":["function"],"text":"Key Takeaway \u5728\u4e0a\u4e00\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u51fd\u6570\u5b9a\u4e49\u4e3a\u4e00\u7ec4\u987a\u5e8f\u6267\u884c\u6307\u4ee4\u7684\u5408\u96c6\u3002\u8fd9\u4e2a\u5b9a\u4e49\u56fa\u7136\u6ca1\u9519\uff0c\u4f46\u662f\u5b83\u5e76\u6ca1\u6709\u8868\u8fbe\u51fa\u51fd\u6570\u7684\u91cd\u8981\u6027\u3002 \u8fd9\u91cc\u8ba9\u6211\u4eec\u91cd\u65b0\u5b9a\u4e49\u51fd\u6570\uff1a\u51fd\u6570\u662f\u4e00\u7ec4\u4e3a\u4e86\u5b8c\u6210\u67d0\u9879\u4efb\u52a1\u800c\u521b\u5efa\u7684\u53ef\u590d\u7528\u7684\u987a\u5e8f\u6307\u4ee4\u96c6\u5408\u3002 \u4f60\u5df2\u7ecf\u77e5\u9053\u4e86\uff0c\u4efb\u4f55\u90fd\u5fc5\u987b\u5305\u542b\u4e00\u4e2a\u540d\u4e3a main \u7684\u51fd\u6570\uff08\u7a0b\u5e8f\u5f00\u59cb\u6267\u884c\u7684\u5730\u65b9\uff09\u3002\u7136\u800c\uff0c\u5982\u679c\u5c06\u7a0b\u5e8f\u7684\u6240\u6709\u4ee3\u7801\u90fd\u585e\u8fdb main \u51fd\u6570\u4e2d\uff0c\u90a3\u4e48\u5b83\u65e0\u7591\u4f1a\u53d8\u5f97\u8d8a\u6765\u8d8a\u957f\uff0c\u4e5f\u8d8a\u6765\u8d8a\u96be\u4ee5\u7ef4\u62a4\u3002\u800c\u51fd\u6570\u5219\u63d0\u4f9b\u4e86\u4e00\u79cd\u5c06\u7a0b\u5e8f\u5206\u5272\u6210\u7b80\u77ed\u7684\u3001\u6a21\u5757\u5316\u7684\u4ee3\u7801\u6bb5\u7684\u65b9\u5f0f\u3002\u8fd9\u79cd\u65b9\u5f0f\u7684\u4ee3\u7801\u66f4\u52a0\u5bb9\u6613\u7ba1\u7406\u3001\u5bb9\u6613\u6d4b\u8bd5\uff0c\u4e5f\u66f4\u5bb9\u6613\u4f7f\u7528\u3002C++ \u6807\u51c6\u5e93\u4e2d\u5305\u542b\u4e86\u975e\u5e38\u591a\u7684\u5df2\u7ecf\u7f16\u5199\u597d\u7684\u51fd\u6570\u53ef\u4f9b\u4f60\u4f7f\u7528\u3002\u4e0d\u8fc7\uff0c\u4f7f\u7528\u81ea\u5df1\u7f16\u5199\u7684\u51fd\u6570\u4e5f\u662f\u975e\u5e38\u5e38\u89c1\u7684\u60c5\u5f62\uff0c\u8fd9\u4e9b\u51fd\u6570\u79f0\u4e3a\u7528\u6237\u81ea\u5b9a\u4e49\u51fd\u6570\u3002 \u6211\u4eec\u53ef\u4ee5\u8003\u8651\u8fd9\u6837\u4e00\u79cd\u751f\u6d3b\u4e2d\u7684\u573a\u666f\uff1a\u5f53\u4f60\u5728\u9605\u8bfb\u7684\u65f6\u5019\uff0c\u7a81\u7136\u60f3\u5230\u8981\u53bb\u6253\u4e00\u4e2a\u7535\u8bdd\u3002\u6b64\u65f6\u4f60\u4f1a\u5c06\u4e66\u7b7e\u5939\u5728\u4e66\u91cc\uff0c\u7136\u540e\u53bb\u6253\u7535\u8bdd\uff0c\u7136\u540e\u5728\u56de\u5230\u5939\u4e86\u4e66\u7b7e\u7684\u5730\u65b9\u7ee7\u7eed\u9605\u8bfb\u3002 C++ \u51fd\u6570\u4e5f\u53ef\u4ee5\u901a\u8fc7\u7c7b\u4f3c\u7684\u65b9\u6cd5\u53bb\u5de5\u4f5c\u3002\u5f53\u9047\u5230\u51fd\u6570\u8c03\u7528\u7684\u65f6\u5019\uff0c\u7a0b\u5e8f\u4f1a\u8fdb\u5165\u51fd\u6570\u5e76\u987a\u5e8f\u5730\u6267\u884c\u5176\u4e2d\u5404\u4e2a\u8bed\u53e5\u3002\u800c\u6240\u8c13\u51fd\u6570\u8c03\u7528\uff0c\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u5b83\u53ef\u4ee5\u544a\u8bc9 CPU \u4e2d\u65ad\u5f53\u524d\u51fd\u6570\u7684\u6267\u884c\u800c\u8f6c\u53bb\u6267\u884c\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\u3002\u6b64\u65f6 CPU \u5c31\u4f1a\u5728\u5f53\u524d\u7684\u6267\u884c\u70b9\u201d\u63d2\u5165\u4e00\u4e2a\u4e66\u7b7e\u201c\uff0c\u7136\u540e\u53bb\u6267\u884c\u53e6\u5916\u7684\u51fd\u6570\u3002\u5f53\u8be5\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\uff0cCPU \u5c31\u4f1a\u8fd4\u56de\u4e4b\u524d\u4e2d\u65ad\u7684\u70b9\u7ee7\u7eed\u6267\u884c\u3002 \u5b9e\u65bd\u51fd\u6570\u8c03\u7528\u7684\u51fd\u6570\uff0c\u79f0\u4e3a \u4e3b\u8c03\u51fd\u6570(caller) \uff0c\u800c\u88ab\u5b83\u8c03\u7528\u7684\u51fd\u6570\u79f0\u4e3a \u88ab\u8c03\u51fd\u6570(callee \u6216 called function) \u3002 \u81ea\u5b9a\u5b9a\u4e49\u51fd\u6570\u6848\u4f8b \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u719f\u6089\u4e00\u4e0b\u5b9a\u4e49\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u3002\u5728\u63a5\u4e0b\u6765\u7684\u51e0\u8282\u8bfe\u4e2d\uff0c\u7528\u6237\u5b9a\u4e49\u51fd\u6570\u90fd\u662f\u5982\u4e0b\u5f62\u5f0f\uff1a 1 2 3 4 return-type identifier() // identifier replaced with the name of your function { // Your code here } \u7b80\u5355\u4ecb\u7ecd\u4e00\u4e0b\u5176\u4e2d\u56db\u4e2a\u4e3b\u8981\u5143\u7d20\uff1a \u5728\u672c\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528 int \uff08\u5bf9\u4e8e main() \u51fd\u6570\uff09\u6216 void \uff08\u5bf9\u4e8e\u5176\u4ed6\u51fd\u6570\uff09\u4f5c\u4e3a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\uff08 return-type \uff09\u3002\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\uff08 2.2 - \u51fd\u6570\u8fd4\u56de\u503c \uff09\u4e2d\u4ecb\u7ecd\u66f4\u591a\u7684\u8fd4\u56de\u503c\u7c7b\u578b\uff1b \u548c\u53d8\u91cf\u4e00\u6837\uff0c\u51fd\u6570\u4e5f\u6709\u51fd\u6570\u540d\u3002 identifier \u5c31\u662f\u4f60\u5b9a\u4e49\u7684\u51fd\u6570\u7684\u51fd\u6570\u540d\uff1b \u6807\u8bc6\u7b26\u540e\u9762\u7684\u62ec\u53f7\u544a\u8bc9\u7f16\u8bd1\u5668\u8fd9\u91cc\u662f\u5728\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff1b \u82b1\u62ec\u53f7\u4ee5\u53ca\u82b1\u62ec\u53f7\u5185\u90e8\u7684\u8bed\u53e5\u79f0\u4e3a \u51fd\u6570\u4f53 \u3002\u8fd9\u91cc\u5c31\u662f\u653e\u7f6e\u51fd\u6570\u8bed\u53e5\u7684\u7684\u5730\u65b9\u3002 \u4e0b\u9762\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5b9a\u4e49\u5e76\u4f7f\u7528\u4e00\u4e2a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // for std::cout // \u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a doPrint() \u7684\u51fd\u6570 void doPrint () // doPrint() \u5728\u672c\u4f8b\u4e2d\u662f\u88ab\u8c03\u51fd\u6570 { std :: cout << \"In doPrint() \\n \" ; } // Definition of function main() int main () { std :: cout << \"Starting main() \\n \" ; doPrint (); // \u4e2d\u65ad main() \u51fd\u6570\u5e76\u8c03\u7528 doPrint()\u3002main() \u51fd\u6570\u662f\u4e3b\u8c03\u51fd\u6570 std :: cout << \"Ending main() \\n \" ; // \u5728 doPrint() \u7ed3\u675f\u540e\u6267\u884c\u8be5\u6307\u4ee4 return 0 ; } \u4e0a\u8ff0\u4ee3\u7801\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 Starting main() In doPrint() Ending main() \u8be5\u7a0b\u5e8f\u4ece main \u51fd\u6570\u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\uff0c\u7b2c\u4e00\u884c\u4f1a\u6253\u5370 Starting main() . main \u51fd\u6570\u7684\u7b2c\u4e8c\u884c\u4f1a\u8c03\u7528 doPrint \u3002\u901a\u8fc7\u5728\u51fd\u6570\u540d\u540e\u9762\u52a0\u4e0a\u62ec\u53f7\u5c31\u53ef\u4ee5\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u4f8b\u5982 doPrint() \u3002\u6ce8\u610f\uff0c\u5982\u679c\u4f60\u5fd8\u8bb0\u4e86\u62ec\u53f7\uff0c\u7a0b\u5e8f\u53ef\u80fd\u65e0\u6cd5\u7f16\u8bd1\uff08\u5982\u679c\u7a0b\u5e8f\u80fd\u591f\u7f16\u8bd1\uff0c\u51fd\u6570\u4e5f\u4e0d\u4f1a\u88ab\u8c03\u7528\uff09\u3002 \u6ce8\u610f \u5728\u8c03\u7528\u51fd\u6570\u7684\u65f6\u5019\uff0c\u4e0d\u8981\u5fd8\u8bb0\u52a0\u4e0a\u62ec\u53f7\u3002 \u56e0\u4e3a\u6267\u884c\u4e86\u51fd\u6570\u8c03\u7528\uff0c main \u51fd\u6570\u4e2d\u8bed\u53e5\u7684\u6267\u884c\u88ab\u6682\u505c\u4e86\uff0c\u6b64\u65f6\u4f1a\u8df3\u8f6c\u5230\u88ab\u8c03\u51fd\u6570 doPrint \u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\u3002 doPrint \u51fd\u6570\u7684\u7b2c\u4e00\u884c\uff08\u4e5f\u662f\u552f\u4e00\u4e00\u884c\uff09\u4f1a\u6253\u5370 In doPrint() \u3002\u5f53\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u7a0b\u5e8f\u4f1a\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570( main \u51fd\u6570)\uff0c\u4ece\u4e4b\u524d\u79bb\u5f00\u7684\u4f4d\u7f6e\u7ee7\u7eed\u6267\u884c\u3002\u56e0\u6b64\uff0c\u4e0b\u4e00\u6761\u8981\u6267\u884c\u7684\u8bed\u53e5\u662f\u6253\u5370 Ending main() \u3002 \u591a\u6b21\u8c03\u7528\u51fd\u6570 \u51fd\u6570\u6700\u6709\u7528\u7684\u529f\u80fd\u83ab\u8fc7\u4e8e\u5b83\u53ef\u4ee5\u88ab\u591a\u6b21\u8c03\u7528\u3002\u4e0b\u9762\u4f8b\u5b50\u5c55\u793a\u4e86\u591a\u6b21\u8c03\u7528\u51fd\u6570\u7684\u573a\u666f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // for std::cout void doPrint () { std :: cout << \"In doPrint() \\n \" ; } // Definition of function main() int main () { std :: cout << \"Starting main() \\n \" ; doPrint (); // doPrint() called for the first time doPrint (); // doPrint() called for the second time std :: cout << \"Ending main() \\n \" ; return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 Starting main() In doPrint() In doPrint() Ending main() \u56e0\u4e3a doPrint \u5728 main \u51fd\u6570\u4e2d\u88ab\u8c03\u7528\u4e86\u4e24\u6b21\uff0c\u56e0\u6b64\u5b83\u4e5f\u4f1a\u6267\u884c\u4e24\u6b21\uff0c\u56e0\u6b64 In doPrint() \u4f1a\u6253\u5370\u4e24\u6b21\uff08\u6bcf\u6b21\u8c03\u7528\u6253\u5370\u4e00\u6b21\uff09\u3002 \u51fd\u6570\u94fe\u5f0f\u8c03\u7528 \u4f60\u5df2\u7ecf\u770b\u5230\u4e86\uff0c main \u51fd\u6570\u53ef\u4ee5\u8c03\u7528\u5176\u4ed6\u51fd\u6570\uff08\u4f8b\u5982 doPrint \uff09\u3002\u4efb\u4f55\u51fd\u6570\u90fd\u53ef\u4ee5\u51fd\u6570\u3002\u5728\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c main \u51fd\u6570\u4f1a\u8c03\u7528\u51fd\u6570 doA \uff0c doA \u51fd\u6570\u53c8\u4f1a\u8c03\u7528 doB \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include // for std::cout void doB () { std :: cout << \"In doB() \\n \" ; } void doA () { std :: cout << \"Starting doA() \\n \" ; doB (); std :: cout << \"Ending doA() \\n \" ; } // Definition of function main() int main () { std :: cout << \"Starting main() \\n \" ; doA (); std :: cout << \"Ending main() \\n \" ; return 0 ; } \u4e0a\u8ff0\u4ee3\u7801\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 Starting main() Starting doA() In doB() Ending doA() Ending main() C++\u4e0d\u652f\u6301\u51fd\u6570\u5d4c\u5957 \u548c\u5176\u4ed6\u8bed\u8a00\u4e0d\u540c\uff0cC++\u5e76\u4e0d\u652f\u6301\u5728\u51fd\u6570\u4e2d\u5b9a\u4e49\u5176\u4ed6\u51fd\u6570\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u4e0d\u5408\u6cd5\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { void foo () // \u9519\u8bef\uff1a\u8be5\u51fd\u6570\u5b9a\u4e49\u4f4d\u4e8e\u5176\u4ed6\u51fd\u6570\u5185\u90e8\u3002 { std :: cout << \"foo! \\n \" ; } foo (); // function call to foo() return 0 ; } \u6b63\u786e\u7684\u65b9\u5f0f\u5e94\u8be5\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include void foo () // no longer inside of main() { std :: cout << \"foo! \\n \" ; } int main () { foo (); return 0 ; } \u9898\u5916\u8bdd \u201cfoo\u201d \u662f\u4e00\u4e2a\u6ca1\u6709\u610f\u4e49\u7684\u5355\u8bcd\uff0c\u5728\u5c55\u793a\u67d0\u4e9b\u6982\u5ff5\u7684\u65f6\u5019\uff0c\u5982\u679c\u540d\u79f0\u4e0d\u91cd\u8981\uff0c\u90a3\u4e48\u5e38\u4f1a\u4f7f\u7528\u5b83\u6765\u4f5c\u4e3a\u5360\u4f4d\u7b26\u3002\u6b64\u7c7b\u5355\u8bcd\u6709\u4e2a\u4e13\u95e8\u7684\u540d\u5b57\u53eb\u505a\uff1a\u4f2a\u53d8\u91cf/ metasyntactic variables (\u4e0d\u8fc7\uff0c\u5728\u65e5\u5e38\u7528\u8bed\u4e2d\u5927\u5bb6\u8fd8\u662f\u559c\u6b22\u79f0\u5176\u4e3a\u201d\u540d\u79f0\u5360\u4f4d\u7b26\u201c\uff0c\u56e0\u4e3a metasyntactic variables\u8fd9\u4e2a\u5355\u8bcd\u771f\u7684\u5f88\u751f\u50fb)\u3002 \u5176\u4ed6C++\u4e2d\u5e38\u89c1\u7684\u4f2a\u53d8\u91cf\u8fd8\u5305\u62ec \u201cbar\u201d\u3001\u201cbaz\u201d\u4ee5\u53ca\u5176\u4ed6\u4ee5\u201coo\u201d\u7ed3\u5c3e\u7684\u4e09\u5b57\u6bcd\u5355\u8bcd\uff0c\u4f8b\u5982\u201cgoo\u201d\u3001\u201cmoo\u201d\u548c\u201cboo\u201d\u3002 \u5982\u679c\u4f60\u5bf9\u5176\u8bcd\u6e90\u611f\u5174\u8da3\uff0c\u53ef\u4ee5\u53bb\u9605\u8bfb RFC 3092 \u3002","title":"2.1 - \u51fd\u6570\u7b80\u4ecb"},{"location":"2-1-Introduction-to-functions/#_1","text":"\u9996\u5148\uff0c\u8ba9\u6211\u4eec\u719f\u6089\u4e00\u4e0b\u5b9a\u4e49\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u3002\u5728\u63a5\u4e0b\u6765\u7684\u51e0\u8282\u8bfe\u4e2d\uff0c\u7528\u6237\u5b9a\u4e49\u51fd\u6570\u90fd\u662f\u5982\u4e0b\u5f62\u5f0f\uff1a 1 2 3 4 return-type identifier() // identifier replaced with the name of your function { // Your code here } \u7b80\u5355\u4ecb\u7ecd\u4e00\u4e0b\u5176\u4e2d\u56db\u4e2a\u4e3b\u8981\u5143\u7d20\uff1a \u5728\u672c\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528 int \uff08\u5bf9\u4e8e main() \u51fd\u6570\uff09\u6216 void \uff08\u5bf9\u4e8e\u5176\u4ed6\u51fd\u6570\uff09\u4f5c\u4e3a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\uff08 return-type \uff09\u3002\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\uff08 2.2 - \u51fd\u6570\u8fd4\u56de\u503c \uff09\u4e2d\u4ecb\u7ecd\u66f4\u591a\u7684\u8fd4\u56de\u503c\u7c7b\u578b\uff1b \u548c\u53d8\u91cf\u4e00\u6837\uff0c\u51fd\u6570\u4e5f\u6709\u51fd\u6570\u540d\u3002 identifier \u5c31\u662f\u4f60\u5b9a\u4e49\u7684\u51fd\u6570\u7684\u51fd\u6570\u540d\uff1b \u6807\u8bc6\u7b26\u540e\u9762\u7684\u62ec\u53f7\u544a\u8bc9\u7f16\u8bd1\u5668\u8fd9\u91cc\u662f\u5728\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff1b \u82b1\u62ec\u53f7\u4ee5\u53ca\u82b1\u62ec\u53f7\u5185\u90e8\u7684\u8bed\u53e5\u79f0\u4e3a \u51fd\u6570\u4f53 \u3002\u8fd9\u91cc\u5c31\u662f\u653e\u7f6e\u51fd\u6570\u8bed\u53e5\u7684\u7684\u5730\u65b9\u3002 \u4e0b\u9762\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5b9a\u4e49\u5e76\u4f7f\u7528\u4e00\u4e2a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // for std::cout // \u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a doPrint() \u7684\u51fd\u6570 void doPrint () // doPrint() \u5728\u672c\u4f8b\u4e2d\u662f\u88ab\u8c03\u51fd\u6570 { std :: cout << \"In doPrint() \\n \" ; } // Definition of function main() int main () { std :: cout << \"Starting main() \\n \" ; doPrint (); // \u4e2d\u65ad main() \u51fd\u6570\u5e76\u8c03\u7528 doPrint()\u3002main() \u51fd\u6570\u662f\u4e3b\u8c03\u51fd\u6570 std :: cout << \"Ending main() \\n \" ; // \u5728 doPrint() \u7ed3\u675f\u540e\u6267\u884c\u8be5\u6307\u4ee4 return 0 ; } \u4e0a\u8ff0\u4ee3\u7801\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 Starting main() In doPrint() Ending main() \u8be5\u7a0b\u5e8f\u4ece main \u51fd\u6570\u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\uff0c\u7b2c\u4e00\u884c\u4f1a\u6253\u5370 Starting main() . main \u51fd\u6570\u7684\u7b2c\u4e8c\u884c\u4f1a\u8c03\u7528 doPrint \u3002\u901a\u8fc7\u5728\u51fd\u6570\u540d\u540e\u9762\u52a0\u4e0a\u62ec\u53f7\u5c31\u53ef\u4ee5\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u4f8b\u5982 doPrint() \u3002\u6ce8\u610f\uff0c\u5982\u679c\u4f60\u5fd8\u8bb0\u4e86\u62ec\u53f7\uff0c\u7a0b\u5e8f\u53ef\u80fd\u65e0\u6cd5\u7f16\u8bd1\uff08\u5982\u679c\u7a0b\u5e8f\u80fd\u591f\u7f16\u8bd1\uff0c\u51fd\u6570\u4e5f\u4e0d\u4f1a\u88ab\u8c03\u7528\uff09\u3002 \u6ce8\u610f \u5728\u8c03\u7528\u51fd\u6570\u7684\u65f6\u5019\uff0c\u4e0d\u8981\u5fd8\u8bb0\u52a0\u4e0a\u62ec\u53f7\u3002 \u56e0\u4e3a\u6267\u884c\u4e86\u51fd\u6570\u8c03\u7528\uff0c main \u51fd\u6570\u4e2d\u8bed\u53e5\u7684\u6267\u884c\u88ab\u6682\u505c\u4e86\uff0c\u6b64\u65f6\u4f1a\u8df3\u8f6c\u5230\u88ab\u8c03\u51fd\u6570 doPrint \u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\u3002 doPrint \u51fd\u6570\u7684\u7b2c\u4e00\u884c\uff08\u4e5f\u662f\u552f\u4e00\u4e00\u884c\uff09\u4f1a\u6253\u5370 In doPrint() \u3002\u5f53\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u7a0b\u5e8f\u4f1a\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570( main \u51fd\u6570)\uff0c\u4ece\u4e4b\u524d\u79bb\u5f00\u7684\u4f4d\u7f6e\u7ee7\u7eed\u6267\u884c\u3002\u56e0\u6b64\uff0c\u4e0b\u4e00\u6761\u8981\u6267\u884c\u7684\u8bed\u53e5\u662f\u6253\u5370 Ending main() \u3002","title":"\u81ea\u5b9a\u5b9a\u4e49\u51fd\u6570\u6848\u4f8b"},{"location":"2-1-Introduction-to-functions/#_2","text":"\u51fd\u6570\u6700\u6709\u7528\u7684\u529f\u80fd\u83ab\u8fc7\u4e8e\u5b83\u53ef\u4ee5\u88ab\u591a\u6b21\u8c03\u7528\u3002\u4e0b\u9762\u4f8b\u5b50\u5c55\u793a\u4e86\u591a\u6b21\u8c03\u7528\u51fd\u6570\u7684\u573a\u666f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // for std::cout void doPrint () { std :: cout << \"In doPrint() \\n \" ; } // Definition of function main() int main () { std :: cout << \"Starting main() \\n \" ; doPrint (); // doPrint() called for the first time doPrint (); // doPrint() called for the second time std :: cout << \"Ending main() \\n \" ; return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 Starting main() In doPrint() In doPrint() Ending main() \u56e0\u4e3a doPrint \u5728 main \u51fd\u6570\u4e2d\u88ab\u8c03\u7528\u4e86\u4e24\u6b21\uff0c\u56e0\u6b64\u5b83\u4e5f\u4f1a\u6267\u884c\u4e24\u6b21\uff0c\u56e0\u6b64 In doPrint() \u4f1a\u6253\u5370\u4e24\u6b21\uff08\u6bcf\u6b21\u8c03\u7528\u6253\u5370\u4e00\u6b21\uff09\u3002","title":"\u591a\u6b21\u8c03\u7528\u51fd\u6570"},{"location":"2-1-Introduction-to-functions/#_3","text":"\u4f60\u5df2\u7ecf\u770b\u5230\u4e86\uff0c main \u51fd\u6570\u53ef\u4ee5\u8c03\u7528\u5176\u4ed6\u51fd\u6570\uff08\u4f8b\u5982 doPrint \uff09\u3002\u4efb\u4f55\u51fd\u6570\u90fd\u53ef\u4ee5\u51fd\u6570\u3002\u5728\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c main \u51fd\u6570\u4f1a\u8c03\u7528\u51fd\u6570 doA \uff0c doA \u51fd\u6570\u53c8\u4f1a\u8c03\u7528 doB \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include // for std::cout void doB () { std :: cout << \"In doB() \\n \" ; } void doA () { std :: cout << \"Starting doA() \\n \" ; doB (); std :: cout << \"Ending doA() \\n \" ; } // Definition of function main() int main () { std :: cout << \"Starting main() \\n \" ; doA (); std :: cout << \"Ending main() \\n \" ; return 0 ; } \u4e0a\u8ff0\u4ee3\u7801\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 Starting main() Starting doA() In doB() Ending doA() Ending main()","title":"\u51fd\u6570\u94fe\u5f0f\u8c03\u7528"},{"location":"2-1-Introduction-to-functions/#c","text":"\u548c\u5176\u4ed6\u8bed\u8a00\u4e0d\u540c\uff0cC++\u5e76\u4e0d\u652f\u6301\u5728\u51fd\u6570\u4e2d\u5b9a\u4e49\u5176\u4ed6\u51fd\u6570\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u4e0d\u5408\u6cd5\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { void foo () // \u9519\u8bef\uff1a\u8be5\u51fd\u6570\u5b9a\u4e49\u4f4d\u4e8e\u5176\u4ed6\u51fd\u6570\u5185\u90e8\u3002 { std :: cout << \"foo! \\n \" ; } foo (); // function call to foo() return 0 ; } \u6b63\u786e\u7684\u65b9\u5f0f\u5e94\u8be5\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include void foo () // no longer inside of main() { std :: cout << \"foo! \\n \" ; } int main () { foo (); return 0 ; } \u9898\u5916\u8bdd \u201cfoo\u201d \u662f\u4e00\u4e2a\u6ca1\u6709\u610f\u4e49\u7684\u5355\u8bcd\uff0c\u5728\u5c55\u793a\u67d0\u4e9b\u6982\u5ff5\u7684\u65f6\u5019\uff0c\u5982\u679c\u540d\u79f0\u4e0d\u91cd\u8981\uff0c\u90a3\u4e48\u5e38\u4f1a\u4f7f\u7528\u5b83\u6765\u4f5c\u4e3a\u5360\u4f4d\u7b26\u3002\u6b64\u7c7b\u5355\u8bcd\u6709\u4e2a\u4e13\u95e8\u7684\u540d\u5b57\u53eb\u505a\uff1a\u4f2a\u53d8\u91cf/ metasyntactic variables (\u4e0d\u8fc7\uff0c\u5728\u65e5\u5e38\u7528\u8bed\u4e2d\u5927\u5bb6\u8fd8\u662f\u559c\u6b22\u79f0\u5176\u4e3a\u201d\u540d\u79f0\u5360\u4f4d\u7b26\u201c\uff0c\u56e0\u4e3a metasyntactic variables\u8fd9\u4e2a\u5355\u8bcd\u771f\u7684\u5f88\u751f\u50fb)\u3002 \u5176\u4ed6C++\u4e2d\u5e38\u89c1\u7684\u4f2a\u53d8\u91cf\u8fd8\u5305\u62ec \u201cbar\u201d\u3001\u201cbaz\u201d\u4ee5\u53ca\u5176\u4ed6\u4ee5\u201coo\u201d\u7ed3\u5c3e\u7684\u4e09\u5b57\u6bcd\u5355\u8bcd\uff0c\u4f8b\u5982\u201cgoo\u201d\u3001\u201cmoo\u201d\u548c\u201cboo\u201d\u3002 \u5982\u679c\u4f60\u5bf9\u5176\u8bcd\u6e90\u611f\u5174\u8da3\uff0c\u53ef\u4ee5\u53bb\u9605\u8bfb RFC 3092 \u3002","title":"C++\u4e0d\u652f\u6301\u51fd\u6570\u5d4c\u5957"},{"location":"2-10-Introduction-to-the-preprocessor/","tags":["preprocessor"],"text":"Key Takeaway \u9884\u5904\u7406\u5668\u53ef\u4ee5\u770b\u505a\u662f\u5355\u72ec\u7684\u7a0b\u5e8f\uff0c\u5b83\u4f7f\u7528\u7684\u9884\u5904\u7406\u6307\u4ee4\u4e5f\u4e0d\u540c\u4e8eC++\u7684\u8bed\u6cd5\u3002 \u51fd\u6570\u7c7b\u578b\u7684\u5b8f\u4e0d\u5b89\u5168\u4e14\u53ef\u4ee5\u88ab\u51fd\u6570\u66ff\u4ee3\uff0c\u56e0\u6b64\u4e0d\u8981\u4f7f\u7528 \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f\uff0c\u5176\u4f5c\u4e3a\u5e38\u91cf\u5b9a\u4e49\u7684\u529f\u80fd\u73b0\u5728\u5df2\u7ecf\u4e0d\u63a8\u8350\u4f7f\u7528\u4e86 #if 0 \u53ef\u4ee5\u7528\u6765\u201c\u6ce8\u91ca\u6389\u201d\u5305\u542b\u591a\u884c\u6ce8\u91ca\u7684\u4ee3\u7801\uff0c\u56e0\u4e3a\u591a\u884c\u6ce8\u91ca\u662f\u4e0d\u80fd\u5d4c\u5957\u7684 \u9884\u5904\u7406\u5668\u6307\u4ee4\u4e4b\u95f4\u4e0d\u4f1a\u4e92\u76f8\u5f71\u54cd\u3002\u56e0\u6b64\u5b9a\u4e49\u67d0\u4e2a\u6807\u8bc6\u7b26\u4e3a\u7a7a\u4e0d\u4f1a\u5bfc\u81f4\u540e\u9762\u7684\u9884\u5904\u7406\u5668\u6307\u4ee4\u88ab\u66ff\u6362\u4e3a\u7a7a \u5b8f\u5b9a\u4e49\u7684\u4f5c\u7528\u57df\u53ea\u5728\u672c\u6587\u4ef6\u4e2d\uff0c\u7531\u4e8e\u9884\u5904\u7406\u5668\u4e0d\u80fd\u7406\u89e3C++\u8bed\u6cd5\uff0c\u56e0\u6b64\u5b8f\u5b9a\u4e49\u662f\u5426\u5728\u51fd\u6570\u5185\u90e8\u5e76\u65e0\u533a\u522b \u7ffb\u8bd1\u53ca\u9884\u5904\u7406\u5668 \u4f60\u53ef\u80fd\u4f1a\u89c9\u5f97\u7f16\u8bd1\u5668\u4f1a\u539f\u5c01\u4e0d\u52a8\u5730\u7f16\u8bd1\u4f60\u5199\u7684\u4ee3\u7801\u3002\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u3002 \u5728\u5f00\u59cb\u7f16\u8bd1\u524d\uff0c\u4ee3\u7801\u4f1a\u7ecf\u8fc7\u4e00\u4e2a\u53eb\u505a \u7ffb\u8bd1(translation) \u7684\u9636\u6bb5\u3002\u5728\u8fd9\u4e2a\u9636\u6bb5\u9700\u8981\u5b8c\u6210\u5f88\u591a\u64cd\u4f5c\uff0c\u624d\u80fd\u4f7f\u4f60\u7684\u4ee3\u7801\u5177\u5907\u88ab\u7f16\u8bd1\u7684\u6761\u4ef6\uff08\u5982\u679c\u6709\u5174\u8da3\u53ef\u4ee5\u5728 \u8fd9\u91cc \u67e5\u770b\u6709\u5173\u7ffb\u8bd1\u9636\u6bb5\u7684\u8be6\u7ec6\u4fe1\u606f\uff09\u3002\u8fdb\u884c\u8fc7\u7ffb\u8bd1\u7684\u4ee3\u7801\u6587\u4ef6\u53eb\u505a\u4e00\u4e2a\u7ffb\u8bd1\u5355\u5143\u3002 \u5728\u7ffb\u8bd1\u9636\u6bb5\u4e2d\u6700\u503c\u5f97\u6211\u4eec\u5173\u6ce8\u7684\u662f \u9884\u5904\u7406\u5668 \uff0c\u5b83\u53ef\u4ee5\u88ab\u770b\u505a\u662f\u4e00\u4e2a\u5bf9\u6bcf\u4e2a\u4ee3\u7801\u6587\u4ef6\u8fdb\u884c\u6587\u672c\u5904\u7406\u7684\u5355\u72ec\u7684\u5de5\u5177\u3002 \u5728\u9884\u5904\u7406\u5668\u5de5\u4f5c\u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u4ece\u5934\u5230\u5c3e\u626b\u63cf\u4ee3\u7801\u6587\u4ef6\uff0c\u67e5\u627e \u9884\u5904\u7406\u5668\u6307\u4ee4 (preprocessor directive) \u3002\u9884\u5904\u7406\u5668\u6307\u4ee4\uff08\u7ecf\u5e38\u7b80\u79f0\u4e3a\u6307\u4ee4\uff09\u662f\u4e00\u4e9b\u4ee5 # \u5f00\u5934\uff0c\u4ee5\u6362\u884c\uff08\u800c\u975e\u5206\u53f7\uff09\u7ed3\u5c3e\u7684\u7b26\u53f7\u3002\u8fd9\u4e9b\u6307\u4ee4\u4f1a\u544a\u8bc9\u9884\u5904\u7406\u5668\u5e94\u8be5\u5bf9\u6587\u672c\u6267\u884c\u54ea\u4e9b\u7279\u5b9a\u7684\u64cd\u4f5c\u3002 \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u9884\u5904\u7406\u5668\u5e76\u4e0d\u7406\u89e3C++\u7684\u8bed\u6cd5\u2014\u2014\u76f8\u53cd\uff0c\u9884\u5904\u7406\u5668\u6307\u4ee4\u6709\u5176\u81ea\u5df1\u7684\u8bed\u6cd5\uff08\u6709\u4e9b\u548cC++\u8bed\u6cd5\u7c7b\u4f3c\uff0c\u6709\u4e9b\u5219\u4e0d\u7136\uff09\u3002 \u9884\u5904\u7406\u5668\u7684\u8f93\u51fa\u4f1a\u7ecf\u5386\u4e00\u4e9b\u5217\u7ffb\u8bd1\u9636\u6bb5\uff0c\u7136\u540e\u624d\u4f1a\u88ab\u7f16\u8bd1\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u9884\u5904\u7406\u5668\u5e76\u4e0d\u4f1a\u4fee\u6539\u539f\u6587\u4ef6\u2014\u2014\u6240\u6709\u7ecf\u9884\u5904\u7406\u5668\u4fee\u6539\u4ea7\u751f\u7684\u53d8\u5316\u90fd\u662f\u4e34\u65f6\u7684\uff08\u7528\u4e8e\u4e00\u6b21\u7f16\u8bd1\uff09 \u672c\u8282\u8bfe\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u6700\u4e3a\u5e38\u89c1\u7684\u9884\u5904\u7406\u5668\u6307\u4ee4\u3002 \u9898\u5916\u8bdd Using \u6307\u4ee4 ( 2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 \u4e2d\u4ecb\u7ecd)\u5e76\u4e0d\u5c5e\u4e8e\u9884\u5904\u7406\u5668\u6307\u4ee4\uff08\u56e0\u6b64\u4e0d\u4f1a\u88ab\u9884\u5904\u7406\u5668\u5904\u7406\uff09\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u6307\u4ee4(directive)\u4e00\u8bcd\u591a\u6307\u9884\u5904\u7406\u5668\u6307\u4ee4\uff0c\u4f46\u4e5f\u4e0d\u662f\u603b\u662f\u8fd9\u6837\u3002 Includes \u6211\u4eec\u5df2\u7ecf\u5728\u5b9e\u9645\u6848\u4f8b\u4e2d\u63a5\u89e6\u8fc7 #include \u6307\u4ee4\u4e86\uff08 #include \uff09\u3002\u5728 #include \u67d0\u4e2a\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u9884\u5904\u7406\u5668\u4f1a\u628a #include \u66ff\u6362\u4e3a\u8be5\u6587\u4ef6\u7684\u5b9e\u9645\u5185\u5bb9\uff0c\u7136\u540e\u9884\u5904\u7406\u5668\u624d\u4f1a\u5bf9\u66ff\u6362\u540e\u7684\u6587\u4ef6\u5185\u5bb9\u8fdb\u884c\u9884\u5904\u7406\u548c\u7f16\u8bd1\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << \"Hello, world!\" ; return 0 ; } \u5f53\u9884\u5904\u7406\u5668\u5904\u7406\u8be5\u7a0b\u5e8f\u65f6\uff0c\u5b83\u4f1a\u628a #include \u66ff\u6362\u4e3a\u7ecf\u8fc7\u9884\u5904\u7406\u7684\u540d\u4e3a\u201ciostream\u201d\u7684\u6587\u4ef6\u7684\u5185\u5bb9\u3002 \u56e0\u4e3a #include \u51e0\u4e4e\u53ea\u7528\u6765\u5305\u542b\u5934\u6587\u4ef6\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u8ba8\u8bba\u5934\u6587\u4ef6\u65f6\u518d\u5bf9\u5176\u8fdb\u884c\u8be6\u7ec6\u7684\u4ecb\u7ecd\u3002\uff08\u53c2\u89c1\uff1a 2.11 - \u5934\u6587\u4ef6 \uff09 \u5b8f\u5b9a\u4e49 #define \u6307\u4ee4\u53ef\u4ee5\u88ab\u7528\u6765\u5b9a\u4e49 \u5b8f(macro) \u3002\u5728C++\u4e2d\uff0c\u5b8f\u6307\u7684\u662f\u4e00\u79cd\u89c4\u5219\uff0c\u8fd9\u79cd\u89c4\u5219\u89c4\u5b9a\u4e86\u4e00\u4e2a\u8f93\u5165\u6587\u672c\u5e94\u8be5\u5982\u4f55\u88ab\u66ff\u6362\u6210\u8f93\u51fa\u6587\u672c\u3002 \u5b8f\u6709\u4e24\u79cd\u57fa\u672c\u7c7b\u578b\uff1a \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f(object-like macros) \u548c \u51fd\u6570\u7c7b\u578b\u7684\u5b8f (function-like macros) \u3002 \u51fd\u6570\u7c7b\u578b\u7684\u5b8f \u548c\u51fd\u6570\u5f88\u50cf\uff0c\u5176\u529f\u80fd\u4e5f\u7c7b\u4f3c\u3002\u6211\u4eec\u5e76\u4e0d\u4f1a\u5728\u8fd9\u91cc\u8be6\u7ec6\u8ba8\u8bba\u5b83\uff0c\u56e0\u4e3a\u5b83\u5e38\u5e38\u88ab\u8ba4\u4e3a\u662f\u4e0d\u5b89\u5168\u7684\uff0c\u51b5\u4e14\u5b83\u80fd\u505a\u7684\u666e\u901a\u51fd\u6570\u4e5f\u80fd\u505a\uff0c \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f \u6709\u4e24\u79cd\u58f0\u660e\u65b9\u5f0f\uff1a 1 2 #define identifier #define identifier substitution_text \u4e0a\u9762\u4e00\u79cd\u65b9\u5f0f\u4e0d\u4f1a\u5bf9\u6587\u672c\u8fdb\u884c\u66ff\u6362\uff0c\u800c\u4e0b\u9762\u4e00\u79cd\u4f1a\u3002\u56e0\u4e3a\u8fd9\u4e9b\u90fd\u5c5e\u4e8e \u9884\u5904\u7406\u5668\u6307\u4ee4 \u800c\u4e0d\u662f\u8bed\u53e5\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u4ee5\u5206\u53f7\u7ed3\u5c3e\uff0c \u6709\u66ff\u6362\u6587\u672c\u7684\u5bf9\u8c61\u7c7b\u578b\u5b8f \u5f53\u9884\u5904\u7406\u5668\u9047\u5230\u8be5\u6307\u4ee4\u65f6\uff0c\u4efb\u4f55\u4f7f\u7528\u8be5\u6807\u8bc6\u7b26\u7684\u5730\u65b9\u90fd\u4f1a\u88ab\u66ff\u6362\u4e3a\u5bf9\u5e94\u7684 \u66ff\u6362\u6587\u672c \u3002\u4e00\u822c\u6765\u8bf4\uff0c\u8fd9\u4e9b\u6807\u8bc6\u7b26\u4f1a\u4ee5\u5168\u5927\u5199\u7684\u5f62\u5f0f\u4f7f\u7528\uff0c\u540c\u65f6\u4f7f\u7528\u4e0b\u5212\u7ebf\u4ee3\u66ff\u7a7a\u683c\u3002 \u8003\u8651\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 #include #define MY_NAME \"Alex\" int main () { std :: cout << \"My name is: \" << MY_NAME ; return 0 ; } \u9884\u5904\u7406\u5668\u4f1a\u628a\u4e0a\u9762\u7684\u4ee3\u7801\u8f6c\u6362\u6210\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 // iostream \u7684\u5185\u5bb9\u4f1a\u88ab\u63d2\u5165\u5230\u8fd9\u91cc int main () { std :: cout << \"My name is: \" << \"Alex\" ; return 0 ; } \u56e0\u6b64\u4e0a\u8ff0\u7a0b\u5e8f\u4f1a\u6253\u5370 My name is: Alex \u3002 \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f\u8fc7\u53bb\u4e5f\u88ab\u7528\u6765\u4f5c\u5b9a\u4e49\u5e38\u91cf\uff08\u4e00\u79cd\u5f00\u9500\u66f4\u5c0f\u7684\u65b9\u5f0f\uff09\u3002\u968f\u7740\u7f16\u8bd1\u5668\u7684\u53d1\u5c55\u548c\u8bed\u8a00\u7684\u66f4\u65b0\uff0c\u6b64\u7c7b\u9700\u6c42\u5728\u73b0\u5982\u4eca\u5df2\u7ecf\u6ca1\u5fc5\u8981\u4e86\u3002\u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f\u5e94\u8be5\u4ec5\u51fa\u73b0\u5728\u5386\u53f2\u9057\u7559\u4ee3\u7801\u4e2d\u624d\u5bf9\u3002 \u6211\u4eec\u5efa\u8bae\u5f7b\u5e95\u653e\u5f03\u4f7f\u7528\u6b64\u7c7b\u5b8f\u5b9a\u4e49\uff0c\u56e0\u4e3a\u6709\u66f4\u597d\u7684\u65b9\u6cd5\u53ef\u4ee5\u7528\u3002\u6211\u4eec\u4f1a\u5728 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \u4e2d\u8fdb\u884c\u4ecb\u7ecd \u65e0\u66ff\u6362\u6587\u672c\u7684\u5bf9\u8c61\u7c7b\u578b\u5b8f \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f\u4e5f\u53ef\u4ee5\u5728\u6ca1\u6709\u66ff\u6362\u6587\u672c\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\uff1a \u4f8b\u5982\uff1a 1 #define USE_YEN \u8fd9\u4e2a\u5b8f\u7684\u4f5c\u7528\u4f60\u5e94\u8be5\u80fd\u591f\u731c\u5230\uff1a\u4efb\u4f55\u9047\u5230\u8be5\u6807\u8bc6\u7b26\u7684\u5730\u65b9\uff0c\u90fd\u4f1a\u88ab\u66ff\u6362\u4e3a\u7a7a\u767d\uff01 \u770b\u4e0a\u53bb\u8fd9\u4e2a\u5b8f\u597d\u50cf\u6ca1\u4ec0\u4e48\u7528\uff0c\u786e\u5b9e\uff0c\u5bf9\u4e8e\u6587\u672c\u66ff\u6362\u6765\u8bf4\u5b83\u786e\u5b9e\u6ca1\u4ec0\u4e48\u7528\u3002\u4e0d\u8fc7\uff0c\u8fd9\u79cd\u7c7b\u578b\u7684\u5b8f\u672c\u6765\u5c31\u4e0d\u662f\u7528\u6765\u8fdb\u884c\u66ff\u6362\u7684\uff0c\u5f88\u5feb\u4f60\u5c31\u4f1a\u770b\u5230\u5b83\u7684\u5b9e\u9645\u7528\u9014\u3002 \u548c\u6709\u66ff\u6362\u6587\u672c\u7684\u5b8f\u4e0d\u540c\uff0c\u8fd9\u79cd\u7c7b\u578b\u7684\u5b8f\u901a\u5e38\u88ab\u8ba4\u4e3a\u662f\u53ef\u4ee5\u4f7f\u7528\u7684\u3002 \u6761\u4ef6\u7f16\u8bd1 \u6761\u4ef6\u7f16\u8bd1\u9884\u5904\u7406\u5668\u6307\u4ee4\u4f7f\u6211\u4eec\u53ef\u4ee5\u63a7\u5236\u5728\u4f55\u79cd\u6761\u4ef6\u4e0b\uff0c\u5bf9\u5e94\u7684\u4ee3\u7801\u9700\u8981\u7f16\u8bd1\u6216\u8005\u4e0d\u9700\u8981\u7f16\u8bd1\u3002\u6761\u4ef6\u7f16\u8bd1\u6307\u4ee4\u7684\u79cd\u7c7b\u6709\u5f88\u591a\uff0c\u8fd9\u91cc\u6211\u4eec\u53ea\u4ecb\u7ecd\u4e09\u79cd\u6700\u4e3a\u5e38\u7528\u7684\uff1a #ifdef \u3001 #ifndef_ \u548c #endif \u3002 #ifdef \u6307\u4ee4\u4f1a\u8ba9\u9884\u5904\u7406\u5668\u68c0\u67e5\u67d0\u4e2a\u6807\u8bc6\u7b26\u662f\u5426\u88ab #define \u8fc7\uff0c\u5982\u679c\u662f\u7684\u8bdd\uff0c #ifdef \u548c #endif \u4e4b\u95f4\u7684\u4ee3\u7801\u5c06\u4f1a\u88ab\u7f16\u8bd1\uff0c\u5426\u5219\u8fd9\u4e9b\u4ee3\u7801\u4f1a\u88ab\u5ffd\u7565\u3002 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #define PRINT_JOE int main () { #ifdef PRINT_JOE std :: cout << \"Joe \\n \" ; // will be compiled since PRINT_JOE is defined #endif #ifdef PRINT_BOB std :: cout << \"Bob \\n \" ; // will be ignored since PRINT_BOB is not defined #endif return 0 ; } \u56e0\u4e3a PRINT_JOE \u88ab #defined \u4e86\uff0c\u56e0\u6b64 std::cout << \"Joe\\n\" \u4f1a\u88ab\u7f16\u8bd1\uff0c\u800c\u56e0\u4e3a PRINT_BOB \u6ca1\u6709\u88ab #defined \uff0c\u6240\u4ee5\u5bf9\u5e94\u7684 std::cout << \"Bob\\n\" \u4f1a\u88ab\u5ffd\u7565\u3002 #ifndef \u662f #ifdef \u7684\u53cd\u4e49\u8bcd\uff0c\u5b83\u63a7\u5236\u9884\u5904\u7406\u5668\u68c0\u67e5\u67d0\u4e2a\u6807\u8bc6\u7b26\u662f\u5426 \u6ca1\u6709 \u88ab #define \u3002 1 2 3 4 5 6 7 8 9 10 #include int main () { #ifndef PRINT_BOB std :: cout << \"Bob \\n \" ; #endif return 0 ; } \u4e0a\u9762\u7684\u7a0b\u5e8f\u4f1a\u6253\u5370 \u201cBob\u201d\uff0c\u56e0\u4e3a PRINT_BOB \u6ca1\u6709\u88ab #define_ \u8fc7\u3002 \u9664\u4e86 #ifdef PRINT_BOB \u548c #ifndef PRINT_BOB \u8fd9\u6837\u7684\u5f62\u5f0f\u4ee5\u5916\uff0c\u4f60\u4e5f\u53ef\u80fd\u4f1a\u9047\u5230 #if defined(PRINT_BOB) \u548c #if !defined(PRINT_BOB) \u8fd9\u6837\u7684\u5f62\u5f0f\u3002\u5b83\u4eec\u7684\u529f\u80fd\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u53ea\u4e0d\u8fc7\u540e\u8005\u770b\u4e0a\u53bb\u548cC++\u7684\u8bed\u6cd5\u98ce\u683c\u66f4\u52a0\u4e00\u81f4\u3002 #if 0 \u8fd8\u6709\u4e00\u4e2a\u5e38\u7528\u7684\u6761\u4ef6\u7f16\u8bd1\u6307\u4ee4\u662f #if 0 \uff0c\u5b83\u53ef\u4ee5\u5c06\u5b83\u5305\u88f9\u8303\u56f4\u5185\u7684\u4ee3\u7801\u6392\u9664\u5728\u7f16\u8bd1\u4e4b\u5916\uff08\u5c31\u597d\u50cf\u8fd9\u4e9b\u4ee3\u7801\u88ab\u6ce8\u91ca\u6389\u4e86\u4e00\u6837\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { std :: cout << \"Joe \\n \" ; #if 0 // \u4ece\u6b64\u884c\u5f00\u59cb\u7684\u4ee3\u7801\u90fd\u4e0d\u7f16\u8bd1 std::cout << \"Bob\\n\"; std::cout << \"Steve\\n\"; #endif // \u76f4\u5230\u8fd9\u4e00\u884c\u4e3a\u6b62 return 0 ; } \u4e0a\u9762\u7684\u7a0b\u5e8f\u53ea\u4f1a\u6253\u5370 \u201cJoe\u201d\uff0c\u56e0\u4e3a\u201cBob\u201d \u548c \u201cSteve\u201d \u5747\u4f4d\u4e8e #if 0 \u5757\u4e2d\uff0c\u6240\u4ee5\u9884\u5904\u7406\u5668\u4f1a\u5c06\u5176\u6392\u9664\u5728\u7f16\u8bd1\u8303\u56f4\u5916\u3002 \u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u65b9\u4fbf\u7684\u201d\u6ce8\u91ca\u6389\u201c\u67d0\u4e9b\u5305\u542b\u4e86\u591a\u884c\u6ce8\u91ca\u7684\u4ee3\u7801\u4e86\uff08\u56e0\u4e3a\u591a\u884c\u6ce8\u91ca\u4e0d\u652f\u6301\u5d4c\u5957\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { std :: cout << \"Joe \\n \" ; #if 0 // Don't compile anything starting here std::cout << \"Bob\\n\"; /* Some * multi-line * comment here */ std::cout << \"Steve\\n\"; #endif // until this point return 0 ; } \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u9884\u5904\u7406\u5668\u6307\u4ee4 \u4f60\u4e5f\u8bb8\u4f1a\u5bf9\u4e0b\u9762\u8fd9\u79cd\u5199\u6cd5\u5fc3\u5b58\u987e\u8651\uff1a 1 2 3 4 #define PRINT_JOE #ifdef PRINT_JOE // ... \u65e2\u7136\u6211\u4eec\u5b9a\u4e49\u4e86 PRINT_JOE \u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u9884\u5904\u7406\u5668\u4e0d\u4f1a\u5c06 #ifdef PRINT_JOE \u4e2d\u7684 PRINT_JOE \u66ff\u6362\u6389\u5462\uff1f \u5b8f\u5b9a\u4e49\u7684\u66ff\u6362\u53ea\u9488\u5bf9\u4e0e\u666e\u901a\u4ee3\u7801\u3002\u5176\u4ed6\u9884\u5904\u7406\u5668\u7684\u6307\u4ee4\u662f\u4e0d\u4f1a\u53d7\u5230\u5f71\u54cd\u7684\uff0c\u56e0\u6b64 #ifdef PRINT_JOE \u4e2d\u7684 PRINT_JOE \u662f\u4f1a\u4fdd\u7559\u7684\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 #define FOO 9 // \u5b8f\u66ff\u6362\u4e3a\u7a7a #ifdef FOO // \u6b64\u5904\u7684 FOO \u4e0d\u4f1a\u88ab\u66ff\u6362\u56e0\u4e3a\u5b83\u5c5e\u4e8e\u9884\u5904\u7406\u5668\u6307\u4ee4 std :: cout << FOO ; // \u6b64\u5904\u7684FOO\u4f1a\u88ab\u66ff\u6362\u56e0\u4e3a\u5b83\u5c5e\u4e8e\u666e\u901a\u4ee3\u7801 #endif \u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\uff0c\u9884\u5904\u7406\u5668\u8f93\u51fa\u7684\u7ed3\u679c\u4e2d\u4e0d\u5305\u542b\u4efb\u4f55\u9884\u5904\u7406\u5668\u6307\u4ee4\u2014\u2014\u5b83\u4eec\u90fd\u4f1a\u5728\u7f16\u8bd1\u524d\u88ab\u5904\u7406\u6389\uff0c\u6bd4\u8f83\u7f16\u8bd1\u5668\u5e76\u4e0d\u77e5\u9053\u8981\u5982\u4f55\u5904\u7406\u8fd9\u4e9b\u6307\u4ee4\u3002 \u5b8f\u5b9a\u4e49\u7684\u4f5c\u7528\u57df \u9884\u5904\u7406\u5668\u6307\u4ee4\u7684\u89e3\u6790\u4f1a\u5728\u7f16\u8bd1\u5668\u5b8c\u6210\uff08\u4ece\u4e0a\u5230\u4e0b\u9010\u6587\u4ef6\u5730\uff09\u3002 \u8003\u8651\u5982\u4e0b\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void foo () { #define MY_NAME \"Alex\" } int main () { std :: cout << \"My name is: \" << MY_NAME ; return 0 ; } \u5c3d\u7ba1\u770b\u4e0a\u53bb #define MY_NAME \u201cAlex\u201d \u50cf\u662f\u88ab\u5b9a\u4e49\u5230\u4e86 foo \u51fd\u6570\u4e2d\uff0c\u4f46\u9884\u5904\u7406\u5668\u5e76\u4e0d\u4f1a\u610f\u8bc6\u5230\u8fd9\u4e00\u70b9\uff0c\u56e0\u4e3a\u5b83\u4e0d\u7406\u89e3\u201d\u51fd\u6570\u201c\u4ee5\u53ca\u5176\u4ed6C++\u7684\u8bed\u6cd5\u6982\u5ff5\u3002\u56e0\u6b64\uff0c\u8fd9\u6bb5\u4ee3\u7801\u7684\u6548\u679c\u597d\u6211\u4eec\u5728\u51fd\u6570\u4e4b\u524d\u6216\u662f\u4e4b\u540e\u5b9a\u4e49 #define MY_NAME \u201cAlex\u201d \u662f\u4e00\u6a21\u4e00\u6837\u7684\u3002\u5904\u4e8e\u53ef\u8bfb\u6027\u7684\u8003\u8651\uff0c\u4e00\u822c #define \u4e0d\u4f1a\u88ab\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8\u3002 \u5f53\u9884\u5904\u7406\u5668\u7684\u5de5\u4f5c\u5b8c\u6210\u540e\uff0c\u6240\u6709\u8fd9\u4e9b #define \u7684\u6807\u8bc6\u7b26\u5c31\u90fd\u88ab\u4e22\u5f03\u4e86\u3002\u56e0\u6b64\u8fd9\u4e9b\u6807\u8bc6\u7b26\u53ea\u6709\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u662f\u6709\u6548\u7684\uff0c\u5b9a\u4e49\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u7684\u5b8f\u4e0d\u4f1a\u5f71\u54cd\u5230\u5176\u4ed6\u6587\u4ef6\u3002 \u8003\u8651\u8fd9\u4e2a\u4f8b\u5b50\uff1a function.cpp: 1 2 3 4 5 6 7 8 9 10 11 #include void doSomething () { #ifdef PRINT std :: cout << \"Printing!\" ; #endif #ifndef PRINT std :: cout << \"Not printing!\" ; #endif } main.cpp: 1 2 3 4 5 6 7 8 9 10 void doSomething (); // forward declaration for function doSomething() #define PRINT int main () { doSomething (); return 0 ; } \u4e0a\u9762\u4ee3\u7801\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 Not printing! \u5c3d\u7ba1 PRINT \u88ab\u5b9a\u4e49\u5728\u4e86 main.cpp \u4e2d\uff0c\u4f46\u662f\u5b83\u4e1d\u6beb\u4e0d\u4f1a\u5f71\u54cd function.cpp \uff08\u5bf9 PRINT \u7684\u5b9a\u4e49\u53ea\u5728 main.cpp \u4e2d\u6709\u6548\uff09\u3002\u6211\u4eec\u4f1a\u5728 2.12 - \u5934\u6587\u4ef6\u91cd\u590d\u5305\u542b\u4fdd\u62a4 \u4e2d\u63a2\u8ba8\u5176\u5f71\u54cd\u3002","title":"2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb"},{"location":"2-10-Introduction-to-the-preprocessor/#_1","text":"\u4f60\u53ef\u80fd\u4f1a\u89c9\u5f97\u7f16\u8bd1\u5668\u4f1a\u539f\u5c01\u4e0d\u52a8\u5730\u7f16\u8bd1\u4f60\u5199\u7684\u4ee3\u7801\u3002\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u3002 \u5728\u5f00\u59cb\u7f16\u8bd1\u524d\uff0c\u4ee3\u7801\u4f1a\u7ecf\u8fc7\u4e00\u4e2a\u53eb\u505a \u7ffb\u8bd1(translation) \u7684\u9636\u6bb5\u3002\u5728\u8fd9\u4e2a\u9636\u6bb5\u9700\u8981\u5b8c\u6210\u5f88\u591a\u64cd\u4f5c\uff0c\u624d\u80fd\u4f7f\u4f60\u7684\u4ee3\u7801\u5177\u5907\u88ab\u7f16\u8bd1\u7684\u6761\u4ef6\uff08\u5982\u679c\u6709\u5174\u8da3\u53ef\u4ee5\u5728 \u8fd9\u91cc \u67e5\u770b\u6709\u5173\u7ffb\u8bd1\u9636\u6bb5\u7684\u8be6\u7ec6\u4fe1\u606f\uff09\u3002\u8fdb\u884c\u8fc7\u7ffb\u8bd1\u7684\u4ee3\u7801\u6587\u4ef6\u53eb\u505a\u4e00\u4e2a\u7ffb\u8bd1\u5355\u5143\u3002 \u5728\u7ffb\u8bd1\u9636\u6bb5\u4e2d\u6700\u503c\u5f97\u6211\u4eec\u5173\u6ce8\u7684\u662f \u9884\u5904\u7406\u5668 \uff0c\u5b83\u53ef\u4ee5\u88ab\u770b\u505a\u662f\u4e00\u4e2a\u5bf9\u6bcf\u4e2a\u4ee3\u7801\u6587\u4ef6\u8fdb\u884c\u6587\u672c\u5904\u7406\u7684\u5355\u72ec\u7684\u5de5\u5177\u3002 \u5728\u9884\u5904\u7406\u5668\u5de5\u4f5c\u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u4ece\u5934\u5230\u5c3e\u626b\u63cf\u4ee3\u7801\u6587\u4ef6\uff0c\u67e5\u627e \u9884\u5904\u7406\u5668\u6307\u4ee4 (preprocessor directive) \u3002\u9884\u5904\u7406\u5668\u6307\u4ee4\uff08\u7ecf\u5e38\u7b80\u79f0\u4e3a\u6307\u4ee4\uff09\u662f\u4e00\u4e9b\u4ee5 # \u5f00\u5934\uff0c\u4ee5\u6362\u884c\uff08\u800c\u975e\u5206\u53f7\uff09\u7ed3\u5c3e\u7684\u7b26\u53f7\u3002\u8fd9\u4e9b\u6307\u4ee4\u4f1a\u544a\u8bc9\u9884\u5904\u7406\u5668\u5e94\u8be5\u5bf9\u6587\u672c\u6267\u884c\u54ea\u4e9b\u7279\u5b9a\u7684\u64cd\u4f5c\u3002 \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u9884\u5904\u7406\u5668\u5e76\u4e0d\u7406\u89e3C++\u7684\u8bed\u6cd5\u2014\u2014\u76f8\u53cd\uff0c\u9884\u5904\u7406\u5668\u6307\u4ee4\u6709\u5176\u81ea\u5df1\u7684\u8bed\u6cd5\uff08\u6709\u4e9b\u548cC++\u8bed\u6cd5\u7c7b\u4f3c\uff0c\u6709\u4e9b\u5219\u4e0d\u7136\uff09\u3002 \u9884\u5904\u7406\u5668\u7684\u8f93\u51fa\u4f1a\u7ecf\u5386\u4e00\u4e9b\u5217\u7ffb\u8bd1\u9636\u6bb5\uff0c\u7136\u540e\u624d\u4f1a\u88ab\u7f16\u8bd1\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u9884\u5904\u7406\u5668\u5e76\u4e0d\u4f1a\u4fee\u6539\u539f\u6587\u4ef6\u2014\u2014\u6240\u6709\u7ecf\u9884\u5904\u7406\u5668\u4fee\u6539\u4ea7\u751f\u7684\u53d8\u5316\u90fd\u662f\u4e34\u65f6\u7684\uff08\u7528\u4e8e\u4e00\u6b21\u7f16\u8bd1\uff09 \u672c\u8282\u8bfe\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u6700\u4e3a\u5e38\u89c1\u7684\u9884\u5904\u7406\u5668\u6307\u4ee4\u3002 \u9898\u5916\u8bdd Using \u6307\u4ee4 ( 2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 \u4e2d\u4ecb\u7ecd)\u5e76\u4e0d\u5c5e\u4e8e\u9884\u5904\u7406\u5668\u6307\u4ee4\uff08\u56e0\u6b64\u4e0d\u4f1a\u88ab\u9884\u5904\u7406\u5668\u5904\u7406\uff09\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u6307\u4ee4(directive)\u4e00\u8bcd\u591a\u6307\u9884\u5904\u7406\u5668\u6307\u4ee4\uff0c\u4f46\u4e5f\u4e0d\u662f\u603b\u662f\u8fd9\u6837\u3002","title":"\u7ffb\u8bd1\u53ca\u9884\u5904\u7406\u5668"},{"location":"2-10-Introduction-to-the-preprocessor/#includes","text":"\u6211\u4eec\u5df2\u7ecf\u5728\u5b9e\u9645\u6848\u4f8b\u4e2d\u63a5\u89e6\u8fc7 #include \u6307\u4ee4\u4e86\uff08 #include \uff09\u3002\u5728 #include \u67d0\u4e2a\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u9884\u5904\u7406\u5668\u4f1a\u628a #include \u66ff\u6362\u4e3a\u8be5\u6587\u4ef6\u7684\u5b9e\u9645\u5185\u5bb9\uff0c\u7136\u540e\u9884\u5904\u7406\u5668\u624d\u4f1a\u5bf9\u66ff\u6362\u540e\u7684\u6587\u4ef6\u5185\u5bb9\u8fdb\u884c\u9884\u5904\u7406\u548c\u7f16\u8bd1\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << \"Hello, world!\" ; return 0 ; } \u5f53\u9884\u5904\u7406\u5668\u5904\u7406\u8be5\u7a0b\u5e8f\u65f6\uff0c\u5b83\u4f1a\u628a #include \u66ff\u6362\u4e3a\u7ecf\u8fc7\u9884\u5904\u7406\u7684\u540d\u4e3a\u201ciostream\u201d\u7684\u6587\u4ef6\u7684\u5185\u5bb9\u3002 \u56e0\u4e3a #include \u51e0\u4e4e\u53ea\u7528\u6765\u5305\u542b\u5934\u6587\u4ef6\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u8ba8\u8bba\u5934\u6587\u4ef6\u65f6\u518d\u5bf9\u5176\u8fdb\u884c\u8be6\u7ec6\u7684\u4ecb\u7ecd\u3002\uff08\u53c2\u89c1\uff1a 2.11 - \u5934\u6587\u4ef6 \uff09","title":"Includes"},{"location":"2-10-Introduction-to-the-preprocessor/#_2","text":"#define \u6307\u4ee4\u53ef\u4ee5\u88ab\u7528\u6765\u5b9a\u4e49 \u5b8f(macro) \u3002\u5728C++\u4e2d\uff0c\u5b8f\u6307\u7684\u662f\u4e00\u79cd\u89c4\u5219\uff0c\u8fd9\u79cd\u89c4\u5219\u89c4\u5b9a\u4e86\u4e00\u4e2a\u8f93\u5165\u6587\u672c\u5e94\u8be5\u5982\u4f55\u88ab\u66ff\u6362\u6210\u8f93\u51fa\u6587\u672c\u3002 \u5b8f\u6709\u4e24\u79cd\u57fa\u672c\u7c7b\u578b\uff1a \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f(object-like macros) \u548c \u51fd\u6570\u7c7b\u578b\u7684\u5b8f (function-like macros) \u3002 \u51fd\u6570\u7c7b\u578b\u7684\u5b8f \u548c\u51fd\u6570\u5f88\u50cf\uff0c\u5176\u529f\u80fd\u4e5f\u7c7b\u4f3c\u3002\u6211\u4eec\u5e76\u4e0d\u4f1a\u5728\u8fd9\u91cc\u8be6\u7ec6\u8ba8\u8bba\u5b83\uff0c\u56e0\u4e3a\u5b83\u5e38\u5e38\u88ab\u8ba4\u4e3a\u662f\u4e0d\u5b89\u5168\u7684\uff0c\u51b5\u4e14\u5b83\u80fd\u505a\u7684\u666e\u901a\u51fd\u6570\u4e5f\u80fd\u505a\uff0c \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f \u6709\u4e24\u79cd\u58f0\u660e\u65b9\u5f0f\uff1a 1 2 #define identifier #define identifier substitution_text \u4e0a\u9762\u4e00\u79cd\u65b9\u5f0f\u4e0d\u4f1a\u5bf9\u6587\u672c\u8fdb\u884c\u66ff\u6362\uff0c\u800c\u4e0b\u9762\u4e00\u79cd\u4f1a\u3002\u56e0\u4e3a\u8fd9\u4e9b\u90fd\u5c5e\u4e8e \u9884\u5904\u7406\u5668\u6307\u4ee4 \u800c\u4e0d\u662f\u8bed\u53e5\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u4ee5\u5206\u53f7\u7ed3\u5c3e\uff0c","title":"\u5b8f\u5b9a\u4e49"},{"location":"2-10-Introduction-to-the-preprocessor/#_3","text":"\u5f53\u9884\u5904\u7406\u5668\u9047\u5230\u8be5\u6307\u4ee4\u65f6\uff0c\u4efb\u4f55\u4f7f\u7528\u8be5\u6807\u8bc6\u7b26\u7684\u5730\u65b9\u90fd\u4f1a\u88ab\u66ff\u6362\u4e3a\u5bf9\u5e94\u7684 \u66ff\u6362\u6587\u672c \u3002\u4e00\u822c\u6765\u8bf4\uff0c\u8fd9\u4e9b\u6807\u8bc6\u7b26\u4f1a\u4ee5\u5168\u5927\u5199\u7684\u5f62\u5f0f\u4f7f\u7528\uff0c\u540c\u65f6\u4f7f\u7528\u4e0b\u5212\u7ebf\u4ee3\u66ff\u7a7a\u683c\u3002 \u8003\u8651\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 #include #define MY_NAME \"Alex\" int main () { std :: cout << \"My name is: \" << MY_NAME ; return 0 ; } \u9884\u5904\u7406\u5668\u4f1a\u628a\u4e0a\u9762\u7684\u4ee3\u7801\u8f6c\u6362\u6210\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 // iostream \u7684\u5185\u5bb9\u4f1a\u88ab\u63d2\u5165\u5230\u8fd9\u91cc int main () { std :: cout << \"My name is: \" << \"Alex\" ; return 0 ; } \u56e0\u6b64\u4e0a\u8ff0\u7a0b\u5e8f\u4f1a\u6253\u5370 My name is: Alex \u3002 \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f\u8fc7\u53bb\u4e5f\u88ab\u7528\u6765\u4f5c\u5b9a\u4e49\u5e38\u91cf\uff08\u4e00\u79cd\u5f00\u9500\u66f4\u5c0f\u7684\u65b9\u5f0f\uff09\u3002\u968f\u7740\u7f16\u8bd1\u5668\u7684\u53d1\u5c55\u548c\u8bed\u8a00\u7684\u66f4\u65b0\uff0c\u6b64\u7c7b\u9700\u6c42\u5728\u73b0\u5982\u4eca\u5df2\u7ecf\u6ca1\u5fc5\u8981\u4e86\u3002\u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f\u5e94\u8be5\u4ec5\u51fa\u73b0\u5728\u5386\u53f2\u9057\u7559\u4ee3\u7801\u4e2d\u624d\u5bf9\u3002 \u6211\u4eec\u5efa\u8bae\u5f7b\u5e95\u653e\u5f03\u4f7f\u7528\u6b64\u7c7b\u5b8f\u5b9a\u4e49\uff0c\u56e0\u4e3a\u6709\u66f4\u597d\u7684\u65b9\u6cd5\u53ef\u4ee5\u7528\u3002\u6211\u4eec\u4f1a\u5728 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \u4e2d\u8fdb\u884c\u4ecb\u7ecd","title":"\u6709\u66ff\u6362\u6587\u672c\u7684\u5bf9\u8c61\u7c7b\u578b\u5b8f"},{"location":"2-10-Introduction-to-the-preprocessor/#_4","text":"\u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f\u4e5f\u53ef\u4ee5\u5728\u6ca1\u6709\u66ff\u6362\u6587\u672c\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\uff1a \u4f8b\u5982\uff1a 1 #define USE_YEN \u8fd9\u4e2a\u5b8f\u7684\u4f5c\u7528\u4f60\u5e94\u8be5\u80fd\u591f\u731c\u5230\uff1a\u4efb\u4f55\u9047\u5230\u8be5\u6807\u8bc6\u7b26\u7684\u5730\u65b9\uff0c\u90fd\u4f1a\u88ab\u66ff\u6362\u4e3a\u7a7a\u767d\uff01 \u770b\u4e0a\u53bb\u8fd9\u4e2a\u5b8f\u597d\u50cf\u6ca1\u4ec0\u4e48\u7528\uff0c\u786e\u5b9e\uff0c\u5bf9\u4e8e\u6587\u672c\u66ff\u6362\u6765\u8bf4\u5b83\u786e\u5b9e\u6ca1\u4ec0\u4e48\u7528\u3002\u4e0d\u8fc7\uff0c\u8fd9\u79cd\u7c7b\u578b\u7684\u5b8f\u672c\u6765\u5c31\u4e0d\u662f\u7528\u6765\u8fdb\u884c\u66ff\u6362\u7684\uff0c\u5f88\u5feb\u4f60\u5c31\u4f1a\u770b\u5230\u5b83\u7684\u5b9e\u9645\u7528\u9014\u3002 \u548c\u6709\u66ff\u6362\u6587\u672c\u7684\u5b8f\u4e0d\u540c\uff0c\u8fd9\u79cd\u7c7b\u578b\u7684\u5b8f\u901a\u5e38\u88ab\u8ba4\u4e3a\u662f\u53ef\u4ee5\u4f7f\u7528\u7684\u3002","title":"\u65e0\u66ff\u6362\u6587\u672c\u7684\u5bf9\u8c61\u7c7b\u578b\u5b8f"},{"location":"2-10-Introduction-to-the-preprocessor/#_5","text":"\u6761\u4ef6\u7f16\u8bd1\u9884\u5904\u7406\u5668\u6307\u4ee4\u4f7f\u6211\u4eec\u53ef\u4ee5\u63a7\u5236\u5728\u4f55\u79cd\u6761\u4ef6\u4e0b\uff0c\u5bf9\u5e94\u7684\u4ee3\u7801\u9700\u8981\u7f16\u8bd1\u6216\u8005\u4e0d\u9700\u8981\u7f16\u8bd1\u3002\u6761\u4ef6\u7f16\u8bd1\u6307\u4ee4\u7684\u79cd\u7c7b\u6709\u5f88\u591a\uff0c\u8fd9\u91cc\u6211\u4eec\u53ea\u4ecb\u7ecd\u4e09\u79cd\u6700\u4e3a\u5e38\u7528\u7684\uff1a #ifdef \u3001 #ifndef_ \u548c #endif \u3002 #ifdef \u6307\u4ee4\u4f1a\u8ba9\u9884\u5904\u7406\u5668\u68c0\u67e5\u67d0\u4e2a\u6807\u8bc6\u7b26\u662f\u5426\u88ab #define \u8fc7\uff0c\u5982\u679c\u662f\u7684\u8bdd\uff0c #ifdef \u548c #endif \u4e4b\u95f4\u7684\u4ee3\u7801\u5c06\u4f1a\u88ab\u7f16\u8bd1\uff0c\u5426\u5219\u8fd9\u4e9b\u4ee3\u7801\u4f1a\u88ab\u5ffd\u7565\u3002 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #define PRINT_JOE int main () { #ifdef PRINT_JOE std :: cout << \"Joe \\n \" ; // will be compiled since PRINT_JOE is defined #endif #ifdef PRINT_BOB std :: cout << \"Bob \\n \" ; // will be ignored since PRINT_BOB is not defined #endif return 0 ; } \u56e0\u4e3a PRINT_JOE \u88ab #defined \u4e86\uff0c\u56e0\u6b64 std::cout << \"Joe\\n\" \u4f1a\u88ab\u7f16\u8bd1\uff0c\u800c\u56e0\u4e3a PRINT_BOB \u6ca1\u6709\u88ab #defined \uff0c\u6240\u4ee5\u5bf9\u5e94\u7684 std::cout << \"Bob\\n\" \u4f1a\u88ab\u5ffd\u7565\u3002 #ifndef \u662f #ifdef \u7684\u53cd\u4e49\u8bcd\uff0c\u5b83\u63a7\u5236\u9884\u5904\u7406\u5668\u68c0\u67e5\u67d0\u4e2a\u6807\u8bc6\u7b26\u662f\u5426 \u6ca1\u6709 \u88ab #define \u3002 1 2 3 4 5 6 7 8 9 10 #include int main () { #ifndef PRINT_BOB std :: cout << \"Bob \\n \" ; #endif return 0 ; } \u4e0a\u9762\u7684\u7a0b\u5e8f\u4f1a\u6253\u5370 \u201cBob\u201d\uff0c\u56e0\u4e3a PRINT_BOB \u6ca1\u6709\u88ab #define_ \u8fc7\u3002 \u9664\u4e86 #ifdef PRINT_BOB \u548c #ifndef PRINT_BOB \u8fd9\u6837\u7684\u5f62\u5f0f\u4ee5\u5916\uff0c\u4f60\u4e5f\u53ef\u80fd\u4f1a\u9047\u5230 #if defined(PRINT_BOB) \u548c #if !defined(PRINT_BOB) \u8fd9\u6837\u7684\u5f62\u5f0f\u3002\u5b83\u4eec\u7684\u529f\u80fd\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u53ea\u4e0d\u8fc7\u540e\u8005\u770b\u4e0a\u53bb\u548cC++\u7684\u8bed\u6cd5\u98ce\u683c\u66f4\u52a0\u4e00\u81f4\u3002","title":"\u6761\u4ef6\u7f16\u8bd1"},{"location":"2-10-Introduction-to-the-preprocessor/#if-0","text":"\u8fd8\u6709\u4e00\u4e2a\u5e38\u7528\u7684\u6761\u4ef6\u7f16\u8bd1\u6307\u4ee4\u662f #if 0 \uff0c\u5b83\u53ef\u4ee5\u5c06\u5b83\u5305\u88f9\u8303\u56f4\u5185\u7684\u4ee3\u7801\u6392\u9664\u5728\u7f16\u8bd1\u4e4b\u5916\uff08\u5c31\u597d\u50cf\u8fd9\u4e9b\u4ee3\u7801\u88ab\u6ce8\u91ca\u6389\u4e86\u4e00\u6837\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { std :: cout << \"Joe \\n \" ; #if 0 // \u4ece\u6b64\u884c\u5f00\u59cb\u7684\u4ee3\u7801\u90fd\u4e0d\u7f16\u8bd1 std::cout << \"Bob\\n\"; std::cout << \"Steve\\n\"; #endif // \u76f4\u5230\u8fd9\u4e00\u884c\u4e3a\u6b62 return 0 ; } \u4e0a\u9762\u7684\u7a0b\u5e8f\u53ea\u4f1a\u6253\u5370 \u201cJoe\u201d\uff0c\u56e0\u4e3a\u201cBob\u201d \u548c \u201cSteve\u201d \u5747\u4f4d\u4e8e #if 0 \u5757\u4e2d\uff0c\u6240\u4ee5\u9884\u5904\u7406\u5668\u4f1a\u5c06\u5176\u6392\u9664\u5728\u7f16\u8bd1\u8303\u56f4\u5916\u3002 \u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u65b9\u4fbf\u7684\u201d\u6ce8\u91ca\u6389\u201c\u67d0\u4e9b\u5305\u542b\u4e86\u591a\u884c\u6ce8\u91ca\u7684\u4ee3\u7801\u4e86\uff08\u56e0\u4e3a\u591a\u884c\u6ce8\u91ca\u4e0d\u652f\u6301\u5d4c\u5957\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { std :: cout << \"Joe \\n \" ; #if 0 // Don't compile anything starting here std::cout << \"Bob\\n\"; /* Some * multi-line * comment here */ std::cout << \"Steve\\n\"; #endif // until this point return 0 ; }","title":"#if 0"},{"location":"2-10-Introduction-to-the-preprocessor/#_6","text":"\u4f60\u4e5f\u8bb8\u4f1a\u5bf9\u4e0b\u9762\u8fd9\u79cd\u5199\u6cd5\u5fc3\u5b58\u987e\u8651\uff1a 1 2 3 4 #define PRINT_JOE #ifdef PRINT_JOE // ... \u65e2\u7136\u6211\u4eec\u5b9a\u4e49\u4e86 PRINT_JOE \u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u9884\u5904\u7406\u5668\u4e0d\u4f1a\u5c06 #ifdef PRINT_JOE \u4e2d\u7684 PRINT_JOE \u66ff\u6362\u6389\u5462\uff1f \u5b8f\u5b9a\u4e49\u7684\u66ff\u6362\u53ea\u9488\u5bf9\u4e0e\u666e\u901a\u4ee3\u7801\u3002\u5176\u4ed6\u9884\u5904\u7406\u5668\u7684\u6307\u4ee4\u662f\u4e0d\u4f1a\u53d7\u5230\u5f71\u54cd\u7684\uff0c\u56e0\u6b64 #ifdef PRINT_JOE \u4e2d\u7684 PRINT_JOE \u662f\u4f1a\u4fdd\u7559\u7684\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 #define FOO 9 // \u5b8f\u66ff\u6362\u4e3a\u7a7a #ifdef FOO // \u6b64\u5904\u7684 FOO \u4e0d\u4f1a\u88ab\u66ff\u6362\u56e0\u4e3a\u5b83\u5c5e\u4e8e\u9884\u5904\u7406\u5668\u6307\u4ee4 std :: cout << FOO ; // \u6b64\u5904\u7684FOO\u4f1a\u88ab\u66ff\u6362\u56e0\u4e3a\u5b83\u5c5e\u4e8e\u666e\u901a\u4ee3\u7801 #endif \u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\uff0c\u9884\u5904\u7406\u5668\u8f93\u51fa\u7684\u7ed3\u679c\u4e2d\u4e0d\u5305\u542b\u4efb\u4f55\u9884\u5904\u7406\u5668\u6307\u4ee4\u2014\u2014\u5b83\u4eec\u90fd\u4f1a\u5728\u7f16\u8bd1\u524d\u88ab\u5904\u7406\u6389\uff0c\u6bd4\u8f83\u7f16\u8bd1\u5668\u5e76\u4e0d\u77e5\u9053\u8981\u5982\u4f55\u5904\u7406\u8fd9\u4e9b\u6307\u4ee4\u3002","title":"\u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u9884\u5904\u7406\u5668\u6307\u4ee4"},{"location":"2-10-Introduction-to-the-preprocessor/#_7","text":"\u9884\u5904\u7406\u5668\u6307\u4ee4\u7684\u89e3\u6790\u4f1a\u5728\u7f16\u8bd1\u5668\u5b8c\u6210\uff08\u4ece\u4e0a\u5230\u4e0b\u9010\u6587\u4ef6\u5730\uff09\u3002 \u8003\u8651\u5982\u4e0b\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void foo () { #define MY_NAME \"Alex\" } int main () { std :: cout << \"My name is: \" << MY_NAME ; return 0 ; } \u5c3d\u7ba1\u770b\u4e0a\u53bb #define MY_NAME \u201cAlex\u201d \u50cf\u662f\u88ab\u5b9a\u4e49\u5230\u4e86 foo \u51fd\u6570\u4e2d\uff0c\u4f46\u9884\u5904\u7406\u5668\u5e76\u4e0d\u4f1a\u610f\u8bc6\u5230\u8fd9\u4e00\u70b9\uff0c\u56e0\u4e3a\u5b83\u4e0d\u7406\u89e3\u201d\u51fd\u6570\u201c\u4ee5\u53ca\u5176\u4ed6C++\u7684\u8bed\u6cd5\u6982\u5ff5\u3002\u56e0\u6b64\uff0c\u8fd9\u6bb5\u4ee3\u7801\u7684\u6548\u679c\u597d\u6211\u4eec\u5728\u51fd\u6570\u4e4b\u524d\u6216\u662f\u4e4b\u540e\u5b9a\u4e49 #define MY_NAME \u201cAlex\u201d \u662f\u4e00\u6a21\u4e00\u6837\u7684\u3002\u5904\u4e8e\u53ef\u8bfb\u6027\u7684\u8003\u8651\uff0c\u4e00\u822c #define \u4e0d\u4f1a\u88ab\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8\u3002 \u5f53\u9884\u5904\u7406\u5668\u7684\u5de5\u4f5c\u5b8c\u6210\u540e\uff0c\u6240\u6709\u8fd9\u4e9b #define \u7684\u6807\u8bc6\u7b26\u5c31\u90fd\u88ab\u4e22\u5f03\u4e86\u3002\u56e0\u6b64\u8fd9\u4e9b\u6807\u8bc6\u7b26\u53ea\u6709\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u662f\u6709\u6548\u7684\uff0c\u5b9a\u4e49\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u7684\u5b8f\u4e0d\u4f1a\u5f71\u54cd\u5230\u5176\u4ed6\u6587\u4ef6\u3002 \u8003\u8651\u8fd9\u4e2a\u4f8b\u5b50\uff1a function.cpp: 1 2 3 4 5 6 7 8 9 10 11 #include void doSomething () { #ifdef PRINT std :: cout << \"Printing!\" ; #endif #ifndef PRINT std :: cout << \"Not printing!\" ; #endif } main.cpp: 1 2 3 4 5 6 7 8 9 10 void doSomething (); // forward declaration for function doSomething() #define PRINT int main () { doSomething (); return 0 ; } \u4e0a\u9762\u4ee3\u7801\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 Not printing! \u5c3d\u7ba1 PRINT \u88ab\u5b9a\u4e49\u5728\u4e86 main.cpp \u4e2d\uff0c\u4f46\u662f\u5b83\u4e1d\u6beb\u4e0d\u4f1a\u5f71\u54cd function.cpp \uff08\u5bf9 PRINT \u7684\u5b9a\u4e49\u53ea\u5728 main.cpp \u4e2d\u6709\u6548\uff09\u3002\u6211\u4eec\u4f1a\u5728 2.12 - \u5934\u6587\u4ef6\u91cd\u590d\u5305\u542b\u4fdd\u62a4 \u4e2d\u63a2\u8ba8\u5176\u5f71\u54cd\u3002","title":"\u5b8f\u5b9a\u4e49\u7684\u4f5c\u7528\u57df"},{"location":"2-11-Header-files/","tags":["header"],"text":"Key Takeaway \u5934\u6587\u4ef6\u4e2d\u4e00\u822c\u4e0d\u5e94\u8be5\u5305\u542b\u51fd\u6570\u548c\u53d8\u91cf\u7684\u5b9a\u4e49\uff0c\u4ee5\u4fbf\u9075\u5faa\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\uff0c\u9664\u975e\u662f\u7b26\u53f7\u5e38\u91cf \u7531\u4e8e 2-12-Header-guards#\u5934\u6587\u4ef6\u9632\u8303\u4e0d\u80fd\u9632\u6b62\u5934\u6587\u4ef6\u88ab\u5305\u542b\u5230\u4e0d\u540c\u7684\u6587\u4ef6 \uff0c\u5934\u6587\u4ef6\u4e2d\u7684\u5b9a\u4e49\u53ef\u80fd\u4f1a\u5bfc\u81f4\u94fe\u63a5\u5668\u53d1\u73b0\u91cd\u5b9a\u4e49\u3002 \u6bcf\u4e2a\u5934\u6587\u4ef6\u90fd\u5e94\u8be5\u6709\u5176\u7279\u5b9a\u7684\u7528\u9014\u5e76\u4e14\u5c3d\u53ef\u80fd\u72ec\u7acb \u4f60\u7f16\u5199\u7684\u4efb\u4f55\u5934\u6587\u4ef6\u90fd\u5e94\u8be5\u80fd\u591f\u72ec\u7acb\u7f16\u8bd1\uff08\u5b83\u5e94\u5f53 #include \u5b83\u6240\u9700\u7684\u5168\u90e8\u4f9d\u8d56\uff09\uff1b \u6e90\u6587\u4ef6\u5e94\u8be5\u5305\u542b\u5176\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\uff08\u5982\u679c\u5b58\u5728\u7684\u8bdd\uff09\uff0c\u53ef\u4ee5\u786e\u4fdd\u5b9a\u4e49\u548c\u58f0\u660e\u4e0d\u5339\u914d\u7684\u95ee\u9898\u5728\u7f16\u8bd1\u65f6\u5c31\u80fd\u88ab\u53d1\u73b0 \u4f7f\u7528\u53cc\u5f15\u53f7\u6765\u5305\u542b\u4f60\u81ea\u5df1\u7f16\u5199\u7684\u5934\u6587\u4ef6\uff0c\u5219\u8be5\u6587\u4ef6\u5fc5\u987b\u4f4d\u4e8e\u5f53\u524d\u76ee\u5f55\u4e2d\u3002\u4f7f\u7528\u5c16\u62ec\u53f7\u5305\u542b\u7f16\u8bd1\u5668\u3001\u64cd\u4f5c\u7cfb\u7edf\u6216\u7b2c\u4e09\u65b9\u63d0\u4f9b\u7684\u5b89\u88c5\u5728\u7cfb\u7edf\u5176\u4ed6\u4f4d\u7f6e\u7684\u5934\u6587\u4ef6\u3002 \u5728\u4f7f\u7528\u6807\u51c6\u5e93\u5934\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u6ca1\u6709 .h \u7248\u672c\u7684\u5934\u6587\u4ef6\u3002\u5bf9\u4e8e\u7528\u6237\u81ea\u5df1\u7f16\u5199\u7684\u5934\u6587\u4ef6\uff0c\u4ecd\u7136\u9700\u8981\u4f7f\u7528 .h \u540e\u7f00\u3002 \u4e0d\u8981 #include .cpp \u6587\u4ef6\u3002 \u5bf9\u4e8e\u7f16\u8bd1\u6240\u9700\u7684\u5934\u6587\u4ef6\uff0c\u6bcf\u4e00\u4e2a\u90fd\u5e94\u8be5\u88ab\u660e\u786e\u5730\u4f7f\u7528 #include \u5305\u542b\u8fdb\u6765\u3002\u4e0d\u8981\u4f9d\u8d56\u88ab\u95f4\u63a5\u5305\u542b\u7684\u5934\u6587\u4ef6\u3002 \u4e3a\u4e86\u6700\u5927\u7a0b\u5ea6\u51cf\u5c11\u5934\u6587\u4ef6\u6ca1\u6709\u6b63\u786e\u5305\u542b\u5f15\u8d77\u7684\u7f16\u8bd1\u5668\u62a5\u9519\uff0c\u8bf7\u6309\u7167\u5982\u4e0b\u987a\u5e8f\u5305\u542b\u5934\u6587\u4ef6\uff1a \u5148\u5305\u542b\u6e90\u6587\u4ef6\u5bf9\u5e94\u7684\u5934\u6587\u4ef6 \u518d\u5305\u542b\u9879\u76ee\u6240\u9700\u7684\u5176\u4ed6\u5934\u6587\u4ef6 \u518d\u5305\u542b\u7b2c\u4e09\u65b9\u5e93\u7684\u5934\u6587\u4ef6 \u518d\u5305\u542b\u6807\u51c6\u5e93\u5934\u6587\u4ef6 \u5934\u6587\u4ef6\u53ca\u5176\u7528\u9014 \u968f\u7740\u7a0b\u5e8f\u8d8a\u6765\u8d8a\u5927\uff08\u4ee5\u53ca\u8d8a\u6765\u8d8a\u591a\u7684\u6587\u4ef6\u88ab\u4f7f\u7528\uff09\uff0c\u4e3a\u6bcf\u4e2a\u88ab\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u521b\u5efa \u524d\u5411\u58f0\u660e \u4f1a\u53d8\u5f97\u975e\u5e38\u9ebb\u70e6\u3002\u5982\u679c\u80fd\u591f\u5c06\u6240\u6709\u7684\u524d\u5411\u58f0\u660e\u90fd\u653e\u5728\u4e00\u4e2a\u6587\u4ef6\u91cc\uff0c\u7136\u540e\u5728\u9700\u8981\u4f7f\u7528\u7684\u65f6\u5019\u5c06\u5176\u5bfc\u5165\uff0c\u5c82\u4e0d\u7f8e\u54c9\uff1f C++ \u4ee3\u7801\u6587\u4ef6(\u6269\u5c55\u540d\u4e3a .cpp ) \u5e76\u4e0d\u662f C++\u9879\u76ee\u4e2d\u552f\u4e00\u5e38\u89c1\u7684\u6587\u4ef6\u7c7b\u578b\u3002\u5934\u6587\u4ef6\u901a\u5e38\u4ee5 .h \u6269\u5c55\u540d\u7ed3\u5c3e\uff0c\u4f46\u662f\u6709\u65f6\u5019\u4f60\u4e5f\u4f1a\u770b\u5230 .hpp \u6269\u5c55\u540d\u7684\u5934\u6587\u4ef6\uff0c\u751a\u81f3\u6709\u4e9b\u90fd\u6ca1\u6709\u6269\u5c55\u540d\u3002 \u8fd9\u7c7b\u5934\u6587\u4ef6\u7684\u4e3b\u8981\u4f5c\u7528\u5c31\u662f\u653e\u7f6e\u4ee3\u7801\u7684\u58f0\u660e\u3002 \u5173\u952e\u4fe1\u606f \u5934\u6587\u4ef6\u4f7f\u6211\u4eec\u53ef\u4ee5\u5c06\u6240\u6709\u7684\u524d\u5411\u58f0\u660e\u90fd\u653e\u5728\u4e00\u4e2a\u6587\u4ef6\u91cc\uff0c\u7136\u540e\u5728\u9700\u8981\u7684\u65f6\u5019\u5c06\u5176\u5bfc\u5165\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u591a\u6587\u4ef6\u7a0b\u5e8f\u4e2d\u907f\u514d\u5f88\u591a\u624b\u5de5\u52b3\u52a8\u3002 \u4f7f\u7528\u6807\u51c6\u5e93\u7684\u5934\u6587\u4ef6 \u8003\u8651\u5982\u4e0b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << \"Hello, world!\" ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u4f7f\u7528 std:: cout \u5c06 \u201cHello, world!\u201d \u6253\u5370\u5728\u63a7\u5236\u53f0\u4e0a\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u7684\u7a0b\u5e8f\u5e76\u6ca1\u6709\u5b9a\u4e49\u8fc7 std:: cout \u51fd\u6570\u5440\uff1f\u7f16\u8bd1\u5668\u5982\u4f55\u5f97\u77e5\u5b83\u7684\u5b9a\u4e49\u7684\u5462\uff1f \u5b9e\u9645\u4e0a std:: cout \u7684\u58f0\u660e\u88ab\u5b9a\u4e49\u5728 \u201ciostream\u201d \u5934\u6587\u4ef6\u4e2d\uff0c\u5f53\u6211\u4eec\u4f7f\u7528 #include \u6307\u4ee4\u7684\u65f6\u5019\uff0c\u5176\u5b9e\u662f\u5728\u544a\u8bc9\u9884\u5904\u7406\u5668\uff0c\u5c06\u8be5\u6587\u4ef6\u7684\u5185\u5bb9\uff08\u5176\u4e2d\u5c31\u5305\u62ec std:: cout \u7684\u524d\u5411\u58f0\u660e\uff09\u5168\u90e8\u62f7\u8d1d\u5230\u6b64\u5904\u3002 \u5173\u952e\u4fe1\u606f \u5f53\u6211\u4eec #include \u4e00\u4e2a\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u8be5\u6587\u4ef6\u7684\u5185\u5bb9\u4f1a\u88ab\u63d2\u5165\u5230\u6b64\u5904\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u4ece\u5176\u4ed6\u6587\u4ef6\u83b7\u53d6\u524d\u5411\u5b9a\u4e49\u3002 \u8003\u8651\u4e00\u4e0b\uff0c\u5982\u679c iostream \u5934\u6587\u4ef6\u4e0d\u5b58\u5728\u4f1a\u600e\u6837\uff1f\u90a3\u4e48\u4f60\u6bcf\u6b21\u4f7f\u7528 std:: cout \u7684\u65f6\u5019\uff0c\u90fd\u5fc5\u987b\u624b\u5de5\u5c06\u6240\u6709\u548c std:: cout \u76f8\u5173\u7684\u58f0\u660e\u90fd\u8f93\u5165\u6216\u62f7\u8d1d\u5230\u6587\u4ef6\u7684\u5f00\u5934\u90e8\u5206\u3002\u8fd9\u6837\u4e0d\u4ec5\u9ebb\u70e6\uff0c\u8fd8\u9700\u8981\u4f7f\u7528\u8005\u77e5\u9053 std:: cout \u5b9e\u73b0\u7684\u7ec6\u8282\uff0c\u8fd9\u5c06\u4f1a\u662f\u975e\u5e38\u975e\u5e38\u5927\u7684\u5de5\u4f5c\u91cf\u3002\u66f4\u7cdf\u7684\u662f\uff0c\u4e00\u65e6\u51fd\u6570\u539f\u578b\u53d1\u751f\u4e86\u6539\u53d8\uff0c\u6211\u4eec\u5fc5\u987b\u624b\u52a8\u66f4\u65b0\u5168\u90e8\u7684\u58f0\u660e\u3002\u6240\u4ee5\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u83ab\u8fc7\u4e8e\u76f4\u63a5\u4f7f\u7528 #include \uff01 \u5bf9\u4e8e\u51fd\u6570\u548c\u53d8\u91cf\u6765\u8bf4\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b83\u4eec\u7684\u5934\u6587\u4ef6\u4e2d\u901a\u5e38\u53ea\u5305\u542b\u58f0\u660e\uff0c\u800c\u4e0d\u5305\u542b\u51fd\u6570\u548c\u53d8\u91cf\u7684\u5b9a\u4e49\uff08\u5426\u5219\u53ef\u80fd\u4f1a\u8fdd\u53cd \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \uff09\u3002 std:: cout \u88ab\u524d\u5411\u5b9a\u4e49\u5728 iostream \u5934\u6587\u4ef6\u4e2d\u5e76\u4f5c\u4e3a C++\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u800c\u6807\u51c6\u5e93\u5219\u4f1a\u5728\u7a0b\u5e8f\u7f16\u8bd1\u65f6\u88ab\u81ea\u52a8\u94fe\u63a5\u3002 \u6700\u4f73\u5b9e\u8df5 \u5934\u6587\u4ef6\u4e2d\u4e00\u822c\u4e0d\u5e94\u8be5\u5305\u542b\u51fd\u6570\u548c\u53d8\u91cf\u7684\u5b9a\u4e49\uff0c\u4ee5\u4fbf\u9075\u5faa\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u3002\u4f8b\u5916\u7684\u662f \u7b26\u53f7\u5e38\u91cf (\u5728 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \u4e2d\u4f1a\u8fdb\u884c\u4ecb\u7ecd)\u3002 \u7f16\u5199\u5934\u6587\u4ef6 \u73b0\u5728\uff0c\u56de\u60f3\u4e00\u4e0b\u4e4b\u524d\u8bfe\u7a0b\u4e2d\u4f7f\u7528\u8fc7\u7684\u7a0b\u5e8f\u3002\u8be5\u7a0b\u5e8f\u5305\u542b\u4e24\u4e2a\u6587\u4ef6 add.cpp \u548c main.cpp \uff1a add.cpp 1 2 3 4 int add ( int x , int y ) { return x + y ; } main.cpp 1 2 3 4 5 6 7 8 9 #include int add ( int x , int y ); // forward declaration using function prototype int main () { std :: cout << \"The sum of 3 and 4 is \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } \uff08\u5982\u679c\u4f60\u60f3\u8981\u4e3a\u8fd9\u4e24\u4e2a\u6587\u4ef6\u91cd\u65b0\u521b\u5efa\u4e00\u4e2a\u9879\u76ee\uff0c\u4e0d\u8981\u5fd8\u8bb0\u628a add.cpp \u6587\u4ef6\u52a0\u5230\u9879\u76ee\u4e2d\uff0c\u8fd9\u6837\u4f60\u624d\u80fd\u7f16\u8bd1\u5b83\uff09\u3002 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u524d\u5411\u58f0\u660e\u4ee5\u4fbf\u8ba9\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1 main.cpp \u65f6\u80fd\u591f\u77e5\u6653 add \u7684\u5b9a\u4e49\u3002\u6b63\u5982\u4e4b\u524d\u4ecb\u7ecd\u7684\u90a3\u6837\uff0c\u5982\u679c\u4e3a\u6bcf\u4e00\u4e2a\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u90fd\u521b\u5efa\u524d\u5411\u58f0\u660e\uff0c\u5c06\u4f1a\u975e\u5e38\u9ebb\u70e6\uff1a \u5176\u5b9e\uff0c\u53ea\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u5934\u6587\u4ef6\u5c31\u53ef\u4ee5\u4e00\u52b3\u6c38\u9038\u89e3\u51b3\u4e0a\u8ff0\u70e6\u607c\u3002\u7f16\u5199\u5934\u6587\u4ef6\u6bd4\u4f60\u60f3\u8c61\u7684\u8981\u7b80\u5355\u7684\u591a\uff0c\u5b83\u53ea\u5305\u542b\u4e24\u90e8\u5206\uff1a \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e \uff0c\u7a0d\u540e\u6211\u4eec\u4f1a\u8be6\u7ec6\u4ecb\u7ecd(\u53c2\u8003\uff1a 2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e )\u3002 \u5934\u6587\u4ef6\u7684\u5b9e\u9645\u5185\u5bb9\uff0c\u5373\u6240\u6709\u6211\u4eec\u5e0c\u671b\u80fd\u591f\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u88ab\u8bbf\u95ee\u7684\u6807\u8bc6\u7b26\u7684\u524d\u5411\u58f0\u660e\u3002 \u5c06\u5934\u6587\u4ef6\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u7684\u65b9\u6cd5\u548c\u6dfb\u52a0\u6e90\u6587\u4ef6\u5dee\u4e0d\u591a\uff08\u53c2\u8003\uff1a 2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f \uff09\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f IDE\uff0c\u53ef\u4ee5\u53c2\u8003\u4e4b\u524d\u8bfe\u7a0b\u4e2d\u63d0\u5230\u7684\u6b65\u9aa4\uff0c\u53ea\u4e0d\u8fc7\u5728\u9009\u62e9 Source \u7684\u65f6\u5019\uff0c\u8981\u6539\u4e3a\u9009\u62e9 Header \u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u547d\u4ee4\u884c\u73af\u5883\uff0c\u5219\u53ea\u9700\u8981\u7528\u7f16\u8f91\u5668\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u5373\u53ef\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u521b\u5efa\u5934\u6587\u4ef6\u7684\u65f6\u5019\u8bf7\u4f7f\u7528 .h \u540e\u7f00\u3002 \u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u901a\u5e38\u662f\u6210\u5bf9\u51fa\u73b0\u7684\uff0c\u5934\u6587\u4ef6\u4f1a\u4e3a\u5bf9\u5e94\u7684\u6e90\u6587\u4ef6\u63d0\u4f9b\u524d\u5411\u58f0\u660e\u3002\u56e0\u4e3a\u5934\u6587\u4ef6\u5305\u542b\u7684\u662f add.cpp \u4e2d\u51fd\u6570\u7684\u58f0\u660e\uff0c\u56e0\u6b64\u5934\u6587\u4ef6\u547d\u540d\u4e3a add.h \u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u6210\u5bf9\u51fa\u73b0\uff08\u4f8b\u5982\uff0cadd.h \u548c add.cpp\uff09\uff0c\u5b83\u4eec\u5e94\u8be5\u662f\u540c\u540d\u4f46\u4e0d\u540c\u540e\u7f00\u7684\u6587\u4ef6\u3002 \u5b8c\u6210\u540e\u7684\u5934\u6587\u4ef6\u5982\u4e0b\uff1a add.h: add.h 1 2 3 4 // 1) \u8fd9\u91cc\u5e94\u8be5\u662f\u5934\u6587\u4ef6\u5305\u542b\u4fdd\u62a4\uff0c\u4f46\u662f\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u53ea\u6709\u4e00\u4e2a\u5934\u6587\u4ef6\uff0c\u4e3a\u4e86\u7b80\u5316\u8fd9\u91cc\u5c31\u7701\u7565\u4e86 // 2) .h \u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u58f0\u660e\u90fd\u5199\u5728\u8fd9\u91cc int add ( int x , int y ); // \u51fd\u6570\u539f\u578b\u2014\u2014\u4e0d\u8981\u5fd8\u8bb0\u5206\u53f7\uff01 \u4e3a\u4e86\u5728 main.cpp \u4e2d\u4f7f\u7528\u8be5\u5934\u6587\u4ef6\uff0c\u6211\u4eec\u9700\u8981 #include (\u8fd9\u91cc\u9700\u8981\u4f7f\u7528\u5f15\u53f7\u800c\u975e\u5c16\u62ec\u53f7)\u3002 main.cpp 1 2 3 4 5 6 7 8 #include \"add.h\" // \u5c06 add.h \u63d2\u5165\u5230\u6b64\u4f4d\u7f6e\u3002\u6ce8\u610f\u4f7f\u7528\u53cc\u5f15\u53f7\u3002 #include int main () { std :: cout << \"The sum of 3 and 4 is \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } add.cpp 1 2 3 4 5 6 #include \"add.h\" // add.h \u7684\u5185\u5bb9\u4f1a\u88ab\u62f7\u8d1d\u5230\u8fd9\u91cc\u3002\u6ce8\u610f\u4f7f\u7528\u53cc\u5f15\u53f7\u3002 int add ( int x , int y ) { return x + y ; } \u5f53\u9884\u5904\u7406\u5668\u5904\u7406\u5230 #include \"add.h\" \u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u628a add.h \u4e2d\u7684\u5185\u5bb9\u90fd\u62f7\u8d1d\u5230\u8fd9\u91cc\u3002\u56e0\u4e3a add.h \u5305\u542b\u4e86\u51fd\u6570 add \u7684\u58f0\u660e\uff0c\u6240\u4ee5\u8be5\u58f0\u660e\u5c31\u4f1a\u88ab\u62f7\u8d1d\u5230\u4e86 main.cpp \u4e2d\u3002\u6700\u7ec8\u7684\u6548\u679c\uff0c\u5c31\u548c\u4e4b\u524d\u76f4\u63a5\u5c06\u58f0\u660e\u5199\u5728 main.cpp \u9876\u90e8\u662f\u4e00\u6837\u7684\u3002 \u73b0\u5728\uff0c\u6211\u4eec\u7684\u7a0b\u5e8f\u5c31\u53ef\u4ee5\u88ab\u6b63\u786e\u7684\u7f16\u8bd1\u548c\u94fe\u63a5\u4e86\u3002 \u6e90\u6587\u4ef6\u9700\u8981\u5305\u542b\u5176\u5bf9\u5e94\u7684\u5934\u6587\u4ef6 C++ \u4e2d\u7684\u6700\u4f73\u5b9e\u8df5\u4e4b\u4e00\u5c31\u662f\u6e90\u6587\u4ef6\u5e94\u8be5\u5305\u542b\u5176\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\uff08\u5982\u679c\u5b58\u5728\u7684\u8bdd\uff09\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c add.cpp \u5e94\u8be5\u5305\u542b add.h \u3002 \u8fd9\u4e48\u505a\u53ef\u4ee5\u4f7f\u5f97\u6709\u4e9b\u95ee\u9898\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u88ab\u53d1\u73b0\uff0c\u800c\u4e0d\u662f\u7559\u5230\u94fe\u63a5\u65f6\u518d\u53d1\u73b0\u3002\u4f8b\u5982\uff1a something.h 1 int something ( int ); // \u58f0\u660e\u7684\u8fd4\u56de\u7c7b\u578b\u662f int something.cpp 1 2 3 4 5 #include \"something.h\" void something ( int ) // \u9519\u8bef: \u9519\u8bef\u7684\u8fd4\u56de\u7c7b\u578b { } \u56e0\u4e3a something.cpp #includes \u4e86 something.h \uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u53d1\u73b0 something() \u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u5339\u914d\u3002\u800c\u5982\u679c something.cpp \u6ca1\u6709 #include something.h \uff0c\u90a3\u4e48\u6211\u4eec\u5fc5\u987b\u8981\u7b49\u5230\u94fe\u63a5\u65f6\uff0c\u8be5\u95ee\u9898\u624d\u4f1a\u88ab\u94fe\u63a5\u5668\u53d1\u73b0\uff0c\u8fd9\u65e0\u7591\u4f1a\u6d6a\u8d39\u65f6\u95f4\u3002\u5176\u4ed6\u4f8b\u5b50\u53ef\u4ee5\u53c2\u8003 \u8fd9\u4e2a\u8bc4\u8bba \u3002 \u6700\u4f73\u5b9e\u8df5 \u6e90\u6587\u4ef6\u9700\u8981\u5305\u542b\u5176\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\uff08\u5982\u679c\u6709\u7684\u8bdd\uff09\u3002 \u9519\u8bef\u6392\u67e5 \u5982\u679c\u7f16\u8bd1\u5668\u62a5\u544a\u4e86 add.h \u65e0\u6cd5\u88ab\u627e\u7684\u9519\u8bef\uff0c\u8bf7\u9996\u5148\u786e\u8ba4\u6587\u4ef6\u540d\u662f\u5426\u4e3a add.h \u3002\u6587\u4ef6\u540d\u53ef\u80fd\u4f1a\u88ab\u9519\u8bef\u5730\u8bbe\u7f6e\u4e3a add (\u65e0\u540e\u7f00) \u6216 add.h.txt \u6216 add.hpp \uff0c\u8fd9\u53ef\u80fd\u53d6\u51b3\u4e8e\u4f60\u662f\u5982\u4f55\u521b\u5efa\u5b83\u4eec\u7684\u3002\u53e6\u5916\uff0c\u4e5f\u8981\u786e\u4fdd\u8be5\u5934\u6587\u4ef6\u548c\u5176\u4ed6\u6587\u4ef6\u4f4d\u4e8e\u76f8\u540c\u7684\u76ee\u5f55\u3002 \u5982\u679c\u94fe\u63a5\u5668\u62a5\u544a\u4e86 add \u51fd\u6570\u672a\u5b9a\u4e49\u7684\u9519\u8bef\uff0c\u8bf7\u786e\u4fdd add.cpp \u88ab\u6dfb\u52a0\u5230\u4e86\u9879\u76ee\u4e2d\uff0c\u8fd9\u6837 add \u51fd\u6570\u624d\u80fd\u591f\u88ab\u6b63\u786e\u94fe\u63a5\u3002 \u5c16\u62ec\u53f7 vs \u53cc\u5f15\u53f7 \u4f60\u53ef\u80fd\u4f1a\u597d\u5947\uff0c\u4e3a\u4ec0\u4e48 iostream \u4f7f\u7528\u7684\u662f\u5c16\u62ec\u53f7\uff0c\u800c add.h \u5c31\u9700\u8981\u4f7f\u7528\u53cc\u5f15\u53f7\u3002\u8fd9\u662f\u56e0\u4e3a\uff0c\u540c\u540d\u7684\u6587\u4ef6\u53ef\u80fd\u4f1a\u5206\u5e03\u5728\u4e0d\u540c\u7684\u76ee\u5f55\u4e2d\u3002\u533a\u5206\u4f7f\u7528\u5c16\u62ec\u53f7\u548c\u53cc\u5f15\u7528\uff0c\u53ef\u4ee5\u544a\u8bc9\u7f16\u8bd1\u5668\u5230\u54ea\u91cc\u5bfb\u627e\u5934\u6587\u4ef6\u3002 \u5f53\u4f7f\u7528\u5c16\u62ec\u53f7\u7684\u65f6\u5019\uff0c\u5176\u5b9e\u662f\u5728\u544a\u8bc9\u9884\u5904\u7406\u5668\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\u5e76\u4e0d\u662f\u6211\u4eec\u7f16\u5199\u7684\u3002\u7f16\u8bd1\u5668\u53ea\u4f1a\u5728 include directories \u6307\u5b9a\u7684\u76ee\u5f55\u4e2d\u641c\u7d22\u3002 include directories \u662f\u9879\u76ee\u3001IDE\u6216\u7f16\u8bd1\u5668\u914d\u7f6e\u7684\u4e00\u90e8\u5206\uff0c\u9ed8\u8ba4\u7684\u8def\u5f84\u662f\u7531\u7f16\u8bd1\u5668\u6216\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684\u3002\u7f16\u8bd1\u5668\u5e76\u4e0d\u4f1a\u5728\u4f60\u7684\u9879\u76ee\u76ee\u5f55\u4e2d\u641c\u7d22\u5bf9\u996e\u7684\u5934\u6587\u4ef6\u3002 \u5f53\u4f7f\u7528\u53cc\u5f15\u53f7\u7684\u65f6\u5019\uff0c\u5176\u5b9e\u662f\u5728\u544a\u8bc9\u9884\u5904\u7406\u5668\u5934\u6587\u4ef6\u662f\u6211\u4eec\u81ea\u5df1\u7f16\u5199\u7684\u3002\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u641c\u7d22\u5f53\u524d\u76ee\u5f55\uff0c\u5982\u679c\u627e\u4e0d\u5230\u6240\u9700\u7684\u5934\u6587\u4ef6\uff0c\u5219\u4f1a\u5728 include directories \u4e2d\u8fdb\u884c\u67e5\u627e\u3002 \u6cd5\u5219 \u4f7f\u7528\u53cc\u5f15\u53f7\u6765\u5305\u542b\u4f60\u81ea\u5df1\u7f16\u5199\u7684\u5934\u6587\u4ef6\uff0c\u5219\u8be5\u6587\u4ef6\u5fc5\u987b\u4f4d\u4e8e\u5f53\u524d\u76ee\u5f55\u4e2d\u3002\u4f7f\u7528\u5c16\u62ec\u53f7\u5305\u542b\u7f16\u8bd1\u5668\u3001\u64cd\u4f5c\u7cfb\u7edf\u6216\u7b2c\u4e09\u65b9\u63d0\u4f9b\u7684\u5b89\u88c5\u5728\u7cfb\u7edf\u5176\u4ed6\u4f4d\u7f6e\u7684\u5934\u6587\u4ef6\u3002 \u4e3a\u4ec0\u4e48 iostream \u6ca1\u6709 .h \u540e\u7f00? \u53e6\u5916\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u662f\uff1a\u201d\u4e3a\u4ec0\u4e48 iostream \uff08\u6216\u8005\u5176\u4ed6\u6807\u51c6\u5e93\u5934\u6587\u4ef6\uff09\u6ca1\u6709 .h \u540e\u7f00\u5462\uff1f\u201c\u3002\u8fd9\u662f\u56e0\u4e3a iostream.h \u548c iostream \u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u5934\u6587\u4ef6\u3002\u8fd9\u6d89\u53ca\u5230\u4e00\u4e9b\u5386\u53f2\u77e5\u8bc6\u3002 \u5728 C++ \u521a\u88ab\u521b\u5efa\u51fa\u6765\u7684\u65f6\u5019\uff0c\u6240\u6709\u7684\u6807\u51c6\u8fd0\u884c\u65f6\u5e93\u90fd\u662f\u4ee5 .h \u7ed3\u5c3e\u7684\u3002\u6700\u521d\u7248\u672c\u7684 cout \u548c cin \u4e5f\u90fd\u88ab\u5b9a\u4e49\u5728 iostream.h \u4e2d\u3002\u5728ANSI\u59d4\u5458\u4f1a\u5bf9\u8bed\u8a00\u8fdb\u884c\u6807\u51c6\u5316\u7684\u65f6\u5019\uff0c\u5b83\u4eec\u51b3\u5b9a\u8981\u5c06\u6807\u51c6\u5e93\u4e2d\u7684\u51fd\u6570\u90fd\u79fb\u5230 std \u547d\u540d\u7a7a\u95f4\u4e2d\u4ee5\u907f\u514d\u5176\u548c\u7528\u6237\u5b9a\u4e49\u7684\u51fd\u6570\u4ea7\u751f\u547d\u540d\u51b2\u7a81\u3002\u4e0d\u8fc7\uff0c\u73b0\u5b9e\u95ee\u9898\u662f\uff0c\u5982\u679c\u6b64\u65f6\u5c06\u8fd9\u4e9b\u51fd\u6570\u90fd\u79fb\u52a8\u5230 std \u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u90a3\u4e48\u4e4b\u524d\u5199\u7684\u4ee3\u7801\u5c31\u90fd\u4e0d\u80fd\u5de5\u4f5c\u4e86\uff01 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ea\u80fd\u4f7f\u7528\u4e00\u7ec4\u65b0\u7684\u5934\u6587\u4ef6\uff0c\u5b83\u4eec\u5177\u6709\u76f8\u540c\u7684\u540d\u5b57\uff0c\u4f46\u6ca1\u6709 .h \u540e\u7f00\u3002\u8fd9\u4e9b\u65b0\u7684\u5934\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\uff0c\u90fd\u5728 std \u4f5c\u7528\u57df\u4e2d\u3002\u8fd9\u6837\uff0c\u4f7f\u7528 #include \u7684\u8001\u4ee3\u7801\u5c31\u4e0d\u9700\u8981\u8fdb\u884c\u4efb\u4f55\u4fee\u6539\u4e86\uff0c\u800c\u65b0\u7f16\u5199\u7684\u4ee3\u7801\u5219\u9700\u8981\u4f7f\u7528 #include \u3002 \u6b64\u5916\uff0c\u4ece C \u8bed\u8a00\u7ee7\u627f\u8fc7\u6765\u7684\u6807\u51c6\u5e93\u5219\u88ab\u6dfb\u52a0\u4e86\u4e00\u4e2a c \u4f5c\u4e3a\u524d\u7f00(\u4f8b\u5982 stdlib.h \u53d8\u6210\u4e86 cstdlib )\u3002\u8fd9\u4e9b\u5e93\u4e2d\u7684\u51fd\u6570\u4e5f\u540c\u6837\u88ab\u79fb\u52a8\u5230\u4e86 std \u6807\u51c6\u5e93\u4e2d\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u4f7f\u7528\u6807\u51c6\u5e93\u5934\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u6ca1\u6709 .h \u7248\u672c\u7684\u5934\u6587\u4ef6\u3002\u5bf9\u4e8e\u7528\u6237\u81ea\u5df1\u7f16\u5199\u7684\u5934\u6587\u4ef6\uff0c\u4ecd\u7136\u9700\u8981\u4f7f\u7528 .h \u540e\u7f00\u3002 include \u5176\u4ed6\u76ee\u5f55\u4e2d\u7684\u5934\u6587\u4ef6 \u53e6\u5916\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\u662f\u5982\u4f55\u5305\u542b\u5176\u4ed6\u76ee\u5f55\u4e2d\u7684\u5934\u6587\u4ef6\u3002 \u4e00\u79cd\u53ef\u884c\uff08\u4f46\u4e0d\u597d\u7684\uff09\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u6765\u6307\u5b9a #include \u7684\u5185\u5bb9\uff0c\u4f8b\u5982\uff1a 1 2 #include \"headers/myHeader.h\" #include \"../moreHeaders/myOtherHeader.h\" \u8fd9\u4e48\u505a\u7a0b\u5e8f\u662f\u53ef\u4ee5\u7f16\u8bd1\u7684\uff08\u5047\u8bbe\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\u786e\u5b9e\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e0b\uff09\uff0c\u4f46\u662f\u5b83\u7684\u7f3a\u70b9\u4e5f\u5f88\u660e\u663e\uff0c\u4f60\u5fc5\u987b\u5728\u4ee3\u7801\u4e2d\u63cf\u8ff0\u5b9e\u9645\u7684\u76ee\u5f55\u7ed3\u6784\u3002\u5982\u679c\u76ee\u5f55\u7ed3\u6784\u66f4\u65b0\u4e86\uff0c\u90a3\u4ee3\u7801\u4e5f\u5c31\u4e0d\u80fd\u5de5\u4f5c\u4e86\u3002 \u66f4\u597d\u7684\u529e\u6cd5\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\u6216\u8005 IDE\u6709\u4e9b\u5934\u6587\u4ef6\u5b58\u653e\u5728\u5176\u4ed6\u4f4d\u7f6e\uff0c\u6b64\u65f6\u5f53\u5b83\u4eec\u65e0\u6cd5\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u88ab\u627e\u5230\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u5230\u6307\u5b9a\u7684\u76ee\u5f55\u4e2d\u67e5\u627e\u3002\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u5728 IDE \u4e2d\u8bbe\u7f6e\u9879\u76ee\u7684 include path \u6216 search directory \u6765\u5b9e\u73b0\u3002 For Visual Studio users \u53f3\u952e\u5355\u51fb\u9879\u76ee\u7684 Solution Explorer \u7136\u540e\u9009\u62e9 Properties \u7136\u540e\u9009\u62e9 VC++ Directories \u9009\u9879\u5361\u3002\u5728\u8fd9\u91cc\u4f60\u53ef\u4ee5\u770b\u5230 Include Directories \u5c06\u4f60\u5e0c\u671b\u7f16\u8bd1\u5668\u641c\u7d22\u7684\u5305\u542b\u4e86\u5934\u6587\u4ef6\u7684\u76ee\u5f55\u586b\u5199\u5230\u8fd9\u91cc\u5373\u53ef\u3002 For Code:: Blocks users \u5728 Code:: Blocks \u4e2d\uff0c\u9009\u62e9 Project \u83dc\u5355\u5e76\u9009\u62e9 Build Options \uff0c\u518d\u9009\u62e9 Search directories \u9009\u9879\u5361\uff0c\u5c06\u4f60\u5e0c\u671b\u7f16\u8bd1\u5668\u641c\u7d22\u7684\u5305\u542b\u4e86\u5934\u6587\u4ef6\u7684\u76ee\u5f55\u586b\u5199\u5230\u8fd9\u91cc\u5373\u53ef\u3002 For GCC/G++ users \u4f7f\u7528 g++ \u662f\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7 -I \u9009\u9879\u6765\u6307\u660e\u5934\u6587\u4ef6\u641c\u7d22\u8def\u5f84\uff1a 1 g ++ - o main - I / source / includes main . cpp \u8fd9\u4e48\u505a\u7684\u597d\u5904\u662f\uff0c\u5982\u679c\u4f60\u6539\u53d8\u4e86\u76ee\u5f55\u7ed3\u6784\uff0c\u90a3\u4e48\u53ea\u9700\u8981\u5728\u8bbe\u7f6e\u91cc\u9762\u4fee\u6539\u8def\u5f84\u5373\u53ef\uff0c\u800c\u4e0d\u5fc5\u5bf9\u4ee3\u7801\u4e2d\u6bcf\u4e00\u5904\u4f7f\u7528\u8be5\u5934\u6587\u4ef6\u7684\u5730\u65b9\u8fdb\u884c\u4fee\u6539\u3002 \u5934\u6587\u4ef6\u4e2d\u53ef\u4ee5\u5305\u542b\u5176\u4ed6\u5934\u6587\u4ef6 \u5934\u6587\u4ef6\u9700\u8981\u4f9d\u8d56\u5176\u4ed6\u5934\u6587\u4ef6\u4e2d\u7684\u5b9a\u4e49\u6216\u58f0\u660e\u4e5f\u662f\u5f88\u5e38\u89c1\u7684\u3002\u56e0\u6b64\uff0c\u5934\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528 #include \u6765\u5305\u542b\u5176\u4ed6\u5934\u6587\u4ef6\u3002 \u5f53\u4f60\u7684\u4ee3\u7801 #includes \u7b2c\u4e00\u4e2a\u5934\u6587\u4ef6\u65f6\uff0c\u4f60\u5176\u5b9e\u4e5f\u5305\u542b\u4e86\u8be5\u5934\u6587\u4ef6\u4e2d\u5305\u542b\u7684\u5176\u4ed6\u5934\u6587\u4ef6\uff08\u4ee5\u53ca\u8fd9\u6240\u6709\u5934\u6587\u4ef6\u4e2d\u5305\u542b\u7684\u5934\u6587\u4ef6\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff09\u3002\u8fd9\u4e9b\u989d\u5916\u7684\u5934\u6587\u4ef6\u6709\u65f6\u5019\u79f0\u4e3a \u95f4\u63a5\u5305\u542b(transitive includes) \uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u88ab\u9690\u5f0f\u5305\u542b\u8fdb\u6765\u7684\uff0c\u6211\u4eec\u660e\u6ca1\u6709\u6307\u660e\u54ea\u4e9b\u9700\u8981\u88ab\u5305\u542b\u3002 \u8fd9\u4e9b\u5934\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u5728\u4f60\u7684\u4ee3\u7801\u4e2d\u662f\u53ef\u7528\u7684\u3002\u4e0d\u8fc7\u4f60\u5e76\u4e0d\u5e94\u8be5\u4f9d\u8d56\u5b83\u4eec\uff0c\u6bd5\u7adf\u5b83\u4eec\u662f\u88ab\u95f4\u63a5\u5305\u542b\u8fdb\u6765\u7684\u3002\u8fd9\u4e9b\u5934\u6587\u4ef6\u53ef\u80fd\u4f1a\u53d8\u5316\uff0c\u4e5f\u53ef\u80fd\u5728\u4e0d\u540c\u7684\u7cfb\u7edf\u4e2d\u5b58\u5728\u5dee\u5f02\uff0c\u8fd9\u65f6\u5019\u4f60\u7684\u4ee3\u7801\u53ef\u80fd\u53ea\u80fd\u5728\u67d0\u5e73\u53f0\u4e0b\u624d\u80fd\u7f16\u8bd1\uff0c\u6216\u8005\u53ef\u80fd\u5728\u5c06\u6765\u65e0\u6cd5\u7f16\u8bd1\u3002\u907f\u514d\u4e0a\u8ff0\u95ee\u9898\u7684\u65b9\u6cd5\u4e5f\u5f88\u7b80\u5355\uff0c\u8bf7\u660e\u786e\u5305\u542b\u4f60\u6240\u9700\u8981\u7684\u5168\u90e8\u5934\u6587\u4ef6\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u4e8e\u7f16\u8bd1\u6240\u9700\u7684\u5934\u6587\u4ef6\uff0c\u6bcf\u4e00\u4e2a\u90fd\u5e94\u8be5\u88ab\u660e\u786e\u5730\u4f7f\u7528 #include \u5305\u542b\u8fdb\u6765\u3002\u4e0d\u8981\u4f9d\u8d56\u88ab\u95f4\u63a5\u5305\u542b\u7684\u5934\u6587\u4ef6\u3002 \u4e0d\u5e78\u7684\u662f\uff0c\u6211\u4eec\u5f88\u96be\u53bb\u7504\u522b\u7a76\u7adf\u54ea\u4e9b\u4ee3\u7801\u6b63\u5728\u4f9d\u8d56\u90a3\u4e9b\u88ab\u95f4\u63a5\u5f15\u5165\u7684\u5934\u6587\u4ef6\u3002 Q: \u6211\u6ca1\u6709 include \uff0c\u4f46\u662f\u7a0b\u5e8f\u4ecd\u7136\u80fd\u6b63\u5e38\u5de5\u4f5c\uff01\u4e3a\u4ec0\u4e48\uff1f \u8fd9\u4e2a\u95ee\u9898\u4e5f\u662f\u5e38\u5907\u95ee\u5230\u7684\u95ee\u9898\u4e4b\u4e00\u3002\u8fd9\u53ef\u80fd\u662f\u56e0\u4e3a\u4f60\u5305\u542b\u4e86\u67d0\u4e2a\u5934\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u6070\u5de7\u8be5\u6587\u4ef6\u4e5f\u5305\u542b\u4e86\u4f60\u6240\u9700\u7684\u90a3\u4e2a\u5934\u6587\u4ef6\uff0c\u6240\u4ee5\u4ee3\u7801\u53ef\u4ee5\u5de5\u4f5c\u3002\u5c3d\u7ba1\u4f60\u7684\u7a0b\u5e8f\u53ef\u4ee5\u7f16\u8bd1\uff0c\u6700\u4f73\u5b9e\u8df5\u544a\u8bc9\u6211\u4eec\uff0c\u8fd9\u79cd\u65b9\u5f0f\u5e76\u4e0d\u53ef\u9760\u3002\u5728\u4f60\u7684\u7535\u8111\u4e0a\u80fd\u7f16\u8bd1\uff0c\u4e0d\u4ee3\u8868\u5728\u5176\u4ed6\u7535\u8111\u4e0a\u4e5f\u80fd\u7f16\u8bd1\u3002 \u5934\u6587\u4ef6 #include \u7684\u987a\u5e8f \u5982\u679c\u4f60\u7684\u5934\u6587\u4ef6\u5185\u5bb9\u6ca1\u95ee\u9898\uff0c\u5e76\u4e14\u4e5f #include \u4e86\u5b83\u4eec\u6240\u4f9d\u8d56\u7684\u5176\u4ed6\u5934\u6587\u4ef6\uff0c\u90a3\u4e48\u5934\u6587\u4ef6\u5305\u542b\u7684\u987a\u5e8f\u5176\u5b9e\u5e76\u4e0d\u91cd\u8981\u3002 \u4e0d\u8fc7\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u5982\u4e0b\u573a\u666f\uff1a\u5934\u6587\u4ef6 A \u9700\u8981\u5934\u6587\u4ef6 B\u4e2d\u7684\u58f0\u660e\uff0c\u4f46\u662f\u5374\u5fd8\u8bb0\u5305\u542b B \u4e86\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u5728\u6e90\u6587\u4ef6\u4e2d\uff0c\u5148\u5305\u542b B \u518d\u5305\u542bA\uff0c\u90a3\u4e48\u7a0b\u5e8f\u662f\u53ef\u4ee5\u7f16\u8bd1\u7684\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u4f1a\u9996\u5148\u7f16\u8bd1\u5230B\u4e2d\u7684\u58f0\u660e\uff0c\u7136\u540e\u624d\u4f1a\u7f16\u8bd1\u5230A\u4e2d\u5bf9B\u6709\u4f9d\u8d56\u7684\u90a3\u4e9b\u4ee3\u7801\u3002 \u53cd\u4e4b\uff0c\u5982\u679c\u6211\u4eec\u5148\u5305\u542b\u5934\u6587\u4ef6 A\uff0c\u518d\u5305\u542b\u5934\u6587\u4ef6 B\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c31\u4f1a\u62a5\u9519\u4e86\uff0c\u56e0\u4e3a\u5b83\u4e0d\u80fd\u5728\u7f16\u8bd1 A \u65f6\u627e\u5230\u5176\u6240\u4f9d\u8d56\u7684 B \u4e2d\u7684\u58f0\u660e\u3002\u5176\u5b9e\u8fd9\u6b63\u662f\u6211\u4eec\u5e0c\u671b\u7684\u7ed3\u679c\uff0c\u56e0\u4e3a\u5b83\u628a\u95ee\u9898\u66b4\u9732\u4e86\u51fa\u6765\uff0c\u6211\u4eec\u4fbf\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u4fee\u590d\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e3a\u4e86\u6700\u5927\u7a0b\u5ea6\u51cf\u5c11\u5934\u6587\u4ef6\u6ca1\u6709\u6b63\u786e\u5305\u542b\u5f15\u8d77\u7684\u7f16\u8bd1\u5668\u62a5\u9519\uff0c\u8bf7\u6309\u7167\u5982\u4e0b\u987a\u5e8f\u5305\u542b\u5934\u6587\u4ef6\uff1a \u5148\u5305\u542b\u6e90\u6587\u4ef6\u5bf9\u5e94\u7684\u5934\u6587\u4ef6 \u518d\u5305\u542b\u9879\u76ee\u6240\u9700\u7684\u5176\u4ed6\u5934\u6587\u4ef6 \u518d\u5305\u542b\u7b2c\u4e09\u65b9\u5e93\u7684\u5934\u6587\u4ef6 \u518d\u5305\u542b\u6807\u51c6\u5e93\u5934\u6587\u4ef6 \u6bcf\u4e00\u7ec4\u5934\u6587\u4ef6\u90fd\u5e94\u8be5\u6309\u7167\u5b57\u6bcd\u8868\u987a\u5e8f\u6392\u5e8f \u8fd9\u6837\uff0c\u4efb\u4f55\u4e00\u4e2a\u4f60\u5b9a\u4e49\u7684\u5934\u6587\u4ef6\u5982\u679c\u6ca1\u6709 #include \u5176\u6240\u9700\u7684\u7b2c\u4e09\u65b9\u5e93\u6216\u6807\u51c6\u5e93\uff0c\u90a3\u4e48\u5f88\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u8fdb\u884c\u4fee\u590d\u4e86\u3002 \u5934\u6587\u4ef6\u6700\u4f73\u5b9e\u8df5 \u5173\u4e8e\u5982\u4f55\u4f7f\u7528\u548c\u521b\u5efa\u5934\u6587\u4ef6\uff0c\u6211\u4eec\u6709\u5982\u4e0b\u5efa\u8bae\uff1a \u59cb\u7ec8\u4f7f\u7528 \u5934\u6587\u4ef6\u91cd\u590d\u5305\u542b\u4fdd\u62a4 \uff1b \u4e0d\u8981\u5728\u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\u53d8\u91cf\u548c\u51fd\u6570 (\u5168\u5c40\u5e38\u91cf\u662f\u4e2a\u4f8b\u5916\u2014\u2014\u7a0d\u540e\u4ecb\u7ecd\uff09\uff1b \u5934\u6587\u4ef6\u548c\u5176\u5bf9\u5e94\u7684\u6e90\u6587\u4ef6\u5e94\u8be5\u5177\u6709\u76f8\u540c\u7684\u6587\u4ef6\u540d\uff08\u4f8b\u5982\uff0c grades.h \u548c grades.cpp \uff0c\u6ce8\u610f\u6269\u5c55\u540d\u662f\u4e0d\u540c\u7684\uff09\uff1b \u6bcf\u4e2a\u5934\u6587\u4ef6\u90fd\u5e94\u8be5\u6709\u5176\u7279\u5b9a\u7684\u7528\u9014\u5e76\u4e14\u5c3d\u53ef\u80fd\u72ec\u7acb\u3002\u4f8b\u5982\uff0c\u4e0eA\u76f8\u5173\u7684\u58f0\u660e\u5e94\u8be5\u653e\u5728 A.h \u4e2d\uff0c\u4e0eB\u76f8\u5173\u7684\u58f0\u660e\u5e94\u8be5\u653e\u5728 B.h \u4e2d\u3002\u8fd9\u6837\u5982\u679c\u4ee5\u540e\u4ec5\u4ec5\u9700\u8981\u4f7f\u7528A\u76f8\u5173\u7684\u529f\u80fd\uff0c\u5219\u65e0\u9700\u5305\u542b B.h \uff0c\u4e5f\u5c31\u4e0d\u4f1a\u6d89\u53ca\u5230\u4efb\u4f55\u4e0eB\u76f8\u5173\u7684\u51fd\u6570\u58f0\u660e\uff1b \u5bf9\u4e8e\u54ea\u4e9b\u51fd\u6570\u5e94\u8be5\u5305\u542b\u54ea\u4e9b\u5934\u6587\u4ef6\u8981\u505a\u5230\u5fc3\u4e2d\u6709\u6570\uff1b \u4f60\u7f16\u5199\u7684\u4efb\u4f55\u5934\u6587\u4ef6\u90fd\u5e94\u8be5\u80fd\u591f\u72ec\u7acb\u7f16\u8bd1\uff08\u5b83\u5e94\u5f53 #include \u5b83\u6240\u9700\u7684\u5168\u90e8\u4f9d\u8d56\uff09\uff1b \u53ea #include \u5fc5\u8981\u7684\u5934\u6587\u4ef6\uff08\u4e0d\u8981\u628a\u80fd\u5305\u542b\u7684\u5168\u90e8\u5305\u542b\u8fdb\u6765\uff09\uff1b \u4e0d\u8981 #include .cpp \u6587\u4ef6\u3002","title":"2.11 - \u5934\u6587\u4ef6"},{"location":"2-11-Header-files/#_1","text":"\u968f\u7740\u7a0b\u5e8f\u8d8a\u6765\u8d8a\u5927\uff08\u4ee5\u53ca\u8d8a\u6765\u8d8a\u591a\u7684\u6587\u4ef6\u88ab\u4f7f\u7528\uff09\uff0c\u4e3a\u6bcf\u4e2a\u88ab\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u521b\u5efa \u524d\u5411\u58f0\u660e \u4f1a\u53d8\u5f97\u975e\u5e38\u9ebb\u70e6\u3002\u5982\u679c\u80fd\u591f\u5c06\u6240\u6709\u7684\u524d\u5411\u58f0\u660e\u90fd\u653e\u5728\u4e00\u4e2a\u6587\u4ef6\u91cc\uff0c\u7136\u540e\u5728\u9700\u8981\u4f7f\u7528\u7684\u65f6\u5019\u5c06\u5176\u5bfc\u5165\uff0c\u5c82\u4e0d\u7f8e\u54c9\uff1f C++ \u4ee3\u7801\u6587\u4ef6(\u6269\u5c55\u540d\u4e3a .cpp ) \u5e76\u4e0d\u662f C++\u9879\u76ee\u4e2d\u552f\u4e00\u5e38\u89c1\u7684\u6587\u4ef6\u7c7b\u578b\u3002\u5934\u6587\u4ef6\u901a\u5e38\u4ee5 .h \u6269\u5c55\u540d\u7ed3\u5c3e\uff0c\u4f46\u662f\u6709\u65f6\u5019\u4f60\u4e5f\u4f1a\u770b\u5230 .hpp \u6269\u5c55\u540d\u7684\u5934\u6587\u4ef6\uff0c\u751a\u81f3\u6709\u4e9b\u90fd\u6ca1\u6709\u6269\u5c55\u540d\u3002 \u8fd9\u7c7b\u5934\u6587\u4ef6\u7684\u4e3b\u8981\u4f5c\u7528\u5c31\u662f\u653e\u7f6e\u4ee3\u7801\u7684\u58f0\u660e\u3002 \u5173\u952e\u4fe1\u606f \u5934\u6587\u4ef6\u4f7f\u6211\u4eec\u53ef\u4ee5\u5c06\u6240\u6709\u7684\u524d\u5411\u58f0\u660e\u90fd\u653e\u5728\u4e00\u4e2a\u6587\u4ef6\u91cc\uff0c\u7136\u540e\u5728\u9700\u8981\u7684\u65f6\u5019\u5c06\u5176\u5bfc\u5165\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u591a\u6587\u4ef6\u7a0b\u5e8f\u4e2d\u907f\u514d\u5f88\u591a\u624b\u5de5\u52b3\u52a8\u3002","title":"\u5934\u6587\u4ef6\u53ca\u5176\u7528\u9014"},{"location":"2-11-Header-files/#_2","text":"\u8003\u8651\u5982\u4e0b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << \"Hello, world!\" ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u4f7f\u7528 std:: cout \u5c06 \u201cHello, world!\u201d \u6253\u5370\u5728\u63a7\u5236\u53f0\u4e0a\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u7684\u7a0b\u5e8f\u5e76\u6ca1\u6709\u5b9a\u4e49\u8fc7 std:: cout \u51fd\u6570\u5440\uff1f\u7f16\u8bd1\u5668\u5982\u4f55\u5f97\u77e5\u5b83\u7684\u5b9a\u4e49\u7684\u5462\uff1f \u5b9e\u9645\u4e0a std:: cout \u7684\u58f0\u660e\u88ab\u5b9a\u4e49\u5728 \u201ciostream\u201d \u5934\u6587\u4ef6\u4e2d\uff0c\u5f53\u6211\u4eec\u4f7f\u7528 #include \u6307\u4ee4\u7684\u65f6\u5019\uff0c\u5176\u5b9e\u662f\u5728\u544a\u8bc9\u9884\u5904\u7406\u5668\uff0c\u5c06\u8be5\u6587\u4ef6\u7684\u5185\u5bb9\uff08\u5176\u4e2d\u5c31\u5305\u62ec std:: cout \u7684\u524d\u5411\u58f0\u660e\uff09\u5168\u90e8\u62f7\u8d1d\u5230\u6b64\u5904\u3002 \u5173\u952e\u4fe1\u606f \u5f53\u6211\u4eec #include \u4e00\u4e2a\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u8be5\u6587\u4ef6\u7684\u5185\u5bb9\u4f1a\u88ab\u63d2\u5165\u5230\u6b64\u5904\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u4ece\u5176\u4ed6\u6587\u4ef6\u83b7\u53d6\u524d\u5411\u5b9a\u4e49\u3002 \u8003\u8651\u4e00\u4e0b\uff0c\u5982\u679c iostream \u5934\u6587\u4ef6\u4e0d\u5b58\u5728\u4f1a\u600e\u6837\uff1f\u90a3\u4e48\u4f60\u6bcf\u6b21\u4f7f\u7528 std:: cout \u7684\u65f6\u5019\uff0c\u90fd\u5fc5\u987b\u624b\u5de5\u5c06\u6240\u6709\u548c std:: cout \u76f8\u5173\u7684\u58f0\u660e\u90fd\u8f93\u5165\u6216\u62f7\u8d1d\u5230\u6587\u4ef6\u7684\u5f00\u5934\u90e8\u5206\u3002\u8fd9\u6837\u4e0d\u4ec5\u9ebb\u70e6\uff0c\u8fd8\u9700\u8981\u4f7f\u7528\u8005\u77e5\u9053 std:: cout \u5b9e\u73b0\u7684\u7ec6\u8282\uff0c\u8fd9\u5c06\u4f1a\u662f\u975e\u5e38\u975e\u5e38\u5927\u7684\u5de5\u4f5c\u91cf\u3002\u66f4\u7cdf\u7684\u662f\uff0c\u4e00\u65e6\u51fd\u6570\u539f\u578b\u53d1\u751f\u4e86\u6539\u53d8\uff0c\u6211\u4eec\u5fc5\u987b\u624b\u52a8\u66f4\u65b0\u5168\u90e8\u7684\u58f0\u660e\u3002\u6240\u4ee5\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u83ab\u8fc7\u4e8e\u76f4\u63a5\u4f7f\u7528 #include \uff01 \u5bf9\u4e8e\u51fd\u6570\u548c\u53d8\u91cf\u6765\u8bf4\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b83\u4eec\u7684\u5934\u6587\u4ef6\u4e2d\u901a\u5e38\u53ea\u5305\u542b\u58f0\u660e\uff0c\u800c\u4e0d\u5305\u542b\u51fd\u6570\u548c\u53d8\u91cf\u7684\u5b9a\u4e49\uff08\u5426\u5219\u53ef\u80fd\u4f1a\u8fdd\u53cd \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \uff09\u3002 std:: cout \u88ab\u524d\u5411\u5b9a\u4e49\u5728 iostream \u5934\u6587\u4ef6\u4e2d\u5e76\u4f5c\u4e3a C++\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u800c\u6807\u51c6\u5e93\u5219\u4f1a\u5728\u7a0b\u5e8f\u7f16\u8bd1\u65f6\u88ab\u81ea\u52a8\u94fe\u63a5\u3002 \u6700\u4f73\u5b9e\u8df5 \u5934\u6587\u4ef6\u4e2d\u4e00\u822c\u4e0d\u5e94\u8be5\u5305\u542b\u51fd\u6570\u548c\u53d8\u91cf\u7684\u5b9a\u4e49\uff0c\u4ee5\u4fbf\u9075\u5faa\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u3002\u4f8b\u5916\u7684\u662f \u7b26\u53f7\u5e38\u91cf (\u5728 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \u4e2d\u4f1a\u8fdb\u884c\u4ecb\u7ecd)\u3002","title":"\u4f7f\u7528\u6807\u51c6\u5e93\u7684\u5934\u6587\u4ef6"},{"location":"2-11-Header-files/#_3","text":"\u73b0\u5728\uff0c\u56de\u60f3\u4e00\u4e0b\u4e4b\u524d\u8bfe\u7a0b\u4e2d\u4f7f\u7528\u8fc7\u7684\u7a0b\u5e8f\u3002\u8be5\u7a0b\u5e8f\u5305\u542b\u4e24\u4e2a\u6587\u4ef6 add.cpp \u548c main.cpp \uff1a add.cpp 1 2 3 4 int add ( int x , int y ) { return x + y ; } main.cpp 1 2 3 4 5 6 7 8 9 #include int add ( int x , int y ); // forward declaration using function prototype int main () { std :: cout << \"The sum of 3 and 4 is \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } \uff08\u5982\u679c\u4f60\u60f3\u8981\u4e3a\u8fd9\u4e24\u4e2a\u6587\u4ef6\u91cd\u65b0\u521b\u5efa\u4e00\u4e2a\u9879\u76ee\uff0c\u4e0d\u8981\u5fd8\u8bb0\u628a add.cpp \u6587\u4ef6\u52a0\u5230\u9879\u76ee\u4e2d\uff0c\u8fd9\u6837\u4f60\u624d\u80fd\u7f16\u8bd1\u5b83\uff09\u3002 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u524d\u5411\u58f0\u660e\u4ee5\u4fbf\u8ba9\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1 main.cpp \u65f6\u80fd\u591f\u77e5\u6653 add \u7684\u5b9a\u4e49\u3002\u6b63\u5982\u4e4b\u524d\u4ecb\u7ecd\u7684\u90a3\u6837\uff0c\u5982\u679c\u4e3a\u6bcf\u4e00\u4e2a\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u90fd\u521b\u5efa\u524d\u5411\u58f0\u660e\uff0c\u5c06\u4f1a\u975e\u5e38\u9ebb\u70e6\uff1a \u5176\u5b9e\uff0c\u53ea\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u5934\u6587\u4ef6\u5c31\u53ef\u4ee5\u4e00\u52b3\u6c38\u9038\u89e3\u51b3\u4e0a\u8ff0\u70e6\u607c\u3002\u7f16\u5199\u5934\u6587\u4ef6\u6bd4\u4f60\u60f3\u8c61\u7684\u8981\u7b80\u5355\u7684\u591a\uff0c\u5b83\u53ea\u5305\u542b\u4e24\u90e8\u5206\uff1a \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e \uff0c\u7a0d\u540e\u6211\u4eec\u4f1a\u8be6\u7ec6\u4ecb\u7ecd(\u53c2\u8003\uff1a 2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e )\u3002 \u5934\u6587\u4ef6\u7684\u5b9e\u9645\u5185\u5bb9\uff0c\u5373\u6240\u6709\u6211\u4eec\u5e0c\u671b\u80fd\u591f\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u88ab\u8bbf\u95ee\u7684\u6807\u8bc6\u7b26\u7684\u524d\u5411\u58f0\u660e\u3002 \u5c06\u5934\u6587\u4ef6\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u7684\u65b9\u6cd5\u548c\u6dfb\u52a0\u6e90\u6587\u4ef6\u5dee\u4e0d\u591a\uff08\u53c2\u8003\uff1a 2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f \uff09\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f IDE\uff0c\u53ef\u4ee5\u53c2\u8003\u4e4b\u524d\u8bfe\u7a0b\u4e2d\u63d0\u5230\u7684\u6b65\u9aa4\uff0c\u53ea\u4e0d\u8fc7\u5728\u9009\u62e9 Source \u7684\u65f6\u5019\uff0c\u8981\u6539\u4e3a\u9009\u62e9 Header \u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u547d\u4ee4\u884c\u73af\u5883\uff0c\u5219\u53ea\u9700\u8981\u7528\u7f16\u8f91\u5668\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u5373\u53ef\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u521b\u5efa\u5934\u6587\u4ef6\u7684\u65f6\u5019\u8bf7\u4f7f\u7528 .h \u540e\u7f00\u3002 \u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u901a\u5e38\u662f\u6210\u5bf9\u51fa\u73b0\u7684\uff0c\u5934\u6587\u4ef6\u4f1a\u4e3a\u5bf9\u5e94\u7684\u6e90\u6587\u4ef6\u63d0\u4f9b\u524d\u5411\u58f0\u660e\u3002\u56e0\u4e3a\u5934\u6587\u4ef6\u5305\u542b\u7684\u662f add.cpp \u4e2d\u51fd\u6570\u7684\u58f0\u660e\uff0c\u56e0\u6b64\u5934\u6587\u4ef6\u547d\u540d\u4e3a add.h \u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u6210\u5bf9\u51fa\u73b0\uff08\u4f8b\u5982\uff0cadd.h \u548c add.cpp\uff09\uff0c\u5b83\u4eec\u5e94\u8be5\u662f\u540c\u540d\u4f46\u4e0d\u540c\u540e\u7f00\u7684\u6587\u4ef6\u3002 \u5b8c\u6210\u540e\u7684\u5934\u6587\u4ef6\u5982\u4e0b\uff1a add.h: add.h 1 2 3 4 // 1) \u8fd9\u91cc\u5e94\u8be5\u662f\u5934\u6587\u4ef6\u5305\u542b\u4fdd\u62a4\uff0c\u4f46\u662f\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u53ea\u6709\u4e00\u4e2a\u5934\u6587\u4ef6\uff0c\u4e3a\u4e86\u7b80\u5316\u8fd9\u91cc\u5c31\u7701\u7565\u4e86 // 2) .h \u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u58f0\u660e\u90fd\u5199\u5728\u8fd9\u91cc int add ( int x , int y ); // \u51fd\u6570\u539f\u578b\u2014\u2014\u4e0d\u8981\u5fd8\u8bb0\u5206\u53f7\uff01 \u4e3a\u4e86\u5728 main.cpp \u4e2d\u4f7f\u7528\u8be5\u5934\u6587\u4ef6\uff0c\u6211\u4eec\u9700\u8981 #include (\u8fd9\u91cc\u9700\u8981\u4f7f\u7528\u5f15\u53f7\u800c\u975e\u5c16\u62ec\u53f7)\u3002 main.cpp 1 2 3 4 5 6 7 8 #include \"add.h\" // \u5c06 add.h \u63d2\u5165\u5230\u6b64\u4f4d\u7f6e\u3002\u6ce8\u610f\u4f7f\u7528\u53cc\u5f15\u53f7\u3002 #include int main () { std :: cout << \"The sum of 3 and 4 is \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } add.cpp 1 2 3 4 5 6 #include \"add.h\" // add.h \u7684\u5185\u5bb9\u4f1a\u88ab\u62f7\u8d1d\u5230\u8fd9\u91cc\u3002\u6ce8\u610f\u4f7f\u7528\u53cc\u5f15\u53f7\u3002 int add ( int x , int y ) { return x + y ; } \u5f53\u9884\u5904\u7406\u5668\u5904\u7406\u5230 #include \"add.h\" \u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u628a add.h \u4e2d\u7684\u5185\u5bb9\u90fd\u62f7\u8d1d\u5230\u8fd9\u91cc\u3002\u56e0\u4e3a add.h \u5305\u542b\u4e86\u51fd\u6570 add \u7684\u58f0\u660e\uff0c\u6240\u4ee5\u8be5\u58f0\u660e\u5c31\u4f1a\u88ab\u62f7\u8d1d\u5230\u4e86 main.cpp \u4e2d\u3002\u6700\u7ec8\u7684\u6548\u679c\uff0c\u5c31\u548c\u4e4b\u524d\u76f4\u63a5\u5c06\u58f0\u660e\u5199\u5728 main.cpp \u9876\u90e8\u662f\u4e00\u6837\u7684\u3002 \u73b0\u5728\uff0c\u6211\u4eec\u7684\u7a0b\u5e8f\u5c31\u53ef\u4ee5\u88ab\u6b63\u786e\u7684\u7f16\u8bd1\u548c\u94fe\u63a5\u4e86\u3002","title":"\u7f16\u5199\u5934\u6587\u4ef6"},{"location":"2-11-Header-files/#_4","text":"C++ \u4e2d\u7684\u6700\u4f73\u5b9e\u8df5\u4e4b\u4e00\u5c31\u662f\u6e90\u6587\u4ef6\u5e94\u8be5\u5305\u542b\u5176\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\uff08\u5982\u679c\u5b58\u5728\u7684\u8bdd\uff09\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c add.cpp \u5e94\u8be5\u5305\u542b add.h \u3002 \u8fd9\u4e48\u505a\u53ef\u4ee5\u4f7f\u5f97\u6709\u4e9b\u95ee\u9898\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u88ab\u53d1\u73b0\uff0c\u800c\u4e0d\u662f\u7559\u5230\u94fe\u63a5\u65f6\u518d\u53d1\u73b0\u3002\u4f8b\u5982\uff1a something.h 1 int something ( int ); // \u58f0\u660e\u7684\u8fd4\u56de\u7c7b\u578b\u662f int something.cpp 1 2 3 4 5 #include \"something.h\" void something ( int ) // \u9519\u8bef: \u9519\u8bef\u7684\u8fd4\u56de\u7c7b\u578b { } \u56e0\u4e3a something.cpp #includes \u4e86 something.h \uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u53d1\u73b0 something() \u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u5339\u914d\u3002\u800c\u5982\u679c something.cpp \u6ca1\u6709 #include something.h \uff0c\u90a3\u4e48\u6211\u4eec\u5fc5\u987b\u8981\u7b49\u5230\u94fe\u63a5\u65f6\uff0c\u8be5\u95ee\u9898\u624d\u4f1a\u88ab\u94fe\u63a5\u5668\u53d1\u73b0\uff0c\u8fd9\u65e0\u7591\u4f1a\u6d6a\u8d39\u65f6\u95f4\u3002\u5176\u4ed6\u4f8b\u5b50\u53ef\u4ee5\u53c2\u8003 \u8fd9\u4e2a\u8bc4\u8bba \u3002 \u6700\u4f73\u5b9e\u8df5 \u6e90\u6587\u4ef6\u9700\u8981\u5305\u542b\u5176\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\uff08\u5982\u679c\u6709\u7684\u8bdd\uff09\u3002","title":"\u6e90\u6587\u4ef6\u9700\u8981\u5305\u542b\u5176\u5bf9\u5e94\u7684\u5934\u6587\u4ef6"},{"location":"2-11-Header-files/#_5","text":"\u5982\u679c\u7f16\u8bd1\u5668\u62a5\u544a\u4e86 add.h \u65e0\u6cd5\u88ab\u627e\u7684\u9519\u8bef\uff0c\u8bf7\u9996\u5148\u786e\u8ba4\u6587\u4ef6\u540d\u662f\u5426\u4e3a add.h \u3002\u6587\u4ef6\u540d\u53ef\u80fd\u4f1a\u88ab\u9519\u8bef\u5730\u8bbe\u7f6e\u4e3a add (\u65e0\u540e\u7f00) \u6216 add.h.txt \u6216 add.hpp \uff0c\u8fd9\u53ef\u80fd\u53d6\u51b3\u4e8e\u4f60\u662f\u5982\u4f55\u521b\u5efa\u5b83\u4eec\u7684\u3002\u53e6\u5916\uff0c\u4e5f\u8981\u786e\u4fdd\u8be5\u5934\u6587\u4ef6\u548c\u5176\u4ed6\u6587\u4ef6\u4f4d\u4e8e\u76f8\u540c\u7684\u76ee\u5f55\u3002 \u5982\u679c\u94fe\u63a5\u5668\u62a5\u544a\u4e86 add \u51fd\u6570\u672a\u5b9a\u4e49\u7684\u9519\u8bef\uff0c\u8bf7\u786e\u4fdd add.cpp \u88ab\u6dfb\u52a0\u5230\u4e86\u9879\u76ee\u4e2d\uff0c\u8fd9\u6837 add \u51fd\u6570\u624d\u80fd\u591f\u88ab\u6b63\u786e\u94fe\u63a5\u3002","title":"\u9519\u8bef\u6392\u67e5"},{"location":"2-11-Header-files/#vs","text":"\u4f60\u53ef\u80fd\u4f1a\u597d\u5947\uff0c\u4e3a\u4ec0\u4e48 iostream \u4f7f\u7528\u7684\u662f\u5c16\u62ec\u53f7\uff0c\u800c add.h \u5c31\u9700\u8981\u4f7f\u7528\u53cc\u5f15\u53f7\u3002\u8fd9\u662f\u56e0\u4e3a\uff0c\u540c\u540d\u7684\u6587\u4ef6\u53ef\u80fd\u4f1a\u5206\u5e03\u5728\u4e0d\u540c\u7684\u76ee\u5f55\u4e2d\u3002\u533a\u5206\u4f7f\u7528\u5c16\u62ec\u53f7\u548c\u53cc\u5f15\u7528\uff0c\u53ef\u4ee5\u544a\u8bc9\u7f16\u8bd1\u5668\u5230\u54ea\u91cc\u5bfb\u627e\u5934\u6587\u4ef6\u3002 \u5f53\u4f7f\u7528\u5c16\u62ec\u53f7\u7684\u65f6\u5019\uff0c\u5176\u5b9e\u662f\u5728\u544a\u8bc9\u9884\u5904\u7406\u5668\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\u5e76\u4e0d\u662f\u6211\u4eec\u7f16\u5199\u7684\u3002\u7f16\u8bd1\u5668\u53ea\u4f1a\u5728 include directories \u6307\u5b9a\u7684\u76ee\u5f55\u4e2d\u641c\u7d22\u3002 include directories \u662f\u9879\u76ee\u3001IDE\u6216\u7f16\u8bd1\u5668\u914d\u7f6e\u7684\u4e00\u90e8\u5206\uff0c\u9ed8\u8ba4\u7684\u8def\u5f84\u662f\u7531\u7f16\u8bd1\u5668\u6216\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684\u3002\u7f16\u8bd1\u5668\u5e76\u4e0d\u4f1a\u5728\u4f60\u7684\u9879\u76ee\u76ee\u5f55\u4e2d\u641c\u7d22\u5bf9\u996e\u7684\u5934\u6587\u4ef6\u3002 \u5f53\u4f7f\u7528\u53cc\u5f15\u53f7\u7684\u65f6\u5019\uff0c\u5176\u5b9e\u662f\u5728\u544a\u8bc9\u9884\u5904\u7406\u5668\u5934\u6587\u4ef6\u662f\u6211\u4eec\u81ea\u5df1\u7f16\u5199\u7684\u3002\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u641c\u7d22\u5f53\u524d\u76ee\u5f55\uff0c\u5982\u679c\u627e\u4e0d\u5230\u6240\u9700\u7684\u5934\u6587\u4ef6\uff0c\u5219\u4f1a\u5728 include directories \u4e2d\u8fdb\u884c\u67e5\u627e\u3002 \u6cd5\u5219 \u4f7f\u7528\u53cc\u5f15\u53f7\u6765\u5305\u542b\u4f60\u81ea\u5df1\u7f16\u5199\u7684\u5934\u6587\u4ef6\uff0c\u5219\u8be5\u6587\u4ef6\u5fc5\u987b\u4f4d\u4e8e\u5f53\u524d\u76ee\u5f55\u4e2d\u3002\u4f7f\u7528\u5c16\u62ec\u53f7\u5305\u542b\u7f16\u8bd1\u5668\u3001\u64cd\u4f5c\u7cfb\u7edf\u6216\u7b2c\u4e09\u65b9\u63d0\u4f9b\u7684\u5b89\u88c5\u5728\u7cfb\u7edf\u5176\u4ed6\u4f4d\u7f6e\u7684\u5934\u6587\u4ef6\u3002","title":"\u5c16\u62ec\u53f7 vs \u53cc\u5f15\u53f7"},{"location":"2-11-Header-files/#iostream-h","text":"\u53e6\u5916\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u662f\uff1a\u201d\u4e3a\u4ec0\u4e48 iostream \uff08\u6216\u8005\u5176\u4ed6\u6807\u51c6\u5e93\u5934\u6587\u4ef6\uff09\u6ca1\u6709 .h \u540e\u7f00\u5462\uff1f\u201c\u3002\u8fd9\u662f\u56e0\u4e3a iostream.h \u548c iostream \u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u5934\u6587\u4ef6\u3002\u8fd9\u6d89\u53ca\u5230\u4e00\u4e9b\u5386\u53f2\u77e5\u8bc6\u3002 \u5728 C++ \u521a\u88ab\u521b\u5efa\u51fa\u6765\u7684\u65f6\u5019\uff0c\u6240\u6709\u7684\u6807\u51c6\u8fd0\u884c\u65f6\u5e93\u90fd\u662f\u4ee5 .h \u7ed3\u5c3e\u7684\u3002\u6700\u521d\u7248\u672c\u7684 cout \u548c cin \u4e5f\u90fd\u88ab\u5b9a\u4e49\u5728 iostream.h \u4e2d\u3002\u5728ANSI\u59d4\u5458\u4f1a\u5bf9\u8bed\u8a00\u8fdb\u884c\u6807\u51c6\u5316\u7684\u65f6\u5019\uff0c\u5b83\u4eec\u51b3\u5b9a\u8981\u5c06\u6807\u51c6\u5e93\u4e2d\u7684\u51fd\u6570\u90fd\u79fb\u5230 std \u547d\u540d\u7a7a\u95f4\u4e2d\u4ee5\u907f\u514d\u5176\u548c\u7528\u6237\u5b9a\u4e49\u7684\u51fd\u6570\u4ea7\u751f\u547d\u540d\u51b2\u7a81\u3002\u4e0d\u8fc7\uff0c\u73b0\u5b9e\u95ee\u9898\u662f\uff0c\u5982\u679c\u6b64\u65f6\u5c06\u8fd9\u4e9b\u51fd\u6570\u90fd\u79fb\u52a8\u5230 std \u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u90a3\u4e48\u4e4b\u524d\u5199\u7684\u4ee3\u7801\u5c31\u90fd\u4e0d\u80fd\u5de5\u4f5c\u4e86\uff01 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ea\u80fd\u4f7f\u7528\u4e00\u7ec4\u65b0\u7684\u5934\u6587\u4ef6\uff0c\u5b83\u4eec\u5177\u6709\u76f8\u540c\u7684\u540d\u5b57\uff0c\u4f46\u6ca1\u6709 .h \u540e\u7f00\u3002\u8fd9\u4e9b\u65b0\u7684\u5934\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\uff0c\u90fd\u5728 std \u4f5c\u7528\u57df\u4e2d\u3002\u8fd9\u6837\uff0c\u4f7f\u7528 #include \u7684\u8001\u4ee3\u7801\u5c31\u4e0d\u9700\u8981\u8fdb\u884c\u4efb\u4f55\u4fee\u6539\u4e86\uff0c\u800c\u65b0\u7f16\u5199\u7684\u4ee3\u7801\u5219\u9700\u8981\u4f7f\u7528 #include \u3002 \u6b64\u5916\uff0c\u4ece C \u8bed\u8a00\u7ee7\u627f\u8fc7\u6765\u7684\u6807\u51c6\u5e93\u5219\u88ab\u6dfb\u52a0\u4e86\u4e00\u4e2a c \u4f5c\u4e3a\u524d\u7f00(\u4f8b\u5982 stdlib.h \u53d8\u6210\u4e86 cstdlib )\u3002\u8fd9\u4e9b\u5e93\u4e2d\u7684\u51fd\u6570\u4e5f\u540c\u6837\u88ab\u79fb\u52a8\u5230\u4e86 std \u6807\u51c6\u5e93\u4e2d\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u4f7f\u7528\u6807\u51c6\u5e93\u5934\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u6ca1\u6709 .h \u7248\u672c\u7684\u5934\u6587\u4ef6\u3002\u5bf9\u4e8e\u7528\u6237\u81ea\u5df1\u7f16\u5199\u7684\u5934\u6587\u4ef6\uff0c\u4ecd\u7136\u9700\u8981\u4f7f\u7528 .h \u540e\u7f00\u3002","title":"\u4e3a\u4ec0\u4e48 iostream \u6ca1\u6709 .h \u540e\u7f00?"},{"location":"2-11-Header-files/#include","text":"\u53e6\u5916\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\u662f\u5982\u4f55\u5305\u542b\u5176\u4ed6\u76ee\u5f55\u4e2d\u7684\u5934\u6587\u4ef6\u3002 \u4e00\u79cd\u53ef\u884c\uff08\u4f46\u4e0d\u597d\u7684\uff09\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u6765\u6307\u5b9a #include \u7684\u5185\u5bb9\uff0c\u4f8b\u5982\uff1a 1 2 #include \"headers/myHeader.h\" #include \"../moreHeaders/myOtherHeader.h\" \u8fd9\u4e48\u505a\u7a0b\u5e8f\u662f\u53ef\u4ee5\u7f16\u8bd1\u7684\uff08\u5047\u8bbe\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\u786e\u5b9e\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e0b\uff09\uff0c\u4f46\u662f\u5b83\u7684\u7f3a\u70b9\u4e5f\u5f88\u660e\u663e\uff0c\u4f60\u5fc5\u987b\u5728\u4ee3\u7801\u4e2d\u63cf\u8ff0\u5b9e\u9645\u7684\u76ee\u5f55\u7ed3\u6784\u3002\u5982\u679c\u76ee\u5f55\u7ed3\u6784\u66f4\u65b0\u4e86\uff0c\u90a3\u4ee3\u7801\u4e5f\u5c31\u4e0d\u80fd\u5de5\u4f5c\u4e86\u3002 \u66f4\u597d\u7684\u529e\u6cd5\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\u6216\u8005 IDE\u6709\u4e9b\u5934\u6587\u4ef6\u5b58\u653e\u5728\u5176\u4ed6\u4f4d\u7f6e\uff0c\u6b64\u65f6\u5f53\u5b83\u4eec\u65e0\u6cd5\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u88ab\u627e\u5230\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u5230\u6307\u5b9a\u7684\u76ee\u5f55\u4e2d\u67e5\u627e\u3002\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u5728 IDE \u4e2d\u8bbe\u7f6e\u9879\u76ee\u7684 include path \u6216 search directory \u6765\u5b9e\u73b0\u3002 For Visual Studio users \u53f3\u952e\u5355\u51fb\u9879\u76ee\u7684 Solution Explorer \u7136\u540e\u9009\u62e9 Properties \u7136\u540e\u9009\u62e9 VC++ Directories \u9009\u9879\u5361\u3002\u5728\u8fd9\u91cc\u4f60\u53ef\u4ee5\u770b\u5230 Include Directories \u5c06\u4f60\u5e0c\u671b\u7f16\u8bd1\u5668\u641c\u7d22\u7684\u5305\u542b\u4e86\u5934\u6587\u4ef6\u7684\u76ee\u5f55\u586b\u5199\u5230\u8fd9\u91cc\u5373\u53ef\u3002 For Code:: Blocks users \u5728 Code:: Blocks \u4e2d\uff0c\u9009\u62e9 Project \u83dc\u5355\u5e76\u9009\u62e9 Build Options \uff0c\u518d\u9009\u62e9 Search directories \u9009\u9879\u5361\uff0c\u5c06\u4f60\u5e0c\u671b\u7f16\u8bd1\u5668\u641c\u7d22\u7684\u5305\u542b\u4e86\u5934\u6587\u4ef6\u7684\u76ee\u5f55\u586b\u5199\u5230\u8fd9\u91cc\u5373\u53ef\u3002 For GCC/G++ users \u4f7f\u7528 g++ \u662f\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7 -I \u9009\u9879\u6765\u6307\u660e\u5934\u6587\u4ef6\u641c\u7d22\u8def\u5f84\uff1a 1 g ++ - o main - I / source / includes main . cpp \u8fd9\u4e48\u505a\u7684\u597d\u5904\u662f\uff0c\u5982\u679c\u4f60\u6539\u53d8\u4e86\u76ee\u5f55\u7ed3\u6784\uff0c\u90a3\u4e48\u53ea\u9700\u8981\u5728\u8bbe\u7f6e\u91cc\u9762\u4fee\u6539\u8def\u5f84\u5373\u53ef\uff0c\u800c\u4e0d\u5fc5\u5bf9\u4ee3\u7801\u4e2d\u6bcf\u4e00\u5904\u4f7f\u7528\u8be5\u5934\u6587\u4ef6\u7684\u5730\u65b9\u8fdb\u884c\u4fee\u6539\u3002","title":"include \u5176\u4ed6\u76ee\u5f55\u4e2d\u7684\u5934\u6587\u4ef6"},{"location":"2-11-Header-files/#_6","text":"\u5934\u6587\u4ef6\u9700\u8981\u4f9d\u8d56\u5176\u4ed6\u5934\u6587\u4ef6\u4e2d\u7684\u5b9a\u4e49\u6216\u58f0\u660e\u4e5f\u662f\u5f88\u5e38\u89c1\u7684\u3002\u56e0\u6b64\uff0c\u5934\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528 #include \u6765\u5305\u542b\u5176\u4ed6\u5934\u6587\u4ef6\u3002 \u5f53\u4f60\u7684\u4ee3\u7801 #includes \u7b2c\u4e00\u4e2a\u5934\u6587\u4ef6\u65f6\uff0c\u4f60\u5176\u5b9e\u4e5f\u5305\u542b\u4e86\u8be5\u5934\u6587\u4ef6\u4e2d\u5305\u542b\u7684\u5176\u4ed6\u5934\u6587\u4ef6\uff08\u4ee5\u53ca\u8fd9\u6240\u6709\u5934\u6587\u4ef6\u4e2d\u5305\u542b\u7684\u5934\u6587\u4ef6\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff09\u3002\u8fd9\u4e9b\u989d\u5916\u7684\u5934\u6587\u4ef6\u6709\u65f6\u5019\u79f0\u4e3a \u95f4\u63a5\u5305\u542b(transitive includes) \uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u88ab\u9690\u5f0f\u5305\u542b\u8fdb\u6765\u7684\uff0c\u6211\u4eec\u660e\u6ca1\u6709\u6307\u660e\u54ea\u4e9b\u9700\u8981\u88ab\u5305\u542b\u3002 \u8fd9\u4e9b\u5934\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u5728\u4f60\u7684\u4ee3\u7801\u4e2d\u662f\u53ef\u7528\u7684\u3002\u4e0d\u8fc7\u4f60\u5e76\u4e0d\u5e94\u8be5\u4f9d\u8d56\u5b83\u4eec\uff0c\u6bd5\u7adf\u5b83\u4eec\u662f\u88ab\u95f4\u63a5\u5305\u542b\u8fdb\u6765\u7684\u3002\u8fd9\u4e9b\u5934\u6587\u4ef6\u53ef\u80fd\u4f1a\u53d8\u5316\uff0c\u4e5f\u53ef\u80fd\u5728\u4e0d\u540c\u7684\u7cfb\u7edf\u4e2d\u5b58\u5728\u5dee\u5f02\uff0c\u8fd9\u65f6\u5019\u4f60\u7684\u4ee3\u7801\u53ef\u80fd\u53ea\u80fd\u5728\u67d0\u5e73\u53f0\u4e0b\u624d\u80fd\u7f16\u8bd1\uff0c\u6216\u8005\u53ef\u80fd\u5728\u5c06\u6765\u65e0\u6cd5\u7f16\u8bd1\u3002\u907f\u514d\u4e0a\u8ff0\u95ee\u9898\u7684\u65b9\u6cd5\u4e5f\u5f88\u7b80\u5355\uff0c\u8bf7\u660e\u786e\u5305\u542b\u4f60\u6240\u9700\u8981\u7684\u5168\u90e8\u5934\u6587\u4ef6\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u4e8e\u7f16\u8bd1\u6240\u9700\u7684\u5934\u6587\u4ef6\uff0c\u6bcf\u4e00\u4e2a\u90fd\u5e94\u8be5\u88ab\u660e\u786e\u5730\u4f7f\u7528 #include \u5305\u542b\u8fdb\u6765\u3002\u4e0d\u8981\u4f9d\u8d56\u88ab\u95f4\u63a5\u5305\u542b\u7684\u5934\u6587\u4ef6\u3002 \u4e0d\u5e78\u7684\u662f\uff0c\u6211\u4eec\u5f88\u96be\u53bb\u7504\u522b\u7a76\u7adf\u54ea\u4e9b\u4ee3\u7801\u6b63\u5728\u4f9d\u8d56\u90a3\u4e9b\u88ab\u95f4\u63a5\u5f15\u5165\u7684\u5934\u6587\u4ef6\u3002 Q: \u6211\u6ca1\u6709 include \uff0c\u4f46\u662f\u7a0b\u5e8f\u4ecd\u7136\u80fd\u6b63\u5e38\u5de5\u4f5c\uff01\u4e3a\u4ec0\u4e48\uff1f \u8fd9\u4e2a\u95ee\u9898\u4e5f\u662f\u5e38\u5907\u95ee\u5230\u7684\u95ee\u9898\u4e4b\u4e00\u3002\u8fd9\u53ef\u80fd\u662f\u56e0\u4e3a\u4f60\u5305\u542b\u4e86\u67d0\u4e2a\u5934\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u6070\u5de7\u8be5\u6587\u4ef6\u4e5f\u5305\u542b\u4e86\u4f60\u6240\u9700\u7684\u90a3\u4e2a\u5934\u6587\u4ef6\uff0c\u6240\u4ee5\u4ee3\u7801\u53ef\u4ee5\u5de5\u4f5c\u3002\u5c3d\u7ba1\u4f60\u7684\u7a0b\u5e8f\u53ef\u4ee5\u7f16\u8bd1\uff0c\u6700\u4f73\u5b9e\u8df5\u544a\u8bc9\u6211\u4eec\uff0c\u8fd9\u79cd\u65b9\u5f0f\u5e76\u4e0d\u53ef\u9760\u3002\u5728\u4f60\u7684\u7535\u8111\u4e0a\u80fd\u7f16\u8bd1\uff0c\u4e0d\u4ee3\u8868\u5728\u5176\u4ed6\u7535\u8111\u4e0a\u4e5f\u80fd\u7f16\u8bd1\u3002","title":"\u5934\u6587\u4ef6\u4e2d\u53ef\u4ee5\u5305\u542b\u5176\u4ed6\u5934\u6587\u4ef6"},{"location":"2-11-Header-files/#include_1","text":"\u5982\u679c\u4f60\u7684\u5934\u6587\u4ef6\u5185\u5bb9\u6ca1\u95ee\u9898\uff0c\u5e76\u4e14\u4e5f #include \u4e86\u5b83\u4eec\u6240\u4f9d\u8d56\u7684\u5176\u4ed6\u5934\u6587\u4ef6\uff0c\u90a3\u4e48\u5934\u6587\u4ef6\u5305\u542b\u7684\u987a\u5e8f\u5176\u5b9e\u5e76\u4e0d\u91cd\u8981\u3002 \u4e0d\u8fc7\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u5982\u4e0b\u573a\u666f\uff1a\u5934\u6587\u4ef6 A \u9700\u8981\u5934\u6587\u4ef6 B\u4e2d\u7684\u58f0\u660e\uff0c\u4f46\u662f\u5374\u5fd8\u8bb0\u5305\u542b B \u4e86\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u5728\u6e90\u6587\u4ef6\u4e2d\uff0c\u5148\u5305\u542b B \u518d\u5305\u542bA\uff0c\u90a3\u4e48\u7a0b\u5e8f\u662f\u53ef\u4ee5\u7f16\u8bd1\u7684\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u4f1a\u9996\u5148\u7f16\u8bd1\u5230B\u4e2d\u7684\u58f0\u660e\uff0c\u7136\u540e\u624d\u4f1a\u7f16\u8bd1\u5230A\u4e2d\u5bf9B\u6709\u4f9d\u8d56\u7684\u90a3\u4e9b\u4ee3\u7801\u3002 \u53cd\u4e4b\uff0c\u5982\u679c\u6211\u4eec\u5148\u5305\u542b\u5934\u6587\u4ef6 A\uff0c\u518d\u5305\u542b\u5934\u6587\u4ef6 B\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c31\u4f1a\u62a5\u9519\u4e86\uff0c\u56e0\u4e3a\u5b83\u4e0d\u80fd\u5728\u7f16\u8bd1 A \u65f6\u627e\u5230\u5176\u6240\u4f9d\u8d56\u7684 B \u4e2d\u7684\u58f0\u660e\u3002\u5176\u5b9e\u8fd9\u6b63\u662f\u6211\u4eec\u5e0c\u671b\u7684\u7ed3\u679c\uff0c\u56e0\u4e3a\u5b83\u628a\u95ee\u9898\u66b4\u9732\u4e86\u51fa\u6765\uff0c\u6211\u4eec\u4fbf\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u4fee\u590d\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e3a\u4e86\u6700\u5927\u7a0b\u5ea6\u51cf\u5c11\u5934\u6587\u4ef6\u6ca1\u6709\u6b63\u786e\u5305\u542b\u5f15\u8d77\u7684\u7f16\u8bd1\u5668\u62a5\u9519\uff0c\u8bf7\u6309\u7167\u5982\u4e0b\u987a\u5e8f\u5305\u542b\u5934\u6587\u4ef6\uff1a \u5148\u5305\u542b\u6e90\u6587\u4ef6\u5bf9\u5e94\u7684\u5934\u6587\u4ef6 \u518d\u5305\u542b\u9879\u76ee\u6240\u9700\u7684\u5176\u4ed6\u5934\u6587\u4ef6 \u518d\u5305\u542b\u7b2c\u4e09\u65b9\u5e93\u7684\u5934\u6587\u4ef6 \u518d\u5305\u542b\u6807\u51c6\u5e93\u5934\u6587\u4ef6 \u6bcf\u4e00\u7ec4\u5934\u6587\u4ef6\u90fd\u5e94\u8be5\u6309\u7167\u5b57\u6bcd\u8868\u987a\u5e8f\u6392\u5e8f \u8fd9\u6837\uff0c\u4efb\u4f55\u4e00\u4e2a\u4f60\u5b9a\u4e49\u7684\u5934\u6587\u4ef6\u5982\u679c\u6ca1\u6709 #include \u5176\u6240\u9700\u7684\u7b2c\u4e09\u65b9\u5e93\u6216\u6807\u51c6\u5e93\uff0c\u90a3\u4e48\u5f88\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u8fdb\u884c\u4fee\u590d\u4e86\u3002","title":"\u5934\u6587\u4ef6 #include \u7684\u987a\u5e8f"},{"location":"2-11-Header-files/#_7","text":"\u5173\u4e8e\u5982\u4f55\u4f7f\u7528\u548c\u521b\u5efa\u5934\u6587\u4ef6\uff0c\u6211\u4eec\u6709\u5982\u4e0b\u5efa\u8bae\uff1a \u59cb\u7ec8\u4f7f\u7528 \u5934\u6587\u4ef6\u91cd\u590d\u5305\u542b\u4fdd\u62a4 \uff1b \u4e0d\u8981\u5728\u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\u53d8\u91cf\u548c\u51fd\u6570 (\u5168\u5c40\u5e38\u91cf\u662f\u4e2a\u4f8b\u5916\u2014\u2014\u7a0d\u540e\u4ecb\u7ecd\uff09\uff1b \u5934\u6587\u4ef6\u548c\u5176\u5bf9\u5e94\u7684\u6e90\u6587\u4ef6\u5e94\u8be5\u5177\u6709\u76f8\u540c\u7684\u6587\u4ef6\u540d\uff08\u4f8b\u5982\uff0c grades.h \u548c grades.cpp \uff0c\u6ce8\u610f\u6269\u5c55\u540d\u662f\u4e0d\u540c\u7684\uff09\uff1b \u6bcf\u4e2a\u5934\u6587\u4ef6\u90fd\u5e94\u8be5\u6709\u5176\u7279\u5b9a\u7684\u7528\u9014\u5e76\u4e14\u5c3d\u53ef\u80fd\u72ec\u7acb\u3002\u4f8b\u5982\uff0c\u4e0eA\u76f8\u5173\u7684\u58f0\u660e\u5e94\u8be5\u653e\u5728 A.h \u4e2d\uff0c\u4e0eB\u76f8\u5173\u7684\u58f0\u660e\u5e94\u8be5\u653e\u5728 B.h \u4e2d\u3002\u8fd9\u6837\u5982\u679c\u4ee5\u540e\u4ec5\u4ec5\u9700\u8981\u4f7f\u7528A\u76f8\u5173\u7684\u529f\u80fd\uff0c\u5219\u65e0\u9700\u5305\u542b B.h \uff0c\u4e5f\u5c31\u4e0d\u4f1a\u6d89\u53ca\u5230\u4efb\u4f55\u4e0eB\u76f8\u5173\u7684\u51fd\u6570\u58f0\u660e\uff1b \u5bf9\u4e8e\u54ea\u4e9b\u51fd\u6570\u5e94\u8be5\u5305\u542b\u54ea\u4e9b\u5934\u6587\u4ef6\u8981\u505a\u5230\u5fc3\u4e2d\u6709\u6570\uff1b \u4f60\u7f16\u5199\u7684\u4efb\u4f55\u5934\u6587\u4ef6\u90fd\u5e94\u8be5\u80fd\u591f\u72ec\u7acb\u7f16\u8bd1\uff08\u5b83\u5e94\u5f53 #include \u5b83\u6240\u9700\u7684\u5168\u90e8\u4f9d\u8d56\uff09\uff1b \u53ea #include \u5fc5\u8981\u7684\u5934\u6587\u4ef6\uff08\u4e0d\u8981\u628a\u80fd\u5305\u542b\u7684\u5168\u90e8\u5305\u542b\u8fdb\u6765\uff09\uff1b \u4e0d\u8981 #include .cpp \u6587\u4ef6\u3002","title":"\u5934\u6587\u4ef6\u6700\u4f73\u5b9e\u8df5"},{"location":"2-12-Header-guards/","tags":["header guards","pragma once"],"text":"Key Takeaway \u7531\u4e8e\u5934\u6587\u4ef6\u9632\u8303\u4e0d\u80fd\u9632\u6b62\u5934\u6587\u4ef6\u88ab\u5305\u542b\u5230\u4e0d\u540c\u7684\u6587\u4ef6\uff0c\u5934\u6587\u4ef6\u4e2d\u7684\u5b9a\u4e49\u53ef\u80fd\u4f1a\u5bfc\u81f4\u94fe\u63a5\u5668\u62a5\u544a\u51fd\u6570\u91cd\u5b9a\u4e49\u3002 \u4e0d\u53ef\u80fd\u5b8c\u5168\u907f\u514d\u5934\u6587\u4ef6\u4e2d\u7684\u5b9a\u4e49\uff0c\u6bd5\u7adf\u7c7b\u662f\u9700\u8981\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u7684\u3002 \u5bf9\u4e8e\u4e0d\u5305\u542b\u5b9a\u4e49\u7684\u5934\u6587\u4ef6\uff0c\u867d\u7136\u6ca1\u5fc5\u8981\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\uff0c\u4f46\u8fd9\u5176\u5b9e\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\uff08\u5c24\u5176\u8003\u8651\u5230\u540e\u9762\u53ef\u80fd\u5bf9\u5934\u6587\u4ef6\u8fdb\u884c\u7684\u6539\u52a8\uff09 \u5904\u4e8e\u517c\u5bb9\u6027\u7684\u8003\u8651\uff0c\u4f18\u5148\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\u800c\u975e #pragma once \u91cd\u590d\u5b9a\u4e49\u95ee\u9898 \u5728 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u53d8\u91cf\u548c\u51fd\u6570\u7684\u6807\u8bc6\u7b26\u53ea\u80fd\u88ab\u5b9a\u4e49\u4e00\u6b21\uff0c\u5373 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219 \u3002\u56e0\u6b64\uff0c\u5982\u679c\u4e00\u4e2a\u7a0b\u5e8f\u5305\u542b\u4e86\u5bf9\u67d0\u4e2a\u6807\u8bc6\u7b26\u7684\u591a\u6b21\u5b9a\u4e49\uff0c\u5c06\u4f1a\u5f15\u8d77\u7f16\u8bd1\u9519\u8bef \uff1a 1 2 3 4 5 6 7 int main () { int x ; // this is a definition for variable x int x ; // compile error: duplicate definition return 0 ; } \u7c7b\u4f3c\u5730\uff0c\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u5728\u7a0b\u5e8f\u4e2d\u88ab\u5b9a\u4e49\u4e86\u591a\u6b21\uff0c\u4e5f\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int foo () // this is a definition for function foo { return 5 ; } int foo () // compile error: duplicate definition { return 5 ; } int main () { std :: cout << foo (); return 0 ; } \u5c3d\u7ba1\u8fd9\u4e9b\u95ee\u9898\u4fee\u590d\u8d77\u6765\u5e76\u4e0d\u96be\uff08\u79fb\u9664\u91cd\u590d\u5b9a\u4e49\u5373\u53ef\uff09\uff0c\u4f46\u662f\u5bf9\u4e8e\u4f7f\u7528\u5934\u6587\u4ef6\u7684\u60c5\u51b5\u6765\u8bf4\uff0c\u5982\u679c\u5934\u6587\u4ef6\u4e2d\u5b58\u5728\u5b9a\u4e49\uff0c\u90a3\u4e48\u5b83\u5f88\u6709\u53ef\u80fd\u4f1a\u88ab\u91cd\u590d\u5730\u5305\u542b\u5230\u4ee3\u7801\u4e2d\u3002\u5c24\u5176\u662f\u4e00\u4e2a\u5934\u6587\u4ef6\u4e2d\u8fd8\u5305\u62ec\u5176\u4ed6\u5934\u6587\u4ef6\u7684\u60c5\u51b5\uff08\u5f88\u5e38\u89c1\uff09\u3002 \u8bf7\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a square.h 1 2 3 4 5 // \u5934\u6587\u4ef6\u91cc\u4e0d\u5e94\u8be5\u5305\u542b\u51fd\u6570\u7684\u5b9a\u4e49\uff0c\u8fd9\u91cc\u53ea\u662f\u4e3a\u4e86\u4e3e\u4f8b int getSquareSides () { return 4 ; } geometry.h 1 #include \"square.h\" main.cpp 1 2 3 4 5 6 7 #include \"square.h\" #include \"geometry.h\" int main () { return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u770b\u4e0a\u53bb\u6ca1\u4ec0\u4e48\u95ee\u9898\uff0c\u4f46\u5b9e\u9645\u4e0a\u5374\u4e0d\u80fd\u7f16\u8bd1\uff01\u539f\u56e0\u5982\u4e0b\uff1a\u9996\u5148\uff0c main.cpp \u6587\u4ef6 #includes \u4e86 square.h \uff0c\u56e0\u6b64\u4f1a\u628a\u51fd\u6570 getSquareSides_into \u7684\u5b9a\u4e49\u62f7\u8d1d\u5230\u4e86 main.cpp \u3002 main.cpp \u6587\u4ef6\u540c\u65f6\u8fd8\u5305\u542b\u4e86 #includes geometry.h \u8fdb\u800c #includes \u4e86 square.h \u3002\u8fd9\u6837 square.h \u7684\u5b9a\u4e49\uff08\u5305\u542b\u4e86 getSquareSides \u7684\u5b9a\u4e49\uff09\u4e5f\u88ab\u62f7\u8d1d\u5230\u4e86 geometry.h \uff0c\u8fdb\u800c\u4e5f\u88ab\u62f7\u8d1d\u5230\u4e86 main.cpp \u3002 \u56e0\u6b64\uff0c\u5728\u89e3\u6790\u4e86\u7684\u5168\u90e8\u7684\u5934\u6587\u4ef6\u540e\uff0c main.cpp \u7684\u5185\u5bb9\u53d8\u6210\u4e86\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int getSquareSides () // from square.h { return 4 ; } int getSquareSides () // from geometry.h (via square.h) { return 4 ; } int main () { return 0 ; } \u5355\u72ec\u770b\u6bcf\u4e2a\u51fd\u6570\u90fd\u6ca1\u6709\u95ee\u9898\uff0c\u4e0d\u8fc7\uff0c\u7531\u4e8e\u6700\u7ec8\u7684\u7ed3\u679c\u7b49\u6548\u4e8e _main.cpp_ \u5305\u542b\u4e86\u4e24\u6b21 square.h \uff0c\u6240\u4ee5\u624d\u9047\u5230\u4e86\u4e0a\u8ff0\u95ee\u9898\u3002 \u5982\u679c geometry.h \u9700\u8981 getSquareSides() \u800c\u4e14 main.cpp \u9700\u8981 geometry.h \u548c square.h \uff0c\u90a3\u4e48\u5e94\u8be5\u5982\u4f55\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\u5462\uff1f \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e \u597d\u6d88\u606f\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 \u5934\u6587\u4ef6\u9632\u8303 header guards \uff08\u6216 include \u9632\u8303\uff09\u6280\u672f\u6765\u907f\u514d\u4e0a\u8ff0\u95ee\u9898\u3002\u5934\u6587\u4ef6\u9632\u8303\u5176\u5b9e\u662f\u4e00\u79cd\u6761\u4ef6\u7f16\u8bd1\uff0c\u5b83\u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 2 3 4 5 6 #ifndef SOME_UNIQUE_NAME_HERE #define SOME_UNIQUE_NAME_HERE // your declarations (and certain types of definitions) here #endif \u5f53\u5934\u6587\u4ef6\u88ab\u5305\u542b\u65f6\uff0c\u9884\u5904\u7406\u5668\u4f1a\u68c0\u67e5 SOME_UNIQUE_NAME_HERE \u662f\u5426\u5df2\u7ecf\u88ab\u5b9a\u4e49\u8fc7\u3002\u5982\u679c\u6ca1\u6709\u8bf4\u660e\u8fd9\u662f\u7b2c\u4e00\u6b21\u5f15\u5165\u8be5\u5934\u6587\u4ef6\uff0c SOME_UNIQUE_NAME_HERE \u80af\u5b9a\u5c1a\u672a\u88ab\u5b9a\u4e49\u3002\u6b64\u65f6\uff0c\u8be5\u5934\u6587\u4ef6\u4f1a #defines SOME_UNIQUE_NAME_HERE \u5e76\u4e14\u5305\u542b\u5934\u6587\u4ef6\u7684\u5185\u5bb9\u3002\u5982\u679c\u8be5\u5934\u6587\u4ef6\u4e4b\u540e\u53c8\u88ab\u5305\u542b\u4e86\uff0c\u90a3\u4e48\u7531\u4e8e SOME_UNIQUE_NAME_HERE \u4e4b\u524d\u5df2\u7ecf\u88ab\u5b9a\u4e49\u8fc7\u4e86\uff0c\u6240\u4ee5\u8be5\u5934\u6587\u4ef6\u4e2d\u5305\u542b\u7684\u5185\u5bb9\u5c06\u4f1a\u88ab\u5ffd\u7565\uff08\u7531\u4e8e #ifndef \u7684\u5b58\u5728\uff09 \u6211\u4eec\u4f7f\u7528\u7684\u6240\u6709\u5934\u6587\u4ef6\uff0c\u90fd\u5e94\u8be5\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e\u3002 SOME_UNIQUE_NAME_HERE \u53ef\u4ee5\u662f\u4efb\u4f55\u540d\u5b57\uff0c\u4f46\u662f\u901a\u5e38\u7684\u60ef\u4f8b\u662f\u4f7f\u7528\u5934\u6587\u4ef6\u7684 \u5168\u540d \u3001\u5168\u90e8\u5927\u5199\u5b57\u6bcd\u5e76\u4f7f\u7528\u4e0b\u5212\u7ebf\u4ee3\u66ff\u7a7a\u683c\u6216\u6807\u70b9\uff0c\u4f8b\u5982 square.h \u7684\u5934\u6587\u4ef6\u9632\u8303\u5982\u4e0b\uff1a square.h 1 2 3 4 5 6 7 8 9 #ifndef SQUARE_H #define SQUARE_H int getSquareSides () { return 4 ; } #endif \u6807\u51c6\u5e93\u4e2d\u4e5f\u4f7f\u7528\u4e86\u5934\u6587\u4ef6\u9632\u8303\u3002\u5982\u679c\u4f60\u53bb\u770b\u770b iostream \u7684\u5934\u6587\u4ef6\uff0c\u4f60\u4f1a\u53d1\u73b0\u5b83\u662f\u8fd9\u6837\u7684\uff1a iostream 1 2 3 4 5 6 #ifndef _IOSTREAM_ #define _IOSTREAM_ // content here #endif \u6269\u5c55\u9605\u8bfb \u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u4f7f\u7528\u4e24\u4e2a\u6765\u81ea\u4e24\u4e2a\u4e0d\u540c\u76ee\u5f55\u7684\u540c\u540d\u5934\u6587\u4ef6\u4e5f\u662f\u53ef\u80fd\u7684(\u4f8b\u5982\uff1a directoryA\\config.h \u548c directoryB\\config.h )\u3002\u5982\u679c\u5934\u6587\u4ef6\u9632\u8303\u53ea\u5229\u7528\u4e86\u6587\u4ef6\u540d\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u5934\u6587\u4ef6\u7684\u5934\u6587\u4ef6\u9632\u8303\u5e94\u8be5\u4e5f\u662f\u4e00\u6837\u7684\uff08\u4f8b\u5982\uff1a CONFIG_H \uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4efb\u4f55\u540c\u65f6\u5305\u542b\u4e86\u4e0a\u8ff0\u4e24\u4e2a\u5934\u6587\u4ef6\u7684\u6587\u4ef6\uff0c\u5176\u5b9e\u5728\u5305\u542b\u7b2c\u4e8c\u4e2a\u7684\u65f6\u5019\u662f\u4e0d\u751f\u6548\u7684\uff0c\u8fd9\u6837\u5c31\u6709\u53ef\u80fd\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 \u6b63\u662f\u7531\u4e8e\u5b58\u5728\u5934\u6587\u4ef6\u9632\u8303\u51b2\u7a81\u7684\u53ef\u80fd\uff0c\u5f88\u591a\u5f00\u53d1\u8005\u5efa\u8bae\u4f7f\u7528\u66f4\u590d\u6742/\u72ec\u7279\u7684\u65b9\u5f0f\u6765\u6784\u5efa\u76f8\u5173\u7684\u5b8f\u3002\u4f8b\u5982\uff1a ___H \u3001 __H \u6216\u8005 __H \u3002 \u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\u66f4\u65b0\u4e4b\u524d\u7684\u4f8b\u5b50 \u518d\u56de\u5230 square.h \u7684\u4f8b\u5b50\uff0c\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u62a4\u6765\u5904\u7406 square.h \u3002\u4e3a\u4e86\u4fdd\u6301\u4e00\u81f4\uff0c\u6211\u4eec\u5728 geometry.h \u4e2d\u4e5f\u6dfb\u52a0\u4e86\u5934\u6587\u4ef6\u5939\u9632\u62a4\u3002 square.h 1 2 3 4 5 6 7 8 9 #ifndef SQUARE_H #define SQUARE_H int getSquareSides () { return 4 ; } #endif geometry.h 1 2 3 4 5 6 #ifndef GEOMETRY_H #define GEOMETRY_H #include \"square.h\" #endif main.cpp 1 2 3 4 5 6 7 #include \"square.h\" #include \"geometry.h\" int main () { return 0 ; } \u5728\u9884\u5904\u7406\u5668\u89e3\u6790\u5168\u90e8\u5934\u6587\u4ef6\u540e\uff0c\u7a0b\u5e8f\u5185\u5bb9\u53d8\u6210\u4e86\u4e0b\u9762\u8fd9\u6837\uff1a main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 // Square.h included from main.cpp #ifndef SQUARE_H // square.h included from main.cpp #define SQUARE_H // SQUARE_H gets defined here // and all this content gets included int getSquareSides () { return 4 ; } #endif // SQUARE_H #ifndef GEOMETRY_H // geometry.h included from main.cpp #define GEOMETRY_H #ifndef SQUARE_H // square.h included from geometry.h, SQUARE_H is already defined from above #define SQUARE_H // so none of this content gets included int getSquareSides () { return 4 ; } #endif // SQUARE_H #endif // GEOMETRY_H int main () { return 0 ; } \u4ece\u4e0a\u9762\u7684\u4f8b\u5b50\u53ef\u4ee5\u770b\u51fa\uff0c\u5f53\u7b2c\u4e8c\u6b21\u5305\u542b square.h \u7684\u65f6\u5019\uff0c\u7531\u4e8e SQUARE_H \u5df2\u7ecf\u88ab\u5b9a\u4e49\u8fc7\u4e86\u3002\u56e0\u6b64 getSquareSides \u51fd\u6570\u53ea\u4f1a\u88ab\u5305\u542b\u4e00\u6b21\u3002 \u5934\u6587\u4ef6\u9632\u8303\u4e0d\u80fd\u9632\u6b62\u5934\u6587\u4ef6\u88ab\u5305\u542b\u5230\u4e0d\u540c\u7684\u6587\u4ef6 \u5934\u6587\u4ef6\u9632\u8303\u7684\u76ee\u6807\u662f\u9632\u6b62\u5934\u6587\u4ef6\u88ab\u591a\u6b21\u5305\u542b\u3002\u4ece\u8bbe\u8ba1\u4e0a\u6765\u8bb2\uff0c\u5b83\u5e76\u4e0d\u80fd\u4fdd\u62a4\u5934\u6587\u4ef6\u88ab\u5305\u542b\u5230\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\uff08\u6bcf\u4e2a\u6587\u4ef6\u53ea\u80fd\u5305\u542b\u4e00\u6b21\uff09\u3002\u8fd9\u53ef\u80fd\u5bfc\u81f4\u96be\u4ee5\u9884\u6599\u7684\u95ee\u9898\u3002 \u8003\u8651\u5982\u4e0b\u60c5\u51b5\uff1a square.h 1 2 3 4 5 6 7 8 9 10 11 #ifndef SQUARE_H #define SQUARE_H int getSquareSides () { return 4 ; } int getSquarePerimeter ( int sideLength ); // getSquarePerimeter \u7684\u524d\u5411\u58f0\u660e #endif square.cpp 1 2 3 4 5 6 #include \"square.h\" // square.h \u5728\u6b64\u5904\u88ab\u5305\u542b\u4e00\u6b21 int getSquarePerimeter ( int sideLength ) { return sideLength * getSquareSides (); } main.cpp 1 2 3 4 5 6 7 8 9 10 #include \"square.h\" // square.h \u5728\u6b64\u5904\u88ab\u5305\u542b\u4e00\u6b21 #include int main () { std :: cout << \"a square has \" << getSquareSides () << \" sides \\n \" ; std :: cout << \"a square of length 5 has perimeter length \" << getSquarePerimeter ( 5 ) << '\\n' ; return 0 ; } \u6ce8\u610f square.h \u88ab\u5305\u542b\u5230\u4e86 main.cpp \u548c square.cpp \u4e2d\u3002\u8fd9\u610f\u5473\u7740 square.h \u7684\u5185\u5bb9\u88ab square.cpp \u548c main.cpp \u5404\u5305\u542b\u4e86\u4e00\u6b21\u3002 \u8ba9\u6211\u4eec\u4ed4\u7ec6\u8bb2\u89e3\u4e00\u4e0b\u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\u3002\u5f53 square.h \u88ab\u5305\u542b\u5230 square.cpp \u65f6 _SQUARE_H_ \u88ab\u5b9a\u4e49\u4e86\uff0c\u5b83\u7684\u5b9a\u4e49\u5230 square.cpp \u6587\u4ef6\u7684\u672b\u5c3e\u4e3a\u6b62\u3002\u8be5\u5b9a\u4e49\u53ef\u4ee5\u907f\u514d square.h \u7684\u5185\u5bb9\u591a\u6b21\u88ab\u5305\u542b\u5230 square.cpp \u4e2d \u3002\u4f46\u662f\uff0c square.cpp \u5904\u7406\u5b8c\u6210\u540e\uff0c SQUARE_H \u5c31\u6ca1\u6709\u5b9a\u4e49\u4e86\u3002\u8fd9\u610f\u5473\u5f53\u9884\u5904\u7406\u5668\u5904\u7406 main.cpp \u65f6\uff0c SQUARE_H \u5c1a\u672a\u5728 main.cpp \u4e2d\u5b9a\u4e49\u3002 \u7531\u4e8e square.cpp \u548c main.cpp \u4e2d\u90fd\u5305\u542b\u4e86 getSquareSides \u51fd\u6570\u7684\u5b9a\u4e49\u3002\u6b64\u65f6\uff0c\u6587\u4ef6\u662f\u53ef\u4ee5\u7f16\u8bd1\u7684\uff0c\u4f46\u662f\u5728\u94fe\u63a5\u65f6\u94fe\u63a5\u5668\u4f1a\u62a5\u544a\u7a0b\u5e8f\u5b58\u5728 getSquareSides \u51fd\u6570\u7684\u91cd\u590d\u5b9a\u4e49\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u6700\u540e\u7684\u65b9\u6cd5\u662f\u628a\u51fd\u6570\u5b9a\u4e49\u653e\u5728\u4e00\u4e2a .cpp \u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u5934\u6587\u4ef6\u4e2d\u5c31\u53ea\u5305\u542b\u51fd\u6570\u7684\u58f0\u660e\uff1a square.h 1 2 3 4 5 6 7 #ifndef SQUARE_H #define SQUARE_H int getSquareSides (); // forward declaration for getSquareSides int getSquarePerimeter ( int sideLength ); // forward declaration for getSquarePerimeter #endif square.cpp 1 2 3 4 5 6 7 8 9 10 11 #include \"square.h\" int getSquareSides () // actual definition for getSquareSides { return 4 ; } int getSquarePerimeter ( int sideLength ) { return sideLength * getSquareSides (); } main.cpp 1 2 3 4 5 6 7 8 9 10 #include \"square.h\" // square.h is also included once here #include int main () { std :: cout << \"a square has \" << getSquareSides () << \"sides \\n \" ; std :: cout << \"a square of length 5 has perimeter length \" << getSquarePerimeter ( 5 ) << '\\n' ; return 0 ; } \u73b0\u5728\uff0c \u7a0b\u5e8f\u5728\u7f16\u8bd1\u7684\u65f6\u5019\uff0c getSquareSides \u51fd\u6570\u53ea\u6709\u4e00\u4e2a\u5b9a\u4e49\u4e86\uff08\u6765\u81ea square.cpp \uff09\uff0c\u56e0\u6b64\u94fe\u63a5\u5668\u4e0d\u4f1a\u518d\u62a5\u9519\u3002 main.cpp \u53ef\u4ee5\u8c03\u7528\u8be5\u51fd\u6570 (\u5373\u4f7f\u5b83\u7684\u5b9a\u4e49\u5728 square.cpp \u4e2d) \uff0c\u56e0\u4e3a\u5b83\u5305\u542b\u4e86 square.h \uff08\u5305\u542b\u8be5\u51fd\u6570\u7684\u58f0\u660e\uff09\uff0c\u94fe\u63a5\u5668\u4f1a\u628a main.cpp \u4e2d\u5bf9 getSquareSides \u51fd\u6570\u7684\u8c03\u7528\u5173\u8054\u5230 square.cpp \u4e2d getSquareSides \u51fd\u6570\u7684\u5b9a\u4e49\u3002 \u4e0d\u628a\u5b9a\u4e49\u653e\u5728\u5934\u6587\u4ef6\u4e2d\u4e0d\u5c31\u884c\u4e86\uff1f \u4e4b\u524d\u6211\u4eec\u8bf4\u8fc7\uff0c\u4e0d\u8981\u628a\u51fd\u6570\u7684\u5b9a\u4e49\u653e\u5728\u5934\u6587\u4ef6\u4e2d\u3002\u6240\u4ee5\u4f60\u53ef\u80fd\u4f1a\u95ee\uff0c\u90a3\u4e3a\u4ec0\u4e48\u8fd8\u8981\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\u5462\uff1f\u6bd5\u7adf\u5b83\u8981\u89e3\u51b3\u7684\u95ee\u9898\u4ece\u4e00\u5f00\u59cb\u5c31\u5e94\u8be5\u907f\u514d\u3002 \u5176\u5b9e\uff0c\u540e\u9762\u6211\u4eec\u4f1a\u5411\u4f60\u5c55\u793a\u4e00\u4e9b \u975e\u51fd\u6570 \u5b9a\u4e49\u88ab\u653e\u7f6e\u5728\u5934\u6587\u4ef6\u4e2d\u7684\u4f8b\u5b50\u3002\u4f8b\u5982\uff0cC++ \u5141\u8bb8\u4f60\u521b\u5efa\u81ea\u5b9a\u4e49\u7c7b\u578b\u3002\u8fd9\u4e9b\u81ea\u5b9a\u4e49\u7c7b\u578b\u901a\u5e38\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u624d\u80fd\u8ba9\u5bf9\u5e94\u7684\u6e90\u6587\u4ef6\u4f7f\u7528\u8fd9\u4e9b\u5b9a\u4e49\u3002\u5982\u679c\u4e0d\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\uff0c\u5219\u5b83\u4eec\u5f88\u53ef\u80fd\u4f1a\u88ab\u591a\u6b21\u62f7\u8d1d\uff0c\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\u3002 \u56e0\u6b64\uff0c\u5373\u4fbf\u6559\u7a0b\u770b\u5230\u8fd9\u91cc\u7684\u65f6\u5019\u4f60\u8fd8\u6ca1\u6709\u5fc5\u8981\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\uff0c\u80fd\u591f\u4ece\u73b0\u5728\u5c31\u517b\u6210\u4e60\u60ef\u8fd8\u662f\u5f88\u597d\u7684\u3002 #pragma once \u73b0\u4ee3\u7f16\u8bd1\u5668\u652f\u6301\u4e00\u79cd\u66f4\u7b80\u5355\u7684\u5904\u7406\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528 #pragma \u6307\u4ee4\u6765\u4ee3\u66ff\u5934\u6587\u4ef6\u9632\u8303\uff1a 1 2 3 #pragma once // your code here #pragma once \u7684\u529f\u80fd\u548c\u5934\u6587\u4ef6\u9632\u8303\u662f\u4e00\u6837\u7684\uff0c\u800c\u4e14\u66f4\u52a0\u7b80\u77ed\u4e14\u4e0d\u6613\u51fa\u9519\u3002\u5728\u5927\u591a\u6570\u7684\u9879\u76ee\u4e2d\uff0c #pragma once \u90fd\u53ef\u4ee5\u5f88\u597d\u7684\u5de5\u4f5c\uff0c\u56e0\u6b64\u5f88\u591a\u5f00\u53d1\u8005\u90fd\u503e\u5411\u4e8e\u4f7f\u7528\u5b83\u800c\u4e0d\u662f\u5934\u6587\u4ef6\u9632\u8303\u3002\u4e0d\u8fc7\uff0c #pragma once \u5e76\u4e0d\u662f C++ \u8bed\u8a00\u7684\u4e00\u90e8\u5206 (\u800c\u4e14\u53ef\u80fd\u6c38\u8fdc\u90fd\u4e0d\u4f1a\u6210\u4e3a C++ \u7684\u4e00\u90e8\u5206\uff0c\u56e0\u4e3a\u6ca1\u529e\u6cd5\u4fdd\u8bc1\u5728\u4efb\u548c\u60c5\u51b5\u4e0b\u5b83\u90fd\u80fd\u6b63\u786e\u5730\u5de5\u4f5c\uff09 \u5904\u4e8e\u517c\u5bb9\u6027\u7684\u8003\u8651\uff0c\u6211\u4eec\u8fd8\u662f\u63a8\u8350\u4f7f\u7528\u4f20\u7edf\u65b9\u5f0f\u2014\u2014\u5934\u6587\u4ef6\u9632\u8303\u3002\u5934\u6587\u4ef6\u9632\u8303\u7528\u8d77\u6765\u4e5f\u4e0d\u590d\u6742\u800c\u4e14\u6240\u6709\u7684\u7f16\u8bd1\u5668\u90fd\u652f\u6301\u3002 \u6700\u4f73\u5b9e\u8df5 \u5904\u4e8e\u517c\u5bb9\u6027\u7684\u8003\u8651\uff0c\u4f18\u5148\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\u800c\u975e #pragma once \u3002 \u5c0f\u7ed3 \u5934\u6587\u4ef6\u9632\u8303\u53ef\u4ee5\u786e\u4fdd\u8be5\u5934\u6587\u4ef6\u7684\u5185\u5bb9\u88ab\u5305\u542b\u5230\u67d0\u4e2a\u6587\u4ef6\u65f6\uff0c\u5176\u5185\u5bb9\u53ea\u88ab\u62f7\u8d1d\u4e00\u6b21\uff0c\u4ee5\u907f\u514d\u91cd\u590d\u5b9a\u4e49\u7684\u95ee\u9898\u3002 \u6ce8\u610f\uff0c\u91cd\u590d\u58f0\u660e\u662f\u53ef\u4ee5\u7684\uff0c\u56e0\u4e3a\u591a\u6b21\u58f0\u660e\u5e76\u4e0d\u4f1a\u5e26\u6765\u4efb\u4f55\u95ee\u9898\u3002\u4e0d\u8fc7\uff0c\u5373\u4f7f\u4f60\u7684\u5934\u6587\u4ef6\u4e2d\u53ea\u5305\u542b\u58f0\u660e\uff0c\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\u4ecd\u7136\u662f\u6700\u4f73\u5b9e\u8df5\u3002 \u6ce8\u610f\uff0c\u5934\u6587\u4ef6\u9632\u8303\u4e0d\u80fd\u907f\u514d\u5934\u6587\u4ef6\u7684\u5185\u5bb9\u88ab\u62f7\u8d1d\u5230\u591a\u4e2a\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\u3002\u4e0d\u8fc7\uff0c\u8fd9\u662f\u597d\u4e8b\uff0c\u56e0\u4e3a\u6211\u4eec\u65f6\u5e38\u9700\u8981\u5c06\u5934\u6587\u4ef6\u5305\u542b\u5230\u591a\u4e2a\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\u3002","title":"2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e"},{"location":"2-12-Header-guards/#_1","text":"\u5728 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u53d8\u91cf\u548c\u51fd\u6570\u7684\u6807\u8bc6\u7b26\u53ea\u80fd\u88ab\u5b9a\u4e49\u4e00\u6b21\uff0c\u5373 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219 \u3002\u56e0\u6b64\uff0c\u5982\u679c\u4e00\u4e2a\u7a0b\u5e8f\u5305\u542b\u4e86\u5bf9\u67d0\u4e2a\u6807\u8bc6\u7b26\u7684\u591a\u6b21\u5b9a\u4e49\uff0c\u5c06\u4f1a\u5f15\u8d77\u7f16\u8bd1\u9519\u8bef \uff1a 1 2 3 4 5 6 7 int main () { int x ; // this is a definition for variable x int x ; // compile error: duplicate definition return 0 ; } \u7c7b\u4f3c\u5730\uff0c\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u5728\u7a0b\u5e8f\u4e2d\u88ab\u5b9a\u4e49\u4e86\u591a\u6b21\uff0c\u4e5f\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int foo () // this is a definition for function foo { return 5 ; } int foo () // compile error: duplicate definition { return 5 ; } int main () { std :: cout << foo (); return 0 ; } \u5c3d\u7ba1\u8fd9\u4e9b\u95ee\u9898\u4fee\u590d\u8d77\u6765\u5e76\u4e0d\u96be\uff08\u79fb\u9664\u91cd\u590d\u5b9a\u4e49\u5373\u53ef\uff09\uff0c\u4f46\u662f\u5bf9\u4e8e\u4f7f\u7528\u5934\u6587\u4ef6\u7684\u60c5\u51b5\u6765\u8bf4\uff0c\u5982\u679c\u5934\u6587\u4ef6\u4e2d\u5b58\u5728\u5b9a\u4e49\uff0c\u90a3\u4e48\u5b83\u5f88\u6709\u53ef\u80fd\u4f1a\u88ab\u91cd\u590d\u5730\u5305\u542b\u5230\u4ee3\u7801\u4e2d\u3002\u5c24\u5176\u662f\u4e00\u4e2a\u5934\u6587\u4ef6\u4e2d\u8fd8\u5305\u62ec\u5176\u4ed6\u5934\u6587\u4ef6\u7684\u60c5\u51b5\uff08\u5f88\u5e38\u89c1\uff09\u3002 \u8bf7\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a square.h 1 2 3 4 5 // \u5934\u6587\u4ef6\u91cc\u4e0d\u5e94\u8be5\u5305\u542b\u51fd\u6570\u7684\u5b9a\u4e49\uff0c\u8fd9\u91cc\u53ea\u662f\u4e3a\u4e86\u4e3e\u4f8b int getSquareSides () { return 4 ; } geometry.h 1 #include \"square.h\" main.cpp 1 2 3 4 5 6 7 #include \"square.h\" #include \"geometry.h\" int main () { return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u770b\u4e0a\u53bb\u6ca1\u4ec0\u4e48\u95ee\u9898\uff0c\u4f46\u5b9e\u9645\u4e0a\u5374\u4e0d\u80fd\u7f16\u8bd1\uff01\u539f\u56e0\u5982\u4e0b\uff1a\u9996\u5148\uff0c main.cpp \u6587\u4ef6 #includes \u4e86 square.h \uff0c\u56e0\u6b64\u4f1a\u628a\u51fd\u6570 getSquareSides_into \u7684\u5b9a\u4e49\u62f7\u8d1d\u5230\u4e86 main.cpp \u3002 main.cpp \u6587\u4ef6\u540c\u65f6\u8fd8\u5305\u542b\u4e86 #includes geometry.h \u8fdb\u800c #includes \u4e86 square.h \u3002\u8fd9\u6837 square.h \u7684\u5b9a\u4e49\uff08\u5305\u542b\u4e86 getSquareSides \u7684\u5b9a\u4e49\uff09\u4e5f\u88ab\u62f7\u8d1d\u5230\u4e86 geometry.h \uff0c\u8fdb\u800c\u4e5f\u88ab\u62f7\u8d1d\u5230\u4e86 main.cpp \u3002 \u56e0\u6b64\uff0c\u5728\u89e3\u6790\u4e86\u7684\u5168\u90e8\u7684\u5934\u6587\u4ef6\u540e\uff0c main.cpp \u7684\u5185\u5bb9\u53d8\u6210\u4e86\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int getSquareSides () // from square.h { return 4 ; } int getSquareSides () // from geometry.h (via square.h) { return 4 ; } int main () { return 0 ; } \u5355\u72ec\u770b\u6bcf\u4e2a\u51fd\u6570\u90fd\u6ca1\u6709\u95ee\u9898\uff0c\u4e0d\u8fc7\uff0c\u7531\u4e8e\u6700\u7ec8\u7684\u7ed3\u679c\u7b49\u6548\u4e8e _main.cpp_ \u5305\u542b\u4e86\u4e24\u6b21 square.h \uff0c\u6240\u4ee5\u624d\u9047\u5230\u4e86\u4e0a\u8ff0\u95ee\u9898\u3002 \u5982\u679c geometry.h \u9700\u8981 getSquareSides() \u800c\u4e14 main.cpp \u9700\u8981 geometry.h \u548c square.h \uff0c\u90a3\u4e48\u5e94\u8be5\u5982\u4f55\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\u5462\uff1f","title":"\u91cd\u590d\u5b9a\u4e49\u95ee\u9898"},{"location":"2-12-Header-guards/#_2","text":"\u597d\u6d88\u606f\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 \u5934\u6587\u4ef6\u9632\u8303 header guards \uff08\u6216 include \u9632\u8303\uff09\u6280\u672f\u6765\u907f\u514d\u4e0a\u8ff0\u95ee\u9898\u3002\u5934\u6587\u4ef6\u9632\u8303\u5176\u5b9e\u662f\u4e00\u79cd\u6761\u4ef6\u7f16\u8bd1\uff0c\u5b83\u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 2 3 4 5 6 #ifndef SOME_UNIQUE_NAME_HERE #define SOME_UNIQUE_NAME_HERE // your declarations (and certain types of definitions) here #endif \u5f53\u5934\u6587\u4ef6\u88ab\u5305\u542b\u65f6\uff0c\u9884\u5904\u7406\u5668\u4f1a\u68c0\u67e5 SOME_UNIQUE_NAME_HERE \u662f\u5426\u5df2\u7ecf\u88ab\u5b9a\u4e49\u8fc7\u3002\u5982\u679c\u6ca1\u6709\u8bf4\u660e\u8fd9\u662f\u7b2c\u4e00\u6b21\u5f15\u5165\u8be5\u5934\u6587\u4ef6\uff0c SOME_UNIQUE_NAME_HERE \u80af\u5b9a\u5c1a\u672a\u88ab\u5b9a\u4e49\u3002\u6b64\u65f6\uff0c\u8be5\u5934\u6587\u4ef6\u4f1a #defines SOME_UNIQUE_NAME_HERE \u5e76\u4e14\u5305\u542b\u5934\u6587\u4ef6\u7684\u5185\u5bb9\u3002\u5982\u679c\u8be5\u5934\u6587\u4ef6\u4e4b\u540e\u53c8\u88ab\u5305\u542b\u4e86\uff0c\u90a3\u4e48\u7531\u4e8e SOME_UNIQUE_NAME_HERE \u4e4b\u524d\u5df2\u7ecf\u88ab\u5b9a\u4e49\u8fc7\u4e86\uff0c\u6240\u4ee5\u8be5\u5934\u6587\u4ef6\u4e2d\u5305\u542b\u7684\u5185\u5bb9\u5c06\u4f1a\u88ab\u5ffd\u7565\uff08\u7531\u4e8e #ifndef \u7684\u5b58\u5728\uff09 \u6211\u4eec\u4f7f\u7528\u7684\u6240\u6709\u5934\u6587\u4ef6\uff0c\u90fd\u5e94\u8be5\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e\u3002 SOME_UNIQUE_NAME_HERE \u53ef\u4ee5\u662f\u4efb\u4f55\u540d\u5b57\uff0c\u4f46\u662f\u901a\u5e38\u7684\u60ef\u4f8b\u662f\u4f7f\u7528\u5934\u6587\u4ef6\u7684 \u5168\u540d \u3001\u5168\u90e8\u5927\u5199\u5b57\u6bcd\u5e76\u4f7f\u7528\u4e0b\u5212\u7ebf\u4ee3\u66ff\u7a7a\u683c\u6216\u6807\u70b9\uff0c\u4f8b\u5982 square.h \u7684\u5934\u6587\u4ef6\u9632\u8303\u5982\u4e0b\uff1a square.h 1 2 3 4 5 6 7 8 9 #ifndef SQUARE_H #define SQUARE_H int getSquareSides () { return 4 ; } #endif \u6807\u51c6\u5e93\u4e2d\u4e5f\u4f7f\u7528\u4e86\u5934\u6587\u4ef6\u9632\u8303\u3002\u5982\u679c\u4f60\u53bb\u770b\u770b iostream \u7684\u5934\u6587\u4ef6\uff0c\u4f60\u4f1a\u53d1\u73b0\u5b83\u662f\u8fd9\u6837\u7684\uff1a iostream 1 2 3 4 5 6 #ifndef _IOSTREAM_ #define _IOSTREAM_ // content here #endif \u6269\u5c55\u9605\u8bfb \u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u4f7f\u7528\u4e24\u4e2a\u6765\u81ea\u4e24\u4e2a\u4e0d\u540c\u76ee\u5f55\u7684\u540c\u540d\u5934\u6587\u4ef6\u4e5f\u662f\u53ef\u80fd\u7684(\u4f8b\u5982\uff1a directoryA\\config.h \u548c directoryB\\config.h )\u3002\u5982\u679c\u5934\u6587\u4ef6\u9632\u8303\u53ea\u5229\u7528\u4e86\u6587\u4ef6\u540d\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u5934\u6587\u4ef6\u7684\u5934\u6587\u4ef6\u9632\u8303\u5e94\u8be5\u4e5f\u662f\u4e00\u6837\u7684\uff08\u4f8b\u5982\uff1a CONFIG_H \uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4efb\u4f55\u540c\u65f6\u5305\u542b\u4e86\u4e0a\u8ff0\u4e24\u4e2a\u5934\u6587\u4ef6\u7684\u6587\u4ef6\uff0c\u5176\u5b9e\u5728\u5305\u542b\u7b2c\u4e8c\u4e2a\u7684\u65f6\u5019\u662f\u4e0d\u751f\u6548\u7684\uff0c\u8fd9\u6837\u5c31\u6709\u53ef\u80fd\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 \u6b63\u662f\u7531\u4e8e\u5b58\u5728\u5934\u6587\u4ef6\u9632\u8303\u51b2\u7a81\u7684\u53ef\u80fd\uff0c\u5f88\u591a\u5f00\u53d1\u8005\u5efa\u8bae\u4f7f\u7528\u66f4\u590d\u6742/\u72ec\u7279\u7684\u65b9\u5f0f\u6765\u6784\u5efa\u76f8\u5173\u7684\u5b8f\u3002\u4f8b\u5982\uff1a ___H \u3001 __H \u6216\u8005 __H \u3002","title":"\u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e"},{"location":"2-12-Header-guards/#_3","text":"\u518d\u56de\u5230 square.h \u7684\u4f8b\u5b50\uff0c\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u62a4\u6765\u5904\u7406 square.h \u3002\u4e3a\u4e86\u4fdd\u6301\u4e00\u81f4\uff0c\u6211\u4eec\u5728 geometry.h \u4e2d\u4e5f\u6dfb\u52a0\u4e86\u5934\u6587\u4ef6\u5939\u9632\u62a4\u3002 square.h 1 2 3 4 5 6 7 8 9 #ifndef SQUARE_H #define SQUARE_H int getSquareSides () { return 4 ; } #endif geometry.h 1 2 3 4 5 6 #ifndef GEOMETRY_H #define GEOMETRY_H #include \"square.h\" #endif main.cpp 1 2 3 4 5 6 7 #include \"square.h\" #include \"geometry.h\" int main () { return 0 ; } \u5728\u9884\u5904\u7406\u5668\u89e3\u6790\u5168\u90e8\u5934\u6587\u4ef6\u540e\uff0c\u7a0b\u5e8f\u5185\u5bb9\u53d8\u6210\u4e86\u4e0b\u9762\u8fd9\u6837\uff1a main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 // Square.h included from main.cpp #ifndef SQUARE_H // square.h included from main.cpp #define SQUARE_H // SQUARE_H gets defined here // and all this content gets included int getSquareSides () { return 4 ; } #endif // SQUARE_H #ifndef GEOMETRY_H // geometry.h included from main.cpp #define GEOMETRY_H #ifndef SQUARE_H // square.h included from geometry.h, SQUARE_H is already defined from above #define SQUARE_H // so none of this content gets included int getSquareSides () { return 4 ; } #endif // SQUARE_H #endif // GEOMETRY_H int main () { return 0 ; } \u4ece\u4e0a\u9762\u7684\u4f8b\u5b50\u53ef\u4ee5\u770b\u51fa\uff0c\u5f53\u7b2c\u4e8c\u6b21\u5305\u542b square.h \u7684\u65f6\u5019\uff0c\u7531\u4e8e SQUARE_H \u5df2\u7ecf\u88ab\u5b9a\u4e49\u8fc7\u4e86\u3002\u56e0\u6b64 getSquareSides \u51fd\u6570\u53ea\u4f1a\u88ab\u5305\u542b\u4e00\u6b21\u3002","title":"\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\u66f4\u65b0\u4e4b\u524d\u7684\u4f8b\u5b50"},{"location":"2-12-Header-guards/#_4","text":"\u5934\u6587\u4ef6\u9632\u8303\u7684\u76ee\u6807\u662f\u9632\u6b62\u5934\u6587\u4ef6\u88ab\u591a\u6b21\u5305\u542b\u3002\u4ece\u8bbe\u8ba1\u4e0a\u6765\u8bb2\uff0c\u5b83\u5e76\u4e0d\u80fd\u4fdd\u62a4\u5934\u6587\u4ef6\u88ab\u5305\u542b\u5230\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\uff08\u6bcf\u4e2a\u6587\u4ef6\u53ea\u80fd\u5305\u542b\u4e00\u6b21\uff09\u3002\u8fd9\u53ef\u80fd\u5bfc\u81f4\u96be\u4ee5\u9884\u6599\u7684\u95ee\u9898\u3002 \u8003\u8651\u5982\u4e0b\u60c5\u51b5\uff1a square.h 1 2 3 4 5 6 7 8 9 10 11 #ifndef SQUARE_H #define SQUARE_H int getSquareSides () { return 4 ; } int getSquarePerimeter ( int sideLength ); // getSquarePerimeter \u7684\u524d\u5411\u58f0\u660e #endif square.cpp 1 2 3 4 5 6 #include \"square.h\" // square.h \u5728\u6b64\u5904\u88ab\u5305\u542b\u4e00\u6b21 int getSquarePerimeter ( int sideLength ) { return sideLength * getSquareSides (); } main.cpp 1 2 3 4 5 6 7 8 9 10 #include \"square.h\" // square.h \u5728\u6b64\u5904\u88ab\u5305\u542b\u4e00\u6b21 #include int main () { std :: cout << \"a square has \" << getSquareSides () << \" sides \\n \" ; std :: cout << \"a square of length 5 has perimeter length \" << getSquarePerimeter ( 5 ) << '\\n' ; return 0 ; } \u6ce8\u610f square.h \u88ab\u5305\u542b\u5230\u4e86 main.cpp \u548c square.cpp \u4e2d\u3002\u8fd9\u610f\u5473\u7740 square.h \u7684\u5185\u5bb9\u88ab square.cpp \u548c main.cpp \u5404\u5305\u542b\u4e86\u4e00\u6b21\u3002 \u8ba9\u6211\u4eec\u4ed4\u7ec6\u8bb2\u89e3\u4e00\u4e0b\u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\u3002\u5f53 square.h \u88ab\u5305\u542b\u5230 square.cpp \u65f6 _SQUARE_H_ \u88ab\u5b9a\u4e49\u4e86\uff0c\u5b83\u7684\u5b9a\u4e49\u5230 square.cpp \u6587\u4ef6\u7684\u672b\u5c3e\u4e3a\u6b62\u3002\u8be5\u5b9a\u4e49\u53ef\u4ee5\u907f\u514d square.h \u7684\u5185\u5bb9\u591a\u6b21\u88ab\u5305\u542b\u5230 square.cpp \u4e2d \u3002\u4f46\u662f\uff0c square.cpp \u5904\u7406\u5b8c\u6210\u540e\uff0c SQUARE_H \u5c31\u6ca1\u6709\u5b9a\u4e49\u4e86\u3002\u8fd9\u610f\u5473\u5f53\u9884\u5904\u7406\u5668\u5904\u7406 main.cpp \u65f6\uff0c SQUARE_H \u5c1a\u672a\u5728 main.cpp \u4e2d\u5b9a\u4e49\u3002 \u7531\u4e8e square.cpp \u548c main.cpp \u4e2d\u90fd\u5305\u542b\u4e86 getSquareSides \u51fd\u6570\u7684\u5b9a\u4e49\u3002\u6b64\u65f6\uff0c\u6587\u4ef6\u662f\u53ef\u4ee5\u7f16\u8bd1\u7684\uff0c\u4f46\u662f\u5728\u94fe\u63a5\u65f6\u94fe\u63a5\u5668\u4f1a\u62a5\u544a\u7a0b\u5e8f\u5b58\u5728 getSquareSides \u51fd\u6570\u7684\u91cd\u590d\u5b9a\u4e49\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u6700\u540e\u7684\u65b9\u6cd5\u662f\u628a\u51fd\u6570\u5b9a\u4e49\u653e\u5728\u4e00\u4e2a .cpp \u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u5934\u6587\u4ef6\u4e2d\u5c31\u53ea\u5305\u542b\u51fd\u6570\u7684\u58f0\u660e\uff1a square.h 1 2 3 4 5 6 7 #ifndef SQUARE_H #define SQUARE_H int getSquareSides (); // forward declaration for getSquareSides int getSquarePerimeter ( int sideLength ); // forward declaration for getSquarePerimeter #endif square.cpp 1 2 3 4 5 6 7 8 9 10 11 #include \"square.h\" int getSquareSides () // actual definition for getSquareSides { return 4 ; } int getSquarePerimeter ( int sideLength ) { return sideLength * getSquareSides (); } main.cpp 1 2 3 4 5 6 7 8 9 10 #include \"square.h\" // square.h is also included once here #include int main () { std :: cout << \"a square has \" << getSquareSides () << \"sides \\n \" ; std :: cout << \"a square of length 5 has perimeter length \" << getSquarePerimeter ( 5 ) << '\\n' ; return 0 ; } \u73b0\u5728\uff0c \u7a0b\u5e8f\u5728\u7f16\u8bd1\u7684\u65f6\u5019\uff0c getSquareSides \u51fd\u6570\u53ea\u6709\u4e00\u4e2a\u5b9a\u4e49\u4e86\uff08\u6765\u81ea square.cpp \uff09\uff0c\u56e0\u6b64\u94fe\u63a5\u5668\u4e0d\u4f1a\u518d\u62a5\u9519\u3002 main.cpp \u53ef\u4ee5\u8c03\u7528\u8be5\u51fd\u6570 (\u5373\u4f7f\u5b83\u7684\u5b9a\u4e49\u5728 square.cpp \u4e2d) \uff0c\u56e0\u4e3a\u5b83\u5305\u542b\u4e86 square.h \uff08\u5305\u542b\u8be5\u51fd\u6570\u7684\u58f0\u660e\uff09\uff0c\u94fe\u63a5\u5668\u4f1a\u628a main.cpp \u4e2d\u5bf9 getSquareSides \u51fd\u6570\u7684\u8c03\u7528\u5173\u8054\u5230 square.cpp \u4e2d getSquareSides \u51fd\u6570\u7684\u5b9a\u4e49\u3002","title":"\u5934\u6587\u4ef6\u9632\u8303\u4e0d\u80fd\u9632\u6b62\u5934\u6587\u4ef6\u88ab\u5305\u542b\u5230\u4e0d\u540c\u7684\u6587\u4ef6"},{"location":"2-12-Header-guards/#_5","text":"\u4e4b\u524d\u6211\u4eec\u8bf4\u8fc7\uff0c\u4e0d\u8981\u628a\u51fd\u6570\u7684\u5b9a\u4e49\u653e\u5728\u5934\u6587\u4ef6\u4e2d\u3002\u6240\u4ee5\u4f60\u53ef\u80fd\u4f1a\u95ee\uff0c\u90a3\u4e3a\u4ec0\u4e48\u8fd8\u8981\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\u5462\uff1f\u6bd5\u7adf\u5b83\u8981\u89e3\u51b3\u7684\u95ee\u9898\u4ece\u4e00\u5f00\u59cb\u5c31\u5e94\u8be5\u907f\u514d\u3002 \u5176\u5b9e\uff0c\u540e\u9762\u6211\u4eec\u4f1a\u5411\u4f60\u5c55\u793a\u4e00\u4e9b \u975e\u51fd\u6570 \u5b9a\u4e49\u88ab\u653e\u7f6e\u5728\u5934\u6587\u4ef6\u4e2d\u7684\u4f8b\u5b50\u3002\u4f8b\u5982\uff0cC++ \u5141\u8bb8\u4f60\u521b\u5efa\u81ea\u5b9a\u4e49\u7c7b\u578b\u3002\u8fd9\u4e9b\u81ea\u5b9a\u4e49\u7c7b\u578b\u901a\u5e38\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u624d\u80fd\u8ba9\u5bf9\u5e94\u7684\u6e90\u6587\u4ef6\u4f7f\u7528\u8fd9\u4e9b\u5b9a\u4e49\u3002\u5982\u679c\u4e0d\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\uff0c\u5219\u5b83\u4eec\u5f88\u53ef\u80fd\u4f1a\u88ab\u591a\u6b21\u62f7\u8d1d\uff0c\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\u3002 \u56e0\u6b64\uff0c\u5373\u4fbf\u6559\u7a0b\u770b\u5230\u8fd9\u91cc\u7684\u65f6\u5019\u4f60\u8fd8\u6ca1\u6709\u5fc5\u8981\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\uff0c\u80fd\u591f\u4ece\u73b0\u5728\u5c31\u517b\u6210\u4e60\u60ef\u8fd8\u662f\u5f88\u597d\u7684\u3002","title":"\u4e0d\u628a\u5b9a\u4e49\u653e\u5728\u5934\u6587\u4ef6\u4e2d\u4e0d\u5c31\u884c\u4e86\uff1f"},{"location":"2-12-Header-guards/#pragma-once","text":"\u73b0\u4ee3\u7f16\u8bd1\u5668\u652f\u6301\u4e00\u79cd\u66f4\u7b80\u5355\u7684\u5904\u7406\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528 #pragma \u6307\u4ee4\u6765\u4ee3\u66ff\u5934\u6587\u4ef6\u9632\u8303\uff1a 1 2 3 #pragma once // your code here #pragma once \u7684\u529f\u80fd\u548c\u5934\u6587\u4ef6\u9632\u8303\u662f\u4e00\u6837\u7684\uff0c\u800c\u4e14\u66f4\u52a0\u7b80\u77ed\u4e14\u4e0d\u6613\u51fa\u9519\u3002\u5728\u5927\u591a\u6570\u7684\u9879\u76ee\u4e2d\uff0c #pragma once \u90fd\u53ef\u4ee5\u5f88\u597d\u7684\u5de5\u4f5c\uff0c\u56e0\u6b64\u5f88\u591a\u5f00\u53d1\u8005\u90fd\u503e\u5411\u4e8e\u4f7f\u7528\u5b83\u800c\u4e0d\u662f\u5934\u6587\u4ef6\u9632\u8303\u3002\u4e0d\u8fc7\uff0c #pragma once \u5e76\u4e0d\u662f C++ \u8bed\u8a00\u7684\u4e00\u90e8\u5206 (\u800c\u4e14\u53ef\u80fd\u6c38\u8fdc\u90fd\u4e0d\u4f1a\u6210\u4e3a C++ \u7684\u4e00\u90e8\u5206\uff0c\u56e0\u4e3a\u6ca1\u529e\u6cd5\u4fdd\u8bc1\u5728\u4efb\u548c\u60c5\u51b5\u4e0b\u5b83\u90fd\u80fd\u6b63\u786e\u5730\u5de5\u4f5c\uff09 \u5904\u4e8e\u517c\u5bb9\u6027\u7684\u8003\u8651\uff0c\u6211\u4eec\u8fd8\u662f\u63a8\u8350\u4f7f\u7528\u4f20\u7edf\u65b9\u5f0f\u2014\u2014\u5934\u6587\u4ef6\u9632\u8303\u3002\u5934\u6587\u4ef6\u9632\u8303\u7528\u8d77\u6765\u4e5f\u4e0d\u590d\u6742\u800c\u4e14\u6240\u6709\u7684\u7f16\u8bd1\u5668\u90fd\u652f\u6301\u3002 \u6700\u4f73\u5b9e\u8df5 \u5904\u4e8e\u517c\u5bb9\u6027\u7684\u8003\u8651\uff0c\u4f18\u5148\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\u800c\u975e #pragma once \u3002","title":"#pragma once"},{"location":"2-12-Header-guards/#_6","text":"\u5934\u6587\u4ef6\u9632\u8303\u53ef\u4ee5\u786e\u4fdd\u8be5\u5934\u6587\u4ef6\u7684\u5185\u5bb9\u88ab\u5305\u542b\u5230\u67d0\u4e2a\u6587\u4ef6\u65f6\uff0c\u5176\u5185\u5bb9\u53ea\u88ab\u62f7\u8d1d\u4e00\u6b21\uff0c\u4ee5\u907f\u514d\u91cd\u590d\u5b9a\u4e49\u7684\u95ee\u9898\u3002 \u6ce8\u610f\uff0c\u91cd\u590d\u58f0\u660e\u662f\u53ef\u4ee5\u7684\uff0c\u56e0\u4e3a\u591a\u6b21\u58f0\u660e\u5e76\u4e0d\u4f1a\u5e26\u6765\u4efb\u4f55\u95ee\u9898\u3002\u4e0d\u8fc7\uff0c\u5373\u4f7f\u4f60\u7684\u5934\u6587\u4ef6\u4e2d\u53ea\u5305\u542b\u58f0\u660e\uff0c\u4f7f\u7528\u5934\u6587\u4ef6\u9632\u8303\u4ecd\u7136\u662f\u6700\u4f73\u5b9e\u8df5\u3002 \u6ce8\u610f\uff0c\u5934\u6587\u4ef6\u9632\u8303\u4e0d\u80fd\u907f\u514d\u5934\u6587\u4ef6\u7684\u5185\u5bb9\u88ab\u62f7\u8d1d\u5230\u591a\u4e2a\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\u3002\u4e0d\u8fc7\uff0c\u8fd9\u662f\u597d\u4e8b\uff0c\u56e0\u4e3a\u6211\u4eec\u65f6\u5e38\u9700\u8981\u5c06\u5934\u6587\u4ef6\u5305\u542b\u5230\u591a\u4e2a\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\u3002","title":"\u5c0f\u7ed3"},{"location":"2-13-How-to-design-your-first-programs/","tags":["desgin"],"text":"Key Takeaway \u5f00\u59cb\u65f6\u4e00\u5207\u4ece\u7b80 \u9010\u6e10\u6dfb\u52a0\u65b0\u7684\u529f\u80fd \u4e00\u6b21\u53ea\u505a\u4e00\u4ef6\u4e8b \u4e00\u8fb9\u5b9e\u73b0\u3001\u4e00\u8fb9\u6d4b\u8bd5 \u4e0d\u8981\u8fc7\u65e9\u5730\u4f18\u5316\u4ee3\u7801 \u5728\u5b66\u4e60\u8fc7\u57fa\u672c\u7684\u7f16\u7a0b\u77e5\u8bc6\u540e\uff0c\u8ba9\u6211\u4eec\u6765\u770b\u770b\u5982\u4f55\u8bbe\u8ba1\u7b2c\u4e00\u4e2a\u7a0b\u5e8f\u3002 \u5f53\u4f60\u5750\u4e0b\u6765\u5f00\u59cb\u7f16\u7a0b\u7684\u65f6\u5019\uff0c\u901a\u5e38\u4f60\u53ea\u6709\u4e00\u4e9b\u5982\u4f55\u7f16\u5199\u8be5\u7a0b\u5e8f\u7684\u57fa\u672c\u7684\u60f3\u6cd5\uff0c\u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u5bf9\u5982\u4f55\u5c06\u60f3\u6cd5\u8f6c\u53d8\u4e3a\u4ee3\u7801\u611f\u5230\u4e0d\u77e5\u6240\u63aa\u3002\u5b9e\u9645\u4e0a\uff0c\u5b8c\u6210\u8fd9\u4ef6\u4e8b\u6240\u9700\u8981\u7684\u6280\u80fd\u4e0e\u4f60\u5728\u751f\u6d3b\u4e2d\u5b66\u5230\u7684\u89e3\u51b3\u95ee\u9898\u7684\u6280\u80fd\u5e76\u65e0\u5dee\u522b\u3002 \u6700\u9700\u8981\u65b0\u624b\u7a0b\u5e8f\u5458\u4eec\u8bb0\u4f4f\u7684\u662f\uff08\u4e5f\u662f\u6700\u96be\u505a\u5230\u7684\u662f\uff09\u5728\u5f00\u59cb \u7f16\u5199 \u4ee3\u7801\u524d\uff0c\u4e00\u5b9a\u8981\u5148 \u8bbe\u8ba1 \u4f60\u7684\u7a0b\u5e8f\u3002\u7f16\u7a0b\u548c\u76d6\u623f\u5b50\u5728\u5f88\u591a\u65b9\u9762\u90fd\u5f88\u76f8\u50cf\u3002\u60f3\u8c61\u4e00\u4e0b\uff0c\u6211\u4eec\u4f1a\u5728\u6ca1\u6709\u56fe\u7eb8\u7684\u60c5\u51b5\u4e0b\u5c31\u5f00\u59cb\u76d6\u623f\u5b50\u5417\uff1f\u5f88\u53ef\u80fd\u4e0d\u4f1a\uff0c\u9664\u975e\u4f60\u662f\u4e2a\u5929\u624d\uff0c\u4e0d\u7136\u4f60\u76d6\u51fa\u6765\u7684\u623f\u5b50\u4e00\u5b9a\u95ee\u9898\u4e0d\u5c11\uff1a\u5899\u662f\u659c\u7684\u3001\u9876\u662f\u6f0f\u7684\u7b49\u7b49\u3002\u540c\u6837\u7684\uff0c\u5982\u679c\u6beb\u65e0\u8ba1\u5212\u5c31\u5f00\u59cb\u7f16\u7a0b\uff0c\u5199\u51fa\u6765\u7684\u4ee3\u7801\u4e5f\u4e00\u5b9a\u662f\u95ee\u9898\u767e\u51fa\uff0c\u7136\u540e\u4f60\u5c31\u9700\u8981\u82b1\u8d39\u5927\u91cf\u7684\u65f6\u95f4\u53bb\u4fee\u590d\u90a3\u4e9b\u4f60\u672c\u6765\u5c31\u53ef\u4ee5\u907f\u514d\u7684\u95ee\u9898\u3002 \u5728\u5f00\u59cb\u524d\u7a0d\u52a0\u8ba1\u5212\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u8282\u7ea6\u65f6\u95f4\uff0c\u8fd8\u80fd\u907f\u514d\u5f88\u591a\u8ba9\u4eba\u611f\u5230\u6cae\u4e27\u7684\u65f6\u523b\u3002 \u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u5411\u4f60\u4ecb\u7ecd\u4e00\u79cd\u5c06\u60f3\u6cd5\u8f6c\u53d8\u4e3a\u7b80\u5355\u7684\u3001\u53ef\u7528\u7684\u7a0b\u5e8f\u7684\u57fa\u672c\u65b9\u6cd5\u3002 \u8bbe\u8ba1\u6b65\u9aa41\uff1a\u5b9a\u76ee\u6807 \u4e3a\u4e86\u7f16\u5199\u4e00\u4e2a\u6210\u529f\u7684\u7a0b\u5e8f\uff0c\u4f60\u5fc5\u987b\u8981\u9996\u5148\u660e\u786e\u4f60\u7684\u76ee\u6807\u662f\u4ec0\u4e48\u3002\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u8be5\u76ee\u6807\u5e94\u8be5\u7528\u4e00\u4e24\u53e5\u8bdd\u5c31\u53ef\u4ee5\u63cf\u8ff0\u3002\u901a\u5e38\u6765\u8bb2\uff0c\u4ece\u7528\u6237\u7684\u89d2\u5ea6\u6765\u63cf\u8ff0\u7f16\u7a0b\u7684\u7ed3\u679c\u662f\u5f88\u6709\u7528\u7684\uff0c\u4f8b\u5982\uff1a \u5141\u8bb8\u7528\u6237\u53ef\u4ee5\u7ba1\u7406\u4e00\u4e9b\u5217\u7684\u59d3\u540d\u5e76\u5c06\u5176\u5173\u8054\u5230\u7535\u8bdd\u53f7\u7801\uff1b \u968f\u673a\u751f\u6210\u770b\u4e0a\u53bb\u5f88\u9177\u7684\u6d1e\u7a74\u573a\u666f\u4ee5\u4f5c\u4e3a\u6e38\u620f\u4e2d\u7684\u5730\u7262\u6765\u4f7f\u7528\uff1b \u751f\u6210\u4e00\u4e2a\u9ad8\u80a1\u606f\u80a1\u7968\u7684\u63a8\u8350\u5217\u8868\uff1b \u5bf9\u4e00\u4e2a\u7403\u4ece\u9ad8\u5854\u6389\u843d\u5230\u5730\u9762\u6240\u9700\u7684\u65f6\u95f4\u8fdb\u884c\u5efa\u6a21\u3002 \u5c3d\u7ba1\u8fd9\u4e00\u6b65\u6240\u505a\u7684\u4e8b\u60c5\u770b\u4e0a\u53bb\u90fd\u662f\u5f88\u663e\u7136\u7684\uff0c\u5b83\u7684\u4f5c\u7528\u5374\u4e0d\u5bb9\u5ffd\u89c6\u3002\u5bf9\u4e8e\u7f16\u7a0b\u6765\u8bf4\uff0c\u6700\u7cdf\u7cd5\u7684\u83ab\u8fc7\u4e8e\u4f60\u5199\u5b8c\u7684\u7a0b\u5e8f\u5374\u4e0d\u662f\u4f60\u60f3\u8981\u7684\uff08\u6216\u8005\u4f60\u8001\u677f\u60f3\u8981\u7684\uff09\uff01 \u8bbe\u8ba1\u6b65\u9aa42\uff1a\u5b9a\u9700\u6c42 \u5c3d\u7ba1\uff0c\u5b9a\u4e49\u95ee\u9898\u6709\u52a9\u4e8e\u4f60\u786e\u5b9a\u4f60\u671f\u671b\u7684\u7ed3\u679c\u662f\u58f0\u660e\uff0c\u4f46\u76ee\u6807\u4ecd\u7136\u662f\u4e0d\u6e05\u695a\u7684\u3002\u63a5\u4e0b\u6765\u4e00\u6b65\u5c31\u662f\u8003\u8651\u5982\u4f55\u8bbe\u8ba1\u9700\u6c42\u3002 \u9700\u6c42\u4e00\u65b9\u9762\u6307\u7684\u662f\u4f60\u80fd\u591f\u4f7f\u7528\u7684\u6709\u9650\u8d44\u6e90\uff08\u4f8b\u5982\uff0c\u9884\u7b97\u3001\u5de5\u671f\u3001\u7a7a\u95f4\u548c\u5185\u5b58\u7b49\u7b49\uff09\uff0c\u53e6\u4e00\u65b9\u9762\u6307\u7684\u662f\u4f60\u7684\u7a0b\u5e8f\u5fc5\u987b\u5177\u5907\u7684\u80fd\u591f\u6ee1\u8db3\u7528\u6237\u9700\u8981\u7684\u80fd\u529b\u3002\u6ce8\u610f\uff0c\u9700\u6c42\u5173\u6ce8\u7684\u4ecd\u7136\u662f \u4ec0\u4e48 \uff0c\u800c\u4e0d\u662f \u5982\u4f55 \u3002 \u4f8b\u5982\uff1a \u7535\u8bdd\u53f7\u7801\u5e94\u8be5\u80fd\u591f\u88ab\u50a8\u5b58\uff0c\u8fd9\u6837\u4ee5\u540e\u53ef\u4ee5\u7528\u6765\u8fdb\u884c\u56de\u62e8\uff1b \u968f\u673a\u7684\u5730\u7262\u5e94\u8be5\u603b\u662f\u5305\u542b\u4e00\u6761\u4ece\u5165\u53e3\u5230\u51fa\u53e3\u7684\u8def\uff1b \u80a1\u7968\u63a8\u8350\u5e94\u8be5\u53c2\u8003\u5386\u53f2\u4ef7\u683c\u4fe1\u606f\uff1b \u7528\u6237\u5e94\u8be5\u80fd\u591f\u8f93\u5165\u9ad8\u5854\u7684\u9ad8\u5ea6\uff1b \u6211\u4eec\u9700\u8981\u5728\u4e03\u5929\u5185\u5b8c\u6210\u53ef\u4f9b\u6d4b\u8bd5\u7684\u7248\u672c\uff1b \u7a0b\u5e8f\u9700\u8981\u572810\u79d2\u5185\u5b8c\u6210\u7528\u6237\u7684\u8bf7\u6c42\uff1b \u7a0b\u5e8f\u5728\u7528\u6237\u4f7f\u7528\u65f6\u5d29\u6e83\u9000\u51fa\u7684\u6982\u7387\u5e94\u8be5\u5c0f\u4e8e 0.1%\u3002 \u4e00\u4e2a\u95ee\u9898\u53ef\u80fd\u4f1a\u6d3e\u751f\u51fa\u5f88\u591a\u9700\u6c42\uff0c\u53ea\u6709\u6240\u6709\u7684\u9700\u6c42\u90fd\u88ab\u6ee1\u8db3\uff0c\u624d\u80fd\u8bf4\u89e3\u51b3\u65b9\u6848\u88ab\u5b9e\u73b0\u4e86\u3002 \u8bbe\u8ba1\u6b65\u9aa43\uff1a\u5b9a\u4e49\u5de5\u5177\u3001\u76ee\u6807\u4ea7\u7269\u4ee5\u53ca\u5907\u4efd\u8ba1\u5212 \u5982\u679c\u4f60\u5df2\u7ecf\u662f\u4e00\u4e2a\u6709\u7ecf\u9a8c\u7684\u7a0b\u5e8f\u5458\u4e86\uff0c\u5728\u8bbe\u8ba1\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u53ef\u80fd\u8fd8\u9700\u8981\u8003\u8651\u5176\u4ed6\u7684\u4e00\u4e9b\u95ee\u9898\uff0c\u4f8b\u5982\uff1a \u5b9a\u4e49\u7a0b\u5e8f\u9700\u8981\u8fd0\u884c\u7684\u5e73\u53f0\u7684\u4f53\u7cfb\u7ed3\u6784\u548c\uff08\u6216\uff09\u64cd\u4f5c\u7cfb\u7edf\uff1b \u786e\u5b9a\u4f60\u9700\u8981\u4f7f\u7528\u54ea\u4e9b\u5de5\u5177\uff1b \u786e\u5b9a\u4f60\u662f\u72ec\u7acb\u5b8c\u6210\u4ee3\u7801\u7f16\u5199\u8fd8\u662f\u4ee5\u56e2\u961f\u5408\u4f5c\u7684\u65b9\u5f0f\u5b8c\u6210\uff1b \u5b9a\u4e49\u6d4b\u8bd5\u3001\u53cd\u9988\u548c\u91ca\u653e\u7b56\u7565\uff1b \u5b9a\u4e49\u5982\u4f55\u5907\u4efd\u4ee3\u7801\u3002 \u4e0d\u8fc7\uff0c\u4f5c\u4e3a\u65b0\u624b\u7a0b\u5e8f\u5458\uff0c\u4e0a\u9762\u51e0\u4e2a\u95ee\u9898\u5176\u5b9e\u90fd\u5f88\u7b80\u5355\uff1a\u4f60\u5c06\u4f1a\u72ec\u7acb\u5730\u5728\u4f60\u81ea\u5df1\u7684\u7535\u8111\u4e0a\u7f16\u5199\u4ee3\u7801\uff0c\u6240\u9700\u7684\u5de5\u5177\u5c31\u662f\u4f60\u4e0b\u8f7d\u5e76\u5b89\u88c5\u4e86\u7684IDE\uff0c\u4f60\u7f16\u5199\u7684\u7a0b\u5e8f\u9664\u4e86\u4f60\u81ea\u5df1\u6ca1\u6709\u5176\u4ed6\u4eba\u4f1a\u4f7f\u7528\u3002 \u5373\u4fbf\u5982\u6b64\uff0c\u5982\u679c\u4f60\u7f16\u5199\u7684\u4ee3\u7801\u4e0d\u662f\u5be5\u5be5\u51e0\u884c\uff0c\u4f60\u8fd8\u662f\u5e94\u8be5\u8003\u8651\u4e00\u4e0b\u5982\u4f55\u5907\u4efd\u4f60\u7684\u4ee3\u7801\u3002\u5c06\u4ee3\u7801\u62f7\u8d1d\u6216\u8005\u538b\u7f29\u540e\u653e\u5230\u7535\u8111\u7684\u5176\u4ed6\u76ee\u5f55\u4e2d\u662f\u4e0d\u591f\u7684\uff08\u5c3d\u7ba1\u804a\u80dc\u4e8e\u65e0\uff09\u3002\u5982\u679c\u4f60\u7684\u7535\u8111\u574f\u4e86\uff0c\u4f60\u7f16\u5199\u7684\u4ee3\u7801\u4e5f\u5c31\u4ed8\u4e4b\u4e1c\u6d41\u4e86\u3002\u4e00\u4e2a\u597d\u7684\u5907\u4efd\u7b56\u7565\uff0c\u5e94\u8be5\u662f\u628a\u4ee3\u7801\u5168\u90e8\u62f7\u8d1d\u4e00\u4efd\u540e\u62f7\u5230\u5176\u4ed6\u7535\u8111\u6216\u5176\u4ed6\u5730\u65b9\u3002\u6709\u5f88\u591a\u529e\u6cd5\u53ef\u4ee5\u5b9e\u73b0\uff0c\u4f8b\u5982\u5c06\u5176\u538b\u7f29\u4e3aZip\u6587\u4ef6\u540e\u53d1\u9001\u5230\u4f60\u81ea\u5df1\u7684\u90ae\u7bb1\uff0c\u62f7\u8d1d\u5230Dropbox\u6216\u8005\u7f51\u76d8\uff0c\u6216\u8005\u901a\u8fc7FTP\u4f20\u5230\u5176\u4ed6\u7535\u8111\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5c40\u57df\u7f51\u62f7\u5230\u5176\u4ed6\u7535\u8111\u4e0a\uff0c\u6216\u8005\u4f7f\u7528\u4f4d\u4e8e\u5176\u4ed6\u7535\u8111\u6216\u4e91\u7aef\u7684\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff08\u4f8b\u5982\uff1aGithub\uff09\u3002\u7248\u672c\u63a7\u5236\u7cfb\u7edf\u9664\u4e86\u5b58\u50a8\u6587\u4ef6\u4ee5\u5916\uff0c\u8fd8\u6709\u5f88\u591a\u5176\u4ed6\u6709\u7528\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u5b83\u53ef\u4ee5\u5c06\u4f60\u7684\u4ee3\u7801\u56de\u9000\u5230\u4e4b\u524d\u7684\u7248\u672c\u3002 \u8bbe\u8ba1\u6b65\u9aa44\uff1a\u5c06\u590d\u6742\u95ee\u9898\u5207\u5206\u4e3a\u591a\u4e2a\u7b80\u5355\u95ee\u9898 \u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\uff0c\u6211\u4eec\u65f6\u5e38\u9700\u8981\u5b8c\u6210\u4e00\u4e9b\u975e\u5e38\u590d\u6742\u7684\u95ee\u9898\u3002\u627e\u5230\u7684\u95ee\u9898\u7684\u89e3\u51b3\u529e\u6cd5\uff0c\u901a\u5e38\u5e76\u4e0d\u5bb9\u6613\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528 \u81ea\u9876\u5411\u4e0b(top down) \u7684\u529e\u6cd5\u6765\u89e3\u51b3\u95ee\u9898\u3002\u4e3a\u4e86\u89e3\u51b3\u4e00\u4e2a\u7279\u522b\u590d\u6742\u7684\u95ee\u9898\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u628a\u8fd9\u4e2a\u95ee\u9898\u5206\u89e3\u4e3a\u591a\u4e2a\u76f8\u5bf9\u6765\u8bf4\u66f4\u5bb9\u6613\u89e3\u51b3\u7684\u5b50\u95ee\u9898\u3002\u5982\u679c\u5b50\u95ee\u9898\u4ecd\u7136\u96be\u4ee5\u89e3\u51b3\uff0c\u5b83\u4eec\u8fd8\u53ef\u4ee5\u88ab\u8fdb\u4e00\u6b65\u5206\u5272\u4e3a\u66f4\u591a\u7684\u5b50\u95ee\u9898\u3002\u901a\u8fc7\u4e0d\u65ad\u5730\u5c06\u590d\u6742\u95ee\u9898\u5206\u89e3\u6210\u7b80\u5355\u7684\u5b50\u95ee\u9898\uff0c\u4f60\u6700\u7ec8\u53ef\u4ee5\u5f97\u5230\u5f88\u591a\u66f4\u5bb9\u6613\u88ab\u5904\u7406\u7684\u5b50\u95ee\u9898\u3002 \u4e3e\u4f8b\u6765\u8bf4\uff0c\u5982\u679c\u6211\u4eec\u9700\u8981\u6253\u626b\u5c4b\u5b50\uff0c\u90a3\u4e48\u5206\u89e3\u4efb\u52a1\u7684\u7ed3\u6784\u53ef\u80fd\u4f1a\u662f\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a \u6253\u626b\u5c4b\u5b50 \u6253\u626b\u5c4b\u5b50\u662f\u4e00\u4e2a\u5f88\u5927\u7684\u4efb\u52a1\uff0c\u8ba9\u6211\u4eec\u5c06\u5176\u5206\u89e3\u4e3a\u591a\u4e2a\u5b50\u4efb\u52a1\uff1a \u6253\u626b\u5c4b\u5b50 \u6e05\u7406\u5730\u6bef \u6253\u626b\u536b\u751f\u95f4 \u6253\u626b\u53a8\u623f \u6bd4\u4e4b\u524d\u5bb9\u6613\u5904\u7406\u4e86\u4e00\u4e9b\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u9010\u9879\u89e3\u51b3\u8fd9\u4e9b\u5b50\u95ee\u9898\u4e86\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u5176\u5b9e\u8fd8\u80fd\u5c06\u8fd9\u4e9b\u4efb\u52a1\u7ee7\u7eed\u5206\u89e3\uff1a \u6253\u626b\u5c4b\u5b50 \u6e05\u7406\u5730\u6bef \u6253\u626b\u536b\u751f\u95f4 \u6e05\u6d17\u9a6c\u6876 \u6e05\u7406\u6c34\u6c60 \u6253\u626b\u53a8\u623f \u6e05\u7406\u7076\u53f0 \u6253\u626b\u7076\u53f0 \u6e05\u6d17\u6c34\u6c60 \u5012\u5783\u573e \u73b0\u5728\uff0c\u6211\u4eec\u9762\u5bf9\u7684\u662f\u4e00\u7cfb\u5217\u5177\u6709\u5c42\u6b21\u7ed3\u6784\u7684\u95ee\u9898\u4e86\uff0c\u800c\u4e14\u5b83\u4eec\u4e2d\u4efb\u4f55\u4e00\u9879\u90fd\u4e0d\u590d\u6742\u3002\u901a\u8fc7\u5b8c\u6210\u8fd9\u4e9b\u76f8\u5bf9\u66f4\u5bb9\u6613\u5904\u7406\u7684\u5b50\u4efb\u52a1\u4e4b\u540e\uff0c\u6211\u4eec\u4e5f\u5c31\u5b8c\u6210\u4e86\u6253\u626b\u5c4b\u5b50\u8fd9\u4e2a\u66f4\u590d\u6742\u7684\u95ee\u9898\u3002 \u53e6\u5916\u4e00\u79cd\u5206\u89e3\u4efb\u52a1\u7684\u65b9\u5f0f\u79f0\u4e3a \u81ea\u5e95\u5411\u4e0a(bottom) \u65b9\u6cd5\u3002\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u65f6\uff0c\u6211\u4eec\u4f1a\u4ece\u4e00\u7cfb\u5217\u7b80\u5355\u7684\u4efb\u52a1\u5f00\u59cb\uff0c\u7136\u540e\u901a\u8fc7\u5408\u5e76\u5206\u7ec4\u7684\u65b9\u5f0f\u6784\u5efa\u4efb\u52a1\u5217\u8868\u3002 \u5927\u591a\u6570\u4eba\u5e73\u65f6\u4e0d\u662f\u4e0a\u5b66\u5c31\u662f\u4e0a\u73ed\uff0c\u90a3\u4e48\u5c31\u4ee5\u6b64\u6765\u4e3e\u4f8b\uff0c\u5047\u8bbe\u6211\u4eec\u8981\u89e3\u51b3\u7684\u662f\u201c\u53bb\u4e0a\u73ed\u201d\u8fd9\u4e2a\u95ee\u9898\uff0c\u5982\u679c\u6709\u4eba\u95ee\u4f60\uff0c\u4f60\u65e9\u4e0a\u51fa\u95e8\u524d\u90fd\u505a\u54ea\u4e9b\u4e8b\u5462\uff1f\u4f60\u7684\u56de\u7b54\u53ef\u80fd\u5305\u542b\u4e0b\u9762\u8fd9\u4e9b\u4efb\u52a1\uff1a \u62ff\u51fa\u8981\u7a7f\u7684\u8863\u670d \u7a7f\u8863 \u5403\u65e9\u996d \u5f00\u8f66\u53bb\u516c\u53f8 \u5237\u7259 \u8d77\u5e8a \u505a\u65e9\u9910 \u4e0a\u8f66 \u6d17\u6fa1 \u4f7f\u7528\u81ea\u5e95\u5411\u4e0a\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u5b83\u4eec\u7684\u76f8\u4f3c\u6027\uff0c\u4e3a\u8fd9\u4e9b\u4efb\u52a1\u521b\u5efa\u4e00\u4e2a\u5c42\u6b21\u7ed3\u6784\uff1a \u4ece\u8d77\u5e8a\u5230\u4e0a\u73ed\u8981\u505a\u7684\u4e8b \u5367\u5ba4\u8981\u505a\u7684\u4e8b \u8d77\u5e8a \u62ff\u8863\u670d \u7a7f\u8863 \u536b\u751f\u95f4\u8981\u505a\u7684\u4e8b \u6d17\u6fa1 \u5237\u7259 \u65e9\u9910\u76f8\u5173 \u51c6\u5907\u9ea6\u7247 \u5403\u9ea6\u7247 \u901a\u52e4\u76f8\u5173 \u4e0a\u8f66 \u5f00\u8f66\u5230\u516c\u53f8 \u5176\u5b9e\u6211\u4eec\u4e0d\u96be\u770b\u51fa\uff0c\u8fd9\u4e9b\u5c42\u6b21\u7ed3\u6784\u5bf9\u4e8e\u7f16\u7a0b\u6765\u8bf4\u662f\u975e\u5e38\u6709\u7528\u7684\uff0c\u56e0\u4e3a\u4e00\u65e6\u4efb\u52a1\u7684\u7ed3\u6784\u6e05\u695a\u4e86\uff0c\u7a0b\u5e8f\u7684\u5927\u4f53\u7ed3\u6784\u5176\u5b9e\u4e5f\u5df2\u7ecf\u6e05\u695a\u4e86\u3002\u6700\u4e0a\u5c42\u7684\u4efb\u52a1\uff08\u6253\u626b\u5c4b\u5b50\u6216\u53bb\u4e0a\u73ed\uff09\u5c31\u53ef\u4ee5\u662f\u7a0b\u5e8f\u7684 main() \u51fd\u6570\uff08\u56e0\u4e3a\u5b83\u662f\u4f60\u8981\u89e3\u51b3\u7684\u4e3b\u8981(main)\u95ee\u9898\uff09\u3002\u800c\u5176\u4ed6\u5b50\u9879\u76ee\u5219\u53ef\u4ee5\u5b9e\u73b0\u4e3a\u4e0d\u540c\u7684\u51fd\u6570\u3002 \u5982\u679c\u5176\u4e2d\u67d0\u4e00\u9879\u4efb\u52a1\uff08\u51fd\u6570\uff09\u5728\u5b9e\u73b0\u65f6\u4ecd\u7136\u8fc7\u4e8e\u56f0\u96be\uff0c\u5219\u53ef\u4ee5\u7ee7\u7eed\u5c06\u5176\u5206\u89e3\u4e3a\u51e0\u4e2a\u5b50\u95ee\u9898\u3002\u6700\u7ec8\u4f60\u5c31\u53ef\u4ee5\u5c06\u7a0b\u5e8f\u5206\u5272\u4e3a\u591a\u4e2a\u5bb9\u6613\u5b9e\u73b0\u7684\u51fd\u6570\u3002 \u8bbe\u8ba1\u6b65\u9aa45\uff1a\u786e\u5b9a\u76f8\u5173\u4e8b\u4ef6\u53d1\u751f\u7684\u987a\u5e8f \u73b0\u5728\uff0c\u7a0b\u5e8f\u7684\u7ed3\u6784\u5df2\u7ecf\u6e05\u695a\u4e86\uff0c\u63a5\u4e0b\u6765\u9700\u8981\u5c06\u8fd9\u4e9b\u4efb\u52a1\u5173\u8054\u5176\u4ed6\u3002\u9996\u5148\uff0c\u6211\u4eec\u8981\u786e\u5b9a\u8fd9\u4e9b\u8981\u4efb\u52a1\u7684\u53d1\u751f\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u4f60\u65e9\u4e0a\u8d77\u6765\uff0c\u5b8c\u6210\u4e0a\u9762\u90a3\u4e9b\u4efb\u52a1\u7684\u987a\u5e8f\u662f\u4ec0\u4e48\uff1f\u5dee\u4e0d\u591a\u5e94\u8be5\u662f\u8fd9\u6837\u7684\u5427\uff1a \u5367\u5ba4\u8981\u505a\u7684\u4e8b \u536b\u751f\u95f4\u8981\u505a\u7684\u4e8b \u65e9\u9910\u76f8\u5173 \u901a\u52e4\u76f8\u5173 \u5982\u679c\u4f60\u8981\u7f16\u5199\u4e00\u4e2a\u8ba1\u7b97\u5668\u7a0b\u5e8f\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u6309\u7167\u4e0b\u9762\u6b65\u9aa4\u6765\u505a\uff1a \u4ece\u7528\u6237\u83b7\u53d6\u7b2c\u4e00\u4e2a\u6570 \u4ece\u7528\u6237\u83b7\u53d6\u9700\u8981\u6267\u884c\u7684\u8fd0\u7b97\u64cd\u4f5c \u4ece\u7528\u6237\u83b7\u53d6\u7b2c\u4e8c\u4e2a\u6570 \u8ba1\u7b97\u7ed3\u679c \u6253\u5370\u7ed3\u679c \u5b8c\u6210\u4e0a\u8ff0\u8bbe\u8ba1\u5de5\u4f5c\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f00\u59cb\u5b9e\u73b0\u4ee3\u7801\u4e86\uff01 \u5b9e\u73b0\u6b65\u9aa41\uff1a\u7f16\u5199main\u51fd\u6570\u6846\u67b6 \u73b0\u5728\u53ef\u4ee5\u5f00\u59cb\u5b9e\u73b0\u4ee3\u7801\u4e86\u3002\u6839\u636e\u4e0a\u9762\u987a\u5e8f\u7ed3\u6784\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u5b8c\u6210 main \u51fd\u6570\u7684\u6846\u67b6\uff0c\u6682\u65f6\u65e0\u9700\u62c5\u5fc3\u8f93\u5165\u8f93\u51fa\u7684\u95ee\u9898\u3002 1 2 3 4 5 6 7 8 9 int main () { // doBedroomThings(); // doBathroomThings(); // doBreakfastThings(); // doTransportationThings(); return 0 ; } \u6216\u8005\u5bf9\u4e8e\u8ba1\u7b97\u5668\u4f8b\u5b50\u6765\u8bf4\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 int main () { // Get first number from user // getUserInput(); // Get mathematical operation from user // getMathematicalOperation(); // Get second number from user // getUserInput(); // Calculate result // calculateResult(); // Print result // printResult(); return 0 ; } \u6ce8\u610f\uff0c\u5982\u679c\u4f60\u4e5f\u4f7f\u7528\u8fd9\u79cd\u201c\u5927\u7eb2\u6846\u67b6\u201d\u7684\u65b9\u6cd5\u6765\u6784\u5efa\u7a0b\u5e8f\uff0c\u8fd9\u91cc\u6d89\u53ca\u7684\u51fd\u6570\u90fd\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\uff0c\u6bd5\u7adf\u5b83\u4eec\u8fd8\u6ca1\u6709\u88ab\u5b9a\u4e49\u3002\u5728\u51fd\u6570\u88ab\u5b9e\u9645\u5b9a\u4e49\u524d\u5148\u5c06\u5176\u6682\u65f6\u5148\u6ce8\u91ca\u6389\u662f\u4e00\u79cd\u65b9\u6cd5\uff08\u4e0a\u9762\u5c55\u793a\u7684\u5c31\u662f\u8fd9\u79cd\u65b9\u6cd5\uff09\u3002\u6b64\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u5bf9\u51fd\u6570\u8fdb\u884c \u6253\u6869(stub) \uff0c\u5373\u4e3a\u7a0b\u5e8f\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u51fd\u6570\u4f53\uff0c\u4ee5\u4fbf\u7a0b\u5e8f\u53ef\u4ee5\u7f16\u8bd1\u901a\u8fc7\u3002 \u5b9e\u73b0\u6b65\u9aa42\uff1a\u5b9e\u73b0\u6bcf\u4e2a\u51fd\u6570 \u5728\u8fd9\u4e00\u6b65\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u51fd\u6570\u90fd\u9700\u8981\u505a\u4e09\u4ef6\u4e8b\uff1a \u5b9a\u4e49\u51fd\u6570\u7684\u539f\u578b\uff08\u8f93\u5165\u8f93\u51fa\uff09\uff1b \u7f16\u5199\u51fd\u6570\uff1b \u6d4b\u8bd5\u51fd\u6570\u3002 \u5982\u679c\u4f60\u7684\u51fd\u6570\u7c92\u5ea6\u8db3\u591f\u7ec6\uff0c\u90a3\u4e48\u6bcf\u4e2a\u51fd\u6570\u90fd\u5e94\u8be5\u662f\u975e\u5e38\u7b80\u5355\u548c\u76f4\u63a5\u7684\u3002\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u5b9e\u73b0\u8d77\u6765\u975e\u5e38\u590d\u6742\uff0c\u90a3\u4e48\u5f88\u53ef\u80fd\u5b83\u8fd8\u9700\u8981\u88ab\u8fdb\u4e00\u6b65\u5206\u5272\u4e3a\u591a\u4e2a\u66f4\u5bb9\u6613\u5b9e\u73b0\u7684\u5b50\u51fd\u6570\uff08 \u4e5f\u53ef\u80fd\u662f\u4f60\u7684\u4efb\u52a1\u987a\u5e8f\u6709\u95ee\u9898\uff0c\u4f60\u5e94\u8be5\u91cd\u65b0\u5ba1\u89c6\u4e00\u4e0b\u5404\u4e2a\u6b65\u9aa4\u7684\u987a\u5e8f \uff09\u3002 \u63a5\u4e0b\u6765\uff0c\u5b9e\u73b0\u8ba1\u7b97\u5668\u7a0b\u5e8f\u4e2d\u7684\u7b2c\u4e00\u4e2a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include // Full implementation of the getUserInput function int getUserInput () { std :: cout << \"Enter an integer \" ; int input {}; std :: cin >> input ; return input ; } int main () { // \u4ece\u7528\u6237\u83b7\u53d6\u7b2c\u4e00\u4e2a\u8f93\u5165 int value { getUserInput () }; // \u6ce8\u610f\uff0c\u8fd9\u91cc\u7684\u4ee3\u7801\u672c\u8eab\u5c31\u5177\u5907\u6d4b\u8bd5\u51fd\u6570\u8f93\u51fa\u7684\u529f\u80fd std :: cout << value ; // \u7528\u4e8e\u786e\u4fdd getUserInput() \u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u7684\u8c03\u8bd5\u4ee3\u7801\uff0c\u7a0d\u540e\u9700\u8981\u79fb\u9664 // Get mathematical operation from user // getMathematicalOperation(); // Get second number from user // getUserInput(); // Calculate result // calculateResult(); // Print result // printResult(); return 0 ; } \u9996\u5148\uff0c\u786e\u5b9a\u51fd\u6570 getUserInput \u4e0d\u9700\u8981\u63a5\u53d7\u4efb\u4f55 \u5b9e\u53c2 \u3002\u540c\u65f6\uff0c\u8be5\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u53cd\u6620\u5230\u51fd\u6570\u539f\u578b\u4e0a\uff0c\u5373\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a int \uff0c\u540c\u65f6\u6ca1\u6709 \u5f62\u53c2 \u3002\u63a5\u4e0b\u6765\uff0c\u7f16\u5199\u51fd\u6570\u4f53\u3002\u51fd\u6570\u4f53\u975e\u5e38\u76f4\u63a5\uff0c\u53ea\u5305\u542b\u56db\u6761\u8bed\u53e5 \u8bed\u53e5 \u3002\u6700\u540e\uff0c\u5728 main \u51fd\u6570\u4e2d\u7f16\u5199\u4e00\u4e9b\u7528\u4e8e\u6d4b\u8bd5\u51fd\u6570 getUserInput \u529f\u80fd\uff08\u5305\u62ec\u5176\u8fd4\u56de\u503c) \u7684\u4e34\u65f6\u4ee3\u7801\uff0c\u786e\u5b9a\u51fd\u6570\u662f\u5426\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u3002 \u591a\u6b21\u8fd0\u884c\u7a0b\u5e8f\u5e76\u4f7f\u7528\u4e0d\u540c\u7684\u8f93\u5165\u503c\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7ed3\u679c\u90fd\u7b26\u5408\u9884\u671f\u3002\u5982\u679c\u53d1\u73b0\u7a0b\u5e8f\u4e0d\u80fd\u6b63\u786e\u5de5\u4f5c\uff0c\u5219\u95ee\u9898\u53ef\u80fd\u5c31\u51fa\u5728\u521a\u624d\u7f16\u5199\u7684\u4ee3\u7801\u4e2d\u3002 \u4e00\u65e6\u4f60\u786e\u5b9a\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\uff0c\u90a3\u4e48\u8fd9\u4e9b\u4e34\u65f6\u6d4b\u8bd5\u4ee3\u7801\u5c31\u53ef\u4ee5\u88ab\u79fb\u9664\u4e86\uff0c\u7136\u540e\u5f00\u59cb\u5b9e\u73b0\u4e0b\u4e00\u4e2a\u51fd\u6570\uff08 getMathematicalOperation \uff09\u3002\u6211\u4eec\u5e76\u4e0d\u4f1a\u5728\u672c\u8282\u8bfe\u4e2d\u5b9e\u73b0\u6240\u6709\u51fd\u6570\uff0c\u56e0\u4e3a\u8fd8\u6709\u4e00\u4e9b\u66f4\u52a0\u91cd\u8981\u7684\u8bdd\u9898\u9700\u8981\u8ba8\u8bba\u3002 \u8bb0\u4f4f\uff1a\u4e0d\u8981\u671f\u671b\u4e00\u6b21\u6027\u5b9e\u73b0\u5168\u90e8\u7684\u4ee3\u7801\u3002\u4e00\u6b65\u4e00\u6b65\u5b8c\u6210\uff0c\u5e76\u4e14\u6bcf\u4e00\u6b65\u90fd\u8981\u8fdb\u884c\u6d4b\u8bd5\u3002 \u5b9e\u73b0\u6b65\u9aa43\uff1a\u6700\u7ec8\u6d4b\u8bd5 \u5f53\u7a0b\u5e8f\u7f16\u5199\u201c\u5b8c\u6210\u201d\u540e\uff0c\u6700\u540e\u4e00\u6b65\u5219\u662f\u5bf9\u5b8c\u6574\u7684\u7a0b\u5e8f\u8fdb\u884c\u6d4b\u8bd5\uff0c\u786e\u5b9a\u5176\u884c\u4e3a\u662f\u5426\u6ee1\u8db3\u9884\u671f\u3002\u5982\u679c\u4e0d\u6ee1\u8db3\uff0c\u5219\u9700\u8981\u8fdb\u884c\u4fee\u590d\u3002 \u7f16\u7a0b\u5fe0\u8a00 \u5f00\u59cb\u65f6\u4e00\u5207\u4ece\u7b80 \uff1a\u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u5bf9\u5176\u8981\u5b9e\u73b0\u7684\u7a0b\u5e8f\u548c\u529f\u80fd\u5177\u6709\u5b8f\u4f1f\u7684\u84dd\u56fe\uff1a\u201c\u6211\u60f3\u8981\u8bbe\u8ba1\u4e00\u6b3e\u5177\u6709\u56fe\u5f62\u754c\u9762\u3001\u914d\u4e50\u548c\u968f\u673a\u5730\u7262\u7684\u89d2\u8272\u626e\u6f14\u6e38\u620f\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u6e38\u620f\u5730\u56fe\u8fd8\u5305\u62ec\u57ce\u9547\uff0c\u4f60\u53ef\u4ee5\u5728\u57ce\u9547\u91cc\u552e\u5356\u4f60\u6536\u96c6\u5230\u7684\u6218\u5229\u54c1\u201d\u3002\u5982\u679c\u4f60\u4ece\u4e00\u5f00\u59cb\u5c31\u8981\u5b8c\u6210\u4e00\u4e2a\u975e\u5e38\u590d\u6742\u7684\u4efb\u52a1\uff0c\u4f60\u53ef\u80fd\u4f1a\u5728\u8fdb\u5ea6\u4e0d\u4f73\u65f6\u611f\u5230\u4e0d\u77e5\u6240\u63aa\u6216\u5fc3\u7070\u610f\u51b7\u3002\u6240\u4ee5\uff0c\u4f60\u5176\u5b9e\u5e94\u8be5\u5c06\u4f60\u5f00\u59cb\u65f6\u7684\u76ee\u6807\u8bbe\u7684\u5c3d\u53ef\u80fd\u7b80\u5355\uff0c\u5373\u4e00\u4e9b\u4f60\u80af\u5b9a\u80fd\u591f\u5b8c\u6210\u7684\u76ee\u6807\u3002\u4f8b\u5982\uff0c\u201c\u6211\u60f3\u8981\u5728\u5c4f\u5e55\u4e0a\u663e\u793a\u4e00\u4e2a2D\u7684\u533a\u57df\u201d\u3002 \u9010\u6e10\u6dfb\u52a0\u65b0\u7684\u529f\u80fd \uff1a\u5f53\u4e0a\u8ff0\u7b80\u5355\u7684\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u8003\u8651\u4e3a\u5176\u589e\u52a0\u65b0\u7684\u529f\u80fd\u4e86\u3002\u4f8b\u5982\uff0c\u5f53\u4e0a\u8ff02D\u533a\u57df\u53ef\u4ee5\u88ab\u6b63\u5e38\u663e\u793a\u5728\u5c4f\u5e55\u4e0a\u540e\uff0c\u4f60\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u80fd\u591f\u79fb\u52a8\u7684\u89d2\u8272\u3002\u5f53\u89d2\u8272\u53ef\u4ee5\u79fb\u52a8\u540e\uff0c\u6dfb\u52a0\u4e00\u4e9b\u5899\u4f53\u9650\u5236\u89d2\u8272\u7684\u79fb\u52a8\u8def\u5f84\u3002\u5899\u4f53\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5229\u7528\u5899\u4f53\u6765\u6784\u5efa\u4e00\u4e2a\u57ce\u9547\u5730\u56fe\u3002\u57ce\u9547\u5b8c\u6210\u540e\uff0c\u6dfb\u52a0\u4e00\u4e2a\u5546\u4ebaNPC\u3002\u901a\u8fc7\u8fd9\u79cd\u5faa\u5e8f\u6e10\u8fdb\u7684\u65b9\u5f0f\u4e3a\u4f60\u7684\u7a0b\u5e8f\u6dfb\u52a0\u65b0\u529f\u80fd\uff0c\u7a0b\u5e8f\u6700\u7ec8\u4f1a\u53d8\u5f97\u8d8a\u6765\u8d8a\u590d\u6742\uff0c\u540c\u65f6\u4e5f\u4e0d\u4f1a\u8ba9\u4f60\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u611f\u5230\u4e0d\u77e5\u6240\u63aa\u3002 \u4e00\u6b21\u53ea\u505a\u4e00\u4ef6\u4e8b \uff1a\u4e0d\u8981\u671f\u671b\u4e00\u6b21\u6027\u5199\u5b8c\u5168\u90e8\u7684\u4ee3\u7801\uff0c\u4e5f\u4e0d\u8981\u628a\u4f60\u7684\u6ce8\u610f\u529b\u5206\u6563\u5728\u591a\u4e2a\u4efb\u52a1\u4e0a\u3002\u4e00\u6b21\u53ea\u505a\u4e00\u4ef6\u4e8b\u3002\u4e00\u4e2a\u6b63\u5e38\u5de5\u4f5c\u7684\u529f\u80fd+\u4e94\u4e2a\u5c1a\u672a\u5b9e\u73b0\u7684\u529f\u80fd\uff0c\u597d\u8fc7\u516d\u4e2a\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u7684\u529f\u80fd\u3002\u6ce8\u610f\u529b\u88ab\u5206\u6563\u7684\u65f6\u5019\uff0c\u4f60\u4f1a\u66f4\u5bb9\u6613\u72af\u9519\uff0c\u4e5f\u66f4\u5bb9\u6613\u5fd8\u8bb0\u91cd\u8981\u7684\u7ec6\u8282\u3002 \u4e00\u8fb9\u5b9e\u73b0\u3001\u4e00\u8fb9\u6d4b\u8bd5 \uff1a\u65b0\u7684\u7a0b\u5e8f\u5458\u5e38\u5e38\u4f1a\u4e00\u6b21\u6027\u5199\u5b8c\u5168\u90e8\u7684\u7a0b\u5e8f\uff0c\u7136\u540e\u624d\u5bf9\u5176\u8fdb\u884c\u7f16\u8bd1\uff0c\u7136\u540e\u5c31\u201c\u6536\u83b7\u201d\u4e86\u975e\u5e38\u591a\u7684\u62a5\u9519\u4fe1\u606f\u3002\u5199\u5b8c\u7684\u4ee3\u7801\u4e0d\u80fd\u7f16\u8bd1\uff0c\u4e0d\u4ec5\u5413\u4eba\uff0c\u800c\u4e14\u8fd8\u96be\u4ee5\u67e5\u627e\u5176\u4ea7\u751f\u9519\u8bef\u7684\u539f\u56e0\u3002\u6b63\u786e\u7684\u505a\u6cd5\u662f\u7f16\u5199\u4e00\u4e9b\u4ee3\u7801\uff0c\u7136\u540e\u5c31\u5bf9\u5176\u8fdb\u884c\u7f16\u8bd1\u548c\u6d4b\u8bd5\u3002\u5982\u679c\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u90a3\u4e48\u4fee\u590d\u8d77\u6765\u4e5f\u975e\u5e38\u7b80\u5355\u3002\u5f53\u4f60\u786e\u5b9a\u8fd9\u4e9b\u4ee3\u7801\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\uff0c\u518d\u91cd\u590d\u4e0a\u8ff0\u64cd\u4f5c\uff0c\u5f00\u59cb\u7f16\u5199\u63a5\u4e0b\u6765\u7684\u4ee3\u7801\u3002\u8fd9\u4e48\u505a\u6216\u8bb8\u4f1a\u201c\u62d6\u7d2f\u201d\u4f60\u7684\u8fdb\u5ea6\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u5f53\u4f60\u5b8c\u6210\u540e \uff0c\u4ee3\u7801\u5e94\u8be5\u662f\u80fd\u591f\u6b63\u786e\u5de5\u4f5c\u7684\uff0c\u8fd9\u597d\u8fc7\u4f60\u5199\u5b8c\u4ee3\u7801\u540e\u518d\u82b1\u8d39\u4e24\u500d\u591a\u7684\u65f6\u95f4\u53bb\u5b9a\u4f4d\u95ee\u9898\u3002 \u4e0d\u8981\u8fc7\u65e9\u5730\u4f18\u5316\u4ee3\u7801 \uff1a\u7b2c\u4e00\u7248\u529f\u80fd\uff08\u6216\u7a0b\u5e8f\uff09\u5f88\u5c11\u662f\u5b8c\u7f8e\u7684\u3002\u800c\u4e14\uff0c\u7a0b\u5e8f\u672c\u8eab\u4e5f\u4f1a\u968f\u7740\u65f6\u95f4\u4e0d\u65ad\u88ab\u66f4\u65b0\uff0c\u6bd5\u7adf\u4f60\u8fd8\u8981\u4e3a\u5176\u6dfb\u52a0\u529f\u80fd\uff0c\u6216\u8005\u91cd\u65b0\u7ec4\u7ec7\u4ee3\u7801\u3002\u5982\u679c\u4f60\u5728\u65e9\u671f\u5c31\u82b1\u8d39\u5f88\u591a\u65f6\u95f4\u53bb\u6253\u78e8\u8fd9\u4e9b\u4ee3\u7801\uff08\u6dfb\u52a0\u5f88\u591a\u6587\u6863\u3001\u786e\u4fdd\u6ee1\u8db3\u6700\u4f73\u5b9e\u8df5\u3001\u4f18\u5316\u7b49\uff09\uff0c\u90a3\u4e48\u6709\u53ef\u80fd\u4f1a\u56e0\u4e3a\u5fc5\u987b\u4fee\u6539\u4ee3\u7801\u800c\u4f7f\u5f97\u8fd9\u4e9b\u52aa\u529b\u90fd\u4ed8\u4e4b\u4e1c\u6d41\u3002\u6b63\u786e\u7684\u505a\u6cd5\u662f\uff0c\u8ba9\u4f60\u7684\u7a0b\u5e8f\u529f\u80fd\u4fdd\u6301\u6700\u7b80\u53ef\u7528\uff0c\u7136\u540e\u7ee7\u7eed\u5176\u4ed6\u7684\u5de5\u4f5c\u3002\u5f53\u4f60\u5bf9\u7a0b\u5e8f\u611f\u5230\u6ee1\u610f\u65f6\uff0c\u518d\u5bf9\u5176\u8fdb\u884c\u4e00\u7cfb\u5217\u7684\u4f18\u5316\u3002\u4e0d\u8fc7\uff0c\u4e0d\u8981\u671f\u671b\u5176\u8fbe\u5230\u5b8c\u7f8e\u7684\u72b6\u6001\uff0c\u5927\u578b\u7a0b\u5e8f\u5f88\u5c11\u662f\u5b8c\u7f8e\u7684\uff0c\u5b83\u603b\u662f\u6709\u8fd9\u6837\u6216\u90a3\u6837\u7684\u95ee\u9898\u9700\u8981\u6211\u4eec\u4f18\u5316\u3002\u505a\u5230\u8db3\u591f\u597d\u5c31\u53ef\u4ee5\u4e86\u3002 \u5927\u591a\u6570\u65b0\u624b\u7a0b\u5e8f\u5458\u5e76\u4e0d\u80fd\u5f88\u597d\u7684\u9075\u5faa\u4e0a\u8ff0\u5fe0\u8a00\uff08\u56e0\u4e3a\u770b\u4e0a\u53bb\u5f88\u9ebb\u70e6\uff0c\u4e5f\u4e0d\u5982\u5199\u4ee3\u7801\u6709\u8da3\uff09\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u5177\u6709\u4e00\u5b9a\u89c4\u6a21\u7684\u4ee3\u7801\u6765\u8bf4\uff0c\u5b8c\u6574\u9075\u5faa\u4e0a\u8ff0\u5efa\u8bae\u7edd\u5bf9\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8282\u7ea6\u5f88\u591a\u65f6\u95f4\uff08\u4ece\u957f\u671f\u6765\u770b\uff09\u3002\u7b80\u5355\u5730\u8fdb\u884c\u4e00\u4e9b\u8bbe\u8ba1\u89c4\u5212\uff0c\u5c31\u53ef\u4ee5\u4e3a\u540e\u671f\u8282\u7ea6\u5927\u91cf\u7684\u8c03\u8bd5\u65f6\u95f4\u3002 \u597d\u6d88\u606f\u662f\uff0c\u4e00\u65e6\u4f60\u4e60\u60ef\u4e86\u8fd9\u4e48\u505a\u4ee5\u540e\uff0c\u5b83\u4fbf\u4f1a\u6162\u6162\u5730\u6210\u4e3a\u4f60\u7684\u4e60\u60ef\u3002\u6700\u7ec8\uff0c\u4f60\u5c06\u80fd\u591f\u4e0d\u5728\u8fdb\u884c\u4efb\u4f55\u9884\u5148\u8bbe\u8ba1\u7684\u60c5\u51b5\u4e0b\uff0c\u5b8c\u6210\u6574\u4e2a\u51fd\u6570\u7684\u7f16\u5199\u3002","title":"2.13 - \u5982\u4f55\u8bbe\u8ba1\u7b2c\u4e00\u4e2a\u7a0b\u5e8f"},{"location":"2-13-How-to-design-your-first-programs/#1","text":"\u4e3a\u4e86\u7f16\u5199\u4e00\u4e2a\u6210\u529f\u7684\u7a0b\u5e8f\uff0c\u4f60\u5fc5\u987b\u8981\u9996\u5148\u660e\u786e\u4f60\u7684\u76ee\u6807\u662f\u4ec0\u4e48\u3002\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u8be5\u76ee\u6807\u5e94\u8be5\u7528\u4e00\u4e24\u53e5\u8bdd\u5c31\u53ef\u4ee5\u63cf\u8ff0\u3002\u901a\u5e38\u6765\u8bb2\uff0c\u4ece\u7528\u6237\u7684\u89d2\u5ea6\u6765\u63cf\u8ff0\u7f16\u7a0b\u7684\u7ed3\u679c\u662f\u5f88\u6709\u7528\u7684\uff0c\u4f8b\u5982\uff1a \u5141\u8bb8\u7528\u6237\u53ef\u4ee5\u7ba1\u7406\u4e00\u4e9b\u5217\u7684\u59d3\u540d\u5e76\u5c06\u5176\u5173\u8054\u5230\u7535\u8bdd\u53f7\u7801\uff1b \u968f\u673a\u751f\u6210\u770b\u4e0a\u53bb\u5f88\u9177\u7684\u6d1e\u7a74\u573a\u666f\u4ee5\u4f5c\u4e3a\u6e38\u620f\u4e2d\u7684\u5730\u7262\u6765\u4f7f\u7528\uff1b \u751f\u6210\u4e00\u4e2a\u9ad8\u80a1\u606f\u80a1\u7968\u7684\u63a8\u8350\u5217\u8868\uff1b \u5bf9\u4e00\u4e2a\u7403\u4ece\u9ad8\u5854\u6389\u843d\u5230\u5730\u9762\u6240\u9700\u7684\u65f6\u95f4\u8fdb\u884c\u5efa\u6a21\u3002 \u5c3d\u7ba1\u8fd9\u4e00\u6b65\u6240\u505a\u7684\u4e8b\u60c5\u770b\u4e0a\u53bb\u90fd\u662f\u5f88\u663e\u7136\u7684\uff0c\u5b83\u7684\u4f5c\u7528\u5374\u4e0d\u5bb9\u5ffd\u89c6\u3002\u5bf9\u4e8e\u7f16\u7a0b\u6765\u8bf4\uff0c\u6700\u7cdf\u7cd5\u7684\u83ab\u8fc7\u4e8e\u4f60\u5199\u5b8c\u7684\u7a0b\u5e8f\u5374\u4e0d\u662f\u4f60\u60f3\u8981\u7684\uff08\u6216\u8005\u4f60\u8001\u677f\u60f3\u8981\u7684\uff09\uff01","title":"\u8bbe\u8ba1\u6b65\u9aa41\uff1a\u5b9a\u76ee\u6807"},{"location":"2-13-How-to-design-your-first-programs/#2","text":"\u5c3d\u7ba1\uff0c\u5b9a\u4e49\u95ee\u9898\u6709\u52a9\u4e8e\u4f60\u786e\u5b9a\u4f60\u671f\u671b\u7684\u7ed3\u679c\u662f\u58f0\u660e\uff0c\u4f46\u76ee\u6807\u4ecd\u7136\u662f\u4e0d\u6e05\u695a\u7684\u3002\u63a5\u4e0b\u6765\u4e00\u6b65\u5c31\u662f\u8003\u8651\u5982\u4f55\u8bbe\u8ba1\u9700\u6c42\u3002 \u9700\u6c42\u4e00\u65b9\u9762\u6307\u7684\u662f\u4f60\u80fd\u591f\u4f7f\u7528\u7684\u6709\u9650\u8d44\u6e90\uff08\u4f8b\u5982\uff0c\u9884\u7b97\u3001\u5de5\u671f\u3001\u7a7a\u95f4\u548c\u5185\u5b58\u7b49\u7b49\uff09\uff0c\u53e6\u4e00\u65b9\u9762\u6307\u7684\u662f\u4f60\u7684\u7a0b\u5e8f\u5fc5\u987b\u5177\u5907\u7684\u80fd\u591f\u6ee1\u8db3\u7528\u6237\u9700\u8981\u7684\u80fd\u529b\u3002\u6ce8\u610f\uff0c\u9700\u6c42\u5173\u6ce8\u7684\u4ecd\u7136\u662f \u4ec0\u4e48 \uff0c\u800c\u4e0d\u662f \u5982\u4f55 \u3002 \u4f8b\u5982\uff1a \u7535\u8bdd\u53f7\u7801\u5e94\u8be5\u80fd\u591f\u88ab\u50a8\u5b58\uff0c\u8fd9\u6837\u4ee5\u540e\u53ef\u4ee5\u7528\u6765\u8fdb\u884c\u56de\u62e8\uff1b \u968f\u673a\u7684\u5730\u7262\u5e94\u8be5\u603b\u662f\u5305\u542b\u4e00\u6761\u4ece\u5165\u53e3\u5230\u51fa\u53e3\u7684\u8def\uff1b \u80a1\u7968\u63a8\u8350\u5e94\u8be5\u53c2\u8003\u5386\u53f2\u4ef7\u683c\u4fe1\u606f\uff1b \u7528\u6237\u5e94\u8be5\u80fd\u591f\u8f93\u5165\u9ad8\u5854\u7684\u9ad8\u5ea6\uff1b \u6211\u4eec\u9700\u8981\u5728\u4e03\u5929\u5185\u5b8c\u6210\u53ef\u4f9b\u6d4b\u8bd5\u7684\u7248\u672c\uff1b \u7a0b\u5e8f\u9700\u8981\u572810\u79d2\u5185\u5b8c\u6210\u7528\u6237\u7684\u8bf7\u6c42\uff1b \u7a0b\u5e8f\u5728\u7528\u6237\u4f7f\u7528\u65f6\u5d29\u6e83\u9000\u51fa\u7684\u6982\u7387\u5e94\u8be5\u5c0f\u4e8e 0.1%\u3002 \u4e00\u4e2a\u95ee\u9898\u53ef\u80fd\u4f1a\u6d3e\u751f\u51fa\u5f88\u591a\u9700\u6c42\uff0c\u53ea\u6709\u6240\u6709\u7684\u9700\u6c42\u90fd\u88ab\u6ee1\u8db3\uff0c\u624d\u80fd\u8bf4\u89e3\u51b3\u65b9\u6848\u88ab\u5b9e\u73b0\u4e86\u3002","title":"\u8bbe\u8ba1\u6b65\u9aa42\uff1a\u5b9a\u9700\u6c42"},{"location":"2-13-How-to-design-your-first-programs/#3","text":"\u5982\u679c\u4f60\u5df2\u7ecf\u662f\u4e00\u4e2a\u6709\u7ecf\u9a8c\u7684\u7a0b\u5e8f\u5458\u4e86\uff0c\u5728\u8bbe\u8ba1\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u53ef\u80fd\u8fd8\u9700\u8981\u8003\u8651\u5176\u4ed6\u7684\u4e00\u4e9b\u95ee\u9898\uff0c\u4f8b\u5982\uff1a \u5b9a\u4e49\u7a0b\u5e8f\u9700\u8981\u8fd0\u884c\u7684\u5e73\u53f0\u7684\u4f53\u7cfb\u7ed3\u6784\u548c\uff08\u6216\uff09\u64cd\u4f5c\u7cfb\u7edf\uff1b \u786e\u5b9a\u4f60\u9700\u8981\u4f7f\u7528\u54ea\u4e9b\u5de5\u5177\uff1b \u786e\u5b9a\u4f60\u662f\u72ec\u7acb\u5b8c\u6210\u4ee3\u7801\u7f16\u5199\u8fd8\u662f\u4ee5\u56e2\u961f\u5408\u4f5c\u7684\u65b9\u5f0f\u5b8c\u6210\uff1b \u5b9a\u4e49\u6d4b\u8bd5\u3001\u53cd\u9988\u548c\u91ca\u653e\u7b56\u7565\uff1b \u5b9a\u4e49\u5982\u4f55\u5907\u4efd\u4ee3\u7801\u3002 \u4e0d\u8fc7\uff0c\u4f5c\u4e3a\u65b0\u624b\u7a0b\u5e8f\u5458\uff0c\u4e0a\u9762\u51e0\u4e2a\u95ee\u9898\u5176\u5b9e\u90fd\u5f88\u7b80\u5355\uff1a\u4f60\u5c06\u4f1a\u72ec\u7acb\u5730\u5728\u4f60\u81ea\u5df1\u7684\u7535\u8111\u4e0a\u7f16\u5199\u4ee3\u7801\uff0c\u6240\u9700\u7684\u5de5\u5177\u5c31\u662f\u4f60\u4e0b\u8f7d\u5e76\u5b89\u88c5\u4e86\u7684IDE\uff0c\u4f60\u7f16\u5199\u7684\u7a0b\u5e8f\u9664\u4e86\u4f60\u81ea\u5df1\u6ca1\u6709\u5176\u4ed6\u4eba\u4f1a\u4f7f\u7528\u3002 \u5373\u4fbf\u5982\u6b64\uff0c\u5982\u679c\u4f60\u7f16\u5199\u7684\u4ee3\u7801\u4e0d\u662f\u5be5\u5be5\u51e0\u884c\uff0c\u4f60\u8fd8\u662f\u5e94\u8be5\u8003\u8651\u4e00\u4e0b\u5982\u4f55\u5907\u4efd\u4f60\u7684\u4ee3\u7801\u3002\u5c06\u4ee3\u7801\u62f7\u8d1d\u6216\u8005\u538b\u7f29\u540e\u653e\u5230\u7535\u8111\u7684\u5176\u4ed6\u76ee\u5f55\u4e2d\u662f\u4e0d\u591f\u7684\uff08\u5c3d\u7ba1\u804a\u80dc\u4e8e\u65e0\uff09\u3002\u5982\u679c\u4f60\u7684\u7535\u8111\u574f\u4e86\uff0c\u4f60\u7f16\u5199\u7684\u4ee3\u7801\u4e5f\u5c31\u4ed8\u4e4b\u4e1c\u6d41\u4e86\u3002\u4e00\u4e2a\u597d\u7684\u5907\u4efd\u7b56\u7565\uff0c\u5e94\u8be5\u662f\u628a\u4ee3\u7801\u5168\u90e8\u62f7\u8d1d\u4e00\u4efd\u540e\u62f7\u5230\u5176\u4ed6\u7535\u8111\u6216\u5176\u4ed6\u5730\u65b9\u3002\u6709\u5f88\u591a\u529e\u6cd5\u53ef\u4ee5\u5b9e\u73b0\uff0c\u4f8b\u5982\u5c06\u5176\u538b\u7f29\u4e3aZip\u6587\u4ef6\u540e\u53d1\u9001\u5230\u4f60\u81ea\u5df1\u7684\u90ae\u7bb1\uff0c\u62f7\u8d1d\u5230Dropbox\u6216\u8005\u7f51\u76d8\uff0c\u6216\u8005\u901a\u8fc7FTP\u4f20\u5230\u5176\u4ed6\u7535\u8111\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5c40\u57df\u7f51\u62f7\u5230\u5176\u4ed6\u7535\u8111\u4e0a\uff0c\u6216\u8005\u4f7f\u7528\u4f4d\u4e8e\u5176\u4ed6\u7535\u8111\u6216\u4e91\u7aef\u7684\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff08\u4f8b\u5982\uff1aGithub\uff09\u3002\u7248\u672c\u63a7\u5236\u7cfb\u7edf\u9664\u4e86\u5b58\u50a8\u6587\u4ef6\u4ee5\u5916\uff0c\u8fd8\u6709\u5f88\u591a\u5176\u4ed6\u6709\u7528\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u5b83\u53ef\u4ee5\u5c06\u4f60\u7684\u4ee3\u7801\u56de\u9000\u5230\u4e4b\u524d\u7684\u7248\u672c\u3002","title":"\u8bbe\u8ba1\u6b65\u9aa43\uff1a\u5b9a\u4e49\u5de5\u5177\u3001\u76ee\u6807\u4ea7\u7269\u4ee5\u53ca\u5907\u4efd\u8ba1\u5212"},{"location":"2-13-How-to-design-your-first-programs/#4","text":"\u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\uff0c\u6211\u4eec\u65f6\u5e38\u9700\u8981\u5b8c\u6210\u4e00\u4e9b\u975e\u5e38\u590d\u6742\u7684\u95ee\u9898\u3002\u627e\u5230\u7684\u95ee\u9898\u7684\u89e3\u51b3\u529e\u6cd5\uff0c\u901a\u5e38\u5e76\u4e0d\u5bb9\u6613\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528 \u81ea\u9876\u5411\u4e0b(top down) \u7684\u529e\u6cd5\u6765\u89e3\u51b3\u95ee\u9898\u3002\u4e3a\u4e86\u89e3\u51b3\u4e00\u4e2a\u7279\u522b\u590d\u6742\u7684\u95ee\u9898\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u628a\u8fd9\u4e2a\u95ee\u9898\u5206\u89e3\u4e3a\u591a\u4e2a\u76f8\u5bf9\u6765\u8bf4\u66f4\u5bb9\u6613\u89e3\u51b3\u7684\u5b50\u95ee\u9898\u3002\u5982\u679c\u5b50\u95ee\u9898\u4ecd\u7136\u96be\u4ee5\u89e3\u51b3\uff0c\u5b83\u4eec\u8fd8\u53ef\u4ee5\u88ab\u8fdb\u4e00\u6b65\u5206\u5272\u4e3a\u66f4\u591a\u7684\u5b50\u95ee\u9898\u3002\u901a\u8fc7\u4e0d\u65ad\u5730\u5c06\u590d\u6742\u95ee\u9898\u5206\u89e3\u6210\u7b80\u5355\u7684\u5b50\u95ee\u9898\uff0c\u4f60\u6700\u7ec8\u53ef\u4ee5\u5f97\u5230\u5f88\u591a\u66f4\u5bb9\u6613\u88ab\u5904\u7406\u7684\u5b50\u95ee\u9898\u3002 \u4e3e\u4f8b\u6765\u8bf4\uff0c\u5982\u679c\u6211\u4eec\u9700\u8981\u6253\u626b\u5c4b\u5b50\uff0c\u90a3\u4e48\u5206\u89e3\u4efb\u52a1\u7684\u7ed3\u6784\u53ef\u80fd\u4f1a\u662f\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a \u6253\u626b\u5c4b\u5b50 \u6253\u626b\u5c4b\u5b50\u662f\u4e00\u4e2a\u5f88\u5927\u7684\u4efb\u52a1\uff0c\u8ba9\u6211\u4eec\u5c06\u5176\u5206\u89e3\u4e3a\u591a\u4e2a\u5b50\u4efb\u52a1\uff1a \u6253\u626b\u5c4b\u5b50 \u6e05\u7406\u5730\u6bef \u6253\u626b\u536b\u751f\u95f4 \u6253\u626b\u53a8\u623f \u6bd4\u4e4b\u524d\u5bb9\u6613\u5904\u7406\u4e86\u4e00\u4e9b\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u9010\u9879\u89e3\u51b3\u8fd9\u4e9b\u5b50\u95ee\u9898\u4e86\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u5176\u5b9e\u8fd8\u80fd\u5c06\u8fd9\u4e9b\u4efb\u52a1\u7ee7\u7eed\u5206\u89e3\uff1a \u6253\u626b\u5c4b\u5b50 \u6e05\u7406\u5730\u6bef \u6253\u626b\u536b\u751f\u95f4 \u6e05\u6d17\u9a6c\u6876 \u6e05\u7406\u6c34\u6c60 \u6253\u626b\u53a8\u623f \u6e05\u7406\u7076\u53f0 \u6253\u626b\u7076\u53f0 \u6e05\u6d17\u6c34\u6c60 \u5012\u5783\u573e \u73b0\u5728\uff0c\u6211\u4eec\u9762\u5bf9\u7684\u662f\u4e00\u7cfb\u5217\u5177\u6709\u5c42\u6b21\u7ed3\u6784\u7684\u95ee\u9898\u4e86\uff0c\u800c\u4e14\u5b83\u4eec\u4e2d\u4efb\u4f55\u4e00\u9879\u90fd\u4e0d\u590d\u6742\u3002\u901a\u8fc7\u5b8c\u6210\u8fd9\u4e9b\u76f8\u5bf9\u66f4\u5bb9\u6613\u5904\u7406\u7684\u5b50\u4efb\u52a1\u4e4b\u540e\uff0c\u6211\u4eec\u4e5f\u5c31\u5b8c\u6210\u4e86\u6253\u626b\u5c4b\u5b50\u8fd9\u4e2a\u66f4\u590d\u6742\u7684\u95ee\u9898\u3002 \u53e6\u5916\u4e00\u79cd\u5206\u89e3\u4efb\u52a1\u7684\u65b9\u5f0f\u79f0\u4e3a \u81ea\u5e95\u5411\u4e0a(bottom) \u65b9\u6cd5\u3002\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u65f6\uff0c\u6211\u4eec\u4f1a\u4ece\u4e00\u7cfb\u5217\u7b80\u5355\u7684\u4efb\u52a1\u5f00\u59cb\uff0c\u7136\u540e\u901a\u8fc7\u5408\u5e76\u5206\u7ec4\u7684\u65b9\u5f0f\u6784\u5efa\u4efb\u52a1\u5217\u8868\u3002 \u5927\u591a\u6570\u4eba\u5e73\u65f6\u4e0d\u662f\u4e0a\u5b66\u5c31\u662f\u4e0a\u73ed\uff0c\u90a3\u4e48\u5c31\u4ee5\u6b64\u6765\u4e3e\u4f8b\uff0c\u5047\u8bbe\u6211\u4eec\u8981\u89e3\u51b3\u7684\u662f\u201c\u53bb\u4e0a\u73ed\u201d\u8fd9\u4e2a\u95ee\u9898\uff0c\u5982\u679c\u6709\u4eba\u95ee\u4f60\uff0c\u4f60\u65e9\u4e0a\u51fa\u95e8\u524d\u90fd\u505a\u54ea\u4e9b\u4e8b\u5462\uff1f\u4f60\u7684\u56de\u7b54\u53ef\u80fd\u5305\u542b\u4e0b\u9762\u8fd9\u4e9b\u4efb\u52a1\uff1a \u62ff\u51fa\u8981\u7a7f\u7684\u8863\u670d \u7a7f\u8863 \u5403\u65e9\u996d \u5f00\u8f66\u53bb\u516c\u53f8 \u5237\u7259 \u8d77\u5e8a \u505a\u65e9\u9910 \u4e0a\u8f66 \u6d17\u6fa1 \u4f7f\u7528\u81ea\u5e95\u5411\u4e0a\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u5b83\u4eec\u7684\u76f8\u4f3c\u6027\uff0c\u4e3a\u8fd9\u4e9b\u4efb\u52a1\u521b\u5efa\u4e00\u4e2a\u5c42\u6b21\u7ed3\u6784\uff1a \u4ece\u8d77\u5e8a\u5230\u4e0a\u73ed\u8981\u505a\u7684\u4e8b \u5367\u5ba4\u8981\u505a\u7684\u4e8b \u8d77\u5e8a \u62ff\u8863\u670d \u7a7f\u8863 \u536b\u751f\u95f4\u8981\u505a\u7684\u4e8b \u6d17\u6fa1 \u5237\u7259 \u65e9\u9910\u76f8\u5173 \u51c6\u5907\u9ea6\u7247 \u5403\u9ea6\u7247 \u901a\u52e4\u76f8\u5173 \u4e0a\u8f66 \u5f00\u8f66\u5230\u516c\u53f8 \u5176\u5b9e\u6211\u4eec\u4e0d\u96be\u770b\u51fa\uff0c\u8fd9\u4e9b\u5c42\u6b21\u7ed3\u6784\u5bf9\u4e8e\u7f16\u7a0b\u6765\u8bf4\u662f\u975e\u5e38\u6709\u7528\u7684\uff0c\u56e0\u4e3a\u4e00\u65e6\u4efb\u52a1\u7684\u7ed3\u6784\u6e05\u695a\u4e86\uff0c\u7a0b\u5e8f\u7684\u5927\u4f53\u7ed3\u6784\u5176\u5b9e\u4e5f\u5df2\u7ecf\u6e05\u695a\u4e86\u3002\u6700\u4e0a\u5c42\u7684\u4efb\u52a1\uff08\u6253\u626b\u5c4b\u5b50\u6216\u53bb\u4e0a\u73ed\uff09\u5c31\u53ef\u4ee5\u662f\u7a0b\u5e8f\u7684 main() \u51fd\u6570\uff08\u56e0\u4e3a\u5b83\u662f\u4f60\u8981\u89e3\u51b3\u7684\u4e3b\u8981(main)\u95ee\u9898\uff09\u3002\u800c\u5176\u4ed6\u5b50\u9879\u76ee\u5219\u53ef\u4ee5\u5b9e\u73b0\u4e3a\u4e0d\u540c\u7684\u51fd\u6570\u3002 \u5982\u679c\u5176\u4e2d\u67d0\u4e00\u9879\u4efb\u52a1\uff08\u51fd\u6570\uff09\u5728\u5b9e\u73b0\u65f6\u4ecd\u7136\u8fc7\u4e8e\u56f0\u96be\uff0c\u5219\u53ef\u4ee5\u7ee7\u7eed\u5c06\u5176\u5206\u89e3\u4e3a\u51e0\u4e2a\u5b50\u95ee\u9898\u3002\u6700\u7ec8\u4f60\u5c31\u53ef\u4ee5\u5c06\u7a0b\u5e8f\u5206\u5272\u4e3a\u591a\u4e2a\u5bb9\u6613\u5b9e\u73b0\u7684\u51fd\u6570\u3002","title":"\u8bbe\u8ba1\u6b65\u9aa44\uff1a\u5c06\u590d\u6742\u95ee\u9898\u5207\u5206\u4e3a\u591a\u4e2a\u7b80\u5355\u95ee\u9898"},{"location":"2-13-How-to-design-your-first-programs/#5","text":"\u73b0\u5728\uff0c\u7a0b\u5e8f\u7684\u7ed3\u6784\u5df2\u7ecf\u6e05\u695a\u4e86\uff0c\u63a5\u4e0b\u6765\u9700\u8981\u5c06\u8fd9\u4e9b\u4efb\u52a1\u5173\u8054\u5176\u4ed6\u3002\u9996\u5148\uff0c\u6211\u4eec\u8981\u786e\u5b9a\u8fd9\u4e9b\u8981\u4efb\u52a1\u7684\u53d1\u751f\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u4f60\u65e9\u4e0a\u8d77\u6765\uff0c\u5b8c\u6210\u4e0a\u9762\u90a3\u4e9b\u4efb\u52a1\u7684\u987a\u5e8f\u662f\u4ec0\u4e48\uff1f\u5dee\u4e0d\u591a\u5e94\u8be5\u662f\u8fd9\u6837\u7684\u5427\uff1a \u5367\u5ba4\u8981\u505a\u7684\u4e8b \u536b\u751f\u95f4\u8981\u505a\u7684\u4e8b \u65e9\u9910\u76f8\u5173 \u901a\u52e4\u76f8\u5173 \u5982\u679c\u4f60\u8981\u7f16\u5199\u4e00\u4e2a\u8ba1\u7b97\u5668\u7a0b\u5e8f\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u6309\u7167\u4e0b\u9762\u6b65\u9aa4\u6765\u505a\uff1a \u4ece\u7528\u6237\u83b7\u53d6\u7b2c\u4e00\u4e2a\u6570 \u4ece\u7528\u6237\u83b7\u53d6\u9700\u8981\u6267\u884c\u7684\u8fd0\u7b97\u64cd\u4f5c \u4ece\u7528\u6237\u83b7\u53d6\u7b2c\u4e8c\u4e2a\u6570 \u8ba1\u7b97\u7ed3\u679c \u6253\u5370\u7ed3\u679c \u5b8c\u6210\u4e0a\u8ff0\u8bbe\u8ba1\u5de5\u4f5c\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f00\u59cb\u5b9e\u73b0\u4ee3\u7801\u4e86\uff01","title":"\u8bbe\u8ba1\u6b65\u9aa45\uff1a\u786e\u5b9a\u76f8\u5173\u4e8b\u4ef6\u53d1\u751f\u7684\u987a\u5e8f"},{"location":"2-13-How-to-design-your-first-programs/#1main","text":"\u73b0\u5728\u53ef\u4ee5\u5f00\u59cb\u5b9e\u73b0\u4ee3\u7801\u4e86\u3002\u6839\u636e\u4e0a\u9762\u987a\u5e8f\u7ed3\u6784\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u5b8c\u6210 main \u51fd\u6570\u7684\u6846\u67b6\uff0c\u6682\u65f6\u65e0\u9700\u62c5\u5fc3\u8f93\u5165\u8f93\u51fa\u7684\u95ee\u9898\u3002 1 2 3 4 5 6 7 8 9 int main () { // doBedroomThings(); // doBathroomThings(); // doBreakfastThings(); // doTransportationThings(); return 0 ; } \u6216\u8005\u5bf9\u4e8e\u8ba1\u7b97\u5668\u4f8b\u5b50\u6765\u8bf4\u662f\u8fd9\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 int main () { // Get first number from user // getUserInput(); // Get mathematical operation from user // getMathematicalOperation(); // Get second number from user // getUserInput(); // Calculate result // calculateResult(); // Print result // printResult(); return 0 ; } \u6ce8\u610f\uff0c\u5982\u679c\u4f60\u4e5f\u4f7f\u7528\u8fd9\u79cd\u201c\u5927\u7eb2\u6846\u67b6\u201d\u7684\u65b9\u6cd5\u6765\u6784\u5efa\u7a0b\u5e8f\uff0c\u8fd9\u91cc\u6d89\u53ca\u7684\u51fd\u6570\u90fd\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\uff0c\u6bd5\u7adf\u5b83\u4eec\u8fd8\u6ca1\u6709\u88ab\u5b9a\u4e49\u3002\u5728\u51fd\u6570\u88ab\u5b9e\u9645\u5b9a\u4e49\u524d\u5148\u5c06\u5176\u6682\u65f6\u5148\u6ce8\u91ca\u6389\u662f\u4e00\u79cd\u65b9\u6cd5\uff08\u4e0a\u9762\u5c55\u793a\u7684\u5c31\u662f\u8fd9\u79cd\u65b9\u6cd5\uff09\u3002\u6b64\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u5bf9\u51fd\u6570\u8fdb\u884c \u6253\u6869(stub) \uff0c\u5373\u4e3a\u7a0b\u5e8f\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u51fd\u6570\u4f53\uff0c\u4ee5\u4fbf\u7a0b\u5e8f\u53ef\u4ee5\u7f16\u8bd1\u901a\u8fc7\u3002","title":"\u5b9e\u73b0\u6b65\u9aa41\uff1a\u7f16\u5199main\u51fd\u6570\u6846\u67b6"},{"location":"2-13-How-to-design-your-first-programs/#2_1","text":"\u5728\u8fd9\u4e00\u6b65\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u51fd\u6570\u90fd\u9700\u8981\u505a\u4e09\u4ef6\u4e8b\uff1a \u5b9a\u4e49\u51fd\u6570\u7684\u539f\u578b\uff08\u8f93\u5165\u8f93\u51fa\uff09\uff1b \u7f16\u5199\u51fd\u6570\uff1b \u6d4b\u8bd5\u51fd\u6570\u3002 \u5982\u679c\u4f60\u7684\u51fd\u6570\u7c92\u5ea6\u8db3\u591f\u7ec6\uff0c\u90a3\u4e48\u6bcf\u4e2a\u51fd\u6570\u90fd\u5e94\u8be5\u662f\u975e\u5e38\u7b80\u5355\u548c\u76f4\u63a5\u7684\u3002\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u5b9e\u73b0\u8d77\u6765\u975e\u5e38\u590d\u6742\uff0c\u90a3\u4e48\u5f88\u53ef\u80fd\u5b83\u8fd8\u9700\u8981\u88ab\u8fdb\u4e00\u6b65\u5206\u5272\u4e3a\u591a\u4e2a\u66f4\u5bb9\u6613\u5b9e\u73b0\u7684\u5b50\u51fd\u6570\uff08 \u4e5f\u53ef\u80fd\u662f\u4f60\u7684\u4efb\u52a1\u987a\u5e8f\u6709\u95ee\u9898\uff0c\u4f60\u5e94\u8be5\u91cd\u65b0\u5ba1\u89c6\u4e00\u4e0b\u5404\u4e2a\u6b65\u9aa4\u7684\u987a\u5e8f \uff09\u3002 \u63a5\u4e0b\u6765\uff0c\u5b9e\u73b0\u8ba1\u7b97\u5668\u7a0b\u5e8f\u4e2d\u7684\u7b2c\u4e00\u4e2a\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include // Full implementation of the getUserInput function int getUserInput () { std :: cout << \"Enter an integer \" ; int input {}; std :: cin >> input ; return input ; } int main () { // \u4ece\u7528\u6237\u83b7\u53d6\u7b2c\u4e00\u4e2a\u8f93\u5165 int value { getUserInput () }; // \u6ce8\u610f\uff0c\u8fd9\u91cc\u7684\u4ee3\u7801\u672c\u8eab\u5c31\u5177\u5907\u6d4b\u8bd5\u51fd\u6570\u8f93\u51fa\u7684\u529f\u80fd std :: cout << value ; // \u7528\u4e8e\u786e\u4fdd getUserInput() \u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u7684\u8c03\u8bd5\u4ee3\u7801\uff0c\u7a0d\u540e\u9700\u8981\u79fb\u9664 // Get mathematical operation from user // getMathematicalOperation(); // Get second number from user // getUserInput(); // Calculate result // calculateResult(); // Print result // printResult(); return 0 ; } \u9996\u5148\uff0c\u786e\u5b9a\u51fd\u6570 getUserInput \u4e0d\u9700\u8981\u63a5\u53d7\u4efb\u4f55 \u5b9e\u53c2 \u3002\u540c\u65f6\uff0c\u8be5\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u53cd\u6620\u5230\u51fd\u6570\u539f\u578b\u4e0a\uff0c\u5373\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a int \uff0c\u540c\u65f6\u6ca1\u6709 \u5f62\u53c2 \u3002\u63a5\u4e0b\u6765\uff0c\u7f16\u5199\u51fd\u6570\u4f53\u3002\u51fd\u6570\u4f53\u975e\u5e38\u76f4\u63a5\uff0c\u53ea\u5305\u542b\u56db\u6761\u8bed\u53e5 \u8bed\u53e5 \u3002\u6700\u540e\uff0c\u5728 main \u51fd\u6570\u4e2d\u7f16\u5199\u4e00\u4e9b\u7528\u4e8e\u6d4b\u8bd5\u51fd\u6570 getUserInput \u529f\u80fd\uff08\u5305\u62ec\u5176\u8fd4\u56de\u503c) \u7684\u4e34\u65f6\u4ee3\u7801\uff0c\u786e\u5b9a\u51fd\u6570\u662f\u5426\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u3002 \u591a\u6b21\u8fd0\u884c\u7a0b\u5e8f\u5e76\u4f7f\u7528\u4e0d\u540c\u7684\u8f93\u5165\u503c\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7ed3\u679c\u90fd\u7b26\u5408\u9884\u671f\u3002\u5982\u679c\u53d1\u73b0\u7a0b\u5e8f\u4e0d\u80fd\u6b63\u786e\u5de5\u4f5c\uff0c\u5219\u95ee\u9898\u53ef\u80fd\u5c31\u51fa\u5728\u521a\u624d\u7f16\u5199\u7684\u4ee3\u7801\u4e2d\u3002 \u4e00\u65e6\u4f60\u786e\u5b9a\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\uff0c\u90a3\u4e48\u8fd9\u4e9b\u4e34\u65f6\u6d4b\u8bd5\u4ee3\u7801\u5c31\u53ef\u4ee5\u88ab\u79fb\u9664\u4e86\uff0c\u7136\u540e\u5f00\u59cb\u5b9e\u73b0\u4e0b\u4e00\u4e2a\u51fd\u6570\uff08 getMathematicalOperation \uff09\u3002\u6211\u4eec\u5e76\u4e0d\u4f1a\u5728\u672c\u8282\u8bfe\u4e2d\u5b9e\u73b0\u6240\u6709\u51fd\u6570\uff0c\u56e0\u4e3a\u8fd8\u6709\u4e00\u4e9b\u66f4\u52a0\u91cd\u8981\u7684\u8bdd\u9898\u9700\u8981\u8ba8\u8bba\u3002 \u8bb0\u4f4f\uff1a\u4e0d\u8981\u671f\u671b\u4e00\u6b21\u6027\u5b9e\u73b0\u5168\u90e8\u7684\u4ee3\u7801\u3002\u4e00\u6b65\u4e00\u6b65\u5b8c\u6210\uff0c\u5e76\u4e14\u6bcf\u4e00\u6b65\u90fd\u8981\u8fdb\u884c\u6d4b\u8bd5\u3002","title":"\u5b9e\u73b0\u6b65\u9aa42\uff1a\u5b9e\u73b0\u6bcf\u4e2a\u51fd\u6570"},{"location":"2-13-How-to-design-your-first-programs/#3_1","text":"\u5f53\u7a0b\u5e8f\u7f16\u5199\u201c\u5b8c\u6210\u201d\u540e\uff0c\u6700\u540e\u4e00\u6b65\u5219\u662f\u5bf9\u5b8c\u6574\u7684\u7a0b\u5e8f\u8fdb\u884c\u6d4b\u8bd5\uff0c\u786e\u5b9a\u5176\u884c\u4e3a\u662f\u5426\u6ee1\u8db3\u9884\u671f\u3002\u5982\u679c\u4e0d\u6ee1\u8db3\uff0c\u5219\u9700\u8981\u8fdb\u884c\u4fee\u590d\u3002","title":"\u5b9e\u73b0\u6b65\u9aa43\uff1a\u6700\u7ec8\u6d4b\u8bd5"},{"location":"2-13-How-to-design-your-first-programs/#_1","text":"\u5f00\u59cb\u65f6\u4e00\u5207\u4ece\u7b80 \uff1a\u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u5bf9\u5176\u8981\u5b9e\u73b0\u7684\u7a0b\u5e8f\u548c\u529f\u80fd\u5177\u6709\u5b8f\u4f1f\u7684\u84dd\u56fe\uff1a\u201c\u6211\u60f3\u8981\u8bbe\u8ba1\u4e00\u6b3e\u5177\u6709\u56fe\u5f62\u754c\u9762\u3001\u914d\u4e50\u548c\u968f\u673a\u5730\u7262\u7684\u89d2\u8272\u626e\u6f14\u6e38\u620f\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u6e38\u620f\u5730\u56fe\u8fd8\u5305\u62ec\u57ce\u9547\uff0c\u4f60\u53ef\u4ee5\u5728\u57ce\u9547\u91cc\u552e\u5356\u4f60\u6536\u96c6\u5230\u7684\u6218\u5229\u54c1\u201d\u3002\u5982\u679c\u4f60\u4ece\u4e00\u5f00\u59cb\u5c31\u8981\u5b8c\u6210\u4e00\u4e2a\u975e\u5e38\u590d\u6742\u7684\u4efb\u52a1\uff0c\u4f60\u53ef\u80fd\u4f1a\u5728\u8fdb\u5ea6\u4e0d\u4f73\u65f6\u611f\u5230\u4e0d\u77e5\u6240\u63aa\u6216\u5fc3\u7070\u610f\u51b7\u3002\u6240\u4ee5\uff0c\u4f60\u5176\u5b9e\u5e94\u8be5\u5c06\u4f60\u5f00\u59cb\u65f6\u7684\u76ee\u6807\u8bbe\u7684\u5c3d\u53ef\u80fd\u7b80\u5355\uff0c\u5373\u4e00\u4e9b\u4f60\u80af\u5b9a\u80fd\u591f\u5b8c\u6210\u7684\u76ee\u6807\u3002\u4f8b\u5982\uff0c\u201c\u6211\u60f3\u8981\u5728\u5c4f\u5e55\u4e0a\u663e\u793a\u4e00\u4e2a2D\u7684\u533a\u57df\u201d\u3002 \u9010\u6e10\u6dfb\u52a0\u65b0\u7684\u529f\u80fd \uff1a\u5f53\u4e0a\u8ff0\u7b80\u5355\u7684\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u8003\u8651\u4e3a\u5176\u589e\u52a0\u65b0\u7684\u529f\u80fd\u4e86\u3002\u4f8b\u5982\uff0c\u5f53\u4e0a\u8ff02D\u533a\u57df\u53ef\u4ee5\u88ab\u6b63\u5e38\u663e\u793a\u5728\u5c4f\u5e55\u4e0a\u540e\uff0c\u4f60\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u80fd\u591f\u79fb\u52a8\u7684\u89d2\u8272\u3002\u5f53\u89d2\u8272\u53ef\u4ee5\u79fb\u52a8\u540e\uff0c\u6dfb\u52a0\u4e00\u4e9b\u5899\u4f53\u9650\u5236\u89d2\u8272\u7684\u79fb\u52a8\u8def\u5f84\u3002\u5899\u4f53\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5229\u7528\u5899\u4f53\u6765\u6784\u5efa\u4e00\u4e2a\u57ce\u9547\u5730\u56fe\u3002\u57ce\u9547\u5b8c\u6210\u540e\uff0c\u6dfb\u52a0\u4e00\u4e2a\u5546\u4ebaNPC\u3002\u901a\u8fc7\u8fd9\u79cd\u5faa\u5e8f\u6e10\u8fdb\u7684\u65b9\u5f0f\u4e3a\u4f60\u7684\u7a0b\u5e8f\u6dfb\u52a0\u65b0\u529f\u80fd\uff0c\u7a0b\u5e8f\u6700\u7ec8\u4f1a\u53d8\u5f97\u8d8a\u6765\u8d8a\u590d\u6742\uff0c\u540c\u65f6\u4e5f\u4e0d\u4f1a\u8ba9\u4f60\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u611f\u5230\u4e0d\u77e5\u6240\u63aa\u3002 \u4e00\u6b21\u53ea\u505a\u4e00\u4ef6\u4e8b \uff1a\u4e0d\u8981\u671f\u671b\u4e00\u6b21\u6027\u5199\u5b8c\u5168\u90e8\u7684\u4ee3\u7801\uff0c\u4e5f\u4e0d\u8981\u628a\u4f60\u7684\u6ce8\u610f\u529b\u5206\u6563\u5728\u591a\u4e2a\u4efb\u52a1\u4e0a\u3002\u4e00\u6b21\u53ea\u505a\u4e00\u4ef6\u4e8b\u3002\u4e00\u4e2a\u6b63\u5e38\u5de5\u4f5c\u7684\u529f\u80fd+\u4e94\u4e2a\u5c1a\u672a\u5b9e\u73b0\u7684\u529f\u80fd\uff0c\u597d\u8fc7\u516d\u4e2a\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u7684\u529f\u80fd\u3002\u6ce8\u610f\u529b\u88ab\u5206\u6563\u7684\u65f6\u5019\uff0c\u4f60\u4f1a\u66f4\u5bb9\u6613\u72af\u9519\uff0c\u4e5f\u66f4\u5bb9\u6613\u5fd8\u8bb0\u91cd\u8981\u7684\u7ec6\u8282\u3002 \u4e00\u8fb9\u5b9e\u73b0\u3001\u4e00\u8fb9\u6d4b\u8bd5 \uff1a\u65b0\u7684\u7a0b\u5e8f\u5458\u5e38\u5e38\u4f1a\u4e00\u6b21\u6027\u5199\u5b8c\u5168\u90e8\u7684\u7a0b\u5e8f\uff0c\u7136\u540e\u624d\u5bf9\u5176\u8fdb\u884c\u7f16\u8bd1\uff0c\u7136\u540e\u5c31\u201c\u6536\u83b7\u201d\u4e86\u975e\u5e38\u591a\u7684\u62a5\u9519\u4fe1\u606f\u3002\u5199\u5b8c\u7684\u4ee3\u7801\u4e0d\u80fd\u7f16\u8bd1\uff0c\u4e0d\u4ec5\u5413\u4eba\uff0c\u800c\u4e14\u8fd8\u96be\u4ee5\u67e5\u627e\u5176\u4ea7\u751f\u9519\u8bef\u7684\u539f\u56e0\u3002\u6b63\u786e\u7684\u505a\u6cd5\u662f\u7f16\u5199\u4e00\u4e9b\u4ee3\u7801\uff0c\u7136\u540e\u5c31\u5bf9\u5176\u8fdb\u884c\u7f16\u8bd1\u548c\u6d4b\u8bd5\u3002\u5982\u679c\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u90a3\u4e48\u4fee\u590d\u8d77\u6765\u4e5f\u975e\u5e38\u7b80\u5355\u3002\u5f53\u4f60\u786e\u5b9a\u8fd9\u4e9b\u4ee3\u7801\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\uff0c\u518d\u91cd\u590d\u4e0a\u8ff0\u64cd\u4f5c\uff0c\u5f00\u59cb\u7f16\u5199\u63a5\u4e0b\u6765\u7684\u4ee3\u7801\u3002\u8fd9\u4e48\u505a\u6216\u8bb8\u4f1a\u201c\u62d6\u7d2f\u201d\u4f60\u7684\u8fdb\u5ea6\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u5f53\u4f60\u5b8c\u6210\u540e \uff0c\u4ee3\u7801\u5e94\u8be5\u662f\u80fd\u591f\u6b63\u786e\u5de5\u4f5c\u7684\uff0c\u8fd9\u597d\u8fc7\u4f60\u5199\u5b8c\u4ee3\u7801\u540e\u518d\u82b1\u8d39\u4e24\u500d\u591a\u7684\u65f6\u95f4\u53bb\u5b9a\u4f4d\u95ee\u9898\u3002 \u4e0d\u8981\u8fc7\u65e9\u5730\u4f18\u5316\u4ee3\u7801 \uff1a\u7b2c\u4e00\u7248\u529f\u80fd\uff08\u6216\u7a0b\u5e8f\uff09\u5f88\u5c11\u662f\u5b8c\u7f8e\u7684\u3002\u800c\u4e14\uff0c\u7a0b\u5e8f\u672c\u8eab\u4e5f\u4f1a\u968f\u7740\u65f6\u95f4\u4e0d\u65ad\u88ab\u66f4\u65b0\uff0c\u6bd5\u7adf\u4f60\u8fd8\u8981\u4e3a\u5176\u6dfb\u52a0\u529f\u80fd\uff0c\u6216\u8005\u91cd\u65b0\u7ec4\u7ec7\u4ee3\u7801\u3002\u5982\u679c\u4f60\u5728\u65e9\u671f\u5c31\u82b1\u8d39\u5f88\u591a\u65f6\u95f4\u53bb\u6253\u78e8\u8fd9\u4e9b\u4ee3\u7801\uff08\u6dfb\u52a0\u5f88\u591a\u6587\u6863\u3001\u786e\u4fdd\u6ee1\u8db3\u6700\u4f73\u5b9e\u8df5\u3001\u4f18\u5316\u7b49\uff09\uff0c\u90a3\u4e48\u6709\u53ef\u80fd\u4f1a\u56e0\u4e3a\u5fc5\u987b\u4fee\u6539\u4ee3\u7801\u800c\u4f7f\u5f97\u8fd9\u4e9b\u52aa\u529b\u90fd\u4ed8\u4e4b\u4e1c\u6d41\u3002\u6b63\u786e\u7684\u505a\u6cd5\u662f\uff0c\u8ba9\u4f60\u7684\u7a0b\u5e8f\u529f\u80fd\u4fdd\u6301\u6700\u7b80\u53ef\u7528\uff0c\u7136\u540e\u7ee7\u7eed\u5176\u4ed6\u7684\u5de5\u4f5c\u3002\u5f53\u4f60\u5bf9\u7a0b\u5e8f\u611f\u5230\u6ee1\u610f\u65f6\uff0c\u518d\u5bf9\u5176\u8fdb\u884c\u4e00\u7cfb\u5217\u7684\u4f18\u5316\u3002\u4e0d\u8fc7\uff0c\u4e0d\u8981\u671f\u671b\u5176\u8fbe\u5230\u5b8c\u7f8e\u7684\u72b6\u6001\uff0c\u5927\u578b\u7a0b\u5e8f\u5f88\u5c11\u662f\u5b8c\u7f8e\u7684\uff0c\u5b83\u603b\u662f\u6709\u8fd9\u6837\u6216\u90a3\u6837\u7684\u95ee\u9898\u9700\u8981\u6211\u4eec\u4f18\u5316\u3002\u505a\u5230\u8db3\u591f\u597d\u5c31\u53ef\u4ee5\u4e86\u3002 \u5927\u591a\u6570\u65b0\u624b\u7a0b\u5e8f\u5458\u5e76\u4e0d\u80fd\u5f88\u597d\u7684\u9075\u5faa\u4e0a\u8ff0\u5fe0\u8a00\uff08\u56e0\u4e3a\u770b\u4e0a\u53bb\u5f88\u9ebb\u70e6\uff0c\u4e5f\u4e0d\u5982\u5199\u4ee3\u7801\u6709\u8da3\uff09\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u5177\u6709\u4e00\u5b9a\u89c4\u6a21\u7684\u4ee3\u7801\u6765\u8bf4\uff0c\u5b8c\u6574\u9075\u5faa\u4e0a\u8ff0\u5efa\u8bae\u7edd\u5bf9\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8282\u7ea6\u5f88\u591a\u65f6\u95f4\uff08\u4ece\u957f\u671f\u6765\u770b\uff09\u3002\u7b80\u5355\u5730\u8fdb\u884c\u4e00\u4e9b\u8bbe\u8ba1\u89c4\u5212\uff0c\u5c31\u53ef\u4ee5\u4e3a\u540e\u671f\u8282\u7ea6\u5927\u91cf\u7684\u8c03\u8bd5\u65f6\u95f4\u3002 \u597d\u6d88\u606f\u662f\uff0c\u4e00\u65e6\u4f60\u4e60\u60ef\u4e86\u8fd9\u4e48\u505a\u4ee5\u540e\uff0c\u5b83\u4fbf\u4f1a\u6162\u6162\u5730\u6210\u4e3a\u4f60\u7684\u4e60\u60ef\u3002\u6700\u7ec8\uff0c\u4f60\u5c06\u80fd\u591f\u4e0d\u5728\u8fdb\u884c\u4efb\u4f55\u9884\u5148\u8bbe\u8ba1\u7684\u60c5\u51b5\u4e0b\uff0c\u5b8c\u6210\u6574\u4e2a\u51fd\u6570\u7684\u7f16\u5199\u3002","title":"\u7f16\u7a0b\u5fe0\u8a00"},{"location":"2-2-Function-return-values-value-returning%20functions/","tags":["return","undefined behavior","function"],"text":"\u8bf7\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { // get a value from the user std :: cout << \"Enter an integer: \" ; int num {}; std :: cin >> num ; // print the value doubled std :: cout << num << \" doubled is: \" << num * 2 << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u4ece\u6982\u5ff5\u4e0a\u5c06\u5305\u542b\u4e24\u90e8\u5206\uff1a\u9996\u5148\u6211\u4eec\u4ece\u7528\u6237\u83b7\u53d6\u4e00\u4e2a\u8f93\u5165\uff0c\u7136\u540e\u544a\u8bc9\u7528\u6237\u8be5\u8f93\u5165\u7684\u4e24\u500d\u662f\u591a\u5c11\u3002 \u867d\u7136\u8fd9\u4e2a\u7a0b\u5e8f\u5df2\u7ecf\u8db3\u591f\u7b80\u5355\uff0c\u6ca1\u5fc5\u8981\u5c06\u5176\u5212\u5206\u4e3a\u591a\u4e2a\u51fd\u6570\uff0c\u4f46\u5982\u679c\u6211\u4eec\u6267\u610f\u8981\u8fd9\u4e48\u505a\u5462\uff1f\u4ece\u7528\u6237\u83b7\u53d6\u8f93\u5165\u662f\u4e00\u4e2a\u975e\u5e38\u660e\u786e\u7684\u4efb\u52a1\uff0c\u5c06\u5176\u4f5c\u4e3a\u4e00\u4e2a\u51fd\u6570\u5e94\u8be5\u662f\u5f88\u5408\u9002\u7684\u3002 \u56e0\u6b64\uff0c\u7a0b\u5e8f\u88ab\u4fee\u6539\u6210\u4e86\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 // This program doesn't work #include void getValueFromUser () { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; } int main () { getValueFromUser (); // Ask user for input int num {}; // How do we get the value from getValueFromUser() and use it to initialize this variable? std :: cout << num << \" doubled is: \" << num * 2 << '\\n' ; return 0 ; } \u5c3d\u7ba1\u770b\u4e0a\u53bb\u4e0a\u9762\u7684\u5c1d\u8bd5\u6ca1\u4ec0\u4e48\u95ee\u9898\uff0c\u4f46\u5b9e\u9645\u4e0a\u5b83\u5374\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002 \u5f53\u51fd\u6570 getValueFromUser \u88ab\u8c03\u7528\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u8981\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6574\u578b\u6570\uff0c\u8fd9\u7b26\u5408\u6211\u4eec\u7684\u9884\u671f\u3002\u4f46\u662f\u5f53\u51fd\u6570 getValueFromUser \u8c03\u7528\u7ed3\u675f\u5e76\u8fd4\u56de main \u51fd\u6570\u65f6\uff0c\u521a\u624d\u8f93\u5165\u7684\u503c\u5c31\u88ab\u4e22\u5f03\u4e86\u3002\u53d8\u91cf num \u59cb\u7ec8\u90fd\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u4e3a\u7528\u6237\u8f93\u5165\u7684\u503c\uff0c\u56e0\u6b64\u8f93\u51fa\u7ed3\u679c\u59cb\u7ec8\u662f 0 \u3002 \u95ee\u9898\u51fa\u5728 getValueFromUser \u51fd\u6570\u8fd4\u56de\u5230 main \u51fd\u6570\u7684\u65f6\u5019\uff0c\u6211\u4eec\u6ca1\u6709\u5c06\u7528\u6237\u8f93\u5165\u7684\u503c\u4e00\u8d77\u8fd4\u56de\uff0c\u56e0\u6b64 main \u51fd\u6570\u5c31\u65e0\u6cd5\u4f7f\u7528\u8be5\u503c\u3002 \u8fd4\u56de\u503c \u5728\u7f16\u5199\u51fd\u6570\u7684\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u786e\u5b9a\u8be5\u51fd\u6570\u662f\u5426\u5e94\u8be5\u8fd4\u56de\u4e00\u4e2a\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u5982\u679c\u9700\u8981\uff0c\u6709\u4e24\u4ef6\u4e8b\u9700\u8981\u53bb\u505a\u3002 \u9996\u5148\uff0c\u5fc5\u987b\u6307\u5b9a\u51fd\u6570\u8fd4\u56de\u503c\u7684\u7c7b\u578b\uff0c\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u7684\u5b9a\u4e49\u4f4d\u4e8e\u51fd\u6570\u540d\u524d\u9762\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c getValueFromUser \u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a void \uff08\u8868\u793a\u65e0\u9700\u8fd4\u56de\u4efb\u4f55\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\uff09\uff0c\u800c main \u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u5219\u4e3a int \uff08\u5373\u8fd4\u56de\u4e00\u4e2a int \u7c7b\u578b\u7684\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\uff09\u3002\u6ce8\u610f\uff0c\u8fd9\u91cc\u5e76\u4e0d\u662f\u5b9a\u4e49\u8fd4\u56de\u7684\u5177\u4f53\u662f\u4ec0\u4e48\u503c\uff0c\u800c\u4ec5\u662f\u6307\u5b9a\u5176 \u7c7b\u578b \u3002 \u76f8\u5173\u5185\u5bb9 \u8fd4\u56de\u503c\u4e3a void \u7c7b\u578b\u7684\u51fd\u6570\u5c06\u5728\u4e0b\u8282\u8bfe\u4e2d\u4ecb\u7ecd\uff08 2-3-Void-functions-non-value returning functions \uff09 \u5176\u6b21\uff0c\u5728\u9700\u8981\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4e2d\uff0c\u9700\u8981\u901a\u8fc7 return \u8bed\u53e5\u6765\u6307\u5b9a\u8981\u8fd4\u56de\u7684\u5177\u4f53\u503c\u3002\u8be5\u5177\u4f53\u503c\u79f0\u4e3a\u51fd\u6570\u7684 \u8fd4\u56de\u503c \u3002\u5f53 return \u8bed\u53e5\u88ab\u6267\u884c\u65f6\uff0c\u51fd\u6570\u4f1a\u7acb\u5373\u9000\u51fa\uff0c\u540c\u65f6\u5c06\u8fd4\u56de\u503c \u62f7\u8d1d \u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a \u6309\u503c\u8fd4\u56de \u3002 \u4ee5\u4e0b\u9762\u8fd9\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u4e3a\u4f8b\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include // int \u662f\u8fd4\u56de\u503c\u7c7b\u578b // \u8fd4\u56de\u503c\u7c7b\u578b\u4e3a int \u610f\u5473\u7740\u51fd\u6570\u4f1a\u8fd4\u56de\u67d0\u4e2a\u6574\u578b\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\uff08\u5177\u4f53\u662f\u4ec0\u4e48\u503c\u5e76\u4e0d\u4f1a\u5728\u6b64\u5904\u6307\u5b9a\uff09 int returnFive () { // return \u8bed\u53e5\u6307\u5b9a\u4e86\u8981\u8fd4\u56de\u7684\u5177\u4f53\u503c return 5 ; // \u8fd4\u56de 5 \u7ed9\u4e3b\u8c03\u51fd\u6570 } int main () { std :: cout << returnFive () << '\\n' ; // prints 5 std :: cout << returnFive () + 2 << '\\n' ; // prints 7 returnFive (); // \u51fd\u6570\u8fd4\u56de\u4e86 5\uff0c\u4f46\u662f\u8be5\u503c\u88ab\u5ffd\u7565\u4e86\uff0c\u56e0\u4e3a\u6ca1\u6709\u4f7f\u7528\u8be5\u503c\u505a\u4efb\u4f55\u4e8b\u60c5 return 0 ; } \u51fd\u6570\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 5 7 \u4ee3\u7801\u6267\u884c\u4ece main \u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\uff0c\u7b2c\u4e00\u6761\u8bed\u53e5\u5bf9 returnFive \u8fdb\u884c\u4e86\u6c42\u503c\uff0c\u5373\u8c03\u7528\u4e86\u8be5\u51fd\u6570\u3002\u51fd\u6570 returnFive \u8fd4\u56de\u503c\u4e3a 5\uff0c\u8be5\u503c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u540e\uff0c\u901a\u8fc7 std:: cout \u88ab\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002 \u5728\u7b2c\u4e8c\u5904\u51fd\u6570\u8c03\u7528\uff0c\u8bed\u53e5 returnFive \u8fdb\u884c\u4e86\u6c42\u503c\uff0c \u5176\u7ed3\u679c\u5c31\u662f returnFive \u51fd\u6570\u518d\u6b21\u88ab\u8c03\u7528\u3002\u51fd\u6570\u4ecd\u7136\u5c06\u8fd4\u56de\u503c 5 \u8fd4\u56de\u7ed9\u4e86\u4e3b\u8c03\u51fd\u6570\u3002\u8868\u8fbe\u5f0f 5 + 2 \u7684\u6c42\u503c\u7ed3\u679c\u662f 7\uff0c\u968f\u540e\u8be5\u503c\u901a\u8fc7 std:: cout \u88ab\u6253\u5370\u5230\u4e86\u63a7\u5236\u53f0\u3002 \u7b2c\u4e09\u6761\u8bed\u53e5\u518d\u6b21\u8c03\u7528\u4e86 returnFive \u51fd\u6570\uff0c\u5176\u8fd4\u56de\u503c\u4ecd\u7136\u88ab\u8fd4\u56de\u7ed9\u4e86\u4e3b\u8c03\u51fd\u6570\uff0c\u4f46\u662f main \u51fd\u6570\u5e76\u6ca1\u6709\u5bf9\u8be5\u8fd4\u56de\u503c\u8fdb\u884c\u4efb\u4f55\u64cd\u4f5c\uff0c\u56e0\u6b64\u4ec0\u4e48\u4e8b\u60c5\u90fd\u6ca1\u6709\u53d1\u751f\uff08\u8fd4\u56de\u503c\u88ab\u5ffd\u7565\u4e86\uff09\u3002 \u6ce8\u610f\uff1a\u9664\u975e\u4e3b\u8c03\u51fd\u6570\u901a\u8fc7 std:: cout \u5c06\u8fd4\u56de\u503c\u53d1\u9001\u5230\u63a7\u5236\u53f0\u8fdb\u884c\u6253\u5370\uff0c\u5426\u5219\u8be5\u503c\u5e76\u4e0d\u4f1a\u88ab\u6253\u5370\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6700\u540e\u4e00\u6b21\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u4e3b\u8c03\u51fd\u6570\u6ca1\u6709\u5c06\u8fd4\u56de\u503c\u53d1\u9001\u5230 std:: cout \uff0c\u56e0\u6b64\u5176\u7ed3\u679c\u6ca1\u6709\u6253\u5370\u3002 \u5c0f\u8d34\u58eb \u5f53\u88ab\u8c03\u51fd\u6570\u8fd4\u56de\u503c\u662f\uff0c\u4e3b\u8c03\u51fd\u6570\u53ef\u4ee5\u51b3\u5b9a\u662f\u5426\u5728\u8868\u8fbe\u5f0f\u6216\u8005\u8bed\u53e5\u4e2d\u4f7f\u7528\u8be5\u8fd4\u56de\u503c\uff08\u4f8b\u5982\uff1a\u5c06\u5176\u8d4b\u503c\u7ed9\u67d0\u4e2a\u53d8\u91cf\u6216\u5c06\u5176\u53d1\u9001\u5230 std:: cout \uff09\uff0c\u4e5f\u53ef\u4ee5\u5ffd\u7565\u5b83\uff08\u4e0d\u5bf9\u8fd4\u56de\u503c\u505a\u4efb\u4f55\u64cd\u4f5c\uff09\u3002 \u4fee\u6539\u7a0b\u5e8f \u6709\u4e86\u4e0a\u9762\u7684\u77e5\u8bc6\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u8bfe\u7a0b\u5f00\u59cb\u65f6\u7684\u7a0b\u5e8f\u8fdb\u884c\u9002\u5f53\u7684\u4fee\u6539\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int getValueFromUser () // this function now returns an integer value { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; // return the value the user entered back to the caller } int main () { int num { getValueFromUser () }; // \u4f7f\u7528 getValueFromUser() \u7684\u8fd4\u56de\u503c\u5bf9 num \u8fdb\u884c\u521d\u59cb\u5316 std :: cout << num << \" doubled is: \" << num * 2 << '\\n' ; return 0 ; } \u5f53\u4e0a\u8ff0\u7a0b\u5e8f\u5f00\u59cb\u6267\u884c\u65f6\uff0c main \u51fd\u6570\u4e2d\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u4f1a\u521b\u5efa\u4e00\u4e2a int \u7c7b\u578b\u7684\u53d8\u91cf num \u3002\u5f53\u7a0b\u5e8f\u51c6\u5907\u8981\u5bf9 num \u521d\u59cb\u5316\u65f6\uff0c\u5b83\u4f1a\u53d1\u73b0\u6b64\u5904\u6709\u4e00\u4e2a\u51fd\u6570\u8c03\u7528 getValueFromUser() \uff0c\u56e0\u6b64\u5b83\u4f1a\u8df3\u8f6c\u53bb\u6267\u884c\u8be5\u51fd\u6570\u3002\u51fd\u6570 getValueFromUser \u8981\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u503c\uff0c\u7136\u540e\u5b83\u5c06\u8be5\u8f93\u5165\u503c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570( main )\u3002\u8be5\u8fd4\u56de\u503c\u88ab\u7528\u6765\u5bf9\u53d8\u91cf num \u8fdb\u884c\u521d\u59cb\u5316\u3002 \u8bf7\u81ea\u884c\u7f16\u8bd1\u8be5\u51fd\u6570\u5e76\u8fd0\u884c\u5176\u51e0\u6b21\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u80fd\u591f\u6b63\u786e\u8fd0\u884c\u3002 \u8fd4\u56de\u5230\u4e3b\u51fd\u6570 \u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u601d\u7ef4\u5de5\u5177\u6765\u7406\u89e3 main \u51fd\u6570\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u5f53\u7a0b\u5e8f\u6267\u884c\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u53d6\u8c03\u7528 main \u51fd\u6570\u3002\u968f\u540e\u7a0b\u5e8f\u5c31\u4f1a\u8df3\u8f6c\u5230 main \u51fd\u6570\u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\u3002\u51fd\u6570\u4f53\u4e2d\u7684\u8bed\u53e5\u4f1a\u987a\u5e8f\u6267\u884c\u3002\u6700\u7ec8\uff0c main \u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\u503c\uff08\u901a\u5e38\u662f 0\uff09\uff0c\u7136\u540e\u7a0b\u5e8f\u5c31\u7ec8\u6b62\u4e86\u3002 main \u51fd\u6570\u7684\u7684\u8fd4\u56de\u503c\u901a\u5e38\u79f0\u4e3a\u72b6\u6001\u7801\uff08\u6709\u65f6\u4e5f\u79f0\u4e3a\u9000\u51fa\u72b6\u6001\u7801\uff0c\u6781\u5c11\u6570\u60c5\u51b5\u4e0b\u4e5f\u79f0\u4e3a\u8fd4\u56de\u7801\uff09\uff0c\u56e0\u4e3a\u8be5\u8fd4\u56de\u503c\u7528\u6765\u53cd\u6620\u7a0b\u5e8f\u662f\u5426\u6b63\u786e\u6267\u884c\u3002 \u6839\u636e\u5b9a\u4e49\uff0c\u72b6\u6001\u7801\u4e3a 0 \u8868\u793a\u7a0b\u5e8f\u6267\u884c\u6210\u529f\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u7a0b\u5e8f\u6b63\u5e38\u8fd0\u884c\uff0c main \u7684\u8fd4\u56de\u503c\u5e94\u8be5\u4e3a 0 \u3002 \u975e 0 \u7684\u8fd4\u56de\u503c\u901a\u5e38\u7528\u6765\u8868\u793a\u7a0b\u5e8f\u6267\u884c\u5931\u8d25\uff08\u5c3d\u7ba1\u5927\u591a\u6570\u64cd\u4f5c\u7cfb\u7edf\u90fd\u6309\u7167\u4e0a\u8ff0\u7ea6\u5b9a\u6765\u5bf9\u5f85\u8fd4\u56de\u503c\uff0c\u4f46\u662f\u4e25\u683c\u6765\u8bb2\uff0c\u5176\u53ef\u79fb\u690d\u6027\u5e76\u4e0d\u80fd\u5f97\u5230\u4fdd\u8bc1\uff09\u3002 \u6269\u5c55\u9605\u8bfb C++\u6807\u51c6\u4e2d\u503c\u5b9a\u4e49\u4e86\u4e09\u79cd\u8fd4\u56de\u72b6\u6001\u7801\uff1a0\u3001 EXIT_SUCCESS \u548c EXIT_FAILURE \u30020 \u548c EXIT_SUCCESS \u90fd\u8868\u793a\u7a0b\u5e8f\u6267\u884c\u6210\u529f\u3002 EXIT_FAILURE \u5219\u8868\u793a\u7a0b\u5e8f\u6ca1\u6709\u6210\u529f\u6267\u884c\u3002 EXIT_SUCCESS \u548c EXIT_FAILURE \u5728 \u5934\u6587\u4ef6\u4e2d\u58f0\u660e\uff1a 1 2 3 4 5 6 #include // for EXIT_SUCCESS and EXIT_FAILURE int main () { return EXIT_SUCCESS ; } \u5982\u679c\u4f60\u60f3\u5c3d\u53ef\u80fd\u4fdd\u8bc1\u53ef\u79fb\u690d\u6027\uff0c\u8bf7\u4f7f\u7528 0 \u6216\u8005 EXIT_SUCCESS \u8868\u793a\u7a0b\u5e8f\u6210\u529f\u6267\u884c\uff0c\u6216\u8005\u4f7f\u7528 EXIT_FAILURE \u8868\u793a\u7a0b\u5e8f\u6267\u884c\u5931\u8d25\u3002 C++ \u4e0d\u5141\u8bb8\u663e\u5f0f\u8c03\u7528 main \u51fd\u6570\u3002 \u5c31\u76ee\u524d\u800c\u8a00\uff0c\u4f60\u8fd8\u5e94\u8be5\u5c06 main \u51fd\u6570\u5b9a\u4e49\u5728\u6587\u4ef6\u7684\u6700\u4e0b\u65b9\uff0c\u5373\u5b9a\u4e49\u5728\u5176\u4ed6\u6240\u6709\u51fd\u6570\u540e\u9762\u3002 \u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4e0d\u8fd4\u56de\u503c\u5219\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a \u4e00\u4e2a\u4f1a\u8fd4\u56de\u503c\u7684\u51fd\u6570\uff0c\u79f0\u4e3a\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570(value-returning function)\uff0c\u9664\u975e\u8fd4\u56de\u503c\u7c7b\u578b\u662f void \uff0c\u5426\u5219\u90fd\u5c5e\u4e8e\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u3002 \u4e00\u4e2a\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u5fc5\u987b\u8fd4\u56de\u5bf9\u5e94\u7c7b\u578b\u7684\u503c\uff08\u4f7f\u7528 return \u8bed\u53e5\uff09\uff0c\u5426\u5219\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u5728 1-6-Uninitialized-variables-and-undefined-behavior \u4e2d\u8ba8\u8bba\u4e86\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u51fd\u6570\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int getValueFromUser () // this function returns an integer value { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; // \u6ce8\u610f\uff1a\u6ca1\u6709 return \u8bed\u53e5 } int main () { int num { getValueFromUser () }; // initialize num with the return value of getValueFromUser() std :: cout << num << \" doubled is: \" << num * 2 << '\\n' ; return 0 ; } \u5728\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u73b0\u4ee3\u7f16\u8bd1\u5668\u4f1a\u4ea7\u751f\u9519\u8bef\u4fe1\u606f\uff0c\u56e0\u4e3a getValueFromUser \u5b9a\u4e49\u4e86 int \u7c7b\u578b\u7684\u8fd4\u56de\u503c\u4f46\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\u5305\u542b return \u8bed\u53e5\u3002\u7136\u800c\uff0c\u5982\u679c\u7a0b\u5e8f\u51fa\u4e8e\u67d0\u79cd\u539f\u56e0\u7f16\u8bd1\u901a\u8fc7\u4e86\uff0c\u90a3\u4e48\u5728\u8fd0\u884c\u4e0a\u8ff0\u7a0b\u5e8f\u65f6\u5c31\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u56e0\u4e3a getValueFromUser() \u662f\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\uff0c\u4f46\u5e76\u6ca1\u6709\u8fd4\u56de\u4e00\u4e2a\u503c\u3002 \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u4f60\u5fd8\u8bb0\u8fd4\u56de\u503c\uff0c\u7f16\u8bd1\u5668\u662f\u80fd\u591f\u68c0\u6d4b\u5230\u7684\u3002\u4f46\u662f\uff0c\u6709\u4e00\u4e9b\u975e\u5e38\u590d\u6742\u7684\u60c5\u51b5\uff0c\u7f16\u8bd1\u5668\u6709\u53ef\u80fd\u65e0\u6cd5\u786e\u5b9a\u51fd\u6570\u662f\u5426\u8fd4\u56de\u503c\u7684\u3002\u6240\u4ee5\u4f60\u4e0d\u5e94\u8be5\u4f9d\u8d56\u7f16\u8bd1\u5668\u3002 \u6700\u4f73\u5b9e\u8df5 \u786e\u4fdd\u975e void \u7c7b\u578b\u7684\u51fd\u6570\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u90fd\u8fd4\u56de\u503c\u3002 \u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4e0d\u8fd4\u56de\u503c\u5219\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 main \u51fd\u6570\u5982\u679c\u6ca1\u6709\u4f7f\u7528 return \u8bed\u53e5\u5219\u4f1a\u9690\u5f0f\u5730\u8fd4\u56de 0 \u4e0a\u8ff0\u539f\u5219\u6709\u4e00\u4e2a\u7279\u4f8b\uff0c\u90a3\u5c31\u662fmain\u51fd\u6570\u3002main \u51fd\u6570\u5982\u679c\u6ca1\u6709\u4f7f\u7528 return \u8bed\u53e5\u5219\u4f1a\u9690\u5f0f\u5730\u8fd4\u56de 0\u3002\u5373\u4fbf\u5982\u6b64\uff0c\u6700\u4f73\u5b9e\u8df5\u4ecd\u7136\u662f\u663e\u5f0f\u5730\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u8fd9\u6837\u65e2\u80fd\u591f\u8868\u660e\u4f60\u7684\u5b9e\u9645\u610f\u56fe\uff0c\u4e5f\u80fd\u591f\u5728\u5f62\u5f0f\u4e0a\u548c\u5176\u4ed6\u51fd\u6570\u4fdd\u6301\u4e00\u81f4(\u53ef\u4ee5\u9884\u9632\u4f60\u5ffd\u7565\u5176\u4ed6\u51fd\u6570\u7684\u8fd4\u56de\u503c)\u3002 \u51fd\u6570\u53ea\u80fd\u8fd4\u56de\u4e00\u4e2a\u5355\u503c \u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4f9d\u6b21\u53ea\u80fd\u8fd4\u56de\u4e00\u4e2a\u5355\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002 \u6ce8\u610f\uff0c return \u8bed\u53e5\u4e2d\u7684\u503c\u4e0d\u4e00\u5b9a\u8981\u7d20\u5b57\u9762\u91cf\u2014\u2014\u4e5f\u53ef\u4ee5\u662f\u4efb\u4f55\u8868\u8fbe\u5f0f\u7684\u503c\uff0c\u5305\u62ec\u53d8\u91cf\uff0c\u751a\u81f3\u53ef\u4ee5\u662f\u5176\u4ed6\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002 getValueFromUser() \u7684\u4f8b\u5b50\u4e2d\uff0c\u8fd4\u56de\u503c\u662f\u53d8\u91cf input \uff0c\u5176\u4e2d\u5305\u542b\u4e86\u7528\u6237\u8f93\u5165\u7684\u5b9e\u9645\u503c\u3002 \u901a\u8fc7\u4e00\u4e9b\u65b9\u6cd5\u662f\u53ef\u4ee5\u7ed5\u8fc7\u51fd\u6570\u53ea\u80fd\u8fd4\u56de\u5355\u4e00\u503c\u7684\u9650\u5236\u7684\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u51fd\u6570\u7684\u7f16\u5199\u8005\u53ef\u4ee5\u51b3\u5b9a\u8fd4\u56de\u503c\u7684\u542b\u4e49 \u51fd\u6570\u8fd4\u56de\u503c\u7684\u542b\u4e49\u662f\u7531\u51fd\u6570\u7f16\u5199\u8005\u51b3\u5b9a\u7684\u3002\u6709\u4e9b\u51fd\u6570\u4f1a\u5c06\u72b6\u6001\u7801\u4f5c\u4e3a\u8fd4\u56de\u503c\u6765\u8868\u793a\u51fd\u6570\u662f\u5426\u6267\u884c\u6210\u529f\u3002\u6709\u7684\u51fd\u6570\u5219\u5c06\u8ba1\u7b97\u7ed3\u679c\u6216\u5176\u4ed6\u9009\u5b9a\u7684\u503c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u8fd8\u6709\u4e9b\u51fd\u6570\u4ec0\u4e48\u90fd\u4e0d\u8fd4\u56de\uff08\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\u4e2d\u4ecb\u7ecd\u6b64\u7c7b\u51fd\u6570\uff09\u3002 \u56e0\u4e3a\u51fd\u6570\u8fd4\u56de\u503c\u7684\u542b\u4e49\u53ef\u80fd\u662f\u5404\u79cd\u5404\u6837\u7684\uff0c\u56e0\u6b64\u901a\u8fc7\u6ce8\u91ca\u7684\u65b9\u5f0f\u5bf9\u51fd\u6570\u8fd4\u56de\u503c\u7684\u542b\u4e49\u8fdb\u884c\u63cf\u8ff0\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 // \u51fd\u6570\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u503c // \u8fd4\u56de\u503c\u662f\u7528\u6237\u4ece\u952e\u76d8\u8f93\u5165\u7684\u503c\uff0c\u6574\u578b int getValueFromUser () { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; // \u5c06\u7528\u6237\u6570\u7ed3\u679c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570 } \u51fd\u6570\u91cd\u7528 \u63a5\u4e0b\u6765\uff0c\u4ecb\u7ecd\u51fd\u6570\u91cd\u7528\u7684\u5178\u578b\u4f8b\u5b50\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { int x {}; std :: cout << \"Enter an integer: \" ; std :: cin >> x ; int y {}; std :: cout << \"Enter an integer: \" ; std :: cin >> y ; std :: cout << x << \" + \" << y << \" = \" << x + y << '\\n' ; return 0 ; } \u867d\u7136\u8fd9\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\uff0c\u4f46\u662f\u4ee3\u7801\u6709\u4e9b\u5197\u4f59\u3002\u5b9e\u9645\u4e0a\u8fd9\u4e2a\u51fd\u6570\u8fdd\u53cd\u4e86\u597d\u4ee3\u7801\u7684\u6838\u5fc3\u4fe1\u6761\uff1a\u4e0d\u8981\u91cd\u590d\u4f60\u81ea\u5df1\u2014\u2014Don\u2019t Repeat Yourself (\u5e38\u5e38\u7f29\u5199\u4e3aDRY)\u3002 \u4e3a\u4ec0\u4e48\u91cd\u590d\u7684\u4ee3\u7801\u662f\u4e0d\u597d\u7684\uff1f\u5982\u679c\u4f60\u60f3\u8981\u5c06\u6587\u672c \u201cEnter an integer:\u201d \u4fee\u6539\u6210\u5176\u4ed6\u5185\u5bb9\uff0c\u90a3\u4e48\u4f60\u5fc5\u987b\u8981\u4fee\u6539\u4e24\u4e2a\u5730\u65b9\u3002\u5982\u679c\u8981\u521d\u59cb\u5316\u7684\u53d8\u91cf\u670910\u4e2a\u800c\u4e0d\u662f\u4e24\u4e2a\u5462\uff1f\u8fd9\u65e0\u7591\u5c06\u4f1a\u4ea7\u751f\u5927\u91cf\u7684\u5197\u4f59\u4ee3\u7801\uff08\u4f7f\u5f97\u7a0b\u5e8f\u53d8\u5f97\u53c8\u957f\u53c8\u96be\u4ee5\u7406\u89e3\uff09\uff0c\u8fd8\u4e3a\u62fc\u5199\u9519\u8bef\u63d0\u4f9b\u4e86\u6ecb\u751f\u7684\u7a7a\u95f4\u3002 \u4e0b\u9762\uff0c\u4f7f\u7528 getValueFromUser \u51fd\u6570\u6765\u91cd\u6784\u4e0a\u8ff0\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int getValueFromUser () { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; } int main () { int x { getValueFromUser () }; // first call to getValueFromUser int y { getValueFromUser () }; // second call to getValueFromUser std :: cout << x << \" + \" << y << \" = \" << x + y << '\\n' ; return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 Enter an integer: 5 Enter an integer: 7 5 + 7 = 12 \u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\uff0c getValueFromUser \u88ab\u8c03\u7528\u4e86\u91cf\u8bcd\uff0c\u4e00\u6b21\u7528\u4e8e\u521d\u59cb\u5316\u53d8\u91cf x \uff0c\u4e00\u6b21\u7528\u4e8e\u521d\u59cb\u5316\u53d8\u91cf y \u3002\u4f7f\u7528\u8be5\u51fd\u6570\u53ef\u4ee5\u6709\u6548\u5730\u907f\u514d\u91cd\u590d\u4ee3\u7801\uff0c\u4e5f\u51cf\u5c11\u4e86\u72af\u9519\u7684\u6982\u7387\u3002\u53ea\u8981 getValueFromUser \u51fd\u6570\u80fd\u591f\u6b63\u786e\u5de5\u4f5c\uff0c\u90a3\u4e48\u4fbf\u53ef\u4ee5\u591a\u6b21\u8c03\u7528\u5b83\u3002 \u8fd9\u4e5f\u662f\u6a21\u5757\u5316\u7f16\u7a0b\u7684\u7cbe\u9ad3\uff1a\u7f16\u5199\u51fd\u6570\u3001\u6d4b\u8bd5\u51fd\u6570\u4ee5\u786e\u4fdd\u5b83\u80fd\u591f\u6b63\u786e\u5de5\u4f5c\uff0c\u7136\u540e\u4fbf\u53ef\u4ee5\u5728\u63a5\u4e0b\u6765\u7684\u5de5\u4f5c\u4e2d\u53cd\u590d\u4f7f\u7528\u8be5\u51fd\u6570\uff08\u53ea\u8981\u6211\u4eec\u6ca1\u6709\u4fee\u6539\u8fd9\u4e2a\u51fd\u6570\u2014\u2014\u5426\u5219\u5fc5\u987b\u5bf9\u8be5\u51fd\u6570\u91cd\u65b0\u6d4b\u8bd5\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u8bf7\u9075\u5faaDRY\u539f\u5219\uff0c\u5373\u4e0d\u8981\"\u91cd\u590d\u4f60\u81ea\u5df1\u7684\u5de5\u4f5c\"\u3002\u5982\u679c\u6709\u4ef6\u4e8b\u9700\u8981\u505a\u591a\u6b21\uff0c\u8bf7\u8003\u8651\u662f\u5426\u53ef\u4ee5\u4fee\u6539\u4ee3\u7801\uff0c\u5c3d\u53ef\u80fd\u79fb\u9664\u5197\u4f59\u7684\u90e8\u5206\u3002\u53d8\u91cf\u53ef\u4ee5\u7528\u6765\u5b58\u653e\u88ab\u8ba1\u7b97\u51fa\u6765\u4e14\u9700\u8981\u591a\u6b21\u4f7f\u7528\u7684\u6570\u636e\uff08\u8fd9\u6837\u5c31\u4e0d\u9700\u8981\u8fdb\u884c\u91cd\u590d\u8ba1\u7b97\uff09\u3002\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u5b9a\u4e49\u4e00\u7ec4\u53ef\u80fd\u4f1a\u88ab\u591a\u6b21\u6267\u884c\u7684\u6307\u4ee4\u3002\u5faa\u73af\uff08\u540e\u9762\u7684\u7ae0\u8282\u4e2d\u4f1a\u4ecb\u7ecd\uff09\u5219\u53ef\u4ee5\u7528\u6765\u591a\u6b21\u6267\u884c\u8bed\u53e5\u3002 \u9898\u5916\u8bdd DRY \u7684\u53cd\u4e49\u8bcd\u662f WET (\u201cWrite everything twice\u201d\u2014\u2014\u4efb\u4f55\u4ee3\u7801\u90fd\u5199\u4e24\u904d).","title":"2.2 - \u51fd\u6570\u8fd4\u56de\u503c"},{"location":"2-2-Function-return-values-value-returning%20functions/#_1","text":"\u5728\u7f16\u5199\u51fd\u6570\u7684\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u786e\u5b9a\u8be5\u51fd\u6570\u662f\u5426\u5e94\u8be5\u8fd4\u56de\u4e00\u4e2a\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u5982\u679c\u9700\u8981\uff0c\u6709\u4e24\u4ef6\u4e8b\u9700\u8981\u53bb\u505a\u3002 \u9996\u5148\uff0c\u5fc5\u987b\u6307\u5b9a\u51fd\u6570\u8fd4\u56de\u503c\u7684\u7c7b\u578b\uff0c\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u7684\u5b9a\u4e49\u4f4d\u4e8e\u51fd\u6570\u540d\u524d\u9762\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c getValueFromUser \u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a void \uff08\u8868\u793a\u65e0\u9700\u8fd4\u56de\u4efb\u4f55\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\uff09\uff0c\u800c main \u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u5219\u4e3a int \uff08\u5373\u8fd4\u56de\u4e00\u4e2a int \u7c7b\u578b\u7684\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\uff09\u3002\u6ce8\u610f\uff0c\u8fd9\u91cc\u5e76\u4e0d\u662f\u5b9a\u4e49\u8fd4\u56de\u7684\u5177\u4f53\u662f\u4ec0\u4e48\u503c\uff0c\u800c\u4ec5\u662f\u6307\u5b9a\u5176 \u7c7b\u578b \u3002 \u76f8\u5173\u5185\u5bb9 \u8fd4\u56de\u503c\u4e3a void \u7c7b\u578b\u7684\u51fd\u6570\u5c06\u5728\u4e0b\u8282\u8bfe\u4e2d\u4ecb\u7ecd\uff08 2-3-Void-functions-non-value returning functions \uff09 \u5176\u6b21\uff0c\u5728\u9700\u8981\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4e2d\uff0c\u9700\u8981\u901a\u8fc7 return \u8bed\u53e5\u6765\u6307\u5b9a\u8981\u8fd4\u56de\u7684\u5177\u4f53\u503c\u3002\u8be5\u5177\u4f53\u503c\u79f0\u4e3a\u51fd\u6570\u7684 \u8fd4\u56de\u503c \u3002\u5f53 return \u8bed\u53e5\u88ab\u6267\u884c\u65f6\uff0c\u51fd\u6570\u4f1a\u7acb\u5373\u9000\u51fa\uff0c\u540c\u65f6\u5c06\u8fd4\u56de\u503c \u62f7\u8d1d \u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a \u6309\u503c\u8fd4\u56de \u3002 \u4ee5\u4e0b\u9762\u8fd9\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u4e3a\u4f8b\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include // int \u662f\u8fd4\u56de\u503c\u7c7b\u578b // \u8fd4\u56de\u503c\u7c7b\u578b\u4e3a int \u610f\u5473\u7740\u51fd\u6570\u4f1a\u8fd4\u56de\u67d0\u4e2a\u6574\u578b\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\uff08\u5177\u4f53\u662f\u4ec0\u4e48\u503c\u5e76\u4e0d\u4f1a\u5728\u6b64\u5904\u6307\u5b9a\uff09 int returnFive () { // return \u8bed\u53e5\u6307\u5b9a\u4e86\u8981\u8fd4\u56de\u7684\u5177\u4f53\u503c return 5 ; // \u8fd4\u56de 5 \u7ed9\u4e3b\u8c03\u51fd\u6570 } int main () { std :: cout << returnFive () << '\\n' ; // prints 5 std :: cout << returnFive () + 2 << '\\n' ; // prints 7 returnFive (); // \u51fd\u6570\u8fd4\u56de\u4e86 5\uff0c\u4f46\u662f\u8be5\u503c\u88ab\u5ffd\u7565\u4e86\uff0c\u56e0\u4e3a\u6ca1\u6709\u4f7f\u7528\u8be5\u503c\u505a\u4efb\u4f55\u4e8b\u60c5 return 0 ; } \u51fd\u6570\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 5 7 \u4ee3\u7801\u6267\u884c\u4ece main \u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\uff0c\u7b2c\u4e00\u6761\u8bed\u53e5\u5bf9 returnFive \u8fdb\u884c\u4e86\u6c42\u503c\uff0c\u5373\u8c03\u7528\u4e86\u8be5\u51fd\u6570\u3002\u51fd\u6570 returnFive \u8fd4\u56de\u503c\u4e3a 5\uff0c\u8be5\u503c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u540e\uff0c\u901a\u8fc7 std:: cout \u88ab\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002 \u5728\u7b2c\u4e8c\u5904\u51fd\u6570\u8c03\u7528\uff0c\u8bed\u53e5 returnFive \u8fdb\u884c\u4e86\u6c42\u503c\uff0c \u5176\u7ed3\u679c\u5c31\u662f returnFive \u51fd\u6570\u518d\u6b21\u88ab\u8c03\u7528\u3002\u51fd\u6570\u4ecd\u7136\u5c06\u8fd4\u56de\u503c 5 \u8fd4\u56de\u7ed9\u4e86\u4e3b\u8c03\u51fd\u6570\u3002\u8868\u8fbe\u5f0f 5 + 2 \u7684\u6c42\u503c\u7ed3\u679c\u662f 7\uff0c\u968f\u540e\u8be5\u503c\u901a\u8fc7 std:: cout \u88ab\u6253\u5370\u5230\u4e86\u63a7\u5236\u53f0\u3002 \u7b2c\u4e09\u6761\u8bed\u53e5\u518d\u6b21\u8c03\u7528\u4e86 returnFive \u51fd\u6570\uff0c\u5176\u8fd4\u56de\u503c\u4ecd\u7136\u88ab\u8fd4\u56de\u7ed9\u4e86\u4e3b\u8c03\u51fd\u6570\uff0c\u4f46\u662f main \u51fd\u6570\u5e76\u6ca1\u6709\u5bf9\u8be5\u8fd4\u56de\u503c\u8fdb\u884c\u4efb\u4f55\u64cd\u4f5c\uff0c\u56e0\u6b64\u4ec0\u4e48\u4e8b\u60c5\u90fd\u6ca1\u6709\u53d1\u751f\uff08\u8fd4\u56de\u503c\u88ab\u5ffd\u7565\u4e86\uff09\u3002 \u6ce8\u610f\uff1a\u9664\u975e\u4e3b\u8c03\u51fd\u6570\u901a\u8fc7 std:: cout \u5c06\u8fd4\u56de\u503c\u53d1\u9001\u5230\u63a7\u5236\u53f0\u8fdb\u884c\u6253\u5370\uff0c\u5426\u5219\u8be5\u503c\u5e76\u4e0d\u4f1a\u88ab\u6253\u5370\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6700\u540e\u4e00\u6b21\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u4e3b\u8c03\u51fd\u6570\u6ca1\u6709\u5c06\u8fd4\u56de\u503c\u53d1\u9001\u5230 std:: cout \uff0c\u56e0\u6b64\u5176\u7ed3\u679c\u6ca1\u6709\u6253\u5370\u3002 \u5c0f\u8d34\u58eb \u5f53\u88ab\u8c03\u51fd\u6570\u8fd4\u56de\u503c\u662f\uff0c\u4e3b\u8c03\u51fd\u6570\u53ef\u4ee5\u51b3\u5b9a\u662f\u5426\u5728\u8868\u8fbe\u5f0f\u6216\u8005\u8bed\u53e5\u4e2d\u4f7f\u7528\u8be5\u8fd4\u56de\u503c\uff08\u4f8b\u5982\uff1a\u5c06\u5176\u8d4b\u503c\u7ed9\u67d0\u4e2a\u53d8\u91cf\u6216\u5c06\u5176\u53d1\u9001\u5230 std:: cout \uff09\uff0c\u4e5f\u53ef\u4ee5\u5ffd\u7565\u5b83\uff08\u4e0d\u5bf9\u8fd4\u56de\u503c\u505a\u4efb\u4f55\u64cd\u4f5c\uff09\u3002","title":"\u8fd4\u56de\u503c"},{"location":"2-2-Function-return-values-value-returning%20functions/#_2","text":"\u6709\u4e86\u4e0a\u9762\u7684\u77e5\u8bc6\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u8bfe\u7a0b\u5f00\u59cb\u65f6\u7684\u7a0b\u5e8f\u8fdb\u884c\u9002\u5f53\u7684\u4fee\u6539\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int getValueFromUser () // this function now returns an integer value { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; // return the value the user entered back to the caller } int main () { int num { getValueFromUser () }; // \u4f7f\u7528 getValueFromUser() \u7684\u8fd4\u56de\u503c\u5bf9 num \u8fdb\u884c\u521d\u59cb\u5316 std :: cout << num << \" doubled is: \" << num * 2 << '\\n' ; return 0 ; } \u5f53\u4e0a\u8ff0\u7a0b\u5e8f\u5f00\u59cb\u6267\u884c\u65f6\uff0c main \u51fd\u6570\u4e2d\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u4f1a\u521b\u5efa\u4e00\u4e2a int \u7c7b\u578b\u7684\u53d8\u91cf num \u3002\u5f53\u7a0b\u5e8f\u51c6\u5907\u8981\u5bf9 num \u521d\u59cb\u5316\u65f6\uff0c\u5b83\u4f1a\u53d1\u73b0\u6b64\u5904\u6709\u4e00\u4e2a\u51fd\u6570\u8c03\u7528 getValueFromUser() \uff0c\u56e0\u6b64\u5b83\u4f1a\u8df3\u8f6c\u53bb\u6267\u884c\u8be5\u51fd\u6570\u3002\u51fd\u6570 getValueFromUser \u8981\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u503c\uff0c\u7136\u540e\u5b83\u5c06\u8be5\u8f93\u5165\u503c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570( main )\u3002\u8be5\u8fd4\u56de\u503c\u88ab\u7528\u6765\u5bf9\u53d8\u91cf num \u8fdb\u884c\u521d\u59cb\u5316\u3002 \u8bf7\u81ea\u884c\u7f16\u8bd1\u8be5\u51fd\u6570\u5e76\u8fd0\u884c\u5176\u51e0\u6b21\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u80fd\u591f\u6b63\u786e\u8fd0\u884c\u3002","title":"\u4fee\u6539\u7a0b\u5e8f"},{"location":"2-2-Function-return-values-value-returning%20functions/#_3","text":"\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u601d\u7ef4\u5de5\u5177\u6765\u7406\u89e3 main \u51fd\u6570\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u5f53\u7a0b\u5e8f\u6267\u884c\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u53d6\u8c03\u7528 main \u51fd\u6570\u3002\u968f\u540e\u7a0b\u5e8f\u5c31\u4f1a\u8df3\u8f6c\u5230 main \u51fd\u6570\u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\u3002\u51fd\u6570\u4f53\u4e2d\u7684\u8bed\u53e5\u4f1a\u987a\u5e8f\u6267\u884c\u3002\u6700\u7ec8\uff0c main \u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\u503c\uff08\u901a\u5e38\u662f 0\uff09\uff0c\u7136\u540e\u7a0b\u5e8f\u5c31\u7ec8\u6b62\u4e86\u3002 main \u51fd\u6570\u7684\u7684\u8fd4\u56de\u503c\u901a\u5e38\u79f0\u4e3a\u72b6\u6001\u7801\uff08\u6709\u65f6\u4e5f\u79f0\u4e3a\u9000\u51fa\u72b6\u6001\u7801\uff0c\u6781\u5c11\u6570\u60c5\u51b5\u4e0b\u4e5f\u79f0\u4e3a\u8fd4\u56de\u7801\uff09\uff0c\u56e0\u4e3a\u8be5\u8fd4\u56de\u503c\u7528\u6765\u53cd\u6620\u7a0b\u5e8f\u662f\u5426\u6b63\u786e\u6267\u884c\u3002 \u6839\u636e\u5b9a\u4e49\uff0c\u72b6\u6001\u7801\u4e3a 0 \u8868\u793a\u7a0b\u5e8f\u6267\u884c\u6210\u529f\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u7a0b\u5e8f\u6b63\u5e38\u8fd0\u884c\uff0c main \u7684\u8fd4\u56de\u503c\u5e94\u8be5\u4e3a 0 \u3002 \u975e 0 \u7684\u8fd4\u56de\u503c\u901a\u5e38\u7528\u6765\u8868\u793a\u7a0b\u5e8f\u6267\u884c\u5931\u8d25\uff08\u5c3d\u7ba1\u5927\u591a\u6570\u64cd\u4f5c\u7cfb\u7edf\u90fd\u6309\u7167\u4e0a\u8ff0\u7ea6\u5b9a\u6765\u5bf9\u5f85\u8fd4\u56de\u503c\uff0c\u4f46\u662f\u4e25\u683c\u6765\u8bb2\uff0c\u5176\u53ef\u79fb\u690d\u6027\u5e76\u4e0d\u80fd\u5f97\u5230\u4fdd\u8bc1\uff09\u3002 \u6269\u5c55\u9605\u8bfb C++\u6807\u51c6\u4e2d\u503c\u5b9a\u4e49\u4e86\u4e09\u79cd\u8fd4\u56de\u72b6\u6001\u7801\uff1a0\u3001 EXIT_SUCCESS \u548c EXIT_FAILURE \u30020 \u548c EXIT_SUCCESS \u90fd\u8868\u793a\u7a0b\u5e8f\u6267\u884c\u6210\u529f\u3002 EXIT_FAILURE \u5219\u8868\u793a\u7a0b\u5e8f\u6ca1\u6709\u6210\u529f\u6267\u884c\u3002 EXIT_SUCCESS \u548c EXIT_FAILURE \u5728 \u5934\u6587\u4ef6\u4e2d\u58f0\u660e\uff1a 1 2 3 4 5 6 #include // for EXIT_SUCCESS and EXIT_FAILURE int main () { return EXIT_SUCCESS ; } \u5982\u679c\u4f60\u60f3\u5c3d\u53ef\u80fd\u4fdd\u8bc1\u53ef\u79fb\u690d\u6027\uff0c\u8bf7\u4f7f\u7528 0 \u6216\u8005 EXIT_SUCCESS \u8868\u793a\u7a0b\u5e8f\u6210\u529f\u6267\u884c\uff0c\u6216\u8005\u4f7f\u7528 EXIT_FAILURE \u8868\u793a\u7a0b\u5e8f\u6267\u884c\u5931\u8d25\u3002 C++ \u4e0d\u5141\u8bb8\u663e\u5f0f\u8c03\u7528 main \u51fd\u6570\u3002 \u5c31\u76ee\u524d\u800c\u8a00\uff0c\u4f60\u8fd8\u5e94\u8be5\u5c06 main \u51fd\u6570\u5b9a\u4e49\u5728\u6587\u4ef6\u7684\u6700\u4e0b\u65b9\uff0c\u5373\u5b9a\u4e49\u5728\u5176\u4ed6\u6240\u6709\u51fd\u6570\u540e\u9762\u3002","title":"\u8fd4\u56de\u5230\u4e3b\u51fd\u6570"},{"location":"2-2-Function-return-values-value-returning%20functions/#_4","text":"\u4e00\u4e2a\u4f1a\u8fd4\u56de\u503c\u7684\u51fd\u6570\uff0c\u79f0\u4e3a\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570(value-returning function)\uff0c\u9664\u975e\u8fd4\u56de\u503c\u7c7b\u578b\u662f void \uff0c\u5426\u5219\u90fd\u5c5e\u4e8e\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u3002 \u4e00\u4e2a\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u5fc5\u987b\u8fd4\u56de\u5bf9\u5e94\u7c7b\u578b\u7684\u503c\uff08\u4f7f\u7528 return \u8bed\u53e5\uff09\uff0c\u5426\u5219\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u5728 1-6-Uninitialized-variables-and-undefined-behavior \u4e2d\u8ba8\u8bba\u4e86\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u51fd\u6570\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int getValueFromUser () // this function returns an integer value { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; // \u6ce8\u610f\uff1a\u6ca1\u6709 return \u8bed\u53e5 } int main () { int num { getValueFromUser () }; // initialize num with the return value of getValueFromUser() std :: cout << num << \" doubled is: \" << num * 2 << '\\n' ; return 0 ; } \u5728\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u73b0\u4ee3\u7f16\u8bd1\u5668\u4f1a\u4ea7\u751f\u9519\u8bef\u4fe1\u606f\uff0c\u56e0\u4e3a getValueFromUser \u5b9a\u4e49\u4e86 int \u7c7b\u578b\u7684\u8fd4\u56de\u503c\u4f46\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\u5305\u542b return \u8bed\u53e5\u3002\u7136\u800c\uff0c\u5982\u679c\u7a0b\u5e8f\u51fa\u4e8e\u67d0\u79cd\u539f\u56e0\u7f16\u8bd1\u901a\u8fc7\u4e86\uff0c\u90a3\u4e48\u5728\u8fd0\u884c\u4e0a\u8ff0\u7a0b\u5e8f\u65f6\u5c31\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u56e0\u4e3a getValueFromUser() \u662f\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\uff0c\u4f46\u5e76\u6ca1\u6709\u8fd4\u56de\u4e00\u4e2a\u503c\u3002 \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u4f60\u5fd8\u8bb0\u8fd4\u56de\u503c\uff0c\u7f16\u8bd1\u5668\u662f\u80fd\u591f\u68c0\u6d4b\u5230\u7684\u3002\u4f46\u662f\uff0c\u6709\u4e00\u4e9b\u975e\u5e38\u590d\u6742\u7684\u60c5\u51b5\uff0c\u7f16\u8bd1\u5668\u6709\u53ef\u80fd\u65e0\u6cd5\u786e\u5b9a\u51fd\u6570\u662f\u5426\u8fd4\u56de\u503c\u7684\u3002\u6240\u4ee5\u4f60\u4e0d\u5e94\u8be5\u4f9d\u8d56\u7f16\u8bd1\u5668\u3002 \u6700\u4f73\u5b9e\u8df5 \u786e\u4fdd\u975e void \u7c7b\u578b\u7684\u51fd\u6570\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u90fd\u8fd4\u56de\u503c\u3002 \u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4e0d\u8fd4\u56de\u503c\u5219\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u3002","title":"\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4e0d\u8fd4\u56de\u503c\u5219\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a"},{"location":"2-2-Function-return-values-value-returning%20functions/#main-return-0","text":"\u4e0a\u8ff0\u539f\u5219\u6709\u4e00\u4e2a\u7279\u4f8b\uff0c\u90a3\u5c31\u662fmain\u51fd\u6570\u3002main \u51fd\u6570\u5982\u679c\u6ca1\u6709\u4f7f\u7528 return \u8bed\u53e5\u5219\u4f1a\u9690\u5f0f\u5730\u8fd4\u56de 0\u3002\u5373\u4fbf\u5982\u6b64\uff0c\u6700\u4f73\u5b9e\u8df5\u4ecd\u7136\u662f\u663e\u5f0f\u5730\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u8fd9\u6837\u65e2\u80fd\u591f\u8868\u660e\u4f60\u7684\u5b9e\u9645\u610f\u56fe\uff0c\u4e5f\u80fd\u591f\u5728\u5f62\u5f0f\u4e0a\u548c\u5176\u4ed6\u51fd\u6570\u4fdd\u6301\u4e00\u81f4(\u53ef\u4ee5\u9884\u9632\u4f60\u5ffd\u7565\u5176\u4ed6\u51fd\u6570\u7684\u8fd4\u56de\u503c)\u3002","title":"main \u51fd\u6570\u5982\u679c\u6ca1\u6709\u4f7f\u7528 return \u8bed\u53e5\u5219\u4f1a\u9690\u5f0f\u5730\u8fd4\u56de 0"},{"location":"2-2-Function-return-values-value-returning%20functions/#_5","text":"\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4f9d\u6b21\u53ea\u80fd\u8fd4\u56de\u4e00\u4e2a\u5355\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002 \u6ce8\u610f\uff0c return \u8bed\u53e5\u4e2d\u7684\u503c\u4e0d\u4e00\u5b9a\u8981\u7d20\u5b57\u9762\u91cf\u2014\u2014\u4e5f\u53ef\u4ee5\u662f\u4efb\u4f55\u8868\u8fbe\u5f0f\u7684\u503c\uff0c\u5305\u62ec\u53d8\u91cf\uff0c\u751a\u81f3\u53ef\u4ee5\u662f\u5176\u4ed6\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002 getValueFromUser() \u7684\u4f8b\u5b50\u4e2d\uff0c\u8fd4\u56de\u503c\u662f\u53d8\u91cf input \uff0c\u5176\u4e2d\u5305\u542b\u4e86\u7528\u6237\u8f93\u5165\u7684\u5b9e\u9645\u503c\u3002 \u901a\u8fc7\u4e00\u4e9b\u65b9\u6cd5\u662f\u53ef\u4ee5\u7ed5\u8fc7\u51fd\u6570\u53ea\u80fd\u8fd4\u56de\u5355\u4e00\u503c\u7684\u9650\u5236\u7684\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"\u51fd\u6570\u53ea\u80fd\u8fd4\u56de\u4e00\u4e2a\u5355\u503c"},{"location":"2-2-Function-return-values-value-returning%20functions/#_6","text":"\u51fd\u6570\u8fd4\u56de\u503c\u7684\u542b\u4e49\u662f\u7531\u51fd\u6570\u7f16\u5199\u8005\u51b3\u5b9a\u7684\u3002\u6709\u4e9b\u51fd\u6570\u4f1a\u5c06\u72b6\u6001\u7801\u4f5c\u4e3a\u8fd4\u56de\u503c\u6765\u8868\u793a\u51fd\u6570\u662f\u5426\u6267\u884c\u6210\u529f\u3002\u6709\u7684\u51fd\u6570\u5219\u5c06\u8ba1\u7b97\u7ed3\u679c\u6216\u5176\u4ed6\u9009\u5b9a\u7684\u503c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u8fd8\u6709\u4e9b\u51fd\u6570\u4ec0\u4e48\u90fd\u4e0d\u8fd4\u56de\uff08\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\u4e2d\u4ecb\u7ecd\u6b64\u7c7b\u51fd\u6570\uff09\u3002 \u56e0\u4e3a\u51fd\u6570\u8fd4\u56de\u503c\u7684\u542b\u4e49\u53ef\u80fd\u662f\u5404\u79cd\u5404\u6837\u7684\uff0c\u56e0\u6b64\u901a\u8fc7\u6ce8\u91ca\u7684\u65b9\u5f0f\u5bf9\u51fd\u6570\u8fd4\u56de\u503c\u7684\u542b\u4e49\u8fdb\u884c\u63cf\u8ff0\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 // \u51fd\u6570\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u503c // \u8fd4\u56de\u503c\u662f\u7528\u6237\u4ece\u952e\u76d8\u8f93\u5165\u7684\u503c\uff0c\u6574\u578b int getValueFromUser () { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; // \u5c06\u7528\u6237\u6570\u7ed3\u679c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570 }","title":"\u51fd\u6570\u7684\u7f16\u5199\u8005\u53ef\u4ee5\u51b3\u5b9a\u8fd4\u56de\u503c\u7684\u542b\u4e49"},{"location":"2-2-Function-return-values-value-returning%20functions/#_7","text":"\u63a5\u4e0b\u6765\uff0c\u4ecb\u7ecd\u51fd\u6570\u91cd\u7528\u7684\u5178\u578b\u4f8b\u5b50\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { int x {}; std :: cout << \"Enter an integer: \" ; std :: cin >> x ; int y {}; std :: cout << \"Enter an integer: \" ; std :: cin >> y ; std :: cout << x << \" + \" << y << \" = \" << x + y << '\\n' ; return 0 ; } \u867d\u7136\u8fd9\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\uff0c\u4f46\u662f\u4ee3\u7801\u6709\u4e9b\u5197\u4f59\u3002\u5b9e\u9645\u4e0a\u8fd9\u4e2a\u51fd\u6570\u8fdd\u53cd\u4e86\u597d\u4ee3\u7801\u7684\u6838\u5fc3\u4fe1\u6761\uff1a\u4e0d\u8981\u91cd\u590d\u4f60\u81ea\u5df1\u2014\u2014Don\u2019t Repeat Yourself (\u5e38\u5e38\u7f29\u5199\u4e3aDRY)\u3002 \u4e3a\u4ec0\u4e48\u91cd\u590d\u7684\u4ee3\u7801\u662f\u4e0d\u597d\u7684\uff1f\u5982\u679c\u4f60\u60f3\u8981\u5c06\u6587\u672c \u201cEnter an integer:\u201d \u4fee\u6539\u6210\u5176\u4ed6\u5185\u5bb9\uff0c\u90a3\u4e48\u4f60\u5fc5\u987b\u8981\u4fee\u6539\u4e24\u4e2a\u5730\u65b9\u3002\u5982\u679c\u8981\u521d\u59cb\u5316\u7684\u53d8\u91cf\u670910\u4e2a\u800c\u4e0d\u662f\u4e24\u4e2a\u5462\uff1f\u8fd9\u65e0\u7591\u5c06\u4f1a\u4ea7\u751f\u5927\u91cf\u7684\u5197\u4f59\u4ee3\u7801\uff08\u4f7f\u5f97\u7a0b\u5e8f\u53d8\u5f97\u53c8\u957f\u53c8\u96be\u4ee5\u7406\u89e3\uff09\uff0c\u8fd8\u4e3a\u62fc\u5199\u9519\u8bef\u63d0\u4f9b\u4e86\u6ecb\u751f\u7684\u7a7a\u95f4\u3002 \u4e0b\u9762\uff0c\u4f7f\u7528 getValueFromUser \u51fd\u6570\u6765\u91cd\u6784\u4e0a\u8ff0\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int getValueFromUser () { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; } int main () { int x { getValueFromUser () }; // first call to getValueFromUser int y { getValueFromUser () }; // second call to getValueFromUser std :: cout << x << \" + \" << y << \" = \" << x + y << '\\n' ; return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 Enter an integer: 5 Enter an integer: 7 5 + 7 = 12 \u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\uff0c getValueFromUser \u88ab\u8c03\u7528\u4e86\u91cf\u8bcd\uff0c\u4e00\u6b21\u7528\u4e8e\u521d\u59cb\u5316\u53d8\u91cf x \uff0c\u4e00\u6b21\u7528\u4e8e\u521d\u59cb\u5316\u53d8\u91cf y \u3002\u4f7f\u7528\u8be5\u51fd\u6570\u53ef\u4ee5\u6709\u6548\u5730\u907f\u514d\u91cd\u590d\u4ee3\u7801\uff0c\u4e5f\u51cf\u5c11\u4e86\u72af\u9519\u7684\u6982\u7387\u3002\u53ea\u8981 getValueFromUser \u51fd\u6570\u80fd\u591f\u6b63\u786e\u5de5\u4f5c\uff0c\u90a3\u4e48\u4fbf\u53ef\u4ee5\u591a\u6b21\u8c03\u7528\u5b83\u3002 \u8fd9\u4e5f\u662f\u6a21\u5757\u5316\u7f16\u7a0b\u7684\u7cbe\u9ad3\uff1a\u7f16\u5199\u51fd\u6570\u3001\u6d4b\u8bd5\u51fd\u6570\u4ee5\u786e\u4fdd\u5b83\u80fd\u591f\u6b63\u786e\u5de5\u4f5c\uff0c\u7136\u540e\u4fbf\u53ef\u4ee5\u5728\u63a5\u4e0b\u6765\u7684\u5de5\u4f5c\u4e2d\u53cd\u590d\u4f7f\u7528\u8be5\u51fd\u6570\uff08\u53ea\u8981\u6211\u4eec\u6ca1\u6709\u4fee\u6539\u8fd9\u4e2a\u51fd\u6570\u2014\u2014\u5426\u5219\u5fc5\u987b\u5bf9\u8be5\u51fd\u6570\u91cd\u65b0\u6d4b\u8bd5\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u8bf7\u9075\u5faaDRY\u539f\u5219\uff0c\u5373\u4e0d\u8981\"\u91cd\u590d\u4f60\u81ea\u5df1\u7684\u5de5\u4f5c\"\u3002\u5982\u679c\u6709\u4ef6\u4e8b\u9700\u8981\u505a\u591a\u6b21\uff0c\u8bf7\u8003\u8651\u662f\u5426\u53ef\u4ee5\u4fee\u6539\u4ee3\u7801\uff0c\u5c3d\u53ef\u80fd\u79fb\u9664\u5197\u4f59\u7684\u90e8\u5206\u3002\u53d8\u91cf\u53ef\u4ee5\u7528\u6765\u5b58\u653e\u88ab\u8ba1\u7b97\u51fa\u6765\u4e14\u9700\u8981\u591a\u6b21\u4f7f\u7528\u7684\u6570\u636e\uff08\u8fd9\u6837\u5c31\u4e0d\u9700\u8981\u8fdb\u884c\u91cd\u590d\u8ba1\u7b97\uff09\u3002\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u5b9a\u4e49\u4e00\u7ec4\u53ef\u80fd\u4f1a\u88ab\u591a\u6b21\u6267\u884c\u7684\u6307\u4ee4\u3002\u5faa\u73af\uff08\u540e\u9762\u7684\u7ae0\u8282\u4e2d\u4f1a\u4ecb\u7ecd\uff09\u5219\u53ef\u4ee5\u7528\u6765\u591a\u6b21\u6267\u884c\u8bed\u53e5\u3002 \u9898\u5916\u8bdd DRY \u7684\u53cd\u4e49\u8bcd\u662f WET (\u201cWrite everything twice\u201d\u2014\u2014\u4efb\u4f55\u4ee3\u7801\u90fd\u5199\u4e24\u904d).","title":"\u51fd\u6570\u91cd\u7528"},{"location":"2-3-Void-functions-non-value%20returning%20functions/","tags":["void"],"text":"\u5728 2.1 - \u51fd\u6570\u7b80\u4ecb \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u51fd\u6570\u5b9a\u4e49\u7684\u8bed\u6cd5\u5e94\u8be5\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 return-type identifier() // identifier replaced with the name of your function { // Your code here } \u5c3d\u7ba1\u6211\u4eec\u5df2\u7ecf\u89c1\u8fc7\u4e00\u4e9b\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a void \u7684\u51fd\u6570\u7684\u4f8b\u5b50\u4e86\uff0c\u4f46\u662f\u8bfe\u7a0b\u4e2d\u5e76\u672a\u4ecb\u7ecd\u8be5\u7c7b\u578b\u7684\u5177\u4f53\u542b\u4e49\u3002\u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u4e3b\u8981\u7814\u7a76\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a void \u7684\u51fd\u6570\u3002 \u8fd4\u56de\u7a7a\u503c \u88ab\u8c03\u51fd\u6570\u65e0\u9700\u5411\u4e3b\u8c03\u51fd\u6570\u8fd4\u56de\u503c\uff0c\u800c\u4e3a\u4e86\u5c06\u8be5\u4fe1\u606f\u544a\u77e5\u7f16\u8bd1\u5668\uff0c\u5b9a\u4e49\u51fd\u6570\u65f6\u9700\u8981\u4f7f\u7528 void \u4f5c\u4e3a\u8fd4\u56de\u503c\u7c7b\u578b\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // void \u8868\u793a\u8be5\u51fd\u6570\u65e0\u9700\u8fd4\u56de\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570 void printHi () { std :: cout << \"Hi\" << '\\n' ; // \u51fd\u6570\u4e0d\u8fd4\u4f1a\u53ea\uff0c\u56e0\u6b64\u4e5f\u65e0\u9700 return \u8bed\u53e5 } int main () { printHi (); // okay: \u51fd\u6570 printHi() \u88ab\u8c03\u7528\uff0c\u65e0\u8fd4\u56de\u503c return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c printHi \u51fd\u6570\u5177\u6709\u5176\u7279\u5b9a\u7684\u529f\u80fd\uff08\u6253\u5370 \"Hi\"\uff09\uff0c\u4f46\u662f\u5b83\u4e0d\u5fc5\u5c06 \u4efb\u4f55\u503c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u56e0\u6b64\uff0c\u5728\u5b9a\u4e49 printHi \u51fd\u6570\u65f6\u4f7f\u7528\u4e86 void \u4f5c\u4e3a\u8fd4\u56de\u503c\u7c7b\u578b\u3002 \u5f53 main \u51fd\u6570\u8c03\u7528 printHi \u65f6\uff0c printHi \u51fd\u6570\u4f53\u4e2d\u7684\u4ee3\u7801\u4f1a\u88ab\u6267\u884c\uff0c\u201cHi\u201d \u88ab\u6253\u5370\u5728\u5c4f\u5e55\u4e0a\u3002\u968f\u540e\u63a7\u5236\u6743\u4ea4\u8fd8\u7ed9\u4e3b\u8c03\u51fd\u6570\uff0c\u7a0b\u5e8f\u7ee7\u7eed\u5411\u4e0b\u6267\u884c\u3002 \u4e0d\u8fd4\u8fd8\u4efb\u4f55\u503c\u7684\u51fd\u6570\uff0c\u79f0\u4e3a\u65e0\u8fd4\u56de\u503c\u51fd\u6570\uff08\u6216\u7a7a\u51fd\u6570\uff09\u3002 Void \u51fd\u6570\u65e0\u9700 return \u8bed\u53e5 \u7a7a\u51fd\u6570\u4f1a\u5728\u51fd\u6570\u7684\u672b\u5c3e\u81ea\u52a8\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570\uff0c\u65e0\u9700 return \u8bed\u53e5\u3002 \u4e0d\u5e26\u8fd4\u56de\u503c\u7684 return \u8bed\u53e5\u53ef\u4ee5\u88ab\u7528\u5728 void \u51fd\u6570\u4e2d\u2014\u2014\u8be5\u8bed\u53e5\u6267\u884c\u65f6\u4f1a\u5bfc\u81f4\u8be5\u51fd\u6570\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570\uff0c\u8fd9\u548c\u51fd\u6570\u8fd0\u884c\u5230\u7ed3\u5c3e\u65f6\u8fd4\u56de\u7684\u6548\u679c\u662f\u4e00\u6837\u7684\u3002\u56e0\u6b64\uff0c\u5c06\u4e00\u4e2a\u4e0d\u5e26\u8fd4\u56de\u503c\u7684 return \u8bed\u53e5\u653e\u5728\u51fd\u6570\u672b\u5c3e\u662f\u591a\u4f59\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // void means the function does not return a value to the caller void printHi () { std :: cout << \"Hi\" << '\\n' ; return ; // tell compiler to return to the caller -- this is redundant since this will happen anyway! } // function will return to caller here int main () { printHi (); return 0 ; } \u6700\u4f73\u5b9e\u8df5 \u4e0d\u8981\u5728\u4e0d\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u672b\u5c3e\u6dfb\u52a0 return \u8bed\u53e5\u3002 Void \u51fd\u6570\u4e0d\u80fd\u88ab\u7528\u5728\u9700\u8981\u503c\u7684\u8868\u8fbe\u5f0f \u6709\u4e9b\u7c7b\u578b\u7684\u8868\u8fbe\u5f0f\u662f\u9700\u8981\u4e00\u4e2a\u503c\u7684\uff0c\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 #include int main () { std :: cout << 5 ; // ok: 5 is a literal value that we're sending to the console to be printed std :: cout << ; // compile error: no value provided return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u9700\u8981\u6253\u5370\u7684\u5185\u5bb9\u5fc5\u987b\u4f4d\u4e8e std:: cout << \u7684\u53f3\u4fa7\uff0c\u5426\u5219\u7a0b\u5e8f\u5c06\u4f1a\u62a5\u9519\u3002\u56e0\u6b64\uff0c\u7b2c\u4e8c\u5904\u8c03\u7528 std:: cout \u7531\u4e8e\u6ca1\u6709\u53ef\u4ee5\u6253\u5370\u7684\u503c\uff0c\u7f16\u8bd1\u5668\u4f1a\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\u3002 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // void means the function does not return a value to the caller void printHi () { std :: cout << \"Hi\" << '\\n' ; } int main () { printHi (); // okay: function printHi() is called, no value is returned std :: cout << printHi (); // compile error return 0 ; } printHi() \u7b2c\u4e00\u6b21\u88ab\u8c03\u7528\u65f6\uff0c\u5176\u4e0a\u4e0b\u6587\u4e0d\u9700\u8981\u4e00\u4e2a\u503c\u3002\u56e0\u6b64\u8be5\u51fd\u6570\u4e0d\u8fd4\u56de\u503c\u662f\u6ca1\u6709\u4efb\u4f55\u95ee\u9898\u7684\u3002 \u8be5\u51fd\u6570\u7b2c\u4e8c\u6b21\u88ab\u8c03\u7528\u7684\u65f6\u5019\uff0c\u7a0b\u5e8f\u662f\u4e0d\u80fd\u6b63\u786e\u7f16\u8bd1\u7684\u3002\u51fd\u6570 printHi \u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a void \uff0c\u4e5f\u5c31\u662f\u8bf4\u8be5\u51fd\u6570\u4e0d\u5177\u6709\u8fd4\u56de\u503c\u3002\u7136\u800c\uff0c\u8be5\u8bed\u53e5\u5c1d\u8bd5\u5c06 printHi \u51fd\u6570\u7684\u8fd4\u56de\u503c\u8f93\u5165 std:: cout \u5e76\u6253\u5370\u3002 std:: cout \u4e0d\u77e5\u9053\u5982\u4f55\u5904\u7406\u8be5\u60c5\u51b5\uff08\u4ec0\u4e48\u503c\u9700\u8981\u88ab\u6253\u5370\uff1f\uff09\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u4f1a\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\u3002\u4f60\u5fc5\u987b\u628a\u8fd9\u4e00\u884c\u6ce8\u91ca\u6389\u624d\u80fd\u6b63\u5e38\u7f16\u8bd1\u8be5\u7a0b\u5e8f\u3002 \u5c0f\u8d34\u58eb \u6709\u4e9b\u8bed\u53e5\u9700\u8981\u503c\uff0c\u800c\u6709\u4e9b\u8bed\u53e5\u5219\u4e0d\u9700\u8981\u3002 \u5f53\u72ec\u7acb\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u7684\u65f6\u5019\uff08\u4f8b\u5982\u4e0a\u4f8b\u4e2d\u7b2c\u4e00\u6b21\u8c03\u7528 printHi() \u51fd\u6570\u65f6\uff09\uff0c\u6211\u4eec\u5173\u5fc3\u7684\u662f\u51fd\u6570\u672c\u8eab\u7684\u884c\u4e3a\uff08\u529f\u80fd\uff09\u800c\u4e0d\u662f\u5176\u8fd4\u56de\u503c\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6ca1\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4f46\u5ffd\u7565\u5176\u8fd4\u56de\u503c\u3002 \u53cd\u4e4b\uff0c\u5982\u679c\u6211\u4eec\u5728\u4e00\u4e2a\u9700\u8981\u503c\u7684\u4e0a\u4e0b\u6587\u4e2d\uff08\u4f8b\u5982 std:: cout \uff09\u8c03\u7528\u67d0\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u53ea\u80fd\u4f7f\u7528\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // Function that does not return a value void returnNothing () { } // Function that returns a value int returnFive () { return 5 ; } int main () { // When calling a function by itself, no value is required returnNothing (); // ok: we can call a function that does not return a value returnFive (); // ok: we can call a function that returns a value, and ignore that return value // When calling a function in a context that requires a value (like std::cout) std :: cout << returnFive (); // ok: we can call a function that returns a value, and the value will be used std :: cout << returnNothing (); // compile error: we can't call a function that returns void in this context return 0 ; } \u4ece void \u51fd\u6570\u4e2d\u8fd4\u56de\u503c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u62a5\u9519 \u5c1d\u8bd5\u5728\u4e00\u4e2a\u4e0d\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4e2d\u8fd4\u56de\u4e00\u4e2a\u503c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u62a5\u9519\uff1a 1 2 3 4 5 6 void printHi () // This function is non-value returning { std :: cout << \"In printHi()\" << '\\n' ; return 5 ; // compile error: we're trying to return a value } \u63d0\u524d\u8fd4\u56de \u5982\u679c\u4e00\u4e2a return \u8bed\u53e5\u51fa\u73b0\u5728\u6700\u540e\u4e00\u884c\u4ee3\u7801\u4e4b\u524d\uff0c\u5219\u79f0\u4e3a\u63d0\u524d\u8fd4\u56de\u3002\u8be5 return \u8bed\u53e5\u6267\u884c\u65f6\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u76f4\u63a5\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570\uff08\u56e0\u4e3a\u5229\u7528\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\u4f1a\u4ece\u6700\u540e\u4e00\u884c\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570\uff0c\u6240\u4ee5\u79f0\u4e3a\u201c\u63d0\u524d\u201d\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void print () // note: void return type { std :: cout << \"A\" ; return ; // \u51fd\u6570\u5c06\u5728\u8fd9\u884c\u8bed\u53e5\u6267\u884c\u65f6\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570(\u6ce8\u610f\uff1a\u6ca1\u6709\u8fd4\u56de\u503c) std :: cout << \"B\" ; // \u8be5\u884c\u6c38\u8fdc\u4e0d\u4f1a\u88ab\u6267\u884c } int main () { print (); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53 print() \u6267\u884c\u65f6\uff0c\u5b83\u4f1a\u9996\u5148\u6253\u5370 A\uff0c\u7136\u540e\u6267\u884c return \u8bed\u53e5\u5e76\u63a7\u5236\u51fd\u6570\u8fd4\u56de\u5176\u4e3b\u8c03\u51fd\u6570 main\u3002B \u6c38\u8fdc\u90fd\u4e0d\u4f1a\u88ab\u6253\u5370\u56e0\u4e3a\u8be5\u884c\u4ee3\u7801\u4e0d\u4f1a\u6267\u884c\u3002 \u63d0\u524d\u8fd4\u56de\u4e5f\u9002\u7528\u4e8e\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int print () // note: return type of int { std :: cout << \"A\" ; return 5 ; // the function will return to the caller here std :: cout << \"B\" ; // this will never be printed } int main () { std :: cout << print (); // print() returns value 5, which will be print to the console return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 A5 \u9996\u5148, print() \u88ab\u8c03\u7528\u3002 print() \u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u4f1a\u6253\u5370 \u201cA\u201d\u3002\u7136\u540e return \u8bed\u53e5\u88ab\u6267\u884c\uff0c\u8fd4\u56de\u503c 5 \u88ab\u4f20\u9012\u7ed9\u4e3b\u8c03\u51fd\u6570\uff0c\u8be5\u503c\u540c\u6837\u88ab\u6253\u5370\u4e86\u51fa\u6765\u3002 std:: cout << \"B\" \u6c38\u8fdc\u4e0d\u4f1a\u88ab\u6267\u884c\uff0c\u56e0\u4e3a\u5728\u6267\u884c\u5b83\u4e4b\u524d\uff0c\u51fd\u6570\u5df2\u7ecf\u8fd4\u56de\u4e86\u4e3b\u8c03\u51fd\u6570\u3002 \u5386\u53f2\u4e0a\uff0c\u63d0\u524d\u8fd4\u56de\u662f\u4e0d\u53d7\u9f13\u52b1\u7684\u884c\u4e3a\u3002\u4e0d\u8fc7\uff0c\u5728\u73b0\u4ee3\u7f16\u7a0b\u4e2d\uff0c\u5b83\u4eec\u662f\u53ef\u4ee5\u88ab\u63a5\u53d7\u7684\uff0c\u63d0\u524d\u8fd4\u56de\u80fd\u591f\u7b80\u5316\u7a0b\u5e8f\u65f6\uff0c\u6216\u5728\u53d1\u751f\u9519\u8bef\u65f6\u7ec8\u6b62\u51fd\u6570\u540e\u7eed\u7684\u64cd\u4f5c\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 7.10 - break \u548c continue \u4e2d\u8ba8\u8bba\u6709\u5173\u63d0\u524d\u8fd4\u56de\u7684\u4e89\u8bba\u3002","title":"2.3 - void \u51fd\u6570"},{"location":"2-3-Void-functions-non-value%20returning%20functions/#_1","text":"\u88ab\u8c03\u51fd\u6570\u65e0\u9700\u5411\u4e3b\u8c03\u51fd\u6570\u8fd4\u56de\u503c\uff0c\u800c\u4e3a\u4e86\u5c06\u8be5\u4fe1\u606f\u544a\u77e5\u7f16\u8bd1\u5668\uff0c\u5b9a\u4e49\u51fd\u6570\u65f6\u9700\u8981\u4f7f\u7528 void \u4f5c\u4e3a\u8fd4\u56de\u503c\u7c7b\u578b\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // void \u8868\u793a\u8be5\u51fd\u6570\u65e0\u9700\u8fd4\u56de\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570 void printHi () { std :: cout << \"Hi\" << '\\n' ; // \u51fd\u6570\u4e0d\u8fd4\u4f1a\u53ea\uff0c\u56e0\u6b64\u4e5f\u65e0\u9700 return \u8bed\u53e5 } int main () { printHi (); // okay: \u51fd\u6570 printHi() \u88ab\u8c03\u7528\uff0c\u65e0\u8fd4\u56de\u503c return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c printHi \u51fd\u6570\u5177\u6709\u5176\u7279\u5b9a\u7684\u529f\u80fd\uff08\u6253\u5370 \"Hi\"\uff09\uff0c\u4f46\u662f\u5b83\u4e0d\u5fc5\u5c06 \u4efb\u4f55\u503c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u56e0\u6b64\uff0c\u5728\u5b9a\u4e49 printHi \u51fd\u6570\u65f6\u4f7f\u7528\u4e86 void \u4f5c\u4e3a\u8fd4\u56de\u503c\u7c7b\u578b\u3002 \u5f53 main \u51fd\u6570\u8c03\u7528 printHi \u65f6\uff0c printHi \u51fd\u6570\u4f53\u4e2d\u7684\u4ee3\u7801\u4f1a\u88ab\u6267\u884c\uff0c\u201cHi\u201d \u88ab\u6253\u5370\u5728\u5c4f\u5e55\u4e0a\u3002\u968f\u540e\u63a7\u5236\u6743\u4ea4\u8fd8\u7ed9\u4e3b\u8c03\u51fd\u6570\uff0c\u7a0b\u5e8f\u7ee7\u7eed\u5411\u4e0b\u6267\u884c\u3002 \u4e0d\u8fd4\u8fd8\u4efb\u4f55\u503c\u7684\u51fd\u6570\uff0c\u79f0\u4e3a\u65e0\u8fd4\u56de\u503c\u51fd\u6570\uff08\u6216\u7a7a\u51fd\u6570\uff09\u3002","title":"\u8fd4\u56de\u7a7a\u503c"},{"location":"2-3-Void-functions-non-value%20returning%20functions/#void-return","text":"\u7a7a\u51fd\u6570\u4f1a\u5728\u51fd\u6570\u7684\u672b\u5c3e\u81ea\u52a8\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570\uff0c\u65e0\u9700 return \u8bed\u53e5\u3002 \u4e0d\u5e26\u8fd4\u56de\u503c\u7684 return \u8bed\u53e5\u53ef\u4ee5\u88ab\u7528\u5728 void \u51fd\u6570\u4e2d\u2014\u2014\u8be5\u8bed\u53e5\u6267\u884c\u65f6\u4f1a\u5bfc\u81f4\u8be5\u51fd\u6570\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570\uff0c\u8fd9\u548c\u51fd\u6570\u8fd0\u884c\u5230\u7ed3\u5c3e\u65f6\u8fd4\u56de\u7684\u6548\u679c\u662f\u4e00\u6837\u7684\u3002\u56e0\u6b64\uff0c\u5c06\u4e00\u4e2a\u4e0d\u5e26\u8fd4\u56de\u503c\u7684 return \u8bed\u53e5\u653e\u5728\u51fd\u6570\u672b\u5c3e\u662f\u591a\u4f59\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // void means the function does not return a value to the caller void printHi () { std :: cout << \"Hi\" << '\\n' ; return ; // tell compiler to return to the caller -- this is redundant since this will happen anyway! } // function will return to caller here int main () { printHi (); return 0 ; } \u6700\u4f73\u5b9e\u8df5 \u4e0d\u8981\u5728\u4e0d\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u672b\u5c3e\u6dfb\u52a0 return \u8bed\u53e5\u3002","title":"Void \u51fd\u6570\u65e0\u9700 return \u8bed\u53e5"},{"location":"2-3-Void-functions-non-value%20returning%20functions/#void","text":"\u6709\u4e9b\u7c7b\u578b\u7684\u8868\u8fbe\u5f0f\u662f\u9700\u8981\u4e00\u4e2a\u503c\u7684\uff0c\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 #include int main () { std :: cout << 5 ; // ok: 5 is a literal value that we're sending to the console to be printed std :: cout << ; // compile error: no value provided return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u9700\u8981\u6253\u5370\u7684\u5185\u5bb9\u5fc5\u987b\u4f4d\u4e8e std:: cout << \u7684\u53f3\u4fa7\uff0c\u5426\u5219\u7a0b\u5e8f\u5c06\u4f1a\u62a5\u9519\u3002\u56e0\u6b64\uff0c\u7b2c\u4e8c\u5904\u8c03\u7528 std:: cout \u7531\u4e8e\u6ca1\u6709\u53ef\u4ee5\u6253\u5370\u7684\u503c\uff0c\u7f16\u8bd1\u5668\u4f1a\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\u3002 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // void means the function does not return a value to the caller void printHi () { std :: cout << \"Hi\" << '\\n' ; } int main () { printHi (); // okay: function printHi() is called, no value is returned std :: cout << printHi (); // compile error return 0 ; } printHi() \u7b2c\u4e00\u6b21\u88ab\u8c03\u7528\u65f6\uff0c\u5176\u4e0a\u4e0b\u6587\u4e0d\u9700\u8981\u4e00\u4e2a\u503c\u3002\u56e0\u6b64\u8be5\u51fd\u6570\u4e0d\u8fd4\u56de\u503c\u662f\u6ca1\u6709\u4efb\u4f55\u95ee\u9898\u7684\u3002 \u8be5\u51fd\u6570\u7b2c\u4e8c\u6b21\u88ab\u8c03\u7528\u7684\u65f6\u5019\uff0c\u7a0b\u5e8f\u662f\u4e0d\u80fd\u6b63\u786e\u7f16\u8bd1\u7684\u3002\u51fd\u6570 printHi \u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a void \uff0c\u4e5f\u5c31\u662f\u8bf4\u8be5\u51fd\u6570\u4e0d\u5177\u6709\u8fd4\u56de\u503c\u3002\u7136\u800c\uff0c\u8be5\u8bed\u53e5\u5c1d\u8bd5\u5c06 printHi \u51fd\u6570\u7684\u8fd4\u56de\u503c\u8f93\u5165 std:: cout \u5e76\u6253\u5370\u3002 std:: cout \u4e0d\u77e5\u9053\u5982\u4f55\u5904\u7406\u8be5\u60c5\u51b5\uff08\u4ec0\u4e48\u503c\u9700\u8981\u88ab\u6253\u5370\uff1f\uff09\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u4f1a\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\u3002\u4f60\u5fc5\u987b\u628a\u8fd9\u4e00\u884c\u6ce8\u91ca\u6389\u624d\u80fd\u6b63\u5e38\u7f16\u8bd1\u8be5\u7a0b\u5e8f\u3002 \u5c0f\u8d34\u58eb \u6709\u4e9b\u8bed\u53e5\u9700\u8981\u503c\uff0c\u800c\u6709\u4e9b\u8bed\u53e5\u5219\u4e0d\u9700\u8981\u3002 \u5f53\u72ec\u7acb\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u7684\u65f6\u5019\uff08\u4f8b\u5982\u4e0a\u4f8b\u4e2d\u7b2c\u4e00\u6b21\u8c03\u7528 printHi() \u51fd\u6570\u65f6\uff09\uff0c\u6211\u4eec\u5173\u5fc3\u7684\u662f\u51fd\u6570\u672c\u8eab\u7684\u884c\u4e3a\uff08\u529f\u80fd\uff09\u800c\u4e0d\u662f\u5176\u8fd4\u56de\u503c\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6ca1\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4f46\u5ffd\u7565\u5176\u8fd4\u56de\u503c\u3002 \u53cd\u4e4b\uff0c\u5982\u679c\u6211\u4eec\u5728\u4e00\u4e2a\u9700\u8981\u503c\u7684\u4e0a\u4e0b\u6587\u4e2d\uff08\u4f8b\u5982 std:: cout \uff09\u8c03\u7528\u67d0\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u53ea\u80fd\u4f7f\u7528\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // Function that does not return a value void returnNothing () { } // Function that returns a value int returnFive () { return 5 ; } int main () { // When calling a function by itself, no value is required returnNothing (); // ok: we can call a function that does not return a value returnFive (); // ok: we can call a function that returns a value, and ignore that return value // When calling a function in a context that requires a value (like std::cout) std :: cout << returnFive (); // ok: we can call a function that returns a value, and the value will be used std :: cout << returnNothing (); // compile error: we can't call a function that returns void in this context return 0 ; }","title":"Void \u51fd\u6570\u4e0d\u80fd\u88ab\u7528\u5728\u9700\u8981\u503c\u7684\u8868\u8fbe\u5f0f"},{"location":"2-3-Void-functions-non-value%20returning%20functions/#void_1","text":"\u5c1d\u8bd5\u5728\u4e00\u4e2a\u4e0d\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4e2d\u8fd4\u56de\u4e00\u4e2a\u503c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u62a5\u9519\uff1a 1 2 3 4 5 6 void printHi () // This function is non-value returning { std :: cout << \"In printHi()\" << '\\n' ; return 5 ; // compile error: we're trying to return a value }","title":"\u4ece void \u51fd\u6570\u4e2d\u8fd4\u56de\u503c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u62a5\u9519"},{"location":"2-3-Void-functions-non-value%20returning%20functions/#_2","text":"\u5982\u679c\u4e00\u4e2a return \u8bed\u53e5\u51fa\u73b0\u5728\u6700\u540e\u4e00\u884c\u4ee3\u7801\u4e4b\u524d\uff0c\u5219\u79f0\u4e3a\u63d0\u524d\u8fd4\u56de\u3002\u8be5 return \u8bed\u53e5\u6267\u884c\u65f6\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u76f4\u63a5\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570\uff08\u56e0\u4e3a\u5229\u7528\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\u4f1a\u4ece\u6700\u540e\u4e00\u884c\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570\uff0c\u6240\u4ee5\u79f0\u4e3a\u201c\u63d0\u524d\u201d\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void print () // note: void return type { std :: cout << \"A\" ; return ; // \u51fd\u6570\u5c06\u5728\u8fd9\u884c\u8bed\u53e5\u6267\u884c\u65f6\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570(\u6ce8\u610f\uff1a\u6ca1\u6709\u8fd4\u56de\u503c) std :: cout << \"B\" ; // \u8be5\u884c\u6c38\u8fdc\u4e0d\u4f1a\u88ab\u6267\u884c } int main () { print (); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53 print() \u6267\u884c\u65f6\uff0c\u5b83\u4f1a\u9996\u5148\u6253\u5370 A\uff0c\u7136\u540e\u6267\u884c return \u8bed\u53e5\u5e76\u63a7\u5236\u51fd\u6570\u8fd4\u56de\u5176\u4e3b\u8c03\u51fd\u6570 main\u3002B \u6c38\u8fdc\u90fd\u4e0d\u4f1a\u88ab\u6253\u5370\u56e0\u4e3a\u8be5\u884c\u4ee3\u7801\u4e0d\u4f1a\u6267\u884c\u3002 \u63d0\u524d\u8fd4\u56de\u4e5f\u9002\u7528\u4e8e\u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int print () // note: return type of int { std :: cout << \"A\" ; return 5 ; // the function will return to the caller here std :: cout << \"B\" ; // this will never be printed } int main () { std :: cout << print (); // print() returns value 5, which will be print to the console return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 A5 \u9996\u5148, print() \u88ab\u8c03\u7528\u3002 print() \u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u4f1a\u6253\u5370 \u201cA\u201d\u3002\u7136\u540e return \u8bed\u53e5\u88ab\u6267\u884c\uff0c\u8fd4\u56de\u503c 5 \u88ab\u4f20\u9012\u7ed9\u4e3b\u8c03\u51fd\u6570\uff0c\u8be5\u503c\u540c\u6837\u88ab\u6253\u5370\u4e86\u51fa\u6765\u3002 std:: cout << \"B\" \u6c38\u8fdc\u4e0d\u4f1a\u88ab\u6267\u884c\uff0c\u56e0\u4e3a\u5728\u6267\u884c\u5b83\u4e4b\u524d\uff0c\u51fd\u6570\u5df2\u7ecf\u8fd4\u56de\u4e86\u4e3b\u8c03\u51fd\u6570\u3002 \u5386\u53f2\u4e0a\uff0c\u63d0\u524d\u8fd4\u56de\u662f\u4e0d\u53d7\u9f13\u52b1\u7684\u884c\u4e3a\u3002\u4e0d\u8fc7\uff0c\u5728\u73b0\u4ee3\u7f16\u7a0b\u4e2d\uff0c\u5b83\u4eec\u662f\u53ef\u4ee5\u88ab\u63a5\u53d7\u7684\uff0c\u63d0\u524d\u8fd4\u56de\u80fd\u591f\u7b80\u5316\u7a0b\u5e8f\u65f6\uff0c\u6216\u5728\u53d1\u751f\u9519\u8bef\u65f6\u7ec8\u6b62\u51fd\u6570\u540e\u7eed\u7684\u64cd\u4f5c\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 7.10 - break \u548c continue \u4e2d\u8ba8\u8bba\u6709\u5173\u63d0\u524d\u8fd4\u56de\u7684\u4e89\u8bba\u3002","title":"\u63d0\u524d\u8fd4\u56de"},{"location":"2-4-Introduction-to-function-parameters-and-arguments/","tags":["parameter","argument"],"text":"\u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4e86\u89e3\u5230\u51fd\u6570\u53ef\u4ee5\u5c06\u503c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\uff0c\u57fa\u4e8e\u6b64\uff0c\u6211\u4eec\u521b\u5efa\u4e86 getValueFromUser \u51fd\u6570\u5e76\u7528\u4e8e\u4e0b\u9762\u8fd9\u6bb5\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int getValueFromUser () { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; } int main () { int num { getValueFromUser () }; std :: cout << num << \" doubled is: \" << num * 2 << '\\n' ; return 0 ; } \u4e0d\u8fc7\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u5c06\u8f93\u51fa\u8bed\u53e5\u4e5f\u5c01\u88c5\u6210\u4e00\u4e2a\u51fd\u6570\u5462\uff1f\u4f60\u53ef\u80fd\u5e0c\u671b\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include int getValueFromUser () { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; } // \u8fd9\u4e2a\u51fd\u6570\u4e0d\u80fd\u7f16\u8bd1 void printDouble () { std :: cout << num << \" doubled is: \" << num * 2 << '\\n' ; } int main () { int num { getValueFromUser () }; printDouble (); return 0 ; } \u8fd9\u6bb5\u7a0b\u5e8f\u5e76\u4e0d\u80fd\u6b63\u786e\u7f16\u8bd1\uff0c\u56e0\u4e3a printDouble \u5e76\u4e0d\u77e5\u9053 num \u662f\u4ec0\u4e48\u3002\u6b64\u65f6\u4f60\u6216\u8bb8\u4f1a\u5728 printDouble() \u4e2d\u5b9a\u4e49 num \uff1a 1 2 3 4 5 void printDouble () { int num {}; // \u6dfb\u52a0\u6b64\u884c std :: cout << num << \" doubled is: \" << num * 2 << '\\n' ; } \u5c3d\u7ba1\u8fd9\u4e48\u505a\u53ef\u4ee5\u907f\u514d\u7f16\u8bd1\u5668\u62a5\u9519\u5e76\u4f7f\u5f97\u7a0b\u5e8f\u6210\u529f\u7f16\u8bd1\uff0c\u4f46\u8be5\u7a0b\u5e8f\u7684\u8fd0\u884c\u7ed3\u679c\u5e76\u4e0d\u6b63\u786e\uff08\u59cb\u7ec8\u4f1a\u6253\u5370\uff1a\u201c0 doubled is: 0\u201d\uff09\u3002\u8fd9\u4e2a\u95ee\u9898\u7684\u6839\u672c\u539f\u56e0\u662f printDouble \u65e0\u6cd5\u8bbf\u95ee\u7528\u6237\u7684\u8f93\u5165\u7ed3\u679c\u3002 \u6211\u4eec\u9700\u8981\u901a\u8fc7\u67d0\u79cd\u65b9\u5f0f\uff0c\u5c06\u53d8\u91cf\u4f20\u9012\u5230 printDouble \u51fd\u6570\u5185\u90e8\uff0c\u4f7f\u5176\u53ef\u4ee5\u5728\u51fd\u6570\u4f53\u5185\u4f7f\u7528\u8be5\u53d8\u91cf\u3002 \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2 \u5f88\u591a\u65f6\u5019\uff0c\u6211\u4eec\u5e0c\u671b\u80fd\u591f\u5728\u8c03\u7528\u51fd\u6570\u65f6\u5c06\u4fe1\u606f\u4e00\u5e76\u4f20\u5165\uff0c\u8fd9\u6837\u8be5\u51fd\u6570\u5c31\u53ef\u4ee5\u57fa\u4e8e\u6b64\u6570\u636e\u8fdb\u884c\u63a5\u4e0b\u6765\u7684\u5de5\u4f5c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u7f16\u5199\u4e00\u4e2a\u8ba1\u7b97\u4e24\u6570\u548c\u7684\u51fd\u6570\uff0c\u90a3\u4e48\u4f60\u5fc5\u987b\u6709\u529e\u6cd5\u544a\u8bc9\u5728\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u544a\u8bc9\u51fd\u6570\u9700\u8981\u76f8\u52a0\u7684\u4e24\u4e2a\u6570\u3002 \u5426\u5219\uff0c\u51fd\u6570\u5982\u4f55\u624d\u80fd\u77e5\u9053\u8981\u5c06\u4ec0\u4e48\u76f8\u52a0\u5462\uff1f\u901a\u8fc7\u51fd\u6570\u7684 \u5f62\u53c2 \u548c \u5b9e\u53c2 \u53ef\u4ee5\u5b8c\u6210\u4e0a\u8ff0\u5de5\u4f5c\u3002 \u51fd\u6570\u7684 \u5f62\u53c2 \u662f\u80fd\u591f\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u7684\u53d8\u91cf\u3002\u5f62\u53c2\u548c\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u51e0\u4e4e\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u9664\u4e86\uff1a\u5f62\u53c2\u603b\u662f\u7531\u4e3b\u8c03\u51fd\u6570\u521d\u59cb\u5316\u5e76\u63d0\u4f9b\u7ed9\u88ab\u8c03\u51fd\u6570\u3002 \u51fd\u6570\u7684\u5f62\u53c2\u662f\u5728\u51fd\u6570\u58f0\u660e\u65f6\u5b9a\u4e49\u7684\uff0c\u5b83\u4eec\u4f4d\u4e8e\u51fd\u6570\u540d\u540e\u7684\u62ec\u53f7\u4e2d\uff0c\u591a\u4e2a\u5f62\u53c2\u5219\u7531\u9017\u53f7\u9694\u5f00\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u5c55\u793a\u4e86\u5305\u542b\u4e0d\u540c\u6570\u91cf\u5f62\u53c2\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 // \u51fd\u6570\u6ca1\u6709\u53c2\u6570 // \u4e0d\u4f9d\u8d56\u4e0e\u4e3b\u8c03\u51fd\u6570\u63d0\u4f9b\u4efb\u4f55\u503c void doPrint () { std :: cout << \"In doPrint() \\n \" ; } // \u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u540d\u4e3a x \u7684\u6574\u578b\u53c2\u6570 // \u4e3b\u8c03\u51fd\u6570\u9700\u8981\u63d0\u4f9b x \u7684\u503c void printValue ( int x ) { std :: cout << x << '\\n' ; } // \u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u4e00\u4e2a\u540d\u4e3a x\uff0c\u4e00\u4e2a\u540d\u4e3a y // \u4e3b\u8c03\u51fd\u6570\u5fc5\u987b\u63d0\u4f9b x \u548c y \u7684\u503c int add ( int x , int y ) { return x + y ; } \u5b9e\u53c2 \u6307\u7684\u662f\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u4e3b\u8c03\u51fd\u6570\u4f20\u9012\u7ed9\u88ab\u8c03\u51fd\u6570\u7684\u503c\uff1a 1 2 3 doPrint (); // \u6ca1\u6709\u5b9e\u53c2 printValue ( 6 ); // 6 \u662f\u4f20\u9012\u7ed9 printValue() \u7684\u5b9e\u53c2 add ( 2 , 3 ); // 2 \u548c 3 \u662f\u4f20\u9012\u7ed9 add() \u7684\u5b9e\u53c2 \u6ce8\u610f\uff0c\u591a\u4e2a\u5b9e\u53c2\u4ecd\u7136\u7531\u9017\u53f7\u8fdb\u884c\u5206\u5272\u3002 \u5f62\u53c2\u548c\u5b9e\u53c2\u662f\u5982\u4f55\u534f\u540c\u5de5\u4f5c\u7684 \u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u6240\u6709\u7684\u5f62\u53c2\u90fd\u88ab\u5f53\u505a\u53d8\u91cf\u521b\u5efa\uff0c\u7136\u540e\u6240\u6709\u7684\u5b9e\u53c2\u90fd\u88ab\u62f7\u8d1d\u5230\u5bf9\u5e94\u7684\u5f62\u53c2\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a \u6309\u503c\u4f20\u9012 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // This function has two integer parameters, one named x, and one named y // The values of x and y are passed in by the caller void printValues ( int x , int y ) { std :: cout << x << '\\n' ; std :: cout << y << '\\n' ; } int main () { printValues ( 6 , 7 ); // This function call has two arguments, 6 and 7 return 0 ; } \u5f53 printValues \u51fd\u6570\u4f7f\u7528\u5b9e\u53c2 6 \u548c 7 \u8fdb\u884c\u8c03\u7528\u65f6\uff0c\u5f62\u53c2 x \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 6\uff0c\u5f62\u53c2 y \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 7\u3002 \u4e0a\u8ff0\u4ee3\u7801\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 6 7 \u6ce8\u610f\uff0c\u5b9e\u53c2\u7684\u4e2a\u6570\u5fc5\u987b\u548c\u5f62\u53c2\u7684\u4e2a\u6570\u4fdd\u6301\u4e00\u81f4\uff0c\u5426\u5219\u7f16\u8bd1\u5668\u5c31\u4f1a\u62a5\u9519\u3002\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u5b9e\u53c2\u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u7684\u8868\u8fbe\u5f0f\uff08\u56e0\u4e3a\u5b9e\u53c2\u672c\u8d28\u4e0a\u53ea\u662f\u5f62\u53c2\u7684\u521d\u59cb\u5316\u503c\uff0c\u800c\u521d\u59cb\u5316\u503c\u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u8868\u8fbe\u5f0f\uff09\u3002 \u4fee\u6b63\u9519\u8bef\u7684\u7a0b\u5e8f \u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u4fee\u6b63\u672c\u8282\u8bfe\u5f00\u5934\u7684\u7a0b\u5e8f\u4e86\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include int getValueFromUser () { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; } void printDouble ( int value ) // \u51fd\u6570\u73b0\u5728\u6709\u4e00\u4e2a\u6574\u578b\u7684\u5f62\u53c2 { std :: cout << value << \" doubled is: \" << value * 2 << '\\n' ; } int main () { int num { getValueFromUser () }; printDouble ( num ); return 0 ; } \u5728\u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\uff0c\u53d8\u91cf num \u9996\u5148\u88ab\u521d\u59cb\u5316\u4e3a\u7528\u6237\u8f93\u5165\u7684\u503c\u3002\u7136\u540e\u51fd\u6570 printDouble \u88ab\u8c03\u7528\uff0c\u5b9e\u53c2 num \u88ab\u62f7\u8d1d\u5230\u5f62\u53c2\u3002\u51fd\u6570 printDouble \u968f\u540e\u5c31\u53ef\u4ee5\u4f7f\u7528\u8be5\u5f62\u53c2\u7684\u503c\u3002 \u4f7f\u7528\u8fd4\u56de\u503c\u4f5c\u4e3a\u5b9e\u53c2 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u53d8\u91cf num \u53ea\u88ab\u4f7f\u7528\u4e86\u4e00\u6b21\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06 getValueFromUser \u7684\u8fd4\u56de\u503c\u4f5c\u4e3a printDouble \u7684\u5b9e\u53c2\uff0c\u5c06\u4e0a\u9762\u7684\u4ee3\u7801\u7a0d\u52a0\u7b80\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int getValueFromUser () { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; } void printDouble ( int value ) { std :: cout << value << \" doubled is: \" << value * 2 << '\\n' ; } int main () { printDouble ( getValueFromUser ()); return 0 ; } \u73b0\u5728\uff0c\u6211\u4eec\u4f7f\u7528 getValueFromUser \u7684\u8fd4\u56de\u503c\u4f5c\u4e3a\u4e86 printDouble \u7684\u5b9e\u53c2\u3002 \u5c3d\u7ba1\u8fd9\u4e2a\u7a0b\u5e8f\u53d8\u5f97\u66f4\u52a0\u7cbe\u7b80\u4e86\uff08\u800c\u4e14\u4e5f\u5f88\u6e05\u695a\u7684\u8868\u660e\u7528\u6237\u7684\u8f93\u5165\u4ec5\u4ec5\u88ab\u7528\u4e8e\u8fdb\u884c\u8ba1\u7b97\uff09\uff0c\u4f46\u662f\u4f60\u4e5f\u8bb8\u4e5f\u4f1a\u51b3\u5b9a\u8fd9\u79cd\u201c\u538b\u7f29\u8bed\u6cd5\u201d\u53ef\u8bfb\u6027\u5e76\u4e0d\u4f1a\u3002\u5982\u679c\u4f60\u89c9\u5f97\u4e4b\u524d\u7684\u7248\u672c\u66f4\u52a0\u6613\u8bfb\uff0c\u90a3\u4e5f\u662f\u53ef\u4ee5\u7684\u3002 \u5f62\u53c2\u548c\u8fd4\u56de\u503c\u662f\u5982\u4f55\u534f\u540c\u5de5\u4f5c\u7684 \u901a\u8fc7\u4e00\u8d77\u4f7f\u7528\u5f62\u53c2\u548c\u8fd4\u56de\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u63a5\u53d7\u67d0\u4e9b\u6570\u636e\uff0c\u5bf9\u6570\u636e\u8fdb\u884c\u8ba1\u7b97\u5e76\u901a\u8fc7\u8fd4\u56de\u503c\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002 \u4e0b\u9762\u7684\u4ee3\u7801\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u51fd\u6570\u8ba1\u7b97\u4e24\u4e2a\u503c\u7684\u548c\u5e76\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // add() takes two integers as parameters, and returns the result of their sum // The values of x and y are determined by the function that calls add() int add ( int x , int y ) { return x + y ; } // main takes no parameters int main () { std :: cout << add ( 4 , 5 ) << '\\n' ; // Arguments 4 and 5 are passed to function add() return 0 ; } \u7a0b\u5e8f\u4ece main \u51fd\u6570\u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\uff0c\u5f53 add(4,5) \u8bed\u53e5\u6c42\u503c\u65f6\uff0c\u51fd\u6570 add \u88ab\u8c03\u7528\uff0c\u5176\u5f62\u53c2 x \u88ab\u521d\u59cb\u5316\u4e3a 4\uff0c\u5f62\u53c2 y \u88ab\u521d\u59cb\u5316\u4e3a 5\u3002 \u51fd\u6570\u7684 return \u8bed\u53e5\u5bf9 x+y \u8fdb\u884c\u4e86\u6c42\u503c\u5e76\u5f97\u5230\u7ed3\u679c 9\uff0c\u8be5\u503c\u968f\u540e\u88ab\u8fd4\u56de\u7ed9 main \u51fd\u6570\u3002\u7136\u540e 9 \u5c31\u88ab\u8f93\u5165 std::cout \u5e76\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002 \u8f93\u51fa\u7ed3\u679c\uff1a 1 9 \u56fe\u89e3\uff1a \u66f4\u591a\u4f8b\u5b50 \u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u66f4\u591a\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include int add ( int x , int y ) { return x + y ; } int multiply ( int z , int w ) { return z * w ; } int main () { std :: cout << add ( 4 , 5 ) << '\\n' ; // within add() x=4, y=5, so x+y=9 std :: cout << add ( 1 + 2 , 3 * 4 ) << '\\n' ; // within add() x=3, y=12, so x+y=15 int a { 5 }; std :: cout << add ( a , a ) << '\\n' ; // evaluates (5 + 5) std :: cout << add ( 1 , multiply ( 2 , 3 )) << '\\n' ; // evaluates 1 + (2 * 3) std :: cout << add ( 1 , add ( 2 , 3 )) << '\\n' ; // evaluates 1 + (2 + 3) return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 9 15 10 7 6 \u7b2c\u4e00\u884c\u8bed\u53e5\u975e\u5e38\u76f4\u767d\u3002 \u7b2c\u4e8c\u884c\u8bed\u53e5\uff0c\u4f5c\u4e3a\u5b9e\u53c2\u7684\u8868\u8fbe\u5f0f\u5728\u4f20\u9012\u524d\u9996\u5148\u8fdb\u884c\u6c42\u503c\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c 1 + 2 \u6c42\u503c\u5f97\u5230 3 \uff0c\u6240\u4ee5 3 \u500d\u62f7\u8d1d\u5230\u5f62\u53c2 x\u3002 3 * 4 \u6c42\u503c\u5f97\u5230 12 \uff0c\u56e0\u6b64 12 \u88ab\u62f7\u8d1d\u5230\u5f62\u53c2 y\u3002 add(3,12) \u7684\u7ed3\u679c\u4e3a 15 \u3002 \u63a5\u4e0b\u6765\u7684\u4e24\u884c\u8bed\u53e5\u4e5f\u6bd4\u8f83\u7b80\u5355\uff1a 1 2 int a { 5 }; std :: cout << add ( a , a ) << '\\n' ; // evaluates (5 + 5) \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c add() \u53d8\u91cf a \u88ab\u62f7\u8d1d\u5230\u5f62\u53c2 x \u548c y\u3002\u7531\u4e8e a \u7684\u503c\u4e3a 5\uff0c\u5219 add(a, a) = add(5,5) \uff0c\u5176\u8ba1\u7b97\u7ed3\u679c\u4e3a 10\u3002 \u63a5\u4e0b\u6765\u662f\u8fd9\u4e00\u7cfb\u5217\u8bed\u53e5\u4e2d\u6700\u590d\u6742\u7684\u4e00\u6761\uff1a 1 std :: cout << add ( 1 , multiply ( 2 , 3 )) << '\\n' ; // evaluates 1 + (2 * 3) \u5f53\u51fd\u6570 add \u6267\u884c\u65f6\uff0c \u7a0b\u5e8f\u5fc5\u987b\u8981\u786e\u5b9a\u5176\u5f62\u53c2 x \u548c y\u3002x \u5f88\u7b80\u5355\uff0c\u56e0\u4e3a\u4f20\u9012\u7ed9\u5b83\u7684\u503c\u5c31\u662f 1\u3002\u800c\u4e3a\u4e86\u83b7\u53d6\u5f62\u53c2 y \u7684\u503c\uff0c\u7a0b\u5e8f\u5fc5\u987b\u8981\u5bf9 multiply(2,3) \u8fdb\u884c\u6c42\u503c\u3002\u4e3a\u6b64\uff0c\u7a0b\u5e8f\u9996\u5148\u8c03\u7528 multiply \u51fd\u6570\u5e76\u5c06 z \u521d\u59cb\u5316\u4e3a 2\uff0cw \u521d\u59cb\u5316\u4e3a 3\uff0c\u56e0\u6b64 multiply(2,3) \u8fd4\u56de\u503c\u4e3a 6\u3002\u8be5\u503c\u968f\u540e\u88ab\u7528\u6765\u521d\u59cb\u5316 add \u51fd\u6570\u7684\u5f62\u53c2 y\u3002 add(1,6) \u7684\u8fd4\u56de\u503c\u4e3a 7\uff0c\u8be5\u503c\u88ab\u4f20\u9012\u7ed9 std:: cout \u7528\u4e8e\u6253\u5370\u3002 \u7b80\u5355\u6765\u8bf4\uff1a add(1, multiply(2,3)) \u6c42\u503c\u5f97\u5230 add(1,6) \uff0c\u8fdb\u800c\u6c42\u503c\u5f97\u5230 7 \u4e0b\u9762\u7684\u8bed\u53e5\u770b\u4e0a\u53bb\u6709\u70b9\u7ed5\uff0c\u56e0\u4e3a add \u51fd\u6570\u7684\u4e00\u4e2a\u5b9e\u53c2\uff0c\u5176\u503c\u6765\u81ea\u4e8e\u53e6\u5916\u4e00\u4e2a add \u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002 1 std :: cout << add ( 1 , add ( 2 , 3 )) << '\\n' ; // evaluates 1 + (2 + 3) \u4e0d\u8fc7\uff0c\u5176\u5b9e\u5b83\u548c\u524d\u4e00\u4e2a\u4f8b\u5b50\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c add(2,3) \u9996\u5148\u6c42\u503c\uff0c\u8fd4\u56de\u503c\u4e3a 5\u3002\u63a5\u4e0b\u6765\uff0c\u5bf9 add(1,5) \u8fdb\u884c\u6c42\u503c\uff0c\u5176\u7ed3\u679c\u4e3a 6\uff0c\u8be5\u503c\u88ab\u4f20\u9012\u7ed9 std::cout \u7528\u4e8e\u6253\u5370\u3002 \u7b80\u5355\u6765\u8bf4\uff1a add(1, add(2,3)) \u6c42\u503c\u5f97\u5230 add(1,5) \uff0c\u7ee7\u7eed\u6c42\u503c\u5f97\u5230 6\u3002 \u7ed3\u8bba \u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\u662f\u51fd\u6570\u80fd\u591f\u88ab\u91cd\u7528\u7684\u5173\u952e\u673a\u5236\uff0c\u56e0\u4e3a\u8be5\u673a\u5236\u53ef\u4ee5\u5141\u8bb8\u6211\u4eec\u5728\u4e0d\u6307\u5b9a\u5177\u4f53\u503c\u7684\u60c5\u51b5\u4e0b\u7f16\u5199\u51fd\u6570\u83b7\u53d6\u6216\u8ba1\u7b97\u6570\u636e\u5e76\u5c06\u8fd4\u56de\u503c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002","title":"2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2"},{"location":"2-4-Introduction-to-function-parameters-and-arguments/#_1","text":"\u5f88\u591a\u65f6\u5019\uff0c\u6211\u4eec\u5e0c\u671b\u80fd\u591f\u5728\u8c03\u7528\u51fd\u6570\u65f6\u5c06\u4fe1\u606f\u4e00\u5e76\u4f20\u5165\uff0c\u8fd9\u6837\u8be5\u51fd\u6570\u5c31\u53ef\u4ee5\u57fa\u4e8e\u6b64\u6570\u636e\u8fdb\u884c\u63a5\u4e0b\u6765\u7684\u5de5\u4f5c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u7f16\u5199\u4e00\u4e2a\u8ba1\u7b97\u4e24\u6570\u548c\u7684\u51fd\u6570\uff0c\u90a3\u4e48\u4f60\u5fc5\u987b\u6709\u529e\u6cd5\u544a\u8bc9\u5728\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u544a\u8bc9\u51fd\u6570\u9700\u8981\u76f8\u52a0\u7684\u4e24\u4e2a\u6570\u3002 \u5426\u5219\uff0c\u51fd\u6570\u5982\u4f55\u624d\u80fd\u77e5\u9053\u8981\u5c06\u4ec0\u4e48\u76f8\u52a0\u5462\uff1f\u901a\u8fc7\u51fd\u6570\u7684 \u5f62\u53c2 \u548c \u5b9e\u53c2 \u53ef\u4ee5\u5b8c\u6210\u4e0a\u8ff0\u5de5\u4f5c\u3002 \u51fd\u6570\u7684 \u5f62\u53c2 \u662f\u80fd\u591f\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u7684\u53d8\u91cf\u3002\u5f62\u53c2\u548c\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u51e0\u4e4e\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u9664\u4e86\uff1a\u5f62\u53c2\u603b\u662f\u7531\u4e3b\u8c03\u51fd\u6570\u521d\u59cb\u5316\u5e76\u63d0\u4f9b\u7ed9\u88ab\u8c03\u51fd\u6570\u3002 \u51fd\u6570\u7684\u5f62\u53c2\u662f\u5728\u51fd\u6570\u58f0\u660e\u65f6\u5b9a\u4e49\u7684\uff0c\u5b83\u4eec\u4f4d\u4e8e\u51fd\u6570\u540d\u540e\u7684\u62ec\u53f7\u4e2d\uff0c\u591a\u4e2a\u5f62\u53c2\u5219\u7531\u9017\u53f7\u9694\u5f00\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u5c55\u793a\u4e86\u5305\u542b\u4e0d\u540c\u6570\u91cf\u5f62\u53c2\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 // \u51fd\u6570\u6ca1\u6709\u53c2\u6570 // \u4e0d\u4f9d\u8d56\u4e0e\u4e3b\u8c03\u51fd\u6570\u63d0\u4f9b\u4efb\u4f55\u503c void doPrint () { std :: cout << \"In doPrint() \\n \" ; } // \u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u540d\u4e3a x \u7684\u6574\u578b\u53c2\u6570 // \u4e3b\u8c03\u51fd\u6570\u9700\u8981\u63d0\u4f9b x \u7684\u503c void printValue ( int x ) { std :: cout << x << '\\n' ; } // \u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u4e00\u4e2a\u540d\u4e3a x\uff0c\u4e00\u4e2a\u540d\u4e3a y // \u4e3b\u8c03\u51fd\u6570\u5fc5\u987b\u63d0\u4f9b x \u548c y \u7684\u503c int add ( int x , int y ) { return x + y ; } \u5b9e\u53c2 \u6307\u7684\u662f\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u4e3b\u8c03\u51fd\u6570\u4f20\u9012\u7ed9\u88ab\u8c03\u51fd\u6570\u7684\u503c\uff1a 1 2 3 doPrint (); // \u6ca1\u6709\u5b9e\u53c2 printValue ( 6 ); // 6 \u662f\u4f20\u9012\u7ed9 printValue() \u7684\u5b9e\u53c2 add ( 2 , 3 ); // 2 \u548c 3 \u662f\u4f20\u9012\u7ed9 add() \u7684\u5b9e\u53c2 \u6ce8\u610f\uff0c\u591a\u4e2a\u5b9e\u53c2\u4ecd\u7136\u7531\u9017\u53f7\u8fdb\u884c\u5206\u5272\u3002","title":"\u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2"},{"location":"2-4-Introduction-to-function-parameters-and-arguments/#_2","text":"\u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u6240\u6709\u7684\u5f62\u53c2\u90fd\u88ab\u5f53\u505a\u53d8\u91cf\u521b\u5efa\uff0c\u7136\u540e\u6240\u6709\u7684\u5b9e\u53c2\u90fd\u88ab\u62f7\u8d1d\u5230\u5bf9\u5e94\u7684\u5f62\u53c2\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a \u6309\u503c\u4f20\u9012 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // This function has two integer parameters, one named x, and one named y // The values of x and y are passed in by the caller void printValues ( int x , int y ) { std :: cout << x << '\\n' ; std :: cout << y << '\\n' ; } int main () { printValues ( 6 , 7 ); // This function call has two arguments, 6 and 7 return 0 ; } \u5f53 printValues \u51fd\u6570\u4f7f\u7528\u5b9e\u53c2 6 \u548c 7 \u8fdb\u884c\u8c03\u7528\u65f6\uff0c\u5f62\u53c2 x \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 6\uff0c\u5f62\u53c2 y \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 7\u3002 \u4e0a\u8ff0\u4ee3\u7801\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 6 7 \u6ce8\u610f\uff0c\u5b9e\u53c2\u7684\u4e2a\u6570\u5fc5\u987b\u548c\u5f62\u53c2\u7684\u4e2a\u6570\u4fdd\u6301\u4e00\u81f4\uff0c\u5426\u5219\u7f16\u8bd1\u5668\u5c31\u4f1a\u62a5\u9519\u3002\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u5b9e\u53c2\u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u7684\u8868\u8fbe\u5f0f\uff08\u56e0\u4e3a\u5b9e\u53c2\u672c\u8d28\u4e0a\u53ea\u662f\u5f62\u53c2\u7684\u521d\u59cb\u5316\u503c\uff0c\u800c\u521d\u59cb\u5316\u503c\u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u8868\u8fbe\u5f0f\uff09\u3002","title":"\u5f62\u53c2\u548c\u5b9e\u53c2\u662f\u5982\u4f55\u534f\u540c\u5de5\u4f5c\u7684"},{"location":"2-4-Introduction-to-function-parameters-and-arguments/#_3","text":"\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u4fee\u6b63\u672c\u8282\u8bfe\u5f00\u5934\u7684\u7a0b\u5e8f\u4e86\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include int getValueFromUser () { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; } void printDouble ( int value ) // \u51fd\u6570\u73b0\u5728\u6709\u4e00\u4e2a\u6574\u578b\u7684\u5f62\u53c2 { std :: cout << value << \" doubled is: \" << value * 2 << '\\n' ; } int main () { int num { getValueFromUser () }; printDouble ( num ); return 0 ; } \u5728\u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\uff0c\u53d8\u91cf num \u9996\u5148\u88ab\u521d\u59cb\u5316\u4e3a\u7528\u6237\u8f93\u5165\u7684\u503c\u3002\u7136\u540e\u51fd\u6570 printDouble \u88ab\u8c03\u7528\uff0c\u5b9e\u53c2 num \u88ab\u62f7\u8d1d\u5230\u5f62\u53c2\u3002\u51fd\u6570 printDouble \u968f\u540e\u5c31\u53ef\u4ee5\u4f7f\u7528\u8be5\u5f62\u53c2\u7684\u503c\u3002","title":"\u4fee\u6b63\u9519\u8bef\u7684\u7a0b\u5e8f"},{"location":"2-4-Introduction-to-function-parameters-and-arguments/#_4","text":"\u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u53d8\u91cf num \u53ea\u88ab\u4f7f\u7528\u4e86\u4e00\u6b21\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06 getValueFromUser \u7684\u8fd4\u56de\u503c\u4f5c\u4e3a printDouble \u7684\u5b9e\u53c2\uff0c\u5c06\u4e0a\u9762\u7684\u4ee3\u7801\u7a0d\u52a0\u7b80\u5316\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int getValueFromUser () { std :: cout << \"Enter an integer: \" ; int input {}; std :: cin >> input ; return input ; } void printDouble ( int value ) { std :: cout << value << \" doubled is: \" << value * 2 << '\\n' ; } int main () { printDouble ( getValueFromUser ()); return 0 ; } \u73b0\u5728\uff0c\u6211\u4eec\u4f7f\u7528 getValueFromUser \u7684\u8fd4\u56de\u503c\u4f5c\u4e3a\u4e86 printDouble \u7684\u5b9e\u53c2\u3002 \u5c3d\u7ba1\u8fd9\u4e2a\u7a0b\u5e8f\u53d8\u5f97\u66f4\u52a0\u7cbe\u7b80\u4e86\uff08\u800c\u4e14\u4e5f\u5f88\u6e05\u695a\u7684\u8868\u660e\u7528\u6237\u7684\u8f93\u5165\u4ec5\u4ec5\u88ab\u7528\u4e8e\u8fdb\u884c\u8ba1\u7b97\uff09\uff0c\u4f46\u662f\u4f60\u4e5f\u8bb8\u4e5f\u4f1a\u51b3\u5b9a\u8fd9\u79cd\u201c\u538b\u7f29\u8bed\u6cd5\u201d\u53ef\u8bfb\u6027\u5e76\u4e0d\u4f1a\u3002\u5982\u679c\u4f60\u89c9\u5f97\u4e4b\u524d\u7684\u7248\u672c\u66f4\u52a0\u6613\u8bfb\uff0c\u90a3\u4e5f\u662f\u53ef\u4ee5\u7684\u3002","title":"\u4f7f\u7528\u8fd4\u56de\u503c\u4f5c\u4e3a\u5b9e\u53c2"},{"location":"2-4-Introduction-to-function-parameters-and-arguments/#_5","text":"\u901a\u8fc7\u4e00\u8d77\u4f7f\u7528\u5f62\u53c2\u548c\u8fd4\u56de\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u63a5\u53d7\u67d0\u4e9b\u6570\u636e\uff0c\u5bf9\u6570\u636e\u8fdb\u884c\u8ba1\u7b97\u5e76\u901a\u8fc7\u8fd4\u56de\u503c\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002 \u4e0b\u9762\u7684\u4ee3\u7801\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u51fd\u6570\u8ba1\u7b97\u4e24\u4e2a\u503c\u7684\u548c\u5e76\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // add() takes two integers as parameters, and returns the result of their sum // The values of x and y are determined by the function that calls add() int add ( int x , int y ) { return x + y ; } // main takes no parameters int main () { std :: cout << add ( 4 , 5 ) << '\\n' ; // Arguments 4 and 5 are passed to function add() return 0 ; } \u7a0b\u5e8f\u4ece main \u51fd\u6570\u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\uff0c\u5f53 add(4,5) \u8bed\u53e5\u6c42\u503c\u65f6\uff0c\u51fd\u6570 add \u88ab\u8c03\u7528\uff0c\u5176\u5f62\u53c2 x \u88ab\u521d\u59cb\u5316\u4e3a 4\uff0c\u5f62\u53c2 y \u88ab\u521d\u59cb\u5316\u4e3a 5\u3002 \u51fd\u6570\u7684 return \u8bed\u53e5\u5bf9 x+y \u8fdb\u884c\u4e86\u6c42\u503c\u5e76\u5f97\u5230\u7ed3\u679c 9\uff0c\u8be5\u503c\u968f\u540e\u88ab\u8fd4\u56de\u7ed9 main \u51fd\u6570\u3002\u7136\u540e 9 \u5c31\u88ab\u8f93\u5165 std::cout \u5e76\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002 \u8f93\u51fa\u7ed3\u679c\uff1a 1 9 \u56fe\u89e3\uff1a","title":"\u5f62\u53c2\u548c\u8fd4\u56de\u503c\u662f\u5982\u4f55\u534f\u540c\u5de5\u4f5c\u7684"},{"location":"2-4-Introduction-to-function-parameters-and-arguments/#_6","text":"\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u66f4\u591a\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include int add ( int x , int y ) { return x + y ; } int multiply ( int z , int w ) { return z * w ; } int main () { std :: cout << add ( 4 , 5 ) << '\\n' ; // within add() x=4, y=5, so x+y=9 std :: cout << add ( 1 + 2 , 3 * 4 ) << '\\n' ; // within add() x=3, y=12, so x+y=15 int a { 5 }; std :: cout << add ( a , a ) << '\\n' ; // evaluates (5 + 5) std :: cout << add ( 1 , multiply ( 2 , 3 )) << '\\n' ; // evaluates 1 + (2 * 3) std :: cout << add ( 1 , add ( 2 , 3 )) << '\\n' ; // evaluates 1 + (2 + 3) return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 9 15 10 7 6 \u7b2c\u4e00\u884c\u8bed\u53e5\u975e\u5e38\u76f4\u767d\u3002 \u7b2c\u4e8c\u884c\u8bed\u53e5\uff0c\u4f5c\u4e3a\u5b9e\u53c2\u7684\u8868\u8fbe\u5f0f\u5728\u4f20\u9012\u524d\u9996\u5148\u8fdb\u884c\u6c42\u503c\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c 1 + 2 \u6c42\u503c\u5f97\u5230 3 \uff0c\u6240\u4ee5 3 \u500d\u62f7\u8d1d\u5230\u5f62\u53c2 x\u3002 3 * 4 \u6c42\u503c\u5f97\u5230 12 \uff0c\u56e0\u6b64 12 \u88ab\u62f7\u8d1d\u5230\u5f62\u53c2 y\u3002 add(3,12) \u7684\u7ed3\u679c\u4e3a 15 \u3002 \u63a5\u4e0b\u6765\u7684\u4e24\u884c\u8bed\u53e5\u4e5f\u6bd4\u8f83\u7b80\u5355\uff1a 1 2 int a { 5 }; std :: cout << add ( a , a ) << '\\n' ; // evaluates (5 + 5) \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c add() \u53d8\u91cf a \u88ab\u62f7\u8d1d\u5230\u5f62\u53c2 x \u548c y\u3002\u7531\u4e8e a \u7684\u503c\u4e3a 5\uff0c\u5219 add(a, a) = add(5,5) \uff0c\u5176\u8ba1\u7b97\u7ed3\u679c\u4e3a 10\u3002 \u63a5\u4e0b\u6765\u662f\u8fd9\u4e00\u7cfb\u5217\u8bed\u53e5\u4e2d\u6700\u590d\u6742\u7684\u4e00\u6761\uff1a 1 std :: cout << add ( 1 , multiply ( 2 , 3 )) << '\\n' ; // evaluates 1 + (2 * 3) \u5f53\u51fd\u6570 add \u6267\u884c\u65f6\uff0c \u7a0b\u5e8f\u5fc5\u987b\u8981\u786e\u5b9a\u5176\u5f62\u53c2 x \u548c y\u3002x \u5f88\u7b80\u5355\uff0c\u56e0\u4e3a\u4f20\u9012\u7ed9\u5b83\u7684\u503c\u5c31\u662f 1\u3002\u800c\u4e3a\u4e86\u83b7\u53d6\u5f62\u53c2 y \u7684\u503c\uff0c\u7a0b\u5e8f\u5fc5\u987b\u8981\u5bf9 multiply(2,3) \u8fdb\u884c\u6c42\u503c\u3002\u4e3a\u6b64\uff0c\u7a0b\u5e8f\u9996\u5148\u8c03\u7528 multiply \u51fd\u6570\u5e76\u5c06 z \u521d\u59cb\u5316\u4e3a 2\uff0cw \u521d\u59cb\u5316\u4e3a 3\uff0c\u56e0\u6b64 multiply(2,3) \u8fd4\u56de\u503c\u4e3a 6\u3002\u8be5\u503c\u968f\u540e\u88ab\u7528\u6765\u521d\u59cb\u5316 add \u51fd\u6570\u7684\u5f62\u53c2 y\u3002 add(1,6) \u7684\u8fd4\u56de\u503c\u4e3a 7\uff0c\u8be5\u503c\u88ab\u4f20\u9012\u7ed9 std:: cout \u7528\u4e8e\u6253\u5370\u3002 \u7b80\u5355\u6765\u8bf4\uff1a add(1, multiply(2,3)) \u6c42\u503c\u5f97\u5230 add(1,6) \uff0c\u8fdb\u800c\u6c42\u503c\u5f97\u5230 7 \u4e0b\u9762\u7684\u8bed\u53e5\u770b\u4e0a\u53bb\u6709\u70b9\u7ed5\uff0c\u56e0\u4e3a add \u51fd\u6570\u7684\u4e00\u4e2a\u5b9e\u53c2\uff0c\u5176\u503c\u6765\u81ea\u4e8e\u53e6\u5916\u4e00\u4e2a add \u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002 1 std :: cout << add ( 1 , add ( 2 , 3 )) << '\\n' ; // evaluates 1 + (2 + 3) \u4e0d\u8fc7\uff0c\u5176\u5b9e\u5b83\u548c\u524d\u4e00\u4e2a\u4f8b\u5b50\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c add(2,3) \u9996\u5148\u6c42\u503c\uff0c\u8fd4\u56de\u503c\u4e3a 5\u3002\u63a5\u4e0b\u6765\uff0c\u5bf9 add(1,5) \u8fdb\u884c\u6c42\u503c\uff0c\u5176\u7ed3\u679c\u4e3a 6\uff0c\u8be5\u503c\u88ab\u4f20\u9012\u7ed9 std::cout \u7528\u4e8e\u6253\u5370\u3002 \u7b80\u5355\u6765\u8bf4\uff1a add(1, add(2,3)) \u6c42\u503c\u5f97\u5230 add(1,5) \uff0c\u7ee7\u7eed\u6c42\u503c\u5f97\u5230 6\u3002","title":"\u66f4\u591a\u4f8b\u5b50"},{"location":"2-4-Introduction-to-function-parameters-and-arguments/#_7","text":"\u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\u662f\u51fd\u6570\u80fd\u591f\u88ab\u91cd\u7528\u7684\u5173\u952e\u673a\u5236\uff0c\u56e0\u4e3a\u8be5\u673a\u5236\u53ef\u4ee5\u5141\u8bb8\u6211\u4eec\u5728\u4e0d\u6307\u5b9a\u5177\u4f53\u503c\u7684\u60c5\u51b5\u4e0b\u7f16\u5199\u51fd\u6570\u83b7\u53d6\u6216\u8ba1\u7b97\u6570\u636e\u5e76\u5c06\u8fd4\u56de\u503c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002","title":"\u7ed3\u8bba"},{"location":"2-5-Introduction-to-local-scope/","tags":["scope"],"text":"Key Takeaway \u51fd\u6570\u5f62\u53c2\u4e5f\u662f\u5c40\u90e8\u53d8\u91cf \u53d8\u91cf\u7684\u521b\u5efa\u548c\u9500\u6bc1\u90fd\u662f\u5728\u8fd0\u884c\u65f6\u53d1\u751f\u7684\uff0c\u56e0\u6b64\uff0c\u751f\u547d\u5468\u671f\u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\u5c5e\u6027\u3002\u800c\u4f5c\u7528\u57df\u662f\u4e00\u4e2a\u7f16\u8bd1\u65f6\u5c5e\u6027 \u5bf9\u8c61\u7684\u521b\u5efa\u548c\u521d\u59cb\u5316\u4e0d\u665a\u4e8e\u5176\u5b9a\u4e49\uff0c\u800c\u9500\u6bc1\u5219\u4e0d\u80fd\u65e9\u4e8e\u82b1\u62ec\u53f7\u7684\u7ed3\u5c3e\uff0c\u8fd9\u70b9\u5fc5\u987b\u4fdd\u8bc1 \u51fa\u4e8e\u4f18\u5316\u7684\u76ee\u7684\uff0c\u5bf9\u8c61\u53ef\u4ee5\u88ab\u65e9\u4e00\u70b9\u521b\u5efa\u6216\u665a\u4e00\u70b9\u9500\u6bc1\u3002\u591a\u6570\u65f6\u5019\uff0c\u5c40\u90e8\u53d8\u91cf\u4f1a\u5728\u51fd\u6570\u8fdb\u5165\u65f6\u521b\u5efa\uff0c\u5e76\u4ee5\u76f8\u53cd\u7684\u987a\u5e8f\u5728\u51fd\u6570\u9000\u51fa\u65f6\u9500\u6bc1\u3002 \u8d85\u51fa\u4f5c\u7528\u57df\u548c\u79bb\u5f00\u4f5c\u7528\u57df\u63cf\u8ff0\u7684\u5bf9\u8c61\u4e0d\u540c\uff0c\u524d\u8005\u63cf\u8ff0\u6807\u8bc6\u7b26\uff0c\u540e\u8005\u63cf\u8ff0\u5bf9\u8c61 \u5c3d\u91cf\u5728\u5c40\u90e8\u53d8\u91cf\u7b2c\u4e00\u6b21\u88ab\u4f7f\u7528\u524d\u6700\u8fd1\u7684\u5730\u65b9\u5b9a\u4e49\u8be5\u5c40\u90e8\u53d8\u91cf \u5c40\u90e8\u53d8\u91cf \u51fd\u6570\u7684 \u5f62\u53c2 \u548c\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u4e00\u6837\uff0c\u90fd\u662f \u5c40\u90e8\u53d8\u91cf(local variable) (\u5c40\u90e8\u53d8\u91cf\u662f\u548c \u5168\u5c40\u53d8\u91cf(global variable) \u76f8\u5bf9\u7684\u6982\u5ff5\uff0c\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5168\u5c40\u53d8\u91cf\uff09\u3002 1 2 3 4 5 6 int add ( int x , int y ) // function parameters x and y are local variables { int z { x + y }; // z is a local variable too return z ; } \u672c\u8282\u8bfe\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5c40\u90e8\u53d8\u91cf\u6240\u5177\u6709\u7684\u4e00\u4e9b\u5c5e\u6027\u3002 \u5c40\u90e8\u53d8\u91cf\u751f\u547d\u5468\u671f \u5728 1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf \u4e2d\u6211\u4eec\u8ba8\u8bba\u4e86\u4ee5\u5f62\u5982 int x; \u7684\u65b9\u5f0f\u5b9a\u4e49\u53d8\u91cf\u662f\u5982\u4f55\u5728\u8bed\u53e5\u6267\u884c\u65f6 \u5b9e\u4f8b\u5316 \u53d8\u91cf\u7684\u3002\u51fd\u6570\u7684 \u5f62\u53c2 \u662f\u5728\u8fdb\u5165\u51fd\u6570\u65f6\u88ab\u521b\u5efa\u548c\u521d\u59cb\u5316\u7684\uff0c\u51fd\u6570\u4f53\u5185\u7684\u51fd\u6570\u5219\u662f\u5728\u5b9a\u4e49\u65f6\u521b\u5efa\u5e76\u521d\u59cb\u5316\u7684\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 int add ( int x , int y ) // x \u548c y \u5728\u6b64\u5904\u521b\u5efa\u5e76\u521d\u59cb\u5316 { int z { x + y }; // z \u5728\u6b64\u5904\u88ab\u521b\u5efa\u548c\u521d\u59cb\u5316 return z ; } \u6b64\u65f6\u6211\u4eec\u4e0d\u7ecf\u8981\u95ee\uff0c\u201d\u90a3\u4e48\u5b9e\u4f8b\u5316\u7684\u53d8\u91cf\u662f\u5728\u4f55\u65f6\u88ab\u9500\u6bc1\u7684\u5462\uff1f\u201c\u3002\u5c40\u90e8\u53d8\u91cf\u662f\u5728 \u5305\u542b\u5176\u5b9a\u4e49\u7684\u4e00\u7ec4\u82b1\u62ec\u53f7\u7684\u7ed3\u5c3e \u65f6\u9500\u6bc1\u7684\uff0c\u5176\u9500\u6bc1\u7684\u987a\u5e8f\u548c\u5b9a\u4e49\u987a\u5e8f\u6b63\u597d\u76f8\u53cd\uff08\u5bf9\u4e8e \u5f62\u53c2 \u6765\u8bf4\uff0c\u5219\u662f\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u9500\u6bc1\uff09\u3002 1 2 3 4 5 6 int add ( int x , int y ) { int z { x + y }; return z ; } // z, y, and x destroyed here \u6b63\u5982\u4eba\u7684\u751f\u547d\u5468\u671f\u88ab\u5b9a\u4e49\u4e3a\u5176\u51fa\u751f\u5230\u6b7b\u4ea1\u7684\u8fd9\u6bb5\u65f6\u95f4\u4e00\u6837\uff0c\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f(lifetime)\u6307\u7684\u662f\u8be5\u53d8\u91cf\u88ab\u521b\u5efa\u5230\u88ab\u9500\u6bc1\u7684\u8fd9\u6bb5\u65f6\u95f4\u3002\u6ce8\u610f\uff0c\u53d8\u91cf\u7684\u521b\u5efa\u548c\u9500\u6bc1\u90fd\u662f\u5728\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u5019\u53d1\u751f\u7684\uff08\u79f0\u4e3a \u8fd0\u884c\u65f6 \uff09\uff0c\u800c\u4e0d\u662f\u5728 \u7f16\u8bd1\u65f6 \u53d1\u751f\u7684\u3002\u56e0\u6b64\uff0c\u751f\u547d\u5468\u671f\u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\u5c5e\u6027\u3002 \u6269\u5c55\u9605\u8bfb \u4e0a\u9762\u5173\u4e8e\u521b\u5efa\u3001\u521d\u59cb\u5316\u548c\u9500\u6bc1\u7684\u89c4\u5219\u662f\u5fc5\u987b\u8981\u6ee1\u8db3\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u521d\u59cb\u5316\u4e0d\u665a\u4e8e\u5176\u5b9a\u4e49\uff0c\u800c\u9500\u6bc1\u5219\u4e0d\u80fd\u65e9\u4e8e\u82b1\u62ec\u53f7\u7684\u7ed3\u5c3e\uff08\u5bf9\u4e8e\u51fd\u6570\u5f62\u53c2\u6765\u8bf4\u5219\u662f\u51fd\u6570\u7ed3\u5c3e\uff09\u3002 \u5b9e\u9645\u4e0a\uff0cC++ \u7684\u89c4\u8303\u7ed9\u4e0e\u7f16\u8bd1\u5668\u4e86\u4e00\u4e9b\u7075\u6d3b\u6027\uff0c\u4f7f\u7f16\u8bd1\u5668\u53ef\u4ee5\u51b3\u5b9a\u4f55\u65f6\u521b\u5efa\u6216\u9500\u6bc1\u5c40\u90e8\u53d8\u91cf\u3002\u51fa\u4e8e\u4f18\u5316\u7684\u76ee\u7684\uff0c\u5bf9\u8c61\u53ef\u4ee5\u88ab\u65e9\u4e00\u70b9\u521b\u5efa\u6216\u665a\u4e00\u70b9\u9500\u6bc1\u3002\u591a\u6570\u65f6\u5019\uff0c\u5c40\u90e8\u53d8\u91cf\u4f1a\u5728\u51fd\u6570\u8fdb\u5165\u65f6\u521b\u5efa\uff0c\u5e76\u4ee5\u76f8\u53cd\u7684\u987a\u5e8f\u5728\u51fd\u6570\u9000\u51fa\u65f6\u9500\u6bc1\u3002\u6211\u4eec\u4f1a\u5728\u5c06\u6765\u7684\u6709\u5173\u51fd\u6570\u8c03\u7528\u6808\u8bfe\u7a0b\u4e2d\u5bf9\u5176\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002 \u4e0b\u9762\u8fd9\u6bb5\u7a0b\u5e8f\u5c55\u793a\u4e86\u6709\u5173\u53d8\u91cf x \u751f\u547d\u5468\u671f\u66f4\u590d\u6742\u7684\u60c5\u51b5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include void doSomething () { std :: cout << \"Hello! \\n \" ; } int main () { int x { 0 }; // x \u7684\u751f\u547d\u5468\u671f\u8d77\u70b9 doSomething (); // \u5728\u51fd\u6570\u8c03\u7528\u671f\u95f4\uff0cx \u4ecd\u7136\u5b58\u5728 return 0 ; } // x \u7684\u751f\u547d\u5468\u671f\u7ec8\u70b9 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cx \u7684\u751f\u547d\u5468\u671f\u4ece\u5b83\u7684\u5b9a\u4e49\u5f00\u59cb\u4e00\u76f4\u5230 main \u51fd\u6570\u7684\u672b\u5c3e\u7ed3\u675f\u3002\u8fd9\u5176\u4e2d\u5305\u542b\u4e86\u51fd\u6570 doSomething \u6267\u884c\u7684\u65f6\u95f4\u3002 \u5c40\u90e8\u4f5c\u7528\u57df \u4e00\u4e2a \u6807\u8bc6\u7b26 \u7684 \u4f5c\u7528\u57df(scope) \u8868\u660e\u4e86\u8be5\u8868\u793a\u7b26\u5728\u6e90\u7801\u4e2d\u80fd\u591f\u88ab\u8bbf\u95ee\u7684\u4f4d\u7f6e\u3002\u5982\u679c\u4e00\u4e2a\u6807\u8bc6\u7b26\u53ef\u4ee5\u88ab\u8bbf\u95ee\uff0c\u5219\u79f0\u5176\u5728\u4f5c\u7528\u57df\u4e2d\u3002\u5f53\u4e00\u4e2a\u6807\u8bc6\u7b26\u4e0d\u80fd\u88ab\u8bbf\u95ee\u65f6\uff0c\u5219\u79f0\u5176\u8d85\u51fa\u4e86\u4f5c\u7528\u57df\uff08Out of scope\uff09\u3002\u4f5c\u7528\u57df\u662f\u4e00\u4e2a \u7f16\u8bd1\u65f6 \u5c5e\u6027\uff0c\u5982\u679c\u5728\u6807\u8bc6\u7b26\u7684\u4f5c\u7528\u57df\u4e4b\u5916\u8bbf\u95ee\u5b83\uff0c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 \u5c40\u90e8\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4ece\u8be5\u53d8\u91cf\u7684\u5b9a\u4e49\u5f00\u59cb\uff0c\u5728\u5305\u542b\u4e86\u8be5\u53d8\u91cf\u5b9a\u4e49\u7684\u4e00\u7ec4\u82b1\u62ec\u53f7\u7684\u7ed3\u5c3e\u7ec8\u6b62\uff08\u5bf9\u4e8e\u51fd\u6570 \u5f62\u53c2 \u5219\u5728\u51fd\u6570\u7ed3\u5c3e\u7ec8\u6b62\uff09\u3002\u8fd9\u786e\u4fdd\u4e86\u53d8\u91cf\u5728\u5b9a\u4e49\u524d\u4e0d\u80fd\u88ab\u4f7f\u7528\uff08\u5373\u4f7f\u7f16\u8bd1\u5668\u9009\u62e9\u5728\u6b64\u4e4b\u524d\u521b\u5efa\u53d8\u91cf\uff09\u3002\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d\u5b9a\u4e49\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u5176\u4f5c\u7528\u57df\u4e0d\u80fd\u5ef6\u4f38\u5230\u5176\u4ed6\u51fd\u6570\u4e2d\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u53d8\u91cf x \u7684\u4f5c\u7528\u57df\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include // x \u4e0d\u5728\u8be5\u51fd\u6570\u7684\u4f5c\u7528\u57df void doSomething () { std :: cout << \"Hello! \\n \" ; } int main () { // x \u6b64\u65f6\u8fd8\u4e0d\u80fd\u88ab\u8bbf\u95ee\uff0c\u56e0\u4e3a\u8fd8\u6ca1\u6709\u8fdb\u5165\u5176\u4f5c\u7528\u57df int x { 0 }; // x \u4ece\u6b64\u65f6\u8fdb\u5165\u4f5c\u7528\u57df\uff0c\u968f\u540e\u53ef\u4ee5\u5728\u8be5\u51fd\u6570\u4e2d\u4f7f\u7528\u5b83 doSomething (); return 0 ; } // x \u4f5c\u7528\u57df\u7ec8\u6b62\u4e0e\u6b64\uff0c\u6b64\u540e\u4e0d\u518d\u80fd\u591f\u88ab\u4f7f\u7528 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u53d8\u91cf x \u4ece\u5176\u5b9a\u4e49\u4f4d\u7f6e\u5f00\u59cb\u8fdb\u5165\u4f5c\u7528\u57df\uff0c\u5728 main \u51fd\u6570\u7ed3\u675f\u65f6\u79bb\u5f00\u5176\u4f5c\u7528\u57df\u3002\u6ce8\u610f\uff0c\u53d8\u91cf x \u7684\u4f5c\u7528\u57df\u4e0d\u4f1a\u5ef6\u4f38\u5230\u51fd\u6570 doSomething() \u4e2d\u3002\u8fd9\u4e5f\u4e00\u4f4d\u7f6e main \u51fd\u6570\u8c03\u7528 doSomething \u548c\u8be5\u8be5\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u6ca1\u6709\u4efb\u4f55\u5173\u7cfb\u3002 \u201c\u8d85\u51fa\u4f5c\u7528\u57df\u201d \u548c \u201c\u79bb\u5f00\u4f5c\u7528\u57df\u201d \u672f\u8bed \u8d85\u51fa\u4f5c\u7528\u57df \u548c \u79bb\u5f00\u4f5c\u7528\u57df \u5bf9\u4e8e\u65b0\u624b\u6765\u8bf4\u53ef\u80fd\u4ee4\u4eba\u56f0\u60d1\u3002 \u6807\u8bc6\u7b26\u8d85\u51fa\u4f5c\u7528\u57df\u8868\u793a\u5176\u4e0d\u80fd\u5728\u4ee3\u7801\u4e2d\u88ab\u8bbf\u95ee\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c \u6807\u8bc6\u7b26(identifier) x \u8fdb\u5165\u4f5c\u7528\u57df\u7684\u65f6\u95f4\u4ece\u5176\u5b9a\u4e49\u5f00\u59cb\u5230 main \u51fd\u6570\u7ed3\u5c3e\u4e3a\u6b62\u3002\u8fd9\u6bb5\u4ee3\u7801\u4ee5\u5916\u7684\u4ee3\u7801\u4e2d\u8bbf\u95ee\u8be5\u6807\u8bc6\u7b26\uff0c\u90fd\u5c5e\u4e8e\u8d85\u51fa\u4f5c\u7528\u57df\u3002 \u672f\u8bed\u79bb\u5f00\u4f5c\u7528\u57df\u63cf\u8ff0\u7684\u5bf9\u8c61\u5219\u4e00\u822c\u662f\u67d0\u4e2a\u5bf9\u8c61\u800c\u4e0d\u662f\u6807\u8bc6\u7b26\u3002\u6211\u4eec\u8bf4\u4e00\u4e2a\u53d8\u91cf\u4f1a\u5728\u5b9e\u4f8b\u5316\u5b83\u4f5c\u7528\u57df\u7684\u7ed3\u5c3e\u5904\uff08\u82b1\u62ec\u53f7\u7ed3\u675f\u5904\uff09\u79bb\u5f00\u4f5c\u7528\u57df\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c \u5bf9\u8c61 x \u4f1a\u5728 main \u51fd\u6570\u7684\u7ed3\u5c3e\u5904\u79bb\u5f00\u4f5c\u7528\u57df\u3002 \u5177\u5907\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u4f1a\u5728\u5176\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\u7ec8\u6b62\uff0c\u5c40\u90e8\u53d8\u91cf\u4f1a\u5728\u6b64\u65f6\u88ab\u9500\u6bc1\u3002 \u6ce8\u610f\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u7c7b\u578b\u7684\u53d8\u91cf\u90fd\u4f1a\u5728\u5176\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\u88ab\u9500\u6bc1\u3002\u6211\u4eec\u4f1a\u5728\u5c06\u6765\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u6b64\u7c7b\u4f8b\u5b50\u3002 \u5176\u4ed6\u4f8b\u5b50 \u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u7a0d\u5fae\u66f4\u590d\u6742\u4e00\u5b9a\u3002\u8bf7\u8bb0\u4f4f\uff0c\u751f\u547d\u5468\u671f\u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\u5c5e\u6027\uff0c\u800c\u4f5c\u7528\u57df\u662f\u7f16\u8bd1\u65f6\u5c5e\u6027\u3002\u867d\u7136\uff0c\u6211\u4eec\u5728\u540c\u4e00\u6bb5\u4ee3\u7801\u4e2d\u8ba8\u8bba\u4e24\u8005\uff0c\u5b83\u4eec\u7684\u51fa\u53d1\u70b9\u662f\u4e0d\u540c\u7684\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int add ( int x , int y ) // x \u548c y \u5728\u6b64\u5904\u88ab\u521b\u5efa\u5e76\u8fdb\u5165\u4f5c\u7528\u57df { // x \u548c y \u4ec5\u5728\u8be5\u51fd\u6570\u4e2d\u53ef\u89c1/\u53ef\u7528 return x + y ; } // y \u548c x \u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1 int main () { int a { 5 }; // a \u5728\u6b64\u5904\u88ab\u521b\u5efa\u3001\u521d\u59cb\u5316\u5e76\u8fdb\u5165\u4f5c\u7528\u57df int b { 6 }; // b \u5728\u6b64\u5904\u88ab\u521b\u5efa\u3001\u521d\u59cb\u5316\u5e76\u8fdb\u5165\u4f5c\u7528\u57df // a \u548c b \u53ea\u5728\u8be5\u51fd\u6570\u4e2d\u53ef\u7528 std :: cout << add ( a , b ) << '\\n' ; // calls function add() with x=5 and y=6 return 0 ; } // b \u548c a \u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1 \u51fd\u6570\u5f62\u53c2 x \u548c y \u5728 add \u51fd\u6570\u88ab\u8c03\u7528\u65f6\u521b\u5efa\uff0c\u4f5c\u4e3a\u53d8\u91cf\uff0c\u5b83\u4eec\u53ea\u5728 add \u51fd\u6570\u4e2d\u53ef\u89c1\u53ef\u7528\uff0c\u5e76\u5728 add \u51fd\u6570\u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u53d8\u91cf a \u548c b \u5728\u51fd\u6570 main \u4e2d\u88ab\u521b\u5efa\uff0c\u53ea\u5728 main \u51fd\u6570\u4e2d\u53ef\u7528\uff0c\u5e76\u4e14\u5728 main \u51fd\u6570\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u3002 \u4e3a\u4e86\u5f3a\u5316\u7406\u89e3\uff0c\u8ba9\u6211\u4eec\u9010\u6b65\u89e3\u6790\u8be5\u51fd\u6570\uff0c\u4e0b\u5217\u64cd\u4f5c\u6309\u987a\u5e8f\u53d1\u751f\uff1a - \u7a0b\u5e8f\u4ece main \u51fd\u6570\u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\uff1b - main \u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf a \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 5\uff1b - main \u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf b \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 6\uff1b - add \u51fd\u6570\u88ab\u8c03\u7528\uff0c\u51fd\u6570\u7684\u5b9e\u53c2\u4e3a 5 \u548c 6\uff1b - add \u7684\u53d8\u91cf x \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 5\uff1b - add \u7684\u53d8\u91cf y \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 6\uff1b - \u8868\u8fbe\u5f0f x + y \u6c42\u503c\u5f97\u5230\u7ed3\u679c 11\uff1b - add \u5c06\u503c 11 \u62f7\u8d1d\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570 main \uff1b - add \u7684 y \u548c x \u88ab\u9500\u6bc1\uff1b - main \u5728\u63a7\u5236\u53f0\u6253\u5370 11\uff1b - main \u8fd4\u56de 0 \u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff1b - main \u7684\u5c40\u90e8\u53d8\u91cf b \u548c a \u88ab\u9500\u6bc1\uff1b \u4ee5\u4e0a\u3002 \u6ce8\u610f\uff0c\u5982\u679c\u51fd\u6570 add \u88ab\u5148\u540e\u4e24\u6b21\u8c03\u7528\uff0c\u90a3\u4e48\u5bf9\u5e94\u7684\u5f62\u53c2 x \u548c y \u4e5f\u88ab\u5148\u540e\u521b\u5efa\u5e76\u9500\u6bc1\u4e24\u6b21\u2014\u2014\u6bcf\u6b21\u8c03\u7528\u5c31\u4f1a\u53d1\u751f\u4e00\u6b21\u521b\u5efa\u548c\u9500\u6bc1\u3002\u5982\u679c\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\u6709\u5927\u91cf\u7684\u51fd\u6570\u8c03\u7528\uff0c\u90a3\u4e48\u53d8\u91cf\u4e5f\u4f1a\u968f\u4e4b\u88ab\u9891\u7e41\u7684\u521b\u5efa\u548c\u9500\u6bc1\u3002 \u51fd\u6570\u7684\u9694\u79bb \u5728\u4e0a\u8ff0\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f88\u660e\u663e\u53ef\u4ee5\u770b\u51fa\uff0c\u53d8\u91cf a \u548c b \u662f\u4e0d\u540c\u4e8e\u53d8\u91cf x \u548c y \u7684\u53d8\u91cf\u3002 \u73b0\u5728\uff0c\u8bf7\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int add ( int x , int y ) // x \u548c y \u5728\u6b64\u5904\u88ab\u521b\u5efa\u5e76\u8fdb\u5165\u4f5c\u7528\u57df { // x \u548c y \u4ec5\u5728\u8be5\u51fd\u6570\u4e2d\u53ef\u89c1/\u53ef\u7528 return x + y ; } // y \u548c x \u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1 int main () { int x { 5 }; // x \u5728\u6b64\u5904\u88ab\u521b\u5efa\u3001\u521d\u59cb\u5316\u5e76\u8fdb\u5165\u4f5c\u7528\u57df int y { 6 }; // y \u5728\u6b64\u5904\u88ab\u521b\u5efa\u3001\u521d\u59cb\u5316\u5e76\u8fdb\u5165\u4f5c\u7528\u57df // x \u548c y \u53ea\u5728\u8be5\u51fd\u6570\u4e2d\u53ef\u7528 std :: cout << add ( x , y ) << '\\n' ; // calls function add() with x=5 and y=6 return 0 ; } // x \u548c y \u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1 \u4e0e\u524d\u4e00\u4e2a\u4f8b\u5b50\u76f8\u6bd4\uff0c\u672c\u4f8b\u53ea\u662f\u5c06 main \u51fd\u6570\u4e2d\u7684\u53d8\u91cf a \u548c b \u4fee\u6539\u6210\u4e86 x \u548c y \u3002\u8fd9\u4e2a\u7a0b\u5e8f\u7684\u7f16\u8bd1\u548c\u8fd0\u884c\u548c\u4e4b\u524d\u7684\u7a0b\u5e8f\u6ca1\u6709\u4efb\u4f55\u533a\u522b\uff0c\u5373\u4f7f main \u51fd\u6570\u548c add \u51fd\u6570\u90fd\u4f7f\u7528\u4e86\u53d8\u91cf\u540d x \u548c y \u3002\u4e3a\u4ec0\u4e48\u8fd9\u6837\u4e5f\u53ef\u4ee5\u5de5\u4f5c\u5462\uff1f \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u8ba4\u8bc6\u5230\u7684\u662f\uff0c\u5373\u4f7f main \u51fd\u6570\u548c add \u51fd\u6570\u90fd\u4f7f\u7528\u4e86\u53d8\u91cf\u540d x \u548c y \uff0c\u4f46\u662f\u8fd9\u4e9b\u53d8\u91cf\u5b9e\u9645\u4e0a\u662f\u5b8c\u5168\u4e0d\u540c\u7684\u3002 main \u51fd\u6570\u4e2d\u7684 x \u548c y \uff0c\u548c add \u51fd\u6570\u4e2d\u7684 x \u548c y \u662f\u6ca1\u6709\u4efb\u4f55\u5173\u7cfb\u7684\u2014\u2014\u5b83\u4eec\u53ea\u662f\u78b0\u5de7\u540c\u540d\u7f62\u4e86\u3002 \u5176\u6b21\uff0c\u5728 main \u51fd\u6570\u4e2d\uff0c \u53d8\u91cf\u540d x \u548c y \u6307\u7684\u662f main \u51fd\u6570\u5c40\u90e8\u4f5c\u7528\u4e2d\u7684\u53d8\u91cf x \u548c y \u3002\u8fd9\u4e9b\u53d8\u91cf\u53ea\u80fd\u591f\u5728 main \u51fd\u6570\u4e2d\u88ab\u770b\u5230\uff08\u88ab\u4f7f\u7528\uff09\u3002\u540c\u6837\u7684\uff0c\u5728 add \u51fd\u6570\u4e2d\uff0c, \u53d8\u91cf\u540d x \u548c y \u6307\u7684\u662f add \u51fd\u6570\u7684\u5f62\u53c2 x \u548c y \uff0c\u5b83\u4eec\u4e5f\u53ea\u80fd\u591f\u5728 add \u4e2d\u88ab\u770b\u5230\u548c\u4f7f\u7528\u3002 \u957f\u8bdd\u77ed\u8bf4\uff0c\u5176\u5b9e add \u548c main \u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u4e0d\u77e5\u9053\u5bf9\u65b9\u4f7f\u7528\u4e86\u4e0e\u81ea\u5df1\u76f8\u540c\u7684\u53d8\u91cf\u540d\u3002\u56e0\u4e3a\uff0c\u8fd9\u4e9b\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u5e76\u6ca1\u6709\u91cd\u53e0\u7684\u90e8\u5206\uff0c\u56e0\u6b64\u5bf9\u4e8e\u7f16\u8bd1\u5668\u6765\u8bf4\uff0c x \u548c y \u5728\u4efb\u4f55\u65f6\u5019\u88ab\u5f15\u7528\uff0c\u5176\u6307\u4ee3\u7684\u5bf9\u8c61\u90fd\u662f\u6e05\u695a\u7684\u3002 \u5173\u952e\u4fe1\u606f \u51fd\u6570\u5f62\u53c2\u6216\u8005\u662f\u5c40\u90e8\u53d8\u91cf\u5728\u58f0\u660e\u7684\u65f6\u5019\u6240\u4f7f\u7528\u7684\u540d\u5b57\uff0c\u53ea\u5728\u51fd\u6570\u4f53\u5185\u90e8\u53ef\u89c1\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u51fd\u6570\u5728\u5b9a\u4e49\u5c40\u90e8\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u4e0d\u8003\u8651\u5176\u4ed6\u51fd\u6570\u662f\u5426\u4f7f\u7528\u4e86\u67d0\u4e9b\u53d8\u91cf\u540d\u3002\u8fd9\u4e00\u7279\u6027\uff0c\u53ef\u4ee5\u5e2e\u52a9\u51fd\u6570\u4fdd\u6301\u72ec\u7acb\u6027\u3002 \u5728\u540e\u7eed\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u4f1a\u63a2\u8ba8\u5173\u4e8e\u5c40\u90e8\u4f5c\u7528\u57df\u7684\u66f4\u591a\u5185\u5bb9\uff0c\u540c\u65f6\u4e5f\u4f1a\u8c08\u5230\u5176\u4ed6\u7c7b\u578b\u7684\u4f5c\u7528\u57df\u3002 \u5728\u4f55\u5904\u5b9a\u4e49\u5c40\u90e8\u53d8\u91cf \u51fd\u6570\u4f53\u5185\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u5176\u5b9a\u4e49\u7684\u4f4d\u7f6e\u5e94\u8be5\u5c3d\u53ef\u80fd\u63a5\u8fd1\u5176\u7b2c\u4e00\u6b21\u88ab\u4f7f\u7528\u7684\u4f4d\u7f6e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; // x \u5728\u6b64\u5904\u5b9a\u4e49 std :: cin >> x ; // \u5728\u6b64\u5904\u4f7f\u7528 std :: cout << \"Enter another integer: \" ; int y {}; // y \u5728\u6b64\u5904\u5b9a\u4e49 std :: cin >> y ; // \u5728\u6b64\u5904\u4f7f\u7528 int sum { x + y }; // sum \u5728\u6b64\u5904\u5b9a\u4e49 std :: cout << \"The sum is: \" << sum << '\\n' ; // \u5728\u6b64\u5904\u4f7f\u7528 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6bcf\u4e2a\u53d8\u91cf\u90fd\u662f\u5728\u5b83\u7b2c\u4e00\u6b21\u88ab\u4f7f\u7528\u65f6\u7684\u524d\u4e00\u884c\u5b9a\u4e49\u7684\u3002\u8fd9\u4e2a\u89c4\u5b9a\u5012\u662f\u65e0\u9700\u4e25\u683c\u9075\u5b88\u2014\u2014\u5982\u679c\u4f60\u5e0c\u671b\u5c06\u7b2c\u4e94\u884c\u548c\u7b2c\u516d\u884c\u7684\u4f4d\u7f6e\u6362\u4e00\u4e0b\u4e5f\u662f\u53ef\u4ee5\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c3d\u91cf\u5728\u5c40\u90e8\u53d8\u91cf\u7b2c\u4e00\u6b21\u88ab\u4f7f\u7528\u524d\u6700\u8fd1\u7684\u5730\u65b9\u5b9a\u4e49\u8be5\u5c40\u90e8\u53d8\u91cf\u3002","title":"2.5 - \u5c40\u90e8\u4f5c\u7528\u57df"},{"location":"2-5-Introduction-to-local-scope/#_1","text":"\u51fd\u6570\u7684 \u5f62\u53c2 \u548c\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u4e00\u6837\uff0c\u90fd\u662f \u5c40\u90e8\u53d8\u91cf(local variable) (\u5c40\u90e8\u53d8\u91cf\u662f\u548c \u5168\u5c40\u53d8\u91cf(global variable) \u76f8\u5bf9\u7684\u6982\u5ff5\uff0c\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4f1a\u4ecb\u7ecd\u5168\u5c40\u53d8\u91cf\uff09\u3002 1 2 3 4 5 6 int add ( int x , int y ) // function parameters x and y are local variables { int z { x + y }; // z is a local variable too return z ; } \u672c\u8282\u8bfe\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5c40\u90e8\u53d8\u91cf\u6240\u5177\u6709\u7684\u4e00\u4e9b\u5c5e\u6027\u3002","title":"\u5c40\u90e8\u53d8\u91cf"},{"location":"2-5-Introduction-to-local-scope/#_2","text":"\u5728 1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf \u4e2d\u6211\u4eec\u8ba8\u8bba\u4e86\u4ee5\u5f62\u5982 int x; \u7684\u65b9\u5f0f\u5b9a\u4e49\u53d8\u91cf\u662f\u5982\u4f55\u5728\u8bed\u53e5\u6267\u884c\u65f6 \u5b9e\u4f8b\u5316 \u53d8\u91cf\u7684\u3002\u51fd\u6570\u7684 \u5f62\u53c2 \u662f\u5728\u8fdb\u5165\u51fd\u6570\u65f6\u88ab\u521b\u5efa\u548c\u521d\u59cb\u5316\u7684\uff0c\u51fd\u6570\u4f53\u5185\u7684\u51fd\u6570\u5219\u662f\u5728\u5b9a\u4e49\u65f6\u521b\u5efa\u5e76\u521d\u59cb\u5316\u7684\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 int add ( int x , int y ) // x \u548c y \u5728\u6b64\u5904\u521b\u5efa\u5e76\u521d\u59cb\u5316 { int z { x + y }; // z \u5728\u6b64\u5904\u88ab\u521b\u5efa\u548c\u521d\u59cb\u5316 return z ; } \u6b64\u65f6\u6211\u4eec\u4e0d\u7ecf\u8981\u95ee\uff0c\u201d\u90a3\u4e48\u5b9e\u4f8b\u5316\u7684\u53d8\u91cf\u662f\u5728\u4f55\u65f6\u88ab\u9500\u6bc1\u7684\u5462\uff1f\u201c\u3002\u5c40\u90e8\u53d8\u91cf\u662f\u5728 \u5305\u542b\u5176\u5b9a\u4e49\u7684\u4e00\u7ec4\u82b1\u62ec\u53f7\u7684\u7ed3\u5c3e \u65f6\u9500\u6bc1\u7684\uff0c\u5176\u9500\u6bc1\u7684\u987a\u5e8f\u548c\u5b9a\u4e49\u987a\u5e8f\u6b63\u597d\u76f8\u53cd\uff08\u5bf9\u4e8e \u5f62\u53c2 \u6765\u8bf4\uff0c\u5219\u662f\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u9500\u6bc1\uff09\u3002 1 2 3 4 5 6 int add ( int x , int y ) { int z { x + y }; return z ; } // z, y, and x destroyed here \u6b63\u5982\u4eba\u7684\u751f\u547d\u5468\u671f\u88ab\u5b9a\u4e49\u4e3a\u5176\u51fa\u751f\u5230\u6b7b\u4ea1\u7684\u8fd9\u6bb5\u65f6\u95f4\u4e00\u6837\uff0c\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f(lifetime)\u6307\u7684\u662f\u8be5\u53d8\u91cf\u88ab\u521b\u5efa\u5230\u88ab\u9500\u6bc1\u7684\u8fd9\u6bb5\u65f6\u95f4\u3002\u6ce8\u610f\uff0c\u53d8\u91cf\u7684\u521b\u5efa\u548c\u9500\u6bc1\u90fd\u662f\u5728\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u5019\u53d1\u751f\u7684\uff08\u79f0\u4e3a \u8fd0\u884c\u65f6 \uff09\uff0c\u800c\u4e0d\u662f\u5728 \u7f16\u8bd1\u65f6 \u53d1\u751f\u7684\u3002\u56e0\u6b64\uff0c\u751f\u547d\u5468\u671f\u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\u5c5e\u6027\u3002 \u6269\u5c55\u9605\u8bfb \u4e0a\u9762\u5173\u4e8e\u521b\u5efa\u3001\u521d\u59cb\u5316\u548c\u9500\u6bc1\u7684\u89c4\u5219\u662f\u5fc5\u987b\u8981\u6ee1\u8db3\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u521d\u59cb\u5316\u4e0d\u665a\u4e8e\u5176\u5b9a\u4e49\uff0c\u800c\u9500\u6bc1\u5219\u4e0d\u80fd\u65e9\u4e8e\u82b1\u62ec\u53f7\u7684\u7ed3\u5c3e\uff08\u5bf9\u4e8e\u51fd\u6570\u5f62\u53c2\u6765\u8bf4\u5219\u662f\u51fd\u6570\u7ed3\u5c3e\uff09\u3002 \u5b9e\u9645\u4e0a\uff0cC++ \u7684\u89c4\u8303\u7ed9\u4e0e\u7f16\u8bd1\u5668\u4e86\u4e00\u4e9b\u7075\u6d3b\u6027\uff0c\u4f7f\u7f16\u8bd1\u5668\u53ef\u4ee5\u51b3\u5b9a\u4f55\u65f6\u521b\u5efa\u6216\u9500\u6bc1\u5c40\u90e8\u53d8\u91cf\u3002\u51fa\u4e8e\u4f18\u5316\u7684\u76ee\u7684\uff0c\u5bf9\u8c61\u53ef\u4ee5\u88ab\u65e9\u4e00\u70b9\u521b\u5efa\u6216\u665a\u4e00\u70b9\u9500\u6bc1\u3002\u591a\u6570\u65f6\u5019\uff0c\u5c40\u90e8\u53d8\u91cf\u4f1a\u5728\u51fd\u6570\u8fdb\u5165\u65f6\u521b\u5efa\uff0c\u5e76\u4ee5\u76f8\u53cd\u7684\u987a\u5e8f\u5728\u51fd\u6570\u9000\u51fa\u65f6\u9500\u6bc1\u3002\u6211\u4eec\u4f1a\u5728\u5c06\u6765\u7684\u6709\u5173\u51fd\u6570\u8c03\u7528\u6808\u8bfe\u7a0b\u4e2d\u5bf9\u5176\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002 \u4e0b\u9762\u8fd9\u6bb5\u7a0b\u5e8f\u5c55\u793a\u4e86\u6709\u5173\u53d8\u91cf x \u751f\u547d\u5468\u671f\u66f4\u590d\u6742\u7684\u60c5\u51b5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include void doSomething () { std :: cout << \"Hello! \\n \" ; } int main () { int x { 0 }; // x \u7684\u751f\u547d\u5468\u671f\u8d77\u70b9 doSomething (); // \u5728\u51fd\u6570\u8c03\u7528\u671f\u95f4\uff0cx \u4ecd\u7136\u5b58\u5728 return 0 ; } // x \u7684\u751f\u547d\u5468\u671f\u7ec8\u70b9 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cx \u7684\u751f\u547d\u5468\u671f\u4ece\u5b83\u7684\u5b9a\u4e49\u5f00\u59cb\u4e00\u76f4\u5230 main \u51fd\u6570\u7684\u672b\u5c3e\u7ed3\u675f\u3002\u8fd9\u5176\u4e2d\u5305\u542b\u4e86\u51fd\u6570 doSomething \u6267\u884c\u7684\u65f6\u95f4\u3002","title":"\u5c40\u90e8\u53d8\u91cf\u751f\u547d\u5468\u671f"},{"location":"2-5-Introduction-to-local-scope/#_3","text":"\u4e00\u4e2a \u6807\u8bc6\u7b26 \u7684 \u4f5c\u7528\u57df(scope) \u8868\u660e\u4e86\u8be5\u8868\u793a\u7b26\u5728\u6e90\u7801\u4e2d\u80fd\u591f\u88ab\u8bbf\u95ee\u7684\u4f4d\u7f6e\u3002\u5982\u679c\u4e00\u4e2a\u6807\u8bc6\u7b26\u53ef\u4ee5\u88ab\u8bbf\u95ee\uff0c\u5219\u79f0\u5176\u5728\u4f5c\u7528\u57df\u4e2d\u3002\u5f53\u4e00\u4e2a\u6807\u8bc6\u7b26\u4e0d\u80fd\u88ab\u8bbf\u95ee\u65f6\uff0c\u5219\u79f0\u5176\u8d85\u51fa\u4e86\u4f5c\u7528\u57df\uff08Out of scope\uff09\u3002\u4f5c\u7528\u57df\u662f\u4e00\u4e2a \u7f16\u8bd1\u65f6 \u5c5e\u6027\uff0c\u5982\u679c\u5728\u6807\u8bc6\u7b26\u7684\u4f5c\u7528\u57df\u4e4b\u5916\u8bbf\u95ee\u5b83\uff0c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 \u5c40\u90e8\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4ece\u8be5\u53d8\u91cf\u7684\u5b9a\u4e49\u5f00\u59cb\uff0c\u5728\u5305\u542b\u4e86\u8be5\u53d8\u91cf\u5b9a\u4e49\u7684\u4e00\u7ec4\u82b1\u62ec\u53f7\u7684\u7ed3\u5c3e\u7ec8\u6b62\uff08\u5bf9\u4e8e\u51fd\u6570 \u5f62\u53c2 \u5219\u5728\u51fd\u6570\u7ed3\u5c3e\u7ec8\u6b62\uff09\u3002\u8fd9\u786e\u4fdd\u4e86\u53d8\u91cf\u5728\u5b9a\u4e49\u524d\u4e0d\u80fd\u88ab\u4f7f\u7528\uff08\u5373\u4f7f\u7f16\u8bd1\u5668\u9009\u62e9\u5728\u6b64\u4e4b\u524d\u521b\u5efa\u53d8\u91cf\uff09\u3002\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d\u5b9a\u4e49\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u5176\u4f5c\u7528\u57df\u4e0d\u80fd\u5ef6\u4f38\u5230\u5176\u4ed6\u51fd\u6570\u4e2d\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u53d8\u91cf x \u7684\u4f5c\u7528\u57df\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include // x \u4e0d\u5728\u8be5\u51fd\u6570\u7684\u4f5c\u7528\u57df void doSomething () { std :: cout << \"Hello! \\n \" ; } int main () { // x \u6b64\u65f6\u8fd8\u4e0d\u80fd\u88ab\u8bbf\u95ee\uff0c\u56e0\u4e3a\u8fd8\u6ca1\u6709\u8fdb\u5165\u5176\u4f5c\u7528\u57df int x { 0 }; // x \u4ece\u6b64\u65f6\u8fdb\u5165\u4f5c\u7528\u57df\uff0c\u968f\u540e\u53ef\u4ee5\u5728\u8be5\u51fd\u6570\u4e2d\u4f7f\u7528\u5b83 doSomething (); return 0 ; } // x \u4f5c\u7528\u57df\u7ec8\u6b62\u4e0e\u6b64\uff0c\u6b64\u540e\u4e0d\u518d\u80fd\u591f\u88ab\u4f7f\u7528 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u53d8\u91cf x \u4ece\u5176\u5b9a\u4e49\u4f4d\u7f6e\u5f00\u59cb\u8fdb\u5165\u4f5c\u7528\u57df\uff0c\u5728 main \u51fd\u6570\u7ed3\u675f\u65f6\u79bb\u5f00\u5176\u4f5c\u7528\u57df\u3002\u6ce8\u610f\uff0c\u53d8\u91cf x \u7684\u4f5c\u7528\u57df\u4e0d\u4f1a\u5ef6\u4f38\u5230\u51fd\u6570 doSomething() \u4e2d\u3002\u8fd9\u4e5f\u4e00\u4f4d\u7f6e main \u51fd\u6570\u8c03\u7528 doSomething \u548c\u8be5\u8be5\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u6ca1\u6709\u4efb\u4f55\u5173\u7cfb\u3002","title":"\u5c40\u90e8\u4f5c\u7528\u57df"},{"location":"2-5-Introduction-to-local-scope/#_4","text":"\u672f\u8bed \u8d85\u51fa\u4f5c\u7528\u57df \u548c \u79bb\u5f00\u4f5c\u7528\u57df \u5bf9\u4e8e\u65b0\u624b\u6765\u8bf4\u53ef\u80fd\u4ee4\u4eba\u56f0\u60d1\u3002 \u6807\u8bc6\u7b26\u8d85\u51fa\u4f5c\u7528\u57df\u8868\u793a\u5176\u4e0d\u80fd\u5728\u4ee3\u7801\u4e2d\u88ab\u8bbf\u95ee\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c \u6807\u8bc6\u7b26(identifier) x \u8fdb\u5165\u4f5c\u7528\u57df\u7684\u65f6\u95f4\u4ece\u5176\u5b9a\u4e49\u5f00\u59cb\u5230 main \u51fd\u6570\u7ed3\u5c3e\u4e3a\u6b62\u3002\u8fd9\u6bb5\u4ee3\u7801\u4ee5\u5916\u7684\u4ee3\u7801\u4e2d\u8bbf\u95ee\u8be5\u6807\u8bc6\u7b26\uff0c\u90fd\u5c5e\u4e8e\u8d85\u51fa\u4f5c\u7528\u57df\u3002 \u672f\u8bed\u79bb\u5f00\u4f5c\u7528\u57df\u63cf\u8ff0\u7684\u5bf9\u8c61\u5219\u4e00\u822c\u662f\u67d0\u4e2a\u5bf9\u8c61\u800c\u4e0d\u662f\u6807\u8bc6\u7b26\u3002\u6211\u4eec\u8bf4\u4e00\u4e2a\u53d8\u91cf\u4f1a\u5728\u5b9e\u4f8b\u5316\u5b83\u4f5c\u7528\u57df\u7684\u7ed3\u5c3e\u5904\uff08\u82b1\u62ec\u53f7\u7ed3\u675f\u5904\uff09\u79bb\u5f00\u4f5c\u7528\u57df\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c \u5bf9\u8c61 x \u4f1a\u5728 main \u51fd\u6570\u7684\u7ed3\u5c3e\u5904\u79bb\u5f00\u4f5c\u7528\u57df\u3002 \u5177\u5907\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u4f1a\u5728\u5176\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\u7ec8\u6b62\uff0c\u5c40\u90e8\u53d8\u91cf\u4f1a\u5728\u6b64\u65f6\u88ab\u9500\u6bc1\u3002 \u6ce8\u610f\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u7c7b\u578b\u7684\u53d8\u91cf\u90fd\u4f1a\u5728\u5176\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\u88ab\u9500\u6bc1\u3002\u6211\u4eec\u4f1a\u5728\u5c06\u6765\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u6b64\u7c7b\u4f8b\u5b50\u3002","title":"\u201c\u8d85\u51fa\u4f5c\u7528\u57df\u201d \u548c \u201c\u79bb\u5f00\u4f5c\u7528\u57df\u201d"},{"location":"2-5-Introduction-to-local-scope/#_5","text":"\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u7a0d\u5fae\u66f4\u590d\u6742\u4e00\u5b9a\u3002\u8bf7\u8bb0\u4f4f\uff0c\u751f\u547d\u5468\u671f\u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\u5c5e\u6027\uff0c\u800c\u4f5c\u7528\u57df\u662f\u7f16\u8bd1\u65f6\u5c5e\u6027\u3002\u867d\u7136\uff0c\u6211\u4eec\u5728\u540c\u4e00\u6bb5\u4ee3\u7801\u4e2d\u8ba8\u8bba\u4e24\u8005\uff0c\u5b83\u4eec\u7684\u51fa\u53d1\u70b9\u662f\u4e0d\u540c\u7684\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int add ( int x , int y ) // x \u548c y \u5728\u6b64\u5904\u88ab\u521b\u5efa\u5e76\u8fdb\u5165\u4f5c\u7528\u57df { // x \u548c y \u4ec5\u5728\u8be5\u51fd\u6570\u4e2d\u53ef\u89c1/\u53ef\u7528 return x + y ; } // y \u548c x \u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1 int main () { int a { 5 }; // a \u5728\u6b64\u5904\u88ab\u521b\u5efa\u3001\u521d\u59cb\u5316\u5e76\u8fdb\u5165\u4f5c\u7528\u57df int b { 6 }; // b \u5728\u6b64\u5904\u88ab\u521b\u5efa\u3001\u521d\u59cb\u5316\u5e76\u8fdb\u5165\u4f5c\u7528\u57df // a \u548c b \u53ea\u5728\u8be5\u51fd\u6570\u4e2d\u53ef\u7528 std :: cout << add ( a , b ) << '\\n' ; // calls function add() with x=5 and y=6 return 0 ; } // b \u548c a \u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1 \u51fd\u6570\u5f62\u53c2 x \u548c y \u5728 add \u51fd\u6570\u88ab\u8c03\u7528\u65f6\u521b\u5efa\uff0c\u4f5c\u4e3a\u53d8\u91cf\uff0c\u5b83\u4eec\u53ea\u5728 add \u51fd\u6570\u4e2d\u53ef\u89c1\u53ef\u7528\uff0c\u5e76\u5728 add \u51fd\u6570\u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u53d8\u91cf a \u548c b \u5728\u51fd\u6570 main \u4e2d\u88ab\u521b\u5efa\uff0c\u53ea\u5728 main \u51fd\u6570\u4e2d\u53ef\u7528\uff0c\u5e76\u4e14\u5728 main \u51fd\u6570\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u3002 \u4e3a\u4e86\u5f3a\u5316\u7406\u89e3\uff0c\u8ba9\u6211\u4eec\u9010\u6b65\u89e3\u6790\u8be5\u51fd\u6570\uff0c\u4e0b\u5217\u64cd\u4f5c\u6309\u987a\u5e8f\u53d1\u751f\uff1a - \u7a0b\u5e8f\u4ece main \u51fd\u6570\u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\uff1b - main \u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf a \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 5\uff1b - main \u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf b \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 6\uff1b - add \u51fd\u6570\u88ab\u8c03\u7528\uff0c\u51fd\u6570\u7684\u5b9e\u53c2\u4e3a 5 \u548c 6\uff1b - add \u7684\u53d8\u91cf x \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 5\uff1b - add \u7684\u53d8\u91cf y \u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e3a 6\uff1b - \u8868\u8fbe\u5f0f x + y \u6c42\u503c\u5f97\u5230\u7ed3\u679c 11\uff1b - add \u5c06\u503c 11 \u62f7\u8d1d\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570 main \uff1b - add \u7684 y \u548c x \u88ab\u9500\u6bc1\uff1b - main \u5728\u63a7\u5236\u53f0\u6253\u5370 11\uff1b - main \u8fd4\u56de 0 \u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff1b - main \u7684\u5c40\u90e8\u53d8\u91cf b \u548c a \u88ab\u9500\u6bc1\uff1b \u4ee5\u4e0a\u3002 \u6ce8\u610f\uff0c\u5982\u679c\u51fd\u6570 add \u88ab\u5148\u540e\u4e24\u6b21\u8c03\u7528\uff0c\u90a3\u4e48\u5bf9\u5e94\u7684\u5f62\u53c2 x \u548c y \u4e5f\u88ab\u5148\u540e\u521b\u5efa\u5e76\u9500\u6bc1\u4e24\u6b21\u2014\u2014\u6bcf\u6b21\u8c03\u7528\u5c31\u4f1a\u53d1\u751f\u4e00\u6b21\u521b\u5efa\u548c\u9500\u6bc1\u3002\u5982\u679c\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\u6709\u5927\u91cf\u7684\u51fd\u6570\u8c03\u7528\uff0c\u90a3\u4e48\u53d8\u91cf\u4e5f\u4f1a\u968f\u4e4b\u88ab\u9891\u7e41\u7684\u521b\u5efa\u548c\u9500\u6bc1\u3002","title":"\u5176\u4ed6\u4f8b\u5b50"},{"location":"2-5-Introduction-to-local-scope/#_6","text":"\u5728\u4e0a\u8ff0\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f88\u660e\u663e\u53ef\u4ee5\u770b\u51fa\uff0c\u53d8\u91cf a \u548c b \u662f\u4e0d\u540c\u4e8e\u53d8\u91cf x \u548c y \u7684\u53d8\u91cf\u3002 \u73b0\u5728\uff0c\u8bf7\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int add ( int x , int y ) // x \u548c y \u5728\u6b64\u5904\u88ab\u521b\u5efa\u5e76\u8fdb\u5165\u4f5c\u7528\u57df { // x \u548c y \u4ec5\u5728\u8be5\u51fd\u6570\u4e2d\u53ef\u89c1/\u53ef\u7528 return x + y ; } // y \u548c x \u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1 int main () { int x { 5 }; // x \u5728\u6b64\u5904\u88ab\u521b\u5efa\u3001\u521d\u59cb\u5316\u5e76\u8fdb\u5165\u4f5c\u7528\u57df int y { 6 }; // y \u5728\u6b64\u5904\u88ab\u521b\u5efa\u3001\u521d\u59cb\u5316\u5e76\u8fdb\u5165\u4f5c\u7528\u57df // x \u548c y \u53ea\u5728\u8be5\u51fd\u6570\u4e2d\u53ef\u7528 std :: cout << add ( x , y ) << '\\n' ; // calls function add() with x=5 and y=6 return 0 ; } // x \u548c y \u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1 \u4e0e\u524d\u4e00\u4e2a\u4f8b\u5b50\u76f8\u6bd4\uff0c\u672c\u4f8b\u53ea\u662f\u5c06 main \u51fd\u6570\u4e2d\u7684\u53d8\u91cf a \u548c b \u4fee\u6539\u6210\u4e86 x \u548c y \u3002\u8fd9\u4e2a\u7a0b\u5e8f\u7684\u7f16\u8bd1\u548c\u8fd0\u884c\u548c\u4e4b\u524d\u7684\u7a0b\u5e8f\u6ca1\u6709\u4efb\u4f55\u533a\u522b\uff0c\u5373\u4f7f main \u51fd\u6570\u548c add \u51fd\u6570\u90fd\u4f7f\u7528\u4e86\u53d8\u91cf\u540d x \u548c y \u3002\u4e3a\u4ec0\u4e48\u8fd9\u6837\u4e5f\u53ef\u4ee5\u5de5\u4f5c\u5462\uff1f \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u8ba4\u8bc6\u5230\u7684\u662f\uff0c\u5373\u4f7f main \u51fd\u6570\u548c add \u51fd\u6570\u90fd\u4f7f\u7528\u4e86\u53d8\u91cf\u540d x \u548c y \uff0c\u4f46\u662f\u8fd9\u4e9b\u53d8\u91cf\u5b9e\u9645\u4e0a\u662f\u5b8c\u5168\u4e0d\u540c\u7684\u3002 main \u51fd\u6570\u4e2d\u7684 x \u548c y \uff0c\u548c add \u51fd\u6570\u4e2d\u7684 x \u548c y \u662f\u6ca1\u6709\u4efb\u4f55\u5173\u7cfb\u7684\u2014\u2014\u5b83\u4eec\u53ea\u662f\u78b0\u5de7\u540c\u540d\u7f62\u4e86\u3002 \u5176\u6b21\uff0c\u5728 main \u51fd\u6570\u4e2d\uff0c \u53d8\u91cf\u540d x \u548c y \u6307\u7684\u662f main \u51fd\u6570\u5c40\u90e8\u4f5c\u7528\u4e2d\u7684\u53d8\u91cf x \u548c y \u3002\u8fd9\u4e9b\u53d8\u91cf\u53ea\u80fd\u591f\u5728 main \u51fd\u6570\u4e2d\u88ab\u770b\u5230\uff08\u88ab\u4f7f\u7528\uff09\u3002\u540c\u6837\u7684\uff0c\u5728 add \u51fd\u6570\u4e2d\uff0c, \u53d8\u91cf\u540d x \u548c y \u6307\u7684\u662f add \u51fd\u6570\u7684\u5f62\u53c2 x \u548c y \uff0c\u5b83\u4eec\u4e5f\u53ea\u80fd\u591f\u5728 add \u4e2d\u88ab\u770b\u5230\u548c\u4f7f\u7528\u3002 \u957f\u8bdd\u77ed\u8bf4\uff0c\u5176\u5b9e add \u548c main \u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u4e0d\u77e5\u9053\u5bf9\u65b9\u4f7f\u7528\u4e86\u4e0e\u81ea\u5df1\u76f8\u540c\u7684\u53d8\u91cf\u540d\u3002\u56e0\u4e3a\uff0c\u8fd9\u4e9b\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u5e76\u6ca1\u6709\u91cd\u53e0\u7684\u90e8\u5206\uff0c\u56e0\u6b64\u5bf9\u4e8e\u7f16\u8bd1\u5668\u6765\u8bf4\uff0c x \u548c y \u5728\u4efb\u4f55\u65f6\u5019\u88ab\u5f15\u7528\uff0c\u5176\u6307\u4ee3\u7684\u5bf9\u8c61\u90fd\u662f\u6e05\u695a\u7684\u3002 \u5173\u952e\u4fe1\u606f \u51fd\u6570\u5f62\u53c2\u6216\u8005\u662f\u5c40\u90e8\u53d8\u91cf\u5728\u58f0\u660e\u7684\u65f6\u5019\u6240\u4f7f\u7528\u7684\u540d\u5b57\uff0c\u53ea\u5728\u51fd\u6570\u4f53\u5185\u90e8\u53ef\u89c1\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u51fd\u6570\u5728\u5b9a\u4e49\u5c40\u90e8\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u4e0d\u8003\u8651\u5176\u4ed6\u51fd\u6570\u662f\u5426\u4f7f\u7528\u4e86\u67d0\u4e9b\u53d8\u91cf\u540d\u3002\u8fd9\u4e00\u7279\u6027\uff0c\u53ef\u4ee5\u5e2e\u52a9\u51fd\u6570\u4fdd\u6301\u72ec\u7acb\u6027\u3002 \u5728\u540e\u7eed\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u4f1a\u63a2\u8ba8\u5173\u4e8e\u5c40\u90e8\u4f5c\u7528\u57df\u7684\u66f4\u591a\u5185\u5bb9\uff0c\u540c\u65f6\u4e5f\u4f1a\u8c08\u5230\u5176\u4ed6\u7c7b\u578b\u7684\u4f5c\u7528\u57df\u3002","title":"\u51fd\u6570\u7684\u9694\u79bb"},{"location":"2-5-Introduction-to-local-scope/#_7","text":"\u51fd\u6570\u4f53\u5185\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u5176\u5b9a\u4e49\u7684\u4f4d\u7f6e\u5e94\u8be5\u5c3d\u53ef\u80fd\u63a5\u8fd1\u5176\u7b2c\u4e00\u6b21\u88ab\u4f7f\u7528\u7684\u4f4d\u7f6e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; // x \u5728\u6b64\u5904\u5b9a\u4e49 std :: cin >> x ; // \u5728\u6b64\u5904\u4f7f\u7528 std :: cout << \"Enter another integer: \" ; int y {}; // y \u5728\u6b64\u5904\u5b9a\u4e49 std :: cin >> y ; // \u5728\u6b64\u5904\u4f7f\u7528 int sum { x + y }; // sum \u5728\u6b64\u5904\u5b9a\u4e49 std :: cout << \"The sum is: \" << sum << '\\n' ; // \u5728\u6b64\u5904\u4f7f\u7528 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6bcf\u4e2a\u53d8\u91cf\u90fd\u662f\u5728\u5b83\u7b2c\u4e00\u6b21\u88ab\u4f7f\u7528\u65f6\u7684\u524d\u4e00\u884c\u5b9a\u4e49\u7684\u3002\u8fd9\u4e2a\u89c4\u5b9a\u5012\u662f\u65e0\u9700\u4e25\u683c\u9075\u5b88\u2014\u2014\u5982\u679c\u4f60\u5e0c\u671b\u5c06\u7b2c\u4e94\u884c\u548c\u7b2c\u516d\u884c\u7684\u4f4d\u7f6e\u6362\u4e00\u4e0b\u4e5f\u662f\u53ef\u4ee5\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c3d\u91cf\u5728\u5c40\u90e8\u53d8\u91cf\u7b2c\u4e00\u6b21\u88ab\u4f7f\u7528\u524d\u6700\u8fd1\u7684\u5730\u65b9\u5b9a\u4e49\u8be5\u5c40\u90e8\u53d8\u91cf\u3002","title":"\u5728\u4f55\u5904\u5b9a\u4e49\u5c40\u90e8\u53d8\u91cf"},{"location":"2-6-Why-functions-are-useful-and-how-to-use-them-effectively/","tags":["function"],"text":"\u901a\u8fc7\u524d\u9762\u7684\u5b66\u4e60\uff0c\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u4e86\u4ec0\u4e48\u662f\u51fd\u6570\u4ee5\u53ca\u51fd\u6570\u7684\u57fa\u672c\u529f\u80fd\uff0c\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u770b\u770b\uff0c\u4e3a\u4ec0\u4e48\u8bf4\u51fd\u6570\u975e\u5e38\u6709\u7528\u3002 \u51fd\u6570\u4e3a\u4ec0\u4e48\u6709\u7528 \u65b0\u624b\u7a0b\u5e8f\u5458\u603b\u4f1a\u95ee\uff1a\u201c\u96be\u9053\u6211\u4eec\u5c31\u4e0d\u80fd\u628a\u6240\u6709\u7684\u4ee3\u7801\u90fd\u585e\u5230 main \u51fd\u6570\u91cc\u9762\u5417\uff1f\u201d \uff0c\u5176\u5b9e\u5bf9\u4e8e\u975e\u5e38\u7b80\u5355\u7684\u7a0b\u5e8f\uff0c\u4f60\u7684\u786e\u53ef\u4ee5\u5c06\u6240\u6709\u7684\u4ee3\u7801\u90fd\u585e\u5230 main \u51fd\u6570\u91cc\u9762\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u4ee3\u7801\u91cf\u6bd4\u8f83\u591a\u6216\u590d\u6742\u5ea6\u8f83\u9ad8\u7684\u7a0b\u5e8f\u6765\u8bf4\uff0c\u4f7f\u7528\u51fd\u6570\u80fd\u591f\u5e26\u6765\u5f88\u591a\u597d\u5904\uff1a \u4fbf\u4e8e\u7ec4\u7ec7\u4ee3\u7801 -- \u968f\u7740\u7a0b\u5e8f\u7684\u590d\u6742\u6027\u8d8a\u6765\u8d8a\u9ad8\uff0c\u5c06\u6240\u6709\u7684\u4ee3\u7801\u90fd\u662f\u585e\u5230 main \u51fd\u6570\u4e2d\u4f1a\u4f7f main \u51fd\u6570\u53d8\u5f97\u975e\u5e38\u590d\u6742\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u51fd\u6570\u770b\u505a\u662f\u4e00\u4e2a\u4e2a\u72ec\u7acb\u4e8e\u4e3b\u7a0b\u5e8f\u7684\u8ff7\u4f60\u7a0b\u5e8f\uff0c\u5728\u7f16\u5199\u5b83\u4eec\u7684\u65f6\u5019\u6211\u4eec\u65e0\u9700\u8003\u8651\u5176\u4ed6\u7a0b\u5e8f\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u4e00\u4e2a\u590d\u6742\u7684\u7a0b\u5e8f\u7b80\u5316\u4e3a\u591a\u4e2a\u66f4\u5c0f\u3001\u66f4\u6613\u7ef4\u62a4\u7684\u6bb5\u843d\uff0c\u4ece\u800c\u964d\u4f4e\u6574\u4e2a\u7a0b\u5e8f\u7684\u590d\u6742\u5ea6\uff1b \u63d0\u9ad8\u53ef\u91cd\u7528\u6027 -- \u5f53\u51fd\u6570\u88ab\u7f16\u5199\u540e\uff0c\u5b83\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u88ab\u591a\u6b21\u8c03\u7528\u3002\u8fd9\u4e48\u505a\u53ef\u4ee5\u907f\u514d\u91cd\u590d\u4ee3\u7801 (\u201cDon\u2019t Repeat Yourself\u201d\u539f\u5219)\u800c\u4e14\u80fd\u591f\u6700\u5927\u7a0b\u5ea6\u4e0a\u51cf\u5c11\u590d\u5236\u7c98\u8d34\u4ee3\u7801\u6240\u53ef\u80fd\u5e26\u6765\u7684\u9519\u8bef\u3002\u51fd\u6570\u8fd8\u53ef\u4ee5\u5728\u7a0b\u5e8f\u95f4\u5171\u4eab\uff0c\u8fd9\u4e48\u505a\u53ef\u4ee5\u907f\u514d\u91cd\u590d\u7f16\u5199\uff08\u6216\u6d4b\u8bd5\uff09\u4ee3\u7801\u3002\uff1b \u4fbf\u4e8e\u6d4b\u8bd5\u4ee3\u7801 -- \u51fd\u6570\u53ef\u4ee5\u51cf\u5c11\u5197\u4f59\u4ee3\u7801\uff0c\u56e0\u6b64\u9996\u5148\u5c31\u53ef\u4ee5\u51cf\u5c11\u9700\u8981\u6d4b\u8bd5\u7684\u4ee3\u7801\u6570\u91cf\u3002\u540c\u65f6\uff0c\u7531\u4e8e\u51fd\u6570\u662f\u81ea\u5305\u542b\u7684\uff0c\u56e0\u6b64\u5f53\u51fd\u6570\u901a\u8fc7\u6d4b\u8bd5\u5e76\u786e\u4fdd\u6b63\u786e\u5de5\u4f5c\u540e\uff0c\u9664\u975e\u5b83\u88ab\u4fee\u6539\u4e86\uff0c\u5426\u5219\u5c06\u65e0\u9700\u5bf9\u5176\u8fdb\u884c\u6d4b\u8bd5\u3002\u8fd9\u4e48\u505a\u663e\u7136\u53ef\u4ee5\u51cf\u5c11\u5f88\u591a\u9700\u8981\u6d4b\u8bd5\u7684\u4ee3\u7801\uff0c\u53d1\u73b0bug\u7684\u96be\u5ea6\u4e5f\u5c31\u968f\u4e4b\u964d\u4f4e\u4e86\uff08\u4e5f\u66f4\u52a0\u5bb9\u6613\u907f\u514dbug\uff09\uff1b \u63d0\u9ad8\u53ef\u6269\u5c55\u6027 -- \u5f53\u6211\u4eec\u9700\u8981\u5bf9\u7a0b\u5e8f\u8fdb\u884c\u6269\u5c55\uff0c\u4ee5\u4f7f\u5176\u80fd\u591f\u5904\u7406\u4e4b\u524d\u4e0d\u80fd\u5904\u7406\u7684\u60c5\u51b5\u65f6\uff0c\u51fd\u6570\u80fd\u591f\u4f7f\u6211\u4eec\u5728\u4e00\u5904\u8fdb\u884c\u6539\u52a8\u5e76\u5c06\u6539\u52a8\u7684\u6548\u679c\u5e94\u7528\u5230\u6240\u6709\u8c03\u7528\u4e86\u8be5\u51fd\u6570\u7684\u5730\u65b9\uff1b \u63d0\u9ad8\u62bd\u8c61\u7a0b\u5ea6 -- \u4e3a\u4e86\u4f7f\u7528\u67d0\u4e2a\u51fd\u6570\uff0c\u4f60\u5fc5\u987b\u77e5\u6653\u5176\u51fd\u6570\u540d\u3001\u8f93\u5165\u3001\u8f93\u51fa\u4ee5\u53ca\u5b83\u6240\u5904\u7684\u4f4d\u7f6e\u3002\u4f46\u662f\uff0c\u4f60\u4e0d\u5fc5\u77e5\u9053\u5b83\u5982\u4f55\u5de5\u4f5c\u6216\u8005\u4efb\u4f55\u4e0e\u4f7f\u7528\u5176\u529f\u80fd\u4e0d\u76f8\u5e72\u7684\u4fe1\u606f\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u5728\u4f7f\u7528\u4ed6\u4eba\u7684\u4ee3\u7801\uff08\u6216\u8005\u4efb\u4f55\u6807\u51c6\u5e93\u4e2d\u7684\u51fd\u6570\uff09\u65f6\uff0c\u4f60\u7684\u8ba4\u77e5\u8d1f\u8f7d\u5c31\u964d\u4f4e\u4e86\u3002 \u5c3d\u7ba1\u770b\u4e0a\u53bb\u4e0d\u50cf\uff0c\u4f46\u5b9e\u9645\u4e0a\u6bcf\u5f53\u4f60\u4f7f\u7528 operator<< \u6216 operator>> \u8fd0\u7b97\u7b26\u65f6\uff0c\u4f60\u5b9e\u9645\u4e0a\u5728\u4f7f\u7528\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u540c\u6837\u5177\u6709\u4e0a\u8ff0\u5168\u90e8\u7684\u4f18\u70b9\u3002 \u9ad8\u6548\u5730\u4f7f\u7528\u51fd\u6570 \u65b0\u624b\u7a0b\u5e8f\u5458\u9047\u5230\u7684\uff08\u9664\u4e86\u5b66\u4e60\u8bed\u8a00\u672c\u8eab\uff09\u6700\u5927\u6311\u6218\u4fbf\u662f\u7406\u89e3\u4f55\u65f6\uff0c\u4ee5\u53ca\u5982\u4f55\u9ad8\u6548\u5730\u4f7f\u7528\u51fd\u6570\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5173\u4e8e\u7f16\u5199\u51fd\u6570\u7684\u6307\u5bfc\u539f\u5219\uff1a \u5728\u7a0b\u5e8f\u4e2d\u91cd\u590d\u51fa\u73b0\u7684\u4e00\u7ec4\u8bed\u53e5\uff0c\u901a\u5e38\u53ef\u4ee5\u88ab\u5c01\u88c5\u6210\u4e00\u4e2a\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u540c\u6837\u7684\u65b9\u5f0f\u591a\u6b21\u4ece\u7528\u6237\u90a3\u91cc\u83b7\u53d6\u8f93\u5165\uff0c\u90a3\u4e48\u8fd9\u4e9b\u91cd\u590d\u4ee3\u7801\u5c31\u5f88\u9002\u5408\u88ab\u5c01\u88c5\u6210\u51fd\u6570\u3002\u5982\u679c\u6211\u4eec\u4ee5\u540c\u6837\u7684\u5f62\u5f0f\u5728\u591a\u5904\u8fdb\u884c\u8f93\u51fa\uff0c\u8fd9\u540c\u6837\u4e5f\u662f\u975e\u5e38\u9002\u5408\u5c01\u88c5\u51fd\u6570\u7684\u60c5\u666f\uff1b \u660e\u786e\u5b9a\u4e49\u4e86\u8f93\u5165\u548c\u8f93\u51fa\u7684\u4ee3\u7801\u975e\u5e38\u9002\u5408\u5c01\u88c5\u6210\u51fd\u6570\uff08\u5c24\u5176\u662f\u5f53\u8fd9\u4e9b\u4ee3\u7801\u975e\u5e38\u590d\u6742\u7684\u65f6\u5019\uff09\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u9700\u8981\u6392\u5e8f\u7684\u5217\u8868\uff0c\u90a3\u4e48\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\u7684\u4ee3\u7801\u5c31\u975e\u5e38\u9002\u5408\u4f5c\u4e3a\u51fd\u6570\uff0c\u65e2\u662f\u8be5\u6392\u5e8f\u53ef\u80fd\u53ea\u8fdb\u884c\u4f9d\u6b21\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8f93\u5165\u662f\u4e00\u4e2a\u672a\u6392\u5e8f\u7684\u5217\u8868\uff0c\u8f93\u51fa\u5219\u662f\u4e00\u4e2a\u6392\u5e8f\u540e\u7684\u5217\u8868\u3002\u8fd8\u6709\u5f88\u597d\u7684\u4f8b\u5b50\u662f\uff0c\u5982\u679c\u4f60\u7f16\u5199\u4e86\u4e00\u4e9b\u6a21\u62df\u63b7\u9ab0\u5b50\u7684\u7a0b\u5e8f\uff0c\u90a3\u4e48\u5b83\u4e5f\u5f88\u9002\u5408\u4f5c\u4e3a\u51fd\u6570\u3002\u5c3d\u7ba1\u5728\u5f53\u524d\u7684\u7a0b\u5e8f\u4e2d\u4f60\u53ef\u80fd\u53ea\u4f1a\u4f7f\u7528\u5b83\u4e00\u6b21\uff0c\u4f46\u662f\u5c06\u6765\u5982\u679c\u4f60\u60f3\u6269\u5c55\u7a0b\u5e8f\u6216\u7f16\u5199\u5176\u4ed6\u7a0b\u5e8f\uff0c\u8be5\u529f\u80fd\u5c31\u53ef\u4ee5\u76f4\u63a5\u88ab\u590d\u7528\uff1b \u4e00\u4e2a\u51fd\u6570\u5e94\u8be5\u53ea\u505a\u4e00\u4ef6\u4e8b\uff1b \u5982\u679c\u4e00\u4e2a\u51fd\u6570\u53d8\u5f97\u53c8\u957f\u3001\u53c8\u590d\u6742\u3001\u6216\u8005\u96be\u4ee5\u7406\u89e3\u4e86\uff0c\u90a3\u4e48\u5b83\u5c31\u5e94\u8be5\u88ab\u518d\u6b21\u62c6\u5206\uff0c\u53d8\u6210\u51e0\u4e2a\u5b50\u51fd\u6570\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a \u91cd\u6784(refactoring) \u3002\u6211\u4eec\u4f1a\u5728 3.10 - \u5728\u95ee\u9898\u53d1\u751f\u524d\u53d1\u73b0\u95ee\u9898 \u4e2d\u4ecb\u7ecd\u91cd\u6784\u7684\u6982\u5ff5\u3002 \u901a\u5e38\uff0c\u5728\u5b66\u4e60C++\u7684\u65f6\u5019\uff0c\u4f60\u7f16\u5199\u7684\u5f88\u591a\u7a0b\u5e8f\u90fd\u6d89\u53ca\u5230\u5982\u4e0b\u4e09\u79cd\u7c7b\u578b\u7684\u5b50\u4efb\u52a1\uff1a \u4ece\u7528\u6237\u83b7\u53d6\u8f93\u5165\uff1b \u57fa\u4e8e\u8f93\u5165\u8fdb\u884c\u8ba1\u7b97\uff1b \u6253\u5370\u8ba1\u7b97\u7684\u7ed3\u679c\u3002 \u5bf9\u4e8e\u77ed\u5c0f\u7684\u7a0b\u5e8f (\u4f8b\u5982\uff0c\u5c0f\u4e8e20\u884c\u4ee3\u7801\u7684\u7a0b\u5e8f\uff09\uff0c\u4e0a\u8ff0\u4e09\u4e2a\u5b50\u4efb\u52a1\u90fd\u53ef\u4ee5\u5728 main \u51fd\u6570\u4e2d\u5b8c\u6210\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u66f4\u957f\uff08\u6216\u8005\u51fa\u4e8e\u7ec3\u4e60\u7684\u76ee\u7684\uff09\uff0c\u4e0a\u8ff0\u4e09\u4e2a\u4efb\u52a1\u90fd\u5f88\u9002\u5408\u5404\u81ea\u4f5c\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u51fd\u6570\u3002 \u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u5c06\u8ba1\u7b97\u7ed3\u679c\u548c\u6253\u5370\u7ed3\u679c\u8fd9\u4e24\u6b65\u5408\u5e76\u4e3a\u4e00\u4e2a\u51fd\u6570\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u8fdd\u53cd\u4e86\u201c\u4e00\u4e2a\u51fd\u6570\u53ea\u505a\u4e00\u4ef6\u4e8b\u201d \u7684\u539f\u5219\u3002\u8d1f\u8d23\u8ba1\u7b97\u7ed3\u679c\u7684\u51fd\u6570\u5e94\u8be5\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\uff0c\u7136\u540e\u8ba9\u4e3b\u8c03\u51fd\u6570\u51b3\u5b9a\u5982\u4f55\u4f7f\u7528\u8be5\u8fd4\u56de\u503c\uff08\u4f8b\u5982\u8c03\u7528\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\u5c06\u8be5\u7ed3\u679c\u6253\u5370\u51fa\u6765\uff09\u3002","title":"2.6 - \u4e3a\u4ec0\u4e48\u51fd\u6570\u6709\u7528\uff0c\u5982\u4f55\u9ad8\u6548\u4f7f\u7528\u51fd\u6570"},{"location":"2-6-Why-functions-are-useful-and-how-to-use-them-effectively/#_1","text":"\u65b0\u624b\u7a0b\u5e8f\u5458\u603b\u4f1a\u95ee\uff1a\u201c\u96be\u9053\u6211\u4eec\u5c31\u4e0d\u80fd\u628a\u6240\u6709\u7684\u4ee3\u7801\u90fd\u585e\u5230 main \u51fd\u6570\u91cc\u9762\u5417\uff1f\u201d \uff0c\u5176\u5b9e\u5bf9\u4e8e\u975e\u5e38\u7b80\u5355\u7684\u7a0b\u5e8f\uff0c\u4f60\u7684\u786e\u53ef\u4ee5\u5c06\u6240\u6709\u7684\u4ee3\u7801\u90fd\u585e\u5230 main \u51fd\u6570\u91cc\u9762\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u4ee3\u7801\u91cf\u6bd4\u8f83\u591a\u6216\u590d\u6742\u5ea6\u8f83\u9ad8\u7684\u7a0b\u5e8f\u6765\u8bf4\uff0c\u4f7f\u7528\u51fd\u6570\u80fd\u591f\u5e26\u6765\u5f88\u591a\u597d\u5904\uff1a \u4fbf\u4e8e\u7ec4\u7ec7\u4ee3\u7801 -- \u968f\u7740\u7a0b\u5e8f\u7684\u590d\u6742\u6027\u8d8a\u6765\u8d8a\u9ad8\uff0c\u5c06\u6240\u6709\u7684\u4ee3\u7801\u90fd\u662f\u585e\u5230 main \u51fd\u6570\u4e2d\u4f1a\u4f7f main \u51fd\u6570\u53d8\u5f97\u975e\u5e38\u590d\u6742\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u51fd\u6570\u770b\u505a\u662f\u4e00\u4e2a\u4e2a\u72ec\u7acb\u4e8e\u4e3b\u7a0b\u5e8f\u7684\u8ff7\u4f60\u7a0b\u5e8f\uff0c\u5728\u7f16\u5199\u5b83\u4eec\u7684\u65f6\u5019\u6211\u4eec\u65e0\u9700\u8003\u8651\u5176\u4ed6\u7a0b\u5e8f\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u4e00\u4e2a\u590d\u6742\u7684\u7a0b\u5e8f\u7b80\u5316\u4e3a\u591a\u4e2a\u66f4\u5c0f\u3001\u66f4\u6613\u7ef4\u62a4\u7684\u6bb5\u843d\uff0c\u4ece\u800c\u964d\u4f4e\u6574\u4e2a\u7a0b\u5e8f\u7684\u590d\u6742\u5ea6\uff1b \u63d0\u9ad8\u53ef\u91cd\u7528\u6027 -- \u5f53\u51fd\u6570\u88ab\u7f16\u5199\u540e\uff0c\u5b83\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u88ab\u591a\u6b21\u8c03\u7528\u3002\u8fd9\u4e48\u505a\u53ef\u4ee5\u907f\u514d\u91cd\u590d\u4ee3\u7801 (\u201cDon\u2019t Repeat Yourself\u201d\u539f\u5219)\u800c\u4e14\u80fd\u591f\u6700\u5927\u7a0b\u5ea6\u4e0a\u51cf\u5c11\u590d\u5236\u7c98\u8d34\u4ee3\u7801\u6240\u53ef\u80fd\u5e26\u6765\u7684\u9519\u8bef\u3002\u51fd\u6570\u8fd8\u53ef\u4ee5\u5728\u7a0b\u5e8f\u95f4\u5171\u4eab\uff0c\u8fd9\u4e48\u505a\u53ef\u4ee5\u907f\u514d\u91cd\u590d\u7f16\u5199\uff08\u6216\u6d4b\u8bd5\uff09\u4ee3\u7801\u3002\uff1b \u4fbf\u4e8e\u6d4b\u8bd5\u4ee3\u7801 -- \u51fd\u6570\u53ef\u4ee5\u51cf\u5c11\u5197\u4f59\u4ee3\u7801\uff0c\u56e0\u6b64\u9996\u5148\u5c31\u53ef\u4ee5\u51cf\u5c11\u9700\u8981\u6d4b\u8bd5\u7684\u4ee3\u7801\u6570\u91cf\u3002\u540c\u65f6\uff0c\u7531\u4e8e\u51fd\u6570\u662f\u81ea\u5305\u542b\u7684\uff0c\u56e0\u6b64\u5f53\u51fd\u6570\u901a\u8fc7\u6d4b\u8bd5\u5e76\u786e\u4fdd\u6b63\u786e\u5de5\u4f5c\u540e\uff0c\u9664\u975e\u5b83\u88ab\u4fee\u6539\u4e86\uff0c\u5426\u5219\u5c06\u65e0\u9700\u5bf9\u5176\u8fdb\u884c\u6d4b\u8bd5\u3002\u8fd9\u4e48\u505a\u663e\u7136\u53ef\u4ee5\u51cf\u5c11\u5f88\u591a\u9700\u8981\u6d4b\u8bd5\u7684\u4ee3\u7801\uff0c\u53d1\u73b0bug\u7684\u96be\u5ea6\u4e5f\u5c31\u968f\u4e4b\u964d\u4f4e\u4e86\uff08\u4e5f\u66f4\u52a0\u5bb9\u6613\u907f\u514dbug\uff09\uff1b \u63d0\u9ad8\u53ef\u6269\u5c55\u6027 -- \u5f53\u6211\u4eec\u9700\u8981\u5bf9\u7a0b\u5e8f\u8fdb\u884c\u6269\u5c55\uff0c\u4ee5\u4f7f\u5176\u80fd\u591f\u5904\u7406\u4e4b\u524d\u4e0d\u80fd\u5904\u7406\u7684\u60c5\u51b5\u65f6\uff0c\u51fd\u6570\u80fd\u591f\u4f7f\u6211\u4eec\u5728\u4e00\u5904\u8fdb\u884c\u6539\u52a8\u5e76\u5c06\u6539\u52a8\u7684\u6548\u679c\u5e94\u7528\u5230\u6240\u6709\u8c03\u7528\u4e86\u8be5\u51fd\u6570\u7684\u5730\u65b9\uff1b \u63d0\u9ad8\u62bd\u8c61\u7a0b\u5ea6 -- \u4e3a\u4e86\u4f7f\u7528\u67d0\u4e2a\u51fd\u6570\uff0c\u4f60\u5fc5\u987b\u77e5\u6653\u5176\u51fd\u6570\u540d\u3001\u8f93\u5165\u3001\u8f93\u51fa\u4ee5\u53ca\u5b83\u6240\u5904\u7684\u4f4d\u7f6e\u3002\u4f46\u662f\uff0c\u4f60\u4e0d\u5fc5\u77e5\u9053\u5b83\u5982\u4f55\u5de5\u4f5c\u6216\u8005\u4efb\u4f55\u4e0e\u4f7f\u7528\u5176\u529f\u80fd\u4e0d\u76f8\u5e72\u7684\u4fe1\u606f\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u5728\u4f7f\u7528\u4ed6\u4eba\u7684\u4ee3\u7801\uff08\u6216\u8005\u4efb\u4f55\u6807\u51c6\u5e93\u4e2d\u7684\u51fd\u6570\uff09\u65f6\uff0c\u4f60\u7684\u8ba4\u77e5\u8d1f\u8f7d\u5c31\u964d\u4f4e\u4e86\u3002 \u5c3d\u7ba1\u770b\u4e0a\u53bb\u4e0d\u50cf\uff0c\u4f46\u5b9e\u9645\u4e0a\u6bcf\u5f53\u4f60\u4f7f\u7528 operator<< \u6216 operator>> \u8fd0\u7b97\u7b26\u65f6\uff0c\u4f60\u5b9e\u9645\u4e0a\u5728\u4f7f\u7528\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u540c\u6837\u5177\u6709\u4e0a\u8ff0\u5168\u90e8\u7684\u4f18\u70b9\u3002","title":"\u51fd\u6570\u4e3a\u4ec0\u4e48\u6709\u7528"},{"location":"2-6-Why-functions-are-useful-and-how-to-use-them-effectively/#_2","text":"\u65b0\u624b\u7a0b\u5e8f\u5458\u9047\u5230\u7684\uff08\u9664\u4e86\u5b66\u4e60\u8bed\u8a00\u672c\u8eab\uff09\u6700\u5927\u6311\u6218\u4fbf\u662f\u7406\u89e3\u4f55\u65f6\uff0c\u4ee5\u53ca\u5982\u4f55\u9ad8\u6548\u5730\u4f7f\u7528\u51fd\u6570\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5173\u4e8e\u7f16\u5199\u51fd\u6570\u7684\u6307\u5bfc\u539f\u5219\uff1a \u5728\u7a0b\u5e8f\u4e2d\u91cd\u590d\u51fa\u73b0\u7684\u4e00\u7ec4\u8bed\u53e5\uff0c\u901a\u5e38\u53ef\u4ee5\u88ab\u5c01\u88c5\u6210\u4e00\u4e2a\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u540c\u6837\u7684\u65b9\u5f0f\u591a\u6b21\u4ece\u7528\u6237\u90a3\u91cc\u83b7\u53d6\u8f93\u5165\uff0c\u90a3\u4e48\u8fd9\u4e9b\u91cd\u590d\u4ee3\u7801\u5c31\u5f88\u9002\u5408\u88ab\u5c01\u88c5\u6210\u51fd\u6570\u3002\u5982\u679c\u6211\u4eec\u4ee5\u540c\u6837\u7684\u5f62\u5f0f\u5728\u591a\u5904\u8fdb\u884c\u8f93\u51fa\uff0c\u8fd9\u540c\u6837\u4e5f\u662f\u975e\u5e38\u9002\u5408\u5c01\u88c5\u51fd\u6570\u7684\u60c5\u666f\uff1b \u660e\u786e\u5b9a\u4e49\u4e86\u8f93\u5165\u548c\u8f93\u51fa\u7684\u4ee3\u7801\u975e\u5e38\u9002\u5408\u5c01\u88c5\u6210\u51fd\u6570\uff08\u5c24\u5176\u662f\u5f53\u8fd9\u4e9b\u4ee3\u7801\u975e\u5e38\u590d\u6742\u7684\u65f6\u5019\uff09\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u9700\u8981\u6392\u5e8f\u7684\u5217\u8868\uff0c\u90a3\u4e48\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\u7684\u4ee3\u7801\u5c31\u975e\u5e38\u9002\u5408\u4f5c\u4e3a\u51fd\u6570\uff0c\u65e2\u662f\u8be5\u6392\u5e8f\u53ef\u80fd\u53ea\u8fdb\u884c\u4f9d\u6b21\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8f93\u5165\u662f\u4e00\u4e2a\u672a\u6392\u5e8f\u7684\u5217\u8868\uff0c\u8f93\u51fa\u5219\u662f\u4e00\u4e2a\u6392\u5e8f\u540e\u7684\u5217\u8868\u3002\u8fd8\u6709\u5f88\u597d\u7684\u4f8b\u5b50\u662f\uff0c\u5982\u679c\u4f60\u7f16\u5199\u4e86\u4e00\u4e9b\u6a21\u62df\u63b7\u9ab0\u5b50\u7684\u7a0b\u5e8f\uff0c\u90a3\u4e48\u5b83\u4e5f\u5f88\u9002\u5408\u4f5c\u4e3a\u51fd\u6570\u3002\u5c3d\u7ba1\u5728\u5f53\u524d\u7684\u7a0b\u5e8f\u4e2d\u4f60\u53ef\u80fd\u53ea\u4f1a\u4f7f\u7528\u5b83\u4e00\u6b21\uff0c\u4f46\u662f\u5c06\u6765\u5982\u679c\u4f60\u60f3\u6269\u5c55\u7a0b\u5e8f\u6216\u7f16\u5199\u5176\u4ed6\u7a0b\u5e8f\uff0c\u8be5\u529f\u80fd\u5c31\u53ef\u4ee5\u76f4\u63a5\u88ab\u590d\u7528\uff1b \u4e00\u4e2a\u51fd\u6570\u5e94\u8be5\u53ea\u505a\u4e00\u4ef6\u4e8b\uff1b \u5982\u679c\u4e00\u4e2a\u51fd\u6570\u53d8\u5f97\u53c8\u957f\u3001\u53c8\u590d\u6742\u3001\u6216\u8005\u96be\u4ee5\u7406\u89e3\u4e86\uff0c\u90a3\u4e48\u5b83\u5c31\u5e94\u8be5\u88ab\u518d\u6b21\u62c6\u5206\uff0c\u53d8\u6210\u51e0\u4e2a\u5b50\u51fd\u6570\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a \u91cd\u6784(refactoring) \u3002\u6211\u4eec\u4f1a\u5728 3.10 - \u5728\u95ee\u9898\u53d1\u751f\u524d\u53d1\u73b0\u95ee\u9898 \u4e2d\u4ecb\u7ecd\u91cd\u6784\u7684\u6982\u5ff5\u3002 \u901a\u5e38\uff0c\u5728\u5b66\u4e60C++\u7684\u65f6\u5019\uff0c\u4f60\u7f16\u5199\u7684\u5f88\u591a\u7a0b\u5e8f\u90fd\u6d89\u53ca\u5230\u5982\u4e0b\u4e09\u79cd\u7c7b\u578b\u7684\u5b50\u4efb\u52a1\uff1a \u4ece\u7528\u6237\u83b7\u53d6\u8f93\u5165\uff1b \u57fa\u4e8e\u8f93\u5165\u8fdb\u884c\u8ba1\u7b97\uff1b \u6253\u5370\u8ba1\u7b97\u7684\u7ed3\u679c\u3002 \u5bf9\u4e8e\u77ed\u5c0f\u7684\u7a0b\u5e8f (\u4f8b\u5982\uff0c\u5c0f\u4e8e20\u884c\u4ee3\u7801\u7684\u7a0b\u5e8f\uff09\uff0c\u4e0a\u8ff0\u4e09\u4e2a\u5b50\u4efb\u52a1\u90fd\u53ef\u4ee5\u5728 main \u51fd\u6570\u4e2d\u5b8c\u6210\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u66f4\u957f\uff08\u6216\u8005\u51fa\u4e8e\u7ec3\u4e60\u7684\u76ee\u7684\uff09\uff0c\u4e0a\u8ff0\u4e09\u4e2a\u4efb\u52a1\u90fd\u5f88\u9002\u5408\u5404\u81ea\u4f5c\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u51fd\u6570\u3002 \u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u5c06\u8ba1\u7b97\u7ed3\u679c\u548c\u6253\u5370\u7ed3\u679c\u8fd9\u4e24\u6b65\u5408\u5e76\u4e3a\u4e00\u4e2a\u51fd\u6570\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u8fdd\u53cd\u4e86\u201c\u4e00\u4e2a\u51fd\u6570\u53ea\u505a\u4e00\u4ef6\u4e8b\u201d \u7684\u539f\u5219\u3002\u8d1f\u8d23\u8ba1\u7b97\u7ed3\u679c\u7684\u51fd\u6570\u5e94\u8be5\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\uff0c\u7136\u540e\u8ba9\u4e3b\u8c03\u51fd\u6570\u51b3\u5b9a\u5982\u4f55\u4f7f\u7528\u8be5\u8fd4\u56de\u503c\uff08\u4f8b\u5982\u8c03\u7528\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\u5c06\u8be5\u7ed3\u679c\u6253\u5370\u51fa\u6765\uff09\u3002","title":"\u9ad8\u6548\u5730\u4f7f\u7528\u51fd\u6570"},{"location":"2-7-Forward-declarations-and-definitions/","tags":["fordward declaration"],"text":"Key Takeaway \u58f0\u660e\u6ee1\u8db3\u7f16\u8bd1\u5668\u7684\u9700\u8981\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728\u5c1a\u672a\u8fdb\u884c\u5177\u4f53\u5b9a\u4e49\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u524d\u5411\u58f0\u660e\u544a\u77e5\u7f16\u8bd1\u5668\u6807\u8bc6\u7b26\u7684\u4fe1\u606f \u5b9a\u4e49\u6ee1\u8db3\u94fe\u63a5\u5668\u7684\u9700\u8981\uff0c\u5426\u5219\u94fe\u63a5\u65f6\u4f1a\u51fa\u9519 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u662f C++\u4e2d\u6700\u8457\u540d\u7684\u89c4\u5219\u4e4b\u4e00\u3002 \u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u4e00\u4e2a\u51fd\u6570\u3001\u53d8\u91cf\u3001\u7c7b\u578b\u6216\u6a21\u677f\u53ea\u80fd\u591f\u88ab\u5b9a\u4e49\u4e00\u6b21\uff1b \u5728\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u4e00\u4e2a\u53d8\u91cf\u6216\u666e\u901a\u51fd\u6570\u53ea\u80fd\u591f\u6709\u4e00\u4e2a\u5b9a\u4e49\uff1b \u7c7b\u578b\u3001\u6a21\u677f\u3001\u5185\u8054\u51fd\u6570\u548c\u5185\u8054\u53d8\u91cf\u53ef\u4ee5\u6709\u4e00\u6837\u7684\u5b9a\u4e49\uff0c\u4f46\u5fc5\u987b\u4f4d\u4e8e\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d \u3002\u7531\u4e8e\u6211\u4eec\u5c1a\u672a\u4ecb\u7ecd\u8fd9\u4e9b\u6982\u5ff5\uff0c\u56e0\u6b64\u76ee\u524d\u65e0\u9700\u8003\u8651\u5b83\u4eec\u3002 \u4e0d\u540c\u6587\u4ef6\u4e2d\u540c\u540d\uff08\u751a\u81f3\u540c\u53c2\u6570\uff09\u7684\u5185\u90e8\u53d8\u91cf\uff08\u51fd\u6570\uff09\u5177\u6709 \u5185\u90e8\u94fe\u63a5 \uff0c\u5b83\u4eec\u662f\u72ec\u7acb\u7684\uff0c\u56e0\u6b64\u4e0d\u4f1a\u8fdd\u53cd\u5355\u4e00\u5b9a\u4e49\u89c4\u5219 \u5c3d\u7ba1\u6240\u6709\u7684\u5b9a\u4e49\u4e5f\u90fd\u662f\u58f0\u660e\uff0c\u4f46\u53cd\u8fc7\u6765\u5374\u4e0d\u662f\u7684\uff1a\u5e76\u4e0d\u662f\u6240\u6709\u7684\u58f0\u660e\u90fd\u662f\u5b9a\u4e49\u3002 \u8003\u8651\u5982\u4e0b\u7b80\u5355\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } int add ( int x , int y ) { return x + y ; } \u6211\u4eec\u671f\u671b\u7684\u7ed3\u679c\u662f\u7a0b\u5e8f\u6709\u5982\u4e0b\u8f93\u51fa\uff1a 1 The sum of 3 and 4 is: 7 \u4f46\u5b9e\u9645\u4e0a\u8fd9\u4e2a\u7a0b\u5e8f\u6839\u672c\u5c31\u4e0d\u80fd\u88ab\u7f16\u8bd1\uff01Visual Studio \u6253\u5370\u7684\u9519\u8bef\u4fe1\u606f\u5982\u4e0b\uff1a 1 add.cpp(5) : error C3861: 'add': identifier not found \u7f16\u8bd1\u5668 \u5728\u7f16\u8bd1\u4e0a\u8ff0\u7a0b\u5e8f\u7684\u65f6\u5019\u4f1a\u987a\u5e8f\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\u7684\u5185\u5bb9\u3002\u5f53\u7f16\u8bd1\u5668\u5f00\u59cb\u8981\u7f16\u8bd1 \u7b2c\u4e94\u884c \u7684 add \u51fd\u6570\u65f6\uff0c\u5b83\u4e0d\u77e5\u9053 add \u662f\u4ec0\u4e48\uff0c\u56e0\u4e3a\u6211\u4eec\u8fd8\u6ca1\u6709\u5b9a\u4e49 add \uff08\u5b83\u662f\u5728\u7b2c\u4e5d\u884c\u5b9a\u4e49\u7684\uff09\uff01\u56e0\u6b64\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u544a\u4e00\u4e2a \u6807\u8bc6\u7b26(identifier) \u672a\u627e\u5230\u7684\u9519\u8bef\uff08identifier not found\uff09\u3002 \u65e7\u7248\u672c\u7684 Visual Studio \u8fd8\u4f1a\u62a5\u544a\u53e6\u5916\u4e00\u4e2a\u9519\u8bef\uff1a 1 add.cpp(9) : error C2365: 'add'; : redefinition; previous definition was 'formerly unknown identifier' \u8fd9\u4e2a\u9519\u8bef\u4fe1\u606f\u53ef\u80fd\u4f1a\u8bef\u5bfc\u6211\u4eec\uff0c\u56e0\u4e3a add \u5c1a\u672a\u88ab\u5b9a\u4e49\uff0c\u53c8\u600e\u4e48\u4f1a\u6709\u91cd\u590d\u5b9a\u4e49(redefinition)\u7684\u95ee\u9898\u5462\uff1f\u629b\u5f00\u8fd9\u4e2a\u95ee\u9898\u4e0d\u8c08\uff0c\u6211\u4eec\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7f16\u8bd1\u5668\u5728\u62a5\u544a\u9519\u8bef\u7684\u65f6\u5019\uff0c\u7684\u786e\u4f1a\u8fde\u5e26\u5730\u62a5\u544a\u4e00\u4e9b\u5197\u4f59\u7684\u6216\u76f8\u5173\u7684\u9519\u8bef\u6216\u544a\u8b66\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u89e3\u51b3\u7f16\u8bd1\u62a5\u9519\u7684\u95ee\u9898\u65f6\uff0c\u603b\u662f\u5148\u89e3\u51b3\u62a5\u544a\u7684\u7b2c\u4e00\u4e2a\u9519\u8bef\u5e76\u91cd\u65b0\u7f16\u8bd1\u3002 \u4e3a\u4e86\u5904\u7406\u6389\u8fd9\u4e2a\u62a5\u9519\u4fe1\u606f\uff0c\u6211\u4eec\u5fc5\u987b\u8981\u89e3\u51b3\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053 add \u662f\u4ec0\u4e48\u8fd9\u4e00\u95ee\u9898\u3002\u901a\u5e38\uff0c\u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u7528\u3002 \u65b9\u6848 1: \u8c03\u6574\u5b9a\u4e49\u987a\u5e8f \u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u5c31\u662f\u5c06 add \u7684\u5b9a\u4e49\u632a\u5230 main \u524d\u9762\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int add ( int x , int y ) { return x + y ; } int main () { std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } \u8fd9\u4e00\u6837\u4e00\u6765\uff0c\u5f53 main \u51fd\u6570\u8c03\u7528 add \u7684\u65f6\u5019\uff0c\u7f16\u8bd1\u5668\u5df2\u7ecf\u77e5\u9053\u4e86 add \u7684\u5b9a\u4e49\u3002\u5bf9\u4e8e\u8fd9\u6837\u4e00\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\u6765\u8bf4\uff0c\u8fd9\u4e48\u505a\u662f\u5f88\u5bb9\u6613\u7684\u3002\u4f46\u662f\u5bf9\u4e8e\u5927\u578b\u7a0b\u5e8f\u6765\u8bf4\uff0c\u641e\u6e05\u695a\u51fd\u6570\u7684\u8c03\u7528\u987a\u5e8f\u5e76\u4f9d\u6b64\u6765\u8c03\u6574\u5176\u5b9a\u4e49\u987a\u5e8f\u662f\u975e\u5e38\u8d39\u529b\u7684\u3002 \u4e0d\u4ec5\u5982\u6b64\uff0c\u5728\u9762\u5bf9\u6709\u4e9b\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u751a\u81f3\u662f\u884c\u4e0d\u901a\u7684\u3002\u4f8b\u5982\uff0c\u67d0\u4e2a\u7a0b\u5e8f\u5305\u542b\u4e24\u4e2a\u51fd\u6570\uff0cA \u548c B\u3002\u5982\u679c\u51fd\u6570 A \u8c03\u7528\u4e86\u51fd\u6570 B\uff0c\u540c\u65f6\u51fd\u6570 B \u53c8\u8c03\u7528\u4e86\u51fd\u6570 A\uff0c\u90a3\u4e48\u6309\u7167\u672c\u65b9\u6cd5\u6765\u505a\u7684\u8bdd\uff0c\u5b9e\u9645\u4e0a\u662f\u65e0\u6cd5\u8c03\u6574\u987a\u5e8f\u4f7f\u5176\u80fd\u591f\u7f16\u8bd1\u7684\u3002\u5982\u679c\u4f60\u5148\u5b9a\u4e49 A\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u62b1\u6028\u627e\u4e0d\u5230 B\uff0c\u5982\u679c\u4f60\u5148\u5b9a\u4e49 B\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u62b1\u6028\u627e\u4e0d\u5230 A\u3002 \u65b9\u6848 2: \u4f7f\u7528\u524d\u5411\u58f0\u660e \u89e3\u51b3\u95ee\u9898\u7684\u65b9\u6848 2\uff0c\u662f\u4f7f\u7528 \u524d\u5411\u58f0\u660e(forward declaration) \u3002 \u4f7f\u7528\u524d\u5411\u58f0\u660e\u53ef\u4ee5\u5728\u5b9e\u9645\u5b9a\u4e49\u8be5\u6807\u8bc6\u7b26\u524d\uff0c\u9884\u5148\u544a\u77e5\u7f16\u8bd1\u5668\u8be5\u6807\u8bc6\u7b26\u662f\u5b58\u5728\u7684\u3002 \u5bf9\u4e8e\u51fd\u6570\u6765\u8bf4\uff0c\u8fd9\u4e48\u505a\u53ef\u4ee5\u5728\u5b9a\u4e49\u51fd\u6570\u4f53\u4e4b\u524d\uff0c\u5148\u544a\u8bc9\u7f16\u8bd1\u5668\u51fd\u6570\u540d\u5b58\u5728\u3002\u56e0\u6b64\uff0c\u5f53\u7f16\u8bd1\u5668\u7f16\u8bd1\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u5b83\u77e5\u9053\u8be5\u6807\u8bc6\u7b26\u8868\u793a\u4e00\u4e2a\u51fd\u6570\uff0c\u6b64\u65f6\u5728\u8fdb\u884c\u51fd\u6570\u8c03\u7528\uff0c\u5e76\u68c0\u67e5\u51fd\u6570\u8c03\u7528\u662f\u5426\u6b63\u786e\uff0c\u5373\u4f7f\u5176\u4e0a\u4e0d\u77e5\u9053\u8be5\u51fd\u6570\u662f\u5982\u4f55\u5b9a\u4e49\u3001\u5728\u54ea\u91cc\u5b9a\u4e49\u7684\u3002 \u7f16\u5199\u524d\u5411\u5347\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u88ab\u79f0\u4e3a \u51fd\u6570\u539f\u578b \u7684\u58f0\u660e\u8bed\u53e5\u3002\u51fd\u6570\u539f\u578b\u5305\u62ec\u4e86\u51fd\u6570\u5934\uff08\u5373\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u3001\u51fd\u6570\u540d\u548c \u5f62\u53c2 \u7c7b\u578b\uff09\u5e76\u4ee5\u5206\u53f7\u7ed3\u5c3e\u3002\u51fd\u6570\u4f53\u5e76\u4e0d\u5c5e\u4e8e\u51fd\u6570\u539f\u578b\u7684\u4e00\u90e8\u5206\u3002 add \u51fd\u6570\u7684\u51fd\u6570\u539f\u578b\u5982\u4e0b\uff1a 1 2 // \u51fd\u6570\u539f\u578b\u5305\u62ec\u8fd4\u56de\u503c\u7c7b\u578b\u3001\u51fd\u6570\u540d\u3001\u5f62\u53c2\u548c\u4e00\u4e2a\u5206\u53f7\u3002\u4e0d\u9700\u8981\u51fd\u6570\u4f53\uff01 int add ( int x , int y ); \u73b0\u5728\uff0c\u4e3a\u4e4b\u524d\u4e0d\u80fd\u7f16\u8bd1\u7684\u7a0b\u5e8f\u6dfb\u52a0 add \u51fd\u6570\u7684\u524d\u5411\u58f0\u660e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int add ( int x , int y ); // \u4f7f\u7528\u51fd\u6570\u539f\u578b\u8fdb\u884c\u524d\u5411\u58f0\u660e int main () { // \u7531\u4e8e\u524d\u5411\u58f0\u660e\u7684\u5b58\u5728\uff0c\u8be5\u884c\u4ee3\u7801\u4e0d\u518d\u62a5\u9519 std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } int add ( int x , int y ) // \u5373\u4f7f\u51fd\u6570\u4f53\u5728\u540e\u9762\u624d\u5b9a\u4e49 { return x + y ; } \u73b0\u5728\uff0c\u5f53\u7f16\u8bd1\u5668\u5230\u8fbe add \u51fd\u6570\u8c03\u7528\u5904\u662f\uff0c\u5b83\u5df2\u7ecf\u77e5\u9053\u4e86 add \u7684\u539f\u578b\uff08\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\uff09\uff0c\u6b64\u65f6\u5b83\u4fbf\u4e0d\u4f1a\u518d\u62a5\u9519\u4e86\u3002 \u5728\u51fd\u6570\u539f\u578b\u4e2d\u5176\u5b9e\u6ca1\u5fc5\u8981\u63d0\u4f9b\u53c2\u6570\u7684\u540d\u5b57\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4f60\u4e5f\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u8fdb\u884c\u524d\u5411\u58f0\u660e\uff1a 1 int add ( int , int ); // valid function prototype \u4e0d\u8fc7\uff0c\u6211\u4eec\u5efa\u8bae\u8fd8\u662f\u4f7f\u7528\u5b9e\u9645\u7684\u53d8\u91cf\u540d\uff0c\u56e0\u4e3a\u8fd9\u6837\u53ef\u4ee5\u5728\u4f60\u770b\u5230\u51fd\u6570\u539f\u578b\u7684\u65f6\u5019\uff0c\u66f4\u597d\u5730\u7406\u89e3\u5176\u542b\u4e49\u3002\u5426\u5219\uff0c\u4f60\u5fc5\u987b\u518d\u6b21\u5bfb\u627e\u5b83\u7684\u5b9a\u4e49\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u5b9a\u4e49\u51fd\u6570\u539f\u578b\u7684\u65f6\u5019\uff0c\u8bf7\u4fdd\u7559\u51fd\u6570\u540d\u3002\u4f60\u53ef\u4ee5\u8d4b\u503c\u7c98\u8d34\u51fd\u6570\u7684\u539f\u578b\u5e76\u5728\u5176\u540e\u9762\u52a0\u4e0a\u4e00\u4e2a\u5206\u53f7\u6765\u8f7b\u677e\u5730\u521b\u5efa\u524d\u5411\u58f0\u660e\u3002 \u5fd8\u8bb0\u5b9a\u4e49\u51fd\u6570\u600e\u4e48\u529e \u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u5e38\u4f1a\u95ee\uff0c\u5982\u679c\u4ec5\u4ec5\u5bf9\u51fd\u6570\u8fdb\u884c\u4e86\u524d\u5411\u58f0\u660e\uff0c\u4f46\u662f\u6ca1\u6709\u5b9e\u9645\u5b9a\u4e49\u8be5\u51fd\u6570\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f \u8fd9\u4e2a\u95ee\u9898\u7684\u7b54\u6848\u662f\uff1a\u5177\u4f53\u60c5\u51b5\u5177\u4f53\u5206\u6790\u3002\u5982\u679c\u51fd\u6570\u5177\u6709\u524d\u5411\u58f0\u660e\uff0c\u4f46\u662f\u8be5\u51fd\u6570\u5e76\u672a\u88ab\u5b9e\u9645\u8c03\u7528\uff0c\u90a3\u4e48\u7a0b\u5e8f\u662f\u53ef\u4ee5\u6b63\u5e38\u7f16\u8bd1\u548c\u8fd0\u884c\u7684\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u51fd\u6570\u88ab\u5b9e\u9645\u8c03\u7528\u4e86\uff0c\u4f46\u7a0b\u5e8f\u5374\u6ca1\u6709\u5b9a\u4e49\u8be5\u51fd\u6570\uff0c\u90a3\u4e48\u53ea\u80fd\u591f\u4fdd\u8bc1\u7f16\u8bd1\u662f\u53ef\u4ee5\u901a\u8fc7\u7684\uff0c\u4f46 \u94fe\u63a5\u5668(linker) \u4f1a\u62a5\u9519\uff0c\u56e0\u4e3a\u5b83\u65e0\u6cd5\u89e3\u6790\u8be5\u51fd\u6570\u8c03\u7528\u3002 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int add ( int x , int y ); // \u4f7f\u7528\u51fd\u6570\u539f\u578b\u8fdb\u884c\u524d\u5411\u58f0\u660e int main () { std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } // \u6ce8\u610f\uff1a\u6ca1\u6709\u5bf9\u51fd\u6570\u8fdb\u884c\u5b9a\u4e49 \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u5bf9 add \u8fdb\u884c\u4e86\u524d\u5411\u58f0\u660e\u5e76\u4e14\u8c03\u7528\u4e86 add \u51fd\u6570\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u5374\u6ca1\u6709\u5bf9\u5176\u8fdb\u884c\u58f0\u660e\u3002\u5982\u679c\u5bf9\u7a0b\u5e8f\u8fdb\u884c\u7f16\u8bd1\uff0cVisual Studio \u4f1a\u62a5\u544a\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 5 Compiling... add.cpp Linking... add.obj : error LNK2001: unresolved external symbol \"int __cdecl add(int,int)\" (?add@@YAHHH@Z) add.exe : fatal error LNK1120: 1 unresolved externals \u53ef\u4ee5\u770b\u5230\uff0c\u7a0b\u5e8f\u662f\u80fd\u591f\u6b63\u786e\u7f16\u8bd1\u7684\uff0c\u4f46\u662f\u5728\u94fe\u63a5\u9636\u6bb5\u5374\u62a5\u9519\u4e86\uff0c\u56e0\u4e3a int add(int, int) \u6ca1\u6709\u88ab\u5b9a\u4e49\u3002 \u5176\u4ed6\u7c7b\u578b\u7684\u524d\u5411\u58f0\u660e \u524d\u5411\u58f0\u660e\u6700\u5e38\u7528\u4e8e\u51fd\u6570\u3002\u4f46\u662f\uff0c\u524d\u5411\u58f0\u660e\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5176\u4ed6\u7684 C++\u6807\u8bc6\u7b26\uff0c\u4f8b\u5982\u53d8\u91cf\u6216\u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u3002\u4e0d\u8fc7\u5b83\u4eec\u7684\u524d\u5411\u58f0\u660e\u65b9\u5f0f\u7a0d\u6709\u4e0d\u540c\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u58f0\u660e vs. \u5b9a\u4e49 \u5728 C++ \u4e2d\uff0c\u4f60\u65f6\u5e38\u4f1a\u542c\u5230 \u58f0\u660e(declaration) \u548c \u5b9a\u4e49(definition) \u8fd9\u4e24\u4e2a\u8bcd\uff0c\u6709\u65f6\u5019\u5b83\u4eec\u662f\u53ef\u4ee5\u4e92\u6362\u7684\u3002\u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u638c\u63e1\u4e86\u8db3\u591f\u7684\u77e5\u8bc6\uff0c\u53ef\u4ee5\u53bb\u7406\u89e3\u5b83\u4eec\u4e4b\u95f4\u7684\u4e0d\u540c\u4e86\u3002 \u5b9a\u4e49(definition) \u5b9e\u9645\u4e0a\u6307\u7684\u662f\u5bf9\u67d0\u4e2a \u6807\u8bc6\u7b26(identifier) \u7684\u5b9e\u73b0\uff08\u5bf9\u4e8e\u51fd\u6570\u6216\u7c7b\u578b\u6765\u8bf4\uff09\u6216\u5b9e\u4f8b\u5316\uff08\u5bf9\u4e8e\u53d8\u91cf\u6765\u8bf4\uff09\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u6709\u5173\u5b9a\u4e49\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 int add ( int x , int y ) // implements function add() { int z { x + y }; // instantiates variable z return z ; } \u5b9a\u4e49\u662f\u4e3a\u4e86\u6ee1\u8db3\u94fe\u63a5\u5668\u7684\u9700\u8981 \u3002\u5982\u679c\u4f60\u4f7f\u7528\u4e86\u672a\u5b9a\u4e49\u7684\u6807\u8bc6\u7b26\uff0c\u94fe\u63a5\u5668\u5c31\u4f1a\u62a5\u9519\u3002 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule,ODR) \u662f C++\u4e2d\u6700\u8457\u540d\u7684\u89c4\u5219\u4e4b\u4e00\u3002 \u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u4e00\u4e2a\u51fd\u6570\u3001\u53d8\u91cf\u3001\u7c7b\u578b\u6216\u6a21\u677f\u53ea\u80fd\u591f\u88ab\u5b9a\u4e49\u4e00\u6b21\uff1b \u5728\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u4e00\u4e2a\u53d8\u91cf\u6216\u666e\u901a\u51fd\u6570\u53ea\u80fd\u591f\u6709\u4e00\u4e2a\u5b9a\u4e49\u3002\u548c\u4e0a\u4e00\u6761\u7684\u533a\u522b\u5728\u4e8e\u4e00\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u7531\u591a\u4e2a\u6587\u4ef6\u7ec4\u6210\uff08\u540e\u9762\u7684\u8bfe\u7a0b\u4f1a\u4ecb\u7ecd\uff09\uff1b \u7c7b\u578b\u3001\u6a21\u677f\u3001\u5185\u8054\u51fd\u6570\u548c\u5185\u8054\u53d8\u91cf \u53ef\u4ee5\u6709\u4e00\u6837\u7684\u5b9a\u4e49 \uff0c\u4f46\u5fc5\u987b\u4f4d\u4e8e\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\u3002\u7531\u4e8e\u6211\u4eec\u5c1a\u672a\u4ecb\u7ecd\u8fd9\u4e9b\u6982\u5ff5\uff0c\u56e0\u6b64\u76ee\u524d\u65e0\u9700\u8003\u8651\u5b83\u4eec\u3002 \u5982\u679c\u8fdd\u53cd\u4e86 ODR \u7684\u7b2c\u4e00\u6761\u89c4\u5219\uff0c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u544a\u91cd\u590d\u5b9a\u4e49\u7684\u9519\u8bef\u3002\u5982\u679c\u8fdd\u53cd\u4e86 ODR \u7684\u7b2c\u4e8c\u6761\u89c4\u5219\uff0c\u5219\u5f88\u53ef\u80fd\u5bfc\u81f4\u94fe\u63a5\u5668\u62a5\u544a\u91cd\u590d\u5b9a\u4e49\u9519\u8bef\u3002\u8fdd\u53cd ODR \u7b2c\u4e09\u6761\u89c4\u5219\u5219\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e0b\u9762\u4f8b\u5b50\u5c55\u793a\u4e86\u8fdd\u53cd\u7b2c\u4e00\u6761\u89c4\u5219\u7684\u60c5\u5f62\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int add ( int x , int y ) { return x + y ; } int add ( int x , int y ) // \u8fdd\u53cd\u4e86ODR\uff0c\u56e0\u4e3a add \u5df2\u7ecf\u88ab\u5b9a\u4e49\u8fc7\u4e86 { return x + y ; } int main () { int x ; int x ; // \u8fdd\u53cd\u4e86ODR\uff0c\u56e0\u4e3a x \u5df2\u7ecf\u88ab\u5b9a\u4e49\u8fc7\u4e86 } \u7531\u4e8e\u4e0a\u8ff0\u7a0b\u5e8f\u8fdd\u53cd\u4e86ODR\u7b2c\u4e00\u6761\u89c4\u5219\uff0cVisual Studio \u7684\u7f16\u8bd1\u5668\u4f1a\u62a5\u544a\u5982\u4e0b\u7f16\u8bd1\u9519\u8bef\uff1a 1 2 3 4 project3.cpp(9): error C2084: function 'int add(int,int)' already has a body project3.cpp(3): note: see previous definition of 'add' project3.cpp(16): error C2086: 'int x': redefinition project3.cpp(15): note: see declaration of 'x' \u6269\u5c55\u9605\u8bfb \u6807\u8bc6\u7b26\u76f8\u540c\u4f46\u53c2\u6570\u4e0d\u540c\u7684\u51fd\u6570\uff0c\u88ab\u770b\u505a\u662f\u4e0d\u540c\u7684\u51fd\u6570\u3002\u6211\u4eec\u4f1a\u5728 8.9 - \u51fd\u6570\u91cd\u8f7d \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u58f0\u660e\u5219\u662f\u4e00\u6761\u8bed\u53e5\uff0c\u5b83\u544a\u8bc9\u7f16\u8bd1\u5668\u67d0\u4e2a\u6807\u8bc6\u7b26\u662f\u5b58\u5728\u7684\uff0c\u5e76\u63d0\u4f9b\u5176\u7c7b\u578b\u4fe1\u606f\u3002\u58f0\u660e\u76f8\u5173\u7684\u4f8b\u5b50\u5982\u4e0b\uff1a 1 2 int add ( int x , int y ); // tells the compiler about a function named \"add\" that takes two int parameters and returns an int. No body! int x ; // tells the compiler about an integer variable named x \u58f0\u660e(declaration) \u7eaf\u7cb9\u4e3a\u4e86\u6ee1\u8db3\u7f16\u8bd1\u5668\u7684\u9700\u8981\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728\u5c1a\u672a\u8fdb\u884c\u5177\u4f53\u5b9a\u4e49\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u524d\u5411\u58f0\u660e\u544a\u77e5\u7f16\u8bd1\u5668\u6807\u8bc6\u7b26\u7684\u4fe1\u606f\u3002 \u5728 C++ \u4e2d\uff0c\u5b9a\u4e49\u4e5f\u5177\u6709\u58f0\u660e\u7684\u529f\u80fd\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728\u4e0a\u9762\u58f0\u660e\u548c\u5b9a\u4e49\u7684\u4f8b\u5b50\u4e2d\u90fd\u51fa\u73b0\u4e86 int x \uff0c\u56e0\u4e3a\u5b83\u65e2\u662f\u58f0\u660e\u4e5f\u662f\u5b9a\u4e49\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5b9a\u4e49\u53ef\u4ee5\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u8981\uff0c\u56e0\u4e3a\u5b83\u65e2\u53ef\u4ee5\u88ab\u7f16\u8bd1\u5668\u4f7f\u7528\uff0c\u4e5f\u53ef\u4ee5\u88ab\u94fe\u63a5\u5668\u4f7f\u7528\u3002\u53ea\u6709\u5f53\u9700\u8981\u5728\u6807\u8bc6\u7b26\u5c1a\u672a\u5b9a\u4e49\u524d\u4f7f\u7528\u5b83\u65f6\uff0c\u624d\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u663e\u5f0f\u7684\u58f0\u660e\u3002 \u5c3d\u7ba1\u6240\u6709\u7684\u5b9a\u4e49\u4e5f\u90fd\u662f\u58f0\u660e\uff0c\u4f46\u53cd\u8fc7\u6765\u5374\u4e0d\u662f\u7684\uff1a\u5e76\u4e0d\u662f\u6240\u6709\u7684\u58f0\u660e\u90fd\u662f\u5b9a\u4e49 \u3002\u4ee5\u51fd\u6570\u539f\u578b\u4e3a\u4f8b\u2014\u2014\u5b83\u53ea\u80fd\u591f\u6ee1\u8db3\u7f16\u8bd1\u5668\u7684\u9700\u8981\uff0c\u4f46\u4e0d\u80fd\u6ee1\u8db3\u94fe\u63a5\u5668\u7684\u9700\u8981\u3002\u5bf9\u4e8e\u90a3\u4e9b\u53ea\u662f\u58f0\u660e\uff0c\u4f46\u662f\u4e0d\u662f\u5b9a\u4e49\u7684\u58f0\u660e\uff0c\u79f0\u4e3a \u7eaf\u58f0\u660e(pure declaration) \u3002 \u5176\u4ed6\u7c7b\u578b\u7684\u7eaf\u58f0\u660e\u5305\u62ec\u53d8\u91cf\u7684\u524d\u5411\u58f0\u660e\u548c\u7c7b\u578b\u58f0\u660e \uff08\u5728\u540e\u7eed\u7684\u7ae0\u8282\u4f1a\u8fdb\u884c\u4ecb\u7ecd\uff0c\u76ee\u524d\u65e0\u9700\u64cd\u5fc3\uff09\u3002 ODR\u539f\u5219\u5e76\u4e0d\u9002\u7528\u4e8e\u7eaf\u58f0\u660e\uff08\u6bd5\u7adfODR\u6307\u7684\u662f \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u800c\u4e0d\u662f \u5355\u4e00\u58f0\u660e\u89c4\u5219 \uff09\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u591a\u6b21\u58f0\u660e\u67d0\u4e2a\u6807\u8bc6\u7b26\uff08\u5c3d\u7ba1\u8fd9\u4e48\u505a\u662f\u591a\u4f59\u7684\uff09\u3002 \u4f5c\u8005\u6ce8 \u5728\u4e00\u822c\u7528\u8bed\u4e2d\uff0c \u58f0\u660e \u4e00\u822c\u6307\u7eaf\u58f0\u660e\uff0c\u800c \u5b9a\u4e49 \u5219\u6307\u7684\u662f\u65e2\u80fd\u505a\u5b9a\u4e49\uff0c\u4e5f\u80fd\u505a\u58f0\u660e\u7684\u5b9a\u4e49\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u79f0 int x; \u4e3a\u5b9a\u4e49\uff0c\u5c3d\u7ba1\u5b83\u65e2\u662f\u5b9a\u4e49\u4e5f\u662f\u58f0\u660e\u3002","title":"2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49"},{"location":"2-7-Forward-declarations-and-definitions/#1","text":"\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u5c31\u662f\u5c06 add \u7684\u5b9a\u4e49\u632a\u5230 main \u524d\u9762\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int add ( int x , int y ) { return x + y ; } int main () { std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } \u8fd9\u4e00\u6837\u4e00\u6765\uff0c\u5f53 main \u51fd\u6570\u8c03\u7528 add \u7684\u65f6\u5019\uff0c\u7f16\u8bd1\u5668\u5df2\u7ecf\u77e5\u9053\u4e86 add \u7684\u5b9a\u4e49\u3002\u5bf9\u4e8e\u8fd9\u6837\u4e00\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\u6765\u8bf4\uff0c\u8fd9\u4e48\u505a\u662f\u5f88\u5bb9\u6613\u7684\u3002\u4f46\u662f\u5bf9\u4e8e\u5927\u578b\u7a0b\u5e8f\u6765\u8bf4\uff0c\u641e\u6e05\u695a\u51fd\u6570\u7684\u8c03\u7528\u987a\u5e8f\u5e76\u4f9d\u6b64\u6765\u8c03\u6574\u5176\u5b9a\u4e49\u987a\u5e8f\u662f\u975e\u5e38\u8d39\u529b\u7684\u3002 \u4e0d\u4ec5\u5982\u6b64\uff0c\u5728\u9762\u5bf9\u6709\u4e9b\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u751a\u81f3\u662f\u884c\u4e0d\u901a\u7684\u3002\u4f8b\u5982\uff0c\u67d0\u4e2a\u7a0b\u5e8f\u5305\u542b\u4e24\u4e2a\u51fd\u6570\uff0cA \u548c B\u3002\u5982\u679c\u51fd\u6570 A \u8c03\u7528\u4e86\u51fd\u6570 B\uff0c\u540c\u65f6\u51fd\u6570 B \u53c8\u8c03\u7528\u4e86\u51fd\u6570 A\uff0c\u90a3\u4e48\u6309\u7167\u672c\u65b9\u6cd5\u6765\u505a\u7684\u8bdd\uff0c\u5b9e\u9645\u4e0a\u662f\u65e0\u6cd5\u8c03\u6574\u987a\u5e8f\u4f7f\u5176\u80fd\u591f\u7f16\u8bd1\u7684\u3002\u5982\u679c\u4f60\u5148\u5b9a\u4e49 A\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u62b1\u6028\u627e\u4e0d\u5230 B\uff0c\u5982\u679c\u4f60\u5148\u5b9a\u4e49 B\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u62b1\u6028\u627e\u4e0d\u5230 A\u3002","title":"\u65b9\u6848 1: \u8c03\u6574\u5b9a\u4e49\u987a\u5e8f"},{"location":"2-7-Forward-declarations-and-definitions/#2","text":"\u89e3\u51b3\u95ee\u9898\u7684\u65b9\u6848 2\uff0c\u662f\u4f7f\u7528 \u524d\u5411\u58f0\u660e(forward declaration) \u3002 \u4f7f\u7528\u524d\u5411\u58f0\u660e\u53ef\u4ee5\u5728\u5b9e\u9645\u5b9a\u4e49\u8be5\u6807\u8bc6\u7b26\u524d\uff0c\u9884\u5148\u544a\u77e5\u7f16\u8bd1\u5668\u8be5\u6807\u8bc6\u7b26\u662f\u5b58\u5728\u7684\u3002 \u5bf9\u4e8e\u51fd\u6570\u6765\u8bf4\uff0c\u8fd9\u4e48\u505a\u53ef\u4ee5\u5728\u5b9a\u4e49\u51fd\u6570\u4f53\u4e4b\u524d\uff0c\u5148\u544a\u8bc9\u7f16\u8bd1\u5668\u51fd\u6570\u540d\u5b58\u5728\u3002\u56e0\u6b64\uff0c\u5f53\u7f16\u8bd1\u5668\u7f16\u8bd1\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u5b83\u77e5\u9053\u8be5\u6807\u8bc6\u7b26\u8868\u793a\u4e00\u4e2a\u51fd\u6570\uff0c\u6b64\u65f6\u5728\u8fdb\u884c\u51fd\u6570\u8c03\u7528\uff0c\u5e76\u68c0\u67e5\u51fd\u6570\u8c03\u7528\u662f\u5426\u6b63\u786e\uff0c\u5373\u4f7f\u5176\u4e0a\u4e0d\u77e5\u9053\u8be5\u51fd\u6570\u662f\u5982\u4f55\u5b9a\u4e49\u3001\u5728\u54ea\u91cc\u5b9a\u4e49\u7684\u3002 \u7f16\u5199\u524d\u5411\u5347\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u88ab\u79f0\u4e3a \u51fd\u6570\u539f\u578b \u7684\u58f0\u660e\u8bed\u53e5\u3002\u51fd\u6570\u539f\u578b\u5305\u62ec\u4e86\u51fd\u6570\u5934\uff08\u5373\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u3001\u51fd\u6570\u540d\u548c \u5f62\u53c2 \u7c7b\u578b\uff09\u5e76\u4ee5\u5206\u53f7\u7ed3\u5c3e\u3002\u51fd\u6570\u4f53\u5e76\u4e0d\u5c5e\u4e8e\u51fd\u6570\u539f\u578b\u7684\u4e00\u90e8\u5206\u3002 add \u51fd\u6570\u7684\u51fd\u6570\u539f\u578b\u5982\u4e0b\uff1a 1 2 // \u51fd\u6570\u539f\u578b\u5305\u62ec\u8fd4\u56de\u503c\u7c7b\u578b\u3001\u51fd\u6570\u540d\u3001\u5f62\u53c2\u548c\u4e00\u4e2a\u5206\u53f7\u3002\u4e0d\u9700\u8981\u51fd\u6570\u4f53\uff01 int add ( int x , int y ); \u73b0\u5728\uff0c\u4e3a\u4e4b\u524d\u4e0d\u80fd\u7f16\u8bd1\u7684\u7a0b\u5e8f\u6dfb\u52a0 add \u51fd\u6570\u7684\u524d\u5411\u58f0\u660e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int add ( int x , int y ); // \u4f7f\u7528\u51fd\u6570\u539f\u578b\u8fdb\u884c\u524d\u5411\u58f0\u660e int main () { // \u7531\u4e8e\u524d\u5411\u58f0\u660e\u7684\u5b58\u5728\uff0c\u8be5\u884c\u4ee3\u7801\u4e0d\u518d\u62a5\u9519 std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } int add ( int x , int y ) // \u5373\u4f7f\u51fd\u6570\u4f53\u5728\u540e\u9762\u624d\u5b9a\u4e49 { return x + y ; } \u73b0\u5728\uff0c\u5f53\u7f16\u8bd1\u5668\u5230\u8fbe add \u51fd\u6570\u8c03\u7528\u5904\u662f\uff0c\u5b83\u5df2\u7ecf\u77e5\u9053\u4e86 add \u7684\u539f\u578b\uff08\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\uff09\uff0c\u6b64\u65f6\u5b83\u4fbf\u4e0d\u4f1a\u518d\u62a5\u9519\u4e86\u3002 \u5728\u51fd\u6570\u539f\u578b\u4e2d\u5176\u5b9e\u6ca1\u5fc5\u8981\u63d0\u4f9b\u53c2\u6570\u7684\u540d\u5b57\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4f60\u4e5f\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u8fdb\u884c\u524d\u5411\u58f0\u660e\uff1a 1 int add ( int , int ); // valid function prototype \u4e0d\u8fc7\uff0c\u6211\u4eec\u5efa\u8bae\u8fd8\u662f\u4f7f\u7528\u5b9e\u9645\u7684\u53d8\u91cf\u540d\uff0c\u56e0\u4e3a\u8fd9\u6837\u53ef\u4ee5\u5728\u4f60\u770b\u5230\u51fd\u6570\u539f\u578b\u7684\u65f6\u5019\uff0c\u66f4\u597d\u5730\u7406\u89e3\u5176\u542b\u4e49\u3002\u5426\u5219\uff0c\u4f60\u5fc5\u987b\u518d\u6b21\u5bfb\u627e\u5b83\u7684\u5b9a\u4e49\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u5b9a\u4e49\u51fd\u6570\u539f\u578b\u7684\u65f6\u5019\uff0c\u8bf7\u4fdd\u7559\u51fd\u6570\u540d\u3002\u4f60\u53ef\u4ee5\u8d4b\u503c\u7c98\u8d34\u51fd\u6570\u7684\u539f\u578b\u5e76\u5728\u5176\u540e\u9762\u52a0\u4e0a\u4e00\u4e2a\u5206\u53f7\u6765\u8f7b\u677e\u5730\u521b\u5efa\u524d\u5411\u58f0\u660e\u3002","title":"\u65b9\u6848 2: \u4f7f\u7528\u524d\u5411\u58f0\u660e"},{"location":"2-7-Forward-declarations-and-definitions/#_1","text":"\u65b0\u624b\u7a0b\u5e8f\u5458\u5e38\u5e38\u4f1a\u95ee\uff0c\u5982\u679c\u4ec5\u4ec5\u5bf9\u51fd\u6570\u8fdb\u884c\u4e86\u524d\u5411\u58f0\u660e\uff0c\u4f46\u662f\u6ca1\u6709\u5b9e\u9645\u5b9a\u4e49\u8be5\u51fd\u6570\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f \u8fd9\u4e2a\u95ee\u9898\u7684\u7b54\u6848\u662f\uff1a\u5177\u4f53\u60c5\u51b5\u5177\u4f53\u5206\u6790\u3002\u5982\u679c\u51fd\u6570\u5177\u6709\u524d\u5411\u58f0\u660e\uff0c\u4f46\u662f\u8be5\u51fd\u6570\u5e76\u672a\u88ab\u5b9e\u9645\u8c03\u7528\uff0c\u90a3\u4e48\u7a0b\u5e8f\u662f\u53ef\u4ee5\u6b63\u5e38\u7f16\u8bd1\u548c\u8fd0\u884c\u7684\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u51fd\u6570\u88ab\u5b9e\u9645\u8c03\u7528\u4e86\uff0c\u4f46\u7a0b\u5e8f\u5374\u6ca1\u6709\u5b9a\u4e49\u8be5\u51fd\u6570\uff0c\u90a3\u4e48\u53ea\u80fd\u591f\u4fdd\u8bc1\u7f16\u8bd1\u662f\u53ef\u4ee5\u901a\u8fc7\u7684\uff0c\u4f46 \u94fe\u63a5\u5668(linker) \u4f1a\u62a5\u9519\uff0c\u56e0\u4e3a\u5b83\u65e0\u6cd5\u89e3\u6790\u8be5\u51fd\u6570\u8c03\u7528\u3002 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int add ( int x , int y ); // \u4f7f\u7528\u51fd\u6570\u539f\u578b\u8fdb\u884c\u524d\u5411\u58f0\u660e int main () { std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } // \u6ce8\u610f\uff1a\u6ca1\u6709\u5bf9\u51fd\u6570\u8fdb\u884c\u5b9a\u4e49 \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u5bf9 add \u8fdb\u884c\u4e86\u524d\u5411\u58f0\u660e\u5e76\u4e14\u8c03\u7528\u4e86 add \u51fd\u6570\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u5374\u6ca1\u6709\u5bf9\u5176\u8fdb\u884c\u58f0\u660e\u3002\u5982\u679c\u5bf9\u7a0b\u5e8f\u8fdb\u884c\u7f16\u8bd1\uff0cVisual Studio \u4f1a\u62a5\u544a\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 5 Compiling... add.cpp Linking... add.obj : error LNK2001: unresolved external symbol \"int __cdecl add(int,int)\" (?add@@YAHHH@Z) add.exe : fatal error LNK1120: 1 unresolved externals \u53ef\u4ee5\u770b\u5230\uff0c\u7a0b\u5e8f\u662f\u80fd\u591f\u6b63\u786e\u7f16\u8bd1\u7684\uff0c\u4f46\u662f\u5728\u94fe\u63a5\u9636\u6bb5\u5374\u62a5\u9519\u4e86\uff0c\u56e0\u4e3a int add(int, int) \u6ca1\u6709\u88ab\u5b9a\u4e49\u3002","title":"\u5fd8\u8bb0\u5b9a\u4e49\u51fd\u6570\u600e\u4e48\u529e"},{"location":"2-7-Forward-declarations-and-definitions/#_2","text":"\u524d\u5411\u58f0\u660e\u6700\u5e38\u7528\u4e8e\u51fd\u6570\u3002\u4f46\u662f\uff0c\u524d\u5411\u58f0\u660e\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5176\u4ed6\u7684 C++\u6807\u8bc6\u7b26\uff0c\u4f8b\u5982\u53d8\u91cf\u6216\u7528\u6237\u5b9a\u4e49\u7c7b\u578b\u3002\u4e0d\u8fc7\u5b83\u4eec\u7684\u524d\u5411\u58f0\u660e\u65b9\u5f0f\u7a0d\u6709\u4e0d\u540c\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"\u5176\u4ed6\u7c7b\u578b\u7684\u524d\u5411\u58f0\u660e"},{"location":"2-7-Forward-declarations-and-definitions/#vs","text":"\u5728 C++ \u4e2d\uff0c\u4f60\u65f6\u5e38\u4f1a\u542c\u5230 \u58f0\u660e(declaration) \u548c \u5b9a\u4e49(definition) \u8fd9\u4e24\u4e2a\u8bcd\uff0c\u6709\u65f6\u5019\u5b83\u4eec\u662f\u53ef\u4ee5\u4e92\u6362\u7684\u3002\u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u638c\u63e1\u4e86\u8db3\u591f\u7684\u77e5\u8bc6\uff0c\u53ef\u4ee5\u53bb\u7406\u89e3\u5b83\u4eec\u4e4b\u95f4\u7684\u4e0d\u540c\u4e86\u3002 \u5b9a\u4e49(definition) \u5b9e\u9645\u4e0a\u6307\u7684\u662f\u5bf9\u67d0\u4e2a \u6807\u8bc6\u7b26(identifier) \u7684\u5b9e\u73b0\uff08\u5bf9\u4e8e\u51fd\u6570\u6216\u7c7b\u578b\u6765\u8bf4\uff09\u6216\u5b9e\u4f8b\u5316\uff08\u5bf9\u4e8e\u53d8\u91cf\u6765\u8bf4\uff09\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u6709\u5173\u5b9a\u4e49\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 int add ( int x , int y ) // implements function add() { int z { x + y }; // instantiates variable z return z ; } \u5b9a\u4e49\u662f\u4e3a\u4e86\u6ee1\u8db3\u94fe\u63a5\u5668\u7684\u9700\u8981 \u3002\u5982\u679c\u4f60\u4f7f\u7528\u4e86\u672a\u5b9a\u4e49\u7684\u6807\u8bc6\u7b26\uff0c\u94fe\u63a5\u5668\u5c31\u4f1a\u62a5\u9519\u3002 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule,ODR) \u662f C++\u4e2d\u6700\u8457\u540d\u7684\u89c4\u5219\u4e4b\u4e00\u3002 \u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u4e00\u4e2a\u51fd\u6570\u3001\u53d8\u91cf\u3001\u7c7b\u578b\u6216\u6a21\u677f\u53ea\u80fd\u591f\u88ab\u5b9a\u4e49\u4e00\u6b21\uff1b \u5728\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u4e00\u4e2a\u53d8\u91cf\u6216\u666e\u901a\u51fd\u6570\u53ea\u80fd\u591f\u6709\u4e00\u4e2a\u5b9a\u4e49\u3002\u548c\u4e0a\u4e00\u6761\u7684\u533a\u522b\u5728\u4e8e\u4e00\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u7531\u591a\u4e2a\u6587\u4ef6\u7ec4\u6210\uff08\u540e\u9762\u7684\u8bfe\u7a0b\u4f1a\u4ecb\u7ecd\uff09\uff1b \u7c7b\u578b\u3001\u6a21\u677f\u3001\u5185\u8054\u51fd\u6570\u548c\u5185\u8054\u53d8\u91cf \u53ef\u4ee5\u6709\u4e00\u6837\u7684\u5b9a\u4e49 \uff0c\u4f46\u5fc5\u987b\u4f4d\u4e8e\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\u3002\u7531\u4e8e\u6211\u4eec\u5c1a\u672a\u4ecb\u7ecd\u8fd9\u4e9b\u6982\u5ff5\uff0c\u56e0\u6b64\u76ee\u524d\u65e0\u9700\u8003\u8651\u5b83\u4eec\u3002 \u5982\u679c\u8fdd\u53cd\u4e86 ODR \u7684\u7b2c\u4e00\u6761\u89c4\u5219\uff0c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u544a\u91cd\u590d\u5b9a\u4e49\u7684\u9519\u8bef\u3002\u5982\u679c\u8fdd\u53cd\u4e86 ODR \u7684\u7b2c\u4e8c\u6761\u89c4\u5219\uff0c\u5219\u5f88\u53ef\u80fd\u5bfc\u81f4\u94fe\u63a5\u5668\u62a5\u544a\u91cd\u590d\u5b9a\u4e49\u9519\u8bef\u3002\u8fdd\u53cd ODR \u7b2c\u4e09\u6761\u89c4\u5219\u5219\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e0b\u9762\u4f8b\u5b50\u5c55\u793a\u4e86\u8fdd\u53cd\u7b2c\u4e00\u6761\u89c4\u5219\u7684\u60c5\u5f62\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int add ( int x , int y ) { return x + y ; } int add ( int x , int y ) // \u8fdd\u53cd\u4e86ODR\uff0c\u56e0\u4e3a add \u5df2\u7ecf\u88ab\u5b9a\u4e49\u8fc7\u4e86 { return x + y ; } int main () { int x ; int x ; // \u8fdd\u53cd\u4e86ODR\uff0c\u56e0\u4e3a x \u5df2\u7ecf\u88ab\u5b9a\u4e49\u8fc7\u4e86 } \u7531\u4e8e\u4e0a\u8ff0\u7a0b\u5e8f\u8fdd\u53cd\u4e86ODR\u7b2c\u4e00\u6761\u89c4\u5219\uff0cVisual Studio \u7684\u7f16\u8bd1\u5668\u4f1a\u62a5\u544a\u5982\u4e0b\u7f16\u8bd1\u9519\u8bef\uff1a 1 2 3 4 project3.cpp(9): error C2084: function 'int add(int,int)' already has a body project3.cpp(3): note: see previous definition of 'add' project3.cpp(16): error C2086: 'int x': redefinition project3.cpp(15): note: see declaration of 'x' \u6269\u5c55\u9605\u8bfb \u6807\u8bc6\u7b26\u76f8\u540c\u4f46\u53c2\u6570\u4e0d\u540c\u7684\u51fd\u6570\uff0c\u88ab\u770b\u505a\u662f\u4e0d\u540c\u7684\u51fd\u6570\u3002\u6211\u4eec\u4f1a\u5728 8.9 - \u51fd\u6570\u91cd\u8f7d \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u58f0\u660e\u5219\u662f\u4e00\u6761\u8bed\u53e5\uff0c\u5b83\u544a\u8bc9\u7f16\u8bd1\u5668\u67d0\u4e2a\u6807\u8bc6\u7b26\u662f\u5b58\u5728\u7684\uff0c\u5e76\u63d0\u4f9b\u5176\u7c7b\u578b\u4fe1\u606f\u3002\u58f0\u660e\u76f8\u5173\u7684\u4f8b\u5b50\u5982\u4e0b\uff1a 1 2 int add ( int x , int y ); // tells the compiler about a function named \"add\" that takes two int parameters and returns an int. No body! int x ; // tells the compiler about an integer variable named x \u58f0\u660e(declaration) \u7eaf\u7cb9\u4e3a\u4e86\u6ee1\u8db3\u7f16\u8bd1\u5668\u7684\u9700\u8981\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728\u5c1a\u672a\u8fdb\u884c\u5177\u4f53\u5b9a\u4e49\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u524d\u5411\u58f0\u660e\u544a\u77e5\u7f16\u8bd1\u5668\u6807\u8bc6\u7b26\u7684\u4fe1\u606f\u3002 \u5728 C++ \u4e2d\uff0c\u5b9a\u4e49\u4e5f\u5177\u6709\u58f0\u660e\u7684\u529f\u80fd\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728\u4e0a\u9762\u58f0\u660e\u548c\u5b9a\u4e49\u7684\u4f8b\u5b50\u4e2d\u90fd\u51fa\u73b0\u4e86 int x \uff0c\u56e0\u4e3a\u5b83\u65e2\u662f\u58f0\u660e\u4e5f\u662f\u5b9a\u4e49\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5b9a\u4e49\u53ef\u4ee5\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u8981\uff0c\u56e0\u4e3a\u5b83\u65e2\u53ef\u4ee5\u88ab\u7f16\u8bd1\u5668\u4f7f\u7528\uff0c\u4e5f\u53ef\u4ee5\u88ab\u94fe\u63a5\u5668\u4f7f\u7528\u3002\u53ea\u6709\u5f53\u9700\u8981\u5728\u6807\u8bc6\u7b26\u5c1a\u672a\u5b9a\u4e49\u524d\u4f7f\u7528\u5b83\u65f6\uff0c\u624d\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u663e\u5f0f\u7684\u58f0\u660e\u3002 \u5c3d\u7ba1\u6240\u6709\u7684\u5b9a\u4e49\u4e5f\u90fd\u662f\u58f0\u660e\uff0c\u4f46\u53cd\u8fc7\u6765\u5374\u4e0d\u662f\u7684\uff1a\u5e76\u4e0d\u662f\u6240\u6709\u7684\u58f0\u660e\u90fd\u662f\u5b9a\u4e49 \u3002\u4ee5\u51fd\u6570\u539f\u578b\u4e3a\u4f8b\u2014\u2014\u5b83\u53ea\u80fd\u591f\u6ee1\u8db3\u7f16\u8bd1\u5668\u7684\u9700\u8981\uff0c\u4f46\u4e0d\u80fd\u6ee1\u8db3\u94fe\u63a5\u5668\u7684\u9700\u8981\u3002\u5bf9\u4e8e\u90a3\u4e9b\u53ea\u662f\u58f0\u660e\uff0c\u4f46\u662f\u4e0d\u662f\u5b9a\u4e49\u7684\u58f0\u660e\uff0c\u79f0\u4e3a \u7eaf\u58f0\u660e(pure declaration) \u3002 \u5176\u4ed6\u7c7b\u578b\u7684\u7eaf\u58f0\u660e\u5305\u62ec\u53d8\u91cf\u7684\u524d\u5411\u58f0\u660e\u548c\u7c7b\u578b\u58f0\u660e \uff08\u5728\u540e\u7eed\u7684\u7ae0\u8282\u4f1a\u8fdb\u884c\u4ecb\u7ecd\uff0c\u76ee\u524d\u65e0\u9700\u64cd\u5fc3\uff09\u3002 ODR\u539f\u5219\u5e76\u4e0d\u9002\u7528\u4e8e\u7eaf\u58f0\u660e\uff08\u6bd5\u7adfODR\u6307\u7684\u662f \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u800c\u4e0d\u662f \u5355\u4e00\u58f0\u660e\u89c4\u5219 \uff09\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u591a\u6b21\u58f0\u660e\u67d0\u4e2a\u6807\u8bc6\u7b26\uff08\u5c3d\u7ba1\u8fd9\u4e48\u505a\u662f\u591a\u4f59\u7684\uff09\u3002 \u4f5c\u8005\u6ce8 \u5728\u4e00\u822c\u7528\u8bed\u4e2d\uff0c \u58f0\u660e \u4e00\u822c\u6307\u7eaf\u58f0\u660e\uff0c\u800c \u5b9a\u4e49 \u5219\u6307\u7684\u662f\u65e2\u80fd\u505a\u5b9a\u4e49\uff0c\u4e5f\u80fd\u505a\u58f0\u660e\u7684\u5b9a\u4e49\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u79f0 int x; \u4e3a\u5b9a\u4e49\uff0c\u5c3d\u7ba1\u5b83\u65e2\u662f\u5b9a\u4e49\u4e5f\u662f\u58f0\u660e\u3002","title":"\u58f0\u660e vs. \u5b9a\u4e49"},{"location":"2-8-Programs-with-multiple-code-files/","tags":["program organization"],"text":"\u5728\u9879\u76ee\u4e2d\u6dfb\u52a0\u6587\u4ef6 \u968f\u7740\u7a0b\u5e8f\u89c4\u6a21\u4e0d\u65ad\u589e\u5927\uff0c\u7a0b\u5e8f\u901a\u5e38\u4f1a\u88ab\u5206\u5272\u4e3a\u591a\u4e2a\u6587\u4ef6\u4ee5\u65b9\u4fbf\u7ec4\u7ec7\u7a0b\u5e8f\u6216\u589e\u5f3a\u5176\u53ef\u590d\u7528\u6027\u3002\u4f7f\u7528 IDE \u53ef\u4ee5\u8ba9\u7ec4\u7ec7\u591a\u6587\u4ef6\u9879\u76ee\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\u3002\u6211\u4eec\u5df2\u7ecf\u5b66\u4e60\u4e86\u5982\u4f55\u53bb\u521b\u5efa\u5e76\u7f16\u8bd1\u4e00\u4e2a\u5355\u6587\u4ef6\u9879\u76ee\uff0c\u4e3a\u8fd9\u4e2a\u9879\u76ee\u6dfb\u52a0\u4e00\u4e2a\u6587\u4ef6\u662f\u975e\u5e38\u7b80\u5355\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u4e3a\u9879\u76ee\u6dfb\u52a0\u4ee3\u7801\u6587\u4ef6\u65f6\uff0c\u8bf7\u4f7f\u7528 .cpp \u4f5c\u4e3a\u6269\u5c55\u540d\u3002 For Visual Studio users \u5728 Visual Studio \u4e2d\uff0c\u5728\u89e3\u51b3\u65b9\u6848\u6d4f\u89c8\u5668\u7a97\u53e3(Solution Explorer)\u53f3\u952e\u70b9\u51fb Source Files \u76ee\u5f55\u5e76\u9009\u62e9 Add > New Item\u2026 . \u9009\u62e9 C++ File (.cpp) \u3002\u4e3a\u5176\u6307\u5b9a\u4e00\u4e2a\u6587\u4ef6\u540d\uff0c\u5c31\u53ef\u4ee5\u5c06\u6587\u4ef6\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u3002 \u6ce8\u610f\uff1a\u5982\u679c\u4f60\u662f\u4ece File \u83dc\u5355\u800c\u4e0d\u662f Solution Explorer \u7a97\u53e3\u521b\u5efa\u7684\u6587\u4ef6\uff0c\u90a3\u4e48\u8be5\u6587\u4ef6\u4e0d\u4f1a\u88ab\u81ea\u52a8\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u3002\u56e0\u6b64\u4f60\u5fc5\u987b\u5728\u89e3\u51b3\u65b9\u6848\u6d4f\u89c8\u5668\u7a97\u53e3(Solution Explorer)\u53f3\u952e\u70b9\u51fb Source Files \u76ee\u5f55\u5e76\u9009\u62e9 Add > Existing Item \uff0c\u7136\u540e\u9009\u62e9\u521a\u624d\u521b\u5efa\u7684\u6587\u4ef6\u3002 \u63a5\u4e0b\u6765\uff0c\u5728\u7f16\u8bd1\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u4f60\u4f1a\u53d1\u73b0\u7f16\u8bd1\u5668\u4f1a\u5c06\u8be5\u6587\u4ef6\u5217\u51fa\u6765\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5bf9\u5176\u8fdb\u884c\u7f16\u8bd1\u3002 For Code:: Blocks users \u5728 Code:: Blocks \u4e2d\uff0c\u5728 File \u83dc\u5355\u4e2d\u9009\u62e9 New > File\u2026 \u3002 \u5728 New from template \u5bf9\u8bdd\u6846\u4e2d\uff0c\u9009\u62e9 C/C++ source \u5e76\u70b9\u51fb Go \u3002 \u4f60\u53ef\u80fd\u4f1a\u6765\u5230\u4e00\u4e2a welcome to the C/C++ source file wizard \u5bf9\u8bdd\u6846\u3002\u6b64\u65f6\u70b9\u51fb Next \u3002 \u5728\u5411\u5bfc\u7684\u4e0b\u4e00\u4e2a\u9875\u9762\u4e2d\uff0c\u9009\u62e9 \u201cC++\u201d \u5e76\u70b9\u51fb Next \u3002 \u63a5\u4e0b\u6765\uff0c\u4e3a\u8fd9\u4e2a\u65b0\u6587\u4ef6\u8d77\u4e00\u4e2a\u540d\u5b57\uff08\u4e0d\u8981\u5fd8\u4e86\u4f7f\u7528 .cpp \u4f5c\u4e3a\u540e\u7f00\u540d\uff09\uff0c\u7136\u540e\u70b9\u51fb All \u6309\u94ae\u786e\u4fdd\u6240\u6709\u7684\u6784\u5efa\u76ee\u6807\u90fd\u88ab\u52fe\u9009\u4e86\u3002\u6700\u540e\uff0c\u9009\u62e9 finish \u3002 \u73b0\u5728\uff0c\u5f53\u4f60\u7f16\u8bd1\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u4f60\u5c31\u53ef\u4ee5\u770b\u5230\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u65f6\u5217\u51fa\u4e86\u8be5\u6587\u4ef6\u7684\u540d\u5b57\u3002 For GCC/G++ users \u4f60\u53ef\u4ee5\u4ece\u547d\u4ee4\u884c\u6765\u521b\u5efa\u6587\u4ef6\u5e76\u4e3a\u5176\u6307\u5b9a\u4e00\u4e2a\u6587\u4ef6\u540d\u3002\u5728\u7f16\u8bd1\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u4f60\u5fc5\u987b\u5c06\u76f8\u5173\u7684\u6587\u4ef6\u90fd\u5305\u542b\u5728\u7f16\u8bd1\u547d\u4ee4\u4e2d\u3002 g++ main.cpp add.cpp -o main , \u5176\u4e2d\uff0c main.cpp \u548c add.cpp \u662f\u9700\u8981\u7f16\u8bd1\u7684\u6587\u4ef6\uff0c main \u5219\u662f\u8f93\u51fa\u6587\u4ef6\u7684\u6587\u4ef6\u540d\u3002 \u4e00\u4e2a\u591a\u6587\u4ef6\u7a0b\u5e8f\u7684\u4f8b\u5b50 \u5728 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u4e00\u4e2a\u5355\u6587\u4ef6\u4e0d\u80fd\u7f16\u8bd1\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } int add ( int x , int y ) { return x + y ; } \u5728\u7f16\u8bd1\u5668\u89e3\u6790\u5230\u7b2c\u4e94\u884c\u7684 add \u51fd\u6570\u8c03\u7528\u65f6\uff0c\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053 add \u4e3a\u4f55\u7269\uff0c\u56e0\u4e3a\u6211\u4eec\u8fd8\u6ca1\u6709\u5b9a\u4e49\u5b83\u5440\uff01\u4e4b\u524d\u6211\u4eec\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u65f6\u4f7f\u7528\u4e86\u4e24\u79cd\u65b9\u6848\uff0c\u8981\u4e48\u91cd\u65b0\u8c03\u6574\u51fd\u6570\u5b9a\u4e49\u7684\u987a\u5e8f\uff0c\u8981\u4e48\u4f7f\u7528 \u524d\u5411\u58f0\u660e(forward declaration) \u3002 \u73b0\u5728\uff0c\u6765\u770b\u4e00\u4e2a\u7c7b\u4f3c\u7684\u591a\u6587\u4ef6\u7a0b\u5e8f\uff1a add.cpp: 1 2 3 4 int add ( int x , int y ) { return x + y ; } main.cpp: 1 2 3 4 5 6 7 #include int main () { std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; // compile error return 0 ; } \u4f60\u7684\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5148\u7f16\u8bd1 add.cpp \u6216\u8005\u5148\u7f16\u8bd1 main.cpp \u3002\u4e0d\u8bba\u662f\u90a3\u79cd\u60c5\u51b5\uff0c\u7f16\u8bd1\u5668\u90fd\u4f1a\u62a5\u544a\u548c\u4e4b\u524d\u4e00\u6837\u7684\u7f16\u8bd1\u9519\u8bef\uff1a 1 main.cpp(5) : error C3861: 'add': identifier not found \u95ee\u9898\u7684\u539f\u56e0\u4e5f\u662f\u4e00\u6837\u7684\uff0c\u5728\u7f16\u8bd1\u5668\u89e3\u6790\u5230\u7b2c\u4e94\u884c\u7684 add \u51fd\u6570\u8c03\u7528\u65f6\uff0c\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053 add \u4e3a\u4f55\u7269\u3002 \u8fd8\u8bb0\u5f97\u5417\uff1f\u7f16\u8bd1\u5668\u662f\u5355\u72ec\u7f16\u8bd1\u5404\u4e2a\u6587\u4ef6\u7684\uff0c\u5b83\u5e76\u4e0d\u77e5\u9053\u5176\u4ed6\u4ee3\u7801\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\uff0c\u4e5f\u4e0d\u4f1a\u8bb0\u5f97\u4e4b\u524d\u7f16\u8bd1\u65f6\u9047\u5230\u7684\u4e1c\u897f\u3002\u4e5f\u8bb8\u7f16\u8bd1\u5668\u66fe\u7ecf\u9047\u5230\u8fc7\u51fd\u6570 add \u7684\u5b9a\u4e49(\u5982\u679c\u5b83\u5148\u7f16\u8bd1 add.cpp \u7684\u8bdd)\uff0c\u4f46\u5b83\u5e76\u4e0d\u4f1a\u5c06\u5176\u8bb0\u4e0b\u6765\u3002 \u7f16\u8bd1\u5668\u7684\u8fd9\u79cd\u77ed\u89c6\u548c\u5065\u5fd8\u662f\u6709\u610f\u800c\u4e3a\u4e4b\u7684\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5177\u6709\u76f8\u540c\u540d\u5b57\u7684\u51fd\u6570\u548c\u53d8\u91cf\u4e0d\u4f1a\u53d1\u751f\u51b2\u7a81\u3002\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\u4e2d\u63a2\u8ba8\u547d\u540d\u51b2\u7a81\u7684\u95ee\u9898\u3002 \u89e3\u51b3\u7684\u529e\u6cd5\u4e5f\u662f\u4e00\u6837\u7684\uff1a\u5c06 add \u51fd\u6570\u7684\u5b9a\u4e49\u79fb\u5230 main \u51fd\u6570\u4e4b\u524d\uff0c\u6216\u8005\u4f7f\u7528\u524d\u5411\u58f0\u660e\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u7531\u4e8e add \u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\uff0c\u56e0\u6b64\u8c03\u6574\u5b9a\u4e49\u987a\u5e8f\u662f\u4e0d\u53ef\u4ee5\u7684\u3002 \u56e0\u6b64\u4f7f\u7528\u524d\u5411\u58f0\u660e\u624d\u662f\u66f4\u597d\u7684\u529e\u6cd5\uff1a main.cpp (\u5305\u542b\u524d\u5411\u58f0\u660e): 1 2 3 4 5 6 7 8 9 #include int add ( int x , int y ); // \u8fd9\u884c\u662f\u5fc5\u987b\u7684\uff0c\u8fd9\u6837main.cpp\u624d\u80fd\u591f\u77e5\u9053add()\u51fd\u6570\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e86 int main () { std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } add.cpp (\u4fdd\u6301\u4e0d\u53d8): 1 2 3 4 int add ( int x , int y ) { return x + y ; } \u73b0\u5728\uff0c\u5f53\u7f16\u8bd1\u5668\u7f16\u8bd1 main.cpp \u65f6\uff0c\u5b83\u4f1a\u77e5\u6653\u6807\u8bc6\u7b26 add \u3002\u94fe\u63a5\u5668\u5219\u4f1a\u5c06 main.cpp \u6587\u4ef6\u4e2d\u7684 add \u51fd\u6570\u8c03\u7528\u548c _add.cpp_\u4e2d\u7684\u51fd\u6570\u5b9a\u4e49\u5173\u8054\u8d77\u6765\u3002 \u4f7f\u7528\u8be5\u65b9\u6cd5\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8bbf\u95ee\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u4e86\u3002 \u8bf7\u81ea\u884c\u4f7f\u7528\u524d\u5411\u58f0\u660e\u5e76\u7f16\u8bd1 add.cpp \u548c main.cpp \uff0c\u5982\u679c\u4f60\u9047\u5230\u4e86\u9519\u8bef\uff0c\u8bf7\u6ce8\u610f\u662f\u5426\u5c06 add.cpp \u52a0\u5165\u5230\u4e86\u9879\u76ee\u6216\u7f16\u8bd1\u547d\u4ee4\u4e2d\u3002 \u53d1\u751f\u9519\u8bef\u4e86\uff01 \u5728\u521b\u5efa\u591a\u6587\u4ef6\u7a0b\u5e8f\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5f88\u591a\u4e0d\u540c\u9519\u8bef\u3002\u5982\u679c\u4f60\u5c1d\u8bd5\u4e86\u4e0a\u9762\u7684\u4f8b\u5b50\u5e76\u9047\u5230\u4e86\u4e00\u4e9b\u9519\u8bef\uff0c\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u4fe1\u606f\uff1a \u5982\u679c\u4f60\u9047\u5230\u4e86 main \u4e2d\u6ca1\u6709\u5b9a\u4e49 add \u7684\u95ee\u9898\uff0c\u548c\u53ef\u80fd\u662f\u56e0\u4e3a\u5fd8\u8bb0\u5728 main.cpp \u4e2d\u5bf9 add \u51fd\u6570\u8fdb\u884c\u524d\u5411\u58f0\u660e\u3002 \u5982\u679c\u94fe\u63a5\u5668\u62a5\u544a\u4e86\u65e0\u6cd5\u627e\u5230 add \u5b9a\u4e49\u7684\u95ee\u9898\uff0c\u4f8b\u5982\uff1a 1 unresolved external symbol \"int __cdecl add(int,int)\" (?add@@YAHHH@Z) referenced in function _main \u6700\u5927\u7684\u53ef\u80fd\u6027\u662f add.cpp \u6587\u4ef6\u6ca1\u6709\u88ab\u6b63\u786e\u5730\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u3002\u5728\u7f16\u8bd1\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u770b\u5230\u7f16\u8bd1\u5668\u5217\u51fa\u4e86 main.cpp \u548c add.cpp \u3002\u5982\u679c\u4f60\u503c\u770b\u5230\u4e86 main.cpp \uff0c\u90a3\u4e48 add.cpp \u80af\u5b9a\u6ca1\u6709\u88ab\u7f16\u8bd1\u3002\u5982\u679c\u4f60\u4f7f\u7528 Visual Studio \u6216 Code:: Blocks\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u5728 Solution Explorer/project \u4e2d\u770b\u5230 add.cpp \u3002\u5982\u679c\u4f60\u6ca1\u6709\u770b\u5230\uff0c\u8bf7\u53f3\u952e\u70b9\u51fb\u9879\u76ee\u5e76\u6dfb\u52a0\u6587\u4ef6\uff0c\u7136\u540e\u5c1d\u8bd5\u91cd\u65b0\u7f16\u8bd1\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u547d\u4ee4\u884c\u7684\u65b9\u5f0f\uff0c\u8bf7\u5c06 main.cpp \u548c add.cpp \u90fd\u5305\u542b\u5728\u7f16\u8bd1\u547d\u4ee4\u4e2d\u3002 \u4f60\u662f\u4e0d\u662f\u5c06_add.cpp_ \u6dfb\u52a0\u5230\u4e86\u9519\u8bef\u7684\u9879\u76ee\u4e2d\uff1f \u4e5f\u6709\u53ef\u80fd\u662f\u8be5\u6587\u4ef6\u88ab\u8bbe\u7f6e\u4e3a\u4e0d\u7f16\u8bd1\u94fe\u63a5\u3002\u8bf7\u68c0\u67e5\u6587\u4ef6\u5c5e\u6027\u5e76\u786e\u4fdd\u5c06\u5176\u914d\u7f6e\u4e3a\u7f16\u8bd1/\u94fe\u63a5\u3002\u5728 Code:: Blocks \u4e2d\uff0c\u7f16\u8bd1\u548c\u94fe\u63a5\u5728\u4e24\u4e2a\u5355\u72ec\u7684\u9009\u62e9\u6846\u4e2d\uff0c\u8bf7\u786e\u4fdd\u5b83\u4eec\u90fd\u88ab\u9009\u4e2d\u4e86\u3002\u5bf9\u4e8e Visual Studio \u6765\u8bf4\uff0c\u8bf7\u786e\u4fdd \u201cexclude from build\u201d \u9009\u9879\u88ab\u8bbe\u7f6e\u4e3a no \u6216\u7a7a\u767d\u3002 \u4e0d\u8981\u5728 main.cpp \u4e2d #include \u201cadd.cpp\u201d \u3002\u8fd9\u4e48\u505a\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u5c06 add.cpp \u6587\u4ef6\u7684\u5185\u5bb9\u76f4\u63a5\u63d2\u5165 main.cpp \u800c\u4e0d\u662f\u5c06\u5176\u5f53\u505a\u5355\u72ec\u7684\u6587\u4ef6\u3002 \u5c0f\u7ed3 \u5f53\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u591a\u6587\u4ef6\u7a0b\u5e8f\u65f6\uff0c\u5b83\u53ef\u80fd\u4f1a\u4ee5\u4efb\u610f\u987a\u5e8f\u6765\u7f16\u8bd1\u5404\u4e2a\u6587\u4ef6\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u7f16\u8bd1\u5668\u5bf9\u4e2a\u6587\u4ef6\u7684\u7f16\u8bd1\u8fd8\u662f\u72ec\u7acb\u7684\uff0c\u5b83\u5728\u7f16\u8bd1\u67d0\u6587\u4ef6\u662f\u4e0d\u5177\u5907\u4efb\u4f55\u5176\u4ed6\u6587\u4ef6\u7684\u4fe1\u606f\u3002 \u5728\u5b66\u4e60\u5230\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u540e\uff0c\u6211\u4eec\u4f1a\u5927\u91cf\u4f7f\u7528\u591a\u6587\u4ef6\u6784\u5efa\u7a0b\u5e8f\uff0c\u6240\u4ee5\u4f60\u4ece\u73b0\u5728\u5c31\u5e94\u8be5\u597d\u597d\u638c\u63e1\u8fd9\u79cd\u65b9\u5f0f\u3002 \u63d0\u9192\u4e00\u4e0b\uff1a\u4efb\u4f55\u65b0\u521b\u5efa\u7684\u4ee3\u7801\u6587\u4ef6\u90fd\u5e94\u8be5\u88ab\u52a0\u5165\u5230\u9879\u76ee\u4e2d\uff0c\u8fd9\u6837\u5b83\u624d\u4f1a\u88ab\u7f16\u8bd1\u3002","title":"2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f"},{"location":"2-8-Programs-with-multiple-code-files/#_1","text":"\u968f\u7740\u7a0b\u5e8f\u89c4\u6a21\u4e0d\u65ad\u589e\u5927\uff0c\u7a0b\u5e8f\u901a\u5e38\u4f1a\u88ab\u5206\u5272\u4e3a\u591a\u4e2a\u6587\u4ef6\u4ee5\u65b9\u4fbf\u7ec4\u7ec7\u7a0b\u5e8f\u6216\u589e\u5f3a\u5176\u53ef\u590d\u7528\u6027\u3002\u4f7f\u7528 IDE \u53ef\u4ee5\u8ba9\u7ec4\u7ec7\u591a\u6587\u4ef6\u9879\u76ee\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\u3002\u6211\u4eec\u5df2\u7ecf\u5b66\u4e60\u4e86\u5982\u4f55\u53bb\u521b\u5efa\u5e76\u7f16\u8bd1\u4e00\u4e2a\u5355\u6587\u4ef6\u9879\u76ee\uff0c\u4e3a\u8fd9\u4e2a\u9879\u76ee\u6dfb\u52a0\u4e00\u4e2a\u6587\u4ef6\u662f\u975e\u5e38\u7b80\u5355\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u4e3a\u9879\u76ee\u6dfb\u52a0\u4ee3\u7801\u6587\u4ef6\u65f6\uff0c\u8bf7\u4f7f\u7528 .cpp \u4f5c\u4e3a\u6269\u5c55\u540d\u3002 For Visual Studio users \u5728 Visual Studio \u4e2d\uff0c\u5728\u89e3\u51b3\u65b9\u6848\u6d4f\u89c8\u5668\u7a97\u53e3(Solution Explorer)\u53f3\u952e\u70b9\u51fb Source Files \u76ee\u5f55\u5e76\u9009\u62e9 Add > New Item\u2026 . \u9009\u62e9 C++ File (.cpp) \u3002\u4e3a\u5176\u6307\u5b9a\u4e00\u4e2a\u6587\u4ef6\u540d\uff0c\u5c31\u53ef\u4ee5\u5c06\u6587\u4ef6\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u3002 \u6ce8\u610f\uff1a\u5982\u679c\u4f60\u662f\u4ece File \u83dc\u5355\u800c\u4e0d\u662f Solution Explorer \u7a97\u53e3\u521b\u5efa\u7684\u6587\u4ef6\uff0c\u90a3\u4e48\u8be5\u6587\u4ef6\u4e0d\u4f1a\u88ab\u81ea\u52a8\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u3002\u56e0\u6b64\u4f60\u5fc5\u987b\u5728\u89e3\u51b3\u65b9\u6848\u6d4f\u89c8\u5668\u7a97\u53e3(Solution Explorer)\u53f3\u952e\u70b9\u51fb Source Files \u76ee\u5f55\u5e76\u9009\u62e9 Add > Existing Item \uff0c\u7136\u540e\u9009\u62e9\u521a\u624d\u521b\u5efa\u7684\u6587\u4ef6\u3002 \u63a5\u4e0b\u6765\uff0c\u5728\u7f16\u8bd1\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u4f60\u4f1a\u53d1\u73b0\u7f16\u8bd1\u5668\u4f1a\u5c06\u8be5\u6587\u4ef6\u5217\u51fa\u6765\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5bf9\u5176\u8fdb\u884c\u7f16\u8bd1\u3002 For Code:: Blocks users \u5728 Code:: Blocks \u4e2d\uff0c\u5728 File \u83dc\u5355\u4e2d\u9009\u62e9 New > File\u2026 \u3002 \u5728 New from template \u5bf9\u8bdd\u6846\u4e2d\uff0c\u9009\u62e9 C/C++ source \u5e76\u70b9\u51fb Go \u3002 \u4f60\u53ef\u80fd\u4f1a\u6765\u5230\u4e00\u4e2a welcome to the C/C++ source file wizard \u5bf9\u8bdd\u6846\u3002\u6b64\u65f6\u70b9\u51fb Next \u3002 \u5728\u5411\u5bfc\u7684\u4e0b\u4e00\u4e2a\u9875\u9762\u4e2d\uff0c\u9009\u62e9 \u201cC++\u201d \u5e76\u70b9\u51fb Next \u3002 \u63a5\u4e0b\u6765\uff0c\u4e3a\u8fd9\u4e2a\u65b0\u6587\u4ef6\u8d77\u4e00\u4e2a\u540d\u5b57\uff08\u4e0d\u8981\u5fd8\u4e86\u4f7f\u7528 .cpp \u4f5c\u4e3a\u540e\u7f00\u540d\uff09\uff0c\u7136\u540e\u70b9\u51fb All \u6309\u94ae\u786e\u4fdd\u6240\u6709\u7684\u6784\u5efa\u76ee\u6807\u90fd\u88ab\u52fe\u9009\u4e86\u3002\u6700\u540e\uff0c\u9009\u62e9 finish \u3002 \u73b0\u5728\uff0c\u5f53\u4f60\u7f16\u8bd1\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u4f60\u5c31\u53ef\u4ee5\u770b\u5230\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u65f6\u5217\u51fa\u4e86\u8be5\u6587\u4ef6\u7684\u540d\u5b57\u3002 For GCC/G++ users \u4f60\u53ef\u4ee5\u4ece\u547d\u4ee4\u884c\u6765\u521b\u5efa\u6587\u4ef6\u5e76\u4e3a\u5176\u6307\u5b9a\u4e00\u4e2a\u6587\u4ef6\u540d\u3002\u5728\u7f16\u8bd1\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u4f60\u5fc5\u987b\u5c06\u76f8\u5173\u7684\u6587\u4ef6\u90fd\u5305\u542b\u5728\u7f16\u8bd1\u547d\u4ee4\u4e2d\u3002 g++ main.cpp add.cpp -o main , \u5176\u4e2d\uff0c main.cpp \u548c add.cpp \u662f\u9700\u8981\u7f16\u8bd1\u7684\u6587\u4ef6\uff0c main \u5219\u662f\u8f93\u51fa\u6587\u4ef6\u7684\u6587\u4ef6\u540d\u3002","title":"\u5728\u9879\u76ee\u4e2d\u6dfb\u52a0\u6587\u4ef6"},{"location":"2-8-Programs-with-multiple-code-files/#_2","text":"\u5728 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u4e00\u4e2a\u5355\u6587\u4ef6\u4e0d\u80fd\u7f16\u8bd1\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } int add ( int x , int y ) { return x + y ; } \u5728\u7f16\u8bd1\u5668\u89e3\u6790\u5230\u7b2c\u4e94\u884c\u7684 add \u51fd\u6570\u8c03\u7528\u65f6\uff0c\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053 add \u4e3a\u4f55\u7269\uff0c\u56e0\u4e3a\u6211\u4eec\u8fd8\u6ca1\u6709\u5b9a\u4e49\u5b83\u5440\uff01\u4e4b\u524d\u6211\u4eec\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u65f6\u4f7f\u7528\u4e86\u4e24\u79cd\u65b9\u6848\uff0c\u8981\u4e48\u91cd\u65b0\u8c03\u6574\u51fd\u6570\u5b9a\u4e49\u7684\u987a\u5e8f\uff0c\u8981\u4e48\u4f7f\u7528 \u524d\u5411\u58f0\u660e(forward declaration) \u3002 \u73b0\u5728\uff0c\u6765\u770b\u4e00\u4e2a\u7c7b\u4f3c\u7684\u591a\u6587\u4ef6\u7a0b\u5e8f\uff1a add.cpp: 1 2 3 4 int add ( int x , int y ) { return x + y ; } main.cpp: 1 2 3 4 5 6 7 #include int main () { std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; // compile error return 0 ; } \u4f60\u7684\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5148\u7f16\u8bd1 add.cpp \u6216\u8005\u5148\u7f16\u8bd1 main.cpp \u3002\u4e0d\u8bba\u662f\u90a3\u79cd\u60c5\u51b5\uff0c\u7f16\u8bd1\u5668\u90fd\u4f1a\u62a5\u544a\u548c\u4e4b\u524d\u4e00\u6837\u7684\u7f16\u8bd1\u9519\u8bef\uff1a 1 main.cpp(5) : error C3861: 'add': identifier not found \u95ee\u9898\u7684\u539f\u56e0\u4e5f\u662f\u4e00\u6837\u7684\uff0c\u5728\u7f16\u8bd1\u5668\u89e3\u6790\u5230\u7b2c\u4e94\u884c\u7684 add \u51fd\u6570\u8c03\u7528\u65f6\uff0c\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053 add \u4e3a\u4f55\u7269\u3002 \u8fd8\u8bb0\u5f97\u5417\uff1f\u7f16\u8bd1\u5668\u662f\u5355\u72ec\u7f16\u8bd1\u5404\u4e2a\u6587\u4ef6\u7684\uff0c\u5b83\u5e76\u4e0d\u77e5\u9053\u5176\u4ed6\u4ee3\u7801\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\uff0c\u4e5f\u4e0d\u4f1a\u8bb0\u5f97\u4e4b\u524d\u7f16\u8bd1\u65f6\u9047\u5230\u7684\u4e1c\u897f\u3002\u4e5f\u8bb8\u7f16\u8bd1\u5668\u66fe\u7ecf\u9047\u5230\u8fc7\u51fd\u6570 add \u7684\u5b9a\u4e49(\u5982\u679c\u5b83\u5148\u7f16\u8bd1 add.cpp \u7684\u8bdd)\uff0c\u4f46\u5b83\u5e76\u4e0d\u4f1a\u5c06\u5176\u8bb0\u4e0b\u6765\u3002 \u7f16\u8bd1\u5668\u7684\u8fd9\u79cd\u77ed\u89c6\u548c\u5065\u5fd8\u662f\u6709\u610f\u800c\u4e3a\u4e4b\u7684\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5177\u6709\u76f8\u540c\u540d\u5b57\u7684\u51fd\u6570\u548c\u53d8\u91cf\u4e0d\u4f1a\u53d1\u751f\u51b2\u7a81\u3002\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\u4e2d\u63a2\u8ba8\u547d\u540d\u51b2\u7a81\u7684\u95ee\u9898\u3002 \u89e3\u51b3\u7684\u529e\u6cd5\u4e5f\u662f\u4e00\u6837\u7684\uff1a\u5c06 add \u51fd\u6570\u7684\u5b9a\u4e49\u79fb\u5230 main \u51fd\u6570\u4e4b\u524d\uff0c\u6216\u8005\u4f7f\u7528\u524d\u5411\u58f0\u660e\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u7531\u4e8e add \u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\uff0c\u56e0\u6b64\u8c03\u6574\u5b9a\u4e49\u987a\u5e8f\u662f\u4e0d\u53ef\u4ee5\u7684\u3002 \u56e0\u6b64\u4f7f\u7528\u524d\u5411\u58f0\u660e\u624d\u662f\u66f4\u597d\u7684\u529e\u6cd5\uff1a main.cpp (\u5305\u542b\u524d\u5411\u58f0\u660e): 1 2 3 4 5 6 7 8 9 #include int add ( int x , int y ); // \u8fd9\u884c\u662f\u5fc5\u987b\u7684\uff0c\u8fd9\u6837main.cpp\u624d\u80fd\u591f\u77e5\u9053add()\u51fd\u6570\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e86 int main () { std :: cout << \"The sum of 3 and 4 is: \" << add ( 3 , 4 ) << '\\n' ; return 0 ; } add.cpp (\u4fdd\u6301\u4e0d\u53d8): 1 2 3 4 int add ( int x , int y ) { return x + y ; } \u73b0\u5728\uff0c\u5f53\u7f16\u8bd1\u5668\u7f16\u8bd1 main.cpp \u65f6\uff0c\u5b83\u4f1a\u77e5\u6653\u6807\u8bc6\u7b26 add \u3002\u94fe\u63a5\u5668\u5219\u4f1a\u5c06 main.cpp \u6587\u4ef6\u4e2d\u7684 add \u51fd\u6570\u8c03\u7528\u548c _add.cpp_\u4e2d\u7684\u51fd\u6570\u5b9a\u4e49\u5173\u8054\u8d77\u6765\u3002 \u4f7f\u7528\u8be5\u65b9\u6cd5\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8bbf\u95ee\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u4e86\u3002 \u8bf7\u81ea\u884c\u4f7f\u7528\u524d\u5411\u58f0\u660e\u5e76\u7f16\u8bd1 add.cpp \u548c main.cpp \uff0c\u5982\u679c\u4f60\u9047\u5230\u4e86\u9519\u8bef\uff0c\u8bf7\u6ce8\u610f\u662f\u5426\u5c06 add.cpp \u52a0\u5165\u5230\u4e86\u9879\u76ee\u6216\u7f16\u8bd1\u547d\u4ee4\u4e2d\u3002","title":"\u4e00\u4e2a\u591a\u6587\u4ef6\u7a0b\u5e8f\u7684\u4f8b\u5b50"},{"location":"2-8-Programs-with-multiple-code-files/#_3","text":"\u5728\u521b\u5efa\u591a\u6587\u4ef6\u7a0b\u5e8f\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5f88\u591a\u4e0d\u540c\u9519\u8bef\u3002\u5982\u679c\u4f60\u5c1d\u8bd5\u4e86\u4e0a\u9762\u7684\u4f8b\u5b50\u5e76\u9047\u5230\u4e86\u4e00\u4e9b\u9519\u8bef\uff0c\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u4fe1\u606f\uff1a \u5982\u679c\u4f60\u9047\u5230\u4e86 main \u4e2d\u6ca1\u6709\u5b9a\u4e49 add \u7684\u95ee\u9898\uff0c\u548c\u53ef\u80fd\u662f\u56e0\u4e3a\u5fd8\u8bb0\u5728 main.cpp \u4e2d\u5bf9 add \u51fd\u6570\u8fdb\u884c\u524d\u5411\u58f0\u660e\u3002 \u5982\u679c\u94fe\u63a5\u5668\u62a5\u544a\u4e86\u65e0\u6cd5\u627e\u5230 add \u5b9a\u4e49\u7684\u95ee\u9898\uff0c\u4f8b\u5982\uff1a 1 unresolved external symbol \"int __cdecl add(int,int)\" (?add@@YAHHH@Z) referenced in function _main \u6700\u5927\u7684\u53ef\u80fd\u6027\u662f add.cpp \u6587\u4ef6\u6ca1\u6709\u88ab\u6b63\u786e\u5730\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u3002\u5728\u7f16\u8bd1\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u770b\u5230\u7f16\u8bd1\u5668\u5217\u51fa\u4e86 main.cpp \u548c add.cpp \u3002\u5982\u679c\u4f60\u503c\u770b\u5230\u4e86 main.cpp \uff0c\u90a3\u4e48 add.cpp \u80af\u5b9a\u6ca1\u6709\u88ab\u7f16\u8bd1\u3002\u5982\u679c\u4f60\u4f7f\u7528 Visual Studio \u6216 Code:: Blocks\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u5728 Solution Explorer/project \u4e2d\u770b\u5230 add.cpp \u3002\u5982\u679c\u4f60\u6ca1\u6709\u770b\u5230\uff0c\u8bf7\u53f3\u952e\u70b9\u51fb\u9879\u76ee\u5e76\u6dfb\u52a0\u6587\u4ef6\uff0c\u7136\u540e\u5c1d\u8bd5\u91cd\u65b0\u7f16\u8bd1\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u547d\u4ee4\u884c\u7684\u65b9\u5f0f\uff0c\u8bf7\u5c06 main.cpp \u548c add.cpp \u90fd\u5305\u542b\u5728\u7f16\u8bd1\u547d\u4ee4\u4e2d\u3002 \u4f60\u662f\u4e0d\u662f\u5c06_add.cpp_ \u6dfb\u52a0\u5230\u4e86\u9519\u8bef\u7684\u9879\u76ee\u4e2d\uff1f \u4e5f\u6709\u53ef\u80fd\u662f\u8be5\u6587\u4ef6\u88ab\u8bbe\u7f6e\u4e3a\u4e0d\u7f16\u8bd1\u94fe\u63a5\u3002\u8bf7\u68c0\u67e5\u6587\u4ef6\u5c5e\u6027\u5e76\u786e\u4fdd\u5c06\u5176\u914d\u7f6e\u4e3a\u7f16\u8bd1/\u94fe\u63a5\u3002\u5728 Code:: Blocks \u4e2d\uff0c\u7f16\u8bd1\u548c\u94fe\u63a5\u5728\u4e24\u4e2a\u5355\u72ec\u7684\u9009\u62e9\u6846\u4e2d\uff0c\u8bf7\u786e\u4fdd\u5b83\u4eec\u90fd\u88ab\u9009\u4e2d\u4e86\u3002\u5bf9\u4e8e Visual Studio \u6765\u8bf4\uff0c\u8bf7\u786e\u4fdd \u201cexclude from build\u201d \u9009\u9879\u88ab\u8bbe\u7f6e\u4e3a no \u6216\u7a7a\u767d\u3002 \u4e0d\u8981\u5728 main.cpp \u4e2d #include \u201cadd.cpp\u201d \u3002\u8fd9\u4e48\u505a\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u5c06 add.cpp \u6587\u4ef6\u7684\u5185\u5bb9\u76f4\u63a5\u63d2\u5165 main.cpp \u800c\u4e0d\u662f\u5c06\u5176\u5f53\u505a\u5355\u72ec\u7684\u6587\u4ef6\u3002","title":"\u53d1\u751f\u9519\u8bef\u4e86\uff01"},{"location":"2-8-Programs-with-multiple-code-files/#_4","text":"\u5f53\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u591a\u6587\u4ef6\u7a0b\u5e8f\u65f6\uff0c\u5b83\u53ef\u80fd\u4f1a\u4ee5\u4efb\u610f\u987a\u5e8f\u6765\u7f16\u8bd1\u5404\u4e2a\u6587\u4ef6\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u7f16\u8bd1\u5668\u5bf9\u4e2a\u6587\u4ef6\u7684\u7f16\u8bd1\u8fd8\u662f\u72ec\u7acb\u7684\uff0c\u5b83\u5728\u7f16\u8bd1\u67d0\u6587\u4ef6\u662f\u4e0d\u5177\u5907\u4efb\u4f55\u5176\u4ed6\u6587\u4ef6\u7684\u4fe1\u606f\u3002 \u5728\u5b66\u4e60\u5230\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u540e\uff0c\u6211\u4eec\u4f1a\u5927\u91cf\u4f7f\u7528\u591a\u6587\u4ef6\u6784\u5efa\u7a0b\u5e8f\uff0c\u6240\u4ee5\u4f60\u4ece\u73b0\u5728\u5c31\u5e94\u8be5\u597d\u597d\u638c\u63e1\u8fd9\u79cd\u65b9\u5f0f\u3002 \u63d0\u9192\u4e00\u4e0b\uff1a\u4efb\u4f55\u65b0\u521b\u5efa\u7684\u4ee3\u7801\u6587\u4ef6\u90fd\u5e94\u8be5\u88ab\u52a0\u5165\u5230\u9879\u76ee\u4e2d\uff0c\u8fd9\u6837\u5b83\u624d\u4f1a\u88ab\u7f16\u8bd1\u3002","title":"\u5c0f\u7ed3"},{"location":"2-9-Naming-collisions-and-an-introduction-to-namespaces/","tags":["namespace"],"text":"\u60f3\u8c61\u4e00\u4e0b\uff0c\u4f60\u7b2c\u4e00\u6b21\u5f00\u8f66\u53bb\u670b\u53cb\u5bb6\uff0c\u4ed6\u7ed9\u4f60\u7684\u5730\u5740\u662f\u897f\u7ea2\u5e02\u91d1\u878d\u8857 245 \u53f7\u3002\u4f60\u5728\u53bb\u4e4b\u524d\uff0c\u767e\u5ea6\u5730\u56fe\u4e86\u4e00\u4e0b\u8fd9\u4e2a\u5730\u5740\uff0c\u7ed3\u679c\u53d1\u73b0\u897f\u7ea2\u5e02\u7adf\u7136\u6709\u4e24\u6761\u91d1\u878d\u8857\u3002\u90a3\u4e48\u4f60\u5e94\u8be5\u53bb\u90a3\u6761\u8857\u7684 245 \u53f7\u5462\uff1f\u9664\u975e\u4f60\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u4fe1\u606f\uff08\u4f8b\u5982\u4f60\u5370\u8c61\u4e2d\u4ed6\u7684\u623f\u5b50\u9760\u7740\u4e00\u6761\u6c5f\uff09\uff0c\u4e0d\u7136\u4f60\u53ea\u80fd\u6253\u7535\u8bdd\u7ed9\u670b\u53cb\u95ee\u4e2a\u6e05\u695a\u4e86\u3002\u5728\u73b0\u5b9e\u4e2d\uff0c\u5982\u679c\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\u7684\u8857\u548c\u95e8\u724c\u53f7\u4e0d\u662f\u552f\u4e00\u7684\uff0c\u90a3\u4e48\u65e0\u7591\u4f1a\u975e\u5e38\u4ee4\u4eba\u56f0\u60d1\uff0c\u4e5f\u4f1a\u964d\u4f4e\u57ce\u5e02\u7684\u8fd0\u884c\u6548\u7387\uff08\u4f8b\u5982\u5feb\u9012\u6548\u7387\uff09\u3002 \u7c7b\u4f3c\u7684\uff0cC++\u9700\u8981\u4fdd\u8bc1\u5176\u6240\u6709\u7684\u6807\u8bc6\u7b26\u90fd\u662f\u6ca1\u6709\u6b67\u4e49\u7684\u3002\u5982\u679c\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\u6709\u4e24\u4e2a\u76f8\u540c\u7684\u6807\u8bc6\u7b26\uff0c\u90a3\u4e48 \u7f16\u8bd1\u5668(compiler) \u6216\u8005 \u94fe\u63a5 \u5c06\u65e0\u6cd5\u533a\u5206\u5b83\u4eec\uff0c\u56e0\u6b64\u5fc5\u7136\u4f1a\u62a5\u544a\u9519\u8bef\u3002\u8fd9\u4e2a\u9519\u8bef\u901a\u5e38\u662f\u5173\u4e8e\u547d\u540d\u51b2\u7a81\u7684\u3002 \u547d\u540d\u51b2\u7a81\u5b9e\u4f8b a.cpp: 1 2 3 4 5 6 #include void myFcn ( int x ) { std :: cout << x ; } COPY main.cpp: 1 2 3 4 5 6 7 8 9 10 11 #include void myFcn ( int x ) { std :: cout << 2 * x ; } int main () { return 0 ; } \u5f53\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u6b64\u7a0b\u5e8f\u65f6\uff0c\u5b83\u4f1a\u72ec\u7acb\u5730\u7f16\u8bd1 a.cpp \u548c main.cpp \u4e24\u4e2a\u6587\u4ef6\uff0c\u5e76\u4e14\u4e0d\u4f1a\u6709\u4efb\u4f55\u95ee\u9898\u3002 \u4e0d\u8fc7\uff0c\u5f53\u94fe\u63a5\u5668\u5f00\u59cb\u5de5\u4f5c\u65f6\uff0c\u5b83\u4f1a\u628a a.cpp \u548c main.cpp \u4e2d\u7684\u5b9a\u4e49\u5173\u8054\u8d77\u6765\uff0c\u7136\u540e\u5b83\u5c31\u4f1a\u53d1\u73b0 myFcn \u51fd\u6570\u7684\u5b9a\u4e49\u51b2\u7a81\u4e86\u3002\u968f\u540e\u94fe\u63a5\u5668\u4fbf\u4f1a\u56e0\u4e3a\u62a5\u9519\u800c\u505c\u6b62\u5de5\u4f5c\u3002\u6ce8\u610f\uff0c\u5373\u4f7f myFcn \u59cb\u7ec8\u6ca1\u6709\u88ab\u8c03\u7528\uff0c\u9519\u8bef\u4ecd\u7136\u4f1a\u53d1\u751f\uff01 \u5927\u591a\u6570\u547d\u540d\u51b2\u7a81\u7531\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\u5f15\u8d77\uff1a \u4e00\u4e2a\u51fd\u6570\uff08\u6216\u5c40\u90e8\u53d8\u91cf\uff09\u7684\u4e24\u4e2a\uff08\u6216\u591a\u4e2a\uff09\u5b9a\u4e49\u88ab\u5b9a\u4e49\u5728\u4e86\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\uff0c\u4f46\u88ab\u7f16\u8bd1\u5230\u4e86\u540c\u6837\u7684\u7a0b\u5e8f\u4e2d\u3002\u8fd9\u6837\u4e00\u6765\u5c31\u4f1a\u5bfc\u81f4\u94fe\u63a5\u5668\u62a5\u9519\uff0c\u5982\u4e0a\u9762\u63cf\u8ff0\u7684\u90a3\u6837\uff1b \u4e00\u4e2a\u51fd\u6570\uff08\u6216\u5c40\u90e8\u53d8\u91cf\uff09\u7684\u4e24\u4e2a\uff08\u6216\u591a\u4e2a\uff09\u5b9a\u4e49\u88ab\u5f15\u5165\u4e86\u540c\u6837\u7684\u51fd\u6570\uff08\u901a\u5e38\u662f\u901a\u8fc7 #include \u5b9e\u73b0\u7684\uff09\u3002\u8fd9\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\u3002 \u968f\u7740\u7a0b\u5e8f\u89c4\u6a21\u7684\u589e\u957f\uff0c\u7a0b\u5e8f\u4e2d\u7684\u6807\u8bc6\u7b26\u4e5f\u8d8a\u6765\u8d8a\u591a\uff0c\u53d1\u751f\u547d\u540d\u51b2\u7a81\u7684\u6982\u7387\u4e5f\u968f\u4e4b\u589e\u957f\u3002\u597d\u6d88\u606f\u662f\uff0cC++ \u7a0b\u5e8f\u63d0\u4f9b\u4e86\u5f88\u591a\u7684\u673a\u5236\u6765\u907f\u514d\u547d\u540d\u51b2\u7a81\u3002\u5c40\u90e8 \u4f5c\u7528\u57df(scope) \u53ef\u4ee5\u907f\u514d\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8\u7684\u5c40\u90e8\u53d8\u91cf\u4e0e\u5176\u4ed6\u53d8\u91cf\u53d1\u751f\u547d\u540d\u51b2\u7a81\uff0c\u8fd9\u662f\u5176\u4e2d\u4e00\u79cd\u673a\u5236\u3002\u4f46\u662f\uff0c\u8be5\u673a\u5236\u4e0d\u80fd\u907f\u514d\u51fd\u6570\u540d\u7684\u51b2\u7a81\u3002\u90a3\u4e48\uff0c\u5e94\u8be5\u5982\u4f55\u907f\u514d\u51fd\u6570\u540d\u51b2\u7a81\u5462\uff1f \u547d\u540d\u7a7a\u95f4\u662f\u4ec0\u4e48? \u56de\u60f3\u4e00\u4e0b\u521a\u624d\u6709\u4e2a\u5730\u5740\u7684\u4f8b\u5b50\uff0c\u4e24\u6761\u540c\u540d\u7684\u8857\u4e4b\u6240\u4ee5\u4f1a\u5e26\u6765\u9ebb\u70e6\uff0c\u53ea\u56e0\u4e3a\u5b83\u4eec\u662f\u540c\u4e00\u4e2a\u57ce\u5e02\u4e2d\u7684\u4e24\u6761\u8857\u3002\u5982\u679c\u4f60\u60f3\u8981\u628a\u4fe1\u5bc4\u5230\u897f\u7ea2\u5e02\u7684\u91d1\u878d\u8857 209 \u548c\u8292\u679c\u5e02\u7684\u91d1\u878d\u8857 417\uff0c\u90a3\u4e48\u6839\u672c\u4e0d\u4f1a\u6709\u4efb\u4f55\u95ee\u9898\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u57fa\u4e8e\u57ce\u5e02\u521b\u5efa\u7684\u5206\u7ec4\u53ef\u4ee5\u907f\u514d\u540c\u540d\u8857\u9053\u4ea7\u751f\u7684\u6b67\u4e49\u3002\u6240\u8c13\u7684 \u547d\u540d\u7a7a\u95f4(namespace) \u5c31\u50cf\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u7684\u57ce\u5e02\u4e00\u6837\u3002 \u4e3a\u4e86\u907f\u514d\u6b67\u4e49\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u5728\u547d\u540d\u7a7a\u95f4\u4e2d\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf\u540d\u3002\u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u4e3a\u5176\u4e2d\u5b9a\u4e49\u7684\u540d\u79f0\u63d0\u4f9b\u4e00\u4e2a\u4f5c\u7528\u57df\u8303\u56f4\uff08\u79f0\u4e3a\u547d\u540d\u7a7a\u95f4\u4f5c\u7528\u57df\uff09\uff0c\u7b80\u5355\u6765\u8bf4\uff0c\u5728\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u540d\u79f0\uff0c\u4e0d\u4f1a\u548c\u5176\u4ed6\u4f5c\u7528\u57df\u4e2d\u540c\u540d\u7684\u6807\u8bc6\u7b26\u4ea7\u751f\u51b2\u7a81\u3002 \u5173\u952e\u4fe1\u606f \u5728\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u540d\u79f0\uff0c\u4e0d\u4f1a\u548c\u5176\u4ed6\u4f5c\u7528\u57df\u4e2d\u540c\u540d\u7684\u6807\u8bc6\u7b26\u4ea7\u751f\u51b2\u7a81\u3002 \u5728\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u6240\u6709\u7684\u540d\u79f0\u90fd\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u5426\u5219\u7167\u6837\u4f1a\u4ea7\u751f\u51b2\u7a81\u3002 \u547d\u540d\u7a7a\u95f4\u901a\u5e38\u7528\u4e8e\u5728\u5927\u578b\u9879\u76ee\u4e2d\u5bf9\u53d8\u91cf\u540d\u8fdb\u884c\u5206\u7ec4\uff0c\u4ee5\u6b64\u6765\u786e\u4fdd \u6807\u8bc6\u7b26(identifier) \u4e0d\u4f1a\u5728\u4e0d\u7ecf\u610f\u95f4\u4e0e\u5176\u4ed6\u6807\u8bc6\u7b26\u53d1\u751f\u51b2\u7a81\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5c06\u6240\u6709\u7684\u6570\u5b66\u51fd\u6570\u90fd\u5b9a\u4e49\u5728 math \u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u90a3\u4e48\u5b83\u4eec\u5c31\u4e0d\u4f1a\u548c\u5176\u4ed6\u7a7a\u95f4\u4e2d\u76f8\u540c\u540d\u79f0\u7684\u51fd\u6570\u53d1\u751f\u51b2\u7a81\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u5982\u4f55\u521b\u5efa\u547d\u540d\u7a7a\u95f4\u3002 \u5168\u5c40\u547d\u540d\u7a7a\u95f4 \u5728 C++ \u4e2d\uff0c\u4efb\u4f55\u6ca1\u6709\u88ab\u5b9a\u4e49\u5728\u7c7b\u3001\u51fd\u6570\u6216\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\uff0c\u90fd\u5c5e\u4e8e\u4e00\u4e2a\u9690\u542b\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u79f0\u4e3a\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff08\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a\u5168\u5c40 \u4f5c\u7528\u57df(scope) \uff09\u3002 \u5728\u6587\u7ae0\u5f00\u59cb\u7684\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570 main() \u548c\u4e24\u4e2a\u7248\u672c\u7684 myFcn() \u90fd\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u7684\u547d\u540d\u51b2\u7a81\u4e5f\u662f\u56e0\u4e3a\u4e24\u4e2a myFcn() \u90fd\u5904\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff0c\u4e5f\u5c31\u8fdd\u53cd\u4e86\u547d\u540d\u7a7a\u95f4\u4e2d\u6807\u8bc6\u7b26\u5fc5\u987b\u552f\u4e00\u7684\u89c4\u5219\u3002 \u53ea\u6709\u58f0\u660e\u548c\u5b9a\u4e49\u8bed\u53e5\u53ef\u4ee5\u51fa\u73b0\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d \u3002\u8fd9\u4e5f\u5c31\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u53d8\u91cf\uff0c\u5c3d\u7ba1\u901a\u5e38\u6211\u4eec\u5e94\u8be5\u907f\u514d\u8fd9\u4e48\u505a(\u6211\u4eec\u4f1a\u5728 6.4 - \u5168\u5c40\u53d8\u91cf \u4e00\u8282\u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u5168\u5c40\u53d8\u91cf)\u3002\u4e5f\u540c\u6837\u4e5f\u610f\u5473\u7740\uff0c \u5176\u4ed6\u7c7b\u578b\u7684\u8bed\u53e5(\u4f8b\u5982\u8868\u8fbe\u5f0f)\u4e0d\u80fd\u4f4d\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4(\u5168\u5c40\u53d8\u91cf\u7684\u521d\u59cb\u5316\u662f\u4e00\u4e2a\u4f8b\u5916)\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // handled by preprocessor // \u4ee5\u4e0b\u6240\u6709\u8bed\u53e5\u90fd\u4f4d\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4 void foo (); // \u53ef\u4ee5\u8fd9\u4e48\u505a: \u53ef\u4ee5\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u8fdb\u884c\u51fd\u6570\u7684\u524d\u5411\u58f0\u660e int x ; // \u53ef\u4ee5\u7f16\u8bd1\u4f46\u5f3a\u70c8\u5efa\u8bae\u4e0d\u8981\u8fd9\u4e48\u505a\uff1a\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\u88ab\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d int y { 5 }; // \u53ef\u4ee5\u7f16\u8bd1\u4f46\u4e0d\u63a8\u8350\u8fd9\u4e48\u505a:\u53d8\u91cf\u88ab\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u4e14\u4f7f\u7528\u521d\u59cb\u5316\u503c\u521d\u59cb\u5316 x = 5 ; // \u7f16\u8bd1\u9519\u8bef\uff1a\u53ef\u6267\u884c\u7684\u8bed\u53e5\u4e0d\u80fd\u653e\u7f6e\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d int main () // \u53ef\u4ee5\u8fd9\u4e48\u505a\uff1a\u51fd\u6570\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d { return 0 ; } void goo (); // \u53ef\u4ee5\u8fd9\u4e48\u505a: \u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u5bf9\u5176\u4ed6\u51fd\u6570\u8fdb\u884c\u524d\u5411\u58f0\u660e std \u547d\u540d\u7a7a\u95f4 C++ \u5728\u6700\u521d\u8bbe\u8ba1\u65f6\uff0c\u6240\u6709 C++ \u6807\u51c6\u5e93\u4e2d\u7684\u6807\u8bc6\u7b26\uff08\u5305\u62ec std::cin \u548c std::cout \uff09\u90fd\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u800c\u65e0\u9700\u6dfb\u52a0 std:: \u524d\u7f00\uff08\u90fd\u5c5e\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff09\u3002\u4e0d\u8fc7\uff0c\u8fd9\u6837\u4e5f\u610f\u5473\u7740\u6807\u51c6\u5e93\u4e2d\u7684\u4efb\u4f55\u6807\u8bc6\u7b26\u90fd\u53ef\u80fd\u4f1a\u548c\u5176\u4ed6\u7528\u6237\u6240\u4f7f\u7528\u7684\u6807\u8bc6\u7b26\uff08\u540c\u6837\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff09\u53d1\u751f\u51b2\u7a81\u3002\u672c\u6765\u53ef\u4ee5\u5de5\u4f5c\u7684\u4ee3\u7801\uff0c\u53ef\u80fd\u4f1a\u56e0\u4e3a #include \u4e86\u4e00\u4e2a\u6807\u51c6\u5e93\u4e2d\u7684\u65b0\u6587\u4ef6\u800c\u51fa\u73b0\u547d\u540d\u51b2\u7a81\u3002\u66f4\u574f\u7684\u662f\uff0c\u57fa\u4e8e\u8001\u7248\u672cC++\u53ef\u4ee5\u4f7f\u7528\u7684\u4ee3\u7801\uff0c\u5728\u65b0\u7248\u672c\u4e2d\u53ef\u80fd\u65e0\u6cd5\u4f7f\u7528\uff0c\u56e0\u4e3a\u65b0\u6dfb\u52a0\u5230\u6807\u51c6\u5e93\u4e2d\u7684\u6807\u8bc6\u7b26\u53ef\u80fd\u4f1a\u4e0e\u5df2\u6709\u7684\u4ee3\u7801\u53d1\u751f\u547d\u540d\u51b2\u7a81\u3002\u56e0\u6b64\uff0cC++ \u5c06\u6240\u6709\u6807\u51c6\u5e93\u4e2d\u7684\u6807\u8bc6\u7b26\u90fd\u79fb\u52a8\u5230\u4e86 std \u547d\u540d\u7a7a\u95f4\u4e2d\uff08std\u8868\u793astandard\uff0c\u5373\u6807\u51c6\uff09\u3002 \u5b9e\u9645\u4e0a\uff0c std:: cout \u7684\u540d\u5b57\u5e76\u4e0d\u662f std:: cout \u800c\u662f cout \u3002 std \u53ea\u662f\u5b83\u6240\u5904\u547d\u540d\u7a7a\u95f4\u7684 \u9650\u5b9a\u7b26(qualifier) \u7f62\u4e86\u3002\u56e0\u4e3a\uff0c cout \u5b9a\u4e49\u5728 std \u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u56e0\u6b64\u5b83\u4e0d\u4f1a\u548c\u5176\u4ed6\u4efb\u4f55\u88ab\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u7684 cout \u53d1\u751f\u51b2\u7a81\u3002 \u540c\u6837\u7684\uff0c\u5f53\u9700\u8981\u4f7f\u7528\u5b9a\u4e49\u5728\u67d0\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u65f6\uff08\u4f8b\u5982 std::cout \uff09\uff0c\u4f60\u5fc5\u987b\u544a\u8bc9\u7f16\u8bd1\u5668\u5230\u54ea\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u53bb\u67e5\u627e\u5b83\uff08\u4f8b\u5982 std \uff09\u3002 \u5173\u952e\u4fe1\u606f \u5f53\u9700\u8981\u4f7f\u7528\u5b9a\u4e49\u5728\u67d0\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u65f6\uff08\u4f8b\u5982 std::cout \uff09\uff0c\u4f60\u5fc5\u987b\u544a\u8bc9\u7f16\u8bd1\u5668\u8be5\u6807\u8bc6\u7b26\u88ab\u5b9a\u4e49\u5728\u547d\u540d\u7a7a\u95f4\u4e2d\u3002 \u6709\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5b8c\u6210\u8be5\u64cd\u4f5c\u3002 \u547d\u540d\u7a7a\u95f4\u4fee\u9970\u7b26 std:: \u6700\u76f4\u63a5\u7684\u529e\u6cd5\u5c31\u662f\u901a\u8fc7 std:: \u524d\u7f00\u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u6211\u4eec\u5e0c\u671b\u4f7f\u7528 std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684 cout \uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << \"Hello world!\" ; // when we say cout, we mean the cout defined in the std namespace return 0 ; } :: \u7b26\u53f7\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u8fd0\u7b97\u7b26\uff0c\u79f0\u4e3a \u7a7a\u95f4\u89e3\u6790\u8fd0\u7b97\u7b26(scope resolution operator) \u3002\u8be5\u8fd0\u7b97\u7b26\u5de6\u4fa7\u662f\u547d\u540d\u7a7a\u95f4\u7684\u540d\u5b57\uff0c\u53f3\u4fa7\u5219\u662f\u8be5\u7a7a\u95f4\u4e2d\u5305\u542b\u7684\u6807\u8bc6\u7b26\u3002\u5982\u679c\u5de6\u4fa7\u672a\u6307\u5b9a\u4efb\u4f55\u547d\u540d\u7a7a\u95f4\uff0c\u5219\u9ed8\u8ba4\u4e3a\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u3002 \u56e0\u6b64\uff0c std:: cout \u8868\u793a cout \u88ab\u5b9a\u4e49\u5728\u547d\u540d\u7a7a\u95f4 std \u4e2d\u3002 \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u7528 cout \u662f\u6700\u5b89\u5168\u7684\uff0c\u56e0\u4e3a\u4e0d\u5b58\u5728\u4efb\u4f55\u6b67\u4e49\uff08 cout \u662f std \u4e2d\u552f\u4e00\u7684\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u524d\u7f00\u6765\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u3002 using namespace std \u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u8fd9\u4e48\u505a \u53e6\u5916\u4e00\u79cd\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u4e2d\u6807\u8bc6\u7b26\u7684\u65b9\u5f0f\u662f\u4f7f\u7528 using \u6307\u4ee4\u8bed\u53e5\u3002\u4ee5\u4e0b\u4f8b\u5b50\u57fa\u4e8e\u8be5\u65b9\u6cd5\u4fee\u6539\u4e86\u539f \u201cHello world\u201d \u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 #include using namespace std ; // \u8be5using\u6307\u4ee4\u8bed\u53e5\u53ef\u4ee5\u4f7f\u6211\u4eec\u8bbf\u95ee\u6807\u8bc6\u7b26\u65f6\u65e0\u9700\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u524d\u7f00 int main () { cout << \"Hello world!\" ; return 0 ; } using \u6307\u4ee4\u8bed\u53e5\u53ef\u4ee5\u4f7f\u6211\u4eec\u8bbf\u95ee\u6807\u8bc6\u7b26\u65f6\u65e0\u9700\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u524d\u7f00\u3002\u56e0\u6b64\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53\u7f16\u8bd1\u5668\u9700\u8981\u786e\u5b9a cout \u7684\u5b9a\u4e49\u65f6\uff0c\u5b83\u4f1a\u5339\u914d\u5230 std:: cout \uff0c\u5728\u4f7f\u7528\u4e86 using \u6307\u4ee4\u540e\uff0c\u5c31\u53ef\u4ee5\u4ee5 cout \u8fd9\u79cd\u5f62\u5f0f\u6765\u4f7f\u7528\u8be5\u51fd\u6570\u4e86\u3002 \u5f88\u591a\u6587\u7ae0\u3001\u6559\u7a0b\u3001\u751a\u81f3\u662f\u4e00\u4e9b\u7f16\u8bd1\u5668\u4f1a\u5efa\u8bae\u5728\u7a0b\u5e8f\u7684\u5f00\u5934\u4f7f\u7528 using \u6307\u4ee4\u8bed\u53e5\u3002\u5176\u5b9e\uff0c\u8fd9\u5e76\u4e0d\u662f\u6700\u4f73\u5b9e\u8df5\uff0c\u800c\u4e14\u6781\u4e3a\u4e0d\u63a8\u8350\u4f7f\u7528\u3002 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // \u5bfc\u5165 `std::cout` \u7684\u5b9a\u4e49\u3002 using namespace std ; // \u4f7f\u5f97 std::cout \u80fd\u591f\u4ee5 \"cout\" \u65b9\u5f0f\u4f7f\u7528 int cout () // \u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u6211\u4eec\u7684cout\u51fd\u6570 { return 5 ; } int main () { cout << \"Hello, world!\" ; // \u7f16\u8bd1\u62a5\u9519\uff0c\u8fd9\u91cc\u4f7f\u7528\u7684cout\u662f\u6211\u4eec\u5b9a\u4e49\u7684\uff0c\u8fd8\u662fstd\u5b9a\u4e49\u7684\uff1f return 0 ; } \u4e0a\u9762\u7684\u4ee3\u7801\u662f\u4e0d\u80fd\u591f\u7f16\u8bd1\u7684\uff0c\u56e0\u4e3a\u6b64\u65f6\u7f16\u8bd1\u5668\u65e0\u6cd5\u533a\u5206\u6211\u4eec\u60f3\u8981\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684 cout \u8fd8\u662f std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684 cout \u3002 \u5f53\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u7528 using \u6307\u4ee4\u65f6\uff0c\u6211\u4eec\u5b9a\u4e49\u7684\u4efb\u4f55\u540d\u79f0\u90fd\u53ef\u80fd\u548c std \u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u4efb\u4f55\u540d\u79f0\u53d1\u751f\u51b2\u7a81\u3002\u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u4e5f\u8bb8\u6b64\u65f6\u5e76\u6ca1\u6709\u51b2\u7a81\uff0c\u4f46\u662f\u5728\u65b0\u7248\u672c\u7684C++\u4e2d\uff0c\u7531\u4e8e std \u4e2d\u65b0\u52a0\u5165\u4e86\u5176\u4ed6\u6807\u8bc6\u7b26\uff0c\u5219\u53ef\u80fd\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\u7684\u53d1\u751f\u3002\u8fd9\u4e5f\u662f\u5c06\u6807\u51c6\u5e93\u4e2d\u6240\u6709\u6807\u8bc6\u7b26\u90fd\u79fb\u52a8\u5230\u4e86 std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u521d\u8877\u3002 \u6ce8\u610f \u5e94\u8be5\u5728\u7a0b\u5e8f\u6216\u5934\u6587\u4ef6\u4e2d\u907f\u514d\u4f7f\u7528 using \u6307\u4ee4 (\u4f8b\u5982 using namespace std; )\uff0c\u8fd9\u4e48\u505a\u5c31\u8fdd\u80cc\u4e86\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u7684\u521d\u8877\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 \u4e2d\u4ecb\u7ecd using \u8bed\u53e5\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u3002","title":"2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4"},{"location":"2-9-Naming-collisions-and-an-introduction-to-namespaces/#_1","text":"a.cpp: 1 2 3 4 5 6 #include void myFcn ( int x ) { std :: cout << x ; } COPY main.cpp: 1 2 3 4 5 6 7 8 9 10 11 #include void myFcn ( int x ) { std :: cout << 2 * x ; } int main () { return 0 ; } \u5f53\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u6b64\u7a0b\u5e8f\u65f6\uff0c\u5b83\u4f1a\u72ec\u7acb\u5730\u7f16\u8bd1 a.cpp \u548c main.cpp \u4e24\u4e2a\u6587\u4ef6\uff0c\u5e76\u4e14\u4e0d\u4f1a\u6709\u4efb\u4f55\u95ee\u9898\u3002 \u4e0d\u8fc7\uff0c\u5f53\u94fe\u63a5\u5668\u5f00\u59cb\u5de5\u4f5c\u65f6\uff0c\u5b83\u4f1a\u628a a.cpp \u548c main.cpp \u4e2d\u7684\u5b9a\u4e49\u5173\u8054\u8d77\u6765\uff0c\u7136\u540e\u5b83\u5c31\u4f1a\u53d1\u73b0 myFcn \u51fd\u6570\u7684\u5b9a\u4e49\u51b2\u7a81\u4e86\u3002\u968f\u540e\u94fe\u63a5\u5668\u4fbf\u4f1a\u56e0\u4e3a\u62a5\u9519\u800c\u505c\u6b62\u5de5\u4f5c\u3002\u6ce8\u610f\uff0c\u5373\u4f7f myFcn \u59cb\u7ec8\u6ca1\u6709\u88ab\u8c03\u7528\uff0c\u9519\u8bef\u4ecd\u7136\u4f1a\u53d1\u751f\uff01 \u5927\u591a\u6570\u547d\u540d\u51b2\u7a81\u7531\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\u5f15\u8d77\uff1a \u4e00\u4e2a\u51fd\u6570\uff08\u6216\u5c40\u90e8\u53d8\u91cf\uff09\u7684\u4e24\u4e2a\uff08\u6216\u591a\u4e2a\uff09\u5b9a\u4e49\u88ab\u5b9a\u4e49\u5728\u4e86\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\uff0c\u4f46\u88ab\u7f16\u8bd1\u5230\u4e86\u540c\u6837\u7684\u7a0b\u5e8f\u4e2d\u3002\u8fd9\u6837\u4e00\u6765\u5c31\u4f1a\u5bfc\u81f4\u94fe\u63a5\u5668\u62a5\u9519\uff0c\u5982\u4e0a\u9762\u63cf\u8ff0\u7684\u90a3\u6837\uff1b \u4e00\u4e2a\u51fd\u6570\uff08\u6216\u5c40\u90e8\u53d8\u91cf\uff09\u7684\u4e24\u4e2a\uff08\u6216\u591a\u4e2a\uff09\u5b9a\u4e49\u88ab\u5f15\u5165\u4e86\u540c\u6837\u7684\u51fd\u6570\uff08\u901a\u5e38\u662f\u901a\u8fc7 #include \u5b9e\u73b0\u7684\uff09\u3002\u8fd9\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\u3002 \u968f\u7740\u7a0b\u5e8f\u89c4\u6a21\u7684\u589e\u957f\uff0c\u7a0b\u5e8f\u4e2d\u7684\u6807\u8bc6\u7b26\u4e5f\u8d8a\u6765\u8d8a\u591a\uff0c\u53d1\u751f\u547d\u540d\u51b2\u7a81\u7684\u6982\u7387\u4e5f\u968f\u4e4b\u589e\u957f\u3002\u597d\u6d88\u606f\u662f\uff0cC++ \u7a0b\u5e8f\u63d0\u4f9b\u4e86\u5f88\u591a\u7684\u673a\u5236\u6765\u907f\u514d\u547d\u540d\u51b2\u7a81\u3002\u5c40\u90e8 \u4f5c\u7528\u57df(scope) \u53ef\u4ee5\u907f\u514d\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8\u7684\u5c40\u90e8\u53d8\u91cf\u4e0e\u5176\u4ed6\u53d8\u91cf\u53d1\u751f\u547d\u540d\u51b2\u7a81\uff0c\u8fd9\u662f\u5176\u4e2d\u4e00\u79cd\u673a\u5236\u3002\u4f46\u662f\uff0c\u8be5\u673a\u5236\u4e0d\u80fd\u907f\u514d\u51fd\u6570\u540d\u7684\u51b2\u7a81\u3002\u90a3\u4e48\uff0c\u5e94\u8be5\u5982\u4f55\u907f\u514d\u51fd\u6570\u540d\u51b2\u7a81\u5462\uff1f","title":"\u547d\u540d\u51b2\u7a81\u5b9e\u4f8b"},{"location":"2-9-Naming-collisions-and-an-introduction-to-namespaces/#_2","text":"\u56de\u60f3\u4e00\u4e0b\u521a\u624d\u6709\u4e2a\u5730\u5740\u7684\u4f8b\u5b50\uff0c\u4e24\u6761\u540c\u540d\u7684\u8857\u4e4b\u6240\u4ee5\u4f1a\u5e26\u6765\u9ebb\u70e6\uff0c\u53ea\u56e0\u4e3a\u5b83\u4eec\u662f\u540c\u4e00\u4e2a\u57ce\u5e02\u4e2d\u7684\u4e24\u6761\u8857\u3002\u5982\u679c\u4f60\u60f3\u8981\u628a\u4fe1\u5bc4\u5230\u897f\u7ea2\u5e02\u7684\u91d1\u878d\u8857 209 \u548c\u8292\u679c\u5e02\u7684\u91d1\u878d\u8857 417\uff0c\u90a3\u4e48\u6839\u672c\u4e0d\u4f1a\u6709\u4efb\u4f55\u95ee\u9898\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u57fa\u4e8e\u57ce\u5e02\u521b\u5efa\u7684\u5206\u7ec4\u53ef\u4ee5\u907f\u514d\u540c\u540d\u8857\u9053\u4ea7\u751f\u7684\u6b67\u4e49\u3002\u6240\u8c13\u7684 \u547d\u540d\u7a7a\u95f4(namespace) \u5c31\u50cf\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u7684\u57ce\u5e02\u4e00\u6837\u3002 \u4e3a\u4e86\u907f\u514d\u6b67\u4e49\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u5728\u547d\u540d\u7a7a\u95f4\u4e2d\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf\u540d\u3002\u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u4e3a\u5176\u4e2d\u5b9a\u4e49\u7684\u540d\u79f0\u63d0\u4f9b\u4e00\u4e2a\u4f5c\u7528\u57df\u8303\u56f4\uff08\u79f0\u4e3a\u547d\u540d\u7a7a\u95f4\u4f5c\u7528\u57df\uff09\uff0c\u7b80\u5355\u6765\u8bf4\uff0c\u5728\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u540d\u79f0\uff0c\u4e0d\u4f1a\u548c\u5176\u4ed6\u4f5c\u7528\u57df\u4e2d\u540c\u540d\u7684\u6807\u8bc6\u7b26\u4ea7\u751f\u51b2\u7a81\u3002 \u5173\u952e\u4fe1\u606f \u5728\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u540d\u79f0\uff0c\u4e0d\u4f1a\u548c\u5176\u4ed6\u4f5c\u7528\u57df\u4e2d\u540c\u540d\u7684\u6807\u8bc6\u7b26\u4ea7\u751f\u51b2\u7a81\u3002 \u5728\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u6240\u6709\u7684\u540d\u79f0\u90fd\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u5426\u5219\u7167\u6837\u4f1a\u4ea7\u751f\u51b2\u7a81\u3002 \u547d\u540d\u7a7a\u95f4\u901a\u5e38\u7528\u4e8e\u5728\u5927\u578b\u9879\u76ee\u4e2d\u5bf9\u53d8\u91cf\u540d\u8fdb\u884c\u5206\u7ec4\uff0c\u4ee5\u6b64\u6765\u786e\u4fdd \u6807\u8bc6\u7b26(identifier) \u4e0d\u4f1a\u5728\u4e0d\u7ecf\u610f\u95f4\u4e0e\u5176\u4ed6\u6807\u8bc6\u7b26\u53d1\u751f\u51b2\u7a81\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5c06\u6240\u6709\u7684\u6570\u5b66\u51fd\u6570\u90fd\u5b9a\u4e49\u5728 math \u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u90a3\u4e48\u5b83\u4eec\u5c31\u4e0d\u4f1a\u548c\u5176\u4ed6\u7a7a\u95f4\u4e2d\u76f8\u540c\u540d\u79f0\u7684\u51fd\u6570\u53d1\u751f\u51b2\u7a81\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u5982\u4f55\u521b\u5efa\u547d\u540d\u7a7a\u95f4\u3002","title":"\u547d\u540d\u7a7a\u95f4\u662f\u4ec0\u4e48?"},{"location":"2-9-Naming-collisions-and-an-introduction-to-namespaces/#_3","text":"\u5728 C++ \u4e2d\uff0c\u4efb\u4f55\u6ca1\u6709\u88ab\u5b9a\u4e49\u5728\u7c7b\u3001\u51fd\u6570\u6216\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\uff0c\u90fd\u5c5e\u4e8e\u4e00\u4e2a\u9690\u542b\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u79f0\u4e3a\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff08\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a\u5168\u5c40 \u4f5c\u7528\u57df(scope) \uff09\u3002 \u5728\u6587\u7ae0\u5f00\u59cb\u7684\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570 main() \u548c\u4e24\u4e2a\u7248\u672c\u7684 myFcn() \u90fd\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u7684\u547d\u540d\u51b2\u7a81\u4e5f\u662f\u56e0\u4e3a\u4e24\u4e2a myFcn() \u90fd\u5904\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff0c\u4e5f\u5c31\u8fdd\u53cd\u4e86\u547d\u540d\u7a7a\u95f4\u4e2d\u6807\u8bc6\u7b26\u5fc5\u987b\u552f\u4e00\u7684\u89c4\u5219\u3002 \u53ea\u6709\u58f0\u660e\u548c\u5b9a\u4e49\u8bed\u53e5\u53ef\u4ee5\u51fa\u73b0\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d \u3002\u8fd9\u4e5f\u5c31\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u53d8\u91cf\uff0c\u5c3d\u7ba1\u901a\u5e38\u6211\u4eec\u5e94\u8be5\u907f\u514d\u8fd9\u4e48\u505a(\u6211\u4eec\u4f1a\u5728 6.4 - \u5168\u5c40\u53d8\u91cf \u4e00\u8282\u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u5168\u5c40\u53d8\u91cf)\u3002\u4e5f\u540c\u6837\u4e5f\u610f\u5473\u7740\uff0c \u5176\u4ed6\u7c7b\u578b\u7684\u8bed\u53e5(\u4f8b\u5982\u8868\u8fbe\u5f0f)\u4e0d\u80fd\u4f4d\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4(\u5168\u5c40\u53d8\u91cf\u7684\u521d\u59cb\u5316\u662f\u4e00\u4e2a\u4f8b\u5916)\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // handled by preprocessor // \u4ee5\u4e0b\u6240\u6709\u8bed\u53e5\u90fd\u4f4d\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4 void foo (); // \u53ef\u4ee5\u8fd9\u4e48\u505a: \u53ef\u4ee5\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u8fdb\u884c\u51fd\u6570\u7684\u524d\u5411\u58f0\u660e int x ; // \u53ef\u4ee5\u7f16\u8bd1\u4f46\u5f3a\u70c8\u5efa\u8bae\u4e0d\u8981\u8fd9\u4e48\u505a\uff1a\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\u88ab\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d int y { 5 }; // \u53ef\u4ee5\u7f16\u8bd1\u4f46\u4e0d\u63a8\u8350\u8fd9\u4e48\u505a:\u53d8\u91cf\u88ab\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u4e14\u4f7f\u7528\u521d\u59cb\u5316\u503c\u521d\u59cb\u5316 x = 5 ; // \u7f16\u8bd1\u9519\u8bef\uff1a\u53ef\u6267\u884c\u7684\u8bed\u53e5\u4e0d\u80fd\u653e\u7f6e\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d int main () // \u53ef\u4ee5\u8fd9\u4e48\u505a\uff1a\u51fd\u6570\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d { return 0 ; } void goo (); // \u53ef\u4ee5\u8fd9\u4e48\u505a: \u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u5bf9\u5176\u4ed6\u51fd\u6570\u8fdb\u884c\u524d\u5411\u58f0\u660e","title":"\u5168\u5c40\u547d\u540d\u7a7a\u95f4"},{"location":"2-9-Naming-collisions-and-an-introduction-to-namespaces/#std","text":"C++ \u5728\u6700\u521d\u8bbe\u8ba1\u65f6\uff0c\u6240\u6709 C++ \u6807\u51c6\u5e93\u4e2d\u7684\u6807\u8bc6\u7b26\uff08\u5305\u62ec std::cin \u548c std::cout \uff09\u90fd\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u800c\u65e0\u9700\u6dfb\u52a0 std:: \u524d\u7f00\uff08\u90fd\u5c5e\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff09\u3002\u4e0d\u8fc7\uff0c\u8fd9\u6837\u4e5f\u610f\u5473\u7740\u6807\u51c6\u5e93\u4e2d\u7684\u4efb\u4f55\u6807\u8bc6\u7b26\u90fd\u53ef\u80fd\u4f1a\u548c\u5176\u4ed6\u7528\u6237\u6240\u4f7f\u7528\u7684\u6807\u8bc6\u7b26\uff08\u540c\u6837\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff09\u53d1\u751f\u51b2\u7a81\u3002\u672c\u6765\u53ef\u4ee5\u5de5\u4f5c\u7684\u4ee3\u7801\uff0c\u53ef\u80fd\u4f1a\u56e0\u4e3a #include \u4e86\u4e00\u4e2a\u6807\u51c6\u5e93\u4e2d\u7684\u65b0\u6587\u4ef6\u800c\u51fa\u73b0\u547d\u540d\u51b2\u7a81\u3002\u66f4\u574f\u7684\u662f\uff0c\u57fa\u4e8e\u8001\u7248\u672cC++\u53ef\u4ee5\u4f7f\u7528\u7684\u4ee3\u7801\uff0c\u5728\u65b0\u7248\u672c\u4e2d\u53ef\u80fd\u65e0\u6cd5\u4f7f\u7528\uff0c\u56e0\u4e3a\u65b0\u6dfb\u52a0\u5230\u6807\u51c6\u5e93\u4e2d\u7684\u6807\u8bc6\u7b26\u53ef\u80fd\u4f1a\u4e0e\u5df2\u6709\u7684\u4ee3\u7801\u53d1\u751f\u547d\u540d\u51b2\u7a81\u3002\u56e0\u6b64\uff0cC++ \u5c06\u6240\u6709\u6807\u51c6\u5e93\u4e2d\u7684\u6807\u8bc6\u7b26\u90fd\u79fb\u52a8\u5230\u4e86 std \u547d\u540d\u7a7a\u95f4\u4e2d\uff08std\u8868\u793astandard\uff0c\u5373\u6807\u51c6\uff09\u3002 \u5b9e\u9645\u4e0a\uff0c std:: cout \u7684\u540d\u5b57\u5e76\u4e0d\u662f std:: cout \u800c\u662f cout \u3002 std \u53ea\u662f\u5b83\u6240\u5904\u547d\u540d\u7a7a\u95f4\u7684 \u9650\u5b9a\u7b26(qualifier) \u7f62\u4e86\u3002\u56e0\u4e3a\uff0c cout \u5b9a\u4e49\u5728 std \u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u56e0\u6b64\u5b83\u4e0d\u4f1a\u548c\u5176\u4ed6\u4efb\u4f55\u88ab\u5b9a\u4e49\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u7684 cout \u53d1\u751f\u51b2\u7a81\u3002 \u540c\u6837\u7684\uff0c\u5f53\u9700\u8981\u4f7f\u7528\u5b9a\u4e49\u5728\u67d0\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u65f6\uff08\u4f8b\u5982 std::cout \uff09\uff0c\u4f60\u5fc5\u987b\u544a\u8bc9\u7f16\u8bd1\u5668\u5230\u54ea\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u53bb\u67e5\u627e\u5b83\uff08\u4f8b\u5982 std \uff09\u3002 \u5173\u952e\u4fe1\u606f \u5f53\u9700\u8981\u4f7f\u7528\u5b9a\u4e49\u5728\u67d0\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u65f6\uff08\u4f8b\u5982 std::cout \uff09\uff0c\u4f60\u5fc5\u987b\u544a\u8bc9\u7f16\u8bd1\u5668\u8be5\u6807\u8bc6\u7b26\u88ab\u5b9a\u4e49\u5728\u547d\u540d\u7a7a\u95f4\u4e2d\u3002 \u6709\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5b8c\u6210\u8be5\u64cd\u4f5c\u3002","title":"std \u547d\u540d\u7a7a\u95f4"},{"location":"2-9-Naming-collisions-and-an-introduction-to-namespaces/#std_1","text":"\u6700\u76f4\u63a5\u7684\u529e\u6cd5\u5c31\u662f\u901a\u8fc7 std:: \u524d\u7f00\u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u6211\u4eec\u5e0c\u671b\u4f7f\u7528 std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684 cout \uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << \"Hello world!\" ; // when we say cout, we mean the cout defined in the std namespace return 0 ; } :: \u7b26\u53f7\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u8fd0\u7b97\u7b26\uff0c\u79f0\u4e3a \u7a7a\u95f4\u89e3\u6790\u8fd0\u7b97\u7b26(scope resolution operator) \u3002\u8be5\u8fd0\u7b97\u7b26\u5de6\u4fa7\u662f\u547d\u540d\u7a7a\u95f4\u7684\u540d\u5b57\uff0c\u53f3\u4fa7\u5219\u662f\u8be5\u7a7a\u95f4\u4e2d\u5305\u542b\u7684\u6807\u8bc6\u7b26\u3002\u5982\u679c\u5de6\u4fa7\u672a\u6307\u5b9a\u4efb\u4f55\u547d\u540d\u7a7a\u95f4\uff0c\u5219\u9ed8\u8ba4\u4e3a\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u3002 \u56e0\u6b64\uff0c std:: cout \u8868\u793a cout \u88ab\u5b9a\u4e49\u5728\u547d\u540d\u7a7a\u95f4 std \u4e2d\u3002 \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u7528 cout \u662f\u6700\u5b89\u5168\u7684\uff0c\u56e0\u4e3a\u4e0d\u5b58\u5728\u4efb\u4f55\u6b67\u4e49\uff08 cout \u662f std \u4e2d\u552f\u4e00\u7684\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u524d\u7f00\u6765\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u3002","title":"\u547d\u540d\u7a7a\u95f4\u4fee\u9970\u7b26 std::"},{"location":"2-9-Naming-collisions-and-an-introduction-to-namespaces/#using-namespace-std","text":"\u53e6\u5916\u4e00\u79cd\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u4e2d\u6807\u8bc6\u7b26\u7684\u65b9\u5f0f\u662f\u4f7f\u7528 using \u6307\u4ee4\u8bed\u53e5\u3002\u4ee5\u4e0b\u4f8b\u5b50\u57fa\u4e8e\u8be5\u65b9\u6cd5\u4fee\u6539\u4e86\u539f \u201cHello world\u201d \u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 #include using namespace std ; // \u8be5using\u6307\u4ee4\u8bed\u53e5\u53ef\u4ee5\u4f7f\u6211\u4eec\u8bbf\u95ee\u6807\u8bc6\u7b26\u65f6\u65e0\u9700\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u524d\u7f00 int main () { cout << \"Hello world!\" ; return 0 ; } using \u6307\u4ee4\u8bed\u53e5\u53ef\u4ee5\u4f7f\u6211\u4eec\u8bbf\u95ee\u6807\u8bc6\u7b26\u65f6\u65e0\u9700\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u524d\u7f00\u3002\u56e0\u6b64\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53\u7f16\u8bd1\u5668\u9700\u8981\u786e\u5b9a cout \u7684\u5b9a\u4e49\u65f6\uff0c\u5b83\u4f1a\u5339\u914d\u5230 std:: cout \uff0c\u5728\u4f7f\u7528\u4e86 using \u6307\u4ee4\u540e\uff0c\u5c31\u53ef\u4ee5\u4ee5 cout \u8fd9\u79cd\u5f62\u5f0f\u6765\u4f7f\u7528\u8be5\u51fd\u6570\u4e86\u3002 \u5f88\u591a\u6587\u7ae0\u3001\u6559\u7a0b\u3001\u751a\u81f3\u662f\u4e00\u4e9b\u7f16\u8bd1\u5668\u4f1a\u5efa\u8bae\u5728\u7a0b\u5e8f\u7684\u5f00\u5934\u4f7f\u7528 using \u6307\u4ee4\u8bed\u53e5\u3002\u5176\u5b9e\uff0c\u8fd9\u5e76\u4e0d\u662f\u6700\u4f73\u5b9e\u8df5\uff0c\u800c\u4e14\u6781\u4e3a\u4e0d\u63a8\u8350\u4f7f\u7528\u3002 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // \u5bfc\u5165 `std::cout` \u7684\u5b9a\u4e49\u3002 using namespace std ; // \u4f7f\u5f97 std::cout \u80fd\u591f\u4ee5 \"cout\" \u65b9\u5f0f\u4f7f\u7528 int cout () // \u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u6211\u4eec\u7684cout\u51fd\u6570 { return 5 ; } int main () { cout << \"Hello, world!\" ; // \u7f16\u8bd1\u62a5\u9519\uff0c\u8fd9\u91cc\u4f7f\u7528\u7684cout\u662f\u6211\u4eec\u5b9a\u4e49\u7684\uff0c\u8fd8\u662fstd\u5b9a\u4e49\u7684\uff1f return 0 ; } \u4e0a\u9762\u7684\u4ee3\u7801\u662f\u4e0d\u80fd\u591f\u7f16\u8bd1\u7684\uff0c\u56e0\u4e3a\u6b64\u65f6\u7f16\u8bd1\u5668\u65e0\u6cd5\u533a\u5206\u6211\u4eec\u60f3\u8981\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684 cout \u8fd8\u662f std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684 cout \u3002 \u5f53\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u7528 using \u6307\u4ee4\u65f6\uff0c\u6211\u4eec\u5b9a\u4e49\u7684\u4efb\u4f55\u540d\u79f0\u90fd\u53ef\u80fd\u548c std \u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u4efb\u4f55\u540d\u79f0\u53d1\u751f\u51b2\u7a81\u3002\u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u4e5f\u8bb8\u6b64\u65f6\u5e76\u6ca1\u6709\u51b2\u7a81\uff0c\u4f46\u662f\u5728\u65b0\u7248\u672c\u7684C++\u4e2d\uff0c\u7531\u4e8e std \u4e2d\u65b0\u52a0\u5165\u4e86\u5176\u4ed6\u6807\u8bc6\u7b26\uff0c\u5219\u53ef\u80fd\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\u7684\u53d1\u751f\u3002\u8fd9\u4e5f\u662f\u5c06\u6807\u51c6\u5e93\u4e2d\u6240\u6709\u6807\u8bc6\u7b26\u90fd\u79fb\u52a8\u5230\u4e86 std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u521d\u8877\u3002 \u6ce8\u610f \u5e94\u8be5\u5728\u7a0b\u5e8f\u6216\u5934\u6587\u4ef6\u4e2d\u907f\u514d\u4f7f\u7528 using \u6307\u4ee4 (\u4f8b\u5982 using namespace std; )\uff0c\u8fd9\u4e48\u505a\u5c31\u8fdd\u80cc\u4e86\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u7684\u521d\u8877\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 \u4e2d\u4ecb\u7ecd using \u8bed\u53e5\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u3002","title":"using namespace std \u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u8fd9\u4e48\u505a"},{"location":"2-x-Chapter-2-summary-and-quiz/","tags":["summary"],"text":"\u7ae0\u8282\u56de\u987e \u51fd\u6570 \u662f\u4e00\u7cfb\u5217\u53ef\u4ee5\u590d\u7528\u7684\u8bed\u53e5\uff0c\u7528\u6765\u5b8c\u6210\u67d0\u4e2a\u7279\u5b9a\u7684\u4efb\u52a1\u3002\u7528\u6237\u81ea\u5df1\u7f16\u5199\u7684\u51fd\u6570\u79f0\u4e3a\u81ea\u5b9a\u4e49\u51fd\u6570\u3002 \u51fd\u6570\u8c03\u7528 \u662f\u4e00\u79cd\u8868\u8fbe\u5f0f\uff0c\u7528\u4e8e\u544a\u8bc9CPU\u53bb\u6267\u884c\u4e00\u4e2a\u51fd\u6570\u3002\u6267\u884c\u51fd\u6570\u8c03\u7528\u7684\u51fd\u6570\uff0c\u79f0\u4e3a \u4e3b\u8c03\u51fd\u6570 \uff0c\u88ab\u8c03\u7528\u7684\u51fd\u6570\u79f0\u4e3a \u88ab\u8c03\u51fd\u6570 \u3002\u5728\u8c03\u7528\u51fd\u6570\u7684\u65f6\u5019\uff0c\u4e0d\u8981\u5fd8\u8bb0\u51fd\u6570\u540d\u540e\u9762\u7684\u62ec\u53f7\u3002 \u51fd\u6570\u5b9a\u4e49\u65f6\u4f7f\u7528\u7684\u82b1\u62ec\u53f7\u548c\u548c\u8bed\u53e5\u79f0\u4e3a \u51fd\u6570\u4f53 \u3002 \u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u79f0\u4e3a \u5e26\u8fd4\u56de\u503c\u7684\u51fd\u6570 \u3002\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u6307\u660e\u4e86\u8fd4\u56de\u503c\u7684\u5177\u4f53\u7c7b\u578b\u3002 return \u8bed\u53e5\u6307\u5b9a\u4e86\u8981\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u7684\u5177\u4f53\u503c\u3002 \u8fd4\u56de\u503c\u662f\u901a\u8fc7 \u62f7\u8d1d \u7684\u65b9\u5f0f\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u7684\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a \u6309\u503c\u8fd4\u56de \u3002\u5982\u679c\u4ee5\u975e\u7a7a\u51fd\u6570\u6ca1\u6709\u80fd\u591f\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a main \u51fd\u6570\u7684\u8fd4\u56de\u503c\u79f0\u4e3a\u7a0b\u5e8f\u7684\u72b6\u6001\u7801\u3002\u7a0b\u5e8f\u901a\u8fc7\u8fd4\u56de\u7684\u72b6\u6001\u7801\u5411\u64cd\u4f5c\u7cfb\u7edf\u8868\u660e\u7a0b\u5e8f\u662f\u5426\u6210\u529f\u6267\u884c\u3002\u8fd4\u56de\u503c\u4e3a 0 \u65f6\u8868\u793a\u6210\u529f\uff0c\u8fd4\u56de\u6b63\u6570\u5219\u8868\u793a\u6267\u884c\u5931\u8d25\u3002 \u8df5\u884c DRY \u7f16\u7a0b\u2014\u2014\u201d\u5373\u4e0d\u8981\u91cd\u590d\u4f60\u505a\u8fc7\u7684\u4e8b\u201c\u3002\u4f7f\u7528\u53d8\u91cf\u548c\u51fd\u6570\u6765\u907f\u514d\u5197\u4f59\u4ee3\u7801\u3002 \u8fd4\u56de\u503c\u7c7b\u578b\u4e3a void \u7684\u51fd\u6570\uff08 2.3 - void \u51fd\u6570 \uff09\uff0c\u4e0d\u9700\u8981\u8fd4\u56de\u4efb\u4f55\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u8fd9\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u79f0\u4e3a void \u51fd\u6570 \u6216\u65e0\u8fd4\u56de\u503c\u7684\u51fd\u6570\u3002Void \u51fd\u6570\u4e0d\u80fd\u88ab\u4f7f\u7528\u5728\u9700\u8981\u8fd4\u56de\u503c\u7684\u573a\u5408\u3002 return \u8bed\u53e5\u5982\u679c\u4e0d\u662f\u51fd\u6570\u7684\u6700\u540e\u4e00\u6761\u8bed\u53e5\uff0c\u5219\u79f0\u4e3a \u63d0\u524d\u8fd4\u56de \u3002 return \u8bed\u53e5\u4f1a\u5bfc\u81f4\u51fd\u6570\u63d0\u524d\u7ec8\u6b62\u5e76\u7acb\u5373\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570\u3002 \u51fd\u6570\u7684 \u5f62\u53c2 \u4e5f\u662f\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\uff0c\u5b83\u7684\u503c\u7531\u4e3b\u8c03\u51fd\u6570\u63d0\u4f9b\u3002\u51fd\u6570\u7684 \u5b9e\u53c2 \u6307\u7684\u662f\u5c31\u662f\u7531\u4e3b\u8c03\u51fd\u6570\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u5177\u4f53\u503c\u3002\u5f53\u5b9e\u53c2\u901a\u8fc7\u62f7\u8d1d\u7684\u65b9\u5f0f\u4f20\u9012\u7ed9\u5f62\u53c2\u65f6\uff0c\u79f0\u4e3a \u6309\u503c\u4f20\u9012(pass by value) \u3002 C++ \u5e76\u6ca1\u6709\u5b9a\u4e49\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u5bf9\u5b9e\u53c2\u7684\u6c42\u503c\u662f\u4ece\u5de6\u5411\u53f3\u8fd8\u662f\u4ece\u53f3\u5411\u5de6\u3002 \u51fd\u6570\u7684\u5f62\u53c2\u548c\u5b9a\u4e49\u5728\u51fd\u6570\u4f53\u5185\u7684\u53d8\u91cf\u79f0\u4e3a\u5c40\u90e8\u53d8\u91cf\u3002\u53d8\u91cf\u5b58\u5728\u7684\u65f6\u95f4\u8303\u56f4\u79f0\u4e3a\u5176\u58f0\u660e\u5468\u671f\u3002\u53d8\u91cf\u5728 \u8fd0\u884c\u65f6 \uff08\u5373\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u5019\uff09\u88ab\u521b\u5efa\u548c\u9500\u6bc1\u3002\u53d8\u91cf\u7684 \u4f5c\u7528\u57df \u51b3\u5b9a\u4e86\u5b83\u5728\u54ea\u91cc\u53ef\u4ee5\u88ab\u8bbf\u95ee\uff0c\u5f53\u53d8\u91cf\u53ef\u4ee5\u88ab\u8bbf\u95ee\u65f6\uff0c\u6211\u4eec\u79f0\u5176\u5728\u4f5c\u7528\u57df\u5185\u3002\u5f53\u5b83\u4e0d\u53ef\u4ee5\u88ab\u8bbf\u95ee\u65f6\uff0c\u6211\u4eec\u79f0\u5176\u4e3a\u4e0d\u5728\u4f5c\u7528\u57df\u3002\u4f5c\u7528\u57df\u662f\u4e00\u4e2a \u7f16\u8bd1\u65f6 \u5c5e\u6027\uff0c\u5373\u5728\u7f16\u8bd1\u65f6\u5c31\u4f1a\u5bf9\u5176\u8fdb\u884c\u68c0\u67e5\u3002 \u7a7a\u767d\u7b26\u6307\u7684\u662f\u54ea\u4e9b\u7528\u6765\u6392\u7248\u7684\u5b57\u7b26\uff0c\u5728C++\u4e2d\uff0c\u5305\u62ec\u7a7a\u683c\u3001\u5236\u8868\u7b26\u548c\u6362\u884c\u3002 \u524d\u5411\u58f0\u660e ( 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 )\u4f7f\u6211\u4eec\u53ef\u4ee5\u5728\u5b9a\u4e49\u6807\u8bc6\u7b26\u4e4b\u524d\uff0c\u544a\u77e5\u7f16\u8bd1\u5668\u8be5\u6807\u8bc6\u7b26\u5b58\u5728\u3002\u7f16\u5199\u51fd\u6570\u7684\u524d\u5411\u58f0\u660e\uff0c\u9700\u8981\u4f7f\u7528\u51fd\u6570\u539f\u578b\uff0c\u5305\u62ec\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u3001\u51fd\u6570\u540d\u548c\u5f62\u53c2\uff0c\u4f46\u4e0d\u9700\u8981\u51fd\u6570\u4f53\u3002 \u5b9a\u4e49 ( 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 )\u5b9e\u9645\u4e0a\u5b9e\u73b0\u4e86\uff08\u5bf9\u4e8e\u51fd\u6570\u548c\u7c7b\u578b\u800c\u8a00\uff09\u6216 \u5b9e\u4f8b\u5316(instantiated) \u4e86\u4e00\u4e2a\u6807\u8bc6\u7b26\u3002 \u58f0\u660e \u5219\u662f\u4e00\u6761\u8bed\u53e5\uff0c\u7528\u4e8e\u544a\u77e5\u7f16\u8bd1\u5668\u6807\u8bc6\u7b26\u662f\u5b58\u5728\u7684\u3002\u5728 C++ \u4e2d\uff0c \u6240\u6709\u7684\u5b9a\u4e49\u90fd\u662f\u58f0\u660e \u3002 \u7eaf\u58f0\u660e \u6307\u7684\u662f\u4e0d\u5177\u6709\u5b9a\u4e49\u529f\u80fd\u7684\u58f0\u660e\uff08\u4f8b\u5982\u51fd\u6570\u539f\u578b\uff09\u3002 \u5927\u591a\u6570\u5177\u6709\u4e00\u5b9a\u89c4\u6a21\u7684\u7a0b\u5e8f\u90fd\u5305\u542b\u591a\u4e2a\u6587\u4ef6\u3002\uff08 2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f \uff09 \u5982\u679c\u4e24\u4e2a\u6807\u8bc6\u7b26\u88ab\u5f15\u5165\u4e86\u540c\u4e00\u4e2a\u7a0b\u5e8f\u4f7f\u5f97\u7f16\u8bd1\u5668\u6216\u94fe\u63a5\u5668\u65e0\u6cd5\u5bf9\u5176\u8fdb\u884c\u533a\u5206\uff0c\u5219\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u6216\u94fe\u63a5\u5668\u62a5\u544a \u547d\u540d\u51b2\u7a81 \u3002 \u547d\u540d\u7a7a\u95f4 \uff08 2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 \uff09\u786e\u4fdd\u6240\u6709\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u90fd\u662f\u72ec\u4e00\u65e0\u4e8c\u7684\u3002 std \u5c31\u5c5e\u4e8e\u547d\u540d\u7a7a\u95f4\u7684\u4e00\u79cd \u9884\u5904\u7406\u5668\uff08 2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb \uff09\u662f\u4e00\u79cd\u7528\u4e8e\u5728\u4ee3\u7801\u7f16\u8bd1\u524d\u5bf9\u6587\u4ef6\u8fdb\u884c\u9884\u5904\u7406\u7684\u7a0b\u5e8f\u3002 \u9884\u5904\u7406\u5668\u6307\u4ee4 \u4ee5 # \u5f00\u5934\uff0c\u4ee5\u6362\u884c\u7ed3\u5c3e\u3002 \u5b8f \u6307\u7684\u662f\u4e00\u6761\u5c06\u8f93\u5165\u6587\u672c\u5982\u4f55\u66ff\u6362\u4e3a\u8f93\u51fa\u6587\u672c\u7684\u89c4\u5219\u3002 \u5934\u6587\u4ef6\uff08 2.11 - \u5934\u6587\u4ef6 \uff09\u6307\u7684\u662f\u5305\u542b\u4ee3\u7801\u58f0\u660e\u7684\u6587\u4ef6\u3002\u5f53\u4f7f\u7528 #include \u6307\u4ee4\u65f6\uff0c\u5b83\u4f1a\u628a\u5934\u6587\u4ef6\u7684\u5185\u5bb9\u66ff\u6362\u5230\u8be5\u6307\u4ee4\u7684\u4f4d\u7f6e\u3002\u5728\u5305\u542b\u5934\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u5c16\u62ec\u53f7\u6765\u5904\u7406\u7cfb\u7edf\u5934\u6587\u4ef6\uff08\u4f8b\u5982C++\u6807\u51c6\u5e93\u7684\u5934\u6587\u4ef6\uff09\uff0c\u4f7f\u7528\u53cc\u5f15\u53f7\u6765\u5904\u7406\u7528\u6237\u81ea\u5b9a\u4e49\u5934\u6587\u4ef6\u3002\u5f53\u4f7f\u7528\u7cfb\u7edf\u63d0\u4f9b\u7684\u5934\u6587\u4ef6\u65f6\uff0c\u4f7f\u7528\u4e0d\u5e26 .h \u540e\u7f00\u7684\u7248\u672c\uff08\u5982\u679c\u5b58\u5728\u7684\u8bdd\uff09 \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e\uff08 2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e ]\uff09\u53ef\u4ee5\u907f\u514d\u5934\u6587\u4ef6\u88ab\u591a\u6b21\u5305\u542b\u5230\u540c\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u4f46\u662f\u4e0d\u4f1a\u907f\u514d\u5b83\u4eec\u88ab\u5305\u542b\u5230\u591a\u4e2a\u6587\u4ef6\u4e2d\uff08\u6bcf\u4e2a\u6587\u4ef6\u53ea\u80fd\u5305\u542b\u4e00\u6b21\uff09\u3002","title":"2.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570\u4e0e\u6587\u4ef6"},{"location":"2-x-Chapter-2-summary-and-quiz/#_1","text":"\u51fd\u6570 \u662f\u4e00\u7cfb\u5217\u53ef\u4ee5\u590d\u7528\u7684\u8bed\u53e5\uff0c\u7528\u6765\u5b8c\u6210\u67d0\u4e2a\u7279\u5b9a\u7684\u4efb\u52a1\u3002\u7528\u6237\u81ea\u5df1\u7f16\u5199\u7684\u51fd\u6570\u79f0\u4e3a\u81ea\u5b9a\u4e49\u51fd\u6570\u3002 \u51fd\u6570\u8c03\u7528 \u662f\u4e00\u79cd\u8868\u8fbe\u5f0f\uff0c\u7528\u4e8e\u544a\u8bc9CPU\u53bb\u6267\u884c\u4e00\u4e2a\u51fd\u6570\u3002\u6267\u884c\u51fd\u6570\u8c03\u7528\u7684\u51fd\u6570\uff0c\u79f0\u4e3a \u4e3b\u8c03\u51fd\u6570 \uff0c\u88ab\u8c03\u7528\u7684\u51fd\u6570\u79f0\u4e3a \u88ab\u8c03\u51fd\u6570 \u3002\u5728\u8c03\u7528\u51fd\u6570\u7684\u65f6\u5019\uff0c\u4e0d\u8981\u5fd8\u8bb0\u51fd\u6570\u540d\u540e\u9762\u7684\u62ec\u53f7\u3002 \u51fd\u6570\u5b9a\u4e49\u65f6\u4f7f\u7528\u7684\u82b1\u62ec\u53f7\u548c\u548c\u8bed\u53e5\u79f0\u4e3a \u51fd\u6570\u4f53 \u3002 \u5177\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u79f0\u4e3a \u5e26\u8fd4\u56de\u503c\u7684\u51fd\u6570 \u3002\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u6307\u660e\u4e86\u8fd4\u56de\u503c\u7684\u5177\u4f53\u7c7b\u578b\u3002 return \u8bed\u53e5\u6307\u5b9a\u4e86\u8981\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u7684\u5177\u4f53\u503c\u3002 \u8fd4\u56de\u503c\u662f\u901a\u8fc7 \u62f7\u8d1d \u7684\u65b9\u5f0f\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u7684\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a \u6309\u503c\u8fd4\u56de \u3002\u5982\u679c\u4ee5\u975e\u7a7a\u51fd\u6570\u6ca1\u6709\u80fd\u591f\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a main \u51fd\u6570\u7684\u8fd4\u56de\u503c\u79f0\u4e3a\u7a0b\u5e8f\u7684\u72b6\u6001\u7801\u3002\u7a0b\u5e8f\u901a\u8fc7\u8fd4\u56de\u7684\u72b6\u6001\u7801\u5411\u64cd\u4f5c\u7cfb\u7edf\u8868\u660e\u7a0b\u5e8f\u662f\u5426\u6210\u529f\u6267\u884c\u3002\u8fd4\u56de\u503c\u4e3a 0 \u65f6\u8868\u793a\u6210\u529f\uff0c\u8fd4\u56de\u6b63\u6570\u5219\u8868\u793a\u6267\u884c\u5931\u8d25\u3002 \u8df5\u884c DRY \u7f16\u7a0b\u2014\u2014\u201d\u5373\u4e0d\u8981\u91cd\u590d\u4f60\u505a\u8fc7\u7684\u4e8b\u201c\u3002\u4f7f\u7528\u53d8\u91cf\u548c\u51fd\u6570\u6765\u907f\u514d\u5197\u4f59\u4ee3\u7801\u3002 \u8fd4\u56de\u503c\u7c7b\u578b\u4e3a void \u7684\u51fd\u6570\uff08 2.3 - void \u51fd\u6570 \uff09\uff0c\u4e0d\u9700\u8981\u8fd4\u56de\u4efb\u4f55\u503c\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u8fd9\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u79f0\u4e3a void \u51fd\u6570 \u6216\u65e0\u8fd4\u56de\u503c\u7684\u51fd\u6570\u3002Void \u51fd\u6570\u4e0d\u80fd\u88ab\u4f7f\u7528\u5728\u9700\u8981\u8fd4\u56de\u503c\u7684\u573a\u5408\u3002 return \u8bed\u53e5\u5982\u679c\u4e0d\u662f\u51fd\u6570\u7684\u6700\u540e\u4e00\u6761\u8bed\u53e5\uff0c\u5219\u79f0\u4e3a \u63d0\u524d\u8fd4\u56de \u3002 return \u8bed\u53e5\u4f1a\u5bfc\u81f4\u51fd\u6570\u63d0\u524d\u7ec8\u6b62\u5e76\u7acb\u5373\u8fd4\u56de\u4e3b\u8c03\u51fd\u6570\u3002 \u51fd\u6570\u7684 \u5f62\u53c2 \u4e5f\u662f\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\uff0c\u5b83\u7684\u503c\u7531\u4e3b\u8c03\u51fd\u6570\u63d0\u4f9b\u3002\u51fd\u6570\u7684 \u5b9e\u53c2 \u6307\u7684\u662f\u5c31\u662f\u7531\u4e3b\u8c03\u51fd\u6570\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u5177\u4f53\u503c\u3002\u5f53\u5b9e\u53c2\u901a\u8fc7\u62f7\u8d1d\u7684\u65b9\u5f0f\u4f20\u9012\u7ed9\u5f62\u53c2\u65f6\uff0c\u79f0\u4e3a \u6309\u503c\u4f20\u9012(pass by value) \u3002 C++ \u5e76\u6ca1\u6709\u5b9a\u4e49\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u5bf9\u5b9e\u53c2\u7684\u6c42\u503c\u662f\u4ece\u5de6\u5411\u53f3\u8fd8\u662f\u4ece\u53f3\u5411\u5de6\u3002 \u51fd\u6570\u7684\u5f62\u53c2\u548c\u5b9a\u4e49\u5728\u51fd\u6570\u4f53\u5185\u7684\u53d8\u91cf\u79f0\u4e3a\u5c40\u90e8\u53d8\u91cf\u3002\u53d8\u91cf\u5b58\u5728\u7684\u65f6\u95f4\u8303\u56f4\u79f0\u4e3a\u5176\u58f0\u660e\u5468\u671f\u3002\u53d8\u91cf\u5728 \u8fd0\u884c\u65f6 \uff08\u5373\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u5019\uff09\u88ab\u521b\u5efa\u548c\u9500\u6bc1\u3002\u53d8\u91cf\u7684 \u4f5c\u7528\u57df \u51b3\u5b9a\u4e86\u5b83\u5728\u54ea\u91cc\u53ef\u4ee5\u88ab\u8bbf\u95ee\uff0c\u5f53\u53d8\u91cf\u53ef\u4ee5\u88ab\u8bbf\u95ee\u65f6\uff0c\u6211\u4eec\u79f0\u5176\u5728\u4f5c\u7528\u57df\u5185\u3002\u5f53\u5b83\u4e0d\u53ef\u4ee5\u88ab\u8bbf\u95ee\u65f6\uff0c\u6211\u4eec\u79f0\u5176\u4e3a\u4e0d\u5728\u4f5c\u7528\u57df\u3002\u4f5c\u7528\u57df\u662f\u4e00\u4e2a \u7f16\u8bd1\u65f6 \u5c5e\u6027\uff0c\u5373\u5728\u7f16\u8bd1\u65f6\u5c31\u4f1a\u5bf9\u5176\u8fdb\u884c\u68c0\u67e5\u3002 \u7a7a\u767d\u7b26\u6307\u7684\u662f\u54ea\u4e9b\u7528\u6765\u6392\u7248\u7684\u5b57\u7b26\uff0c\u5728C++\u4e2d\uff0c\u5305\u62ec\u7a7a\u683c\u3001\u5236\u8868\u7b26\u548c\u6362\u884c\u3002 \u524d\u5411\u58f0\u660e ( 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 )\u4f7f\u6211\u4eec\u53ef\u4ee5\u5728\u5b9a\u4e49\u6807\u8bc6\u7b26\u4e4b\u524d\uff0c\u544a\u77e5\u7f16\u8bd1\u5668\u8be5\u6807\u8bc6\u7b26\u5b58\u5728\u3002\u7f16\u5199\u51fd\u6570\u7684\u524d\u5411\u58f0\u660e\uff0c\u9700\u8981\u4f7f\u7528\u51fd\u6570\u539f\u578b\uff0c\u5305\u62ec\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u3001\u51fd\u6570\u540d\u548c\u5f62\u53c2\uff0c\u4f46\u4e0d\u9700\u8981\u51fd\u6570\u4f53\u3002 \u5b9a\u4e49 ( 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 )\u5b9e\u9645\u4e0a\u5b9e\u73b0\u4e86\uff08\u5bf9\u4e8e\u51fd\u6570\u548c\u7c7b\u578b\u800c\u8a00\uff09\u6216 \u5b9e\u4f8b\u5316(instantiated) \u4e86\u4e00\u4e2a\u6807\u8bc6\u7b26\u3002 \u58f0\u660e \u5219\u662f\u4e00\u6761\u8bed\u53e5\uff0c\u7528\u4e8e\u544a\u77e5\u7f16\u8bd1\u5668\u6807\u8bc6\u7b26\u662f\u5b58\u5728\u7684\u3002\u5728 C++ \u4e2d\uff0c \u6240\u6709\u7684\u5b9a\u4e49\u90fd\u662f\u58f0\u660e \u3002 \u7eaf\u58f0\u660e \u6307\u7684\u662f\u4e0d\u5177\u6709\u5b9a\u4e49\u529f\u80fd\u7684\u58f0\u660e\uff08\u4f8b\u5982\u51fd\u6570\u539f\u578b\uff09\u3002 \u5927\u591a\u6570\u5177\u6709\u4e00\u5b9a\u89c4\u6a21\u7684\u7a0b\u5e8f\u90fd\u5305\u542b\u591a\u4e2a\u6587\u4ef6\u3002\uff08 2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f \uff09 \u5982\u679c\u4e24\u4e2a\u6807\u8bc6\u7b26\u88ab\u5f15\u5165\u4e86\u540c\u4e00\u4e2a\u7a0b\u5e8f\u4f7f\u5f97\u7f16\u8bd1\u5668\u6216\u94fe\u63a5\u5668\u65e0\u6cd5\u5bf9\u5176\u8fdb\u884c\u533a\u5206\uff0c\u5219\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u6216\u94fe\u63a5\u5668\u62a5\u544a \u547d\u540d\u51b2\u7a81 \u3002 \u547d\u540d\u7a7a\u95f4 \uff08 2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 \uff09\u786e\u4fdd\u6240\u6709\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u90fd\u662f\u72ec\u4e00\u65e0\u4e8c\u7684\u3002 std \u5c31\u5c5e\u4e8e\u547d\u540d\u7a7a\u95f4\u7684\u4e00\u79cd \u9884\u5904\u7406\u5668\uff08 2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb \uff09\u662f\u4e00\u79cd\u7528\u4e8e\u5728\u4ee3\u7801\u7f16\u8bd1\u524d\u5bf9\u6587\u4ef6\u8fdb\u884c\u9884\u5904\u7406\u7684\u7a0b\u5e8f\u3002 \u9884\u5904\u7406\u5668\u6307\u4ee4 \u4ee5 # \u5f00\u5934\uff0c\u4ee5\u6362\u884c\u7ed3\u5c3e\u3002 \u5b8f \u6307\u7684\u662f\u4e00\u6761\u5c06\u8f93\u5165\u6587\u672c\u5982\u4f55\u66ff\u6362\u4e3a\u8f93\u51fa\u6587\u672c\u7684\u89c4\u5219\u3002 \u5934\u6587\u4ef6\uff08 2.11 - \u5934\u6587\u4ef6 \uff09\u6307\u7684\u662f\u5305\u542b\u4ee3\u7801\u58f0\u660e\u7684\u6587\u4ef6\u3002\u5f53\u4f7f\u7528 #include \u6307\u4ee4\u65f6\uff0c\u5b83\u4f1a\u628a\u5934\u6587\u4ef6\u7684\u5185\u5bb9\u66ff\u6362\u5230\u8be5\u6307\u4ee4\u7684\u4f4d\u7f6e\u3002\u5728\u5305\u542b\u5934\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u5c16\u62ec\u53f7\u6765\u5904\u7406\u7cfb\u7edf\u5934\u6587\u4ef6\uff08\u4f8b\u5982C++\u6807\u51c6\u5e93\u7684\u5934\u6587\u4ef6\uff09\uff0c\u4f7f\u7528\u53cc\u5f15\u53f7\u6765\u5904\u7406\u7528\u6237\u81ea\u5b9a\u4e49\u5934\u6587\u4ef6\u3002\u5f53\u4f7f\u7528\u7cfb\u7edf\u63d0\u4f9b\u7684\u5934\u6587\u4ef6\u65f6\uff0c\u4f7f\u7528\u4e0d\u5e26 .h \u540e\u7f00\u7684\u7248\u672c\uff08\u5982\u679c\u5b58\u5728\u7684\u8bdd\uff09 \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e\uff08 2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e ]\uff09\u53ef\u4ee5\u907f\u514d\u5934\u6587\u4ef6\u88ab\u591a\u6b21\u5305\u542b\u5230\u540c\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u4f46\u662f\u4e0d\u4f1a\u907f\u514d\u5b83\u4eec\u88ab\u5305\u542b\u5230\u591a\u4e2a\u6587\u4ef6\u4e2d\uff08\u6bcf\u4e2a\u6587\u4ef6\u53ea\u80fd\u5305\u542b\u4e00\u6b21\uff09\u3002","title":"\u7ae0\u8282\u56de\u987e"},{"location":"20-x-Chapter-20-comprehensive-quiz/","tags":["summary"],"text":"\u5f02\u5e38\u5904\u7406\u63d0\u4f9b\u4e86\u5c06\u9519\u8bef\u5904\u7406\u6216\u5f02\u5e38\u60c5\u51b5\u5904\u7406\u6d41\u7a0b\u4e0e\u6b63\u5e38\u6d41\u7a0b\u4ee3\u7801\u89e3\u8026\u7684\u4e00\u79cd\u673a\u5236\u3002\u8fd9\u6837\u4e00\u6765\u7a0b\u5e8f\u5458\u5c31\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\uff0c\u66f4\u52a0\u81ea\u5982\u5730\u5904\u7406\u9519\u8bef\uff0c\u540c\u65f6\u8fd8\u53ef\u4ee5\u5728\u5f88\u5927\u7a0b\u5ea6\u4e0a\u7f13\u89e3\u7531\u57fa\u4e8e\u8fd4\u56de\u9519\u8bef\u7801\u7684\u65b9\u5f0f\u5904\u7406\u5f02\u5e38\u6240\u5e26\u6765\u7684\u95ee\u9898\u3002 throw \u8bed\u53e5\u53ef\u4ee5\u7528\u6765\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u3002 Try \u8bed\u53e5\u5757 \u5219\u4f1a\u5173\u6ce8\u5176\u5757\u5185\u4ee3\u7801\u662f\u5426\u629b\u51fa\u5f02\u5e38\u3002\u4e00\u65e6\u6709\u5f02\u5e38\u629b\u51fa\uff0c\u8fd9\u4e9b\u5f02\u5e38\u5c31\u4f1a\u88ab\u91cd\u5b9a\u5411\u5230\u5bf9\u5e94\u7684 catch \u8bed\u53e5\u5757 \u4e2d\uff0c\u4e0d\u540c\u7c7b\u578b\u7684\u8bed\u53e5\u5757\u53ef\u4ee5\u6355\u83b7\u5bf9\u5e94\u7c7b\u578b\u7684\u5f02\u5e38\u5e76\u5bf9\u5176\u8fdb\u884c\u5904\u7406\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5f02\u5e38\u53ea\u8981\u88ab\u6355\u83b7\uff0c\u5c31\u53ef\u4ee5\u8ba4\u4e3a\u662f\u5904\u7406\u4e86\u3002 \u5f02\u5e38\u4f1a\u88ab\u7acb\u5373\u5904\u7406\u2014\u2014\u4e00\u65e6\u5f02\u5e38\u88ab\u629b\u51fa\uff0c\u63a7\u5236\u6d41\u4f1a\u7acb\u5373\u8df3\u8f6c\u5230\u8ddd\u79bb\u8be5Try\u8bed\u53e5\u5757\u6700\u8fd1\uff0c\u4e14\u80fd\u591f\u5904\u7406\u8be5\u5f02\u5e38\u7684catch\u8bed\u53e5\u5757\u3002\u5982\u679c\u629b\u51fa\u5f02\u5e38\u7684\u8bed\u53e5\u4e0d\u4f4d\u4e8e\u4efb\u4f55try\u8bed\u53e5\u5757\u4e2d\uff0c\u5219\u5f02\u5e38\u4f1a\u6cbf\u7740\u51fd\u6570\u8c03\u7528\u6808\u4e0a\u9001\uff0c\u5982\u679c\u5728\u6574\u4e2a\u51fd\u6570\u6808\u4e2d\u90fd\u6ca1\u6709\u80fd\u591f\u627e\u5230\u53ef\u7528\u7684\u5f02\u5e38\u5904\u7406\u903b\u8f91\uff0c\u7a0b\u5e8f\u5c31\u4f1a\u56e0\u4e3a \u5f02\u5e38\u672a\u5904\u7406 \u800c\u505c\u6b62\u8fd0\u884c\u3002 \u629b\u51fa\u7684\u5f02\u5e38\u53ef\u4ee5\u662f\u4efb\u610f\u7c7b\u578b\u7684\uff0c\u5305\u62ec\u7c7b\u3002 catch \u8bed\u53e5\u5757\u53ef\u4ee5\u88ab\u58f0\u660e\u4e3a\u53ea\u6355\u83b7\u67d0\u79cd\u7279\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7701\u7565\u53f7\u6765\u58f0\u660e\u4e3a\u6355\u83b7\u6240\u6709\u7c7b\u578b\u7684\u5f02\u5e38\u3002\u80fd\u591f\u6355\u83b7\u67d0\u79cd\u57fa\u7c7b\u7684 catch \u8bed\u53e5\u5757\uff0c\u4e5f\u53ef\u4ee5\u6355\u83b7\u8be5\u57fa\u7c7b\u5bf9\u5e94\u7684\u6d3e\u751f\u7c7b\u7c7b\u578b\u7684\u5f02\u5e38\u3002\u6807\u51c6\u5e93\u4e2d\u629b\u51fa\u7684\u6240\u6709\u5f02\u5e38\u7c7b\u578b\uff0c\u90fd\u6d3e\u751f\u81ea std::exception \u7c7b\uff08\u4f4d\u4e8e exception \u5934\u6587\u4ef6\u4e2d\uff09\u3002\u56e0\u6b64\uff0c\u6355\u83b7 std::exception \u7684 catch \u7684\u8bed\u53e5\u5757\uff0c\u53ef\u4ee5\u6355\u83b7\u6807\u51c6\u5e93\u4e2d\u7684\u5f02\u5e38\u3002 what() \u6210\u5458\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u786e\u5b9a\u88ab\u629b\u51fa\u7684 std::exception \u662f\u4ec0\u4e48\u3002 \u5728 catch \u8bed\u53e5\u5757\u4e2d\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u629b\u51fa\u65b0\u7684\u9057\u4ea7\u3002\u56e0\u4e3a\u8fd9\u4e2a\u5f02\u5e38\u4e0d\u662f\u5728 try \u8bed\u53e5\u5757\u4e2d\u629b\u51fa\u7684\uff0c\u6240\u4ee5\u5b83\u4e0d\u4f1a\u88ab\u629b\u51fa\u5b83\u7684 catch \u8bed\u53e5\u5757\u6355\u83b7\u3002\u5f02\u5e38\u53ef\u4ee5\u5728 catch \u8bed\u53e5\u5757\u4e2d\u901a\u8fc7 throw \u5173\u952e\u5b57\u5c06\u8be5\u5f02\u5e38\u91cd\u65b0\u629b\u51fa\u3002\u4e0d\u8fc7\uff0c\u4e0d\u8981\u5c06\u6355\u83b7\u7684\u7684\u5f02\u5e38\u53d8\u91cf\u76f4\u63a5\u91cd\u65b0\u629b\u51fa\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4 \u5bf9\u8c61\u5207\u7247 \u3002 \u51fd\u6570\u4e2d\u7684try-block\u8bed\u53e5\u5757\u4f7f\u6211\u4eec\u53ef\u4ee5\u6355\u83b7\u53d1\u751f\u5728\u51fd\u6570\u5185\u90e8\u6216\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u53d1\u751f\u7684\u5f02\u5e38\u3002\u901a\u5e38\u53ea\u4f1a\u5728\u6d3e\u751f\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4e2d\u4f7f\u7528\u3002 \u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\uff0c\u4e5f\u4e0d\u8981\u5728\u6790\u6784\u51fd\u6570\u4e2d\u629b\u51fa\u5f02\u5e38\u3002 \u901a\u8fc7 noexcept \u5f02\u5e38\u6807\u8bc6\u7b26\u53ef\u4ee5\u5c06\u51fd\u6570\u6807\u8bb0\u4e3a\u4e0d\u5f15\u53d1\u5f02\u5e38\u3002 \u5f02\u5e38\u5904\u7406\u662f\u6709\u5f00\u9500\u7684\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u5f02\u5e38\u7684\u4ee3\u7801\u4f1a\u7a0d\u6162\u4e00\u4e9b\uff0c\u800c\u4e14\u5904\u7406\u5f02\u5e38\u7684\u5f00\u9500\u975e\u5e38\u9ad8\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5e94\u8be5\u53ea\u5728\u9700\u8981\u5904\u7406\u5f02\u5e38\u7684\u573a\u5408\u4e0b\u4f7f\u7528\u5f02\u5e38\uff0c\u800c\u4e0d\u8981\u5728\u4e00\u822c\u7684\u9519\u8bef\u5904\u7406\u65f6\u4f7f\u7528\u5f02\u5e38\uff08\u4f8b\u5982\u5904\u7406\u975e\u6cd5\u8f93\u5165\uff09\u3002","title":"20.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f02\u5e38"},{"location":"20.4/","text":"","title":"20.4"},{"location":"21-1-the-standard-library/","tags":["stl"],"text":"Key Takeaway \u606d\u559c\u4f60\uff01\u4f60\u5df2\u7ecf\u5b8c\u6210\u4e86\u6559\u7a0b\u7684\u4e3b\u8981\u90e8\u5206\uff01\u5728\u524d\u9762\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86C++\u8bed\u8a00\u7684\u8bb8\u591a\u4e3b\u8981\u7279\u6027(\u5305\u62ecC++11/14/17\u6269\u5c55\u7684\u4e00\u4e9b\u7279\u6027)\u3002 \u90a3\u4e48\u4f60\u80af\u5b9a\u8981\u95ee\u4e86\uff0c\u201c\u63a5\u4e0b\u6765\u8981\u600e\u4e48\u505a\u5462\uff1f\u201d \u4f60\u53ef\u80fd\u5df2\u7ecf\u6ce8\u610f\u5230\u4e86\uff0c\u5927\u91cf\u7684\u7a0b\u5e8f\u90fd\u5728\u53cd\u590d\u4f7f\u7528\u76f8\u540c\u7684\u6982\u5ff5\uff1a\u5faa\u73af\u3001\u5b57\u7b26\u4e32\u3001\u6570\u7ec4\u3001\u6392\u5e8f\u7b49\u2026\u2026\u4f60\u53ef\u80fd\u8fd8\u6ce8\u610f\u5230\uff0c\u4f7f\u7528\u975e\u7c7b\u7248\u672c\u7684\u5bb9\u5668\u548c\u5e38\u7528\u7b97\u6cd5\u7f16\u5199\u7684\u7a0b\u5e8f\u5f88\u5bb9\u6613\u51fa\u9519\u3002\u597d\u6d88\u606f\u662f\uff0cC++\u9644\u5e26\u4e86\u4e00\u4e2a\u5e93\uff0c\u5176\u4e2d\u63d0\u4f9b\u4e86\u5f88\u591a\u53ef\u91cd\u7528\u7684\u7c7b\uff0c\u4f60\u53ef\u4ee5\u5229\u7528\u5b83\u4eec\u6784\u5efa\u7a0b\u5e8f\u3002\u8fd9\u4e2a\u5e93\u88ab\u79f0\u4e3aC++\u6807\u51c6\u5e93\u3002 \u6807\u51c6\u5e93 \u6807\u51c6\u5e93\u5305\u542b\u4e00\u7ec4\u7c7b\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u6a21\u677f\u5bb9\u5668\u3001\u7b97\u6cd5\u548c\u8fed\u4ee3\u5668\u3002\u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a\u5e38\u7528\u7684\u7c7b\u6216\u7b97\u6cd5\uff0c\u6807\u51c6\u5e93\u4e2d\u5f88\u53ef\u80fd\u5df2\u7ecf\u63d0\u4f9b\u4e86\u5b83\u3002\u4f7f\u7528\u6807\u51c6\u5e93\u7684\u4f18\u70b9\u662f\u4f60\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8fd9\u4e9b\u7c7b\uff0c\u800c\u4e0d\u5fc5\u81ea\u5df1\u7f16\u5199\u548c\u8c03\u8bd5\u7c7b\u3002\u800c\u4e14\u6807\u51c6\u5e93\u4e2d\u7684\u7c7b\u90fd\u975e\u5e38\u9ad8\u6548\u3002\u7f3a\u70b9\u662f\u6807\u51c6\u5e93\u5f88\u590d\u6742\uff0c\u800c\u4e14\u770b\u8d77\u6765\u53ef\u80fd\u6709\u70b9\u5413\u4eba\uff0c\u56e0\u4e3a\u6240\u6709\u4e1c\u897f\u90fd\u662f\u6a21\u677f\u5316\u7684\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u4f60\u53ef\u4ee5\u5148\u5c06\u6807\u51c6\u5e93\u5206\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u53ea\u4f7f\u7528\u4f60\u9700\u8981\u7684\u90e8\u5206\uff0c\u800c\u5ffd\u7565\u5176\u4ed6\u90e8\u5206\uff0c\u76f4\u5230\u4f60\u9700\u8981\u4f7f\u7528\u5b83\u4eec\u7684\u65f6\u5019\u518d\u53bb\u4ed4\u7ec6\u7814\u7a76\u3002 \u5728\u63a5\u4e0b\u6765\u7684\u51e0\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f1a\u5bbd\u6cdb\u5730\u4e86\u89e3\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u5bb9\u5668\u3001\u7b97\u6cd5\u548c\u8fed\u4ee3\u5668\u7684\u7c7b\u578b\u3002\u7136\u540e\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u6df1\u5165\u7814\u7a76\u4e00\u4e9b\u5177\u4f53\u7684\u7c7b\u3002","title":"21.1 - \u6807\u51c6\u5e93"},{"location":"21-1-the-standard-library/#_1","text":"\u6807\u51c6\u5e93\u5305\u542b\u4e00\u7ec4\u7c7b\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u6a21\u677f\u5bb9\u5668\u3001\u7b97\u6cd5\u548c\u8fed\u4ee3\u5668\u3002\u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a\u5e38\u7528\u7684\u7c7b\u6216\u7b97\u6cd5\uff0c\u6807\u51c6\u5e93\u4e2d\u5f88\u53ef\u80fd\u5df2\u7ecf\u63d0\u4f9b\u4e86\u5b83\u3002\u4f7f\u7528\u6807\u51c6\u5e93\u7684\u4f18\u70b9\u662f\u4f60\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8fd9\u4e9b\u7c7b\uff0c\u800c\u4e0d\u5fc5\u81ea\u5df1\u7f16\u5199\u548c\u8c03\u8bd5\u7c7b\u3002\u800c\u4e14\u6807\u51c6\u5e93\u4e2d\u7684\u7c7b\u90fd\u975e\u5e38\u9ad8\u6548\u3002\u7f3a\u70b9\u662f\u6807\u51c6\u5e93\u5f88\u590d\u6742\uff0c\u800c\u4e14\u770b\u8d77\u6765\u53ef\u80fd\u6709\u70b9\u5413\u4eba\uff0c\u56e0\u4e3a\u6240\u6709\u4e1c\u897f\u90fd\u662f\u6a21\u677f\u5316\u7684\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u4f60\u53ef\u4ee5\u5148\u5c06\u6807\u51c6\u5e93\u5206\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u53ea\u4f7f\u7528\u4f60\u9700\u8981\u7684\u90e8\u5206\uff0c\u800c\u5ffd\u7565\u5176\u4ed6\u90e8\u5206\uff0c\u76f4\u5230\u4f60\u9700\u8981\u4f7f\u7528\u5b83\u4eec\u7684\u65f6\u5019\u518d\u53bb\u4ed4\u7ec6\u7814\u7a76\u3002 \u5728\u63a5\u4e0b\u6765\u7684\u51e0\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f1a\u5bbd\u6cdb\u5730\u4e86\u89e3\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u5bb9\u5668\u3001\u7b97\u6cd5\u548c\u8fed\u4ee3\u5668\u7684\u7c7b\u578b\u3002\u7136\u540e\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u6df1\u5165\u7814\u7a76\u4e00\u4e9b\u5177\u4f53\u7684\u7c7b\u3002","title":"\u6807\u51c6\u5e93"},{"location":"21-2-STL-containers-overview/","tags":["stl","containers"],"text":"Key Takeaway STL\u5e93\u4e2d\u6700\u6709\u7528\u7684\u529f\u80fd\u8981\u7b97\u662fSTL\u5bb9\u5668\u7c7b\u4e86\u3002\u5982\u679c\u4f60\u9700\u8981\u5feb\u901f\u590d\u4e60\u4e00\u4e0b\u4ec0\u4e48\u662f\u5bb9\u5668\u7c7b\uff0c\u8bf7\u53c2\u8003 16.6 - \u5bb9\u5668\u7c7b \u3002 STL \u63d0\u4f9b\u4e86\u5f88\u591a\u4e0d\u540c\u573a\u5408\u4f7f\u7528\u7684 \u5bb9\u5668\u7c7b \u3002\u901a\u5e38\uff0c\u8fd9\u4e9b\u5bb9\u5668\u7c7b\u53ef\u4ee5\u5206\u4e3a\u4e09\u4e2a\u5927\u7c7b\uff1a \u987a\u5e8f\u5bb9\u5668 \uff0c \u5173\u8054\u5bb9\u5668 \u548c \u5bb9\u5668\u9002\u914d\u5668 \u3002 \u987a\u5e8f\u5bb9\u5668 \u987a\u5e8f\u5bb9\u5668\u662f\u6307\u90a3\u4e9b\u5728\u5bb9\u5668\u4e2d\u6309\u987a\u5e8f\u5b58\u653e\u5143\u7d20\u7684\u5bb9\u5668\u7c7b\u3002\u987a\u5e8f\u5bb9\u5668\u7684\u4e00\u5927\u7279\u70b9\u5c31\u662f\u4f60\u53ef\u4ee5\u51b3\u5b9a\u5728\u54ea\u4e2a\u4f4d\u7f6e\u63d2\u5165\u5143\u7d20\u3002\u6700\u5e38\u89c1\u7684\u987a\u5e8f\u5bb9\u5668\u7684\u4f8b\u5b50\u5c31\u662f\u6570\u7ec4\uff1a\u5982\u679c\u4f60\u9700\u8981\u5728\u6570\u7ec4\u7684\u67d0\u4e2a\u4f4d\u7f6e\u63d2\u5165\u5143\u7d20\uff0c\u90a3\u4e48\u8be5\u5143\u7d20\u4e00\u5b9a\u4f1a\u88ab\u63d2\u5165\u5230\u8fd9\u4e2a\u4f4d\u7f6e\u3002 \u5bf9\u4e8eC++11\u6765\u8bf4\uff0cSTL\u5305\u542b\u4e866\u79cd\u987a\u5e8f\u5bb9\u5668\uff1a std::vector std::deque std::array std::list std::forward_list std::basic_string \u5982\u679c\u4f60\u5b66\u8fc7\u7269\u7406\u7684\u8bdd\uff0c\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3avector\u8868\u793a\u7684\u5177\u6709\u5927\u5c0f\u548c\u65b9\u5411\u7684\u5411\u91cf\u3002\u4e0d\u8fc7\uff0cSTL\u4e2d\u7684 vector \u53ef\u4e0d\u662f\u5411\u91cf\uff0c\u5b83\u662f\u4e00\u4e2a\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u5927\u5c0f\u7684\u52a8\u6001\u6570\u7ec4\u3002vector \u5bb9\u5668\u7c7b\u652f\u6301\u901a\u8fc7\u4e0b\u6807\u8fd0\u7b97\u7b26\u8fdb\u884c\u968f\u673a\u8bbf\u95ee\uff0c\u4e5f\u652f\u6301\u50cf\u5c3e\u90e8\u5feb\u901f\u63d2\u5165\u5143\u7d20\u3002 \u4e0b\u9762\u7684\u4ee3\u7801\u5411 vector \u4e2d\u63d2\u5165\u4e86 6 \u4e2a\u6570\u5b57\uff0c\u5e76\u4e14\u4f7f\u7528\u4e0b\u6807\u8fd0\u7b97\u7b26\u4f9d\u6b21\u8bbf\u95ee\u5e76\u6253\u5370\u5143\u7d20\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: vector < int > vect ; for ( int count = 0 ; count < 6 ; ++ count ) vect . push_back ( 10 - count ); // insert at end of array for ( int index = 0 ; index < vect . size (); ++ index ) std :: cout << vect [ index ] << ' ' ; std :: cout << '\\n' ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 10 9 8 7 6 5 deque \u7c7b\uff08\u8bfb\u4f5cdeck\uff09\u662f\u4e00\u4e2a\u53cc\u7aef\u961f\u5217\u5bb9\u5668\u7c7b\uff0c\u5b83\u4f7f\u7528\u52a8\u6001\u6570\u7ec4\u5b9e\u73b0\uff0c\u652f\u6301\u5411\u524d\u9762\u6216\u8005\u540e\u9762\u63d2\u5165\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: deque < int > deq ; for ( int count = 0 ; count < 3 ; ++ count ) { deq . push_back ( count ); // insert at end of array deq . push_front ( 10 - count ); // insert at front of array } for ( int index = 0 ; index < deq . size (); ++ index ) std :: cout << deq [ index ] << ' ' ; std :: cout << '\\n' ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 8 9 10 0 1 2 list \u662f\u4e00\u79cd\u7279\u6b8a\u7684\u987a\u5e8f\u5bb9\u5668\uff0c\u79f0\u4e3a\u53cc\u5411\u94fe\u8868\u3002\u5bb9\u5668\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u901a\u8fc7\u6307\u9488\u6307\u5411\u524d\u4e00\u4e2a\u5143\u7d20\u548c\u540e\u4e00\u4e2a\u5143\u7d20\u3002\u94fe\u8868\u53ea\u63d0\u4f9b\u4e86\u4ece\u7b2c\u4e00\u4e2a\u5143\u7d20\u6216\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u8bbf\u95ee\u5bb9\u5668\u7684\u65b9\u6cd5\u2014\u2014\u5373\u4e0d\u652f\u6301\u5143\u7d20\u7684\u968f\u673a\u8bbf\u95ee\u3002\u5982\u679c\u4f60\u9700\u8981\u8bbf\u95ee\u4e2d\u95f4\u4f4d\u7f6e\u7684\u5143\u7d20\uff0c\u4f60\u5fc5\u987b\u904d\u5386\u94fe\u8868\u4ee5\u627e\u5230\u4f60\u9700\u8981\u8bbf\u95ee\u7684\u5143\u7d20\u3002\u94fe\u8868\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u652f\u6301\u5feb\u901f\u63d2\u5165\u5143\u7d20\uff08\u5982\u679c\u4f60\u77e5\u9053\u8981\u5411\u54ea\u91cc\u63d2\u5165\u7684\u8bdd\uff09\u3002\u901a\u5e38\u6211\u4eec\u4f1a\u4f7f\u7528\u8fed\u4ee3\u5668\u904d\u5386\u94fe\u8868\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u94fe\u8868\u548c\u8fed\u4ee3\u5668\u3002 \u5c3d\u7ba1 STL \u4e2d\u7684 string (\u548c wstring )\u901a\u5e38\u4e0d\u88ab\u770b\u505a\u662f\u4e00\u79cd\u987a\u5e8f\u5bb9\u5668\uff0c\u4f46\u4ece\u672c\u8d28\u4e0a\u8bb2\u5b83\u662f\u7684\u3002\u4f60\u53ef\u4ee5\u5c06\u5b83\u770b\u505a\u662f\u5143\u7d20\u7c7b\u578b\u4e3achar\u6216wchar\u7684vector\u3002 \u5173\u8054\u5bb9\u5668 \u5173\u8054\u5bb9\u5668\u4f1a\u5728\u65b0\u5143\u7d20\u63d2\u5165\u65f6\u81ea\u52a8\u6392\u5e8f\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5173\u8054\u5bb9\u5668\u4f1a\u4f7f\u7528 < \u5bf9\u5143\u7d20\u8fdb\u884c\u6bd4\u8f83\u3002 set \u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u653e\u552f\u4e00\u5143\u7d20\u7684\u5bb9\u5668\uff0c\u91cd\u590d\u7684\u5143\u7d20\u662f\u4e0d\u88ab\u5141\u8bb8\u7684\u3002\u5143\u7d20\u4f1a\u6309\u7167\u5b83\u4eec\u7684\u503c\u8fdb\u884c\u6392\u5e8f\uff1b multiset \u662f\u4e00\u79cd\u5141\u8bb8\u91cd\u590d\u5143\u7d20\u7684\u96c6\u5408\uff1b map (\u4e5f\u79f0\u4e3a\u5173\u8054\u6570\u7ec4) \u662f\u4e00\u4e2a\u5305\u542b\u5143\u7d20\u5bf9\u7684\u96c6\u5408\uff0c\u79f0\u4e3a\u952e\u503c\u5bf9\u3002\u5176\u4e2d\u952e\u7528\u6765\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u548c\u7d22\u5f15\uff0c\u800c\u4e14\u952e\u5fc5\u987b\u662f\u552f\u4e00\u7684\u3002\u5176\u4e2d\u503c\u662f\u5b9e\u9645\u6570\u636e\uff1b multimap (\u4e5f\u79f0\u4e3a\u5b57\u5178)\u662f\u4e00\u79cd\u6620\u5c04\uff0c\u5b83\u5141\u8bb8\u91cd\u590d\u7684\u952e\u3002\u5728\u5b9e\u9645\u751f\u6d3b\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u79cd multimaps \uff1a\u952e\u662f\u8981\u67e5\u7684\u8bcd\uff0c\u800c\u503c\u5219\u662f\u8bcd\u7684\u610f\u601d\u3002\u6240\u6709\u7684\u952e\u5747\u662f\u6309\u5347\u5e8f\u6392\u5217\u7684\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u952e\u6765\u67e5\u503c\u3002\u6709\u4e9b\u8bcd\u6709\u591a\u4e2a\u542b\u4e49\uff0c\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5b57\u5178\u662f\u4e00\u4e2a multimap \u800c\u4e0d\u662f map \u3002 \u5bb9\u5668\u9002\u914d\u5668 \u5bb9\u5668\u9002\u914d\u5668\u662f\u7528\u4e8e\u7279\u5b9a\u7528\u9014\u7684\u7279\u6b8a\u9884\u5b9a\u4e49\u5bb9\u5668\u3002\u5bb9\u5668\u9002\u914d\u5668\u7684\u6709\u8da3\u4e4b\u5904\u5728\u4e8e\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u5b83\u4eec\u4f7f\u7528\u7684 \u987a\u5e8f\u5bb9\u5668 \u3002 stack \u5bb9\u5668\u652f\u6301\u5143\u7d20\u7684 LIFO \uff08\u540e\u5165\u5148\u51fa\uff09\u3002\u5143\u7d20\u4ece\u5bb9\u5668\u7684\u672b\u7aef\u88ab\u538b\u5165(push)\u6216\u5f39\u51fa(pop)\u3002 stack \u9ed8\u8ba4\u4f7f\u7528 deque \u4f5c\u4e3a\u987a\u5e8f\u5bb9\u5668\uff08\u770b\u4e0a\u53bb\u591a\u5c11\u6709\u70b9\u5947\u602a\uff0c\u6bd5\u7adf\u611f\u89c9 vector \u66f4\u5408\u9002\uff09\uff0c\u4f46\u662f\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 vector \u6216\u94fe\u8868\uff1b queue \u5bb9\u5668\u652f\u6301\u5143\u7d20\u7684 FIFO\uff08\u5148\u5165\u5148\u51fa\uff09\u3002\u5143\u7d20\u4ece\u5bb9\u5668\u7684\u5c3e\u90e8\u63d2\u5165\uff0c\u7136\u540e\u4ece\u5934\u90e8\u5f39\u51fa\u3002 queue \u9ed8\u8ba4\u4f7f\u7528 deque \u4f5c\u4e3a\u987a\u5e8f\u5bb9\u5668\uff0c\u4f46\u662f\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u94fe\u8868\uff1b priority queue \u662f\u4e00\u79cd\u59cb\u7ec8\u4fdd\u6301\u5143\u7d20\u4e3a\u6392\u5e8f\u72b6\u6001\uff08\u901a\u8fc7 < \u8fd0\u7b97\u7b26\uff09\u7684\u961f\u5217\u3002\u5f53\u5143\u7d20\u88ab\u538b\u5165\u4f18\u5148\u7ea7\u961f\u5217\u65f6\uff0c\u5143\u7d20\u4f1a\u5728\u961f\u5217\u4e2d\u8fdb\u884c\u6392\u5e8f\u3002\u5220\u9664\u65f6\uff0c\u5219\u4ece\u961f\u9996\u79fb\u9664\u5143\u7d20\uff08\u5f53\u524d\u4f18\u5148\u7ea7\u6700\u9ad8\u7684\u5143\u7d20\uff09\u3002","title":"21.2 - STL\u5bb9\u5668"},{"location":"21-2-STL-containers-overview/#_1","text":"\u987a\u5e8f\u5bb9\u5668\u662f\u6307\u90a3\u4e9b\u5728\u5bb9\u5668\u4e2d\u6309\u987a\u5e8f\u5b58\u653e\u5143\u7d20\u7684\u5bb9\u5668\u7c7b\u3002\u987a\u5e8f\u5bb9\u5668\u7684\u4e00\u5927\u7279\u70b9\u5c31\u662f\u4f60\u53ef\u4ee5\u51b3\u5b9a\u5728\u54ea\u4e2a\u4f4d\u7f6e\u63d2\u5165\u5143\u7d20\u3002\u6700\u5e38\u89c1\u7684\u987a\u5e8f\u5bb9\u5668\u7684\u4f8b\u5b50\u5c31\u662f\u6570\u7ec4\uff1a\u5982\u679c\u4f60\u9700\u8981\u5728\u6570\u7ec4\u7684\u67d0\u4e2a\u4f4d\u7f6e\u63d2\u5165\u5143\u7d20\uff0c\u90a3\u4e48\u8be5\u5143\u7d20\u4e00\u5b9a\u4f1a\u88ab\u63d2\u5165\u5230\u8fd9\u4e2a\u4f4d\u7f6e\u3002 \u5bf9\u4e8eC++11\u6765\u8bf4\uff0cSTL\u5305\u542b\u4e866\u79cd\u987a\u5e8f\u5bb9\u5668\uff1a std::vector std::deque std::array std::list std::forward_list std::basic_string \u5982\u679c\u4f60\u5b66\u8fc7\u7269\u7406\u7684\u8bdd\uff0c\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3avector\u8868\u793a\u7684\u5177\u6709\u5927\u5c0f\u548c\u65b9\u5411\u7684\u5411\u91cf\u3002\u4e0d\u8fc7\uff0cSTL\u4e2d\u7684 vector \u53ef\u4e0d\u662f\u5411\u91cf\uff0c\u5b83\u662f\u4e00\u4e2a\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u5927\u5c0f\u7684\u52a8\u6001\u6570\u7ec4\u3002vector \u5bb9\u5668\u7c7b\u652f\u6301\u901a\u8fc7\u4e0b\u6807\u8fd0\u7b97\u7b26\u8fdb\u884c\u968f\u673a\u8bbf\u95ee\uff0c\u4e5f\u652f\u6301\u50cf\u5c3e\u90e8\u5feb\u901f\u63d2\u5165\u5143\u7d20\u3002 \u4e0b\u9762\u7684\u4ee3\u7801\u5411 vector \u4e2d\u63d2\u5165\u4e86 6 \u4e2a\u6570\u5b57\uff0c\u5e76\u4e14\u4f7f\u7528\u4e0b\u6807\u8fd0\u7b97\u7b26\u4f9d\u6b21\u8bbf\u95ee\u5e76\u6253\u5370\u5143\u7d20\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int main () { std :: vector < int > vect ; for ( int count = 0 ; count < 6 ; ++ count ) vect . push_back ( 10 - count ); // insert at end of array for ( int index = 0 ; index < vect . size (); ++ index ) std :: cout << vect [ index ] << ' ' ; std :: cout << '\\n' ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 10 9 8 7 6 5 deque \u7c7b\uff08\u8bfb\u4f5cdeck\uff09\u662f\u4e00\u4e2a\u53cc\u7aef\u961f\u5217\u5bb9\u5668\u7c7b\uff0c\u5b83\u4f7f\u7528\u52a8\u6001\u6570\u7ec4\u5b9e\u73b0\uff0c\u652f\u6301\u5411\u524d\u9762\u6216\u8005\u540e\u9762\u63d2\u5165\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: deque < int > deq ; for ( int count = 0 ; count < 3 ; ++ count ) { deq . push_back ( count ); // insert at end of array deq . push_front ( 10 - count ); // insert at front of array } for ( int index = 0 ; index < deq . size (); ++ index ) std :: cout << deq [ index ] << ' ' ; std :: cout << '\\n' ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 8 9 10 0 1 2 list \u662f\u4e00\u79cd\u7279\u6b8a\u7684\u987a\u5e8f\u5bb9\u5668\uff0c\u79f0\u4e3a\u53cc\u5411\u94fe\u8868\u3002\u5bb9\u5668\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u901a\u8fc7\u6307\u9488\u6307\u5411\u524d\u4e00\u4e2a\u5143\u7d20\u548c\u540e\u4e00\u4e2a\u5143\u7d20\u3002\u94fe\u8868\u53ea\u63d0\u4f9b\u4e86\u4ece\u7b2c\u4e00\u4e2a\u5143\u7d20\u6216\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u8bbf\u95ee\u5bb9\u5668\u7684\u65b9\u6cd5\u2014\u2014\u5373\u4e0d\u652f\u6301\u5143\u7d20\u7684\u968f\u673a\u8bbf\u95ee\u3002\u5982\u679c\u4f60\u9700\u8981\u8bbf\u95ee\u4e2d\u95f4\u4f4d\u7f6e\u7684\u5143\u7d20\uff0c\u4f60\u5fc5\u987b\u904d\u5386\u94fe\u8868\u4ee5\u627e\u5230\u4f60\u9700\u8981\u8bbf\u95ee\u7684\u5143\u7d20\u3002\u94fe\u8868\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u652f\u6301\u5feb\u901f\u63d2\u5165\u5143\u7d20\uff08\u5982\u679c\u4f60\u77e5\u9053\u8981\u5411\u54ea\u91cc\u63d2\u5165\u7684\u8bdd\uff09\u3002\u901a\u5e38\u6211\u4eec\u4f1a\u4f7f\u7528\u8fed\u4ee3\u5668\u904d\u5386\u94fe\u8868\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u94fe\u8868\u548c\u8fed\u4ee3\u5668\u3002 \u5c3d\u7ba1 STL \u4e2d\u7684 string (\u548c wstring )\u901a\u5e38\u4e0d\u88ab\u770b\u505a\u662f\u4e00\u79cd\u987a\u5e8f\u5bb9\u5668\uff0c\u4f46\u4ece\u672c\u8d28\u4e0a\u8bb2\u5b83\u662f\u7684\u3002\u4f60\u53ef\u4ee5\u5c06\u5b83\u770b\u505a\u662f\u5143\u7d20\u7c7b\u578b\u4e3achar\u6216wchar\u7684vector\u3002","title":"\u987a\u5e8f\u5bb9\u5668"},{"location":"21-2-STL-containers-overview/#_2","text":"\u5173\u8054\u5bb9\u5668\u4f1a\u5728\u65b0\u5143\u7d20\u63d2\u5165\u65f6\u81ea\u52a8\u6392\u5e8f\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5173\u8054\u5bb9\u5668\u4f1a\u4f7f\u7528 < \u5bf9\u5143\u7d20\u8fdb\u884c\u6bd4\u8f83\u3002 set \u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u653e\u552f\u4e00\u5143\u7d20\u7684\u5bb9\u5668\uff0c\u91cd\u590d\u7684\u5143\u7d20\u662f\u4e0d\u88ab\u5141\u8bb8\u7684\u3002\u5143\u7d20\u4f1a\u6309\u7167\u5b83\u4eec\u7684\u503c\u8fdb\u884c\u6392\u5e8f\uff1b multiset \u662f\u4e00\u79cd\u5141\u8bb8\u91cd\u590d\u5143\u7d20\u7684\u96c6\u5408\uff1b map (\u4e5f\u79f0\u4e3a\u5173\u8054\u6570\u7ec4) \u662f\u4e00\u4e2a\u5305\u542b\u5143\u7d20\u5bf9\u7684\u96c6\u5408\uff0c\u79f0\u4e3a\u952e\u503c\u5bf9\u3002\u5176\u4e2d\u952e\u7528\u6765\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u548c\u7d22\u5f15\uff0c\u800c\u4e14\u952e\u5fc5\u987b\u662f\u552f\u4e00\u7684\u3002\u5176\u4e2d\u503c\u662f\u5b9e\u9645\u6570\u636e\uff1b multimap (\u4e5f\u79f0\u4e3a\u5b57\u5178)\u662f\u4e00\u79cd\u6620\u5c04\uff0c\u5b83\u5141\u8bb8\u91cd\u590d\u7684\u952e\u3002\u5728\u5b9e\u9645\u751f\u6d3b\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u79cd multimaps \uff1a\u952e\u662f\u8981\u67e5\u7684\u8bcd\uff0c\u800c\u503c\u5219\u662f\u8bcd\u7684\u610f\u601d\u3002\u6240\u6709\u7684\u952e\u5747\u662f\u6309\u5347\u5e8f\u6392\u5217\u7684\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u952e\u6765\u67e5\u503c\u3002\u6709\u4e9b\u8bcd\u6709\u591a\u4e2a\u542b\u4e49\uff0c\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5b57\u5178\u662f\u4e00\u4e2a multimap \u800c\u4e0d\u662f map \u3002","title":"\u5173\u8054\u5bb9\u5668"},{"location":"21-2-STL-containers-overview/#_3","text":"\u5bb9\u5668\u9002\u914d\u5668\u662f\u7528\u4e8e\u7279\u5b9a\u7528\u9014\u7684\u7279\u6b8a\u9884\u5b9a\u4e49\u5bb9\u5668\u3002\u5bb9\u5668\u9002\u914d\u5668\u7684\u6709\u8da3\u4e4b\u5904\u5728\u4e8e\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u5b83\u4eec\u4f7f\u7528\u7684 \u987a\u5e8f\u5bb9\u5668 \u3002 stack \u5bb9\u5668\u652f\u6301\u5143\u7d20\u7684 LIFO \uff08\u540e\u5165\u5148\u51fa\uff09\u3002\u5143\u7d20\u4ece\u5bb9\u5668\u7684\u672b\u7aef\u88ab\u538b\u5165(push)\u6216\u5f39\u51fa(pop)\u3002 stack \u9ed8\u8ba4\u4f7f\u7528 deque \u4f5c\u4e3a\u987a\u5e8f\u5bb9\u5668\uff08\u770b\u4e0a\u53bb\u591a\u5c11\u6709\u70b9\u5947\u602a\uff0c\u6bd5\u7adf\u611f\u89c9 vector \u66f4\u5408\u9002\uff09\uff0c\u4f46\u662f\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 vector \u6216\u94fe\u8868\uff1b queue \u5bb9\u5668\u652f\u6301\u5143\u7d20\u7684 FIFO\uff08\u5148\u5165\u5148\u51fa\uff09\u3002\u5143\u7d20\u4ece\u5bb9\u5668\u7684\u5c3e\u90e8\u63d2\u5165\uff0c\u7136\u540e\u4ece\u5934\u90e8\u5f39\u51fa\u3002 queue \u9ed8\u8ba4\u4f7f\u7528 deque \u4f5c\u4e3a\u987a\u5e8f\u5bb9\u5668\uff0c\u4f46\u662f\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u94fe\u8868\uff1b priority queue \u662f\u4e00\u79cd\u59cb\u7ec8\u4fdd\u6301\u5143\u7d20\u4e3a\u6392\u5e8f\u72b6\u6001\uff08\u901a\u8fc7 < \u8fd0\u7b97\u7b26\uff09\u7684\u961f\u5217\u3002\u5f53\u5143\u7d20\u88ab\u538b\u5165\u4f18\u5148\u7ea7\u961f\u5217\u65f6\uff0c\u5143\u7d20\u4f1a\u5728\u961f\u5217\u4e2d\u8fdb\u884c\u6392\u5e8f\u3002\u5220\u9664\u65f6\uff0c\u5219\u4ece\u961f\u9996\u79fb\u9664\u5143\u7d20\uff08\u5f53\u524d\u4f18\u5148\u7ea7\u6700\u9ad8\u7684\u5143\u7d20\uff09\u3002","title":"\u5bb9\u5668\u9002\u914d\u5668"},{"location":"21-3-STL-iterators-overview/","tags":["stl","containers"],"text":"Key Takeaway \u8fed\u4ee3\u5668 \u662f\u4e00\u4e2a\u53ef\u4ee5\u904d\u5386 \u5bb9\u5668\u7c7b \u7684\u5bf9\u8c61\uff0c\u4f7f\u7528\u5b83\u904d\u5386\u5bb9\u5668\u65f6\uff0c\u4f7f\u7528\u8005\u65e0\u9700\u77e5\u9053\u5bb9\u5668\u7684\u5b9e\u73b0\u7ec6\u8282\u3002\u5bf9\u4e8e\u5f88\u591a\u7c7b\u6765\u8bf4\uff08\u5c24\u5176\u662f\u94fe\u8868\u6216\u5173\u8054\u5bb9\u5668\uff09\uff0c\u8fed\u4ee3\u5668\u662f\u662f\u8bbf\u95ee\u5176\u5143\u7d20\u7684\u9996\u9009\u65b9\u5f0f\u3002 \u8fed\u4ee3\u5668\u53ef\u4ee5\u88ab\u5f62\u8c61\u5730\u770b\u505a\u662f\u4e00\u4e2a\u6307\u5411\u5bb9\u5668\u5143\u7d20\u7684\u6307\u9488\uff0c\u540c\u65f6\u5b83\u5177\u6709\u4e00\u7cfb\u5217\u88ab\u91cd\u8f7d\u7684\u8fd0\u7b97\u7b26\uff1a Operator* -- \u8fed\u4ee3\u5668\u7684 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \uff0c\u5b83\u53ef\u4ee5\u8fd4\u56de\u8fed\u4ee3\u5668\u5f53\u524d\u6240\u6307\u7684\u5143\u7d20\uff1b Operator++ -- \u5c06\u8fed\u4ee3\u5668\u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002\u5f88\u591a\u8fed\u4ee3\u5668\u4e5f\u63d0\u4f9b\u4e86 Operator-- \u7528\u4e8e\u5c06\u8fed\u4ee3\u5668\u79fb\u52a8\u5230\u4e0a\u4e00\u4e2a\u5143\u7d20\uff1b Operator== \u548c Operator!= -- \u57fa\u672c\u7684\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff0c\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u8fed\u4ee3\u5668\u6240\u6307\u7684\u5143\u7d20\u662f\u5426\u76f8\u540c\u3002\u5982\u679c\u9700\u8981\u6bd4\u8f83\u4e24\u4e2a\u8fed\u4ee3\u5668\u6240\u6307\u5143\u7d20\u7684\u503c\u662f\u5426\u76f8\u540c\uff0c\u5219\u9700\u8981\u5148\u5bf9\u8fed\u4ee3\u5668\u89e3\u5f15\u7528\uff0c\u7136\u540e\u5728\u6bd4\u8f83\uff1b Operator= -- \u4e3a\u8fed\u4ee3\u5668\u8d4b\u503c\u4e00\u4e2a\u65b0\u7684\u4f4d\u7f6e\uff08\u4e00\u822c\u6765\u8bf4\u662f\u5bb9\u5668\u7684\u8d77\u70b9\u6216\u672b\u5c3e\u5143\u7d20\uff09\u3002\u5982\u679c\u9700\u8981\u7ed9\u8fed\u4ee3\u5668\u6240\u6307\u7684\u5143\u7d20\u8d4b\u503c\uff0c\u5219\u9700\u8981\u5148\u5bf9\u8fed\u4ee3\u5668\u89e3\u5f15\u7528\u7136\u540e\u518d\u8d4b\u503c\u3002 \u6bcf\u4e2a\u5bb9\u5668\u90fd\u63d0\u4f9b\u4e86\u56db\u4e2a\u57fa\u672c\u7684\u6210\u5458\u51fd\u6570\uff0c\u7528\u4e8e\u914d\u5408\u8d4b\u503c\u53f7\u4f7f\u7528\uff1a begin() \uff1a\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u5bb9\u5668\u8d77\u59cb\u5143\u7d20\u7684\u8fed\u4ee3\u5668\uff1b end() \uff1a\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u5bb9\u5668\u672b\u5c3e\u7684\u8fed\u4ee3\u5668\uff08\u5176\u524d\u4e00\u4e2a\u5143\u7d20\u4e3a\u5bb9\u5668\u4e2d\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff09\uff1b cbegin() \uff1a\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u5bb9\u5668\u8d77\u59cb\u5143\u7d20\u7684const\u8fed\u4ee3\u5668\uff1b cend() \uff1a\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u5bb9\u5668\u672b\u5c3e\u7684const\u8fed\u4ee3\u5668\uff08\u5176\u524d\u4e00\u4e2a\u5143\u7d20\u4e3a\u5bb9\u5668\u4e2d\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff09\u3002 \u867d\u7136 end() \u6307\u5411\u7684\u4e0d\u662f\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u800c\u662f\u5176\u540e\u9762\u4e00\u4e2a\u4f4d\u7f6e\u6709\u70b9\u5947\u602a\uff0c\u4f46\u5176\u5b9e\u8fd9\u4e48\u505a\u53ef\u4ee5\u7b80\u5316\u5faa\u73af\uff1a\u8fed\u4ee3\u53ef\u4ee5\u4e00\u76f4\u8fdb\u884c\uff0c\u76f4\u5230\u8fed\u4ee3\u5668\u5230\u8fbe end() \uff0c\u6b64\u65f6\u6211\u4eec\u5c31\u77e5\u9053\u904d\u5386\u7ed3\u675f\u4e86\u3002 \u6700\u540e\uff0c\u6240\u6709\u7684\u5bb9\u5668\u90fd\u63d0\u4f9b\u4e86\uff08\u81f3\u5c11\uff09\u4e24\u79cd\u7c7b\u578b\u7684\u8fed\u4ee3\u5668\uff1a container::iterator \uff1a\u53ef\u4ee5\u8bfb\u5199\u7684\u8fed\u4ee3\u5668 container::const_iterator \uff1a \u53ea\u8bfb\u8fed\u4ee3\u5668 \u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u770b\u51e0\u4e2a\u4f7f\u7528\u8fed\u4ee3\u5668\u7684\u5b9e\u4f8b\u5427\u3002 \u904d\u5386 vector 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include int main () { std :: vector < int > vect ; for ( int count = 0 ; count < 6 ; ++ count ) vect . push_back ( count ); std :: vector < int >:: const_iterator it ; // \u58f0\u660e\u4e00\u4e2a\u53ea\u8bfb\u8fed\u4ee3\u5668 it = vect . cbegin (); // \u5c06\u8fed\u4ee3\u5668\u8d4b\u503c\u4e3a vector \u7684\u8d77\u70b9 while ( it != vect . cend ()) // \u5982\u679c it \u8fd8\u6ca1\u6709\u5230\u8fbe\u5bb9\u5668\u7684\u672b\u5c3e { std :: cout << * it << ' ' ; // \u6253\u5370 it \u6240\u6307\u7684\u5143\u7d20 ++ it ; // \u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5143\u7d20 } std :: cout << '\\n' ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 0 1 2 3 4 5 \u904d\u5386\u5217\u8868 \u904d\u5386\u5217\u8868\u4e5f\u662f\u7c7b\u4f3c\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include int main () { std :: list < int > li ; for ( int count = 0 ; count < 6 ; ++ count ) li . push_back ( count ); std :: list < int >:: const_iterator it ; // declare an iterator it = li . cbegin (); // assign it to the start of the list while ( it != li . cend ()) // while it hasn't reach the end { std :: cout << * it << ' ' ; // print the value of the element it points to ++ it ; // and iterate to the next element } std :: cout << '\\n' ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 0 1 2 3 4 5 \u53ef\u4ee5\u770b\u5230\uff0c\u5c3d\u7ba1 vector \u548c\u94fe\u8868\u7684\u5177\u4f53\u5b9e\u73b0\u622a\u7136\u4e0d\u540c\uff0c\u4f46\u662f\u904d\u5386\u5b83\u4eec\u7684\u4ee3\u7801\u5374\u51e0\u4e4e\u5b8c\u5168\u4e00\u6837\uff01 \u904d\u5386\u96c6\u5408 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u5305\u542b6\u4e2a\u6570\u7684\u96c6\u5408\uff0c\u7136\u540e\u518d\u4f7f\u7528\u8fed\u4ee3\u5668\u4f9d\u6b21\u6253\u5370\u5b83\u4eec\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include int main () { std :: set < int > myset ; myset . insert ( 7 ); myset . insert ( 2 ); myset . insert ( -6 ); myset . insert ( 8 ); myset . insert ( 1 ); myset . insert ( -4 ); std :: set < int >:: const_iterator it ; // declare an iterator it = myset . cbegin (); // assign it to the start of the set while ( it != myset . cend ()) // while it hasn't reach the end { std :: cout << * it << ' ' ; // print the value of the element it points to ++ it ; // and iterate to the next element } std :: cout << '\\n' ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 -6 -4 1 2 7 8 \u4ece\u8fd9\u4e2a\u4f8b\u5b50\u53ef\u4ee5\u770b\u51fa\uff0c\u867d\u7136\u586b\u5145\u96c6\u5408\u7684\u65b9\u6cd5\u548c\u586b\u5145vector\u4ee5\u53ca\u94fe\u8868\u5b8c\u5168\u4e0d\u540c\uff0c\u4f46\u662f\u4f7f\u7528\u8fed\u4ee3\u5668\u904d\u5386\u5b83\u4eec\u7684\u65b9\u6cd5\u5374\u662f\u4e00\u81f4\u7684\u3002 \u904d\u5386\u6620\u5c04 \u904d\u5386\u6620\u5c04\u7a0d\u5fae\u590d\u6742\u4e9b\u3002 Maps \u548c multimaps \u9700\u8981\u586b\u5145\u5143\u7d20\u5bf9(\u5b9a\u4e49\u4e3a std::pair )\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 make_pair() \u51fd\u6570\u5f88\u65b9\u4fbf\u7684\u521b\u5efa\u5143\u7d20\u5bf9\u3002 std::pair \u5141\u8bb8\u6211\u4eec\u901a\u8fc7 first \u548c second \u4e24\u4e2a\u6210\u5458\u53d8\u91cf\u8bbf\u95ee\u5143\u7d20\u5bf9\u3002 first \u662f\u952e\uff0c second \u662f\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include #include int main () { std :: map < int , std :: string > mymap ; mymap . insert ( std :: make_pair ( 4 , \"apple\" )); mymap . insert ( std :: make_pair ( 2 , \"orange\" )); mymap . insert ( std :: make_pair ( 1 , \"banana\" )); mymap . insert ( std :: make_pair ( 3 , \"grapes\" )); mymap . insert ( std :: make_pair ( 6 , \"mango\" )); mymap . insert ( std :: make_pair ( 5 , \"peach\" )); auto it { mymap . cbegin () }; // declare a const iterator and assign to start of vector while ( it != mymap . cend ()) // while it hasn't reach the end { std :: cout << it -> first << '=' << it -> second << ' ' ; // the value of the element it points to ++ it ; // and iterate to the next element } std :: cout << '\\n' ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 1=banana 2=orange 3=grapes 4=apple 5=peach 6=mango \u53ef\u4ee5\u770b\u5230\uff0c\u8fed\u4ee3\u5668\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u4f9d\u6b21\u904d\u5386\u5bb9\u5668\u4e2d\u7684\u5404\u4e2a\u5143\u7d20\uff0c\u6211\u4eec\u5b8c\u5168\u65e0\u9700\u64cd\u5fc3 map \u7a76\u7adf\u662f\u5982\u4f55\u5b58\u50a8\u8fd9\u4e9b\u5143\u7d20\u7684\uff01 \u5c0f\u7ed3 \u8fed\u4ee3\u5668\u63d0\u4f9b\u4e86\u4e00\u79cd\u65e0\u9700\u77e5\u9053\u5b9e\u73b0\u7ec6\u8282\u5c31\u53ef\u4ee5\u8f7b\u677e\u904d\u5386 \u5bb9\u5668\u7c7b \u7684\u65b9\u6cd5\u3002\u7ed3\u5408STL\u7b97\u6cd5\u548c\u5bb9\u5668\u7c7b\u7684\u6210\u5458\u51fd\u6570\uff0c\u8fed\u4ee3\u5668\u4f1a\u662f\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\u3002\u5728\u4e0b\u8282\u8bfe\u4e2d\uff0c\u4f60\u5c06\u4f1a\u770b\u5230\u5982\u4f55\u4f7f\u7528\u8fed\u4ee3\u5668\u5411\u94fe\u8868\u63d2\u5165\u5143\u7d20\uff08\u94fe\u8868\u6ca1\u6709\u63d0\u4f9b\u4e0b\u6807\u8fd0\u7b97\u7b26\u6240\u4ee5\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u5143\u7d20\uff09\u3002 \u8fd8\u6709\u4e00\u4ef6\u4e8b\u503c\u5f97\u6ce8\u610f\uff1a\u8fed\u4ee3\u5668\u5fc5\u987b\u88ab\u5b9e\u73b0\u4e3a\u7c7b\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u77e5\u9053\u7c7b\u7684\u5b9e\u73b0\u7ec6\u8282\u3002\u56e0\u6b64\uff0c\u8fed\u4ee3\u5668\u603b\u662f\u548c\u7279\u5b9a\u7684\u5bb9\u5668\u7c7b\u7ed1\u5b9a\u7684\u3002","title":"21.3 - STL\u8fed\u4ee3\u5668"},{"location":"21-3-STL-iterators-overview/#vector","text":"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include int main () { std :: vector < int > vect ; for ( int count = 0 ; count < 6 ; ++ count ) vect . push_back ( count ); std :: vector < int >:: const_iterator it ; // \u58f0\u660e\u4e00\u4e2a\u53ea\u8bfb\u8fed\u4ee3\u5668 it = vect . cbegin (); // \u5c06\u8fed\u4ee3\u5668\u8d4b\u503c\u4e3a vector \u7684\u8d77\u70b9 while ( it != vect . cend ()) // \u5982\u679c it \u8fd8\u6ca1\u6709\u5230\u8fbe\u5bb9\u5668\u7684\u672b\u5c3e { std :: cout << * it << ' ' ; // \u6253\u5370 it \u6240\u6307\u7684\u5143\u7d20 ++ it ; // \u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5143\u7d20 } std :: cout << '\\n' ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 0 1 2 3 4 5","title":"\u904d\u5386 vector"},{"location":"21-3-STL-iterators-overview/#_1","text":"\u904d\u5386\u5217\u8868\u4e5f\u662f\u7c7b\u4f3c\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include int main () { std :: list < int > li ; for ( int count = 0 ; count < 6 ; ++ count ) li . push_back ( count ); std :: list < int >:: const_iterator it ; // declare an iterator it = li . cbegin (); // assign it to the start of the list while ( it != li . cend ()) // while it hasn't reach the end { std :: cout << * it << ' ' ; // print the value of the element it points to ++ it ; // and iterate to the next element } std :: cout << '\\n' ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 0 1 2 3 4 5 \u53ef\u4ee5\u770b\u5230\uff0c\u5c3d\u7ba1 vector \u548c\u94fe\u8868\u7684\u5177\u4f53\u5b9e\u73b0\u622a\u7136\u4e0d\u540c\uff0c\u4f46\u662f\u904d\u5386\u5b83\u4eec\u7684\u4ee3\u7801\u5374\u51e0\u4e4e\u5b8c\u5168\u4e00\u6837\uff01","title":"\u904d\u5386\u5217\u8868"},{"location":"21-3-STL-iterators-overview/#_2","text":"\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u5305\u542b6\u4e2a\u6570\u7684\u96c6\u5408\uff0c\u7136\u540e\u518d\u4f7f\u7528\u8fed\u4ee3\u5668\u4f9d\u6b21\u6253\u5370\u5b83\u4eec\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include int main () { std :: set < int > myset ; myset . insert ( 7 ); myset . insert ( 2 ); myset . insert ( -6 ); myset . insert ( 8 ); myset . insert ( 1 ); myset . insert ( -4 ); std :: set < int >:: const_iterator it ; // declare an iterator it = myset . cbegin (); // assign it to the start of the set while ( it != myset . cend ()) // while it hasn't reach the end { std :: cout << * it << ' ' ; // print the value of the element it points to ++ it ; // and iterate to the next element } std :: cout << '\\n' ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 -6 -4 1 2 7 8 \u4ece\u8fd9\u4e2a\u4f8b\u5b50\u53ef\u4ee5\u770b\u51fa\uff0c\u867d\u7136\u586b\u5145\u96c6\u5408\u7684\u65b9\u6cd5\u548c\u586b\u5145vector\u4ee5\u53ca\u94fe\u8868\u5b8c\u5168\u4e0d\u540c\uff0c\u4f46\u662f\u4f7f\u7528\u8fed\u4ee3\u5668\u904d\u5386\u5b83\u4eec\u7684\u65b9\u6cd5\u5374\u662f\u4e00\u81f4\u7684\u3002","title":"\u904d\u5386\u96c6\u5408"},{"location":"21-3-STL-iterators-overview/#_3","text":"\u904d\u5386\u6620\u5c04\u7a0d\u5fae\u590d\u6742\u4e9b\u3002 Maps \u548c multimaps \u9700\u8981\u586b\u5145\u5143\u7d20\u5bf9(\u5b9a\u4e49\u4e3a std::pair )\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 make_pair() \u51fd\u6570\u5f88\u65b9\u4fbf\u7684\u521b\u5efa\u5143\u7d20\u5bf9\u3002 std::pair \u5141\u8bb8\u6211\u4eec\u901a\u8fc7 first \u548c second \u4e24\u4e2a\u6210\u5458\u53d8\u91cf\u8bbf\u95ee\u5143\u7d20\u5bf9\u3002 first \u662f\u952e\uff0c second \u662f\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include #include int main () { std :: map < int , std :: string > mymap ; mymap . insert ( std :: make_pair ( 4 , \"apple\" )); mymap . insert ( std :: make_pair ( 2 , \"orange\" )); mymap . insert ( std :: make_pair ( 1 , \"banana\" )); mymap . insert ( std :: make_pair ( 3 , \"grapes\" )); mymap . insert ( std :: make_pair ( 6 , \"mango\" )); mymap . insert ( std :: make_pair ( 5 , \"peach\" )); auto it { mymap . cbegin () }; // declare a const iterator and assign to start of vector while ( it != mymap . cend ()) // while it hasn't reach the end { std :: cout << it -> first << '=' << it -> second << ' ' ; // the value of the element it points to ++ it ; // and iterate to the next element } std :: cout << '\\n' ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 1=banana 2=orange 3=grapes 4=apple 5=peach 6=mango \u53ef\u4ee5\u770b\u5230\uff0c\u8fed\u4ee3\u5668\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u4f9d\u6b21\u904d\u5386\u5bb9\u5668\u4e2d\u7684\u5404\u4e2a\u5143\u7d20\uff0c\u6211\u4eec\u5b8c\u5168\u65e0\u9700\u64cd\u5fc3 map \u7a76\u7adf\u662f\u5982\u4f55\u5b58\u50a8\u8fd9\u4e9b\u5143\u7d20\u7684\uff01","title":"\u904d\u5386\u6620\u5c04"},{"location":"21-3-STL-iterators-overview/#_4","text":"\u8fed\u4ee3\u5668\u63d0\u4f9b\u4e86\u4e00\u79cd\u65e0\u9700\u77e5\u9053\u5b9e\u73b0\u7ec6\u8282\u5c31\u53ef\u4ee5\u8f7b\u677e\u904d\u5386 \u5bb9\u5668\u7c7b \u7684\u65b9\u6cd5\u3002\u7ed3\u5408STL\u7b97\u6cd5\u548c\u5bb9\u5668\u7c7b\u7684\u6210\u5458\u51fd\u6570\uff0c\u8fed\u4ee3\u5668\u4f1a\u662f\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\u3002\u5728\u4e0b\u8282\u8bfe\u4e2d\uff0c\u4f60\u5c06\u4f1a\u770b\u5230\u5982\u4f55\u4f7f\u7528\u8fed\u4ee3\u5668\u5411\u94fe\u8868\u63d2\u5165\u5143\u7d20\uff08\u94fe\u8868\u6ca1\u6709\u63d0\u4f9b\u4e0b\u6807\u8fd0\u7b97\u7b26\u6240\u4ee5\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u5143\u7d20\uff09\u3002 \u8fd8\u6709\u4e00\u4ef6\u4e8b\u503c\u5f97\u6ce8\u610f\uff1a\u8fed\u4ee3\u5668\u5fc5\u987b\u88ab\u5b9e\u73b0\u4e3a\u7c7b\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u77e5\u9053\u7c7b\u7684\u5b9e\u73b0\u7ec6\u8282\u3002\u56e0\u6b64\uff0c\u8fed\u4ee3\u5668\u603b\u662f\u548c\u7279\u5b9a\u7684\u5bb9\u5668\u7c7b\u7ed1\u5b9a\u7684\u3002","title":"\u5c0f\u7ed3"},{"location":"21-4-STL-algorithms-overview/","tags":["stl","containers"],"text":"Key Takeaway \u7b97\u6cd5\u90fd\u662f\u914d\u5408\u8fed\u4ee3\u5668\u4f7f\u7528\u7684\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u4e00\u822c\u4e5f\u662f\u8fed\u4ee3\u5668 \u9664\u4e86 \u5bb9\u5668\u7c7b \u548c \u8fed\u4ee3\u5668 \uff0cSTL\u8fd8\u63d0\u4f9b\u4e86\u5f88\u591a \u6cdb\u578b\u7b97\u6cd5 \u6765\u914d\u5408\u5bb9\u5668\u7c7b\u4e2d\u7684\u5143\u7d20\u4f7f\u7528\uff0c\u5bf9\u5b83\u4eec\u8fdb\u884c\u641c\u7d22\u3001\u6392\u5e8f\u3001\u63d2\u5165\u3001\u91cd\u6392\u3001\u5220\u9664\u548c\u62f7\u8d1d\u3002 \u6ce8\u610f\uff0c\u8fd9\u4e9b\u7b97\u6cd5\u90fd\u88ab\u5b9e\u73b0\u4e3a\u4f7f\u7528\u8fed\u4ee3\u5668\u8fdb\u884c\u64cd\u4f5c\u7684\u51fd\u6570\u3002\u8fd9\u610f\u5473\u7740\u6bcf\u4e2a\u7b97\u6cd5\u90fd\u53ea\u9700\u8981\u5b9e\u73b0\u4e00\u6b21\uff0c\u5c31\u53ef\u4ee5\u914d\u5408\u6240\u6709\u63d0\u4f9b\u8fed\u4ee3\u5668\u7684\u5bb9\u5668\u4f7f\u7528\uff08\u5305\u62ec\u4f60\u81ea\u5b9a\u4e49\u7684\u7c7b\uff09\u3002\u5c3d\u7ba1\u8fd9\u4e9b\u7b97\u6cd5\u975e\u5e38\u5f3a\u5927\u800c\u4e14\u53ef\u4ee5\u65b9\u4fbf\u5730\u7f16\u5199\u590d\u6742\u7684\u4ee3\u7801\u903b\u8f91\uff0c\u4f46\u662f\u5b83\u4e5f\u6709\u7f3a\u70b9\uff1a\u67d0\u4e9b\u7b97\u6cd5\u548c\u5bb9\u5668\u7c7b\u578b\u7684\u7ec4\u5408\u53ef\u80fd\u65e0\u6cd5\u5de5\u4f5c\uff0c\u6709\u4e9b\u53ef\u80fd\u5bfc\u81f4\u6b7b\u5faa\u73af\uff0c\u6216\u8005\u6027\u80fd\u6781\u5dee\u3002\u6240\u4ee5\u5728\u4f7f\u7528\u65f6\u9700\u8981\u4f60\u81ea\u5df1\u8003\u8651\u6e05\u695a\u5e76\u627f\u62c5\u76f8\u5e94\u7684\u98ce\u9669\u3002 STL \u63d0\u4f9b\u4e86\u975e\u5e38\u591a\u7684\u7b97\u6cd5\u2014\u2014\u6211\u4eec\u53ea\u4f1a\u5728\u6b64\u4ecb\u7ecd\u4e00\u4e9b\u975e\u5e38\u5e38\u89c1\u7684\u3002\u5269\u4e0b\u7684\u7b97\u6cd5\u4f1a\u5728STL\u7b97\u6cd5\u7ae0\u8282\u4e2d\u8fdb\u884c\u4ecb\u7ecd \u4f7f\u7528STL\u7b97\u6cd5\uff0c\u53ea\u9700\u8981\u6dfb\u52a0algorithm\u5934\u6587\u4ef6\u5373\u53ef\u3002 min_element \u548c max_element std::min_element \u548c std::max_element \u7b97\u6cd5\u7528\u4e8e\u67e5\u627e\u5bb9\u5668 \u5bb9\u5668\u7c7b \u4e2d\u7684\u6700\u5927\u5143\u7d20\u548c\u6700\u5c0f\u5143\u7d20\u3002 std::iota \u53ef\u4ee5\u751f\u6210\u4e00\u7ec4\u8fde\u7eed\u7684\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // std::min_element and std::max_element #include #include #include // std::iota int main () { std :: list < int > li ( 6 ); // \u4f7f\u7528\u6570\u5b57\u586b\u5145 li \uff0c\u4ece0\u5f00\u59cb std :: iota ( li . begin (), li . end (), 0 ); std :: cout << * std :: min_element ( li . begin (), li . end ()) << ' ' << * std :: max_element ( li . begin (), li . end ()) << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 0 5 find \u548c list::insert \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c std::find() \u7b97\u6cd5\u7528\u4e8e\u5728\u94fe\u8868\u4e2d\u8fdb\u884c\u67e5\u627e\uff0c\u7136\u540e\u4f7f\u7528 list::insert() \u51fd\u6570\u5411\u94fe\u8868\u4e2d\u7684\u8be5\u4f4d\u7f6e\u63d2\u5165\u4e00\u4e2a\u65b0\u7684\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include #include #include int main () { std :: list < int > li ( 6 ); std :: iota ( li . begin (), li . end (), 0 ); // \u5728\u94fe\u8868\u4e2d\u627e\u5230 3 auto it { std :: find ( li . begin (), li . end (), 3 ) }; // \u5c06 8 \u63d2\u5165\u5230 3 \u7684\u524d\u9762 li . insert ( it , 8 ); for ( int i : li ) // \u57fa\u4e8e\u8fed\u4ee3\u5668\u7684for\u5faa\u73af std :: cout << i << ' ' ; std :: cout << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 0 1 2 8 3 4 5 \u5982\u679c\u7b97\u6cd5\u6ca1\u6709\u627e\u5230\u76ee\u6807\u503c\uff0c\u5b83\u4f1a\u8fd4\u56deend\u8fed\u4ee3\u5668\u3002\u5982\u679c\u6211\u4eec\u4e0d\u786e\u5b9a3\u662f\u5426\u5728 li \u4e2d\uff0c\u5219\u6700\u597d\u5148\u901a\u8fc7 std::find \u67e5\u627e\u5e76\u901a\u8fc7\u8fd4\u56de\u7684\u8fed\u4ee3\u5668\u786e\u8ba4\u662f\u5426\u627e\u5230\uff0c\u7136\u540e\u518d\u4f7f\u7528\u8be5\u8fed\u4ee3\u5668\u3002 1 2 3 4 5 6 7 8 if ( it == li . end ()) { std :: cout << \"3 was not found \\n \" ; } else { // ... } sort \u548c reverse \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f1a\u9996\u5148\u5bf9vector\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u518d\u5c06\u5176\u9006\u5e8f\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include #include #include int main () { std :: vector < int > vect { 7 , -3 , 6 , 2 , -5 , 0 , 4 }; // \u6392\u5e8f std :: sort ( vect . begin (), vect . end ()); for ( int i : vect ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; // \u9006\u5e8f std :: reverse ( vect . begin (), vect . end ()); for ( int i : vect ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 2 -5 -3 0 2 4 6 7 7 6 4 2 0 -3 -5 \u6211\u4eec\u4e5f\u53ef\u4ee5\u5c06\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u6bd4\u8f83\u51fd\u6570\u4f5c\u4e3a std::sort \u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u4f20\u5165\u3002\u5728 \u5934\u6587\u4ef6\u4e2d\u4e5f\u63d0\u4f9b\u4e86\u4e0d\u5c11\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7684\u6b64\u7c7b\u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u5c06 std::greater \u4f20\u5165 std::sort \uff0c\u7136\u540e\u4e0d\u8981\u4f7f\u7528 std::reverse \u3002\u6b64\u65f6 vector \u4e5f\u662f\u4ece\u5927\u5230\u5c0f\u6392\u5e8f\u7684\u3002 \u6ce8\u610f std::sort() \u4e0d\u80fd\u914d\u5408\u94fe\u8868\u4f7f\u7528\uff0c\u94fe\u8868\u63d0\u4f9b\u4e86\u81ea\u5df1\u7684 sort() \u6210\u5458\u51fd\u6570\uff0c\u5b83\u7684\u6548\u7387\u8981\u6bd4\u6cdb\u578b\u7684\u6392\u5e8f\u9ad8\u7684\u591a\u3002 \u5c0f\u7ed3 \u5c3d\u7ba1\u672c\u6587\u53ea\u662f\u7b80\u5355\u5730\u4ecb\u7ecd\u4e86STL\u4e2d\u63d0\u4f9b\u7684\u4e00\u4e9b\u7b97\u6cd5\uff0c\u5b83\u8db3\u4ee5\u5411\u4f60\u8bc1\u660e\u914d\u5408\u8fed\u4ee3\u5668\u548c\u5bb9\u5668\u4f7f\u7528\u8fd9\u4e9b\u7b97\u6cd5\u662f\u591a\u4e48\u7684\u7b80\u5355\u3002\u8981\u60f3\u8bb2\u5b8c\u5269\u4e0b\u7684\u7b97\u6cd5\uff0c\u90a3\u5f97\u9700\u8981\u4e00\u6574\u4e2a\u7ae0\u8282\u624d\u884c\u5462\uff01","title":"21.4 - STL\u7b97\u6cd5"},{"location":"21-4-STL-algorithms-overview/#min_element-max_element","text":"std::min_element \u548c std::max_element \u7b97\u6cd5\u7528\u4e8e\u67e5\u627e\u5bb9\u5668 \u5bb9\u5668\u7c7b \u4e2d\u7684\u6700\u5927\u5143\u7d20\u548c\u6700\u5c0f\u5143\u7d20\u3002 std::iota \u53ef\u4ee5\u751f\u6210\u4e00\u7ec4\u8fde\u7eed\u7684\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // std::min_element and std::max_element #include #include #include // std::iota int main () { std :: list < int > li ( 6 ); // \u4f7f\u7528\u6570\u5b57\u586b\u5145 li \uff0c\u4ece0\u5f00\u59cb std :: iota ( li . begin (), li . end (), 0 ); std :: cout << * std :: min_element ( li . begin (), li . end ()) << ' ' << * std :: max_element ( li . begin (), li . end ()) << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 0 5","title":"min_element \u548c max_element"},{"location":"21-4-STL-algorithms-overview/#find-listinsert","text":"\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c std::find() \u7b97\u6cd5\u7528\u4e8e\u5728\u94fe\u8868\u4e2d\u8fdb\u884c\u67e5\u627e\uff0c\u7136\u540e\u4f7f\u7528 list::insert() \u51fd\u6570\u5411\u94fe\u8868\u4e2d\u7684\u8be5\u4f4d\u7f6e\u63d2\u5165\u4e00\u4e2a\u65b0\u7684\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include #include #include int main () { std :: list < int > li ( 6 ); std :: iota ( li . begin (), li . end (), 0 ); // \u5728\u94fe\u8868\u4e2d\u627e\u5230 3 auto it { std :: find ( li . begin (), li . end (), 3 ) }; // \u5c06 8 \u63d2\u5165\u5230 3 \u7684\u524d\u9762 li . insert ( it , 8 ); for ( int i : li ) // \u57fa\u4e8e\u8fed\u4ee3\u5668\u7684for\u5faa\u73af std :: cout << i << ' ' ; std :: cout << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 0 1 2 8 3 4 5 \u5982\u679c\u7b97\u6cd5\u6ca1\u6709\u627e\u5230\u76ee\u6807\u503c\uff0c\u5b83\u4f1a\u8fd4\u56deend\u8fed\u4ee3\u5668\u3002\u5982\u679c\u6211\u4eec\u4e0d\u786e\u5b9a3\u662f\u5426\u5728 li \u4e2d\uff0c\u5219\u6700\u597d\u5148\u901a\u8fc7 std::find \u67e5\u627e\u5e76\u901a\u8fc7\u8fd4\u56de\u7684\u8fed\u4ee3\u5668\u786e\u8ba4\u662f\u5426\u627e\u5230\uff0c\u7136\u540e\u518d\u4f7f\u7528\u8be5\u8fed\u4ee3\u5668\u3002 1 2 3 4 5 6 7 8 if ( it == li . end ()) { std :: cout << \"3 was not found \\n \" ; } else { // ... }","title":"find \u548c list::insert"},{"location":"21-4-STL-algorithms-overview/#sort-reverse","text":"\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f1a\u9996\u5148\u5bf9vector\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u518d\u5c06\u5176\u9006\u5e8f\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include #include #include int main () { std :: vector < int > vect { 7 , -3 , 6 , 2 , -5 , 0 , 4 }; // \u6392\u5e8f std :: sort ( vect . begin (), vect . end ()); for ( int i : vect ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; // \u9006\u5e8f std :: reverse ( vect . begin (), vect . end ()); for ( int i : vect ) { std :: cout << i << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 2 -5 -3 0 2 4 6 7 7 6 4 2 0 -3 -5 \u6211\u4eec\u4e5f\u53ef\u4ee5\u5c06\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u6bd4\u8f83\u51fd\u6570\u4f5c\u4e3a std::sort \u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u4f20\u5165\u3002\u5728 \u5934\u6587\u4ef6\u4e2d\u4e5f\u63d0\u4f9b\u4e86\u4e0d\u5c11\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7684\u6b64\u7c7b\u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u5c06 std::greater \u4f20\u5165 std::sort \uff0c\u7136\u540e\u4e0d\u8981\u4f7f\u7528 std::reverse \u3002\u6b64\u65f6 vector \u4e5f\u662f\u4ece\u5927\u5230\u5c0f\u6392\u5e8f\u7684\u3002 \u6ce8\u610f std::sort() \u4e0d\u80fd\u914d\u5408\u94fe\u8868\u4f7f\u7528\uff0c\u94fe\u8868\u63d0\u4f9b\u4e86\u81ea\u5df1\u7684 sort() \u6210\u5458\u51fd\u6570\uff0c\u5b83\u7684\u6548\u7387\u8981\u6bd4\u6cdb\u578b\u7684\u6392\u5e8f\u9ad8\u7684\u591a\u3002","title":"sort \u548c reverse"},{"location":"21-4-STL-algorithms-overview/#_1","text":"\u5c3d\u7ba1\u672c\u6587\u53ea\u662f\u7b80\u5355\u5730\u4ecb\u7ecd\u4e86STL\u4e2d\u63d0\u4f9b\u7684\u4e00\u4e9b\u7b97\u6cd5\uff0c\u5b83\u8db3\u4ee5\u5411\u4f60\u8bc1\u660e\u914d\u5408\u8fed\u4ee3\u5668\u548c\u5bb9\u5668\u4f7f\u7528\u8fd9\u4e9b\u7b97\u6cd5\u662f\u591a\u4e48\u7684\u7b80\u5355\u3002\u8981\u60f3\u8bb2\u5b8c\u5269\u4e0b\u7684\u7b97\u6cd5\uff0c\u90a3\u5f97\u9700\u8981\u4e00\u6574\u4e2a\u7ae0\u8282\u624d\u884c\u5462\uff01","title":"\u5c0f\u7ed3"},{"location":"22-1-std-string-and-std-wstring/","tags":["string","wstring"],"text":"Key Takeaway C++\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u5f88\u591a\u6709\u7528\u7684\u7c7b\uff0c\u4f46\u662f\u5176\u4e2d\u6700\u6709\u7528\u7684\uff0c\u53ef\u80fd\u975e std::string \u83ab\u5c5e\u4e86\u3002 std::string (\u548c std::wstring ) \u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b\uff0c\u63d0\u4f9b\u4e86\u5b57\u7b26\u4e32\u76f8\u5173\u7684\u591a\u79cd\u64cd\u4f5c\uff0c\u5305\u62ec\u8d4b\u503c\u3001\u6bd4\u8f83\u548c\u4fee\u6539\uff0c\u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u4f1a\u4e00\u8d77\u6df1\u5165\u5b66\u4e60\u5b57\u7b26\u4e32\u7c7b\u3002 \u6ce8\u610f\uff1a\u6211\u4eec\u5c06C\u8bed\u8a00\u4e2d\u7684\u5b57\u7b26\u4e32\u79f0\u4e3aC\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u800c\u5c06 std::string (\u548c std::wstring \u79f0\u4e3a\u5b57\u7b26\u4e32)\u3002 \u4f5c\u8005\u6ce8 \u6ce8\u610f\uff0c\u672c\u7ae0\u8282\u7684\u5185\u5bb9\u7a0d\u5fae\u6709\u4e9b\u8fc7\u65f6\uff0c\u800c\u4e14\u5f88\u53ef\u80fd\u4f1a\u5728\u5c06\u6765\u8fdb\u884c\u66f4\u65b0\u3002\u4f60\u53ef\u4ee5\u6d4f\u89c8\u4e00\u4e0b\u672c\u7ae0\u8282\uff0c\u91cd\u70b9\u5173\u6ce8\u6709\u5173\u5b57\u7b26\u4e32\u7684\u6838\u5fc3\u601d\u60f3\u548c\u4f8b\u5b50\u3002\u4f46\u5982\u679c\u662f\u4f5c\u4e3a\u53c2\u8003\u6587\u6863\uff0c\u4e13\u95e8\u7684\u53c2\u8003\u7f51\u7ad9(\u4f8b\u5982 cppreference ) \u4f1a\u4f7f\u66f4\u597d\u7684\u9009\u62e9\uff0c\u5728\u90a3\u91cc\u4f60\u53ef\u4ee5\u627e\u5230\u66f4\u65b0\u3001\u66f4\u51c6\u786e\u7684\u4fe1\u606f\u3002 \u8bbe\u8ba1\u5b57\u7b26\u4e32\u7c7b\u7684\u521d\u8877 \u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86 C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 \uff0c\u5b83\u7684\u672c\u8d28\u5c31\u662f\u4e00\u4e2a\u5b57\u7b26\u6570\u7ec4\uff0c\u5b58\u653e\u4e86\u7ec4\u6210\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u3002\u5982\u679c\u4f60\u66fe\u7ecf\u4f7f\u7528\u8fc7C\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u4f60\u80af\u5b9a\u4e5f\u4f1a\u89c9\u5f97\u5b83\u7528\u8d77\u6765\u5f88\u56f0\u96be\u3001\u5bb9\u6613\u51fa\u9519\u800c\u4e14\u4e0d\u5bb9\u6613\u8c03\u8bd5\u3002 C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u6709\u5f88\u591a\u7f3a\u9677\uff0c\u8fd9\u4e3b\u8981\u662f\u56e0\u4e3a\u4f60\u5fc5\u987b\u81ea\u5df1\u7ba1\u7406\u5b83\u7684\u5185\u5b58\u3002\u6bd4\u65b9\u8bf4\uff0c\u5982\u679c\u4f60\u9700\u8981\u5c06\u5b57\u7b26\u4e32\"hello\"\u8d4b\u503c\u5230\u4e00\u4e2a\u7f13\u5b58\u4e2d\uff0c\u5219\u4f60\u5fc5\u987b\u9996\u5148\u5206\u914d\u4e00\u4e2a\u957f\u5ea6\u6b63\u786e\u7684\u7f13\u5b58\uff1a 1 char * strHello { new char [ 7 ] }; \u4e0d\u8981\u5fd8\u8bb0\u5728\u8ba1\u7b97\u957f\u5ea6\u65f6\u591a\u52a0\u4e00\u4e2a\u7ed3\u675f\u7b26\u7684\u957f\u5ea6\uff01 \u7136\u540e\u4f60\u9700\u8981\u5c06\u503c\u62f7\u8d1d\u5230\u8fd9\u5757\u7f13\u5b58\u4e2d\uff1a 1 strcpy ( strHello , \"hello!\" ); \u53ea\u6709\u5f53\u4f60\u8ba1\u7b97\u7684\u7f13\u5b58\u957f\u5ea6\u6b63\u786e\u65f6\uff0c\u624d\u4e0d\u4f1a\u51fa\u73b0\u7f13\u51b2\u533a\u6ea2\u51fa\u3002 \u5f53\u7136\uff0c\u56e0\u4e3a\u5b57\u7b26\u4e32\u662f\u52a8\u6001\u5206\u914d\u7684\uff0c\u6240\u4ee5\u4f60\u8fd8\u5fc5\u987b\u8bb0\u5f97\u5728\u4f7f\u7528\u5b8c\u6210\u540e\u6b63\u786e\u5730\u91ca\u653e\u5b83\uff1a 1 delete [] strHello ; \u4e0d\u8981\u5fd8\u8bb0\u4f7f\u7528\u6570\u7ec4delete\u800c\u4e0d\u662f\u666e\u901adelete\u3002 \u6b64\u5916\uff0cC\u8bed\u8a00\u63d0\u4f9b\u7684\u8bb8\u591a\u7528\u4e8e\u5904\u7406\u6570\u5b57\u7684\u64cd\u4f5c\u7b26\uff0c\u4f8b\u5982\u8d4b\u503c\u548c\u6bd4\u8f83\uff0c\u6839\u672c\u65e0\u6cd5\u7528\u4e8e\u5904\u7406C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002\u6709\u65f6\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u770b\u4f3c\u6709\u6548\uff0c\u4f46\u5b9e\u9645\u4e0a\u4f1a\u4ea7\u751f\u4e0d\u6b63\u786e\u7684\u7ed3\u679c\u2014\u2014\u4f8b\u5982\uff0c\u4f7f\u7528 == \u6bd4\u8f83\u4e24\u4e2aC\u98ce\u683c\u5b57\u7b26\u4e32\u65f6\uff0c\u5b9e\u9645\u4e0a\u6bd4\u8f83\u7684\u662f\u4e24\u4e2a\u6307\u9488\uff0c\u800c\u975e\u5b57\u7b26\u4e32\u672c\u8eab\u3002\u4f7f\u7528 operator= \u5c06\u4e00\u4e2aC\u98ce\u683c\u5b57\u7b26\u4e32\u8d4b\u503c\u7ed9\u53e6\u4e00\u4e2aC\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u4e4d\u4e00\u770b\u4f3c\u4e4e\u53ef\u884c\uff0c\u4f46\u5b9e\u9645\u4e0a\u662f\u5728\u8fdb\u884c\u6307\u9488\u7684 \u6d45\u62f7\u8d1d \uff0c\u8fd9\u901a\u5e38\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c\u3002\u8fd9\u7c7b\u4e8b\u60c5\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\uff0c\u800c\u4e14\u5f88\u96be\u53d1\u73b0\u548c\u8c03\u8bd5\uff01 \u5f52\u6839\u7ed3\u5e95\uff0c\u4f7f\u7528C\u98ce\u683c\u5b57\u7b26\u4e32\u9700\u8981\u8bb0\u4f4f\u8bb8\u591a\u5173\u4e8e\u5b89\u5168/\u4e0d\u5b89\u5168\u7684\u89c4\u5219\uff0c\u8bb0\u4f4f\u4e00\u5806\u5177\u6709\u6709\u8da3\u540d\u79f0\u7684\u51fd\u6570\uff0c\u5982 strcat() \u548c strcmp() \uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u76f4\u89c2\u7684\u64cd\u4f5c\u7b26\uff0c\u800c\u4e14\u8fd8\u9700\u8981\u8fdb\u884c\u5927\u91cf\u7684\u624b\u52a8\u5185\u5b58\u7ba1\u7406\u3002 \u5e78\u8fd0\u7684\u662f\uff0cC++\u548c\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u5904\u7406\u5b57\u7b26\u4e32\u7684\u66f4\u597d\u7684\u65b9\u6cd5\uff1a std::string \u548c std::wstring \u7c7b\u3002\u901a\u8fc7\u4f7f\u7528\u6784\u9020\u51fd\u6570\u3001\u6790\u6784\u51fd\u6570\u548c\u64cd\u4f5c\u7b26\u91cd\u8f7d\u7b49C++\u6982\u5ff5\uff0c std::string \u4f7f\u6211\u4eec\u53ef\u4ee5\u4ee5\u4e00\u79cd\u76f4\u89c2\u548c\u5b89\u5168\u7684\u65b9\u5f0f\u521b\u5efa\u548c\u64cd\u4f5c\u5b57\u7b26\u4e32\uff01\u4e0d\u518d\u9700\u8981\u5185\u5b58\u7ba1\u7406\uff0c\u4e0d\u518d\u9700\u8981\u5947\u602a\u7684\u51fd\u6570\u540d\uff0c\u4e5f\u5927\u5927\u964d\u4f4e\u4e86\u53d1\u751f\u95ee\u9898\u7684\u53ef\u80fd\u6027\u3002 \u5b57\u7b26\u4e32\u7b80\u4ecb \u6240\u6709\u6807\u51c6\u5e93\u5b57\u7b26\u4e32\u51fd\u6570\u90fd\u4f4d\u4e8e\u5934\u6587\u4ef6\u4e2d\uff0c\u4f7f\u7528\u65f6\u53ea\u9700\u8981\u5305\u542b\u8be5\u5934\u6587\u4ef6\u5373\u53ef\uff1a 1 #include \u5728\u5b57\u7b26\u4e32\u5934\u6587\u4ef6\u4e2d\u5b9e\u9645\u4e0a\u67093\u4e2a\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\u7c7b\u3002\u7b2c\u4e00\u4e2a\u662f\u540d\u4e3a basic_string \u7684\u6a21\u677f\u57fa\u7c7b\uff1a 1 2 3 4 5 namespace std { template < class charT , class traits = char_traits < charT > , class Allocator = allocator < charT > > class basic_string ; } \u6211\u4eec\u5e76\u4e0d\u4f1a\u76f4\u63a5\u4f7f\u7528\u8fd9\u4e2a\u7c7b\uff0c\u6240\u4ee5\u6682\u65f6\u4e0d\u8981\u62c5\u5fc3 trait \u6216 Allocator \u8fd9\u4e9b\u4e71\u4e03\u516b\u7cdf\u7684\u60c5\u51b5\u3002\u5728\u51e0\u4e4e\u6240\u6709\u53ef\u4ee5\u60f3\u8c61\u5230\u7684\u60c5\u51b5\u4e0b\uff0c\u9ed8\u8ba4\u503c\u5c31\u8db3\u591f\u4e86\u3002 \u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u4e24\u79cd\u7c7b\u578b\u7684 basic_string \uff1a 1 2 3 4 5 namespace std { typedef basic_string < char > string ; typedef basic_string < wchar_t > wstring ; } \u8fd9\u662f\u4f60\u5c06\u5b9e\u9645\u4f7f\u7528\u7684\u4e24\u4e2a\u7c7b\u3002 string \u7528\u4e8e\u6807\u51c6\u7684ASCII\u548cutf-8\u5b57\u7b26\u4e32\u3002 wstring \u7528\u4e8e\u5bbd\u5b57\u7b26/unicode (utf-16)\u5b57\u7b26\u4e32\u3002\u4f46\u662f\u5e76\u6ca1\u6709\u7528\u4e8eutf-32\u5b57\u7b26\u4e32\u7684\u5185\u7f6e\u7c7b(\u4e0d\u8fc7\uff0c\u5982\u679c\u9700\u8981\u7684\u8bdd\uff0c\u60a8\u5e94\u8be5\u80fd\u591f\u4ece basic_string \u81ea\u5df1\u6269\u5c55)\u3002 \u5c3d\u7ba1\u4f60\u4f1a\u76f4\u63a5\u4f7f\u7528\u7684\u662f std::string \u548c std::wstring \uff0c\u4f46\u6240\u6709\u7684\u5b57\u7b26\u4e32\u529f\u80fd\u90fd\u662f\u5728 basic_string \u7c7b\u4e2d\u5b9e\u73b0\u7684\u3002 string \u548c wstring \u80fd\u591f\u76f4\u63a5\u8bbf\u95ee\u8fd9\u4e9b\u529f\u80fd\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u6a21\u677f\u5316\u7684\u3002\u56e0\u6b64\uff0c\u6240\u63d0\u4f9b\u7684\u6240\u6709\u51fd\u6570\u90fd\u9002\u7528\u4e8e string \u548c wstring \u3002\u4f46\u662f\uff0c\u7531\u4e8e basic_string \u662f\u4e00\u4e2a\u6a21\u677f\u5316\u7c7b\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u5f53\u4f60\u5bf9 string \u6216 wstring \u8fdb\u884c\u8bed\u6cd5\u9519\u8bef\u64cd\u4f5c\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u4ea7\u751f\u53ef\u6015\u7684\u6a21\u677f\u9519\u8bef\u3002\u4e0d\u8981\u88ab\u8fd9\u4e9b\u9519\u8bef\u5413\u5012\uff1a\u5b83\u4eec\u53ea\u662f\u770b\u8d77\u6765\u6bd4\u8f83\u5413\u4eba\u7f62\u4e86\u3002 \u8fd9\u662f string \u7c7b\u4e2d\u6240\u6709\u51fd\u6570\u7684\u5217\u8868\u3002\u8fd9\u4e9b\u51fd\u6570\u4e2d\u7684\u5927\u591a\u6570\u90fd\u6709\u591a\u79cd\u53d8\u79cd\u7528\u4e8e\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u8f93\u5165\uff0c\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\u66f4\u6df1\u5165\u5730\u8ba8\u5b83\u4eec\u7684\u5177\u4f53\u7528\u6cd5\u3002 \u6784\u5efa\u548c\u6790\u6784 Function Effect (constructor) \u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u62f7\u8d1d (destructor) \u9500\u6bc1\u5b57\u7b26\u4e32 \u957f\u5ea6\u548c\u5bb9\u91cf Function Effect capacity() \u5728\u4e0d\u91cd\u65b0\u5206\u914d\u5185\u5b58\u60c5\u51b5\u4e0b\uff0c\u80fd\u591f\u5b58\u653e\u7684\u6700\u591a\u7684\u5b57\u7b26\u4e2a\u6570 empty() \u8fd4\u56de\u5e03\u5c14\u503c\u8868\u793a\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u7a7a\u5b57\u7b26\u4e32 length(), size() \u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u4e2a\u6570 max_size() \u8fd4\u56de\u53ef\u4ee5\u5206\u914d\u7684\u6700\u957f\u7684\u5b57\u7b26\u4e32\u5927\u5c0f reserve() \u6269\u5927\u6216\u7f29\u5c0f\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf \u5143\u7d20\u8bbf\u95ee Function Effect [] , at() \u4f7f\u7528\u7d22\u5f15\u8bbf\u95ee\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26 \u4fee\u6539\u5b57\u7b26\u4e32 Function Effect =, assign() \u5b57\u7b26\u4e32\u8d4b\u503c +=, append(), push_back() \u5b57\u7b26\u4e32\u672b\u5c3e\u6dfb\u52a0 insert() \u5728\u4efb\u610f\u7d22\u5f15\u4f4d\u7f6e\u63d2\u5165 clear() \u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u7b26 erase() \u6e05\u9664\u5b57\u7b26 replace() \u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u4e00\u90e8\u5206 resize() \u8c03\u6574\u5b57\u7b26\u4e32\u5927\u5c0f(\u5728\u672b\u5c3e\u622a\u65ad\u6216\u6dfb\u52a0) swap() \u4ea4\u6362\u4e24\u4e2a\u5b57\u7b26\u4e32 \u8f93\u5165\u8f93\u51fa Function Effect >> , getline() \u4ece\u8f93\u5165\u6d41\u4e2d\u83b7\u53d6\u8f93\u5165 << \u5c06\u5b57\u7b26\u4e32\u8f93\u51fa\u5230\u8f93\u51fa\u6d41 c_str() \u8fd4\u56de\u4ee5\u7a7a\u5b57\u7b26\u7ed3\u5c3e\u7684C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 copy() \u62f7\u8d1d\u5b57\u7b26\u4e32\u5185\u5bb9\u5230\u5b57\u7b26\u6570\u7ec4(\u4e0d\u5305\u542b\u7ed3\u675f\u7b26) data() \u7c7b\u4f3c c_str() \u3002\u975econst\u91cd\u8f7d\uff0c\u5141\u8bb8\u5199\u5165\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u3002 \u5b57\u7b26\u4e32\u6bd4\u8f83 Function Effect == , != \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\uff08\u8fd4\u56de\u5e03\u5c14\u503c\uff09 < , <= , > , >= \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u5927\u5c0f\u5173\u7cfb\uff08\u8fd4\u56de\u5e03\u5c14\u503c\uff09 compare() \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u76f8\u7b49(\u8fd4\u56de -1, 0, \u6216 1) \u5b50\u4e32\u548c\u62fc\u63a5 Function Effect + \u8fde\u63a5\u4e24\u4e2a\u5b57\u7b26\u4e32 substr() \u8fd4\u56de\u4e00\u4e2a\u5b50\u4e32 \u641c\u7d22 Function Effect find() \u641c\u7d22\u5b57\u7b26\u3001\u5b50\u4e32\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u4f4d\u7f6e find_first_of() \u627e\u5230\u5b57\u7b26\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u4f4d\u7f6e find_first_not_of() \u627e\u5230\u7b2c\u4e00\u4e2a\u4e0d\u5728\u5b57\u7b26\u96c6\u4e2d\u7684\u5b57\u7b26\u51fa\u73b0\u7684\u4f4d\u7f6e find_last_of() \u627e\u5230\u5b57\u7b26\u96c6\u4e2d\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u51fa\u73b0\u7684\u7d22\u5f15\u4f4d\u7f6e find_last_not_of() \u627e\u5230\u6700\u540e\u4e00\u4e2a\u4e0d\u5728\u5b57\u7b26\u96c6\u4e2d\u7684\u5b57\u7b26\u51fa\u73b0\u7684\u4f4d\u7f6e rfind() \u641c\u7d22\u5b57\u7b26\u3001\u5b50\u4e32\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u4f4d\u7f6e \u8fed\u4ee3\u5668\u548c\u5185\u5b58\u5206\u914d Function Effect begin() , end() \u6b63\u5411\u8fed\u4ee3\u5668\uff0c\u5f00\u5934/\u7ed3\u5c3e get_allocator() \u8fd4\u56de\u5206\u914d\u5668 rbegin() , rend() \u53cd\u5411\u8fed\u4ee3\u5668\uff0c\u5f00\u5934/\u7ed3\u5c3e \u867d\u7136\u6807\u51c6\u5e93\u5b57\u7b26\u4e32\u7c7b\u63d0\u4f9b\u4e86\u5f88\u591a\u529f\u80fd\uff0c\u4f46\u6709\u4e00\u4e9b\u660e\u663e\u7684\u9057\u6f0f\uff1a \u5bf9\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u652f\u6301\uff1b \u4ece\u6570\u5b57\u521b\u5efa\u5b57\u7b26\u4e32\u7684\u6784\u9020\u51fd\u6570\uff1b \u5927\u5c0f\u5199\u5b57\u6bcd\u8f6c\u6362\u51fd\u6570\uff1b \u5ffd\u7565\u5927\u5c0f\u5199\u7684\u5b57\u7b26\u4e32\u6bd4\u8f83\u65b9\u6cd5\uff1b \u5b57\u7b26\u4e32\u5206\u8bcd(Tokenization)\u540e\u5b58\u5165\u6570\u7ec4\uff1b \u83b7\u53d6\u5b57\u7b26\u4e32\u5de6\u534a\u90e8\u5206\u6216\u53f3\u534a\u90e8\u5206\u7684\u51fd\u6570\uff1b \u7a7a\u767d\u79fb\u9664\u51fd\u6570(trimming)\uff1b sprintf \u98ce\u683c\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u51fd\u6570\uff1b utf-8 \u548c utf-16 \u8f6c\u6362\u3002 \u5bf9\u4e8e\u5176\u4e2d\u7684\u5927\u591a\u6570\u529f\u80fd\uff0c\u4f60\u5fc5\u987b\u7f16\u5199\u81ea\u5df1\u7684\u51fd\u6570\uff0c\u6216\u8005\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aC\u98ce\u683c\u7684\u5b57\u7b26\u4e32(\u4f7f\u7528 c_str() )\uff0c\u5e76\u4f7f\u7528\u63d0\u4f9b\u6b64\u529f\u80fd\u7684C\u51fd\u6570\u3002 \u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u66f4\u6df1\u5165\u5730\u7814\u7a76\u5b57\u7b26\u4e32\u7c7b\u7684\u5404\u79cd\u529f\u80fd\u3002\u5c3d\u7ba1\u6211\u4eec\u5c06\u5728\u793a\u4f8b\u4e2d\u4f7f\u7528 string \uff0c\u4f46\u6240\u6709\u5185\u5bb9\u90fd\u540c\u6837\u9002\u7528\u4e8e wstring \u3002","title":"22.1 - std::string \u548c std::wstring"},{"location":"22-1-std-string-and-std-wstring/#_1","text":"\u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86 C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 \uff0c\u5b83\u7684\u672c\u8d28\u5c31\u662f\u4e00\u4e2a\u5b57\u7b26\u6570\u7ec4\uff0c\u5b58\u653e\u4e86\u7ec4\u6210\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u3002\u5982\u679c\u4f60\u66fe\u7ecf\u4f7f\u7528\u8fc7C\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u4f60\u80af\u5b9a\u4e5f\u4f1a\u89c9\u5f97\u5b83\u7528\u8d77\u6765\u5f88\u56f0\u96be\u3001\u5bb9\u6613\u51fa\u9519\u800c\u4e14\u4e0d\u5bb9\u6613\u8c03\u8bd5\u3002 C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u6709\u5f88\u591a\u7f3a\u9677\uff0c\u8fd9\u4e3b\u8981\u662f\u56e0\u4e3a\u4f60\u5fc5\u987b\u81ea\u5df1\u7ba1\u7406\u5b83\u7684\u5185\u5b58\u3002\u6bd4\u65b9\u8bf4\uff0c\u5982\u679c\u4f60\u9700\u8981\u5c06\u5b57\u7b26\u4e32\"hello\"\u8d4b\u503c\u5230\u4e00\u4e2a\u7f13\u5b58\u4e2d\uff0c\u5219\u4f60\u5fc5\u987b\u9996\u5148\u5206\u914d\u4e00\u4e2a\u957f\u5ea6\u6b63\u786e\u7684\u7f13\u5b58\uff1a 1 char * strHello { new char [ 7 ] }; \u4e0d\u8981\u5fd8\u8bb0\u5728\u8ba1\u7b97\u957f\u5ea6\u65f6\u591a\u52a0\u4e00\u4e2a\u7ed3\u675f\u7b26\u7684\u957f\u5ea6\uff01 \u7136\u540e\u4f60\u9700\u8981\u5c06\u503c\u62f7\u8d1d\u5230\u8fd9\u5757\u7f13\u5b58\u4e2d\uff1a 1 strcpy ( strHello , \"hello!\" ); \u53ea\u6709\u5f53\u4f60\u8ba1\u7b97\u7684\u7f13\u5b58\u957f\u5ea6\u6b63\u786e\u65f6\uff0c\u624d\u4e0d\u4f1a\u51fa\u73b0\u7f13\u51b2\u533a\u6ea2\u51fa\u3002 \u5f53\u7136\uff0c\u56e0\u4e3a\u5b57\u7b26\u4e32\u662f\u52a8\u6001\u5206\u914d\u7684\uff0c\u6240\u4ee5\u4f60\u8fd8\u5fc5\u987b\u8bb0\u5f97\u5728\u4f7f\u7528\u5b8c\u6210\u540e\u6b63\u786e\u5730\u91ca\u653e\u5b83\uff1a 1 delete [] strHello ; \u4e0d\u8981\u5fd8\u8bb0\u4f7f\u7528\u6570\u7ec4delete\u800c\u4e0d\u662f\u666e\u901adelete\u3002 \u6b64\u5916\uff0cC\u8bed\u8a00\u63d0\u4f9b\u7684\u8bb8\u591a\u7528\u4e8e\u5904\u7406\u6570\u5b57\u7684\u64cd\u4f5c\u7b26\uff0c\u4f8b\u5982\u8d4b\u503c\u548c\u6bd4\u8f83\uff0c\u6839\u672c\u65e0\u6cd5\u7528\u4e8e\u5904\u7406C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002\u6709\u65f6\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u770b\u4f3c\u6709\u6548\uff0c\u4f46\u5b9e\u9645\u4e0a\u4f1a\u4ea7\u751f\u4e0d\u6b63\u786e\u7684\u7ed3\u679c\u2014\u2014\u4f8b\u5982\uff0c\u4f7f\u7528 == \u6bd4\u8f83\u4e24\u4e2aC\u98ce\u683c\u5b57\u7b26\u4e32\u65f6\uff0c\u5b9e\u9645\u4e0a\u6bd4\u8f83\u7684\u662f\u4e24\u4e2a\u6307\u9488\uff0c\u800c\u975e\u5b57\u7b26\u4e32\u672c\u8eab\u3002\u4f7f\u7528 operator= \u5c06\u4e00\u4e2aC\u98ce\u683c\u5b57\u7b26\u4e32\u8d4b\u503c\u7ed9\u53e6\u4e00\u4e2aC\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u4e4d\u4e00\u770b\u4f3c\u4e4e\u53ef\u884c\uff0c\u4f46\u5b9e\u9645\u4e0a\u662f\u5728\u8fdb\u884c\u6307\u9488\u7684 \u6d45\u62f7\u8d1d \uff0c\u8fd9\u901a\u5e38\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c\u3002\u8fd9\u7c7b\u4e8b\u60c5\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\uff0c\u800c\u4e14\u5f88\u96be\u53d1\u73b0\u548c\u8c03\u8bd5\uff01 \u5f52\u6839\u7ed3\u5e95\uff0c\u4f7f\u7528C\u98ce\u683c\u5b57\u7b26\u4e32\u9700\u8981\u8bb0\u4f4f\u8bb8\u591a\u5173\u4e8e\u5b89\u5168/\u4e0d\u5b89\u5168\u7684\u89c4\u5219\uff0c\u8bb0\u4f4f\u4e00\u5806\u5177\u6709\u6709\u8da3\u540d\u79f0\u7684\u51fd\u6570\uff0c\u5982 strcat() \u548c strcmp() \uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u76f4\u89c2\u7684\u64cd\u4f5c\u7b26\uff0c\u800c\u4e14\u8fd8\u9700\u8981\u8fdb\u884c\u5927\u91cf\u7684\u624b\u52a8\u5185\u5b58\u7ba1\u7406\u3002 \u5e78\u8fd0\u7684\u662f\uff0cC++\u548c\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u5904\u7406\u5b57\u7b26\u4e32\u7684\u66f4\u597d\u7684\u65b9\u6cd5\uff1a std::string \u548c std::wstring \u7c7b\u3002\u901a\u8fc7\u4f7f\u7528\u6784\u9020\u51fd\u6570\u3001\u6790\u6784\u51fd\u6570\u548c\u64cd\u4f5c\u7b26\u91cd\u8f7d\u7b49C++\u6982\u5ff5\uff0c std::string \u4f7f\u6211\u4eec\u53ef\u4ee5\u4ee5\u4e00\u79cd\u76f4\u89c2\u548c\u5b89\u5168\u7684\u65b9\u5f0f\u521b\u5efa\u548c\u64cd\u4f5c\u5b57\u7b26\u4e32\uff01\u4e0d\u518d\u9700\u8981\u5185\u5b58\u7ba1\u7406\uff0c\u4e0d\u518d\u9700\u8981\u5947\u602a\u7684\u51fd\u6570\u540d\uff0c\u4e5f\u5927\u5927\u964d\u4f4e\u4e86\u53d1\u751f\u95ee\u9898\u7684\u53ef\u80fd\u6027\u3002","title":"\u8bbe\u8ba1\u5b57\u7b26\u4e32\u7c7b\u7684\u521d\u8877"},{"location":"22-1-std-string-and-std-wstring/#_2","text":"\u6240\u6709\u6807\u51c6\u5e93\u5b57\u7b26\u4e32\u51fd\u6570\u90fd\u4f4d\u4e8e\u5934\u6587\u4ef6\u4e2d\uff0c\u4f7f\u7528\u65f6\u53ea\u9700\u8981\u5305\u542b\u8be5\u5934\u6587\u4ef6\u5373\u53ef\uff1a 1 #include \u5728\u5b57\u7b26\u4e32\u5934\u6587\u4ef6\u4e2d\u5b9e\u9645\u4e0a\u67093\u4e2a\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\u7c7b\u3002\u7b2c\u4e00\u4e2a\u662f\u540d\u4e3a basic_string \u7684\u6a21\u677f\u57fa\u7c7b\uff1a 1 2 3 4 5 namespace std { template < class charT , class traits = char_traits < charT > , class Allocator = allocator < charT > > class basic_string ; } \u6211\u4eec\u5e76\u4e0d\u4f1a\u76f4\u63a5\u4f7f\u7528\u8fd9\u4e2a\u7c7b\uff0c\u6240\u4ee5\u6682\u65f6\u4e0d\u8981\u62c5\u5fc3 trait \u6216 Allocator \u8fd9\u4e9b\u4e71\u4e03\u516b\u7cdf\u7684\u60c5\u51b5\u3002\u5728\u51e0\u4e4e\u6240\u6709\u53ef\u4ee5\u60f3\u8c61\u5230\u7684\u60c5\u51b5\u4e0b\uff0c\u9ed8\u8ba4\u503c\u5c31\u8db3\u591f\u4e86\u3002 \u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u4e24\u79cd\u7c7b\u578b\u7684 basic_string \uff1a 1 2 3 4 5 namespace std { typedef basic_string < char > string ; typedef basic_string < wchar_t > wstring ; } \u8fd9\u662f\u4f60\u5c06\u5b9e\u9645\u4f7f\u7528\u7684\u4e24\u4e2a\u7c7b\u3002 string \u7528\u4e8e\u6807\u51c6\u7684ASCII\u548cutf-8\u5b57\u7b26\u4e32\u3002 wstring \u7528\u4e8e\u5bbd\u5b57\u7b26/unicode (utf-16)\u5b57\u7b26\u4e32\u3002\u4f46\u662f\u5e76\u6ca1\u6709\u7528\u4e8eutf-32\u5b57\u7b26\u4e32\u7684\u5185\u7f6e\u7c7b(\u4e0d\u8fc7\uff0c\u5982\u679c\u9700\u8981\u7684\u8bdd\uff0c\u60a8\u5e94\u8be5\u80fd\u591f\u4ece basic_string \u81ea\u5df1\u6269\u5c55)\u3002 \u5c3d\u7ba1\u4f60\u4f1a\u76f4\u63a5\u4f7f\u7528\u7684\u662f std::string \u548c std::wstring \uff0c\u4f46\u6240\u6709\u7684\u5b57\u7b26\u4e32\u529f\u80fd\u90fd\u662f\u5728 basic_string \u7c7b\u4e2d\u5b9e\u73b0\u7684\u3002 string \u548c wstring \u80fd\u591f\u76f4\u63a5\u8bbf\u95ee\u8fd9\u4e9b\u529f\u80fd\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u6a21\u677f\u5316\u7684\u3002\u56e0\u6b64\uff0c\u6240\u63d0\u4f9b\u7684\u6240\u6709\u51fd\u6570\u90fd\u9002\u7528\u4e8e string \u548c wstring \u3002\u4f46\u662f\uff0c\u7531\u4e8e basic_string \u662f\u4e00\u4e2a\u6a21\u677f\u5316\u7c7b\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u5f53\u4f60\u5bf9 string \u6216 wstring \u8fdb\u884c\u8bed\u6cd5\u9519\u8bef\u64cd\u4f5c\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u4ea7\u751f\u53ef\u6015\u7684\u6a21\u677f\u9519\u8bef\u3002\u4e0d\u8981\u88ab\u8fd9\u4e9b\u9519\u8bef\u5413\u5012\uff1a\u5b83\u4eec\u53ea\u662f\u770b\u8d77\u6765\u6bd4\u8f83\u5413\u4eba\u7f62\u4e86\u3002 \u8fd9\u662f string \u7c7b\u4e2d\u6240\u6709\u51fd\u6570\u7684\u5217\u8868\u3002\u8fd9\u4e9b\u51fd\u6570\u4e2d\u7684\u5927\u591a\u6570\u90fd\u6709\u591a\u79cd\u53d8\u79cd\u7528\u4e8e\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u8f93\u5165\uff0c\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\u66f4\u6df1\u5165\u5730\u8ba8\u5b83\u4eec\u7684\u5177\u4f53\u7528\u6cd5\u3002","title":"\u5b57\u7b26\u4e32\u7b80\u4ecb"},{"location":"22-1-std-string-and-std-wstring/#_3","text":"Function Effect (constructor) \u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u62f7\u8d1d (destructor) \u9500\u6bc1\u5b57\u7b26\u4e32","title":"\u6784\u5efa\u548c\u6790\u6784"},{"location":"22-1-std-string-and-std-wstring/#_4","text":"Function Effect capacity() \u5728\u4e0d\u91cd\u65b0\u5206\u914d\u5185\u5b58\u60c5\u51b5\u4e0b\uff0c\u80fd\u591f\u5b58\u653e\u7684\u6700\u591a\u7684\u5b57\u7b26\u4e2a\u6570 empty() \u8fd4\u56de\u5e03\u5c14\u503c\u8868\u793a\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u7a7a\u5b57\u7b26\u4e32 length(), size() \u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u4e2a\u6570 max_size() \u8fd4\u56de\u53ef\u4ee5\u5206\u914d\u7684\u6700\u957f\u7684\u5b57\u7b26\u4e32\u5927\u5c0f reserve() \u6269\u5927\u6216\u7f29\u5c0f\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf","title":"\u957f\u5ea6\u548c\u5bb9\u91cf"},{"location":"22-1-std-string-and-std-wstring/#_5","text":"Function Effect [] , at() \u4f7f\u7528\u7d22\u5f15\u8bbf\u95ee\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26","title":"\u5143\u7d20\u8bbf\u95ee"},{"location":"22-1-std-string-and-std-wstring/#_6","text":"Function Effect =, assign() \u5b57\u7b26\u4e32\u8d4b\u503c +=, append(), push_back() \u5b57\u7b26\u4e32\u672b\u5c3e\u6dfb\u52a0 insert() \u5728\u4efb\u610f\u7d22\u5f15\u4f4d\u7f6e\u63d2\u5165 clear() \u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u7b26 erase() \u6e05\u9664\u5b57\u7b26 replace() \u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u4e00\u90e8\u5206 resize() \u8c03\u6574\u5b57\u7b26\u4e32\u5927\u5c0f(\u5728\u672b\u5c3e\u622a\u65ad\u6216\u6dfb\u52a0) swap() \u4ea4\u6362\u4e24\u4e2a\u5b57\u7b26\u4e32","title":"\u4fee\u6539\u5b57\u7b26\u4e32"},{"location":"22-1-std-string-and-std-wstring/#_7","text":"Function Effect >> , getline() \u4ece\u8f93\u5165\u6d41\u4e2d\u83b7\u53d6\u8f93\u5165 << \u5c06\u5b57\u7b26\u4e32\u8f93\u51fa\u5230\u8f93\u51fa\u6d41 c_str() \u8fd4\u56de\u4ee5\u7a7a\u5b57\u7b26\u7ed3\u5c3e\u7684C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 copy() \u62f7\u8d1d\u5b57\u7b26\u4e32\u5185\u5bb9\u5230\u5b57\u7b26\u6570\u7ec4(\u4e0d\u5305\u542b\u7ed3\u675f\u7b26) data() \u7c7b\u4f3c c_str() \u3002\u975econst\u91cd\u8f7d\uff0c\u5141\u8bb8\u5199\u5165\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u3002","title":"\u8f93\u5165\u8f93\u51fa"},{"location":"22-1-std-string-and-std-wstring/#_8","text":"Function Effect == , != \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\uff08\u8fd4\u56de\u5e03\u5c14\u503c\uff09 < , <= , > , >= \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u5927\u5c0f\u5173\u7cfb\uff08\u8fd4\u56de\u5e03\u5c14\u503c\uff09 compare() \u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u76f8\u7b49(\u8fd4\u56de -1, 0, \u6216 1)","title":"\u5b57\u7b26\u4e32\u6bd4\u8f83"},{"location":"22-1-std-string-and-std-wstring/#_9","text":"Function Effect + \u8fde\u63a5\u4e24\u4e2a\u5b57\u7b26\u4e32 substr() \u8fd4\u56de\u4e00\u4e2a\u5b50\u4e32","title":"\u5b50\u4e32\u548c\u62fc\u63a5"},{"location":"22-1-std-string-and-std-wstring/#_10","text":"Function Effect find() \u641c\u7d22\u5b57\u7b26\u3001\u5b50\u4e32\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u4f4d\u7f6e find_first_of() \u627e\u5230\u5b57\u7b26\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u4f4d\u7f6e find_first_not_of() \u627e\u5230\u7b2c\u4e00\u4e2a\u4e0d\u5728\u5b57\u7b26\u96c6\u4e2d\u7684\u5b57\u7b26\u51fa\u73b0\u7684\u4f4d\u7f6e find_last_of() \u627e\u5230\u5b57\u7b26\u96c6\u4e2d\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u51fa\u73b0\u7684\u7d22\u5f15\u4f4d\u7f6e find_last_not_of() \u627e\u5230\u6700\u540e\u4e00\u4e2a\u4e0d\u5728\u5b57\u7b26\u96c6\u4e2d\u7684\u5b57\u7b26\u51fa\u73b0\u7684\u4f4d\u7f6e rfind() \u641c\u7d22\u5b57\u7b26\u3001\u5b50\u4e32\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u4f4d\u7f6e","title":"\u641c\u7d22"},{"location":"22-1-std-string-and-std-wstring/#_11","text":"Function Effect begin() , end() \u6b63\u5411\u8fed\u4ee3\u5668\uff0c\u5f00\u5934/\u7ed3\u5c3e get_allocator() \u8fd4\u56de\u5206\u914d\u5668 rbegin() , rend() \u53cd\u5411\u8fed\u4ee3\u5668\uff0c\u5f00\u5934/\u7ed3\u5c3e \u867d\u7136\u6807\u51c6\u5e93\u5b57\u7b26\u4e32\u7c7b\u63d0\u4f9b\u4e86\u5f88\u591a\u529f\u80fd\uff0c\u4f46\u6709\u4e00\u4e9b\u660e\u663e\u7684\u9057\u6f0f\uff1a \u5bf9\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u652f\u6301\uff1b \u4ece\u6570\u5b57\u521b\u5efa\u5b57\u7b26\u4e32\u7684\u6784\u9020\u51fd\u6570\uff1b \u5927\u5c0f\u5199\u5b57\u6bcd\u8f6c\u6362\u51fd\u6570\uff1b \u5ffd\u7565\u5927\u5c0f\u5199\u7684\u5b57\u7b26\u4e32\u6bd4\u8f83\u65b9\u6cd5\uff1b \u5b57\u7b26\u4e32\u5206\u8bcd(Tokenization)\u540e\u5b58\u5165\u6570\u7ec4\uff1b \u83b7\u53d6\u5b57\u7b26\u4e32\u5de6\u534a\u90e8\u5206\u6216\u53f3\u534a\u90e8\u5206\u7684\u51fd\u6570\uff1b \u7a7a\u767d\u79fb\u9664\u51fd\u6570(trimming)\uff1b sprintf \u98ce\u683c\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u51fd\u6570\uff1b utf-8 \u548c utf-16 \u8f6c\u6362\u3002 \u5bf9\u4e8e\u5176\u4e2d\u7684\u5927\u591a\u6570\u529f\u80fd\uff0c\u4f60\u5fc5\u987b\u7f16\u5199\u81ea\u5df1\u7684\u51fd\u6570\uff0c\u6216\u8005\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aC\u98ce\u683c\u7684\u5b57\u7b26\u4e32(\u4f7f\u7528 c_str() )\uff0c\u5e76\u4f7f\u7528\u63d0\u4f9b\u6b64\u529f\u80fd\u7684C\u51fd\u6570\u3002 \u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u66f4\u6df1\u5165\u5730\u7814\u7a76\u5b57\u7b26\u4e32\u7c7b\u7684\u5404\u79cd\u529f\u80fd\u3002\u5c3d\u7ba1\u6211\u4eec\u5c06\u5728\u793a\u4f8b\u4e2d\u4f7f\u7528 string \uff0c\u4f46\u6240\u6709\u5185\u5bb9\u90fd\u540c\u6837\u9002\u7528\u4e8e wstring \u3002","title":"\u8fed\u4ee3\u5668\u548c\u5185\u5b58\u5206\u914d"},{"location":"22-2-std-string-construction-and-destruction/","tags":["string"],"text":"Key Takeaway \u5728\u8fd9\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u770b\u770b\u5982\u4f55\u6784\u9020 std::string \u7684\u5bf9\u8c61\uff0c\u4ee5\u53ca\u5982\u4f55\u4ece\u6570\u5b57\u521b\u5efa\u5b57\u7b26\u4e32\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002 \u5b57\u7b26\u4e32\u6784\u9020 \u5b57\u7b26\u4e32\u7c7b\u6709\u8bb8\u591a\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u5b57\u7b26\u4e32\u7684\u6784\u9020\u51fd\u6570\u3002\u6211\u4eec\u5c06\u5728\u8fd9\u91cc\u9010\u4e00\u4e86\u89e3\u5b83\u4eec\u3002 \u6ce8\u610f\uff1a string::size_type \u89e3\u6790\u4e3a size_t \uff0c\u5b83\u4e0e sizeof \u64cd\u4f5c\u7b26\u8fd4\u56de\u7684\u65e0\u7b26\u53f7\u6574\u578b\u76f8\u540c\u3002 size_t \u7684\u5b9e\u9645\u5927\u5c0f\u53d6\u51b3\u4e8e\u73af\u5883\u3002\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u5c06\u5176\u770b\u505a\u65e0\u7b26\u53f7\u6574\u578b\u3002 string::string() \u5b57\u7b26\u4e32\u7c7b\u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u2014\u2014\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\u3002 \u4f8b\u5b50\uff1a 1 2 std :: string sSource ; std :: cout << sSource ; \u8f93\u51fa\uff1a 1 string::string(const string& strString) \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002\u8be5\u6784\u9020\u51fd\u6570\u57fa\u4e8e\u4e00\u4e2a\u5b57\u7b26\u4e32 strString \u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u3002 \u4f8b\u5b50\uff1a 1 2 3 std :: string sSource { \"my string\" }; std :: string sOutput { sSource }; std :: cout << sOutput ; \u8f93\u51fa\uff1a 1 my string string::string(const string& strString, size_type unIndex) string::string(const string& strString, size_type unIndex, size_type unLength) \u8be5\u6784\u9020\u51fd\u6570\u57fa\u4e8e strString \u5b57\u7b26\u4e32\u4e2d\u6700\u591a unLength \u4e2a\u5b57\u7b26\uff08\u4ece unIndex \u5f00\u59cb\uff09\u6784\u9020\u65b0\u7684\u5b57\u7b26\u4e32\u3002\u5982\u679c\u9047\u5230\u7ed3\u675f\u7b26\uff0c\u5219\u505c\u6b62\u62f7\u8d1d\uff0c\u5373\u4f7f\u8fd8\u6ca1\u6709\u5230 unLength \u4e2a\u5b57\u7b26\uff1b \u5982\u679c\u6ca1\u6709 unLength \u4e2a\u5b57\u7b26\uff0c\u5219\u4f1a\u4ece unIndex \u5f00\u59cb\u62f7\u8d1d\u5168\u90e8\u5b57\u7b26\uff1b \u5982\u679c unIndex \u6bd4\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u8fd8\u957f\uff0c\u5219\u6216\u629b\u51fa out_of_range \u5f02\u5e38\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 std :: string sSource { \"my string\" }; std :: string sOutput { sSource , 3 }; std :: cout << sOutput << '\\n' ; std :: string sOutput2 ( sSource , 3 , 4 ); std :: cout << sOutput2 << '\\n' ; \u8f93\u51fa\uff1a 1 2 string stri string::string(const char* szCString) \u8be5\u6784\u9020\u51fd\u6570\u4f1a\u57fa\u4e8e\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 szCString \u6784\u9020\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\uff0c\u4f46\u662f\u4e0d\u4f1a\u5305\u542b\u7ed3\u675f\u7b26\uff1b \u5982\u679c\u7ed3\u679c\u4f1a\u8d85\u8fc7\u5b57\u7b26\u4e32\u7684\u6700\u5927\u957f\u5ea6\uff0c\u5219\u4f1a\u629b\u51fa length_error \u5f02\u5e38\uff1b \u8b66\u544a\u26a0\ufe0f \uff1a szCString \u4e0d\u80fd\u662f NULL\u3002 \u4f8b\u5b50\uff1a 1 2 3 const char * szSource { \"my string\" }; std :: string sOutput { szSource }; std :: cout << sOutput << '\\n' ; \u8f93\u51fa\uff1a 1 my string string::string(const char* szCString, size_type unLength) This constructor creates a new string from the first unLength chars from the C-style string szCString. If the resulting size exceeds the maximum string length, the length_error exception will be thrown. Warning: For this function only, NULLs are not treated as end-of-string characters in szCString! This means it is possible to read off the end of your string if unLength is too big. Be careful not to overflow your string buffer! \u4f8b\u5b50\uff1a 1 2 3 const char * szSource { \"my string\" }; std :: string sOutput ( szSource , 4 ); std :: cout << sOutput << '\\n' ; \u8f93\u51fa\uff1a 1 my s string::string(size_type nNum, char chChar) \u8be5\u6784\u9020\u51fd\u6570\u57fa\u4e8e\u67d0\u4e2a\u5b57\u7b26\uff0c\u5c06\u5176\u91cd\u590d chChar \u6b21\u521b\u5efa\u51fa\u65b0\u7684\u5b57\u7b26\u4e32\uff1b \u5982\u679c\u7ed3\u679c\u7684\u957f\u5ea6\u8d85\u8fc7\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u5219\u629b\u51fa length_error \u5f02\u5e38\u3002 \u4f8b\u5b50\uff1a 1 2 std :: string sOutput ( 4 , 'Q' ); std :: cout << sOutput << '\\n' ; \u8f93\u51fa\uff1a 1 QQQQ template string::string(InputIterator itBeg, InputIterator itEnd) \u8be5\u6784\u9020\u51fd\u6570\u57fa\u4e8e\u4e00\u4e2a\u8303\u56f4 [itBeg, itEnd) \u7684\u5b57\u7b26\u4e32\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\uff1b \u5982\u679c\u7ed3\u679c\u7684\u957f\u5ea6\u8d85\u8fc7\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u5219\u629b\u51fa length_error \u5f02\u5e38\u3002 \u6ca1\u6709\u793a\u4f8b\u4ee3\u7801\u3002\u5b83\u592a\u6666\u6da9\u4e86\uff0c\u4f60\u53ef\u80fd\u6c38\u8fdc\u4e0d\u4f1a\u7528\u5b83\u3002 \u5b57\u7b26\u4e32\u6790\u6784 string::~string() \u6790\u6784\u51fd\u6570\uff0c\u7528\u4e8e\u9500\u6bc1\u5b57\u7b26\u4e32\u5e76\u91ca\u653e\u5185\u5b58 \u6ca1\u6709\u793a\u4f8b\u4ee3\u7801\u3002\u4f60\u4e0d\u4f1a\u663e\u793a\u5730\u8c03\u7528\u5b83\u3002 \u4ece\u6570\u5b57\u6784\u5efa\u5b57\u7b26\u4e32 std::string \u7c7b\u9057\u6f0f\u4e86\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u529f\u80fd\uff0c\u5373\u4ece\u6570\u5b57\u521b\u5efa\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\uff1a 1 std :: string sFour { 4 }; \u8f93\u51fa\u7ed3\u679c\uff1a 1 c:vcprojectstest2test2test.cpp(10) : error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it)' : cannot convert parameter 1 from 'int' to 'std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it' \u8fd8\u8bb0\u5f97\u6211\u8bf4\u8fc7\u5b57\u7b26\u4e32\u7c7b\u4f1a\u4ea7\u751f\u53ef\u6015\u7684\u62a5\u9519\u4fe1\u606f\u5417\uff1f\u5c31\u662f\u50cf\u4e0a\u9762\u8fd9\u6837\u3002\u8fd9\u91cc\u7684\u6709\u7528\u7684\u4fe1\u606f\u662f\uff1a 1 cannot convert parameter 1 from 'int' to 'std::basic_string \u6362\u53e5\u8bdd\u8bf4\uff0c\u5b83\u8bd5\u56fe\u5c06int\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u4f46\u5931\u8d25\u4e86\u3002 \u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 std::ostringstream \u7c7b\u3002 std::ostringstream \u53ef\u4ee5\u63a5\u53d7\u6765\u81ea\u5404\u79cd\u6765\u6e90\u7684\u8f93\u5165\uff0c\u5305\u62ec\u5b57\u7b26\u3001\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u7b49\u2026\u5b83\u8fd8\u80fd\u591f\u8f93\u51fa\u5b57\u7b26\u4e32(\u901a\u8fc7 \u63d0\u53d6\u8fd0\u7b97\u7b26 \uff0c\u6216\u901a\u8fc7 str() \u51fd\u6570)\u3002\u6709\u5173 std::ostringstream \u7684\u66f4\u591a\u4fe1\u606f\uff0c\u53c2\u8003 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b \u3002 \u4ece\u4e0d\u540c\u7c7b\u578b\u7684\u8f93\u5165\u521b\u5efa\u5b57\u7b26\u4e32\u7684\u4f8b\u5b50\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include #include #include template < typename T > inline std :: string ToString ( T tX ) { std :: ostringstream oStream ; oStream << tX ; return oStream . str (); } \u4e0b\u9762\u662f\u4e00\u4e9b\u6d4b\u8bd5\u5b83\u7684\u793a\u4f8b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 int main () { std :: string sFour { ToString ( 4 ) }; std :: string sSixPointSeven { ToString ( 6.7 ) }; std :: string sA { ToString ( 'A' ) }; std :: cout << sFour << '\\n' ; std :: cout << sSixPointSeven << '\\n' ; std :: cout << sA << '\\n' ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 6.7 A \u6ce8\u610f\uff0c\u8fd9\u91cc\u6211\u4eec\u5ffd\u7565\u4e86\u9519\u8bef\u68c0\u67e5\u3002\u5c06 tX \u63d2\u5165 oStream \u4e2d\u53ef\u80fd\u4f1a\u5931\u8d25\uff0c\u5982\u679c\u8f6c\u6362\u5931\u8d25\u5219\u5e94\u8be5\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u3002 \u76f8\u5173\u5185\u5bb9 \u6807\u51c6\u5e93\u8fd8\u5305\u542b\u4e00\u4e2a\u540d\u4e3a std::to_string() \u7684\u51fd\u6570\uff0c\u53ef\u7528\u4e8e\u5c06\u5b57\u7b26\u548c\u6570\u5b57\u8f6c\u6362\u4e3a std::string \u3002\u867d\u7136\u8fd9\u662f\u57fa\u672c\u573a\u5408\u4e2d\u66f4\u7b80\u5355\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u4f46 std::to_string \u7684\u8f93\u51fa\u53ef\u80fd\u4e0e\u4e0a\u9762 std::cout \u6216 out ToString() \u51fd\u6570\u7684\u8f93\u51fa\u4e0d\u540c\u3002\u5176\u4e2d\u4e00\u4e9b\u5dee\u5f02\u88ab\u8bb0\u5f55 \u8fd9\u91cc \u3002 \u4ece\u5b57\u7b26\u4e32\u521b\u5efa\u6570\u5b57 \u548c\u4e0a\u9762\u7684\u89e3\u51b3\u65b9\u6848\u7c7b\u4f3c\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include #include template < typename T > inline bool FromString ( const std :: string & sString , T & tX ) { std :: istringstream iStream ( sString ); return ! ( iStream >> tX ). fail (); // \u5c06\u503c\u63d0\u53d6\u5230 tX \u5e76\u8fd4\u56de\u662f\u5426\u6210\u529f } \u4e0b\u9762\u662f\u4e00\u4e9b\u6d4b\u8bd5\u5b83\u7684\u793a\u4f8b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 int main () { double dX ; if ( FromString ( \"3.4\" , dX )) std :: cout << dX << '\\n' ; if ( FromString ( \"ABC\" , dX )) std :: cout << dX << '\\n' ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 3.4 \u6ce8\u610f\uff0c\u7b2c\u4e8c\u6b21\u8f6c\u6362\u5931\u8d25\u5e76\u8fd4\u56de\u4e86 false \u3002","title":"22.2 - std-string \u7684\u6784\u9020\u548c\u6790\u6784"},{"location":"22-2-std-string-construction-and-destruction/#_1","text":"\u5b57\u7b26\u4e32\u7c7b\u6709\u8bb8\u591a\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u5b57\u7b26\u4e32\u7684\u6784\u9020\u51fd\u6570\u3002\u6211\u4eec\u5c06\u5728\u8fd9\u91cc\u9010\u4e00\u4e86\u89e3\u5b83\u4eec\u3002 \u6ce8\u610f\uff1a string::size_type \u89e3\u6790\u4e3a size_t \uff0c\u5b83\u4e0e sizeof \u64cd\u4f5c\u7b26\u8fd4\u56de\u7684\u65e0\u7b26\u53f7\u6574\u578b\u76f8\u540c\u3002 size_t \u7684\u5b9e\u9645\u5927\u5c0f\u53d6\u51b3\u4e8e\u73af\u5883\u3002\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u5c06\u5176\u770b\u505a\u65e0\u7b26\u53f7\u6574\u578b\u3002 string::string() \u5b57\u7b26\u4e32\u7c7b\u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u2014\u2014\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\u3002 \u4f8b\u5b50\uff1a 1 2 std :: string sSource ; std :: cout << sSource ; \u8f93\u51fa\uff1a 1 string::string(const string& strString) \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002\u8be5\u6784\u9020\u51fd\u6570\u57fa\u4e8e\u4e00\u4e2a\u5b57\u7b26\u4e32 strString \u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u3002 \u4f8b\u5b50\uff1a 1 2 3 std :: string sSource { \"my string\" }; std :: string sOutput { sSource }; std :: cout << sOutput ; \u8f93\u51fa\uff1a 1 my string string::string(const string& strString, size_type unIndex) string::string(const string& strString, size_type unIndex, size_type unLength) \u8be5\u6784\u9020\u51fd\u6570\u57fa\u4e8e strString \u5b57\u7b26\u4e32\u4e2d\u6700\u591a unLength \u4e2a\u5b57\u7b26\uff08\u4ece unIndex \u5f00\u59cb\uff09\u6784\u9020\u65b0\u7684\u5b57\u7b26\u4e32\u3002\u5982\u679c\u9047\u5230\u7ed3\u675f\u7b26\uff0c\u5219\u505c\u6b62\u62f7\u8d1d\uff0c\u5373\u4f7f\u8fd8\u6ca1\u6709\u5230 unLength \u4e2a\u5b57\u7b26\uff1b \u5982\u679c\u6ca1\u6709 unLength \u4e2a\u5b57\u7b26\uff0c\u5219\u4f1a\u4ece unIndex \u5f00\u59cb\u62f7\u8d1d\u5168\u90e8\u5b57\u7b26\uff1b \u5982\u679c unIndex \u6bd4\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u8fd8\u957f\uff0c\u5219\u6216\u629b\u51fa out_of_range \u5f02\u5e38\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 std :: string sSource { \"my string\" }; std :: string sOutput { sSource , 3 }; std :: cout << sOutput << '\\n' ; std :: string sOutput2 ( sSource , 3 , 4 ); std :: cout << sOutput2 << '\\n' ; \u8f93\u51fa\uff1a 1 2 string stri string::string(const char* szCString) \u8be5\u6784\u9020\u51fd\u6570\u4f1a\u57fa\u4e8e\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 szCString \u6784\u9020\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\uff0c\u4f46\u662f\u4e0d\u4f1a\u5305\u542b\u7ed3\u675f\u7b26\uff1b \u5982\u679c\u7ed3\u679c\u4f1a\u8d85\u8fc7\u5b57\u7b26\u4e32\u7684\u6700\u5927\u957f\u5ea6\uff0c\u5219\u4f1a\u629b\u51fa length_error \u5f02\u5e38\uff1b \u8b66\u544a\u26a0\ufe0f \uff1a szCString \u4e0d\u80fd\u662f NULL\u3002 \u4f8b\u5b50\uff1a 1 2 3 const char * szSource { \"my string\" }; std :: string sOutput { szSource }; std :: cout << sOutput << '\\n' ; \u8f93\u51fa\uff1a 1 my string string::string(const char* szCString, size_type unLength) This constructor creates a new string from the first unLength chars from the C-style string szCString. If the resulting size exceeds the maximum string length, the length_error exception will be thrown. Warning: For this function only, NULLs are not treated as end-of-string characters in szCString! This means it is possible to read off the end of your string if unLength is too big. Be careful not to overflow your string buffer! \u4f8b\u5b50\uff1a 1 2 3 const char * szSource { \"my string\" }; std :: string sOutput ( szSource , 4 ); std :: cout << sOutput << '\\n' ; \u8f93\u51fa\uff1a 1 my s string::string(size_type nNum, char chChar) \u8be5\u6784\u9020\u51fd\u6570\u57fa\u4e8e\u67d0\u4e2a\u5b57\u7b26\uff0c\u5c06\u5176\u91cd\u590d chChar \u6b21\u521b\u5efa\u51fa\u65b0\u7684\u5b57\u7b26\u4e32\uff1b \u5982\u679c\u7ed3\u679c\u7684\u957f\u5ea6\u8d85\u8fc7\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u5219\u629b\u51fa length_error \u5f02\u5e38\u3002 \u4f8b\u5b50\uff1a 1 2 std :: string sOutput ( 4 , 'Q' ); std :: cout << sOutput << '\\n' ; \u8f93\u51fa\uff1a 1 QQQQ template string::string(InputIterator itBeg, InputIterator itEnd) \u8be5\u6784\u9020\u51fd\u6570\u57fa\u4e8e\u4e00\u4e2a\u8303\u56f4 [itBeg, itEnd) \u7684\u5b57\u7b26\u4e32\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\uff1b \u5982\u679c\u7ed3\u679c\u7684\u957f\u5ea6\u8d85\u8fc7\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u5219\u629b\u51fa length_error \u5f02\u5e38\u3002 \u6ca1\u6709\u793a\u4f8b\u4ee3\u7801\u3002\u5b83\u592a\u6666\u6da9\u4e86\uff0c\u4f60\u53ef\u80fd\u6c38\u8fdc\u4e0d\u4f1a\u7528\u5b83\u3002","title":"\u5b57\u7b26\u4e32\u6784\u9020"},{"location":"22-2-std-string-construction-and-destruction/#_2","text":"string::~string() \u6790\u6784\u51fd\u6570\uff0c\u7528\u4e8e\u9500\u6bc1\u5b57\u7b26\u4e32\u5e76\u91ca\u653e\u5185\u5b58 \u6ca1\u6709\u793a\u4f8b\u4ee3\u7801\u3002\u4f60\u4e0d\u4f1a\u663e\u793a\u5730\u8c03\u7528\u5b83\u3002","title":"\u5b57\u7b26\u4e32\u6790\u6784"},{"location":"22-2-std-string-construction-and-destruction/#_3","text":"std::string \u7c7b\u9057\u6f0f\u4e86\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u529f\u80fd\uff0c\u5373\u4ece\u6570\u5b57\u521b\u5efa\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\uff1a 1 std :: string sFour { 4 }; \u8f93\u51fa\u7ed3\u679c\uff1a 1 c:vcprojectstest2test2test.cpp(10) : error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it)' : cannot convert parameter 1 from 'int' to 'std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it' \u8fd8\u8bb0\u5f97\u6211\u8bf4\u8fc7\u5b57\u7b26\u4e32\u7c7b\u4f1a\u4ea7\u751f\u53ef\u6015\u7684\u62a5\u9519\u4fe1\u606f\u5417\uff1f\u5c31\u662f\u50cf\u4e0a\u9762\u8fd9\u6837\u3002\u8fd9\u91cc\u7684\u6709\u7528\u7684\u4fe1\u606f\u662f\uff1a 1 cannot convert parameter 1 from 'int' to 'std::basic_string \u6362\u53e5\u8bdd\u8bf4\uff0c\u5b83\u8bd5\u56fe\u5c06int\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u4f46\u5931\u8d25\u4e86\u3002 \u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 std::ostringstream \u7c7b\u3002 std::ostringstream \u53ef\u4ee5\u63a5\u53d7\u6765\u81ea\u5404\u79cd\u6765\u6e90\u7684\u8f93\u5165\uff0c\u5305\u62ec\u5b57\u7b26\u3001\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u7b49\u2026\u5b83\u8fd8\u80fd\u591f\u8f93\u51fa\u5b57\u7b26\u4e32(\u901a\u8fc7 \u63d0\u53d6\u8fd0\u7b97\u7b26 \uff0c\u6216\u901a\u8fc7 str() \u51fd\u6570)\u3002\u6709\u5173 std::ostringstream \u7684\u66f4\u591a\u4fe1\u606f\uff0c\u53c2\u8003 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b \u3002 \u4ece\u4e0d\u540c\u7c7b\u578b\u7684\u8f93\u5165\u521b\u5efa\u5b57\u7b26\u4e32\u7684\u4f8b\u5b50\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include #include #include template < typename T > inline std :: string ToString ( T tX ) { std :: ostringstream oStream ; oStream << tX ; return oStream . str (); } \u4e0b\u9762\u662f\u4e00\u4e9b\u6d4b\u8bd5\u5b83\u7684\u793a\u4f8b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 int main () { std :: string sFour { ToString ( 4 ) }; std :: string sSixPointSeven { ToString ( 6.7 ) }; std :: string sA { ToString ( 'A' ) }; std :: cout << sFour << '\\n' ; std :: cout << sSixPointSeven << '\\n' ; std :: cout << sA << '\\n' ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 6.7 A \u6ce8\u610f\uff0c\u8fd9\u91cc\u6211\u4eec\u5ffd\u7565\u4e86\u9519\u8bef\u68c0\u67e5\u3002\u5c06 tX \u63d2\u5165 oStream \u4e2d\u53ef\u80fd\u4f1a\u5931\u8d25\uff0c\u5982\u679c\u8f6c\u6362\u5931\u8d25\u5219\u5e94\u8be5\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u3002 \u76f8\u5173\u5185\u5bb9 \u6807\u51c6\u5e93\u8fd8\u5305\u542b\u4e00\u4e2a\u540d\u4e3a std::to_string() \u7684\u51fd\u6570\uff0c\u53ef\u7528\u4e8e\u5c06\u5b57\u7b26\u548c\u6570\u5b57\u8f6c\u6362\u4e3a std::string \u3002\u867d\u7136\u8fd9\u662f\u57fa\u672c\u573a\u5408\u4e2d\u66f4\u7b80\u5355\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u4f46 std::to_string \u7684\u8f93\u51fa\u53ef\u80fd\u4e0e\u4e0a\u9762 std::cout \u6216 out ToString() \u51fd\u6570\u7684\u8f93\u51fa\u4e0d\u540c\u3002\u5176\u4e2d\u4e00\u4e9b\u5dee\u5f02\u88ab\u8bb0\u5f55 \u8fd9\u91cc \u3002","title":"\u4ece\u6570\u5b57\u6784\u5efa\u5b57\u7b26\u4e32"},{"location":"22-2-std-string-construction-and-destruction/#_4","text":"\u548c\u4e0a\u9762\u7684\u89e3\u51b3\u65b9\u6848\u7c7b\u4f3c\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include #include template < typename T > inline bool FromString ( const std :: string & sString , T & tX ) { std :: istringstream iStream ( sString ); return ! ( iStream >> tX ). fail (); // \u5c06\u503c\u63d0\u53d6\u5230 tX \u5e76\u8fd4\u56de\u662f\u5426\u6210\u529f } \u4e0b\u9762\u662f\u4e00\u4e9b\u6d4b\u8bd5\u5b83\u7684\u793a\u4f8b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 int main () { double dX ; if ( FromString ( \"3.4\" , dX )) std :: cout << dX << '\\n' ; if ( FromString ( \"ABC\" , dX )) std :: cout << dX << '\\n' ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 3.4 \u6ce8\u610f\uff0c\u7b2c\u4e8c\u6b21\u8f6c\u6362\u5931\u8d25\u5e76\u8fd4\u56de\u4e86 false \u3002","title":"\u4ece\u5b57\u7b26\u4e32\u521b\u5efa\u6570\u5b57"},{"location":"22-3-std-string-length-and-capacity/","tags":["string"],"text":"Key Takeaway \u4e00\u65e6\u521b\u5efa\u4e86\u5b57\u7b26\u4e32\uff0c\u77e5\u9053\u5b83\u4eec\u7684\u957f\u5ea6\u901a\u5e38\u662f\u5f88\u6709\u7528\u7684\u3002\u6211\u4eec\u8fd8\u5c06\u8ba8\u8bba\u5c06 std::string \u8f6c\u6362\u56deC\u98ce\u683c\u5b57\u7b26\u4e32\u7684\u5404\u79cd\u65b9\u6cd5\uff0c\u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u5728\u5c06\u5176\u7528\u5728\u671f\u671b char* \u7c7b\u578b\u5b57\u7b26\u4e32\u7684\u51fd\u6570\u4e2d\u4f7f\u7528\u5b83\u4eec\u3002 \u5b57\u7b26\u4e32\u7684\u957f\u5ea6 \u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u975e\u5e38\u7b80\u5355\u2014\u2014\u5373\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u4e2a\u6570\u3002\u6709\u4e24\u4e2a\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u786e\u5b9a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff1a size_type string::length() const size_type string::size() const \u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u53ef\u4ee5\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u4e2a\u6570\uff0c\u5305\u62ec\u7ed3\u675f\u7b26\u3002 \u4f8b\u5b50\uff1a 1 2 std :: string s { \"012345678\" }; std :: cout << s . length () << '\\n' ; \u8f93\u51fa\uff1a 1 9 \u5c3d\u7ba1\u4f7f\u7528 length() \u5224\u65ad\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u6709\u5b57\u7b26\u4e5f\u662f\u53ef\u884c\u7684\uff0c\u4f46\u662f\u6700\u4f73\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 empty() \u51fd\u6570\uff1a bool string::empty() const \u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u6ca1\u6709\u5b57\u7b26\uff0c\u5219\u8fd4\u56de true \uff0c\u5426\u5219\u8fd4\u56de false \u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string string1 { \"Not Empty\" }; std :: cout << ( string1 . empty () ? \"true\" : \"false\" ) << '\\n' ; std :: string string2 ; // empty std :: cout << ( string2 . empty () ? \"true\" : \"false\" ) << '\\n' ; \u8f93\u51fa\uff1a 1 2 false true \u8fd8\u6709\u4e00\u4e2a\u4e0e\u5927\u5c0f\u76f8\u5173\u7684\u51fd\u6570\uff0c\u867d\u7136\u53ef\u80fd\u6c38\u8fdc\u90fd\u7528\u4e0d\u5230\uff0c\u4f46\u4e3a\u4e86\u5b8c\u6574\uff0c\u6211\u4eec\u51b3\u5b9a\u8fd8\u662f\u5c06\u5176\u5217\u5728\u8fd9\u91cc\uff1a size_type string::max_size() const \u8fd4\u56de\u5b57\u7b26\u4e32\u5141\u8bb8\u7684\u6700\u5927\u5b57\u7b26\u6570\uff1b \u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u548c\u7cfb\u7edf\u67b6\u6784\u7684\u4e0d\u540c\uff0c\u8be5\u503c\u4f1a\u6709\u6240\u4e0d\u540c\u3002 \u4f8b\u5b50\uff1a 1 2 std :: string s { \"MyString\" }; std :: cout << s . max_size () << '\\n' ; \u8f93\u51fa\uff1a 1 4294967294 \u5b57\u7b26\u4e32\u7684\u5bb9\u91cf \u5b57\u7b26\u4e32\u7684\u5bb9\u91cf\u53cd\u6620\u4e86\u8be5\u5b57\u7b26\u4e32\u5206\u914d\u591a\u5c11\u5185\u5b58\u6765\u4fdd\u5b58\u5176\u5185\u5bb9\u3002\u8be5\u503c\u7528\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u4e0d\u5305\u62ec\u7ec8\u6b62\u7b26\u3002\u4f8b\u5982\uff0c\u5bb9\u91cf\u4e3a8\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u5bb9\u7eb38\u4e2a\u5b57\u7b26\u3002 size_type string::capacity() const \u8fd4\u56de\u5b57\u7b26\u4e32\u5728\u4e0d\u91cd\u65b0\u5206\u914d\u7684\u60c5\u51b5\u4e0b\u53ef\u4ee5\u5bb9\u7eb3\u7684\u5b57\u7b26\u6570\u3002 \u4f8b\u5b50\uff1a 1 2 3 std :: string s { \"01234567\" }; std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; \u8f93\u51fa\uff1a 1 2 Length: 8 Capacity: 15 \u6ce8\u610f\uff0c\u8be5\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf\u5927\u4e8e\u5176\u957f\u5ea6\uff01\u867d\u7136\u6211\u4eec\u7684\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f8\uff0c\u4f46\u8be5\u5b57\u7b26\u4e32\u5b9e\u9645\u4e0a\u5206\u914d\u4e86\u8db3\u591f\u5b58\u653e15\u4e2a\u5b57\u7b26\u7684\u5185\u5b58\uff01\u4e3a\u4ec0\u4e48\u8981\u8fd9\u6837\u505a? \u8fd9\u91cc\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u7528\u6237\u5e0c\u671b\u5728\u5b57\u7b26\u4e32\u4e2d\u653e\u5165\u7684\u5b57\u7b26\u591a\u4e8e\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf\uff0c\u5219\u5b57\u7b26\u4e32\u9700\u8981\u91cd\u65b0\u5206\u914d\u66f4\u5927\u7684\u5bb9\u91cf\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u548c\u5bb9\u91cf\u90fd\u4e3a8\uff0c\u90a3\u4e48\u5411\u5b57\u7b26\u4e32\u4e2d\u6dfb\u52a0\u4efb\u4f55\u5b57\u7b26\u90fd\u5c06\u5f3a\u5236\u91cd\u65b0\u5206\u914d\u3002\u901a\u8fc7\u4f7f\u5bb9\u91cf\u5927\u4e8e\u5b9e\u9645\u5b57\u7b26\u4e32\uff0c\u8fd9\u4e3a\u7528\u6237\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7f13\u51b2\u7a7a\u95f4\uff0c\u4ee5\u4fbf\u5728\u9700\u8981\u8fdb\u884c\u91cd\u65b0\u5206\u914d\u4e4b\u524d\u80fd\u591f\u5b58\u653e\u4e00\u4e9b\u989d\u5916\u7684\u5b57\u7b26\u3002 \u4e8b\u5b9e\u8bc1\u660e\uff0c\u518d\u5206\u914d\u6709\u4ee5\u4e0b\u7f3a\u9677\uff1a \u9996\u5148\uff0c\u91cd\u65b0\u5206\u914d\u5b57\u7b26\u4e32\u5f00\u9500\u8f83\u5927\u3002\u9996\u5148\uff0c\u5fc5\u987b\u5206\u914d\u65b0\u7684\u5185\u5b58\u3002\u7136\u540e\u5fc5\u987b\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u590d\u5236\u5230\u65b0\u7684\u5185\u5b58\u4e2d\u3002\u5982\u679c\u5b57\u7b26\u4e32\u5f88\u5927\uff0c\u8fd9\u53ef\u80fd\u9700\u8981\u5f88\u957f\u65f6\u95f4\u3002\u6700\u540e\uff0c\u65e7\u7684\u5185\u5b58\u5fc5\u987b\u88ab\u91ca\u653e\u3002\u5982\u679c\u4f60\u6b63\u5728\u8fdb\u884c\u591a\u6b21\u91cd\u65b0\u5206\u914d\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4f1a\u6781\u5927\u5730\u62d6\u7d2f\u7a0b\u5e8f\u6027\u80fd\u3002 \u5176\u6b21\uff0c \u6bcf\u5f53\u4e00\u4e2a\u5b57\u7b26\u4e32\u88ab\u91cd\u65b0\u5206\u914d\u65f6\uff0c\u8be5\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u5c06\u653e\u7f6e\u5230\u4e00\u4e2a\u65b0\u7684\u5185\u5b58\u5730\u5740\u3002\u8fd9\u610f\u5473\u7740\u5bf9\u5b57\u7b26\u4e32\u7684\u6240\u6709\u5f15\u7528\u3001\u6307\u9488\u548c\u8fed\u4ee3\u5668\u90fd\u4f1a\u65e0\u6548\uff01 \u6ce8\u610f\uff0c\u5206\u914d\u7ed9\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf\u5e76\u4e0d\u603b\u662f\u5927\u4e8e\u957f\u5ea6\u3002\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 std :: string s { \"0123456789abcde\" }; std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 Length: 15 Capacity: 15 \uff08\u5b9e\u9645\u7ed3\u679c\u6839\u636e\u7f16\u8bd1\u5668\u7684\u4e0d\u540c\u4e5f\u53ef\u80fd\u4f1a\u4e0d\u540c\uff09 \u63a5\u4e0b\u6765\uff0c\u5728\u5b57\u7b26\u4e32\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5b57\u7b26\u5e76\u89c2\u5bdf\u5176\u5bb9\u91cf\u7684\u53d8\u5316: 1 2 3 4 5 6 7 8 std :: string s ( \"0123456789abcde\" ); std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; // Now add a new character s += \"f\" ; std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 Length: 15 Capacity: 15 Length: 16 Capacity: 31 void string::reserve() void string::reserve(size_type unSize) \u6b64\u51fd\u6570\u7684\u7b2c\u4e8c\u79cd\u5f62\u5f0f\u5c06\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf\u8bbe\u7f6e\u4e3a\u81f3\u5c11 unSize (\u53ef\u4ee5\u66f4\u5927)\u3002\u6ce8\u610f\uff0c\u8fd9\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u91cd\u65b0\u5206\u914d\u3002 \u5982\u679c\u8c03\u7528\u51fd\u6570\u7684\u7b2c\u4e00\u79cd\u5f62\u5f0f\uff0c\u6216\u8005\u8c03\u7528\u7b2c\u4e8c\u4e2a\u5473\u9053\u65f6 unSize \u5c0f\u4e8e\u5f53\u524d\u5bb9\u91cf\uff0c\u51fd\u6570\u5c06\u5c1d\u8bd5\u7f29\u5c0f\u5bb9\u91cf\u4ee5\u5339\u914d\u957f\u5ea6\u3002\u6839\u636e\u6267\u884c\u60c5\u51b5\uff0c\u53ef\u80fd\u4f1a\u5ffd\u7565\u8fd9\u4e2a\u7f29\u5c0f\u5bb9\u91cf\u7684\u8981\u6c42\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 std :: string s { \"01234567\" }; std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; s . reserve ( 200 ); std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; s . reserve (); std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; \u8f93\u51fa\uff1a 1 2 3 4 5 6 Length: 8 Capacity: 15 Length: 8 Capacity: 207 Length: 8 Capacity: 207 \u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u4e24\u4e2a\u6709\u8da3\u7684\u73b0\u8c61\u3002\u9996\u5148\uff0c\u867d\u7136\u6211\u4eec\u8981\u6c42\u7684\u5bb9\u91cf\u662f200\uff0c\u4f46\u5b9e\u9645\u4e0a\u6211\u4eec\u5f97\u5230\u7684\u5bb9\u91cf\u662f207\u3002\u5bb9\u91cf\u603b\u662f\u4fdd\u8bc1\u81f3\u5c11\u4e0e\u4f60\u7684\u8bf7\u6c42\u4e00\u6837\u5927\uff0c\u4f46\u4e5f\u53ef\u80fd\u66f4\u5927\u3002\u7136\u540e\u6211\u4eec\u8bf7\u6c42\u5bb9\u91cf\u66f4\u6539\u4ee5\u9002\u5e94\u5b57\u7b26\u4e32\u3002\u8be5\u8bf7\u6c42\u88ab\u5ffd\u7565\uff0c\u5bb9\u91cf\u5e76\u6ca1\u6709\u6539\u53d8\u3002 \u5982\u679c\u4f60\u63d0\u524d\u77e5\u9053\u4f60\u5c06\u901a\u8fc7\u5927\u91cf\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u6765\u6784\u9020\u4e00\u4e2a\u5f88\u957f\u5b57\u7b26\u4e32\uff08\u8fd9\u4f1a\u589e\u52a0\u5b57\u7b26\u4e32\u7684\u5927\u5c0f\uff09\uff0c\u5219\u5e94\u8be5\u5728\u5f00\u59cb\u65f6\u5c31\u9884\u7559\u8db3\u591f\u7684\u5bb9\u91cf\u6765\u907f\u514d\u91cd\u65b0\u5206\u914d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include #include // for rand() and srand() #include // for time() int main () { std :: srand ( std :: time ( nullptr )); // seed random number generator std :: string s {}; // length 0 s . reserve ( 64 ); // reserve 64 characters // Fill string up with random lower case characters for ( int count { 0 }; count < 64 ; ++ count ) s += 'a' + std :: rand () % 26 ; std :: cout << s ; } \u8fd9\u4e2a\u7a0b\u5e8f\u7684\u7ed3\u679c\u6bcf\u6b21\u90fd\u4f1a\u6539\u53d8\uff0c\u5176\u4e2d\u4e00\u6b21\u7684\u7ed3\u679c\u4e3a\uff1a 1 wzpzujwuaokbakgijqdawvzjqlgcipiiuuxhyfkdppxpyycvytvyxwqsbtielxpy \u4e3a\u4e86\u907f\u514d\u91cd\u65b0\u5206\u914d\uff0c\u8fd9\u91cc\u6211\u4eec\u5148\u8bbe\u7f6e\u4e00\u6b21\u5bb9\u91cf\uff0c\u7136\u540e\u586b\u5145\u5b57\u7b26\u4e32\u3002\u5f53\u901a\u8fc7\u8fde\u63a5\u6784\u9020\u957f\u5b57\u7b26\u4e32\u65f6\uff0c\u5176\u6027\u80fd\u5dee\u5f02\u662f\u975e\u5e38\u660e\u663e\u7684\u3002","title":"22.3 - std-string \u7684\u957f\u5ea6\u548c\u5bb9\u91cf"},{"location":"22-3-std-string-length-and-capacity/#_1","text":"\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u975e\u5e38\u7b80\u5355\u2014\u2014\u5373\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u4e2a\u6570\u3002\u6709\u4e24\u4e2a\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u786e\u5b9a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff1a size_type string::length() const size_type string::size() const \u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u53ef\u4ee5\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u4e2a\u6570\uff0c\u5305\u62ec\u7ed3\u675f\u7b26\u3002 \u4f8b\u5b50\uff1a 1 2 std :: string s { \"012345678\" }; std :: cout << s . length () << '\\n' ; \u8f93\u51fa\uff1a 1 9 \u5c3d\u7ba1\u4f7f\u7528 length() \u5224\u65ad\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u6709\u5b57\u7b26\u4e5f\u662f\u53ef\u884c\u7684\uff0c\u4f46\u662f\u6700\u4f73\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 empty() \u51fd\u6570\uff1a bool string::empty() const \u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u6ca1\u6709\u5b57\u7b26\uff0c\u5219\u8fd4\u56de true \uff0c\u5426\u5219\u8fd4\u56de false \u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string string1 { \"Not Empty\" }; std :: cout << ( string1 . empty () ? \"true\" : \"false\" ) << '\\n' ; std :: string string2 ; // empty std :: cout << ( string2 . empty () ? \"true\" : \"false\" ) << '\\n' ; \u8f93\u51fa\uff1a 1 2 false true \u8fd8\u6709\u4e00\u4e2a\u4e0e\u5927\u5c0f\u76f8\u5173\u7684\u51fd\u6570\uff0c\u867d\u7136\u53ef\u80fd\u6c38\u8fdc\u90fd\u7528\u4e0d\u5230\uff0c\u4f46\u4e3a\u4e86\u5b8c\u6574\uff0c\u6211\u4eec\u51b3\u5b9a\u8fd8\u662f\u5c06\u5176\u5217\u5728\u8fd9\u91cc\uff1a size_type string::max_size() const \u8fd4\u56de\u5b57\u7b26\u4e32\u5141\u8bb8\u7684\u6700\u5927\u5b57\u7b26\u6570\uff1b \u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u548c\u7cfb\u7edf\u67b6\u6784\u7684\u4e0d\u540c\uff0c\u8be5\u503c\u4f1a\u6709\u6240\u4e0d\u540c\u3002 \u4f8b\u5b50\uff1a 1 2 std :: string s { \"MyString\" }; std :: cout << s . max_size () << '\\n' ; \u8f93\u51fa\uff1a 1 4294967294","title":"\u5b57\u7b26\u4e32\u7684\u957f\u5ea6"},{"location":"22-3-std-string-length-and-capacity/#_2","text":"\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf\u53cd\u6620\u4e86\u8be5\u5b57\u7b26\u4e32\u5206\u914d\u591a\u5c11\u5185\u5b58\u6765\u4fdd\u5b58\u5176\u5185\u5bb9\u3002\u8be5\u503c\u7528\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u4e0d\u5305\u62ec\u7ec8\u6b62\u7b26\u3002\u4f8b\u5982\uff0c\u5bb9\u91cf\u4e3a8\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u5bb9\u7eb38\u4e2a\u5b57\u7b26\u3002 size_type string::capacity() const \u8fd4\u56de\u5b57\u7b26\u4e32\u5728\u4e0d\u91cd\u65b0\u5206\u914d\u7684\u60c5\u51b5\u4e0b\u53ef\u4ee5\u5bb9\u7eb3\u7684\u5b57\u7b26\u6570\u3002 \u4f8b\u5b50\uff1a 1 2 3 std :: string s { \"01234567\" }; std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; \u8f93\u51fa\uff1a 1 2 Length: 8 Capacity: 15 \u6ce8\u610f\uff0c\u8be5\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf\u5927\u4e8e\u5176\u957f\u5ea6\uff01\u867d\u7136\u6211\u4eec\u7684\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f8\uff0c\u4f46\u8be5\u5b57\u7b26\u4e32\u5b9e\u9645\u4e0a\u5206\u914d\u4e86\u8db3\u591f\u5b58\u653e15\u4e2a\u5b57\u7b26\u7684\u5185\u5b58\uff01\u4e3a\u4ec0\u4e48\u8981\u8fd9\u6837\u505a? \u8fd9\u91cc\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u7528\u6237\u5e0c\u671b\u5728\u5b57\u7b26\u4e32\u4e2d\u653e\u5165\u7684\u5b57\u7b26\u591a\u4e8e\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf\uff0c\u5219\u5b57\u7b26\u4e32\u9700\u8981\u91cd\u65b0\u5206\u914d\u66f4\u5927\u7684\u5bb9\u91cf\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u548c\u5bb9\u91cf\u90fd\u4e3a8\uff0c\u90a3\u4e48\u5411\u5b57\u7b26\u4e32\u4e2d\u6dfb\u52a0\u4efb\u4f55\u5b57\u7b26\u90fd\u5c06\u5f3a\u5236\u91cd\u65b0\u5206\u914d\u3002\u901a\u8fc7\u4f7f\u5bb9\u91cf\u5927\u4e8e\u5b9e\u9645\u5b57\u7b26\u4e32\uff0c\u8fd9\u4e3a\u7528\u6237\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7f13\u51b2\u7a7a\u95f4\uff0c\u4ee5\u4fbf\u5728\u9700\u8981\u8fdb\u884c\u91cd\u65b0\u5206\u914d\u4e4b\u524d\u80fd\u591f\u5b58\u653e\u4e00\u4e9b\u989d\u5916\u7684\u5b57\u7b26\u3002 \u4e8b\u5b9e\u8bc1\u660e\uff0c\u518d\u5206\u914d\u6709\u4ee5\u4e0b\u7f3a\u9677\uff1a \u9996\u5148\uff0c\u91cd\u65b0\u5206\u914d\u5b57\u7b26\u4e32\u5f00\u9500\u8f83\u5927\u3002\u9996\u5148\uff0c\u5fc5\u987b\u5206\u914d\u65b0\u7684\u5185\u5b58\u3002\u7136\u540e\u5fc5\u987b\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u590d\u5236\u5230\u65b0\u7684\u5185\u5b58\u4e2d\u3002\u5982\u679c\u5b57\u7b26\u4e32\u5f88\u5927\uff0c\u8fd9\u53ef\u80fd\u9700\u8981\u5f88\u957f\u65f6\u95f4\u3002\u6700\u540e\uff0c\u65e7\u7684\u5185\u5b58\u5fc5\u987b\u88ab\u91ca\u653e\u3002\u5982\u679c\u4f60\u6b63\u5728\u8fdb\u884c\u591a\u6b21\u91cd\u65b0\u5206\u914d\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4f1a\u6781\u5927\u5730\u62d6\u7d2f\u7a0b\u5e8f\u6027\u80fd\u3002 \u5176\u6b21\uff0c \u6bcf\u5f53\u4e00\u4e2a\u5b57\u7b26\u4e32\u88ab\u91cd\u65b0\u5206\u914d\u65f6\uff0c\u8be5\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u5c06\u653e\u7f6e\u5230\u4e00\u4e2a\u65b0\u7684\u5185\u5b58\u5730\u5740\u3002\u8fd9\u610f\u5473\u7740\u5bf9\u5b57\u7b26\u4e32\u7684\u6240\u6709\u5f15\u7528\u3001\u6307\u9488\u548c\u8fed\u4ee3\u5668\u90fd\u4f1a\u65e0\u6548\uff01 \u6ce8\u610f\uff0c\u5206\u914d\u7ed9\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf\u5e76\u4e0d\u603b\u662f\u5927\u4e8e\u957f\u5ea6\u3002\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 std :: string s { \"0123456789abcde\" }; std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 Length: 15 Capacity: 15 \uff08\u5b9e\u9645\u7ed3\u679c\u6839\u636e\u7f16\u8bd1\u5668\u7684\u4e0d\u540c\u4e5f\u53ef\u80fd\u4f1a\u4e0d\u540c\uff09 \u63a5\u4e0b\u6765\uff0c\u5728\u5b57\u7b26\u4e32\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5b57\u7b26\u5e76\u89c2\u5bdf\u5176\u5bb9\u91cf\u7684\u53d8\u5316: 1 2 3 4 5 6 7 8 std :: string s ( \"0123456789abcde\" ); std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; // Now add a new character s += \"f\" ; std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 Length: 15 Capacity: 15 Length: 16 Capacity: 31 void string::reserve() void string::reserve(size_type unSize) \u6b64\u51fd\u6570\u7684\u7b2c\u4e8c\u79cd\u5f62\u5f0f\u5c06\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf\u8bbe\u7f6e\u4e3a\u81f3\u5c11 unSize (\u53ef\u4ee5\u66f4\u5927)\u3002\u6ce8\u610f\uff0c\u8fd9\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u91cd\u65b0\u5206\u914d\u3002 \u5982\u679c\u8c03\u7528\u51fd\u6570\u7684\u7b2c\u4e00\u79cd\u5f62\u5f0f\uff0c\u6216\u8005\u8c03\u7528\u7b2c\u4e8c\u4e2a\u5473\u9053\u65f6 unSize \u5c0f\u4e8e\u5f53\u524d\u5bb9\u91cf\uff0c\u51fd\u6570\u5c06\u5c1d\u8bd5\u7f29\u5c0f\u5bb9\u91cf\u4ee5\u5339\u914d\u957f\u5ea6\u3002\u6839\u636e\u6267\u884c\u60c5\u51b5\uff0c\u53ef\u80fd\u4f1a\u5ffd\u7565\u8fd9\u4e2a\u7f29\u5c0f\u5bb9\u91cf\u7684\u8981\u6c42\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 std :: string s { \"01234567\" }; std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; s . reserve ( 200 ); std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; s . reserve (); std :: cout << \"Length: \" << s . length () << '\\n' ; std :: cout << \"Capacity: \" << s . capacity () << '\\n' ; \u8f93\u51fa\uff1a 1 2 3 4 5 6 Length: 8 Capacity: 15 Length: 8 Capacity: 207 Length: 8 Capacity: 207 \u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u4e24\u4e2a\u6709\u8da3\u7684\u73b0\u8c61\u3002\u9996\u5148\uff0c\u867d\u7136\u6211\u4eec\u8981\u6c42\u7684\u5bb9\u91cf\u662f200\uff0c\u4f46\u5b9e\u9645\u4e0a\u6211\u4eec\u5f97\u5230\u7684\u5bb9\u91cf\u662f207\u3002\u5bb9\u91cf\u603b\u662f\u4fdd\u8bc1\u81f3\u5c11\u4e0e\u4f60\u7684\u8bf7\u6c42\u4e00\u6837\u5927\uff0c\u4f46\u4e5f\u53ef\u80fd\u66f4\u5927\u3002\u7136\u540e\u6211\u4eec\u8bf7\u6c42\u5bb9\u91cf\u66f4\u6539\u4ee5\u9002\u5e94\u5b57\u7b26\u4e32\u3002\u8be5\u8bf7\u6c42\u88ab\u5ffd\u7565\uff0c\u5bb9\u91cf\u5e76\u6ca1\u6709\u6539\u53d8\u3002 \u5982\u679c\u4f60\u63d0\u524d\u77e5\u9053\u4f60\u5c06\u901a\u8fc7\u5927\u91cf\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u6765\u6784\u9020\u4e00\u4e2a\u5f88\u957f\u5b57\u7b26\u4e32\uff08\u8fd9\u4f1a\u589e\u52a0\u5b57\u7b26\u4e32\u7684\u5927\u5c0f\uff09\uff0c\u5219\u5e94\u8be5\u5728\u5f00\u59cb\u65f6\u5c31\u9884\u7559\u8db3\u591f\u7684\u5bb9\u91cf\u6765\u907f\u514d\u91cd\u65b0\u5206\u914d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include #include // for rand() and srand() #include // for time() int main () { std :: srand ( std :: time ( nullptr )); // seed random number generator std :: string s {}; // length 0 s . reserve ( 64 ); // reserve 64 characters // Fill string up with random lower case characters for ( int count { 0 }; count < 64 ; ++ count ) s += 'a' + std :: rand () % 26 ; std :: cout << s ; } \u8fd9\u4e2a\u7a0b\u5e8f\u7684\u7ed3\u679c\u6bcf\u6b21\u90fd\u4f1a\u6539\u53d8\uff0c\u5176\u4e2d\u4e00\u6b21\u7684\u7ed3\u679c\u4e3a\uff1a 1 wzpzujwuaokbakgijqdawvzjqlgcipiiuuxhyfkdppxpyycvytvyxwqsbtielxpy \u4e3a\u4e86\u907f\u514d\u91cd\u65b0\u5206\u914d\uff0c\u8fd9\u91cc\u6211\u4eec\u5148\u8bbe\u7f6e\u4e00\u6b21\u5bb9\u91cf\uff0c\u7136\u540e\u586b\u5145\u5b57\u7b26\u4e32\u3002\u5f53\u901a\u8fc7\u8fde\u63a5\u6784\u9020\u957f\u5b57\u7b26\u4e32\u65f6\uff0c\u5176\u6027\u80fd\u5dee\u5f02\u662f\u975e\u5e38\u660e\u663e\u7684\u3002","title":"\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf"},{"location":"22-4-std-string-character-access-and-conversion-to-C-style-arrays/","tags":["string"],"text":"Key Takeaway \u8bbf\u95ee\u5b57\u7b26 \u8bbf\u95ee\u5b57\u7b26\u4e5f\u6709\u4e24\u4e2a\u6781\u5176\u7c7b\u4f3c\u7684\u65b9\u6cd5\u3002\u6700\u7b80\u5355\u4e5f\u662f\u6700\u5757\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 \uff1a char& string::operator[] (size_type nIndex) const char& string::operator[] (size_type nIndex) const \u51fd\u6570\u8fd4\u56de\u4f4d\u4e8e\u7d22\u5f15 nIndex \u5904\u7684\u5b57\u7b26\uff1b \u4f20\u5165\u975e\u6cd5\u7d22\u5f15\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \uff1b \u56e0\u4e3a\u8fd4\u56de\u7c7b\u578b\u662f char& \u6240\u4ee5\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u4fee\u6539\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sSource { \"abcdefg\" }; std :: cout << sSource [ 5 ] << '\\n' ; sSource [ 5 ] = 'X' ; std :: cout << sSource << '\\n' ; \u8f93\u51fa\uff1a 1 2 f abcdeXg \u6b64\u5916\uff0c\u8fd8\u6709\u4e00\u4e2a\u975e\u64cd\u4f5c\u7b26\u7684\u7248\u672c\u3002\u8be5\u7248\u672c\u7684\u51fd\u6570\u901f\u5ea6\u4f1a\u6162\u4e00\u4e9b\uff0c\u56e0\u4e3a\u5b83==\u4f1a\u68c0\u67e5\u6570\u7ec4\u4e0b\u6807\u662f\u5426\u5408\u6cd5==\u3002\u5982\u679c\u4f60\u4e0d\u786e\u5b9a\u7d22\u5f15 nIndex \u662f\u5426\u4e00\u5b9a\u6709\u6548\uff0c\u5219\u6700\u597d\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u6765\u8bbf\u95ee\uff1a char& string::at (size_type nIndex) const char& string::at (size_type nIndex) const \u51fd\u6570\u8fd4\u56de\u4f4d\u4e8e\u7d22\u5f15 nIndex \u5904\u7684\u5b57\u7b26\uff1b \u4f20\u5165\u975e\u6cd5\u7d22\u5f15\u4f1a\u5bfc\u81f4 out_of_range \u5f02\u5e38\uff1b \u56e0\u4e3a\u8fd4\u56de\u7c7b\u578b\u662f char& \u6240\u4ee5\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u4fee\u6539\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sSource { \"abcdefg\" }; std :: cout << sSource . at ( 5 ) << '\\n' ; sSource . at ( 5 ) = 'X' ; std :: cout << sSource << '\\n' ; \u8f93\u51fa 1 2 f abcdeXg \u8f6c\u6362\u4e3aC\u98ce\u683c\u6570\u7ec4 \u5f88\u591a\u51fd\u6570\uff08\u5305\u62ec\u6240\u6709C\u8bed\u8a00\u51fd\u6570\uff09\u671f\u671b\u7684\u8f93\u5165\u662fC\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u800c\u4e0d\u662f std::string \u3002\u56e0\u6b64\uff0c std::string \u63d0\u4f9b\u4e86\u4e09\u79cd\u5c06 std::string \u8f6c\u6362\u4e3aC\u8bed\u8a00\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff1a const char* string::c_str () const \u5c06\u5b57\u7b26\u4e32\u5185\u5bb9\u4f5c\u4e3aC\u98ce\u683c\u5b57\u7b26\u4e32\u5e38\u91cf\u8fd4\u56de\uff1b \u7ed3\u5c3e\u4f1a\u6dfb\u52a0\u7ed3\u675f\u7b26\uff1b \u8be5C\u98ce\u683c\u5b57\u7b26\u4e32\u5f52 std::string \u6240\u6709\u4e14\u4e0d\u5e94\u8be5\u88ab\u624b\u52a8\u91ca\u653e\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 #include std :: string sSource { \"abcdefg\" }; std :: cout << std :: strlen ( sSource . c_str ()); \u8f93\u51fa\uff1a 1 7 const char* string::data () const \u5c06\u5b57\u7b26\u4e32\u5185\u5bb9\u4f5c\u4e3aC\u98ce\u683c\u5b57\u7b26\u4e32\u5e38\u91cf\u8fd4\u56de\uff1b \u7ed3\u5c3e\u4f1a\u6dfb\u52a0\u7ed3\u675f\u7b26\uff1b\u8be5\u51fd\u6570\u884c\u4e3a\u4e0e c_str() \u662f\u4e00\u6837\u7684\uff1b \u8be5C\u98ce\u683c\u5b57\u7b26\u4e32\u5f52 std::string \u6240\u6709\u4e14\u4e0d\u5e94\u8be5\u88ab\u624b\u52a8\u91ca\u653e\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 #include std :: string sSource { \"abcdefg\" }; const char * szString { \"abcdefg\" }; // memcmp compares the first n characters of two C-style strings and returns 0 if they are equal if ( std :: memcmp ( sSource . data (), szString , sSource . length ()) == 0 ) std :: cout << \"The strings are equal\" ; else std :: cout << \"The strings are not equal\" ; \u8f93\u51fa\uff1a 1 The strings are equal size_type string::copy(char* szBuf, size_type nLength, size_type nIndex = 0) const \u4ece\u5b57\u7b26\u4e32\u4e2d\u62f7\u8d1d\u81f3\u591a nLength \u4e2a\u5b57\u7b26\u5230 szBuf \uff0c\u4ece\u7d22\u5f15 nIndex \u5f00\u59cb\uff1b \u8fd4\u56de\u6210\u529f\u62f7\u8d1d\u7684\u5b57\u7b26\u7684\u4e2a\u6570\uff1b \u5e76\u4e0d\u4f1a\u6dfb\u52a0\u7ed3\u675f\u7b26 \u3002\u6761\u7528\u5728\u5fc5\u987b\u4fdd\u8bc1 szBuf \u88ab\u521d\u59cb\u5316\u4e3a\u7a7a\u6216\u57fa\u4e8e\u8fd4\u56de\u7684\u957f\u5ea6\u4e3a\u5b57\u7b26\u4e32\u6dfb\u52a0\u7ed3\u675f\u7b26\uff1b \u8c03\u7528\u8005\u9700\u8981\u786e\u4fdd szBuf \u4e0d\u6ea2\u51fa\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 std :: string sSource { \"sphinx of black quartz, judge my vow\" }; char szBuf [ 20 ]; int nLength { static_cast < int > ( sSource . copy ( szBuf , 5 , 10 )) }; szBuf [ nLength ] = '\\0' ; // Make sure we terminate the string in the buffer std :: cout << szBuf << '\\n' ; \u8f93\u51fa\uff1a 1 black \u8fd9\u4e2a\u51fd\u6570\u5e94\u8be5\u5c3d\u53ef\u80fd\u907f\u514d\uff0c\u56e0\u4e3a\u5176\u76f8\u5bf9\u6bd4\u8f83\u5371\u9669\u7684(\u56e0\u4e3a\u9700\u8981\u7531\u8c03\u7528\u8005\u63d0\u4f9b\u7ed3\u675f\u7b26\u5e76\u907f\u514d\u7f13\u51b2\u533a\u6ea2\u51fa)\u3002","title":"22.4 - std-string \u5b57\u7b26\u8bbf\u95ee\u548cC\u5b57\u7b26\u6570\u7ec4\u8f6c\u6362"},{"location":"22-4-std-string-character-access-and-conversion-to-C-style-arrays/#_1","text":"\u8bbf\u95ee\u5b57\u7b26\u4e5f\u6709\u4e24\u4e2a\u6781\u5176\u7c7b\u4f3c\u7684\u65b9\u6cd5\u3002\u6700\u7b80\u5355\u4e5f\u662f\u6700\u5757\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 \uff1a char& string::operator[] (size_type nIndex) const char& string::operator[] (size_type nIndex) const \u51fd\u6570\u8fd4\u56de\u4f4d\u4e8e\u7d22\u5f15 nIndex \u5904\u7684\u5b57\u7b26\uff1b \u4f20\u5165\u975e\u6cd5\u7d22\u5f15\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \uff1b \u56e0\u4e3a\u8fd4\u56de\u7c7b\u578b\u662f char& \u6240\u4ee5\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u4fee\u6539\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sSource { \"abcdefg\" }; std :: cout << sSource [ 5 ] << '\\n' ; sSource [ 5 ] = 'X' ; std :: cout << sSource << '\\n' ; \u8f93\u51fa\uff1a 1 2 f abcdeXg \u6b64\u5916\uff0c\u8fd8\u6709\u4e00\u4e2a\u975e\u64cd\u4f5c\u7b26\u7684\u7248\u672c\u3002\u8be5\u7248\u672c\u7684\u51fd\u6570\u901f\u5ea6\u4f1a\u6162\u4e00\u4e9b\uff0c\u56e0\u4e3a\u5b83==\u4f1a\u68c0\u67e5\u6570\u7ec4\u4e0b\u6807\u662f\u5426\u5408\u6cd5==\u3002\u5982\u679c\u4f60\u4e0d\u786e\u5b9a\u7d22\u5f15 nIndex \u662f\u5426\u4e00\u5b9a\u6709\u6548\uff0c\u5219\u6700\u597d\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u6765\u8bbf\u95ee\uff1a char& string::at (size_type nIndex) const char& string::at (size_type nIndex) const \u51fd\u6570\u8fd4\u56de\u4f4d\u4e8e\u7d22\u5f15 nIndex \u5904\u7684\u5b57\u7b26\uff1b \u4f20\u5165\u975e\u6cd5\u7d22\u5f15\u4f1a\u5bfc\u81f4 out_of_range \u5f02\u5e38\uff1b \u56e0\u4e3a\u8fd4\u56de\u7c7b\u578b\u662f char& \u6240\u4ee5\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u4fee\u6539\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sSource { \"abcdefg\" }; std :: cout << sSource . at ( 5 ) << '\\n' ; sSource . at ( 5 ) = 'X' ; std :: cout << sSource << '\\n' ; \u8f93\u51fa 1 2 f abcdeXg","title":"\u8bbf\u95ee\u5b57\u7b26"},{"location":"22-4-std-string-character-access-and-conversion-to-C-style-arrays/#c","text":"\u5f88\u591a\u51fd\u6570\uff08\u5305\u62ec\u6240\u6709C\u8bed\u8a00\u51fd\u6570\uff09\u671f\u671b\u7684\u8f93\u5165\u662fC\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u800c\u4e0d\u662f std::string \u3002\u56e0\u6b64\uff0c std::string \u63d0\u4f9b\u4e86\u4e09\u79cd\u5c06 std::string \u8f6c\u6362\u4e3aC\u8bed\u8a00\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff1a const char* string::c_str () const \u5c06\u5b57\u7b26\u4e32\u5185\u5bb9\u4f5c\u4e3aC\u98ce\u683c\u5b57\u7b26\u4e32\u5e38\u91cf\u8fd4\u56de\uff1b \u7ed3\u5c3e\u4f1a\u6dfb\u52a0\u7ed3\u675f\u7b26\uff1b \u8be5C\u98ce\u683c\u5b57\u7b26\u4e32\u5f52 std::string \u6240\u6709\u4e14\u4e0d\u5e94\u8be5\u88ab\u624b\u52a8\u91ca\u653e\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 #include std :: string sSource { \"abcdefg\" }; std :: cout << std :: strlen ( sSource . c_str ()); \u8f93\u51fa\uff1a 1 7 const char* string::data () const \u5c06\u5b57\u7b26\u4e32\u5185\u5bb9\u4f5c\u4e3aC\u98ce\u683c\u5b57\u7b26\u4e32\u5e38\u91cf\u8fd4\u56de\uff1b \u7ed3\u5c3e\u4f1a\u6dfb\u52a0\u7ed3\u675f\u7b26\uff1b\u8be5\u51fd\u6570\u884c\u4e3a\u4e0e c_str() \u662f\u4e00\u6837\u7684\uff1b \u8be5C\u98ce\u683c\u5b57\u7b26\u4e32\u5f52 std::string \u6240\u6709\u4e14\u4e0d\u5e94\u8be5\u88ab\u624b\u52a8\u91ca\u653e\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 #include std :: string sSource { \"abcdefg\" }; const char * szString { \"abcdefg\" }; // memcmp compares the first n characters of two C-style strings and returns 0 if they are equal if ( std :: memcmp ( sSource . data (), szString , sSource . length ()) == 0 ) std :: cout << \"The strings are equal\" ; else std :: cout << \"The strings are not equal\" ; \u8f93\u51fa\uff1a 1 The strings are equal size_type string::copy(char* szBuf, size_type nLength, size_type nIndex = 0) const \u4ece\u5b57\u7b26\u4e32\u4e2d\u62f7\u8d1d\u81f3\u591a nLength \u4e2a\u5b57\u7b26\u5230 szBuf \uff0c\u4ece\u7d22\u5f15 nIndex \u5f00\u59cb\uff1b \u8fd4\u56de\u6210\u529f\u62f7\u8d1d\u7684\u5b57\u7b26\u7684\u4e2a\u6570\uff1b \u5e76\u4e0d\u4f1a\u6dfb\u52a0\u7ed3\u675f\u7b26 \u3002\u6761\u7528\u5728\u5fc5\u987b\u4fdd\u8bc1 szBuf \u88ab\u521d\u59cb\u5316\u4e3a\u7a7a\u6216\u57fa\u4e8e\u8fd4\u56de\u7684\u957f\u5ea6\u4e3a\u5b57\u7b26\u4e32\u6dfb\u52a0\u7ed3\u675f\u7b26\uff1b \u8c03\u7528\u8005\u9700\u8981\u786e\u4fdd szBuf \u4e0d\u6ea2\u51fa\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 std :: string sSource { \"sphinx of black quartz, judge my vow\" }; char szBuf [ 20 ]; int nLength { static_cast < int > ( sSource . copy ( szBuf , 5 , 10 )) }; szBuf [ nLength ] = '\\0' ; // Make sure we terminate the string in the buffer std :: cout << szBuf << '\\n' ; \u8f93\u51fa\uff1a 1 black \u8fd9\u4e2a\u51fd\u6570\u5e94\u8be5\u5c3d\u53ef\u80fd\u907f\u514d\uff0c\u56e0\u4e3a\u5176\u76f8\u5bf9\u6bd4\u8f83\u5371\u9669\u7684(\u56e0\u4e3a\u9700\u8981\u7531\u8c03\u7528\u8005\u63d0\u4f9b\u7ed3\u675f\u7b26\u5e76\u907f\u514d\u7f13\u51b2\u533a\u6ea2\u51fa)\u3002","title":"\u8f6c\u6362\u4e3aC\u98ce\u683c\u6570\u7ec4"},{"location":"22-5-std-string-assignment-and-swapping/","tags":["string"],"text":"Key Takeaway String \u8d4b\u503c \u4e3a\u5b57\u7b26\u4e32\u8d4b\u503c\u7684\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26 \uff0c\u6b64\u5916\uff0c assign() \u51fd\u6570\u4e5f\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u7684\u529f\u80fd\u3002 string& string::operator= (const string& str) string& string::assign (const string& str) string& string::operator= (const char* str) string& string::assign (const char* str) string& string::operator= (char c) \u8fd9\u4e9b\u51fd\u6570\u90fd\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u8d4b\u503c\uff1b \u51fd\u6570\u8fd4\u56de *this \uff0c\u6240\u4ee5\u53ef\u4ee5\u8fdb\u884c\u94fe\u5f0f\u8c03\u7528\uff1b \u6ce8\u610f\uff0c assign() \u51fd\u6570\u7684\u5404\u4e2a\u7248\u672c\u90fd\u4e0d\u80fd\u63a5\u6536\u4e00\u4e2a\u5355\u72ec\u5b57\u7b26\u4f5c\u4e3a\u53c2\u6570\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 std :: string sString ; // Assign a string value sString = std :: string ( \"One\" ); std :: cout << sString << '\\n' ; const std :: string sTwo ( \"Two\" ); sString . assign ( sTwo ); std :: cout << sString << '\\n' ; // Assign a C-style string sString = \"Three\" ; std :: cout << sString << '\\n' ; sString . assign ( \"Four\" ); std :: cout << sString << '\\n' ; // Assign a char sString = '5' ; std :: cout << sString << '\\n' ; // Chain assignment std :: string sOther ; sString = sOther = \"Six\" ; std :: cout << sString << ' ' << sOther << '\\n' ; \u8f93\u51fa\uff1a 1 2 3 4 5 6 One Two Three Four 5 Six Six assign() \u6210\u5458\u51fd\u6570\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u7248\u672c\uff1a string& string::assign (const string& str, size_type index, size_type len) \u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u5b50\u4e32\u8d4b\u503c\u7ed9\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4ece index \u5f00\u59cb\uff0c\u957f\u5ea6\u4e3a len \u5982\u679c\u7d22\u5f15 index \u8d8a\u754c\uff0c\u5219\u629b\u51fa out_of_range \u5f02\u5e38\uff1b \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 const std :: string sSource ( \"abcdefg\" ); std :: string sDest ; sDest . assign ( sSource , 2 , 4 ); // assign a substring of source from index 2 of length 4 std :: cout << sDest << '\\n' ; \u8f93\u51fa\uff1a 1 cdef string& string::assign (const char* chars, size_type len) \u5c06C\u98ce\u683c\u5b57\u7b26\u4e32\u6570\u7ec4\u4e2d\u7684 len \u4e2a\u5b57\u7b26\u8d4b\u503c\u7ed9\u5b57\u7b26\u4e32\uff1b \u5982\u679c\u8981\u62f7\u8d1d\u7684\u957f\u5ea6\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\uff0c\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sDest ; sDest . assign ( \"abcdefg\" , 4 ); std :: cout << sDest << '\\n' ; \u8f93\u51fa\uff1a 1 abcd \u8fd9\u4e2a\u51fd\u6570\u5b58\u5728\u6f5c\u5728\u98ce\u9669\uff0c\u4e0d\u5efa\u8bae\u4f7f\u7528\u3002 string& string::assign (size_type len, char c) \u5c06\u5b57\u7b26c\u91cd\u590d len \u6b21\u8d4b\u503c\u7ed9\u5b57\u7b26\u4e32\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\uff0c\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sDest ; sDest . assign ( 4 , 'g' ); std :: cout << sDest << '\\n' ; \u8f93\u51fa\uff1a 1 gggg \u4ea4\u6362\u5b57\u7b26\u4e32 \u5982\u679c\u4f60\u6709\u4e24\u4e2a\u5b57\u7b26\u4e32\u5e76\u5e0c\u671b\u4ea4\u6362\u5b83\u4eec\u7684\u503c\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528\u4e24\u4e2a\u540d\u4e3a swap() \u7684\u51fd\u6570\u3002 void string::swap (string& str) void swap (string& str1, string& str2) \u4e24\u4e2a\u51fd\u6570\u90fd\u53ef\u4ee5\u4ea4\u6362\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u503c\u3002\u6210\u5458\u51fd\u6570\u4f1a\u4ea4\u6362 *this \u548c str \uff0c\u5168\u5c40\u51fd\u6570\u5219\u4ea4\u6362 str1 \u548c str2 \uff1b \u8fd9\u4e9b\u51fd\u6570\u662f\u9ad8\u6548\u7684\uff0c\u5e94\u8be5\u7528\u6765\u4ee3\u66ff\u8d4b\u503c\u6765\u6267\u884c\u5b57\u7b26\u4e32\u4ea4\u6362\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 std :: string sStr1 ( \"red\" ); std :: string sStr2 ( \"blue\" ); std :: cout << sStr1 << ' ' << sStr2 << '\\n' ; swap ( sStr1 , sStr2 ); std :: cout << sStr1 << ' ' << sStr2 << '\\n' ; sStr1 . swap ( sStr2 ); std :: cout << sStr1 << ' ' << sStr2 << '\\n' ; \u8f93\u51fa\uff1a 1 2 3 red blue blue red red blue","title":"22.5 - std-string \u8d4b\u503c\u548c\u4ea4\u6362"},{"location":"22-5-std-string-assignment-and-swapping/#string","text":"\u4e3a\u5b57\u7b26\u4e32\u8d4b\u503c\u7684\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26 \uff0c\u6b64\u5916\uff0c assign() \u51fd\u6570\u4e5f\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u7684\u529f\u80fd\u3002 string& string::operator= (const string& str) string& string::assign (const string& str) string& string::operator= (const char* str) string& string::assign (const char* str) string& string::operator= (char c) \u8fd9\u4e9b\u51fd\u6570\u90fd\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u8d4b\u503c\uff1b \u51fd\u6570\u8fd4\u56de *this \uff0c\u6240\u4ee5\u53ef\u4ee5\u8fdb\u884c\u94fe\u5f0f\u8c03\u7528\uff1b \u6ce8\u610f\uff0c assign() \u51fd\u6570\u7684\u5404\u4e2a\u7248\u672c\u90fd\u4e0d\u80fd\u63a5\u6536\u4e00\u4e2a\u5355\u72ec\u5b57\u7b26\u4f5c\u4e3a\u53c2\u6570\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 std :: string sString ; // Assign a string value sString = std :: string ( \"One\" ); std :: cout << sString << '\\n' ; const std :: string sTwo ( \"Two\" ); sString . assign ( sTwo ); std :: cout << sString << '\\n' ; // Assign a C-style string sString = \"Three\" ; std :: cout << sString << '\\n' ; sString . assign ( \"Four\" ); std :: cout << sString << '\\n' ; // Assign a char sString = '5' ; std :: cout << sString << '\\n' ; // Chain assignment std :: string sOther ; sString = sOther = \"Six\" ; std :: cout << sString << ' ' << sOther << '\\n' ; \u8f93\u51fa\uff1a 1 2 3 4 5 6 One Two Three Four 5 Six Six assign() \u6210\u5458\u51fd\u6570\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u7248\u672c\uff1a string& string::assign (const string& str, size_type index, size_type len) \u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u5b50\u4e32\u8d4b\u503c\u7ed9\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4ece index \u5f00\u59cb\uff0c\u957f\u5ea6\u4e3a len \u5982\u679c\u7d22\u5f15 index \u8d8a\u754c\uff0c\u5219\u629b\u51fa out_of_range \u5f02\u5e38\uff1b \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 const std :: string sSource ( \"abcdefg\" ); std :: string sDest ; sDest . assign ( sSource , 2 , 4 ); // assign a substring of source from index 2 of length 4 std :: cout << sDest << '\\n' ; \u8f93\u51fa\uff1a 1 cdef string& string::assign (const char* chars, size_type len) \u5c06C\u98ce\u683c\u5b57\u7b26\u4e32\u6570\u7ec4\u4e2d\u7684 len \u4e2a\u5b57\u7b26\u8d4b\u503c\u7ed9\u5b57\u7b26\u4e32\uff1b \u5982\u679c\u8981\u62f7\u8d1d\u7684\u957f\u5ea6\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\uff0c\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sDest ; sDest . assign ( \"abcdefg\" , 4 ); std :: cout << sDest << '\\n' ; \u8f93\u51fa\uff1a 1 abcd \u8fd9\u4e2a\u51fd\u6570\u5b58\u5728\u6f5c\u5728\u98ce\u9669\uff0c\u4e0d\u5efa\u8bae\u4f7f\u7528\u3002 string& string::assign (size_type len, char c) \u5c06\u5b57\u7b26c\u91cd\u590d len \u6b21\u8d4b\u503c\u7ed9\u5b57\u7b26\u4e32\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\uff0c\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sDest ; sDest . assign ( 4 , 'g' ); std :: cout << sDest << '\\n' ; \u8f93\u51fa\uff1a 1 gggg","title":"String \u8d4b\u503c"},{"location":"22-5-std-string-assignment-and-swapping/#_1","text":"\u5982\u679c\u4f60\u6709\u4e24\u4e2a\u5b57\u7b26\u4e32\u5e76\u5e0c\u671b\u4ea4\u6362\u5b83\u4eec\u7684\u503c\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528\u4e24\u4e2a\u540d\u4e3a swap() \u7684\u51fd\u6570\u3002 void string::swap (string& str) void swap (string& str1, string& str2) \u4e24\u4e2a\u51fd\u6570\u90fd\u53ef\u4ee5\u4ea4\u6362\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u503c\u3002\u6210\u5458\u51fd\u6570\u4f1a\u4ea4\u6362 *this \u548c str \uff0c\u5168\u5c40\u51fd\u6570\u5219\u4ea4\u6362 str1 \u548c str2 \uff1b \u8fd9\u4e9b\u51fd\u6570\u662f\u9ad8\u6548\u7684\uff0c\u5e94\u8be5\u7528\u6765\u4ee3\u66ff\u8d4b\u503c\u6765\u6267\u884c\u5b57\u7b26\u4e32\u4ea4\u6362\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 std :: string sStr1 ( \"red\" ); std :: string sStr2 ( \"blue\" ); std :: cout << sStr1 << ' ' << sStr2 << '\\n' ; swap ( sStr1 , sStr2 ); std :: cout << sStr1 << ' ' << sStr2 << '\\n' ; sStr1 . swap ( sStr2 ); std :: cout << sStr1 << ' ' << sStr2 << '\\n' ; \u8f93\u51fa\uff1a 1 2 3 red blue blue red red blue","title":"\u4ea4\u6362\u5b57\u7b26\u4e32"},{"location":"22-6-std-string-appending/","tags":["string"],"text":"Key Takeaway \u8ffd\u52a0\u5b57\u7b26\u4e32 \u5728\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u8ffd\u52a0\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528 operator+= \u6216 append() \u6216 push_back() \u3002 string& string::operator+= (const string& str) string& string::append (const string& str) \u8fd9\u4e9b\u51fd\u6570\u90fd\u53ef\u4ee5\u5c06 str \u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\u3002 Sample code: 1 2 3 4 5 6 7 8 std :: string sString { \"one\" }; sString += std :: string { \" two\" }; std :: string sThree { \" three\" }; sString . append ( sThree ); std :: cout << sString << '\\n' ; Output: 1 one two three append() \u51fd\u6570\u4e5f\u6709\u51e0\u79cd\uff0c\u53ef\u4ee5\u7528\u4e8e\u6dfb\u52a0\u4e00\u4e2a\u5b50\u4e32\uff1a string& string::append (const string& str, size_type index, size_type num) \u8be5\u51fd\u6570\u5c06 str \u4e2d\u4ece\u7d22\u5f15 index \u5f00\u59cb\u7684 num \u4e2a\u5b57\u7b26\u4e32\u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\u672b\u5c3e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u629b\u51fa out_of_range \uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 std :: string sString { \"one \" }; const std :: string sTemp { \"twothreefour\" }; sString . append ( sTemp , 3 , 5 ); // append substring of sTemp starting at index 3 of length 5 std :: cout << sString << '\\n' ; \u8f93\u51fa\uff1a 1 one three Operator+= \u548c append() \u4e5f\u63d0\u4f9b\u4e86\u914d\u5408C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u5de5\u4f5c\u7684\u7248\u672c\uff1a string& string::operator+= (const char* str) string& string::append (const char* str) \u8fd9\u4e9b\u51fd\u6570\u90fd\u53ef\u4ee5\u5c06 str \u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b str \u4e0d\u80fd\u662f NULL\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 std :: string sString { \"one\" }; sString += \" two\" ; sString . append ( \" three\" ); std :: cout << sString << '\\n' ; \u8f93\u51fa\uff1a 1 one two three append() \u8fd8\u6709\u53e6\u5916\u4e00\u4e2a\u914d\u5408C\u8bed\u8a00\u5b57\u7b26\u4e32\u5de5\u4f5c\u7684\u7248\u672c\uff1a string& string::append (const char* str, size_type len) \u5c06 str \u7684\u524d len \u4e2a\u5b57\u7b26\u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f1a\u5ffd\u7565\u7279\u6b8a\u5b57\u7b26(\u5305\u62ec \u201d )\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sString { \"one \" }; sString . append ( \"threefour\" , 5 ); std :: cout << sString << '\\n' ; \u8f93\u51fa\uff1a 1 one three \u8be5\u51fd\u6570\u4e0d\u5b89\u5168\uff0c\u4e0d\u63a8\u8350\u4f7f\u7528\u3002 \u8fd8\u6709\u53e6\u5916\u4e00\u4e9b\u53ef\u4ee5\u8ffd\u52a0 \u5b57\u7b26 \u7684\u51fd\u6570\uff0c\u6ce8\u610f\uff0c\u51fd\u6570\u540d\u662f push_back() \u800c\u4e0d\u662f append() \uff01 string& string::operator+= (char c) void string::push_back (char c) \u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u53ef\u4ee5\u5c06\u5b57\u7b26c\u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\uff1b operator += \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f8b\u5b50\uff1a 1 2 3 4 5 std :: string sString { \"one\" }; sString += ' ' ; sString . push_back ( '2' ); std :: cout << sString << '\\n' ; \u8f93\u51fa\uff1a 1 one 2 \u5982\u679c\u4f60\u597d\u5947\u4e3a\u4ec0\u4e48\u8fd9\u4e2a\u51fd\u6570\u7684\u540d\u5b57\u662f push_back() \u800c\u4e0d\u662f append() \u3002\u8fd9\u91cc\u662f\u9075\u5faa\u4e86 \u6808 \u64cd\u4f5c\u7684\u547d\u540d\u4e60\u60ef\uff0c push_back() \u7528\u4e8e\u5c06\u4e00\u4e2a\u5355\u4e2a\u5185\u5bb9\u6dfb\u52a0\u5230\u6808\u9876\u3002\u5982\u679c\u6211\u4eec\u5c06\u5b57\u7b26\u4e32\u770b\u505a\u662f\u4e00\u4e2a\u5b57\u7b26\u7ec4\u6210\u7684\u6808\uff0c\u5219\u4f7f\u7528 push_back() \u5c06\u4e00\u4e2a\u5b57\u7b26\u6dfb\u52a0\u5230\u6808\u9876\u662f\u5f88\u7b26\u5408\u76f4\u89c9\u7684\u3002\u4f46\u662f\uff0c\u6ca1\u6709\u63d0\u4f9b append() \u51fd\u6570\u5728\u6211\u770b\u6765\u5728\u4e00\u81f4\u6027\u65b9\u9762\u662f\u4e00\u79cd\u7f3a\u5931\uff01 append() \u51fd\u6570\u6709\u4e00\u4e2a\u7248\u672c\u53ef\u4ee5\u6dfb\u52a0\u591a\u4e2a\u5b57\u7b26\u4e32\uff1a string& string::append (size_type num, char c) \u5c06\u5b57\u7b26c\u91cd\u590d num \u6b21\u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\u4e2d\uff1b \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sString { \"aaa\" }; sString . append ( 4 , 'b' ); std :: cout << sString << '\\n' ; \u8f93\u51fa\uff1a 1 aaabbbb \u6700\u540e\u4e00\u79cd\u7248\u672c\u7684 append() \u53ef\u4ee5\u914d\u5408 \u8fed\u4ee3\u5668 \u5de5\u4f5c\uff1a string& string::append (InputIterator start, InputIterator end) \u5c06 [start, end) \u8303\u56f4\u5185\u7684\u5b57\u7b26\u8ffd\u52a0\u5230\u5b57\u7b26\u4e32 (\u5de6\u95ed\u53f3\u5f00\u533a\u95f4)\uff1b \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b","title":"22.6 - std-string \u8ffd\u52a0"},{"location":"22-6-std-string-appending/#_1","text":"\u5728\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u8ffd\u52a0\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528 operator+= \u6216 append() \u6216 push_back() \u3002 string& string::operator+= (const string& str) string& string::append (const string& str) \u8fd9\u4e9b\u51fd\u6570\u90fd\u53ef\u4ee5\u5c06 str \u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\u3002 Sample code: 1 2 3 4 5 6 7 8 std :: string sString { \"one\" }; sString += std :: string { \" two\" }; std :: string sThree { \" three\" }; sString . append ( sThree ); std :: cout << sString << '\\n' ; Output: 1 one two three append() \u51fd\u6570\u4e5f\u6709\u51e0\u79cd\uff0c\u53ef\u4ee5\u7528\u4e8e\u6dfb\u52a0\u4e00\u4e2a\u5b50\u4e32\uff1a string& string::append (const string& str, size_type index, size_type num) \u8be5\u51fd\u6570\u5c06 str \u4e2d\u4ece\u7d22\u5f15 index \u5f00\u59cb\u7684 num \u4e2a\u5b57\u7b26\u4e32\u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\u672b\u5c3e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u629b\u51fa out_of_range \uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 std :: string sString { \"one \" }; const std :: string sTemp { \"twothreefour\" }; sString . append ( sTemp , 3 , 5 ); // append substring of sTemp starting at index 3 of length 5 std :: cout << sString << '\\n' ; \u8f93\u51fa\uff1a 1 one three Operator+= \u548c append() \u4e5f\u63d0\u4f9b\u4e86\u914d\u5408C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u5de5\u4f5c\u7684\u7248\u672c\uff1a string& string::operator+= (const char* str) string& string::append (const char* str) \u8fd9\u4e9b\u51fd\u6570\u90fd\u53ef\u4ee5\u5c06 str \u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b str \u4e0d\u80fd\u662f NULL\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 std :: string sString { \"one\" }; sString += \" two\" ; sString . append ( \" three\" ); std :: cout << sString << '\\n' ; \u8f93\u51fa\uff1a 1 one two three append() \u8fd8\u6709\u53e6\u5916\u4e00\u4e2a\u914d\u5408C\u8bed\u8a00\u5b57\u7b26\u4e32\u5de5\u4f5c\u7684\u7248\u672c\uff1a string& string::append (const char* str, size_type len) \u5c06 str \u7684\u524d len \u4e2a\u5b57\u7b26\u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f1a\u5ffd\u7565\u7279\u6b8a\u5b57\u7b26(\u5305\u62ec \u201d )\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sString { \"one \" }; sString . append ( \"threefour\" , 5 ); std :: cout << sString << '\\n' ; \u8f93\u51fa\uff1a 1 one three \u8be5\u51fd\u6570\u4e0d\u5b89\u5168\uff0c\u4e0d\u63a8\u8350\u4f7f\u7528\u3002 \u8fd8\u6709\u53e6\u5916\u4e00\u4e9b\u53ef\u4ee5\u8ffd\u52a0 \u5b57\u7b26 \u7684\u51fd\u6570\uff0c\u6ce8\u610f\uff0c\u51fd\u6570\u540d\u662f push_back() \u800c\u4e0d\u662f append() \uff01 string& string::operator+= (char c) void string::push_back (char c) \u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u53ef\u4ee5\u5c06\u5b57\u7b26c\u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\uff1b operator += \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f8b\u5b50\uff1a 1 2 3 4 5 std :: string sString { \"one\" }; sString += ' ' ; sString . push_back ( '2' ); std :: cout << sString << '\\n' ; \u8f93\u51fa\uff1a 1 one 2 \u5982\u679c\u4f60\u597d\u5947\u4e3a\u4ec0\u4e48\u8fd9\u4e2a\u51fd\u6570\u7684\u540d\u5b57\u662f push_back() \u800c\u4e0d\u662f append() \u3002\u8fd9\u91cc\u662f\u9075\u5faa\u4e86 \u6808 \u64cd\u4f5c\u7684\u547d\u540d\u4e60\u60ef\uff0c push_back() \u7528\u4e8e\u5c06\u4e00\u4e2a\u5355\u4e2a\u5185\u5bb9\u6dfb\u52a0\u5230\u6808\u9876\u3002\u5982\u679c\u6211\u4eec\u5c06\u5b57\u7b26\u4e32\u770b\u505a\u662f\u4e00\u4e2a\u5b57\u7b26\u7ec4\u6210\u7684\u6808\uff0c\u5219\u4f7f\u7528 push_back() \u5c06\u4e00\u4e2a\u5b57\u7b26\u6dfb\u52a0\u5230\u6808\u9876\u662f\u5f88\u7b26\u5408\u76f4\u89c9\u7684\u3002\u4f46\u662f\uff0c\u6ca1\u6709\u63d0\u4f9b append() \u51fd\u6570\u5728\u6211\u770b\u6765\u5728\u4e00\u81f4\u6027\u65b9\u9762\u662f\u4e00\u79cd\u7f3a\u5931\uff01 append() \u51fd\u6570\u6709\u4e00\u4e2a\u7248\u672c\u53ef\u4ee5\u6dfb\u52a0\u591a\u4e2a\u5b57\u7b26\u4e32\uff1a string& string::append (size_type num, char c) \u5c06\u5b57\u7b26c\u91cd\u590d num \u6b21\u8ffd\u52a0\u5230\u5b57\u7b26\u4e32\u4e2d\uff1b \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f8b\u5b50\uff1a 1 2 3 4 std :: string sString { \"aaa\" }; sString . append ( 4 , 'b' ); std :: cout << sString << '\\n' ; \u8f93\u51fa\uff1a 1 aaabbbb \u6700\u540e\u4e00\u79cd\u7248\u672c\u7684 append() \u53ef\u4ee5\u914d\u5408 \u8fed\u4ee3\u5668 \u5de5\u4f5c\uff1a string& string::append (InputIterator start, InputIterator end) \u5c06 [start, end) \u8303\u56f4\u5185\u7684\u5b57\u7b26\u8ffd\u52a0\u5230\u5b57\u7b26\u4e32 (\u5de6\u95ed\u53f3\u5f00\u533a\u95f4)\uff1b \u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u8fc7\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b","title":"\u8ffd\u52a0\u5b57\u7b26\u4e32"},{"location":"22-7-std-string-inserting/","tags":["string"],"text":"Key Takeaway \u63d2\u5165 \u5411\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u5b57\u7b26\u53ef\u4ee5\u901a\u8fc7 insert() \u51fd\u6570\u5b8c\u6210\u3002 string& string::insert (size_type index, const string& str) string& string::insert (size_type index, const char* str) \u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u5c06 str \u4e2d\u7684\u5b57\u7b26\uff0c\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u7d22\u5f15 index \u4f4d\u7f6e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u4f1a\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7d22\u5f15\u65e0\u6548\uff0c\u5219\u4f1a\u629b\u51fa out_of_range \u5f02\u5e38\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u51fa\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u5bf9\u4e8e\u63a5\u53d7C\u98ce\u683c\u5b57\u7b26\u4e32\u7248\u672c\u7684\u51fd\u6570\uff0c str \u4e0d\u80fd\u662fNULL\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 string sString ( \"aaaa\" ); cout << sString << endl ; sString . insert ( 2 , string ( \"bbbb\" )); cout << sString << endl ; sString . insert ( 4 , \"cccc\" ); cout << sString << endl ; \u8f93\u51fa\uff1a 1 2 3 aaaa aabbbbaa aabbccccbbaa \u8fd8\u6709\u4e00\u4e2a\u7279\u522b\u5413\u4eba\u7684 insert() \u7248\u672c\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b50\u4e32\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u7684 index \u4f4d\u7f6e\uff1a string& string::insert (size_type index, const string& str, size_type startindex, size_type num) \u8be5\u51fd\u6570\u4f1a\u5c06 str \u4e2d\u4ece startindex \u5f00\u59cb\u7684 num \u4e2a\u5b57\u7b26\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u7684 index \u4f4d\u7f6e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u4f1a\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c index \u6216\u8005 startindex \uff0c\u8d8a\u754c\uff0c\u5219\u629b\u51fa out_of_range \u5f02\u5e38\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u51fa\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f8b\u5b50\uff1a 1 2 3 4 5 string sString ( \"aaaa\" ); const string sInsert ( \"01234567\" ); sString . insert ( 2 , sInsert , 3 , 4 ); // insert substring of sInsert from index [3,7) into sString at index 2 cout << sString << endl ; \u8f93\u51fa\uff1a 1 aa3456aa \u8fd8\u6709\u4e00\u4e2a\u7248\u672c\u7684 insert() \u53ef\u4ee5\u5c06C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u7684\u524d\u9762\u4e00\u90e8\u5206\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\uff1a string& string::insert(size_type index, const char* str, size_type len) \u5c06 str \u4e2d\u7684\u524d len \u4e2a\u5b57\u7b26\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d index \u4f4d\u7f6e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u4f1a\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7d22\u5f15\u65e0\u6548\uff0c\u5219\u629b\u51fa out_of_range \u5f02\u5e38\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u51fa\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f1a\u5ffd\u7565\u7279\u6b8a\u5b57\u7b26 (\u4f8b\u5982 \u201d ) \u4f8b\u5b50\uff1a 1 2 3 4 string sString ( \"aaaa\" ); sString . insert ( 2 , \"bcdef\" , 3 ); cout << sString << endl ; \u8f93\u51fa\uff1a 1 aabcdaa \u8fd8\u6709\u4e00\u4e2a\u7248\u672c\u7684 insert() \u53ef\u4ee5\u63d2\u5165\u67d0\u4e2a\u5b57\u7b26\u82e5\u5e72\u6b21\uff1a string& string::insert(size_type index, size_type num, char c) \u5c06\u5b57\u7b26 c \u5728\u5b57\u7b26\u4e32 index \u5904\u63d2\u5165 num \u6b21\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u4f1a\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7d22\u5f15\u65e0\u6548\uff0c\u5219\u629b\u51fa out_of_range \u5f02\u5e38\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u51fa\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f8b\u5b50\uff1a 1 2 3 4 string sString ( \"aaaa\" ); sString . insert ( 2 , 4 , 'c' ); cout << sString << endl ; \u8f93\u51fa\uff1a 1 aaccccaa \u6700\u540e\uff0c\u8fd8\u6709\u4e09\u4e2a\u7248\u672c\u7684 insert() \u7528\u4e8e\u914d\u5408 \u8fed\u4ee3\u5668 \u4f7f\u7528\uff1a void insert(iterator it, size_type num, char c) iterator string::insert(iterator it, char c) void string::insert(iterator it, InputIterator begin, InputIterator end) \u7b2c\u4e00\u4e2a\u51fd\u6570\u5c06\u5b57\u7b26 c \u5728\u8fed\u4ee3\u5668 it \u524d\u63d2\u5165 num \u6b21\uff1b \u7b2c\u4e8c\u4e2a\u51fd\u6570\u5c06\u5b57\u7b26c\u63d2\u5165\u5230\u8fed\u4ee3\u5668 it \u524d\u5e76\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u8be5\u5b57\u7b26\u7684\u8fed\u4ee3\u5668\uff1b \u7b2c\u4e09\u4e2a\u51fd\u6570\u5c06 [begin,end) \u8303\u56f4\u5185\u7684\u5b57\u7b26\u63d2\u5165\u5230\u8fed\u4ee3\u5668 it \u524d\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u51fa\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b","title":"22.7 - std-string \u63d2\u5165"},{"location":"22-7-std-string-inserting/#_1","text":"\u5411\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u5b57\u7b26\u53ef\u4ee5\u901a\u8fc7 insert() \u51fd\u6570\u5b8c\u6210\u3002 string& string::insert (size_type index, const string& str) string& string::insert (size_type index, const char* str) \u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u5c06 str \u4e2d\u7684\u5b57\u7b26\uff0c\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u7d22\u5f15 index \u4f4d\u7f6e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u4f1a\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7d22\u5f15\u65e0\u6548\uff0c\u5219\u4f1a\u629b\u51fa out_of_range \u5f02\u5e38\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u51fa\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u5bf9\u4e8e\u63a5\u53d7C\u98ce\u683c\u5b57\u7b26\u4e32\u7248\u672c\u7684\u51fd\u6570\uff0c str \u4e0d\u80fd\u662fNULL\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 string sString ( \"aaaa\" ); cout << sString << endl ; sString . insert ( 2 , string ( \"bbbb\" )); cout << sString << endl ; sString . insert ( 4 , \"cccc\" ); cout << sString << endl ; \u8f93\u51fa\uff1a 1 2 3 aaaa aabbbbaa aabbccccbbaa \u8fd8\u6709\u4e00\u4e2a\u7279\u522b\u5413\u4eba\u7684 insert() \u7248\u672c\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b50\u4e32\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u7684 index \u4f4d\u7f6e\uff1a string& string::insert (size_type index, const string& str, size_type startindex, size_type num) \u8be5\u51fd\u6570\u4f1a\u5c06 str \u4e2d\u4ece startindex \u5f00\u59cb\u7684 num \u4e2a\u5b57\u7b26\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u7684 index \u4f4d\u7f6e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u4f1a\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c index \u6216\u8005 startindex \uff0c\u8d8a\u754c\uff0c\u5219\u629b\u51fa out_of_range \u5f02\u5e38\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u51fa\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f8b\u5b50\uff1a 1 2 3 4 5 string sString ( \"aaaa\" ); const string sInsert ( \"01234567\" ); sString . insert ( 2 , sInsert , 3 , 4 ); // insert substring of sInsert from index [3,7) into sString at index 2 cout << sString << endl ; \u8f93\u51fa\uff1a 1 aa3456aa \u8fd8\u6709\u4e00\u4e2a\u7248\u672c\u7684 insert() \u53ef\u4ee5\u5c06C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u7684\u524d\u9762\u4e00\u90e8\u5206\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\uff1a string& string::insert(size_type index, const char* str, size_type len) \u5c06 str \u4e2d\u7684\u524d len \u4e2a\u5b57\u7b26\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d index \u4f4d\u7f6e\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u4f1a\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7d22\u5f15\u65e0\u6548\uff0c\u5219\u629b\u51fa out_of_range \u5f02\u5e38\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u51fa\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f1a\u5ffd\u7565\u7279\u6b8a\u5b57\u7b26 (\u4f8b\u5982 \u201d ) \u4f8b\u5b50\uff1a 1 2 3 4 string sString ( \"aaaa\" ); sString . insert ( 2 , \"bcdef\" , 3 ); cout << sString << endl ; \u8f93\u51fa\uff1a 1 aabcdaa \u8fd8\u6709\u4e00\u4e2a\u7248\u672c\u7684 insert() \u53ef\u4ee5\u63d2\u5165\u67d0\u4e2a\u5b57\u7b26\u82e5\u5e72\u6b21\uff1a string& string::insert(size_type index, size_type num, char c) \u5c06\u5b57\u7b26 c \u5728\u5b57\u7b26\u4e32 index \u5904\u63d2\u5165 num \u6b21\uff1b \u8fd9\u4e9b\u51fd\u6570\u90fd\u4f1a\u8fd4\u56de *this \u4ee5\u4fbf\u53ef\u4ee5\u88ab\u94fe\u5f0f\u8c03\u7528\uff1b \u5982\u679c\u7d22\u5f15\u65e0\u6548\uff0c\u5219\u629b\u51fa out_of_range \u5f02\u5e38\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u51fa\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b \u4f8b\u5b50\uff1a 1 2 3 4 string sString ( \"aaaa\" ); sString . insert ( 2 , 4 , 'c' ); cout << sString << endl ; \u8f93\u51fa\uff1a 1 aaccccaa \u6700\u540e\uff0c\u8fd8\u6709\u4e09\u4e2a\u7248\u672c\u7684 insert() \u7528\u4e8e\u914d\u5408 \u8fed\u4ee3\u5668 \u4f7f\u7528\uff1a void insert(iterator it, size_type num, char c) iterator string::insert(iterator it, char c) void string::insert(iterator it, InputIterator begin, InputIterator end) \u7b2c\u4e00\u4e2a\u51fd\u6570\u5c06\u5b57\u7b26 c \u5728\u8fed\u4ee3\u5668 it \u524d\u63d2\u5165 num \u6b21\uff1b \u7b2c\u4e8c\u4e2a\u51fd\u6570\u5c06\u5b57\u7b26c\u63d2\u5165\u5230\u8fed\u4ee3\u5668 it \u524d\u5e76\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u8be5\u5b57\u7b26\u7684\u8fed\u4ee3\u5668\uff1b \u7b2c\u4e09\u4e2a\u51fd\u6570\u5c06 [begin,end) \u8303\u56f4\u5185\u7684\u5b57\u7b26\u63d2\u5165\u5230\u8fed\u4ee3\u5668 it \u524d\uff1b \u5982\u679c\u7ed3\u679c\u8d85\u51fa\u6700\u5927\u5b57\u7b26\u4e2a\u6570\u5219\u629b\u51fa length_error \u5f02\u5e38\uff1b","title":"\u63d2\u5165"},{"location":"23-1-Input-and-output-IO-streams/","tags":["io"],"text":"Key Takeaway \u8f93\u5165\u8f93\u51fa\u5e76\u6ca1\u6709\u88ab\u5b9a\u4e49\u4e3aC++\u8bed\u8a00\u7684\u6838\u5fc3\u529f\u80fd\uff0c\u5b83\u662f\u901a\u8fc7C++\u7684\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\uff08\u56e0\u6b64\u4f4d\u4e8estd\u547d\u540d\u7a7a\u95f4\u4e2d\uff09\u3002\u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5728\u4ee3\u7801\u4e2d\u5305\u542b\u4e86 iostream \u5e93\u7684\u5934\u6587\u4ef6\u5e76\u4f7f\u7528\u4e86 cin \u548c cout \u5bf9\u8c61\u8fdb\u884c\u7b80\u5355\u7684\u8f93\u5165\u8f93\u51fa\u3002\u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u8fdb\u4e00\u6b65\u7814\u7a76\u5176\u7ec6\u8282\u3002 iostream \u5e93 \u5f53\u6211\u4eec\u5305\u542b iostream \u5934\u6587\u4ef6\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u8bbf\u95ee\u63d0\u4f9b\u8f93\u5165\u8f93\u51fa\u529f\u80fd\u7684\u5168\u90e8\u7c7b\u4e86\uff08\u5305\u62ec\u4e00\u4e2a\u79f0\u4e3a iostream \u7684\u7c7b\uff09\u3002\u6240\u6709\u975e\u6587\u4ef6IO\u7684\u7c7b\u7684\u7ee7\u627f\u56fe\u53ef\u4ee5\u5728 \u8fd9\u91cc \u627e\u5230\u3002 \u5173\u4e8e\u8fd9\u4e2a\u5c42\u6b21\u7ed3\u6784\uff0c\u60a8\u53ef\u80fd\u6ce8\u610f\u5230\u7684\u7b2c\u4e00\u4ef6\u4e8b\u662f\u5b83\u4f7f\u7528\u4e86\u591a\u91cd\u7ee7\u627f(\u8fd9\u662f\u6211\u4eec\u591a\u6b21\u63d0\u9192\u4f60\u8981\u907f\u514d\u7684\u4e8b\u60c5)\u3002\u4f46\u662f\uff0c iostream \u5e93\u7ecf\u8fc7\u4e86\u5e7f\u6cdb\u7684\u8bbe\u8ba1\u548c\u6d4b\u8bd5\uff0c\u4ee5\u907f\u514d\u4efb\u4f55\u5178\u578b\u7684\u591a\u91cd\u7ee7\u627f\u95ee\u9898\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u653e\u5fc3\u5927\u80c6\u5730\u4f7f\u7528\u5b83\u3002 \u6d41\uff08Streams\uff09 \u4f60\u53ef\u80fd\u6ce8\u610f\u5230\u7684\u7b2c\u4e8c\u4ef6\u4e8b\u662f\u201c\u6d41(stream)\u201d\u8fd9\u4e2a\u8bcd\u88ab\u4f7f\u7528\u5f97\u975e\u5e38\u9891\u7e41\u3002\u5728\u6700\u57fa\u672c\u7684\u60c5\u51b5\u4e0b\uff0cC++\u4e2d\u7684I/O\u662f\u901a\u8fc7\u6d41\u5b9e\u73b0\u7684\u3002\u62bd\u8c61\u5730\u8bf4\uff0c\u4e00\u4e2a \u6d41 \u53ea\u662f\u4e00\u4e2a\u53ef\u4ee5\u6309\u987a\u5e8f\u8bbf\u95ee\u7684\u5b57\u8282\u5e8f\u5217\u3002\u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\uff0c\u6d41\u53ef\u80fd\u4ea7\u751f\u6216\u6d88\u8017\u65e0\u9650\u6570\u91cf\u7684\u6570\u636e\u3002 \u901a\u5e38\u60c5\u51b5\u4e0b\u6211\u4eec\u9700\u8981\u5904\u7406\u4e24\u79cd\u7c7b\u578b\u7684\u6d41\u3002 \u8f93\u5165\u6d41 \u7528\u6765\u4fdd\u5b58\u4ece\u6570\u636e\u751f\u4ea7\u8005\u5f97\u5230\u7684\u8f93\u5165\uff0c\u4f8b\u5982\u952e\u76d8\u3001\u6587\u4ef6\u6216\u7f51\u7edc\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u6309\u4e0b\u952e\u76d8\u4e0a\u7684\u6309\u952e\u6765\u4ea7\u751f\u8f93\u51fa\uff08\u5373\u4fbf\u7a0b\u5e8f\u6ca1\u6709\u8981\u6c42\u4efb\u4f55\u8f93\u5165\uff09\uff0c\u8be5\u8f93\u5165\u6570\u636e\u5e76\u6ca1\u6709\u88ab\u76f4\u63a5\u5ffd\u7565\uff0c\u800c\u662f\u88ab\u9001\u5165\u4e86\u8f93\u5165\u6d41\u4e2d\uff0c\u5b83\u53ea\u662f\u5728\u90a3\u91cc\u7b49\u5f85\u7a0b\u5e8f\u53bb\u8bfb\u53d6\u5b83\u3002 \u53e6\u4e00\u65b9\u9762\uff0c \u8f93\u51fa\u6d41 \u7528\u4e8e\u4fdd\u5b58\u6570\u636e\u6d88\u8d39\u8005\u9700\u8981\u7684\u6570\u636e\uff0c\u4f8b\u5982\u663e\u793a\u5668\u3001\u6587\u4ef6\u6216\u6253\u5370\u673a\u3002\u5f53\u5411\u8f93\u51fa\u8bbe\u5907\u5199\u6570\u636e\u65f6\uff0c\u8be5\u8bbe\u5907\u53ef\u80fd\u5e76\u6ca1\u6709\u51c6\u5907\u597d\u63a5\u6536\u8be5\u6570\u636e\u2014\u2014\u4f8b\u5982\uff1a\u5f53\u6570\u636e\u88ab\u5199\u5230\u8f93\u51fa\u6d41\u65f6\uff0c\u6253\u5370\u673a\u7684\u53ef\u80fd\u8fd8\u5728\u51c6\u5907\u3002\u6570\u636e\u5c31\u4f1a\u5728\u8f93\u51fa\u6d41\u4e2d\u7b49\u5f85\u6253\u5370\u673a\u53bb\u6d88\u8d39\u5b83\u3002 \u6709\u4e9b\u8bbe\u5907\uff0c\u5982\u6587\u4ef6\u548c\u7f51\u7edc\uff0c\u53ef\u4ee5\u540c\u65f6\u4f5c\u4e3a\u8f93\u5165\u548c\u8f93\u51fa\u6e90\u3002 \u6d41\u7684\u597d\u5904\u662f\uff0c\u7a0b\u5e8f\u5458\u53ea\u9700\u5b66\u4e60\u5982\u4f55\u4e0e\u6d41\u4ea4\u4e92\uff0c\u4ee5\u4fbf\u5411\u8bb8\u591a\u4e0d\u540c\u7c7b\u578b\u7684\u8bbe\u5907\u8bfb\u5199\u6570\u636e\u3002\u5173\u4e8e\u6d41\u5982\u4f55\u4e0e\u5b83\u4eec\u6240\u8fde\u63a5\u7684\u5b9e\u9645\u8bbe\u5907\u8fdb\u884c\u63a5\u53e3\u7684\u7ec6\u8282\u7531\u73af\u5883\u6216\u64cd\u4f5c\u7cfb\u7edf\u51b3\u5b9a\u3002 C++\u4e2d\u7684 Input/output \u5c3d\u7ba1 ios \u7c7b\u901a\u5e38\u6d3e\u751f\u81ea ios_base \uff0c\u4f46 ios \u901a\u5e38\u662f\u4f60\u4f1a\u76f4\u63a5\u4f7f\u7528\u7684\u6700\u4e0a\u5c42\u7684\u57fa\u7c7b\u3002 ios \u7c7b\u5b9a\u4e49\u4e86\u4e00\u5806\u5bf9\u8f93\u5165\u548c\u8f93\u51fa\u6d41\u90fd\u901a\u7528\u7684\u4e1c\u897f\u3002\u6211\u4eec\u5c06\u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u5185\u5bb9\u3002 istream \u7c7b\u662f\u5904\u7406\u8f93\u5165\u6d41\u65f6\u9700\u8981\u7528\u5230\u7684\u4e3b\u8981\u7c7b\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 \u63d0\u53d6\u8fd0\u7b97\u7b26>> \u4ece\u6d41\u4e2d\u63d0\u53d6\u503c\u3002\u5f53\u7528\u6237\u6309\u952e\u65f6\uff0c\u5bf9\u5e94\u7684\u6570\u636e\u4f1a\u88ab\u653e\u7f6e\u5728\u8f93\u5165\u6d41\u4e2d\uff0c\u6b64\u65f6\u4f60\u7684\u7a0b\u5e8f\u53ef\u4ee5\u4ece\u6d41\u4e2d\u63d0\u53d6\u8be5\u503c\u4ee5\u4fbf\u4f7f\u7528\u8be5\u6570\u636e\u3002 ostream \u7c7b\u5219\u662f\u5904\u7406\u8f93\u51fa\u6d41\u7684\u4e3b\u8981\u7c7b\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 \u63d2\u5165\u8fd0\u7b97\u7b26<< \u6d41\u4e2d\u63d2\u5165\u6570\u636e\u3002\u6211\u4eec\u9700\u8981\u5c06\u6570\u636e\u63d2\u5165\u6d41\u4e2d\uff0c\u8fd9\u6837\u6570\u636e\u6d88\u8d39\u8005\uff08\u4f8b\u5982\u663e\u793a\u5668\uff09\u624d\u80fd\u8bbf\u95ee\u8be5\u6570\u636e\u3002 iostream \u7c7b\u53ef\u4ee5\u5904\u7406\u8f93\u5165\u548c\u8f93\u51fa\uff0c\u5b9e\u73b0\u53cc\u5411 I/O. C++ \u4e2d\u7684\u6807\u51c6\u6d41 \u6807\u51c6\u6d41 \u662f\u7cfb\u7edf\u73af\u5883\u63d0\u4f9b\u7ed9\u7a0b\u5e8f\u7684\u9884\u5148\u8fde\u63a5\u597d\u7684\u6d41\u3002C++\u6709\u56db\u4e2a\u9884\u5b9a\u4e49\u7684\u6807\u51c6\u6d41\u5bf9\u8c61\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5b83\u4eec\uff0c\u5176\u4e2d\u524d\u4e09\u4e2a\u4f60\u4e4b\u524d\u5df2\u7ecf\u89c1\u8fc7\u4e86\uff1a cin -- \u7ed1\u5b9a\u5230\u6807\u51c6\u8f93\u5165\u6d41(\u4f8b\u5982\u952e\u76d8)\u7684 istream \u5bf9\u8c61\uff1b cout -- \u7ed1\u5b9a\u5230\u6807\u51c6\u8f93\u51fa\u6d41(\u901a\u5e38\u662f\u663e\u793a\u5668)\u7684 ostream \u5bf9\u8c61 \uff1b cerr -- \u7ed1\u5b9a\u5230\u6807\u51c6\u9519\u8bef\u6d41(\u901a\u5e38\u662f\u663e\u793a\u5668)\u7684 ostream \u5bf9\u8c61\uff0c\u63d0\u4f9b\u65e0\u7f13\u51b2\u8f93\u51fa\uff1b clog -- \u7ed1\u5b9a\u5230\u6807\u51c6\u9519\u8bef\u6d41(\u901a\u5e38\u662f\u663e\u793a\u5668)\u7684 ostream \u5bf9\u8c61\uff0c\u63d0\u4f9b\u7f13\u51b2\u8f93\u51fa\u3002 \u65e0\u7f13\u51b2\u8f93\u51fa\u901a\u5e38\u7acb\u5373\u5904\u7406\uff0c\u800c\u7f13\u51b2\u8f93\u51fa\u901a\u5e38\u4f5c\u4e3a\u5757\u5b58\u50a8\u548c\u5199\u5165\u3002\u56e0\u4e3aclog\u4e0d\u7ecf\u5e38\u4f7f\u7528\uff0c\u6240\u4ee5\u5b83\u7ecf\u5e38\u4ece\u6807\u51c6\u6d41\u5217\u8868\u4e2d\u88ab\u7701\u7565\u3002 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u66f4\u8be6\u7ec6\u5730\u4e86\u89e3\u66f4\u591a\u4e0eI/O\u76f8\u5173\u7684\u529f\u80fd\u3002","title":"23.1 - \u8f93\u5165\u8f93\u51fa\u6d41"},{"location":"23-1-Input-and-output-IO-streams/#iostream","text":"\u5f53\u6211\u4eec\u5305\u542b iostream \u5934\u6587\u4ef6\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u8bbf\u95ee\u63d0\u4f9b\u8f93\u5165\u8f93\u51fa\u529f\u80fd\u7684\u5168\u90e8\u7c7b\u4e86\uff08\u5305\u62ec\u4e00\u4e2a\u79f0\u4e3a iostream \u7684\u7c7b\uff09\u3002\u6240\u6709\u975e\u6587\u4ef6IO\u7684\u7c7b\u7684\u7ee7\u627f\u56fe\u53ef\u4ee5\u5728 \u8fd9\u91cc \u627e\u5230\u3002 \u5173\u4e8e\u8fd9\u4e2a\u5c42\u6b21\u7ed3\u6784\uff0c\u60a8\u53ef\u80fd\u6ce8\u610f\u5230\u7684\u7b2c\u4e00\u4ef6\u4e8b\u662f\u5b83\u4f7f\u7528\u4e86\u591a\u91cd\u7ee7\u627f(\u8fd9\u662f\u6211\u4eec\u591a\u6b21\u63d0\u9192\u4f60\u8981\u907f\u514d\u7684\u4e8b\u60c5)\u3002\u4f46\u662f\uff0c iostream \u5e93\u7ecf\u8fc7\u4e86\u5e7f\u6cdb\u7684\u8bbe\u8ba1\u548c\u6d4b\u8bd5\uff0c\u4ee5\u907f\u514d\u4efb\u4f55\u5178\u578b\u7684\u591a\u91cd\u7ee7\u627f\u95ee\u9898\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u653e\u5fc3\u5927\u80c6\u5730\u4f7f\u7528\u5b83\u3002","title":"iostream \u5e93"},{"location":"23-1-Input-and-output-IO-streams/#streams","text":"\u4f60\u53ef\u80fd\u6ce8\u610f\u5230\u7684\u7b2c\u4e8c\u4ef6\u4e8b\u662f\u201c\u6d41(stream)\u201d\u8fd9\u4e2a\u8bcd\u88ab\u4f7f\u7528\u5f97\u975e\u5e38\u9891\u7e41\u3002\u5728\u6700\u57fa\u672c\u7684\u60c5\u51b5\u4e0b\uff0cC++\u4e2d\u7684I/O\u662f\u901a\u8fc7\u6d41\u5b9e\u73b0\u7684\u3002\u62bd\u8c61\u5730\u8bf4\uff0c\u4e00\u4e2a \u6d41 \u53ea\u662f\u4e00\u4e2a\u53ef\u4ee5\u6309\u987a\u5e8f\u8bbf\u95ee\u7684\u5b57\u8282\u5e8f\u5217\u3002\u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\uff0c\u6d41\u53ef\u80fd\u4ea7\u751f\u6216\u6d88\u8017\u65e0\u9650\u6570\u91cf\u7684\u6570\u636e\u3002 \u901a\u5e38\u60c5\u51b5\u4e0b\u6211\u4eec\u9700\u8981\u5904\u7406\u4e24\u79cd\u7c7b\u578b\u7684\u6d41\u3002 \u8f93\u5165\u6d41 \u7528\u6765\u4fdd\u5b58\u4ece\u6570\u636e\u751f\u4ea7\u8005\u5f97\u5230\u7684\u8f93\u5165\uff0c\u4f8b\u5982\u952e\u76d8\u3001\u6587\u4ef6\u6216\u7f51\u7edc\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u6309\u4e0b\u952e\u76d8\u4e0a\u7684\u6309\u952e\u6765\u4ea7\u751f\u8f93\u51fa\uff08\u5373\u4fbf\u7a0b\u5e8f\u6ca1\u6709\u8981\u6c42\u4efb\u4f55\u8f93\u5165\uff09\uff0c\u8be5\u8f93\u5165\u6570\u636e\u5e76\u6ca1\u6709\u88ab\u76f4\u63a5\u5ffd\u7565\uff0c\u800c\u662f\u88ab\u9001\u5165\u4e86\u8f93\u5165\u6d41\u4e2d\uff0c\u5b83\u53ea\u662f\u5728\u90a3\u91cc\u7b49\u5f85\u7a0b\u5e8f\u53bb\u8bfb\u53d6\u5b83\u3002 \u53e6\u4e00\u65b9\u9762\uff0c \u8f93\u51fa\u6d41 \u7528\u4e8e\u4fdd\u5b58\u6570\u636e\u6d88\u8d39\u8005\u9700\u8981\u7684\u6570\u636e\uff0c\u4f8b\u5982\u663e\u793a\u5668\u3001\u6587\u4ef6\u6216\u6253\u5370\u673a\u3002\u5f53\u5411\u8f93\u51fa\u8bbe\u5907\u5199\u6570\u636e\u65f6\uff0c\u8be5\u8bbe\u5907\u53ef\u80fd\u5e76\u6ca1\u6709\u51c6\u5907\u597d\u63a5\u6536\u8be5\u6570\u636e\u2014\u2014\u4f8b\u5982\uff1a\u5f53\u6570\u636e\u88ab\u5199\u5230\u8f93\u51fa\u6d41\u65f6\uff0c\u6253\u5370\u673a\u7684\u53ef\u80fd\u8fd8\u5728\u51c6\u5907\u3002\u6570\u636e\u5c31\u4f1a\u5728\u8f93\u51fa\u6d41\u4e2d\u7b49\u5f85\u6253\u5370\u673a\u53bb\u6d88\u8d39\u5b83\u3002 \u6709\u4e9b\u8bbe\u5907\uff0c\u5982\u6587\u4ef6\u548c\u7f51\u7edc\uff0c\u53ef\u4ee5\u540c\u65f6\u4f5c\u4e3a\u8f93\u5165\u548c\u8f93\u51fa\u6e90\u3002 \u6d41\u7684\u597d\u5904\u662f\uff0c\u7a0b\u5e8f\u5458\u53ea\u9700\u5b66\u4e60\u5982\u4f55\u4e0e\u6d41\u4ea4\u4e92\uff0c\u4ee5\u4fbf\u5411\u8bb8\u591a\u4e0d\u540c\u7c7b\u578b\u7684\u8bbe\u5907\u8bfb\u5199\u6570\u636e\u3002\u5173\u4e8e\u6d41\u5982\u4f55\u4e0e\u5b83\u4eec\u6240\u8fde\u63a5\u7684\u5b9e\u9645\u8bbe\u5907\u8fdb\u884c\u63a5\u53e3\u7684\u7ec6\u8282\u7531\u73af\u5883\u6216\u64cd\u4f5c\u7cfb\u7edf\u51b3\u5b9a\u3002","title":"\u6d41\uff08Streams\uff09"},{"location":"23-1-Input-and-output-IO-streams/#c-inputoutput","text":"\u5c3d\u7ba1 ios \u7c7b\u901a\u5e38\u6d3e\u751f\u81ea ios_base \uff0c\u4f46 ios \u901a\u5e38\u662f\u4f60\u4f1a\u76f4\u63a5\u4f7f\u7528\u7684\u6700\u4e0a\u5c42\u7684\u57fa\u7c7b\u3002 ios \u7c7b\u5b9a\u4e49\u4e86\u4e00\u5806\u5bf9\u8f93\u5165\u548c\u8f93\u51fa\u6d41\u90fd\u901a\u7528\u7684\u4e1c\u897f\u3002\u6211\u4eec\u5c06\u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u5185\u5bb9\u3002 istream \u7c7b\u662f\u5904\u7406\u8f93\u5165\u6d41\u65f6\u9700\u8981\u7528\u5230\u7684\u4e3b\u8981\u7c7b\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 \u63d0\u53d6\u8fd0\u7b97\u7b26>> \u4ece\u6d41\u4e2d\u63d0\u53d6\u503c\u3002\u5f53\u7528\u6237\u6309\u952e\u65f6\uff0c\u5bf9\u5e94\u7684\u6570\u636e\u4f1a\u88ab\u653e\u7f6e\u5728\u8f93\u5165\u6d41\u4e2d\uff0c\u6b64\u65f6\u4f60\u7684\u7a0b\u5e8f\u53ef\u4ee5\u4ece\u6d41\u4e2d\u63d0\u53d6\u8be5\u503c\u4ee5\u4fbf\u4f7f\u7528\u8be5\u6570\u636e\u3002 ostream \u7c7b\u5219\u662f\u5904\u7406\u8f93\u51fa\u6d41\u7684\u4e3b\u8981\u7c7b\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 \u63d2\u5165\u8fd0\u7b97\u7b26<< \u6d41\u4e2d\u63d2\u5165\u6570\u636e\u3002\u6211\u4eec\u9700\u8981\u5c06\u6570\u636e\u63d2\u5165\u6d41\u4e2d\uff0c\u8fd9\u6837\u6570\u636e\u6d88\u8d39\u8005\uff08\u4f8b\u5982\u663e\u793a\u5668\uff09\u624d\u80fd\u8bbf\u95ee\u8be5\u6570\u636e\u3002 iostream \u7c7b\u53ef\u4ee5\u5904\u7406\u8f93\u5165\u548c\u8f93\u51fa\uff0c\u5b9e\u73b0\u53cc\u5411 I/O.","title":"C++\u4e2d\u7684 Input/output"},{"location":"23-1-Input-and-output-IO-streams/#c","text":"\u6807\u51c6\u6d41 \u662f\u7cfb\u7edf\u73af\u5883\u63d0\u4f9b\u7ed9\u7a0b\u5e8f\u7684\u9884\u5148\u8fde\u63a5\u597d\u7684\u6d41\u3002C++\u6709\u56db\u4e2a\u9884\u5b9a\u4e49\u7684\u6807\u51c6\u6d41\u5bf9\u8c61\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5b83\u4eec\uff0c\u5176\u4e2d\u524d\u4e09\u4e2a\u4f60\u4e4b\u524d\u5df2\u7ecf\u89c1\u8fc7\u4e86\uff1a cin -- \u7ed1\u5b9a\u5230\u6807\u51c6\u8f93\u5165\u6d41(\u4f8b\u5982\u952e\u76d8)\u7684 istream \u5bf9\u8c61\uff1b cout -- \u7ed1\u5b9a\u5230\u6807\u51c6\u8f93\u51fa\u6d41(\u901a\u5e38\u662f\u663e\u793a\u5668)\u7684 ostream \u5bf9\u8c61 \uff1b cerr -- \u7ed1\u5b9a\u5230\u6807\u51c6\u9519\u8bef\u6d41(\u901a\u5e38\u662f\u663e\u793a\u5668)\u7684 ostream \u5bf9\u8c61\uff0c\u63d0\u4f9b\u65e0\u7f13\u51b2\u8f93\u51fa\uff1b clog -- \u7ed1\u5b9a\u5230\u6807\u51c6\u9519\u8bef\u6d41(\u901a\u5e38\u662f\u663e\u793a\u5668)\u7684 ostream \u5bf9\u8c61\uff0c\u63d0\u4f9b\u7f13\u51b2\u8f93\u51fa\u3002 \u65e0\u7f13\u51b2\u8f93\u51fa\u901a\u5e38\u7acb\u5373\u5904\u7406\uff0c\u800c\u7f13\u51b2\u8f93\u51fa\u901a\u5e38\u4f5c\u4e3a\u5757\u5b58\u50a8\u548c\u5199\u5165\u3002\u56e0\u4e3aclog\u4e0d\u7ecf\u5e38\u4f7f\u7528\uff0c\u6240\u4ee5\u5b83\u7ecf\u5e38\u4ece\u6807\u51c6\u6d41\u5217\u8868\u4e2d\u88ab\u7701\u7565\u3002 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u66f4\u8be6\u7ec6\u5730\u4e86\u89e3\u66f4\u591a\u4e0eI/O\u76f8\u5173\u7684\u529f\u80fd\u3002","title":"C++ \u4e2d\u7684\u6807\u51c6\u6d41"},{"location":"23-2-Input-with-istream/","tags":["io"],"text":"Key Takeaway \u63d0\u53d6\u8fd0\u7b97\u7b26 \u4f1a\u5ffd\u7565\u7a7a\u767d\u7b26 std::get() \u53ef\u4ee5\u83b7\u53d6\u5b57\u7b26\u6216\u83b7\u53d6\u5b57\u7b26\u4e32\uff08\u6307\u5b9a\u6700\u5927\u5b57\u7b26\u4e2a\u6570\uff09\uff0c\u4f46\u662f\u5b83\u4e0d\u8bfb\u53d6\u6362\u884c\u7b26 getline() \uff0c\u5b83\u7684\u5de5\u4f5c\u65b9\u5f0f\u548c get() \u5b8c\u5168\u4e00\u81f4\u4f46\u662f\u4f1a\u8bfb\u53d6\u6362\u884c\u7b26\uff01 getline() \u7684\u4e00\u4e2a\u7279\u6b8a\u7248\u672c\u5b58\u5728\u4e8e istream \u7c7b\u4e4b\u5916\uff0c\u7528\u4e8e\u8bfb\u5165 std::string \u7c7b\u578b\u7684\u53d8\u91cf\u3002\u8fd9\u4e2a\u7279\u6b8a\u7248\u672c\u4e0d\u662f ostream \u6216 istream \u7684\u6210\u5458\uff0c\u5305\u542b\u5728 string \u5934\u6587\u4ef6\u4e2d\u3002 iostream \u5e93\u662f\u5f88\u590d\u6742\u7684\u2014\u2014\u6240\u4ee5\u672c\u6559\u7a0b\u4e0d\u53ef\u80fd\u6db5\u76d6\u5b83\u7684\u65b9\u65b9\u9762\u9762\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u4f1a\u5411\u4f60\u4ecb\u7ecd\u5176\u4e2d\u6700\u5e38\u7528\u7684\u90a3\u4e9b\u529f\u80fd\u3002\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba\u8f93\u5165\u5e93 istream \u7684\u5404\u9879\u529f\u80fd\u3002 \u63d0\u53d6\u8fd0\u7b97\u7b26 >> \u5728\u4e4b\u524d\u7684\u5f88\u591a\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u5728\u4f7f\u7528 \u63d0\u53d6\u8fd0\u7b97\u7b26 \u4ece\u8f93\u5165\u6d41\u4e2d\u83b7\u53d6\u6570\u636e\u4e86\u3002C++\u4e3a\u6240\u6709\u7684\u5185\u5efa\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u4e86\u9884\u5b9a\u4e49\u7684\u63d0\u53d6\u8fd0\u7b97\u7b26\uff0c\u540c\u65f6\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 \u4e3a\u6211\u4eec\u81ea\u5df1\u7684\u7c7b\u63d0\u4f9b\u63d0\u53d6\u8fd0\u7b97\u7b26\u3002 \u8bfb\u53d6\u5b57\u7b26\u4e32\u65f6\uff0c\u63d0\u53d6\u64cd\u4f5c\u7b26\u7684\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\u662f\u5982\u4f55\u9632\u6b62\u8f93\u5165\u6ea2\u51fa\u7f13\u51b2\u533a\u3002\u4e3e\u4e2a\u4f8b\u5b50: 1 2 char buf [ 10 ]; std :: cin >> buf ; \u5982\u679c\u7528\u6237\u8f93\u516518\u4e2a\u5b57\u7b26\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u6b64\u65f6\u7f13\u51b2\u533a\u4f1a\u6ea2\u51fa\u5e76\u53d1\u751f\u4e0d\u597d\u7684\u4e8b\u60c5\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u6211\u4eec\u4e0d\u5e94\u8be5\u5047\u8bbe\u7528\u6237\u4f1a\u8f93\u5165\u591a\u5c11\u5b57\u7b26\u3002 \u5904\u7406\u8fd9\u4e2a\u95ee\u9898\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528manipulators\u3002manipulator\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u7528\u4e8e\u5728\u5e94\u7528\u63d0\u53d6(>>)\u6216\u63d2\u5165(setw(\u4f4d\u4e8e iomanip \u5934\u6587\u4ef6)\u65f6\u4fee\u6539\u6d41\uff0c\u53ef\u4ee5\u7528\u4e8e\u9650\u5236\u4ece\u6d41\u4e2d\u8bfb\u5165\u7684\u5b57\u7b26\u6570\u91cf\u3002\u8981\u4f7f\u7528 setw() \uff0c\u53ea\u9700\u63d0\u4f9b\u8981\u8bfb\u53d6\u7684\u6700\u5927\u5b57\u7b26\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u50cf\u8fd9\u6837\u5c06\u5176\u63d2\u5165\u5230\u8f93\u5165\u8bed\u53e5\u4e2d: 1 2 3 #include char buf [ 10 ]; std :: cin >> std :: setw ( 10 ) >> buf ; \u8fd9\u4e2a\u7a0b\u5e8f\u53ea\u4f1a\u4ece\u6d41\u4e2d\u8bfb\u53d69\u4e2a\u5b57\u7b26\uff08\u4e3a\u7ed3\u675f\u7b26\u9884\u7559\u4e00\u4e2a\u5b57\u7b26\uff09\u3002\u4efb\u4f55\u5176\u4ed6\u7684\u5b57\u7b26\u90fd\u4f1a\u88ab\u7559\u5728\u6d41\u4e2d\u7b49\u5f85\u4e0b\u4e00\u6b21\u63d0\u53d6\u3002 \u63d0\u53d6\u64cd\u4f5c\u548c\u7a7a\u767d\u5b57\u7b26 \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u63d0\u53d6\u64cd\u4f5c\u7b26\u4f1a\u8df3\u8fc7\u7a7a\u767d\u5b57\u7b26\uff08\u7a7a\u683c\u3001tab\u548c\u7a7a\u884c\uff09\u3002 \u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 int main () { char ch ; while ( std :: cin >> ch ) std :: cout << ch ; return 0 ; } \u7528\u6237\u8f93\u5165\uff1a 1 Hello my name is Alex \u7531\u4e8e \u63d0\u53d6\u8fd0\u7b97\u7b26 \u4f1a\u8df3\u8fc7\u7a7a\u683c\u548c\u6362\u884c\uff0c\u6240\u4ee5\u8bfb\u53d6\u7684\u7ed3\u679c\u662f\uff1a 1 HellomynameisAlex \u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u4f60\u5e0c\u671b\u83b7\u5f97\u7528\u6237\u8f93\u5165\u5e76\u4fdd\u7559\u7a7a\u683c\u3002\u4e3a\u6b64\uff0cistream\u7c7b\u63d0\u4f9b\u4e86\u8bb8\u591a\u53ef\u7528\u4e8e\u6b64\u76ee\u7684\u7684\u51fd\u6570\u3002 \u5176\u4e2d\u6700\u6709\u7528\u7684\u662f get() \u51fd\u6570\uff0c\u5b83\u53ea\u662f\u4ece\u8f93\u5165\u6d41\u4e2d\u83b7\u53d6\u4e00\u4e2a\u5b57\u7b26\u3002\u4e0b\u9762\u662f\u4e0a\u9762\u4f7f\u7528 get() \u7684\u76f8\u540c\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 int main () { char ch ; while ( std :: cin . get ( ch )) std :: cout << ch ; return 0 ; } \u7528\u6237\u8f93\u5165\uff1a 1 Hello my name is Alex \u8f93\u51fa\u4e3a\uff1a 1 Hello my name is Alex std::get() \u8fd8\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32\u7248\u672c\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\u4f5c\u4e3a\u8bfb\u53d6\u5b57\u7b26\u4e2a\u6570\u7684\u6700\u5927\u503c\uff1a 1 2 3 4 5 6 7 8 int main () { char strBuf [ 11 ]; std :: cin . get ( strBuf , 11 ); std :: cout << strBuf << '\\n' ; return 0 ; } \u5982\u679c\u8f93\u5165\u4e3a\uff1a 1 Hello my name is Alex \u5219\u8f93\u51fa\u4e3a\uff1a 1 Hello my n \u6ce8\u610f\uff0c\u6211\u4eec\u53ea\u8bfb\u53d6\u4e86\u524d10\u4e2a\u5b57\u7b26(\u5fc5\u987b\u4e3a\u7ec8\u6b62\u7b26\u7559\u4e0b\u4e00\u4e2a\u5b57\u7b26)\u3002\u5176\u4f59\u7684\u5b57\u7b26\u7559\u5728\u8f93\u5165\u6d41\u4e2d\u3002 \u5173\u4e8e get() \u9700\u8981\u6ce8\u610f\u7684\u4e00\u4ef6\u91cd\u8981\u4e8b\u60c5\u662f\uff0c\u5b83\u4e0d\u8bfb\u53d6\u6362\u884c\u7b26\uff01\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c: 1 2 3 4 5 6 7 8 9 10 11 12 int main () { char strBuf [ 11 ]; // Read up to 10 characters std :: cin . get ( strBuf , 11 ); std :: cout << strBuf << '\\n' ; // Read up to 10 more characters std :: cin . get ( strBuf , 11 ); std :: cout << strBuf << '\\n' ; return 0 ; } \u5982\u679c\u8f93\u5165\u4e3a\uff1a 1 Hello! \u5219\u7a0b\u5e8f\u8f93\u51fa\u4e3a\uff1a 1 Hello! \u7136\u540e\u7a0b\u5e8f\u5c31\u7ed3\u675f\u4e86\uff01 \u4e3a\u4ec0\u4e48\u5b83\u4e0d\u8981\u6211\u518d\u8f93\u516510\u4e2a\u5b57\u7b26\uff1f\u8fd9\u662f\u56e0\u4e3a\u7b2c\u4e00\u4e2a get() \u8bfb\u53d6\u5230\u6362\u884c\u7136\u540e\u5c31\u7ed3\u675f\u4e86\u3002\u800c\u7b2c\u4e8c\u4e2a get() \u5219\u4f1a\u7ee7\u7eed\u8bfb\u53d6 cin \u6d41\uff0c\u6b64\u65f6\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u662f\u6362\u884c\uff0c\u6240\u4ee5\u5b83\u4f1a\u7acb\u5373\u505c\u6b62\u3002 \u56e0\u6b64\uff0c C++\u63d0\u4f9b\u4e86 getline() \uff0c\u5b83\u7684\u5de5\u4f5c\u65b9\u5f0f\u548c get() \u5b8c\u5168\u4e00\u81f4\u4f46\u662f\u4f1a\u8bfb\u53d6\u6362\u884c\u7b26\uff01 1 2 3 4 5 6 7 8 9 10 11 12 int main () { char strBuf [ 11 ]; // Read up to 10 characters std :: cin . getline ( strBuf , 11 ); std :: cout << strBuf << '\\n' ; // Read up to 10 more characters std :: cin . getline ( strBuf , 11 ); std :: cout << strBuf << '\\n' ; return 0 ; } \u5373\u4f7f\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u4e2d\u6709\u6362\u884c\u7b26\uff0c\u8fd9\u6bb5\u4ee3\u7801\u4e5f\u4f1a\u6309\u7167\u4f60\u7684\u671f\u671b\u6267\u884c\u3002 \u5982\u679c\u4f60\u9700\u8981\u77e5\u9053\u6700\u540e\u4e00\u6b21\u8c03\u7528 getline() \u63d0\u53d6\u4e86\u591a\u5c11\u5b57\u7b26\uff0c\u4f7f\u7528 gcount() : 1 2 3 4 5 6 7 8 9 int main () { char strBuf [ 100 ]; std :: cin . getline ( strBuf , 100 ); std :: cout << strBuf << '\\n' ; std :: cout << std :: cin . gcount () << \" characters were read\" << '\\n' ; return 0 ; } \u7528\u4e8e std::string \u7684 getline() getline() \u7684\u4e00\u4e2a\u7279\u6b8a\u7248\u672c\u5b58\u5728\u4e8e istream \u7c7b\u4e4b\u5916\uff0c\u7528\u4e8e\u8bfb\u5165 std::string \u7c7b\u578b\u7684\u53d8\u91cf\u3002\u8fd9\u4e2a\u7279\u6b8a\u7248\u672c\u4e0d\u662f ostream \u6216 istream \u7684\u6210\u5458\uff0c\u5305\u542b\u5728 string \u5934\u6587\u4ef6\u4e2d\u3002\u4e0b\u9762\u662f\u5b83\u7684\u7528\u6cd5\u793a\u4f8b: 1 2 3 4 5 6 7 8 9 10 11 #include #include int main () { std :: string strBuf ; std :: getline ( std :: cin , strBuf ); std :: cout << strBuf << '\\n' ; return 0 ; } \u4e00\u4e9b\u66f4\u6709\u7528\u7684 istream \u51fd\u6570 \u8fd8\u6709\u4e00\u4e9b\u66f4\u6709\u7528\u7684\u8f93\u5165\u51fd\u6570\u4f60\u53ef\u80fd\u4f1a\u7528\u5230\uff1a ignore() \uff1a\u5ffd\u7565\u6d41\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\uff1b ignore(int nCount) \uff1a\u5ffd\u7565\u6d41\u4e2d\u7684\u524d nCount \u4e2a\u5b57\u7b26\uff1b peek() \uff1a\u5141\u8bb8\u4f60\u4ece\u6d41\u4e2d\u8bfb\u53d6\u5b57\u7b26\u800c\u4e0d\u5c06\u5176\u4ece\u6d41\u4e2d\u5220\u9664\u3002 unget() \uff1a\u8fd4\u56de\u8bfb\u56de\u6d41\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\uff0c\u4ee5\u4fbf\u4e0b\u4e00\u6b21\u8c03\u7528\u53ef\u4ee5\u518d\u6b21\u8bfb\u53d6\u5b83\u3002 putback(char ch) \uff1a\u5141\u8bb8\u4f60\u5c06\u6240\u9009\u7684\u5b57\u7b26\u653e\u56de\u6d41\u4e2d\uff0c\u4ee5\u4fbf\u4e0b\u4e00\u4e2a\u8c03\u7528\u8bfb\u53d6\u3002\u8fd4\u56de\u8bfb\u56de\u6d41\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\uff0c\u4ee5\u4fbf\u4e0b\u4e00\u6b21\u8c03\u7528\u53ef\u4ee5\u518d\u6b21\u8bfb\u53d6\u5b83\u3002 istream \u5305\u542b\u8bb8\u591a\u5176\u4ed6\u51fd\u6570\u548c\u4e0a\u9762\u63d0\u5230\u7684\u51fd\u6570\u7684\u53d8\u4f53\uff0c\u5b83\u4eec\u53ef\u80fd\u6709\u7528\uff0c\u8fd9\u53d6\u51b3\u4e8e\u4f60\u9700\u8981\u505a\u4ec0\u4e48\u3002\u7136\u800c\uff0c\u8fd9\u4e9b\u8bae\u9898\u66f4\u9002\u5408\u653e\u5728\u4e13\u6ce8\u4e8e\u6807\u51c6\u5e93\u7684\u6559\u7a0b\u6216\u4e66\u4e2d(\u4f8b\u5982Nicolai M. Josuttis\u7684 \u201cc++\u6807\u51c6\u5e93\u201d )\u3002","title":"23.2 - \u4f7f\u7528istream\u5904\u7406\u8f93\u5165"},{"location":"23-2-Input-with-istream/#_1","text":"\u5728\u4e4b\u524d\u7684\u5f88\u591a\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u5728\u4f7f\u7528 \u63d0\u53d6\u8fd0\u7b97\u7b26 \u4ece\u8f93\u5165\u6d41\u4e2d\u83b7\u53d6\u6570\u636e\u4e86\u3002C++\u4e3a\u6240\u6709\u7684\u5185\u5efa\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u4e86\u9884\u5b9a\u4e49\u7684\u63d0\u53d6\u8fd0\u7b97\u7b26\uff0c\u540c\u65f6\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 \u4e3a\u6211\u4eec\u81ea\u5df1\u7684\u7c7b\u63d0\u4f9b\u63d0\u53d6\u8fd0\u7b97\u7b26\u3002 \u8bfb\u53d6\u5b57\u7b26\u4e32\u65f6\uff0c\u63d0\u53d6\u64cd\u4f5c\u7b26\u7684\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\u662f\u5982\u4f55\u9632\u6b62\u8f93\u5165\u6ea2\u51fa\u7f13\u51b2\u533a\u3002\u4e3e\u4e2a\u4f8b\u5b50: 1 2 char buf [ 10 ]; std :: cin >> buf ; \u5982\u679c\u7528\u6237\u8f93\u516518\u4e2a\u5b57\u7b26\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u6b64\u65f6\u7f13\u51b2\u533a\u4f1a\u6ea2\u51fa\u5e76\u53d1\u751f\u4e0d\u597d\u7684\u4e8b\u60c5\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u6211\u4eec\u4e0d\u5e94\u8be5\u5047\u8bbe\u7528\u6237\u4f1a\u8f93\u5165\u591a\u5c11\u5b57\u7b26\u3002 \u5904\u7406\u8fd9\u4e2a\u95ee\u9898\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528manipulators\u3002manipulator\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u7528\u4e8e\u5728\u5e94\u7528\u63d0\u53d6(>>)\u6216\u63d2\u5165(setw(\u4f4d\u4e8e iomanip \u5934\u6587\u4ef6)\u65f6\u4fee\u6539\u6d41\uff0c\u53ef\u4ee5\u7528\u4e8e\u9650\u5236\u4ece\u6d41\u4e2d\u8bfb\u5165\u7684\u5b57\u7b26\u6570\u91cf\u3002\u8981\u4f7f\u7528 setw() \uff0c\u53ea\u9700\u63d0\u4f9b\u8981\u8bfb\u53d6\u7684\u6700\u5927\u5b57\u7b26\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u50cf\u8fd9\u6837\u5c06\u5176\u63d2\u5165\u5230\u8f93\u5165\u8bed\u53e5\u4e2d: 1 2 3 #include char buf [ 10 ]; std :: cin >> std :: setw ( 10 ) >> buf ; \u8fd9\u4e2a\u7a0b\u5e8f\u53ea\u4f1a\u4ece\u6d41\u4e2d\u8bfb\u53d69\u4e2a\u5b57\u7b26\uff08\u4e3a\u7ed3\u675f\u7b26\u9884\u7559\u4e00\u4e2a\u5b57\u7b26\uff09\u3002\u4efb\u4f55\u5176\u4ed6\u7684\u5b57\u7b26\u90fd\u4f1a\u88ab\u7559\u5728\u6d41\u4e2d\u7b49\u5f85\u4e0b\u4e00\u6b21\u63d0\u53d6\u3002","title":"\u63d0\u53d6\u8fd0\u7b97\u7b26 >>"},{"location":"23-2-Input-with-istream/#_2","text":"\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u63d0\u53d6\u64cd\u4f5c\u7b26\u4f1a\u8df3\u8fc7\u7a7a\u767d\u5b57\u7b26\uff08\u7a7a\u683c\u3001tab\u548c\u7a7a\u884c\uff09\u3002 \u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 int main () { char ch ; while ( std :: cin >> ch ) std :: cout << ch ; return 0 ; } \u7528\u6237\u8f93\u5165\uff1a 1 Hello my name is Alex \u7531\u4e8e \u63d0\u53d6\u8fd0\u7b97\u7b26 \u4f1a\u8df3\u8fc7\u7a7a\u683c\u548c\u6362\u884c\uff0c\u6240\u4ee5\u8bfb\u53d6\u7684\u7ed3\u679c\u662f\uff1a 1 HellomynameisAlex \u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u4f60\u5e0c\u671b\u83b7\u5f97\u7528\u6237\u8f93\u5165\u5e76\u4fdd\u7559\u7a7a\u683c\u3002\u4e3a\u6b64\uff0cistream\u7c7b\u63d0\u4f9b\u4e86\u8bb8\u591a\u53ef\u7528\u4e8e\u6b64\u76ee\u7684\u7684\u51fd\u6570\u3002 \u5176\u4e2d\u6700\u6709\u7528\u7684\u662f get() \u51fd\u6570\uff0c\u5b83\u53ea\u662f\u4ece\u8f93\u5165\u6d41\u4e2d\u83b7\u53d6\u4e00\u4e2a\u5b57\u7b26\u3002\u4e0b\u9762\u662f\u4e0a\u9762\u4f7f\u7528 get() \u7684\u76f8\u540c\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 int main () { char ch ; while ( std :: cin . get ( ch )) std :: cout << ch ; return 0 ; } \u7528\u6237\u8f93\u5165\uff1a 1 Hello my name is Alex \u8f93\u51fa\u4e3a\uff1a 1 Hello my name is Alex std::get() \u8fd8\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32\u7248\u672c\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\u4f5c\u4e3a\u8bfb\u53d6\u5b57\u7b26\u4e2a\u6570\u7684\u6700\u5927\u503c\uff1a 1 2 3 4 5 6 7 8 int main () { char strBuf [ 11 ]; std :: cin . get ( strBuf , 11 ); std :: cout << strBuf << '\\n' ; return 0 ; } \u5982\u679c\u8f93\u5165\u4e3a\uff1a 1 Hello my name is Alex \u5219\u8f93\u51fa\u4e3a\uff1a 1 Hello my n \u6ce8\u610f\uff0c\u6211\u4eec\u53ea\u8bfb\u53d6\u4e86\u524d10\u4e2a\u5b57\u7b26(\u5fc5\u987b\u4e3a\u7ec8\u6b62\u7b26\u7559\u4e0b\u4e00\u4e2a\u5b57\u7b26)\u3002\u5176\u4f59\u7684\u5b57\u7b26\u7559\u5728\u8f93\u5165\u6d41\u4e2d\u3002 \u5173\u4e8e get() \u9700\u8981\u6ce8\u610f\u7684\u4e00\u4ef6\u91cd\u8981\u4e8b\u60c5\u662f\uff0c\u5b83\u4e0d\u8bfb\u53d6\u6362\u884c\u7b26\uff01\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c: 1 2 3 4 5 6 7 8 9 10 11 12 int main () { char strBuf [ 11 ]; // Read up to 10 characters std :: cin . get ( strBuf , 11 ); std :: cout << strBuf << '\\n' ; // Read up to 10 more characters std :: cin . get ( strBuf , 11 ); std :: cout << strBuf << '\\n' ; return 0 ; } \u5982\u679c\u8f93\u5165\u4e3a\uff1a 1 Hello! \u5219\u7a0b\u5e8f\u8f93\u51fa\u4e3a\uff1a 1 Hello! \u7136\u540e\u7a0b\u5e8f\u5c31\u7ed3\u675f\u4e86\uff01 \u4e3a\u4ec0\u4e48\u5b83\u4e0d\u8981\u6211\u518d\u8f93\u516510\u4e2a\u5b57\u7b26\uff1f\u8fd9\u662f\u56e0\u4e3a\u7b2c\u4e00\u4e2a get() \u8bfb\u53d6\u5230\u6362\u884c\u7136\u540e\u5c31\u7ed3\u675f\u4e86\u3002\u800c\u7b2c\u4e8c\u4e2a get() \u5219\u4f1a\u7ee7\u7eed\u8bfb\u53d6 cin \u6d41\uff0c\u6b64\u65f6\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u662f\u6362\u884c\uff0c\u6240\u4ee5\u5b83\u4f1a\u7acb\u5373\u505c\u6b62\u3002 \u56e0\u6b64\uff0c C++\u63d0\u4f9b\u4e86 getline() \uff0c\u5b83\u7684\u5de5\u4f5c\u65b9\u5f0f\u548c get() \u5b8c\u5168\u4e00\u81f4\u4f46\u662f\u4f1a\u8bfb\u53d6\u6362\u884c\u7b26\uff01 1 2 3 4 5 6 7 8 9 10 11 12 int main () { char strBuf [ 11 ]; // Read up to 10 characters std :: cin . getline ( strBuf , 11 ); std :: cout << strBuf << '\\n' ; // Read up to 10 more characters std :: cin . getline ( strBuf , 11 ); std :: cout << strBuf << '\\n' ; return 0 ; } \u5373\u4f7f\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u4e2d\u6709\u6362\u884c\u7b26\uff0c\u8fd9\u6bb5\u4ee3\u7801\u4e5f\u4f1a\u6309\u7167\u4f60\u7684\u671f\u671b\u6267\u884c\u3002 \u5982\u679c\u4f60\u9700\u8981\u77e5\u9053\u6700\u540e\u4e00\u6b21\u8c03\u7528 getline() \u63d0\u53d6\u4e86\u591a\u5c11\u5b57\u7b26\uff0c\u4f7f\u7528 gcount() : 1 2 3 4 5 6 7 8 9 int main () { char strBuf [ 100 ]; std :: cin . getline ( strBuf , 100 ); std :: cout << strBuf << '\\n' ; std :: cout << std :: cin . gcount () << \" characters were read\" << '\\n' ; return 0 ; }","title":"\u63d0\u53d6\u64cd\u4f5c\u548c\u7a7a\u767d\u5b57\u7b26"},{"location":"23-2-Input-with-istream/#stdstring-getline","text":"getline() \u7684\u4e00\u4e2a\u7279\u6b8a\u7248\u672c\u5b58\u5728\u4e8e istream \u7c7b\u4e4b\u5916\uff0c\u7528\u4e8e\u8bfb\u5165 std::string \u7c7b\u578b\u7684\u53d8\u91cf\u3002\u8fd9\u4e2a\u7279\u6b8a\u7248\u672c\u4e0d\u662f ostream \u6216 istream \u7684\u6210\u5458\uff0c\u5305\u542b\u5728 string \u5934\u6587\u4ef6\u4e2d\u3002\u4e0b\u9762\u662f\u5b83\u7684\u7528\u6cd5\u793a\u4f8b: 1 2 3 4 5 6 7 8 9 10 11 #include #include int main () { std :: string strBuf ; std :: getline ( std :: cin , strBuf ); std :: cout << strBuf << '\\n' ; return 0 ; }","title":"\u7528\u4e8estd::string \u7684 getline()"},{"location":"23-2-Input-with-istream/#istream","text":"\u8fd8\u6709\u4e00\u4e9b\u66f4\u6709\u7528\u7684\u8f93\u5165\u51fd\u6570\u4f60\u53ef\u80fd\u4f1a\u7528\u5230\uff1a ignore() \uff1a\u5ffd\u7565\u6d41\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\uff1b ignore(int nCount) \uff1a\u5ffd\u7565\u6d41\u4e2d\u7684\u524d nCount \u4e2a\u5b57\u7b26\uff1b peek() \uff1a\u5141\u8bb8\u4f60\u4ece\u6d41\u4e2d\u8bfb\u53d6\u5b57\u7b26\u800c\u4e0d\u5c06\u5176\u4ece\u6d41\u4e2d\u5220\u9664\u3002 unget() \uff1a\u8fd4\u56de\u8bfb\u56de\u6d41\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\uff0c\u4ee5\u4fbf\u4e0b\u4e00\u6b21\u8c03\u7528\u53ef\u4ee5\u518d\u6b21\u8bfb\u53d6\u5b83\u3002 putback(char ch) \uff1a\u5141\u8bb8\u4f60\u5c06\u6240\u9009\u7684\u5b57\u7b26\u653e\u56de\u6d41\u4e2d\uff0c\u4ee5\u4fbf\u4e0b\u4e00\u4e2a\u8c03\u7528\u8bfb\u53d6\u3002\u8fd4\u56de\u8bfb\u56de\u6d41\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\uff0c\u4ee5\u4fbf\u4e0b\u4e00\u6b21\u8c03\u7528\u53ef\u4ee5\u518d\u6b21\u8bfb\u53d6\u5b83\u3002 istream \u5305\u542b\u8bb8\u591a\u5176\u4ed6\u51fd\u6570\u548c\u4e0a\u9762\u63d0\u5230\u7684\u51fd\u6570\u7684\u53d8\u4f53\uff0c\u5b83\u4eec\u53ef\u80fd\u6709\u7528\uff0c\u8fd9\u53d6\u51b3\u4e8e\u4f60\u9700\u8981\u505a\u4ec0\u4e48\u3002\u7136\u800c\uff0c\u8fd9\u4e9b\u8bae\u9898\u66f4\u9002\u5408\u653e\u5728\u4e13\u6ce8\u4e8e\u6807\u51c6\u5e93\u7684\u6559\u7a0b\u6216\u4e66\u4e2d(\u4f8b\u5982Nicolai M. Josuttis\u7684 \u201cc++\u6807\u51c6\u5e93\u201d )\u3002","title":"\u4e00\u4e9b\u66f4\u6709\u7528\u7684istream \u51fd\u6570"},{"location":"23-3-output-with-ostream-and-ios/","tags":["io"],"text":"Key Takeaway \u66f4\u6539\u683c\u5f0f\u5316\u7684\u65b9\u5f0f\u6709\u4e24\u79cd\uff0c\u6807\u8bb0\u548cmanipulators\u3002\u6807\u8bb0\u4f60\u53ef\u4ee5\u5c06\u5176\u770b\u505a\u662f\u53ef\u4ee5\u5f00\u5173\u7684\u5e03\u5c14\u91cf\u3002\u800cmanipulators\u5219\u662f\u653e\u7f6e\u5728\u6d41\u4e2d\uff0c\u53ef\u4ee5\u4fee\u6539\u8f93\u5165\u8f93\u51fa\u7684\u5bf9\u8c61 \u3002 setf() \u548c unsetf() \u5206\u522b\u7528\u4e8e\u8bbe\u7f6e\u548c\u5173\u95ed\u6807\u8bb0 \u8bb8\u591a\u6807\u5fd7\u5c5e\u4e8e\u7ec4\uff0c\u79f0\u4e3a\u683c\u5f0f\u7ec4\u3002 \u683c\u5f0f\u7ec4 \u662f\u4e00\u7ec4\u6267\u884c\u7c7b\u4f3c(\u6709\u65f6\u4e92\u65a5)\u683c\u5f0f\u5316\u9009\u9879\u7684\u6807\u5fd7\uff0c\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528\u4e24\u4e2a\u53c2\u6570\u7248\u672c\u7684 setf() \uff0c\u5e76\u901a\u8fc7\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u660e\u7ec4\u3002\u6b64\u65f6\u7ec4\u5185\u5176\u4ed6\u6807\u8bb0\u4f1a\u88ab\u81ea\u52a8\u5173\u95ed\u3002 \u672c\u8282\u8bfe\u6211\u4eec\u5c06\u4ecb\u7ecd iostream \u7684\u8f93\u51fa\u7c7b ostream \u3002 \u63d2\u5165\u8fd0\u7b97\u7b26 \u63d2\u5165\u8fd0\u7b97\u7b26 \u7528\u4e8e\u5411\u8f93\u51fa\u6d41\u63d2\u5165\u6570\u636e\u3002C++\u4e3a\u6240\u6709\u7684\u5185\u5efa\u6570\u636e\u7ed3\u6784\u90fd\u9884\u5b9a\u4e49\u4e86\u63d2\u5165\u8fd0\u7b97\u7b26\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7 \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 \u4e3a\u81ea\u5b9a\u4e49\u7684\u7c7b\u63d0\u4f9b\u63d2\u5165\u8fd0\u7b97\u7b26\u3002 \u5728 23.1 - \u8f93\u5165\u8f93\u51fa\u6d41 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c istream \u548c ostream \u90fd\u662f\u4ece ios \u7c7b\u6d3e\u751f\u6765\u7684\u3002 ios (\u548c ios_base ) \u7c7b\u7684\u4e00\u4e2a\u529f\u80fd\u5c31\u662f\u63a7\u5236\u8f93\u51fa\u7684\u683c\u5f0f\u3002 \u683c\u5f0f\u5316 \u66f4\u6539\u683c\u5f0f\u5316\u7684\u65b9\u5f0f\u6709\u4e24\u79cd\uff0c\u6807\u8bb0\u548cmanipulators\u3002\u6807\u8bb0\u4f60\u53ef\u4ee5\u5c06\u5176\u770b\u505a\u662f\u53ef\u4ee5\u5f00\u5173\u7684\u5e03\u5c14\u91cf\u3002\u800cmanipulators\u5219\u662f\u653e\u7f6e\u5728\u6d41\u4e2d\uff0c\u53ef\u4ee5\u4fee\u6539\u8f93\u5165\u8f93\u51fa\u7684\u5bf9\u8c61 \u3002 ==\u8bbe\u7f6e\u4e00\u4e2a\u6807\u8bb0\u53ef\u4ee5\u4f7f\u7528 setf() \u51fd\u6570\u5e76\u63d0\u4f9b\u5408\u9002\u7684\u6807\u8bb0\u4f5c\u4e3a\u53c2\u6570\u3002==\u4f8b\u5982\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0bC++\u5e76\u4e0d\u4f1a\u5728\u6b63\u6570\u524d\u52a0\u4e00\u4e2a+\u53f7\u3002\u4f46\u662f\uff0c\u4f7f\u7528 std::ios::showpos \u6807\u8bb0\u5c31\u53ef\u4ee5\u4fee\u6539\u8be5\u884c\u4e3a\uff1a 1 2 std :: cout . setf ( std :: ios :: showpos ); // \u6253\u5f00std::ios::showpos \u6807\u8bb0 std :: cout << 27 << '\\n' ; \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 +27 \u6211\u4eec\u4e5f\u53ef\u4ee5\u5229\u7528 \u6309\u4f4d\u6216 \u64cd\u4f5c\u4e00\u6b21\u6027\u6253\u5f00\u591a\u4e2a ios \u6807\u8bb0 \uff1a 1 2 std :: cout . setf ( std :: ios :: showpos | std :: ios :: uppercase ); // \u6253\u5f00 std::ios::showpos \u548c std::ios::uppercase \u6807\u8bb0 std :: cout << 1234567.89f << '\\n' ; \u8f93\u51fa\u7ed3\u679c\uff1a 1 +1.23457E+06 \u5173\u95ed\u6807\u8bb0\u4e5f\u5f88\u7b80\u5355\uff0c\u4f7f\u7528 unsetf() \u51fd\u6570\u5373\u53ef\uff1a 1 2 3 4 std :: cout . setf ( std :: ios :: showpos ); // turn on the std::ios::showpos flag std :: cout << 27 << '\\n' ; std :: cout . unsetf ( std :: ios :: showpos ); // turn off the std::ios::showpos flag std :: cout << 28 << '\\n' ; \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 +27 28 \u5728\u4f7f\u7528 setf() \u65f6\u8fd8\u6709\u4e00\u4e2a\u9700\u8981\u63d0\u53ca\u7684\u5c0f\u6280\u5de7\u3002 \u8bb8\u591a\u6807\u5fd7\u5c5e\u4e8e\u7ec4\uff0c\u79f0\u4e3a\u683c\u5f0f\u7ec4\u3002 \u683c\u5f0f\u7ec4 \u662f\u4e00\u7ec4\u6267\u884c\u7c7b\u4f3c(\u6709\u65f6\u4e92\u65a5)\u683c\u5f0f\u5316\u9009\u9879\u7684\u6807\u5fd7 \u3002\u4f8b\u5982\uff0c\u540d\u4e3a\u201cbasefield\u201d\u7684\u683c\u5f0f\u7ec4\u5305\u542b\u6807\u5fd7\u201coct\u201d\u3001\u201cdec\u201d\u548c\u201chex\u201d\uff0c\u5b83\u4eec\u63a7\u5236\u6574\u6570\u503c\u7684\u57fa\u6570\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u8bbe\u7f6e\u4e86\u201cdec\u201d\u6807\u5fd7\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u8fd9\u6837\u505a: 1 2 std :: cout . setf ( std :: ios :: hex ); // \u5c1d\u8bd5\u542f\u52a8\u5341\u516d\u8fdb\u5236\u8f93\u51fa std :: cout << 27 << '\\n' ; \u5219\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 27 \u6ca1\u6709\u6548\u679c\uff01\u8fd9\u662f\u56e0\u4e3a setf() \u53ea\u80fd\u6253\u5f00\u6807\u8bb0\u2014\u2014\u4f46\u662f\u5b83\u6ca1\u6709\u667a\u80fd\u5230\u61c2\u5f97\u53bb\u5173\u95ed\u4e92\u65a5\u7684\u6807\u8bb0\u3002\u56e0\u6b64\uff0c\u5f53 std::ios::hex \u5f00\u542f\u65f6\uff0c std::ios::dec \u4ecd\u7136\u662f\u5f00\u542f\u72b6\u6001\uff0c\u7531\u4e8e\u5b83\u4f18\u5148\u7ea7\u66f4\u9ad8\uff0c\u6240\u4ee5\u4ecd\u7136\u662f\u6309\u7167\u5341\u8fdb\u5236\u8f93\u51fa\u7684\u3002\u6709\u4e24\u4e2a\u529e\u6cd5\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002 \u7b2c\u4e00\u79cd\u65b9\u6cd5\u662f\u5173\u95ed std::ios::dec \u5e76\u5f00\u542f std::hex \uff1a 1 2 3 std :: cout . unsetf ( std :: ios :: dec ); // turn off decimal output std :: cout . setf ( std :: ios :: hex ); // turn on hexadecimal output std :: cout << 27 << '\\n' ; \u8f93\u51fa\u7ed3\u679c\u7b26\u5408\u9884\u671f\uff1a 1 1b \u7b2c\u4e8c\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u53e6\u4e00\u4e2a\u7248\u672c\u7684 setf() \uff0c\u5b83\u6709\u4e24\u4e2a\u5f62\u53c2\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u660e\u6807\u8bb0\u6240\u5c5e\u7684 \u683c\u5f0f\u7ec4 \u3002\u5f53\u4f7f\u7528\u8fd9\u4e2a\u7248\u672c\u7684 setf() \u65f6\uff0c\u540c\u7ec4\u7684\u5176\u4ed6\u6807\u8bb0\u90fd\u4f1a\u88ab\u81ea\u52a8\u5173\u95ed\uff0c\u53ea\u6709\u6211\u4eec\u8bbe\u7f6e\u7684\u6807\u8bb0\u4f1a\u88ab\u6253\u5f00\u3002\u4f8b\u5982\uff1a 1 2 3 // Turn on std::ios::hex as the only std::ios::basefield flag std :: cout . setf ( std :: ios :: hex , std :: ios :: basefield ); std :: cout << 27 << '\\n' ; \u8f93\u51fa\u7ed3\u679c\u4ecd\u7136\u7b26\u5408\u9884\u671f\uff1a 1 1b \u4f7f\u7528 setf() \u548c unsetf() \u662f\u6709\u70b9\u522b\u626d\u7684\uff0c\u56e0\u6b64C++\u63d0\u4f9b\u4e86\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u6765\u6539\u53d8\u683c\u5f0f\u5316\u9009\u9879\uff1amanipulators\u3002manipulators \u6700\u8d5e\u7684\u5730\u65b9\u5728\u4e8e\u5b83\u8db3\u591f\u667a\u80fd\uff0c\u5b83\u80fd\u591f\u81ea\u52a8\u5f00\u5173\u76f8\u5173\u8054\u7684\u6807\u8bb0\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86 manipulators \u6539\u53d8\u8ba1\u6570\u8fdb\u5236\uff1a 1 2 3 std :: cout << std :: hex << 27 << '\\n' ; // print 27 in hex std :: cout << 28 << '\\n' ; // \u4ecd\u7136\u662f\u5341\u516d\u8fdb\u5236\uff0c\u4e0d\u662f\u4e00\u6b21\u6027\u7684 std :: cout << std :: dec << 29 << '\\n' ; // back to decimal \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 3 1b 1c 29 \u901a\u5e38\uff0c\u4f7f\u7528manipulators\u6bd4\u8bbe\u7f6e\u548c\u53d6\u6d88\u8bbe\u7f6e\u6807\u8bb0\u8981\u5bb9\u6613\u5f97\u591a\u3002\u8bb8\u591a\u6807\u8bb0\u90fd\u53ef\u4ee5\u901a\u8fc7\u6807\u5fd7 \u548c manipulators\u6765\u5b9e\u73b0(\u4f8b\u5982\u66f4\u6539\u57fa\u6570)\uff0c\u7136\u800c\uff0c\u53e6\u5916\u4e00\u4e9b\u6807\u8bb0\u5219\u53ea\u80fd\u901a\u8fc7\u6807\u8bb0\u6216manipulators\u5b9e\u73b0\uff0c\u56e0\u6b64\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u8fd9\u4e24\u79cd\u65b9\u6cd5\u975e\u5e38\u91cd\u8981\u3002 \u6709\u7528\u7684\u6807\u8bb0 \u8fd9\u91cc\u6211\u4eec\u5217\u4e3e\u4e00\u4e9b\u5f88\u6709\u7528\u7684\u6807\u8bb0\u3001manipulators \u4ee5\u53ca\u6210\u5458\u51fd\u6570\u3002 \u6807\u8bb0\u4f4d\u4e8e std::ios \u7c7b\u4e2d\uff0cmanipulators \u5219\u4f4d\u4e8estd \u547d\u540d\u7a7a\u95f4 \u603b\uff0c\u800c\u6210\u5458\u51fd\u6570\u5219\u4f4d\u4e8e std::ostream \u7c7b\u4e2d\u3002 \u7ec4\u6807 \u8bb0 \u542b\u4e49 std::ios::boolalpha \u6253\u5f00\u8be5\u6807\u8bb0\u540e\uff0c\u5e03\u5c14\u7c7b\u578b\u4f1a\u6253\u5370 \u201ctrue\u201d \u6216 \u201cfalse\u201d\u3002\u5982\u679c\u5173\u95ed\uff0c\u5219\u59270\u62161 Manipulator \u542b\u4e49 std::boolalpha \u5e03\u5c14\u7c7b\u578b\u4f1a\u6253\u5370 \u201ctrue\u201d or \u201cfalse\u201d std::noboolalpha \u5e03\u5c14\u7c7b\u578b\u4f1a\u6253\u5370 0 or 1 (default) \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 std :: cout << true << ' ' << false << '\\n' ; std :: cout . setf ( std :: ios :: boolalpha ); std :: cout << true << ' ' << false << '\\n' ; std :: cout << std :: noboolalpha << true << ' ' << false << '\\n' ; std :: cout << std :: boolalpha << true << ' ' << false << '\\n' ; \u7ed3\u679c\uff1a 1 2 3 4 1 0 true false 1 0 true false \u7ec4 \u6807\u8bb0 \u542b\u4e49 std::ios::showpos \u8bbe\u7f6e\u540e\uff0c\u6b63\u6570\u524d\u9762\u4f1a\u6dfb\u52a0+\u53f7 Manipulator \u542b\u4e49 std::showpos \u6b63\u6570\u524d\u9762\u4f1a\u6dfb\u52a0+\u53f7 std::noshowpos \u6b63\u6570\u524d\u9762\u4e0d\u6dfb\u52a0+\u53f7 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 std :: cout << 5 << '\\n' ; std :: cout . setf ( std :: ios :: showpos ); std :: cout << 5 << '\\n' ; std :: cout << std :: noshowpos << 5 << '\\n' ; std :: cout << std :: showpos << 5 << '\\n' ; \u7ed3\u679c\uff1a 1 2 3 4 5 +5 5 +5 \u7ec4 \u6807\u8bb0 \u542b\u4e49 std::ios::uppercase If set, uses upper case letters Manipulator \u542b\u4e49 std::uppercase \u4f7f\u7528\u5927\u5199\u5b57\u6bcd std::nouppercase \u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 std :: cout << 12345678.9 << '\\n' ; std :: cout . setf ( std :: ios :: uppercase ); std :: cout << 12345678.9 << '\\n' ; std :: cout << std :: nouppercase << 12345678.9 << '\\n' ; std :: cout << std :: uppercase << 12345678.9 << '\\n' ; \u7ed3\u679c\uff1a 1 2 3 4 1.23457e+007 1.23457E+007 1.23457e+007 1.23457E+007 \u7ec4 \u6807\u8bb0 \u542b\u4e49 std::ios::basefield std::ios::dec \u6309\u7167\u5341\u8fdb\u5236\u6253\u5370\uff08\u9ed8\u8ba4\u7684\uff09 std::ios::basefield std::ios::hex \u6309\u7167\u5341\u516d\u8fdb\u5236\u6253\u5370 std::ios::basefield std::ios::oct \u6309\u7167\u516b\u8fdb\u5236\u6253\u5370 std::ios::basefield (none) \u6839\u636e\u524d\u7f00\u5b57\u6bcd\u6253\u5370 Manipulator Meaning std::dec \u6309\u7167\u5341\u8fdb\u5236\u6253\u5370 std::hex \u6309\u7167\u5341\u516d\u8fdb\u5236\u6253\u5370 std::oct \u6309\u7167\u516b\u8fdb\u5236\u6253\u5370 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 std :: cout << 27 << '\\n' ; std :: cout . setf ( std :: ios :: dec , std :: ios :: basefield ); std :: cout << 27 << '\\n' ; std :: cout . setf ( std :: ios :: oct , std :: ios :: basefield ); std :: cout << 27 << '\\n' ; std :: cout . setf ( std :: ios :: hex , std :: ios :: basefield ); std :: cout << 27 << '\\n' ; std :: cout << std :: dec << 27 << '\\n' ; std :: cout << std :: oct << 27 << '\\n' ; std :: cout << std :: hex << 27 << '\\n' ; \u7ed3\u679c\uff1a 1 2 3 4 5 6 7 27 27 33 1b 27 33 1b \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u770b\u5230\u901a\u8fc7\u6807\u5fd7\u548c\u901a\u8fc7\u64cd\u4f5c\u7b26\u8bbe\u7f6e\u683c\u5f0f\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u5728\u4ee5\u540e\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u64cd\u7eb5\u7b26\uff0c\u9664\u975e\u5b83\u4eec\u4e0d\u53ef\u7528\u3002 \u7cbe\u5ea6\u3001\u8ba1\u6570\u6cd5\u548c\u5c0f\u6570\u70b9 \u4f7f\u7528 manipulators (\u6216\u8005\u6807\u8bb0) \u4e5f\u53ef\u4ee5\u6539\u53d8\u6570\u503c\u6253\u5370\u65f6\u7684\u7cbe\u5ea6\u548c\u8981\u5c55\u793a\u7684\u5c0f\u6570\u70b9\u4f4d\u6570\u3002\u8fd9\u4e9b\u683c\u5f0f\u5316\u9009\u9879\u7684\u7ec4\u5408\u6bd4\u8f83\u590d\u6742\uff0c\u8ba9\u6211\u4eec\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\uff1a |\u7ec4 |\u6807\u8bb0 |\u542b\u4e49| |:--|:--|:--|:--| | | std::ios::floatfield | std::ios::fixed |\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528\u5341\u8fdb\u5236\u8bb0\u6570\u6cd5 | | std::ios::floatfield | std::ios::scientific |\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528\u79d1\u5b66\u8bb0\u6570\u6cd5 | | std::ios::floatfield |(none) |\u4f4d\u6570\u8f83\u5c11\u65f6\u4f7f\u7528\u56fa\u5b9a\u8ba1\u6570\u6cd5\uff0c\u4f4d\u6570\u591a\u65f6\u4f7f\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5 | | std::ios::floatfield | std::ios::showpoint |\u5bf9\u4e8e\u6d6e\u70b9\u503c\uff0c\u59cb\u7ec8\u663e\u793a\u5c0f\u6570\u70b9\u548c\u672b\u5c3e0 Manipulator \u542b\u4e49 std::fixed \u4f7f\u7528\u5341\u8fdb\u5236\u8bb0\u6570\u6cd5 std::scientific \u4f7f\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5 std::showpoint \u5bf9\u4e8e\u6d6e\u70b9\u503c\uff0c\u59cb\u7ec8\u663e\u793a\u5c0f\u6570\u70b9\u548c\u672b\u5c3e0 std::noshowpoint \u5bf9\u4e8e\u6d6e\u70b9\u503c\uff0c\u4e0d\u663e\u793a\u5c0f\u6570\u70b9\u548c\u672b\u5c3e0 std::setprecision(int) \u4e3a\u6d6e\u70b9\u6570\u8bbe\u7f6e\u7cbe\u5ea6(\u5b9a\u4e49\u5728 iomanip \u5934\u6587\u4ef6\u4e2d) \u6210\u5458\u51fd\u6570 \u542b\u4e49 std::ios_base::precision() \u8fd4\u56de\u5f53\u524d\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6 std::ios_base::precision(int) \u8bbe\u7f6e\u6d6e\u70b9\u6570\u7cbe\u5ea6\u5e76\u8fd4\u56de\u4e4b\u524d\u7684\u7cbe\u5ea6 \u5982\u679c\u4f7f\u7528\u56fa\u5b9a\u8bb0\u6570\u6cd5\u6216\u79d1\u5b66\u8bb0\u6570\u6cd5\uff0c\u5219\u7cbe\u5ea6\u51b3\u5b9a\u5206\u6570\u4e2d\u663e\u793a\u7684\u5c0f\u6570\u70b9\u540e\u591a\u5c11\u4f4d\u3002\u6ce8\u610f\uff0c\u5982\u679c\u7cbe\u5ea6\u5c0f\u4e8e\u6709\u6548\u4f4d\u6570\uff0c\u5219\u6570\u5b57\u5c06\u56db\u820d\u4e94\u5165\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 std :: cout << std :: fixed << '\\n' ; std :: cout << std :: setprecision ( 3 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 4 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 5 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 6 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 7 ) << 123.456 << '\\n' ; std :: cout << std :: scientific << '\\n' ; std :: cout << std :: setprecision ( 3 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 4 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 5 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 6 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 7 ) << 123.456 << '\\n' ; \u6253\u5370\uff1a 1 2 3 4 5 6 7 8 9 10 11 123.456 123.4560 123.45600 123.456000 123.4560000 1.235e+002 1.2346e+002 1.23456e+002 1.234560e+002 1.2345600e+002 \u5982\u679c\u65e2\u4e0d\u4f7f\u7528\u56fa\u5b9a\u8ba1\u6570\u6cd5\u4e5f\u4e0d\u4f7f\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5\uff0c\u5219\u7cbe\u5ea6\u51b3\u5b9a\u4e86\u5e94\u8be5\u663e\u793a\u591a\u5c11\u6709\u6548\u6570\u5b57\u3002\u540c\u6837\uff0c\u5982\u679c\u7cbe\u5ea6\u5c0f\u4e8e\u6709\u6548\u4f4d\u6570\uff0c\u5219\u6570\u5b57\u5c06\u56db\u820d\u4e94\u5165\u3002 1 2 3 4 5 std :: cout << std :: setprecision ( 3 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 4 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 5 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 6 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 7 ) << 123.456 << '\\n' ; \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 4 5 123 123.5 123.46 123.456 123.456 \u4f7f\u7528 showpoint manipulator \u6216\u6807\u8bb0\uff0c\u53ef\u4ee5\u6253\u5370\u5c0f\u6570\u70b9\u548c\u672b\u5c3e0\u3002 1 2 3 4 5 6 std :: cout << std :: showpoint << '\\n' ; std :: cout << std :: setprecision ( 3 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 4 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 5 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 6 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 7 ) << 123.456 << '\\n' ; \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 123. 123.5 123.46 123.456 123.4560 \u4e0b\u9762\u662f\u4e00\u4e2a\u6709\u66f4\u591a\u4f8b\u5b50\u7684\u6c47\u603b\u8868: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Option Precision 12345.0 0.12345 Normal 3 1.23e+004 0.123 4 1.235e+004 0.1235 5 12345 0.12345 6 12345 0.12345 Showpoint 3 1.23e+004 0.123 4 1.235e+004 0.1235 5 12345. 0.12345 6 12345.0 0.123450 Fixed 3 12345.000 0.123 4 12345.0000 0.1235 5 12345.00000 0.12345 6 12345.000000 0.123450 Scientific 3 1.235e+004 1.235e-001 4 1.2345e+004 1.2345e-001 5 1.23450e+004 1.23450e-001 6 1.234500e+004 1.234500e-001 \u5bbd\u5ea6\u3001\u586b\u5145\u5b57\u7b26\u548c\u5bf9\u9f50 \u901a\u5e38\uff0c\u5f53\u6211\u4eec\u6253\u5370\u6570\u5b57\u65f6\u662f\u4e0d\u8003\u8651\u5176\u5468\u56f4\u7684\u7a7a\u95f4\u7684\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u7684\u786e\u53ef\u4ee5\u8ba9\u6570\u5b57\u5411\u5de6\u6216\u5411\u53f3\u5bf9\u5176\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5fc5\u987b\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u5b57\u6bb5\u5bbd\u5ea6\u2014\u2014\u5373\u8f93\u51fa\u7a7a\u95f4\u7684\u5927\u5c0f\u3002\u5982\u679c\u5b9e\u9645\u6253\u5370\u7684\u6570\u5b57\u5c0f\u4e8e\u5b57\u6bb5\u5bbd\u5ea6\uff0c\u5b83\u5c06\u88ab\u5de6\u5bf9\u9f50\u6216\u53f3\u5bf9\u9f50(\u6839\u636e\u6307\u5b9a)\u3002\u5982\u679c\u5b9e\u9645\u6570\u5b57\u5927\u4e8e\u5b57\u6bb5\u5bbd\u5ea6\uff0c\u5b83\u5e76\u4e0d\u4f1a\u88ab\u622a\u65ad\u2014\u2014\u800c\u662f\u4f1a\u6ea2\u51fa\u3002 Group Flag Meaning std::ios::adjustfield std::ios::internal Left-justifies the sign of the number, and right-justifies the value std::ios::adjustfield std::ios::left Left-justifies the sign and value std::ios::adjustfield std::ios::right Right-justifies the sign and value (default) Manipulator Meaning std::internal Left-justifies the sign of the number, and right-justifies the value std::left Left-justifies the sign and value std::right Right-justifies the sign and value std::setfill(char) Sets the parameter as the fill character (defined in the iomanip header) std::setw(int) Sets the field width for input and output to the parameter (defined in the iomanip header) Member function Meaning std::basic_ostream::fill() Returns the current fill character std::basic_ostream::fill(char) Sets the fill character and returns the old fill character std::ios_base::width() Returns the current field width std::ios_base::width(int) Sets the current field width and returns old field width \u4e3a\u4e86\u4f7f\u7528\u8fd9\u4e9b\u683c\u5f0f\u5316\u64cd\u4f5c\uff0c\u6211\u4eec\u5fc5\u987b\u5148\u8bbe\u7f6e\u5bbd\u5ea6\u3002\u8bbe\u7f6e\u5bbd\u5ea6\u53ef\u4ee5\u901a\u8fc7 width(int) \u6210\u5458\u51fd\u6570\u6765\u5b8c\u6210\uff0c\u6216\u8005\u901a\u8fc7 setw() manipulator\u3002\u6ce8\u610f\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u53f3\u5bf9\u9f50\u7684\u3002 1 2 3 4 5 std :: cout << -12345 << '\\n' ; // print default value with no field width std :: cout << std :: setw ( 10 ) << -12345 << '\\n' ; // print default with field width std :: cout << std :: setw ( 10 ) << std :: left << -12345 << '\\n' ; // print left justified std :: cout << std :: setw ( 10 ) << std :: right << -12345 << '\\n' ; // print right justified std :: cout << std :: setw ( 10 ) << std :: internal << -12345 << '\\n' ; // print internally justified \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 2 3 4 5 -12345 -12345 -12345 -12345 - 12345 \u6ce8\u610f\uff0c setw() \u548c width() \u53ea\u5bf9\u4e0b\u4e00\u4e2a\u8f93\u51fa\u8bed\u53e5\u6709\u6548\uff0c\u548c\u5176\u4ed6\u7684\u6807\u8bb0\u6216manipulators\u4e0d\u540c\uff0c\u5b83\u4eec\u4e0d\u662f\u6301\u4e45\u5316\u7684\u3002 \u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u8bbe\u7f6e\u4e00\u4e2a\u586b\u5145\u5b57\u7b26\u5e76\u8fdb\u884c\u7c7b\u4f3c\u7684\u64cd\u4f5c\uff1a 1 2 3 4 5 6 std :: cout . fill ( '*' ); std :: cout << -12345 << '\\n' ; // print default value with no field width std :: cout << std :: setw ( 10 ) << -12345 << '\\n' ; // print default with field width std :: cout << std :: setw ( 10 ) << std :: left << -12345 << '\\n' ; // print left justified std :: cout << std :: setw ( 10 ) << std :: right << -12345 << '\\n' ; // print right justified std :: cout << std :: setw ( 10 ) << std :: internal << -12345 << '\\n' ; // print internally justified \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 5 -12345 ****-12345 -12345**** ****-12345 -****12345 \u6ce8\u610f\uff0c\u4e4b\u524d\u7684\u7a7a\u683c\u88ab\u586b\u5145\u5b57\u7b26\u6240\u66ff\u4ee3\u4e86\u3002 ostream \u7c7b\u548c iostream \u5e93\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u6709\u7528\u7684\u8f93\u51fa\u51fd\u6570\u3001\u6807\u8bb0\u548c manipulators\u3002\u548c istream \u7c7b\u4e00\u6837\uff0c\u8fd9\u4e9b\u8bae\u9898\u66f4\u9002\u5408\u653e\u5728\u4e13\u6ce8\u4e8e\u6807\u51c6\u5e93\u7684\u6559\u7a0b\u6216\u4e66\u4e2d(\u4f8b\u5982Nicolai M. Josuttis\u7684 \u201cc++\u6807\u51c6\u5e93\u201d )\u3002","title":"23.3 - \u4f7f\u7528ostream\u548cios\u5904\u7406\u8f93\u51fa"},{"location":"23-3-output-with-ostream-and-ios/#_1","text":"\u63d2\u5165\u8fd0\u7b97\u7b26 \u7528\u4e8e\u5411\u8f93\u51fa\u6d41\u63d2\u5165\u6570\u636e\u3002C++\u4e3a\u6240\u6709\u7684\u5185\u5efa\u6570\u636e\u7ed3\u6784\u90fd\u9884\u5b9a\u4e49\u4e86\u63d2\u5165\u8fd0\u7b97\u7b26\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7 \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 \u4e3a\u81ea\u5b9a\u4e49\u7684\u7c7b\u63d0\u4f9b\u63d2\u5165\u8fd0\u7b97\u7b26\u3002 \u5728 23.1 - \u8f93\u5165\u8f93\u51fa\u6d41 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c istream \u548c ostream \u90fd\u662f\u4ece ios \u7c7b\u6d3e\u751f\u6765\u7684\u3002 ios (\u548c ios_base ) \u7c7b\u7684\u4e00\u4e2a\u529f\u80fd\u5c31\u662f\u63a7\u5236\u8f93\u51fa\u7684\u683c\u5f0f\u3002","title":"\u63d2\u5165\u8fd0\u7b97\u7b26"},{"location":"23-3-output-with-ostream-and-ios/#_2","text":"\u66f4\u6539\u683c\u5f0f\u5316\u7684\u65b9\u5f0f\u6709\u4e24\u79cd\uff0c\u6807\u8bb0\u548cmanipulators\u3002\u6807\u8bb0\u4f60\u53ef\u4ee5\u5c06\u5176\u770b\u505a\u662f\u53ef\u4ee5\u5f00\u5173\u7684\u5e03\u5c14\u91cf\u3002\u800cmanipulators\u5219\u662f\u653e\u7f6e\u5728\u6d41\u4e2d\uff0c\u53ef\u4ee5\u4fee\u6539\u8f93\u5165\u8f93\u51fa\u7684\u5bf9\u8c61 \u3002 ==\u8bbe\u7f6e\u4e00\u4e2a\u6807\u8bb0\u53ef\u4ee5\u4f7f\u7528 setf() \u51fd\u6570\u5e76\u63d0\u4f9b\u5408\u9002\u7684\u6807\u8bb0\u4f5c\u4e3a\u53c2\u6570\u3002==\u4f8b\u5982\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0bC++\u5e76\u4e0d\u4f1a\u5728\u6b63\u6570\u524d\u52a0\u4e00\u4e2a+\u53f7\u3002\u4f46\u662f\uff0c\u4f7f\u7528 std::ios::showpos \u6807\u8bb0\u5c31\u53ef\u4ee5\u4fee\u6539\u8be5\u884c\u4e3a\uff1a 1 2 std :: cout . setf ( std :: ios :: showpos ); // \u6253\u5f00std::ios::showpos \u6807\u8bb0 std :: cout << 27 << '\\n' ; \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 +27 \u6211\u4eec\u4e5f\u53ef\u4ee5\u5229\u7528 \u6309\u4f4d\u6216 \u64cd\u4f5c\u4e00\u6b21\u6027\u6253\u5f00\u591a\u4e2a ios \u6807\u8bb0 \uff1a 1 2 std :: cout . setf ( std :: ios :: showpos | std :: ios :: uppercase ); // \u6253\u5f00 std::ios::showpos \u548c std::ios::uppercase \u6807\u8bb0 std :: cout << 1234567.89f << '\\n' ; \u8f93\u51fa\u7ed3\u679c\uff1a 1 +1.23457E+06 \u5173\u95ed\u6807\u8bb0\u4e5f\u5f88\u7b80\u5355\uff0c\u4f7f\u7528 unsetf() \u51fd\u6570\u5373\u53ef\uff1a 1 2 3 4 std :: cout . setf ( std :: ios :: showpos ); // turn on the std::ios::showpos flag std :: cout << 27 << '\\n' ; std :: cout . unsetf ( std :: ios :: showpos ); // turn off the std::ios::showpos flag std :: cout << 28 << '\\n' ; \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 +27 28 \u5728\u4f7f\u7528 setf() \u65f6\u8fd8\u6709\u4e00\u4e2a\u9700\u8981\u63d0\u53ca\u7684\u5c0f\u6280\u5de7\u3002 \u8bb8\u591a\u6807\u5fd7\u5c5e\u4e8e\u7ec4\uff0c\u79f0\u4e3a\u683c\u5f0f\u7ec4\u3002 \u683c\u5f0f\u7ec4 \u662f\u4e00\u7ec4\u6267\u884c\u7c7b\u4f3c(\u6709\u65f6\u4e92\u65a5)\u683c\u5f0f\u5316\u9009\u9879\u7684\u6807\u5fd7 \u3002\u4f8b\u5982\uff0c\u540d\u4e3a\u201cbasefield\u201d\u7684\u683c\u5f0f\u7ec4\u5305\u542b\u6807\u5fd7\u201coct\u201d\u3001\u201cdec\u201d\u548c\u201chex\u201d\uff0c\u5b83\u4eec\u63a7\u5236\u6574\u6570\u503c\u7684\u57fa\u6570\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u8bbe\u7f6e\u4e86\u201cdec\u201d\u6807\u5fd7\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u8fd9\u6837\u505a: 1 2 std :: cout . setf ( std :: ios :: hex ); // \u5c1d\u8bd5\u542f\u52a8\u5341\u516d\u8fdb\u5236\u8f93\u51fa std :: cout << 27 << '\\n' ; \u5219\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 27 \u6ca1\u6709\u6548\u679c\uff01\u8fd9\u662f\u56e0\u4e3a setf() \u53ea\u80fd\u6253\u5f00\u6807\u8bb0\u2014\u2014\u4f46\u662f\u5b83\u6ca1\u6709\u667a\u80fd\u5230\u61c2\u5f97\u53bb\u5173\u95ed\u4e92\u65a5\u7684\u6807\u8bb0\u3002\u56e0\u6b64\uff0c\u5f53 std::ios::hex \u5f00\u542f\u65f6\uff0c std::ios::dec \u4ecd\u7136\u662f\u5f00\u542f\u72b6\u6001\uff0c\u7531\u4e8e\u5b83\u4f18\u5148\u7ea7\u66f4\u9ad8\uff0c\u6240\u4ee5\u4ecd\u7136\u662f\u6309\u7167\u5341\u8fdb\u5236\u8f93\u51fa\u7684\u3002\u6709\u4e24\u4e2a\u529e\u6cd5\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002 \u7b2c\u4e00\u79cd\u65b9\u6cd5\u662f\u5173\u95ed std::ios::dec \u5e76\u5f00\u542f std::hex \uff1a 1 2 3 std :: cout . unsetf ( std :: ios :: dec ); // turn off decimal output std :: cout . setf ( std :: ios :: hex ); // turn on hexadecimal output std :: cout << 27 << '\\n' ; \u8f93\u51fa\u7ed3\u679c\u7b26\u5408\u9884\u671f\uff1a 1 1b \u7b2c\u4e8c\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u53e6\u4e00\u4e2a\u7248\u672c\u7684 setf() \uff0c\u5b83\u6709\u4e24\u4e2a\u5f62\u53c2\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u660e\u6807\u8bb0\u6240\u5c5e\u7684 \u683c\u5f0f\u7ec4 \u3002\u5f53\u4f7f\u7528\u8fd9\u4e2a\u7248\u672c\u7684 setf() \u65f6\uff0c\u540c\u7ec4\u7684\u5176\u4ed6\u6807\u8bb0\u90fd\u4f1a\u88ab\u81ea\u52a8\u5173\u95ed\uff0c\u53ea\u6709\u6211\u4eec\u8bbe\u7f6e\u7684\u6807\u8bb0\u4f1a\u88ab\u6253\u5f00\u3002\u4f8b\u5982\uff1a 1 2 3 // Turn on std::ios::hex as the only std::ios::basefield flag std :: cout . setf ( std :: ios :: hex , std :: ios :: basefield ); std :: cout << 27 << '\\n' ; \u8f93\u51fa\u7ed3\u679c\u4ecd\u7136\u7b26\u5408\u9884\u671f\uff1a 1 1b \u4f7f\u7528 setf() \u548c unsetf() \u662f\u6709\u70b9\u522b\u626d\u7684\uff0c\u56e0\u6b64C++\u63d0\u4f9b\u4e86\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u6765\u6539\u53d8\u683c\u5f0f\u5316\u9009\u9879\uff1amanipulators\u3002manipulators \u6700\u8d5e\u7684\u5730\u65b9\u5728\u4e8e\u5b83\u8db3\u591f\u667a\u80fd\uff0c\u5b83\u80fd\u591f\u81ea\u52a8\u5f00\u5173\u76f8\u5173\u8054\u7684\u6807\u8bb0\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86 manipulators \u6539\u53d8\u8ba1\u6570\u8fdb\u5236\uff1a 1 2 3 std :: cout << std :: hex << 27 << '\\n' ; // print 27 in hex std :: cout << 28 << '\\n' ; // \u4ecd\u7136\u662f\u5341\u516d\u8fdb\u5236\uff0c\u4e0d\u662f\u4e00\u6b21\u6027\u7684 std :: cout << std :: dec << 29 << '\\n' ; // back to decimal \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 3 1b 1c 29 \u901a\u5e38\uff0c\u4f7f\u7528manipulators\u6bd4\u8bbe\u7f6e\u548c\u53d6\u6d88\u8bbe\u7f6e\u6807\u8bb0\u8981\u5bb9\u6613\u5f97\u591a\u3002\u8bb8\u591a\u6807\u8bb0\u90fd\u53ef\u4ee5\u901a\u8fc7\u6807\u5fd7 \u548c manipulators\u6765\u5b9e\u73b0(\u4f8b\u5982\u66f4\u6539\u57fa\u6570)\uff0c\u7136\u800c\uff0c\u53e6\u5916\u4e00\u4e9b\u6807\u8bb0\u5219\u53ea\u80fd\u901a\u8fc7\u6807\u8bb0\u6216manipulators\u5b9e\u73b0\uff0c\u56e0\u6b64\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u8fd9\u4e24\u79cd\u65b9\u6cd5\u975e\u5e38\u91cd\u8981\u3002","title":"\u683c\u5f0f\u5316"},{"location":"23-3-output-with-ostream-and-ios/#_3","text":"\u8fd9\u91cc\u6211\u4eec\u5217\u4e3e\u4e00\u4e9b\u5f88\u6709\u7528\u7684\u6807\u8bb0\u3001manipulators \u4ee5\u53ca\u6210\u5458\u51fd\u6570\u3002 \u6807\u8bb0\u4f4d\u4e8e std::ios \u7c7b\u4e2d\uff0cmanipulators \u5219\u4f4d\u4e8estd \u547d\u540d\u7a7a\u95f4 \u603b\uff0c\u800c\u6210\u5458\u51fd\u6570\u5219\u4f4d\u4e8e std::ostream \u7c7b\u4e2d\u3002 \u7ec4\u6807 \u8bb0 \u542b\u4e49 std::ios::boolalpha \u6253\u5f00\u8be5\u6807\u8bb0\u540e\uff0c\u5e03\u5c14\u7c7b\u578b\u4f1a\u6253\u5370 \u201ctrue\u201d \u6216 \u201cfalse\u201d\u3002\u5982\u679c\u5173\u95ed\uff0c\u5219\u59270\u62161 Manipulator \u542b\u4e49 std::boolalpha \u5e03\u5c14\u7c7b\u578b\u4f1a\u6253\u5370 \u201ctrue\u201d or \u201cfalse\u201d std::noboolalpha \u5e03\u5c14\u7c7b\u578b\u4f1a\u6253\u5370 0 or 1 (default) \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 std :: cout << true << ' ' << false << '\\n' ; std :: cout . setf ( std :: ios :: boolalpha ); std :: cout << true << ' ' << false << '\\n' ; std :: cout << std :: noboolalpha << true << ' ' << false << '\\n' ; std :: cout << std :: boolalpha << true << ' ' << false << '\\n' ; \u7ed3\u679c\uff1a 1 2 3 4 1 0 true false 1 0 true false \u7ec4 \u6807\u8bb0 \u542b\u4e49 std::ios::showpos \u8bbe\u7f6e\u540e\uff0c\u6b63\u6570\u524d\u9762\u4f1a\u6dfb\u52a0+\u53f7 Manipulator \u542b\u4e49 std::showpos \u6b63\u6570\u524d\u9762\u4f1a\u6dfb\u52a0+\u53f7 std::noshowpos \u6b63\u6570\u524d\u9762\u4e0d\u6dfb\u52a0+\u53f7 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 std :: cout << 5 << '\\n' ; std :: cout . setf ( std :: ios :: showpos ); std :: cout << 5 << '\\n' ; std :: cout << std :: noshowpos << 5 << '\\n' ; std :: cout << std :: showpos << 5 << '\\n' ; \u7ed3\u679c\uff1a 1 2 3 4 5 +5 5 +5 \u7ec4 \u6807\u8bb0 \u542b\u4e49 std::ios::uppercase If set, uses upper case letters Manipulator \u542b\u4e49 std::uppercase \u4f7f\u7528\u5927\u5199\u5b57\u6bcd std::nouppercase \u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 std :: cout << 12345678.9 << '\\n' ; std :: cout . setf ( std :: ios :: uppercase ); std :: cout << 12345678.9 << '\\n' ; std :: cout << std :: nouppercase << 12345678.9 << '\\n' ; std :: cout << std :: uppercase << 12345678.9 << '\\n' ; \u7ed3\u679c\uff1a 1 2 3 4 1.23457e+007 1.23457E+007 1.23457e+007 1.23457E+007 \u7ec4 \u6807\u8bb0 \u542b\u4e49 std::ios::basefield std::ios::dec \u6309\u7167\u5341\u8fdb\u5236\u6253\u5370\uff08\u9ed8\u8ba4\u7684\uff09 std::ios::basefield std::ios::hex \u6309\u7167\u5341\u516d\u8fdb\u5236\u6253\u5370 std::ios::basefield std::ios::oct \u6309\u7167\u516b\u8fdb\u5236\u6253\u5370 std::ios::basefield (none) \u6839\u636e\u524d\u7f00\u5b57\u6bcd\u6253\u5370 Manipulator Meaning std::dec \u6309\u7167\u5341\u8fdb\u5236\u6253\u5370 std::hex \u6309\u7167\u5341\u516d\u8fdb\u5236\u6253\u5370 std::oct \u6309\u7167\u516b\u8fdb\u5236\u6253\u5370 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 std :: cout << 27 << '\\n' ; std :: cout . setf ( std :: ios :: dec , std :: ios :: basefield ); std :: cout << 27 << '\\n' ; std :: cout . setf ( std :: ios :: oct , std :: ios :: basefield ); std :: cout << 27 << '\\n' ; std :: cout . setf ( std :: ios :: hex , std :: ios :: basefield ); std :: cout << 27 << '\\n' ; std :: cout << std :: dec << 27 << '\\n' ; std :: cout << std :: oct << 27 << '\\n' ; std :: cout << std :: hex << 27 << '\\n' ; \u7ed3\u679c\uff1a 1 2 3 4 5 6 7 27 27 33 1b 27 33 1b \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u770b\u5230\u901a\u8fc7\u6807\u5fd7\u548c\u901a\u8fc7\u64cd\u4f5c\u7b26\u8bbe\u7f6e\u683c\u5f0f\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u5728\u4ee5\u540e\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u64cd\u7eb5\u7b26\uff0c\u9664\u975e\u5b83\u4eec\u4e0d\u53ef\u7528\u3002","title":"\u6709\u7528\u7684\u6807\u8bb0"},{"location":"23-3-output-with-ostream-and-ios/#_4","text":"\u4f7f\u7528 manipulators (\u6216\u8005\u6807\u8bb0) \u4e5f\u53ef\u4ee5\u6539\u53d8\u6570\u503c\u6253\u5370\u65f6\u7684\u7cbe\u5ea6\u548c\u8981\u5c55\u793a\u7684\u5c0f\u6570\u70b9\u4f4d\u6570\u3002\u8fd9\u4e9b\u683c\u5f0f\u5316\u9009\u9879\u7684\u7ec4\u5408\u6bd4\u8f83\u590d\u6742\uff0c\u8ba9\u6211\u4eec\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\uff1a |\u7ec4 |\u6807\u8bb0 |\u542b\u4e49| |:--|:--|:--|:--| | | std::ios::floatfield | std::ios::fixed |\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528\u5341\u8fdb\u5236\u8bb0\u6570\u6cd5 | | std::ios::floatfield | std::ios::scientific |\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528\u79d1\u5b66\u8bb0\u6570\u6cd5 | | std::ios::floatfield |(none) |\u4f4d\u6570\u8f83\u5c11\u65f6\u4f7f\u7528\u56fa\u5b9a\u8ba1\u6570\u6cd5\uff0c\u4f4d\u6570\u591a\u65f6\u4f7f\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5 | | std::ios::floatfield | std::ios::showpoint |\u5bf9\u4e8e\u6d6e\u70b9\u503c\uff0c\u59cb\u7ec8\u663e\u793a\u5c0f\u6570\u70b9\u548c\u672b\u5c3e0 Manipulator \u542b\u4e49 std::fixed \u4f7f\u7528\u5341\u8fdb\u5236\u8bb0\u6570\u6cd5 std::scientific \u4f7f\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5 std::showpoint \u5bf9\u4e8e\u6d6e\u70b9\u503c\uff0c\u59cb\u7ec8\u663e\u793a\u5c0f\u6570\u70b9\u548c\u672b\u5c3e0 std::noshowpoint \u5bf9\u4e8e\u6d6e\u70b9\u503c\uff0c\u4e0d\u663e\u793a\u5c0f\u6570\u70b9\u548c\u672b\u5c3e0 std::setprecision(int) \u4e3a\u6d6e\u70b9\u6570\u8bbe\u7f6e\u7cbe\u5ea6(\u5b9a\u4e49\u5728 iomanip \u5934\u6587\u4ef6\u4e2d) \u6210\u5458\u51fd\u6570 \u542b\u4e49 std::ios_base::precision() \u8fd4\u56de\u5f53\u524d\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6 std::ios_base::precision(int) \u8bbe\u7f6e\u6d6e\u70b9\u6570\u7cbe\u5ea6\u5e76\u8fd4\u56de\u4e4b\u524d\u7684\u7cbe\u5ea6 \u5982\u679c\u4f7f\u7528\u56fa\u5b9a\u8bb0\u6570\u6cd5\u6216\u79d1\u5b66\u8bb0\u6570\u6cd5\uff0c\u5219\u7cbe\u5ea6\u51b3\u5b9a\u5206\u6570\u4e2d\u663e\u793a\u7684\u5c0f\u6570\u70b9\u540e\u591a\u5c11\u4f4d\u3002\u6ce8\u610f\uff0c\u5982\u679c\u7cbe\u5ea6\u5c0f\u4e8e\u6709\u6548\u4f4d\u6570\uff0c\u5219\u6570\u5b57\u5c06\u56db\u820d\u4e94\u5165\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 std :: cout << std :: fixed << '\\n' ; std :: cout << std :: setprecision ( 3 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 4 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 5 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 6 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 7 ) << 123.456 << '\\n' ; std :: cout << std :: scientific << '\\n' ; std :: cout << std :: setprecision ( 3 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 4 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 5 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 6 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 7 ) << 123.456 << '\\n' ; \u6253\u5370\uff1a 1 2 3 4 5 6 7 8 9 10 11 123.456 123.4560 123.45600 123.456000 123.4560000 1.235e+002 1.2346e+002 1.23456e+002 1.234560e+002 1.2345600e+002 \u5982\u679c\u65e2\u4e0d\u4f7f\u7528\u56fa\u5b9a\u8ba1\u6570\u6cd5\u4e5f\u4e0d\u4f7f\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5\uff0c\u5219\u7cbe\u5ea6\u51b3\u5b9a\u4e86\u5e94\u8be5\u663e\u793a\u591a\u5c11\u6709\u6548\u6570\u5b57\u3002\u540c\u6837\uff0c\u5982\u679c\u7cbe\u5ea6\u5c0f\u4e8e\u6709\u6548\u4f4d\u6570\uff0c\u5219\u6570\u5b57\u5c06\u56db\u820d\u4e94\u5165\u3002 1 2 3 4 5 std :: cout << std :: setprecision ( 3 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 4 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 5 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 6 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 7 ) << 123.456 << '\\n' ; \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 4 5 123 123.5 123.46 123.456 123.456 \u4f7f\u7528 showpoint manipulator \u6216\u6807\u8bb0\uff0c\u53ef\u4ee5\u6253\u5370\u5c0f\u6570\u70b9\u548c\u672b\u5c3e0\u3002 1 2 3 4 5 6 std :: cout << std :: showpoint << '\\n' ; std :: cout << std :: setprecision ( 3 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 4 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 5 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 6 ) << 123.456 << '\\n' ; std :: cout << std :: setprecision ( 7 ) << 123.456 << '\\n' ; \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 123. 123.5 123.46 123.456 123.4560 \u4e0b\u9762\u662f\u4e00\u4e2a\u6709\u66f4\u591a\u4f8b\u5b50\u7684\u6c47\u603b\u8868: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Option Precision 12345.0 0.12345 Normal 3 1.23e+004 0.123 4 1.235e+004 0.1235 5 12345 0.12345 6 12345 0.12345 Showpoint 3 1.23e+004 0.123 4 1.235e+004 0.1235 5 12345. 0.12345 6 12345.0 0.123450 Fixed 3 12345.000 0.123 4 12345.0000 0.1235 5 12345.00000 0.12345 6 12345.000000 0.123450 Scientific 3 1.235e+004 1.235e-001 4 1.2345e+004 1.2345e-001 5 1.23450e+004 1.23450e-001 6 1.234500e+004 1.234500e-001","title":"\u7cbe\u5ea6\u3001\u8ba1\u6570\u6cd5\u548c\u5c0f\u6570\u70b9"},{"location":"23-3-output-with-ostream-and-ios/#_5","text":"\u901a\u5e38\uff0c\u5f53\u6211\u4eec\u6253\u5370\u6570\u5b57\u65f6\u662f\u4e0d\u8003\u8651\u5176\u5468\u56f4\u7684\u7a7a\u95f4\u7684\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u7684\u786e\u53ef\u4ee5\u8ba9\u6570\u5b57\u5411\u5de6\u6216\u5411\u53f3\u5bf9\u5176\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5fc5\u987b\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u5b57\u6bb5\u5bbd\u5ea6\u2014\u2014\u5373\u8f93\u51fa\u7a7a\u95f4\u7684\u5927\u5c0f\u3002\u5982\u679c\u5b9e\u9645\u6253\u5370\u7684\u6570\u5b57\u5c0f\u4e8e\u5b57\u6bb5\u5bbd\u5ea6\uff0c\u5b83\u5c06\u88ab\u5de6\u5bf9\u9f50\u6216\u53f3\u5bf9\u9f50(\u6839\u636e\u6307\u5b9a)\u3002\u5982\u679c\u5b9e\u9645\u6570\u5b57\u5927\u4e8e\u5b57\u6bb5\u5bbd\u5ea6\uff0c\u5b83\u5e76\u4e0d\u4f1a\u88ab\u622a\u65ad\u2014\u2014\u800c\u662f\u4f1a\u6ea2\u51fa\u3002 Group Flag Meaning std::ios::adjustfield std::ios::internal Left-justifies the sign of the number, and right-justifies the value std::ios::adjustfield std::ios::left Left-justifies the sign and value std::ios::adjustfield std::ios::right Right-justifies the sign and value (default) Manipulator Meaning std::internal Left-justifies the sign of the number, and right-justifies the value std::left Left-justifies the sign and value std::right Right-justifies the sign and value std::setfill(char) Sets the parameter as the fill character (defined in the iomanip header) std::setw(int) Sets the field width for input and output to the parameter (defined in the iomanip header) Member function Meaning std::basic_ostream::fill() Returns the current fill character std::basic_ostream::fill(char) Sets the fill character and returns the old fill character std::ios_base::width() Returns the current field width std::ios_base::width(int) Sets the current field width and returns old field width \u4e3a\u4e86\u4f7f\u7528\u8fd9\u4e9b\u683c\u5f0f\u5316\u64cd\u4f5c\uff0c\u6211\u4eec\u5fc5\u987b\u5148\u8bbe\u7f6e\u5bbd\u5ea6\u3002\u8bbe\u7f6e\u5bbd\u5ea6\u53ef\u4ee5\u901a\u8fc7 width(int) \u6210\u5458\u51fd\u6570\u6765\u5b8c\u6210\uff0c\u6216\u8005\u901a\u8fc7 setw() manipulator\u3002\u6ce8\u610f\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u53f3\u5bf9\u9f50\u7684\u3002 1 2 3 4 5 std :: cout << -12345 << '\\n' ; // print default value with no field width std :: cout << std :: setw ( 10 ) << -12345 << '\\n' ; // print default with field width std :: cout << std :: setw ( 10 ) << std :: left << -12345 << '\\n' ; // print left justified std :: cout << std :: setw ( 10 ) << std :: right << -12345 << '\\n' ; // print right justified std :: cout << std :: setw ( 10 ) << std :: internal << -12345 << '\\n' ; // print internally justified \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 2 3 4 5 -12345 -12345 -12345 -12345 - 12345 \u6ce8\u610f\uff0c setw() \u548c width() \u53ea\u5bf9\u4e0b\u4e00\u4e2a\u8f93\u51fa\u8bed\u53e5\u6709\u6548\uff0c\u548c\u5176\u4ed6\u7684\u6807\u8bb0\u6216manipulators\u4e0d\u540c\uff0c\u5b83\u4eec\u4e0d\u662f\u6301\u4e45\u5316\u7684\u3002 \u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u8bbe\u7f6e\u4e00\u4e2a\u586b\u5145\u5b57\u7b26\u5e76\u8fdb\u884c\u7c7b\u4f3c\u7684\u64cd\u4f5c\uff1a 1 2 3 4 5 6 std :: cout . fill ( '*' ); std :: cout << -12345 << '\\n' ; // print default value with no field width std :: cout << std :: setw ( 10 ) << -12345 << '\\n' ; // print default with field width std :: cout << std :: setw ( 10 ) << std :: left << -12345 << '\\n' ; // print left justified std :: cout << std :: setw ( 10 ) << std :: right << -12345 << '\\n' ; // print right justified std :: cout << std :: setw ( 10 ) << std :: internal << -12345 << '\\n' ; // print internally justified \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 5 -12345 ****-12345 -12345**** ****-12345 -****12345 \u6ce8\u610f\uff0c\u4e4b\u524d\u7684\u7a7a\u683c\u88ab\u586b\u5145\u5b57\u7b26\u6240\u66ff\u4ee3\u4e86\u3002 ostream \u7c7b\u548c iostream \u5e93\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u6709\u7528\u7684\u8f93\u51fa\u51fd\u6570\u3001\u6807\u8bb0\u548c manipulators\u3002\u548c istream \u7c7b\u4e00\u6837\uff0c\u8fd9\u4e9b\u8bae\u9898\u66f4\u9002\u5408\u653e\u5728\u4e13\u6ce8\u4e8e\u6807\u51c6\u5e93\u7684\u6559\u7a0b\u6216\u4e66\u4e2d(\u4f8b\u5982Nicolai M. Josuttis\u7684 \u201cc++\u6807\u51c6\u5e93\u201d )\u3002","title":"\u5bbd\u5ea6\u3001\u586b\u5145\u5b57\u7b26\u548c\u5bf9\u9f50"},{"location":"23-4-stream-classes-for-strings/","tags":["io","string"],"text":"Key Takeaway \u4f7f\u7528 \u63d0\u53d6\u8fd0\u7b97\u7b26 \u4ece\u6d41\u4e2d\u63d0\u53d6\u201c\u6570\u5b57\u683c\u5f0f\u201d\u7684\u5b57\u7b26\u4e32\u5230\u6570\u503c\u7c7b\u578b\uff0c\u5373\u53ef\u5b8c\u6210\u5b57\u7b26\u4e32\u5230\u6570\u503c\u7684\u8f6c\u6362 \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u4f60\u770b\u5230\u7684\u6240\u6709I/O\u793a\u4f8b\u90fd\u662f\u4ece cout \u5199\u5165\u6216\u4ece cin \u8bfb\u53d6\u7684\u3002\u4f46\u662f\uff0c\u8fd8\u6709\u53e6\u4e00\u7ec4\u79f0\u4e3a\u5b57\u7b26\u4e32\u6d41\u7c7b\u7684\u7c7b\uff0c\u5b83\u5141\u8bb8\u4f60\u4f7f\u7528\u719f\u6089\u7684 \u63d2\u5165\u8fd0\u7b97\u7b26 \u548c \u63d0\u53d6\u8fd0\u7b97\u7b26 \u6765\u5904\u7406\u5b57\u7b26\u4e32\u3002\u50cf istream \u548c ostream \u4e00\u6837\uff0c\u5b57\u7b26\u4e32\u6d41\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7f13\u51b2\u533a\u6765\u4fdd\u5b58\u6570\u636e\u3002\u7136\u800c\uff0c\u4e0e cin \u548c cout \u4e0d\u540c\u7684\u662f\uff0c\u8fd9\u4e9b\u6d41\u4e0d\u8fde\u63a5\u5230I/O\u901a\u9053(\u5982\u952e\u76d8\u3001\u663e\u793a\u5668\u7b49)\u3002\u5b57\u7b26\u4e32\u6d41\u7684\u4e3b\u8981\u7528\u9014\u4e4b\u4e00\u662f\u7f13\u51b2\u8f93\u51fa\u4ee5\u4fbf\u7a0d\u540e\u663e\u793a\uff0c\u6216\u8005\u9010\u884c\u5904\u7406\u8f93\u5165\u3002 \u5b57\u7b26\u4e32\u6709\u516d\u4e2a\u6d41\u7c7b: \u5176\u4e2d istringstream (\u6d3e\u751f\u81ea istream )\u3001 ostringstream (\u6d3e\u751f\u81ea ostream )\u548c stringstream (\u6d3e\u751f\u81ea iostream )\u7528\u4e8e\u8bfb\u5199\u666e\u901a\u5b57\u7b26\u5bbd\u5ea6\u7684\u5b57\u7b26\u4e32\u3002 Wistringstream \u3001 wostringstream \u548c wstringstream \u7528\u4e8e\u8bfb\u5199\u5bbd\u5b57\u7b26\u4e32\u3002\u8981\u4f7f\u7528 stringstreams \uff0c\u4f60\u9700\u8981 #include sstream \u5934\u6587\u4ef6\u3002 \u6709\u4e24\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u5411 stringstream \u8f93\u5165\u6570\u636e\uff1a \u4f7f\u7528 \u63d2\u5165\u8fd0\u7b97\u7b26 \uff1a 1 2 std :: stringstream os ; os << \"en garde! \\n \" ; // insert \"en garde!\" into the stringstream \u4f7f\u7528 str(string) \u51fd\u6570\u8bbe\u7f6e\u5b57\u7b26\u4e32\u5230\u7f13\u51b2\uff1a 1 2 std :: stringstream os ; os . str ( \"en garde!\" ); // set the stringstream buffer to \"en garde!\" \u7c7b\u4f3c\u7684\uff0c\u4ece stringstream \u8bfb\u53d6\u4e5f\u6709\u4e24\u4e2a\u65b9\u6cd5\uff1a \u4f7f\u7528 str() \u51fd\u6570\u83b7\u53d6\u7f13\u51b2\u4e2d\u7684\u5185\u5bb9\uff1a 1 2 3 std :: stringstream os ; os << \"12345 67.89 \\n \" ; std :: cout << os . str (); \u6253\u5370\uff1a 1 12345 67.89 \u4f7f\u7528 \u63d0\u53d6\u8fd0\u7b97\u7b26 \uff1a 1 2 3 4 5 6 7 8 9 10 11 std :: stringstream os ; os << \"12345 67.89\" ; // insert a string of numbers into the stream std :: string strValue ; os >> strValue ; std :: string strValue2 ; os >> strValue2 ; // print the numbers separated by a dash std :: cout << strValue << \" - \" << strValue2 << '\\n' ; \u7a0b\u5e8f\u6253\u5370\uff1a 1 12345 - 67.89 \u6ce8\u610f\uff0c\u63d0\u53d6\u8fd0\u7b97\u7b26\u4f1a\u904d\u5386\u5b57\u7b26\u4e32\uff0c\u6bcf\u4e2a>>\u4f1a\u8fd4\u56de\u6d41\u4e2d\u4e0b\u4e00\u4e2a\u53ef\u4ee5\u63d0\u53d6\u7684\u5b57\u7b26\u4e32\u3002\u53e6\u4e00\u65b9\u9762\uff0c str() \u53ef\u4ee5\u8fd4\u56de\u6574\u4e2a\u6d41\u4e2d\u7684\u6570\u636e\uff0c\u5373\u4f7f\u6211\u4eec\u5df2\u7ecf\u4f7f\u7528 >> \u4ece\u4e2d\u8fdb\u884c\u4e86\u63d0\u53d6\u3002 \u8f6c\u6362\u5b57\u7b26\u4e32\u548c\u6570\u5b57 \u56e0\u4e3a\u63d2\u5165\u548c\u63d0\u53d6\u64cd\u4f5c\u7b26\u77e5\u9053\u5982\u4f55\u5904\u7406\u6240\u6709\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002 \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u5982\u4f55\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32: 1 2 3 4 5 6 7 8 9 10 std :: stringstream os ; int nValue { 12345 }; double dValue { 67.89 }; os << nValue << ' ' << dValue ; std :: string strValue1 , strValue2 ; os >> strValue1 >> strValue2 ; std :: cout << strValue1 << ' ' << strValue2 << '\\n' ; \u8f93\u51fa\u7ed3\u679c\uff1a 1 12345 67.89 \u73b0\u5728\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\uff1a 1 2 3 4 5 6 7 8 std :: stringstream os ; os << \"12345 67.89\" ; // insert a string of numbers into the stream int nValue ; double dValue ; os >> nValue >> dValue ; std :: cout << nValue << ' ' << dValue << '\\n' ; \u8f93\u51fa\u7ed3\u679c\uff1a 1 12345 67.89 \u6e05\u7406 stringstream \u4e3a\u518d\u6b21\u4f7f\u7528\u505a\u51c6\u5907 \u6e05\u7a7a stringstream \u7f13\u51b2\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff1a \u4f7f\u7528 str() \u8bbe\u7f6e\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u7a7a\u5b57\u7b26\u4e32\uff1a 1 2 3 4 5 6 7 std :: stringstream os ; os << \"Hello \" ; os . str ( \"\" ); // erase the buffer os << \"World!\" ; std :: cout << os . str (); \u4f7f\u7528 str() \u548c\u7a7a\u7684 std::string \u5bf9\u8c61\u5c06\u5176\u8bbe\u7f6e\u4e3a\u7a7a: 1 2 3 4 5 6 7 std :: stringstream os ; os << \"Hello \" ; os . str ( std :: string {}); // \u6e05\u7a7a os << \"World!\" ; std :: cout << os . str (); \u4e24\u4e2a\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u662f\u76f8\u540c\u7684\uff1a 1 World! \u6e05\u7406 stringstream \u65f6\uff0c\u5e94\u8be5\u540c\u65f6\u8c03\u7528 clear() \u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 std :: stringstream os ; os << \"Hello \" ; os . str ( \"\" ); // \u6e05\u7a7a buffer os . clear (); // \u91cd\u7f6e\u9519\u8bef\u6807\u8bb0 os << \"World!\" ; std :: cout << os . str (); clear() \u91cd\u7f6e\u53ef\u80fd\u5df2\u8bbe\u7f6e\u7684\u4efb\u4f55\u9519\u8bef\u6807\u5fd7\uff0c\u5e76\u5c06\u6d41\u8fd4\u56de\u5230ok\u72b6\u6001\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u66f4\u591a\u5730\u8ba8\u8bba\u6d41\u72b6\u6001\u548c\u9519\u8bef\u6807\u5fd7\u3002","title":"23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b"},{"location":"23-4-stream-classes-for-strings/#_1","text":"\u56e0\u4e3a\u63d2\u5165\u548c\u63d0\u53d6\u64cd\u4f5c\u7b26\u77e5\u9053\u5982\u4f55\u5904\u7406\u6240\u6709\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002 \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u5982\u4f55\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32: 1 2 3 4 5 6 7 8 9 10 std :: stringstream os ; int nValue { 12345 }; double dValue { 67.89 }; os << nValue << ' ' << dValue ; std :: string strValue1 , strValue2 ; os >> strValue1 >> strValue2 ; std :: cout << strValue1 << ' ' << strValue2 << '\\n' ; \u8f93\u51fa\u7ed3\u679c\uff1a 1 12345 67.89 \u73b0\u5728\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\uff1a 1 2 3 4 5 6 7 8 std :: stringstream os ; os << \"12345 67.89\" ; // insert a string of numbers into the stream int nValue ; double dValue ; os >> nValue >> dValue ; std :: cout << nValue << ' ' << dValue << '\\n' ; \u8f93\u51fa\u7ed3\u679c\uff1a 1 12345 67.89","title":"\u8f6c\u6362\u5b57\u7b26\u4e32\u548c\u6570\u5b57"},{"location":"23-4-stream-classes-for-strings/#stringstream","text":"\u6e05\u7a7a stringstream \u7f13\u51b2\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff1a \u4f7f\u7528 str() \u8bbe\u7f6e\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u7a7a\u5b57\u7b26\u4e32\uff1a 1 2 3 4 5 6 7 std :: stringstream os ; os << \"Hello \" ; os . str ( \"\" ); // erase the buffer os << \"World!\" ; std :: cout << os . str (); \u4f7f\u7528 str() \u548c\u7a7a\u7684 std::string \u5bf9\u8c61\u5c06\u5176\u8bbe\u7f6e\u4e3a\u7a7a: 1 2 3 4 5 6 7 std :: stringstream os ; os << \"Hello \" ; os . str ( std :: string {}); // \u6e05\u7a7a os << \"World!\" ; std :: cout << os . str (); \u4e24\u4e2a\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u662f\u76f8\u540c\u7684\uff1a 1 World! \u6e05\u7406 stringstream \u65f6\uff0c\u5e94\u8be5\u540c\u65f6\u8c03\u7528 clear() \u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 std :: stringstream os ; os << \"Hello \" ; os . str ( \"\" ); // \u6e05\u7a7a buffer os . clear (); // \u91cd\u7f6e\u9519\u8bef\u6807\u8bb0 os << \"World!\" ; std :: cout << os . str (); clear() \u91cd\u7f6e\u53ef\u80fd\u5df2\u8bbe\u7f6e\u7684\u4efb\u4f55\u9519\u8bef\u6807\u5fd7\uff0c\u5e76\u5c06\u6d41\u8fd4\u56de\u5230ok\u72b6\u6001\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u66f4\u591a\u5730\u8ba8\u8bba\u6d41\u72b6\u6001\u548c\u9519\u8bef\u6807\u5fd7\u3002","title":"\u6e05\u7406 stringstream \u4e3a\u518d\u6b21\u4f7f\u7528\u505a\u51c6\u5907"},{"location":"23-5-stream-states-and-input-validation/","tags":["io"],"text":"Key Takeaway \u6d41\u7684\u72b6\u6001 ios_base \u7c7b\u5305\u542b\u4e86\u591a\u4e2a\u4e0d\u540c\u7684\u72b6\u6001\u6807\u8bb0\uff0c\u5b83\u4eec\u53ef\u4ee5\u7528\u6765\u8868\u793a\u6d41\u7684\u4e0d\u540c\u72b6\u6001\uff1a \u6807\u8bb0 \u542b\u4e49 goodbit \u4e00\u5207\u6b63\u5e38 badbit \u53d1\u751f\u4e86\u67d0\u79cd\u4e25\u91cd\u9519\u8bef(\u4f8b\u5982\uff1a\u7a0b\u5e8f\u8bfb\u53d6\u8d85\u8fc7\u4e86\u6587\u4ef6\u672b\u5c3e) eofbit \u6d41\u5230\u8fbe\u6587\u4ef6\u672b\u5c3e failbit \u53d1\u751f\u975e\u81f4\u547d\u9519\u8bef(\u4f8b\u5982\uff0c\u7a0b\u5e8f\u671f\u671b\u6574\u578b\u65f6\u7528\u6237\u8f93\u5165\u4e86\u5b57\u7b26) \u5c3d\u7ba1\u8fd9\u4e9b\u6807\u8bb0\u5b9a\u4e49\u5728 ios_base \u4e2d\uff0c\u4f46\u662f\u56e0\u4e3a ios \u6d3e\u751f\u81ea ios_base \u800c\u4e14 ios \u5199\u8d77\u6765\u66f4\u7b80\u5355\uff0c\u6240\u4ee5\u6211\u4eec\u901a\u5e38\u4f1a\u901a\u8fc7 ios \u8bbf\u95ee\u8fd9\u4e9b\u6807\u8bb0(\u4f8b\u5982\uff1a as std::ios::failbit ). ios \u540c\u6837\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6210\u5458\u51fd\u6570\uff0c\u7528\u4e8e\u8bbf\u95ee\u8fd9\u4e9b\u72b6\u6001\uff1a \u6210\u5458\u51fd\u6570 \u542b\u4e49 good() \u5982\u679c\u8bbe\u7f6e\u4e86 goodbit \u5219\u8fd4\u56de true (\u6d41\u72b6\u6001\u6b63\u5e38) bad() \u5982\u679c\u8bbe\u7f6e\u4e86 badbit \u5219\u8fd4\u56de true (\u53d1\u751f\u4e25\u91cd\u9519\u8bef) eof() \u5982\u679c\u8bbe\u7f6e\u4e86 eofbit \u5219\u8fd4\u56de true (\u6d41\u5230\u8fbe\u6587\u4ef6\u672b\u5c3e) fail() \u5982\u679c\u8bbe\u7f6e\u4e86 failbit \u5219\u8fd4\u56de true (\u53d1\u751f\u975e\u81f4\u547d\u9519\u8bef) clear() \u6e05\u7a7a\u6240\u6709\u6807\u8bb0\u5e76\u5c06\u6d41\u72b6\u6001\u8bbe\u7f6e\u4e3a goodbit clear(state) \u6e05\u7a7a\u6240\u6709\u6807\u8bb0\u5e76\u5c06\u6d41\u72b6\u6001\u8bbe\u7f6e\u4e3a\u53c2\u6570 state \u8868\u793a\u7684\u72b6\u6001 rdstate() \u8fd4\u56de\u5f53\u524d\u8bbe\u7f6e\u7684\u6807\u8bb0 setstate(state) \u5c06\u6d41\u72b6\u6001\u8bbe\u7f6e\u4e3a\u53c2\u6570 state \u8868\u793a\u7684\u72b6\u6001 \u5b9e\u8df5\u4e2d\u6700\u5e38\u9047\u5230\u7684\u6807\u8bb0\u662f failbit \uff0c\u5f53\u7528\u6237\u8f93\u5165\u975e\u6cd5\u6570\u636e\u65f6\u5219\u8be5\u6807\u8bb0\u5c31\u4f1a\u88ab\u8bbe\u7f6e\u3002\u4f8b\u5982\uff1a 1 2 3 std :: cout << \"Enter your age: \" ; int age {}; std :: cin >> age ; \u7a0b\u5e8f\u671f\u671b\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6574\u578b\u3002\u4f46\u662f\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u662f\u4e00\u4e2a\u975e\u6570\u503c\u6570\u636e\uff0c\u4f8b\u5982 \u201cAlex\u201d\uff0c cin \u5c31\u65e0\u6cd5\u63d0\u53d6\u4efb\u4f55\u5e74\u9f84\u4fe1\u606f\uff0c\u6b64\u65f6 failbit \u4f1a\u88ab\u8bbe\u7f6e \u3002 \u5f53\u53d1\u751f\u9519\u8bef\u65f6\uff0c\u6d41\u4f1a\u88ab\u8bbe\u7f6e\u4e3a\u9664 goodbit \u4ee5\u5916\u7684\u72b6\u6001\uff0c\u540e\u7eed\u5bf9\u6d41\u7684\u64cd\u4f5c\u4e5f\u4f1a\u88ab\u5ffd\u7565\u3002\u6b64\u65f6\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528 clear() \u51fd\u6570\u6765\u91cd\u7f6e\u72b6\u6001\u3002 \u8f93\u5165\u9a8c\u8bc1 \u8f93\u5165\u9a8c\u8bc1 \u662f\u68c0\u67e5\u7528\u6237\u8f93\u5165\u662f\u5426\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u7684\u8fc7\u7a0b\u3002\u8f93\u5165\u9a8c\u8bc1\u901a\u5e38\u53ef\u4ee5\u5206\u4e3a\u4e24\u79cd\u7c7b\u578b\uff1a\u5b57\u7b26\u4e32\u548c\u6570\u5b57\u3002 \u5bf9\u4e8e\u5b57\u7b26\u4e32\u9a8c\u8bc1\uff0c\u6240\u6709\u7528\u6237\u8f93\u5165\u90fd\u4f1a\u88ab\u4f5c\u4e3a\u5b57\u7b26\u4e32\u63a5\u53d7\uff0c\u7136\u540e\u6839\u636e\u5176\u683c\u5f0f\u662f\u5426\u6ee1\u8db3\u8981\u6c42\u6765\u5224\u65ad\u5e94\u8be5\u63a5\u53d7\u6216\u62d2\u7edd\u8be5\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u8981\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u7535\u8bdd\u53f7\u7801\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u786e\u4fdd\u5176\u8f93\u5165\u7684\u6570\u636e\u670910\u4f4d\u6570\u5b57\u3002\u5728\u5927\u591a\u6570\u8bed\u8a00\u4e2d(\u7279\u522b\u662f\u50cfPerl\u548cPHP\u8fd9\u6837\u7684\u811a\u672c\u8bed\u8a00)\uff0c\u8fd9\u662f\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u5b8c\u6210\u7684\u3002C++\u6807\u51c6\u5e93\u4e5f\u6709\u4e00\u4e2a \u6b63\u5219\u8868\u8fbe\u5f0f\u5e93 \u3002\u56e0\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\u6bd4\u624b\u52a8\u5b57\u7b26\u4e32\u9a8c\u8bc1\u6162\uff0c\u6240\u4ee5\u53ea\u6709\u5728\u6027\u80fd(\u7f16\u8bd1\u65f6\u548c\u8fd0\u884c\u65f6)\u8981\u6c42\u4e0d\u9ad8\u6216\u8005\u624b\u52a8\u9a8c\u8bc1\u592a\u9ebb\u70e6\u7684\u60c5\u51b5\u4e0b\u624d\u5e94\u8be5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002 \u5bf9\u4e8e\u6570\u503c\u9a8c\u8bc1\uff0c\u6211\u4eec\u901a\u5e38\u5173\u5fc3\u7684\u662f\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\u662f\u5426\u5728\u7279\u5b9a\u7684\u8303\u56f4\u5185(\u4f8b\u5982\uff0c\u57280\u523020\u4e4b\u95f4)\u3002\u7136\u800c\uff0c\u4e0e\u5b57\u7b26\u4e32\u9a8c\u8bc1\u4e0d\u540c\u7684\u662f\uff0c\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u5b8c\u5168\u4e0d\u662f\u6570\u5b57\u7684\u4e1c\u897f\u2014\u2014\u6211\u4eec\u4e5f\u9700\u8981\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\u3002 \u4e3a\u6b64\uff0cC++\u63d0\u4f9b\u4e86\u8bb8\u591a\u6709\u7528\u7684\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u6765\u786e\u5b9a\u7279\u5b9a\u7684\u5b57\u7b26\u662f\u6570\u5b57\u8fd8\u662f\u5b57\u6bcd\u3002\u4ee5\u4e0b\u51fd\u6570\u4f4d\u4e8e cctype \u5934\u6587\u4ef6\u4e2d \u51fd\u6570 \u542b\u4e49 std::isalnum(int) \u5982\u679c\u53c2\u6570\u662f\u5b57\u6bcd\u6216\u6570\u5b57\u5219\u8fd4\u56de\u975e\u96f6\u503c std::isalpha(int) \u5982\u679c\u53c2\u6570\u662f\u5b57\u6bcd\u5219\u8fd4\u56de\u975e\u96f6\u503c std::iscntrl(int) \u5982\u679c\u53c2\u6570\u662f\u63a7\u5236\u5b57\u7b26\u5219\u8fd4\u56de\u975e\u96f6\u503c std::isdigit(int) \u5982\u679c\u53c2\u6570\u662f\u6570\u5b57\u5219\u8fd4\u56de\u975e\u96f6\u503c std::isgraph(int) \u5982\u679c\u53c2\u6570\u662f\u975e\u7a7a\u767d\u53ef\u6253\u5370\u5b57\u7b26\u5219\u8fd4\u56de\u975e\u96f6\u503c std::isprint(int) \u5982\u679c\u53c2\u6570\u662f\u53ef\u6253\u5370\u5b57\u7b26\uff08\u5305\u62ec\u975e\u7a7a\u767d\uff09\u5219\u8fd4\u56de\u975e\u96f6\u503c std::ispunct(int) \u5982\u679c\u53c2\u6570\u662f\u4e0d\u662f\u5b57\u6bcd\u3001\u6570\u5b57\u6216\u7a7a\u767d\u7b26\u8fd4\u56de\u975e\u96f6\u503c std::isspace(int) \u5982\u679c\u53c2\u6570\u662f\u7a7a\u767d\u7b26\u5219\u8fd4\u56de\u975e\u96f6\u503c std::isxdigit(int) \u5982\u679c\u53c2\u6570\u662f\u5341\u516d\u8fdb\u5236\u5219\u8fd4\u56de\u975e\u96f6\u503c \u5b57\u7b26\u4e32\u9a8c\u8bc1 \u8ba9\u6211\u4eec\u505a\u4e00\u4e2a\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u9a8c\u8bc1\u6848\u4f8b\uff0c\u8981\u6c42\u7528\u6237\u8f93\u5165\u4ed6\u4eec\u7684\u540d\u5b57\u3002\u9a8c\u8bc1\u6807\u51c6\u662f\u7528\u6237\u53ea\u80fd\u8f93\u5165\u5b57\u6bcd\u5b57\u7b26\u6216\u7a7a\u683c\u3002\u5982\u679c\u9047\u5230\u5176\u4ed6\u60c5\u51b5\uff0c\u5219\u5c06\u62d2\u7edd\u8f93\u5165\u3002 \u5bf9\u4e8e\u4e0d\u5b9a\u957f\u5ea6\u7684\u8f93\u5165\uff0c\u9a8c\u8bc1\u5b57\u7b26\u4e32\u7684\u6700\u4f73\u65b9\u6cd5(\u9664\u4e86\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5e93\u4e4b\u5916)\u662f\u9010\u4e2a\u904d\u5386\u5b57\u7b26\u4e32\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u786e\u4fdd\u5b83\u6ee1\u8db3\u9a8c\u8bc1\u6807\u51c6\u3002\u8fd9\u6b63\u662f\u6211\u4eec\u5728\u8fd9\u91cc\u8981\u505a\u7684\uff0c\u6216\u8005\u901a\u8fc7 std::all_of \u5b8c\u6210\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include // std::all_of #include // std::isalpha, std::isspace #include #include #include #include bool isValidName ( std :: string_view name ) { return std :: ranges :: all_of ( name , []( char ch ) { return ( std :: isalpha ( ch ) || std :: isspace ( ch )); }); // Before C++20, without ranges // return std::all_of(name.begin(), name.end(), [](char ch) { // return (std::isalpha(ch) || std::isspace(ch)); // }); } int main () { std :: string name {}; do { std :: cout << \"Enter your name: \" ; std :: getline ( std :: cin , name ); // \u8bfb\u53d6\u6574\u884c\u8f93\u5165\uff0c\u5305\u62ec\u7a7a\u683c } while ( ! isValidName ( name )); std :: cout << \"Hello \" << name << \"! \\n \" ; } \u6ce8\u610f\uff0c\u8fd9\u6bb5\u4ee3\u7801\u5e76\u4e0d\u5b8c\u7f8e\uff1a\u7528\u6237\u53ef\u4ee5\u8bf4\u4ed6\u4eec\u7684\u540d\u5b57\u662f\u201casf w jweo s di we ao\u201d\u6216\u5176\u4ed6\u4e00\u4e9b\u4e71\u4e03\u516b\u7cdf\u7684\u4e1c\u897f\uff0c\u751a\u81f3\u66f4\u7cdf\uff0c\u53ea\u662f\u4e00\u5806\u7a7a\u683c\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6539\u8fdb\u9a8c\u8bc1\u6807\u51c6\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f7f\u5176\u53ea\u63a5\u53d7\u81f3\u5c11\u5305\u542b\u4e00\u4e2a\u5b57\u7b26\u548c\u6700\u591a\u4e00\u4e2a\u7a7a\u683c\u7684\u5b57\u7b26\u4e32\u3002 \u4f5c\u8005\u6ce8 \u8bfb\u8005\u201cWaldo\u201d\u63d0\u4f9b\u4e86\u4e00\u4e2aC++20\u89e3\u51b3\u65b9\u6848(\u4f7f\u7528std::ranges)\u6765\u89e3\u51b3\u8fd9\u4e9b\u7f3a\u70b9\uff0c\u53c2\u8003 \u8fd9\u91cc \u518d\u770b\u53e6\u4e00\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u8981\u6c42\u7528\u6237\u8f93\u5165\u4ed6\u4eec\u7684\u7535\u8bdd\u53f7\u7801\u3002\u867d\u7136\u7528\u6237\u540d\u7684\u957f\u5ea6\u662f\u53ef\u53d8\u7684\uff0c\u4f46\u6bcf\u4e2a\u5b57\u7b26\u7684\u9a8c\u8bc1\u6807\u51c6\u90fd\u662f\u76f8\u540c\u7684\u3002\u800c\u7535\u8bdd\u53f7\u7801\u7684\u957f\u5ea6\u867d\u7136\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u9a8c\u8bc1\u6807\u51c6\u5219\u6839\u636e\u5b57\u7b26\u7684\u4f4d\u7f6e\u800c\u4e0d\u540c\u3002\u56e0\u6b64\u9700\u8981\u91c7\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u9a8c\u8bc1\u7535\u8bdd\u53f7\u7801\u8f93\u5165\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u6839\u636e\u9884\u5148\u786e\u5b9a\u7684\u6a21\u677f\u68c0\u67e5\u7528\u6237\u7684\u8f93\u5165\u662f\u5426\u5339\u914d\u3002\u6a21\u677f\u7684\u5de5\u4f5c\u65b9\u5f0f\u5982\u4e0b: # \u4f1a\u5339\u914d\u4efb\u4f55\u8f93\u5165\u7684\u6570\u5b57\uff1b @ \u4f1a\u5339\u914d\u4efb\u4f55\u8f93\u5165\u7684\u82f1\u6587\u5b57\u6bcd\uff1b _ \u4f1a\u5339\u914d\u4efb\u4f55\u8f93\u5165\u7684\u7a7a\u767d\uff1b ? \u5339\u914d\u6240\u6709\u3002 \u9664\u6b64\u4e4b\u5916\uff0c\u7528\u6237\u8f93\u5165\u7684\u5176\u4ed6\u5b57\u7b26\u5fc5\u987b\u7cbe\u786e\u5339\u914d\u3002 \u56e0\u6b64\uff0c\u5982\u679c\u6a21\u677f\u4e3a \u201c(###) ###-####\u201d\uff0c\u5219\u8868\u793a\u7528\u6237\u9700\u8981\u8f93\u5165 \u2018 ( \u2018\uff0c\u4e09\u4e2a\u6570\u5b57\uff0c \u2018 ) \u2019 \uff0c\u7a7a\u683c\uff0c\u4e09\u4e2a\u6570\u5b57\uff0c\u77ed\u6a2a\u7ebf\uff0c\u56db\u4e2a\u6570\u5b57\u3002\u5bf9\u4e8e\u4efb\u4f55\u4e0d\u6ee1\u8db3\u4e0a\u8ff0\u8981\u6c42\u7684\u8f93\u5165\uff0c\u90fd\u4f1a\u88ab\u7a0b\u5e8f\u62d2\u7edd\u3002 \u4ee3\u7801\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 #include // std::equal #include // std::isdigit, std::isspace, std::isalpha #include #include #include #include #include bool inputMatches ( std :: string_view input , std :: string_view pattern ) { if ( input . length () != pattern . length ()) { return false ; } // This table defines all special symbols that can match a range of user input // Each symbol is mapped to a function that determines whether the input is valid for that symbol static const std :: map < char , int ( * )( int ) > validators { { '#' , & std :: isdigit }, { '_' , & std :: isspace }, { '@' , & std :: isalpha }, { '?' , []( int ) { return 1 ; } } }; // Before C++20, use // return std::equal(input.begin(), input.end(), pattern.begin(), [](char ch, char mask) -> bool { // ... return std :: ranges :: equal ( input , pattern , []( char ch , char mask ) -> bool { if ( auto found { validators . find ( mask ) }; found != validators . end ()) { // \u627e\u5230\u53ef\u5339\u914d\u7684\u6a21\u5f0f\uff0c\u8c03\u7528\u5bf9\u5e94\u7684\u6821\u9a8c\u51fd\u6570 return ( * found -> second )( ch ); } else { // \u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u6a21\u5f0f\uff0c\u6b64\u65f6\u8981\u6c42\u5b57\u7b26\u7cbe\u786e\u5339\u914d return ( ch == mask ); } }); } int main () { std :: string phoneNumber {}; do { std :: cout << \"Enter a phone number (###) ###-####: \" ; std :: getline ( std :: cin , phoneNumber ); } while ( ! inputMatches ( phoneNumber , \"(###) ###-####\" )); std :: cout << \"You entered: \" << phoneNumber << '\\n' ; } \u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u5f3a\u5236\u7528\u6237\u6309\u7167\u6211\u4eec\u8981\u6c42\u7684\u683c\u5f0f\u8f93\u5165\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u4ecd\u7136\u6709\u5c40\u9650\u6027\uff1a\u5982\u679c # \u3001 @ \u3001 _ \u548c ? \u4e5f\u662f\u5408\u6cd5\u7684\u7528\u6237\u8f93\u5165\uff0c\u5219\u8be5\u51fd\u6570\u5c06\u65e0\u6cd5\u5de5\u4f5c\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u7b26\u53f7\u5df2\u7ecf\u88ab\u8d4b\u4e88\u4e86\u7279\u6b8a\u7684\u542b\u4e49\u3002\u6b64\u5916\uff0c\u4e0d\u540c\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u8be5\u6a21\u677f\u4e2d\u6ca1\u6709\u7b26\u53f7\u53ef\u4ee5\u8868\u793a\u201c\u8f93\u5165\u4e00\u7ec4\u4e0d\u5b9a\u957f\u7684\u5b57\u7b26\u201d\u3002\u56e0\u6b64\uff0c\u8be5\u6a21\u677f\u4e0d\u80fd\u88ab\u7528\u6765\u786e\u4fdd\u7528\u6237\u8f93\u5165\u4e24\u4e2a\u5355\u8bcd\uff08\u7a7a\u683c\u9694\u5f00\uff09\uff0c\u56e0\u4e3a\u5b83\u4e0d\u80fd\u5904\u7406\u53d8\u957f\u7684\u8f93\u5165\u3002\u5bf9\u4e8e\u8fd9\u79cd\u7c7b\u578b\u7684\u8f93\u5165\uff0c\u975e\u6a21\u677f\u7684\u65b9\u6cd5\u66f4\u52a0\u5408\u9002 \u3002 \u6570\u503c\u9a8c\u8bc1 \u5728\u5904\u7406\u6570\u503c\u578b\u8f93\u5165\u65f6\uff0c\u6700\u5e38\u89c1\u7684\u505a\u6cd5\u662f\u4f7f\u7528 \u63d0\u53d6\u8fd0\u7b97\u7b26 \u5c06\u8f93\u5165\u63d0\u53d6\u5230\u4e00\u4e2a\u6570\u503c\u7c7b\u578b\u7684\u53d8\u91cf\u4e2d \uff08\u53c2\u89c1\uff1a 23-4-stream-classes-for-strings#\u8f6c\u6362\u5b57\u7b26\u4e32\u548c\u6570\u5b57 \uff09\u3002\u901a\u8fc7\u68c0\u67e5\u6d41\u72b6\u6001\u6807\u8bb0 failbit \uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u77e5\u9053\u7528\u6237\u7684\u8f93\u5165\u662f\u5426\u662f\u4e00\u4e2a\u6570\u5b57\u3002 \u8bf7\u770b\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include int main () { int age {}; while ( true ) { std :: cout << \"Enter your age: \" ; std :: cin >> age ; if ( std :: cin . fail ()) // \u65e0\u6cd5\u63d0\u53d6 { std :: cin . clear (); // \u91cd\u7f6e\u72b6\u6001\u4e3a goodbit\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u8c03\u7528 ignore() std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // \u4ece\u6d41\u4e2d\u5ffd\u7565\u975e\u6cd5\u8f93\u5165 continue ; // \u7ee7\u7eed } if ( age <= 0 ) // \u786e\u4fdd\u8f93\u5165\u7684\u5e74\u9f84\u662f\u6b63\u6570 continue ; break ; } std :: cout << \"You entered: \" << age << '\\n' ; } \u5982\u679c\u7528\u6237\u8f93\u5165\u4e86\u4e00\u4e2a\u6574\u578b\u6570\uff0c\u5219\u63d0\u53d6\u53ef\u4ee5\u6210\u529f\u3002 std::cin.fail() \u7684\u7ed3\u679c\u4e3a false \uff0c\u4e0d\u8fdb\u5165if\u8bed\u53e5\u3002\u5047\u8bbe\u7528\u6237\u8f93\u5165\u7684\u662f\u6b63\u6570\u7684\u8bdd\uff0c\u5219\u63a5\u4e0b\u6765\u4f1a\u8d70\u5230 break \u8bed\u53e5\uff0c\u9000\u51fa\u5faa\u73af\u3002 \u5982\u679c\uff0c\u7528\u6237\u8f93\u5165\u7684\u4e0d\u662f\u6574\u6570\u800c\u662f\u5b57\u6bcd\uff0c\u5219\u63d0\u53d6\u4f1a\u5931\u8d25\u3002\u6b64\u65f6 std::cin.fail() \u8fd4\u56de true \uff0c\u6240\u4ee5\u4f1a\u8d70\u5230if\u8bed\u53e5\u4e2d\u3002\u5728\u8bed\u53e5\u5757\u7684\u6700\u540e\uff0c\u4f1a\u9047\u5230 continue \uff0c\u7ee7\u7eed\u6267\u884cwhile\u5faa\u73af\uff0c\u8981\u6c42\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002 \u4f46\u662f\uff0c\u8fd8\u6709\u4e00\u79cd\u60c5\u51b5\u6211\u4eec\u8fd8\u6ca1\u6709\u6d4b\u8bd5\u2014\u2014\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u5f00\u5934\u662f\u6570\u5b57\uff0c\u4f46\u662f\u540e\u9762\u662f\u5b57\u6bcd(\u4f8b\u5982 \u201c34abcd56\u201d)\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f00\u5934\u662f\u6570\u5b57(34)\uff0c\u5b83\u4f1a\u88ab\u63d0\u53d6\u6210\u5e74\u9f84\uff0c\u5269\u4e0b\u7684\u5b57\u7b26\u4e32\u5219\u4f1a\u6d41\u5728\u8f93\u5165\u6d41\u4e2d\uff0c failbit \u5e76\u4e0d\u4f1a\u88ab\u8bbe\u7f6e\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u4e24\u4e2a\u95ee\u9898\uff1a \u5982\u679c\u4f60\u5c06\u5b83\u4f5c\u4e3a\u5408\u6cd5\u8f93\u5165\uff0c\u5219\u8f93\u5165\u6d41\u4e2d\u6b8b\u7559\u4e86\u5783\u573e\u4fe1\u606f\uff1b \u5982\u679c\u4f60\u4e0d\u8ba4\u4e3a\u8fd9\u662f\u5408\u6cd5\u8f93\u5165\uff0c\u4f46\u662f\u5b83\u5e76\u4e0d\u4f1a\u88ab\u62d2\u7edd\uff08\u800c\u4e14\u4f60\u7684\u6d41\u4e2d\u8fd8\u6709\u5783\u573e\u4fe1\u606f\uff09\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u65b9\u6cd5\u4e5f\u5f88\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include #include int main () { int age {}; while ( true ) { std :: cout << \"Enter your age: \" ; std :: cin >> age ; if ( std :: cin . fail ()) // no extraction took place { std :: cin . clear (); // reset the state bits back to goodbit so we can use ignore() std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // \u4ece\u6d41\u4e2d\u6e05\u9664\u9519\u8bef\u7684\u8f93\u5165 continue ; // try again } std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // \u4ece\u8f93\u5165\u6d41\u4e2d\u5254\u9664\u591a\u4f59\u7684\u8f93\u5165 if ( age <= 0 ) // make sure age is positive continue ; break ; } std :: cout << \"You entered: \" << age << '\\n' ; } \u5982\u679c\u4f60\u4e0d\u5e0c\u671b\u8fd9\u6837\u7684\u8f93\u5165\u662f\u6709\u6548\u7684\uff0c\u90a3\u8fd8\u9700\u8981\u505a\u4e00\u4e9b\u989d\u5916\u7684\u5de5\u4f5c\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u524d\u9762\u7684\u89e3\u51b3\u65b9\u6848\u5df2\u7ecf\u5b8c\u6210\u4e86\u4e00\u534a\u7684\u5de5\u4f5c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 gcount() \u51fd\u6570\u6765\u786e\u5b9a\u6709\u591a\u5c11\u5b57\u7b26\u88ab\u5ffd\u7565\u4e86\u3002\u5982\u679c\u8f93\u5165\u6709\u6548\uff0c\u5219 gcount() \u5e94\u8be5\u8fd4\u56de1(\u88ab\u4e22\u5f03\u7684\u6362\u884c\u7b26)\u3002\u5982\u679c\u8fd4\u56de\u503c\u5927\u4e8e1\uff0c\u8bf4\u660e\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u6ca1\u6709\u88ab\u6b63\u786e\u63d0\u53d6\uff0c\u6b64\u65f6\u5e94\u8be5\u8981\u6c42\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002\u8fd9\u91cc\u6709\u4e00\u4e2a\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include #include int main () { int age {}; while ( true ) { std :: cout << \"Enter your age: \" ; std :: cin >> age ; if ( std :: cin . fail ()) // no extraction took place { std :: cin . clear (); // reset the state bits back to goodbit so we can use ignore() std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // clear out the bad input from the stream continue ; // try again } std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // clear out any additional input from the stream if ( std :: cin . gcount () > 1 ) // if we cleared out more than one additional character { continue ; // we'll consider this input to be invalid } if ( age <= 0 ) // make sure age is positive { continue ; } break ; } std :: cout << \"You entered: \" << age << '\\n' ; } \u6570\u503c\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fdb\u884c\u9a8c\u8bc1 \u4e3a\u4e86\u5f97\u5230\u4e00\u4e2a\u503c\uff0c\u7adf\u7136\u8981\u50cf\u4e0a\u9762\u90a3\u6837\u8fd9\u4e48\u591a\u7684\u64cd\u4f5c\uff01\u5904\u7406\u6570\u5b57\u8f93\u5165\u7684\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u5c06\u5176\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8bfb\u5165\uff0c\u7136\u540e\u5c1d\u8bd5\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u5b57\u7c7b\u578b\u3002\u4e0b\u9762\u7684\u7a0b\u5e8f\u4f7f\u7528\u4e86\u8fd9\u79cd\u65b9\u6cd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include // std::from_chars #include #include #include #include std :: optional < int > extractAge ( std :: string_view age ) { int result {}; auto end { age . data () + age . length () }; // \u5c1d\u8bd5\u4eceage\u4e2d\u63d0\u53d6\u4e00\u4e2a\u6574\u6570 if ( std :: from_chars ( age . data (), end , result ). ptr != end ) { return {}; } if ( result <= 0 ) // \u786e\u4fdd\u5e74\u9f84\u662f\u6b63\u6570 { return {}; } return result ; } int main () { int age {}; while ( true ) { std :: cout << \"Enter your age: \" ; std :: string strAge {}; std :: getline ( std :: cin >> std :: ws , strAge ); if ( auto extracted { extractAge ( strAge ) }) { age = * extracted ; break ; } } std :: cout << \"You entered: \" << age << '\\n' ; } COPY \u4e0e\u76f4\u63a5\u63d0\u53d6\u6570\u5b57\u76f8\u6bd4\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7684\u5de5\u4f5c\u91cf\u662f\u591a\u8fd8\u662f\u5c11\u53d6\u51b3\u4e8e\u9a8c\u8bc1\u53c2\u6570\u548c\u9650\u5236\u3002 \u5982\u4f60\u6240\u89c1\uff0c\u5728C++\u4e2d\u8fdb\u884c\u8f93\u5165\u9a8c\u8bc1\u9700\u8981\u5927\u91cf\u7684\u5de5\u4f5c\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u8bb8\u591a\u8fd9\u6837\u7684\u4efb\u52a1(\u4f8b\u5982\uff0c\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fdb\u884c\u6570\u503c\u9a8c\u8bc1)\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u8f6c\u6362\u4e3a\u53ef\u4ee5\u5728\u5404\u79cd\u60c5\u51b5\u4e0b\u91cd\u7528\u7684\u51fd\u6570\u3002","title":"23.5 - \u6d41\u72b6\u6001\u548c\u8f93\u5165\u9a8c\u8bc1"},{"location":"23-5-stream-states-and-input-validation/#_1","text":"ios_base \u7c7b\u5305\u542b\u4e86\u591a\u4e2a\u4e0d\u540c\u7684\u72b6\u6001\u6807\u8bb0\uff0c\u5b83\u4eec\u53ef\u4ee5\u7528\u6765\u8868\u793a\u6d41\u7684\u4e0d\u540c\u72b6\u6001\uff1a \u6807\u8bb0 \u542b\u4e49 goodbit \u4e00\u5207\u6b63\u5e38 badbit \u53d1\u751f\u4e86\u67d0\u79cd\u4e25\u91cd\u9519\u8bef(\u4f8b\u5982\uff1a\u7a0b\u5e8f\u8bfb\u53d6\u8d85\u8fc7\u4e86\u6587\u4ef6\u672b\u5c3e) eofbit \u6d41\u5230\u8fbe\u6587\u4ef6\u672b\u5c3e failbit \u53d1\u751f\u975e\u81f4\u547d\u9519\u8bef(\u4f8b\u5982\uff0c\u7a0b\u5e8f\u671f\u671b\u6574\u578b\u65f6\u7528\u6237\u8f93\u5165\u4e86\u5b57\u7b26) \u5c3d\u7ba1\u8fd9\u4e9b\u6807\u8bb0\u5b9a\u4e49\u5728 ios_base \u4e2d\uff0c\u4f46\u662f\u56e0\u4e3a ios \u6d3e\u751f\u81ea ios_base \u800c\u4e14 ios \u5199\u8d77\u6765\u66f4\u7b80\u5355\uff0c\u6240\u4ee5\u6211\u4eec\u901a\u5e38\u4f1a\u901a\u8fc7 ios \u8bbf\u95ee\u8fd9\u4e9b\u6807\u8bb0(\u4f8b\u5982\uff1a as std::ios::failbit ). ios \u540c\u6837\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6210\u5458\u51fd\u6570\uff0c\u7528\u4e8e\u8bbf\u95ee\u8fd9\u4e9b\u72b6\u6001\uff1a \u6210\u5458\u51fd\u6570 \u542b\u4e49 good() \u5982\u679c\u8bbe\u7f6e\u4e86 goodbit \u5219\u8fd4\u56de true (\u6d41\u72b6\u6001\u6b63\u5e38) bad() \u5982\u679c\u8bbe\u7f6e\u4e86 badbit \u5219\u8fd4\u56de true (\u53d1\u751f\u4e25\u91cd\u9519\u8bef) eof() \u5982\u679c\u8bbe\u7f6e\u4e86 eofbit \u5219\u8fd4\u56de true (\u6d41\u5230\u8fbe\u6587\u4ef6\u672b\u5c3e) fail() \u5982\u679c\u8bbe\u7f6e\u4e86 failbit \u5219\u8fd4\u56de true (\u53d1\u751f\u975e\u81f4\u547d\u9519\u8bef) clear() \u6e05\u7a7a\u6240\u6709\u6807\u8bb0\u5e76\u5c06\u6d41\u72b6\u6001\u8bbe\u7f6e\u4e3a goodbit clear(state) \u6e05\u7a7a\u6240\u6709\u6807\u8bb0\u5e76\u5c06\u6d41\u72b6\u6001\u8bbe\u7f6e\u4e3a\u53c2\u6570 state \u8868\u793a\u7684\u72b6\u6001 rdstate() \u8fd4\u56de\u5f53\u524d\u8bbe\u7f6e\u7684\u6807\u8bb0 setstate(state) \u5c06\u6d41\u72b6\u6001\u8bbe\u7f6e\u4e3a\u53c2\u6570 state \u8868\u793a\u7684\u72b6\u6001 \u5b9e\u8df5\u4e2d\u6700\u5e38\u9047\u5230\u7684\u6807\u8bb0\u662f failbit \uff0c\u5f53\u7528\u6237\u8f93\u5165\u975e\u6cd5\u6570\u636e\u65f6\u5219\u8be5\u6807\u8bb0\u5c31\u4f1a\u88ab\u8bbe\u7f6e\u3002\u4f8b\u5982\uff1a 1 2 3 std :: cout << \"Enter your age: \" ; int age {}; std :: cin >> age ; \u7a0b\u5e8f\u671f\u671b\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6574\u578b\u3002\u4f46\u662f\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u662f\u4e00\u4e2a\u975e\u6570\u503c\u6570\u636e\uff0c\u4f8b\u5982 \u201cAlex\u201d\uff0c cin \u5c31\u65e0\u6cd5\u63d0\u53d6\u4efb\u4f55\u5e74\u9f84\u4fe1\u606f\uff0c\u6b64\u65f6 failbit \u4f1a\u88ab\u8bbe\u7f6e \u3002 \u5f53\u53d1\u751f\u9519\u8bef\u65f6\uff0c\u6d41\u4f1a\u88ab\u8bbe\u7f6e\u4e3a\u9664 goodbit \u4ee5\u5916\u7684\u72b6\u6001\uff0c\u540e\u7eed\u5bf9\u6d41\u7684\u64cd\u4f5c\u4e5f\u4f1a\u88ab\u5ffd\u7565\u3002\u6b64\u65f6\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528 clear() \u51fd\u6570\u6765\u91cd\u7f6e\u72b6\u6001\u3002","title":"\u6d41\u7684\u72b6\u6001"},{"location":"23-5-stream-states-and-input-validation/#_2","text":"\u8f93\u5165\u9a8c\u8bc1 \u662f\u68c0\u67e5\u7528\u6237\u8f93\u5165\u662f\u5426\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u7684\u8fc7\u7a0b\u3002\u8f93\u5165\u9a8c\u8bc1\u901a\u5e38\u53ef\u4ee5\u5206\u4e3a\u4e24\u79cd\u7c7b\u578b\uff1a\u5b57\u7b26\u4e32\u548c\u6570\u5b57\u3002 \u5bf9\u4e8e\u5b57\u7b26\u4e32\u9a8c\u8bc1\uff0c\u6240\u6709\u7528\u6237\u8f93\u5165\u90fd\u4f1a\u88ab\u4f5c\u4e3a\u5b57\u7b26\u4e32\u63a5\u53d7\uff0c\u7136\u540e\u6839\u636e\u5176\u683c\u5f0f\u662f\u5426\u6ee1\u8db3\u8981\u6c42\u6765\u5224\u65ad\u5e94\u8be5\u63a5\u53d7\u6216\u62d2\u7edd\u8be5\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u8981\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u7535\u8bdd\u53f7\u7801\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u786e\u4fdd\u5176\u8f93\u5165\u7684\u6570\u636e\u670910\u4f4d\u6570\u5b57\u3002\u5728\u5927\u591a\u6570\u8bed\u8a00\u4e2d(\u7279\u522b\u662f\u50cfPerl\u548cPHP\u8fd9\u6837\u7684\u811a\u672c\u8bed\u8a00)\uff0c\u8fd9\u662f\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u5b8c\u6210\u7684\u3002C++\u6807\u51c6\u5e93\u4e5f\u6709\u4e00\u4e2a \u6b63\u5219\u8868\u8fbe\u5f0f\u5e93 \u3002\u56e0\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\u6bd4\u624b\u52a8\u5b57\u7b26\u4e32\u9a8c\u8bc1\u6162\uff0c\u6240\u4ee5\u53ea\u6709\u5728\u6027\u80fd(\u7f16\u8bd1\u65f6\u548c\u8fd0\u884c\u65f6)\u8981\u6c42\u4e0d\u9ad8\u6216\u8005\u624b\u52a8\u9a8c\u8bc1\u592a\u9ebb\u70e6\u7684\u60c5\u51b5\u4e0b\u624d\u5e94\u8be5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002 \u5bf9\u4e8e\u6570\u503c\u9a8c\u8bc1\uff0c\u6211\u4eec\u901a\u5e38\u5173\u5fc3\u7684\u662f\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\u662f\u5426\u5728\u7279\u5b9a\u7684\u8303\u56f4\u5185(\u4f8b\u5982\uff0c\u57280\u523020\u4e4b\u95f4)\u3002\u7136\u800c\uff0c\u4e0e\u5b57\u7b26\u4e32\u9a8c\u8bc1\u4e0d\u540c\u7684\u662f\uff0c\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u5b8c\u5168\u4e0d\u662f\u6570\u5b57\u7684\u4e1c\u897f\u2014\u2014\u6211\u4eec\u4e5f\u9700\u8981\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\u3002 \u4e3a\u6b64\uff0cC++\u63d0\u4f9b\u4e86\u8bb8\u591a\u6709\u7528\u7684\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u6765\u786e\u5b9a\u7279\u5b9a\u7684\u5b57\u7b26\u662f\u6570\u5b57\u8fd8\u662f\u5b57\u6bcd\u3002\u4ee5\u4e0b\u51fd\u6570\u4f4d\u4e8e cctype \u5934\u6587\u4ef6\u4e2d \u51fd\u6570 \u542b\u4e49 std::isalnum(int) \u5982\u679c\u53c2\u6570\u662f\u5b57\u6bcd\u6216\u6570\u5b57\u5219\u8fd4\u56de\u975e\u96f6\u503c std::isalpha(int) \u5982\u679c\u53c2\u6570\u662f\u5b57\u6bcd\u5219\u8fd4\u56de\u975e\u96f6\u503c std::iscntrl(int) \u5982\u679c\u53c2\u6570\u662f\u63a7\u5236\u5b57\u7b26\u5219\u8fd4\u56de\u975e\u96f6\u503c std::isdigit(int) \u5982\u679c\u53c2\u6570\u662f\u6570\u5b57\u5219\u8fd4\u56de\u975e\u96f6\u503c std::isgraph(int) \u5982\u679c\u53c2\u6570\u662f\u975e\u7a7a\u767d\u53ef\u6253\u5370\u5b57\u7b26\u5219\u8fd4\u56de\u975e\u96f6\u503c std::isprint(int) \u5982\u679c\u53c2\u6570\u662f\u53ef\u6253\u5370\u5b57\u7b26\uff08\u5305\u62ec\u975e\u7a7a\u767d\uff09\u5219\u8fd4\u56de\u975e\u96f6\u503c std::ispunct(int) \u5982\u679c\u53c2\u6570\u662f\u4e0d\u662f\u5b57\u6bcd\u3001\u6570\u5b57\u6216\u7a7a\u767d\u7b26\u8fd4\u56de\u975e\u96f6\u503c std::isspace(int) \u5982\u679c\u53c2\u6570\u662f\u7a7a\u767d\u7b26\u5219\u8fd4\u56de\u975e\u96f6\u503c std::isxdigit(int) \u5982\u679c\u53c2\u6570\u662f\u5341\u516d\u8fdb\u5236\u5219\u8fd4\u56de\u975e\u96f6\u503c","title":"\u8f93\u5165\u9a8c\u8bc1"},{"location":"23-5-stream-states-and-input-validation/#_3","text":"\u8ba9\u6211\u4eec\u505a\u4e00\u4e2a\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u9a8c\u8bc1\u6848\u4f8b\uff0c\u8981\u6c42\u7528\u6237\u8f93\u5165\u4ed6\u4eec\u7684\u540d\u5b57\u3002\u9a8c\u8bc1\u6807\u51c6\u662f\u7528\u6237\u53ea\u80fd\u8f93\u5165\u5b57\u6bcd\u5b57\u7b26\u6216\u7a7a\u683c\u3002\u5982\u679c\u9047\u5230\u5176\u4ed6\u60c5\u51b5\uff0c\u5219\u5c06\u62d2\u7edd\u8f93\u5165\u3002 \u5bf9\u4e8e\u4e0d\u5b9a\u957f\u5ea6\u7684\u8f93\u5165\uff0c\u9a8c\u8bc1\u5b57\u7b26\u4e32\u7684\u6700\u4f73\u65b9\u6cd5(\u9664\u4e86\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5e93\u4e4b\u5916)\u662f\u9010\u4e2a\u904d\u5386\u5b57\u7b26\u4e32\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u786e\u4fdd\u5b83\u6ee1\u8db3\u9a8c\u8bc1\u6807\u51c6\u3002\u8fd9\u6b63\u662f\u6211\u4eec\u5728\u8fd9\u91cc\u8981\u505a\u7684\uff0c\u6216\u8005\u901a\u8fc7 std::all_of \u5b8c\u6210\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include // std::all_of #include // std::isalpha, std::isspace #include #include #include #include bool isValidName ( std :: string_view name ) { return std :: ranges :: all_of ( name , []( char ch ) { return ( std :: isalpha ( ch ) || std :: isspace ( ch )); }); // Before C++20, without ranges // return std::all_of(name.begin(), name.end(), [](char ch) { // return (std::isalpha(ch) || std::isspace(ch)); // }); } int main () { std :: string name {}; do { std :: cout << \"Enter your name: \" ; std :: getline ( std :: cin , name ); // \u8bfb\u53d6\u6574\u884c\u8f93\u5165\uff0c\u5305\u62ec\u7a7a\u683c } while ( ! isValidName ( name )); std :: cout << \"Hello \" << name << \"! \\n \" ; } \u6ce8\u610f\uff0c\u8fd9\u6bb5\u4ee3\u7801\u5e76\u4e0d\u5b8c\u7f8e\uff1a\u7528\u6237\u53ef\u4ee5\u8bf4\u4ed6\u4eec\u7684\u540d\u5b57\u662f\u201casf w jweo s di we ao\u201d\u6216\u5176\u4ed6\u4e00\u4e9b\u4e71\u4e03\u516b\u7cdf\u7684\u4e1c\u897f\uff0c\u751a\u81f3\u66f4\u7cdf\uff0c\u53ea\u662f\u4e00\u5806\u7a7a\u683c\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6539\u8fdb\u9a8c\u8bc1\u6807\u51c6\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f7f\u5176\u53ea\u63a5\u53d7\u81f3\u5c11\u5305\u542b\u4e00\u4e2a\u5b57\u7b26\u548c\u6700\u591a\u4e00\u4e2a\u7a7a\u683c\u7684\u5b57\u7b26\u4e32\u3002 \u4f5c\u8005\u6ce8 \u8bfb\u8005\u201cWaldo\u201d\u63d0\u4f9b\u4e86\u4e00\u4e2aC++20\u89e3\u51b3\u65b9\u6848(\u4f7f\u7528std::ranges)\u6765\u89e3\u51b3\u8fd9\u4e9b\u7f3a\u70b9\uff0c\u53c2\u8003 \u8fd9\u91cc \u518d\u770b\u53e6\u4e00\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u8981\u6c42\u7528\u6237\u8f93\u5165\u4ed6\u4eec\u7684\u7535\u8bdd\u53f7\u7801\u3002\u867d\u7136\u7528\u6237\u540d\u7684\u957f\u5ea6\u662f\u53ef\u53d8\u7684\uff0c\u4f46\u6bcf\u4e2a\u5b57\u7b26\u7684\u9a8c\u8bc1\u6807\u51c6\u90fd\u662f\u76f8\u540c\u7684\u3002\u800c\u7535\u8bdd\u53f7\u7801\u7684\u957f\u5ea6\u867d\u7136\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u9a8c\u8bc1\u6807\u51c6\u5219\u6839\u636e\u5b57\u7b26\u7684\u4f4d\u7f6e\u800c\u4e0d\u540c\u3002\u56e0\u6b64\u9700\u8981\u91c7\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u9a8c\u8bc1\u7535\u8bdd\u53f7\u7801\u8f93\u5165\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u6839\u636e\u9884\u5148\u786e\u5b9a\u7684\u6a21\u677f\u68c0\u67e5\u7528\u6237\u7684\u8f93\u5165\u662f\u5426\u5339\u914d\u3002\u6a21\u677f\u7684\u5de5\u4f5c\u65b9\u5f0f\u5982\u4e0b: # \u4f1a\u5339\u914d\u4efb\u4f55\u8f93\u5165\u7684\u6570\u5b57\uff1b @ \u4f1a\u5339\u914d\u4efb\u4f55\u8f93\u5165\u7684\u82f1\u6587\u5b57\u6bcd\uff1b _ \u4f1a\u5339\u914d\u4efb\u4f55\u8f93\u5165\u7684\u7a7a\u767d\uff1b ? \u5339\u914d\u6240\u6709\u3002 \u9664\u6b64\u4e4b\u5916\uff0c\u7528\u6237\u8f93\u5165\u7684\u5176\u4ed6\u5b57\u7b26\u5fc5\u987b\u7cbe\u786e\u5339\u914d\u3002 \u56e0\u6b64\uff0c\u5982\u679c\u6a21\u677f\u4e3a \u201c(###) ###-####\u201d\uff0c\u5219\u8868\u793a\u7528\u6237\u9700\u8981\u8f93\u5165 \u2018 ( \u2018\uff0c\u4e09\u4e2a\u6570\u5b57\uff0c \u2018 ) \u2019 \uff0c\u7a7a\u683c\uff0c\u4e09\u4e2a\u6570\u5b57\uff0c\u77ed\u6a2a\u7ebf\uff0c\u56db\u4e2a\u6570\u5b57\u3002\u5bf9\u4e8e\u4efb\u4f55\u4e0d\u6ee1\u8db3\u4e0a\u8ff0\u8981\u6c42\u7684\u8f93\u5165\uff0c\u90fd\u4f1a\u88ab\u7a0b\u5e8f\u62d2\u7edd\u3002 \u4ee3\u7801\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 #include // std::equal #include // std::isdigit, std::isspace, std::isalpha #include #include #include #include #include bool inputMatches ( std :: string_view input , std :: string_view pattern ) { if ( input . length () != pattern . length ()) { return false ; } // This table defines all special symbols that can match a range of user input // Each symbol is mapped to a function that determines whether the input is valid for that symbol static const std :: map < char , int ( * )( int ) > validators { { '#' , & std :: isdigit }, { '_' , & std :: isspace }, { '@' , & std :: isalpha }, { '?' , []( int ) { return 1 ; } } }; // Before C++20, use // return std::equal(input.begin(), input.end(), pattern.begin(), [](char ch, char mask) -> bool { // ... return std :: ranges :: equal ( input , pattern , []( char ch , char mask ) -> bool { if ( auto found { validators . find ( mask ) }; found != validators . end ()) { // \u627e\u5230\u53ef\u5339\u914d\u7684\u6a21\u5f0f\uff0c\u8c03\u7528\u5bf9\u5e94\u7684\u6821\u9a8c\u51fd\u6570 return ( * found -> second )( ch ); } else { // \u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u6a21\u5f0f\uff0c\u6b64\u65f6\u8981\u6c42\u5b57\u7b26\u7cbe\u786e\u5339\u914d return ( ch == mask ); } }); } int main () { std :: string phoneNumber {}; do { std :: cout << \"Enter a phone number (###) ###-####: \" ; std :: getline ( std :: cin , phoneNumber ); } while ( ! inputMatches ( phoneNumber , \"(###) ###-####\" )); std :: cout << \"You entered: \" << phoneNumber << '\\n' ; } \u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u5f3a\u5236\u7528\u6237\u6309\u7167\u6211\u4eec\u8981\u6c42\u7684\u683c\u5f0f\u8f93\u5165\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u4ecd\u7136\u6709\u5c40\u9650\u6027\uff1a\u5982\u679c # \u3001 @ \u3001 _ \u548c ? \u4e5f\u662f\u5408\u6cd5\u7684\u7528\u6237\u8f93\u5165\uff0c\u5219\u8be5\u51fd\u6570\u5c06\u65e0\u6cd5\u5de5\u4f5c\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u7b26\u53f7\u5df2\u7ecf\u88ab\u8d4b\u4e88\u4e86\u7279\u6b8a\u7684\u542b\u4e49\u3002\u6b64\u5916\uff0c\u4e0d\u540c\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u8be5\u6a21\u677f\u4e2d\u6ca1\u6709\u7b26\u53f7\u53ef\u4ee5\u8868\u793a\u201c\u8f93\u5165\u4e00\u7ec4\u4e0d\u5b9a\u957f\u7684\u5b57\u7b26\u201d\u3002\u56e0\u6b64\uff0c\u8be5\u6a21\u677f\u4e0d\u80fd\u88ab\u7528\u6765\u786e\u4fdd\u7528\u6237\u8f93\u5165\u4e24\u4e2a\u5355\u8bcd\uff08\u7a7a\u683c\u9694\u5f00\uff09\uff0c\u56e0\u4e3a\u5b83\u4e0d\u80fd\u5904\u7406\u53d8\u957f\u7684\u8f93\u5165\u3002\u5bf9\u4e8e\u8fd9\u79cd\u7c7b\u578b\u7684\u8f93\u5165\uff0c\u975e\u6a21\u677f\u7684\u65b9\u6cd5\u66f4\u52a0\u5408\u9002 \u3002","title":"\u5b57\u7b26\u4e32\u9a8c\u8bc1"},{"location":"23-5-stream-states-and-input-validation/#_4","text":"\u5728\u5904\u7406\u6570\u503c\u578b\u8f93\u5165\u65f6\uff0c\u6700\u5e38\u89c1\u7684\u505a\u6cd5\u662f\u4f7f\u7528 \u63d0\u53d6\u8fd0\u7b97\u7b26 \u5c06\u8f93\u5165\u63d0\u53d6\u5230\u4e00\u4e2a\u6570\u503c\u7c7b\u578b\u7684\u53d8\u91cf\u4e2d \uff08\u53c2\u89c1\uff1a 23-4-stream-classes-for-strings#\u8f6c\u6362\u5b57\u7b26\u4e32\u548c\u6570\u5b57 \uff09\u3002\u901a\u8fc7\u68c0\u67e5\u6d41\u72b6\u6001\u6807\u8bb0 failbit \uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u77e5\u9053\u7528\u6237\u7684\u8f93\u5165\u662f\u5426\u662f\u4e00\u4e2a\u6570\u5b57\u3002 \u8bf7\u770b\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include int main () { int age {}; while ( true ) { std :: cout << \"Enter your age: \" ; std :: cin >> age ; if ( std :: cin . fail ()) // \u65e0\u6cd5\u63d0\u53d6 { std :: cin . clear (); // \u91cd\u7f6e\u72b6\u6001\u4e3a goodbit\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u8c03\u7528 ignore() std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // \u4ece\u6d41\u4e2d\u5ffd\u7565\u975e\u6cd5\u8f93\u5165 continue ; // \u7ee7\u7eed } if ( age <= 0 ) // \u786e\u4fdd\u8f93\u5165\u7684\u5e74\u9f84\u662f\u6b63\u6570 continue ; break ; } std :: cout << \"You entered: \" << age << '\\n' ; } \u5982\u679c\u7528\u6237\u8f93\u5165\u4e86\u4e00\u4e2a\u6574\u578b\u6570\uff0c\u5219\u63d0\u53d6\u53ef\u4ee5\u6210\u529f\u3002 std::cin.fail() \u7684\u7ed3\u679c\u4e3a false \uff0c\u4e0d\u8fdb\u5165if\u8bed\u53e5\u3002\u5047\u8bbe\u7528\u6237\u8f93\u5165\u7684\u662f\u6b63\u6570\u7684\u8bdd\uff0c\u5219\u63a5\u4e0b\u6765\u4f1a\u8d70\u5230 break \u8bed\u53e5\uff0c\u9000\u51fa\u5faa\u73af\u3002 \u5982\u679c\uff0c\u7528\u6237\u8f93\u5165\u7684\u4e0d\u662f\u6574\u6570\u800c\u662f\u5b57\u6bcd\uff0c\u5219\u63d0\u53d6\u4f1a\u5931\u8d25\u3002\u6b64\u65f6 std::cin.fail() \u8fd4\u56de true \uff0c\u6240\u4ee5\u4f1a\u8d70\u5230if\u8bed\u53e5\u4e2d\u3002\u5728\u8bed\u53e5\u5757\u7684\u6700\u540e\uff0c\u4f1a\u9047\u5230 continue \uff0c\u7ee7\u7eed\u6267\u884cwhile\u5faa\u73af\uff0c\u8981\u6c42\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002 \u4f46\u662f\uff0c\u8fd8\u6709\u4e00\u79cd\u60c5\u51b5\u6211\u4eec\u8fd8\u6ca1\u6709\u6d4b\u8bd5\u2014\u2014\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u5f00\u5934\u662f\u6570\u5b57\uff0c\u4f46\u662f\u540e\u9762\u662f\u5b57\u6bcd(\u4f8b\u5982 \u201c34abcd56\u201d)\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f00\u5934\u662f\u6570\u5b57(34)\uff0c\u5b83\u4f1a\u88ab\u63d0\u53d6\u6210\u5e74\u9f84\uff0c\u5269\u4e0b\u7684\u5b57\u7b26\u4e32\u5219\u4f1a\u6d41\u5728\u8f93\u5165\u6d41\u4e2d\uff0c failbit \u5e76\u4e0d\u4f1a\u88ab\u8bbe\u7f6e\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u4e24\u4e2a\u95ee\u9898\uff1a \u5982\u679c\u4f60\u5c06\u5b83\u4f5c\u4e3a\u5408\u6cd5\u8f93\u5165\uff0c\u5219\u8f93\u5165\u6d41\u4e2d\u6b8b\u7559\u4e86\u5783\u573e\u4fe1\u606f\uff1b \u5982\u679c\u4f60\u4e0d\u8ba4\u4e3a\u8fd9\u662f\u5408\u6cd5\u8f93\u5165\uff0c\u4f46\u662f\u5b83\u5e76\u4e0d\u4f1a\u88ab\u62d2\u7edd\uff08\u800c\u4e14\u4f60\u7684\u6d41\u4e2d\u8fd8\u6709\u5783\u573e\u4fe1\u606f\uff09\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u65b9\u6cd5\u4e5f\u5f88\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include #include int main () { int age {}; while ( true ) { std :: cout << \"Enter your age: \" ; std :: cin >> age ; if ( std :: cin . fail ()) // no extraction took place { std :: cin . clear (); // reset the state bits back to goodbit so we can use ignore() std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // \u4ece\u6d41\u4e2d\u6e05\u9664\u9519\u8bef\u7684\u8f93\u5165 continue ; // try again } std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // \u4ece\u8f93\u5165\u6d41\u4e2d\u5254\u9664\u591a\u4f59\u7684\u8f93\u5165 if ( age <= 0 ) // make sure age is positive continue ; break ; } std :: cout << \"You entered: \" << age << '\\n' ; } \u5982\u679c\u4f60\u4e0d\u5e0c\u671b\u8fd9\u6837\u7684\u8f93\u5165\u662f\u6709\u6548\u7684\uff0c\u90a3\u8fd8\u9700\u8981\u505a\u4e00\u4e9b\u989d\u5916\u7684\u5de5\u4f5c\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u524d\u9762\u7684\u89e3\u51b3\u65b9\u6848\u5df2\u7ecf\u5b8c\u6210\u4e86\u4e00\u534a\u7684\u5de5\u4f5c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 gcount() \u51fd\u6570\u6765\u786e\u5b9a\u6709\u591a\u5c11\u5b57\u7b26\u88ab\u5ffd\u7565\u4e86\u3002\u5982\u679c\u8f93\u5165\u6709\u6548\uff0c\u5219 gcount() \u5e94\u8be5\u8fd4\u56de1(\u88ab\u4e22\u5f03\u7684\u6362\u884c\u7b26)\u3002\u5982\u679c\u8fd4\u56de\u503c\u5927\u4e8e1\uff0c\u8bf4\u660e\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u6ca1\u6709\u88ab\u6b63\u786e\u63d0\u53d6\uff0c\u6b64\u65f6\u5e94\u8be5\u8981\u6c42\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002\u8fd9\u91cc\u6709\u4e00\u4e2a\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include #include int main () { int age {}; while ( true ) { std :: cout << \"Enter your age: \" ; std :: cin >> age ; if ( std :: cin . fail ()) // no extraction took place { std :: cin . clear (); // reset the state bits back to goodbit so we can use ignore() std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // clear out the bad input from the stream continue ; // try again } std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // clear out any additional input from the stream if ( std :: cin . gcount () > 1 ) // if we cleared out more than one additional character { continue ; // we'll consider this input to be invalid } if ( age <= 0 ) // make sure age is positive { continue ; } break ; } std :: cout << \"You entered: \" << age << '\\n' ; }","title":"\u6570\u503c\u9a8c\u8bc1"},{"location":"23-5-stream-states-and-input-validation/#_5","text":"\u4e3a\u4e86\u5f97\u5230\u4e00\u4e2a\u503c\uff0c\u7adf\u7136\u8981\u50cf\u4e0a\u9762\u90a3\u6837\u8fd9\u4e48\u591a\u7684\u64cd\u4f5c\uff01\u5904\u7406\u6570\u5b57\u8f93\u5165\u7684\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u5c06\u5176\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8bfb\u5165\uff0c\u7136\u540e\u5c1d\u8bd5\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u5b57\u7c7b\u578b\u3002\u4e0b\u9762\u7684\u7a0b\u5e8f\u4f7f\u7528\u4e86\u8fd9\u79cd\u65b9\u6cd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include // std::from_chars #include #include #include #include std :: optional < int > extractAge ( std :: string_view age ) { int result {}; auto end { age . data () + age . length () }; // \u5c1d\u8bd5\u4eceage\u4e2d\u63d0\u53d6\u4e00\u4e2a\u6574\u6570 if ( std :: from_chars ( age . data (), end , result ). ptr != end ) { return {}; } if ( result <= 0 ) // \u786e\u4fdd\u5e74\u9f84\u662f\u6b63\u6570 { return {}; } return result ; } int main () { int age {}; while ( true ) { std :: cout << \"Enter your age: \" ; std :: string strAge {}; std :: getline ( std :: cin >> std :: ws , strAge ); if ( auto extracted { extractAge ( strAge ) }) { age = * extracted ; break ; } } std :: cout << \"You entered: \" << age << '\\n' ; } COPY \u4e0e\u76f4\u63a5\u63d0\u53d6\u6570\u5b57\u76f8\u6bd4\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7684\u5de5\u4f5c\u91cf\u662f\u591a\u8fd8\u662f\u5c11\u53d6\u51b3\u4e8e\u9a8c\u8bc1\u53c2\u6570\u548c\u9650\u5236\u3002 \u5982\u4f60\u6240\u89c1\uff0c\u5728C++\u4e2d\u8fdb\u884c\u8f93\u5165\u9a8c\u8bc1\u9700\u8981\u5927\u91cf\u7684\u5de5\u4f5c\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u8bb8\u591a\u8fd9\u6837\u7684\u4efb\u52a1(\u4f8b\u5982\uff0c\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fdb\u884c\u6570\u503c\u9a8c\u8bc1)\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u8f6c\u6362\u4e3a\u53ef\u4ee5\u5728\u5404\u79cd\u60c5\u51b5\u4e0b\u91cd\u7528\u7684\u51fd\u6570\u3002","title":"\u6570\u503c\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fdb\u884c\u9a8c\u8bc1"},{"location":"23-6-basic-file-IO/","tags":["io","file"],"text":"Key Takeaway \u57fa\u672c\u7684\u6587\u4ef6IO\u7c7b\u6709\u4e09\u4e2a\uff1a ifstream (\u6d3e\u751f\u81ea istream )\u3001 ofstream (\u6d3e\u751f\u81ea ostream ) \u548c fstream (\u6d3e\u751f\u81ea iostream ) C++\u4e2d\u7684\u6587\u4ef6I/O\u548c\u666e\u901a I/O \u975e\u5e38\u7c7b\u4f3c\u3002\u57fa\u672c\u7684\u6587\u4ef6IO\u7c7b\u6709\u4e09\u4e2a\uff1a ifstream (\u6d3e\u751f\u81ea istream )\u3001 ofstream (\u6d3e\u751f\u81ea ostream ) \u548c fstream (\u6d3e\u751f\u81ea iostream )\u3002\u8fd9\u4e9b\u7c7b\u53ef\u4ee5\u5206\u522b\u7528\u4e8e\u6587\u4ef6\u8f93\u5165\u3001\u6587\u4ef6\u8f93\u51fa\u548c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa\u3002\u4f7f\u7528\u8fd9\u4e9b\u6587\u4ef6IO\u7c7b\u65f6\uff0c\u9700\u8981\u5305\u542b fstream \u5934\u6587\u4ef6\u3002 \u548c cout \u3001 cin \u3001 cerr \u4ee5\u53ca clog \u8fd9\u4e9b\u5df2\u7ecf\u8fde\u63a5\u597d\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7684\u6d41\u4e0d\u540c\uff0c\u6587\u4ef6\u6d41\u5fc5\u987b\u7531\u7a0b\u5e8f\u5458\u624b\u52a8\u8bbe\u7f6e\u3002\u4e0d\u8fc7\uff0c\u64cd\u4f5c\u5f88\u7b80\u5355\uff1a\u8981\u6253\u5f00\u4e00\u4e2a\u7528\u4e8e\u8bfb\u5199\u7684\u6587\u4ef6\u65f6\uff0c\u53ea\u9700\u8981\u5b9e\u4f8b\u5316\u4e00\u4e2a\u9700\u8981\u7684\u6587\u4ef6IO\u7c7b\uff08\u5c06\u6587\u4ef6\u540d\u4f5c\u4e3a\u53c2\u6570\uff09\u3002\u7136\u540e\u4f7f\u7528 \u63d0\u53d6\u8fd0\u7b97\u7b26 \u6216 \u63d2\u5165\u8fd0\u7b97\u7b26 \u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u6216\u5411\u6587\u4ef6\u5199\u5165\u6570\u636e\u3002\u5b8c\u6210\u64cd\u4f5c\u540e\uff0c \u6709\u51e0\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5173\u95ed\u8be5\u6587\u4ef6\uff1a\u663e\u5f0f\u5730\u8c03\u7528 close() \u51fd\u6570\u6216\u8005\u8ba9\u6587\u4ef6IO\u53d8\u91cf \u79bb\u5f00\u4f5c\u7528\u57df \uff08 \u6790\u6784\u51fd\u6570 \u4f1a\u5e2e\u4f60\u5173\u95ed\u6587\u4ef6\uff09\u3002 \u6587\u4ef6\u8f93\u51fa \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86 ofstream \u7c7b\u8fdb\u884c\u6587\u4ef6\u8f93\u51fa\u3002\u975e\u5e38\u7b80\u5355\u76f4\u63a5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include int main () { // ofstream \u7528\u4e8e\u5199\u6587\u4ef6 // \u521b\u5efa\u4e00\u4e2a\u540d\u4e3a Sample.txt \u7684\u6587\u4ef6 std :: ofstream outf { \"Sample.txt\" }; // \u5982\u679c\u65e0\u6cd5\u6253\u5f00\u8f93\u51fa\u6587\u4ef6\u6d41 if ( ! outf ) { // \u6253\u5370\u9519\u8bef\u5e76\u9000\u51fa std :: cerr << \"Uh oh, Sample.txt could not be opened for writing! \\n \" ; return 1 ; } // \u5411\u6587\u4ef6\u5199\u5165\u4e24\u884c\u6587\u5b57 outf << \"This is line 1 \\n \" ; outf << \"This is line 2 \\n \" ; return 0 ; // outf \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0cofstream\u6790\u6784\u51fd\u6570\u4f1a\u5173\u95ed\u6587\u4ef6 } \u6b64\u65f6\u6253\u5f00\u9879\u76ee\u76ee\u5f55\uff0c\u5e94\u8be5\u4f1a\u770b\u5230\u4e00\u4e2a\u540d\u4e3a Sample.txt \u7684\u6587\u4ef6\u3002\u5982\u679c\u7528\u6587\u672c\u7f16\u8f91\u5668\u6253\u5f00\u5b83\uff0c\u53ef\u4ee5\u770b\u5230\u5b83\u786e\u5b9e\u5305\u542b\u4e86\u521a\u624d\u5199\u5165\u6587\u4ef6\u7684\u4e24\u884c\u6587\u672c\u3002 \u6ce8\u610f\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 put() \u51fd\u6570\u5411\u6587\u4ef6\u5199\u5165\u5355\u4e2a\u5b57\u7b26\u3002 \u6587\u4ef6\u8f93\u5165 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f1a\u6253\u5f00\u4e0a\u4e00\u4e2a\u4f8b\u5b50\u4e2d\u65b0\u521b\u5efa\u7684\u6587\u4ef6\u5e76\u8bfb\u53d6\u5176\u5185\u5bb9\u3002\u6ce8\u610f\uff0c\u5728\u5230\u8fbe\u6587\u4ef6\u672b\u5c3e\uff08EOF\uff09\u65f6\uff0c ifstream \u4f1a\u8fd4\u56de0\u3002\u6211\u4eec\u9700\u8981\u57fa\u4e8e\u8fd9\u4e2a\u7279\u6027\u6765\u5224\u65ad\u8bfb\u53d6\u591a\u5c11\u5185\u5bb9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include #include int main () { // ifstream \u7528\u4e8e\u8bfb\u53d6\u6587\u4ef6 // \u4ece Sample.txt \u8bfb\u53d6\u5185\u5bb9 std :: ifstream inf { \"Sample.txt\" }; // \u5982\u679c\u65e0\u6cd5\u6253\u5f00\u8f93\u5165\u6587\u4ef6\u6d41 if ( ! inf ) { // \u6253\u5370\u9519\u8bef\u5e76\u9000\u51fa std :: cerr << \"Uh oh, Sample.txt could not be opened for reading! \\n \" ; return 1 ; } // \u5982\u679c\u4ecd\u7136\u6709\u5185\u5bb9\u53ef\u4ee5\u8bfb\u53d6 while ( inf ) { // \u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5b57\u7b26\u4e32\u5e76\u6253\u5370 std :: string strInput ; inf >> strInput ; std :: cout << strInput << '\\n' ; } return 0 ; // \u5f53 inf \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0cifstream \u7684\u6790\u6784\u51fd\u6570\u4f1a\u8d1f\u8d23\u5173\u95ed\u6587\u4ef6 } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 5 6 7 8 This is line 1 This is line 2 \u989d\u3002\u3002\u8fd9\u597d\u50cf\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c\u3002\u8fd8\u8bb0\u5f97\u5417\uff1f \u63d0\u53d6\u8fd0\u7b97\u7b26 \u4f1a\u88ab\u7a7a\u683c\u5206\u5272\uff0c\u6240\u4ee5\u4e3a\u4e86\u8bfb\u53d6\u4e00\u6574\u884c\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528 getline() \u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include #include #include int main () { // ifstream \u7528\u4e8e\u8bfb\u53d6\u6587\u4ef6 // \u4ece Sample.txt \u8bfb\u53d6\u5185\u5bb9 std :: ifstream inf { \"Sample.txt\" }; // \u5982\u679c\u65e0\u6cd5\u6253\u5f00\u8f93\u5165\u6587\u4ef6\u6d41 if ( ! inf ) { // \u6253\u5370\u9519\u8bef\u5e76\u9000\u51fa std :: cerr << \"Uh oh, Sample.txt could not be opened for reading! \\n \" ; return 1 ; } // \u5982\u679c\u4ecd\u7136\u6709\u5185\u5bb9\u53ef\u4ee5\u8bfb\u53d6 while ( inf ) { // \u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5b57\u7b26\u4e32\u5e76\u6253\u5370 std :: string strInput ; std :: getline ( inf , strInput ); //\u4f7f\u7528 getline \u8bfb\u53d6\u4e00\u6574\u884c std :: cout << strInput << '\\n' ; } return 0 ; // \u5f53 inf \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0cifstream \u7684\u6790\u6784\u51fd\u6570\u4f1a\u8d1f\u8d23\u5173\u95ed\u6587\u4ef6 } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 This is line 1 This is line 2 \u5e26\u7f13\u51b2\u7684\u8f93\u51fa C++\u4e2d\u7684\u8f93\u51fa\u53ef\u4ee5\u88ab\u7f13\u51b2\u3002\u8fd9\u610f\u5473\u7740\u8f93\u51fa\u5230\u6587\u4ef6\u6d41\u7684\u4efb\u4f55\u5185\u5bb9\u90fd\u4e0d\u4f1a\u7acb\u5373\u5199\u5165\u78c1\u76d8\u3002\u8fd9\u6837\u4e00\u6765\u5c31\u53ef\u4ee5\u5c06\u51e0\u4e2a\u8f93\u51fa\u64cd\u4f5c\u5408\u5e76\u5904\u7406\u3002\u8fd9\u6837\u505a\u4e3b\u8981\u662f\u51fa\u4e8e\u6027\u80fd\u539f\u56e0\uff0c\u5f53\u7f13\u51b2\u533a\u88ab\u5199\u5165\u78c1\u76d8\u65f6\uff0c\u8fd9\u88ab\u79f0\u4e3a \u5237\u65b0\u7f13\u51b2\u533a \u3002 \u89e6\u53d1\u7f13\u51b2\u533a\u5237\u65b0\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u5173\u95ed\u6587\u4ef6\u2014\u2014\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u5c06\u88ab\u5237\u65b0\u5230\u78c1\u76d8\uff0c\u7136\u540e\u5173\u95ed\u6587\u4ef6\u3002 \u7f13\u51b2\u901a\u5e38\u4e0d\u662f\u95ee\u9898\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5b83\u53ef\u80fd\u4f1a\u7ed9\u7c97\u5fc3\u7684\u4eba\u5e26\u6765\u9ebb\u70e6\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e3b\u8981\u7684\u7f6a\u9b41\u7978\u9996\u662f\u7f13\u51b2\u533a\u4e2d\u6709\u6570\u636e\uff0c\u7136\u540e\u7a0b\u5e8f\u7acb\u5373\u7ec8\u6b62(\u901a\u8fc7\u5d29\u6e83\u6216\u8c03\u7528 exit() )\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e0d\u4f1a\u6267\u884c\u6587\u4ef6\u6d41\u7c7b\u7684\u6790\u6784\u51fd\u6570\uff0c\u8fd9\u610f\u5473\u7740\u6587\u4ef6\u6c38\u8fdc\u4e0d\u4f1a\u5173\u95ed\uff0c\u6240\u4ee5\u7f13\u51b2\u533a\u6c38\u8fdc\u4e0d\u4f1a\u5237\u65b0\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f13\u51b2\u533a\u4e2d\u7684\u6570\u636e\u5c06\u4e0d\u4f1a\u88ab\u5199\u5165\u78c1\u76d8\uff0c\u5e76\u4e14\u6c38\u8fdc\u4e22\u5931\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u5728\u8c03\u7528 exit() \u4e4b\u524d\u663e\u5f0f\u5173\u95ed\u6240\u6709\u6253\u5f00\u7684\u6587\u4ef6\u603b\u662f\u4e00\u4e2a\u597d\u4e3b\u610f \u7f13\u51b2\u533a\u53ef\u4ee5\u901a\u8fc7 ostream::flush() \u51fd\u6570\u624b\u52a8\u5237\u65b0\uff0c\u6216\u8005\u5411\u8f93\u51fa\u6d41\u53d1\u9001 std::flush \u4e5f\u53ef\u4ee5\u3002\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u53ef\u4ee5\u7528\u4e8e\u786e\u4fdd\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u7acb\u5373\u5199\u5165\u78c1\u76d8\uff0c\u4ee5\u9632\u7a0b\u5e8f\u7a81\u7136\u5d29\u6e83\u3002 \u53e6\u5916\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c std::endl; \u4f1a\u5237\u65b0\u8f93\u51fa\u6d41\u3002\u56e0\u6b64\uff0c\u8fc7\u5ea6\u4f7f\u7528 std::endl (\u5bfc\u81f4\u4e0d\u5fc5\u8981\u7684\u7f13\u51b2\u533a\u5237\u65b0)\u53ef\u80fd\u4f1a\u5728\u6267\u884c\u7f13\u5b58I/O\u65f6\u4ea7\u751f\u6027\u80fd\u5f71\u54cd\uff0c\u56e0\u4e3a\u5237\u65b0\u64cd\u4f5c\u5f00\u9500\u5f88\u5927(\u4f8b\u5982\u5199\u5165\u6587\u4ef6)\u3002\u7531\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u6ce8\u91cd\u6027\u80fd\u7684\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u4f7f\u7528 \\n \u800c\u4e0d\u662f std::endl \u5728\u8f93\u51fa\u6d41\u4e2d\u63d2\u5165\u6362\u884c\u7b26\uff0c\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u7f13\u51b2\u533a\u5237\u65b0\u3002 \u6587\u4ef6\u6a21\u5f0f \u5982\u679c\u6211\u4eec\u8bd5\u56fe\u5411\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u6587\u4ef6\u5199\u5165\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u518d\u6b21\u8fd0\u884c\u8f93\u51fa\u793a\u4f8b\u53ef\u4ee5\u770b\u5230\uff0c\u6bcf\u6b21\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u539f\u59cb\u6587\u4ef6\u90fd\u88ab\u5b8c\u5168\u8986\u76d6\u3002\u5982\u679c\u6211\u4eec\u60f3\u5728\u6587\u4ef6\u7684\u672b\u5c3e\u8ffd\u52a0\u66f4\u591a\u7684\u6570\u636e\u5e94\u8be5\u600e\u4e48\u64cd\u4f5c\u5462\uff1f\u5b9e\u9645\u4e0a\uff0c\u6587\u4ef6\u6d41\u6784\u9020\u51fd\u6570\u63a5\u53d7\u7b2c\u4e8c\u4e2a\u53ef\u9009\u53c2\u6570\uff0c\u8be5\u53c2\u6570\u53ef\u4ee5\u6307\u5b9a\u5e94\u8be5\u5982\u4f55\u6253\u5f00\u6587\u4ef6\u3002\u8fd9\u4e2a\u53c2\u6570\u88ab\u79f0\u4e3a\u6a21\u5f0f(mode)\uff0c\u5b83\u63a5\u53d7\u7684\u6709\u6548\u6807\u5fd7\u5b58\u5728\u4e8e ios \u7c7b\u4e2d\u3002 Ios file mode Meaning app \u4f7f\u7528\u8ffd\u52a0\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6 ate \u5728\u8bfb\u5199\u524d\u79fb\u52a8\u5230\u6587\u4ef6\u672b\u5c3e binary \u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6(\u800c\u975e\u6587\u672c\u6a21\u5f0f) in \u4ee5\u8bfb\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6( ifstream \u7684\u9ed8\u8ba4\u6a21\u5f0f) out \u4ee5\u5199\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6( ofstream \u7684\u9ed8\u8ba4\u6a21\u5f0f) trunc \u5982\u679c\u6587\u4ef6\u5b58\u5728\u5219\u6e05\u6389\u5176\u5185\u5bb9 \u4f7f\u7528 \u6309\u4f4d\u6216 \u8fd0\u7b97\u7b26\u53ef\u4ee5\u540c\u65f6\u8bbe\u7f6e\u8bfb\u4e2a\u6807\u8bb0\u3002 ifstream \u9ed8\u8ba4\u4f7f\u7528\u7684\u662f std::ios::in \u6587\u4ef6\u6a21\u5f0f\u3002\u800c ofstream \u9ed8\u8ba4\u4f7f\u7528\u4e86 std::ios::out \u6587\u4ef6\u6a21\u5f0f\u3002 fstream \u9ed8\u8ba4\u4f7f\u7528\u4e86 std::ios::in | std::ios::out \u6587\u4ef6\u6a21\u5f0f\uff0c\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u5bf9\u8be5\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u3002 \u5c0f\u8d34\u58eb \u5982\u679c\u4f7f\u7528\u4e86 std::ios::in \u4f46\u6587\u4ef6\u5e76\u4e0d\u5b58\u5728\uff0c\u5219 fstream \u4f1a\u5931\u8d25\u3002\u6240\u4ee5\u5982\u679c\u4f60\u5e0c\u671b\u4f7f\u7528 fstream \u521b\u5efa\u4ee5\u65b0\u6587\u4ef6\uff0c\u8bf7\u786e\u4fdd\u53ea\u4f7f\u7528 std::ios::out \u6a21\u5f0f\u3002 \u8ba9\u6211\u4eec\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5411\u4e4b\u524d\u521b\u5efa\u7684 Sample.txt \u6587\u4ef6\u8ffd\u52a0\u4e24\u884c\u5185\u5bb9: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include int main () { // \u4f7f\u7528 ios:app \u6807\u8bb0\u544a\u77e5 ofstream \u8ffd\u52a0\u5185\u5bb9\u800c\u4e0d\u662f\u8986\u5199 // \u6211\u4eec\u4e0d\u9700\u8981\u6dfb\u52a0 std::ios::out // \u56e0\u4e3a ofstream \u9ed8\u8ba4\u662f std::ios::out std :: ofstream outf { \"Sample.txt\" , std :: ios :: app }; // \u5982\u679c\u4e0d\u80fd\u6253\u5f00\u6587\u4ef6 if ( ! outf ) { // \u6253\u5370\u9519\u8bef\u5e76\u9000\u51fa std :: cerr << \"Uh oh, Sample.txt could not be opened for writing! \\n \" ; return 1 ; } outf << \"This is line 3 \\n \" ; outf << \"This is line 4 \\n \" ; return 0 ; // \u5f53 inf \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0cifstream \u7684\u6790\u6784\u51fd\u6570\u4f1a\u8d1f\u8d23\u5173\u95ed\u6587\u4ef6 } \u73b0\u5728\uff0c\u5982\u679c\u6211\u4eec\u770b\u4e00\u4e0b Sample .txt (\u4f7f\u7528\u4e0a\u9762\u7684\u4e00\u4e2a\u793a\u4f8b\u7a0b\u5e8f\u6253\u5370\u5b83\u7684\u5185\u5bb9\uff0c\u6216\u5728\u6587\u672c\u7f16\u8f91\u5668\u4e2d\u52a0\u8f7d\u5b83)\uff0c\u53ef\u4ee5\u770b\u5230\u4ee5\u4e0b\u5185\u5bb9: 1 2 3 4 This is line 1 This is line 2 This is line 3 This is line 4 \u4f7f\u7528 open() \u6253\u5f00\u6587\u4ef6 \u5c31\u50cf\u53ef\u4ee5\u4f7f\u7528 close() \u663e\u5f0f\u5730\u5173\u95ed\u6587\u4ef6\u6d41\u4e00\u6837\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 open() \u663e\u5f0f\u5730\u6253\u5f00\u6587\u4ef6\u6d41\u3002 open() \u7684\u5de5\u4f5c\u539f\u7406\u4e0e\u6587\u4ef6\u6d41\u6784\u9020\u51fd\u6570\u7c7b\u4f3c\u2014\u2014\u5b83\u63a5\u53d7\u4e00\u4e2a\u6587\u4ef6\u540d\u548c\u4e00\u4e2a\u53ef\u9009\u7684\u6587\u4ef6\u6a21\u5f0f\u3002 \u4f8b\u5982: 1 2 3 4 5 6 7 8 9 std :: ofstream outf { \"Sample.txt\" }; outf << \"This is line 1 \\n \" ; outf << \"This is line 2 \\n \" outf . close (); // explicitly close the file // Oops, we forgot something outf . open ( \"Sample.txt\" , std :: ios :: app ); outf << \"This is line 3 \\n \" ; outf . close (); \u66f4\u591a\u5173\u4e8e open() \u51fd\u6570\u7684\u4fe1\u606f\u53ef\u4ee5\u5728 \u8fd9\u91cc \u627e\u5230\u3002","title":"23.6 - \u57fa\u672c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa"},{"location":"23-6-basic-file-IO/#_1","text":"\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86 ofstream \u7c7b\u8fdb\u884c\u6587\u4ef6\u8f93\u51fa\u3002\u975e\u5e38\u7b80\u5355\u76f4\u63a5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include int main () { // ofstream \u7528\u4e8e\u5199\u6587\u4ef6 // \u521b\u5efa\u4e00\u4e2a\u540d\u4e3a Sample.txt \u7684\u6587\u4ef6 std :: ofstream outf { \"Sample.txt\" }; // \u5982\u679c\u65e0\u6cd5\u6253\u5f00\u8f93\u51fa\u6587\u4ef6\u6d41 if ( ! outf ) { // \u6253\u5370\u9519\u8bef\u5e76\u9000\u51fa std :: cerr << \"Uh oh, Sample.txt could not be opened for writing! \\n \" ; return 1 ; } // \u5411\u6587\u4ef6\u5199\u5165\u4e24\u884c\u6587\u5b57 outf << \"This is line 1 \\n \" ; outf << \"This is line 2 \\n \" ; return 0 ; // outf \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0cofstream\u6790\u6784\u51fd\u6570\u4f1a\u5173\u95ed\u6587\u4ef6 } \u6b64\u65f6\u6253\u5f00\u9879\u76ee\u76ee\u5f55\uff0c\u5e94\u8be5\u4f1a\u770b\u5230\u4e00\u4e2a\u540d\u4e3a Sample.txt \u7684\u6587\u4ef6\u3002\u5982\u679c\u7528\u6587\u672c\u7f16\u8f91\u5668\u6253\u5f00\u5b83\uff0c\u53ef\u4ee5\u770b\u5230\u5b83\u786e\u5b9e\u5305\u542b\u4e86\u521a\u624d\u5199\u5165\u6587\u4ef6\u7684\u4e24\u884c\u6587\u672c\u3002 \u6ce8\u610f\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 put() \u51fd\u6570\u5411\u6587\u4ef6\u5199\u5165\u5355\u4e2a\u5b57\u7b26\u3002","title":"\u6587\u4ef6\u8f93\u51fa"},{"location":"23-6-basic-file-IO/#_2","text":"\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f1a\u6253\u5f00\u4e0a\u4e00\u4e2a\u4f8b\u5b50\u4e2d\u65b0\u521b\u5efa\u7684\u6587\u4ef6\u5e76\u8bfb\u53d6\u5176\u5185\u5bb9\u3002\u6ce8\u610f\uff0c\u5728\u5230\u8fbe\u6587\u4ef6\u672b\u5c3e\uff08EOF\uff09\u65f6\uff0c ifstream \u4f1a\u8fd4\u56de0\u3002\u6211\u4eec\u9700\u8981\u57fa\u4e8e\u8fd9\u4e2a\u7279\u6027\u6765\u5224\u65ad\u8bfb\u53d6\u591a\u5c11\u5185\u5bb9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include #include int main () { // ifstream \u7528\u4e8e\u8bfb\u53d6\u6587\u4ef6 // \u4ece Sample.txt \u8bfb\u53d6\u5185\u5bb9 std :: ifstream inf { \"Sample.txt\" }; // \u5982\u679c\u65e0\u6cd5\u6253\u5f00\u8f93\u5165\u6587\u4ef6\u6d41 if ( ! inf ) { // \u6253\u5370\u9519\u8bef\u5e76\u9000\u51fa std :: cerr << \"Uh oh, Sample.txt could not be opened for reading! \\n \" ; return 1 ; } // \u5982\u679c\u4ecd\u7136\u6709\u5185\u5bb9\u53ef\u4ee5\u8bfb\u53d6 while ( inf ) { // \u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5b57\u7b26\u4e32\u5e76\u6253\u5370 std :: string strInput ; inf >> strInput ; std :: cout << strInput << '\\n' ; } return 0 ; // \u5f53 inf \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0cifstream \u7684\u6790\u6784\u51fd\u6570\u4f1a\u8d1f\u8d23\u5173\u95ed\u6587\u4ef6 } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 5 6 7 8 This is line 1 This is line 2 \u989d\u3002\u3002\u8fd9\u597d\u50cf\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c\u3002\u8fd8\u8bb0\u5f97\u5417\uff1f \u63d0\u53d6\u8fd0\u7b97\u7b26 \u4f1a\u88ab\u7a7a\u683c\u5206\u5272\uff0c\u6240\u4ee5\u4e3a\u4e86\u8bfb\u53d6\u4e00\u6574\u884c\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528 getline() \u51fd\u6570\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include #include #include int main () { // ifstream \u7528\u4e8e\u8bfb\u53d6\u6587\u4ef6 // \u4ece Sample.txt \u8bfb\u53d6\u5185\u5bb9 std :: ifstream inf { \"Sample.txt\" }; // \u5982\u679c\u65e0\u6cd5\u6253\u5f00\u8f93\u5165\u6587\u4ef6\u6d41 if ( ! inf ) { // \u6253\u5370\u9519\u8bef\u5e76\u9000\u51fa std :: cerr << \"Uh oh, Sample.txt could not be opened for reading! \\n \" ; return 1 ; } // \u5982\u679c\u4ecd\u7136\u6709\u5185\u5bb9\u53ef\u4ee5\u8bfb\u53d6 while ( inf ) { // \u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5b57\u7b26\u4e32\u5e76\u6253\u5370 std :: string strInput ; std :: getline ( inf , strInput ); //\u4f7f\u7528 getline \u8bfb\u53d6\u4e00\u6574\u884c std :: cout << strInput << '\\n' ; } return 0 ; // \u5f53 inf \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0cifstream \u7684\u6790\u6784\u51fd\u6570\u4f1a\u8d1f\u8d23\u5173\u95ed\u6587\u4ef6 } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 This is line 1 This is line 2","title":"\u6587\u4ef6\u8f93\u5165"},{"location":"23-6-basic-file-IO/#_3","text":"C++\u4e2d\u7684\u8f93\u51fa\u53ef\u4ee5\u88ab\u7f13\u51b2\u3002\u8fd9\u610f\u5473\u7740\u8f93\u51fa\u5230\u6587\u4ef6\u6d41\u7684\u4efb\u4f55\u5185\u5bb9\u90fd\u4e0d\u4f1a\u7acb\u5373\u5199\u5165\u78c1\u76d8\u3002\u8fd9\u6837\u4e00\u6765\u5c31\u53ef\u4ee5\u5c06\u51e0\u4e2a\u8f93\u51fa\u64cd\u4f5c\u5408\u5e76\u5904\u7406\u3002\u8fd9\u6837\u505a\u4e3b\u8981\u662f\u51fa\u4e8e\u6027\u80fd\u539f\u56e0\uff0c\u5f53\u7f13\u51b2\u533a\u88ab\u5199\u5165\u78c1\u76d8\u65f6\uff0c\u8fd9\u88ab\u79f0\u4e3a \u5237\u65b0\u7f13\u51b2\u533a \u3002 \u89e6\u53d1\u7f13\u51b2\u533a\u5237\u65b0\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u5173\u95ed\u6587\u4ef6\u2014\u2014\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u5c06\u88ab\u5237\u65b0\u5230\u78c1\u76d8\uff0c\u7136\u540e\u5173\u95ed\u6587\u4ef6\u3002 \u7f13\u51b2\u901a\u5e38\u4e0d\u662f\u95ee\u9898\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5b83\u53ef\u80fd\u4f1a\u7ed9\u7c97\u5fc3\u7684\u4eba\u5e26\u6765\u9ebb\u70e6\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e3b\u8981\u7684\u7f6a\u9b41\u7978\u9996\u662f\u7f13\u51b2\u533a\u4e2d\u6709\u6570\u636e\uff0c\u7136\u540e\u7a0b\u5e8f\u7acb\u5373\u7ec8\u6b62(\u901a\u8fc7\u5d29\u6e83\u6216\u8c03\u7528 exit() )\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e0d\u4f1a\u6267\u884c\u6587\u4ef6\u6d41\u7c7b\u7684\u6790\u6784\u51fd\u6570\uff0c\u8fd9\u610f\u5473\u7740\u6587\u4ef6\u6c38\u8fdc\u4e0d\u4f1a\u5173\u95ed\uff0c\u6240\u4ee5\u7f13\u51b2\u533a\u6c38\u8fdc\u4e0d\u4f1a\u5237\u65b0\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f13\u51b2\u533a\u4e2d\u7684\u6570\u636e\u5c06\u4e0d\u4f1a\u88ab\u5199\u5165\u78c1\u76d8\uff0c\u5e76\u4e14\u6c38\u8fdc\u4e22\u5931\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u5728\u8c03\u7528 exit() \u4e4b\u524d\u663e\u5f0f\u5173\u95ed\u6240\u6709\u6253\u5f00\u7684\u6587\u4ef6\u603b\u662f\u4e00\u4e2a\u597d\u4e3b\u610f \u7f13\u51b2\u533a\u53ef\u4ee5\u901a\u8fc7 ostream::flush() \u51fd\u6570\u624b\u52a8\u5237\u65b0\uff0c\u6216\u8005\u5411\u8f93\u51fa\u6d41\u53d1\u9001 std::flush \u4e5f\u53ef\u4ee5\u3002\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u53ef\u4ee5\u7528\u4e8e\u786e\u4fdd\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u7acb\u5373\u5199\u5165\u78c1\u76d8\uff0c\u4ee5\u9632\u7a0b\u5e8f\u7a81\u7136\u5d29\u6e83\u3002 \u53e6\u5916\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c std::endl; \u4f1a\u5237\u65b0\u8f93\u51fa\u6d41\u3002\u56e0\u6b64\uff0c\u8fc7\u5ea6\u4f7f\u7528 std::endl (\u5bfc\u81f4\u4e0d\u5fc5\u8981\u7684\u7f13\u51b2\u533a\u5237\u65b0)\u53ef\u80fd\u4f1a\u5728\u6267\u884c\u7f13\u5b58I/O\u65f6\u4ea7\u751f\u6027\u80fd\u5f71\u54cd\uff0c\u56e0\u4e3a\u5237\u65b0\u64cd\u4f5c\u5f00\u9500\u5f88\u5927(\u4f8b\u5982\u5199\u5165\u6587\u4ef6)\u3002\u7531\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u6ce8\u91cd\u6027\u80fd\u7684\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u4f7f\u7528 \\n \u800c\u4e0d\u662f std::endl \u5728\u8f93\u51fa\u6d41\u4e2d\u63d2\u5165\u6362\u884c\u7b26\uff0c\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u7f13\u51b2\u533a\u5237\u65b0\u3002","title":"\u5e26\u7f13\u51b2\u7684\u8f93\u51fa"},{"location":"23-6-basic-file-IO/#_4","text":"\u5982\u679c\u6211\u4eec\u8bd5\u56fe\u5411\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u6587\u4ef6\u5199\u5165\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u518d\u6b21\u8fd0\u884c\u8f93\u51fa\u793a\u4f8b\u53ef\u4ee5\u770b\u5230\uff0c\u6bcf\u6b21\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u539f\u59cb\u6587\u4ef6\u90fd\u88ab\u5b8c\u5168\u8986\u76d6\u3002\u5982\u679c\u6211\u4eec\u60f3\u5728\u6587\u4ef6\u7684\u672b\u5c3e\u8ffd\u52a0\u66f4\u591a\u7684\u6570\u636e\u5e94\u8be5\u600e\u4e48\u64cd\u4f5c\u5462\uff1f\u5b9e\u9645\u4e0a\uff0c\u6587\u4ef6\u6d41\u6784\u9020\u51fd\u6570\u63a5\u53d7\u7b2c\u4e8c\u4e2a\u53ef\u9009\u53c2\u6570\uff0c\u8be5\u53c2\u6570\u53ef\u4ee5\u6307\u5b9a\u5e94\u8be5\u5982\u4f55\u6253\u5f00\u6587\u4ef6\u3002\u8fd9\u4e2a\u53c2\u6570\u88ab\u79f0\u4e3a\u6a21\u5f0f(mode)\uff0c\u5b83\u63a5\u53d7\u7684\u6709\u6548\u6807\u5fd7\u5b58\u5728\u4e8e ios \u7c7b\u4e2d\u3002 Ios file mode Meaning app \u4f7f\u7528\u8ffd\u52a0\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6 ate \u5728\u8bfb\u5199\u524d\u79fb\u52a8\u5230\u6587\u4ef6\u672b\u5c3e binary \u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6(\u800c\u975e\u6587\u672c\u6a21\u5f0f) in \u4ee5\u8bfb\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6( ifstream \u7684\u9ed8\u8ba4\u6a21\u5f0f) out \u4ee5\u5199\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6( ofstream \u7684\u9ed8\u8ba4\u6a21\u5f0f) trunc \u5982\u679c\u6587\u4ef6\u5b58\u5728\u5219\u6e05\u6389\u5176\u5185\u5bb9 \u4f7f\u7528 \u6309\u4f4d\u6216 \u8fd0\u7b97\u7b26\u53ef\u4ee5\u540c\u65f6\u8bbe\u7f6e\u8bfb\u4e2a\u6807\u8bb0\u3002 ifstream \u9ed8\u8ba4\u4f7f\u7528\u7684\u662f std::ios::in \u6587\u4ef6\u6a21\u5f0f\u3002\u800c ofstream \u9ed8\u8ba4\u4f7f\u7528\u4e86 std::ios::out \u6587\u4ef6\u6a21\u5f0f\u3002 fstream \u9ed8\u8ba4\u4f7f\u7528\u4e86 std::ios::in | std::ios::out \u6587\u4ef6\u6a21\u5f0f\uff0c\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u5bf9\u8be5\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u3002 \u5c0f\u8d34\u58eb \u5982\u679c\u4f7f\u7528\u4e86 std::ios::in \u4f46\u6587\u4ef6\u5e76\u4e0d\u5b58\u5728\uff0c\u5219 fstream \u4f1a\u5931\u8d25\u3002\u6240\u4ee5\u5982\u679c\u4f60\u5e0c\u671b\u4f7f\u7528 fstream \u521b\u5efa\u4ee5\u65b0\u6587\u4ef6\uff0c\u8bf7\u786e\u4fdd\u53ea\u4f7f\u7528 std::ios::out \u6a21\u5f0f\u3002 \u8ba9\u6211\u4eec\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5411\u4e4b\u524d\u521b\u5efa\u7684 Sample.txt \u6587\u4ef6\u8ffd\u52a0\u4e24\u884c\u5185\u5bb9: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include int main () { // \u4f7f\u7528 ios:app \u6807\u8bb0\u544a\u77e5 ofstream \u8ffd\u52a0\u5185\u5bb9\u800c\u4e0d\u662f\u8986\u5199 // \u6211\u4eec\u4e0d\u9700\u8981\u6dfb\u52a0 std::ios::out // \u56e0\u4e3a ofstream \u9ed8\u8ba4\u662f std::ios::out std :: ofstream outf { \"Sample.txt\" , std :: ios :: app }; // \u5982\u679c\u4e0d\u80fd\u6253\u5f00\u6587\u4ef6 if ( ! outf ) { // \u6253\u5370\u9519\u8bef\u5e76\u9000\u51fa std :: cerr << \"Uh oh, Sample.txt could not be opened for writing! \\n \" ; return 1 ; } outf << \"This is line 3 \\n \" ; outf << \"This is line 4 \\n \" ; return 0 ; // \u5f53 inf \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0cifstream \u7684\u6790\u6784\u51fd\u6570\u4f1a\u8d1f\u8d23\u5173\u95ed\u6587\u4ef6 } \u73b0\u5728\uff0c\u5982\u679c\u6211\u4eec\u770b\u4e00\u4e0b Sample .txt (\u4f7f\u7528\u4e0a\u9762\u7684\u4e00\u4e2a\u793a\u4f8b\u7a0b\u5e8f\u6253\u5370\u5b83\u7684\u5185\u5bb9\uff0c\u6216\u5728\u6587\u672c\u7f16\u8f91\u5668\u4e2d\u52a0\u8f7d\u5b83)\uff0c\u53ef\u4ee5\u770b\u5230\u4ee5\u4e0b\u5185\u5bb9: 1 2 3 4 This is line 1 This is line 2 This is line 3 This is line 4","title":"\u6587\u4ef6\u6a21\u5f0f"},{"location":"23-6-basic-file-IO/#open","text":"\u5c31\u50cf\u53ef\u4ee5\u4f7f\u7528 close() \u663e\u5f0f\u5730\u5173\u95ed\u6587\u4ef6\u6d41\u4e00\u6837\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 open() \u663e\u5f0f\u5730\u6253\u5f00\u6587\u4ef6\u6d41\u3002 open() \u7684\u5de5\u4f5c\u539f\u7406\u4e0e\u6587\u4ef6\u6d41\u6784\u9020\u51fd\u6570\u7c7b\u4f3c\u2014\u2014\u5b83\u63a5\u53d7\u4e00\u4e2a\u6587\u4ef6\u540d\u548c\u4e00\u4e2a\u53ef\u9009\u7684\u6587\u4ef6\u6a21\u5f0f\u3002 \u4f8b\u5982: 1 2 3 4 5 6 7 8 9 std :: ofstream outf { \"Sample.txt\" }; outf << \"This is line 1 \\n \" ; outf << \"This is line 2 \\n \" outf . close (); // explicitly close the file // Oops, we forgot something outf . open ( \"Sample.txt\" , std :: ios :: app ); outf << \"This is line 3 \\n \" ; outf . close (); \u66f4\u591a\u5173\u4e8e open() \u51fd\u6570\u7684\u4fe1\u606f\u53ef\u4ee5\u5728 \u8fd9\u91cc \u627e\u5230\u3002","title":"\u4f7f\u7528 open()\u6253\u5f00\u6587\u4ef6"},{"location":"23-7-random-file-IO/","tags":["io"],"text":"Key Takeaway \u6587\u4ef6\u6307\u9488 \u6bcf\u4e2a\u6587\u4ef6\u6d41\u7c7b\u90fd\u5305\u542b\u4e00\u4e2a\u6587\u4ef6\u6307\u9488\uff0c\u7528\u4e8e\u8ddf\u8e2a\u6587\u4ef6\u5185\u7684\u5f53\u524d\u8bfb/\u5199\u4f4d\u7f6e\u3002\u5f53\u5bf9\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u65f6\uff0c\u8bfb/\u5199\u64cd\u4f5c\u53d1\u751f\u5728\u6587\u4ef6\u6307\u9488\u7684\u5f53\u524d\u4f4d\u7f6e\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5f53\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u65f6\uff0c\u6587\u4ef6\u6307\u9488\u88ab\u8bbe\u7f6e\u4e3a\u6587\u4ef6\u7684\u5f00\u5934\u3002\u4f46\u662f\uff0c\u5982\u679c\u4ee5\u8ffd\u52a0\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u5219\u6587\u4ef6\u6307\u9488\u5c06\u79fb\u52a8\u5230\u6587\u4ef6\u7684\u672b\u5c3e\uff0c\u56e0\u6b64\u5199\u5165\u64cd\u4f5c\u4e0d\u4f1a\u8986\u76d6\u6587\u4ef6\u7684\u4efb\u4f55\u5f53\u524d\u5185\u5bb9\u3002 \u4f7f\u7528 seekg() \u548c seekp() \u8fdb\u884c\u968f\u673a\u6587\u4ef6\u8bbf\u95ee \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u6240\u5b8c\u6210\u7684\u6587\u4ef6\u8bbf\u95ee\u90fd\u662f\u987a\u5e8f\u7684\u2014\u2014\u5373\u6309\u987a\u5e8f\u8bfb\u53d6\u6216\u5199\u5165\u4e86\u6587\u4ef6\u5185\u5bb9\u3002\u4f46\u662f\uff0c\u5176\u5b9e\u4e5f\u53ef\u4ee5\u8fdb\u884c\u968f\u673a\u6587\u4ef6\u8bbf\u95ee\u2014\u2014\u5373\u8df3\u5230\u6587\u4ef6\u4e2d\u7684\u67d0\u4e2a\u4f4d\u7f6e\u518d\u8bfb\u53d6\u5176\u5185\u5bb9\u3002\u5f53\u4f60\u5e0c\u671b\u4ece\u5305\u542b\u5927\u91cf\u8bb0\u5f55\u7684\u6587\u4ef6\u4e2d\u68c0\u7d22\u7279\u5b9a\u7684\u8bb0\u5f55\u65f6\uff0c\u8fd9\u662f\u5f88\u6709\u7528\u7684\u3002\u56e0\u4e3a\u4f60\u53ef\u4ee5\u76f4\u63a5\u8df3\u5230\u60f3\u8981\u68c0\u7d22\u7684\u8bb0\u5f55\uff0c\u800c\u4e0d\u5fc5\u8bfb\u53d6\u6240\u6709\u7684\u8bb0\u5f55\u5e76\u4ece\u4e2d\u627e\u5230\u4f60\u60f3\u8981\u7684\u8bb0\u5f55\u3002 \u901a\u8fc7\u4f7f\u7528 seekg() \u51fd\u6570(\u7528\u4e8e\u8f93\u5165)\u548c seekp() \u51fd\u6570(\u7528\u4e8e\u8f93\u51fa)\u64cd\u4f5c\u6587\u4ef6\u6307\u9488\u53ef\u4ee5\u5b9e\u73b0\u6587\u4ef6\u968f\u673a\u8bbf\u95ee\u3002\u5982\u679c\u4f60\u60f3\u77e5\u9053\uff0c g \u4ee3\u8868\"get\" \uff0c p \u4ee3\u8868\"put\"\u3002\u5bf9\u4e8e\u67d0\u4e9b\u7c7b\u578b\u7684\u6d41\uff0c seekg() (\u6539\u53d8\u8bfb\u4f4d\u7f6e)\u548c seekp() (\u6539\u53d8\u5199\u4f4d\u7f6e)\u72ec\u7acb\u64cd\u4f5c\u2014\u2014\u7136\u800c\uff0c\u5bf9\u4e8e\u6587\u4ef6\u6d41\uff0c\u8bfb\u548c\u5199\u4f4d\u7f6e\u603b\u662f\u76f8\u540c\u7684\uff0c\u56e0\u6b64 seekg \u548c seekp \u53ef\u4ee5\u4e92\u6362\u4f7f\u7528\u3002 seekg() \u548c seekp() \u51fd\u6570\u6709\u4e24\u4e2a \u5f62\u53c2 \u3002\u7b2c\u4e00\u4e2a\u5f62\u53c2\u662f\u6587\u4ef6\u6307\u9488\u9700\u8981\u7f16\u8bd1\u7684\u5b57\u8282\u6570\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5219\u662f ios \u6807\u8bb0\uff0c\u7528\u4e8e\u6307\u5b9a\u4ece\u54ea\u91cc\u504f\u79fb\uff08\u504f\u79fb\u57fa\u51c6\u70b9\uff09\u3002 ios \u641c\u7d22\u6807\u8bb0 \u542b\u4e49 beg \u76f8\u5bf9\u4e8e\u6587\u4ef6\u5f00\u5934\u8fdb\u884c\u504f\u79fb (\u9ed8\u8ba4) cur \u76f8\u5bf9\u4e8e\u5f53\u524d\u4f4d\u7f6e\u8fdb\u884c\u504f\u79fb end \u76f8\u5bf9\u4e8e\u6587\u4ef6\u7ed3\u5c3e\u8fdb\u884c\u504f\u79fb \u6b63\u504f\u79fb\u91cf\u610f\u5473\u7740\u5c06\u6587\u4ef6\u6307\u9488\u5411\u6587\u4ef6\u7684\u672b\u5c3e\u79fb\u52a8\uff0c\u800c\u8d1f\u504f\u79fb\u91cf\u610f\u5473\u7740\u5c06\u6587\u4ef6\u6307\u9488\u5411\u6587\u4ef6\u5f00\u5934\u79fb\u52a8\u3002 \u4e0b\u9762\u662f\u4e00\u4e9b\u4f8b\u5b50\uff1a 1 2 3 4 5 inf . seekg ( 14 , std :: ios :: cur ); // \u5411\u524d\u79fb\u52a814\u5b57\u8282 inf . seekg ( -18 , std :: ios :: cur ); // \u5411\u540e\u79fb\u52a818\u5b57\u8282 inf . seekg ( 22 , std :: ios :: beg ); // \u79fb\u52a8\u5230\u6587\u4ef6\u7684\u7b2c22\u5b57\u8282 inf . seekg ( 24 ); // \u79fb\u52a8\u5230\u6587\u4ef6\u7684\u7b2c22\u5b57\u8282 inf . seekg ( -28 , std :: ios :: end ); // \u79fb\u52a8\u5230\u6587\u4ef6\u7ed3\u675f\u524d\u7684\u7b2c28\u5b57\u8282 \u79fb\u52a8\u5230\u6587\u4ef6\u7684\u5f00\u5934\u6216\u7ed3\u5c3e\u5f88\u5bb9\u6613\uff1a 1 2 inf . seekg ( 0 , std :: ios :: beg ); // \u79fb\u52a8\u5230\u6587\u4ef6\u7684\u5f00\u5934 inf . seekg ( 0 , std :: ios :: end ); // \u79fb\u52a8\u5230\u6587\u4ef6\u7684\u7ed3\u5c3e \u4f7f\u7528 seekg() \u548c\u4e0a\u4e00\u8bfe\u4e2d\u7684\u521b\u5efa\u7684\u8f93\u5165\u6587\u4ef6\u505a\u4e00\u4e2a\u4f8b\u5b50\u3002\u8f93\u5165\u6587\u4ef6\u7684\u5185\u5bb9\u5982\u4e0b\uff1a 1 2 3 4 This is line 1 This is line 2 This is line 3 This is line 4 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include #include #include int main () { std :: ifstream inf { \"Sample.txt\" }; // If we couldn't open the input file stream for reading if ( ! inf ) { // Print an error and exit std :: cerr << \"Uh oh, Sample.txt could not be opened for reading! \\n \" ; return 1 ; } std :: string strData ; inf . seekg ( 5 ); // move to 5th character // Get the rest of the line and print it, moving to line 2 std :: getline ( inf , strData ); std :: cout << strData << '\\n' ; inf . seekg ( 8 , std :: ios :: cur ); // move 8 more bytes into file // Get rest of the line and print it std :: getline ( inf , strData ); std :: cout << strData << '\\n' ; inf . seekg ( -14 , std :: ios :: end ); // move 14 bytes before end of file // Get rest of the line and print it std :: getline ( inf , strData ); std :: cout << strData << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 is line 1 line 2 This is line 4 \u6ce8\u610f\uff1a\u5f53\u4e0e\u6587\u672c\u6587\u4ef6\u4e00\u8d77\u4f7f\u7528\u65f6\uff0c\u4e00\u4e9b\u7f16\u8bd1\u5668\u5bf9 seekg() \u548c seekp() \u7684\u5b9e\u73b0\u6709bug(\u7531\u4e8e\u7f13\u51b2\u7684\u5173\u7cfb)\u3002\u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u662f\u5176\u4e2d\u4e4b\u4e00(\u5982\u679c\u4f60\u7684\u8f93\u51fa\u7ed3\u679c\u548c\u4e0a\u9762\u4e0d\u540c\uff0c\u5219\u8bf4\u660e\u6709\u6b64\u7c7b\u95ee\u9898)\uff0c\u6b64\u65f6\u4f60\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6: 1 std :: ifstream inf ( \"Sample.txt\" , std :: ifstream :: binary ); \u53e6\u5916\u4e24\u4e2a\u6709\u7528\u7684\u51fd\u6570\u662f tellg() \u548c tellp() \uff0c\u5b83\u4eec\u8fd4\u56de\u6587\u4ef6\u6307\u9488\u7684\u7edd\u5bf9\u4f4d\u7f6e\u3002\u8fd9\u53ef\u4ee5\u7528\u6765\u786e\u5b9a\u6587\u4ef6\u7684\u5927\u5c0f: 1 2 3 std :: ifstream inf ( \"Sample.txt\" ); inf . seekg ( 0 , std :: ios :: end ); // move to end of file std :: cout << inf . tellg (); \u6253\u5370\u7ed3\u679c\uff1a 1 64 \u8fd9\u5c31\u662f Sample.txt \u7684\u5b57\u8282\u957f\u5ea6(\u5047\u8bbe\u5728\u6700\u540e\u4e00\u884c\u4e4b\u540e\u6709\u4e00\u4e2a\u56de\u8f66)\u3002 \u4f7f\u7528 fstream \u540c\u65f6\u8fdb\u884c\u6587\u4ef6\u7684\u8bfb\u5199 fstream \u7c7b\u53ef\u4ee5\u540c\u65f6\u8bfb\u5199\u6587\u4ef6\uff01\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b83\u4e0d\u80fd\u5728\u8bfb\u53d6\u548c\u5199\u5165\u4e4b\u95f4\u968f\u610f\u5207\u6362\u3002\u4e00\u65e6\u8fdb\u884c\u4e86\u8bfb\u6216\u5199\u64cd\u4f5c\uff0c\u5728\u4e24\u8005\u4e4b\u95f4\u5207\u6362\u7684\u552f\u4e00\u65b9\u6cd5\u662f\u6267\u884c\u4fee\u6539\u6587\u4ef6\u6307\u9488\u4f4d\u7f6e\u7684\u64cd\u4f5c(\u4f8b\u5982 seek )\u3002\u5982\u679c\u4f60\u4e0d\u60f3\u79fb\u52a8\u6587\u4ef6\u6307\u9488(\u56e0\u4e3a\u5b83\u5df2\u7ecf\u5728\u9700\u8981\u7684\u4f4d\u7f6e\u4e86)\uff0c\u4f60\u53ef\u4ee5\u5c06\u6307\u9488\u8c03\u6574\u5230\u5f53\u524d\u4f4d\u7f6e\uff1a 1 2 // assume iofile is an object of type fstream iofile . seekg ( iofile . tellg (), std :: ios :: beg ); // \u6307\u9488\u79fb\u52a8\u5230\u5f53\u524d\u4f4d\u7f6e \u5982\u679c\u4f60\u4e0d\u8fd9\u6837\u505a\uff0c\u4efb\u4f55\u5947\u602a\u7684\u4e8b\u60c5\u90fd\u53ef\u80fd\u53d1\u751f\u3002 (\u6ce8\u610f\uff1a\u5c3d\u7ba1\u770b\u8d77\u6765 iofile.seekg(0, std::ios::cur) \u4e5f\u80fd\u8d77\u5230\u76f8\u4f3c\u7684\u4f5c\u7528\u3002\u4f46\u662f\u5b9e\u9645\u4e0a\u6709\u4e9b\u7f16\u8bd1\u5668\u4f1a\u5c06\u5176\u4f18\u5316\u6389)\u3002 \u8fd8\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u548c ifstream \u4e0d\u540c\uff0c fstream \u4e0d\u80fd\u901a\u8fc7 while (inf) \u6765\u5224\u65ad\u662f\u5426\u8fbe\u5230\u6587\u4ef6\u672b\u5c3e\u3002 \u63a5\u4e0b\u6765\u4f7f\u7528 fstream \u6f14\u793a\u4e00\u4e0b\u6587\u4ef6\u8f93\u5165\u8f93\u51fa\u3002\u4e0b\u9762\u7a0b\u5e8f\u4f1a\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u8bfb\u53d6\u5176\u5185\u5bb9\uff0c\u7136\u540e\u5c06\u5176\u4e2d\u6240\u6709\u7684\u5143\u97f3\u5b57\u6bcd\u66ff\u6362\u4e3a\u2018 # \u2019\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 #include #include #include int main () { // Note we have to specify both in and out because we're using fstream std :: fstream iofile { \"Sample.txt\" , std :: ios :: in | std :: ios :: out }; // If we couldn't open iofile, print an error if ( ! iofile ) { // Print an error and exit std :: cerr << \"Uh oh, Sample.txt could not be opened! \\n \" ; return 1 ; } char chChar {}; // we're going to do this character by character // While there's still data to process while ( iofile . get ( chChar )) { switch ( chChar ) { // If we find a vowel case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : case 'A' : case 'E' : case 'I' : case 'O' : case 'U' : // Back up one character iofile . seekg ( -1 , std :: ios :: cur ); // Because we did a seek, we can now safely do a write, so // let's write a # over the vowel iofile << '#' ; // Now we want to go back to read mode so the next call // to get() will perform correctly. We'll seekg() to the current // location because we don't want to move the file pointer. iofile . seekg ( iofile . tellg (), std :: ios :: beg ); std :: cout << iofile . rdbuf (); break ; } } return 0 ; } \u8fd0\u884c\u7a0b\u5e8f\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 Th#s #s l#n# 1 Th#s #s l#n# 2 Th#s #s l#n# 3 Th#s #s l#n# 4 \u5176\u4ed6\u6709\u7528\u7684\u6587\u4ef6\u51fd\u6570 \u8981\u5220\u9664\u6587\u4ef6\uff0c\u53ea\u9700\u4f7f\u7528 remove() \u51fd\u6570\u3002 \u6b64\u5916\uff0c\u5982\u679c\u6d41\u662f\u6253\u5f00\u72b6\u6001\uff0c is_open() \u51fd\u6570\u4f1a\u8fd4\u56de true \uff0c\u5426\u5219\u8fd4\u56de false \u3002 \u5173\u4e8e\u5c06\u6307\u9488\u5199\u5165\u786c\u76d8\u7684\u8b66\u544a\u26a0\ufe0f \u867d\u7136\u5c06\u53d8\u91cf\u5199\u5230\u6587\u4ef6\u662f\u5f88\u5bb9\u6613\u505a\u5230\u7684\uff0c\u4f46\u5728\u5904\u7406\u6307\u9488\u65f6\uff0c\u60c5\u51b5\u5c31\u53d8\u5f97\u66f4\u52a0\u590d\u6742\u4e86\u3002\u8bb0\u4f4f\uff0c\u6307\u9488\u53ea\u662f\u4fdd\u5b58\u5b83\u6240\u6307\u5411\u7684\u53d8\u91cf\u7684\u5730\u5740\u3002\u5c3d\u7ba1\u53ef\u4ee5\u5c06\u5730\u5740\u8bfb\u5199\u5230\u78c1\u76d8\uff0c\u4f46\u8fd9\u6837\u505a\u662f\u975e\u5e38\u5371\u9669\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u53d8\u91cf\u7684\u5730\u5740\u5728\u6bcf\u6b21\u6267\u884c\u65f6\u53ef\u80fd\u4e0d\u540c\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u5f53\u4f60\u5c06\u8be5\u5730\u5740\u5199\u5165\u78c1\u76d8\u65f6\uff0c\u53d8\u91cf\u53ef\u80fd\u4f4d\u4e8e\u5730\u5740 0x0012FF7C \uff0c\u4f46\u5f53\u4f60\u518d\u6b21\u8bfb\u53d6\u8be5\u5730\u5740\u65f6\uff0c\u5b83\u53ef\u80fd\u4e0d\u518d\u4f4d\u4e8e\u8be5\u5730\u5740\u4e86\uff01 \u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3a nValue \u7684\u6574\u6570\uff0c\u4f4d\u4e8e\u5730\u5740 0x0012FF7C \u3002\u4f60\u7ed9 nValue \u8d4b\u503c5\u3002\u540c\u65f6\uff0c\u58f0\u660e\u4e86\u4e00\u4e2a\u540d\u4e3a *pnValue \u7684\u6307\u9488\uff0c \u5b83\u6307\u5411nValue \u3002 pnValue \u4fdd\u5b58 nValue \u7684\u5730\u5740 0x0012FF7C \u3002\u6b64\u65f6\uff0c\u4f60\u5e0c\u671b\u5c06\u8fd9\u4e9b\u53d8\u91cf\u4fdd\u5b58\u5230\u6587\u4ef6\u4ee5\u5907\u5c06\u6765\u4f7f\u7528\uff0c\u56e0\u6b64\u4f60\u5c06\u503c5\u548c\u5730\u5740 0x0012FF7C \u5199\u5165\u78c1\u76d8\u3002 \u51e0\u5468\u540e\uff0c\u518d\u6b21\u8fd0\u884c\u8be5\u7a0b\u5e8f\u5e76\u4ece\u78c1\u76d8\u8bfb\u53d6\u8fd9\u4e9b\u503c\u3002\u5c06\u503c5\u8bfb\u5165\u53e6\u4e00\u4e2a\u540d\u4e3a nValue \u7684\u53d8\u91cf\uff0c\u8be5\u53d8\u91cf\u4f4d\u4e8e 0x0012FF78 \u3002\u4f60\u5c06\u5730\u5740 0x0012FF7C \u8bfb\u5165\u540d\u4e3a *pnValue \u7684\u6307\u9488\u4e2d\u3002\u56e0\u4e3a pnValue \u73b0\u5728\u6307\u5411 0x0012FF7C \uff0c\u800c nValue \u4f4d\u4e8e 0x0012FF78 \uff0c pnValue \u4e0d\u518d\u6307\u5411 nValue \uff0c\u8bd5\u56fe\u8bbf\u95ee pnValue \u5c06\u4f1a\u5e26\u6765\u9ebb\u70e6\u3002 \u6ce8\u610f \u4e0d\u8981\u5411\u6587\u4ef6\u5199\u5165\u5185\u5b58\u5730\u5740\u3002\u5f53\u4f60\u4ece\u78c1\u76d8\u8bfb\u56de\u8fd9\u4e9b\u503c\u65f6\uff0c\u6700\u521d\u4f4d\u4e8e\u8fd9\u4e9b\u5730\u5740\u7684\u53d8\u91cf\u53ef\u80fd\u4f4d\u4e8e\u4e0d\u540c\u7684\u5730\u5740\uff0c\u8fd9\u4e9b\u5730\u5740\u662f\u65e0\u6548\u7684\u3002","title":"23.7 - \u968f\u673a\u6587\u4ef6\u8f93\u5165\u8f93\u51fa"},{"location":"23-7-random-file-IO/#_1","text":"\u6bcf\u4e2a\u6587\u4ef6\u6d41\u7c7b\u90fd\u5305\u542b\u4e00\u4e2a\u6587\u4ef6\u6307\u9488\uff0c\u7528\u4e8e\u8ddf\u8e2a\u6587\u4ef6\u5185\u7684\u5f53\u524d\u8bfb/\u5199\u4f4d\u7f6e\u3002\u5f53\u5bf9\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u65f6\uff0c\u8bfb/\u5199\u64cd\u4f5c\u53d1\u751f\u5728\u6587\u4ef6\u6307\u9488\u7684\u5f53\u524d\u4f4d\u7f6e\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5f53\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u65f6\uff0c\u6587\u4ef6\u6307\u9488\u88ab\u8bbe\u7f6e\u4e3a\u6587\u4ef6\u7684\u5f00\u5934\u3002\u4f46\u662f\uff0c\u5982\u679c\u4ee5\u8ffd\u52a0\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u5219\u6587\u4ef6\u6307\u9488\u5c06\u79fb\u52a8\u5230\u6587\u4ef6\u7684\u672b\u5c3e\uff0c\u56e0\u6b64\u5199\u5165\u64cd\u4f5c\u4e0d\u4f1a\u8986\u76d6\u6587\u4ef6\u7684\u4efb\u4f55\u5f53\u524d\u5185\u5bb9\u3002","title":"\u6587\u4ef6\u6307\u9488"},{"location":"23-7-random-file-IO/#seekg-seekp","text":"\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u6240\u5b8c\u6210\u7684\u6587\u4ef6\u8bbf\u95ee\u90fd\u662f\u987a\u5e8f\u7684\u2014\u2014\u5373\u6309\u987a\u5e8f\u8bfb\u53d6\u6216\u5199\u5165\u4e86\u6587\u4ef6\u5185\u5bb9\u3002\u4f46\u662f\uff0c\u5176\u5b9e\u4e5f\u53ef\u4ee5\u8fdb\u884c\u968f\u673a\u6587\u4ef6\u8bbf\u95ee\u2014\u2014\u5373\u8df3\u5230\u6587\u4ef6\u4e2d\u7684\u67d0\u4e2a\u4f4d\u7f6e\u518d\u8bfb\u53d6\u5176\u5185\u5bb9\u3002\u5f53\u4f60\u5e0c\u671b\u4ece\u5305\u542b\u5927\u91cf\u8bb0\u5f55\u7684\u6587\u4ef6\u4e2d\u68c0\u7d22\u7279\u5b9a\u7684\u8bb0\u5f55\u65f6\uff0c\u8fd9\u662f\u5f88\u6709\u7528\u7684\u3002\u56e0\u4e3a\u4f60\u53ef\u4ee5\u76f4\u63a5\u8df3\u5230\u60f3\u8981\u68c0\u7d22\u7684\u8bb0\u5f55\uff0c\u800c\u4e0d\u5fc5\u8bfb\u53d6\u6240\u6709\u7684\u8bb0\u5f55\u5e76\u4ece\u4e2d\u627e\u5230\u4f60\u60f3\u8981\u7684\u8bb0\u5f55\u3002 \u901a\u8fc7\u4f7f\u7528 seekg() \u51fd\u6570(\u7528\u4e8e\u8f93\u5165)\u548c seekp() \u51fd\u6570(\u7528\u4e8e\u8f93\u51fa)\u64cd\u4f5c\u6587\u4ef6\u6307\u9488\u53ef\u4ee5\u5b9e\u73b0\u6587\u4ef6\u968f\u673a\u8bbf\u95ee\u3002\u5982\u679c\u4f60\u60f3\u77e5\u9053\uff0c g \u4ee3\u8868\"get\" \uff0c p \u4ee3\u8868\"put\"\u3002\u5bf9\u4e8e\u67d0\u4e9b\u7c7b\u578b\u7684\u6d41\uff0c seekg() (\u6539\u53d8\u8bfb\u4f4d\u7f6e)\u548c seekp() (\u6539\u53d8\u5199\u4f4d\u7f6e)\u72ec\u7acb\u64cd\u4f5c\u2014\u2014\u7136\u800c\uff0c\u5bf9\u4e8e\u6587\u4ef6\u6d41\uff0c\u8bfb\u548c\u5199\u4f4d\u7f6e\u603b\u662f\u76f8\u540c\u7684\uff0c\u56e0\u6b64 seekg \u548c seekp \u53ef\u4ee5\u4e92\u6362\u4f7f\u7528\u3002 seekg() \u548c seekp() \u51fd\u6570\u6709\u4e24\u4e2a \u5f62\u53c2 \u3002\u7b2c\u4e00\u4e2a\u5f62\u53c2\u662f\u6587\u4ef6\u6307\u9488\u9700\u8981\u7f16\u8bd1\u7684\u5b57\u8282\u6570\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5219\u662f ios \u6807\u8bb0\uff0c\u7528\u4e8e\u6307\u5b9a\u4ece\u54ea\u91cc\u504f\u79fb\uff08\u504f\u79fb\u57fa\u51c6\u70b9\uff09\u3002 ios \u641c\u7d22\u6807\u8bb0 \u542b\u4e49 beg \u76f8\u5bf9\u4e8e\u6587\u4ef6\u5f00\u5934\u8fdb\u884c\u504f\u79fb (\u9ed8\u8ba4) cur \u76f8\u5bf9\u4e8e\u5f53\u524d\u4f4d\u7f6e\u8fdb\u884c\u504f\u79fb end \u76f8\u5bf9\u4e8e\u6587\u4ef6\u7ed3\u5c3e\u8fdb\u884c\u504f\u79fb \u6b63\u504f\u79fb\u91cf\u610f\u5473\u7740\u5c06\u6587\u4ef6\u6307\u9488\u5411\u6587\u4ef6\u7684\u672b\u5c3e\u79fb\u52a8\uff0c\u800c\u8d1f\u504f\u79fb\u91cf\u610f\u5473\u7740\u5c06\u6587\u4ef6\u6307\u9488\u5411\u6587\u4ef6\u5f00\u5934\u79fb\u52a8\u3002 \u4e0b\u9762\u662f\u4e00\u4e9b\u4f8b\u5b50\uff1a 1 2 3 4 5 inf . seekg ( 14 , std :: ios :: cur ); // \u5411\u524d\u79fb\u52a814\u5b57\u8282 inf . seekg ( -18 , std :: ios :: cur ); // \u5411\u540e\u79fb\u52a818\u5b57\u8282 inf . seekg ( 22 , std :: ios :: beg ); // \u79fb\u52a8\u5230\u6587\u4ef6\u7684\u7b2c22\u5b57\u8282 inf . seekg ( 24 ); // \u79fb\u52a8\u5230\u6587\u4ef6\u7684\u7b2c22\u5b57\u8282 inf . seekg ( -28 , std :: ios :: end ); // \u79fb\u52a8\u5230\u6587\u4ef6\u7ed3\u675f\u524d\u7684\u7b2c28\u5b57\u8282 \u79fb\u52a8\u5230\u6587\u4ef6\u7684\u5f00\u5934\u6216\u7ed3\u5c3e\u5f88\u5bb9\u6613\uff1a 1 2 inf . seekg ( 0 , std :: ios :: beg ); // \u79fb\u52a8\u5230\u6587\u4ef6\u7684\u5f00\u5934 inf . seekg ( 0 , std :: ios :: end ); // \u79fb\u52a8\u5230\u6587\u4ef6\u7684\u7ed3\u5c3e \u4f7f\u7528 seekg() \u548c\u4e0a\u4e00\u8bfe\u4e2d\u7684\u521b\u5efa\u7684\u8f93\u5165\u6587\u4ef6\u505a\u4e00\u4e2a\u4f8b\u5b50\u3002\u8f93\u5165\u6587\u4ef6\u7684\u5185\u5bb9\u5982\u4e0b\uff1a 1 2 3 4 This is line 1 This is line 2 This is line 3 This is line 4 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include #include #include int main () { std :: ifstream inf { \"Sample.txt\" }; // If we couldn't open the input file stream for reading if ( ! inf ) { // Print an error and exit std :: cerr << \"Uh oh, Sample.txt could not be opened for reading! \\n \" ; return 1 ; } std :: string strData ; inf . seekg ( 5 ); // move to 5th character // Get the rest of the line and print it, moving to line 2 std :: getline ( inf , strData ); std :: cout << strData << '\\n' ; inf . seekg ( 8 , std :: ios :: cur ); // move 8 more bytes into file // Get rest of the line and print it std :: getline ( inf , strData ); std :: cout << strData << '\\n' ; inf . seekg ( -14 , std :: ios :: end ); // move 14 bytes before end of file // Get rest of the line and print it std :: getline ( inf , strData ); std :: cout << strData << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 is line 1 line 2 This is line 4 \u6ce8\u610f\uff1a\u5f53\u4e0e\u6587\u672c\u6587\u4ef6\u4e00\u8d77\u4f7f\u7528\u65f6\uff0c\u4e00\u4e9b\u7f16\u8bd1\u5668\u5bf9 seekg() \u548c seekp() \u7684\u5b9e\u73b0\u6709bug(\u7531\u4e8e\u7f13\u51b2\u7684\u5173\u7cfb)\u3002\u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u662f\u5176\u4e2d\u4e4b\u4e00(\u5982\u679c\u4f60\u7684\u8f93\u51fa\u7ed3\u679c\u548c\u4e0a\u9762\u4e0d\u540c\uff0c\u5219\u8bf4\u660e\u6709\u6b64\u7c7b\u95ee\u9898)\uff0c\u6b64\u65f6\u4f60\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6: 1 std :: ifstream inf ( \"Sample.txt\" , std :: ifstream :: binary ); \u53e6\u5916\u4e24\u4e2a\u6709\u7528\u7684\u51fd\u6570\u662f tellg() \u548c tellp() \uff0c\u5b83\u4eec\u8fd4\u56de\u6587\u4ef6\u6307\u9488\u7684\u7edd\u5bf9\u4f4d\u7f6e\u3002\u8fd9\u53ef\u4ee5\u7528\u6765\u786e\u5b9a\u6587\u4ef6\u7684\u5927\u5c0f: 1 2 3 std :: ifstream inf ( \"Sample.txt\" ); inf . seekg ( 0 , std :: ios :: end ); // move to end of file std :: cout << inf . tellg (); \u6253\u5370\u7ed3\u679c\uff1a 1 64 \u8fd9\u5c31\u662f Sample.txt \u7684\u5b57\u8282\u957f\u5ea6(\u5047\u8bbe\u5728\u6700\u540e\u4e00\u884c\u4e4b\u540e\u6709\u4e00\u4e2a\u56de\u8f66)\u3002","title":"\u4f7f\u7528 seekg() \u548c seekp() \u8fdb\u884c\u968f\u673a\u6587\u4ef6\u8bbf\u95ee"},{"location":"23-7-random-file-IO/#fstream","text":"fstream \u7c7b\u53ef\u4ee5\u540c\u65f6\u8bfb\u5199\u6587\u4ef6\uff01\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b83\u4e0d\u80fd\u5728\u8bfb\u53d6\u548c\u5199\u5165\u4e4b\u95f4\u968f\u610f\u5207\u6362\u3002\u4e00\u65e6\u8fdb\u884c\u4e86\u8bfb\u6216\u5199\u64cd\u4f5c\uff0c\u5728\u4e24\u8005\u4e4b\u95f4\u5207\u6362\u7684\u552f\u4e00\u65b9\u6cd5\u662f\u6267\u884c\u4fee\u6539\u6587\u4ef6\u6307\u9488\u4f4d\u7f6e\u7684\u64cd\u4f5c(\u4f8b\u5982 seek )\u3002\u5982\u679c\u4f60\u4e0d\u60f3\u79fb\u52a8\u6587\u4ef6\u6307\u9488(\u56e0\u4e3a\u5b83\u5df2\u7ecf\u5728\u9700\u8981\u7684\u4f4d\u7f6e\u4e86)\uff0c\u4f60\u53ef\u4ee5\u5c06\u6307\u9488\u8c03\u6574\u5230\u5f53\u524d\u4f4d\u7f6e\uff1a 1 2 // assume iofile is an object of type fstream iofile . seekg ( iofile . tellg (), std :: ios :: beg ); // \u6307\u9488\u79fb\u52a8\u5230\u5f53\u524d\u4f4d\u7f6e \u5982\u679c\u4f60\u4e0d\u8fd9\u6837\u505a\uff0c\u4efb\u4f55\u5947\u602a\u7684\u4e8b\u60c5\u90fd\u53ef\u80fd\u53d1\u751f\u3002 (\u6ce8\u610f\uff1a\u5c3d\u7ba1\u770b\u8d77\u6765 iofile.seekg(0, std::ios::cur) \u4e5f\u80fd\u8d77\u5230\u76f8\u4f3c\u7684\u4f5c\u7528\u3002\u4f46\u662f\u5b9e\u9645\u4e0a\u6709\u4e9b\u7f16\u8bd1\u5668\u4f1a\u5c06\u5176\u4f18\u5316\u6389)\u3002 \u8fd8\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u548c ifstream \u4e0d\u540c\uff0c fstream \u4e0d\u80fd\u901a\u8fc7 while (inf) \u6765\u5224\u65ad\u662f\u5426\u8fbe\u5230\u6587\u4ef6\u672b\u5c3e\u3002 \u63a5\u4e0b\u6765\u4f7f\u7528 fstream \u6f14\u793a\u4e00\u4e0b\u6587\u4ef6\u8f93\u5165\u8f93\u51fa\u3002\u4e0b\u9762\u7a0b\u5e8f\u4f1a\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u8bfb\u53d6\u5176\u5185\u5bb9\uff0c\u7136\u540e\u5c06\u5176\u4e2d\u6240\u6709\u7684\u5143\u97f3\u5b57\u6bcd\u66ff\u6362\u4e3a\u2018 # \u2019\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 #include #include #include int main () { // Note we have to specify both in and out because we're using fstream std :: fstream iofile { \"Sample.txt\" , std :: ios :: in | std :: ios :: out }; // If we couldn't open iofile, print an error if ( ! iofile ) { // Print an error and exit std :: cerr << \"Uh oh, Sample.txt could not be opened! \\n \" ; return 1 ; } char chChar {}; // we're going to do this character by character // While there's still data to process while ( iofile . get ( chChar )) { switch ( chChar ) { // If we find a vowel case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : case 'A' : case 'E' : case 'I' : case 'O' : case 'U' : // Back up one character iofile . seekg ( -1 , std :: ios :: cur ); // Because we did a seek, we can now safely do a write, so // let's write a # over the vowel iofile << '#' ; // Now we want to go back to read mode so the next call // to get() will perform correctly. We'll seekg() to the current // location because we don't want to move the file pointer. iofile . seekg ( iofile . tellg (), std :: ios :: beg ); std :: cout << iofile . rdbuf (); break ; } } return 0 ; } \u8fd0\u884c\u7a0b\u5e8f\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 Th#s #s l#n# 1 Th#s #s l#n# 2 Th#s #s l#n# 3 Th#s #s l#n# 4","title":"\u4f7f\u7528fstream\u540c\u65f6\u8fdb\u884c\u6587\u4ef6\u7684\u8bfb\u5199"},{"location":"23-7-random-file-IO/#_2","text":"\u8981\u5220\u9664\u6587\u4ef6\uff0c\u53ea\u9700\u4f7f\u7528 remove() \u51fd\u6570\u3002 \u6b64\u5916\uff0c\u5982\u679c\u6d41\u662f\u6253\u5f00\u72b6\u6001\uff0c is_open() \u51fd\u6570\u4f1a\u8fd4\u56de true \uff0c\u5426\u5219\u8fd4\u56de false \u3002","title":"\u5176\u4ed6\u6709\u7528\u7684\u6587\u4ef6\u51fd\u6570"},{"location":"23-7-random-file-IO/#_3","text":"\u867d\u7136\u5c06\u53d8\u91cf\u5199\u5230\u6587\u4ef6\u662f\u5f88\u5bb9\u6613\u505a\u5230\u7684\uff0c\u4f46\u5728\u5904\u7406\u6307\u9488\u65f6\uff0c\u60c5\u51b5\u5c31\u53d8\u5f97\u66f4\u52a0\u590d\u6742\u4e86\u3002\u8bb0\u4f4f\uff0c\u6307\u9488\u53ea\u662f\u4fdd\u5b58\u5b83\u6240\u6307\u5411\u7684\u53d8\u91cf\u7684\u5730\u5740\u3002\u5c3d\u7ba1\u53ef\u4ee5\u5c06\u5730\u5740\u8bfb\u5199\u5230\u78c1\u76d8\uff0c\u4f46\u8fd9\u6837\u505a\u662f\u975e\u5e38\u5371\u9669\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u53d8\u91cf\u7684\u5730\u5740\u5728\u6bcf\u6b21\u6267\u884c\u65f6\u53ef\u80fd\u4e0d\u540c\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u5f53\u4f60\u5c06\u8be5\u5730\u5740\u5199\u5165\u78c1\u76d8\u65f6\uff0c\u53d8\u91cf\u53ef\u80fd\u4f4d\u4e8e\u5730\u5740 0x0012FF7C \uff0c\u4f46\u5f53\u4f60\u518d\u6b21\u8bfb\u53d6\u8be5\u5730\u5740\u65f6\uff0c\u5b83\u53ef\u80fd\u4e0d\u518d\u4f4d\u4e8e\u8be5\u5730\u5740\u4e86\uff01 \u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3a nValue \u7684\u6574\u6570\uff0c\u4f4d\u4e8e\u5730\u5740 0x0012FF7C \u3002\u4f60\u7ed9 nValue \u8d4b\u503c5\u3002\u540c\u65f6\uff0c\u58f0\u660e\u4e86\u4e00\u4e2a\u540d\u4e3a *pnValue \u7684\u6307\u9488\uff0c \u5b83\u6307\u5411nValue \u3002 pnValue \u4fdd\u5b58 nValue \u7684\u5730\u5740 0x0012FF7C \u3002\u6b64\u65f6\uff0c\u4f60\u5e0c\u671b\u5c06\u8fd9\u4e9b\u53d8\u91cf\u4fdd\u5b58\u5230\u6587\u4ef6\u4ee5\u5907\u5c06\u6765\u4f7f\u7528\uff0c\u56e0\u6b64\u4f60\u5c06\u503c5\u548c\u5730\u5740 0x0012FF7C \u5199\u5165\u78c1\u76d8\u3002 \u51e0\u5468\u540e\uff0c\u518d\u6b21\u8fd0\u884c\u8be5\u7a0b\u5e8f\u5e76\u4ece\u78c1\u76d8\u8bfb\u53d6\u8fd9\u4e9b\u503c\u3002\u5c06\u503c5\u8bfb\u5165\u53e6\u4e00\u4e2a\u540d\u4e3a nValue \u7684\u53d8\u91cf\uff0c\u8be5\u53d8\u91cf\u4f4d\u4e8e 0x0012FF78 \u3002\u4f60\u5c06\u5730\u5740 0x0012FF7C \u8bfb\u5165\u540d\u4e3a *pnValue \u7684\u6307\u9488\u4e2d\u3002\u56e0\u4e3a pnValue \u73b0\u5728\u6307\u5411 0x0012FF7C \uff0c\u800c nValue \u4f4d\u4e8e 0x0012FF78 \uff0c pnValue \u4e0d\u518d\u6307\u5411 nValue \uff0c\u8bd5\u56fe\u8bbf\u95ee pnValue \u5c06\u4f1a\u5e26\u6765\u9ebb\u70e6\u3002 \u6ce8\u610f \u4e0d\u8981\u5411\u6587\u4ef6\u5199\u5165\u5185\u5b58\u5730\u5740\u3002\u5f53\u4f60\u4ece\u78c1\u76d8\u8bfb\u56de\u8fd9\u4e9b\u503c\u65f6\uff0c\u6700\u521d\u4f4d\u4e8e\u8fd9\u4e9b\u5730\u5740\u7684\u53d8\u91cf\u53ef\u80fd\u4f4d\u4e8e\u4e0d\u540c\u7684\u5730\u5740\uff0c\u8fd9\u4e9b\u5730\u5740\u662f\u65e0\u6548\u7684\u3002","title":"\u5173\u4e8e\u5c06\u6307\u9488\u5199\u5165\u786c\u76d8\u7684\u8b66\u544a\u26a0\ufe0f"},{"location":"3-1-Syntax-and-semantic-errors/","tags":["Syntax"],"text":"\u8f6f\u4ef6\u9519\u8bef\u662f\u666e\u904d\u5b58\u5728\u7684\u3002\u5236\u9020\u5b83\u4eec\u5f88\u5bb9\u6613\uff0c\u4f46\u8981\u627e\u5230\u5b83\u4eec\u5374\u5f88\u96be\u3002\u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8C++\u7a0b\u5e8f\u95ee\u9898\u5b9a\u4f4d\u4e0e\u62cd\u9519\u7684\u76f8\u5173\u4e3b\u9898\uff0c\u5305\u62ec\u5b66\u4e60\u5982\u4f55\u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\uff0c\u5b83\u662fIDE\u7684\u4e00\u90e8\u5206\u3002 \u5c3d\u7ba1\u8c03\u8bd5\u5de5\u5177\u548c\u6280\u672f\u5e76\u4e0d\u662fC++\u6807\u51c6\u7684\u4e00\u90e8\u5206\uff0c\u4f46\u5b66\u4f1a\u5728\u6240\u7f16\u5199\u7684\u7a0b\u5e8f\u4e2d\u53d1\u73b0\u5e76\u5220\u9664\u9519\u8bef\u662f\u6210\u4e3a\u4e00\u540d\u6210\u529f\u7a0b\u5e8f\u5458\u6781\u4e3a\u91cd\u8981\u7684\u4e00\u90e8\u5206\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5c06\u82b1\u4e00\u70b9\u65f6\u95f4\u8ba8\u8bba\u8fd9\u4e9b\u4e3b\u9898\uff0c\u4ee5\u4fbf\u968f\u7740\u4f60\u7f16\u5199\u7684\u7a0b\u5e8f\u53d8\u5f97\u66f4\u52a0\u590d\u6742\uff0c\u4f60\u8bca\u65ad\u548c\u7ea0\u6b63\u95ee\u9898\u7684\u80fd\u529b\u4e5f\u4f1a\u4ee5\u5339\u914d\u7684\u901f\u5ea6\u63d0\u9ad8\u3002 \u5982\u679c\u4f60\u6709\u7528\u53e6\u4e00\u79cd\u7f16\u8bd1\u7f16\u7a0b\u8bed\u8a00\u8c03\u8bd5\u7a0b\u5e8f\u7684\u7ecf\u9a8c\uff0c\u90a3\u4e48\u5e94\u8be5\u5df2\u7ecf\u719f\u6089\u5176\u4e2d\u7684\u5927\u90e8\u5206\u5185\u5bb9\u3002 \u8bed\u6cd5\u9519\u8bef\u548c\u8bed\u4e49\u9519\u8bef \u7f16\u7a0b\u5e76\u4e0d\u7b80\u5355\uff0c\u66f4\u4f55\u51b5 C++\u662f\u4e00\u79cd\u6709\u70b9\u53e4\u602a\u7684\u8bed\u8a00\u3002\u628a\u8fd9\u4e24\u8005\u653e\u5728\u4e00\u8d77\uff0c\u5c31\u4f1a\u6709\u5f88\u591a\u51fa\u9519\u7684\u65b9\u5f0f\u3002\u9519\u8bef\u901a\u5e38\u5206\u4e3a\u4e24\u7c7b\uff1a\u8bed\u6cd5\u9519\u8bef\u548c\u8bed\u4e49\u9519\u8bef(\u903b\u8f91\u9519\u8bef)\u3002 \u5f53\u7f16\u5199\u7684\u8bed\u53e5\u5c5e\u4e8eC++\u65e0\u6548\u65e0\u6548\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f \u8bed\u6cd5\u9519\u8bef \u3002\u8fd9\u5305\u62ec\u7f3a\u5c11\u5206\u53f7\u3001\u4f7f\u7528\u672a\u58f0\u660e\u7684\u53d8\u91cf\u3001\u62ec\u53f7\u6216\u5927\u62ec\u53f7\u4e0d\u5339\u914d\u7b49\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u7a0b\u5e8f\u5305\u542b\u76f8\u5f53\u591a\u7684\u8bed\u6cd5\u9519\u8bef\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout < \"Hi there\" ; << x ; // invalid operator (<), extraneous semicolon, undeclared variable (x) return 0 // missing semicolon at end of statement } \u5e78\u8fd0\u7684\u662f\uff0c\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u6355\u83b7\u8bed\u6cd5\u9519\u8bef\u5e76\u751f\u6210\u8b66\u544a\u6216\u9519\u8bef\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u8bc6\u522b\u548c\u4fee\u590d\u8fd9\u4e9b\u95ee\u9898\u3002\u7136\u540e\u5c31\u662f\u91cd\u65b0\u7f16\u8bd1\u76f4\u5230\u6d88\u9664\u6240\u6709\u9519\u8bef\u7684\u95ee\u9898\u4e86\u3002 \u4e00\u65e6\u4f60\u7684\u7a0b\u5e8f\u7f16\u8bd1\u6b63\u786e\uff0c\u8ba9\u5b83\u5b9e\u9645\u4ea7\u751f\u4f60\u60f3\u8981\u7684\u7ed3\u679c\u53ef\u80fd\u662f\u68d8\u624b\u7684\u3002\u5f53\u8bed\u53e5\u5728\u8bed\u6cd5\u4e0a\u662f\u6709\u6548\u7684\uff0c\u4f46\u5e76\u6ca1\u6709\u6309\u7167\u7a0b\u5e8f\u5458\u7684\u610f\u56fe\u6267\u884c\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f \u8bed\u4e49\u9519\u8bef \u3002 \u6709\u65f6\u8fd9\u5c06\u5bfc\u81f4\u4f60\u7684\u7a0b\u5e8f\u5d29\u6e83\uff0c\u4f8b\u5982\u96640\u7684\u60c5\u51b5\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { int a { 10 }; int b { 0 }; std :: cout << a << \" / \" << b << \" = \" << a / b ; // division by 0 is undefined return 0 ; } \u4f46\u66f4\u591a\u65f6\u5019\u8bed\u4e49\u9519\u8bef\u53ea\u4f1a\u5bfc\u81f4\u7ed3\u679c\u9519\u8bef\u6216\u884c\u4e3a\u9519\u8bef\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { int x ; std :: cout << x ; // Use of uninitialized variable leads to undefined result return 0 ; } \u6216\u8005 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int add ( int x , int y ) { return x - y ; // function is supposed to add, but it doesn't } int main () { std :: cout << add ( 5 , 3 ); // should produce 8, but produces 2 return 0 ; } \u4ea6\u6216\u8005 1 2 3 4 5 6 7 8 #include int main () { return 0 ; // function returns here std :: cout << \"Hello, world!\" ; // so this never executes } \u73b0\u4ee3\u7f16\u8bd1\u5668\u5728\u68c0\u6d4b\u67d0\u4e9b\u7c7b\u578b\u7684\u5e38\u89c1\u8bed\u4e49\u9519\u8bef(\u4f8b\u5982\u4f7f\u7528\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf)\u65b9\u9762\u505a\u5f97\u8d8a\u6765\u8d8a\u597d\u3002\u7136\u800c\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u5c06\u65e0\u6cd5\u6355\u83b7\u5927\u591a\u6570\u8fd9\u7c7b\u95ee\u9898\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u88ab\u8bbe\u8ba1\u4e3a\u5f3a\u5236\u8bed\u6cd5\uff0c\u800c\u4e0d\u662f\u5f3a\u5236\u8bed\u4e49\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u9519\u8bef\u5f88\u5bb9\u6613\u53d1\u73b0\u3002\u4f46\u662f\u5728\u5927\u591a\u6570\u91cd\u8981\u7684\u7a0b\u5e8f\u4e2d\uff0c\u901a\u8fc7\u76ee\u6d4b\u4ee3\u7801\u4e0d\u5bb9\u6613\u53d1\u73b0\u8bed\u4e49\u9519\u8bef\u3002\u8fd9\u5c31\u662f\u8c03\u8bd5\u6280\u672f\u6d3e\u4e0a\u7528\u573a\u7684\u5730\u65b9\u3002","title":"3.1 - \u8bed\u6cd5\u548c\u8bed\u4e49\u9519\u8bef"},{"location":"3-1-Syntax-and-semantic-errors/#_1","text":"\u7f16\u7a0b\u5e76\u4e0d\u7b80\u5355\uff0c\u66f4\u4f55\u51b5 C++\u662f\u4e00\u79cd\u6709\u70b9\u53e4\u602a\u7684\u8bed\u8a00\u3002\u628a\u8fd9\u4e24\u8005\u653e\u5728\u4e00\u8d77\uff0c\u5c31\u4f1a\u6709\u5f88\u591a\u51fa\u9519\u7684\u65b9\u5f0f\u3002\u9519\u8bef\u901a\u5e38\u5206\u4e3a\u4e24\u7c7b\uff1a\u8bed\u6cd5\u9519\u8bef\u548c\u8bed\u4e49\u9519\u8bef(\u903b\u8f91\u9519\u8bef)\u3002 \u5f53\u7f16\u5199\u7684\u8bed\u53e5\u5c5e\u4e8eC++\u65e0\u6548\u65e0\u6548\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f \u8bed\u6cd5\u9519\u8bef \u3002\u8fd9\u5305\u62ec\u7f3a\u5c11\u5206\u53f7\u3001\u4f7f\u7528\u672a\u58f0\u660e\u7684\u53d8\u91cf\u3001\u62ec\u53f7\u6216\u5927\u62ec\u53f7\u4e0d\u5339\u914d\u7b49\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u7a0b\u5e8f\u5305\u542b\u76f8\u5f53\u591a\u7684\u8bed\u6cd5\u9519\u8bef\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout < \"Hi there\" ; << x ; // invalid operator (<), extraneous semicolon, undeclared variable (x) return 0 // missing semicolon at end of statement } \u5e78\u8fd0\u7684\u662f\uff0c\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u6355\u83b7\u8bed\u6cd5\u9519\u8bef\u5e76\u751f\u6210\u8b66\u544a\u6216\u9519\u8bef\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u8bc6\u522b\u548c\u4fee\u590d\u8fd9\u4e9b\u95ee\u9898\u3002\u7136\u540e\u5c31\u662f\u91cd\u65b0\u7f16\u8bd1\u76f4\u5230\u6d88\u9664\u6240\u6709\u9519\u8bef\u7684\u95ee\u9898\u4e86\u3002 \u4e00\u65e6\u4f60\u7684\u7a0b\u5e8f\u7f16\u8bd1\u6b63\u786e\uff0c\u8ba9\u5b83\u5b9e\u9645\u4ea7\u751f\u4f60\u60f3\u8981\u7684\u7ed3\u679c\u53ef\u80fd\u662f\u68d8\u624b\u7684\u3002\u5f53\u8bed\u53e5\u5728\u8bed\u6cd5\u4e0a\u662f\u6709\u6548\u7684\uff0c\u4f46\u5e76\u6ca1\u6709\u6309\u7167\u7a0b\u5e8f\u5458\u7684\u610f\u56fe\u6267\u884c\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f \u8bed\u4e49\u9519\u8bef \u3002 \u6709\u65f6\u8fd9\u5c06\u5bfc\u81f4\u4f60\u7684\u7a0b\u5e8f\u5d29\u6e83\uff0c\u4f8b\u5982\u96640\u7684\u60c5\u51b5\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { int a { 10 }; int b { 0 }; std :: cout << a << \" / \" << b << \" = \" << a / b ; // division by 0 is undefined return 0 ; } \u4f46\u66f4\u591a\u65f6\u5019\u8bed\u4e49\u9519\u8bef\u53ea\u4f1a\u5bfc\u81f4\u7ed3\u679c\u9519\u8bef\u6216\u884c\u4e3a\u9519\u8bef\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { int x ; std :: cout << x ; // Use of uninitialized variable leads to undefined result return 0 ; } \u6216\u8005 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int add ( int x , int y ) { return x - y ; // function is supposed to add, but it doesn't } int main () { std :: cout << add ( 5 , 3 ); // should produce 8, but produces 2 return 0 ; } \u4ea6\u6216\u8005 1 2 3 4 5 6 7 8 #include int main () { return 0 ; // function returns here std :: cout << \"Hello, world!\" ; // so this never executes } \u73b0\u4ee3\u7f16\u8bd1\u5668\u5728\u68c0\u6d4b\u67d0\u4e9b\u7c7b\u578b\u7684\u5e38\u89c1\u8bed\u4e49\u9519\u8bef(\u4f8b\u5982\u4f7f\u7528\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf)\u65b9\u9762\u505a\u5f97\u8d8a\u6765\u8d8a\u597d\u3002\u7136\u800c\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u5c06\u65e0\u6cd5\u6355\u83b7\u5927\u591a\u6570\u8fd9\u7c7b\u95ee\u9898\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u88ab\u8bbe\u8ba1\u4e3a\u5f3a\u5236\u8bed\u6cd5\uff0c\u800c\u4e0d\u662f\u5f3a\u5236\u8bed\u4e49\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u9519\u8bef\u5f88\u5bb9\u6613\u53d1\u73b0\u3002\u4f46\u662f\u5728\u5927\u591a\u6570\u91cd\u8981\u7684\u7a0b\u5e8f\u4e2d\uff0c\u901a\u8fc7\u76ee\u6d4b\u4ee3\u7801\u4e0d\u5bb9\u6613\u53d1\u73b0\u8bed\u4e49\u9519\u8bef\u3002\u8fd9\u5c31\u662f\u8c03\u8bd5\u6280\u672f\u6d3e\u4e0a\u7528\u573a\u7684\u5730\u65b9\u3002","title":"\u8bed\u6cd5\u9519\u8bef\u548c\u8bed\u4e49\u9519\u8bef"},{"location":"3-10-Finding-issues-before-they-become-problems/","text":"When you make a semantic error, that error may or may not be immediately noticeable when you run your program. An issue may lurk undetected in your code for a long time before newly introduced code or changed circumstances cause it to manifest as a program malfunction. The longer an error sits in the code base before it is found, the more likely it is to be hard to find, and something that may have been easy to fix originally turns into a debugging adventure that eats up time and energy. So what can we do about that? \u4e0d\u8981\u72af\u9519 Well, the best thing is to not make errors in the first place. Here\u2019s an incomplete list of things that can help avoid making errors: Follow best practices Don\u2019t program when tired Understand where the common pitfalls are in a language (all those things we warn you not to do) Keep your programs simple Don\u2019t let your functions get too long Prefer using the standard library to writing your own code, when possible Comment your code liberally \u4ee3\u7801\u91cd\u6784 As you add new capabilities to your programs (\u201cbehavioral changes\u201d), you will find that some of your functions grow in length. As functions get longer, they get both more complex and harder to understand. One way to address this is to break a single long function into multiple shorter functions. This process of making structural changes to your code without changing its behavior (typically in order to make your program more organized, modular, or performant) is called refactoring. So how long is too long for a function? A function that takes up one vertical screen worth of code is generally regarded as far too long -- if you have to scroll to read the whole function, the function\u2019s comprehensibility drops significantly. But the shorter the better -- functions less than ten lines are good. Functions that are less than five lines are even better. \u5173\u952e\u4fe1\u606f When making changes to your code, make behavioral changes OR structural changes, and then retest for correctness. Making behavioral and structural changes at the same time tends to lead to more errors as well as errors that are harder to find. \u9632\u5fa1\u5f0f\u7f16\u7a0b\u7b80\u4ecb Errors can be not only of your own making (e.g. incorrect logic), but also occur when your users use the application in a way that you did not anticipate. For example, if you ask the user to enter an integer, and they enter a letter instead, how does your program behave in such a case? Unless you anticipated this, and added some error handling for this case, probably not very well. Defensive programming is a practice whereby the programmer tries to anticipate all of the ways the software could be misused, either by end-users, or by other developers (including the programmer themselves) using the code. These misuses can often be detected and then mitigated (e.g. by asking a user who entered bad input to try again). We\u2019ll explore topics related to error handling in future lessons. Finding errors fast Since not making errors is difficult in large programs, the next best thing is to catch errors you do make quickly. The best way to do this is to program a little bit at a time, and then test your code and make sure it works. However, there are a few other techniques we can also use. \u6d4b\u8bd5\u51fd\u6570\u7b80\u4ecb One common way to help uncover issues with your program is to write testing functions to \u201cexercise\u201d the code you\u2019ve written. Here\u2019s a primitive attempt, more for illustrative purposes than anything: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int add ( int x , int y ) { return x + y ; } void testadd () { std :: cout << \"This function should print: 2 0 0 -2 \\n \" ; std :: cout << add ( 1 , 1 ) << ' ' ; std :: cout << add ( -1 , 1 ) << ' ' ; std :: cout << add ( 1 , -1 ) << ' ' ; std :: cout << add ( -1 , -1 ) << ' ' ; } int main () { testadd (); return 0 ; } COPY The testadd() function tests the add() function by calling it with different values. If all the values match our expectations, then we can be reasonably confident the function works. Even better, we can keep this function around, and run it any time we change function add to ensure we haven\u2019t accidentally broken it. This is a primitive form of unit testing, which is a software testing method by which small units of source code are tested to determine whether they are correct. As with logging frameworks, there are many 3rd party unit testing frameworks that can be used. It\u2019s also possible to write your own, though we\u2019ll need more language features at our disposal to do the topic justice. We\u2019ll come back to some of this in a future lesson. An introduction to constraints Constraints-based techniques involve the addition of some extra code (that can be compiled out in a non-debug build, if desired) to check that some set of assumptions or expectations are not violated. For example, if we were writing a function to calculate the factorial of a number, which expects a non-negative argument, the function could check to make sure the caller passed in a non-negative number before proceeding. If the caller passes in a negative number, then the function could immediately error out rather than producing some indeterminate result, helping ensure the problem is caught immediately. One common method of doing this is via assert and static_assert , which we cover in lesson 7.17 -- Assert and static_assert . Shotgunning for general issues Programmers tend to make certain kinds of common mistakes, and some of those mistakes can be discovered by programs trained to look for them. These programs, generally known as static analysis tools (sometimes informally called linters ) are programs that analyze your code to identify specific semantic issues (in this context, _static_means that these tools analyze the source code). The issues found by static analysis tools may or may not be the cause of any particular problem you are having, but may help point out fragile areas of code or issues that can be problematic in certain circumstances. You already have one static analysis tool at your disposal -- your compiler! In addition to ensuring your program is syntactically correct, most modern C++ compilers will do some light static analysis to identify some common problems. For example, many compilers will warn you if you try to use a variable that has not been initialized. If you haven\u2019t already, turning up your compiler warning and error levels (see lesson 0.11 -- Configuring your compiler: Warning and error levels ) can help surface these. Many static analysis tools exist , some of which can identify over 300 types of programming errors. And on large programs, this can surface tens or hundreds of potential issues. On our small academic programs, use of a static analysis tool is optional (but may help you find areas where your code is non-compliant with best practices). On larger non-academic programs, use of a static analysis tool is highly recommended. \u6700\u4f73\u5b9e\u8df5 Use a static analysis tool on your programs to help find areas where your code is non-compliant with best practices. For Visual Studio users Visual Studio 2019 comes with a built-in static analysis tool. You can access it via Build > Run Code Analysis on Solution (Alt+F11) .","title":"3.10 - \u5728\u95ee\u9898\u53d1\u751f\u524d\u53d1\u73b0\u95ee\u9898"},{"location":"3-10-Finding-issues-before-they-become-problems/#_1","text":"Well, the best thing is to not make errors in the first place. Here\u2019s an incomplete list of things that can help avoid making errors: Follow best practices Don\u2019t program when tired Understand where the common pitfalls are in a language (all those things we warn you not to do) Keep your programs simple Don\u2019t let your functions get too long Prefer using the standard library to writing your own code, when possible Comment your code liberally","title":"\u4e0d\u8981\u72af\u9519"},{"location":"3-10-Finding-issues-before-they-become-problems/#_2","text":"As you add new capabilities to your programs (\u201cbehavioral changes\u201d), you will find that some of your functions grow in length. As functions get longer, they get both more complex and harder to understand. One way to address this is to break a single long function into multiple shorter functions. This process of making structural changes to your code without changing its behavior (typically in order to make your program more organized, modular, or performant) is called refactoring. So how long is too long for a function? A function that takes up one vertical screen worth of code is generally regarded as far too long -- if you have to scroll to read the whole function, the function\u2019s comprehensibility drops significantly. But the shorter the better -- functions less than ten lines are good. Functions that are less than five lines are even better. \u5173\u952e\u4fe1\u606f When making changes to your code, make behavioral changes OR structural changes, and then retest for correctness. Making behavioral and structural changes at the same time tends to lead to more errors as well as errors that are harder to find.","title":"\u4ee3\u7801\u91cd\u6784"},{"location":"3-10-Finding-issues-before-they-become-problems/#_3","text":"Errors can be not only of your own making (e.g. incorrect logic), but also occur when your users use the application in a way that you did not anticipate. For example, if you ask the user to enter an integer, and they enter a letter instead, how does your program behave in such a case? Unless you anticipated this, and added some error handling for this case, probably not very well. Defensive programming is a practice whereby the programmer tries to anticipate all of the ways the software could be misused, either by end-users, or by other developers (including the programmer themselves) using the code. These misuses can often be detected and then mitigated (e.g. by asking a user who entered bad input to try again). We\u2019ll explore topics related to error handling in future lessons. Finding errors fast Since not making errors is difficult in large programs, the next best thing is to catch errors you do make quickly. The best way to do this is to program a little bit at a time, and then test your code and make sure it works. However, there are a few other techniques we can also use.","title":"\u9632\u5fa1\u5f0f\u7f16\u7a0b\u7b80\u4ecb"},{"location":"3-10-Finding-issues-before-they-become-problems/#_4","text":"One common way to help uncover issues with your program is to write testing functions to \u201cexercise\u201d the code you\u2019ve written. Here\u2019s a primitive attempt, more for illustrative purposes than anything: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int add ( int x , int y ) { return x + y ; } void testadd () { std :: cout << \"This function should print: 2 0 0 -2 \\n \" ; std :: cout << add ( 1 , 1 ) << ' ' ; std :: cout << add ( -1 , 1 ) << ' ' ; std :: cout << add ( 1 , -1 ) << ' ' ; std :: cout << add ( -1 , -1 ) << ' ' ; } int main () { testadd (); return 0 ; } COPY The testadd() function tests the add() function by calling it with different values. If all the values match our expectations, then we can be reasonably confident the function works. Even better, we can keep this function around, and run it any time we change function add to ensure we haven\u2019t accidentally broken it. This is a primitive form of unit testing, which is a software testing method by which small units of source code are tested to determine whether they are correct. As with logging frameworks, there are many 3rd party unit testing frameworks that can be used. It\u2019s also possible to write your own, though we\u2019ll need more language features at our disposal to do the topic justice. We\u2019ll come back to some of this in a future lesson.","title":"\u6d4b\u8bd5\u51fd\u6570\u7b80\u4ecb"},{"location":"3-10-Finding-issues-before-they-become-problems/#an-introduction-to-constraints","text":"Constraints-based techniques involve the addition of some extra code (that can be compiled out in a non-debug build, if desired) to check that some set of assumptions or expectations are not violated. For example, if we were writing a function to calculate the factorial of a number, which expects a non-negative argument, the function could check to make sure the caller passed in a non-negative number before proceeding. If the caller passes in a negative number, then the function could immediately error out rather than producing some indeterminate result, helping ensure the problem is caught immediately. One common method of doing this is via assert and static_assert , which we cover in lesson 7.17 -- Assert and static_assert .","title":"An introduction to constraints"},{"location":"3-10-Finding-issues-before-they-become-problems/#shotgunning-for-general-issues","text":"Programmers tend to make certain kinds of common mistakes, and some of those mistakes can be discovered by programs trained to look for them. These programs, generally known as static analysis tools (sometimes informally called linters ) are programs that analyze your code to identify specific semantic issues (in this context, _static_means that these tools analyze the source code). The issues found by static analysis tools may or may not be the cause of any particular problem you are having, but may help point out fragile areas of code or issues that can be problematic in certain circumstances. You already have one static analysis tool at your disposal -- your compiler! In addition to ensuring your program is syntactically correct, most modern C++ compilers will do some light static analysis to identify some common problems. For example, many compilers will warn you if you try to use a variable that has not been initialized. If you haven\u2019t already, turning up your compiler warning and error levels (see lesson 0.11 -- Configuring your compiler: Warning and error levels ) can help surface these. Many static analysis tools exist , some of which can identify over 300 types of programming errors. And on large programs, this can surface tens or hundreds of potential issues. On our small academic programs, use of a static analysis tool is optional (but may help you find areas where your code is non-compliant with best practices). On larger non-academic programs, use of a static analysis tool is highly recommended. \u6700\u4f73\u5b9e\u8df5 Use a static analysis tool on your programs to help find areas where your code is non-compliant with best practices. For Visual Studio users Visual Studio 2019 comes with a built-in static analysis tool. You can access it via Build > Run Code Analysis on Solution (Alt+F11) .","title":"Shotgunning for general issues"},{"location":"3-2-The-debugging-process/","tags":["debugging"],"text":"\u5047\u8bbe\u4f60\u7f16\u5199\u4e86\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5b83\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u2014\u2014\u4ee3\u7801\u7f16\u8bd1\u6ca1\u95ee\u9898\uff0c\u4f46\u662f\u8fd0\u884c\u7684\u7ed3\u679c\u4e0d\u6b63\u786e\u3002\u90a3\u4e48\u4f60\u4e00\u5b9a\u5728\u4ec0\u4e48\u5730\u65b9\u5199\u4e86\u6709\u8bed\u4e49\u9519\u8bef\u7684\u4ee3\u7801\u3002\u6b64\u65f6\u5e94\u8be5\u5982\u4f55\u5b9a\u4f4d\u95ee\u9898\u7684\u539f\u56e0\u5462\uff1f\u5982\u679c\u4f60\u4e00\u76f4\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\uff0c\u7f16\u5199\u5c11\u91cf\u4ee3\u7801\u5e76\u8fdb\u884c\u6d4b\u8bd5\uff0c\u90a3\u4e48\u53ef\u80fd\u5f88\u6e05\u695a\u9519\u8bef\u4f1a\u53d1\u751f\u5728\u54ea\u91cc\uff0c\u5426\u5219\u4f60\u53ef\u80fd\u5f88\u96be\u4e00\u4e0b\u627e\u5230\u95ee\u9898\u7684\u539f\u56e0\u3002 \u6240\u6709\u7684bug\u90fd\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u524d\u63d0\u2014\u2014\u5373\u4f60\u4ee5\u4e3a\u7684\u5e76\u4e0d\u662f\u4f60\u4ee5\u4e3a\u7684\u3002\u5b9e\u9645\u4e0a\uff0c\u627e\u51fa\u9519\u8bef\u5728\u54ea\u91cc\u662f\u5f88\u6709\u6311\u6218\u6027\u7684\u3002\u5728\u8fd9\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u6982\u8ff0\u8c03\u8bd5\u7a0b\u5e8f\u7684\u4e00\u822c\u8fc7\u7a0b\u3002 \u56e0\u4e3a\u6211\u4eec\u8fd8\u6ca1\u6709\u6d89\u53ca\u5230\u90a3\u4e48\u591a\u7684C++\u4e3b\u9898\uff0c\u672c\u7ae0\u4e2d\u7684\u793a\u4f8b\u7a0b\u5e8f\u5c06\u662f\u76f8\u5f53\u57fa\u7840\u7684\u3002\u8fd9\u53ef\u80fd\u4f1a\u8ba9\u6211\u4eec\u5728\u8fd9\u91cc\u5c55\u793a\u7684\u4e00\u4e9b\u6280\u672f\u663e\u5f97\u591a\u4f59\u3002\u4f46\u662f\uff0c\u8bf7\u8bb0\u4f4f\uff0c\u8fd9\u4e9b\u6280\u672f\u662f\u4e3a\u66f4\u5927\u3001\u66f4\u590d\u6742\u7684\u7a0b\u5e8f\u800c\u8bbe\u8ba1\u7684\uff0c\u5728\u8fd9\u6837\u7684\u4ee3\u7801\u4e2d(\u8fd9\u662f\u4f60\u6700\u9700\u8981\u5b83\u4eec\u7684\u5730\u65b9)\u4f1a\u66f4\u6709\u7528\u3002 \u4ee3\u7801\u6392\u9519\u7684\u4e00\u822c\u65b9\u6cd5 \u4e00\u65e6\u786e\u5b9a\u4e86\u95ee\u9898\uff0c\u4ee3\u7801\u6392\u9519\u901a\u5e38\u5305\u62ec\u4e94\u4e2a\u6b65\u9aa4: \u627e\u51fa\u95ee\u9898\u7684\u6839\u672c\u539f\u56e0(\u901a\u5e38\u662f\u65e0\u6cd5\u5de5\u4f5c\u7684\u4ee3\u7801\u884c)\uff1b \u786e\u4fdd\u4f60\u4e86\u89e3\u95ee\u9898\u53d1\u751f\u7684\u539f\u56e0\uff1b \u51b3\u5b9a\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1b \u4fee\u590d\u5f15\u8d77\u95ee\u9898\u7684\u95ee\u9898\uff1b \u91cd\u65b0\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u95ee\u9898\u5df2\u88ab\u4fee\u590d\uff0c\u4e14\u6ca1\u6709\u51fa\u73b0\u65b0\u95ee\u9898\u3002 \u8ba9\u6211\u4eec\u6253\u4e00\u4e2a\u73b0\u5b9e\u751f\u6d3b\u7684\u6bd4\u55bb\u3002\u5047\u8bbe\u6709\u4e00\u5929\u665a\u4e0a\uff0c\u4f60\u4ece\u51b0\u7bb1\u91cc\u7684\u5236\u51b0\u673a\u91cc\u53d6\u51b0\uff0c\u628a\u676f\u5b50\u653e\u5728\u51fa\u53e3\u5904\uff0c\u6309\u4e0b\u6309\u94ae\u540e\u58f0\u660e\u90fd\u6ca1\u6709\u51fa\u6765\u3002\u54e6\u54e6\uff0c\u51fa\u95ee\u9898\u4e86\uff01\u6b64\u65f6\u4f60\u4f1a\u600e\u4e48\u505a\uff1f\u4f60\u53ef\u80fd\u4f1a\u5f00\u59cb\u68c0\u67e5\u53ca\u5176\uff0c\u770b\u770b\u662f\u5426\u53ef\u4ee5\u786e\u5b9a\u95ee\u9898\u7684\u6839\u672c\u539f\u56e0\u3002 \u627e\u5230\u95ee\u9898\u7684\u6839\u56e0 \uff1a \u65e2\u7136\u4f60\u542c\u5230\u4e86\u5236\u51b0\u673a\u8bd5\u56fe\u9001\u51b0\u7684\u58f0\u97f3\uff0c\u90a3\u5f88\u53ef\u80fd\u4e0d\u662f\u9001\u51b0\u88c5\u7f6e\u672c\u8eab\u3002\u6240\u4ee5\u4f60\u6253\u5f00\u51b0\u7bb1\uff0c\u68c0\u67e5\u51b0\u76d8\u3002\u6ca1\u6709\u51b0\u3002\u8fd9\u662f\u95ee\u9898\u7684\u6839\u672c\u539f\u56e0\u5417\uff1f\u4e0d\uff0c\u8fd9\u662f\u4e00\u79cd\u73b0\u8c61\u3002\u5728\u8fdb\u4e00\u6b65\u68c0\u67e5\u4e4b\u540e\uff0c\u4f60\u786e\u5b9a\u5236\u51b0\u673a\u4f3c\u4e4e\u6ca1\u6709\u5728\u5236\u51b0\u3002\u662f\u5236\u51b0\u673a\u51fa\u4e86\u95ee\u9898\u8fd8\u662f\u522b\u7684\u4ec0\u4e48\u95ee\u9898\uff1f\u51b0\u7bb1\u4ecd\u7136\u662f\u51b7\u7684\uff0c\u6c34\u7ba1\u6ca1\u6709\u5835\u585e\uff0c\u5176\u4ed6\u4e00\u5207\u4f3c\u4e4e\u90fd\u5728\u5de5\u4f5c\uff0c\u6240\u4ee5\u4f60\u5f97\u51fa\u7ed3\u8bba\uff0c\u6839\u672c\u539f\u56e0\u662f\u5236\u51b0\u673a\u574f\u4e86\u3002 \u7406\u89e3\u95ee\u9898 \uff1a\u5728\u672c\u4f8b\u4e2d\uff0c\u8fd9\u5f88\u7b80\u5355\u3002\u574f\u4e86\u7684\u5236\u51b0\u673a\u5236\u4e0d\u51fa\u51b0\u3002 \u786e\u5b9a\u4fee\u590d\u65b9\u6cd5 \uff1a \u6b64\u65f6\uff0c\u4f60\u6709\u51e0\u4e2a\u89e3\u51b3\u65b9\u6848\uff1a\u4e00\u662f\u53ef\u4ee5\u7ed5\u8fc7\u8fd9\u4e2a\u95ee\u9898(\u4ece\u5546\u5e97\u8d2d\u4e70\u888b\u88c5\u51b0)\u3002\u6216\u8005\u4f60\u53ef\u4ee5\u8bd5\u7740\u8fdb\u4e00\u6b65\u8bca\u65ad\u5236\u51b0\u673a\uff0c\u770b\u770b\u662f\u5426\u6709\u96f6\u4ef6\u9700\u8981\u4fee\u590d\u3002\u4e09\u662f\u4f60\u53ef\u4ee5\u4e70\u4e00\u4e2a\u65b0\u7684\u5236\u51b0\u673a\uff0c\u628a\u5b83\u5b89\u88c5\u5728\u73b0\u6709\u7684\u90a3\u4e2a\u5730\u65b9\u3002\u4f60\u51b3\u5b9a\u4e70\u4e00\u4e2a\u65b0\u7684\u5236\u51b0\u673a\u3002 \u4fee\u590d\u95ee\u9898 \uff1a\u5236\u51b0\u673a\u4e00\u5230\uff0c\u4f60\u5c31\u628a\u5b83\u88c5\u4e0a\u3002 \u6d4b\u8bd5 \uff1a \u5728\u91cd\u65b0\u63a5\u901a\u7535\u6e90\u5e76\u7b49\u5f85\u4e00\u591c\u4e4b\u540e\uff0c\u4f60\u7684\u65b0\u5236\u51b0\u673a\u5f00\u59cb\u5236\u51b0\u3002\u6ca1\u6709\u53d1\u73b0\u65b0\u7684\u95ee\u9898\u3002 \u73b0\u5728\u8ba9\u6211\u4eec\u628a\u8fd9\u4e2a\u8fc7\u7a0b\u5e94\u7528\u5230\u4e0a\u8282\u8bfe\u7684\u7b80\u5355\u7a0b\u5e8f\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include // Adds two numbers int add ( int x , int y ) { return x - y ; // function is supposed to add, but it doesn't } int main () { std :: cout << add ( 5 , 3 ) << '\\n' ; // should produce 8, but produces 2 return 0 ; } \u8fd9\u6bb5\u4ee3\u7801\u7684\u552f\u4e00\u4f18\u70b9\u5c31\u662f\u9519\u7684\u5f88\u660e\u663e\uff1a\u9519\u8bef\u7684\u7b54\u6848\u901a\u8fc7\u7b2c11\u884c\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\u3002\u8fd9\u4e3a\u6211\u4eec\u7684\u8c03\u67e5\u63d0\u4f9b\u4e86\u4e00\u4e2a\u8d77\u70b9\u3002 \u627e\u5230\u95ee\u9898\u7684\u6839\u56e0 \uff1a\u5728\u7b2c11\u884c\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4f20\u9012\u7684\u53c2\u6570(5\u548c3)\u662f\u5b57\u9762\u91cf\uff0c\u56e0\u6b64\u6ca1\u6709\u51fa\u9519\u7684\u7a7a\u95f4\u3002\u7531\u4e8e\u51fd\u6570 add \u7684\u8f93\u5165\u662f\u6b63\u786e\u7684\uff0c\u4f46\u8f93\u51fa\u662f\u9519\u8bef\u7684\uff0c\u5f88\u660e\u663e\u51fd\u6570 add \u4e00\u5b9a\u8ba1\u7b97\u51fa\u4e86\u9519\u8bef\u7684\u503c\u3002\u51fd\u6570 add \u4e2d\u552f\u4e00\u7684\u8bed\u53e5\u662f return \u8bed\u53e5\uff0c\u5b83\u4e00\u5b9a\u662f\u7f6a\u9b41\u7978\u9996\u3002\u65b9\u5411\u6ca1\u6709\u9519\uff0c\u73b0\u5728\u6211\u4eec\u77e5\u9053\u6ce8\u610f\u529b\u5e94\u8be5\u96c6\u4e2d\u5728\u54ea\u91cc\uff0c\u968f\u540e\u4fbf\u53ef\u4ee5\u53d1\u73b0\u8fd9\u91cc\u5728\u505a\u51cf\u6cd5\u800c\u4e0d\u662f\u52a0\u6cd5\uff0c\u57fa\u672c\u4e0a\u4e00\u773c\u5c31\u53ef\u4ee5\u770b\u51fa\u6765\uff01 \u7406\u89e3\u95ee\u9898 \uff1a\u5728\u8fd9\u91cc\u6307\u9488\uff0c\u5f88\u663e\u7136\u4e3a\u4ec0\u4e48\u4f1a\u5f97\u5230\u9519\u8bef\u7684\u503c\u2014\u2014\u56e0\u4e3a\u4f7f\u7528\u4e86\u9519\u8bef\u7684\u64cd\u4f5c\u7b26\u3002 \u786e\u5b9a\u4fee\u590d\u65b9\u6cd5 \uff1a\u5c06\u51cf\u53f7\u6539\u6210\u52a0\u53f7\u5373\u53ef\u3002 \u4fee\u590d\u95ee\u9898 \uff1a\u5728\u7a0b\u5e8f\u4e2d\u5c06\u51cf\u53f7\u6539\u4e3a\u52a0\u53f7\uff0c\u786e\u4fdd\u7a0b\u5e8f\u53ef\u4ee5\u7f16\u8bd1\u3002 \u6d4b\u8bd5 \uff1a\u5728\u5b9e\u73b0\u66f4\u6539\u4e4b\u540e\uff0c\u91cd\u65b0\u8fd0\u884c\u7a0b\u5e8f\uff0c\u786e\u5b9a\u5f97\u5230\u4e86\u6b63\u786e\u503c\uff1a8\u3002\u5bf9\u4e8e\u8fd9\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\uff0c\u8fd9\u5c31\u662f\u6240\u6709\u9700\u8981\u7684\u6d4b\u8bd5\u3002 \u8fd9\u4e2a\u793a\u4f8b\u5f88\u7b80\u5355\uff0c\u4f46\u8bf4\u660e\u4e86\u5bf9\u4efb\u4f55\u7a0b\u5e8f\u8fdb\u884c\u6392\u9519(debug)\u65f6\u5e94\u8be5\u7ecf\u5386\u7684\u57fa\u672c\u8fc7\u7a0b\u3002","title":"3.2 - \u4ee3\u7801\u8c03\u8bd5\u6b65\u9aa4"},{"location":"3-2-The-debugging-process/#_1","text":"\u4e00\u65e6\u786e\u5b9a\u4e86\u95ee\u9898\uff0c\u4ee3\u7801\u6392\u9519\u901a\u5e38\u5305\u62ec\u4e94\u4e2a\u6b65\u9aa4: \u627e\u51fa\u95ee\u9898\u7684\u6839\u672c\u539f\u56e0(\u901a\u5e38\u662f\u65e0\u6cd5\u5de5\u4f5c\u7684\u4ee3\u7801\u884c)\uff1b \u786e\u4fdd\u4f60\u4e86\u89e3\u95ee\u9898\u53d1\u751f\u7684\u539f\u56e0\uff1b \u51b3\u5b9a\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1b \u4fee\u590d\u5f15\u8d77\u95ee\u9898\u7684\u95ee\u9898\uff1b \u91cd\u65b0\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u95ee\u9898\u5df2\u88ab\u4fee\u590d\uff0c\u4e14\u6ca1\u6709\u51fa\u73b0\u65b0\u95ee\u9898\u3002 \u8ba9\u6211\u4eec\u6253\u4e00\u4e2a\u73b0\u5b9e\u751f\u6d3b\u7684\u6bd4\u55bb\u3002\u5047\u8bbe\u6709\u4e00\u5929\u665a\u4e0a\uff0c\u4f60\u4ece\u51b0\u7bb1\u91cc\u7684\u5236\u51b0\u673a\u91cc\u53d6\u51b0\uff0c\u628a\u676f\u5b50\u653e\u5728\u51fa\u53e3\u5904\uff0c\u6309\u4e0b\u6309\u94ae\u540e\u58f0\u660e\u90fd\u6ca1\u6709\u51fa\u6765\u3002\u54e6\u54e6\uff0c\u51fa\u95ee\u9898\u4e86\uff01\u6b64\u65f6\u4f60\u4f1a\u600e\u4e48\u505a\uff1f\u4f60\u53ef\u80fd\u4f1a\u5f00\u59cb\u68c0\u67e5\u53ca\u5176\uff0c\u770b\u770b\u662f\u5426\u53ef\u4ee5\u786e\u5b9a\u95ee\u9898\u7684\u6839\u672c\u539f\u56e0\u3002 \u627e\u5230\u95ee\u9898\u7684\u6839\u56e0 \uff1a \u65e2\u7136\u4f60\u542c\u5230\u4e86\u5236\u51b0\u673a\u8bd5\u56fe\u9001\u51b0\u7684\u58f0\u97f3\uff0c\u90a3\u5f88\u53ef\u80fd\u4e0d\u662f\u9001\u51b0\u88c5\u7f6e\u672c\u8eab\u3002\u6240\u4ee5\u4f60\u6253\u5f00\u51b0\u7bb1\uff0c\u68c0\u67e5\u51b0\u76d8\u3002\u6ca1\u6709\u51b0\u3002\u8fd9\u662f\u95ee\u9898\u7684\u6839\u672c\u539f\u56e0\u5417\uff1f\u4e0d\uff0c\u8fd9\u662f\u4e00\u79cd\u73b0\u8c61\u3002\u5728\u8fdb\u4e00\u6b65\u68c0\u67e5\u4e4b\u540e\uff0c\u4f60\u786e\u5b9a\u5236\u51b0\u673a\u4f3c\u4e4e\u6ca1\u6709\u5728\u5236\u51b0\u3002\u662f\u5236\u51b0\u673a\u51fa\u4e86\u95ee\u9898\u8fd8\u662f\u522b\u7684\u4ec0\u4e48\u95ee\u9898\uff1f\u51b0\u7bb1\u4ecd\u7136\u662f\u51b7\u7684\uff0c\u6c34\u7ba1\u6ca1\u6709\u5835\u585e\uff0c\u5176\u4ed6\u4e00\u5207\u4f3c\u4e4e\u90fd\u5728\u5de5\u4f5c\uff0c\u6240\u4ee5\u4f60\u5f97\u51fa\u7ed3\u8bba\uff0c\u6839\u672c\u539f\u56e0\u662f\u5236\u51b0\u673a\u574f\u4e86\u3002 \u7406\u89e3\u95ee\u9898 \uff1a\u5728\u672c\u4f8b\u4e2d\uff0c\u8fd9\u5f88\u7b80\u5355\u3002\u574f\u4e86\u7684\u5236\u51b0\u673a\u5236\u4e0d\u51fa\u51b0\u3002 \u786e\u5b9a\u4fee\u590d\u65b9\u6cd5 \uff1a \u6b64\u65f6\uff0c\u4f60\u6709\u51e0\u4e2a\u89e3\u51b3\u65b9\u6848\uff1a\u4e00\u662f\u53ef\u4ee5\u7ed5\u8fc7\u8fd9\u4e2a\u95ee\u9898(\u4ece\u5546\u5e97\u8d2d\u4e70\u888b\u88c5\u51b0)\u3002\u6216\u8005\u4f60\u53ef\u4ee5\u8bd5\u7740\u8fdb\u4e00\u6b65\u8bca\u65ad\u5236\u51b0\u673a\uff0c\u770b\u770b\u662f\u5426\u6709\u96f6\u4ef6\u9700\u8981\u4fee\u590d\u3002\u4e09\u662f\u4f60\u53ef\u4ee5\u4e70\u4e00\u4e2a\u65b0\u7684\u5236\u51b0\u673a\uff0c\u628a\u5b83\u5b89\u88c5\u5728\u73b0\u6709\u7684\u90a3\u4e2a\u5730\u65b9\u3002\u4f60\u51b3\u5b9a\u4e70\u4e00\u4e2a\u65b0\u7684\u5236\u51b0\u673a\u3002 \u4fee\u590d\u95ee\u9898 \uff1a\u5236\u51b0\u673a\u4e00\u5230\uff0c\u4f60\u5c31\u628a\u5b83\u88c5\u4e0a\u3002 \u6d4b\u8bd5 \uff1a \u5728\u91cd\u65b0\u63a5\u901a\u7535\u6e90\u5e76\u7b49\u5f85\u4e00\u591c\u4e4b\u540e\uff0c\u4f60\u7684\u65b0\u5236\u51b0\u673a\u5f00\u59cb\u5236\u51b0\u3002\u6ca1\u6709\u53d1\u73b0\u65b0\u7684\u95ee\u9898\u3002 \u73b0\u5728\u8ba9\u6211\u4eec\u628a\u8fd9\u4e2a\u8fc7\u7a0b\u5e94\u7528\u5230\u4e0a\u8282\u8bfe\u7684\u7b80\u5355\u7a0b\u5e8f\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include // Adds two numbers int add ( int x , int y ) { return x - y ; // function is supposed to add, but it doesn't } int main () { std :: cout << add ( 5 , 3 ) << '\\n' ; // should produce 8, but produces 2 return 0 ; } \u8fd9\u6bb5\u4ee3\u7801\u7684\u552f\u4e00\u4f18\u70b9\u5c31\u662f\u9519\u7684\u5f88\u660e\u663e\uff1a\u9519\u8bef\u7684\u7b54\u6848\u901a\u8fc7\u7b2c11\u884c\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\u3002\u8fd9\u4e3a\u6211\u4eec\u7684\u8c03\u67e5\u63d0\u4f9b\u4e86\u4e00\u4e2a\u8d77\u70b9\u3002 \u627e\u5230\u95ee\u9898\u7684\u6839\u56e0 \uff1a\u5728\u7b2c11\u884c\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4f20\u9012\u7684\u53c2\u6570(5\u548c3)\u662f\u5b57\u9762\u91cf\uff0c\u56e0\u6b64\u6ca1\u6709\u51fa\u9519\u7684\u7a7a\u95f4\u3002\u7531\u4e8e\u51fd\u6570 add \u7684\u8f93\u5165\u662f\u6b63\u786e\u7684\uff0c\u4f46\u8f93\u51fa\u662f\u9519\u8bef\u7684\uff0c\u5f88\u660e\u663e\u51fd\u6570 add \u4e00\u5b9a\u8ba1\u7b97\u51fa\u4e86\u9519\u8bef\u7684\u503c\u3002\u51fd\u6570 add \u4e2d\u552f\u4e00\u7684\u8bed\u53e5\u662f return \u8bed\u53e5\uff0c\u5b83\u4e00\u5b9a\u662f\u7f6a\u9b41\u7978\u9996\u3002\u65b9\u5411\u6ca1\u6709\u9519\uff0c\u73b0\u5728\u6211\u4eec\u77e5\u9053\u6ce8\u610f\u529b\u5e94\u8be5\u96c6\u4e2d\u5728\u54ea\u91cc\uff0c\u968f\u540e\u4fbf\u53ef\u4ee5\u53d1\u73b0\u8fd9\u91cc\u5728\u505a\u51cf\u6cd5\u800c\u4e0d\u662f\u52a0\u6cd5\uff0c\u57fa\u672c\u4e0a\u4e00\u773c\u5c31\u53ef\u4ee5\u770b\u51fa\u6765\uff01 \u7406\u89e3\u95ee\u9898 \uff1a\u5728\u8fd9\u91cc\u6307\u9488\uff0c\u5f88\u663e\u7136\u4e3a\u4ec0\u4e48\u4f1a\u5f97\u5230\u9519\u8bef\u7684\u503c\u2014\u2014\u56e0\u4e3a\u4f7f\u7528\u4e86\u9519\u8bef\u7684\u64cd\u4f5c\u7b26\u3002 \u786e\u5b9a\u4fee\u590d\u65b9\u6cd5 \uff1a\u5c06\u51cf\u53f7\u6539\u6210\u52a0\u53f7\u5373\u53ef\u3002 \u4fee\u590d\u95ee\u9898 \uff1a\u5728\u7a0b\u5e8f\u4e2d\u5c06\u51cf\u53f7\u6539\u4e3a\u52a0\u53f7\uff0c\u786e\u4fdd\u7a0b\u5e8f\u53ef\u4ee5\u7f16\u8bd1\u3002 \u6d4b\u8bd5 \uff1a\u5728\u5b9e\u73b0\u66f4\u6539\u4e4b\u540e\uff0c\u91cd\u65b0\u8fd0\u884c\u7a0b\u5e8f\uff0c\u786e\u5b9a\u5f97\u5230\u4e86\u6b63\u786e\u503c\uff1a8\u3002\u5bf9\u4e8e\u8fd9\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\uff0c\u8fd9\u5c31\u662f\u6240\u6709\u9700\u8981\u7684\u6d4b\u8bd5\u3002 \u8fd9\u4e2a\u793a\u4f8b\u5f88\u7b80\u5355\uff0c\u4f46\u8bf4\u660e\u4e86\u5bf9\u4efb\u4f55\u7a0b\u5e8f\u8fdb\u884c\u6392\u9519(debug)\u65f6\u5e94\u8be5\u7ecf\u5386\u7684\u57fa\u672c\u8fc7\u7a0b\u3002","title":"\u4ee3\u7801\u6392\u9519\u7684\u4e00\u822c\u65b9\u6cd5"},{"location":"3-3-A-strategy-for-debugging/","tags":["debugging"],"text":"\u5728\u8c03\u8bd5\u7a0b\u5e8f\u65f6\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f60\u7684\u7edd\u5927\u591a\u6570\u65f6\u95f4\u5c06\u82b1\u5728\u8bd5\u56fe\u627e\u5230\u9519\u8bef\u7684\u5b9e\u9645\u4f4d\u7f6e\u4e0a\u3002\u4e00\u65e6\u53d1\u73b0\u95ee\u9898\uff0c\u5269\u4e0b\u7684\u6b65\u9aa4(\u4fee\u590d\u95ee\u9898\u548c\u9a8c\u8bc1\u95ee\u9898\u5df2\u4fee\u590d)\u76f8\u6bd4\u8d77\u6765\u901a\u5e38\u662f\u5fae\u4e0d\u8db3\u9053\u7684\u3002 \u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u5f00\u59cb\u63a2\u7d22\u5982\u4f55\u67e5\u627e\u9519\u8bef\u3002 \u901a\u8fc7\u4ee3\u7801\u68c0\u89c6\u627e\u5230\u95ee\u9898 \u5047\u8bbe\u60a8\u6ce8\u610f\u5230\u4e00\u4e2a\u95ee\u9898\uff0c\u60a8\u60f3\u8981\u8ffd\u8e2a\u8fd9\u4e2a\u7279\u5b9a\u95ee\u9898\u7684\u539f\u56e0\u3002\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b(\u7279\u522b\u662f\u5728\u8f83\u5c0f\u7684\u7a0b\u5e8f\u4e2d)\uff0c\u6211\u4eec\u53ef\u4ee5\u5feb\u901f\u5730\u5b9a\u4f4d\u95ee\u9898\u6240\u5728\u7684\u4f4d\u7f6e\u3002 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\u7247\u6bb5\uff1a 1 2 3 4 5 6 7 8 int main () { getNames (); // ask user to enter a bunch of names sortNames (); // sort them in alphabetical order printNames (); // print the sorted list of names return 0 ; } \u5982\u679c\u4f60\u671f\u671b\u8fd9\u4e2a\u7a0b\u5e8f\u6309\u5b57\u6bcd\u987a\u5e8f\u6253\u5370\u540d\u79f0\uff0c\u4f46\u5b83\u6253\u5370\u7684\u7ed3\u679c\u5374\u662f\u76f8\u53cd\u7684\uff0c\u90a3\u4e48\u95ee\u9898\u53ef\u80fd\u51fa\u5728 sortNames \u51fd\u6570\u4e0a\u3002\u5bf9\u4e8e\u8fd9\u79cd\u53ef\u4ee5\u5c06\u95ee\u9898\u7f29\u5c0f\u5230\u7279\u5b9a\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u53ea\u9700\u67e5\u770b\u4ee3\u7801\u5c31\u53ef\u4ee5\u53d1\u73b0\u95ee\u9898\u3002 \u7136\u800c\uff0c\u968f\u7740\u7a0b\u5e8f\u53d8\u5f97\u8d8a\u6765\u8d8a\u590d\u6742\uff0c\u901a\u8fc7\u4ee3\u7801\u68c0\u67e5\u53d1\u73b0\u95ee\u9898\u4e5f\u53d8\u5f97\u8d8a\u6765\u8d8a\u590d\u6742\u3002 \u9996\u5148\uff0c\u6709\u5f88\u591a\u4ee3\u7801\u8981\u770b\u3002\u5728\u4e00\u4e2a\u6570\u5343\u884c\u957f\u7684\u7a0b\u5e8f\u4e2d\uff0c\u9010\u884c\u8d70\u8bfb\u4ee3\u7801\u4f1a\u82b1\u8d39\u5f88\u957f\u65f6\u95f4(\u800c\u4e14\u975e\u5e38\u65e0\u804a)\u3002\u5176\u6b21\uff0c\u4ee3\u7801\u672c\u8eab\u5f80\u5f80\u66f4\u590d\u6742\uff0c\u5f88\u591a\u5730\u65b9\u90fd\u53ef\u80fd\u51fa\u9519\u3002\u7b2c\u4e09\uff0c\u4ee3\u7801\u7684\u884c\u4e3a\u53ef\u80fd\u4e0d\u80fd\u63d0\u9ad8\u592a\u591a\u7ebf\u7d22\u3002\u5982\u679c\u4f60\u7f16\u5199\u4e86\u4e00\u4e2a\u8f93\u51fa\u80a1\u7968\u63a8\u8350\u7684\u7a0b\u5e8f\uff0c\u800c\u5b83\u5b9e\u9645\u4e0a\u4ec0\u4e48\u4e5f\u6ca1\u6709\u8f93\u51fa\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u80fd\u5c31\u4e0d\u77e5\u9053\u4ece\u54ea\u91cc\u5f00\u59cb\u67e5\u627e\u95ee\u9898\u4e86\u3002 \u6700\u540e\uff0c\u9519\u8bef\u53ef\u80fd\u662f\u7531\u9519\u8bef\u7684\u5047\u8bbe\u5f15\u8d77\u7684\u3002\u51e0\u4e4e\u4e0d\u53ef\u80fd\u4ece\u89c6\u89c9\u4e0a\u53d1\u73b0\u7531\u9519\u8bef\u5047\u8bbe\u5f15\u8d77\u7684\u9519\u8bef\uff0c\u56e0\u4e3a\u5728\u68c0\u67e5\u4ee3\u7801\u65f6\uff0c\u6211\u4eec\u5f80\u5f80\u503e\u5411\u4e8e\u4f1a\u505a\u51fa\u540c\u6837\u7684\u9519\u8bef\u5047\u8bbe\uff0c\u5bfc\u81f4\u6ca1\u6709\u6ce8\u610f\u5230\u9519\u8bef\u3002\u6240\u4ee5\uff0c\u5982\u679c\u4e00\u4e2a\u7a0b\u5e8f\u65e0\u6cd5\u901a\u8fc7\u4ee3\u7801\u68c0\u89c6\u53d1\u73b0\u95ee\u9898\uff0c\u90a3\u5e94\u8be5\u5982\u4f55\u5b9a\u4f4d\u5462\uff1f \u901a\u8fc7\u8fd0\u884c\u7a0b\u5e8f\u627e\u5230\u95ee\u9898 \u6240\u5e78\uff0c\u5982\u679c\u4e0d\u80fd\u901a\u8fc7\u4ee3\u7801\u68c0\u89c6\u53d1\u73b0\u95ee\u9898\uff0c\u8fd8\u53ef\u4ee5\u91c7\u7528\u53e6\u4e00\u79cd\u65b9\u6cd5\uff1a\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u89c2\u5bdf\u5b83\u7684\u884c\u4e3a\uff0c\u5e76\u5c1d\u8bd5\u4ece\u4e2d\u8bca\u65ad\u95ee\u9898\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6982\u62ec\u4e3a: \u627e\u5230\u590d\u73b0\u95ee\u9898\u7684\u6761\u4ef6\uff1b \u8fd0\u884c\u7a0b\u5e8f\uff0c\u83b7\u53d6\u4fe1\u606f\uff0c\u9010\u6b65\u903c\u8fd1\"\u771f\u50cf\"\uff1b \u91cd\u590d\u4e0a\u8ff0\u6b65\u9aa4\u76f4\u5230\u627e\u5230\u95ee\u9898\u6240\u5728\u3002 \u5728\u672c\u7ae0\u7684\u5176\u4f59\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5b9e\u65bd\u8fd9\u79cd\u65b9\u6cd5\u7684\u6280\u672f\u3002 \u590d\u73b0\u95ee\u9898 \u53d1\u73b0\u95ee\u9898\u7684\u7b2c\u4e00\u6b65\u4e5f\u662f\u6700\u91cd\u8981\u7684\u4e00\u6b65\u662f\u80fd\u591f\u91cd\u73b0\u95ee\u9898\uff0c\u539f\u56e0\u5f88\u7b80\u5355\uff1a\u5982\u679c\u4f60\u4e0d\u80fd\u518d\u6b21\u89c2\u5bdf\u5230\u95ee\u9898\u7684\u8bdd\uff0c\u8981\u5982\u4f55\u624d\u80fd\u5b9a\u4f4d\u5b83\u5462\uff1f \u56de\u5230\u5236\u51b0\u673a\u7684\u7c7b\u6bd4\u2014\u2014\u5047\u8bbe\u6709\u4e00\u5929\u4f60\u7684\u670b\u53cb\u544a\u8bc9\u4f60\uff0c\u4f60\u7684\u5236\u51b0\u673a\u574f\u4e86\u3002\u4f60\u53bb\u68c0\u67e5\u7684\u65f6\u5019\u53d1\u73b0\u5b83\u5de5\u4f5c\u5f97\u5f88\u597d\u3002\u4f60\u5e94\u8be5\u5982\u4f55\u8bca\u65ad\u8fd9\u4e2a\u95ee\u9898\uff1f\u8fd9\u4e2a\u65f6\u5019\u5c31\u6bd4\u8f83\u68d8\u624b\u4e86\u3002\u7136\u800c\uff0c\u5982\u679c\u4f60\u80fd\u591f\u518d\u6b21\u8ba9\u5236\u51b0\u673a\u8fdb\u5165\u5230\u4e0d\u80fd\u5de5\u4f5c\u7684\u72b6\u6001\uff0c\u90a3\u4e48\u624d\u53ef\u4ee5\u5f00\u59cb\u5b9a\u4f4d\u95ee\u9898\u3002 \u5982\u679c\u4e00\u4e2a\u8f6f\u4ef6bug\u5f88\u660e\u663e\u7684(\u4f8b\u5982\uff0c\u7a0b\u5e8f\u5728\u6bcf\u6b21\u8fd0\u884c\u65f6\u90fd\u5728\u76f8\u540c\u7684\u5730\u65b9\u5d29\u6e83)\uff0c\u90a3\u4e48\u91cd\u73b0\u8fd9\u4e2a\u95ee\u9898\u5c31\u76f8\u5f53\u7b80\u5355\u4e86\u3002\u7136\u800c\uff0c\u590d\u73b0\u95ee\u9898\u6709\u65f6\u5019\u662f\u5f88\u56f0\u96be\u7684\u3002\u4f8b\u5982\u8be5\u95ee\u9898\u53ef\u80fd\u53ea\u53d1\u751f\u5728\u67d0\u4e9b\u8bbe\u5907\u4e0a\uff0c\u6216\u53ea\u5728\u7279\u5b9a\u60c5\u51b5\u4e0b(\u4f8b\u5982\uff0c\u5f53\u7528\u6237\u8f93\u5165\u67d0\u4e9b\u8f93\u5165\u65f6)\u624d\u51fa\u73b0\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8bbe\u8ba1\u4e00\u7ec4\u590d\u73b0\u95ee\u9898\u7684\u6b65\u9aa4\u4f1a\u5f88\u6709\u5e2e\u52a9\u3002\u590d\u73b0\u6b65\u9aa4\u662f\u4e00\u4e2a\u6e05\u6670\u4e14\u7cbe\u786e\u7684\u5206\u6b65\u6267\u884c\u5217\u8868\uff0c\u4f60\u53ef\u4ee5\u9075\u5faa\u8fd9\u4e9b\u6b65\u9aa4\u4f7f\u95ee\u9898\u4ee5\u66f4\u5927\u7684\u6982\u7387\u518d\u6b21\u51fa\u73b0\u3002\u6211\u4eec\u7684\u76ee\u6807\u662f\u5c31\u80fd\u591f\u5c3d\u53ef\u80fd\u5730\u4f7f\u95ee\u9898\u518d\u6b21\u51fa\u73b0\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u53cd\u590d\u8fd0\u884c\u7a0b\u5e8f\uff0c\u5e76\u67e5\u627e\u7ebf\u7d22\u6765\u786e\u5b9a\u662f\u4ec0\u4e48\u539f\u56e0\u5bfc\u81f4\u4e86\u95ee\u9898\u3002\u5982\u679c\u95ee\u9898\u53ef\u4ee5100%\u7684\u91cd\u73b0\uff0c\u90a3\u81ea\u7136\u662f\u6700\u7406\u60f3\u7684\u72b6\u6001\uff0c\u4f46\u4f4e\u4e8e100%\u901a\u5e38\u4e5f\u662f\u53ef\u4ee5\u7684\u3002\u4e00\u4e2a\u53ea\u670950%\u51e0\u7387\u89e6\u53d1\u7684\u95ee\u9898\uff0c\u610f\u5473\u7740\u6211\u4eec\u9700\u8981\u82b1\u8d39\u4e24\u500d\u7684\u65f6\u95f4\u6765\u8bca\u65ad\u95ee\u9898\uff0c\u56e0\u4e3a\u6709\u4e00\u534a\u7684\u65f6\u95f4\u7a0b\u5e8f\u4e0d\u4f1a\u663e\u793a\u95ee\u9898\uff0c\u56e0\u6b64\u4e0d\u4f1a\u63d0\u4f9b\u4efb\u4f55\u6709\u7528\u7684\u8bca\u65ad\u4fe1\u606f\u3002 \u5173\u6ce8\u95ee\u9898 \u4e00\u65e6\u6211\u4eec\u80fd\u591f\u6bd4\u8f83\u7a33\u5b9a\u5730\u590d\u73b0\u95ee\u9898\uff0c\u63a5\u4e0b\u6765\u5c31\u662f\u627e\u5230\u5bfc\u81f4\u95ee\u9898\u7684\u4ee3\u7801\u3002\u6839\u636e\u95ee\u9898\u7684\u6027\u8d28\u4e0d\u540c\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u53ef\u80fd\u5f88\u7b80\u5355\uff0c\u4e5f\u53ef\u80fd\u5f88\u56f0\u96be\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u6211\u4eec\u4e0d\u592a\u6e05\u695a\u95ee\u9898\u5230\u5e95\u5728\u54ea\u91cc\u3002\u6211\u4eec\u600e\u4e48\u627e\u5230\u5b83? \u8fd9\u91cc\u53ef\u4ee5\u505a\u4e2a\u7c7b\u6bd4\u3002\u6211\u4eec\u6765\u73a9\u4e2a\u731c\u6570\u5b57\u6e38\u620f\uff0c\u4f60\u9700\u8981\u731c\u4e00\u4e2a1\u523010\u4e4b\u95f4\u7684\u6570\u5b57\uff0c\u6bcf\u731c\u4e00\u6b21\uff0c\u6211\u5c31\u4f1a\u544a\u8bc9\u4f60\u662f\u592a\u9ad8\u3001\u592a\u4f4e\u8fd8\u662f\u6b63\u786e\uff1a 1 2 3 4 5 6 7 8 You: 5 Me: Too low You: 8 Me: Too high You: 6 Me: Too low You: 7 Me: Correct \u5728\u4e0a\u9762\u7684\u6e38\u620f\u4e2d\uff0c\u4f60\u4e0d\u9700\u8981\u731c\u6bcf\u4e00\u4e2a\u6570\u5b57\u6765\u627e\u5230\u7b54\u6848\u3002\u901a\u8fc7\u731c\u6d4b\u548c\u8003\u8651\u4ece\u6bcf\u6b21\u731c\u6d4b\u4e2d\u5b66\u5230\u7684\u4fe1\u606f\u7684\u8fc7\u7a0b\uff0c\u4f60\u53ef\u80fd\u53ea\u9700\u51e0\u6b21\u731c\u6d4b\u5c31\u53ef\u4ee5\u201c\u9501\u5b9a\u201d\u6b63\u786e\u7684\u6570\u5b57(\u5982\u679c\u4f60\u4f7f\u7528\u6700\u4f18\u7b56\u7565\uff0c\u90a3\u4e48\u603b\u662f\u53ef\u4ee5\u57284\u6b21\u6216\u66f4\u5c11\u7684\u731c\u6d4b\u4e2d\u627e\u5230\u76ee\u6807\u6570\u5b57)\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u6765\u8c03\u8bd5\u7a0b\u5e8f\u3002\u5728\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u4e0d\u77e5\u9053bug\u5728\u54ea\u91cc\u3002\u7136\u800c\uff0c\u6211\u4eec\u77e5\u9053\u95ee\u9898\u4e00\u5b9a\u662f\u5728\u7a0b\u5e8f\u5f00\u59cb\u5230\u7a0b\u5e8f\u663e\u793a\u51fa\u6211\u4eec\u53ef\u4ee5\u89c2\u5bdf\u5230\u7684\u7b2c\u4e00\u4e2a\u9519\u8bef\u75c7\u72b6\u4e4b\u95f4\u6267\u884c\u7684\u4ee3\u7801\u4e2d\u7684\u67d0\u4e2a\u5730\u65b9\u3002\u8fd9\u81f3\u5c11\u6392\u9664\u4e86\u5728\u51fa\u73b0\u7b2c\u4e00\u4e2a\u53ef\u89c2\u5bdf\u5230\u7684\u75c7\u72b6\u540e\u6267\u884c\u7684\u7a0b\u5e8f\u90e8\u5206\u3002\u4f46\u8fd9\u4ecd\u7136\u53ef\u80fd\u7559\u4e0b\u5927\u91cf\u7684\u4ee3\u7801\u9700\u8981\u8986\u76d6\u3002\u4e3a\u4e86\u8bca\u65ad\u95ee\u9898\uff0c\u6211\u4eec\u5c06\u5bf9\u95ee\u9898\u7684\u4f4d\u7f6e\u8fdb\u884c\u4e00\u4e9b\u6709\u6839\u636e\u7684\u731c\u6d4b\uff0c\u76ee\u7684\u662f\u5feb\u901f\u627e\u5230\u95ee\u9898\u6240\u5728\u3002 \u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u65e0\u8bba\u662f\u4ec0\u4e48\u539f\u56e0\u5bfc\u81f4\u6211\u4eec\u6ce8\u610f\u5230\u95ee\u9898\uff0c\u90fd\u4f1a\u7ed9\u6211\u4eec\u4e00\u4e2a\u63a5\u8fd1\u5b9e\u9645\u95ee\u9898\u6240\u5728\u7684\u521d\u59cb\u731c\u6d4b\u3002\u4f8b\u5982\uff0c\u5982\u679c\u7a0b\u5e8f\u5728\u5e94\u8be5\u5199\u5165\u6570\u636e\u7684\u65f6\u5019\u6ca1\u6709\u5199\u5165\u6587\u4ef6\uff0c\u90a3\u4e48\u95ee\u9898\u53ef\u80fd\u51fa\u5728\u5904\u7406\u5199\u5165\u6587\u4ef6\u7684\u4ee3\u7801\u7684\u67d0\u4e2a\u5730\u65b9(\u5e9f\u8bdd!)\u7136\u540e\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u6e38\u620f\u7b56\u7565\u6765\u5c1d\u8bd5\u627e\u51fa\u771f\u6b63\u7684\u95ee\u9898\u6240\u5728\u3002 \u4f8b\u5982\uff1a \u5982\u679c\u5728\u7a0b\u5e8f\u7684\u67d0\u4e2a\u65f6\u523b\uff0c\u6211\u4eec\u53ef\u4ee5\u8bc1\u660e\u95ee\u9898\u8fd8\u6ca1\u6709\u53d1\u751f\uff0c\u8fd9\u5c31\u7c7b\u4f3c\u4e8e\u6536\u5230\u4e00\u4e2a\u201c\u4f4e\u4e86\u201d\u7684\u7ed3\u679c\u2014\u2014\u6211\u4eec\u77e5\u9053\u95ee\u9898\u4e00\u5b9a\u662f\u5728\u7a0b\u5e8f\u540e\u9762\u7684\u67d0\u4e2a\u5730\u65b9\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u7684\u7a0b\u5e8f\u6bcf\u6b21\u90fd\u5728\u540c\u4e00\u4e2a\u5730\u65b9\u5d29\u6e83\uff0c\u5e76\u4e14\u6211\u4eec\u53ef\u4ee5\u8bc1\u660e\u7a0b\u5e8f\u6ca1\u6709\u5728\u7a0b\u5e8f\u6267\u884c\u7684\u67d0\u4e2a\u7279\u5b9a\u70b9\u5d29\u6e83\uff0c\u90a3\u4e48\u5d29\u6e83\u4e00\u5b9a\u53d1\u751f\u5728\u4ee3\u7801\u7684\u540e\u9762\u3002 \u5982\u679c\u5728\u7a0b\u5e8f\u7684\u67d0\u4e2a\u70b9\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u89c2\u5bdf\u5230\u4e0e\u95ee\u9898\u76f8\u5173\u7684\u4e0d\u6b63\u786e\u884c\u4e3a\uff0c\u90a3\u4e48\u8fd9\u5c31\u7c7b\u4f3c\u4e8e\u63a5\u6536\u5230\u4e00\u4e2a\u201c\u9ad8\u4e86\u201d\u7684\u7ed3\u679c\uff0c\u6211\u4eec\u77e5\u9053\u95ee\u9898\u4e00\u5b9a\u662f\u5728\u7a0b\u5e8f\u7684\u65e9\u671f\u67d0\u4e2a\u5730\u65b9\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4e00\u4e2a\u7a0b\u5e8f\u8f93\u51fa\u67d0\u4e2a\u53d8\u91cf x \u7684\u503c\u3002\u4f60\u5e0c\u671b\u5b83\u8f93\u51fa 2 \uff0c\u4f46\u662f\u5b83\u8f93\u51fa\u4e868\u3002\u53d8\u91cf x \u7684\u503c\u4e00\u5b9a\u662f\u9519\u8bef\u7684\u3002\u5982\u679c\u5728\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u7684\u67d0\u4e2a\u65f6\u523b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u53d8\u91cf x \u5df2\u7ecf\u6709\u503c 8 \uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u77e5\u9053\u95ee\u9898\u4e00\u5b9a\u5728\u8fd9\u4e2a\u65f6\u523b\u4e4b\u524d\u5c31\u5df2\u7ecf\u53d1\u751f\u4e86\u3002 \u201c\u9ad8\u4e86\u4f4e\u4e86\u201d\u7684\u7c7b\u6bd4\u5e76\u4e0d\u5b8c\u7f8e\u2014\u2014\u6211\u4eec\u6709\u65f6\u4e5f\u53ef\u4ee5\u4ece\u8003\u8651\u4e2d\u5220\u9664\u4ee3\u7801\u7684\u6574\u4e2a\u90e8\u5206\uff0c\u800c\u4e0d\u83b7\u5f97\u4efb\u4f55\u5173\u4e8e\u5b9e\u9645\u95ee\u9898\u662f\u5728\u8be5\u70b9\u4e4b\u524d\u8fd8\u662f\u4e4b\u540e\u7684\u4fe1\u606f\u3002 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u5c55\u793a\u8fd9\u4e09\u79cd\u60c5\u51b5\u7684\u4f8b\u5b50\u3002 \u6700\u7ec8\uff0c\u6709\u4e86\u8db3\u591f\u7684\u7ebf\u7d22\uff0c\u518d\u914d\u5408\u4f7f\u7528\u5408\u9002\u7684\u6280\u672f\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u627e\u5230\u5f15\u8d77\u95ee\u9898\u7684\u786e\u5207\u4f4d\u7f6e\uff01\u5982\u679c\u6211\u4eec\u662f\u56e0\u4e3a\u67d0\u4e9b\u9519\u8bef\u5047\u8bbe\u800c\u9020\u6210\u4e86\u95ee\u9898\uff0c\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u4e5f\u53ef\u4ee5\u88ab\u53d1\u73b0\u3002\u5f53\u4f60\u6392\u9664\u4e86\u6240\u6709\u5176\u4ed6\u56e0\u7d20\uff0c\u5269\u4e0b\u7684\u4e00\u5b9a\u662f\u5bfc\u81f4\u95ee\u9898\u7684\u552f\u4e00\u56e0\u7d20\u3002\u90a3\u5c31\u53ea\u9700\u8981\u7406\u89e3\u4e3a\u4ec0\u4e48\u4e86\u3002 \u4f7f\u7528\u4ec0\u4e48\u731c\u6d4b\u7b56\u7565\u53d6\u51b3\u4e8e\u4f60\u81ea\u5df1\u2014\u2014\u731c\u6d4b\u7b56\u7565\u5e94\u8be5\u6839\u636ebug\u7684\u6027\u8d28\uff0c\u56e0\u5730\u5236\u5b9c\u5730\u4f7f\u7528\u3002\u6240\u4ee5\u4f60\u53ef\u80fd\u9700\u8981\u5c1d\u8bd5\u8bb8\u591a\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u7f29\u5c0f\u95ee\u9898\u7684\u8303\u56f4\u3002\u5f53\u79ef\u7d2f\u4e86\u8db3\u591f\u591a\u8c03\u8bd5\u95ee\u9898\u7684\u7ecf\u9a8c\u65f6\uff0c\u4f60\u7684\u76f4\u89c9\u4e5f\u4f1a\u53d8\u5f97\u8d8a\u6765\u8d8a\u51c6\u3002 \u90a3\u4e48\u6211\u4eec\u5982\u4f55\u201c\u731c\u6d4b\u201d\u5462\uff1f\u6709\u5f88\u591a\u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u7ae0\u4ece\u4e00\u4e9b\u7b80\u5355\u7684\u65b9\u6cd5\u5f00\u59cb\uff0c\u7136\u540e\u6211\u4eec\u5c06\u5728\u8fd9\u4e9b\u65b9\u6cd5\u7684\u57fa\u7840\u4e0a\u7ee7\u7eed\u53d1\u5c55\uff0c\u5e76\u5728\u4ee5\u540e\u7684\u7ae0\u8282\u4e2d\u63a2\u8ba8\u5176\u4ed6\u65b9\u6cd5\u3002","title":"3.3 - \u4ee3\u7801\u8c03\u8bd5\u7b56\u7565"},{"location":"3-3-A-strategy-for-debugging/#_1","text":"\u5047\u8bbe\u60a8\u6ce8\u610f\u5230\u4e00\u4e2a\u95ee\u9898\uff0c\u60a8\u60f3\u8981\u8ffd\u8e2a\u8fd9\u4e2a\u7279\u5b9a\u95ee\u9898\u7684\u539f\u56e0\u3002\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b(\u7279\u522b\u662f\u5728\u8f83\u5c0f\u7684\u7a0b\u5e8f\u4e2d)\uff0c\u6211\u4eec\u53ef\u4ee5\u5feb\u901f\u5730\u5b9a\u4f4d\u95ee\u9898\u6240\u5728\u7684\u4f4d\u7f6e\u3002 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\u7247\u6bb5\uff1a 1 2 3 4 5 6 7 8 int main () { getNames (); // ask user to enter a bunch of names sortNames (); // sort them in alphabetical order printNames (); // print the sorted list of names return 0 ; } \u5982\u679c\u4f60\u671f\u671b\u8fd9\u4e2a\u7a0b\u5e8f\u6309\u5b57\u6bcd\u987a\u5e8f\u6253\u5370\u540d\u79f0\uff0c\u4f46\u5b83\u6253\u5370\u7684\u7ed3\u679c\u5374\u662f\u76f8\u53cd\u7684\uff0c\u90a3\u4e48\u95ee\u9898\u53ef\u80fd\u51fa\u5728 sortNames \u51fd\u6570\u4e0a\u3002\u5bf9\u4e8e\u8fd9\u79cd\u53ef\u4ee5\u5c06\u95ee\u9898\u7f29\u5c0f\u5230\u7279\u5b9a\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u53ea\u9700\u67e5\u770b\u4ee3\u7801\u5c31\u53ef\u4ee5\u53d1\u73b0\u95ee\u9898\u3002 \u7136\u800c\uff0c\u968f\u7740\u7a0b\u5e8f\u53d8\u5f97\u8d8a\u6765\u8d8a\u590d\u6742\uff0c\u901a\u8fc7\u4ee3\u7801\u68c0\u67e5\u53d1\u73b0\u95ee\u9898\u4e5f\u53d8\u5f97\u8d8a\u6765\u8d8a\u590d\u6742\u3002 \u9996\u5148\uff0c\u6709\u5f88\u591a\u4ee3\u7801\u8981\u770b\u3002\u5728\u4e00\u4e2a\u6570\u5343\u884c\u957f\u7684\u7a0b\u5e8f\u4e2d\uff0c\u9010\u884c\u8d70\u8bfb\u4ee3\u7801\u4f1a\u82b1\u8d39\u5f88\u957f\u65f6\u95f4(\u800c\u4e14\u975e\u5e38\u65e0\u804a)\u3002\u5176\u6b21\uff0c\u4ee3\u7801\u672c\u8eab\u5f80\u5f80\u66f4\u590d\u6742\uff0c\u5f88\u591a\u5730\u65b9\u90fd\u53ef\u80fd\u51fa\u9519\u3002\u7b2c\u4e09\uff0c\u4ee3\u7801\u7684\u884c\u4e3a\u53ef\u80fd\u4e0d\u80fd\u63d0\u9ad8\u592a\u591a\u7ebf\u7d22\u3002\u5982\u679c\u4f60\u7f16\u5199\u4e86\u4e00\u4e2a\u8f93\u51fa\u80a1\u7968\u63a8\u8350\u7684\u7a0b\u5e8f\uff0c\u800c\u5b83\u5b9e\u9645\u4e0a\u4ec0\u4e48\u4e5f\u6ca1\u6709\u8f93\u51fa\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u80fd\u5c31\u4e0d\u77e5\u9053\u4ece\u54ea\u91cc\u5f00\u59cb\u67e5\u627e\u95ee\u9898\u4e86\u3002 \u6700\u540e\uff0c\u9519\u8bef\u53ef\u80fd\u662f\u7531\u9519\u8bef\u7684\u5047\u8bbe\u5f15\u8d77\u7684\u3002\u51e0\u4e4e\u4e0d\u53ef\u80fd\u4ece\u89c6\u89c9\u4e0a\u53d1\u73b0\u7531\u9519\u8bef\u5047\u8bbe\u5f15\u8d77\u7684\u9519\u8bef\uff0c\u56e0\u4e3a\u5728\u68c0\u67e5\u4ee3\u7801\u65f6\uff0c\u6211\u4eec\u5f80\u5f80\u503e\u5411\u4e8e\u4f1a\u505a\u51fa\u540c\u6837\u7684\u9519\u8bef\u5047\u8bbe\uff0c\u5bfc\u81f4\u6ca1\u6709\u6ce8\u610f\u5230\u9519\u8bef\u3002\u6240\u4ee5\uff0c\u5982\u679c\u4e00\u4e2a\u7a0b\u5e8f\u65e0\u6cd5\u901a\u8fc7\u4ee3\u7801\u68c0\u89c6\u53d1\u73b0\u95ee\u9898\uff0c\u90a3\u5e94\u8be5\u5982\u4f55\u5b9a\u4f4d\u5462\uff1f","title":"\u901a\u8fc7\u4ee3\u7801\u68c0\u89c6\u627e\u5230\u95ee\u9898"},{"location":"3-3-A-strategy-for-debugging/#_2","text":"\u6240\u5e78\uff0c\u5982\u679c\u4e0d\u80fd\u901a\u8fc7\u4ee3\u7801\u68c0\u89c6\u53d1\u73b0\u95ee\u9898\uff0c\u8fd8\u53ef\u4ee5\u91c7\u7528\u53e6\u4e00\u79cd\u65b9\u6cd5\uff1a\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u89c2\u5bdf\u5b83\u7684\u884c\u4e3a\uff0c\u5e76\u5c1d\u8bd5\u4ece\u4e2d\u8bca\u65ad\u95ee\u9898\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6982\u62ec\u4e3a: \u627e\u5230\u590d\u73b0\u95ee\u9898\u7684\u6761\u4ef6\uff1b \u8fd0\u884c\u7a0b\u5e8f\uff0c\u83b7\u53d6\u4fe1\u606f\uff0c\u9010\u6b65\u903c\u8fd1\"\u771f\u50cf\"\uff1b \u91cd\u590d\u4e0a\u8ff0\u6b65\u9aa4\u76f4\u5230\u627e\u5230\u95ee\u9898\u6240\u5728\u3002 \u5728\u672c\u7ae0\u7684\u5176\u4f59\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5b9e\u65bd\u8fd9\u79cd\u65b9\u6cd5\u7684\u6280\u672f\u3002","title":"\u901a\u8fc7\u8fd0\u884c\u7a0b\u5e8f\u627e\u5230\u95ee\u9898"},{"location":"3-3-A-strategy-for-debugging/#_3","text":"\u53d1\u73b0\u95ee\u9898\u7684\u7b2c\u4e00\u6b65\u4e5f\u662f\u6700\u91cd\u8981\u7684\u4e00\u6b65\u662f\u80fd\u591f\u91cd\u73b0\u95ee\u9898\uff0c\u539f\u56e0\u5f88\u7b80\u5355\uff1a\u5982\u679c\u4f60\u4e0d\u80fd\u518d\u6b21\u89c2\u5bdf\u5230\u95ee\u9898\u7684\u8bdd\uff0c\u8981\u5982\u4f55\u624d\u80fd\u5b9a\u4f4d\u5b83\u5462\uff1f \u56de\u5230\u5236\u51b0\u673a\u7684\u7c7b\u6bd4\u2014\u2014\u5047\u8bbe\u6709\u4e00\u5929\u4f60\u7684\u670b\u53cb\u544a\u8bc9\u4f60\uff0c\u4f60\u7684\u5236\u51b0\u673a\u574f\u4e86\u3002\u4f60\u53bb\u68c0\u67e5\u7684\u65f6\u5019\u53d1\u73b0\u5b83\u5de5\u4f5c\u5f97\u5f88\u597d\u3002\u4f60\u5e94\u8be5\u5982\u4f55\u8bca\u65ad\u8fd9\u4e2a\u95ee\u9898\uff1f\u8fd9\u4e2a\u65f6\u5019\u5c31\u6bd4\u8f83\u68d8\u624b\u4e86\u3002\u7136\u800c\uff0c\u5982\u679c\u4f60\u80fd\u591f\u518d\u6b21\u8ba9\u5236\u51b0\u673a\u8fdb\u5165\u5230\u4e0d\u80fd\u5de5\u4f5c\u7684\u72b6\u6001\uff0c\u90a3\u4e48\u624d\u53ef\u4ee5\u5f00\u59cb\u5b9a\u4f4d\u95ee\u9898\u3002 \u5982\u679c\u4e00\u4e2a\u8f6f\u4ef6bug\u5f88\u660e\u663e\u7684(\u4f8b\u5982\uff0c\u7a0b\u5e8f\u5728\u6bcf\u6b21\u8fd0\u884c\u65f6\u90fd\u5728\u76f8\u540c\u7684\u5730\u65b9\u5d29\u6e83)\uff0c\u90a3\u4e48\u91cd\u73b0\u8fd9\u4e2a\u95ee\u9898\u5c31\u76f8\u5f53\u7b80\u5355\u4e86\u3002\u7136\u800c\uff0c\u590d\u73b0\u95ee\u9898\u6709\u65f6\u5019\u662f\u5f88\u56f0\u96be\u7684\u3002\u4f8b\u5982\u8be5\u95ee\u9898\u53ef\u80fd\u53ea\u53d1\u751f\u5728\u67d0\u4e9b\u8bbe\u5907\u4e0a\uff0c\u6216\u53ea\u5728\u7279\u5b9a\u60c5\u51b5\u4e0b(\u4f8b\u5982\uff0c\u5f53\u7528\u6237\u8f93\u5165\u67d0\u4e9b\u8f93\u5165\u65f6)\u624d\u51fa\u73b0\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8bbe\u8ba1\u4e00\u7ec4\u590d\u73b0\u95ee\u9898\u7684\u6b65\u9aa4\u4f1a\u5f88\u6709\u5e2e\u52a9\u3002\u590d\u73b0\u6b65\u9aa4\u662f\u4e00\u4e2a\u6e05\u6670\u4e14\u7cbe\u786e\u7684\u5206\u6b65\u6267\u884c\u5217\u8868\uff0c\u4f60\u53ef\u4ee5\u9075\u5faa\u8fd9\u4e9b\u6b65\u9aa4\u4f7f\u95ee\u9898\u4ee5\u66f4\u5927\u7684\u6982\u7387\u518d\u6b21\u51fa\u73b0\u3002\u6211\u4eec\u7684\u76ee\u6807\u662f\u5c31\u80fd\u591f\u5c3d\u53ef\u80fd\u5730\u4f7f\u95ee\u9898\u518d\u6b21\u51fa\u73b0\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u53cd\u590d\u8fd0\u884c\u7a0b\u5e8f\uff0c\u5e76\u67e5\u627e\u7ebf\u7d22\u6765\u786e\u5b9a\u662f\u4ec0\u4e48\u539f\u56e0\u5bfc\u81f4\u4e86\u95ee\u9898\u3002\u5982\u679c\u95ee\u9898\u53ef\u4ee5100%\u7684\u91cd\u73b0\uff0c\u90a3\u81ea\u7136\u662f\u6700\u7406\u60f3\u7684\u72b6\u6001\uff0c\u4f46\u4f4e\u4e8e100%\u901a\u5e38\u4e5f\u662f\u53ef\u4ee5\u7684\u3002\u4e00\u4e2a\u53ea\u670950%\u51e0\u7387\u89e6\u53d1\u7684\u95ee\u9898\uff0c\u610f\u5473\u7740\u6211\u4eec\u9700\u8981\u82b1\u8d39\u4e24\u500d\u7684\u65f6\u95f4\u6765\u8bca\u65ad\u95ee\u9898\uff0c\u56e0\u4e3a\u6709\u4e00\u534a\u7684\u65f6\u95f4\u7a0b\u5e8f\u4e0d\u4f1a\u663e\u793a\u95ee\u9898\uff0c\u56e0\u6b64\u4e0d\u4f1a\u63d0\u4f9b\u4efb\u4f55\u6709\u7528\u7684\u8bca\u65ad\u4fe1\u606f\u3002","title":"\u590d\u73b0\u95ee\u9898"},{"location":"3-3-A-strategy-for-debugging/#_4","text":"\u4e00\u65e6\u6211\u4eec\u80fd\u591f\u6bd4\u8f83\u7a33\u5b9a\u5730\u590d\u73b0\u95ee\u9898\uff0c\u63a5\u4e0b\u6765\u5c31\u662f\u627e\u5230\u5bfc\u81f4\u95ee\u9898\u7684\u4ee3\u7801\u3002\u6839\u636e\u95ee\u9898\u7684\u6027\u8d28\u4e0d\u540c\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u53ef\u80fd\u5f88\u7b80\u5355\uff0c\u4e5f\u53ef\u80fd\u5f88\u56f0\u96be\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u6211\u4eec\u4e0d\u592a\u6e05\u695a\u95ee\u9898\u5230\u5e95\u5728\u54ea\u91cc\u3002\u6211\u4eec\u600e\u4e48\u627e\u5230\u5b83? \u8fd9\u91cc\u53ef\u4ee5\u505a\u4e2a\u7c7b\u6bd4\u3002\u6211\u4eec\u6765\u73a9\u4e2a\u731c\u6570\u5b57\u6e38\u620f\uff0c\u4f60\u9700\u8981\u731c\u4e00\u4e2a1\u523010\u4e4b\u95f4\u7684\u6570\u5b57\uff0c\u6bcf\u731c\u4e00\u6b21\uff0c\u6211\u5c31\u4f1a\u544a\u8bc9\u4f60\u662f\u592a\u9ad8\u3001\u592a\u4f4e\u8fd8\u662f\u6b63\u786e\uff1a 1 2 3 4 5 6 7 8 You: 5 Me: Too low You: 8 Me: Too high You: 6 Me: Too low You: 7 Me: Correct \u5728\u4e0a\u9762\u7684\u6e38\u620f\u4e2d\uff0c\u4f60\u4e0d\u9700\u8981\u731c\u6bcf\u4e00\u4e2a\u6570\u5b57\u6765\u627e\u5230\u7b54\u6848\u3002\u901a\u8fc7\u731c\u6d4b\u548c\u8003\u8651\u4ece\u6bcf\u6b21\u731c\u6d4b\u4e2d\u5b66\u5230\u7684\u4fe1\u606f\u7684\u8fc7\u7a0b\uff0c\u4f60\u53ef\u80fd\u53ea\u9700\u51e0\u6b21\u731c\u6d4b\u5c31\u53ef\u4ee5\u201c\u9501\u5b9a\u201d\u6b63\u786e\u7684\u6570\u5b57(\u5982\u679c\u4f60\u4f7f\u7528\u6700\u4f18\u7b56\u7565\uff0c\u90a3\u4e48\u603b\u662f\u53ef\u4ee5\u57284\u6b21\u6216\u66f4\u5c11\u7684\u731c\u6d4b\u4e2d\u627e\u5230\u76ee\u6807\u6570\u5b57)\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u6765\u8c03\u8bd5\u7a0b\u5e8f\u3002\u5728\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u4e0d\u77e5\u9053bug\u5728\u54ea\u91cc\u3002\u7136\u800c\uff0c\u6211\u4eec\u77e5\u9053\u95ee\u9898\u4e00\u5b9a\u662f\u5728\u7a0b\u5e8f\u5f00\u59cb\u5230\u7a0b\u5e8f\u663e\u793a\u51fa\u6211\u4eec\u53ef\u4ee5\u89c2\u5bdf\u5230\u7684\u7b2c\u4e00\u4e2a\u9519\u8bef\u75c7\u72b6\u4e4b\u95f4\u6267\u884c\u7684\u4ee3\u7801\u4e2d\u7684\u67d0\u4e2a\u5730\u65b9\u3002\u8fd9\u81f3\u5c11\u6392\u9664\u4e86\u5728\u51fa\u73b0\u7b2c\u4e00\u4e2a\u53ef\u89c2\u5bdf\u5230\u7684\u75c7\u72b6\u540e\u6267\u884c\u7684\u7a0b\u5e8f\u90e8\u5206\u3002\u4f46\u8fd9\u4ecd\u7136\u53ef\u80fd\u7559\u4e0b\u5927\u91cf\u7684\u4ee3\u7801\u9700\u8981\u8986\u76d6\u3002\u4e3a\u4e86\u8bca\u65ad\u95ee\u9898\uff0c\u6211\u4eec\u5c06\u5bf9\u95ee\u9898\u7684\u4f4d\u7f6e\u8fdb\u884c\u4e00\u4e9b\u6709\u6839\u636e\u7684\u731c\u6d4b\uff0c\u76ee\u7684\u662f\u5feb\u901f\u627e\u5230\u95ee\u9898\u6240\u5728\u3002 \u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u65e0\u8bba\u662f\u4ec0\u4e48\u539f\u56e0\u5bfc\u81f4\u6211\u4eec\u6ce8\u610f\u5230\u95ee\u9898\uff0c\u90fd\u4f1a\u7ed9\u6211\u4eec\u4e00\u4e2a\u63a5\u8fd1\u5b9e\u9645\u95ee\u9898\u6240\u5728\u7684\u521d\u59cb\u731c\u6d4b\u3002\u4f8b\u5982\uff0c\u5982\u679c\u7a0b\u5e8f\u5728\u5e94\u8be5\u5199\u5165\u6570\u636e\u7684\u65f6\u5019\u6ca1\u6709\u5199\u5165\u6587\u4ef6\uff0c\u90a3\u4e48\u95ee\u9898\u53ef\u80fd\u51fa\u5728\u5904\u7406\u5199\u5165\u6587\u4ef6\u7684\u4ee3\u7801\u7684\u67d0\u4e2a\u5730\u65b9(\u5e9f\u8bdd!)\u7136\u540e\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u6e38\u620f\u7b56\u7565\u6765\u5c1d\u8bd5\u627e\u51fa\u771f\u6b63\u7684\u95ee\u9898\u6240\u5728\u3002 \u4f8b\u5982\uff1a \u5982\u679c\u5728\u7a0b\u5e8f\u7684\u67d0\u4e2a\u65f6\u523b\uff0c\u6211\u4eec\u53ef\u4ee5\u8bc1\u660e\u95ee\u9898\u8fd8\u6ca1\u6709\u53d1\u751f\uff0c\u8fd9\u5c31\u7c7b\u4f3c\u4e8e\u6536\u5230\u4e00\u4e2a\u201c\u4f4e\u4e86\u201d\u7684\u7ed3\u679c\u2014\u2014\u6211\u4eec\u77e5\u9053\u95ee\u9898\u4e00\u5b9a\u662f\u5728\u7a0b\u5e8f\u540e\u9762\u7684\u67d0\u4e2a\u5730\u65b9\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u7684\u7a0b\u5e8f\u6bcf\u6b21\u90fd\u5728\u540c\u4e00\u4e2a\u5730\u65b9\u5d29\u6e83\uff0c\u5e76\u4e14\u6211\u4eec\u53ef\u4ee5\u8bc1\u660e\u7a0b\u5e8f\u6ca1\u6709\u5728\u7a0b\u5e8f\u6267\u884c\u7684\u67d0\u4e2a\u7279\u5b9a\u70b9\u5d29\u6e83\uff0c\u90a3\u4e48\u5d29\u6e83\u4e00\u5b9a\u53d1\u751f\u5728\u4ee3\u7801\u7684\u540e\u9762\u3002 \u5982\u679c\u5728\u7a0b\u5e8f\u7684\u67d0\u4e2a\u70b9\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u89c2\u5bdf\u5230\u4e0e\u95ee\u9898\u76f8\u5173\u7684\u4e0d\u6b63\u786e\u884c\u4e3a\uff0c\u90a3\u4e48\u8fd9\u5c31\u7c7b\u4f3c\u4e8e\u63a5\u6536\u5230\u4e00\u4e2a\u201c\u9ad8\u4e86\u201d\u7684\u7ed3\u679c\uff0c\u6211\u4eec\u77e5\u9053\u95ee\u9898\u4e00\u5b9a\u662f\u5728\u7a0b\u5e8f\u7684\u65e9\u671f\u67d0\u4e2a\u5730\u65b9\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4e00\u4e2a\u7a0b\u5e8f\u8f93\u51fa\u67d0\u4e2a\u53d8\u91cf x \u7684\u503c\u3002\u4f60\u5e0c\u671b\u5b83\u8f93\u51fa 2 \uff0c\u4f46\u662f\u5b83\u8f93\u51fa\u4e868\u3002\u53d8\u91cf x \u7684\u503c\u4e00\u5b9a\u662f\u9519\u8bef\u7684\u3002\u5982\u679c\u5728\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u7684\u67d0\u4e2a\u65f6\u523b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u53d8\u91cf x \u5df2\u7ecf\u6709\u503c 8 \uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u77e5\u9053\u95ee\u9898\u4e00\u5b9a\u5728\u8fd9\u4e2a\u65f6\u523b\u4e4b\u524d\u5c31\u5df2\u7ecf\u53d1\u751f\u4e86\u3002 \u201c\u9ad8\u4e86\u4f4e\u4e86\u201d\u7684\u7c7b\u6bd4\u5e76\u4e0d\u5b8c\u7f8e\u2014\u2014\u6211\u4eec\u6709\u65f6\u4e5f\u53ef\u4ee5\u4ece\u8003\u8651\u4e2d\u5220\u9664\u4ee3\u7801\u7684\u6574\u4e2a\u90e8\u5206\uff0c\u800c\u4e0d\u83b7\u5f97\u4efb\u4f55\u5173\u4e8e\u5b9e\u9645\u95ee\u9898\u662f\u5728\u8be5\u70b9\u4e4b\u524d\u8fd8\u662f\u4e4b\u540e\u7684\u4fe1\u606f\u3002 \u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u5c55\u793a\u8fd9\u4e09\u79cd\u60c5\u51b5\u7684\u4f8b\u5b50\u3002 \u6700\u7ec8\uff0c\u6709\u4e86\u8db3\u591f\u7684\u7ebf\u7d22\uff0c\u518d\u914d\u5408\u4f7f\u7528\u5408\u9002\u7684\u6280\u672f\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u627e\u5230\u5f15\u8d77\u95ee\u9898\u7684\u786e\u5207\u4f4d\u7f6e\uff01\u5982\u679c\u6211\u4eec\u662f\u56e0\u4e3a\u67d0\u4e9b\u9519\u8bef\u5047\u8bbe\u800c\u9020\u6210\u4e86\u95ee\u9898\uff0c\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u4e5f\u53ef\u4ee5\u88ab\u53d1\u73b0\u3002\u5f53\u4f60\u6392\u9664\u4e86\u6240\u6709\u5176\u4ed6\u56e0\u7d20\uff0c\u5269\u4e0b\u7684\u4e00\u5b9a\u662f\u5bfc\u81f4\u95ee\u9898\u7684\u552f\u4e00\u56e0\u7d20\u3002\u90a3\u5c31\u53ea\u9700\u8981\u7406\u89e3\u4e3a\u4ec0\u4e48\u4e86\u3002 \u4f7f\u7528\u4ec0\u4e48\u731c\u6d4b\u7b56\u7565\u53d6\u51b3\u4e8e\u4f60\u81ea\u5df1\u2014\u2014\u731c\u6d4b\u7b56\u7565\u5e94\u8be5\u6839\u636ebug\u7684\u6027\u8d28\uff0c\u56e0\u5730\u5236\u5b9c\u5730\u4f7f\u7528\u3002\u6240\u4ee5\u4f60\u53ef\u80fd\u9700\u8981\u5c1d\u8bd5\u8bb8\u591a\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u7f29\u5c0f\u95ee\u9898\u7684\u8303\u56f4\u3002\u5f53\u79ef\u7d2f\u4e86\u8db3\u591f\u591a\u8c03\u8bd5\u95ee\u9898\u7684\u7ecf\u9a8c\u65f6\uff0c\u4f60\u7684\u76f4\u89c9\u4e5f\u4f1a\u53d8\u5f97\u8d8a\u6765\u8d8a\u51c6\u3002 \u90a3\u4e48\u6211\u4eec\u5982\u4f55\u201c\u731c\u6d4b\u201d\u5462\uff1f\u6709\u5f88\u591a\u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u7ae0\u4ece\u4e00\u4e9b\u7b80\u5355\u7684\u65b9\u6cd5\u5f00\u59cb\uff0c\u7136\u540e\u6211\u4eec\u5c06\u5728\u8fd9\u4e9b\u65b9\u6cd5\u7684\u57fa\u7840\u4e0a\u7ee7\u7eed\u53d1\u5c55\uff0c\u5e76\u5728\u4ee5\u540e\u7684\u7ae0\u8282\u4e2d\u63a2\u8ba8\u5176\u4ed6\u65b9\u6cd5\u3002","title":"\u5173\u6ce8\u95ee\u9898"},{"location":"3-4-Basic-debugging-tactics/","tags":["debugging"],"text":"\u5728\u4e0a\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u63a2\u7d22\u4e86\u4e00\u79cd\u901a\u8fc7\u8fd0\u884c\u7a0b\u5e8f\u548c\u4f7f\u7528\u731c\u6d4b\u6765\u67e5\u627e\u95ee\u9898\u6240\u5728\u7684\u7b56\u7565\u3002\u672c\u8282\u8bfe\u6211\u4eec\u5c06\u63a2\u7d22\u4e00\u4e9b\u7528\u4e8e\u731c\u6d4b\u548c\u6536\u96c6\u4fe1\u606f\u4ee5\u5e2e\u52a9\u53d1\u73b0\u95ee\u9898\u7684\u57fa\u672c\u7b56\u7565\u3002 Debug \u6280\u672f 1\uff1a\u6ce8\u91ca\u6389\u4ee3\u7801 \u5148\u4ece\u4e00\u4e2a\u7b80\u5355\u7684\u95ee\u9898\u5f00\u59cb\u3002\u5982\u679c\u7a0b\u5e8f\u8868\u73b0\u51fa\u9519\u8bef\u7684\u884c\u4e3a\uff0c\u6211\u4eec\u53ef\u4ee5\u6ce8\u91ca\u6389\u4e00\u4e9b\u4ee3\u7801\uff0c\u770b\u770b\u95ee\u9898\u662f\u5426\u4ecd\u7136\u5b58\u5728\u3002\u5982\u679c\u95ee\u9898\u4ecd\u7136\u5b58\u5728\uff0c\u5219\u6ce8\u91ca\u6389\u7684\u4ee3\u7801\u80af\u5b9a\u6ca1\u6709\u95ee\u9898\u3002 \u8003\u8651\u4ee5\u4e0b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 int main () { getNames (); // ask user to enter a bunch of names doMaintenance (); // do some random stuff sortNames (); // sort them in alphabetical order printNames (); // print the sorted list of names return 0 ; } \u5047\u8bbe\u8fd9\u4e2a\u7a0b\u5e8f\u5e94\u8be5\u6309\u5b57\u6bcd\u987a\u5e8f\u6253\u5370\u7528\u6237\u8f93\u5165\u7684\u540d\u5b57\uff0c\u4f46\u5b83\u5374\u6309\u76f8\u53cd\u7684\u5b57\u6bcd\u987a\u5e8f\u6253\u5370\u3002\u95ee\u9898\u5728\u54ea\u91cc? getNames \u662f\u5426\u8f93\u5165\u4e86\u9519\u8bef\u7684\u540d\u79f0\uff1f sortNames \u662f\u5426\u5c06\u5b83\u4eec\u53cd\u5411\u6392\u5e8f\uff1f\u8fd8\u662f printNames \u6253\u5370\u7684\u987a\u5e8f\u4e0d\u5bf9\uff1f\u4ee5\u4e0a\u4efb\u4f55\u4e00\u79cd\u731c\u6d4b\u90fd\u6709\u53ef\u80fd\uff0c\u4f46\u662f domainmaintenance() \u591a\u534a\u4e0e\u95ee\u9898\u65e0\u5173\uff0c\u6240\u4ee5\u53ef\u4ee5\u5148\u5c06\u5176\u6ce8\u91ca\u6389\u3002 1 2 3 4 5 6 7 8 9 int main () { getNames (); // ask user to enter a bunch of names // doMaintenance(); // do some random stuff sortNames (); // sort them in alphabetical order printNames (); // print the sorted list of names return 0 ; } \u5982\u679c\u95ee\u9898\u6d88\u5931\u4e86\uff0c\u90a3\u4e48 domainance \u53cd\u5012\u662f\u6709\u95ee\u9898\u7684\uff0c\u5230\u65f6\u5019\u518d\u6ce8\u610f\u529b\u96c6\u4e2d\u5728\u90a3\u91cc\u3002 \u4e0d\u8fc7\uff0c\u95ee\u9898\u5f88\u53ef\u80fd\u8fd8\u4f1a\u5b58\u5728\uff0c\u56e0\u6b64 doMaintenance \u4e0d\u662f\u9519\u8bef\u7684\u6839\u6e90\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u641c\u7d22\u4e2d\u6392\u9664\u6574\u4e2a\u51fd\u6570\u3002\u8fd9\u5e76\u4e0d\u80fd\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u5b9e\u9645\u95ee\u9898\u662f\u53d1\u751f\u5728 doMaintenance \u8c03\u7528\u4e4b\u524d\u8fd8\u662f\u4e4b\u540e\uff0c\u4f46\u7ec8\u5f52\u662f\u7f29\u5c0f\u4e86\u8303\u56f4\u3002 \u4e0d\u8981\u5fd8\u8bb0\u6ce8\u91ca\u6389\u4e86\u54ea\u4e9b\u51fd\u6570\uff0c\u4ee5\u4fbf\u4ee5\u540e\u53ef\u4ee5\u53d6\u6d88\u6ce8\u91ca! Debug \u6280\u672f 2\uff1a\u9a8c\u8bc1\u4ee3\u7801\u6d41\u7a0b \u590d\u6742\u7a0b\u5e8f\u4e2d\u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u662f\u7a0b\u5e8f\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u7684\u6b21\u6570\u592a\u591a\u6216\u8fc7\u5c11(\u5305\u62ec\u6839\u672c\u4e0d\u8c03\u7528)\u3002 \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5c06\u8bed\u53e5\u653e\u5728\u51fd\u6570\u7684\u9876\u90e8\u4ee5\u6253\u5370\u51fd\u6570\u540d\u4f1a\u5f88\u6709\u5e2e\u52a9\u3002\u8fd9\u6837\uff0c\u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u5c31\u53ef\u4ee5\u6253\u5370\u51fa\u8c03\u7528\u4e86\u54ea\u4e9b\u51fd\u6570\u3002 \u5c0f\u8d34\u58eb \u5f53\u6253\u5370\u7528\u4e8e\u8c03\u8bd5\u76ee\u7684\u7684\u4fe1\u606f\u65f6\uff0c\u4f7f\u7528 std::cerr \u800c\u4e0d\u662f std::cout \u3002\u8fd9\u6837\u505a\u7684\u4e00\u4e2a\u539f\u56e0\u662f std::cout \u53ef\u80fd\u4f1a\u88ab\u7f13\u51b2\uff0c\u8fd9\u610f\u5473\u7740\u5728\u8981\u6c42 std::cout \u6253\u5370\u4fe1\u606f\u548c\u5b83\u5b9e\u9645\u6253\u5370\u4fe1\u606f\u4e4b\u95f4\u53ef\u80fd\u4f1a\u6709\u4e00\u4e2a\u65f6\u95f4\u5dee\u3002\u5982\u679c\u4f7f\u7528 std::cout \u8fdb\u884c\u6253\u5370\u7136\u540e\u7a0b\u5e8f\u7acb\u5373\u5d29\u6e83\uff0c\u90a3\u4e48 std::cout \u53ef\u80fd\u8fd8\u6ca1\u6709\u6765\u5f97\u53ca\u6253\u5370\u3002\u8fd9\u53ef\u80fd\u4f1a\u8bef\u5bfc\u4f60\u3002\u53e6\u4e00\u65b9\u9762\uff0c std::cerr \u662f\u65e0\u7f13\u51b2\u7684\uff0c\u8fd9\u610f\u5473\u7740\u53d1\u9001\u7ed9\u5b83\u7684\u4efb\u4f55\u5185\u5bb9\u90fd\u5c06\u7acb\u5373\u6253\u5370\u3002\u8fd9\u6709\u52a9\u4e8e\u786e\u4fdd\u6240\u6709\u8c03\u8bd5\u8f93\u51fa\u5c3d\u5feb\u51fa\u73b0(\u4ee5\u4e00\u4e9b\u6027\u80fd\u4e3a\u4ee3\u4ef7\uff0c\u6211\u4eec\u5728\u8c03\u8bd5\u65f6\u901a\u5e38\u4e0d\u5173\u7cfb\u6027\u80fd)\u3002 \u4f7f\u7528 std::cerr \u4e5f\u53ef\u4ee5\u8868\u660e\u8be5\u6253\u5370\u4fe1\u606f\u548c\u9519\u8bef\u6d41\u7a0b\u6709\u5173\u3002 \u8003\u8651\u4ee5\u4e0b\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int getValue () { return 4 ; } int main () { std :: cout << getValue ; return 0 ; } \u4f60\u5fc5\u987b\u5173\u95ed \u201c\u5c06\u8b66\u544a\u5f53\u505a\u9519\u8bef\u5904\u7406\u201d \u7684\u7f16\u8bd1\u9009\u9879\uff0c\u624d\u80fd\u7f16\u8bd1\u8be5\u4ee3\u7801\u3002 \u867d\u7136\u6211\u4eec\u671f\u671b\u8fd9\u4e2a\u7a0b\u5e8f\u6253\u5370\u503c 4\uff0c\u4f46\u5b83\u5b9e\u9645\u6253\u5370\u4e86 1 \u3002 \u5728 Visual Studio (\u6216\u8005\u5176\u4ed6\u7f16\u8bd1\u5668)\u4e0a\uff0c\u4e5f\u53ef\u80fd\u6253\u5370\uff1a 1 00101424 \u6dfb\u52a0\u4e00\u4e9b\u8c03\u8bd5\u8bed\u53e5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int getValue () { std :: cerr << \"getValue() called \\n \" ; return 4 ; } int main () { std :: cerr << \"main() called \\n \" ; std :: cout << getValue ; return 0 ; } \u5c0f\u8d34\u58eb \u5728\u6dfb\u52a0\u4e34\u65f6\u8c03\u8bd5\u8bed\u53e5\u65f6\uff0c\u4e0d\u7f29\u8fdb\u4f1a\u5f88\u6709\u5e2e\u52a9\u3002\u8fd9\u6837\u5728\u4e0d\u9700\u8981\u65f6\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u627e\u5230\u5b83\u4eec\u5e76\u5220\u9664\u3002 \u73b0\u5728\uff0c\u51fd\u6570\u6267\u884c\u65f6\u4f1a\u6253\u5370\u5bf9\u5e94\u7684\u51fd\u6570\u540d\uff1a 1 2 main () called 1 \u53ef\u4ee5\u770b\u5230 getValue \u6839\u672c\u6ca1\u88ab\u8c03\u7528\u8fc7\u3002\u90a3\u4e48\u80af\u5b9a\u662f\u8c03\u7528\u8be5\u51fd\u6570\u7684\u4ee3\u7801\u51fa\u4e86\u95ee\u9898\u3002\u4ed4\u7ec6\u770b\u770b\u90a3\u884c\u4ee3\u7801\uff1a 1 std :: cout << getValue ; \u54e6\uff0c\u770b\uff0c\u6211\u4eec\u5fd8\u8bb0\u4e86\u51fd\u6570\u8c03\u7528\u7684\u62ec\u53f7\u3002\u5b83\u5e94\u8be5\u662f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int getValue () { std :: cerr << \"getValue() called \\n \" ; return 4 ; } int main () { std :: cerr << \"main() called \\n \" ; std :: cout << getValue (); // added parenthesis here return 0 ; } \u8fd9\u6837\u4e00\u6765\u5c31\u6ca1\u95ee\u9898\u4e86\uff1a 1 2 3 main() called getValue() called 4 \u6b64\u65f6\u53ef\u4ee5\u5220\u9664\u4e34\u65f6\u8c03\u8bd5\u8bed\u53e5\u4e86\u3002 Debug \u6280\u672f 3\uff1a \u6253\u5370\u503c \u5bf9\u4e8e\u67d0\u4e9b\u7c7b\u578b\u7684\u9519\u8bef\uff0c\u7a0b\u5e8f\u53ef\u80fd\u8ba1\u7b97\u6216\u4f20\u9012\u4e86\u9519\u8bef\u7684\u503c\u3002 \u6211\u4eec\u8fd8\u53ef\u4ee5\u8f93\u51fa\u53d8\u91cf(\u5305\u62ec\u53c2\u6570)\u6216\u8868\u8fbe\u5f0f\u7684\u503c\uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u662f\u6b63\u786e\u7684\u3002 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff0c\u5b83\u5e94\u8be5\u628a\u4e24\u4e2a\u6570\u5b57\u76f8\u52a0\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include int add ( int x , int y ) { return x + y ; } void printResult ( int z ) { std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { int x { getUserInput () }; int y { getUserInput () }; std :: cout << x << \" + \" << y << '\\n' ; int z { add ( x , 5 ) }; printResult ( z ); return 0 ; } \u8f93\u51fa\u7ed3\u679c\u4e0d\u6b63\u786e\uff1a 1 2 3 4 Enter a number: 4 Enter a number: 3 4 + 3 The answer is: 9 \u7ed3\u679c\u4e0d\u5bf9\uff0c\u4f46\u4f60\u80fd\u53d1\u73b0\u9519\u8bef\u5417\uff1f\u5373\u4f7f\u5728\u8fd9\u4e2a\u7b80\u77ed\u7684\u7a0b\u5e8f\u4e2d\uff0c\u95ee\u9898\u4e5f\u5f88\u96be\u88ab\u53d1\u73b0\u3002\u63a5\u4e0b\u6765\u6dfb\u52a0\u4e00\u4e9b\u4ee3\u7801\u6765\u6253\u5370\u53d8\u91cf\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include int add ( int x , int y ) { return x + y ; } void printResult ( int z ) { std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { int x { getUserInput () }; std :: cerr << \"main::x = \" << x << '\\n' ; int y { getUserInput () }; std :: cerr << \"main::y = \" << y << '\\n' ; std :: cout << x << \" + \" << y << '\\n' ; int z { add ( x , 5 ) }; std :: cerr << \"main::z = \" << z << '\\n' ; printResult ( z ); return 0 ; } \u8f93\u51fa\uff1a 1 2 3 4 5 6 7 Enter a number: 4 main::x = 4 Enter a number: 3 main::y = 3 4 + 3 main::z = 9 The answer is: 9 \u53d8\u91cf x \u548c y \u7684\u503c\u90fd\u662f\u6b63\u786e\u7684\uff0c\u4f46\u662f z \u7684\u503c\u4e0d\u6b63\u786e\u3002\u6240\u4ee5\u95ee\u9898\u80af\u5b9a\u5728\u8fd9\u9644\u8fd1\uff0c\u8fd9\u6837\u4e00\u6765 add \u51fd\u6570\u5c31\u975e\u5e38\u53ef\u7591\u4e86\u3002 \u4fee\u6539\u4e00\u4e0b add \u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include int add ( int x , int y ) { std :: cerr << \"add() called (x=\" << x << \", y=\" << y << \") \\n \" ; return x + y ; } void printResult ( int z ) { std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { int x { getUserInput () }; std :: cerr << \"main::x = \" << x << '\\n' ; int y { getUserInput () }; std :: cerr << \"main::y = \" << y << '\\n' ; std :: cout << x << \" + \" << y << '\\n' ; int z { add ( x , 5 ) }; std :: cerr << \"main::z = \" << z << '\\n' ; printResult ( z ); return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 5 6 7 Enter a number: 4 main::x = 4 Enter a number: 3 main::y = 3 add() called (x=4, y=5) main::z = 9 The answer is: 9 \u53d8\u91cf y \u7684\u503c\u662f3\uff0c\u4f46\u662f\u6211\u4eec\u7684\u51fd\u6570 add \u7684\u53c2\u6570 y \u7684\u503c\u662f 5 \u3002\u6211\u4eec\u80af\u5b9a\u4f20\u9012\u4e86\u9519\u8bef\u7684\u53c2\u6570\u3002\u679c\u7136\uff1a 1 int z { add ( x , 5 ) }; \u627e\u5230\u4e86\uff01\u5728\u8c03\u7528 add \u51fd\u6570\u7684\u65f6\u5019\u6211\u4eec\u4f20\u9012\u4e86\u5b57\u9762\u91cf 5 \u800c\u4e0d\u662f\u53d8\u91cf y \u3002\u4fee\u6539\u8fd9\u4e2a\u95ee\u9898\u53ea\u9700\u8981\u628a\u8fd9\u91cc\u7684\u5b9e\u53c2\u6539\u6389\u5c31\u53ef\u4ee5\u4e86\u3002 \u518d\u770b\u4e00\u4e2a\u4f8b\u5b50\uff1a \u8fd9\u4e2a\u7a0b\u5e8f\u4e0e\u524d\u9762\u7684\u7a0b\u5e8f\u975e\u5e38\u76f8\u4f3c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include int add ( int x , int y ) { return x + y ; } void printResult ( int z ) { std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return -- x ; } int main () { int x { getUserInput () }; int y { getUserInput () }; int z { add ( x , y ) }; printResult ( z ); return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 Enter a number: 4 Enter a number: 3 The answer is: 5 \u7ed3\u679c\u4e0d\u5bf9\uff0c\u4ec0\u4e48\u539f\u56e0\u5462\uff1f \u8ba9\u6211\u4eec\u5bf9\u8fd9\u6bb5\u4ee3\u7801\u8fdb\u884c\u4e00\u4e9b\u8c03\u8bd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include int add ( int x , int y ) { std :: cerr << \"add() called (x=\" << x << \", y=\" << y << \") \\n \" ; return x + y ; } void printResult ( int z ) { std :: cerr << \"printResult() called (z=\" << z << \") \\n \" ; std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cerr << \"getUserInput() called \\n \" ; std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return -- x ; } int main () { std :: cerr << \"main() called \\n \" ; int x { getUserInput () }; std :: cerr << \"main::x = \" << x << '\\n' ; int y { getUserInput () }; std :: cerr << \"main::y = \" << y << '\\n' ; int z { add ( x , y ) }; std :: cerr << \"main::z = \" << z << '\\n' ; printResult ( z ); return 0 ; } \u73b0\u5728\u8ba9\u6211\u4eec\u7528\u76f8\u540c\u7684\u8f93\u5165\u518d\u6b21\u8fd0\u884c\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 main() called getUserInput() called Enter a number: 4 main::x = 3 getUserInput() called Enter a number: 3 main::y = 2 add() called (x=3, y=2) main::z = 5 printResult() called (z=5) The answer is: 5 \u8fd9\u6837\u4e00\u6765\u5c31\u53ef\u4ee5\u770b\u51fa\u95ee\u9898\u4e86\uff1a\u7528\u6237\u8f93\u5165\u7684\u503c\u662f 4 \uff0c\u4f46\u662f main \u51fd\u6570\u4e2d x \u5f97\u5230\u7684\u503c\u5374\u662f 3 \u3002\u5728\u7528\u6237\u8f93\u5165\u7684\u4f4d\u7f6e\u548c\u7ed9 x \u8d4b\u503c\u4e4b\u95f4\u7684\u4ee3\u7801\u80af\u5b9a\u51fa\u4e86\u95ee\u9898\u3002\u5728\u51fd\u6570 getUserInput \u4e2d\u6dfb\u52a0\u4e00\u4e9b\u8c03\u8bd5\u4ee3\u7801\u6765\uff0c\u786e\u5b9a\u7a0b\u5e8f\u662f\u5426\u4ece\u7528\u6237\u90a3\u91cc\u83b7\u5f97\u4e86\u6b63\u786e\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include int add ( int x , int y ) { std :: cerr << \"add() called (x=\" << x << \", y=\" << y << \") \\n \" ; return x + y ; } void printResult ( int z ) { std :: cerr << \"printResult() called (z=\" << z << \") \\n \" ; std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cerr << \"getUserInput() called \\n \" ; std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; std :: cerr << \"getUserInput::x = \" << x << '\\n' ; // \u6dfb\u52a0\u8c03\u8bd5\u4fe1\u606f return -- x ; } int main () { std :: cerr << \"main() called \\n \" ; int x { getUserInput () }; std :: cerr << \"main::x = \" << x << '\\n' ; int y { getUserInput () }; std :: cerr << \"main::y = \" << y << '\\n' ; int z { add ( x , y ) }; std :: cerr << \"main::z = \" << z << '\\n' ; printResult ( z ); return 0 ; } \u8f93\u51fa\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 main() called getUserInput() called Enter a number: 4 getUserInput::x = 4 main::x = 3 getUserInput() called Enter a number: 3 getUserInput::x = 3 main::y = 2 add() called (x=3, y=2) main::z = 5 printResult() called (z=5) The answer is: 5 \u6dfb\u52a0\u8fd9\u884c\u8c03\u8bd5\u4ee3\u7801\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u7528\u6237\u8f93\u5165\u88ab\u6b63\u786e\u5730\u63a5\u6536\u5230 getUserInput() \u7684\u53d8\u91cf x \u4e2d\u3002\u4f46\u662f\u53d8\u91cf x \u5374\u5f97\u5230\u4e86\u9519\u8bef\u7684\u503c\u3002\u95ee\u9898\u80af\u5b9a\u5728\u8fd9\u4e24\u70b9\u4e4b\u95f4\u3002\u552f\u4e00\u7684\u7f6a\u9b41\u7978\u9996\u662f\u51fd\u6570 getUserInput() \u7684\u8fd4\u56de\u503c\uff1a 1 return -- x ; \u55ef\uff0c\u8fd9\u5f88\u5947\u602a\u3002 x \u524d\u9762\u7684\u201c -- \u201d\u7b26\u53f7\u662f\u4ec0\u4e48\uff1f\u7531\u4e8e\u6211\u4eec\u7684\u6559\u7a0b\u8fd8\u6ca1\u6709\u8bb2\u5230\u8fd9\u4e00\u70b9\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u5b83\u7684\u610f\u601d\uff0c\u4e5f\u4e0d\u7528\u62c5\u5fc3\u3002\u4f46\u662f\uff0c\u5373\u4f7f\u4e0d\u77e5\u9053\u5b83\u662f\u4ec0\u4e48\u610f\u601d\uff0c\u901a\u8fc7\u8c03\u8bd5\u5de5\u4f5c\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5408\u7406\u5730\u786e\u5b9a\u8fd9\u4e00\u884c\u6709\u95ee\u9898\u2014\u2014\u56e0\u6b64\uff0c\u5f88\u53ef\u80fd\u662f\u8fd9\u4e2a\u201c -- \u201d\u7b26\u53f7\u5bfc\u81f4\u4e86\u95ee\u9898\u3002 \u56e0\u4e3a\u6211\u4eec\u5e0c\u671b getUserInput \u53ea\u8fd4\u56de x \u7684\u503c\uff0c\u6240\u4ee5\u5e72\u8106\u628a -- \u5220\u4e86\u8bd5\u8bd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include int add ( int x , int y ) { std :: cerr << \"add() called (x=\" << x << \", y=\" << y << \") \\n \" ; return x + y ; } void printResult ( int z ) { std :: cerr << \"printResult() called (z=\" << z << \") \\n \" ; std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cerr << \"getUserInput() called \\n \" ; std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; std :: cerr << \"getUserInput::x = \" << x << '\\n' ; return x ; // removed -- before x } int main () { std :: cerr << \"main() called \\n \" ; int x { getUserInput () }; std :: cerr << \"main::x = \" << x << '\\n' ; int y { getUserInput () }; std :: cerr << \"main::y = \" << y << '\\n' ; int z { add ( x , y ) }; std :: cerr << \"main::z = \" << z << '\\n' ; printResult ( z ); return 0 ; } \u8f93\u51fa\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 main() called getUserInput() called Enter a number: 4 getUserInput::x = 4 main::x = 4 getUserInput() called Enter a number: 3 getUserInput::x = 3 main::y = 3 add() called (x=4, y=3) main::z = 7 printResult() called (z=7) The answer is: 7 \u7a0b\u5e8f\u73b0\u5728\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\uff0c\u867d\u7136\u6211\u4eec\u4e0d\u77e5\u9053 -- \u662f\u5e72\u561b\u7684\uff0c\u4f46\u662f\u8fd9\u5e76\u4e0d\u5f71\u54cd\u6211\u4eec\u5b9a\u4f4d\u548c\u4fee\u590d\u8fd9\u4e2a\u95ee\u9898\u3002 \u4e3a\u4ec0\u4e48\u4f7f\u7528\u6253\u5370\u8bed\u53e5\u6765\u8c03\u8bd5\u7a0b\u5e8f\u5e76\u4e0d\u662f\u4e0a\u7b56 \u867d\u7136\u4e3a\u8bca\u65ad\u76ee\u7684\u5411\u7a0b\u5e8f\u6dfb\u52a0\u8c03\u8bd5\u8bed\u53e5\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u57fa\u672c\u6280\u672f\uff0c\u4e5f\u5f88\u6709\u7528(\u7279\u522b\u662f\u5f53\u8c03\u8bd5\u5668\u7531\u4e8e\u67d0\u4e9b\u539f\u56e0\u4e0d\u53ef\u7528\u65f6)\uff0c\u4f46\u7531\u4e8e\u4ee5\u4e0b\u51e0\u4e2a\u539f\u56e0\uff0c\u5b83\u5e76\u4e0d\u662f\u5f88\u597d: \u8c03\u8bd5\u8bed\u53e5\u4f7f\u4ee3\u7801\u53d8\u5f97\u6df7\u4e71\uff1b \u8c03\u8bd5\u8bed\u53e5\u4f7f\u7a0b\u5e8f\u7684\u8f93\u51fa\u53d8\u5f97\u6df7\u4e71\uff1b \u8c03\u8bd5\u8bed\u53e5\u5728\u4f7f\u7528\u5b8c\u4e4b\u540e\u5fc5\u987b\u5220\u9664\uff0c\u8fd9\u4f7f\u5f97\u5b83\u4eec\u4e0d\u53ef\u91cd\u7528\uff1b \u8c03\u8bd5\u8bed\u53e5\u9700\u8981\u4fee\u6539\u4ee3\u7801\u6765\u6dfb\u52a0\u548c\u5220\u9664\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5f15\u5165\u65b0\u7684\u9519\u8bef\u3002 \u4e0b\u8282\u8bfe\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u4e00\u79cd\u66f4\u597d\u7684\u529e\u6cd5\u6765\u8c03\u8bd5\u7a0b\u5e8f\u3002","title":"3.4 \u2014 \u57fa\u672c\u4ee3\u7801\u8c03\u8bd5\u6280\u672f"},{"location":"3-4-Basic-debugging-tactics/#debug-1","text":"\u5148\u4ece\u4e00\u4e2a\u7b80\u5355\u7684\u95ee\u9898\u5f00\u59cb\u3002\u5982\u679c\u7a0b\u5e8f\u8868\u73b0\u51fa\u9519\u8bef\u7684\u884c\u4e3a\uff0c\u6211\u4eec\u53ef\u4ee5\u6ce8\u91ca\u6389\u4e00\u4e9b\u4ee3\u7801\uff0c\u770b\u770b\u95ee\u9898\u662f\u5426\u4ecd\u7136\u5b58\u5728\u3002\u5982\u679c\u95ee\u9898\u4ecd\u7136\u5b58\u5728\uff0c\u5219\u6ce8\u91ca\u6389\u7684\u4ee3\u7801\u80af\u5b9a\u6ca1\u6709\u95ee\u9898\u3002 \u8003\u8651\u4ee5\u4e0b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 int main () { getNames (); // ask user to enter a bunch of names doMaintenance (); // do some random stuff sortNames (); // sort them in alphabetical order printNames (); // print the sorted list of names return 0 ; } \u5047\u8bbe\u8fd9\u4e2a\u7a0b\u5e8f\u5e94\u8be5\u6309\u5b57\u6bcd\u987a\u5e8f\u6253\u5370\u7528\u6237\u8f93\u5165\u7684\u540d\u5b57\uff0c\u4f46\u5b83\u5374\u6309\u76f8\u53cd\u7684\u5b57\u6bcd\u987a\u5e8f\u6253\u5370\u3002\u95ee\u9898\u5728\u54ea\u91cc? getNames \u662f\u5426\u8f93\u5165\u4e86\u9519\u8bef\u7684\u540d\u79f0\uff1f sortNames \u662f\u5426\u5c06\u5b83\u4eec\u53cd\u5411\u6392\u5e8f\uff1f\u8fd8\u662f printNames \u6253\u5370\u7684\u987a\u5e8f\u4e0d\u5bf9\uff1f\u4ee5\u4e0a\u4efb\u4f55\u4e00\u79cd\u731c\u6d4b\u90fd\u6709\u53ef\u80fd\uff0c\u4f46\u662f domainmaintenance() \u591a\u534a\u4e0e\u95ee\u9898\u65e0\u5173\uff0c\u6240\u4ee5\u53ef\u4ee5\u5148\u5c06\u5176\u6ce8\u91ca\u6389\u3002 1 2 3 4 5 6 7 8 9 int main () { getNames (); // ask user to enter a bunch of names // doMaintenance(); // do some random stuff sortNames (); // sort them in alphabetical order printNames (); // print the sorted list of names return 0 ; } \u5982\u679c\u95ee\u9898\u6d88\u5931\u4e86\uff0c\u90a3\u4e48 domainance \u53cd\u5012\u662f\u6709\u95ee\u9898\u7684\uff0c\u5230\u65f6\u5019\u518d\u6ce8\u610f\u529b\u96c6\u4e2d\u5728\u90a3\u91cc\u3002 \u4e0d\u8fc7\uff0c\u95ee\u9898\u5f88\u53ef\u80fd\u8fd8\u4f1a\u5b58\u5728\uff0c\u56e0\u6b64 doMaintenance \u4e0d\u662f\u9519\u8bef\u7684\u6839\u6e90\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u641c\u7d22\u4e2d\u6392\u9664\u6574\u4e2a\u51fd\u6570\u3002\u8fd9\u5e76\u4e0d\u80fd\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u5b9e\u9645\u95ee\u9898\u662f\u53d1\u751f\u5728 doMaintenance \u8c03\u7528\u4e4b\u524d\u8fd8\u662f\u4e4b\u540e\uff0c\u4f46\u7ec8\u5f52\u662f\u7f29\u5c0f\u4e86\u8303\u56f4\u3002 \u4e0d\u8981\u5fd8\u8bb0\u6ce8\u91ca\u6389\u4e86\u54ea\u4e9b\u51fd\u6570\uff0c\u4ee5\u4fbf\u4ee5\u540e\u53ef\u4ee5\u53d6\u6d88\u6ce8\u91ca!","title":"Debug \u6280\u672f 1\uff1a\u6ce8\u91ca\u6389\u4ee3\u7801"},{"location":"3-4-Basic-debugging-tactics/#debug-2","text":"\u590d\u6742\u7a0b\u5e8f\u4e2d\u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u662f\u7a0b\u5e8f\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u7684\u6b21\u6570\u592a\u591a\u6216\u8fc7\u5c11(\u5305\u62ec\u6839\u672c\u4e0d\u8c03\u7528)\u3002 \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5c06\u8bed\u53e5\u653e\u5728\u51fd\u6570\u7684\u9876\u90e8\u4ee5\u6253\u5370\u51fd\u6570\u540d\u4f1a\u5f88\u6709\u5e2e\u52a9\u3002\u8fd9\u6837\uff0c\u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u5c31\u53ef\u4ee5\u6253\u5370\u51fa\u8c03\u7528\u4e86\u54ea\u4e9b\u51fd\u6570\u3002 \u5c0f\u8d34\u58eb \u5f53\u6253\u5370\u7528\u4e8e\u8c03\u8bd5\u76ee\u7684\u7684\u4fe1\u606f\u65f6\uff0c\u4f7f\u7528 std::cerr \u800c\u4e0d\u662f std::cout \u3002\u8fd9\u6837\u505a\u7684\u4e00\u4e2a\u539f\u56e0\u662f std::cout \u53ef\u80fd\u4f1a\u88ab\u7f13\u51b2\uff0c\u8fd9\u610f\u5473\u7740\u5728\u8981\u6c42 std::cout \u6253\u5370\u4fe1\u606f\u548c\u5b83\u5b9e\u9645\u6253\u5370\u4fe1\u606f\u4e4b\u95f4\u53ef\u80fd\u4f1a\u6709\u4e00\u4e2a\u65f6\u95f4\u5dee\u3002\u5982\u679c\u4f7f\u7528 std::cout \u8fdb\u884c\u6253\u5370\u7136\u540e\u7a0b\u5e8f\u7acb\u5373\u5d29\u6e83\uff0c\u90a3\u4e48 std::cout \u53ef\u80fd\u8fd8\u6ca1\u6709\u6765\u5f97\u53ca\u6253\u5370\u3002\u8fd9\u53ef\u80fd\u4f1a\u8bef\u5bfc\u4f60\u3002\u53e6\u4e00\u65b9\u9762\uff0c std::cerr \u662f\u65e0\u7f13\u51b2\u7684\uff0c\u8fd9\u610f\u5473\u7740\u53d1\u9001\u7ed9\u5b83\u7684\u4efb\u4f55\u5185\u5bb9\u90fd\u5c06\u7acb\u5373\u6253\u5370\u3002\u8fd9\u6709\u52a9\u4e8e\u786e\u4fdd\u6240\u6709\u8c03\u8bd5\u8f93\u51fa\u5c3d\u5feb\u51fa\u73b0(\u4ee5\u4e00\u4e9b\u6027\u80fd\u4e3a\u4ee3\u4ef7\uff0c\u6211\u4eec\u5728\u8c03\u8bd5\u65f6\u901a\u5e38\u4e0d\u5173\u7cfb\u6027\u80fd)\u3002 \u4f7f\u7528 std::cerr \u4e5f\u53ef\u4ee5\u8868\u660e\u8be5\u6253\u5370\u4fe1\u606f\u548c\u9519\u8bef\u6d41\u7a0b\u6709\u5173\u3002 \u8003\u8651\u4ee5\u4e0b\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int getValue () { return 4 ; } int main () { std :: cout << getValue ; return 0 ; } \u4f60\u5fc5\u987b\u5173\u95ed \u201c\u5c06\u8b66\u544a\u5f53\u505a\u9519\u8bef\u5904\u7406\u201d \u7684\u7f16\u8bd1\u9009\u9879\uff0c\u624d\u80fd\u7f16\u8bd1\u8be5\u4ee3\u7801\u3002 \u867d\u7136\u6211\u4eec\u671f\u671b\u8fd9\u4e2a\u7a0b\u5e8f\u6253\u5370\u503c 4\uff0c\u4f46\u5b83\u5b9e\u9645\u6253\u5370\u4e86 1 \u3002 \u5728 Visual Studio (\u6216\u8005\u5176\u4ed6\u7f16\u8bd1\u5668)\u4e0a\uff0c\u4e5f\u53ef\u80fd\u6253\u5370\uff1a 1 00101424 \u6dfb\u52a0\u4e00\u4e9b\u8c03\u8bd5\u8bed\u53e5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int getValue () { std :: cerr << \"getValue() called \\n \" ; return 4 ; } int main () { std :: cerr << \"main() called \\n \" ; std :: cout << getValue ; return 0 ; } \u5c0f\u8d34\u58eb \u5728\u6dfb\u52a0\u4e34\u65f6\u8c03\u8bd5\u8bed\u53e5\u65f6\uff0c\u4e0d\u7f29\u8fdb\u4f1a\u5f88\u6709\u5e2e\u52a9\u3002\u8fd9\u6837\u5728\u4e0d\u9700\u8981\u65f6\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u627e\u5230\u5b83\u4eec\u5e76\u5220\u9664\u3002 \u73b0\u5728\uff0c\u51fd\u6570\u6267\u884c\u65f6\u4f1a\u6253\u5370\u5bf9\u5e94\u7684\u51fd\u6570\u540d\uff1a 1 2 main () called 1 \u53ef\u4ee5\u770b\u5230 getValue \u6839\u672c\u6ca1\u88ab\u8c03\u7528\u8fc7\u3002\u90a3\u4e48\u80af\u5b9a\u662f\u8c03\u7528\u8be5\u51fd\u6570\u7684\u4ee3\u7801\u51fa\u4e86\u95ee\u9898\u3002\u4ed4\u7ec6\u770b\u770b\u90a3\u884c\u4ee3\u7801\uff1a 1 std :: cout << getValue ; \u54e6\uff0c\u770b\uff0c\u6211\u4eec\u5fd8\u8bb0\u4e86\u51fd\u6570\u8c03\u7528\u7684\u62ec\u53f7\u3002\u5b83\u5e94\u8be5\u662f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int getValue () { std :: cerr << \"getValue() called \\n \" ; return 4 ; } int main () { std :: cerr << \"main() called \\n \" ; std :: cout << getValue (); // added parenthesis here return 0 ; } \u8fd9\u6837\u4e00\u6765\u5c31\u6ca1\u95ee\u9898\u4e86\uff1a 1 2 3 main() called getValue() called 4 \u6b64\u65f6\u53ef\u4ee5\u5220\u9664\u4e34\u65f6\u8c03\u8bd5\u8bed\u53e5\u4e86\u3002","title":"Debug \u6280\u672f 2\uff1a\u9a8c\u8bc1\u4ee3\u7801\u6d41\u7a0b"},{"location":"3-4-Basic-debugging-tactics/#debug-3","text":"\u5bf9\u4e8e\u67d0\u4e9b\u7c7b\u578b\u7684\u9519\u8bef\uff0c\u7a0b\u5e8f\u53ef\u80fd\u8ba1\u7b97\u6216\u4f20\u9012\u4e86\u9519\u8bef\u7684\u503c\u3002 \u6211\u4eec\u8fd8\u53ef\u4ee5\u8f93\u51fa\u53d8\u91cf(\u5305\u62ec\u53c2\u6570)\u6216\u8868\u8fbe\u5f0f\u7684\u503c\uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u662f\u6b63\u786e\u7684\u3002 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff0c\u5b83\u5e94\u8be5\u628a\u4e24\u4e2a\u6570\u5b57\u76f8\u52a0\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include int add ( int x , int y ) { return x + y ; } void printResult ( int z ) { std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { int x { getUserInput () }; int y { getUserInput () }; std :: cout << x << \" + \" << y << '\\n' ; int z { add ( x , 5 ) }; printResult ( z ); return 0 ; } \u8f93\u51fa\u7ed3\u679c\u4e0d\u6b63\u786e\uff1a 1 2 3 4 Enter a number: 4 Enter a number: 3 4 + 3 The answer is: 9 \u7ed3\u679c\u4e0d\u5bf9\uff0c\u4f46\u4f60\u80fd\u53d1\u73b0\u9519\u8bef\u5417\uff1f\u5373\u4f7f\u5728\u8fd9\u4e2a\u7b80\u77ed\u7684\u7a0b\u5e8f\u4e2d\uff0c\u95ee\u9898\u4e5f\u5f88\u96be\u88ab\u53d1\u73b0\u3002\u63a5\u4e0b\u6765\u6dfb\u52a0\u4e00\u4e9b\u4ee3\u7801\u6765\u6253\u5370\u53d8\u91cf\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include int add ( int x , int y ) { return x + y ; } void printResult ( int z ) { std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { int x { getUserInput () }; std :: cerr << \"main::x = \" << x << '\\n' ; int y { getUserInput () }; std :: cerr << \"main::y = \" << y << '\\n' ; std :: cout << x << \" + \" << y << '\\n' ; int z { add ( x , 5 ) }; std :: cerr << \"main::z = \" << z << '\\n' ; printResult ( z ); return 0 ; } \u8f93\u51fa\uff1a 1 2 3 4 5 6 7 Enter a number: 4 main::x = 4 Enter a number: 3 main::y = 3 4 + 3 main::z = 9 The answer is: 9 \u53d8\u91cf x \u548c y \u7684\u503c\u90fd\u662f\u6b63\u786e\u7684\uff0c\u4f46\u662f z \u7684\u503c\u4e0d\u6b63\u786e\u3002\u6240\u4ee5\u95ee\u9898\u80af\u5b9a\u5728\u8fd9\u9644\u8fd1\uff0c\u8fd9\u6837\u4e00\u6765 add \u51fd\u6570\u5c31\u975e\u5e38\u53ef\u7591\u4e86\u3002 \u4fee\u6539\u4e00\u4e0b add \u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include int add ( int x , int y ) { std :: cerr << \"add() called (x=\" << x << \", y=\" << y << \") \\n \" ; return x + y ; } void printResult ( int z ) { std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { int x { getUserInput () }; std :: cerr << \"main::x = \" << x << '\\n' ; int y { getUserInput () }; std :: cerr << \"main::y = \" << y << '\\n' ; std :: cout << x << \" + \" << y << '\\n' ; int z { add ( x , 5 ) }; std :: cerr << \"main::z = \" << z << '\\n' ; printResult ( z ); return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 5 6 7 Enter a number: 4 main::x = 4 Enter a number: 3 main::y = 3 add() called (x=4, y=5) main::z = 9 The answer is: 9 \u53d8\u91cf y \u7684\u503c\u662f3\uff0c\u4f46\u662f\u6211\u4eec\u7684\u51fd\u6570 add \u7684\u53c2\u6570 y \u7684\u503c\u662f 5 \u3002\u6211\u4eec\u80af\u5b9a\u4f20\u9012\u4e86\u9519\u8bef\u7684\u53c2\u6570\u3002\u679c\u7136\uff1a 1 int z { add ( x , 5 ) }; \u627e\u5230\u4e86\uff01\u5728\u8c03\u7528 add \u51fd\u6570\u7684\u65f6\u5019\u6211\u4eec\u4f20\u9012\u4e86\u5b57\u9762\u91cf 5 \u800c\u4e0d\u662f\u53d8\u91cf y \u3002\u4fee\u6539\u8fd9\u4e2a\u95ee\u9898\u53ea\u9700\u8981\u628a\u8fd9\u91cc\u7684\u5b9e\u53c2\u6539\u6389\u5c31\u53ef\u4ee5\u4e86\u3002 \u518d\u770b\u4e00\u4e2a\u4f8b\u5b50\uff1a \u8fd9\u4e2a\u7a0b\u5e8f\u4e0e\u524d\u9762\u7684\u7a0b\u5e8f\u975e\u5e38\u76f8\u4f3c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include int add ( int x , int y ) { return x + y ; } void printResult ( int z ) { std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return -- x ; } int main () { int x { getUserInput () }; int y { getUserInput () }; int z { add ( x , y ) }; printResult ( z ); return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 Enter a number: 4 Enter a number: 3 The answer is: 5 \u7ed3\u679c\u4e0d\u5bf9\uff0c\u4ec0\u4e48\u539f\u56e0\u5462\uff1f \u8ba9\u6211\u4eec\u5bf9\u8fd9\u6bb5\u4ee3\u7801\u8fdb\u884c\u4e00\u4e9b\u8c03\u8bd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include int add ( int x , int y ) { std :: cerr << \"add() called (x=\" << x << \", y=\" << y << \") \\n \" ; return x + y ; } void printResult ( int z ) { std :: cerr << \"printResult() called (z=\" << z << \") \\n \" ; std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cerr << \"getUserInput() called \\n \" ; std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return -- x ; } int main () { std :: cerr << \"main() called \\n \" ; int x { getUserInput () }; std :: cerr << \"main::x = \" << x << '\\n' ; int y { getUserInput () }; std :: cerr << \"main::y = \" << y << '\\n' ; int z { add ( x , y ) }; std :: cerr << \"main::z = \" << z << '\\n' ; printResult ( z ); return 0 ; } \u73b0\u5728\u8ba9\u6211\u4eec\u7528\u76f8\u540c\u7684\u8f93\u5165\u518d\u6b21\u8fd0\u884c\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 main() called getUserInput() called Enter a number: 4 main::x = 3 getUserInput() called Enter a number: 3 main::y = 2 add() called (x=3, y=2) main::z = 5 printResult() called (z=5) The answer is: 5 \u8fd9\u6837\u4e00\u6765\u5c31\u53ef\u4ee5\u770b\u51fa\u95ee\u9898\u4e86\uff1a\u7528\u6237\u8f93\u5165\u7684\u503c\u662f 4 \uff0c\u4f46\u662f main \u51fd\u6570\u4e2d x \u5f97\u5230\u7684\u503c\u5374\u662f 3 \u3002\u5728\u7528\u6237\u8f93\u5165\u7684\u4f4d\u7f6e\u548c\u7ed9 x \u8d4b\u503c\u4e4b\u95f4\u7684\u4ee3\u7801\u80af\u5b9a\u51fa\u4e86\u95ee\u9898\u3002\u5728\u51fd\u6570 getUserInput \u4e2d\u6dfb\u52a0\u4e00\u4e9b\u8c03\u8bd5\u4ee3\u7801\u6765\uff0c\u786e\u5b9a\u7a0b\u5e8f\u662f\u5426\u4ece\u7528\u6237\u90a3\u91cc\u83b7\u5f97\u4e86\u6b63\u786e\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include int add ( int x , int y ) { std :: cerr << \"add() called (x=\" << x << \", y=\" << y << \") \\n \" ; return x + y ; } void printResult ( int z ) { std :: cerr << \"printResult() called (z=\" << z << \") \\n \" ; std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cerr << \"getUserInput() called \\n \" ; std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; std :: cerr << \"getUserInput::x = \" << x << '\\n' ; // \u6dfb\u52a0\u8c03\u8bd5\u4fe1\u606f return -- x ; } int main () { std :: cerr << \"main() called \\n \" ; int x { getUserInput () }; std :: cerr << \"main::x = \" << x << '\\n' ; int y { getUserInput () }; std :: cerr << \"main::y = \" << y << '\\n' ; int z { add ( x , y ) }; std :: cerr << \"main::z = \" << z << '\\n' ; printResult ( z ); return 0 ; } \u8f93\u51fa\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 main() called getUserInput() called Enter a number: 4 getUserInput::x = 4 main::x = 3 getUserInput() called Enter a number: 3 getUserInput::x = 3 main::y = 2 add() called (x=3, y=2) main::z = 5 printResult() called (z=5) The answer is: 5 \u6dfb\u52a0\u8fd9\u884c\u8c03\u8bd5\u4ee3\u7801\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u7528\u6237\u8f93\u5165\u88ab\u6b63\u786e\u5730\u63a5\u6536\u5230 getUserInput() \u7684\u53d8\u91cf x \u4e2d\u3002\u4f46\u662f\u53d8\u91cf x \u5374\u5f97\u5230\u4e86\u9519\u8bef\u7684\u503c\u3002\u95ee\u9898\u80af\u5b9a\u5728\u8fd9\u4e24\u70b9\u4e4b\u95f4\u3002\u552f\u4e00\u7684\u7f6a\u9b41\u7978\u9996\u662f\u51fd\u6570 getUserInput() \u7684\u8fd4\u56de\u503c\uff1a 1 return -- x ; \u55ef\uff0c\u8fd9\u5f88\u5947\u602a\u3002 x \u524d\u9762\u7684\u201c -- \u201d\u7b26\u53f7\u662f\u4ec0\u4e48\uff1f\u7531\u4e8e\u6211\u4eec\u7684\u6559\u7a0b\u8fd8\u6ca1\u6709\u8bb2\u5230\u8fd9\u4e00\u70b9\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u5b83\u7684\u610f\u601d\uff0c\u4e5f\u4e0d\u7528\u62c5\u5fc3\u3002\u4f46\u662f\uff0c\u5373\u4f7f\u4e0d\u77e5\u9053\u5b83\u662f\u4ec0\u4e48\u610f\u601d\uff0c\u901a\u8fc7\u8c03\u8bd5\u5de5\u4f5c\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5408\u7406\u5730\u786e\u5b9a\u8fd9\u4e00\u884c\u6709\u95ee\u9898\u2014\u2014\u56e0\u6b64\uff0c\u5f88\u53ef\u80fd\u662f\u8fd9\u4e2a\u201c -- \u201d\u7b26\u53f7\u5bfc\u81f4\u4e86\u95ee\u9898\u3002 \u56e0\u4e3a\u6211\u4eec\u5e0c\u671b getUserInput \u53ea\u8fd4\u56de x \u7684\u503c\uff0c\u6240\u4ee5\u5e72\u8106\u628a -- \u5220\u4e86\u8bd5\u8bd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include int add ( int x , int y ) { std :: cerr << \"add() called (x=\" << x << \", y=\" << y << \") \\n \" ; return x + y ; } void printResult ( int z ) { std :: cerr << \"printResult() called (z=\" << z << \") \\n \" ; std :: cout << \"The answer is: \" << z << '\\n' ; } int getUserInput () { std :: cerr << \"getUserInput() called \\n \" ; std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; std :: cerr << \"getUserInput::x = \" << x << '\\n' ; return x ; // removed -- before x } int main () { std :: cerr << \"main() called \\n \" ; int x { getUserInput () }; std :: cerr << \"main::x = \" << x << '\\n' ; int y { getUserInput () }; std :: cerr << \"main::y = \" << y << '\\n' ; int z { add ( x , y ) }; std :: cerr << \"main::z = \" << z << '\\n' ; printResult ( z ); return 0 ; } \u8f93\u51fa\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 main() called getUserInput() called Enter a number: 4 getUserInput::x = 4 main::x = 4 getUserInput() called Enter a number: 3 getUserInput::x = 3 main::y = 3 add() called (x=4, y=3) main::z = 7 printResult() called (z=7) The answer is: 7 \u7a0b\u5e8f\u73b0\u5728\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\uff0c\u867d\u7136\u6211\u4eec\u4e0d\u77e5\u9053 -- \u662f\u5e72\u561b\u7684\uff0c\u4f46\u662f\u8fd9\u5e76\u4e0d\u5f71\u54cd\u6211\u4eec\u5b9a\u4f4d\u548c\u4fee\u590d\u8fd9\u4e2a\u95ee\u9898\u3002","title":"Debug \u6280\u672f 3\uff1a \u6253\u5370\u503c"},{"location":"3-4-Basic-debugging-tactics/#_1","text":"\u867d\u7136\u4e3a\u8bca\u65ad\u76ee\u7684\u5411\u7a0b\u5e8f\u6dfb\u52a0\u8c03\u8bd5\u8bed\u53e5\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u57fa\u672c\u6280\u672f\uff0c\u4e5f\u5f88\u6709\u7528(\u7279\u522b\u662f\u5f53\u8c03\u8bd5\u5668\u7531\u4e8e\u67d0\u4e9b\u539f\u56e0\u4e0d\u53ef\u7528\u65f6)\uff0c\u4f46\u7531\u4e8e\u4ee5\u4e0b\u51e0\u4e2a\u539f\u56e0\uff0c\u5b83\u5e76\u4e0d\u662f\u5f88\u597d: \u8c03\u8bd5\u8bed\u53e5\u4f7f\u4ee3\u7801\u53d8\u5f97\u6df7\u4e71\uff1b \u8c03\u8bd5\u8bed\u53e5\u4f7f\u7a0b\u5e8f\u7684\u8f93\u51fa\u53d8\u5f97\u6df7\u4e71\uff1b \u8c03\u8bd5\u8bed\u53e5\u5728\u4f7f\u7528\u5b8c\u4e4b\u540e\u5fc5\u987b\u5220\u9664\uff0c\u8fd9\u4f7f\u5f97\u5b83\u4eec\u4e0d\u53ef\u91cd\u7528\uff1b \u8c03\u8bd5\u8bed\u53e5\u9700\u8981\u4fee\u6539\u4ee3\u7801\u6765\u6dfb\u52a0\u548c\u5220\u9664\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5f15\u5165\u65b0\u7684\u9519\u8bef\u3002 \u4e0b\u8282\u8bfe\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u4e00\u79cd\u66f4\u597d\u7684\u529e\u6cd5\u6765\u8c03\u8bd5\u7a0b\u5e8f\u3002","title":"\u4e3a\u4ec0\u4e48\u4f7f\u7528\u6253\u5370\u8bed\u53e5\u6765\u8c03\u8bd5\u7a0b\u5e8f\u5e76\u4e0d\u662f\u4e0a\u7b56"},{"location":"3-5-More-debugging-tactics/","tags":["debugging"],"text":"\u5728\u4e0a\u8282\u8bfe ( 3.4 \u2014 \u57fa\u672c\u4ee3\u7801\u8c03\u8bd5\u6280\u672f ) \u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u624b\u52a8\u8c03\u8bd5\u4ee3\u7801\u3002\u5728\u8fd9\u8282\u8bfe\u4e2d\u6211\u4eec\u63d0\u4f9b\u4e00\u79cd\u65b0\u7684\u65b9\u5f0f\uff0c\u5b83\u53ef\u4ee5\u907f\u514d\u8c03\u8bd5\u8bed\u53e5\u5e26\u6765\u7684\u95ee\u9898\uff1a \u8c03\u8bd5\u8bed\u53e5\u4f7f\u4ee3\u7801\u53d8\u5f97\u6df7\u4e71\uff1b \u8c03\u8bd5\u8bed\u53e5\u4f7f\u7a0b\u5e8f\u7684\u8f93\u51fa\u53d8\u5f97\u6df7\u4e71\uff1b \u8c03\u8bd5\u8bed\u53e5\u5728\u4f7f\u7528\u5b8c\u4e4b\u540e\u5fc5\u987b\u5220\u9664\uff0c\u8fd9\u4f7f\u5f97\u5b83\u4eec\u4e0d\u53ef\u91cd\u7528\uff1b \u8c03\u8bd5\u8bed\u53e5\u9700\u8981\u4fee\u6539\u4ee3\u7801\u6765\u6dfb\u52a0\u548c\u5220\u9664\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5f15\u5165\u65b0\u7684\u9519\u8bef\u3002 \u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u7d22\u8fd9\u6837\u505a\u7684\u4e00\u4e9b\u57fa\u672c\u6280\u5de7\u3002 \u4e3a\u8c03\u8bd5\u4ee3\u7801\u6dfb\u52a0\u6267\u884c\u6761\u4ef6 \u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int getUserInput () { std :: cerr << \"getUserInput() called \\n \" ; std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { std :: cerr << \"main() called \\n \" ; int x { getUserInput () }; std :: cout << \"You entered: \" << x ; return 0 ; } \u5f53\u5b8c\u6210\u95ee\u9898\u5b9a\u4f4d\u540e\uff0c \u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\u7684\u8c03\u8bd5\u8bed\u53e5\u90fd\u9700\u8981\u88ab\u5220\u6389\u6216\u8005\u6ce8\u91ca\u6389\u3002\u4e07\u4e00\u5c06\u6765\u4f60\u8fd8\u9700\u8981\u5728\u8fd9\u91cc\u5b9a\u4f4d\u95ee\u9898\uff0c\u90a3\u4e48\u8fd8\u5f97\u628a\u5b83\u4eec\u52a0\u56de\u6765\u6216\u8005\u653e\u5f00\u5b83\u4eec\u7684\u6ce8\u91ca\u3002 \u4e00\u79cd\u66f4\u5bb9\u6613\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u7981\u7528\u548c\u542f\u7528\u8c03\u8bd5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u9884\u5904\u7406\u5668\u6307\u4ee4\u4f7f\u8c03\u8bd5\u8bed\u53e5\u5177\u6709\u6761\u4ef6: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #define ENABLE_DEBUG // comment out to disable debugging int getUserInput () { #ifdef ENABLE_DEBUG std :: cerr << \"getUserInput() called \\n \" ; #endif std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { #ifdef ENABLE_DEBUG std :: cerr << \"main() called \\n \" ; #endif int x { getUserInput () }; std :: cout << \"You entered: \" << x ; return 0 ; } \u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u53ea\u9700\u8981\u6ce8\u91ca\u6216\u8005\u53d6\u6d88\u5bf9 #define ENABLE_DEBUG \u7684\u6ce8\u91ca\uff0c\u5373\u53ef\u91cd\u7528\u4e4b\u524d\u5168\u90e8\u7684\u8c03\u8bd5\u8bed\u53e5\uff0c\u800c\u4e0d\u9700\u8981\u4f9d\u6b21\u5bf9\u5176\u8fdb\u884c\u6dfb\u52a0\u548c\u5220\u9664\u3002\u5982\u679c\u4f60\u7684\u9879\u76ee\u4e2d\u5305\u542b\u591a\u4e2a\u6587\u4ef6\uff0c\u5219\u53ef\u4ee5\u628a #define ENABLE_DEBUG \u5b9a\u4e49\u5230\u5934\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u88ab\u5176\u4ed6\u6587\u4ef6\u5305\u542b\uff0c\u8fd9\u6837\u4e00\u6765\u5728\u4e00\u5904\u5bf9\u6b64\u5b8f\u5b9a\u4e49\u8fdb\u884c\u6ce8\u91ca\u5c31\u53ef\u4ee5\u5c06\u6548\u679c\u5e94\u7528\u5230\u5168\u90e8\u4ee3\u7801\u6587\u4ef6\u3002 \u8fd9\u89e3\u51b3\u4e86\u5fc5\u987b\u5220\u9664\u8c03\u8bd5\u8bed\u53e5\u7684\u95ee\u9898\u53ca\u5176\u6f5c\u5728\u7684\u98ce\u9669\uff0c\u4f46\u4ee3\u4ef7\u662f\u4ee3\u7801\u4f1a\u66f4\u52a0\u6df7\u4e71\u3002\u53e6\u4e00\u4e2a\u7f3a\u70b9\u662f\uff0c\u5982\u679c\u4f60\u72af\u4e86\u4e00\u4e2a\u62fc\u5199\u9519\u8bef(\u4f8b\u5982\u62fc\u9519\u4e86\u201cDEBUG '\u201d)\u6216\u5fd8\u8bb0\u5c06\u5934\u6587\u4ef6\u5305\u542b\u5230\u4e00\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\uff0c\u8be5\u6587\u4ef6\u7684\u90e8\u5206\u6216\u5168\u90e8\u8c03\u8bd5\u53ef\u80fd\u65e0\u6cd5\u542f\u7528\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u8fd9\u6bd4\u65e0\u6761\u4ef6\u7684\u7248\u672c\u66f4\u597d\uff0c\u4f46\u4ecd\u6709\u6539\u8fdb\u7684\u7a7a\u95f4\u3002 \u4f7f\u7528\u65e5\u5fd7 \u9664\u4e86\u901a\u8fc7\u9884\u5904\u7406\u5668\u8fdb\u884c\u6761\u4ef6\u5316\u8c03\u8bd5\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5c06\u8c03\u8bd5\u4fe1\u606f\u53d1\u9001\u5230\u65e5\u5fd7\u6587\u4ef6\u3002\u65e5\u5fd7\u6587\u4ef6\u662f\u8bb0\u5f55\u8f6f\u4ef6\u4e2d\u53d1\u751f\u7684\u4e8b\u4ef6\u7684\u6587\u4ef6(\u901a\u5e38\u5b58\u50a8\u5728\u78c1\u76d8\u4e0a)\u3002\u5c06\u4fe1\u606f\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\u7684\u8fc7\u7a0b\u79f0\u4e3a\u65e5\u5fd7\u8bb0\u5f55\u3002\u5927\u591a\u6570\u5e94\u7528\u7a0b\u5e8f\u548c\u64cd\u4f5c\u7cfb\u7edf\u90fd\u4f1a\u7f16\u5199\u65e5\u5fd7\u6587\u4ef6\uff0c\u7528\u4e8e\u5e2e\u52a9\u8bca\u65ad\u53d1\u751f\u7684\u95ee\u9898\u3002 \u56e0\u4e3a\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\u7684\u4fe1\u606f\u4e0e\u7a0b\u5e8f\u7684\u8f93\u51fa\u662f\u5206\u5f00\u7684\uff0c\u6240\u4ee5\u53ef\u4ee5\u907f\u514d\u6df7\u5408\u6b63\u5e38\u8f93\u51fa\u548c\u8c03\u8bd5\u8f93\u51fa\u6240\u9020\u6210\u7684\u6df7\u4e71\u3002\u65e5\u5fd7\u6587\u4ef6\u4e5f\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u53d1\u9001\u7ed9\u5176\u4ed6\u4eba\u8fdb\u884c\u8bca\u65ad\u2014\u2014\u56e0\u6b64\uff0c\u5982\u679c\u8f6f\u4ef6\u5ba2\u6237\u9047\u5230\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u8981\u6c42\u4ed6\u4eec\u5c06\u65e5\u5fd7\u6587\u4ef6\u53d1\u8fc7\u6765\uff0c\u8fd9\u53ef\u80fd\u6709\u52a9\u4e8e\u4e3a\u4f60\u63d0\u4f9b\u95ee\u9898\u7684\u7ebf\u7d22\u3002 \u867d\u7136\u4f60\u53ef\u4ee5\u81ea\u5df1\u7f16\u5199\u4ee3\u7801\u6765\u521b\u5efa\u65e5\u5fd7\u6587\u4ef6\u5e76\u5411\u5b83\u4eec\u53d1\u9001\u8f93\u51fa\uff0c\u4f46\u6211\u4eec\u8fd8\u662f\u63a8\u8350\u4f7f\u7528\u73b0\u6709\u7684\u8bb8\u591a\u7b2c\u4e09\u65b9\u65e5\u5fd7\u5de5\u5177\uff0c\u5177\u4f53\u7528\u54ea\u4e00\u4e2a\u53d6\u51b3\u4e8e\u4f60\u81ea\u5df1\u3002 \u6211\u4eec\u4f1a\u4f7f\u7528 plog \u65e5\u5fd7\u8bb0\u5f55\u5668\u8f93\u51fa\u5230\u65e5\u5fd7\u8bb0\u5f55\u5668\u7684\u6837\u5b50\u3002plog\u662f\u4e00\u4e2a\u7eaf\u5934\u6587\u4ef6\u5b9e\u73b0\u7684\u5e93\uff0c\u56e0\u6b64\u5f88\u5bb9\u6613\u5728\u4efb\u4f55\u9700\u8981\u5b83\u7684\u5730\u65b9\u5305\u542b\u5b83\uff0c\u800c\u4e14\u5b83\u662f\u8f7b\u91cf\u7ea7\u7684\uff0c\u6613\u4e8e\u4f7f\u7528\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include // Step 1: include the logger headers #include int getUserInput () { PLOGD << \"getUserInput() called\" ; // PLOGD is defined by the plog library std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { plog :: init ( plog :: debug , \"Logfile.txt\" ); // Step 2: initialize the logger PLOGD << \"main() called\" ; // Step 3: Output to the log as if you were writing to the console int x { getUserInput () }; std :: cout << \"You entered: \" << x ; return 0 ; } \u4ee3\u7801\u8f93\u51fa\u7684\u65e5\u5fd7\u5982\u4e0b\u6240\u793a(\u5728 Logfile.txt \u6587\u4ef6\u4e2d): 1 2 2018-12-26 20:03:33.295 DEBUG [4752] [main@14] main() called 2018-12-26 20:03:33.296 DEBUG [4752] [getUserInput@4] getUserInput() called \u5982\u4f55\u5305\u542b\u3001\u521d\u59cb\u5316\u548c\u4f7f\u7528\u8bb0\u5f55\u5668\u53d6\u51b3\u4e8e\u4f60\u9009\u62e9\u7684\u65e5\u5fd7\u5de5\u5177\u3002 \u6ce8\u610f\uff0c\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u4e0d\u9700\u8981\u6761\u4ef6\u7f16\u8bd1\u6307\u4ee4\uff0c\u56e0\u4e3a\u5927\u591a\u6570\u8bb0\u5f55\u5668\u90fd\u6709\u4e00\u79cd\u65b9\u6cd5\u6765\u51cf\u5c11/\u6d88\u9664\u5bf9\u65e5\u5fd7\u7684\u5199\u5165\u8f93\u51fa\u3002\u8fd9\u4f7f\u5f97\u4ee3\u7801\u66f4\u5bb9\u6613\u9605\u8bfb\uff0c\u56e0\u4e3a\u6761\u4ef6\u7f16\u8bd1\u884c\u589e\u52a0\u4e86\u5f88\u591a\u6df7\u4e71\u3002\u4f7f\u7528 plog \uff0c\u53ef\u4ee5\u901a\u8fc7\u66f4\u6539 init \u8bed\u53e5\u6765\u4e34\u65f6\u7981\u7528\u65e5\u5fd7\u8bb0\u5f55: 1 plog :: init ( plog :: none , \"Logfile.txt\" ); // plog::none eliminates writing of most messages, essentially turning logging off \u6211\u4eec\u4e0d\u4f1a\u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\u4f7f\u7528plog\uff0c\u6240\u4ee5\u4f60\u4e0d\u9700\u8981\u62c5\u5fc3\u5b66\u4e60\u5b83\u3002 \u9898\u5916\u8bdd \u5982\u679c\u4f60\u60f3\u81ea\u5df1\u7f16\u8bd1\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u6216\u8005\u5728\u4f60\u81ea\u5df1\u7684\u9879\u76ee\u4e2d\u4f7f\u7528plog\uff0c\u53ef\u4ee5\u6309\u7167\u8fd9\u4e9b\u8bf4\u660e\u6765\u5b89\u88c5\u5b83: \u9996\u5148\uff0c\u83b7\u53d6\u6700\u65b0\u7248\u672c\u7684 plog: - \u8bbf\u95ee plog \u7684 Github \u4ed3\u5e93\uff1b - \u70b9\u51fb\u53f3\u4e0a\u89d2\u7684\u7eff\u8272Code\u6309\u94ae\uff0c\u7136\u540e\u9009\u62e9\u201cDownload zip\u201d\u3002 \u63a5\u4e0b\u6765\uff0c\u5728\u7535\u8111\u4e0a\u89e3\u538b\u538b\u7f29\u5305\uff08\u5047\u8bbe\u662f somewhere \uff09\u3002 \u6700\u540e\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u9700\u8981\u4f7f\u7528\u5b83\u7684\u9879\u76ee\uff0c\u5728IDE\u7684 include directory \u8bbe\u7f6e somewhere\\plog-master\\include\\ \u3002\u5982\u679c\u4f60\u4f7f\u7528 Visual Studio \u53ef\u4ee5\u53c2\u8003\uff1a A.2 - \u5728visual studio\u4e2d\u4f7f\u7528\u5e93 \uff0c\u5982\u679c\u662fCode::Blocks\u5219\u53c2\u8003 A.3 - \u5728Code Blocks\u4e2d\u4f7f\u7528\u5e93 \u3002","title":"3.5 - \u66f4\u591a\u4ee3\u7801\u8c03\u8bd5\u6280\u672f"},{"location":"3-5-More-debugging-tactics/#_1","text":"\u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int getUserInput () { std :: cerr << \"getUserInput() called \\n \" ; std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { std :: cerr << \"main() called \\n \" ; int x { getUserInput () }; std :: cout << \"You entered: \" << x ; return 0 ; } \u5f53\u5b8c\u6210\u95ee\u9898\u5b9a\u4f4d\u540e\uff0c \u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\u7684\u8c03\u8bd5\u8bed\u53e5\u90fd\u9700\u8981\u88ab\u5220\u6389\u6216\u8005\u6ce8\u91ca\u6389\u3002\u4e07\u4e00\u5c06\u6765\u4f60\u8fd8\u9700\u8981\u5728\u8fd9\u91cc\u5b9a\u4f4d\u95ee\u9898\uff0c\u90a3\u4e48\u8fd8\u5f97\u628a\u5b83\u4eec\u52a0\u56de\u6765\u6216\u8005\u653e\u5f00\u5b83\u4eec\u7684\u6ce8\u91ca\u3002 \u4e00\u79cd\u66f4\u5bb9\u6613\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u7981\u7528\u548c\u542f\u7528\u8c03\u8bd5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u9884\u5904\u7406\u5668\u6307\u4ee4\u4f7f\u8c03\u8bd5\u8bed\u53e5\u5177\u6709\u6761\u4ef6: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #define ENABLE_DEBUG // comment out to disable debugging int getUserInput () { #ifdef ENABLE_DEBUG std :: cerr << \"getUserInput() called \\n \" ; #endif std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { #ifdef ENABLE_DEBUG std :: cerr << \"main() called \\n \" ; #endif int x { getUserInput () }; std :: cout << \"You entered: \" << x ; return 0 ; } \u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u53ea\u9700\u8981\u6ce8\u91ca\u6216\u8005\u53d6\u6d88\u5bf9 #define ENABLE_DEBUG \u7684\u6ce8\u91ca\uff0c\u5373\u53ef\u91cd\u7528\u4e4b\u524d\u5168\u90e8\u7684\u8c03\u8bd5\u8bed\u53e5\uff0c\u800c\u4e0d\u9700\u8981\u4f9d\u6b21\u5bf9\u5176\u8fdb\u884c\u6dfb\u52a0\u548c\u5220\u9664\u3002\u5982\u679c\u4f60\u7684\u9879\u76ee\u4e2d\u5305\u542b\u591a\u4e2a\u6587\u4ef6\uff0c\u5219\u53ef\u4ee5\u628a #define ENABLE_DEBUG \u5b9a\u4e49\u5230\u5934\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u88ab\u5176\u4ed6\u6587\u4ef6\u5305\u542b\uff0c\u8fd9\u6837\u4e00\u6765\u5728\u4e00\u5904\u5bf9\u6b64\u5b8f\u5b9a\u4e49\u8fdb\u884c\u6ce8\u91ca\u5c31\u53ef\u4ee5\u5c06\u6548\u679c\u5e94\u7528\u5230\u5168\u90e8\u4ee3\u7801\u6587\u4ef6\u3002 \u8fd9\u89e3\u51b3\u4e86\u5fc5\u987b\u5220\u9664\u8c03\u8bd5\u8bed\u53e5\u7684\u95ee\u9898\u53ca\u5176\u6f5c\u5728\u7684\u98ce\u9669\uff0c\u4f46\u4ee3\u4ef7\u662f\u4ee3\u7801\u4f1a\u66f4\u52a0\u6df7\u4e71\u3002\u53e6\u4e00\u4e2a\u7f3a\u70b9\u662f\uff0c\u5982\u679c\u4f60\u72af\u4e86\u4e00\u4e2a\u62fc\u5199\u9519\u8bef(\u4f8b\u5982\u62fc\u9519\u4e86\u201cDEBUG '\u201d)\u6216\u5fd8\u8bb0\u5c06\u5934\u6587\u4ef6\u5305\u542b\u5230\u4e00\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\uff0c\u8be5\u6587\u4ef6\u7684\u90e8\u5206\u6216\u5168\u90e8\u8c03\u8bd5\u53ef\u80fd\u65e0\u6cd5\u542f\u7528\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u8fd9\u6bd4\u65e0\u6761\u4ef6\u7684\u7248\u672c\u66f4\u597d\uff0c\u4f46\u4ecd\u6709\u6539\u8fdb\u7684\u7a7a\u95f4\u3002","title":"\u4e3a\u8c03\u8bd5\u4ee3\u7801\u6dfb\u52a0\u6267\u884c\u6761\u4ef6"},{"location":"3-5-More-debugging-tactics/#_2","text":"\u9664\u4e86\u901a\u8fc7\u9884\u5904\u7406\u5668\u8fdb\u884c\u6761\u4ef6\u5316\u8c03\u8bd5\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5c06\u8c03\u8bd5\u4fe1\u606f\u53d1\u9001\u5230\u65e5\u5fd7\u6587\u4ef6\u3002\u65e5\u5fd7\u6587\u4ef6\u662f\u8bb0\u5f55\u8f6f\u4ef6\u4e2d\u53d1\u751f\u7684\u4e8b\u4ef6\u7684\u6587\u4ef6(\u901a\u5e38\u5b58\u50a8\u5728\u78c1\u76d8\u4e0a)\u3002\u5c06\u4fe1\u606f\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\u7684\u8fc7\u7a0b\u79f0\u4e3a\u65e5\u5fd7\u8bb0\u5f55\u3002\u5927\u591a\u6570\u5e94\u7528\u7a0b\u5e8f\u548c\u64cd\u4f5c\u7cfb\u7edf\u90fd\u4f1a\u7f16\u5199\u65e5\u5fd7\u6587\u4ef6\uff0c\u7528\u4e8e\u5e2e\u52a9\u8bca\u65ad\u53d1\u751f\u7684\u95ee\u9898\u3002 \u56e0\u4e3a\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\u7684\u4fe1\u606f\u4e0e\u7a0b\u5e8f\u7684\u8f93\u51fa\u662f\u5206\u5f00\u7684\uff0c\u6240\u4ee5\u53ef\u4ee5\u907f\u514d\u6df7\u5408\u6b63\u5e38\u8f93\u51fa\u548c\u8c03\u8bd5\u8f93\u51fa\u6240\u9020\u6210\u7684\u6df7\u4e71\u3002\u65e5\u5fd7\u6587\u4ef6\u4e5f\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u53d1\u9001\u7ed9\u5176\u4ed6\u4eba\u8fdb\u884c\u8bca\u65ad\u2014\u2014\u56e0\u6b64\uff0c\u5982\u679c\u8f6f\u4ef6\u5ba2\u6237\u9047\u5230\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u8981\u6c42\u4ed6\u4eec\u5c06\u65e5\u5fd7\u6587\u4ef6\u53d1\u8fc7\u6765\uff0c\u8fd9\u53ef\u80fd\u6709\u52a9\u4e8e\u4e3a\u4f60\u63d0\u4f9b\u95ee\u9898\u7684\u7ebf\u7d22\u3002 \u867d\u7136\u4f60\u53ef\u4ee5\u81ea\u5df1\u7f16\u5199\u4ee3\u7801\u6765\u521b\u5efa\u65e5\u5fd7\u6587\u4ef6\u5e76\u5411\u5b83\u4eec\u53d1\u9001\u8f93\u51fa\uff0c\u4f46\u6211\u4eec\u8fd8\u662f\u63a8\u8350\u4f7f\u7528\u73b0\u6709\u7684\u8bb8\u591a\u7b2c\u4e09\u65b9\u65e5\u5fd7\u5de5\u5177\uff0c\u5177\u4f53\u7528\u54ea\u4e00\u4e2a\u53d6\u51b3\u4e8e\u4f60\u81ea\u5df1\u3002 \u6211\u4eec\u4f1a\u4f7f\u7528 plog \u65e5\u5fd7\u8bb0\u5f55\u5668\u8f93\u51fa\u5230\u65e5\u5fd7\u8bb0\u5f55\u5668\u7684\u6837\u5b50\u3002plog\u662f\u4e00\u4e2a\u7eaf\u5934\u6587\u4ef6\u5b9e\u73b0\u7684\u5e93\uff0c\u56e0\u6b64\u5f88\u5bb9\u6613\u5728\u4efb\u4f55\u9700\u8981\u5b83\u7684\u5730\u65b9\u5305\u542b\u5b83\uff0c\u800c\u4e14\u5b83\u662f\u8f7b\u91cf\u7ea7\u7684\uff0c\u6613\u4e8e\u4f7f\u7528\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include // Step 1: include the logger headers #include int getUserInput () { PLOGD << \"getUserInput() called\" ; // PLOGD is defined by the plog library std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; return x ; } int main () { plog :: init ( plog :: debug , \"Logfile.txt\" ); // Step 2: initialize the logger PLOGD << \"main() called\" ; // Step 3: Output to the log as if you were writing to the console int x { getUserInput () }; std :: cout << \"You entered: \" << x ; return 0 ; } \u4ee3\u7801\u8f93\u51fa\u7684\u65e5\u5fd7\u5982\u4e0b\u6240\u793a(\u5728 Logfile.txt \u6587\u4ef6\u4e2d): 1 2 2018-12-26 20:03:33.295 DEBUG [4752] [main@14] main() called 2018-12-26 20:03:33.296 DEBUG [4752] [getUserInput@4] getUserInput() called \u5982\u4f55\u5305\u542b\u3001\u521d\u59cb\u5316\u548c\u4f7f\u7528\u8bb0\u5f55\u5668\u53d6\u51b3\u4e8e\u4f60\u9009\u62e9\u7684\u65e5\u5fd7\u5de5\u5177\u3002 \u6ce8\u610f\uff0c\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u4e0d\u9700\u8981\u6761\u4ef6\u7f16\u8bd1\u6307\u4ee4\uff0c\u56e0\u4e3a\u5927\u591a\u6570\u8bb0\u5f55\u5668\u90fd\u6709\u4e00\u79cd\u65b9\u6cd5\u6765\u51cf\u5c11/\u6d88\u9664\u5bf9\u65e5\u5fd7\u7684\u5199\u5165\u8f93\u51fa\u3002\u8fd9\u4f7f\u5f97\u4ee3\u7801\u66f4\u5bb9\u6613\u9605\u8bfb\uff0c\u56e0\u4e3a\u6761\u4ef6\u7f16\u8bd1\u884c\u589e\u52a0\u4e86\u5f88\u591a\u6df7\u4e71\u3002\u4f7f\u7528 plog \uff0c\u53ef\u4ee5\u901a\u8fc7\u66f4\u6539 init \u8bed\u53e5\u6765\u4e34\u65f6\u7981\u7528\u65e5\u5fd7\u8bb0\u5f55: 1 plog :: init ( plog :: none , \"Logfile.txt\" ); // plog::none eliminates writing of most messages, essentially turning logging off \u6211\u4eec\u4e0d\u4f1a\u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\u4f7f\u7528plog\uff0c\u6240\u4ee5\u4f60\u4e0d\u9700\u8981\u62c5\u5fc3\u5b66\u4e60\u5b83\u3002 \u9898\u5916\u8bdd \u5982\u679c\u4f60\u60f3\u81ea\u5df1\u7f16\u8bd1\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u6216\u8005\u5728\u4f60\u81ea\u5df1\u7684\u9879\u76ee\u4e2d\u4f7f\u7528plog\uff0c\u53ef\u4ee5\u6309\u7167\u8fd9\u4e9b\u8bf4\u660e\u6765\u5b89\u88c5\u5b83: \u9996\u5148\uff0c\u83b7\u53d6\u6700\u65b0\u7248\u672c\u7684 plog: - \u8bbf\u95ee plog \u7684 Github \u4ed3\u5e93\uff1b - \u70b9\u51fb\u53f3\u4e0a\u89d2\u7684\u7eff\u8272Code\u6309\u94ae\uff0c\u7136\u540e\u9009\u62e9\u201cDownload zip\u201d\u3002 \u63a5\u4e0b\u6765\uff0c\u5728\u7535\u8111\u4e0a\u89e3\u538b\u538b\u7f29\u5305\uff08\u5047\u8bbe\u662f somewhere \uff09\u3002 \u6700\u540e\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u9700\u8981\u4f7f\u7528\u5b83\u7684\u9879\u76ee\uff0c\u5728IDE\u7684 include directory \u8bbe\u7f6e somewhere\\plog-master\\include\\ \u3002\u5982\u679c\u4f60\u4f7f\u7528 Visual Studio \u53ef\u4ee5\u53c2\u8003\uff1a A.2 - \u5728visual studio\u4e2d\u4f7f\u7528\u5e93 \uff0c\u5982\u679c\u662fCode::Blocks\u5219\u53c2\u8003 A.3 - \u5728Code Blocks\u4e2d\u4f7f\u7528\u5e93 \u3002","title":"\u4f7f\u7528\u65e5\u5fd7"},{"location":"3-6-Using-an-integrated-debugger-Stepping/","tags":["debugger"],"text":"\u5f53\u4f60\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u4ece main \u51fd\u6570\u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\uff0c\u7136\u540e\u9010\u6761\u6267\u884c\uff0c\u76f4\u5230\u7a0b\u5e8f\u7ed3\u675f\u3002\u5728\u7a0b\u5e8f\u8fd0\u884c\u7684\u4efb\u4f55\u65f6\u95f4\u70b9\uff0c\u7a0b\u5e8f\u90fd\u5728\u8ddf\u8e2a\u5f88\u591a\u4e8b\u60c5\uff1a\u4f60\u6b63\u5728\u4f7f\u7528\u7684\u53d8\u91cf\u7684\u503c\uff0c\u8c03\u7528\u4e86\u54ea\u4e9b\u51fd\u6570(\u8fd9\u6837\u5f53\u8fd9\u4e9b\u51fd\u6570\u8fd4\u56de\u65f6\uff0c\u7a0b\u5e8f\u5c31\u77e5\u9053\u8981\u8fd4\u56de\u5230\u54ea\u91cc)\uff0c\u4ee5\u53ca\u7a0b\u5e8f\u4e2d\u7684\u5f53\u524d\u6267\u884c\u70b9(\u8fd9\u6837\u5b83\u5c31\u77e5\u9053\u4e0b\u4e00\u6b65\u8981\u6267\u884c\u54ea\u6761\u8bed\u53e5)\u3002\u6240\u6709\u8fd9\u4e9b\u88ab\u8ddf\u8e2a\u7684\u4fe1\u606f\u90fd\u88ab\u79f0\u4e3a\u7a0b\u5e8f\u72b6\u6001(\u6216\u7b80\u79f0\u4e3a state )\u3002 \u5728\u524d\u9762\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u7684\u8c03\u8bd5\u65b9\u6cd5\u90fd\u9700\u8981\u5bf9\u4ee3\u7801\u8fdb\u884c\u4fee\u6539\uff0c\u4f8b\u5982\u6dfb\u52a0\u6253\u5370\u8bed\u53e5\u6216\u4f7f\u7528\u65e5\u5fd7\u5de5\u5177\u3002\u8fd9\u4e9b\u662f\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u68c0\u67e5\u7a0b\u5e8f\u72b6\u6001\u7684\u7b80\u5355\u65b9\u6cd5\u3002\u5c3d\u7ba1\u5982\u679c\u4f7f\u7528\u5f97\u5f53\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u662f\u6709\u6548\u7684\uff0c\u4f46\u4e5f\u6709\u7f3a\u70b9\uff1a\u5b83\u4eec\u9700\u8981\u4fee\u6539\u4ee3\u7801\uff0c\u8fd9\u9700\u8981\u65f6\u95f4\uff0c\u5e76\u53ef\u80fd\u5f15\u5165\u65b0\u7684\u9519\u8bef\uff0c\u5e76\u4e14\u5b83\u4eec\u4f1a\u4f7f\u4ee3\u7801\u53d8\u5f97\u6df7\u4e71\uff0c\u4f7f\u73b0\u6709\u7684\u4ee3\u7801\u66f4\u96be\u7406\u89e3\u3002 \u6211\u4eec\u4e4b\u6240\u4ee5\u8fd9\u4e48\u505a\uff0c\u662f\u56e0\u4e3a\u6211\u4eec\u57fa\u4e8e\u4e00\u4e2a\u5047\u8bbe\u2014\u2014\u5373\u4ee3\u7801\u4e00\u65e6\u5f00\u59cb\u8fd0\u884c\uff0c\u7a0b\u5e8f\u5c31\u4f1a\u8fd0\u884c\u5230\u7ed3\u675f(\u53ea\u4f1a\u5728\u63a5\u53d7\u8f93\u5165\u65f6\u6682\u505c)\uff0c\u6211\u4eec\u6ca1\u6709\u673a\u4f1a\u5728\u4efb\u4f55\u6211\u4eec\u60f3\u8981\u7684\u65f6\u5019\u8fdb\u884c\u5e72\u9884\u548c\u68c0\u67e5\u7a0b\u5e8f\u7684\u7ed3\u679c\u3002 \u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u80fd\u591f\u6d88\u9664\u8fd9\u79cd\u5047\u8bbe\u5462\uff1f\u5e78\u8fd0\u7684\u662f\uff0c\u5927\u591a\u6570\u73b0\u4ee3IDE\u90fd\u63d0\u4f9b\u4e86\u4e00\u79cd\u79f0\u4e3a\u8c03\u8bd5\u5668\u7684\u96c6\u6210\u5de5\u5177\uff0c\u8be5\u5de5\u5177\u6b63\u662f\u4e3a\u6b64\u800c\u8bbe\u8ba1\u7684\u3002 \u8c03\u8bd5\u5668 \u8c03\u8bd5\u5668\u662f\u4e00\u79cd\u8ba1\u7b97\u673a\u7a0b\u5e8f\uff0c\u5b83\u5141\u8bb8\u7a0b\u5e8f\u5458\u63a7\u5236\u53e6\u4e00\u4e2a\u7a0b\u5e8f\u7684\u6267\u884c\u65b9\u5f0f\uff0c\u5e76\u5728\u8be5\u7a0b\u5e8f\u8fd0\u884c\u65f6\u68c0\u67e5\u8be5\u7a0b\u5e8f\u7684\u72b6\u6001\u3002\u4f8b\u5982\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u4f7f\u7528\u8c03\u8bd5\u5668\u9010\u884c\u6267\u884c\u7a0b\u5e8f\uff0c\u5728\u6574\u4e2a\u8fc7\u7a0b\u4e2d\u68c0\u67e5\u53d8\u91cf\u7684\u503c\u3002\u901a\u8fc7\u5c06\u53d8\u91cf\u7684\u5b9e\u9645\u503c\u4e0e\u9884\u671f\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u6216\u8005\u89c2\u5bdf\u6574\u4e2a\u4ee3\u7801\u7684\u6267\u884c\u8def\u5f84\uff0c\u8c03\u8bd5\u5668\u53ef\u4ee5\u6781\u5927\u5730\u5e2e\u52a9\u8ddf\u8e2a\u8bed\u4e49(\u903b\u8f91)\u9519\u8bef\u3002 \u8c03\u8bd5\u5668\u6709\u4e24\u5927\u6838\u5fc3\u529f\u80fd\uff1a\u7cbe\u786e\u63a7\u5236\u7a0b\u5e8f\u6267\u884c\u7684\u80fd\u529b\uff0c\u4ee5\u53ca\u67e5\u770b(\u548c\u4fee\u6539\uff0c\u5982\u679c\u9700\u8981\u7684\u8bdd)\u7a0b\u5e8f\u72b6\u6001\u7684\u80fd\u529b\u3002 \u65e9\u671f\u7684\u8c03\u8bd5\u5668\uff0c\u5982 gdb \uff0c\u662f\u5177\u6709\u547d\u4ee4\u884c\u63a5\u53e3\u7684\u72ec\u7acb\u7a0b\u5e8f\uff0c\u7a0b\u5e8f\u5458\u5fc5\u987b\u952e\u5165\u795e\u79d8\u7684\u547d\u4ee4\u624d\u80fd\u4f7f\u5b83\u4eec\u5de5\u4f5c\u3002\u540e\u6765\u7684\u8c03\u8bd5\u5668(\u5982Borland\u7684[turbo debugger]\u7684\u65e9\u671f\u7248\u672c(https://en.wikipedia.org/wiki/Turbo_Debugger))\u4ecd\u7136\u662f\u72ec\u7acb\u7684\uff0c\u4f46\u6709\u81ea\u5df1\u7684\u201c\u56fe\u5f62\u5316\u201d\u524d\u7aef\uff0c\u80fd\u591f\u4f7f\u5176\u66f4\u52a0\u6613\u7528\u3002\u76ee\u524d\u5927\u591a\u6570\u73b0\u4ee3IDE\u90fd\u6709\u4e00\u4e2a\u96c6\u6210\u7684\u8c03\u8bd5\u5668\u2014\u2014\u4e5f\u5c31\u662f\u8bf4\uff0c\u8c03\u8bd5\u5668\u4f7f\u7528\u4e0e\u4ee3\u7801\u7f16\u8f91\u5668\u76f8\u540c\u7684\u63a5\u53e3\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u4f7f\u7528\u7f16\u5199\u4ee3\u7801\u65f6\u4f7f\u7528\u7684\u76f8\u540c\u73af\u5883\u8fdb\u884c\u8c03\u8bd5(\u800c\u4e0d\u5fc5\u5207\u6362\u7a0b\u5e8f)\u3002 \u51e0\u4e4e\u6240\u6709\u7684\u73b0\u4ee3\u8c03\u8bd5\u5668\u90fd\u5305\u542b\u76f8\u540c\u7684\u57fa\u672c\u529f\u80fd\u6807\u51c6\u96c6\u2014\u2014\u7136\u800c\uff0c\u5728\u8bbf\u95ee\u8fd9\u4e9b\u529f\u80fd\u7684\u83dc\u5355\u5982\u4f55\u4f7f\u7528\u65b9\u9762\u51e0\u4e4e\u6ca1\u6709\u4e00\u81f4\u6027\uff0c\u952e\u76d8\u5feb\u6377\u952e\u7684\u4e00\u81f4\u6027\u751a\u81f3\u66f4\u4f4e\u3002\u5c3d\u7ba1\u6211\u4eec\u7684\u793a\u4f8b\u5c06\u4f7f\u7528Microsoft Visual Studio\u7684\u622a\u56fe(\u6211\u4eec\u4e5f\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728Code::Blocks\u4e2d\u5b8c\u6210\u6240\u6709\u64cd\u4f5c)\uff0c\u4f46\u65e0\u8bba\u4f60\u4f7f\u7528\u7684\u662f\u54ea\u79cdIDE\uff0c\u90fd\u5e94\u8be5\u4e0d\u96be\u5f04\u6e05\u695a\u5982\u4f55\u8bbf\u95ee\u6211\u4eec\u8ba8\u8bba\u7684\u6bcf\u4e2a\u529f\u80fd\u3002 \u5c0f\u8d34\u58eb \u8c03\u8bd5\u5668\u952e\u76d8\u5feb\u6377\u952e\u53ea\u6709\u5728IDE/\u96c6\u6210\u8c03\u8bd5\u5668\u662f\u6d3b\u52a8\u7a97\u53e3\u65f6\u624d\u6709\u6548\u3002 \u672c\u7ae0\u7684\u5269\u4f59\u90e8\u5206\u5c06\u5b66\u4e60\u5982\u4f55\u4f7f\u7528\u8c03\u8bd5\u5668\u3002 \u5c0f\u8d34\u58eb \u4e0d\u8981\u5ffd\u89c6\u5b66\u4e60\u4f7f\u7528\u8c03\u8bd5\u5668\u3002\u968f\u7740\u7a0b\u5e8f\u53d8\u5f97\u8d8a\u6765\u8d8a\u590d\u6742\uff0c\u4e0e\u67e5\u627e\u548c\u4fee\u590d\u95ee\u9898\u6240\u8282\u7701\u7684\u65f6\u95f4\u76f8\u6bd4\uff0c\u5b66\u4e60\u6709\u6548\u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u6240\u82b1\u8d39\u7684\u65f6\u95f4\u5c31\u663e\u5f97\u5fae\u4e0d\u8db3\u9053\u4e86\u3002 \u6ce8\u610f Before proceeding with this lesson (and subsequent lessons related to using a debugger), make sure your project is compiled using a debug build configuration (see 0.9 -- Configuring your compiler: Build configurations for more information). If you\u2019re compiling your project using a release configuration instead, the functionality of the debugger may not work correctly (e.g. when you try to step into your program, it will just run the program instead). For Code::Blocks users If you\u2019re using Code::Blocks, your debugger may or may not be set up correctly. Let\u2019s check. First, go to Settings menu > Debugger\u2026 . Next, open the GDB/CDB debugger tree on the left, and choose Default . A dialog should open that looks something like this: If you see a big red bar where the \u201cExecutable path\u201d should be, then you need to locate your debugger. To do so, click the \u2026 button to the right of the Executable path_field. Next, find the \u201cgdb32.exe\u201d file on your system -- mine was in _C:\\Program Files (x86)\\CodeBlocks\\MinGW\\bin\\gdb32.exe . Then click OK . For Code::Blocks users There have been reports that the Code::Blocks integrated debugger (GDB) can have issues recognizing some file paths that contain spaces or non-English characters in them. If the debugger appears to be malfunctioning as you go through these lessons, that could be a reason why. \u5355\u6b65\u8c03\u8bd5 We\u2019re going to start our exploration of the debugger by first examining some of the debugging tools that allow us to control the way a program executes. Stepping is the name for a set of related debugger features that let us execute (step through) our code statement by statement. There are a number of related stepping commands that we\u2019ll cover in turn. Step into The step into command executes the next statement in the normal execution path of the program, and then pauses execution of the program so we can examine the program\u2019s state using the debugger. If the statement being executed contains a function call, step into causes the program to jump to the top of the function being called, where it will pause. Let\u2019s take a look at a very simple program: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void printValue ( int value ) { std :: cout << value << '\\n' ; } int main () { printValue ( 5 ); return 0 ; } Let\u2019s debug this program using the step into command. First, locate and then execute the step into debug command once. For Visual Studio users In Visual Studio, the step into command can be accessed via Debug menu > Step Into , or by pressing the F11 shortcut key. For Code::Blocks users In Code::Blocks, the step into command can be accessed via Debug menu > Step into , or by pressing the Shift-F7 For other compilers If using a different IDE, you\u2019ll likely find the step into command under a Debug or Run menu. When your program isn\u2019t running and you execute the first debug command, you may see quite a few things happen: The program will recompile if needed. The program will begin to run. Because our application is a console program, a console output window should open. It will be empty because we haven\u2019t output anything yet. Your IDE may open some diagnostic windows, which may have names such as \u201cDiagnostic Tools\u201d, \u201cCall Stack\u201d, and \u201cWatch\u201d. We\u2019ll cover what some of these are later -- for now you can ignore them. Because we did a step into , you should now see some kind of marker appear to the left of the opening brace of function main (line 9). In Visual Studio, this marker is a yellow arrow (Code::Blocks uses a yellow triangle). If you are using a different IDE, you should see something that serves the same purpose. This arrow marker indicates that the line being pointed to will be executed next. In this case, the debugger is telling us that the next line to be executed is the opening brace of function main (line 9). Choose step into (using the appropriate command for your IDE, listed above) to execute the opening brace, and the arrow will move to the next statement (line 10). This means the next line that will be executed is the call to function printValue . Choose step into again. Because this statement contains a function call to printValue , we step into the function, and the arrow will move to the top of the body of printValue (line 4). Choose step into again to execute the opening brace of function printValue , which will advance the arrow to line 5. Choose step into yet again, which will execute the statement std::cout << value and move the arrow to line 6. \u6ce8\u610f Because operator<< is implemented as a function, your IDE may step into the implementation of operator<< instead. If this happens, you\u2019ll see your IDE open a new code file, and the arrow marker will move to the top of a function named operator<< (this is part of the standard library). Close the code file that just opened, then find and execute step out debug command (instructions are below under the \u201cstep out\u201d section, if you need help). Now because std::cout << value has executed, we should see the value 5 appear in the console window. \u5c0f\u8d34\u58eb In a prior lesson, we mentioned that std::cout is buffered, which means there may be a delay between when you ask std::cout to print a value, and when it actually does. Because of this, you may not see the value 5 appear at this point. To ensure that all output from std::cout is output immediately, you can add the following statement to the top of your main() function: 1 std :: cout << std :: unitbuf ; // enable automatic flushing for std::cout (for debugging) COPY Choose step into again to execute the closing brace of function printValue . At this point, printValue has finished executing and control is returned to main . You will note that the arrow is again pointing to printValue ! While you might think that the debugger intends to call printValue again, in actuality the debugger is just letting you know that it is returning from the function call. Choose step into three more times. At this point, we have executed all the lines in our program, so we are done. Some debuggers will terminate the debugging session automatically at this point, others may not. If your debugger does not, you may need to find a \u201cStop Debugging\u201d command in your menus (in Visual Studio, this is under Debug > Stop Debugging ). Note that Stop Debugging can be used at any point in the debugging process to end the debugging session. Congratulations, you\u2019ve now stepped through a program and watched every line execute! Step over Like step into , The step over command executes the next statement in the normal execution path of the program. However, whereas step into will enter function calls and execute them line by line, step over will execute an entire function without stopping and return control to you after the function has been executed. For Visual Studio users In Visual Studio, the step over command can be accessed via Debug menu > Step Over , or by pressing the F10 shortcut key. For Code::Blocks users In Code::Blocks, the step over command is called Next line instead, and can be accessed via Debug menu > Next line , or by pressing the F7 shortcut key. Let\u2019s take a look at an example where we step over the function call to printValue : 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void printValue ( int value ) { std :: cout << value << '\\n' ; } int main () { printValue ( 5 ); return 0 ; } First, use step into on your program until the execution marker is on line 10: Now, choose step over . The debugger will execute the function (which prints the value 5 in the console output window) and then return control to you on the next statement (line 12). The step over command provides a convenient way to skip functions when you are sure they already work or are not interested in debugging them right now. Step out Unlike the other two stepping commands, Step out does not just execute the next line of code. Instead, it executes all remaining code in the function currently being executed, and then returns control to you when the function has returned. For Visual Studio users In Visual Studio, the step out command can be accessed via Debug menu > Step Out , or by pressing the Shift-F11 shortcut combo. For Code::Blocks users In Code::Blocks, the step out command can be accessed via Debug menu > Step out , or by pressing the ctrl-F7 shortcut combo. Let\u2019s take a look at an example of this using the same program as above: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void printValue ( int value ) { std :: cout << value << '\\n' ; } int main () { printValue ( 5 ); return 0 ; } Step into the program until you are inside function printValue , with the execution marker on line 4. Then choose step out . You will notice the value 5 appears in the output window, and the debugger returns control to you after the function has terminated (on line 10). This command is most useful when you\u2019ve accidentally stepped into a function that you don\u2019t want to debug. A step too far When stepping through a program, you can normally only step forward. It\u2019s very easy to accidentally step past (overstep) the place you wanted to examine. If you step past your intended destination, the usual thing to do is stop debugging and restart debugging again, being a little more careful not to pass your target this time. Step back Some debuggers (such as Visual Studio Enterprise Edition and GDB 7.0) have introduced a stepping capability generally referred to as step back or reverse debugging . The goal of a step back is to rewind the last step, so you can return the program to a prior state. This can be useful if you overstep, or if you want to re-examine a statement that just executed. Implementing step back requires a great deal of sophistication on the part of the debugger (because it has to keep track of a separate program state for each step). Because of the complexity, this capability isn\u2019t standardized yet, and varies by debugger. As of the time of writing (Jan 2019), neither Visual Studio Community edition nor the latest version of Code::Blocks support this capability. Hopefully at some point in the future, it will trickle down into these products and be available for wider use.","title":"3.6 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u5355\u6b65\u8c03\u8bd5"},{"location":"3-6-Using-an-integrated-debugger-Stepping/#_1","text":"\u8c03\u8bd5\u5668\u662f\u4e00\u79cd\u8ba1\u7b97\u673a\u7a0b\u5e8f\uff0c\u5b83\u5141\u8bb8\u7a0b\u5e8f\u5458\u63a7\u5236\u53e6\u4e00\u4e2a\u7a0b\u5e8f\u7684\u6267\u884c\u65b9\u5f0f\uff0c\u5e76\u5728\u8be5\u7a0b\u5e8f\u8fd0\u884c\u65f6\u68c0\u67e5\u8be5\u7a0b\u5e8f\u7684\u72b6\u6001\u3002\u4f8b\u5982\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u4f7f\u7528\u8c03\u8bd5\u5668\u9010\u884c\u6267\u884c\u7a0b\u5e8f\uff0c\u5728\u6574\u4e2a\u8fc7\u7a0b\u4e2d\u68c0\u67e5\u53d8\u91cf\u7684\u503c\u3002\u901a\u8fc7\u5c06\u53d8\u91cf\u7684\u5b9e\u9645\u503c\u4e0e\u9884\u671f\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u6216\u8005\u89c2\u5bdf\u6574\u4e2a\u4ee3\u7801\u7684\u6267\u884c\u8def\u5f84\uff0c\u8c03\u8bd5\u5668\u53ef\u4ee5\u6781\u5927\u5730\u5e2e\u52a9\u8ddf\u8e2a\u8bed\u4e49(\u903b\u8f91)\u9519\u8bef\u3002 \u8c03\u8bd5\u5668\u6709\u4e24\u5927\u6838\u5fc3\u529f\u80fd\uff1a\u7cbe\u786e\u63a7\u5236\u7a0b\u5e8f\u6267\u884c\u7684\u80fd\u529b\uff0c\u4ee5\u53ca\u67e5\u770b(\u548c\u4fee\u6539\uff0c\u5982\u679c\u9700\u8981\u7684\u8bdd)\u7a0b\u5e8f\u72b6\u6001\u7684\u80fd\u529b\u3002 \u65e9\u671f\u7684\u8c03\u8bd5\u5668\uff0c\u5982 gdb \uff0c\u662f\u5177\u6709\u547d\u4ee4\u884c\u63a5\u53e3\u7684\u72ec\u7acb\u7a0b\u5e8f\uff0c\u7a0b\u5e8f\u5458\u5fc5\u987b\u952e\u5165\u795e\u79d8\u7684\u547d\u4ee4\u624d\u80fd\u4f7f\u5b83\u4eec\u5de5\u4f5c\u3002\u540e\u6765\u7684\u8c03\u8bd5\u5668(\u5982Borland\u7684[turbo debugger]\u7684\u65e9\u671f\u7248\u672c(https://en.wikipedia.org/wiki/Turbo_Debugger))\u4ecd\u7136\u662f\u72ec\u7acb\u7684\uff0c\u4f46\u6709\u81ea\u5df1\u7684\u201c\u56fe\u5f62\u5316\u201d\u524d\u7aef\uff0c\u80fd\u591f\u4f7f\u5176\u66f4\u52a0\u6613\u7528\u3002\u76ee\u524d\u5927\u591a\u6570\u73b0\u4ee3IDE\u90fd\u6709\u4e00\u4e2a\u96c6\u6210\u7684\u8c03\u8bd5\u5668\u2014\u2014\u4e5f\u5c31\u662f\u8bf4\uff0c\u8c03\u8bd5\u5668\u4f7f\u7528\u4e0e\u4ee3\u7801\u7f16\u8f91\u5668\u76f8\u540c\u7684\u63a5\u53e3\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u4f7f\u7528\u7f16\u5199\u4ee3\u7801\u65f6\u4f7f\u7528\u7684\u76f8\u540c\u73af\u5883\u8fdb\u884c\u8c03\u8bd5(\u800c\u4e0d\u5fc5\u5207\u6362\u7a0b\u5e8f)\u3002 \u51e0\u4e4e\u6240\u6709\u7684\u73b0\u4ee3\u8c03\u8bd5\u5668\u90fd\u5305\u542b\u76f8\u540c\u7684\u57fa\u672c\u529f\u80fd\u6807\u51c6\u96c6\u2014\u2014\u7136\u800c\uff0c\u5728\u8bbf\u95ee\u8fd9\u4e9b\u529f\u80fd\u7684\u83dc\u5355\u5982\u4f55\u4f7f\u7528\u65b9\u9762\u51e0\u4e4e\u6ca1\u6709\u4e00\u81f4\u6027\uff0c\u952e\u76d8\u5feb\u6377\u952e\u7684\u4e00\u81f4\u6027\u751a\u81f3\u66f4\u4f4e\u3002\u5c3d\u7ba1\u6211\u4eec\u7684\u793a\u4f8b\u5c06\u4f7f\u7528Microsoft Visual Studio\u7684\u622a\u56fe(\u6211\u4eec\u4e5f\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728Code::Blocks\u4e2d\u5b8c\u6210\u6240\u6709\u64cd\u4f5c)\uff0c\u4f46\u65e0\u8bba\u4f60\u4f7f\u7528\u7684\u662f\u54ea\u79cdIDE\uff0c\u90fd\u5e94\u8be5\u4e0d\u96be\u5f04\u6e05\u695a\u5982\u4f55\u8bbf\u95ee\u6211\u4eec\u8ba8\u8bba\u7684\u6bcf\u4e2a\u529f\u80fd\u3002 \u5c0f\u8d34\u58eb \u8c03\u8bd5\u5668\u952e\u76d8\u5feb\u6377\u952e\u53ea\u6709\u5728IDE/\u96c6\u6210\u8c03\u8bd5\u5668\u662f\u6d3b\u52a8\u7a97\u53e3\u65f6\u624d\u6709\u6548\u3002 \u672c\u7ae0\u7684\u5269\u4f59\u90e8\u5206\u5c06\u5b66\u4e60\u5982\u4f55\u4f7f\u7528\u8c03\u8bd5\u5668\u3002 \u5c0f\u8d34\u58eb \u4e0d\u8981\u5ffd\u89c6\u5b66\u4e60\u4f7f\u7528\u8c03\u8bd5\u5668\u3002\u968f\u7740\u7a0b\u5e8f\u53d8\u5f97\u8d8a\u6765\u8d8a\u590d\u6742\uff0c\u4e0e\u67e5\u627e\u548c\u4fee\u590d\u95ee\u9898\u6240\u8282\u7701\u7684\u65f6\u95f4\u76f8\u6bd4\uff0c\u5b66\u4e60\u6709\u6548\u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u6240\u82b1\u8d39\u7684\u65f6\u95f4\u5c31\u663e\u5f97\u5fae\u4e0d\u8db3\u9053\u4e86\u3002 \u6ce8\u610f Before proceeding with this lesson (and subsequent lessons related to using a debugger), make sure your project is compiled using a debug build configuration (see 0.9 -- Configuring your compiler: Build configurations for more information). If you\u2019re compiling your project using a release configuration instead, the functionality of the debugger may not work correctly (e.g. when you try to step into your program, it will just run the program instead). For Code::Blocks users If you\u2019re using Code::Blocks, your debugger may or may not be set up correctly. Let\u2019s check. First, go to Settings menu > Debugger\u2026 . Next, open the GDB/CDB debugger tree on the left, and choose Default . A dialog should open that looks something like this: If you see a big red bar where the \u201cExecutable path\u201d should be, then you need to locate your debugger. To do so, click the \u2026 button to the right of the Executable path_field. Next, find the \u201cgdb32.exe\u201d file on your system -- mine was in _C:\\Program Files (x86)\\CodeBlocks\\MinGW\\bin\\gdb32.exe . Then click OK . For Code::Blocks users There have been reports that the Code::Blocks integrated debugger (GDB) can have issues recognizing some file paths that contain spaces or non-English characters in them. If the debugger appears to be malfunctioning as you go through these lessons, that could be a reason why.","title":"\u8c03\u8bd5\u5668"},{"location":"3-6-Using-an-integrated-debugger-Stepping/#_2","text":"We\u2019re going to start our exploration of the debugger by first examining some of the debugging tools that allow us to control the way a program executes. Stepping is the name for a set of related debugger features that let us execute (step through) our code statement by statement. There are a number of related stepping commands that we\u2019ll cover in turn.","title":"\u5355\u6b65\u8c03\u8bd5"},{"location":"3-6-Using-an-integrated-debugger-Stepping/#step-into","text":"The step into command executes the next statement in the normal execution path of the program, and then pauses execution of the program so we can examine the program\u2019s state using the debugger. If the statement being executed contains a function call, step into causes the program to jump to the top of the function being called, where it will pause. Let\u2019s take a look at a very simple program: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void printValue ( int value ) { std :: cout << value << '\\n' ; } int main () { printValue ( 5 ); return 0 ; } Let\u2019s debug this program using the step into command. First, locate and then execute the step into debug command once. For Visual Studio users In Visual Studio, the step into command can be accessed via Debug menu > Step Into , or by pressing the F11 shortcut key. For Code::Blocks users In Code::Blocks, the step into command can be accessed via Debug menu > Step into , or by pressing the Shift-F7 For other compilers If using a different IDE, you\u2019ll likely find the step into command under a Debug or Run menu. When your program isn\u2019t running and you execute the first debug command, you may see quite a few things happen: The program will recompile if needed. The program will begin to run. Because our application is a console program, a console output window should open. It will be empty because we haven\u2019t output anything yet. Your IDE may open some diagnostic windows, which may have names such as \u201cDiagnostic Tools\u201d, \u201cCall Stack\u201d, and \u201cWatch\u201d. We\u2019ll cover what some of these are later -- for now you can ignore them. Because we did a step into , you should now see some kind of marker appear to the left of the opening brace of function main (line 9). In Visual Studio, this marker is a yellow arrow (Code::Blocks uses a yellow triangle). If you are using a different IDE, you should see something that serves the same purpose. This arrow marker indicates that the line being pointed to will be executed next. In this case, the debugger is telling us that the next line to be executed is the opening brace of function main (line 9). Choose step into (using the appropriate command for your IDE, listed above) to execute the opening brace, and the arrow will move to the next statement (line 10). This means the next line that will be executed is the call to function printValue . Choose step into again. Because this statement contains a function call to printValue , we step into the function, and the arrow will move to the top of the body of printValue (line 4). Choose step into again to execute the opening brace of function printValue , which will advance the arrow to line 5. Choose step into yet again, which will execute the statement std::cout << value and move the arrow to line 6. \u6ce8\u610f Because operator<< is implemented as a function, your IDE may step into the implementation of operator<< instead. If this happens, you\u2019ll see your IDE open a new code file, and the arrow marker will move to the top of a function named operator<< (this is part of the standard library). Close the code file that just opened, then find and execute step out debug command (instructions are below under the \u201cstep out\u201d section, if you need help). Now because std::cout << value has executed, we should see the value 5 appear in the console window. \u5c0f\u8d34\u58eb In a prior lesson, we mentioned that std::cout is buffered, which means there may be a delay between when you ask std::cout to print a value, and when it actually does. Because of this, you may not see the value 5 appear at this point. To ensure that all output from std::cout is output immediately, you can add the following statement to the top of your main() function: 1 std :: cout << std :: unitbuf ; // enable automatic flushing for std::cout (for debugging) COPY Choose step into again to execute the closing brace of function printValue . At this point, printValue has finished executing and control is returned to main . You will note that the arrow is again pointing to printValue ! While you might think that the debugger intends to call printValue again, in actuality the debugger is just letting you know that it is returning from the function call. Choose step into three more times. At this point, we have executed all the lines in our program, so we are done. Some debuggers will terminate the debugging session automatically at this point, others may not. If your debugger does not, you may need to find a \u201cStop Debugging\u201d command in your menus (in Visual Studio, this is under Debug > Stop Debugging ). Note that Stop Debugging can be used at any point in the debugging process to end the debugging session. Congratulations, you\u2019ve now stepped through a program and watched every line execute!","title":"Step into"},{"location":"3-6-Using-an-integrated-debugger-Stepping/#step-over","text":"Like step into , The step over command executes the next statement in the normal execution path of the program. However, whereas step into will enter function calls and execute them line by line, step over will execute an entire function without stopping and return control to you after the function has been executed. For Visual Studio users In Visual Studio, the step over command can be accessed via Debug menu > Step Over , or by pressing the F10 shortcut key. For Code::Blocks users In Code::Blocks, the step over command is called Next line instead, and can be accessed via Debug menu > Next line , or by pressing the F7 shortcut key. Let\u2019s take a look at an example where we step over the function call to printValue : 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void printValue ( int value ) { std :: cout << value << '\\n' ; } int main () { printValue ( 5 ); return 0 ; } First, use step into on your program until the execution marker is on line 10: Now, choose step over . The debugger will execute the function (which prints the value 5 in the console output window) and then return control to you on the next statement (line 12). The step over command provides a convenient way to skip functions when you are sure they already work or are not interested in debugging them right now.","title":"Step over"},{"location":"3-6-Using-an-integrated-debugger-Stepping/#step-out","text":"Unlike the other two stepping commands, Step out does not just execute the next line of code. Instead, it executes all remaining code in the function currently being executed, and then returns control to you when the function has returned. For Visual Studio users In Visual Studio, the step out command can be accessed via Debug menu > Step Out , or by pressing the Shift-F11 shortcut combo. For Code::Blocks users In Code::Blocks, the step out command can be accessed via Debug menu > Step out , or by pressing the ctrl-F7 shortcut combo. Let\u2019s take a look at an example of this using the same program as above: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void printValue ( int value ) { std :: cout << value << '\\n' ; } int main () { printValue ( 5 ); return 0 ; } Step into the program until you are inside function printValue , with the execution marker on line 4. Then choose step out . You will notice the value 5 appears in the output window, and the debugger returns control to you after the function has terminated (on line 10). This command is most useful when you\u2019ve accidentally stepped into a function that you don\u2019t want to debug.","title":"Step out"},{"location":"3-6-Using-an-integrated-debugger-Stepping/#a-step-too-far","text":"When stepping through a program, you can normally only step forward. It\u2019s very easy to accidentally step past (overstep) the place you wanted to examine. If you step past your intended destination, the usual thing to do is stop debugging and restart debugging again, being a little more careful not to pass your target this time.","title":"A step too far"},{"location":"3-6-Using-an-integrated-debugger-Stepping/#step-back","text":"Some debuggers (such as Visual Studio Enterprise Edition and GDB 7.0) have introduced a stepping capability generally referred to as step back or reverse debugging . The goal of a step back is to rewind the last step, so you can return the program to a prior state. This can be useful if you overstep, or if you want to re-examine a statement that just executed. Implementing step back requires a great deal of sophistication on the part of the debugger (because it has to keep track of a separate program state for each step). Because of the complexity, this capability isn\u2019t standardized yet, and varies by debugger. As of the time of writing (Jan 2019), neither Visual Studio Community edition nor the latest version of Code::Blocks support this capability. Hopefully at some point in the future, it will trickle down into these products and be available for wider use.","title":"Step back"},{"location":"3-7-Using-an-integrated-debugger-Running-and-breakpoints/","tags":["debugger"],"text":"While stepping (covered in lesson 3.6 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u5355\u6b65\u8c03\u8bd5 ) is useful for examining each individual line of your code in isolation, in a large program, it can take a long time to step through your code to even get to the point where you want to examine in more detail. Fortunately, modern debuggers provide more tools to help us efficiently debug our programs. In this lesson, we\u2019ll look at some of the debugger features that let us more quickly navigate our code. Run to cursor The first useful command is commonly called Run to cursor . This Run to cursor command executes the program until execution reaches the statement selected by your cursor. Then it returns control to you so you can debug starting at that point. This makes for an efficient way to start debugging at a particular point in your code, or if already debugging, to move straight to some place you want to examine further. For Visual Studio users In Visual Studio, the run to cursor command can be accessed by right clicking a statement in your code and choosing Run to Cursor from the context menu, or by pressing the ctrl-F10 keyboard combo. For Code::Blocks users In Code::Blocks, the run to cursor command can be accessed by right clicking a statement in your code and choosing either Run to cursor from the context menu or Debug menu > Run to cursor , or by pressing the F4 shortcut key. Let\u2019s try it using the same program we\u2019ve been using: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void printValue ( int value ) { std :: cout << value ; } int main () { printValue ( 5 ); return 0 ; } COPY Simply right click anywhere on line 5, then choose \u201cRun to cursor\u201d. You will notice the program starts running, and the execution marker moves to the line you just selected. Your program has executed up to this point and is now waiting for your further debugging commands. From here, you can step through your program, run to cursor to a different location, etc\u2026 If you run to cursor to a location that doesn\u2019t execute, run to cursor will simply run your program until termination. Continue Once you\u2019re in the middle of a debugging session, you may want to just run the program from that point forward. The easiest way to do this is to use the _continue_command. The continue debug command simply continues running the program as per normal, either until the program terminates, or until something triggers control to return back to you again (such as a breakpoint, which we\u2019ll cover later in this lesson). For Visual Studio users In Visual Studio, the continue command can be accessed while already debugging a program via Debug menu > Continue , or by pressing the F5 shortcut key. For Code::Blocks users In Code::Blocks, the continue command can be accessed while already debugging a program via Debug menu > Start / Continue , or by pressing the F8 shortcut key. Let\u2019s test out the continue command. If your execution marker isn\u2019t already on line 5, run to cursor to line 5. Then choose continue from this point. Your program will finish executing and then terminate. Start The continue command has a twin brother named start . The start command performs the same action as continue , just starting from the beginning of the program. It can only be invoked when not already in a debug session. For Visual Studio users In Visual Studio, the start command can be accessed while not debugging a program via Debug menu > Start Debugging , or by pressing the F5 shortcut key. For Code::Blocks users In Code::Blocks, the start command can be accessed while not debugging a program via Debug menu > Start / Continue , or by pressing the F8 shortcut key. If you use the start command on the above sample program, it will run all the way through without interruption. While this may seem unremarkable, that\u2019s only because we haven\u2019t told the debugger to interrupt the program. We\u2019ll put this command to better use in the next section. Breakpoints The last topic we are going to talk about in this section is breakpoints. A breakpoint is a special marker that tells the debugger to stop execution of the program at the breakpoint when running in debug mode. For Visual Studio users In Visual Studio, you can set or remove a breakpoint via Debug menu > Toggle Breakpoint , or by right clicking on a statement and choosing Toggle Breakpoint from the context menu, or by pressing the F9 shortcut key, or by clicking to the left of the line number (in the light grey area). For Code::Blocks users In Code::Blocks, you can set or remove a breakpoint via Debug menu > Toggle breakpoint , or by right clicking on a statement and choosing Toggle breakpoint from the context menu, or by pressing the F5 shortcut key, or by clicking to the right of the line number. When you set a breakpoint, you will see a new type of icon appear. Visual Studio uses a red circle, Code::Blocks uses a red octagon (like a stop sign): Go ahead and set a breakpoint on the line 5, as shown in the image above. Now, choose the Start command to have the debugger run your code, and let\u2019s see the breakpoint in action. You will notice that instead of running all the way to the end of the program, the debugger stops at the breakpoint (with the execution marker sitting on top of the breakpoint icon): It\u2019s just as if you\u2019d run to cursor to this point. Breakpoints have a couple of advantages over run to cursor . First, a breakpoint will cause the debugger to return control to you every time they are encountered (unlike run to cursor , which only runs to the cursor once each time it is invoked). Second, you can set a breakpoint and it will persist until you remove it, whereas with run to cursor you have to locate the spot you want to run to each time you invoke the command. Note that breakpoints placed on lines that are not in the path of execution will not cause the debugger to halt execution of the code. Let\u2019s take a look at a slightly modified program that better illustrates the difference between breakpoints and run to cursor : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include void printValue ( int value ) { std :: cout << value ; } int main () { printValue ( 5 ); printValue ( 6 ); printValue ( 7 ); return 0 ; } COPY First, start a new debugging session and then do a run to cursor to line 5. Now choose continue . The program will continue to the end (it won\u2019t stop on line 5 again, even though line 5 is executed twice more). Next, place a breakpoint on line 5, then choose start . The program will stop on line 5. Now choose continue . The program will stop on line 5 a second time. Choose continue_again, and it will stop a third time. One more _continue , and the program will terminate. You can see that the breakpoint caused the program to stop as many times as that line was executed. Set next statement There\u2019s one more debugging command that\u2019s used fairly uncommonly, but is still at least worth knowing about, even if you won\u2019t use it very often. The set next statementcommand allows us to change the point of execution to some other statement (sometimes informally called jumping ). This can be used to jump the point of execution forwards and skip some code that would otherwise execute, or backwards and have something that already executed run again. For Visual Studio users In Visual Studio, you can jump the point of execution by right clicking on a statement and choosing Set next statement from the context menu, or by pressing the Ctrl-Shift-F10 shortcut combination. This option is contextual and only occurs while already debugging a program. For Code::Blocks users In Code::Blocks, you can jump the point of execution via Debug menu > Set next statement , or by right clicking on a statement and choosing Set next statement from the context menu. Code::Blocks doesn\u2019t have a keyboard shortcut for this command. Let\u2019s see jumping forwards in action: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include void printValue ( int value ) { std :: cout << value ; } int main () { printValue ( 5 ); printValue ( 6 ); printValue ( 7 ); return 0 ; } COPY First, run to cursor to line 11. At this point, you should see the value of 5 in the console output window. Now, right click on line 12, and choose set next statement . This causes line 11 to be skipped and not execute. Then choose continue to finish executing your program. The output of your program should look like this: 57 We can see that printValue(6) was skipped. This functionality can be useful in several contexts. In our exploration of basic debugging techniques, we discussed commenting out a function as a way to determine whether that function had a role in causing an issue. This requires modifying our code, and remembering to uncomment the function later. In the debugger, there\u2019s no direct way to skip a function, so if you decide you want to do this, using set next statement to jump over a function call is the easiest way to do so. Jumping backwards can also be useful if we want to watch a function that just executed run again, so we can see what it is doing. With the same code above, run to cursor to line 12. Then set next statement on line 11, and continue . The program\u2019s output should be: 5667 Warning The set next statement command will change the point of execution, but will not otherwise change the program state. Your variables will retain whatever values they had before the jump. As a result, jumping may cause your program to produce different values, results, or behaviors than it would otherwise. Use this capability judiciously (especially jumping backwards). Warning You should not use set next statement to change the point of execution to a different function. This will result in undefined behavior, and likely a crash. Conclusion You now learned the major ways that you can use an integrated debugger to watch and control how your program executes. While these commands can be useful for diagnosing code flow issues (e.g. to determine if certain functions are or aren\u2019t being called), they are only a portion of the benefit that the integrated debugger brings to the table. In the next lesson, we\u2019ll start exploring additional ways to examine your program\u2019s state, for which you\u2019ll need these commands as a prerequisite. Let\u2019s go!","title":"3.7 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8fd0\u884c\u548c\u65ad\u70b9"},{"location":"3-7-Using-an-integrated-debugger-Running-and-breakpoints/#run-to-cursor","text":"The first useful command is commonly called Run to cursor . This Run to cursor command executes the program until execution reaches the statement selected by your cursor. Then it returns control to you so you can debug starting at that point. This makes for an efficient way to start debugging at a particular point in your code, or if already debugging, to move straight to some place you want to examine further. For Visual Studio users In Visual Studio, the run to cursor command can be accessed by right clicking a statement in your code and choosing Run to Cursor from the context menu, or by pressing the ctrl-F10 keyboard combo. For Code::Blocks users In Code::Blocks, the run to cursor command can be accessed by right clicking a statement in your code and choosing either Run to cursor from the context menu or Debug menu > Run to cursor , or by pressing the F4 shortcut key. Let\u2019s try it using the same program we\u2019ve been using: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void printValue ( int value ) { std :: cout << value ; } int main () { printValue ( 5 ); return 0 ; } COPY Simply right click anywhere on line 5, then choose \u201cRun to cursor\u201d. You will notice the program starts running, and the execution marker moves to the line you just selected. Your program has executed up to this point and is now waiting for your further debugging commands. From here, you can step through your program, run to cursor to a different location, etc\u2026 If you run to cursor to a location that doesn\u2019t execute, run to cursor will simply run your program until termination. Continue Once you\u2019re in the middle of a debugging session, you may want to just run the program from that point forward. The easiest way to do this is to use the _continue_command. The continue debug command simply continues running the program as per normal, either until the program terminates, or until something triggers control to return back to you again (such as a breakpoint, which we\u2019ll cover later in this lesson). For Visual Studio users In Visual Studio, the continue command can be accessed while already debugging a program via Debug menu > Continue , or by pressing the F5 shortcut key. For Code::Blocks users In Code::Blocks, the continue command can be accessed while already debugging a program via Debug menu > Start / Continue , or by pressing the F8 shortcut key. Let\u2019s test out the continue command. If your execution marker isn\u2019t already on line 5, run to cursor to line 5. Then choose continue from this point. Your program will finish executing and then terminate. Start The continue command has a twin brother named start . The start command performs the same action as continue , just starting from the beginning of the program. It can only be invoked when not already in a debug session. For Visual Studio users In Visual Studio, the start command can be accessed while not debugging a program via Debug menu > Start Debugging , or by pressing the F5 shortcut key. For Code::Blocks users In Code::Blocks, the start command can be accessed while not debugging a program via Debug menu > Start / Continue , or by pressing the F8 shortcut key. If you use the start command on the above sample program, it will run all the way through without interruption. While this may seem unremarkable, that\u2019s only because we haven\u2019t told the debugger to interrupt the program. We\u2019ll put this command to better use in the next section. Breakpoints The last topic we are going to talk about in this section is breakpoints. A breakpoint is a special marker that tells the debugger to stop execution of the program at the breakpoint when running in debug mode. For Visual Studio users In Visual Studio, you can set or remove a breakpoint via Debug menu > Toggle Breakpoint , or by right clicking on a statement and choosing Toggle Breakpoint from the context menu, or by pressing the F9 shortcut key, or by clicking to the left of the line number (in the light grey area). For Code::Blocks users In Code::Blocks, you can set or remove a breakpoint via Debug menu > Toggle breakpoint , or by right clicking on a statement and choosing Toggle breakpoint from the context menu, or by pressing the F5 shortcut key, or by clicking to the right of the line number. When you set a breakpoint, you will see a new type of icon appear. Visual Studio uses a red circle, Code::Blocks uses a red octagon (like a stop sign): Go ahead and set a breakpoint on the line 5, as shown in the image above. Now, choose the Start command to have the debugger run your code, and let\u2019s see the breakpoint in action. You will notice that instead of running all the way to the end of the program, the debugger stops at the breakpoint (with the execution marker sitting on top of the breakpoint icon): It\u2019s just as if you\u2019d run to cursor to this point. Breakpoints have a couple of advantages over run to cursor . First, a breakpoint will cause the debugger to return control to you every time they are encountered (unlike run to cursor , which only runs to the cursor once each time it is invoked). Second, you can set a breakpoint and it will persist until you remove it, whereas with run to cursor you have to locate the spot you want to run to each time you invoke the command. Note that breakpoints placed on lines that are not in the path of execution will not cause the debugger to halt execution of the code. Let\u2019s take a look at a slightly modified program that better illustrates the difference between breakpoints and run to cursor : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include void printValue ( int value ) { std :: cout << value ; } int main () { printValue ( 5 ); printValue ( 6 ); printValue ( 7 ); return 0 ; } COPY First, start a new debugging session and then do a run to cursor to line 5. Now choose continue . The program will continue to the end (it won\u2019t stop on line 5 again, even though line 5 is executed twice more). Next, place a breakpoint on line 5, then choose start . The program will stop on line 5. Now choose continue . The program will stop on line 5 a second time. Choose continue_again, and it will stop a third time. One more _continue , and the program will terminate. You can see that the breakpoint caused the program to stop as many times as that line was executed. Set next statement There\u2019s one more debugging command that\u2019s used fairly uncommonly, but is still at least worth knowing about, even if you won\u2019t use it very often. The set next statementcommand allows us to change the point of execution to some other statement (sometimes informally called jumping ). This can be used to jump the point of execution forwards and skip some code that would otherwise execute, or backwards and have something that already executed run again. For Visual Studio users In Visual Studio, you can jump the point of execution by right clicking on a statement and choosing Set next statement from the context menu, or by pressing the Ctrl-Shift-F10 shortcut combination. This option is contextual and only occurs while already debugging a program. For Code::Blocks users In Code::Blocks, you can jump the point of execution via Debug menu > Set next statement , or by right clicking on a statement and choosing Set next statement from the context menu. Code::Blocks doesn\u2019t have a keyboard shortcut for this command. Let\u2019s see jumping forwards in action: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include void printValue ( int value ) { std :: cout << value ; } int main () { printValue ( 5 ); printValue ( 6 ); printValue ( 7 ); return 0 ; } COPY First, run to cursor to line 11. At this point, you should see the value of 5 in the console output window. Now, right click on line 12, and choose set next statement . This causes line 11 to be skipped and not execute. Then choose continue to finish executing your program. The output of your program should look like this: 57 We can see that printValue(6) was skipped. This functionality can be useful in several contexts. In our exploration of basic debugging techniques, we discussed commenting out a function as a way to determine whether that function had a role in causing an issue. This requires modifying our code, and remembering to uncomment the function later. In the debugger, there\u2019s no direct way to skip a function, so if you decide you want to do this, using set next statement to jump over a function call is the easiest way to do so. Jumping backwards can also be useful if we want to watch a function that just executed run again, so we can see what it is doing. With the same code above, run to cursor to line 12. Then set next statement on line 11, and continue . The program\u2019s output should be: 5667 Warning The set next statement command will change the point of execution, but will not otherwise change the program state. Your variables will retain whatever values they had before the jump. As a result, jumping may cause your program to produce different values, results, or behaviors than it would otherwise. Use this capability judiciously (especially jumping backwards). Warning You should not use set next statement to change the point of execution to a different function. This will result in undefined behavior, and likely a crash. Conclusion You now learned the major ways that you can use an integrated debugger to watch and control how your program executes. While these commands can be useful for diagnosing code flow issues (e.g. to determine if certain functions are or aren\u2019t being called), they are only a portion of the benefit that the integrated debugger brings to the table. In the next lesson, we\u2019ll start exploring additional ways to examine your program\u2019s state, for which you\u2019ll need these commands as a prerequisite. Let\u2019s go!","title":"Run to cursor"},{"location":"3-8-Using-an-integrated-debugger-Watching-variables/","tags":["debugger"],"text":"In the previous lessons ( 3.6 -- Using an integrated debugger: Stepping and 3.7 -- Using an integrated debugger: Running and breakpoints ), you learned how to use the debugger to watch the path of execution through your program. However, stepping through a program is only half of what makes the debugger useful. The debugger also lets you examine the value of variables as you step through your code, all without having to modify your code. As per previous lessons, our examples here will use Visual Studio -- if you are using a different IDE/debugger, the commands may have slightly different names or be located in different locations. Warning In case you are returning, make sure your project is compiled using a debug build configuration (see 0.9 -- Configuring your compiler: Build configurations for more information). If you\u2019re compiling your project using a release configuration instead, the functionality of the debugger may not work correctly. Watching variables Watching a variable is the process of inspecting the value of a variable while the program is executing in debug mode. Most debuggers provide several ways to do this. Let\u2019s take a look at a sample program: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int x { 1 }; std :: cout << x << ' ' ; x = x + 2 ; std :: cout << x << ' ' ; x = x + 3 ; std :: cout << x << ' ' ; return 0 ; } COPY This is a pretty straightforward sample program -- it prints the numbers 1, 3, and 6. First, run to cursor to line 6. At this point, the variable x has already been created and initialized with the value 1, so when we examine the value of x, we should expect to see the value 1. The easiest way to examine the value of a simple variable like x is to hover your mouse over the variable x. Some modern debuggers support this method of inspecting simple variables, and it is the most straightforward way to do so. For Code::Blocks users If you\u2019re using Code::Blocks, this option is (inexplicably) off by default. Let\u2019s turn it on. First, go to Settings menu > Debugger\u2026 . Then under the GDB/CDB debugger node , select the Default profile. Finally, check the box labeled Evaluate expression under cursor . Hover your mouse cursor over variable x on line 6, and you should see something like this: Note that you can hover over any variable x, not just the one on the current line. For example, if we hover over the x on line 12, we\u2019ll see the same value: If you\u2019re using Visual Studio, you can also use QuickWatch. Highlight the variable name x with your mouse, and then choose \u201cQuickWatch\u201d from the right-click menu. This will pull up a subwindow containing the current value of the variable: Go ahead and close QuickWatch if you opened it. Now let\u2019s watch this variable change as we step through the program. Either choose step over twice, or run to cursor to line 9. The variable x should now have value 3 . Inspect it and make sure that it does! The watch window Using the mouse hover or QuickWatch methods to inspect variables is fine if you want to know the value of a variable at a particular point in time, but it\u2019s not particularly well suited to watching the value of a variable change as you run the code because you continually have to rehover/reselect the variable. In order to address this issue, all modern integrated debuggers provide another feature, called a watch window. The watch window is a window where you can add variables you would like to continually inspect, and these variables will be updated as you step through your program. The watch window may already be on your screen when you enter debug mode, but if it is not, you can bring it up through your IDE\u2019s window commands (these are typically found in a View or Debug menu). For Visual Studio users In Visual Studio, the watch menu can be found at Debug menu > Windows > Watch > Watch 1 . Do note that you have to be in debug mode for this option to be enabled, so step into your program first. Where this window appears (docked left, right, or bottom) may vary. You can change where it is docked by dragging the Watch 1 tab to a different side of the application window. For Code::Blocks users In Code::Blocks, the watch menu can be found at Debug menu > Debugging windows > Watches . This window will likely appear as a separate window. You can dock it into your main window by dragging it over. You should now see something like this: The watches window may or may not contain anything in it already. There are typically two different ways to add variables to the watch window: Pull up the watch window, and type in the name of the variable you would like to watch in the leftmost column of the watch window. In the code window, right click on the variable you\u2019d like to watch, and choose Add Watch (Visual Studio) or Watch x (replace x with the variable\u2019s name) (Code::Blocks). If you\u2019re not already in a debugging session with the execution marker on line 9 of your program, start a new debugging session and run to cursor to line 9. Now, go ahead and add the variable \u201cx\u201d to your watch list. You should now see this: Now step over twice, or run to cursor to line 12, and you should see the value of x change from 3 to 6 . Variables that go out of scope (e.g. a local variable inside a function that has already returned to the caller) will stay in your watch window, but will generally either be marked as \u201cnot available\u201d, or may show the last known value but grayed out. If the variable returns to scope (e.g. the function is called again), its value will begin showing again. Therefore, it\u2019s fine to leave variables in the watch window, even if they\u2019re out of scope. Using watches is the best way to watch the value of a variable change over time as you step through your program. The watch window can evaluate expressions too The watch window will also allow you to evaluate simple expressions. If you haven\u2019t already, run to cursor to line 12. Then try entering x + 2 into the watch window and see what happens (it should evaluate to 8). You can also highlight an expression in your code and then inspect the value of that expression via hover or by adding it to the watch window via the right-click context menu. Warning Identifiers in watched expressions will evaluate to their current values. If you want to know what value an expression in your code is actually evaluating to, run to cursor to it first, so that all identifiers have the correct values. Local watches Because inspecting the value of local variables inside a function is common while debugging, many debuggers will offer some way to quickly watch the value of all local variables in scope. For Visual Studio users In Visual Studio, you can see the value of all local variables in the Locals window, which can be found at Debug menu > Windows > Locals . Note that you have to be in a debug session to activate this window. For Code::Blocks users In Code::Blocks, this is integrated into the Watch window, under the Locals node. If you don\u2019t see any, there either aren\u2019t any, or you need to uncollapse the node. If you\u2019re just looking to watch the value of a local variable, check the locals window first. It should already be there.","title":"3.8 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u76d1\u63a7\u53d8\u91cf"},{"location":"3-9-Using-an-integrated-debugger-The-call-stack/","tags":["debugger","call stack"],"text":"Modern debuggers contain one more debugging information window that can be very useful in debugging your program, and that is the call stack window. When your program calls a function, you already know that it bookmarks the current location, makes the function call, and then returns. How does it know where to return to? The answer is that it keeps track in the call stack. The call stack is a list of all the active functions that have been called to get to the current point of execution. The call stack includes an entry for each function called, as well as which line of code will be returned to when the function returns. Whenever a new function is called, that function is added to the top of the call stack. When the current function returns to the caller, it is removed from the top of the call stack, and control returns to the function just below it. The call stack window is a debugger window that shows the current call stack. If you don\u2019t see the call stack window, you will need to tell the IDE to show it. For Visual Studio users In Visual Studio, the call stack window can be found via Debug menu > Windows > Call Stack . Note that you have to be in a debug session to activate this window. For Code::Blocks users In Code::Blocks, the call stack window can be found via Debug menu > Debugging windows > Call stack . Let\u2019s take a look at the call stack using a sample program: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include void a () { std :: cout << \"a() called \\n \" ; } void b () { std :: cout << \"b() called \\n \" ; a (); } int main () { a (); b (); return 0 ; } COPY Put breakpoints on lines 5 and 10 of this program, and then start debugging mode. Because function a is called first, the breakpoint on line 5 will be hit first. At this point, you should see something like this: Note that your line numbers may be slightly different (off by 1), and that instead of [External Code] you may see a bunch of other crazily named functions. These differences are insubstantial. What\u2019s relevant here is the top two lines. From the bottom up, we can see that function main was called first, and then that function a was called next. The line 5 next to function a shows us where the current point of execution is (which matches the execution marker in the code window). The line 17 on the second line indicates the line that will be returned to when control returns to function main . Tip The line numbers after the function names show the next line to be executed in each function. Since the top entry on the call stack represents the currently executing function, the line number here shows the next line that will execute when execution resumes. The remaining entries in the call stack represent functions that will be returned to at some point, so the line number for these represent the next statement that will execute after the function is returned to. Now, choose the continue debug command to advance execution to the next breakpoint, which will be on line 10. The call stack should update to reflect the new situation: You\u2019ll notice that function b is now the top line of the call stack, reflecting the fact that function b is the function that is actively being executed. Note that function a is no longer represented on the call stack. This is because function a was removed from the call stack when it returned. Choose the continue debug command one more time, and we\u2019ll hit the breakpoint on line 5 again (because function b calls function a ). The call stack will look like this: There are now three functions on the call stack: (from bottom to top) main , which called function b , which called function a . The call stack is useful in conjunction with breakpoints, when your breakpoint is hit and you want to know what functions were called to get to that specific point in the code. Conclusion Congratulations, you now know the basics of using an integrated debugger! Using stepping, breakpoints, watches, and the call stack window, you now have the fundamentals to be able to debug almost any problem. Like many things, becoming good at using a debugger takes some practice and some trial and error. But again, we\u2019ll reiterate the point that the time devoted to learning how to use an integrated debugger effectively will be repaid many times over in time saved debugging your programs!","title":"3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808"},{"location":"3-x-Chapter-3-summary-and-quiz/","tags":["summary"],"text":"3.1 - \u8bed\u6cd5\u548c\u8bed\u4e49\u9519\u8bef \u5982\u679c\u4f60\u7684\u8bed\u53e5\u4e0d\u7b26\u5408 C++ \u7684\u8bed\u6cd5\uff0c\u5219\u4f1a\u4ea7\u751f \u8bed\u6cd5\u9519\u8bef \u3002\u7f16\u8bd1\u5668\u6216\u6355\u83b7\u8bed\u6cd5\u9519\u8bef\u3002 \u5f53\u8bed\u53e5\u7684\u8bed\u6cd5\u6b63\u786e\uff0c\u4f46\u662f\u5176\u884c\u4e3a\u5e76\u4e0d\u7b26\u5408\u7a0b\u5e8f\u5458\u7684\u672c\u610f\uff0c\u6b64\u65f6\u79f0\u4e3a \u8bed\u4e49\u9519\u8bef \u3002 3.2 - \u4ee3\u7801\u8c03\u8bd5\u6b65\u9aa4 \u5b9a\u4f4d\u5e76\u4fee\u590d\u7a0b\u5e8f\u9519\u8bef\u7684\u8fc7\u7a0b\uff0c\u79f0\u4e3adebugging\u3002 \u53ef\u4ee5\u4f7f\u7528\u4e94\u6b65\u6cd5\u6765\u5b9a\u4f4d\u5e76\u4fee\u590d\u7a0b\u5e8f\u9519\u8bef\uff1a \u5b9a\u4f4d\u6839\u56e0\uff1b \u7406\u89e3\u95ee\u9898\uff1b \u786e\u5b9a\u4fee\u590d\u65b9\u5f0f\uff1b \u4fee\u590d\u95ee\u9898\uff1b \u91cd\u65b0\u6d4b\u8bd5 debug\u8fc7\u7a0b\u4e2d\u6700\u56f0\u96be\u7684\u90e8\u5206\u5c31\u662f\u627e\u5230\u95ee\u9898\u6240\u5728\u3002 \u9759\u6001\u5206\u6790\u5de5\u5177\u53ef\u4ee5\u7528\u6765\u5bf9\u4ee3\u7801\u8fdb\u884c\u5206\u6790\uff0c\u67e5\u627e\u53ef\u80fd\u5bfc\u81f4\u95ee\u9898\u7684\u8bed\u4e49\u9519\u8bef\u3002 debug\u8fc7\u7a0b\u4e2d\uff0c\u6700\u91cd\u8981\u7684\u4e00\u6b65\u5c31\u662f\u8981\u80fd\u591f\u7a33\u5b9a\u5730\u590d\u73b0\u95ee\u9898\u3002 \u4ee5\u4e0b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9a\u4f4d\u95ee\u9898\uff1a \u6ce8\u91ca\u6389\u4ee3\u7801\uff1b \u4f7f\u7528\u8f93\u51fa\u8bed\u53e5\u6765\u9a8c\u8bc1\u4ee3\u7801\u6d41\u7a0b\uff1b \u6253\u5370\u503c\u3002 \u5f53\u4f7f\u7528\u6253\u5370\u8bed\u53e5\u65f6\uff0c\u63a8\u8350\u4f7f\u7528 std::cerr \u6765\u4ee3\u66ff std::cout \uff0c\u4e0d\u8fc7\u6700\u597d\u907f\u514d\u4f9d\u8d56\u6253\u5370\u8bed\u53e5\u6765\u5b9a\u4f4d\u95ee\u9898\u3002 \u65e5\u5fd7\u6587\u4ef6\u8bb0\u5f55\u4e86\u7a0b\u5e8f\u4e2d\u7684\u5404\u4e2a\u4e8b\u4ef6\u3002\u5c06\u4fe1\u606f\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\u7684\u8fc7\u7a0b\uff0c\u79f0\u4e3a\u5199\u65e5\u5fd7\uff08logging\uff09\u3002 \u5728\u4fdd\u8bc1\u4ee3\u7801\u884c\u4e3a\u4e0d\u53d8\u7684\u524d\u63d0\u4e0b\uff0c\u4fee\u6539\u4ee3\u7801\u7ed3\u6784\uff0c\u79f0\u4e3a \u91cd\u6784 \u3002\u91cd\u6784\u7ecf\u5e38\u88ab\u7528\u6765\u4fee\u6539\u4ee3\u7801\u4f7f\u5176\u66f4\u52a0\u6709\u6761\u7406\u3001\u6a21\u5757\u5316\u6216\u8005\u4f7f\u5176\u6027\u80fd\u66f4\u597d\u3002 \u5355\u5143\u6d4b\u8bd5\u662f\u4e00\u79cd\u8f6f\u4ef6\u6d4b\u8bd5\u65b9\u6cd5\uff0c\u5b83\u5bf9\u4e00\u4e2a\u4e2a\u5355\u5143\u7684\u6e90\u7801\u8fdb\u884c\u6d4b\u8bd5\u6765\u5224\u65ad\u8f6f\u4ef6\u4ee3\u7801\u662f\u5426\u6b63\u786e\u3002 \u9632\u5fa1\u5f0f\u7f16\u7a0b\u601d\u60f3\u8981\u6c42\u7a0b\u5e8f\u5458\u9884\u6d4b\u8f6f\u4ef6\u53ef\u80fd\u906d\u9047\u7684\u7684\u5404\u79cd\u8bef\u7528\u3002\u8fd9\u4e9b\u8bef\u7528\u901a\u5e38\u662f\u53ef\u4ee5\u88ab\u68c0\u6d4b\u548c\u9884\u9632\u7684\u3002 \u5bf9\u7a0b\u5e8f\u8fd0\u884c\u8fdb\u884c\u8ffd\u8e2a\u7684\u5404\u9879\u4fe1\u606f\uff08\u53d8\u91cf\u7684\u503c\u3001\u88ab\u8c03\u7528\u7684\u51fd\u6570\u3001\u5f53\u524d\u6267\u884c\u70b9\uff09\u90fd\u662f\u7a0b\u5e8f\u72b6\u6001\u7684\u7b2c\u4e00\u90e8\u5206\u3002 3.6 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u5355\u6b65\u8c03\u8bd5 3.7 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8fd0\u884c\u548c\u65ad\u70b9 3.8 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u76d1\u63a7\u53d8\u91cf 3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808 \u8c03\u8bd5\u5668\uff08debugger\uff09\u53ef\u4ee5\u5e2e\u52a9\u7a0b\u5e8f\u5458\u63a7\u5236\u7a0b\u5e8f\u7684\u8fd0\u4ee5\u53ca\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u67e5\u770b\u7a0b\u5e8f\u72b6\u6001\u3002\u96c6\u6210\u8c03\u8bd5\u5668\u6307\u7684\u662f\u54ea\u4e9b\u96c6\u6210\u5728\u4ee3\u7801\u7f16\u8f91\u5668\u4e2d\u7684\u8c03\u8bd5\u5668\u3002 - \u5355\u6b65\u8c03\u8bd5\u662f\u8c03\u8bd5\u5668\u7684\u4e00\u4e2a\u529f\u80fd\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u9010\u8bed\u53e5\u5730\u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8c03\u8bd5\uff1b - \u5355\u6b65\u8fdb\u5165\u7a0b\u5e8f\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u8fd0\u884c\u4e0b\u4e00\u4e2a\u6761\u8bed\u53e5\u7136\u540e\u505c\u6b62\u3002\u5982\u679c\u8be5\u8bed\u53e5\u4e2d\u5305\u542b\u51fd\u6570\u8c03\u7528\uff0c\u5355\u6b65\u8fdb\u5165\uff08step into\uff09\u53ef\u4ee5\u8df3\u8f6c\u5230\u8be5\u51fd\u6570\u7684\u9876\u90e8\uff1b - \u5355\u6b65\u8df3\u8fc7\u4f1a\u6267\u884c\u4e0b\u4e00\u6761\u8bed\u53e5\uff0c\u7136\u540e\u505c\u6b62\u6267\u884c\u3002\u5982\u679c\u8be5\u8bed\u53e5\u5305\u542b\u51fd\u6570\u8c03\u7528\uff0c\u5219\u5355\u6b65\u8df3\u8fc7\u4f1a\u6267\u884c\u8be5\u51fd\u6570\u5e76\u5c06\u63a7\u5236\u6743\u4ea4\u8fd8\u7ed9\u4f60\uff1b - \u5355\u6b65\u8df3\u51fa\u4f1a\u6267\u884c\u51fd\u6570\u4e2d\u5269\u4e0b\u7684\u8bed\u53e5\u7136\u540e\u5728\u51fd\u6570\u8fd4\u56de\u540e\u5c06\u63a7\u5236\u6743\u4ea4\u7ed9\u4f60\uff1b - \u8fd0\u884c\u5230\u5149\u6807\u51fa\u4f1a\u6301\u7eed\u6267\u884c\u4ee3\u7801\u76f4\u5230\u5230\u8fbe\u4f60\u9f20\u6807\u6240\u9009\u5904\uff1b - \u7ee7\u7eed\u6267\u884c\uff0c\u4f1a\u6301\u7eed\u6267\u884c\u7a0b\u5e8f\u4ee3\u7801\u76f4\u5230\u9047\u5230\u4e0b\u4e00\u4e2a\u65ad\u70b9\uff1b - \u542f\u52a8\u6267\u884c\u548c\u7ee7\u7eed\u6267\u884c\u7c7b\u4f3c\uff0c\u4e0d\u8fc7\u5b83\u4f1a\u4ece\u7a0b\u5e8f\u7684\u5f00\u59cb\u5904\u8fdb\u884c\u6267\u884c\uff1b - \u65ad\u70b9 \u662f\u6807\u8bb0\u8c03\u8bd5\u5668\u5e94\u8be5\u505c\u6b62\u6267\u884c\u7a0b\u5e8f\u7684\u4f4d\u7f6e\uff1b - \u76d1\u89c6\u53d8\u91cf\u5141\u8bb8\u6211\u4eec\u5728\u7a0b\u5e8f\u8fd0\u884c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u67e5\u770b\u53d8\u91cf\u7684\u503c\u3002\u76d1\u89c6\u7a97\u53e3\u4e2d\u53ef\u4ee5\u770b\u5230\u88ab\u76d1\u89c6\u7684\u53d8\u91cf\u548c\u8868\u8fbe\u5f0f\uff1b - \u8c03\u7528\u6808\u662f\u4e00\u4e2a\u5305\u542b\u4e86\u5f53\u524d\u6d3b\u52a8\u51fd\u6570\u7684\u5217\u8868\u3002\u8c03\u7528\u6808\u7a97\u53e3\u4e2d\u4f1a\u663e\u793a\u7a0b\u5e8f\u7684\u8c03\u7528\u6808\u3002","title":"3.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4ee3\u7801\u8c03\u8bd5"},{"location":"4-1-Introduction-to-fundamental-data-types/","tags":["data type","C++11"],"text":"Key Takeaway \u53d8\u91cf\u53ea\u662f\u4e00\u6bb5\u53ef\u4ee5\u5b58\u653e\u4fe1\u606f\u7684\u5185\u5b58\u7684\u540d\u79f0 \u4f7f\u7528 \u6570\u636e\u7c7b\u578b \uff08\u901a\u5e38\u7b80\u79f0\u4e3a\u7c7b\u578b\uff09\u544a\u8bc9\u8ba1\u7b97\u673a\u5982\u4f55\u5c06\u5185\u5b58\u4e2d\u7684\u6570\u636e\u89e3\u6790\u4e3a\u6709\u610f\u4e49\u7684\u503c \u4e00\u4e2abyte\u901a\u5e38\u7b49\u4e8e8\u4e2abit \u201c\u6574\u6570\u578b\u7684\u7c7b\u578b\u201d\uff0c\u5305\u62ec\u5e03\u5c14\u7c7b\u578b\u3001\u5b57\u7b26\u3001\u6574\u578b\u548c\u679a\u4e3e \u5b57\u7b26\u4e32\u4e0d\u5c5e\u4e8e C++ \u7684\u57fa\u672c\u7c7b\u578b\uff0c\u5b83\u5c5e\u4e8e\u590d\u5408\u7c7b\u578b\uff0c\u7531 std::string \u5b9a\u4e49 Bits\u3001bytes \u548c\u5185\u5b58\u5730\u5740 \u5728 1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u4e8b\u5b9e\u4e0a\u53d8\u91cf\u53ea\u662f\u4e00\u6bb5\u53ef\u4ee5\u5b58\u653e\u4fe1\u606f\u7684\u5185\u5b58\u7684\u540d\u79f0\u3002\u7b80\u5355\u56de\u5fc6\u4e00\u4e0b\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u4f7f\u7528\u8ba1\u7b97\u673a\u63d0\u4f9b\u7684 \u968f\u673a\u8bbf\u95ee\u5185\u5b58(RAM) \u3002\u5f53\u53d8\u91cf\u88ab\u5b9a\u4e49\u7684\u65f6\u5019\uff0c\u4e00\u5757\u5185\u5b58\u88ab\u5173\u8054\u5230\u4e86\u8be5\u53d8\u91cf\u3002 \u5185\u5b58\u7684\u6700\u5c0f\u5355\u5143\u662f\u4e00\u4e2a\u4e8c\u8fdb\u5236\u4f4d\uff08\u4e5f\u79f0\u4e3a \u4f4d \uff09\uff0c\u5b83\u53ef\u4ee5\u5b58\u50a8 0 \u6216 1.\u4f60\u53ef\u4ee5\u628a\u4e00\u4e2a\u6bd4\u7279\u770b\u6210\u662f\u4e00\u4e2a\u706f\u6ce1\u7684\u5f00\u5173\u2014\u2014\u5b83\u53ea\u6709\u5f00\uff081\uff09\u548c\u5173\uff080\uff09\u4e24\u79cd\u72b6\u6001\uff0c\u6ca1\u6709\u5176\u4ed6\u4efb\u4f55\u4e2d\u95f4\u72b6\u6001\u3002\u5982\u679c\u4f60\u53bb\u89c2\u5bdf\u4e00\u6bb5\u5185\u5b58\u5185\u5bb9\u7684\u8bdd\uff0c\u4f60\u603b\u80fd\u770b\u5230 \u2026011010100101010\u2026 \u6216\u5176\u4ed6\u7c7b\u4f3c\u7ec4\u5408\u3002 \u6211\u4eec\u5c06\u5185\u5b58\u5206\u5272\u6210\u4e00\u7cfb\u5217\u8fde\u7eed\u7684\u5355\u5143\uff0c\u79f0\u4e3a\u5185\u5b58\u5730\u5740\u3002\u7c7b\u4f3c\u4e8e\u8857\u9053\u5730\u5740\u53ef\u4ee5\u7528\u6765\u67e5\u627e\u8857\u9053\u4e0a\u67d0\u5e62\u5efa\u7b51\uff0c\u901a\u8fc7\u5185\u5b58\u5730\u5740\u6211\u4eec\u4e5f\u53ef\u4ee5\u627e\u5230\u7279\u5b9a\u5730\u5740\u4e2d\u5b58\u653e\u7684\u5185\u5bb9\u3002 \u4e0d\u591f\u6709\u4ef6\u4e8b\u53ef\u80fd\u51fa\u4e4e\u4f60\u7684\u610f\u6599\uff0c\u5bf9\u4e8e\u73b0\u4ee3\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u6765\u8bf4\uff0c\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684bit\u5e76\u4e0d\u90fd\u5177\u6709\u4e00\u4e2a\u552f\u4e00\u7684\u5730\u5740\u3002\u8fd9\u662f\u56e0\u4e3a\u5185\u5b58\u5730\u5740\u7684\u6570\u91cf\u662f\u6709\u9650\u7684\uff0c\u800c\u4e14\u4e5f\u5e76\u4e0d\u9700\u8981\u4e3a\u6bcf\u4e2abit\u90fd\u6307\u5b9a\u4e00\u4e2a\u552f\u4e00\u7684\u5730\u5740\u3002\u5b9e\u9645\u4e0a\uff0c\u6bcf\u4e2a\u5185\u5b58\u5730\u5740\u8868\u793a\u4e00\u4e2a \u5b57\u8282 \u7684\u5730\u5740\u3002\u4e00\u4e2a\u5b57\u8282\u8868\u793a\u4e00\u7ec4\u88ab\u4f5c\u4e3a\u4e00\u4e2a\u5355\u5143\u64cd\u4f5c\u7684\u6bd4\u7279\u3002\u4e00\u822c\u6765\u8bb2\uff08\u73b0\u4ee3\u8ba1\u7b97\u673a\uff09\u4e00\u4e2a\u5b57\u8282\u5305\u542b8\u4e2a\u8fde\u7eed\u7684 bit\u3002 \u5173\u952e\u4fe1\u606f \u5728 C++\uff0c\u6211\u4eec\u901a\u5e38\u4ee5 byte \u4e3a\u5355\u4f4d\u64cd\u4f5c\u6570\u636e\u3002 \u4e0b\u56fe\u663e\u793a\u4e86\u4e00\u4e9b\u5185\u5b58\u5730\u5740\u53ca\u5176\u5185\u90e8\u5b58\u653e\u7684\u6570\u636e\uff08byte\u4e3a\u5355\u4f4d\uff09\uff1a \u9898\u5916\u8bdd \u5728\u4e00\u4e9b\u8001\u7684\u6216\u8005\u975e\u6807\u7684\u8ba1\u7b97\u673a\u4e0a\uff0c\u4e00\u4e2a\u5b57\u8282\u53ef\u80fd\u8868\u793a\u4e0d\u540c\u957f\u5ea6\u7684\u6bd4\u7279\uff081 \u5230 48 \u6bd4\u7279\u90fd\u6709\u53ef\u80fd\uff09\u2014\u2014 \u4e0d\u8fc7\uff0c\u4f60\u901a\u5e38\u4e0d\u9700\u8981\u62c5\u5fc3\u8fd9\u4e2a\u95ee\u9898\uff0c\u56e0\u4e3a\u73b0\u4ee3\u8ba1\u7b97\u673a\u7684\u4e8b\u5b9e\u6807\u51c6\u5c318\u6bd4\u7279\uff0c\u6211\u4eec\u53ef\u4ee5\u5047\u8bbe1\u4e2a\u5b57\u8282\u5c31\u662f8\u4e2a\u6bd4\u7279\u3002 \u6570\u636e\u7c7b\u578b \u7531\u4e8e\u8ba1\u7b97\u673a\u4e0a\u6240\u6709\u7684\u6570\u636e\u90fd\u53ea\u662f\u4e00\u4e9b\u8fde\u7eed\u7684\u6bd4\u7279\uff0c\u6211\u4eec\u5fc5\u987b\u8981\u4f7f\u7528 \u6570\u636e\u7c7b\u578b \uff08\u901a\u5e38\u7b80\u79f0\u4e3a\u7c7b\u578b\uff09\u544a\u8bc9\u8ba1\u7b97\u673a\u5982\u4f55\u5c06\u5185\u5b58\u4e2d\u7684\u6570\u636e\u89e3\u6790\u4e3a\u6709\u610f\u4e49\u7684\u503c\u3002\u6211\u4eec\u5df2\u7ecf\u5b66\u4e60\u4e86\u4e00\u79cd\u6570\u636e\u7c7b\u578b\uff1a\u6574\u578b\u3002\u5f53\u4e00\u4e2a\u6574\u578b\u53d8\u91cf\u88ab\u5b9a\u4e49\u65f6\uff0c\u6211\u4eec\u544a\u8bc9\u8ba1\u7b97\u673a\u201d\u8be5\u53d8\u91cf\u4f7f\u7528\u7684\u8fd9\u4e00\u6bb5\u5185\u5b58\u9700\u8981\u88ab\u89e3\u6790\u4e3a\u4e00\u4e2a\u6574\u578b\u503c\u201c\u3002 \u5f53\u4e3a\u4e00\u4e2a\u5bf9\u8c61\u8fdb\u884c\u8d4b\u503c\u65f6\uff0c\u7f16\u8bd1\u5668\u548cCPU\u4f1a\u5c06\u8fd9\u4e2a\u503c\u7f16\u7801\u6210\u4e00\u7cfb\u5217\u8fde\u7eed\u7684 bit\u7136\u540e\u5b58\u653e\u5230\u5185\u5b58\u4e2d\uff08\u6ce8\u610f\uff1a\u5185\u5b58\u53ea\u80fd\u5b58\u653e\u6bd4\u7279\uff09\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5c06\u4e00\u4e2a\u6574\u578b\u7684\u5bf9\u8c61\u8d4b\u503c\u4e3a65\uff0c\u5219\u8be5\u503c\u4f1a\u88ab\u8f6c\u6362\u4e3a 0100 0001 \u540e\u5b58\u653e\u5728\u5185\u5b58\u5173\u8054\u7684\u5730\u5740\u4e2d\u3002 \u53cd\u8fc7\u6765\uff0c\u5f53\u5bf9\u8c61\u88ab\u6c42\u503c\u65f6\uff0c\u8fd9\u4e9b\u8fde\u7eed\u7684 bit \u4f1a\u88ab\u91cd\u65b0\u7ec4\u7ec7\u6210\u539f\u6765\u7684\u503c\uff0c\u5373 0100 0001 \u4f1a\u88ab\u8f6c\u6362\u4e3a 65\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u7f16\u8bd1\u5668\u548cCPU\u4f1a\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u4e0a\u8ff0\u5de5\u4f5c\uff0c\u6211\u4eec\u901a\u5e38\u4e0d\u9700\u8981\u62c5\u5fc3\u8fd9\u4e9b\u503c\u662f\u5982\u4f55\u88ab\u8f6c\u6362\u7684\u3002 \u6211\u4eec\u7684\u5de5\u4f5c\u4ec5\u4ec5\u662f\u4e3a\u53d8\u91cf\u6307\u5b9a\u4e00\u4e2a\u6211\u4eec\u9700\u8981\u7684\u503c\u3002 \u57fa\u672c\u6570\u636e\u7c7b\u578b C++ \u5177\u6709\u5f88\u591a\u5185\u7f6e\u7684\u6570\u636e\u7c7b\u578b\uff0c\u79f0\u4e3a\u57fa\u7840\u6570\u636e\u7c7b\u578b\uff0c\u4e5f\u7ecf\u5e38\u88ab\u79f0\u4e3a\u57fa\u672c\u7c7b\u578b\u3001\u539f\u59cb\u7c7b\u578b\u6216\u5185\u7f6e\u7c7b\u578b\u3002 \u4e0b\u8868\u662f\u4e00\u4e9b\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u6709\u4e9b\u53ef\u80fd\u4f60\u5df2\u7ecf\u89c1\u8fc7\u4e86\uff1a \u7c7b\u578b \u5206\u7c7b \u542b\u4e49 \u4f8b\u5b50 float Floating Point a number with a fractional part 3.14159 double Floating Point a number with a fractional part 3.14159 long double Floating Point a number with a fractional part 3.14159 bool Integral (Boolean) true or false true char Integral (Character) a single character of text \u2018c\u2019 wchar_t Integral (Character) a single character of text \u2018c\u2019 char8_t (C++20) Integral (Character) a single character of text \u2018c\u2019 char16_t (C++11) Integral (Character) a single character of text \u2018c\u2019 char32_t (C++11) Integral (Character) a single character of text \u2018c\u2019 short Integral (Integer) positive and negative whole numbers, including 0 64 int Integral (Integer) positive and negative whole numbers, including 0 64 long Integral (Integer) positive and negative whole numbers, including 0 64 long long (C++11) Integral (Integer) positive and negative whole numbers, including 0 64 std::nullptr_t (C++11) Null Pointer a null pointer nullptr void Void no type n/a \u672c\u7ae0\u5c06\u76f8\u7ee7\u4ecb\u7ecd\u8fd9\u4e9b\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff08\u9664\u4e86 std::nullptr_t \uff0c\u6211\u4eec\u4f1a\u5728\u8ba8\u8bba\u6307\u9488\u65f6\u8ba8\u8bba\u5b83\uff09\u3002C++ \u4e5f\u652f\u6301\u5f88\u591a\u590d\u6742\u7c7b\u578b\uff0c\u79f0\u4e3a \u590d\u5408\u7c7b\u578b \uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u7ae0\u8282\u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u4f5c\u8005\u6ce8 \u672f\u8bed\u6574\u578b\uff08interger\uff09\u548c\u6574\u6570\u7684\uff08integral\uff09\u7c7b\u4f3c\uff0c\u4f46\u662f\u610f\u601d\u5e76\u4e0d\u5b8c\u5168\u76f8\u540c\u3002 \u6574\u578b \u6307\u7684\u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u653e\u975e\u5c0f\u6570\u503c\u7684\u7279\u5b9a\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u6574\u6570\u30010\u6216\u8005\u8d1f\u6574\u6570\u3002\u800c \u6574\u6570\u578b\u7684 \u5219\u8868\u793a\u201c\u50cf\u6574\u578b\u201d\u3002\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6574\u578b\u7684\u4e00\u8bcd\u88ab\u7ec4\u6210\u5176\u4ed6\u672f\u8bed\uff0c\u4f8b\u5982\u201c\u6574\u6570\u578b\u7684\u7c7b\u578b\u201d\uff0c\u5305\u62ec\u5e03\u5c14\u7c7b\u578b\u3001\u5b57\u7b26\u548c\u6574\u578b\uff08\u8fd8\u5305\u62ec\u679a\u4e3e\u2014\u2014\u5728\u7b2c\u4e5d\u7ae0\u4f1a\u8fdb\u884c\u4ecb\u7ecd\uff09\u3002\u201c\u6574\u6570\u578b\u7684\u7c7b\u578b\u201d\u4e4b\u6240\u4ee5\u8fd9\u4e48\u547d\u540d\uff0c\u662f\u56e0\u4e3a\u8fd9\u4e9b\u7c7b\u578b\u7684\u6570\u636e\u5728\u5185\u5b58\u4e2d\u662f\u4ee5\u6574\u578b\u6570\u5b58\u653e\u7684\uff0c\u5373\u4f7f\u5b83\u4eec\u7684\u884c\u4e3a\u53ef\u80fd\u4e0d\u540c\uff08\u7a0d\u540e\u6211\u4eec\u4f1a\u5728\u8ba8\u8bba\u5b57\u7b26\u7c7b\u578b\u65f6\u4e86\u89e3\u5176\u5dee\u5f02\uff09\u3002 \u4f5c\u8005\u6ce8 \u5927\u591a\u6570\u73b0\u4ee3\u7f16\u7a0b\u8bed\u8a00\u90fd\u5305\u542b\u57fa\u672c\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\uff08\u5b57\u7b26\u4e32\u6307\u7684\u662f\u5305\u542b\u4e00\u7cfb\u5217\u5b57\u7b26\u7684\u6570\u636e\uff0c\u901a\u5e38\u7528\u6765\u8868\u793a\u6587\u672c\uff09\u3002\u5728C++\u4e2d\uff0c\u5b57\u7b26\u4e32\u5e76\u4e0d\u5c5e\u4e8e\u57fa\u672c\u7c7b\u578b\uff08\u5b83\u5c5e\u4e8e\u590d\u5408\u7c7b\u578b\uff09\u3002\u4f46\u662f\u56e0\u4e3a\u5b57\u7b26\u4e32\u7684\u4f7f\u7528\u65b9\u6cd5\u5f88\u76f4\u63a5\uff0c\u800c\u4e14\u4e5f\u975e\u5e38\u6709\u7528\uff0c\u56e0\u6b64\u5728\u672c\u7ae0\u4e2d\u6211\u4eec\u4e5f\u4f1a\u4ecb\u7ecd\u5b57\u7b26\u4e32\u7c7b\u578b\uff08 4.13 - std::string \u7b80\u4ecb \uff09\u3002 The _t suffix \u5f88\u591a\u5728\u65b0\u7248\u672c C++ \u4e2d\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982 std::nullptr_t \uff09\u90fd\u5305\u542b\u4e00\u4e2a _t \u540e\u7f00\u3002\u8fd9\u4e2a\u540e\u7f00\u8868\u793a type\uff08\u7c7b\u578b\uff09\uff0c\u8fd9\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7528\u4e8e\u73b0\u4ee3\u7c7b\u578b\u7684\u547d\u540d\u65b9\u5f0f\u3002 \u5982\u679c\u4f60\u9047\u5230\u4e86\u5e26\u6709 _t \u540e\u7f00\u7684\u6807\u8bc6\u7b26\uff0c\u5b83\u5f88\u53ef\u80fd\u662f\u4e00\u4e2a\u7c7b\u578b\uff0c\u4f46\u662f\u4e5f\u6709\u5f88\u591a\u7c7b\u578b\u662f\u4e0d\u5305\u542b\u8be5\u540e\u7f00\u7684\uff0c\u6240\u4ee5\u8be5\u547d\u540d\u89c4\u5219\u5e76\u4e0d\u662f\u88ab\u4e00\u8d2f\u9075\u5b88\u7684\u3002","title":"4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb"},{"location":"4-1-Introduction-to-fundamental-data-types/#bitsbytes","text":"\u5728 1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u4e8b\u5b9e\u4e0a\u53d8\u91cf\u53ea\u662f\u4e00\u6bb5\u53ef\u4ee5\u5b58\u653e\u4fe1\u606f\u7684\u5185\u5b58\u7684\u540d\u79f0\u3002\u7b80\u5355\u56de\u5fc6\u4e00\u4e0b\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u4f7f\u7528\u8ba1\u7b97\u673a\u63d0\u4f9b\u7684 \u968f\u673a\u8bbf\u95ee\u5185\u5b58(RAM) \u3002\u5f53\u53d8\u91cf\u88ab\u5b9a\u4e49\u7684\u65f6\u5019\uff0c\u4e00\u5757\u5185\u5b58\u88ab\u5173\u8054\u5230\u4e86\u8be5\u53d8\u91cf\u3002 \u5185\u5b58\u7684\u6700\u5c0f\u5355\u5143\u662f\u4e00\u4e2a\u4e8c\u8fdb\u5236\u4f4d\uff08\u4e5f\u79f0\u4e3a \u4f4d \uff09\uff0c\u5b83\u53ef\u4ee5\u5b58\u50a8 0 \u6216 1.\u4f60\u53ef\u4ee5\u628a\u4e00\u4e2a\u6bd4\u7279\u770b\u6210\u662f\u4e00\u4e2a\u706f\u6ce1\u7684\u5f00\u5173\u2014\u2014\u5b83\u53ea\u6709\u5f00\uff081\uff09\u548c\u5173\uff080\uff09\u4e24\u79cd\u72b6\u6001\uff0c\u6ca1\u6709\u5176\u4ed6\u4efb\u4f55\u4e2d\u95f4\u72b6\u6001\u3002\u5982\u679c\u4f60\u53bb\u89c2\u5bdf\u4e00\u6bb5\u5185\u5b58\u5185\u5bb9\u7684\u8bdd\uff0c\u4f60\u603b\u80fd\u770b\u5230 \u2026011010100101010\u2026 \u6216\u5176\u4ed6\u7c7b\u4f3c\u7ec4\u5408\u3002 \u6211\u4eec\u5c06\u5185\u5b58\u5206\u5272\u6210\u4e00\u7cfb\u5217\u8fde\u7eed\u7684\u5355\u5143\uff0c\u79f0\u4e3a\u5185\u5b58\u5730\u5740\u3002\u7c7b\u4f3c\u4e8e\u8857\u9053\u5730\u5740\u53ef\u4ee5\u7528\u6765\u67e5\u627e\u8857\u9053\u4e0a\u67d0\u5e62\u5efa\u7b51\uff0c\u901a\u8fc7\u5185\u5b58\u5730\u5740\u6211\u4eec\u4e5f\u53ef\u4ee5\u627e\u5230\u7279\u5b9a\u5730\u5740\u4e2d\u5b58\u653e\u7684\u5185\u5bb9\u3002 \u4e0d\u591f\u6709\u4ef6\u4e8b\u53ef\u80fd\u51fa\u4e4e\u4f60\u7684\u610f\u6599\uff0c\u5bf9\u4e8e\u73b0\u4ee3\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u6765\u8bf4\uff0c\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684bit\u5e76\u4e0d\u90fd\u5177\u6709\u4e00\u4e2a\u552f\u4e00\u7684\u5730\u5740\u3002\u8fd9\u662f\u56e0\u4e3a\u5185\u5b58\u5730\u5740\u7684\u6570\u91cf\u662f\u6709\u9650\u7684\uff0c\u800c\u4e14\u4e5f\u5e76\u4e0d\u9700\u8981\u4e3a\u6bcf\u4e2abit\u90fd\u6307\u5b9a\u4e00\u4e2a\u552f\u4e00\u7684\u5730\u5740\u3002\u5b9e\u9645\u4e0a\uff0c\u6bcf\u4e2a\u5185\u5b58\u5730\u5740\u8868\u793a\u4e00\u4e2a \u5b57\u8282 \u7684\u5730\u5740\u3002\u4e00\u4e2a\u5b57\u8282\u8868\u793a\u4e00\u7ec4\u88ab\u4f5c\u4e3a\u4e00\u4e2a\u5355\u5143\u64cd\u4f5c\u7684\u6bd4\u7279\u3002\u4e00\u822c\u6765\u8bb2\uff08\u73b0\u4ee3\u8ba1\u7b97\u673a\uff09\u4e00\u4e2a\u5b57\u8282\u5305\u542b8\u4e2a\u8fde\u7eed\u7684 bit\u3002 \u5173\u952e\u4fe1\u606f \u5728 C++\uff0c\u6211\u4eec\u901a\u5e38\u4ee5 byte \u4e3a\u5355\u4f4d\u64cd\u4f5c\u6570\u636e\u3002 \u4e0b\u56fe\u663e\u793a\u4e86\u4e00\u4e9b\u5185\u5b58\u5730\u5740\u53ca\u5176\u5185\u90e8\u5b58\u653e\u7684\u6570\u636e\uff08byte\u4e3a\u5355\u4f4d\uff09\uff1a \u9898\u5916\u8bdd \u5728\u4e00\u4e9b\u8001\u7684\u6216\u8005\u975e\u6807\u7684\u8ba1\u7b97\u673a\u4e0a\uff0c\u4e00\u4e2a\u5b57\u8282\u53ef\u80fd\u8868\u793a\u4e0d\u540c\u957f\u5ea6\u7684\u6bd4\u7279\uff081 \u5230 48 \u6bd4\u7279\u90fd\u6709\u53ef\u80fd\uff09\u2014\u2014 \u4e0d\u8fc7\uff0c\u4f60\u901a\u5e38\u4e0d\u9700\u8981\u62c5\u5fc3\u8fd9\u4e2a\u95ee\u9898\uff0c\u56e0\u4e3a\u73b0\u4ee3\u8ba1\u7b97\u673a\u7684\u4e8b\u5b9e\u6807\u51c6\u5c318\u6bd4\u7279\uff0c\u6211\u4eec\u53ef\u4ee5\u5047\u8bbe1\u4e2a\u5b57\u8282\u5c31\u662f8\u4e2a\u6bd4\u7279\u3002 \u6570\u636e\u7c7b\u578b \u7531\u4e8e\u8ba1\u7b97\u673a\u4e0a\u6240\u6709\u7684\u6570\u636e\u90fd\u53ea\u662f\u4e00\u4e9b\u8fde\u7eed\u7684\u6bd4\u7279\uff0c\u6211\u4eec\u5fc5\u987b\u8981\u4f7f\u7528 \u6570\u636e\u7c7b\u578b \uff08\u901a\u5e38\u7b80\u79f0\u4e3a\u7c7b\u578b\uff09\u544a\u8bc9\u8ba1\u7b97\u673a\u5982\u4f55\u5c06\u5185\u5b58\u4e2d\u7684\u6570\u636e\u89e3\u6790\u4e3a\u6709\u610f\u4e49\u7684\u503c\u3002\u6211\u4eec\u5df2\u7ecf\u5b66\u4e60\u4e86\u4e00\u79cd\u6570\u636e\u7c7b\u578b\uff1a\u6574\u578b\u3002\u5f53\u4e00\u4e2a\u6574\u578b\u53d8\u91cf\u88ab\u5b9a\u4e49\u65f6\uff0c\u6211\u4eec\u544a\u8bc9\u8ba1\u7b97\u673a\u201d\u8be5\u53d8\u91cf\u4f7f\u7528\u7684\u8fd9\u4e00\u6bb5\u5185\u5b58\u9700\u8981\u88ab\u89e3\u6790\u4e3a\u4e00\u4e2a\u6574\u578b\u503c\u201c\u3002 \u5f53\u4e3a\u4e00\u4e2a\u5bf9\u8c61\u8fdb\u884c\u8d4b\u503c\u65f6\uff0c\u7f16\u8bd1\u5668\u548cCPU\u4f1a\u5c06\u8fd9\u4e2a\u503c\u7f16\u7801\u6210\u4e00\u7cfb\u5217\u8fde\u7eed\u7684 bit\u7136\u540e\u5b58\u653e\u5230\u5185\u5b58\u4e2d\uff08\u6ce8\u610f\uff1a\u5185\u5b58\u53ea\u80fd\u5b58\u653e\u6bd4\u7279\uff09\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5c06\u4e00\u4e2a\u6574\u578b\u7684\u5bf9\u8c61\u8d4b\u503c\u4e3a65\uff0c\u5219\u8be5\u503c\u4f1a\u88ab\u8f6c\u6362\u4e3a 0100 0001 \u540e\u5b58\u653e\u5728\u5185\u5b58\u5173\u8054\u7684\u5730\u5740\u4e2d\u3002 \u53cd\u8fc7\u6765\uff0c\u5f53\u5bf9\u8c61\u88ab\u6c42\u503c\u65f6\uff0c\u8fd9\u4e9b\u8fde\u7eed\u7684 bit \u4f1a\u88ab\u91cd\u65b0\u7ec4\u7ec7\u6210\u539f\u6765\u7684\u503c\uff0c\u5373 0100 0001 \u4f1a\u88ab\u8f6c\u6362\u4e3a 65\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u7f16\u8bd1\u5668\u548cCPU\u4f1a\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u4e0a\u8ff0\u5de5\u4f5c\uff0c\u6211\u4eec\u901a\u5e38\u4e0d\u9700\u8981\u62c5\u5fc3\u8fd9\u4e9b\u503c\u662f\u5982\u4f55\u88ab\u8f6c\u6362\u7684\u3002 \u6211\u4eec\u7684\u5de5\u4f5c\u4ec5\u4ec5\u662f\u4e3a\u53d8\u91cf\u6307\u5b9a\u4e00\u4e2a\u6211\u4eec\u9700\u8981\u7684\u503c\u3002","title":"Bits\u3001bytes \u548c\u5185\u5b58\u5730\u5740"},{"location":"4-1-Introduction-to-fundamental-data-types/#_1","text":"C++ \u5177\u6709\u5f88\u591a\u5185\u7f6e\u7684\u6570\u636e\u7c7b\u578b\uff0c\u79f0\u4e3a\u57fa\u7840\u6570\u636e\u7c7b\u578b\uff0c\u4e5f\u7ecf\u5e38\u88ab\u79f0\u4e3a\u57fa\u672c\u7c7b\u578b\u3001\u539f\u59cb\u7c7b\u578b\u6216\u5185\u7f6e\u7c7b\u578b\u3002 \u4e0b\u8868\u662f\u4e00\u4e9b\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u6709\u4e9b\u53ef\u80fd\u4f60\u5df2\u7ecf\u89c1\u8fc7\u4e86\uff1a \u7c7b\u578b \u5206\u7c7b \u542b\u4e49 \u4f8b\u5b50 float Floating Point a number with a fractional part 3.14159 double Floating Point a number with a fractional part 3.14159 long double Floating Point a number with a fractional part 3.14159 bool Integral (Boolean) true or false true char Integral (Character) a single character of text \u2018c\u2019 wchar_t Integral (Character) a single character of text \u2018c\u2019 char8_t (C++20) Integral (Character) a single character of text \u2018c\u2019 char16_t (C++11) Integral (Character) a single character of text \u2018c\u2019 char32_t (C++11) Integral (Character) a single character of text \u2018c\u2019 short Integral (Integer) positive and negative whole numbers, including 0 64 int Integral (Integer) positive and negative whole numbers, including 0 64 long Integral (Integer) positive and negative whole numbers, including 0 64 long long (C++11) Integral (Integer) positive and negative whole numbers, including 0 64 std::nullptr_t (C++11) Null Pointer a null pointer nullptr void Void no type n/a \u672c\u7ae0\u5c06\u76f8\u7ee7\u4ecb\u7ecd\u8fd9\u4e9b\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff08\u9664\u4e86 std::nullptr_t \uff0c\u6211\u4eec\u4f1a\u5728\u8ba8\u8bba\u6307\u9488\u65f6\u8ba8\u8bba\u5b83\uff09\u3002C++ \u4e5f\u652f\u6301\u5f88\u591a\u590d\u6742\u7c7b\u578b\uff0c\u79f0\u4e3a \u590d\u5408\u7c7b\u578b \uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u7ae0\u8282\u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u4f5c\u8005\u6ce8 \u672f\u8bed\u6574\u578b\uff08interger\uff09\u548c\u6574\u6570\u7684\uff08integral\uff09\u7c7b\u4f3c\uff0c\u4f46\u662f\u610f\u601d\u5e76\u4e0d\u5b8c\u5168\u76f8\u540c\u3002 \u6574\u578b \u6307\u7684\u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u653e\u975e\u5c0f\u6570\u503c\u7684\u7279\u5b9a\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u6574\u6570\u30010\u6216\u8005\u8d1f\u6574\u6570\u3002\u800c \u6574\u6570\u578b\u7684 \u5219\u8868\u793a\u201c\u50cf\u6574\u578b\u201d\u3002\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6574\u578b\u7684\u4e00\u8bcd\u88ab\u7ec4\u6210\u5176\u4ed6\u672f\u8bed\uff0c\u4f8b\u5982\u201c\u6574\u6570\u578b\u7684\u7c7b\u578b\u201d\uff0c\u5305\u62ec\u5e03\u5c14\u7c7b\u578b\u3001\u5b57\u7b26\u548c\u6574\u578b\uff08\u8fd8\u5305\u62ec\u679a\u4e3e\u2014\u2014\u5728\u7b2c\u4e5d\u7ae0\u4f1a\u8fdb\u884c\u4ecb\u7ecd\uff09\u3002\u201c\u6574\u6570\u578b\u7684\u7c7b\u578b\u201d\u4e4b\u6240\u4ee5\u8fd9\u4e48\u547d\u540d\uff0c\u662f\u56e0\u4e3a\u8fd9\u4e9b\u7c7b\u578b\u7684\u6570\u636e\u5728\u5185\u5b58\u4e2d\u662f\u4ee5\u6574\u578b\u6570\u5b58\u653e\u7684\uff0c\u5373\u4f7f\u5b83\u4eec\u7684\u884c\u4e3a\u53ef\u80fd\u4e0d\u540c\uff08\u7a0d\u540e\u6211\u4eec\u4f1a\u5728\u8ba8\u8bba\u5b57\u7b26\u7c7b\u578b\u65f6\u4e86\u89e3\u5176\u5dee\u5f02\uff09\u3002 \u4f5c\u8005\u6ce8 \u5927\u591a\u6570\u73b0\u4ee3\u7f16\u7a0b\u8bed\u8a00\u90fd\u5305\u542b\u57fa\u672c\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\uff08\u5b57\u7b26\u4e32\u6307\u7684\u662f\u5305\u542b\u4e00\u7cfb\u5217\u5b57\u7b26\u7684\u6570\u636e\uff0c\u901a\u5e38\u7528\u6765\u8868\u793a\u6587\u672c\uff09\u3002\u5728C++\u4e2d\uff0c\u5b57\u7b26\u4e32\u5e76\u4e0d\u5c5e\u4e8e\u57fa\u672c\u7c7b\u578b\uff08\u5b83\u5c5e\u4e8e\u590d\u5408\u7c7b\u578b\uff09\u3002\u4f46\u662f\u56e0\u4e3a\u5b57\u7b26\u4e32\u7684\u4f7f\u7528\u65b9\u6cd5\u5f88\u76f4\u63a5\uff0c\u800c\u4e14\u4e5f\u975e\u5e38\u6709\u7528\uff0c\u56e0\u6b64\u5728\u672c\u7ae0\u4e2d\u6211\u4eec\u4e5f\u4f1a\u4ecb\u7ecd\u5b57\u7b26\u4e32\u7c7b\u578b\uff08 4.13 - std::string \u7b80\u4ecb \uff09\u3002","title":"\u57fa\u672c\u6570\u636e\u7c7b\u578b"},{"location":"4-1-Introduction-to-fundamental-data-types/#the-_t-suffix","text":"\u5f88\u591a\u5728\u65b0\u7248\u672c C++ \u4e2d\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982 std::nullptr_t \uff09\u90fd\u5305\u542b\u4e00\u4e2a _t \u540e\u7f00\u3002\u8fd9\u4e2a\u540e\u7f00\u8868\u793a type\uff08\u7c7b\u578b\uff09\uff0c\u8fd9\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7528\u4e8e\u73b0\u4ee3\u7c7b\u578b\u7684\u547d\u540d\u65b9\u5f0f\u3002 \u5982\u679c\u4f60\u9047\u5230\u4e86\u5e26\u6709 _t \u540e\u7f00\u7684\u6807\u8bc6\u7b26\uff0c\u5b83\u5f88\u53ef\u80fd\u662f\u4e00\u4e2a\u7c7b\u578b\uff0c\u4f46\u662f\u4e5f\u6709\u5f88\u591a\u7c7b\u578b\u662f\u4e0d\u5305\u542b\u8be5\u540e\u7f00\u7684\uff0c\u6240\u4ee5\u8be5\u547d\u540d\u89c4\u5219\u5e76\u4e0d\u662f\u88ab\u4e00\u8d2f\u9075\u5b88\u7684\u3002","title":"The _t suffix"},{"location":"4-10-Introduction-to-if-statements/","tags":["if","boolean"],"text":"Key Takeaway \u975e\u5e03\u5c14\u503c\u5728\u4f5c\u4e3a\u6761\u4ef6\u8868\u8fbe\u5f0f\u4f7f\u7528\u65f6\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff0c\u975e0\u503c\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c true \uff0c0\u503c\u5219\u88ab\u8f6c\u6362\u4e3a false \u60f3\u8c61\u4e00\u4e0b\uff0c\u5982\u679c\u4f60\u6b63\u8981\u53bb\u8d85\u5e02\uff0c\u7136\u540e\u4f60\u7684\u5ba4\u53cb\u5bf9\u4f60\u8bf4\uff1a\u201d\u5982\u679c\u6709\u8349\u8393\u7684\u8bdd\uff0c\u5c31\u4e70\u4e00\u70b9\u201c\u3002\u8fd9\u662f\u4e00\u4e2a\u6761\u4ef6\u8bed\u53e5\uff0c\u5c31\u662f\u8bf4\u4f60\u505a\u67d0\u4e2a\u52a8\u4f5c\uff08\"\u4e70\u4e1c\u897f\"\uff09\u7684\u6761\u4ef6\u662f\u67d0\u4e2a\u6761\u4ef6\uff08\u6709\u8349\u8393\u5356\uff09\u4e3a\u771f\u3002 \u8fd9\u79cd\u6761\u4ef6\u5728\u7f16\u7a0b\u4e2d\u975e\u5e38\u5e38\u89c1\uff0c\u6761\u4ef6\u8bed\u53e5\u4f7f\u6211\u4eec\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u67d0\u79cd\u6761\u4ef6\u884c\u4e3a\u3002C++ \u4e2d\u6700\u7b80\u5355\u7684\u6761\u4ef6\u8bed\u53e5\u662f if \u8bed\u53e5\u3002 if \u8bed\u53e5\u4f7f\u6211\u4eec\u53ef\u4ee5\u5728\u67d0\u4e2a\u6761\u4ef6\u4e3a\u771f\u65f6\u624d\u6267\u884c\u4e00\u884c\uff08\u6216\u591a\u884c\uff09\u4ee3\u7801\u3002 \u6700\u7b80\u5355\u7684 if \u8bed\u53e5\u5f62\u5f0f\u5982\u4e0b\uff1a 1 if (condition) true_statement; \u4e0b\u9762\u8fd9\u79cd\u5f62\u5f0f\u53ef\u8bfb\u6027\u66f4\u597d\uff1a 1 2 if (condition) true_statement; \u6240\u8c13\u201c\u6761\u4ef6\u201d (\u4e5f\u79f0\u4e3a\u6761\u4ef6\u8868\u8fbe\u5f0f)\u7684\u6c42\u503c\u7ed3\u679c\u662f\u4e00\u4e2a\u5e03\u5c14\u503c\u3002 \u5982\u679c if \u8bed\u53e5\u8bed\u53e5\u7684\u6c42\u503c\u7ed3\u679c\u4e3a\u5e03\u5c14\u503c true \uff0c\u5219 true_statement \u5c31\u4f1a\u6267\u884c\u3002\u53cd\u4e4b\uff0c\u5982\u56fe\u6761\u4ef6\u6c42\u503c\u7684\u7ed3\u679c\u4e3a\u5e03\u5c14\u503c false \uff0c\u5219\u8bed\u53e5 true_statement \u4f1a\u88ab\u8df3\u8fc7\u3002 \u4e00\u4e2a\u4f7f\u7528\u4e86 if \u8bed\u53e5\u7684\u4f8b\u7a0b\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; if ( x == 0 ) std :: cout << \"The value is zero \\n \" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Enter an integer: 0 The value is zero \u6211\u4eec\u4ed4\u7ec6\u5206\u6790\u4e00\u4e0b\u8fd9\u4e2a\u7a0b\u5e8f\u3002 \u9996\u5148\uff0c\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6574\u578b\u3002\u7136\u540e\u6761\u4ef6 x == 0 \u8fdb\u884c\u6c42\u503c\u3002 \u76f8\u7b49\u6bd4\u8f83\u8fd0\u7b97\u7b26 ( == ) \u88ab\u7528\u6765\u6d4b\u8bd5\u4e24\u4e2a\u503c\u662f\u5426\u76f8\u7b49\u3002\u5982\u679c\u4e24\u6570\u76f8\u7b49\uff0c\u5219\u7b49\u6bd4\u8f83\u8fd0\u7b97\u7b26 ( == ) \u8fd4\u56de true \uff0c\u5426\u5219\u8fd4\u56de false \u3002\u56e0\u4e3a x \u7684\u503c\u4e3a0\uff0c\u800c\u4e14 0 == 0 \u4e3a\u771f\uff0c\u6240\u4ee5\u8868\u8fbe\u5f0f\u6c42\u503c\u7684\u7ed3\u679c\u4e3a true \u3002 \u56e0\u4e3a\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u6c42\u503c\u7ed3\u679c\u4e3a true \uff0c\u5219\u63a5\u4e0b\u6765\u7684\u8bed\u53e5\u4f1a\u88ab\u6267\u884c\uff0c\u6253\u5370 The value is zero \u3002 \u518d\u6b21\u8fd0\u884c\u7a0b\u5e8f\uff1a 1 Enter an integer: 5 \u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c x == 0 \u6c42\u503c\u7ed3\u679c\u4e3a false \u3002\u56e0\u6b64\u63a5\u4e0b\u6765\u7684\u8bed\u53e5\u4f1a\u88ab\u8df3\u8fc7\uff0c\u7136\u540e\u7a0b\u5e8f\u5c31\u7ed3\u675f\u4e86\uff0c\u4e0d\u4f1a\u6253\u5370\u4efb\u4f55\u6587\u672c\u3002 \u6ce8\u610f If \u8bed\u53e5\u53ea\u4f1a\u6709\u6761\u4ef6\u5730\u6267\u884c\u4e00\u6761\u8bed\u53e5\u3002\u6211\u4eec\u4f1a\u5728 7.2 - if \u8bed\u53e5\u548c\u8bed\u53e5\u5757 \u4e2d\u4ecb\u7ecd\u5982\u4f55\u5728\u6761\u4ef6\u4e3a\u771f\u65f6\u6267\u884c\u591a\u6761\u8bed\u53e5\u3002 If-else \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u544a\u8bc9\u7528\u6237\uff0c\u4ed6\u4eec\u8f93\u5165\u7684\u503c\u662f\u5426\u4e3a\u975e0\u5462\uff1f \u7a0b\u5e8f\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u5199\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; if ( x == 0 ) std :: cout << \"The value is zero \\n \" ; if ( x != 0 ) std :: cout << \"The value is non-zero \\n \" ; return 0 ; } \u6216\u8005\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; bool zero { ( x == 0 ) }; if ( zero ) std :: cout << \"The value is zero \\n \" ; if ( ! zero ) std :: cout << \"The value is non-zero \\n \" ; return 0 ; } \u8fd9\u4e24\u4e2a\u7a0b\u5e8f\u5176\u5b9e\u6ca1\u5fc5\u8981\u8fd9\u4e48\u590d\u6742\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u53e6\u4e00\u79cd\u5f62\u5f0f\u7684 if \u8bed\u53e5\u2014\u2014 if-else \u8bed\u53e5\u3002 if-else \u8bed\u53e5\u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 2 3 4 if (condition) true_statement; else false_statement; \u5982\u679c condition \u6c42\u503c\u7ed3\u679c\u4e3a true \uff0c\u5219 true_statement \u4f1a\u6267\u884c\uff0c\u5426\u5219 false_statement \u5c06\u4f1a\u6267\u884c\u3002 \u8ba9\u6211\u4eec\u4f7f\u7528 if-else \u6765\u4fee\u6539\u4e00\u4e0b\u4e0a\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; if ( x == 0 ) std :: cout << \"The value is zero \\n \" ; else std :: cout << \"The value is non-zero \\n \" ; return 0 ; } \u73b0\u5728\uff0c\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Enter an integer: 0 The value is zero 1 2 Enter an integer: 5 The value is non-zero \u94fe\u5f0f if \u8bed\u53e5 \u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u8fde\u7eed\u68c0\u67e5\u4e00\u4e9b\u6761\u4ef6\u662f\u5426\u4e3a\u771f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u94fe\u5f0f\u7684 if \u8bed\u53e5\u6765\u5b8c\u6210\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; if ( x > 0 ) std :: cout << \"The value is positive \\n \" ; else if ( x < 0 ) std :: cout << \"The value is negative \\n \" ; else std :: cout << \"The value is zero \\n \" ; return 0 ; } \u8fd9\u91cc\u7684\u5c0f\u4e8e\u53f7( < ) \u88ab\u7528\u6765\u6d4b\u8bd5\u4e00\u4e2a\u503c\u662f\u5426\u5c0f\u4e8e\u53e6\u4e00\u4e2a\u503c\u3002\u7c7b\u4f3c\u5730\uff0c\u5927\u4e8e\u53f7 ( > )\u5219\u7528\u6765\u6d4b\u8bd5\u4e00\u4e2a\u503c\u662f\u5426\u5927\u4e8e\u53e6\u4e00\u4e2a\u3002\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u8fd4\u56de\u7684\u90fd\u662f\u5e03\u5c14\u503c\u3002 \u591a\u6b21\u8fd0\u884c\u7a0b\u5e8f\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Enter an integer: 4 The value is positive 1 2 Enter an integer: -3 The value is negative 1 2 Enter an integer: 0 The value is zero \u6ce8\u610f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4efb\u610f\u6b21\u94fe\u5f0f_if_ \u8bed\u53e5\u5b8c\u6210\u4f60\u6240\u9700\u8981\u8fdb\u884c\u7684\u6761\u4ef6\u5224\u65ad\u3002 \u5e03\u5c14\u7c7b\u578b\u8fd4\u56de\u503c\u548c if \u8bed\u53e5 \u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff08 4.9 - \u5e03\u5c14\u503c \uff09\uff0c\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a\u8fd4\u56de\u5e03\u5c14\u503c\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // \u5982\u679c x \u548c y \u76f8\u7b49\u5219\u8fd4\u56de true \u5426\u5219\u8fd4\u56de false bool isEqual ( int x , int y ) { return ( x == y ); // \u5982\u679c x \u548c y \u76f8\u7b49\uff0coperator== \u5219\u8fd4\u56de true \u5426\u5219\u8fd4\u56de false } int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another integer: \" ; int y {}; std :: cin >> y ; std :: cout << std :: boolalpha ; // print bools as true or false std :: cout << x << \" and \" << y << \" are equal? \" ; std :: cout << isEqual ( x , y ); // will return true or false return 0 ; } \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 if \u8bed\u53e5\u5bf9\u4e0a\u9762\u7684\u4ee3\u7801\u8fdb\u884c\u6539\u5199\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // returns true if x and y are equal, false otherwise bool isEqual ( int x , int y ) { return ( x == y ); // operator== returns true if x equals y, and false otherwise } int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another integer: \" ; int y {}; std :: cin >> y ; if ( isEqual ( x , y )) std :: cout << x << \" and \" << y << \" are equal \\n \" ; else std :: cout << x << \" and \" << y << \" are not equal \\n \" ; return 0 ; } \u4e24\u6b21\u8fd0\u884c\u4e0a\u8ff0\u7a0b\u5e8f\uff1a 1 2 3 Enter an integer: 5 Enter another integer: 5 5 and 5 are equal 1 2 3 Enter an integer: 6 Enter another integer: 4 6 and 4 are not equal \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u5c31\u662f isEqual \u51fd\u6570\uff0c\u5b83\u7684\u8fd4\u56de\u503c\u662f\u5e03\u5c14\u7c7b\u578b\u7684\u3002 \u975e\u5e03\u5c14\u7c7b\u578b\u6761\u4ef6 \u5728\u4e0a\u8ff0\u6240\u6709\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u7684\u6761\u4ef6\u8981\u4e48\u662f\u5e03\u5c14\u503c\uff08true \u6216 false\uff09\uff0c\u8981\u4e48\u662f\u8fd4\u56de\u5e03\u5c14\u503c\u7684\u51fd\u6570\u3002\u90a3\u5982\u679c\u6211\u4eec\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u6c42\u503c\u7684\u7ed3\u679c\u4e0d\u662f\u5e03\u5c14\u7c7b\u578b\u5462\uff1f \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6761\u4ef6\u8868\u8fbe\u5f0f\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff1a\u975e0\u503c\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c true \uff0c0\u503c\u5219\u88ab\u8f6c\u6362\u4e3a false \u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { if ( 4 ) // \u6ca1\u6709\u5b9e\u9645\u610f\u4e49\uff0c\u4ec5\u4f9b\u4e3e\u4f8b\u4f7f\u7528 std :: cout << \"hi\" ; else std :: cout << \"bye\" ; return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u4f1a\u6253\u5370 \u201chi\u201d\uff0c\u56e0\u4e3a4\u662f\u4e00\u4e2a\u975e0\u503c\uff0c\u6240\u4ee5\u88ab\u8f6c\u6362\u6210\u4e86\u5e03\u5c14\u503c true \uff0c\u4f7f\u5f97\u5bf9\u5e94\u7684\u8bed\u53e5\u80fd\u591f\u88ab\u6267\u884c\u3002 \u6211\u4eec\u4f1a\u5728 7.2 - if \u8bed\u53e5\u548c\u8bed\u53e5\u5757 \u4e2d\u66f4\u8be6\u7ec6\u5730\u4ecb\u7ecd if \u8bed\u53e5\u3002","title":"4.10 - if \u8bed\u53e5\u7b80\u4ecb"},{"location":"4-10-Introduction-to-if-statements/#if-else","text":"\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u544a\u8bc9\u7528\u6237\uff0c\u4ed6\u4eec\u8f93\u5165\u7684\u503c\u662f\u5426\u4e3a\u975e0\u5462\uff1f \u7a0b\u5e8f\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u5199\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; if ( x == 0 ) std :: cout << \"The value is zero \\n \" ; if ( x != 0 ) std :: cout << \"The value is non-zero \\n \" ; return 0 ; } \u6216\u8005\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; bool zero { ( x == 0 ) }; if ( zero ) std :: cout << \"The value is zero \\n \" ; if ( ! zero ) std :: cout << \"The value is non-zero \\n \" ; return 0 ; } \u8fd9\u4e24\u4e2a\u7a0b\u5e8f\u5176\u5b9e\u6ca1\u5fc5\u8981\u8fd9\u4e48\u590d\u6742\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u53e6\u4e00\u79cd\u5f62\u5f0f\u7684 if \u8bed\u53e5\u2014\u2014 if-else \u8bed\u53e5\u3002 if-else \u8bed\u53e5\u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 2 3 4 if (condition) true_statement; else false_statement; \u5982\u679c condition \u6c42\u503c\u7ed3\u679c\u4e3a true \uff0c\u5219 true_statement \u4f1a\u6267\u884c\uff0c\u5426\u5219 false_statement \u5c06\u4f1a\u6267\u884c\u3002 \u8ba9\u6211\u4eec\u4f7f\u7528 if-else \u6765\u4fee\u6539\u4e00\u4e0b\u4e0a\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; if ( x == 0 ) std :: cout << \"The value is zero \\n \" ; else std :: cout << \"The value is non-zero \\n \" ; return 0 ; } \u73b0\u5728\uff0c\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Enter an integer: 0 The value is zero 1 2 Enter an integer: 5 The value is non-zero","title":"If-else"},{"location":"4-10-Introduction-to-if-statements/#if","text":"\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u8fde\u7eed\u68c0\u67e5\u4e00\u4e9b\u6761\u4ef6\u662f\u5426\u4e3a\u771f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u94fe\u5f0f\u7684 if \u8bed\u53e5\u6765\u5b8c\u6210\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; if ( x > 0 ) std :: cout << \"The value is positive \\n \" ; else if ( x < 0 ) std :: cout << \"The value is negative \\n \" ; else std :: cout << \"The value is zero \\n \" ; return 0 ; } \u8fd9\u91cc\u7684\u5c0f\u4e8e\u53f7( < ) \u88ab\u7528\u6765\u6d4b\u8bd5\u4e00\u4e2a\u503c\u662f\u5426\u5c0f\u4e8e\u53e6\u4e00\u4e2a\u503c\u3002\u7c7b\u4f3c\u5730\uff0c\u5927\u4e8e\u53f7 ( > )\u5219\u7528\u6765\u6d4b\u8bd5\u4e00\u4e2a\u503c\u662f\u5426\u5927\u4e8e\u53e6\u4e00\u4e2a\u3002\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u8fd4\u56de\u7684\u90fd\u662f\u5e03\u5c14\u503c\u3002 \u591a\u6b21\u8fd0\u884c\u7a0b\u5e8f\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Enter an integer: 4 The value is positive 1 2 Enter an integer: -3 The value is negative 1 2 Enter an integer: 0 The value is zero \u6ce8\u610f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4efb\u610f\u6b21\u94fe\u5f0f_if_ \u8bed\u53e5\u5b8c\u6210\u4f60\u6240\u9700\u8981\u8fdb\u884c\u7684\u6761\u4ef6\u5224\u65ad\u3002","title":"\u94fe\u5f0f if \u8bed\u53e5"},{"location":"4-10-Introduction-to-if-statements/#if_1","text":"\u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff08 4.9 - \u5e03\u5c14\u503c \uff09\uff0c\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a\u8fd4\u56de\u5e03\u5c14\u503c\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // \u5982\u679c x \u548c y \u76f8\u7b49\u5219\u8fd4\u56de true \u5426\u5219\u8fd4\u56de false bool isEqual ( int x , int y ) { return ( x == y ); // \u5982\u679c x \u548c y \u76f8\u7b49\uff0coperator== \u5219\u8fd4\u56de true \u5426\u5219\u8fd4\u56de false } int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another integer: \" ; int y {}; std :: cin >> y ; std :: cout << std :: boolalpha ; // print bools as true or false std :: cout << x << \" and \" << y << \" are equal? \" ; std :: cout << isEqual ( x , y ); // will return true or false return 0 ; } \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 if \u8bed\u53e5\u5bf9\u4e0a\u9762\u7684\u4ee3\u7801\u8fdb\u884c\u6539\u5199\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // returns true if x and y are equal, false otherwise bool isEqual ( int x , int y ) { return ( x == y ); // operator== returns true if x equals y, and false otherwise } int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another integer: \" ; int y {}; std :: cin >> y ; if ( isEqual ( x , y )) std :: cout << x << \" and \" << y << \" are equal \\n \" ; else std :: cout << x << \" and \" << y << \" are not equal \\n \" ; return 0 ; } \u4e24\u6b21\u8fd0\u884c\u4e0a\u8ff0\u7a0b\u5e8f\uff1a 1 2 3 Enter an integer: 5 Enter another integer: 5 5 and 5 are equal 1 2 3 Enter an integer: 6 Enter another integer: 4 6 and 4 are not equal \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u5c31\u662f isEqual \u51fd\u6570\uff0c\u5b83\u7684\u8fd4\u56de\u503c\u662f\u5e03\u5c14\u7c7b\u578b\u7684\u3002","title":"\u5e03\u5c14\u7c7b\u578b\u8fd4\u56de\u503c\u548c if \u8bed\u53e5"},{"location":"4-10-Introduction-to-if-statements/#_1","text":"\u5728\u4e0a\u8ff0\u6240\u6709\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u7684\u6761\u4ef6\u8981\u4e48\u662f\u5e03\u5c14\u503c\uff08true \u6216 false\uff09\uff0c\u8981\u4e48\u662f\u8fd4\u56de\u5e03\u5c14\u503c\u7684\u51fd\u6570\u3002\u90a3\u5982\u679c\u6211\u4eec\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u6c42\u503c\u7684\u7ed3\u679c\u4e0d\u662f\u5e03\u5c14\u7c7b\u578b\u5462\uff1f \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6761\u4ef6\u8868\u8fbe\u5f0f\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff1a\u975e0\u503c\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c true \uff0c0\u503c\u5219\u88ab\u8f6c\u6362\u4e3a false \u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { if ( 4 ) // \u6ca1\u6709\u5b9e\u9645\u610f\u4e49\uff0c\u4ec5\u4f9b\u4e3e\u4f8b\u4f7f\u7528 std :: cout << \"hi\" ; else std :: cout << \"bye\" ; return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u4f1a\u6253\u5370 \u201chi\u201d\uff0c\u56e0\u4e3a4\u662f\u4e00\u4e2a\u975e0\u503c\uff0c\u6240\u4ee5\u88ab\u8f6c\u6362\u6210\u4e86\u5e03\u5c14\u503c true \uff0c\u4f7f\u5f97\u5bf9\u5e94\u7684\u8bed\u53e5\u80fd\u591f\u88ab\u6267\u884c\u3002 \u6211\u4eec\u4f1a\u5728 7.2 - if \u8bed\u53e5\u548c\u8bed\u53e5\u5757 \u4e2d\u66f4\u8be6\u7ec6\u5730\u4ecb\u7ecd if \u8bed\u53e5\u3002","title":"\u975e\u5e03\u5c14\u7c7b\u578b\u6761\u4ef6"},{"location":"4-11-Chars/","tags":["char","C++11"],"text":"Key Takeaway \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u4ecb\u7ecd\u7684\u57fa\u7840\u6570\u636e\u7c7b\u578b\u90fd\u662f\u7528\u6765\u4fdd\u5b58\u6570\u5b57\u7684\uff08\u6574\u578b\u6216\u8005\u6d6e\u70b9\u6570\uff09\u6216\u8005\u5e03\u5c14\u503c\u7684\u3002\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u60f3\u5b58\u653e\u5b57\u6bcd\u548c\u6807\u70b9\u600e\u4e48\u529e\uff1f 1 2 3 4 5 6 7 8 9 10 11 #include int main () { std :: cout << \"Would you like a burrito? (y/n)\" ; // We want the user to enter a 'y' or 'n' character // How do we do this? return 0 ; } char \u7c7b\u578b\u662f\u7528\u6765\u4fdd\u5b58 \u5b57\u7b26(character) \u3002\u5b57\u7b26\u53ef\u4ee5\u662f\u4e00\u4e2a\u5b57\u6bcd\u3001\u6570\u5b57\u3001\u7b26\u53f7\u6216\u7a7a\u767d\u3002 char\u7c7b\u578b\u4e5f\u5c5e\u4e8e\u6574\u578b\u7c7b\u578b\uff0c\u4e5f\u5c31\u662f\u8bf4\u5b83\u5e95\u5c42\u5b58\u50a8\u7684\u8fd8\u662f\u6574\u578b\u6570\u3002\u548c\u5e03\u5c14\u503c\u5c060\u89e3\u6790\u4e3afalse\u7c7b\u4f3c\uff0c\u5c06\u975e\u96f6\u503c\u89e3\u6790\u4e3atrue\u7c7b\u4f3c\uff0cchar \u7c7b\u578b\u5c06\u5b58\u50a8\u7684\u6574\u578b\u89e3\u6790\u4e3a ASCII \u5b57\u7b26\u3002 ASCII \u5373\u7f8e\u56fd\u4fe1\u606f\u4ea4\u6362\u6807\u51c6\u4ee3\u7801\u662f\uff0c\u5b83\u662f\u4e00\u79cd\u5c06\u82f1\u6587\u5b57\u6bcd\uff08\u4ee5\u53ca\u5c11\u6570\u5176\u4ed6\u7b26\u53f7\uff09\u8868\u793a\u4e3a0\u5230127\u6574\u578b\u6570\u5b57\u7684\u65b9\u6cd5\u3002\u4f8b\u5982ASCII\u4e2d97\u8868\u793a\u5b57\u6bcd a \u3002 \u5b57\u7b26\u5e38\u91cf\u901a\u5e38\u653e\u7f6e\u5728\u5355\u5f15\u53f7\u4e2d\uff08\u4f8b\u5982, 'g' , '1' , ' ' \uff09\u3002 \u5b8c\u6574\u7684 ASCII \u5b57\u6bcd\u8868\u5982\u4e0b\uff1a ASCII \u7801 \u7b26\u53f7 ASCII \u7801 \u7b26\u53f7 ASCII \u7801 \u7b26\u53f7 ASCII \u7801 \u7b26\u53f7 0 NUL (null) 32 (space) 64 @ 96 ` 1 SOH (start of header) 33 ! 65 A 97 a 2 STX (start of text) 34 \u201d 66 B 98 b 3 ETX (end of text) 35 # 67 C 99 c 4 EOT (end of transmission) 36 $ 68 D 100 d 5 ENQ (enquiry) 37 % 69 E 101 e 6 ACK (acknowledge) 38 & 70 F 102 f 7 BEL (bell) 39 \u2019 71 G 103 g 8 BS (backspace) 40 ( 72 H 104 h 9 HT (horizontal tab) 41 ) 73 I 105 i 10 LF (line feed/new line) 42 * 74 J 106 j 11 VT (vertical tab) 43 + 75 K 107 k 12 FF (form feed / new page) 44 , 76 L 108 l 13 CR (carriage return) 45 - 77 M 109 m 14 SO (shift out) 46 . 78 N 110 n 15 SI (shift in) 47 / 79 O 111 o 16 DLE (data link escape) 48 0 80 P 112 p 17 DC1 (data control 1) 49 1 81 Q 113 q 18 DC2 (data control 2) 50 2 82 R 114 r 19 DC3 (data control 3) 51 3 83 S 115 s 20 DC4 (data control 4) 52 4 84 T 116 t 21 NAK (negative acknowledge) 53 5 85 U 117 u 22 SYN (synchronous idle) 54 6 86 V 118 v 23 ETB (end of transmission block) 55 7 87 W 119 w 24 CAN (cancel) 56 8 88 X 120 x 25 EM (end of medium) 57 9 89 Y 121 y 26 SUB (substitute) 58 : 90 Z 122 z 27 ESC (escape) 59 ; 91 [ 123 { 28 FS (file separator) 60 < 92 \\ 124 29 GS (group separator) 61 = 93 ] 125 } 30 RS (record separator) 62 > 94 ^ 126 ~ 31 US (unit separator) 63 ? 95 _ 127 DEL (\u5220\u9664) ASCII \u7801 0-31 \u4e3a\u4e0d\u53ef\u6253\u5370\u5b57\u7b26\uff0c\u5b83\u4eec\u5927\u591a\u88ab\u7528\u6765\u6392\u7248\u6216\u63a7\u5236\u6253\u5370\u673a\uff0c\u5176\u4e2d\u5f88\u591a\u73b0\u5728\u5df2\u7ecf\u4e0d\u518d\u4f7f\u7528\u4e86\u3002\u5982\u679c\u4f60\u5c1d\u8bd5\u5c06\u5b83\u4eec\u6253\u5370\u51fa\u6765\u7684\u8bdd\uff0c\u6253\u5370\u7684\u7ed3\u679c\u53ef\u80fd\u548c\u4f60\u4f7f\u7528\u7684\u7cfb\u7edf\u76f8\u540c\uff08\u4f60\u53ef\u80fd\u4f1a\u5f97\u5230\u4e00\u4e9b\u50cfemoji\u7684\u5b57\u7b26\uff09\u3002 ASCII \u7801 32-127 \u4e3a\u53ef\u6253\u5370\u5b57\u7b26\uff0c\u5b83\u4eec\u901a\u5e38\u8868\u793a\u5b57\u6bcd\u3001\u6570\u5b57\u5b57\u7b26\u548c\u6807\u70b9\uff0c\u591a\u88ab\u7528\u6765\u8868\u793a\u57fa\u672c\u7684\u82f1\u6587\u6587\u672c\u3002 \u521d\u59cb\u5316\u5b57\u7b26 \u4f7f\u7528\u5b57\u7b26\u5b57\u9762\u91cf\u53ef\u4ee5\u521d\u59cb\u5316\u5b57\u7b26\u53d8\u91cf\uff1a 1 char ch2 { 'a' }; // \u4f7f\u7528 'a' \u6765\u521d\u59cb\u5316(\u88ab\u5b58\u50a8\u4e3a 97) (\u63a8\u8350) \u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6574\u578b\u6570\u6765\u521d\u59cb\u5316\u5b57\u7b26\u53d8\u91cf\uff0c\u4f46\u662f\u5e94\u8be5\u5c3d\u91cf\u907f\u514d\u8fd9\u4e48\u505a\u3002 1 char ch1 { 97 }; // \u4f7f\u7528 97 \u6765\u521d\u59cb\u5316('a') (\u4e0d\u63a8\u8350) \u6ce8\u610f \u6ce8\u610f\u4e0d\u8981\u5c06\u5b57\u7b26\u6570\u5b57\u548c\u6574\u578b\u6570\u5b57\u641e\u6df7\uff0c\u4e0b\u9762\u4e24\u79cd\u521d\u59cb\u5316\u5e76\u4e0d\u76f8\u540c\uff1a 1 2 char ch { 5 }; // \u4f7f\u7528 5 \u521d\u59cb\u5316(\u4fdd\u5b58\u4e3a\u6574\u578b 5) char ch { '5' }; // \u4f7f\u7528 '5' \u521d\u59cb\u5316 (\u4fdd\u5b58\u4e3a\u6574\u578b 53) \u53ea\u6709\u5f53\u9700\u8981\u5c06\u6570\u5b57\u663e\u793a\u6210\u6587\u672c\u7684\u65f6\u5019\uff0c\u624d\u4f7f\u7528\u5b57\u7b26\u6570\u5b57\u800c\u4e0d\u662f\u6574\u578b\u6570\u5b57\u3002\u6574\u578b\u6570\u5b57\u7528\u4e8e\u6570\u5b66\u8fd0\u7b97\u3002 \u6253\u5370\u5b57\u7b26 \u5f53\u4f7f\u7528 std::cout \u6253\u5370\u5b57\u7b26\u7684\u65f6\u5019 std::cout \u4f1a\u628a\u5b57\u7b26\u53d8\u91cf\u6253\u5370\u6210ASCII\u5b57\u7b26\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { char ch1 { 'a' }; // (preferred) std :: cout << ch1 ; // cout prints character 'a' char ch2 { 98 }; // code point for 'b' (not preferred) std :: cout << ch2 ; // cout prints a character ('b') return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1aab \u5b57\u7b26\u5b57\u9762\u91cf\u4e5f\u53ef\u4ee5\u76f4\u63a5\u6253\u5370\uff1a 1 cout << 'c' ; \u8f93\u51fa\u7ed3\u679c\uff1ac \u8f93\u5165\u5b57\u7b26 \u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\u4f1a\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\uff0c\u7136\u540e\u5c06\u5b57\u7b26\u6253\u5370\u51fa\u6765\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { std :: cout << \"Input a keyboard character: \" ; char ch {}; std :: cin >> ch ; std :: cout << \"You entered: \" << ch << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 2 Input a keyboard character: q You entered q \u6ce8\u610f\uff0c std::cin \u5141\u8bb8\u4f60\u8f93\u5165\u591a\u4e2a\u5b57\u7b26\u3002\u4e0d\u8fc7\u53d8\u91cf ch \u53ea\u80fd\u63a5\u6536\u4e00\u4e2a\u5b57\u7b26\uff0c\u56e0\u6b64\u53ea\u6709\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4f1a\u88ab\u5b58\u653e\u5230\u53d8\u91cf\u4e2d\uff0c\u5269\u4e0b\u5b57\u7b26\u4f1a\u88ab\u7559\u5728 std::cin \u7684\u7f13\u51b2\u4e2d\uff0c\u53ef\u4ee5\u88ab\u63a5\u4e0b\u6765\u518d\u6b21\u8c03\u7528 std::cin \u65f6\u4f7f\u7528\u3002 \u4ee5\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e3a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { std :: cout << \"Input a keyboard character: \" ; // \u5047\u8bbe\u7528\u6237\u8f93\u5165\u4e86 \"abcd\" (\u5e26\u53cc\u5f15\u53f7) char ch {}; std :: cin >> ch ; // ch = 'a', \"bcd\" \u88ab\u7559\u5728\u4e86\u7f13\u51b2\u4e2d std :: cout << \"You entered: \" << ch << '\\n' ; // \u6ce8\u610f\uff0c\u4e0b\u9762\u7684ch\u6ca1\u6709\u8981\u6c42\u7528\u6237\u518d\u6b21\u8f93\u5165\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u4ece\u7f13\u51b2\u4e2d\u83b7\u53d6\u8f93\u5165 std :: cin >> ch ; // ch = 'b', \"cd\" \u88ab\u6d41\u5728\u7f13\u51b2\u4e2d std :: cout << \"You entered: \" << ch << '\\n' ; return 0 ; } 1 2 3 Input a keyboard character: abcd You entered: a You entered: b \u5982\u679c\u4f60\u60f3\u4e00\u6b21\u6027\u8bfb\u5165\u591a\u4e2a\u5b57\u7b26\uff08\u4f8b\u5982\uff0c\u540d\u5b57\u3001\u5355\u8bcd\u6216\u8005\u53e5\u5b50\uff09\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528\u5b57\u7b26\u4e32\u800c\u4e0d\u662f\u5b57\u7b26\u3002\u5b57\u7b26\u4e32\u662f\u4e00\u4e2a\u5b57\u7b26\u5e8f\u5217\uff08\u56e0\u6b64\u5b57\u7b26\u4e32\u53ef\u4ee5\u4fdd\u5b58\u591a\u4e2a\u7b26\u53f7\uff09\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 4.17 - std::string \u7b80\u4ecb \u4e2d\u8ba8\u8bba\u5173\u4e8e\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u3002 \u5b57\u7b26\u7c7b\u578b\u7684\u5927\u5c0f\u3001\u8303\u56f4\u548c\u9ed8\u8ba4\u7b26\u53f7 C++ \u5c06\u5b57\u7b26\u7c7b\u578b\u7684\u5927\u5c0f\u5b9a\u4e49\u4e3a\u56fa\u5b9a\u76841\u5b57\u8282\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5b57\u7b26\u53ef\u4ee5\u662f\u6709\u7b26\u53f7\u6216\u8005\u65e0\u7b26\u53f7\u7684\uff08\u591a\u6570\u60c5\u51b5\u4e3a\u6709\u7b26\u53f7\uff09\u3002\u5982\u679c\u4f60\u4f7f\u7528\u5b57\u7b26\u53d8\u91cf\u6765\u4fdd\u5b58ASCII\u5b57\u7b26\u7684\u8bdd\uff0c\u4f60\u4e0d\u9700\u8981\u6307\u5b9a\u7b26\u53f7\uff0c\u56e0\u4e3a\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u7684\u5b57\u7b26\u53d8\u91cf\u90fd\u53ef\u4ee5\u4fdd\u5b580-127\u5185\u7684\u503c\u3002 \u5982\u679c\u4f60\u4f7f\u7528\u5b57\u7b26\u53d8\u91cf\u6765\u4fdd\u5b58\u4e00\u4e9b\u6bd4\u8f83\u5c0f\u7684\u6574\u578b\u503c\uff08\u5982\u975e\u4f18\u5316\u7a7a\u95f4\u8fd9\u6837\u7684\u7279\u522b\u9700\u8981\uff0c\u8bf7\u4e0d\u8981\u8fd9\u4e48\u505a\uff09\uff0c\u4f60\u5e94\u8be5\u6307\u5b9a\u5b83\u662f\u6709\u7b26\u53f7\u8fd8\u662f\u65e0\u7b26\u53f7\u7684\u3002\u6709\u7b26\u53f7\u7684\u5b57\u7b26\u53ef\u4ee5\u4fdd\u5b58\u7684\u6574\u6570\u8303\u56f4\u4e3a-128 \u5230 127\u3002\u65e0\u7b26\u53f7\u5b57\u7b26\u5219\u53ef\u4ee5\u4fdd\u5b58 0 \u5230 255 \u8303\u56f4\u5185\u7684\u6574\u6570\u3002 \u8f6c\u8bd1\u5e8f\u5217 C++ \u4e2d\u7684\u67d0\u4e9b\u5b57\u7b26\u662f\u5177\u6709\u7279\u6b8a\u542b\u4e49\u7684\uff0c\u8fd9\u4e9b\u5b57\u7b26\u79f0\u4e3a\u8f6c\u8bd1\u5e8f\u5217\uff08escape sequences\uff09\u3002\u8f6c\u8bd1\u5e8f\u5217\u4ee5 \\ \uff08\u53cd\u659c\u7ebf\uff09\u5f00\u5934\uff0c\u7d27\u63a5\u7740\u662f\u4e00\u4e2a\u5b57\u6bcd\u6216\u6570\u5b57\u3002 \u4f60\u53ef\u80fd\u5df2\u7ecf\u89c1\u8fc7\u5728\u5e38\u9047\u5230\u7684\u8f6c\u4e49\u5e8f\u5217 '\\n' \uff0c\u5b83\u4f1a\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u4e00\u4e2a\u6362\u884c\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << \"First line \\n Second line \\n \" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 First line Second line \u53e6\u5916\u4e00\u4e2a\u5e38\u7528\u7684\u8f6c\u8bd1\u5e8f\u5217\u662f '\\t' \uff0c\u8868\u793a\u4e00\u4e2a\u6c34\u5e73\u5236\u8868\u7b26\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << \"First part \\t Second part\" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u4e3a 1 First part Second part \u5176\u4ed6\u4e00\u4e9b\u503c\u5f97\u6ce8\u610f\u7684\u8f6c\u8bd1\u5e8f\u5217\u6709\uff1a \\' \uff1a\u6253\u5370\u5355\u5f15\u53f7 \\\" \uff1a\u6253\u5370\u53cc\u5f15\u53f7 \\\\ \u6253\u5370\u53cd\u659c\u7ebf \u4e0b\u8868\u5c55\u793a\u4e86\u5168\u90e8\u7684\u8f6c\u4e49\u5e8f\u5217 \u540d\u79f0 \u7b26\u53f7 \u542b\u4e49 Alert \\a Makes an alert, such as a beep Backspace \\b Moves the cursor back one space Formfeed \\f Moves the cursor to next logical page Newline \\n Moves cursor to next line Carriage return \\r Moves cursor to beginning of line Horizontal tab \\t Prints a horizontal tab Vertical tab \\v Prints a vertical tab Single quote \\\u2019 Prints a single quote Double quote \\\u201d Prints a double quote Backslash \\ Prints a backslash. Question mark \\? Prints a question mark. No longer relevant. You can use question marks unescaped. Octal number (number) Translates into char represented by octal Hex number \\x(number) Translates into char represented by hex number \u4e3e\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { std :: cout << \" \\\" This is quoted text \\\"\\n \" ; std :: cout << \"This string contains a single backslash \\\\\\n \" ; std :: cout << \"6F in hex is char ' \\x6F ' \\n \" ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 \"This is quoted text\" This string contains a single backslash \\ 6F in hex is char 'o' Newline ( \\n ) vs. std::endl \u5728 1.5 - iostream \u7b80\u4ecb\uff1a cout\uff0ccin \u548c endl \u4e2d\u5df2\u7ecf\u4ecb\u7ecd\u4e86\u3002 \u5b57\u7b26\u653e\u5728\u5355\u5f15\u53f7\u548c\u53cc\u5f15\u53f7\u4e2d\u6709\u4ec0\u4e48\u533a\u522b \u5355\u5b57\u7b26\u603b\u662f\u653e\u5728\u5355\u5f15\u53f7\u4e2d (\u4f8b\u5982 \u2018a\u2019, \u2018+\u2019, \u20185\u2019)\u3002\u5b57\u7b26\u53ea\u80fd\u8868\u793a\u4e00\u4e2a\u7b26\u53f7\uff08\u4f8b\u5982\uff0c\u5b57\u6bcd\u3001\u52a0\u53f7\u548c\u6570\u5b575\uff09\uff0c\u4e0b\u9762\u8fd9\u6837\u7684\u5199\u6cd5\u662f\u4e0d\u5bf9\u7684\uff1a 1 char ch { ' 56 ' }; // char \u53ea\u80fd\u4fdd\u6301\u4e00\u4e2a\u7b26\u53f7 \u53cc\u5f15\u53f7\u4e2d\u7684\u6587\u672c\u4f1a\u88ab\u770b\u505a\u662f\u5b57\u7b26\u4e32\uff08\u4f8b\u5982 \u201cHello, world!\u201d\uff09\u3002 \u4f60\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff1a 1 std :: cout << \"Hello, world!\" ; // \"Hello, world!\" is a string literal \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u53ea\u6709\u4e00\u4e2a\u5b57\u7b26\uff0c\u8bf7\u653e\u5728\u5355\u5f15\u53f7\u4e2d (\u4f8b\u5982 \u2018t\u2019 \u6216 \u2018\\n\u2019, \u800c\u4e0d\u662f \u201ct\u201d \u6216 \u201c\\n\u201d)\u3002\u4ee5\u4fbf\u7f16\u8bd1\u5668\u8fdb\u884c\u4f18\u5316\u3002 \u8fd8\u6709\u5176\u4ed6\u5b57\u7b26\u7c7b\u578b\u5417\uff1f wchar_t, char16_t \u548c char32_t? wchar_t \u5728\u51e0\u4e4e\u4efb\u4f55\u573a\u5408\u4e0b\u90fd\u5e94\u8be5\u907f\u514d\u4f7f\u7528 (\u5904\u7406\u5728\u4f7f\u7528 Windows API \u65f6)\u3002\u5b83\u7684\u5927\u5c0f\u662f\u5b9e\u73b0\u65f6\u5b9a\u4e49\u7684\uff0c\u975e\u5e38\u4e0d\u53ef\u9760\u800c\u4e14\u51e0\u4e4e\u5df2\u7ecf\u5e9f\u5f03\u4e86\u3002 \u9898\u5916\u8bdd \u672f\u8bed\u201c\u5e9f\u5f03\u201d\u6307\u7684\u662f\uff0c\u4ecd\u7136\u652f\u6301\uff0c\u4f46\u662f\u4e0d\u518d\u63a8\u8350\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u5df2\u7ecf\u88ab\u5176\u4ed6\u66f4\u597d\u7684\u540c\u7c7b\u66ff\u6362\u4e86\uff0c\u540c\u65f6\u5b83\u4e5f\u662f\u4e0d\u5b89\u5168\u7684\u3002 \u548c ASCII \u5c06\u6574\u578b 0-127 \u6620\u5c04\u5230\u82f1\u6587\u5b57\u6bcd\u4e00\u6837\uff0c\u4e5f\u6709\u5176\u4ed6\u5b57\u7b26\u7f16\u7801\u6807\u51c6\u53ef\u4ee5\u5c06\u6574\u578b\u6620\u5c04\u5230\u5176\u4ed6\u8bed\u8a00\u7684\u5b57\u7b26\u3002\u9664\u4e86 ASCII \u4ee5\u5916\u6700\u8457\u540d\u7684\u5b57\u6bcd\u7f16\u7801\u662f Unicode\uff0c\u5b83\u5c06 144,000 \u4e2a\u6574\u578b\u6570\u6620\u5c04\u5230\u4e0d\u540c\u8bed\u8a00\u7684\u5b57\u7b26\u3002\u56e0\u4e3a Unicode \u7684\u7f16\u7801\u70b9\u975e\u5e38\u591a\uff0c\u6240\u4ee5\u4e00\u4e2a Unicode \u7f16\u7801\u70b9\u9700\u898132\u4f4d\u624d\u80fd\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff08\u79f0\u4e3a UTF-32\uff09\u3002\u4e0d\u8fc7\uff0cUnicode \u5b57\u7b26\u4e5f\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a 16 \u4f4d\u6216 8\u4f4d\u5b57\u7b26 (\u5206\u522b\u79f0\u4e3a UTF-16 \u548c UTF-8)\u3002 char16_t \u548c char32_t \u5728 C++11 \u4e2d\u88ab\u5f15\u5165\uff0c\u7528\u4e8e\u652f\u6301 16 \u4f4d\u548c 32 \u4f4d Unicode \u5b57\u7b26\u3002 char8_t \u5728 C++20 \u4e2d\u88ab\u5f15\u5165\u3002 \u4f60\u4e0d\u9700\u8981\u4f7f\u7528 char8_t \u3001 char16_t \u6216 char32_t \uff0c\u89e6\u53d1\u4f60\u5e0c\u671b\u7a0b\u5e8f\u517c\u5bb9 Unicode\u3002Unicode \u548c\u672c\u5730\u5316\u4e00\u822c\u6765\u8bf4\u5df2\u7ecf\u8d85\u51fa\u4e86\u672c\u6559\u7a0b\u7684\u8303\u56f4\uff0c\u6240\u4ee5\u6211\u4eec\u5e76\u4e0d\u4f1a\u6d89\u53ca\u76f8\u5173\u5185\u5bb9\u3002 \u540c\u65f6\uff0c\u5f53\u4f60\u4f7f\u7528\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u7684\u65f6\u5019\uff0c\u5e94\u8be5\u53ea\u4f7f\u7528ASCII\u5b57\u7b26\u3002\u4f7f\u7528\u5176\u4ed6\u5b57\u7b26\u96c6\u7684\u5b57\u7b26\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5b57\u7b26\u663e\u793a\u9519\u8bef\u3002","title":"4.11 - \u5b57\u7b26"},{"location":"4-11-Chars/#_1","text":"\u4f7f\u7528\u5b57\u7b26\u5b57\u9762\u91cf\u53ef\u4ee5\u521d\u59cb\u5316\u5b57\u7b26\u53d8\u91cf\uff1a 1 char ch2 { 'a' }; // \u4f7f\u7528 'a' \u6765\u521d\u59cb\u5316(\u88ab\u5b58\u50a8\u4e3a 97) (\u63a8\u8350) \u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6574\u578b\u6570\u6765\u521d\u59cb\u5316\u5b57\u7b26\u53d8\u91cf\uff0c\u4f46\u662f\u5e94\u8be5\u5c3d\u91cf\u907f\u514d\u8fd9\u4e48\u505a\u3002 1 char ch1 { 97 }; // \u4f7f\u7528 97 \u6765\u521d\u59cb\u5316('a') (\u4e0d\u63a8\u8350) \u6ce8\u610f \u6ce8\u610f\u4e0d\u8981\u5c06\u5b57\u7b26\u6570\u5b57\u548c\u6574\u578b\u6570\u5b57\u641e\u6df7\uff0c\u4e0b\u9762\u4e24\u79cd\u521d\u59cb\u5316\u5e76\u4e0d\u76f8\u540c\uff1a 1 2 char ch { 5 }; // \u4f7f\u7528 5 \u521d\u59cb\u5316(\u4fdd\u5b58\u4e3a\u6574\u578b 5) char ch { '5' }; // \u4f7f\u7528 '5' \u521d\u59cb\u5316 (\u4fdd\u5b58\u4e3a\u6574\u578b 53) \u53ea\u6709\u5f53\u9700\u8981\u5c06\u6570\u5b57\u663e\u793a\u6210\u6587\u672c\u7684\u65f6\u5019\uff0c\u624d\u4f7f\u7528\u5b57\u7b26\u6570\u5b57\u800c\u4e0d\u662f\u6574\u578b\u6570\u5b57\u3002\u6574\u578b\u6570\u5b57\u7528\u4e8e\u6570\u5b66\u8fd0\u7b97\u3002","title":"\u521d\u59cb\u5316\u5b57\u7b26"},{"location":"4-11-Chars/#_2","text":"\u5f53\u4f7f\u7528 std::cout \u6253\u5370\u5b57\u7b26\u7684\u65f6\u5019 std::cout \u4f1a\u628a\u5b57\u7b26\u53d8\u91cf\u6253\u5370\u6210ASCII\u5b57\u7b26\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { char ch1 { 'a' }; // (preferred) std :: cout << ch1 ; // cout prints character 'a' char ch2 { 98 }; // code point for 'b' (not preferred) std :: cout << ch2 ; // cout prints a character ('b') return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1aab \u5b57\u7b26\u5b57\u9762\u91cf\u4e5f\u53ef\u4ee5\u76f4\u63a5\u6253\u5370\uff1a 1 cout << 'c' ; \u8f93\u51fa\u7ed3\u679c\uff1ac","title":"\u6253\u5370\u5b57\u7b26"},{"location":"4-11-Chars/#_3","text":"\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\u4f1a\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\uff0c\u7136\u540e\u5c06\u5b57\u7b26\u6253\u5370\u51fa\u6765\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { std :: cout << \"Input a keyboard character: \" ; char ch {}; std :: cin >> ch ; std :: cout << \"You entered: \" << ch << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 2 Input a keyboard character: q You entered q \u6ce8\u610f\uff0c std::cin \u5141\u8bb8\u4f60\u8f93\u5165\u591a\u4e2a\u5b57\u7b26\u3002\u4e0d\u8fc7\u53d8\u91cf ch \u53ea\u80fd\u63a5\u6536\u4e00\u4e2a\u5b57\u7b26\uff0c\u56e0\u6b64\u53ea\u6709\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4f1a\u88ab\u5b58\u653e\u5230\u53d8\u91cf\u4e2d\uff0c\u5269\u4e0b\u5b57\u7b26\u4f1a\u88ab\u7559\u5728 std::cin \u7684\u7f13\u51b2\u4e2d\uff0c\u53ef\u4ee5\u88ab\u63a5\u4e0b\u6765\u518d\u6b21\u8c03\u7528 std::cin \u65f6\u4f7f\u7528\u3002 \u4ee5\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e3a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { std :: cout << \"Input a keyboard character: \" ; // \u5047\u8bbe\u7528\u6237\u8f93\u5165\u4e86 \"abcd\" (\u5e26\u53cc\u5f15\u53f7) char ch {}; std :: cin >> ch ; // ch = 'a', \"bcd\" \u88ab\u7559\u5728\u4e86\u7f13\u51b2\u4e2d std :: cout << \"You entered: \" << ch << '\\n' ; // \u6ce8\u610f\uff0c\u4e0b\u9762\u7684ch\u6ca1\u6709\u8981\u6c42\u7528\u6237\u518d\u6b21\u8f93\u5165\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u4ece\u7f13\u51b2\u4e2d\u83b7\u53d6\u8f93\u5165 std :: cin >> ch ; // ch = 'b', \"cd\" \u88ab\u6d41\u5728\u7f13\u51b2\u4e2d std :: cout << \"You entered: \" << ch << '\\n' ; return 0 ; } 1 2 3 Input a keyboard character: abcd You entered: a You entered: b \u5982\u679c\u4f60\u60f3\u4e00\u6b21\u6027\u8bfb\u5165\u591a\u4e2a\u5b57\u7b26\uff08\u4f8b\u5982\uff0c\u540d\u5b57\u3001\u5355\u8bcd\u6216\u8005\u53e5\u5b50\uff09\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528\u5b57\u7b26\u4e32\u800c\u4e0d\u662f\u5b57\u7b26\u3002\u5b57\u7b26\u4e32\u662f\u4e00\u4e2a\u5b57\u7b26\u5e8f\u5217\uff08\u56e0\u6b64\u5b57\u7b26\u4e32\u53ef\u4ee5\u4fdd\u5b58\u591a\u4e2a\u7b26\u53f7\uff09\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 4.17 - std::string \u7b80\u4ecb \u4e2d\u8ba8\u8bba\u5173\u4e8e\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u3002","title":"\u8f93\u5165\u5b57\u7b26"},{"location":"4-11-Chars/#_4","text":"C++ \u5c06\u5b57\u7b26\u7c7b\u578b\u7684\u5927\u5c0f\u5b9a\u4e49\u4e3a\u56fa\u5b9a\u76841\u5b57\u8282\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5b57\u7b26\u53ef\u4ee5\u662f\u6709\u7b26\u53f7\u6216\u8005\u65e0\u7b26\u53f7\u7684\uff08\u591a\u6570\u60c5\u51b5\u4e3a\u6709\u7b26\u53f7\uff09\u3002\u5982\u679c\u4f60\u4f7f\u7528\u5b57\u7b26\u53d8\u91cf\u6765\u4fdd\u5b58ASCII\u5b57\u7b26\u7684\u8bdd\uff0c\u4f60\u4e0d\u9700\u8981\u6307\u5b9a\u7b26\u53f7\uff0c\u56e0\u4e3a\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u7684\u5b57\u7b26\u53d8\u91cf\u90fd\u53ef\u4ee5\u4fdd\u5b580-127\u5185\u7684\u503c\u3002 \u5982\u679c\u4f60\u4f7f\u7528\u5b57\u7b26\u53d8\u91cf\u6765\u4fdd\u5b58\u4e00\u4e9b\u6bd4\u8f83\u5c0f\u7684\u6574\u578b\u503c\uff08\u5982\u975e\u4f18\u5316\u7a7a\u95f4\u8fd9\u6837\u7684\u7279\u522b\u9700\u8981\uff0c\u8bf7\u4e0d\u8981\u8fd9\u4e48\u505a\uff09\uff0c\u4f60\u5e94\u8be5\u6307\u5b9a\u5b83\u662f\u6709\u7b26\u53f7\u8fd8\u662f\u65e0\u7b26\u53f7\u7684\u3002\u6709\u7b26\u53f7\u7684\u5b57\u7b26\u53ef\u4ee5\u4fdd\u5b58\u7684\u6574\u6570\u8303\u56f4\u4e3a-128 \u5230 127\u3002\u65e0\u7b26\u53f7\u5b57\u7b26\u5219\u53ef\u4ee5\u4fdd\u5b58 0 \u5230 255 \u8303\u56f4\u5185\u7684\u6574\u6570\u3002","title":"\u5b57\u7b26\u7c7b\u578b\u7684\u5927\u5c0f\u3001\u8303\u56f4\u548c\u9ed8\u8ba4\u7b26\u53f7"},{"location":"4-11-Chars/#_5","text":"C++ \u4e2d\u7684\u67d0\u4e9b\u5b57\u7b26\u662f\u5177\u6709\u7279\u6b8a\u542b\u4e49\u7684\uff0c\u8fd9\u4e9b\u5b57\u7b26\u79f0\u4e3a\u8f6c\u8bd1\u5e8f\u5217\uff08escape sequences\uff09\u3002\u8f6c\u8bd1\u5e8f\u5217\u4ee5 \\ \uff08\u53cd\u659c\u7ebf\uff09\u5f00\u5934\uff0c\u7d27\u63a5\u7740\u662f\u4e00\u4e2a\u5b57\u6bcd\u6216\u6570\u5b57\u3002 \u4f60\u53ef\u80fd\u5df2\u7ecf\u89c1\u8fc7\u5728\u5e38\u9047\u5230\u7684\u8f6c\u4e49\u5e8f\u5217 '\\n' \uff0c\u5b83\u4f1a\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u4e00\u4e2a\u6362\u884c\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << \"First line \\n Second line \\n \" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 First line Second line \u53e6\u5916\u4e00\u4e2a\u5e38\u7528\u7684\u8f6c\u8bd1\u5e8f\u5217\u662f '\\t' \uff0c\u8868\u793a\u4e00\u4e2a\u6c34\u5e73\u5236\u8868\u7b26\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << \"First part \\t Second part\" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u4e3a 1 First part Second part \u5176\u4ed6\u4e00\u4e9b\u503c\u5f97\u6ce8\u610f\u7684\u8f6c\u8bd1\u5e8f\u5217\u6709\uff1a \\' \uff1a\u6253\u5370\u5355\u5f15\u53f7 \\\" \uff1a\u6253\u5370\u53cc\u5f15\u53f7 \\\\ \u6253\u5370\u53cd\u659c\u7ebf \u4e0b\u8868\u5c55\u793a\u4e86\u5168\u90e8\u7684\u8f6c\u4e49\u5e8f\u5217 \u540d\u79f0 \u7b26\u53f7 \u542b\u4e49 Alert \\a Makes an alert, such as a beep Backspace \\b Moves the cursor back one space Formfeed \\f Moves the cursor to next logical page Newline \\n Moves cursor to next line Carriage return \\r Moves cursor to beginning of line Horizontal tab \\t Prints a horizontal tab Vertical tab \\v Prints a vertical tab Single quote \\\u2019 Prints a single quote Double quote \\\u201d Prints a double quote Backslash \\ Prints a backslash. Question mark \\? Prints a question mark. No longer relevant. You can use question marks unescaped. Octal number (number) Translates into char represented by octal Hex number \\x(number) Translates into char represented by hex number \u4e3e\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { std :: cout << \" \\\" This is quoted text \\\"\\n \" ; std :: cout << \"This string contains a single backslash \\\\\\n \" ; std :: cout << \"6F in hex is char ' \\x6F ' \\n \" ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 \"This is quoted text\" This string contains a single backslash \\ 6F in hex is char 'o'","title":"\u8f6c\u8bd1\u5e8f\u5217"},{"location":"4-11-Chars/#newline-n-vs-stdendl","text":"\u5728 1.5 - iostream \u7b80\u4ecb\uff1a cout\uff0ccin \u548c endl \u4e2d\u5df2\u7ecf\u4ecb\u7ecd\u4e86\u3002","title":"Newline (\\n) vs. std::endl"},{"location":"4-11-Chars/#_6","text":"\u5355\u5b57\u7b26\u603b\u662f\u653e\u5728\u5355\u5f15\u53f7\u4e2d (\u4f8b\u5982 \u2018a\u2019, \u2018+\u2019, \u20185\u2019)\u3002\u5b57\u7b26\u53ea\u80fd\u8868\u793a\u4e00\u4e2a\u7b26\u53f7\uff08\u4f8b\u5982\uff0c\u5b57\u6bcd\u3001\u52a0\u53f7\u548c\u6570\u5b575\uff09\uff0c\u4e0b\u9762\u8fd9\u6837\u7684\u5199\u6cd5\u662f\u4e0d\u5bf9\u7684\uff1a 1 char ch { ' 56 ' }; // char \u53ea\u80fd\u4fdd\u6301\u4e00\u4e2a\u7b26\u53f7 \u53cc\u5f15\u53f7\u4e2d\u7684\u6587\u672c\u4f1a\u88ab\u770b\u505a\u662f\u5b57\u7b26\u4e32\uff08\u4f8b\u5982 \u201cHello, world!\u201d\uff09\u3002 \u4f60\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff1a 1 std :: cout << \"Hello, world!\" ; // \"Hello, world!\" is a string literal \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u53ea\u6709\u4e00\u4e2a\u5b57\u7b26\uff0c\u8bf7\u653e\u5728\u5355\u5f15\u53f7\u4e2d (\u4f8b\u5982 \u2018t\u2019 \u6216 \u2018\\n\u2019, \u800c\u4e0d\u662f \u201ct\u201d \u6216 \u201c\\n\u201d)\u3002\u4ee5\u4fbf\u7f16\u8bd1\u5668\u8fdb\u884c\u4f18\u5316\u3002","title":"\u5b57\u7b26\u653e\u5728\u5355\u5f15\u53f7\u548c\u53cc\u5f15\u53f7\u4e2d\u6709\u4ec0\u4e48\u533a\u522b"},{"location":"4-11-Chars/#wchar_t-char16_t-char32_t","text":"wchar_t \u5728\u51e0\u4e4e\u4efb\u4f55\u573a\u5408\u4e0b\u90fd\u5e94\u8be5\u907f\u514d\u4f7f\u7528 (\u5904\u7406\u5728\u4f7f\u7528 Windows API \u65f6)\u3002\u5b83\u7684\u5927\u5c0f\u662f\u5b9e\u73b0\u65f6\u5b9a\u4e49\u7684\uff0c\u975e\u5e38\u4e0d\u53ef\u9760\u800c\u4e14\u51e0\u4e4e\u5df2\u7ecf\u5e9f\u5f03\u4e86\u3002 \u9898\u5916\u8bdd \u672f\u8bed\u201c\u5e9f\u5f03\u201d\u6307\u7684\u662f\uff0c\u4ecd\u7136\u652f\u6301\uff0c\u4f46\u662f\u4e0d\u518d\u63a8\u8350\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u5df2\u7ecf\u88ab\u5176\u4ed6\u66f4\u597d\u7684\u540c\u7c7b\u66ff\u6362\u4e86\uff0c\u540c\u65f6\u5b83\u4e5f\u662f\u4e0d\u5b89\u5168\u7684\u3002 \u548c ASCII \u5c06\u6574\u578b 0-127 \u6620\u5c04\u5230\u82f1\u6587\u5b57\u6bcd\u4e00\u6837\uff0c\u4e5f\u6709\u5176\u4ed6\u5b57\u7b26\u7f16\u7801\u6807\u51c6\u53ef\u4ee5\u5c06\u6574\u578b\u6620\u5c04\u5230\u5176\u4ed6\u8bed\u8a00\u7684\u5b57\u7b26\u3002\u9664\u4e86 ASCII \u4ee5\u5916\u6700\u8457\u540d\u7684\u5b57\u6bcd\u7f16\u7801\u662f Unicode\uff0c\u5b83\u5c06 144,000 \u4e2a\u6574\u578b\u6570\u6620\u5c04\u5230\u4e0d\u540c\u8bed\u8a00\u7684\u5b57\u7b26\u3002\u56e0\u4e3a Unicode \u7684\u7f16\u7801\u70b9\u975e\u5e38\u591a\uff0c\u6240\u4ee5\u4e00\u4e2a Unicode \u7f16\u7801\u70b9\u9700\u898132\u4f4d\u624d\u80fd\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff08\u79f0\u4e3a UTF-32\uff09\u3002\u4e0d\u8fc7\uff0cUnicode \u5b57\u7b26\u4e5f\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a 16 \u4f4d\u6216 8\u4f4d\u5b57\u7b26 (\u5206\u522b\u79f0\u4e3a UTF-16 \u548c UTF-8)\u3002 char16_t \u548c char32_t \u5728 C++11 \u4e2d\u88ab\u5f15\u5165\uff0c\u7528\u4e8e\u652f\u6301 16 \u4f4d\u548c 32 \u4f4d Unicode \u5b57\u7b26\u3002 char8_t \u5728 C++20 \u4e2d\u88ab\u5f15\u5165\u3002 \u4f60\u4e0d\u9700\u8981\u4f7f\u7528 char8_t \u3001 char16_t \u6216 char32_t \uff0c\u89e6\u53d1\u4f60\u5e0c\u671b\u7a0b\u5e8f\u517c\u5bb9 Unicode\u3002Unicode \u548c\u672c\u5730\u5316\u4e00\u822c\u6765\u8bf4\u5df2\u7ecf\u8d85\u51fa\u4e86\u672c\u6559\u7a0b\u7684\u8303\u56f4\uff0c\u6240\u4ee5\u6211\u4eec\u5e76\u4e0d\u4f1a\u6d89\u53ca\u76f8\u5173\u5185\u5bb9\u3002 \u540c\u65f6\uff0c\u5f53\u4f60\u4f7f\u7528\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u7684\u65f6\u5019\uff0c\u5e94\u8be5\u53ea\u4f7f\u7528ASCII\u5b57\u7b26\u3002\u4f7f\u7528\u5176\u4ed6\u5b57\u7b26\u96c6\u7684\u5b57\u7b26\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5b57\u7b26\u663e\u793a\u9519\u8bef\u3002","title":"\u8fd8\u6709\u5176\u4ed6\u5b57\u7b26\u7c7b\u578b\u5417\uff1f wchar_t, char16_t \u548c char32_t?"},{"location":"4-12-Introduction-to-type-conversion-and-static_cast/","tags":["type conversion","static_cast"],"text":"Key Takeaway \u7f16\u8bd1\u5668\u4f1a\u5bf9\u57fa\u672c\u6570\u636e\u7c7b\u578b\u8fdb\u884c\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\uff0c\u4f46\u5bfc\u81f4\u4fe1\u606f\u4e22\u5931\u7684\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u4f1a\u5f15\u53d1\u544a\u8b66\u6216\u9519\u8bef\uff08\u62ec\u53f7\u521d\u59cb\u5316\u65f6\uff09 static_cast(expression) \u8fdb\u884c\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u6bcf\u5f53\u4f60\u770b\u5230\u6709\u5c16\u62ec\u53f7( <> )\u5b58\u5728\u7684 C++ \u8bed\u6cd5\uff08\u9664\u4e86\u9884\u5904\u7406\u5668\u6307\u4ee4\uff09\u65f6\uff0c\u5728\u4e24\u4e2a\u5c16\u62ec\u53f7\u4e2d\u95f4\u7684\u5185\u5bb9\u5f88\u53ef\u80fd\u662f\u4e00\u4e2a\u7c7b\u578b\u540d\u3002\u8fd9\u662fC++\u4e2d\u5904\u7406\u9700\u8981\u53ef\u53c2\u6570\u5316\u7c7b\u578b\u65f6\u7684\u65b9\u6cd5\u3002 static_cast \u8fd0\u7b97\u7b26\u4e0d\u4f1a\u505a\u4efb\u4f55\u8303\u56f4\u68c0\u67e5\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u5728\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u65b0\u7c7b\u578b\u5e76\u4e0d\u80fd\u5305\u542b\u8be5\u503c\uff0c\u5219\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u5927\u591a\u6570\u7f16\u8bd1\u5668\u5c06 std::int8_t \u548c std::uint8_t (\u4ee5\u53ca\u5bf9\u5e94\u7684\u901f\u5ea6\u4f18\u5148\u3001\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u7c7b\u578b) \u5206\u522b\u5f53\u505a signed char \u548c unsigned char \u5904\u7406\u3002 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u8003\u8651\u5982\u4e0b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void print ( double x ) // print takes an double parameter { std :: cout << x ; } int main () { print ( 5 ); // what happens when we pass an int value? return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c print() \u51fd\u6570\u7684 \u5f62\u53c2 \u4e3a double \u7c7b\u578b\uff0c\u4e3b\u8c03\u51fd\u6570\u5c06 5 \u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u4f46\u6b64\u5904 5 \u7684\u7c7b\u578b\u662f\u6574\u578b\u3002\u8fd9\u6837\u505a\u6709\u4ec0\u4e48\u540e\u679c\u5417\uff1f \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0cC++ \u4f1a\u5f88\u4e50\u4e8e\u5c06\u4e00\u79cd\u57fa\u672c\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u57fa\u672c\u7c7b\u578b\uff0c\u8fd9\u4e2a\u8f6c\u6362\u8fc7\u7a0b\u79f0\u4e3a \u7c7b\u578b\u8f6c\u6362(type conversion) \u3002\u56e0\u6b64\uff0c \u5b9e\u53c2 5 \u4f1a\u88ab\u8f6c\u6362\u4e3a double \u7c7b\u578b\u7684\u503c 5.0 \u5e76\u4e14\u88ab\u62f7\u8d1d\u7ed9\u5f62\u53c2 x \u3002\u4e8e\u662f print() \u5c31\u5c06\u8be5\u503c\u6253\u5370\u4e86\u51fa\u6765\uff1a 1 5 \u5f53\u7f16\u8bd1\u5668\u5e2e\u52a9\u6211\u4eec\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u7684\u65f6\u5019\uff0c\u6211\u4eec\u79f0\u5176\u4e3a \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u3002 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u544a\u8b66 \u5c3d\u7ba1\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u5df2\u7ecf\u8db3\u591f\u6ee1\u8db3\u5927\u591a\u6570\u7684\u7c7b\u578b\u8f6c\u6362\u9700\u8981\uff0c\u4f46\u662f\u4ecd\u7136\u6709\u4e9b\u60c5\u51b5\u662f\u65e0\u6cd5\u6ee1\u8db3\u7684\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void print ( int x ) // print \u51fd\u6570\u73b0\u5728\u63a5\u6536\u4e00\u4e2a\u6574\u578b\u7684\u53c2\u6570 { std :: cout << x ; } int main () { print ( 5.5 ); // \u8b66\u544a\uff1a\u6211\u4eec\u4f20\u5165\u4e86\u4e00\u4e2a double \u7c7b\u578b\u7684\u503c\u3002 return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u5c06 print() \u7684\u5f62\u53c2\u6539\u6210\u4e86 int \u7c7b\u578b\uff0c\u800c\u8c03\u7528 print() \u7684\u65f6\u5019\u5374\u4f20\u5165\u4e86\u4e00\u4e2a double \u7c7b\u578b\u7684\u503c 5.5 \u3002\u548c\u6700\u521d\u7684\u4f8b\u5b50\u7684\u60c5\u51b5\u7c7b\u4f3c\uff0c\u7f16\u8bd1\u5668\u4f1a\u5bf9\u5176\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff0c\u5c06 double \u503c 5.5 \u8f6c\u6362\u4e3a int \u7c7b\u578b\uff0c\u4ee5\u4fbf\u80fd\u591f\u88ab\u4f20\u5165 print() \u3002 \u4f46\u662f\u548c\u6700\u521d\u7684\u4f8b\u5b50\u76f8\u6bd4\uff0c\u5f53\u8fd9\u4e2a\u7a0b\u5e8f\u88ab\u7f16\u8bd1\u7684\u65f6\u5019\uff0c\u7f16\u8bd1\u5668\u4f1a\u4ea7\u751f\u5173\u4e8e\u6570\u636e\u4e22\u5931\u7684\u544a\u8b66\u3002\u5982\u679c\u4f60\u6253\u5f00\u4e86\u201c\u5c06\u8b66\u544a\u5f53\u505a\u9519\u8bef\u5904\u7406\u201d\u7684\u8bdd\uff0c\u4f60\u7684\u7f16\u8bd1\u5668\u5c31\u4f1a\u505c\u6b62\u7f16\u8bd1\u8fc7\u7a0b\u3002 \u5c0f\u8d34\u58eb \u4f60\u9700\u8981\u6682\u65f6\u5173\u95ed \u201c\u5c06\u8b66\u544a\u5f53\u505a\u9519\u8bef\u5904\u7406\u201d \u529f\u80fd\u624d\u80fd\u7f16\u8bd1\u8be5\u4f8b\u7a0b\uff0c\u8bf7\u53c2\u8003 0.11 -- Configuring your compiler: Warning and error levels for more information about this setting. \u7f16\u8bd1\u7a0b\u5e8f\u5e76\u8fd0\u884c\uff0c\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 5 \u6ce8\u610f\uff0c\u5c3d\u7ba1\u6211\u4eec\u4f20\u5165\u7684\u503c\u662f 5.5\uff0c\u4f46\u6253\u5370\u7ed3\u679c\u5374\u662f 5\u3002\u56e0\u4e3a\u6574\u578b\u53d8\u91cf\u4e0d\u80fd\u4fdd\u5b58\u5c0f\u6570\u90e8\u5206\uff0c\u6240\u4ee5 double \u7c7b\u578b\u7684\u503c 5.5 \u5c31\u88ab\u8f6c\u6362\u6210\u4e86\u4e00\u4e2a int\uff0c\u5c0f\u6570\u90e8\u5206\u76f4\u63a5\u88ab\u4e22\u5f03\u4e86\uff0c\u53ea\u6709\u6574\u6570\u90e8\u5206\u88ab\u4fdd\u7559\u4e86\u4e0b\u6765\u3002 \u56e0\u4e3a\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u6210\u6574\u578b\u503c\u4f1a\u5bfc\u81f4\u6d88\u5931\u90e8\u5206\u88ab\u4e22\u5f03\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u9700\u8981\u5728\u8fdb\u884c\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u7684\u65f6\u5019\u5411\u6211\u4eec\u53d1\u51fa\u544a\u8b66\u3002\u5373\u4f7f\u6211\u4eec\u4f20\u5165\u7684\u6d6e\u70b9\u6570\u672c\u8eab\u6ca1\u6709\u5c0f\u6570\u90e8\u5206\uff0c\u7f16\u8bd1\u5668\u4ecd\u7136\u4f1a\u53d1\u51fa\u544a\u8b66\uff0c\u4f8b\u5982 5.0 \u88ab\u8f6c\u6362\u6210\u6574\u578b\u65f6\u6ca1\u6709\u4e22\u5931\u5c0f\u6570\uff0c\u4f46\u662f\u7f16\u8bd1\u5668\u4ecd\u7136\u4f1a\u63d0\u9192\u6211\u4eec\u8fd9\u79cd\u8f6c\u6362\u662f\u4e0d\u5b89\u5168\u7684\u3002 \u5173\u952e\u4fe1\u606f \u6709\u4e9b\u7c7b\u578b\u8f6c\u6362\u603b\u662f\u5b89\u5168\u7684\uff08\u4f8b\u5982 int \u5230 double \uff09\uff0c\u800c\u53e6\u5916\u4e00\u4e9b\u7c7b\u578b\u5728\u8f6c\u6362\u65f6\u4f1a\u5bfc\u81f4\u503c\u7684\u53d8\u5316\uff08\u4f8b\u5982 double \u5230 int \uff09\u3002\u4e0d\u5b89\u5168\u7684\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u901a\u5e38\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u544a\u8b66\u6216\u62a5\u9519\uff08\u5728\u62ec\u53f7\u521d\u59cb\u5316\u65f6\uff09\u3002 \u8fd9\u4e5f\u662f\u63a8\u8350\u4f7f\u7528 \u62ec\u53f7\u521d\u59cb\u5316 \u7684\u4e3b\u8981\u539f\u56e0\u4e4b\u4e00\u3002\u62ec\u53f7\u521d\u59cb\u5316\u80fd\u591f\u907f\u514d\u521d\u59cb\u5316\u503c\u56e0\u4e3a\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u800c\u4e22\u5931\u4fe1\u606f\u3002 1 2 3 4 5 6 7 int main () { double d { 5 }; // okay: int to double is safe int x { 5.5 }; // error: double to int not safe return 0 ; } \u76f8\u5173\u5185\u5bb9 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u662f\u4e00\u4e2a\u5185\u5bb9\u975e\u5e38\u4e30\u5bcc\u7684\u8bdd\u9898\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u5bf9\u5176\u8fdb\u884c\u6df1\u5165\u89e3\u6790\uff08 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \uff09\u3002 \u4f7f\u7528 static_cast \u8fd0\u7b97\u7b26\u8fdb\u884c\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u5728\u56de\u5230\u7684 print() \u4f8b\u5b50\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u4e3b\u52a8\u5c06\u4e00\u4e2a double \u7c7b\u578b\u7684\u503c\u8f6c\u6362\u6210 int \u7c7b\u578b\u540e\u4f20\u5165\u51fd\u6570\u5462\uff1f\uff08\u5373\u4f7f\u77e5\u9053\u8fd9\u4e48\u505a\u4f1a\u4e22\u5931\u5c0f\u6570\u90e8\u5206\uff09\u3002\u5173\u95ed\u7f16\u8bd1\u5668\u7684\u201c\u5c06\u8b66\u544a\u5f53\u505a\u9519\u8bef\u5904\u7406\u201d\u529f\u80fd\u5e76\u4e0d\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\uff0c\u56e0\u5b83\u53ef\u80fd\u4f1a\u9020\u6210\u5176\u4ed6\u672c\u5e94\u8be5\u53d1\u73b0\u7684\u95ee\u9898\u88ab\u5ffd\u7565\u3002 C++ \u8fd8\u652f\u6301\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\u8f6c\u6362\uff0c\u79f0\u4e3a \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u3002\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u5141\u8bb8\u6211\u4eec\u660e\u786e\u5730\u544a\u8bc9\u7f16\u8bd1\u5668\u5c06\u4e00\u79cd\u7c7b\u578b\u7684\u503c\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\uff0c\u7a0b\u5e8f\u5458\u4f1a\u4e3a\u6b64\u64cd\u4f5c\u8d1f\u8d23\uff08\u5373\u5982\u679c\u8f6c\u6362\u5bfc\u81f4\u4e86\u4fe1\u606f\u7684\u4e22\u5931\uff0c\u7b97\u6211\u4eec\u7684\u9519\uff09\u3002 \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528 static_cast \u64cd\u4f5c\u7b26\u8fdb\u884c\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u3002 static cast \u7684\u8bed\u6cd5\u770b\u4e0a\u53bb\u6709\u4e9b\u53e4\u602a\uff1a 1 static_cast(expression) static_cast \u5c06\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u6570\uff0c\u7136\u540e\u8fd4\u56de\u7531 new_type \u6307\u5b9a\u7684\u65b0\u7c7b\u578b\u7684\u503c(\u4f8b\u5982 int\u3001bool\u3001char\u3001double)\u3002 \u5173\u952e\u4fe1\u606f \u6bcf\u5f53\u4f60\u770b\u5230\u6709\u5c16\u62ec\u53f7( <> )\u5b58\u5728\u7684 C++ \u8bed\u6cd5\uff08\u9664\u4e86\u9884\u5904\u7406\u5668\u6307\u4ee4\uff09\u65f6\uff0c\u5728\u4e24\u4e2a\u5c16\u62ec\u53f7\u4e2d\u95f4\u7684\u5185\u5bb9\u5f88\u53ef\u80fd\u662f\u4e00\u4e2a\u7c7b\u578b\u540d\u3002\u8fd9\u662f C++ \u4e2d\u5904\u7406\u9700\u8981\u53ef\u53c2\u6570\u5316\u7c7b\u578b\u65f6\u7684\u65b9\u6cd5\u3002 \u4f7f\u7528 static_cast \u4fee\u6539\u4e4b\u524d\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void print ( int x ) { std :: cout << x ; } int main () { print ( static_cast < int > ( 5.5 ) ); // explicitly convert double value 5.5 to an int return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u663e\u5f0f\u5730\u8981\u6c42\u5c06 double \u7c7b\u578b\u7684\u503c 5.5 \u8f6c\u6362\u4e3a int \u7c7b\u578b\u503c\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4e0d\u4f1a\u4ea7\u751f\u544a\u8b66\u4fe1\u606f\uff08\u65e0\u9700\u5173\u95ed \u201c\u5c06\u8b66\u544a\u5f53\u505a\u9519\u8bef\u5904\u7406\u201d \u529f\u80fd\uff09\u3002 \u76f8\u5173\u5185\u5bb9 C++ \u8fd8\u652f\u6301\u5176\u4ed6\u7c7b\u578b\u7684\u8f6c\u6362\u3002\u6211\u4eec\u4f1a\u5728 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd \u4f7f\u7528 static_cast \u5c06 char \u8f6c\u6362\u4e3a int \u5728 4.11 - \u5b57\u7b26 \u4e2d\uff0c\u6211\u4eec\u770b\u5230\u4f7f\u7528 std::cout \u6253\u5370\u5b57\u7b26\u7c7b\u578b\u65f6\uff0c\u5c4f\u5e55\u4e0a\u4f1a\u6253\u5370\u4e00\u4e2a\u5b57\u7b26\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char ch { 97 }; // 97 is ASCII code for 'a' std :: cout << ch << '\\n' ; return 0 ; } This prints: 1 a \u5982\u679c\u6211\u4eec\u60f3\u8981\u6253\u5370\u5176\u5bf9\u5e94\u7684\u6574\u578b\u503c\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06 char \u8f6c\u6362\u4e3a int \uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char ch { 97 }; // 97 \u662f ASCII \u7801\u4e2d\u7684 'a' std :: cout << ch << \" has value \" << static_cast < int > ( ch ) << '\\n' ; // \u5c06 ch \u6253\u5370\u4e3a int return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 a has value 97 \u8bf7\u6ce8\u610f static_cast \u7684\u5f62\u53c2\u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\u3002\u5f53\u4f20\u5165\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u53d8\u91cf\u9996\u5148\u4f1a\u8fdb\u884c\u6c42\u503c\u5e76\u5f97\u5230\u5177\u4f53\u7684\u503c\uff0c\u7136\u540e\u8be5\u503c\u4f1a\u88ab\u8f6c\u6362\u6210\u65b0\u7684\u7c7b\u578b\u3002\u4f5c\u4e3a\u5b9e\u53c2\u7684\u53d8\u91cf\u672c\u8eab\u5e76\u4e0d\u4f1a\u53d7\u5230\u5f71\u54cd\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c ch \u4ecd\u7136\u662f\u5b57\u7b26\u7c7b\u578b\uff0c\u5b83\u4fdd\u5b58\u7684\u503c\u4e5f\u662f\u4ecd\u7136\u662f\u539f\u6765\u7684\u503c\u3002 \u5c06\u65e0\u7b26\u53f7\u6570\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u6570 \u5c06\u65e0\u7b26\u53f7\u7c7b\u578b\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u7c7b\u578b\uff0c\u540c\u6837\u53ef\u4ee5\u4f7f\u7528 static_cast \u8fd0\u7b97\u7b26\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { unsigned int u { 5u }; // 5u \u8868\u793a 5 \u662f\u65e0\u7b26\u53f7\u6574\u578b int s { static_cast < int > ( u ) }; // \u8fd4\u56de\u503c\u7c7b\u578b\u4e3a int std :: cout << s ; return 0 ; } static_cast \u8fd0\u7b97\u7b26\u4e0d\u4f1a\u505a\u4efb\u4f55\u8303\u56f4\u68c0\u67e5\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u5728\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u65b0\u7c7b\u578b\u5e76\u4e0d\u80fd\u5305\u542b\u8be5\u503c\uff0c\u5219\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u56e0\u6b64\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c unsigned int \u4fdd\u5b58\u7684\u503c\u6bd4 int \u80fd\u591f\u4fdd\u5b58\u7684\u6700\u5927\u503c\u8fd8\u5927\uff0c\u5219\u5c06 unsigned int \u8f6c\u6362\u4e3a int \u65f6\u4f1a\u4ea7\u751f\u65e0\u6cd5\u9884\u8ba1\u7684\u7ed3\u679c\u3002 \u6ce8\u610f \u5982\u679c\u88ab\u8f6c\u6362\u7684\u503c\u4e0d\u80fd\u5339\u914d\u65b0\u7c7b\u578b\u7684\u8303\u56f4\uff0c\u5219 static_cast \u8fd0\u7b97\u7b26\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 std::int8_t \u548c std::uint8_t \u884c\u4e3a\u66f4\u50cf chars \u800c\u4e0d\u662f\u6574\u578b \u6b63\u5982 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t \u4e2d\u4ecb\u7ecd\u7684\u90a3\u6837\uff0c\u5927\u591a\u6570\u7f16\u8bd1\u5668\u5c06 std::int8_t \u548c std::uint8_t (\u4ee5\u53ca\u5bf9\u5e94\u7684\u901f\u5ea6\u4f18\u5148\u3001\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u7c7b\u578b) \u5206\u522b\u5f53\u505a signed char \u548c unsigned char \u5904\u7406\u3002\u73b0\u5728\uff0c\u65e2\u7136\u6211\u4eec\u5df2\u7ecf\u4ecb\u7ecd\u4e86\u4ec0\u4e48\u662f\u5b57\u7b26\uff0c\u63a5\u4e0b\u6765\u5c31\u53ef\u4ee5\u8c08\u8c08\uff0c\u7f16\u8bd1\u5668\u8fd9\u6837\u505a\u4f1a\u5e26\u6765\u4ec0\u4e48\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { std :: int8_t myint { 65 }; // initialize myint with value 65 std :: cout << myint ; // you're probably expecting this to print 65 return 0 ; } \u56e0\u4e3a std::int8_t \u81ea\u79f0\u662f\u4e00\u79cd\u6574\u578b\uff0c\u6240\u4ee5\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u4e0a\u9762\u7684\u7a0b\u5e8f\u4f1a\u6253\u5370 65\u3002\u4e0d\u8fc7\uff0c\u5728\u5927\u591a\u6570\u7cfb\u7edf\u4e0a\uff0c\u4e0a\u9762\u7684\u7a0b\u5e8f\u6253\u5370\u7684\u5176\u5b9e\u662f A \uff08 myint \u88ab\u5f53\u505a\u4e86 signed char \uff09\u5904\u7406\u3002\u4f46\u662f\uff0c\u8fd9\u4e5f\u4e0d\u662f\u7edd\u5bf9\u7684\uff08\u6709\u4e9b\u7cfb\u7edf\u786e\u5b9e\u4f1a\u6253\u5370 65\uff09\u3002 \u5982\u679c\u4f60\u60f3\u8981\u786e\u4fdd std::int8_t \u6216 std::uint8_t \u5bf9\u8c61\u90fd\u88ab\u5f53\u505a\u6574\u578b\u5904\u7406\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { std :: int8_t myint { 65 }; std :: cout << static_cast < int > ( myint ); // \u603b\u662f\u6253\u5370 65 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c std::int8_t \u88ab\u5f53\u505a\u6574\u578b\u5904\u7406\u4e86\uff0c\u4f46\u662f\u5982\u679c\u4ece\u63a7\u5236\u53f0\u83b7\u53d6\u8f93\u5165\uff0c\u4ecd\u7136\u4f1a\u5e26\u6765\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include int main () { std :: cout << \"Enter a number between 0 and 255: \" ; std :: int8_t myint {}; std :: cin >> myint ; std :: cout << \"You entered: \" << static_cast < int > ( myint ); return 0 ; } \u8fd0\u884c\u7a0b\u5e8f\uff1a 1 2 Enter a number between 0 and 255: 35 You entered: 51 \u8fd9\u4e2a\u95ee\u9898\u662f\u8fd9\u6837\u7684\uff0c\u5f53 std::int8_t \u88ab\u770b\u505a\u5b57\u7b26\u65f6\uff0c\u8f93\u5165\u51fd\u6570\u4f1a\u5c06\u8f93\u5165\u7684\u5185\u5bb9\u770b\u505a\u662f\u4e00\u7cfb\u5217\u7684\u5b57\u7b26\u800c\u4e0d\u662f\u6574\u578b\u3002\u5f53\u4f60\u8f93\u516535\u4e4b\u540e\uff0c\u4f60\u5b9e\u9645\u4e0a\u6570\u4e86 '3' \u548c '5' \u4e24\u4e2a\u5b57\u7b26\u3002\u56e0\u4e3a\u5b57\u7b26\u7c7b\u578b\u53ea\u80fd\u4fdd\u5b58\u4e00\u4e2a\u5b57\u7b26\uff0c\u56e0\u6b64\u53ea '3' \u88ab\u4fdd\u5b58\u5230\u4e86\u53d8\u91cf\u4e2d\uff08 '5' \u88ab\u7559\u5728\u4e86\u8f93\u5165\u6d41\u4e2d\u4ee5\u4fbf\u540e\u7eed\u7684\u4f7f\u7528\uff09\u3002\u56e0\u4e3a\u5b57\u7b26 '3' \u7684 ASCII \u7801\u662f 51\uff0c\u6240\u4ee551\u5c31\u88ab\u5b58\u653e\u5230\u4e86 myint \u4e2d\uff0c\u6700\u7ec8\u4e5f\u5c31\u88ab\u6253\u5370\u4e86\u51fa\u6765\u3002 \u4e0e\u4e4b\u76f8\u5bf9\u7684\uff0c\u5176\u4ed6\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u5728\u8f93\u5165\u548c\u8f93\u51fa\u65f6\u603b\u662f\u4f1a\u88ab\u5f53\u505a\u6574\u578b\u503c\u5904\u7406\u3002","title":"4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast"},{"location":"4-12-Introduction-to-type-conversion-and-static_cast/#_1","text":"\u8003\u8651\u5982\u4e0b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void print ( double x ) // print takes an double parameter { std :: cout << x ; } int main () { print ( 5 ); // what happens when we pass an int value? return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c print() \u51fd\u6570\u7684 \u5f62\u53c2 \u4e3a double \u7c7b\u578b\uff0c\u4e3b\u8c03\u51fd\u6570\u5c06 5 \u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u4f46\u6b64\u5904 5 \u7684\u7c7b\u578b\u662f\u6574\u578b\u3002\u8fd9\u6837\u505a\u6709\u4ec0\u4e48\u540e\u679c\u5417\uff1f \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0cC++ \u4f1a\u5f88\u4e50\u4e8e\u5c06\u4e00\u79cd\u57fa\u672c\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u57fa\u672c\u7c7b\u578b\uff0c\u8fd9\u4e2a\u8f6c\u6362\u8fc7\u7a0b\u79f0\u4e3a \u7c7b\u578b\u8f6c\u6362(type conversion) \u3002\u56e0\u6b64\uff0c \u5b9e\u53c2 5 \u4f1a\u88ab\u8f6c\u6362\u4e3a double \u7c7b\u578b\u7684\u503c 5.0 \u5e76\u4e14\u88ab\u62f7\u8d1d\u7ed9\u5f62\u53c2 x \u3002\u4e8e\u662f print() \u5c31\u5c06\u8be5\u503c\u6253\u5370\u4e86\u51fa\u6765\uff1a 1 5 \u5f53\u7f16\u8bd1\u5668\u5e2e\u52a9\u6211\u4eec\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u7684\u65f6\u5019\uff0c\u6211\u4eec\u79f0\u5176\u4e3a \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u3002","title":"\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362"},{"location":"4-12-Introduction-to-type-conversion-and-static_cast/#_2","text":"\u5c3d\u7ba1\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u5df2\u7ecf\u8db3\u591f\u6ee1\u8db3\u5927\u591a\u6570\u7684\u7c7b\u578b\u8f6c\u6362\u9700\u8981\uff0c\u4f46\u662f\u4ecd\u7136\u6709\u4e9b\u60c5\u51b5\u662f\u65e0\u6cd5\u6ee1\u8db3\u7684\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void print ( int x ) // print \u51fd\u6570\u73b0\u5728\u63a5\u6536\u4e00\u4e2a\u6574\u578b\u7684\u53c2\u6570 { std :: cout << x ; } int main () { print ( 5.5 ); // \u8b66\u544a\uff1a\u6211\u4eec\u4f20\u5165\u4e86\u4e00\u4e2a double \u7c7b\u578b\u7684\u503c\u3002 return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u5c06 print() \u7684\u5f62\u53c2\u6539\u6210\u4e86 int \u7c7b\u578b\uff0c\u800c\u8c03\u7528 print() \u7684\u65f6\u5019\u5374\u4f20\u5165\u4e86\u4e00\u4e2a double \u7c7b\u578b\u7684\u503c 5.5 \u3002\u548c\u6700\u521d\u7684\u4f8b\u5b50\u7684\u60c5\u51b5\u7c7b\u4f3c\uff0c\u7f16\u8bd1\u5668\u4f1a\u5bf9\u5176\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff0c\u5c06 double \u503c 5.5 \u8f6c\u6362\u4e3a int \u7c7b\u578b\uff0c\u4ee5\u4fbf\u80fd\u591f\u88ab\u4f20\u5165 print() \u3002 \u4f46\u662f\u548c\u6700\u521d\u7684\u4f8b\u5b50\u76f8\u6bd4\uff0c\u5f53\u8fd9\u4e2a\u7a0b\u5e8f\u88ab\u7f16\u8bd1\u7684\u65f6\u5019\uff0c\u7f16\u8bd1\u5668\u4f1a\u4ea7\u751f\u5173\u4e8e\u6570\u636e\u4e22\u5931\u7684\u544a\u8b66\u3002\u5982\u679c\u4f60\u6253\u5f00\u4e86\u201c\u5c06\u8b66\u544a\u5f53\u505a\u9519\u8bef\u5904\u7406\u201d\u7684\u8bdd\uff0c\u4f60\u7684\u7f16\u8bd1\u5668\u5c31\u4f1a\u505c\u6b62\u7f16\u8bd1\u8fc7\u7a0b\u3002 \u5c0f\u8d34\u58eb \u4f60\u9700\u8981\u6682\u65f6\u5173\u95ed \u201c\u5c06\u8b66\u544a\u5f53\u505a\u9519\u8bef\u5904\u7406\u201d \u529f\u80fd\u624d\u80fd\u7f16\u8bd1\u8be5\u4f8b\u7a0b\uff0c\u8bf7\u53c2\u8003 0.11 -- Configuring your compiler: Warning and error levels for more information about this setting. \u7f16\u8bd1\u7a0b\u5e8f\u5e76\u8fd0\u884c\uff0c\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 5 \u6ce8\u610f\uff0c\u5c3d\u7ba1\u6211\u4eec\u4f20\u5165\u7684\u503c\u662f 5.5\uff0c\u4f46\u6253\u5370\u7ed3\u679c\u5374\u662f 5\u3002\u56e0\u4e3a\u6574\u578b\u53d8\u91cf\u4e0d\u80fd\u4fdd\u5b58\u5c0f\u6570\u90e8\u5206\uff0c\u6240\u4ee5 double \u7c7b\u578b\u7684\u503c 5.5 \u5c31\u88ab\u8f6c\u6362\u6210\u4e86\u4e00\u4e2a int\uff0c\u5c0f\u6570\u90e8\u5206\u76f4\u63a5\u88ab\u4e22\u5f03\u4e86\uff0c\u53ea\u6709\u6574\u6570\u90e8\u5206\u88ab\u4fdd\u7559\u4e86\u4e0b\u6765\u3002 \u56e0\u4e3a\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u6210\u6574\u578b\u503c\u4f1a\u5bfc\u81f4\u6d88\u5931\u90e8\u5206\u88ab\u4e22\u5f03\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u9700\u8981\u5728\u8fdb\u884c\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u7684\u65f6\u5019\u5411\u6211\u4eec\u53d1\u51fa\u544a\u8b66\u3002\u5373\u4f7f\u6211\u4eec\u4f20\u5165\u7684\u6d6e\u70b9\u6570\u672c\u8eab\u6ca1\u6709\u5c0f\u6570\u90e8\u5206\uff0c\u7f16\u8bd1\u5668\u4ecd\u7136\u4f1a\u53d1\u51fa\u544a\u8b66\uff0c\u4f8b\u5982 5.0 \u88ab\u8f6c\u6362\u6210\u6574\u578b\u65f6\u6ca1\u6709\u4e22\u5931\u5c0f\u6570\uff0c\u4f46\u662f\u7f16\u8bd1\u5668\u4ecd\u7136\u4f1a\u63d0\u9192\u6211\u4eec\u8fd9\u79cd\u8f6c\u6362\u662f\u4e0d\u5b89\u5168\u7684\u3002 \u5173\u952e\u4fe1\u606f \u6709\u4e9b\u7c7b\u578b\u8f6c\u6362\u603b\u662f\u5b89\u5168\u7684\uff08\u4f8b\u5982 int \u5230 double \uff09\uff0c\u800c\u53e6\u5916\u4e00\u4e9b\u7c7b\u578b\u5728\u8f6c\u6362\u65f6\u4f1a\u5bfc\u81f4\u503c\u7684\u53d8\u5316\uff08\u4f8b\u5982 double \u5230 int \uff09\u3002\u4e0d\u5b89\u5168\u7684\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u901a\u5e38\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u544a\u8b66\u6216\u62a5\u9519\uff08\u5728\u62ec\u53f7\u521d\u59cb\u5316\u65f6\uff09\u3002 \u8fd9\u4e5f\u662f\u63a8\u8350\u4f7f\u7528 \u62ec\u53f7\u521d\u59cb\u5316 \u7684\u4e3b\u8981\u539f\u56e0\u4e4b\u4e00\u3002\u62ec\u53f7\u521d\u59cb\u5316\u80fd\u591f\u907f\u514d\u521d\u59cb\u5316\u503c\u56e0\u4e3a\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u800c\u4e22\u5931\u4fe1\u606f\u3002 1 2 3 4 5 6 7 int main () { double d { 5 }; // okay: int to double is safe int x { 5.5 }; // error: double to int not safe return 0 ; } \u76f8\u5173\u5185\u5bb9 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u662f\u4e00\u4e2a\u5185\u5bb9\u975e\u5e38\u4e30\u5bcc\u7684\u8bdd\u9898\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u5bf9\u5176\u8fdb\u884c\u6df1\u5165\u89e3\u6790\uff08 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \uff09\u3002","title":"\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u544a\u8b66"},{"location":"4-12-Introduction-to-type-conversion-and-static_cast/#static_cast","text":"\u5728\u56de\u5230\u7684 print() \u4f8b\u5b50\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u4e3b\u52a8\u5c06\u4e00\u4e2a double \u7c7b\u578b\u7684\u503c\u8f6c\u6362\u6210 int \u7c7b\u578b\u540e\u4f20\u5165\u51fd\u6570\u5462\uff1f\uff08\u5373\u4f7f\u77e5\u9053\u8fd9\u4e48\u505a\u4f1a\u4e22\u5931\u5c0f\u6570\u90e8\u5206\uff09\u3002\u5173\u95ed\u7f16\u8bd1\u5668\u7684\u201c\u5c06\u8b66\u544a\u5f53\u505a\u9519\u8bef\u5904\u7406\u201d\u529f\u80fd\u5e76\u4e0d\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\uff0c\u56e0\u5b83\u53ef\u80fd\u4f1a\u9020\u6210\u5176\u4ed6\u672c\u5e94\u8be5\u53d1\u73b0\u7684\u95ee\u9898\u88ab\u5ffd\u7565\u3002 C++ \u8fd8\u652f\u6301\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\u8f6c\u6362\uff0c\u79f0\u4e3a \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u3002\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u5141\u8bb8\u6211\u4eec\u660e\u786e\u5730\u544a\u8bc9\u7f16\u8bd1\u5668\u5c06\u4e00\u79cd\u7c7b\u578b\u7684\u503c\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\uff0c\u7a0b\u5e8f\u5458\u4f1a\u4e3a\u6b64\u64cd\u4f5c\u8d1f\u8d23\uff08\u5373\u5982\u679c\u8f6c\u6362\u5bfc\u81f4\u4e86\u4fe1\u606f\u7684\u4e22\u5931\uff0c\u7b97\u6211\u4eec\u7684\u9519\uff09\u3002 \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528 static_cast \u64cd\u4f5c\u7b26\u8fdb\u884c\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u3002 static cast \u7684\u8bed\u6cd5\u770b\u4e0a\u53bb\u6709\u4e9b\u53e4\u602a\uff1a 1 static_cast(expression) static_cast \u5c06\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u6570\uff0c\u7136\u540e\u8fd4\u56de\u7531 new_type \u6307\u5b9a\u7684\u65b0\u7c7b\u578b\u7684\u503c(\u4f8b\u5982 int\u3001bool\u3001char\u3001double)\u3002 \u5173\u952e\u4fe1\u606f \u6bcf\u5f53\u4f60\u770b\u5230\u6709\u5c16\u62ec\u53f7( <> )\u5b58\u5728\u7684 C++ \u8bed\u6cd5\uff08\u9664\u4e86\u9884\u5904\u7406\u5668\u6307\u4ee4\uff09\u65f6\uff0c\u5728\u4e24\u4e2a\u5c16\u62ec\u53f7\u4e2d\u95f4\u7684\u5185\u5bb9\u5f88\u53ef\u80fd\u662f\u4e00\u4e2a\u7c7b\u578b\u540d\u3002\u8fd9\u662f C++ \u4e2d\u5904\u7406\u9700\u8981\u53ef\u53c2\u6570\u5316\u7c7b\u578b\u65f6\u7684\u65b9\u6cd5\u3002 \u4f7f\u7528 static_cast \u4fee\u6539\u4e4b\u524d\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void print ( int x ) { std :: cout << x ; } int main () { print ( static_cast < int > ( 5.5 ) ); // explicitly convert double value 5.5 to an int return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u663e\u5f0f\u5730\u8981\u6c42\u5c06 double \u7c7b\u578b\u7684\u503c 5.5 \u8f6c\u6362\u4e3a int \u7c7b\u578b\u503c\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4e0d\u4f1a\u4ea7\u751f\u544a\u8b66\u4fe1\u606f\uff08\u65e0\u9700\u5173\u95ed \u201c\u5c06\u8b66\u544a\u5f53\u505a\u9519\u8bef\u5904\u7406\u201d \u529f\u80fd\uff09\u3002 \u76f8\u5173\u5185\u5bb9 C++ \u8fd8\u652f\u6301\u5176\u4ed6\u7c7b\u578b\u7684\u8f6c\u6362\u3002\u6211\u4eec\u4f1a\u5728 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd","title":"\u4f7f\u7528 static_cast \u8fd0\u7b97\u7b26\u8fdb\u884c\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362"},{"location":"4-12-Introduction-to-type-conversion-and-static_cast/#static_cast-char-int","text":"\u5728 4.11 - \u5b57\u7b26 \u4e2d\uff0c\u6211\u4eec\u770b\u5230\u4f7f\u7528 std::cout \u6253\u5370\u5b57\u7b26\u7c7b\u578b\u65f6\uff0c\u5c4f\u5e55\u4e0a\u4f1a\u6253\u5370\u4e00\u4e2a\u5b57\u7b26\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char ch { 97 }; // 97 is ASCII code for 'a' std :: cout << ch << '\\n' ; return 0 ; } This prints: 1 a \u5982\u679c\u6211\u4eec\u60f3\u8981\u6253\u5370\u5176\u5bf9\u5e94\u7684\u6574\u578b\u503c\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06 char \u8f6c\u6362\u4e3a int \uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char ch { 97 }; // 97 \u662f ASCII \u7801\u4e2d\u7684 'a' std :: cout << ch << \" has value \" << static_cast < int > ( ch ) << '\\n' ; // \u5c06 ch \u6253\u5370\u4e3a int return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 a has value 97 \u8bf7\u6ce8\u610f static_cast \u7684\u5f62\u53c2\u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\u3002\u5f53\u4f20\u5165\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u53d8\u91cf\u9996\u5148\u4f1a\u8fdb\u884c\u6c42\u503c\u5e76\u5f97\u5230\u5177\u4f53\u7684\u503c\uff0c\u7136\u540e\u8be5\u503c\u4f1a\u88ab\u8f6c\u6362\u6210\u65b0\u7684\u7c7b\u578b\u3002\u4f5c\u4e3a\u5b9e\u53c2\u7684\u53d8\u91cf\u672c\u8eab\u5e76\u4e0d\u4f1a\u53d7\u5230\u5f71\u54cd\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c ch \u4ecd\u7136\u662f\u5b57\u7b26\u7c7b\u578b\uff0c\u5b83\u4fdd\u5b58\u7684\u503c\u4e5f\u662f\u4ecd\u7136\u662f\u539f\u6765\u7684\u503c\u3002","title":"\u4f7f\u7528 static_cast \u5c06 char \u8f6c\u6362\u4e3a int"},{"location":"4-12-Introduction-to-type-conversion-and-static_cast/#_3","text":"\u5c06\u65e0\u7b26\u53f7\u7c7b\u578b\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u7c7b\u578b\uff0c\u540c\u6837\u53ef\u4ee5\u4f7f\u7528 static_cast \u8fd0\u7b97\u7b26\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { unsigned int u { 5u }; // 5u \u8868\u793a 5 \u662f\u65e0\u7b26\u53f7\u6574\u578b int s { static_cast < int > ( u ) }; // \u8fd4\u56de\u503c\u7c7b\u578b\u4e3a int std :: cout << s ; return 0 ; } static_cast \u8fd0\u7b97\u7b26\u4e0d\u4f1a\u505a\u4efb\u4f55\u8303\u56f4\u68c0\u67e5\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u5728\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u65b0\u7c7b\u578b\u5e76\u4e0d\u80fd\u5305\u542b\u8be5\u503c\uff0c\u5219\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u56e0\u6b64\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c unsigned int \u4fdd\u5b58\u7684\u503c\u6bd4 int \u80fd\u591f\u4fdd\u5b58\u7684\u6700\u5927\u503c\u8fd8\u5927\uff0c\u5219\u5c06 unsigned int \u8f6c\u6362\u4e3a int \u65f6\u4f1a\u4ea7\u751f\u65e0\u6cd5\u9884\u8ba1\u7684\u7ed3\u679c\u3002 \u6ce8\u610f \u5982\u679c\u88ab\u8f6c\u6362\u7684\u503c\u4e0d\u80fd\u5339\u914d\u65b0\u7c7b\u578b\u7684\u8303\u56f4\uff0c\u5219 static_cast \u8fd0\u7b97\u7b26\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u3002","title":"\u5c06\u65e0\u7b26\u53f7\u6570\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u6570"},{"location":"4-12-Introduction-to-type-conversion-and-static_cast/#stdint8_t-stduint8_t-chars","text":"\u6b63\u5982 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t \u4e2d\u4ecb\u7ecd\u7684\u90a3\u6837\uff0c\u5927\u591a\u6570\u7f16\u8bd1\u5668\u5c06 std::int8_t \u548c std::uint8_t (\u4ee5\u53ca\u5bf9\u5e94\u7684\u901f\u5ea6\u4f18\u5148\u3001\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u7c7b\u578b) \u5206\u522b\u5f53\u505a signed char \u548c unsigned char \u5904\u7406\u3002\u73b0\u5728\uff0c\u65e2\u7136\u6211\u4eec\u5df2\u7ecf\u4ecb\u7ecd\u4e86\u4ec0\u4e48\u662f\u5b57\u7b26\uff0c\u63a5\u4e0b\u6765\u5c31\u53ef\u4ee5\u8c08\u8c08\uff0c\u7f16\u8bd1\u5668\u8fd9\u6837\u505a\u4f1a\u5e26\u6765\u4ec0\u4e48\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { std :: int8_t myint { 65 }; // initialize myint with value 65 std :: cout << myint ; // you're probably expecting this to print 65 return 0 ; } \u56e0\u4e3a std::int8_t \u81ea\u79f0\u662f\u4e00\u79cd\u6574\u578b\uff0c\u6240\u4ee5\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u4e0a\u9762\u7684\u7a0b\u5e8f\u4f1a\u6253\u5370 65\u3002\u4e0d\u8fc7\uff0c\u5728\u5927\u591a\u6570\u7cfb\u7edf\u4e0a\uff0c\u4e0a\u9762\u7684\u7a0b\u5e8f\u6253\u5370\u7684\u5176\u5b9e\u662f A \uff08 myint \u88ab\u5f53\u505a\u4e86 signed char \uff09\u5904\u7406\u3002\u4f46\u662f\uff0c\u8fd9\u4e5f\u4e0d\u662f\u7edd\u5bf9\u7684\uff08\u6709\u4e9b\u7cfb\u7edf\u786e\u5b9e\u4f1a\u6253\u5370 65\uff09\u3002 \u5982\u679c\u4f60\u60f3\u8981\u786e\u4fdd std::int8_t \u6216 std::uint8_t \u5bf9\u8c61\u90fd\u88ab\u5f53\u505a\u6574\u578b\u5904\u7406\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { std :: int8_t myint { 65 }; std :: cout << static_cast < int > ( myint ); // \u603b\u662f\u6253\u5370 65 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c std::int8_t \u88ab\u5f53\u505a\u6574\u578b\u5904\u7406\u4e86\uff0c\u4f46\u662f\u5982\u679c\u4ece\u63a7\u5236\u53f0\u83b7\u53d6\u8f93\u5165\uff0c\u4ecd\u7136\u4f1a\u5e26\u6765\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include int main () { std :: cout << \"Enter a number between 0 and 255: \" ; std :: int8_t myint {}; std :: cin >> myint ; std :: cout << \"You entered: \" << static_cast < int > ( myint ); return 0 ; } \u8fd0\u884c\u7a0b\u5e8f\uff1a 1 2 Enter a number between 0 and 255: 35 You entered: 51 \u8fd9\u4e2a\u95ee\u9898\u662f\u8fd9\u6837\u7684\uff0c\u5f53 std::int8_t \u88ab\u770b\u505a\u5b57\u7b26\u65f6\uff0c\u8f93\u5165\u51fd\u6570\u4f1a\u5c06\u8f93\u5165\u7684\u5185\u5bb9\u770b\u505a\u662f\u4e00\u7cfb\u5217\u7684\u5b57\u7b26\u800c\u4e0d\u662f\u6574\u578b\u3002\u5f53\u4f60\u8f93\u516535\u4e4b\u540e\uff0c\u4f60\u5b9e\u9645\u4e0a\u6570\u4e86 '3' \u548c '5' \u4e24\u4e2a\u5b57\u7b26\u3002\u56e0\u4e3a\u5b57\u7b26\u7c7b\u578b\u53ea\u80fd\u4fdd\u5b58\u4e00\u4e2a\u5b57\u7b26\uff0c\u56e0\u6b64\u53ea '3' \u88ab\u4fdd\u5b58\u5230\u4e86\u53d8\u91cf\u4e2d\uff08 '5' \u88ab\u7559\u5728\u4e86\u8f93\u5165\u6d41\u4e2d\u4ee5\u4fbf\u540e\u7eed\u7684\u4f7f\u7528\uff09\u3002\u56e0\u4e3a\u5b57\u7b26 '3' \u7684 ASCII \u7801\u662f 51\uff0c\u6240\u4ee551\u5c31\u88ab\u5b58\u653e\u5230\u4e86 myint \u4e2d\uff0c\u6700\u7ec8\u4e5f\u5c31\u88ab\u6253\u5370\u4e86\u51fa\u6765\u3002 \u4e0e\u4e4b\u76f8\u5bf9\u7684\uff0c\u5176\u4ed6\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u5728\u8f93\u5165\u548c\u8f93\u51fa\u65f6\u603b\u662f\u4f1a\u88ab\u5f53\u505a\u6574\u578b\u503c\u5904\u7406\u3002","title":"std::int8_t \u548c std::uint8_t \u884c\u4e3a\u66f4\u50cf chars \u800c\u4e0d\u662f\u6574\u578b"},{"location":"4-13-Const-variables-and-symbolic-constants/","tags":["const"],"text":"Key Takeaway const \u53d8\u91cf\u5fc5\u987b\u521d\u59cb\u5316\uff0c\u4e14\u521d\u59cb\u5316\u4e4b\u540e\u503c\u4e0d\u53ef\u4ee5\u6539\u53d8 \u51fd\u6570\u53c2\u6570\u53ef\u4ee5\u4e3a const \uff0c\u4f46\u662f\u4f20\u503c\u7684\u60c5\u51b5\u4e0b\u4e0d\u9700\u8981\u7528 \u51fd\u6570\u7684\u8fd4\u56de\u503c\u53ef\u4ee5\u4e3a const \uff0c\u4f46\u662f\u4e00\u822c\u4e0d\u8981\u7528\uff0c\u6ca1\u6709\u610f\u4e49\u800c\u4e14\u4f1a\u5f71\u54cd\u6027\u80fd \u907f\u514d\u5c06\u5bf9\u8c61\u5f62\u5f0f\u7684\u9884\u5904\u7406\u5668\u5b8f\u7528\u4e8e\u7b26\u53f7\u5e38\u91cf \u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u5171\u4eab\u7b26\u53f7\u5e38\u91cf\uff0c\u53ef\u4ee5 \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf \u5728\u7f16\u7a0b\u4e2d\uff0c\u5e38\u91cf\uff08constant\uff09\u6307\u7684\u662f\u4e0d\u4f1a\u6539\u53d8\u7684\u503c\u3002C++\u652f\u6301\u51e0\u79cd\u7c7b\u578b\u7684\u5e38\u91cf\uff1a const \u53d8\u91cf\uff08\u53c2\u89c1\uff1a 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr \uff09\u548c \u5b57\u9762\u91cf \uff08\u53c2\u89c1\uff1a 4.15 - \u5b57\u9762\u91cf \uff09 const \u53d8\u91cf \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u770b\u5230\u7684\u6240\u6709\u53d8\u91cf\u90fd\u662f\u975e\u5e38\u91cf\u2014\u2014\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4eec\u7684\u503c\u53ef\u4ee5\u5728\u4efb\u4f55\u65f6\u5019\u66f4\u6539(\u901a\u5e38\u901a\u8fc7\u8d4b\u503c)\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 int main () { int x { 4 }; // x is a non-constant variable x = 5 ; // change value of x to 5 using assignment operator return 0 ; } \u4e0d\u8fc7\uff0c\u6709\u65f6\u5019\u9700\u8981\u5c06\u53d8\u91cf\u5b9a\u4e49\u4e3a\u4e0d\u80fd\u6539\u53d8\u3002\u4f8b\u5982\uff0c\u5730\u7403\u7684\u5f15\u529b\u662f 9.8 meters/second^2 \uff0c\u8fd9\u4e2a\u503c\u4e0d\u592a\u53ef\u80fd\u4f1a\u968f\u65f6\u6539\u53d8\uff08\u5982\u679c\u771f\u7684\u4f1a\u968f\u65f6\u6539\u53d8\u7684\u8bdd\uff0c\u4f60\u5e94\u8be5\u62c5\u5fc3\u7684\u5c31\u4e0d\u662f C++\u4e86\uff09\u3002\u5c06\u8be5\u503c\u5b9a\u4e49\u4e3a\u5e38\u91cf\u6709\u52a9\u4e8e\u786e\u4fdd\u8be5\u503c\u4e0d\u4f1a\u610f\u5916\u66f4\u6539\u3002\u5e38\u91cf\u8fd8\u6709\u5176\u4ed6\u597d\u5904\uff0c\u6211\u4eec\u7a0d\u540e\u5c06\u63a2\u8ba8\u3002 \u503c\u4e0d\u80fd\u4fee\u6539\u7684\u53d8\u91cf\u79f0\u4e3a\u5e38\u6570\u53d8\u91cf\u3002 const \u5173\u952e\u5b57 \u5c06\u53d8\u91cf\u5b9a\u4e49\u4e3a\u5e38\u91cf\uff0c\u53ea\u9700\u8981\u5728\u7c7b\u578b\u524d\u9762\u6216\u540e\u9762\u6dfb\u52a0 const \u5173\u952e\u5b57\uff0c\u4f8b\u5982\uff1a 1 2 const double gravity { 9.8 }; // preferred use of const before type int const sidesInSquare { 4 }; // \"east const\" style, okay but not preferred \u5c3d\u7ba1 C++ \u5141\u8bb8\u4f60\u5728\u7c7b\u578b\u524d\u9762\u6216\u8005\u540e\u9762\u6dfb\u52a0 const \u5173\u952e\u5b57\uff0c\u6211\u4eec\u8fd8\u662f\u63a8\u8350\u4f60\u628a\u5b83\u653e\u5728\u7c7b\u578b\u524d\u9762\uff0c\u8fd9\u6837\u770b\u4e0a\u53bb\u66f4\u50cf\u662f\u6b63\u5e38\u7684\u82f1\u8bed\u8bed\u6cd5\uff08\u4f8b\u5982\uff1a\u201ca green ball\u201d \u800c\u4e0d\u662f \u201ca ball green\u201d\uff09\u3002 \u9898\u5916\u8bdd \u57fa\u4e8e\u7f16\u8bd1\u5668\u89e3\u6790\u590d\u6742\u58f0\u660e\u7684\u65b9\u5f0f\uff0c\u4e00\u4e9b\u5f00\u53d1\u4eba\u5458\u66f4\u559c\u6b22\u5c06 const \u653e\u5728\u7c7b\u578b\u4e4b\u540e(\u66f4\u4e00\u81f4)\u3002\u8fd9\u79cd\u98ce\u683c\u88ab\u79f0\u4e3a\u201ceast const\u201d\u3002\u867d\u7136\u8fd9\u79cd\u98ce\u683c\u6709\u4e00\u4e9b\u62e5\u62a4\u8005(\u4e5f\u6709\u4e00\u4e9b\u5408\u7406\u7684\u89c2\u70b9)\uff0c\u4f46\u5b83\u5e76\u6ca1\u6709\u5927\u53d7\u6b22\u8fce\u3002 \u6700\u4f73\u5b9e\u8df5 \u628a const \u653e\u7f6e\u5728\u7c7b\u578b\u524d\uff08\u66f4\u7b26\u5408\u4e60\u60ef\u7684\u505a\u6cd5\uff09\u3002 Const \u53d8\u91cf\u5fc5\u987b\u521d\u59cb\u5316 const \u53d8\u91cf \u5fc5\u987b \u5728\u5b9a\u4e49\u65f6\u521d\u59cb\u5316\uff0c\u6b64\u540e\u4f60\u4e5f\u4e0d\u80fd\u901a\u8fc7\u8d4b\u503c\u6765\u6539\u53d8\u5b83\uff1a 1 2 3 4 5 6 7 int main () { const double gravity ; // error: const variables must be initialized gravity = 9.9 ; // error: const variables can not be changed return 0 ; } \u6ce8\u610f\uff0cconst \u53d8\u91cf\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u7c7b\u578b\u7684\u53d8\u91cf\u521d\u59cb\u5316\uff08\u5305\u62ec\u975e const \u7c7b\u578b\u7684\u53d8\u91cf\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << \"Enter your age: \" ; int age {}; std :: cin >> age ; const int constAge { age }; // initialize const variable using non-const value age = 5 ; // ok: age is non-const, so we can change its value constAge = 6 ; // error: constAge is const, so we cannot change its value return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u7528\u975e const \u53d8\u91cf age \u521d\u59cb\u5316 const \u53d8\u91cf constAge \u3002\u56e0\u4e3a age \u4ecd\u7136\u662f\u975e const \u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u6539\u53d8\u5b83\u7684\u503c\u3002\u4f46\u662f\uff0c\u7531\u4e8e constAge \u662f const \uff0c\u6211\u4eec\u4e0d\u80fd\u5728\u521d\u59cb\u5316\u540e\u66f4\u6539\u5b83\u7684\u503c\u3002 const \u53d8\u91cf\u547d\u540d const \u53d8\u91cf\u7684\u547d\u540d\u6709\u5f88\u591a\u4e0d\u540c\u7684\u4e60\u60ef\u3002 \u4ece C \u8bed\u8a00\u8fc7\u6765\u7684\u7a0b\u5e8f\u5458\u559c\u6b22\u4f7f\u7528\u4e0b\u5212\u7ebf\u3001\u5168\u5927\u5199\u5b57\u6bcd\u7684\u65b9\u5f0f\u547d\u540d const \u53d8\u91cf \uff08\u4f8b\u5982 EARTH_GRAVITY \uff09\uff0c\u800c C++ \u4e2d\u5219\u591a\u4f7f\u7528\u5927\u5c0f\u5199\u4ea4\u66ff\u7684\u65b9\u5f0f\uff0c\u540c\u65f6\u6dfb\u52a0 k \u4f5c\u4e3a\u524d\u7f00(\u4f8b\u5982 kEarthGravity )\u3002 \u4e0d\u8fc7\uff0c\u7531\u4e8e const \u53d8\u91cf\u7684\u884c\u4e3a\u548c\u666e\u901a\u53d8\u91cf\u6ca1\u4ec0\u4e48\u533a\u522b\uff08\u9664\u4e86\u4e0d\u80fd\u8d4b\u503c\u4ee5\u5916\uff09\uff0c\u6240\u4ee5\u6ca1\u5fc5\u8981\u4e13\u95e8\u4e3a\u5b83\u4f7f\u7528\u4e00\u79cd\u7279\u6b8a\u5f62\u5f0f\u7684\u547d\u540d\u65b9\u5f0f\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5efa\u8bae\u4f7f\u7528\u548c\u975e const \u7c7b\u578b\u4e00\u6837\u7684\u53d8\u91cf\u540d\uff08\u4f8b\u5982 earthGravity \uff09\u3002 Const \u51fd\u6570\u5f62\u53c2 \u51fd\u6570\u7684 \u5f62\u53c2 \u4e5f\u53ef\u4ee5\u901a\u8fc7 const \u5173\u952e\u5b57\u5b9a\u4e49\u4e3a\u5e38\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include void printInt ( const int x ) { std :: cout << x ; } int main () { printInt ( 5 ); // 5 will be used as the initializer for x printInt ( 6 ); // 6 will be used as the initializer for x return 0 ; } \u6ce8\u610f\uff0c\u6211\u4eec\u6ca1\u6709\u4e3a const \u5f62\u53c2 x \u63d0\u4f9b\u663e\u5f0f\u7684\u521d\u59cb\u5316\u503c\u2014\u2014\u51fd\u6570\u8c03\u7528\u4e2d\u7684\u5b9e\u53c2\u503c\u5c06\u88ab\u7528\u4f5c x \u7684\u521d\u59cb\u5316\u5f0f\u3002 \u5c06\u51fd\u6570\u5f62\u53c2\u8bbe\u4e3a\u5e38\u91cf\u53ef\u4ee5\u5f97\u5230\u7f16\u8bd1\u5668\u7684\u5e2e\u52a9\uff0c\u4ee5\u786e\u4fdd\u5f62\u53c2\u7684\u503c\u4e0d\u4f1a\u5728\u51fd\u6570\u5185\u90e8\u88ab\u66f4\u6539\u3002\u7136\u800c\uff0c\u5f53\u5b9e\u53c2\u901a\u8fc7 \u6309\u503c\u4f20\u9012 \u65f6\uff0c\u6211\u4eec\u901a\u5e38\u4e0d\u5173\u5fc3\u51fd\u6570\u662f\u5426\u6539\u53d8\u4e86\u5f62\u53c2\u7684\u503c(\u56e0\u4e3a\u5b83\u53ea\u662f\u4e00\u4e2a\u526f\u672c\uff0c\u65e0\u8bba\u5982\u4f55\u90fd\u4f1a\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u9500\u6bc1)\u3002\u7531\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u6211\u4eec\u901a\u5e38\u4e0d\u4f7f\u7528 const \u5f62\u53c2\u6765\u4f20\u9012\u503c(\u56e0\u4e3a\u5b83\u4f1a\u7ed9\u6211\u4eec\u7684\u4ee3\u7801\u589e\u52a0\u6df7\u4e71\uff0c\u800c\u4e0d\u4f1a\u63d0\u4f9b\u592a\u591a\u7684\u5b9e\u9645\u4ef7\u503c)\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728 \u6309\u503c\u4f20\u9012 \u65f6\u4e0d\u8981\u4f7f\u7528 const \u3002 \u5728\u672c\u7cfb\u5217\u6559\u7a0b\u7684\u540e\u9762\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5411\u51fd\u6570\u4f20\u9012\u53c2\u6570\u7684\u53e6\u5916\u4e24\u79cd\u65b9\u6cd5\uff1a \u4f20\u5f15\u7528 \u548c \u4f20\u5730\u5740 \u3002\u5728\u4f7f\u7528\u8fd9\u4e24\u79cd\u65b9\u6cd5\u65f6\uff0c\u6b63\u786e\u4f7f\u7528 const \u975e\u5e38\u91cd\u8981\u3002 Const \u7c7b\u578b\u8fd4\u56de\u503c \u51fd\u6570\u7684\u8fd4\u56de\u503c\u540c\u6837\u53ef\u4ee5\u662f const \u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include const int getValue () { return 5 ; } int main () { std :: cout << getValue (); return 0 ; } \u7136\u800c\uff0c\u7531\u4e8e\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u526f\u672c\uff0c\u5c06\u5176\u8bbe\u4e3a const \u6ca1\u6709\u4ec0\u4e48\u610f\u4e49\u3002\u8fd4\u56de const \u503c\u8fd8\u53ef\u80fd\u59a8\u788d\u67d0\u4e9b\u7c7b\u578b\u7684\u7f16\u8bd1\u5668\u4f18\u5316\uff0c\u4ece\u800c\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e0d\u8981\u4f7f\u7528 const \u7c7b\u578b\u7684\u8fd4\u56de\u503c\u3002 \u4ec0\u4e48\u662f\u7b26\u53f7\u5e38\u91cf\uff1f \u7b26\u53f7\u5e38\u6570\u662f\u8d4b\u4e88\u5e38\u6570\u503c\u7684\u540d\u79f0\u3002\u5e38\u91cf\u53d8\u91cf\u662f\u7b26\u53f7\u5e38\u91cf\u7684\u4e00\u79cd\uff0c\u56e0\u4e3a\u53d8\u91cf\u6709\u4e00\u4e2a\u540d\u79f0(\u5b83\u7684\u6807\u8bc6\u7b26)\u548c\u4e00\u4e2a\u5e38\u91cf\u503c\u3002 \u5728 2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86 \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f \u6709\u4e24\u79cd\u5f62\u5f0f\u2014\u2014\u4e00\u79cd\u7528\u4e8e\u66ff\u6362\uff0c\u4e00\u79cd\u4e0d\u7528\u4e8e\u66ff\u6362\u3002\u8fd9\u91cc\u6211\u4eec\u4f1a\u8ba8\u8bba\u4e00\u4e0b\u7528\u4e8e\u66ff\u6362\u7684\u5b8f\uff0c\u5b83\u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 `#define` identifier substitution_text \u6bcf\u5f53 \u9884\u5904\u7406\u5668 \u9047\u5230\u8be5\u6307\u4ee4\u65f6\uff0c\u540e\u7eed\u6240\u6709 identifier \u90fd\u4f1a\u88ab\u66ff\u6362\u4e3a substitution_text \u3002\u8fd9\u91cc\u7684 identifier \u901a\u5e38\u4f1a\u4f7f\u7528\u5168\u5927\u5199\u5f62\u5f0f\u5e76\u4f7f\u7528\u4e0b\u5212\u7ebf\u4ee3\u66ff\u7a7a\u683c\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 #include #define MAX_STUDENTS_PER_CLASS 30 int main () { std :: cout << \"The class has \" << MAX_STUDENTS_PER_CLASS << \" students. \\n \" ; return 0 ; } \u5f53\u4f60\u7f16\u8bd1\u4ee3\u7801\u65f6\uff0c\u9884\u5904\u7406\u5668\u5c31\u4f1a\u628a MAX_STUDENTS_PER_CLASS \u66ff\u6362\u4e3a\u5b57\u9762\u91cf 30\uff0c\u7136\u540e\u88ab\u7f16\u8bd1\u5230\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\u3002 \u56e0\u4e3a\u5b8f\u662f\u6709\u540d\u5b57\u7684\uff0c\u800c\u4e14\u5b83\u7684\u66ff\u6362\u6587\u672c\u662f\u4e00\u4e2a\u5e38\u91cf\uff0c\u6240\u4ee5\u5b83\u4e5f\u5c5e\u4e8e\u7b26\u53f7\u5e38\u91cf\u7684\u4e00\u79cd\u3002 \u907f\u514d\u5c06\u5bf9\u8c61\u5f62\u5f0f\u7684\u9884\u5904\u7406\u5668\u5b8f\u7528\u4e8e\u7b26\u53f7\u5e38\u91cf \u6240\u4ee5\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u7528 #define \u5b9a\u4e49\u7b26\u53f7\u5e38\u91cf\u5462\uff1f\u8fd9\u91cc\u6709\uff08\u81f3\u5c11\uff09\u4e09\u4e2a\u4e3b\u8981\u95ee\u9898\u3002 \u9996\u5148\uff0c\u56e0\u4e3a\u5b8f\u7684\u89e3\u6790\u662f\u9884\u5904\u7406\u5668\u8d1f\u8d23\u7684\uff0c\u6240\u4ee5\u6240\u6709\u7684\u66ff\u6362\u90fd\u53d1\u751f\u5728\u7f16\u8bd1\u4e4b\u524d\u3002\u5f53\u4f60\u8c03\u8bd5\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u4f60\u65e0\u6cd5\u770b\u5230\u5b9e\u9645\u7684\u503c\uff08\u4f8b\u5982 30\uff09\uff0c\u800c\u53ea\u80fd\u770b\u5230\u8be5\u7b26\u53f7\u5e38\u91cf\u7684\u540d\u5b57(\u4f8b\u5982 MAX_STUDENTS_PER_CLASS )\u3002\u800c\u4e14\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u5b8f\u5b9a\u4e49\u5e76\u4e0d\u662f\u53d8\u91cf\uff0c\u6240\u4ee5\u518d\u8c03\u8bd5\u5668\u4e2d\u4f60\u6ca1\u6cd5\u5bf9\u5176\u503c\u8fdb\u884c\u76d1\u63a7\u3002 \u5982\u679c\u4f60\u60f3\u8981\u6307\u5b9a MAX_STUDENTS_PER_CLASS \u89e3\u6790\u540e\u7684\u503c\u662f\u591a\u5c11\uff0c\u4f60\u5fc5\u987b\u53d6\u627e\u5230 MAX_STUDENTS_PER_CLASS \u7684\u5b9a\u4e49\u624d\u884c(\u8be5\u5b9a\u4e49\u8fd8\u53ef\u80fd\u662f\u5728\u522b\u7684\u6587\u4ef6\u4e2d)\u3002\u8fd9\u6837\u5c31\u4f1a\u4f7f\u4f60\u7684\u7a0b\u5e8f\u96be\u4ee5\u8c03\u8bd5\u3002 \u53e6\u5916\uff0c\u5b8f\u548c\u666e\u901a\u4ee3\u7801\u53ef\u80fd\u4f1a\u4ea7\u751f\u547d\u540d\u51b2\u7a81\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 #include \"someheader.h\" #include int main () { int beta { 5 }; std :: cout << beta ; return 0 ; } \u5982\u679c someheader.h \u6070\u597d #define \u4e86\u4e00\u4e2a\u540d\u4e3a beta \u7684\u5b8f\uff0c\u90a3\u4e48\u8fd9\u4e2a\u7a0b\u5e8f\u5c31\u65e0\u6cd5\u7f16\u8bd1\uff0c\u56e0\u4e3a\u9884\u5904\u7406\u5668\u4f1a\u628a int \u53d8\u91cf\u7684\u540d\u5b57\u66ff\u6362\u6389\u3002\u901a\u5e38\uff0c\u4f7f\u7528\u5168\u5927\u5199\u7684\u5b8f\u540d\u53ef\u4ee5\u907f\u514d\u6b64\u7c7b\u95ee\u9898\uff0c\u4f46\u5e76\u65e0\u6cd5\u5b8c\u5168\u675c\u7edd\u3002 \u7b2c\u4e09\uff0c\u5b8f\u5e76\u4e0d\u9075\u5faa\u6b63\u5e38\u7684\u4f5c\u7528\u57df\u89c4\u5219\uff0c\u8fd9\u610f\u5473\u7740\u5b9a\u6781\u5c11\u6570\u60c5\u51b5\u4e0b\uff0c\u5b9a\u4e49\u5728\u51fd\u6570\u67d0\u90e8\u5206\u7684\u5b8f\u53ef\u80fd\u4f1a\u548c\u5176\u4ed6\u90e8\u5206\u7684\u4ee3\u7801\u53d1\u751f\u51b2\u7a81\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u5e38\u91cf\u800c\u4e0d\u662f\u5b8f\u66ff\u6362\u6765\u521b\u5efa\u5e38\u6570\u53d8\u91cf\u3002 \u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u5171\u7528\u7b26\u53f7\u5e38\u91cf \u5728\u5f88\u591a\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u6709\u4e9b\u7b26\u53f7\u5e38\u91cf\u9700\u8981\u88ab\u6240\u6709\u7684\u4ee3\u7801\u4f7f\u7528\uff08\u800c\u4e0d\u4ec5\u4ec5\u662f\u88ab\u5c40\u90e8\u7684\u4ee3\u7801\u4f7f\u7528\uff09\u3002\u8fd9\u4e9b\u53d8\u91cf\u53ef\u80fd\u662f\u7269\u7406\u5e38\u91cf\u6216\u6570\u5b66\u5e38\u91cf\uff08\u4f8b\u5982 \u03c0 \u6216\u963f\u4f0f\u4f3d\u5fb7\u7f57\u5e38\u6570\uff09\uff0c\u6216\u8005\u662f\u67d0\u4e2a\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u7684\u53c2\u6570\uff08\u4f8b\u5982\u6469\u64e6\u7cfb\u6570\u6216\u5f15\u529b\u7cfb\u6570\uff09\u3002\u4e0e\u5176\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u5404\u5b9a\u4e49\u4e00\u904d\u8fd9\u4e9b\u53d8\u91cf\uff0c\u4e0d\u5982\u5c06\u5b83\u4eec\u96c6\u4e2d\u5b9a\u4e49\u5728\u4e00\u4e2a\u5730\u65b9\u7136\u540e\u6309\u9700\u4f7f\u7528\u3002\u8fd9\u6837\uff0c\u4e07\u4e00\u4f60\u9700\u8981\u4fee\u6539\u5b83\u4eec\u7684\u503c\uff0c\u4f60\u53ea\u9700\u8981\u5728\u4e00\u5904\u4fee\u6539\u5373\u53ef\u3002 \u5728 C++ \u4e2d\u6709\u5f88\u591a\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u4e0a\u8ff0\u9700\u6c42\uff0c\u6211\u4eec\u4f1a\u5728 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf \u4e2d\u8fdb\u884c\u8be6\u7ec6\u7684\u4ecb\u7ecd\u3002","title":"4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf"},{"location":"4-13-Const-variables-and-symbolic-constants/#const","text":"\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u770b\u5230\u7684\u6240\u6709\u53d8\u91cf\u90fd\u662f\u975e\u5e38\u91cf\u2014\u2014\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4eec\u7684\u503c\u53ef\u4ee5\u5728\u4efb\u4f55\u65f6\u5019\u66f4\u6539(\u901a\u5e38\u901a\u8fc7\u8d4b\u503c)\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 int main () { int x { 4 }; // x is a non-constant variable x = 5 ; // change value of x to 5 using assignment operator return 0 ; } \u4e0d\u8fc7\uff0c\u6709\u65f6\u5019\u9700\u8981\u5c06\u53d8\u91cf\u5b9a\u4e49\u4e3a\u4e0d\u80fd\u6539\u53d8\u3002\u4f8b\u5982\uff0c\u5730\u7403\u7684\u5f15\u529b\u662f 9.8 meters/second^2 \uff0c\u8fd9\u4e2a\u503c\u4e0d\u592a\u53ef\u80fd\u4f1a\u968f\u65f6\u6539\u53d8\uff08\u5982\u679c\u771f\u7684\u4f1a\u968f\u65f6\u6539\u53d8\u7684\u8bdd\uff0c\u4f60\u5e94\u8be5\u62c5\u5fc3\u7684\u5c31\u4e0d\u662f C++\u4e86\uff09\u3002\u5c06\u8be5\u503c\u5b9a\u4e49\u4e3a\u5e38\u91cf\u6709\u52a9\u4e8e\u786e\u4fdd\u8be5\u503c\u4e0d\u4f1a\u610f\u5916\u66f4\u6539\u3002\u5e38\u91cf\u8fd8\u6709\u5176\u4ed6\u597d\u5904\uff0c\u6211\u4eec\u7a0d\u540e\u5c06\u63a2\u8ba8\u3002 \u503c\u4e0d\u80fd\u4fee\u6539\u7684\u53d8\u91cf\u79f0\u4e3a\u5e38\u6570\u53d8\u91cf\u3002","title":"const \u53d8\u91cf"},{"location":"4-13-Const-variables-and-symbolic-constants/#const_1","text":"\u5c06\u53d8\u91cf\u5b9a\u4e49\u4e3a\u5e38\u91cf\uff0c\u53ea\u9700\u8981\u5728\u7c7b\u578b\u524d\u9762\u6216\u540e\u9762\u6dfb\u52a0 const \u5173\u952e\u5b57\uff0c\u4f8b\u5982\uff1a 1 2 const double gravity { 9.8 }; // preferred use of const before type int const sidesInSquare { 4 }; // \"east const\" style, okay but not preferred \u5c3d\u7ba1 C++ \u5141\u8bb8\u4f60\u5728\u7c7b\u578b\u524d\u9762\u6216\u8005\u540e\u9762\u6dfb\u52a0 const \u5173\u952e\u5b57\uff0c\u6211\u4eec\u8fd8\u662f\u63a8\u8350\u4f60\u628a\u5b83\u653e\u5728\u7c7b\u578b\u524d\u9762\uff0c\u8fd9\u6837\u770b\u4e0a\u53bb\u66f4\u50cf\u662f\u6b63\u5e38\u7684\u82f1\u8bed\u8bed\u6cd5\uff08\u4f8b\u5982\uff1a\u201ca green ball\u201d \u800c\u4e0d\u662f \u201ca ball green\u201d\uff09\u3002 \u9898\u5916\u8bdd \u57fa\u4e8e\u7f16\u8bd1\u5668\u89e3\u6790\u590d\u6742\u58f0\u660e\u7684\u65b9\u5f0f\uff0c\u4e00\u4e9b\u5f00\u53d1\u4eba\u5458\u66f4\u559c\u6b22\u5c06 const \u653e\u5728\u7c7b\u578b\u4e4b\u540e(\u66f4\u4e00\u81f4)\u3002\u8fd9\u79cd\u98ce\u683c\u88ab\u79f0\u4e3a\u201ceast const\u201d\u3002\u867d\u7136\u8fd9\u79cd\u98ce\u683c\u6709\u4e00\u4e9b\u62e5\u62a4\u8005(\u4e5f\u6709\u4e00\u4e9b\u5408\u7406\u7684\u89c2\u70b9)\uff0c\u4f46\u5b83\u5e76\u6ca1\u6709\u5927\u53d7\u6b22\u8fce\u3002 \u6700\u4f73\u5b9e\u8df5 \u628a const \u653e\u7f6e\u5728\u7c7b\u578b\u524d\uff08\u66f4\u7b26\u5408\u4e60\u60ef\u7684\u505a\u6cd5\uff09\u3002","title":"const \u5173\u952e\u5b57"},{"location":"4-13-Const-variables-and-symbolic-constants/#const_2","text":"const \u53d8\u91cf \u5fc5\u987b \u5728\u5b9a\u4e49\u65f6\u521d\u59cb\u5316\uff0c\u6b64\u540e\u4f60\u4e5f\u4e0d\u80fd\u901a\u8fc7\u8d4b\u503c\u6765\u6539\u53d8\u5b83\uff1a 1 2 3 4 5 6 7 int main () { const double gravity ; // error: const variables must be initialized gravity = 9.9 ; // error: const variables can not be changed return 0 ; } \u6ce8\u610f\uff0cconst \u53d8\u91cf\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u7c7b\u578b\u7684\u53d8\u91cf\u521d\u59cb\u5316\uff08\u5305\u62ec\u975e const \u7c7b\u578b\u7684\u53d8\u91cf\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << \"Enter your age: \" ; int age {}; std :: cin >> age ; const int constAge { age }; // initialize const variable using non-const value age = 5 ; // ok: age is non-const, so we can change its value constAge = 6 ; // error: constAge is const, so we cannot change its value return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u7528\u975e const \u53d8\u91cf age \u521d\u59cb\u5316 const \u53d8\u91cf constAge \u3002\u56e0\u4e3a age \u4ecd\u7136\u662f\u975e const \u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u6539\u53d8\u5b83\u7684\u503c\u3002\u4f46\u662f\uff0c\u7531\u4e8e constAge \u662f const \uff0c\u6211\u4eec\u4e0d\u80fd\u5728\u521d\u59cb\u5316\u540e\u66f4\u6539\u5b83\u7684\u503c\u3002","title":"Const \u53d8\u91cf\u5fc5\u987b\u521d\u59cb\u5316"},{"location":"4-13-Const-variables-and-symbolic-constants/#const_3","text":"const \u53d8\u91cf\u7684\u547d\u540d\u6709\u5f88\u591a\u4e0d\u540c\u7684\u4e60\u60ef\u3002 \u4ece C \u8bed\u8a00\u8fc7\u6765\u7684\u7a0b\u5e8f\u5458\u559c\u6b22\u4f7f\u7528\u4e0b\u5212\u7ebf\u3001\u5168\u5927\u5199\u5b57\u6bcd\u7684\u65b9\u5f0f\u547d\u540d const \u53d8\u91cf \uff08\u4f8b\u5982 EARTH_GRAVITY \uff09\uff0c\u800c C++ \u4e2d\u5219\u591a\u4f7f\u7528\u5927\u5c0f\u5199\u4ea4\u66ff\u7684\u65b9\u5f0f\uff0c\u540c\u65f6\u6dfb\u52a0 k \u4f5c\u4e3a\u524d\u7f00(\u4f8b\u5982 kEarthGravity )\u3002 \u4e0d\u8fc7\uff0c\u7531\u4e8e const \u53d8\u91cf\u7684\u884c\u4e3a\u548c\u666e\u901a\u53d8\u91cf\u6ca1\u4ec0\u4e48\u533a\u522b\uff08\u9664\u4e86\u4e0d\u80fd\u8d4b\u503c\u4ee5\u5916\uff09\uff0c\u6240\u4ee5\u6ca1\u5fc5\u8981\u4e13\u95e8\u4e3a\u5b83\u4f7f\u7528\u4e00\u79cd\u7279\u6b8a\u5f62\u5f0f\u7684\u547d\u540d\u65b9\u5f0f\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5efa\u8bae\u4f7f\u7528\u548c\u975e const \u7c7b\u578b\u4e00\u6837\u7684\u53d8\u91cf\u540d\uff08\u4f8b\u5982 earthGravity \uff09\u3002","title":"const \u53d8\u91cf\u547d\u540d"},{"location":"4-13-Const-variables-and-symbolic-constants/#const_4","text":"\u51fd\u6570\u7684 \u5f62\u53c2 \u4e5f\u53ef\u4ee5\u901a\u8fc7 const \u5173\u952e\u5b57\u5b9a\u4e49\u4e3a\u5e38\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include void printInt ( const int x ) { std :: cout << x ; } int main () { printInt ( 5 ); // 5 will be used as the initializer for x printInt ( 6 ); // 6 will be used as the initializer for x return 0 ; } \u6ce8\u610f\uff0c\u6211\u4eec\u6ca1\u6709\u4e3a const \u5f62\u53c2 x \u63d0\u4f9b\u663e\u5f0f\u7684\u521d\u59cb\u5316\u503c\u2014\u2014\u51fd\u6570\u8c03\u7528\u4e2d\u7684\u5b9e\u53c2\u503c\u5c06\u88ab\u7528\u4f5c x \u7684\u521d\u59cb\u5316\u5f0f\u3002 \u5c06\u51fd\u6570\u5f62\u53c2\u8bbe\u4e3a\u5e38\u91cf\u53ef\u4ee5\u5f97\u5230\u7f16\u8bd1\u5668\u7684\u5e2e\u52a9\uff0c\u4ee5\u786e\u4fdd\u5f62\u53c2\u7684\u503c\u4e0d\u4f1a\u5728\u51fd\u6570\u5185\u90e8\u88ab\u66f4\u6539\u3002\u7136\u800c\uff0c\u5f53\u5b9e\u53c2\u901a\u8fc7 \u6309\u503c\u4f20\u9012 \u65f6\uff0c\u6211\u4eec\u901a\u5e38\u4e0d\u5173\u5fc3\u51fd\u6570\u662f\u5426\u6539\u53d8\u4e86\u5f62\u53c2\u7684\u503c(\u56e0\u4e3a\u5b83\u53ea\u662f\u4e00\u4e2a\u526f\u672c\uff0c\u65e0\u8bba\u5982\u4f55\u90fd\u4f1a\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u9500\u6bc1)\u3002\u7531\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u6211\u4eec\u901a\u5e38\u4e0d\u4f7f\u7528 const \u5f62\u53c2\u6765\u4f20\u9012\u503c(\u56e0\u4e3a\u5b83\u4f1a\u7ed9\u6211\u4eec\u7684\u4ee3\u7801\u589e\u52a0\u6df7\u4e71\uff0c\u800c\u4e0d\u4f1a\u63d0\u4f9b\u592a\u591a\u7684\u5b9e\u9645\u4ef7\u503c)\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728 \u6309\u503c\u4f20\u9012 \u65f6\u4e0d\u8981\u4f7f\u7528 const \u3002 \u5728\u672c\u7cfb\u5217\u6559\u7a0b\u7684\u540e\u9762\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5411\u51fd\u6570\u4f20\u9012\u53c2\u6570\u7684\u53e6\u5916\u4e24\u79cd\u65b9\u6cd5\uff1a \u4f20\u5f15\u7528 \u548c \u4f20\u5730\u5740 \u3002\u5728\u4f7f\u7528\u8fd9\u4e24\u79cd\u65b9\u6cd5\u65f6\uff0c\u6b63\u786e\u4f7f\u7528 const \u975e\u5e38\u91cd\u8981\u3002","title":"Const \u51fd\u6570\u5f62\u53c2"},{"location":"4-13-Const-variables-and-symbolic-constants/#const_5","text":"\u51fd\u6570\u7684\u8fd4\u56de\u503c\u540c\u6837\u53ef\u4ee5\u662f const \u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include const int getValue () { return 5 ; } int main () { std :: cout << getValue (); return 0 ; } \u7136\u800c\uff0c\u7531\u4e8e\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u526f\u672c\uff0c\u5c06\u5176\u8bbe\u4e3a const \u6ca1\u6709\u4ec0\u4e48\u610f\u4e49\u3002\u8fd4\u56de const \u503c\u8fd8\u53ef\u80fd\u59a8\u788d\u67d0\u4e9b\u7c7b\u578b\u7684\u7f16\u8bd1\u5668\u4f18\u5316\uff0c\u4ece\u800c\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e0d\u8981\u4f7f\u7528 const \u7c7b\u578b\u7684\u8fd4\u56de\u503c\u3002","title":"Const \u7c7b\u578b\u8fd4\u56de\u503c"},{"location":"4-13-Const-variables-and-symbolic-constants/#_1","text":"\u7b26\u53f7\u5e38\u6570\u662f\u8d4b\u4e88\u5e38\u6570\u503c\u7684\u540d\u79f0\u3002\u5e38\u91cf\u53d8\u91cf\u662f\u7b26\u53f7\u5e38\u91cf\u7684\u4e00\u79cd\uff0c\u56e0\u4e3a\u53d8\u91cf\u6709\u4e00\u4e2a\u540d\u79f0(\u5b83\u7684\u6807\u8bc6\u7b26)\u548c\u4e00\u4e2a\u5e38\u91cf\u503c\u3002 \u5728 2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86 \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f \u6709\u4e24\u79cd\u5f62\u5f0f\u2014\u2014\u4e00\u79cd\u7528\u4e8e\u66ff\u6362\uff0c\u4e00\u79cd\u4e0d\u7528\u4e8e\u66ff\u6362\u3002\u8fd9\u91cc\u6211\u4eec\u4f1a\u8ba8\u8bba\u4e00\u4e0b\u7528\u4e8e\u66ff\u6362\u7684\u5b8f\uff0c\u5b83\u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 `#define` identifier substitution_text \u6bcf\u5f53 \u9884\u5904\u7406\u5668 \u9047\u5230\u8be5\u6307\u4ee4\u65f6\uff0c\u540e\u7eed\u6240\u6709 identifier \u90fd\u4f1a\u88ab\u66ff\u6362\u4e3a substitution_text \u3002\u8fd9\u91cc\u7684 identifier \u901a\u5e38\u4f1a\u4f7f\u7528\u5168\u5927\u5199\u5f62\u5f0f\u5e76\u4f7f\u7528\u4e0b\u5212\u7ebf\u4ee3\u66ff\u7a7a\u683c\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 #include #define MAX_STUDENTS_PER_CLASS 30 int main () { std :: cout << \"The class has \" << MAX_STUDENTS_PER_CLASS << \" students. \\n \" ; return 0 ; } \u5f53\u4f60\u7f16\u8bd1\u4ee3\u7801\u65f6\uff0c\u9884\u5904\u7406\u5668\u5c31\u4f1a\u628a MAX_STUDENTS_PER_CLASS \u66ff\u6362\u4e3a\u5b57\u9762\u91cf 30\uff0c\u7136\u540e\u88ab\u7f16\u8bd1\u5230\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\u3002 \u56e0\u4e3a\u5b8f\u662f\u6709\u540d\u5b57\u7684\uff0c\u800c\u4e14\u5b83\u7684\u66ff\u6362\u6587\u672c\u662f\u4e00\u4e2a\u5e38\u91cf\uff0c\u6240\u4ee5\u5b83\u4e5f\u5c5e\u4e8e\u7b26\u53f7\u5e38\u91cf\u7684\u4e00\u79cd\u3002","title":"\u4ec0\u4e48\u662f\u7b26\u53f7\u5e38\u91cf\uff1f"},{"location":"4-13-Const-variables-and-symbolic-constants/#_2","text":"\u6240\u4ee5\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u7528 #define \u5b9a\u4e49\u7b26\u53f7\u5e38\u91cf\u5462\uff1f\u8fd9\u91cc\u6709\uff08\u81f3\u5c11\uff09\u4e09\u4e2a\u4e3b\u8981\u95ee\u9898\u3002 \u9996\u5148\uff0c\u56e0\u4e3a\u5b8f\u7684\u89e3\u6790\u662f\u9884\u5904\u7406\u5668\u8d1f\u8d23\u7684\uff0c\u6240\u4ee5\u6240\u6709\u7684\u66ff\u6362\u90fd\u53d1\u751f\u5728\u7f16\u8bd1\u4e4b\u524d\u3002\u5f53\u4f60\u8c03\u8bd5\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u4f60\u65e0\u6cd5\u770b\u5230\u5b9e\u9645\u7684\u503c\uff08\u4f8b\u5982 30\uff09\uff0c\u800c\u53ea\u80fd\u770b\u5230\u8be5\u7b26\u53f7\u5e38\u91cf\u7684\u540d\u5b57(\u4f8b\u5982 MAX_STUDENTS_PER_CLASS )\u3002\u800c\u4e14\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u5b8f\u5b9a\u4e49\u5e76\u4e0d\u662f\u53d8\u91cf\uff0c\u6240\u4ee5\u518d\u8c03\u8bd5\u5668\u4e2d\u4f60\u6ca1\u6cd5\u5bf9\u5176\u503c\u8fdb\u884c\u76d1\u63a7\u3002 \u5982\u679c\u4f60\u60f3\u8981\u6307\u5b9a MAX_STUDENTS_PER_CLASS \u89e3\u6790\u540e\u7684\u503c\u662f\u591a\u5c11\uff0c\u4f60\u5fc5\u987b\u53d6\u627e\u5230 MAX_STUDENTS_PER_CLASS \u7684\u5b9a\u4e49\u624d\u884c(\u8be5\u5b9a\u4e49\u8fd8\u53ef\u80fd\u662f\u5728\u522b\u7684\u6587\u4ef6\u4e2d)\u3002\u8fd9\u6837\u5c31\u4f1a\u4f7f\u4f60\u7684\u7a0b\u5e8f\u96be\u4ee5\u8c03\u8bd5\u3002 \u53e6\u5916\uff0c\u5b8f\u548c\u666e\u901a\u4ee3\u7801\u53ef\u80fd\u4f1a\u4ea7\u751f\u547d\u540d\u51b2\u7a81\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 #include \"someheader.h\" #include int main () { int beta { 5 }; std :: cout << beta ; return 0 ; } \u5982\u679c someheader.h \u6070\u597d #define \u4e86\u4e00\u4e2a\u540d\u4e3a beta \u7684\u5b8f\uff0c\u90a3\u4e48\u8fd9\u4e2a\u7a0b\u5e8f\u5c31\u65e0\u6cd5\u7f16\u8bd1\uff0c\u56e0\u4e3a\u9884\u5904\u7406\u5668\u4f1a\u628a int \u53d8\u91cf\u7684\u540d\u5b57\u66ff\u6362\u6389\u3002\u901a\u5e38\uff0c\u4f7f\u7528\u5168\u5927\u5199\u7684\u5b8f\u540d\u53ef\u4ee5\u907f\u514d\u6b64\u7c7b\u95ee\u9898\uff0c\u4f46\u5e76\u65e0\u6cd5\u5b8c\u5168\u675c\u7edd\u3002 \u7b2c\u4e09\uff0c\u5b8f\u5e76\u4e0d\u9075\u5faa\u6b63\u5e38\u7684\u4f5c\u7528\u57df\u89c4\u5219\uff0c\u8fd9\u610f\u5473\u7740\u5b9a\u6781\u5c11\u6570\u60c5\u51b5\u4e0b\uff0c\u5b9a\u4e49\u5728\u51fd\u6570\u67d0\u90e8\u5206\u7684\u5b8f\u53ef\u80fd\u4f1a\u548c\u5176\u4ed6\u90e8\u5206\u7684\u4ee3\u7801\u53d1\u751f\u51b2\u7a81\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u5e38\u91cf\u800c\u4e0d\u662f\u5b8f\u66ff\u6362\u6765\u521b\u5efa\u5e38\u6570\u53d8\u91cf\u3002","title":"\u907f\u514d\u5c06\u5bf9\u8c61\u5f62\u5f0f\u7684\u9884\u5904\u7406\u5668\u5b8f\u7528\u4e8e\u7b26\u53f7\u5e38\u91cf"},{"location":"4-13-Const-variables-and-symbolic-constants/#_3","text":"\u5728\u5f88\u591a\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u6709\u4e9b\u7b26\u53f7\u5e38\u91cf\u9700\u8981\u88ab\u6240\u6709\u7684\u4ee3\u7801\u4f7f\u7528\uff08\u800c\u4e0d\u4ec5\u4ec5\u662f\u88ab\u5c40\u90e8\u7684\u4ee3\u7801\u4f7f\u7528\uff09\u3002\u8fd9\u4e9b\u53d8\u91cf\u53ef\u80fd\u662f\u7269\u7406\u5e38\u91cf\u6216\u6570\u5b66\u5e38\u91cf\uff08\u4f8b\u5982 \u03c0 \u6216\u963f\u4f0f\u4f3d\u5fb7\u7f57\u5e38\u6570\uff09\uff0c\u6216\u8005\u662f\u67d0\u4e2a\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u7684\u53c2\u6570\uff08\u4f8b\u5982\u6469\u64e6\u7cfb\u6570\u6216\u5f15\u529b\u7cfb\u6570\uff09\u3002\u4e0e\u5176\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u5404\u5b9a\u4e49\u4e00\u904d\u8fd9\u4e9b\u53d8\u91cf\uff0c\u4e0d\u5982\u5c06\u5b83\u4eec\u96c6\u4e2d\u5b9a\u4e49\u5728\u4e00\u4e2a\u5730\u65b9\u7136\u540e\u6309\u9700\u4f7f\u7528\u3002\u8fd9\u6837\uff0c\u4e07\u4e00\u4f60\u9700\u8981\u4fee\u6539\u5b83\u4eec\u7684\u503c\uff0c\u4f60\u53ea\u9700\u8981\u5728\u4e00\u5904\u4fee\u6539\u5373\u53ef\u3002 \u5728 C++ \u4e2d\u6709\u5f88\u591a\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u4e0a\u8ff0\u9700\u6c42\uff0c\u6211\u4eec\u4f1a\u5728 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf \u4e2d\u8fdb\u884c\u8be6\u7ec6\u7684\u4ecb\u7ecd\u3002","title":"\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u5171\u7528\u7b26\u53f7\u5e38\u91cf"},{"location":"4-14-Compile-time-constants-constant-expressions-and-constexpr/","tags":["const","constexpr"],"text":"Key Takeaway \u8003\u8651\u4e0b\u9762\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << 3 + 4 ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 7 \u4e0d\u8fc7\uff0c\u8fd9\u91cc\u5305\u542b\u4e86\u4e00\u4e2a\u4e0d\u6613\u88ab\u53d1\u73b0\u7684\u4f18\u5316\u70b9\u3002 \u5982\u679c\u5bf9\u4e0a\u8ff0\u6587\u4ef6\u539f\u5c01\u4e0d\u52a8\u5730\u8fdb\u884c\u7f16\u8bd1\uff0c\u7f16\u8bd1\u5668\u5c06\u751f\u6210\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u5e76\u5728\u8fd0\u884c\u65f6(\u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6)\u8ba1\u7b97 3 + 4 \u7684\u7ed3\u679c\u3002\u5982\u679c\u7a0b\u5e8f\u88ab\u6267\u884c 100 \u4e07\u6b21\uff0c3 + 4 \u5c06\u88ab\u8ba1\u7b97 100 \u4e07\u6b21\uff0c7 \u7684\u7ed3\u679c\u503c\u5c06\u4ea7\u751f 100 \u4e07\u6b21\u3002\u4f46\u662f\u8bf7\u6ce8\u610f\uff0c3 + 4 \u7684\u7ed3\u679c\u6c38\u8fdc\u4e0d\u4f1a\u6539\u53d8\u2014\u2014\u5b83\u603b\u662f 7\u3002\u56e0\u6b64\uff0c\u6bcf\u6b21\u7a0b\u5e8f\u8fd0\u884c\u65f6\u91cd\u65b0\u8ba1\u7b97 3 + 4 \u662f\u4e00\u79cd\u6d6a\u8d39\u3002 \u5e38\u91cf\u8868\u8fbe\u5f0f \u5e38\u91cf\u8868\u8fbe\u5f0f\u662f\u53ef\u4ee5\u7531\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\u8868\u8fbe\u5f0f\u3002\u8981\u6210\u4e3a\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u8868\u8fbe\u5f0f\u4e2d\u7684\u6240\u6709\u503c\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u5df2\u77e5(\u8c03\u7528\u7684\u6240\u6709\u64cd\u4f5c\u7b26\u548c\u51fd\u6570\u5fc5\u987b\u652f\u6301\u7f16\u8bd1\u65f6\u6c42\u503c)\u3002 \u5f53\u7f16\u8bd1\u5668\u9047\u5230\u5e38\u91cf\u8868\u8fbe\u5f0f\u65f6\uff0c\u5b83\u5c06\u7528\u8be5\u5e38\u91cf\u8868\u8fbe\u5f0f\u7684\u6c42\u503c\u7ed3\u679c\u66ff\u6362\u8be5\u5e38\u91cf\u8868\u8fbe\u5f0f\u3002 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u8868\u8fbe\u5f0f 3 + 4 \u662f\u4e00\u4e2a\u5e38\u91cf\u8868\u8fbe\u5f0f\u3002\u56e0\u6b64\uff0c\u5f53\u8fd9\u4e2a\u7a0b\u5e8f\u88ab\u7f16\u8bd1\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u8ba1\u7b97\u5e38\u6570\u8868\u8fbe\u5f0f 3 + 4 \uff0c\u7136\u540e\u5c06\u5e38\u6570\u8868\u8fbe\u5f0f 3 + 4 \u66ff\u6362\u4e3a\u7ed3\u679c\u503c 7 \u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u7f16\u8bd1\u5668\u5b9e\u9645\u4e0a\u7f16\u8bd1\u7684\u662f\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << 7 ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u4ea7\u751f\u76f8\u540c\u7684\u8f93\u51fa( 7 )\uff0c\u4f46\u6700\u7ec8\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4e0d\u518d\u9700\u8981\u5728\u8fd0\u884c\u65f6\u82b1\u8d39 CPU \u5468\u671f\u8ba1\u7b97 3 + 4 ! \u6ce8\u610f\uff0c\u66ff\u6362\u540e\u7684\u8868\u8fbe\u5f0f std:: cout << 7 \u4e0d\u662f\u4e00\u4e2a\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u56e0\u4e3a\u6211\u4eec\u7684\u7a0b\u5e8f\u4e0d\u80fd\u5728\u7f16\u8bd1\u65f6\u5c06\u503c\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002\u6240\u4ee5\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4f1a\u5728\u8fd0\u884c\u65f6\u6c42\u503c\u3002 \u5173\u952e\u4fe1\u606f \u5728\u7f16\u8bd1\u65f6\u5bf9\u5e38\u91cf\u8868\u8fbe\u5f0f\u6c42\u503c\u4f1a\u4f7f\u7f16\u8bd1\u82b1\u8d39\u66f4\u957f\u7684\u65f6\u95f4(\u56e0\u4e3a\u7f16\u8bd1\u5668\u5fc5\u987b\u505a\u66f4\u591a\u7684\u5de5\u4f5c)\uff0c\u4f46\u662f\u8fd9\u6837\u7684\u8868\u8fbe\u5f0f\u53ea\u9700\u8981\u6c42\u503c\u4e00\u6b21(\u800c\u4e0d\u662f\u6bcf\u6b21\u7a0b\u5e8f\u8fd0\u884c\u65f6)\u3002\u5f97\u5230\u7684\u53ef\u6267\u884c\u6587\u4ef6\u901f\u5ea6\u66f4\u5feb\uff0c\u4f7f\u7528\u7684\u5185\u5b58\u66f4\u5c11\u3002 \u7f16\u8bd1\u65f6\u5e38\u6570 \u7f16\u8bd1\u65f6\u5e38\u6570\u662f\u4e00\u4e2a\u5728\u7f16\u8bd1\u65f6\u5176\u503c\u5df2\u77e5\u7684\u5e38\u6570\u3002\u5b57\u9762\u91cf(\u4f8b\u5982 1 \uff0c 2.3 \u548c \"Hello, world!\" )\u662f\u4e00\u79cd\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 \u90a3\u4e48 const \u53d8\u91cf\u5462\uff1f const \u53d8\u91cf\u53ef\u80fd\u662f\u4e5f\u53ef\u80fd\u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 \u7f16\u8bd1\u65f6\u5e38\u91cf( const ) \u5982\u679c const \u53d8\u91cf\u7684\u521d\u59cb\u5316\u503c\u662f\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u90a3\u4e48\u5b83\u5c31\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 \u8003\u8651\u4e00\u4e2a\u4e0e\u4e0a\u9762\u7c7b\u4f3c\u7684\u4f7f\u7528 const \u53d8\u91cf\u7684\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 #include int main () { const int x { 3 }; // x \u662f\u7f16\u8bd1\u65f6\u5e38\u91cf const int y { 4 }; // y \u662f\u7f16\u8bd1\u65f6\u5e38\u91cf std :: cout << x + y ; // x + y \u662f\u7f16\u8bd1\u65f6\u5e38\u91cf return 0 ; } \u56e0\u4e3a x \u548c y \u7684\u521d\u59cb\u5316\u503c\u662f\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u6240\u4ee5 x \u548c y \u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u8fd9\u610f\u5473\u7740 x + y \u4e5f\u662f\u4e00\u4e2a\u5e38\u6570\u8868\u8fbe\u5f0f\u3002\u56e0\u6b64\uff0c\u5f53\u7f16\u8bd1\u5668\u7f16\u8bd1\u8fd9\u4e2a\u7a0b\u5e8f\u65f6\uff0c\u5b83\u53ef\u4ee5\u8ba1\u7b97 x + y \u7684\u503c\uff0c\u5e76\u5c06\u5e38\u91cf\u8868\u8fbe\u5f0f\u66ff\u6362\u4e3a\u7ed3\u679c\u5b57\u9762\u503c 7 \u3002 \u8bf7\u6ce8\u610f\uff0c\u7f16\u8bd1\u65f6 const \u7684\u521d\u59cb\u5316\u5f0f\u53ef\u4ee5\u662f\u4efb\u4f55\u5e38\u91cf\u8868\u8fbe\u5f0f\u3002\u4ee5\u4e0b\u4e24\u4e2a\u90fd\u662f\u7f16\u8bd1\u65f6\u7684 const \u53d8\u91cf: 1 2 const int z { 1 + 2 }; const int w { z * 2 }; \u7f16\u8bd1\u65f6\u7684 const \u53d8\u91cf\u901a\u5e38\u88ab\u7528\u4f5c\u7b26\u53f7\u5e38\u91cf: 1 const double gravity { 9.8 }; \u7f16\u8bd1\u65f6\u5e38\u91cf\u4f7f\u7f16\u8bd1\u5668\u80fd\u591f\u6267\u884c\u975e\u7f16\u8bd1\u65f6\u5e38\u91cf\u65e0\u6cd5\u63d0\u4f9b\u7684\u4f18\u5316\u3002\u4f8b\u5982\uff0c\u6bcf\u5f53\u4f7f\u7528 gravity \u65f6\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u7b80\u5355\u5730\u7528\u53cc\u7cbe\u5ea6\u5b57\u9762\u503c 9.8 \u66ff\u6362\u6807\u8bc6\u7b26 gravity \uff0c\u8fd9\u5c31\u907f\u514d\u4e86\u5fc5\u987b\u4ece\u5185\u5b58\u4e2d\u67d0\u5904\u83b7\u53d6\u503c\u3002 \u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u65f6\u5e38\u91cf\u5c06\u88ab \u4f18\u5316\u6389 \u3002\u5728\u65e0\u6cd5\u5b9e\u73b0\u7684\u60c5\u51b5\u4e0b(\u6216\u5f53\u4f18\u5316\u5173\u95ed\u65f6)\uff0c\u53d8\u91cf\u4ecd\u7136\u4f1a\u5728\u8fd0\u884c\u65f6\u521b\u5efa(\u548c\u521d\u59cb\u5316)\u3002 \u8fd0\u884c\u65f6\u5e38\u91cf( const ) \u4efb\u4f55\u7528\u975e\u5e38\u91cf\u8868\u8fbe\u5f0f\u521d\u59cb\u5316\u7684 const \u53d8\u91cf\u90fd\u662f\u8fd0\u884c\u65f6\u5e38\u91cf\u3002\u8fd0\u884c\u65f6\u5e38\u91cf\u662f\u5728\u8fd0\u884c\u65f6\u624d\u77e5\u9053\u5176\u521d\u59cb\u5316\u503c\u7684\u5e38\u91cf\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u6f14\u793a\u4e86\u8fd0\u884c\u65f6\u5e38\u91cf\u7684\u7528\u6cd5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int getNumber () { std :: cout << \"Enter a number: \" ; int y {}; std :: cin >> y ; return y ; } int main () { const int x { 3 }; // x is a compile time constant std :: cout << \"Enter a number: \" ; const int y { getNumber () }; // y is a runtime constant std :: cout << x + y ; // x + y is a runtime expression return 0 ; } \u5373\u4f7f y \u662f const \uff0c\u5176\u521d\u59cb\u5316\u503c( getNumber() \u7684\u8fd4\u56de\u503c)\u8981\u5230\u8fd0\u884c\u65f6\u624d\u77e5\u9053\u3002\u56e0\u6b64\uff0c y \u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\u5e38\u91cf\uff0c\u800c\u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u56e0\u6b64\uff0c\u8868\u8fbe\u5f0f x + y \u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\u8868\u8fbe\u5f0f\u3002 constexpr \u5173\u952e\u5b57 \u5f53\u58f0\u660e const \u53d8\u91cf\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u9690\u5f0f\u5730\u8ddf\u8e2a\u5b83\u662f\u8fd0\u884c\u65f6\u5e38\u91cf\u8fd8\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u9664\u4e86\u4f18\u5316\u76ee\u7684\u4e4b\u5916\uff0c\u662f\u8fd0\u884c\u65f6\u5e38\u91cf\u8fd8\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u5e76\u4e0d\u91cd\u8981\uff0c\u4f46\u4e5f\u6709\u4e00\u4e9b\u5947\u602a\u7684\u60c5\u51b5\u4e0b\uff0cC++\u9700\u8981\u7f16\u8bd1\u65f6\u5e38\u91cf\u800c\u4e0d\u662f\u8fd0\u884c\u65f6\u5e38\u91cf(\u6211\u4eec\u5c06\u5728\u7a0d\u540e\u4ecb\u7ecd\u8fd9\u4e9b\u4e3b\u9898\u65f6\u8ba8\u8bba\u8fd9\u4e9b\u60c5\u51b5)\u3002 \u56e0\u4e3a\u7f16\u8bd1\u65f6\u5e38\u91cf\u901a\u5e38\u5141\u8bb8\u66f4\u597d\u7684\u4f18\u5316(\u5e76\u4e14\u51e0\u4e4e\u6ca1\u6709\u7f3a\u70b9)\uff0c\u6240\u4ee5\u6211\u4eec\u901a\u5e38\u5e0c\u671b\u5c3d\u53ef\u80fd\u4f7f\u7528\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 \u5f53\u4f7f\u7528 const \u65f6\uff0c\u53d8\u91cf\u6700\u7ec8\u53ef\u80fd\u662f\u7f16\u8bd1\u65f6\u7684 const \u6216\u8fd0\u884c\u65f6\u7684 const \uff0c\u8fd9\u53d6\u51b3\u4e8e\u521d\u59cb\u5316\u5f0f\u662f\u5426\u662f\u7f16\u8bd1\u65f6\u8868\u8fbe\u5f0f\u3002\u56e0\u4e3a\u4e24\u8005\u7684\u5b9a\u4e49\u770b\u8d77\u6765\u5b8c\u5168\u76f8\u540c\uff0c\u6240\u4ee5\u6211\u4eec\u6700\u7ec8\u5f97\u5230\u7684\u53ef\u80fd\u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\u5e38\u91cf\uff0c\u800c\u6211\u4eec\u539f\u4ee5\u4e3a\u5f97\u5230\u7684\u662f\u4e00\u4e2a\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u5728\u524d\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f88\u96be\u533a\u5206 y \u662f\u7f16\u8bd1\u65f6\u7684 const \u8fd8\u662f\u8fd0\u884c\u65f6\u7684 const \u2014\u2014\u6211\u4eec\u5fc5\u987b\u67e5\u770b getNumber() \u7684\u8fd4\u56de\u503c\u6765\u786e\u5b9a\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u7f16\u8bd1\u5668\u7684\u5e2e\u52a9\uff0c\u4ee5\u786e\u4fdd\u5728\u9884\u671f\u7684\u5730\u65b9\u5f97\u5230\u7f16\u8bd1\u65f6\u7684 const \u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5728\u53d8\u91cf\u58f0\u660e\u4e2d\u4f7f\u7528 constexpr \u800c\u4e0d\u662f const \u3002 constexpr (\u201c\u5e38\u91cf\u8868\u8fbe\u5f0f\u201d\u7684\u7f29\u5199)\u53d8\u91cf\u53ea\u80fd\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u5982\u679c constexpr \u53d8\u91cf\u7684\u521d\u59cb\u5316\u503c\u4e0d\u662f\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u7f16\u8bd1\u5668\u5c06\u51fa\u9519\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { constexpr double gravity { 9.8 }; // ok: 9.8 is a constant expression constexpr int sum { 4 + 5 }; // ok: 4 + 5 is a constant expression constexpr int something { sum }; // ok: sum is a constant expression std :: cout << \"Enter your age: \" ; int age {}; std :: cin >> age ; constexpr int myAge { age }; // compile error: age is not a constant expression return 0 ; } \u6700\u4f73\u5b9e\u8df5 \u4efb\u4f55\u5728\u521d\u59cb\u5316\u540e\u503c\u5c31\u4e0d\u80fd\u88ab\u6539\u53d8\uff0c\u4e14\u521d\u59cb\u5316\u503c\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u786e\u5b9a\u7684\u53d8\u91cf\uff0c\u90fd\u5fc5\u987b\u58f0\u660e\u4e3a constexpr \u3002 \u4efb\u4f55\u5728\u521d\u59cb\u5316\u540e\u503c\u5c31\u4e0d\u80fd\u88ab\u6539\u53d8\uff0c\u4f46\u662f\u521d\u59cb\u5316\u503c\u4e0d\u80fd\u5728\u7f16\u8bd1\u65f6\u786e\u5b9a\u7684\u53d8\u91cf\uff0c\u90fd\u5e94\u8be5\u58f0\u660e\u4e3a const \u3002 Although function parameters can be const , function parameters cannot be constexpr . \u76f8\u5173\u5185\u5bb9 C++ \u4e5f\u652f\u6301\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\u51fd\u6570(\u56e0\u6b64\u53ef\u4ee5\u5728\u5e38\u91cf\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528)\u2014\u2014\u6211\u4eec\u4f1a\u5728 6.14 - Constexpr \u548c consteval \u51fd\u6570 \u4e2d\u8ba8\u8bba\u8fd9\u4e9b\u51fd\u6570\u3002","title":"4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr"},{"location":"4-14-Compile-time-constants-constant-expressions-and-constexpr/#_1","text":"\u5e38\u91cf\u8868\u8fbe\u5f0f\u662f\u53ef\u4ee5\u7531\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\u8868\u8fbe\u5f0f\u3002\u8981\u6210\u4e3a\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u8868\u8fbe\u5f0f\u4e2d\u7684\u6240\u6709\u503c\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u5df2\u77e5(\u8c03\u7528\u7684\u6240\u6709\u64cd\u4f5c\u7b26\u548c\u51fd\u6570\u5fc5\u987b\u652f\u6301\u7f16\u8bd1\u65f6\u6c42\u503c)\u3002 \u5f53\u7f16\u8bd1\u5668\u9047\u5230\u5e38\u91cf\u8868\u8fbe\u5f0f\u65f6\uff0c\u5b83\u5c06\u7528\u8be5\u5e38\u91cf\u8868\u8fbe\u5f0f\u7684\u6c42\u503c\u7ed3\u679c\u66ff\u6362\u8be5\u5e38\u91cf\u8868\u8fbe\u5f0f\u3002 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u8868\u8fbe\u5f0f 3 + 4 \u662f\u4e00\u4e2a\u5e38\u91cf\u8868\u8fbe\u5f0f\u3002\u56e0\u6b64\uff0c\u5f53\u8fd9\u4e2a\u7a0b\u5e8f\u88ab\u7f16\u8bd1\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u8ba1\u7b97\u5e38\u6570\u8868\u8fbe\u5f0f 3 + 4 \uff0c\u7136\u540e\u5c06\u5e38\u6570\u8868\u8fbe\u5f0f 3 + 4 \u66ff\u6362\u4e3a\u7ed3\u679c\u503c 7 \u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u7f16\u8bd1\u5668\u5b9e\u9645\u4e0a\u7f16\u8bd1\u7684\u662f\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << 7 ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u4ea7\u751f\u76f8\u540c\u7684\u8f93\u51fa( 7 )\uff0c\u4f46\u6700\u7ec8\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4e0d\u518d\u9700\u8981\u5728\u8fd0\u884c\u65f6\u82b1\u8d39 CPU \u5468\u671f\u8ba1\u7b97 3 + 4 ! \u6ce8\u610f\uff0c\u66ff\u6362\u540e\u7684\u8868\u8fbe\u5f0f std:: cout << 7 \u4e0d\u662f\u4e00\u4e2a\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u56e0\u4e3a\u6211\u4eec\u7684\u7a0b\u5e8f\u4e0d\u80fd\u5728\u7f16\u8bd1\u65f6\u5c06\u503c\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002\u6240\u4ee5\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4f1a\u5728\u8fd0\u884c\u65f6\u6c42\u503c\u3002 \u5173\u952e\u4fe1\u606f \u5728\u7f16\u8bd1\u65f6\u5bf9\u5e38\u91cf\u8868\u8fbe\u5f0f\u6c42\u503c\u4f1a\u4f7f\u7f16\u8bd1\u82b1\u8d39\u66f4\u957f\u7684\u65f6\u95f4(\u56e0\u4e3a\u7f16\u8bd1\u5668\u5fc5\u987b\u505a\u66f4\u591a\u7684\u5de5\u4f5c)\uff0c\u4f46\u662f\u8fd9\u6837\u7684\u8868\u8fbe\u5f0f\u53ea\u9700\u8981\u6c42\u503c\u4e00\u6b21(\u800c\u4e0d\u662f\u6bcf\u6b21\u7a0b\u5e8f\u8fd0\u884c\u65f6)\u3002\u5f97\u5230\u7684\u53ef\u6267\u884c\u6587\u4ef6\u901f\u5ea6\u66f4\u5feb\uff0c\u4f7f\u7528\u7684\u5185\u5b58\u66f4\u5c11\u3002","title":"\u5e38\u91cf\u8868\u8fbe\u5f0f"},{"location":"4-14-Compile-time-constants-constant-expressions-and-constexpr/#_2","text":"\u7f16\u8bd1\u65f6\u5e38\u6570\u662f\u4e00\u4e2a\u5728\u7f16\u8bd1\u65f6\u5176\u503c\u5df2\u77e5\u7684\u5e38\u6570\u3002\u5b57\u9762\u91cf(\u4f8b\u5982 1 \uff0c 2.3 \u548c \"Hello, world!\" )\u662f\u4e00\u79cd\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 \u90a3\u4e48 const \u53d8\u91cf\u5462\uff1f const \u53d8\u91cf\u53ef\u80fd\u662f\u4e5f\u53ef\u80fd\u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002","title":"\u7f16\u8bd1\u65f6\u5e38\u6570"},{"location":"4-14-Compile-time-constants-constant-expressions-and-constexpr/#const","text":"\u5982\u679c const \u53d8\u91cf\u7684\u521d\u59cb\u5316\u503c\u662f\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u90a3\u4e48\u5b83\u5c31\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 \u8003\u8651\u4e00\u4e2a\u4e0e\u4e0a\u9762\u7c7b\u4f3c\u7684\u4f7f\u7528 const \u53d8\u91cf\u7684\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 #include int main () { const int x { 3 }; // x \u662f\u7f16\u8bd1\u65f6\u5e38\u91cf const int y { 4 }; // y \u662f\u7f16\u8bd1\u65f6\u5e38\u91cf std :: cout << x + y ; // x + y \u662f\u7f16\u8bd1\u65f6\u5e38\u91cf return 0 ; } \u56e0\u4e3a x \u548c y \u7684\u521d\u59cb\u5316\u503c\u662f\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u6240\u4ee5 x \u548c y \u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u8fd9\u610f\u5473\u7740 x + y \u4e5f\u662f\u4e00\u4e2a\u5e38\u6570\u8868\u8fbe\u5f0f\u3002\u56e0\u6b64\uff0c\u5f53\u7f16\u8bd1\u5668\u7f16\u8bd1\u8fd9\u4e2a\u7a0b\u5e8f\u65f6\uff0c\u5b83\u53ef\u4ee5\u8ba1\u7b97 x + y \u7684\u503c\uff0c\u5e76\u5c06\u5e38\u91cf\u8868\u8fbe\u5f0f\u66ff\u6362\u4e3a\u7ed3\u679c\u5b57\u9762\u503c 7 \u3002 \u8bf7\u6ce8\u610f\uff0c\u7f16\u8bd1\u65f6 const \u7684\u521d\u59cb\u5316\u5f0f\u53ef\u4ee5\u662f\u4efb\u4f55\u5e38\u91cf\u8868\u8fbe\u5f0f\u3002\u4ee5\u4e0b\u4e24\u4e2a\u90fd\u662f\u7f16\u8bd1\u65f6\u7684 const \u53d8\u91cf: 1 2 const int z { 1 + 2 }; const int w { z * 2 }; \u7f16\u8bd1\u65f6\u7684 const \u53d8\u91cf\u901a\u5e38\u88ab\u7528\u4f5c\u7b26\u53f7\u5e38\u91cf: 1 const double gravity { 9.8 }; \u7f16\u8bd1\u65f6\u5e38\u91cf\u4f7f\u7f16\u8bd1\u5668\u80fd\u591f\u6267\u884c\u975e\u7f16\u8bd1\u65f6\u5e38\u91cf\u65e0\u6cd5\u63d0\u4f9b\u7684\u4f18\u5316\u3002\u4f8b\u5982\uff0c\u6bcf\u5f53\u4f7f\u7528 gravity \u65f6\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u7b80\u5355\u5730\u7528\u53cc\u7cbe\u5ea6\u5b57\u9762\u503c 9.8 \u66ff\u6362\u6807\u8bc6\u7b26 gravity \uff0c\u8fd9\u5c31\u907f\u514d\u4e86\u5fc5\u987b\u4ece\u5185\u5b58\u4e2d\u67d0\u5904\u83b7\u53d6\u503c\u3002 \u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u65f6\u5e38\u91cf\u5c06\u88ab \u4f18\u5316\u6389 \u3002\u5728\u65e0\u6cd5\u5b9e\u73b0\u7684\u60c5\u51b5\u4e0b(\u6216\u5f53\u4f18\u5316\u5173\u95ed\u65f6)\uff0c\u53d8\u91cf\u4ecd\u7136\u4f1a\u5728\u8fd0\u884c\u65f6\u521b\u5efa(\u548c\u521d\u59cb\u5316)\u3002","title":"\u7f16\u8bd1\u65f6\u5e38\u91cf(const)"},{"location":"4-14-Compile-time-constants-constant-expressions-and-constexpr/#const_1","text":"\u4efb\u4f55\u7528\u975e\u5e38\u91cf\u8868\u8fbe\u5f0f\u521d\u59cb\u5316\u7684 const \u53d8\u91cf\u90fd\u662f\u8fd0\u884c\u65f6\u5e38\u91cf\u3002\u8fd0\u884c\u65f6\u5e38\u91cf\u662f\u5728\u8fd0\u884c\u65f6\u624d\u77e5\u9053\u5176\u521d\u59cb\u5316\u503c\u7684\u5e38\u91cf\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u6f14\u793a\u4e86\u8fd0\u884c\u65f6\u5e38\u91cf\u7684\u7528\u6cd5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int getNumber () { std :: cout << \"Enter a number: \" ; int y {}; std :: cin >> y ; return y ; } int main () { const int x { 3 }; // x is a compile time constant std :: cout << \"Enter a number: \" ; const int y { getNumber () }; // y is a runtime constant std :: cout << x + y ; // x + y is a runtime expression return 0 ; } \u5373\u4f7f y \u662f const \uff0c\u5176\u521d\u59cb\u5316\u503c( getNumber() \u7684\u8fd4\u56de\u503c)\u8981\u5230\u8fd0\u884c\u65f6\u624d\u77e5\u9053\u3002\u56e0\u6b64\uff0c y \u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\u5e38\u91cf\uff0c\u800c\u4e0d\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u56e0\u6b64\uff0c\u8868\u8fbe\u5f0f x + y \u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\u8868\u8fbe\u5f0f\u3002","title":"\u8fd0\u884c\u65f6\u5e38\u91cf(const)"},{"location":"4-14-Compile-time-constants-constant-expressions-and-constexpr/#constexpr","text":"\u5f53\u58f0\u660e const \u53d8\u91cf\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u9690\u5f0f\u5730\u8ddf\u8e2a\u5b83\u662f\u8fd0\u884c\u65f6\u5e38\u91cf\u8fd8\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u9664\u4e86\u4f18\u5316\u76ee\u7684\u4e4b\u5916\uff0c\u662f\u8fd0\u884c\u65f6\u5e38\u91cf\u8fd8\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u5e76\u4e0d\u91cd\u8981\uff0c\u4f46\u4e5f\u6709\u4e00\u4e9b\u5947\u602a\u7684\u60c5\u51b5\u4e0b\uff0cC++\u9700\u8981\u7f16\u8bd1\u65f6\u5e38\u91cf\u800c\u4e0d\u662f\u8fd0\u884c\u65f6\u5e38\u91cf(\u6211\u4eec\u5c06\u5728\u7a0d\u540e\u4ecb\u7ecd\u8fd9\u4e9b\u4e3b\u9898\u65f6\u8ba8\u8bba\u8fd9\u4e9b\u60c5\u51b5)\u3002 \u56e0\u4e3a\u7f16\u8bd1\u65f6\u5e38\u91cf\u901a\u5e38\u5141\u8bb8\u66f4\u597d\u7684\u4f18\u5316(\u5e76\u4e14\u51e0\u4e4e\u6ca1\u6709\u7f3a\u70b9)\uff0c\u6240\u4ee5\u6211\u4eec\u901a\u5e38\u5e0c\u671b\u5c3d\u53ef\u80fd\u4f7f\u7528\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 \u5f53\u4f7f\u7528 const \u65f6\uff0c\u53d8\u91cf\u6700\u7ec8\u53ef\u80fd\u662f\u7f16\u8bd1\u65f6\u7684 const \u6216\u8fd0\u884c\u65f6\u7684 const \uff0c\u8fd9\u53d6\u51b3\u4e8e\u521d\u59cb\u5316\u5f0f\u662f\u5426\u662f\u7f16\u8bd1\u65f6\u8868\u8fbe\u5f0f\u3002\u56e0\u4e3a\u4e24\u8005\u7684\u5b9a\u4e49\u770b\u8d77\u6765\u5b8c\u5168\u76f8\u540c\uff0c\u6240\u4ee5\u6211\u4eec\u6700\u7ec8\u5f97\u5230\u7684\u53ef\u80fd\u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\u5e38\u91cf\uff0c\u800c\u6211\u4eec\u539f\u4ee5\u4e3a\u5f97\u5230\u7684\u662f\u4e00\u4e2a\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u5728\u524d\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f88\u96be\u533a\u5206 y \u662f\u7f16\u8bd1\u65f6\u7684 const \u8fd8\u662f\u8fd0\u884c\u65f6\u7684 const \u2014\u2014\u6211\u4eec\u5fc5\u987b\u67e5\u770b getNumber() \u7684\u8fd4\u56de\u503c\u6765\u786e\u5b9a\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u7f16\u8bd1\u5668\u7684\u5e2e\u52a9\uff0c\u4ee5\u786e\u4fdd\u5728\u9884\u671f\u7684\u5730\u65b9\u5f97\u5230\u7f16\u8bd1\u65f6\u7684 const \u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5728\u53d8\u91cf\u58f0\u660e\u4e2d\u4f7f\u7528 constexpr \u800c\u4e0d\u662f const \u3002 constexpr (\u201c\u5e38\u91cf\u8868\u8fbe\u5f0f\u201d\u7684\u7f29\u5199)\u53d8\u91cf\u53ea\u80fd\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002\u5982\u679c constexpr \u53d8\u91cf\u7684\u521d\u59cb\u5316\u503c\u4e0d\u662f\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u7f16\u8bd1\u5668\u5c06\u51fa\u9519\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { constexpr double gravity { 9.8 }; // ok: 9.8 is a constant expression constexpr int sum { 4 + 5 }; // ok: 4 + 5 is a constant expression constexpr int something { sum }; // ok: sum is a constant expression std :: cout << \"Enter your age: \" ; int age {}; std :: cin >> age ; constexpr int myAge { age }; // compile error: age is not a constant expression return 0 ; } \u6700\u4f73\u5b9e\u8df5 \u4efb\u4f55\u5728\u521d\u59cb\u5316\u540e\u503c\u5c31\u4e0d\u80fd\u88ab\u6539\u53d8\uff0c\u4e14\u521d\u59cb\u5316\u503c\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u786e\u5b9a\u7684\u53d8\u91cf\uff0c\u90fd\u5fc5\u987b\u58f0\u660e\u4e3a constexpr \u3002 \u4efb\u4f55\u5728\u521d\u59cb\u5316\u540e\u503c\u5c31\u4e0d\u80fd\u88ab\u6539\u53d8\uff0c\u4f46\u662f\u521d\u59cb\u5316\u503c\u4e0d\u80fd\u5728\u7f16\u8bd1\u65f6\u786e\u5b9a\u7684\u53d8\u91cf\uff0c\u90fd\u5e94\u8be5\u58f0\u660e\u4e3a const \u3002 Although function parameters can be const , function parameters cannot be constexpr . \u76f8\u5173\u5185\u5bb9 C++ \u4e5f\u652f\u6301\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\u51fd\u6570(\u56e0\u6b64\u53ef\u4ee5\u5728\u5e38\u91cf\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528)\u2014\u2014\u6211\u4eec\u4f1a\u5728 6.14 - Constexpr \u548c consteval \u51fd\u6570 \u4e2d\u8ba8\u8bba\u8fd9\u4e9b\u51fd\u6570\u3002","title":"constexpr\u00a0\u5173\u952e\u5b57"},{"location":"4-15-Literals/","tags":["literal","constants","C++14"],"text":"Key Takeaway \u5e38\u91cf\u6709\u4e24\u79cd\uff0c\u5b57\u9762\u91cf\u5e38\u91cf\uff08\u7b80\u79f0\u5b57\u9762\u91cf\uff09\u548c\u7b26\u53f7\u5e38\u91cf \u5b57\u9762\u91cf\u7684\u7c7b\u578b\u5177\u6709\u9ed8\u8ba4\u503c\uff0c\u53ef\u4ee5\u901a\u8fc7\u540e\u7f00\u4fee\u6539 \u5c0f\u6570\u5b57\u9762\u91cf\u7684\u9ed8\u8ba4\u7c7b\u578b\u4e3adouble\u800c\u4e0d\u662ffloat C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u9ed8\u8ba4\u7c7b\u578b\u4e3a\u5b57\u7b26\u6570\u7ec4 std::string \u548c std::string_view \u7c7b\u578b\u7684\u5b57\u9762\u91cf\u53ef\u4ee5\u901a\u8fc7 s \u548c sv \u540e\u7f00\u6307\u5b9a\uff0c\u5728\u7c7b\u578b\u63a8\u65ad\u65f6\u6bd4\u8f83\u6709\u7528\uff0c\u5176\u4ed6\u60c5\u51b5\u4e00\u822c\u4e0d\u9700\u8981 \u58f0\u660e\u6d6e\u70b9\u6570\u5b57\u9762\u91cf\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a 3.14159 \u6216 1.9e10 \u53ea\u6709\u5f53\u4e00\u4e2a\u6570\u5b57\uff08\u4e5f\u53ef\u80fd\u662f\u5176\u4ed6\u7c7b\u578b\uff09\u542b\u4e49\u4e0d\u660e\u786e\u6216\u591a\u6b21\u88ab\u4f7f\u7528\u65f6\u624d\u88ab\u770b\u505a\u662f\u9b54\u672f\u6570\u5b57\u3002\u6709\u4e9b\u573a\u5408\u4e0b\u7684\u5b57\u9762\u91cf\uff0c\u5c24\u5176\u662f\u53ea\u4f7f\u7528\u4e00\u6b21\u7684\u5b57\u9762\u91cf\uff0c\u53ef\u4ee5\u4e0d\u88ab\u770b\u505a\u662f\u9b54\u672f\u6570\u5b57 \u4f7f\u7528\u5e38\u91cf\u6765\u907f\u514d\u9b54\u672f\u6570\u5b57 \u5b57\u9762\u91cf\u5e38\u91cf\uff08\u901a\u5e38\u79f0\u4e3a \u5b57\u9762\u91cf \uff09 \u6307\u7684\u662f\u76f4\u63a5\u5d4c\u5165\u5230\u4ee3\u7801\u4e2d\u7684\u672a\u547d\u540d\u7684\u503c\uff0c\u4f8b\u5982\uff1a 1 2 3 return 5 ; // 5 \u662f\u4e00\u4e2a\u6574\u578b\u5b57\u9762\u91cf bool myNameIsAlex { true }; // true \u662f\u4e00\u4e2a\u5e03\u5c14\u5b57\u9762\u91cf std :: cout << 3.4 ; // 3.4 \u662f\u4e00\u4e2a double \u5b57\u9762\u91cf \u5b57\u9762\u91cf\u901a\u5e38\u88ab\u79f0\u4e3a \u5b57\u9762\u91cf\u5e38\u91cf \uff0c\u56e0\u4e3a\u4f60\u4e0d\u80fd\u52a8\u6001\u5730\u6539\u53d8\u5b83\u4eec\u7684\u503c \u5b57\u9762\u91cf\u7684\u7c7b\u578b \u548c\u5176\u4ed6\u5bf9\u8c61\u8bf6\u65b0\u4e00\u6837\uff0c\u6240\u6709\u7684\u5b57\u9762\u91cf\u4e5f\u6709\u5176\u7c7b\u578b\u3002\u5b57\u9762\u91cf\u7684\u7c7b\u578b\u7531\u5176\u503c\u548c\u5b57\u9762\u91cf\u672c\u8eab\u7684\u683c\u5f0f\u63a8\u5b9a\u3002 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff1a \u5b57\u9762\u91cf\u503c \u4f8b\u5b50 \u9ed8\u8ba4\u7c7b\u578b integral value 5, 0, -3 int boolean value true, false bool floating point value 3.4, -2.2 double (\u4e0d\u662f float)! char value \u2018a\u2019 char C-style string \u201cHello, world!\u201d const char[14] \u5b57\u9762\u91cf\u540e\u7f00 \u5982\u679c\u9ed8\u8ba4\u7c7b\u578b\u4e0d\u662f\u4f60\u60f3\u8981\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u9762\u91cf\u7684\u540e\u7f00\u6539\u53d8\u5176\u7c7b\u578b\uff1a \u6570\u636e\u7c7b\u578b \u540e\u7f00 \u542b\u4e49 int u or U unsigned int int l or L long int ul, uL, Ul, UL, lu, lU, Lu, or LU unsigned long int ll or LL long long int ull, uLL, Ull, ULL, llu, llU, LLu, or LLU unsigned long long double f or F float double l or L long double \u540e\u7f00\u662f\u533a\u5206\u5927\u5c0f\u5199\u7684\uff0c\u4f46\u662f\u7531\u4e8e\u5c0f\u5199\u5b57\u6bcd L \u770b\u8d77\u6765\u5f88\u50cf 1 \uff0c\u6240\u4ee5\u5f00\u53d1\u8005\u901a\u5e38\u559c\u6b22\u7528\u5927\u5199\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 4.17 - std::string \u7b80\u4ecb \u548c 4.18 - std::string_view \u7b80\u4ecb \u4e2d\u8ba8\u8bba\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7684\u540e\u7f00\u3002 \u6574\u578b\u5b57\u9762\u91cf \u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u4f60\u4e0d\u9700\u8981\u4e3a\u6574\u578b\u6307\u5b9a\u540e\u7f00\uff0c\u4f46\u662f\u5982\u679c\u8981\u505a\u7684\u8bdd\uff0c\u53ef\u4ee5\u53c2\u8003\u4e0b\u9762\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { std :: cout << 5 ; // 5 (no suffix) is type int (by default) std :: cout << 5L ; // 5L is type long return 0 ; } u (\u6216\u8005 \u2018U\u2019) \u662f\u4e00\u4e2a\u7279\u4f8b\uff0c\u901a\u5e38\u7528\u6765\u6807\u8bb0\u65e0\u7b26\u53f7\u6574\u578b\u5b57\u9762\u91cf\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { unsigned int x { 5u }; // 5u is type unsigned int std :: cout << x ; return 0 ; } \u6d6e\u70b9\u6570\u5b57\u9762\u91cf \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6d6e\u70b9\u6570\u5b57\u9762\u91cf\u7684\u7c7b\u578b\u4e3a double \uff0c\u5982\u679c\u5e0c\u671b\u4f7f\u7528 float \u7c7b\u578b\uff0c\u5219\u9700\u8981\u6307\u5b9a f \u6216 F \u540e\u7f00\uff1a 1 2 std :: cout << 5.0 ; // 5.0 (no suffix) is type double (by default) std :: cout << 5.0f ; // 5.0f is type float \u65b0\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u5947\u602a\u4e3a\u4ec0\u4e48\u4e0b\u9762\u7684\u4ee3\u7801\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u544a\u8b66\uff1a 1 float f { 4.1 }; // warning: 4.1 \u662f double \u7c7b\u578b\u7684\u5b57\u9762\u91cf\u800c\u4e0d\u662f float \u7c7b\u578b \u56e0\u4e3a\uff0c4.1 \u6ca1\u6709\u540e\u7f00\u6240\u4ee5\u662f double \u7c7b\u578b\u7684\u5b57\u9762\u91cf\uff0c\u800c\u4e0d\u662f float \u7c7b\u578b\u3002\u5f53C++\u5b9a\u4e49\u5b57\u9762\u91cf\u7684\u7c7b\u578b\u65f6\uff0c\u5b83\u4e0d\u5728\u4e4e\u8be5\u5b57\u9762\u91cf\u7684\u7528\u9014\uff08\u4f8b\u5982\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u662f\u7528\u4e8e\u521d\u59cb\u5316\u4e00\u4e2a float \u53d8\u91cf\uff09\u3002\u56e0\u6b64\uff0c4.1 \u5fc5\u987b\u88ab\u8f6c\u6362\u4e3a double \u7c7b\u578b\u624d\u53ef\u4ee5\u88ab\u8d4b\u503c\u7ed9\u53d8\u91cf f \uff0c\u800c\u8fd9\u4e48\u505a\u662f\u4f1a\u5bfc\u81f4\u7cbe\u5ea6\u4e22\u5931\u7684\u3002 \u5728C++\u4e2d\uff0c\u53ea\u8981\u5b57\u9762\u91cf\u7684\u610f\u601d\u662f\u6e05\u6670\u7684\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u653e\u5fc3\u4f7f\u7528\u3002\u4f8b\u5982\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4f1a\u4f7f\u7528\u5b57\u9762\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u6216\u8d4b\u503c\u3001\u6570\u5b66\u8fd0\u7b97\u6216\u76f4\u63a5\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\u3002 \u5b57\u7b26\u4e32\u5b57\u9762\u91cf \u5728 4.11 - \u5b57\u7b26 \u4e2d\u6211\u4eec\u5c06\u5b57\u7b26\u4e32\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u5b57\u7b26\u5e8f\u5217\u7684\u96c6\u5408\u3002\u5728 C++ \u4e2d\u4e5f\u652f\u6301\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff1a 1 2 std :: cout << \"Hello, world!\" ; // \"Hello, world!\" is a C-style string literal std :: cout << \"Hello,\" \" world!\" ; // C++ \u4f1a\u94fe\u63a5\u5b57\u7b26\u4e32\u5b57\u9762\u91cf \u51fa\u4e8e\u67d0\u4e9b\u5386\u53f2\u539f\u56e0\uff0cC++ \u5904\u7406\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u6709\u4e9b\u7279\u522b\u3002\u76ee\u524d\u6765\u8bb2\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u6765\u4f5c\u4e3a\u6253\u5370\u6587\u672c\u6216\u7528\u4e8e\u521d\u59cb\u5316 std::string \u3002 \u6269\u5c55\u9605\u8bfb C++ \u540c\u6837\u4e5f\u652f\u6301 std::string \u548c std::string_view \u5b57\u9762\u91cf\u3002\u867d\u7136\u5728\u5f88\u591a\u65f6\u5019\u5e76\u4e0d\u9700\u8981\u4f7f\u7528\u5b83\u4eec\uff0c\u4f46\u662f\u5728\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u7684\u65f6\u5019\uff0c\u5b83\u4eec\u8fd8\u662f\u633a\u6709\u7528\u7684\u3002\u7c7b\u578b\u63a8\u65ad\u53ef\u4ee5\u53d1\u751f\u5728\u4f7f\u7528 auto \u5173\u952e\u5b57\uff08 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad \uff09\u6216\u662f\u5728\u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65ad\u65f6\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include #include // for std::string #include // for std::string_view int main () { using namespace std :: literals ; // easiest way to access the s and sv suffixes std :: cout << \"foo \\n \" ; // no suffix is a C-style string literal std :: cout << \"goo \\n \" s ; // s \u540e\u7f00\u8868\u793a std::string \u5b57\u9762\u91cf std :: cout << \"moo \\n \" sv ; // sv \u540e\u7f00\u8868\u793a a std::string_view \u5b57\u9762\u91cf return 0 ; }; \u6b64\u5904\u4f7f\u7528 using \u6765\u5f15\u5165\u6574\u4e2a\u547d\u540d\u7a7a\u95f4\u662f\u4e00\u4e2a\u88ab\u5141\u8bb8\u7684\u7279\u4f8b\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u6df1\u5165\u8ba8\u8bba\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u3002 \u6d6e\u70b9\u6570\u5b57\u9762\u91cf\u7684\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a \u58f0\u660e\u6d6e\u70b9\u6570\u5b57\u9762\u91cf\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a 1 2 double pi { 3.14159 }; // 3.14159 \u662f double \u7c7b\u578b\u5b57\u9762\u91cf double avogadro { 6.02e23 }; // 6.02 x 10^23 \u662f double \u7c7b\u578b\uff0c\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a \u5bf9\u4e8e\u79d1\u5b66\u8ba1\u6570\u6cd5\u6765\u8bb2\uff0c\u6307\u6570\u90e8\u5206\u4e5f\u53ef\u4ee5\u662f\u8d1f\u503c\uff1a 1 double electron { 1.6e-19 }; // charge on an electron is 1.6 x 10^-19 \u9b54\u672f\u6570\u5b57 \u9b54\u672f\u6570\u5b57\u6307\u7684\u662f\u542b\u4e49\u4e0d\u6e05\u6216\u591a\u6b21\u4f7f\u7528\u7684\u5b57\u9762\u91cf\uff08\u901a\u5e38\u662f\u6570\u5b57\uff09\u3002 \u4e0b\u9762\u4ee3\u7801\u5c55\u793a\u4e86\u4e00\u79cd\u542b\u4e49\u4e0d\u6e05\u7684\u9b54\u672f\u6570\u5b57\uff1a 1 constexpr int maxStudentsPerSchool { numClassrooms * 30 }; \u8fd9\u91cc\u7684 30 \u5230\u5e95\u662f\u4ec0\u4e48\u542b\u4e49\uff1f\u5c3d\u7ba1\u6709\u65f6\u5019\u4f60\u53ef\u80fd\u53ef\u4ee5\u731c\u5230\u5b83\u7684\u542b\u4e49\uff0c\u4f8b\u5982\u8fd9\u91cc\u6307\u7684\u662f\u6bcf\u4e2a\u6559\u5ba4\u6700\u591a\u7684\u5b66\u751f\u6570\uff0c\u4f46\u662f\u8fd9\u4e2a\u610f\u4e49\u5e76\u4e0d\u660e\u663e\u3002\u5bf9\u4e8e\u66f4\u52a0\u590d\u6742\u7684\u7a0b\u5e8f\u6765\u8bf4\uff0c\u63a8\u6d4b\u67d0\u4e2a\u786c\u7f16\u7801\u7684\u6570\u5b57\u662f\u5f88\u96be\u7684\uff0c\u9664\u975e\u6709\u6ce8\u91ca\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7b26\u53f7\u5e38\u91cf\u6765\u907f\u514d\u8fd9\u79cd\u542b\u4e49\u4e0d\u6e05\u7684\u9b54\u672f\u6570\u5b57\uff1a 1 2 constexpr int maxStudentsPerClass { 30 }; // now obvious what 30 is constexpr int maxStudentsPerSchool { numClassrooms * maxStudentsPerClass }; \u4f7f\u7528\u9b54\u672f\u6570\u5b57\u901a\u5e38\u88ab\u8ba4\u4e3a\u662f\u4e00\u79cd\u4e0d\u597d\u7684\u7f16\u7801\u4e60\u60ef\uff0c\u5b83\u4eec\u4e0d\u4ec5\u6ca1\u6709\u63d0\u4f9b\u5173\u4e8e\u5176\u7528\u9014\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u800c\u4e14\u8fd8\u7559\u4e0b\u4e86\u4e00\u4e2a\u9690\u60a3\uff08\u4e07\u4e00\u9700\u8981\u4fee\u6539\u5219\u9700\u8981\u5728\u591a\u5904\u4fee\u6539\uff09\u3002\u5047\u8bbe\uff0c\u5b66\u6821\u8d2d\u4e70\u4e86\u4e00\u4e9b\u65b0\u7684\u8bfe\u684c\uff0c\u73b0\u5728\u4e00\u4e2a\u6559\u5ba4\u80fd\u591f\u5bb9\u7eb3 35 \u4eba\u4e86\uff0c\u90a3\u4e48\u6211\u4eec\u7684\u7a0b\u5e8f\u4e5f\u5fc5\u987b\u53cd\u6620\u8fd9\u4e00\u60c5\u51b5\u3002\u8003\u8651\u5982\u4e0b\u4ee3\u7801\uff1a 1 2 constexpr int maxStudents { numClassrooms * 30 }; setMax ( 30 ); \u4e3a\u4e86\u4fee\u6539\u7a0b\u5e8f\u9002\u5e94\u65b0\u7684\u60c5\u51b5\uff0c\u6211\u4eec\u5fc5\u987b\u628a\u5e38\u91cf 30 \u4fee\u6539\u4e3a 35\uff0c\u4f46\u662f setMax() \u600e\u4e48\u529e\uff1f\u5b83\u7684\u53c2\u6570 30 \u548c\u5176\u4ed6 30 \u662f\u4e00\u4e2a\u610f\u601d\u5417\uff1f\u5e94\u8be5\u66f4\u65b0\u8fd8\u662f\u5e94\u8be5\u4fdd\u7559\u554a\uff0c\u641e\u4e0d\u597d\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u51fa\u95ee\u9898\u7684\u3002\u5982\u4f60\u8fdb\u884c\u5168\u5c40\u7684\u67e5\u627e\u548c\u66ff\u6362\uff0c\u90a3\u4e48\u4f60\u53ef\u80fd\u4f1a\u66ff\u6362\u6389\u672c\u4e0d\u5e94\u8be5\u88ab\u66f4\u65b0\u7684 setMax() \u4e2d\u7684 30\u3002\u6240\u4ee5\u4f60\u53ef\u80fd\u9700\u8981\u9010\u4e2a\u68c0\u67e5\u6bcf\u4e00\u4e2a 30\uff0c\u786e\u4fdd\u5b83\u5e94\u8be5\u88ab\u66ff\u6362\u624d\u6267\u884c\u64cd\u4f5c\u3002\u8fd9\u5c31\u592a\u8d39\u65f6\u95f4\uff08\u800c\u4e14\u8fd8\u5bb9\u6613\u51fa\u9519\uff09\u3002 \u4e0b\u9762\u7684\u4ee3\u7801 (\u4f7f\u7528\u7b26\u53f7\u5e38\u91cf) \u5219\u66f4\u52a0\u6e05\u6670\uff0c\u660e\u663e\u53ef\u4ee5\u770b\u51fa\u4e24\u4e2a 30 \u662f\u4e0d\u662f\u4e00\u56de\u4e8b\uff1a 1 2 3 4 5 constexpr int maxStudentsPerClass { 30 }; // now obvious what 30 is constexpr int totalStudents { numClassrooms * maxStudentsPerClass }; constexpr int maxNameLength { 30 }; setMax ( maxNameLength ); // now obvious this 30 is used in a different context \u9b54\u672f\u6570\u5b57\u5e76\u4e0d\u603b\u662f\u6570\u5b57\u2014\u2014\u4e5f\u53ef\u80fd\u662f\u5b57\u7b26\u4e32\u6216\u5176\u4ed6\u7c7b\u578b\u3002 \u6ce8\u610f\uff0c\u53ea\u4f7f\u7528\u4e00\u6b21\uff0c\u4e14\u542b\u4e49\u660e\u786e\u7684 \u5b57\u9762\u91cf \u901a\u5e38\u4e0d\u88ab\u8ba4\u4e3a\u662f\u9b54\u672f\u6570\u5b57\u3002\u50cf-1\u30010\u30010.0 \u548c 1\u8fd9\u6837\u7684\u503c\u5c31\u7ecf\u5e38\u88ab\u7528\u5728\u542b\u4e49\u975e\u5e38\u660e\u786e\u7684\u60c5\u5883\u4e2d\uff1a 1 2 int idGenerator { 0 }; // fine: we're starting our id generator with value 0 idGenerator = idGenerator + 1 ; // fine: we're just incrementing our generator \u5176\u4ed6\u503c\u5728\u67d0\u4e9b\u8bed\u5883\u4e0b\u542b\u4e49\u4e5f\u662f\u660e\u663e\u7684 (\u56e0\u6b64\u4e5f\u53ef\u4ee5\u4e0d\u88ab\u770b\u505a\u662f\u9b54\u672f\u6570\u5b57)\uff1a 1 int kmtoM ( int km ) { return km * 1000 ; } // \u53ef\u4ee5\u8fd9\u4e48\u505a: \u5f88\u663e\u7136\u8fd9\u91cc1000\u662fkm\u548cm\u4e4b\u95f4\u7684\u8f6c\u6362\u7cfb\u6570 \u6700\u4f73\u5b9e\u8df5 \u5728\u4ee3\u7801\u4e2d\u907f\u514d\u9b54\u672f\u6570\u5b57 (\u4f7f\u7528\u5e38\u91cf\u6765\u4ee3\u66ff)\u3002","title":"4.15 - \u5b57\u9762\u91cf"},{"location":"4-15-Literals/#_1","text":"\u548c\u5176\u4ed6\u5bf9\u8c61\u8bf6\u65b0\u4e00\u6837\uff0c\u6240\u6709\u7684\u5b57\u9762\u91cf\u4e5f\u6709\u5176\u7c7b\u578b\u3002\u5b57\u9762\u91cf\u7684\u7c7b\u578b\u7531\u5176\u503c\u548c\u5b57\u9762\u91cf\u672c\u8eab\u7684\u683c\u5f0f\u63a8\u5b9a\u3002 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff1a \u5b57\u9762\u91cf\u503c \u4f8b\u5b50 \u9ed8\u8ba4\u7c7b\u578b integral value 5, 0, -3 int boolean value true, false bool floating point value 3.4, -2.2 double (\u4e0d\u662f float)! char value \u2018a\u2019 char C-style string \u201cHello, world!\u201d const char[14]","title":"\u5b57\u9762\u91cf\u7684\u7c7b\u578b"},{"location":"4-15-Literals/#_2","text":"\u5982\u679c\u9ed8\u8ba4\u7c7b\u578b\u4e0d\u662f\u4f60\u60f3\u8981\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u9762\u91cf\u7684\u540e\u7f00\u6539\u53d8\u5176\u7c7b\u578b\uff1a \u6570\u636e\u7c7b\u578b \u540e\u7f00 \u542b\u4e49 int u or U unsigned int int l or L long int ul, uL, Ul, UL, lu, lU, Lu, or LU unsigned long int ll or LL long long int ull, uLL, Ull, ULL, llu, llU, LLu, or LLU unsigned long long double f or F float double l or L long double \u540e\u7f00\u662f\u533a\u5206\u5927\u5c0f\u5199\u7684\uff0c\u4f46\u662f\u7531\u4e8e\u5c0f\u5199\u5b57\u6bcd L \u770b\u8d77\u6765\u5f88\u50cf 1 \uff0c\u6240\u4ee5\u5f00\u53d1\u8005\u901a\u5e38\u559c\u6b22\u7528\u5927\u5199\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 4.17 - std::string \u7b80\u4ecb \u548c 4.18 - std::string_view \u7b80\u4ecb \u4e2d\u8ba8\u8bba\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7684\u540e\u7f00\u3002","title":"\u5b57\u9762\u91cf\u540e\u7f00"},{"location":"4-15-Literals/#_3","text":"\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u4f60\u4e0d\u9700\u8981\u4e3a\u6574\u578b\u6307\u5b9a\u540e\u7f00\uff0c\u4f46\u662f\u5982\u679c\u8981\u505a\u7684\u8bdd\uff0c\u53ef\u4ee5\u53c2\u8003\u4e0b\u9762\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { std :: cout << 5 ; // 5 (no suffix) is type int (by default) std :: cout << 5L ; // 5L is type long return 0 ; } u (\u6216\u8005 \u2018U\u2019) \u662f\u4e00\u4e2a\u7279\u4f8b\uff0c\u901a\u5e38\u7528\u6765\u6807\u8bb0\u65e0\u7b26\u53f7\u6574\u578b\u5b57\u9762\u91cf\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { unsigned int x { 5u }; // 5u is type unsigned int std :: cout << x ; return 0 ; }","title":"\u6574\u578b\u5b57\u9762\u91cf"},{"location":"4-15-Literals/#_4","text":"\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6d6e\u70b9\u6570\u5b57\u9762\u91cf\u7684\u7c7b\u578b\u4e3a double \uff0c\u5982\u679c\u5e0c\u671b\u4f7f\u7528 float \u7c7b\u578b\uff0c\u5219\u9700\u8981\u6307\u5b9a f \u6216 F \u540e\u7f00\uff1a 1 2 std :: cout << 5.0 ; // 5.0 (no suffix) is type double (by default) std :: cout << 5.0f ; // 5.0f is type float \u65b0\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u5947\u602a\u4e3a\u4ec0\u4e48\u4e0b\u9762\u7684\u4ee3\u7801\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u544a\u8b66\uff1a 1 float f { 4.1 }; // warning: 4.1 \u662f double \u7c7b\u578b\u7684\u5b57\u9762\u91cf\u800c\u4e0d\u662f float \u7c7b\u578b \u56e0\u4e3a\uff0c4.1 \u6ca1\u6709\u540e\u7f00\u6240\u4ee5\u662f double \u7c7b\u578b\u7684\u5b57\u9762\u91cf\uff0c\u800c\u4e0d\u662f float \u7c7b\u578b\u3002\u5f53C++\u5b9a\u4e49\u5b57\u9762\u91cf\u7684\u7c7b\u578b\u65f6\uff0c\u5b83\u4e0d\u5728\u4e4e\u8be5\u5b57\u9762\u91cf\u7684\u7528\u9014\uff08\u4f8b\u5982\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u662f\u7528\u4e8e\u521d\u59cb\u5316\u4e00\u4e2a float \u53d8\u91cf\uff09\u3002\u56e0\u6b64\uff0c4.1 \u5fc5\u987b\u88ab\u8f6c\u6362\u4e3a double \u7c7b\u578b\u624d\u53ef\u4ee5\u88ab\u8d4b\u503c\u7ed9\u53d8\u91cf f \uff0c\u800c\u8fd9\u4e48\u505a\u662f\u4f1a\u5bfc\u81f4\u7cbe\u5ea6\u4e22\u5931\u7684\u3002 \u5728C++\u4e2d\uff0c\u53ea\u8981\u5b57\u9762\u91cf\u7684\u610f\u601d\u662f\u6e05\u6670\u7684\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u653e\u5fc3\u4f7f\u7528\u3002\u4f8b\u5982\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4f1a\u4f7f\u7528\u5b57\u9762\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u6216\u8d4b\u503c\u3001\u6570\u5b66\u8fd0\u7b97\u6216\u76f4\u63a5\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\u3002","title":"\u6d6e\u70b9\u6570\u5b57\u9762\u91cf"},{"location":"4-15-Literals/#_5","text":"\u5728 4.11 - \u5b57\u7b26 \u4e2d\u6211\u4eec\u5c06\u5b57\u7b26\u4e32\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u5b57\u7b26\u5e8f\u5217\u7684\u96c6\u5408\u3002\u5728 C++ \u4e2d\u4e5f\u652f\u6301\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff1a 1 2 std :: cout << \"Hello, world!\" ; // \"Hello, world!\" is a C-style string literal std :: cout << \"Hello,\" \" world!\" ; // C++ \u4f1a\u94fe\u63a5\u5b57\u7b26\u4e32\u5b57\u9762\u91cf \u51fa\u4e8e\u67d0\u4e9b\u5386\u53f2\u539f\u56e0\uff0cC++ \u5904\u7406\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u6709\u4e9b\u7279\u522b\u3002\u76ee\u524d\u6765\u8bb2\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u6765\u4f5c\u4e3a\u6253\u5370\u6587\u672c\u6216\u7528\u4e8e\u521d\u59cb\u5316 std::string \u3002 \u6269\u5c55\u9605\u8bfb C++ \u540c\u6837\u4e5f\u652f\u6301 std::string \u548c std::string_view \u5b57\u9762\u91cf\u3002\u867d\u7136\u5728\u5f88\u591a\u65f6\u5019\u5e76\u4e0d\u9700\u8981\u4f7f\u7528\u5b83\u4eec\uff0c\u4f46\u662f\u5728\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u7684\u65f6\u5019\uff0c\u5b83\u4eec\u8fd8\u662f\u633a\u6709\u7528\u7684\u3002\u7c7b\u578b\u63a8\u65ad\u53ef\u4ee5\u53d1\u751f\u5728\u4f7f\u7528 auto \u5173\u952e\u5b57\uff08 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad \uff09\u6216\u662f\u5728\u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65ad\u65f6\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include #include // for std::string #include // for std::string_view int main () { using namespace std :: literals ; // easiest way to access the s and sv suffixes std :: cout << \"foo \\n \" ; // no suffix is a C-style string literal std :: cout << \"goo \\n \" s ; // s \u540e\u7f00\u8868\u793a std::string \u5b57\u9762\u91cf std :: cout << \"moo \\n \" sv ; // sv \u540e\u7f00\u8868\u793a a std::string_view \u5b57\u9762\u91cf return 0 ; }; \u6b64\u5904\u4f7f\u7528 using \u6765\u5f15\u5165\u6574\u4e2a\u547d\u540d\u7a7a\u95f4\u662f\u4e00\u4e2a\u88ab\u5141\u8bb8\u7684\u7279\u4f8b\u3002 \u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u6df1\u5165\u8ba8\u8bba\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u3002","title":"\u5b57\u7b26\u4e32\u5b57\u9762\u91cf"},{"location":"4-15-Literals/#_6","text":"\u58f0\u660e\u6d6e\u70b9\u6570\u5b57\u9762\u91cf\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a 1 2 double pi { 3.14159 }; // 3.14159 \u662f double \u7c7b\u578b\u5b57\u9762\u91cf double avogadro { 6.02e23 }; // 6.02 x 10^23 \u662f double \u7c7b\u578b\uff0c\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a \u5bf9\u4e8e\u79d1\u5b66\u8ba1\u6570\u6cd5\u6765\u8bb2\uff0c\u6307\u6570\u90e8\u5206\u4e5f\u53ef\u4ee5\u662f\u8d1f\u503c\uff1a 1 double electron { 1.6e-19 }; // charge on an electron is 1.6 x 10^-19","title":"\u6d6e\u70b9\u6570\u5b57\u9762\u91cf\u7684\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a"},{"location":"4-15-Literals/#_7","text":"\u9b54\u672f\u6570\u5b57\u6307\u7684\u662f\u542b\u4e49\u4e0d\u6e05\u6216\u591a\u6b21\u4f7f\u7528\u7684\u5b57\u9762\u91cf\uff08\u901a\u5e38\u662f\u6570\u5b57\uff09\u3002 \u4e0b\u9762\u4ee3\u7801\u5c55\u793a\u4e86\u4e00\u79cd\u542b\u4e49\u4e0d\u6e05\u7684\u9b54\u672f\u6570\u5b57\uff1a 1 constexpr int maxStudentsPerSchool { numClassrooms * 30 }; \u8fd9\u91cc\u7684 30 \u5230\u5e95\u662f\u4ec0\u4e48\u542b\u4e49\uff1f\u5c3d\u7ba1\u6709\u65f6\u5019\u4f60\u53ef\u80fd\u53ef\u4ee5\u731c\u5230\u5b83\u7684\u542b\u4e49\uff0c\u4f8b\u5982\u8fd9\u91cc\u6307\u7684\u662f\u6bcf\u4e2a\u6559\u5ba4\u6700\u591a\u7684\u5b66\u751f\u6570\uff0c\u4f46\u662f\u8fd9\u4e2a\u610f\u4e49\u5e76\u4e0d\u660e\u663e\u3002\u5bf9\u4e8e\u66f4\u52a0\u590d\u6742\u7684\u7a0b\u5e8f\u6765\u8bf4\uff0c\u63a8\u6d4b\u67d0\u4e2a\u786c\u7f16\u7801\u7684\u6570\u5b57\u662f\u5f88\u96be\u7684\uff0c\u9664\u975e\u6709\u6ce8\u91ca\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7b26\u53f7\u5e38\u91cf\u6765\u907f\u514d\u8fd9\u79cd\u542b\u4e49\u4e0d\u6e05\u7684\u9b54\u672f\u6570\u5b57\uff1a 1 2 constexpr int maxStudentsPerClass { 30 }; // now obvious what 30 is constexpr int maxStudentsPerSchool { numClassrooms * maxStudentsPerClass }; \u4f7f\u7528\u9b54\u672f\u6570\u5b57\u901a\u5e38\u88ab\u8ba4\u4e3a\u662f\u4e00\u79cd\u4e0d\u597d\u7684\u7f16\u7801\u4e60\u60ef\uff0c\u5b83\u4eec\u4e0d\u4ec5\u6ca1\u6709\u63d0\u4f9b\u5173\u4e8e\u5176\u7528\u9014\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u800c\u4e14\u8fd8\u7559\u4e0b\u4e86\u4e00\u4e2a\u9690\u60a3\uff08\u4e07\u4e00\u9700\u8981\u4fee\u6539\u5219\u9700\u8981\u5728\u591a\u5904\u4fee\u6539\uff09\u3002\u5047\u8bbe\uff0c\u5b66\u6821\u8d2d\u4e70\u4e86\u4e00\u4e9b\u65b0\u7684\u8bfe\u684c\uff0c\u73b0\u5728\u4e00\u4e2a\u6559\u5ba4\u80fd\u591f\u5bb9\u7eb3 35 \u4eba\u4e86\uff0c\u90a3\u4e48\u6211\u4eec\u7684\u7a0b\u5e8f\u4e5f\u5fc5\u987b\u53cd\u6620\u8fd9\u4e00\u60c5\u51b5\u3002\u8003\u8651\u5982\u4e0b\u4ee3\u7801\uff1a 1 2 constexpr int maxStudents { numClassrooms * 30 }; setMax ( 30 ); \u4e3a\u4e86\u4fee\u6539\u7a0b\u5e8f\u9002\u5e94\u65b0\u7684\u60c5\u51b5\uff0c\u6211\u4eec\u5fc5\u987b\u628a\u5e38\u91cf 30 \u4fee\u6539\u4e3a 35\uff0c\u4f46\u662f setMax() \u600e\u4e48\u529e\uff1f\u5b83\u7684\u53c2\u6570 30 \u548c\u5176\u4ed6 30 \u662f\u4e00\u4e2a\u610f\u601d\u5417\uff1f\u5e94\u8be5\u66f4\u65b0\u8fd8\u662f\u5e94\u8be5\u4fdd\u7559\u554a\uff0c\u641e\u4e0d\u597d\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u51fa\u95ee\u9898\u7684\u3002\u5982\u4f60\u8fdb\u884c\u5168\u5c40\u7684\u67e5\u627e\u548c\u66ff\u6362\uff0c\u90a3\u4e48\u4f60\u53ef\u80fd\u4f1a\u66ff\u6362\u6389\u672c\u4e0d\u5e94\u8be5\u88ab\u66f4\u65b0\u7684 setMax() \u4e2d\u7684 30\u3002\u6240\u4ee5\u4f60\u53ef\u80fd\u9700\u8981\u9010\u4e2a\u68c0\u67e5\u6bcf\u4e00\u4e2a 30\uff0c\u786e\u4fdd\u5b83\u5e94\u8be5\u88ab\u66ff\u6362\u624d\u6267\u884c\u64cd\u4f5c\u3002\u8fd9\u5c31\u592a\u8d39\u65f6\u95f4\uff08\u800c\u4e14\u8fd8\u5bb9\u6613\u51fa\u9519\uff09\u3002 \u4e0b\u9762\u7684\u4ee3\u7801 (\u4f7f\u7528\u7b26\u53f7\u5e38\u91cf) \u5219\u66f4\u52a0\u6e05\u6670\uff0c\u660e\u663e\u53ef\u4ee5\u770b\u51fa\u4e24\u4e2a 30 \u662f\u4e0d\u662f\u4e00\u56de\u4e8b\uff1a 1 2 3 4 5 constexpr int maxStudentsPerClass { 30 }; // now obvious what 30 is constexpr int totalStudents { numClassrooms * maxStudentsPerClass }; constexpr int maxNameLength { 30 }; setMax ( maxNameLength ); // now obvious this 30 is used in a different context \u9b54\u672f\u6570\u5b57\u5e76\u4e0d\u603b\u662f\u6570\u5b57\u2014\u2014\u4e5f\u53ef\u80fd\u662f\u5b57\u7b26\u4e32\u6216\u5176\u4ed6\u7c7b\u578b\u3002 \u6ce8\u610f\uff0c\u53ea\u4f7f\u7528\u4e00\u6b21\uff0c\u4e14\u542b\u4e49\u660e\u786e\u7684 \u5b57\u9762\u91cf \u901a\u5e38\u4e0d\u88ab\u8ba4\u4e3a\u662f\u9b54\u672f\u6570\u5b57\u3002\u50cf-1\u30010\u30010.0 \u548c 1\u8fd9\u6837\u7684\u503c\u5c31\u7ecf\u5e38\u88ab\u7528\u5728\u542b\u4e49\u975e\u5e38\u660e\u786e\u7684\u60c5\u5883\u4e2d\uff1a 1 2 int idGenerator { 0 }; // fine: we're starting our id generator with value 0 idGenerator = idGenerator + 1 ; // fine: we're just incrementing our generator \u5176\u4ed6\u503c\u5728\u67d0\u4e9b\u8bed\u5883\u4e0b\u542b\u4e49\u4e5f\u662f\u660e\u663e\u7684 (\u56e0\u6b64\u4e5f\u53ef\u4ee5\u4e0d\u88ab\u770b\u505a\u662f\u9b54\u672f\u6570\u5b57)\uff1a 1 int kmtoM ( int km ) { return km * 1000 ; } // \u53ef\u4ee5\u8fd9\u4e48\u505a: \u5f88\u663e\u7136\u8fd9\u91cc1000\u662fkm\u548cm\u4e4b\u95f4\u7684\u8f6c\u6362\u7cfb\u6570 \u6700\u4f73\u5b9e\u8df5 \u5728\u4ee3\u7801\u4e2d\u907f\u514d\u9b54\u672f\u6570\u5b57 (\u4f7f\u7528\u5e38\u91cf\u6765\u4ee3\u66ff)\u3002","title":"\u9b54\u672f\u6570\u5b57"},{"location":"4-16-Numeral-systems-decimal-binary-hexadecimal-and-octal/","tags":["Numeral systems"],"text":"Key Takeaway C++14\u624d\u652f\u6301\u4e86\u4f7f\u7528 0b \u521b\u5efa\u4e8c\u8fdb\u5236\u5b57\u9762\u91cf\uff0c\u540c\u65f6\u53ef\u4ee5\u7528\u5355\u5f15\u53f7\u4f5c\u4e3a\u4f4d\u7684\u5206\u9694\u7b26\uff0c\u4f46\u662f\u5206\u9694\u7b26\u4e0d\u80fd\u653e\u5728\u7b2c\u4e00\u4f4d\u524d\u9762 \u4f7f\u7528 std::dec \u3001 std::oct \u548c std::hex \u53ef\u4ee5\u8f7b\u677e\u6253\u5370\u5341\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u548c\u5341\u516d\u8fdb\u5236\u3002\u4f46\u4e0d\u80fd\u76f4\u63a5\u6253\u5370\u4e8c\u8fdb\u5236\u3002 std::cout << std::hex << x << '\\n'; // hexadecimal \u4f7f\u7528 std::bitset \uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a std::bitset \u53d8\u91cf\uff0c\u5e76\u4e14\u544a\u8bc9 std::bitset \u6709\u591a\u5c11\u4f4d\u9700\u8981\u5b58\u50a8\u3002\u9700\u8981\u5305\u542b \u5934\u6587\u4ef6 \u4f5c\u8005\u6ce8 \u672c\u8282\u8bfe\u4e3a\u9009\u5b66\u3002 \u5728\u5c06\u6765\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4f1a\u4f7f\u7528\u5341\u516d\u8fdb\u5236\u6570\uff0c\u6240\u4ee5\u4f60\u81f3\u5c11\u9700\u8981\u638c\u63e1\u5341\u516d\u8fdb\u5236\u3002 \u5728\u65e5\u5e38\u751f\u6d3b\u4e2d\uff0c\u6211\u4eec\u4e60\u60ef\u4e86\u6570\u5b57\u53ef\u4ee5\u6709 0, 1, 2, 3, 4, 5, 6, 7, 8, \u6216 9\u3002\u5341\u8fdb\u5236\u4e5f\u79f0\u4e3a\u201cbase 10\u201d\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u670910\u79cd\u4e0d\u540c\u7684\u6570\u5b57(0 \u5230 9)\u3002\u5728\u8fd9\u4e2a\u7cfb\u7edf\u91cc\uff0c\u6211\u4eec\u4f1a\u6309\u7167 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, \u2026 \u7684\u65b9\u5f0f\u6765\u8ba1\u6570\uff0c\u800c\u5728 C++ \u4e2d\uff0c\u6570\u5b57\u4e5f\u662f\u9ed8\u8ba4\u5341\u8fdb\u5236\u8868\u793a\u6cd5\u3002 1 int x { 12 }; // 12 is assumed to be a decimal number \u5bf9\u4e8e\u4e8c\u8fdb\u5236\u6765\u8bf4\uff0c\u6570\u5b57\u53ea\u6709 0 \u548c 1 \u4e24\u79cd\u53ef\u80fd\uff0c\u6240\u4ee5\u624d\u53eb\u505a 2 \uff08\u201cbase 2\u201d\uff09\u3002\u4e8c\u8fdb\u5236\u8ba1\u6570\u65f6\u901a\u5e38\u662f\u8fd9\u6837\u7684\uff1a0, 1, 10, 11, 100, 101, 110, 111, \u2026 \u5f53\u7136\uff0c\u8ba1\u7b97\u673a\u6709\u65f6\u5019\u4e5f\u4f7f\u7528\u5176\u4ed6\u7684\u8fdb\u5236\uff0c\u4f8b\u5982\u516b\u8fdb\u5236\u548c\u5341\u516d\u8fdb\u5236\u3002 8\u8fdb\u5236\u548c16\u8fdb\u5236\u5b57\u9762\u91cf \u516b\u8fdb\u5236\u7684\u6570\u5b57\u67098\u79cd\u53ef\u80fd: 0, 1, 2, 3, 4, 5, 6, \u548c 7\u3002\u8ba1\u6570\u65b9\u5f0f\u7c7b\u4f3c\u4e8e 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, \u2026 \uff08\u6ce8\u610f\uff0c\u6ca1\u67098\u548c9\uff0c\u6240\u4ee5\u6211\u4eec\u4ece7\u8df3\u5230\u4e8610\uff08\u4e00\u96f6\uff09\uff09 |\u5341\u8fdb\u5236 |0 |1 |2 |3 |4 |5 |6 |7 |8 |9 |10 |11| |---|---|---|---|---|---|---|---|---|---|---|---| |\u516b\u8fdb\u5236 |0 |1 |2 |3 |4 |5 |6 |7 |10 |11 |12 |13| \u5982\u9700\u4f7f\u7528\u516b\u8fdb\u5236\uff0c\u5219\u9700\u8981\u5728\u6570\u5b57\u524d\u52a0\u4e00\u4e2a 0 \uff08\u96f6\uff09\u4f5c\u4e3a\u524d\u7f00\u3002 1 2 3 4 5 6 7 8 #include int main () { int x { 012 }; // 0 before the number means this is octal std :: cout << x ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 10 \u4e3a\u4ec0\u4e48\u6253\u5370\u7684\u662f 10 \u800c\u4e0d\u662f 12\uff1f\u56e0\u4e3a\u6253\u5370\u65f6\u4f7f\u7528\u7684\u662f\u5341\u8fdb\u5236\uff0c\u800c\u516b\u8fdb\u5236\u768412\u7b49\u4e8e10\u8fdb\u5236\u768410\u3002 \u516b\u8fdb\u5236\u5f88\u96be\u7528\uff0c\u6211\u5efa\u8bae\u4f60\u907f\u514d\u4f7f\u7528\u5b83\u3002 \u5341\u516d\u8fdb\u5236\u670916\u4e2a\u6570\uff1a0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10, 11, 12, \u2026 \u5341\u8fdb\u5236 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 \u5341\u516d\u8fdb\u5236 0 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 \u5982\u9700\u4f7f\u7528\u5341\u516d\u8fdb\u5236\uff0c\u5219\u9700\u8981\u5728\u6570\u5b57\u524d\u52a0\u4e00\u4e2a 0x \uff08\u96f6x\uff09\u4f5c\u4e3a\u524d\u7f00\u3002 1 2 3 4 5 6 7 8 #include int main () { int x { 0xF }; // 0x before the number means this is hexadecimal std :: cout << x ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 15 \u56e0\u4e3a16\u8fdb\u5236\u4e2d\u6bcf\u4e00\u4f4d\u6570\u5b57\u90fd\u670916\u79cd\u53ef\u80fd\uff0c\u6240\u4ee5\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u7684\u6570\u521a\u597d\u5360\u7528\u4e864\u4e2a\u4f4d\u3002\u56e0\u6b64\uff0c\u4e00\u5bf9\u5341\u516d\u8fdb\u5236\u6570\u521a\u597d\u53ef\u4ee5\u8868\u793a\u4e00\u4e2a\u5b57\u8282\uff088\u4e2a\u4f4d\uff09 \u3002 \u4e00\u4e2a32\u4f4d\u7684\u6574\u578b\u8868\u793a\u6210\u4e8c\u8fdb\u5236\u662f\u8fd9\u4e2a\u6837\u5b50\u7684\uff1a0011 1010 0111 1111 1001 1000 0010 0110\u3002\u8fd9\u4e2a\u4e8c\u8fdb\u5236\u6570\u975e\u5e38\u957f\u800c\u4e14\u90fd\u662f\u91cd\u590d\u7684\u6570\u5b57\uff0c\u8bfb\u8d77\u6765\u5c31\u5f88\u56f0\u96be\u3002\u800c\u5982\u679c\u5199\u6210\u5341\u516d\u8fdb\u5236\uff0c\u5219\u53d8\u6210\u4e86\uff1a3A7F 9826\u3002\u8fd9\u4e5f\u4f7f\u5f97\u5341\u516d\u8fdb\u5236\u6570\u6210\u4e3a\u4e86\u4e00\u79cd\u975e\u5e38\u9002\u5408\u7cbe\u7b80\u5730\u8868\u793a\u5185\u5b58\u4e2d\u503c\u7684\u65b9\u5f0f\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u901a\u5e38\u90fd\u4f1a\u4f7f\u7528\u5341\u516d\u8fdb\u5236\u6570\u6765\u8868\u793a\u5185\u5b58\u5730\u5740\u6216\u5185\u5b58\u4e2d\u7684\u539f\u59cb\u6570\u636e\u3002 \u5728 C++14\u4e4b\u524d\uff0c\u662f\u6ca1\u6709\u529e\u6cd5\u76f4\u63a5\u8d4b\u503c\u4e8c\u8fdb\u5236\u5b57\u9762\u91cf\u7684\uff0c\u4e0d\u8fc7\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5341\u516d\u8fdb\u5236\u503c\u6765\u66f2\u7ebf\u6551\u56fd\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { int bin {}; // assume 16-bit ints bin = 0x0001 ; // assign binary 0000 0000 0000 0001 to the variable bin = 0x0002 ; // assign binary 0000 0000 0000 0010 to the variable bin = 0x0004 ; // assign binary 0000 0000 0000 0100 to the variable bin = 0x0008 ; // assign binary 0000 0000 0000 1000 to the variable bin = 0x0010 ; // assign binary 0000 0000 0001 0000 to the variable bin = 0x0020 ; // assign binary 0000 0000 0010 0000 to the variable bin = 0x0040 ; // assign binary 0000 0000 0100 0000 to the variable bin = 0x0080 ; // assign binary 0000 0000 1000 0000 to the variable bin = 0x00FF ; // assign binary 0000 0000 1111 1111 to the variable bin = 0x00B3 ; // assign binary 0000 0000 1011 0011 to the variable bin = 0xF770 ; // assign binary 1111 0111 0111 0000 to the variable return 0 ; } \u5728 C++14 \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 0b \u524d\u7f00\u6765\u521b\u5efa\u4e8c\u8fdb\u5236\u5b57\u9762\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int bin {}; // assume 16-bit ints bin = 0b1 ; // assign binary 0000 0000 0000 0001 to the variable bin = 0b11 ; // assign binary 0000 0000 0000 0011 to the variable bin = 0b1010 ; // assign binary 0000 0000 0000 1010 to the variable bin = 0b11110000 ; // assign binary 0000 0000 1111 0000 to the variable return 0 ; } \u56e0\u4e3a\u4e8c\u8fdb\u5236\u5b57\u9762\u91cf\u4e00\u65e6\u957f\u4e86\u5c31\u5f88\u96be\u9605\u8bfb\uff0cC++14\u5f15\u5165\u4e86\u5355\u5f15\u53f7 ' \u4f5c\u4e3a\u5206\u9694\u7b26\u3002 1 2 3 4 5 6 7 8 9 #include int main () { int bin { 0b1011'0010 }; // assign binary 1011 0010 to the variable long value { 2'132'673'462 }; // much easier to read than 2132673462 return 0 ; } \u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u4e0d\u652f\u6301 C++14\uff0c\u90a3\u4e48\u4f60\u4f7f\u7528\u4e0a\u8ff0\u8868\u793a\u6cd5\u7684\u65f6\u5019\u5c06\u4f1a\u62a5\u9519\u3002 \u6b64\u5916\uff0c\u8bf7\u6ce8\u610f\u5206\u9694\u7b26\u4e0d\u80fd\u653e\u5728\u7b2c\u4e00\u4f4d\u524d\u9762\uff1a 1 int bin { 0 b ' 1011'0010 }; // error: ' used before first digit of value \u5341\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u6253\u5370 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cC++ \u4f1a\u6309\u7167\u5341\u8fdb\u5236\u6765\u6253\u5370\u6570\u503c\u3002\u4e0d\u8fc7\u4f60\u53ef\u4ee5\u544a\u8bc9\u5b83\u6253\u5370\u5176\u4ed6\u683c\u5f0f\u3002\u4f7f\u7528 std::dec \u3001 std::oct \u548c std::hex \u53ef\u4ee5\u8f7b\u677e\u6253\u5370\u5341\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u548c\u5341\u516d\u8fdb\u5236\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 12 }; std :: cout << x << '\\n' ; // decimal (by default) std :: cout << std :: hex << x << '\\n' ; // hexadecimal std :: cout << x << '\\n' ; // now hexadecimal std :: cout << std :: oct << x << '\\n' ; // octal std :: cout << std :: dec << x << '\\n' ; // return to decimal std :: cout << x << '\\n' ; // decimal return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 3 4 5 6 12 c c 14 12 12 \u4e8c\u8fdb\u5236\u6253\u5370 \u6253\u5370\u4e8c\u8fdb\u5236\u6709\u70b9\u590d\u6742\uff0c\u56e0\u4e3a std::cout \u672c\u8eab\u5e76\u4e0d\u652f\u6301\u6253\u5370\u4e8c\u8fdb\u5236\u3002\u5e78\u597d\uff0cC++\u6807\u51c6\u5e93\u91cc\u9762\u6709\u4e00\u4e2a\u53eb\u505a std::bitset \u7684\u7c7b\u578b\u53ef\u4ee5\u4e3a\u6211\u6240\u7528 (\u9700\u8981 \u5934\u6587\u4ef6)\u3002 \u4f7f\u7528 std::bitset \uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a std::bitset \u53d8\u91cf\uff0c\u5e76\u4e14\u544a\u8bc9 std::bitset \u6709\u591a\u5c11\u4f4d\u9700\u8981\u5b58\u50a8\u3002\u8fd9\u4e2a\u4f4d\u6570\u5fc5\u987b\u662f\u4e00\u4e2a\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 std::bitset \u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u65e0\u7b26\u53f7\u5b57\u9762\u91cf\u6765\u521d\u59cb\u5316\uff08\u4efb\u4f55\u683c\u5f0f\u90fd\u53ef\u4ee5\uff0c\u5341\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u6216\u8005\u4e8c\u8fdb\u5236\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // for std::bitset #include int main () { // std::bitset<8> \u8868\u793a\u8981\u5b58\u50a88\u4e2a\u4f4d std :: bitset < 8 > bin1 { 0b1100'0101 }; // binary literal for binary 1100 0101 std :: bitset < 8 > bin2 { 0xC5 }; // hexadecimal literal for binary 1100 0101 std :: cout << bin1 << ' ' << bin2 << '\\n' ; std :: cout << std :: bitset < 4 > { 0b1010 } << '\\n' ; // create a temporary std::bitset and print it return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 11000101 11000101 1010 \u4e0a\u9762\u4ee3\u7801\u4e2d\u7684\u8fd9\u4e00\u884c\uff1a 1 std :: cout << std :: bitset < 4 > { 0b1010 } << '\\n' ; // create a temporary std::bitset and print it \u521b\u5efa\u4e86\u4e00\u4e2a\u4e34\u65f6\u76844\u4e2a\u4f4d\u7684 std::bitset \u5bf9\u8c61\uff0c\u7136\u540e\u7528\u4e8c\u8fdb\u5236 0b1010 \u5bf9\u5176\u8fdb\u884c\u4e86\u521d\u59cb\u5316\u5e76\u6253\u5370\u4e3a\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0c\u968f\u540e\u5c31\u4e22\u5f03\u4e86\u8fd9\u4e2a\u4e34\u65f6\u7684\u5bf9\u8c61\u3002","title":"4.16 - \u6570\u503c\u7cfb\u7edf\uff08\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236\uff09"},{"location":"4-16-Numeral-systems-decimal-binary-hexadecimal-and-octal/#816","text":"\u516b\u8fdb\u5236\u7684\u6570\u5b57\u67098\u79cd\u53ef\u80fd: 0, 1, 2, 3, 4, 5, 6, \u548c 7\u3002\u8ba1\u6570\u65b9\u5f0f\u7c7b\u4f3c\u4e8e 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, \u2026 \uff08\u6ce8\u610f\uff0c\u6ca1\u67098\u548c9\uff0c\u6240\u4ee5\u6211\u4eec\u4ece7\u8df3\u5230\u4e8610\uff08\u4e00\u96f6\uff09\uff09 |\u5341\u8fdb\u5236 |0 |1 |2 |3 |4 |5 |6 |7 |8 |9 |10 |11| |---|---|---|---|---|---|---|---|---|---|---|---| |\u516b\u8fdb\u5236 |0 |1 |2 |3 |4 |5 |6 |7 |10 |11 |12 |13| \u5982\u9700\u4f7f\u7528\u516b\u8fdb\u5236\uff0c\u5219\u9700\u8981\u5728\u6570\u5b57\u524d\u52a0\u4e00\u4e2a 0 \uff08\u96f6\uff09\u4f5c\u4e3a\u524d\u7f00\u3002 1 2 3 4 5 6 7 8 #include int main () { int x { 012 }; // 0 before the number means this is octal std :: cout << x ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 10 \u4e3a\u4ec0\u4e48\u6253\u5370\u7684\u662f 10 \u800c\u4e0d\u662f 12\uff1f\u56e0\u4e3a\u6253\u5370\u65f6\u4f7f\u7528\u7684\u662f\u5341\u8fdb\u5236\uff0c\u800c\u516b\u8fdb\u5236\u768412\u7b49\u4e8e10\u8fdb\u5236\u768410\u3002 \u516b\u8fdb\u5236\u5f88\u96be\u7528\uff0c\u6211\u5efa\u8bae\u4f60\u907f\u514d\u4f7f\u7528\u5b83\u3002 \u5341\u516d\u8fdb\u5236\u670916\u4e2a\u6570\uff1a0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10, 11, 12, \u2026 \u5341\u8fdb\u5236 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 \u5341\u516d\u8fdb\u5236 0 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 \u5982\u9700\u4f7f\u7528\u5341\u516d\u8fdb\u5236\uff0c\u5219\u9700\u8981\u5728\u6570\u5b57\u524d\u52a0\u4e00\u4e2a 0x \uff08\u96f6x\uff09\u4f5c\u4e3a\u524d\u7f00\u3002 1 2 3 4 5 6 7 8 #include int main () { int x { 0xF }; // 0x before the number means this is hexadecimal std :: cout << x ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 15 \u56e0\u4e3a16\u8fdb\u5236\u4e2d\u6bcf\u4e00\u4f4d\u6570\u5b57\u90fd\u670916\u79cd\u53ef\u80fd\uff0c\u6240\u4ee5\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u7684\u6570\u521a\u597d\u5360\u7528\u4e864\u4e2a\u4f4d\u3002\u56e0\u6b64\uff0c\u4e00\u5bf9\u5341\u516d\u8fdb\u5236\u6570\u521a\u597d\u53ef\u4ee5\u8868\u793a\u4e00\u4e2a\u5b57\u8282\uff088\u4e2a\u4f4d\uff09 \u3002 \u4e00\u4e2a32\u4f4d\u7684\u6574\u578b\u8868\u793a\u6210\u4e8c\u8fdb\u5236\u662f\u8fd9\u4e2a\u6837\u5b50\u7684\uff1a0011 1010 0111 1111 1001 1000 0010 0110\u3002\u8fd9\u4e2a\u4e8c\u8fdb\u5236\u6570\u975e\u5e38\u957f\u800c\u4e14\u90fd\u662f\u91cd\u590d\u7684\u6570\u5b57\uff0c\u8bfb\u8d77\u6765\u5c31\u5f88\u56f0\u96be\u3002\u800c\u5982\u679c\u5199\u6210\u5341\u516d\u8fdb\u5236\uff0c\u5219\u53d8\u6210\u4e86\uff1a3A7F 9826\u3002\u8fd9\u4e5f\u4f7f\u5f97\u5341\u516d\u8fdb\u5236\u6570\u6210\u4e3a\u4e86\u4e00\u79cd\u975e\u5e38\u9002\u5408\u7cbe\u7b80\u5730\u8868\u793a\u5185\u5b58\u4e2d\u503c\u7684\u65b9\u5f0f\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u901a\u5e38\u90fd\u4f1a\u4f7f\u7528\u5341\u516d\u8fdb\u5236\u6570\u6765\u8868\u793a\u5185\u5b58\u5730\u5740\u6216\u5185\u5b58\u4e2d\u7684\u539f\u59cb\u6570\u636e\u3002 \u5728 C++14\u4e4b\u524d\uff0c\u662f\u6ca1\u6709\u529e\u6cd5\u76f4\u63a5\u8d4b\u503c\u4e8c\u8fdb\u5236\u5b57\u9762\u91cf\u7684\uff0c\u4e0d\u8fc7\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5341\u516d\u8fdb\u5236\u503c\u6765\u66f2\u7ebf\u6551\u56fd\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { int bin {}; // assume 16-bit ints bin = 0x0001 ; // assign binary 0000 0000 0000 0001 to the variable bin = 0x0002 ; // assign binary 0000 0000 0000 0010 to the variable bin = 0x0004 ; // assign binary 0000 0000 0000 0100 to the variable bin = 0x0008 ; // assign binary 0000 0000 0000 1000 to the variable bin = 0x0010 ; // assign binary 0000 0000 0001 0000 to the variable bin = 0x0020 ; // assign binary 0000 0000 0010 0000 to the variable bin = 0x0040 ; // assign binary 0000 0000 0100 0000 to the variable bin = 0x0080 ; // assign binary 0000 0000 1000 0000 to the variable bin = 0x00FF ; // assign binary 0000 0000 1111 1111 to the variable bin = 0x00B3 ; // assign binary 0000 0000 1011 0011 to the variable bin = 0xF770 ; // assign binary 1111 0111 0111 0000 to the variable return 0 ; } \u5728 C++14 \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 0b \u524d\u7f00\u6765\u521b\u5efa\u4e8c\u8fdb\u5236\u5b57\u9762\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int bin {}; // assume 16-bit ints bin = 0b1 ; // assign binary 0000 0000 0000 0001 to the variable bin = 0b11 ; // assign binary 0000 0000 0000 0011 to the variable bin = 0b1010 ; // assign binary 0000 0000 0000 1010 to the variable bin = 0b11110000 ; // assign binary 0000 0000 1111 0000 to the variable return 0 ; } \u56e0\u4e3a\u4e8c\u8fdb\u5236\u5b57\u9762\u91cf\u4e00\u65e6\u957f\u4e86\u5c31\u5f88\u96be\u9605\u8bfb\uff0cC++14\u5f15\u5165\u4e86\u5355\u5f15\u53f7 ' \u4f5c\u4e3a\u5206\u9694\u7b26\u3002 1 2 3 4 5 6 7 8 9 #include int main () { int bin { 0b1011'0010 }; // assign binary 1011 0010 to the variable long value { 2'132'673'462 }; // much easier to read than 2132673462 return 0 ; } \u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u4e0d\u652f\u6301 C++14\uff0c\u90a3\u4e48\u4f60\u4f7f\u7528\u4e0a\u8ff0\u8868\u793a\u6cd5\u7684\u65f6\u5019\u5c06\u4f1a\u62a5\u9519\u3002 \u6b64\u5916\uff0c\u8bf7\u6ce8\u610f\u5206\u9694\u7b26\u4e0d\u80fd\u653e\u5728\u7b2c\u4e00\u4f4d\u524d\u9762\uff1a 1 int bin { 0 b ' 1011'0010 }; // error: ' used before first digit of value","title":"8\u8fdb\u5236\u548c16\u8fdb\u5236\u5b57\u9762\u91cf"},{"location":"4-16-Numeral-systems-decimal-binary-hexadecimal-and-octal/#_1","text":"\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cC++ \u4f1a\u6309\u7167\u5341\u8fdb\u5236\u6765\u6253\u5370\u6570\u503c\u3002\u4e0d\u8fc7\u4f60\u53ef\u4ee5\u544a\u8bc9\u5b83\u6253\u5370\u5176\u4ed6\u683c\u5f0f\u3002\u4f7f\u7528 std::dec \u3001 std::oct \u548c std::hex \u53ef\u4ee5\u8f7b\u677e\u6253\u5370\u5341\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u548c\u5341\u516d\u8fdb\u5236\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 12 }; std :: cout << x << '\\n' ; // decimal (by default) std :: cout << std :: hex << x << '\\n' ; // hexadecimal std :: cout << x << '\\n' ; // now hexadecimal std :: cout << std :: oct << x << '\\n' ; // octal std :: cout << std :: dec << x << '\\n' ; // return to decimal std :: cout << x << '\\n' ; // decimal return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 3 4 5 6 12 c c 14 12 12","title":"\u5341\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u6253\u5370"},{"location":"4-16-Numeral-systems-decimal-binary-hexadecimal-and-octal/#_2","text":"\u6253\u5370\u4e8c\u8fdb\u5236\u6709\u70b9\u590d\u6742\uff0c\u56e0\u4e3a std::cout \u672c\u8eab\u5e76\u4e0d\u652f\u6301\u6253\u5370\u4e8c\u8fdb\u5236\u3002\u5e78\u597d\uff0cC++\u6807\u51c6\u5e93\u91cc\u9762\u6709\u4e00\u4e2a\u53eb\u505a std::bitset \u7684\u7c7b\u578b\u53ef\u4ee5\u4e3a\u6211\u6240\u7528 (\u9700\u8981 \u5934\u6587\u4ef6)\u3002 \u4f7f\u7528 std::bitset \uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a std::bitset \u53d8\u91cf\uff0c\u5e76\u4e14\u544a\u8bc9 std::bitset \u6709\u591a\u5c11\u4f4d\u9700\u8981\u5b58\u50a8\u3002\u8fd9\u4e2a\u4f4d\u6570\u5fc5\u987b\u662f\u4e00\u4e2a\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 std::bitset \u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u65e0\u7b26\u53f7\u5b57\u9762\u91cf\u6765\u521d\u59cb\u5316\uff08\u4efb\u4f55\u683c\u5f0f\u90fd\u53ef\u4ee5\uff0c\u5341\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u6216\u8005\u4e8c\u8fdb\u5236\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // for std::bitset #include int main () { // std::bitset<8> \u8868\u793a\u8981\u5b58\u50a88\u4e2a\u4f4d std :: bitset < 8 > bin1 { 0b1100'0101 }; // binary literal for binary 1100 0101 std :: bitset < 8 > bin2 { 0xC5 }; // hexadecimal literal for binary 1100 0101 std :: cout << bin1 << ' ' << bin2 << '\\n' ; std :: cout << std :: bitset < 4 > { 0b1010 } << '\\n' ; // create a temporary std::bitset and print it return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 11000101 11000101 1010 \u4e0a\u9762\u4ee3\u7801\u4e2d\u7684\u8fd9\u4e00\u884c\uff1a 1 std :: cout << std :: bitset < 4 > { 0b1010 } << '\\n' ; // create a temporary std::bitset and print it \u521b\u5efa\u4e86\u4e00\u4e2a\u4e34\u65f6\u76844\u4e2a\u4f4d\u7684 std::bitset \u5bf9\u8c61\uff0c\u7136\u540e\u7528\u4e8c\u8fdb\u5236 0b1010 \u5bf9\u5176\u8fdb\u884c\u4e86\u521d\u59cb\u5316\u5e76\u6253\u5370\u4e3a\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0c\u968f\u540e\u5c31\u4e22\u5f03\u4e86\u8fd9\u4e2a\u4e34\u65f6\u7684\u5bf9\u8c61\u3002","title":"\u4e8c\u8fdb\u5236\u6253\u5370"},{"location":"4-17-an-introduction-to-std-string/","tags":["string","input-manipulator"],"text":"Key Takeaway std::string \u4e0d\u80fd\u63a5\u6536\u7a7a\u683c\uff0c\u4e3a\u4e86\u8f93\u5165\u4e00\u6574\u884c\u6587\u672c\uff0c\u6700\u597d\u662f\u4f7f\u7528 std::getline() \u51fd\u6570 \u4f7f\u7528\u8f93\u5165\u6216\u8f93\u51fa\u64cd\u7eb5\u5668\uff0c\u53ef\u4ee5\u4fee\u6539 std::cin \u6216 std::cout \u7684\u884c\u4e3a\uff0c std::ws \u8f93\u5165\u64cd\u7eb5\u5668\u544a\u8bc9 std::cin \u8981\u5ffd\u7565\u4efb\u4f55\u524d\u7f6e\u7a7a\u767d \u8f93\u5165\u5b57\u7b26\u4e32\u65f6\uff0c\u6572\u51fb\u56de\u8f66\u4f1a\u5bfc\u81f4\u8f93\u5165\u6d41\u4e2d\u586b\u5165 \\n \uff0c\u5b83\u53ef\u80fd\u88ab\u63d0\u53d6\uff0c\u4e5f\u53ef\u80fd\u88ab\u7559\u5728\u6d41\u4e2d\uff0c\u8fd9\u53d6\u51b3\u4e8e\u63a5\u6536\u5b83\u7684\u53d8\u91cf\u662f\u4ec0\u4e48\u7c7b\u578b\u3002\u5982\u679c\u7559\u5728\u6d41\u4e2d\u5219\u4f1a\u5bfc\u81f4 std::getline \u8ba4\u4e3a\u8f93\u5165\u5df2\u7ecf\u5b8c\u6210 \u4f7f\u7528 std::string \u7684\u6210\u5458\u51fd\u6570 std::string::length() \u83b7\u53d6\u5b57\u7b26\u4e32\u957f\u5ea6\uff0c\u5b83\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a size_t \uff08\u65e0\u7b26\u53f7\u6574\u578b\uff09 \u4f60\u7f16\u5199\u7684\u7b2c\u4e00\u4e2a C++ \u7a0b\u5e8f\u53ef\u80fd\u662f\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << \"Hello, world! \\n \" ; return 0 ; } \u90a3\u4e48\uff0c\u201cHello, world!\u201d \u7a76\u7adf\u662f\u4ec0\u4e48\u5462\uff1f\u201cHello, world!\u201d \u662f\u4e00\u7ec4\u5b57\u7b26\u7684\u96c6\u5408\uff0c\u79f0\u4e3a\u5b57\u7b26\u4e32\u3002\u5728 C++ \u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u7b26\u4e32\u6765\u8868\u793a\u6587\u672c\uff0c\u4f8b\u5982\u540d\u5b57\u3001\u5730\u5740\u3001\u5355\u8bcd\u548c\u53e5\u5b50\u3002\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff08\u4f8b\u5982 \u201cHello, world!\\n\u201d\uff09\u88ab\u653e\u5728\u53cc\u5f15\u53f7\u4e2d\u95f4\uff0c\u4ee5\u8868\u660e\u5b83\u4eec\u662f\u5b57\u7b26\u4e32\u3002 \u56e0\u4e3a\u5b57\u7b26\u4e32\u5728\u7a0b\u5e8f\u4e2d\u5f88\u5e38\u89c1\uff0c\u6240\u4ee5\u5927\u591a\u6570\u73b0\u4ee3\u7f16\u7a0b\u8bed\u8a00\u90fd\u5305\u542b\u57fa\u7840\u5b57\u7b26\u4e32\u6570\u636e\u7c7b\u578b\u3002\u4e0d\u8fc7\uff0c\u5728 C++ \u4e2d\uff0c\u5b57\u7b26\u4e32\u5e76\u4e0d\u662f\u57fa\u7840\u6570\u636e\u7c7b\u578b\uff08\u5b83\u88ab\u79f0\u4e3a \u590d\u5408\u7c7b\u578b \uff0c\u5e76\u88ab\u5b9a\u4e49\u5728 C++ \u7684\u6807\u51c6\u5e93\u4e2d\uff0c\u800c\u4e0d\u662f\u4f5c\u4e3aC++\u6838\u5fc3\u7684\u4e00\u90e8\u5206\uff09\u3002\u4f46\u662f\uff0c\u5b57\u7b26\u4e32\u5f88\u7b80\u5355\u4e5f\u5f88\u6709\u7528\uff0c\u6240\u4ee5\u6211\u4eec\u51c6\u5907\u63d0\u65e9\u4ecb\u7ecd\u5b83\u800c\u4e0d\u662f\u7b49\u5230\u7b2c\u4e5d\u7ae0\u4ecb\u7ecd\u590d\u5408\u7c7b\u578b\u65f6\u518d\u4ecb\u7ecd\u5b83\u3002 std::string \u7b80\u4ecb \u4e3a\u4e86\u5728 C++ \u4e2d\u4f7f\u7528\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u9996\u5148\u5e94\u5f53 #include \u5934\u6587\u4ef6\u4ee5\u4fbf\u83b7\u53d6 std::string \u7684\u58f0\u660e\u3002\u8fd9\u4e00\u6b65\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5b9a\u4e49 std::string \u7c7b\u578b\u7684\u53d8\u91cf\u4e86\u3002 1 2 3 #include // allows use of std::string std :: string myName {}; // empty string \u548c\u5176\u4ed6\u666e\u901a\u53d8\u91cf\u4e00\u6837\uff0c\u4f60\u53ef\u4ee5\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u521d\u59cb\u5316\u548c\u8d4b\u503c\uff1a 1 2 std :: string myName { \"Alex\" }; // initialize myName with string literal \"Alex\" myName = \"John\" ; // assign variable myName the string literal \"John\" \u6ce8\u610f\uff0c\u5b57\u7b26\u4e32\u4e5f\u53ef\u4ee5\u8868\u793a\u6570\u5b57\uff1a 1 std :: string myID { \"45\" }; // \"45\" \u4e0d\u540c\u4e8e\u6574\u578b 45! \u5728\u5b57\u7b26\u4e32\u4e2d\uff0c\u6570\u5b57\u88ab\u5f53\u505a\u6587\u672c\u5904\u7406\uff0c\u56e0\u6b64\u5b83\u4eec\u4e0d\u80fd\u88ab\u5f53\u505a\u6570\u5b57\u6765\u4f7f\u7528\uff08\u4f8b\u5982\uff0c\u4f60\u4e0d\u80fd\u5c06\u5b83\u4eec\u76f8\u4e58\uff09\u3002C++\u5e76\u4e0d\u4f1a\u5c06\u5b57\u7b26\u4e32\u6570\u5b57\u81ea\u52a8\u8f6c\u6362\u4e3a\u6574\u578b\u6216\u6d6e\u70b9\u578b\u7684\u503c\u3002 \u5b57\u7b26\u4e32\u8f93\u51fa \u5b57\u7b26\u4e32\u53ef\u4ee5\u4f7f\u7528 std::cout \u8f93\u51fa\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { std :: string myName { \"Alex\" }; std :: cout << \"My name is: \" << myName << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 My name is: Alex \u5982\u679c\u662f\u7a7a\u5b57\u7b26\u4e32\uff0c\u5219\u4ec0\u4e48\u4e5f\u4e0d\u4f1a\u6253\u5370\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { std :: string empty { }; std :: cout << '[' << empty << ']' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 [] \u4f7f\u7528 std::cin \u8f93\u5165\u5b57\u7b26\u4e32 \u4f7f\u7528 std::cin \u8f93\u5165\u5b57\u7b26\u4e32\u53ef\u80fd\u4f1a\u6709\u4e00\u4e9b\u51fa\u4eba\u610f\u6599\u7684\u5730\u65b9\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: cout << \"Enter your full name: \" ; std :: string name {}; std :: cin >> name ; // \u53ef\u80fd\u5e76\u4e0d\u80fd\u5982\u613f\u5de5\u4f5c\uff0c\u56e0\u4e3a std::cin \u662f\u7a7a\u683c\u5206\u5272\u7684 std :: cout << \"Enter your age: \" ; std :: string age {}; std :: cin >> age ; std :: cout << \"Your name is \" << name << \" and your age is \" << age << '\\n' ; return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Enter your full name: John Doe Enter your age: Your name is John and your age is Doe \u5b8c\u5168\u4e0d\u5bf9\u561b\uff01\u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\uff1f\u5b9e\u9645\u4e0a\uff0c\u5728\u4f7f\u7528 operator>> \u63d0\u53d6\u5b57\u7b26\u4e32\u5230 cin \u7684\u65f6\u5019\uff0c operator>> \u53ea\u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u7a7a\u683c\u524d\u7684\u5b57\u7b26\u3002\u5176\u4ed6\u7684\u5b57\u7b26\u90fd\u4f1a\u88ab\u7559\u5728 std::cin \u4e2d\u4f9b\u4e0b\u4e00\u6b21\u63d0\u53d6\u3002 \u56e0\u6b64\u5f53\u6211\u4eec\u7528 operator>> \u63d0\u53d6\u5b57\u7b26\u4e32\u5230 name \u65f6\uff0c\u53ea\u6709 \"John\" \u88ab\u63d0\u53d6\u4e86\uff0c\u800c \" Doe\" \u5219\u88ab\u7559\u5728\u4e86 std::cin \u7684\u7f13\u51b2\u4e2d\u3002\u968f\u540e\u5728\u4f7f\u7528 operator>> \u83b7\u53d6 age \u7684\u65f6\u5019\uff0c\u5b83\u5c31\u4f1a\u63d0\u53d6 \"Doe\" \u800c\u4e0d\u662f\u5e74\u9f84\u3002\u7136\u540e\u7a0b\u5e8f\u5c31\u622a\u6b62\u4e86\u3002 \u4f7f\u7528 std::getline() \u8f93\u5165\u6587\u672c \u4e3a\u4e86\u8f93\u5165\u4e00\u6574\u884c\u6587\u672c\uff0c\u6700\u597d\u662f\u4f7f\u7528 std::getline() \u51fd\u6570\u3002 std::getline() \u53ef\u4ee5\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f std::cin \uff0c\u7b2c\u4e8c\u4e2a\u5219\u662f\u5b57\u7b26\u4e32\u53d8\u91cf\u3002 \u76f8\u540c\u7684\u7a0b\u5e8f\uff0c\u4f7f\u7528 std::getline() \u6765\u8fdb\u884c\u8f93\u5165\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // For std::string and std::getline #include int main () { std :: cout << \"Enter your full name: \" ; std :: string name {}; std :: getline ( std :: cin >> std :: ws , name ); // read a full line of text into name std :: cout << \"Enter your age: \" ; std :: string age {}; std :: getline ( std :: cin >> std :: ws , age ); // read a full line of text into age std :: cout << \"Your name is \" << name << \" and your age is \" << age << '\\n' ; return 0 ; } \u73b0\u5728\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\uff1a 1 2 3 Enter your full name: John Doe Enter your age: 23 Your name is John Doe and your age is 23 std::ws \u53c8\u662f\u4ec0\u4e48\uff1f \u5728 4.8 - \u6d6e\u70b9\u6570 \u4e2d\u6211\u4eec\u8ba8\u8bba\u4e86 \u8f93\u51fa\u64cd\u7eb5\u5668(output manipulators) \uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u6539\u53d8\u8f93\u51fa\u7684\u65b9\u5f0f\u3002\u5f53\u65f6\uff0c\u6211\u4eec\u4f7f\u7528\u8f93\u51fa\u64cd\u7eb5\u51fd\u6570 std::setprecision() \u4fee\u6539\u4e86 std::cout \u7684\u8f93\u51fa\u7cbe\u5ea6\u3002 C++ \u5f53\u7136\u4e5f\u63d0\u4f9b\u4e86 \u8f93\u5165\u64cd\u7eb5\u5668(input manipulators) \uff0c\u5b83\u53ef\u4ee5\u4fee\u6539\u8f93\u5165\u7684\u65b9\u5f0f\u3002 std::ws \u8f93\u5165\u64cd\u7eb5\u5668\u544a\u8bc9 std::cin \u8981\u5ffd\u7565\u4efb\u4f55\u524d\u7f6e\u7a7a\u767d\u3002 \u901a\u8fc7\u4e0b\u9762\u7684\u7a0b\u5e8f\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u5b83\u6709\u4ec0\u4e48\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: cout << \"Pick 1 or 2: \" ; int choice {}; std :: cin >> choice ; std :: cout << \"Now enter your name: \" ; std :: string name {}; std :: getline ( std :: cin , name ); // \u6ce8\u610f\uff1a\u6ca1\u6709\u4f7f\u7528 std::ws std :: cout << \"Hello, \" << name << \", you picked \" << choice << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Pick 1 or 2: 2 Now enter your name: Hello, , you picked 2 \u7a0b\u5e8f\u9996\u5148\u63d0\u9192\u4f60\u8f93\u5165 1 \u6216\u8005 2\uff0c\u7136\u540e\u7b49\u5f85\u4f60\u7684\u8f93\u5165\u3002\u5230\u76ee\u524d\u4e3a\u6b62\u90fd\u6ca1\u6709\u4ec0\u4e48\u95ee\u9898\u3002\u7136\u540e\u7a0b\u5e8f\u53c8\u8ba9\u4f60\u8f93\u5165\u59d3\u540d\uff0c\u4f46\u662f\u5b83\u5e76\u6ca1\u6709\u7b49\u5f85\u4f60\u8f93\u5165\u5c31\u6253\u5370\u4e86\"Hello\"\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5c31\u9000\u51fa\u4e86\u3002 \u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\uff1f \u5b9e\u9645\u4e0a\uff0c\u5f53\u4f60\u4f7f\u7528 operator>> \u83b7\u53d6\u8f93\u5165\u7684\u65f6\u5019\uff0c std::cin \u5e76\u4e0d\u662f\u4ec5\u4ec5\u6355\u83b7\u503c\uff0c\u5b83\u4e5f\u6355\u83b7\u6362\u884c\u7b26 \\n \uff08\u5f53\u4f60\u6572\u4e0b\u56de\u8f66\u7684\u65f6\u5019\uff09\u3002\u6240\u4ee5\uff0c\u5728\u4f60\u8f93\u5165 2 \u5e76\u6572\u51fb\u56de\u8f66\u540e\uff0c std::cin \u5f97\u5230\u7684\u8f93\u5165\u5b57\u7b26\u4e32\u5176\u5b9e\u662f \"2\\n\" \u3002\u968f\u540e\uff0c2\u88ab\u63d0\u53d6\u5230\u4e86 choice \uff0c\u800c\u6362\u884c\u7b26\u5c31\u88ab\u7559\u5230\u4e86\u7f13\u51b2\u4e2d\u3002\u7136\u540e\uff0c\u5f53 std::getline() \u5f00\u59cb\u8bfb\u53d6\u59d3\u540d\u65f6\uff0c\u5b83\u53d1\u73b0 \"\\n\" \u5df2\u7ecf\u5728\u8f93\u5165\u6d41\u4e2d\u4e86\uff0c\u6240\u4ee5\u5b83\u8f93\u5165\u5728\u4e4b\u524d\u80af\u5b9a\u5df2\u7ecf\u5b8c\u6210\uff08\u8f93\u5165\u7684\u662f\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\uff09\uff01\u8fd9\u5f53\u7136\u5e76\u4e0d\u662f\u6211\u4eec\u5e0c\u671b\u7684\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 std::ws \u4fee\u6539\u8be5\u7a0b\u5e8f\uff0c\u8fd9\u6837 std::getline() \u5c31\u4f1a\u5ffd\u7565\u4efb\u4f55\u524d\u7f6e\u7684\u7a7a\u767d\u5b57\u7b26\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: cout << \"Pick 1 or 2: \" ; int choice {}; std :: cin >> choice ; std :: cout << \"Now enter your name: \" ; std :: string name {}; std :: getline ( std :: cin >> std :: ws , name ); // note: added std::ws here std :: cout << \"Hello, \" << name << \", you picked \" << choice << '\\n' ; return 0 ; } \u73b0\u5728\uff0c \u7a0b\u5e8f\u5c31\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\u3002 1 2 3 Pick 1 or 2: 2 Now enter your name: Alex Hello, Alex, you picked 2 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f7f\u7528 std::getline \u6765\u8bfb\u53d6\u5b57\u7b26\u4e32\uff0c\u8bf7\u4f7f\u7528 std::ws \u5ffd\u7565\u524d\u7f6e\u7684\u7a7a\u767d \u5173\u952e\u4fe1\u606f \u4f7f\u7528\u63d0\u53d6\u64cd\u4f5c\u7b26( >> ) \u548c std::cin \u6765\u5ffd\u7565\u524d\u7f6e\u7a7a\u767d\u3002 \u9664\u975e\u4f7f\u7528 std::ws \u6307\u660e\uff0c\u5426\u5219 std::getline \u5e76\u4e0d\u4f1a\u5ffd\u7565\u524d\u7f6e\u7a7a\u767d\u3002 \u5b57\u7b26\u4e32\u957f\u5ea6 \u5982\u679c\u60f3\u8981\u77e5\u9053 std::string \u5305\u542b\u591a\u5c11\u5b57\u7b26\uff0c\u5219\u53ef\u4ee5\u67e5\u8be2 std::string \u7684\u957f\u5ea6\uff0c\u5176\u8bed\u6cd5\u53ef\u80fd\u548c\u4e4b\u524d\u6211\u4eec\u63a5\u89e6\u5230\u7684\u4e0d\u592a\u4e00\u6837\uff0c\u4f46\u662f\u975e\u5e38\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 #include #include int main () { std :: string myName { \"Alex\" }; std :: cout << myName << \" has \" << myName . length () << \" characters \\n \" ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 Alex has 4 characters \u6ce8\u610f\uff0c\u5e76\u4e0d\u662f\u901a\u8fc7 length(myName) \u8fd9\u79cd\u5f62\u5f0f\u6765\u67e5\u8be2\uff0c\u800c\u662f myName.length() \u3002\u56e0\u4e3a length() \u51fd\u6570\u5e76\u4e0d\u662f\u4e00\u4e2a\u666e\u901a\u7684\u3001\u72ec\u7acb\u7684\u51fd\u6570\u2014\u2014\u5b83\u662f\u4e00\u7c7b\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u5d4c\u5165\u5728 std::string \uff0c\u79f0\u4e3a \u6210\u5458\u51fd\u6570(member function) \u3002\u56e0\u4e3a length() \u5b58\u5728\u4e8e std::string \u5185\u90e8\uff0c\u56e0\u6b64\u5728\u6587\u6863\u4e2d\u6709\u65f6\u4f1a\u5199\u4f5c std::string::length \u3002 \u6211\u4eec\u4f1a\u5728\u7a0d\u540e\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u548c\u7f16\u5199\u6210\u5458\u51fd\u6570\u3002 \u540c\u65f6\uff0c\u8fd8\u8981\u6ce8\u610f\u5230 std::string::length() \u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u65e0\u7b26\u53f7\u6574\u578b\u6570\uff08\u5f88\u53ef\u80fd\u662f size_t \uff09\u3002\u5982\u679c\u4f60\u5e0c\u671b\u5c06\u5b83\u8d4b\u503c\u7ed9\u4e00\u4e2a int \u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528 static_cast \uff0c\u8fd9\u6837\u624d\u80fd\u907f\u514d signed/unsigned \u8f6c\u6362\u5bfc\u81f4\u7684\u7f16\u8bd1\u5668\u544a\u8b66\uff1a 1 int length = static_cast < int > ( myName . length ()); In C++20, you can also use the std::ssize() function to get the length of a std::string as a signed integer: 1 2 3 4 5 6 7 8 9 10 #include #include int main () { std :: string name { \"Alex\" }; std :: cout << name << \" has \" << std :: ssize ( name ) << \" characters \\n \" ; return 0 ; } COPY std::string is expensive to initialize and copy Whenever a std::string is initialized, a copy of the string used to initialize it is made. And whenever a std::string is passed by value to a std::string parameter, another copy is made. These copies are expensive, and should be avoided if possible. Best practice Do not pass std::string by value, as making copies of std::string is expensive. Prefer std::string_view parameters. We\u2019ll discuss this topic (and std::string_view ) further in lesson 4.18 - std::string_view \u7b80\u4ecb Literals for std::string Double-quoted string literals (like \u201cHello, world!\u201d) are C-style strings by default (and thus, have a strange type). We can create string literals with type std::string by using a s suffix after the double-quoted string literal. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include #include // for std::string #include // for std::string_view int main () { using namespace std :: literals ; // easiest way to access the s and sv suffixes std :: cout << \"foo \\n \" ; // no suffix is a C-style string literal std :: cout << \"goo \\n \" s ; // s suffix is a std::string literal std :: cout << \"moo \\n \" sv ; // sv suffix is a std::string_view literal return 0 ; }; \u5c0f\u8d34\u58eb The \u201cs\u201d suffix lives in the namespace std::literals::string_literals . The easiest way to access the literal suffixes is via using directive using namespace std::literals . We discuss using directives in lesson 6.12 -- Using declarations and using directives . This is one of the exception cases where using an entire namespace is okay. You probably won\u2019t need to use std::string literals very often (as it\u2019s fine to initialize a std::string object with a C-style string literal), but we\u2019ll see a few cases in future lessons where using std::string literals instead of C-style string literals makes things easier. Constexpr strings If you try to define a constexpr std::string , your compiler will probably generate an error: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include using namespace std :: literals ; int main () { constexpr std :: string name { \"Alex\" s }; // compile error std :: cout << \"My name is: \" << name ; return 0 ; } This happens because constexpr std::string isn\u2019t supported in C++17 or earlier, and only has minimal support in C++20. If you need constexpr strings, use std::string_view instead (discussed in lesson 4.18 -- Introduction to std::string_view . \u7ed3\u8bba std::string \u5f88\u590d\u6742\uff0c\u800c\u4e14\u4f7f\u7528\u4e86\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u5230\u7684\u591a\u79cd\u8bed\u8a00\u7279\u6027\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u5bf9\u4e8e\u5b8c\u6210\u7b80\u5355\u7684\u4efb\u52a1\u6765\u8bf4\u6765\u8bf4\uff0c\u4f60\u5e76\u4e0d\u9700\u8981\u5b8c\u5168\u7406\u89e3\u5b83\uff0c\u6bd4\u5982\u57fa\u672c\u7684\u8f93\u5165\u8f93\u51fa\u3002\u5efa\u8bae\u4f60\u4ece\u73b0\u5728\u5f00\u59cb\u5c31\u53ef\u4ee5\u591a\u5c1d\u8bd5\u5e76\u63a2\u7d22z\u5b57\u7b26\u4e32\u7684\u4f7f\u7528\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u5185\u5bb9\u4e2d\u4ecb\u7ecdstring\u7684\u5176\u4ed6\u529f\u80fd\u3002","title":"4.17 - std::string \u7b80\u4ecb"},{"location":"4-17-an-introduction-to-std-string/#stdstring","text":"\u4e3a\u4e86\u5728 C++ \u4e2d\u4f7f\u7528\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u9996\u5148\u5e94\u5f53 #include \u5934\u6587\u4ef6\u4ee5\u4fbf\u83b7\u53d6 std::string \u7684\u58f0\u660e\u3002\u8fd9\u4e00\u6b65\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5b9a\u4e49 std::string \u7c7b\u578b\u7684\u53d8\u91cf\u4e86\u3002 1 2 3 #include // allows use of std::string std :: string myName {}; // empty string \u548c\u5176\u4ed6\u666e\u901a\u53d8\u91cf\u4e00\u6837\uff0c\u4f60\u53ef\u4ee5\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u521d\u59cb\u5316\u548c\u8d4b\u503c\uff1a 1 2 std :: string myName { \"Alex\" }; // initialize myName with string literal \"Alex\" myName = \"John\" ; // assign variable myName the string literal \"John\" \u6ce8\u610f\uff0c\u5b57\u7b26\u4e32\u4e5f\u53ef\u4ee5\u8868\u793a\u6570\u5b57\uff1a 1 std :: string myID { \"45\" }; // \"45\" \u4e0d\u540c\u4e8e\u6574\u578b 45! \u5728\u5b57\u7b26\u4e32\u4e2d\uff0c\u6570\u5b57\u88ab\u5f53\u505a\u6587\u672c\u5904\u7406\uff0c\u56e0\u6b64\u5b83\u4eec\u4e0d\u80fd\u88ab\u5f53\u505a\u6570\u5b57\u6765\u4f7f\u7528\uff08\u4f8b\u5982\uff0c\u4f60\u4e0d\u80fd\u5c06\u5b83\u4eec\u76f8\u4e58\uff09\u3002C++\u5e76\u4e0d\u4f1a\u5c06\u5b57\u7b26\u4e32\u6570\u5b57\u81ea\u52a8\u8f6c\u6362\u4e3a\u6574\u578b\u6216\u6d6e\u70b9\u578b\u7684\u503c\u3002","title":"std::string \u7b80\u4ecb"},{"location":"4-17-an-introduction-to-std-string/#_1","text":"\u5b57\u7b26\u4e32\u53ef\u4ee5\u4f7f\u7528 std::cout \u8f93\u51fa\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { std :: string myName { \"Alex\" }; std :: cout << \"My name is: \" << myName << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 My name is: Alex \u5982\u679c\u662f\u7a7a\u5b57\u7b26\u4e32\uff0c\u5219\u4ec0\u4e48\u4e5f\u4e0d\u4f1a\u6253\u5370\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { std :: string empty { }; std :: cout << '[' << empty << ']' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 []","title":"\u5b57\u7b26\u4e32\u8f93\u51fa"},{"location":"4-17-an-introduction-to-std-string/#stdcin","text":"\u4f7f\u7528 std::cin \u8f93\u5165\u5b57\u7b26\u4e32\u53ef\u80fd\u4f1a\u6709\u4e00\u4e9b\u51fa\u4eba\u610f\u6599\u7684\u5730\u65b9\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: cout << \"Enter your full name: \" ; std :: string name {}; std :: cin >> name ; // \u53ef\u80fd\u5e76\u4e0d\u80fd\u5982\u613f\u5de5\u4f5c\uff0c\u56e0\u4e3a std::cin \u662f\u7a7a\u683c\u5206\u5272\u7684 std :: cout << \"Enter your age: \" ; std :: string age {}; std :: cin >> age ; std :: cout << \"Your name is \" << name << \" and your age is \" << age << '\\n' ; return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Enter your full name: John Doe Enter your age: Your name is John and your age is Doe \u5b8c\u5168\u4e0d\u5bf9\u561b\uff01\u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\uff1f\u5b9e\u9645\u4e0a\uff0c\u5728\u4f7f\u7528 operator>> \u63d0\u53d6\u5b57\u7b26\u4e32\u5230 cin \u7684\u65f6\u5019\uff0c operator>> \u53ea\u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u7a7a\u683c\u524d\u7684\u5b57\u7b26\u3002\u5176\u4ed6\u7684\u5b57\u7b26\u90fd\u4f1a\u88ab\u7559\u5728 std::cin \u4e2d\u4f9b\u4e0b\u4e00\u6b21\u63d0\u53d6\u3002 \u56e0\u6b64\u5f53\u6211\u4eec\u7528 operator>> \u63d0\u53d6\u5b57\u7b26\u4e32\u5230 name \u65f6\uff0c\u53ea\u6709 \"John\" \u88ab\u63d0\u53d6\u4e86\uff0c\u800c \" Doe\" \u5219\u88ab\u7559\u5728\u4e86 std::cin \u7684\u7f13\u51b2\u4e2d\u3002\u968f\u540e\u5728\u4f7f\u7528 operator>> \u83b7\u53d6 age \u7684\u65f6\u5019\uff0c\u5b83\u5c31\u4f1a\u63d0\u53d6 \"Doe\" \u800c\u4e0d\u662f\u5e74\u9f84\u3002\u7136\u540e\u7a0b\u5e8f\u5c31\u622a\u6b62\u4e86\u3002","title":"\u4f7f\u7528 std::cin \u8f93\u5165\u5b57\u7b26\u4e32"},{"location":"4-17-an-introduction-to-std-string/#stdgetline","text":"\u4e3a\u4e86\u8f93\u5165\u4e00\u6574\u884c\u6587\u672c\uff0c\u6700\u597d\u662f\u4f7f\u7528 std::getline() \u51fd\u6570\u3002 std::getline() \u53ef\u4ee5\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f std::cin \uff0c\u7b2c\u4e8c\u4e2a\u5219\u662f\u5b57\u7b26\u4e32\u53d8\u91cf\u3002 \u76f8\u540c\u7684\u7a0b\u5e8f\uff0c\u4f7f\u7528 std::getline() \u6765\u8fdb\u884c\u8f93\u5165\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include // For std::string and std::getline #include int main () { std :: cout << \"Enter your full name: \" ; std :: string name {}; std :: getline ( std :: cin >> std :: ws , name ); // read a full line of text into name std :: cout << \"Enter your age: \" ; std :: string age {}; std :: getline ( std :: cin >> std :: ws , age ); // read a full line of text into age std :: cout << \"Your name is \" << name << \" and your age is \" << age << '\\n' ; return 0 ; } \u73b0\u5728\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\uff1a 1 2 3 Enter your full name: John Doe Enter your age: 23 Your name is John Doe and your age is 23","title":"\u4f7f\u7528 std::getline() \u8f93\u5165\u6587\u672c"},{"location":"4-17-an-introduction-to-std-string/#stdws","text":"\u5728 4.8 - \u6d6e\u70b9\u6570 \u4e2d\u6211\u4eec\u8ba8\u8bba\u4e86 \u8f93\u51fa\u64cd\u7eb5\u5668(output manipulators) \uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u6539\u53d8\u8f93\u51fa\u7684\u65b9\u5f0f\u3002\u5f53\u65f6\uff0c\u6211\u4eec\u4f7f\u7528\u8f93\u51fa\u64cd\u7eb5\u51fd\u6570 std::setprecision() \u4fee\u6539\u4e86 std::cout \u7684\u8f93\u51fa\u7cbe\u5ea6\u3002 C++ \u5f53\u7136\u4e5f\u63d0\u4f9b\u4e86 \u8f93\u5165\u64cd\u7eb5\u5668(input manipulators) \uff0c\u5b83\u53ef\u4ee5\u4fee\u6539\u8f93\u5165\u7684\u65b9\u5f0f\u3002 std::ws \u8f93\u5165\u64cd\u7eb5\u5668\u544a\u8bc9 std::cin \u8981\u5ffd\u7565\u4efb\u4f55\u524d\u7f6e\u7a7a\u767d\u3002 \u901a\u8fc7\u4e0b\u9762\u7684\u7a0b\u5e8f\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u5b83\u6709\u4ec0\u4e48\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: cout << \"Pick 1 or 2: \" ; int choice {}; std :: cin >> choice ; std :: cout << \"Now enter your name: \" ; std :: string name {}; std :: getline ( std :: cin , name ); // \u6ce8\u610f\uff1a\u6ca1\u6709\u4f7f\u7528 std::ws std :: cout << \"Hello, \" << name << \", you picked \" << choice << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 Pick 1 or 2: 2 Now enter your name: Hello, , you picked 2 \u7a0b\u5e8f\u9996\u5148\u63d0\u9192\u4f60\u8f93\u5165 1 \u6216\u8005 2\uff0c\u7136\u540e\u7b49\u5f85\u4f60\u7684\u8f93\u5165\u3002\u5230\u76ee\u524d\u4e3a\u6b62\u90fd\u6ca1\u6709\u4ec0\u4e48\u95ee\u9898\u3002\u7136\u540e\u7a0b\u5e8f\u53c8\u8ba9\u4f60\u8f93\u5165\u59d3\u540d\uff0c\u4f46\u662f\u5b83\u5e76\u6ca1\u6709\u7b49\u5f85\u4f60\u8f93\u5165\u5c31\u6253\u5370\u4e86\"Hello\"\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5c31\u9000\u51fa\u4e86\u3002 \u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\uff1f \u5b9e\u9645\u4e0a\uff0c\u5f53\u4f60\u4f7f\u7528 operator>> \u83b7\u53d6\u8f93\u5165\u7684\u65f6\u5019\uff0c std::cin \u5e76\u4e0d\u662f\u4ec5\u4ec5\u6355\u83b7\u503c\uff0c\u5b83\u4e5f\u6355\u83b7\u6362\u884c\u7b26 \\n \uff08\u5f53\u4f60\u6572\u4e0b\u56de\u8f66\u7684\u65f6\u5019\uff09\u3002\u6240\u4ee5\uff0c\u5728\u4f60\u8f93\u5165 2 \u5e76\u6572\u51fb\u56de\u8f66\u540e\uff0c std::cin \u5f97\u5230\u7684\u8f93\u5165\u5b57\u7b26\u4e32\u5176\u5b9e\u662f \"2\\n\" \u3002\u968f\u540e\uff0c2\u88ab\u63d0\u53d6\u5230\u4e86 choice \uff0c\u800c\u6362\u884c\u7b26\u5c31\u88ab\u7559\u5230\u4e86\u7f13\u51b2\u4e2d\u3002\u7136\u540e\uff0c\u5f53 std::getline() \u5f00\u59cb\u8bfb\u53d6\u59d3\u540d\u65f6\uff0c\u5b83\u53d1\u73b0 \"\\n\" \u5df2\u7ecf\u5728\u8f93\u5165\u6d41\u4e2d\u4e86\uff0c\u6240\u4ee5\u5b83\u8f93\u5165\u5728\u4e4b\u524d\u80af\u5b9a\u5df2\u7ecf\u5b8c\u6210\uff08\u8f93\u5165\u7684\u662f\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\uff09\uff01\u8fd9\u5f53\u7136\u5e76\u4e0d\u662f\u6211\u4eec\u5e0c\u671b\u7684\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 std::ws \u4fee\u6539\u8be5\u7a0b\u5e8f\uff0c\u8fd9\u6837 std::getline() \u5c31\u4f1a\u5ffd\u7565\u4efb\u4f55\u524d\u7f6e\u7684\u7a7a\u767d\u5b57\u7b26\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include int main () { std :: cout << \"Pick 1 or 2: \" ; int choice {}; std :: cin >> choice ; std :: cout << \"Now enter your name: \" ; std :: string name {}; std :: getline ( std :: cin >> std :: ws , name ); // note: added std::ws here std :: cout << \"Hello, \" << name << \", you picked \" << choice << '\\n' ; return 0 ; } \u73b0\u5728\uff0c \u7a0b\u5e8f\u5c31\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\u3002 1 2 3 Pick 1 or 2: 2 Now enter your name: Alex Hello, Alex, you picked 2 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f7f\u7528 std::getline \u6765\u8bfb\u53d6\u5b57\u7b26\u4e32\uff0c\u8bf7\u4f7f\u7528 std::ws \u5ffd\u7565\u524d\u7f6e\u7684\u7a7a\u767d \u5173\u952e\u4fe1\u606f \u4f7f\u7528\u63d0\u53d6\u64cd\u4f5c\u7b26( >> ) \u548c std::cin \u6765\u5ffd\u7565\u524d\u7f6e\u7a7a\u767d\u3002 \u9664\u975e\u4f7f\u7528 std::ws \u6307\u660e\uff0c\u5426\u5219 std::getline \u5e76\u4e0d\u4f1a\u5ffd\u7565\u524d\u7f6e\u7a7a\u767d\u3002","title":"std::ws \u53c8\u662f\u4ec0\u4e48\uff1f"},{"location":"4-17-an-introduction-to-std-string/#_2","text":"\u5982\u679c\u60f3\u8981\u77e5\u9053 std::string \u5305\u542b\u591a\u5c11\u5b57\u7b26\uff0c\u5219\u53ef\u4ee5\u67e5\u8be2 std::string \u7684\u957f\u5ea6\uff0c\u5176\u8bed\u6cd5\u53ef\u80fd\u548c\u4e4b\u524d\u6211\u4eec\u63a5\u89e6\u5230\u7684\u4e0d\u592a\u4e00\u6837\uff0c\u4f46\u662f\u975e\u5e38\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 #include #include int main () { std :: string myName { \"Alex\" }; std :: cout << myName << \" has \" << myName . length () << \" characters \\n \" ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 Alex has 4 characters \u6ce8\u610f\uff0c\u5e76\u4e0d\u662f\u901a\u8fc7 length(myName) \u8fd9\u79cd\u5f62\u5f0f\u6765\u67e5\u8be2\uff0c\u800c\u662f myName.length() \u3002\u56e0\u4e3a length() \u51fd\u6570\u5e76\u4e0d\u662f\u4e00\u4e2a\u666e\u901a\u7684\u3001\u72ec\u7acb\u7684\u51fd\u6570\u2014\u2014\u5b83\u662f\u4e00\u7c7b\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u5d4c\u5165\u5728 std::string \uff0c\u79f0\u4e3a \u6210\u5458\u51fd\u6570(member function) \u3002\u56e0\u4e3a length() \u5b58\u5728\u4e8e std::string \u5185\u90e8\uff0c\u56e0\u6b64\u5728\u6587\u6863\u4e2d\u6709\u65f6\u4f1a\u5199\u4f5c std::string::length \u3002 \u6211\u4eec\u4f1a\u5728\u7a0d\u540e\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u548c\u7f16\u5199\u6210\u5458\u51fd\u6570\u3002 \u540c\u65f6\uff0c\u8fd8\u8981\u6ce8\u610f\u5230 std::string::length() \u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u65e0\u7b26\u53f7\u6574\u578b\u6570\uff08\u5f88\u53ef\u80fd\u662f size_t \uff09\u3002\u5982\u679c\u4f60\u5e0c\u671b\u5c06\u5b83\u8d4b\u503c\u7ed9\u4e00\u4e2a int \u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528 static_cast \uff0c\u8fd9\u6837\u624d\u80fd\u907f\u514d signed/unsigned \u8f6c\u6362\u5bfc\u81f4\u7684\u7f16\u8bd1\u5668\u544a\u8b66\uff1a 1 int length = static_cast < int > ( myName . length ()); In C++20, you can also use the std::ssize() function to get the length of a std::string as a signed integer: 1 2 3 4 5 6 7 8 9 10 #include #include int main () { std :: string name { \"Alex\" }; std :: cout << name << \" has \" << std :: ssize ( name ) << \" characters \\n \" ; return 0 ; } COPY std::string is expensive to initialize and copy Whenever a std::string is initialized, a copy of the string used to initialize it is made. And whenever a std::string is passed by value to a std::string parameter, another copy is made. These copies are expensive, and should be avoided if possible. Best practice Do not pass std::string by value, as making copies of std::string is expensive. Prefer std::string_view parameters. We\u2019ll discuss this topic (and std::string_view ) further in lesson 4.18 - std::string_view \u7b80\u4ecb","title":"\u5b57\u7b26\u4e32\u957f\u5ea6"},{"location":"4-17-an-introduction-to-std-string/#literals-for-stdstring","text":"Double-quoted string literals (like \u201cHello, world!\u201d) are C-style strings by default (and thus, have a strange type). We can create string literals with type std::string by using a s suffix after the double-quoted string literal. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include #include // for std::string #include // for std::string_view int main () { using namespace std :: literals ; // easiest way to access the s and sv suffixes std :: cout << \"foo \\n \" ; // no suffix is a C-style string literal std :: cout << \"goo \\n \" s ; // s suffix is a std::string literal std :: cout << \"moo \\n \" sv ; // sv suffix is a std::string_view literal return 0 ; }; \u5c0f\u8d34\u58eb The \u201cs\u201d suffix lives in the namespace std::literals::string_literals . The easiest way to access the literal suffixes is via using directive using namespace std::literals . We discuss using directives in lesson 6.12 -- Using declarations and using directives . This is one of the exception cases where using an entire namespace is okay. You probably won\u2019t need to use std::string literals very often (as it\u2019s fine to initialize a std::string object with a C-style string literal), but we\u2019ll see a few cases in future lessons where using std::string literals instead of C-style string literals makes things easier.","title":"Literals for\u00a0std::string"},{"location":"4-17-an-introduction-to-std-string/#constexpr-strings","text":"If you try to define a constexpr std::string , your compiler will probably generate an error: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include using namespace std :: literals ; int main () { constexpr std :: string name { \"Alex\" s }; // compile error std :: cout << \"My name is: \" << name ; return 0 ; } This happens because constexpr std::string isn\u2019t supported in C++17 or earlier, and only has minimal support in C++20. If you need constexpr strings, use std::string_view instead (discussed in lesson 4.18 -- Introduction to std::string_view .","title":"Constexpr strings"},{"location":"4-17-an-introduction-to-std-string/#_3","text":"std::string \u5f88\u590d\u6742\uff0c\u800c\u4e14\u4f7f\u7528\u4e86\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u5230\u7684\u591a\u79cd\u8bed\u8a00\u7279\u6027\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u5bf9\u4e8e\u5b8c\u6210\u7b80\u5355\u7684\u4efb\u52a1\u6765\u8bf4\u6765\u8bf4\uff0c\u4f60\u5e76\u4e0d\u9700\u8981\u5b8c\u5168\u7406\u89e3\u5b83\uff0c\u6bd4\u5982\u57fa\u672c\u7684\u8f93\u5165\u8f93\u51fa\u3002\u5efa\u8bae\u4f60\u4ece\u73b0\u5728\u5f00\u59cb\u5c31\u53ef\u4ee5\u591a\u5c1d\u8bd5\u5e76\u63a2\u7d22z\u5b57\u7b26\u4e32\u7684\u4f7f\u7528\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u5185\u5bb9\u4e2d\u4ecb\u7ecdstring\u7684\u5176\u4ed6\u529f\u80fd\u3002","title":"\u7ed3\u8bba"},{"location":"4-18-Introduction-to-std-string_view/","tags":["string_view","C++17"],"text":"Key Takeaway \u5f53\u9700\u8981\u4f7f\u7528\u53ea\u8bfb\u5b57\u7b26\u4e32\u65f6\uff0c\u4f7f\u7528 std:: string_view \u800c\u4e0d\u662f std:: string \uff0c\u5c24\u5176\u662f\u51fd\u6570\u5f62\u53c2\u3002 std:: string_view \u4e0d\u80fd\u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a std:: string \u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06 std:: string_view \u8f6c\u6362\u4e3a std:: string \u3002 std:: string_view \u53ef\u4ee5\u901a\u8fc7 std:: string \u521d\u59cb\u5316 \u521b\u5efa std:: string_view \u7c7b\u578b\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff0c\u5219\u9700\u8981\u5728\u53cc\u5f15\u53f7\u5b57\u7b26\u4e32\u540e\u9762\u6dfb\u52a0 sv \u540e\u7f00\u3002\u5982\u679c\u4e0d\u6dfb\u52a0\u540e\u7f00\u5219\u9ed8\u8ba4\u662f C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { int x { 5 }; std :: cout << x << '\\n' ; return 0 ; } \u5f53\u5bf9 x \u8fdb\u884c\u5b9a\u4e49\u65f6\uff0c\u521d\u59cb\u5316\u503c 5 \u4f1a\u88ab\u62f7\u8d1d\u5230 int x \u7684\u5185\u5b58\u5730\u5740\u3002\u5bf9\u4e8e\u57fa\u7840\u6570\u636e\u7c7b\u578b\u6765\u8bf4\uff0c\u521d\u59cb\u5316\uff08\u6216\u8005\u62f7\u8d1d\uff09\u4e00\u4e2a\u53d8\u91cf\u662f\u5f88\u5feb\u7684\u3002 \u5728\u770b\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { std :: string s { \"Hello, world!\" }; std :: cout << s << '\\n' ; return 0 ; } \u5f53 s \u521d\u59cb\u5316\u65f6\uff0cC \u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32 \u5b57\u9762\u91cf \"Hello, world!\" \u4f1a\u88ab\u62f7\u8d1d\u5230 std:: string s \u7684\u5185\u5b58\u5730\u5740\u3002\u548c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e0d\u540c\u7684\u662f\uff0c\u521d\u59cb\u5316\u6216\u8005\u62f7\u8d1d std:: string \u662f\u8f83\u6162\u7684\u3002 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u5c06 s \u7684\u503c\u6253\u5370\u5230\u63a7\u5236\u53f0\uff0c\u7136\u540e\u9500\u6bc1 s \u3002\u6211\u4eec\u590d\u5236\u4e86\u4e00\u4efd\u201cHello, world!\u201d\uff0c\u53ea\u662f\u4e3a\u4e86\u6253\u5370\uff0c\u7136\u540e\u5c31\u9500\u6bc1\u4e86\u90a3\u4e2a\u526f\u672c\uff0c\u76f8\u5f53\u4f4e\u6548\u3002 \u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u4e5f\u7c7b\u4f3c\u7684\u60c5\u51b5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include void printString ( std :: string str ) { std :: cout << str '\\n' ; } int main () { std :: string s { \"Hello, world!\" }; printString ( s ); return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cC\u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u201dHello world!\u201c \u88ab\u590d\u5236\u4e86\u4e24\u6b21\u3002\u7b2c\u4e00\u6b21\u662f\u5728 main() \u51fd\u6570\u4e2d\u5bf9 s \u521d\u59cb\u5316\u7684\u65f6\u5019\uff0c\u53e6\u5916\u4e00\u6b21\u662f\u5728 printString() \u4e2d\u521d\u59cb\u5316\u53c2\u6570 str \u7684\u65f6\u5019\u3002\u4e3a\u4e86\u6253\u5370\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u9700\u8981\u8fdb\u884c\u5f88\u591a\u6b21\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\uff01 std:: string_view \uff08C++17\uff09 \u4e3a\u4e86\u89e3\u51b3 std:: string \u521d\u59cb\u5316\u6216\u62f7\u8d1d\u5f00\u9500\u6bd4\u8f83\u5927\u7684\u95ee\u9898\uff0cC++17 \u5f15\u5165\u4e86 std:: string_view (\u5b58\u5728\u4e8e \u5934\u6587\u4ef6\u4e2d)\u3002 std:: string_view \u4e3a\u5df2\u6709\u7684\u5b57\u7b26\u4e32(C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u3001 std:: string \u6216\u8005\u5b57\u7b26\u6570\u7ec4) \u63d0\u4f9b\u4e00\u79cd \u53ea\u8bfb \u7684\u8bbf\u95ee\u65b9\u5f0f\uff0c\u800c\u65e0\u9700\u521b\u5efa\u4e00\u4efd\u62f7\u8d1d\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u548c\u4e4b\u524d\u7684\u7a0b\u5e8f\u6548\u679c\u662f\u5b8c\u5168\u4e00\u81f4\u7684\uff0c\u53ea\u4e0d\u8fc7\u6211\u4eec\u4f7f\u7528 std:: string_view \u66ff\u6362\u4e86 std:: string \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include #include void printSV ( std :: string_view str ) // now a std::string_view { std :: cout << str '\\n' ; } int main () { std :: string_view s { \"Hello, world!\" }; // now a std::string_view printSV ( s ); return 0 ; } \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u548c\u4e4b\u524d\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u4f46\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u5e76\u4e0d\u4f1a\u521b\u5efa \u201cHello, world!\u201d \u7684\u526f\u672c\u3002 std:: string_view s \u901a\u8fc7 C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 \"Hello, world!\" \u8fdb\u884c\u521d\u59cb\u5316\u3002\u53d8\u91cf s \u4e3a \u201cHello, world!\u201d \u63d0\u4f9b\u4e86\u53ea\u8bfb\u7684\u8bbf\u95ee\u65b9\u5f0f\uff0c\u65e0\u9700\u521b\u5efa\u526f\u672c\u3002\u5f53\u6211\u4eec\u5c06\u53d8\u91cf s \u4f20\u9012\u7ed9 printSV() \u65f6\uff0c\u51fd\u6570\u7684\u5f62\u53c2 str \u88ab\u521d\u59cb\u5316\u4e3a s \u3002\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 str \u8bbf\u95ee\u5b57\u7b26\u4e32 \u201cHello, world!\u201d\uff0c\u800c\u4e0d\u9700\u8981\u521b\u5efa\u8be5\u5b57\u7b26\u4e32\u7684\u62f7\u8d1d\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u9700\u8981\u4f7f\u7528\u53ea\u8bfb\u5b57\u7b26\u4e32\u65f6\uff0c\u4f7f\u7528 std:: string_view \u800c\u4e0d\u662f std:: string \uff0c\u5c24\u5176\u662f\u51fd\u6570\u5f62\u53c2\u3002 constexpr std:: string_view \u548c std:: string \u4e0d\u540c\u7684\u662f\uff0c std:: string_view \u5b8c\u5168\u652f\u6301 constexpr \uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { constexpr std :: string_view s { \"Hello, world!\" }; std :: cout << s << '\\n' ; // s \u4f1a\u5728\u7f16\u8bd1\u65f6\u88ab\u66ff\u6362\u4e3a \"Hello, world!\" return 0 ; } \u5c06 std:: string \u8f6c\u6362\u4e3a std:: string_view std:: string_view \u53ef\u4ee5\u901a\u8fc7 std:: string \u7c7b\u578b\u7684\u521d\u59cb\u5316\u503c\u521b\u5efa\uff0c\u6b64\u65f6 std:: string \u4f1a\u88ab \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u4e3a std:: string_view \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include #include void printSV ( std :: string_view str ) { std :: cout << str << '\\n' ; } int main () { std :: string s { \"Hello, world\" }; std :: string_view sv { s }; // Initialize a std::string_view from a std::string std :: cout << sv ; printSV ( s ); // implicitly convert a std::string to std::string_view return 0 ; } \u5c06 std:: string_view \u8f6c\u6362\u4e3a std:: string \u56e0\u4e3a std:: string \u4f1a\u62f7\u8d1d\u5b83\u7684\u521d\u59cb\u5316\u503c\uff0c\u6240\u4ee5 C++\u4e0d\u5141\u8bb8\u5c06 std:: string_view \u9690\u5f0f\u8f6c\u6362\u4e3a std:: string \u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u53ef\u4ee5\u663e\u793a\u5730\u4f7f\u7528 std:: string_view \u4f5c\u4e3a\u521d\u59cb\u5316\u503c\u5e76\u521b\u5efa std:: string \u3002\u6216\u8005\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06 std:: string_view \u8f6c\u6362\u4e3a std:: string \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include #include void printString ( std :: string str ) { std :: cout << str << '\\n' ; } int main () { std :: string_view sv { \"balloon\" }; std :: string str { sv }; // okay, \u53ef\u4ee5\u4f7f\u7528std::string_view\u521d\u59cb\u5316\u503c\u521b\u5efastd::string // printString(sv); // \u7f16\u8bd1\u9519\u8bef: std::string_view \u4e0d\u80fd\u9690\u5f0f\u8f6c\u6362\u4e3astd::string print ( static_cast < std :: string > ( sv )); // okay,\u53ef\u4ee5\u663e\u793a\u5730\u5c06 std::string_view \u8f6c\u6362\u4e3a std::string return 0 ; } std:: string_view \u5b57\u9762\u91cf \u53cc\u5f15\u53f7\u5185\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u901a\u5e38\u9ed8\u8ba4\u662f C \u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002\u5982\u679c\u5e0c\u671b\u521b\u5efa std:: string_view \u7c7b\u578b\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff0c\u5219\u9700\u8981\u5728\u53cc\u5f15\u53f7\u5b57\u7b26\u4e32\u540e\u9762\u6dfb\u52a0 sv \u540e\u7f00\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include #include // for std::string #include // for std::string_view int main () { using namespace std :: literals ; // easiest way to access the s and sv suffixes std :: cout << \"foo \\n \" ; // no suffix is a C-style string literal std :: cout << \"goo \\n \" s ; // s suffix is a std::string literal std :: cout << \"moo \\n \" sv ; // sv suffix is a std::string_view literal return 0 ; }; \u5c0f\u8d34\u58eb sv \u540e\u7f00\u4f4d\u4e8e\u547d\u540d\u7a7a\u95f4 std:: literals:: string_view_literals \u4e2d\u3002\u8bbf\u95ee\u5b57\u9762\u91cf\u540e\u7f00\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u6307\u4ee4 using namespace std:: literals \u3002\u6211\u4eec\u5728\u8bfe\u7a0b 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 \u4e2d\u8ba8\u8bba\u4e86 using \u6307\u4ee4\u3002\u8fd9\u662f using \u6574\u4e2a\u547d\u540d\u7a7a\u95f4\u662f\u53ef\u4ee5\u7684\u4f8b\u5916\u60c5\u51b5\u4e4b\u4e00\u3002 \u4e0d\u8981\u8fd4\u56de std:: string_view \u7c7b\u578b\u7684\u503c \u4ece\u51fd\u6570\u8fd4\u56de std:: string_view \u7c7b\u578b\u7684\u503c\u53ef\u4e0d\u662f\u4e2a\u597d\u4e3b\u610f\uff0c\u6211\u4eec\u4f1a\u5728 11.7 - std:: string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 \u4e2d\u8ba8\u8bba\u3002\u76ee\u524d\u4f60\u53ea\u9700\u8981\u8bb0\u4f4f\u4e0d\u8981\u8fd9\u4e48\u505a\u5c31\u884c\u3002","title":"4.18 - std::string_view \u7b80\u4ecb"},{"location":"4-18-Introduction-to-std-string_view/#std-string_viewc17","text":"\u4e3a\u4e86\u89e3\u51b3 std:: string \u521d\u59cb\u5316\u6216\u62f7\u8d1d\u5f00\u9500\u6bd4\u8f83\u5927\u7684\u95ee\u9898\uff0cC++17 \u5f15\u5165\u4e86 std:: string_view (\u5b58\u5728\u4e8e \u5934\u6587\u4ef6\u4e2d)\u3002 std:: string_view \u4e3a\u5df2\u6709\u7684\u5b57\u7b26\u4e32(C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32\u3001 std:: string \u6216\u8005\u5b57\u7b26\u6570\u7ec4) \u63d0\u4f9b\u4e00\u79cd \u53ea\u8bfb \u7684\u8bbf\u95ee\u65b9\u5f0f\uff0c\u800c\u65e0\u9700\u521b\u5efa\u4e00\u4efd\u62f7\u8d1d\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u548c\u4e4b\u524d\u7684\u7a0b\u5e8f\u6548\u679c\u662f\u5b8c\u5168\u4e00\u81f4\u7684\uff0c\u53ea\u4e0d\u8fc7\u6211\u4eec\u4f7f\u7528 std:: string_view \u66ff\u6362\u4e86 std:: string \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include #include void printSV ( std :: string_view str ) // now a std::string_view { std :: cout << str '\\n' ; } int main () { std :: string_view s { \"Hello, world!\" }; // now a std::string_view printSV ( s ); return 0 ; } \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u548c\u4e4b\u524d\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u4f46\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u5e76\u4e0d\u4f1a\u521b\u5efa \u201cHello, world!\u201d \u7684\u526f\u672c\u3002 std:: string_view s \u901a\u8fc7 C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 \"Hello, world!\" \u8fdb\u884c\u521d\u59cb\u5316\u3002\u53d8\u91cf s \u4e3a \u201cHello, world!\u201d \u63d0\u4f9b\u4e86\u53ea\u8bfb\u7684\u8bbf\u95ee\u65b9\u5f0f\uff0c\u65e0\u9700\u521b\u5efa\u526f\u672c\u3002\u5f53\u6211\u4eec\u5c06\u53d8\u91cf s \u4f20\u9012\u7ed9 printSV() \u65f6\uff0c\u51fd\u6570\u7684\u5f62\u53c2 str \u88ab\u521d\u59cb\u5316\u4e3a s \u3002\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 str \u8bbf\u95ee\u5b57\u7b26\u4e32 \u201cHello, world!\u201d\uff0c\u800c\u4e0d\u9700\u8981\u521b\u5efa\u8be5\u5b57\u7b26\u4e32\u7684\u62f7\u8d1d\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u9700\u8981\u4f7f\u7528\u53ea\u8bfb\u5b57\u7b26\u4e32\u65f6\uff0c\u4f7f\u7528 std:: string_view \u800c\u4e0d\u662f std:: string \uff0c\u5c24\u5176\u662f\u51fd\u6570\u5f62\u53c2\u3002","title":"std:: string_view\uff08C++17\uff09"},{"location":"4-18-Introduction-to-std-string_view/#constexpr-std-string_view","text":"\u548c std:: string \u4e0d\u540c\u7684\u662f\uff0c std:: string_view \u5b8c\u5168\u652f\u6301 constexpr \uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { constexpr std :: string_view s { \"Hello, world!\" }; std :: cout << s << '\\n' ; // s \u4f1a\u5728\u7f16\u8bd1\u65f6\u88ab\u66ff\u6362\u4e3a \"Hello, world!\" return 0 ; }","title":"constexpr std:: string_view"},{"location":"4-18-Introduction-to-std-string_view/#std-string-std-string_view","text":"std:: string_view \u53ef\u4ee5\u901a\u8fc7 std:: string \u7c7b\u578b\u7684\u521d\u59cb\u5316\u503c\u521b\u5efa\uff0c\u6b64\u65f6 std:: string \u4f1a\u88ab \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u4e3a std:: string_view \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include #include void printSV ( std :: string_view str ) { std :: cout << str << '\\n' ; } int main () { std :: string s { \"Hello, world\" }; std :: string_view sv { s }; // Initialize a std::string_view from a std::string std :: cout << sv ; printSV ( s ); // implicitly convert a std::string to std::string_view return 0 ; }","title":"\u5c06\u00a0std:: string\u00a0\u8f6c\u6362\u4e3a\u00a0std:: string_view"},{"location":"4-18-Introduction-to-std-string_view/#std-string_view-std-string","text":"\u56e0\u4e3a std:: string \u4f1a\u62f7\u8d1d\u5b83\u7684\u521d\u59cb\u5316\u503c\uff0c\u6240\u4ee5 C++\u4e0d\u5141\u8bb8\u5c06 std:: string_view \u9690\u5f0f\u8f6c\u6362\u4e3a std:: string \u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u53ef\u4ee5\u663e\u793a\u5730\u4f7f\u7528 std:: string_view \u4f5c\u4e3a\u521d\u59cb\u5316\u503c\u5e76\u521b\u5efa std:: string \u3002\u6216\u8005\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06 std:: string_view \u8f6c\u6362\u4e3a std:: string \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include #include void printString ( std :: string str ) { std :: cout << str << '\\n' ; } int main () { std :: string_view sv { \"balloon\" }; std :: string str { sv }; // okay, \u53ef\u4ee5\u4f7f\u7528std::string_view\u521d\u59cb\u5316\u503c\u521b\u5efastd::string // printString(sv); // \u7f16\u8bd1\u9519\u8bef: std::string_view \u4e0d\u80fd\u9690\u5f0f\u8f6c\u6362\u4e3astd::string print ( static_cast < std :: string > ( sv )); // okay,\u53ef\u4ee5\u663e\u793a\u5730\u5c06 std::string_view \u8f6c\u6362\u4e3a std::string return 0 ; }","title":"\u5c06 std:: string_view\u00a0\u8f6c\u6362\u4e3a std:: string"},{"location":"4-18-Introduction-to-std-string_view/#std-string_view","text":"\u53cc\u5f15\u53f7\u5185\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u901a\u5e38\u9ed8\u8ba4\u662f C \u8bed\u8a00\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u3002\u5982\u679c\u5e0c\u671b\u521b\u5efa std:: string_view \u7c7b\u578b\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff0c\u5219\u9700\u8981\u5728\u53cc\u5f15\u53f7\u5b57\u7b26\u4e32\u540e\u9762\u6dfb\u52a0 sv \u540e\u7f00\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include #include // for std::string #include // for std::string_view int main () { using namespace std :: literals ; // easiest way to access the s and sv suffixes std :: cout << \"foo \\n \" ; // no suffix is a C-style string literal std :: cout << \"goo \\n \" s ; // s suffix is a std::string literal std :: cout << \"moo \\n \" sv ; // sv suffix is a std::string_view literal return 0 ; }; \u5c0f\u8d34\u58eb sv \u540e\u7f00\u4f4d\u4e8e\u547d\u540d\u7a7a\u95f4 std:: literals:: string_view_literals \u4e2d\u3002\u8bbf\u95ee\u5b57\u9762\u91cf\u540e\u7f00\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u6307\u4ee4 using namespace std:: literals \u3002\u6211\u4eec\u5728\u8bfe\u7a0b 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 \u4e2d\u8ba8\u8bba\u4e86 using \u6307\u4ee4\u3002\u8fd9\u662f using \u6574\u4e2a\u547d\u540d\u7a7a\u95f4\u662f\u53ef\u4ee5\u7684\u4f8b\u5916\u60c5\u51b5\u4e4b\u4e00\u3002","title":"std:: string_view \u5b57\u9762\u91cf"},{"location":"4-18-Introduction-to-std-string_view/#std-string_view_1","text":"\u4ece\u51fd\u6570\u8fd4\u56de std:: string_view \u7c7b\u578b\u7684\u503c\u53ef\u4e0d\u662f\u4e2a\u597d\u4e3b\u610f\uff0c\u6211\u4eec\u4f1a\u5728 11.7 - std:: string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 \u4e2d\u8ba8\u8bba\u3002\u76ee\u524d\u4f60\u53ea\u9700\u8981\u8bb0\u4f4f\u4e0d\u8981\u8fd9\u4e48\u505a\u5c31\u884c\u3002","title":"\u4e0d\u8981\u8fd4\u56de std:: string_view \u7c7b\u578b\u7684\u503c"},{"location":"4-2-Void/","tags":["void"],"text":"Void \u662f\u6700\u5bb9\u6613\u4ecb\u7ecd\u7684\u6570\u636e\u7c7b\u578b\u3002\u57fa\u672c\u4e0a\uff0cvoid \u610f\u5473\u7740\u201c\u6ca1\u6709\u7c7b\u578b\u201d\uff01 \u56e0\u6b64\uff0c\u53d8\u91cf\u4e0d\u80fd\u88ab\u5b9a\u4e49\u4e3a void \u7c7b\u578b\uff1a 1 void value ; // won't work, variables can't be defined with a void type \u4f46 Void \u53ef\u4ee5\u88ab\u7528\u5728\u5176\u4ed6\u5730\u65b9\u3002 \u4e0d\u8fd4\u56de\u503c\u7684\u51fd\u6570 \u6700\u5e38\u89c1\u7684\u7528\u5230 void \u7684\u5730\u65b9\u662f\u6307\u793a\u51fd\u6570\u6ca1\u6709\u8fd4\u56de\u503c\uff1a 1 2 3 4 5 void writeValue ( int x ) // void here means no return value { std :: cout << \"The value of x is: \" << x << '\\n' ; // no return statement, because this function doesn't return a value } \u5982\u679c\u4f60\u5728\u8fd9\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u4e2d\u4f7f\u7528\u4e86 return \u8bed\u53e5\uff0c\u5219\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\uff1a 1 2 3 4 void noReturn ( int x ) // void here means no return value { return 5 ; // error } \u5728 Visual Studio 2017 \u4e0a\uff0c\u62a5\u9519\u4fe1\u606f\u5982\u4e0b\uff1a 1 error C2562: 'noReturn': 'void' function returning a value \u5f03\u7528: \u4e0d\u63a5\u53d7\u4ea7\u751f\u7684\u51fd\u6570 \u5728 C \u8bed\u8a00\u4e2d\uff0cvoid \u8fd8\u88ab\u7528\u6765\u8868\u660e\u51fd\u6570\u4e0d\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\uff1a 1 2 3 4 5 6 int getValue ( void ) // void here means no parameters { int x {}; std :: cin >> x ; return x ; } \u5c3d\u7ba1\u4e0a\u8ff0\u7528\u6cd5\u5728 C++ \u4e2d\u4ecd\u7136\u53ef\u7528\uff08\u5411\u540e\u517c\u5bb9\uff09\uff0c\u8fd9\u79cd\u65b9\u5f0f\u5728 C++ \u4e2d\u88ab\u8ba4\u4e3a\u662f\u5df2\u7ecf\u5f03\u7528\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u8fd9\u79cd\u65b9\u5f0f\u662f\u7b49\u4ef7\u7684\uff0c\u4e5f\u662f C++ \u6240\u63a8\u8350\u7684\uff1a 1 2 3 4 5 6 int getValue () // \u7a7a\u7684\u51fd\u6570\u5f62\u53c2\u5c31\u9690\u542b\u4e0d\u63a5\u53d7\u53c2\u6570\u7684\u542b\u4e49 { int x {}; std :: cin >> x ; return x ; } \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u7a7a\u7684\u5f62\u53c2\u5217\u8868\u800c\u975e void \u6765\u8868\u793a\u51fd\u6570\u4e0d\u63a5\u53d7\u53c2\u6570 void \u7684\u5176\u4ed6\u7528\u6cd5 void \u5173\u952e\u5b57\u5728 C++ \u4e2d\u7684\u7b2c\u4e09\u79cd\u7528\u6cd5\uff08\u66f4\u9ad8\u7ea7\u7684\u7528\u6cd5\uff09\u6211\u4eec\u4f1a\u5728 11.4 - void \u6307\u9488 \u4e2d\u8fdb\u884c\u4ecb\u7ecd . \u7531\u4e8e\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u6307\u9488\u7684\u76f8\u5173\u6982\u5ff5\uff0c\u6240\u4ee5\u6682\u65f6\u4e0d\u9700\u8981\u5173\u7cfb\u7a7a\u6307\u9488\u7684\u7528\u6cd5\u3002 \u7ee7\u7eed\u5f80\u4e0b\u770b\u5427\uff01","title":"4.2 - Void"},{"location":"4-2-Void/#_1","text":"\u6700\u5e38\u89c1\u7684\u7528\u5230 void \u7684\u5730\u65b9\u662f\u6307\u793a\u51fd\u6570\u6ca1\u6709\u8fd4\u56de\u503c\uff1a 1 2 3 4 5 void writeValue ( int x ) // void here means no return value { std :: cout << \"The value of x is: \" << x << '\\n' ; // no return statement, because this function doesn't return a value } \u5982\u679c\u4f60\u5728\u8fd9\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u4e2d\u4f7f\u7528\u4e86 return \u8bed\u53e5\uff0c\u5219\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\uff1a 1 2 3 4 void noReturn ( int x ) // void here means no return value { return 5 ; // error } \u5728 Visual Studio 2017 \u4e0a\uff0c\u62a5\u9519\u4fe1\u606f\u5982\u4e0b\uff1a 1 error C2562: 'noReturn': 'void' function returning a value","title":"\u4e0d\u8fd4\u56de\u503c\u7684\u51fd\u6570"},{"location":"4-2-Void/#_2","text":"\u5728 C \u8bed\u8a00\u4e2d\uff0cvoid \u8fd8\u88ab\u7528\u6765\u8868\u660e\u51fd\u6570\u4e0d\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\uff1a 1 2 3 4 5 6 int getValue ( void ) // void here means no parameters { int x {}; std :: cin >> x ; return x ; } \u5c3d\u7ba1\u4e0a\u8ff0\u7528\u6cd5\u5728 C++ \u4e2d\u4ecd\u7136\u53ef\u7528\uff08\u5411\u540e\u517c\u5bb9\uff09\uff0c\u8fd9\u79cd\u65b9\u5f0f\u5728 C++ \u4e2d\u88ab\u8ba4\u4e3a\u662f\u5df2\u7ecf\u5f03\u7528\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u8fd9\u79cd\u65b9\u5f0f\u662f\u7b49\u4ef7\u7684\uff0c\u4e5f\u662f C++ \u6240\u63a8\u8350\u7684\uff1a 1 2 3 4 5 6 int getValue () // \u7a7a\u7684\u51fd\u6570\u5f62\u53c2\u5c31\u9690\u542b\u4e0d\u63a5\u53d7\u53c2\u6570\u7684\u542b\u4e49 { int x {}; std :: cin >> x ; return x ; } \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u7a7a\u7684\u5f62\u53c2\u5217\u8868\u800c\u975e void \u6765\u8868\u793a\u51fd\u6570\u4e0d\u63a5\u53d7\u53c2\u6570","title":"\u5f03\u7528: \u4e0d\u63a5\u53d7\u4ea7\u751f\u7684\u51fd\u6570"},{"location":"4-2-Void/#void","text":"void \u5173\u952e\u5b57\u5728 C++ \u4e2d\u7684\u7b2c\u4e09\u79cd\u7528\u6cd5\uff08\u66f4\u9ad8\u7ea7\u7684\u7528\u6cd5\uff09\u6211\u4eec\u4f1a\u5728 11.4 - void \u6307\u9488 \u4e2d\u8fdb\u884c\u4ecb\u7ecd . \u7531\u4e8e\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u6307\u9488\u7684\u76f8\u5173\u6982\u5ff5\uff0c\u6240\u4ee5\u6682\u65f6\u4e0d\u9700\u8981\u5173\u7cfb\u7a7a\u6307\u9488\u7684\u7528\u6cd5\u3002 \u7ee7\u7eed\u5f80\u4e0b\u770b\u5427\uff01","title":"void \u7684\u5176\u4ed6\u7528\u6cd5"},{"location":"4-3-Object-sizes-and-the-sizeof-operator/","tags":["sizeof"],"text":"Key Takeaway C++ \u53ea\u4fdd\u8bc1\u4e86\u6bcf\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\u5360\u7528\u7a7a\u95f4\u7684\u6700\u5c0f\u503c\uff0c\u5177\u4f53\u5927\u5c0f\u548c\u7f16\u8bd1\u5668\u53ca\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u76f8\u5173 CPU \u6309\u7167\u67d0\u4e2a\u957f\u5ea6\u6765\u5904\u7406\u6570\u636e\uff0c\u53ea\u6709\u5f53\u6570\u636e\u7c7b\u578b\u7684\u5927\u5c0f\u7b49\u4e8e\u8be5\u957f\u5ea6\u65f6\u624d\u4f1a\u66f4\u5feb\uff0c\u5e76\u4e0d\u4e00\u5b9a\u5360\u7528\u5185\u5b58\u5c0f\u7684\u6570\u636e\u7ed3\u6784\u66f4\u5feb \u5bf9\u8c61\u7684\u5927\u5c0f \u6b63\u5982\u5728 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb \u4e2d\u4ecb\u7ecd\u7684\u90a3\u6837\uff0c\u73b0\u4ee3\u8ba1\u7b97\u673a\u4ee5 \u5b57\u8282(byte) \u4e3a\u5355\u4f4d\u4f7f\u7528\u5185\u5b58\uff0c\u6bcf\u4e2a\u5b57\u8282\u7684\u5185\u5b58\u90fd\u6709\u5176\u552f\u4e00\u5bf9\u5e94\u7684\u5730\u5740\u3002\u4ece\u8fd9\u4e2a\u89d2\u5ea6\u6765\u8bf4\uff0c\u5185\u5b58\u53ef\u4ee5\u88ab\u60f3\u8c61\u6210\u4e00\u7cfb\u5217\u7684\u5c0f\u50a8\u85cf\u95f4\u6216\u4fe1\u7bb1\uff0c\u4f60\u53ef\u4ee5\u4ece\u4e2d\u5b58\u653e\u6216\u8bfb\u53d6\u4fe1\u606f\uff0c\u800c\u53d8\u91cf\u540d\u5219\u7528\u6765\u5bf9\u8fd9\u4e9b\u50a8\u85cf\u95f4\u6216\u4fe1\u7bb1\u8fdb\u884c\u8bbf\u95ee\u3002 \u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u6bd4\u55bb\u5e76\u4e0d\u5b8c\u5168\u51c6\u786e\u2014\u2014\u5f88\u591a\u5bf9\u8c61\u5b9e\u9645\u4e0a\u5360\u636e\u7684\u5185\u5b58\u8981\u5927\u4e8e1\u4e2a\u5b57\u8282\u3002\u4e00\u4e2a\u5bf9\u8c61\u53ef\u80fd\u4f1a\u4f7f\u75282\u30014\u30018\u6216\u8005\u66f4\u591a\u8fde\u7eed\u7684\u5185\u5b58\u5730\u5740\u3002\u800c\u5bf9\u8c61\u4f7f\u7528\u591a\u5927\u7684\u5185\u5b58\uff0c\u53d6\u51b3\u4e8e\u5bf9\u8c61\u7684\u6570\u636e\u7c7b\u578b\u3002 \u56e0\u4e3a\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u53d8\u91cf\u540d\u6765\u8bbf\u95ee\u5185\u5b58\uff08\u800c\u975e\u76f4\u63a5\u8bbf\u95ee\u5185\u5b58\uff09\uff0c\u7f16\u8bd1\u5668\u5e2e\u6211\u4eec\u9690\u85cf\u4e86\u4e0a\u8ff0\u7ec6\u8282\uff0c\u5373\u4e00\u4e2a\u5bf9\u8c61\u5360\u7528\u591a\u5c11\u5185\u5b58\u3002\u5f53\u4f7f\u7528\u53d8\u91cf x \u8bbf\u95ee\u5176\u5bf9\u5e94\u7684\u5185\u5b58\u65f6\uff0c\u7f16\u8bd1\u5668\u6307\u5b9a\u5e94\u8be5\u83b7\u53d6\u591a\u5c11\u5b57\u8282\u7684\u6570\u636e\uff08\u57fa\u4e8e x \u7684\u6570\u636e\u7c7b\u578b\uff09\uff0c\u5b83\u4e3a\u6211\u4eec\u5b8c\u6210\u4e86\u4e0a\u8ff0\u5de5\u4f5c\u3002 \u5373\u4f7f\u8fd9\u4e5f\uff0c\u5f88\u591a\u65f6\u5019\u77e5\u9053\u5bf9\u8c61\u5360\u7528\u591a\u5c11\u5185\u5b58\u662f\u6709\u7528\u7684\u3002 \u9996\u5148\uff0c\u5bf9\u8c61\u5360\u7528\u7684\u5185\u5b58\u51b3\u5b9a\u4e86\u8be5\u5bf9\u8c61\u80fd\u591f\u4fdd\u5b58\u591a\u5c11\u4fe1\u606f\u3002 \u4e00\u4e2a\u6bd4\u7279\u53ef\u4ee5\u4fdd\u5b58\u4e24\u4e2a\u53ef\u80fd\u7684\u503c\uff0c\u53730\u62161\uff1a bit 0 0 1 bit 0 bit 1 0 0 1 1 1 0 1 1 bit 0 bit 1 bit 2 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 \u6982\u62ec\u6765\u8bf4\uff0c\u5177\u6709 n \u4e2a \u4f4d \u5927\u5c0f\u7684\u5bf9\u8c61\uff08n\u4e3a\u6b63\u6574\u6570\uff09\u53ef\u4ee5\u4fdd\u5b58 \\(2^n\\) \u4e2d\u4e0d\u540c\u7684\u503c\uff082\u7684n\u6b21\u65b9\uff0c\u4e00\u822c\u4e5f\u5199\u4f5c 2^n \uff09\u3002\u56e0\u6b64\uff0c\u5bf9\u4e8e\u4e00\u4e2a8\u4e2a\u6bd4\u7279\u7684\u5b57\u8282\u6765\u8bf4\uff0c\u5927\u5c0f\u4f4d1\u5b57\u8282\u7684\u5bf9\u8c61\u53ef\u4ee5\u4fdd\u5b58 \\(2^8\\) (256)\u4e2d\u53ef\u80fd\u7684\u503c\u3002\u4f7f\u75282\u4e2a\u5b57\u8282\u7684\u5bf9\u8c61\u53ef\u4ee5\u4fdd\u5b58 \\(2^{16}\\) (65536)\u79cd\u4e0d\u540c\u7684\u503c\uff01 \u56e0\u6b64\uff0c\u5bf9\u8c61\u7684\u5927\u5c0f\u51b3\u5b9a\u4e86\u5b83\u80fd\u4fdd\u5b58\u7684\u4e0d\u540c\u503c\u7684\u4e0a\u9650\u2014\u2014\u4f7f\u7528\u7684\u5185\u5b58\u8d8a\u591a\uff0c\u80fd\u591f\u5b58\u653e\u7684\u4e0d\u540c\u503c\u4e5f\u5c31\u8d8a\u591a\u3002\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u5bf9\u6574\u578b\u8fdb\u884c\u66f4\u8be6\u7ec6\u5730\u4ecb\u7ecd\u3002 \u5176\u6b21\uff0c\u8ba1\u7b97\u673a\u7684\u5185\u5b58\u662f\u6709\u9650\u7684\u3002\u5f53\u6211\u4eec\u5b9a\u4e49\u5bf9\u8c61\u7684\u65f6\u5019\uff0c\u4e00\u5c0f\u5757\u7a7a\u95f2\u7684\u5185\u5b58\u5c31\u88ab\u5360\u7528\u4e86\uff08\u53ea\u8981\u8be5\u5bf9\u8c61\u8fd8\u5b58\u5728\uff09\u3002\u56e0\u4e3a\u73b0\u4ee3\u8ba1\u7b97\u673a\u7684\u5185\u5b58\u76f8\u5bf9\u6765\u8bf4\u6bd4\u8f83\u591a\uff0c\u6240\u4ee5\u5360\u7528\u4e00\u5c0f\u5757\u5185\u5b58\u5f71\u54cd\u4e0d\u5927\u3002\u4f46\u662f\uff0c\u5f53\u7a0b\u5e8f\u4e2d\u5177\u6709\u5927\u91cf\u5bf9\u8c61\u6216\u8005\u6570\u636e\u65f6\uff08\u4f8b\u5982\uff0c\u4e00\u4e2a\u6b63\u5728\u6e32\u67d3\u51e0\u767e\u4e07\u4e2a\u5730\u7262\u7684\u5927\u578b\u6e38\u620f\uff09\uff0c\u5bf9\u8c61\u5360\u75281\u4e2a\u5b57\u8282\u7684\u5927\u5c0f\u8fd8\u662f8\u4e2a\u5b57\u8282\u5c31\u4f1a\u9020\u6210\u5f88\u5927\u7684\u5dee\u522b\u3002 \u5173\u952e\u4fe1\u606f \u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u8fc7\u5206\u5173\u6ce8\u4f18\u5316\u4ed6\u4eec\u7684\u4ee3\u7801\u5e76\u4f7f\u5176\u7a0b\u5e8f\u80fd\u591f\u4f7f\u7528\u5c3d\u53ef\u80fd\u5c11\u7684\u5185\u5b58\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u5176\u5b9e\u5f71\u54cd\u4e0d\u5927\u3002\u4f60\u7684\u6ce8\u610f\u529b\u5e94\u8be5\u653e\u5728\u7f16\u5199\u6613\u4e8e\u7ef4\u62a4\u7684\u4ee3\u7801\uff0c\u53ea\u6709\u5f53\u4f18\u5316\u53ef\u4ee5\u5e26\u6765\u663e\u8457\u6536\u76ca\u7684\u65f6\u5019\u518d\u53bb\u505a\u3002 \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7684\u5927\u5c0f \u63a5\u4e0b\u6765\u7684\u95ee\u9898\u5f88\u663e\u7136\uff1a\u201d\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7a76\u7adf\u5360\u7528\u591a\u5c11\u5185\u5b58\uff1f\u201c\u3002\u7b54\u6848\u53ef\u80fd\u4f1a\u4ee4\u4f60\u5403\u60ca\uff0c\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u5360\u7528\u7684\u5185\u5b58\u548c\u7f16\u8bd1\u5668\u4ee5\u53ca\uff08\u6216\uff09\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u6709\u5173\u3002 C++ \u53ea\u4fdd\u8bc1\u4e86\u6bcf\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\u6700\u5c0f\u7684\u5c3a\u5bf8\uff1a \u5206\u7c7b \u7c7b\u578b \u6700\u5c0f\u5c3a\u5bf8 \u5907\u6ce8 boolean bool 1 byte character char 1 byte \u603b\u662f1\u4e2a\u5b57\u8282 character wchat_t 1 byte character char16_t 2 byte character char32_t 4 byte integer short 2 byte integer int 2 byte integer long 4 byte integer long long 8 byte floating point float 4 byte floating point double 8 byte floating point long double 8 byte \u4e0d\u7136\uff0c\u8fd9\u4e9b\u53d8\u91cf\u7684\u5b9e\u9645\u5927\u5c0f\u548c\u4f60\u7684\u673a\u5668\u76f8\u5173\uff08\u4f8b\u5982 int \u901a\u5e38\u5c31\u662f4\u4e2a\u5b57\u8282\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u5904\u4e8e\u517c\u5bb9\u6027\u8003\u8651\uff0c\u4f60\u4e0d\u80fd\u591f\u5047\u8bbe\u53d8\u91cf\u7684\u5b9e\u9645\u5927\u5c0f\u4f1a\u5927\u4e8e\u5176\u6700\u5c0f\u503c\u3002 sizeof \u8fd0\u7b97\u7b26 \u4e3a\u4e86\u786e\u5b9a\u6570\u636e\u7c7b\u578b\u5728\u5177\u4f53\u673a\u5668\u4e0a\u5360\u7528\u7684\u5185\u5b58\u5927\u5c0f\uff0cC++ \u63d0\u4f9b\u4e86 size \u8fd0\u7b97\u7b26\u3002\u8be5\u8fd0\u7b97\u7b26\u662f\u4e00\u4e2a\u4e00\u5143\u8fd0\u7b97\u7b26\uff0c\u5b83\u4f7f\u7528\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\u6216\u53d8\u91cf\u4f5c\u4e3a \u64cd\u4f5c\u6570(operands) \uff0c\u8ba1\u7b97\u5f97\u5230\u8be5\u6570\u636e\u7c7b\u578b\u6216\u53d8\u91cf\u5360\u7528\u5185\u5b58\u7684\u5927\u5c0f\uff08\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\uff09\u3002\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u7f16\u8bd1\u548c\u8fd0\u884c\u4e0b\u9762\u7684\u7a0b\u5e8f\u4ee5\u4fbf\u786e\u5b9a\u5728\u4f60\u7684\u7535\u8111\u4e0a\uff0c\u5404\u4e2a\u6570\u636e\u7c7b\u578b\u90fd\u5360\u7528\u591a\u5c11\u5185\u5b58\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { std :: cout << \"bool: \\t\\t \" << sizeof ( bool ) << \" bytes \\n \" ; std :: cout << \"char: \\t\\t \" << sizeof ( char ) << \" bytes \\n \" ; std :: cout << \"wchar_t: \\t \" << sizeof ( wchar_t ) << \" bytes \\n \" ; std :: cout << \"char16_t: \\t \" << sizeof ( char16_t ) << \" bytes \\n \" ; std :: cout << \"char32_t: \\t \" << sizeof ( char32_t ) << \" bytes \\n \" ; std :: cout << \"short: \\t\\t \" << sizeof ( short ) << \" bytes \\n \" ; std :: cout << \"int: \\t\\t \" << sizeof ( int ) << \" bytes \\n \" ; std :: cout << \"long: \\t\\t \" << sizeof ( long ) << \" bytes \\n \" ; std :: cout << \"long long: \\t \" << sizeof ( long long ) << \" bytes \\n \" ; std :: cout << \"float: \\t\\t \" << sizeof ( float ) << \" bytes \\n \" ; std :: cout << \"double: \\t\\t \" << sizeof ( double ) << \" bytes \\n \" ; std :: cout << \"long double: \\t \" << sizeof ( long double ) << \" bytes \\n \" ; return 0 ; } \u4f5c\u8005\u4f7f\u7528\u7684\u662f 64 \u4f4d\u673a\u5668\uff0c\u901a\u8fc7 Visual Studio \u7f16\u8bd1\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u8f93\u51fa\u7684\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 bool: 1 bytes char: 1 bytes wchar_t: 2 bytes char16_t: 2 bytes char32_t: 4 bytes short: 2 bytes int: 4 bytes long: 4 bytes long long: 8 bytes float: 4 bytes double: 8 bytes long double: 8 bytes \u4f60\u5f97\u5230\u7684\u7ed3\u679c\u53ef\u80fd\u4f1a\u548c\u6b64\u5904\u4e0d\u4e00\u6837\uff0c\u8fd9\u53d6\u51b3\u4e8e\u662f\u5426\u4f7f\u7528\u4e86\u4e0d\u540c\u7684\u8ba1\u7b97\u673a\u6216\u7f16\u8bd1\u5668\u3002\u6ce8\u610f\uff1a\u4f60\u4e0d\u80fd\u4f7f\u7528 sizeof \u8ba1\u7b97 void \u7684\u5927\u5c0f\uff0c\u56e0\u4e3a\u5b83\u6ca1\u6709\u5927\u5c0f\uff08\u8fd9\u4e48\u505a\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\uff09\u3002 \u6269\u5c55\u9605\u8bfb \u4e5f\u8bb8\u4f60\u6709\u5174\u8da3\u77e5\u9053\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u7684 \\t \u6709\u4ec0\u4e48\u4f5c\u7528\u3002\u8fd9\u662f\u4e00\u4e2a\u7279\u6b8a\u7b26\u53f7\uff0c\u7528\u4e8e\u63d2\u5165\u4e00\u4e2a\u5236\u8868\u7b26\uff08\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b83\u5c06\u8f93\u51fa\u7ed3\u679c\u6309\u5217\u5bf9\u5176\uff09\u3002\u6211\u4eec\u4f1a\u5728 4.11 - \u5b57\u7b26 \u4e2d\u4ecb\u7ecd \\t \u548c\u5176\u4ed6\u7684\u7279\u6b8a\u5b57\u7b26\u3002 \u4f60\u4e5f\u53ef\u4ee5\u5bf9\u53d8\u91cf\u540d\u4f7f\u7528 sizeof \u3002 1 2 3 4 5 6 7 8 9 #include int main () { int x {}; std :: cout << \"x is \" << sizeof ( x ) << \" bytes \\n \" ; return 0 ; } 1 x is 4 bytes \u8bd1\u8005\u6ce8 \u5bf9\u6307\u9488\u4f7f\u7528 sizeof \u7684\u7ed3\u679c\u662f\u56fa\u5b9a\u7684\uff0c\u4e0d\u53d6\u51b3\u4e8e\u6307\u9488\u6307\u5411\u4ec0\u4e48\u7c7b\u578b\u7684\u6570\u636e\u3002\u901a\u5e38\u4e3a4\u6216\u80058\u4e2a\u5b57\u8282\u3002 \u57fa\u672c\u6570\u636e\u7ed3\u6784\u7684\u6027\u80fd \u5728\u73b0\u4ee3\u8ba1\u7b97\u673a\u4e0a\uff0c\u57fa\u7840\u6570\u636e\u7c7b\u578b\u7684\u5bf9\u8c61\u6027\u80fd\u901a\u5e38\u90fd\u975e\u5e38\u597d\uff0c\u56e0\u6b64\u8fd9\u4e9b\u53d8\u91cf\u7684\u6027\u80fd\u95ee\u9898\u901a\u5e38\u4e0d\u9700\u8981\u6211\u4eec\u7279\u522b\u8003\u8651\u3002 \u9898\u5916\u8bdd \u4f60\u53ef\u80fd\u4f1a\u60f3\u5f53\u7136\u5730\u8ba4\u4e3a\u4f7f\u7528\u5185\u5b58\u5c11\u7684\u6570\u636e\u7ed3\u6784\u901f\u5ea6\u4e5f\u4f1a\u66f4\u5feb\uff0c\u5b9e\u9645\u4e0a\u5e76\u4e0d\u603b\u662f\u8fd9\u6837\u3002CPU \u901a\u5e38\u4f1a\u5bf9\u6570\u636e\u7684\u6d41\u7a0b\u8fdb\u884c\u4f18\u5316\uff0c\u6309\u7167\u67d0\u4e2a\u957f\u5ea6\u6765\u5904\u7406\u6570\u636e\uff08\u4f8b\u598232\u4f4d\uff09\uff0c\u5982\u679c\u6570\u636e\u7c7b\u578b\u7684\u5927\u5c0f\u6070\u597d\u548c\u8be5\u957f\u5ea6\u76f8\u540c\uff0c\u5219\u4f1a\u66f4\u5feb\u4e00\u4e9b\u3002\u5728\u8fd9\u6837\u7684\u8ba1\u7b97\u673a\u4e0a\uff0c32\u4f4d\u7684 int \u4f1a\u6bd4 16\u4e3a\u7684 int \u66f4\u5feb\u3002","title":"4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26"},{"location":"4-3-Object-sizes-and-the-sizeof-operator/#_1","text":"\u6b63\u5982\u5728 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb \u4e2d\u4ecb\u7ecd\u7684\u90a3\u6837\uff0c\u73b0\u4ee3\u8ba1\u7b97\u673a\u4ee5 \u5b57\u8282(byte) \u4e3a\u5355\u4f4d\u4f7f\u7528\u5185\u5b58\uff0c\u6bcf\u4e2a\u5b57\u8282\u7684\u5185\u5b58\u90fd\u6709\u5176\u552f\u4e00\u5bf9\u5e94\u7684\u5730\u5740\u3002\u4ece\u8fd9\u4e2a\u89d2\u5ea6\u6765\u8bf4\uff0c\u5185\u5b58\u53ef\u4ee5\u88ab\u60f3\u8c61\u6210\u4e00\u7cfb\u5217\u7684\u5c0f\u50a8\u85cf\u95f4\u6216\u4fe1\u7bb1\uff0c\u4f60\u53ef\u4ee5\u4ece\u4e2d\u5b58\u653e\u6216\u8bfb\u53d6\u4fe1\u606f\uff0c\u800c\u53d8\u91cf\u540d\u5219\u7528\u6765\u5bf9\u8fd9\u4e9b\u50a8\u85cf\u95f4\u6216\u4fe1\u7bb1\u8fdb\u884c\u8bbf\u95ee\u3002 \u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u6bd4\u55bb\u5e76\u4e0d\u5b8c\u5168\u51c6\u786e\u2014\u2014\u5f88\u591a\u5bf9\u8c61\u5b9e\u9645\u4e0a\u5360\u636e\u7684\u5185\u5b58\u8981\u5927\u4e8e1\u4e2a\u5b57\u8282\u3002\u4e00\u4e2a\u5bf9\u8c61\u53ef\u80fd\u4f1a\u4f7f\u75282\u30014\u30018\u6216\u8005\u66f4\u591a\u8fde\u7eed\u7684\u5185\u5b58\u5730\u5740\u3002\u800c\u5bf9\u8c61\u4f7f\u7528\u591a\u5927\u7684\u5185\u5b58\uff0c\u53d6\u51b3\u4e8e\u5bf9\u8c61\u7684\u6570\u636e\u7c7b\u578b\u3002 \u56e0\u4e3a\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u53d8\u91cf\u540d\u6765\u8bbf\u95ee\u5185\u5b58\uff08\u800c\u975e\u76f4\u63a5\u8bbf\u95ee\u5185\u5b58\uff09\uff0c\u7f16\u8bd1\u5668\u5e2e\u6211\u4eec\u9690\u85cf\u4e86\u4e0a\u8ff0\u7ec6\u8282\uff0c\u5373\u4e00\u4e2a\u5bf9\u8c61\u5360\u7528\u591a\u5c11\u5185\u5b58\u3002\u5f53\u4f7f\u7528\u53d8\u91cf x \u8bbf\u95ee\u5176\u5bf9\u5e94\u7684\u5185\u5b58\u65f6\uff0c\u7f16\u8bd1\u5668\u6307\u5b9a\u5e94\u8be5\u83b7\u53d6\u591a\u5c11\u5b57\u8282\u7684\u6570\u636e\uff08\u57fa\u4e8e x \u7684\u6570\u636e\u7c7b\u578b\uff09\uff0c\u5b83\u4e3a\u6211\u4eec\u5b8c\u6210\u4e86\u4e0a\u8ff0\u5de5\u4f5c\u3002 \u5373\u4f7f\u8fd9\u4e5f\uff0c\u5f88\u591a\u65f6\u5019\u77e5\u9053\u5bf9\u8c61\u5360\u7528\u591a\u5c11\u5185\u5b58\u662f\u6709\u7528\u7684\u3002 \u9996\u5148\uff0c\u5bf9\u8c61\u5360\u7528\u7684\u5185\u5b58\u51b3\u5b9a\u4e86\u8be5\u5bf9\u8c61\u80fd\u591f\u4fdd\u5b58\u591a\u5c11\u4fe1\u606f\u3002 \u4e00\u4e2a\u6bd4\u7279\u53ef\u4ee5\u4fdd\u5b58\u4e24\u4e2a\u53ef\u80fd\u7684\u503c\uff0c\u53730\u62161\uff1a bit 0 0 1 bit 0 bit 1 0 0 1 1 1 0 1 1 bit 0 bit 1 bit 2 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 \u6982\u62ec\u6765\u8bf4\uff0c\u5177\u6709 n \u4e2a \u4f4d \u5927\u5c0f\u7684\u5bf9\u8c61\uff08n\u4e3a\u6b63\u6574\u6570\uff09\u53ef\u4ee5\u4fdd\u5b58 \\(2^n\\) \u4e2d\u4e0d\u540c\u7684\u503c\uff082\u7684n\u6b21\u65b9\uff0c\u4e00\u822c\u4e5f\u5199\u4f5c 2^n \uff09\u3002\u56e0\u6b64\uff0c\u5bf9\u4e8e\u4e00\u4e2a8\u4e2a\u6bd4\u7279\u7684\u5b57\u8282\u6765\u8bf4\uff0c\u5927\u5c0f\u4f4d1\u5b57\u8282\u7684\u5bf9\u8c61\u53ef\u4ee5\u4fdd\u5b58 \\(2^8\\) (256)\u4e2d\u53ef\u80fd\u7684\u503c\u3002\u4f7f\u75282\u4e2a\u5b57\u8282\u7684\u5bf9\u8c61\u53ef\u4ee5\u4fdd\u5b58 \\(2^{16}\\) (65536)\u79cd\u4e0d\u540c\u7684\u503c\uff01 \u56e0\u6b64\uff0c\u5bf9\u8c61\u7684\u5927\u5c0f\u51b3\u5b9a\u4e86\u5b83\u80fd\u4fdd\u5b58\u7684\u4e0d\u540c\u503c\u7684\u4e0a\u9650\u2014\u2014\u4f7f\u7528\u7684\u5185\u5b58\u8d8a\u591a\uff0c\u80fd\u591f\u5b58\u653e\u7684\u4e0d\u540c\u503c\u4e5f\u5c31\u8d8a\u591a\u3002\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u5bf9\u6574\u578b\u8fdb\u884c\u66f4\u8be6\u7ec6\u5730\u4ecb\u7ecd\u3002 \u5176\u6b21\uff0c\u8ba1\u7b97\u673a\u7684\u5185\u5b58\u662f\u6709\u9650\u7684\u3002\u5f53\u6211\u4eec\u5b9a\u4e49\u5bf9\u8c61\u7684\u65f6\u5019\uff0c\u4e00\u5c0f\u5757\u7a7a\u95f2\u7684\u5185\u5b58\u5c31\u88ab\u5360\u7528\u4e86\uff08\u53ea\u8981\u8be5\u5bf9\u8c61\u8fd8\u5b58\u5728\uff09\u3002\u56e0\u4e3a\u73b0\u4ee3\u8ba1\u7b97\u673a\u7684\u5185\u5b58\u76f8\u5bf9\u6765\u8bf4\u6bd4\u8f83\u591a\uff0c\u6240\u4ee5\u5360\u7528\u4e00\u5c0f\u5757\u5185\u5b58\u5f71\u54cd\u4e0d\u5927\u3002\u4f46\u662f\uff0c\u5f53\u7a0b\u5e8f\u4e2d\u5177\u6709\u5927\u91cf\u5bf9\u8c61\u6216\u8005\u6570\u636e\u65f6\uff08\u4f8b\u5982\uff0c\u4e00\u4e2a\u6b63\u5728\u6e32\u67d3\u51e0\u767e\u4e07\u4e2a\u5730\u7262\u7684\u5927\u578b\u6e38\u620f\uff09\uff0c\u5bf9\u8c61\u5360\u75281\u4e2a\u5b57\u8282\u7684\u5927\u5c0f\u8fd8\u662f8\u4e2a\u5b57\u8282\u5c31\u4f1a\u9020\u6210\u5f88\u5927\u7684\u5dee\u522b\u3002 \u5173\u952e\u4fe1\u606f \u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u8fc7\u5206\u5173\u6ce8\u4f18\u5316\u4ed6\u4eec\u7684\u4ee3\u7801\u5e76\u4f7f\u5176\u7a0b\u5e8f\u80fd\u591f\u4f7f\u7528\u5c3d\u53ef\u80fd\u5c11\u7684\u5185\u5b58\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u5176\u5b9e\u5f71\u54cd\u4e0d\u5927\u3002\u4f60\u7684\u6ce8\u610f\u529b\u5e94\u8be5\u653e\u5728\u7f16\u5199\u6613\u4e8e\u7ef4\u62a4\u7684\u4ee3\u7801\uff0c\u53ea\u6709\u5f53\u4f18\u5316\u53ef\u4ee5\u5e26\u6765\u663e\u8457\u6536\u76ca\u7684\u65f6\u5019\u518d\u53bb\u505a\u3002","title":"\u5bf9\u8c61\u7684\u5927\u5c0f"},{"location":"4-3-Object-sizes-and-the-sizeof-operator/#_2","text":"\u63a5\u4e0b\u6765\u7684\u95ee\u9898\u5f88\u663e\u7136\uff1a\u201d\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7a76\u7adf\u5360\u7528\u591a\u5c11\u5185\u5b58\uff1f\u201c\u3002\u7b54\u6848\u53ef\u80fd\u4f1a\u4ee4\u4f60\u5403\u60ca\uff0c\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u5360\u7528\u7684\u5185\u5b58\u548c\u7f16\u8bd1\u5668\u4ee5\u53ca\uff08\u6216\uff09\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u6709\u5173\u3002 C++ \u53ea\u4fdd\u8bc1\u4e86\u6bcf\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\u6700\u5c0f\u7684\u5c3a\u5bf8\uff1a \u5206\u7c7b \u7c7b\u578b \u6700\u5c0f\u5c3a\u5bf8 \u5907\u6ce8 boolean bool 1 byte character char 1 byte \u603b\u662f1\u4e2a\u5b57\u8282 character wchat_t 1 byte character char16_t 2 byte character char32_t 4 byte integer short 2 byte integer int 2 byte integer long 4 byte integer long long 8 byte floating point float 4 byte floating point double 8 byte floating point long double 8 byte \u4e0d\u7136\uff0c\u8fd9\u4e9b\u53d8\u91cf\u7684\u5b9e\u9645\u5927\u5c0f\u548c\u4f60\u7684\u673a\u5668\u76f8\u5173\uff08\u4f8b\u5982 int \u901a\u5e38\u5c31\u662f4\u4e2a\u5b57\u8282\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u5904\u4e8e\u517c\u5bb9\u6027\u8003\u8651\uff0c\u4f60\u4e0d\u80fd\u591f\u5047\u8bbe\u53d8\u91cf\u7684\u5b9e\u9645\u5927\u5c0f\u4f1a\u5927\u4e8e\u5176\u6700\u5c0f\u503c\u3002","title":"\u57fa\u7840\u6570\u636e\u7c7b\u578b\u7684\u5927\u5c0f"},{"location":"4-3-Object-sizes-and-the-sizeof-operator/#sizeof","text":"\u4e3a\u4e86\u786e\u5b9a\u6570\u636e\u7c7b\u578b\u5728\u5177\u4f53\u673a\u5668\u4e0a\u5360\u7528\u7684\u5185\u5b58\u5927\u5c0f\uff0cC++ \u63d0\u4f9b\u4e86 size \u8fd0\u7b97\u7b26\u3002\u8be5\u8fd0\u7b97\u7b26\u662f\u4e00\u4e2a\u4e00\u5143\u8fd0\u7b97\u7b26\uff0c\u5b83\u4f7f\u7528\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\u6216\u53d8\u91cf\u4f5c\u4e3a \u64cd\u4f5c\u6570(operands) \uff0c\u8ba1\u7b97\u5f97\u5230\u8be5\u6570\u636e\u7c7b\u578b\u6216\u53d8\u91cf\u5360\u7528\u5185\u5b58\u7684\u5927\u5c0f\uff08\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\uff09\u3002\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u7f16\u8bd1\u548c\u8fd0\u884c\u4e0b\u9762\u7684\u7a0b\u5e8f\u4ee5\u4fbf\u786e\u5b9a\u5728\u4f60\u7684\u7535\u8111\u4e0a\uff0c\u5404\u4e2a\u6570\u636e\u7c7b\u578b\u90fd\u5360\u7528\u591a\u5c11\u5185\u5b58\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { std :: cout << \"bool: \\t\\t \" << sizeof ( bool ) << \" bytes \\n \" ; std :: cout << \"char: \\t\\t \" << sizeof ( char ) << \" bytes \\n \" ; std :: cout << \"wchar_t: \\t \" << sizeof ( wchar_t ) << \" bytes \\n \" ; std :: cout << \"char16_t: \\t \" << sizeof ( char16_t ) << \" bytes \\n \" ; std :: cout << \"char32_t: \\t \" << sizeof ( char32_t ) << \" bytes \\n \" ; std :: cout << \"short: \\t\\t \" << sizeof ( short ) << \" bytes \\n \" ; std :: cout << \"int: \\t\\t \" << sizeof ( int ) << \" bytes \\n \" ; std :: cout << \"long: \\t\\t \" << sizeof ( long ) << \" bytes \\n \" ; std :: cout << \"long long: \\t \" << sizeof ( long long ) << \" bytes \\n \" ; std :: cout << \"float: \\t\\t \" << sizeof ( float ) << \" bytes \\n \" ; std :: cout << \"double: \\t\\t \" << sizeof ( double ) << \" bytes \\n \" ; std :: cout << \"long double: \\t \" << sizeof ( long double ) << \" bytes \\n \" ; return 0 ; } \u4f5c\u8005\u4f7f\u7528\u7684\u662f 64 \u4f4d\u673a\u5668\uff0c\u901a\u8fc7 Visual Studio \u7f16\u8bd1\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u8f93\u51fa\u7684\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 bool: 1 bytes char: 1 bytes wchar_t: 2 bytes char16_t: 2 bytes char32_t: 4 bytes short: 2 bytes int: 4 bytes long: 4 bytes long long: 8 bytes float: 4 bytes double: 8 bytes long double: 8 bytes \u4f60\u5f97\u5230\u7684\u7ed3\u679c\u53ef\u80fd\u4f1a\u548c\u6b64\u5904\u4e0d\u4e00\u6837\uff0c\u8fd9\u53d6\u51b3\u4e8e\u662f\u5426\u4f7f\u7528\u4e86\u4e0d\u540c\u7684\u8ba1\u7b97\u673a\u6216\u7f16\u8bd1\u5668\u3002\u6ce8\u610f\uff1a\u4f60\u4e0d\u80fd\u4f7f\u7528 sizeof \u8ba1\u7b97 void \u7684\u5927\u5c0f\uff0c\u56e0\u4e3a\u5b83\u6ca1\u6709\u5927\u5c0f\uff08\u8fd9\u4e48\u505a\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\uff09\u3002 \u6269\u5c55\u9605\u8bfb \u4e5f\u8bb8\u4f60\u6709\u5174\u8da3\u77e5\u9053\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u7684 \\t \u6709\u4ec0\u4e48\u4f5c\u7528\u3002\u8fd9\u662f\u4e00\u4e2a\u7279\u6b8a\u7b26\u53f7\uff0c\u7528\u4e8e\u63d2\u5165\u4e00\u4e2a\u5236\u8868\u7b26\uff08\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b83\u5c06\u8f93\u51fa\u7ed3\u679c\u6309\u5217\u5bf9\u5176\uff09\u3002\u6211\u4eec\u4f1a\u5728 4.11 - \u5b57\u7b26 \u4e2d\u4ecb\u7ecd \\t \u548c\u5176\u4ed6\u7684\u7279\u6b8a\u5b57\u7b26\u3002 \u4f60\u4e5f\u53ef\u4ee5\u5bf9\u53d8\u91cf\u540d\u4f7f\u7528 sizeof \u3002 1 2 3 4 5 6 7 8 9 #include int main () { int x {}; std :: cout << \"x is \" << sizeof ( x ) << \" bytes \\n \" ; return 0 ; } 1 x is 4 bytes \u8bd1\u8005\u6ce8 \u5bf9\u6307\u9488\u4f7f\u7528 sizeof \u7684\u7ed3\u679c\u662f\u56fa\u5b9a\u7684\uff0c\u4e0d\u53d6\u51b3\u4e8e\u6307\u9488\u6307\u5411\u4ec0\u4e48\u7c7b\u578b\u7684\u6570\u636e\u3002\u901a\u5e38\u4e3a4\u6216\u80058\u4e2a\u5b57\u8282\u3002","title":"sizeof \u8fd0\u7b97\u7b26"},{"location":"4-3-Object-sizes-and-the-sizeof-operator/#_3","text":"\u5728\u73b0\u4ee3\u8ba1\u7b97\u673a\u4e0a\uff0c\u57fa\u7840\u6570\u636e\u7c7b\u578b\u7684\u5bf9\u8c61\u6027\u80fd\u901a\u5e38\u90fd\u975e\u5e38\u597d\uff0c\u56e0\u6b64\u8fd9\u4e9b\u53d8\u91cf\u7684\u6027\u80fd\u95ee\u9898\u901a\u5e38\u4e0d\u9700\u8981\u6211\u4eec\u7279\u522b\u8003\u8651\u3002 \u9898\u5916\u8bdd \u4f60\u53ef\u80fd\u4f1a\u60f3\u5f53\u7136\u5730\u8ba4\u4e3a\u4f7f\u7528\u5185\u5b58\u5c11\u7684\u6570\u636e\u7ed3\u6784\u901f\u5ea6\u4e5f\u4f1a\u66f4\u5feb\uff0c\u5b9e\u9645\u4e0a\u5e76\u4e0d\u603b\u662f\u8fd9\u6837\u3002CPU \u901a\u5e38\u4f1a\u5bf9\u6570\u636e\u7684\u6d41\u7a0b\u8fdb\u884c\u4f18\u5316\uff0c\u6309\u7167\u67d0\u4e2a\u957f\u5ea6\u6765\u5904\u7406\u6570\u636e\uff08\u4f8b\u598232\u4f4d\uff09\uff0c\u5982\u679c\u6570\u636e\u7c7b\u578b\u7684\u5927\u5c0f\u6070\u597d\u548c\u8be5\u957f\u5ea6\u76f8\u540c\uff0c\u5219\u4f1a\u66f4\u5feb\u4e00\u4e9b\u3002\u5728\u8fd9\u6837\u7684\u8ba1\u7b97\u673a\u4e0a\uff0c32\u4f4d\u7684 int \u4f1a\u6bd4 16\u4e3a\u7684 int \u66f4\u5feb\u3002","title":"\u57fa\u672c\u6570\u636e\u7ed3\u6784\u7684\u6027\u80fd"},{"location":"4-4-Signed-integers/","tags":["data type","integer","undefined behavior"],"text":"Key Takeaway n \u4f4d\u6709\u7b26\u53f7\u6574\u578b\u7684\u8303\u56f4\u4e3a \\(-(2^{n-1})\\) \u5230 \\(2^{n-1}-1\\) \u3002 \u6709\u7b26\u53f7\u6574\u578b\u7684\u6ea2\u51fa\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u6574\u578b\u662f\u4e00\u79cd\u6574\u6570\u7c7b\u578b\uff0c\u5b83\u53ef\u4ee5\u8868\u793a\u6b63\u6574\u6570\u548c\u8d1f\u6574\u6570\uff0c\u5f53\u7136\u4e5f\u5305\u62ec 0\uff08\u4f8b\u5982\uff1a-2\u3001-1\u30010\u30011\u30012\uff09\u3002C++ \u4e2d\u6709 4 \u4e2d\u57fa\u7840\u6574\u578b\uff1a Type Minimum Size Note short 16 bits int 16 bits \u73b0\u4ee3\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u4e0b\u901a\u5e38\u4e3a 32 \u4f4d long 32 bits long long 64 bits \u8fd9\u4e9b\u7c7b\u578b\u7684\u4e3b\u8981\u5dee\u5f02\u662f\u5b83\u4eec\u7684\u5927\u5c0f\u4e0d\u540c\u2014\u2014\u8d8a\u5927\u7684\u6574\u578b\u53ef\u4ee5\u8868\u793a\u8d8a\u5927\u7684\u6570\u3002 \u63d0\u9192 C++ \u53ea\u4fdd\u8bc1\u6574\u578b\u5360\u7528\u7a7a\u95f4\u7684\u6700\u5c0f\u503c\uff0c\u800c\u4e0d\u4fdd\u8bc1\u5176\u5177\u4f53\u503c\u3002\u8bf7\u53c2\u8003 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 \u6765\u786e\u5b9a\u6bcf\u79cd\u7c7b\u578b\u5728\u4f60\u673a\u5668\u4e0a\u7684\u5177\u4f53\u5927\u5c0f\u3002 \u6709\u7b26\u53f7\u6574\u578b \u5728\u65e5\u5e38\u751f\u6d3b\u4e2d\uff0c\u5982\u679c\u6211\u4eec\u8981\u8868\u793a\u8d1f\u6574\u6570\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528\u8d1f\u53f7\u3002\u4f8b\u5982\uff0c -3 \u8868\u793a \u201c\u8d1f\u4e09\u201d\u3002\u6211\u4eec\u8fd8\u4f1a\u4f7f\u7528+3 \u6765\u8868\u793a\u201d\u6b63\u4e09\u201c\uff08\u5c3d\u7ba1\u901a\u5e38\u6b63\u53f7\u4f1a\u88ab\u7701\u7565\uff09\u3002\u6570\u5b57\u7684\u8fd9\u79cd\u201c\u6b63\u8d1f\u96f6\u201d\u5c5e\u6027\uff0c\u79f0\u4e3a\u6570\u5b57\u7684\u8d1f\u53f7\u3002 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6574\u578b\u662f\u6709\u7b26\u53f7\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\u6570\u5b57\u7684\u7b26\u53f7\u662f\u6570\u5b57\u672c\u8eab\u7684\u4e00\u90e8\u5206\uff08\u4f7f\u7528\u4e00\u4e2a\u4f4d\u8fdb\u884c\u5b58\u50a8\uff0c\u79f0\u4e3a\u7b26\u53f7\u4f4d\uff09\u3002\u56e0\u6b64\uff0c\u6709\u7b26\u53f7\u6574\u578b\u53ef\u4ee5\u8868\u793a\u6b63\u6570\u8d1f\u6570\u548c 0\u3002 \u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u4e13\u6ce8\u4e8e\u4ecb\u7ecd\u6709\u7b26\u53f7\u6574\u578b(signed)\u3002\u65e0\u7b26\u53f7(unsigned)\u6574\u578b\uff08\u53ea\u80fd\u4fdd\u5b58\u975e\u8d1f\u503c\uff09\u5219\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u8ba8\u8bba\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u5728 O.4 -- Converting between binary and decimal \u4e2d\u8ba8\u8bba\u4e86\u4e8c\u8fdb\u5236\u8868\u793a\u6cd5\u65f6\u7b26\u53f7\u4f4d\u5982\u4f55\u4f7f\u7528\u3002 \u5b9a\u4e49\u6709\u7b26\u53f7\u6574\u578b \u5b9a\u4e49\u6709\u7b26\u53f7\u6574\u578b\u65f6\uff0c\u63a8\u8350\u7684\u65b9\u5f0f\u5982\u4e0b\uff1a 1 2 3 4 short s ; int i ; long l ; long long ll ; \u6240\u6709\u7684\u6574\u578b(\u9664\u4e86 int )\u4ee5\u5916\uff0c\u90fd\u53ef\u4ee5\u5e26\u4e0a\u4e00\u4e2a\u53ef\u9009\u7684 int \u540e\u7f00\uff1a 1 2 3 short int si ; //short int \u4e2d\u7684 int \u5373\u53ef\u9009\u540e\u7f00 long int li ; long long int lli ; \u4e0d\u8fc7\uff0c\u5e76\u4e0d\u63a8\u8350\u4f7f\u7528\u8fd9\u4e9b\u540e\u7f00\u3002\u4f7f\u7528\u8fd9\u4e9b\u540e\u7f00\u4e0d\u4ec5\u9700\u8981\u6253\u66f4\u591a\u5b57\uff0c\u800c\u4e14\u6dfb\u52a0 int \u540e\u7f00\u540e\uff0c\u4e0d\u5bb9\u6613\u5c06\u8fd9\u4e9b\u7c7b\u578b\u548c int \u7c7b\u578b\u7684\u53d8\u91cf\u533a\u5206\u5f00\u6765\uff0c\u800c\u4e14\u5982\u679c\u4e0d\u7ecf\u610f\u5fd8\u8bb0\u4e86 short \u6216\u8005 long \u4fee\u9970\u7b26\u7684\u8bdd\u5219\u53ef\u80fd\u4f1a\u5f15\u5165\u95ee\u9898\u3002 \u6574\u578b\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u53ef\u9009\u7684 singed \u5173\u952e\u5b57\uff0c\u901a\u5e38\u4f1a\u5c06\u5176\u653e\u7f6e\u5728\u7c7b\u578b\u540d\u524d\uff1a 1 2 3 4 signed short ss ; signed int si ; signed long sl ; signed long long sll ; \u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u5173\u952e\u5b57\u4e5f\u4e0d\u63a8\u8350\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u662f\u591a\u4f59\u7684\uff0c\u6574\u578b\u9ed8\u8ba4\u5c31\u662f\u6709\u7b26\u53f7\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u6700\u7cbe\u7b80\u7684\u5199\u6cd5\uff0c\u4e0d\u8981\u4f7f\u7528 int \u540e\u7f00\u6216 signed \u524d\u7f00\u3002 \u6709\u7b26\u53f7\u6574\u578b\u7684\u8303\u56f4 \u6b63\u5982\u4e4b\u524d\u4ecb\u7ecd\u7684\u90a3\u6837\uff0c\u5728\u5185\u5b58\u4e2d\u5360\u7528 n \u4f4d\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u4fdd\u5b58 \\(2^n\\) \u4e2a\u53ef\u80fd\u7684\u503c\u3002\u4f46\u662f\uff0c\u5177\u4f53\u662f\u54ea\u4e9b\u503c\u5462\uff1f\u6211\u4eec\u79f0\u4e00\u79cd\u7c7b\u578b\u53ef\u4ee5\u8868\u793a\u7684\u503c\u4e3a\u5176\u8303\u56f4\u3002\u800c\u6574\u578b\u7684\u8303\u56f4\u6709\u4e24\u4e2a\u56e0\u7d20\u51b3\u5b9a\uff1a\u5927\u5c0f\uff08\u591a\u5c11\u4f4d\uff09\uff0c\u662f\u5426\u6709\u7b26\u53f7\u3002 \u6839\u636e\u5b9a\u4e49\uff0c8 \u4f4d\u6709\u7b26\u53f7\u6574\u578b\u53ef\u4ee5\u8868\u793a\u7684\u8303\u56f4\u662f\uff1a-128 \u5230 127\u3002\u4e5f\u5c31\u662f\u8bf4\u4e00\u4e2a\u6709\u7b26\u53f7\u6574\u578b\u53ef\u4ee5\u5b89\u5168\u5730\u8868\u793a -128 \u5230 127 (\u5305\u62ec) \u4e2d\u7684\u4efb\u610f\u6574\u6570\u3002 \u9898\u5916\u8bdd \u6570\u5b66\u65f6\u95f4\uff1a8 \u4f4d\u6574\u578b\u6709 8 \u4e2a\u4f4d\uff0c2 \u7684 8 \u6b21\u65b9\u662f 256\uff0c\u6240\u4ee5\u5b83\u4e00\u5171\u53ef\u4ee5\u4fdd\u5b58 256 \u4e2a\u53ef\u80fd\u7684\u503c\u3002\u5728 -128 \u5230 127 \u4e2d\u4e00\u5171\u6709 256 \u4e2a\u53ef\u80fd\u7684\u6574\u6570\u503c\u3002 \u4e0b\u8868\u5c55\u793a\u4e86\u4e0d\u540c\u6709\u7b26\u53f7\u6574\u578b\u53ef\u4ee5\u8868\u793a\u7684\u8303\u56f4\uff1a \u5927\u5c0f/\u7c7b\u578b \u8303\u56f4 8 bit signed -128 \u5230 127 16 bit signed -32,768 \u5230 32,767 32 bit signed -2,147,483,648 \u5230 2,147,483,647 64 bit signed -9,223,372,036,854,775,808 \u5230 9,223,372,036,854,775,807 \u5bf9\u4e8e\u559c\u6b22\u6570\u5b66\u8868\u793a\u6cd5\u7684\u4eba\u6765\u8bf4\uff1a n \u4f4d\u6709\u7b26\u53f7\u6574\u578b\u7684\u8303\u56f4\u4e3a \\(-(2^{n-1})\\) \u5230 \\(2^{n-1}-1\\) \u3002 \u5bf9\u4e8e\u4e0d\u559c\u6b22\u6570\u5b66\u7684\u4eba\u6765\u8bf4\uff0c\u53c2\u8003\u4e0a\u8868\u5c31\u53ef\u4ee5\u4e86\u3002 \u6574\u6570\u6ea2\u51fa \u5982\u679c\u6211\u4eec\u5c1d\u8bd5\u628a 280 \u8d4b\u503c\u7ed9\u4e00\u4e2a 8 \u4f4d\u6709\u7b26\u53f7\u6574\u578b\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f\u8fd9\u4e2a\u6570\u5b57\u5df2\u7ecf\u8d85\u51fa\u4e86 8 \u4f4d\u6709\u7b26\u53f7\u6574\u578b\u6570\u80fd\u591f\u8868\u793a\u7684\u8303\u56f4\u3002280 \u9700\u8981 9 \u4e2a\u4f4d\uff08\u52a0\u4e00\u4e2a\u7b26\u53f7\u4f4d\uff09\u624d\u80fd\u8868\u793a\uff0c\u4f46\u662f\u4e00\u4e2a8\u4f4d\u6574\u578b\u53ea\u67097\u4e2a\u4f4d\uff08\u52a0\u4e00\u4e2a\u7b26\u53f7\u4f4d\uff09\u3002 \u5f53\u9700\u8981\u5b58\u50a8\u7684\u503c\u7684\u8303\u56f4\u8d85\u8fc7\u4e86\u53d8\u91cf\u53ef\u4ee5\u8868\u793a\u7684\u8303\u56f4\uff0c\u5c31\u4f1a\u53d1\u751f\u6574\u6570\u6ea2\u51fa(\u901a\u5e38\u7b80\u79f0\u4e3a\u6ea2\u51fa) \u3002\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u8868\u793a\u7684\u6570\uff0c\u9700\u8981\u6bd4\u8be5\u53d8\u91cf\u80fd\u591f\u4f7f\u7528\u7684\u66f4\u591a\u7684\u4f4d\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53d8\u91cf\u5c06\u6ca1\u6709\u8db3\u591f\u7684\u5185\u5b58\u7a7a\u95f4\u53ef\u4ee5\u7528\u6765\u5b58\u653e\u6570\u636e\u3002 \u5bf9\u4e8e\u6709\u7b26\u53f7\u6574\u578b\u6765\u8bf4\uff0c\u7a76\u7adf\u54ea\u4e2a\u4f4d\u7684\u6570\u636e\u4f1a\u88ab\u4e22\u5f03\u5e76\u6ca1\u6709\u660e\u786e\u7684\u5b9a\u4e49\uff0c\u56e0\u6b64\u6709\u7b26\u53f7\u6574\u578b\u7684\u6ea2\u51fa\u662f\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 \u6ce8\u610f \u6709\u7b26\u53f7\u6574\u578b\u7684\u6ea2\u51fa\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a \u901a\u5e38\u6765\u8bf4\uff0c\u6ea2\u51fa\u4f1a\u5bfc\u81f4\u4fe1\u606f\u7684\u4e22\u5931\uff0c\u8fd9\u662f\u6211\u4eec\u6700\u4e0d\u5e0c\u671b\u53d1\u751f\u7684\u662f\u3002\u5982\u679c\u4f60\u6000\u7591\u5bf9\u8c61\u9700\u8981\u4fdd\u5b58\u7684\u503c\u6709\u53ef\u80fd\u4f1a\u8d85\u8fc7\u5b83\u80fd\u591f\u8868\u793a\u7684\u8303\u56f4\uff0c\u8bf7\u4f7f\u7528\u8303\u56f4\u66f4\u5927\u7684\u7c7b\u578b\uff01 \u6574\u578b\u9664\u6cd5 \u5f53\u5bf9\u4e24\u4e2a\u6574\u578b\u8fdb\u884c\u9664\u6cd5\u8fd0\u7b97\u65f6\uff0c \u5982\u679c\u5546\u662f\u6574\u6570\uff0c\u5219\u7ed3\u679c\u5e94\u8be5\u5982\u4f60\u6240\u6599\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << 20 / 4 ; return 0 ; } \u8ba1\u7b97\u7ed3\u679c\u4e3a 5\u3002 \u4f46\u662f\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u8fd9\u4e2a\u5546\u662f\u5c0f\u6570\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << 8 / 5 ; return 0 ; } \u8ba1\u7b97\u7ed3\u679c\u4e3a 1\u3002 \u5f53 C++\u5bf9\u4e24\u4e2a\u6574\u578b\u505a\u9664\u6cd5\u65f6\uff08\u79f0\u4e3a\u6574\u6570\u9664\u6cd5\uff09\uff0c\u5b83\u53ea\u80fd\u4ea7\u751f\u6574\u6570\u7ed3\u679c\uff0c\u56e0\u4e3a\u6574\u578b\u4e0d\u80fd\u4fdd\u5b58\u5c0f\u6570\u503c\uff0c\u7ed3\u679c\u4e2d\u7684\u5c0f\u6570\u90e8\u5206\u4f1a\u88ab \u4e22\u5f03 \uff08\u800c\u4e0d\u662f\u56db\u820d\u4e94\u5165\uff09\u3002 \u4ed4\u7ec6\u770b\u770b\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c8 / 5 \u5e94\u8be5\u7b49\u4e8e1.6\uff0c\u4f46\u662f\u5c0f\u6570\u90e8\u5206(0.6)\u4f1a\u88ab\u4e22\u5f03\uff0c\u6240\u4ee5\u7ed3\u679c\u4e3a1\u3002 \u540c\u6837\u7684\uff0c-8 / 5 \u7684\u7ed3\u679c\u4e3a -1\u3002 \u6ce8\u610f \u5728\u4f7f\u7528\u6574\u6570\u9664\u6cd5\u65f6\u8981\u5c0f\u5fc3\uff0c\u56e0\u4e3a\u5546\u4e2d\u7684\u5c0f\u6570\u90e8\u5206\u4f1a\u88ab\u4e22\u5f03\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u8fd9\u6070\u597d\u662f\u4f60\u671f\u671b\u7684\uff0c\u90a3\u4e48\u53ef\u4ee5\u653e\u5fc3\u4f7f\u7528\u3002 \u5982\u679c\u4f60\u5e0c\u671b\u4fdd\u7559\u5c0f\u6570\u90e8\u5206\uff0c\u8bf7\u53c2\u8003 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 \u4e2d\u4ecb\u7ecd\u7684\u65b9\u6cd5\u3002","title":"4.4 - \u6709\u7b26\u53f7\u6574\u578b"},{"location":"4-4-Signed-integers/#_1","text":"\u5728\u65e5\u5e38\u751f\u6d3b\u4e2d\uff0c\u5982\u679c\u6211\u4eec\u8981\u8868\u793a\u8d1f\u6574\u6570\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528\u8d1f\u53f7\u3002\u4f8b\u5982\uff0c -3 \u8868\u793a \u201c\u8d1f\u4e09\u201d\u3002\u6211\u4eec\u8fd8\u4f1a\u4f7f\u7528+3 \u6765\u8868\u793a\u201d\u6b63\u4e09\u201c\uff08\u5c3d\u7ba1\u901a\u5e38\u6b63\u53f7\u4f1a\u88ab\u7701\u7565\uff09\u3002\u6570\u5b57\u7684\u8fd9\u79cd\u201c\u6b63\u8d1f\u96f6\u201d\u5c5e\u6027\uff0c\u79f0\u4e3a\u6570\u5b57\u7684\u8d1f\u53f7\u3002 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6574\u578b\u662f\u6709\u7b26\u53f7\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\u6570\u5b57\u7684\u7b26\u53f7\u662f\u6570\u5b57\u672c\u8eab\u7684\u4e00\u90e8\u5206\uff08\u4f7f\u7528\u4e00\u4e2a\u4f4d\u8fdb\u884c\u5b58\u50a8\uff0c\u79f0\u4e3a\u7b26\u53f7\u4f4d\uff09\u3002\u56e0\u6b64\uff0c\u6709\u7b26\u53f7\u6574\u578b\u53ef\u4ee5\u8868\u793a\u6b63\u6570\u8d1f\u6570\u548c 0\u3002 \u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u4e13\u6ce8\u4e8e\u4ecb\u7ecd\u6709\u7b26\u53f7\u6574\u578b(signed)\u3002\u65e0\u7b26\u53f7(unsigned)\u6574\u578b\uff08\u53ea\u80fd\u4fdd\u5b58\u975e\u8d1f\u503c\uff09\u5219\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u8ba8\u8bba\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u5728 O.4 -- Converting between binary and decimal \u4e2d\u8ba8\u8bba\u4e86\u4e8c\u8fdb\u5236\u8868\u793a\u6cd5\u65f6\u7b26\u53f7\u4f4d\u5982\u4f55\u4f7f\u7528\u3002","title":"\u6709\u7b26\u53f7\u6574\u578b"},{"location":"4-4-Signed-integers/#_2","text":"\u5b9a\u4e49\u6709\u7b26\u53f7\u6574\u578b\u65f6\uff0c\u63a8\u8350\u7684\u65b9\u5f0f\u5982\u4e0b\uff1a 1 2 3 4 short s ; int i ; long l ; long long ll ; \u6240\u6709\u7684\u6574\u578b(\u9664\u4e86 int )\u4ee5\u5916\uff0c\u90fd\u53ef\u4ee5\u5e26\u4e0a\u4e00\u4e2a\u53ef\u9009\u7684 int \u540e\u7f00\uff1a 1 2 3 short int si ; //short int \u4e2d\u7684 int \u5373\u53ef\u9009\u540e\u7f00 long int li ; long long int lli ; \u4e0d\u8fc7\uff0c\u5e76\u4e0d\u63a8\u8350\u4f7f\u7528\u8fd9\u4e9b\u540e\u7f00\u3002\u4f7f\u7528\u8fd9\u4e9b\u540e\u7f00\u4e0d\u4ec5\u9700\u8981\u6253\u66f4\u591a\u5b57\uff0c\u800c\u4e14\u6dfb\u52a0 int \u540e\u7f00\u540e\uff0c\u4e0d\u5bb9\u6613\u5c06\u8fd9\u4e9b\u7c7b\u578b\u548c int \u7c7b\u578b\u7684\u53d8\u91cf\u533a\u5206\u5f00\u6765\uff0c\u800c\u4e14\u5982\u679c\u4e0d\u7ecf\u610f\u5fd8\u8bb0\u4e86 short \u6216\u8005 long \u4fee\u9970\u7b26\u7684\u8bdd\u5219\u53ef\u80fd\u4f1a\u5f15\u5165\u95ee\u9898\u3002 \u6574\u578b\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u53ef\u9009\u7684 singed \u5173\u952e\u5b57\uff0c\u901a\u5e38\u4f1a\u5c06\u5176\u653e\u7f6e\u5728\u7c7b\u578b\u540d\u524d\uff1a 1 2 3 4 signed short ss ; signed int si ; signed long sl ; signed long long sll ; \u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u5173\u952e\u5b57\u4e5f\u4e0d\u63a8\u8350\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u662f\u591a\u4f59\u7684\uff0c\u6574\u578b\u9ed8\u8ba4\u5c31\u662f\u6709\u7b26\u53f7\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u6700\u7cbe\u7b80\u7684\u5199\u6cd5\uff0c\u4e0d\u8981\u4f7f\u7528 int \u540e\u7f00\u6216 signed \u524d\u7f00\u3002","title":"\u5b9a\u4e49\u6709\u7b26\u53f7\u6574\u578b"},{"location":"4-4-Signed-integers/#_3","text":"\u6b63\u5982\u4e4b\u524d\u4ecb\u7ecd\u7684\u90a3\u6837\uff0c\u5728\u5185\u5b58\u4e2d\u5360\u7528 n \u4f4d\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u4fdd\u5b58 \\(2^n\\) \u4e2a\u53ef\u80fd\u7684\u503c\u3002\u4f46\u662f\uff0c\u5177\u4f53\u662f\u54ea\u4e9b\u503c\u5462\uff1f\u6211\u4eec\u79f0\u4e00\u79cd\u7c7b\u578b\u53ef\u4ee5\u8868\u793a\u7684\u503c\u4e3a\u5176\u8303\u56f4\u3002\u800c\u6574\u578b\u7684\u8303\u56f4\u6709\u4e24\u4e2a\u56e0\u7d20\u51b3\u5b9a\uff1a\u5927\u5c0f\uff08\u591a\u5c11\u4f4d\uff09\uff0c\u662f\u5426\u6709\u7b26\u53f7\u3002 \u6839\u636e\u5b9a\u4e49\uff0c8 \u4f4d\u6709\u7b26\u53f7\u6574\u578b\u53ef\u4ee5\u8868\u793a\u7684\u8303\u56f4\u662f\uff1a-128 \u5230 127\u3002\u4e5f\u5c31\u662f\u8bf4\u4e00\u4e2a\u6709\u7b26\u53f7\u6574\u578b\u53ef\u4ee5\u5b89\u5168\u5730\u8868\u793a -128 \u5230 127 (\u5305\u62ec) \u4e2d\u7684\u4efb\u610f\u6574\u6570\u3002 \u9898\u5916\u8bdd \u6570\u5b66\u65f6\u95f4\uff1a8 \u4f4d\u6574\u578b\u6709 8 \u4e2a\u4f4d\uff0c2 \u7684 8 \u6b21\u65b9\u662f 256\uff0c\u6240\u4ee5\u5b83\u4e00\u5171\u53ef\u4ee5\u4fdd\u5b58 256 \u4e2a\u53ef\u80fd\u7684\u503c\u3002\u5728 -128 \u5230 127 \u4e2d\u4e00\u5171\u6709 256 \u4e2a\u53ef\u80fd\u7684\u6574\u6570\u503c\u3002 \u4e0b\u8868\u5c55\u793a\u4e86\u4e0d\u540c\u6709\u7b26\u53f7\u6574\u578b\u53ef\u4ee5\u8868\u793a\u7684\u8303\u56f4\uff1a \u5927\u5c0f/\u7c7b\u578b \u8303\u56f4 8 bit signed -128 \u5230 127 16 bit signed -32,768 \u5230 32,767 32 bit signed -2,147,483,648 \u5230 2,147,483,647 64 bit signed -9,223,372,036,854,775,808 \u5230 9,223,372,036,854,775,807 \u5bf9\u4e8e\u559c\u6b22\u6570\u5b66\u8868\u793a\u6cd5\u7684\u4eba\u6765\u8bf4\uff1a n \u4f4d\u6709\u7b26\u53f7\u6574\u578b\u7684\u8303\u56f4\u4e3a \\(-(2^{n-1})\\) \u5230 \\(2^{n-1}-1\\) \u3002 \u5bf9\u4e8e\u4e0d\u559c\u6b22\u6570\u5b66\u7684\u4eba\u6765\u8bf4\uff0c\u53c2\u8003\u4e0a\u8868\u5c31\u53ef\u4ee5\u4e86\u3002","title":"\u6709\u7b26\u53f7\u6574\u578b\u7684\u8303\u56f4"},{"location":"4-4-Signed-integers/#_4","text":"\u5982\u679c\u6211\u4eec\u5c1d\u8bd5\u628a 280 \u8d4b\u503c\u7ed9\u4e00\u4e2a 8 \u4f4d\u6709\u7b26\u53f7\u6574\u578b\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f\u8fd9\u4e2a\u6570\u5b57\u5df2\u7ecf\u8d85\u51fa\u4e86 8 \u4f4d\u6709\u7b26\u53f7\u6574\u578b\u6570\u80fd\u591f\u8868\u793a\u7684\u8303\u56f4\u3002280 \u9700\u8981 9 \u4e2a\u4f4d\uff08\u52a0\u4e00\u4e2a\u7b26\u53f7\u4f4d\uff09\u624d\u80fd\u8868\u793a\uff0c\u4f46\u662f\u4e00\u4e2a8\u4f4d\u6574\u578b\u53ea\u67097\u4e2a\u4f4d\uff08\u52a0\u4e00\u4e2a\u7b26\u53f7\u4f4d\uff09\u3002 \u5f53\u9700\u8981\u5b58\u50a8\u7684\u503c\u7684\u8303\u56f4\u8d85\u8fc7\u4e86\u53d8\u91cf\u53ef\u4ee5\u8868\u793a\u7684\u8303\u56f4\uff0c\u5c31\u4f1a\u53d1\u751f\u6574\u6570\u6ea2\u51fa(\u901a\u5e38\u7b80\u79f0\u4e3a\u6ea2\u51fa) \u3002\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u8868\u793a\u7684\u6570\uff0c\u9700\u8981\u6bd4\u8be5\u53d8\u91cf\u80fd\u591f\u4f7f\u7528\u7684\u66f4\u591a\u7684\u4f4d\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53d8\u91cf\u5c06\u6ca1\u6709\u8db3\u591f\u7684\u5185\u5b58\u7a7a\u95f4\u53ef\u4ee5\u7528\u6765\u5b58\u653e\u6570\u636e\u3002 \u5bf9\u4e8e\u6709\u7b26\u53f7\u6574\u578b\u6765\u8bf4\uff0c\u7a76\u7adf\u54ea\u4e2a\u4f4d\u7684\u6570\u636e\u4f1a\u88ab\u4e22\u5f03\u5e76\u6ca1\u6709\u660e\u786e\u7684\u5b9a\u4e49\uff0c\u56e0\u6b64\u6709\u7b26\u53f7\u6574\u578b\u7684\u6ea2\u51fa\u662f\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 \u6ce8\u610f \u6709\u7b26\u53f7\u6574\u578b\u7684\u6ea2\u51fa\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a \u901a\u5e38\u6765\u8bf4\uff0c\u6ea2\u51fa\u4f1a\u5bfc\u81f4\u4fe1\u606f\u7684\u4e22\u5931\uff0c\u8fd9\u662f\u6211\u4eec\u6700\u4e0d\u5e0c\u671b\u53d1\u751f\u7684\u662f\u3002\u5982\u679c\u4f60\u6000\u7591\u5bf9\u8c61\u9700\u8981\u4fdd\u5b58\u7684\u503c\u6709\u53ef\u80fd\u4f1a\u8d85\u8fc7\u5b83\u80fd\u591f\u8868\u793a\u7684\u8303\u56f4\uff0c\u8bf7\u4f7f\u7528\u8303\u56f4\u66f4\u5927\u7684\u7c7b\u578b\uff01","title":"\u6574\u6570\u6ea2\u51fa"},{"location":"4-4-Signed-integers/#_5","text":"\u5f53\u5bf9\u4e24\u4e2a\u6574\u578b\u8fdb\u884c\u9664\u6cd5\u8fd0\u7b97\u65f6\uff0c \u5982\u679c\u5546\u662f\u6574\u6570\uff0c\u5219\u7ed3\u679c\u5e94\u8be5\u5982\u4f60\u6240\u6599\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << 20 / 4 ; return 0 ; } \u8ba1\u7b97\u7ed3\u679c\u4e3a 5\u3002 \u4f46\u662f\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u8fd9\u4e2a\u5546\u662f\u5c0f\u6570\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 #include int main () { std :: cout << 8 / 5 ; return 0 ; } \u8ba1\u7b97\u7ed3\u679c\u4e3a 1\u3002 \u5f53 C++\u5bf9\u4e24\u4e2a\u6574\u578b\u505a\u9664\u6cd5\u65f6\uff08\u79f0\u4e3a\u6574\u6570\u9664\u6cd5\uff09\uff0c\u5b83\u53ea\u80fd\u4ea7\u751f\u6574\u6570\u7ed3\u679c\uff0c\u56e0\u4e3a\u6574\u578b\u4e0d\u80fd\u4fdd\u5b58\u5c0f\u6570\u503c\uff0c\u7ed3\u679c\u4e2d\u7684\u5c0f\u6570\u90e8\u5206\u4f1a\u88ab \u4e22\u5f03 \uff08\u800c\u4e0d\u662f\u56db\u820d\u4e94\u5165\uff09\u3002 \u4ed4\u7ec6\u770b\u770b\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c8 / 5 \u5e94\u8be5\u7b49\u4e8e1.6\uff0c\u4f46\u662f\u5c0f\u6570\u90e8\u5206(0.6)\u4f1a\u88ab\u4e22\u5f03\uff0c\u6240\u4ee5\u7ed3\u679c\u4e3a1\u3002 \u540c\u6837\u7684\uff0c-8 / 5 \u7684\u7ed3\u679c\u4e3a -1\u3002 \u6ce8\u610f \u5728\u4f7f\u7528\u6574\u6570\u9664\u6cd5\u65f6\u8981\u5c0f\u5fc3\uff0c\u56e0\u4e3a\u5546\u4e2d\u7684\u5c0f\u6570\u90e8\u5206\u4f1a\u88ab\u4e22\u5f03\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u8fd9\u6070\u597d\u662f\u4f60\u671f\u671b\u7684\uff0c\u90a3\u4e48\u53ef\u4ee5\u653e\u5fc3\u4f7f\u7528\u3002 \u5982\u679c\u4f60\u5e0c\u671b\u4fdd\u7559\u5c0f\u6570\u90e8\u5206\uff0c\u8bf7\u53c2\u8003 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 \u4e2d\u4ecb\u7ecd\u7684\u65b9\u6cd5\u3002","title":"\u6574\u578b\u9664\u6cd5"},{"location":"4-5-Unsigned-integers-and-why-to-avoid-them/","tags":["data type","integer","overflow"],"text":"Key Takeaway \u65e0\u7b26\u53f7\u6574\u578b\u8868\u793a\u8d85\u8fc7\u8303\u56f4\u7684\u6570\u65f6\uff0c\u5b9e\u9645\u5b58\u50a8\u7684\u503c\u4e3a\u539f\u503c\u9664\u4ee5\u8303\u56f4\u6700\u5927\u503c\u52a0 1 \u540e\u7684\u4f59\u6570\u3002\u4f8b\u5982\uff0c value mod 2^32 \u3002\u79f0\u4e3a\u65e0\u7b26\u53f7\u7684\u53cd\u8f6c\u3002 \u65e0\u7b26\u53f7\u4f1a\u7ffb\u8f6c\uff0c\u6709\u7b26\u53f7\u4f1a\u6ea2\u51fa \u65e0\u7b26\u53f7\u53cd\u8f6c\u53ef\u4ee5\u54110\u53cd\u8f6c\u4e5f\u53ef\u4ee5\u5411\u6700\u5927\u503c\u53cd\u8f6c \u65e0\u7b26\u53f7\u6574\u578b\u8868\u793a\u8d85\u8fc7\u8303\u56f4\u7684\u6570\u65f6\u4e0d\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \uff0c\u5b83\u7684\u884c\u4e3a\u662f\u786e\u5b9a\u7684\uff0c\u56e0\u6b64 C++ \u6807\u51c6\u660e\u786e\u8bf4\u660e\u65e0\u7b26\u53f7\u6574\u578b\u4e0d\u4f1a\"\u6ea2\u51fa\" \u6df7\u7528\u65e0\u7b26\u53f7\u548c\u6709\u7b26\u53f7\u7c7b\u578b\u65f6\u53ef\u80fd\u4ea7\u751f\u95ee\u9898\uff0c\u56e0\u4e3a\u6709\u7b26\u53f7\u6570\u4f1a\u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a\u65e0\u7b26\u53f7\u6570\uff0c\u6b64\u65f6\u53ef\u80fd\u53d1\u751f\u53cd\u8f6c\u3002 \u5c3d\u91cf\u4f7f\u7528\u6709\u7b26\u53f7\u6570\u4fdd\u5b58\u6570\u91cf\u503c\u548c\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\uff0c\u65e0\u7b26\u53f7+\u6709\u7b26\u53f7\u4f1a\u53d1\u751f\u8f6c\u6362\u5bfc\u81f4\u6709\u8d1f\u7684\u7b26\u53f7\u6570\u6ea2\u51fa\uff08 8.4 - \u7b97\u672f\u8f6c\u6362 \uff09 \u8fdb\u884c\u4f4d\u8fd0\u7b97\u7684\u65f6\u5019\u53ef\u4ee5\u4f7f\u7528\u65e0\u7b26\u53f7\u6574\u578b \u65e0\u7b26\u53f7\u6574\u578b \u5728\u4e0a\u8282\u8bfe ( 4.4 - \u6709\u7b26\u53f7\u6574\u578b ) \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u6709\u7b26\u53f7\u6574\u578b\uff0c\u8fd9\u4e9b\u5f81\u4fe1\u53ef\u4ee5\u4fdd\u5b58\u6b63\u8d1f\u6574\u6570\u548c 0\u3002 C++ \u8fd8\u652f\u6301\u65e0\u7b26\u53f7\u6574\u578b\u3002\u65e0\u7b26\u53f7\u6574\u578b\u53ea\u80fd\u8868\u793a\u975e\u7b26\u6574\u6570\u3002 \u5b9a\u4e49\u65e0\u7b26\u53f7\u6574\u578b \u5b9a\u4e49\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u9700\u8981\u4f7f\u7528 unsigned \u5173\u952e\u5b57\u3002\u901a\u5e38\u6765\u8bb2\uff0c\u8be5\u5173\u952e\u5b57\u5e94\u8be5\u5199\u5728\u7c7b\u578b\u524d\uff1a 1 2 3 4 unsigned short us ; unsigned int ui ; unsigned long ul ; unsigned long long ull ; \u65e0\u7b26\u53f7\u6574\u578b\u7684\u8303\u56f4 1 \u5b57\u8282(8 \u4f4d)\u65e0\u7b26\u53f7\u6574\u578b\u7684\u8303\u56f4\u4e3a 0 \u548c 255\uff0c\u800c 1 \u5b57\u8282\u6709\u7b26\u53f7\u6574\u578b\u7684\u8303\u56f4\u662f -128 \u5230 127\u3002\u5b83\u4eec\u90fd\u80fd\u591f\u5b58\u653e 256 \u4e2a\u4e0d\u540c\u7684\u503c\uff0c\u4f46\u662f\u6709\u7b26\u53f7\u6574\u578b\u7684\u4e00\u534a\u8303\u56f4\u88ab\u7528\u6765\u8868\u793a\u8d1f\u6570\uff0c\u800c\u65e0\u7b26\u53f7\u6574\u578b\u5219\u5168\u90e8\u7528\u6765\u5b58\u653e\u6574\u6570\uff0c\u56e0\u6b64\u80fd\u591f\u8868\u793a\u7684\u6b63\u6570\u8303\u56f4\u662f\u6709\u7b26\u53f7\u6574\u578b\u7684\u4e24\u500d\u3002 \u4e0b\u8868\u5c55\u793a\u4e86\u4e0d\u540c\u6709\u7b26\u53f7\u6574\u578b\u53ef\u4ee5\u8868\u793a\u7684\u8303\u56f4\uff1a \u5927\u5c0f/\u7c7b\u578b \u8303\u56f4 1 byte unsigned 0 \u5230 255 2 byte unsigned 0 \u5230 65,535 4 byte unsigned 0 \u5230 4,294,967,295 8 byte unsigned 0 \u5230 18,446,744,073,709,551,615 \u4e00\u4e2a n \u4e3a\u7684\u65e0\u7b26\u53f7\u6574\u578b\u53ef\u8868\u793a\u7684\u8303\u56f4\u4e3a 0 \u5230 \\((2^n)-1\\) \u3002 \u5982\u679c\u4e0d\u9700\u8981\u8868\u793a\u8d1f\u6570\uff0c\u90a3\u4e48\u65e0\u7b26\u53f7\u6574\u578b\u66f4\u9002\u5408\u5728\u5185\u5b58\u6bd4\u8f83\u5c0f\u7684\u7f51\u7edc\u548c\u7cfb\u7edf\u4e0a\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5728\u4e0d\u5360\u7528\u66f4\u591a\u5185\u5b58\u7684\u524d\u63d0\u4e0b\u4fdd\u5b58\u66f4\u591a\u7684\u6b63\u6574\u6570\u3002 \u5206\u6e05\u6709\u7b26\u53f7(signed) \u548c\u65e0\u7b26\u53f7(unsigned) \u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u7ecf\u5e38\u628a\u6709\u7b26\u53f7(signed) \u548c\u65e0\u7b26\u53f7(unsigned)\u8fd9\u4e24\u4e2a\u672f\u8bed\u641e\u6df7\u3002\u4ee5\u4e0b\u4ecb\u7ecd\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u8bb0\u5fc6\u5b83\u4eec\u7684\u4e0d\u540c\uff1a\u4e3a\u4e86\u533a\u5206\u8d1f\u6570\u548c\u6b63\u6570\uff0c\u6211\u4eec\u9700\u8981\u8d1f\u53f7\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u7b26\u53f7\uff0c\u6211\u4eec\u5047\u5b9a\u8be5\u6570\u4e3a\u6b63\u6570\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u5177\u6709\u7b26\u53f7\u7684\u6574\u578b\uff08\u6709\u7b26\u53f7\u6574\u578b\uff09\u53ef\u4ee5\u533a\u5206\u6b63\u8d1f\u3002\u800c\u4e0d\u5177\u6709\u7b26\u53f7\u7684\u6574\u578b\uff08\u6709\u7b26\u53f7\u6574\u578b\uff09\u5219\u5168\u90e8\u4e3a\u6b63\u6570\u3002 \u65e0\u7b26\u53f7\u6574\u578b\u6ea2\u51fa What happens if we try to store the number 280 (which requires 9 bits to represent) in a 1-byte (8-bit) unsigned integer? The answer is overflow. \u5982\u679c\u6211\u4eec\u5c1d\u8bd5\u628a 280 \uff08\u9700\u8981 9 \u4e2a\u4f4d\u6765\u8868\u793a\uff09\u8d4b\u503c\u7ed9\u4e00\u4e2a 8 \u4f4d\u65e0\u7b26\u53f7\u6574\u578b\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f\u7b54\u6848\u662f\u2014\u2014\u6ea2\u51fa\u3002 \u4f5c\u8005\u6ce8 \u5947\u602a\u7684\u662f\uff0cC++\u6807\u51c6\u4e2d\u660e\u786e\u5199\u9053\uff0c\u65e0\u7b26\u53f7\u64cd\u4f5c\u6570\u53c2\u4e0e\u7684\u8fd0\u7b97\u6c38\u8fdc\u4e0d\u4f1a\u6ea2\u51fa\u3002\u8fd9\u548c\u6211\u4eec\u7684\u7f16\u7a0b\u5e38\u8bc6\uff08\u65e0\u7b26\u53f7\u548c\u6709\u7b26\u53f7\u6574\u578b\u90fd\u4f1a\u6ea2\u51fa\uff09\u662f\u77db\u76fe\u7684\u3002( \u53c2\u8003\u6587\u732e )\u3002\u7531\u4e8e\u5927\u591a\u6570\u7684\u7a0b\u5e8f\u5458\u90fd\u4f1a\u8003\u8651\u8be5\u6ea2\u51fa\uff0c\u6211\u4eec\u8fd9\u91cc\u4e5f\u6cbf\u7528\u4e86\u8be5\u8bf4\u6cd5\uff0c\u5c3d\u7ba1\u548cC++\u6807\u51c6\u6709\u6240\u77db\u76fe\u3002 \u5f53\u65e0\u7b26\u53f7\u503c\u8d85\u8fc7\u8303\u56f4\u65f6\uff0c\u5b83\u4f1a\u88ab\u9664\u4ee5\u8be5\u7c7b\u578b\u7684\u6700\u5927\u503c\u52a0 1\uff0c\u4e14\u53ea\u4fdd\u7559\u4f59\u6570\u3002 280 \u4e0d\u5728 0 \u5230 255 \u7684\u8303\u56f4\u5185\u3002\u8be5\u8303\u56f4\u7684\u6700\u5927\u503c+1 \u662f 256\uff0c\u6240\u4ee5 280 \u4f1a\u9664\u4ee5 256\uff0c\u4f59\u6570\u4e3a 24\u3002\u6700\u7ec8 24 \u4f1a\u88ab\u5b58\u653e\u5230\u5185\u5b58\u4e2d\u3002 \u8fd9\u4e2a\u95ee\u9898\u8fd8\u53ef\u4ee5\u8fd9\u6837\u7406\u89e3\u3002\u6bd4\u6700\u5927\u503c\u8fd8\u5927\u7684\u6570\u5728\u5b58\u653e\u65f6\u4f1a\u88ab\u201cwraps around\u201d (\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a\u201cmodulo wrapping\u201d)\uff0c\u5373\u4ece 0 \u5f00\u59cb\u91cd\u65b0\u8ba1\u7b97\u3002255 \u5728 1 \u5b57\u8282\u6574\u578b\u7684\u8868\u793a\u8303\u56f4\u5185\uff0c\u6240\u4ee5 255 \u6ca1\u95ee\u9898\u3002\u800c 256 \u5219\u8d85\u8fc7\u4e86\u8303\u56f4\uff0c\u56e0\u6b64\u5b83\u4f1a\u88ab\u5f52\u4e3a 0\u3002257 \u5219\u4f1a\u5f52\u4e3a 1\u3002280 \u5219\u5f52\u4e3a 24. \u8fd9\u91cc\u662f\u4e00\u4e2a 2 \u5b57\u8282\u65e0\u7b26\u53f7\u77ed\u6574\u578b\uff08 unsigned short \uff09 \u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { unsigned short x { 65535 }; // largest 16-bit unsigned value possible std :: cout << \"x was: \" << x << '\\n' ; x = 65536 ; // 65536 is out of our range, so we get wrap-around std :: cout << \"x is now: \" << x << '\\n' ; x = 65537 ; // 65537 is out of our range, so we get wrap-around std :: cout << \"x is now: \" << x << '\\n' ; return 0 ; } \u8bd1\u8005\u6ce8 \u65e0\u7b26\u53f7\u4e0d\u4f1a\u6ea2\u51fa\uff0c\u7684\u786e\u662f C++\u6807\u51c6\uff0c\u4e0d\u6ea2\u51fa\u8fd9\u4e2a\u8bf4\u6cd5\u4e5f\u662f\u5408\u7406\u7684\uff0c\u56e0\u4e3a\u65e0\u7b26\u53f7\u5728\u9047\u5230\u8d85\u8fc7\u8868\u793a\u8303\u56f4\u7684\u60c5\u51b5\u65f6\u5e76\u4e0d\u4f1a\u8868\u73b0\u51fa \u672a\u5b9a\u4e49\u884c\u4e3a \uff0c\u7ed3\u679c\u4e5f\u4e0d\u662f\u7b80\u5355\u7684\u4e22\u5f03\uff0c\u800c\u662f\u5bf9\u539f\u503c\u8fdb\u884c\u6c42\u6a21\u8fd0\u7b97\u3002\u82f1\u6587\u53eb warp around\uff0c\u5373\u4ece 0 \u5f00\u59cb\u91cd\u65b0\u8ba1\u7b97\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e 32 \u4f4d\u65e0\u7b26\u53f7\uff0c\u8d85\u51fa\u8303\u56f4\u65f6\u7ed3\u679c\u7b49\u4e8e value mod 2^32 \u4f60\u89c9\u5f97\u7a0b\u5e8f\u7684\u7ed3\u679c\u4f1a\u662f\u4ec0\u4e48\u5462\uff1f (\u6ce8\u610f\uff0c\u5982\u679c\u4f60\u7f16\u8bd1\u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8bdd\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u4ea7\u751f\u5173\u4e8e\u6ea2\u51fa\u6216\u622a\u65ad\u7684\u544a\u8b66\u2014\u2014\u4f60\u9700\u8981\u5173\u95ed\u201d\u5c06\u544a\u8b66\u770b\u505a\u9519\u8bef\u201c\u529f\u80fd\uff0c\u624d\u80fd\u8fd0\u884c\u8be5\u7a0b\u5e8f\uff09 1 2 3 x was: 65535 x is now: 0 x is now: 1 \u5411\u53e6\u4e00\u4e2a\u65b9\u5411 wrap \u4e5f\u662f\u53ef\u4ee5\u7684\uff0c0 \u53ef\u4ee5\u88ab 2 \u5b57\u8282\u5927\u5c0f\u7684\u65e0\u7b26\u53f7\u6574\u578b\u8868\u793a\uff0c\u4f46\u662f-1 \u4e0d\u53ef\u4ee5\uff0c\u56e0\u6b64\u5b83\u4f1a\u88ab wrap \u5230\u6700\u5927\u503c\uff0c\u7ed3\u679c\u4e3a 65535\u3002-2 \u7684\u7ed3\u679c\u662f 65534\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { unsigned short x { 0 }; // smallest 2-byte unsigned value possible std :: cout << \"x was: \" << x << '\\n' ; x = -1 ; // -1 is out of our range, so we get wrap-around std :: cout << \"x is now: \" << x << '\\n' ; x = -2 ; // -2 is out of our range, so we get wrap-around std :: cout << \"x is now: \" << x << '\\n' ; return 0 ; } 1 2 3 x was: 0 x is now: 65535 x is now: 65534 \u4e0a\u8ff0\u4ee3\u7801\u4f1a\u5f15\u8d77\u67d0\u4e9b\u7f16\u8bd1\u5668\u4ea7\u751f\u544a\u8b66\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u68c0\u6d4b\u5230\u6574\u578b\u5b57\u9762\u91cf\u8d85\u8fc7\u4e86\u8868\u793a\u5b83\u7684\u7c7b\u578b\u7684\u8303\u56f4\u3002\u5982\u679c\u4f60\u5e0c\u671b\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\uff0c\u8bf7\u6682\u65f6\u5173\u95ed\u201d\u5c06\u544a\u8b66\u770b\u505a\u9519\u8bef\u201c\u529f\u80fd\u3002 \u9898\u5916\u8bdd \u5f88\u591a\u7535\u5b50\u6e38\u620f\u53f2\u4e0a\u7684\u8457\u540d\u7684 bug \u90fd\u548c\u65e0\u7b26\u53f7\u6570\u53cd\u8f6c\u6709\u5173\u3002\u8857\u673a\u6e38\u620f\u300a\u5927\u91d1\u521a\u300b\u4e2d\uff0c\u4f60\u4e0d\u53ef\u80fd\u901a\u8fc722\u5173\uff0c\u56e0\u4e3a\u6ea2\u51fabug\u5bfc\u81f4\u7528\u6237\u5728\u8fd9\u4e00\u5173\u4e2d\u4e0d\u53ef\u80fd\u6709\u8db3\u591f\u7684\u65f6\u95f4\u3002 \u7535\u8111\u6e38\u620f\u300a\u6587\u660e\u300b\u4e2d\uff0c\u7518\u5730\u901a\u5e38\u662f\u7b2c\u4e00\u4e2a\u4f7f\u7528\u6838\u6b66\u5668\u7684\uff0c\u8fd9\u548c\u4ed6\u672c\u5e94\u8be5\u6709\u7684\u4fdd\u5b88\u7684\u5c5e\u6027\u975e\u5e38\u77db\u76fe\u3002\u73a9\u5bb6\u8ba4\u4e3a\u8fd9\u662f\u7531\u4e8e\u7518\u5730\u7684\u653b\u51fb\u6027\u88ab\u521d\u59cb\u5316\u62101\uff0c\u4f46\u662f\u5982\u679c\u4ed6\u8d70\u6c11\u4e3b\u8def\u7ebf\u7684\u8bdd\uff0c\u5176\u653b\u51fb\u6027\u4f1a-2\u3002\u8fdb\u800c\u5bfc\u81f4\u4e86\u6570\u503c\u88ab\u53cd\u8f6c\u6210\u4e86255\uff0c\u4f7f\u5176\u53d8\u5f97\u975e\u5e38\u597d\u6597\u3002\u4e0d\u8fc7\uff0c\u6700\u8fd1\u5e2d\u5fb7\u6885\u5c14\u5bf9\u6b64\u8fdb\u884c\u4e86\u6f84\u6e05\uff0c\u4ed6\u8bf4\u8fd9\u5e76\u4e0d\u662f\u5f15\u8d77\u4e0a\u8ff0\u95ee\u9898\u7684\u539f\u56e0\u3002 \u6709\u5173\u65e0\u7b26\u53f7\u6574\u578b\u7684\u4e89\u8bba \u5f88\u591a\u7a0b\u5e8f\u5458 (\u4ee5\u53ca\u4e00\u4e9b\u5927\u5382\uff0c\u4f8b\u5982\u8c37\u6b4c\uff09\u8ba4\u4e3a\u7a0b\u5e8f\u4e5f\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u65e0\u7b26\u53f7\u6574\u578b\u3002 \u8fd9\u4e3b\u8981\u662f\u56e0\u4e3a\u65e0\u7b26\u53f7\u6574\u578b\u6709\u4e24\u79cd\u884c\u4e3a\u53ef\u80fd\u4f1a\u5bfc\u81f4\u95ee\u9898\u3002 \u9996\u5148\uff0c\u5c06\u4e24\u4e2a\u65e0\u7b26\u53f7\u6574\u578b\u76f8\u51cf\uff0c\u4f8b\u59823-5\u7b49\u4e8e-2\uff0c\u4f46\u662f-2\u4e0d\u80fd\u88ab\u65e0\u7b26\u53f7\u6574\u578b\u8868\u793a\u3002 1 2 3 4 5 6 7 8 9 10 #include int main () { unsigned int x { 3 }; unsigned int y { 5 }; std :: cout << x - y << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\uff0c\u5c3d\u7ba1\u4e0a\u9762\u7684\u4ee3\u7801\u770b\u4e0a\u53bb\u4eba\u755c\u65e0\u5bb3\uff0c\u5b9e\u9645\u4e0a\u7ed3\u679c\u5374\u4ee4\u4eba\u5403\u60ca\uff1a 1 4294967294 \u8fd9\u662f\u56e0\u4e3a -2 \u88ab\u53cd\u8f6c\u5230\u4e86\u4e00\u4e2a\u63a5\u8fd1 4\u81ea\u5df1\u6574\u578b\u6700\u5927\u503c\u7684\u6570\u3002\u53e6\u5916\u4e00\u4e2a\u53ef\u80fd\u4ea7\u751f\u53cd\u8f6c\u7684\u60c5\u51b5\u662f\uff0c\u5bf9\u65e0\u7b26\u53f7\u6574\u578b\u4f7f\u7528\u9012\u51cf\u8fd0\u7b97\u7b26( -- )\u3002\u90a3\u4e48\u5f53\u53d8\u91cf\u5faa\u73af\u9012\u51cf\u65f6\uff0c\u6700\u7ec8\u53ef\u80fd\u4f1a\u53d1\u751f\u4e8e\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\u540c\u6837\u7684\u95ee\u9898\u3002 . \u6b64\u5916\uff0c\u5f53\u5c06\u6709\u7b26\u53f7\u6574\u578b\u548c\u65e0\u7b26\u53f7\u6574\u578b\u6df7\u7528\u65f6\uff0c\u4e5f\u53ef\u80fd\u4ea7\u751f\u610f\u5916\u3002\u5728C++\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u65f6\uff08\u4f8b\u5982\u7b97\u6570\u6216\u6bd4\u8f83\uff09\uff0c\u5982\u679c\u4e24\u4e2a\u64cd\u4f5c\u6570\u5206\u522b\u4e3a\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u90a3\u4e48\u6709\u7b26\u53f7\u7684\u4e00\u65b9\u4f1a\u88ab\u8f6c\u6362\u4e3a\u65e0\u7b26\u53f7\u3002\u7136\u540e\uff0c\u56e0\u4e3a\u65e0\u7b26\u53f7\u6574\u578b\u4e0d\u53ef\u4ee5\u4fdd\u5b58\u8d1f\u6570\uff0c\u8fd9\u5c31\u6709\u53ef\u80fd\u9020\u6210\u6570\u636e\u4e22\u5931\u7684\u60c5\u51b5\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { signed int s { -1 }; unsigned int u { 1 }; if ( s < u ) // -1 \u88ab\u9690\u5f0f\u8f6c\u6362\u6210\u4e86 4294967295, \u800c 4294967295 < 1 \u662f\u9519\u8bef\u7684 std :: cout << \"-1 is less than 1 \\n \" ; else std :: cout << \"1 is less than -1 \\n \" ; // \u8fd9\u53e5\u8bed\u53e5\u4f1a\u88ab\u6267\u884c return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u7f16\u8bd1\uff0c\u770b\u4e0a\u53bb\u4e5f\u7b26\u5408\u903b\u8f91\u3002\u4f46\u662f\u5b83\u6253\u5370\u7684\u7ed3\u679c\u5374\u662f\u9519\u8bef\u7684\u3002\u5c3d\u7ba1\u7f16\u8bd1\u5668\u5728\u68c0\u67e5\u5230\u6709\u7b26\u53f7/\u65e0\u7b26\u53f7\u4e0d\u5339\u914d\u65f6\uff0c\u4f1a\u53d1\u51fa\u4e00\u4e2a\u8b66\u544a\u3002\u4f46\u662f\u7531\u4e8e\u8fd8\u6709\u5f88\u591a\u5176\u4ed6\u60c5\u51b5\u4e0b\u7f16\u8bd1\u5668\u4e5f\u4f1a\u62a5\u544a\u5b8c\u5168\u4e00\u6837\u7684\u8b66\u544a\uff08\u4f8b\u5982\u4e24\u4e2a\u6570\u90fd\u662f\u6b63\u6570\u65f6\uff09\uff0c\u8fd9\u4f1a\u8ba9\u6211\u4eec\u5f88\u96be\u5224\u65ad\u771f\u6b63\u7684\u95ee\u9898\u5728\u54ea\u91cc\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 4.10 - if \u8bed\u53e5 \u4e2d\u4ecb\u7ecd if \u8bed\u53e5\u3002 \u6b64\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u53ef\u80fd\u6709\u95ee\u9898\u7684\u60c5\u51b5\uff0c\u662f\u6839\u672c\u65e0\u6cd5\u88ab\u68c0\u67e5\u5230\u7684\u3002\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 void doSomething ( unsigned int x ) { // Run some code x times std :: cout << \"x is \" << x << '\\n' ; } int main () { doSomething ( -1 ); return 0 ; } doSomething() \u51fd\u6570\u7684\u4f5c\u8005\u671f\u671b\u7528\u6237\u53ea\u4f20\u9012\u6b63\u6570\u7ed9\u8be5\u51fd\u6570\u3002\u4f46\u662f\u8c03\u7528\u8005\u72af\u4e86\u4e00\u4e2a\u9519\u8bef\uff0c\u5b83\u4f20\u4e86-1\u3002\u8fd9\u662f\u4e2a\u5f88\u663e\u7136\u7684\u9519\u8bef\uff0c\u4f46\u662f\u4eba\u4eec\u4ecd\u7136\u96be\u514d\u4f1a\u72af\u3002\u90a3\u4e48\u6b64\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f \u5b9e\u53c2 -1 \u88ab\u9690\u5f0f\u8f6c\u6362\u6210\u4e86\u4e00\u4e2a\u65e0\u7b26\u53f7 \u5f62\u53c2 \u3002-1 \u4e0d\u5728\u65e0\u7b26\u53f7\u6574\u578b\u7684\u8868\u793a\u8303\u56f4\uff0c\u56e0\u6b64\u5fc5\u7136\u88ab\u53cd\u8f6c\u6210\u4e00\u4e2a\u975e\u5e38\u5927\u7684\u6570(\u53ef\u80fd\u662f 4294967295)\u3002\u7136\u540e\u4f60\u7684\u7a0b\u5e8f\u5c31\u4f1a\u51fa\u73b0\u95ee\u9898\u3002 \u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u5e76\u6ca1\u6709\u4e00\u79cd\u65b9\u6cd5\u80fd\u591f\u907f\u514d\u4e0a\u8ff0\u95ee\u9898\u7684\u53d1\u751f\u3002C++ \u53ef\u4ee5\u81ea\u7531\u5730\u8f6c\u6362\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u6570\uff0c\u4f46\u662f\u5b83\u5e76\u4e0d\u4f1a\u5728\u8f6c\u6362\u65f6\u8fdb\u884c\u4efb\u4f55\u7684\u8303\u56f4\u68c0\u67e5\u4ee5\u786e\u4fdd\u4e0d\u53d1\u751f\u6ea2\u51fa\u3002 \u4e0a\u8ff0\u8fd9\u4e9b\u95ee\u9898\u90fd\u662f\u7ecf\u5e38\u4f1a\u9047\u5230\u7684\uff0c\u5b83\u4eec\u4ea7\u751f\u610f\u6599\u4e4b\u5916\u7684\u884c\u4e3a\uff0c\u5e76\u4e14\u96be\u4ee5\u88ab\u53d1\u73b0\u3002\u5373\u4f7f\u4f7f\u7528\u4e00\u4e9b\u88ab\u4e13\u7528\u4e8e\u67e5\u627e\u6b64\u7c7b\u95ee\u9898\u7684\u5de5\u5177\u6709\u5f88\u96be\u53d1\u73b0\u3002 \u56e0\u6b64\uff0c\u4e00\u4e9b\u6709\u4e89\u8bae\u7684\u6700\u4f73\u5b9e\u8df5\u8ba4\u4e3a\uff0c\u6211\u4eec\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u65e0\u7b26\u53f7\u7c7b\u578b\uff0c\u9664\u975e\u786e\u5b9e\u6709\u7279\u6b8a\u7684\u9700\u6c42\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c3d\u91cf\u4f7f\u7528\u6709\u7b26\u53f7\u6570\u6765\u5b58\u653e\u6570\u91cf\u503c\uff08\u5c3d\u7ba1\u6570\u91cf\u53ef\u80fd\u90fd\u662f\u975e\u8d1f\u7684\uff09\u548c\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u3002\u907f\u514d\u6df7\u7528\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u7c7b\u578b\u3002 \u76f8\u5173\u5185\u5bb9 \u652f\u6301\u4e0a\u8ff0\u5efa\u8bae\u7684\u4e00\u4e9b\u6750\u6599(\u540c\u65f6\u4e5f\u9a73\u65a5\u4e86\u4e00\u4e9b\u5e38\u89c1\u7684\u53cd\u5bf9\u610f\u89c1\uff09\uff1a Interactive C++ panel (see 12:12-13:08,42:40-45:26, and 1:02:50-1:03:15) Subscripts and sizes should be signed Unsigned integers from the libtorrent blog \u90a3\u4e48\u4ec0\u4e48\u65f6\u5019\u624d\u4f7f\u7528\u65e0\u7b26\u53f7\u6570\u5462\uff1f \u8fd8\u662f\u6709\u4e00\u4e9b\u573a\u5408\u662f\u9700\u8981\u6216\u8005\u53ef\u4ee5\u4f7f\u7528\u65e0\u7b26\u53f7\u6570\u7684\u3002 \u9996\u5148\uff0c\u65e0\u7b26\u53f7\u6570\u5728\u8fdb\u884c\u4f4d\u8fd0\u7b97\u65f6\u662f\u63a8\u8350\u4f7f\u7528\u7684\uff08\u5728\u7ae0\u8282O\u4e2d\u4ecb\u7ecd\uff09\u3002\u5728\u4e00\u4e9b\u786e\u5b9e\u9700\u8981\u53cd\u8f6c\u884c\u4e3a\u7684\u573a\u5408\uff0c\u65e0\u7b26\u53f7\u6570\u4e5f\u662f\u6709\u7528\u7684\uff08\u4f8b\u5982\u4e00\u4e9b\u52a0\u5bc6\u7b97\u6cd5\u548c\u968f\u673a\u6570\u751f\u6210\uff09\u3002 \u5176\u6b21\uff0c\u5728\u6709\u4e9b\u65f6\u5019\u65e0\u7b26\u53f7\u6570\u662f\u65e0\u6cd5\u907f\u514d\u7684\uff08\u5f88\u591a\u4e0e\u6570\u7ec4\u8fdb\u884c\u7d22\u5f15\u6709\u5173\uff09\u3002\u6211\u4eec\u4f1a\u5728\u6709\u5173\u6570\u7ec4\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002\u5728\u8fd9\u4e9b\u60c5\u51b5\u4e0b\uff0c\u65e0\u7b26\u53f7\u6570\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u6570\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast \u4e2d\u4ecb\u7ecd\u5982\u4f55\u5c06\u65e0\u7b26\u53f7\u6570\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u6570\u3002 \u540c\u65f6\u8bf7\u6ce8\u610f\uff0c\u5982\u679c\u4f60\u5728\u5f00\u53d1\u5d4c\u5165\u5f0f\u7cfb\u7edf\uff08\u4f8b\u5982 Arduino\uff09\u6216\u8005\u5176\u4ed6\u5904\u7406\u5668/\u5185\u5b58\u6bd4\u8f83\u5403\u7d27\u7684\u73af\u5883\uff0c\u51fa\u4e8e\u6027\u80fd\u7684\u539f\u56e0\u800c\u4f7f\u7528\u65e0\u7b26\u53f7\u6570\u662f\u975e\u5e38\u5e38\u89c1\u7684\uff0c\u540c\u65f6\u4e5f\u662f\u53ef\u4ee5\u88ab\u63a5\u53d7\u7684\uff08\u5728\u6709\u4e9b\u573a\u666f\u4e0b\u4e5f\u662f\u65e0\u6cd5\u907f\u514d\u7684\uff09\u3002","title":"4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83"},{"location":"4-5-Unsigned-integers-and-why-to-avoid-them/#_1","text":"\u5728\u4e0a\u8282\u8bfe ( 4.4 - \u6709\u7b26\u53f7\u6574\u578b ) \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u6709\u7b26\u53f7\u6574\u578b\uff0c\u8fd9\u4e9b\u5f81\u4fe1\u53ef\u4ee5\u4fdd\u5b58\u6b63\u8d1f\u6574\u6570\u548c 0\u3002 C++ \u8fd8\u652f\u6301\u65e0\u7b26\u53f7\u6574\u578b\u3002\u65e0\u7b26\u53f7\u6574\u578b\u53ea\u80fd\u8868\u793a\u975e\u7b26\u6574\u6570\u3002","title":"\u65e0\u7b26\u53f7\u6574\u578b"},{"location":"4-5-Unsigned-integers-and-why-to-avoid-them/#_2","text":"\u5b9a\u4e49\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u9700\u8981\u4f7f\u7528 unsigned \u5173\u952e\u5b57\u3002\u901a\u5e38\u6765\u8bb2\uff0c\u8be5\u5173\u952e\u5b57\u5e94\u8be5\u5199\u5728\u7c7b\u578b\u524d\uff1a 1 2 3 4 unsigned short us ; unsigned int ui ; unsigned long ul ; unsigned long long ull ;","title":"\u5b9a\u4e49\u65e0\u7b26\u53f7\u6574\u578b"},{"location":"4-5-Unsigned-integers-and-why-to-avoid-them/#_3","text":"1 \u5b57\u8282(8 \u4f4d)\u65e0\u7b26\u53f7\u6574\u578b\u7684\u8303\u56f4\u4e3a 0 \u548c 255\uff0c\u800c 1 \u5b57\u8282\u6709\u7b26\u53f7\u6574\u578b\u7684\u8303\u56f4\u662f -128 \u5230 127\u3002\u5b83\u4eec\u90fd\u80fd\u591f\u5b58\u653e 256 \u4e2a\u4e0d\u540c\u7684\u503c\uff0c\u4f46\u662f\u6709\u7b26\u53f7\u6574\u578b\u7684\u4e00\u534a\u8303\u56f4\u88ab\u7528\u6765\u8868\u793a\u8d1f\u6570\uff0c\u800c\u65e0\u7b26\u53f7\u6574\u578b\u5219\u5168\u90e8\u7528\u6765\u5b58\u653e\u6574\u6570\uff0c\u56e0\u6b64\u80fd\u591f\u8868\u793a\u7684\u6b63\u6570\u8303\u56f4\u662f\u6709\u7b26\u53f7\u6574\u578b\u7684\u4e24\u500d\u3002 \u4e0b\u8868\u5c55\u793a\u4e86\u4e0d\u540c\u6709\u7b26\u53f7\u6574\u578b\u53ef\u4ee5\u8868\u793a\u7684\u8303\u56f4\uff1a \u5927\u5c0f/\u7c7b\u578b \u8303\u56f4 1 byte unsigned 0 \u5230 255 2 byte unsigned 0 \u5230 65,535 4 byte unsigned 0 \u5230 4,294,967,295 8 byte unsigned 0 \u5230 18,446,744,073,709,551,615 \u4e00\u4e2a n \u4e3a\u7684\u65e0\u7b26\u53f7\u6574\u578b\u53ef\u8868\u793a\u7684\u8303\u56f4\u4e3a 0 \u5230 \\((2^n)-1\\) \u3002 \u5982\u679c\u4e0d\u9700\u8981\u8868\u793a\u8d1f\u6570\uff0c\u90a3\u4e48\u65e0\u7b26\u53f7\u6574\u578b\u66f4\u9002\u5408\u5728\u5185\u5b58\u6bd4\u8f83\u5c0f\u7684\u7f51\u7edc\u548c\u7cfb\u7edf\u4e0a\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5728\u4e0d\u5360\u7528\u66f4\u591a\u5185\u5b58\u7684\u524d\u63d0\u4e0b\u4fdd\u5b58\u66f4\u591a\u7684\u6b63\u6574\u6570\u3002","title":"\u65e0\u7b26\u53f7\u6574\u578b\u7684\u8303\u56f4"},{"location":"4-5-Unsigned-integers-and-why-to-avoid-them/#signed-unsigned","text":"\u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u7ecf\u5e38\u628a\u6709\u7b26\u53f7(signed) \u548c\u65e0\u7b26\u53f7(unsigned)\u8fd9\u4e24\u4e2a\u672f\u8bed\u641e\u6df7\u3002\u4ee5\u4e0b\u4ecb\u7ecd\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u8bb0\u5fc6\u5b83\u4eec\u7684\u4e0d\u540c\uff1a\u4e3a\u4e86\u533a\u5206\u8d1f\u6570\u548c\u6b63\u6570\uff0c\u6211\u4eec\u9700\u8981\u8d1f\u53f7\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u7b26\u53f7\uff0c\u6211\u4eec\u5047\u5b9a\u8be5\u6570\u4e3a\u6b63\u6570\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u5177\u6709\u7b26\u53f7\u7684\u6574\u578b\uff08\u6709\u7b26\u53f7\u6574\u578b\uff09\u53ef\u4ee5\u533a\u5206\u6b63\u8d1f\u3002\u800c\u4e0d\u5177\u6709\u7b26\u53f7\u7684\u6574\u578b\uff08\u6709\u7b26\u53f7\u6574\u578b\uff09\u5219\u5168\u90e8\u4e3a\u6b63\u6570\u3002","title":"\u5206\u6e05\u6709\u7b26\u53f7(signed) \u548c\u65e0\u7b26\u53f7(unsigned)"},{"location":"4-5-Unsigned-integers-and-why-to-avoid-them/#_4","text":"What happens if we try to store the number 280 (which requires 9 bits to represent) in a 1-byte (8-bit) unsigned integer? The answer is overflow. \u5982\u679c\u6211\u4eec\u5c1d\u8bd5\u628a 280 \uff08\u9700\u8981 9 \u4e2a\u4f4d\u6765\u8868\u793a\uff09\u8d4b\u503c\u7ed9\u4e00\u4e2a 8 \u4f4d\u65e0\u7b26\u53f7\u6574\u578b\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f\u7b54\u6848\u662f\u2014\u2014\u6ea2\u51fa\u3002 \u4f5c\u8005\u6ce8 \u5947\u602a\u7684\u662f\uff0cC++\u6807\u51c6\u4e2d\u660e\u786e\u5199\u9053\uff0c\u65e0\u7b26\u53f7\u64cd\u4f5c\u6570\u53c2\u4e0e\u7684\u8fd0\u7b97\u6c38\u8fdc\u4e0d\u4f1a\u6ea2\u51fa\u3002\u8fd9\u548c\u6211\u4eec\u7684\u7f16\u7a0b\u5e38\u8bc6\uff08\u65e0\u7b26\u53f7\u548c\u6709\u7b26\u53f7\u6574\u578b\u90fd\u4f1a\u6ea2\u51fa\uff09\u662f\u77db\u76fe\u7684\u3002( \u53c2\u8003\u6587\u732e )\u3002\u7531\u4e8e\u5927\u591a\u6570\u7684\u7a0b\u5e8f\u5458\u90fd\u4f1a\u8003\u8651\u8be5\u6ea2\u51fa\uff0c\u6211\u4eec\u8fd9\u91cc\u4e5f\u6cbf\u7528\u4e86\u8be5\u8bf4\u6cd5\uff0c\u5c3d\u7ba1\u548cC++\u6807\u51c6\u6709\u6240\u77db\u76fe\u3002 \u5f53\u65e0\u7b26\u53f7\u503c\u8d85\u8fc7\u8303\u56f4\u65f6\uff0c\u5b83\u4f1a\u88ab\u9664\u4ee5\u8be5\u7c7b\u578b\u7684\u6700\u5927\u503c\u52a0 1\uff0c\u4e14\u53ea\u4fdd\u7559\u4f59\u6570\u3002 280 \u4e0d\u5728 0 \u5230 255 \u7684\u8303\u56f4\u5185\u3002\u8be5\u8303\u56f4\u7684\u6700\u5927\u503c+1 \u662f 256\uff0c\u6240\u4ee5 280 \u4f1a\u9664\u4ee5 256\uff0c\u4f59\u6570\u4e3a 24\u3002\u6700\u7ec8 24 \u4f1a\u88ab\u5b58\u653e\u5230\u5185\u5b58\u4e2d\u3002 \u8fd9\u4e2a\u95ee\u9898\u8fd8\u53ef\u4ee5\u8fd9\u6837\u7406\u89e3\u3002\u6bd4\u6700\u5927\u503c\u8fd8\u5927\u7684\u6570\u5728\u5b58\u653e\u65f6\u4f1a\u88ab\u201cwraps around\u201d (\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a\u201cmodulo wrapping\u201d)\uff0c\u5373\u4ece 0 \u5f00\u59cb\u91cd\u65b0\u8ba1\u7b97\u3002255 \u5728 1 \u5b57\u8282\u6574\u578b\u7684\u8868\u793a\u8303\u56f4\u5185\uff0c\u6240\u4ee5 255 \u6ca1\u95ee\u9898\u3002\u800c 256 \u5219\u8d85\u8fc7\u4e86\u8303\u56f4\uff0c\u56e0\u6b64\u5b83\u4f1a\u88ab\u5f52\u4e3a 0\u3002257 \u5219\u4f1a\u5f52\u4e3a 1\u3002280 \u5219\u5f52\u4e3a 24. \u8fd9\u91cc\u662f\u4e00\u4e2a 2 \u5b57\u8282\u65e0\u7b26\u53f7\u77ed\u6574\u578b\uff08 unsigned short \uff09 \u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { unsigned short x { 65535 }; // largest 16-bit unsigned value possible std :: cout << \"x was: \" << x << '\\n' ; x = 65536 ; // 65536 is out of our range, so we get wrap-around std :: cout << \"x is now: \" << x << '\\n' ; x = 65537 ; // 65537 is out of our range, so we get wrap-around std :: cout << \"x is now: \" << x << '\\n' ; return 0 ; } \u8bd1\u8005\u6ce8 \u65e0\u7b26\u53f7\u4e0d\u4f1a\u6ea2\u51fa\uff0c\u7684\u786e\u662f C++\u6807\u51c6\uff0c\u4e0d\u6ea2\u51fa\u8fd9\u4e2a\u8bf4\u6cd5\u4e5f\u662f\u5408\u7406\u7684\uff0c\u56e0\u4e3a\u65e0\u7b26\u53f7\u5728\u9047\u5230\u8d85\u8fc7\u8868\u793a\u8303\u56f4\u7684\u60c5\u51b5\u65f6\u5e76\u4e0d\u4f1a\u8868\u73b0\u51fa \u672a\u5b9a\u4e49\u884c\u4e3a \uff0c\u7ed3\u679c\u4e5f\u4e0d\u662f\u7b80\u5355\u7684\u4e22\u5f03\uff0c\u800c\u662f\u5bf9\u539f\u503c\u8fdb\u884c\u6c42\u6a21\u8fd0\u7b97\u3002\u82f1\u6587\u53eb warp around\uff0c\u5373\u4ece 0 \u5f00\u59cb\u91cd\u65b0\u8ba1\u7b97\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e 32 \u4f4d\u65e0\u7b26\u53f7\uff0c\u8d85\u51fa\u8303\u56f4\u65f6\u7ed3\u679c\u7b49\u4e8e value mod 2^32 \u4f60\u89c9\u5f97\u7a0b\u5e8f\u7684\u7ed3\u679c\u4f1a\u662f\u4ec0\u4e48\u5462\uff1f (\u6ce8\u610f\uff0c\u5982\u679c\u4f60\u7f16\u8bd1\u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8bdd\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u4ea7\u751f\u5173\u4e8e\u6ea2\u51fa\u6216\u622a\u65ad\u7684\u544a\u8b66\u2014\u2014\u4f60\u9700\u8981\u5173\u95ed\u201d\u5c06\u544a\u8b66\u770b\u505a\u9519\u8bef\u201c\u529f\u80fd\uff0c\u624d\u80fd\u8fd0\u884c\u8be5\u7a0b\u5e8f\uff09 1 2 3 x was: 65535 x is now: 0 x is now: 1 \u5411\u53e6\u4e00\u4e2a\u65b9\u5411 wrap \u4e5f\u662f\u53ef\u4ee5\u7684\uff0c0 \u53ef\u4ee5\u88ab 2 \u5b57\u8282\u5927\u5c0f\u7684\u65e0\u7b26\u53f7\u6574\u578b\u8868\u793a\uff0c\u4f46\u662f-1 \u4e0d\u53ef\u4ee5\uff0c\u56e0\u6b64\u5b83\u4f1a\u88ab wrap \u5230\u6700\u5927\u503c\uff0c\u7ed3\u679c\u4e3a 65535\u3002-2 \u7684\u7ed3\u679c\u662f 65534\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { unsigned short x { 0 }; // smallest 2-byte unsigned value possible std :: cout << \"x was: \" << x << '\\n' ; x = -1 ; // -1 is out of our range, so we get wrap-around std :: cout << \"x is now: \" << x << '\\n' ; x = -2 ; // -2 is out of our range, so we get wrap-around std :: cout << \"x is now: \" << x << '\\n' ; return 0 ; } 1 2 3 x was: 0 x is now: 65535 x is now: 65534 \u4e0a\u8ff0\u4ee3\u7801\u4f1a\u5f15\u8d77\u67d0\u4e9b\u7f16\u8bd1\u5668\u4ea7\u751f\u544a\u8b66\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u68c0\u6d4b\u5230\u6574\u578b\u5b57\u9762\u91cf\u8d85\u8fc7\u4e86\u8868\u793a\u5b83\u7684\u7c7b\u578b\u7684\u8303\u56f4\u3002\u5982\u679c\u4f60\u5e0c\u671b\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\uff0c\u8bf7\u6682\u65f6\u5173\u95ed\u201d\u5c06\u544a\u8b66\u770b\u505a\u9519\u8bef\u201c\u529f\u80fd\u3002 \u9898\u5916\u8bdd \u5f88\u591a\u7535\u5b50\u6e38\u620f\u53f2\u4e0a\u7684\u8457\u540d\u7684 bug \u90fd\u548c\u65e0\u7b26\u53f7\u6570\u53cd\u8f6c\u6709\u5173\u3002\u8857\u673a\u6e38\u620f\u300a\u5927\u91d1\u521a\u300b\u4e2d\uff0c\u4f60\u4e0d\u53ef\u80fd\u901a\u8fc722\u5173\uff0c\u56e0\u4e3a\u6ea2\u51fabug\u5bfc\u81f4\u7528\u6237\u5728\u8fd9\u4e00\u5173\u4e2d\u4e0d\u53ef\u80fd\u6709\u8db3\u591f\u7684\u65f6\u95f4\u3002 \u7535\u8111\u6e38\u620f\u300a\u6587\u660e\u300b\u4e2d\uff0c\u7518\u5730\u901a\u5e38\u662f\u7b2c\u4e00\u4e2a\u4f7f\u7528\u6838\u6b66\u5668\u7684\uff0c\u8fd9\u548c\u4ed6\u672c\u5e94\u8be5\u6709\u7684\u4fdd\u5b88\u7684\u5c5e\u6027\u975e\u5e38\u77db\u76fe\u3002\u73a9\u5bb6\u8ba4\u4e3a\u8fd9\u662f\u7531\u4e8e\u7518\u5730\u7684\u653b\u51fb\u6027\u88ab\u521d\u59cb\u5316\u62101\uff0c\u4f46\u662f\u5982\u679c\u4ed6\u8d70\u6c11\u4e3b\u8def\u7ebf\u7684\u8bdd\uff0c\u5176\u653b\u51fb\u6027\u4f1a-2\u3002\u8fdb\u800c\u5bfc\u81f4\u4e86\u6570\u503c\u88ab\u53cd\u8f6c\u6210\u4e86255\uff0c\u4f7f\u5176\u53d8\u5f97\u975e\u5e38\u597d\u6597\u3002\u4e0d\u8fc7\uff0c\u6700\u8fd1\u5e2d\u5fb7\u6885\u5c14\u5bf9\u6b64\u8fdb\u884c\u4e86\u6f84\u6e05\uff0c\u4ed6\u8bf4\u8fd9\u5e76\u4e0d\u662f\u5f15\u8d77\u4e0a\u8ff0\u95ee\u9898\u7684\u539f\u56e0\u3002","title":"\u65e0\u7b26\u53f7\u6574\u578b\u6ea2\u51fa"},{"location":"4-5-Unsigned-integers-and-why-to-avoid-them/#_5","text":"\u5f88\u591a\u7a0b\u5e8f\u5458 (\u4ee5\u53ca\u4e00\u4e9b\u5927\u5382\uff0c\u4f8b\u5982\u8c37\u6b4c\uff09\u8ba4\u4e3a\u7a0b\u5e8f\u4e5f\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u65e0\u7b26\u53f7\u6574\u578b\u3002 \u8fd9\u4e3b\u8981\u662f\u56e0\u4e3a\u65e0\u7b26\u53f7\u6574\u578b\u6709\u4e24\u79cd\u884c\u4e3a\u53ef\u80fd\u4f1a\u5bfc\u81f4\u95ee\u9898\u3002 \u9996\u5148\uff0c\u5c06\u4e24\u4e2a\u65e0\u7b26\u53f7\u6574\u578b\u76f8\u51cf\uff0c\u4f8b\u59823-5\u7b49\u4e8e-2\uff0c\u4f46\u662f-2\u4e0d\u80fd\u88ab\u65e0\u7b26\u53f7\u6574\u578b\u8868\u793a\u3002 1 2 3 4 5 6 7 8 9 10 #include int main () { unsigned int x { 3 }; unsigned int y { 5 }; std :: cout << x - y << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\uff0c\u5c3d\u7ba1\u4e0a\u9762\u7684\u4ee3\u7801\u770b\u4e0a\u53bb\u4eba\u755c\u65e0\u5bb3\uff0c\u5b9e\u9645\u4e0a\u7ed3\u679c\u5374\u4ee4\u4eba\u5403\u60ca\uff1a 1 4294967294 \u8fd9\u662f\u56e0\u4e3a -2 \u88ab\u53cd\u8f6c\u5230\u4e86\u4e00\u4e2a\u63a5\u8fd1 4\u81ea\u5df1\u6574\u578b\u6700\u5927\u503c\u7684\u6570\u3002\u53e6\u5916\u4e00\u4e2a\u53ef\u80fd\u4ea7\u751f\u53cd\u8f6c\u7684\u60c5\u51b5\u662f\uff0c\u5bf9\u65e0\u7b26\u53f7\u6574\u578b\u4f7f\u7528\u9012\u51cf\u8fd0\u7b97\u7b26( -- )\u3002\u90a3\u4e48\u5f53\u53d8\u91cf\u5faa\u73af\u9012\u51cf\u65f6\uff0c\u6700\u7ec8\u53ef\u80fd\u4f1a\u53d1\u751f\u4e8e\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\u540c\u6837\u7684\u95ee\u9898\u3002 . \u6b64\u5916\uff0c\u5f53\u5c06\u6709\u7b26\u53f7\u6574\u578b\u548c\u65e0\u7b26\u53f7\u6574\u578b\u6df7\u7528\u65f6\uff0c\u4e5f\u53ef\u80fd\u4ea7\u751f\u610f\u5916\u3002\u5728C++\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u65f6\uff08\u4f8b\u5982\u7b97\u6570\u6216\u6bd4\u8f83\uff09\uff0c\u5982\u679c\u4e24\u4e2a\u64cd\u4f5c\u6570\u5206\u522b\u4e3a\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u90a3\u4e48\u6709\u7b26\u53f7\u7684\u4e00\u65b9\u4f1a\u88ab\u8f6c\u6362\u4e3a\u65e0\u7b26\u53f7\u3002\u7136\u540e\uff0c\u56e0\u4e3a\u65e0\u7b26\u53f7\u6574\u578b\u4e0d\u53ef\u4ee5\u4fdd\u5b58\u8d1f\u6570\uff0c\u8fd9\u5c31\u6709\u53ef\u80fd\u9020\u6210\u6570\u636e\u4e22\u5931\u7684\u60c5\u51b5\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { signed int s { -1 }; unsigned int u { 1 }; if ( s < u ) // -1 \u88ab\u9690\u5f0f\u8f6c\u6362\u6210\u4e86 4294967295, \u800c 4294967295 < 1 \u662f\u9519\u8bef\u7684 std :: cout << \"-1 is less than 1 \\n \" ; else std :: cout << \"1 is less than -1 \\n \" ; // \u8fd9\u53e5\u8bed\u53e5\u4f1a\u88ab\u6267\u884c return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u7f16\u8bd1\uff0c\u770b\u4e0a\u53bb\u4e5f\u7b26\u5408\u903b\u8f91\u3002\u4f46\u662f\u5b83\u6253\u5370\u7684\u7ed3\u679c\u5374\u662f\u9519\u8bef\u7684\u3002\u5c3d\u7ba1\u7f16\u8bd1\u5668\u5728\u68c0\u67e5\u5230\u6709\u7b26\u53f7/\u65e0\u7b26\u53f7\u4e0d\u5339\u914d\u65f6\uff0c\u4f1a\u53d1\u51fa\u4e00\u4e2a\u8b66\u544a\u3002\u4f46\u662f\u7531\u4e8e\u8fd8\u6709\u5f88\u591a\u5176\u4ed6\u60c5\u51b5\u4e0b\u7f16\u8bd1\u5668\u4e5f\u4f1a\u62a5\u544a\u5b8c\u5168\u4e00\u6837\u7684\u8b66\u544a\uff08\u4f8b\u5982\u4e24\u4e2a\u6570\u90fd\u662f\u6b63\u6570\u65f6\uff09\uff0c\u8fd9\u4f1a\u8ba9\u6211\u4eec\u5f88\u96be\u5224\u65ad\u771f\u6b63\u7684\u95ee\u9898\u5728\u54ea\u91cc\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 4.10 - if \u8bed\u53e5 \u4e2d\u4ecb\u7ecd if \u8bed\u53e5\u3002 \u6b64\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u53ef\u80fd\u6709\u95ee\u9898\u7684\u60c5\u51b5\uff0c\u662f\u6839\u672c\u65e0\u6cd5\u88ab\u68c0\u67e5\u5230\u7684\u3002\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 void doSomething ( unsigned int x ) { // Run some code x times std :: cout << \"x is \" << x << '\\n' ; } int main () { doSomething ( -1 ); return 0 ; } doSomething() \u51fd\u6570\u7684\u4f5c\u8005\u671f\u671b\u7528\u6237\u53ea\u4f20\u9012\u6b63\u6570\u7ed9\u8be5\u51fd\u6570\u3002\u4f46\u662f\u8c03\u7528\u8005\u72af\u4e86\u4e00\u4e2a\u9519\u8bef\uff0c\u5b83\u4f20\u4e86-1\u3002\u8fd9\u662f\u4e2a\u5f88\u663e\u7136\u7684\u9519\u8bef\uff0c\u4f46\u662f\u4eba\u4eec\u4ecd\u7136\u96be\u514d\u4f1a\u72af\u3002\u90a3\u4e48\u6b64\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f \u5b9e\u53c2 -1 \u88ab\u9690\u5f0f\u8f6c\u6362\u6210\u4e86\u4e00\u4e2a\u65e0\u7b26\u53f7 \u5f62\u53c2 \u3002-1 \u4e0d\u5728\u65e0\u7b26\u53f7\u6574\u578b\u7684\u8868\u793a\u8303\u56f4\uff0c\u56e0\u6b64\u5fc5\u7136\u88ab\u53cd\u8f6c\u6210\u4e00\u4e2a\u975e\u5e38\u5927\u7684\u6570(\u53ef\u80fd\u662f 4294967295)\u3002\u7136\u540e\u4f60\u7684\u7a0b\u5e8f\u5c31\u4f1a\u51fa\u73b0\u95ee\u9898\u3002 \u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u5e76\u6ca1\u6709\u4e00\u79cd\u65b9\u6cd5\u80fd\u591f\u907f\u514d\u4e0a\u8ff0\u95ee\u9898\u7684\u53d1\u751f\u3002C++ \u53ef\u4ee5\u81ea\u7531\u5730\u8f6c\u6362\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u6570\uff0c\u4f46\u662f\u5b83\u5e76\u4e0d\u4f1a\u5728\u8f6c\u6362\u65f6\u8fdb\u884c\u4efb\u4f55\u7684\u8303\u56f4\u68c0\u67e5\u4ee5\u786e\u4fdd\u4e0d\u53d1\u751f\u6ea2\u51fa\u3002 \u4e0a\u8ff0\u8fd9\u4e9b\u95ee\u9898\u90fd\u662f\u7ecf\u5e38\u4f1a\u9047\u5230\u7684\uff0c\u5b83\u4eec\u4ea7\u751f\u610f\u6599\u4e4b\u5916\u7684\u884c\u4e3a\uff0c\u5e76\u4e14\u96be\u4ee5\u88ab\u53d1\u73b0\u3002\u5373\u4f7f\u4f7f\u7528\u4e00\u4e9b\u88ab\u4e13\u7528\u4e8e\u67e5\u627e\u6b64\u7c7b\u95ee\u9898\u7684\u5de5\u5177\u6709\u5f88\u96be\u53d1\u73b0\u3002 \u56e0\u6b64\uff0c\u4e00\u4e9b\u6709\u4e89\u8bae\u7684\u6700\u4f73\u5b9e\u8df5\u8ba4\u4e3a\uff0c\u6211\u4eec\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u65e0\u7b26\u53f7\u7c7b\u578b\uff0c\u9664\u975e\u786e\u5b9e\u6709\u7279\u6b8a\u7684\u9700\u6c42\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c3d\u91cf\u4f7f\u7528\u6709\u7b26\u53f7\u6570\u6765\u5b58\u653e\u6570\u91cf\u503c\uff08\u5c3d\u7ba1\u6570\u91cf\u53ef\u80fd\u90fd\u662f\u975e\u8d1f\u7684\uff09\u548c\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u3002\u907f\u514d\u6df7\u7528\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u7c7b\u578b\u3002 \u76f8\u5173\u5185\u5bb9 \u652f\u6301\u4e0a\u8ff0\u5efa\u8bae\u7684\u4e00\u4e9b\u6750\u6599(\u540c\u65f6\u4e5f\u9a73\u65a5\u4e86\u4e00\u4e9b\u5e38\u89c1\u7684\u53cd\u5bf9\u610f\u89c1\uff09\uff1a Interactive C++ panel (see 12:12-13:08,42:40-45:26, and 1:02:50-1:03:15) Subscripts and sizes should be signed Unsigned integers from the libtorrent blog","title":"\u6709\u5173\u65e0\u7b26\u53f7\u6574\u578b\u7684\u4e89\u8bba"},{"location":"4-5-Unsigned-integers-and-why-to-avoid-them/#_6","text":"\u8fd8\u662f\u6709\u4e00\u4e9b\u573a\u5408\u662f\u9700\u8981\u6216\u8005\u53ef\u4ee5\u4f7f\u7528\u65e0\u7b26\u53f7\u6570\u7684\u3002 \u9996\u5148\uff0c\u65e0\u7b26\u53f7\u6570\u5728\u8fdb\u884c\u4f4d\u8fd0\u7b97\u65f6\u662f\u63a8\u8350\u4f7f\u7528\u7684\uff08\u5728\u7ae0\u8282O\u4e2d\u4ecb\u7ecd\uff09\u3002\u5728\u4e00\u4e9b\u786e\u5b9e\u9700\u8981\u53cd\u8f6c\u884c\u4e3a\u7684\u573a\u5408\uff0c\u65e0\u7b26\u53f7\u6570\u4e5f\u662f\u6709\u7528\u7684\uff08\u4f8b\u5982\u4e00\u4e9b\u52a0\u5bc6\u7b97\u6cd5\u548c\u968f\u673a\u6570\u751f\u6210\uff09\u3002 \u5176\u6b21\uff0c\u5728\u6709\u4e9b\u65f6\u5019\u65e0\u7b26\u53f7\u6570\u662f\u65e0\u6cd5\u907f\u514d\u7684\uff08\u5f88\u591a\u4e0e\u6570\u7ec4\u8fdb\u884c\u7d22\u5f15\u6709\u5173\uff09\u3002\u6211\u4eec\u4f1a\u5728\u6709\u5173\u6570\u7ec4\u7684\u8bfe\u7a0b\u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002\u5728\u8fd9\u4e9b\u60c5\u51b5\u4e0b\uff0c\u65e0\u7b26\u53f7\u6570\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u6570\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast \u4e2d\u4ecb\u7ecd\u5982\u4f55\u5c06\u65e0\u7b26\u53f7\u6570\u8f6c\u6362\u4e3a\u6709\u7b26\u53f7\u6570\u3002 \u540c\u65f6\u8bf7\u6ce8\u610f\uff0c\u5982\u679c\u4f60\u5728\u5f00\u53d1\u5d4c\u5165\u5f0f\u7cfb\u7edf\uff08\u4f8b\u5982 Arduino\uff09\u6216\u8005\u5176\u4ed6\u5904\u7406\u5668/\u5185\u5b58\u6bd4\u8f83\u5403\u7d27\u7684\u73af\u5883\uff0c\u51fa\u4e8e\u6027\u80fd\u7684\u539f\u56e0\u800c\u4f7f\u7528\u65e0\u7b26\u53f7\u6570\u662f\u975e\u5e38\u5e38\u89c1\u7684\uff0c\u540c\u65f6\u4e5f\u662f\u53ef\u4ee5\u88ab\u63a5\u53d7\u7684\uff08\u5728\u6709\u4e9b\u573a\u666f\u4e0b\u4e5f\u662f\u65e0\u6cd5\u907f\u514d\u7684\uff09\u3002","title":"\u90a3\u4e48\u4ec0\u4e48\u65f6\u5019\u624d\u4f7f\u7528\u65e0\u7b26\u53f7\u6570\u5462\uff1f"},{"location":"4-6-Fixed-width-integers-and-size_t/","tags":["data type","fixed-width","integer","size_t","C++11"],"text":"Key Takeaway \u4eceC\u8bed\u8a00\u7ee7\u627f\u800c\u6765\u7684\u6574\u578b\u5927\u5c0f\u662f\u4e0d\u56fa\u5b9a\u7684\uff0c\u6700\u5c0f\u662f2\u5b57\u8282\uff0c\u4f46\u662f\u66f4\u591a\u65f6\u5019\u662f4\u5b57\u8282\u3002 C99\u548cC++11\u63d0\u4f9b\u4e86\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\uff0c\u901a\u8fc7\u5934\u6587\u4ef6 \u53ef\u4ee5\u4f7f\u7528\u3002\u7f3a\u70b9\u662f\u5e76\u975e\u6240\u6709\u673a\u5668\u4e0a\u90fd\u6709\u5b9a\u4e49\u4e14\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd C++\u63d0\u4f9b\u4e86\u901f\u5ea6\u4f18\u5148\u548c\u5927\u5c0f\u4f18\u5148\u7684\u4e24\u7c7b\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b \u907f\u514d\u4f7f\u75288\u4f4d\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u3002\u5982\u679c\u4e00\u5b9a\u8981\u4f7f\u7528\uff0c\u8bf7\u6ce8\u610f\u5b83\u4eec\u7684\u884c\u4e3a\u66f4\u7c7b\u4f3c\u4e8e\u5b57\u7b26\u3002 \u6574\u578b\u4f7f\u7528\u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u6574\u578b\u7684\u5927\u5c0f\u4e0d\u91cd\u8981\uff08\u4f8b\u5982\u5b58\u653e\u7684\u6570\u603b\u662f\u57282\u5b57\u8282\u6709\u7b26\u53f7\u6574\u578b\u8303\u56f4\u5185\uff09\uff0c\u63a8\u8350\u4f7f\u7528 int \u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u8981\u6c42\u7528\u6237\u8f93\u5165\u5e74\u9f84\uff0c\u6216\u8005\u8fdb\u884c10\u4ee5\u5185\u7684\u8ba1\u6570\uff08\u6b64\u65f62\u5b57\u8282\u8303\u56f4\u603b\u662f\u591f\u7528\u7684\uff09\u3002\u90a3\u4e48\u4f7f\u7528 int \u5c31\u8db3\u591f\u5e94\u5bf9\u7edd\u5927\u591a\u6570\u573a\u666f\u4e86\uff1b \u5982\u679c\u9700\u8981\u5b58\u50a8\u6570\u91cf\u503c\uff0c\u4e14\u8303\u56f4\u5fc5\u987b\u660e\u786e\u65f6\uff0c\u63a8\u8350\u4f7f\u7528 std::int#_t \uff1b \u5982\u679c\u9700\u8981\u8fdb\u884c\u4f4d\u8fd0\u7b97\u6216\u60f3\u8981\u5229\u7528\u65e0\u7b26\u53f7\u6570\u7684\u7ffb\u8f6c\u7279\u6027\u65f6\uff0c\u4f7f\u7528 std::uint#_t \u3002 \u907f\u514d\uff1a \u4f7f\u7528\u65e0\u7b26\u53f7\u7c7b\u578b\u4fdd\u5b58\u6570\u91cf\u503c\uff1b \u4f7f\u7528 8 \u4e3a\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\uff1b \u4f7f\u7528\u901f\u5ea6\u6216\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u7c7b\u578b\uff1b \u4efb\u4f55\u4e0e\u7279\u5b9a\u7f16\u8bd1\u5668\u76f8\u5173\u7684\u56fa\u5b9a\u6574\u578b\u7c7b\u578b\u2014\u2014\u4f8b\u5982 Visual Studio \u5b9a\u4e49\u7684 __int8 \uff0c __int16 \u7b49\u3002 std::size_t \u662f\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u5176\u5bbd\u5ea6\u901a\u5e38\u7b49\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\u4f4d\u6570\uff0c\u540c\u65f6\u5176\u80fd\u591f\u8868\u793a\u7684\u6700\u5927\u503c\u4e5f\u662f\u7cfb\u7edf\u80fd\u591f\u521b\u5efa\u5bf9\u8c61\u5927\u5c0f\u7684\u7406\u8bba\u4e0a\u9650 \u4e0a\u9762\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u6574\u578b\uff0c\u5e76\u4e14\u63d0\u5230 C++ \u53ea\u4fdd\u8bc1\u6574\u578b\u503c\u5360\u7528\u7a7a\u95f4\u7684\u6700\u5c0f\u503c\u2014\u2014\u4f46\u662f\u5b83\u5360\u7528\u7684\u7a7a\u95f4\u53ef\u4ee5\u66f4\u5927\uff0c\u8fd9\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u7cfb\u7edf\u3002 \u4e3a\u4ec0\u4e48\u6574\u578b\u7684\u5927\u5c0f\u4e0d\u662f\u56fa\u5b9a\u7684\uff1f \u7b80\u800c\u8a00\u4e4b\uff0c\u8fd9\u8fd8\u662f\u4ece C \u8bed\u8a00\u7ee7\u627f\u800c\u6765\u7684\u3002\u5728\u8ba1\u7b97\u673a\u975e\u5e38\u6162\u7684\u5e74\u4ee3\uff0c\u6027\u80fd\u662f\u6700\u9700\u8981\u88ab\u8003\u8651\u7684\u95ee\u9898\u3002C \u8bed\u8a00\u9009\u62e9\u6545\u610f\u4e0d\u53bb\u56fa\u5b9a\u6574\u578b\u7684\u5927\u5c0f\uff0c\u4ee5\u4fbf\u7f16\u8bd1\u5668\u7684\u5b9e\u73b0\u8005\u53ef\u4ee5\u4e3a\u76ee\u6807\u8ba1\u7b97\u673a\u9009\u62e9\u6700\u5408\u9002\u7684\u6574\u578b\u5927\u5c0f\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002 \u5f88\u8ba8\u538c\u662f\u5427\uff1f \u4ece\u73b0\u4ee3\u6807\u51c6\u7684\u89d2\u5ea6\u6765\u8bb2\uff0c\u662f\u7684\u3002\u4f5c\u4e3a\u7a0b\u5e8f\u5458\u8fd8\u8981\u5904\u7406\u7c7b\u578b\u8303\u56f4\u4e0d\u786e\u5b9a\u7684\u60c5\u51b5\u786e\u5b9e\u6709\u70b9\u8352\u5510\u3002 \u8003\u8651 int \u7c7b\u578b\uff0c\u5176\u5360\u7528\u5185\u5b58\u6700\u5c0f\u4e3a 2 \u5b57\u8282\uff0c\u4f46\u662f\u5728\u73b0\u4ee3\u8ba1\u7b97\u673a\u4e0a\u5374\u7ecf\u5e38\u4e3a 4 \u5b57\u8282\u3002\u5982\u679c\u4f60\u5047\u8bbe int \u662f 4 \u4e2a\u5b57\u8282\uff08\u786e\u5b9e 4 \u5b57\u8282\u60c5\u51b5\u6700\u6709\u53ef\u80fd\uff09\uff0c\u90a3\u4e48\u4f60\u7684\u7a0b\u5e8f\u5728 int \u5b9e\u9645\u53ea\u5360\u4e24\u4e2a\u5b57\u8282\u7684\u8ba1\u7b97\u673a\u4e0a\u5c31\u4f1a\u4ea7\u751f\u5f02\u5e38\u7684\u884c\u4e3a\uff08\u56e0\u4e3a\u4f60\u5f88\u53ef\u80fd\u628a\u4e00\u4e2a\u9700\u8981 4 \u5b57\u8282\u624d\u80fd\u5b58\u653e\u7684\u503c\u5b58\u653e\u5230\u4e86\u5b9e\u9645\u53ea\u6709 2 \u4e2a\u5b57\u8282\u7684\u53d8\u91cf\u4e2d\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u6ea2\u51fa\u6216 \u672a\u5b9a\u4e49\u884c\u4e3a \uff09\u3002\u800c\u5982\u679c\u4f60\u5047\u8bbe int \u53ea\u6709\u4e24\u4e2a\u5b57\u8282\u4ee5\u4fbf\u83b7\u5f97\u6700\u597d\u7684\u517c\u5bb9\u6027\uff0c\u90a3\u4e48\u5728 4 \u5b57\u8282\u7cfb\u7edf\u4e0a\u65e0\u7591\u4f1a\u6d6a\u8d39\u4e24\u4e2a\u5b57\u8282\uff0c\u8fd9\u4f1a\u4f7f\u5f97\u4f60\u7684\u5185\u5b58\u4f7f\u7528\u6210\u500d\u589e\u52a0\uff01 \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cC99 \u5b9a\u4e49\u4e86\u4e00\u7ec4\u56fa\u5b9a\u5bbd\u5ea6\u6574\u5f62\uff08\u4f4d\u4e8e stdint.h \uff09\u6765\u786e\u4fdd\u6574\u578b\u5728\u4e0d\u540c\u7684\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u4e0b\u90fd\u5177\u6709\u76f8\u540c\u7684\u5927\u5c0f\u3002 \u540d\u79f0 \u7c7b\u578b \u8303\u56f4 \u5907\u6ce8 std::int8_t 1 byte signed -128 \u5230 127 \u5728\u5927\u591a\u6570\u7cfb\u7edf\u4e0a\u88ab\u770b\u505a\u6709\u7b26\u53f7\u5b57\u7b26\u5904\u7406\uff0c\u89c1\u4e0b\u9762\u5907\u6ce8\u3002 std::uint8_t 1 byte unsigned 0 \u5230 255 \u5728\u5927\u591a\u6570\u7cfb\u7edf\u4e0a\u88ab\u770b\u505a\u65e0\u7b26\u53f7\u5b57\u7b26\u5904\u7406\uff0c\u89c1\u4e0b\u9762\u5907\u6ce8 std::int16_t 2 byte signed -32,768 \u5230 32,767 std::uint16_t 2 byte unsigned 0 \u5230 65,535 std::int32_t 4 byte signed -2,147,483,648 \u5230 2,147,483,647 std::uint32_t 4 byte unsigned 0 \u5230 4,294,967,295 std::int64_t 8 byte signed -9,223,372,036,854,775,808 \u5230 9,223,372,036,854,775,807 std::uint64_t 8 byte unsigned 0 \u5230 18,446,744,073,709,551,615 C++ \u5728 C++11 \u4e2d\u5b98\u65b9\u5438\u7eb3\u4e86\u8fd9\u4e9b\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u3002\u901a\u8fc7\u5305\u542b \u5934\u6587\u4ef6\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\uff0c\u5b83\u4eec\u88ab\u5b9a\u4e49\u5728 std \u547d\u540d\u7a7a\u95f4\u4e2d\u3002 \u8bf7\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 #include // for fixed-width integers #include int main () { std :: int16_t i { 5 }; std :: cout << i ; return 0 ; } \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u901a\u5e38\u6765\u8bb2\u6709\u4e24\u4e2a\u7f3a\u9677\u3002 \u9996\u5148\uff0c\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u4e0d\u80fd\u4fdd\u8bc1\u5728\u6240\u6709\u7684\u4f53\u7cfb\u7ed3\u6784\u4e2d\u90fd\u88ab\u5b9a\u4e49\u4e86\u3002\u5b83\u53ea\u5b58\u5728\u4e8e, They only exist on systems where there are fundamental types matching their widths and following a certain binary representation.\u5982\u679c\u4f53\u7cfb\u7ed3\u6784\u4e0d\u652f\u6301\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\uff0c\u5219\u4f60\u7684\u7a0b\u5e8f\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\u3002\u4e0d\u8fc7\uff0c\u5927\u591a\u6570\u7684\u73b0\u4ee3\u4f53\u7cfb\u7ed3\u6784\u90fd\u4ee5 8/16/32/64-\u4f4d\u7684\u53d8\u91cf\u4e3a\u6807\u51c6\uff0c\u56e0\u6b64\u9664\u975e\u4f60\u9700\u8981\u5c06\u7a0b\u5e8f\u79fb\u690d\u5230\u67d0\u4e2a\u6781\u5176\u7279\u6b8a\u7684\u5927\u578b\u673a\u6216\u5d4c\u5165\u5f0f\u7cfb\u7edf\u4e0a\uff0c\u5426\u5219\u4e00\u822c\u6ca1\u6709\u95ee\u9898\u3002 \u5176\u6b21\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u4e86\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\uff0c\u5b83\u7684\u6027\u80fd\u76f8\u8f83\u4e8e\u4f7f\u7528\u66f4\u5bbd\u7684\u7c7b\u578b\u53ef\u80fd\u6216\u7a0d\u5dee\uff08\u5728\u540c\u6837\u7684\u4f53\u7cfb\u7ed3\u6784\u4e0b\uff09\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a\u5927\u5c0f\u786e\u5b9a\u4e3a 32 \u4f4d\u7684\u6574\u578b\uff0c\u4f60\u53ef\u80fd\u4f1a\u4f7f\u7528 std:: int32_t \uff0c\u4f46\u662f\u4f60\u7684 CPU \u53ef\u80fd\u5728\u5904\u7406 64 \u4f4d\u6574\u5f62\u65f6\u66f4\u5feb\u3002\u4e0d\u8fc7\uff0c\u5373\u4fbf CPU \u80fd\u591f\u66f4\u5feb\u7684\u5904\u7406\u67d0\u79cd\u7ed9\u5b9a\u7684\u7c7b\u578b\uff0c\u4e5f\u4e0d\u4e00\u5b9a\u610f\u5473\u7740\u7a0b\u5e8f\u7684\u8fd0\u884c\u901f\u5ea6\u66f4\u5feb\u2014\u2014\u73b0\u4ee3\u7a0b\u5e8f\u66f4\u591a\u53d7\u9650\u4e8e\u5185\u5b58\u4f7f\u7528\u800c\u4e0d\u662f CPU \u5904\u7406\u901f\u5ea6\u3002\u4f7f\u7528\u66f4\u5927\u7684\u5185\u5b58\u8db3\u8ff9(memory footprint)\u5e26\u6765\u7684\u6027\u80fd\u635f\u5931\u53ef\u80fd\u4f1a\u8d85\u8fc7 CPU \u5bf9\u5176\u8fdb\u884c\u7684\u52a0\u901f\u3002\u4e0d\u7ecf\u5b9e\u9645\u6d4b\u8bd5\u662f\u5f88\u96be\u8fdb\u884c\u5bf9\u6bd4\u7684\u3002 \u901f\u5ea6\u4f18\u5148\u6574\u578b\u548c\u5c3a\u5bf8\u4f18\u5148\u6574\u578b \u4e3a\u4e86\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\uff0cC++\u8fd8\u5b9a\u4e49\u4e86\u53e6\u5916\u4e24\u7ec4\u6574\u578b\uff0c\u5e76\u786e\u4fdd\u5b83\u4eec\u603b\u662f\u5177\u6709\u5b9a\u4e49\u3002 \u901f\u5ea6\u4f18\u5148\u7684\u7c7b\u7c7b\u578b( std::int_fast#_t \u548c std::uint_fast#_t ) \u63d0\u4f9b\u4e86\u6700\u5feb\u7684\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u540c\u65f6\u5176\u5bbd\u5ea6\u6700\u5c0f\u4e3a # \u4f4d ( # = 8\u300116\u300132 \u6216 64)\u3002\u4f8b\u5982\uff0c std::int_fast32_t \u53ef\u4ee5\u5b9a\u4e49\u5bbd\u5ea6\u6700\u5c0f\u4e3a 32 \u4f4d\u4e14\u901f\u5ea6\u6700\u5feb\u7684\u6709\u7b26\u53f7\u6574\u578b\u3002 \u5c3a\u5bf8\u4f18\u5148\u7684\u7c7b\u578b( std::int_least#_t \u548c std::uint_least#_t ) \u63d0\u4f9b\u4e86\u6700\u5c0f\u7684\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u5176\u5bbd\u5ea6\u4e3a # \u4f4d ( # = 8\u300116\u300132 \u6216 64)\u3002\u4f8b\u5982\uff0c std::uint_least32_t \u53ef\u4ee5\u5b9a\u4e49\u5bbd\u5ea6\u81f3\u5c11\u4e3a 32 \u4f4d\u7684\u6700\u5c0f\u7684\u65e0\u7b26\u53f7\u6574\u578b\u3002 \u4ee5\u4f5c\u8005\u4f7f\u7528\u7684 Visual Studio (32-bit \u63a7\u5236\u53f0\u7a0b\u5e8f)\u4e3a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // for fixed-width integers #include int main () { std :: cout << \"least 8: \" << sizeof ( std :: int_least8_t ) * 8 << \" bits \\n \" ; std :: cout << \"least 16: \" << sizeof ( std :: int_least16_t ) * 8 << \" bits \\n \" ; std :: cout << \"least 32: \" << sizeof ( std :: int_least32_t ) * 8 << \" bits \\n \" ; std :: cout << '\\n' ; std :: cout << \"fast 8: \" << sizeof ( std :: int_fast8_t ) * 8 << \" bits \\n \" ; std :: cout << \"fast 16: \" << sizeof ( std :: int_fast16_t ) * 8 << \" bits \\n \" ; std :: cout << \"fast 32: \" << sizeof ( std :: int_fast32_t ) * 8 << \" bits \\n \" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 least 8: 8 bits least 16: 16 bits least 32: 32 bits fast 8: 8 bits fast 16: 32 bits fast 32: 32 bits \u53ef\u4ee5\u770b\u51fa std::int_least16_t \u662f 16 \u4f4d\u7684\uff0c\u4f46\u662f std::int_fast16_t \u5b9e\u9645\u4e0a\u5374\u662f 32 \u4f4d\u3002\u8fd9\u662f\u56e0\u4e3a\u5728\u4f5c\u8005\u7684\u673a\u5668\u4e0a\uff0c32 \u4f4d\u6574\u578b\u7684\u5904\u7406\u901f\u5ea6\u8981\u6bd4 16 \u4f4d\u5757\u3002 \u7136\u800c\uff0c\u8fd9\u4e24\u79cd\u7c7b\u578b\u4e5f\u6709\u5176\u7f3a\u70b9\uff1a\u9996\u5148\uff0c\u4f7f\u7528\u5b83\u4eec\u7684\u7a0b\u5e8f\u5458\u5b9e\u9645\u4e0a\u5e76\u4e0d\u591a\uff0c\u4e0d\u719f\u6089\u7684\u60c5\u51b5\u4f7f\u7528\u5b83\u4eec\u4f1a\u9020\u6210\u9519\u8bef\u3002\u53e6\u5916\uff0c\u901f\u5ea6\u4f18\u5148\u7684\u7c7b\u578b\u540c\u6837\u4e5f\u4f1a\u5e26\u6765\u548c 4 \u6bd4\u7279\u5bbd\u6574\u578b\u4e00\u6837\u7684\u5185\u5b58\u6d6a\u8d39\u95ee\u9898\u3002\u66f4\u4e25\u91cd\u7684\u662f\uff0c\u56e0\u4e3a\u8fd9\u4e24\u79cd\u6574\u578b\u7c7b\u578b\u7684\u5927\u5c0f\u662f\u53d8\u5316\u7684\uff0c\u90a3\u4e48\u53ef\u80fd\u4f1a\u9020\u6210\u4f60\u7684\u7a0b\u5e8f\u5728\u4e0d\u540c\u4f53\u7cfb\u7ed3\u6784\u7684\u7535\u8111\u4e0a\u884c\u4e3a\u6709\u6240\u4e0d\u540c\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include // for fixed-width integers #include int main () { std :: uint_fast16_t sometype { 0 }; -- sometype ; // \u6545\u610f\u5229\u7528\u65e0\u7b26\u53f7\u6574\u578b\u53cd\u8f6c\u7684\u7279\u6027 std :: cout << sometype ; return 0 ; } \u4e0a\u8ff0\u4ee3\u7801\u7684\u8f93\u51fa\u7ed3\u679c\u53d6\u51b3\u4e8e std::uint_fast16_t \u662f 16 \u4f4d\u300132 \u4f4d\u8fd8\u662f 64 \u4f4d\u3002 \u9664\u975e\u80fd\u5728\u5bf9\u5e94\u7684\u5e73\u53f0\u4e0a\u8fdb\u884c\u6d4b\u8bd5\uff0c\u5426\u5219\u6211\u4eec\u5f88\u96be\u9884\u77e5\u7a0b\u5e8f\u5728\u4ec0\u4e48\u5e73\u53f0\u4e0a\u8fd0\u884c\u65f6\u4f1a\u51fa\u95ee\u9898\uff0c\u800c\u5bf9\u4e8e\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u6211\u4eec\u80fd\u591f\u4f7f\u7528\u7684\u5e73\u53f0\u79cd\u7c7b\u5e76\u4e0d\u4f1a\u5f88\u591a\u3002 std:: int8_t \u548c std:: uint8_t \u7684\u884c\u4e3a\u53ef\u80fd\u66f4\u50cf\u5b57\u7b26\u800c\u975e\u6574\u578b \u7531\u4e8e\u5bf9C++\u6807\u51c6\u7814\u7a76\u4e0d\u591f\u4ed4\u7ec6\uff0c\u5f88\u591a\u7f16\u8bd1\u5668\u5b9a\u4e49\u548c\u5bf9\u5f85 std::int8_t \u548c std::uint8_t \u7c7b\u578b(\u4ee5\u53ca\u5176\u5bf9\u5e94\u7684\u901f\u5ea6\u6216\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u7c7b\u578b) \u7684\u65b9\u5f0f\u548c signed char \u4e0e unsigned char \u662f\u5b8c\u5168\u4e00\u6837\u7684\u3002\u8fd9\u4e5f\u5c31\u610f\u5473\u77408\u4f4d\u7684\u7c7b\u578b\u53ef\u80fd\uff08\u4e5f\u53ef\u80fd\u4e0d\uff09\u4e0e\u5176\u4ed6\u56fa\u5b9a\u5bbd\u5ea6\u7c7b\u578b\u5177\u6709\u4e0d\u540c\u7684\u884c\u4e3a\uff0c\u8fd9\u6709\u53ef\u80fd\u9020\u6210\u9519\u8bef\u3002\u5177\u4f53\u7684\u884c\u4e3a\u548c\u7cfb\u7edf\u76f8\u5173\uff0c\u6240\u4ee5\u5728\u67d0\u4e2a\u5e73\u53f0\u4e0a\u6b63\u5e38\u5de5\u4f5c\u7684\u7a0b\u5e8f\u5728\u5176\u4ed6\u5e73\u53f0\u4e0a\u53ef\u80fd\u65e0\u6cd5\u5de5\u4f5c\u6216\u7f16\u8bd1\u3002 \u6211\u4eec\u4f1a\u5728 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast \u4e2d\u4e3e\u4f8b\u8bf4\u660e\u3002 \u4e3a\u4e86\u4fdd\u6301\u4e00\u81f4\u6027\uff0c\u6700\u597d\u80fd\u591f\u907f\u514d\u4f7f\u7528 _ std::int8_t \u548c std::uint8_t (\u4ee5\u53ca\u5176\u76f8\u5173\u7684\u901f\u5ea6\u6216\u5927\u5c0f\u4f18\u5148\u7c7b\u578b) \u3002\u53ef\u4ee5\u4f7f\u7528 std::int16_t \u6216 std::uint16_t \u4ee3\u66ff\u3002 \u6ce8\u610f \u907f\u514d\u4f7f\u75288\u4f4d\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u3002\u5982\u679c\u4e00\u5b9a\u8981\u4f7f\u7528\uff0c\u8bf7\u6ce8\u610f\u5b83\u4eec\u7684\u884c\u4e3a\u66f4\u7c7b\u4f3c\u4e8e\u5b57\u7b26\u3002 \u6574\u578b\u7c7b\u578b\u9009\u62e9\u7684\u6700\u4f73\u5b9e\u8df5 \u7531\u4e8e\u5404\u79cd\u57fa\u7840\u6574\u578b\u7c7b\u578b\u90fd\u5b58\u5728\u5404\u81ea\u7684\u4f18\u7f3a\u70b9\uff0c\u5982\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u7c7b\u578b\uff0c\u901f\u5ea6/\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u7c7b\u578b\u4ee5\u53ca\u6709\u7b26\u53f7\u65e0\u7b26\u53f7\u6574\u578b\u3002\u6211\u4eec\u9700\u8981\u5728\u4f7f\u7528\u65f6\u53c2\u8003\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u3002 \u6211\u4eec\u7684\u5b97\u65e8\u662f\uff0c\u6b63\u786e\u6027\u4f18\u4e8e\u901f\u5ea6\uff0c\u7f16\u8bd1\u65f6\u53d1\u73b0\u9519\u8bef\u4f18\u4e8e\u8fd0\u884c\u65f6\u53d1\u73b0\u9519\u8bef\u2014\u2014\u57fa\u4e8e\u8fd9\u4e2a\u6307\u5bfc\u539f\u5219\uff0c\u6211\u4eec\u5efa\u8bae\u907f\u514d\u4f7f\u7528\u901f\u5ea6/\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u7c7b\u578b\uff0c\u4f7f\u7528\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u7c7b\u578b\u3002\u5982\u679c\u5c06\u6765\u53d1\u73b0\u67d0\u4e2a\u5e73\u53f0\u4e0a\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u4e0d\u80fd\u7f16\u8bd1\uff0c\u90a3\u65f6\u4f60\u53ef\u4ee5\u518d\u51b3\u5b9a\u662f\u5426\u8981\u4fee\u6539\u4f60\u7684\u7a0b\u5e8f\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u6574\u578b\u7684\u5927\u5c0f\u4e0d\u91cd\u8981\uff08\u4f8b\u5982\u5b58\u653e\u7684\u6570\u603b\u662f\u57282\u5b57\u8282\u6709\u7b26\u53f7\u6574\u578b\u8303\u56f4\u5185\uff09\uff0c\u63a8\u8350\u4f7f\u7528 int \u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u8981\u6c42\u7528\u6237\u8f93\u5165\u5e74\u9f84\uff0c\u6216\u8005\u8fdb\u884c10\u4ee5\u5185\u7684\u8ba1\u6570\uff08\u6b64\u65f62\u5b57\u8282\u8303\u56f4\u603b\u662f\u591f\u7528\u7684\uff09\u3002\u90a3\u4e48\u4f7f\u7528 int \u5c31\u8db3\u591f\u5e94\u5bf9\u7edd\u5927\u591a\u6570\u573a\u666f\u4e86\uff1b \u5982\u679c\u9700\u8981\u5b58\u50a8\u6570\u91cf\u503c\uff0c\u4e14\u8303\u56f4\u5fc5\u987b\u660e\u786e\u65f6\uff0c\u63a8\u8350\u4f7f\u7528 std::int#_t \uff1b \u5982\u679c\u9700\u8981\u8fdb\u884c\u4f4d\u8fd0\u7b97\u6216\u60f3\u8981\u5229\u7528\u65e0\u7b26\u53f7\u6570\u7684\u7ffb\u8f6c\u7279\u6027\u65f6\uff0c\u4f7f\u7528 std::uint#_t \u3002 \u5c3d\u91cf\u907f\u514d\uff1a \u4f7f\u7528\u65e0\u7b26\u53f7\u7c7b\u578b\u4fdd\u5b58\u6570\u91cf\u503c\uff1b \u4f7f\u7528 8 \u4e3a\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\uff1b \u4f7f\u7528\u901f\u5ea6\u6216\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u7c7b\u578b\uff1b \u4efb\u4f55\u4e0e\u7279\u5b9a\u7f16\u8bd1\u5668\u76f8\u5173\u7684\u56fa\u5b9a\u6574\u578b\u7c7b\u578b\u2014\u2014\u4f8b\u5982 Visual Studio \u5b9a\u4e49\u7684 __int8 \uff0c __int16 \u7b49\u3002 std:: size_t \u662f\u4ec0\u4e48\uff1f \u8003\u8651\u5982\u4e0b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << sizeof ( int ) << '\\n' ; return 0 ; } \u5728\u4f5c\u8005\u7684\u673a\u5668\u4e0a\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 4 \u5f88\u7b80\u5355\u6ca1\u9519\u5427\uff1f\u6211\u4eec\u77e5\u9053 sizeof \u8fd0\u7b97\u7b26\u80af\u5b9a\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\u6570\uff0c\u4f46\u662f\u8fd9\u4e2a\u6574\u578b\u6570\u7a76\u7adf\u662f\u4ec0\u4e48\u7c7b\u578b\u5462\uff1f\u662f int \uff1f\u8fd8\u662f short \uff1f\u3002 \u5b9e\u9645\u4e0a sizeof \uff08\u4ee5\u53ca\u5176\u4ed6\u8fd4\u56de\u957f\u5ea6\u6216\u5927\u5c0f\u7684\u51fd\u6570\uff09\u8fd4\u56de\u7684\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u4e3a std::size_t \u3002 std::size_t \u88ab\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u5b83\u901a\u5e38\u88ab\u7528\u6765\u8868\u793a\u5bf9\u8c61\u7684\u5927\u5c0f\u6216\u957f\u5ea6\u3002 \u6709\u8da3\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 sizeof \u8fd0\u7b97\u7b26\uff08\u8fd4\u56de\u7c7b\u578b\u4e3a std:: size_t \uff09\u8fd4\u56de std:: size_t \u672c\u8eab\u7684\u957f\u5ea6\uff1a 1 2 3 4 5 6 7 8 9 #include // std::size_t #include int main () { std :: cout << sizeof ( std :: size_t ) << '\\n' ; return 0 ; } \u572832\u4f4d\uff084\u5b57\u8282\uff09\u7684\u63a7\u5236\u53f0\u7a0b\u5e8f\u4e2d\uff0c\u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 4 \u548c\u6574\u578b\u7684\u5927\u5c0f\u53ef\u4ee5\u6539\u53d8\u4e00\u6837\uff0c std::size_t \u540c\u6837\u53ef\u80fd\u4f1a\u6539\u53d8\u3002 std::size_t \u53ea\u4fdd\u8bc1\u5b83\u4e3a\u65e0\u7b26\u53f7\uff0c\u5e76\u4e14\u6700\u5c0f\u4e3a16\u4f4d\uff0c\u4f46\u662f\u5728\u5927\u591a\u6570\u7cfb\u7edf\u4e2d\uff0c\u5b83\u7684\u5b9e\u9645\u5927\u5c0f\u901a\u5e38\u7b49\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684[[address-width|\u5730\u5740\u5bbd\u5ea6(address-width)]]\u3002\u5373\u5bf9\u4e8e32\u4f4d\u5e94\u7528\u7a0b\u5e8f\uff0c std::size_t \u901a\u5e38\u4e3a 32 \u4f4d\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u800c\u5bf9\u4e8e 64 \u4f4d\u7a0b\u5e8f\uff0c size_t \u901a\u5e38\u4e3a64\u4f4d\u65e0\u7b26\u53f7\u6574\u578b\u3002 size_t \u88ab\u5b9a\u4e49\u4e3a\u8981\u8db3\u591f\u5927\u80fd\u4ee5\u4fbf\u80fd\u8868\u793a\u5bf9\u5e94\u7cfb\u7edf\u80fd\u591f\u521b\u5efa\u7684\u6700\u5927\u7684\u5bf9\u8c61\uff08\u4ee5\u5b57\u8282\u8ba1\uff09\u7684\u5c3a\u5bf8\u3002\u4f8b\u5982\uff0c\u5982\u679c std::size_t \u662f4\u4e2a\u5b57\u8282\u5bbd\uff0c\u5219\u8be5\u7cfb\u7edf\u80fd\u591f\u521b\u5efa\u7684\u6700\u5927\u7684\u5bf9\u8c61\u4e0d\u53ef\u80fd\u8d85\u8fc7 4,294,967,295 \u5b57\u8282\uff0c\u56e0\u4e3a\u8fd9\u662f4\u5b57\u8282\u65e0\u7b26\u53f7\u6574\u578b\u80fd\u591f\u5b58\u653e\u7684\u6700\u5927\u503c\u3002\u4e0d\u8fc7\uff0c\u8fd9\u53ea\u662f\u5bf9\u8c61\u5c3a\u5bf8\u7684\u7406\u8bba\u4e0a\u9650\uff0c\u5b9e\u9645\u4e0a\u9650\u53d6\u51b3\u4e8e\u4f60\u4f7f\u7528\u7684\u7f16\u8bd1\u5668\u3002 \u4ece\u5b9a\u4e49\u4e0a\u770b\uff0c\u4efb\u4f55\u5c3a\u5bf8\u5927\u4e8e size_t \u80fd\u591f\u8868\u793a\u7684\u503c\u7684\u5bf9\u8c61\uff0c\u90fd\u88ab\u770b\u505a\u662f\u9519\u8bef\u7684\uff08\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\uff09\uff0c\u56e0\u4e3a\u6b64\u65f6 size_t \u5df2\u7ecf\u4e0d\u80fd\u591f\u6b63\u786e\u8868\u793a\u8be5\u5bf9\u8c61\u7684\u5927\u5c0f\uff08\u4f1a\u53d1\u751f\u65e0\u7b26\u53f7\u6570\u7ffb\u8f6c\uff09\u3002 \u9898\u5916\u8bdd \u6709\u4e9b\u7f16\u8bd1\u5668\u5c06\u6700\u5927\u53ef\u521b\u5efa\u5bf9\u8c61\u7684\u5927\u5c0f\u9650\u5236\u4e3a std::size_t \u80fd\u591f\u8868\u793a\u7684\u6700\u5927\u503c(\u6709\u5174\u8da3\u53ef\u4ee5\u9605\u8bfb \u8fd9\u7bc7\u6587\u7ae0 ). \u5b9e\u9645\u4e0a\uff0c\u6700\u5927\u53ef\u521b\u5efa\u5bf9\u8c61\u7684\u5927\u5c0f\u53ef\u80fd\u4f1a\u5c0f\u4e8e\uff08\u8fdc\u5c0f\u4e8e\uff09\u8fd9\u4e2a\u503c\uff0c\u5b83\u53d6\u51b3\u4e8e\u4f60\u7684\u8ba1\u7b97\u673a\u6709\u591a\u5c11\u53ef\u7528\u7684\u8fde\u7eed\u5185\u5b58\u3002","title":"4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t"},{"location":"4-6-Fixed-width-integers-and-size_t/#_1","text":"\u7b80\u800c\u8a00\u4e4b\uff0c\u8fd9\u8fd8\u662f\u4ece C \u8bed\u8a00\u7ee7\u627f\u800c\u6765\u7684\u3002\u5728\u8ba1\u7b97\u673a\u975e\u5e38\u6162\u7684\u5e74\u4ee3\uff0c\u6027\u80fd\u662f\u6700\u9700\u8981\u88ab\u8003\u8651\u7684\u95ee\u9898\u3002C \u8bed\u8a00\u9009\u62e9\u6545\u610f\u4e0d\u53bb\u56fa\u5b9a\u6574\u578b\u7684\u5927\u5c0f\uff0c\u4ee5\u4fbf\u7f16\u8bd1\u5668\u7684\u5b9e\u73b0\u8005\u53ef\u4ee5\u4e3a\u76ee\u6807\u8ba1\u7b97\u673a\u9009\u62e9\u6700\u5408\u9002\u7684\u6574\u578b\u5927\u5c0f\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002","title":"\u4e3a\u4ec0\u4e48\u6574\u578b\u7684\u5927\u5c0f\u4e0d\u662f\u56fa\u5b9a\u7684\uff1f"},{"location":"4-6-Fixed-width-integers-and-size_t/#_2","text":"\u4ece\u73b0\u4ee3\u6807\u51c6\u7684\u89d2\u5ea6\u6765\u8bb2\uff0c\u662f\u7684\u3002\u4f5c\u4e3a\u7a0b\u5e8f\u5458\u8fd8\u8981\u5904\u7406\u7c7b\u578b\u8303\u56f4\u4e0d\u786e\u5b9a\u7684\u60c5\u51b5\u786e\u5b9e\u6709\u70b9\u8352\u5510\u3002 \u8003\u8651 int \u7c7b\u578b\uff0c\u5176\u5360\u7528\u5185\u5b58\u6700\u5c0f\u4e3a 2 \u5b57\u8282\uff0c\u4f46\u662f\u5728\u73b0\u4ee3\u8ba1\u7b97\u673a\u4e0a\u5374\u7ecf\u5e38\u4e3a 4 \u5b57\u8282\u3002\u5982\u679c\u4f60\u5047\u8bbe int \u662f 4 \u4e2a\u5b57\u8282\uff08\u786e\u5b9e 4 \u5b57\u8282\u60c5\u51b5\u6700\u6709\u53ef\u80fd\uff09\uff0c\u90a3\u4e48\u4f60\u7684\u7a0b\u5e8f\u5728 int \u5b9e\u9645\u53ea\u5360\u4e24\u4e2a\u5b57\u8282\u7684\u8ba1\u7b97\u673a\u4e0a\u5c31\u4f1a\u4ea7\u751f\u5f02\u5e38\u7684\u884c\u4e3a\uff08\u56e0\u4e3a\u4f60\u5f88\u53ef\u80fd\u628a\u4e00\u4e2a\u9700\u8981 4 \u5b57\u8282\u624d\u80fd\u5b58\u653e\u7684\u503c\u5b58\u653e\u5230\u4e86\u5b9e\u9645\u53ea\u6709 2 \u4e2a\u5b57\u8282\u7684\u53d8\u91cf\u4e2d\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u6ea2\u51fa\u6216 \u672a\u5b9a\u4e49\u884c\u4e3a \uff09\u3002\u800c\u5982\u679c\u4f60\u5047\u8bbe int \u53ea\u6709\u4e24\u4e2a\u5b57\u8282\u4ee5\u4fbf\u83b7\u5f97\u6700\u597d\u7684\u517c\u5bb9\u6027\uff0c\u90a3\u4e48\u5728 4 \u5b57\u8282\u7cfb\u7edf\u4e0a\u65e0\u7591\u4f1a\u6d6a\u8d39\u4e24\u4e2a\u5b57\u8282\uff0c\u8fd9\u4f1a\u4f7f\u5f97\u4f60\u7684\u5185\u5b58\u4f7f\u7528\u6210\u500d\u589e\u52a0\uff01","title":"\u5f88\u8ba8\u538c\u662f\u5427\uff1f"},{"location":"4-6-Fixed-width-integers-and-size_t/#_3","text":"\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cC99 \u5b9a\u4e49\u4e86\u4e00\u7ec4\u56fa\u5b9a\u5bbd\u5ea6\u6574\u5f62\uff08\u4f4d\u4e8e stdint.h \uff09\u6765\u786e\u4fdd\u6574\u578b\u5728\u4e0d\u540c\u7684\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u4e0b\u90fd\u5177\u6709\u76f8\u540c\u7684\u5927\u5c0f\u3002 \u540d\u79f0 \u7c7b\u578b \u8303\u56f4 \u5907\u6ce8 std::int8_t 1 byte signed -128 \u5230 127 \u5728\u5927\u591a\u6570\u7cfb\u7edf\u4e0a\u88ab\u770b\u505a\u6709\u7b26\u53f7\u5b57\u7b26\u5904\u7406\uff0c\u89c1\u4e0b\u9762\u5907\u6ce8\u3002 std::uint8_t 1 byte unsigned 0 \u5230 255 \u5728\u5927\u591a\u6570\u7cfb\u7edf\u4e0a\u88ab\u770b\u505a\u65e0\u7b26\u53f7\u5b57\u7b26\u5904\u7406\uff0c\u89c1\u4e0b\u9762\u5907\u6ce8 std::int16_t 2 byte signed -32,768 \u5230 32,767 std::uint16_t 2 byte unsigned 0 \u5230 65,535 std::int32_t 4 byte signed -2,147,483,648 \u5230 2,147,483,647 std::uint32_t 4 byte unsigned 0 \u5230 4,294,967,295 std::int64_t 8 byte signed -9,223,372,036,854,775,808 \u5230 9,223,372,036,854,775,807 std::uint64_t 8 byte unsigned 0 \u5230 18,446,744,073,709,551,615 C++ \u5728 C++11 \u4e2d\u5b98\u65b9\u5438\u7eb3\u4e86\u8fd9\u4e9b\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u3002\u901a\u8fc7\u5305\u542b \u5934\u6587\u4ef6\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\uff0c\u5b83\u4eec\u88ab\u5b9a\u4e49\u5728 std \u547d\u540d\u7a7a\u95f4\u4e2d\u3002 \u8bf7\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 #include // for fixed-width integers #include int main () { std :: int16_t i { 5 }; std :: cout << i ; return 0 ; } \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u901a\u5e38\u6765\u8bb2\u6709\u4e24\u4e2a\u7f3a\u9677\u3002 \u9996\u5148\uff0c\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u4e0d\u80fd\u4fdd\u8bc1\u5728\u6240\u6709\u7684\u4f53\u7cfb\u7ed3\u6784\u4e2d\u90fd\u88ab\u5b9a\u4e49\u4e86\u3002\u5b83\u53ea\u5b58\u5728\u4e8e, They only exist on systems where there are fundamental types matching their widths and following a certain binary representation.\u5982\u679c\u4f53\u7cfb\u7ed3\u6784\u4e0d\u652f\u6301\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\uff0c\u5219\u4f60\u7684\u7a0b\u5e8f\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\u3002\u4e0d\u8fc7\uff0c\u5927\u591a\u6570\u7684\u73b0\u4ee3\u4f53\u7cfb\u7ed3\u6784\u90fd\u4ee5 8/16/32/64-\u4f4d\u7684\u53d8\u91cf\u4e3a\u6807\u51c6\uff0c\u56e0\u6b64\u9664\u975e\u4f60\u9700\u8981\u5c06\u7a0b\u5e8f\u79fb\u690d\u5230\u67d0\u4e2a\u6781\u5176\u7279\u6b8a\u7684\u5927\u578b\u673a\u6216\u5d4c\u5165\u5f0f\u7cfb\u7edf\u4e0a\uff0c\u5426\u5219\u4e00\u822c\u6ca1\u6709\u95ee\u9898\u3002 \u5176\u6b21\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u4e86\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\uff0c\u5b83\u7684\u6027\u80fd\u76f8\u8f83\u4e8e\u4f7f\u7528\u66f4\u5bbd\u7684\u7c7b\u578b\u53ef\u80fd\u6216\u7a0d\u5dee\uff08\u5728\u540c\u6837\u7684\u4f53\u7cfb\u7ed3\u6784\u4e0b\uff09\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a\u5927\u5c0f\u786e\u5b9a\u4e3a 32 \u4f4d\u7684\u6574\u578b\uff0c\u4f60\u53ef\u80fd\u4f1a\u4f7f\u7528 std:: int32_t \uff0c\u4f46\u662f\u4f60\u7684 CPU \u53ef\u80fd\u5728\u5904\u7406 64 \u4f4d\u6574\u5f62\u65f6\u66f4\u5feb\u3002\u4e0d\u8fc7\uff0c\u5373\u4fbf CPU \u80fd\u591f\u66f4\u5feb\u7684\u5904\u7406\u67d0\u79cd\u7ed9\u5b9a\u7684\u7c7b\u578b\uff0c\u4e5f\u4e0d\u4e00\u5b9a\u610f\u5473\u7740\u7a0b\u5e8f\u7684\u8fd0\u884c\u901f\u5ea6\u66f4\u5feb\u2014\u2014\u73b0\u4ee3\u7a0b\u5e8f\u66f4\u591a\u53d7\u9650\u4e8e\u5185\u5b58\u4f7f\u7528\u800c\u4e0d\u662f CPU \u5904\u7406\u901f\u5ea6\u3002\u4f7f\u7528\u66f4\u5927\u7684\u5185\u5b58\u8db3\u8ff9(memory footprint)\u5e26\u6765\u7684\u6027\u80fd\u635f\u5931\u53ef\u80fd\u4f1a\u8d85\u8fc7 CPU \u5bf9\u5176\u8fdb\u884c\u7684\u52a0\u901f\u3002\u4e0d\u7ecf\u5b9e\u9645\u6d4b\u8bd5\u662f\u5f88\u96be\u8fdb\u884c\u5bf9\u6bd4\u7684\u3002","title":"\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b"},{"location":"4-6-Fixed-width-integers-and-size_t/#_4","text":"\u4e3a\u4e86\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\uff0cC++\u8fd8\u5b9a\u4e49\u4e86\u53e6\u5916\u4e24\u7ec4\u6574\u578b\uff0c\u5e76\u786e\u4fdd\u5b83\u4eec\u603b\u662f\u5177\u6709\u5b9a\u4e49\u3002 \u901f\u5ea6\u4f18\u5148\u7684\u7c7b\u7c7b\u578b( std::int_fast#_t \u548c std::uint_fast#_t ) \u63d0\u4f9b\u4e86\u6700\u5feb\u7684\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u540c\u65f6\u5176\u5bbd\u5ea6\u6700\u5c0f\u4e3a # \u4f4d ( # = 8\u300116\u300132 \u6216 64)\u3002\u4f8b\u5982\uff0c std::int_fast32_t \u53ef\u4ee5\u5b9a\u4e49\u5bbd\u5ea6\u6700\u5c0f\u4e3a 32 \u4f4d\u4e14\u901f\u5ea6\u6700\u5feb\u7684\u6709\u7b26\u53f7\u6574\u578b\u3002 \u5c3a\u5bf8\u4f18\u5148\u7684\u7c7b\u578b( std::int_least#_t \u548c std::uint_least#_t ) \u63d0\u4f9b\u4e86\u6700\u5c0f\u7684\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u5176\u5bbd\u5ea6\u4e3a # \u4f4d ( # = 8\u300116\u300132 \u6216 64)\u3002\u4f8b\u5982\uff0c std::uint_least32_t \u53ef\u4ee5\u5b9a\u4e49\u5bbd\u5ea6\u81f3\u5c11\u4e3a 32 \u4f4d\u7684\u6700\u5c0f\u7684\u65e0\u7b26\u53f7\u6574\u578b\u3002 \u4ee5\u4f5c\u8005\u4f7f\u7528\u7684 Visual Studio (32-bit \u63a7\u5236\u53f0\u7a0b\u5e8f)\u4e3a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // for fixed-width integers #include int main () { std :: cout << \"least 8: \" << sizeof ( std :: int_least8_t ) * 8 << \" bits \\n \" ; std :: cout << \"least 16: \" << sizeof ( std :: int_least16_t ) * 8 << \" bits \\n \" ; std :: cout << \"least 32: \" << sizeof ( std :: int_least32_t ) * 8 << \" bits \\n \" ; std :: cout << '\\n' ; std :: cout << \"fast 8: \" << sizeof ( std :: int_fast8_t ) * 8 << \" bits \\n \" ; std :: cout << \"fast 16: \" << sizeof ( std :: int_fast16_t ) * 8 << \" bits \\n \" ; std :: cout << \"fast 32: \" << sizeof ( std :: int_fast32_t ) * 8 << \" bits \\n \" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 least 8: 8 bits least 16: 16 bits least 32: 32 bits fast 8: 8 bits fast 16: 32 bits fast 32: 32 bits \u53ef\u4ee5\u770b\u51fa std::int_least16_t \u662f 16 \u4f4d\u7684\uff0c\u4f46\u662f std::int_fast16_t \u5b9e\u9645\u4e0a\u5374\u662f 32 \u4f4d\u3002\u8fd9\u662f\u56e0\u4e3a\u5728\u4f5c\u8005\u7684\u673a\u5668\u4e0a\uff0c32 \u4f4d\u6574\u578b\u7684\u5904\u7406\u901f\u5ea6\u8981\u6bd4 16 \u4f4d\u5757\u3002 \u7136\u800c\uff0c\u8fd9\u4e24\u79cd\u7c7b\u578b\u4e5f\u6709\u5176\u7f3a\u70b9\uff1a\u9996\u5148\uff0c\u4f7f\u7528\u5b83\u4eec\u7684\u7a0b\u5e8f\u5458\u5b9e\u9645\u4e0a\u5e76\u4e0d\u591a\uff0c\u4e0d\u719f\u6089\u7684\u60c5\u51b5\u4f7f\u7528\u5b83\u4eec\u4f1a\u9020\u6210\u9519\u8bef\u3002\u53e6\u5916\uff0c\u901f\u5ea6\u4f18\u5148\u7684\u7c7b\u578b\u540c\u6837\u4e5f\u4f1a\u5e26\u6765\u548c 4 \u6bd4\u7279\u5bbd\u6574\u578b\u4e00\u6837\u7684\u5185\u5b58\u6d6a\u8d39\u95ee\u9898\u3002\u66f4\u4e25\u91cd\u7684\u662f\uff0c\u56e0\u4e3a\u8fd9\u4e24\u79cd\u6574\u578b\u7c7b\u578b\u7684\u5927\u5c0f\u662f\u53d8\u5316\u7684\uff0c\u90a3\u4e48\u53ef\u80fd\u4f1a\u9020\u6210\u4f60\u7684\u7a0b\u5e8f\u5728\u4e0d\u540c\u4f53\u7cfb\u7ed3\u6784\u7684\u7535\u8111\u4e0a\u884c\u4e3a\u6709\u6240\u4e0d\u540c\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include // for fixed-width integers #include int main () { std :: uint_fast16_t sometype { 0 }; -- sometype ; // \u6545\u610f\u5229\u7528\u65e0\u7b26\u53f7\u6574\u578b\u53cd\u8f6c\u7684\u7279\u6027 std :: cout << sometype ; return 0 ; } \u4e0a\u8ff0\u4ee3\u7801\u7684\u8f93\u51fa\u7ed3\u679c\u53d6\u51b3\u4e8e std::uint_fast16_t \u662f 16 \u4f4d\u300132 \u4f4d\u8fd8\u662f 64 \u4f4d\u3002 \u9664\u975e\u80fd\u5728\u5bf9\u5e94\u7684\u5e73\u53f0\u4e0a\u8fdb\u884c\u6d4b\u8bd5\uff0c\u5426\u5219\u6211\u4eec\u5f88\u96be\u9884\u77e5\u7a0b\u5e8f\u5728\u4ec0\u4e48\u5e73\u53f0\u4e0a\u8fd0\u884c\u65f6\u4f1a\u51fa\u95ee\u9898\uff0c\u800c\u5bf9\u4e8e\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u6211\u4eec\u80fd\u591f\u4f7f\u7528\u7684\u5e73\u53f0\u79cd\u7c7b\u5e76\u4e0d\u4f1a\u5f88\u591a\u3002","title":"\u901f\u5ea6\u4f18\u5148\u6574\u578b\u548c\u5c3a\u5bf8\u4f18\u5148\u6574\u578b"},{"location":"4-6-Fixed-width-integers-and-size_t/#std-int8_t-std-uint8_t","text":"\u7531\u4e8e\u5bf9C++\u6807\u51c6\u7814\u7a76\u4e0d\u591f\u4ed4\u7ec6\uff0c\u5f88\u591a\u7f16\u8bd1\u5668\u5b9a\u4e49\u548c\u5bf9\u5f85 std::int8_t \u548c std::uint8_t \u7c7b\u578b(\u4ee5\u53ca\u5176\u5bf9\u5e94\u7684\u901f\u5ea6\u6216\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u7c7b\u578b) \u7684\u65b9\u5f0f\u548c signed char \u4e0e unsigned char \u662f\u5b8c\u5168\u4e00\u6837\u7684\u3002\u8fd9\u4e5f\u5c31\u610f\u5473\u77408\u4f4d\u7684\u7c7b\u578b\u53ef\u80fd\uff08\u4e5f\u53ef\u80fd\u4e0d\uff09\u4e0e\u5176\u4ed6\u56fa\u5b9a\u5bbd\u5ea6\u7c7b\u578b\u5177\u6709\u4e0d\u540c\u7684\u884c\u4e3a\uff0c\u8fd9\u6709\u53ef\u80fd\u9020\u6210\u9519\u8bef\u3002\u5177\u4f53\u7684\u884c\u4e3a\u548c\u7cfb\u7edf\u76f8\u5173\uff0c\u6240\u4ee5\u5728\u67d0\u4e2a\u5e73\u53f0\u4e0a\u6b63\u5e38\u5de5\u4f5c\u7684\u7a0b\u5e8f\u5728\u5176\u4ed6\u5e73\u53f0\u4e0a\u53ef\u80fd\u65e0\u6cd5\u5de5\u4f5c\u6216\u7f16\u8bd1\u3002 \u6211\u4eec\u4f1a\u5728 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast \u4e2d\u4e3e\u4f8b\u8bf4\u660e\u3002 \u4e3a\u4e86\u4fdd\u6301\u4e00\u81f4\u6027\uff0c\u6700\u597d\u80fd\u591f\u907f\u514d\u4f7f\u7528 _ std::int8_t \u548c std::uint8_t (\u4ee5\u53ca\u5176\u76f8\u5173\u7684\u901f\u5ea6\u6216\u5927\u5c0f\u4f18\u5148\u7c7b\u578b) \u3002\u53ef\u4ee5\u4f7f\u7528 std::int16_t \u6216 std::uint16_t \u4ee3\u66ff\u3002 \u6ce8\u610f \u907f\u514d\u4f7f\u75288\u4f4d\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u3002\u5982\u679c\u4e00\u5b9a\u8981\u4f7f\u7528\uff0c\u8bf7\u6ce8\u610f\u5b83\u4eec\u7684\u884c\u4e3a\u66f4\u7c7b\u4f3c\u4e8e\u5b57\u7b26\u3002","title":"std:: int8_t \u548c std:: uint8_t \u7684\u884c\u4e3a\u53ef\u80fd\u66f4\u50cf\u5b57\u7b26\u800c\u975e\u6574\u578b"},{"location":"4-6-Fixed-width-integers-and-size_t/#_5","text":"\u7531\u4e8e\u5404\u79cd\u57fa\u7840\u6574\u578b\u7c7b\u578b\u90fd\u5b58\u5728\u5404\u81ea\u7684\u4f18\u7f3a\u70b9\uff0c\u5982\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u7c7b\u578b\uff0c\u901f\u5ea6/\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u7c7b\u578b\u4ee5\u53ca\u6709\u7b26\u53f7\u65e0\u7b26\u53f7\u6574\u578b\u3002\u6211\u4eec\u9700\u8981\u5728\u4f7f\u7528\u65f6\u53c2\u8003\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u3002 \u6211\u4eec\u7684\u5b97\u65e8\u662f\uff0c\u6b63\u786e\u6027\u4f18\u4e8e\u901f\u5ea6\uff0c\u7f16\u8bd1\u65f6\u53d1\u73b0\u9519\u8bef\u4f18\u4e8e\u8fd0\u884c\u65f6\u53d1\u73b0\u9519\u8bef\u2014\u2014\u57fa\u4e8e\u8fd9\u4e2a\u6307\u5bfc\u539f\u5219\uff0c\u6211\u4eec\u5efa\u8bae\u907f\u514d\u4f7f\u7528\u901f\u5ea6/\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u7c7b\u578b\uff0c\u4f7f\u7528\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u7c7b\u578b\u3002\u5982\u679c\u5c06\u6765\u53d1\u73b0\u67d0\u4e2a\u5e73\u53f0\u4e0a\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u4e0d\u80fd\u7f16\u8bd1\uff0c\u90a3\u65f6\u4f60\u53ef\u4ee5\u518d\u51b3\u5b9a\u662f\u5426\u8981\u4fee\u6539\u4f60\u7684\u7a0b\u5e8f\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u6574\u578b\u7684\u5927\u5c0f\u4e0d\u91cd\u8981\uff08\u4f8b\u5982\u5b58\u653e\u7684\u6570\u603b\u662f\u57282\u5b57\u8282\u6709\u7b26\u53f7\u6574\u578b\u8303\u56f4\u5185\uff09\uff0c\u63a8\u8350\u4f7f\u7528 int \u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u8981\u6c42\u7528\u6237\u8f93\u5165\u5e74\u9f84\uff0c\u6216\u8005\u8fdb\u884c10\u4ee5\u5185\u7684\u8ba1\u6570\uff08\u6b64\u65f62\u5b57\u8282\u8303\u56f4\u603b\u662f\u591f\u7528\u7684\uff09\u3002\u90a3\u4e48\u4f7f\u7528 int \u5c31\u8db3\u591f\u5e94\u5bf9\u7edd\u5927\u591a\u6570\u573a\u666f\u4e86\uff1b \u5982\u679c\u9700\u8981\u5b58\u50a8\u6570\u91cf\u503c\uff0c\u4e14\u8303\u56f4\u5fc5\u987b\u660e\u786e\u65f6\uff0c\u63a8\u8350\u4f7f\u7528 std::int#_t \uff1b \u5982\u679c\u9700\u8981\u8fdb\u884c\u4f4d\u8fd0\u7b97\u6216\u60f3\u8981\u5229\u7528\u65e0\u7b26\u53f7\u6570\u7684\u7ffb\u8f6c\u7279\u6027\u65f6\uff0c\u4f7f\u7528 std::uint#_t \u3002 \u5c3d\u91cf\u907f\u514d\uff1a \u4f7f\u7528\u65e0\u7b26\u53f7\u7c7b\u578b\u4fdd\u5b58\u6570\u91cf\u503c\uff1b \u4f7f\u7528 8 \u4e3a\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\uff1b \u4f7f\u7528\u901f\u5ea6\u6216\u5927\u5c0f\u4f18\u5148\u7684\u56fa\u5b9a\u5bbd\u5ea6\u7c7b\u578b\uff1b \u4efb\u4f55\u4e0e\u7279\u5b9a\u7f16\u8bd1\u5668\u76f8\u5173\u7684\u56fa\u5b9a\u6574\u578b\u7c7b\u578b\u2014\u2014\u4f8b\u5982 Visual Studio \u5b9a\u4e49\u7684 __int8 \uff0c __int16 \u7b49\u3002","title":"\u6574\u578b\u7c7b\u578b\u9009\u62e9\u7684\u6700\u4f73\u5b9e\u8df5"},{"location":"4-6-Fixed-width-integers-and-size_t/#std-size_t","text":"\u8003\u8651\u5982\u4e0b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << sizeof ( int ) << '\\n' ; return 0 ; } \u5728\u4f5c\u8005\u7684\u673a\u5668\u4e0a\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 4 \u5f88\u7b80\u5355\u6ca1\u9519\u5427\uff1f\u6211\u4eec\u77e5\u9053 sizeof \u8fd0\u7b97\u7b26\u80af\u5b9a\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\u6570\uff0c\u4f46\u662f\u8fd9\u4e2a\u6574\u578b\u6570\u7a76\u7adf\u662f\u4ec0\u4e48\u7c7b\u578b\u5462\uff1f\u662f int \uff1f\u8fd8\u662f short \uff1f\u3002 \u5b9e\u9645\u4e0a sizeof \uff08\u4ee5\u53ca\u5176\u4ed6\u8fd4\u56de\u957f\u5ea6\u6216\u5927\u5c0f\u7684\u51fd\u6570\uff09\u8fd4\u56de\u7684\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u4e3a std::size_t \u3002 std::size_t \u88ab\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u5b83\u901a\u5e38\u88ab\u7528\u6765\u8868\u793a\u5bf9\u8c61\u7684\u5927\u5c0f\u6216\u957f\u5ea6\u3002 \u6709\u8da3\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 sizeof \u8fd0\u7b97\u7b26\uff08\u8fd4\u56de\u7c7b\u578b\u4e3a std:: size_t \uff09\u8fd4\u56de std:: size_t \u672c\u8eab\u7684\u957f\u5ea6\uff1a 1 2 3 4 5 6 7 8 9 #include // std::size_t #include int main () { std :: cout << sizeof ( std :: size_t ) << '\\n' ; return 0 ; } \u572832\u4f4d\uff084\u5b57\u8282\uff09\u7684\u63a7\u5236\u53f0\u7a0b\u5e8f\u4e2d\uff0c\u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 4 \u548c\u6574\u578b\u7684\u5927\u5c0f\u53ef\u4ee5\u6539\u53d8\u4e00\u6837\uff0c std::size_t \u540c\u6837\u53ef\u80fd\u4f1a\u6539\u53d8\u3002 std::size_t \u53ea\u4fdd\u8bc1\u5b83\u4e3a\u65e0\u7b26\u53f7\uff0c\u5e76\u4e14\u6700\u5c0f\u4e3a16\u4f4d\uff0c\u4f46\u662f\u5728\u5927\u591a\u6570\u7cfb\u7edf\u4e2d\uff0c\u5b83\u7684\u5b9e\u9645\u5927\u5c0f\u901a\u5e38\u7b49\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684[[address-width|\u5730\u5740\u5bbd\u5ea6(address-width)]]\u3002\u5373\u5bf9\u4e8e32\u4f4d\u5e94\u7528\u7a0b\u5e8f\uff0c std::size_t \u901a\u5e38\u4e3a 32 \u4f4d\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u800c\u5bf9\u4e8e 64 \u4f4d\u7a0b\u5e8f\uff0c size_t \u901a\u5e38\u4e3a64\u4f4d\u65e0\u7b26\u53f7\u6574\u578b\u3002 size_t \u88ab\u5b9a\u4e49\u4e3a\u8981\u8db3\u591f\u5927\u80fd\u4ee5\u4fbf\u80fd\u8868\u793a\u5bf9\u5e94\u7cfb\u7edf\u80fd\u591f\u521b\u5efa\u7684\u6700\u5927\u7684\u5bf9\u8c61\uff08\u4ee5\u5b57\u8282\u8ba1\uff09\u7684\u5c3a\u5bf8\u3002\u4f8b\u5982\uff0c\u5982\u679c std::size_t \u662f4\u4e2a\u5b57\u8282\u5bbd\uff0c\u5219\u8be5\u7cfb\u7edf\u80fd\u591f\u521b\u5efa\u7684\u6700\u5927\u7684\u5bf9\u8c61\u4e0d\u53ef\u80fd\u8d85\u8fc7 4,294,967,295 \u5b57\u8282\uff0c\u56e0\u4e3a\u8fd9\u662f4\u5b57\u8282\u65e0\u7b26\u53f7\u6574\u578b\u80fd\u591f\u5b58\u653e\u7684\u6700\u5927\u503c\u3002\u4e0d\u8fc7\uff0c\u8fd9\u53ea\u662f\u5bf9\u8c61\u5c3a\u5bf8\u7684\u7406\u8bba\u4e0a\u9650\uff0c\u5b9e\u9645\u4e0a\u9650\u53d6\u51b3\u4e8e\u4f60\u4f7f\u7528\u7684\u7f16\u8bd1\u5668\u3002 \u4ece\u5b9a\u4e49\u4e0a\u770b\uff0c\u4efb\u4f55\u5c3a\u5bf8\u5927\u4e8e size_t \u80fd\u591f\u8868\u793a\u7684\u503c\u7684\u5bf9\u8c61\uff0c\u90fd\u88ab\u770b\u505a\u662f\u9519\u8bef\u7684\uff08\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\uff09\uff0c\u56e0\u4e3a\u6b64\u65f6 size_t \u5df2\u7ecf\u4e0d\u80fd\u591f\u6b63\u786e\u8868\u793a\u8be5\u5bf9\u8c61\u7684\u5927\u5c0f\uff08\u4f1a\u53d1\u751f\u65e0\u7b26\u53f7\u6570\u7ffb\u8f6c\uff09\u3002 \u9898\u5916\u8bdd \u6709\u4e9b\u7f16\u8bd1\u5668\u5c06\u6700\u5927\u53ef\u521b\u5efa\u5bf9\u8c61\u7684\u5927\u5c0f\u9650\u5236\u4e3a std::size_t \u80fd\u591f\u8868\u793a\u7684\u6700\u5927\u503c(\u6709\u5174\u8da3\u53ef\u4ee5\u9605\u8bfb \u8fd9\u7bc7\u6587\u7ae0 ). \u5b9e\u9645\u4e0a\uff0c\u6700\u5927\u53ef\u521b\u5efa\u5bf9\u8c61\u7684\u5927\u5c0f\u53ef\u80fd\u4f1a\u5c0f\u4e8e\uff08\u8fdc\u5c0f\u4e8e\uff09\u8fd9\u4e2a\u503c\uff0c\u5b83\u53d6\u51b3\u4e8e\u4f60\u7684\u8ba1\u7b97\u673a\u6709\u591a\u5c11\u53ef\u7528\u7684\u8fde\u7eed\u5185\u5b58\u3002","title":"std:: size_t \u662f\u4ec0\u4e48\uff1f"},{"location":"4-7-Introduction-to-scientific-notation/","tags":["data type","scientific notation"],"text":"\u5728\u8c08\u8bba\u4e0b\u4e00\u4e2a\u8bdd\u9898\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u6765\u8ba8\u8bba\u4e00\u4e0b\u79d1\u5b66\u8ba1\u6570\u6cd5\u3002 \u79d1\u5b66\u8ba1\u6570\u6cd5 \u662f\u4e00\u79cd\u5c06\u5927\u6570\u8868\u793a\u4e3a\u7cbe\u7b80\u683c\u5f0f\u7684\u975e\u5e38\u6709\u7528\u7684\u65b9\u6cd5\u3002\u5c3d\u7ba1\u79d1\u5b66\u8ba1\u6570\u6cd5\u7b2c\u4e00\u773c\u770b\u4e0a\u53bb\u4f1a\u6709\u4e9b\u964c\u751f\uff0c\u7406\u89e3\u79d1\u5b66\u8ba1\u6570\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u6d6e\u70b9\u6570\u7684\u5de5\u4f5c\u539f\u7406\u53ca\u5176\u9650\u5236\u3002 \u79d1\u5b66\u8ba1\u6570\u6cd5\u7684\u8868\u793a\u5f62\u5f0f\u4e3a\uff1a \\(\u6709\u6548\u6570\u5b57 * 10^{\u6307\u6570}\\) \u3002\u4f8b\u5982 1.2 x 10\u2074 \u4e2d\uff0c 1.2 \u662f\u6709\u6548\u6570\u5b57\u800c4\u662f\u6307\u6570\u3002\u56e0\u4e3a10\u7684\u56db\u6b21\u65b9\u7b49\u4e8e10,000\uff0c\u6240\u4ee5 1.2 x 10\u2074 \u7b49\u4e8e12,000\u3002 \u6309\u7167\u60ef\u4f8b\uff0c\u79d1\u5b66\u8ba1\u6570\u6cd5\u7684\u6709\u6548\u6570\u5b57\u90e8\u5206\uff0c\u5c0f\u6570\u70b9\u524d\u5199\u4e00\u4e2a\u975e\u96f6\u6570\u5b57\uff0c\u5176\u4ed6\u975e\u96f6\u6570\u5b57\u5219\u5199\u5230\u5c0f\u6570\u70b9\u540e\u3002 \u4f8b\u5982\uff0c\u4ee5\u5341\u8fdb\u5236\u8868\u793a\u6cd5\u8868\u793a\u5730\u7403\u7684\u8d28\u91cf\u65f6\uff0c\u6211\u4eec\u4f1a\u5199\u6210 5973600000000000000000000 kg \u3002\u597d\u957f\u4e00\u4e32\u6570\u5b57\uff01\uff08\u800c\u4e14\u5927\u5230\u4e0d\u80fd\u88ab8\u5b57\u8282\u6574\u578b\u6240\u8868\u793a\uff09\u3002\u540c\u65f6\uff0c\u8bfb\u8d77\u6765\u4e5f\u5f88\u56f0\u96be\uff08\u5230\u5e95\u662f19\u4e2a0\u8fd8\u662f20\u4e2a0\uff1f\uff09\u3002\u5373\u4fbf\u4ee5\u9017\u53f7\u6765\u5206\u5272\u6210 5,973,600,000,000,000,000,000,000\uff0c\u770b\u4e0a\u53bb\u4ecd\u7136\u975e\u5e38\u5403\u529b\u3002 \u5982\u679c\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a\uff0c\u5219\u53ef\u4ee5\u5199\u4f5c 5.9736 x 10\u00b2\u2074 kg \uff0c\u770b\u4e0a\u53bb\u5c31\u975e\u5e38\u7b80\u6d01\u4e86\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u79d1\u5b66\u8ba1\u6570\u6cd5\u8fd8\u6709\u5229\u4e8e\u5bf9\u4e24\u4e2a\u975e\u5e38\u5927\u6216\u975e\u5e38\u5c0f\u7684\u503c\u6bd4\u8f83\u5176\u6570\u91cf\u7ea7\uff0c\u56e0\u4e3a\u53ea\u9700\u8981\u6bd4\u8f83\u6307\u6570\u90e8\u5206\u5373\u53ef\u3002 \u56e0\u4e3a\u5728C++\u4e2d\uff0c\u8f93\u5165\u548c\u663e\u793a\u6307\u6570\u8bb0\u53f7\u4f1a\u6bd4\u8f83\u56f0\u96be\uff0c\u6240\u4ee5\u6211\u4eec\u4f7f\u7528\u5b57\u6bcd e \uff08\u6709\u65f6\u4e5f\u4f7f\u7528\u5927\u5199\u5b57\u6bcd E \uff09\u6765\u8868\u793a10\u7684\u6b21\u65b9\u3002\u4f8b\u5982 1.2 x 10\u2074 \u4f1a\u5199\u4f5c 1.2e4 \uff0c\u800c 5.9736 x 10\u00b2\u2074 \u5219\u5199\u4f5c 5.9736e24 \u3002 \u5bf9\u4e8e\u5c0f\u4e8e1\u7684\u6570\uff0c\u6307\u6570\u90e8\u5206\u5219\u662f\u8d1f\u6570\uff0c\u4f8b\u5982 5e-2 \u7b49\u4ef7\u4e8e 5 * 10\u207b\u00b2 \uff0c\u5373 5 / 10\u00b2 \u6216 0.05 \u3002\u7535\u5b50\u7684\u8d28\u91cf\u53ef\u4ee5\u8868\u793a\u4e3a 9.1093822e-31 kg \u3002 \u5982\u4f55\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u79d1\u5b66\u8ba1\u6570\u6cd5 \u4f7f\u7528\u4e0b\u9762\u6b65\u9aa4\uff1a \u6307\u6570\u90e8\u5206\u4ece0\u5f00\u59cb\u3002 \u79fb\u52a8\u5c0f\u6570\u70b9\uff0c\u4f7f\u5176\u5de6\u8fb9\u53ea\u6709\u4e00\u4f4d\u6570\u3002 \u6bcf\u6b21\u5411\u5de6\u79fb\u52a8\uff0c\u6307\u6570+1 \u6bcf\u6b21\u5411\u53f3\u79fb\u52a8\uff0c\u6307\u6570-1 \u5220\u9664\u6709\u6548\u6570\u5b57\u5de6\u8fb9\u76840 \u5982\u679c\u539f\u6570\u5b57\u6ca1\u6709\u5c0f\u6570\u90e8\u5206\uff0c\u5220\u9664\u6709\u6548\u6570\u5b57\u672b\u5c3e\u76840\uff0c\u5982\u679c\u6ca1\u6709\u7279\u522b\u6307\u660e\uff0c\u6211\u4eec\u8ba4\u4e3a\u8fd9\u4e9b0\u6ca1\u6709\u610f\u4e49\u3002 \u4e00\u4e9b\u4f8b\u5b50\uff1a 1 2 3 4 \u8d77\u59cb\u503c: 42030 \u5c0f\u6570\u70b9\u5de6\u79fb4\u4f4d\uff1a4.2030e4 \u6709\u6548\u6570\u5b57\u5de6\u4fa7\u6ca1\u6709\u9700\u8981\u5220\u9664\u76840\uff1a4.2030e4 \u5220\u9664\u6709\u6548\u6570\u5b57\u672b\u5c3e\u76840\uff1a4.203e4 (4 \u4f4d\u6709\u6548\u6570\u5b57) 1 2 3 4 \u8d77\u59cb\u503c: 0.0078900 \u5c0f\u6570\u70b9\u53f3\u79fb3\u4f4d: 0007.8900e-3 \u5220\u9664\u6709\u6548\u6570\u5b57\u5de6\u4fa7\u76840: 7.8900e-3 \u4e0d\u9700\u8981\u5220\u9664\u6709\u6548\u6570\u5b57\u672b\u5c3e\u76840: 7.8900e-3 (5 \u4f4d\u6709\u6548\u6570\u5b57) 1 2 3 4 \u8d77\u59cb\u503c: 600.410 \u5c0f\u6570\u70b9\u5de6\u79fb2\u4f4d: 6.00410e2 \u6709\u6548\u6570\u5b57\u5de6\u4fa7\u6ca1\u6709\u9700\u8981\u5220\u9664\u76840: 6.00410e2 \u4e0d\u9700\u8981\u5220\u9664\u6709\u6548\u6570\u5b57\u672b\u5c3e\u76840: 6.00410e2 (6 \u4f4d\u6709\u6548\u6570\u5b57) \u91cd\u70b9\u7406\u89e3\uff1a\u6709\u6548\u6570\u5b57\u90e8\u5206\u7684\u4f4d\u6570 (\u2018e\u2019\u524d\u9762\u7684\u90e8\u5206) \u79f0\u4e3a\u6709\u6548\u6570\u5b57 \u3002\u6709\u6548\u6570\u5b57\u51b3\u5b9a\u4e86\u8fd9\u4e2a\u6570\u7684 \u7cbe\u5ea6 \uff0c\u6709\u6548\u6570\u5b57\u4f4d\u6570\u8d8a\u591a\u8bf4\u660e\u7cbe\u5ea6\u8d8a\u9ad8\u3002 \u7cbe\u5ea6\u4ee5\u53ca\u5c0f\u6570\u70b9\u540e\u76840 \u60f3\u8c61\u4e00\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u5c31\u540c\u4e00\u4e2a\u82f9\u679c\u7684\u91cd\u91cf\u8be2\u95ee\u4e24\u4e2a\u5b9e\u9a8c\u5ba4\u52a9\u624b\uff0c\u4e00\u4e2a\u56de\u7b54\u82f9\u679c\u91cd\u4e3a87\u514b\uff0c\u53e6\u4e00\u4e2a\u56de\u7b54\u4e3a87.00\u514b\uff0c\u5047\u8bbe\u5b83\u4eec\u7684\u7ed3\u679c\u90fd\u662f\u5bf9\u7684\uff0c\u5bf9\u4e8e\u524d\u4e00\u4e2a\u56de\u7b54\u6765\u8bf4\uff0c\u82f9\u679c\u7684\u771f\u5b9e\u91cd\u91cf\u53ef\u80fd\u4f4d\u4e8e86.50 \u548c 87.49 \u514b\u4e4b\u95f4\u3002\u4e5f\u8bb8\u4ed6\u4f7f\u7528\u7684\u5929\u5e73\u7cbe\u5ea6\u53ea\u5230\u514b\uff0c\u4e5f\u8bb8\u662f\u4ed6\u8fdb\u884c\u4e86\u56db\u820d\u4e94\u5165\u3002\u800c\u5bf9\u4e8e\u540e\u4e00\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u5176\u7cbe\u5ea6\u66f4\u52a0\u81ea\u4fe1(\u91cd\u91cf\u5728 86.9950 \u548c 87.0049 \u514b\u4e4b\u95f4\uff0c\u53d8\u5316\u533a\u95f4\u66f4\u5c0f\u4e86)\u3002 \u6240\u4ee5\uff0c\u5728\u6807\u51c6\u7684\u79d1\u5b66\u8ba1\u6570\u6cd5\u4e2d\uff0c\u6211\u4eec\u503e\u5411\u4e8e\u4fdd\u7559\u5c0f\u6570\u70b9\u540e\u9762\u5c3e\u90e8\u76840\uff0c\u56e0\u4e3a\u8fd9\u4e9b0\u53ef\u4ee5\u6709\u6548\u5730\u8868\u660e\u8be5\u6570\u503c\u7684\u7cbe\u5ea6\u3002 \u4e0d\u8fc7\uff0c\u5728C++\u4e2d\uff0c87 \u548c 87.000 \u5e76\u6ca1\u6709\u4efb\u4f55\u533a\u522b\uff0c\u7f16\u8bd1\u5668\u5b58\u50a8\u4e24\u6570\u7684\u7ed3\u679c\u4e5f\u662f\u5b8c\u5168\u4e00\u6837\u7684\u3002\u4ece\u6280\u672f\u4e0a\u8bb2\uff0c\u4f7f\u7528\u4e24\u79cd\u5199\u6cd5\u6ca1\u6709\u4efb\u4f55\u533a\u522b\uff08\u5f53\u7136\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u4ee3\u7801\u5373\u6587\u6863\u7684\u8868\u793a\u65b9\u5f0f\uff0c\u4e5f\u8bb8\u4f1a\u6709\u51fa\u4e8e\u79d1\u5b66\u4e0a\u7684\u7406\u7531\uff09\u3002 \u4ecb\u7ecd\u5b8c\u79d1\u5b66\u8ba1\u6570\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb\u4ecb\u7ecd\u6d6e\u70b9\u6570\u4e86\u3002","title":"4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5"},{"location":"4-7-Introduction-to-scientific-notation/#_1","text":"\u4f7f\u7528\u4e0b\u9762\u6b65\u9aa4\uff1a \u6307\u6570\u90e8\u5206\u4ece0\u5f00\u59cb\u3002 \u79fb\u52a8\u5c0f\u6570\u70b9\uff0c\u4f7f\u5176\u5de6\u8fb9\u53ea\u6709\u4e00\u4f4d\u6570\u3002 \u6bcf\u6b21\u5411\u5de6\u79fb\u52a8\uff0c\u6307\u6570+1 \u6bcf\u6b21\u5411\u53f3\u79fb\u52a8\uff0c\u6307\u6570-1 \u5220\u9664\u6709\u6548\u6570\u5b57\u5de6\u8fb9\u76840 \u5982\u679c\u539f\u6570\u5b57\u6ca1\u6709\u5c0f\u6570\u90e8\u5206\uff0c\u5220\u9664\u6709\u6548\u6570\u5b57\u672b\u5c3e\u76840\uff0c\u5982\u679c\u6ca1\u6709\u7279\u522b\u6307\u660e\uff0c\u6211\u4eec\u8ba4\u4e3a\u8fd9\u4e9b0\u6ca1\u6709\u610f\u4e49\u3002 \u4e00\u4e9b\u4f8b\u5b50\uff1a 1 2 3 4 \u8d77\u59cb\u503c: 42030 \u5c0f\u6570\u70b9\u5de6\u79fb4\u4f4d\uff1a4.2030e4 \u6709\u6548\u6570\u5b57\u5de6\u4fa7\u6ca1\u6709\u9700\u8981\u5220\u9664\u76840\uff1a4.2030e4 \u5220\u9664\u6709\u6548\u6570\u5b57\u672b\u5c3e\u76840\uff1a4.203e4 (4 \u4f4d\u6709\u6548\u6570\u5b57) 1 2 3 4 \u8d77\u59cb\u503c: 0.0078900 \u5c0f\u6570\u70b9\u53f3\u79fb3\u4f4d: 0007.8900e-3 \u5220\u9664\u6709\u6548\u6570\u5b57\u5de6\u4fa7\u76840: 7.8900e-3 \u4e0d\u9700\u8981\u5220\u9664\u6709\u6548\u6570\u5b57\u672b\u5c3e\u76840: 7.8900e-3 (5 \u4f4d\u6709\u6548\u6570\u5b57) 1 2 3 4 \u8d77\u59cb\u503c: 600.410 \u5c0f\u6570\u70b9\u5de6\u79fb2\u4f4d: 6.00410e2 \u6709\u6548\u6570\u5b57\u5de6\u4fa7\u6ca1\u6709\u9700\u8981\u5220\u9664\u76840: 6.00410e2 \u4e0d\u9700\u8981\u5220\u9664\u6709\u6548\u6570\u5b57\u672b\u5c3e\u76840: 6.00410e2 (6 \u4f4d\u6709\u6548\u6570\u5b57) \u91cd\u70b9\u7406\u89e3\uff1a\u6709\u6548\u6570\u5b57\u90e8\u5206\u7684\u4f4d\u6570 (\u2018e\u2019\u524d\u9762\u7684\u90e8\u5206) \u79f0\u4e3a\u6709\u6548\u6570\u5b57 \u3002\u6709\u6548\u6570\u5b57\u51b3\u5b9a\u4e86\u8fd9\u4e2a\u6570\u7684 \u7cbe\u5ea6 \uff0c\u6709\u6548\u6570\u5b57\u4f4d\u6570\u8d8a\u591a\u8bf4\u660e\u7cbe\u5ea6\u8d8a\u9ad8\u3002","title":"\u5982\u4f55\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u79d1\u5b66\u8ba1\u6570\u6cd5"},{"location":"4-7-Introduction-to-scientific-notation/#0","text":"\u60f3\u8c61\u4e00\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u5c31\u540c\u4e00\u4e2a\u82f9\u679c\u7684\u91cd\u91cf\u8be2\u95ee\u4e24\u4e2a\u5b9e\u9a8c\u5ba4\u52a9\u624b\uff0c\u4e00\u4e2a\u56de\u7b54\u82f9\u679c\u91cd\u4e3a87\u514b\uff0c\u53e6\u4e00\u4e2a\u56de\u7b54\u4e3a87.00\u514b\uff0c\u5047\u8bbe\u5b83\u4eec\u7684\u7ed3\u679c\u90fd\u662f\u5bf9\u7684\uff0c\u5bf9\u4e8e\u524d\u4e00\u4e2a\u56de\u7b54\u6765\u8bf4\uff0c\u82f9\u679c\u7684\u771f\u5b9e\u91cd\u91cf\u53ef\u80fd\u4f4d\u4e8e86.50 \u548c 87.49 \u514b\u4e4b\u95f4\u3002\u4e5f\u8bb8\u4ed6\u4f7f\u7528\u7684\u5929\u5e73\u7cbe\u5ea6\u53ea\u5230\u514b\uff0c\u4e5f\u8bb8\u662f\u4ed6\u8fdb\u884c\u4e86\u56db\u820d\u4e94\u5165\u3002\u800c\u5bf9\u4e8e\u540e\u4e00\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u5176\u7cbe\u5ea6\u66f4\u52a0\u81ea\u4fe1(\u91cd\u91cf\u5728 86.9950 \u548c 87.0049 \u514b\u4e4b\u95f4\uff0c\u53d8\u5316\u533a\u95f4\u66f4\u5c0f\u4e86)\u3002 \u6240\u4ee5\uff0c\u5728\u6807\u51c6\u7684\u79d1\u5b66\u8ba1\u6570\u6cd5\u4e2d\uff0c\u6211\u4eec\u503e\u5411\u4e8e\u4fdd\u7559\u5c0f\u6570\u70b9\u540e\u9762\u5c3e\u90e8\u76840\uff0c\u56e0\u4e3a\u8fd9\u4e9b0\u53ef\u4ee5\u6709\u6548\u5730\u8868\u660e\u8be5\u6570\u503c\u7684\u7cbe\u5ea6\u3002 \u4e0d\u8fc7\uff0c\u5728C++\u4e2d\uff0c87 \u548c 87.000 \u5e76\u6ca1\u6709\u4efb\u4f55\u533a\u522b\uff0c\u7f16\u8bd1\u5668\u5b58\u50a8\u4e24\u6570\u7684\u7ed3\u679c\u4e5f\u662f\u5b8c\u5168\u4e00\u6837\u7684\u3002\u4ece\u6280\u672f\u4e0a\u8bb2\uff0c\u4f7f\u7528\u4e24\u79cd\u5199\u6cd5\u6ca1\u6709\u4efb\u4f55\u533a\u522b\uff08\u5f53\u7136\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u4ee3\u7801\u5373\u6587\u6863\u7684\u8868\u793a\u65b9\u5f0f\uff0c\u4e5f\u8bb8\u4f1a\u6709\u51fa\u4e8e\u79d1\u5b66\u4e0a\u7684\u7406\u7531\uff09\u3002 \u4ecb\u7ecd\u5b8c\u79d1\u5b66\u8ba1\u6570\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb\u4ecb\u7ecd\u6d6e\u70b9\u6570\u4e86\u3002","title":"\u7cbe\u5ea6\u4ee5\u53ca\u5c0f\u6570\u70b9\u540e\u76840"},{"location":"4-8-Floating-point-numbers/","tags":["data type","float"],"text":"Key Takeaway \u6d6e\u70b9\u6570\u6570\u636e\u7c7b\u578b\u6709\u4e09\u79cd\uff1a float (4 bytes), double (8 byte) \u548c long double (8\u300112\u300116byte) \u6d6e\u70b9\u6570\u6570\u636e\u7c7b\u578b\u59cb\u7ec8\u662f\u6709\u7b26\u53f7\u7684 \u5728\u4f7f\u7528\u6d6e\u70b9\u6570\u5b57\u9762\u91cf\u65f6\uff0c\u8bf7\u59cb\u7ec8\u4fdd\u7559\u4e00\u4f4d\u5c0f\u6570 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\u6d6e\u70b9\u6570\u5b57\u9762\u91cf\u4e3a double \u3002\u4f7f\u7528 f \u540e\u7f00\u53ef\u4ee5\u6807\u6ce8\u8be5\u5b57\u9762\u91cf\u4e3a float \u8bf7\u786e\u4fdd\u4f60\u4f7f\u7528\u7684\u5b57\u9762\u91cf\u548c\u5b83\u8d4b\u503c\u7684\u7c7b\u578b\u662f\u5339\u914d\u7684\u3002\u5426\u5219\u4f1a\u53d1\u751f\u4e0d\u5fc5\u8981\u7684\u8f6c\u6362\uff0c\u5bfc\u81f4\u7cbe\u5ea6\u4e22\u5931 \u5982\u679c\u5185\u5b58\u7a7a\u95f4\u5141\u8bb8\uff0c\u5c3d\u91cf\u4f7f\u7528 double \u800c\u4e0d\u662f float\uff0c\u56e0\u4e3a float \u7684\u7cbe\u5ea6\u66f4\u4f4e\uff0c\u4e5f\u5c31\u6709\u53ef\u80fd\u9020\u6210\u6570\u503c\u4e0d\u51c6\u786e\u7684\u95ee\u9898 \u820d\u5165\u8bef\u5dee\u59cb\u7ec8\u5b58\u5728\uff0c\u5b83\u662f\u89c4\u5219\u7684\u4e00\u90e8\u5206\uff0c\u6c38\u8fdc\u4e0d\u80fd\u5047\u5b9a\u6d6e\u70b9\u6570\u662f\u7cbe\u786e\u7684 \u6574\u578b\u53ef\u4ee5\u5f88\u597d\u5730\u8868\u793a\u6574\u6570\uff0c\u4f46\u662f\u5f88\u591a\u65f6\u5019\u6211\u4eec\u9700\u8981\u4fdd\u5b58\u975e\u5e38\u5927\u7684\u6570\uff0c\u6216\u8005\u5c0f\u6570\u3002 \u6d6e\u70b9\u6570 \u7c7b\u578b\u7684\u53d8\u91cf\u53ef\u4ee5\u7528\u6765\u5b58\u653e\u5b9e\u6570\uff0c\u4f8b\u59824320.0\u3001-3.33 \u6216 0.01226\u3002\u6d6e\u70b9\u6570\u540d\u5b57\u4e2d\u7684 \u6d6e\u70b9 \u4e8c\u5b57\uff0c\u5f62\u8c61\u5730\u8bf4\u660e\u4e86\u5c0f\u6570\u70b9\u53ef\u4ee5\u6d6e\u52a8\uff1b\u4e5f\u5c31\u662f\u8bf4\u5b83\u53ef\u4ee5\u652f\u6301\u5c0f\u6570\u70b9\u524d\u548c\u5c0f\u6570\u70b9\u540e\u5177\u6709\u4e0d\u540c\u4f4d\u7684\u6570\u5b57\u3002 \u6d6e\u70b9\u6570\u6570\u636e\u7c7b\u578b\u6709\u4e09\u79cd\uff1a float , double \u548c long double \u3002\u548c\u6574\u578b\u4e00\u6837 C++ \u5e76\u6ca1\u6709\u5b9a\u4e49\u8fd9\u4e09\u79cd\u7c7b\u578b\u7684\u5177\u4f53\u957f\u5ea6\uff08\u53ea\u786e\u4fdd\u5176\u6700\u5c0f\u503c\uff09\u3002\u5728\u73b0\u4ee3\u8ba1\u7b97\u673a\u4e0a\uff0c\u6d6e\u70b9\u6570\u7684\u8868\u793a\u65b9\u6cd5\u51e0\u4e4e\u603b\u662f\u9075\u5faa IEEE 754 \u4e8c\u8fdb\u5236\u683c\u5f0f\u3002\u5728\u8fd9\u79cd\u683c\u5f0f\u4e0b\uff0c float \u4e3a4\u4e2a\u5b57\u8282\uff0c double \u5219\u662f 8 \u4e2a\u5b57\u8282\uff0c\u800c long double \u53ef\u4ee5\u548c double \u76f8\u540c\uff088\u5b57\u8282\uff09\uff0c\u4e5f\u53ef\u80fd\u4e3a80\u4f4d\uff08\u901a\u5e38\u4f1a\u8865\u4f4d\u523012\u5b57\u8282\uff09\u6216\u800516\u5b57\u8282\u3002 \u6d6e\u70b9\u6570\u6570\u636e\u7c7b\u578b\u59cb\u7ec8\u662f\u6709\u7b26\u53f7\u7684\uff08\u53ef\u4ee5\u4fdd\u5b58\u6b63\u8d1f\u6570\uff09\u3002 Category Type Minimum Size Typical Size floating point float 4 bytes 4 bytes double 8 bytes 8 bytes long double 8 bytes 8, 12, or 16 bytes \u4e0b\u9762\u662f\u4e00\u4e9b\u6d6e\u70b9\u6570\u7684\u4f8b\u5b50\uff1a 1 2 3 float fValue ; double dValue ; long double ldValue ; \u5728\u4f7f\u7528\u6d6e\u70b9\u6570 \u5b57\u9762\u91cf \u65f6\uff0c\u8bf7\u59cb\u7ec8\u4fdd\u7559\u4e00\u4f4d\u5c0f\u6570\uff08\u5373\u4f7f\u8fd9\u4e00\u4f4d\u662f0\uff09\u3002\u8fd9\u53ef\u4ee5\u5e2e\u52a9\u7f16\u8bd1\u5668\u660e\u786e\u8be5\u6570\u503c\u4e3a\u6d6e\u70b9\u6570\u800c\u975e\u6574\u578b\u6570\u3002 1 2 3 int x { 5 }; // 5 \u8868\u793a\u6574\u578b\u6570 double y { 5.0 }; // 5.0 \u662f\u6d6e\u70b9\u6570\u5b57\u9762\u91cf (\u6ca1\u6709\u540e\u7f00\u7684\u60c5\u51b5\u4e0b\u9ed8\u8ba4\u4e3a double) float z { 5.0f }; // 5.0 \u662f\u6d6e\u70b9\u6570\u5b57\u9762\u91cf\uff0cf \u540e\u7f00\u8868\u793a float \u6ce8\u610f\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u6d6e\u70b9\u6570\u5b57\u9762\u91cf\u4e3a double \u3002\u4f7f\u7528 f \u540e\u7f00\u53ef\u4ee5\u6807\u6ce8\u8be5\u5b57\u9762\u91cf\u4e3a float \u6700\u4f73\u5b9e\u8df5 \u8bf7\u786e\u4fdd\u4f60\u4f7f\u7528\u7684\u5b57\u9762\u91cf\u548c\u5b83\u8d4b\u503c\u7684\u7c7b\u578b\u662f\u5339\u914d\u7684\u3002\u5426\u5219\u4f1a\u53d1\u751f\u4e0d\u5fc5\u8981\u7684\u8f6c\u6362\uff0c\u5bfc\u81f4\u7cbe\u5ea6\u4e22\u5931\u3002 \u6ce8\u610f \u8bf7\u786e\u4fdd\u5728\u5e94\u5f53\u4f7f\u7528\u6d6e\u70b9\u6570\u5b57\u9762\u91cf\u65f6\uff0c\u4e0d\u8981\u8bef\u7528\u6574\u578b\u5b57\u9762\u91cf\u3002\u8fd9\u53ef\u80fd\u4f1a\u53d1\u751f\u5bf9\u6d6e\u70b9\u7c7b\u578b\u5bf9\u8c61\u521d\u59cb\u5316\u3001\u8d4b\u503c\u3001\u6570\u5b66\u8fd0\u7b97\u4ee5\u53ca\u8c03\u7528\u8fd4\u56de\u503c\u5e94\u5f53\u4e3a\u6d6e\u70b9\u6570\u7684\u51fd\u6570\u65f6\u3002 \u6253\u5370\u6d6e\u70b9\u6570 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { std :: cout << 5.0 << '\\n' ; std :: cout << 6.7f << '\\n' ; std :: cout << 9876543.21 << '\\n' ; return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u53ef\u80fd\u4f1a\u8ba9\u4f60\u611f\u5230\u610f\u5916\uff1a 1 2 3 5 6.7 9.87654e+06 \u5728\u7b2c\u4e00\u6761\u6253\u5370\u4e2d\uff0c std::cout \u6253\u5370 5\uff0c\u5373\u4f7f\u6211\u4eec\u8f93\u5165\u7684\u662f5.0\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b std::cout \u4e0d\u4f1a\u8f93\u5165\u5c0f\u6570\u90e8\u5206\u76840\u3002 \u5728\u7b2c\u4e8c\u6761\u6253\u5370\u4e2d\uff0c\u7ed3\u679c\u548c\u6211\u4eec\u671f\u671b\u7684\u662f\u4e00\u6837\u3002 \u5728\u7b2c\u4e09\u6761\u6253\u5370\u4e2d\uff0c\u6253\u5370\u7ed3\u679c\u4e3a\u79d1\u5b66\u8ba1\u6570\u6cd5\uff08\u5982\u679c\u4f60\u9700\u8981\u590d\u4e60\u4e00\u4e0b\u79d1\u5b66\u8ba1\u6570\u6cd5\u53ef\u4ee5\u53c2\u8003 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 \uff09\u3002 \u6d6e\u70b9\u6570\u7684\u8303\u56f4 \u5047\u5b9a\u4f7f\u7528 IEEE 754 \u8868\u793a\u6cd5\uff1a \u5927\u5c0f \u8303\u56f4 \u7cbe\u5ea6 4 bytes \u00b11.18 x 10-38 \u5230 \u00b13.4 x 1038 6-9 \u4f4d\u6709\u6548\u6570\u5b57\uff0c\u4e00\u822c\u4e3a7 8 bytes \u00b12.23 x 10-308 \u5230 \u00b11.80 x 10308 15-18 \u4f4d\u6709\u6548\u6570\u5b57\uff0c\u4e00\u822c\u4e3a16 80-bits (typically uses 12 \u6216 16 bytes) \u00b13.36 x 10-4932 \u5230 \u00b11.18 x 104932 18-21 \u4f4d\u6709\u6548\u6570\u5b57 16 bytes \u00b13.36 x 10-4932 \u5230 \u00b11.18 x 104932 33-36 \u4f4d\u6709\u6548\u6570\u5b57 80\u4f4d\u7684\u6d6e\u70b9\u6570\u7c7b\u578b\u53ef\u4ee5\u770b\u505a\u662f\u5386\u53f2\u9057\u7559\u95ee\u9898\u3002\u5728\u73b0\u4ee3\u5904\u7406\u5668\u4e0a\uff0c\u5b83\u901a\u5e38\u88ab\u5b9e\u73b0\u4e3a12\u5b57\u8282\u621616\u5b57\u8282\uff08\u5bf9\u4e8e\u5904\u7406\u5668\u6765\u8bf4\u662f\u66f4\u52a0\u65b9\u4fbf\u7684\u957f\u5ea6\uff09\u3002 \u4f60\u53ef\u80fd\u4f1a\u5947\u602a\u4e3a\u4ec0\u4e4880\u4f4d\u7684\u6d6e\u70b9\u6570\u7c7b\u578b\u548c16\u5b57\u8282\u7684\u6d6e\u70b9\u6570\u7c7b\u578b\u5177\u6709\u76f8\u540c\u7684\u8303\u56f4\u3002\u8fd9\u662f\u56e0\u4e3a\u5b83\u4eec\u4e2d\u4e13\u95e8\u7528\u6765\u8868\u793a\u6307\u6570\u7684\u4f4d\u662f\u76f8\u540c\u7684\u2014\u2014\u4e0d\u8fc7\uff0c16\u5b57\u8282\u7684\u6d6e\u70b9\u6570\u53ef\u4ee5\u8868\u793a\u66f4\u591a\u7684\u6709\u6548\u6570\u5b57\u3002 \u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6 \u8003\u8651\u4ee5\u4e0b\u5206\u6570 1/3\u3002\u5b83\u7684\u5341\u8fdb\u5236\u8868\u793a\u6cd5\u4e3a 0.33333333333333\u2026 \u65e0\u9650\u5faa\u73af3\u3002\u5982\u679c\u4f60\u5728\u7eb8\u4e0a\u4e00\u76f4\u5199\u7684\u8bdd\uff0c\u5199\u7740\u5199\u7740\u4f60\u5c31\u53d7\u4e0d\u4e86\u4e86\u3002\u6700\u7ec8\u4f60\u5199\u4e0b\u6765\u7684\u53ef\u80fd\u662f 0.3333333333\u2026. (\u65e0\u9650\u5faa\u73af3)\uff0c\u4f46\u7edd\u5bf9\u4e0d\u53ef\u80fd\u662f\u5168\u90e8\u7684\u503c\u3002 \u5bf9\u4e8e\u8ba1\u7b97\u673a\u6765\u8bf4\uff0c\u65e0\u9650\u957f\u5ea6\u7684\u6570\u5b57\u9700\u8981\u65e0\u9650\u5927\u7684\u5185\u5b58\u624d\u80fd\u5b58\u50a8\uff0c\u800c\u901a\u5e38\u6211\u4eec\u53ea\u80fd\u4f7f\u75284\u5b57\u8282\u62168\u5b57\u8282\u7684\u7a7a\u95f4\u3002\u6709\u9650\u7684\u5185\u5b58\u5c31\u610f\u5473\u7740\u6d6e\u70b9\u6570\u53ea\u80fd\u5b58\u653e\u7279\u5b9a\u957f\u5ea6\u7684\u6709\u6548\u6570\u5b57\u2014\u2014\u5176\u4ed6\u5269\u4f59\u90e8\u5206\u5c31\u88ab\u4e22\u5f03\u4e86\u3002\u6700\u7ec8\u88ab\u5b58\u50a8\u4e0b\u6765\u7684\u6570\u5b57\u662f\u6211\u4eec\u80fd\u591f\u63a5\u53d7\u7684\u503c\uff0c\u800c\u4e0d\u662f\u5b9e\u9645\u503c\u3002 \u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u88ab\u5b9a\u4e49\u4e3a\uff1a\u5728\u4e0d\u635f\u5931\u4fe1\u606f\u7684\u60c5\u51b5\u4e0b\u80fd\u591f\u8868\u793a\u7684\u6700\u591a\u7684\u6709\u6548\u6570\u5b57\u4f4d\u6570\u3002 \u5728\u8f93\u51fa\u6d6e\u70b9\u6570\u65f6\uff0c std::cout \u7684\u9ed8\u8ba4\u7cbe\u5ea6\u4e3a6\u2014\u2014\u4e5f\u5c31\u8bf4\u5b83\u5047\u8bbe\u6240\u6709\u6d6e\u70b9\u6570\u90fd\u53ea\u67096\u4f4d\u6709\u6548\u6570\u5b57\uff0c\u8d85\u8fc7\u7684\u90e8\u5206\u90fd\u4f1a\u88ab\u622a\u65ad\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u5bf9 std::cout \u622a\u65ad\u52306\u4f4d\u6709\u6548\u6570\u5b57\u8fdb\u884c\u4e86\u6f14\u793a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { std :: cout << 9.87654321f << '\\n' ; std :: cout << 987.654321f << '\\n' ; std :: cout << 987654.321f << '\\n' ; std :: cout << 9876543.21f << '\\n' ; std :: cout << 0.0000987654321f << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 3 4 5 9.87654 987.654 987654 9.87654e+006 9.87654e-005 \u6211\u4eec\u6ce8\u610f\u5230\uff0c\u8f93\u51fa\u7ed3\u679c\u4e2d\u7684\u6570\u90fd\u53ea\u67096\u4f4d\u6709\u6548\u6570\u5b57\u3002 \u540c\u65f6\u6211\u4eec\u6ce8\u610f\u5230\uff0c std::cout \u6709\u4e9b\u60c5\u51b5\u4e0b\u4f1a\u81ea\u52a8\u6539\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5\u3002\u6839\u636e\u7f16\u8bd1\u5668\u7684\u4e0d\u540c\uff0c\u6307\u6570\u90e8\u5206\u4f1a\u88ab\u88650\u5230\u6700\u5c0f\u4f4d\u6570\u3002\u4e0d\u8981\u62c5\u5fc3\uff0c9.87654e+006 \u548c 9.87654e6 \u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u53ea\u662f\u8865\u4e86\u4e00\u4e9b0\u7f62\u4e86\u3002\u6307\u6570\u7684\u6700\u5c0f\u4f4d\u6570\u6839\u7f16\u8bd1\u5668\u6709\u5173\uff0cVisual Studio \u662f 3\uff0c\u5176\u4ed6\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u4f7f\u7528C99\u7684\u6807\u51c6\u53732\u3002 \u6d6e\u70b9\u6570\u7cbe\u5ea6\u53d6\u51b3\u4e8e\u6d6e\u70b9\u6570\u7c7b\u578b\u7684\u5927\u5c0f\uff08float\u7684\u7cbe\u5ea6\u5c0f\u4e8edouble\uff09\u4ee5\u53ca\u88ab\u5b58\u50a8\u7684\u5177\u4f53\u503c\uff08\u6709\u4e9b\u503c\u7684\u7cbe\u5ea6\u672c\u8eab\u5c31\u6bd4\u5176\u4ed6\u503c\u9ad8\uff09\u3002float \u7c7b\u578b\u7684\u7cbe\u5ea6\u901a\u5e38\u4f4d\u4e8e 6 \u5230 9\u4e4b\u95f4\uff0c\u591a\u6570\u5177\u6709\u81f3\u5c117\u4f4d\u6709\u6548\u6570\u5b57\u3002Double \u7c7b\u578b\u7684\u7cbe\u5ea6\u901a\u5e38\u4e3a 15 \u5230 18\u4e4b\u95f4\uff0c\u591a\u6570\u5177\u6709\u81f3\u5c1116\u4f4d\u6709\u6548\u6570\u5b57\u3002Long double \u7c7b\u578b\u7684\u7cbe\u5ea6\u6700\u5c0f\u4e3a15\u300118\u621633\u4f4d\u6709\u6548\u6570\u5b57\uff0c\u53d6\u51b3\u4e8e\u5b83\u5360\u7528\u591a\u5c11\u5b57\u8282\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u540d\u4e3a std::setprecision() \u7684 \u8f93\u51fa\u64cd\u7eb5\u5668(Output manipulators) \u51fd\u6570\u6765\u6307\u5b9a std::cout \u7684\u7cbe\u5ea6\u3002\u8f93\u51fa\u64cd\u7eb5\u5668\u53ef\u4ee5\u4fee\u6539\u6570\u636e\u8f93\u51fa\u7684\u65b9\u5f0f\uff0c\u5b83\u4eec\u88ab\u5b9a\u4e49\u5728 iomanip \u5934\u6587\u4ef6\u4e2d. 1 2 3 4 5 6 7 8 9 10 11 #include #include // for output manipulator std::setprecision() int main () { std :: cout << std :: setprecision ( 16 ); // \u663e\u793a16\u4f4d\u7cbe\u5ea6 std :: cout << 3.33333333333333333333333333333333333333f << '\\n' ; // f \u540e\u7f00\u8868\u793a float std :: cout << 3.33333333333333333333333333333333333333 << '\\n' ; // \u6ca1\u6709\u540e\u7f00\u9ed8\u8ba4 double return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3.333333253860474 3.333333333333334 \u901a\u8fc7 std::setprecision() \u8bbe\u7f6e\u7cbe\u5ea6\u540e\uff0c\u4e0a\u8ff0\u6570\u5b57\u90fd\u88ab\u6253\u5370\u4e3a16\u4f4d\u7cbe\u5ea6\u3002\u4f46\u662f\uff0c\u5982\u4f60\u6240\u89c1\uff0c\u5176\u7cbe\u5ea6\u5e76\u6ca1\u6709\u8fbe\u523016\u4f4d\u3002\u800c\u4e14\u7531\u4e8e float \u7684\u7cbe\u5ea6\u6bd4 double \u8fd8\u4f4e\uff0c float \u7c7b\u578b\u7684\u503c\u88ab\u6253\u5370\u51fa\u6765\u540e\u8bef\u5dee\u4e5f\u66f4\u5927\u3002 \u7cbe\u5ea6\u95ee\u9898\u4e0d\u4ec5\u4ec5\u5f71\u54cd\u5c0f\u6570\uff0c\u5b83\u8fd8\u4f1a\u5f71\u54cd\u5177\u6709\u5f88\u591a\u6709\u6548\u6570\u5b57\u4f4d\u7684\u6570\u3002\u8ba9\u6211\u4eec\u8003\u8651\u5982\u4e0b\u5927\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include // for std::setprecision() #include int main () { float f { 123456789.0f }; // f \u6709 10 \u4f4d\u6709\u6548\u6570\u5b57 std :: cout << std :: setprecision ( 9 ); // \u663e\u793a f \u7684 9 \u4f4d\u6709\u6548\u6570\u5b57\u3002 std :: cout << f << '\\n' ; return 0 ; } \u8f93\u51fa 1 123456792 123456792 \u6bd4 123456789 \u8981\u5927\u3002123456789.0 \u5177\u6709 10 \u4f4d\u6709\u6548\u6570\u5b57\uff0c\u4f46\u662f float \u901a\u5e38\u53ea\u67097\u4f4d\u7cbe\u5ea6\uff08\u7ed3\u679c123456792\u6b63\u53f7\u67097\u4f4d\u6709\u6548\u6570\u5b57\uff09\u3002\u7cbe\u5ea6\u4e22\u5931\u4e86\uff01\u7cbe\u5ea6\u4e22\u5931\u4e3b\u8981\u662f\u56e0\u4e3a\u6570\u6ca1\u6709\u88ab\u7cbe\u786e\u7684\u5b58\u50a8\uff0c\u6211\u4eec\u79f0\u5176\u4e3a \u820d\u5165\u8bef\u5dee(rounding error) \u3002 \u56e0\u6b64\uff0c\u5728\u4f7f\u7528\u7684\u6d6e\u70b9\u6570\u7cbe\u5ea6\u6bd4\u5bf9\u5e94\u6d6e\u70b9\u7c7b\u578b\u53d8\u91cf\u80fd\u591f\u8868\u793a\u7684\u7cbe\u5ea6\u66f4\u9ad8\u65f6\uff0c\u8981\u591a\u52a0\u6ce8\u610f\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u5185\u5b58\u7a7a\u95f4\u5141\u8bb8\uff0c\u5c3d\u91cf\u4f7f\u7528 double \u800c\u4e0d\u662f float\uff0c\u56e0\u4e3a float \u7684\u7cbe\u5ea6\u66f4\u4f4e\uff0c\u4e5f\u5c31\u6709\u53ef\u80fd\u9020\u6210\u6570\u503c\u4e0d\u51c6\u786e\u7684\u95ee\u9898\u3002 \u820d\u5165\u8bef\u5dee\u7684\u5b58\u5728\u589e\u52a0\u4e86\u6d6e\u70b9\u6570\u6bd4\u8f83\u7684\u96be\u5ea6 \u6d6e\u70b9\u6570\u4f7f\u7528\u8d77\u6765\u66f4\u52a0\u56f0\u96be\uff0c\u8fd9\u662f\u56e0\u4e3a\u5b83\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u6cd5\uff08\u6570\u636e\u5b58\u50a8\u5f62\u5f0f\uff09\u548c\u5341\u8fdb\u5236\u8868\u793a\u6cd5\uff08\u6211\u4eec\u7684\u7406\u89e3\u65b9\u5f0f\uff09\u4e4b\u95f4\u7684\u5173\u7cfb\u5e76\u4e0d\u660e\u663e\u3002\u5bf9\u4e8e\u5206\u6570 1/10 \u6765\u8bf4\uff0c\u5341\u8fdb\u5236\u8868\u793a\u6cd5\u5f88\u5bb9\u6613\u5c06\u5176\u8868\u793a\u4e3a 0.1\uff0c\u800c\u6211\u4eec\u4e5f\u4e60\u60ef\u6027\u7684\u8ba4\u4e3a0.1\u5f88\u5bb9\u6613\u8868\u793a\uff0c\u6bd5\u7adf\u5b83\u53ea\u6709\u4e00\u4f4d\u6709\u6548\u6570\u5b57\u3002\u5b9e\u9645\u4e0a\uff0c\u5bf9\u4e8e\u4e8c\u8fdb\u5236\u6765\u8bf4\uff0c0.1\u9700\u8981\u8868\u793a\u4e3a\u4e00\u4e2a\u65e0\u7a77\u5e8f\u5217\uff1a0.00011001100110011\u2026 \u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u628a0.1\u8d4b\u503c\u7ed9\u6d6e\u70b9\u6570\u65f6\uff0c\u5b9e\u9645\u4e0a\u4f1a\u51fa\u73b0\u7cbe\u5ea6\u95ee\u9898\u3002 \u900f\u8fc7\u4e0b\u9762\u7a0b\u5e8f\u4e2d\u4f60\u53ef\u4ee5\u770b\u5230\u5176\u5f71\u54cd\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include // for std::setprecision() #include int main () { double d { 0.1 }; std :: cout << d << '\\n' ; // use default cout precision of 6 std :: cout << std :: setprecision ( 17 ); std :: cout << d << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 0.1 0.10000000000000001 \u7b2c\u4e00\u884c\uff0c std::cout \u6253\u5370\u4e86 0.1\uff0c\u5982\u6211\u4eec\u6240\u6599\u3002 \u7b2c\u4e8c\u884c\uff0c\u6211\u4eec\u8ba9 std::cout \u663e\u793a17\u4f4d\u7cbe\u5ea6\uff0c\u53ef\u4ee5\u770b\u5230\uff0c d \u7684\u6253\u5370\u7ed3\u679c\u5e76\u4e0d\u662f0.1\u3002\u8fd9\u662f\u56e0\u4e3a double \u5fc5\u987b\u5bf9\u6570\u636e\u8fdb\u884c\u622a\u65ad\uff0c\u6bd5\u7adf\u5185\u5b58\u4e0d\u662f\u65e0\u9650\u7684\u3002\u7ed3\u679c\u5c31\u662f\u6570\u636e\u7684\u7cbe\u5ea6\u7f16\u7a0b\u4e8616\u4f4d(double\u4fdd\u8bc1\u7684)\uff0c\u4f46\u662f\u5176\u503c\u5374\u4e0d\u662f0.1\u3002\u820d\u5165\u8bef\u5dee\u53ef\u80fd\u4f1a\u5f97\u5230\u4e00\u4e2a\u7a0d\u5927\u4e00\u70b9\u6216\u7a0d\u5c0f\u4e00\u70b9\u7684\u503c\uff0c\u8fd9\u53d6\u51b3\u4e8e\u622a\u65ad\u53d1\u751f\u5728\u54ea\u91cc\u3002 \u820d\u5165\u8bef\u5dee\u4f1a\u5e26\u6765\u975e\u9884\u671f\u7684\u7ed3\u679c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // for std::setprecision() #include int main () { std :: cout << std :: setprecision ( 17 ); double d1 { 1.0 }; std :: cout << d1 << '\\n' ; double d2 { 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 }; // should equal 1.0 std :: cout << d2 << '\\n' ; return 0 ; } 1 2 1 0.99999999999999989 \u5c3d\u7ba1\u6211\u4eec\u671f\u671b d1 \u7684\u503c\u5e94\u8be5\u7b49\u4e8e d2 \uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u6211\u4eec\u4e5f\u770b\u5230\u4e86\u5b83\u4eec\u5e76\u4e0d\u76f8\u7b49\u3002\u5982\u679c\u6211\u4eec\u5728\u7a0b\u5e8f\u4e2d\u5bf9 d1 \u548c d2 \u8fdb\u884c\u6bd4\u8f83\uff0c\u90a3\u4e48\u7ed3\u679c\u53ef\u80fd\u5c31\u4f1a\u51fa\u4eba\u610f\u6599\u3002\u56e0\u4e3a\u6d6e\u70b9\u6570\u7684\u8fd9\u79cd\u4e0d\u7cbe\u786e\u7684\u7279\u6027\uff0c\u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\u901a\u5e38\u4f1a\u4ea7\u751f\u95ee\u9898\u2014\u2014\u6211\u4eec\u4f1a\u5728 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 \u4e2d\u8fdb\u884c\u8be6\u7ec6\u5730\u8ba8\u8bba\u3002 \u5173\u4e8e\u820d\u5165\u8bef\u5dee\u7684\u6700\u540e\u4e00\u70b9\u53ee\u5631\uff1a\u6570\u5b66\u8fd0\u7b97 (\u4f8b\u5982\u52a0\u6cd5\u548c\u4e58\u6cd5) \u4f1a\u5bfc\u81f4\u820d\u5165\u8bef\u5dee\u7684\u79ef\u7d2f\u3002\u6240\u4ee5\u5373\u4f7f 0.1 \u53ea\u5728\u7b2c 17\u4f4d\u6709\u6548\u6570\u5b57\u65f6\u5b58\u5728\u8bef\u5dee\uff0c\u5f53\u6211\u4eec\u5c06\u662f\u4e2a0.1\u76f8\u52a0\u65f6\uff0c\u820d\u5165\u8bef\u5dee\u5c31\u51fa\u73b0\u5728\u4e86\u7b2c16\u4f4d\u3002\u4e0d\u65ad\u5730\u8ba1\u7b97\u4f1a\u5bfc\u81f4\u8fd9\u4e9b\u8bef\u5dee\u88ab\u4e0d\u65ad\u5730\u653e\u5927\u3002 \u5173\u952e\u4fe1\u606f \u5f53\u6570\u636e\u4e0d\u80fd\u88ab\u7cbe\u786e\u7684\u5b58\u50a8\u65f6\uff0c\u4f1a\u51fa\u73b0\u820d\u5165\u8bef\u5dee\u3002\u5373\u4f7f\u662f\u975e\u5e38\u7b80\u5355\u7684\u6570\u5b57\u4e5f\u5b58\u5728\u8be5\u95ee\u9898\uff08\u4f8b\u5982 0.1\uff09\u3002\u56e0\u6b64\uff0c\u820d\u5165\u8bef\u5dee\u603b\u662f\u65e0\u65f6\u65e0\u523b\u5730\u53d1\u751f\u3002\u6444\u5165\u8bef\u5dee\u5e76\u4e0d\u662f\u5f02\u5e38\uff0c\u5b83\u662f\u89c4\u5219\u7684\u4e00\u90e8\u5206\u3002\u6c38\u8fdc\u4e0d\u8981\u5047\u8bbe\u4f60\u7684\u6d6e\u70b9\u6570\u662f\u7cbe\u786e\u7684\u3002 \u8fd9\u6761\u89c4\u5219\u7684\u7ed3\u679c\u5c31\u662f\u5728\u4f7f\u7528\u6d6e\u70b9\u6570\u5904\u7406\u91d1\u878d\u6216\u8d27\u5e01\u6570\u636e\u65f6\uff0c\u4e00\u5b9a\u8981\u683c\u5916\u8b66\u60d5\u3002 NaN \u548c Inf \u8fd8\u6709\u4e24\u7c7b\u7279\u6b8a\u7684\u6d6e\u70b9\u6570\u3002\u4e00\u4e2a\u662f Inf \uff0c\u8868\u793a\u65e0\u7a77\u3002 Inf \u53ef\u4ee5\u662f\u6b63\u4e5f\u53ef\u4ee5\u662f\u8d1f\u3002\u7b2c\u4e8c\u4e2a\u662f NaN \uff0c\u8868\u793a\u201d\u975e\u6570\u5b57\u201c\u3002 NaN \u7684\u79cd\u7c7b\u6709\u5f88\u591a\uff08\u6211\u4eec\u4e0d\u4f1a\u5728\u6b64\u8ba8\u8bba\uff09\u3002 NaN \u548c Inf \u53ea\u6709\u5728\u7f16\u8bd1\u5668\u4f7f\u7528\u7279\u5b9a\u683c\u5f0f(IEEE 754) \u7684\u6d6e\u70b9\u6570\u65f6\u624d\u53ef\u4ee5\u4f7f\u7528\u3002\u5f53\u4f7f\u7528\u5176\u4ed6\u683c\u5f0f\u7684\u65f6\u5019\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u5c31\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e0b\u9762\u7684\u4f8b\u7a0b\u5c55\u793a\u4e86\u4e0a\u8ff0\u4e09\u79cd\u6d6e\u70b9\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { double zero { 0.0 }; double posinf { 5.0 / zero }; // \u6b63\u65e0\u7a77 std :: cout << posinf << '\\n' ; double neginf { -5.0 / zero }; // \u8d1f\u65e0\u7a77 std :: cout << neginf << '\\n' ; double nan { zero / zero }; // \u975e\u6570\u5b57 (\u6570\u5b66\u4e0a\u65e0\u6548) std :: cout << nan << '\\n' ; return 0 ; } \u5728 Windows \u4e0a\u7684 Visual Studio 2008 \u8fd0\u884c\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 1.#INF -1.#INF 1.#IND INF \u8868\u793a\u65e0\u7a77\uff0c\u800c IND \u8868\u793a\u4e0d\u786e\u5b9a\uff08indeterminate\uff09\u3002\u6ce8\u610f\uff0c Inf \u548c NaN \u7684\u6253\u5370\u7ed3\u679c\u548c\u5e73\u53f0\u662f\u76f8\u5173\u7684\uff0c\u5728\u4f60\u7684\u7535\u8111\u4e0a\u7ed3\u679c\u53ef\u80fd\u6709\u6240\u4e0d\u540c\u3002 \u6700\u4f73\u5b9e\u8df5 \u8981\u5f7b\u5e95\u907f\u514d\u96640\uff0c\u5373\u4f7f\u4f60\u7684\u7684\u8ba1\u7b97\u673a\u652f\u6301\u8fd9\u79cd\u7528\u6cd5\u3002 \u7ed3\u8bba \u603b\u7ed3\u4e00\u4e0b\uff0c\u5173\u4e8e\u6d6e\u70b9\u6570\u4f60\u9700\u8981\u8bb0\u4f4f\u4e0b\u9762\u4e24\u4ef6\u4e8b\uff1a \u6d6e\u70b9\u6570\u5728\u8868\u793a\u5f88\u5927\u6216\u5f88\u5c0f\u7684\u6570\u65f6\u975e\u5e38\u6709\u7528\uff0c\u5f53\u7136\u4e5f\u9002\u5408\u4fdd\u5b58\u6709\u5c0f\u6570\u90e8\u5206\u7684\u6570\u3002 \u6d6e\u70b9\u6570\u901a\u5e38\u4f1a\u6709\u5fae\u5c0f\u7684\u820d\u5165\u8bef\u5dee\u95ee\u9898\uff0c\u5373\u4fbf\u4e00\u4e2a\u6570\u7684\u6709\u6548\u6570\u5b57\u6bd4\u7cbe\u5ea6\u80fd\u591f\u8868\u793a\u7684\u6709\u6548\u6570\u5b57\u5c11\uff0c\u4e5f\u5b58\u5728\u8be5\u95ee\u9898\u3002\u5f88\u591a\u65f6\u5019\u8fd9\u4e9b\u95ee\u9898\u4e0d\u4f1a\u88ab\u6211\u4eec\u6ce8\u610f\u5230\uff0c\u56e0\u4e3a\u6444\u5165\u8bef\u5dee\u5f88\u5c0f\uff0c\u800c\u4e14\u8f93\u51fa\u7ed3\u679c\u4f1a\u88ab\u622a\u65ad\uff0c\u4f46\u662f\uff0c\u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\u65f6\uff0c\u820d\u5165\u8bef\u5dee\u7684\u5b58\u5728\u53ef\u80fd\u4f1a\u5e26\u6765\u610f\u5916\u7684\u7ed3\u679c\u3002 \u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u65f6\u4e5f\u53ef\u80fd\u9020\u6210\u820d\u5165\u8bef\u5dee\u7684\u4e0d\u65ad\u589e\u5927\u3002","title":"4.8 - \u6d6e\u70b9\u6570"},{"location":"4-8-Floating-point-numbers/#_1","text":"\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { std :: cout << 5.0 << '\\n' ; std :: cout << 6.7f << '\\n' ; std :: cout << 9876543.21 << '\\n' ; return 0 ; } \u4e0a\u8ff0\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u53ef\u80fd\u4f1a\u8ba9\u4f60\u611f\u5230\u610f\u5916\uff1a 1 2 3 5 6.7 9.87654e+06 \u5728\u7b2c\u4e00\u6761\u6253\u5370\u4e2d\uff0c std::cout \u6253\u5370 5\uff0c\u5373\u4f7f\u6211\u4eec\u8f93\u5165\u7684\u662f5.0\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b std::cout \u4e0d\u4f1a\u8f93\u5165\u5c0f\u6570\u90e8\u5206\u76840\u3002 \u5728\u7b2c\u4e8c\u6761\u6253\u5370\u4e2d\uff0c\u7ed3\u679c\u548c\u6211\u4eec\u671f\u671b\u7684\u662f\u4e00\u6837\u3002 \u5728\u7b2c\u4e09\u6761\u6253\u5370\u4e2d\uff0c\u6253\u5370\u7ed3\u679c\u4e3a\u79d1\u5b66\u8ba1\u6570\u6cd5\uff08\u5982\u679c\u4f60\u9700\u8981\u590d\u4e60\u4e00\u4e0b\u79d1\u5b66\u8ba1\u6570\u6cd5\u53ef\u4ee5\u53c2\u8003 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 \uff09\u3002","title":"\u6253\u5370\u6d6e\u70b9\u6570"},{"location":"4-8-Floating-point-numbers/#_2","text":"\u5047\u5b9a\u4f7f\u7528 IEEE 754 \u8868\u793a\u6cd5\uff1a \u5927\u5c0f \u8303\u56f4 \u7cbe\u5ea6 4 bytes \u00b11.18 x 10-38 \u5230 \u00b13.4 x 1038 6-9 \u4f4d\u6709\u6548\u6570\u5b57\uff0c\u4e00\u822c\u4e3a7 8 bytes \u00b12.23 x 10-308 \u5230 \u00b11.80 x 10308 15-18 \u4f4d\u6709\u6548\u6570\u5b57\uff0c\u4e00\u822c\u4e3a16 80-bits (typically uses 12 \u6216 16 bytes) \u00b13.36 x 10-4932 \u5230 \u00b11.18 x 104932 18-21 \u4f4d\u6709\u6548\u6570\u5b57 16 bytes \u00b13.36 x 10-4932 \u5230 \u00b11.18 x 104932 33-36 \u4f4d\u6709\u6548\u6570\u5b57 80\u4f4d\u7684\u6d6e\u70b9\u6570\u7c7b\u578b\u53ef\u4ee5\u770b\u505a\u662f\u5386\u53f2\u9057\u7559\u95ee\u9898\u3002\u5728\u73b0\u4ee3\u5904\u7406\u5668\u4e0a\uff0c\u5b83\u901a\u5e38\u88ab\u5b9e\u73b0\u4e3a12\u5b57\u8282\u621616\u5b57\u8282\uff08\u5bf9\u4e8e\u5904\u7406\u5668\u6765\u8bf4\u662f\u66f4\u52a0\u65b9\u4fbf\u7684\u957f\u5ea6\uff09\u3002 \u4f60\u53ef\u80fd\u4f1a\u5947\u602a\u4e3a\u4ec0\u4e4880\u4f4d\u7684\u6d6e\u70b9\u6570\u7c7b\u578b\u548c16\u5b57\u8282\u7684\u6d6e\u70b9\u6570\u7c7b\u578b\u5177\u6709\u76f8\u540c\u7684\u8303\u56f4\u3002\u8fd9\u662f\u56e0\u4e3a\u5b83\u4eec\u4e2d\u4e13\u95e8\u7528\u6765\u8868\u793a\u6307\u6570\u7684\u4f4d\u662f\u76f8\u540c\u7684\u2014\u2014\u4e0d\u8fc7\uff0c16\u5b57\u8282\u7684\u6d6e\u70b9\u6570\u53ef\u4ee5\u8868\u793a\u66f4\u591a\u7684\u6709\u6548\u6570\u5b57\u3002","title":"\u6d6e\u70b9\u6570\u7684\u8303\u56f4"},{"location":"4-8-Floating-point-numbers/#_3","text":"\u8003\u8651\u4ee5\u4e0b\u5206\u6570 1/3\u3002\u5b83\u7684\u5341\u8fdb\u5236\u8868\u793a\u6cd5\u4e3a 0.33333333333333\u2026 \u65e0\u9650\u5faa\u73af3\u3002\u5982\u679c\u4f60\u5728\u7eb8\u4e0a\u4e00\u76f4\u5199\u7684\u8bdd\uff0c\u5199\u7740\u5199\u7740\u4f60\u5c31\u53d7\u4e0d\u4e86\u4e86\u3002\u6700\u7ec8\u4f60\u5199\u4e0b\u6765\u7684\u53ef\u80fd\u662f 0.3333333333\u2026. (\u65e0\u9650\u5faa\u73af3)\uff0c\u4f46\u7edd\u5bf9\u4e0d\u53ef\u80fd\u662f\u5168\u90e8\u7684\u503c\u3002 \u5bf9\u4e8e\u8ba1\u7b97\u673a\u6765\u8bf4\uff0c\u65e0\u9650\u957f\u5ea6\u7684\u6570\u5b57\u9700\u8981\u65e0\u9650\u5927\u7684\u5185\u5b58\u624d\u80fd\u5b58\u50a8\uff0c\u800c\u901a\u5e38\u6211\u4eec\u53ea\u80fd\u4f7f\u75284\u5b57\u8282\u62168\u5b57\u8282\u7684\u7a7a\u95f4\u3002\u6709\u9650\u7684\u5185\u5b58\u5c31\u610f\u5473\u7740\u6d6e\u70b9\u6570\u53ea\u80fd\u5b58\u653e\u7279\u5b9a\u957f\u5ea6\u7684\u6709\u6548\u6570\u5b57\u2014\u2014\u5176\u4ed6\u5269\u4f59\u90e8\u5206\u5c31\u88ab\u4e22\u5f03\u4e86\u3002\u6700\u7ec8\u88ab\u5b58\u50a8\u4e0b\u6765\u7684\u6570\u5b57\u662f\u6211\u4eec\u80fd\u591f\u63a5\u53d7\u7684\u503c\uff0c\u800c\u4e0d\u662f\u5b9e\u9645\u503c\u3002 \u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u88ab\u5b9a\u4e49\u4e3a\uff1a\u5728\u4e0d\u635f\u5931\u4fe1\u606f\u7684\u60c5\u51b5\u4e0b\u80fd\u591f\u8868\u793a\u7684\u6700\u591a\u7684\u6709\u6548\u6570\u5b57\u4f4d\u6570\u3002 \u5728\u8f93\u51fa\u6d6e\u70b9\u6570\u65f6\uff0c std::cout \u7684\u9ed8\u8ba4\u7cbe\u5ea6\u4e3a6\u2014\u2014\u4e5f\u5c31\u8bf4\u5b83\u5047\u8bbe\u6240\u6709\u6d6e\u70b9\u6570\u90fd\u53ea\u67096\u4f4d\u6709\u6548\u6570\u5b57\uff0c\u8d85\u8fc7\u7684\u90e8\u5206\u90fd\u4f1a\u88ab\u622a\u65ad\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u5bf9 std::cout \u622a\u65ad\u52306\u4f4d\u6709\u6548\u6570\u5b57\u8fdb\u884c\u4e86\u6f14\u793a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { std :: cout << 9.87654321f << '\\n' ; std :: cout << 987.654321f << '\\n' ; std :: cout << 987654.321f << '\\n' ; std :: cout << 9876543.21f << '\\n' ; std :: cout << 0.0000987654321f << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 3 4 5 9.87654 987.654 987654 9.87654e+006 9.87654e-005 \u6211\u4eec\u6ce8\u610f\u5230\uff0c\u8f93\u51fa\u7ed3\u679c\u4e2d\u7684\u6570\u90fd\u53ea\u67096\u4f4d\u6709\u6548\u6570\u5b57\u3002 \u540c\u65f6\u6211\u4eec\u6ce8\u610f\u5230\uff0c std::cout \u6709\u4e9b\u60c5\u51b5\u4e0b\u4f1a\u81ea\u52a8\u6539\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5\u3002\u6839\u636e\u7f16\u8bd1\u5668\u7684\u4e0d\u540c\uff0c\u6307\u6570\u90e8\u5206\u4f1a\u88ab\u88650\u5230\u6700\u5c0f\u4f4d\u6570\u3002\u4e0d\u8981\u62c5\u5fc3\uff0c9.87654e+006 \u548c 9.87654e6 \u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u53ea\u662f\u8865\u4e86\u4e00\u4e9b0\u7f62\u4e86\u3002\u6307\u6570\u7684\u6700\u5c0f\u4f4d\u6570\u6839\u7f16\u8bd1\u5668\u6709\u5173\uff0cVisual Studio \u662f 3\uff0c\u5176\u4ed6\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u4f7f\u7528C99\u7684\u6807\u51c6\u53732\u3002 \u6d6e\u70b9\u6570\u7cbe\u5ea6\u53d6\u51b3\u4e8e\u6d6e\u70b9\u6570\u7c7b\u578b\u7684\u5927\u5c0f\uff08float\u7684\u7cbe\u5ea6\u5c0f\u4e8edouble\uff09\u4ee5\u53ca\u88ab\u5b58\u50a8\u7684\u5177\u4f53\u503c\uff08\u6709\u4e9b\u503c\u7684\u7cbe\u5ea6\u672c\u8eab\u5c31\u6bd4\u5176\u4ed6\u503c\u9ad8\uff09\u3002float \u7c7b\u578b\u7684\u7cbe\u5ea6\u901a\u5e38\u4f4d\u4e8e 6 \u5230 9\u4e4b\u95f4\uff0c\u591a\u6570\u5177\u6709\u81f3\u5c117\u4f4d\u6709\u6548\u6570\u5b57\u3002Double \u7c7b\u578b\u7684\u7cbe\u5ea6\u901a\u5e38\u4e3a 15 \u5230 18\u4e4b\u95f4\uff0c\u591a\u6570\u5177\u6709\u81f3\u5c1116\u4f4d\u6709\u6548\u6570\u5b57\u3002Long double \u7c7b\u578b\u7684\u7cbe\u5ea6\u6700\u5c0f\u4e3a15\u300118\u621633\u4f4d\u6709\u6548\u6570\u5b57\uff0c\u53d6\u51b3\u4e8e\u5b83\u5360\u7528\u591a\u5c11\u5b57\u8282\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u540d\u4e3a std::setprecision() \u7684 \u8f93\u51fa\u64cd\u7eb5\u5668(Output manipulators) \u51fd\u6570\u6765\u6307\u5b9a std::cout \u7684\u7cbe\u5ea6\u3002\u8f93\u51fa\u64cd\u7eb5\u5668\u53ef\u4ee5\u4fee\u6539\u6570\u636e\u8f93\u51fa\u7684\u65b9\u5f0f\uff0c\u5b83\u4eec\u88ab\u5b9a\u4e49\u5728 iomanip \u5934\u6587\u4ef6\u4e2d. 1 2 3 4 5 6 7 8 9 10 11 #include #include // for output manipulator std::setprecision() int main () { std :: cout << std :: setprecision ( 16 ); // \u663e\u793a16\u4f4d\u7cbe\u5ea6 std :: cout << 3.33333333333333333333333333333333333333f << '\\n' ; // f \u540e\u7f00\u8868\u793a float std :: cout << 3.33333333333333333333333333333333333333 << '\\n' ; // \u6ca1\u6709\u540e\u7f00\u9ed8\u8ba4 double return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3.333333253860474 3.333333333333334 \u901a\u8fc7 std::setprecision() \u8bbe\u7f6e\u7cbe\u5ea6\u540e\uff0c\u4e0a\u8ff0\u6570\u5b57\u90fd\u88ab\u6253\u5370\u4e3a16\u4f4d\u7cbe\u5ea6\u3002\u4f46\u662f\uff0c\u5982\u4f60\u6240\u89c1\uff0c\u5176\u7cbe\u5ea6\u5e76\u6ca1\u6709\u8fbe\u523016\u4f4d\u3002\u800c\u4e14\u7531\u4e8e float \u7684\u7cbe\u5ea6\u6bd4 double \u8fd8\u4f4e\uff0c float \u7c7b\u578b\u7684\u503c\u88ab\u6253\u5370\u51fa\u6765\u540e\u8bef\u5dee\u4e5f\u66f4\u5927\u3002 \u7cbe\u5ea6\u95ee\u9898\u4e0d\u4ec5\u4ec5\u5f71\u54cd\u5c0f\u6570\uff0c\u5b83\u8fd8\u4f1a\u5f71\u54cd\u5177\u6709\u5f88\u591a\u6709\u6548\u6570\u5b57\u4f4d\u7684\u6570\u3002\u8ba9\u6211\u4eec\u8003\u8651\u5982\u4e0b\u5927\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include // for std::setprecision() #include int main () { float f { 123456789.0f }; // f \u6709 10 \u4f4d\u6709\u6548\u6570\u5b57 std :: cout << std :: setprecision ( 9 ); // \u663e\u793a f \u7684 9 \u4f4d\u6709\u6548\u6570\u5b57\u3002 std :: cout << f << '\\n' ; return 0 ; } \u8f93\u51fa 1 123456792 123456792 \u6bd4 123456789 \u8981\u5927\u3002123456789.0 \u5177\u6709 10 \u4f4d\u6709\u6548\u6570\u5b57\uff0c\u4f46\u662f float \u901a\u5e38\u53ea\u67097\u4f4d\u7cbe\u5ea6\uff08\u7ed3\u679c123456792\u6b63\u53f7\u67097\u4f4d\u6709\u6548\u6570\u5b57\uff09\u3002\u7cbe\u5ea6\u4e22\u5931\u4e86\uff01\u7cbe\u5ea6\u4e22\u5931\u4e3b\u8981\u662f\u56e0\u4e3a\u6570\u6ca1\u6709\u88ab\u7cbe\u786e\u7684\u5b58\u50a8\uff0c\u6211\u4eec\u79f0\u5176\u4e3a \u820d\u5165\u8bef\u5dee(rounding error) \u3002 \u56e0\u6b64\uff0c\u5728\u4f7f\u7528\u7684\u6d6e\u70b9\u6570\u7cbe\u5ea6\u6bd4\u5bf9\u5e94\u6d6e\u70b9\u7c7b\u578b\u53d8\u91cf\u80fd\u591f\u8868\u793a\u7684\u7cbe\u5ea6\u66f4\u9ad8\u65f6\uff0c\u8981\u591a\u52a0\u6ce8\u610f\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u5185\u5b58\u7a7a\u95f4\u5141\u8bb8\uff0c\u5c3d\u91cf\u4f7f\u7528 double \u800c\u4e0d\u662f float\uff0c\u56e0\u4e3a float \u7684\u7cbe\u5ea6\u66f4\u4f4e\uff0c\u4e5f\u5c31\u6709\u53ef\u80fd\u9020\u6210\u6570\u503c\u4e0d\u51c6\u786e\u7684\u95ee\u9898\u3002","title":"\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6"},{"location":"4-8-Floating-point-numbers/#_4","text":"\u6d6e\u70b9\u6570\u4f7f\u7528\u8d77\u6765\u66f4\u52a0\u56f0\u96be\uff0c\u8fd9\u662f\u56e0\u4e3a\u5b83\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u6cd5\uff08\u6570\u636e\u5b58\u50a8\u5f62\u5f0f\uff09\u548c\u5341\u8fdb\u5236\u8868\u793a\u6cd5\uff08\u6211\u4eec\u7684\u7406\u89e3\u65b9\u5f0f\uff09\u4e4b\u95f4\u7684\u5173\u7cfb\u5e76\u4e0d\u660e\u663e\u3002\u5bf9\u4e8e\u5206\u6570 1/10 \u6765\u8bf4\uff0c\u5341\u8fdb\u5236\u8868\u793a\u6cd5\u5f88\u5bb9\u6613\u5c06\u5176\u8868\u793a\u4e3a 0.1\uff0c\u800c\u6211\u4eec\u4e5f\u4e60\u60ef\u6027\u7684\u8ba4\u4e3a0.1\u5f88\u5bb9\u6613\u8868\u793a\uff0c\u6bd5\u7adf\u5b83\u53ea\u6709\u4e00\u4f4d\u6709\u6548\u6570\u5b57\u3002\u5b9e\u9645\u4e0a\uff0c\u5bf9\u4e8e\u4e8c\u8fdb\u5236\u6765\u8bf4\uff0c0.1\u9700\u8981\u8868\u793a\u4e3a\u4e00\u4e2a\u65e0\u7a77\u5e8f\u5217\uff1a0.00011001100110011\u2026 \u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u628a0.1\u8d4b\u503c\u7ed9\u6d6e\u70b9\u6570\u65f6\uff0c\u5b9e\u9645\u4e0a\u4f1a\u51fa\u73b0\u7cbe\u5ea6\u95ee\u9898\u3002 \u900f\u8fc7\u4e0b\u9762\u7a0b\u5e8f\u4e2d\u4f60\u53ef\u4ee5\u770b\u5230\u5176\u5f71\u54cd\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include // for std::setprecision() #include int main () { double d { 0.1 }; std :: cout << d << '\\n' ; // use default cout precision of 6 std :: cout << std :: setprecision ( 17 ); std :: cout << d << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 0.1 0.10000000000000001 \u7b2c\u4e00\u884c\uff0c std::cout \u6253\u5370\u4e86 0.1\uff0c\u5982\u6211\u4eec\u6240\u6599\u3002 \u7b2c\u4e8c\u884c\uff0c\u6211\u4eec\u8ba9 std::cout \u663e\u793a17\u4f4d\u7cbe\u5ea6\uff0c\u53ef\u4ee5\u770b\u5230\uff0c d \u7684\u6253\u5370\u7ed3\u679c\u5e76\u4e0d\u662f0.1\u3002\u8fd9\u662f\u56e0\u4e3a double \u5fc5\u987b\u5bf9\u6570\u636e\u8fdb\u884c\u622a\u65ad\uff0c\u6bd5\u7adf\u5185\u5b58\u4e0d\u662f\u65e0\u9650\u7684\u3002\u7ed3\u679c\u5c31\u662f\u6570\u636e\u7684\u7cbe\u5ea6\u7f16\u7a0b\u4e8616\u4f4d(double\u4fdd\u8bc1\u7684)\uff0c\u4f46\u662f\u5176\u503c\u5374\u4e0d\u662f0.1\u3002\u820d\u5165\u8bef\u5dee\u53ef\u80fd\u4f1a\u5f97\u5230\u4e00\u4e2a\u7a0d\u5927\u4e00\u70b9\u6216\u7a0d\u5c0f\u4e00\u70b9\u7684\u503c\uff0c\u8fd9\u53d6\u51b3\u4e8e\u622a\u65ad\u53d1\u751f\u5728\u54ea\u91cc\u3002 \u820d\u5165\u8bef\u5dee\u4f1a\u5e26\u6765\u975e\u9884\u671f\u7684\u7ed3\u679c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // for std::setprecision() #include int main () { std :: cout << std :: setprecision ( 17 ); double d1 { 1.0 }; std :: cout << d1 << '\\n' ; double d2 { 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 }; // should equal 1.0 std :: cout << d2 << '\\n' ; return 0 ; } 1 2 1 0.99999999999999989 \u5c3d\u7ba1\u6211\u4eec\u671f\u671b d1 \u7684\u503c\u5e94\u8be5\u7b49\u4e8e d2 \uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u6211\u4eec\u4e5f\u770b\u5230\u4e86\u5b83\u4eec\u5e76\u4e0d\u76f8\u7b49\u3002\u5982\u679c\u6211\u4eec\u5728\u7a0b\u5e8f\u4e2d\u5bf9 d1 \u548c d2 \u8fdb\u884c\u6bd4\u8f83\uff0c\u90a3\u4e48\u7ed3\u679c\u53ef\u80fd\u5c31\u4f1a\u51fa\u4eba\u610f\u6599\u3002\u56e0\u4e3a\u6d6e\u70b9\u6570\u7684\u8fd9\u79cd\u4e0d\u7cbe\u786e\u7684\u7279\u6027\uff0c\u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\u901a\u5e38\u4f1a\u4ea7\u751f\u95ee\u9898\u2014\u2014\u6211\u4eec\u4f1a\u5728 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 \u4e2d\u8fdb\u884c\u8be6\u7ec6\u5730\u8ba8\u8bba\u3002 \u5173\u4e8e\u820d\u5165\u8bef\u5dee\u7684\u6700\u540e\u4e00\u70b9\u53ee\u5631\uff1a\u6570\u5b66\u8fd0\u7b97 (\u4f8b\u5982\u52a0\u6cd5\u548c\u4e58\u6cd5) \u4f1a\u5bfc\u81f4\u820d\u5165\u8bef\u5dee\u7684\u79ef\u7d2f\u3002\u6240\u4ee5\u5373\u4f7f 0.1 \u53ea\u5728\u7b2c 17\u4f4d\u6709\u6548\u6570\u5b57\u65f6\u5b58\u5728\u8bef\u5dee\uff0c\u5f53\u6211\u4eec\u5c06\u662f\u4e2a0.1\u76f8\u52a0\u65f6\uff0c\u820d\u5165\u8bef\u5dee\u5c31\u51fa\u73b0\u5728\u4e86\u7b2c16\u4f4d\u3002\u4e0d\u65ad\u5730\u8ba1\u7b97\u4f1a\u5bfc\u81f4\u8fd9\u4e9b\u8bef\u5dee\u88ab\u4e0d\u65ad\u5730\u653e\u5927\u3002 \u5173\u952e\u4fe1\u606f \u5f53\u6570\u636e\u4e0d\u80fd\u88ab\u7cbe\u786e\u7684\u5b58\u50a8\u65f6\uff0c\u4f1a\u51fa\u73b0\u820d\u5165\u8bef\u5dee\u3002\u5373\u4f7f\u662f\u975e\u5e38\u7b80\u5355\u7684\u6570\u5b57\u4e5f\u5b58\u5728\u8be5\u95ee\u9898\uff08\u4f8b\u5982 0.1\uff09\u3002\u56e0\u6b64\uff0c\u820d\u5165\u8bef\u5dee\u603b\u662f\u65e0\u65f6\u65e0\u523b\u5730\u53d1\u751f\u3002\u6444\u5165\u8bef\u5dee\u5e76\u4e0d\u662f\u5f02\u5e38\uff0c\u5b83\u662f\u89c4\u5219\u7684\u4e00\u90e8\u5206\u3002\u6c38\u8fdc\u4e0d\u8981\u5047\u8bbe\u4f60\u7684\u6d6e\u70b9\u6570\u662f\u7cbe\u786e\u7684\u3002 \u8fd9\u6761\u89c4\u5219\u7684\u7ed3\u679c\u5c31\u662f\u5728\u4f7f\u7528\u6d6e\u70b9\u6570\u5904\u7406\u91d1\u878d\u6216\u8d27\u5e01\u6570\u636e\u65f6\uff0c\u4e00\u5b9a\u8981\u683c\u5916\u8b66\u60d5\u3002","title":"\u820d\u5165\u8bef\u5dee\u7684\u5b58\u5728\u589e\u52a0\u4e86\u6d6e\u70b9\u6570\u6bd4\u8f83\u7684\u96be\u5ea6"},{"location":"4-8-Floating-point-numbers/#nan-inf","text":"\u8fd8\u6709\u4e24\u7c7b\u7279\u6b8a\u7684\u6d6e\u70b9\u6570\u3002\u4e00\u4e2a\u662f Inf \uff0c\u8868\u793a\u65e0\u7a77\u3002 Inf \u53ef\u4ee5\u662f\u6b63\u4e5f\u53ef\u4ee5\u662f\u8d1f\u3002\u7b2c\u4e8c\u4e2a\u662f NaN \uff0c\u8868\u793a\u201d\u975e\u6570\u5b57\u201c\u3002 NaN \u7684\u79cd\u7c7b\u6709\u5f88\u591a\uff08\u6211\u4eec\u4e0d\u4f1a\u5728\u6b64\u8ba8\u8bba\uff09\u3002 NaN \u548c Inf \u53ea\u6709\u5728\u7f16\u8bd1\u5668\u4f7f\u7528\u7279\u5b9a\u683c\u5f0f(IEEE 754) \u7684\u6d6e\u70b9\u6570\u65f6\u624d\u53ef\u4ee5\u4f7f\u7528\u3002\u5f53\u4f7f\u7528\u5176\u4ed6\u683c\u5f0f\u7684\u65f6\u5019\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u5c31\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e0b\u9762\u7684\u4f8b\u7a0b\u5c55\u793a\u4e86\u4e0a\u8ff0\u4e09\u79cd\u6d6e\u70b9\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { double zero { 0.0 }; double posinf { 5.0 / zero }; // \u6b63\u65e0\u7a77 std :: cout << posinf << '\\n' ; double neginf { -5.0 / zero }; // \u8d1f\u65e0\u7a77 std :: cout << neginf << '\\n' ; double nan { zero / zero }; // \u975e\u6570\u5b57 (\u6570\u5b66\u4e0a\u65e0\u6548) std :: cout << nan << '\\n' ; return 0 ; } \u5728 Windows \u4e0a\u7684 Visual Studio 2008 \u8fd0\u884c\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 1.#INF -1.#INF 1.#IND INF \u8868\u793a\u65e0\u7a77\uff0c\u800c IND \u8868\u793a\u4e0d\u786e\u5b9a\uff08indeterminate\uff09\u3002\u6ce8\u610f\uff0c Inf \u548c NaN \u7684\u6253\u5370\u7ed3\u679c\u548c\u5e73\u53f0\u662f\u76f8\u5173\u7684\uff0c\u5728\u4f60\u7684\u7535\u8111\u4e0a\u7ed3\u679c\u53ef\u80fd\u6709\u6240\u4e0d\u540c\u3002 \u6700\u4f73\u5b9e\u8df5 \u8981\u5f7b\u5e95\u907f\u514d\u96640\uff0c\u5373\u4f7f\u4f60\u7684\u7684\u8ba1\u7b97\u673a\u652f\u6301\u8fd9\u79cd\u7528\u6cd5\u3002","title":"NaN \u548c Inf"},{"location":"4-8-Floating-point-numbers/#_5","text":"\u603b\u7ed3\u4e00\u4e0b\uff0c\u5173\u4e8e\u6d6e\u70b9\u6570\u4f60\u9700\u8981\u8bb0\u4f4f\u4e0b\u9762\u4e24\u4ef6\u4e8b\uff1a \u6d6e\u70b9\u6570\u5728\u8868\u793a\u5f88\u5927\u6216\u5f88\u5c0f\u7684\u6570\u65f6\u975e\u5e38\u6709\u7528\uff0c\u5f53\u7136\u4e5f\u9002\u5408\u4fdd\u5b58\u6709\u5c0f\u6570\u90e8\u5206\u7684\u6570\u3002 \u6d6e\u70b9\u6570\u901a\u5e38\u4f1a\u6709\u5fae\u5c0f\u7684\u820d\u5165\u8bef\u5dee\u95ee\u9898\uff0c\u5373\u4fbf\u4e00\u4e2a\u6570\u7684\u6709\u6548\u6570\u5b57\u6bd4\u7cbe\u5ea6\u80fd\u591f\u8868\u793a\u7684\u6709\u6548\u6570\u5b57\u5c11\uff0c\u4e5f\u5b58\u5728\u8be5\u95ee\u9898\u3002\u5f88\u591a\u65f6\u5019\u8fd9\u4e9b\u95ee\u9898\u4e0d\u4f1a\u88ab\u6211\u4eec\u6ce8\u610f\u5230\uff0c\u56e0\u4e3a\u6444\u5165\u8bef\u5dee\u5f88\u5c0f\uff0c\u800c\u4e14\u8f93\u51fa\u7ed3\u679c\u4f1a\u88ab\u622a\u65ad\uff0c\u4f46\u662f\uff0c\u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\u65f6\uff0c\u820d\u5165\u8bef\u5dee\u7684\u5b58\u5728\u53ef\u80fd\u4f1a\u5e26\u6765\u610f\u5916\u7684\u7ed3\u679c\u3002 \u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u65f6\u4e5f\u53ef\u80fd\u9020\u6210\u820d\u5165\u8bef\u5dee\u7684\u4e0d\u65ad\u589e\u5927\u3002","title":"\u7ed3\u8bba"},{"location":"4-9-Boolean-values/","tags":["data type","bool"],"text":"Key Takeaway \u4e0d\u80fd\u7528\u62ec\u53f7\u521d\u59cb\u5316\u548c\u4e00\u4e2a\u6574\u578b\u6570\u6765\u521d\u59cb\u5316\u5e03\u5c14\u53d8\u91cf\uff0c\u5176\u4ed6\u60c5\u51b5\u662f\u53ef\u4ee5\u8f6c\u6362\u7684 std::cin \u4e0d\u80fd\u63a5\u6536 true \u6216\u8005 false \uff0c\u9664\u975e\u4f7f\u7528 std::cout << std::boolalpha; \u5728\u73b0\u5b9e\u4e2d\uff0c\u6709\u4e9b\u95ee\u9898\u7684\u7b54\u6848\u53ea\u6709\u201d\u662f\u201c\u6216\u201d\u4e0d\u662f\u201c\u4e24\u79cd\u3002\u201d\u82f9\u679c\u662f\u6c34\u679c\u5417\uff1f\u201c\uff0c\u662f\u7684\u3002\u201d\u4f60\u7231\u5403\u7b0b\u5417\uff1f\u201c\u4e0d\u3002 \u73b0\u5728\uff0c\u6211\u4eec\u8003\u8651\u4e00\u4e2a\u7c7b\u4f3c\u7684\u8bed\u53e5\uff0c\u5b83\u53ea\u80fd\u7528 true \u6216 false \u6765\u56de\u7b54\uff1a\u201d\u82f9\u679c\u662f\u6c34\u679c\u201c\uff0c true \u3002\u201d\u6211\u559c\u6b22\u5403\u7b0b\u201c\uff0c false \u3002 \u50cf\u8fd9\u79cd\u53ea\u6709\u4e24\u79cd\u53ef\u80fd\u7b54\u6848\u7684\u8bed\u53e5\uff08yes/true \u6216 no/false\uff09\u5f88\u5e38\u89c1\uff0c\u5f88\u591a\u7f16\u7a0b\u8bed\u8a00\u90fd\u6709\u7528\u4e8e\u5904\u7406\u6b64\u7c7b\u8f93\u51fa\u7ed3\u679c\u7684\u7279\u6b8a\u7c7b\u578b\u3002\u5b83\u4eec\u79f0\u4e3a\u5e03\u5c14\u7c7b\u578b\uff08\u6ce8\u610f\uff1aBoolean\u5728\u82f1\u8bed\u4e2d\u662f\u5927\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684\uff0c\u56e0\u4e3a\u5b83\u7684\u53d1\u660e\u4eba\u662f\u4e54\u6cbb\u5e03\u5c14\uff08George Boole\uff09\uff09\u3002 \u5e03\u5c14\u53d8\u91cf \u5e03\u5c14\u53d8\u91cf\u53ea\u6709\u4e24\u79cd\u53ef\u80fd\u7684\u503c\uff1a true \u548c false \u3002 \u58f0\u660e\u5e03\u5c14\u7c7b\u578b\u7684\u53d8\u91cf\u9700\u8981\u4f7f\u7528\u5173\u952e\u5b57 bool \u3002 1 bool b ; \u521d\u59cb\u5316\u5e03\u5c14\u7c7b\u578b\u7684\u53d8\u91cf\u6216\u4e3a\u5176\u8d4b\u503c\uff0c\u9700\u8981\u4f7f\u7528 true \u6216 false \u3002 1 2 3 4 bool b1 { true }; bool b2 { false }; b1 = false ; bool b3 {}; // \u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a false \u548c\u4e00\u5143\u8d1f\u53f7\u8fd0\u7b97\u7b26( - )\u53ef\u4ee5\u88ab\u7528\u6765\u5c06\u4e00\u4e2a\u6b63\u6570\u53d8\u6210\u8d1f\u6570\u4e00\u6837\uff0c\u4f7f\u7528\u903b\u8f91 \u975e \u8fd0\u7b97\u7b26( ! )\u53ef\u4ee5\u5c06\u5e03\u5c14\u503c true \u53d8\u6210 false \u6216\u8005\u628a false \u53d8\u6210 true \uff1a 1 2 bool b1 { ! true }; // b1 \u88ab\u521d\u59cb\u5316\u4e3a false bool b2 { ! false }; // b2 \u88ab\u521d\u59cb\u5316\u4e3a true \u5e03\u5c14\u503c\u901a\u5e38\u5e76\u4e0d\u4f1a\u4ee5 \u201ctrue\u201d \u6216 \u201cfalse\u201d\u6765\u5b58\u50a8\u3002\u5b9e\u9645\u4e0a\u5b83\u662f\u4ee5\u6574\u578b\u6765\u5b58\u50a8\u7684\uff1a true \u4f1a\u53d8\u62101\uff0c false \u4f1a\u53d8\u62100\u3002\u7c7b\u4f3c\u5730\uff0c\u5f53\u5e03\u5c14\u7c7b\u578b\u6c42\u503c\u65f6\uff0c\u6c42\u503c\u7684\u5b9e\u9645\u7ed3\u679c\u4e5f\u5e76\u4e0d\u662f\u201ctrue\u201d \u6216 \u201cfalse\u201d\u3002\u901a\u5e38\u5b83\u4eec\u4f1a\u6c42\u503c\u6210_0_ (false) \u6216 1 (true)\u3002\u56e0\u4e3a\u5e03\u5c14\u7c7b\u578b\u5b9e\u9645\u4e0a\u662f\u4ee5\u6574\u578b\u5b58\u50a8\u7684\uff0c\u56e0\u6b64\u5b83\u5c5e\u4e8e\u4e00\u79cd\u6574\u5f62\u7684\u7c7b\u578b(integral type)\u3002 \u6253\u5370\u5e03\u5c14\u53d8\u91cf \u5f53\u4f7f\u7528 std::cout \u6253\u5370\u5e03\u5c14\u53d8\u91cf\u65f6\uff0c std::cout \u4f1a\u5c06 false \u6253\u5370\u6210 0\uff0c\u800c\u628a true \u6253\u5370\u62101\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { std :: cout << true << '\\n' ; // true evaluates to 1 std :: cout << ! true << '\\n' ; // !true evaluates to 0 bool b { false }; std :: cout << b << '\\n' ; // b is false, which evaluates to 0 std :: cout << ! b << '\\n' ; // !b is true, which evaluates to 1 return 0 ; } Outputs: 1 2 3 4 1 0 0 1 \u5982\u679c\u4f60\u5e0c\u671b std::cout \u6253\u5370 \u201ctrue\u201d \u6216 \u201cfalse\u201d \u800c\u4e0d\u662f 0 \u6216 1\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 std::boolalpha \u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { std :: cout << true << '\\n' ; std :: cout << false << '\\n' ; std :: cout << std :: boolalpha ; // print bools as true or false std :: cout << true << '\\n' ; std :: cout << false << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 4 1 0 true false \u4f60\u53ef\u4ee5\u4f7f\u7528 std::noboolalpha \u6765\u5173\u95ed\u6b64\u529f\u80fd\u3002 \u6574\u578b\u8f6c\u6362\u4e3a\u5e03\u5c14\u7c7b\u578b \u4f60\u4e0d\u80fd\u7528\u62ec\u53f7\u521d\u59cb\u5316\u548c\u4e00\u4e2a\u6574\u578b\u6570\u6765\u521d\u59cb\u5316\u5e03\u5c14\u53d8\u91cf\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { bool b { 4 }; // \u9519\u8bef\uff1a: \u4e0d\u5141\u8bb8\u7f29\u7a84\u8f6c\u6362 std :: cout << b ; return 0 ; } (\u6ce8\u610f\uff1a\u6709\u4e9b\u7248\u672c\u7684g++\u5e76\u4e0d\u4f1a\u6709\u4e0a\u8ff0\u9650\u5236\uff09 \u4e0d\u8fc7\uff0c\u5728\u5176\u4ed6\u573a\u666f\u4e0b\u6574\u578b\u662f\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\u7684\u3002\u6574\u578b\u503c0\u4f1a\u88ab\u8f6c\u6362\u4e3a false \uff0c\u800c\u5176\u4ed6\u6574\u578b\u4f1a\u88ab\u8f6c\u6362\u4e3a true \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << std :: boolalpha ; // print bools as true or false bool b1 = 4 ; // \u62f7\u8d1d\u521d\u59cb\u5316\u5141\u8bb8\u9690\u5f0f\u8f6c\u6362 std :: cout << b1 << '\\n' ; bool b2 = 0 ; // \u62f7\u8d1d\u521d\u59cb\u5316\u5141\u8bb8\u9690\u5f0f\u8f6c\u6362 std :: cout << b2 << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 true false \u6ce8\u610f\uff1a bool b1 = 4; \u53ef\u4ee5\u80fd\u4f1a\u4ea7\u751f\u8b66\u544a\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f60\u9700\u8981\u5173\u95ed\u7f16\u8bd1\u7684\u201d\u5c06\u544a\u8b66\u5f53\u505a\u9519\u8bef\u5904\u7406\u201c\u529f\u80fd\uff0c\u8fd9\u6837\u4f60\u731c\u5462\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\u3002 \u8f93\u5165\u5e03\u5c14\u503c \u4f7f\u7528 std::cin \u6765\u8f93\u5165\u5e03\u5c14\u7c7b\u578b\u5e38\u5e38\u4f1a\u8ba9\u65b0\u624b\u7a0b\u5e8f\u5458\u6293\u72c2\u3002 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { bool b {}; // \u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a false std :: cout << \"Enter a boolean value: \" ; std :: cin >> b ; std :: cout << \"You entered: \" << b << '\\n' ; return 0 ; } 1 2 Enter a Boolean value: true You entered: 0 \u4ec0\u4e48\uff01\u600e\u4e48\u4f1a\u8fd9\u6837\uff1f \u5b9e\u9645\u4e0a\uff0c std::cin \u53ea\u80fd\u591f\u63a5\u6536\u4e24\u79cd\u5e03\u5c14\u7c7b\u578b\u8f93\u5165\uff1a0\u548c1\uff08\u800c\u4e0d\u662f true \u6216 false \uff09\u3002\u4efb\u4f55\u5176\u4ed6\u7684\u8f93\u5165\u90fd\u4f1a\u5bfc\u81f4 std::cin \u51fa\u9519\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u8f93\u5165\u4e86true\uff0c\u56e0\u6b64\u5b9e\u9645\u4e0a std::cin \u51fa\u9519\u4e86\uff0c\u8f93\u5165\u51fa\u9519\u540e\uff0c\u63a5\u53d7\u8f93\u5165\u7684\u53d8\u91cf\u4f1a\u88ab\u6e05\u96f6\uff0c\u56e0\u6b64 b \u5c31\u88ab\u8d4b\u503c\u4e3a false \u3002\u56e0\u6b64\uff0c\u5f53 std::cout \u6253\u5370 b \u7684\u65f6\u5019\uff0c\u4f1a\u6253\u53700\u3002 \u4e3a\u4e86\u8ba9 std::cin \u80fd\u591f\u63a5\u53d7 \u201cfalse\u201d \u548c \u201ctrue\u201d \u4f5c\u4e3a\u8f93\u5165\uff0c std::boolalpha \u5fc5\u987b\u88ab\u6253\u5f00\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { bool b {}; std :: cout << \"Enter a boolean value: \" ; // \u5141\u8bb8\u7528\u6237\u8f93\u5165 'true' \u6216 'false' \u4f5c\u4e3a\u5e03\u5c14\u503c std :: cin >> std :: boolalpha ; std :: cin >> b ; std :: cout << \"You entered: \" << b << '\\n' ; return 0 ; } \u4e0d\u8fc7\uff0c\u5f53 std::boolalpha \u6253\u5361\u540e\uff0c\u201c0\u201d \u548c \u201c1\u201d \u5c31\u4e0d\u4f1a\u518d\u88ab\u770b\u51fa\u5e03\u5c14\u7c7b\u578b\u4e86\u3002 \u5e03\u5c14\u7c7b\u578b\u7684\u8fd4\u56de\u503c \u5e03\u5c14\u503c\u5e38\u5907\u7528\u4f5c\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u7528\u6765\u68c0\u67e5\u67d0\u4ef6\u4e8b(\u7269)\u662f\u5426\u4e3atrue\u3002\u8fd9\u79cd\u51fd\u6570\u547d\u540d\u65f6\u901a\u5e38\u4ee5 is \u5f00\u5934\uff08\u4f8b\u5982 isEqual \uff09\u6216 has \uff08\u4f8b\u5982 hasCommonDivisor \uff09\u3002 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u548c\u4e0a\u9762\u6709\u4e9b\u7c7b\u4f3c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // \u5982\u679c x \u548c y \u76f8\u7b49\u5219\u8fd4\u56de true \u5426\u5219\u8fd4\u56de false bool isEqual ( int x , int y ) { return ( x == y ); // \u5982\u679c x \u548c y \u76f8\u7b49\uff0coperator== \u5219\u8fd4\u56de true \u5426\u5219\u8fd4\u56de false } int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another integer: \" ; int y {}; std :: cin >> y ; std :: cout << std :: boolalpha ; // print bools as true or false std :: cout << x << \" and \" << y << \" are equal? \" ; std :: cout << isEqual ( x , y ); // will return true or false return 0 ; } \u4e24\u6b21\u8fd0\u884c\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 Enter an integer: 5 Enter another integer: 5 5 and 5 are equal? true 1 2 3 Enter an integer: 6 Enter another integer: 4 6 and 4 are equal? false \u539f\u7406\u662f\u4ec0\u4e48\u5462\uff1f\u9996\u5148\u6211\u4eec\u4ece\u8f93\u5165\u83b7\u53d6 x \u548c y \u7684\u503c\u3002\u63a5\u4e0b\u6765\uff0c\u8868\u8fbe\u5f0f isEqual(x, y) \u4f1a\u8fdb\u884c\u6c42\u503c\u3002\u5728\u7b2c\u4e00\u6b21\u8fd0\u884c\u65f6\uff0c\u6267\u884c\u7684\u51fd\u6570\u76f8\u5f53\u4e8e isEqual(5, 5) \u3002\u5728\u51fd\u6570\u4e2d\uff0c 5 == 5 \u88ab\u6c42\u503c\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f true \u3002\u968f\u540e true \u88ab\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u5e76\u901a\u8fc7 std::cout \u88ab\u6253\u5370\u51fa\u6765\u3002\u5728\u7b2c\u4e8c\u6b21\u8fd0\u884c\u65f6\uff0c\u8c03\u7528\u7684\u51fd\u6570\u76f8\u5f53\u4e8e isEqual(6, 4) \uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u4e3a false \u3002 \u4f7f\u7528\u5e03\u5c14\u503c\u9700\u8981\u719f\u6089\u4e00\u4e0b\uff0c\u4e0d\u8fc7\u5f53\u4f60\u4e60\u60ef\u4e4b\u540e\uff0c\u4f1a\u975e\u5e38\u559c\u6b22\u5b83\u8868\u73b0\u51fa\u6765\u7684\u7b80\u6d01\uff01\u5e03\u5c14\u503c\u4e5f\u662f\u7f16\u7a0b\u4e2d\u975e\u5e38\u5927\u7684\u4e00\u90e8\u5206\u2014\u2014\u4f60\u4f7f\u7528\u5e03\u5c14\u503c\u7684\u60c5\u51b5\u6bd4\u5176\u4ed6\u57fa\u672c\u7c7b\u578b\u52a0\u8d77\u6765\u90fd\u591a\u3002 \u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\u4e2d\u7ee7\u7eed\u8ba8\u8bba\u5e03\u5c14\u503c\u3002","title":"4.9 - \u5e03\u5c14\u503c"},{"location":"4-9-Boolean-values/#_1","text":"\u5e03\u5c14\u53d8\u91cf\u53ea\u6709\u4e24\u79cd\u53ef\u80fd\u7684\u503c\uff1a true \u548c false \u3002 \u58f0\u660e\u5e03\u5c14\u7c7b\u578b\u7684\u53d8\u91cf\u9700\u8981\u4f7f\u7528\u5173\u952e\u5b57 bool \u3002 1 bool b ; \u521d\u59cb\u5316\u5e03\u5c14\u7c7b\u578b\u7684\u53d8\u91cf\u6216\u4e3a\u5176\u8d4b\u503c\uff0c\u9700\u8981\u4f7f\u7528 true \u6216 false \u3002 1 2 3 4 bool b1 { true }; bool b2 { false }; b1 = false ; bool b3 {}; // \u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a false \u548c\u4e00\u5143\u8d1f\u53f7\u8fd0\u7b97\u7b26( - )\u53ef\u4ee5\u88ab\u7528\u6765\u5c06\u4e00\u4e2a\u6b63\u6570\u53d8\u6210\u8d1f\u6570\u4e00\u6837\uff0c\u4f7f\u7528\u903b\u8f91 \u975e \u8fd0\u7b97\u7b26( ! )\u53ef\u4ee5\u5c06\u5e03\u5c14\u503c true \u53d8\u6210 false \u6216\u8005\u628a false \u53d8\u6210 true \uff1a 1 2 bool b1 { ! true }; // b1 \u88ab\u521d\u59cb\u5316\u4e3a false bool b2 { ! false }; // b2 \u88ab\u521d\u59cb\u5316\u4e3a true \u5e03\u5c14\u503c\u901a\u5e38\u5e76\u4e0d\u4f1a\u4ee5 \u201ctrue\u201d \u6216 \u201cfalse\u201d\u6765\u5b58\u50a8\u3002\u5b9e\u9645\u4e0a\u5b83\u662f\u4ee5\u6574\u578b\u6765\u5b58\u50a8\u7684\uff1a true \u4f1a\u53d8\u62101\uff0c false \u4f1a\u53d8\u62100\u3002\u7c7b\u4f3c\u5730\uff0c\u5f53\u5e03\u5c14\u7c7b\u578b\u6c42\u503c\u65f6\uff0c\u6c42\u503c\u7684\u5b9e\u9645\u7ed3\u679c\u4e5f\u5e76\u4e0d\u662f\u201ctrue\u201d \u6216 \u201cfalse\u201d\u3002\u901a\u5e38\u5b83\u4eec\u4f1a\u6c42\u503c\u6210_0_ (false) \u6216 1 (true)\u3002\u56e0\u4e3a\u5e03\u5c14\u7c7b\u578b\u5b9e\u9645\u4e0a\u662f\u4ee5\u6574\u578b\u5b58\u50a8\u7684\uff0c\u56e0\u6b64\u5b83\u5c5e\u4e8e\u4e00\u79cd\u6574\u5f62\u7684\u7c7b\u578b(integral type)\u3002","title":"\u5e03\u5c14\u53d8\u91cf"},{"location":"4-9-Boolean-values/#_2","text":"\u5f53\u4f7f\u7528 std::cout \u6253\u5370\u5e03\u5c14\u53d8\u91cf\u65f6\uff0c std::cout \u4f1a\u5c06 false \u6253\u5370\u6210 0\uff0c\u800c\u628a true \u6253\u5370\u62101\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { std :: cout << true << '\\n' ; // true evaluates to 1 std :: cout << ! true << '\\n' ; // !true evaluates to 0 bool b { false }; std :: cout << b << '\\n' ; // b is false, which evaluates to 0 std :: cout << ! b << '\\n' ; // !b is true, which evaluates to 1 return 0 ; } Outputs: 1 2 3 4 1 0 0 1 \u5982\u679c\u4f60\u5e0c\u671b std::cout \u6253\u5370 \u201ctrue\u201d \u6216 \u201cfalse\u201d \u800c\u4e0d\u662f 0 \u6216 1\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 std::boolalpha \u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { std :: cout << true << '\\n' ; std :: cout << false << '\\n' ; std :: cout << std :: boolalpha ; // print bools as true or false std :: cout << true << '\\n' ; std :: cout << false << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 4 1 0 true false \u4f60\u53ef\u4ee5\u4f7f\u7528 std::noboolalpha \u6765\u5173\u95ed\u6b64\u529f\u80fd\u3002","title":"\u6253\u5370\u5e03\u5c14\u53d8\u91cf"},{"location":"4-9-Boolean-values/#_3","text":"\u4f60\u4e0d\u80fd\u7528\u62ec\u53f7\u521d\u59cb\u5316\u548c\u4e00\u4e2a\u6574\u578b\u6570\u6765\u521d\u59cb\u5316\u5e03\u5c14\u53d8\u91cf\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { bool b { 4 }; // \u9519\u8bef\uff1a: \u4e0d\u5141\u8bb8\u7f29\u7a84\u8f6c\u6362 std :: cout << b ; return 0 ; } (\u6ce8\u610f\uff1a\u6709\u4e9b\u7248\u672c\u7684g++\u5e76\u4e0d\u4f1a\u6709\u4e0a\u8ff0\u9650\u5236\uff09 \u4e0d\u8fc7\uff0c\u5728\u5176\u4ed6\u573a\u666f\u4e0b\u6574\u578b\u662f\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\u7684\u3002\u6574\u578b\u503c0\u4f1a\u88ab\u8f6c\u6362\u4e3a false \uff0c\u800c\u5176\u4ed6\u6574\u578b\u4f1a\u88ab\u8f6c\u6362\u4e3a true \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << std :: boolalpha ; // print bools as true or false bool b1 = 4 ; // \u62f7\u8d1d\u521d\u59cb\u5316\u5141\u8bb8\u9690\u5f0f\u8f6c\u6362 std :: cout << b1 << '\\n' ; bool b2 = 0 ; // \u62f7\u8d1d\u521d\u59cb\u5316\u5141\u8bb8\u9690\u5f0f\u8f6c\u6362 std :: cout << b2 << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 2 true false \u6ce8\u610f\uff1a bool b1 = 4; \u53ef\u4ee5\u80fd\u4f1a\u4ea7\u751f\u8b66\u544a\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f60\u9700\u8981\u5173\u95ed\u7f16\u8bd1\u7684\u201d\u5c06\u544a\u8b66\u5f53\u505a\u9519\u8bef\u5904\u7406\u201c\u529f\u80fd\uff0c\u8fd9\u6837\u4f60\u731c\u5462\u7f16\u8bd1\u4e0a\u8ff0\u4ee3\u7801\u3002","title":"\u6574\u578b\u8f6c\u6362\u4e3a\u5e03\u5c14\u7c7b\u578b"},{"location":"4-9-Boolean-values/#_4","text":"\u4f7f\u7528 std::cin \u6765\u8f93\u5165\u5e03\u5c14\u7c7b\u578b\u5e38\u5e38\u4f1a\u8ba9\u65b0\u624b\u7a0b\u5e8f\u5458\u6293\u72c2\u3002 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { bool b {}; // \u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a false std :: cout << \"Enter a boolean value: \" ; std :: cin >> b ; std :: cout << \"You entered: \" << b << '\\n' ; return 0 ; } 1 2 Enter a Boolean value: true You entered: 0 \u4ec0\u4e48\uff01\u600e\u4e48\u4f1a\u8fd9\u6837\uff1f \u5b9e\u9645\u4e0a\uff0c std::cin \u53ea\u80fd\u591f\u63a5\u6536\u4e24\u79cd\u5e03\u5c14\u7c7b\u578b\u8f93\u5165\uff1a0\u548c1\uff08\u800c\u4e0d\u662f true \u6216 false \uff09\u3002\u4efb\u4f55\u5176\u4ed6\u7684\u8f93\u5165\u90fd\u4f1a\u5bfc\u81f4 std::cin \u51fa\u9519\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u8f93\u5165\u4e86true\uff0c\u56e0\u6b64\u5b9e\u9645\u4e0a std::cin \u51fa\u9519\u4e86\uff0c\u8f93\u5165\u51fa\u9519\u540e\uff0c\u63a5\u53d7\u8f93\u5165\u7684\u53d8\u91cf\u4f1a\u88ab\u6e05\u96f6\uff0c\u56e0\u6b64 b \u5c31\u88ab\u8d4b\u503c\u4e3a false \u3002\u56e0\u6b64\uff0c\u5f53 std::cout \u6253\u5370 b \u7684\u65f6\u5019\uff0c\u4f1a\u6253\u53700\u3002 \u4e3a\u4e86\u8ba9 std::cin \u80fd\u591f\u63a5\u53d7 \u201cfalse\u201d \u548c \u201ctrue\u201d \u4f5c\u4e3a\u8f93\u5165\uff0c std::boolalpha \u5fc5\u987b\u88ab\u6253\u5f00\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { bool b {}; std :: cout << \"Enter a boolean value: \" ; // \u5141\u8bb8\u7528\u6237\u8f93\u5165 'true' \u6216 'false' \u4f5c\u4e3a\u5e03\u5c14\u503c std :: cin >> std :: boolalpha ; std :: cin >> b ; std :: cout << \"You entered: \" << b << '\\n' ; return 0 ; } \u4e0d\u8fc7\uff0c\u5f53 std::boolalpha \u6253\u5361\u540e\uff0c\u201c0\u201d \u548c \u201c1\u201d \u5c31\u4e0d\u4f1a\u518d\u88ab\u770b\u51fa\u5e03\u5c14\u7c7b\u578b\u4e86\u3002","title":"\u8f93\u5165\u5e03\u5c14\u503c"},{"location":"4-9-Boolean-values/#_5","text":"\u5e03\u5c14\u503c\u5e38\u5907\u7528\u4f5c\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u7528\u6765\u68c0\u67e5\u67d0\u4ef6\u4e8b(\u7269)\u662f\u5426\u4e3atrue\u3002\u8fd9\u79cd\u51fd\u6570\u547d\u540d\u65f6\u901a\u5e38\u4ee5 is \u5f00\u5934\uff08\u4f8b\u5982 isEqual \uff09\u6216 has \uff08\u4f8b\u5982 hasCommonDivisor \uff09\u3002 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u548c\u4e0a\u9762\u6709\u4e9b\u7c7b\u4f3c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include // \u5982\u679c x \u548c y \u76f8\u7b49\u5219\u8fd4\u56de true \u5426\u5219\u8fd4\u56de false bool isEqual ( int x , int y ) { return ( x == y ); // \u5982\u679c x \u548c y \u76f8\u7b49\uff0coperator== \u5219\u8fd4\u56de true \u5426\u5219\u8fd4\u56de false } int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another integer: \" ; int y {}; std :: cin >> y ; std :: cout << std :: boolalpha ; // print bools as true or false std :: cout << x << \" and \" << y << \" are equal? \" ; std :: cout << isEqual ( x , y ); // will return true or false return 0 ; } \u4e24\u6b21\u8fd0\u884c\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 Enter an integer: 5 Enter another integer: 5 5 and 5 are equal? true 1 2 3 Enter an integer: 6 Enter another integer: 4 6 and 4 are equal? false \u539f\u7406\u662f\u4ec0\u4e48\u5462\uff1f\u9996\u5148\u6211\u4eec\u4ece\u8f93\u5165\u83b7\u53d6 x \u548c y \u7684\u503c\u3002\u63a5\u4e0b\u6765\uff0c\u8868\u8fbe\u5f0f isEqual(x, y) \u4f1a\u8fdb\u884c\u6c42\u503c\u3002\u5728\u7b2c\u4e00\u6b21\u8fd0\u884c\u65f6\uff0c\u6267\u884c\u7684\u51fd\u6570\u76f8\u5f53\u4e8e isEqual(5, 5) \u3002\u5728\u51fd\u6570\u4e2d\uff0c 5 == 5 \u88ab\u6c42\u503c\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f true \u3002\u968f\u540e true \u88ab\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u5e76\u901a\u8fc7 std::cout \u88ab\u6253\u5370\u51fa\u6765\u3002\u5728\u7b2c\u4e8c\u6b21\u8fd0\u884c\u65f6\uff0c\u8c03\u7528\u7684\u51fd\u6570\u76f8\u5f53\u4e8e isEqual(6, 4) \uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u4e3a false \u3002 \u4f7f\u7528\u5e03\u5c14\u503c\u9700\u8981\u719f\u6089\u4e00\u4e0b\uff0c\u4e0d\u8fc7\u5f53\u4f60\u4e60\u60ef\u4e4b\u540e\uff0c\u4f1a\u975e\u5e38\u559c\u6b22\u5b83\u8868\u73b0\u51fa\u6765\u7684\u7b80\u6d01\uff01\u5e03\u5c14\u503c\u4e5f\u662f\u7f16\u7a0b\u4e2d\u975e\u5e38\u5927\u7684\u4e00\u90e8\u5206\u2014\u2014\u4f60\u4f7f\u7528\u5e03\u5c14\u503c\u7684\u60c5\u51b5\u6bd4\u5176\u4ed6\u57fa\u672c\u7c7b\u578b\u52a0\u8d77\u6765\u90fd\u591a\u3002 \u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8bfe\u4e2d\u7ee7\u7eed\u8ba8\u8bba\u5e03\u5c14\u503c\u3002","title":"\u5e03\u5c14\u7c7b\u578b\u7684\u8fd4\u56de\u503c"},{"location":"4-x-Chapter-4-summary-and-quiz/","tags":["summary"],"text":"\u7ae0\u8282\u56de\u987e 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb \u5185\u5b58\u7684\u6700\u5c0f\u5355\u5143\u79f0\u4e3a \u4f4d \u3002\u5185\u5b58\u53ef\u5bfb\u627e\u7684\u6700\u5c0f\u5355\u4f4d\u662f \u5b57\u8282 \uff0c\u73b0\u4ee3\u6807\u51c6\u4e00\u4e2a\u5b57\u8282\u7b49\u4e8e8\u4e2a\u4f4d\u3002 \u4e00\u79cd \u6570\u636e\u7c7b\u578b \u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u4f55\u5c06\u5185\u5b58\u4e2d\u7684\u6570\u636e\u89e3\u6790\u6210\u6709\u610f\u4e49\u7684\u503c\u3002 C++ \u652f\u6301\u591a\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6d6e\u70b9\u6570\u3001\u6574\u578b\u3001\u5e03\u5c14\u7c7b\u578b\u3001\u5b57\u7b26\u3001null\u6307\u9488\u548c void\u3002 4.2 - Void Void \u7528\u4e8e\u8868\u793a\u65e0\u7c7b\u578b\u3002\u5176\u6700\u4e3b\u8981\u7684\u4f5c\u7528\u662f\u8868\u660e\u51fd\u6570\u65e0\u8fd4\u56de\u503c\u3002 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 \u4e0d\u540c\u6570\u636e\u7c7b\u578b\u5360\u636e\u7684\u5185\u5b58\u5927\u5c0f\u4e5f\u4e0d\u540c\uff0c\u540c\u65f6\u4e5f\u4f1a\u6536\u5230\u673a\u5668\u4f53\u7cfb\u7ed3\u6784\u7684\u5f71\u54cd\u3002\u53c2\u8003 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 \u4e2d\u7684\u8868\u683c\u4e86\u89e3\u6bcf\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\u5360\u7528\u7684\u7a7a\u95f4\u3002 sizeof \u8fd0\u7b97\u7b26\u53ef\u4ee5\u8fd4\u56de\u67d0\u4e2a\u7c7b\u578b\u6240\u5360\u7528\u7684\u5b57\u8282\u6570\u3002 4.4 - \u6709\u7b26\u53f7\u6574\u578b \u6709\u7b26\u53f7\u6574\u578b \u7528\u4e8e\u4fdd\u5b58\u6b63\u6570\u548c\u8d1f\u6570\uff0c\u4e5f\u5305\u62ec0\u3002\u4e00\u4e2a\u7279\u5b9a\u6570\u636e\u7c7b\u578b\u80fd\u591f\u4fdd\u5b58\u7684\u503c\uff0c\u79f0\u4e3a\u8be5\u7c7b\u578b\u7684\u8303\u56f4\u3002\u5f53\u4f7f\u7528\u6574\u578b\u65f6\uff0c\u4e00\u5b9a\u8981\u6ce8\u610f\u6ea2\u51fa\u548c\u6574\u578b\u96640\u95ee\u9898\u3002 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 \u65e0\u7b26\u53f7\u6574\u578b \u53ea\u80fd\u4fdd\u5b58\u6b63\u6574\u6570\uff0c\u901a\u5e38\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u5b83\uff0c\u9664\u975e\u662f\u7528\u4e8e\u4f4d\u8fd0\u7b97\u3002 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b \u662f\u5177\u6709\u786e\u5b9a\u5927\u5c0f\u7684\u6574\u578b\uff0c\u4f46\u662f\u5e76\u4e0d\u662f\u6240\u6709\u7684\u4f53\u7cfb\u7ed3\u6784\u90fd\u652f\u6301\u3002\u901f\u5ea6\u4f18\u5148\u548c\u5927\u5c0f\u4f18\u5148\u7684\u6574\u578b\u662f\u4fdd\u8bc1\u67d0\u5176\u5177\u6709\u67d0\u5927\u5c0f\u524d\u63d0\u4e0b\u7684\u6700\u5feb\u7684\u6216\u6700\u5c0f\u7684\u6574\u578b\u3002 std::int8_t \u548c std::uint8_t \u4e00\u822c\u4e0d\u8981\u4f7f\u7528\uff0c\u5b83\u4eec\u7684\u884c\u4e3a\u66f4\u50cf\u5b57\u7b26\u800c\u4e0d\u662f\u6574\u578b\u3002 size_t \u662f\u4e00\u79cd\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u5b83\u7528\u4e8e\u8868\u793a\u5bf9\u8c61\u7684\u5927\u5c0f\u6216\u957f\u5ea6\u3002 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 \u79d1\u5b66\u8ba1\u6570\u6cd5\u662f\u4e00\u79cd\u8868\u793a\u5927\u6570\u7684\u7b80\u5355\u8bb0\u6cd5\u3002C++ \u652f\u6301\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a\u6d6e\u70b9\u6570\u3002\u79d1\u5b66\u8ba1\u6570\u6cd5\u4e2d\uff0ce\u524d\u9762\u7684\u90e8\u5206\u79f0\u4e3a\u6709\u6548\u6570\u5b57\u3002 4.8 - \u6d6e\u70b9\u6570 \u6d6e\u70b9\u6570 \u7c7b\u578b\u7528\u4e8e\u4fdd\u5b58\u5b9e\u6570\uff08\u5305\u62ec\u54ea\u4e9b\u6709\u5c0f\u6570\u90e8\u5206\u7684\u6570\uff09\u3002\u6570\u5b57\u7684\u7cbe\u5ea6\u6307\u7684\u662f\u5728\u4e0d\u4e22\u5931\u7cbe\u5ea6\u7684\u524d\u63d0\u4e0b\uff0c\u53ef\u4ee5\u8868\u793a\u591a\u5c11\u4e2a\u6709\u6548\u6570\u5b57\u3002 \u820d\u5165\u8bef\u5dee \u51fa\u73b0\u5728\u8fc7\u591a\u7684\u6709\u6548\u6570\u5b57\u88ab\u5b58\u653e\u5728\u65e0\u6cd5\u4fdd\u5b58\u8fd9\u4e48\u591a\u6709\u6548\u6570\u5b57\u7684\u6d6e\u70b9\u6570\u4e2d\u3002\u820d\u5165\u8bef\u5dee\u4e00\u81f4\u5b58\u5728\uff0c\u5373\u4f7f\u662f\u50cf 0.1 \u8fd9\u6837\u7b80\u5355\u7684\u6570\u3002\u56e0\u6b64\uff0c\u4f60\u4e0d\u80fd\u5c06\u6d6e\u70b9\u6570\u8fdb\u884c\u76f4\u63a5\u6bd4\u8f83\u3002 4.9 - \u5e03\u5c14\u503c \u5e03\u5c14 \u7c7b\u578b\u7528\u4e8e\u5b58\u653e true \u548c false \u3002 4.10 - if \u8bed\u53e5\u7b80\u4ecb If \u8bed\u53e5 4.10 - if \u8bed\u53e5\u7b80\u4ecb \u5141\u8bb8\u6211\u4eec\u6ee1\u8db3\u67d0\u79cd\u6761\u4ef6\u65f6\u624d\u6267\u884c\u4e00\u6761\u6216\u591a\u6761\u8bed\u53e5\u3002if \u8bed\u53e5\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u4f1a\u88ab\u89e3\u6790\u4e3a\u5e03\u5c14\u503c\u3002 4.11 - \u5b57\u7b26 \u5b57\u7b26 \u5b58\u653e\u7684\u503c\u4f1a\u88ab\u89e3\u6790\u4e3a ASCII \u5b57\u6bcd\u3002\u5f53\u4f7f\u7528\u5b57\u7b26\u65f6\uff0c\u5c0f\u5fc3\u4e0d\u8981\u5c06 ASCII \u7801\u503c\u548c\u6570\u5b57\u641e\u6df7\u3002\u5982\u679c\u9700\u8981\u628a\u5b57\u7b26\u5f53\u6574\u578b\u6253\u5370\u5904\u7406\uff0c\u9700\u8981\u4f7f\u7528 static_cast \u3002 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast \u5c16\u62ec\u53f7\u5728 C++ \u4e2d\u901a\u5e38\u7528\u6765\u8868\u793a\u53c2\u6570\u5316\u7c7b\u578b\u3002\u5728 static_cast \u4e2d\uff0c\u5c16\u62ec\u53f7\u7684\u5185\u5bb9\u6307\u5b9a\u4e86\u76ee\u6807\u7c7b\u578b\u3002(\u4f8b\u5982 static_cast(x) \u4f1a\u628a x \u8f6c\u6362\u4e3a int )\u3002 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \u5e38\u91cf\u6307\u7684\u662f\u503c\u4e0d\u4f1a\u6539\u53d8\u7684\u53d8\u91cf\u3002C++ \u652f\u6301\u4e24\u79cd\u5e38\u91cf\uff1a\u5b57\u9762\u91cf\u5e38\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \u7b26\u53f7\u5e38\u91cf \u6307\u7684\u662f\u7ed9\u4e0e\u5e38\u91cf\u7684\u540d\u5b57\u3002\u5e38\u91cf\u662f\u7b26\u53f7\u53d8\u91cf\u7684\u4e00\u79cd\uff0c\u4f7f\u7528\u66ff\u6362\u6587\u672c\u7684\u7c7b\u5bf9\u8c61\u5b8f\u4e5f\u662f\u7b26\u53f7\u5e38\u91cf\uff1b 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr \u5e38\u91cf\u8868\u8fbe\u5f0f \u662f\u4e00\u4e2a\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\u8868\u8fbe\u5f0f\u3002 \u7f16\u8bd1\u65f6\u5e38\u91cf \u662f\u5176\u503c\u5728\u7f16\u8bd1\u65f6\u5df2\u77e5\u7684\u5e38\u91cf\u3002 \u8fd0\u884c\u65f6\u5e38\u91cf \u662f\u5728\u8fd0\u884c\u65f6\u4e4b\u524d\u4e0d\u77e5\u9053\u5176\u521d\u59cb\u5316\u503c\u7684\u5e38\u91cf\u3002constexprvariable\u5fc5\u987b\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 4.15 - \u5b57\u9762\u91cf \u5b57\u9762\u91cf \u662f\u76f4\u63a5\u5d4c\u5165\u5230\u4ee3\u7801\u4e2d\u7684\u5e38\u91cf\u3002\u5b57\u9762\u91cf\u662f\u6709\u7c7b\u578b\u7684\uff0c\u800c\u5b57\u9762\u91cf\u540e\u7f00\u53ef\u4ee5\u7528\u6765\u4fee\u6539\u5b57\u9762\u91cf\u7684\u9ed8\u8ba4\u7c7b\u578b\u3002 \u9b54\u9b3c\u6570\u5b57\u6307\u7684\u662f\u542b\u4e49\u4e0d\u660e\u786e\u6216\u662f\u7a0d\u540e\u4f1a\u88ab\u4fee\u6539\u7684\u5b57\u9762\u91cf\uff08\u901a\u5e38\u662f\u6570\u5b57\uff09\uff0c\u4e0d\u8981\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u9b54\u672f\u6570\u5b57\uff0c\u5e94\u8be5\u7528\u7b26\u53f7\u5e38\u91cf\u4ee3\u66ff\u5b83\u3002 4.16 - \u6570\u503c\u7cfb\u7edf\uff08\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236\uff09 \u5728\u65e5\u5e38\u751f\u6d3b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5341\u8fdb\u5236\u6570\uff0c\u5b83\u670910\u4f4d\u6570\u5b57\u3002\u8ba1\u7b97\u673a\u4f7f\u7528\u4e8c\u8fdb\u5236\uff0c\u5b83\u53ea\u67092\u4f4d\u6570\u5b57\u3002C++\u8fd8\u652f\u6301\u516b\u8fdb\u5236(\u4ee58\u4e3a\u57fa\u6570)\u548c\u5341\u516d\u8fdb\u5236(\u4ee516\u4e3a\u57fa\u6570)\u3002\u8fd9\u4e9b\u90fd\u662f\u6570\u5b57\u7cfb\u7edf\u7684\u4f8b\u5b50\uff0c\u662f\u7528\u6765\u8868\u793a\u6570\u5b57\u7684\u7b26\u53f7(\u6570\u5b57)\u7684\u96c6\u5408\u3002 4.17 - std::string \u7b80\u4ecb 4.18 - std::string_view \u7b80\u4ecb \u5b57\u7b26\u4e32\u662f\u7528\u4e8e\u8868\u793a\u6587\u672c(\u5982\u540d\u79f0\u3001\u5355\u8bcd\u548c\u53e5\u5b50)\u7684\u987a\u5e8f\u5b57\u7b26\u7684\u96c6\u5408\u3002\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u603b\u662f\u653e\u5728\u53cc\u5f15\u53f7\u4e4b\u95f4\u3002C++\u4e2d\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u662fC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u5b83\u7684\u7c7b\u578b\u6bd4\u8f83\u5947\u602a\uff0c\u5f88\u96be\u5904\u7406\u3002 std::string \u63d0\u4f9b\u4e86\u7b80\u4fbf\u3001\u5b89\u5168\u7684\u5904\u7406\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u3002\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u603b\u662f\u653e\u5728\u53cc\u5f15\u53f7\u4e2d\u3002\u4f7f\u7528 std::string \u9700\u8981\u5305\u542b \u5934\u6587\u4ef6\uff1b std::string_view \u4e3a\u5df2\u7ecf\u5b58\u5728\u7684\u5b57\u7b26\u4e32\uff08C\u98ce\u683c\u5b57\u7b26\u4e32\u3001 std::string \u6216\u5b57\u7b26\u6570\u7ec4\uff09\u63d0\u4f9b\u4e86\u4e00\u79cd\u53ea\u8bfb\u7684\u8bbf\u95ee\u65b9\u5f0f\uff0c\u800c\u4e0d\u9700\u521b\u5efa\u62f7\u8d1d\u3002","title":"4.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u57fa\u672c\u6570\u636e\u7c7b\u578b"},{"location":"4-x-Chapter-4-summary-and-quiz/#_1","text":"4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb \u5185\u5b58\u7684\u6700\u5c0f\u5355\u5143\u79f0\u4e3a \u4f4d \u3002\u5185\u5b58\u53ef\u5bfb\u627e\u7684\u6700\u5c0f\u5355\u4f4d\u662f \u5b57\u8282 \uff0c\u73b0\u4ee3\u6807\u51c6\u4e00\u4e2a\u5b57\u8282\u7b49\u4e8e8\u4e2a\u4f4d\u3002 \u4e00\u79cd \u6570\u636e\u7c7b\u578b \u544a\u8bc9\u7f16\u8bd1\u5668\u5982\u4f55\u5c06\u5185\u5b58\u4e2d\u7684\u6570\u636e\u89e3\u6790\u6210\u6709\u610f\u4e49\u7684\u503c\u3002 C++ \u652f\u6301\u591a\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6d6e\u70b9\u6570\u3001\u6574\u578b\u3001\u5e03\u5c14\u7c7b\u578b\u3001\u5b57\u7b26\u3001null\u6307\u9488\u548c void\u3002 4.2 - Void Void \u7528\u4e8e\u8868\u793a\u65e0\u7c7b\u578b\u3002\u5176\u6700\u4e3b\u8981\u7684\u4f5c\u7528\u662f\u8868\u660e\u51fd\u6570\u65e0\u8fd4\u56de\u503c\u3002 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 \u4e0d\u540c\u6570\u636e\u7c7b\u578b\u5360\u636e\u7684\u5185\u5b58\u5927\u5c0f\u4e5f\u4e0d\u540c\uff0c\u540c\u65f6\u4e5f\u4f1a\u6536\u5230\u673a\u5668\u4f53\u7cfb\u7ed3\u6784\u7684\u5f71\u54cd\u3002\u53c2\u8003 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 \u4e2d\u7684\u8868\u683c\u4e86\u89e3\u6bcf\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\u5360\u7528\u7684\u7a7a\u95f4\u3002 sizeof \u8fd0\u7b97\u7b26\u53ef\u4ee5\u8fd4\u56de\u67d0\u4e2a\u7c7b\u578b\u6240\u5360\u7528\u7684\u5b57\u8282\u6570\u3002 4.4 - \u6709\u7b26\u53f7\u6574\u578b \u6709\u7b26\u53f7\u6574\u578b \u7528\u4e8e\u4fdd\u5b58\u6b63\u6570\u548c\u8d1f\u6570\uff0c\u4e5f\u5305\u62ec0\u3002\u4e00\u4e2a\u7279\u5b9a\u6570\u636e\u7c7b\u578b\u80fd\u591f\u4fdd\u5b58\u7684\u503c\uff0c\u79f0\u4e3a\u8be5\u7c7b\u578b\u7684\u8303\u56f4\u3002\u5f53\u4f7f\u7528\u6574\u578b\u65f6\uff0c\u4e00\u5b9a\u8981\u6ce8\u610f\u6ea2\u51fa\u548c\u6574\u578b\u96640\u95ee\u9898\u3002 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 \u65e0\u7b26\u53f7\u6574\u578b \u53ea\u80fd\u4fdd\u5b58\u6b63\u6574\u6570\uff0c\u901a\u5e38\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u5b83\uff0c\u9664\u975e\u662f\u7528\u4e8e\u4f4d\u8fd0\u7b97\u3002 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b \u662f\u5177\u6709\u786e\u5b9a\u5927\u5c0f\u7684\u6574\u578b\uff0c\u4f46\u662f\u5e76\u4e0d\u662f\u6240\u6709\u7684\u4f53\u7cfb\u7ed3\u6784\u90fd\u652f\u6301\u3002\u901f\u5ea6\u4f18\u5148\u548c\u5927\u5c0f\u4f18\u5148\u7684\u6574\u578b\u662f\u4fdd\u8bc1\u67d0\u5176\u5177\u6709\u67d0\u5927\u5c0f\u524d\u63d0\u4e0b\u7684\u6700\u5feb\u7684\u6216\u6700\u5c0f\u7684\u6574\u578b\u3002 std::int8_t \u548c std::uint8_t \u4e00\u822c\u4e0d\u8981\u4f7f\u7528\uff0c\u5b83\u4eec\u7684\u884c\u4e3a\u66f4\u50cf\u5b57\u7b26\u800c\u4e0d\u662f\u6574\u578b\u3002 size_t \u662f\u4e00\u79cd\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u5b83\u7528\u4e8e\u8868\u793a\u5bf9\u8c61\u7684\u5927\u5c0f\u6216\u957f\u5ea6\u3002 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 \u79d1\u5b66\u8ba1\u6570\u6cd5\u662f\u4e00\u79cd\u8868\u793a\u5927\u6570\u7684\u7b80\u5355\u8bb0\u6cd5\u3002C++ \u652f\u6301\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a\u6d6e\u70b9\u6570\u3002\u79d1\u5b66\u8ba1\u6570\u6cd5\u4e2d\uff0ce\u524d\u9762\u7684\u90e8\u5206\u79f0\u4e3a\u6709\u6548\u6570\u5b57\u3002 4.8 - \u6d6e\u70b9\u6570 \u6d6e\u70b9\u6570 \u7c7b\u578b\u7528\u4e8e\u4fdd\u5b58\u5b9e\u6570\uff08\u5305\u62ec\u54ea\u4e9b\u6709\u5c0f\u6570\u90e8\u5206\u7684\u6570\uff09\u3002\u6570\u5b57\u7684\u7cbe\u5ea6\u6307\u7684\u662f\u5728\u4e0d\u4e22\u5931\u7cbe\u5ea6\u7684\u524d\u63d0\u4e0b\uff0c\u53ef\u4ee5\u8868\u793a\u591a\u5c11\u4e2a\u6709\u6548\u6570\u5b57\u3002 \u820d\u5165\u8bef\u5dee \u51fa\u73b0\u5728\u8fc7\u591a\u7684\u6709\u6548\u6570\u5b57\u88ab\u5b58\u653e\u5728\u65e0\u6cd5\u4fdd\u5b58\u8fd9\u4e48\u591a\u6709\u6548\u6570\u5b57\u7684\u6d6e\u70b9\u6570\u4e2d\u3002\u820d\u5165\u8bef\u5dee\u4e00\u81f4\u5b58\u5728\uff0c\u5373\u4f7f\u662f\u50cf 0.1 \u8fd9\u6837\u7b80\u5355\u7684\u6570\u3002\u56e0\u6b64\uff0c\u4f60\u4e0d\u80fd\u5c06\u6d6e\u70b9\u6570\u8fdb\u884c\u76f4\u63a5\u6bd4\u8f83\u3002 4.9 - \u5e03\u5c14\u503c \u5e03\u5c14 \u7c7b\u578b\u7528\u4e8e\u5b58\u653e true \u548c false \u3002 4.10 - if \u8bed\u53e5\u7b80\u4ecb If \u8bed\u53e5 4.10 - if \u8bed\u53e5\u7b80\u4ecb \u5141\u8bb8\u6211\u4eec\u6ee1\u8db3\u67d0\u79cd\u6761\u4ef6\u65f6\u624d\u6267\u884c\u4e00\u6761\u6216\u591a\u6761\u8bed\u53e5\u3002if \u8bed\u53e5\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u4f1a\u88ab\u89e3\u6790\u4e3a\u5e03\u5c14\u503c\u3002 4.11 - \u5b57\u7b26 \u5b57\u7b26 \u5b58\u653e\u7684\u503c\u4f1a\u88ab\u89e3\u6790\u4e3a ASCII \u5b57\u6bcd\u3002\u5f53\u4f7f\u7528\u5b57\u7b26\u65f6\uff0c\u5c0f\u5fc3\u4e0d\u8981\u5c06 ASCII \u7801\u503c\u548c\u6570\u5b57\u641e\u6df7\u3002\u5982\u679c\u9700\u8981\u628a\u5b57\u7b26\u5f53\u6574\u578b\u6253\u5370\u5904\u7406\uff0c\u9700\u8981\u4f7f\u7528 static_cast \u3002 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast \u5c16\u62ec\u53f7\u5728 C++ \u4e2d\u901a\u5e38\u7528\u6765\u8868\u793a\u53c2\u6570\u5316\u7c7b\u578b\u3002\u5728 static_cast \u4e2d\uff0c\u5c16\u62ec\u53f7\u7684\u5185\u5bb9\u6307\u5b9a\u4e86\u76ee\u6807\u7c7b\u578b\u3002(\u4f8b\u5982 static_cast(x) \u4f1a\u628a x \u8f6c\u6362\u4e3a int )\u3002 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \u5e38\u91cf\u6307\u7684\u662f\u503c\u4e0d\u4f1a\u6539\u53d8\u7684\u53d8\u91cf\u3002C++ \u652f\u6301\u4e24\u79cd\u5e38\u91cf\uff1a\u5b57\u9762\u91cf\u5e38\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \u7b26\u53f7\u5e38\u91cf \u6307\u7684\u662f\u7ed9\u4e0e\u5e38\u91cf\u7684\u540d\u5b57\u3002\u5e38\u91cf\u662f\u7b26\u53f7\u53d8\u91cf\u7684\u4e00\u79cd\uff0c\u4f7f\u7528\u66ff\u6362\u6587\u672c\u7684\u7c7b\u5bf9\u8c61\u5b8f\u4e5f\u662f\u7b26\u53f7\u5e38\u91cf\uff1b 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr \u5e38\u91cf\u8868\u8fbe\u5f0f \u662f\u4e00\u4e2a\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\u8868\u8fbe\u5f0f\u3002 \u7f16\u8bd1\u65f6\u5e38\u91cf \u662f\u5176\u503c\u5728\u7f16\u8bd1\u65f6\u5df2\u77e5\u7684\u5e38\u91cf\u3002 \u8fd0\u884c\u65f6\u5e38\u91cf \u662f\u5728\u8fd0\u884c\u65f6\u4e4b\u524d\u4e0d\u77e5\u9053\u5176\u521d\u59cb\u5316\u503c\u7684\u5e38\u91cf\u3002constexprvariable\u5fc5\u987b\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 4.15 - \u5b57\u9762\u91cf \u5b57\u9762\u91cf \u662f\u76f4\u63a5\u5d4c\u5165\u5230\u4ee3\u7801\u4e2d\u7684\u5e38\u91cf\u3002\u5b57\u9762\u91cf\u662f\u6709\u7c7b\u578b\u7684\uff0c\u800c\u5b57\u9762\u91cf\u540e\u7f00\u53ef\u4ee5\u7528\u6765\u4fee\u6539\u5b57\u9762\u91cf\u7684\u9ed8\u8ba4\u7c7b\u578b\u3002 \u9b54\u9b3c\u6570\u5b57\u6307\u7684\u662f\u542b\u4e49\u4e0d\u660e\u786e\u6216\u662f\u7a0d\u540e\u4f1a\u88ab\u4fee\u6539\u7684\u5b57\u9762\u91cf\uff08\u901a\u5e38\u662f\u6570\u5b57\uff09\uff0c\u4e0d\u8981\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u9b54\u672f\u6570\u5b57\uff0c\u5e94\u8be5\u7528\u7b26\u53f7\u5e38\u91cf\u4ee3\u66ff\u5b83\u3002 4.16 - \u6570\u503c\u7cfb\u7edf\uff08\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236\uff09 \u5728\u65e5\u5e38\u751f\u6d3b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5341\u8fdb\u5236\u6570\uff0c\u5b83\u670910\u4f4d\u6570\u5b57\u3002\u8ba1\u7b97\u673a\u4f7f\u7528\u4e8c\u8fdb\u5236\uff0c\u5b83\u53ea\u67092\u4f4d\u6570\u5b57\u3002C++\u8fd8\u652f\u6301\u516b\u8fdb\u5236(\u4ee58\u4e3a\u57fa\u6570)\u548c\u5341\u516d\u8fdb\u5236(\u4ee516\u4e3a\u57fa\u6570)\u3002\u8fd9\u4e9b\u90fd\u662f\u6570\u5b57\u7cfb\u7edf\u7684\u4f8b\u5b50\uff0c\u662f\u7528\u6765\u8868\u793a\u6570\u5b57\u7684\u7b26\u53f7(\u6570\u5b57)\u7684\u96c6\u5408\u3002 4.17 - std::string \u7b80\u4ecb 4.18 - std::string_view \u7b80\u4ecb \u5b57\u7b26\u4e32\u662f\u7528\u4e8e\u8868\u793a\u6587\u672c(\u5982\u540d\u79f0\u3001\u5355\u8bcd\u548c\u53e5\u5b50)\u7684\u987a\u5e8f\u5b57\u7b26\u7684\u96c6\u5408\u3002\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u603b\u662f\u653e\u5728\u53cc\u5f15\u53f7\u4e4b\u95f4\u3002C++\u4e2d\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u662fC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u5b83\u7684\u7c7b\u578b\u6bd4\u8f83\u5947\u602a\uff0c\u5f88\u96be\u5904\u7406\u3002 std::string \u63d0\u4f9b\u4e86\u7b80\u4fbf\u3001\u5b89\u5168\u7684\u5904\u7406\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u3002\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u603b\u662f\u653e\u5728\u53cc\u5f15\u53f7\u4e2d\u3002\u4f7f\u7528 std::string \u9700\u8981\u5305\u542b \u5934\u6587\u4ef6\uff1b std::string_view \u4e3a\u5df2\u7ecf\u5b58\u5728\u7684\u5b57\u7b26\u4e32\uff08C\u98ce\u683c\u5b57\u7b26\u4e32\u3001 std::string \u6216\u5b57\u7b26\u6570\u7ec4\uff09\u63d0\u4f9b\u4e86\u4e00\u79cd\u53ea\u8bfb\u7684\u8bbf\u95ee\u65b9\u5f0f\uff0c\u800c\u4e0d\u9700\u521b\u5efa\u62f7\u8d1d\u3002","title":"\u7ae0\u8282\u56de\u987e"},{"location":"5-1-Operator-precedence-and-associativity/","tags":["operator"],"text":"Key Takeaway \u4f7f\u7528\u62ec\u53f7\u6765\u6e05\u695a\u5730\u8868\u660e\u590d\u6742\u8868\u8fbe\u5f0f\u5e94\u8be5\u5982\u4f55\u6c42\u503c(\u5c3d\u7ba1\u4ece\u6280\u672f\u4e0a\u6765\u8bb2\u8fd9\u662f\u591a\u4f59\u7684)\u3002 \u5982\u679c\u8868\u8fbe\u5f0f\u4e2d\u53ea\u6709\u4e00\u4e2a\u8d4b\u503c\u8fd0\u7b97\u7b26\u5b83\u53f3\u8fb9\u7684\u6574\u4e2a\u8868\u8fbe\u5f0f\u6ca1\u5fc5\u8981\u653e\u5728\u62ec\u53f7\u91cc\u3002 \u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u7b26\u5408\u8868\u8fbe\u5f0f\u4e2d\u64cd\u4f5c\u6570\u53ef\u4ee5\u4ee5\u4efb\u610f\u987a\u5e8f\u6c42\u503c\uff0c\u5305\u62ec\u51fd\u6570\u8c03\u7528\u548c\u53c2\u6570\u6c42\u503c\u3002 \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b\u89c4\u5b9a\u4ee5\u5916\u7684\u60c5\u51b5\uff0c\u90fd\u53ea\u80fd\u5047\u8bbe\u4f1a\u4ee5\u4efb\u610f\u987a\u5e8f\u6c42\u503c\u3002\u786e\u4fdd\u4f60\u7f16\u5199\u7684\u8868\u8fbe\u5f0f\u4e0d\u4f9d\u8d56\u4e8e\u8fd9\u4e9b\u90e8\u5206\u7684\u6c42\u503c\u987a\u5e8f\u3002 \u7ae0\u8282\u7b80\u4ecb \u672c\u7ae0\u7684\u5185\u5bb9\u5efa\u7acb\u5728 1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 \u7684\u57fa\u7840\u4e0a\u3002\u9996\u5148\u8ba9\u6211\u4eec\u6765\u590d\u4e60\u4e00\u4e0b\u5b66\u8fc7\u7684\u5185\u5bb9\uff1a \u6570\u5b66\u4e2d\u7684\u8fd0\u7b97\u6307\u7684\u662f\u4e00\u4e2a\u8ba1\u7b97\u64cd\u4f5c\uff0c\u5b83\u6d89\u53ca\u52300\u4e2a\u6216\u591a\u4e2a\u8f93\u5165\u503c\uff08\u79f0\u4e3a \u64cd\u4f5c\u6570 \uff09\uff0c\u5e76\u80fd\u591f\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u503c\uff08\u79f0\u4e3a\u8f93\u51fa\u503c\uff09\u3002\u8fd9\u4e2a\u7279\u5b9a\u7684\u8ba1\u7b97\u64cd\u4f5c\uff0c\u901a\u8fc7\u67d0\u79cd\u7ed3\u6784\uff08\u901a\u5e38\u662f\u4e00\u4e2a\u6216\u4e00\u5bf9\u7b26\u53f7\uff09\u6765\u6307\u5b9a\uff0c\u8fd9\u79cd\u7ed3\u6784\u79f0\u4e4b\u4e3a \u8fd0\u7b97\u7b26(operator) \u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u90fd\u77e5\u9053 2 + 3 = 5 \u3002\u5728\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4e2d\uff0c2\u548c3\u79f0\u4e4b\u4e3a\u8fd0\u7b97\u7b26\uff0c\u800c\u7b26\u53f7 + \u5219\u79f0\u4e3a\u8fd0\u7b97\u7b26\uff0c\u8be5\u8fd0\u7b97\u7b26\u544a\u8bc9\u6211\u4eec\u6b64\u65f6\u5c06\u5bf9\u4e24\u4e2a\u64cd\u4f5c\u6570\u6267\u884c\u4e00\u4e2a\u52a0\u6cd5\u64cd\u4f5c\uff0c\u5e76\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u503c 5\u3002 \u5728\u672c\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba\u6709\u5173\u8fd0\u7b97\u7b26\u7684\u8bdd\u9898\uff0c\u540c\u65f6\u4ecb\u7ecd\u5f88\u591a C++ \u652f\u6301\u7684\u8fd0\u7b97\u7b26\u3002 \u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7 \u8003\u8651\u4e00\u4e0b\u8fd9\u4e2a\u7a0d\u5fae\u6709\u70b9\u590d\u6742\u7684\u8868\u8fbe\u5f0f 4 + 2 * 3 \u3002\u4e00\u4e2a\u542b\u6709\u591a\u79cd\u8fd0\u7b97\u7b26\u7684\u8868\u8fbe\u5f0f\u79f0\u4e3a \u590d\u5408\u8868\u8fbe\u5f0f \uff0c\u4e3a\u4e86\u5bf9\u4e00\u4e2a\u590d\u5408\u8868\u8fbe\u5f0f\u6c42\u503c\uff0c\u6211\u4eec\u5fc5\u987b\u77e5\u9053\u6bcf\u4e2a\u8fd0\u7b97\u7b26\u7684\u542b\u4e49\uff0c\u4ee5\u53ca\u8fd0\u7b97\u7b26\u7684\u6267\u884c\u987a\u5e8f\u3002\u590d\u6742\u8868\u8fbe\u5f0f\u6c42\u503c\u65f6\u8fd0\u7b97\u7b26\u7684\u6267\u884c\u987a\u5e8f\u6709\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u51b3\u5b9a\u3002\u57fa\u4e8e\u5bf9\u666e\u901a\u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u7684\u7406\u89e3\uff08\u4e58\u6cd5\u4f18\u5148\u7ea7\u9ad8\u4e8e\u52a0\u6cd5\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u77e5\u9053\u4e0a\u8ff0\u8868\u8fbe\u5f0f\u5e94\u8be5\u6309\u7167 4 + (2 * 3) \u6c42\u503c\u5e76\u5f97\u5230\u7ed3\u679c 10\u3002 \u5728 C++ \u4e2d\uff0c\u5f53\u7f16\u8bd1\u5668\u9047\u5230\u8868\u8fbe\u5f0f\u65f6\uff0c\u5b83\u540c\u6837\u9700\u8981\u5206\u6790\u8868\u8fbe\u5f0f\u5e76\u786e\u5b9a\u5982\u4f55\u6c42\u503c\u3002\u4e3a\u4e86\u5e2e\u52a9\u7f16\u8bd1\u5668\u5224\u65ad\uff0c\u6240\u6709\u7684\u8fd0\u7b97\u7b26\u90fd\u88ab\u6307\u5b9a\u4e86\u4f18\u5148\u7ea7\u3002\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u8d8a\u9ad8\uff0c\u8d8a\u5148\u6c42\u503c\u3002 \u4f60\u53ef\u4ee5\u4ece\u4e0b\u9762\u7684\u8868\u4e2d\u770b\u5230\uff0c\u4e58\u6cd5\u548c\u9664\u6cd5\uff08\u4f18\u5148\u7ea7 5\uff09\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e\u52a0\u6cd5\u548c\u51cf\u6cd5\uff08\u4f18\u5148\u7ea7 6\uff09\u56e0\u6b64 4 + 2 * 3 \u4f1a\u88ab\u770b\u505a 4 + (2 * 3) \u56e0\u4e3a\u4e58\u6cd5\u7684\u4f18\u5148\u7ea7\u66f4\u9ad8\u3002 \u8fd0\u7b97\u7b26\u7684\u7ed3\u5408\u5f8b \u5982\u679c\u4e24\u4e2a\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u6070\u597d\u76f8\u7b49\u4f1a\u600e\u6837\uff1f\u4f8b\u5982\uff0c\u5728 3 * 4 / 2 \u4e2d\uff0c\u4e58\u6cd5\u548c\u9664\u6cd5\u7684\u4f18\u5148\u7ea7\u5c31\u662f\u4e00\u6837\u7684\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u7f16\u8bd1\u5668\u4ec5\u9760\u4f18\u5148\u7ea7\u662f\u4e0d\u80fd\u5224\u65ad\u5982\u4f55\u6c42\u503c\u7684\u3002 \u5982\u679c\u4e24\u4e2a\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u76f8\u540c\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u53c2\u8003\u8fd0\u7b97\u7b26\u7684\u7ed3\u5408\u5f8b\u4ee5\u5224\u65ad\u8be5\u8fd0\u7b97\u7b26\u7684\u6c42\u503c\u987a\u5e8f\u662f\u4ece\u5de6\u5411\u53f3\u8fd8\u662f\u4ece\u53f3\u5411\u5de6\u3002\u4f18\u5148\u7ea7 5 \u7684\u8fd0\u7b97\u7b26\uff0c\u7ed3\u5408\u5f8b\u4e3a\u4ece\u5de6\u5411\u53f3\uff0c\u56e0\u6b64\u8868\u8fbe\u5f0f\u88ab\u770b\u505a (3 * 4) / 2 = 6 \u3002 \u8fd0\u7b97\u7b26\u8868 \u4e0b\u8868\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u4f5c\u4e3a\u53c2\u8003\u624b\u518c\u6765\u4f7f\u7528\uff08\u5982\u679c\u4f60\u5c06\u6765\u9047\u5230\u6709\u5173\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b\u76f8\u5173\u7684\u95ee\u9898\u65f6\u53ef\u4ee5\u8fc7\u6765\u67e5\u8be2\uff09\u3002 \u6ce8\u610f\uff1a \u4f18\u5148\u7ea71\u662f\u6700\u9ad8\u7684\u4f18\u5148\u7ea7\uff0c17 \u4e3a\u6700\u4f4e\u3002\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u8d8a\u9ad8\uff0c\u8d8a\u5148\u6c42\u503c\uff1b L->R \u8868\u793a\u4ece\u5de6\u5411\u53f3\uff1b R->L \u8868\u793a\u4ece\u53f3\u5411\u5de6\uff1b \u4f60\u80af\u5b9a\u5df2\u7ecf\u8ba4\u8bc6\u4e0a\u8868\u4e2d\u7684\u67d0\u4e9b\u64cd\u4f5c\u7b26\u4e86\uff0c\u4f8b\u5982 + , - , * , / , () \u548c sizeof \u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u7684\u57fa\u7840\u7684\u8bdd\uff0c\u5176\u4e2d\u5927\u591a\u662f\u64cd\u4f5c\u7b26\u4f60\u5e94\u8be5\u8fd8\u4e0d\u80fd\u7406\u89e3\u5176\u542b\u4e49\u3002\u8fd9\u662f\u6b63\u5e38\u7684\uff0c\u6211\u4eec\u672c\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u5b83\u4eec\u4e2d\u7684\u5927\u591a\u662f\uff0c\u5269\u4e0b\u7684\u4e00\u4e9b\u5219\u4f1a\u5728\u9047\u5230\u65f6\u518d\u4ecb\u7ecd\u3002 Q: \u4e3a\u4ec0\u4e48\u6ca1\u6709\u6307\u6570\u8fd0\u7b97\u7b26? C++ \u5e76\u4e0d\u652f\u6301\u6307\u6570\u8fd0\u7b97\u7b26\uff08 operator^ \u5728 C++ \u4e2d\u6709\u5176\u4ed6\u7684\u542b\u4e49 \uff09\u3002\u6211\u4eec\u4f1a\u5728 5.3 - \u6c42\u6a21\u548c\u6307\u6570\u8fd0\u7b97 \u4e2d\u4ecb\u7ecd\u6307\u6570\u8fd0\u7b97\u3002 \u52a0\u62ec\u53f7 \u5728\u6570\u5b66\u8fd0\u7b97\u4e2d\uff0c\u6211\u4eec\u90fd\u77e5\u9053\u52a0\u62ec\u53f7\u53ef\u4ee5\u6539\u53d8\u8fd0\u7b97\u7b26\u6267\u884c\u7684\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u77e5\u9053 4 + 2 * 3 \u4f1a\u6309\u7167 4 + (2 * 3) \u6c42\u503c\u3002\u4f46\u662f\u5982\u679c\u6211\u5e0c\u671b\u6309\u7167 (4 + 2) * 3 \u6c42\u503c\u5462\uff1f\u4f60\u5c31\u53ef\u4ee5\u5411\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4e00\u6837\uff0c\u4f7f\u7528\u62ec\u53f7\u660e\u786e\u8868\u793a\u4f60\u5e0c\u671b\u7684\u6c42\u503c\u65b9\u5f0f\u3002C++ \u4e2d\u4f60\u4e5f\u53ef\u4ee5\u8fd9\u6837\u505a\uff0c\u56e0\u4e3a\u62ec\u53f7\u5177\u6709\u6700\u9ad8\u7684\u4f18\u5148\u7ea7\uff0c\u6240\u4ee5\u62ec\u53f7\u603b\u662f\u5148\u4e8e\u5b83\u5185\u90e8\u7684\u4efb\u4f55\u7b26\u53f7\u8fdb\u884c\u6c42\u503c\u3002 \u518d\u8003\u8651\u4e0b\u9762\u7684\u8868\u8fbe\u5f0f x && y || z \u3002\u5b83\u5e94\u8be5\u6309\u7167 (x && y) || z \u6c42\u503c\u8fd8\u662f\u6309\u7167 x && (y || z) \u6c42\u503c\u5462\uff1f\u4f60\u53ef\u4ee5\u67e5\u770b\u4e0a\u8868\uff0c\u53d1\u73b0 && \u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e || \u3002\u4f46\u662f\u8fd0\u7b97\u7b26\u6bd5\u7adf\u592a\u591a\u4e86\uff0c\u4f60\u4e5f\u4e0d\u53ef\u80fd\u90fd\u8bb0\u4f4f\u3002 \u4e3a\u4e86\u51cf\u5c11\u9519\u8bef\uff0c\u540c\u65f6\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff08\u51cf\u5c11\u67e5\u8868\uff09\uff0c\u5bf9\u4e8e\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u4f7f\u7528\u62ec\u53f7\u660e\u786e\u4f60\u7684\u76ee\u7684\u662f\u6700\u597d\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u62ec\u53f7\u6765\u6e05\u695a\u5730\u8868\u660e\u590d\u6742\u8868\u8fbe\u5f0f\u5e94\u8be5\u5982\u4f55\u6c42\u503c(\u5c3d\u7ba1\u4ece\u6280\u672f\u4e0a\u6765\u8bb2\u8fd9\u662f\u591a\u4f59\u7684)\u3002 \u4e0a\u9762\u6240\u8bf4\u7684\u6700\u4f73\u5b9e\u8df5\u6709\u4e00\u4e2a\u4f8b\u5916\uff1a\u5bf9\u4e8e\u8d4b\u503c\u8fd0\u7b97\u7b26\u6765\u8bf4\uff0c\u5982\u679c\u8868\u8fbe\u5f0f\u4e2d\u53ea\u6709\u4e00\u4e2a\u8d4b\u503c\u8fd0\u7b97\u7b26\u5b83\u53f3\u8fb9\u7684\u6574\u4e2a\u8868\u8fbe\u5f0f\u6ca1\u5fc5\u8981\u653e\u5728\u62ec\u53f7\u91cc\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 x = ( y + z + w ); // \u4e0d\u9700\u8981 x = y + z + w ; // \u8fd9\u6837\u5c31\u53ef\u4ee5 x = (( y || z ) && w ); // \u4e0d\u9700\u8981 x = ( y || z ) && w ; // \u8fd9\u6837\u5c31\u53ef\u4ee5 x = ( y *= z ); // \u6709\u591a\u4e2a\u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u60c5\u51b5\u4e0b\uff0c\u52a0\u62ec\u53f7\u4ecd\u7136\u5f88\u6709\u7528 \u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u662f\u5012\u6570\u7b2c\u4e8c\u9ad8\u7684\uff08\u9017\u53f7\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u6700\u4f4e\uff0c\u800c\u4e14\u4e5f\u4e0d\u5e38\u7528\uff09\u3002\u56e0\u6b64\uff0c\u53ea\u8981\u8868\u8fbe\u5f0f\u4e2d\u53ea\u6709\u4e00\u4e2a\u8d4b\u503c\u8fd0\u7b97\u7b26\uff08\u800c\u4e14\u6ca1\u6709\u9017\u53f7\u8fd0\u7b97\u7b26\uff09\uff0c\u90a3\u4e48\u53ef\u4ee5\u786e\u5b9a\u8d4b\u503c\u53f7\u53f3\u8fb9\u7684\u8868\u8fbe\u5f0f\u4f1a\u5728\u8d4b\u503c\u524d\u5b8c\u5168\u6c42\u503c\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u8868\u8fbe\u5f0f\u4e2d\u53ea\u6709\u4e00\u4e2a\u8d4b\u503c\u8fd0\u7b97\u7b26\u5b83\u53f3\u8fb9\u7684\u6574\u4e2a\u8868\u8fbe\u5f0f\u6ca1\u5fc5\u8981\u653e\u5728\u62ec\u53f7\u91cc\u3002 \u8868\u8fbe\u5f0f\u6c42\u503c\u987a\u5e8f\u548c\u51fd\u6570\u53c2\u6570\u5904\u7406\u987a\u5e8f\u5927\u591a\u662f\u672a\u6307\u660e\u7684 \u8003\u8651\u4e0b\u9762\u7684\u8868\u8fbe\u5f0f\uff1a 1 a + b * c \u57fa\u4e8e\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b\uff0c\u6211\u4eec\u6307\u5b9a\u4e0a\u9762\u7684\u8868\u8fbe\u5f0f\u4f1a\u6309\u7167\u4e0b\u9762\u7684\u65b9\u5f0f\u6c42\u503c\uff1a 1 a + ( b * c ) \u5982\u679c a=1 \uff0c b=2 \uff0c c=3 \uff0c\u5219\u6574\u4e2a\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u4e3a 7\u3002 \u4f46\u662f\uff0c\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b\u53ea\u8868\u660e\u8fd0\u7b97\u7b26\u4e4b\u95f4\u7684\u6c42\u503c\u987a\u5e8f\uff0c\u4f46\u5e76\u6ca1\u6709\u8bf4\u660e\u8868\u8fbe\u5f0f\u5176\u4ed6\u90e8\u5206\u7684\u6c42\u503c\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u4e0a\u9762\u4f8b\u5b50\u4e2d a,b,c\u7684\u6c42\u503c\u987a\u5e8f\u662f\u4ec0\u4e48\u5462\uff1f \u4ee4\u4eba\u5403\u60ca\u7684\u662f\uff0c\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u590d\u6742\u8868\u8fbe\u5f0f\u4e2d\u5404\u90e8\u5206\u7684\u6c42\u503c\u987a\u5e8f\u662f\u6ca1\u6709\u660e\u786e\u89c4\u5b9a\u7684\uff08\u5305\u62ec\u51fd\u6570\u8c03\u7528\u548c\u53c2\u6570\u6c42\u503c\uff09\u3002\u56e0\u6b64\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u81ea\u7531\u9009\u62e9\u5b83\u8ba4\u4e3a\u6700\u4f18\u7684\u6c42\u503c\u987a\u5e8f\u3002 \u6ce8\u610f \u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u7b26\u5408\u8868\u8fbe\u5f0f\u4e2d\u64cd\u4f5c\u6570\u53ef\u4ee5\u4ee5\u4efb\u610f\u987a\u5e8f\u6c42\u503c\uff0c\u5305\u62ec\u51fd\u6570\u8c03\u7528\u548c\u53c2\u6570\u6c42\u503c\u3002 \u5bf9\u4e8e\u5927\u591a\u6570\u8868\u8fbe\u5f0f\uff0c\u8fd9\u90fd\u4e0d\u662f\u4ec0\u4e48\u5927\u4e8b\u3002\u4ee5\u4e0a\u9762\u7684\u8868\u8fbe\u5f0f\u4e3a\u4f8b\uff0c\u51e0\u4e2a\u53d8\u91cf\u54ea\u4e2a\u5148\u6c42\u503c\u90fd\u662f\u53ef\u4ee5\u7684\uff0c\u7ed3\u679c\u4e5f\u603b\u662f\u7b49\u4e8e 7\u3002 \u4f46\u662f\uff0c\u786e\u5b9e\u6709\u4e9b\u8868\u8fbe\u5f0f\u662f\u6709\u53ef\u80fd\u53d7\u5f71\u54cd\u7684\u3002\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u5b83\u5176\u4e2d\u5305\u542b\u7684\u9519\u8bef\u662f\u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u90fd\u4f1a\u72af\u7684\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int getValue () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; return x ; } int main () { std :: cout << getValue () + ( getValue () * getValue ()); // a + (b * c) return 0 ; } \u8fd0\u884c\u8be5\u7a0b\u5e8f\u5e76\u4f9d\u6b21\u8f93\u5165 1\u30012 \u548c 3\u7684\u8bdd\uff0c\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u8f93\u51fa\u7ed3\u679c\u5e94\u8be5\u662f 7\u3002\u4f46\u662f\uff0c\u8fd9\u662f\u5efa\u7acb\u5728\u8c03\u7528 getValue() \u4f1a\u4ece\u5de6\u5411\u53f3\u6c42\u503c\u7684\u5047\u8bbe\u4e0a\u7684\u3002\u5b9e\u9645\u4e0a\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u9009\u62e9\u4e0d\u540c\u7684\u6c42\u503c\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u7f16\u8bd1\u5668\u91c7\u7528\u4ece\u53f3\u5411\u5de6\u7684\u987a\u5e8f\u6c42\u503c\uff0c\u5219\u76f8\u540c\u7684\u8f93\u5165\u5f97\u5230\u7684\u7ed3\u679c\u5c31\u662f 5 \uff08 3+(2*1) \uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b\u89c4\u5b9a\u4ee5\u5916\u7684\u60c5\u51b5\uff0c\u90fd\u53ea\u80fd\u5047\u8bbe\u4f1a\u4ee5\u4efb\u610f\u987a\u5e8f\u6c42\u503c\u3002\u786e\u4fdd\u4f60\u7f16\u5199\u7684\u8868\u8fbe\u5f0f\u4e0d\u4f9d\u8d56\u4e8e\u8fd9\u4e9b\u90e8\u5206\u7684\u6c42\u503c\u987a\u5e8f\u3002 \u4e0a\u9762\u7684\u7a0b\u5e8f\u53ef\u4ee5\u7a0d\u52a0\u4fee\u6539\u53d8\u6210\u6ca1\u6709\u6b67\u4e49\u7684\u5f62\u5f0f\uff0c\u5373\u5c06\u51fd\u6570\u8c03\u7528\u4f5c\u4e3a\u5355\u72ec\u7684\u8bed\u53e5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int getValue () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; return x ; } int main () { int a { getValue () }; // will execute first int b { getValue () }; // will execute second int c { getValue () }; // will execute third std :: cout << a + ( b * c ); // order of eval doesn't matter now return 0 ; } \u76f8\u5173\u5185\u5bb9 \u8fd8\u6709\u4e00\u4e9b\u60c5\u51b5\u4e5f\u4f1a\u9020\u6210\u4e0a\u8ff0\u6c42\u503c\u987a\u5e8f\u95ee\u9898\uff0c\u6211\u4eec\u4f1a\u5728 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"5.1 - \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b"},{"location":"5-1-Operator-precedence-and-associativity/#_1","text":"\u672c\u7ae0\u7684\u5185\u5bb9\u5efa\u7acb\u5728 1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 \u7684\u57fa\u7840\u4e0a\u3002\u9996\u5148\u8ba9\u6211\u4eec\u6765\u590d\u4e60\u4e00\u4e0b\u5b66\u8fc7\u7684\u5185\u5bb9\uff1a \u6570\u5b66\u4e2d\u7684\u8fd0\u7b97\u6307\u7684\u662f\u4e00\u4e2a\u8ba1\u7b97\u64cd\u4f5c\uff0c\u5b83\u6d89\u53ca\u52300\u4e2a\u6216\u591a\u4e2a\u8f93\u5165\u503c\uff08\u79f0\u4e3a \u64cd\u4f5c\u6570 \uff09\uff0c\u5e76\u80fd\u591f\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u503c\uff08\u79f0\u4e3a\u8f93\u51fa\u503c\uff09\u3002\u8fd9\u4e2a\u7279\u5b9a\u7684\u8ba1\u7b97\u64cd\u4f5c\uff0c\u901a\u8fc7\u67d0\u79cd\u7ed3\u6784\uff08\u901a\u5e38\u662f\u4e00\u4e2a\u6216\u4e00\u5bf9\u7b26\u53f7\uff09\u6765\u6307\u5b9a\uff0c\u8fd9\u79cd\u7ed3\u6784\u79f0\u4e4b\u4e3a \u8fd0\u7b97\u7b26(operator) \u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u90fd\u77e5\u9053 2 + 3 = 5 \u3002\u5728\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4e2d\uff0c2\u548c3\u79f0\u4e4b\u4e3a\u8fd0\u7b97\u7b26\uff0c\u800c\u7b26\u53f7 + \u5219\u79f0\u4e3a\u8fd0\u7b97\u7b26\uff0c\u8be5\u8fd0\u7b97\u7b26\u544a\u8bc9\u6211\u4eec\u6b64\u65f6\u5c06\u5bf9\u4e24\u4e2a\u64cd\u4f5c\u6570\u6267\u884c\u4e00\u4e2a\u52a0\u6cd5\u64cd\u4f5c\uff0c\u5e76\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u503c 5\u3002 \u5728\u672c\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba\u6709\u5173\u8fd0\u7b97\u7b26\u7684\u8bdd\u9898\uff0c\u540c\u65f6\u4ecb\u7ecd\u5f88\u591a C++ \u652f\u6301\u7684\u8fd0\u7b97\u7b26\u3002","title":"\u7ae0\u8282\u7b80\u4ecb"},{"location":"5-1-Operator-precedence-and-associativity/#_2","text":"\u8003\u8651\u4e00\u4e0b\u8fd9\u4e2a\u7a0d\u5fae\u6709\u70b9\u590d\u6742\u7684\u8868\u8fbe\u5f0f 4 + 2 * 3 \u3002\u4e00\u4e2a\u542b\u6709\u591a\u79cd\u8fd0\u7b97\u7b26\u7684\u8868\u8fbe\u5f0f\u79f0\u4e3a \u590d\u5408\u8868\u8fbe\u5f0f \uff0c\u4e3a\u4e86\u5bf9\u4e00\u4e2a\u590d\u5408\u8868\u8fbe\u5f0f\u6c42\u503c\uff0c\u6211\u4eec\u5fc5\u987b\u77e5\u9053\u6bcf\u4e2a\u8fd0\u7b97\u7b26\u7684\u542b\u4e49\uff0c\u4ee5\u53ca\u8fd0\u7b97\u7b26\u7684\u6267\u884c\u987a\u5e8f\u3002\u590d\u6742\u8868\u8fbe\u5f0f\u6c42\u503c\u65f6\u8fd0\u7b97\u7b26\u7684\u6267\u884c\u987a\u5e8f\u6709\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u51b3\u5b9a\u3002\u57fa\u4e8e\u5bf9\u666e\u901a\u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u7684\u7406\u89e3\uff08\u4e58\u6cd5\u4f18\u5148\u7ea7\u9ad8\u4e8e\u52a0\u6cd5\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u77e5\u9053\u4e0a\u8ff0\u8868\u8fbe\u5f0f\u5e94\u8be5\u6309\u7167 4 + (2 * 3) \u6c42\u503c\u5e76\u5f97\u5230\u7ed3\u679c 10\u3002 \u5728 C++ \u4e2d\uff0c\u5f53\u7f16\u8bd1\u5668\u9047\u5230\u8868\u8fbe\u5f0f\u65f6\uff0c\u5b83\u540c\u6837\u9700\u8981\u5206\u6790\u8868\u8fbe\u5f0f\u5e76\u786e\u5b9a\u5982\u4f55\u6c42\u503c\u3002\u4e3a\u4e86\u5e2e\u52a9\u7f16\u8bd1\u5668\u5224\u65ad\uff0c\u6240\u6709\u7684\u8fd0\u7b97\u7b26\u90fd\u88ab\u6307\u5b9a\u4e86\u4f18\u5148\u7ea7\u3002\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u8d8a\u9ad8\uff0c\u8d8a\u5148\u6c42\u503c\u3002 \u4f60\u53ef\u4ee5\u4ece\u4e0b\u9762\u7684\u8868\u4e2d\u770b\u5230\uff0c\u4e58\u6cd5\u548c\u9664\u6cd5\uff08\u4f18\u5148\u7ea7 5\uff09\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e\u52a0\u6cd5\u548c\u51cf\u6cd5\uff08\u4f18\u5148\u7ea7 6\uff09\u56e0\u6b64 4 + 2 * 3 \u4f1a\u88ab\u770b\u505a 4 + (2 * 3) \u56e0\u4e3a\u4e58\u6cd5\u7684\u4f18\u5148\u7ea7\u66f4\u9ad8\u3002","title":"\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7"},{"location":"5-1-Operator-precedence-and-associativity/#_3","text":"\u5982\u679c\u4e24\u4e2a\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u6070\u597d\u76f8\u7b49\u4f1a\u600e\u6837\uff1f\u4f8b\u5982\uff0c\u5728 3 * 4 / 2 \u4e2d\uff0c\u4e58\u6cd5\u548c\u9664\u6cd5\u7684\u4f18\u5148\u7ea7\u5c31\u662f\u4e00\u6837\u7684\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u7f16\u8bd1\u5668\u4ec5\u9760\u4f18\u5148\u7ea7\u662f\u4e0d\u80fd\u5224\u65ad\u5982\u4f55\u6c42\u503c\u7684\u3002 \u5982\u679c\u4e24\u4e2a\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u76f8\u540c\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u53c2\u8003\u8fd0\u7b97\u7b26\u7684\u7ed3\u5408\u5f8b\u4ee5\u5224\u65ad\u8be5\u8fd0\u7b97\u7b26\u7684\u6c42\u503c\u987a\u5e8f\u662f\u4ece\u5de6\u5411\u53f3\u8fd8\u662f\u4ece\u53f3\u5411\u5de6\u3002\u4f18\u5148\u7ea7 5 \u7684\u8fd0\u7b97\u7b26\uff0c\u7ed3\u5408\u5f8b\u4e3a\u4ece\u5de6\u5411\u53f3\uff0c\u56e0\u6b64\u8868\u8fbe\u5f0f\u88ab\u770b\u505a (3 * 4) / 2 = 6 \u3002","title":"\u8fd0\u7b97\u7b26\u7684\u7ed3\u5408\u5f8b"},{"location":"5-1-Operator-precedence-and-associativity/#_4","text":"\u4e0b\u8868\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u4f5c\u4e3a\u53c2\u8003\u624b\u518c\u6765\u4f7f\u7528\uff08\u5982\u679c\u4f60\u5c06\u6765\u9047\u5230\u6709\u5173\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b\u76f8\u5173\u7684\u95ee\u9898\u65f6\u53ef\u4ee5\u8fc7\u6765\u67e5\u8be2\uff09\u3002 \u6ce8\u610f\uff1a \u4f18\u5148\u7ea71\u662f\u6700\u9ad8\u7684\u4f18\u5148\u7ea7\uff0c17 \u4e3a\u6700\u4f4e\u3002\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u8d8a\u9ad8\uff0c\u8d8a\u5148\u6c42\u503c\uff1b L->R \u8868\u793a\u4ece\u5de6\u5411\u53f3\uff1b R->L \u8868\u793a\u4ece\u53f3\u5411\u5de6\uff1b \u4f60\u80af\u5b9a\u5df2\u7ecf\u8ba4\u8bc6\u4e0a\u8868\u4e2d\u7684\u67d0\u4e9b\u64cd\u4f5c\u7b26\u4e86\uff0c\u4f8b\u5982 + , - , * , / , () \u548c sizeof \u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u7684\u57fa\u7840\u7684\u8bdd\uff0c\u5176\u4e2d\u5927\u591a\u662f\u64cd\u4f5c\u7b26\u4f60\u5e94\u8be5\u8fd8\u4e0d\u80fd\u7406\u89e3\u5176\u542b\u4e49\u3002\u8fd9\u662f\u6b63\u5e38\u7684\uff0c\u6211\u4eec\u672c\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u5b83\u4eec\u4e2d\u7684\u5927\u591a\u662f\uff0c\u5269\u4e0b\u7684\u4e00\u4e9b\u5219\u4f1a\u5728\u9047\u5230\u65f6\u518d\u4ecb\u7ecd\u3002 Q: \u4e3a\u4ec0\u4e48\u6ca1\u6709\u6307\u6570\u8fd0\u7b97\u7b26? C++ \u5e76\u4e0d\u652f\u6301\u6307\u6570\u8fd0\u7b97\u7b26\uff08 operator^ \u5728 C++ \u4e2d\u6709\u5176\u4ed6\u7684\u542b\u4e49 \uff09\u3002\u6211\u4eec\u4f1a\u5728 5.3 - \u6c42\u6a21\u548c\u6307\u6570\u8fd0\u7b97 \u4e2d\u4ecb\u7ecd\u6307\u6570\u8fd0\u7b97\u3002","title":"\u8fd0\u7b97\u7b26\u8868"},{"location":"5-1-Operator-precedence-and-associativity/#_5","text":"\u5728\u6570\u5b66\u8fd0\u7b97\u4e2d\uff0c\u6211\u4eec\u90fd\u77e5\u9053\u52a0\u62ec\u53f7\u53ef\u4ee5\u6539\u53d8\u8fd0\u7b97\u7b26\u6267\u884c\u7684\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u77e5\u9053 4 + 2 * 3 \u4f1a\u6309\u7167 4 + (2 * 3) \u6c42\u503c\u3002\u4f46\u662f\u5982\u679c\u6211\u5e0c\u671b\u6309\u7167 (4 + 2) * 3 \u6c42\u503c\u5462\uff1f\u4f60\u5c31\u53ef\u4ee5\u5411\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4e00\u6837\uff0c\u4f7f\u7528\u62ec\u53f7\u660e\u786e\u8868\u793a\u4f60\u5e0c\u671b\u7684\u6c42\u503c\u65b9\u5f0f\u3002C++ \u4e2d\u4f60\u4e5f\u53ef\u4ee5\u8fd9\u6837\u505a\uff0c\u56e0\u4e3a\u62ec\u53f7\u5177\u6709\u6700\u9ad8\u7684\u4f18\u5148\u7ea7\uff0c\u6240\u4ee5\u62ec\u53f7\u603b\u662f\u5148\u4e8e\u5b83\u5185\u90e8\u7684\u4efb\u4f55\u7b26\u53f7\u8fdb\u884c\u6c42\u503c\u3002 \u518d\u8003\u8651\u4e0b\u9762\u7684\u8868\u8fbe\u5f0f x && y || z \u3002\u5b83\u5e94\u8be5\u6309\u7167 (x && y) || z \u6c42\u503c\u8fd8\u662f\u6309\u7167 x && (y || z) \u6c42\u503c\u5462\uff1f\u4f60\u53ef\u4ee5\u67e5\u770b\u4e0a\u8868\uff0c\u53d1\u73b0 && \u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e || \u3002\u4f46\u662f\u8fd0\u7b97\u7b26\u6bd5\u7adf\u592a\u591a\u4e86\uff0c\u4f60\u4e5f\u4e0d\u53ef\u80fd\u90fd\u8bb0\u4f4f\u3002 \u4e3a\u4e86\u51cf\u5c11\u9519\u8bef\uff0c\u540c\u65f6\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff08\u51cf\u5c11\u67e5\u8868\uff09\uff0c\u5bf9\u4e8e\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u4f7f\u7528\u62ec\u53f7\u660e\u786e\u4f60\u7684\u76ee\u7684\u662f\u6700\u597d\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u62ec\u53f7\u6765\u6e05\u695a\u5730\u8868\u660e\u590d\u6742\u8868\u8fbe\u5f0f\u5e94\u8be5\u5982\u4f55\u6c42\u503c(\u5c3d\u7ba1\u4ece\u6280\u672f\u4e0a\u6765\u8bb2\u8fd9\u662f\u591a\u4f59\u7684)\u3002 \u4e0a\u9762\u6240\u8bf4\u7684\u6700\u4f73\u5b9e\u8df5\u6709\u4e00\u4e2a\u4f8b\u5916\uff1a\u5bf9\u4e8e\u8d4b\u503c\u8fd0\u7b97\u7b26\u6765\u8bf4\uff0c\u5982\u679c\u8868\u8fbe\u5f0f\u4e2d\u53ea\u6709\u4e00\u4e2a\u8d4b\u503c\u8fd0\u7b97\u7b26\u5b83\u53f3\u8fb9\u7684\u6574\u4e2a\u8868\u8fbe\u5f0f\u6ca1\u5fc5\u8981\u653e\u5728\u62ec\u53f7\u91cc\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 x = ( y + z + w ); // \u4e0d\u9700\u8981 x = y + z + w ; // \u8fd9\u6837\u5c31\u53ef\u4ee5 x = (( y || z ) && w ); // \u4e0d\u9700\u8981 x = ( y || z ) && w ; // \u8fd9\u6837\u5c31\u53ef\u4ee5 x = ( y *= z ); // \u6709\u591a\u4e2a\u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u60c5\u51b5\u4e0b\uff0c\u52a0\u62ec\u53f7\u4ecd\u7136\u5f88\u6709\u7528 \u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u662f\u5012\u6570\u7b2c\u4e8c\u9ad8\u7684\uff08\u9017\u53f7\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u6700\u4f4e\uff0c\u800c\u4e14\u4e5f\u4e0d\u5e38\u7528\uff09\u3002\u56e0\u6b64\uff0c\u53ea\u8981\u8868\u8fbe\u5f0f\u4e2d\u53ea\u6709\u4e00\u4e2a\u8d4b\u503c\u8fd0\u7b97\u7b26\uff08\u800c\u4e14\u6ca1\u6709\u9017\u53f7\u8fd0\u7b97\u7b26\uff09\uff0c\u90a3\u4e48\u53ef\u4ee5\u786e\u5b9a\u8d4b\u503c\u53f7\u53f3\u8fb9\u7684\u8868\u8fbe\u5f0f\u4f1a\u5728\u8d4b\u503c\u524d\u5b8c\u5168\u6c42\u503c\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u8868\u8fbe\u5f0f\u4e2d\u53ea\u6709\u4e00\u4e2a\u8d4b\u503c\u8fd0\u7b97\u7b26\u5b83\u53f3\u8fb9\u7684\u6574\u4e2a\u8868\u8fbe\u5f0f\u6ca1\u5fc5\u8981\u653e\u5728\u62ec\u53f7\u91cc\u3002","title":"\u52a0\u62ec\u53f7"},{"location":"5-1-Operator-precedence-and-associativity/#_6","text":"\u8003\u8651\u4e0b\u9762\u7684\u8868\u8fbe\u5f0f\uff1a 1 a + b * c \u57fa\u4e8e\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b\uff0c\u6211\u4eec\u6307\u5b9a\u4e0a\u9762\u7684\u8868\u8fbe\u5f0f\u4f1a\u6309\u7167\u4e0b\u9762\u7684\u65b9\u5f0f\u6c42\u503c\uff1a 1 a + ( b * c ) \u5982\u679c a=1 \uff0c b=2 \uff0c c=3 \uff0c\u5219\u6574\u4e2a\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u4e3a 7\u3002 \u4f46\u662f\uff0c\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b\u53ea\u8868\u660e\u8fd0\u7b97\u7b26\u4e4b\u95f4\u7684\u6c42\u503c\u987a\u5e8f\uff0c\u4f46\u5e76\u6ca1\u6709\u8bf4\u660e\u8868\u8fbe\u5f0f\u5176\u4ed6\u90e8\u5206\u7684\u6c42\u503c\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u4e0a\u9762\u4f8b\u5b50\u4e2d a,b,c\u7684\u6c42\u503c\u987a\u5e8f\u662f\u4ec0\u4e48\u5462\uff1f \u4ee4\u4eba\u5403\u60ca\u7684\u662f\uff0c\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u590d\u6742\u8868\u8fbe\u5f0f\u4e2d\u5404\u90e8\u5206\u7684\u6c42\u503c\u987a\u5e8f\u662f\u6ca1\u6709\u660e\u786e\u89c4\u5b9a\u7684\uff08\u5305\u62ec\u51fd\u6570\u8c03\u7528\u548c\u53c2\u6570\u6c42\u503c\uff09\u3002\u56e0\u6b64\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u81ea\u7531\u9009\u62e9\u5b83\u8ba4\u4e3a\u6700\u4f18\u7684\u6c42\u503c\u987a\u5e8f\u3002 \u6ce8\u610f \u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u7b26\u5408\u8868\u8fbe\u5f0f\u4e2d\u64cd\u4f5c\u6570\u53ef\u4ee5\u4ee5\u4efb\u610f\u987a\u5e8f\u6c42\u503c\uff0c\u5305\u62ec\u51fd\u6570\u8c03\u7528\u548c\u53c2\u6570\u6c42\u503c\u3002 \u5bf9\u4e8e\u5927\u591a\u6570\u8868\u8fbe\u5f0f\uff0c\u8fd9\u90fd\u4e0d\u662f\u4ec0\u4e48\u5927\u4e8b\u3002\u4ee5\u4e0a\u9762\u7684\u8868\u8fbe\u5f0f\u4e3a\u4f8b\uff0c\u51e0\u4e2a\u53d8\u91cf\u54ea\u4e2a\u5148\u6c42\u503c\u90fd\u662f\u53ef\u4ee5\u7684\uff0c\u7ed3\u679c\u4e5f\u603b\u662f\u7b49\u4e8e 7\u3002 \u4f46\u662f\uff0c\u786e\u5b9e\u6709\u4e9b\u8868\u8fbe\u5f0f\u662f\u6709\u53ef\u80fd\u53d7\u5f71\u54cd\u7684\u3002\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u5b83\u5176\u4e2d\u5305\u542b\u7684\u9519\u8bef\u662f\u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u90fd\u4f1a\u72af\u7684\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int getValue () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; return x ; } int main () { std :: cout << getValue () + ( getValue () * getValue ()); // a + (b * c) return 0 ; } \u8fd0\u884c\u8be5\u7a0b\u5e8f\u5e76\u4f9d\u6b21\u8f93\u5165 1\u30012 \u548c 3\u7684\u8bdd\uff0c\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u8f93\u51fa\u7ed3\u679c\u5e94\u8be5\u662f 7\u3002\u4f46\u662f\uff0c\u8fd9\u662f\u5efa\u7acb\u5728\u8c03\u7528 getValue() \u4f1a\u4ece\u5de6\u5411\u53f3\u6c42\u503c\u7684\u5047\u8bbe\u4e0a\u7684\u3002\u5b9e\u9645\u4e0a\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u9009\u62e9\u4e0d\u540c\u7684\u6c42\u503c\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u7f16\u8bd1\u5668\u91c7\u7528\u4ece\u53f3\u5411\u5de6\u7684\u987a\u5e8f\u6c42\u503c\uff0c\u5219\u76f8\u540c\u7684\u8f93\u5165\u5f97\u5230\u7684\u7ed3\u679c\u5c31\u662f 5 \uff08 3+(2*1) \uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b\u89c4\u5b9a\u4ee5\u5916\u7684\u60c5\u51b5\uff0c\u90fd\u53ea\u80fd\u5047\u8bbe\u4f1a\u4ee5\u4efb\u610f\u987a\u5e8f\u6c42\u503c\u3002\u786e\u4fdd\u4f60\u7f16\u5199\u7684\u8868\u8fbe\u5f0f\u4e0d\u4f9d\u8d56\u4e8e\u8fd9\u4e9b\u90e8\u5206\u7684\u6c42\u503c\u987a\u5e8f\u3002 \u4e0a\u9762\u7684\u7a0b\u5e8f\u53ef\u4ee5\u7a0d\u52a0\u4fee\u6539\u53d8\u6210\u6ca1\u6709\u6b67\u4e49\u7684\u5f62\u5f0f\uff0c\u5373\u5c06\u51fd\u6570\u8c03\u7528\u4f5c\u4e3a\u5355\u72ec\u7684\u8bed\u53e5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int getValue () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; return x ; } int main () { int a { getValue () }; // will execute first int b { getValue () }; // will execute second int c { getValue () }; // will execute third std :: cout << a + ( b * c ); // order of eval doesn't matter now return 0 ; } \u76f8\u5173\u5185\u5bb9 \u8fd8\u6709\u4e00\u4e9b\u60c5\u51b5\u4e5f\u4f1a\u9020\u6210\u4e0a\u8ff0\u6c42\u503c\u987a\u5e8f\u95ee\u9898\uff0c\u6211\u4eec\u4f1a\u5728 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"\u8868\u8fbe\u5f0f\u6c42\u503c\u987a\u5e8f\u548c\u51fd\u6570\u53c2\u6570\u5904\u7406\u987a\u5e8f\u5927\u591a\u662f\u672a\u6307\u660e\u7684"},{"location":"5-2-Arithmetic-operators/","tags":["operator"],"text":"Key Takeaway \u4f7f\u7528 static_cast \u5bf9\u6574\u578b\u6570\u8fdb\u884c\u6d6e\u70b9\u9664\u6cd5 std::cout << \"double / int = \" << static_cast(x) / y << '\\n'; \u9664 0 (\u6216 0.0) \u5f88\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4f60\u7684\u7a0b\u5e8f\u5d29\u6e83 \u4e00\u5143\u6570\u5b66\u8fd0\u7b97\u7b26 \u4e00\u5143\u8fd0\u7b97\u7b26\u6709\u4e24\u79cd\uff0c\u6b63\u53f7( + )\u548c\u8d1f\u53f7( - )\u3002\u63d0\u9192\u4e00\u4e0b\uff0c\u4e00\u5143\u8fd0\u7b97\u7b26\u53ea\u9700\u8981\u4e00\u4e2a \u64cd\u4f5c\u6570 \u3002 \u8fd0\u7b97\u7b26 \u7b26\u53f7 \u683c\u5f0f \u64cd\u4f5c Unary plus + +x x\u7684\u503c Unary minus - -x x\u7684\u76f8\u53cd\u6570 \u4e00\u5143\u7b26\u53f7\u8fd0\u7b97\u7b26\u8fd4\u56de\u64cd\u4f5c\u6570\u4e58\u4ee5-1\u4e4b\u540e\u7684\u7ed3\u679c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c x = 5\uff0c\u5219 -x \u662f -5\u3002 \u4e00\u5143\u6b63\u53f7\u8fd0\u7b97\u7b26\u8fd4\u56de\u64cd\u4f5c\u6570\u672c\u8eab\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c +5 \u662f 5 \u5219 +x \u662f x\u3002\u4e00\u822c\u6765\u8bf4\u4f60\u4e0d\u9700\u8981\u4f7f\u7528\u5b83\uff0c\u56e0\u4e3a\u662f\u591a\u4f59\u7684\u3002\u53ea\u6709\u4e3a\u4e86\u548c\u4e00\u5143\u8d1f\u53f7\u8fd0\u7b97\u7b26\u5f62\u6210\u5bf9\u79f0\u7684\u60c5\u51b5\u4e0b\u624d\u4f1a\u4f7f\u7528\u5b83\u3002 \u8fd9\u4e9b\u64cd\u4f5c\u7b26\u5e94\u5f53\u653e\u7f6e\u5728\u7d27\u90bb\u7740\u64cd\u4f5c\u6570\u524d\u9762\u7684\u4f4d\u7f6e\uff08\u4f8b\u5982\u5e94\u8be5\u5199\u4f5c -x \u800c\u4e0d\u662f - x \uff09\u3002 \u4e0d\u8981\u628a\u4e00\u5143\u8d1f\u53f7\u8fd0\u7b97\u7b26\u548c\u51cf\u53f7\u641e\u6df7\u4e86\uff0c\u5c3d\u7ba1\u5b83\u4eec\u7684\u7b26\u53f7\u7684\u786e\u662f\u540c\u4e00\u4e2a\u3002\u4f8b\u5982\uff0c\u5728\u8868\u8fbe\u5f0f x = 5 - -3; \u4e2d\uff0c\u7b2c\u4e00\u4e2a - \u662f\u51cf\u53f7\uff0c\u7b2c\u4e8c\u4e2a - \u5219\u662f\u8d1f\u53f7\u3002 \u4e8c\u5143\u6570\u5b66\u8fd0\u7b97\u7b26 \u4e8c\u5143\u6570\u5b66\u8fd0\u7b97\u7b26\u6709 5 \u4e2a\uff0c\u5b83\u4eec\u9700\u8981\u4e24\u4e2a\u64cd\u4f5c\u6570\u3002 \u8fd0\u7b97\u7b26 \u7b26\u53f7 \u5f62\u5f0f \u64cd\u4f5c Addition + x + y x \u52a0 y Subtraction - x - y x \u51cf y Multiplication * x * y x \u4e58 y Division / x / y x \u9664 y Modulus (Remainder) % x % y x \u9664 y \u7684\u4f59\u6570 \u52a0\u51cf\u4e58\u548c\u65e5\u5e38\u751f\u6d3b\u4e2d\u7684\u7528\u6cd5\u662f\u4e00\u6837\u7684\u3002 \u9664\u6cd5\u548c\u6c42\u6a21\uff08\u6c42\u4f59\u6570\uff09\u5219\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u989d\u5916\u7684\u8bf4\u660e\u3002\u6211\u4eec\u4f1a\u5728\u672c\u8282\u8bfe\u4ecb\u7ecd\u9664\u6cd5\uff0c\u4e0b\u8282\u8bfe\u4f1a\u4ecb\u7ecd\u6c42\u4f59\u3002 \u6574\u6570\u9664\u6cd5\u548c\u6d6e\u70b9\u6570\u9664\u6cd5 \u53ef\u4ee5\u7b80\u5355\u5730\u8ba4\u4e3a\u9664\u6cd5\u8fd0\u7b97\u7b26\u6709\u4e24\u79cd\u201d\u6a21\u5f0f\u201c\u3002 \u5982\u679c\u4efb\u4e00(\u6216\u5168\u90e8)\u7684\u64cd\u4f5c\u6570\u662f\u6d6e\u70b9\u503c\uff0c\u5219\u9664\u6cd5\u64cd\u4f5c\u6570\u4f1a\u6267\u884c \u6d6e\u70b9\u9664\u6cd5 \u3002\u6d6e\u70b9\u6570\u9664\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6d6e\u70b9\u503c\uff0c\u5c0f\u6570\u90e8\u5206\u4f1a\u88ab\u4fdd\u7559\u3002\u4f8b\u5982 7.0 / 4 = 1.75 \u3001 7 / 4.0 = 1.75 \u548c 7.0 / 4.0 = 1.75 \u3002\u548c\u5176\u4ed6\u6d6e\u70b9\u8fd0\u7b97\u4e00\u6837\uff0c \u820d\u5165\u8bef\u5dee \u53ef\u80fd\u4f1a\u53d1\u751f\u3002 \u5982\u679c\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u662f\u6574\u578b\uff0c\u5219\u9664\u6cd5\u8fd0\u7b97\u7b26\u4f1a\u6267\u884c\u6574\u578b\u9664\u6cd5\u3002\u6574\u578b\u9664\u6cd5\u4f1a\u4e22\u5f03\u7ed3\u679c\u7684\u5c0f\u6570\u90e8\u5206\u5e76\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\u6570\u3002\u4f8b\u5982 7 / 4 = 1 \uff0c\u56e0\u4e3a\u5c0f\u6570\u90e8\u5206\u88ab\u4e22\u5f03\u4e86\u3002\u7c7b\u4f3c\u7684 -7 / 4 = -1 \u56e0\u4e3a\u5c0f\u6570\u90e8\u5206\u88ab\u4e22\u5f03\u4e86\u3002 \u4f7f\u7528 static_cast \u5bf9\u6574\u578b\u6570\u8fdb\u884c\u6d6e\u70b9\u9664\u6cd5 \u5982\u679c\u6709\u4e24\u4e2a\u6574\u578b\u6570\uff0c\u6211\u5e0c\u671b\u4e0d\u8981\u4e22\u5931\u5b83\u4eec\u505a\u9664\u6cd5\u540e\u7684\u5c0f\u6570\u90e8\u5206\uff0c\u90a3\u4e48\u5e94\u8be5\u600e\u4e48\u505a\u5462\uff1f \u5728 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast \u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 static_cast<> \u5c06\u5b57\u7b26\u8f6c\u6362\u6210\u6574\u578b\u4ee5\u4fbf\u6253\u5370\u65f6\u80fd\u591f\u5c06\u5176\u6253\u5370\u4e3a\u4e00\u4e2a\u6574\u6570\u3002 \u8fd9\u91cc\uff0c\u6211\u4eec\u540c\u6837\u53ef\u4ee5\u7528 static_cast<> \u5c06\u4e00\u4e2a\u6574\u578b\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u4ee5\u4fbf\u4f7f\u7528\u6d6e\u70b9\u6570\u9664\u6cd5\u3002\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 7 }; int y { 4 }; std :: cout << \"int / int = \" << x / y << '\\n' ; std :: cout << \"double / int = \" << static_cast < double > ( x ) / y << '\\n' ; std :: cout << \"int / double = \" << x / static_cast < double > ( y ) << '\\n' ; std :: cout << \"double / double = \" << static_cast < double > ( x ) / static_cast < double > ( y ) << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 int / int = 1 double / int = 1.75 int / double = 1.75 double / double = 1.75 \u4ece\u7ed3\u679c\u6765\u770b\uff0c\u53ea\u8981\u6709\u4efb\u4f55\u4e00\u4e2a\u64cd\u4f5c\u6570\u662f\u6d6e\u70b9\u6570\uff0c\u5c31\u4f1a\u6267\u884c\u6d6e\u70b9\u9664\u6cd5\uff0c\u800c\u4e0d\u662f\u6574\u6570\u9664\u6cd5\u3002 \u9664 0 \u9519\u8bef \u9664 0 (\u6216 0.0) \u5f88\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4f60\u7684\u7a0b\u5e8f\u5d29\u6e83\uff0c\u56e0\u4e3a\u8be5\u8fd0\u7b97\u7684\u7ed3\u679c\u662f\u6ca1\u6709\u6570\u5b66\u5b9a\u4e49\u7684\u3002 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { std :: cout << \"Enter a divisor: \" ; int x {}; std :: cin >> x ; std :: cout << \"12 / \" << x << \" = \" << 12 / x << '\\n' ; return 0 ; } \u5982\u679c\u4f60\u8fd0\u884c\u4e0a\u8ff0\u7a0b\u5e8f\u5e76\u8f93\u5165 0\uff0c\u4f60\u7684\u7a0b\u5e8f\u4f1a\u5d29\u6e83\u6216\u5f02\u5e38\u7ec8\u6b62\u3002\u4f60\u53ef\u4ee5\u4eb2\u81ea\u4f53\u9a8c\u4e00\u4e0b\uff0c\u8fd9\u4e0d\u4f1a\u5bf9\u4f60\u7535\u8111\u6709\u4ec0\u4e48\u5f71\u54cd\u7684\u3002 \u7b97\u6570\u8d4b\u503c\u8fd0\u7b97\u7b26 \u8fd0\u7b97\u7b26 \u7b26\u53f7 \u683c\u5f0f \u64cd\u4f5c Assignment = x = y y \u8d4b\u503c\u7ed9 x Addition assignment += x += y \u628a y \u52a0\u5230 x \u4e0a Subtraction assignment -= x -= y \u4ece x \u4e2d\u51cf\u53bb y Multiplication assignment *= x *= y \u628a x \u4e58\u4e0a y Division assignment /= x /= y \u628a x \u9664 y Modulus assignment %= x %= y \u628a x / y \u7684\u4f59\u6570\u8d4b\u503c\u7ed9 x \u5b66\u5230\u8fd9\u91cc\uff0c\u5982\u679c\u4f60\u9700\u8981\u7ed9\u4e00\u4e2a\u53d8\u91cf\u52a0\u4e0a 4\uff0c\u4f60\u5f88\u53ef\u80fd\u4f1a\u8fd9\u6837\u505a\uff1a 1 x = x + 4 ; // add 4 to existing value of x \u8fd9\u4e48\u505a\u662f\u6ca1\u95ee\u9898\u7684\uff0c\u5c31\u662f\u6709\u4e9b\u5570\u55e6\uff0c\u5b83\u9700\u8981\u4e24\u4e2a\u8fd0\u7b97\u7b26\u624d\u80fd\u5b8c\u6210\uff08 operator+ \u548c operator= \uff09\u3002 \u56e0\u4e3a x = x + 4 \u8fd9\u6837\u7684\u8868\u8fbe\u5f0f\u592a\u5e38\u89c1\u4e86\uff0c\u6240\u4ee5 C++ \u63d0\u4f9b\u4e86\u4e94\u79cd\u540c\u6837\u529f\u80fd\u4f46\u66f4\u65b9\u4fbf\u7684\u8fd0\u7b97\u7b26\u3002 x = x + 4 \u53ef\u4ee5\u66ff\u6362\u4e3a x += 4 \u3002\u800c\u5bf9\u4e8e x = x * y \uff0c\u4f60\u53ef\u4ee5\u5199\u6210 x *= y \u3002 \u56e0\u6b64\uff0c\u4e0a\u8ff0\u4ee3\u7801\u53ef\u4ee5\u53d8\u6210\u4e0b\u9762\u7684\u5f62\u5f0f\uff1a 1 x += 4 ; // add 4 to existing value of x","title":"5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26"},{"location":"5-2-Arithmetic-operators/#_1","text":"\u4e00\u5143\u8fd0\u7b97\u7b26\u6709\u4e24\u79cd\uff0c\u6b63\u53f7( + )\u548c\u8d1f\u53f7( - )\u3002\u63d0\u9192\u4e00\u4e0b\uff0c\u4e00\u5143\u8fd0\u7b97\u7b26\u53ea\u9700\u8981\u4e00\u4e2a \u64cd\u4f5c\u6570 \u3002 \u8fd0\u7b97\u7b26 \u7b26\u53f7 \u683c\u5f0f \u64cd\u4f5c Unary plus + +x x\u7684\u503c Unary minus - -x x\u7684\u76f8\u53cd\u6570 \u4e00\u5143\u7b26\u53f7\u8fd0\u7b97\u7b26\u8fd4\u56de\u64cd\u4f5c\u6570\u4e58\u4ee5-1\u4e4b\u540e\u7684\u7ed3\u679c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c x = 5\uff0c\u5219 -x \u662f -5\u3002 \u4e00\u5143\u6b63\u53f7\u8fd0\u7b97\u7b26\u8fd4\u56de\u64cd\u4f5c\u6570\u672c\u8eab\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c +5 \u662f 5 \u5219 +x \u662f x\u3002\u4e00\u822c\u6765\u8bf4\u4f60\u4e0d\u9700\u8981\u4f7f\u7528\u5b83\uff0c\u56e0\u4e3a\u662f\u591a\u4f59\u7684\u3002\u53ea\u6709\u4e3a\u4e86\u548c\u4e00\u5143\u8d1f\u53f7\u8fd0\u7b97\u7b26\u5f62\u6210\u5bf9\u79f0\u7684\u60c5\u51b5\u4e0b\u624d\u4f1a\u4f7f\u7528\u5b83\u3002 \u8fd9\u4e9b\u64cd\u4f5c\u7b26\u5e94\u5f53\u653e\u7f6e\u5728\u7d27\u90bb\u7740\u64cd\u4f5c\u6570\u524d\u9762\u7684\u4f4d\u7f6e\uff08\u4f8b\u5982\u5e94\u8be5\u5199\u4f5c -x \u800c\u4e0d\u662f - x \uff09\u3002 \u4e0d\u8981\u628a\u4e00\u5143\u8d1f\u53f7\u8fd0\u7b97\u7b26\u548c\u51cf\u53f7\u641e\u6df7\u4e86\uff0c\u5c3d\u7ba1\u5b83\u4eec\u7684\u7b26\u53f7\u7684\u786e\u662f\u540c\u4e00\u4e2a\u3002\u4f8b\u5982\uff0c\u5728\u8868\u8fbe\u5f0f x = 5 - -3; \u4e2d\uff0c\u7b2c\u4e00\u4e2a - \u662f\u51cf\u53f7\uff0c\u7b2c\u4e8c\u4e2a - \u5219\u662f\u8d1f\u53f7\u3002","title":"\u4e00\u5143\u6570\u5b66\u8fd0\u7b97\u7b26"},{"location":"5-2-Arithmetic-operators/#_2","text":"\u4e8c\u5143\u6570\u5b66\u8fd0\u7b97\u7b26\u6709 5 \u4e2a\uff0c\u5b83\u4eec\u9700\u8981\u4e24\u4e2a\u64cd\u4f5c\u6570\u3002 \u8fd0\u7b97\u7b26 \u7b26\u53f7 \u5f62\u5f0f \u64cd\u4f5c Addition + x + y x \u52a0 y Subtraction - x - y x \u51cf y Multiplication * x * y x \u4e58 y Division / x / y x \u9664 y Modulus (Remainder) % x % y x \u9664 y \u7684\u4f59\u6570 \u52a0\u51cf\u4e58\u548c\u65e5\u5e38\u751f\u6d3b\u4e2d\u7684\u7528\u6cd5\u662f\u4e00\u6837\u7684\u3002 \u9664\u6cd5\u548c\u6c42\u6a21\uff08\u6c42\u4f59\u6570\uff09\u5219\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u989d\u5916\u7684\u8bf4\u660e\u3002\u6211\u4eec\u4f1a\u5728\u672c\u8282\u8bfe\u4ecb\u7ecd\u9664\u6cd5\uff0c\u4e0b\u8282\u8bfe\u4f1a\u4ecb\u7ecd\u6c42\u4f59\u3002","title":"\u4e8c\u5143\u6570\u5b66\u8fd0\u7b97\u7b26"},{"location":"5-2-Arithmetic-operators/#_3","text":"\u53ef\u4ee5\u7b80\u5355\u5730\u8ba4\u4e3a\u9664\u6cd5\u8fd0\u7b97\u7b26\u6709\u4e24\u79cd\u201d\u6a21\u5f0f\u201c\u3002 \u5982\u679c\u4efb\u4e00(\u6216\u5168\u90e8)\u7684\u64cd\u4f5c\u6570\u662f\u6d6e\u70b9\u503c\uff0c\u5219\u9664\u6cd5\u64cd\u4f5c\u6570\u4f1a\u6267\u884c \u6d6e\u70b9\u9664\u6cd5 \u3002\u6d6e\u70b9\u6570\u9664\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6d6e\u70b9\u503c\uff0c\u5c0f\u6570\u90e8\u5206\u4f1a\u88ab\u4fdd\u7559\u3002\u4f8b\u5982 7.0 / 4 = 1.75 \u3001 7 / 4.0 = 1.75 \u548c 7.0 / 4.0 = 1.75 \u3002\u548c\u5176\u4ed6\u6d6e\u70b9\u8fd0\u7b97\u4e00\u6837\uff0c \u820d\u5165\u8bef\u5dee \u53ef\u80fd\u4f1a\u53d1\u751f\u3002 \u5982\u679c\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u662f\u6574\u578b\uff0c\u5219\u9664\u6cd5\u8fd0\u7b97\u7b26\u4f1a\u6267\u884c\u6574\u578b\u9664\u6cd5\u3002\u6574\u578b\u9664\u6cd5\u4f1a\u4e22\u5f03\u7ed3\u679c\u7684\u5c0f\u6570\u90e8\u5206\u5e76\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\u6570\u3002\u4f8b\u5982 7 / 4 = 1 \uff0c\u56e0\u4e3a\u5c0f\u6570\u90e8\u5206\u88ab\u4e22\u5f03\u4e86\u3002\u7c7b\u4f3c\u7684 -7 / 4 = -1 \u56e0\u4e3a\u5c0f\u6570\u90e8\u5206\u88ab\u4e22\u5f03\u4e86\u3002","title":"\u6574\u6570\u9664\u6cd5\u548c\u6d6e\u70b9\u6570\u9664\u6cd5"},{"location":"5-2-Arithmetic-operators/#static_cast","text":"\u5982\u679c\u6709\u4e24\u4e2a\u6574\u578b\u6570\uff0c\u6211\u5e0c\u671b\u4e0d\u8981\u4e22\u5931\u5b83\u4eec\u505a\u9664\u6cd5\u540e\u7684\u5c0f\u6570\u90e8\u5206\uff0c\u90a3\u4e48\u5e94\u8be5\u600e\u4e48\u505a\u5462\uff1f \u5728 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast \u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 static_cast<> \u5c06\u5b57\u7b26\u8f6c\u6362\u6210\u6574\u578b\u4ee5\u4fbf\u6253\u5370\u65f6\u80fd\u591f\u5c06\u5176\u6253\u5370\u4e3a\u4e00\u4e2a\u6574\u6570\u3002 \u8fd9\u91cc\uff0c\u6211\u4eec\u540c\u6837\u53ef\u4ee5\u7528 static_cast<> \u5c06\u4e00\u4e2a\u6574\u578b\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u4ee5\u4fbf\u4f7f\u7528\u6d6e\u70b9\u6570\u9664\u6cd5\u3002\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 7 }; int y { 4 }; std :: cout << \"int / int = \" << x / y << '\\n' ; std :: cout << \"double / int = \" << static_cast < double > ( x ) / y << '\\n' ; std :: cout << \"int / double = \" << x / static_cast < double > ( y ) << '\\n' ; std :: cout << \"double / double = \" << static_cast < double > ( x ) / static_cast < double > ( y ) << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 int / int = 1 double / int = 1.75 int / double = 1.75 double / double = 1.75 \u4ece\u7ed3\u679c\u6765\u770b\uff0c\u53ea\u8981\u6709\u4efb\u4f55\u4e00\u4e2a\u64cd\u4f5c\u6570\u662f\u6d6e\u70b9\u6570\uff0c\u5c31\u4f1a\u6267\u884c\u6d6e\u70b9\u9664\u6cd5\uff0c\u800c\u4e0d\u662f\u6574\u6570\u9664\u6cd5\u3002","title":"\u4f7f\u7528 static_cast \u5bf9\u6574\u578b\u6570\u8fdb\u884c\u6d6e\u70b9\u9664\u6cd5"},{"location":"5-2-Arithmetic-operators/#0","text":"\u9664 0 (\u6216 0.0) \u5f88\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4f60\u7684\u7a0b\u5e8f\u5d29\u6e83\uff0c\u56e0\u4e3a\u8be5\u8fd0\u7b97\u7684\u7ed3\u679c\u662f\u6ca1\u6709\u6570\u5b66\u5b9a\u4e49\u7684\u3002 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { std :: cout << \"Enter a divisor: \" ; int x {}; std :: cin >> x ; std :: cout << \"12 / \" << x << \" = \" << 12 / x << '\\n' ; return 0 ; } \u5982\u679c\u4f60\u8fd0\u884c\u4e0a\u8ff0\u7a0b\u5e8f\u5e76\u8f93\u5165 0\uff0c\u4f60\u7684\u7a0b\u5e8f\u4f1a\u5d29\u6e83\u6216\u5f02\u5e38\u7ec8\u6b62\u3002\u4f60\u53ef\u4ee5\u4eb2\u81ea\u4f53\u9a8c\u4e00\u4e0b\uff0c\u8fd9\u4e0d\u4f1a\u5bf9\u4f60\u7535\u8111\u6709\u4ec0\u4e48\u5f71\u54cd\u7684\u3002","title":"\u9664 0 \u9519\u8bef"},{"location":"5-2-Arithmetic-operators/#_4","text":"\u8fd0\u7b97\u7b26 \u7b26\u53f7 \u683c\u5f0f \u64cd\u4f5c Assignment = x = y y \u8d4b\u503c\u7ed9 x Addition assignment += x += y \u628a y \u52a0\u5230 x \u4e0a Subtraction assignment -= x -= y \u4ece x \u4e2d\u51cf\u53bb y Multiplication assignment *= x *= y \u628a x \u4e58\u4e0a y Division assignment /= x /= y \u628a x \u9664 y Modulus assignment %= x %= y \u628a x / y \u7684\u4f59\u6570\u8d4b\u503c\u7ed9 x \u5b66\u5230\u8fd9\u91cc\uff0c\u5982\u679c\u4f60\u9700\u8981\u7ed9\u4e00\u4e2a\u53d8\u91cf\u52a0\u4e0a 4\uff0c\u4f60\u5f88\u53ef\u80fd\u4f1a\u8fd9\u6837\u505a\uff1a 1 x = x + 4 ; // add 4 to existing value of x \u8fd9\u4e48\u505a\u662f\u6ca1\u95ee\u9898\u7684\uff0c\u5c31\u662f\u6709\u4e9b\u5570\u55e6\uff0c\u5b83\u9700\u8981\u4e24\u4e2a\u8fd0\u7b97\u7b26\u624d\u80fd\u5b8c\u6210\uff08 operator+ \u548c operator= \uff09\u3002 \u56e0\u4e3a x = x + 4 \u8fd9\u6837\u7684\u8868\u8fbe\u5f0f\u592a\u5e38\u89c1\u4e86\uff0c\u6240\u4ee5 C++ \u63d0\u4f9b\u4e86\u4e94\u79cd\u540c\u6837\u529f\u80fd\u4f46\u66f4\u65b9\u4fbf\u7684\u8fd0\u7b97\u7b26\u3002 x = x + 4 \u53ef\u4ee5\u66ff\u6362\u4e3a x += 4 \u3002\u800c\u5bf9\u4e8e x = x * y \uff0c\u4f60\u53ef\u4ee5\u5199\u6210 x *= y \u3002 \u56e0\u6b64\uff0c\u4e0a\u8ff0\u4ee3\u7801\u53ef\u4ee5\u53d8\u6210\u4e0b\u9762\u7684\u5f62\u5f0f\uff1a 1 x += 4 ; // add 4 to existing value of x","title":"\u7b97\u6570\u8d4b\u503c\u8fd0\u7b97\u7b26"},{"location":"5-3-Modulus-and-Exponentiation/","tags":["operator"],"text":"Key Takeaway \u5728\u9700\u8981\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u80fd\u88ab\u53e6\u5916\u4e00\u4e2a\u6570\u6574\u9664\uff08\u9664\u6cd5\u7ed3\u679c\u6ca1\u6709\u4f59\u6570\uff09\u65f6\u6c42\u6a21\u8fd0\u7b97\u975e\u5e38\u6709\uff1a\u5982\u679c x%y \u7ed3\u679c\u4e3a0\uff0c\u8bf4\u660e x \u53ef\u4ee5\u88ab y \u6574\u9664\u3002 \u8d1f\u6570\u4e5f\u53ef\u4ee5\u6c42\u6a21\u3002 x % y \u7684\u7ed3\u679c\u603b\u662f\u548c x \u7684\u7b26\u53f7\u4e00\u6837\u3002 C++ \u4e0d\u652f\u6301\u6307\u6570\u8fd0\u7b97\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528 \u4e2d\u7684 pow \u51fd\u6570 \u7f16\u5199\u6574\u578b\u6307\u6570\u8fd0\u7b97\u51fd\u6570\u53ef\u4ee5\u4f7f\u7528\u5e73\u65b9\u6c42\u5e42\u6cd5 \u6c42\u6a21\u8fd0\u7b97\u7b26 \u6c42\u6a21\u8fd0\u7b97\u7b26\uff08\u6709\u65f6\u5019\u4e5f\u88ab\u53eb\u505a\u6c42\u4f59\u8fd0\u7b97\u7b26\uff09\u53ef\u4ee5\u8fd4\u56de\u6574\u6570\u9664\u6cd5\u7ed3\u679c\u7684\u4f59\u6570\u3002\u4f8b\u5982 7 / 4 = 1 \u4f59 3\u3002\u56e0\u6b64 7 % 4 = 3\u3002\u53c8\u6bd4\u5982\uff0c25 / 7 = 3 \u4f59 4\uff0c\u56e0\u6b64 25 % 7 = 4\u3002\u6c42\u6a21\u8fd0\u7b97\u53ea\u80fd\u7528\u4e8e\u6574\u6570\u64cd\u4f5c\u6570\u3002 \u5728\u9700\u8981\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u80fd\u88ab\u53e6\u5916\u4e00\u4e2a\u6570\u6574\u9664\uff08\u9664\u6cd5\u7ed3\u679c\u6ca1\u6709\u4f59\u6570\uff09\u65f6\u6c42\u6a21\u8fd0\u7b97\u975e\u5e38\u6709\uff1a\u5982\u679c x%y \u7ed3\u679c\u4e3a0\uff0c\u8bf4\u660e x \u53ef\u4ee5\u88ab y \u6574\u9664\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another integer: \" ; int y {}; std :: cin >> y ; std :: cout << \"The remainder is: \" << x % y << '\\n' ; if (( x % y ) == 0 ) std :: cout << x << \" is evenly divisible by \" << y << '\\n' ; else std :: cout << x << \" is not evenly divisible by \" << y << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 Enter an integer: 6 Enter another integer: 3 The remainder is: 0 6 is evenly divisible by 3 1 2 3 4 Enter an integer: 6 Enter another integer: 4 The remainder is: 2 6 is not evenly divisible by 4 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5b9e\u65f6\u5982\u679c\u7b2c\u4e8c\u4e2a\u6570\u6bd4\u7b2c\u4e00\u4e2a\u6570\u5927\u4f1a\u600e\u4e48\u6837\uff1a 1 2 3 4 Enter an integer: 2 Enter another integer: 4 The remainder is: 2 2 is not evenly divisible by 4 \u4f59\u6570\u662f 2 \uff0c\u4e4d\u4e00\u770b\u539f\u56e0\u53ef\u80fd\u4e0d\u662f\u5f88\u660e\u663e\uff0c\u4f46\u5176\u5b9e\u5f88\u7b80\u5355\uff1a2 / 4 \u662f 0 (\u6574\u6570\u9664\u6cd5) \u4f59\u6570\u4e3a 2\u3002\u4efb\u4f55\u65f6\u5019\u53ea\u8981\u7b2c\u4e8c\u4e2a\u6570\u5927\u4e8e\u7b2c\u4e00\u4e2a\uff0c\u5219\u7b2c\u4e00\u4e2a\u6570\u4e3a\u4f59\u6570\u3002 \u8d1f\u6570\u6c42\u6a21 \u8d1f\u6570\u4e5f\u53ef\u4ee5\u6c42\u6a21\u3002 x % y \u7684\u7ed3\u679c\u603b\u662f\u548c x \u7684\u7b26\u53f7\u4e00\u6837\u3002 \u6267\u884c\u4e0a\u8ff0\u7a0b\u5e8f\uff1a 1 2 3 4 Enter an integer: -6 Enter another integer: 4 The remainder is: -2 -6 is not evenly divisible by 4 1 2 3 4 Enter an integer: 6 Enter another integer: -4 The remainder is: 2 6 is not evenly divisible by -4 \u5728\u4e0a\u9762\u4e24\u79cd\u4f8b\u5b50\u4e2d\u53ef\u4ee5\u770b\u5230\u4f59\u6570\u7684\u7b26\u53f7\u603b\u662f\u548c\u7b2c\u4e00\u4e2a\u64cd\u4f5c\u7b26\u4e00\u6837\u3002 \u6307\u6570\u8fd0\u7b97\u7b26\u53bb\u54ea\u4e86? \u4f60\u53ef\u80fd\u5df2\u7ecf\u6ce8\u610f\u5230\u4e86 ^ \u8fd0\u7b97\u7b26(\u901a\u5e38\u7528\u4e8e\u8868\u793a\u6307\u6570\u8fd0\u7b97)\u5728 C++ \u4e2d\u662f\u5f02\u6216\u8fd0\u7b97\u7b26 (\u5728 O.3 -- Bit manipulation with bitwise operators and bit masks \u4e2d\u4ecb\u7ecd)\u3002C++ \u5e76\u6ca1\u6709\u6307\u6570\u8fd0\u7b97\u7b26\u3002 \u5728 C++ \u4e2d\u8fdb\u884c\u6307\u6570\u8fd0\u7b97\u9700\u8981 #include \u5934\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528 pow() \u51fd\u6570\uff1a 1 2 3 #include double x { std :: pow ( 3.0 , 4.0 ) }; // 3 to the 4th power \u6ce8\u610f\uff0c\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u90fd\u662f double\u3002\u800c\u7531\u4e8e\u6d6e\u70b9\u6570\u5b58\u5728 \u820d\u5165\u8bef\u5dee \uff0c\u6240\u4ee5 pow() \u7684\u7ed3\u679c\u53ef\u80fd\u5e76\u4e0d\u7cbe\u786e\uff08\u5373\u4f7f\u4f60\u4f20\u5165\u7684\u662f\u6574\u6570\uff09\u3002 \u5982\u679c\u4f60\u60f3\u8fdb\u884c\u6574\u6570\u6307\u6570\u8fd0\u7b97\uff0c\u4f60\u6700\u597d\u81ea\u5df1\u5199\u4e00\u4e2a\u51fd\u6570\u3002\u4e0b\u9762\u8fd9\u4e2a\u51fd\u6570\u5c31\u5b9e\u73b0\u4e86\u6574\u578b\u7684\u6307\u6570\u8fd0\u7b97\uff08\u4f7f\u7528\u4e86\u4e0d\u592a\u76f4\u89c2\u7684 \u5e73\u65b9\u6c42\u5e42 \u7b97\u6cd5\u4ee5\u63d0\u9ad8\u6548\u7387\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include // for std::int64_t #include // for assert // note: exp must be non-negative std :: int64_t powint ( int base , int exp ) { assert ( exp > 0 && \"powint: exp parameter has negative value\" ); std :: int64_t result { 1 }; while ( exp ) { if ( exp & 1 ) result *= base ; exp >>= 1 ; base *= base ; } return result ; } int main () { std :: cout << powint ( 7 , 12 ); // 7 to the 12th power return 0 ; } \u8bd1\u8005\u6ce8 \u6c42x\u7684n\u6b21\u65b9\uff0c\u6700\u76f4\u63a5\u7684\u7b97\u6cd5\u662f\u628ax\u4e58\u4ee5x\u6267\u884cn\u6b21\uff0c\u4f46\u662f\u8fd9\u6837\u5f88\u6162\u3002 \u4f7f\u7528\u5e73\u65b9\u6c42\u5e42\u7684\u65b9\u6cd5\u53ef\u4ee5\u6781\u5927\u63d0\u9ad8\u901f\u5ea6\uff0c\u5b83\u7684\u539f\u7406\u5982\u4e0b\uff1a \u5047\u8bbe\u6c42x\u768416\u6b21\u65b9\uff0c\u5176\u5b9e\u6ca1\u5fc5\u8981\u628ax\u4e58\u4ee5x\u6267\u884c16\u6b21 \u66f4\u5feb\u7684\u65b9\u6cd5\u662f\u628ax\u5e73\u65b9\uff0c\u7136\u540e\u518d\u5e73\u65b9\uff0c\u518d\u5e73\u65b9\uff0c\u518d\u5e73\u65b9\u3002\u5373 \\(((((x^2)^2)^2)^2)\\) \uff0c\u56e0\u4e3a\u6211\u4eec\u77e5\u9053\u6c42\u6307\u6570\u7684\u6307\u6570\u5b9e\u9645\u4e0a\u662f\u628a\u4e24\u4e2a\u6307\u6570\u76f8\u4e58\u3002\u6240\u4ee5 \\(((((x^2)^2)^2)^2)=x^{16}\\) \u5982\u679c\u8f6c\u6362\u4e3a\u66f4\u4e00\u822c\u7684\u5f62\u5f0f\uff0cx\u7684n\u6b21\u65b9\u5176\u5b9e\u5c31\u662f\u770bn\u8f6c\u6362\u62102\u8fdb\u5236\u4e4b\u540e\uff0c\u91cc\u9762\u5305\u542b\u591a\u5c11\u4e2a1\u3002\u5047\u8bben = 37\uff0c\u5219\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u662f 100101 \u3002 \\(x^{37} = x^1 * x^{100} * x^{10000}\\) , \u5373 \\(x^{37} = x^1 * x^{4} * x^{32}\\) \u6240\u4ee5\uff0c\u6211\u4eec\u53ea\u8981\u628a\u6307\u6570\u5148\u8f6c\u53d8\u6210\u4e8c\u8fdb\u5236\uff0c\u7136\u540e\u4e00\u4f4d\u4e00\u4f4d\u7684\u770b\uff0c\u5982\u679c\u9047\u52301\uff0c\u5219\u770b\u8be5\u4f4d\u662f\u7b2c\u51e0\u4f4d\uff0c\u7136\u540e\u6c422\u7684\u51e0\u6b21\u65b9\uff0c\u5047\u8bbe\u7ed3\u679c\u4e3an\uff0c\u5219\u518d\u6b21\u6c42x\u7684n\u6b21\u65b9\uff0c\u6700\u540e\u628a\u6240\u6709\u7684\u7ed3\u679c\u4e58\u8d77\u6765\u3002 \u4e0d\u8fc7\u6309\u7167\u4e0a\u9762\u8ba1\u7b97\u6bcf\u4e2a\u90e8\u5206\u7684\u65f6\u5019\u8fd8\u662f\u5f88\u9ebb\u70e6\uff0c\u5b9e\u9645\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a \u4e0d\u8bba\u662f1\u8fd8\u662f0\uff0c\u90fd\u628ax\u81ea\u8eab\u6210\u4ee5\u81ea\u8eab \u5982\u679c\u9047\u52301\uff0c\u5219\u628ax\u7684\u7ed3\u679c\u4e58\u5230result\u4e0a \u5982\u679c\u9047\u52300\uff0c\u8df3\u8fc7 \u6700\u540e\u7684\u7ed3\u679c\u76f8\u5f53\u4e8e\u6bcf\u5904\u7406\u6307\u6570\u7684\u4e00\u4e2a\u4f4d\uff0cx\u90fd\u5e73\u65b9\u4e00\u6b21\u3002\u7136\u540e\u53ea\u6709a\u4f4d\u4e0a\u4e3a1\u65f6\uff0cx\u7684a\u6b21\u65b9\u624d\u88ab\u4e58\u5230\u7ed3\u679c\u4e0a\uff0c\u8fd9\u6837\u5c31\u5f88\u81ea\u7136\uff0c\u4e0d\u7528\u518d\u7279\u610f\u53bb\u8ba1\u7b97\u5f53\u524d\u662f\u7b2c\u51e0\u4f4d\u4e86\u3002 \u89c6\u9891\u8bb2\u89e3 \u7ed3\u679c\u4e3a\uff1a 1 13841287201 \u5982\u679c\u4f60\u4e0d\u7406\u89e3\u8fd9\u4e2a\u51fd\u6570\u7684\u539f\u7406\u4e5f\u4e0d\u8981\u7d27 \u2014\u2014 \u4f7f\u7528\u5b83\u5e76\u4e0d\u9700\u8981\u4f60\u4e86\u89e3\u5b83\u7684\u539f\u7406\u3002 \u6ce8\u610f \u5927\u90e8\u5206\u60c5\u51b5\u4e0b\uff0c\u6574\u578b\u7684\u6307\u6570\u8fd0\u7b97\u90fd\u53ef\u80fd\u4f1a\u6ea2\u51fa\u6574\u578b\u3002\u8fd9\u5f88\u53ef\u80fd\u662f\u8be5\u51fd\u6570\u6ca1\u6709\u88ab\u5305\u542b\u5230\u6807\u51c6\u5e93\u4e2d\u7684\u539f\u56e0\u3002","title":"5.3 - \u6c42\u6a21\u548c\u6307\u6570\u8fd0\u7b97"},{"location":"5-3-Modulus-and-Exponentiation/#_1","text":"\u6c42\u6a21\u8fd0\u7b97\u7b26\uff08\u6709\u65f6\u5019\u4e5f\u88ab\u53eb\u505a\u6c42\u4f59\u8fd0\u7b97\u7b26\uff09\u53ef\u4ee5\u8fd4\u56de\u6574\u6570\u9664\u6cd5\u7ed3\u679c\u7684\u4f59\u6570\u3002\u4f8b\u5982 7 / 4 = 1 \u4f59 3\u3002\u56e0\u6b64 7 % 4 = 3\u3002\u53c8\u6bd4\u5982\uff0c25 / 7 = 3 \u4f59 4\uff0c\u56e0\u6b64 25 % 7 = 4\u3002\u6c42\u6a21\u8fd0\u7b97\u53ea\u80fd\u7528\u4e8e\u6574\u6570\u64cd\u4f5c\u6570\u3002 \u5728\u9700\u8981\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u80fd\u88ab\u53e6\u5916\u4e00\u4e2a\u6570\u6574\u9664\uff08\u9664\u6cd5\u7ed3\u679c\u6ca1\u6709\u4f59\u6570\uff09\u65f6\u6c42\u6a21\u8fd0\u7b97\u975e\u5e38\u6709\uff1a\u5982\u679c x%y \u7ed3\u679c\u4e3a0\uff0c\u8bf4\u660e x \u53ef\u4ee5\u88ab y \u6574\u9664\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another integer: \" ; int y {}; std :: cin >> y ; std :: cout << \"The remainder is: \" << x % y << '\\n' ; if (( x % y ) == 0 ) std :: cout << x << \" is evenly divisible by \" << y << '\\n' ; else std :: cout << x << \" is not evenly divisible by \" << y << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 Enter an integer: 6 Enter another integer: 3 The remainder is: 0 6 is evenly divisible by 3 1 2 3 4 Enter an integer: 6 Enter another integer: 4 The remainder is: 2 6 is not evenly divisible by 4 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5b9e\u65f6\u5982\u679c\u7b2c\u4e8c\u4e2a\u6570\u6bd4\u7b2c\u4e00\u4e2a\u6570\u5927\u4f1a\u600e\u4e48\u6837\uff1a 1 2 3 4 Enter an integer: 2 Enter another integer: 4 The remainder is: 2 2 is not evenly divisible by 4 \u4f59\u6570\u662f 2 \uff0c\u4e4d\u4e00\u770b\u539f\u56e0\u53ef\u80fd\u4e0d\u662f\u5f88\u660e\u663e\uff0c\u4f46\u5176\u5b9e\u5f88\u7b80\u5355\uff1a2 / 4 \u662f 0 (\u6574\u6570\u9664\u6cd5) \u4f59\u6570\u4e3a 2\u3002\u4efb\u4f55\u65f6\u5019\u53ea\u8981\u7b2c\u4e8c\u4e2a\u6570\u5927\u4e8e\u7b2c\u4e00\u4e2a\uff0c\u5219\u7b2c\u4e00\u4e2a\u6570\u4e3a\u4f59\u6570\u3002","title":"\u6c42\u6a21\u8fd0\u7b97\u7b26"},{"location":"5-3-Modulus-and-Exponentiation/#_2","text":"\u8d1f\u6570\u4e5f\u53ef\u4ee5\u6c42\u6a21\u3002 x % y \u7684\u7ed3\u679c\u603b\u662f\u548c x \u7684\u7b26\u53f7\u4e00\u6837\u3002 \u6267\u884c\u4e0a\u8ff0\u7a0b\u5e8f\uff1a 1 2 3 4 Enter an integer: -6 Enter another integer: 4 The remainder is: -2 -6 is not evenly divisible by 4 1 2 3 4 Enter an integer: 6 Enter another integer: -4 The remainder is: 2 6 is not evenly divisible by -4 \u5728\u4e0a\u9762\u4e24\u79cd\u4f8b\u5b50\u4e2d\u53ef\u4ee5\u770b\u5230\u4f59\u6570\u7684\u7b26\u53f7\u603b\u662f\u548c\u7b2c\u4e00\u4e2a\u64cd\u4f5c\u7b26\u4e00\u6837\u3002","title":"\u8d1f\u6570\u6c42\u6a21"},{"location":"5-3-Modulus-and-Exponentiation/#_3","text":"\u4f60\u53ef\u80fd\u5df2\u7ecf\u6ce8\u610f\u5230\u4e86 ^ \u8fd0\u7b97\u7b26(\u901a\u5e38\u7528\u4e8e\u8868\u793a\u6307\u6570\u8fd0\u7b97)\u5728 C++ \u4e2d\u662f\u5f02\u6216\u8fd0\u7b97\u7b26 (\u5728 O.3 -- Bit manipulation with bitwise operators and bit masks \u4e2d\u4ecb\u7ecd)\u3002C++ \u5e76\u6ca1\u6709\u6307\u6570\u8fd0\u7b97\u7b26\u3002 \u5728 C++ \u4e2d\u8fdb\u884c\u6307\u6570\u8fd0\u7b97\u9700\u8981 #include \u5934\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528 pow() \u51fd\u6570\uff1a 1 2 3 #include double x { std :: pow ( 3.0 , 4.0 ) }; // 3 to the 4th power \u6ce8\u610f\uff0c\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u90fd\u662f double\u3002\u800c\u7531\u4e8e\u6d6e\u70b9\u6570\u5b58\u5728 \u820d\u5165\u8bef\u5dee \uff0c\u6240\u4ee5 pow() \u7684\u7ed3\u679c\u53ef\u80fd\u5e76\u4e0d\u7cbe\u786e\uff08\u5373\u4f7f\u4f60\u4f20\u5165\u7684\u662f\u6574\u6570\uff09\u3002 \u5982\u679c\u4f60\u60f3\u8fdb\u884c\u6574\u6570\u6307\u6570\u8fd0\u7b97\uff0c\u4f60\u6700\u597d\u81ea\u5df1\u5199\u4e00\u4e2a\u51fd\u6570\u3002\u4e0b\u9762\u8fd9\u4e2a\u51fd\u6570\u5c31\u5b9e\u73b0\u4e86\u6574\u578b\u7684\u6307\u6570\u8fd0\u7b97\uff08\u4f7f\u7528\u4e86\u4e0d\u592a\u76f4\u89c2\u7684 \u5e73\u65b9\u6c42\u5e42 \u7b97\u6cd5\u4ee5\u63d0\u9ad8\u6548\u7387\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include // for std::int64_t #include // for assert // note: exp must be non-negative std :: int64_t powint ( int base , int exp ) { assert ( exp > 0 && \"powint: exp parameter has negative value\" ); std :: int64_t result { 1 }; while ( exp ) { if ( exp & 1 ) result *= base ; exp >>= 1 ; base *= base ; } return result ; } int main () { std :: cout << powint ( 7 , 12 ); // 7 to the 12th power return 0 ; } \u8bd1\u8005\u6ce8 \u6c42x\u7684n\u6b21\u65b9\uff0c\u6700\u76f4\u63a5\u7684\u7b97\u6cd5\u662f\u628ax\u4e58\u4ee5x\u6267\u884cn\u6b21\uff0c\u4f46\u662f\u8fd9\u6837\u5f88\u6162\u3002 \u4f7f\u7528\u5e73\u65b9\u6c42\u5e42\u7684\u65b9\u6cd5\u53ef\u4ee5\u6781\u5927\u63d0\u9ad8\u901f\u5ea6\uff0c\u5b83\u7684\u539f\u7406\u5982\u4e0b\uff1a \u5047\u8bbe\u6c42x\u768416\u6b21\u65b9\uff0c\u5176\u5b9e\u6ca1\u5fc5\u8981\u628ax\u4e58\u4ee5x\u6267\u884c16\u6b21 \u66f4\u5feb\u7684\u65b9\u6cd5\u662f\u628ax\u5e73\u65b9\uff0c\u7136\u540e\u518d\u5e73\u65b9\uff0c\u518d\u5e73\u65b9\uff0c\u518d\u5e73\u65b9\u3002\u5373 \\(((((x^2)^2)^2)^2)\\) \uff0c\u56e0\u4e3a\u6211\u4eec\u77e5\u9053\u6c42\u6307\u6570\u7684\u6307\u6570\u5b9e\u9645\u4e0a\u662f\u628a\u4e24\u4e2a\u6307\u6570\u76f8\u4e58\u3002\u6240\u4ee5 \\(((((x^2)^2)^2)^2)=x^{16}\\) \u5982\u679c\u8f6c\u6362\u4e3a\u66f4\u4e00\u822c\u7684\u5f62\u5f0f\uff0cx\u7684n\u6b21\u65b9\u5176\u5b9e\u5c31\u662f\u770bn\u8f6c\u6362\u62102\u8fdb\u5236\u4e4b\u540e\uff0c\u91cc\u9762\u5305\u542b\u591a\u5c11\u4e2a1\u3002\u5047\u8bben = 37\uff0c\u5219\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u662f 100101 \u3002 \\(x^{37} = x^1 * x^{100} * x^{10000}\\) , \u5373 \\(x^{37} = x^1 * x^{4} * x^{32}\\) \u6240\u4ee5\uff0c\u6211\u4eec\u53ea\u8981\u628a\u6307\u6570\u5148\u8f6c\u53d8\u6210\u4e8c\u8fdb\u5236\uff0c\u7136\u540e\u4e00\u4f4d\u4e00\u4f4d\u7684\u770b\uff0c\u5982\u679c\u9047\u52301\uff0c\u5219\u770b\u8be5\u4f4d\u662f\u7b2c\u51e0\u4f4d\uff0c\u7136\u540e\u6c422\u7684\u51e0\u6b21\u65b9\uff0c\u5047\u8bbe\u7ed3\u679c\u4e3an\uff0c\u5219\u518d\u6b21\u6c42x\u7684n\u6b21\u65b9\uff0c\u6700\u540e\u628a\u6240\u6709\u7684\u7ed3\u679c\u4e58\u8d77\u6765\u3002 \u4e0d\u8fc7\u6309\u7167\u4e0a\u9762\u8ba1\u7b97\u6bcf\u4e2a\u90e8\u5206\u7684\u65f6\u5019\u8fd8\u662f\u5f88\u9ebb\u70e6\uff0c\u5b9e\u9645\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a \u4e0d\u8bba\u662f1\u8fd8\u662f0\uff0c\u90fd\u628ax\u81ea\u8eab\u6210\u4ee5\u81ea\u8eab \u5982\u679c\u9047\u52301\uff0c\u5219\u628ax\u7684\u7ed3\u679c\u4e58\u5230result\u4e0a \u5982\u679c\u9047\u52300\uff0c\u8df3\u8fc7 \u6700\u540e\u7684\u7ed3\u679c\u76f8\u5f53\u4e8e\u6bcf\u5904\u7406\u6307\u6570\u7684\u4e00\u4e2a\u4f4d\uff0cx\u90fd\u5e73\u65b9\u4e00\u6b21\u3002\u7136\u540e\u53ea\u6709a\u4f4d\u4e0a\u4e3a1\u65f6\uff0cx\u7684a\u6b21\u65b9\u624d\u88ab\u4e58\u5230\u7ed3\u679c\u4e0a\uff0c\u8fd9\u6837\u5c31\u5f88\u81ea\u7136\uff0c\u4e0d\u7528\u518d\u7279\u610f\u53bb\u8ba1\u7b97\u5f53\u524d\u662f\u7b2c\u51e0\u4f4d\u4e86\u3002 \u89c6\u9891\u8bb2\u89e3 \u7ed3\u679c\u4e3a\uff1a 1 13841287201 \u5982\u679c\u4f60\u4e0d\u7406\u89e3\u8fd9\u4e2a\u51fd\u6570\u7684\u539f\u7406\u4e5f\u4e0d\u8981\u7d27 \u2014\u2014 \u4f7f\u7528\u5b83\u5e76\u4e0d\u9700\u8981\u4f60\u4e86\u89e3\u5b83\u7684\u539f\u7406\u3002 \u6ce8\u610f \u5927\u90e8\u5206\u60c5\u51b5\u4e0b\uff0c\u6574\u578b\u7684\u6307\u6570\u8fd0\u7b97\u90fd\u53ef\u80fd\u4f1a\u6ea2\u51fa\u6574\u578b\u3002\u8fd9\u5f88\u53ef\u80fd\u662f\u8be5\u51fd\u6570\u6ca1\u6709\u88ab\u5305\u542b\u5230\u6807\u51c6\u5e93\u4e2d\u7684\u539f\u56e0\u3002","title":"\u6307\u6570\u8fd0\u7b97\u7b26\u53bb\u54ea\u4e86?"},{"location":"5-4-Increment-decrement-operators-and-side-effects/","tags":["operator"],"text":"Key Takeaway \u540e\u7f00\u7248\u672c\u7684\u8fd0\u7b97\u7b26\u591a\u4e86\u5f88\u591a\u6b65\u9aa4\uff08\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u526f\u672c\uff09\uff0c\u56e0\u6b64\u5b83\u7684\u6027\u80fd\u4e0d\u5982\u524d\u7f00\u7248\u672c \u5f3a\u70c8\u63a8\u8350\u4f7f\u7528\u524d\u7f00\u7248\u672c\u7684\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26\uff0c\u56e0\u4e3a\u5b83\u4eec\u6027\u80fd\u66f4\u597d\uff0c\u800c\u4e14\u4e5f\u4e0d\u5bb9\u6613\u5e26\u6765\u95ee\u9898 \u56e0\u4e3a C++ \u6807\u51c6\u6ca1\u6709\u89c4\u5b9a\u51fd\u6570\u53c2\u6570\u7684\u6c42\u503c\u987a\u5e8f\u3002\u6240\u4ee5 add(x, ++x) \u8fd9\u6837\u7684\u51fd\u6570\u8c03\u7528\u7ed3\u679c\u662f\u4e0d\u786e\u5b9a\u7684\uff0c\u56e0\u4e3a\u51fd\u6570 add \u7684\u5176\u4e2d\u4e00\u4e2a\u53c2\u6570\u5177\u6709\u526f\u4f5c\u7528\u3002 C++ \u6ca1\u6709\u5b9a\u4e49\u51fd\u6570\u53c2\u6570\u6216\u8fd0\u7b97\u7b26\u64cd\u4f5c\u6570\u7684\u6c42\u503c\u987a\u5e8f\u3002 \u53d8\u91cf\u7684\u81ea\u589e\u81ea\u51cf \u5c06\u4e00\u4e2a\u53d8\u91cf\u9012\u589e\uff08\u52a01\uff09\u548c\u9012\u51cf\uff08\u51cf1\uff09\u90fd\u662f\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u56e0\u6b64\u5b83\u4eec\u6709\u4e13\u95e8\u7684\u8fd0\u7b97\u7b26\u3002 \u8fd0\u7b97\u7b26 \u7b26\u53f7 \u5f62\u5f0f \u64cd\u4f5c Prefix increment (pre-increment) ++ ++x Increment x, then return x Prefix decrement (pre-decrement) \u2013\u2013 \u2013\u2013x Decrement x, then return x Postfix increment (post-increment) ++ x++ Copy x, then increment x, then return the copy Postfix decrement (post-decrement) \u2013\u2013 x\u2013\u2013 Copy x, then decrement x, then return the copy \u6ce8\u610f\uff0c\u6bcf\u79cd\u64cd\u4f5c\u7b26\u90fd\u6709\u4e24\u4e2a\u7248\u672c\uff0c\u4e00\u4e2a\u662f\u524d\u7f00\u5f62\u5f0f\uff08\u8fd0\u7b97\u7b26\u5728\u64cd\u4f5c\u7b26\u524d\u9762\uff09\uff0c\u4e00\u4e2a\u662f\u540e\u7f00\u5f62\u5f0f\uff08\u8fd0\u7b97\u7b26\u5728\u64cd\u4f5c\u7b26\u540e\u9762\uff09\u3002 \u524d\u7f00\u9012\u589e/\u9012\u51cf\u64cd\u4f5c\u7b26\u975e\u5e38\u7b80\u5355\uff0c\u5373\u64cd\u4f5c\u6570\u5148\u9012\u589e\u6216\u9012\u51cf\uff0c\u7136\u540e\u8868\u8fbe\u5f0f\u518d\u6c42\u503c\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { int x { 5 }; int y = ++ x ; // x is incremented to 6, x is evaluated to the value 6, and 6 is assigned to y std :: cout << x << ' ' << y ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 6 6 \u540e\u7f00\u81ea\u589e/\u81ea\u51cf\u7a0d\u5fae\u6709\u70b9\u590d\u6742\u3002\u9996\u5148\uff0c\u5b83\u62f7\u8d1d\u4e86\u4e00\u4efd\u64cd\u4f5c\u6570\uff0c\u7136\u540e\u5bf9\u64cd\u4f5c\u6570\uff08\u800c\u4e0d\u662f\u5b83\u7684\u62f7\u8d1d\uff09\u8fdb\u884c\u9012\u589e\u6216\u9012\u51cf\u3002\u6700\u540e\u5bf9\u62f7\u8d1d\u7684\u503c\uff08\u800c\u975e\u539f\u59cb\u503c\uff09\u8fdb\u884c\u6c42\u503c\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { int x { 5 }; int y = x ++ ; // x \u81ea\u589e\u4e3a6\uff0c\u62f7\u8d1d\u7684\u6700\u521d\u7684\u503c\u4e3a5,5\u88ab\u8d4b\u503c\u7ed9\u4e86y std :: cout << x << ' ' << y ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 6 5 \u8ba9\u6211\u4eec\u4ecb\u7ecd\u4e00\u4e0b\u7b2c\u516d\u884c\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a x \u7684\u4e34\u65f6\u62f7\u8d1d\uff0c\u503c\u548cx\u4e00\u6837\uff085\uff09\u3002\u968f\u540e\uff0c\u8fd9\u4e2a\u62f7\u8d1d\u88ab\u8fd4\u56de\u7136\u540e\u8d4b\u503c\u7ed9 y \u3002\u7136\u540e\uff0c\u8fd9\u4e2a\u4e34\u65f6\u7684\u62f7\u8d1d\u5c31\u88ab\u4e22\u5f03\u4e86\u3002 \u56e0\u6b64\uff0c y \u6700\u7ec8\u7684\u503c\u5c31\u662f 5\uff08\u9012\u589e\u524d\u7684\u503c\uff09\uff0c\u7136\u540e x \u7684\u503c\u53d8\u6210\u4e86 6\uff08\u9012\u589e\u540e\u7684\u503c\uff09\u3002 \u6ce8\u610f\uff0c\u540e\u7f00\u7248\u672c\u7684\u8fd0\u7b97\u7b26\u591a\u4e86\u5f88\u591a\u6b65\u9aa4\uff0c\u56e0\u6b64\u5b83\u7684\u6027\u80fd\u4e0d\u5982\u524d\u7f00\u7248\u672c\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u6f14\u793a\u4e86\u524d\u7f00\u9012\u589e\u9012\u51cf\u548c\u540e\u7f00\u9012\u589e\u9012\u51cf\u7684\u4e0d\u540c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 5 }; int y { 5 }; std :: cout << x << ' ' << y << '\\n' ; std :: cout << ++ x << ' ' << -- y << '\\n' ; // prefix std :: cout << x << ' ' << y << '\\n' ; std :: cout << x ++ << ' ' << y -- << '\\n' ; // postfix std :: cout << x << ' ' << y << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 5 5 6 4 6 4 6 4 7 3 \u5728\u7b2c\u516b\u884c\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u524d\u7f00\u9012\u589e\u9012\u51cf\u3002\u56e0\u6b64\u8fd9\u4e00\u884c\u4e2d x \u548c y \u5148\u9012\u589e\uff0c\u518d\u6253\u5370\uff0c\u56e0\u6b64\u5b83\u4eec\u66f4\u65b0\u540e\u7684\u503c\u88ab\u6253\u5370\u4e86\u51fa\u6765\u3002 \u5728\u7b2c\u5341\u884c\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u7684\u662f\u540e\u7f00\u9012\u589e\u9012\u51cf\u3002\u56e0\u6b64\u8fd9\u4e00\u884c\u4e2d\uff0cx \u548c y \u5148\u88ab\u62f7\u8d1d\uff0c\u7136\u540e\u6253\u5370\u3002\u6240\u4ee5\u6211\u4eec\u6ca1\u6709\u770b\u5230\u9012\u589e\u540e\u7684\u7ed3\u679c\u88ab\u6253\u5370\u51fa\u6765\u3002\u9012\u589e\u7684\u7ed3\u679c\u53ea\u6709\u518d\u4e0b\u4e00\u6b21\u6253\u5370\u65f6\u624d\u80fd\u6709\u6240\u4f53\u73b0\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f3a\u70c8\u63a8\u8350\u4f7f\u7528\u524d\u7f00\u7248\u672c\u7684\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26\uff0c\u56e0\u4e3a\u5b83\u4eec\u6027\u80fd\u66f4\u597d\uff0c\u800c\u4e14\u4e5f\u4e0d\u5bb9\u6613\u5e26\u6765\u95ee\u9898\u3002 \u526f\u4f5c\u7528 \u5982\u679c\u6211\u4eec\u8bf4\u4e00\u4e2a\u51fd\u6570\u6216\u8868\u8fbe\u5f0f\u5177\u6709\u526f\u4f5c\u7528\uff0c\u5219\u610f\u5473\u7740\u5b83\u6267\u884c\u540e\u5e26\u6765\u4e86\u67d0\u4e9b\u5f71\u54cd\u4e14\u8fd9\u4e9b\u5f71\u54cd\u5728\u51fd\u6570\u6216\u8868\u8fbe\u5f0f\u7684\u751f\u547d\u5468\u671f\u7ed3\u675f\u540e\u8fd8\u5b58\u5728\u3002 \u526f\u4f5c\u7528\u7684\u5178\u578b\u4f8b\u5b50\u5305\u62ec\u5bf9\u8c61\u7684\u503c\u88ab\u6539\u53d8\u3001\u8fdb\u884c\u4e86\u8f93\u5165\u8f93\u51fa\u6216\u8005\u66f4\u65b0\u4e86\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08\u4f8b\u5982\u6253\u5f00\u6216\u5173\u95ed\u67d0\u4e2a\u6309\u94ae\uff09\u3002 \u5f88\u591a\u65f6\u5019\uff0c\u526f\u4f5c\u7528\u662f\u6709\u7528\u7684\uff1a 1 2 3 x = 5 ; // the assignment operator modifies the state of x ++ x ; // operator++ modifies the state of x std :: cout << x ; // operator<< modifies the state of the console \u4e0a\u9762\u8d4b\u503c\u53f7\u7684\u526f\u4f5c\u7528\u662f\u6c38\u4e45\u5730\u6539\u53d8\u4e86 x \u7684\u503c\uff0c\u5373\u4f7f\u5728\u8d4b\u503c\u8bed\u53e5\u7ed3\u675f\u6267\u884c\u540e\uff0c x \u7684\u503c\u4e5f\u8fd8\u662f 5\u3002\u540c\u6837\u7684\uff0c ++ \u8fd0\u7b97\u7b26\u4e5f\u6709\u526f\u4f5c\u7528\uff0c ++x \u6267\u884c\u540e x \u7684\u503c\u5c31\u9012\u589e\u4e86\u3002\u6253\u5370 x \u540c\u6837\u6709\u526f\u4f5c\u7528\uff0c\u5b83\u6539\u53d8\u4e86\u63a7\u5236\u53f0\u7684\u72b6\u6001\uff0c\u56e0\u4e3a\u4f60\u53ef\u4ee5\u770b\u5230 x \u7684\u503c\u88ab\u6253\u5370\u5230\u4e86\u63a7\u5236\u53f0\u3002 \u4e0d\u8fc7\uff0c\u526f\u4f5c\u7528\u6709\u65f6\u5019\u4e5f\u4f1a\u5e26\u6765\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int add ( int x , int y ) { return x + y ; } int main () { int x { 5 }; int value { add ( x , ++ x ) }; // \u7a76\u7adf\u662f 5 + 6 \u8fd8\u662f 6 + 6? // \u8fd9\u53d6\u51b3\u4e8e\u4f60\u7684\u7f16\u8bd1\u5668\u5982\u4f55\u9009\u62e9\u51fd\u6570\u53c2\u6570\u7684\u6c42\u503c\u987a\u5e8f std :: cout << value << '\\n' ; // value \u53ef\u80fd\u662f 11 \u6216\u8005 12\uff0c\u53d6\u51b3\u4e8e\u7f16\u8bd1\u5668 return 0 ; } \u56e0\u4e3a C++ \u6807\u51c6\u6ca1\u6709\u89c4\u5b9a\u51fd\u6570\u53c2\u6570\u7684\u6c42\u503c\u987a\u5e8f\u3002\u5982\u679c\u5de6\u8fb9\u7684\u53c2\u6570\u5148\u6c42\u503c\uff0c\u5219\u5b9e\u9645\u8c03\u7528\u4e3a add(5,6) \uff0c\u7ed3\u679c\u4e3a 11\u3002\u5982\u679c\u53f3\u8fb9\u7684\u53c2\u6570\u5148\u6c42\u503c\uff0c\u5219\u5b9e\u9645\u8c03\u7528\u4e3a add(6,6) \uff0c\u7ed3\u679c\u4e3a 12\u3002\u9020\u6210\u8fd9\u4e2a\u95ee\u9898\u7684\u539f\u56e0\uff0c\u662f\u56e0\u4e3a\u51fd\u6570 add \u7684\u5176\u4e2d\u4e00\u4e2a\u53c2\u6570\u5177\u6709\u526f\u4f5c\u7528\u3002 \u9898\u5916\u8bdd C++ \u6807\u51c6\u6545\u610f\u6ca1\u6709\u5b9a\u4e49\u8fd9\u4e9b\u4e8b\u60c5\uff0c\u8fd9\u6837\u7f16\u8bd1\u5668\u53ef\u4ee5\u6839\u636e\u8ba1\u7b97\u673a\u7684\u4f53\u7cfb\u7ed3\u6784\u9009\u62e9\u6700\u81ea\u7136\uff08\u4e00\u822c\u4e5f\u662f\u6700\u9ad8\u6548\uff09\u7684\u65b9\u5f0f\u6765\u5904\u7406\u3002 C++ \u6807\u51c6\u6ca1\u6709\u89c4\u5b9a\u6c42\u503c\u987a\u5e8f\u7684\u573a\u666f\u8fd8\u6709\u4e00\u4e9b\uff08\u4f8b\u5982\u64cd\u4f5c\u6570\u7684\u6c42\u503c\u987a\u5e8f\uff09\uff0c\u6240\u4ee5\u4e0d\u540c\u7684\u7f16\u8bd1\u5668\u884c\u4e3a\u4e5f\u53ef\u80fd\u662f\u4e0d\u4e00\u6837\u7684\u3002\u5373\u4f7fC++\u6807\u51c6\u80fd\u591f\u660e\u786e\u8fd9\u4e9b\u95ee\u9898\uff0c\u7531\u4e8e\u5386\u53f2\u539f\u56e0\uff0c\u8fd9\u91cc\u4e5f\u662f\u7f16\u8bd1\u5668bug\u591a\u53d1\u4e4b\u5730\u3002\u901a\u5e38\uff0c\u8fd9\u4e9b\u95ee\u9898\u662f\u53ef\u4ee5\u907f\u514d\u7684\uff0c\u53ea\u8981\u4f60\u907f\u514d\u4e0d\u8981\u8ba9\u526f\u4f5c\u7528\u591a\u6b21\u4f5c\u7528\u5728\u4e00\u4e2a\u53d8\u91cf\u4e0a\u3002 \u6ce8\u610f C++ \u6ca1\u6709\u5b9a\u4e49\u51fd\u6570\u53c2\u6570\u6216\u8fd0\u7b97\u7b26\u64cd\u4f5c\u6570\u7684\u6c42\u503c\u987a\u5e8f\u3002 \u6ce8\u610f \u5728\u4e00\u4e2a\u8bed\u53e5\u4e2d\uff0c\u4e0d\u8981\u8ba9\u526f\u4f5c\u7528\u591a\u6b21\u4f5c\u7528\u5728\u4e00\u4e2a\u53d8\u91cf\u4e0a\u3002\u5982\u679c\u8fd9\u4e48\u505a\u7684\u8bdd\uff0c\u7ed3\u679c\u53ef\u80fd\u662f\u4e0d\u786e\u5b9a\u7684\u3002","title":"5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528"},{"location":"5-4-Increment-decrement-operators-and-side-effects/#_1","text":"\u5c06\u4e00\u4e2a\u53d8\u91cf\u9012\u589e\uff08\u52a01\uff09\u548c\u9012\u51cf\uff08\u51cf1\uff09\u90fd\u662f\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u56e0\u6b64\u5b83\u4eec\u6709\u4e13\u95e8\u7684\u8fd0\u7b97\u7b26\u3002 \u8fd0\u7b97\u7b26 \u7b26\u53f7 \u5f62\u5f0f \u64cd\u4f5c Prefix increment (pre-increment) ++ ++x Increment x, then return x Prefix decrement (pre-decrement) \u2013\u2013 \u2013\u2013x Decrement x, then return x Postfix increment (post-increment) ++ x++ Copy x, then increment x, then return the copy Postfix decrement (post-decrement) \u2013\u2013 x\u2013\u2013 Copy x, then decrement x, then return the copy \u6ce8\u610f\uff0c\u6bcf\u79cd\u64cd\u4f5c\u7b26\u90fd\u6709\u4e24\u4e2a\u7248\u672c\uff0c\u4e00\u4e2a\u662f\u524d\u7f00\u5f62\u5f0f\uff08\u8fd0\u7b97\u7b26\u5728\u64cd\u4f5c\u7b26\u524d\u9762\uff09\uff0c\u4e00\u4e2a\u662f\u540e\u7f00\u5f62\u5f0f\uff08\u8fd0\u7b97\u7b26\u5728\u64cd\u4f5c\u7b26\u540e\u9762\uff09\u3002 \u524d\u7f00\u9012\u589e/\u9012\u51cf\u64cd\u4f5c\u7b26\u975e\u5e38\u7b80\u5355\uff0c\u5373\u64cd\u4f5c\u6570\u5148\u9012\u589e\u6216\u9012\u51cf\uff0c\u7136\u540e\u8868\u8fbe\u5f0f\u518d\u6c42\u503c\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { int x { 5 }; int y = ++ x ; // x is incremented to 6, x is evaluated to the value 6, and 6 is assigned to y std :: cout << x << ' ' << y ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 6 6 \u540e\u7f00\u81ea\u589e/\u81ea\u51cf\u7a0d\u5fae\u6709\u70b9\u590d\u6742\u3002\u9996\u5148\uff0c\u5b83\u62f7\u8d1d\u4e86\u4e00\u4efd\u64cd\u4f5c\u6570\uff0c\u7136\u540e\u5bf9\u64cd\u4f5c\u6570\uff08\u800c\u4e0d\u662f\u5b83\u7684\u62f7\u8d1d\uff09\u8fdb\u884c\u9012\u589e\u6216\u9012\u51cf\u3002\u6700\u540e\u5bf9\u62f7\u8d1d\u7684\u503c\uff08\u800c\u975e\u539f\u59cb\u503c\uff09\u8fdb\u884c\u6c42\u503c\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { int x { 5 }; int y = x ++ ; // x \u81ea\u589e\u4e3a6\uff0c\u62f7\u8d1d\u7684\u6700\u521d\u7684\u503c\u4e3a5,5\u88ab\u8d4b\u503c\u7ed9\u4e86y std :: cout << x << ' ' << y ; return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 6 5 \u8ba9\u6211\u4eec\u4ecb\u7ecd\u4e00\u4e0b\u7b2c\u516d\u884c\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a x \u7684\u4e34\u65f6\u62f7\u8d1d\uff0c\u503c\u548cx\u4e00\u6837\uff085\uff09\u3002\u968f\u540e\uff0c\u8fd9\u4e2a\u62f7\u8d1d\u88ab\u8fd4\u56de\u7136\u540e\u8d4b\u503c\u7ed9 y \u3002\u7136\u540e\uff0c\u8fd9\u4e2a\u4e34\u65f6\u7684\u62f7\u8d1d\u5c31\u88ab\u4e22\u5f03\u4e86\u3002 \u56e0\u6b64\uff0c y \u6700\u7ec8\u7684\u503c\u5c31\u662f 5\uff08\u9012\u589e\u524d\u7684\u503c\uff09\uff0c\u7136\u540e x \u7684\u503c\u53d8\u6210\u4e86 6\uff08\u9012\u589e\u540e\u7684\u503c\uff09\u3002 \u6ce8\u610f\uff0c\u540e\u7f00\u7248\u672c\u7684\u8fd0\u7b97\u7b26\u591a\u4e86\u5f88\u591a\u6b65\u9aa4\uff0c\u56e0\u6b64\u5b83\u7684\u6027\u80fd\u4e0d\u5982\u524d\u7f00\u7248\u672c\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u6f14\u793a\u4e86\u524d\u7f00\u9012\u589e\u9012\u51cf\u548c\u540e\u7f00\u9012\u589e\u9012\u51cf\u7684\u4e0d\u540c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 5 }; int y { 5 }; std :: cout << x << ' ' << y << '\\n' ; std :: cout << ++ x << ' ' << -- y << '\\n' ; // prefix std :: cout << x << ' ' << y << '\\n' ; std :: cout << x ++ << ' ' << y -- << '\\n' ; // postfix std :: cout << x << ' ' << y << '\\n' ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 5 5 6 4 6 4 6 4 7 3 \u5728\u7b2c\u516b\u884c\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u524d\u7f00\u9012\u589e\u9012\u51cf\u3002\u56e0\u6b64\u8fd9\u4e00\u884c\u4e2d x \u548c y \u5148\u9012\u589e\uff0c\u518d\u6253\u5370\uff0c\u56e0\u6b64\u5b83\u4eec\u66f4\u65b0\u540e\u7684\u503c\u88ab\u6253\u5370\u4e86\u51fa\u6765\u3002 \u5728\u7b2c\u5341\u884c\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u7684\u662f\u540e\u7f00\u9012\u589e\u9012\u51cf\u3002\u56e0\u6b64\u8fd9\u4e00\u884c\u4e2d\uff0cx \u548c y \u5148\u88ab\u62f7\u8d1d\uff0c\u7136\u540e\u6253\u5370\u3002\u6240\u4ee5\u6211\u4eec\u6ca1\u6709\u770b\u5230\u9012\u589e\u540e\u7684\u7ed3\u679c\u88ab\u6253\u5370\u51fa\u6765\u3002\u9012\u589e\u7684\u7ed3\u679c\u53ea\u6709\u518d\u4e0b\u4e00\u6b21\u6253\u5370\u65f6\u624d\u80fd\u6709\u6240\u4f53\u73b0\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f3a\u70c8\u63a8\u8350\u4f7f\u7528\u524d\u7f00\u7248\u672c\u7684\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26\uff0c\u56e0\u4e3a\u5b83\u4eec\u6027\u80fd\u66f4\u597d\uff0c\u800c\u4e14\u4e5f\u4e0d\u5bb9\u6613\u5e26\u6765\u95ee\u9898\u3002","title":"\u53d8\u91cf\u7684\u81ea\u589e\u81ea\u51cf"},{"location":"5-4-Increment-decrement-operators-and-side-effects/#_2","text":"\u5982\u679c\u6211\u4eec\u8bf4\u4e00\u4e2a\u51fd\u6570\u6216\u8868\u8fbe\u5f0f\u5177\u6709\u526f\u4f5c\u7528\uff0c\u5219\u610f\u5473\u7740\u5b83\u6267\u884c\u540e\u5e26\u6765\u4e86\u67d0\u4e9b\u5f71\u54cd\u4e14\u8fd9\u4e9b\u5f71\u54cd\u5728\u51fd\u6570\u6216\u8868\u8fbe\u5f0f\u7684\u751f\u547d\u5468\u671f\u7ed3\u675f\u540e\u8fd8\u5b58\u5728\u3002 \u526f\u4f5c\u7528\u7684\u5178\u578b\u4f8b\u5b50\u5305\u62ec\u5bf9\u8c61\u7684\u503c\u88ab\u6539\u53d8\u3001\u8fdb\u884c\u4e86\u8f93\u5165\u8f93\u51fa\u6216\u8005\u66f4\u65b0\u4e86\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08\u4f8b\u5982\u6253\u5f00\u6216\u5173\u95ed\u67d0\u4e2a\u6309\u94ae\uff09\u3002 \u5f88\u591a\u65f6\u5019\uff0c\u526f\u4f5c\u7528\u662f\u6709\u7528\u7684\uff1a 1 2 3 x = 5 ; // the assignment operator modifies the state of x ++ x ; // operator++ modifies the state of x std :: cout << x ; // operator<< modifies the state of the console \u4e0a\u9762\u8d4b\u503c\u53f7\u7684\u526f\u4f5c\u7528\u662f\u6c38\u4e45\u5730\u6539\u53d8\u4e86 x \u7684\u503c\uff0c\u5373\u4f7f\u5728\u8d4b\u503c\u8bed\u53e5\u7ed3\u675f\u6267\u884c\u540e\uff0c x \u7684\u503c\u4e5f\u8fd8\u662f 5\u3002\u540c\u6837\u7684\uff0c ++ \u8fd0\u7b97\u7b26\u4e5f\u6709\u526f\u4f5c\u7528\uff0c ++x \u6267\u884c\u540e x \u7684\u503c\u5c31\u9012\u589e\u4e86\u3002\u6253\u5370 x \u540c\u6837\u6709\u526f\u4f5c\u7528\uff0c\u5b83\u6539\u53d8\u4e86\u63a7\u5236\u53f0\u7684\u72b6\u6001\uff0c\u56e0\u4e3a\u4f60\u53ef\u4ee5\u770b\u5230 x \u7684\u503c\u88ab\u6253\u5370\u5230\u4e86\u63a7\u5236\u53f0\u3002 \u4e0d\u8fc7\uff0c\u526f\u4f5c\u7528\u6709\u65f6\u5019\u4e5f\u4f1a\u5e26\u6765\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int add ( int x , int y ) { return x + y ; } int main () { int x { 5 }; int value { add ( x , ++ x ) }; // \u7a76\u7adf\u662f 5 + 6 \u8fd8\u662f 6 + 6? // \u8fd9\u53d6\u51b3\u4e8e\u4f60\u7684\u7f16\u8bd1\u5668\u5982\u4f55\u9009\u62e9\u51fd\u6570\u53c2\u6570\u7684\u6c42\u503c\u987a\u5e8f std :: cout << value << '\\n' ; // value \u53ef\u80fd\u662f 11 \u6216\u8005 12\uff0c\u53d6\u51b3\u4e8e\u7f16\u8bd1\u5668 return 0 ; } \u56e0\u4e3a C++ \u6807\u51c6\u6ca1\u6709\u89c4\u5b9a\u51fd\u6570\u53c2\u6570\u7684\u6c42\u503c\u987a\u5e8f\u3002\u5982\u679c\u5de6\u8fb9\u7684\u53c2\u6570\u5148\u6c42\u503c\uff0c\u5219\u5b9e\u9645\u8c03\u7528\u4e3a add(5,6) \uff0c\u7ed3\u679c\u4e3a 11\u3002\u5982\u679c\u53f3\u8fb9\u7684\u53c2\u6570\u5148\u6c42\u503c\uff0c\u5219\u5b9e\u9645\u8c03\u7528\u4e3a add(6,6) \uff0c\u7ed3\u679c\u4e3a 12\u3002\u9020\u6210\u8fd9\u4e2a\u95ee\u9898\u7684\u539f\u56e0\uff0c\u662f\u56e0\u4e3a\u51fd\u6570 add \u7684\u5176\u4e2d\u4e00\u4e2a\u53c2\u6570\u5177\u6709\u526f\u4f5c\u7528\u3002 \u9898\u5916\u8bdd C++ \u6807\u51c6\u6545\u610f\u6ca1\u6709\u5b9a\u4e49\u8fd9\u4e9b\u4e8b\u60c5\uff0c\u8fd9\u6837\u7f16\u8bd1\u5668\u53ef\u4ee5\u6839\u636e\u8ba1\u7b97\u673a\u7684\u4f53\u7cfb\u7ed3\u6784\u9009\u62e9\u6700\u81ea\u7136\uff08\u4e00\u822c\u4e5f\u662f\u6700\u9ad8\u6548\uff09\u7684\u65b9\u5f0f\u6765\u5904\u7406\u3002 C++ \u6807\u51c6\u6ca1\u6709\u89c4\u5b9a\u6c42\u503c\u987a\u5e8f\u7684\u573a\u666f\u8fd8\u6709\u4e00\u4e9b\uff08\u4f8b\u5982\u64cd\u4f5c\u6570\u7684\u6c42\u503c\u987a\u5e8f\uff09\uff0c\u6240\u4ee5\u4e0d\u540c\u7684\u7f16\u8bd1\u5668\u884c\u4e3a\u4e5f\u53ef\u80fd\u662f\u4e0d\u4e00\u6837\u7684\u3002\u5373\u4f7fC++\u6807\u51c6\u80fd\u591f\u660e\u786e\u8fd9\u4e9b\u95ee\u9898\uff0c\u7531\u4e8e\u5386\u53f2\u539f\u56e0\uff0c\u8fd9\u91cc\u4e5f\u662f\u7f16\u8bd1\u5668bug\u591a\u53d1\u4e4b\u5730\u3002\u901a\u5e38\uff0c\u8fd9\u4e9b\u95ee\u9898\u662f\u53ef\u4ee5\u907f\u514d\u7684\uff0c\u53ea\u8981\u4f60\u907f\u514d\u4e0d\u8981\u8ba9\u526f\u4f5c\u7528\u591a\u6b21\u4f5c\u7528\u5728\u4e00\u4e2a\u53d8\u91cf\u4e0a\u3002 \u6ce8\u610f C++ \u6ca1\u6709\u5b9a\u4e49\u51fd\u6570\u53c2\u6570\u6216\u8fd0\u7b97\u7b26\u64cd\u4f5c\u6570\u7684\u6c42\u503c\u987a\u5e8f\u3002 \u6ce8\u610f \u5728\u4e00\u4e2a\u8bed\u53e5\u4e2d\uff0c\u4e0d\u8981\u8ba9\u526f\u4f5c\u7528\u591a\u6b21\u4f5c\u7528\u5728\u4e00\u4e2a\u53d8\u91cf\u4e0a\u3002\u5982\u679c\u8fd9\u4e48\u505a\u7684\u8bdd\uff0c\u7ed3\u679c\u53ef\u80fd\u662f\u4e0d\u786e\u5b9a\u7684\u3002","title":"\u526f\u4f5c\u7528"},{"location":"5-5-Comma-and-conditional-operators/","tags":["operator"],"text":"Key Takeaway \u9017\u53f7\u8868\u8fbe\u5f0f\u9996\u5148\u5bf9\u5b83\u5de6\u4fa7\u7684\u64cd\u4f5c\u6570\u6c42\u503c\uff0c\u7136\u540e\u5bf9\u53f3\u4fa7\u64cd\u4f5c\u6570\u6c42\u503c\uff0c\u7136\u540e\u8fd4\u56de\u53f3\u4fa7\u64cd\u4f5c\u6570\u7684\u7ed3\u679c\u3002 \u907f\u514d\u4f7f\u7528\u9017\u53f7\u8fd0\u7b97\u7b26\uff0c\u9664\u975e\u662f\u5728 for \u5faa\u73af\u4e2d\u3002 \u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u6574\u4e2a\u8868\u8fbe\u5f0f\u90fd\u8981\u653e\u5728\u62ec\u53f7\u4e2d\uff0c\u6b64\u5916\uff0c\u6761\u4ef6\u90e8\u5206\u4e5f\u8981\u653e\u5728\u62ec\u53f7\u4e2d\u3002 \u6761\u4ef6\u8fd0\u7b97\u7b26\u53ef\u4ee5\u4f5c\u4e3a\u8868\u8fbe\u5f0f\u6c42\u503c\uff0c\u8fd9\u6837\u4fbf\u53ef\u4ee5\u7528\u4e8e\u521d\u59cb\u5316\u3001\u8d4b\u503c\u548c\u4f20\u53c2 if/else \u8bed\u53e5\u5757\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u5728 if/else \u7ed3\u675f\u540e\u5c31\u9500\u6bc1\u4e86 \u6761\u4ef6\u8bed\u53e5\u4e2d\u7684\u4e24\u4e2a\u8868\u8fbe\u5f0f\u7c7b\u578b\u9700\u8981\u5339\u914d\u6216\u8005\u53ef\u4ee5\u8f6c\u53d8\u4e3a\u5171\u540c\u7684\u7c7b\u578b\uff0c\u5426\u5219\u65e0\u6cd5\u7f16\u8bd1\uff0c\u5fc5\u987b\u662f if/else \u5b8c\u6210\u3002 \u4ec5\u5728\u9762\u5bf9\u7b80\u5355\u6761\u4ef6\u4e14\u4f60\u4f7f\u7528\u5176\u7ed3\u679c\uff0c\u540c\u65f6\u80fd\u591f\u589e\u5f3a\u53ef\u8bfb\u6027\u7684\u6761\u4ef6\u4e0b\uff0c\u4f7f\u7528\u6761\u4ef6\u8fd0\u7b97\u7b26 \u9017\u53f7\u8fd0\u7b97\u7b26 \u8fd0\u7b97\u7b26 \u7b26\u53f7 \u5f62\u5f0f \u64cd\u4f5c \u9017\u53f7\u8fd0\u7b97\u7b26 , x, y \u5148\u5bf9 x \u6c42\u503c\uff0c\u518d\u5bf9 y \u6c42\u503c\uff0c\u7136\u540e\u8fd4\u56de y \u6c42\u503c\u7ed3\u679c \u9017\u53f7\u8fd0\u7b97\u7b26 ( , ) \u5141\u8bb8\u4f60\u5728\u5141\u8bb8\u5355\u72ec\u8868\u8fbe\u5f0f\u7684\u5730\u65b9\uff0c\u5bf9\u591a\u4e2a\u8868\u8fbe\u5f0f\u6c42\u503c\u3002\u9017\u53f7\u8868\u8fbe\u5f0f\u9996\u5148\u5bf9\u5b83\u5de6\u4fa7\u7684\u64cd\u4f5c\u6570\u6c42\u503c\uff0c\u7136\u540e\u5bf9\u53f3\u4fa7\u64cd\u4f5c\u6570\u6c42\u503c\uff0c\u7136\u540e\u8fd4\u56de\u53f3\u4fa7\u64cd\u4f5c\u6570\u7684\u7ed3\u679c\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { int x { 1 }; int y { 2 }; std :: cout << ( ++ x , ++ y ); // \u5bf9 x \u548c y \u9012\u589e\uff0c \u5bf9\u53f3\u4fa7\u64cd\u4f5c\u6570\u6c42\u503c return 0 ; } \u9996\u5148\uff0c\u9017\u53f7\u8fd0\u7b97\u7b26\u5de6\u4fa7\u64cd\u4f5c\u6570\u6c42\u503c\uff0c\u5c06 x \u9012\u589e\u4e3a 2\u3002\u7136\u540e\u9017\u53f7\u8fd0\u7b97\u7b26\u53f3\u4fa7\u64cd\u4f5c\u6570\u6c42\u503c\uff0c\u5c06 y \u9012\u589e\u4e3a 3\u3002\u7136\u540e\u9017\u53f7\u8868\u8fbe\u5f0f\u8fd4\u56de\u7684\u662f\u53f3\u4fa7\u6c42\u503c\u7684\u7ed3\u679c\uff083\uff09\uff0c\u5e76\u6253\u5370\u5728\u5c4f\u5e55\u4e0a\u3002 \u6ce8\u610f\uff0c\u9017\u53f7\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u662f\u6700\u4f4e\u7684\uff0c\u751a\u81f3\u6bd4\u8d4b\u503c\u8868\u8fbe\u5f0f\u8fd8\u4f4e\u3002\u56e0\u6b64\uff0c\u4e0b\u9762\u4e24\u884c\u4ee3\u7801\u7684\u6548\u679c\u662f\u4e0d\u540c\u7684\u3002 1 2 z = ( a , b ); // \u5148\u5bf9 (a, b) \u6c42\u503c\u5f97\u5230 b \u7684\u7ed3\u679c\uff0c\u7136\u540e\u8d4b\u503c\u7ed9 z z = a , b ; // \u7b49\u4ef7\u4e8e \"(z = a), b\"\uff0c\u6240\u4ee5 z \u9996\u5148\u88ab\u8d4b\u503c\u4e3a a\uff0c\u7136\u540e b \u6c42\u503c\uff0c\u7ed3\u679c\u4e22\u5f03\u3002 \u8fd9\u4e5f\u4f7f\u5f97\u9017\u53f7\u8fd0\u7b97\u7b26\u7528\u8d77\u6765\u5b58\u5728\u5371\u9669\u3002 \u51e0\u4e4e\u4efb\u4f55\u4f7f\u7528\u9017\u53f7\u8fd0\u7b97\u7b26\u7f16\u5199\u7684\u8bed\u53e5\u90fd\u53ef\u4ee5\u7528\u591a\u4e2a\u5355\u72ec\u7684\u8bed\u53e5\u6765\u66ff\u6362\u3002\u4f8b\u5982\uff0c\u4e0a\u8ff0\u4ee3\u7801\u53ef\u4ee5\u6539\u5199\u6210\u4e0b\u9762\u7684\u5f62\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int x { 1 }; int y { 2 }; ++ x ; std :: cout << ++ y ; return 0 ; } \u5927\u591a\u6570\u7a0b\u5e8f\u5458\u51e0\u4e4e\u4ece\u6765\u4e0d\u4f1a\u4f7f\u7528\u9017\u53f7\u8fd0\u7b97\u7b26\uff0c\u53ea\u6709\u5728 for \u5faa\u73af\u4e2d\u5b58\u5728\u4f8b\u5916\uff0c\u6211\u4eec\u4f1a\u5728 7.9 - for \u8bed\u53e5 \u4e2d\u8be6\u7ec6\u8ba8\u8bba\uff1a \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u4f7f\u7528\u9017\u53f7\u8fd0\u7b97\u7b26\uff0c\u9664\u975e\u662f\u5728 for \u5faa\u73af\u4e2d\u3002 \u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26 \u5728 C++\uff0c\u9017\u53f7\u5e38\u7528\u4f5c\u5206\u9694\u7b26\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u548c\u9017\u53f7\u8fd0\u7b97\u7b26\u5e76\u6ca1\u6709\u5173\u7cfb\uff0c\u4e5f\u4e0d\u5177\u5907\u9017\u53f7\u8fd0\u7b97\u7b26\u7684\u529f\u80fd\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 void foo ( int x , int y ) // \u5728\u51fd\u6570\u4e2d\u9017\u53f7\u7528\u4e8e\u5206\u5272\u53c2\u6570 { add ( x , y ); // Comma used to separate arguments in function call constexpr int z { 3 }, w { 5 }; // \u9017\u53f7\u7528\u4e8e\u5206\u5272\u540c\u4e00\u884c\u4e2d\u5b9a\u4e49\u7684\u591a\u4e2a\u53d8\u91cf\uff08\u4e0d\u8981\u8fd9\u4e48\u505a\uff09 } \u4e0d\u9700\u8981\u907f\u514d\u4f7f\u7528\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\uff08\u9664\u975e\u662f\u58f0\u660e\u591a\u4e2a\u53d8\u91cf\u65f6\uff0c\u4e0d\u8981\u8fd9\u4e48\u505a\uff09\u3002 \u6761\u4ef6\u8fd0\u7b97\u7b26 \u8fd0\u7b97\u7b26 \u7b26\u53f7 \u5f62\u5f0f \u64cd\u4f5c \u6761\u4ef6\u8fd0\u7b97\u7b26 ?: c ? x : y \u5982\u679c c \u975e\u96f6\uff08\u771f\uff09\u5219\u5bf9 x \u5176\u4e2d\uff0c\u5426\u5219\u5bf9 y \u6c42\u503c \u6761\u4ef6\u8fd0\u7b97\u7b26 ( ?: ) (\u6709\u65f6\u4e5f\u79f0\u4e3a \u201d\u7b97\u6570 if \u201c\u8fd0\u7b97\u7b26) \u662f\u4e00\u4e2a\u4e09\u5143\u8fd0\u7b97\u7b26\uff08\u6709\u4e09\u4e2a\u64cd\u4f5c\u6570\uff09\u3002\u56e0\u4e3a\u5b83\u662f C++ \u4e2d\u552f\u4e00\u7684\u4e09\u5143\u8fd0\u7b97\u7b26\uff0c\u56e0\u6b64\u6709\u4f7f\u7528\u4e5f\u79f0\u5b83\u4e3a\u201d\u90a3\u4e2a\u4e09\u5143\u8fd0\u7b97\u7b26\u201c\u3002 ?: \u8fd0\u7b97\u7b26\u63d0\u4f9b\u4e86\u4e00\u79cd\u5feb\u6377\u7684\u6761\u4ef6\u8bed\u53e5\u529f\u80fd\u3002\u5982\u679c\u4f60\u5bf9\u6761\u4ef6\u8bed\u53e5\u8fd8\u6709\u7591\u95ee\uff0c\u8bf7\u53c2\u8003 4.10 - if \u8bed\u53e5\u7b80\u4ecb \u3002 if/else \u8bed\u53e5\u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 2 3 4 if ( condition ) statement1 ; else statement2 ; \u5982\u679c condition \u6c42\u503c\u7ed3\u679c\u4e3a true \uff0c\u5219 statement1 \u4f1a\u88ab\u6267\u884c\uff0c\u5426\u5219\u6267\u884c statement2 \u3002 ?: \u8fd0\u7b97\u7b26\u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 (condition) ? expression1 : expression2; \u5982\u679c condition \u6c42\u503c\u7ed3\u679c\u4e3a true \uff0c\u5219 statement1 \u4f1a\u88ab\u6267\u884c\uff0c\u5426\u5219\u6267\u884c statement2 \u3002\u6ce8\u610f\uff0c\u8fd9\u91cc\u7684 expression2 \u662f\u5fc5\u987b\u8981\u6709\u7684\u3002 \u4e0b\u9762\u8fd9\u4e2a if/else \u8bed\u53e5\uff1a 1 2 3 4 if ( x > y ) larger = x ; else larger = y ; \u53ef\u4ee5\u6539\u5199\u6210\u5982\u4e0b\u5f62\u5f0f\uff1a 1 larger = ( x > y ) ? x : y ; \u8fd9\u6837\u53ef\u4ee5\u5728\u4e0d\u727a\u7272\u53ef\u8bfb\u6027\u7684\u524d\u63d0\u4e0b\uff0c\u8ba9\u4ee3\u7801\u53d8\u5f97\u66f4\u52a0\u7d27\u51d1\u3002 \u4e3a\u6761\u4ef6\u8fd0\u7b97\u7b26\u6dfb\u52a0\u62ec\u53f7 \u6839\u636e\u60ef\u4f8b\uff0c\u6211\u4eec\u4e00\u822c\u4f1a\u628a\u6761\u4ef6\u8fd0\u7b97\u7b26\u7684 \u6761\u4ef6 \u90e8\u5206\u653e\u5728\u62ec\u53f7\u91cc\uff0c\u8fd9\u6837\u4e0d\u4ec5\u53ef\u8bfb\u6027\u66f4\u597d\uff0c\u800c\u4e14\u53ef\u4ee5\u786e\u4fdd\u8fd0\u7b97\u4f18\u5148\u7ea7\u662f\u6b63\u786e\u7684\u3002\u5176\u4ed6\u64cd\u4f5c\u6570\u7684\u6c42\u503c\u4f18\u5148\u7ea7\uff0c\u52a0\u4e0d\u52a0\u62ec\u53f7\u90fd\u662f\u4e00\u6837\u7684\uff0c\u6240\u4ee5\u5c31\u6ca1\u5fc5\u8981\u663e\u793a\u5730\u6dfb\u52a0\u62ec\u53f7\u4e86\u3002 \u6ce8\u610f\uff0c ?: \u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u975e\u5e38\u4f4e\u3002\u9664\u975e\u662f\u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u91cc\uff0c\u4e0d\u7136\u6574\u4e2a\u6761\u4ef6\u8fd0\u7b97\u7b26\u8868\u8fbe\u5f0f\u4e5f\u8981\u653e\u5728\u62ec\u53f7\u4e2d\u3002 \u4f8b\u5982\uff0c\u4e3a\u4e86\u6253\u5370 x \u548c y \u4e2d\u8f83\u5927\u7684\u503c\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 2 3 4 if ( x > y ) std :: cout << x ; else std :: cout << y ; \u6216\u8005\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8fd0\u7b97\u7b26\uff1a 1 std :: cout << (( x > y ) ? x : y ); \u8ba9\u6211\u4eec\u6d4b\u8bd5\u4e00\u4e0b\uff0c\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c\u5982\u679c\u4e0d\u628a\u6761\u4ef6\u8fd0\u7b97\u7b26\u8868\u8fbe\u5f0f\u653e\u5728\u4e00\u4e2a\u62ec\u53f7\u4e2d\u4f1a\u600e\u6837\u3002 \u56e0 << operator \u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e ?: \u8fd0\u7b97\u7b26\uff0c\u56e0\u6b64\u4e0b\u9762\u8bed\u53e5\uff1a 1 std :: cout << ( x > y ) ? x : y ; \u7b49\u4ef7\u4e8e\uff1a 1 ( std :: cout << ( x > y )) ? x : y ; \u56e0\u6b64\uff0c\u5982\u679c x>y \u5219\u7ed3\u679c\u4f1a\u6253\u5370 1(true)\uff0c\u5426\u5219\u6253\u53700\u3002 \u6700\u4f73\u5b9e\u8df5 \u603b\u662f\u628a\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u6761\u4ef6\u90e8\u5206\u653e\u5728\u62ec\u53f7\u91cc\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u8003\u8651\u628a\u6574\u4e2a\u90e8\u5206\u90fd\u653e\u5728\u62ec\u53f7\u91cc\u3002 \u6761\u4ef6\u8fd0\u7b97\u7b26\u4f5c\u4e3a\u8868\u8fbe\u5f0f\u6c42\u503c \u56e0\u4e3a\u6761\u4ef6\u8fd0\u7b97\u7b26\u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u8bed\u53e5\uff0c\u6240\u4ee5\u6761\u4ef6\u8fd0\u7b97\u7b26\u53ef\u4ee5\u88ab\u7528\u5728\u65e0\u6cd5\u4f7f\u7528 if/else \u7684\u5730\u65b9\u3002 \u4f8b\u5982\uff0c\u53ef\u4ee5\u7528\u5b83\u6765\u521d\u59cb\u5316\u4e00\u4e2a\u5e38\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { constexpr bool inBigClassroom { false }; constexpr int classSize { inBigClassroom ? 30 : 20 }; std :: cout << \"The class size is: \" << classSize << '\\n' ; return 0 ; } \u4f7f\u7528 if/else \u5e76\u4e0d\u80fd\u7528\u4e8e\u8fd9\u79cd\u5e38\u89c1\uff0c\u4f60\u53ef\u80fd\u4f1a\u89c9\u5f97\u4e0b\u9762\u8fd9\u79cd\u65b9\u6cd5\u662f\u53ef\u884c\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { constexpr bool inBigClassroom { false }; if ( inBigClassroom ) constexpr int classSize { 30 }; else constexpr int classSize { 20 }; std :: cout << \"The class size is: \" << classSize ; return 0 ; } \u5b9e\u9645\u4e0a\uff0c\u4e0a\u8ff0\u4ee3\u7801\u662f\u4e0d\u80fd\u7f16\u8bd1\u7684\uff0c\u4f60\u4f1a\u88ab\u544a\u77e5 claseSize \u6ca1\u6709\u5b9a\u4e49\u3002\u6b63\u5982\u5b9a\u4e49\u5728\u51fd\u6570\u4e2d\u7684\u53d8\u91cf\u4f1a\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u9500\u6bc1\u4e00\u6837\uff0c\u5b9a\u4e49\u5728 if/else \u8bed\u53e5\u5757\u4e2d\u7684\u53d8\u91cf\u4e5f\u4f1a\u5728 if/else \u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u6253\u5370 classSize \u65f6\uff0c\u8be5\u53d8\u91cf\u5b9e\u9645\u4e0a\u5df2\u7ecf\u88ab\u9500\u6bc1\u4e86\u3002 \u8bd1\u8005\u6ce8 \u5982\u679c\u4f60\u6ca1\u6709\u4e00\u773c\u770b\u51fa\u4e0a\u9762\u7684\u95ee\u9898\uff0c\u53ef\u80fd\u548c\u5b83\u7684\u8868\u73b0\u5f62\u5f0f\u6709\u5173\uff0c\u6211\u4eec\u90fd\u77e5\u9053\uff0c\u901a\u8fc7\u5927\u62ec\u53f7\u53ef\u4ee5\u9650\u5236\u53d8\u91cf\u7684\u4f5c\u7528\u57df\uff0c\u5982\u679c\u4fee\u6539\u4e3a\u4e0b\u9762\u8fd9\u6837\u7684\u5f62\u5f0f\uff0c\u4f60\u5e94\u8be5\u5c31\u80fd\u591f\u770b\u51fa\u4e86\uff1a 1 2 3 4 5 6 7 8 if ( inBigClassroom ) { constexpr int classSize { 30 }; } else { constexpr int classSize { 20 }; } \u53c2\u8003 6.1 - \u590d\u5408\u8bed\u53e5\uff08\u8bed\u53e5\u5757\uff09 \u548c 6.3 - \u5c40\u90e8\u53d8\u91cf \u56e0\u6b64\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u4f7f\u7528 if/else \u5b8c\u6210\u548c\u6761\u4ef6\u8868\u8fbe\u5f0f\u4e00\u6837\u7684\u6548\u679c\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u8fd9\u6837\u505a\uff1b 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int getClassSize ( bool inBigClassroom ) { if ( inBigClassroom ) return 30 ; return 20 ; } int main () { const int classSize { getClassSize ( false ) }; std :: cout << \"The class size is: \" << classSize ; return 0 ; } \u8fd9\u79cd\u65b9\u6cd5\u662f\u53ef\u884c\u7684\u56e0\u4e3a\u5b83\u6ca1\u6709\u5728 if/else \u4e2d\u5b9a\u4e49\u53d8\u91cf\uff0c\u6211\u4eec\u662f\u628a\u503c\u8fd4\u56de\u7ed9\u4e86\u4e3b\u8c03\u51fd\u6570\uff0c\u7136\u540e\u8be5\u503c\u88ab\u7528\u4e8e\u521d\u59cb\u5316\u3002 \u4f46\u8fd9\u4e48\u505a\u663e\u7136\u9700\u8981\u5f88\u591a\u989d\u5916\u7684\u6b65\u9aa4\u3002 \u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u5fc5\u987b\u5339\u914d\u6216\u8005\u53ef\u8f6c\u6362\u4e3a\u5339\u914d\u7684\u7c7b\u578b \u4e3a\u4e86\u7b26\u5408 C++ \u7c7b\u578b\u68c0\u67e5\u7684\u8981\u6c42\uff0c\u6761\u4ef6\u8bed\u53e5\u4e2d\u7684\u4e24\u4e2a\u8868\u8fbe\u5f0f\u7c7b\u578b\u9700\u8981\u5339\u914d\u6216\u8005\u53ef\u4ee5\u8f6c\u53d8\u4e3a\u5171\u540c\u7684\u7c7b\u578b\u3002 \u6269\u5c55\u9605\u8bfb \u5f53\u7c7b\u578b\u4e0d\u5339\u914d\u65f6\uff0c\u4f7f\u7528\u7684\u8f6c\u6362\u89c4\u5219\u975e\u5e38\u590d\u6742\uff0c\u4f60\u53ef\u4ee5\u5728 \u8fd9\u91cc \u627e\u5230\u8be6\u7ec6\u4fe1\u606f \u4e5f\u8bb8\u4f60\u89c9\u5f97\u4e0b\u9762\u8fd9\u6837\u505a\u662f\u53ef\u884c\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { constexpr int x { 5 }; std :: cout << ( x != 5 ? x : \"x is 5\" ); // \u4e0d\u80fd\u7f16\u8bd1 return 0 ; } \u5176\u5b9e\u4e0a\u9762\u7684\u4ee3\u7801\u5e76\u4e0d\u80fd\u7f16\u8bd1\u3002\u56e0\u4e3a\u5176\u4e2d\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7c7b\u578b\u662f\u6574\u578b\uff0c\u800c\u53e6\u5916\u4e00\u4e2a\u662f\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u3002\u7f16\u8bd1\u5668\u4e0d\u80fd\u591f\u786e\u5b9a\u8fd9\u4e9b\u7c7b\u578b\u516c\u5171\u7684\u7c7b\u578b\u662f\u4ec0\u4e48\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f60\u5fc5\u987b\u4f7f\u7528 if/else \u8bed\u53e5\u3002 \u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u4f7f\u7528\u6761\u4ef6\u64cd\u4f5c\u7b26\uff1f \u6761\u4ef6\u8868\u8fbe\u5f0f\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u52a0\u7cbe\u7b80\u7684\u7c7b\u4f3c if/else \u8bed\u53e5\u7684\u529f\u80fd\u3002\u5b83\u5728\u9700\u8981\u8fdb\u884c\u6761\u4ef6\u521d\u59cb\u5316\uff08\u6216\u8d4b\u503c\uff09\u6216\u4f20\u53c2\u65f6\u5c24\u4e3a\u6709\u6548\u3002 \u8bf7\u4e0d\u8981\u5c1d\u8bd5\u5229\u7528\u5b83\u6765\u4ee3\u66ff\u590d\u6742\u7684 if/else \u8bed\u53e5\uff0c\u5426\u5219\u5f88\u5feb\u4f1a\u53d8\u5f97\u590d\u6742\u7684\u96be\u4ee5\u7ef4\u62a4\u3002 \u6700\u4f73\u5b9e\u8df5 \u4ec5\u5728\u9762\u5bf9\u7b80\u5355\u6761\u4ef6\u4e14\u4f60\u4f7f\u7528\u5176\u7ed3\u679c\uff0c\u540c\u65f6\u80fd\u591f\u589e\u5f3a\u53ef\u8bfb\u6027\u7684\u6761\u4ef6\u4e0b\uff0c\u4f7f\u7528\u6761\u4ef6\u8fd0\u7b97\u7b26","title":"5.5 - \u9017\u53f7\u548c\u6761\u4ef6\u8fd0\u7b97\u7b26"},{"location":"5-5-Comma-and-conditional-operators/#_1","text":"\u8fd0\u7b97\u7b26 \u7b26\u53f7 \u5f62\u5f0f \u64cd\u4f5c \u9017\u53f7\u8fd0\u7b97\u7b26 , x, y \u5148\u5bf9 x \u6c42\u503c\uff0c\u518d\u5bf9 y \u6c42\u503c\uff0c\u7136\u540e\u8fd4\u56de y \u6c42\u503c\u7ed3\u679c \u9017\u53f7\u8fd0\u7b97\u7b26 ( , ) \u5141\u8bb8\u4f60\u5728\u5141\u8bb8\u5355\u72ec\u8868\u8fbe\u5f0f\u7684\u5730\u65b9\uff0c\u5bf9\u591a\u4e2a\u8868\u8fbe\u5f0f\u6c42\u503c\u3002\u9017\u53f7\u8868\u8fbe\u5f0f\u9996\u5148\u5bf9\u5b83\u5de6\u4fa7\u7684\u64cd\u4f5c\u6570\u6c42\u503c\uff0c\u7136\u540e\u5bf9\u53f3\u4fa7\u64cd\u4f5c\u6570\u6c42\u503c\uff0c\u7136\u540e\u8fd4\u56de\u53f3\u4fa7\u64cd\u4f5c\u6570\u7684\u7ed3\u679c\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { int x { 1 }; int y { 2 }; std :: cout << ( ++ x , ++ y ); // \u5bf9 x \u548c y \u9012\u589e\uff0c \u5bf9\u53f3\u4fa7\u64cd\u4f5c\u6570\u6c42\u503c return 0 ; } \u9996\u5148\uff0c\u9017\u53f7\u8fd0\u7b97\u7b26\u5de6\u4fa7\u64cd\u4f5c\u6570\u6c42\u503c\uff0c\u5c06 x \u9012\u589e\u4e3a 2\u3002\u7136\u540e\u9017\u53f7\u8fd0\u7b97\u7b26\u53f3\u4fa7\u64cd\u4f5c\u6570\u6c42\u503c\uff0c\u5c06 y \u9012\u589e\u4e3a 3\u3002\u7136\u540e\u9017\u53f7\u8868\u8fbe\u5f0f\u8fd4\u56de\u7684\u662f\u53f3\u4fa7\u6c42\u503c\u7684\u7ed3\u679c\uff083\uff09\uff0c\u5e76\u6253\u5370\u5728\u5c4f\u5e55\u4e0a\u3002 \u6ce8\u610f\uff0c\u9017\u53f7\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u662f\u6700\u4f4e\u7684\uff0c\u751a\u81f3\u6bd4\u8d4b\u503c\u8868\u8fbe\u5f0f\u8fd8\u4f4e\u3002\u56e0\u6b64\uff0c\u4e0b\u9762\u4e24\u884c\u4ee3\u7801\u7684\u6548\u679c\u662f\u4e0d\u540c\u7684\u3002 1 2 z = ( a , b ); // \u5148\u5bf9 (a, b) \u6c42\u503c\u5f97\u5230 b \u7684\u7ed3\u679c\uff0c\u7136\u540e\u8d4b\u503c\u7ed9 z z = a , b ; // \u7b49\u4ef7\u4e8e \"(z = a), b\"\uff0c\u6240\u4ee5 z \u9996\u5148\u88ab\u8d4b\u503c\u4e3a a\uff0c\u7136\u540e b \u6c42\u503c\uff0c\u7ed3\u679c\u4e22\u5f03\u3002 \u8fd9\u4e5f\u4f7f\u5f97\u9017\u53f7\u8fd0\u7b97\u7b26\u7528\u8d77\u6765\u5b58\u5728\u5371\u9669\u3002 \u51e0\u4e4e\u4efb\u4f55\u4f7f\u7528\u9017\u53f7\u8fd0\u7b97\u7b26\u7f16\u5199\u7684\u8bed\u53e5\u90fd\u53ef\u4ee5\u7528\u591a\u4e2a\u5355\u72ec\u7684\u8bed\u53e5\u6765\u66ff\u6362\u3002\u4f8b\u5982\uff0c\u4e0a\u8ff0\u4ee3\u7801\u53ef\u4ee5\u6539\u5199\u6210\u4e0b\u9762\u7684\u5f62\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int x { 1 }; int y { 2 }; ++ x ; std :: cout << ++ y ; return 0 ; } \u5927\u591a\u6570\u7a0b\u5e8f\u5458\u51e0\u4e4e\u4ece\u6765\u4e0d\u4f1a\u4f7f\u7528\u9017\u53f7\u8fd0\u7b97\u7b26\uff0c\u53ea\u6709\u5728 for \u5faa\u73af\u4e2d\u5b58\u5728\u4f8b\u5916\uff0c\u6211\u4eec\u4f1a\u5728 7.9 - for \u8bed\u53e5 \u4e2d\u8be6\u7ec6\u8ba8\u8bba\uff1a \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u4f7f\u7528\u9017\u53f7\u8fd0\u7b97\u7b26\uff0c\u9664\u975e\u662f\u5728 for \u5faa\u73af\u4e2d\u3002","title":"\u9017\u53f7\u8fd0\u7b97\u7b26"},{"location":"5-5-Comma-and-conditional-operators/#_2","text":"\u5728 C++\uff0c\u9017\u53f7\u5e38\u7528\u4f5c\u5206\u9694\u7b26\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u548c\u9017\u53f7\u8fd0\u7b97\u7b26\u5e76\u6ca1\u6709\u5173\u7cfb\uff0c\u4e5f\u4e0d\u5177\u5907\u9017\u53f7\u8fd0\u7b97\u7b26\u7684\u529f\u80fd\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 void foo ( int x , int y ) // \u5728\u51fd\u6570\u4e2d\u9017\u53f7\u7528\u4e8e\u5206\u5272\u53c2\u6570 { add ( x , y ); // Comma used to separate arguments in function call constexpr int z { 3 }, w { 5 }; // \u9017\u53f7\u7528\u4e8e\u5206\u5272\u540c\u4e00\u884c\u4e2d\u5b9a\u4e49\u7684\u591a\u4e2a\u53d8\u91cf\uff08\u4e0d\u8981\u8fd9\u4e48\u505a\uff09 } \u4e0d\u9700\u8981\u907f\u514d\u4f7f\u7528\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\uff08\u9664\u975e\u662f\u58f0\u660e\u591a\u4e2a\u53d8\u91cf\u65f6\uff0c\u4e0d\u8981\u8fd9\u4e48\u505a\uff09\u3002","title":"\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26"},{"location":"5-5-Comma-and-conditional-operators/#_3","text":"\u8fd0\u7b97\u7b26 \u7b26\u53f7 \u5f62\u5f0f \u64cd\u4f5c \u6761\u4ef6\u8fd0\u7b97\u7b26 ?: c ? x : y \u5982\u679c c \u975e\u96f6\uff08\u771f\uff09\u5219\u5bf9 x \u5176\u4e2d\uff0c\u5426\u5219\u5bf9 y \u6c42\u503c \u6761\u4ef6\u8fd0\u7b97\u7b26 ( ?: ) (\u6709\u65f6\u4e5f\u79f0\u4e3a \u201d\u7b97\u6570 if \u201c\u8fd0\u7b97\u7b26) \u662f\u4e00\u4e2a\u4e09\u5143\u8fd0\u7b97\u7b26\uff08\u6709\u4e09\u4e2a\u64cd\u4f5c\u6570\uff09\u3002\u56e0\u4e3a\u5b83\u662f C++ \u4e2d\u552f\u4e00\u7684\u4e09\u5143\u8fd0\u7b97\u7b26\uff0c\u56e0\u6b64\u6709\u4f7f\u7528\u4e5f\u79f0\u5b83\u4e3a\u201d\u90a3\u4e2a\u4e09\u5143\u8fd0\u7b97\u7b26\u201c\u3002 ?: \u8fd0\u7b97\u7b26\u63d0\u4f9b\u4e86\u4e00\u79cd\u5feb\u6377\u7684\u6761\u4ef6\u8bed\u53e5\u529f\u80fd\u3002\u5982\u679c\u4f60\u5bf9\u6761\u4ef6\u8bed\u53e5\u8fd8\u6709\u7591\u95ee\uff0c\u8bf7\u53c2\u8003 4.10 - if \u8bed\u53e5\u7b80\u4ecb \u3002 if/else \u8bed\u53e5\u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 2 3 4 if ( condition ) statement1 ; else statement2 ; \u5982\u679c condition \u6c42\u503c\u7ed3\u679c\u4e3a true \uff0c\u5219 statement1 \u4f1a\u88ab\u6267\u884c\uff0c\u5426\u5219\u6267\u884c statement2 \u3002 ?: \u8fd0\u7b97\u7b26\u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 (condition) ? expression1 : expression2; \u5982\u679c condition \u6c42\u503c\u7ed3\u679c\u4e3a true \uff0c\u5219 statement1 \u4f1a\u88ab\u6267\u884c\uff0c\u5426\u5219\u6267\u884c statement2 \u3002\u6ce8\u610f\uff0c\u8fd9\u91cc\u7684 expression2 \u662f\u5fc5\u987b\u8981\u6709\u7684\u3002 \u4e0b\u9762\u8fd9\u4e2a if/else \u8bed\u53e5\uff1a 1 2 3 4 if ( x > y ) larger = x ; else larger = y ; \u53ef\u4ee5\u6539\u5199\u6210\u5982\u4e0b\u5f62\u5f0f\uff1a 1 larger = ( x > y ) ? x : y ; \u8fd9\u6837\u53ef\u4ee5\u5728\u4e0d\u727a\u7272\u53ef\u8bfb\u6027\u7684\u524d\u63d0\u4e0b\uff0c\u8ba9\u4ee3\u7801\u53d8\u5f97\u66f4\u52a0\u7d27\u51d1\u3002","title":"\u6761\u4ef6\u8fd0\u7b97\u7b26"},{"location":"5-5-Comma-and-conditional-operators/#_4","text":"\u6839\u636e\u60ef\u4f8b\uff0c\u6211\u4eec\u4e00\u822c\u4f1a\u628a\u6761\u4ef6\u8fd0\u7b97\u7b26\u7684 \u6761\u4ef6 \u90e8\u5206\u653e\u5728\u62ec\u53f7\u91cc\uff0c\u8fd9\u6837\u4e0d\u4ec5\u53ef\u8bfb\u6027\u66f4\u597d\uff0c\u800c\u4e14\u53ef\u4ee5\u786e\u4fdd\u8fd0\u7b97\u4f18\u5148\u7ea7\u662f\u6b63\u786e\u7684\u3002\u5176\u4ed6\u64cd\u4f5c\u6570\u7684\u6c42\u503c\u4f18\u5148\u7ea7\uff0c\u52a0\u4e0d\u52a0\u62ec\u53f7\u90fd\u662f\u4e00\u6837\u7684\uff0c\u6240\u4ee5\u5c31\u6ca1\u5fc5\u8981\u663e\u793a\u5730\u6dfb\u52a0\u62ec\u53f7\u4e86\u3002 \u6ce8\u610f\uff0c ?: \u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u975e\u5e38\u4f4e\u3002\u9664\u975e\u662f\u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u91cc\uff0c\u4e0d\u7136\u6574\u4e2a\u6761\u4ef6\u8fd0\u7b97\u7b26\u8868\u8fbe\u5f0f\u4e5f\u8981\u653e\u5728\u62ec\u53f7\u4e2d\u3002 \u4f8b\u5982\uff0c\u4e3a\u4e86\u6253\u5370 x \u548c y \u4e2d\u8f83\u5927\u7684\u503c\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 1 2 3 4 if ( x > y ) std :: cout << x ; else std :: cout << y ; \u6216\u8005\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8fd0\u7b97\u7b26\uff1a 1 std :: cout << (( x > y ) ? x : y ); \u8ba9\u6211\u4eec\u6d4b\u8bd5\u4e00\u4e0b\uff0c\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c\u5982\u679c\u4e0d\u628a\u6761\u4ef6\u8fd0\u7b97\u7b26\u8868\u8fbe\u5f0f\u653e\u5728\u4e00\u4e2a\u62ec\u53f7\u4e2d\u4f1a\u600e\u6837\u3002 \u56e0 << operator \u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e ?: \u8fd0\u7b97\u7b26\uff0c\u56e0\u6b64\u4e0b\u9762\u8bed\u53e5\uff1a 1 std :: cout << ( x > y ) ? x : y ; \u7b49\u4ef7\u4e8e\uff1a 1 ( std :: cout << ( x > y )) ? x : y ; \u56e0\u6b64\uff0c\u5982\u679c x>y \u5219\u7ed3\u679c\u4f1a\u6253\u5370 1(true)\uff0c\u5426\u5219\u6253\u53700\u3002 \u6700\u4f73\u5b9e\u8df5 \u603b\u662f\u628a\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u6761\u4ef6\u90e8\u5206\u653e\u5728\u62ec\u53f7\u91cc\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u8003\u8651\u628a\u6574\u4e2a\u90e8\u5206\u90fd\u653e\u5728\u62ec\u53f7\u91cc\u3002","title":"\u4e3a\u6761\u4ef6\u8fd0\u7b97\u7b26\u6dfb\u52a0\u62ec\u53f7"},{"location":"5-5-Comma-and-conditional-operators/#_5","text":"\u56e0\u4e3a\u6761\u4ef6\u8fd0\u7b97\u7b26\u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u8bed\u53e5\uff0c\u6240\u4ee5\u6761\u4ef6\u8fd0\u7b97\u7b26\u53ef\u4ee5\u88ab\u7528\u5728\u65e0\u6cd5\u4f7f\u7528 if/else \u7684\u5730\u65b9\u3002 \u4f8b\u5982\uff0c\u53ef\u4ee5\u7528\u5b83\u6765\u521d\u59cb\u5316\u4e00\u4e2a\u5e38\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { constexpr bool inBigClassroom { false }; constexpr int classSize { inBigClassroom ? 30 : 20 }; std :: cout << \"The class size is: \" << classSize << '\\n' ; return 0 ; } \u4f7f\u7528 if/else \u5e76\u4e0d\u80fd\u7528\u4e8e\u8fd9\u79cd\u5e38\u89c1\uff0c\u4f60\u53ef\u80fd\u4f1a\u89c9\u5f97\u4e0b\u9762\u8fd9\u79cd\u65b9\u6cd5\u662f\u53ef\u884c\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { constexpr bool inBigClassroom { false }; if ( inBigClassroom ) constexpr int classSize { 30 }; else constexpr int classSize { 20 }; std :: cout << \"The class size is: \" << classSize ; return 0 ; } \u5b9e\u9645\u4e0a\uff0c\u4e0a\u8ff0\u4ee3\u7801\u662f\u4e0d\u80fd\u7f16\u8bd1\u7684\uff0c\u4f60\u4f1a\u88ab\u544a\u77e5 claseSize \u6ca1\u6709\u5b9a\u4e49\u3002\u6b63\u5982\u5b9a\u4e49\u5728\u51fd\u6570\u4e2d\u7684\u53d8\u91cf\u4f1a\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u9500\u6bc1\u4e00\u6837\uff0c\u5b9a\u4e49\u5728 if/else \u8bed\u53e5\u5757\u4e2d\u7684\u53d8\u91cf\u4e5f\u4f1a\u5728 if/else \u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u6253\u5370 classSize \u65f6\uff0c\u8be5\u53d8\u91cf\u5b9e\u9645\u4e0a\u5df2\u7ecf\u88ab\u9500\u6bc1\u4e86\u3002 \u8bd1\u8005\u6ce8 \u5982\u679c\u4f60\u6ca1\u6709\u4e00\u773c\u770b\u51fa\u4e0a\u9762\u7684\u95ee\u9898\uff0c\u53ef\u80fd\u548c\u5b83\u7684\u8868\u73b0\u5f62\u5f0f\u6709\u5173\uff0c\u6211\u4eec\u90fd\u77e5\u9053\uff0c\u901a\u8fc7\u5927\u62ec\u53f7\u53ef\u4ee5\u9650\u5236\u53d8\u91cf\u7684\u4f5c\u7528\u57df\uff0c\u5982\u679c\u4fee\u6539\u4e3a\u4e0b\u9762\u8fd9\u6837\u7684\u5f62\u5f0f\uff0c\u4f60\u5e94\u8be5\u5c31\u80fd\u591f\u770b\u51fa\u4e86\uff1a 1 2 3 4 5 6 7 8 if ( inBigClassroom ) { constexpr int classSize { 30 }; } else { constexpr int classSize { 20 }; } \u53c2\u8003 6.1 - \u590d\u5408\u8bed\u53e5\uff08\u8bed\u53e5\u5757\uff09 \u548c 6.3 - \u5c40\u90e8\u53d8\u91cf \u56e0\u6b64\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u4f7f\u7528 if/else \u5b8c\u6210\u548c\u6761\u4ef6\u8868\u8fbe\u5f0f\u4e00\u6837\u7684\u6548\u679c\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u8fd9\u6837\u505a\uff1b 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int getClassSize ( bool inBigClassroom ) { if ( inBigClassroom ) return 30 ; return 20 ; } int main () { const int classSize { getClassSize ( false ) }; std :: cout << \"The class size is: \" << classSize ; return 0 ; } \u8fd9\u79cd\u65b9\u6cd5\u662f\u53ef\u884c\u7684\u56e0\u4e3a\u5b83\u6ca1\u6709\u5728 if/else \u4e2d\u5b9a\u4e49\u53d8\u91cf\uff0c\u6211\u4eec\u662f\u628a\u503c\u8fd4\u56de\u7ed9\u4e86\u4e3b\u8c03\u51fd\u6570\uff0c\u7136\u540e\u8be5\u503c\u88ab\u7528\u4e8e\u521d\u59cb\u5316\u3002 \u4f46\u8fd9\u4e48\u505a\u663e\u7136\u9700\u8981\u5f88\u591a\u989d\u5916\u7684\u6b65\u9aa4\u3002","title":"\u6761\u4ef6\u8fd0\u7b97\u7b26\u4f5c\u4e3a\u8868\u8fbe\u5f0f\u6c42\u503c"},{"location":"5-5-Comma-and-conditional-operators/#_6","text":"\u4e3a\u4e86\u7b26\u5408 C++ \u7c7b\u578b\u68c0\u67e5\u7684\u8981\u6c42\uff0c\u6761\u4ef6\u8bed\u53e5\u4e2d\u7684\u4e24\u4e2a\u8868\u8fbe\u5f0f\u7c7b\u578b\u9700\u8981\u5339\u914d\u6216\u8005\u53ef\u4ee5\u8f6c\u53d8\u4e3a\u5171\u540c\u7684\u7c7b\u578b\u3002 \u6269\u5c55\u9605\u8bfb \u5f53\u7c7b\u578b\u4e0d\u5339\u914d\u65f6\uff0c\u4f7f\u7528\u7684\u8f6c\u6362\u89c4\u5219\u975e\u5e38\u590d\u6742\uff0c\u4f60\u53ef\u4ee5\u5728 \u8fd9\u91cc \u627e\u5230\u8be6\u7ec6\u4fe1\u606f \u4e5f\u8bb8\u4f60\u89c9\u5f97\u4e0b\u9762\u8fd9\u6837\u505a\u662f\u53ef\u884c\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { constexpr int x { 5 }; std :: cout << ( x != 5 ? x : \"x is 5\" ); // \u4e0d\u80fd\u7f16\u8bd1 return 0 ; } \u5176\u5b9e\u4e0a\u9762\u7684\u4ee3\u7801\u5e76\u4e0d\u80fd\u7f16\u8bd1\u3002\u56e0\u4e3a\u5176\u4e2d\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7c7b\u578b\u662f\u6574\u578b\uff0c\u800c\u53e6\u5916\u4e00\u4e2a\u662f\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u3002\u7f16\u8bd1\u5668\u4e0d\u80fd\u591f\u786e\u5b9a\u8fd9\u4e9b\u7c7b\u578b\u516c\u5171\u7684\u7c7b\u578b\u662f\u4ec0\u4e48\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f60\u5fc5\u987b\u4f7f\u7528 if/else \u8bed\u53e5\u3002","title":"\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u5fc5\u987b\u5339\u914d\u6216\u8005\u53ef\u8f6c\u6362\u4e3a\u5339\u914d\u7684\u7c7b\u578b"},{"location":"5-5-Comma-and-conditional-operators/#_7","text":"\u6761\u4ef6\u8868\u8fbe\u5f0f\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u52a0\u7cbe\u7b80\u7684\u7c7b\u4f3c if/else \u8bed\u53e5\u7684\u529f\u80fd\u3002\u5b83\u5728\u9700\u8981\u8fdb\u884c\u6761\u4ef6\u521d\u59cb\u5316\uff08\u6216\u8d4b\u503c\uff09\u6216\u4f20\u53c2\u65f6\u5c24\u4e3a\u6709\u6548\u3002 \u8bf7\u4e0d\u8981\u5c1d\u8bd5\u5229\u7528\u5b83\u6765\u4ee3\u66ff\u590d\u6742\u7684 if/else \u8bed\u53e5\uff0c\u5426\u5219\u5f88\u5feb\u4f1a\u53d8\u5f97\u590d\u6742\u7684\u96be\u4ee5\u7ef4\u62a4\u3002 \u6700\u4f73\u5b9e\u8df5 \u4ec5\u5728\u9762\u5bf9\u7b80\u5355\u6761\u4ef6\u4e14\u4f60\u4f7f\u7528\u5176\u7ed3\u679c\uff0c\u540c\u65f6\u80fd\u591f\u589e\u5f3a\u53ef\u8bfb\u6027\u7684\u6761\u4ef6\u4e0b\uff0c\u4f7f\u7528\u6761\u4ef6\u8fd0\u7b97\u7b26","title":"\u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u4f7f\u7528\u6761\u4ef6\u64cd\u4f5c\u7b26\uff1f"},{"location":"5-6-Relational-operators-and-floating-point-comparisons/","tags":["operator","float point"],"text":"Key Takeaway \u8bf7\u907f\u514d\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528 operator== \u548c operator!= \u3002 \u6bd4\u8f83\u8fd0\u7b97\u7b26\u53ef\u4ee5\u5e94\u7528\u4e8e\u6d6e\u70b9\u6570 \u5173\u7cfb\u8fd0\u7b97\u7b26\u6307\u7684\u662f\u90a3\u4e9b\u5141\u8bb8\u6211\u4eec\u6bd4\u8f83\u4e24\u4e2a\u6570\u7684\u8fd0\u7b97\u7b26\uff0c\u8fd9\u7c7b\u8fd0\u7b97\u7b26\u6709 6 \u4e2a\uff1a \u8fd0\u7b97\u7b26 \u7b26\u53f7 \u5f62\u5f0f \u64cd\u4f5c \u5927\u4e8e > x > y \u5982\u679c x \u5927\u4e8e y\uff0c\u5219\u8fd4\u56de true\uff0c\u5426\u5219\u4e3a false \u5c0f\u4e8e < x < y \u5982\u679c x \u5c0f\u4e8e y\uff0c\u5219\u8fd4\u56de true\uff0c\u5426\u5219\u4e3a false \u5927\u4e8e\u7b49\u4e8e >= x >= y \u5982\u679c x \u5927\u4e8e\u7b49\u4e8e y\uff0c\u5219\u8fd4\u56de true\uff0c\u5426\u5219\u4e3a false \u5c0f\u4e8e\u7b49\u4e8e <= x <= y \u5982\u679c x \u5c0f\u4e8e\u7b49\u4e8e y\uff0c\u5219\u8fd4\u56de true\uff0c\u5426\u5219\u4e3a false \u76f8\u7b49 == x == y \u5982\u679c x \u76f8\u7b49 y\uff0c\u5219\u8fd4\u56de true\uff0c\u5426\u5219\u4e3a false \u4e0d\u7b49 != x != y \u5982\u679c x \u4e0d\u7b49 y\uff0c\u5219\u8fd4\u56de true\uff0c\u5426\u5219\u4e3a false \u8fd9\u91cc\u9762\u5927\u591a\u6570\u8fd0\u7b97\u7b26\u5982\u4f55\u5de5\u4f5c\uff0c\u76f8\u6bd4\u4f60\u5df2\u7ecf\u89c1\u8bc6\u8fc7\u4e86\uff0c\u800c\u4e14\u90fd\u975e\u5e38\u7b26\u5408\u5e38\u8bc6\u3002\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u7684\u6c42\u503c\u7ed3\u679c\u53ea\u4f1a\u662f true (1) \u6216 false (0) \u4e24\u79cd\u3002 \u4e0b\u9762\u4f8b\u7a0b\u5c55\u793a\u4e86\u5b83\u4eec\u7684\u4f7f\u7528\u65b9\u6cd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another integer: \" ; int y {}; std :: cin >> y ; if ( x == y ) std :: cout << x << \" equals \" << y << '\\n' ; if ( x != y ) std :: cout << x << \" does not equal \" << y << '\\n' ; if ( x > y ) std :: cout << x << \" is greater than \" << y << '\\n' ; if ( x < y ) std :: cout << x << \" is less than \" << y << '\\n' ; if ( x >= y ) std :: cout << x << \" is greater than or equal to \" << y << '\\n' ; if ( x <= y ) std :: cout << x << \" is less than or equal to \" << y << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 Enter an integer: 4 Enter another integer: 5 4 does not equal 5 4 is less than 5 4 is less than or equal to 5 \u5728\u5bf9\u6574\u578b\u8fdb\u884c\u6bd4\u8f83\u7684\u65f6\u5019\uff0c\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u90fd\u975e\u5e38\u7b80\u5355\u3002 \u5e03\u5c14\u6761\u4ef6\u503c \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c if \u8bed\u53e5\u6216 \u6761\u4ef6\u8fd0\u7b97\u7b26 \uff08\u4ee5\u53ca\u5176\u4ed6\u4e00\u4e9b\u7279\u6b8a\u573a\u666f\u4e0b\uff09\u4e2d\u7684\u6761\u4ef6\u6c42\u503c\u7ed3\u679c\u603b\u662f\u5e03\u5c14\u7c7b\u578b\u7684\u503c\u3002 \u5f88\u591a\u7a0b\u5e8f\u5458\u4f1a\u4f7f\u7528\u4e0b\u9762\u7684\u5199\u6cd5\uff1a 1 if ( b1 == true ) ... \u4f46\u662f\u8fd9\u79cd\u5199\u6cd5\u6709\u4e9b\u5197\u4f59\uff0c\u56e0\u4e3a == true \u5e76\u6ca1\u6709\u4e3a\u8fd9\u4e2a\u6761\u4ef6\u6dfb\u52a0\u4efb\u4f55\u503c\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u8fd9\u4e48\u5199\uff1a 1 if ( b1 ) ... \u540c\u6837\u7684\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 if ( b1 == false ) ... \u6700\u597d\u5199\u6210\u8fd9\u6837\uff1a 1 if ( ! b1 ) ... \u6700\u4f73\u5b9e\u8df5 \u4e0d\u8981\u4e3a\u6761\u4ef6\u6dfb\u52a0\u6ca1\u5fc5\u8981\u7684 == \u6216 != \uff0c\u5982\u679c\u6ca1\u6709\u5176\u4ed6\u7684\u503c\u5219\u4f1a\u8ba9\u53ef\u8bfb\u6027\u53d8\u5f97\u66f4\u5dee \u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\u53ef\u80fd\u4f1a\u5e26\u6765\u95ee\u9898 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { double d1 { 100.0 - 99.99 }; // \u5e94\u8be5\u7b49\u4e8e 0.01 double d2 { 10.0 - 9.99 }; // \u5e94\u8be5\u7b49\u4e8e 0.01 if ( d1 == d2 ) std :: cout << \"d1 == d2\" << '\\n' ; else if ( d1 > d2 ) std :: cout << \"d1 > d2\" << '\\n' ; else if ( d1 < d2 ) std :: cout << \"d1 < d2\" << '\\n' ; return 0 ; } \u53d8\u91cf d1 \u548c d2 \u5e94\u8be5\u90fd\u7b49\u4e8e 0.01 \u3002\u4f46\u662f\u4f60\u5bf9\u5b83\u4eec\u6bd4\u8f83\u7684\u8bdd\uff0c\u5c06\u4f1a\u4ea7\u751f\u4ee4\u4eba\u610f\u5916\u7684\u7ed3\u679c\uff1a 1 d1 > d2 \u5982\u679c\u4f60\u4f7f\u7528\u8c03\u8bd5\u5668\u6765\u67e5\u770b\u4e24\u4e2a\u53d8\u91cf\uff0c\u5219 d1 = 0.0100000000000005116 \u548c d2 = 0.0099999999999997868 \u3002\u8fd9\u4e24\u4e2a\u503c\u90fd\u63a5\u8fd1\u4e8e 0.01\uff0c\u4f46\u662f d1 \u6bd4 0.1 \u5927\uff0cd2 \u5219\u6bd4 0.1 \u5c0f\u3002 \u5982\u679c\u4f60\u9700\u8981\u5f88\u9ad8\u7684\u7cbe\u5ea6\uff0c\u5219\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528\u4e0a\u8ff0\u6bd4\u8f83\u8fd0\u7b97\u7b26\u662f\u5f88\u5371\u9669\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u6d6e\u70b9\u6570\u5e76\u4e0d\u7cbe\u786e\uff0c\u5f88\u5c0f\u7684 \u820d\u5165\u8bef\u5dee \u90fd\u6709\u53ef\u80fd\u9020\u6210\u4e0a\u8ff0\u610f\u5916\u60c5\u51b5\u3002 \u6211\u4eec\u5728 4.8 - \u6d6e\u70b9\u6570 \u4e2d\u4ecb\u7ecd\u4e86\u820d\u5165\u8bef\u5dee\u7684\u95ee\u9898\u3002 \u5f53\u5927\u4e8e\u5c0f\u4e8e\u53f7( < , <= , > \u548c >= ) \u6bcf\u7528\u5728\u6d6e\u70b9\u6570\u6bd4\u8f83\u65f6\uff0c\u901a\u5e38\u662f\u53ef\u4ee5\u5f97\u5230\u6b63\u786e\u7ed3\u679c\u7684\uff08\u9664\u975e\u4e24\u4e2a\u6570\u975e\u5e38\u63a5\u8fd1\uff09\u3002\u56e0\u6b64\uff0c\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528\u4f7f\u7528\u6b64\u7c7b\u6bd4\u8f83\u8fd0\u7b97\u7b26\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\uff0c\u53ea\u6709\u5f53\u4e24\u4e2a\u6570\u975e\u5e38\u63a5\u8fd1\u7684\u65f6\u5019\u624d\u6709\u53ef\u80fd\u5f97\u5230\u9519\u8bef\u7684\u7ed3\u679c\u3002 \u4f8b\u5982\uff0c\u8003\u8651\u6211\u4eec\u5728\u8bbe\u8ba1\u4e00\u4e2a\u6e38\u620f\uff08\u6bd4\u65b9\u8bf4\u300a\u592a\u7a7a\u4fb5\u7565\u8005\u300b\uff09\uff0c\u6b64\u65f6\u4f60\u9700\u8981\u5224\u65ad\u4e24\u4e2a\u7269\u4f53\u662f\u5426\u4f1a\u76f8\u4ea4\uff08\u6bd4\u5982\u8bf4\u5bfc\u5f39\u548c\u5916\u661f\u4eba\uff09\u3002\u5f53\u4e24\u4e2a\u7269\u4f53\u76f8\u8ddd\u751a\u8fdc\u65f6\uff0c\u8fd9\u4e9b\u6bd4\u8f83\u8fd0\u7b97\u7b26\u53ef\u4ee5\u8fd4\u56de\u6b63\u786e\u7684\u7ed3\u679c\u3002\u5f53\u4e24\u4e2a\u7269\u4f53\u5df2\u7ecf\u975e\u5e38\u63a5\u8fd1\u65f6\uff0c\u90a3\u4f60\u5176\u5b9e\u5df2\u7ecf\u5f97\u5230\u7ed3\u679c\u4e86\uff0c\u5373\u4f7f\u6bd4\u8f83\u7684\u7ed3\u8bba\u662f\u9519\u8bef\u7684\uff0c\u4f60\u53ef\u80fd\u4e5f\u4e0d\u4f1a\u6ce8\u610f\u5230\uff08\u770b\u4e0a\u53bb\u662f\u5728\u975e\u5e38\u8fd1\u7684\u4e3e\u4f8b\u547d\u4e2d\u6216\u4e22\u5931\u4e86\uff09\uff0c\u4e5f\u4e0d\u4f1a\u5bf9\u4f60\u7684\u6e38\u620f\u9020\u6210\u4e25\u91cd\u7684\u5f71\u54cd\u3002 \u6d6e\u70b9\u503c\u76f8\u7b49 \u76f8\u7b49\u8fd0\u7b97\u7b26( == \u548c != ) \u7684\u9ebb\u70e6\u5c31\u6bd4\u8f83\u5927\u3002\u5bf9\u4e8e\u8fd0\u7b97\u7b26 operator== \uff0c\u5b83\u53ea\u6709\u5728\u4e24\u4e2a\u64cd\u4f5c\u6570\u5b8c\u5168\u76f8\u7b49\u65f6\u624d\u8fd4\u56de true \u3002\u56e0\u4e3a\u5f88\u5c0f\u7684\u820d\u5165\u8bef\u5dee\u5c31\u4f1a\u4f7f\u4e24\u4e2a\u6d6e\u70b9\u6570\u4e0d\u7b49\uff0c\u6240\u4ee5 operator== \u6709\u975e\u5e38\u5927\u7684\u53ef\u80fd\u5728\u4f60\u8ba4\u4e3a\u5e94\u8be5\u8fd4\u56de true \u7684\u65f6\u5019\u8fd4\u56de false \u3002 Operator!= \u4e5f\u6709\u7c7b\u4f3c\u7684\u95ee\u9898\u3002 \u56e0\u6b64\uff0c\u8bf7\u907f\u514d\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528\u8fd9\u4e24\u79cd\u8fd0\u7b97\u7b26\u3002 \u6ce8\u610f \u8bf7\u907f\u514d\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528 operator== \u548c operator!= \u3002 \u6bd4\u8f83\u6d6e\u70b9\u6570 \uff08\u6269\u5c55\u9605\u8bfb\uff09 \u90a3\u4e48\uff0c\u5bf9\u4e24\u4e2a\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\u7684\u5408\u7406\u65b9\u6cd5\u662f\u4ec0\u4e48\u5462\uff1f \u5224\u65ad\u6d6e\u70b9\u6570\u662f\u5426\u76f8\u7b49\u7684\u5e38\u7528\u65b9\u6cd5\uff0c\u662f\u4f7f\u7528\u4e00\u4e2a\u51fd\u6570\u6765\u5224\u65ad\u4e24\u79cd\u662f\u4e0d\u662f \u975e\u5e38\u63a5\u8fd1 \u3002\u53ea\u8981\u975e\u5e38\u63a5\u8fd1\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u79f0\u5176 \u201d\u76f8\u7b49\u201c \u3002\u8868\u793a\u201d\u975e\u5e38\u63a5\u8fd1\u201c\u7684\u4f20\u7edf\u65b9\u6cd5\u662f\u4f7f\u7528epsilon\u3002\u5b83\u901a\u5e38\u4f1a\u88ab\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u975e\u5e38\u5c0f\u7684\u6b63\u6570\uff08\u4f8b\u5982\uff0c 0.00000001 \u6709\u65f6\u5019\u5199\u4f5c 1e-8 \uff09\u3002 \u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u4f7f\u7528\u81ea\u5df1\u5b9a\u4e49\u7684\u201d\u975e\u5e38\u63a5\u8fd1\u201c\u51fd\u6570\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 #include // for std::abs() // epsilon is an absolute value bool approximatelyEqualAbs ( double a , double b , double absEpsilon ) { // if the distance between a and b is less than absEpsilon, then a and b are \"close enough\" return std :: abs ( a - b ) <= absEpsilon ; } std::abs() \u662f \u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u5b83\u8fd4\u56de\u7684\u662f\u5176 \u5b9e\u53c2 \u7684\u7edd\u5bf9\u503c\u3002 std::abs(a - b) <= absEpsilon \u4f1a\u68c0\u67e5 a \u548c b \u7684\u5dee\u7684\u7edd\u5bf9\u503c\u662f\u5426\u5c0f\u4e8e\u4f20\u5165\u7684\u4efb\u4f55\u8868\u793a\u5f88\u63a5\u8fd1\u503c\u7684 \"epsilon\"\u3002\u5982\u679c a \u548c b \u8db3\u591f\u63a5\u8fd1\uff0c\u5219\u51fd\u6570\u4f1a\u8fd4\u56de true\uff0c\u8868\u793a\u5b83\u4eec\u76f8\u7b49\uff0c\u5426\u5219\u5219\u8fd4\u56de false\u3002 \u5c3d\u7ba1\u8fd9\u4e2a\u51fd\u6570\u786e\u5b9e\u53ef\u7528\uff0c\u4f46\u5e76\u4e0d\u662f\u6700\u4f73\u7684\u89e3\u51b3\u65b9\u6848\u3002 0.00001 \u5bf9\u4e8e1.0\u5de6\u53f3\u7684\u8f93\u5165\u662f\u5408\u9002\u7684\uff0c\u4f46\u662f\u5bf9\u4e8e 0.0000001 \u5de6\u53f3\u7684\u6570\u5c31\u592a\u5927\u4e86\uff0c\u800c\u5bf9\u4e8e 10000 \u8fd9\u6837\u7684\u8f93\u5165\u53c8\u592a\u5c0f\u4e86\u3002 \u9898\u5916\u8bdd \u5982\u679c\u6211\u4eec\u8bf4\uff0c\u5f53\u4e24\u4e2a\u6570\u7684\u5dee\u8ddd\u5c0f\u4e8e 0.00001 \u65f6\uff0c\u5b83\u4eec\u5c31\u80fd\u88ab\u770b\u505a\u662f\u76f8\u540c\u7684\u6570\uff0c\u90a3\u4e48\uff1a 1 \u548c 1.0001 \u662f\u4e0d\u540c\u7684\uff0c\u4f46\u662f 1 \u548c 1.00001 \u5219\u662f\u76f8\u540c\u7684\uff0c\u8fd9\u5e76\u4e0d\u5408\u7406\uff1b 0.0000001 \u548c 0.00001 \u662f\u76f8\u540c\u7684\uff0c\u8fd9\u770b\u8d77\u6765\u5e76\u4e0d\u5408\u7406\uff0c\u6bd4\u8f83\u5b83\u4eec\u5dee\u4e86\u4e00\u4e2a\u6570\u91cf\u7ea7\uff1b 10000 \u548c 10000.00001 \u5219\u662f\u4e0d\u540c\u7684\uff0c\u8fd9\u770b\u8d77\u6765\u4e5f\u5e76\u4e0d\u5408\u7406\uff0c\u8003\u8651\u5230\u8fd9\u4e24\u4e2a\u6570\u7684\u6570\u91cf\u7ea7\uff0c\u6709\u8fd9\u4e48\u5c0f\u7684\u5dee\u5f02\u88ab\u770b\u505a\u662f\u4e0d\u540c\u7684\u4e24\u4e2a\u6570\u663e\u7136\u4e0d\u5408\u7406\u3002 \u8fd9\u4e5f\u610f\u5473\u7740\uff0c\u6bcf\u6b21\u8c03\u7528\u8be5\u51fd\u6570\u7684\u65f6\u5019\uff0c\u6211\u4eec\u90fd\u5fc5\u987b\u4e3a\u8f93\u5165\u7684\u503c\u6311\u9009\u4e00\u4e2a\u5408\u9002\u7684 epsilon \u503c\u3002\u65e2\u7136\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u4e86 epsilon \u7684\u503c\u9700\u8981\u6839\u636e\u8f93\u5165\u6570\u636e\u7684\u6570\u91cf\u7ea7\u53d8\u5316\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u5e94\u8be5\u901a\u8fc7\u4fee\u6539\u51fd\u6570\u6765\u4f7f\u5176\u81ea\u52a8\u4e3a\u6211\u4eec\u5b8c\u6210\u8fd9\u4e2a\u5de5\u4f5c\u3002 Donald Knuth \u662f\u4e00\u4e2a\u8457\u540d\u7684\u8ba1\u7b97\u673a\u79d1\u5b66\u5bb6\uff0c\u4ed6\u5728\u4ed6\u7684\u8457\u4f5c \u201c\u8ba1\u7b97\u673a\u7a0b\u5e8f\u8bbe\u8ba1\u827a\u672f\u7b2c\u4e8c\u5377\uff1a\u534a\u6570\u503c\u7b97\u6cd5(Addison-Wesley, 1969)\u201c \u4e2d\u4ecb\u7ecd\u4e86\u8fd9\u6837\u7684\u65b9\u6cd5\uff1a 1 2 3 4 5 6 7 8 #include // std::max #include // std::abs // return true if the difference between a and b is within epsilon percent of the larger of a and b bool approximatelyEqualRel ( double a , double b , double relEpsilon ) { return ( std :: abs ( a - b ) <= ( std :: max ( std :: abs ( a ), std :: abs ( b )) * relEpsilon )); } \u548c\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e00\u4e2a\u7edd\u5bf9\u7684 epsilon \u503c\u76f8\u6bd4\uff0c\u8fd9\u91cc\u7684 epsilon \u662f\u4e00\u4e2a\u4e0e a \u6216 b \u6570\u91cf\u7ea7\u6709\u5173\u7684\u76f8\u5bf9\u503c\u3002 \u8ba9\u6211\u4eec\u4ed4\u7ec6\u5206\u6790\u4e00\u4e0b\u8fd9\u4e2a\u770b\u4e0a\u53bb\u975e\u5e38\u590d\u6742\u7684\u51fd\u6570\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u5728\u4e0d\u7b49\u53f7 <= operator \u7684\u5de6\u8fb9 std::abs(a - b) \u4e3a\u6211\u4eec\u6c42\u51fa\u4e86 a \u548c b \u4e4b\u95f4\u7684\u5dee\u5f02\uff08\u4ee5\u4e00\u4e2a\u6b63\u6570\u6765\u8868\u793a\uff09\u3002 \u5728\u4e0d\u7b49\u53f7 <= operator \u7684\u53f3\u8fb9\uff0c\u6211\u4eec\u9700\u8981\u8ba1\u7b97\u90a3\u4e2a\u6240\u8c13\u201d\u8db3\u591f\u63a5\u8fd1\u201c\u7684\u503c\u7684\u6700\u5927\u503c\u3002\u4e3a\u6b64\uff0c\u8fd9\u4e2a\u7b97\u6cd5\u9009\u53d6\u4e86 a \u548c b \u4e2d\u8f83\u5927\u7684\u4e00\u4e2a\u503c\uff08\u4f5c\u4e3a\u6570\u91cf\u7ea7\u7684\u4e00\u4e2a\u8fd1\u4f3c\u6307\u6807\uff09\uff0c\u7136\u540e\u5c06\u5176\u4e58\u4ee5 relEpsilon \u3002\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c relEpsilon \u8868\u793a\u7684\u662f\u4e00\u4e2a\u767e\u5206\u6bd4\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u79f0\u201d\u8db3\u591f\u63a5\u8fd1\u201c\u610f\u5473\u7740 a \u548c b \u7684\u5dee\u5f02\u5728 1% \u4ee5\u5185\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u5c06 relEpsilon \u8bbe\u4e3a 0.01 (1% = 1/100 = 0.01)\uff0c relEpsilon \u7684\u503c\u53ef\u4ee5\u9009\u53d6\u6700\u9002\u5408\u6211\u4eec\u7684\u503c (\u4f8b\u5982 epsilon \u4e3a 0.002 \u8868\u793a\u4e24\u8005\u5dee\u5f02\u5728 0.2% \u4ee5\u5185)\u3002 \u5982\u679c\u8981\u5224\u65ad\u662f\u5426\u4e0d\u76f8\u7b49 ( != ) \uff0c\u4e5f\u53ef\u4ee5\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u7136\u540e\u5728\u524d\u9762\u6dfb\u52a0\u903b\u8f91\u975e ( ! ) \u8fd0\u7b97\u7b26\uff0c\u5bf9\u7ed3\u679c\u53d6\u53cd\uff1a 1 2 if ( ! approximatelyEqualRel ( a , b , 0.001 )) std :: cout << a << \" is not equal to \" << b << '\\n' ; \u6ce8\u610f\uff0c\u5c3d\u7ba1 approximatelyEqual() \u51fd\u6570\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u90fd\u53ef\u4ee5\u6b63\u786e\u5de5\u4f5c\uff0c\u5b83\u4ecd\u7136\u4e0d\u662f\u5b8c\u7f8e\u7684\uff0c\u5c24\u5176\u662f\u5f53\u4e24\u6570\u63a5\u8fd10\u65f6\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include #include // return true if the difference between a and b is within epsilon percent of the larger of a and b bool approximatelyEqualRel ( double a , double b , double relEpsilon ) { return ( std :: abs ( a - b ) <= ( std :: max ( std :: abs ( a ), std :: abs ( b )) * relEpsilon )); } int main () { // a \u975e\u5e38\u63a5\u8fd1 1.0\uff0c\u4f46\u662f\u7531\u4e8e\u820d\u5165\u8bef\u5dee\u7684\u5b58\u5728\uff0c\u5b83\u7a0d\u5fae\u5c0f\u4e8e 1.0 double a { 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 }; // \u9996\u5148, \u8ba9\u6211\u4eec\u628a a \u548c 0.1 \u76f8\u6bd4 std :: cout << approximatelyEqualRel ( a , 1.0 , 1e-8 ) << '\\n' ; // \u7136\u540e, \u8ba9\u6211\u4eec\u628a a - 0.1 (\u51e0\u4e4e\u4e3a 0.0) \u548c 0.0 \u76f8\u6bd4 std :: cout << approximatelyEqualRel ( a -1.0 , 0.0 , 1e-8 ) << '\\n' ; } \u51fa\u4eba\u610f\u6599\u7684\u662f\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 1 0 \u7b2c\u4e8c\u4e2a\u51fd\u6570\u8c03\u7528\u5e76\u6ca1\u6709\u7b26\u5408\u6211\u4eec\u7684\u9884\u671f\uff0c\u5f53\u6570\u636e\u63a5\u8fd1 0 \u65f6\u5c31\u4f1a\u51fa\u95ee\u9898\u3002 \u907f\u514d\u51fa\u73b0\u8be5\u95ee\u9898\u7684\u65b9\u5f0f\uff0c\u662f\u540c\u65f6\u4f7f\u7528\u4e00\u4e2a\u7edd\u5bf9 epsilon (\u50cf\u7b2c\u4e00\u4e2a\u4f8b\u5b50\u90a3\u6837) \u548c\u4e00\u4e2a\u76f8\u5bf9 epsilon (\u50cf Knuth \u7684\u4f8b\u5b50\u90a3\u6837)\uff1a 1 2 3 4 5 6 7 8 9 10 11 // return true if the difference between a and b is less than absEpsilon, or within relEpsilon percent of the larger of a and b bool approximatelyEqualAbsRel ( double a , double b , double absEpsilon , double relEpsilon ) { // Check if the numbers are really close -- needed when comparing numbers near zero. double diff { std :: abs ( a - b ) }; if ( diff <= absEpsilon ) return true ; // Otherwise fall back to Knuth's algorithm return ( diff <= ( std :: max ( std :: abs ( a ), std :: abs ( b )) * relEpsilon )); } \u5728\u8fd9\u4e2a\u7b97\u6cd5\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u68c0\u67e5 a \u548c b \u662f\u5426\u8db3\u591f\u63a5\u8fd1\uff0c\u7528\u4e8e\u5904\u7406 a \u548c b \u90fd\u975e\u5e38\u63a5\u8fd1 0 \u7684\u60c5\u51b5\u3002 absEpsilon \u53c2\u6570\u5e94\u8be5\u88ab\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u975e\u5e38\u5c0f\u7684\u6570\uff08\u4f8b\u5982 1e-12 \uff09\uff0c\u5982\u679c\u8fd9\u4e00\u6b65\u7684\u5224\u65ad\u6ca1\u6709\u6210\u529f\uff0c\u5219\u4f7f\u7528 Knuth \u7684\u7b97\u6cd5\uff0c\u4f7f\u7528\u76f8\u5bf9\u7684 epsilon\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u7528\u4e8e\u5bf9\u4e24\u79cd\u65b9\u6cd5\u8fdb\u884c\u6d4b\u8bd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include #include // return true if the difference between a and b is within epsilon percent of the larger of a and b bool approximatelyEqualRel ( double a , double b , double relEpsilon ) { return ( std :: abs ( a - b ) <= ( std :: max ( std :: abs ( a ), std :: abs ( b )) * relEpsilon )); } bool approximatelyEqualAbsRel ( double a , double b , double absEpsilon , double relEpsilon ) { // Check if the numbers are really close -- needed when comparing numbers near zero. double diff { std :: abs ( a - b ) }; if ( diff <= absEpsilon ) return true ; // Otherwise fall back to Knuth's algorithm return ( diff <= ( std :: max ( std :: abs ( a ), std :: abs ( b )) * relEpsilon )); } int main () { // a is really close to 1.0, but has rounding errors double a { 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 }; std :: cout << approximatelyEqualRel ( a , 1.0 , 1e-8 ) << '\\n' ; // compare \"almost 1.0\" to 1.0 std :: cout << approximatelyEqualRel ( a -1.0 , 0.0 , 1e-8 ) << '\\n' ; // compare \"almost 0.0\" to 0.0 std :: cout << approximatelyEqualAbsRel ( a , 1.0 , 1e-12 , 1e-8 ) << '\\n' ; // compare \"almost 1.0\" to 1.0 std :: cout << approximatelyEqualAbsRel ( a -1.0 , 0.0 , 1e-12 , 1e-8 ) << '\\n' ; // compare \"almost 0.0\" to 0.0 } 1 2 3 4 1 0 1 1 \u53ef\u4ee5\u770b\u5230 approximatelyEqualAbsRel() \u53ef\u4ee5\u6b63\u786e\u5730\u5904\u7406\u6570\u503c\u5f88\u5c0f\u7684\u60c5\u51b5\u3002 \u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\u662f\u4e00\u4e2a\u5f88\u590d\u6742\u7684\u95ee\u9898\uff0c\u800c\u4e14\u6ca1\u6709\u4e07\u80fd\u7684\u7b97\u6cd5\u53ef\u4ee5\u5e94\u5bf9\u6240\u6709\u7684\u60c5\u5f62\u3002\u4e0d\u8fc7\uff0c approximatelyEqualAbsRel() \u914d\u5408 1e-12 \u7684 absEpsilon \u4ee5\u53ca 1e-8 \u7684 relEpsilon \u5df2\u7ecf\u8db3\u591f\u5e94\u5bf9\u5927\u591a\u6570\u60c5\u51b5\u4e86\u3002","title":"5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83"},{"location":"5-6-Relational-operators-and-floating-point-comparisons/#_1","text":"\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c if \u8bed\u53e5\u6216 \u6761\u4ef6\u8fd0\u7b97\u7b26 \uff08\u4ee5\u53ca\u5176\u4ed6\u4e00\u4e9b\u7279\u6b8a\u573a\u666f\u4e0b\uff09\u4e2d\u7684\u6761\u4ef6\u6c42\u503c\u7ed3\u679c\u603b\u662f\u5e03\u5c14\u7c7b\u578b\u7684\u503c\u3002 \u5f88\u591a\u7a0b\u5e8f\u5458\u4f1a\u4f7f\u7528\u4e0b\u9762\u7684\u5199\u6cd5\uff1a 1 if ( b1 == true ) ... \u4f46\u662f\u8fd9\u79cd\u5199\u6cd5\u6709\u4e9b\u5197\u4f59\uff0c\u56e0\u4e3a == true \u5e76\u6ca1\u6709\u4e3a\u8fd9\u4e2a\u6761\u4ef6\u6dfb\u52a0\u4efb\u4f55\u503c\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u8fd9\u4e48\u5199\uff1a 1 if ( b1 ) ... \u540c\u6837\u7684\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 if ( b1 == false ) ... \u6700\u597d\u5199\u6210\u8fd9\u6837\uff1a 1 if ( ! b1 ) ... \u6700\u4f73\u5b9e\u8df5 \u4e0d\u8981\u4e3a\u6761\u4ef6\u6dfb\u52a0\u6ca1\u5fc5\u8981\u7684 == \u6216 != \uff0c\u5982\u679c\u6ca1\u6709\u5176\u4ed6\u7684\u503c\u5219\u4f1a\u8ba9\u53ef\u8bfb\u6027\u53d8\u5f97\u66f4\u5dee","title":"\u5e03\u5c14\u6761\u4ef6\u503c"},{"location":"5-6-Relational-operators-and-floating-point-comparisons/#_2","text":"\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { double d1 { 100.0 - 99.99 }; // \u5e94\u8be5\u7b49\u4e8e 0.01 double d2 { 10.0 - 9.99 }; // \u5e94\u8be5\u7b49\u4e8e 0.01 if ( d1 == d2 ) std :: cout << \"d1 == d2\" << '\\n' ; else if ( d1 > d2 ) std :: cout << \"d1 > d2\" << '\\n' ; else if ( d1 < d2 ) std :: cout << \"d1 < d2\" << '\\n' ; return 0 ; } \u53d8\u91cf d1 \u548c d2 \u5e94\u8be5\u90fd\u7b49\u4e8e 0.01 \u3002\u4f46\u662f\u4f60\u5bf9\u5b83\u4eec\u6bd4\u8f83\u7684\u8bdd\uff0c\u5c06\u4f1a\u4ea7\u751f\u4ee4\u4eba\u610f\u5916\u7684\u7ed3\u679c\uff1a 1 d1 > d2 \u5982\u679c\u4f60\u4f7f\u7528\u8c03\u8bd5\u5668\u6765\u67e5\u770b\u4e24\u4e2a\u53d8\u91cf\uff0c\u5219 d1 = 0.0100000000000005116 \u548c d2 = 0.0099999999999997868 \u3002\u8fd9\u4e24\u4e2a\u503c\u90fd\u63a5\u8fd1\u4e8e 0.01\uff0c\u4f46\u662f d1 \u6bd4 0.1 \u5927\uff0cd2 \u5219\u6bd4 0.1 \u5c0f\u3002 \u5982\u679c\u4f60\u9700\u8981\u5f88\u9ad8\u7684\u7cbe\u5ea6\uff0c\u5219\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528\u4e0a\u8ff0\u6bd4\u8f83\u8fd0\u7b97\u7b26\u662f\u5f88\u5371\u9669\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u6d6e\u70b9\u6570\u5e76\u4e0d\u7cbe\u786e\uff0c\u5f88\u5c0f\u7684 \u820d\u5165\u8bef\u5dee \u90fd\u6709\u53ef\u80fd\u9020\u6210\u4e0a\u8ff0\u610f\u5916\u60c5\u51b5\u3002 \u6211\u4eec\u5728 4.8 - \u6d6e\u70b9\u6570 \u4e2d\u4ecb\u7ecd\u4e86\u820d\u5165\u8bef\u5dee\u7684\u95ee\u9898\u3002 \u5f53\u5927\u4e8e\u5c0f\u4e8e\u53f7( < , <= , > \u548c >= ) \u6bcf\u7528\u5728\u6d6e\u70b9\u6570\u6bd4\u8f83\u65f6\uff0c\u901a\u5e38\u662f\u53ef\u4ee5\u5f97\u5230\u6b63\u786e\u7ed3\u679c\u7684\uff08\u9664\u975e\u4e24\u4e2a\u6570\u975e\u5e38\u63a5\u8fd1\uff09\u3002\u56e0\u6b64\uff0c\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528\u4f7f\u7528\u6b64\u7c7b\u6bd4\u8f83\u8fd0\u7b97\u7b26\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\uff0c\u53ea\u6709\u5f53\u4e24\u4e2a\u6570\u975e\u5e38\u63a5\u8fd1\u7684\u65f6\u5019\u624d\u6709\u53ef\u80fd\u5f97\u5230\u9519\u8bef\u7684\u7ed3\u679c\u3002 \u4f8b\u5982\uff0c\u8003\u8651\u6211\u4eec\u5728\u8bbe\u8ba1\u4e00\u4e2a\u6e38\u620f\uff08\u6bd4\u65b9\u8bf4\u300a\u592a\u7a7a\u4fb5\u7565\u8005\u300b\uff09\uff0c\u6b64\u65f6\u4f60\u9700\u8981\u5224\u65ad\u4e24\u4e2a\u7269\u4f53\u662f\u5426\u4f1a\u76f8\u4ea4\uff08\u6bd4\u5982\u8bf4\u5bfc\u5f39\u548c\u5916\u661f\u4eba\uff09\u3002\u5f53\u4e24\u4e2a\u7269\u4f53\u76f8\u8ddd\u751a\u8fdc\u65f6\uff0c\u8fd9\u4e9b\u6bd4\u8f83\u8fd0\u7b97\u7b26\u53ef\u4ee5\u8fd4\u56de\u6b63\u786e\u7684\u7ed3\u679c\u3002\u5f53\u4e24\u4e2a\u7269\u4f53\u5df2\u7ecf\u975e\u5e38\u63a5\u8fd1\u65f6\uff0c\u90a3\u4f60\u5176\u5b9e\u5df2\u7ecf\u5f97\u5230\u7ed3\u679c\u4e86\uff0c\u5373\u4f7f\u6bd4\u8f83\u7684\u7ed3\u8bba\u662f\u9519\u8bef\u7684\uff0c\u4f60\u53ef\u80fd\u4e5f\u4e0d\u4f1a\u6ce8\u610f\u5230\uff08\u770b\u4e0a\u53bb\u662f\u5728\u975e\u5e38\u8fd1\u7684\u4e3e\u4f8b\u547d\u4e2d\u6216\u4e22\u5931\u4e86\uff09\uff0c\u4e5f\u4e0d\u4f1a\u5bf9\u4f60\u7684\u6e38\u620f\u9020\u6210\u4e25\u91cd\u7684\u5f71\u54cd\u3002","title":"\u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\u53ef\u80fd\u4f1a\u5e26\u6765\u95ee\u9898"},{"location":"5-6-Relational-operators-and-floating-point-comparisons/#_3","text":"\u76f8\u7b49\u8fd0\u7b97\u7b26( == \u548c != ) \u7684\u9ebb\u70e6\u5c31\u6bd4\u8f83\u5927\u3002\u5bf9\u4e8e\u8fd0\u7b97\u7b26 operator== \uff0c\u5b83\u53ea\u6709\u5728\u4e24\u4e2a\u64cd\u4f5c\u6570\u5b8c\u5168\u76f8\u7b49\u65f6\u624d\u8fd4\u56de true \u3002\u56e0\u4e3a\u5f88\u5c0f\u7684\u820d\u5165\u8bef\u5dee\u5c31\u4f1a\u4f7f\u4e24\u4e2a\u6d6e\u70b9\u6570\u4e0d\u7b49\uff0c\u6240\u4ee5 operator== \u6709\u975e\u5e38\u5927\u7684\u53ef\u80fd\u5728\u4f60\u8ba4\u4e3a\u5e94\u8be5\u8fd4\u56de true \u7684\u65f6\u5019\u8fd4\u56de false \u3002 Operator!= \u4e5f\u6709\u7c7b\u4f3c\u7684\u95ee\u9898\u3002 \u56e0\u6b64\uff0c\u8bf7\u907f\u514d\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528\u8fd9\u4e24\u79cd\u8fd0\u7b97\u7b26\u3002 \u6ce8\u610f \u8bf7\u907f\u514d\u5bf9\u6d6e\u70b9\u6570\u4f7f\u7528 operator== \u548c operator!= \u3002","title":"\u6d6e\u70b9\u503c\u76f8\u7b49"},{"location":"5-6-Relational-operators-and-floating-point-comparisons/#_4","text":"\u90a3\u4e48\uff0c\u5bf9\u4e24\u4e2a\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\u7684\u5408\u7406\u65b9\u6cd5\u662f\u4ec0\u4e48\u5462\uff1f \u5224\u65ad\u6d6e\u70b9\u6570\u662f\u5426\u76f8\u7b49\u7684\u5e38\u7528\u65b9\u6cd5\uff0c\u662f\u4f7f\u7528\u4e00\u4e2a\u51fd\u6570\u6765\u5224\u65ad\u4e24\u79cd\u662f\u4e0d\u662f \u975e\u5e38\u63a5\u8fd1 \u3002\u53ea\u8981\u975e\u5e38\u63a5\u8fd1\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u79f0\u5176 \u201d\u76f8\u7b49\u201c \u3002\u8868\u793a\u201d\u975e\u5e38\u63a5\u8fd1\u201c\u7684\u4f20\u7edf\u65b9\u6cd5\u662f\u4f7f\u7528epsilon\u3002\u5b83\u901a\u5e38\u4f1a\u88ab\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u975e\u5e38\u5c0f\u7684\u6b63\u6570\uff08\u4f8b\u5982\uff0c 0.00000001 \u6709\u65f6\u5019\u5199\u4f5c 1e-8 \uff09\u3002 \u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u4f7f\u7528\u81ea\u5df1\u5b9a\u4e49\u7684\u201d\u975e\u5e38\u63a5\u8fd1\u201c\u51fd\u6570\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 #include // for std::abs() // epsilon is an absolute value bool approximatelyEqualAbs ( double a , double b , double absEpsilon ) { // if the distance between a and b is less than absEpsilon, then a and b are \"close enough\" return std :: abs ( a - b ) <= absEpsilon ; } std::abs() \u662f \u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u5b83\u8fd4\u56de\u7684\u662f\u5176 \u5b9e\u53c2 \u7684\u7edd\u5bf9\u503c\u3002 std::abs(a - b) <= absEpsilon \u4f1a\u68c0\u67e5 a \u548c b \u7684\u5dee\u7684\u7edd\u5bf9\u503c\u662f\u5426\u5c0f\u4e8e\u4f20\u5165\u7684\u4efb\u4f55\u8868\u793a\u5f88\u63a5\u8fd1\u503c\u7684 \"epsilon\"\u3002\u5982\u679c a \u548c b \u8db3\u591f\u63a5\u8fd1\uff0c\u5219\u51fd\u6570\u4f1a\u8fd4\u56de true\uff0c\u8868\u793a\u5b83\u4eec\u76f8\u7b49\uff0c\u5426\u5219\u5219\u8fd4\u56de false\u3002 \u5c3d\u7ba1\u8fd9\u4e2a\u51fd\u6570\u786e\u5b9e\u53ef\u7528\uff0c\u4f46\u5e76\u4e0d\u662f\u6700\u4f73\u7684\u89e3\u51b3\u65b9\u6848\u3002 0.00001 \u5bf9\u4e8e1.0\u5de6\u53f3\u7684\u8f93\u5165\u662f\u5408\u9002\u7684\uff0c\u4f46\u662f\u5bf9\u4e8e 0.0000001 \u5de6\u53f3\u7684\u6570\u5c31\u592a\u5927\u4e86\uff0c\u800c\u5bf9\u4e8e 10000 \u8fd9\u6837\u7684\u8f93\u5165\u53c8\u592a\u5c0f\u4e86\u3002 \u9898\u5916\u8bdd \u5982\u679c\u6211\u4eec\u8bf4\uff0c\u5f53\u4e24\u4e2a\u6570\u7684\u5dee\u8ddd\u5c0f\u4e8e 0.00001 \u65f6\uff0c\u5b83\u4eec\u5c31\u80fd\u88ab\u770b\u505a\u662f\u76f8\u540c\u7684\u6570\uff0c\u90a3\u4e48\uff1a 1 \u548c 1.0001 \u662f\u4e0d\u540c\u7684\uff0c\u4f46\u662f 1 \u548c 1.00001 \u5219\u662f\u76f8\u540c\u7684\uff0c\u8fd9\u5e76\u4e0d\u5408\u7406\uff1b 0.0000001 \u548c 0.00001 \u662f\u76f8\u540c\u7684\uff0c\u8fd9\u770b\u8d77\u6765\u5e76\u4e0d\u5408\u7406\uff0c\u6bd4\u8f83\u5b83\u4eec\u5dee\u4e86\u4e00\u4e2a\u6570\u91cf\u7ea7\uff1b 10000 \u548c 10000.00001 \u5219\u662f\u4e0d\u540c\u7684\uff0c\u8fd9\u770b\u8d77\u6765\u4e5f\u5e76\u4e0d\u5408\u7406\uff0c\u8003\u8651\u5230\u8fd9\u4e24\u4e2a\u6570\u7684\u6570\u91cf\u7ea7\uff0c\u6709\u8fd9\u4e48\u5c0f\u7684\u5dee\u5f02\u88ab\u770b\u505a\u662f\u4e0d\u540c\u7684\u4e24\u4e2a\u6570\u663e\u7136\u4e0d\u5408\u7406\u3002 \u8fd9\u4e5f\u610f\u5473\u7740\uff0c\u6bcf\u6b21\u8c03\u7528\u8be5\u51fd\u6570\u7684\u65f6\u5019\uff0c\u6211\u4eec\u90fd\u5fc5\u987b\u4e3a\u8f93\u5165\u7684\u503c\u6311\u9009\u4e00\u4e2a\u5408\u9002\u7684 epsilon \u503c\u3002\u65e2\u7136\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u4e86 epsilon \u7684\u503c\u9700\u8981\u6839\u636e\u8f93\u5165\u6570\u636e\u7684\u6570\u91cf\u7ea7\u53d8\u5316\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u5e94\u8be5\u901a\u8fc7\u4fee\u6539\u51fd\u6570\u6765\u4f7f\u5176\u81ea\u52a8\u4e3a\u6211\u4eec\u5b8c\u6210\u8fd9\u4e2a\u5de5\u4f5c\u3002 Donald Knuth \u662f\u4e00\u4e2a\u8457\u540d\u7684\u8ba1\u7b97\u673a\u79d1\u5b66\u5bb6\uff0c\u4ed6\u5728\u4ed6\u7684\u8457\u4f5c \u201c\u8ba1\u7b97\u673a\u7a0b\u5e8f\u8bbe\u8ba1\u827a\u672f\u7b2c\u4e8c\u5377\uff1a\u534a\u6570\u503c\u7b97\u6cd5(Addison-Wesley, 1969)\u201c \u4e2d\u4ecb\u7ecd\u4e86\u8fd9\u6837\u7684\u65b9\u6cd5\uff1a 1 2 3 4 5 6 7 8 #include // std::max #include // std::abs // return true if the difference between a and b is within epsilon percent of the larger of a and b bool approximatelyEqualRel ( double a , double b , double relEpsilon ) { return ( std :: abs ( a - b ) <= ( std :: max ( std :: abs ( a ), std :: abs ( b )) * relEpsilon )); } \u548c\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e00\u4e2a\u7edd\u5bf9\u7684 epsilon \u503c\u76f8\u6bd4\uff0c\u8fd9\u91cc\u7684 epsilon \u662f\u4e00\u4e2a\u4e0e a \u6216 b \u6570\u91cf\u7ea7\u6709\u5173\u7684\u76f8\u5bf9\u503c\u3002 \u8ba9\u6211\u4eec\u4ed4\u7ec6\u5206\u6790\u4e00\u4e0b\u8fd9\u4e2a\u770b\u4e0a\u53bb\u975e\u5e38\u590d\u6742\u7684\u51fd\u6570\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u5728\u4e0d\u7b49\u53f7 <= operator \u7684\u5de6\u8fb9 std::abs(a - b) \u4e3a\u6211\u4eec\u6c42\u51fa\u4e86 a \u548c b \u4e4b\u95f4\u7684\u5dee\u5f02\uff08\u4ee5\u4e00\u4e2a\u6b63\u6570\u6765\u8868\u793a\uff09\u3002 \u5728\u4e0d\u7b49\u53f7 <= operator \u7684\u53f3\u8fb9\uff0c\u6211\u4eec\u9700\u8981\u8ba1\u7b97\u90a3\u4e2a\u6240\u8c13\u201d\u8db3\u591f\u63a5\u8fd1\u201c\u7684\u503c\u7684\u6700\u5927\u503c\u3002\u4e3a\u6b64\uff0c\u8fd9\u4e2a\u7b97\u6cd5\u9009\u53d6\u4e86 a \u548c b \u4e2d\u8f83\u5927\u7684\u4e00\u4e2a\u503c\uff08\u4f5c\u4e3a\u6570\u91cf\u7ea7\u7684\u4e00\u4e2a\u8fd1\u4f3c\u6307\u6807\uff09\uff0c\u7136\u540e\u5c06\u5176\u4e58\u4ee5 relEpsilon \u3002\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c relEpsilon \u8868\u793a\u7684\u662f\u4e00\u4e2a\u767e\u5206\u6bd4\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u79f0\u201d\u8db3\u591f\u63a5\u8fd1\u201c\u610f\u5473\u7740 a \u548c b \u7684\u5dee\u5f02\u5728 1% \u4ee5\u5185\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u5c06 relEpsilon \u8bbe\u4e3a 0.01 (1% = 1/100 = 0.01)\uff0c relEpsilon \u7684\u503c\u53ef\u4ee5\u9009\u53d6\u6700\u9002\u5408\u6211\u4eec\u7684\u503c (\u4f8b\u5982 epsilon \u4e3a 0.002 \u8868\u793a\u4e24\u8005\u5dee\u5f02\u5728 0.2% \u4ee5\u5185)\u3002 \u5982\u679c\u8981\u5224\u65ad\u662f\u5426\u4e0d\u76f8\u7b49 ( != ) \uff0c\u4e5f\u53ef\u4ee5\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u7136\u540e\u5728\u524d\u9762\u6dfb\u52a0\u903b\u8f91\u975e ( ! ) \u8fd0\u7b97\u7b26\uff0c\u5bf9\u7ed3\u679c\u53d6\u53cd\uff1a 1 2 if ( ! approximatelyEqualRel ( a , b , 0.001 )) std :: cout << a << \" is not equal to \" << b << '\\n' ; \u6ce8\u610f\uff0c\u5c3d\u7ba1 approximatelyEqual() \u51fd\u6570\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u90fd\u53ef\u4ee5\u6b63\u786e\u5de5\u4f5c\uff0c\u5b83\u4ecd\u7136\u4e0d\u662f\u5b8c\u7f8e\u7684\uff0c\u5c24\u5176\u662f\u5f53\u4e24\u6570\u63a5\u8fd10\u65f6\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include #include // return true if the difference between a and b is within epsilon percent of the larger of a and b bool approximatelyEqualRel ( double a , double b , double relEpsilon ) { return ( std :: abs ( a - b ) <= ( std :: max ( std :: abs ( a ), std :: abs ( b )) * relEpsilon )); } int main () { // a \u975e\u5e38\u63a5\u8fd1 1.0\uff0c\u4f46\u662f\u7531\u4e8e\u820d\u5165\u8bef\u5dee\u7684\u5b58\u5728\uff0c\u5b83\u7a0d\u5fae\u5c0f\u4e8e 1.0 double a { 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 }; // \u9996\u5148, \u8ba9\u6211\u4eec\u628a a \u548c 0.1 \u76f8\u6bd4 std :: cout << approximatelyEqualRel ( a , 1.0 , 1e-8 ) << '\\n' ; // \u7136\u540e, \u8ba9\u6211\u4eec\u628a a - 0.1 (\u51e0\u4e4e\u4e3a 0.0) \u548c 0.0 \u76f8\u6bd4 std :: cout << approximatelyEqualRel ( a -1.0 , 0.0 , 1e-8 ) << '\\n' ; } \u51fa\u4eba\u610f\u6599\u7684\u662f\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 1 0 \u7b2c\u4e8c\u4e2a\u51fd\u6570\u8c03\u7528\u5e76\u6ca1\u6709\u7b26\u5408\u6211\u4eec\u7684\u9884\u671f\uff0c\u5f53\u6570\u636e\u63a5\u8fd1 0 \u65f6\u5c31\u4f1a\u51fa\u95ee\u9898\u3002 \u907f\u514d\u51fa\u73b0\u8be5\u95ee\u9898\u7684\u65b9\u5f0f\uff0c\u662f\u540c\u65f6\u4f7f\u7528\u4e00\u4e2a\u7edd\u5bf9 epsilon (\u50cf\u7b2c\u4e00\u4e2a\u4f8b\u5b50\u90a3\u6837) \u548c\u4e00\u4e2a\u76f8\u5bf9 epsilon (\u50cf Knuth \u7684\u4f8b\u5b50\u90a3\u6837)\uff1a 1 2 3 4 5 6 7 8 9 10 11 // return true if the difference between a and b is less than absEpsilon, or within relEpsilon percent of the larger of a and b bool approximatelyEqualAbsRel ( double a , double b , double absEpsilon , double relEpsilon ) { // Check if the numbers are really close -- needed when comparing numbers near zero. double diff { std :: abs ( a - b ) }; if ( diff <= absEpsilon ) return true ; // Otherwise fall back to Knuth's algorithm return ( diff <= ( std :: max ( std :: abs ( a ), std :: abs ( b )) * relEpsilon )); } \u5728\u8fd9\u4e2a\u7b97\u6cd5\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u68c0\u67e5 a \u548c b \u662f\u5426\u8db3\u591f\u63a5\u8fd1\uff0c\u7528\u4e8e\u5904\u7406 a \u548c b \u90fd\u975e\u5e38\u63a5\u8fd1 0 \u7684\u60c5\u51b5\u3002 absEpsilon \u53c2\u6570\u5e94\u8be5\u88ab\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u975e\u5e38\u5c0f\u7684\u6570\uff08\u4f8b\u5982 1e-12 \uff09\uff0c\u5982\u679c\u8fd9\u4e00\u6b65\u7684\u5224\u65ad\u6ca1\u6709\u6210\u529f\uff0c\u5219\u4f7f\u7528 Knuth \u7684\u7b97\u6cd5\uff0c\u4f7f\u7528\u76f8\u5bf9\u7684 epsilon\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u7528\u4e8e\u5bf9\u4e24\u79cd\u65b9\u6cd5\u8fdb\u884c\u6d4b\u8bd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include #include // return true if the difference between a and b is within epsilon percent of the larger of a and b bool approximatelyEqualRel ( double a , double b , double relEpsilon ) { return ( std :: abs ( a - b ) <= ( std :: max ( std :: abs ( a ), std :: abs ( b )) * relEpsilon )); } bool approximatelyEqualAbsRel ( double a , double b , double absEpsilon , double relEpsilon ) { // Check if the numbers are really close -- needed when comparing numbers near zero. double diff { std :: abs ( a - b ) }; if ( diff <= absEpsilon ) return true ; // Otherwise fall back to Knuth's algorithm return ( diff <= ( std :: max ( std :: abs ( a ), std :: abs ( b )) * relEpsilon )); } int main () { // a is really close to 1.0, but has rounding errors double a { 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 }; std :: cout << approximatelyEqualRel ( a , 1.0 , 1e-8 ) << '\\n' ; // compare \"almost 1.0\" to 1.0 std :: cout << approximatelyEqualRel ( a -1.0 , 0.0 , 1e-8 ) << '\\n' ; // compare \"almost 0.0\" to 0.0 std :: cout << approximatelyEqualAbsRel ( a , 1.0 , 1e-12 , 1e-8 ) << '\\n' ; // compare \"almost 1.0\" to 1.0 std :: cout << approximatelyEqualAbsRel ( a -1.0 , 0.0 , 1e-12 , 1e-8 ) << '\\n' ; // compare \"almost 0.0\" to 0.0 } 1 2 3 4 1 0 1 1 \u53ef\u4ee5\u770b\u5230 approximatelyEqualAbsRel() \u53ef\u4ee5\u6b63\u786e\u5730\u5904\u7406\u6570\u503c\u5f88\u5c0f\u7684\u60c5\u51b5\u3002 \u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\u662f\u4e00\u4e2a\u5f88\u590d\u6742\u7684\u95ee\u9898\uff0c\u800c\u4e14\u6ca1\u6709\u4e07\u80fd\u7684\u7b97\u6cd5\u53ef\u4ee5\u5e94\u5bf9\u6240\u6709\u7684\u60c5\u5f62\u3002\u4e0d\u8fc7\uff0c approximatelyEqualAbsRel() \u914d\u5408 1e-12 \u7684 absEpsilon \u4ee5\u53ca 1e-8 \u7684 relEpsilon \u5df2\u7ecf\u8db3\u591f\u5e94\u5bf9\u5927\u591a\u6570\u60c5\u51b5\u4e86\u3002","title":"\u6bd4\u8f83\u6d6e\u70b9\u6570 \uff08\u6269\u5c55\u9605\u8bfb\uff09"},{"location":"5-7-Logical-operators/","tags":["operator"],"text":"Key Takeaway \u903b\u8f91\u975e\u7684\u4f18\u5148\u7ea7\u975e\u5e38\u9ad8\uff0c\u5982\u679c\u4f7f\u7528\u5b83\u7684\u76ee\u7684\u662f\u5bf9\u6574\u4e2a\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u53d6\u53cd\uff0c\u5219\u8981\u5bf9\u6574\u4e2a\u8868\u8fbe\u5f0f\u52a0\u62ec\u53f7 \u533a\u5206 \u903b\u8f91\u4e0e( && ) \u548c \u6309\u4f4d\u4e0e( & ) \uff0c \u903b\u8f91\u975e( || ) \u548c \u6309\u4f4d\u975e( | ) \u77ed\u8def\u6c42\u503c\u53ef\u80fd\u4f1a\u5bfc\u81f4 \u903b\u8f91\u4e0e \u6216 \u903b\u8f91\u975e \u4e0d\u5bf9\u7b2c\u4e8c\u4e2a\u8868\u8fbe\u5f0f\u6c42\u503c\u3002\u6240\u4ee5\u8bf7\u907f\u514d\u5c06\u5177\u6709\u526f\u4f5c\u7528\u7684\u8fd0\u7b97\u7b26\u548c\u903b\u8f91\u4e0e\u6216\u903b\u8f91\u975e\u4e00\u8d77\u4f7f\u7528\u3002 \u903b\u8f91\u6216\u548c\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u7684\u4e24\u4e2a\u64cd\u4f5c\u6570\uff0c\u6c42\u503c\u987a\u5e8f\u662f\u56fa\u5b9a\u7684\uff0c\u56e0\u4e3a\u6807\u51c6\u91cc\u5bf9\u5176\u8fdb\u884c\u4e86\u8bf4\u660e\uff0c\u5de6\u8fb9\u7684\u64cd\u4f5c\u6570\u603b\u662f\u5148\u6c42\u503c\u3002 \u53ea\u6709 C++ \u81ea\u5e26\u7684\u8fd9\u4e9b\u903b\u8f91\u8fd0\u7b97\u7b26\u4f1a\u6267\u884c\u77ed\u8def\u6c42\u503c\uff0c\u5982\u679c\u4f60\u81ea\u5df1\u91cd\u8f7d\u4e86\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u4ee5\u4fbf\u5c06\u5176\u5e94\u7528\u4e8e\u4f60\u81ea\u5df1\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u8fd9\u4e9b\u91cd\u8f7d\u540e\u7684\u8fd0\u7b97\u7b26\u662f\u4e0d\u5177\u5907\u77ed\u8def\u6c42\u503c\u529f\u80fd\u7684\u3002 \u903b\u8f91\u4e0e\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e\u903b\u8f91\u6216\u7684\u4f18\u5148\u7ea7\uff0c\u5982\u679c\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u91cc\u540c\u65f6\u4f7f\u7528\u903b\u8f91\u4e0e\u548c\u903b\u8f91\u6216\uff0c\u6700\u597d\u4f7f\u7528\u62ec\u53f7\u4e3a\u5176\u660e\u786e\u6307\u5b9a\u4f18\u5148\u7ea7\u3002 \u5fb7\u6469\u6839\u5b9a\u5f8b !(x && y) \u7b49\u4ef7\u4e8e !x || !y !(x || y) \u7b49\u4ef7\u4e8e !x && !y C++ \u6ca1\u6709\u5f02\u6216\u8fd0\u7b97\u7b26\u3002\u53ef\u4ee5\u4f7f\u7528\u4e0d\u7b49\u53f7( != )\u6765\u6a21\u62df\u903b\u8f91\u5f02\u6216\uff08\u76f8\u540c\u4e3a\u771f\uff09 C++ \u4e2d\u7684\u5f88\u591a\u64cd\u4f5c\u7b26 (\u4f8b\u5982 operator || ) \u90fd\u53ef\u4ee5\u4f7f\u7528\u540d\u5b57\u6765\u4ee3\u66ff\u7b26\u53f7\u5417\uff0c\u56e0\u4e3a\u5e76\u4e0d\u662f\u6240\u6709\u7684\u952e\u76d8\u90fd\u652f\u6301\u8f93\u5165\u7b26\u53f7\u3002 \u5c3d\u7ba1\u6761\u4ef6\uff08\u6bd4\u8f83\uff09\u8fd0\u7b97\u7b26\u53ef\u4ee5\u88ab\u7528\u6765\u6d4b\u8bd5\u4e00\u4e2a\u7279\u5b9a\u7684\u8868\u8fbe\u5f0f\u662f\u5426\u4e3a\u771f\uff0c\u4f46\u5b83\u4eec\u4e00\u6b21\u53ea\u80fd\u5bf9\u4e00\u4e2a\u6761\u4ef6\u8fdb\u884c\u6d4b\u8bd5\u3002\u5f88\u591a\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u77e5\u9053\u591a\u4e2a\u6761\u4ef6\u662f\u5426\u540c\u65f6\u4e3a\u771f\u3002\u4f8b\u5982\uff0c\u4e3a\u4e86\u786e\u5b9a\u5f69\u7968\u662f\u5426\u4e2d\u5956\uff0c\u6211\u4eec\u5fc5\u987b\u5c06\u8d2d\u4e70\u7684\u6bcf\u4e00\u4e2a\u6570\u5b57\u548c\u4e2d\u5956\u53f7\u7801\u7684\u5404\u4e2a\u6570\u5b57\u9010\u4e00\u6bd4\u8f83\u3002\u5bf9\u4e8e\u67096\u4e2a\u6570\u7684\u5f69\u7968\uff0c\u8fd9\u5c31\u9700\u89816\u6b21\u6bd4\u8f83\uff0c\u53ea\u6709\u5f53\u5b83\u4eec\u7684\u7ed3\u679c\u5168\u90e8\u90fd\u662f\u771f\u7684\u65f6\u5019\uff0c\u624d\u80fd\u770b\u505a\u4e2d\u5956\u3002\u8fd8\u6709\u4e00\u4e9b\u60c5\u51b5\uff0c\u6211\u4eec\u9700\u8981\u77e5\u9053\u591a\u4e2a\u6761\u4ef6\u4e2d\u662f\u5426\u6709\u4e00\u4e2a\u4e3a\u771f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eca\u5929\u751f\u75c5\u4e86\uff0c\u6216\u8005\u592a\u7d2f\u4e86\uff0c\u53c8\u6216\u8005\u4e2d\u4e86\u5f69\u7968\uff0c\u8fd9\u4e09\u4e2a\u6761\u4ef6\u53ea\u8981\u6709\u4e00\u6761\u4e3a\u771f\u6211\u5c31\u4f1a\u8003\u8651\u7fd8\u73ed\u3002 \u903b\u8f91\u8fd0\u7b97\u7b26\u5c31\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u8fd9\u6837\u7684\u80fd\u529b\u3002 C++ \u63d0\u4f9b\u4e86\u4e09\u79cd\u903b\u8f91\u8fd0\u7b97\u7b26\uff1a \u8fd0\u7b97\u7b26 \u7b26\u53f7 \u5f62\u5f0f \u64cd\u4f5c \u903b\u8f91\u975e NOT ! !x \u5982\u679c x \u4e3a\u771f\u5219\u4e3a false\uff0c\u5982\u679c x \u4e3a\u5047\u5219\u4e3a true \u903b\u8f91\u4e0e AND && x && y \u5982\u679c x \u548c y \u90fd\u662f\u771f\uff0c\u5219\u4e3a true\uff0c\u5426\u5219\u4e3a false \u903b\u8f91\u6216 OR || x || y \u5982\u679c x \u6216 y \u5176\u4e2d\u4e00\u4e2a\u4f4d\u771f\uff0c\u5219\u4e3a\u771f\uff0c\u5426\u5219\u4e3a\u5047 \u903b\u8f91\u975e\uff08NOT\uff09 \u5728 4.9 - \u5e03\u5c14\u503c \u4e00\u8282\u8bfe\u4e2d\u4f60\u5df2\u7ecf\u9047\u5230\u903b\u8f91\u975e\u8fd9\u4e2a\u4e00\u5143\u8fd0\u7b97\u7b26\uff1a \u64cd\u4f5c\u7b26 \u7ed3\u679c true false false true \u5982\u679c\u903b\u8f91\u975e\u7684\u64cd\u4f5c\u6570\u6c42\u503c\u4e3a true \uff0c\u5219\u903b\u8f91\u975e\u6c42\u503c\u4e3a false \u3002\u5982\u679c\u903b\u8f91\u975e\u7684\u64cd\u4f5c\u6570\u6c42\u503c\u4e3a false \uff0c\u903b\u8f91\u975e\u7684\u6c42\u503c\u4e3a true \u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u903b\u8f91\u975e\u53ef\u4ee5\u5c06\u5e03\u5c14\u503c\u53cd\u8f6c\uff0c\u771f\u53d8\u6210\u5047\uff0c\u5047\u53d8\u6210\u771f\u3002 \u903b\u8f91\u975e\u7ecf\u5e38\u88ab\u7528\u5728\u6761\u4ef6\u8868\u8fbe\u5f0f\u4e2d\uff1a 1 2 3 4 5 bool tooLarge { x > 100 }; // tooLarge is true if x > 100 if ( ! tooLarge ) // do something with x else // print an error \u6709\u4ef6\u4e8b\u60c5\u9700\u8981\u6ce8\u610f\uff0c\u90a3\u5c31\u903b\u8f91\u975e\u7684\u4f18\u5148\u7ea7\u975e\u5e38\u9ad8\u3002\u65b0\u624b\u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u72af\u8fd9\u6837\u7684\u9519\u8bef\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 5 }; int y { 7 }; if ( ! x > y ) std :: cout << x << \" is not greater than \" << y << '\\n' ; else std :: cout << x << \" is greater than \" << y << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 5 is greater than 7 \u4f46\u662f x \u5e76\u4e0d\u6bd4 7 \u5927\uff0c\u8fd9\u7ed3\u679c\u662f\u600e\u4e48\u5f97\u5230\u7684\uff1f\u8fd9\u662f\u56e0\u4e3a\u903b\u8f91\u975e\u7684\u4f18\u5148\u7ea7\u6bd4\u5927\u4e8e\u53f7\u7684\u4f18\u5148\u7ea7\u9ad8\uff0c\u56e0\u6b64 ! x > y \u5b9e\u9645\u4e0a\u76f8\u5f53\u4e8e (!x) > y \u3002\u56e0\u4e3a x \u662f 5\uff0c !x \u6c42\u503c\u7ed3\u679c\u4e3a 0 \uff0c\u800c 0 > y \u662f\u5047\uff0c\u6240\u4ee5\u6267\u884c\u7684\u662f else \u7684\u8bed\u53e5\u3002 \u6b63\u786e\u7684\u505a\u6cd5\u5e94\u8be5\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 5 }; int y { 7 }; if ( ! ( x > y )) std :: cout << x << \" is not greater than \" << y << '\\n' ; else std :: cout << x << \" is greater than \" << y << '\\n' ; return 0 ; } \u8fd9\u6837 x > y \u4f1a\u9996\u5148\u8fdb\u884c\u6c42\u503c\uff0c\u7136\u540e\u518d\u901a\u8fc7\u903b\u8f91\u975e\u5c06\u5e03\u5c14\u7ed3\u679c\u53cd\u8f6c\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u903b\u8f91\u975e\u8981\u5bf9\u5176\u4ed6\u8fd0\u7b97\u7b26\u7684\u7ed3\u679c\u8fdb\u884c\u64cd\u4f5c\uff0c\u90a3\u4e48\u5176\u4ed6\u8fd0\u7b97\u7b26\u548c\u5b83\u4eec\u7684\u64cd\u4f5c\u6570\u5e94\u8be5\u653e\u5728\u62ec\u53f7\u4e2d\u3002 \u903b\u8f91\u975e\u7684\u7b80\u5355\u7528\u6cd5\uff0c\u4f8b\u5982 if (!value) \u5e76\u4e0d\u9700\u8981\u62ec\u53f7\uff0c\u56e0\u4e3a\u8fd9\u65f6\u4e0d\u4f1a\u53d7\u5230\u4f18\u5148\u7ea7\u7684\u5f71\u54cd\u3002 \u903b\u8f91\u6216\uff08OR\uff09 \u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u88ab\u7528\u6765\u6d4b\u8bd5\u4e24\u4e2a\u6761\u4ef6\u4e2d\u662f\u5426\u6709\u4e3a\u771f\u7684\u3002\u5982\u679c\u5de6\u64cd\u4f5c\u6570\u6216\u53f3\u64cd\u4f5c\u6570\u4e3a true \uff0c\u6216\u8005\u4e24\u4e2a\u90fd\u662f true \uff0c\u5219\u903b\u8f91\u6216\u8868\u8fbe\u5f0f\u4f1a\u8fd4\u56de true \uff0c\u5426\u5219\u8fd4\u56de false \u3002 \u5de6\u64cd\u4f5c\u6570 \u53f3\u64cd\u4f5c\u6570 \u7ed3\u679c false false false false true true true false true true true true \u4f8b\u5982\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { std :: cout << \"Enter a number: \" ; int value {}; std :: cin >> value ; if ( value == 0 || value == 1 ) std :: cout << \"You picked 0 or 1 \\n \" ; else std :: cout << \"You did not pick 0 or 1 \\n \" ; return 0 ; } \u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f7f\u7528\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u6765\u5224\u65ad\u505a\u64cd\u4f5c\u6570 ( value == 0 ) \u6216\u8005\u53f3\u64cd\u4f5c\u6570 ( value == 1 ) \u662f\u5426\u4e3a\u771f\u3002\u5982\u679c\u5176\u4e00\uff08\u6216\u4e24\u4e2a\uff09\u5168\u90e8\u4e3a\u771f\uff0c\u5219\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u7684\u6c42\u503c\u7ed3\u679c\u4e3a\u771f\uff0cif \u8bed\u53e5\u5c31\u4f1a\u88ab\u6267\u884c\u3002\u5982\u679c\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u662f\u5047\uff0c\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u7684\u7ed3\u679c\u5c31\u662f\u5047\uff0c\u90a3\u4e48 else \u8bed\u53e5\u5c31\u4f1a\u6267\u884c\u3002 \u4f60\u53ef\u4ee5\u5c06\u591a\u4e2a\u903b\u8f91\u6216\u8868\u8fbe\u5f0f\u8fde\u63a5\u8d77\u6765\uff1a 1 2 if ( value == 0 || value == 1 || value == 2 || value == 3 ) std :: cout << \"You picked 0, 1, 2, or 3 \\n \" ; \u65b0\u624b\u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u628a\u903b\u8f91\u6216\u8fd0\u7b97\u7b26 ( || ) \u548c\u6309\u4f4d\u6216\u8fd0\u7b97\u7b26\u641e\u6df7 ( | ) (\u7a0d\u540e\u4f1a\u4ecb\u7ecd)\u3002 \u5373\u4f7f\u5b83\u4eec\u7684\u540d\u5b57\u4e2d\u90fd\u6709\u4e00\u4e2a\u6216\uff0c\u5176\u529f\u80fd\u786e\u662f\u4e0d\u540c\u7684\u3002\u5c06\u5b83\u4eec\u6df7\u4e3a\u4e00\u8c08\u4f1a\u5e26\u6765\u9519\u8bef\u7684\u7ed3\u679c\u3002 \u903b\u8f91\u4e0e\uff08AND) \u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u88ab\u7528\u4f5c\u6d4b\u8bd5\u4e24\u4e2a\u64cd\u4f5c\u6570\u662f\u5426\u5747\u4e3a\u771f\u3002\u5982\u679c\u5747\u4e3a\u771f\uff0c\u5219\u903b\u8f91\u4e0e\u4f1a\u8fd4\u56de true \uff0c\u5426\u5219\u8fd4\u56de\u503c\u4e3a false \u3002 \u5de6\u64cd\u4f5c\u6570 \u53f3\u64cd\u4f5c\u6570 \u7ed3\u679c false false false false true false true false false true true true \u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u8981\u5224\u65ad x \u662f\u5426\u5728 10 \u5230 20 \u4e4b\u95f4\uff1a\u6211\u4eec\u5fc5\u987b\u77e5\u9053 x \u662f\u5426\u5927\u4e8e 10 \uff0c \u5e76\u4e14 \u662f\u5426\u5c0f\u4e8e 20\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { std :: cout << \"Enter a number: \" ; int value {}; std :: cin >> value ; if ( value > 10 && value < 20 ) std :: cout << \"Your value is between 10 and 20 \\n \" ; else std :: cout << \"Your value is not between 10 and 20 \\n \" ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u68c0\u6d4b\u5de6\u4fa7\u7684\u6761\u4ef6 (value > 10) \u548c \u53f3\u4fa7\u7684\u6761\u4ef6 (value < 20) \u662f\u5426\u90fd\u662f true \u3002\u5982\u679c\u90fd\u662f\u771f\uff0c\u5219\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u7684\u6c42\u503c\u7ed3\u679c\u4e3a true \uff0c\u6267\u884c if \u8bed\u53e5\u3002\u5982\u679c\u5de6\u53f3\u4e24\u4e2a\u6761\u4ef6\u90fd\u662f\u5047\uff0c\u5219\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u7684\u6c42\u503c\u7ed3\u679c\u4e3a false \uff0c\u6267\u884c else \u8bed\u53e5\u3002 \u548c\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u4e00\u6837\uff0c\u4f60\u53ef\u4ee5\u628a\u591a\u4e2a\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u8fde\u63a5\u8d77\u6765\uff1a 1 2 3 4 if ( value > 10 && value < 20 && value != 16 ) // do something else // do something else \u5982\u679c\u4e0a\u8ff0\u6240\u6709\u6761\u4ef6\u90fd\u662f\u771f\uff0c\u5219\u6267\u884c if \u8bed\u53e5\u3002\u5982\u679c\u4efb\u4f55\u4e00\u4e2a\u6761\u4ef6\u4e3a\u5047\uff0c\u5219\u6267\u884c else \u8bed\u53e5\u3002 \u65b0\u624b\u7a0b\u5e8f\u5458\u4e0d\u4ec5\u5bb9\u6613\u628a \u903b\u8f91\u6216 \u548c \u6309\u4f4d\u6216 \u641e\u6df7\uff0c\u4e5f\u4f1a\u628a\u903b\u8f91\u4e0e ( && ) \u548c\u6309\u4f4d\u4e0e ( & )\u641e\u6df7\u3002 \u77ed\u8def\u6c42\u503c \u4e3a\u4e86\u8ba9\u903b\u8f91\u4e0e\u8fd4\u56de true \uff0c\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u9700\u8981\u6c42\u503c\u4e3a\u771f\u3002\u5982\u679c\u7b2c\u4e00\u4e2a\u64cd\u4f5c\u6570\u6c42\u503c\u4e3a\u5047\uff0c\u4e0d\u8bba\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u6570\u6c42\u503c\u7ed3\u679c\u5982\u4f55\uff0c\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u4e00\u5b9a\u6c42\u503c\u4e3a false \u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u4f1a\u7acb\u5373\u8fd4\u56de false \uff0c\u800c\u4e0d\u4f1a\u518d\u5bf9\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u6570\u6c42\u503c\u3002\u8fd9\u4e2a\u65b9\u6cd5\u79f0\u4e3a\u77ed\u8def\u6c42\u503c\uff0c\u8fd9\u4e48\u505a\u53ef\u4ee5\u4f18\u5316\u7a0b\u5e8f\u6027\u80fd\u3002 \u7c7b\u4f3c\u5730\uff0c\u5982\u679c\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u7684\u7b2c\u4e00\u4e2a\u64cd\u4f5c\u6570\u6c42\u503c\u7ed3\u679c\u4e3a\u771f\uff0c\u5219\u5b83\u4f1a\u76f4\u63a5\u8fd4\u56de true \uff0c\u800c\u4e0d\u5fc5\u5bf9\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u6570\u8fdb\u884c\u6c42\u503c\u3002 \u77ed\u8def\u6c42\u503c\u8fd8\u5411\u6211\u4eec\u5c55\u793a\u4e86\uff0c\u4e3a\u4ec0\u4e48\u6709\u526f\u4f5c\u7528\u7684\u8fd0\u7b97\u7b26\uff0c\u4e0d\u5e94\u8be5\u653e\u5230\u590d\u5408\u8868\u8fbe\u5f0f\u4e2d\u3002\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 if ( x == 1 && ++ y == 2 ) // do something \u5982\u679c x \u4e0d\u7b49\u4e8e 1\uff0c\u90a3\u4e48\u6574\u4e2a\u6761\u4ef6\u4e00\u5b9a\u4f1a\u8fd4\u56de false \u3002\u6240\u4ee5 ++y \u6839\u672c\u4e0d\u4f1a\u88ab\u6c42\u503c\u3002\u56e0\u6b64\uff0c\u53ea\u6709\u5f53 x \u7b49\u4e8e 1 \u65f6\uff0c y \u624d\u4f1a\u9012\u589e\uff0c\u4e5f\u8bb8\u8fd9\u5e76\u4e0d\u662f\u4f60\u9700\u8981\u7684\uff01 \u6ce8\u610f \u77ed\u8def\u6c42\u503c\u53ef\u80fd\u4f1a\u5bfc\u81f4 \u903b\u8f91\u4e0e \u6216 \u903b\u8f91\u975e \u4e0d\u5bf9\u7b2c\u4e8c\u4e2a\u8868\u8fbe\u5f0f\u6c42\u503c\u3002\u6240\u4ee5\u8bf7\u907f\u514d\u5c06\u5177\u6709\u526f\u4f5c\u7528\u7684\u8fd0\u7b97\u7b26\u548c\u903b\u8f91\u4e0e\u6216\u903b\u8f91\u975e\u4e00\u8d77\u4f7f\u7528\u3002 \u5173\u952e\u4fe1\u606f \u903b\u8f91\u6216\u548c\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u7684\u4e24\u4e2a\u64cd\u4f5c\u6570\uff0c\u6c42\u503c\u987a\u5e8f\u662f\u56fa\u5b9a\u7684\uff0c\u56e0\u4e3a\u6807\u51c6\u91cc\u5bf9\u5176\u8fdb\u884c\u4e86\u8bf4\u660e\uff0c\u5de6\u8fb9\u7684\u64cd\u4f5c\u6570\u603b\u662f\u5148\u6c42\u503c\u3002\uff08\u53c2\u8003\uff1a 5-1-Operator-precedence-and-associativity#\u8868\u8fbe\u5f0f\u6c42\u503c\u987a\u5e8f\u548c\u51fd\u6570\u53c2\u6570\u5904\u7406\u987a\u5e8f\u5927\u591a\u662f\u672a\u6307\u660e\u7684 \uff09 \u6269\u5c55\u9605\u8bfb \u53ea\u6709 C++ \u81ea\u5e26\u7684\u8fd9\u4e9b\u903b\u8f91\u8fd0\u7b97\u7b26\u4f1a\u6267\u884c\u77ed\u8def\u6c42\u503c\uff0c\u5982\u679c\u4f60\u81ea\u5df1\u91cd\u8f7d\u4e86\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u4ee5\u4fbf\u5c06\u5176\u5e94\u7528\u4e8e\u4f60\u81ea\u5df1\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u8fd9\u4e9b\u91cd\u8f7d\u540e\u7684\u8fd0\u7b97\u7b26\u662f\u4e0d\u5177\u5907\u77ed\u8def\u6c42\u503c\u529f\u80fd\u7684\u3002 \u6df7\u5408 AND \u548c OR \u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u91cc\u9762\u6df7\u5408\u4f7f\u7528\u903b\u8f91\u4e0e\u548c\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u901a\u5e38\u662f\u65e0\u6cd5\u907f\u514d\u7684\uff0c\u4f46\u662f\u8fd9\u6837\u7684\u7528\u6cd5\u4e5f\u5b58\u5728\u4e00\u5b9a\u7684\u5371\u9669\u3002 \u5f88\u591a\u7a0b\u5e8f\u5458\u4f1a\u5047\u5b9a\u903b\u8f91\u4e0e\u548c\u903b\u8f91\u6216\u5177\u6709\u76f8\u540c\u7684\u4f18\u5148\u7ea7\uff08\u6216\u5fd8\u8bb0\u4e86\u5b83\u4eec\u7684\u4f18\u5148\u7ea7\u662f\u4e0d\u540c\u7684\uff09\uff0c\u5c31\u50cf\u52a0/\u51cf\u6216\u8005\u4e58/\u9664\u90a3\u6837\u3002\u4f46\u662f\uff0c\u903b\u8f91\u4e0e\u7684\u4f18\u5148\u7ea7\u5176\u5b9e\u6bd4\u903b\u8f91\u6216\u8981\u9ad8\uff0c\u56e0\u6b64\u903b\u8f91\u4e0e\u4f1a\u5148\u4e8e\u903b\u8f91\u6216\u6c42\u503c\uff08\u9664\u975e\u6709\u62ec\u53f7\u4fee\u6539\u5176\u4f18\u5148\u7ea7\uff09\u3002 \u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u5199\u51fa\u8fd9\u6837\u7684\u8868\u8fbe\u5f0f\uff1a value1 || value2 && value3 \u3002\u56e0\u4e3a\u903b\u8f91\u4e0e\u7684\u4f18\u5148\u7ea7\u66f4\u9ad8\uff0c\u6240\u4ee5\u4e0a\u8ff0\u8868\u8fbe\u5f0f\u7b49\u4ef7\u4e8e value1 || (value2 && value3) \u800c\u4e0d\u662f (value1 || value2) && value3 \u3002\u5982\u679c\u8fd9\u6b63\u662f\u4f60\u60f3\u8981\u7684\uff0c\u90a3\u5c31\u597d\u4e86\u3002\u4f46\u662f\u5982\u679c\u4f60\u5047\u8bbe\u4e0a\u9762\u7684\u8868\u8fbe\u5f0f\u662f\u6309\u7167\u4ece\u5de6\u5411\u53f3\u7684\u987a\u5e8f\u6c42\u503c\uff0c\u90a3\u5f97\u5230\u7684\u7ed3\u679c\u80af\u5b9a\u51fa\u4f60\u6240\u6599\uff01 \u5982\u679c\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u91cc\u540c\u65f6\u4f7f\u7528\u903b\u8f91\u4e0e\u548c\u903b\u8f91\u6216\uff0c\u6700\u597d\u4f7f\u7528\u62ec\u53f7\u4e3a\u5176\u660e\u786e\u6307\u5b9a\u4f18\u5148\u7ea7\u3002\u8fd9\u6837\u4e0d\u4ec5\u53ef\u4ee5\u907f\u514d\u7531\u4f18\u5148\u7ea7\u5f15\u8d77\u7684\u987a\u5e8f\uff0c\u8fd8\u80fd\u591f\u589e\u5f3a\u53ef\u8bfb\u6027\u5e76\u6e05\u6670\u5730\u8868\u660e\u4f60\u7684\u76ee\u7684\u3002\u4f8b\u5982\uff0c value1 && value2 || value3 && value4 \u6700\u597d\u6539\u5199\u4e3a (value1 && value2) || (value3 && value4) . \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u91cc\u540c\u65f6\u4f7f\u7528\u903b\u8f91\u4e0e\u548c\u903b\u8f91\u6216\uff0c\u6700\u597d\u4f7f\u7528\u62ec\u53f7\u4e3a\u5176\u660e\u786e\u6307\u5b9a\u4f18\u5148\u7ea7\u3002 \u5fb7\u6469\u6839\u5b9a\u5f8b\uff08De Morgan\u2018s law\uff09 \u5f88\u591a\u7a0b\u5e8f\u5458\u4f1a\u8ba4\u4e3a !(x && y) \u7b49\u4ef7\u4e8e !x && !y \u3002\u4e0d\u5e78\u7684\u662f\uff0c\u903b\u8f91\u975e\u8fd0\u7b97\u7b26\u5e76\u4e0d\u80fd\u50cf\u8fd9\u6837\u8fdb\u884c\u5206\u914d\u3002 \u6839\u636e \u5fb7\u6469\u6839\u5b9a\u5f8b \uff0c\u903b\u8f91\u975e\u7684\u5206\u914d\u5f8b\u5e94\u8be5\u662f\u4e0b\u9762\u8fd9\u79cd\u65b9\u5f0f\uff1a !(x && y) \u7b49\u4ef7\u4e8e !x || !y !(x || y) \u7b49\u4ef7\u4e8e !x && !y \u6362\u8a00\u4e4b\uff0c\u5f53\u4f60\u5206\u914d\u903b\u8f91\u975e\u8fd0\u7b97\u7b26\u65f6\uff0c\u5fc5\u987b\u5c06\u903b\u8f91\u4e0e\u66ff\u6362\u4e3a\u903b\u8f91\u6216\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002 \u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5fb7\u6469\u6839\u5b9a\u5f8b\u5bf9\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u8fdb\u884c\u7b80\u5316\u3002 \u6269\u5c55\u9605\u8bfb \u6211\u4eec\u53ef\u4ee5\u8bc1\u660e\u5fb7\u6469\u6839\u5b9a\u5f8b\u7684\u7b2c\u4e00\u90e8\u5206\u662f\u6b63\u786e\u7684\uff0c\u5373\u5bf9\u4e8e\u4efb\u4f55\u53ef\u80fd\u7684 x \u548c y \uff0c !(x && y) \u7b49\u4e8e !x || !y \u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u79f0\u4e3a \u771f\u503c\u8868 \u7684\u6570\u5b66\u6982\u5ff5\u3002 x y !x !y !(x && y) !x || !y false false true true true true false true true false true true true false false true true true true true false false false false \u5728\u8868\u4e2d\uff0c\u7b2c\u4e00\u5217\u548c\u7b2c\u4e8c\u5217\u5206\u522b\u8868\u793a x \u548c y \u3002\u6bcf\u4e00\u884c\u5219\u8868\u793a\u4e00\u7ec4\u53ef\u80fd\u7684 x \u548c y \u7ec4\u5408\uff0c4\u884c\u5c31\u8db3\u4ee5\u8868\u793a\u6240\u6709\u7684\u53ef\u80fd\u3002 \u5269\u4e0b\u7684\u5217\u8868\u793a\u57fa\u4e8e\u521d\u59cb x \u548c y \u6784\u9020\u7684\u8868\u8fbe\u5f0f\u3002\u7b2c\u4e09\u5217\u548c\u7b2c\u56db\u5217\u5206\u522b\u8ba1\u7b97 !x \u548c !y \u3002\u7b2c\u4e94\u5217\u8ba1\u7b97 !(x && y) \u3002\u7b2c\u516d\u5217\u5219\u8ba1\u7b97 !x || !y \u3002 \u53ef\u4ee5\u6ce8\u610f\u5230\uff0c\u7b2c\u4e94\u5217\u548c\u7b2c\u516d\u5217\u7684\u503c\u603b\u662f\u76f8\u540c\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u4e8e\u4efb\u610f x \u548c y \uff0c !(x && y) \u7b49\u4e8e !x || !y \uff0c\u8fd9\u4e5f\u6b63\u662f\u6211\u4eec\u8981\u8bc1\u660e\u7684\u3002 \u540c\u6837\u7684\uff0c\u6211\u4eec\u8fd8\u80fd\u591f\u8bc1\u660e\u5fb7\u6469\u6839\u5b9a\u5f8b\u7684\u7b2c\u4e8c\u90e8\u5206\u4e5f\u662f\u6b63\u786e\u7684\uff1a x y !x !y !(x || y) !x && !y false false true true true true false true true false false false true false false true false false true true false false false false \u540c\u6837\u7684\uff0c\u5bf9\u4e8e\u4efb\u610f x \u548c y \uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230 !(x || y) \u7b49\u4e8e !x && !y \u3002 \u903b\u8f91\u5f02\u6216\u8fd0\u7b97\u7b26\u5728\u54ea\u91cc\uff1f \u6709\u4e9b\u8bed\u8a00\u63d0\u4f9b\u4e86\u903b\u8f91\u5f02\u6216\uff08XOR\uff09\u8fd0\u7b97\u7b26\uff0c\u7528\u6765\u6d4b\u8bd5 \u662f\u5426\u6709\u5947\u6570\u4e2a\u6761\u4ef6\u4e3a\u771f \u3002 \u5de6\u64cd\u4f5c\u6570 \u53f3\u64cd\u4f5c\u6570 \u7ed3\u679c false false false false true true true false true true true false C++ \u5e76\u6ca1\u6709\u63d0\u4f9b\u903b\u8f91\u5f02\u6216\u8fd0\u7b97\u7b26\u3002\u548c \u903b\u8f91\u6216 \u6216\u8005 \u903b\u8f91\u4e0e \u4e0d\u540c\uff0c \u903b\u8f91\u5f02\u6216 \u5e76\u6ca1\u6709\u77ed\u8def\u6c42\u503c\u7279\u6027\u3002\u56e0\u6b64\uff0c\u4f7f\u7528\u903b\u8f91\u6216\u6216\u8005\u903b\u8f91\u4e0e\u6765\u6784\u5efa\u4e00\u4e2a\u903b\u8f91\u5f02\u6216\u662f\u5f88\u56f0\u96be\u7684\u3002\u4f46\u662f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0d\u7b49\u53f7( != )\u6765\u6a21\u62df\u903b\u8f91\u5f02\u6216\uff1a 1 if ( a != b ) ... // a XOR b, \u5047\u8bbe a \u548c b \u90fd\u662f\u5e03\u5c14\u503c \u4e5f\u53ef\u4ee5\u5bf9\u591a\u4e2a\u64cd\u4f5c\u6570\u6765\u4f7f\u7528\uff1a 1 if ( a != b != c != d ) ... // a XOR b XOR c XOR d, \u5047\u8bbe a, b, c, \u548c d \u90fd\u662f\u5e03\u5c14\u503c \u6ce8\u610f\uff0c\u4e0a\u9762\u7684\u65b9\u6cd5\u53ea\u6709\u5f53\u64cd\u4f5c\u6570\u90fd\u662f\u5e03\u5c14\u7c7b\u578b\u65f6\u624d\u6709\u6548 (\u4e0d\u80fd\u662f\u6574\u578b)\u3002\u5982\u679c\u4f60\u60f3\u5c06\u903b\u8f91\u5f02\u6216\u5e94\u7528\u4e8e\u975e\u5e03\u5c14\u7c7b\u578b\u7684\u64cd\u4f5c\u6570\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06\u5b83\u8f6c\u6362\u4e3a\u5e03\u5c14\u7c7b\u578b\u3002 1 if ( static_cast < bool > ( a ) != static_cast < bool > ( b ) != static_cast < bool > ( c ) != static_cast < bool > ( d )) ... // a XOR b XOR c XOR d, for any type that can be converted to bool \u8fd0\u7b97\u7b26\u7684\u5176\u4ed6\u8868\u793a\u5f62\u5f0f C++ \u4e2d\u7684\u5f88\u591a\u64cd\u4f5c\u7b26 (\u4f8b\u5982 operator || ) \u90fd\u6709\u53ef\u4ee5\u4ee3\u66ff\u7b26\u53f7\u8868\u793a\u6cd5\u7684\u5173\u952e\u5b57\u3002\u51fa\u4e8e\u5386\u53f2\u539f\u56e0\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u7684\u952e\u76d8\u6216\u8005\u6807\u51c6\u652f\u6301\u8f93\u5165\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u7684\u7b26\u53f7\u3002\u56e0\u6b64C++\u63d0\u4f9b\u4e86\u4e00\u7ec4\u5173\u952e\u5b57\u7528\u4e8e\u4ee3\u66ff\u7b26\u53f7\u3002\u4f8b\u5982\uff0c || \u4e5f\u53ef\u4ee5\u7528 or \u6765\u66ff\u4ee3\u3002 \u5b8c\u6574\u7684\u5217\u8868\u53ef\u4ee5\u53c2\u8003 \u8fd9\u91cc \uff0c\u5c24\u5176\u53ef\u4ee5\u6ce8\u610f\u4e00\u4e0b\u4e0b\u9762\u4e09\u79cd\uff1a \u8fd0\u7b97\u7b26 \u5173\u952e\u5b57\u66ff\u4ee3\u540d && and || or ! not \u56e0\u6b64\u4e0b\u9762\u4e24\u884c\u4ee3\u7801\u662f\u7b49\u4ef7\u7684\uff1a 1 2 std :: cout << ! a && ( b || c ); std :: cout << not a and ( b or c ); \u5c3d\u7ba1\u8fd9\u4e9b\u5173\u952e\u5b57\u770b\u8d77\u6765\u53ef\u8bfb\u6027\u66f4\u597d\uff0c\u4f46\u662f\u5927\u591a\u6570\u6709\u7ecf\u9a8c\u7684 C++ \u7a0b\u5e8f\u5458\u8fd8\u662f\u559c\u6b22\u4f7f\u7528\u7b26\u53f7\u800c\u4e0d\u662f\u5173\u952e\u5b57\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u8fd8\u662f\u5efa\u8bae\u4f60\u5b66\u4e60\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u7684\u7b26\u53f7\u8868\u793a\u6cd5\uff0c\u8fd9\u4e5f\u662f\u4ee3\u7801\u4e2d\u6700\u5e38\u89c1\u7684\u5f62\u5f0f\u3002","title":"5.7 - \u903b\u8f91\u8fd0\u7b97\u7b26"},{"location":"5-7-Logical-operators/#not","text":"\u5728 4.9 - \u5e03\u5c14\u503c \u4e00\u8282\u8bfe\u4e2d\u4f60\u5df2\u7ecf\u9047\u5230\u903b\u8f91\u975e\u8fd9\u4e2a\u4e00\u5143\u8fd0\u7b97\u7b26\uff1a \u64cd\u4f5c\u7b26 \u7ed3\u679c true false false true \u5982\u679c\u903b\u8f91\u975e\u7684\u64cd\u4f5c\u6570\u6c42\u503c\u4e3a true \uff0c\u5219\u903b\u8f91\u975e\u6c42\u503c\u4e3a false \u3002\u5982\u679c\u903b\u8f91\u975e\u7684\u64cd\u4f5c\u6570\u6c42\u503c\u4e3a false \uff0c\u903b\u8f91\u975e\u7684\u6c42\u503c\u4e3a true \u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u903b\u8f91\u975e\u53ef\u4ee5\u5c06\u5e03\u5c14\u503c\u53cd\u8f6c\uff0c\u771f\u53d8\u6210\u5047\uff0c\u5047\u53d8\u6210\u771f\u3002 \u903b\u8f91\u975e\u7ecf\u5e38\u88ab\u7528\u5728\u6761\u4ef6\u8868\u8fbe\u5f0f\u4e2d\uff1a 1 2 3 4 5 bool tooLarge { x > 100 }; // tooLarge is true if x > 100 if ( ! tooLarge ) // do something with x else // print an error \u6709\u4ef6\u4e8b\u60c5\u9700\u8981\u6ce8\u610f\uff0c\u90a3\u5c31\u903b\u8f91\u975e\u7684\u4f18\u5148\u7ea7\u975e\u5e38\u9ad8\u3002\u65b0\u624b\u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u72af\u8fd9\u6837\u7684\u9519\u8bef\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 5 }; int y { 7 }; if ( ! x > y ) std :: cout << x << \" is not greater than \" << y << '\\n' ; else std :: cout << x << \" is greater than \" << y << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 5 is greater than 7 \u4f46\u662f x \u5e76\u4e0d\u6bd4 7 \u5927\uff0c\u8fd9\u7ed3\u679c\u662f\u600e\u4e48\u5f97\u5230\u7684\uff1f\u8fd9\u662f\u56e0\u4e3a\u903b\u8f91\u975e\u7684\u4f18\u5148\u7ea7\u6bd4\u5927\u4e8e\u53f7\u7684\u4f18\u5148\u7ea7\u9ad8\uff0c\u56e0\u6b64 ! x > y \u5b9e\u9645\u4e0a\u76f8\u5f53\u4e8e (!x) > y \u3002\u56e0\u4e3a x \u662f 5\uff0c !x \u6c42\u503c\u7ed3\u679c\u4e3a 0 \uff0c\u800c 0 > y \u662f\u5047\uff0c\u6240\u4ee5\u6267\u884c\u7684\u662f else \u7684\u8bed\u53e5\u3002 \u6b63\u786e\u7684\u505a\u6cd5\u5e94\u8be5\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 5 }; int y { 7 }; if ( ! ( x > y )) std :: cout << x << \" is not greater than \" << y << '\\n' ; else std :: cout << x << \" is greater than \" << y << '\\n' ; return 0 ; } \u8fd9\u6837 x > y \u4f1a\u9996\u5148\u8fdb\u884c\u6c42\u503c\uff0c\u7136\u540e\u518d\u901a\u8fc7\u903b\u8f91\u975e\u5c06\u5e03\u5c14\u7ed3\u679c\u53cd\u8f6c\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u903b\u8f91\u975e\u8981\u5bf9\u5176\u4ed6\u8fd0\u7b97\u7b26\u7684\u7ed3\u679c\u8fdb\u884c\u64cd\u4f5c\uff0c\u90a3\u4e48\u5176\u4ed6\u8fd0\u7b97\u7b26\u548c\u5b83\u4eec\u7684\u64cd\u4f5c\u6570\u5e94\u8be5\u653e\u5728\u62ec\u53f7\u4e2d\u3002 \u903b\u8f91\u975e\u7684\u7b80\u5355\u7528\u6cd5\uff0c\u4f8b\u5982 if (!value) \u5e76\u4e0d\u9700\u8981\u62ec\u53f7\uff0c\u56e0\u4e3a\u8fd9\u65f6\u4e0d\u4f1a\u53d7\u5230\u4f18\u5148\u7ea7\u7684\u5f71\u54cd\u3002","title":"\u903b\u8f91\u975e\uff08NOT\uff09"},{"location":"5-7-Logical-operators/#or","text":"\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u88ab\u7528\u6765\u6d4b\u8bd5\u4e24\u4e2a\u6761\u4ef6\u4e2d\u662f\u5426\u6709\u4e3a\u771f\u7684\u3002\u5982\u679c\u5de6\u64cd\u4f5c\u6570\u6216\u53f3\u64cd\u4f5c\u6570\u4e3a true \uff0c\u6216\u8005\u4e24\u4e2a\u90fd\u662f true \uff0c\u5219\u903b\u8f91\u6216\u8868\u8fbe\u5f0f\u4f1a\u8fd4\u56de true \uff0c\u5426\u5219\u8fd4\u56de false \u3002 \u5de6\u64cd\u4f5c\u6570 \u53f3\u64cd\u4f5c\u6570 \u7ed3\u679c false false false false true true true false true true true true \u4f8b\u5982\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { std :: cout << \"Enter a number: \" ; int value {}; std :: cin >> value ; if ( value == 0 || value == 1 ) std :: cout << \"You picked 0 or 1 \\n \" ; else std :: cout << \"You did not pick 0 or 1 \\n \" ; return 0 ; } \u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f7f\u7528\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u6765\u5224\u65ad\u505a\u64cd\u4f5c\u6570 ( value == 0 ) \u6216\u8005\u53f3\u64cd\u4f5c\u6570 ( value == 1 ) \u662f\u5426\u4e3a\u771f\u3002\u5982\u679c\u5176\u4e00\uff08\u6216\u4e24\u4e2a\uff09\u5168\u90e8\u4e3a\u771f\uff0c\u5219\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u7684\u6c42\u503c\u7ed3\u679c\u4e3a\u771f\uff0cif \u8bed\u53e5\u5c31\u4f1a\u88ab\u6267\u884c\u3002\u5982\u679c\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u662f\u5047\uff0c\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u7684\u7ed3\u679c\u5c31\u662f\u5047\uff0c\u90a3\u4e48 else \u8bed\u53e5\u5c31\u4f1a\u6267\u884c\u3002 \u4f60\u53ef\u4ee5\u5c06\u591a\u4e2a\u903b\u8f91\u6216\u8868\u8fbe\u5f0f\u8fde\u63a5\u8d77\u6765\uff1a 1 2 if ( value == 0 || value == 1 || value == 2 || value == 3 ) std :: cout << \"You picked 0, 1, 2, or 3 \\n \" ; \u65b0\u624b\u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u628a\u903b\u8f91\u6216\u8fd0\u7b97\u7b26 ( || ) \u548c\u6309\u4f4d\u6216\u8fd0\u7b97\u7b26\u641e\u6df7 ( | ) (\u7a0d\u540e\u4f1a\u4ecb\u7ecd)\u3002 \u5373\u4f7f\u5b83\u4eec\u7684\u540d\u5b57\u4e2d\u90fd\u6709\u4e00\u4e2a\u6216\uff0c\u5176\u529f\u80fd\u786e\u662f\u4e0d\u540c\u7684\u3002\u5c06\u5b83\u4eec\u6df7\u4e3a\u4e00\u8c08\u4f1a\u5e26\u6765\u9519\u8bef\u7684\u7ed3\u679c\u3002","title":"\u903b\u8f91\u6216\uff08OR\uff09"},{"location":"5-7-Logical-operators/#and","text":"\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u88ab\u7528\u4f5c\u6d4b\u8bd5\u4e24\u4e2a\u64cd\u4f5c\u6570\u662f\u5426\u5747\u4e3a\u771f\u3002\u5982\u679c\u5747\u4e3a\u771f\uff0c\u5219\u903b\u8f91\u4e0e\u4f1a\u8fd4\u56de true \uff0c\u5426\u5219\u8fd4\u56de\u503c\u4e3a false \u3002 \u5de6\u64cd\u4f5c\u6570 \u53f3\u64cd\u4f5c\u6570 \u7ed3\u679c false false false false true false true false false true true true \u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u8981\u5224\u65ad x \u662f\u5426\u5728 10 \u5230 20 \u4e4b\u95f4\uff1a\u6211\u4eec\u5fc5\u987b\u77e5\u9053 x \u662f\u5426\u5927\u4e8e 10 \uff0c \u5e76\u4e14 \u662f\u5426\u5c0f\u4e8e 20\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { std :: cout << \"Enter a number: \" ; int value {}; std :: cin >> value ; if ( value > 10 && value < 20 ) std :: cout << \"Your value is between 10 and 20 \\n \" ; else std :: cout << \"Your value is not between 10 and 20 \\n \" ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u68c0\u6d4b\u5de6\u4fa7\u7684\u6761\u4ef6 (value > 10) \u548c \u53f3\u4fa7\u7684\u6761\u4ef6 (value < 20) \u662f\u5426\u90fd\u662f true \u3002\u5982\u679c\u90fd\u662f\u771f\uff0c\u5219\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u7684\u6c42\u503c\u7ed3\u679c\u4e3a true \uff0c\u6267\u884c if \u8bed\u53e5\u3002\u5982\u679c\u5de6\u53f3\u4e24\u4e2a\u6761\u4ef6\u90fd\u662f\u5047\uff0c\u5219\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u7684\u6c42\u503c\u7ed3\u679c\u4e3a false \uff0c\u6267\u884c else \u8bed\u53e5\u3002 \u548c\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u4e00\u6837\uff0c\u4f60\u53ef\u4ee5\u628a\u591a\u4e2a\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u8fde\u63a5\u8d77\u6765\uff1a 1 2 3 4 if ( value > 10 && value < 20 && value != 16 ) // do something else // do something else \u5982\u679c\u4e0a\u8ff0\u6240\u6709\u6761\u4ef6\u90fd\u662f\u771f\uff0c\u5219\u6267\u884c if \u8bed\u53e5\u3002\u5982\u679c\u4efb\u4f55\u4e00\u4e2a\u6761\u4ef6\u4e3a\u5047\uff0c\u5219\u6267\u884c else \u8bed\u53e5\u3002 \u65b0\u624b\u7a0b\u5e8f\u5458\u4e0d\u4ec5\u5bb9\u6613\u628a \u903b\u8f91\u6216 \u548c \u6309\u4f4d\u6216 \u641e\u6df7\uff0c\u4e5f\u4f1a\u628a\u903b\u8f91\u4e0e ( && ) \u548c\u6309\u4f4d\u4e0e ( & )\u641e\u6df7\u3002","title":"\u903b\u8f91\u4e0e\uff08AND)"},{"location":"5-7-Logical-operators/#_1","text":"\u4e3a\u4e86\u8ba9\u903b\u8f91\u4e0e\u8fd4\u56de true \uff0c\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u9700\u8981\u6c42\u503c\u4e3a\u771f\u3002\u5982\u679c\u7b2c\u4e00\u4e2a\u64cd\u4f5c\u6570\u6c42\u503c\u4e3a\u5047\uff0c\u4e0d\u8bba\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u6570\u6c42\u503c\u7ed3\u679c\u5982\u4f55\uff0c\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u4e00\u5b9a\u6c42\u503c\u4e3a false \u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u4f1a\u7acb\u5373\u8fd4\u56de false \uff0c\u800c\u4e0d\u4f1a\u518d\u5bf9\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u6570\u6c42\u503c\u3002\u8fd9\u4e2a\u65b9\u6cd5\u79f0\u4e3a\u77ed\u8def\u6c42\u503c\uff0c\u8fd9\u4e48\u505a\u53ef\u4ee5\u4f18\u5316\u7a0b\u5e8f\u6027\u80fd\u3002 \u7c7b\u4f3c\u5730\uff0c\u5982\u679c\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u7684\u7b2c\u4e00\u4e2a\u64cd\u4f5c\u6570\u6c42\u503c\u7ed3\u679c\u4e3a\u771f\uff0c\u5219\u5b83\u4f1a\u76f4\u63a5\u8fd4\u56de true \uff0c\u800c\u4e0d\u5fc5\u5bf9\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u6570\u8fdb\u884c\u6c42\u503c\u3002 \u77ed\u8def\u6c42\u503c\u8fd8\u5411\u6211\u4eec\u5c55\u793a\u4e86\uff0c\u4e3a\u4ec0\u4e48\u6709\u526f\u4f5c\u7528\u7684\u8fd0\u7b97\u7b26\uff0c\u4e0d\u5e94\u8be5\u653e\u5230\u590d\u5408\u8868\u8fbe\u5f0f\u4e2d\u3002\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 if ( x == 1 && ++ y == 2 ) // do something \u5982\u679c x \u4e0d\u7b49\u4e8e 1\uff0c\u90a3\u4e48\u6574\u4e2a\u6761\u4ef6\u4e00\u5b9a\u4f1a\u8fd4\u56de false \u3002\u6240\u4ee5 ++y \u6839\u672c\u4e0d\u4f1a\u88ab\u6c42\u503c\u3002\u56e0\u6b64\uff0c\u53ea\u6709\u5f53 x \u7b49\u4e8e 1 \u65f6\uff0c y \u624d\u4f1a\u9012\u589e\uff0c\u4e5f\u8bb8\u8fd9\u5e76\u4e0d\u662f\u4f60\u9700\u8981\u7684\uff01 \u6ce8\u610f \u77ed\u8def\u6c42\u503c\u53ef\u80fd\u4f1a\u5bfc\u81f4 \u903b\u8f91\u4e0e \u6216 \u903b\u8f91\u975e \u4e0d\u5bf9\u7b2c\u4e8c\u4e2a\u8868\u8fbe\u5f0f\u6c42\u503c\u3002\u6240\u4ee5\u8bf7\u907f\u514d\u5c06\u5177\u6709\u526f\u4f5c\u7528\u7684\u8fd0\u7b97\u7b26\u548c\u903b\u8f91\u4e0e\u6216\u903b\u8f91\u975e\u4e00\u8d77\u4f7f\u7528\u3002 \u5173\u952e\u4fe1\u606f \u903b\u8f91\u6216\u548c\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u7684\u4e24\u4e2a\u64cd\u4f5c\u6570\uff0c\u6c42\u503c\u987a\u5e8f\u662f\u56fa\u5b9a\u7684\uff0c\u56e0\u4e3a\u6807\u51c6\u91cc\u5bf9\u5176\u8fdb\u884c\u4e86\u8bf4\u660e\uff0c\u5de6\u8fb9\u7684\u64cd\u4f5c\u6570\u603b\u662f\u5148\u6c42\u503c\u3002\uff08\u53c2\u8003\uff1a 5-1-Operator-precedence-and-associativity#\u8868\u8fbe\u5f0f\u6c42\u503c\u987a\u5e8f\u548c\u51fd\u6570\u53c2\u6570\u5904\u7406\u987a\u5e8f\u5927\u591a\u662f\u672a\u6307\u660e\u7684 \uff09 \u6269\u5c55\u9605\u8bfb \u53ea\u6709 C++ \u81ea\u5e26\u7684\u8fd9\u4e9b\u903b\u8f91\u8fd0\u7b97\u7b26\u4f1a\u6267\u884c\u77ed\u8def\u6c42\u503c\uff0c\u5982\u679c\u4f60\u81ea\u5df1\u91cd\u8f7d\u4e86\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u4ee5\u4fbf\u5c06\u5176\u5e94\u7528\u4e8e\u4f60\u81ea\u5df1\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u8fd9\u4e9b\u91cd\u8f7d\u540e\u7684\u8fd0\u7b97\u7b26\u662f\u4e0d\u5177\u5907\u77ed\u8def\u6c42\u503c\u529f\u80fd\u7684\u3002","title":"\u77ed\u8def\u6c42\u503c"},{"location":"5-7-Logical-operators/#and-or","text":"\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u91cc\u9762\u6df7\u5408\u4f7f\u7528\u903b\u8f91\u4e0e\u548c\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u901a\u5e38\u662f\u65e0\u6cd5\u907f\u514d\u7684\uff0c\u4f46\u662f\u8fd9\u6837\u7684\u7528\u6cd5\u4e5f\u5b58\u5728\u4e00\u5b9a\u7684\u5371\u9669\u3002 \u5f88\u591a\u7a0b\u5e8f\u5458\u4f1a\u5047\u5b9a\u903b\u8f91\u4e0e\u548c\u903b\u8f91\u6216\u5177\u6709\u76f8\u540c\u7684\u4f18\u5148\u7ea7\uff08\u6216\u5fd8\u8bb0\u4e86\u5b83\u4eec\u7684\u4f18\u5148\u7ea7\u662f\u4e0d\u540c\u7684\uff09\uff0c\u5c31\u50cf\u52a0/\u51cf\u6216\u8005\u4e58/\u9664\u90a3\u6837\u3002\u4f46\u662f\uff0c\u903b\u8f91\u4e0e\u7684\u4f18\u5148\u7ea7\u5176\u5b9e\u6bd4\u903b\u8f91\u6216\u8981\u9ad8\uff0c\u56e0\u6b64\u903b\u8f91\u4e0e\u4f1a\u5148\u4e8e\u903b\u8f91\u6216\u6c42\u503c\uff08\u9664\u975e\u6709\u62ec\u53f7\u4fee\u6539\u5176\u4f18\u5148\u7ea7\uff09\u3002 \u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u5199\u51fa\u8fd9\u6837\u7684\u8868\u8fbe\u5f0f\uff1a value1 || value2 && value3 \u3002\u56e0\u4e3a\u903b\u8f91\u4e0e\u7684\u4f18\u5148\u7ea7\u66f4\u9ad8\uff0c\u6240\u4ee5\u4e0a\u8ff0\u8868\u8fbe\u5f0f\u7b49\u4ef7\u4e8e value1 || (value2 && value3) \u800c\u4e0d\u662f (value1 || value2) && value3 \u3002\u5982\u679c\u8fd9\u6b63\u662f\u4f60\u60f3\u8981\u7684\uff0c\u90a3\u5c31\u597d\u4e86\u3002\u4f46\u662f\u5982\u679c\u4f60\u5047\u8bbe\u4e0a\u9762\u7684\u8868\u8fbe\u5f0f\u662f\u6309\u7167\u4ece\u5de6\u5411\u53f3\u7684\u987a\u5e8f\u6c42\u503c\uff0c\u90a3\u5f97\u5230\u7684\u7ed3\u679c\u80af\u5b9a\u51fa\u4f60\u6240\u6599\uff01 \u5982\u679c\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u91cc\u540c\u65f6\u4f7f\u7528\u903b\u8f91\u4e0e\u548c\u903b\u8f91\u6216\uff0c\u6700\u597d\u4f7f\u7528\u62ec\u53f7\u4e3a\u5176\u660e\u786e\u6307\u5b9a\u4f18\u5148\u7ea7\u3002\u8fd9\u6837\u4e0d\u4ec5\u53ef\u4ee5\u907f\u514d\u7531\u4f18\u5148\u7ea7\u5f15\u8d77\u7684\u987a\u5e8f\uff0c\u8fd8\u80fd\u591f\u589e\u5f3a\u53ef\u8bfb\u6027\u5e76\u6e05\u6670\u5730\u8868\u660e\u4f60\u7684\u76ee\u7684\u3002\u4f8b\u5982\uff0c value1 && value2 || value3 && value4 \u6700\u597d\u6539\u5199\u4e3a (value1 && value2) || (value3 && value4) . \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u91cc\u540c\u65f6\u4f7f\u7528\u903b\u8f91\u4e0e\u548c\u903b\u8f91\u6216\uff0c\u6700\u597d\u4f7f\u7528\u62ec\u53f7\u4e3a\u5176\u660e\u786e\u6307\u5b9a\u4f18\u5148\u7ea7\u3002","title":"\u6df7\u5408 AND \u548c OR"},{"location":"5-7-Logical-operators/#de-morgans-law","text":"\u5f88\u591a\u7a0b\u5e8f\u5458\u4f1a\u8ba4\u4e3a !(x && y) \u7b49\u4ef7\u4e8e !x && !y \u3002\u4e0d\u5e78\u7684\u662f\uff0c\u903b\u8f91\u975e\u8fd0\u7b97\u7b26\u5e76\u4e0d\u80fd\u50cf\u8fd9\u6837\u8fdb\u884c\u5206\u914d\u3002 \u6839\u636e \u5fb7\u6469\u6839\u5b9a\u5f8b \uff0c\u903b\u8f91\u975e\u7684\u5206\u914d\u5f8b\u5e94\u8be5\u662f\u4e0b\u9762\u8fd9\u79cd\u65b9\u5f0f\uff1a !(x && y) \u7b49\u4ef7\u4e8e !x || !y !(x || y) \u7b49\u4ef7\u4e8e !x && !y \u6362\u8a00\u4e4b\uff0c\u5f53\u4f60\u5206\u914d\u903b\u8f91\u975e\u8fd0\u7b97\u7b26\u65f6\uff0c\u5fc5\u987b\u5c06\u903b\u8f91\u4e0e\u66ff\u6362\u4e3a\u903b\u8f91\u6216\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002 \u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5fb7\u6469\u6839\u5b9a\u5f8b\u5bf9\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u8fdb\u884c\u7b80\u5316\u3002 \u6269\u5c55\u9605\u8bfb \u6211\u4eec\u53ef\u4ee5\u8bc1\u660e\u5fb7\u6469\u6839\u5b9a\u5f8b\u7684\u7b2c\u4e00\u90e8\u5206\u662f\u6b63\u786e\u7684\uff0c\u5373\u5bf9\u4e8e\u4efb\u4f55\u53ef\u80fd\u7684 x \u548c y \uff0c !(x && y) \u7b49\u4e8e !x || !y \u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u79f0\u4e3a \u771f\u503c\u8868 \u7684\u6570\u5b66\u6982\u5ff5\u3002 x y !x !y !(x && y) !x || !y false false true true true true false true true false true true true false false true true true true true false false false false \u5728\u8868\u4e2d\uff0c\u7b2c\u4e00\u5217\u548c\u7b2c\u4e8c\u5217\u5206\u522b\u8868\u793a x \u548c y \u3002\u6bcf\u4e00\u884c\u5219\u8868\u793a\u4e00\u7ec4\u53ef\u80fd\u7684 x \u548c y \u7ec4\u5408\uff0c4\u884c\u5c31\u8db3\u4ee5\u8868\u793a\u6240\u6709\u7684\u53ef\u80fd\u3002 \u5269\u4e0b\u7684\u5217\u8868\u793a\u57fa\u4e8e\u521d\u59cb x \u548c y \u6784\u9020\u7684\u8868\u8fbe\u5f0f\u3002\u7b2c\u4e09\u5217\u548c\u7b2c\u56db\u5217\u5206\u522b\u8ba1\u7b97 !x \u548c !y \u3002\u7b2c\u4e94\u5217\u8ba1\u7b97 !(x && y) \u3002\u7b2c\u516d\u5217\u5219\u8ba1\u7b97 !x || !y \u3002 \u53ef\u4ee5\u6ce8\u610f\u5230\uff0c\u7b2c\u4e94\u5217\u548c\u7b2c\u516d\u5217\u7684\u503c\u603b\u662f\u76f8\u540c\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u4e8e\u4efb\u610f x \u548c y \uff0c !(x && y) \u7b49\u4e8e !x || !y \uff0c\u8fd9\u4e5f\u6b63\u662f\u6211\u4eec\u8981\u8bc1\u660e\u7684\u3002 \u540c\u6837\u7684\uff0c\u6211\u4eec\u8fd8\u80fd\u591f\u8bc1\u660e\u5fb7\u6469\u6839\u5b9a\u5f8b\u7684\u7b2c\u4e8c\u90e8\u5206\u4e5f\u662f\u6b63\u786e\u7684\uff1a x y !x !y !(x || y) !x && !y false false true true true true false true true false false false true false false true false false true true false false false false \u540c\u6837\u7684\uff0c\u5bf9\u4e8e\u4efb\u610f x \u548c y \uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230 !(x || y) \u7b49\u4e8e !x && !y \u3002","title":"\u5fb7\u6469\u6839\u5b9a\u5f8b\uff08De Morgan\u2018s law\uff09"},{"location":"5-7-Logical-operators/#_2","text":"\u6709\u4e9b\u8bed\u8a00\u63d0\u4f9b\u4e86\u903b\u8f91\u5f02\u6216\uff08XOR\uff09\u8fd0\u7b97\u7b26\uff0c\u7528\u6765\u6d4b\u8bd5 \u662f\u5426\u6709\u5947\u6570\u4e2a\u6761\u4ef6\u4e3a\u771f \u3002 \u5de6\u64cd\u4f5c\u6570 \u53f3\u64cd\u4f5c\u6570 \u7ed3\u679c false false false false true true true false true true true false C++ \u5e76\u6ca1\u6709\u63d0\u4f9b\u903b\u8f91\u5f02\u6216\u8fd0\u7b97\u7b26\u3002\u548c \u903b\u8f91\u6216 \u6216\u8005 \u903b\u8f91\u4e0e \u4e0d\u540c\uff0c \u903b\u8f91\u5f02\u6216 \u5e76\u6ca1\u6709\u77ed\u8def\u6c42\u503c\u7279\u6027\u3002\u56e0\u6b64\uff0c\u4f7f\u7528\u903b\u8f91\u6216\u6216\u8005\u903b\u8f91\u4e0e\u6765\u6784\u5efa\u4e00\u4e2a\u903b\u8f91\u5f02\u6216\u662f\u5f88\u56f0\u96be\u7684\u3002\u4f46\u662f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0d\u7b49\u53f7( != )\u6765\u6a21\u62df\u903b\u8f91\u5f02\u6216\uff1a 1 if ( a != b ) ... // a XOR b, \u5047\u8bbe a \u548c b \u90fd\u662f\u5e03\u5c14\u503c \u4e5f\u53ef\u4ee5\u5bf9\u591a\u4e2a\u64cd\u4f5c\u6570\u6765\u4f7f\u7528\uff1a 1 if ( a != b != c != d ) ... // a XOR b XOR c XOR d, \u5047\u8bbe a, b, c, \u548c d \u90fd\u662f\u5e03\u5c14\u503c \u6ce8\u610f\uff0c\u4e0a\u9762\u7684\u65b9\u6cd5\u53ea\u6709\u5f53\u64cd\u4f5c\u6570\u90fd\u662f\u5e03\u5c14\u7c7b\u578b\u65f6\u624d\u6709\u6548 (\u4e0d\u80fd\u662f\u6574\u578b)\u3002\u5982\u679c\u4f60\u60f3\u5c06\u903b\u8f91\u5f02\u6216\u5e94\u7528\u4e8e\u975e\u5e03\u5c14\u7c7b\u578b\u7684\u64cd\u4f5c\u6570\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 static_cast \u5c06\u5b83\u8f6c\u6362\u4e3a\u5e03\u5c14\u7c7b\u578b\u3002 1 if ( static_cast < bool > ( a ) != static_cast < bool > ( b ) != static_cast < bool > ( c ) != static_cast < bool > ( d )) ... // a XOR b XOR c XOR d, for any type that can be converted to bool","title":"\u903b\u8f91\u5f02\u6216\u8fd0\u7b97\u7b26\u5728\u54ea\u91cc\uff1f"},{"location":"5-7-Logical-operators/#_3","text":"C++ \u4e2d\u7684\u5f88\u591a\u64cd\u4f5c\u7b26 (\u4f8b\u5982 operator || ) \u90fd\u6709\u53ef\u4ee5\u4ee3\u66ff\u7b26\u53f7\u8868\u793a\u6cd5\u7684\u5173\u952e\u5b57\u3002\u51fa\u4e8e\u5386\u53f2\u539f\u56e0\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u7684\u952e\u76d8\u6216\u8005\u6807\u51c6\u652f\u6301\u8f93\u5165\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u7684\u7b26\u53f7\u3002\u56e0\u6b64C++\u63d0\u4f9b\u4e86\u4e00\u7ec4\u5173\u952e\u5b57\u7528\u4e8e\u4ee3\u66ff\u7b26\u53f7\u3002\u4f8b\u5982\uff0c || \u4e5f\u53ef\u4ee5\u7528 or \u6765\u66ff\u4ee3\u3002 \u5b8c\u6574\u7684\u5217\u8868\u53ef\u4ee5\u53c2\u8003 \u8fd9\u91cc \uff0c\u5c24\u5176\u53ef\u4ee5\u6ce8\u610f\u4e00\u4e0b\u4e0b\u9762\u4e09\u79cd\uff1a \u8fd0\u7b97\u7b26 \u5173\u952e\u5b57\u66ff\u4ee3\u540d && and || or ! not \u56e0\u6b64\u4e0b\u9762\u4e24\u884c\u4ee3\u7801\u662f\u7b49\u4ef7\u7684\uff1a 1 2 std :: cout << ! a && ( b || c ); std :: cout << not a and ( b or c ); \u5c3d\u7ba1\u8fd9\u4e9b\u5173\u952e\u5b57\u770b\u8d77\u6765\u53ef\u8bfb\u6027\u66f4\u597d\uff0c\u4f46\u662f\u5927\u591a\u6570\u6709\u7ecf\u9a8c\u7684 C++ \u7a0b\u5e8f\u5458\u8fd8\u662f\u559c\u6b22\u4f7f\u7528\u7b26\u53f7\u800c\u4e0d\u662f\u5173\u952e\u5b57\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u8fd8\u662f\u5efa\u8bae\u4f60\u5b66\u4e60\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u7684\u7b26\u53f7\u8868\u793a\u6cd5\uff0c\u8fd9\u4e5f\u662f\u4ee3\u7801\u4e2d\u6700\u5e38\u89c1\u7684\u5f62\u5f0f\u3002","title":"\u8fd0\u7b97\u7b26\u7684\u5176\u4ed6\u8868\u793a\u5f62\u5f0f"},{"location":"5-x-Chapter-5-summary-and-quiz/","tags":["summary"],"text":"\u7ae0\u8282\u56de\u987e \u4f7f\u7528\u62ec\u53f7\u660e\u786e\u6307\u5b9a\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\uff0c\u907f\u514d\u6b67\u4e49\u3002\uff08 5.1 - \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b \uff09 \u7b97\u6570\u8fd0\u7b97\u7b26\u7684\u884c\u4e3a\u548c\u666e\u901a\u7684\u6570\u5b66\u8fd0\u7b97\u7b26\u662f\u4e00\u6837\u7684\u3002\uff08 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 \uff09\u6c42\u6a21\u8fd0\u7b97\u7b26( % )\u8fd4\u56de\u6b63\u6570\u76f8\u9664\u7684\u4f59\u6570\uff08 5.3 - \u6c42\u6a21\u548c\u6307\u6570\u8fd0\u7b97 \uff09 \u81ea\u589e\u81ea\u51cf\u64cd\u4f5c\u7b26\u53ef\u4ee5\u5bf9\u4e00\u4e2a\u53d8\u91cf\u8fdb\u884c\u9012\u589e\u548c\u9012\u51cf\u64cd\u4f5c\u3002\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u81ea\u589e\u81ea\u51cf\u64cd\u4f5c\u7b26\u7684\u540e\u7f00\u5f62\u5f0f\uff08 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 \uff09 \u6ce8\u610f\u64cd\u4f5c\u7b26\u7684\u526f\u4f5c\u7528\uff0c\u5c24\u5176\u662f\u5728\u51fd\u6570\u53c2\u6570\u6c42\u503c\u65f6\u3002\u5728\u4e00\u4e2a\u8bed\u53e5\u4e2d\uff0c\u4e0d\u8981\u8ba9\u526f\u4f5c\u7528\u4e24\u6b21\u4f5c\u7528\u4e8e\u4e00\u4e2a\u53d8\u91cf 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 \uff09 \u9017\u53f7\u8fd0\u7b97\u7b26\u53ef\u4ee5\u5c06\u591a\u4e2a\u8bed\u53e5\u5408\u5e76\u4e3a\u4e00\u4e2a\u3002\u4f46\u662f\u591a\u4e2a\u8bed\u53e5\u901a\u5e38\u5206\u5f00\u5199\u4f1a\u66f4\u597d\u3002\uff08 5.5 - \u9017\u53f7\u548c\u6761\u4ef6\u8fd0\u7b97\u7b26 \uff09 \u6761\u4ef6\u8fd0\u7b97\u7b26\u53ef\u4ee5\u6784\u5efa\u4e00\u4e2a \u7cbe\u7b80\u7248 \u7684 if \u8bed\u53e5\uff0c\u4f46\u662f\u5e76\u4e0d\u8981\u628a\u5b83\u5f53\u505a if \u8bed\u53e5\u7684\u4ee3\u66ff\u3002\u53ea\u6709\u5f53\u4f60\u5173\u7cfb\u5b83\u7684\u7ed3\u679c\u65f6\u518d\u4f7f\u7528\uff08 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 \uff09 \u5173\u7cfb\u8fd0\u7b97\u7b26\u53ef\u4ee5\u88ab\u7528\u6765\u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\uff0c\u4f46\u662f\u8981\u5c0f\u5fc3\u5bf9\u6d6e\u70b9\u6570\u5e94\u7528 \u7b49\u4e8e \u548c \u4e0d\u7b49\u4e8e \uff08 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 \uff09 \u4f7f\u7528\u903b\u8f91\u8fd0\u7b97\u7b26\u6211\u4eec\u53ef\u4ee5\u6784\u5efa\u590d\u5408\u6761\u4ef6\u8bed\u53e5\uff08 5.7 - \u903b\u8f91\u8fd0\u7b97\u7b26 \uff09","title":"5.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26"},{"location":"5-x-Chapter-5-summary-and-quiz/#_1","text":"\u4f7f\u7528\u62ec\u53f7\u660e\u786e\u6307\u5b9a\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\uff0c\u907f\u514d\u6b67\u4e49\u3002\uff08 5.1 - \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b \uff09 \u7b97\u6570\u8fd0\u7b97\u7b26\u7684\u884c\u4e3a\u548c\u666e\u901a\u7684\u6570\u5b66\u8fd0\u7b97\u7b26\u662f\u4e00\u6837\u7684\u3002\uff08 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 \uff09\u6c42\u6a21\u8fd0\u7b97\u7b26( % )\u8fd4\u56de\u6b63\u6570\u76f8\u9664\u7684\u4f59\u6570\uff08 5.3 - \u6c42\u6a21\u548c\u6307\u6570\u8fd0\u7b97 \uff09 \u81ea\u589e\u81ea\u51cf\u64cd\u4f5c\u7b26\u53ef\u4ee5\u5bf9\u4e00\u4e2a\u53d8\u91cf\u8fdb\u884c\u9012\u589e\u548c\u9012\u51cf\u64cd\u4f5c\u3002\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u81ea\u589e\u81ea\u51cf\u64cd\u4f5c\u7b26\u7684\u540e\u7f00\u5f62\u5f0f\uff08 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 \uff09 \u6ce8\u610f\u64cd\u4f5c\u7b26\u7684\u526f\u4f5c\u7528\uff0c\u5c24\u5176\u662f\u5728\u51fd\u6570\u53c2\u6570\u6c42\u503c\u65f6\u3002\u5728\u4e00\u4e2a\u8bed\u53e5\u4e2d\uff0c\u4e0d\u8981\u8ba9\u526f\u4f5c\u7528\u4e24\u6b21\u4f5c\u7528\u4e8e\u4e00\u4e2a\u53d8\u91cf 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 \uff09 \u9017\u53f7\u8fd0\u7b97\u7b26\u53ef\u4ee5\u5c06\u591a\u4e2a\u8bed\u53e5\u5408\u5e76\u4e3a\u4e00\u4e2a\u3002\u4f46\u662f\u591a\u4e2a\u8bed\u53e5\u901a\u5e38\u5206\u5f00\u5199\u4f1a\u66f4\u597d\u3002\uff08 5.5 - \u9017\u53f7\u548c\u6761\u4ef6\u8fd0\u7b97\u7b26 \uff09 \u6761\u4ef6\u8fd0\u7b97\u7b26\u53ef\u4ee5\u6784\u5efa\u4e00\u4e2a \u7cbe\u7b80\u7248 \u7684 if \u8bed\u53e5\uff0c\u4f46\u662f\u5e76\u4e0d\u8981\u628a\u5b83\u5f53\u505a if \u8bed\u53e5\u7684\u4ee3\u66ff\u3002\u53ea\u6709\u5f53\u4f60\u5173\u7cfb\u5b83\u7684\u7ed3\u679c\u65f6\u518d\u4f7f\u7528\uff08 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 \uff09 \u5173\u7cfb\u8fd0\u7b97\u7b26\u53ef\u4ee5\u88ab\u7528\u6765\u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u6bd4\u8f83\uff0c\u4f46\u662f\u8981\u5c0f\u5fc3\u5bf9\u6d6e\u70b9\u6570\u5e94\u7528 \u7b49\u4e8e \u548c \u4e0d\u7b49\u4e8e \uff08 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 \uff09 \u4f7f\u7528\u903b\u8f91\u8fd0\u7b97\u7b26\u6211\u4eec\u53ef\u4ee5\u6784\u5efa\u590d\u5408\u6761\u4ef6\u8bed\u53e5\uff08 5.7 - \u903b\u8f91\u8fd0\u7b97\u7b26 \uff09","title":"\u7ae0\u8282\u56de\u987e"},{"location":"6-1-Compound-statements-blocks/","tags":["statement"],"text":"Key Takeaway \u51fd\u6570\u7684\u5d4c\u5957\u5c42\u6570\u4e0d\u8981\u8d85\u8fc73\u5c42\u3002\u5982\u679c\u4f60\u7684\u51fd\u6570\u5d4c\u5957\u8fc7\u9ad8\uff0c\u8bf7\u8003\u8651\u5c06\u5176\u5206\u89e3\u4e3a\u591a\u4e2a\u5b50\u51fd\u6570 \u590d\u5408\u8bed\u53e5\uff08\u4e5f\u79f0\u4e3a\u8bed\u5757\uff0c\u53e5\u5757\uff09\u6307\u7684\u662f\u5305\u542b \u96f6\u4e2a \u6216\u591a\u4e2a\u8bed\u53e5\u7684\u7ec4\u5408\u3002\u540c\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c06\u5b83\u4eec\u770b\u505a\u662f\u4e00\u4e2a\u5355\u72ec\u7684\u8bed\u53e5\u3002 \u8bed\u53e5\u5757\u4ee5 { \u5f00\u5934\uff0c\u4ee5 } \u7ed3\u5c3e\uff0c\u9700\u8981\u6267\u884c\u7684\u8bed\u53e5\u653e\u5728\u4e2d\u95f4\u3002\u4efb\u4f55\u80fd\u591f\u4f7f\u7528\u5355\u72ec\u8bed\u53e5\u7684\u5730\u65b9\uff0c\u4e5f\u90fd\u53ef\u4ee5\u4f7f\u7528\u8bed\u53e5\u5757\u3002\u5728\u8bed\u53e5\u5757\u7684\u672b\u5c3e\u4e0d\u9700\u8981\u6dfb\u52a0\u5206\u53f7\u3002 \u5728\u5b66\u4e60\u51fd\u6570\u7684\u65f6\u5019\u6211\u4eec\u5df2\u7ecf\u89c1\u8fc7\u8fd9\u6837\u7684\u7ed3\u6784\u4e86\uff0c\u51fd\u6570\u4f53\u5c31\u662f\u4e00\u4e2a\u5757\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int add ( int x , int y ) { // start block return x + y ; } // end block (no semicolon) int main () { // start block // multiple statements int value {}; // this is initialization, not a block add ( 3 , 4 ); return 0 ; } // end block (no semicolon) \u8bed\u53e5\u5757\u5d4c\u5957 \u5c3d\u7ba1\u51fd\u6570\u4f53\u5185\u4e0d\u80fd\u5d4c\u5165\u5176\u4ed6\u51fd\u6570\uff0c\u4f46\u662f\u8bed\u53e5\u5757\u4e2d\u662f\u53ef\u4ee5\u5d4c\u5165\u5176\u4ed6\u8bed\u53e5\u5757\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 int add ( int x , int y ) { // block return x + y ; } // end block int main () { // outer block // multiple statements int value {}; { // inner/nested block add ( 3 , 4 ); } // end inner/nested block return 0 ; } // end outer block \u5f53\u8bed\u53e5\u5757\u5d4c\u5957\u4e4b\u540e\uff0c\u5305\u542b\u5176\u4ed6\u8bed\u53e5\u5757\u7684\u8bed\u53e5\u5757\u79f0\u4e3a \u5916\u5c42\u8bed\u53e5\u5757 \uff0c\u88ab\u5305\u542b\u7684\u8bed\u53e5\u5757\u79f0\u4e3a \u5185\u5c42\u8bed\u53e5\u5757 \u6216\u8005\u5d4c\u5957\u8bed\u53e5\u5757\u3002 \u4f7f\u7528\u8bed\u53e5\u5757\u6309\u6761\u4ef6\u6267\u884c\u591a\u6761\u8bed\u53e5 \u8bed\u53e5\u5757\u6700\u5e38\u7528\u7684\u573a\u666f\u662f\u914d\u5408 if \u8bed\u53e5\u6765\u4f7f\u7528\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cif \u8bed\u53e5\u5f53\u6761\u4ef6\u6c42\u503c\u4e3a\u771f\u65f6\uff0c\u4f1a\u6267\u884c\u4e00\u6761\u8bed\u53e5\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8bed\u53e5\u5757\u6765\u66ff\u6362\u5355\u56fe\u8bed\u53e5\uff0c\u8fd9\u6837 if \u5c31\u80fd\u591f\u5728\u6c42\u503c\u4e3a\u771f\u65f6\uff0c\u6267\u884c\u591a\u6761\u8bed\u53e5\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int main () { // start of outer block std :: cout << \"Enter an integer: \" ; int value {}; std :: cin >> value ; if ( value >= 0 ) { // start of nested block std :: cout << value << \" is a positive integer (or zero) \\n \" ; std :: cout << \"Double this number is \" << value * 2 << '\\n' ; } // end of nested block else { // start of another nested block std :: cout << value << \" is a negative integer \\n \" ; std :: cout << \"The positive of this number is \" << - value << '\\n' ; } // end of another nested block return 0 ; } // end of outer block \u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u662f 3\uff1a 1 2 3 Enter an integer: 3 3 is a positive integer (or zero) Double this number is 6 \u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u662f 4\uff1a 1 2 3 Enter an integer: -4 -4 is a negative integer The positive of this number is 4 \u8bed\u53e5\u5757\u5d4c\u5957\u7684\u5c42\u6570 \u5728\u8bed\u53e5\u5757\u4e2d\u5d4c\u5957\u8bed\u53e5\u5757\uff0c\u518d\u5d4c\u5957\u8bed\u53e5\u5757\u4e5f\u662f\u53ef\u4ee5\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 int main () { // block 1, nesting level 1 std :: cout << \"Enter an integer: \" ; int value {}; std :: cin >> value ; if ( value > 0 ) { // block 2, nesting level 2 if (( value % 2 ) == 0 ) { // block 3, nesting level 3 std :: cout << value << \" is positive and even \\n \" ; } else { // block 4, also nesting level 3 std :: cout << value << \" is positive and odd \\n \" ; } } return 0 ; } \u51fd\u6570\u7684\u5d4c\u5957\u5c42\u6570 (\u4e5f\u79f0\u4e3a\u5d4c\u5957\u6df1\u5ea6) \u6307\u7684\u662f\u51fd\u6570\u4e2d\u5d4c\u5957\u8bed\u53e5\u5757\u7684\u6700\u5927\u503c\uff08\u5305\u62ec\u6700\u5916\u5c42\u8bed\u53e5\u5757\uff09\u3002\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u4e00\u5171\u67094\u4e2a\u8bed\u53e5\u5757\uff0c\u4f46\u662f\u5d4c\u5957\u5c42\u6570\u4e3a 3\uff0c\u56e0\u4e3a\u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\u4efb\u610f\u4f4d\u7f6e\u4f60\u4e0d\u53ef\u80fd\u5904\u5728\u5927\u4e8e3\u5c42\u7684\u5d4c\u5957\u4e2d\u3002 C++ \u6807\u51c6\u5199\u9053\uff0cC++\u7f16\u8bd1\u5668\u9700\u8981\u652f\u6301 256 \u5c42\u5d4c\u5957\u2014\u2014\u4f46\u662f\u5e76\u4e0d\u662f\u6240\u6709\u7f16\u8bd1\u5668\u90fd\u8fd9\u4e48\u505a\u4e86\uff08\u4f8b\u5982\uff0c\u622a\u6b62\u5230\u672c\u6587\u64b0\u5199\u65f6\uff0cVisual Studio \u652f\u6301\u7684\u5d4c\u5957\u5c42\u6570\u5728100\u5230110\u4e4b\u95f4\uff09\u3002 \u4e0d\u7ba1 C++ \u5728\u6280\u672f\u4e0a\u652f\u6301\u591a\u5c11\u5c42\u5d4c\u5957\uff0c\u5728\u6211\u4eec\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u5d4c\u5957\u5c42\u6570\u6700\u597d\u4e0d\u8981\u8d85\u8fc73\u3002\u5c31\u50cf\u7279\u522b\u957f\u7684\u51fd\u6570\u5e94\u8be5\u91cd\u6784\uff08\u91cd\u5199\u4e3a\u66f4\u5c0f\u7684\u51fd\u6570\uff09\u4e00\u6837\uff0c\u8fc7\u5ea6\u5d4c\u5957\u7684\u51fd\u6570\u96be\u4ee5\u9605\u8bfb\uff0c\u5d4c\u5957\u6700\u4e25\u91cd\u7684\u8bed\u53e5\u5757\u4e5f\u5e94\u8be5\u88ab\u91cd\u6784\u4e3a\u5355\u72ec\u7684\u51fd\u6570\u3002 \u6700\u4f73\u5b9e\u8df5 \u51fd\u6570\u7684\u5d4c\u5957\u5c42\u6570\u4e0d\u8981\u8d85\u8fc73\u5c42\u3002\u5982\u679c\u4f60\u7684\u51fd\u6570\u5d4c\u5957\u8fc7\u9ad8\uff0c\u8bf7\u8003\u8651\u5c06\u5176\u5206\u89e3\u4e3a\u591a\u4e2a\u5b50\u51fd\u6570","title":"6.1 - \u590d\u5408\u8bed\u53e5\uff08\u8bed\u53e5\u5757\uff09"},{"location":"6-1-Compound-statements-blocks/#_1","text":"\u5c3d\u7ba1\u51fd\u6570\u4f53\u5185\u4e0d\u80fd\u5d4c\u5165\u5176\u4ed6\u51fd\u6570\uff0c\u4f46\u662f\u8bed\u53e5\u5757\u4e2d\u662f\u53ef\u4ee5\u5d4c\u5165\u5176\u4ed6\u8bed\u53e5\u5757\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 int add ( int x , int y ) { // block return x + y ; } // end block int main () { // outer block // multiple statements int value {}; { // inner/nested block add ( 3 , 4 ); } // end inner/nested block return 0 ; } // end outer block \u5f53\u8bed\u53e5\u5757\u5d4c\u5957\u4e4b\u540e\uff0c\u5305\u542b\u5176\u4ed6\u8bed\u53e5\u5757\u7684\u8bed\u53e5\u5757\u79f0\u4e3a \u5916\u5c42\u8bed\u53e5\u5757 \uff0c\u88ab\u5305\u542b\u7684\u8bed\u53e5\u5757\u79f0\u4e3a \u5185\u5c42\u8bed\u53e5\u5757 \u6216\u8005\u5d4c\u5957\u8bed\u53e5\u5757\u3002","title":"\u8bed\u53e5\u5757\u5d4c\u5957"},{"location":"6-1-Compound-statements-blocks/#_2","text":"\u8bed\u53e5\u5757\u6700\u5e38\u7528\u7684\u573a\u666f\u662f\u914d\u5408 if \u8bed\u53e5\u6765\u4f7f\u7528\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cif \u8bed\u53e5\u5f53\u6761\u4ef6\u6c42\u503c\u4e3a\u771f\u65f6\uff0c\u4f1a\u6267\u884c\u4e00\u6761\u8bed\u53e5\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8bed\u53e5\u5757\u6765\u66ff\u6362\u5355\u56fe\u8bed\u53e5\uff0c\u8fd9\u6837 if \u5c31\u80fd\u591f\u5728\u6c42\u503c\u4e3a\u771f\u65f6\uff0c\u6267\u884c\u591a\u6761\u8bed\u53e5\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int main () { // start of outer block std :: cout << \"Enter an integer: \" ; int value {}; std :: cin >> value ; if ( value >= 0 ) { // start of nested block std :: cout << value << \" is a positive integer (or zero) \\n \" ; std :: cout << \"Double this number is \" << value * 2 << '\\n' ; } // end of nested block else { // start of another nested block std :: cout << value << \" is a negative integer \\n \" ; std :: cout << \"The positive of this number is \" << - value << '\\n' ; } // end of another nested block return 0 ; } // end of outer block \u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u662f 3\uff1a 1 2 3 Enter an integer: 3 3 is a positive integer (or zero) Double this number is 6 \u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u662f 4\uff1a 1 2 3 Enter an integer: -4 -4 is a negative integer The positive of this number is 4","title":"\u4f7f\u7528\u8bed\u53e5\u5757\u6309\u6761\u4ef6\u6267\u884c\u591a\u6761\u8bed\u53e5"},{"location":"6-1-Compound-statements-blocks/#_3","text":"\u5728\u8bed\u53e5\u5757\u4e2d\u5d4c\u5957\u8bed\u53e5\u5757\uff0c\u518d\u5d4c\u5957\u8bed\u53e5\u5757\u4e5f\u662f\u53ef\u4ee5\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 int main () { // block 1, nesting level 1 std :: cout << \"Enter an integer: \" ; int value {}; std :: cin >> value ; if ( value > 0 ) { // block 2, nesting level 2 if (( value % 2 ) == 0 ) { // block 3, nesting level 3 std :: cout << value << \" is positive and even \\n \" ; } else { // block 4, also nesting level 3 std :: cout << value << \" is positive and odd \\n \" ; } } return 0 ; } \u51fd\u6570\u7684\u5d4c\u5957\u5c42\u6570 (\u4e5f\u79f0\u4e3a\u5d4c\u5957\u6df1\u5ea6) \u6307\u7684\u662f\u51fd\u6570\u4e2d\u5d4c\u5957\u8bed\u53e5\u5757\u7684\u6700\u5927\u503c\uff08\u5305\u62ec\u6700\u5916\u5c42\u8bed\u53e5\u5757\uff09\u3002\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u4e00\u5171\u67094\u4e2a\u8bed\u53e5\u5757\uff0c\u4f46\u662f\u5d4c\u5957\u5c42\u6570\u4e3a 3\uff0c\u56e0\u4e3a\u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\u4efb\u610f\u4f4d\u7f6e\u4f60\u4e0d\u53ef\u80fd\u5904\u5728\u5927\u4e8e3\u5c42\u7684\u5d4c\u5957\u4e2d\u3002 C++ \u6807\u51c6\u5199\u9053\uff0cC++\u7f16\u8bd1\u5668\u9700\u8981\u652f\u6301 256 \u5c42\u5d4c\u5957\u2014\u2014\u4f46\u662f\u5e76\u4e0d\u662f\u6240\u6709\u7f16\u8bd1\u5668\u90fd\u8fd9\u4e48\u505a\u4e86\uff08\u4f8b\u5982\uff0c\u622a\u6b62\u5230\u672c\u6587\u64b0\u5199\u65f6\uff0cVisual Studio \u652f\u6301\u7684\u5d4c\u5957\u5c42\u6570\u5728100\u5230110\u4e4b\u95f4\uff09\u3002 \u4e0d\u7ba1 C++ \u5728\u6280\u672f\u4e0a\u652f\u6301\u591a\u5c11\u5c42\u5d4c\u5957\uff0c\u5728\u6211\u4eec\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u5d4c\u5957\u5c42\u6570\u6700\u597d\u4e0d\u8981\u8d85\u8fc73\u3002\u5c31\u50cf\u7279\u522b\u957f\u7684\u51fd\u6570\u5e94\u8be5\u91cd\u6784\uff08\u91cd\u5199\u4e3a\u66f4\u5c0f\u7684\u51fd\u6570\uff09\u4e00\u6837\uff0c\u8fc7\u5ea6\u5d4c\u5957\u7684\u51fd\u6570\u96be\u4ee5\u9605\u8bfb\uff0c\u5d4c\u5957\u6700\u4e25\u91cd\u7684\u8bed\u53e5\u5757\u4e5f\u5e94\u8be5\u88ab\u91cd\u6784\u4e3a\u5355\u72ec\u7684\u51fd\u6570\u3002 \u6700\u4f73\u5b9e\u8df5 \u51fd\u6570\u7684\u5d4c\u5957\u5c42\u6570\u4e0d\u8981\u8d85\u8fc73\u5c42\u3002\u5982\u679c\u4f60\u7684\u51fd\u6570\u5d4c\u5957\u8fc7\u9ad8\uff0c\u8bf7\u8003\u8651\u5c06\u5176\u5206\u89e3\u4e3a\u591a\u4e2a\u5b50\u51fd\u6570","title":"\u8bed\u53e5\u5757\u5d4c\u5957\u7684\u5c42\u6570"},{"location":"6-10-Static-local-variables/","tags":["static","local variable"],"text":"Key Takeaway \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\uff0c\u4f1a\u5728\u53d8\u91cf\u5b9a\u4e49\u65f6\u521b\u5efa\u53d8\u91cf\uff0c\u9000\u51fa\u8bed\u53e5\u5757\u65f6\u9500\u6bc1\u53d8\u91cf \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\uff0c\u4f1a\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u521b\u5efa\u53d8\u91cf\uff0c\u5e76\u4e14\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u9500\u6bc1\u3002 \u5177\u6709\u521d\u503c0\u6216\u8005constexpr\u7c7b\u578b\u521d\u59cb\u5316\u503c\u7684\u9759\u6001\u53d8\u91cf\uff0c\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u5c31\u4f1a\u88ab\u521d\u59cb\u5316\u3002\u800c\u5177\u6709\u975econstexpr\u7c7b\u578b\u521d\u59cb\u5316\u503c\u7684\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u5219\u4f1a\u5728\u7b2c\u4e00\u6b21\u88ab\u5b9a\u4e49\u65f6\u8fdb\u884c\u521d\u59cb\u5316(\u5728\u540e\u7eed\u7684\u51fd\u6570\u8c03\u7528\u4e2d\uff0c\u8be5\u53d8\u91cf\u4e0d\u4f1a\u88ab\u91cd\u65b0\u521d\u59cb\u5316\uff09\u3002 \u9759\u6001\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u4e00\u6837\u90fd\u53ef\u4ee5\u6301\u7eed\u5230\u7a0b\u5e8f\u7ed3\u675f\u524d\uff0c\u4f46\u662f\u5b83\u7684\u53ef\u89c1\u8303\u56f4\u66f4\u5c0f\uff0c\u4e5f\u5c31\u66f4\u5b89\u5168\u3002 \u5bf9\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u3002\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u53ea\u4f1a\u5728\u7b2c\u4e00\u6b21\u6267\u884c\u65f6\u88ab\u521d\u59cb\u5316\uff0c\u540e\u9762\u4e0d\u4f1a\u5bf9\u5176\u8fdb\u884c\u91cd\u590d\u5730\u521d\u59cb\u5316\u3002 \u5f53\u53d8\u91cf\u521b\u5efa\u6216\u8005\u521d\u59cb\u5316\u5f00\u9500\u5f88\u5927\u65f6\uff0c\u53ef\u4ee5\u5c06\u5176\u5b9a\u4e49\u4e3a\u9759\u6001const\u5c40\u90e8\u53d8\u91cf \u9664\u975e\u53d8\u91cf\u6c38\u8fdc\u4e0d\u9700\u8981\u88ab\u91cd\u7f6e\uff0c\u5426\u5219\u8981\u907f\u514d\u4f7f\u7528\u9759\u6001\u5c40\u90e8\u53d8\u91cf static \u662f C++ \u4e2d\u6700\u4ee4\u4eba\u56f0\u60d1\u7684\u672f\u8bed\u4e4b\u4e00\uff0c\u8fd9\u4e3b\u8981\u662f\u56e0\u4e3a static \u5728\u4e0d\u540c\u8bed\u5883\u4e0b\u7684\u542b\u4e49\u4e5f\u662f\u4e0d\u540c\u7684\u3002 \u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff08 6.4 - \u5168\u5c40\u53d8\u91cf \uff09\uff0c\u6211\u4eec\u4ecb\u7ecd\u8fc7\u5168\u5c40\u53d8\u91cf\u5177\u6709 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u4e5f\u5c31\u662f\u8bf4\u5168\u5c40\u53d8\u91cf\u4f1a\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u88ab\u521b\u5efa\uff0c\u5e76\u4e14\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u3002 \u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86 static \u5173\u952e\u5b57\u53ef\u4ee5\u4f7f\u5168\u5c40\u53d8\u91cf\u5177\u6709 \u5185\u90e8\u94fe\u63a5 \u5c5e\u6027\uff0c\u4e5f\u5c31\u662f\u8be5\u5168\u5c40\u53d8\u91cf\u53ea\u80fd\u5728\u5b9a\u4e49\u5b83\u7684\u6587\u4ef6\u4e2d\u4f7f\u7528\u3002 \u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba static \u5bf9\u5c40\u90e8\u53d8\u91cf\u7684\u5f71\u54cd\u3002 \u9759\u6001\u5c40\u90e8\u53d8\u91cf \u5728 2.5 - \u5c40\u90e8\u4f5c\u7528\u57df \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u8fc7\u5c40\u90e8\u53d8\u91cf\u9ed8\u8ba4\u5177\u6709 \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4f1a\u5728\u5b9a\u4e49\u65f6\u88ab\u521b\u5efa\uff0c\u5728\u8bed\u53e5\u5757\u9000\u51fa\u65f6\u88ab\u9500\u6bc1\u3002 \u5bf9\u5c40\u90e8\u53d8\u91cf\u4f7f\u7528 static \u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u5c06\u5c40\u90e8\u53d8\u91cf\u7684 \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u4ece \u81ea\u52a8 \u4fee\u6539\u4e3a \u9759\u6001 \u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8fd9\u79cd\u5c40\u90e8\u53d8\u91cf\u4f1a\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u88ab\u521b\u5efa\uff0c\u7136\u540e\u5728\u7a0b\u5e8f\u7ed3\u675f\u540e\u88ab\u9500\u6bc1\uff08\u5c31\u50cf\u5168\u5c40\u53d8\u91cf\u4e00\u6837\uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u9759\u6001\u53d8\u91cf\u53ef\u4ee5\u5728\u79bb\u5f00\u4f5c\u7528\u57df\u540e\u7ee7\u7eed\u4fdd\u6301\u5b83\u7684\u503c\u3002 \u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u5c31\u53ef\u4ee5\u5f88\u597d\u7684\u5c55\u793a \u81ea\u52a8\u6301\u7eed\u65f6\u95f4 \u548c \u9759\u6001\u6301\u7eed\u65f6\u95f4 \u7684\u4e0d\u540c\u3002 \u81ea\u52a8\u6301\u7eed\u65f6\u95f4 \uff08\u9ed8\u8ba4\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void incrementAndPrint () { int value { 1 }; // automatic duration by default ++ value ; std :: cout << value << '\\n' ; } // value is destroyed here int main () { incrementAndPrint (); incrementAndPrint (); incrementAndPrint (); return 0 ; } \u6bcf\u6b21\u8c03\u7528 incrementAndPrint() \u7684\u65f6\u5019\uff0c\u53d8\u91cf value \u90fd\u4f1a\u88ab\u521b\u5efa\u5e76\u4e14\u8d4b\u503c\u4e3a 1\u3002 incrementAndPrint() \u4f1a\u628a value \u9012\u589e\u4e3a 2\uff0c\u7136\u540e\u5c06 2 \u6253\u5370\u51fa\u6765\uff0c\u5f53 incrementAndPrint() \u51fd\u6570\u9000\u51fa\u540e\uff0c\u53d8\u91cf\u5c31\u4f1a\u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1\u3002\u6240\u4ee5\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 2 2 2 \u63a5\u4e0b\u6765\uff0c\u8003\u8651 static \u7248\u672c\u7684\u7a0b\u5e8f\u3002\u8fd9\u4e24\u4e2a\u7248\u672c\u7a0b\u5e8f\u7684\u552f\u4e00\u533a\u522b\u5c31\u662f\u63a5\u4e0b\u6765\u6211\u4eec\u4f7f\u7528 static \u5173\u952e\u5b57\u5c06\u81ea\u52a8\u6301\u7eed\u65f6\u95f4\u4fee\u6539\u4e3a\u9759\u6001\u6301\u7eed\u65f6\u95f4\u3002 \u9759\u6001\u6301\u7eed\u65f6\u95f4 \uff08\u4f7f\u7528 static \u5173\u952e\u5b57\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void incrementAndPrint () { static int s_value { 1 }; // static duration via static keyword. This initializer is only executed once. ++ s_value ; std :: cout << s_value << '\\n' ; } // s_value is not destroyed here, but becomes inaccessible because it goes out of scope int main () { incrementAndPrint (); incrementAndPrint (); incrementAndPrint (); return 0 ; } \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u56e0\u4e3a s_value \u88ab\u5b9a\u4e49\u4e3a static \u7c7b\u578b\uff0c\u56e0\u6b64\u5b83\u4f1a\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u88ab\u521b\u5efa\u3002 \u5177\u6709\u521d\u503c0\u6216\u8005constexpr\u7c7b\u578b\u521d\u59cb\u5316\u503c\u7684\u9759\u6001\u53d8\u91cf\uff0c\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u5c31\u4f1a\u88ab\u521d\u59cb\u5316\u3002\u800c\u5177\u6709\u975econstexpr\u7c7b\u578b\u521d\u59cb\u5316\u503c\u7684\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u5219\u4f1a\u5728\u7b2c\u4e00\u6b21\u88ab\u5b9a\u4e49\u65f6\u8fdb\u884c\u521d\u59cb\u5316(\u5728\u540e\u7eed\u7684\u51fd\u6570\u8c03\u7528\u4e2d\uff0c\u8be5\u53d8\u91cf\u4e0d\u4f1a\u88ab\u91cd\u65b0\u521d\u59cb\u5316\uff09\u3002\u56e0\u4e3a s_value \u5177\u6709 constexpr \u521d\u59cb\u5316\u503c 1 \uff0c\u6240\u4ee5\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6 s_value \u5c31\u4f1a\u88ab\u521d\u59cb\u5316\u3002 \u5f53\u51fd\u6570\u7ed3\u675f\u65f6\uff0c s_value \u4f1a \u8d85\u51fa\u4f5c\u7528\u57df \uff0c\u4f46\u662f\u5b83\u4e0d\u4f1a\u88ab\u9500\u6bc1\u3002\u6bcf\u6b21 incrementAndPrint() \u88ab\u8c03\u7528\u65f6\uff0c s_value \u7684\u503c\u4ecd\u7136\u4e3a\u4e4b\u524d\u7684\u503c\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u7a0b\u5e8f\u7684\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 2 3 4 \u7c7b\u4f3c \u201cg_\u201d \u901a\u5e38\u4f5c\u4e3a\u5168\u5c40\u53d8\u91cf\u7684\u524d\u7f00\uff0c\u201cs_\u201d \u5e38\u88ab\u7528\u6765\u4f5c\u4e3a\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u7684\u524d\u7f00\u3002 \u9759\u6001\u5c40\u90e8\u53d8\u91cf\u6700\u5e38\u89c1\u7684\u7528\u6cd5\u662f\u6700\u4e3a\u4e00\u4e2a\u552f\u4e00ID\u751f\u6210\u5668\u3002\u60f3\u8c61\u4e00\u4e0b\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\u5305\u542b\u4e86\u5f88\u591a\u7c7b\u4f3c\u7684\u5bf9\u8c61\uff08\u4f8b\u5982\uff0c\u5728\u67d0\u4e2a\u6e38\u620f\u4e2d\u4f60\u88ab\u5f88\u591a\u50f5\u5c38\u653b\u51fb\uff0c\u6216\u8005\u4f60\u5e0c\u671b\u6a21\u62df\u5f88\u591a\u4e09\u89d2\u5f62\uff09\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f60\u51e0\u4e4e\u4e0d\u53ef\u80fd\u5b9a\u4f4d\u662f\u54ea\u4e2a\u5bf9\u8c61\u51fa\u95ee\u9898\u4e86\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u521b\u5efa\u4e00\u4e2a\u552f\u4e00\u7684\u6807\u8bc6\u7b26\uff0c\u5219\u53ef\u4ee5\u5728\u65e5\u540edebug\u65f6\u8f7b\u677e\u5730\u5c06\u5b83\u4eec\u533a\u522b\u5f00\u6765\u3002 \u4f7f\u7528\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u53ef\u4ee5\u5f88\u8f7b\u677e\u5730\u521b\u5efa\u552f\u4e00ID\uff1a 1 2 3 4 5 int generateID () { static int s_itemID { 0 }; return s_itemID ++ ; // makes copy of s_itemID, increments the real s_itemID, then returns the value in the copy } \u5f53\u51fd\u6570\u7b2c\u4e00\u6b21\u88ab\u8c03\u7528\u65f6\uff0c\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u88ab\u521d\u59cb\u5316\u4e3a0\u3002\u7b2c\u4e8c\u6b21\u8c03\u7528\u65f6\uff0c\u8be5\u53d8\u91cf\u9012\u589e\u4e3a\u4e86 1\u3002\u6bcf\u6b21\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u7684\u503c\u90fd\u4f1a\u6bd4\u4e4b\u524d\u4e00\u6b21\u8c03\u7528\u59271\u3002\u4f60\u53ef\u4ee5\u628a\u8fd9\u4e9b\u503c\u4f5c\u4e3a\u552f\u4e00ID\u8d4b\u503c\u7ed9\u5bf9\u8c61\u3002\u56e0\u4e3a s_itemID \u662f\u5c40\u90e8\u53d8\u91cf\uff0c\u6240\u4ee5\u5b83\u5e76\u4e0d\u4f1a\u88ab\u5176\u4ed6\u51fd\u6570\u201c\u7be1\u6539\u201d\u3002 \u9759\u6001\u53d8\u91cf\u76f8\u5bf9\u4e8e\u5168\u5c40\u53d8\u91cf\u5177\u6709\u4e00\u4e9b\u4f18\u52bf\uff08\u5b83\u4eec\u90fd\u4e0d\u4f1a\u5728\u7a0b\u5e8f\u7ed3\u675f\u524d\u88ab\u9500\u6bc1\uff09\uff0c\u56e0\u4e3a\u5b83\u53ea\u5728\u5b9a\u4e49\u5b83\u7684\u5757\u4e2d\u53ef\u89c1\u3002\u8fd9\u6837\u4e00\u6765\u5b83\u5c31\u4f1a\u66f4\u52a0\u5b89\u5168\uff08\u5f53\u4f60\u9700\u8981\u9891\u7e41\u4fee\u6539\u5b83\u7684\u503c\u7684\u65f6\u5019\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u3002\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u53ea\u4f1a\u5728\u7b2c\u4e00\u6b21\u6267\u884c\u65f6\u88ab\u521d\u59cb\u5316\uff0c\u540e\u9762\u4e0d\u4f1a\u5bf9\u5176\u8fdb\u884c\u91cd\u590d\u5730\u521d\u59cb\u5316\u3002 \u9759\u6001\u5c40\u90e8\u5e38\u91cf \u9759\u6001\u5c40\u90e8\u53d8\u91cf\u4e5f\u53ef\u4ee5\u88ab\u5b9a\u4e49\u4e3a\u5e38\u91cf\u3002\u9759\u6001\u5c40\u90e8\u5e38\u91cf\u7684\u5178\u578b\u4f7f\u7528\u573a\u666f\u662f\u5f53\u4f60\u7684\u51fd\u6570\u9700\u8981\u4e00\u4e2a\u5e38\u91cf\u503c\uff0c\u4f46\u662f\u521b\u5efa\u6216\u521d\u59cb\u5316\u8be5\u5e38\u91cf\u7684\u5f00\u9500\u975e\u5e38\u5927\uff08\u4f8b\u5982\u8be5\u503c\u662f\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u7684\uff09\u3002\u5982\u679c\u4f60\u4f7f\u7528\u4e00\u4e2a\u666e\u901a\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u90a3\u4e48\u8fd9\u4e2a\u53d8\u91cf\u5c31\u4f1a\u5728\u6bcf\u6b21\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u521d\u59cb\u5316\u3002\u800c\u4f7f\u7528const\u7c7b\u578b\u7684\u9759\u6001\u5c40\u90e8\u53d8\u91cf\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728\u7b2c\u4e00\u6b21\u8c03\u7528\u51fd\u6570\u65f6\u521d\u59cb\u5316\u5b83\uff0c\u7136\u540e\u5728\u540e\u7eed\u7684\u8c03\u7528\u4e2d\u5bf9\u5176\u8fdb\u884c\u91cd\u7528\u3002 \u4e0d\u8981\u4f7f\u7528\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u6765\u6539\u53d8\u6d41\u7a0b \u8003\u8651\u5982\u4e0b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include int getInteger () { static bool s_isFirstCall { true }; if ( s_isFirstCall ) { std :: cout << \"Enter an integer: \" ; s_isFirstCall = false ; } else { std :: cout << \"Enter another integer: \" ; } int i {}; std :: cin >> i ; return i ; } int main () { int a { getInteger () }; int b { getInteger () }; std :: cout << a << \" + \" << b << \" = \" << ( a + b ) << '\\n' ; return 0 ; } \u8f93\u51fa\u5982\u4e0b\uff1a 1 2 3 Enter an integer: 5 Enter another integer: 9 5 + 9 = 14 \u8fd9\u6bb5\u4ee3\u7801\u7684\u786e\u80fd\u591f\u6309\u7167\u9884\u671f\u5de5\u4f5c\uff0c\u4f46\u662f\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528\u7684\u662f\u4e00\u4e2a\u9759\u6001\u5c40\u90e8\u53d8\u91cf\uff0c\u8fd9\u4f1a\u4f7f\u5f97\u7a0b\u5e8f\u96be\u4ee5\u7406\u89e3\u3002\u5982\u679c\u6709\u4eba\u5728\u4e0d\u77e5\u9053 getInteger() \u51fd\u6570\u7684\u5b9e\u73b0\u7ec6\u8282\u65f6\uff0c\u9605\u8bfb main \u51fd\u6570\u7684\u4ee3\u7801\uff0c\u5b83\u5b8c\u5168\u4e0d\u4f1a\u8ba4\u4e3a\u4e24\u6b21\u8c03\u7528 getInteger() \u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u6548\u679c\u3002\u4f46\u662f\u4e24\u6b21\u8c03\u7528\u8be5\u51fd\u6570\u7684\u786e\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u8f93\u51fa\uff0c\u8fd9\u5c31\u597d\u8ba9\u4eba\u611f\u5230\u975e\u5e38\u56f0\u60d1\uff0c 1 \u5047\u8bbe\u4f60\u7684\u5fae\u6ce2\u7089\u4e0a\u6709\u4e00\u4e2a+1\u6309\u94ae\uff0c\u5f53\u4f60\u6309\u4e0b\u5b83\u7684\u65f6\u5019\u5c31\u4f1a\u5ef6\u957f\u4e00\u5206\u949f\u52a0\u70ed\u65f6\u95f4\u3002\u4f60\u51c6\u5907\u52a0\u70ed\u4e00\u4e9b\u5403\u7684\uff0c\u8bbe\u5b9a\u597d\u65f6\u95f4\uff0c\u7136\u540e\u7b49\u7740\u51fa\u9505\u3002\u8fd9\u671f\u95f4\u4f60\u53ef\u4ee5\u770b\u770b\u7a97\u5916\u7684\u732b\u54aa\uff0c\u6d88\u78e8\u6d88\u78e8\u65f6\u95f4\u3002\u7136\u540e\u5fae\u6ce2\u7089\u54cd\u4e86\uff0c\u4f60\u628a\u996d\u62ff\u51fa\u6765\u957f\u4e86\u4e00\u53e3\uff0c\u53d1\u73b0\u8fd8\u6709\u70b9\u51c9\u3002\u6ca1\u5173\u7cfb\uff0c\u4f60\u53c8\u628a\u996d\u653e\u4e86\u56de\u53bb\u7136\u540e\u6309\u4e86\u4e00\u4e0b+1\u6309\u94ae\uff0c\u51c6\u5907\u518d\u70ed\u4e00\u5206\u949f\u3002\u53ef\u662f\u8fd9\u4e00\u6b21\uff0c\u5fae\u6ce2\u7089\u53ea\u52a0\u70ed\u4e86\u4e00\u79d2\u949f\uff0c\u800c\u4e0d\u662f\u4e00\u5206\u949f\u3002\u8fd9\u79cd\u60c5\u51b5\u5c31\u50cf\u6211\u4eec\u603b\u8bf4\u7684\u90a3\u6837\u201c\u6211\u4eec\u4ec0\u4e48\u4e5f\u6ca1\u6539\uff0c\u7a0b\u5e8f\u5c31\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u4e86\u201d\u6216\u662f\u201c\u4e0a\u6b21\u7528\u8fd8\u597d\u597d\u7684\u5462\u201d\u3002\u6211\u4eec\u603b\u662f\u671f\u671b\uff0c\u5f53\u91cd\u590d\u4e00\u4e2a\u52a8\u4f5c\u65f6\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u4e5f\u5e94\u8be5\u548c\u4e4b\u524d\u662f\u4e00\u6837\u7684\uff0c\u540c\u6837\u7684\u9053\u7406\u4e5f\u9002\u7528\u4e8e\u51fd\u6570\u3002 \u5047\u8bbe\u6211\u4eec\u60f3\u8981\u4e3a\u8ba1\u7b97\u5668\u6dfb\u52a0\u51cf\u6cd5\u529f\u80fd\uff0c\u671f\u671b\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 Addition Enter an integer: 5 Enter another integer: 9 5 + 9 = 14 Subtraction Enter an integer: 12 Enter another integer: 3 12 - 3 = 9 \u6b64\u65f6\u4f60\u53ef\u80fd\u4f1a\u8003\u8651\u4f7f\u7528 getInteger() \u518d\u8bfb\u53d6\u4e24\u4e2a\u6574\u6570\uff0c\u5c31\u50cf\u505a\u52a0\u6cd5\u65f6\u4e00\u6837\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 int main () { std :: cout << \"Addition \\n \" ; int a { getInteger () }; int b { getInteger () }; std :: cout << a << \" + \" << b << \" = \" << ( a + b ) << '\\n' ; std :: cout << \"Subtraction \\n \" ; int c { getInteger () }; int d { getInteger () }; std :: cout << c << \" - \" << d << \" = \" << ( c - d ) << '\\n' ; return 0 ; } \u4f46\u662f\uff0c\u8fd9\u6837\u5e76\u6ca1\u6709\u7528\uff0c\u8f93\u51fa\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 Addition Enter an integer: 5 Enter another integer: 9 5 + 9 = 14 Subtraction Enter another integer: 12 Enter another integer: 3 12 - 3 = 9 (\u8f93\u51fa\u7684\u8fd8\u662f \u201cEnter another integer\u201d \u800c\u4e0d\u662f \u201cEnter an integer\u201d) getInteger() \u662f\u4e0d\u53ef\u91cd\u7528\u7684\uff0c\u56e0\u5b83\u5177\u6709\u67d0\u79cd\u5185\u90e8\u72b6\u6001\uff08\u9759\u6001\u5c40\u90e8\u53d8\u91cf s_isFirstCall \uff09\uff0c\u800c\u8be5\u72b6\u6001\u5e76\u4e0d\u80fd\u591f\u4ece\u5916\u90e8\u88ab\u91cd\u7f6e\u3002 s_isFirstCall \u5e76\u4e0d\u662f\u4e00\u4e2a\u9700\u8981\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u4fdd\u6301\u552f\u4e00\u7684\u53d8\u91cf\uff0c\u5c3d\u7ba1\u4e0a\u9762\u7b2c\u4e00\u4e2a\u7a0b\u5e8f\u80fd\u591f\u6b63\u786e\u8fd0\u884c\uff0c\u4f46\u662f\u9759\u6001\u53d8\u91cf\u963b\u788d\u4e86\u6211\u4eec\u5bf9\u51fd\u6570\u7684\u91cd\u7528\u3002 \u5b9e\u73b0 getInteger \u7684\u66f4\u597d\u7684\u65b9\u6cd5\u662f\u5c06 s_isFirstCall \u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u3002\u8fd9\u6837\u4e3b\u8c03\u51fd\u6570\u5c31\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u6253\u5370\u7684\u5185\u5bb9\u3002 \u9759\u6001\u5c40\u90e8\u53d8\u91cf\u9002\u7528\u4e8e\u8be5\u53d8\u91cf\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\uff08\u6216\u53ef\u9884\u89c1\u7684\u672a\u6765\uff09\u9700\u8981\u4fdd\u6301\u552f\u4e00\u6027\u4e14\u65e0\u9700\u5bf9\u5176\u8fdb\u884c\u91cd\u7f6e\u7684\u60c5\u51b5\u3002 \u6700\u4f73\u5b9e\u8df5 \u9664\u975e\u53d8\u91cf\u6c38\u8fdc\u4e0d\u9700\u8981\u88ab\u91cd\u7f6e\uff0c\u5426\u5219\u8981\u907f\u514d\u4f7f\u7528\u9759\u6001\u5c40\u90e8\u53d8\u91cf","title":"6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf"},{"location":"6-10-Static-local-variables/#_1","text":"\u5728 2.5 - \u5c40\u90e8\u4f5c\u7528\u57df \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u8fc7\u5c40\u90e8\u53d8\u91cf\u9ed8\u8ba4\u5177\u6709 \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4f1a\u5728\u5b9a\u4e49\u65f6\u88ab\u521b\u5efa\uff0c\u5728\u8bed\u53e5\u5757\u9000\u51fa\u65f6\u88ab\u9500\u6bc1\u3002 \u5bf9\u5c40\u90e8\u53d8\u91cf\u4f7f\u7528 static \u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u5c06\u5c40\u90e8\u53d8\u91cf\u7684 \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u4ece \u81ea\u52a8 \u4fee\u6539\u4e3a \u9759\u6001 \u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8fd9\u79cd\u5c40\u90e8\u53d8\u91cf\u4f1a\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u88ab\u521b\u5efa\uff0c\u7136\u540e\u5728\u7a0b\u5e8f\u7ed3\u675f\u540e\u88ab\u9500\u6bc1\uff08\u5c31\u50cf\u5168\u5c40\u53d8\u91cf\u4e00\u6837\uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u9759\u6001\u53d8\u91cf\u53ef\u4ee5\u5728\u79bb\u5f00\u4f5c\u7528\u57df\u540e\u7ee7\u7eed\u4fdd\u6301\u5b83\u7684\u503c\u3002 \u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u5c31\u53ef\u4ee5\u5f88\u597d\u7684\u5c55\u793a \u81ea\u52a8\u6301\u7eed\u65f6\u95f4 \u548c \u9759\u6001\u6301\u7eed\u65f6\u95f4 \u7684\u4e0d\u540c\u3002 \u81ea\u52a8\u6301\u7eed\u65f6\u95f4 \uff08\u9ed8\u8ba4\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void incrementAndPrint () { int value { 1 }; // automatic duration by default ++ value ; std :: cout << value << '\\n' ; } // value is destroyed here int main () { incrementAndPrint (); incrementAndPrint (); incrementAndPrint (); return 0 ; } \u6bcf\u6b21\u8c03\u7528 incrementAndPrint() \u7684\u65f6\u5019\uff0c\u53d8\u91cf value \u90fd\u4f1a\u88ab\u521b\u5efa\u5e76\u4e14\u8d4b\u503c\u4e3a 1\u3002 incrementAndPrint() \u4f1a\u628a value \u9012\u589e\u4e3a 2\uff0c\u7136\u540e\u5c06 2 \u6253\u5370\u51fa\u6765\uff0c\u5f53 incrementAndPrint() \u51fd\u6570\u9000\u51fa\u540e\uff0c\u53d8\u91cf\u5c31\u4f1a\u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1\u3002\u6240\u4ee5\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 2 2 2 \u63a5\u4e0b\u6765\uff0c\u8003\u8651 static \u7248\u672c\u7684\u7a0b\u5e8f\u3002\u8fd9\u4e24\u4e2a\u7248\u672c\u7a0b\u5e8f\u7684\u552f\u4e00\u533a\u522b\u5c31\u662f\u63a5\u4e0b\u6765\u6211\u4eec\u4f7f\u7528 static \u5173\u952e\u5b57\u5c06\u81ea\u52a8\u6301\u7eed\u65f6\u95f4\u4fee\u6539\u4e3a\u9759\u6001\u6301\u7eed\u65f6\u95f4\u3002 \u9759\u6001\u6301\u7eed\u65f6\u95f4 \uff08\u4f7f\u7528 static \u5173\u952e\u5b57\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void incrementAndPrint () { static int s_value { 1 }; // static duration via static keyword. This initializer is only executed once. ++ s_value ; std :: cout << s_value << '\\n' ; } // s_value is not destroyed here, but becomes inaccessible because it goes out of scope int main () { incrementAndPrint (); incrementAndPrint (); incrementAndPrint (); return 0 ; } \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u56e0\u4e3a s_value \u88ab\u5b9a\u4e49\u4e3a static \u7c7b\u578b\uff0c\u56e0\u6b64\u5b83\u4f1a\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u88ab\u521b\u5efa\u3002 \u5177\u6709\u521d\u503c0\u6216\u8005constexpr\u7c7b\u578b\u521d\u59cb\u5316\u503c\u7684\u9759\u6001\u53d8\u91cf\uff0c\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u5c31\u4f1a\u88ab\u521d\u59cb\u5316\u3002\u800c\u5177\u6709\u975econstexpr\u7c7b\u578b\u521d\u59cb\u5316\u503c\u7684\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u5219\u4f1a\u5728\u7b2c\u4e00\u6b21\u88ab\u5b9a\u4e49\u65f6\u8fdb\u884c\u521d\u59cb\u5316(\u5728\u540e\u7eed\u7684\u51fd\u6570\u8c03\u7528\u4e2d\uff0c\u8be5\u53d8\u91cf\u4e0d\u4f1a\u88ab\u91cd\u65b0\u521d\u59cb\u5316\uff09\u3002\u56e0\u4e3a s_value \u5177\u6709 constexpr \u521d\u59cb\u5316\u503c 1 \uff0c\u6240\u4ee5\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6 s_value \u5c31\u4f1a\u88ab\u521d\u59cb\u5316\u3002 \u5f53\u51fd\u6570\u7ed3\u675f\u65f6\uff0c s_value \u4f1a \u8d85\u51fa\u4f5c\u7528\u57df \uff0c\u4f46\u662f\u5b83\u4e0d\u4f1a\u88ab\u9500\u6bc1\u3002\u6bcf\u6b21 incrementAndPrint() \u88ab\u8c03\u7528\u65f6\uff0c s_value \u7684\u503c\u4ecd\u7136\u4e3a\u4e4b\u524d\u7684\u503c\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u7a0b\u5e8f\u7684\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 2 3 4 \u7c7b\u4f3c \u201cg_\u201d \u901a\u5e38\u4f5c\u4e3a\u5168\u5c40\u53d8\u91cf\u7684\u524d\u7f00\uff0c\u201cs_\u201d \u5e38\u88ab\u7528\u6765\u4f5c\u4e3a\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u7684\u524d\u7f00\u3002 \u9759\u6001\u5c40\u90e8\u53d8\u91cf\u6700\u5e38\u89c1\u7684\u7528\u6cd5\u662f\u6700\u4e3a\u4e00\u4e2a\u552f\u4e00ID\u751f\u6210\u5668\u3002\u60f3\u8c61\u4e00\u4e0b\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\u5305\u542b\u4e86\u5f88\u591a\u7c7b\u4f3c\u7684\u5bf9\u8c61\uff08\u4f8b\u5982\uff0c\u5728\u67d0\u4e2a\u6e38\u620f\u4e2d\u4f60\u88ab\u5f88\u591a\u50f5\u5c38\u653b\u51fb\uff0c\u6216\u8005\u4f60\u5e0c\u671b\u6a21\u62df\u5f88\u591a\u4e09\u89d2\u5f62\uff09\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f60\u51e0\u4e4e\u4e0d\u53ef\u80fd\u5b9a\u4f4d\u662f\u54ea\u4e2a\u5bf9\u8c61\u51fa\u95ee\u9898\u4e86\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u521b\u5efa\u4e00\u4e2a\u552f\u4e00\u7684\u6807\u8bc6\u7b26\uff0c\u5219\u53ef\u4ee5\u5728\u65e5\u540edebug\u65f6\u8f7b\u677e\u5730\u5c06\u5b83\u4eec\u533a\u522b\u5f00\u6765\u3002 \u4f7f\u7528\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u53ef\u4ee5\u5f88\u8f7b\u677e\u5730\u521b\u5efa\u552f\u4e00ID\uff1a 1 2 3 4 5 int generateID () { static int s_itemID { 0 }; return s_itemID ++ ; // makes copy of s_itemID, increments the real s_itemID, then returns the value in the copy } \u5f53\u51fd\u6570\u7b2c\u4e00\u6b21\u88ab\u8c03\u7528\u65f6\uff0c\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u88ab\u521d\u59cb\u5316\u4e3a0\u3002\u7b2c\u4e8c\u6b21\u8c03\u7528\u65f6\uff0c\u8be5\u53d8\u91cf\u9012\u589e\u4e3a\u4e86 1\u3002\u6bcf\u6b21\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u7684\u503c\u90fd\u4f1a\u6bd4\u4e4b\u524d\u4e00\u6b21\u8c03\u7528\u59271\u3002\u4f60\u53ef\u4ee5\u628a\u8fd9\u4e9b\u503c\u4f5c\u4e3a\u552f\u4e00ID\u8d4b\u503c\u7ed9\u5bf9\u8c61\u3002\u56e0\u4e3a s_itemID \u662f\u5c40\u90e8\u53d8\u91cf\uff0c\u6240\u4ee5\u5b83\u5e76\u4e0d\u4f1a\u88ab\u5176\u4ed6\u51fd\u6570\u201c\u7be1\u6539\u201d\u3002 \u9759\u6001\u53d8\u91cf\u76f8\u5bf9\u4e8e\u5168\u5c40\u53d8\u91cf\u5177\u6709\u4e00\u4e9b\u4f18\u52bf\uff08\u5b83\u4eec\u90fd\u4e0d\u4f1a\u5728\u7a0b\u5e8f\u7ed3\u675f\u524d\u88ab\u9500\u6bc1\uff09\uff0c\u56e0\u4e3a\u5b83\u53ea\u5728\u5b9a\u4e49\u5b83\u7684\u5757\u4e2d\u53ef\u89c1\u3002\u8fd9\u6837\u4e00\u6765\u5b83\u5c31\u4f1a\u66f4\u52a0\u5b89\u5168\uff08\u5f53\u4f60\u9700\u8981\u9891\u7e41\u4fee\u6539\u5b83\u7684\u503c\u7684\u65f6\u5019\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u3002\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u53ea\u4f1a\u5728\u7b2c\u4e00\u6b21\u6267\u884c\u65f6\u88ab\u521d\u59cb\u5316\uff0c\u540e\u9762\u4e0d\u4f1a\u5bf9\u5176\u8fdb\u884c\u91cd\u590d\u5730\u521d\u59cb\u5316\u3002","title":"\u9759\u6001\u5c40\u90e8\u53d8\u91cf"},{"location":"6-10-Static-local-variables/#_2","text":"\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u4e5f\u53ef\u4ee5\u88ab\u5b9a\u4e49\u4e3a\u5e38\u91cf\u3002\u9759\u6001\u5c40\u90e8\u5e38\u91cf\u7684\u5178\u578b\u4f7f\u7528\u573a\u666f\u662f\u5f53\u4f60\u7684\u51fd\u6570\u9700\u8981\u4e00\u4e2a\u5e38\u91cf\u503c\uff0c\u4f46\u662f\u521b\u5efa\u6216\u521d\u59cb\u5316\u8be5\u5e38\u91cf\u7684\u5f00\u9500\u975e\u5e38\u5927\uff08\u4f8b\u5982\u8be5\u503c\u662f\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u7684\uff09\u3002\u5982\u679c\u4f60\u4f7f\u7528\u4e00\u4e2a\u666e\u901a\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u90a3\u4e48\u8fd9\u4e2a\u53d8\u91cf\u5c31\u4f1a\u5728\u6bcf\u6b21\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u521d\u59cb\u5316\u3002\u800c\u4f7f\u7528const\u7c7b\u578b\u7684\u9759\u6001\u5c40\u90e8\u53d8\u91cf\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728\u7b2c\u4e00\u6b21\u8c03\u7528\u51fd\u6570\u65f6\u521d\u59cb\u5316\u5b83\uff0c\u7136\u540e\u5728\u540e\u7eed\u7684\u8c03\u7528\u4e2d\u5bf9\u5176\u8fdb\u884c\u91cd\u7528\u3002","title":"\u9759\u6001\u5c40\u90e8\u5e38\u91cf"},{"location":"6-10-Static-local-variables/#_3","text":"\u8003\u8651\u5982\u4e0b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include int getInteger () { static bool s_isFirstCall { true }; if ( s_isFirstCall ) { std :: cout << \"Enter an integer: \" ; s_isFirstCall = false ; } else { std :: cout << \"Enter another integer: \" ; } int i {}; std :: cin >> i ; return i ; } int main () { int a { getInteger () }; int b { getInteger () }; std :: cout << a << \" + \" << b << \" = \" << ( a + b ) << '\\n' ; return 0 ; } \u8f93\u51fa\u5982\u4e0b\uff1a 1 2 3 Enter an integer: 5 Enter another integer: 9 5 + 9 = 14 \u8fd9\u6bb5\u4ee3\u7801\u7684\u786e\u80fd\u591f\u6309\u7167\u9884\u671f\u5de5\u4f5c\uff0c\u4f46\u662f\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528\u7684\u662f\u4e00\u4e2a\u9759\u6001\u5c40\u90e8\u53d8\u91cf\uff0c\u8fd9\u4f1a\u4f7f\u5f97\u7a0b\u5e8f\u96be\u4ee5\u7406\u89e3\u3002\u5982\u679c\u6709\u4eba\u5728\u4e0d\u77e5\u9053 getInteger() \u51fd\u6570\u7684\u5b9e\u73b0\u7ec6\u8282\u65f6\uff0c\u9605\u8bfb main \u51fd\u6570\u7684\u4ee3\u7801\uff0c\u5b83\u5b8c\u5168\u4e0d\u4f1a\u8ba4\u4e3a\u4e24\u6b21\u8c03\u7528 getInteger() \u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u6548\u679c\u3002\u4f46\u662f\u4e24\u6b21\u8c03\u7528\u8be5\u51fd\u6570\u7684\u786e\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u8f93\u51fa\uff0c\u8fd9\u5c31\u597d\u8ba9\u4eba\u611f\u5230\u975e\u5e38\u56f0\u60d1\uff0c 1 \u5047\u8bbe\u4f60\u7684\u5fae\u6ce2\u7089\u4e0a\u6709\u4e00\u4e2a+1\u6309\u94ae\uff0c\u5f53\u4f60\u6309\u4e0b\u5b83\u7684\u65f6\u5019\u5c31\u4f1a\u5ef6\u957f\u4e00\u5206\u949f\u52a0\u70ed\u65f6\u95f4\u3002\u4f60\u51c6\u5907\u52a0\u70ed\u4e00\u4e9b\u5403\u7684\uff0c\u8bbe\u5b9a\u597d\u65f6\u95f4\uff0c\u7136\u540e\u7b49\u7740\u51fa\u9505\u3002\u8fd9\u671f\u95f4\u4f60\u53ef\u4ee5\u770b\u770b\u7a97\u5916\u7684\u732b\u54aa\uff0c\u6d88\u78e8\u6d88\u78e8\u65f6\u95f4\u3002\u7136\u540e\u5fae\u6ce2\u7089\u54cd\u4e86\uff0c\u4f60\u628a\u996d\u62ff\u51fa\u6765\u957f\u4e86\u4e00\u53e3\uff0c\u53d1\u73b0\u8fd8\u6709\u70b9\u51c9\u3002\u6ca1\u5173\u7cfb\uff0c\u4f60\u53c8\u628a\u996d\u653e\u4e86\u56de\u53bb\u7136\u540e\u6309\u4e86\u4e00\u4e0b+1\u6309\u94ae\uff0c\u51c6\u5907\u518d\u70ed\u4e00\u5206\u949f\u3002\u53ef\u662f\u8fd9\u4e00\u6b21\uff0c\u5fae\u6ce2\u7089\u53ea\u52a0\u70ed\u4e86\u4e00\u79d2\u949f\uff0c\u800c\u4e0d\u662f\u4e00\u5206\u949f\u3002\u8fd9\u79cd\u60c5\u51b5\u5c31\u50cf\u6211\u4eec\u603b\u8bf4\u7684\u90a3\u6837\u201c\u6211\u4eec\u4ec0\u4e48\u4e5f\u6ca1\u6539\uff0c\u7a0b\u5e8f\u5c31\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u4e86\u201d\u6216\u662f\u201c\u4e0a\u6b21\u7528\u8fd8\u597d\u597d\u7684\u5462\u201d\u3002\u6211\u4eec\u603b\u662f\u671f\u671b\uff0c\u5f53\u91cd\u590d\u4e00\u4e2a\u52a8\u4f5c\u65f6\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u4e5f\u5e94\u8be5\u548c\u4e4b\u524d\u662f\u4e00\u6837\u7684\uff0c\u540c\u6837\u7684\u9053\u7406\u4e5f\u9002\u7528\u4e8e\u51fd\u6570\u3002 \u5047\u8bbe\u6211\u4eec\u60f3\u8981\u4e3a\u8ba1\u7b97\u5668\u6dfb\u52a0\u51cf\u6cd5\u529f\u80fd\uff0c\u671f\u671b\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 Addition Enter an integer: 5 Enter another integer: 9 5 + 9 = 14 Subtraction Enter an integer: 12 Enter another integer: 3 12 - 3 = 9 \u6b64\u65f6\u4f60\u53ef\u80fd\u4f1a\u8003\u8651\u4f7f\u7528 getInteger() \u518d\u8bfb\u53d6\u4e24\u4e2a\u6574\u6570\uff0c\u5c31\u50cf\u505a\u52a0\u6cd5\u65f6\u4e00\u6837\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 int main () { std :: cout << \"Addition \\n \" ; int a { getInteger () }; int b { getInteger () }; std :: cout << a << \" + \" << b << \" = \" << ( a + b ) << '\\n' ; std :: cout << \"Subtraction \\n \" ; int c { getInteger () }; int d { getInteger () }; std :: cout << c << \" - \" << d << \" = \" << ( c - d ) << '\\n' ; return 0 ; } \u4f46\u662f\uff0c\u8fd9\u6837\u5e76\u6ca1\u6709\u7528\uff0c\u8f93\u51fa\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 Addition Enter an integer: 5 Enter another integer: 9 5 + 9 = 14 Subtraction Enter another integer: 12 Enter another integer: 3 12 - 3 = 9 (\u8f93\u51fa\u7684\u8fd8\u662f \u201cEnter another integer\u201d \u800c\u4e0d\u662f \u201cEnter an integer\u201d) getInteger() \u662f\u4e0d\u53ef\u91cd\u7528\u7684\uff0c\u56e0\u5b83\u5177\u6709\u67d0\u79cd\u5185\u90e8\u72b6\u6001\uff08\u9759\u6001\u5c40\u90e8\u53d8\u91cf s_isFirstCall \uff09\uff0c\u800c\u8be5\u72b6\u6001\u5e76\u4e0d\u80fd\u591f\u4ece\u5916\u90e8\u88ab\u91cd\u7f6e\u3002 s_isFirstCall \u5e76\u4e0d\u662f\u4e00\u4e2a\u9700\u8981\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u4fdd\u6301\u552f\u4e00\u7684\u53d8\u91cf\uff0c\u5c3d\u7ba1\u4e0a\u9762\u7b2c\u4e00\u4e2a\u7a0b\u5e8f\u80fd\u591f\u6b63\u786e\u8fd0\u884c\uff0c\u4f46\u662f\u9759\u6001\u53d8\u91cf\u963b\u788d\u4e86\u6211\u4eec\u5bf9\u51fd\u6570\u7684\u91cd\u7528\u3002 \u5b9e\u73b0 getInteger \u7684\u66f4\u597d\u7684\u65b9\u6cd5\u662f\u5c06 s_isFirstCall \u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u3002\u8fd9\u6837\u4e3b\u8c03\u51fd\u6570\u5c31\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u6253\u5370\u7684\u5185\u5bb9\u3002 \u9759\u6001\u5c40\u90e8\u53d8\u91cf\u9002\u7528\u4e8e\u8be5\u53d8\u91cf\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\uff08\u6216\u53ef\u9884\u89c1\u7684\u672a\u6765\uff09\u9700\u8981\u4fdd\u6301\u552f\u4e00\u6027\u4e14\u65e0\u9700\u5bf9\u5176\u8fdb\u884c\u91cd\u7f6e\u7684\u60c5\u51b5\u3002 \u6700\u4f73\u5b9e\u8df5 \u9664\u975e\u53d8\u91cf\u6c38\u8fdc\u4e0d\u9700\u8981\u88ab\u91cd\u7f6e\uff0c\u5426\u5219\u8981\u907f\u514d\u4f7f\u7528\u9759\u6001\u5c40\u90e8\u53d8\u91cf","title":"\u4e0d\u8981\u4f7f\u7528\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u6765\u6539\u53d8\u6d41\u7a0b"},{"location":"6-11-Scope-duration-and-linkage-summary/","tags":["linkage","scope","C++11"],"text":"\u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5\u8fd9\u4e9b\u6982\u5ff5\u5f88\u7ed5\u4eba\uff0c\u6240\u4ee5\u6211\u4eec\u5229\u7528\u8fd9\u8282\u8bfe\u5bf9\u5b83\u4eec\u8fdb\u884c\u4e00\u6b21\u6574\u7406\u548c\u603b\u7ed3\u3002\u6709\u4e9b\u5185\u5bb9\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\uff0c\u4f46\u662f\u6211\u4eec\u4ecd\u7136\u5c06\u5176\u7f57\u5217\u5728\u8fd9\u91cc\uff0c\u5728\u540e\u7eed\u5b66\u4e60\u65f6\u53ef\u4ee5\u8fdb\u884c\u53c2\u8003\u3002 \u4f5c\u7528\u57df\u5c0f\u7ed3 \u53d8\u91cf\u6807\u8bc6\u7b26\u7684 \u4f5c\u7528\u57df \u51b3\u5b9a\u4e86\u6807\u8bc6\u7b26\u5728\u54ea\u91cc\u53ef\u4ee5\u88ab\u8bbf\u95ee\u3002 \u5177\u6709 \u5757\uff08\u5c40\u90e8\uff09\u4f5c\u7528\u57df \u7684\u6807\u8bc6\u7b26\uff0c\u53ea\u80fd\u591f\u5728\u58f0\u660e\u5b83\u4eec\u7684\u5757\uff08\u53ca\u5176\u5d4c\u5957\u5757\uff09\u4e2d\u8bbf\u95ee\uff0c\u8fd9\u4e9b\u53d8\u91cf\u5305\u62ec\uff1a \u5c40\u90e8\u53d8\u91cf \u51fd\u6570\u5f62\u53c2 \u5b9a\u4e49\u5728\u5757\u4e2d\u7684\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u578b\uff08\u5305\u62ec\u679a\u4e3e\u548c\u7c7b\uff09 \u5177\u6709 \u6587\u4ef6\uff08\u5168\u5c40\uff09\u4f5c\u7528\u57df \u7684\u53d8\u91cf\u548c\u51fd\u6570\u4ece\u58f0\u660e\u4f4d\u7f6e\u5f00\u59cb\uff0c\u76f4\u5230\u6587\u4ef6\u7ed3\u5c3e\u90fd\u53ef\u4ee5\u88ab\u8bbf\u95ee\uff0c \u5305\u62ec\uff1a \u5168\u5c40\u53d8\u91cf \u51fd\u6570 \u5b9a\u4e49\u5728\u5168\u5c40\u4f5c\u7528\u57df\u6216\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u578b \uff08\u5305\u62ec\u679a\u4e3e\u548c\u7c7b\uff09 \u6301\u7eed\u65f6\u95f4\u5c0f\u7ed3 \u53d8\u91cf\u7684\u6301\u7eed\u65f6\u95f4\u51b3\u5b9a\u4e86\u5b83\u5408\u9002\u88ab\u521b\u5efa\u548c\u9500\u6bc1\u3002 \u5177\u6709 \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u7684\u53d8\u91cf\uff0c\u5728\u5b9a\u4e49\u65f6\u88ab\u521b\u5efa\uff0c\u800c\u4e14\u5728\u79bb\u5f00\u8bed\u53e5\u5757\u65f6\u88ab\u9500\u6bc1\uff0c\u5305\u62ec\uff1a \u5c40\u90e8\u53d8\u91cf \u51fd\u6570\u5f62\u53c2 \u5177\u6709 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u7684\u53d8\u91cf\uff0c\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u521b\u5efa\uff0c\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u9500\u6bc1\uff0c\u5305\u62ec\uff1a \u5168\u5c40\u53d8\u91cf \u9759\u6001\u5c40\u90e8\u53d8\u91cf \u5177\u6709 \u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u65f6\u95f4\u7684\u53d8\u91cf\u5728\u53ef\u4ee5\u6839\u636e\u7a0b\u5e8f\u5458\u7684\u9700\u8981\u521b\u5efa\u548c\u9500\u6bc1\uff0c\u5305\u62ec\uff1a \u52a8\u6001\u5206\u914d\u7684\u53d8\u91cf \u94fe\u63a5\u5c0f\u7ed3 \u6807\u8bc6\u7b26\u7684\u94fe\u63a5\u5c5e\u6027\uff0c\u51b3\u5b9a\u4e86\u6807\u8bc6\u7b26\u7684\u591a\u4e2a\u58f0\u660e\u662f\u5426\u88ab\u770b\u505a\u540c\u4e00\u4e2a\u8868\u793a\u7b26\u3002 \u4e0d\u5177\u5907\u94fe\u63a5\u5c5e\u6027 \u7684\u6807\u8bc6\u7b26\uff0c\u610f\u5473\u7740\u8be5\u6807\u8bc6\u7b26\u53ea\u8868\u793a\u5b83\u81ea\u5df1\uff0c\u5305\u62ec\uff1a \u5c40\u90e8\u53d8\u91cf \u5b9a\u4e49\u5728\u5757\u4e2d\u7684\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u578b\uff08\u5305\u62ec\u679a\u4e3e\u548c\u7c7b\uff09 \u5177\u5907\u5185\u90e8\u94fe\u63a5\u5c5e\u6027 **\u7684\u6807\u8bc6\u7b26\u53ef\u4ee5\u5728\u6587\u4ef6\u5185\u90e8\u7684\u4efb\u4f55\u4f4d\u7f6e\u88ab\u8bbf\u95ee\uff0c\u5305\u62ec\uff1a \u9759\u6001\u5168\u5c40\u53d8\u91cf(\u521d\u59cb\u5316\u6216\u672a\u521d\u59cb\u5316\u7684) \u9759\u6001( static )\u51fd\u6570 const \u5168\u5c40\u53d8\u91cf \u5b9a\u4e49\u5728\u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u51fd\u6570 \u5b9a\u4e49\u5728\u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u578b\uff08\u5305\u62ec\u679a\u4e3e\u548c\u7c7b\uff09 \u5177\u5907\u5916\u90e8\u94fe\u63a5\u5c5e\u6027 \u7684\u6807\u8bc6\u7b26\u53ef\u4ee5\u5728\u5b9a\u4e49\u5b83\u7684\u6587\u4ef6\u4e2d\u8bbf\u95ee\uff0c\u4e5f\u53ef\u4ee5\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\uff08\u901a\u8fc7 \u524d\u5411\u58f0\u660e \uff09\u8bbf\u95ee\uff0c\u5305\u62ec\uff1a \u51fd\u6570 \u975e\u5e38\u91cf\u5168\u5c40\u53d8\u91cf(\u521d\u59cb\u5316\u6216\u672a\u521d\u59cb\u5316\u7684) \u5916\u90e8\u5e38\u91cf\u5168\u5c40\u53d8\u91cf \u5185\u8054\u5e38\u91cf\u5168\u5c40\u53d8\u91cf \u5b9a\u4e49\u5728\u547d\u540d\u7a7a\u95f4\u6216\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u7684\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u578b \uff08\u5305\u62ec\u679a\u4e3e\u548c\u7c7b\uff09 \u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u6807\u8bc6\u53f7\u5982\u679c\u5728\u591a\u4e2acpp\u6587\u4ef6\u4e2d\u88ab\u7f16\u8bd1\uff0c\u5219\u4f1a\u5bfc\u81f4\u94fe\u63a5\u5668\u62a5\u9519\uff08\u56e0\u4e3a\u8fdd\u53cd\u4e86 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \uff09\u3002\u4e0d\u8fc7\uff0c\u4e5f\u6709\u4f8b\u5916\u7684\u60c5\u51b5\uff08\u5bf9\u4e8e\u7c7b\u578b\u3001\u76ee\u6807\u3001\u5185\u8054\u51fd\u6570\u548c\u5185\u8054\u53d8\u91cf\uff09\u2014\u2014\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u76f8\u5173\u5185\u5bb9\u3002 \u540c\u65f6\uff0c\u6ce8\u610f \u51fd\u6570\u9ed8\u8ba4\u5177\u6709\u5916\u90e8\u94fe\u63a5 \uff0c\u5fc5\u987b\u8981\u4f7f\u7528 static \u624d\u80fd\u4f7f\u5176\u5177\u6709\u5185\u90e8\u94fe\u63a5\u3002 \u53d8\u91cf\u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5\u5c0f\u7ed3 \u56e0\u4e3a\u53d8\u91cf\u5177\u6709\u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5\u5c5e\u6027\uff0c\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u8868\u683c\u5bf9\u5176\u8fdb\u884c\u603b\u7ed3\uff1a \u7c7b\u578b \u4f8b\u5b50 \u4f5c\u7528\u57df \u6301\u7eed\u65f6\u95f4 \u94fe\u63a5\u5c5e\u6027 \u5907\u6ce8 \u5c40\u90e8\u53d8\u91cf int x; \u5757 \u81ea\u52a8 \u65e0 \u9759\u6001\u5c40\u90e8\u53d8\u91cf static int s_x; \u5757 \u9759\u6001 \u65e0 \u52a8\u6001\u53d8\u91cf int *x { new int{} }; \u5757 \u52a8\u6001 \u65e0 \u51fd\u6570\u5f62\u53c2 void foo(int x) \u5757 \u81ea\u52a8 \u65e0 \u5916\u90e8\u975e\u5e38\u91cf\u5168\u5c40\u53d8\u91cf int g_x; \u6587\u4ef6 \u9759\u6001 \u5916\u90e8 \u521d\u59cb\u5316\u6216\u672a\u521d\u59cb\u5316 \u5185\u90e8\u975e\u5e38\u91cf\u5168\u5c40\u53d8\u91cf static int g_x; \u6587\u4ef6 \u9759\u6001 \u5185\u90e8 \u521d\u59cb\u5316\u6216\u672a\u521d\u59cb\u5316 \u5185\u90e8\u5e38\u91cf\u5168\u5c40\u53d8\u91cf constexpr int g_x { 1 }; \u6587\u4ef6 \u9759\u6001 \u5185\u90e8 \u5fc5\u987b\u521d\u59cb\u5316 \u5916\u90e8\u5e38\u91cf\u5168\u5c40\u53d8\u91cf extern const int g_x { 1 }; \u6587\u4ef6 \u9759\u6001 \u5916\u90e8 \u5fc5\u987b\u521d\u59cb\u5316 \u5185\u8054\u5e38\u91cf\u5168\u5c40\u53d8\u91cf(C++17) inline constexpr int g_x { 1 }; \u6587\u4ef6 \u9759\u6001 \u5916\u90e8 \u5fc5\u987b\u521d\u59cb\u5316 \u524d\u5411\u58f0\u660e\u5c0f\u7ed3 \u4f7f\u7528\u524d\u5411\u58f0\u660e\u53ef\u4ee5\u4ece\u5176\u4ed6\u6587\u4ef6\u4e2d\u8bbf\u95ee\u4e00\u4e2a\u51fd\u6570\u6216\u8005\u53d8\u91cf\u3002\u58f0\u660e\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4e0e\u5f80\u5e38\u4e00\u6837(\u5168\u5c40\u53d8\u91cf\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\uff0c\u5c40\u90e8\u53d8\u91cf\u5177\u6709\u5757\u4f5c\u7528\u57df)\u3002 \u7c7b\u578b \u4f8b\u5b50 \u5907\u6ce8 \u51fd\u6570\u524d\u5411\u58f0\u660e void foo(int x); \u53ea\u9700\u8981\u51fd\u6570\u539f\u578b\uff0c\u4e0d\u80fd\u6709\u51fd\u6570\u4f53 \u975e\u5e38\u91cf\u7684\u53d8\u91cf\u524d\u5411\u58f0\u660e extern int g_x; \u5fc5\u987b\u672a\u521d\u59cb\u5316 \u5e38\u91cf\u7684\u524d\u5411\u58f0\u660e extern const int g_x; \u5fc5\u987b\u672a\u521d\u59cb\u5316 Constexpr \u53d8\u91cf\u524d\u5411\u58f0\u660e extern constexpr int g_x; \u4e0d\u5141\u8bb8\uff0c constexpr \u4e0d\u80fd\u524d\u5411\u58f0\u660e \u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26\u5230\u5e95\u662f\u4ec0\u4e48\uff1f \u5f53 static \u548c extern \u5173\u952e\u5b57\uff08\u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26\uff09\u4f5c\u4e3a\u53d8\u91cf\u58f0\u660e\u7684\u4e00\u90e8\u5206\u65f6\uff0c\u5b83\u4eec\u53ef\u4ee5\u8bbe\u5b9a\u6807\u8bc6\u7b26\u7684 \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u548c\u94fe\u63a5\u5c5e\u6027\u3002 C++ \u652f\u6301 4 \u79cd\u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26\uff1a \u8bf4\u660e\u7b26 \u542b\u4e49 \u5907\u6ce8 extern static (\u6216 thread_local) \u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u548c\u5916\u90e8\u94fe\u63a5 static static (\u6216 thread_local) \u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u548c\u5185\u90e8\u94fe\u63a5 thread_local thread \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 mutable \u5373\u4f7f\u7c7b\u662fconst\u7c7b\u578b\u7684\uff0c\u53d8\u91cf\u4e5f\u5141\u8bb8\u4fee\u6539 auto \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u5728 C++11 \u4e2d\u5df2\u7ecf\u5f03\u7528 register \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u5e76\u4e14\u63d0\u793a\u7f16\u8bd1\u5668\u5c06\u5176\u5b58\u653e\u5728\u5bc4\u5b58\u5668\u4e2d \u5728 C++17 \u4e2d\u5df2\u7ecf\u5f03\u7528 \u672f\u8bed \u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26 \u901a\u5e38\u53ea\u4f1a\u5728\u6b63\u5f0f\u7684\u6587\u6863\u4e2d\u4f7f\u7528\u3002","title":"6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3"},{"location":"6-11-Scope-duration-and-linkage-summary/#_1","text":"\u53d8\u91cf\u6807\u8bc6\u7b26\u7684 \u4f5c\u7528\u57df \u51b3\u5b9a\u4e86\u6807\u8bc6\u7b26\u5728\u54ea\u91cc\u53ef\u4ee5\u88ab\u8bbf\u95ee\u3002 \u5177\u6709 \u5757\uff08\u5c40\u90e8\uff09\u4f5c\u7528\u57df \u7684\u6807\u8bc6\u7b26\uff0c\u53ea\u80fd\u591f\u5728\u58f0\u660e\u5b83\u4eec\u7684\u5757\uff08\u53ca\u5176\u5d4c\u5957\u5757\uff09\u4e2d\u8bbf\u95ee\uff0c\u8fd9\u4e9b\u53d8\u91cf\u5305\u62ec\uff1a \u5c40\u90e8\u53d8\u91cf \u51fd\u6570\u5f62\u53c2 \u5b9a\u4e49\u5728\u5757\u4e2d\u7684\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u578b\uff08\u5305\u62ec\u679a\u4e3e\u548c\u7c7b\uff09 \u5177\u6709 \u6587\u4ef6\uff08\u5168\u5c40\uff09\u4f5c\u7528\u57df \u7684\u53d8\u91cf\u548c\u51fd\u6570\u4ece\u58f0\u660e\u4f4d\u7f6e\u5f00\u59cb\uff0c\u76f4\u5230\u6587\u4ef6\u7ed3\u5c3e\u90fd\u53ef\u4ee5\u88ab\u8bbf\u95ee\uff0c \u5305\u62ec\uff1a \u5168\u5c40\u53d8\u91cf \u51fd\u6570 \u5b9a\u4e49\u5728\u5168\u5c40\u4f5c\u7528\u57df\u6216\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u578b \uff08\u5305\u62ec\u679a\u4e3e\u548c\u7c7b\uff09","title":"\u4f5c\u7528\u57df\u5c0f\u7ed3"},{"location":"6-11-Scope-duration-and-linkage-summary/#_2","text":"\u53d8\u91cf\u7684\u6301\u7eed\u65f6\u95f4\u51b3\u5b9a\u4e86\u5b83\u5408\u9002\u88ab\u521b\u5efa\u548c\u9500\u6bc1\u3002 \u5177\u6709 \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u7684\u53d8\u91cf\uff0c\u5728\u5b9a\u4e49\u65f6\u88ab\u521b\u5efa\uff0c\u800c\u4e14\u5728\u79bb\u5f00\u8bed\u53e5\u5757\u65f6\u88ab\u9500\u6bc1\uff0c\u5305\u62ec\uff1a \u5c40\u90e8\u53d8\u91cf \u51fd\u6570\u5f62\u53c2 \u5177\u6709 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u7684\u53d8\u91cf\uff0c\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u521b\u5efa\uff0c\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u9500\u6bc1\uff0c\u5305\u62ec\uff1a \u5168\u5c40\u53d8\u91cf \u9759\u6001\u5c40\u90e8\u53d8\u91cf \u5177\u6709 \u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u65f6\u95f4\u7684\u53d8\u91cf\u5728\u53ef\u4ee5\u6839\u636e\u7a0b\u5e8f\u5458\u7684\u9700\u8981\u521b\u5efa\u548c\u9500\u6bc1\uff0c\u5305\u62ec\uff1a \u52a8\u6001\u5206\u914d\u7684\u53d8\u91cf","title":"\u6301\u7eed\u65f6\u95f4\u5c0f\u7ed3"},{"location":"6-11-Scope-duration-and-linkage-summary/#_3","text":"\u6807\u8bc6\u7b26\u7684\u94fe\u63a5\u5c5e\u6027\uff0c\u51b3\u5b9a\u4e86\u6807\u8bc6\u7b26\u7684\u591a\u4e2a\u58f0\u660e\u662f\u5426\u88ab\u770b\u505a\u540c\u4e00\u4e2a\u8868\u793a\u7b26\u3002 \u4e0d\u5177\u5907\u94fe\u63a5\u5c5e\u6027 \u7684\u6807\u8bc6\u7b26\uff0c\u610f\u5473\u7740\u8be5\u6807\u8bc6\u7b26\u53ea\u8868\u793a\u5b83\u81ea\u5df1\uff0c\u5305\u62ec\uff1a \u5c40\u90e8\u53d8\u91cf \u5b9a\u4e49\u5728\u5757\u4e2d\u7684\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u578b\uff08\u5305\u62ec\u679a\u4e3e\u548c\u7c7b\uff09 \u5177\u5907\u5185\u90e8\u94fe\u63a5\u5c5e\u6027 **\u7684\u6807\u8bc6\u7b26\u53ef\u4ee5\u5728\u6587\u4ef6\u5185\u90e8\u7684\u4efb\u4f55\u4f4d\u7f6e\u88ab\u8bbf\u95ee\uff0c\u5305\u62ec\uff1a \u9759\u6001\u5168\u5c40\u53d8\u91cf(\u521d\u59cb\u5316\u6216\u672a\u521d\u59cb\u5316\u7684) \u9759\u6001( static )\u51fd\u6570 const \u5168\u5c40\u53d8\u91cf \u5b9a\u4e49\u5728\u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u51fd\u6570 \u5b9a\u4e49\u5728\u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u578b\uff08\u5305\u62ec\u679a\u4e3e\u548c\u7c7b\uff09 \u5177\u5907\u5916\u90e8\u94fe\u63a5\u5c5e\u6027 \u7684\u6807\u8bc6\u7b26\u53ef\u4ee5\u5728\u5b9a\u4e49\u5b83\u7684\u6587\u4ef6\u4e2d\u8bbf\u95ee\uff0c\u4e5f\u53ef\u4ee5\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\uff08\u901a\u8fc7 \u524d\u5411\u58f0\u660e \uff09\u8bbf\u95ee\uff0c\u5305\u62ec\uff1a \u51fd\u6570 \u975e\u5e38\u91cf\u5168\u5c40\u53d8\u91cf(\u521d\u59cb\u5316\u6216\u672a\u521d\u59cb\u5316\u7684) \u5916\u90e8\u5e38\u91cf\u5168\u5c40\u53d8\u91cf \u5185\u8054\u5e38\u91cf\u5168\u5c40\u53d8\u91cf \u5b9a\u4e49\u5728\u547d\u540d\u7a7a\u95f4\u6216\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u7684\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u578b \uff08\u5305\u62ec\u679a\u4e3e\u548c\u7c7b\uff09 \u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u6807\u8bc6\u53f7\u5982\u679c\u5728\u591a\u4e2acpp\u6587\u4ef6\u4e2d\u88ab\u7f16\u8bd1\uff0c\u5219\u4f1a\u5bfc\u81f4\u94fe\u63a5\u5668\u62a5\u9519\uff08\u56e0\u4e3a\u8fdd\u53cd\u4e86 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \uff09\u3002\u4e0d\u8fc7\uff0c\u4e5f\u6709\u4f8b\u5916\u7684\u60c5\u51b5\uff08\u5bf9\u4e8e\u7c7b\u578b\u3001\u76ee\u6807\u3001\u5185\u8054\u51fd\u6570\u548c\u5185\u8054\u53d8\u91cf\uff09\u2014\u2014\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u76f8\u5173\u5185\u5bb9\u3002 \u540c\u65f6\uff0c\u6ce8\u610f \u51fd\u6570\u9ed8\u8ba4\u5177\u6709\u5916\u90e8\u94fe\u63a5 \uff0c\u5fc5\u987b\u8981\u4f7f\u7528 static \u624d\u80fd\u4f7f\u5176\u5177\u6709\u5185\u90e8\u94fe\u63a5\u3002","title":"\u94fe\u63a5\u5c0f\u7ed3"},{"location":"6-11-Scope-duration-and-linkage-summary/#_4","text":"\u56e0\u4e3a\u53d8\u91cf\u5177\u6709\u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5\u5c5e\u6027\uff0c\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u8868\u683c\u5bf9\u5176\u8fdb\u884c\u603b\u7ed3\uff1a \u7c7b\u578b \u4f8b\u5b50 \u4f5c\u7528\u57df \u6301\u7eed\u65f6\u95f4 \u94fe\u63a5\u5c5e\u6027 \u5907\u6ce8 \u5c40\u90e8\u53d8\u91cf int x; \u5757 \u81ea\u52a8 \u65e0 \u9759\u6001\u5c40\u90e8\u53d8\u91cf static int s_x; \u5757 \u9759\u6001 \u65e0 \u52a8\u6001\u53d8\u91cf int *x { new int{} }; \u5757 \u52a8\u6001 \u65e0 \u51fd\u6570\u5f62\u53c2 void foo(int x) \u5757 \u81ea\u52a8 \u65e0 \u5916\u90e8\u975e\u5e38\u91cf\u5168\u5c40\u53d8\u91cf int g_x; \u6587\u4ef6 \u9759\u6001 \u5916\u90e8 \u521d\u59cb\u5316\u6216\u672a\u521d\u59cb\u5316 \u5185\u90e8\u975e\u5e38\u91cf\u5168\u5c40\u53d8\u91cf static int g_x; \u6587\u4ef6 \u9759\u6001 \u5185\u90e8 \u521d\u59cb\u5316\u6216\u672a\u521d\u59cb\u5316 \u5185\u90e8\u5e38\u91cf\u5168\u5c40\u53d8\u91cf constexpr int g_x { 1 }; \u6587\u4ef6 \u9759\u6001 \u5185\u90e8 \u5fc5\u987b\u521d\u59cb\u5316 \u5916\u90e8\u5e38\u91cf\u5168\u5c40\u53d8\u91cf extern const int g_x { 1 }; \u6587\u4ef6 \u9759\u6001 \u5916\u90e8 \u5fc5\u987b\u521d\u59cb\u5316 \u5185\u8054\u5e38\u91cf\u5168\u5c40\u53d8\u91cf(C++17) inline constexpr int g_x { 1 }; \u6587\u4ef6 \u9759\u6001 \u5916\u90e8 \u5fc5\u987b\u521d\u59cb\u5316","title":"\u53d8\u91cf\u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5\u5c0f\u7ed3"},{"location":"6-11-Scope-duration-and-linkage-summary/#_5","text":"\u4f7f\u7528\u524d\u5411\u58f0\u660e\u53ef\u4ee5\u4ece\u5176\u4ed6\u6587\u4ef6\u4e2d\u8bbf\u95ee\u4e00\u4e2a\u51fd\u6570\u6216\u8005\u53d8\u91cf\u3002\u58f0\u660e\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4e0e\u5f80\u5e38\u4e00\u6837(\u5168\u5c40\u53d8\u91cf\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\uff0c\u5c40\u90e8\u53d8\u91cf\u5177\u6709\u5757\u4f5c\u7528\u57df)\u3002 \u7c7b\u578b \u4f8b\u5b50 \u5907\u6ce8 \u51fd\u6570\u524d\u5411\u58f0\u660e void foo(int x); \u53ea\u9700\u8981\u51fd\u6570\u539f\u578b\uff0c\u4e0d\u80fd\u6709\u51fd\u6570\u4f53 \u975e\u5e38\u91cf\u7684\u53d8\u91cf\u524d\u5411\u58f0\u660e extern int g_x; \u5fc5\u987b\u672a\u521d\u59cb\u5316 \u5e38\u91cf\u7684\u524d\u5411\u58f0\u660e extern const int g_x; \u5fc5\u987b\u672a\u521d\u59cb\u5316 Constexpr \u53d8\u91cf\u524d\u5411\u58f0\u660e extern constexpr int g_x; \u4e0d\u5141\u8bb8\uff0c constexpr \u4e0d\u80fd\u524d\u5411\u58f0\u660e","title":"\u524d\u5411\u58f0\u660e\u5c0f\u7ed3"},{"location":"6-11-Scope-duration-and-linkage-summary/#_6","text":"\u5f53 static \u548c extern \u5173\u952e\u5b57\uff08\u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26\uff09\u4f5c\u4e3a\u53d8\u91cf\u58f0\u660e\u7684\u4e00\u90e8\u5206\u65f6\uff0c\u5b83\u4eec\u53ef\u4ee5\u8bbe\u5b9a\u6807\u8bc6\u7b26\u7684 \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u548c\u94fe\u63a5\u5c5e\u6027\u3002 C++ \u652f\u6301 4 \u79cd\u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26\uff1a \u8bf4\u660e\u7b26 \u542b\u4e49 \u5907\u6ce8 extern static (\u6216 thread_local) \u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u548c\u5916\u90e8\u94fe\u63a5 static static (\u6216 thread_local) \u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u548c\u5185\u90e8\u94fe\u63a5 thread_local thread \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 mutable \u5373\u4f7f\u7c7b\u662fconst\u7c7b\u578b\u7684\uff0c\u53d8\u91cf\u4e5f\u5141\u8bb8\u4fee\u6539 auto \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u5728 C++11 \u4e2d\u5df2\u7ecf\u5f03\u7528 register \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u5e76\u4e14\u63d0\u793a\u7f16\u8bd1\u5668\u5c06\u5176\u5b58\u653e\u5728\u5bc4\u5b58\u5668\u4e2d \u5728 C++17 \u4e2d\u5df2\u7ecf\u5f03\u7528 \u672f\u8bed \u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26 \u901a\u5e38\u53ea\u4f1a\u5728\u6b63\u5f0f\u7684\u6587\u6863\u4e2d\u4f7f\u7528\u3002","title":"\u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26\u5230\u5e95\u662f\u4ec0\u4e48\uff1f"},{"location":"6-12-Using-declarations-and-using%20directives/","tags":["using"],"text":"Key Takeaway \u51fd\u6570\u5185\u90e8\u4f7f\u7528 using \u58f0\u660e\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\uff0c\u540c\u65f6\u4e5f\u662f\u5b89\u5168\u7684 \u660e\u786e\u6307\u5b9a\u4f5c\u7528\u57df\u597d\u8fc7 using \u8bed\u53e5\u3002\u5c3d\u53ef\u80fd\u5730\u907f\u514d\u4f7f\u7528 using \u6307\u4ee4\u3002\u5728\u8bed\u53e5\u5757\u4e2d\u4f7f\u7528 using \u58f0\u660e\u901a\u5e38\u662f\u53ef\u4ee5\u7684\u3002 \u4f60\u53ef\u80fd\u5728\u5f88\u591a\u6559\u6750\u6216\u8005\u6559\u7a0b\u4e2d\u90fd\u770b\u8fc7\u8fd9\u6837\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 #include using namespace std ; int main () { cout << \"Hello world!\" ; return 0 ; } \u6709\u4e00\u4e9b\u53e4\u65e9\u7684 IDE \u751a\u81f3\u4f1a\u81ea\u52a8\u4e3a\u4f60\u65b0\u5efa\u8fd9\u6837\u7684 C++ \u9879\u76ee\uff08\u4ee5\u4fbf\u4f60\u53ef\u4ee5\u5feb\u901f\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\uff09\u3002 \u5982\u679c\u4f60\u770b\u5230\u8fd9\u6837\u7684\u4ee3\u7801\uff0c\u5343\u4e07\u8981\u539f\u8c05\u8fd9\u4e9b\u8bfe\u672c\u3001\u6559\u7a0b\u6216\u662f\u7f16\u8bd1\u5668\uff0c\u5b83\u4eec\u5f88\u53ef\u80fd\u5df2\u7ecf\u8fc7\u65f6\u4e86\u3002\u4e3a\u4ec0\u4e48\uff1f\u4e0a\u5b8c\u8fd9\u8282\u8bfe\u4f60\u5c31\u77e5\u9053\u4e86\u3002 \u5386\u53f2\u8bfe \u5728 C++ \u5f00\u59cb\u652f\u6301\u547d\u540d\u7a7a\u95f4\u4e4b\u524d\uff0c\u6240\u6709\u73b0\u5728\u4f4d\u4e8e std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\uff0c\u5f7c\u65f6\u90fd\u4f4d\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u8fd9\u4f7f\u5f97\u7528\u6237\u7684\u547d\u540d\u5f88\u5bb9\u6613\u548c\u6807\u51c6\u5e93\u4e2d\u7684\u540d\u79f0\u53d1\u751f\u51b2\u7a81\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u5728\u67d0\u4e2a C++\u7248\u672c\u4e0b\u53ef\u4ee5\u8fd0\u884c\u7684\u7a0b\u5e8f\uff0c\u5f53\u4f7f\u7528\u4e86\u65b0\u7248\u672c\u7684 C++ \u65f6\u4e5f\u5f88\u53ef\u80fd\u65e0\u6cd5\u5de5\u4f5c\u3002 \u547d\u540d\u7a7a\u95f4\u7684\u6807\u51c6\u5316\u662f\u5728 1995 \u5e74\u5b8c\u6210\u7684\uff0c\u4ece\u90a3\u65f6\u8d77\uff0c\u6240\u6709\u6807\u51c6\u5e93\u4e2d\u63d0\u4f9b\u7684\u529f\u80fd\uff0c\u90fd\u88ab\u79fb\u52a8\u5230\u4e86 std \u547d\u540d\u7a7a\u95f4\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4e0d\u5177\u6709 std:: \u524d\u7f00\u7684\u4ee3\u7801\u5c31\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u4e86\u3002 \u7ef4\u62a4\u8fc7\u5927\u578b\u9879\u76ee\u7684\u4eba\u90fd\u77e5\u9053\uff0c\u4efb\u4f55\u4e00\u70b9\u6539\u52a8\u90fd\u6709\u53ef\u80fd\u7834\u574f\u6574\u4e2a\u7a0b\u5e8f\uff08\u4e0d\u8bba\u6539\u52a8\u662f\u591a\u4e48\u7684\u4e0d\u8d77\u773c\uff09\u3002\u56e0\u6b64\uff0c\u4e3a\u6240\u6709\u76f8\u5173\u7684\u6807\u8bc6\u7b26\u6dfb\u52a0 std:: \u524d\u7f00\u7684\u98ce\u9669\u975e\u5e38\u5927\u3002\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u63d0\u4f9b\u4e00\u79cd\u66f4\u52a0\u6709\u6548\u7684\u65b9\u5f0f\u3002 \u65f6\u95f4\u4e00\u6643\u5230\u4e86\u4eca\u5929\u2014\u2014\u5982\u679c\u4f60\u5927\u91cf\u4f7f\u7528\u4e86\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u529f\u80fd\uff0c\u4e3a\u6bcf\u4e00\u4e2a\u76f8\u5173\u7684\u6807\u8bc6\u7b26\u6dfb\u52a0 std:: \u4e5f\u662f\u4e00\u79cd\u975e\u5e38\u91cd\u590d\u7684\u5de5\u4f5c\uff0c\u800c\u4e14\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4f1a\u8ba9\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u53d8\u5dee\u3002 \u4e3a\u6b64\uff0cC++ \u4e3a\u8fd9\u4e9b\u95ee\u9898\u63d0\u4f9b\u4e86\u4e00\u79cd\u89e3\u51b3\u65b9\u6cd5\uff0c\u5373 using \u8bed\u53e5\u3002 \u5728\u7ee7\u7eed\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u6765\u4ecb\u7ecd\u4e24\u4e2a\u672f\u8bed \u9650\u5b9a\u540d\u548c\u975e\u9650\u5b9a\u540d \u4e00\u4e2a\u53d8\u91cf\u540d\uff08\u51fd\u6570\u540d\uff09\u53ef\u4ee5\u662f\u9650\u5b9a\u540d\uff08qualified\uff09\u6216\u8005\u975e\u9650\u5b9a\u540d\uff08unqualified\uff09\u3002 \u5305\u542b\u4e86\u4f5c\u7528\u57df\u4fe1\u606f\u7684\u540d\u5b57\uff0c\u79f0\u4e3a\u9650\u5b9a\u540d\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 ( :: )\u53ef\u4ee5\u521b\u5efa\u9650\u5b9a\u540d\u3002\u4f8b\u5982\uff1a 1 2 std :: cout // identifier cout is qualified by namespace std :: foo // identifier foo is qualified by the global namespace \u6269\u5c55\u9605\u8bfb \u7ed3\u5408\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u548c\u7c7b\u540d\u4e5f\u53ef\u4ee5\u521b\u5efa\u9650\u5b9a\u540d\uff0c\u6216\u8005\u5bf9\u7c7b\u5bf9\u8c61\u4f7f\u7528\u6210\u5458\u9009\u62e9\u64cd\u4f5c\u7b26\uff08 . \u6216 -> \uff09\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 class C ; // some class C :: s_member ; // s_member is qualified by class C obj . x ; // x is qualified by class object obj ptr -> y ; // y is qualified by pointer to class object ptr \u800c\u4e0d\u5177\u6709\u4f5c\u7528\u57df\u9650\u5b9a\u7b26\u7684\u53d8\u91cf\u540d\uff0c\u5219\u4e3a\u975e\u9650\u5b9a\u540d\u3002\u4f8b\u5982 cout \u548c x \u90fd\u662f\u975e\u9650\u5b9a\u540d\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e0d\u5305\u542b\u4efb\u4f55\u7684\u4f5c\u7528\u57df\u4fe1\u606f\u3002 Using \u58f0\u660e \u51cf\u5c11\u91cd\u590d\u8f93\u5165 std:: \u7684\u4e00\u4e2a\u65b9\u6cd5\u662f\u4f7f\u7528 using \u58f0\u660e\u8bed\u53e5\u3002using \u58f0\u660e\u8bed\u53e5\u4f7f\u6211\u4eec\u53ef\u4ee5\u5c06\u975e\u9650\u5b9a\u540d\u4f5c\u4e3a\u9650\u5b9a\u540d\u7684\u522b\u540d\u6765\u4f7f\u7528\u3002 \u4e0b\u9762\u8fd9\u4e2a\u7ecf\u5178\u7684 Hello World \u7a0b\u5e8f\uff0c\u5728\u7b2c\u4e94\u884c\u4f7f\u7528\u4e86 using \u58f0\u660e\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { using std :: cout ; // this using declaration tells the compiler that cout should resolve to std::cout cout << \"Hello world!\" ; // so no std:: prefix is needed here! return 0 ; } // the using declaration expires here using std:: cout; \u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u6211\u4eec\u8981\u4f7f\u7528 std namespace \u4e2d\u7684 cout \uff0c\u6240\u4ee5\u5728\u4efb\u4f55\u4f7f\u7528 cout \u7684\u5730\u65b9\uff0c\u7f16\u8bd1\u5668\u90fd\u4f1a\u5047\u5b9a\u5b83\u7b49\u4ef7\u4e8e std:: cout \u3002\u5982\u679c\u5728\u9047\u5230 std:: cout \u548c\u5176\u4ed6 cout \u77db\u76fe\u7684\u5730\u65b9\uff0c\u5219\u4f18\u5148\u8ba4\u4e3a\u662f std:: cout \u3002\u56e0\u6b64\uff0c\u5728\u7b2c\u516d\u884c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 cout \u800c\u65e0\u9700\u4f7f\u7528 std:: cout \u3002 \u4ece\u8fd9\u4e2a\u5c0f\u4f8b\u5b50\u6211\u4eec\u5f88\u96be\u770b\u51fa using \u8bed\u53e5\u80fd\u591f\u5e26\u6765\u7684\u6536\u76ca\uff0c\u4f46\u662f\u5f53\u4f60\u5728\u51fd\u6570\u4e2d\u5927\u91cf\u4f7f\u7528 cout \u7684\u65f6\u5019\uff0c using \u58f0\u660e\u53ef\u4ee5\u8ba9\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u53d8\u5f97\u66f4\u597d\u3002\u6ce8\u610f\uff0c\u5bf9\u4e8e\u4e0d\u540c\u7684\u53d8\u91cf\u540d\uff0c\u9700\u8981\u5355\u72ec\u7684 using \u58f0\u660e\uff08\u4f8b\u5982 std:: cout \u662f\u4e00\u4e2a\uff0c std:: cin \u662f\u53e6\u4e00\u4e2a\uff09\u3002 \u5c3d\u7ba1\u8fd9\u79cd\u65b9\u6cd5\u6ca1\u6709\u4f7f\u7528 std:: \u524d\u7f00\u90a3\u4e48\u660e\u786e\uff0c\u4f46\u901a\u5e38\u4e5f\u88ab\u8ba4\u4e3a\u662f\u4e00\u79cd\u5b89\u5168\u7684\u3001\u53ef\u63a5\u53d7\u7684\u65b9\u6cd5\uff08\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\uff09\u3002 Using \u6307\u4ee4 \u53e6\u5916\u4e00\u79cd\u7b80\u5316\u65b9\u5f0f\u662f\u4f7f\u7528 using \u6307\u4ee4\uff0c\u5b83\u53ef\u4ee5\u5c06\u8be5\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u90fd\u5bfc\u5165\u5230\u4f7f\u7528\u4e86\u8be5 using \u6307\u4ee4\u7684\u4f5c\u7528\u57df\u3002 \u6269\u5c55\u9605\u8bfb \u4ece\u6280\u672f\u89d2\u5ea6\u6765\u8bb2\uff0c\u4f7f\u7528 using \u6307\u4ee4\u5176\u5b9e\u5e76\u6ca1\u6709\u5c06\u6807\u8bc6\u7b26\u5bfc\u5165\u5230\u5f53\u524d\u7684\u4f5c\u7528\u57df\u2014\u2014\u5b9e\u9645\u4e0a\u5b83\u4eec\u88ab\u5bfc\u5165\u5230\u5916\u5c42\u4f5c\u7528\u57df\uff08\u5173\u4e8e\u5916\u5c42\u4f5c\u7528\u57df\u7684\u8be6\u7ec6\u4fe1\u606f\u53ef\u4ee5\u53c2\u8003 \u8fd9\u91cc \uff09\u3002\u4e0d\u8fc7\uff0c\u5728\u5916\u5c42\u4f5c\u7528\u57df\u4e2d\u5e76\u4e0d\u80fd\u8bbf\u95ee\u8fd9\u4e9b\u53d8\u91cf\u540d\uff0c\u5b83\u4eec\u53ea\u80fd\u5728\u4f7f\u7528\u4e86 using \u6307\u4ee4\u7684\u4f5c\u7528\u57df\u4e2d\u8bbf\u95ee\u3002 \u4ece\u5b9e\u9645\u6548\u679c\u6765\u770b\uff0c\u4f7f\u7528 using \u6307\u4ee4\u7684\u884c\u4e3a\u770b\u4e0a\u53bb\u5c31\u50cf\u662f\u53d8\u91cf\u540d\u88ab\u5bfc\u5165\u5230\u4e86\u5f53\u524d\u4f5c\u7528\u57df\u3002\u4e3a\u4e86\u4e0d\u628a\u4e8b\u60c5\u641e\u590d\u6742\uff0c\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4f1a\u6cbf\u7528\u8fd9\u79cd\u7b80\u5316\u7684\u8bf4\u6cd5\uff0c\u5373\u53d8\u91cf\u540d\u88ab \u5bfc\u5165 \u5230\u4e86\u5f53\u524d\u4f5c\u7528\u57df\u3002 \u518d\u56de\u8fc7\u5934\u770b Hello World \u7a0b\u5e8f\uff0c\u7b2c\u4e94\u884c\u4f7f\u7528\u4e86 using \u6307\u4ee4\uff1a 1 2 3 4 5 6 7 8 #include int main () { using namespace std ; // this using directive tells the compiler to import all names from namespace std into the current namespace without qualification cout << \"Hello world!\" ; // so no std:: prefix is needed here return 0 ; } using namespace std; \u544a\u8bc9\u7f16\u8bd1\u5668\u5c06 std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684 \u5168\u90e8 \u6807\u8bc6\u7b26\u5bfc\u5165\u5230\u5f53\u524d\u4f5c\u7528\u57df\uff08\u4f8b\u5982 main() \u51fd\u6570\uff09\u3002\u5f53\u6211\u4eec\u4f7f\u7528\u975e\u9650\u5b9a\u6807\u8bc6\u7b26 cout \u7684\u65f6\u5019\uff0c\u5b83\u5c31\u4f1a\u88ab\u89e3\u6790\u4e3a\u5bfc\u5165\u7684 std:: cout \u3002 Using \u6307\u4ee4\u662f\u4e3a\u4e86\u90a3\u4e9b\u5728\u547d\u540d\u7a7a\u95f4\u53d1\u660e\u524d\u5c31\u5b58\u5728\u7684\u9879\u76ee\u800c\u8bbe\u8ba1\u7684\u3002\u5728\u8fd9\u79cd\u53e4\u8001\u7684\u9879\u76ee\u4e2d\uff0c\u6709\u4e86 using \u6307\u4ee4\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u4e0d\u5fc5\u9010\u4e00\u5730\u5c06\u975e\u9650\u5b9a\u540d\u4fee\u6539\u4e3a\u9650\u5b9a\u540d\uff08\u8fd9\u4e48\u505a\u98ce\u9669\u975e\u5e38\u5927\uff09\uff0c\u53ea\u9700\u5c06 using namespace std; \u653e\u7f6e\u5728\u6587\u4ef6\u7684\u5f00\u59cb\u5904\uff0c\u90a3\u4e48\u6240\u6709\u7684\u5df2\u7ecf\u88ab\u79fb\u52a8\u5230\u4e86 std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\uff0c\u4ecd\u7136\u53ef\u4ee5\u4ee5\u975e\u9650\u5b9a\u540d\u7684\u65b9\u5f0f\u6765\u4f7f\u7528\u3002 Using \u6307\u4ee4\u5e26\u6765\u7684\u95ee\u9898 (\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528 \u201cusing namespace std;\u201d) \u5728\u73b0\u4ee3 C++ \u4e2d\uff0c\u4f7f\u7528 using \u6307\u4ee4\u901a\u5e38\u662f\u5f0a\u5927\u4e8e\u5229\u7684\uff08\u9664\u4e86\u80fd\u5c11\u6253\u5b57\u4ee5\u5916\uff09\u3002\u56e0\u4e3a\u4f7f\u7528 using \u6307\u4ee4\u5bfc\u5165\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6240\u6709\u6807\u8bc6\u7b26\u4f1a\u6781\u5927\u5730\u63d0\u9ad8\u547d\u540d\u51b2\u7a81\u7684\u53ef\u80fd\uff08\u5c24\u5176\u662f\u5bfc\u5165 std \uff09\u3002 \u901a\u8fc7\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230 using \u6307\u4ee4\u53ef\u80fd\u5e26\u6765\u7684\u4e8c\u4e49\u6027\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include namespace a { int x { 10 }; } namespace b { int x { 20 }; } int main () { using namespace a ; using namespace b ; std :: cout << x << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u51b3\u5b9a\u8fd9\u91cc\u7684 main \u4e2d\u7684 x \u5230\u5e95\u6307\u7684\u662f a:: x \u8fd8\u662f b:: x \u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u4f1a\u56e0\u4e3a\u201c\u6b67\u4e49\u7b26\u53f7\u201d\u9519\u8bef\u800c\u65e0\u6cd5\u7f16\u8bd1\u3002\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u529e\u6cd5\u662f\u79fb\u9664\u5176\u4e2d\u4e00\u4e2a using \u8bed\u53e5\uff0c\u4f7f\u7528 using \u58f0\u660e\uff0c\u6216\u8005\u76f4\u63a5\u4f7f\u7528\u663e\u5f0f\u7684\u4f5c\u7528\u57df\u4fee\u9970\u7b26( a:: \u6216 b:: )\u3002 \u518d\u770b\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // imports the declaration of std::cout int cout () // declares our own \"cout\" function { return 5 ; } int main () { using namespace std ; // makes std::cout accessible as \"cout\" cout << \"Hello, world!\" ; // uh oh! Which cout do we want here? The one in the std namespace or the one we defined above? return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u786e\u5b9a cout \u5230\u5e95\u6307\u7684\u662f std:: cout \u8fd8\u662f\u6211\u4eec\u5b9a\u4e49\u7684 cout \u51fd\u6570\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u4ecd\u7136\u4f1a\u56e0\u4e3a\u201c\u6b67\u4e49\u7b26\u53f7\u201d\u9519\u8bef\u800c\u65e0\u6cd5\u7f16\u8bd1\u3002\u5982\u679c\u6211\u4eec\u4f7f\u7528\u524d\u7f00 std:: \uff0c\u50cf\u8fd9\u6837\uff1a 1 std :: cout << \"Hello, world!\" ; // tell the compiler we mean std::cout \u6216\u8005\u4f7f\u7528 using \u58f0\u660e\u800c\u4e0d\u662f using \u6307\u4ee4\uff1a 1 2 using std :: cout ; // tell the compiler that cout means std::cout cout << \"Hello, world!\" ; // so this means std::cout \u7a0b\u5e8f\u5c06\u53ef\u4ee5\u987a\u5229\u5b8c\u6210\u7f16\u8bd1\u3002 \u867d\u7136\u4f60\u5f88\u53ef\u80fd\u5e76\u4e0d\u4f1a\u5b9a\u4e49\u4e00\u4e2a\u53eb\u505a cout \u7684\u51fd\u6570\uff0c\u4f46\u662f std \u547d\u540d\u7a7a\u95f4\u4e2d\u6709\u6210\u767e\u4e0a\u5343\u7684\u6807\u8bc6\u7b26\u53ef\u80fd\u4f1a\u4e0e\u4f60\u5b9a\u4e49\u7684\u6807\u8bc6\u7b26\u4ea7\u751f\u51b2\u7a81\uff0c\u6bd4\u65b9\u8bf4\u201ccount\u201d, \u201cmin\u201d, \u201cmax\u201d, \u201csearch\u201d, \u201csort\u201d \u7b49\u7b49\u3002 \u800c\u4e14\uff0c\u5373\u4f7f using \u6307\u4ee4\u4eca\u5929\u6ca1\u6709\u5e26\u6765\u547d\u540d\u51b2\u7a81\uff0c\u4e5f\u96be\u4fdd\u4e0d\u5728\u672a\u6765\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u7684\u4ee3\u7801\u4f7f\u7528 using \u5bfc\u5165\u4e86\u67d0\u4e2a\u547d\u540d\u7a7a\u95f4\uff0c\u7136\u540e\u67d0\u5929\u8be5\u5e93\u66f4\u65b0\u4e86\uff0c\u6dfb\u52a0\u4e86\u4e00\u4e9b\u65b0\u7684\u53d8\u91cf\u540d\uff0c\u90a3\u6ca1\u51c6\u5c31\u4f1a\u548c\u4f60\u7684\u4ee3\u7801\u4ea7\u751f\u51b2\u7a81\u54af\u3002 \u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u9634\u9669\u7684\u95ee\u9898\u4e5f\u53ef\u80fd\u53d1\u751f\u3002\u5f53\u5e93\u66f4\u65b0\u4e4b\u540e\uff0c\u5b83\u5b9a\u4e49\u7684\u4e00\u4e9b\u65b0\u7684\u51fd\u6570\uff0c\u53ef\u80fd\u4e0d\u4ec5\u51fd\u6570\u540d\u548c\u6211\u4eec\u5b9a\u4e49\u7684\u51fd\u6570\u6709\u51b2\u7a81\uff0c\u751a\u81f3\u5b83\u66f4\u7b26\u5408\u67d0\u79cd\u67d0\u5904\u7684\u51fd\u6570\u8c03\u7528\u65b9\u5f0f\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c31\u53ef\u80fd\u4f1a\u4f7f\u7528\u8be5\u51fd\u6570\u6765\u66ff\u6362\u6211\u4eec\u7684\u51fd\u6570\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u7684\u884c\u4e3a\u5c06\u4f1a\u88ab\u6539\u53d8\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a ```cpp title=foolib.h\"\" namespace foo { // pretend there is some useful code that we use here } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ```cpp title=\"main.cpp\" #include #include // a third-party library, thus angled brackets used int someFcn(double) { return 1; } int main() { using namespace foo; // Because we're lazy and want to access foo:: qualified names without typing the foo:: prefix std::cout << someFcn(0); // \u5b57\u9762\u91cf\u5e94\u5f53\u4f7f\u7528 0.0 \u4f46\u88ab\u8bef\u7528\u4e3a 0\uff0c\u8fd9\u662f\u4e00\u4e2a\u5f88\u5bb9\u6613\u72af\u7684\u9519\u8bef\u3002 return 0; } \u7a0b\u5e8f\u8fd0\u884c\u540e\u4f1a\u6253\u5370 1 \u3002 \u73b0\u5728\uff0c\u66f4\u65b0 foolib\uff08\u4f7f\u7528\u66f4\u65b0\u7684 foolib.h \uff09\uff0c\u7a0b\u5e8f\u7f16\u7a0b\u4e0b\u9762\u8fd9\u6837\uff1a foolib.h 1 2 3 4 5 6 7 8 9 10 namespace foo { // newly introduced function int someFcn ( int ) { return 2 ; } // pretend there is some useful code that we use here } main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int someFcn ( double ) { return 1 ; } int main () { using namespace foo ; // Because we're lazy and want to access foo:: qualified names without typing the foo:: prefix std :: cout << someFcn ( 0 ); // \u5b57\u9762\u91cf\u5e94\u5f53\u4f7f\u7528 0.0 \u4f46\u88ab\u8bef\u7528\u4e3a 0\uff0c\u8fd9\u662f\u4e00\u4e2a\u5f88\u5bb9\u6613\u72af\u7684\u9519\u8bef\u3002 return 0 ; } main.cpp \u6587\u4ef6\u5b8c\u5168\u6ca1\u6709\u6539\u53d8\uff0c\u4f46\u662f\u7a0b\u5e8f\u5374\u8f93\u51fa\u4e86 2\uff01 \u5f53\u7f16\u8bd1\u5668\u9047\u5230\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u5b83\u9700\u8981\u786e\u5b9a\u4e0e\u51fd\u6570\u8c03\u7528\u6700\u5339\u914d\u7684\u51fd\u6570\u5b9a\u4e49\u662f\u4ec0\u4e48\u3002\u5f53\u6709\u591a\u4e2a\u5b9a\u4e49\u53ef\u4f9b\u7f16\u8bd1\u5668\u9009\u62e9\u65f6\uff0c\u5b83\u4e00\u5b9a\u4f1a\u9009\u62e9\u65e0\u9700\u53c2\u6570\u8f6c\u6362\u7684\u90a3\u4e00\u4e2a\u3002\u56e0\u4e3a 0 \u662f\u4e00\u4e2a\u6574\u578b\uff0cC++ \u4e00\u5b9a\u4f1a\u9009\u62e9\u66f4\u52a0\u5339\u914d\u7684\u65b0\u5b9a\u4e49\u7684 someFcn(int) (\u65e0\u9700\u8f6c\u6362) \u800c\u4e0d\u662f someFcn(double) (\u9700\u8981\u5c06 int \u8f6c\u6362\u4e3a double)\u3002\u8fd9\u6837\u5c31\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u884c\u4e3a\u88ab\u610f\u5916\u5730\u6539\u53d8\u3002 \u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u4f7f\u7528\u7684\u662f using \u58f0\u660e\u6216\u8005\u4f5c\u7528\u57df\u4fee\u9970\u7b26\u7684\u8bdd\uff0c\u5c31\u5b8c\u5168\u4e0d\u4f1a\u53d1\u751f\u8fd9\u7c7b\u95ee\u9898\u3002 \u6700\u540e\uff0c\u5bf9\u4e8e\u7a0b\u5e8f\u7684\u8bfb\u8005\u6765\u8bf4\uff0c\u5982\u679c\u4e0d\u4f7f\u7528\u4f5c\u7528\u57df\u4fee\u9970\u7b26\u7684\u8bdd\uff0c\u8bfb\u8005\u5c06\u96be\u786e\u5b9a\u67d0\u4e2a\u51fd\u6570\u5230\u5e95\u662f\u81ea\u5b9a\u4e49\u7684\uff0c\u8fd8\u662f\u5c5e\u4e8e\u67d0\u4e2a\u5e93\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f7f\u7528 using \u6307\u4ee4\uff1a 1 2 3 4 5 6 using namespace ns ; int main () { foo (); // is this foo a user-defined function, or part of the ns library? } \u8fd9\u79cd\u60c5\u51b5\u4e0b\u5c31\u5f88\u96be\u786e\u5b9a foo() \u6307\u7684\u662f ns:: foo() \u8fd8\u662f\u7528\u6237\u81ea\u5b9a\u4e49\u7684 foo() \u51fd\u6570\u3002\u5728\u4f7f\u7528\u73b0\u4ee3 IDE \u65f6\uff0c\u5982\u679c\u5c06\u9f20\u6807\u60ac\u6d6e\u5728\u53d8\u91cf\u540d\u4e0a\u65f6\uff0cIDE \u4e00\u822c\u90fd\u53ef\u4ee5\u5e2e\u6211\u4eec\u786e\u5b9a\u6807\u8bc6\u7b26\u5230\u5e95\u6307\u7684\u662f\u4ec0\u4e48\uff0c\u4f46\u662f\u8fd9\u6837\u663e\u7136\u5f88\u9ebb\u70e6\uff0c\u6bd5\u7adf\u6bcf\u4e2a\u53d8\u91cf\u540d\u90fd\u60ac\u6d6e\u9f20\u6807\u770b\u4e00\u4e0b\u662f\u5f88\u9ebb\u70e6\u7684\u3002 \u5982\u679c\u4e0d\u662f\u4f7f\u7528 using \u6307\u4ee4\uff0c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u5c31\u4f1a\u597d\u5f88\u591a\uff1a 1 2 3 4 5 int main () { ns :: foo (); // clearly part of the ns library foo (); // likely a user-defined function } \u5728\u8fd9\u4e2a\u7248\u672c\u4e2d\uff0c ns:: foo() \u663e\u7136\u662f\u8c03\u7528\u5e93\u51fd\u6570\u3002\u800c foo() \u5219\u662f\u8c03\u7528\u7528\u6237\u81ea\u5b9a\u4e49\u51fd\u6570\u3002 Using \u58f0\u660e\u548c\u6307\u4ee4\u7684\u4f5c\u7528\u57df \u5982\u679c\u5728\u8bed\u53e5\u5757\u4e2d\u4f7f\u7528 using \u58f0\u660e\u6216 using \u6307\u4ee4\uff0c\u90a3\u5bf9\u5e94\u7684\u53d8\u91cf\u540d\u53ea\u5728\u8be5\u4f5c\u7528\u57df\u4e2d\u6709\u6548\u3002\u8fd9\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u7279\u6027\uff0c\u56e0\u4e3a\u5b83\u5c06\u547d\u540d\u51b2\u7a81\u7684\u98ce\u9669\u9650\u5b9a\u5728\u4e86\u8bed\u53e5\u5757\u4e2d\u3002 \u5982\u679c\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u4f7f\u7528 using \u58f0\u660e\u6216 using \u6307\u4ee4\uff0c\u53d8\u91cf\u540d\u5c06\u5f71\u54cd\u5230\u6574\u4e2a\u6587\u4ef6\uff08\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\uff09\u3002 \u53d6\u6d88\u6216\u8005\u66ff\u6362 using \u8bed\u53e5 \u4e00\u65e6\u4f7f\u7528\u4e86 using \u8bed\u53e5\uff0c\u5728\u76f8\u540c\u7684\u4f5c\u7528\u57df\u4e2d\u5c06\u6ca1\u6709\u529e\u6cd5\u5bf9\u5176\u8fdb\u884c\u53d6\u6d88\u6216\u66ff\u6362\u3002 1 2 3 4 5 6 7 8 9 int main () { using namespace foo ; // \u6ca1\u6709\u529e\u6cd5\u53d6\u6d88 \"using namespace foo\" \u7684\u6548\u679c // \u4e5f\u6ca1\u6709\u529e\u6cd5\u4f7f\u7528\u5176\u4ed6 using \u8bed\u53e5\u66ff\u6362 return 0 ; } // using namespace foo ends here \u5982\u679c\u5e0c\u671b\u9650\u5236 using \u8bed\u53e5\u7684\u4f5c\u7528\u8303\u56f4\uff0c\u53ef\u4ee5\u5c06\u5b83\u653e\u5728\u4e00\u4e2a\u8bed\u53e5\u5757\u4e2d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int main () { { using namespace foo ; // calls to foo:: stuff here } // using namespace foo expires { using namespace Goo ; // calls to Goo:: stuff here } // using namespace Goo expires return 0 ; } \u5f53\u7136\uff0c\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u53ef\u4ee5\u4e00\u52b3\u6c38\u9038\u5730\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u3002 using \u8bed\u53e5\u7684\u6700\u4f73\u5b9e\u8df5 \u9664\u975e\u9047\u5230\u975e\u5e38\u7279\u6b8a\u7684\u60c5\u51b5\uff0c\u8bf7\u907f\u514d\u4f7f\u7528 using \u6307\u4ee4 (\u5c24\u5176\u662f using namespace std; )\u3002\u5728\u8bed\u53e5\u5757\u4e2d\u4f7f\u7528 using \u58f0\u660e\u901a\u5e38\u88ab\u8ba4\u4e3a\u662f\u5b89\u5168\u7684\u3002\u7edd\u5bf9\u4e0d\u8981\u5728\u5934\u6587\u4ef6\u7684\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u4f7f\u7528\u5b83\u4eec\uff0c\u5728\u6e90\u6587\u4ef6\u7684\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u6709\u9650\u5730\u4f7f\u7528\u5b83\u4eec\u3002 \u6700\u4f73\u5b9e\u8df5 \u660e\u786e\u6307\u5b9a\u4f5c\u7528\u57df\u597d\u8fc7 using \u8bed\u53e5\u3002\u5c3d\u53ef\u80fd\u5730\u907f\u514d\u4f7f\u7528 using \u6307\u4ee4\u3002\u5728\u8bed\u53e5\u5757\u4e2d\u4f7f\u7528 using \u58f0\u660e\u901a\u5e38\u662f\u53ef\u4ee5\u7684\u3002 \u76f8\u5173\u5185\u5bb9 using \u5173\u952e\u5b57\u8fd8\u53ef\u4ee5\u7528\u6765\u5b9a\u4e49\u7c7b\u578b\u522b\u540d\uff0c\u6211\u4eec\u4f1a\u5728 8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d \u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u7c7b\u578b\u522b\u540d\u3002","title":"6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4"},{"location":"6-12-Using-declarations-and-using%20directives/#_1","text":"\u5728 C++ \u5f00\u59cb\u652f\u6301\u547d\u540d\u7a7a\u95f4\u4e4b\u524d\uff0c\u6240\u6709\u73b0\u5728\u4f4d\u4e8e std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\uff0c\u5f7c\u65f6\u90fd\u4f4d\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u8fd9\u4f7f\u5f97\u7528\u6237\u7684\u547d\u540d\u5f88\u5bb9\u6613\u548c\u6807\u51c6\u5e93\u4e2d\u7684\u540d\u79f0\u53d1\u751f\u51b2\u7a81\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u5728\u67d0\u4e2a C++\u7248\u672c\u4e0b\u53ef\u4ee5\u8fd0\u884c\u7684\u7a0b\u5e8f\uff0c\u5f53\u4f7f\u7528\u4e86\u65b0\u7248\u672c\u7684 C++ \u65f6\u4e5f\u5f88\u53ef\u80fd\u65e0\u6cd5\u5de5\u4f5c\u3002 \u547d\u540d\u7a7a\u95f4\u7684\u6807\u51c6\u5316\u662f\u5728 1995 \u5e74\u5b8c\u6210\u7684\uff0c\u4ece\u90a3\u65f6\u8d77\uff0c\u6240\u6709\u6807\u51c6\u5e93\u4e2d\u63d0\u4f9b\u7684\u529f\u80fd\uff0c\u90fd\u88ab\u79fb\u52a8\u5230\u4e86 std \u547d\u540d\u7a7a\u95f4\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4e0d\u5177\u6709 std:: \u524d\u7f00\u7684\u4ee3\u7801\u5c31\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u4e86\u3002 \u7ef4\u62a4\u8fc7\u5927\u578b\u9879\u76ee\u7684\u4eba\u90fd\u77e5\u9053\uff0c\u4efb\u4f55\u4e00\u70b9\u6539\u52a8\u90fd\u6709\u53ef\u80fd\u7834\u574f\u6574\u4e2a\u7a0b\u5e8f\uff08\u4e0d\u8bba\u6539\u52a8\u662f\u591a\u4e48\u7684\u4e0d\u8d77\u773c\uff09\u3002\u56e0\u6b64\uff0c\u4e3a\u6240\u6709\u76f8\u5173\u7684\u6807\u8bc6\u7b26\u6dfb\u52a0 std:: \u524d\u7f00\u7684\u98ce\u9669\u975e\u5e38\u5927\u3002\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u63d0\u4f9b\u4e00\u79cd\u66f4\u52a0\u6709\u6548\u7684\u65b9\u5f0f\u3002 \u65f6\u95f4\u4e00\u6643\u5230\u4e86\u4eca\u5929\u2014\u2014\u5982\u679c\u4f60\u5927\u91cf\u4f7f\u7528\u4e86\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u529f\u80fd\uff0c\u4e3a\u6bcf\u4e00\u4e2a\u76f8\u5173\u7684\u6807\u8bc6\u7b26\u6dfb\u52a0 std:: \u4e5f\u662f\u4e00\u79cd\u975e\u5e38\u91cd\u590d\u7684\u5de5\u4f5c\uff0c\u800c\u4e14\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4f1a\u8ba9\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u53d8\u5dee\u3002 \u4e3a\u6b64\uff0cC++ \u4e3a\u8fd9\u4e9b\u95ee\u9898\u63d0\u4f9b\u4e86\u4e00\u79cd\u89e3\u51b3\u65b9\u6cd5\uff0c\u5373 using \u8bed\u53e5\u3002 \u5728\u7ee7\u7eed\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u6765\u4ecb\u7ecd\u4e24\u4e2a\u672f\u8bed","title":"\u5386\u53f2\u8bfe"},{"location":"6-12-Using-declarations-and-using%20directives/#_2","text":"\u4e00\u4e2a\u53d8\u91cf\u540d\uff08\u51fd\u6570\u540d\uff09\u53ef\u4ee5\u662f\u9650\u5b9a\u540d\uff08qualified\uff09\u6216\u8005\u975e\u9650\u5b9a\u540d\uff08unqualified\uff09\u3002 \u5305\u542b\u4e86\u4f5c\u7528\u57df\u4fe1\u606f\u7684\u540d\u5b57\uff0c\u79f0\u4e3a\u9650\u5b9a\u540d\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 ( :: )\u53ef\u4ee5\u521b\u5efa\u9650\u5b9a\u540d\u3002\u4f8b\u5982\uff1a 1 2 std :: cout // identifier cout is qualified by namespace std :: foo // identifier foo is qualified by the global namespace \u6269\u5c55\u9605\u8bfb \u7ed3\u5408\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u548c\u7c7b\u540d\u4e5f\u53ef\u4ee5\u521b\u5efa\u9650\u5b9a\u540d\uff0c\u6216\u8005\u5bf9\u7c7b\u5bf9\u8c61\u4f7f\u7528\u6210\u5458\u9009\u62e9\u64cd\u4f5c\u7b26\uff08 . \u6216 -> \uff09\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 class C ; // some class C :: s_member ; // s_member is qualified by class C obj . x ; // x is qualified by class object obj ptr -> y ; // y is qualified by pointer to class object ptr \u800c\u4e0d\u5177\u6709\u4f5c\u7528\u57df\u9650\u5b9a\u7b26\u7684\u53d8\u91cf\u540d\uff0c\u5219\u4e3a\u975e\u9650\u5b9a\u540d\u3002\u4f8b\u5982 cout \u548c x \u90fd\u662f\u975e\u9650\u5b9a\u540d\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e0d\u5305\u542b\u4efb\u4f55\u7684\u4f5c\u7528\u57df\u4fe1\u606f\u3002","title":"\u9650\u5b9a\u540d\u548c\u975e\u9650\u5b9a\u540d"},{"location":"6-12-Using-declarations-and-using%20directives/#using","text":"\u51cf\u5c11\u91cd\u590d\u8f93\u5165 std:: \u7684\u4e00\u4e2a\u65b9\u6cd5\u662f\u4f7f\u7528 using \u58f0\u660e\u8bed\u53e5\u3002using \u58f0\u660e\u8bed\u53e5\u4f7f\u6211\u4eec\u53ef\u4ee5\u5c06\u975e\u9650\u5b9a\u540d\u4f5c\u4e3a\u9650\u5b9a\u540d\u7684\u522b\u540d\u6765\u4f7f\u7528\u3002 \u4e0b\u9762\u8fd9\u4e2a\u7ecf\u5178\u7684 Hello World \u7a0b\u5e8f\uff0c\u5728\u7b2c\u4e94\u884c\u4f7f\u7528\u4e86 using \u58f0\u660e\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { using std :: cout ; // this using declaration tells the compiler that cout should resolve to std::cout cout << \"Hello world!\" ; // so no std:: prefix is needed here! return 0 ; } // the using declaration expires here using std:: cout; \u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u6211\u4eec\u8981\u4f7f\u7528 std namespace \u4e2d\u7684 cout \uff0c\u6240\u4ee5\u5728\u4efb\u4f55\u4f7f\u7528 cout \u7684\u5730\u65b9\uff0c\u7f16\u8bd1\u5668\u90fd\u4f1a\u5047\u5b9a\u5b83\u7b49\u4ef7\u4e8e std:: cout \u3002\u5982\u679c\u5728\u9047\u5230 std:: cout \u548c\u5176\u4ed6 cout \u77db\u76fe\u7684\u5730\u65b9\uff0c\u5219\u4f18\u5148\u8ba4\u4e3a\u662f std:: cout \u3002\u56e0\u6b64\uff0c\u5728\u7b2c\u516d\u884c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 cout \u800c\u65e0\u9700\u4f7f\u7528 std:: cout \u3002 \u4ece\u8fd9\u4e2a\u5c0f\u4f8b\u5b50\u6211\u4eec\u5f88\u96be\u770b\u51fa using \u8bed\u53e5\u80fd\u591f\u5e26\u6765\u7684\u6536\u76ca\uff0c\u4f46\u662f\u5f53\u4f60\u5728\u51fd\u6570\u4e2d\u5927\u91cf\u4f7f\u7528 cout \u7684\u65f6\u5019\uff0c using \u58f0\u660e\u53ef\u4ee5\u8ba9\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u53d8\u5f97\u66f4\u597d\u3002\u6ce8\u610f\uff0c\u5bf9\u4e8e\u4e0d\u540c\u7684\u53d8\u91cf\u540d\uff0c\u9700\u8981\u5355\u72ec\u7684 using \u58f0\u660e\uff08\u4f8b\u5982 std:: cout \u662f\u4e00\u4e2a\uff0c std:: cin \u662f\u53e6\u4e00\u4e2a\uff09\u3002 \u5c3d\u7ba1\u8fd9\u79cd\u65b9\u6cd5\u6ca1\u6709\u4f7f\u7528 std:: \u524d\u7f00\u90a3\u4e48\u660e\u786e\uff0c\u4f46\u901a\u5e38\u4e5f\u88ab\u8ba4\u4e3a\u662f\u4e00\u79cd\u5b89\u5168\u7684\u3001\u53ef\u63a5\u53d7\u7684\u65b9\u6cd5\uff08\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\uff09\u3002","title":"Using \u58f0\u660e"},{"location":"6-12-Using-declarations-and-using%20directives/#using_1","text":"\u53e6\u5916\u4e00\u79cd\u7b80\u5316\u65b9\u5f0f\u662f\u4f7f\u7528 using \u6307\u4ee4\uff0c\u5b83\u53ef\u4ee5\u5c06\u8be5\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u90fd\u5bfc\u5165\u5230\u4f7f\u7528\u4e86\u8be5 using \u6307\u4ee4\u7684\u4f5c\u7528\u57df\u3002 \u6269\u5c55\u9605\u8bfb \u4ece\u6280\u672f\u89d2\u5ea6\u6765\u8bb2\uff0c\u4f7f\u7528 using \u6307\u4ee4\u5176\u5b9e\u5e76\u6ca1\u6709\u5c06\u6807\u8bc6\u7b26\u5bfc\u5165\u5230\u5f53\u524d\u7684\u4f5c\u7528\u57df\u2014\u2014\u5b9e\u9645\u4e0a\u5b83\u4eec\u88ab\u5bfc\u5165\u5230\u5916\u5c42\u4f5c\u7528\u57df\uff08\u5173\u4e8e\u5916\u5c42\u4f5c\u7528\u57df\u7684\u8be6\u7ec6\u4fe1\u606f\u53ef\u4ee5\u53c2\u8003 \u8fd9\u91cc \uff09\u3002\u4e0d\u8fc7\uff0c\u5728\u5916\u5c42\u4f5c\u7528\u57df\u4e2d\u5e76\u4e0d\u80fd\u8bbf\u95ee\u8fd9\u4e9b\u53d8\u91cf\u540d\uff0c\u5b83\u4eec\u53ea\u80fd\u5728\u4f7f\u7528\u4e86 using \u6307\u4ee4\u7684\u4f5c\u7528\u57df\u4e2d\u8bbf\u95ee\u3002 \u4ece\u5b9e\u9645\u6548\u679c\u6765\u770b\uff0c\u4f7f\u7528 using \u6307\u4ee4\u7684\u884c\u4e3a\u770b\u4e0a\u53bb\u5c31\u50cf\u662f\u53d8\u91cf\u540d\u88ab\u5bfc\u5165\u5230\u4e86\u5f53\u524d\u4f5c\u7528\u57df\u3002\u4e3a\u4e86\u4e0d\u628a\u4e8b\u60c5\u641e\u590d\u6742\uff0c\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4f1a\u6cbf\u7528\u8fd9\u79cd\u7b80\u5316\u7684\u8bf4\u6cd5\uff0c\u5373\u53d8\u91cf\u540d\u88ab \u5bfc\u5165 \u5230\u4e86\u5f53\u524d\u4f5c\u7528\u57df\u3002 \u518d\u56de\u8fc7\u5934\u770b Hello World \u7a0b\u5e8f\uff0c\u7b2c\u4e94\u884c\u4f7f\u7528\u4e86 using \u6307\u4ee4\uff1a 1 2 3 4 5 6 7 8 #include int main () { using namespace std ; // this using directive tells the compiler to import all names from namespace std into the current namespace without qualification cout << \"Hello world!\" ; // so no std:: prefix is needed here return 0 ; } using namespace std; \u544a\u8bc9\u7f16\u8bd1\u5668\u5c06 std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684 \u5168\u90e8 \u6807\u8bc6\u7b26\u5bfc\u5165\u5230\u5f53\u524d\u4f5c\u7528\u57df\uff08\u4f8b\u5982 main() \u51fd\u6570\uff09\u3002\u5f53\u6211\u4eec\u4f7f\u7528\u975e\u9650\u5b9a\u6807\u8bc6\u7b26 cout \u7684\u65f6\u5019\uff0c\u5b83\u5c31\u4f1a\u88ab\u89e3\u6790\u4e3a\u5bfc\u5165\u7684 std:: cout \u3002 Using \u6307\u4ee4\u662f\u4e3a\u4e86\u90a3\u4e9b\u5728\u547d\u540d\u7a7a\u95f4\u53d1\u660e\u524d\u5c31\u5b58\u5728\u7684\u9879\u76ee\u800c\u8bbe\u8ba1\u7684\u3002\u5728\u8fd9\u79cd\u53e4\u8001\u7684\u9879\u76ee\u4e2d\uff0c\u6709\u4e86 using \u6307\u4ee4\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u4e0d\u5fc5\u9010\u4e00\u5730\u5c06\u975e\u9650\u5b9a\u540d\u4fee\u6539\u4e3a\u9650\u5b9a\u540d\uff08\u8fd9\u4e48\u505a\u98ce\u9669\u975e\u5e38\u5927\uff09\uff0c\u53ea\u9700\u5c06 using namespace std; \u653e\u7f6e\u5728\u6587\u4ef6\u7684\u5f00\u59cb\u5904\uff0c\u90a3\u4e48\u6240\u6709\u7684\u5df2\u7ecf\u88ab\u79fb\u52a8\u5230\u4e86 std \u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\uff0c\u4ecd\u7136\u53ef\u4ee5\u4ee5\u975e\u9650\u5b9a\u540d\u7684\u65b9\u5f0f\u6765\u4f7f\u7528\u3002","title":"Using \u6307\u4ee4"},{"location":"6-12-Using-declarations-and-using%20directives/#using-using-namespace-std","text":"\u5728\u73b0\u4ee3 C++ \u4e2d\uff0c\u4f7f\u7528 using \u6307\u4ee4\u901a\u5e38\u662f\u5f0a\u5927\u4e8e\u5229\u7684\uff08\u9664\u4e86\u80fd\u5c11\u6253\u5b57\u4ee5\u5916\uff09\u3002\u56e0\u4e3a\u4f7f\u7528 using \u6307\u4ee4\u5bfc\u5165\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6240\u6709\u6807\u8bc6\u7b26\u4f1a\u6781\u5927\u5730\u63d0\u9ad8\u547d\u540d\u51b2\u7a81\u7684\u53ef\u80fd\uff08\u5c24\u5176\u662f\u5bfc\u5165 std \uff09\u3002 \u901a\u8fc7\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230 using \u6307\u4ee4\u53ef\u80fd\u5e26\u6765\u7684\u4e8c\u4e49\u6027\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include namespace a { int x { 10 }; } namespace b { int x { 20 }; } int main () { using namespace a ; using namespace b ; std :: cout << x << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u51b3\u5b9a\u8fd9\u91cc\u7684 main \u4e2d\u7684 x \u5230\u5e95\u6307\u7684\u662f a:: x \u8fd8\u662f b:: x \u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u4f1a\u56e0\u4e3a\u201c\u6b67\u4e49\u7b26\u53f7\u201d\u9519\u8bef\u800c\u65e0\u6cd5\u7f16\u8bd1\u3002\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u529e\u6cd5\u662f\u79fb\u9664\u5176\u4e2d\u4e00\u4e2a using \u8bed\u53e5\uff0c\u4f7f\u7528 using \u58f0\u660e\uff0c\u6216\u8005\u76f4\u63a5\u4f7f\u7528\u663e\u5f0f\u7684\u4f5c\u7528\u57df\u4fee\u9970\u7b26( a:: \u6216 b:: )\u3002 \u518d\u770b\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include // imports the declaration of std::cout int cout () // declares our own \"cout\" function { return 5 ; } int main () { using namespace std ; // makes std::cout accessible as \"cout\" cout << \"Hello, world!\" ; // uh oh! Which cout do we want here? The one in the std namespace or the one we defined above? return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u786e\u5b9a cout \u5230\u5e95\u6307\u7684\u662f std:: cout \u8fd8\u662f\u6211\u4eec\u5b9a\u4e49\u7684 cout \u51fd\u6570\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u4ecd\u7136\u4f1a\u56e0\u4e3a\u201c\u6b67\u4e49\u7b26\u53f7\u201d\u9519\u8bef\u800c\u65e0\u6cd5\u7f16\u8bd1\u3002\u5982\u679c\u6211\u4eec\u4f7f\u7528\u524d\u7f00 std:: \uff0c\u50cf\u8fd9\u6837\uff1a 1 std :: cout << \"Hello, world!\" ; // tell the compiler we mean std::cout \u6216\u8005\u4f7f\u7528 using \u58f0\u660e\u800c\u4e0d\u662f using \u6307\u4ee4\uff1a 1 2 using std :: cout ; // tell the compiler that cout means std::cout cout << \"Hello, world!\" ; // so this means std::cout \u7a0b\u5e8f\u5c06\u53ef\u4ee5\u987a\u5229\u5b8c\u6210\u7f16\u8bd1\u3002 \u867d\u7136\u4f60\u5f88\u53ef\u80fd\u5e76\u4e0d\u4f1a\u5b9a\u4e49\u4e00\u4e2a\u53eb\u505a cout \u7684\u51fd\u6570\uff0c\u4f46\u662f std \u547d\u540d\u7a7a\u95f4\u4e2d\u6709\u6210\u767e\u4e0a\u5343\u7684\u6807\u8bc6\u7b26\u53ef\u80fd\u4f1a\u4e0e\u4f60\u5b9a\u4e49\u7684\u6807\u8bc6\u7b26\u4ea7\u751f\u51b2\u7a81\uff0c\u6bd4\u65b9\u8bf4\u201ccount\u201d, \u201cmin\u201d, \u201cmax\u201d, \u201csearch\u201d, \u201csort\u201d \u7b49\u7b49\u3002 \u800c\u4e14\uff0c\u5373\u4f7f using \u6307\u4ee4\u4eca\u5929\u6ca1\u6709\u5e26\u6765\u547d\u540d\u51b2\u7a81\uff0c\u4e5f\u96be\u4fdd\u4e0d\u5728\u672a\u6765\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u7684\u4ee3\u7801\u4f7f\u7528 using \u5bfc\u5165\u4e86\u67d0\u4e2a\u547d\u540d\u7a7a\u95f4\uff0c\u7136\u540e\u67d0\u5929\u8be5\u5e93\u66f4\u65b0\u4e86\uff0c\u6dfb\u52a0\u4e86\u4e00\u4e9b\u65b0\u7684\u53d8\u91cf\u540d\uff0c\u90a3\u6ca1\u51c6\u5c31\u4f1a\u548c\u4f60\u7684\u4ee3\u7801\u4ea7\u751f\u51b2\u7a81\u54af\u3002 \u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u9634\u9669\u7684\u95ee\u9898\u4e5f\u53ef\u80fd\u53d1\u751f\u3002\u5f53\u5e93\u66f4\u65b0\u4e4b\u540e\uff0c\u5b83\u5b9a\u4e49\u7684\u4e00\u4e9b\u65b0\u7684\u51fd\u6570\uff0c\u53ef\u80fd\u4e0d\u4ec5\u51fd\u6570\u540d\u548c\u6211\u4eec\u5b9a\u4e49\u7684\u51fd\u6570\u6709\u51b2\u7a81\uff0c\u751a\u81f3\u5b83\u66f4\u7b26\u5408\u67d0\u79cd\u67d0\u5904\u7684\u51fd\u6570\u8c03\u7528\u65b9\u5f0f\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c31\u53ef\u80fd\u4f1a\u4f7f\u7528\u8be5\u51fd\u6570\u6765\u66ff\u6362\u6211\u4eec\u7684\u51fd\u6570\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u7684\u884c\u4e3a\u5c06\u4f1a\u88ab\u6539\u53d8\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a ```cpp title=foolib.h\"\" namespace foo { // pretend there is some useful code that we use here } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ```cpp title=\"main.cpp\" #include #include // a third-party library, thus angled brackets used int someFcn(double) { return 1; } int main() { using namespace foo; // Because we're lazy and want to access foo:: qualified names without typing the foo:: prefix std::cout << someFcn(0); // \u5b57\u9762\u91cf\u5e94\u5f53\u4f7f\u7528 0.0 \u4f46\u88ab\u8bef\u7528\u4e3a 0\uff0c\u8fd9\u662f\u4e00\u4e2a\u5f88\u5bb9\u6613\u72af\u7684\u9519\u8bef\u3002 return 0; } \u7a0b\u5e8f\u8fd0\u884c\u540e\u4f1a\u6253\u5370 1 \u3002 \u73b0\u5728\uff0c\u66f4\u65b0 foolib\uff08\u4f7f\u7528\u66f4\u65b0\u7684 foolib.h \uff09\uff0c\u7a0b\u5e8f\u7f16\u7a0b\u4e0b\u9762\u8fd9\u6837\uff1a foolib.h 1 2 3 4 5 6 7 8 9 10 namespace foo { // newly introduced function int someFcn ( int ) { return 2 ; } // pretend there is some useful code that we use here } main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include int someFcn ( double ) { return 1 ; } int main () { using namespace foo ; // Because we're lazy and want to access foo:: qualified names without typing the foo:: prefix std :: cout << someFcn ( 0 ); // \u5b57\u9762\u91cf\u5e94\u5f53\u4f7f\u7528 0.0 \u4f46\u88ab\u8bef\u7528\u4e3a 0\uff0c\u8fd9\u662f\u4e00\u4e2a\u5f88\u5bb9\u6613\u72af\u7684\u9519\u8bef\u3002 return 0 ; } main.cpp \u6587\u4ef6\u5b8c\u5168\u6ca1\u6709\u6539\u53d8\uff0c\u4f46\u662f\u7a0b\u5e8f\u5374\u8f93\u51fa\u4e86 2\uff01 \u5f53\u7f16\u8bd1\u5668\u9047\u5230\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u5b83\u9700\u8981\u786e\u5b9a\u4e0e\u51fd\u6570\u8c03\u7528\u6700\u5339\u914d\u7684\u51fd\u6570\u5b9a\u4e49\u662f\u4ec0\u4e48\u3002\u5f53\u6709\u591a\u4e2a\u5b9a\u4e49\u53ef\u4f9b\u7f16\u8bd1\u5668\u9009\u62e9\u65f6\uff0c\u5b83\u4e00\u5b9a\u4f1a\u9009\u62e9\u65e0\u9700\u53c2\u6570\u8f6c\u6362\u7684\u90a3\u4e00\u4e2a\u3002\u56e0\u4e3a 0 \u662f\u4e00\u4e2a\u6574\u578b\uff0cC++ \u4e00\u5b9a\u4f1a\u9009\u62e9\u66f4\u52a0\u5339\u914d\u7684\u65b0\u5b9a\u4e49\u7684 someFcn(int) (\u65e0\u9700\u8f6c\u6362) \u800c\u4e0d\u662f someFcn(double) (\u9700\u8981\u5c06 int \u8f6c\u6362\u4e3a double)\u3002\u8fd9\u6837\u5c31\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u884c\u4e3a\u88ab\u610f\u5916\u5730\u6539\u53d8\u3002 \u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u4f7f\u7528\u7684\u662f using \u58f0\u660e\u6216\u8005\u4f5c\u7528\u57df\u4fee\u9970\u7b26\u7684\u8bdd\uff0c\u5c31\u5b8c\u5168\u4e0d\u4f1a\u53d1\u751f\u8fd9\u7c7b\u95ee\u9898\u3002 \u6700\u540e\uff0c\u5bf9\u4e8e\u7a0b\u5e8f\u7684\u8bfb\u8005\u6765\u8bf4\uff0c\u5982\u679c\u4e0d\u4f7f\u7528\u4f5c\u7528\u57df\u4fee\u9970\u7b26\u7684\u8bdd\uff0c\u8bfb\u8005\u5c06\u96be\u786e\u5b9a\u67d0\u4e2a\u51fd\u6570\u5230\u5e95\u662f\u81ea\u5b9a\u4e49\u7684\uff0c\u8fd8\u662f\u5c5e\u4e8e\u67d0\u4e2a\u5e93\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f7f\u7528 using \u6307\u4ee4\uff1a 1 2 3 4 5 6 using namespace ns ; int main () { foo (); // is this foo a user-defined function, or part of the ns library? } \u8fd9\u79cd\u60c5\u51b5\u4e0b\u5c31\u5f88\u96be\u786e\u5b9a foo() \u6307\u7684\u662f ns:: foo() \u8fd8\u662f\u7528\u6237\u81ea\u5b9a\u4e49\u7684 foo() \u51fd\u6570\u3002\u5728\u4f7f\u7528\u73b0\u4ee3 IDE \u65f6\uff0c\u5982\u679c\u5c06\u9f20\u6807\u60ac\u6d6e\u5728\u53d8\u91cf\u540d\u4e0a\u65f6\uff0cIDE \u4e00\u822c\u90fd\u53ef\u4ee5\u5e2e\u6211\u4eec\u786e\u5b9a\u6807\u8bc6\u7b26\u5230\u5e95\u6307\u7684\u662f\u4ec0\u4e48\uff0c\u4f46\u662f\u8fd9\u6837\u663e\u7136\u5f88\u9ebb\u70e6\uff0c\u6bd5\u7adf\u6bcf\u4e2a\u53d8\u91cf\u540d\u90fd\u60ac\u6d6e\u9f20\u6807\u770b\u4e00\u4e0b\u662f\u5f88\u9ebb\u70e6\u7684\u3002 \u5982\u679c\u4e0d\u662f\u4f7f\u7528 using \u6307\u4ee4\uff0c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u5c31\u4f1a\u597d\u5f88\u591a\uff1a 1 2 3 4 5 int main () { ns :: foo (); // clearly part of the ns library foo (); // likely a user-defined function } \u5728\u8fd9\u4e2a\u7248\u672c\u4e2d\uff0c ns:: foo() \u663e\u7136\u662f\u8c03\u7528\u5e93\u51fd\u6570\u3002\u800c foo() \u5219\u662f\u8c03\u7528\u7528\u6237\u81ea\u5b9a\u4e49\u51fd\u6570\u3002","title":"Using \u6307\u4ee4\u5e26\u6765\u7684\u95ee\u9898 (\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528 \u201cusing namespace std;\u201d)"},{"location":"6-12-Using-declarations-and-using%20directives/#using_2","text":"\u5982\u679c\u5728\u8bed\u53e5\u5757\u4e2d\u4f7f\u7528 using \u58f0\u660e\u6216 using \u6307\u4ee4\uff0c\u90a3\u5bf9\u5e94\u7684\u53d8\u91cf\u540d\u53ea\u5728\u8be5\u4f5c\u7528\u57df\u4e2d\u6709\u6548\u3002\u8fd9\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u7279\u6027\uff0c\u56e0\u4e3a\u5b83\u5c06\u547d\u540d\u51b2\u7a81\u7684\u98ce\u9669\u9650\u5b9a\u5728\u4e86\u8bed\u53e5\u5757\u4e2d\u3002 \u5982\u679c\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u4f7f\u7528 using \u58f0\u660e\u6216 using \u6307\u4ee4\uff0c\u53d8\u91cf\u540d\u5c06\u5f71\u54cd\u5230\u6574\u4e2a\u6587\u4ef6\uff08\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\uff09\u3002","title":"Using \u58f0\u660e\u548c\u6307\u4ee4\u7684\u4f5c\u7528\u57df"},{"location":"6-12-Using-declarations-and-using%20directives/#using_3","text":"\u4e00\u65e6\u4f7f\u7528\u4e86 using \u8bed\u53e5\uff0c\u5728\u76f8\u540c\u7684\u4f5c\u7528\u57df\u4e2d\u5c06\u6ca1\u6709\u529e\u6cd5\u5bf9\u5176\u8fdb\u884c\u53d6\u6d88\u6216\u66ff\u6362\u3002 1 2 3 4 5 6 7 8 9 int main () { using namespace foo ; // \u6ca1\u6709\u529e\u6cd5\u53d6\u6d88 \"using namespace foo\" \u7684\u6548\u679c // \u4e5f\u6ca1\u6709\u529e\u6cd5\u4f7f\u7528\u5176\u4ed6 using \u8bed\u53e5\u66ff\u6362 return 0 ; } // using namespace foo ends here \u5982\u679c\u5e0c\u671b\u9650\u5236 using \u8bed\u53e5\u7684\u4f5c\u7528\u8303\u56f4\uff0c\u53ef\u4ee5\u5c06\u5b83\u653e\u5728\u4e00\u4e2a\u8bed\u53e5\u5757\u4e2d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int main () { { using namespace foo ; // calls to foo:: stuff here } // using namespace foo expires { using namespace Goo ; // calls to Goo:: stuff here } // using namespace Goo expires return 0 ; } \u5f53\u7136\uff0c\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u53ef\u4ee5\u4e00\u52b3\u6c38\u9038\u5730\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u3002","title":"\u53d6\u6d88\u6216\u8005\u66ff\u6362 using \u8bed\u53e5"},{"location":"6-12-Using-declarations-and-using%20directives/#using_4","text":"\u9664\u975e\u9047\u5230\u975e\u5e38\u7279\u6b8a\u7684\u60c5\u51b5\uff0c\u8bf7\u907f\u514d\u4f7f\u7528 using \u6307\u4ee4 (\u5c24\u5176\u662f using namespace std; )\u3002\u5728\u8bed\u53e5\u5757\u4e2d\u4f7f\u7528 using \u58f0\u660e\u901a\u5e38\u88ab\u8ba4\u4e3a\u662f\u5b89\u5168\u7684\u3002\u7edd\u5bf9\u4e0d\u8981\u5728\u5934\u6587\u4ef6\u7684\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u4f7f\u7528\u5b83\u4eec\uff0c\u5728\u6e90\u6587\u4ef6\u7684\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u6709\u9650\u5730\u4f7f\u7528\u5b83\u4eec\u3002 \u6700\u4f73\u5b9e\u8df5 \u660e\u786e\u6307\u5b9a\u4f5c\u7528\u57df\u597d\u8fc7 using \u8bed\u53e5\u3002\u5c3d\u53ef\u80fd\u5730\u907f\u514d\u4f7f\u7528 using \u6307\u4ee4\u3002\u5728\u8bed\u53e5\u5757\u4e2d\u4f7f\u7528 using \u58f0\u660e\u901a\u5e38\u662f\u53ef\u4ee5\u7684\u3002 \u76f8\u5173\u5185\u5bb9 using \u5173\u952e\u5b57\u8fd8\u53ef\u4ee5\u7528\u6765\u5b9a\u4e49\u7c7b\u578b\u522b\u540d\uff0c\u6211\u4eec\u4f1a\u5728 8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d \u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u7c7b\u578b\u522b\u540d\u3002","title":"using \u8bed\u53e5\u7684\u6700\u4f73\u5b9e\u8df5"},{"location":"6-13-Inline-functions/","tags":["inline"],"text":"Key Takeaway \u73b0\u4ee3\u7f16\u8bd1\u5668\u53ef\u4ee5\u51b3\u5b9a\u51fd\u6570\u662f\u5426\u5e94\u5f53\u88ab\u5c55\u5f00\u3002 \u6709\u4e9b\u51fd\u6570\u662f\u9690\u5f0f\u7684\u5185\u8054\u51fd\u6570\uff0c\u5305\u62ec\uff1a \u5b9a\u4e49\u5728\u7c7b\u3001\u7ed3\u6784\u4f53\u6216\u8054\u5408\u4f53\u4e2d\u7684\u51fd\u6570\uff1b Constexpr / consteval \u51fd\u6570 6.14 - Constexpr \u548c consteval \u51fd\u6570 inline \u5173\u952e\u5b57\u662f\u4e00\u79cd \u63d0\u793a \uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u5ffd\u7565\u5b83\u3002\u540c\u65f6\u7f16\u8bd1\u5668\u4e5f\u53ef\u4ee5\u5c55\u5f00\u6ca1\u6709\u6807\u8bb0 inline \u7684\u51fd\u6570\u3002 \u73b0\u4ee3 inline \u5173\u952e\u5b57\u7684\u542b\u4e49\u662f \u5141\u8bb8\u591a\u5904\u5b9a\u4e49 \u5185\u8054\u51fd\u6570\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff1a\u4e3a\u4e86\u80fd\u591f\u5c55\u5f00\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u5fc5\u987b\u80fd\u591f\u5728\u51fd\u6570\u8c03\u7528\u5904\u770b\u5230\u51fd\u6570\u7684\u5b9a\u4e49\u3002\u56e0\u6b64\uff0c\u5185\u8054\u51fd\u6570\u901a\u5e38\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u5b83\u4eec\u5c31\u53ef\u4ee5\u88ab\u5305\u542b\u5230\u4f7f\u7528\u5b83\u4eec\u7684\u6e90\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u7f16\u8bd1\u5668\u770b\u5230\u51fd\u6570\u7684\u5b8c\u6574\u5b9a\u4e49\u3002 \u6709\u4e9b\u65f6\u5019\u6211\u4eec\u9700\u8981\u7f16\u5199\u4ee3\u7801\u5b8c\u6210\u4e00\u4e9b\u72ec\u7acb\u7684\u4efb\u52a1\uff0c\u4f8b\u5982\u4ece\u7528\u6237\u8bfb\u53d6\u8f93\u5165\u3001\u8f93\u51fa\u5230\u6587\u4ef6\u6216\u8005\u8ba1\u7b97\u7279\u5b9a\u7684\u503c\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u6709\u4e24\u79cd\u9009\u62e9\uff1a \u5c06\u4ee3\u7801\u4f5c\u4e3a\u5df2\u6709\u51fd\u6570\u7684\u4e00\u90e8\u5206\uff08\u79f0\u4e3a\u5d4c\u5165\uff09\uff1b \u521b\u5efa\u4e00\u4e2a\u5355\u72ec\u7684\u51fd\u6570\uff08\u6216\u8005\u5b50\u51fd\u6570\uff09\u5b8c\u6210\u8be5\u4efb\u52a1\u3002 \u5c06\u8fd9\u4e9b\u4ee3\u7801\u4f5c\u4e3a\u51fd\u6570\u80fd\u591f\u5e26\u6765\u5f88\u591a\u6f5c\u5728\u7684\u597d\u5904\uff0c\u56e0\u4e3a\u51fd\u6570\u4e2d\u7684\u4ee3\u7801\uff1a \u53ef\u8bfb\u6027\u66f4\u597d\uff0c\u66f4\u5bb9\u6613\u7406\u89e3\uff1b \u53ef\u7528\u6027\u66f4\u597d\uff0c\u53ef\u4ee5\u5728\u4e0d\u4e86\u89e3\u51fd\u6570\u5b9e\u73b0\u7ec6\u8282\u7684\u524d\u63d0\u4e0b\u8c03\u7528\u51fd\u6570\uff1b \u66f4\u5bb9\u6613\u66f4\u65b0\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u4ee3\u7801\u53ef\u4ee5\u5728\u4e00\u5904\u4fee\u6539\u5904\u5904\u66f4\u65b0\uff1b \u53ef\u590d\u7528\u6027\u66f4\u597d\uff0c\u56e0\u4e3a\u51fd\u6570\u5929\u751f\u662f\u6a21\u5757\u5316\u7684\u3002 \u4e0d\u8fc7\uff0c\u51fd\u6570\u8c03\u7528\u4e5f\u662f\u4e5f\u6709\u4e0d\u597d\u7684\u5730\u65b9\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5e26\u6765\u989d\u5916\u7684\u6027\u80fd\u5f00\u9500\u3002\u4f8b\u5982\u4e0b\u5217\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int min ( int x , int y ) { return ( x < y ) ? x : y ; } int main () { std :: cout << min ( 5 , 6 ) << '\\n' ; std :: cout << min ( 3 , 2 ) << '\\n' ; return 0 ; } \u5f53 min() \u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0cCPU\u5fc5\u987b\u4fdd\u5b58\u5f53\u524d\u6307\u4ee4\u7684\u5730\u5740\uff08\u4ee5\u4fbf\u7a0d\u540e\u8fd4\u56de\uff09\u4ee5\u53ca\u4e00\u4e9bCPU\u5bc4\u5b58\u5668\u7684\u503c\uff08\u7528\u4e8e\u8fd4\u56de\u65f6\u6062\u590d\u4e0a\u4e0b\u6587\uff09\u3002\u7136\u540e \u5b9e\u53c2 x \u548c y \u5fc5\u987b\u88ab\u5b9e\u4f8b\u5316\u548c\u521d\u59cb\u5316\u3002\u7136\u540e\u8df3\u8f6c\u5230 min() \u51fd\u6570\u6267\u884c\u3002\u5f53\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u7a0b\u5e8f\u53c8\u4f1a\u8df3\u8f6c\u56de\u5230\u4e3b\u8c03\u51fd\u6570\uff0c\u540c\u65f6\u8fd4\u56de\u503c\u88ab\u62f7\u8d1d\u8fd4\u56de\u3002\u6362\u8a00\u4e4b\uff0c\u6bcf\u6b21\u51fd\u6570\u8c03\u7528\u90fd\u4f1a\u4ea7\u751f\u5f88\u5927\u7684\u5f00\u9500\u3002 \u5bf9\u4e8e\u90a3\u4e9b\u590d\u6742\u7684\u51fd\u6570\u6765\u8bf4\uff0c\u8fd9\u4e9b\u5f00\u9500\u76f8\u5bf9\u4e8e\u51fd\u6570\u6267\u884c\u5b9e\u9645\u6765\u8bb2\u53ef\u80fd\u662f\u5fae\u4e0d\u8db3\u9053\u7684\u3002\u4e0d\u8fc7\uff0c\u5bf9\u4e8e\u5f88\u591a\u5c0f\u51fd\u6570\uff08\u4f8b\u5982\u8fd9\u91cc\u7684 min() \uff09\u3002\u51fd\u6570\u8c03\u7528\u7684\u5f00\u9500\u751a\u81f3\u53ef\u80fd\u6bd4\u51fd\u6570\u5185\u90e8\u4ee3\u7801\u6267\u884c\u8017\u65f6\u8fd8\u8981\u9ad8\u3002\u5982\u679c\u8fd9\u4e9b\u51fd\u6570\u88ab\u9891\u7e41\u8c03\u7528\uff0c\u90a3\u4e48\u548c\u4e0d\u4f7f\u7528\u4ee3\u7801\u6765\u6bd4\u6027\u80fd\u5dee\u5f02\u4f1a\u975e\u5e38\u5927\u3002 \u5185\u8054\u5c55\u5f00 \u597d\u5728\uff0cC++\u7f16\u8bd1\u5668\u6709\u529e\u6cd5\u514b\u670d\u8fd9\u79cd\u5f00\u9500\uff1a\u5185\u8054\u5c55\u5f00\u53ef\u4ee5\u5c06\u51fd\u6570\u8c03\u7528\u6839\u636e\u5176\u5b9a\u4e49\u76f4\u63a5\u66ff\u6362\u4e3a\u5bf9\u5e94\u7684\u4ee3\u7801\u3002 \u4f8b\u5982\uff0c\u5982\u679c\u7f16\u8bd1\u5668\u80fd\u591f\u5c06\u4e0a\u9762\u7684 min() \u51fd\u6570\u5c55\u5f00\u7684\u8bdd\uff0c\u5c06\u4f1a\u5f97\u5230\u5982\u4e0b\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << (( 5 < 6 ) ? 5 : 6 ) << '\\n' ; std :: cout << (( 3 < 2 ) ? 3 : 2 ) << '\\n' ; return 0 ; } \u6ce8\u610f\uff0c min() \u51fd\u6570\u4e2d\u7684\u51fd\u6570\u8c03\u7528\u88ab\u5176\u51fd\u6570\u4f53\u4ee3\u7801\u66ff\u6362\uff08\u5b9e\u53c2\u6570\u503c\u66ff\u6362\u4e86\u5f62\u53c2\uff09\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u907f\u514d\u51fd\u6570\u8c03\u7528\u5e26\u6765\u7684\u989d\u5916\u5f00\u9500\u3002 \u5185\u8054\u4ee3\u7801\u7684\u6027\u80fd \u9664\u4e86\u80fd\u591f\u907f\u514d\u51fd\u6570\u8c03\u7528\u7684\u5f00\u9500\uff0c\u5185\u8054\u5c55\u5f00\u8fd8\u53ef\u4ee5\u4f7f\u7f16\u8bd1\u5668\u66f4\u52a0\u6709\u6548\u5730\u4f18\u5316\u4ee3\u7801\u2014\u2014\u4f8b\u5982\uff0c\u56e0\u4e3a\u8868\u8fbe\u5f0f ((5 < 6) ? 5 : 6) \u73b0\u5728\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c31\u53ef\u4ee5\u5c06 main() \u51fd\u6570\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u4f18\u5316\u4e3a std::cout << 5 << '\\n'; \u3002 \u4e0d\u8fc7\uff0c\u5185\u8054\u5c55\u5f00\u4e5f\u6709\u5b83\u7684\u5f00\u9500\uff1a\u5982\u679c\u5c55\u5f00\u51fd\u6570\u4f53\u6240\u9700\u7684\u6307\u4ee4\u6bd4\u51fd\u6570\u8c03\u7528\u672c\u8eab\u8fd8\u591a\uff0c\u90a3\u4e48\u6bcf\u4e00\u5904\u5185\u8054\u5c55\u5f00\u5c06\u4f1a\u5bfc\u81f4\u53ef\u6267\u884c\u6587\u4ef6\u7684\u81a8\u80c0\u3002\u53ef\u6267\u884c\u7a0b\u5e8f\u8d8a\u5927\uff0c\u901f\u5ea6\u4e5f\u53ef\u80fd\u4f1a\u8d8a\u5dee\uff08\u7531\u4e8e\u4e0d\u80fd\u5339\u914d\u7f13\u5b58\u7684\u5927\u5c0f\uff09\u3002 \u786e\u5b9a\u51fd\u6570\u662f\u5426\u5e94\u8be5\u88ab\u5b9a\u4e49\u4e3a\u5185\u8054\uff08\u79fb\u9664\u51fd\u6570\u8c03\u7528\u5f00\u9500\u5e26\u6765\u7684\u597d\u5904\u5927\u4e8e\u53ef\u6267\u884c\u6587\u4ef6\u5927\u5c0f\u589e\u957f\u5e26\u6765\u7684\u574f\u5904\uff09\u7684\u4f9d\u636e\u5e76\u4e0d\u660e\u663e\u3002\u5185\u8054\u5c55\u5f00\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u7684\u63d0\u5347\u3001\u6027\u80fd\u7684\u4e0b\u964d\u6216\u8005\u5bf9\u6027\u80fd\u5b8c\u5168\u6ca1\u6709\u5f71\u54cd\uff0c\u8fd9\u53d6\u51b3\u4e8e\u51fd\u6570\u8c03\u7528\u7684\u5f00\u9500\uff0c\u51fd\u6570\u5927\u5c0f\u548c\u7f16\u8bd1\u5668\u80fd\u591f\u6267\u884c\u54ea\u4e9b\u4f18\u5316\u3002 \u5185\u8054\u5c55\u5f00\u6700\u9002\u5408\u7b80\u5355\u3001\u5c0f\u7684\u51fd\u6570\uff08\u4f8b\u5982\u53ea\u6709\u51e0\u884c\u8bed\u53e5\u7684\u51fd\u6570\uff09\uff0c\u7279\u522b\u662f\u4f1a\u88ab\u591a\u6b21\u8c03\u7528\u7684\u51fd\u6570\uff08\u4f8b\u5982\u5faa\u73af\u4e2d\u7684\u51fd\u6570\uff09\u3002 \u5185\u8054\u4ee3\u7801\u7684\u5c55\u5f00\u65f6\u673a \u6240\u6709\u7684\u51fd\u6570\u90fd\u53ef\u4ee5\u5f52\u4e3a\u4ee5\u4e0b\u4e09\u7c7b\uff1a \u5fc5\u987b\u88ab\u5c55\u5f00\uff1b \u53ef\u80fd\u4f1a\u88ab\u5c55\u5f00(\u5f88\u591a\u51fd\u6570\u90fd\u5c5e\u4e8e\u8fd9\u4e00\u7c7b). \u4e0d\u80fd\u88ab\u5c55\u5f00\u3002 \u80fd\u591f\u88ab\u5c55\u5f00\u7684\u51fd\u6570\u79f0\u4e3a\u5185\u8054\u51fd\u6570\u3002 \u5927\u591a\u6570\u51fd\u6570\u90fd\u5c5e\u4e8e\u7b2c\u4e8c\u7c7b\uff1a\u201c\u53ef\u80fd\u4f1a\u88ab\u5c55\u5f00\u201d\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u51fd\u6570\u6765\u8bf4\uff0c\u5982\u679c\u5c55\u5f00\u80fd\u591f\u5e26\u6765\u597d\u5904\uff0c\u90a3\u4e48\u5c31\u53ef\u80fd\u88ab\u5c55\u5f00\u3002\u5bf9\u4e8e\u8fd9\u4e00\u7c7b\u51fd\u6570\uff0c\u73b0\u4ee3\u7f16\u8bd1\u5668\u53ef\u4ee5\u5206\u6790\u6bcf\u4e2a\u51fd\u6570\u548c\u6bcf\u5904\u51fd\u6570\u8c03\u7528\u6765\u786e\u5b9a\u5bf9\u5e94\u7684\u51fd\u6570\u662f\u4e0d\u662f\u88ab\u5185\u8054\u5c55\u5f00\u540e\u7684\u6536\u76ca\u66f4\u9ad8\u3002\u7f16\u8bd1\u5668\u6700\u7ec8\u4f1a\u51b3\u5b9a\u662f\u5426\u5c06\u5b83\u4eec\u4e2d\u4e00\u4e9b\u3001\u6216\u8005\u5168\u90e8\u5c55\u5f00\u3002 \u5c0f\u8d34\u58eb \u73b0\u4ee3\u7f16\u8bd1\u5668\u53ef\u4ee5\u51b3\u5b9a\u51fd\u6570\u662f\u5426\u5e94\u5f53\u88ab\u5c55\u5f00\u3002 \u6269\u5c55\u9605\u8bfb \u6709\u4e9b\u51fd\u6570\u662f\u9690\u5f0f\u7684\u5185\u8054\u51fd\u6570\uff0c\u5305\u62ec\uff1a - \u5b9a\u4e49\u5728\u7c7b\u3001\u7ed3\u6784\u4f53\u6216\u8054\u5408\u4f53\u4e2d\u7684\u51fd\u6570\uff1b - Constexpr / consteval \u51fd\u6570 6.14 - Constexpr \u548c consteval \u51fd\u6570 \u5386\u53f2\u4e0a\u7684 inline \u5173\u952e\u5b57 \u56de\u987e C++ \u7684\u5386\u53f2\uff0c\u7f16\u8bd1\u5668\u66fe\u7ecf\u8981\u4e48\u4e0d\u5177\u5907\u5224\u65ad\u51fd\u6570\u5c55\u5f00\u662f\u5426\u80fd\u591f\u5e26\u6765\u597d\u5904\u7684\u80fd\u529b\uff0c\u8981\u4e48\u4e0d\u80fd\u591f\u5f88\u597d\u7684\u6267\u884c\u4e0a\u8ff0\u5224\u65ad\u3002\u56e0\u6b64\uff0cC++\u7279\u610f\u63d0\u4f9b\u4e86 inline \u5173\u952e\u5b57\uff0c\u7528\u4e8e \u63d0\u793a \u7f16\u8bd1\u5668\u8be5\u51fd\u6570\u5e94\u8be5\u88ab\u5185\u8054\u5c55\u5f00\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include inline int min ( int x , int y ) // hint to the compiler that it should do inline expansion of this function { return ( x < y ) ? x : y ; } int main () { std :: cout << min ( 5 , 6 ) << '\\n' ; std :: cout << min ( 3 , 2 ) << '\\n' ; return 0 ; } \u8fd9\u5c31\u662f\u6240\u8c13\u201c\u5185\u8054\u51fd\u6570\u201d\u7684\u6765\u5386\uff08\u56e0\u4e3a\u8fd9\u4e9b\u51fd\u6570\u7684\u58f0\u660e\u8bed\u6cd5\u4e2d\u5305\u542b inline \u5173\u952e\u5b57\uff09\u3002 \u4f46\u662f\uff0c\u73b0\u4ee3 C++ \u4e2d\uff0c inline \u5173\u952e\u5b57\u5df2\u7ecf\u4e0d\u518d\u88ab\u7528\u4e8e\u544a\u8bc9\u7f16\u8bd1\u5668\u5c55\u5f00\u5bf9\u5e94\u7684\u51fd\u6570\uff0c\u8fd9\u4e48\u505a\u7684\u539f\u56e0\u6709\u5f88\u591a\uff1a \u4f7f\u7528 inline \u8981\u6c42\u5185\u8054\u5c55\u5f00\u662f\u4e00\u79cd\u8fc7\u65e9\u4f18\u5316\u884c\u4e3a\uff0c\u6ee5\u7528 inline \u6709\u53cd\u5012\u4f1a\u4f24\u5bb3\u7a0b\u5e8f\u6027\u80fd\uff1b inline \u5173\u952e\u5b57\u53ea\u662f\u4e00\u4e2a \u63d0\u793a \u2014\u2014\u7f16\u8bd1\u5668\u5b8c\u5168\u53ef\u4ee5\u5ffd\u7565\u5b83\uff0c\u5c24\u5176\u662f\u5f53\u4f60\u8981\u6c42\u7f16\u8bd1\u5668\u5185\u8054\u5c55\u5f00\u4e00\u4e2a\u5f88\u5927\u7684\u51fd\u6570\u65f6\u3002\u540c\u65f6\uff0c\u7f16\u8bd1\u5668\u4e5f\u53ef\u4ee5\u5c55\u5f00\u90a3\u4e9b\u5e76\u6ca1\u6709\u88ab\u6807\u8bb0\u4e3a inline \u7684\u51fd\u6570\u3002 inline \u5173\u952e\u5b57\u7684\u5b9a\u4e49\u5904\u4e8e\u4e00\u79cd\u4e0d\u5408\u9002\u7684\u7c92\u5ea6\u3002\u6211\u4eec\u5728\u51fd\u6570\u58f0\u660e\u4e2d\u4f7f\u7528\u8be5\u5173\u952e\u5b57\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u51fd\u6570\u7684\u5c55\u5f00\u662f\u57fa\u4e8e\u6bcf\u4e2a\u51fd\u6570\u8c03\u7528\u7684\u3002\u5bf9\u4e8e\u4e00\u4e2a\u51fd\u6570\u7684\u5404\u5904\u8c03\u7528\uff0c\u5b83\u4eec\u4e2d\u7684\u4e00\u90e8\u5206\u53ef\u80fd\u88ab\u5c55\u5f00\uff0c\u800c\u53e6\u4e00\u4e9b\u5219\u4e0d\u88ab\u5c55\u5f00\uff0c\u7136\u800c\u8fd9\u4e2a\u5173\u952e\u5b57\u5374\u4e0d\u80fd\u5bf9\u6b64\u4ea7\u751f\u5f71\u54cd\u3002 \u73b0\u4ee3\u7f16\u8bd1\u5668\u901a\u5e38\u5f88\u64c5\u957f\u51b3\u5b9a\u90a3\u4e9b\u51fd\u6570\u5e94\u8be5\u88ab\u4f5c\u4e3a\u5185\u8054\u51fd\u6570\u2014\u2014\u5b83\u7684\u8fd9\u79cd\u80fd\u529b\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u90fd\u6bd4\u4eba\u7c7b\u5f3a\u3002 \u56e0\u6b64\uff0c\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u5ffd\u7565\u4f60\u4e3a\u51fd\u6570\u6dfb\u52a0\u7684 inline \u6807\u8bb0\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e0d\u8981\u4f7f\u7528 inline \u5173\u952e\u5b57\u8981\u6c42\u5185\u8054\u5c55\u5f00\u51fd\u6570\u3002 \u73b0\u4ee3 inline \u5173\u952e\u5b57 \u5728\u4e4b\u524d\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u5934\u6587\u4ef6\u4e2d\u4e0d\u5e94\u8be5\u653e\u7f6e\u51fd\u6570\u7684\u5b9e\u73b0\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u5934\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u4f1a\u88ab\u5305\u542b\u5230\u591a\u4e2a.cpp\u6587\u4ef6\u4e2d\uff0c\u90a3\u4e48\u8fd9\u4e9b\u51fd\u6570\u7684\u5b9a\u4e49\u4e5f\u5c31\u4f1a\u88ab\u590d\u5236\u591a\u4efd\u3002\u56e0\u6b64\uff0c\u8fd9\u4e9b\u6587\u4ef6\u7f16\u8bd1\u540e\uff0c\u94fe\u63a5\u5668\u5c31\u4f1a\u62a5\u9519\uff0c\u56e0\u4e3a\u51fd\u6570\u7684\u5b9a\u4e49\u8fdd\u53cd\u4e86 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u3002 \u5728 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\uff0c\u5728\u73b0\u4ee3 C++ \u4e2d\uff0c inline \u6982\u5ff5\u53d1\u5c55\u51fa\u4e86\u65b0\u7684\u542b\u4e49\uff1a\u5141\u8bb8\u591a\u91cd\u5b9a\u4e49\u3002\u5bf9\u4e8e\u51fd\u6570\u548c\u53d8\u91cf\u90fd\u662f\u9002\u7528\u7684\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u5c06\u51fd\u6570\u6807\u8bb0\u4e3a\u5185\u8054\u540e\uff0c\u8be5\u51fd\u6570\u5c31\u53ef\u4ee5\u88ab\u591a\u6b21\u5b9a\u4e49\u4e86\uff08\u5728\u4e0d\u540c\u7684\u51fd\u6570\u4e2d\uff09\uff0c\u53ea\u8981\u8fd9\u4e9b\u5b9a\u4e49\u662f\u4e00\u81f4\u7684\u5373\u53ef\u3002 \u4e3a\u4e86\u80fd\u591f\u5c55\u5f00\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u5fc5\u987b\u80fd\u591f\u5728\u51fd\u6570\u8c03\u7528\u5904\u770b\u5230\u51fd\u6570\u7684\u5b9a\u4e49\u3002\u56e0\u6b64\uff0c\u5185\u8054\u51fd\u6570\u901a\u5e38\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u5b83\u4eec\u5c31\u53ef\u4ee5\u88ab\u5305\u542b\u5230\u4f7f\u7528\u5b83\u4eec\u7684\u6e90\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u7f16\u8bd1\u5668\u770b\u5230\u51fd\u6570\u7684\u5b8c\u6574\u5b9a\u4e49\u3002 \u5173\u952e\u4fe1\u606f \u7f16\u8bd1\u5668\u5fc5\u987b\u80fd\u591f\u5728\u5185\u8054\u51fd\u6570\u8c03\u7528\u5904\u770b\u5230\u51fd\u6570\u7684\u5b8c\u6574\u5b9a\u4e49\u3002 \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4e0d\u9700\u8981\u5c06\u51fd\u6570\u5b9a\u4e49\u4e3a\u5185\u8054\uff0c\u4f46\u662f\u6211\u4eec\u5728\u5c06\u6765\u7684\u8bfe\u7a0b\u4e2d\u4f1a\u770b\u5230\u5b83\u6709\u7528\u7684\u573a\u666f\u3002 \u6700\u4f73\u5b9e\u8df5 \u9664\u975e\u6709\u7279\u6b8a\u7684\u3001\u4ee4\u4eba\u4fe1\u670d\u7684\u7406\u7531\uff0c\u5426\u5219\u4e0d\u8981\u4f7f\u7528 inline \u5173\u952e\u5b57\u3002","title":"6.13 - \u5185\u8054\u51fd\u6570"},{"location":"6-13-Inline-functions/#_1","text":"\u597d\u5728\uff0cC++\u7f16\u8bd1\u5668\u6709\u529e\u6cd5\u514b\u670d\u8fd9\u79cd\u5f00\u9500\uff1a\u5185\u8054\u5c55\u5f00\u53ef\u4ee5\u5c06\u51fd\u6570\u8c03\u7528\u6839\u636e\u5176\u5b9a\u4e49\u76f4\u63a5\u66ff\u6362\u4e3a\u5bf9\u5e94\u7684\u4ee3\u7801\u3002 \u4f8b\u5982\uff0c\u5982\u679c\u7f16\u8bd1\u5668\u80fd\u591f\u5c06\u4e0a\u9762\u7684 min() \u51fd\u6570\u5c55\u5f00\u7684\u8bdd\uff0c\u5c06\u4f1a\u5f97\u5230\u5982\u4e0b\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << (( 5 < 6 ) ? 5 : 6 ) << '\\n' ; std :: cout << (( 3 < 2 ) ? 3 : 2 ) << '\\n' ; return 0 ; } \u6ce8\u610f\uff0c min() \u51fd\u6570\u4e2d\u7684\u51fd\u6570\u8c03\u7528\u88ab\u5176\u51fd\u6570\u4f53\u4ee3\u7801\u66ff\u6362\uff08\u5b9e\u53c2\u6570\u503c\u66ff\u6362\u4e86\u5f62\u53c2\uff09\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u907f\u514d\u51fd\u6570\u8c03\u7528\u5e26\u6765\u7684\u989d\u5916\u5f00\u9500\u3002","title":"\u5185\u8054\u5c55\u5f00"},{"location":"6-13-Inline-functions/#_2","text":"\u9664\u4e86\u80fd\u591f\u907f\u514d\u51fd\u6570\u8c03\u7528\u7684\u5f00\u9500\uff0c\u5185\u8054\u5c55\u5f00\u8fd8\u53ef\u4ee5\u4f7f\u7f16\u8bd1\u5668\u66f4\u52a0\u6709\u6548\u5730\u4f18\u5316\u4ee3\u7801\u2014\u2014\u4f8b\u5982\uff0c\u56e0\u4e3a\u8868\u8fbe\u5f0f ((5 < 6) ? 5 : 6) \u73b0\u5728\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c31\u53ef\u4ee5\u5c06 main() \u51fd\u6570\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u4f18\u5316\u4e3a std::cout << 5 << '\\n'; \u3002 \u4e0d\u8fc7\uff0c\u5185\u8054\u5c55\u5f00\u4e5f\u6709\u5b83\u7684\u5f00\u9500\uff1a\u5982\u679c\u5c55\u5f00\u51fd\u6570\u4f53\u6240\u9700\u7684\u6307\u4ee4\u6bd4\u51fd\u6570\u8c03\u7528\u672c\u8eab\u8fd8\u591a\uff0c\u90a3\u4e48\u6bcf\u4e00\u5904\u5185\u8054\u5c55\u5f00\u5c06\u4f1a\u5bfc\u81f4\u53ef\u6267\u884c\u6587\u4ef6\u7684\u81a8\u80c0\u3002\u53ef\u6267\u884c\u7a0b\u5e8f\u8d8a\u5927\uff0c\u901f\u5ea6\u4e5f\u53ef\u80fd\u4f1a\u8d8a\u5dee\uff08\u7531\u4e8e\u4e0d\u80fd\u5339\u914d\u7f13\u5b58\u7684\u5927\u5c0f\uff09\u3002 \u786e\u5b9a\u51fd\u6570\u662f\u5426\u5e94\u8be5\u88ab\u5b9a\u4e49\u4e3a\u5185\u8054\uff08\u79fb\u9664\u51fd\u6570\u8c03\u7528\u5f00\u9500\u5e26\u6765\u7684\u597d\u5904\u5927\u4e8e\u53ef\u6267\u884c\u6587\u4ef6\u5927\u5c0f\u589e\u957f\u5e26\u6765\u7684\u574f\u5904\uff09\u7684\u4f9d\u636e\u5e76\u4e0d\u660e\u663e\u3002\u5185\u8054\u5c55\u5f00\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u7684\u63d0\u5347\u3001\u6027\u80fd\u7684\u4e0b\u964d\u6216\u8005\u5bf9\u6027\u80fd\u5b8c\u5168\u6ca1\u6709\u5f71\u54cd\uff0c\u8fd9\u53d6\u51b3\u4e8e\u51fd\u6570\u8c03\u7528\u7684\u5f00\u9500\uff0c\u51fd\u6570\u5927\u5c0f\u548c\u7f16\u8bd1\u5668\u80fd\u591f\u6267\u884c\u54ea\u4e9b\u4f18\u5316\u3002 \u5185\u8054\u5c55\u5f00\u6700\u9002\u5408\u7b80\u5355\u3001\u5c0f\u7684\u51fd\u6570\uff08\u4f8b\u5982\u53ea\u6709\u51e0\u884c\u8bed\u53e5\u7684\u51fd\u6570\uff09\uff0c\u7279\u522b\u662f\u4f1a\u88ab\u591a\u6b21\u8c03\u7528\u7684\u51fd\u6570\uff08\u4f8b\u5982\u5faa\u73af\u4e2d\u7684\u51fd\u6570\uff09\u3002","title":"\u5185\u8054\u4ee3\u7801\u7684\u6027\u80fd"},{"location":"6-13-Inline-functions/#_3","text":"\u6240\u6709\u7684\u51fd\u6570\u90fd\u53ef\u4ee5\u5f52\u4e3a\u4ee5\u4e0b\u4e09\u7c7b\uff1a \u5fc5\u987b\u88ab\u5c55\u5f00\uff1b \u53ef\u80fd\u4f1a\u88ab\u5c55\u5f00(\u5f88\u591a\u51fd\u6570\u90fd\u5c5e\u4e8e\u8fd9\u4e00\u7c7b). \u4e0d\u80fd\u88ab\u5c55\u5f00\u3002 \u80fd\u591f\u88ab\u5c55\u5f00\u7684\u51fd\u6570\u79f0\u4e3a\u5185\u8054\u51fd\u6570\u3002 \u5927\u591a\u6570\u51fd\u6570\u90fd\u5c5e\u4e8e\u7b2c\u4e8c\u7c7b\uff1a\u201c\u53ef\u80fd\u4f1a\u88ab\u5c55\u5f00\u201d\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u51fd\u6570\u6765\u8bf4\uff0c\u5982\u679c\u5c55\u5f00\u80fd\u591f\u5e26\u6765\u597d\u5904\uff0c\u90a3\u4e48\u5c31\u53ef\u80fd\u88ab\u5c55\u5f00\u3002\u5bf9\u4e8e\u8fd9\u4e00\u7c7b\u51fd\u6570\uff0c\u73b0\u4ee3\u7f16\u8bd1\u5668\u53ef\u4ee5\u5206\u6790\u6bcf\u4e2a\u51fd\u6570\u548c\u6bcf\u5904\u51fd\u6570\u8c03\u7528\u6765\u786e\u5b9a\u5bf9\u5e94\u7684\u51fd\u6570\u662f\u4e0d\u662f\u88ab\u5185\u8054\u5c55\u5f00\u540e\u7684\u6536\u76ca\u66f4\u9ad8\u3002\u7f16\u8bd1\u5668\u6700\u7ec8\u4f1a\u51b3\u5b9a\u662f\u5426\u5c06\u5b83\u4eec\u4e2d\u4e00\u4e9b\u3001\u6216\u8005\u5168\u90e8\u5c55\u5f00\u3002 \u5c0f\u8d34\u58eb \u73b0\u4ee3\u7f16\u8bd1\u5668\u53ef\u4ee5\u51b3\u5b9a\u51fd\u6570\u662f\u5426\u5e94\u5f53\u88ab\u5c55\u5f00\u3002 \u6269\u5c55\u9605\u8bfb \u6709\u4e9b\u51fd\u6570\u662f\u9690\u5f0f\u7684\u5185\u8054\u51fd\u6570\uff0c\u5305\u62ec\uff1a - \u5b9a\u4e49\u5728\u7c7b\u3001\u7ed3\u6784\u4f53\u6216\u8054\u5408\u4f53\u4e2d\u7684\u51fd\u6570\uff1b - Constexpr / consteval \u51fd\u6570 6.14 - Constexpr \u548c consteval \u51fd\u6570","title":"\u5185\u8054\u4ee3\u7801\u7684\u5c55\u5f00\u65f6\u673a"},{"location":"6-13-Inline-functions/#inline","text":"\u56de\u987e C++ \u7684\u5386\u53f2\uff0c\u7f16\u8bd1\u5668\u66fe\u7ecf\u8981\u4e48\u4e0d\u5177\u5907\u5224\u65ad\u51fd\u6570\u5c55\u5f00\u662f\u5426\u80fd\u591f\u5e26\u6765\u597d\u5904\u7684\u80fd\u529b\uff0c\u8981\u4e48\u4e0d\u80fd\u591f\u5f88\u597d\u7684\u6267\u884c\u4e0a\u8ff0\u5224\u65ad\u3002\u56e0\u6b64\uff0cC++\u7279\u610f\u63d0\u4f9b\u4e86 inline \u5173\u952e\u5b57\uff0c\u7528\u4e8e \u63d0\u793a \u7f16\u8bd1\u5668\u8be5\u51fd\u6570\u5e94\u8be5\u88ab\u5185\u8054\u5c55\u5f00\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include inline int min ( int x , int y ) // hint to the compiler that it should do inline expansion of this function { return ( x < y ) ? x : y ; } int main () { std :: cout << min ( 5 , 6 ) << '\\n' ; std :: cout << min ( 3 , 2 ) << '\\n' ; return 0 ; } \u8fd9\u5c31\u662f\u6240\u8c13\u201c\u5185\u8054\u51fd\u6570\u201d\u7684\u6765\u5386\uff08\u56e0\u4e3a\u8fd9\u4e9b\u51fd\u6570\u7684\u58f0\u660e\u8bed\u6cd5\u4e2d\u5305\u542b inline \u5173\u952e\u5b57\uff09\u3002 \u4f46\u662f\uff0c\u73b0\u4ee3 C++ \u4e2d\uff0c inline \u5173\u952e\u5b57\u5df2\u7ecf\u4e0d\u518d\u88ab\u7528\u4e8e\u544a\u8bc9\u7f16\u8bd1\u5668\u5c55\u5f00\u5bf9\u5e94\u7684\u51fd\u6570\uff0c\u8fd9\u4e48\u505a\u7684\u539f\u56e0\u6709\u5f88\u591a\uff1a \u4f7f\u7528 inline \u8981\u6c42\u5185\u8054\u5c55\u5f00\u662f\u4e00\u79cd\u8fc7\u65e9\u4f18\u5316\u884c\u4e3a\uff0c\u6ee5\u7528 inline \u6709\u53cd\u5012\u4f1a\u4f24\u5bb3\u7a0b\u5e8f\u6027\u80fd\uff1b inline \u5173\u952e\u5b57\u53ea\u662f\u4e00\u4e2a \u63d0\u793a \u2014\u2014\u7f16\u8bd1\u5668\u5b8c\u5168\u53ef\u4ee5\u5ffd\u7565\u5b83\uff0c\u5c24\u5176\u662f\u5f53\u4f60\u8981\u6c42\u7f16\u8bd1\u5668\u5185\u8054\u5c55\u5f00\u4e00\u4e2a\u5f88\u5927\u7684\u51fd\u6570\u65f6\u3002\u540c\u65f6\uff0c\u7f16\u8bd1\u5668\u4e5f\u53ef\u4ee5\u5c55\u5f00\u90a3\u4e9b\u5e76\u6ca1\u6709\u88ab\u6807\u8bb0\u4e3a inline \u7684\u51fd\u6570\u3002 inline \u5173\u952e\u5b57\u7684\u5b9a\u4e49\u5904\u4e8e\u4e00\u79cd\u4e0d\u5408\u9002\u7684\u7c92\u5ea6\u3002\u6211\u4eec\u5728\u51fd\u6570\u58f0\u660e\u4e2d\u4f7f\u7528\u8be5\u5173\u952e\u5b57\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u51fd\u6570\u7684\u5c55\u5f00\u662f\u57fa\u4e8e\u6bcf\u4e2a\u51fd\u6570\u8c03\u7528\u7684\u3002\u5bf9\u4e8e\u4e00\u4e2a\u51fd\u6570\u7684\u5404\u5904\u8c03\u7528\uff0c\u5b83\u4eec\u4e2d\u7684\u4e00\u90e8\u5206\u53ef\u80fd\u88ab\u5c55\u5f00\uff0c\u800c\u53e6\u4e00\u4e9b\u5219\u4e0d\u88ab\u5c55\u5f00\uff0c\u7136\u800c\u8fd9\u4e2a\u5173\u952e\u5b57\u5374\u4e0d\u80fd\u5bf9\u6b64\u4ea7\u751f\u5f71\u54cd\u3002 \u73b0\u4ee3\u7f16\u8bd1\u5668\u901a\u5e38\u5f88\u64c5\u957f\u51b3\u5b9a\u90a3\u4e9b\u51fd\u6570\u5e94\u8be5\u88ab\u4f5c\u4e3a\u5185\u8054\u51fd\u6570\u2014\u2014\u5b83\u7684\u8fd9\u79cd\u80fd\u529b\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u90fd\u6bd4\u4eba\u7c7b\u5f3a\u3002 \u56e0\u6b64\uff0c\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u5ffd\u7565\u4f60\u4e3a\u51fd\u6570\u6dfb\u52a0\u7684 inline \u6807\u8bb0\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e0d\u8981\u4f7f\u7528 inline \u5173\u952e\u5b57\u8981\u6c42\u5185\u8054\u5c55\u5f00\u51fd\u6570\u3002","title":"\u5386\u53f2\u4e0a\u7684 inline \u5173\u952e\u5b57"},{"location":"6-13-Inline-functions/#inline_1","text":"\u5728\u4e4b\u524d\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u5934\u6587\u4ef6\u4e2d\u4e0d\u5e94\u8be5\u653e\u7f6e\u51fd\u6570\u7684\u5b9e\u73b0\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u5934\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u4f1a\u88ab\u5305\u542b\u5230\u591a\u4e2a.cpp\u6587\u4ef6\u4e2d\uff0c\u90a3\u4e48\u8fd9\u4e9b\u51fd\u6570\u7684\u5b9a\u4e49\u4e5f\u5c31\u4f1a\u88ab\u590d\u5236\u591a\u4efd\u3002\u56e0\u6b64\uff0c\u8fd9\u4e9b\u6587\u4ef6\u7f16\u8bd1\u540e\uff0c\u94fe\u63a5\u5668\u5c31\u4f1a\u62a5\u9519\uff0c\u56e0\u4e3a\u51fd\u6570\u7684\u5b9a\u4e49\u8fdd\u53cd\u4e86 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u3002 \u5728 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\uff0c\u5728\u73b0\u4ee3 C++ \u4e2d\uff0c inline \u6982\u5ff5\u53d1\u5c55\u51fa\u4e86\u65b0\u7684\u542b\u4e49\uff1a\u5141\u8bb8\u591a\u91cd\u5b9a\u4e49\u3002\u5bf9\u4e8e\u51fd\u6570\u548c\u53d8\u91cf\u90fd\u662f\u9002\u7528\u7684\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u5c06\u51fd\u6570\u6807\u8bb0\u4e3a\u5185\u8054\u540e\uff0c\u8be5\u51fd\u6570\u5c31\u53ef\u4ee5\u88ab\u591a\u6b21\u5b9a\u4e49\u4e86\uff08\u5728\u4e0d\u540c\u7684\u51fd\u6570\u4e2d\uff09\uff0c\u53ea\u8981\u8fd9\u4e9b\u5b9a\u4e49\u662f\u4e00\u81f4\u7684\u5373\u53ef\u3002 \u4e3a\u4e86\u80fd\u591f\u5c55\u5f00\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u5fc5\u987b\u80fd\u591f\u5728\u51fd\u6570\u8c03\u7528\u5904\u770b\u5230\u51fd\u6570\u7684\u5b9a\u4e49\u3002\u56e0\u6b64\uff0c\u5185\u8054\u51fd\u6570\u901a\u5e38\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u5b83\u4eec\u5c31\u53ef\u4ee5\u88ab\u5305\u542b\u5230\u4f7f\u7528\u5b83\u4eec\u7684\u6e90\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u7f16\u8bd1\u5668\u770b\u5230\u51fd\u6570\u7684\u5b8c\u6574\u5b9a\u4e49\u3002 \u5173\u952e\u4fe1\u606f \u7f16\u8bd1\u5668\u5fc5\u987b\u80fd\u591f\u5728\u5185\u8054\u51fd\u6570\u8c03\u7528\u5904\u770b\u5230\u51fd\u6570\u7684\u5b8c\u6574\u5b9a\u4e49\u3002 \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4e0d\u9700\u8981\u5c06\u51fd\u6570\u5b9a\u4e49\u4e3a\u5185\u8054\uff0c\u4f46\u662f\u6211\u4eec\u5728\u5c06\u6765\u7684\u8bfe\u7a0b\u4e2d\u4f1a\u770b\u5230\u5b83\u6709\u7528\u7684\u573a\u666f\u3002 \u6700\u4f73\u5b9e\u8df5 \u9664\u975e\u6709\u7279\u6b8a\u7684\u3001\u4ee4\u4eba\u4fe1\u670d\u7684\u7406\u7531\uff0c\u5426\u5219\u4e0d\u8981\u4f7f\u7528 inline \u5173\u952e\u5b57\u3002","title":"\u73b0\u4ee3 inline \u5173\u952e\u5b57"},{"location":"6-14-Constexpr-and-consteval-functions/","tags":["constexpr","consteval","C++20"],"text":"Key Takeaway \u4e3a\u4e86\u80fd\u591f\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u51fd\u6570\u5fc5\u987b\u5177\u6709 constexpr \u7c7b\u578b\u7684\u8fd4\u56de\u503c\uff0c\u540c\u65f6\u4e0d\u80fd\u591f\u8c03\u7528\u4efb\u4f55\u975econstexpr\u51fd\u6570\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u4e5f\u5fc5\u987b\u4f7f\u7528 constexpr \u5b9e\u53c2 (\u4f8b\u5982 constexpr \u53d8\u91cf\u6216\u5b57\u9762\u91cf)\u3002 constexpr \u51fd\u6570\u5177\u6709\u9690\u542b\u7684\u5185\u8054\u5c5e\u6027\uff0c\u8fd9\u4f7f\u5f97\u5b83\u4eec\u80fd\u591f\u514d\u4e8e\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u7684\u9650\u5236\u3002\u56e0\u4e3a\u8fd0\u884c\u65f6\u6c42\u503c\u5fc5\u987b\u80fd\u591f\u8ba9\u7f16\u8bd1\u5668\u5728\u51fd\u6570\u8c03\u7528\u5904\u770b\u5230\u51fd\u6570\u5b8c\u6574\u5b9a\u4e49\u3002\u6240\u4ee5\u5b83\u4eec\u5e94\u8be5\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u4e14\u88ab\u5305\u542b\u5230\u4f7f\u7528\u5b83\u4eec\u7684\u6e90\u6587\u4ef6\u3002 \u6839\u636e C++ \u6807\u51c6\uff0c\u5982\u679c constexpr \u51fd\u6570 \u6709\u8d44\u683c \u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c \u800c\u4e14\u5b83\u7684\u8fd4\u56de\u503c\u88ab\u7528\u5728\u5e38\u6570\u8868\u8fbe\u5f0f\u4e2d\uff0c\u90a3\u4e48\u5c31 \u5fc5\u987b \u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\u3002 \u4f7f\u7528\u6ee1\u8db3\u6761\u4ef6\u7684\u51fd\u6570\u8fd4\u56de\u503c\u521d\u59cb\u5316\u4e00\u4e2a constexpr \u53d8\u91cf\u53ef\u4ee5\u786e\u4fdd\u51fd\u6570\u5728\u7f16\u8bd1\u65f6\u6c42\u503c C++20 \u5f15\u5165\u4e86 consteval \u5173\u952e\u5b57\uff0c\u5b83\u53ef\u4ee5\u6307\u5b9a\u51fd\u6570 \u5fc5\u987b \u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u5426\u5219\u5c06\u4f1a\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002\u8fd9\u79cd\u51fd\u6570\u88ab\u79f0\u4e3a \u5373\u65f6\u51fd\u6570(immediate functions) \u3002 \u4f7f\u7528 consteval \u53ef\u4ee5\u8ba9 constexpr \u51fd\u6570\u5728\u7f16\u8bd1\u65f6\u6267\u884c \u5728 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86 constexpr \u5173\u952e\u5b57\uff0c\u5b9e\u7528\u5b83\u53ef\u4ee5\u521b\u5efa \u7f16\u8bd1\u65f6 \u7684 \u7b26\u53f7\u5e38\u91cf \u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86\u5e38\u91cf\u8868\u8fbe\u5f0f\u2014\u2014\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\u8868\u8fbe\u5f0f\u3002 \u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { constexpr int x { 5 }; constexpr int y { 6 }; std :: cout << ( x > y ? x : y ) << \" is greater!\" ; return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 6 is greater! \u56e0\u4e3a x \u548c y \u662f constexpr \uff0c\u7f16\u8bd1\u5668\u4f1a\u5728\u7f16\u8bd1\u65f6\u5bf9 (x > y ? x : y) \u8fdb\u884c\u6c42\u503c\uff0c\u4f7f\u7528 6 \u6765\u66ff\u6362\u8be5\u8868\u8fbe\u5f0f\u3002\u56e0\u4e3a\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u65e0\u9700\u5728\u8fd0\u884c\u65f6\u6c42\u503c\uff0c\u7a0b\u5e8f\u663e\u7136\u4f1a\u8fd0\u884c\u5730\u66f4\u5feb\u3002 \u4e0d\u8fc7\uff0c\u5728\u6253\u5370\u8bed\u53e5\u4e2d\u4f7f\u7528\u8fd9\u6837\u4e00\u4e2a\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u5e76\u4e0d\u662f\u7406\u60f3\u7684\u65b9\u5f0f\u2014\u2014\u6700\u597d\u662f\u5c06\u8be5\u8868\u8fbe\u5f0f\u5c01\u88c5\u6210\u4e00\u4e2a\u51fd\u6570\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u5c31\u662f\u8fd9\u6837\u505a\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int greater ( int x , int y ) { return ( x > y ? x : y ); // \u8868\u8fbe\u5f0f\u5728\u6b64 } int main () { constexpr int x { 5 }; constexpr int y { 6 }; std :: cout << greater ( x , y ) << \" is greater!\" ; // \u8fd0\u884c\u65f6\u6c42\u503c return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u7684\u8fd0\u884c\u7ed3\u679c\u548c\u524d\u4e00\u4e2a\u7a0b\u5e8f\u5e76\u6ca1\u6709\u533a\u522b\u3002\u4f46\u662f\u5b83\u7684\u7f3a\u70b9\u662f\uff0c\u5c06\u8868\u8fbe\u5f0f\u5c01\u88c5\u6210\u51fd\u6570\u540e\uff0c greater(x, y) \u51fd\u6570\u5fc5\u987b\u5728\u8fd0\u884c\u65f6\u88ab\u8c03\u7528\u3002\u4f7f\u7528\u51fd\u6570\u4ee3\u66ff\u8868\u8fbe\u5f0f\u4e4b\u540e\uff0c\u6211\u4eec\u5931\u53bb\u4e86\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\u80fd\u529b\uff08\u4f24\u5bb3\u6027\u80fd\uff09\u3002 \u90a3\u4e48\u5e94\u8be5\u5982\u4f55\u89e3\u51b3\u8be5\u95ee\u9898\u5462\uff1f Constexpr \u51fd\u6570\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u6c42\u503c constexpr \u51fd\u6570\u7684\u8fd4\u56de\u503c\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u4e3a\u4e86\u5c06\u51fd\u6570\u5b9a\u4e49\u4e3a constexpr \u7c7b\u578b\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5728\u8fd4\u56de\u503c\u7c7b\u578b\u524d\u6dfb\u52a0 constexpr \u5173\u952e\u5b57\u5373\u53ef\u3002\u8bf7\u89c1\u4e0b\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include constexpr int greater ( int x , int y ) // now a constexpr function { return ( x > y ? x : y ); } int main () { constexpr int x { 5 }; constexpr int y { 6 }; // We'll explain why we use variable g here later in the lesson constexpr int g { greater ( x , y ) }; // will be evaluated at compile-time std :: cout << g << \" is greater!\" ; return 0 ; } \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4ecd\u7136\u662f\u4e00\u6837\u7684\uff0c \u4f46\u662f greater() \u51fd\u6570\u4f1a\u5728\u7f16\u8bd1\u65f6\u8fdb\u884c\u6c42\u503c\uff01 \u4e3a\u4e86\u80fd\u591f\u6709\u8d44\u683c\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u51fd\u6570\u5fc5\u987b\u5177\u6709 constexpr \u7c7b\u578b\u7684\u8fd4\u56de\u503c\uff0c\u540c\u65f6\u4e0d\u80fd\u591f\u8c03\u7528\u4efb\u4f55\u975econstexpr\u51fd\u6570\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u4e5f\u5fc5\u987b\u4f7f\u7528 constexpr \u5b9e\u53c2 (\u4f8b\u5982 constexpr \u53d8\u91cf\u6216\u5b57\u9762\u91cf)\u3002 \u4f5c\u8005\u6ce8 \u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u6587\u7ae0\u4e2d\u4f7f\u7528\u201c\u6709\u8d44\u683c\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u201d\u8fd9\u4e2a\u672f\u8bed\u3002 \u6269\u5c55\u9605\u8bfb \u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u8981\u6c42\u89c1 \u8fd9\u91cc . greater() \u51fd\u6570\u80fd\u591f\u6ee1\u8db3\u4e0a\u8ff0\u8981\u6c42\uff0c\u6240\u4ee5\u5b83\u6709\u8d44\u683c\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528 constexpr \u8fd4\u56de\u7c7b\u578b\u5fc5\u987b\u8fd4\u56de\u4e00\u4e2a\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002 Constexpr \u51fd\u6570\u5177\u6709\u9690\u542b\u5185\u8054\u5c5e\u6027 \u56e0\u4e3a constexpr \u51fd\u6570\u53ef\u80fd\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u5fc5\u987b\u80fd\u591f\u5728\u8c03\u7528\u8be5\u51fd\u6570\u7684\u5730\u65b9\u770b\u5230\u8be5\u51fd\u6570\u7684\u5b8c\u6574\u5b9a\u4e49\u3002 \u8fd9\u4e5f\u610f\u5473\u7740 constexpr \u51fd\u6570\u5982\u679c\u591a\u4e2a\u6587\u4ef6\u4e2d\u88ab\u8c03\u7528\uff0c\u90a3\u4e48\u5b83\u7684\u5b9a\u4e49\u4e5f\u5fc5\u987b\u6bcf\u4e2a\u6587\u4ef6\u4e00\u4efd\u2014\u2014\u8fd9\u5c31\u53ef\u80fd\u4f1a\u8fdd\u53cd \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\uff0c constexpr \u51fd\u6570\u5177\u6709\u9690\u542b\u7684\u5185\u8054\u5c5e\u6027\uff0c\u8fd9\u4f7f\u5f97\u5b83\u4eec\u80fd\u591f\u514d\u4e8e\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u7684\u9650\u5236\u3002 \u56e0\u6b64\uff0c constexpr \u51fd\u6570\u4e5f\u901a\u5e38\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u56e0\u6b64\u5b83\u4eec\u53ef\u4ee5\u88ab #included \u5230\u4efb\u4f55 .cpp \u6587\u4ef6\u4ee5\u63d0\u4f9b\u51fd\u6570\u7684\u5b8c\u6574\u5b9a\u4e49\u3002 Constexpr \u51fd\u6570\u4e5f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6c42\u503c \u5177\u6709 constexpr \u8fd4\u56de\u503c\u7c7b\u578b\u7684\u51fd\u6570\u4e5f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6c42\u503c\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5b83\u4f1a\u8fd4\u56de\u975e constexpr \u7684\u7ed3\u679c\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include constexpr int greater ( int x , int y ) { return ( x > y ? x : y ); } int main () { int x { 5 }; // \u975e constexpr int y { 6 }; // \u975e constexpr std :: cout << greater ( x , y ) << \" is greater!\" ; // will be evaluated at runtime return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u53c2\u6570 x \u548c y \u5e76\u975e constexpr \u7c7b\u578b\uff0c\u6240\u4ee5\u51fd\u6570\u5c31\u4e0d\u80fd\u5728\u7f16\u8bd1\u65f6\u88ab\u6c42\u503c\u3002\u4e0d\u8fc7\uff0c\u51fd\u6570\u8fd8\u662f\u53ef\u4ee5\u5728 \u8fd0\u884c\u65f6 \u6c42\u503c\u7684\uff0c\u53ea\u4e0d\u8fc7\u8fd4\u56de\u7684\u503c\u662f\u975e constexpr \u7c7b\u578b\u7684 int \u3002 \u5173\u952e\u4fe1\u606f \u4f7f\u51fd\u6570\u5177\u6709 constexpr \u8fd4\u56de\u503c\u7c7b\u578b\u5373\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u4e5f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6c42\u503c\uff0c \u56e0\u6b64\u8fd9\u6837\u7684\u51fd\u6570\u53ef\u4ee5\u6ee1\u8db3\u4e24\u79cd\u573a\u666f\u3002\u5982\u679c\u4e0d\u662f\u8fd9\u6837\u7684\u8bdd\uff0c\u4f60\u5c31\u5fc5\u987b\u5b9a\u4e49\u4e24\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\uff08constexpr \u7248\u672c\u548c\u975e constexpr \u7248\u672c\uff09\uff0c\u4f46\u662f\u7531\u4e8e\u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u80fd\u7528\u6765\u533a\u522b\u51fd\u6570\u91cd\u8f7d\uff0c\u6240\u4ee5\u4f60\u5c31\u4e0d\u5f97\u4e0d\u4e3a\u5b83\u8d77\u4e00\u4e2a\u4e0d\u540c\u7684\u540d\u5b57\u3002 \u90a3\u4e48\uff0cconstexpr \u51fd\u6570\u4ec0\u4e48\u65f6\u5019\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff1f \u4f60\u53ef\u80fd\u4f1a\u89c9\u5f97\uff0c constexpr \u51fd\u6570\u5982\u679c\u53ef\u80fd\u7684\u8bdd\u603b\u662f\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u8fd9\u6837\u3002 \u6839\u636e C++ \u6807\u51c6\uff0c\u5982\u679c constexpr \u51fd\u6570\u6709\u8d44\u683c\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c \u800c\u4e14\u5b83\u7684\u8fd4\u56de\u503c\u88ab\u7528\u5728\u5e38\u6570\u8868\u8fbe\u5f0f\u4e2d\uff0c\u90a3\u4e48\u5c31 \u5fc5\u987b \u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u5219\u53ef\u4ee5\u81ea\u7531\u9009\u62e9\u662f\u5728\u7f16\u8bd1\u65f6\u8fd8\u662f\u8fd0\u884c\u65f6\u6c42\u503c\u3002 \u8ba9\u6211\u4eec\u901a\u8fc7\u4e0b\u9762\u7684\u4f8b\u5b50\u8fdb\u884c\u66f4\u8fdb\u4e00\u6b65\u7684\u89e3\u91ca\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include constexpr int greater ( int x , int y ) { return ( x > y ? x : y ); } int main () { constexpr int g { greater ( 5 , 6 ) }; // case 1: \u5728\u7f16\u8bd1\u65f6\u6c42\u503c std :: cout << g << \"is greater!\" ; int x { 5 }; // not constexpr std :: cout << greater ( x , 6 ) << \" is greater!\" ; // case 2: \u5728\u8fd0\u884c\u65f6\u6c42\u503c std :: cout << greater ( 5 , 6 ) << \" is greater!\" ; // case 3: \u5728\u7f16\u8bd1\u65f6\u6216\u8005\u8fd0\u884c\u65f6\u6c42\u503c\u90fd\u53ef\u80fd return 0 ; } \u5bf9\u4e8e case 1\uff0c\u6211\u4eec\u901a\u8fc7 constexpr \u53c2\u6570\u8c03\u7528\u4e86 greater() \u51fd\u6570\uff0c\u56e0\u6b64\u5b83\u6709\u8d44\u683c\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u540c\u65f6\uff0c constexpr \u53d8\u91cf g \u7684\u521d\u59cb\u5316\u503c\u5fc5\u987b\u662f constexpr \uff0c\u6240\u4ee5\u51fd\u6570\u7684\u8fd4\u56de\u503c\u88ab\u7528\u5728\u5e38\u91cf\u8868\u8fbe\u5f0f\u4e2d\u3002\u56e0\u6b64 greater() \u5fc5\u987b\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\u3002 \u5728 case 2 \u4e2d\uff0c\u6211\u4eec\u8c03\u7528 greater() \u65f6\u6240\u7528\u7684\u5176\u4e2d\u4e00\u4e2a\u53c2\u6570\u662f\u975e constexpr \u7c7b\u578b\u7684\u3002\u56e0\u6b64 greater() \u5e76\u4e0d\u80fd\u591f\u5728\u7f16\u8bd1\u65f6\u88ab\u6c42\u503c\uff0c\u53ea\u80fd\u5728\u8fd0\u884c\u65f6\u6c42\u503c\u3002 case 3 \u5c31\u6709\u610f\u601d\u4e86\u3002 greater() \u5728\u8c03\u7528\u65f6\u4f7f\u7528\u4e86 constexpr \u53c2\u6570\uff0c\u6240\u4ee5\u5b83\u662f\u6709\u8d44\u683c\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\u3002\u4f46\u662f\u5b83\u7684\u8fd4\u56de\u503c\u5e76\u6ca1\u6709\u7528\u5728\u9700\u8981\u5e38\u6570\u8868\u8fbe\u5f0f\u7684\u5730\u65b9\uff08 << \u603b\u662f\u5728\u8fd0\u884c\u65f6\u6c42\u503c\uff09\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u53ef\u4ee5\u81ea\u884c\u51b3\u5b9a greater() \u51fd\u6570\u662f\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u51fd\u6570\u8fd0\u884c\u65f6\uff01 \u6ce8\u610f\uff0c\u4f60\u7684\u7f16\u8bd1\u5668\u4f18\u5316\u7b49\u7ea7\u8bbe\u7f6e\u53ef\u80fd\u4f1a\u5f71\u54cd\u5230\u5b83\u5bf9\u51fd\u6570\u5e94\u8be5\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u8fd8\u662f\u8fd0\u884c\u65f6\u6c42\u503c\u7684\u51b3\u5b9a\u3002\u8fd9\u4e5f\u610f\u5473\u7740\u7f16\u8bd1\u5668\u5728 debug \u548c release \u6784\u5efa\u65f6\u53ef\u80fd\u4f1a\u505a\u51fa\u4e0d\u540c\u7684\u51b3\u5b9a\uff08\u56e0\u4e3adebug\u6a21\u5f0f\u4e0b\u901a\u5e38\u4f1a\u5173\u95ed\u4f18\u5316\uff09\u3002 \u5173\u952e\u4fe1\u606f constexpr \u51fd\u6570\u662f\u6709\u8d44\u683c\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\uff0c\u800c\u4e14\u5b83\u53ea\u6709\u5728\u8fd4\u56de\u503c\u88ab\u7528\u5728\u9700\u8981\u5e38\u91cf\u8868\u8fbe\u5f0f\u7684\u5730\u65b9\u65f6\uff0c\u5b83\u624d\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u5176\u4ed6\u60c5\u51b5\u4e0b\u662f\u5426\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u662f\u4e0d\u80fd\u4fdd\u8bc1\u7684\u3002 \u56e0\u6b64\uff0c constexpr \u51fd\u6570\u6700\u597d\u88ab\u770b\u505a\u662f\u201c\u53ef\u4ee5\u88ab\u7528\u5728\u5e38\u91cf\u8868\u8fbe\u5f0f\u201d\u800c\u4e0d\u662f\u201c\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u201d\u3002 \u786e\u5b9a constexpr \u51fd\u6570\u8c03\u7528\u662f\u5426\u662f\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684 \u5728 C++20 \u4ee5\u524d\uff0c\u5e76\u6ca1\u6709\u6807\u51c6\u7684\u8bed\u8a00\u5de5\u5177\u53ef\u4ee5\u5b8c\u6210\u8be5\u4efb\u52a1\u3002 \u5728 C++20 \u4e2d std::is_constant_evaluated() (\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d)\u4f1a\u8fd4\u56de\u4e00\u4e2a bool \u7c7b\u578b\u7684\u7ed3\u679c\u6307\u793a\u5f53\u524d\u51fd\u6570\u8c03\u7528\u662f\u5426\u7684\u5e38\u91cf\u4e0a\u4e0b\u6587\u4e2d\u6267\u884c\u7684\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u6761\u4ef6\u8bed\u53e5\u6765\u4f7f\u5f97\u51fd\u6570\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u548c\u8fd0\u884c\u65f6\u6c42\u503c\u65f6\u8868\u73b0\u51fa\u4e0d\u540c\u7684\u884c\u4e3a\u3002 1 2 3 4 5 6 7 8 #include // for std::is_constant_evaluated constexpr int someFunction () { if ( std :: is_constant_evaluated ()) // if compile-time evaluation // do something else // runtime evaluation // do something else } \u4f7f\u7528\u5f97\u5f53\u7684\u8bdd\uff0c\u4f60\u53ef\u4ee5\u4f60\u53ef\u4ee5\u8ba9\u51fd\u6570\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u65f6\u4ea7\u751f\u53ef\u4ee5\u88ab\u89c2\u5bdf\u5230\u7684\u4e0d\u540c\u884c\u4e3a\uff08\u4f8b\u5982\u8fd4\u56de\u4e00\u4e2a\u7279\u6b8a\u7684\u503c\uff09\uff0c\u7136\u540e\u4f60\u53ef\u4ee5\u901a\u8fc7\u8be5\u503c\u6765\u5224\u65ad\u51fd\u6570\u662f\u5728\u4f55\u65f6\u6c42\u503c\u7684\uff0c \u5f3a\u5236 constexpr \u51fd\u6570\u5728\u7f16\u8bd1\u65f6\u6c42\u503c \u6ca1\u6709\u529e\u6cd5\u53bb\u544a\u8bc9\u7f16\u8bd1\u5668 constexpr \u51fd\u6570\u5e94\u8be5\u5728\u6761\u4ef6\u5141\u8bb8\u7684\u60c5\u51b5\u4e0b\u5c3d\u53ef\u80fd\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff08\u5373\u4f7f\u8fd4\u56de\u503c\u88ab\u7528\u5728\u975e\u5e38\u6570\u8868\u8fbe\u5f0f\u65f6\uff09\u3002 \u4e0d\u8fc7\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8ba9\u51fd\u6570\u8fd4\u56de\u503c\u4f7f\u7528\u5728\u5e38\u6570\u8868\u8fbe\u5f0f\u4e2d\u4ee5\u786e\u4fdd\u5177\u5907\u6761\u4ef6\u7684\u51fd\u6570\u80fd\u591f\u786e\u4fdd\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u5f53\u7136\uff0c\u8fd9\u662f\u9488\u5bf9\u6bcf\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u800c\u8a00\u7684\u3002 \u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u8fd4\u56de\u503c\u6765\u521d\u59cb\u5316\u4e00\u4e2a constexpr \u53d8\u91cf\uff08\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728\u4e4b\u524d\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u4f7f\u7528\u4e86 \u2018g\u2019 \u8fd9\u4e2a\u53d8\u91cf\uff09\u3002\u53ef\u60dc\u7684\u662f\uff0c\u8fd9\u9700\u8981\u4e3a\u7a0b\u5e8f\u5f15\u5165\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\u800c\u4e14\u8fd9\u4e2a\u53d8\u91cf\u53ea\u662f\u4e3a\u4e86\u786e\u4fdd\u51fd\u6570\u662f\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\uff0c\u8fd9\u6837\u975e\u5e38\u4e0d\u4f18\u96c5\u800c\u4e14\u4e5f\u4f1a\u964d\u4f4e\u7a0b\u5e8f\u7684\u53ef\u8bfb\u6027\u3002 \u6269\u5c55\u9605\u8bfb \u4e3a\u4e86\u907f\u514d\u6bcf\u6b21\u90fd\u9700\u8981\u5f15\u5165\u4e00\u4e2a\u65b0\u7684 constexpr \u53d8\u91cf\u624d\u80fd\u786e\u4fdd\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u5f88\u591a\u4eba\u5c1d\u8bd5\u4e86\u4e0d\u5c11\u89e3\u51b3\u529e\u6cd5\u3002\u53c2\u89c1 \u8fd9\u91cc \u548c \u8fd9\u91cc \u3002 \u4e0d\u8fc7\uff0cC++20 \u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u529e\u6cd5\uff0c\u9a6c\u4e0a\u6211\u4eec\u5c31\u80fd\u770b\u5230\u3002 Consteval (C++20) C++20 \u5f15\u5165\u4e86 consteval \u5173\u952e\u5b57\uff0c\u5b83\u53ef\u4ee5\u6307\u5b9a\u51fd\u6570 \u5fc5\u987b \u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u5426\u5219\u5c06\u4f1a\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002\u8fd9\u79cd\u51fd\u6570\u88ab\u79f0\u4e3a \u5373\u65f6\u51fd\u6570(immediate functions) \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include consteval int greater ( int x , int y ) // \u51fd\u6570\u73b0\u5728\u662f consteval \u7c7b\u578b { return ( x > y ? x : y ); } int main () { constexpr int g { greater ( 5 , 6 ) }; // ok: will evaluate at compile-time std :: cout << greater ( 5 , 6 ) << \" is greater!\" ; // ok: will evaluate at compile-time int x { 5 }; // not constexpr std :: cout << greater ( x , 6 ) << \" is greater!\" ; // \u9519\u8bef: consteval \u51fd\u6570\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u6c42\u503c return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u524d\u9762\u4e24\u5904 greater() \u51fd\u6570\u8c03\u7528\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u800c greater(x, 6) \u5e76\u4e0d\u80fd\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u6240\u4ee5\u4f1a\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002 \u548c constexpr \u51fd\u6570\u4e00\u6837\uff0c consteval \u4e5f\u662f\u9690\u5f0f\u5185\u8054\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f60\u6709\u51fd\u6570\u51fa\u4e8e\u67d0\u4e9b\u539f\u56e0\uff08\u4f8b\u5982\u6027\u80fd\uff09\u800c\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 consteval \u3002 \u4f7f\u7528 consteval \u8ba9 constexpr \u5728\u7f16\u8bd1\u65f6\u6267\u884c(C++20) consteval \u51fd\u6570\u662f\u4e0d\u80fd\u591f\u5728\u8fd0\u884c\u65f6\u6c42\u503c\u7684\uff0c\u8fd9\u4e5f\u4f7f\u5176\u4e0d\u5982 constexpr \u51fd\u6570\u90a3\u6837\u7075\u6d3b\uff08\u65e2\u80fd\u591f\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u4e5f\u80fd\u591f\u5728\u8fd0\u884c\u65f6\u6c42\u503c\uff09\u3002\u6240\u4ee5\uff0c\u5982\u679c\u80fd\u591f\u6709\u529e\u6cd5\u5f3a\u5236 constexpr \u51fd\u6570\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff08\u5373\u4f7f\u5b83\u7684\u8fd4\u56de\u503c\u6ca1\u6709\u88ab\u7528\u5728\u5e38\u6570\u8868\u8fbe\u5f0f\u4e2d\uff09\uff0c\u4e5f\u662f\u975e\u5e38\u6709\u7528\u7684\u3002\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u8ba9\u51fd\u6570\u5c3d\u53ef\u80fd\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u5982\u679c\u51fd\u6570\u4e0d\u5177\u5907 \u8d44\u683c \uff0c\u5219\u5728\u8fd0\u884c\u65f6\u6c42\u503c\u3002 Consteval \u51fd\u6570\u4e3a\u4e0a\u9762\u7684\u60f3\u6cd5\u63d0\u4f9b\u4e86\u5b9e\u73b0\u7684\u53ef\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7b80\u6d01\u7684\u5e2e\u52a9\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include // Uses abbreviated function template (C++20) and `auto` return type to make this function work with any type of value // See 'related content' box below for more info (you don't need to know how these work to use this function) consteval auto compileTime ( auto value ) { return value ; } constexpr int greater ( int x , int y ) // \u51fd\u6570\u662f constexpr \u7c7b\u578b\u7684 { return ( x > y ? x : y ); } int main () { std :: cout << greater ( 5 , 6 ); // \u53ef\u80fd\u4f1a\uff0c\u4e5f\u53ef\u80fd\u4e0d\u4f1a\u5728\u7f16\u8bd1\u65f6\u6267\u884c std :: cout << compileTime ( greater ( 5 , 6 )); // \u4f1a\u5728\u7f16\u8bd1\u65f6\u6267\u884c int x { 5 }; std :: cout << greater ( x , 6 ); // \u4ecd\u7136\u53ef\u4ee5\u8c03\u7528 constexpr \u7248\u672c\u4f7f\u5176\u5728\u8fd0\u884c\u65f6\u6267\u884c return 0 ; } consteval \u51fd\u6570\u9700\u8981\u5e38\u91cf\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u53c2\u6570\u2014\u2014\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u5c06 constexpr \u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5b83\uff0c\u5219 constexpr \u51fd\u6570\u5c31\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002 consteval \u51fd\u6570\u53ea\u662f\u628a\u53c2\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c\u8fd4\u56de\u4e86\uff0c\u56e0\u6b64\u8c03\u7528\u8005\u8fd8\u662f\u53ef\u4ee5\u6b63\u5e38\u4f7f\u7528\u8be5\u503c\u7684\u3002 \u6ce8\u610f\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u51fd\u6570\u662f \u503c\u8fd4\u56de \uff0c\u867d\u7136\u5728\u8fd0\u884c\u65f6\u503c\u8fd4\u56de\u4f1a\u5f71\u54cd\u6027\u80fd\uff08\u5982\u679c\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u5c5e\u4e8e\u62f7\u8d1d\u5f00\u9500\u6bd4\u8f83\u5927\u7684\u90a3\u79cd\uff0c\u4f8b\u5982 std::string \uff09\uff0c\u5728\u7f16\u8bd1\u65f6\u5176\u5b9e\u4e0d\u5b58\u5728\u8be5\u95ee\u9898\uff0c\u56e0\u4e3a\u5168\u90e8\u8c03\u7528 consteval \u51fd\u6570\u7684\u5730\u65b9\u90fd\u4f1a\u88ab\u66ff\u6362\u4e3a\u8ba1\u7b97\u540e\u7684\u503c\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad \u4e2d\u4ecb\u7ecd auto \u7c7b\u578b\u7684\u8fd4\u56de\u503c\uff1b \u6211\u4eec\u4f1a\u5728 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f \u4e2d\u4ecb\u7ecd\u7b80\u5199\u51fd\u6570\u6a21\u677f\uff08 auto \u5f62\u53c2 \uff09","title":"6.14 - Constexpr \u548c consteval \u51fd\u6570"},{"location":"6-14-Constexpr-and-consteval-functions/#constexpr","text":"constexpr \u51fd\u6570\u7684\u8fd4\u56de\u503c\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u4e3a\u4e86\u5c06\u51fd\u6570\u5b9a\u4e49\u4e3a constexpr \u7c7b\u578b\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5728\u8fd4\u56de\u503c\u7c7b\u578b\u524d\u6dfb\u52a0 constexpr \u5173\u952e\u5b57\u5373\u53ef\u3002\u8bf7\u89c1\u4e0b\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include constexpr int greater ( int x , int y ) // now a constexpr function { return ( x > y ? x : y ); } int main () { constexpr int x { 5 }; constexpr int y { 6 }; // We'll explain why we use variable g here later in the lesson constexpr int g { greater ( x , y ) }; // will be evaluated at compile-time std :: cout << g << \" is greater!\" ; return 0 ; } \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4ecd\u7136\u662f\u4e00\u6837\u7684\uff0c \u4f46\u662f greater() \u51fd\u6570\u4f1a\u5728\u7f16\u8bd1\u65f6\u8fdb\u884c\u6c42\u503c\uff01 \u4e3a\u4e86\u80fd\u591f\u6709\u8d44\u683c\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u51fd\u6570\u5fc5\u987b\u5177\u6709 constexpr \u7c7b\u578b\u7684\u8fd4\u56de\u503c\uff0c\u540c\u65f6\u4e0d\u80fd\u591f\u8c03\u7528\u4efb\u4f55\u975econstexpr\u51fd\u6570\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u4e5f\u5fc5\u987b\u4f7f\u7528 constexpr \u5b9e\u53c2 (\u4f8b\u5982 constexpr \u53d8\u91cf\u6216\u5b57\u9762\u91cf)\u3002 \u4f5c\u8005\u6ce8 \u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u6587\u7ae0\u4e2d\u4f7f\u7528\u201c\u6709\u8d44\u683c\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u201d\u8fd9\u4e2a\u672f\u8bed\u3002 \u6269\u5c55\u9605\u8bfb \u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u8981\u6c42\u89c1 \u8fd9\u91cc . greater() \u51fd\u6570\u80fd\u591f\u6ee1\u8db3\u4e0a\u8ff0\u8981\u6c42\uff0c\u6240\u4ee5\u5b83\u6709\u8d44\u683c\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528 constexpr \u8fd4\u56de\u7c7b\u578b\u5fc5\u987b\u8fd4\u56de\u4e00\u4e2a\u7f16\u8bd1\u65f6\u5e38\u91cf\u3002","title":"Constexpr \u51fd\u6570\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u6c42\u503c"},{"location":"6-14-Constexpr-and-consteval-functions/#constexpr_1","text":"\u56e0\u4e3a constexpr \u51fd\u6570\u53ef\u80fd\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u5fc5\u987b\u80fd\u591f\u5728\u8c03\u7528\u8be5\u51fd\u6570\u7684\u5730\u65b9\u770b\u5230\u8be5\u51fd\u6570\u7684\u5b8c\u6574\u5b9a\u4e49\u3002 \u8fd9\u4e5f\u610f\u5473\u7740 constexpr \u51fd\u6570\u5982\u679c\u591a\u4e2a\u6587\u4ef6\u4e2d\u88ab\u8c03\u7528\uff0c\u90a3\u4e48\u5b83\u7684\u5b9a\u4e49\u4e5f\u5fc5\u987b\u6bcf\u4e2a\u6587\u4ef6\u4e00\u4efd\u2014\u2014\u8fd9\u5c31\u53ef\u80fd\u4f1a\u8fdd\u53cd \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\uff0c constexpr \u51fd\u6570\u5177\u6709\u9690\u542b\u7684\u5185\u8054\u5c5e\u6027\uff0c\u8fd9\u4f7f\u5f97\u5b83\u4eec\u80fd\u591f\u514d\u4e8e\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u7684\u9650\u5236\u3002 \u56e0\u6b64\uff0c constexpr \u51fd\u6570\u4e5f\u901a\u5e38\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u56e0\u6b64\u5b83\u4eec\u53ef\u4ee5\u88ab #included \u5230\u4efb\u4f55 .cpp \u6587\u4ef6\u4ee5\u63d0\u4f9b\u51fd\u6570\u7684\u5b8c\u6574\u5b9a\u4e49\u3002","title":"Constexpr \u51fd\u6570\u5177\u6709\u9690\u542b\u5185\u8054\u5c5e\u6027"},{"location":"6-14-Constexpr-and-consteval-functions/#constexpr_2","text":"\u5177\u6709 constexpr \u8fd4\u56de\u503c\u7c7b\u578b\u7684\u51fd\u6570\u4e5f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6c42\u503c\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5b83\u4f1a\u8fd4\u56de\u975e constexpr \u7684\u7ed3\u679c\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include constexpr int greater ( int x , int y ) { return ( x > y ? x : y ); } int main () { int x { 5 }; // \u975e constexpr int y { 6 }; // \u975e constexpr std :: cout << greater ( x , y ) << \" is greater!\" ; // will be evaluated at runtime return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u53c2\u6570 x \u548c y \u5e76\u975e constexpr \u7c7b\u578b\uff0c\u6240\u4ee5\u51fd\u6570\u5c31\u4e0d\u80fd\u5728\u7f16\u8bd1\u65f6\u88ab\u6c42\u503c\u3002\u4e0d\u8fc7\uff0c\u51fd\u6570\u8fd8\u662f\u53ef\u4ee5\u5728 \u8fd0\u884c\u65f6 \u6c42\u503c\u7684\uff0c\u53ea\u4e0d\u8fc7\u8fd4\u56de\u7684\u503c\u662f\u975e constexpr \u7c7b\u578b\u7684 int \u3002 \u5173\u952e\u4fe1\u606f \u4f7f\u51fd\u6570\u5177\u6709 constexpr \u8fd4\u56de\u503c\u7c7b\u578b\u5373\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u4e5f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6c42\u503c\uff0c \u56e0\u6b64\u8fd9\u6837\u7684\u51fd\u6570\u53ef\u4ee5\u6ee1\u8db3\u4e24\u79cd\u573a\u666f\u3002\u5982\u679c\u4e0d\u662f\u8fd9\u6837\u7684\u8bdd\uff0c\u4f60\u5c31\u5fc5\u987b\u5b9a\u4e49\u4e24\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\uff08constexpr \u7248\u672c\u548c\u975e constexpr \u7248\u672c\uff09\uff0c\u4f46\u662f\u7531\u4e8e\u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u80fd\u7528\u6765\u533a\u522b\u51fd\u6570\u91cd\u8f7d\uff0c\u6240\u4ee5\u4f60\u5c31\u4e0d\u5f97\u4e0d\u4e3a\u5b83\u8d77\u4e00\u4e2a\u4e0d\u540c\u7684\u540d\u5b57\u3002","title":"Constexpr \u51fd\u6570\u4e5f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6c42\u503c"},{"location":"6-14-Constexpr-and-consteval-functions/#constexpr_3","text":"\u4f60\u53ef\u80fd\u4f1a\u89c9\u5f97\uff0c constexpr \u51fd\u6570\u5982\u679c\u53ef\u80fd\u7684\u8bdd\u603b\u662f\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u8fd9\u6837\u3002 \u6839\u636e C++ \u6807\u51c6\uff0c\u5982\u679c constexpr \u51fd\u6570\u6709\u8d44\u683c\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c \u800c\u4e14\u5b83\u7684\u8fd4\u56de\u503c\u88ab\u7528\u5728\u5e38\u6570\u8868\u8fbe\u5f0f\u4e2d\uff0c\u90a3\u4e48\u5c31 \u5fc5\u987b \u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u5219\u53ef\u4ee5\u81ea\u7531\u9009\u62e9\u662f\u5728\u7f16\u8bd1\u65f6\u8fd8\u662f\u8fd0\u884c\u65f6\u6c42\u503c\u3002 \u8ba9\u6211\u4eec\u901a\u8fc7\u4e0b\u9762\u7684\u4f8b\u5b50\u8fdb\u884c\u66f4\u8fdb\u4e00\u6b65\u7684\u89e3\u91ca\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include constexpr int greater ( int x , int y ) { return ( x > y ? x : y ); } int main () { constexpr int g { greater ( 5 , 6 ) }; // case 1: \u5728\u7f16\u8bd1\u65f6\u6c42\u503c std :: cout << g << \"is greater!\" ; int x { 5 }; // not constexpr std :: cout << greater ( x , 6 ) << \" is greater!\" ; // case 2: \u5728\u8fd0\u884c\u65f6\u6c42\u503c std :: cout << greater ( 5 , 6 ) << \" is greater!\" ; // case 3: \u5728\u7f16\u8bd1\u65f6\u6216\u8005\u8fd0\u884c\u65f6\u6c42\u503c\u90fd\u53ef\u80fd return 0 ; } \u5bf9\u4e8e case 1\uff0c\u6211\u4eec\u901a\u8fc7 constexpr \u53c2\u6570\u8c03\u7528\u4e86 greater() \u51fd\u6570\uff0c\u56e0\u6b64\u5b83\u6709\u8d44\u683c\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u540c\u65f6\uff0c constexpr \u53d8\u91cf g \u7684\u521d\u59cb\u5316\u503c\u5fc5\u987b\u662f constexpr \uff0c\u6240\u4ee5\u51fd\u6570\u7684\u8fd4\u56de\u503c\u88ab\u7528\u5728\u5e38\u91cf\u8868\u8fbe\u5f0f\u4e2d\u3002\u56e0\u6b64 greater() \u5fc5\u987b\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\u3002 \u5728 case 2 \u4e2d\uff0c\u6211\u4eec\u8c03\u7528 greater() \u65f6\u6240\u7528\u7684\u5176\u4e2d\u4e00\u4e2a\u53c2\u6570\u662f\u975e constexpr \u7c7b\u578b\u7684\u3002\u56e0\u6b64 greater() \u5e76\u4e0d\u80fd\u591f\u5728\u7f16\u8bd1\u65f6\u88ab\u6c42\u503c\uff0c\u53ea\u80fd\u5728\u8fd0\u884c\u65f6\u6c42\u503c\u3002 case 3 \u5c31\u6709\u610f\u601d\u4e86\u3002 greater() \u5728\u8c03\u7528\u65f6\u4f7f\u7528\u4e86 constexpr \u53c2\u6570\uff0c\u6240\u4ee5\u5b83\u662f\u6709\u8d44\u683c\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\u3002\u4f46\u662f\u5b83\u7684\u8fd4\u56de\u503c\u5e76\u6ca1\u6709\u7528\u5728\u9700\u8981\u5e38\u6570\u8868\u8fbe\u5f0f\u7684\u5730\u65b9\uff08 << \u603b\u662f\u5728\u8fd0\u884c\u65f6\u6c42\u503c\uff09\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u53ef\u4ee5\u81ea\u884c\u51b3\u5b9a greater() \u51fd\u6570\u662f\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u51fd\u6570\u8fd0\u884c\u65f6\uff01 \u6ce8\u610f\uff0c\u4f60\u7684\u7f16\u8bd1\u5668\u4f18\u5316\u7b49\u7ea7\u8bbe\u7f6e\u53ef\u80fd\u4f1a\u5f71\u54cd\u5230\u5b83\u5bf9\u51fd\u6570\u5e94\u8be5\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u8fd8\u662f\u8fd0\u884c\u65f6\u6c42\u503c\u7684\u51b3\u5b9a\u3002\u8fd9\u4e5f\u610f\u5473\u7740\u7f16\u8bd1\u5668\u5728 debug \u548c release \u6784\u5efa\u65f6\u53ef\u80fd\u4f1a\u505a\u51fa\u4e0d\u540c\u7684\u51b3\u5b9a\uff08\u56e0\u4e3adebug\u6a21\u5f0f\u4e0b\u901a\u5e38\u4f1a\u5173\u95ed\u4f18\u5316\uff09\u3002 \u5173\u952e\u4fe1\u606f constexpr \u51fd\u6570\u662f\u6709\u8d44\u683c\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\uff0c\u800c\u4e14\u5b83\u53ea\u6709\u5728\u8fd4\u56de\u503c\u88ab\u7528\u5728\u9700\u8981\u5e38\u91cf\u8868\u8fbe\u5f0f\u7684\u5730\u65b9\u65f6\uff0c\u5b83\u624d\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u5176\u4ed6\u60c5\u51b5\u4e0b\u662f\u5426\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u662f\u4e0d\u80fd\u4fdd\u8bc1\u7684\u3002 \u56e0\u6b64\uff0c constexpr \u51fd\u6570\u6700\u597d\u88ab\u770b\u505a\u662f\u201c\u53ef\u4ee5\u88ab\u7528\u5728\u5e38\u91cf\u8868\u8fbe\u5f0f\u201d\u800c\u4e0d\u662f\u201c\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u201d\u3002","title":"\u90a3\u4e48\uff0cconstexpr \u51fd\u6570\u4ec0\u4e48\u65f6\u5019\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff1f"},{"location":"6-14-Constexpr-and-consteval-functions/#constexpr_4","text":"\u5728 C++20 \u4ee5\u524d\uff0c\u5e76\u6ca1\u6709\u6807\u51c6\u7684\u8bed\u8a00\u5de5\u5177\u53ef\u4ee5\u5b8c\u6210\u8be5\u4efb\u52a1\u3002 \u5728 C++20 \u4e2d std::is_constant_evaluated() (\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d)\u4f1a\u8fd4\u56de\u4e00\u4e2a bool \u7c7b\u578b\u7684\u7ed3\u679c\u6307\u793a\u5f53\u524d\u51fd\u6570\u8c03\u7528\u662f\u5426\u7684\u5e38\u91cf\u4e0a\u4e0b\u6587\u4e2d\u6267\u884c\u7684\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u6761\u4ef6\u8bed\u53e5\u6765\u4f7f\u5f97\u51fd\u6570\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u548c\u8fd0\u884c\u65f6\u6c42\u503c\u65f6\u8868\u73b0\u51fa\u4e0d\u540c\u7684\u884c\u4e3a\u3002 1 2 3 4 5 6 7 8 #include // for std::is_constant_evaluated constexpr int someFunction () { if ( std :: is_constant_evaluated ()) // if compile-time evaluation // do something else // runtime evaluation // do something else } \u4f7f\u7528\u5f97\u5f53\u7684\u8bdd\uff0c\u4f60\u53ef\u4ee5\u4f60\u53ef\u4ee5\u8ba9\u51fd\u6570\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u65f6\u4ea7\u751f\u53ef\u4ee5\u88ab\u89c2\u5bdf\u5230\u7684\u4e0d\u540c\u884c\u4e3a\uff08\u4f8b\u5982\u8fd4\u56de\u4e00\u4e2a\u7279\u6b8a\u7684\u503c\uff09\uff0c\u7136\u540e\u4f60\u53ef\u4ee5\u901a\u8fc7\u8be5\u503c\u6765\u5224\u65ad\u51fd\u6570\u662f\u5728\u4f55\u65f6\u6c42\u503c\u7684\uff0c","title":"\u786e\u5b9a constexpr \u51fd\u6570\u8c03\u7528\u662f\u5426\u662f\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684"},{"location":"6-14-Constexpr-and-consteval-functions/#constexpr_5","text":"\u6ca1\u6709\u529e\u6cd5\u53bb\u544a\u8bc9\u7f16\u8bd1\u5668 constexpr \u51fd\u6570\u5e94\u8be5\u5728\u6761\u4ef6\u5141\u8bb8\u7684\u60c5\u51b5\u4e0b\u5c3d\u53ef\u80fd\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff08\u5373\u4f7f\u8fd4\u56de\u503c\u88ab\u7528\u5728\u975e\u5e38\u6570\u8868\u8fbe\u5f0f\u65f6\uff09\u3002 \u4e0d\u8fc7\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8ba9\u51fd\u6570\u8fd4\u56de\u503c\u4f7f\u7528\u5728\u5e38\u6570\u8868\u8fbe\u5f0f\u4e2d\u4ee5\u786e\u4fdd\u5177\u5907\u6761\u4ef6\u7684\u51fd\u6570\u80fd\u591f\u786e\u4fdd\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u5f53\u7136\uff0c\u8fd9\u662f\u9488\u5bf9\u6bcf\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u800c\u8a00\u7684\u3002 \u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u8fd4\u56de\u503c\u6765\u521d\u59cb\u5316\u4e00\u4e2a constexpr \u53d8\u91cf\uff08\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728\u4e4b\u524d\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u4f7f\u7528\u4e86 \u2018g\u2019 \u8fd9\u4e2a\u53d8\u91cf\uff09\u3002\u53ef\u60dc\u7684\u662f\uff0c\u8fd9\u9700\u8981\u4e3a\u7a0b\u5e8f\u5f15\u5165\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\u800c\u4e14\u8fd9\u4e2a\u53d8\u91cf\u53ea\u662f\u4e3a\u4e86\u786e\u4fdd\u51fd\u6570\u662f\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u7684\uff0c\u8fd9\u6837\u975e\u5e38\u4e0d\u4f18\u96c5\u800c\u4e14\u4e5f\u4f1a\u964d\u4f4e\u7a0b\u5e8f\u7684\u53ef\u8bfb\u6027\u3002 \u6269\u5c55\u9605\u8bfb \u4e3a\u4e86\u907f\u514d\u6bcf\u6b21\u90fd\u9700\u8981\u5f15\u5165\u4e00\u4e2a\u65b0\u7684 constexpr \u53d8\u91cf\u624d\u80fd\u786e\u4fdd\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u5f88\u591a\u4eba\u5c1d\u8bd5\u4e86\u4e0d\u5c11\u89e3\u51b3\u529e\u6cd5\u3002\u53c2\u89c1 \u8fd9\u91cc \u548c \u8fd9\u91cc \u3002 \u4e0d\u8fc7\uff0cC++20 \u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u529e\u6cd5\uff0c\u9a6c\u4e0a\u6211\u4eec\u5c31\u80fd\u770b\u5230\u3002","title":"\u5f3a\u5236 constexpr \u51fd\u6570\u5728\u7f16\u8bd1\u65f6\u6c42\u503c"},{"location":"6-14-Constexpr-and-consteval-functions/#consteval-c20","text":"C++20 \u5f15\u5165\u4e86 consteval \u5173\u952e\u5b57\uff0c\u5b83\u53ef\u4ee5\u6307\u5b9a\u51fd\u6570 \u5fc5\u987b \u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u5426\u5219\u5c06\u4f1a\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002\u8fd9\u79cd\u51fd\u6570\u88ab\u79f0\u4e3a \u5373\u65f6\u51fd\u6570(immediate functions) \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include consteval int greater ( int x , int y ) // \u51fd\u6570\u73b0\u5728\u662f consteval \u7c7b\u578b { return ( x > y ? x : y ); } int main () { constexpr int g { greater ( 5 , 6 ) }; // ok: will evaluate at compile-time std :: cout << greater ( 5 , 6 ) << \" is greater!\" ; // ok: will evaluate at compile-time int x { 5 }; // not constexpr std :: cout << greater ( x , 6 ) << \" is greater!\" ; // \u9519\u8bef: consteval \u51fd\u6570\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u6c42\u503c return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u524d\u9762\u4e24\u5904 greater() \u51fd\u6570\u8c03\u7528\u4f1a\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u800c greater(x, 6) \u5e76\u4e0d\u80fd\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u6240\u4ee5\u4f1a\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002 \u548c constexpr \u51fd\u6570\u4e00\u6837\uff0c consteval \u4e5f\u662f\u9690\u5f0f\u5185\u8054\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f60\u6709\u51fd\u6570\u51fa\u4e8e\u67d0\u4e9b\u539f\u56e0\uff08\u4f8b\u5982\u6027\u80fd\uff09\u800c\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 consteval \u3002","title":"Consteval\u00a0(C++20)"},{"location":"6-14-Constexpr-and-consteval-functions/#consteval-constexpr-c20","text":"consteval \u51fd\u6570\u662f\u4e0d\u80fd\u591f\u5728\u8fd0\u884c\u65f6\u6c42\u503c\u7684\uff0c\u8fd9\u4e5f\u4f7f\u5176\u4e0d\u5982 constexpr \u51fd\u6570\u90a3\u6837\u7075\u6d3b\uff08\u65e2\u80fd\u591f\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u4e5f\u80fd\u591f\u5728\u8fd0\u884c\u65f6\u6c42\u503c\uff09\u3002\u6240\u4ee5\uff0c\u5982\u679c\u80fd\u591f\u6709\u529e\u6cd5\u5f3a\u5236 constexpr \u51fd\u6570\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff08\u5373\u4f7f\u5b83\u7684\u8fd4\u56de\u503c\u6ca1\u6709\u88ab\u7528\u5728\u5e38\u6570\u8868\u8fbe\u5f0f\u4e2d\uff09\uff0c\u4e5f\u662f\u975e\u5e38\u6709\u7528\u7684\u3002\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u8ba9\u51fd\u6570\u5c3d\u53ef\u80fd\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u5982\u679c\u51fd\u6570\u4e0d\u5177\u5907 \u8d44\u683c \uff0c\u5219\u5728\u8fd0\u884c\u65f6\u6c42\u503c\u3002 Consteval \u51fd\u6570\u4e3a\u4e0a\u9762\u7684\u60f3\u6cd5\u63d0\u4f9b\u4e86\u5b9e\u73b0\u7684\u53ef\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7b80\u6d01\u7684\u5e2e\u52a9\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include // Uses abbreviated function template (C++20) and `auto` return type to make this function work with any type of value // See 'related content' box below for more info (you don't need to know how these work to use this function) consteval auto compileTime ( auto value ) { return value ; } constexpr int greater ( int x , int y ) // \u51fd\u6570\u662f constexpr \u7c7b\u578b\u7684 { return ( x > y ? x : y ); } int main () { std :: cout << greater ( 5 , 6 ); // \u53ef\u80fd\u4f1a\uff0c\u4e5f\u53ef\u80fd\u4e0d\u4f1a\u5728\u7f16\u8bd1\u65f6\u6267\u884c std :: cout << compileTime ( greater ( 5 , 6 )); // \u4f1a\u5728\u7f16\u8bd1\u65f6\u6267\u884c int x { 5 }; std :: cout << greater ( x , 6 ); // \u4ecd\u7136\u53ef\u4ee5\u8c03\u7528 constexpr \u7248\u672c\u4f7f\u5176\u5728\u8fd0\u884c\u65f6\u6267\u884c return 0 ; } consteval \u51fd\u6570\u9700\u8981\u5e38\u91cf\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u53c2\u6570\u2014\u2014\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u5c06 constexpr \u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5b83\uff0c\u5219 constexpr \u51fd\u6570\u5c31\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u6c42\u503c\u3002 consteval \u51fd\u6570\u53ea\u662f\u628a\u53c2\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c\u8fd4\u56de\u4e86\uff0c\u56e0\u6b64\u8c03\u7528\u8005\u8fd8\u662f\u53ef\u4ee5\u6b63\u5e38\u4f7f\u7528\u8be5\u503c\u7684\u3002 \u6ce8\u610f\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u51fd\u6570\u662f \u503c\u8fd4\u56de \uff0c\u867d\u7136\u5728\u8fd0\u884c\u65f6\u503c\u8fd4\u56de\u4f1a\u5f71\u54cd\u6027\u80fd\uff08\u5982\u679c\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u5c5e\u4e8e\u62f7\u8d1d\u5f00\u9500\u6bd4\u8f83\u5927\u7684\u90a3\u79cd\uff0c\u4f8b\u5982 std::string \uff09\uff0c\u5728\u7f16\u8bd1\u65f6\u5176\u5b9e\u4e0d\u5b58\u5728\u8be5\u95ee\u9898\uff0c\u56e0\u4e3a\u5168\u90e8\u8c03\u7528 consteval \u51fd\u6570\u7684\u5730\u65b9\u90fd\u4f1a\u88ab\u66ff\u6362\u4e3a\u8ba1\u7b97\u540e\u7684\u503c\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad \u4e2d\u4ecb\u7ecd auto \u7c7b\u578b\u7684\u8fd4\u56de\u503c\uff1b \u6211\u4eec\u4f1a\u5728 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f \u4e2d\u4ecb\u7ecd\u7b80\u5199\u51fd\u6570\u6a21\u677f\uff08 auto \u5f62\u53c2 \uff09","title":"\u4f7f\u7528 consteval \u8ba9 constexpr \u5728\u7f16\u8bd1\u65f6\u6267\u884c(C++20)"},{"location":"6-15-Unnamed-and-inline-namespaces/","tags":["inline namespace","unmanned namespace"],"text":"Key Takeaway \u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u5185\u5bb9\u53ef\u4ee5\u5728\u7236\u547d\u540d\u7a7a\u95f4\u4e2d\u8bbf\u95ee\uff0c\u4f46\u662f\u5b83\u4eec\u5177\u6709\u5185\u90e8\u94fe\u63a5\uff0c\u53ea\u80fd\u5728\u8be5\u6587\u4ef6\u4e2d\u8bbf\u95ee \u5bf9\u4e8e\u51fd\u6570\u6765\u8bf4\uff0c\u628a\u5b83\u4eec\u5b9a\u4e49\u5728\u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6548\u679c\u548c\u628a\u5b83\u4eec\u5b9a\u4e49\u4e3a static \u7c7b\u578b\u7684\u6548\u679c\u662f\u5b8c\u5168\u4e00\u6837\u7684 \u5185\u8054\u547d\u540d\u7a7a\u95f4\u5e38\u7528\u6765\u5bf9\u7279\u5b9a\u7684\u5185\u5bb9\u8fdb\u884c\u7248\u672c\u7ba1\u7406\u3002\u548c\u533f\u540d\u547d\u540d\u7a7a\u95f4\u7c7b\u4f3c\uff0c\u5b9a\u4e49\u5728\u5185\u8054\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u5185\u5bb9\u90fd\u5c5e\u4e8e\u5176\u7236\u547d\u540d\u7a7a\u95f4\u3002\u4f46\u5185\u8054\u547d\u540d\u7a7a\u95f4\u5e76\u4e0d\u4f1a\u7ed9\u5176\u5185\u90e8\u5b9a\u4e49\u7684\u5185\u5bb9\u8d4b\u4e88 \u5185\u90e8\u94fe\u63a5 \u3002 C++ \u652f\u6301\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u53ea\u8981\u6709\u4e24\u79cd\u503c\u5f97\u6211\u4eec\u53bb\u4e86\u89e3\u3002\u672c\u8bfe\u7a0b\u4e0d\u4f1a\u5efa\u7acb\u5728\u8fd9\u4e9b\u57fa\u7840\u4e0a\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u628a\u8fd9\u8282\u8bfe\u5f53\u505a\u9009\u4fee\u3002 \u533f\u540d\u547d\u540d\u7a7a\u95f4 \u672a\u547d\u540d\u547d\u540d\u7a7a\u95f4\uff08\u4e5f\u53eb\u533f\u540d\u547d\u540d\u7a7a\u95f4\uff09\u662f\u4e00\u79cd\u6ca1\u6709\u540d\u5b57\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include namespace // \u533f\u540d\u547d\u540d\u7a7a\u95f4 { void doSomething () // \u53ea\u80fd\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u88ab\u8bbf\u95ee { std :: cout << \"v1 \\n \" ; } } int main () { doSomething (); // \u4e0d\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u524d\u7f00\u8c03\u7528 doSomething() return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 v1 \u6240\u6709\u5728 \u533f\u540d\u547d\u540d\u7a7a\u95f4 \u4e2d\u5b9a\u4e49\u7684\u5185\u5bb9\uff0c\u90fd\u88ab\u770b\u505a\u662f\u8be5\u547d\u540d\u7a7a\u95f4\u7684\u7236\u547d\u540d\u7a7a\u95f4\u7684\u4e00\u90e8\u5206\u3002\u6240\u4ee5\u5373\u4f7f doSomething \u88ab\u5b9a\u4e49\u5728\u4e00\u4e2a\u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u8fd9\u4e2a\u51fd\u6570\u4e5f\u662f\u53ef\u4ee5\u5728\u5b83\u7684\u7236\u547d\u540d\u7a7a\u95f4\uff08\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u662f\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff09\u4e2d\u88ab\u8bbf\u95ee\u7684\uff0c\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728 main \u51fd\u6570\u4e2d\u53ef\u4ee5\u4e0d\u4f7f\u7528\u4efb\u4f55\u9650\u5b9a\u7b26\u5c31\u8bbf\u95ee doSomething \u3002 \u8fd9\u6837\u4e00\u6765\uff0c\u597d\u50cf\u533f\u540d\u547d\u540d\u7a7a\u95f4\u5e76\u6ca1\u6709\u4ec0\u4e48\u7528\u3002\u4f46\u662f\u533f\u540d\u547d\u540d\u7a7a\u95f4\u8fd8\u6709\u4e00\u4e2a\u6548\u679c\uff0c\u5c31\u662f\u5c06\u5176\u5185\u90e8\u7684\u6807\u8bc6\u7b26\u770b\u505a\u662f\u5b83\u7684\u5185\u90e8\u94fe\u63a5\uff0c\u8fd9\u610f\u5473\u7740\u8be5\u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u5185\u5bb9\u4e0d\u80fd\u5728\u8be5\u6587\u4ef6\u4ee5\u5916\u7684\u5730\u65b9\u88ab\u8bbf\u95ee\u3002 \u5bf9\u4e8e\u51fd\u6570\u6765\u8bf4\uff0c\u628a\u5b83\u4eec\u5b9a\u4e49\u5728\u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6548\u679c\u548c\u628a\u5b83\u4eec\u5b9a\u4e49\u4e3a static \u7c7b\u578b\u7684\u6548\u679c\u662f\u5b8c\u5168\u4e00\u6837\u7684\u3002\u4e0b\u9762\u7684\u7a0b\u5e8f\u548c\u4e0a\u9762\u7684\u4f8b\u7a0b\u6548\u679c\u662f\u4e00\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include static void doSomething () // \u53ea\u80fd\u5728\u8be5\u6587\u4ef6\u4e2d\u8bbf\u95ee { std :: cout << \"v1 \\n \" ; } int main () { doSomething (); // \u4e0d\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u524d\u7f00\u8c03\u7528 doSomething() return 0 ; } \u5728\u6709\u5f88\u591a\u5185\u5bb9\u9700\u8981\u88ab\u9650\u5b9a\u5728\u7ed9\u5b9a\u6587\u4ef6\u4e2d\u65f6\uff0c\u533f\u540d\u547d\u540d\u7a7a\u95f4\u5c31\u5f88\u6709\u7528\u3002\u6bd5\u7adf\u548c\u5c06\u5b83\u4eec\u9010\u4e2a\u5b9a\u4e49\u4e3a static \u76f8\u6bd4\uff0c\u4f7f\u7528\u533f\u540d\u547d\u540d\u7a7a\u95f4\u8981\u65b9\u4fbf\u7684\u591a\u3002\u533f\u540d\u547d\u540d\u7a7a\u95f4\u8fd8\u53ef\u4ee5\u628a \u7528\u6237\u5b9a\u4e49\u7c7b\u578b \uff08\u7a0d\u540e\u4f1a\u4ecb\u7ecd\uff09\u9650\u5b9a\u5728\u6587\u4ef6\u4e2d\uff0c\u8fd9\u4e2a\u95ee\u9898\u6ca1\u6709\u5176\u4ed6\u53ef\u7528\u7684\u65b9\u6cd5\u3002 \u5185\u8054\u547d\u540d\u7a7a\u95f4 \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void doSomething () { std :: cout << \"v1 \\n \" ; } int main () { doSomething (); return 0 ; } \u6253\u5370\uff1a 1 v1 \u5f88\u7b80\u5355\uff0c\u5bf9\u5427\uff1f \u4f46\u662f\uff0c\u5047\u8bbe\u4f60\u60f3\u8981\u5347\u7ea7\u4e00\u4e0b doSomething \u51fd\u6570\u5e76\u6539\u53d8\u5b83\u7684\u884c\u4e3a\u3002\u4f46\u662f\u8fd9\u6837\u7684\u8bdd\uff0c\u4ecd\u7136\u4f7f\u7528\u8001\u7248\u672c doSomething \u51fd\u6570\u7684\u4ee3\u7801\u5c31\u4f1a\u906d\u5230\u7834\u574f\u3002\u6709\u4ec0\u4e48\u529e\u6cd5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5462\uff1f \u4e00\u79cd\u529e\u6cd5\u662f\u91cd\u65b0\u5b9a\u4e49\u4e00\u4e2a\u540d\u5b57\u4e0d\u540c\u7684\u65b0\u7248\u672c\u7684\u51fd\u6570\u3002\u4f46\u662f\uff0c\u968f\u7740\u4f60\u4e00\u6b21\u6b21\u5730\u4fee\u6539\u51fd\u6570\uff0c\u6700\u7ec8\u7684\u4e0b\u573a\u5c31\u662f\u4f60\u4f1a\u62e5\u6709\u4e00\u5806\u540d\u5b57\u51e0\u4e4e\u76f8\u540c\u7684\u51fd\u6570\uff08 doSomething , doSomething_v2 , doSomething_v3 \u7b49\u7b49\uff09\u3002 \u53e6\u5916\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 \u5185\u8054\u547d\u540d\u7a7a\u95f4 \u3002\u5185\u8054\u547d\u540d\u7a7a\u95f4\u5e38\u7528\u6765\u5bf9\u7279\u5b9a\u7684\u5185\u5bb9\u8fdb\u884c\u7248\u672c\u7ba1\u7406\u3002\u548c\u533f\u540d\u547d\u540d\u7a7a\u95f4\u7c7b\u4f3c\uff0c\u5b9a\u4e49\u5728\u5185\u8054\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u5185\u5bb9\u90fd\u5c5e\u4e8e\u5176\u7236\u547d\u540d\u7a7a\u95f4\u3002\u4e0d\u8fc7\uff0c\u5185\u8054\u547d\u540d\u7a7a\u95f4\u5e76\u4e0d\u4f1a\u7ed9\u5176\u5185\u90e8\u5b9a\u4e49\u7684\u5185\u5bb9\u8d4b\u4e88 \u5185\u90e8\u94fe\u63a5 \u3002 \u5b9a\u4e49\u5185\u8054\u547d\u540d\u7a7a\u95f4\uff0c\u9700\u8981\u4f7f\u7528 inline \u5173\u952e\u5b57\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include inline namespace v1 // \u58f0\u660e\u5185\u8054\u547d\u540d\u7a7a\u95f4 v1 { void doSomething () { std :: cout << \"v1 \\n \" ; } } namespace v2 // \u58f0\u660e\u5185\u8054\u547d\u540d\u7a7a\u95f4 v2 { void doSomething () { std :: cout << \"v2 \\n \" ; } } int main () { v1 :: doSomething (); // calls the v1 version of doSomething() v2 :: doSomething (); // calls the v2 version of doSomething() doSomething (); // calls the inline version of doSomething() (which is v1) return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 v1 v2 v1 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u8c03\u7528 doSomething \u4f1a\u4f7f\u7528 v1 \u7248\u672c\u7684(\u5185\u8054\u7248\u672c)\u7684 doSomething \u3002\u800c\u4efb\u4f55\u60f3\u8981\u4f7f\u7528\u65b0\u7248\u672c\u7684\u5730\u65b9\uff0c\u53ef\u4ee5\u663e\u5f0f\u5730\u8c03\u7528 v2::dosomething() \u3002\u8fd9\u6837\u6211\u4eec\u4e0d\u4ec5\u53ef\u4ee5\u4f7f\u7528\u65b0\u7248\u672c\u7684\u51fd\u6570\uff0c\u540c\u65f6\u4e5f\u80fd\u591f\u4fdd\u5b58\u8001\u7248\u672c\u3002 \u6216\u8005\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u63a8\u5e7f\u65b0\u7248\u672c\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include namespace v1 // \u58f0\u660e\u5185\u8054\u547d\u540d\u7a7a\u95f4 v1 { void doSomething () { std :: cout << \"v1 \\n \" ; } } inline namespace v2 // \u58f0\u660e\u5185\u8054\u547d\u540d\u7a7a\u95f4 v2 { void doSomething () { std :: cout << \"v2 \\n \" ; } } int main () { v1 :: doSomething (); // calls the v1 version of doSomething() v2 :: doSomething (); // calls the v2 version of doSomething() doSomething (); // calls the inline version of doSomething() (which is v2) return 0 ; } \u6253\u5370\uff1a 1 2 3 v1 v2 v2 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u8c03\u7528 doSomething \u7684\u5730\u65b9\u90fd\u4f1a\u4f7f\u7528 v2 \u7248\u672c\u7684\u51fd\u6570\uff08\u66f4\u65b0\u66f4\u597d\u7684\u7248\u672c\uff09\u3002\u800c\u90a3\u4e9b\u613f\u610f\u4f7f\u7528\u8001\u7248\u672c\u51fd\u6570\u7684\u4eba\uff0c\u4e5f\u53ef\u4ee5\u663e\u5f0f\u5730\u8c03\u7528 v1::doSomething() \u6765\u4f7f\u7528\u65e7\u7248\u672c\u7684\u51fd\u6570\u3002\u8fd9\u4e5f\u610f\u5473\u7740\u9700\u8981\u4f7f\u7528 v1::doSomething \u7248\u672c\u7684\u5730\u65b9\u5fc5\u987b\u8981\u5c06 doSomething \u8fdb\u884c\u66ff\u6362\u4e3a v1::doSomething \u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u547d\u540d\u5f97\u5f53\u7684\u8bdd\uff0c\u4e5f\u5e76\u4e0d\u4f1a\u5e26\u6765\u4ec0\u4e48\u5927\u9ebb\u70e6\u3002","title":"6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4"},{"location":"6-15-Unnamed-and-inline-namespaces/#_1","text":"\u672a\u547d\u540d\u547d\u540d\u7a7a\u95f4\uff08\u4e5f\u53eb\u533f\u540d\u547d\u540d\u7a7a\u95f4\uff09\u662f\u4e00\u79cd\u6ca1\u6709\u540d\u5b57\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include namespace // \u533f\u540d\u547d\u540d\u7a7a\u95f4 { void doSomething () // \u53ea\u80fd\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u88ab\u8bbf\u95ee { std :: cout << \"v1 \\n \" ; } } int main () { doSomething (); // \u4e0d\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u524d\u7f00\u8c03\u7528 doSomething() return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 v1 \u6240\u6709\u5728 \u533f\u540d\u547d\u540d\u7a7a\u95f4 \u4e2d\u5b9a\u4e49\u7684\u5185\u5bb9\uff0c\u90fd\u88ab\u770b\u505a\u662f\u8be5\u547d\u540d\u7a7a\u95f4\u7684\u7236\u547d\u540d\u7a7a\u95f4\u7684\u4e00\u90e8\u5206\u3002\u6240\u4ee5\u5373\u4f7f doSomething \u88ab\u5b9a\u4e49\u5728\u4e00\u4e2a\u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u8fd9\u4e2a\u51fd\u6570\u4e5f\u662f\u53ef\u4ee5\u5728\u5b83\u7684\u7236\u547d\u540d\u7a7a\u95f4\uff08\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u662f\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff09\u4e2d\u88ab\u8bbf\u95ee\u7684\uff0c\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728 main \u51fd\u6570\u4e2d\u53ef\u4ee5\u4e0d\u4f7f\u7528\u4efb\u4f55\u9650\u5b9a\u7b26\u5c31\u8bbf\u95ee doSomething \u3002 \u8fd9\u6837\u4e00\u6765\uff0c\u597d\u50cf\u533f\u540d\u547d\u540d\u7a7a\u95f4\u5e76\u6ca1\u6709\u4ec0\u4e48\u7528\u3002\u4f46\u662f\u533f\u540d\u547d\u540d\u7a7a\u95f4\u8fd8\u6709\u4e00\u4e2a\u6548\u679c\uff0c\u5c31\u662f\u5c06\u5176\u5185\u90e8\u7684\u6807\u8bc6\u7b26\u770b\u505a\u662f\u5b83\u7684\u5185\u90e8\u94fe\u63a5\uff0c\u8fd9\u610f\u5473\u7740\u8be5\u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u5185\u5bb9\u4e0d\u80fd\u5728\u8be5\u6587\u4ef6\u4ee5\u5916\u7684\u5730\u65b9\u88ab\u8bbf\u95ee\u3002 \u5bf9\u4e8e\u51fd\u6570\u6765\u8bf4\uff0c\u628a\u5b83\u4eec\u5b9a\u4e49\u5728\u533f\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6548\u679c\u548c\u628a\u5b83\u4eec\u5b9a\u4e49\u4e3a static \u7c7b\u578b\u7684\u6548\u679c\u662f\u5b8c\u5168\u4e00\u6837\u7684\u3002\u4e0b\u9762\u7684\u7a0b\u5e8f\u548c\u4e0a\u9762\u7684\u4f8b\u7a0b\u6548\u679c\u662f\u4e00\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include static void doSomething () // \u53ea\u80fd\u5728\u8be5\u6587\u4ef6\u4e2d\u8bbf\u95ee { std :: cout << \"v1 \\n \" ; } int main () { doSomething (); // \u4e0d\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u524d\u7f00\u8c03\u7528 doSomething() return 0 ; } \u5728\u6709\u5f88\u591a\u5185\u5bb9\u9700\u8981\u88ab\u9650\u5b9a\u5728\u7ed9\u5b9a\u6587\u4ef6\u4e2d\u65f6\uff0c\u533f\u540d\u547d\u540d\u7a7a\u95f4\u5c31\u5f88\u6709\u7528\u3002\u6bd5\u7adf\u548c\u5c06\u5b83\u4eec\u9010\u4e2a\u5b9a\u4e49\u4e3a static \u76f8\u6bd4\uff0c\u4f7f\u7528\u533f\u540d\u547d\u540d\u7a7a\u95f4\u8981\u65b9\u4fbf\u7684\u591a\u3002\u533f\u540d\u547d\u540d\u7a7a\u95f4\u8fd8\u53ef\u4ee5\u628a \u7528\u6237\u5b9a\u4e49\u7c7b\u578b \uff08\u7a0d\u540e\u4f1a\u4ecb\u7ecd\uff09\u9650\u5b9a\u5728\u6587\u4ef6\u4e2d\uff0c\u8fd9\u4e2a\u95ee\u9898\u6ca1\u6709\u5176\u4ed6\u53ef\u7528\u7684\u65b9\u6cd5\u3002","title":"\u533f\u540d\u547d\u540d\u7a7a\u95f4"},{"location":"6-15-Unnamed-and-inline-namespaces/#_2","text":"\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void doSomething () { std :: cout << \"v1 \\n \" ; } int main () { doSomething (); return 0 ; } \u6253\u5370\uff1a 1 v1 \u5f88\u7b80\u5355\uff0c\u5bf9\u5427\uff1f \u4f46\u662f\uff0c\u5047\u8bbe\u4f60\u60f3\u8981\u5347\u7ea7\u4e00\u4e0b doSomething \u51fd\u6570\u5e76\u6539\u53d8\u5b83\u7684\u884c\u4e3a\u3002\u4f46\u662f\u8fd9\u6837\u7684\u8bdd\uff0c\u4ecd\u7136\u4f7f\u7528\u8001\u7248\u672c doSomething \u51fd\u6570\u7684\u4ee3\u7801\u5c31\u4f1a\u906d\u5230\u7834\u574f\u3002\u6709\u4ec0\u4e48\u529e\u6cd5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5462\uff1f \u4e00\u79cd\u529e\u6cd5\u662f\u91cd\u65b0\u5b9a\u4e49\u4e00\u4e2a\u540d\u5b57\u4e0d\u540c\u7684\u65b0\u7248\u672c\u7684\u51fd\u6570\u3002\u4f46\u662f\uff0c\u968f\u7740\u4f60\u4e00\u6b21\u6b21\u5730\u4fee\u6539\u51fd\u6570\uff0c\u6700\u7ec8\u7684\u4e0b\u573a\u5c31\u662f\u4f60\u4f1a\u62e5\u6709\u4e00\u5806\u540d\u5b57\u51e0\u4e4e\u76f8\u540c\u7684\u51fd\u6570\uff08 doSomething , doSomething_v2 , doSomething_v3 \u7b49\u7b49\uff09\u3002 \u53e6\u5916\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 \u5185\u8054\u547d\u540d\u7a7a\u95f4 \u3002\u5185\u8054\u547d\u540d\u7a7a\u95f4\u5e38\u7528\u6765\u5bf9\u7279\u5b9a\u7684\u5185\u5bb9\u8fdb\u884c\u7248\u672c\u7ba1\u7406\u3002\u548c\u533f\u540d\u547d\u540d\u7a7a\u95f4\u7c7b\u4f3c\uff0c\u5b9a\u4e49\u5728\u5185\u8054\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u5185\u5bb9\u90fd\u5c5e\u4e8e\u5176\u7236\u547d\u540d\u7a7a\u95f4\u3002\u4e0d\u8fc7\uff0c\u5185\u8054\u547d\u540d\u7a7a\u95f4\u5e76\u4e0d\u4f1a\u7ed9\u5176\u5185\u90e8\u5b9a\u4e49\u7684\u5185\u5bb9\u8d4b\u4e88 \u5185\u90e8\u94fe\u63a5 \u3002 \u5b9a\u4e49\u5185\u8054\u547d\u540d\u7a7a\u95f4\uff0c\u9700\u8981\u4f7f\u7528 inline \u5173\u952e\u5b57\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include inline namespace v1 // \u58f0\u660e\u5185\u8054\u547d\u540d\u7a7a\u95f4 v1 { void doSomething () { std :: cout << \"v1 \\n \" ; } } namespace v2 // \u58f0\u660e\u5185\u8054\u547d\u540d\u7a7a\u95f4 v2 { void doSomething () { std :: cout << \"v2 \\n \" ; } } int main () { v1 :: doSomething (); // calls the v1 version of doSomething() v2 :: doSomething (); // calls the v2 version of doSomething() doSomething (); // calls the inline version of doSomething() (which is v1) return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 v1 v2 v1 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u8c03\u7528 doSomething \u4f1a\u4f7f\u7528 v1 \u7248\u672c\u7684(\u5185\u8054\u7248\u672c)\u7684 doSomething \u3002\u800c\u4efb\u4f55\u60f3\u8981\u4f7f\u7528\u65b0\u7248\u672c\u7684\u5730\u65b9\uff0c\u53ef\u4ee5\u663e\u5f0f\u5730\u8c03\u7528 v2::dosomething() \u3002\u8fd9\u6837\u6211\u4eec\u4e0d\u4ec5\u53ef\u4ee5\u4f7f\u7528\u65b0\u7248\u672c\u7684\u51fd\u6570\uff0c\u540c\u65f6\u4e5f\u80fd\u591f\u4fdd\u5b58\u8001\u7248\u672c\u3002 \u6216\u8005\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u63a8\u5e7f\u65b0\u7248\u672c\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include namespace v1 // \u58f0\u660e\u5185\u8054\u547d\u540d\u7a7a\u95f4 v1 { void doSomething () { std :: cout << \"v1 \\n \" ; } } inline namespace v2 // \u58f0\u660e\u5185\u8054\u547d\u540d\u7a7a\u95f4 v2 { void doSomething () { std :: cout << \"v2 \\n \" ; } } int main () { v1 :: doSomething (); // calls the v1 version of doSomething() v2 :: doSomething (); // calls the v2 version of doSomething() doSomething (); // calls the inline version of doSomething() (which is v2) return 0 ; } \u6253\u5370\uff1a 1 2 3 v1 v2 v2 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u8c03\u7528 doSomething \u7684\u5730\u65b9\u90fd\u4f1a\u4f7f\u7528 v2 \u7248\u672c\u7684\u51fd\u6570\uff08\u66f4\u65b0\u66f4\u597d\u7684\u7248\u672c\uff09\u3002\u800c\u90a3\u4e9b\u613f\u610f\u4f7f\u7528\u8001\u7248\u672c\u51fd\u6570\u7684\u4eba\uff0c\u4e5f\u53ef\u4ee5\u663e\u5f0f\u5730\u8c03\u7528 v1::doSomething() \u6765\u4f7f\u7528\u65e7\u7248\u672c\u7684\u51fd\u6570\u3002\u8fd9\u4e5f\u610f\u5473\u7740\u9700\u8981\u4f7f\u7528 v1::doSomething \u7248\u672c\u7684\u5730\u65b9\u5fc5\u987b\u8981\u5c06 doSomething \u8fdb\u884c\u66ff\u6362\u4e3a v1::doSomething \u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u547d\u540d\u5f97\u5f53\u7684\u8bdd\uff0c\u4e5f\u5e76\u4e0d\u4f1a\u5e26\u6765\u4ec0\u4e48\u5927\u9ebb\u70e6\u3002","title":"\u5185\u8054\u547d\u540d\u7a7a\u95f4"},{"location":"6-2-User-defined-namespaces-and-the-scope-resolution-operator/","tags":["namespace","namespace aliases","scope resolution operator","C++17"],"text":"Key Takeaway \u4e0d\u6307\u5b9a\u547d\u540d\u7a7a\u95f4\u7684\u524d\u63d0\u4e0b\uff0c\u7f16\u8bd1\u5668\u9996\u5148\u5728\u5f53\u524d\u547d\u540d\u7a7a\u95f4\u4e2d\u67e5\u627e\uff0c\u7136\u540e\u6309\u7167\u5305\u542b\u5173\u7cfb\u9010\u5c42\u67e5\u627e\uff0c\u76f4\u5230\u627e\u5230\u5168\u5c40\u547d\u540d\u7a7a\u95f4 \u4e0d\u5e26\u524d\u7f00\u7684\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u8868\u793a\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u67e5\u627e\uff08 ::print() \uff09 \u5c06\u547d\u540d\u7a7a\u95f4\u7684\u58f0\u660e\u5206\u6210\u591a\u4e2a\u90e8\u5206\u653e\u5728\u4e0d\u540c\u7684\u4f4d\u7f6e\u662f\u5408\u6cd5\u7684\uff08\u591a\u4e2a\u6587\u4ef6\u3001\u76f8\u540c\u6587\u4ef6\u7684\u4e0d\u540c\u4f4d\u7f6e\uff09\u3002\u6240\u6709\u8fd9\u4e9b\u58f0\u660e\u90fd\u88ab\u8ba4\u4e3a\u662f\u5bf9\u5e94\u547d\u540d\u7a7a\u95f4\u58f0\u660e\u7684\u4e00\u90e8\u5206\u3002 \u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u5d4c\u5957\uff0c\u4f46\u662f\u5d4c\u5957\u540e\u5f15\u7528\u5185\u90e8\u7684\u51fd\u6570\u5199\u8d77\u6765\u5f88\u9ebb\u70e6\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u522b\u540d\u4e3a\u5176\u521b\u5efa\u4e00\u4e2a\u7b80\u77ed\u7684\u3001\u4e34\u65f6\u7684\u540d\u5b57 C++17 \u8fd8\u652f\u6301 namespace foo::goo \u8fd9\u79cd\u65b9\u5f0f\u6765\u521b\u5efa\u5d4c\u5957\u7684\u547d\u540d\u7a7a\u95f4 \u547d\u540d\u7a7a\u95f4\u522b\u540d\u6700\u597d\u7684\u4e00\u70b9\u662f\uff1a\u5982\u679c\u4f60\u9700\u8981\u5c06 foo::goo \u4e2d\u7684\u51fd\u6570\u79fb\u52a8\u5230\u53e6\u5916\u7684\u5730\u65b9\uff0c\u90a3\u4e48\u4f60\u53ea\u9700\u8981\u66f4\u65b0\u4e00\u4e0b\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684 active \u522b\u540d\u4f7f\u5176\u6307\u5411\u65b0\u7684\u547d\u540d\u7a7a\u95f4\u5373\u53ef\uff0c\u800c\u4e0d\u5fc5\u67e5\u627e\u66ff\u6362\u6240\u6709\u7684 foo::goo . C++ \u7684\u547d\u540d\u7a7a\u95f4\u4e0d\u662f\u4e3a\u4e86\u50cf C# \u6216\u8005 Java \u90a3\u6837\u521b\u5efa\u4e00\u4e2a\u5c42\u6b21\u7ed3\u6784\uff0c\u5b83\u53ea\u662f\u4e00\u79cd\u907f\u514d\u547d\u540d\u51b2\u7a81\u7684\u673a\u5236\uff0c\u56e0\u6b64\u4e0d\u8981\u8fc7\u5ea6\u5d4c\u5957\u3002 \u81ea\u5df1\u7f16\u5199\u7684\u5e93\uff0c\u5982\u679c\u63d0\u4f9b\u7ed9\u4ed6\u4eba\u4f7f\u7528\uff0c\u8bf7\u653e\u5728\u81ea\u5b9a\u4e49\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u547d\u540d\u51b2\u7a81\uff0c\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u7f16\u8f91\u5668\u7684\u4ee3\u7801\u8865\u5168\u548c\u63d0\u793a\u8fdb\u884c\u5de5\u4f5c \u5728 2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4\u3002\u63d0\u9192\u4e00\u4e0b\uff0c\u547d\u540d\u51b2\u7a81\u53d1\u751f\u5728\u76f8\u540c\u4f5c\u7528\u57df\u4e2d\u6709\u4e24\u4e2a\u76f8\u540c\u540d\u5b57\u7684\u65f6\u5019\uff0c\u8fd9\u65f6\u7f16\u8bd1\u5668\u5c06\u4e0d\u80fd\u591f\u533a\u5206\u5b83\u4eec\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u6216\u8005\u94fe\u63a5\u5668\u4f1a\u62a5\u544a\u9519\u8bef\u4fe1\u606f\u3002\u5f53\u7a0b\u5e8f\u8d8a\u53d8\u8d8a\u5927\u7684\u65f6\u5019\uff0c\u6807\u8bc6\u7b26\u7684\u6570\u91cf\u4e5f\u4f1a\u7ebf\u6027\u589e\u957f\uff0c\u8fd9\u4f1a\u4f7f\u5f97\u547d\u540d\u51b2\u7a81\u7684\u53ef\u80fd\u6027\u6210\u6307\u6570\u500d\u589e\u957f\u3002 \u8ba9\u6211\u4eec\u518d\u56de\u987e\u4e00\u4e0b\u547d\u540d\u51b2\u7a81\u7684\u4f8b\u5b50\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c foo.cpp \u548c goo.cpp \u4e2d\u5305\u542b\u4e86\u529f\u80fd\u4e0d\u540c\uff0c\u4f46\u51fd\u6570\u540d\u5b8c\u5168\u548c\u53c2\u6570\u5b8c\u5168\u76f8\u540c\u7684\u4e24\u4e2a\u51fd\u6570\u3002 foo.cpp 1 2 3 4 5 // This doSomething() adds the value of its parameters int doSomething ( int x , int y ) { return x + y ; } goo.cpp 1 2 3 4 5 // This doSomething() subtracts the value of its parameters int doSomething ( int x , int y ) { return x - y ; } main.cpp 1 2 3 4 5 6 7 8 9 #include int doSomething ( int x , int y ); // forward declaration for doSomething int main () { std :: cout << doSomething ( 4 , 3 ) << '\\n' ; // which doSomething will we get? return 0 ; } \u5982\u679c\u7a0b\u5e8f\u4e2d\u53ea\u5305\u542b foo.cpp \u6216\u8005 goo.cpp (\u4f46\u4e0d\u662f\u4e24\u4e2a\u90fd\u6709)\uff0c\u7a0b\u5e8f\u5c31\u53ef\u4ee5\u987a\u5229\u5730\u7f16\u8bd1\u548c\u8fd0\u884c\u3002\u4e0d\u8fc7\uff0c\u5f53\u628a\u5b83\u4eec\u4e24\u4e2a\u90fd\u7f16\u8bd1\u5230\u540c\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\u65f6\uff0c\u6211\u4eec\u5c31\u5411\u540c\u4e00\u4e2a\u4f5c\u7528\u57df\uff08\u5168\u5c40\u4f5c\u7528\u57df\uff09\u4e2d\u5f15\u5165\u4e86\u4e24\u4e2a\u540d\u5b57\u548c\u53c2\u6570\u90fd\u5b8c\u5168\u76f8\u540c\u7684\u51fd\u6570\uff0c\u8fd9\u5c31\u4f1a\u4ee3\u7801\u547d\u540d\u51b2\u7a81\uff0c\u9020\u6210\u7684\u540e\u679c\u5c31\u662f\u94fe\u63a5\u5668\u62a5\u9519\uff1a 1 goo.cpp:3: multiple definition of `doSomething(int, int)'; foo.cpp:3: first defined here \u6ce8\u610f\uff0c\u8fd9\u4e2a\u95ee\u9898\u5728\u4e8e \u91cd\u590d\u5b9a\u4e49 \uff0c\u6240\u4ee5\u65e0\u5173\u662f\u5426 doSomething \u88ab\u8c03\u7528\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u662f\u5bf9\u5176\u4e2d\u4e00\u4e2a\u51fd\u6570\u91cd\u547d\u540d\uff0c\u8fd9\u6837\u547d\u540d\u5c31\u4e0d\u4f1a\u51b2\u7a81\u4e86\u3002\u4f46\u662f\uff0c\u8fd9\u4e48\u505a\u5c31\u610f\u5473\u7740\u6240\u6709\u8c03\u7528\u8be5\u51fd\u6570\u7684\u5730\u65b9\u7684\u4ee3\u7801\u4e5f\u8981\u5bf9\u5e94\u5730\u4fee\u6539\uff0c\u8fd9\u4e0d\u4ec5\u662f\u4e00\u9879\u5927\u5de5\u7a0b\uff0c\u4e5f\u975e\u5e38\u5bb9\u6613\u72af\u9519\u3002\u53e6\u4e00\u4e2a\u907f\u514d\u547d\u540d\u51b2\u7a81\u7684\u529e\u6cd5\uff0c\u662f\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u547d\u540d\u7a7a\u95f4\u3002\u4e5f\u6b63\u662f\u8fd9\u6837\uff0c\u6807\u51c6\u5e93\u7684\u51fd\u6570\u90fd\u88ab\u79fb\u52a8\u5230\u4e86 std \u547d\u540d\u7a7a\u95f4\u4e2d\u3002 \u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4 C++ \u5141\u8bb8\u6211\u4eec\u901a\u8fc7 namespace \u5173\u952e\u5b57\u521b\u5efa\u4f60\u81ea\u5df1\u7684\u547d\u540d\u7a7a\u95f4\u3002\u7528\u6237\u4e3a\u4e86\u81ea\u5df1\u7684\u58f0\u660e\u521b\u5efa\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u79f0\u4e3a \u7528\u6237\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4 \u3002C++ \u63d0\u4f9b\u7684\u547d\u540d\u7a7a\u95f4\uff08\u4f8b\u5982 global \u548c std \uff09\u5e76\u6ca1\u6709\u8003\u8651\u7528\u6237\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u3002 \u547d\u540d\u7a7a\u95f4\u7684\u6807\u8bc6\u7b26\u901a\u5e38\u4e0d\u662f\u5927\u5199\u5f62\u5f0f \u6211\u4eec\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u91cd\u65b0\u7f16\u5199\u4e86\u4e0a\u9762\u7684\u51e0\u4e2a\u7a0b\u5e8f\uff1a foo.cpp 1 2 3 4 5 6 7 8 namespace foo // define a namespace named foo { // This doSomething() belongs to namespace foo int doSomething ( int x , int y ) { return x + y ; } } goo.cpp 1 2 3 4 5 6 7 8 namespace goo // define a namespace named goo { // This doSomething() belongs to namespace goo int doSomething ( int x , int y ) { return x - y ; } } \u73b0\u5728 foo.cpp \u4e2d\u7684 doSomething() \u4f4d\u4e8e foo \u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u800c goo.cpp \u4e2d\u7684 doSomething() \u5219\u4f4d\u4e8e goo \u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u91cd\u65b0\u7f16\u8bd1\u7a0b\u5e8f\uff0c\u770b\u770b\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002 main.cpp 1 2 3 4 5 6 7 int doSomething ( int x , int y ); // forward declaration for doSomething int main () { std :: cout << doSomething ( 4 , 3 ) << '\\n' ; // which doSomething will we get? return 0 ; } \u7ed3\u679c\u5c31\u662f\u6211\u4eec\u5f97\u5230\u4e86\u53e6\u5916\u7684\u9519\u8bef\u4fe1\u606f\uff01 1 ConsoleApplication1.obj : error LNK2019: unresolved external symbol \"int __cdecl doSomething(int,int)\" (?doSomething@@YAHHH@Z) referenced in function _main \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7f16\u8bd1\u6ca1\u6709\u95ee\u9898(\u56e0\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86 \u524d\u5411\u58f0\u660e )\uff0c\u4f46\u662f\u94fe\u63a5\u5668\u5e76\u4e0d\u80fd\u5728 global \u4f5c\u7528\u57df\u4e2d\u627e\u5230 doSomething \u7684\u5b9a\u4e49\u3002\u8fd9\u662f\u56e0\u4e3a\u8fd9\u4e24\u4e2a\u7248\u672c\u7684 doSomething \u90fd\u5df2\u7ecf\u4e0d\u5728 global \u4f5c\u7528\u57df\u4e2d\u4e86\u3002 \u544a\u8bc9\u7f16\u8bd1\u5668\u4f7f\u7528\u54ea\u4e2a\u7248\u672c doSomething() \u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff0c\u4e00\u79cd\u662f \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \uff0c\u4e00\u79cd\u662fusing\u8bed\u53e5\uff08\u53c2\u89c1: 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 \uff09\u3002 \u5728\u63a5\u4e0b\u6765\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8fd8\u662f\u56de\u5230\u5355\u6587\u4ef6\u7684\u573a\u666f\uff0c\u8fd9\u6837\u770b\u8d77\u6765\u66f4\u52a0\u6e05\u6670\u3002 \u901a\u8fc7\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26( :: )\u8bbf\u95ee\u547d\u540d\u7a7a\u95f4 \u544a\u8bc9\u7f16\u8bd1\u5668\u5728\u54ea\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u67e5\u627e\u51fd\u6570\u7684\u6700\u597d\u7684\u529e\u6cd5\u5c31\u662f\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26( :: )\u3002\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u544a\u8bc9\u7f16\u8bd1\u5668\u8be5\u8fd0\u7b97\u7b26\u5de6\u9762\u7684\u64cd\u4f5c\u6570\u662f\u5b83\u5e94\u8be5\u67e5\u627e\u7684\u547d\u540d\u7a7a\u95f4\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u67e5\u627e\u7f16\u8bd1\u5668\u67e5\u627e foo \u547d\u540d\u7a7a\u95f4\u7684 doSomething() \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include namespace foo // define a namespace named foo { // This doSomething() belongs to namespace foo int doSomething ( int x , int y ) { return x + y ; } } namespace goo // define a namespace named goo { // This doSomething() belongs to namespace goo int doSomething ( int x , int y ) { return x - y ; } } int main () { std :: cout << foo :: doSomething ( 4 , 3 ) << '\\n' ; // use the doSomething() that exists in namespace foo return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 7 \u5982\u679c\u4f60\u5e0c\u671b\u4f7f\u7528 goo \u4e2d\u7684 doSomething() \uff0c\u5219\u5e94\u8be5\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include namespace foo // define a namespace named foo { // This doSomething() belongs to namespace foo int doSomething ( int x , int y ) { return x + y ; } } namespace goo // define a namespace named goo { // This doSomething() belongs to namespace goo int doSomething ( int x , int y ) { return x - y ; } } int main () { std :: cout << goo :: doSomething ( 4 , 3 ) << '\\n' ; // use the doSomething() that exists in namespace goo return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 1 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u975e\u5e38\u597d\u7528\uff0c\u5b83\u5141\u8bb8\u6211\u4eec \u663e\u5f0f\u5730 \u6307\u5b9a\u4e00\u4e2a\u9700\u8981\u67e5\u627e\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u4e0d\u5b58\u5728\u4efb\u4f55\u6a21\u7cca\u7684\u542b\u4e49\u3002\u6211\u4eec\u751a\u81f3\u53ef\u4ee5\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include namespace foo // define a namespace named foo { // This doSomething() belongs to namespace foo int doSomething ( int x , int y ) { return x + y ; } } namespace goo // define a namespace named goo { // This doSomething() belongs to namespace goo int doSomething ( int x , int y ) { return x - y ; } } int main () { std :: cout << foo :: doSomething ( 4 , 3 ) << '\\n' ; // use the doSomething() that exists in namespace foo std :: cout << goo :: doSomething ( 4 , 3 ) << '\\n' ; // use the doSomething() that exists in namespace goo return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 7 1 \u4f7f\u7528\u6ca1\u6709\u524d\u7f00\u7684\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u4e5f\u53ef\u4ee5\u7528\u5728\u4e00\u4e2a\u6807\u8bc6\u7b26\u524d\u9762\uff0c\u4f46\u4e0d\u6307\u5b9a\u547d\u540d\u7a7a\u95f4\uff08\u4f8b\u5982 ::doSomething \uff09\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u4f1a\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u67e5\u627e\u6807\u8bc6\u7b26\uff08 doSomething \uff09 \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include void print () // this print lives in the global namespace { std :: cout << \" there \\n \" ; } namespace foo { void print () // this print lives in the foo namespace { std :: cout << \"Hello\" ; } } int main () { foo :: print (); // call print() in foo namespace :: print (); // call print() in global namespace (same as just calling print() in this case) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c ::print() \u7684\u6548\u679c\u548c print() \uff08\u4e0d\u8fdb\u884c\u4f5c\u7528\u57df\u89e3\u6790\uff09\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u6240\u4ee5\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u662f\u591a\u4f59\u7684\u3002\u4f46\u662f\uff0c\u5728\u63a5\u4e0b\u6765\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u80fd\u591f\u770b\u5230\u8fd9\u79cd\u65e0\u524d\u7f00\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u7684\u4f5c\u7528\u3002 \u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u89e3\u6790 \u5f53\u4e00\u4e2a\u6807\u8bc6\u7b26\u4f4d\u4e8e\u67d0\u4e2a\u547d\u540d\u7a7a\u95f4\u65f6\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\u4f5c\u7528\u57df\u89e3\u6790\uff0c\u5219\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u5728\u76f8\u540c\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\u8fdb\u884c\u67e5\u627e\u3002\u5982\u679c\u6ca1\u6709\u627e\u5230\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u5728\u6309\u7167\u5305\u542b\u5173\u7cfb\uff0c\u5728\u547d\u540d\u7a7a\u95f4\u5e8f\u5217\u4e2d\u4f9d\u6b21\u67e5\u627e\uff0c\u6700\u540e\u624d\u4f1a\u67e5\u627e global \u547d\u540d\u7a7a\u95f4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include void print () // this print lives in the global namespace { std :: cout << \" there \\n \" ; } namespace foo { void print () // this print lives in the foo namespace { std :: cout << \"Hello\" ; } void printHelloThere () { print (); // calls print() in foo namespace :: print (); // calls print() in global namespace } } int main () { foo :: printHelloThere (); return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 Hello there \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u8c03\u7528 print() \u51fd\u6570\u65f6\u5e76\u6ca1\u6709\u6307\u5b9a\u9700\u8981\u89e3\u6790\u7684\u4f5c\u7528\u57df\u3002\u56e0\u6b64\u5728 foo \u547d\u540d\u7a7a\u95f4\u4e2d\u8c03\u7528 print() \u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u67e5\u627e foo::print() \u7684\u5b9a\u4e49\u3002\u56e0\u4e3a\u8be5\u5b9a\u4e49\u5b58\u5728\uff0c\u6240\u4ee5 foo::print() \u88ab\u6267\u884c\u3002 \u5982\u679c foo::print() \u5e76\u6ca1\u6709\u88ab\u627e\u5230\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c31\u5fc5\u987b\u67e5\u627e\u5305\u542b\u8be5\u4f5c\u7528\u57df\u7684\u4f5c\u7528\u57df\uff08\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u662f global \u4f5c\u7528\u57df\uff09\u4ee5\u5c1d\u8bd5\u627e\u5230 print() \u3002 \u6ce8\u610f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0d\u5e26\u524d\u7f00\u7684\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\uff08 ::print() \uff09\u660e\u786e\u6307\u5b9a\u7f16\u8bd1\u5668\u4f7f\u7528 global \u547d\u540d\u7a7a\u95f4\u4e2d\u7684 print() \u3002 \u547d\u540d\u7a7a\u95f4\u5b9a\u4e49\u4e3a\u591a\u4e2a\u5757 \u5c06\u547d\u540d\u7a7a\u95f4\u7684\u58f0\u660e\u5206\u6210\u591a\u4e2a\u90e8\u5206\u653e\u5728\u4e0d\u540c\u7684\u4f4d\u7f6e\u662f\u5408\u6cd5\u7684\uff08\u591a\u4e2a\u6587\u4ef6\u3001\u76f8\u540c\u6587\u4ef6\u7684\u4e0d\u540c\u4f4d\u7f6e\uff09\u3002\u6240\u6709\u8fd9\u4e9b\u58f0\u660e\u90fd\u88ab\u8ba4\u4e3a\u662f\u5bf9\u5e94\u547d\u540d\u7a7a\u95f4\u58f0\u660e\u7684\u4e00\u90e8\u5206\u3002 circle.h 1 2 3 4 5 6 7 8 9 #ifndef CIRCLE_H #define CIRCLE_H namespace basicMath { constexpr double pi { 3.14 }; } #endif growth.h 1 2 3 4 5 6 7 8 9 10 #ifndef GROWTH_H #define GROWTH_H namespace basicMath { // the constant e is also part of namespace basicMath constexpr double e { 2.7 }; } #endif main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 #include \"circle.h\" // for basicMath::pi #include \"growth.h\" // for basicMath::e #include int main () { std :: cout << basicMath :: pi << '\\n' ; std :: cout << basicMath :: e << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u6211\u4eec\u6240\u613f\uff1a 1 2 3.14 2.7 \u6807\u51c6\u5e93\u5927\u91cf\u4f7f\u7528\u4e86\u8fd9\u4e00\u7279\u6027\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u6807\u51c6\u5e93\u7684\u5934\u6587\u4ef6\u90fd\u5c06\u5b83\u58f0\u660e\u7684\u5185\u5bb9\u653e\u5728 namespace std \u5757\u4e2d\u3002\u5426\u5219\uff0c\u5168\u90e8\u7684\u6807\u51c6\u5e93\u5c06\u5fc5\u987b\u5b9a\u4e49\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u6587\u4ef6\u4e2d\u4e86\u3002 \u6ce8\u610f\uff0c\u501f\u52a9\u8fd9\u4e2a\u529f\u80fd\uff0c\u4f60\u5b9e\u9645\u4e0a\u53ef\u4ee5\u628a\u81ea\u5b9a\u4e49\u7684\u51fd\u6570\u4e5f\u653e\u5230 std \u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u4e0d\u8fc7\u8fd9\u4e48\u505a\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u7684\u53d1\u751f\uff0c\u56e0\u4e3a std \u547d\u540d\u7a7a\u95f4\u5177\u6709\u7279\u6b8a\u7684\u89c4\u5219\u4ee5\u4fbf\u7981\u6b62\u7528\u6237\u53bb\u6269\u5c55\u6807\u51c6\u5e93\u7684\u4ee3\u7801\u3002 \u6ce8\u610f \u4e0d\u8981\u628a\u81ea\u5b9a\u4e49\u7684\u529f\u80fd\u5b9a\u4e49\u5728 std \u547d\u540d\u7a7a\u95f4\u4e2d\u3002 \u5982\u679c\u4f60\u9700\u8981\u628a\u4ee3\u7801\u5206\u6563\u5230\u591a\u4e2a\u6587\u4ef6\u65f6\uff0c\u4f60\u5fc5\u987b\u5728\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u4e2d\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\uff1a add.h 1 2 3 4 5 6 7 8 9 10 #ifndef ADD_H #define ADD_H namespace basicMath { // function add() is part of namespace basicMath int add ( int x , int y ); } #endif add.cpp 1 2 3 4 5 6 7 8 9 10 #include \"add.h\" namespace basicMath { // define the function add() int add ( int x , int y ) { return x + y ; } } main.cpp 1 2 3 4 5 6 7 8 9 10 #include \"add.h\" // for basicMath::add() #include int main () { std :: cout << basicMath :: add ( 4 , 3 ) << '\\n' ; return 0 ; } \u5982\u679c\u4f60\u5728\u6e90\u6587\u4ef6\u4e2d\u7701\u7565\u4e86\u547d\u540d\u7a7a\u95f4\uff0c\u7f16\u8bd1\u5668\u5c06\u65e0\u6cd5\u627e\u5230 basicMath::add \uff0c\u56e0\u4e3a\u6e90\u6587\u4ef6\u53ea\u5b9a\u4e49\u4e86 add (\u5168\u5c40\u547d\u540d\u7a7a\u95f4)\u3002\u5982\u679c\u8be5\u547d\u540d\u7a7a\u95f4\u5728\u5934\u6587\u4ef6\u4e2d\u88ab\u7701\u7565\uff0c\u5219\u201cmain.cpp\u201d \u5c31\u65e0\u6cd5\u4f7f\u7528 basicMath::add \uff0c\u56e0\u4e3a\u5b83\u53ea\u80fd\u770b\u5230 add \u7684\u58f0\u660e (\u5168\u5c40\u547d\u540d\u7a7a\u95f4)\u3002 \u547d\u540d\u7a7a\u95f4\u5d4c\u5957 \u547d\u540d\u7a7a\u95f4\u4e5f\u53ef\u4ee5\u5d4c\u5957\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include namespace foo { namespace goo // goo is a namespace inside the foo namespace { int add ( int x , int y ) { return x + y ; } } } int main () { std :: cout << foo :: goo :: add ( 1 , 2 ) << '\\n' ; return 0 ; } \u6ce8\u610f\uff0c\u56e0\u4e3a\u547d\u540d\u7a7a\u95f4 goo \u5b9a\u4e49\u5728\u547d\u540d\u7a7a\u95f4 foo \u4e2d\uff0c\u6240\u4ee5\u8981\u8bbf\u95ee add \u5fc5\u987b\u50cf\u8fd9\u6837 foo::goo::add \u3002 \u4ece C++17\u5f00\u59cb\uff0c\u5d4c\u5957\u547d\u540d\u7a7a\u95f4\u4e5f\u53ef\u4ee5\u8fd9\u6837\u58f0\u660e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include namespace foo :: goo // goo is a namespace inside the foo namespace (C++17 style) { int add ( int x , int y ) { return x + y ; } } int main () { std :: cout << foo :: goo :: add ( 1 , 2 ) << '\\n' ; return 0 ; } \u547d\u540d\u7a7a\u95f4\u522b\u540d \u5728C++\u4e2d\u5b8c\u6574\u8f93\u5165\u5d4c\u5957\u547d\u540d\u7a7a\u95f4\u7684\u6807\u8bc6\u7b26\u662f\u975e\u5e38\u75db\u82e6\u7684\u4e00\u4ef6\u4e8b\uff0c\u6240\u4ee5C++\u5141\u8bb8\u4f60\u4e3a\u547d\u540d\u7a7a\u95f4\u521b\u5efa \u522b\u540d \uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u522b\u540d\u4e3a\u4e00\u4e2a\u975e\u5e38\u957f\u7684\u547d\u540d\u7a7a\u95f4\u5e8f\u5217\u521b\u5efa\u4e00\u4e2a\u7b80\u77ed\u7684\u3001\u4e34\u65f6\u7684\u540d\u79f0\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include namespace foo :: goo { int add ( int x , int y ) { return x + y ; } } int main () { namespace active = foo :: goo ; // active now refers to foo::goo std :: cout << active :: add ( 1 , 2 ) << '\\n' ; // This is really foo::goo::add() return 0 ; } // The active alias ends here \u547d\u540d\u7a7a\u95f4\u522b\u540d\u6700\u597d\u7684\u4e00\u70b9\u662f\uff1a\u5982\u679c\u4f60\u9700\u8981\u5c06 foo::goo \u4e2d\u7684\u51fd\u6570\u79fb\u52a8\u5230\u53e6\u5916\u7684\u5730\u65b9\uff0c\u90a3\u4e48\u4f60\u53ea\u9700\u8981\u66f4\u65b0\u4e00\u4e0b\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684 active \u522b\u540d\u4f7f\u5176\u6307\u5411\u65b0\u7684\u547d\u540d\u7a7a\u95f4\u5373\u53ef\uff0c\u800c\u4e0d\u5fc5\u67e5\u627e\u66ff\u6362\u6240\u6709\u7684 foo::goo . 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include namespace foo :: goo { } namespace v2 { int add ( int x , int y ) { return x + y ; } } int main () { namespace active = v2 ; // active now refers to v2 std :: cout << active :: add ( 1 , 2 ) << '\\n' ; // We don't have to change this return 0 ; } \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0cC++\u7684\u547d\u540d\u7a7a\u95f4\u88ab\u8bbe\u8ba1\u51fa\u6765\u7684\u76ee\u7684\u662f\u4f5c\u4e3a\u4e00\u79cd\u653e\u7f6e\u547d\u540d\u51b2\u7a81\u7684\u673a\u5236\uff0c\u4f46\u5b83\u5e76\u4e0d\u662f\u4e3a\u4e86\u5b9e\u73b0\u4e00\u79cd\u4fe1\u606f\u7684\u5c42\u6b21\u5173\u7cfb\u3002\u8fd9\u4e00\u70b9\u6211\u4eec\u53ef\u4ee5\u4ece\u6807\u51c6\u5e93\u4e2d\u770b\u51fa\uff1a\u51e0\u4e4e\u5168\u90e8\u7684\u6807\u51c6\u5e93\u90fd\u4f4d\u4e8e std:: \u547d\u540d\u7a7a\u95f4\u4e2d\uff08\u5c11\u6570\u65b0\u7684\u529f\u80fd\u653e\u7f6e\u5728\u5d4c\u5957\u547d\u540d\u7a7a\u95f4\u4e2d\uff09\u3002\u5728\u8fd9\u4e00\u70b9\u4e0a\uff0c\u5176\u4ed6\u7684\u4e00\u4e9b\u8bed\u8a00\uff08\u4f8b\u5982C#\uff09\u548c C++ \u662f\u622a\u7136\u4e0d\u540c\u7684\u3002 \u603b\u7684\u6765\u8bf4\uff0c\u5e94\u5f53\u5c3d\u91cf\u907f\u514d\u547d\u540d\u7a7a\u95f4\u7684\u5d4c\u5957\u3002 \u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u4f7f\u7528\u547d\u540d\u7a7a\u95f4 \u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u88ab\u7528\u6765\u5206\u5272\u4e0e\u5f53\u524d\u5e94\u7528\u7a0b\u5e8f\u5f3a\u76f8\u5173\u7684\u4ee3\u7801\u548c\u90a3\u4e9b\u53ef\u80fd\u5728\u65e5\u540e\u88ab\u91cd\u7528\u7684\u4ee3\u7801\uff08\u4f8b\u5982\u6570\u5b66\u51fd\u6570\uff09\u3002\u4f8b\u5982\uff0c\u7269\u7406\u548c\u6570\u5b66\u51fd\u6570\u53ef\u4ee5\u5b9a\u4e49\u5728\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d(\u4f8b\u5982 math:: )\u3002\u8bed\u8a00\u548c\u672c\u5730\u5316\u7684\u51fd\u6570\u5219\u53ef\u4ee5\u5b9a\u4e49\u5728\u53e6\u5916\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\uff08\u4f8b\u5982 lang:: \uff09 \u3002 \u5982\u679c\u4f60\u7f16\u5199\u7684\u662f\u5e93\u51fd\u6570\u6216\u7528\u4e8e\u63d0\u4f9b\u7ed9\u4ed6\u4eba\u4f7f\u7528\u7684\u4ee3\u7801\uff0c\u8bf7\u59cb\u7ec8\u628a\u4f60\u7684\u51fd\u6570\u5b9a\u4e49\u5728\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u5982\u679c\u4f60\u4e0d\u9075\u5faa\u8fd9\u9879\u6700\u4f73\u5b9e\u8df5\uff0c\u5373\u4f60\u7684\u4ee3\u7801\u6ca1\u6709\u58f0\u660e\u5728\u4e00\u4e2a\u7279\u5b9a\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u90a3\u4e48\u5f88\u6709\u53ef\u80fd\u4f1a\u9020\u6210\u547d\u540d\u51b2\u7a81\u3002\u5c06\u4f60\u7684\u4ee3\u7801\u653e\u5728\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u53e6\u5916\u4e00\u4e2a\u597d\u5904\u662f\uff0c\u4f60\u7684\u7528\u6237\u53ef\u4ee5\u501f\u52a9\u7f16\u8f91\u5668\u7684\u81ea\u52a8\u8865\u5168\u548c\u63d0\u793a\u529f\u80fd\u770b\u5230\u4f60\u5e93\u4e2d\u7684\u7684\u5185\u5bb9\u3002 \u5982\u4f55\u4f7f\u7528\u547d\u540d\u7a7a\u95f4 \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0cC++ \u4e2d\u7684\u547d\u540d\u7a7a\u95f4\u6700\u521d\u5e76\u4e0d\u662f\u4f5c\u4e3a\u4e00\u79cd\u5b9e\u73b0\u4fe1\u606f\u5c42\u6b21\u7ed3\u6784\u7684\u65b9\u6cd5\u800c\u8bbe\u8ba1\u7684\uff0c\u5b83\u4eec\u4e3b\u8981\u662f\u4f5c\u4e3a\u4e00\u79cd\u9632\u6b62\u547d\u540d\u51b2\u7a81\u7684\u673a\u5236\u800c\u8bbe\u8ba1\u7684\u3002\u6574\u4e2a\u6807\u51c6\u5e93\u90fd\u4f4d\u4e8e\u5355\u4e00\u7684\u9876\u7ea7\u547d\u540d\u7a7a\u95f4 std \u4e0b\uff0c\u8fd9\u5c31\u662f\u6700\u597d\u7684\u8bc1\u660e\u3002\u5f15\u5165\u5927\u91cf\u540d\u79f0\u7684\u65b0\u6807\u51c6\u5e93\u529f\u80fd\u5df2\u5f00\u59cb\u4f7f\u7528\u5d4c\u5957\u540d\u79f0\u7a7a\u95f4\uff08\u5982 std::ranges \uff09\uff0c\u4ee5\u907f\u514d\u5728 std \u540d\u79f0\u7a7a\u95f4\u5185\u53d1\u751f\u547d\u540d\u51b2\u7a81\u3002 \u4e3a\u81ea\u5df1\u5f00\u53d1\u7684\u5c0f\u578b\u5e94\u7528\u7a0b\u5e8f\u901a\u5e38\u4e0d\u9700\u8981\u653e\u5728\u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u5305\u542b\u5927\u91cf\u7b2c\u4e09\u65b9\u5e93\u7684\u5927\u578b\u4e2a\u4eba\u9879\u76ee\uff0c\u5bf9\u4ee3\u7801\u8fdb\u884c\u547d\u540d\u53ef\u4ee5\u907f\u514d\u4e0e\u672a\u6b63\u786e\u547d\u540d\u7684\u5e93\u53d1\u751f\u547d\u540d\u51b2\u7a81\u3002 \u4f5c\u8005\u6ce8 \u8fd9\u4e9b\u6559\u7a0b\u4e2d\u7684\u793a\u4f8b\u901a\u5e38\u4e0d\u4f1a\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\uff0c\u9664\u975e\u6211\u4eec\u8981\u8bf4\u660e\u547d\u540d\u7a7a\u95f4\u7684\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\uff0c\u4ee5\u4fdd\u6301\u793a\u4f8b\u7684\u7b80\u6d01\u6027\u3002 \u4efb\u4f55\u5c06\u53d1\u5e03\u7ed9\u4ed6\u4eba\u7684\u4ee3\u7801\u90fd\u5fc5\u987b\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\uff0c\u4ee5\u9632\u6b62\u4e0e\u96c6\u6210\u5230\u5176\u4e2d\u7684\u4ee3\u7801\u53d1\u751f\u51b2\u7a81\u3002\u901a\u5e38\u4e00\u4e2a\u9876\u7ea7\u547d\u540d\u7a7a\u95f4\u5c31\u8db3\u591f\u4e86\uff08\u4f8b\u5982 Foologger \uff09\u3002\u5c06\u5e93\u4ee3\u7801\u7f6e\u4e8e\u547d\u540d\u7a7a\u95f4\u5185\u8fd8\u6709\u4e00\u4e2a\u597d\u5904\uff0c\u5373\u7528\u6237\u53ef\u4ee5\u4f7f\u7528\u7f16\u8f91\u5668\u7684\u81ea\u52a8\u5b8c\u6210\u548c\u5efa\u8bae\u529f\u80fd\u67e5\u770b\u5e93\u7684\u5185\u5bb9\uff08\u4f8b\u5982\uff0c\u5982\u679c\u8f93\u5165 Foologger \uff0c\u81ea\u52a8\u5b8c\u6210\u529f\u80fd\u4f1a\u663e\u793a Foologger \u4e2d\u7684\u6240\u6709\u540d\u79f0\uff09\u3002 \u5728\u591a\u56e2\u961f\u7ec4\u7ec7\u4e2d\uff0c\u4e3a\u4e86\u9632\u6b62\u4e0d\u540c\u56e2\u961f\u751f\u6210\u7684\u4ee3\u7801\u4e4b\u95f4\u51fa\u73b0\u547d\u540d\u51b2\u7a81\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528\u4e24\u7ea7\u751a\u81f3\u4e09\u7ea7\u547d\u540d\u7a7a\u95f4\u3002\u8fd9\u4e9b\u547d\u540d\u7a7a\u95f4\u901a\u5e38\u91c7\u7528\u4ee5\u4e0b\u5f62\u5f0f\u4e4b\u4e00\uff1a \u9879\u76ee\u6216\u5e93 :: \u6a21\u5757\uff08\u4f8b\u5982 Foologger::Lang \uff09 \u516c\u53f8\u6216\u7ec4\u7ec7 :: \u9879\u76ee\u6216\u5e93\uff08\u4f8b\u5982 Foosoft::Foologger \uff09 \u516c\u53f8\u6216\u7ec4\u7ec7 :: \u9879\u76ee\u6216\u5e93 :: \u6a21\u5757\uff08\u4f8b\u5982 Foosoft::Foologger::Lang \uff09 \u4f7f\u7528\u6a21\u5757\u7ea7\u547d\u540d\u7a7a\u95f4\u6709\u52a9\u4e8e\u5c06\u65e5\u540e\u53ef\u80fd\u91cd\u7528\u7684\u4ee3\u7801\u4e0e\u4e0d\u53ef\u91cd\u7528\u7684\u7279\u5b9a\u5e94\u7528\u7a0b\u5e8f\u4ee3\u7801\u533a\u5206\u5f00\u6765\u3002\u4f8b\u5982\uff0c\u7269\u7406\u548c\u6570\u5b66\u51fd\u6570\u53ef\u4ee5\u653e\u5728\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\uff08\u5982 Math:: \uff09\u3002\u8bed\u8a00\u548c\u672c\u5730\u5316\u529f\u80fd\u53ef\u4ee5\u653e\u5728\u53e6\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\uff08\u5982 Lang:: \uff09\u3002\u4e0d\u8fc7\uff0c\u76ee\u5f55\u7ed3\u6784\u4e5f\u53ef\u7528\u4e8e\u6b64\u76ee\u7684\uff08\u7279\u5b9a\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\u4ee3\u7801\u653e\u5728\u9879\u76ee\u76ee\u5f55\u6811\u4e2d\uff0c\u53ef\u91cd\u590d\u4f7f\u7528\u7684\u4ee3\u7801\u653e\u5728\u5355\u72ec\u7684\u5171\u4eab\u76ee\u5f55\u6811\u4e2d\uff09\u3002 \u4e00\u822c\u6765\u8bf4\uff0c\u5e94\u907f\u514d\u6df1\u5ea6\u5d4c\u5957\u547d\u540d\u7a7a\u95f4\uff08\u8d85\u8fc7 3 \u5c42\uff09\u3002","title":"6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26"},{"location":"6-2-User-defined-namespaces-and-the-scope-resolution-operator/#_1","text":"C++ \u5141\u8bb8\u6211\u4eec\u901a\u8fc7 namespace \u5173\u952e\u5b57\u521b\u5efa\u4f60\u81ea\u5df1\u7684\u547d\u540d\u7a7a\u95f4\u3002\u7528\u6237\u4e3a\u4e86\u81ea\u5df1\u7684\u58f0\u660e\u521b\u5efa\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u79f0\u4e3a \u7528\u6237\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4 \u3002C++ \u63d0\u4f9b\u7684\u547d\u540d\u7a7a\u95f4\uff08\u4f8b\u5982 global \u548c std \uff09\u5e76\u6ca1\u6709\u8003\u8651\u7528\u6237\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u3002","title":"\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4"},{"location":"6-2-User-defined-namespaces-and-the-scope-resolution-operator/#_2","text":"\u6211\u4eec\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u91cd\u65b0\u7f16\u5199\u4e86\u4e0a\u9762\u7684\u51e0\u4e2a\u7a0b\u5e8f\uff1a foo.cpp 1 2 3 4 5 6 7 8 namespace foo // define a namespace named foo { // This doSomething() belongs to namespace foo int doSomething ( int x , int y ) { return x + y ; } } goo.cpp 1 2 3 4 5 6 7 8 namespace goo // define a namespace named goo { // This doSomething() belongs to namespace goo int doSomething ( int x , int y ) { return x - y ; } } \u73b0\u5728 foo.cpp \u4e2d\u7684 doSomething() \u4f4d\u4e8e foo \u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u800c goo.cpp \u4e2d\u7684 doSomething() \u5219\u4f4d\u4e8e goo \u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u91cd\u65b0\u7f16\u8bd1\u7a0b\u5e8f\uff0c\u770b\u770b\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002 main.cpp 1 2 3 4 5 6 7 int doSomething ( int x , int y ); // forward declaration for doSomething int main () { std :: cout << doSomething ( 4 , 3 ) << '\\n' ; // which doSomething will we get? return 0 ; } \u7ed3\u679c\u5c31\u662f\u6211\u4eec\u5f97\u5230\u4e86\u53e6\u5916\u7684\u9519\u8bef\u4fe1\u606f\uff01 1 ConsoleApplication1.obj : error LNK2019: unresolved external symbol \"int __cdecl doSomething(int,int)\" (?doSomething@@YAHHH@Z) referenced in function _main \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7f16\u8bd1\u6ca1\u6709\u95ee\u9898(\u56e0\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86 \u524d\u5411\u58f0\u660e )\uff0c\u4f46\u662f\u94fe\u63a5\u5668\u5e76\u4e0d\u80fd\u5728 global \u4f5c\u7528\u57df\u4e2d\u627e\u5230 doSomething \u7684\u5b9a\u4e49\u3002\u8fd9\u662f\u56e0\u4e3a\u8fd9\u4e24\u4e2a\u7248\u672c\u7684 doSomething \u90fd\u5df2\u7ecf\u4e0d\u5728 global \u4f5c\u7528\u57df\u4e2d\u4e86\u3002 \u544a\u8bc9\u7f16\u8bd1\u5668\u4f7f\u7528\u54ea\u4e2a\u7248\u672c doSomething() \u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff0c\u4e00\u79cd\u662f \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \uff0c\u4e00\u79cd\u662fusing\u8bed\u53e5\uff08\u53c2\u89c1: 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 \uff09\u3002 \u5728\u63a5\u4e0b\u6765\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8fd8\u662f\u56de\u5230\u5355\u6587\u4ef6\u7684\u573a\u666f\uff0c\u8fd9\u6837\u770b\u8d77\u6765\u66f4\u52a0\u6e05\u6670\u3002","title":"\u547d\u540d\u7a7a\u95f4\u7684\u6807\u8bc6\u7b26\u901a\u5e38\u4e0d\u662f\u5927\u5199\u5f62\u5f0f"},{"location":"6-2-User-defined-namespaces-and-the-scope-resolution-operator/#_3","text":"\u544a\u8bc9\u7f16\u8bd1\u5668\u5728\u54ea\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u67e5\u627e\u51fd\u6570\u7684\u6700\u597d\u7684\u529e\u6cd5\u5c31\u662f\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26( :: )\u3002\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u544a\u8bc9\u7f16\u8bd1\u5668\u8be5\u8fd0\u7b97\u7b26\u5de6\u9762\u7684\u64cd\u4f5c\u6570\u662f\u5b83\u5e94\u8be5\u67e5\u627e\u7684\u547d\u540d\u7a7a\u95f4\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u67e5\u627e\u7f16\u8bd1\u5668\u67e5\u627e foo \u547d\u540d\u7a7a\u95f4\u7684 doSomething() \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include namespace foo // define a namespace named foo { // This doSomething() belongs to namespace foo int doSomething ( int x , int y ) { return x + y ; } } namespace goo // define a namespace named goo { // This doSomething() belongs to namespace goo int doSomething ( int x , int y ) { return x - y ; } } int main () { std :: cout << foo :: doSomething ( 4 , 3 ) << '\\n' ; // use the doSomething() that exists in namespace foo return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 7 \u5982\u679c\u4f60\u5e0c\u671b\u4f7f\u7528 goo \u4e2d\u7684 doSomething() \uff0c\u5219\u5e94\u8be5\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include namespace foo // define a namespace named foo { // This doSomething() belongs to namespace foo int doSomething ( int x , int y ) { return x + y ; } } namespace goo // define a namespace named goo { // This doSomething() belongs to namespace goo int doSomething ( int x , int y ) { return x - y ; } } int main () { std :: cout << goo :: doSomething ( 4 , 3 ) << '\\n' ; // use the doSomething() that exists in namespace goo return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 1 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u975e\u5e38\u597d\u7528\uff0c\u5b83\u5141\u8bb8\u6211\u4eec \u663e\u5f0f\u5730 \u6307\u5b9a\u4e00\u4e2a\u9700\u8981\u67e5\u627e\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u4e0d\u5b58\u5728\u4efb\u4f55\u6a21\u7cca\u7684\u542b\u4e49\u3002\u6211\u4eec\u751a\u81f3\u53ef\u4ee5\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include namespace foo // define a namespace named foo { // This doSomething() belongs to namespace foo int doSomething ( int x , int y ) { return x + y ; } } namespace goo // define a namespace named goo { // This doSomething() belongs to namespace goo int doSomething ( int x , int y ) { return x - y ; } } int main () { std :: cout << foo :: doSomething ( 4 , 3 ) << '\\n' ; // use the doSomething() that exists in namespace foo std :: cout << goo :: doSomething ( 4 , 3 ) << '\\n' ; // use the doSomething() that exists in namespace goo return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 7 1","title":"\u901a\u8fc7\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26(::)\u8bbf\u95ee\u547d\u540d\u7a7a\u95f4"},{"location":"6-2-User-defined-namespaces-and-the-scope-resolution-operator/#_4","text":"\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u4e5f\u53ef\u4ee5\u7528\u5728\u4e00\u4e2a\u6807\u8bc6\u7b26\u524d\u9762\uff0c\u4f46\u4e0d\u6307\u5b9a\u547d\u540d\u7a7a\u95f4\uff08\u4f8b\u5982 ::doSomething \uff09\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u4f1a\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u67e5\u627e\u6807\u8bc6\u7b26\uff08 doSomething \uff09 \u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include void print () // this print lives in the global namespace { std :: cout << \" there \\n \" ; } namespace foo { void print () // this print lives in the foo namespace { std :: cout << \"Hello\" ; } } int main () { foo :: print (); // call print() in foo namespace :: print (); // call print() in global namespace (same as just calling print() in this case) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c ::print() \u7684\u6548\u679c\u548c print() \uff08\u4e0d\u8fdb\u884c\u4f5c\u7528\u57df\u89e3\u6790\uff09\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u6240\u4ee5\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u662f\u591a\u4f59\u7684\u3002\u4f46\u662f\uff0c\u5728\u63a5\u4e0b\u6765\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u80fd\u591f\u770b\u5230\u8fd9\u79cd\u65e0\u524d\u7f00\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u7684\u4f5c\u7528\u3002","title":"\u4f7f\u7528\u6ca1\u6709\u524d\u7f00\u7684\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26"},{"location":"6-2-User-defined-namespaces-and-the-scope-resolution-operator/#_5","text":"\u5f53\u4e00\u4e2a\u6807\u8bc6\u7b26\u4f4d\u4e8e\u67d0\u4e2a\u547d\u540d\u7a7a\u95f4\u65f6\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\u4f5c\u7528\u57df\u89e3\u6790\uff0c\u5219\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u5728\u76f8\u540c\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\u8fdb\u884c\u67e5\u627e\u3002\u5982\u679c\u6ca1\u6709\u627e\u5230\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u5728\u6309\u7167\u5305\u542b\u5173\u7cfb\uff0c\u5728\u547d\u540d\u7a7a\u95f4\u5e8f\u5217\u4e2d\u4f9d\u6b21\u67e5\u627e\uff0c\u6700\u540e\u624d\u4f1a\u67e5\u627e global \u547d\u540d\u7a7a\u95f4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include void print () // this print lives in the global namespace { std :: cout << \" there \\n \" ; } namespace foo { void print () // this print lives in the foo namespace { std :: cout << \"Hello\" ; } void printHelloThere () { print (); // calls print() in foo namespace :: print (); // calls print() in global namespace } } int main () { foo :: printHelloThere (); return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 Hello there \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u8c03\u7528 print() \u51fd\u6570\u65f6\u5e76\u6ca1\u6709\u6307\u5b9a\u9700\u8981\u89e3\u6790\u7684\u4f5c\u7528\u57df\u3002\u56e0\u6b64\u5728 foo \u547d\u540d\u7a7a\u95f4\u4e2d\u8c03\u7528 print() \u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u67e5\u627e foo::print() \u7684\u5b9a\u4e49\u3002\u56e0\u4e3a\u8be5\u5b9a\u4e49\u5b58\u5728\uff0c\u6240\u4ee5 foo::print() \u88ab\u6267\u884c\u3002 \u5982\u679c foo::print() \u5e76\u6ca1\u6709\u88ab\u627e\u5230\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c31\u5fc5\u987b\u67e5\u627e\u5305\u542b\u8be5\u4f5c\u7528\u57df\u7684\u4f5c\u7528\u57df\uff08\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u662f global \u4f5c\u7528\u57df\uff09\u4ee5\u5c1d\u8bd5\u627e\u5230 print() \u3002 \u6ce8\u610f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0d\u5e26\u524d\u7f00\u7684\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\uff08 ::print() \uff09\u660e\u786e\u6307\u5b9a\u7f16\u8bd1\u5668\u4f7f\u7528 global \u547d\u540d\u7a7a\u95f4\u4e2d\u7684 print() \u3002","title":"\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u89e3\u6790"},{"location":"6-2-User-defined-namespaces-and-the-scope-resolution-operator/#_6","text":"\u5c06\u547d\u540d\u7a7a\u95f4\u7684\u58f0\u660e\u5206\u6210\u591a\u4e2a\u90e8\u5206\u653e\u5728\u4e0d\u540c\u7684\u4f4d\u7f6e\u662f\u5408\u6cd5\u7684\uff08\u591a\u4e2a\u6587\u4ef6\u3001\u76f8\u540c\u6587\u4ef6\u7684\u4e0d\u540c\u4f4d\u7f6e\uff09\u3002\u6240\u6709\u8fd9\u4e9b\u58f0\u660e\u90fd\u88ab\u8ba4\u4e3a\u662f\u5bf9\u5e94\u547d\u540d\u7a7a\u95f4\u58f0\u660e\u7684\u4e00\u90e8\u5206\u3002 circle.h 1 2 3 4 5 6 7 8 9 #ifndef CIRCLE_H #define CIRCLE_H namespace basicMath { constexpr double pi { 3.14 }; } #endif growth.h 1 2 3 4 5 6 7 8 9 10 #ifndef GROWTH_H #define GROWTH_H namespace basicMath { // the constant e is also part of namespace basicMath constexpr double e { 2.7 }; } #endif main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 #include \"circle.h\" // for basicMath::pi #include \"growth.h\" // for basicMath::e #include int main () { std :: cout << basicMath :: pi << '\\n' ; std :: cout << basicMath :: e << '\\n' ; return 0 ; } \u6253\u5370\u7ed3\u679c\u5982\u6211\u4eec\u6240\u613f\uff1a 1 2 3.14 2.7 \u6807\u51c6\u5e93\u5927\u91cf\u4f7f\u7528\u4e86\u8fd9\u4e00\u7279\u6027\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u6807\u51c6\u5e93\u7684\u5934\u6587\u4ef6\u90fd\u5c06\u5b83\u58f0\u660e\u7684\u5185\u5bb9\u653e\u5728 namespace std \u5757\u4e2d\u3002\u5426\u5219\uff0c\u5168\u90e8\u7684\u6807\u51c6\u5e93\u5c06\u5fc5\u987b\u5b9a\u4e49\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u6587\u4ef6\u4e2d\u4e86\u3002 \u6ce8\u610f\uff0c\u501f\u52a9\u8fd9\u4e2a\u529f\u80fd\uff0c\u4f60\u5b9e\u9645\u4e0a\u53ef\u4ee5\u628a\u81ea\u5b9a\u4e49\u7684\u51fd\u6570\u4e5f\u653e\u5230 std \u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u4e0d\u8fc7\u8fd9\u4e48\u505a\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u7684\u53d1\u751f\uff0c\u56e0\u4e3a std \u547d\u540d\u7a7a\u95f4\u5177\u6709\u7279\u6b8a\u7684\u89c4\u5219\u4ee5\u4fbf\u7981\u6b62\u7528\u6237\u53bb\u6269\u5c55\u6807\u51c6\u5e93\u7684\u4ee3\u7801\u3002 \u6ce8\u610f \u4e0d\u8981\u628a\u81ea\u5b9a\u4e49\u7684\u529f\u80fd\u5b9a\u4e49\u5728 std \u547d\u540d\u7a7a\u95f4\u4e2d\u3002 \u5982\u679c\u4f60\u9700\u8981\u628a\u4ee3\u7801\u5206\u6563\u5230\u591a\u4e2a\u6587\u4ef6\u65f6\uff0c\u4f60\u5fc5\u987b\u5728\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u4e2d\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\uff1a add.h 1 2 3 4 5 6 7 8 9 10 #ifndef ADD_H #define ADD_H namespace basicMath { // function add() is part of namespace basicMath int add ( int x , int y ); } #endif add.cpp 1 2 3 4 5 6 7 8 9 10 #include \"add.h\" namespace basicMath { // define the function add() int add ( int x , int y ) { return x + y ; } } main.cpp 1 2 3 4 5 6 7 8 9 10 #include \"add.h\" // for basicMath::add() #include int main () { std :: cout << basicMath :: add ( 4 , 3 ) << '\\n' ; return 0 ; } \u5982\u679c\u4f60\u5728\u6e90\u6587\u4ef6\u4e2d\u7701\u7565\u4e86\u547d\u540d\u7a7a\u95f4\uff0c\u7f16\u8bd1\u5668\u5c06\u65e0\u6cd5\u627e\u5230 basicMath::add \uff0c\u56e0\u4e3a\u6e90\u6587\u4ef6\u53ea\u5b9a\u4e49\u4e86 add (\u5168\u5c40\u547d\u540d\u7a7a\u95f4)\u3002\u5982\u679c\u8be5\u547d\u540d\u7a7a\u95f4\u5728\u5934\u6587\u4ef6\u4e2d\u88ab\u7701\u7565\uff0c\u5219\u201cmain.cpp\u201d \u5c31\u65e0\u6cd5\u4f7f\u7528 basicMath::add \uff0c\u56e0\u4e3a\u5b83\u53ea\u80fd\u770b\u5230 add \u7684\u58f0\u660e (\u5168\u5c40\u547d\u540d\u7a7a\u95f4)\u3002","title":"\u547d\u540d\u7a7a\u95f4\u5b9a\u4e49\u4e3a\u591a\u4e2a\u5757"},{"location":"6-2-User-defined-namespaces-and-the-scope-resolution-operator/#_7","text":"\u547d\u540d\u7a7a\u95f4\u4e5f\u53ef\u4ee5\u5d4c\u5957\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include namespace foo { namespace goo // goo is a namespace inside the foo namespace { int add ( int x , int y ) { return x + y ; } } } int main () { std :: cout << foo :: goo :: add ( 1 , 2 ) << '\\n' ; return 0 ; } \u6ce8\u610f\uff0c\u56e0\u4e3a\u547d\u540d\u7a7a\u95f4 goo \u5b9a\u4e49\u5728\u547d\u540d\u7a7a\u95f4 foo \u4e2d\uff0c\u6240\u4ee5\u8981\u8bbf\u95ee add \u5fc5\u987b\u50cf\u8fd9\u6837 foo::goo::add \u3002 \u4ece C++17\u5f00\u59cb\uff0c\u5d4c\u5957\u547d\u540d\u7a7a\u95f4\u4e5f\u53ef\u4ee5\u8fd9\u6837\u58f0\u660e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include namespace foo :: goo // goo is a namespace inside the foo namespace (C++17 style) { int add ( int x , int y ) { return x + y ; } } int main () { std :: cout << foo :: goo :: add ( 1 , 2 ) << '\\n' ; return 0 ; }","title":"\u547d\u540d\u7a7a\u95f4\u5d4c\u5957"},{"location":"6-2-User-defined-namespaces-and-the-scope-resolution-operator/#_8","text":"\u5728C++\u4e2d\u5b8c\u6574\u8f93\u5165\u5d4c\u5957\u547d\u540d\u7a7a\u95f4\u7684\u6807\u8bc6\u7b26\u662f\u975e\u5e38\u75db\u82e6\u7684\u4e00\u4ef6\u4e8b\uff0c\u6240\u4ee5C++\u5141\u8bb8\u4f60\u4e3a\u547d\u540d\u7a7a\u95f4\u521b\u5efa \u522b\u540d \uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u522b\u540d\u4e3a\u4e00\u4e2a\u975e\u5e38\u957f\u7684\u547d\u540d\u7a7a\u95f4\u5e8f\u5217\u521b\u5efa\u4e00\u4e2a\u7b80\u77ed\u7684\u3001\u4e34\u65f6\u7684\u540d\u79f0\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include namespace foo :: goo { int add ( int x , int y ) { return x + y ; } } int main () { namespace active = foo :: goo ; // active now refers to foo::goo std :: cout << active :: add ( 1 , 2 ) << '\\n' ; // This is really foo::goo::add() return 0 ; } // The active alias ends here \u547d\u540d\u7a7a\u95f4\u522b\u540d\u6700\u597d\u7684\u4e00\u70b9\u662f\uff1a\u5982\u679c\u4f60\u9700\u8981\u5c06 foo::goo \u4e2d\u7684\u51fd\u6570\u79fb\u52a8\u5230\u53e6\u5916\u7684\u5730\u65b9\uff0c\u90a3\u4e48\u4f60\u53ea\u9700\u8981\u66f4\u65b0\u4e00\u4e0b\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684 active \u522b\u540d\u4f7f\u5176\u6307\u5411\u65b0\u7684\u547d\u540d\u7a7a\u95f4\u5373\u53ef\uff0c\u800c\u4e0d\u5fc5\u67e5\u627e\u66ff\u6362\u6240\u6709\u7684 foo::goo . 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include namespace foo :: goo { } namespace v2 { int add ( int x , int y ) { return x + y ; } } int main () { namespace active = v2 ; // active now refers to v2 std :: cout << active :: add ( 1 , 2 ) << '\\n' ; // We don't have to change this return 0 ; } \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0cC++\u7684\u547d\u540d\u7a7a\u95f4\u88ab\u8bbe\u8ba1\u51fa\u6765\u7684\u76ee\u7684\u662f\u4f5c\u4e3a\u4e00\u79cd\u653e\u7f6e\u547d\u540d\u51b2\u7a81\u7684\u673a\u5236\uff0c\u4f46\u5b83\u5e76\u4e0d\u662f\u4e3a\u4e86\u5b9e\u73b0\u4e00\u79cd\u4fe1\u606f\u7684\u5c42\u6b21\u5173\u7cfb\u3002\u8fd9\u4e00\u70b9\u6211\u4eec\u53ef\u4ee5\u4ece\u6807\u51c6\u5e93\u4e2d\u770b\u51fa\uff1a\u51e0\u4e4e\u5168\u90e8\u7684\u6807\u51c6\u5e93\u90fd\u4f4d\u4e8e std:: \u547d\u540d\u7a7a\u95f4\u4e2d\uff08\u5c11\u6570\u65b0\u7684\u529f\u80fd\u653e\u7f6e\u5728\u5d4c\u5957\u547d\u540d\u7a7a\u95f4\u4e2d\uff09\u3002\u5728\u8fd9\u4e00\u70b9\u4e0a\uff0c\u5176\u4ed6\u7684\u4e00\u4e9b\u8bed\u8a00\uff08\u4f8b\u5982C#\uff09\u548c C++ \u662f\u622a\u7136\u4e0d\u540c\u7684\u3002 \u603b\u7684\u6765\u8bf4\uff0c\u5e94\u5f53\u5c3d\u91cf\u907f\u514d\u547d\u540d\u7a7a\u95f4\u7684\u5d4c\u5957\u3002","title":"\u547d\u540d\u7a7a\u95f4\u522b\u540d"},{"location":"6-2-User-defined-namespaces-and-the-scope-resolution-operator/#_9","text":"\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u88ab\u7528\u6765\u5206\u5272\u4e0e\u5f53\u524d\u5e94\u7528\u7a0b\u5e8f\u5f3a\u76f8\u5173\u7684\u4ee3\u7801\u548c\u90a3\u4e9b\u53ef\u80fd\u5728\u65e5\u540e\u88ab\u91cd\u7528\u7684\u4ee3\u7801\uff08\u4f8b\u5982\u6570\u5b66\u51fd\u6570\uff09\u3002\u4f8b\u5982\uff0c\u7269\u7406\u548c\u6570\u5b66\u51fd\u6570\u53ef\u4ee5\u5b9a\u4e49\u5728\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d(\u4f8b\u5982 math:: )\u3002\u8bed\u8a00\u548c\u672c\u5730\u5316\u7684\u51fd\u6570\u5219\u53ef\u4ee5\u5b9a\u4e49\u5728\u53e6\u5916\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\uff08\u4f8b\u5982 lang:: \uff09 \u3002 \u5982\u679c\u4f60\u7f16\u5199\u7684\u662f\u5e93\u51fd\u6570\u6216\u7528\u4e8e\u63d0\u4f9b\u7ed9\u4ed6\u4eba\u4f7f\u7528\u7684\u4ee3\u7801\uff0c\u8bf7\u59cb\u7ec8\u628a\u4f60\u7684\u51fd\u6570\u5b9a\u4e49\u5728\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u5982\u679c\u4f60\u4e0d\u9075\u5faa\u8fd9\u9879\u6700\u4f73\u5b9e\u8df5\uff0c\u5373\u4f60\u7684\u4ee3\u7801\u6ca1\u6709\u58f0\u660e\u5728\u4e00\u4e2a\u7279\u5b9a\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u90a3\u4e48\u5f88\u6709\u53ef\u80fd\u4f1a\u9020\u6210\u547d\u540d\u51b2\u7a81\u3002\u5c06\u4f60\u7684\u4ee3\u7801\u653e\u5728\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u53e6\u5916\u4e00\u4e2a\u597d\u5904\u662f\uff0c\u4f60\u7684\u7528\u6237\u53ef\u4ee5\u501f\u52a9\u7f16\u8f91\u5668\u7684\u81ea\u52a8\u8865\u5168\u548c\u63d0\u793a\u529f\u80fd\u770b\u5230\u4f60\u5e93\u4e2d\u7684\u7684\u5185\u5bb9\u3002","title":"\u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u4f7f\u7528\u547d\u540d\u7a7a\u95f4"},{"location":"6-2-User-defined-namespaces-and-the-scope-resolution-operator/#_10","text":"\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0cC++ \u4e2d\u7684\u547d\u540d\u7a7a\u95f4\u6700\u521d\u5e76\u4e0d\u662f\u4f5c\u4e3a\u4e00\u79cd\u5b9e\u73b0\u4fe1\u606f\u5c42\u6b21\u7ed3\u6784\u7684\u65b9\u6cd5\u800c\u8bbe\u8ba1\u7684\uff0c\u5b83\u4eec\u4e3b\u8981\u662f\u4f5c\u4e3a\u4e00\u79cd\u9632\u6b62\u547d\u540d\u51b2\u7a81\u7684\u673a\u5236\u800c\u8bbe\u8ba1\u7684\u3002\u6574\u4e2a\u6807\u51c6\u5e93\u90fd\u4f4d\u4e8e\u5355\u4e00\u7684\u9876\u7ea7\u547d\u540d\u7a7a\u95f4 std \u4e0b\uff0c\u8fd9\u5c31\u662f\u6700\u597d\u7684\u8bc1\u660e\u3002\u5f15\u5165\u5927\u91cf\u540d\u79f0\u7684\u65b0\u6807\u51c6\u5e93\u529f\u80fd\u5df2\u5f00\u59cb\u4f7f\u7528\u5d4c\u5957\u540d\u79f0\u7a7a\u95f4\uff08\u5982 std::ranges \uff09\uff0c\u4ee5\u907f\u514d\u5728 std \u540d\u79f0\u7a7a\u95f4\u5185\u53d1\u751f\u547d\u540d\u51b2\u7a81\u3002 \u4e3a\u81ea\u5df1\u5f00\u53d1\u7684\u5c0f\u578b\u5e94\u7528\u7a0b\u5e8f\u901a\u5e38\u4e0d\u9700\u8981\u653e\u5728\u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u5305\u542b\u5927\u91cf\u7b2c\u4e09\u65b9\u5e93\u7684\u5927\u578b\u4e2a\u4eba\u9879\u76ee\uff0c\u5bf9\u4ee3\u7801\u8fdb\u884c\u547d\u540d\u53ef\u4ee5\u907f\u514d\u4e0e\u672a\u6b63\u786e\u547d\u540d\u7684\u5e93\u53d1\u751f\u547d\u540d\u51b2\u7a81\u3002 \u4f5c\u8005\u6ce8 \u8fd9\u4e9b\u6559\u7a0b\u4e2d\u7684\u793a\u4f8b\u901a\u5e38\u4e0d\u4f1a\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\uff0c\u9664\u975e\u6211\u4eec\u8981\u8bf4\u660e\u547d\u540d\u7a7a\u95f4\u7684\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\uff0c\u4ee5\u4fdd\u6301\u793a\u4f8b\u7684\u7b80\u6d01\u6027\u3002 \u4efb\u4f55\u5c06\u53d1\u5e03\u7ed9\u4ed6\u4eba\u7684\u4ee3\u7801\u90fd\u5fc5\u987b\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\uff0c\u4ee5\u9632\u6b62\u4e0e\u96c6\u6210\u5230\u5176\u4e2d\u7684\u4ee3\u7801\u53d1\u751f\u51b2\u7a81\u3002\u901a\u5e38\u4e00\u4e2a\u9876\u7ea7\u547d\u540d\u7a7a\u95f4\u5c31\u8db3\u591f\u4e86\uff08\u4f8b\u5982 Foologger \uff09\u3002\u5c06\u5e93\u4ee3\u7801\u7f6e\u4e8e\u547d\u540d\u7a7a\u95f4\u5185\u8fd8\u6709\u4e00\u4e2a\u597d\u5904\uff0c\u5373\u7528\u6237\u53ef\u4ee5\u4f7f\u7528\u7f16\u8f91\u5668\u7684\u81ea\u52a8\u5b8c\u6210\u548c\u5efa\u8bae\u529f\u80fd\u67e5\u770b\u5e93\u7684\u5185\u5bb9\uff08\u4f8b\u5982\uff0c\u5982\u679c\u8f93\u5165 Foologger \uff0c\u81ea\u52a8\u5b8c\u6210\u529f\u80fd\u4f1a\u663e\u793a Foologger \u4e2d\u7684\u6240\u6709\u540d\u79f0\uff09\u3002 \u5728\u591a\u56e2\u961f\u7ec4\u7ec7\u4e2d\uff0c\u4e3a\u4e86\u9632\u6b62\u4e0d\u540c\u56e2\u961f\u751f\u6210\u7684\u4ee3\u7801\u4e4b\u95f4\u51fa\u73b0\u547d\u540d\u51b2\u7a81\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528\u4e24\u7ea7\u751a\u81f3\u4e09\u7ea7\u547d\u540d\u7a7a\u95f4\u3002\u8fd9\u4e9b\u547d\u540d\u7a7a\u95f4\u901a\u5e38\u91c7\u7528\u4ee5\u4e0b\u5f62\u5f0f\u4e4b\u4e00\uff1a \u9879\u76ee\u6216\u5e93 :: \u6a21\u5757\uff08\u4f8b\u5982 Foologger::Lang \uff09 \u516c\u53f8\u6216\u7ec4\u7ec7 :: \u9879\u76ee\u6216\u5e93\uff08\u4f8b\u5982 Foosoft::Foologger \uff09 \u516c\u53f8\u6216\u7ec4\u7ec7 :: \u9879\u76ee\u6216\u5e93 :: \u6a21\u5757\uff08\u4f8b\u5982 Foosoft::Foologger::Lang \uff09 \u4f7f\u7528\u6a21\u5757\u7ea7\u547d\u540d\u7a7a\u95f4\u6709\u52a9\u4e8e\u5c06\u65e5\u540e\u53ef\u80fd\u91cd\u7528\u7684\u4ee3\u7801\u4e0e\u4e0d\u53ef\u91cd\u7528\u7684\u7279\u5b9a\u5e94\u7528\u7a0b\u5e8f\u4ee3\u7801\u533a\u5206\u5f00\u6765\u3002\u4f8b\u5982\uff0c\u7269\u7406\u548c\u6570\u5b66\u51fd\u6570\u53ef\u4ee5\u653e\u5728\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\uff08\u5982 Math:: \uff09\u3002\u8bed\u8a00\u548c\u672c\u5730\u5316\u529f\u80fd\u53ef\u4ee5\u653e\u5728\u53e6\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\uff08\u5982 Lang:: \uff09\u3002\u4e0d\u8fc7\uff0c\u76ee\u5f55\u7ed3\u6784\u4e5f\u53ef\u7528\u4e8e\u6b64\u76ee\u7684\uff08\u7279\u5b9a\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\u4ee3\u7801\u653e\u5728\u9879\u76ee\u76ee\u5f55\u6811\u4e2d\uff0c\u53ef\u91cd\u590d\u4f7f\u7528\u7684\u4ee3\u7801\u653e\u5728\u5355\u72ec\u7684\u5171\u4eab\u76ee\u5f55\u6811\u4e2d\uff09\u3002 \u4e00\u822c\u6765\u8bf4\uff0c\u5e94\u907f\u514d\u6df1\u5ea6\u5d4c\u5957\u547d\u540d\u7a7a\u95f4\uff08\u8d85\u8fc7 3 \u5c42\uff09\u3002","title":"\u5982\u4f55\u4f7f\u7528\u547d\u540d\u7a7a\u95f4"},{"location":"6-3-Local-variables/","tags":["local variables","storage duration","linkage"],"text":"Key Takeaway \u51fd\u6570\u53c2\u6570\u4e5f\u662f\u5c40\u90e8\u53d8\u91cf \u6807\u8bc6\u7b26\u6709\u591a\u79cd\u5c5e\u6027\uff1a\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u3001\u751f\u547d\u5468\u671f\u3001\u94fe\u63a5 \u5c40\u90e8\u53d8\u91cf\u7684\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u548c\u5b83\u7684\u751f\u547d\u5468\u671f\u662f\u4e00\u6837\u7684\uff0c\u5b83\u5177\u6709\u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\uff0c\u6240\u4ee5\u4e5f\u79f0\u4e3a \u81ea\u52a8\u53d8\u91cf \u5c40\u90e8\u53d8\u91cf\u7684\u94fe\u63a5\u5c5e\u6027\u4e3a\u65e0\u94fe\u63a5\uff0c\u6bcf\u4e2a\u58f0\u660e\u90fd\u6307\u4ee3\u7684\u662f\u4e00\u4e2a\u552f\u4e00\u7684\u5bf9\u8c61 \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u6027\u770b\u4e0a\u53bb\u6709\u70b9\u7c7b\u4f3c\u3002\u5176\u5b9e\u4e0d\u7136\uff0c\u4f5c\u7528\u57df\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5355\u72ec\u7684\u58f0\u660e\u5728\u54ea\u91cc\u53ef\u4ee5\u88ab\u8bbf\u95ee\uff0c\u800c\u94fe\u63a5\u6027\u5b9a\u4e49\u4e86\u591a\u4e2a\u58f0\u660e\u662f\u5426\u6307\u4ee3\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002 \u53d8\u91cf\u5e94\u8be5\u88ab\u5b9a\u4e49\u5728\u5c3d\u53ef\u80fd\u5c0f\u7684\u4f5c\u7528\u57df\u4e2d \u4f7f\u7528\u4e00\u4e2a\u4e13\u95e8\u7684\u8bed\u53e5\u5757\u6765\u9650\u5236\u4e00\u4e2a\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u662f\u6ca1\u5fc5\u8981\u7684\uff0c\u4e0d\u8981\u8fd9\u6837\u505a\u3002\u5982\u679c\u4e00\u5b9a\u6709\u5fc5\u8981\uff0c\u6700\u597d\u8fd8\u662f\u63d0\u70bc\u6210\u4e00\u4e2a\u5355\u72ec\u7684\u51fd\u6570 \u5728 2.5 - \u5c40\u90e8\u4f5c\u7528\u57df \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5c40\u90e8\u53d8\u91cf\u2014\u2014\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8\u7684\u53d8\u91cf\uff08\u5305\u62ec\u51fd\u6570\u53c2\u6570\uff09\u3002 C++\u4e2d\u5e76\u6ca1\u6709\u4e00\u4e2a\u4e13\u95e8\u5c5e\u6027\u80fd\u591f\u5c06\u53d8\u91cf\u5b9a\u4e49\u4e3a\u5c40\u90e8\u53d8\u91cf\u3002\u5c40\u90e8\u53d8\u91cf\u533a\u522b\u4e8e\u5176\u4ed6\u53d8\u91cf\u7684\u5c5e\u6027\u6709\u5f88\u591a\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u8fd9\u4e9b\u4e0d\u540c\u7684\u5c5e\u6027\u3002 \u5728 2.5 - \u5c40\u90e8\u4f5c\u7528\u57df \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86 \u4f5c\u7528\u57df \u7684\u6982\u5ff5\u3002\u4e00\u4e2a \u6807\u8bc6\u7b26 \u7684\u4f5c\u7528\u57df\u6307\u7684\u662f\u5b83\u53ef\u4ee5\u5728\u6e90\u4ee3\u7801\u4e2d\u88ab\u8bbf\u95ee\u7684\u8303\u56f4\u3002\u5f53\u4e00\u4e2a\u6807\u8bc6\u7b26\u53ef\u4ee5\u88ab\u8bbf\u95ee\u65f6\uff0c\u6211\u4eec\u79f0\u5176\u4e3a \u5728\u4f5c\u7528\u57df\u4e2d \u3002\u800c\u5f53\u4e00\u4e2a\u6807\u8bc6\u7b26\u4e0d\u53ef\u4ee5\u88ab\u8bbf\u95ee\u65f6\uff0c\u6211\u4eec\u79f0\u5176 \u8d85\u51fa\u4f5c\u7528\u57df \u3002\u4f5c\u7528\u57df\u662f\u4e00\u4e2a \u7f16\u8bd1\u65f6 \u5c5e\u6027\uff0c\u5f53\u6807\u8bc6\u7b26\u8d85\u51fa\u4f5c\u7528\u57df\u65f6\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 \u5c40\u90e8\u53d8\u91cf\u7684\u5757\u4f5c\u7528\u57df \u5c40\u90e8\u53d8\u91cf\u5177\u6709 \u5757\u4f5c\u7528\u57df \uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4eec\u7684\u4f5c\u7528\u57df\u88ab\u9650\u5b9a\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u4ee3\u7801\u5757\u4e2d\u3002 \u76f8\u5173\u5185\u5bb9 \u5982\u679c\u4f60\u5fd8\u8bb0\u4e86\u4ec0\u4e48\u662f\u4ee3\u7801\u5757\uff0c\u8bf7\u53c2\u8003 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u3002 1 2 3 4 5 6 7 int main () { int i { 5 }; // i enters scope here double d { 4.0 }; // d enters scope here return 0 ; } // i and d go out of scope here \u5c3d\u7ba1\u51fd\u6570\u7684 \u5f62\u53c2 \u5e76\u6ca1\u6709\u88ab\u5b9a\u4e49\u5728\u51fd\u6570\u4f53\u5185\uff0c\u901a\u5e38\u51fd\u6570\u4ecd\u5c06\u5176\u770b\u505a\u662f\u51fd\u6570\u4f53\u4ee3\u7801\u5757\u7684\u4e00\u90e8\u5206\u3002 1 2 3 4 5 6 7 int max ( int x , int y ) // x and y enter scope here { // assign the greater of x or y to max int max { ( x > y ) ? x : y }; // max enters scope here return max ; } // x, y, and max leave scope here \u4f5c\u7528\u57df\u4e2d\u7684\u6240\u6709\u53d8\u91cf\u5fc5\u987b\u5177\u5907\u4e0d\u540c\u7684\u53d8\u91cf\u540d \u5728\u7ed9\u5b9a\u4f5c\u7528\u57df\u4e2d\uff0c\u53d8\u91cf\u7684\u540d\u5b57\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u5426\u5219\u5728\u5f15\u7528\u53d8\u91cf\u7684\u65f6\u5019\u5c31\u4f1a\u51fa\u73b0\u6b67\u4e49\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 void someFunction ( int x ) { int x {}; // compilation failure due to name collision with function parameter } int main () { return 0 ; } \u7531\u4e8e x \u88ab\u5b9a\u4e49\u5728\u51fd\u6570\u4f53\u5185\uff0c\u4e14\u540c\u65f6\u51fd\u6570\u7684\u5f62\u53c2\u4e5f\u662f x \uff0c\u8fd9\u5c31\u9020\u6210\u4e86\u5757\u4f5c\u7528\u57df\u4e2d\u51fa\u73b0\u4e86\u540c\u540d\u7684\u53d8\u91cf\uff0c\u56e0\u6b64\u7a0b\u5e8f\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\u3002 \u5c40\u90e8\u53d8\u91cf\u7684\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u662f\u81ea\u52a8\u7684 \u4e00\u4e2a\u53d8\u91cf\u7684 \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff08\u4e5f\u79f0\u4e3a\u6301\u7eed\u65f6\u95f4\uff09\u51b3\u5b9a\u4e86\u8be5\u53d8\u91cf\u88ab\u521b\u5efa\u548c\u9500\u6bc1\u5e94\u8be5\u9075\u5faa\u7684\u89c4\u5219\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u53d8\u91cf\u7684\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u6709\u5b83\u7684\u751f\u547d\u5468\u671f\u76f4\u63a5\u51b3\u5b9a\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u5728 2.5 - \u5c40\u90e8\u4f5c\u7528\u57df \u4e2d\u63a2\u8ba8\u4e86\u751f\u547d\u5468\u671f\u3002 \u5c40\u90e8\u53d8\u91cf\u5177\u6709\u81ea\u52a8\u7684\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\uff0c\u610f\u5473\u7740\u5b83\u4f1a\u5728\u5b9a\u4e49\u65f6\u88ab\u521b\u5efa\u5e76\u4e14\u5728\u8bed\u53e5\u5757\u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 int main () { int i { 5 }; // i created and initialized here double d { 4.0 }; // d created and initialized here return 0 ; } // i and d are destroyed here \u6b63\u56e0\u4e3a\u6b64\uff0c\u5c40\u90e8\u53d8\u91cf\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a\u81ea\u52a8\u53d8\u91cf\u3002 \u5d4c\u5957\u8bed\u53e5\u5757\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf \u5c40\u90e8\u53d8\u91cf\u53ef\u4ee5\u4f4d\u4e8e\u5d4c\u5957\u7684\u5757\u4e2d\u3002\u8fd9\u79cd\u573a\u666f\u548c\u51fd\u6570\u4f53\u5185\u7684\u5c40\u90e8\u53d8\u91cf\u6ca1\u6709\u4efb\u4f55\u533a\u522b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int main () // outer block { int x { 5 }; // x enters scope and is created here { // nested block int y { 7 }; // y enters scope and is created here } // y goes out of scope and is destroyed here // y can not be used here because it is out of scope in this block return 0 ; } // x goes out of scope and is destroyed here \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf y \u88ab\u5b9a\u4e49\u5728\u4e00\u4e2a\u5d4c\u5957\u7684\u5757\u4e2d\uff0c\u5b83\u7684\u4f5c\u7528\u57df\u8303\u56f4\u4ece\u5b9a\u4e49\u5f00\u59cb\u5230\u5d4c\u5957\u5757\u7ed3\u675f\u65f6\u7ed3\u675f\uff0c\u5b83\u7684\u751f\u547d\u5468\u671f\u4e5f\u662f\u4e00\u6837\u7684\u3002\u56e0\u6b64\u53d8\u91cf y \u7684\u4f5c\u7528\u57df\u88ab\u9650\u5236\u5728\u4e86\u5757\u7684\u5185\u90e8\uff0c\u6240\u4ee5\u5728\u5bf9\u5e94\u4ee3\u7801\u5757\u7684\u5916\u90e8\u662f\u4e0d\u80fd\u591f\u8bbf\u95ee\u8be5\u53d8\u91cf\u3002 \u9700\u8981\u6ce8\u610f\u7684\uff0c\u5d4c\u5957\u5757\u88ab\u770b\u505a\u662f\u5b83\u5916\u5c42\u5757\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u5b9a\u4e49\u5728\u5916\u5c42\u5757\u4e2d\u7684\u53d8\u91cf\u53ef\u4ee5\u5728\u5d4c\u5957\u5757\u4e2d\u8bbf\u95ee\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { // outer block int x { 5 }; // x enters scope and is created here { // nested block int y { 7 }; // y enters scope and is created here // x and y are both in scope here std :: cout << x << \" + \" << y << \" = \" << x + y << '\\n' ; } // y goes out of scope and is destroyed here // y can not be used here because it is out of scope in this block return 0 ; } // x goes out of scope and is destroyed here \u5c40\u90e8\u53d8\u91cf\u7684\u94fe\u63a5\u6027 \u6807\u8bc6\u7b26\u8fd8\u6709\u53e6\u5916\u7684\u4e00\u4e2a\u5c5e\u6027\u2014\u2014 \u94fe\u63a5\u6027(linkage) \u3002\u6807\u8bc6\u7b26\u7684\u94fe\u63a5\u6027\u51b3\u5b9a\u4e86\u76f8\u540c\u540d\u5b57\u7684\u58f0\u660e\u662f\u5426\u6307\u4ee3\u7684\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002 \u5c40\u90e8\u53d8\u91cf\u6ca1\u6709\u94fe\u63a5\uff08 no linkage \uff09\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u6bcf\u4e2a\u58f0\u660e\u90fd\u6307\u4ee3\u7684\u662f\u4e00\u4e2a\u552f\u4e00\u7684\u5bf9\u8c61\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { int x { 2 }; // \u5c40\u90e8\u53d8\u91cf\uff0c\u6ca1\u6709\u94fe\u63a5\u6027 { int x { 3 }; // \u8fd9\u91cc\u7684 x \u8868\u793a\u7684\u662f\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61 } return 0 ; } \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u6027\u770b\u4e0a\u53bb\u6709\u70b9\u7c7b\u4f3c\u3002\u5176\u5b9e\u4e0d\u7136\uff0c\u4f5c\u7528\u57df\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5355\u72ec\u7684\u58f0\u660e\u5728\u54ea\u91cc\u53ef\u4ee5\u88ab\u8bbf\u95ee\uff0c\u800c\u94fe\u63a5\u6027\u5b9a\u4e49\u4e86\u591a\u4e2a\u58f0\u660e\u662f\u5426\u6307\u4ee3\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 6.5 - \u53d8\u91cf\u906e\u853d \u4e2d\u8ba8\u8bba\u5177\u4e00\u4e2a\u53d8\u91cf\u4e0e\u5176\u5d4c\u5957\u8bed\u53e5\u5757\u4e2d\u7684\u540c\u540d\u53d8\u91cf\u4e4b\u95f4\u6709\u4ec0\u4e48\u5f71\u54cd\u3002 \u5c40\u90e8\u53d8\u91cf\u7684\u94fe\u63a5\u6027\u6ca1\u4ec0\u4e48\u597d\u8bb2\u7684\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u5bf9\u94fe\u63a5\u6027\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002 \u53d8\u91cf\u5e94\u8be5\u88ab\u5b9a\u4e49\u5728\u5c3d\u53ef\u80fd\u5c0f\u7684\u4f5c\u7528\u57df\u4e2d \u5982\u679c\u53d8\u91cf\u53ea\u5728\u67d0\u4e2a\u5d4c\u5957\u5757\u4e2d\u4f7f\u7528\uff0c\u90a3\u4e48\u5b83\u5c31\u5e94\u8be5\u88ab\u5b9a\u4e49\u5728\u5d4c\u5957\u5757\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { // do not define y here { // y is only used inside this block, so define it here int y { 5 }; std :: cout << y << '\\n' ; } // otherwise y could still be used here, where it's not needed return 0 ; } \u901a\u8fc7\u9650\u5236\u53d8\u91cf\u7684\u4f5c\u7528\u57df\uff0c\u4f60\u53ef\u4ee5\u51cf\u5c0f\u7a0b\u5e8f\u7684\u590d\u6742\u5ea6\uff0c\u56e0\u4e3a\u8fd9\u6837\u53ef\u4ee5\u51cf\u5c11\u6d3b\u52a8\u53d8\u91cf\u7684\u6570\u91cf\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u8fd9\u6837\u8fd8\u80fd\u66f4\u597d\u7684\u770b\u51fa\u53d8\u91cf\u5728\u54ea\u91cc\u88ab\u4f7f\u7528\u4e86\uff08\u6216\u6ca1\u88ab\u4f7f\u7528\uff09\u3002\u5b9a\u4e49\u5728\u4e00\u4e2a\u5757\u4e2d\u7684\u53d8\u91cf\u53ea\u80fd\u591f\u5728\u8fd9\u4e2a\u5757\uff08\u53ca\u5176\u5d4c\u5957\u5757\uff09\u4e2d\u88ab\u4f7f\u7528\u3002\u8fd9\u4f7f\u5f97\u7a0b\u5e8f\u4e5f\u66f4\u5bb9\u6613\u7406\u89e3\u3002 \u5982\u679c\u5916\u5c42\u5757\u9700\u8981\u4f7f\u7528\u53d8\u91cf\uff0c\u90a3\u4e48\u5b83\u9700\u8981\u5728\u5916\u5c42\u5757\u4e2d\u58f0\u660e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { int y { 5 }; // \u56e0\u4e3a\u5916\u5c42\u5757\u9700\u8981\u4f7f\u7528 y\uff0c\u5219 y \u8981\u88ab\u5b9a\u4e49\u5728\u5916\u5c42 { int x {}; std :: cin >> x ; // if we declared y here, immediately before its actual first use... if ( x == 4 ) y = 4 ; } // ... it would be destroyed here std :: cout << y ; // and we need y to exist here return 0 ; } \u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\u8fd8\u5c55\u793a\u4e86\u4e00\u4e2a\u4f8b\u5916\u60c5\u51b5\uff0c\u5c31\u662f\u53d8\u91cf\u5728\u5b83\u88ab\u4f7f\u7528\u524d\uff0c\u65e9\u65e9\u5730\u5c31\u58f0\u660e\u4e86\uff08\u53c2\u89c1\uff1a 2-5-Introduction-to-local-scope#\u5728\u4f55\u5904\u5b9a\u4e49\u5c40\u90e8\u53d8\u91cf \uff09 \u65b0\u624b\u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u8003\u8651\u662f\u5426\u6709\u5fc5\u8981\u6545\u610f\u521b\u5efa\u4e00\u4e2a\u5d4c\u5957\u5757\u6765\u9650\u5236\u53d8\u91cf\u7684\u4f5c\u7528\u57df\uff08\u5f3a\u5236\u5c06\u5176\u53ca\u65e9\u5730 \u8d85\u51fa\u4f5c\u7528\u57df \u6216\u9500\u6bc1\uff09\u3002\u8fd9\u4e48\u505a\u7684\u786e\u53ef\u4ee5\u4f7f\u53d8\u91cf\u66f4\u7b80\u5355\uff0c\u4f46\u662f\u51fd\u6570\u603b\u4f53\u4e0a\u6765\u8bb2\u53d8\u5f97\u66f4\u957f\u3001\u590d\u6742\u5ea6\u4e5f\u53d8\u9ad8\u4e86\u3002\u8fd9\u4e48\u505a\u901a\u5e38\u6765\u8bf4\u662f\u4e0d\u5212\u7b97\u7684\u3002\u5982\u679c\u7684\u786e\u9700\u8981\u6545\u610f\u521b\u5efa\u4e00\u4e2a\u8bed\u53e5\u5757\u6765\u9650\u5236\u53d8\u91cf\u7684\u4f5c\u7528\u57df\uff0c\u901a\u5e38\u6700\u597d\u7684\u529e\u6cd5\u8fd8\u662f\u628a\u5b83\u653e\u5230\u4e00\u4e2a\u5355\u72ec\u7684\u51fd\u6570\u4e2d\u3002 \u6700\u4f73\u5b9e\u8df5 \u53d8\u91cf\u5e94\u8be5\u88ab\u5b9a\u4e49\u5728\u5c3d\u53ef\u80fd\u5c0f\u7684\u4f5c\u7528\u57df\u4e2d\u3002\u4f7f\u7528\u4e00\u4e2a\u4e13\u95e8\u7684\u8bed\u53e5\u5757\u6765\u9650\u5236\u4e00\u4e2a\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u662f\u6ca1\u5fc5\u8981\u7684\uff0c\u4e0d\u8981\u8fd9\u6837\u505a\u3002","title":"6.3 - \u5c40\u90e8\u53d8\u91cf"},{"location":"6-3-Local-variables/#_1","text":"\u5c40\u90e8\u53d8\u91cf\u5177\u6709 \u5757\u4f5c\u7528\u57df \uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4eec\u7684\u4f5c\u7528\u57df\u88ab\u9650\u5b9a\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u4ee3\u7801\u5757\u4e2d\u3002 \u76f8\u5173\u5185\u5bb9 \u5982\u679c\u4f60\u5fd8\u8bb0\u4e86\u4ec0\u4e48\u662f\u4ee3\u7801\u5757\uff0c\u8bf7\u53c2\u8003 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u3002 1 2 3 4 5 6 7 int main () { int i { 5 }; // i enters scope here double d { 4.0 }; // d enters scope here return 0 ; } // i and d go out of scope here \u5c3d\u7ba1\u51fd\u6570\u7684 \u5f62\u53c2 \u5e76\u6ca1\u6709\u88ab\u5b9a\u4e49\u5728\u51fd\u6570\u4f53\u5185\uff0c\u901a\u5e38\u51fd\u6570\u4ecd\u5c06\u5176\u770b\u505a\u662f\u51fd\u6570\u4f53\u4ee3\u7801\u5757\u7684\u4e00\u90e8\u5206\u3002 1 2 3 4 5 6 7 int max ( int x , int y ) // x and y enter scope here { // assign the greater of x or y to max int max { ( x > y ) ? x : y }; // max enters scope here return max ; } // x, y, and max leave scope here","title":"\u5c40\u90e8\u53d8\u91cf\u7684\u5757\u4f5c\u7528\u57df"},{"location":"6-3-Local-variables/#_2","text":"\u5728\u7ed9\u5b9a\u4f5c\u7528\u57df\u4e2d\uff0c\u53d8\u91cf\u7684\u540d\u5b57\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u5426\u5219\u5728\u5f15\u7528\u53d8\u91cf\u7684\u65f6\u5019\u5c31\u4f1a\u51fa\u73b0\u6b67\u4e49\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 void someFunction ( int x ) { int x {}; // compilation failure due to name collision with function parameter } int main () { return 0 ; } \u7531\u4e8e x \u88ab\u5b9a\u4e49\u5728\u51fd\u6570\u4f53\u5185\uff0c\u4e14\u540c\u65f6\u51fd\u6570\u7684\u5f62\u53c2\u4e5f\u662f x \uff0c\u8fd9\u5c31\u9020\u6210\u4e86\u5757\u4f5c\u7528\u57df\u4e2d\u51fa\u73b0\u4e86\u540c\u540d\u7684\u53d8\u91cf\uff0c\u56e0\u6b64\u7a0b\u5e8f\u662f\u65e0\u6cd5\u7f16\u8bd1\u7684\u3002","title":"\u4f5c\u7528\u57df\u4e2d\u7684\u6240\u6709\u53d8\u91cf\u5fc5\u987b\u5177\u5907\u4e0d\u540c\u7684\u53d8\u91cf\u540d"},{"location":"6-3-Local-variables/#_3","text":"\u4e00\u4e2a\u53d8\u91cf\u7684 \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff08\u4e5f\u79f0\u4e3a\u6301\u7eed\u65f6\u95f4\uff09\u51b3\u5b9a\u4e86\u8be5\u53d8\u91cf\u88ab\u521b\u5efa\u548c\u9500\u6bc1\u5e94\u8be5\u9075\u5faa\u7684\u89c4\u5219\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u53d8\u91cf\u7684\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u6709\u5b83\u7684\u751f\u547d\u5468\u671f\u76f4\u63a5\u51b3\u5b9a\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u5728 2.5 - \u5c40\u90e8\u4f5c\u7528\u57df \u4e2d\u63a2\u8ba8\u4e86\u751f\u547d\u5468\u671f\u3002 \u5c40\u90e8\u53d8\u91cf\u5177\u6709\u81ea\u52a8\u7684\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\uff0c\u610f\u5473\u7740\u5b83\u4f1a\u5728\u5b9a\u4e49\u65f6\u88ab\u521b\u5efa\u5e76\u4e14\u5728\u8bed\u53e5\u5757\u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 int main () { int i { 5 }; // i created and initialized here double d { 4.0 }; // d created and initialized here return 0 ; } // i and d are destroyed here \u6b63\u56e0\u4e3a\u6b64\uff0c\u5c40\u90e8\u53d8\u91cf\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a\u81ea\u52a8\u53d8\u91cf\u3002","title":"\u5c40\u90e8\u53d8\u91cf\u7684\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u662f\u81ea\u52a8\u7684"},{"location":"6-3-Local-variables/#_4","text":"\u5c40\u90e8\u53d8\u91cf\u53ef\u4ee5\u4f4d\u4e8e\u5d4c\u5957\u7684\u5757\u4e2d\u3002\u8fd9\u79cd\u573a\u666f\u548c\u51fd\u6570\u4f53\u5185\u7684\u5c40\u90e8\u53d8\u91cf\u6ca1\u6709\u4efb\u4f55\u533a\u522b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int main () // outer block { int x { 5 }; // x enters scope and is created here { // nested block int y { 7 }; // y enters scope and is created here } // y goes out of scope and is destroyed here // y can not be used here because it is out of scope in this block return 0 ; } // x goes out of scope and is destroyed here \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf y \u88ab\u5b9a\u4e49\u5728\u4e00\u4e2a\u5d4c\u5957\u7684\u5757\u4e2d\uff0c\u5b83\u7684\u4f5c\u7528\u57df\u8303\u56f4\u4ece\u5b9a\u4e49\u5f00\u59cb\u5230\u5d4c\u5957\u5757\u7ed3\u675f\u65f6\u7ed3\u675f\uff0c\u5b83\u7684\u751f\u547d\u5468\u671f\u4e5f\u662f\u4e00\u6837\u7684\u3002\u56e0\u6b64\u53d8\u91cf y \u7684\u4f5c\u7528\u57df\u88ab\u9650\u5236\u5728\u4e86\u5757\u7684\u5185\u90e8\uff0c\u6240\u4ee5\u5728\u5bf9\u5e94\u4ee3\u7801\u5757\u7684\u5916\u90e8\u662f\u4e0d\u80fd\u591f\u8bbf\u95ee\u8be5\u53d8\u91cf\u3002 \u9700\u8981\u6ce8\u610f\u7684\uff0c\u5d4c\u5957\u5757\u88ab\u770b\u505a\u662f\u5b83\u5916\u5c42\u5757\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u5b9a\u4e49\u5728\u5916\u5c42\u5757\u4e2d\u7684\u53d8\u91cf\u53ef\u4ee5\u5728\u5d4c\u5957\u5757\u4e2d\u8bbf\u95ee\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { // outer block int x { 5 }; // x enters scope and is created here { // nested block int y { 7 }; // y enters scope and is created here // x and y are both in scope here std :: cout << x << \" + \" << y << \" = \" << x + y << '\\n' ; } // y goes out of scope and is destroyed here // y can not be used here because it is out of scope in this block return 0 ; } // x goes out of scope and is destroyed here","title":"\u5d4c\u5957\u8bed\u53e5\u5757\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf"},{"location":"6-3-Local-variables/#_5","text":"\u6807\u8bc6\u7b26\u8fd8\u6709\u53e6\u5916\u7684\u4e00\u4e2a\u5c5e\u6027\u2014\u2014 \u94fe\u63a5\u6027(linkage) \u3002\u6807\u8bc6\u7b26\u7684\u94fe\u63a5\u6027\u51b3\u5b9a\u4e86\u76f8\u540c\u540d\u5b57\u7684\u58f0\u660e\u662f\u5426\u6307\u4ee3\u7684\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002 \u5c40\u90e8\u53d8\u91cf\u6ca1\u6709\u94fe\u63a5\uff08 no linkage \uff09\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u6bcf\u4e2a\u58f0\u660e\u90fd\u6307\u4ee3\u7684\u662f\u4e00\u4e2a\u552f\u4e00\u7684\u5bf9\u8c61\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { int x { 2 }; // \u5c40\u90e8\u53d8\u91cf\uff0c\u6ca1\u6709\u94fe\u63a5\u6027 { int x { 3 }; // \u8fd9\u91cc\u7684 x \u8868\u793a\u7684\u662f\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61 } return 0 ; } \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u6027\u770b\u4e0a\u53bb\u6709\u70b9\u7c7b\u4f3c\u3002\u5176\u5b9e\u4e0d\u7136\uff0c\u4f5c\u7528\u57df\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5355\u72ec\u7684\u58f0\u660e\u5728\u54ea\u91cc\u53ef\u4ee5\u88ab\u8bbf\u95ee\uff0c\u800c\u94fe\u63a5\u6027\u5b9a\u4e49\u4e86\u591a\u4e2a\u58f0\u660e\u662f\u5426\u6307\u4ee3\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 6.5 - \u53d8\u91cf\u906e\u853d \u4e2d\u8ba8\u8bba\u5177\u4e00\u4e2a\u53d8\u91cf\u4e0e\u5176\u5d4c\u5957\u8bed\u53e5\u5757\u4e2d\u7684\u540c\u540d\u53d8\u91cf\u4e4b\u95f4\u6709\u4ec0\u4e48\u5f71\u54cd\u3002 \u5c40\u90e8\u53d8\u91cf\u7684\u94fe\u63a5\u6027\u6ca1\u4ec0\u4e48\u597d\u8bb2\u7684\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u5bf9\u94fe\u63a5\u6027\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002","title":"\u5c40\u90e8\u53d8\u91cf\u7684\u94fe\u63a5\u6027"},{"location":"6-3-Local-variables/#_6","text":"\u5982\u679c\u53d8\u91cf\u53ea\u5728\u67d0\u4e2a\u5d4c\u5957\u5757\u4e2d\u4f7f\u7528\uff0c\u90a3\u4e48\u5b83\u5c31\u5e94\u8be5\u88ab\u5b9a\u4e49\u5728\u5d4c\u5957\u5757\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { // do not define y here { // y is only used inside this block, so define it here int y { 5 }; std :: cout << y << '\\n' ; } // otherwise y could still be used here, where it's not needed return 0 ; } \u901a\u8fc7\u9650\u5236\u53d8\u91cf\u7684\u4f5c\u7528\u57df\uff0c\u4f60\u53ef\u4ee5\u51cf\u5c0f\u7a0b\u5e8f\u7684\u590d\u6742\u5ea6\uff0c\u56e0\u4e3a\u8fd9\u6837\u53ef\u4ee5\u51cf\u5c11\u6d3b\u52a8\u53d8\u91cf\u7684\u6570\u91cf\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u8fd9\u6837\u8fd8\u80fd\u66f4\u597d\u7684\u770b\u51fa\u53d8\u91cf\u5728\u54ea\u91cc\u88ab\u4f7f\u7528\u4e86\uff08\u6216\u6ca1\u88ab\u4f7f\u7528\uff09\u3002\u5b9a\u4e49\u5728\u4e00\u4e2a\u5757\u4e2d\u7684\u53d8\u91cf\u53ea\u80fd\u591f\u5728\u8fd9\u4e2a\u5757\uff08\u53ca\u5176\u5d4c\u5957\u5757\uff09\u4e2d\u88ab\u4f7f\u7528\u3002\u8fd9\u4f7f\u5f97\u7a0b\u5e8f\u4e5f\u66f4\u5bb9\u6613\u7406\u89e3\u3002 \u5982\u679c\u5916\u5c42\u5757\u9700\u8981\u4f7f\u7528\u53d8\u91cf\uff0c\u90a3\u4e48\u5b83\u9700\u8981\u5728\u5916\u5c42\u5757\u4e2d\u58f0\u660e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { int y { 5 }; // \u56e0\u4e3a\u5916\u5c42\u5757\u9700\u8981\u4f7f\u7528 y\uff0c\u5219 y \u8981\u88ab\u5b9a\u4e49\u5728\u5916\u5c42 { int x {}; std :: cin >> x ; // if we declared y here, immediately before its actual first use... if ( x == 4 ) y = 4 ; } // ... it would be destroyed here std :: cout << y ; // and we need y to exist here return 0 ; } \u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\u8fd8\u5c55\u793a\u4e86\u4e00\u4e2a\u4f8b\u5916\u60c5\u51b5\uff0c\u5c31\u662f\u53d8\u91cf\u5728\u5b83\u88ab\u4f7f\u7528\u524d\uff0c\u65e9\u65e9\u5730\u5c31\u58f0\u660e\u4e86\uff08\u53c2\u89c1\uff1a 2-5-Introduction-to-local-scope#\u5728\u4f55\u5904\u5b9a\u4e49\u5c40\u90e8\u53d8\u91cf \uff09 \u65b0\u624b\u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u8003\u8651\u662f\u5426\u6709\u5fc5\u8981\u6545\u610f\u521b\u5efa\u4e00\u4e2a\u5d4c\u5957\u5757\u6765\u9650\u5236\u53d8\u91cf\u7684\u4f5c\u7528\u57df\uff08\u5f3a\u5236\u5c06\u5176\u53ca\u65e9\u5730 \u8d85\u51fa\u4f5c\u7528\u57df \u6216\u9500\u6bc1\uff09\u3002\u8fd9\u4e48\u505a\u7684\u786e\u53ef\u4ee5\u4f7f\u53d8\u91cf\u66f4\u7b80\u5355\uff0c\u4f46\u662f\u51fd\u6570\u603b\u4f53\u4e0a\u6765\u8bb2\u53d8\u5f97\u66f4\u957f\u3001\u590d\u6742\u5ea6\u4e5f\u53d8\u9ad8\u4e86\u3002\u8fd9\u4e48\u505a\u901a\u5e38\u6765\u8bf4\u662f\u4e0d\u5212\u7b97\u7684\u3002\u5982\u679c\u7684\u786e\u9700\u8981\u6545\u610f\u521b\u5efa\u4e00\u4e2a\u8bed\u53e5\u5757\u6765\u9650\u5236\u53d8\u91cf\u7684\u4f5c\u7528\u57df\uff0c\u901a\u5e38\u6700\u597d\u7684\u529e\u6cd5\u8fd8\u662f\u628a\u5b83\u653e\u5230\u4e00\u4e2a\u5355\u72ec\u7684\u51fd\u6570\u4e2d\u3002 \u6700\u4f73\u5b9e\u8df5 \u53d8\u91cf\u5e94\u8be5\u88ab\u5b9a\u4e49\u5728\u5c3d\u53ef\u80fd\u5c0f\u7684\u4f5c\u7528\u57df\u4e2d\u3002\u4f7f\u7528\u4e00\u4e2a\u4e13\u95e8\u7684\u8bed\u53e5\u5757\u6765\u9650\u5236\u4e00\u4e2a\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u662f\u6ca1\u5fc5\u8981\u7684\uff0c\u4e0d\u8981\u8fd9\u6837\u505a\u3002","title":"\u53d8\u91cf\u5e94\u8be5\u88ab\u5b9a\u4e49\u5728\u5c3d\u53ef\u80fd\u5c0f\u7684\u4f5c\u7528\u57df\u4e2d"},{"location":"6-4-Introduction-to-global-variables/","tags":["global","storage duration"],"text":"Key Takeaway \u8003\u8651\u4e3a\u5168\u5c40\u53d8\u91cf\u6dfb\u52a0 \u201cg\u201d or \u201cg_\u201d \u524d\u7f00\u4ee5\u533a\u522b\u4e8e\u5176\u4ed6\u53d8\u91cf\u3002 \u5168\u5c40\u53d8\u91cf\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\u548c\u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u5168\u5c40\u53d8\u91cf\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u88ab\u521b\u5efa\uff0c\u5e76\u4e14\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u2014\u2014\u5373 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u5177\u6709\u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u7684\u53d8\u91cf\u79f0\u4e3a \u9759\u6001\u53d8\u91cf(static variables) \u3002 \u9759\u6001\u53d8\u91cf\u5177\u6709\u9ed8\u8ba4\u521d\u59cb\u5316\u503c 0 \u5728 6.3 - \u5c40\u90e8\u53d8\u91cf \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5c40\u90e8\u53d8\u91cf\u3002\u5c40\u90e8\u53d8\u91cf\u662f\u5b9a\u4e49\u5728\u51fd\u6570\u4e2d\uff08\u6216\u51fd\u6570\u53c2\u6570\uff09\u7684\u53d8\u91cf\u3002\u5c40\u90e8\u53d8\u91cf\u5177\u6709\u5757\u4f5c\u7528\u57df\uff08\u53ea\u5728\u5b9a\u4e49\u5b83\u7684\u5757\u4e2d\u53ef\u89c1\uff09\uff0c\u540c\u65f6\u5177\u6709\u81ea\u52a8\u6301\u7eed\u65f6\u95f4\uff08\u5728\u5b9a\u4e49\u65f6\u521b\u5efa\uff0c\u5728\u5757\u7ed3\u675f\u65f6\u9500\u6bc1\uff09\u3002 C++ \u4e2d\uff0c\u53d8\u91cf\u4e5f\u53ef\u4ee5\u88ab\u58f0\u660e\u5728\u51fd\u6570 \u5916\u9762 \u3002\u8fd9\u4e9b\u53d8\u91cf\u79f0\u4e3a\u5168\u5c40\u53d8\u91cf\u3002 \u5168\u5c40\u53d8\u91cf\u7684\u58f0\u660e\u548c\u547d\u540d \u6309\u7167\u60ef\u4f8b\uff0c\u5168\u5c40\u53d8\u91cf\u4f1a\u88ab\u5b9a\u4e49\u5728\u6587\u4ef6\u7684\u5f00\u5934\uff0c\u7d27\u63a5\u7740\u5934\u6587\u4ef6\u5305\u542b\u7684\u540e\u9762\uff0c\u4f46\u662f\u5728\u6240\u6709\u4ee3\u7801\u7684\u524d\u9762\u3002\u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include // Variables declared outside of a function are global variables int g_x {}; // global variable g_x void doSomething () { // global variables can be seen and used everywhere in the file g_x = 3 ; std :: cout << g_x << '\\n' ; } int main () { doSomething (); std :: cout << g_x << '\\n' ; // global variables can be seen and used everywhere in the file g_x = 5 ; std :: cout << g_x << '\\n' ; return 0 ; } // g_x goes out of scope here \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b 1 2 3 3 3 5 \u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u5f88\u591a\u7a0b\u5e8f\u5458\u559c\u6b22\u4e3a\u5168\u5c40\u53d8\u91cf\u6dfb\u52a0\u524d\u7f00 \u201cg\u201d \u6216 \u201cg_\u201d \u4ee5\u8868\u660e\u5b83\u662f\u5168\u5c40\u7684\uff08global\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u8003\u8651\u4e3a\u5168\u5c40\u53d8\u91cf\u6dfb\u52a0 \u201cg\u201d or \u201cg_\u201d \u524d\u7f00\u4ee5\u533a\u522b\u4e8e\u5176\u4ed6\u53d8\u91cf\u3002 \u5168\u5c40\u53d8\u91cf\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\u548c\u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u5168\u5c40\u53d8\u91cf\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\uff08\u6709\u65f6\u5019\u4e5f\u975e\u6b63\u5f0f\u5730\u79f0\u4e3a\u5168\u5c40\u4f5c\u7528\u57df\u6216\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4f5c\u7528\u57df\uff09\uff0c\u4e5f\u5c31\u662f\u8bf4\u8fd9\u7c7b\u53d8\u91cf\u4ece\u58f0\u660e\u5b83\u7684\u6587\u4ef6\u5f00\u5934\uff0c\u5230\u6587\u4ef6\u7ed3\u5c3e\u90fd\u662f\u53ef\u89c1\u7684\u3002\u4e00\u65e6\u58f0\u660e\u4e4b\u540e\uff0c\u5168\u5c40\u53d8\u91cf\u5c31\u53ef\u4ee5\u5728\u6587\u4ef6\u7684\u4efb\u4f55\u5730\u65b9\u4f7f\u7528\uff01\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5168\u5c40\u53d8\u91cf g_x \u65e2\u53ef\u4ee5\u5728 doSomething() \u4e2d\u4f7f\u7528\uff0c\u4e5f\u53ef\u4ee5\u5728 main() \u4e2d\u4f7f\u7528\u3002 \u56e0\u4e3a\u5b83\u4eec\u88ab\u5b9a\u4e49\u5728\u51fd\u6570\u7684\u5916\u9762\uff0c\u5168\u5c40\u53d8\u91cf\u901a\u5e38\u88ab\u8ba4\u4e3a\u662f\u5c5e\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u3002 \u5168\u5c40\u53d8\u91cf\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u88ab\u521b\u5efa\uff0c\u5e76\u4e14\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u2014\u2014\u5373 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u5177\u6709\u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u7684\u53d8\u91cf\u79f0\u4e3a \u9759\u6001\u53d8\u91cf(static variables) \u3002 \u5c40\u90e8\u53d8\u91cf\u9ed8\u8ba4\u662f\u672a\u521d\u59cb\u5316\u7684\uff0c\u9759\u6001\u53d8\u91cf\u5219\u4e0d\u540c\uff0c\u5b83\u5177\u6709\u9ed8\u8ba4\u7684\u521d\u59cb\u503c 0 \u3002 \u5168\u5c40\u53d8\u91cf\u521d\u59cb\u5316 \u975e\u5e38\u91cf\u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u4e5f\u53ef\u4ee5\u8fdb\u884c\u521d\u59cb\u5316\uff08\u53ef\u9009\uff09\u3002 1 2 3 int g_x ; // no explicit initializer (zero-initialized by default) int g_y {}; // zero initialized int g_z { 1 }; // initialized with value \u5e38\u91cf\u5168\u5c40\u53d8\u91cf \u548c\u5c40\u90e8\u53d8\u91cf\u4e00\u6837\uff0c\u5168\u5c40\u53d8\u91cf\u4e5f\u53ef\u4ee5\u662f\u5e38\u91cf\u3002\u548c\u6240\u6709\u7684\u5e38\u91cf\u4e00\u6837\uff0c\u5e38\u91cf\u5168\u5c40\u53d8\u91cf\u5fc5\u987b\u88ab\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include const int g_x ; // \u9519\u8bef\uff1aconst \u5e38\u91cf\u5fc5\u987b\u521d\u59cb\u5316 constexpr int g_w ; // \u9519\u8bef\uff1aconstexpr \u5e38\u91cf\u5fc5\u987b\u521d\u59cb\u5316 const int g_y { 1 }; // const global variable g_y, initialized with a value constexpr int g_z { 2 }; // constexpr global variable g_z, initialized with a value void doSomething () { // global variables can be seen and used everywhere in the file std :: cout << g_y << '\\n' ; std :: cout << g_z << '\\n' ; } int main () { doSomething (); // global variables can be seen and used everywhere in the file std :: cout << g_y << '\\n' ; std :: cout << g_z << '\\n' ; return 0 ; } // g_y and g_z goes out of scope here \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf \u8be6\u7ec6\u4ecb\u7ecd\u5e38\u91cf\u5168\u5c40\u53d8\u91cf\u3002 \u5173\u4e8e\u975e\u5e38\u91cf\u7c7b\u578b\u5168\u5c40\u53d8\u91cf\u7684\u63d0\u9192 \u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u5c1d\u8bd5\u5b9a\u4e49\u5f88\u591a\u5168\u5c40\u53d8\u91cf\uff0c\u56e0\u4e3a\u8fd9\u6837\u4ed6\u4eec\u5c31\u53ef\u4ee5\u4e0d\u9700\u8981\u4f20\u53c2\u5c31\u80fd\u591f\u4f7f\u7528\u8fd9\u4e9b\u53d8\u91cf\u3002\u4e0d\u8fc7\uff0c\u901a\u5e38\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\uff01\u6211\u4eec\u4f1a\u5728 6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c \u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002 \u5c0f\u7ed3 1 2 3 4 5 6 7 8 9 10 11 12 // \u975e\u5e38\u91cf\u5168\u5c40\u53d8\u91cf int g_x ; // \u5b9a\u4e49\u672a\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\uff08\u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a0\uff09 int g_x {}; // \u663e\u793a\u5730\u5b9a\u4e49\u521d\u59cb\u5316\u4e3a0\u7684\u5168\u5c40\u53d8\u91cf int g_x { 1 }; // \u663e\u793a\u5730\u521d\u59cb\u5316\u5168\u5c40\u53d8\u91cf // Const \u5168\u5c40\u53d8\u91cf const int g_y ; // \u9519\u8bef\uff1aconst \u53d8\u91cf\u5fc5\u987b\u521d\u59cb\u5316 const int g_y { 2 }; // \u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u5168\u5c40\u5e38\u91cf // Constexpr \u5168\u5c40\u53d8\u91cf constexpr int g_y ; // \u9519\u8bef: constexpr \u53d8\u91cf\u5fc5\u987b\u521d\u59cb\u5316 constexpr int g_y { 3 }; // \u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u5168\u5c40 const \u53d8\u91cf","title":"6.4 - \u5168\u5c40\u53d8\u91cf"},{"location":"6-4-Introduction-to-global-variables/#_1","text":"\u6309\u7167\u60ef\u4f8b\uff0c\u5168\u5c40\u53d8\u91cf\u4f1a\u88ab\u5b9a\u4e49\u5728\u6587\u4ef6\u7684\u5f00\u5934\uff0c\u7d27\u63a5\u7740\u5934\u6587\u4ef6\u5305\u542b\u7684\u540e\u9762\uff0c\u4f46\u662f\u5728\u6240\u6709\u4ee3\u7801\u7684\u524d\u9762\u3002\u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include // Variables declared outside of a function are global variables int g_x {}; // global variable g_x void doSomething () { // global variables can be seen and used everywhere in the file g_x = 3 ; std :: cout << g_x << '\\n' ; } int main () { doSomething (); std :: cout << g_x << '\\n' ; // global variables can be seen and used everywhere in the file g_x = 5 ; std :: cout << g_x << '\\n' ; return 0 ; } // g_x goes out of scope here \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b 1 2 3 3 3 5 \u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u5f88\u591a\u7a0b\u5e8f\u5458\u559c\u6b22\u4e3a\u5168\u5c40\u53d8\u91cf\u6dfb\u52a0\u524d\u7f00 \u201cg\u201d \u6216 \u201cg_\u201d \u4ee5\u8868\u660e\u5b83\u662f\u5168\u5c40\u7684\uff08global\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u8003\u8651\u4e3a\u5168\u5c40\u53d8\u91cf\u6dfb\u52a0 \u201cg\u201d or \u201cg_\u201d \u524d\u7f00\u4ee5\u533a\u522b\u4e8e\u5176\u4ed6\u53d8\u91cf\u3002","title":"\u5168\u5c40\u53d8\u91cf\u7684\u58f0\u660e\u548c\u547d\u540d"},{"location":"6-4-Introduction-to-global-variables/#_2","text":"\u5168\u5c40\u53d8\u91cf\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\uff08\u6709\u65f6\u5019\u4e5f\u975e\u6b63\u5f0f\u5730\u79f0\u4e3a\u5168\u5c40\u4f5c\u7528\u57df\u6216\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4f5c\u7528\u57df\uff09\uff0c\u4e5f\u5c31\u662f\u8bf4\u8fd9\u7c7b\u53d8\u91cf\u4ece\u58f0\u660e\u5b83\u7684\u6587\u4ef6\u5f00\u5934\uff0c\u5230\u6587\u4ef6\u7ed3\u5c3e\u90fd\u662f\u53ef\u89c1\u7684\u3002\u4e00\u65e6\u58f0\u660e\u4e4b\u540e\uff0c\u5168\u5c40\u53d8\u91cf\u5c31\u53ef\u4ee5\u5728\u6587\u4ef6\u7684\u4efb\u4f55\u5730\u65b9\u4f7f\u7528\uff01\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5168\u5c40\u53d8\u91cf g_x \u65e2\u53ef\u4ee5\u5728 doSomething() \u4e2d\u4f7f\u7528\uff0c\u4e5f\u53ef\u4ee5\u5728 main() \u4e2d\u4f7f\u7528\u3002 \u56e0\u4e3a\u5b83\u4eec\u88ab\u5b9a\u4e49\u5728\u51fd\u6570\u7684\u5916\u9762\uff0c\u5168\u5c40\u53d8\u91cf\u901a\u5e38\u88ab\u8ba4\u4e3a\u662f\u5c5e\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u3002 \u5168\u5c40\u53d8\u91cf\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u88ab\u521b\u5efa\uff0c\u5e76\u4e14\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u2014\u2014\u5373 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u5177\u6709\u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u7684\u53d8\u91cf\u79f0\u4e3a \u9759\u6001\u53d8\u91cf(static variables) \u3002 \u5c40\u90e8\u53d8\u91cf\u9ed8\u8ba4\u662f\u672a\u521d\u59cb\u5316\u7684\uff0c\u9759\u6001\u53d8\u91cf\u5219\u4e0d\u540c\uff0c\u5b83\u5177\u6709\u9ed8\u8ba4\u7684\u521d\u59cb\u503c 0 \u3002","title":"\u5168\u5c40\u53d8\u91cf\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\u548c\u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4"},{"location":"6-4-Introduction-to-global-variables/#_3","text":"\u975e\u5e38\u91cf\u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u4e5f\u53ef\u4ee5\u8fdb\u884c\u521d\u59cb\u5316\uff08\u53ef\u9009\uff09\u3002 1 2 3 int g_x ; // no explicit initializer (zero-initialized by default) int g_y {}; // zero initialized int g_z { 1 }; // initialized with value","title":"\u5168\u5c40\u53d8\u91cf\u521d\u59cb\u5316"},{"location":"6-4-Introduction-to-global-variables/#_4","text":"\u548c\u5c40\u90e8\u53d8\u91cf\u4e00\u6837\uff0c\u5168\u5c40\u53d8\u91cf\u4e5f\u53ef\u4ee5\u662f\u5e38\u91cf\u3002\u548c\u6240\u6709\u7684\u5e38\u91cf\u4e00\u6837\uff0c\u5e38\u91cf\u5168\u5c40\u53d8\u91cf\u5fc5\u987b\u88ab\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include const int g_x ; // \u9519\u8bef\uff1aconst \u5e38\u91cf\u5fc5\u987b\u521d\u59cb\u5316 constexpr int g_w ; // \u9519\u8bef\uff1aconstexpr \u5e38\u91cf\u5fc5\u987b\u521d\u59cb\u5316 const int g_y { 1 }; // const global variable g_y, initialized with a value constexpr int g_z { 2 }; // constexpr global variable g_z, initialized with a value void doSomething () { // global variables can be seen and used everywhere in the file std :: cout << g_y << '\\n' ; std :: cout << g_z << '\\n' ; } int main () { doSomething (); // global variables can be seen and used everywhere in the file std :: cout << g_y << '\\n' ; std :: cout << g_z << '\\n' ; return 0 ; } // g_y and g_z goes out of scope here \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf \u8be6\u7ec6\u4ecb\u7ecd\u5e38\u91cf\u5168\u5c40\u53d8\u91cf\u3002","title":"\u5e38\u91cf\u5168\u5c40\u53d8\u91cf"},{"location":"6-4-Introduction-to-global-variables/#_5","text":"\u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u5c1d\u8bd5\u5b9a\u4e49\u5f88\u591a\u5168\u5c40\u53d8\u91cf\uff0c\u56e0\u4e3a\u8fd9\u6837\u4ed6\u4eec\u5c31\u53ef\u4ee5\u4e0d\u9700\u8981\u4f20\u53c2\u5c31\u80fd\u591f\u4f7f\u7528\u8fd9\u4e9b\u53d8\u91cf\u3002\u4e0d\u8fc7\uff0c\u901a\u5e38\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\uff01\u6211\u4eec\u4f1a\u5728 6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c \u4e2d\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002","title":"\u5173\u4e8e\u975e\u5e38\u91cf\u7c7b\u578b\u5168\u5c40\u53d8\u91cf\u7684\u63d0\u9192"},{"location":"6-4-Introduction-to-global-variables/#_6","text":"1 2 3 4 5 6 7 8 9 10 11 12 // \u975e\u5e38\u91cf\u5168\u5c40\u53d8\u91cf int g_x ; // \u5b9a\u4e49\u672a\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\uff08\u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a0\uff09 int g_x {}; // \u663e\u793a\u5730\u5b9a\u4e49\u521d\u59cb\u5316\u4e3a0\u7684\u5168\u5c40\u53d8\u91cf int g_x { 1 }; // \u663e\u793a\u5730\u521d\u59cb\u5316\u5168\u5c40\u53d8\u91cf // Const \u5168\u5c40\u53d8\u91cf const int g_y ; // \u9519\u8bef\uff1aconst \u53d8\u91cf\u5fc5\u987b\u521d\u59cb\u5316 const int g_y { 2 }; // \u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u5168\u5c40\u5e38\u91cf // Constexpr \u5168\u5c40\u53d8\u91cf constexpr int g_y ; // \u9519\u8bef: constexpr \u53d8\u91cf\u5fc5\u987b\u521d\u59cb\u5316 constexpr int g_y { 3 }; // \u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u5168\u5c40 const \u53d8\u91cf","title":"\u5c0f\u7ed3"},{"location":"6-5-Variable-shadowing-name-hiding/","tags":["variable shadowing"],"text":"Key Takeaway \u5185\u5916\u4ee3\u7801\u5757\u4e2d\u5982\u679c\u6709\u540c\u540d\u7684\u53d8\u91cf\uff0c\u90a3\u4e48\u5728\u4e24\u4e2a\u53d8\u91cf\u90fd\u6709\u6548\u7684\u4f5c\u7528\u57df\u4e2d\uff0c\u5185\u5c42\u5b9a\u4e49\u7684\u53d8\u91cf\u4f1a\u4e34\u65f6\u5c4f\u853d\u5916\u5c42\u5b9a\u4e49\u7684\u53d8\u91cf \u5728\u5185\u5c42\u5d4c\u5957\u5757\u4e2d\uff0c\u662f\u6ca1\u6709\u529e\u6cd5\u76f4\u63a5\u8bbf\u95ee\u5df2\u7ecf\u88ab\u4e34\u65f6\u5c4f\u853d\u7684\u5916\u5c42\u53d8\u91cf\u7684\u3002 \u5f53\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u91cd\u540d\u65f6\uff0c\u5b83\u4e5f\u4f1a\u5728\u5c40\u90e8\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4e2d\u5956\u5168\u5c40\u53d8\u91cf\u4e34\u65f6\u5c4f\u853d,\u4e0d\u8fc7\uff0c\u56e0\u4e3a\u5168\u5c40\u53d8\u91cf\u5c5e\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u7684\u4e00\u90e8\u5206\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6ca1\u6709\u524d\u7f00\u7684 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 ( :: )\u544a\u8bc9\u7f16\u8bd1\u5668\u53bb\u67e5\u627e\u5168\u5c40\u53d8\u91cf\u800c\u4e0d\u662f\u5c40\u90e8\u53d8\u91cf\u3002 \u5168\u5c40\u53d8\u91cf\u540d\u5177\u6709\u201cg_\u201d \u524d\u7f00\u7684\u65f6\u5019\uff0c\u4e0d\u5bb9\u6613\u53d1\u751f\u53d8\u91cf\u906e\u853d \u907f\u514d\u53d8\u91cf\u906e\u853d \u5404\u4e2a\u5757\u5b9a\u4e49\u4e86\u81ea\u5df1\u7684\u4f5c\u7528\u57df\u3002\u6240\u4ee5\u5982\u679c\u6211\u4eec\u5728\u4e00\u4e2a\u5d4c\u5957\u5757\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\uff0c\u800c\u5728\u5176\u5916\u5c42\u5757\u4e2d\uff0c\u6709\u4e00\u4e2a\u540c\u540d\u7684\u53d8\u91cf\uff0c\u6b64\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u5f53\u4e24\u4e2a\u53d8\u91cf\u90fd\u5728\u4f5c\u7528\u57df\u4e2d\u65f6\uff0c\u5d4c\u5957\u5757\u4e2d\u7684\u53d8\u91cf\u4f1a\u5c06\u5916\u5c42\u5757\u4e2d\u7684\u540c\u540d\u53d8\u91cf\u9690\u85cf\u8d77\u6765\uff0c\u79f0\u4e3a\u53d8\u91cf\u9690\u85cf\u6216\u53d8\u91cf\u906e\u853d\uff08shadowing\uff09\u3002 \u5c40\u90e8\u53d8\u91cf\u7684\u906e\u853d 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include int main () { // \u5916\u5c42\u5757 int apples { 5 }; // \u5916\u5c42\u5757\u4e2d\u7684 apples { // \u5d4c\u5957\u5757 // \u6b64\u5904\u7684 apples \u6307\u7684\u662f\u5916\u5c42\u7684 apples std :: cout << apples << '\\n' ; // \u6253\u5370\u5916\u5c42\u7684 apples \u7684\u503c int apples { 0 }; // \u5728\u5d4c\u5957\u5757\u4e2d\u5b9a\u4e49 apples // \u4ece\u6b64\uff0capples \u6307\u7684\u662f\u5d4c\u5957\u5757\u4e2d\u7684 apples // \u5916\u5c42\u7684 apples \u6682\u65f6\u88ab\u5c4f\u853d\u4e86 apples = 10 ; // \u5c06 10 \u8d4b\u503c\u7ed9\u5d4c\u5957\u5757\u4e2d\u7684 apples std :: cout << apples << '\\n' ; // \u6253\u5370\u5d4c\u5957\u5757\u7684 apples \u7684\u503c } // \u5d4c\u5957\u5757\u7684 apples \u9500\u6bc1 std :: cout << apples << '\\n' ; // \u6253\u5370\u5916\u5c42\u7684 apples \u7684\u503c return 0 ; } // \u5916\u5c42\u5757\u7684 apples \u9500\u6bc1 \u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 5 10 5 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5728\u5916\u5c42\u5757\u4e2d\u58f0\u660e\u4e86\u4e00\u4e2a\u540d\u4e3a apples \u7684\u53d8\u91cf\u3002\u8fd9\u4e2a\u53d8\u91cf\u5728\u5d4c\u5957\u5757\u4e2d\u4e5f\u662f\u53ef\u89c1\u7684\uff0c\u53ef\u4ee5\u770b\u5230\u5b83\u7684\u503c\u80fd\u591f\u6b63\u786e\u6253\u5370\u51fa\u6765(5)\u3002 \u7136\u540e\u6211\u4eec\u5728\u5d4c\u5957\u5757\u4e2d\u58f0\u660e\u4e86\u4e00\u4e2a\u4e0d\u540c\u7684\u53d8\u91cf\uff08\u4f46\u540d\u5b57\u4ecd\u7136\u662f apples \uff09\u3002\u4ece\u8fd9\u65f6\u5f00\u59cb\u5230\u5d4c\u5957\u5757\u7684\u7ed3\u5c3e\uff0c apples \u6307\u7684\u5c31\u662f\u5d4c\u5957\u5c42\u7684 apples \u800c\u4e0d\u662f\u5916\u5c42\u7684 apples \u3002 \u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u5c06 10 \u8d4b\u503c\u7ed9 apples \u65f6\uff0c\u6211\u4eec\u8d4b\u503c\u7684\u662f\u5185\u5c42\u5757\u7684 apples \u3002\u7136\u540e\u6253\u5370 apples \uff0c\u5f97\u5230\u7ed3\u679c10\uff0c\u7b49\u5230\u5185\u5c42\u5757\u7ed3\u675f\u65f6\uff0c apples \u5c31\u9500\u6bc1\u4e86\u3002\u4f46\u662f\uff0c\u5916\u5c42\u5757\u7684 apples \u5e76\u4e0d\u4f1a\u53d7\u5f71\u54cd\uff0c\u6240\u4ee5\u6211\u4eec\u5728\u5916\u5c42\u5757\u4e2d\u4ecd\u7136\u53ef\u4ee5\u6253\u5370 apples ( 5 )\u3002 \u6ce8\u610f\uff0c\u5982\u679c\u6211\u4eec\u6ca1\u6709\u5728\u5185\u5c42\u5757\u4e2d\u5b9a\u4e49 apples \uff0c\u90a3\u4e48\u5185\u5c42\u5757\u4e2d\u7684 apples \u6307\u7684\u4ecd\u7136\u662f\u5916\u5c42\u7684 apples \u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5bf9 apples \u8d4b\u503c10\uff0c\u5219\u63a5\u6536\u8be5\u503c\u7684\u4e5f\u662f\u5916\u5c42\u5b9a\u4e49\u7684 apples \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int main () { // outer block int apples { 5 }; // here's the outer block apples { // nested block // apples refers to outer block apples here std :: cout << apples << '\\n' ; // print value of outer block apples // no inner block apples defined in this example apples = 10 ; // this applies to outer block apples std :: cout << apples << '\\n' ; // print value of outer block apples } // outer block apples retains its value even after we leave the nested block std :: cout << apples << '\\n' ; // prints value of outer block apples return 0 ; } // outer block apples destroyed \u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 5 10 10 \u5728\u5185\u5c42\u5d4c\u5957\u5757\u4e2d\uff0c\u662f\u6ca1\u6709\u529e\u6cd5\u76f4\u63a5\u8bbf\u95ee\u5df2\u7ecf\u88ab\u4e34\u65f6\u5c4f\u853d\u7684\u5916\u5c42\u53d8\u91cf\u7684\u3002 \u5168\u5c40\u53d8\u91cf\u7684\u906e\u853d \u5f53\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u91cd\u540d\u65f6\uff0c\u5b83\u4e5f\u4f1a\u5728\u5c40\u90e8\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4e2d\u5956\u5168\u5c40\u53d8\u91cf\u4e34\u65f6\u5c4f\u853d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int value { 5 }; // global variable void foo () { std :: cout << \"global variable value: \" << value << '\\n' ; // value is not shadowed here, so this refers to the global value } int main () { int value { 7 }; // hides the global variable value until the end of this block ++ value ; // increments local value, not global value std :: cout << \"local variable value: \" << value << '\\n' ; foo (); return 0 ; } // local value is destroyed \u6253\u5370\u7ed3\u679c\uff1a 1 2 local variable value: 8 global variable value: 5 \u4e0d\u8fc7\uff0c\u56e0\u4e3a\u5168\u5c40\u53d8\u91cf\u5c5e\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u7684\u4e00\u90e8\u5206\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6ca1\u6709\u524d\u7f00\u7684 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 ( :: )\u544a\u8bc9\u7f16\u8bd1\u5668\u53bb\u67e5\u627e\u5168\u5c40\u53d8\u91cf\u800c\u4e0d\u662f\u5c40\u90e8\u53d8\u91cf\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int value { 5 }; // global variable int main () { int value { 7 }; // hides the global variable value ++ value ; // increments local value, not global value -- ( :: value ); // decrements global value, not local value (parenthesis added for readability) std :: cout << \"local variable value: \" << value << '\\n' ; std :: cout << \"global variable value: \" << :: value << '\\n' ; return 0 ; } // local value is destroyed \u6253\u5370\u7ed3\u679c\uff1a 1 2 local variable value: 8 global variable value: 4 \u907f\u514d\u53d8\u91cf\u906e\u853d \u901a\u5e38\u60c5\u51b5\u4e0b\u5e94\u8be5\u907f\u514d\u53d8\u91cf\u906e\u853d\uff0c \u56e0\u4e3a\u5b83\u5bb9\u6613\u5f15\u8d77\u53d8\u91cf\u8bef\u7528\u7684\u9519\u8bef\u3002\u5f88\u591a\u7f16\u8bd1\u5668\u4f1a\u5728\u68c0\u6d4b\u5230\u53d8\u91cf\u906e\u853d\u65f6\u53d1\u51fa\u544a\u8b66\u3002 \u4e5f\u6b63\u662f\u5904\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u6211\u4eec\u63a8\u8350\u907f\u514d\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u7684\u906e\u853d\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u4f60\u7684\u5168\u5c40\u53d8\u91cf\u540d\u5177\u6709\u201cg_\u201d \u524d\u7f00\u7684\u8bdd\uff0c\u8fd9\u901a\u5e38\u662f\u5f88\u5bb9\u6613\u907f\u514d\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u53d8\u91cf\u906e\u853d","title":"6.5 - \u53d8\u91cf\u906e\u853d"},{"location":"6-5-Variable-shadowing-name-hiding/#_1","text":"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include int main () { // \u5916\u5c42\u5757 int apples { 5 }; // \u5916\u5c42\u5757\u4e2d\u7684 apples { // \u5d4c\u5957\u5757 // \u6b64\u5904\u7684 apples \u6307\u7684\u662f\u5916\u5c42\u7684 apples std :: cout << apples << '\\n' ; // \u6253\u5370\u5916\u5c42\u7684 apples \u7684\u503c int apples { 0 }; // \u5728\u5d4c\u5957\u5757\u4e2d\u5b9a\u4e49 apples // \u4ece\u6b64\uff0capples \u6307\u7684\u662f\u5d4c\u5957\u5757\u4e2d\u7684 apples // \u5916\u5c42\u7684 apples \u6682\u65f6\u88ab\u5c4f\u853d\u4e86 apples = 10 ; // \u5c06 10 \u8d4b\u503c\u7ed9\u5d4c\u5957\u5757\u4e2d\u7684 apples std :: cout << apples << '\\n' ; // \u6253\u5370\u5d4c\u5957\u5757\u7684 apples \u7684\u503c } // \u5d4c\u5957\u5757\u7684 apples \u9500\u6bc1 std :: cout << apples << '\\n' ; // \u6253\u5370\u5916\u5c42\u7684 apples \u7684\u503c return 0 ; } // \u5916\u5c42\u5757\u7684 apples \u9500\u6bc1 \u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 5 10 5 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5728\u5916\u5c42\u5757\u4e2d\u58f0\u660e\u4e86\u4e00\u4e2a\u540d\u4e3a apples \u7684\u53d8\u91cf\u3002\u8fd9\u4e2a\u53d8\u91cf\u5728\u5d4c\u5957\u5757\u4e2d\u4e5f\u662f\u53ef\u89c1\u7684\uff0c\u53ef\u4ee5\u770b\u5230\u5b83\u7684\u503c\u80fd\u591f\u6b63\u786e\u6253\u5370\u51fa\u6765(5)\u3002 \u7136\u540e\u6211\u4eec\u5728\u5d4c\u5957\u5757\u4e2d\u58f0\u660e\u4e86\u4e00\u4e2a\u4e0d\u540c\u7684\u53d8\u91cf\uff08\u4f46\u540d\u5b57\u4ecd\u7136\u662f apples \uff09\u3002\u4ece\u8fd9\u65f6\u5f00\u59cb\u5230\u5d4c\u5957\u5757\u7684\u7ed3\u5c3e\uff0c apples \u6307\u7684\u5c31\u662f\u5d4c\u5957\u5c42\u7684 apples \u800c\u4e0d\u662f\u5916\u5c42\u7684 apples \u3002 \u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u5c06 10 \u8d4b\u503c\u7ed9 apples \u65f6\uff0c\u6211\u4eec\u8d4b\u503c\u7684\u662f\u5185\u5c42\u5757\u7684 apples \u3002\u7136\u540e\u6253\u5370 apples \uff0c\u5f97\u5230\u7ed3\u679c10\uff0c\u7b49\u5230\u5185\u5c42\u5757\u7ed3\u675f\u65f6\uff0c apples \u5c31\u9500\u6bc1\u4e86\u3002\u4f46\u662f\uff0c\u5916\u5c42\u5757\u7684 apples \u5e76\u4e0d\u4f1a\u53d7\u5f71\u54cd\uff0c\u6240\u4ee5\u6211\u4eec\u5728\u5916\u5c42\u5757\u4e2d\u4ecd\u7136\u53ef\u4ee5\u6253\u5370 apples ( 5 )\u3002 \u6ce8\u610f\uff0c\u5982\u679c\u6211\u4eec\u6ca1\u6709\u5728\u5185\u5c42\u5757\u4e2d\u5b9a\u4e49 apples \uff0c\u90a3\u4e48\u5185\u5c42\u5757\u4e2d\u7684 apples \u6307\u7684\u4ecd\u7136\u662f\u5916\u5c42\u7684 apples \u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5bf9 apples \u8d4b\u503c10\uff0c\u5219\u63a5\u6536\u8be5\u503c\u7684\u4e5f\u662f\u5916\u5c42\u5b9a\u4e49\u7684 apples \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int main () { // outer block int apples { 5 }; // here's the outer block apples { // nested block // apples refers to outer block apples here std :: cout << apples << '\\n' ; // print value of outer block apples // no inner block apples defined in this example apples = 10 ; // this applies to outer block apples std :: cout << apples << '\\n' ; // print value of outer block apples } // outer block apples retains its value even after we leave the nested block std :: cout << apples << '\\n' ; // prints value of outer block apples return 0 ; } // outer block apples destroyed \u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 5 10 10 \u5728\u5185\u5c42\u5d4c\u5957\u5757\u4e2d\uff0c\u662f\u6ca1\u6709\u529e\u6cd5\u76f4\u63a5\u8bbf\u95ee\u5df2\u7ecf\u88ab\u4e34\u65f6\u5c4f\u853d\u7684\u5916\u5c42\u53d8\u91cf\u7684\u3002","title":"\u5c40\u90e8\u53d8\u91cf\u7684\u906e\u853d"},{"location":"6-5-Variable-shadowing-name-hiding/#_2","text":"\u5f53\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u91cd\u540d\u65f6\uff0c\u5b83\u4e5f\u4f1a\u5728\u5c40\u90e8\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4e2d\u5956\u5168\u5c40\u53d8\u91cf\u4e34\u65f6\u5c4f\u853d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int value { 5 }; // global variable void foo () { std :: cout << \"global variable value: \" << value << '\\n' ; // value is not shadowed here, so this refers to the global value } int main () { int value { 7 }; // hides the global variable value until the end of this block ++ value ; // increments local value, not global value std :: cout << \"local variable value: \" << value << '\\n' ; foo (); return 0 ; } // local value is destroyed \u6253\u5370\u7ed3\u679c\uff1a 1 2 local variable value: 8 global variable value: 5 \u4e0d\u8fc7\uff0c\u56e0\u4e3a\u5168\u5c40\u53d8\u91cf\u5c5e\u4e8e\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u7684\u4e00\u90e8\u5206\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6ca1\u6709\u524d\u7f00\u7684 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 ( :: )\u544a\u8bc9\u7f16\u8bd1\u5668\u53bb\u67e5\u627e\u5168\u5c40\u53d8\u91cf\u800c\u4e0d\u662f\u5c40\u90e8\u53d8\u91cf\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int value { 5 }; // global variable int main () { int value { 7 }; // hides the global variable value ++ value ; // increments local value, not global value -- ( :: value ); // decrements global value, not local value (parenthesis added for readability) std :: cout << \"local variable value: \" << value << '\\n' ; std :: cout << \"global variable value: \" << :: value << '\\n' ; return 0 ; } // local value is destroyed \u6253\u5370\u7ed3\u679c\uff1a 1 2 local variable value: 8 global variable value: 4","title":"\u5168\u5c40\u53d8\u91cf\u7684\u906e\u853d"},{"location":"6-5-Variable-shadowing-name-hiding/#_3","text":"\u901a\u5e38\u60c5\u51b5\u4e0b\u5e94\u8be5\u907f\u514d\u53d8\u91cf\u906e\u853d\uff0c \u56e0\u4e3a\u5b83\u5bb9\u6613\u5f15\u8d77\u53d8\u91cf\u8bef\u7528\u7684\u9519\u8bef\u3002\u5f88\u591a\u7f16\u8bd1\u5668\u4f1a\u5728\u68c0\u6d4b\u5230\u53d8\u91cf\u906e\u853d\u65f6\u53d1\u51fa\u544a\u8b66\u3002 \u4e5f\u6b63\u662f\u5904\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u6211\u4eec\u63a8\u8350\u907f\u514d\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u7684\u906e\u853d\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u4f60\u7684\u5168\u5c40\u53d8\u91cf\u540d\u5177\u6709\u201cg_\u201d \u524d\u7f00\u7684\u8bdd\uff0c\u8fd9\u901a\u5e38\u662f\u5f88\u5bb9\u6613\u907f\u514d\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u53d8\u91cf\u906e\u853d","title":"\u907f\u514d\u53d8\u91cf\u906e\u853d"},{"location":"6-6-Internal-linkage/","tags":["linkage"],"text":"Key Takeaway \u5168\u5c40\u53d8\u91cf\u548c\u51fd\u6570\u7684\u8868\u793a\u7b26\uff0c\u94fe\u63a5\u5c5e\u6027\u4e3a \u5185\u90e8\u94fe\u63a5(internal linkage) \u6216 \u5185\u90e8\u94fe\u63a5(external linkage) \u5177\u6709\u5185\u90e8\u94fe\u63a5\u5c5e\u6027\u7684\u6807\u8bc6\u7b26\uff0c\u5728\u4e00\u4e2a\u6587\u4ef6\u5185\u53ef\u89c1\u53ef\u7528\uff0c \u4f46\u662f\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u4e0d\u53ef\u8bbf\u95ee static \u53ef\u4ee5\u5c06\u5168\u5c40\u53d8\u91cf\u58f0\u660e\u4e3a \u5185\u90e8\u53d8\u91cf \uff0c Const \u548c constexpr \u7684\u5168\u5c40\u53d8\u91cf\u5219\u9ed8\u8ba4\u5177\u6709\u5185\u90e8\u94fe\u63a5\u5c5e\u6027 \u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26\u53ef\u4ee5\u540c\u65f6\u8bbe\u7f6e\u53d8\u91cf\u7684\u94fe\u63a5\u5c5e\u6027\u548c\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\uff08\u4f46\u4e0d\u662f\u5176\u4f5c\u7528\u57df\uff09\uff0c\u6700\u5e38\u7528\u7684\u6709 static , extern , and mutable \u3002 \u5b9a\u4e49\u5728\u4e0d\u540c\u6587\u4ef6\u4e2d\u7684\u5185\u90e8\u53d8\u91cf\u662f\u72ec\u7acb\u7684\u5b9e\u4f53\uff0c\u56e0\u6b64\u4e0d\u8fdd\u53cd \u5355\u4e00\u5b9a\u4e49\u89c4\u5219 \u94fe\u63a5\u662f\u6807\u8bc6\u7b26\u7684\u5c5e\u6027\uff08\u800c\u4e0d\u662f\u53d8\u91cf\u7684\u5c5e\u6027\uff09 \u51fd\u6570\u6807\u8bc6\u7b26\u9ed8\u8ba4\u662f\u5916\u90e8\u94fe\u63a5\uff0c\u53ef\u4ee5\u901a\u8fc7 static \u5173\u952e\u5b57\u4fee\u6539\u4e3a\u5185\u90e8\u94fe\u63a5\uff0c\u6b64\u65f6\u51fd\u6570\u53ea\u80fd\u5728\u6587\u4ef6\u5185\u90e8\u4f7f\u7528 \u5728 6.3 - \u5c40\u90e8\u53d8\u91cf \u4e2d\u6211\u4eec\u8bb2\u8fc7\uff0c\u6807\u8bc6\u7b26\u7684\u94fe\u63a5\u5c5e\u6027\u51b3\u5b9a\u4e86\u53d8\u91cf\u7684\u591a\u4e2a\u58f0\u660e\u662f\u5426\u6307\u7684\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002\u540c\u65f6\u6211\u4eec\u4e5f\u4ecb\u7ecd\u4e86\u5c40\u90e8\u53d8\u91cf\u7684\u94fe\u63a5\u5c5e\u6027\u4e3a \u65e0\u94fe\u63a5 \u3002 \u5168\u5c40\u53d8\u91cf\u548c\u51fd\u6570\u7684\u6807\u8bc6\u7b26\u7684\u94fe\u63a5\u5c5e\u6027\u53ef\u4ee5\u662f \u5185\u90e8\u94fe\u63a5(internal linkage) \u6216 \u5916\u90e8\u94fe\u63a5(external linkage) \u3002\u672c\u8282\u8bfe\u4f1a\u9488\u5bf9\u5185\u90e8\u94fe\u63a5\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u800c\u5916\u90e8\u94fe\u63a5\u4f1a\u653e\u5230\u4e0b\u4e00\u8282\u8bfe\u2014\u2014 6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e \u3002 \u5177\u6709\u5185\u90e8\u94fe\u63a5\u5c5e\u6027\u7684\u6807\u8bc6\u7b26\uff0c\u5728\u4e00\u4e2a\u6587\u4ef6\u5185\u53ef\u89c1\u53ef\u7528\uff0c\u4f46\u662f\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u4e0d\u53ef\u8bbf\u95ee\uff08\u5373\u4e0d\u4f1a\u66b4\u9732\u7ed9\u94fe\u63a5\u5668\uff09\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u4e24\u4e2a\u6587\u4ef6\u4e2d\u5177\u6709\u540c\u540d\u7684\u6807\u8bc6\u7b26\uff0c\u4e14\u5b83\u4eec\u5177\u6709\u5185\u90e8\u94fe\u63a5\uff0c\u8fd9\u4e9b\u6807\u8bc6\u7b26\u4f1a\u88ab\u770b\u505a\u662f\u4e92\u76f8\u72ec\u7acb\u7684\u3002 \u5168\u5c40\u53d8\u91cf\u548c\u5185\u90e8\u94fe\u63a5 \u5177\u6709\u5185\u90e8\u94fe\u63a5\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a \u5185\u90e8\u53d8\u91cf \u3002 \u901a\u8fc7 static \u5173\u952e\u5b57\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00\u4e2a\u975e\u5e38\u7684\u5168\u5c40\u53d8\u91cf\u58f0\u660e\u4e3a\u5185\u90e8\u53d8\u91cf\u3002 1 2 3 4 5 6 7 8 9 static int g_x ; // non-constant globals have external linkage by default, but can be given internal linkage via the static keyword const int g_y { 1 }; // const globals have internal linkage by default constexpr int g_z { 2 }; // constexpr globals have internal linkage by default int main () { return 0 ; } Const \u548c constexpr \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u9ed8\u8ba4\u5177\u6709\u5185\u90e8\u94fe\u63a5\u5c5e\u6027\uff08\u56e0\u6b64\u4e0d\u9700\u8981\u5173\u952e\u5b57 static \uff0c\u5373\u4f7f\u7528\u4e86\u4e5f\u4f1a\u88ab\u5ffd\u7565\uff09\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u591a\u4e2a\u6587\u4ef6\u4e2d\u5177\u6709\u5185\u90e8\u53d8\u91cf\u7684\u60c5\u51b5\uff1a a.cpp 1 constexpr int g_x { 2 }; // \u8fd9\u4e2a\u5185\u90e8\u53d8\u91cf g_x \u53ea\u5728 a.cpp \u4e2d\u53ef\u4ee5\u8bbf\u95ee main.cpp 1 2 3 4 5 6 7 8 9 10 #include static int g_x { 3 }; // \u8be5\u5185\u90e8\u53d8\u91cf g_x \u53ea\u5728 main.cpp \u4e2d\u53ef\u4ee5\u8bbf\u95ee int main () { std :: cout << g_x << '\\n' ; // uses main.cpp's g_x, prints 3 return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 3 \u56e0\u4e3a g_x is internal to each file, main.cpp has no idea that a.cpp also has a variable named g_x (and vice versa). \u6269\u5c55\u9605\u8bfb static \u53d8\u91cf\u662f\u4e00\u4e2a\u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26\uff0c\u5b83\u53ef\u4ee5\u540c\u65f6\u8bbe\u7f6e\u53d8\u91cf\u7684\u94fe\u63a5\u5c5e\u6027\u548c \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff08\u4f46\u4e0d\u662f\u5176 \u4f5c\u7528\u57df \uff09\u3002\u6700\u5e38\u7528\u7684[[storage-class-specifier|\u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26]]\u6709 static , extern , and mutable \u3002\u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26\u8fd9\u4e2a\u8bcd\u5e38\u88ab\u7528\u5728\u6280\u672f\u6587\u6863\u4e2d\u3002 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u548c\u5185\u90e8\u94fe\u63a5 \u5728 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \uff0c\u5373\u4e00\u4e2a\u5bf9\u8c61\u6216\u8005\u51fd\u6570\u5728\u4e00\u4e2a\u51fd\u6570\u6216\u7a0b\u5e8f\u4e2d\u4e0d\u80fd\u88ab\u5b9a\u4e49\u4e00\u6b21\u4ee5\u4e0a\u3002 \u4f46\u662f\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b9a\u4e49\u5728\u4e0d\u540c\u6587\u4ef6\u4e2d\u7684\u5185\u90e8\u5bf9\u8c61\uff08\u548c\u51fd\u6570\uff09\u88ab\u770b\u505a\u662f\u72ec\u7acb\u7684\u5b9e\u4f53\uff08\u5373\u4f7f\u5b83\u4eec\u7684\u540d\u5b57\u548c\u7c7b\u578b\u662f\u76f8\u540c\u7684\uff09\uff0c\u56e0\u6b64\u8fd9\u5e76\u4e0d\u4f1a\u8fdd\u53cd\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u3002\u6bcf\u4e2a\u5185\u90e8\u5bf9\u8c61\u4ecd\u7136\u53ea\u88ab\u5b9a\u4e49\u4e86\u4e00\u6b21\u3002 \u5177\u6709\u5185\u90e8\u94fe\u63a5\u7684\u51fd\u6570 \u56e0\u4e3a\u94fe\u63a5\u662f\u6807\u8bc6\u7b26\u7684\u5c5e\u6027\uff08\u800c\u4e0d\u662f\u53d8\u91cf\u7684\u5c5e\u6027\uff09\uff0c\u6240\u4ee5\u51fd\u6570\u7684\u8868\u793a\u7b26\u540c\u6837\u5177\u6709\u94fe\u63a5\u5c5e\u6027\u3002\u51fd\u6570\u9ed8\u8ba4\u662f \u5916\u90e8\u94fe\u63a5 \u7684\uff0c\u4f46\u662f\u4e5f\u53ef\u4ee5\u7528 static \u4fee\u6539\u4e3a\u5185\u90e8\u94fe\u63a5\uff1a add.cpp 1 2 3 4 5 6 // \u51fd\u6570\u88ab\u58f0\u660e\u4e3a static\uff0c\u5148\u5728\u5b83\u53ea\u80fd\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528 // \u5c1d\u8bd5\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u901a\u8fc7\u524d\u5411\u58f0\u660e\u6765\u8bbf\u95ee\u8be5\u51fd\u6570\u4f1a\u5bfc\u81f4\u62a5\u9519 static int add ( int x , int y ) { return x + y ; } main.cpp 1 2 3 4 5 6 7 8 9 10 #include int add ( int x , int y ); // \u524d\u5411\u58f0\u660e add int main () { std :: cout << add ( 3 , 4 ) << '\\n' ; return 0 ; } \u7a0b\u5e8f\u65e0\u6cd5\u94fe\u63a5\uff0c add \u5728 add.cpp \u4ee5\u5916\u7684\u5730\u65b9\u4e0d\u80fd\u88ab\u8bbf\u95ee\u3002 \u5c0f\u7ed3 1 2 3 4 5 6 7 8 9 // \u5185\u90e8\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49 static int g_x ; // \u5b9a\u4e49\u672a\u521d\u59cb\u5316\u7684\u5185\u90e8\u5168\u5c40\u53d8\u91cf\uff08\u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a0\uff09 static int g_x { 1 }; // \u5b9a\u4e49\u521d\u59cb\u5316\u7684\u5185\u90e8\u5168\u5c40\u53d8\u91cf const int g_y { 2 }; // \u5b9a\u4e49\u521d\u59cb\u5316\u7684\u5185\u90e8\u5168\u5c40 const \u53d8\u91cf constexpr int g_y { 3 }; // \u5b9a\u4e49\u521d\u59cb\u5316\u7684\u5185\u90e8\u5168\u5c40 constexpr \u53d8\u91cf // \u5185\u90e8\u51fd\u6570\u5b9a\u4e49 static int foo () {}; // \u5b9a\u4e49\u5185\u90e8\u51fd\u6570 \u6211\u4eec\u4f1a\u5728 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 \u4e2d\u5bf9\u76f8\u5173\u5185\u5bb9\u8fdb\u884c\u66f4\u8be6\u7ec6\u7684\u603b\u7ed3\u3002","title":"6.6 - \u5185\u90e8\u94fe\u63a5"},{"location":"6-6-Internal-linkage/#_1","text":"\u5177\u6709\u5185\u90e8\u94fe\u63a5\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a \u5185\u90e8\u53d8\u91cf \u3002 \u901a\u8fc7 static \u5173\u952e\u5b57\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00\u4e2a\u975e\u5e38\u7684\u5168\u5c40\u53d8\u91cf\u58f0\u660e\u4e3a\u5185\u90e8\u53d8\u91cf\u3002 1 2 3 4 5 6 7 8 9 static int g_x ; // non-constant globals have external linkage by default, but can be given internal linkage via the static keyword const int g_y { 1 }; // const globals have internal linkage by default constexpr int g_z { 2 }; // constexpr globals have internal linkage by default int main () { return 0 ; } Const \u548c constexpr \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u9ed8\u8ba4\u5177\u6709\u5185\u90e8\u94fe\u63a5\u5c5e\u6027\uff08\u56e0\u6b64\u4e0d\u9700\u8981\u5173\u952e\u5b57 static \uff0c\u5373\u4f7f\u7528\u4e86\u4e5f\u4f1a\u88ab\u5ffd\u7565\uff09\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u591a\u4e2a\u6587\u4ef6\u4e2d\u5177\u6709\u5185\u90e8\u53d8\u91cf\u7684\u60c5\u51b5\uff1a a.cpp 1 constexpr int g_x { 2 }; // \u8fd9\u4e2a\u5185\u90e8\u53d8\u91cf g_x \u53ea\u5728 a.cpp \u4e2d\u53ef\u4ee5\u8bbf\u95ee main.cpp 1 2 3 4 5 6 7 8 9 10 #include static int g_x { 3 }; // \u8be5\u5185\u90e8\u53d8\u91cf g_x \u53ea\u5728 main.cpp \u4e2d\u53ef\u4ee5\u8bbf\u95ee int main () { std :: cout << g_x << '\\n' ; // uses main.cpp's g_x, prints 3 return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 3 \u56e0\u4e3a g_x is internal to each file, main.cpp has no idea that a.cpp also has a variable named g_x (and vice versa). \u6269\u5c55\u9605\u8bfb static \u53d8\u91cf\u662f\u4e00\u4e2a\u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26\uff0c\u5b83\u53ef\u4ee5\u540c\u65f6\u8bbe\u7f6e\u53d8\u91cf\u7684\u94fe\u63a5\u5c5e\u6027\u548c \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff08\u4f46\u4e0d\u662f\u5176 \u4f5c\u7528\u57df \uff09\u3002\u6700\u5e38\u7528\u7684[[storage-class-specifier|\u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26]]\u6709 static , extern , and mutable \u3002\u5b58\u50a8\u7c7b\u578b\u8bf4\u660e\u7b26\u8fd9\u4e2a\u8bcd\u5e38\u88ab\u7528\u5728\u6280\u672f\u6587\u6863\u4e2d\u3002","title":"\u5168\u5c40\u53d8\u91cf\u548c\u5185\u90e8\u94fe\u63a5"},{"location":"6-6-Internal-linkage/#_2","text":"\u5728 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \uff0c\u5373\u4e00\u4e2a\u5bf9\u8c61\u6216\u8005\u51fd\u6570\u5728\u4e00\u4e2a\u51fd\u6570\u6216\u7a0b\u5e8f\u4e2d\u4e0d\u80fd\u88ab\u5b9a\u4e49\u4e00\u6b21\u4ee5\u4e0a\u3002 \u4f46\u662f\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b9a\u4e49\u5728\u4e0d\u540c\u6587\u4ef6\u4e2d\u7684\u5185\u90e8\u5bf9\u8c61\uff08\u548c\u51fd\u6570\uff09\u88ab\u770b\u505a\u662f\u72ec\u7acb\u7684\u5b9e\u4f53\uff08\u5373\u4f7f\u5b83\u4eec\u7684\u540d\u5b57\u548c\u7c7b\u578b\u662f\u76f8\u540c\u7684\uff09\uff0c\u56e0\u6b64\u8fd9\u5e76\u4e0d\u4f1a\u8fdd\u53cd\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u3002\u6bcf\u4e2a\u5185\u90e8\u5bf9\u8c61\u4ecd\u7136\u53ea\u88ab\u5b9a\u4e49\u4e86\u4e00\u6b21\u3002","title":"\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u548c\u5185\u90e8\u94fe\u63a5"},{"location":"6-6-Internal-linkage/#_3","text":"\u56e0\u4e3a\u94fe\u63a5\u662f\u6807\u8bc6\u7b26\u7684\u5c5e\u6027\uff08\u800c\u4e0d\u662f\u53d8\u91cf\u7684\u5c5e\u6027\uff09\uff0c\u6240\u4ee5\u51fd\u6570\u7684\u8868\u793a\u7b26\u540c\u6837\u5177\u6709\u94fe\u63a5\u5c5e\u6027\u3002\u51fd\u6570\u9ed8\u8ba4\u662f \u5916\u90e8\u94fe\u63a5 \u7684\uff0c\u4f46\u662f\u4e5f\u53ef\u4ee5\u7528 static \u4fee\u6539\u4e3a\u5185\u90e8\u94fe\u63a5\uff1a add.cpp 1 2 3 4 5 6 // \u51fd\u6570\u88ab\u58f0\u660e\u4e3a static\uff0c\u5148\u5728\u5b83\u53ea\u80fd\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528 // \u5c1d\u8bd5\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u901a\u8fc7\u524d\u5411\u58f0\u660e\u6765\u8bbf\u95ee\u8be5\u51fd\u6570\u4f1a\u5bfc\u81f4\u62a5\u9519 static int add ( int x , int y ) { return x + y ; } main.cpp 1 2 3 4 5 6 7 8 9 10 #include int add ( int x , int y ); // \u524d\u5411\u58f0\u660e add int main () { std :: cout << add ( 3 , 4 ) << '\\n' ; return 0 ; } \u7a0b\u5e8f\u65e0\u6cd5\u94fe\u63a5\uff0c add \u5728 add.cpp \u4ee5\u5916\u7684\u5730\u65b9\u4e0d\u80fd\u88ab\u8bbf\u95ee\u3002","title":"\u5177\u6709\u5185\u90e8\u94fe\u63a5\u7684\u51fd\u6570"},{"location":"6-6-Internal-linkage/#_4","text":"1 2 3 4 5 6 7 8 9 // \u5185\u90e8\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49 static int g_x ; // \u5b9a\u4e49\u672a\u521d\u59cb\u5316\u7684\u5185\u90e8\u5168\u5c40\u53d8\u91cf\uff08\u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a0\uff09 static int g_x { 1 }; // \u5b9a\u4e49\u521d\u59cb\u5316\u7684\u5185\u90e8\u5168\u5c40\u53d8\u91cf const int g_y { 2 }; // \u5b9a\u4e49\u521d\u59cb\u5316\u7684\u5185\u90e8\u5168\u5c40 const \u53d8\u91cf constexpr int g_y { 3 }; // \u5b9a\u4e49\u521d\u59cb\u5316\u7684\u5185\u90e8\u5168\u5c40 constexpr \u53d8\u91cf // \u5185\u90e8\u51fd\u6570\u5b9a\u4e49 static int foo () {}; // \u5b9a\u4e49\u5185\u90e8\u51fd\u6570 \u6211\u4eec\u4f1a\u5728 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 \u4e2d\u5bf9\u76f8\u5173\u5185\u5bb9\u8fdb\u884c\u66f4\u8be6\u7ec6\u7684\u603b\u7ed3\u3002","title":"\u5c0f\u7ed3"},{"location":"6-7-External-linkage-and-variable-forward-declarations/","tags":["linkage","declaration"],"text":"Key Takeaway \u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u6807\u8bc6\u7b26\uff0c\u53ef\u4ee5\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u6587\u4ef6\u4e2d\u88ab\u4f7f\u7528\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u524d\u5411\u58f0\u660e\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u4f7f\u7528\u3002 \u94fe\u63a5\u662f\u6807\u8bc6\u7b26\u7684\u9009\u9879\uff0c\u800c\u4e0d\u662f\u53d8\u91cf\u7684\u5c5e\u6027\uff0c\u56e0\u6b64\u51fd\u6570\u4e5f\u6709\u94fe\u63a5\u5c5e\u6027 \u51fd\u6570\u662f\u9ed8\u8ba4\u5916\u90e8\u94fe\u63a5\u7684\uff0c\u53ef\u4ee5\u7528 static \u4fee\u6539\u4e3a\u5185\u90e8\u94fe\u63a5 \u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u5168\u5c40\u53d8\u91cf\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a\u5916\u90e8\u53d8\u91cf\u3002\u4f7f\u7528 extern \u5173\u952e\u5b57\u53ef\u4ee5\u5c06\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u4e3a\u5916\u90e8\u53d8\u91cf\uff08\u4f7f\u5176\u53ef\u4ee5\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u8bbf\u95ee\uff09\u3002\u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\u9ed8\u8ba4\u662f\u5916\u90e8\u53d8\u91cf\u3002 \u524d\u5411\u58f0\u660e\u4e00\u4e2a\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u5916\u90e8\u53d8\u91cf\uff0c\u4e5f\u9700\u8981\u4f7f\u7528 extern \u5173\u952e\u5b57 \u5982\u679c\u4f60\u5e0c\u671b\u5b9a\u4e49\u4e00\u4e2a\u672a\u521d\u59cb\u5316\u7684\u975econst\u5168\u5c40\u53d8\u91cf\u3002\u8bf7\u4e0d\u8981\u4f7f\u7528 extern \u5173\u952e\u5b57\uff0c\u5426\u5219 C++ \u4f1a\u8ba4\u4e3a\u4f60\u662f\u5728\u524d\u5411\u58f0\u660e\u67d0\u4e2a\u53d8\u91cf \u5728C++\u4e2d\uff0c \u6240\u6709 \u5168\u5c40\u53d8\u91cf\u90fd\u5177\u6709\u201c\u6587\u4ef6\u4f5c\u7528\u57df\u201d\uff0c\u540c\u65f6\u94fe\u63a5\u5c5e\u6027\u5219\u51b3\u5b9a\u4e86\u5b83\u4eec\u662f\u5426\u53ef\u4ee5\u88ab\u5176\u4ed6\u6587\u4ef6\u4f7f\u7528\u3002 \u5728\u524d\u9762\u7684\u8bfe\u7a0b\u4e2d ( 6.6 - \u5185\u90e8\u94fe\u63a5 )\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u5982\u4f55\u4f7f\u7528 \u5185\u90e8\u94fe\u63a5 \u5c06\u6807\u8bc6\u7b26\u9650\u5b9a\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u6587\u4ef6\u4e2d\u3002\u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u63a2\u8ba8 \u5916\u90e8\u94fe\u63a5 \u3002 \u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u6807\u8bc6\u7b26\uff0c\u53ef\u4ee5\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u6587\u4ef6\u4e2d\u88ab\u4f7f\u7528\uff0c\u4e5f\u53ef\u4ee5\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u88ab\u4f7f\u7528\uff08\u901a\u8fc7 \u524d\u5411\u58f0\u660e \uff09\u3002\u4ece\u8fd9\u4e2a\u610f\u4e49\u4e0a\u6765\u8bb2\uff0c\u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u53d8\u91cf\u624d\u662f\u771f\u6b63\u7684*\u201d\u5168\u5c40\u201c\u53d8\u91cf\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5728\u7a0b\u5e8f\u7684\u4efb\u4f55\u5730\u65b9\u88ab\u4f7f\u7528\u3002 \u51fd\u6570\u9ed8\u8ba4\u5177\u4f53\u5916\u90e8\u94fe\u63a5\u5c5e\u6027 \u5728 2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u8c03\u7528\u53e6\u5916\u4e00\u4e2a\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\uff0c\u8fd9\u662f\u56e0\u4e3a\u51fd\u6570\u9ed8\u8ba4\u5177\u6709\u5916\u90e8\u94fe\u63a5\u5c5e\u6027\u3002 \u4e3a\u4e86\u80fd\u591f\u8c03\u7528\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\uff0c\u5fc5\u987b\u8981\u5728\u4f7f\u7528\u8be5\u51fd\u6570\u7684\u6587\u4ef6\u4e2d\u8fdb\u884c\u524d\u5411\u58f0\u660e\u3002\u524d\u5411\u58f0\u660e\u544a\u8bc9\u7f16\u8bd1\u5668\u51fd\u6570\u7684\u5b58\u5728\uff0c\u7136\u540e\u94fe\u63a5\u5668\u53ef\u4ee5\u5c06\u51fd\u6570\u8c03\u7528\u5173\u8054\u5230\u5b9e\u9645\u7684\u5b9a\u4e49\u3002 \u4f8b\u5982\uff1a a.cpp 1 2 3 4 5 6 #include void sayHi () // \u51fd\u6570\u9ed8\u8ba4\u5177\u6709\u5916\u90e8\u94fe\u63a5\uff0c\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u53ef\u89c1 { std :: cout << \"Hi!\" ; } main.cpp 1 2 3 4 5 6 7 8 void sayHi (); // \u5bf9\u51fd\u6570\u8fdb\u884c\u524d\u5411\u58f0\u660e\uff0c\u4f7f\u8be5\u51fd\u6570\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u53ef\u4ee5\u88ab\u8bbf\u95ee int main () { sayHi (); // \u8c03\u7528\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\uff0c\u94fe\u63a5\u5668\u4f1a\u5c06\u8c03\u7528\u5173\u8054\u5230\u51fd\u6570\u7684\u5b9a\u4e49 return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 Hi! \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c sayHi() \u88ab\u524d\u5411\u58f0\u660e\u5728 main.cpp \u4e2d\uff0c\u8fd9\u4f7f\u5f97 main.cpp \u53ef\u4ee5\u8bbf\u95ee\u5b9a\u4e49\u5728 a.cpp \u4e2d\u7684 sayHi() \u3002\u524d\u5411\u58f0\u660e\u6ee1\u8db3\u4e86\u7f16\u8bd1\u5668\u7684\u9700\u8981\uff0c\u800c\u94fe\u63a5\u5668\u4e5f\u80fd\u591f\u5c06\u51fd\u6570\u8c03\u7528\u5173\u8054\u5230\u51fd\u6570\u5b9a\u4e49\u3002 \u5982\u679c sayHi() \u5177\u6709\u5185\u90e8\u94fe\u63a5\u5c5e\u6027\uff0c\u5219\u94fe\u63a5\u5668\u4fbf\u4e0d\u80fd\u591f\u5c06\u51fd\u6570\u8c03\u7528\u5173\u8054\u5230\u51fd\u6570\u5b9a\u4e49\uff0c\u4f1a\u4ea7\u751f\u94fe\u63a5\u9519\u8bef\u3002 \u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u5168\u5c40\u53d8\u91cf \u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u5168\u5c40\u53d8\u91cf\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a \u5916\u90e8\u53d8\u91cf(external variable) \u3002\u4f7f\u7528 extern \u5173\u952e\u5b57\u53ef\u4ee5\u5c06\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u4e3a\u5916\u90e8\u53d8\u91cf\uff08\u4f7f\u5176\u53ef\u4ee5\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u8bbf\u95ee\uff09\uff1a 1 2 3 4 5 6 7 8 9 int g_x { 2 }; // \u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\u9ed8\u8ba4\u662f\u5916\u90e8\u94fe\u63a5 extern const int g_y { 3 }; // const \u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u88ab\u5b9a\u4e49\u4e3a extern\uff0c\u4f7f\u5176\u6210\u4e3a\u5916\u90e8\u53d8\u91cf extern constexpr int g_z { 3 }; // constexpr \u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u88ab\u5b9a\u4e49\u4e3a extern\uff0c\u4f7f\u5176\u6210\u4e3a\u5916\u90e8\u53d8\u91cf (\u4f46\u662f\u6ca1\u6709\u610f\u4e49) int main () { return 0 ; } \u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\u9ed8\u8ba4\u662f\u5916\u90e8\u94fe\u63a5\uff08\u5373\u4f7f\u4f7f\u7528 extern \u4e5f\u4f1a\u88ab\u5ffd\u7565\uff09\u3002 \u4f7f\u7528 extern \u5173\u952e\u5b57\u8fdb\u884c\u53d8\u91cf\u7684\u524d\u5411\u58f0\u660e \u4e3a\u4e86\u4f7f\u7528\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u5168\u5c40 \u5916\u90e8\u53d8\u91cf \uff0c\u4f60\u540c\u6837\u9700\u8981\u5728\u4f7f\u7528\u5b83\u7684\u6587\u4ef6\u4e2d\u5bf9\u5176\u8fdb\u884c \u524d\u5411\u58f0\u660e \u3002\u5bf9\u4e8e\u53d8\u91cf\u6765\u8bf4\uff0c\u521b\u5efa\u524d\u5411\u58f0\u660e\u8fd8\u9700\u8981\u4f7f\u7528 extern \u5173\u952e\u5b57\uff08\u4e0d\u9700\u8981\u521d\u59cb\u5316\u503c\uff09\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u524d\u5411\u58f0\u660e\u7684\u4f8b\u5b50\uff1a a.cpp 1 2 3 // \u5168\u5c40\u53d8\u91cf\u5b9a\u4e49 int g_x { 2 }; // \u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\u9ed8\u8ba4\u662f\u5916\u90e8\u94fe\u63a5 extern const int g_y { 3 }; // extern \u4e3a g_y \u521b\u5efa\u5916\u90e8\u94fe\u63a5 main.cpp 1 2 3 4 5 6 7 8 9 10 11 #include extern int g_x ; // \u8fd9\u4e2a extern \u662f\u53d8\u91cf g_x\u7684\u524d\u5411\u58f0\u660e\uff0cg_x\u88ab\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6 extern const int g_y ; // \u8fd9\u4e2a extern \u662f const \u53d8\u91cf g_y \u7684\u524d\u5411\u58f0\u660e\uff0c\u5b83\u4e5f\u88ab\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6 int main () { std :: cout << g_x ; // prints 2 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c a.cpp \u548c main.cpp \u90fd\u5f15\u7528\u4e86\u4e00\u4e2a\u76f8\u540c\u7684\u5168\u5c40\u53d8\u91cf g_x \u3002\u56e0\u6b64\uff0c\u5373\u4f7f g_x \u662f\u5728 a.cpp \u4e2d\u5b9a\u4e49\u548c\u521d\u59cb\u5316\u7684\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u5728 main.cpp \u4e2d\u4f7f\u7528\u5b83\uff08\u9700\u8981\u5bf9 g_x \u8fdb\u884c\u524d\u5411\u58f0\u660e\uff09\u3002 \u6ce8\u610f\uff0c extern \u5173\u952e\u5b57\u5728\u4e0d\u540c\u7684\u8bed\u5883\u4e0b\u6709\u4e0d\u540c\u7684\u542b\u4e49\u3002\u6709\u4e9b\u8bed\u5883\u4e0b\uff0c extern \u8868\u793a \u201c\u4e3a\u53d8\u91cf\u521b\u5efa\u5916\u90e8\u94fe\u63a5\u201d\u3002\u800c\u5728\u53e6\u5916\u7684\u8bed\u53e5\u4e0b\uff0c extern \u8868\u793a \u201c\u8fd9\u662f\u4e00\u4e2a\u5b9a\u4e49\u5728\u5176\u4ed6\u5730\u65b9\u7684\u5916\u90e8\u53d8\u91cf\u7684\u524d\u5411\u58f0\u660e\u201d\u3002\u7684\u786e\u6709\u70b9\u7ed5\uff0c\u6240\u4ee5\u6211\u4eec\u4f1a\u5728 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 \u4e2d\u5bf9\u8fd9\u4e9b\u95ee\u9898\u8fdb\u884c\u8be6\u7ec6\u7684\u603b\u7ed3\u3002 \u6ce8\u610f \u5982\u679c\u4f60\u5e0c\u671b\u5b9a\u4e49\u4e00\u4e2a\u672a\u521d\u59cb\u5316\u7684\u975econst\u5168\u5c40\u53d8\u91cf\u3002\u8bf7\u4e0d\u8981\u4f7f\u7528 extern \u5173\u952e\u5b57\uff0c\u5426\u5219 C++ \u4f1a\u8ba4\u4e3a\u4f60\u662f\u5728\u524d\u5411\u58f0\u660e\u67d0\u4e2a\u53d8\u91cf\u3002 \u6ce8\u610f \u5c3d\u7ba1 constexpr \u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7 extern \u5173\u952e\u5b57\u8d4b\u4e88\u5916\u90e8\u94fe\u63a5\u5c5e\u6027\uff0c\u4f46\u662f\u7531\u4e8e\u5b83\u4eec\u5e76\u4e0d\u80fd\u88ab\u524d\u5411\u58f0\u660e\uff0c\u6240\u4ee5\u8fd9\u4e48\u505a\u5e76\u65e0\u610f\u4e49\u3002 \u8fd9\u662f\u56e0\u4e3a\u7f16\u8bd1\u5668\u5728 \u7f16\u8bd1\u65f6 \u5c31\u5fc5\u987b\u6307\u5b9a constexpr \u53d8\u91cf\u7684\u503c\u3002\u5982\u679c\u8be5\u53d8\u91cf\u88ab\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c06\u65e0\u6cd5\u77e5\u9053\u5b83\u7684\u503c\u3002 ^ce6263 \u6ce8\u610f\uff0c\u51fd\u6570\u7684\u524d\u5411\u58f0\u660e\u4e0d\u9700\u8981\u4f7f\u7528 extern \u5173\u952e\u5b57\u2014\u2014\u7f16\u8bd1\u5668\u53ef\u4ee5\u901a\u8fc7\u662f\u5426\u6709\u51fd\u6570\u4f53\u6765\u5224\u65ad\u6b64\u5904\u662f\u5728\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u8fd8\u662f\u5728\u8fdb\u884c\u524d\u5411\u58f0\u660e\u3002\u53d8\u91cf\u7684\u524d\u5411\u58f0\u660e\u662f\u9700\u8981 extern \u5173\u952e\u5b57\u7684\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u533a\u5206\u53d8\u91cf\u5b9a\u4e49\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e\uff08\u770b\u4e0a\u53bb\u53ef\u80fd\u4f1a\u5b8c\u5168\u4e00\u6837\uff09\uff1a 1 2 3 4 5 6 7 // \u975e\u5e38\u91cf int g_x ; // \u53d8\u91cf\u58f0\u660e\u4e5f\u662f\u5b9a\u4e49 (\u53ef\u4ee5\u521d\u59cb\u5316\u4e5f\u53ef\u4ee5\u4e0d\u521d\u59cb\u5316) extern int g_x ; // \u524d\u5411\u58f0\u660e (\u65e0\u521d\u59cb\u5316\u503c) // \u5e38\u91cf extern const int g_y { 1 }; // \u53d8\u91cf\u5b9a\u4e49(const\u5fc5\u987b\u8981\u521d\u59cb\u5316) extern const int g_y ; // \u524d\u5411\u58f0\u660e (\u65e0\u521d\u59cb\u5316\u503c) \u6587\u4ef6\u4f5c\u7528\u57df vs. \u5168\u5c40\u4f5c\u7528\u57df \u201c\u6587\u4ef6\u4f5c\u7528\u57df\u201d\u548c\u201c\u5168\u5c40\u4f5c\u7528\u57df\u201d\u8fd9\u4e24\u4e2a\u672f\u8bed\u53ef\u80fd\u4f1a\u4ee4\u4eba\u611f\u5230\u56f0\u60d1\uff0c\u8fd9\u4e3b\u8981\u662f\u56e0\u4e3a\u5728\u975e\u6b63\u5f0f\u573a\u5408\u5b83\u4eec\u7684\u7528\u6cd5\u4e0d\u591f\u4e25\u8c28\u3002\u4ece\u6280\u672f\u4e0a\u6765\u8bb2\uff0c\u5728 C++\u4e2d\uff0c \u6240\u6709 \u5168\u5c40\u53d8\u91cf\u90fd\u5177\u6709\u201c\u6587\u4ef6\u4f5c\u7528\u57df\u201d\uff0c\u540c\u65f6\u94fe\u63a5\u5c5e\u6027\u5219\u51b3\u5b9a\u4e86\u5b83\u4eec\u662f\u5426\u53ef\u4ee5\u88ab\u5176\u4ed6\u6587\u4ef6\u4f7f\u7528\u3002 \u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a global.cpp 1 2 int g_x { 2 }; // \u9ed8\u8ba4\u662f\u5916\u90e8\u94fe\u63a5 // g_x \u4ece\u8fd9\u91cc\u79bb\u5f00\u4f5c\u7528\u57df main.cpp 1 2 3 4 5 6 7 8 9 extern int g_x ; // \u5bf9 g_x \u8fdb\u884c\u524d\u5411\u58f0\u660e\uff0c\u4ece\u6b64\u65f6\u5f00\u59cb g_x \u53ef\u4ee5\u88ab\u8be5\u6587\u4ef6\u4f7f\u7528\u3002 int main () { std :: cout << g_x ; // should print 2 return 0 ; } // g_x \u7684\u524d\u5411\u58f0\u660e\u4ece\u6b64\u79bb\u5f00\u4f5c\u7528\u57df \u53d8\u91cf g_x \u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\uff08 global.cpp \uff09\u2014\u2014 \u5b83\u53ef\u4ee5\u88ab\u4f7f\u7528\u7684\u8303\u56f4\u4ece\u5b9a\u4e49\u5f00\u59cb\uff0c\u5230\u6587\u4ef6\u7ed3\u675f\u4e3a\u6b62\uff0c\u4f46\u662f\u5b83\u4e0d\u80fd\u76f4\u63a5\u5728 global.cpp \u4ee5\u5916\u7684\u5730\u65b9\u88ab\u4f7f\u7528\u3002 \u5728 main.cpp \u4e2d\uff0c g_x \u7684\u524d\u5411\u58f0\u660e\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\u2014\u2014\u5b83\u53ef\u4ee5\u88ab\u4f7f\u7528\u7684\u8303\u56f4\u4ece\u58f0\u660e\u5f00\u59cb\uff0c\u5230\u6587\u4ef6\u7ed3\u675f\u4e3a\u6b62\u3002 \u4e0d\u8fc7\uff0c\u6211\u4eec\u901a\u5e38\u975e\u6b63\u5f0f\u5730\u5c06\u201c\u6587\u4ef6\u4f5c\u7528\u57df\u201d\u7528\u6765\u63cf\u8ff0\u5177\u6709\u5185\u90e8\u94fe\u63a5\u7684\u53d8\u91cf\uff0c\u800c\u7528\u201c\u5168\u5c40\u4f5c\u7528\u57df\u201d\u6765\u63cf\u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u53d8\u91cf\uff08\u56e0\u4e3a\u8fd9\u7c7b\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7\u524d\u5411\u58f0\u660e\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u88ab\u4f7f\u7528\uff09\u3002 \u5c0f\u7ed3 1 2 3 4 5 6 7 8 9 // \u5916\u90e8\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49 int g_x ; // \u5b9a\u4e49\u5916\u90e8\u5168\u5c40\u53d8\u91cf\u4f46\u4e0d\u521d\u59cb\u5316\uff08\u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a0\uff09 extern const int g_x { 1 }; // \u5b9a\u4e49\u521d\u59cb\u5316\u7684 const \u5916\u90e8\u5168\u5c40\u53d8\u91cf extern constexpr int g_x { 2 }; // \u5b9a\u4e49\u521d\u59cb\u5316\u7684 constexpr \u5916\u90e8\u5168\u5c40\u53d8\u91cf // Forward declarations extern int g_y ; // \u524d\u5411\u58f0\u660e\u975e\u5e38\u6570\u7684\u5168\u5c40\u53d8\u91cf extern const int g_y ; // \u524d\u5411\u58f0\u660e const \u7684\u5168\u5c40\u53d8\u91cf extern constexpr int g_y ; // \u4e0d\u5141\u8bb8\u8fd9\u6837\u505a\uff1aconstexpr \u53d8\u91cf\u4e0d\u80fd\u524d\u5411\u58f0\u660e \u6211\u4eec\u4f1a\u5728 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 \u4e2d\u5bf9\u76f8\u5173\u5185\u5bb9\u8fdb\u884c\u66f4\u8be6\u7ec6\u7684\u603b\u7ed3\u3002","title":"6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e"},{"location":"6-7-External-linkage-and-variable-forward-declarations/#_1","text":"\u5728 2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u8c03\u7528\u53e6\u5916\u4e00\u4e2a\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\uff0c\u8fd9\u662f\u56e0\u4e3a\u51fd\u6570\u9ed8\u8ba4\u5177\u6709\u5916\u90e8\u94fe\u63a5\u5c5e\u6027\u3002 \u4e3a\u4e86\u80fd\u591f\u8c03\u7528\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\uff0c\u5fc5\u987b\u8981\u5728\u4f7f\u7528\u8be5\u51fd\u6570\u7684\u6587\u4ef6\u4e2d\u8fdb\u884c\u524d\u5411\u58f0\u660e\u3002\u524d\u5411\u58f0\u660e\u544a\u8bc9\u7f16\u8bd1\u5668\u51fd\u6570\u7684\u5b58\u5728\uff0c\u7136\u540e\u94fe\u63a5\u5668\u53ef\u4ee5\u5c06\u51fd\u6570\u8c03\u7528\u5173\u8054\u5230\u5b9e\u9645\u7684\u5b9a\u4e49\u3002 \u4f8b\u5982\uff1a a.cpp 1 2 3 4 5 6 #include void sayHi () // \u51fd\u6570\u9ed8\u8ba4\u5177\u6709\u5916\u90e8\u94fe\u63a5\uff0c\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u53ef\u89c1 { std :: cout << \"Hi!\" ; } main.cpp 1 2 3 4 5 6 7 8 void sayHi (); // \u5bf9\u51fd\u6570\u8fdb\u884c\u524d\u5411\u58f0\u660e\uff0c\u4f7f\u8be5\u51fd\u6570\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u53ef\u4ee5\u88ab\u8bbf\u95ee int main () { sayHi (); // \u8c03\u7528\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\uff0c\u94fe\u63a5\u5668\u4f1a\u5c06\u8c03\u7528\u5173\u8054\u5230\u51fd\u6570\u7684\u5b9a\u4e49 return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 Hi! \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c sayHi() \u88ab\u524d\u5411\u58f0\u660e\u5728 main.cpp \u4e2d\uff0c\u8fd9\u4f7f\u5f97 main.cpp \u53ef\u4ee5\u8bbf\u95ee\u5b9a\u4e49\u5728 a.cpp \u4e2d\u7684 sayHi() \u3002\u524d\u5411\u58f0\u660e\u6ee1\u8db3\u4e86\u7f16\u8bd1\u5668\u7684\u9700\u8981\uff0c\u800c\u94fe\u63a5\u5668\u4e5f\u80fd\u591f\u5c06\u51fd\u6570\u8c03\u7528\u5173\u8054\u5230\u51fd\u6570\u5b9a\u4e49\u3002 \u5982\u679c sayHi() \u5177\u6709\u5185\u90e8\u94fe\u63a5\u5c5e\u6027\uff0c\u5219\u94fe\u63a5\u5668\u4fbf\u4e0d\u80fd\u591f\u5c06\u51fd\u6570\u8c03\u7528\u5173\u8054\u5230\u51fd\u6570\u5b9a\u4e49\uff0c\u4f1a\u4ea7\u751f\u94fe\u63a5\u9519\u8bef\u3002","title":"\u51fd\u6570\u9ed8\u8ba4\u5177\u4f53\u5916\u90e8\u94fe\u63a5\u5c5e\u6027"},{"location":"6-7-External-linkage-and-variable-forward-declarations/#_2","text":"\u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u5168\u5c40\u53d8\u91cf\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a \u5916\u90e8\u53d8\u91cf(external variable) \u3002\u4f7f\u7528 extern \u5173\u952e\u5b57\u53ef\u4ee5\u5c06\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u4e3a\u5916\u90e8\u53d8\u91cf\uff08\u4f7f\u5176\u53ef\u4ee5\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u8bbf\u95ee\uff09\uff1a 1 2 3 4 5 6 7 8 9 int g_x { 2 }; // \u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\u9ed8\u8ba4\u662f\u5916\u90e8\u94fe\u63a5 extern const int g_y { 3 }; // const \u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u88ab\u5b9a\u4e49\u4e3a extern\uff0c\u4f7f\u5176\u6210\u4e3a\u5916\u90e8\u53d8\u91cf extern constexpr int g_z { 3 }; // constexpr \u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u88ab\u5b9a\u4e49\u4e3a extern\uff0c\u4f7f\u5176\u6210\u4e3a\u5916\u90e8\u53d8\u91cf (\u4f46\u662f\u6ca1\u6709\u610f\u4e49) int main () { return 0 ; } \u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\u9ed8\u8ba4\u662f\u5916\u90e8\u94fe\u63a5\uff08\u5373\u4f7f\u4f7f\u7528 extern \u4e5f\u4f1a\u88ab\u5ffd\u7565\uff09\u3002","title":"\u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u5168\u5c40\u53d8\u91cf"},{"location":"6-7-External-linkage-and-variable-forward-declarations/#extern","text":"\u4e3a\u4e86\u4f7f\u7528\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u5168\u5c40 \u5916\u90e8\u53d8\u91cf \uff0c\u4f60\u540c\u6837\u9700\u8981\u5728\u4f7f\u7528\u5b83\u7684\u6587\u4ef6\u4e2d\u5bf9\u5176\u8fdb\u884c \u524d\u5411\u58f0\u660e \u3002\u5bf9\u4e8e\u53d8\u91cf\u6765\u8bf4\uff0c\u521b\u5efa\u524d\u5411\u58f0\u660e\u8fd8\u9700\u8981\u4f7f\u7528 extern \u5173\u952e\u5b57\uff08\u4e0d\u9700\u8981\u521d\u59cb\u5316\u503c\uff09\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u524d\u5411\u58f0\u660e\u7684\u4f8b\u5b50\uff1a a.cpp 1 2 3 // \u5168\u5c40\u53d8\u91cf\u5b9a\u4e49 int g_x { 2 }; // \u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\u9ed8\u8ba4\u662f\u5916\u90e8\u94fe\u63a5 extern const int g_y { 3 }; // extern \u4e3a g_y \u521b\u5efa\u5916\u90e8\u94fe\u63a5 main.cpp 1 2 3 4 5 6 7 8 9 10 11 #include extern int g_x ; // \u8fd9\u4e2a extern \u662f\u53d8\u91cf g_x\u7684\u524d\u5411\u58f0\u660e\uff0cg_x\u88ab\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6 extern const int g_y ; // \u8fd9\u4e2a extern \u662f const \u53d8\u91cf g_y \u7684\u524d\u5411\u58f0\u660e\uff0c\u5b83\u4e5f\u88ab\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6 int main () { std :: cout << g_x ; // prints 2 return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c a.cpp \u548c main.cpp \u90fd\u5f15\u7528\u4e86\u4e00\u4e2a\u76f8\u540c\u7684\u5168\u5c40\u53d8\u91cf g_x \u3002\u56e0\u6b64\uff0c\u5373\u4f7f g_x \u662f\u5728 a.cpp \u4e2d\u5b9a\u4e49\u548c\u521d\u59cb\u5316\u7684\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u5728 main.cpp \u4e2d\u4f7f\u7528\u5b83\uff08\u9700\u8981\u5bf9 g_x \u8fdb\u884c\u524d\u5411\u58f0\u660e\uff09\u3002 \u6ce8\u610f\uff0c extern \u5173\u952e\u5b57\u5728\u4e0d\u540c\u7684\u8bed\u5883\u4e0b\u6709\u4e0d\u540c\u7684\u542b\u4e49\u3002\u6709\u4e9b\u8bed\u5883\u4e0b\uff0c extern \u8868\u793a \u201c\u4e3a\u53d8\u91cf\u521b\u5efa\u5916\u90e8\u94fe\u63a5\u201d\u3002\u800c\u5728\u53e6\u5916\u7684\u8bed\u53e5\u4e0b\uff0c extern \u8868\u793a \u201c\u8fd9\u662f\u4e00\u4e2a\u5b9a\u4e49\u5728\u5176\u4ed6\u5730\u65b9\u7684\u5916\u90e8\u53d8\u91cf\u7684\u524d\u5411\u58f0\u660e\u201d\u3002\u7684\u786e\u6709\u70b9\u7ed5\uff0c\u6240\u4ee5\u6211\u4eec\u4f1a\u5728 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 \u4e2d\u5bf9\u8fd9\u4e9b\u95ee\u9898\u8fdb\u884c\u8be6\u7ec6\u7684\u603b\u7ed3\u3002 \u6ce8\u610f \u5982\u679c\u4f60\u5e0c\u671b\u5b9a\u4e49\u4e00\u4e2a\u672a\u521d\u59cb\u5316\u7684\u975econst\u5168\u5c40\u53d8\u91cf\u3002\u8bf7\u4e0d\u8981\u4f7f\u7528 extern \u5173\u952e\u5b57\uff0c\u5426\u5219 C++ \u4f1a\u8ba4\u4e3a\u4f60\u662f\u5728\u524d\u5411\u58f0\u660e\u67d0\u4e2a\u53d8\u91cf\u3002 \u6ce8\u610f \u5c3d\u7ba1 constexpr \u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7 extern \u5173\u952e\u5b57\u8d4b\u4e88\u5916\u90e8\u94fe\u63a5\u5c5e\u6027\uff0c\u4f46\u662f\u7531\u4e8e\u5b83\u4eec\u5e76\u4e0d\u80fd\u88ab\u524d\u5411\u58f0\u660e\uff0c\u6240\u4ee5\u8fd9\u4e48\u505a\u5e76\u65e0\u610f\u4e49\u3002 \u8fd9\u662f\u56e0\u4e3a\u7f16\u8bd1\u5668\u5728 \u7f16\u8bd1\u65f6 \u5c31\u5fc5\u987b\u6307\u5b9a constexpr \u53d8\u91cf\u7684\u503c\u3002\u5982\u679c\u8be5\u53d8\u91cf\u88ab\u5b9a\u4e49\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c06\u65e0\u6cd5\u77e5\u9053\u5b83\u7684\u503c\u3002 ^ce6263 \u6ce8\u610f\uff0c\u51fd\u6570\u7684\u524d\u5411\u58f0\u660e\u4e0d\u9700\u8981\u4f7f\u7528 extern \u5173\u952e\u5b57\u2014\u2014\u7f16\u8bd1\u5668\u53ef\u4ee5\u901a\u8fc7\u662f\u5426\u6709\u51fd\u6570\u4f53\u6765\u5224\u65ad\u6b64\u5904\u662f\u5728\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u8fd8\u662f\u5728\u8fdb\u884c\u524d\u5411\u58f0\u660e\u3002\u53d8\u91cf\u7684\u524d\u5411\u58f0\u660e\u662f\u9700\u8981 extern \u5173\u952e\u5b57\u7684\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u533a\u5206\u53d8\u91cf\u5b9a\u4e49\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e\uff08\u770b\u4e0a\u53bb\u53ef\u80fd\u4f1a\u5b8c\u5168\u4e00\u6837\uff09\uff1a 1 2 3 4 5 6 7 // \u975e\u5e38\u91cf int g_x ; // \u53d8\u91cf\u58f0\u660e\u4e5f\u662f\u5b9a\u4e49 (\u53ef\u4ee5\u521d\u59cb\u5316\u4e5f\u53ef\u4ee5\u4e0d\u521d\u59cb\u5316) extern int g_x ; // \u524d\u5411\u58f0\u660e (\u65e0\u521d\u59cb\u5316\u503c) // \u5e38\u91cf extern const int g_y { 1 }; // \u53d8\u91cf\u5b9a\u4e49(const\u5fc5\u987b\u8981\u521d\u59cb\u5316) extern const int g_y ; // \u524d\u5411\u58f0\u660e (\u65e0\u521d\u59cb\u5316\u503c)","title":"\u4f7f\u7528 extern \u5173\u952e\u5b57\u8fdb\u884c\u53d8\u91cf\u7684\u524d\u5411\u58f0\u660e"},{"location":"6-7-External-linkage-and-variable-forward-declarations/#vs","text":"\u201c\u6587\u4ef6\u4f5c\u7528\u57df\u201d\u548c\u201c\u5168\u5c40\u4f5c\u7528\u57df\u201d\u8fd9\u4e24\u4e2a\u672f\u8bed\u53ef\u80fd\u4f1a\u4ee4\u4eba\u611f\u5230\u56f0\u60d1\uff0c\u8fd9\u4e3b\u8981\u662f\u56e0\u4e3a\u5728\u975e\u6b63\u5f0f\u573a\u5408\u5b83\u4eec\u7684\u7528\u6cd5\u4e0d\u591f\u4e25\u8c28\u3002\u4ece\u6280\u672f\u4e0a\u6765\u8bb2\uff0c\u5728 C++\u4e2d\uff0c \u6240\u6709 \u5168\u5c40\u53d8\u91cf\u90fd\u5177\u6709\u201c\u6587\u4ef6\u4f5c\u7528\u57df\u201d\uff0c\u540c\u65f6\u94fe\u63a5\u5c5e\u6027\u5219\u51b3\u5b9a\u4e86\u5b83\u4eec\u662f\u5426\u53ef\u4ee5\u88ab\u5176\u4ed6\u6587\u4ef6\u4f7f\u7528\u3002 \u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a global.cpp 1 2 int g_x { 2 }; // \u9ed8\u8ba4\u662f\u5916\u90e8\u94fe\u63a5 // g_x \u4ece\u8fd9\u91cc\u79bb\u5f00\u4f5c\u7528\u57df main.cpp 1 2 3 4 5 6 7 8 9 extern int g_x ; // \u5bf9 g_x \u8fdb\u884c\u524d\u5411\u58f0\u660e\uff0c\u4ece\u6b64\u65f6\u5f00\u59cb g_x \u53ef\u4ee5\u88ab\u8be5\u6587\u4ef6\u4f7f\u7528\u3002 int main () { std :: cout << g_x ; // should print 2 return 0 ; } // g_x \u7684\u524d\u5411\u58f0\u660e\u4ece\u6b64\u79bb\u5f00\u4f5c\u7528\u57df \u53d8\u91cf g_x \u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\uff08 global.cpp \uff09\u2014\u2014 \u5b83\u53ef\u4ee5\u88ab\u4f7f\u7528\u7684\u8303\u56f4\u4ece\u5b9a\u4e49\u5f00\u59cb\uff0c\u5230\u6587\u4ef6\u7ed3\u675f\u4e3a\u6b62\uff0c\u4f46\u662f\u5b83\u4e0d\u80fd\u76f4\u63a5\u5728 global.cpp \u4ee5\u5916\u7684\u5730\u65b9\u88ab\u4f7f\u7528\u3002 \u5728 main.cpp \u4e2d\uff0c g_x \u7684\u524d\u5411\u58f0\u660e\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\u2014\u2014\u5b83\u53ef\u4ee5\u88ab\u4f7f\u7528\u7684\u8303\u56f4\u4ece\u58f0\u660e\u5f00\u59cb\uff0c\u5230\u6587\u4ef6\u7ed3\u675f\u4e3a\u6b62\u3002 \u4e0d\u8fc7\uff0c\u6211\u4eec\u901a\u5e38\u975e\u6b63\u5f0f\u5730\u5c06\u201c\u6587\u4ef6\u4f5c\u7528\u57df\u201d\u7528\u6765\u63cf\u8ff0\u5177\u6709\u5185\u90e8\u94fe\u63a5\u7684\u53d8\u91cf\uff0c\u800c\u7528\u201c\u5168\u5c40\u4f5c\u7528\u57df\u201d\u6765\u63cf\u5177\u6709\u5916\u90e8\u94fe\u63a5\u7684\u53d8\u91cf\uff08\u56e0\u4e3a\u8fd9\u7c7b\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7\u524d\u5411\u58f0\u660e\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u88ab\u4f7f\u7528\uff09\u3002","title":"\u6587\u4ef6\u4f5c\u7528\u57df vs. \u5168\u5c40\u4f5c\u7528\u57df"},{"location":"6-7-External-linkage-and-variable-forward-declarations/#_3","text":"1 2 3 4 5 6 7 8 9 // \u5916\u90e8\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49 int g_x ; // \u5b9a\u4e49\u5916\u90e8\u5168\u5c40\u53d8\u91cf\u4f46\u4e0d\u521d\u59cb\u5316\uff08\u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a0\uff09 extern const int g_x { 1 }; // \u5b9a\u4e49\u521d\u59cb\u5316\u7684 const \u5916\u90e8\u5168\u5c40\u53d8\u91cf extern constexpr int g_x { 2 }; // \u5b9a\u4e49\u521d\u59cb\u5316\u7684 constexpr \u5916\u90e8\u5168\u5c40\u53d8\u91cf // Forward declarations extern int g_y ; // \u524d\u5411\u58f0\u660e\u975e\u5e38\u6570\u7684\u5168\u5c40\u53d8\u91cf extern const int g_y ; // \u524d\u5411\u58f0\u660e const \u7684\u5168\u5c40\u53d8\u91cf extern constexpr int g_y ; // \u4e0d\u5141\u8bb8\u8fd9\u6837\u505a\uff1aconstexpr \u53d8\u91cf\u4e0d\u80fd\u524d\u5411\u58f0\u660e \u6211\u4eec\u4f1a\u5728 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 \u4e2d\u5bf9\u76f8\u5173\u5185\u5bb9\u8fdb\u884c\u66f4\u8be6\u7ec6\u7684\u603b\u7ed3\u3002","title":"\u5c0f\u7ed3"},{"location":"6-8-Why-non-const-global-variables-are-evil/","tags":["global variable"],"text":"Key Takeaway \u975e const \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u88ab\u6240\u6709\u51fd\u6570\u4fee\u6539\uff0c\u56e0\u6b64\u4f1a\u8ba9\u7a0b\u5e8f\u53d8\u5f97\u96be\u4ee5\u9884\u6599\u3002 \u9759\u6001\u53d8\u91cf\u521d\u59cb\u5316\u6709\u4e24\u4e2a\u9636\u6bb5 \u7b2c\u4e00\u9636\u6bb5\u79f0\u4e3a\u9759\u6001\u521d\u59cb\u5316 \u3002\u5728\u9759\u6001\u521d\u59cb\u5316\u9636\u6bb5\uff0cconstexpr \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\uff08\u5305\u62ec\u5b57\u9762\u91cf\uff09\u90fd\u4f1a\u88ab\u521d\u59cb\u5316\u6210\u5177\u4f53\u7684\u503c\u3002\u6b64\u5916\uff0c\u6ca1\u6709\u663e\u5f0f\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\u4e5f\u4f1a\u88ab\u521d\u59cb\u5316\u4e3a0\u3002 -\u7b2c\u4e8c\u9636\u6bb5\u79f0\u4e3a\u52a8\u6001\u521d\u59cb\u5316\u3002\u8fd9\u4e2a\u9636\u6bb5\u8981\u590d\u6742\u7684\u591a\uff0c\u4f46\u662f\u5b83\u7684\u7cbe\u9ad3\u5728\u4e8e\u5177\u6709\u975e constexpr \u521d\u59cb\u5316\u503c \u7684\u5168\u5c40\u53d8\u91cf\u4f1a\u88ab\u521d\u59cb\u5316\u3002 \u5168\u5c40\u53d8\u91cf\u7684\u52a8\u6001\u521d\u59cb\u5316\u4f1a\u9020\u6210\u5f88\u5927\u95ee\u9898\uff0c\u5c3d\u91cf\u907f\u514d\u52a8\u6001\u521d\u59cb\u5316\u3002 \u5982\u679c\u4f60\u5411\u7f16\u7a0b\u5927\u4f6c\u8ba8\u6559\u4e00\u6761\u7f16\u7a0b\u5b9e\u8df5\u7684\u5efa\u8bae\uff0c\u5f88\u591a\u4eba\u90fd\u4f1a\u5728\u7a0d\u52a0\u601d\u8003\u540e\u544a\u8bc9\u4f60\uff1a\u201c\u907f\u514d\u5168\u5c40\u53d8\u91cf\uff01\u201d\u3002\u8fd9\u662f\u56e0\u4e3a\u5168\u5c40\u53d8\u91cf\u662f\u7f16\u7a0b\u8bed\u8a00\u4e2d\u6700\u88ab\u6ee5\u7528\u7684\u6982\u5ff5\u3002\u5c3d\u7ba1\u5728\u4e00\u4e9b\u7f16\u7a0b\u5c0f\u7ec3\u4e60\u4e2d\uff0c\u5168\u5c40\u53d8\u91cf\u770b\u8d77\u6765\u4eba\u755c\u65e0\u5bb3\uff0c\u4f46\u662f\u5230\u4e86\u5927\u578b\u7a0b\u5e8f\u4e2d\u5c31\u975e\u5e38\u5bb9\u6613\u5bfc\u81f4\u95ee\u9898\u3002 \u65b0\u624b\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u60f3\u8981\u4f7f\u7528\u5927\u91cf\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u56e0\u4e3a\u5168\u5c40\u53d8\u91cf\u7528\u8d77\u6765\u5f88\u65b9\u4fbf\uff0c\u5c24\u5176\u662f\u8bb8\u591a\u7a0b\u5e8f\u90fd\u9700\u8981\u8fd9\u4e9b\u53d8\u91cf\u7684\u65f6\u5019\uff08\u4f5c\u4e3a\u53c2\u6570\u4f7f\u7528\u7684\u8bdd\uff0c\u4f20\u53c2\u4f1a\u975e\u5e38\u75db\u82e6\uff09\u3002\u4e0d\u8fc7\uff0c\u8fd9\u5e76\u4e0d\u662f\u4e2a\u597d\u4e3b\u610f\u3002\u5f88\u591a\u7a0b\u5e8f\u5458\u90fd\u8ba4\u4e3a\u5e94\u8be5\u575a\u51b3\u907f\u514d\u975e const \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\uff01 \u5728\u6211\u4eec\u6df1\u7a76\u5176\u539f\u56e0\u4e4b\u524d\uff0c\u9996\u5148\u6f84\u6e05\u4e00\u4e2a\u95ee\u9898\u3002\u5f53\u6211\u4eec\u8c08\u8bba\u201c\u5168\u5c40\u662f\u9b54\u9b3c\u201d\u7684\u65f6\u5019\uff0c\u5e76\u4e0d\u662f\u8bf4 \u6240\u6709 \u7684\u5168\u5c40\u53d8\u91cf\u90fd\u662f\u9b54\u9b3c\u3002\u591a\u6570\u60c5\u51b5\u4e0b\u6211\u4eec\u6307\u7684\u662f\u975e const \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u3002 \u4e3a\u4ec0\u4e48\u975e const \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c \u5230\u76ee\u524d\u4f4d\u7f6e\uff0c\u975e const \u7c7b\u578b\u5168\u5c40\u53d8\u91cf\u5371\u9669\u7684\u6700\u5927\u539f\u56e0\uff0c\u662f\u56e0\u4e3a\u5b83\u7684\u503c\u53ef\u4ee5\u88ab\u4efb\u4f55\u51fd\u6570\u4fee\u6539\uff0c\u540c\u65f6\u4e5f\u6ca1\u6709\u7b80\u5355\u7684\u529e\u6cd5\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u5458\u6e05\u695a\u5730\u4e86\u89e3\u5230\u53d1\u751f\u4e86\u4ec0\u4e48\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 int g_mode ; // \u58f0\u660e\u5168\u5c40\u53d8\u91cf (\u9ed8\u8ba4\u4f1a\u521d\u59cb\u5316\u4e3a0) void doSomething () { g_mode = 2 ; // \u5c06\u5168\u5c40\u53d8\u91cf\u8bbe\u7f6e\u4e3a 2 } int main () { g_mode = 1 ; // \u6ce8\u610f\uff1a\u8fd9\u91cc\u53c8\u5c06\u5168\u5c40\u53d8\u91cf g_mode \u53d8\u91cf\u8bbe\u7f6e\u4e3a 1\uff0c\u800c\u5e76\u6ca1\u6709\u58f0\u660e\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf g_mode doSomething (); // \u7a0b\u5e8f\u5458\u6b64\u65f6\u8fd8\u4ee5\u4e3a g_mode \u662f 1 // \u4f46\u662f doSomething \u5df2\u7ecf\u5c06\u5b83\u4fee\u6539\u4e3a 2 \u4e86\u3002 if ( g_mode == 1 ) std :: cout << \"No threat detected. \\n \" ; else std :: cout << \"Launching nuclear missiles... \\n \" ; return 0 ; } \u6ce8\u610f\uff0c\u7a0b\u5e8f\u5458\u9996\u5148\u5c06\u53d8\u91cf g_mode \u8bbe\u7f6e\u4e3a\u4e86 1 \uff0c\u7136\u540e\u5c31\u8c03\u7528\u4e86 doSomething() \u3002\u9664\u975e\u7a0b\u5e8f\u5458\u660e\u786e\u77e5\u9053 doSomething() \u5185\u90e8\u4f1a\u4fee\u6539 g_mode \u7684\u503c\uff0c\u5426\u5219\u4ed6\u53ef\u80fd\u5e76\u4e0d\u4f1a\u60f3\u5230 doSomething() \u4f1a\u4fee\u6539 g_mode \u7684\u503c\uff01\u56e0\u6b64\uff0c\u63a5\u4e0b\u6765 main() \u6240\u505a\u7684\u4e8b\u60c5\u5c31\u4e0d\u50cf\u6211\u4eec\u671f\u671b\u7684\u90a3\u6837\u4e86\u3002 \u7b80\u5355\u6765\u8bf4\uff0c\u5168\u5c40\u53d8\u91cf\u4f1a\u8ba9\u7a0b\u5e8f\u96be\u4ee5\u9884\u6599\u3002 \u7a0b\u5e8f\u4e2d\u7684\u6bcf\u4e2a\u51fd\u6570\u90fd\u53ef\u80fd\u5f88\u5371\u9669\uff0c\u800c\u4e14\u7a0b\u5e8f\u5458\u901a\u5e38\u5e76\u6ca1\u6709\u7b80\u5355\u7684\u529e\u6cd5\u53ef\u4ee5\u5224\u65ad\u54ea\u4e2a\u51fd\u6570\u5371\u9669\u54ea\u4e2a\u4e0d\u5371\u9669\u3002\u5c40\u90e8\u53d8\u91cf\u5c31\u5b89\u5168\u7684\u591a\uff0c\u56e0\u4e3a\u5176\u4ed6\u51fd\u6570\u6ca1\u6709\u529e\u6cd5\u76f4\u63a5\u4fee\u6539\u5b83\u3002 \u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u6709\u5f88\u591a\u5176\u4ed6\u4e0d\u4f7f\u7528\u975econst\u5168\u5c40\u53d8\u91cf\u7684\u7406\u7531\u3002 \u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u7684\u7a0b\u5e8f\u4e2d\uff0c\u4e0b\u9762\u8fd9\u6837\u7684\u51fd\u6570\u5e76\u4e0d\u5c11\u89c1\uff1a 1 2 3 4 5 6 void someFunction () { // useful code if ( g_mode == 4 ) // do something good } \u5f53\u7a0b\u5e8f\u51fa\u73b0\u95ee\u9898\u65f6\uff0c\u4f60\u901a\u8fc7\u8c03\u8bd5\u786e\u5b9a\u4e86\u95ee\u9898\u7684\u539f\u56e0\u662f\u56e0\u4e3a g_mode \u7684\u503c\u662f 3 \u800c\u4e0d\u662f 4\u3002\u6b64\u65f6\u4f60\u5e94\u8be5\u5982\u4f55\u4fee\u590d\u8fd9\u4e2a\u95ee\u9898\u5462\uff1f\u4e3a\u4e86\u4fee\u590d\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f60\u5fc5\u987b\u627e\u5230 g_mode \u53ef\u80fd\u88ab\u8bbe\u7f6e\u4e3a 3 \u7684\u6240\u6709\u7684\u5730\u65b9\uff0c\u7136\u540e\u8ffd\u8e2a\u4ee3\u7801\u8fd0\u884c\uff0c\u5224\u65ad\u5b83\u662f\u4ec0\u4e48\u65f6\u5019\u88ab\u4fee\u6539\u4e3a 3 \u7684\u3002\u800c\u8fd9\u91cc\u9762\u53ef\u80fd\u4f1a\u6d89\u53ca\u5230\u5f88\u591a\u5b8c\u5168\u4e0d\u76f8\u5173\u7684\u4ee3\u7801\u3002 \u4e4b\u524d\u6211\u4eec\u8bf4\u8fc7\uff0c\u5c40\u90e8\u53d8\u91cf\u7684\u58f0\u660e\u5e94\u8be5\u5728\u6700\u63a5\u8fd1\u4f7f\u7528\u5b83\u7684\u5730\u65b9\uff0c\u56e0\u4e3a\u8fd9\u4e48\u505a\u53ef\u4ee5\u5728\u51fa\u73b0\u95ee\u9898\u65f6\uff0c\u6700\u5927\u7a0b\u5ea6\u51cf\u5c11\u4f60\u9700\u8981\u5206\u6790\u7684\u4ee3\u7801\u3002\u5168\u5c40\u53d8\u91cf\u5219\u6b63\u53f7\u76f8\u53cd\u2014\u2014\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u88ab\u4f7f\u7528\uff0c\u6240\u4ee5\u4f60\u5fc5\u987b\u9605\u8bfb\u5168\u90e8\u7684\u4ee3\u7801\u624d\u80fd\u591f\u4e86\u89e3\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u5728\u7b80\u77ed\u7684\u7a0b\u5e8f\u4e2d\u8fd9\u4e5f\u8bb8\u5e76\u4e0d\u662f\u95ee\u9898\uff0c\u4f46\u662f\u5728\u5927\u578b\u7a0b\u5e8f\u4e2d\uff0c\u8fd9\u5c31\u5f88\u9ebb\u70e6\u4e86\u3002 \u4f8b\u5982\uff0c\u4f60\u7684\u7a0b\u5e8f\u53ef\u80fd\u6709 442 \u5904\u4f7f\u7528\u4e86 g_mode \u7684\u5730\u65b9\u3002\u9664\u975e\u6709\u5f88\u597d\u7684\u6587\u6863\u8bf4\u660e\uff0c\u4e0d\u7136\u4f60\u53ef\u80fd\u9700\u8981\u7406\u89e3\u6bcf\u4e00\u5904\u4f7f\u7528\u4e86 g_mode \u7684\u4ee3\u7801\uff0c\u5b83\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u7684\u662f\u5982\u4f55\u4f7f\u7528\u7684\u3001\u6709\u6548\u503c\u662f\u591a\u5c11\u3001\u4e3b\u8981\u529f\u80fd\u662f\u4ec0\u4e48\u3002 \u5168\u5c40\u53d8\u91cf\u8fd8\u4f1a\u7834\u574f\u7a0b\u5e8f\u7684\u6a21\u5757\u6027\u548c\u7075\u6d3b\u6027\u3002\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u503c\u4f7f\u7528\u5176\u53c2\u6570\u4e14\u4e0d\u5177\u6709\u526f\u4f5c\u7528\uff0c\u90a3\u4e48\u5b83\u662f\u5b8c\u5168\u6a21\u5757\u5316\u7684\u3002\u6a21\u5757\u6027\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u7a0b\u5e8f\uff0c\u540c\u65f6\u4e5f\u80fd\u591f\u589e\u5f3a\u7a0b\u5e8f\u7684\u53ef\u590d\u7528\u6027\u3002\u5168\u5c40\u53d8\u91cf\u4f1a\u6781\u5927\u5730\u964d\u4f4e\u7a0b\u5e8f\u7684\u6a21\u5757\u6027\u3002 \u7279\u522b\u5730\uff0c\u907f\u514d\u5c06\u201c\u51b3\u7b56\u70b9\u201d\u53d8\u91cf\uff08\u4f8b\u5982\u6761\u4ef6\u8bed\u53e5\u4e2d\u7684\u53d8\u91cf\uff0c\u5c31\u50cf\u4e0a\u6587\u4e2d\u7684 g_mode \uff09\u5b9a\u4e49\u4e3a\u5168\u5c40\u53d8\u91cf\u3002\u5982\u679c\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u4ec5\u4ec5\u5305\u542b\u67d0\u4e2a\u6570\u636e\uff0c\u4e5f\u8bb8\u5e76\u4e0d\u4f1a\u8ba9\u4f60\u7684\u7a0b\u5e8f\u88ab\u7834\u574f\u3002\u800c\u5982\u679c\u8be5\u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u51b3\u5b9a\u7a0b\u5e8f \u5982\u4f55 \u8fd0\u884c\uff0c\u90a3\u4e48\u5b83\u9020\u6210\u95ee\u9898\u7684\u53ef\u80fd\u6027\u5c31\u4f1a\u5927\u5927\u63d0\u9ad8\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u53ef\u80fd\uff0c\u5c3d\u91cf\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\u800c\u4e0d\u662f\u5168\u5c40\u53d8\u91cf\u3002 \u5168\u5c40\u53d8\u91cf\u7684\u521d\u59cb\u5316\u987a\u5e8f\u95ee\u9898 \u9759\u6001\u53d8\u91cf\u7684\u521d\u59cb\u5316\uff08\u5176\u4e2d\u5305\u542b\u5168\u5c40\u53d8\u91cf\uff09\u662f\u7a0b\u5e8f\u542f\u52a8\u7684\u4e00\u90e8\u5206\uff0c\u5728\u6267\u884c main \u51fd\u6570\u524d\u5c31\u4f1a\u8fdb\u884c\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u6709\u4e24\u4e2a\u9636\u6bb5\uff1a \u7b2c\u4e00\u9636\u6bb5\u79f0\u4e3a \u9759\u6001\u521d\u59cb\u5316(static initialization) \u3002\u5728\u9759\u6001\u521d\u59cb\u5316\u9636\u6bb5\uff0cconstexpr \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\uff08\u5305\u62ec\u5b57\u9762\u91cf\uff09\u90fd\u4f1a\u88ab\u521d\u59cb\u5316\u6210\u5177\u4f53\u7684\u503c\u3002\u6b64\u5916\uff0c\u6ca1\u6709\u663e\u5f0f\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\u4e5f\u4f1a\u88ab\u521d\u59cb\u5316\u4e3a0\u3002 \u7b2c\u4e8c\u9636\u6bb5\u79f0\u4e3a \u52a8\u6001\u521d\u59cb\u5316(dynamic initialization) \u3002\u8fd9\u4e2a\u9636\u6bb5\u8981\u590d\u6742\u7684\u591a\uff0c\u4f46\u662f\u5b83\u7684\u7cbe\u9ad3\u5728\u4e8e\u5177\u6709\u975e constexpr \u521d\u59cb\u5316\u503c \u7684\u5168\u5c40\u53d8\u91cf\u4f1a\u88ab\u521d\u59cb\u5316\u3002 \u975e constexpr \u521d\u59cb\u5316\u503c \u7684\u4f8b\u5b50\u5982\u4e0b\uff1a 1 2 3 4 5 6 int init () { return 5 ; } int g_something { init () }; // \u975e constexpr \u521d\u59cb\u5316 \u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u5168\u5c40\u53d8\u91cf\u7684\u521d\u59cb\u5316\u987a\u5e8f\u548c\u5b83\u4eec\u88ab\u5b9a\u4e49\u7684\u987a\u5e8f\u4e00\u822c\u662f\u4e00\u81f4\u7684\uff08\u4e5f\u6709\u4e00\u4e9b\u4f8b\u5916\u89c4\u5219\uff09\u3002\u8003\u8651\u5230\u8fd9\u4e00\u70b9\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u53d8\u91cf\u7684\u521d\u59cb\u5316\u503c\u4e0d\u4f9d\u8d56\u4e8e\u540e\u9762\u624d\u521d\u59cb\u5316\u7684\u53d8\u91cf\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int initx (); // forward declaration int inity (); // forward declaration int g_x { initx () }; // g_x \u9996\u5148\u521d\u59cb\u5316 int g_y { inity () }; int initx () { return g_y ; // \u51fd\u6570\u8c03\u7528\u65f6\uff0cg_y \u5e76\u6ca1\u6709\u521d\u59cb\u5316 } int inity () { return 5 ; } int main () { std :: cout << g_x << ' ' << g_y << '\\n' ; } \u6253\u5370\u7ed3\u679c\uff1a 1 0 5 \u66f4\u4e25\u91cd\u7684\u95ee\u9898\u662f\uff0c\u4e0d\u540c\u6587\u4ef6\u4e2d\u5168\u5c40\u53d8\u91cf\u7684\u521d\u59cb\u5316\u987a\u5e8f\u662f\u4e0d\u786e\u5b9a\u7684\u3002\u7ed9\u5b9a\u4e24\u4e2a\u6587\u4ef6 a.cpp \u548c b.cpp \uff0c\u6bcf\u4e2a\u6587\u4ef6\u4e2d\u7684\u5168\u5c40\u53d8\u91cf\u90fd\u53ef\u80fd\u9996\u5148\u521d\u59cb\u5316\u3002\u8fd9\u4e5f\u610f\u5473\u7740\uff0c\u5982\u679c\u6587\u4ef6 a.cpp \u4e2d\u7684\u53d8\u91cf\u4f9d\u8d56 b.cpp \u6587\u4ef6\u4e2d\u7684\u53d8\u91cf\uff0c\u90a3\u4e48\u6709 50%\u7684\u51e0\u7387\u4f1a\u9047\u5230\u53d8\u91cf\u672a\u521d\u59cb\u5316\u7684\u60c5\u51b5\u3002 \u6ce8\u610f \u5168\u5c40\u53d8\u91cf\u7684\u52a8\u6001\u521d\u59cb\u5316\u4f1a\u9020\u6210\u5f88\u5927\u95ee\u9898\uff0c\u5c3d\u91cf\u907f\u514d\u52a8\u6001\u521d\u59cb\u5316\u3002 \u4ec0\u4e48\u65f6\u5019\u53ef\u4ee5\u5408\u7406\u4f7f\u7528\u975e const \u7684\u5168\u5c40\u53d8\u91cf \u8fd9\u79cd\u673a\u4f1a\u5e76\u4e0d\u5927\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u90fd\u53ef\u4ee5\u907f\u514d\u4f7f\u7528\u975e const \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u3002\u4f46\u662f\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5ba1\u614e\u5730\u4f7f\u7528\u975e const \u5168\u5c40\u53d8\u91cf\u53cd\u5230\u53ef\u4ee5\u964d\u4f4e\u7a0b\u5e8f\u7684\u590d\u6742\u5ea6\u3002 \u65e5\u5fd7\u6587\u4ef6\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u4f8b\u5b50\uff0c\u5728\u65e5\u5fd7\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u4f1a\u5b58\u653e\u9519\u8bef\u4fe1\u606f\u548c\u8c03\u8bd5\u4fe1\u606f\uff0c\u56e0\u6b64\u5c06\u5176\u5b9a\u4e49\u4e3a\u5168\u5c40\u53d8\u91cf\u662f\u5408\u7406\u7684\uff0c\u56e0\u4e3a\u4e00\u4e2a\u7a0b\u5e8f\u901a\u5e38\u53ea\u4f1a\u6709\u4e00\u4e2a\u65e5\u5fd7\uff0c\u5e76\u4e14\u5b83\u53ef\u80fd\u5728\u7a0b\u5e8f\u4e2d\u7684\u4efb\u4f55\u90e8\u5206\u88ab\u4f7f\u7528\u3002 \u4e0d\u7ba1\u600e\u4e48\u8bf4\uff0c std::cout \u548c std::cin \u5bf9\u8c61\u4e5f\u88ab\u5b9a\u4e49\u6210\u4e86\u5168\u5c40\u53d8\u91cf(\u5728 std \u547d\u540d\u7a7a\u95f4\u4e2d)\u3002 \u4e00\u822c\u8bf4\u6765\uff0c\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u81f3\u5c11\u8981\u6ee1\u8db3\u4e0b\u9762\u4e24\u4e2a\u6761\u4ef6\uff1a\u8be5\u53d8\u91cf\u5728\u7a0b\u5e8f\u4e2d\u7684\u529f\u80fd\u662f\u552f\u4e00\u7684\uff0c\u800c\u4e14\u5b83\u4f1a\u5728\u7a0b\u5e8f\u7684\u5404\u4e2a\u5730\u65b9\u88ab\u4f7f\u7528\u3002 \u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u4f1a\u9519\u8bef\u5730\u8ba4\u4e3a\uff0c\u56e0\u4e3a\u73b0\u5728\u53ea\u9700\u8981\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u6240\u4ee5\u7528\u7528\u4e5f\u6ca1\u5173\u7cfb\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u4f60\u73b0\u5728\u53ef\u80fd\u5728\u5f00\u53d1\u4e00\u4e2a\u5355\u4eba\u6e38\u620f\uff0c\u6240\u4ee5\u53ea\u9700\u8981\u4e00\u4e2a\u7528\u6237\u3002\u4f46\u662f\u4e07\u4e00\u4ee5\u540e\u4f60\u9700\u8981\u4e3a\u5b83\u6dfb\u52a0\u591a\u4eba\u6a21\u5f0f\u5462\uff1f \u6709\u5173\u5168\u5c40\u53d8\u91cf\u7684\u5fe0\u544a \u5982\u679c\u4f60\u6709\u8db3\u591f\u7684\u7406\u7531\u800c\u4f7f\u7528\u4e86\u4e00\u4e2a\u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u8fd9\u91cc\u6709\u4e00\u4e9b\u5fe0\u544a\u5e0c\u671b\u80fd\u591f\u5e2e\u4f60\u5c3d\u53ef\u80fd\u5730\u907f\u514d\u95ee\u9898\u3002\u5f53\u7136\uff0c\u8fd9\u4e9b\u5fe0\u544a\u4e0d\u4ec5\u4ec5\u53ea\u9488\u5bf9\u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u5b83\u5bf9\u6240\u6709\u7684\u5168\u5c40\u53d8\u91cf\u90fd\u662f\u6709\u610f\u4e49\u7684\u3002 \u9996\u5148\uff0c\u8bf7\u4e3a\u4e0d\u5728\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u5168\u5c40\u53d8\u91cf\u6dfb\u52a0 \u201cg\u201d \u6216\u8005 \u201cg_\u201d \u524d\u7f00\uff0c\u6216\u8005\u5e72\u8106\u5c06\u5b83\u4eec\u653e\u5230\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\uff08\u5728 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u4e2d\u6709\u76f8\u5173\u8ba8\u8bba\uff09\u4ee5\u907f\u514d\u547d\u540d\u51b2\u7a81\u3002 \u4f8b\u5982\uff0c\u4e0d\u8981\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 constexpr double gravity { 9.8 }; // unclear if this is a local or global variable from the name int main () { return 0 ; } \u8981\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 namespace constants { constexpr double gravity { 9.8 }; } int main () { return 0 ; } \u5176\u6b21\uff0c\u4e0d\u8981\u5141\u8bb8\u5168\u5c40\u53d8\u91cf\u7684\u76f4\u63a5\u8bbf\u95ee\uff0c\u6700\u597d\u662f\u5c06\u5b83\u4eec\u201c\u5c01\u88c5\u201d\u8d77\u6765\u3002\u786e\u4fdd\u53d8\u91cf\u53ea\u80fd\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u6587\u4ef6\u5185\u90e8\u88ab\u8bbf\u95ee\uff0c\u4f8b\u5982\u4e3a\u53d8\u91cf\u6dfb\u52a0 static \u6216 const \u4fee\u9970\u7b26\uff0c\u7136\u540e\u63d0\u4f9b\u4e00\u4e2a\u5916\u90e8\u7684\u5168\u5c40\u201c\u8bbf\u95ee\u51fd\u6570\u201d\u7528\u4e8e\u8be5\u53d8\u91cf\u7684\u8bbf\u95ee\u3002\u901a\u8fc7\u8fd9\u4e2a\u8bbf\u95ee\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u53d8\u91cf\u7684\u4f7f\u7528\u65b9\u5f0f\u8fdb\u884c\u7ba1\u7406\uff08\u4f8b\u5982\uff1a\u8f93\u5165\u6821\u9a8c\uff0c\u8303\u56f4\u68c0\u67e5\u7b49\u7b49\uff09\u3002\u6b64\u5916\uff0c\u5982\u679c\u4f60\u4ee5\u540e\u5e0c\u671b\u4fee\u6539\u5e95\u5c42\u5b9e\u73b0\uff08\u4f8b\u5982\u4ece\u4e00\u4e2a\u6570\u636e\u5e93\u8fc1\u79fb\u5230\u53e6\u5916\u4e00\u4e2a\u6570\u636e\u5e93\u65f6\uff09\uff0c\u4f60\u53ea\u9700\u4fee\u6539\u8fd9\u4e2a\u8bbf\u95ee\u51fd\u6570\uff0c\u4f7f\u5176\u8bbf\u95ee\u65b0\u7684\u53d8\u91cf\u5373\u53ef\uff0c\u800c\u65e0\u9700\u66ff\u6362\u6e90\u7801\u4e2d\u6bcf\u5904\u4f7f\u7528\u8be5\u5168\u5c40\u53d8\u91cf\u7684\u5730\u3002 \u4f8b\u5982\uff0c\u4e0d\u8981\u8fd9\u4e48\u505a\uff1a 1 2 3 4 namespace constants { extern const double gravity { 9.8 }; // has external linkage, is directly accessible by other files } \u8981\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 namespace constants { constexpr double gravity { 9.8 }; // has internal linkage, is accessible only by this file } double getGravity () // this function can be exported to other files to access the global outside of this file { // We could add logic here if needed later // or change the implementation transparently to the callers return constants :: gravity ; } \u4e00\u4e2a\u63d0\u9192 \u5168\u5c40 const \u53d8\u91cf\u9ed8\u8ba4\u5177\u6709\u5185\u90e8\u94fe\u63a5\u5c5e\u6027\uff0c\u6240\u4ee5 gravity \u5e76\u4e0d\u9700\u8981\u88ab\u5b9a\u4e49\u4e3a static \u3002 \u7b2c\u4e09\uff0c\u5f53\u51fd\u6570\u5fc5\u987b\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u4e0d\u8981\u76f4\u63a5\u5728\u51fd\u6570\u4f53\u4e2d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3002\u5e94\u8be5\u5c06\u5168\u5c40\u53d8\u91cf\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u8fd9\u6837\u7684\u8bdd\uff0c\u4e0d\u7ba1\u4ec0\u4e48\u65f6\u5019\u4f60\u9700\u8981\u4f7f\u7528\u4e0d\u540c\u7684\u503c\u65f6\uff0c\u4f60\u53ea\u9700\u8981\u4fee\u6539\u51fd\u6570\u7684\u5165\u53c2\u5c31\u53ef\u4ee5\uff0c\u8fd9\u4e48\u505a\u6709\u52a9\u4e8e\u4fdd\u6301\u51fd\u6570\u7684\u6a21\u5757\u5316\u3002 \u4e0d\u8981\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include namespace constants { constexpr double gravity { 9.8 }; } // \u8fd9\u4e2a\u51fd\u6570\u53ea\u80fd\u57fa\u4e8e\u5168\u5c40 gravity \u8ba1\u7b97\u901f\u5ea6 double instantVelocity ( int time ) { return constants :: gravity * time ; } int main () { std :: cout << instantVelocity ( 5 ); } \u8981\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include namespace constants { constexpr double gravity { 9.8 }; } // \u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u57fa\u4e8e\u4efb\u4f55 gravity \u503c\u8ba1\u7b97\u901f\u5ea6\uff08\u66f4\u6709\u7528\uff09 double instantVelocity ( int time , double gravity ) { return gravity * time ; } int main () { std :: cout << instantVelocity ( 5 , constants :: gravity ); // \u5c06\u5e38\u91cf\u5f53\u505a\u53c2\u6570\u4f20\u5165 } \u4e00\u4e2a\u7b11\u8bdd \u95ee\uff1a\u5168\u5c40\u53d8\u91cf\u548c\u4ec0\u4e48\u524d\u7f00\u6700\u914d\uff1f \u7b54: // \uff08\u6ce8\u91ca\uff09 C++ \u7b11\u8bdd\u6700\u68d2\u4e86\uff01","title":"6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c"},{"location":"6-8-Why-non-const-global-variables-are-evil/#const","text":"\u5230\u76ee\u524d\u4f4d\u7f6e\uff0c\u975e const \u7c7b\u578b\u5168\u5c40\u53d8\u91cf\u5371\u9669\u7684\u6700\u5927\u539f\u56e0\uff0c\u662f\u56e0\u4e3a\u5b83\u7684\u503c\u53ef\u4ee5\u88ab\u4efb\u4f55\u51fd\u6570\u4fee\u6539\uff0c\u540c\u65f6\u4e5f\u6ca1\u6709\u7b80\u5355\u7684\u529e\u6cd5\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u5458\u6e05\u695a\u5730\u4e86\u89e3\u5230\u53d1\u751f\u4e86\u4ec0\u4e48\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 int g_mode ; // \u58f0\u660e\u5168\u5c40\u53d8\u91cf (\u9ed8\u8ba4\u4f1a\u521d\u59cb\u5316\u4e3a0) void doSomething () { g_mode = 2 ; // \u5c06\u5168\u5c40\u53d8\u91cf\u8bbe\u7f6e\u4e3a 2 } int main () { g_mode = 1 ; // \u6ce8\u610f\uff1a\u8fd9\u91cc\u53c8\u5c06\u5168\u5c40\u53d8\u91cf g_mode \u53d8\u91cf\u8bbe\u7f6e\u4e3a 1\uff0c\u800c\u5e76\u6ca1\u6709\u58f0\u660e\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf g_mode doSomething (); // \u7a0b\u5e8f\u5458\u6b64\u65f6\u8fd8\u4ee5\u4e3a g_mode \u662f 1 // \u4f46\u662f doSomething \u5df2\u7ecf\u5c06\u5b83\u4fee\u6539\u4e3a 2 \u4e86\u3002 if ( g_mode == 1 ) std :: cout << \"No threat detected. \\n \" ; else std :: cout << \"Launching nuclear missiles... \\n \" ; return 0 ; } \u6ce8\u610f\uff0c\u7a0b\u5e8f\u5458\u9996\u5148\u5c06\u53d8\u91cf g_mode \u8bbe\u7f6e\u4e3a\u4e86 1 \uff0c\u7136\u540e\u5c31\u8c03\u7528\u4e86 doSomething() \u3002\u9664\u975e\u7a0b\u5e8f\u5458\u660e\u786e\u77e5\u9053 doSomething() \u5185\u90e8\u4f1a\u4fee\u6539 g_mode \u7684\u503c\uff0c\u5426\u5219\u4ed6\u53ef\u80fd\u5e76\u4e0d\u4f1a\u60f3\u5230 doSomething() \u4f1a\u4fee\u6539 g_mode \u7684\u503c\uff01\u56e0\u6b64\uff0c\u63a5\u4e0b\u6765 main() \u6240\u505a\u7684\u4e8b\u60c5\u5c31\u4e0d\u50cf\u6211\u4eec\u671f\u671b\u7684\u90a3\u6837\u4e86\u3002 \u7b80\u5355\u6765\u8bf4\uff0c\u5168\u5c40\u53d8\u91cf\u4f1a\u8ba9\u7a0b\u5e8f\u96be\u4ee5\u9884\u6599\u3002 \u7a0b\u5e8f\u4e2d\u7684\u6bcf\u4e2a\u51fd\u6570\u90fd\u53ef\u80fd\u5f88\u5371\u9669\uff0c\u800c\u4e14\u7a0b\u5e8f\u5458\u901a\u5e38\u5e76\u6ca1\u6709\u7b80\u5355\u7684\u529e\u6cd5\u53ef\u4ee5\u5224\u65ad\u54ea\u4e2a\u51fd\u6570\u5371\u9669\u54ea\u4e2a\u4e0d\u5371\u9669\u3002\u5c40\u90e8\u53d8\u91cf\u5c31\u5b89\u5168\u7684\u591a\uff0c\u56e0\u4e3a\u5176\u4ed6\u51fd\u6570\u6ca1\u6709\u529e\u6cd5\u76f4\u63a5\u4fee\u6539\u5b83\u3002 \u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u6709\u5f88\u591a\u5176\u4ed6\u4e0d\u4f7f\u7528\u975econst\u5168\u5c40\u53d8\u91cf\u7684\u7406\u7531\u3002 \u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u7684\u7a0b\u5e8f\u4e2d\uff0c\u4e0b\u9762\u8fd9\u6837\u7684\u51fd\u6570\u5e76\u4e0d\u5c11\u89c1\uff1a 1 2 3 4 5 6 void someFunction () { // useful code if ( g_mode == 4 ) // do something good } \u5f53\u7a0b\u5e8f\u51fa\u73b0\u95ee\u9898\u65f6\uff0c\u4f60\u901a\u8fc7\u8c03\u8bd5\u786e\u5b9a\u4e86\u95ee\u9898\u7684\u539f\u56e0\u662f\u56e0\u4e3a g_mode \u7684\u503c\u662f 3 \u800c\u4e0d\u662f 4\u3002\u6b64\u65f6\u4f60\u5e94\u8be5\u5982\u4f55\u4fee\u590d\u8fd9\u4e2a\u95ee\u9898\u5462\uff1f\u4e3a\u4e86\u4fee\u590d\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f60\u5fc5\u987b\u627e\u5230 g_mode \u53ef\u80fd\u88ab\u8bbe\u7f6e\u4e3a 3 \u7684\u6240\u6709\u7684\u5730\u65b9\uff0c\u7136\u540e\u8ffd\u8e2a\u4ee3\u7801\u8fd0\u884c\uff0c\u5224\u65ad\u5b83\u662f\u4ec0\u4e48\u65f6\u5019\u88ab\u4fee\u6539\u4e3a 3 \u7684\u3002\u800c\u8fd9\u91cc\u9762\u53ef\u80fd\u4f1a\u6d89\u53ca\u5230\u5f88\u591a\u5b8c\u5168\u4e0d\u76f8\u5173\u7684\u4ee3\u7801\u3002 \u4e4b\u524d\u6211\u4eec\u8bf4\u8fc7\uff0c\u5c40\u90e8\u53d8\u91cf\u7684\u58f0\u660e\u5e94\u8be5\u5728\u6700\u63a5\u8fd1\u4f7f\u7528\u5b83\u7684\u5730\u65b9\uff0c\u56e0\u4e3a\u8fd9\u4e48\u505a\u53ef\u4ee5\u5728\u51fa\u73b0\u95ee\u9898\u65f6\uff0c\u6700\u5927\u7a0b\u5ea6\u51cf\u5c11\u4f60\u9700\u8981\u5206\u6790\u7684\u4ee3\u7801\u3002\u5168\u5c40\u53d8\u91cf\u5219\u6b63\u53f7\u76f8\u53cd\u2014\u2014\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u88ab\u4f7f\u7528\uff0c\u6240\u4ee5\u4f60\u5fc5\u987b\u9605\u8bfb\u5168\u90e8\u7684\u4ee3\u7801\u624d\u80fd\u591f\u4e86\u89e3\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u5728\u7b80\u77ed\u7684\u7a0b\u5e8f\u4e2d\u8fd9\u4e5f\u8bb8\u5e76\u4e0d\u662f\u95ee\u9898\uff0c\u4f46\u662f\u5728\u5927\u578b\u7a0b\u5e8f\u4e2d\uff0c\u8fd9\u5c31\u5f88\u9ebb\u70e6\u4e86\u3002 \u4f8b\u5982\uff0c\u4f60\u7684\u7a0b\u5e8f\u53ef\u80fd\u6709 442 \u5904\u4f7f\u7528\u4e86 g_mode \u7684\u5730\u65b9\u3002\u9664\u975e\u6709\u5f88\u597d\u7684\u6587\u6863\u8bf4\u660e\uff0c\u4e0d\u7136\u4f60\u53ef\u80fd\u9700\u8981\u7406\u89e3\u6bcf\u4e00\u5904\u4f7f\u7528\u4e86 g_mode \u7684\u4ee3\u7801\uff0c\u5b83\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u7684\u662f\u5982\u4f55\u4f7f\u7528\u7684\u3001\u6709\u6548\u503c\u662f\u591a\u5c11\u3001\u4e3b\u8981\u529f\u80fd\u662f\u4ec0\u4e48\u3002 \u5168\u5c40\u53d8\u91cf\u8fd8\u4f1a\u7834\u574f\u7a0b\u5e8f\u7684\u6a21\u5757\u6027\u548c\u7075\u6d3b\u6027\u3002\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u503c\u4f7f\u7528\u5176\u53c2\u6570\u4e14\u4e0d\u5177\u6709\u526f\u4f5c\u7528\uff0c\u90a3\u4e48\u5b83\u662f\u5b8c\u5168\u6a21\u5757\u5316\u7684\u3002\u6a21\u5757\u6027\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u7a0b\u5e8f\uff0c\u540c\u65f6\u4e5f\u80fd\u591f\u589e\u5f3a\u7a0b\u5e8f\u7684\u53ef\u590d\u7528\u6027\u3002\u5168\u5c40\u53d8\u91cf\u4f1a\u6781\u5927\u5730\u964d\u4f4e\u7a0b\u5e8f\u7684\u6a21\u5757\u6027\u3002 \u7279\u522b\u5730\uff0c\u907f\u514d\u5c06\u201c\u51b3\u7b56\u70b9\u201d\u53d8\u91cf\uff08\u4f8b\u5982\u6761\u4ef6\u8bed\u53e5\u4e2d\u7684\u53d8\u91cf\uff0c\u5c31\u50cf\u4e0a\u6587\u4e2d\u7684 g_mode \uff09\u5b9a\u4e49\u4e3a\u5168\u5c40\u53d8\u91cf\u3002\u5982\u679c\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u4ec5\u4ec5\u5305\u542b\u67d0\u4e2a\u6570\u636e\uff0c\u4e5f\u8bb8\u5e76\u4e0d\u4f1a\u8ba9\u4f60\u7684\u7a0b\u5e8f\u88ab\u7834\u574f\u3002\u800c\u5982\u679c\u8be5\u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u51b3\u5b9a\u7a0b\u5e8f \u5982\u4f55 \u8fd0\u884c\uff0c\u90a3\u4e48\u5b83\u9020\u6210\u95ee\u9898\u7684\u53ef\u80fd\u6027\u5c31\u4f1a\u5927\u5927\u63d0\u9ad8\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u53ef\u80fd\uff0c\u5c3d\u91cf\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\u800c\u4e0d\u662f\u5168\u5c40\u53d8\u91cf\u3002","title":"\u4e3a\u4ec0\u4e48\u975e const \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c"},{"location":"6-8-Why-non-const-global-variables-are-evil/#_1","text":"\u9759\u6001\u53d8\u91cf\u7684\u521d\u59cb\u5316\uff08\u5176\u4e2d\u5305\u542b\u5168\u5c40\u53d8\u91cf\uff09\u662f\u7a0b\u5e8f\u542f\u52a8\u7684\u4e00\u90e8\u5206\uff0c\u5728\u6267\u884c main \u51fd\u6570\u524d\u5c31\u4f1a\u8fdb\u884c\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u6709\u4e24\u4e2a\u9636\u6bb5\uff1a \u7b2c\u4e00\u9636\u6bb5\u79f0\u4e3a \u9759\u6001\u521d\u59cb\u5316(static initialization) \u3002\u5728\u9759\u6001\u521d\u59cb\u5316\u9636\u6bb5\uff0cconstexpr \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\uff08\u5305\u62ec\u5b57\u9762\u91cf\uff09\u90fd\u4f1a\u88ab\u521d\u59cb\u5316\u6210\u5177\u4f53\u7684\u503c\u3002\u6b64\u5916\uff0c\u6ca1\u6709\u663e\u5f0f\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\u4e5f\u4f1a\u88ab\u521d\u59cb\u5316\u4e3a0\u3002 \u7b2c\u4e8c\u9636\u6bb5\u79f0\u4e3a \u52a8\u6001\u521d\u59cb\u5316(dynamic initialization) \u3002\u8fd9\u4e2a\u9636\u6bb5\u8981\u590d\u6742\u7684\u591a\uff0c\u4f46\u662f\u5b83\u7684\u7cbe\u9ad3\u5728\u4e8e\u5177\u6709\u975e constexpr \u521d\u59cb\u5316\u503c \u7684\u5168\u5c40\u53d8\u91cf\u4f1a\u88ab\u521d\u59cb\u5316\u3002 \u975e constexpr \u521d\u59cb\u5316\u503c \u7684\u4f8b\u5b50\u5982\u4e0b\uff1a 1 2 3 4 5 6 int init () { return 5 ; } int g_something { init () }; // \u975e constexpr \u521d\u59cb\u5316 \u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u5168\u5c40\u53d8\u91cf\u7684\u521d\u59cb\u5316\u987a\u5e8f\u548c\u5b83\u4eec\u88ab\u5b9a\u4e49\u7684\u987a\u5e8f\u4e00\u822c\u662f\u4e00\u81f4\u7684\uff08\u4e5f\u6709\u4e00\u4e9b\u4f8b\u5916\u89c4\u5219\uff09\u3002\u8003\u8651\u5230\u8fd9\u4e00\u70b9\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u53d8\u91cf\u7684\u521d\u59cb\u5316\u503c\u4e0d\u4f9d\u8d56\u4e8e\u540e\u9762\u624d\u521d\u59cb\u5316\u7684\u53d8\u91cf\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include int initx (); // forward declaration int inity (); // forward declaration int g_x { initx () }; // g_x \u9996\u5148\u521d\u59cb\u5316 int g_y { inity () }; int initx () { return g_y ; // \u51fd\u6570\u8c03\u7528\u65f6\uff0cg_y \u5e76\u6ca1\u6709\u521d\u59cb\u5316 } int inity () { return 5 ; } int main () { std :: cout << g_x << ' ' << g_y << '\\n' ; } \u6253\u5370\u7ed3\u679c\uff1a 1 0 5 \u66f4\u4e25\u91cd\u7684\u95ee\u9898\u662f\uff0c\u4e0d\u540c\u6587\u4ef6\u4e2d\u5168\u5c40\u53d8\u91cf\u7684\u521d\u59cb\u5316\u987a\u5e8f\u662f\u4e0d\u786e\u5b9a\u7684\u3002\u7ed9\u5b9a\u4e24\u4e2a\u6587\u4ef6 a.cpp \u548c b.cpp \uff0c\u6bcf\u4e2a\u6587\u4ef6\u4e2d\u7684\u5168\u5c40\u53d8\u91cf\u90fd\u53ef\u80fd\u9996\u5148\u521d\u59cb\u5316\u3002\u8fd9\u4e5f\u610f\u5473\u7740\uff0c\u5982\u679c\u6587\u4ef6 a.cpp \u4e2d\u7684\u53d8\u91cf\u4f9d\u8d56 b.cpp \u6587\u4ef6\u4e2d\u7684\u53d8\u91cf\uff0c\u90a3\u4e48\u6709 50%\u7684\u51e0\u7387\u4f1a\u9047\u5230\u53d8\u91cf\u672a\u521d\u59cb\u5316\u7684\u60c5\u51b5\u3002 \u6ce8\u610f \u5168\u5c40\u53d8\u91cf\u7684\u52a8\u6001\u521d\u59cb\u5316\u4f1a\u9020\u6210\u5f88\u5927\u95ee\u9898\uff0c\u5c3d\u91cf\u907f\u514d\u52a8\u6001\u521d\u59cb\u5316\u3002","title":"\u5168\u5c40\u53d8\u91cf\u7684\u521d\u59cb\u5316\u987a\u5e8f\u95ee\u9898"},{"location":"6-8-Why-non-const-global-variables-are-evil/#const_1","text":"\u8fd9\u79cd\u673a\u4f1a\u5e76\u4e0d\u5927\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u90fd\u53ef\u4ee5\u907f\u514d\u4f7f\u7528\u975e const \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u3002\u4f46\u662f\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5ba1\u614e\u5730\u4f7f\u7528\u975e const \u5168\u5c40\u53d8\u91cf\u53cd\u5230\u53ef\u4ee5\u964d\u4f4e\u7a0b\u5e8f\u7684\u590d\u6742\u5ea6\u3002 \u65e5\u5fd7\u6587\u4ef6\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u4f8b\u5b50\uff0c\u5728\u65e5\u5fd7\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u4f1a\u5b58\u653e\u9519\u8bef\u4fe1\u606f\u548c\u8c03\u8bd5\u4fe1\u606f\uff0c\u56e0\u6b64\u5c06\u5176\u5b9a\u4e49\u4e3a\u5168\u5c40\u53d8\u91cf\u662f\u5408\u7406\u7684\uff0c\u56e0\u4e3a\u4e00\u4e2a\u7a0b\u5e8f\u901a\u5e38\u53ea\u4f1a\u6709\u4e00\u4e2a\u65e5\u5fd7\uff0c\u5e76\u4e14\u5b83\u53ef\u80fd\u5728\u7a0b\u5e8f\u4e2d\u7684\u4efb\u4f55\u90e8\u5206\u88ab\u4f7f\u7528\u3002 \u4e0d\u7ba1\u600e\u4e48\u8bf4\uff0c std::cout \u548c std::cin \u5bf9\u8c61\u4e5f\u88ab\u5b9a\u4e49\u6210\u4e86\u5168\u5c40\u53d8\u91cf(\u5728 std \u547d\u540d\u7a7a\u95f4\u4e2d)\u3002 \u4e00\u822c\u8bf4\u6765\uff0c\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u81f3\u5c11\u8981\u6ee1\u8db3\u4e0b\u9762\u4e24\u4e2a\u6761\u4ef6\uff1a\u8be5\u53d8\u91cf\u5728\u7a0b\u5e8f\u4e2d\u7684\u529f\u80fd\u662f\u552f\u4e00\u7684\uff0c\u800c\u4e14\u5b83\u4f1a\u5728\u7a0b\u5e8f\u7684\u5404\u4e2a\u5730\u65b9\u88ab\u4f7f\u7528\u3002 \u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u4f1a\u9519\u8bef\u5730\u8ba4\u4e3a\uff0c\u56e0\u4e3a\u73b0\u5728\u53ea\u9700\u8981\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u6240\u4ee5\u7528\u7528\u4e5f\u6ca1\u5173\u7cfb\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u4f60\u73b0\u5728\u53ef\u80fd\u5728\u5f00\u53d1\u4e00\u4e2a\u5355\u4eba\u6e38\u620f\uff0c\u6240\u4ee5\u53ea\u9700\u8981\u4e00\u4e2a\u7528\u6237\u3002\u4f46\u662f\u4e07\u4e00\u4ee5\u540e\u4f60\u9700\u8981\u4e3a\u5b83\u6dfb\u52a0\u591a\u4eba\u6a21\u5f0f\u5462\uff1f","title":"\u4ec0\u4e48\u65f6\u5019\u53ef\u4ee5\u5408\u7406\u4f7f\u7528\u975e const \u7684\u5168\u5c40\u53d8\u91cf"},{"location":"6-8-Why-non-const-global-variables-are-evil/#_2","text":"\u5982\u679c\u4f60\u6709\u8db3\u591f\u7684\u7406\u7531\u800c\u4f7f\u7528\u4e86\u4e00\u4e2a\u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u8fd9\u91cc\u6709\u4e00\u4e9b\u5fe0\u544a\u5e0c\u671b\u80fd\u591f\u5e2e\u4f60\u5c3d\u53ef\u80fd\u5730\u907f\u514d\u95ee\u9898\u3002\u5f53\u7136\uff0c\u8fd9\u4e9b\u5fe0\u544a\u4e0d\u4ec5\u4ec5\u53ea\u9488\u5bf9\u975e\u5e38\u91cf\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u5b83\u5bf9\u6240\u6709\u7684\u5168\u5c40\u53d8\u91cf\u90fd\u662f\u6709\u610f\u4e49\u7684\u3002 \u9996\u5148\uff0c\u8bf7\u4e3a\u4e0d\u5728\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u5168\u5c40\u53d8\u91cf\u6dfb\u52a0 \u201cg\u201d \u6216\u8005 \u201cg_\u201d \u524d\u7f00\uff0c\u6216\u8005\u5e72\u8106\u5c06\u5b83\u4eec\u653e\u5230\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\uff08\u5728 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u4e2d\u6709\u76f8\u5173\u8ba8\u8bba\uff09\u4ee5\u907f\u514d\u547d\u540d\u51b2\u7a81\u3002 \u4f8b\u5982\uff0c\u4e0d\u8981\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 constexpr double gravity { 9.8 }; // unclear if this is a local or global variable from the name int main () { return 0 ; } \u8981\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 namespace constants { constexpr double gravity { 9.8 }; } int main () { return 0 ; } \u5176\u6b21\uff0c\u4e0d\u8981\u5141\u8bb8\u5168\u5c40\u53d8\u91cf\u7684\u76f4\u63a5\u8bbf\u95ee\uff0c\u6700\u597d\u662f\u5c06\u5b83\u4eec\u201c\u5c01\u88c5\u201d\u8d77\u6765\u3002\u786e\u4fdd\u53d8\u91cf\u53ea\u80fd\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u6587\u4ef6\u5185\u90e8\u88ab\u8bbf\u95ee\uff0c\u4f8b\u5982\u4e3a\u53d8\u91cf\u6dfb\u52a0 static \u6216 const \u4fee\u9970\u7b26\uff0c\u7136\u540e\u63d0\u4f9b\u4e00\u4e2a\u5916\u90e8\u7684\u5168\u5c40\u201c\u8bbf\u95ee\u51fd\u6570\u201d\u7528\u4e8e\u8be5\u53d8\u91cf\u7684\u8bbf\u95ee\u3002\u901a\u8fc7\u8fd9\u4e2a\u8bbf\u95ee\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u53d8\u91cf\u7684\u4f7f\u7528\u65b9\u5f0f\u8fdb\u884c\u7ba1\u7406\uff08\u4f8b\u5982\uff1a\u8f93\u5165\u6821\u9a8c\uff0c\u8303\u56f4\u68c0\u67e5\u7b49\u7b49\uff09\u3002\u6b64\u5916\uff0c\u5982\u679c\u4f60\u4ee5\u540e\u5e0c\u671b\u4fee\u6539\u5e95\u5c42\u5b9e\u73b0\uff08\u4f8b\u5982\u4ece\u4e00\u4e2a\u6570\u636e\u5e93\u8fc1\u79fb\u5230\u53e6\u5916\u4e00\u4e2a\u6570\u636e\u5e93\u65f6\uff09\uff0c\u4f60\u53ea\u9700\u4fee\u6539\u8fd9\u4e2a\u8bbf\u95ee\u51fd\u6570\uff0c\u4f7f\u5176\u8bbf\u95ee\u65b0\u7684\u53d8\u91cf\u5373\u53ef\uff0c\u800c\u65e0\u9700\u66ff\u6362\u6e90\u7801\u4e2d\u6bcf\u5904\u4f7f\u7528\u8be5\u5168\u5c40\u53d8\u91cf\u7684\u5730\u3002 \u4f8b\u5982\uff0c\u4e0d\u8981\u8fd9\u4e48\u505a\uff1a 1 2 3 4 namespace constants { extern const double gravity { 9.8 }; // has external linkage, is directly accessible by other files } \u8981\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 namespace constants { constexpr double gravity { 9.8 }; // has internal linkage, is accessible only by this file } double getGravity () // this function can be exported to other files to access the global outside of this file { // We could add logic here if needed later // or change the implementation transparently to the callers return constants :: gravity ; }","title":"\u6709\u5173\u5168\u5c40\u53d8\u91cf\u7684\u5fe0\u544a"},{"location":"6-8-Why-non-const-global-variables-are-evil/#_3","text":"\u5168\u5c40 const \u53d8\u91cf\u9ed8\u8ba4\u5177\u6709\u5185\u90e8\u94fe\u63a5\u5c5e\u6027\uff0c\u6240\u4ee5 gravity \u5e76\u4e0d\u9700\u8981\u88ab\u5b9a\u4e49\u4e3a static \u3002 \u7b2c\u4e09\uff0c\u5f53\u51fd\u6570\u5fc5\u987b\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u4e0d\u8981\u76f4\u63a5\u5728\u51fd\u6570\u4f53\u4e2d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3002\u5e94\u8be5\u5c06\u5168\u5c40\u53d8\u91cf\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u8fd9\u6837\u7684\u8bdd\uff0c\u4e0d\u7ba1\u4ec0\u4e48\u65f6\u5019\u4f60\u9700\u8981\u4f7f\u7528\u4e0d\u540c\u7684\u503c\u65f6\uff0c\u4f60\u53ea\u9700\u8981\u4fee\u6539\u51fd\u6570\u7684\u5165\u53c2\u5c31\u53ef\u4ee5\uff0c\u8fd9\u4e48\u505a\u6709\u52a9\u4e8e\u4fdd\u6301\u51fd\u6570\u7684\u6a21\u5757\u5316\u3002 \u4e0d\u8981\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include namespace constants { constexpr double gravity { 9.8 }; } // \u8fd9\u4e2a\u51fd\u6570\u53ea\u80fd\u57fa\u4e8e\u5168\u5c40 gravity \u8ba1\u7b97\u901f\u5ea6 double instantVelocity ( int time ) { return constants :: gravity * time ; } int main () { std :: cout << instantVelocity ( 5 ); } \u8981\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include namespace constants { constexpr double gravity { 9.8 }; } // \u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u57fa\u4e8e\u4efb\u4f55 gravity \u503c\u8ba1\u7b97\u901f\u5ea6\uff08\u66f4\u6709\u7528\uff09 double instantVelocity ( int time , double gravity ) { return gravity * time ; } int main () { std :: cout << instantVelocity ( 5 , constants :: gravity ); // \u5c06\u5e38\u91cf\u5f53\u505a\u53c2\u6570\u4f20\u5165 }","title":"\u4e00\u4e2a\u63d0\u9192"},{"location":"6-8-Why-non-const-global-variables-are-evil/#_4","text":"\u95ee\uff1a\u5168\u5c40\u53d8\u91cf\u548c\u4ec0\u4e48\u524d\u7f00\u6700\u914d\uff1f \u7b54: // \uff08\u6ce8\u91ca\uff09 C++ \u7b11\u8bdd\u6700\u68d2\u4e86\uff01","title":"\u4e00\u4e2a\u7b11\u8bdd"},{"location":"6-9-Sharing-global-constants-across-multiple-files-using-inline-variables/","tags":["inline","global","C++17"],"text":"Key Takeaway \u5168\u5c40\u5e38\u91cf\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u5b83\u662f\u5185\u90e8\u53d8\u91cf\uff0c\u5934\u6587\u4ef6\u5305\u542b\u65f6\u4f1a\u88ab\u590d\u5236\u5230\u5230\u6e90\u6587\u4ef6\u4e2d\u3002 \u5f53\u5168\u5c40\u53d8\u91cf\u4e3a\u5185\u90e8\u53d8\u91cf\u65f6\uff0c\u4f1a\u5bfc\u81f4\u4e24\u4e2a\u95ee\u9898\uff1a \u4fee\u6539\u540e\u6240\u6709\u5305\u542b\u8be5\u5934\u6587\u4ef6\u7684\u6587\u4ef6\u90fd\u8981\u91cd\u65b0\u7f16\u8bd1 \u53d8\u91cf\u590d\u5236\u5360\u7528\u5927\u91cf\u5185\u5b58 \u5c06\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u5728cpp\u6587\u4ef6\u4e2d\uff0c\u5e76\u5728\u5934\u6587\u4ef6\u4e2d\u5bf9\u5176\u8fdb\u884c\u524d\u5411\u58f0\u660e\uff0c\u7136\u540e\u518d\u9700\u8981\u5b83\u7684\u5730\u65b9\u5305\u542b\u5934\u6587\u4ef6\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5168\u5c40\u53d8\u91cf\u4e3a\u5916\u90e8\u53d8\u91cf\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e constexpr \u4e0d\u80fd\u88ab \u524d\u5411\u58f0\u660e \uff0c\u56e0\u6b64\u53ea\u80fd\u4f7f\u7528 const \u3002 \u5f53\u5168\u5c40\u53d8\u91cf\u4e3a\u5916\u90e8\u53d8\u91cf\u65f6\uff0c\u4e5f\u4f1a\u5bfc\u81f4\u4e24\u4e2a\u95ee\u9898\uff1a \u8fd9\u4e9b\u53d8\u91cf\u53ea\u6709\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u6587\u4ef6\u4e2d\u5c5e\u4e8e\u7f16\u8bd1\u65f6\u5e38\u91cf\uff0c\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u90fd\u5c5e\u4e8e\u8fd0\u884c\u65f6\u5e38\u91cf\u3002\u56e0\u6b64\u5728\u9700\u8981\u4f7f\u7528\u7f16\u8bd1\u65f6\u5e38\u91cf\u7684\u5730\u65b9\u4e0d\u80fd\u4f7f\u7528\u5b83\u4eec\u3002 \u8fd0\u884c\u65f6\u5e38\u91cf\u7684\u4f18\u5316\u4e0d\u5982\u7f16\u8bd1\u65f6\u5e38\u91cf \u4e3a\u4e86\u5728\u7f16\u8bd1\u65f6\u4f7f\u7528\u67d0\u4e9b\u53d8\u91cf\uff08\u4f8b\u5982\uff0c\u6570\u7ec4\u5927\u5c0f\uff09\uff0c\u7f16\u8bd1\u5668\u5fc5\u987b\u770b\u5230\u53d8\u91cf\u7684\u5b9a\u4e49\uff08\u800c\u4e0d\u4ec5\u4ec5\u662f\u524d\u9879\u58f0\u660e\uff09\u3002 C++17 \u5f15\u5165\u4e86\u5185\u8054\u53d8\u91cf\uff0c\u5373\u53ef\u4ee5\u88ab\u591a\u5904\u5b9a\u4e49\u7684\u53d8\u91cf\u3002\u7f16\u8bd1\u5668\u4f1a\u5c06\u5b83\u4eec\u7684\u5b9a\u4e49\u5408\u5e76\u3002\u540c\u65f6\u5b83\u8fd8\u5177\u6709\u548c constexpr \u540c\u6837\u7684\u5c5e\u6027\u3002 \u5185\u8054\u53d8\u91cf\u6709\u4e24\u4e2a\u4e3b\u8981\u7684\u9650\u5236\uff0c\u9700\u8981\u6211\u4eec\u9075\u5faa\uff1a \u5185\u8054\u53d8\u91cf\u7684\u6240\u6709\u5b9a\u4e49\u5fc5\u987b\u662f\u5b8c\u5168\u4e00\u81f4\u7684\uff08\u5426\u5219\u4f1a\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \uff09\u3002 \u5185\u8054\u53d8\u91cf\u7684\u5b9a\u4e49\uff08\u800c\u975e\u58f0\u660e\uff09\u5fc5\u987b\u51fa\u73b0\u5728\u4efb\u4f55\u4f7f\u7528\u5b83\u4eec\u7684\u6587\u4ef6\u4e2d\u3002 \u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u652f\u6301 C++17\uff0c\u6700\u597d\u5c06\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u4e3a inline constexpr \u7c7b\u578b\u5e76\u653e\u7f6e\u5728\u5934\u6587\u4ef6\u4e2d\u3002 \u5728\u5f88\u591a\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u6709\u4e9b\u7b26\u53f7\u5e38\u91cf\u9700\u8981\u88ab\u6240\u6709\u7684\u4ee3\u7801\u4f7f\u7528\uff08\u800c\u4e0d\u4ec5\u4ec5\u662f\u88ab\u5c40\u90e8\u7684\u4ee3\u7801\u4f7f\u7528\uff09\u3002\u8fd9\u4e9b\u53d8\u91cf\u53ef\u80fd\u662f\u7269\u7406\u5e38\u91cf\u6216\u6570\u5b66\u5e38\u91cf\uff08\u4f8b\u5982 \u03c0 \u6216\u963f\u4f0f\u4f3d\u5fb7\u7f57\u5e38\u6570\uff09\uff0c\u6216\u8005\u662f\u67d0\u4e2a\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u7684\u53c2\u6570\uff08\u4f8b\u5982\u6469\u64e6\u7cfb\u6570\u6216\u5f15\u529b\u7cfb\u6570\uff09\u3002\u4e0e\u5176\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u5404\u5b9a\u4e49\u4e00\u904d\u8fd9\u4e9b\u53d8\u91cf\uff0c\u4e0d\u5982\u5c06\u5b83\u4eec\u96c6\u4e2d\u5b9a\u4e49\u5728\u4e00\u4e2a\u5730\u65b9\u7136\u540e\u6309\u9700\u4f7f\u7528\u3002\u8fd9\u6837\uff0c\u4e07\u4e00\u4f60\u9700\u8981\u4fee\u6539\u5b83\u4eec\u7684\u503c\uff0c\u4f60\u53ea\u9700\u8981\u5728\u4e00\u5904\u4fee\u6539\u5373\u53ef\u3002 \u672c\u8282\u8bfe\u4f1a\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u4e0a\u8ff0\u5168\u5c40\u5e38\u91cf\u7684\u5b9a\u4e49\u3002 \u5c06\u5168\u5c40\u5e38\u91cf\u4f5c\u4e3a\u5185\u90e8\u53d8\u91cf \u5728 C++17 \u4e4b\u524d\uff0c\u5b9a\u4e49\u5168\u5c40\u5e38\u91cf\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a \u5b9a\u4e49\u4e00\u4e2a\u5934\u6587\u4ef6\u6765\u5b58\u653e\u8fd9\u4e9b\u5e38\u91cf \u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4 (\u53c2\u89c1\uff1a 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u5c06\u5168\u90e8\u7684\u5e38\u91cf\u653e\u5230\u8be5\u547d\u540d\u7a7a\u95f4\u4e2d\uff08\u540c\u65f6\u786e\u4fdd\u5b83\u4eec\u90fd\u662f constexpr \u7c7b\u578b\u7684\uff09 \u5728\u9700\u8981\u4f7f\u7528\u8fd9\u4e9b\u5e38\u91cf\u7684\u5730\u65b9\uff0c #include \u8be5\u5934\u6587\u4ef6 constants.h 1 2 3 4 5 6 7 8 9 10 11 12 13 #ifndef CONSTANTS_H #define CONSTANTS_H // \u5b9a\u4e49\u5305\u542b\u5e38\u91cf\u7684\u547d\u540d\u7a7a\u95f4 namespace constants { // \u9ed8\u8ba4\u5185\u90e8\u94fe\u63a5 constexpr double pi { 3.14159 }; constexpr double avogadro { 6.0221413e23 }; constexpr double myGravity { 9.2 }; // m/s^2 -- gravity is light on this planet // ... other related constants } #endif \u7136\u540e\u5728 .cpp \u6587\u4ef6\u4e2d\u4f7f\u7528 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 ( :: )\u6765\u4f7f\u7528\u8fd9\u4e9b\u5e38\u91cf\uff0c\u8fd0\u7b97\u7b26\u5de6\u4fa7\u662f\u547d\u540d\u7a7a\u95f4\uff0c\u53f3\u4fa7\u662f\u5e38\u91cf\u540d\uff1a main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include \"constants.h\" // include a copy of each constant in this file #include int main () { std :: cout << \"Enter a radius: \" ; int radius {}; std :: cin >> radius ; std :: cout << \"The circumference is: \" << 2.0 * radius * constants :: pi << '\\n' ; return 0 ; } \u5f53\u5934\u6587\u4ef6\u88ab #included \u5230\u4e86 a .cpp \u4e2d\u540e\uff0c\u5934\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u5e38\u91cf\u5b9e\u9645\u4e0a\u90fd\u4f1a\u88ab \u62f7\u8d1d \u5230\u6e90\u6587\u4ef6\u4e2d\u3002\u56e0\u4e3a\u8fd9\u4e9b\u53d8\u91cf\u4e0d\u5c5e\u4e8e\u4efb\u4f55\u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u4eec\u5728\u88ab\u5305\u542b\u5230\u6e90\u6587\u4ef6\u540e\uff0c\u4f1a\u88ab\u5f53\u505a\u5168\u5c40\u53d8\u91cf\u5bf9\u5f85\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u5728\u6587\u4ef6\u7684\u4efb\u4f55\u5730\u65b9\u4f7f\u7528\u5b83\u4eec\u3002 \u56e0\u4e3a const \u5168\u5c40\u53d8\u91cf\u5177\u6709 \u5185\u90e8\u94fe\u63a5 \u5c5e\u6027\uff0c\u56e0\u6b64\u6bcf\u4e2a\u53d8\u91cf\u5728.cpp\u6587\u4ef6\u4e4b\u95f4\u662f\u72ec\u7acb\u7684\uff0c\u800c\u4e14\u94fe\u63a5\u5668\u5e76\u4e0d\u80fd\u770b\u5230\u5b83\u4eec\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7531\u4e8e\u5b83\u4eec\u662f\u5e38\u91cf\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u5c06\u5176\u201c\u4f18\u5316\u6389\u201d\u3002 \u9898\u5916\u8bdd \u201c\u4f18\u5316\u6389\u201d\u8fd9\u4e2a\u672f\u8bed\uff0c\u6307\u7684\u662f\u7f16\u8bd1\u5668\u4e3a\u4e86\u4f18\u5316\u7a0b\u5e8f\u6027\u80fd\u800c\u5220\u9664\u4e86\u67d0\u4e9b\u4e1c\u897f\uff0c\u4f46\u4e0d\u4f1a\u5bf9\u7a0b\u5e8f\u7684\u529f\u80fd\u4ea7\u751f\u5f71\u54cd\u3002\u4f8b\u5982\uff0c\u4f60\u5b9a\u4e49\u4e86\u4e00\u4e2a const \u7c7b\u578b\u7684\u53d8\u91cf x \u5e76\u4e14\u5c06\u5176\u521d\u59cb\u5316\u4e3a 4\u3002\u5219\u7f16\u8bd1\u5668\u4f1a\u5c06\u4efb\u4f55\u5f15\u7528\u8be5\u53d8\u91cf\u7684\u5730\u65b9\uff0c\u76f4\u63a5\u66ff\u6362\u4e3a 4\uff08\u56e0\u4e3a x \u662fconst\u7c7b\u578b\u7684\uff0c\u6240\u4ee5\u5b83\u4e0d\u80fd\u88ab\u4fee\u6539\uff09\uff0c\u4ece\u800c\u907f\u514d\u4e86\u53d8\u91cf\u7684\u5b9a\u4e49\u548c\u521d\u59cb\u5316\u8fc7\u7a0b\u3002 \u5c06\u5168\u5c40\u5e38\u91cf\u4f5c\u4e3a\u5916\u90e8\u53d8\u91cf \u4e0a\u9762\u63d0\u5230\u7684\u65b9\u6cd5\u5b9e\u9645\u4e0a\u6709\u4e00\u4e9b\u7f3a\u9677\u3002 \u5c3d\u7ba1\u5c06 constants.h \u5934\u6587\u4ef6 #included \u5230\u6bcf\u4e00\u4e2a\u9700\u8981\u5b83\u7684\u6e90\u6587\u4ef6\u4e2d\u662f\u975e\u5e38\u7b80\u5355\uff08\u5bf9\u4e8e\u5c0f\u7a0b\u5e8f\u6765\u8bf4\u4e5f\u901a\u5e38\u4e0d\u4f1a\u6709\u4ec0\u4e48\u95ee\u9898\uff09\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u4f46\u6bd5\u7adf\u8fd9\u4f1a\u5bfc\u81f4\u53d8\u91cf\u88ab\u590d\u5236\u5230\u6e90\u6587\u4ef6\u4e2d\u3002\u56e0\u6b64\uff0c\u5982\u679c constants.h \u88ab 20 \u4e2a\u6587\u4ef6\u6240\u5305\u542b\uff0c\u90a3\u4e48\u8fd9\u4e9b\u5168\u5c40\u53d8\u91cf\u5c31\u4f1a\u88ab\u590d\u5236 20 \u6b21\u3002 \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e \u5e76\u4e0d\u80fd\u9632\u6b62\u8be5\u95ee\u9898\u7684\u53d1\u751f\uff0c\u56e0\u4e3a\u5b83\u53ea\u80fd\u4fdd\u8bc1\u5934\u6587\u4ef6\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u88ab\u5305\u542b\u4e00\u6b21\uff0c\u800c\u4e0d\u662f\u963b\u6b62\u5934\u6587\u4ef6\u88ab\u591a\u4e2a\u4e0d\u540c\u7684\u6587\u4ef6\u5305\u542b\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u4f1a\u9762\u4e34\u4e24\u4e2a\u95ee\u9898\uff1a \u4fee\u6539\u5e38\u91cf\u4f1a\u5bfc\u81f4\u6240\u6709\u5305\u542b\u8be5\u5934\u6587\u4ef6\u7684\u6587\u4ef6\u91cd\u65b0\u7f16\u8bd1\uff0c\u5bf9\u4e8e\u5927\u578b\u9879\u76ee\u6765\u8bf4\u8fd9\u4f1a\u6d6a\u8d39\u5f88\u591a\u65f6\u95f4 \u5982\u679c\u5e38\u91cf\u5f88\u5927\uff0c\u800c\u4e14\u65e0\u6cd5\u88ab\u201c\u4f18\u5316\u6389\u201d\uff0c\u90a3\u4e48\u5927\u91cf\u7684\u590d\u5236\u4f1a\u5bfc\u81f4\u5927\u91cf\u7684\u5185\u5b58\u5360\u7528\u3002 \u907f\u514d\u8fd9\u4e2a\u95ee\u9898\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u662f\u5c06\u5168\u5c40\u5e38\u91cf\u5b9a\u4e49\u4e3a \u5916\u90e8\u53d8\u91cf \uff0c\u8fd9\u6837\u4e00\u4e2a\u53d8\u91cf\uff08\u521d\u59cb\u5316\u4e00\u6b21\uff09\u5c31\u53ef\u4ee5\u88ab\u6240\u6709\u7684\u6587\u4ef6\u201c\u5171\u4eab\u4f7f\u7528\u201d\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5fc5\u987b\u5c06\u5e38\u91cf\u5b9a\u4e49\u5728\u4e00\u4e2a a.cpp \u6587\u4ef6\u4e2d\uff08\u786e\u4fdd\u5b83\u53ea\u88ab\u5b9a\u4e49\u4e00\u6b21\uff09\uff0c\u7136\u540e\u5728\u5934\u6587\u4ef6\u4e2d\u6dfb\u52a0\u5176\u524d\u5411\u5b9a\u4e49\uff08\u8be5\u5934\u6587\u4ef6\u4ecd\u9700\u8981\u88ab\u5305\u542b\u5230\u4f7f\u7528\u4e86\u8be5\u53d8\u91cf\u7684\u6587\u4ef6\u4e2d\uff09\u3002 \u4f5c\u8005\u6ce8 \u8fd9\u91cc\u4f7f\u7528\u4e86 const \u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u8fd9\u662f\u56e0\u4e3a constexpr \u7c7b\u578b\u7684\u53d8\u91cf\u4e0d\u80fd\u88ab\u524d\u5411\u58f0\u660e\uff08 6-7-External-linkage-and-variable-forward-declarations#^ce6263 \uff09\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u9700\u8981\u5728\u7f16\u8bd1\u65f6\u77e5\u9053 constexpr \u7c7b\u578b\u53d8\u91cf\u7684\u5b9a\u4e49\uff0c\u800c\u524d\u5411\u58f0\u660e\u5e76\u4e0d\u80fd\u63d0\u4f9b\u5b9a\u4e49\u7684\u4fe1\u606f\u3002 constants.cpp 1 2 3 4 5 6 7 8 9 #include \"constants.h\" namespace constants { // actual global variables extern const double pi { 3.14159 }; extern const double avogadro { 6.0221413e23 }; extern const double myGravity { 9.2 }; // m/s^2 -- gravity is light on this planet } constants.h 1 2 3 4 5 6 7 8 9 10 11 12 #ifndef CONSTANTS_H #define CONSTANTS_H namespace constants { // since the actual variables are inside a namespace, the forward declarations need to be inside a namespace as well extern const double pi ; extern const double avogadro ; extern const double myGravity ; } #endif \u4f7f\u7528\u65b9\u6cd5\u548c\u4e4b\u524d\u662f\u4e00\u6837\u7684\uff1a main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include \"constants.h\" // include all the forward declarations #include int main () { std :: cout << \"Enter a radius: \" ; int radius {}; std :: cin >> radius ; std :: cout << \"The circumference is: \" << 2.0 * radius * constants :: pi << '\\n' ; return 0 ; } \u56e0\u4e3a\u5168\u5c40\u7b26\u53f7\u5e38\u91cf\u5e94\u8be5\u88ab\u653e\u7f6e\u5728\u547d\u540d\u7a7a\u95f4\u4e2d\uff08\u907f\u514d\u4e0e\u5176\u4ed6\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u4ea7\u751f\u547d\u540d\u51b2\u7a81\uff09\uff0c\u6240\u4ee5\u6ca1\u5fc5\u8981\u5bf9\u5176\u4f7f\u7528 g_ \u524d\u7f00\u3002 \u8fd9\u6837\u4e00\u6765\uff0c\u7b26\u53f7\u5e38\u91cf\u53ea\u4f1a\u5728 constants.cpp \u4e2d\u88ab\u521d\u59cb\u5316\u4e00\u6b21\uff0c\u800c\u4e0d\u662f\u5728\u6bcf\u4e2a constants.h \u88ab\u5305\u542b\u7684\u5730\u65b9\u88ab\u521d\u59cb\u5316\u3002\u540c\u65f6\uff0c\u8fd9\u4e9b\u5e38\u6570\u90fd\u4f1a\u88ab\u94fe\u63a5\u5230 constants.cpp \u4e2d\u521d\u59cb\u5316\u7684\u53d8\u91cf\uff0c\u56e0\u6b64\u5f53\u8fd9\u4e9b\u53d8\u91cf\u88ab\u4fee\u6539\u65f6\uff0c\u53ea\u6709 constants.cpp \u9700\u8981\u88ab\u91cd\u65b0\u7f16\u8bd1\u3002 \u4e0d\u8fc7\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u4e0d\u662f\u5b8c\u7f8e\u7684\u3002\u9996\u5148\uff0c\u8fd9\u4e9b\u5e38\u91cf\u73b0\u5728\u53ea\u4f1a\u5728\u5b83\u4eec\u88ab\u5b9a\u4e49\u7684\u6587\u4ef6\u4e2d\u88ab\u770b\u505a \u7f16\u8bd1\u65f6 \u5e38\u91cf\u3002\u800c\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\uff0c\u7f16\u8bd1\u5668\u53ea\u80fd\u770b\u5230\u5b83\u4eec\u7684\u58f0\u660e\uff0c\u800c\u770b\u4e0d\u5230\u5e38\u91cf\u5177\u4f53\u7684\u4e2d\uff08\u5fc5\u987b\u6709\u94fe\u63a5\u5668\u8fdb\u884c\u89e3\u6790\uff09\u3002\u56e0\u6b64\uff0c\u5728 constants.cpp \u4ee5\u5916\u7684\u5730\u65b9\uff0c\u8fd9\u4e9b\u5e38\u91cf\u90fd\u4e0d\u80fd\u88ab\u770b\u505a\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\uff0c\u4e5f\u5c31\u4e0d\u80fd\u5728\u8981\u6c42\u4f7f\u7528\u7f16\u8bd1\u65f6\u5e38\u91cf\u7684\u5730\u65b9\u4f7f\u7528\u5b83\u4eec\u3002\u53e6\u5916\uff0c\u56e0\u4e3a\u7f16\u8bd1\u65f6\u5e38\u91cf\u76f8\u5bf9\u4e8e\u8fd0\u884c\u65f6\u5e38\u91cf\u66f4\u5bb9\u6613\u4f18\u5316\uff0c\u6240\u4ee5\u5bf9\u4e8e\u8fd9\u4e9b\u8fd0\u884c\u65f6\u5e38\u91cf\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u65e0\u6cd5\u5bf9\u5176\u8fdb\u884c\u8db3\u591f\u7684\u4f18\u5316\u3002 \u5173\u952e\u4fe1\u606f \u4e3a\u4e86\u5728\u7f16\u8bd1\u65f6\u4f7f\u7528\u67d0\u4e9b\u53d8\u91cf\uff08\u4f8b\u5982\uff0c\u6570\u7ec4\u5927\u5c0f\uff09\uff0c\u7f16\u8bd1\u5668\u5fc5\u987b\u770b\u5230\u53d8\u91cf\u7684\u5b9a\u4e49\uff08\u800c\u4e0d\u4ec5\u4ec5\u662f\u524d\u9879\u58f0\u660e\uff09\u3002 \u56e0\u4e3a\u7f16\u8bd1\u5668\u662f\u72ec\u7acb\u7f16\u8bd1\u5404\u4e2a\u6e90\u6587\u4ef6\u7684\uff0c\u56e0\u6b64\u5b83\u53ea\u80fd\u5bf9\u6e90\u6587\u4ef6\u4e2d\u5b58\u5728\u5b9a\u4e49\u7684\u53d8\u91cf\u8fdb\u884c\u7f16\u8bd1\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49\u5728 constants.cpp \u7684\u53d8\u91cf\uff0c\u5728\u7f16\u8bd1\u5668\u7f16\u8bd1 main.cpp \u7684\u65f6\u5019\u662f\u4e0d\u53ef\u89c1\u7684\uff0c\u51fa\u4e8e\u8fd9\u4e2a\u539f\u56e0 constexpr \u7684\u5b9a\u4e49\u548c\u58f0\u660e\u4e0d\u80fd\u88ab\u5206\u6563\u5230\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u4e2d\uff0c\u5b83\u5fc5\u987b\u88ab\u5b9a\u4e49\u5728\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\u91cc\u3002 \u51fa\u4e8e\u4e0a\u8ff0\u539f\u56e0\uff0c\u6211\u4eec\u8fd8\u662f\u5efa\u8bae\u6700\u597d\u5c06\u5e38\u91cf\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u3002\u5982\u679c\u56e0\u6b64\u5bfc\u81f4\u4e86\u67d0\u4e9b\u7279\u5b9a\u7684\u95ee\u9898\uff0c\u4e0d\u59a8\u5230\u65f6\u5019\u5728\u628a\u5b83\u4eec\u4e00\u8d77\u79fb\u52a8\u5230.cpp\u6587\u4ef6\u53bb\u3002 \u5c06\u5168\u5c40\u5e38\u91cf\u4f5c\u4e3a\u5185\u8054\u53d8\u91cf (C++17) C++17 \u5f15\u5165\u4e86\u4e00\u4e2a\u65b0\u7684\u6982\u5ff5\u2014\u2014 \u5185\u8054\u53d8\u91cf(inline variables) \u3002\u5728 C++ \u4e2d\uff0c[[inline|\u5185\u8054]]\u7684\u610f\u601d\u9010\u6e10\u5f15\u7533\u6210\u4e86\u201c\u5141\u8bb8\u591a\u5904\u5b9a\u4e49\u201d\u3002\u56e0\u6b64\uff0c\u5185\u8054\u53d8\u91cf\u53ef\u4ee5\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u88ab\u591a\u6b21\u5b9a\u4e49\uff0c\u4e14\u4e0d\u4f1a\u8fdd\u53cd \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u3002\u5185\u8054\u7684\u5168\u5c40\u53d8\u91cf\u9ed8\u8ba4\u5177\u6709\u5916\u90e8\u94fe\u63a5\u5c5e\u6027\u3002 \u94fe\u63a5\u5668\u53ef\u4ee5\u5c06\u6240\u6709\u7684\u5185\u8054\u5b9a\u4e49\u5408\u5e76\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u5b9a\u4e49\uff08\u8fd9\u6837\u5c31\u6ee1\u8db3\u4e86\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u5f53\u6211\u4eec\u628a\u5185\u8054\u53d8\u91cf\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u7684\u540c\u65f6\uff0c\u8fd8\u80fd\u591f\u5c06\u5b83\u4eec\u5728.cpp\u6587\u4ef6\u4e2d\u7684\u5b9a\u4e49\u770b\u505a\u662f\u4e00\u4e2a\u5b9a\u4e49\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4e00\u4e2a\u666e\u901a\u7684\u5e38\u91cf\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u540c\u65f6\u88ab #include \u5230\u4e8610\u4e2a\u4e0d\u540c\u7684\u6e90\u6587\u4ef6\u3002\u5982\u679c\u5b83\u4e0d\u662f\u5185\u8054\u7684\uff0c\u5219\u6b64\u65f6\u4f1a\u4ea7\u751f10\u4e2a\u5b9a\u4e49\u3002\u5982\u679c\u662f\u5185\u8054\u7684\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u9009\u53d6\u4e00\u4e2a\u5b9a\u4e49\u4f5c\u4e3a\u4e3b\u5b9a\u4e49\uff0c\u8fd9\u6837\u4e00\u6765\u5c31\u53ea\u4f1a\u4ea7\u751f\u4e00\u4e2a\u5b9a\u4e49\u3002\u8fd9\u6837\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8282\u77019\u4e2a\u5e38\u91cf\u7684\u5185\u5b58\u7a7a\u95f4\u3002 \u8fd9\u4e9b\u53d8\u91cf\u540c\u65f6\u8fd8\u4fdd\u7559\u4e86 constexpr \u7684\u7279\u6027\uff0c\u56e0\u6b64\u5728\u4efb\u4f55\u5305\u542b\u5b83\u4eec\u7684\u6587\u4ef6\u4e2d\uff0c\u53ea\u8981\u662f constexpr \u53ef\u7528\u7684\u5730\u65b9\uff0c\u8fd9\u4e9b\u53d8\u91cf\u4e5f\u53ef\u7528\u3002\u540c\u65f6\uff0c\u76f8\u6bd4\u8f83\u4e8e\u8fd0\u884c\u65f6\u5e38\u91cf\uff08\u6216\u975e\u5e38\u91cf\uff09\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u6781\u5927\u5730\u4f18\u5316 constexpr \u7c7b\u578b\u7684\u53d8\u91cf \u5185\u8054\u53d8\u91cf\u6709\u4e24\u4e2a\u4e3b\u8981\u7684\u9650\u5236\uff0c\u9700\u8981\u6211\u4eec\u9075\u5faa\uff1a \u5185\u8054\u53d8\u91cf\u7684\u6240\u6709\u5b9a\u4e49\u5fc5\u987b\u662f\u5b8c\u5168\u4e00\u81f4\u7684\uff08\u5426\u5219\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\uff09\u3002 \u5185\u8054\u53d8\u91cf\u7684\u5b9a\u4e49\uff08\u800c\u975e\u58f0\u660e\uff09\u5fc5\u987b\u51fa\u73b0\u5728\u4efb\u4f55\u4f7f\u7528\u5b83\u4eec\u7684\u6587\u4ef6\u4e2d\u3002 \u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u7ee7\u7eed\u6cbf\u7528\u5c06\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u7684\u65b9\u5f0f\uff0c\u540c\u65f6\u8fd8\u53ef\u4ee5\u907f\u514d\u53d8\u91cf\u7684\u91cd\u590d\u5b9a\u4e49\uff1a constants.h 1 2 3 4 5 6 7 8 9 10 11 12 #ifndef CONSTANTS_H #define CONSTANTS_H // define your own namespace to hold constants namespace constants { inline constexpr double pi { 3.14159 }; // note: now inline constexpr inline constexpr double avogadro { 6.0221413e23 }; inline constexpr double myGravity { 9.2 }; // m/s^2 -- gravity is light on this planet // ... other related constants } #endif main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include \"constants.h\" #include int main () { std :: cout << \"Enter a radius: \" ; int radius {}; std :: cin >> radius ; std :: cout << \"The circumference is: \" << 2.0 * radius * constants :: pi << '\\n' ; return 0 ; } \u5c06 constants.h \u5305\u542b\u5230\u6240\u6709\u9700\u8981\u4f7f\u7528\u5b83\u7684\u4ee3\u7801\u6587\u4ef6\u4e2d\uff0c\u4f46\u662f\u8fd9\u4e9b\u53d8\u91cf\u53ea\u4f1a\u88ab\u521d\u59cb\u5316\u4e00\u6b21\uff0c\u5e76\u4e14\u5728\u6240\u6709\u6587\u4ef6\u4e2d\u5171\u4eab\u4f7f\u7528\u3002 \u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u4ecd\u7136\u4e0d\u80fd\u907f\u514d\u6587\u4ef6\u91cd\u590d\u7f16\u8bd1\u7684\u95ee\u9898\u3002\u5f53\u4efb\u4f55\u5e38\u91cf\u88ab\u4fee\u6539\u65f6\uff0c\u6bcf\u4e2a\u5305\u542b\u5b83\u7684\u6587\u4ef6\u90fd\u9700\u8981\u88ab\u91cd\u65b0\u7f16\u8bd1\u3002\u6240\u4ee5\uff0c\u5982\u679c\u4f60\u7684\u5e38\u91cf\u7ecf\u5e38\u9700\u8981\u4fee\u6539\uff08\u51fa\u4e8e\u8c03\u4f18\u7684\u76ee\u7684\uff09\uff0c\u8fd9\u65e0\u7591\u4f1a\u5bfc\u81f4\u5f88\u957f\u7684\u91cd\u65b0\u7f16\u8bd1\u65f6\u95f4\uff0c\u6b64\u65f6\u4f60\u53ef\u4ee5\u8003\u8651\u5c06\u5e38\u91cf\u5f15\u5165\u5230\u5355\u72ec\u7684\u5934\u6587\u4ef6\u4e2d\uff08\u51cf\u5c11include\u7684\u6570\u91cf\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u652f\u6301 C++17\uff0c\u6700\u597d\u5c06\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u4e3a inline constexpr \u7c7b\u578b\u5e76\u653e\u7f6e\u5728\u5934\u6587\u4ef6\u4e2d\u3002 \u6ce8\u610f \u4f7f\u7528 std::string_view \u5b9a\u4e49 constexpr \u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u5728 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \u4e2d\u8fdb\u884c\u4e86\u4ecb\u7ecd\u3002","title":"6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf"},{"location":"6-9-Sharing-global-constants-across-multiple-files-using-inline-variables/#_1","text":"\u5728 C++17 \u4e4b\u524d\uff0c\u5b9a\u4e49\u5168\u5c40\u5e38\u91cf\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a \u5b9a\u4e49\u4e00\u4e2a\u5934\u6587\u4ef6\u6765\u5b58\u653e\u8fd9\u4e9b\u5e38\u91cf \u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4 (\u53c2\u89c1\uff1a 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u5c06\u5168\u90e8\u7684\u5e38\u91cf\u653e\u5230\u8be5\u547d\u540d\u7a7a\u95f4\u4e2d\uff08\u540c\u65f6\u786e\u4fdd\u5b83\u4eec\u90fd\u662f constexpr \u7c7b\u578b\u7684\uff09 \u5728\u9700\u8981\u4f7f\u7528\u8fd9\u4e9b\u5e38\u91cf\u7684\u5730\u65b9\uff0c #include \u8be5\u5934\u6587\u4ef6 constants.h 1 2 3 4 5 6 7 8 9 10 11 12 13 #ifndef CONSTANTS_H #define CONSTANTS_H // \u5b9a\u4e49\u5305\u542b\u5e38\u91cf\u7684\u547d\u540d\u7a7a\u95f4 namespace constants { // \u9ed8\u8ba4\u5185\u90e8\u94fe\u63a5 constexpr double pi { 3.14159 }; constexpr double avogadro { 6.0221413e23 }; constexpr double myGravity { 9.2 }; // m/s^2 -- gravity is light on this planet // ... other related constants } #endif \u7136\u540e\u5728 .cpp \u6587\u4ef6\u4e2d\u4f7f\u7528 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 ( :: )\u6765\u4f7f\u7528\u8fd9\u4e9b\u5e38\u91cf\uff0c\u8fd0\u7b97\u7b26\u5de6\u4fa7\u662f\u547d\u540d\u7a7a\u95f4\uff0c\u53f3\u4fa7\u662f\u5e38\u91cf\u540d\uff1a main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include \"constants.h\" // include a copy of each constant in this file #include int main () { std :: cout << \"Enter a radius: \" ; int radius {}; std :: cin >> radius ; std :: cout << \"The circumference is: \" << 2.0 * radius * constants :: pi << '\\n' ; return 0 ; } \u5f53\u5934\u6587\u4ef6\u88ab #included \u5230\u4e86 a .cpp \u4e2d\u540e\uff0c\u5934\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u5e38\u91cf\u5b9e\u9645\u4e0a\u90fd\u4f1a\u88ab \u62f7\u8d1d \u5230\u6e90\u6587\u4ef6\u4e2d\u3002\u56e0\u4e3a\u8fd9\u4e9b\u53d8\u91cf\u4e0d\u5c5e\u4e8e\u4efb\u4f55\u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u4eec\u5728\u88ab\u5305\u542b\u5230\u6e90\u6587\u4ef6\u540e\uff0c\u4f1a\u88ab\u5f53\u505a\u5168\u5c40\u53d8\u91cf\u5bf9\u5f85\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u5728\u6587\u4ef6\u7684\u4efb\u4f55\u5730\u65b9\u4f7f\u7528\u5b83\u4eec\u3002 \u56e0\u4e3a const \u5168\u5c40\u53d8\u91cf\u5177\u6709 \u5185\u90e8\u94fe\u63a5 \u5c5e\u6027\uff0c\u56e0\u6b64\u6bcf\u4e2a\u53d8\u91cf\u5728.cpp\u6587\u4ef6\u4e4b\u95f4\u662f\u72ec\u7acb\u7684\uff0c\u800c\u4e14\u94fe\u63a5\u5668\u5e76\u4e0d\u80fd\u770b\u5230\u5b83\u4eec\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7531\u4e8e\u5b83\u4eec\u662f\u5e38\u91cf\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u5c06\u5176\u201c\u4f18\u5316\u6389\u201d\u3002 \u9898\u5916\u8bdd \u201c\u4f18\u5316\u6389\u201d\u8fd9\u4e2a\u672f\u8bed\uff0c\u6307\u7684\u662f\u7f16\u8bd1\u5668\u4e3a\u4e86\u4f18\u5316\u7a0b\u5e8f\u6027\u80fd\u800c\u5220\u9664\u4e86\u67d0\u4e9b\u4e1c\u897f\uff0c\u4f46\u4e0d\u4f1a\u5bf9\u7a0b\u5e8f\u7684\u529f\u80fd\u4ea7\u751f\u5f71\u54cd\u3002\u4f8b\u5982\uff0c\u4f60\u5b9a\u4e49\u4e86\u4e00\u4e2a const \u7c7b\u578b\u7684\u53d8\u91cf x \u5e76\u4e14\u5c06\u5176\u521d\u59cb\u5316\u4e3a 4\u3002\u5219\u7f16\u8bd1\u5668\u4f1a\u5c06\u4efb\u4f55\u5f15\u7528\u8be5\u53d8\u91cf\u7684\u5730\u65b9\uff0c\u76f4\u63a5\u66ff\u6362\u4e3a 4\uff08\u56e0\u4e3a x \u662fconst\u7c7b\u578b\u7684\uff0c\u6240\u4ee5\u5b83\u4e0d\u80fd\u88ab\u4fee\u6539\uff09\uff0c\u4ece\u800c\u907f\u514d\u4e86\u53d8\u91cf\u7684\u5b9a\u4e49\u548c\u521d\u59cb\u5316\u8fc7\u7a0b\u3002","title":"\u5c06\u5168\u5c40\u5e38\u91cf\u4f5c\u4e3a\u5185\u90e8\u53d8\u91cf"},{"location":"6-9-Sharing-global-constants-across-multiple-files-using-inline-variables/#_2","text":"\u4e0a\u9762\u63d0\u5230\u7684\u65b9\u6cd5\u5b9e\u9645\u4e0a\u6709\u4e00\u4e9b\u7f3a\u9677\u3002 \u5c3d\u7ba1\u5c06 constants.h \u5934\u6587\u4ef6 #included \u5230\u6bcf\u4e00\u4e2a\u9700\u8981\u5b83\u7684\u6e90\u6587\u4ef6\u4e2d\u662f\u975e\u5e38\u7b80\u5355\uff08\u5bf9\u4e8e\u5c0f\u7a0b\u5e8f\u6765\u8bf4\u4e5f\u901a\u5e38\u4e0d\u4f1a\u6709\u4ec0\u4e48\u95ee\u9898\uff09\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u4f46\u6bd5\u7adf\u8fd9\u4f1a\u5bfc\u81f4\u53d8\u91cf\u88ab\u590d\u5236\u5230\u6e90\u6587\u4ef6\u4e2d\u3002\u56e0\u6b64\uff0c\u5982\u679c constants.h \u88ab 20 \u4e2a\u6587\u4ef6\u6240\u5305\u542b\uff0c\u90a3\u4e48\u8fd9\u4e9b\u5168\u5c40\u53d8\u91cf\u5c31\u4f1a\u88ab\u590d\u5236 20 \u6b21\u3002 \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e \u5e76\u4e0d\u80fd\u9632\u6b62\u8be5\u95ee\u9898\u7684\u53d1\u751f\uff0c\u56e0\u4e3a\u5b83\u53ea\u80fd\u4fdd\u8bc1\u5934\u6587\u4ef6\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u88ab\u5305\u542b\u4e00\u6b21\uff0c\u800c\u4e0d\u662f\u963b\u6b62\u5934\u6587\u4ef6\u88ab\u591a\u4e2a\u4e0d\u540c\u7684\u6587\u4ef6\u5305\u542b\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u4f1a\u9762\u4e34\u4e24\u4e2a\u95ee\u9898\uff1a \u4fee\u6539\u5e38\u91cf\u4f1a\u5bfc\u81f4\u6240\u6709\u5305\u542b\u8be5\u5934\u6587\u4ef6\u7684\u6587\u4ef6\u91cd\u65b0\u7f16\u8bd1\uff0c\u5bf9\u4e8e\u5927\u578b\u9879\u76ee\u6765\u8bf4\u8fd9\u4f1a\u6d6a\u8d39\u5f88\u591a\u65f6\u95f4 \u5982\u679c\u5e38\u91cf\u5f88\u5927\uff0c\u800c\u4e14\u65e0\u6cd5\u88ab\u201c\u4f18\u5316\u6389\u201d\uff0c\u90a3\u4e48\u5927\u91cf\u7684\u590d\u5236\u4f1a\u5bfc\u81f4\u5927\u91cf\u7684\u5185\u5b58\u5360\u7528\u3002 \u907f\u514d\u8fd9\u4e2a\u95ee\u9898\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u662f\u5c06\u5168\u5c40\u5e38\u91cf\u5b9a\u4e49\u4e3a \u5916\u90e8\u53d8\u91cf \uff0c\u8fd9\u6837\u4e00\u4e2a\u53d8\u91cf\uff08\u521d\u59cb\u5316\u4e00\u6b21\uff09\u5c31\u53ef\u4ee5\u88ab\u6240\u6709\u7684\u6587\u4ef6\u201c\u5171\u4eab\u4f7f\u7528\u201d\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5fc5\u987b\u5c06\u5e38\u91cf\u5b9a\u4e49\u5728\u4e00\u4e2a a.cpp \u6587\u4ef6\u4e2d\uff08\u786e\u4fdd\u5b83\u53ea\u88ab\u5b9a\u4e49\u4e00\u6b21\uff09\uff0c\u7136\u540e\u5728\u5934\u6587\u4ef6\u4e2d\u6dfb\u52a0\u5176\u524d\u5411\u5b9a\u4e49\uff08\u8be5\u5934\u6587\u4ef6\u4ecd\u9700\u8981\u88ab\u5305\u542b\u5230\u4f7f\u7528\u4e86\u8be5\u53d8\u91cf\u7684\u6587\u4ef6\u4e2d\uff09\u3002 \u4f5c\u8005\u6ce8 \u8fd9\u91cc\u4f7f\u7528\u4e86 const \u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u8fd9\u662f\u56e0\u4e3a constexpr \u7c7b\u578b\u7684\u53d8\u91cf\u4e0d\u80fd\u88ab\u524d\u5411\u58f0\u660e\uff08 6-7-External-linkage-and-variable-forward-declarations#^ce6263 \uff09\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u9700\u8981\u5728\u7f16\u8bd1\u65f6\u77e5\u9053 constexpr \u7c7b\u578b\u53d8\u91cf\u7684\u5b9a\u4e49\uff0c\u800c\u524d\u5411\u58f0\u660e\u5e76\u4e0d\u80fd\u63d0\u4f9b\u5b9a\u4e49\u7684\u4fe1\u606f\u3002 constants.cpp 1 2 3 4 5 6 7 8 9 #include \"constants.h\" namespace constants { // actual global variables extern const double pi { 3.14159 }; extern const double avogadro { 6.0221413e23 }; extern const double myGravity { 9.2 }; // m/s^2 -- gravity is light on this planet } constants.h 1 2 3 4 5 6 7 8 9 10 11 12 #ifndef CONSTANTS_H #define CONSTANTS_H namespace constants { // since the actual variables are inside a namespace, the forward declarations need to be inside a namespace as well extern const double pi ; extern const double avogadro ; extern const double myGravity ; } #endif \u4f7f\u7528\u65b9\u6cd5\u548c\u4e4b\u524d\u662f\u4e00\u6837\u7684\uff1a main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include \"constants.h\" // include all the forward declarations #include int main () { std :: cout << \"Enter a radius: \" ; int radius {}; std :: cin >> radius ; std :: cout << \"The circumference is: \" << 2.0 * radius * constants :: pi << '\\n' ; return 0 ; } \u56e0\u4e3a\u5168\u5c40\u7b26\u53f7\u5e38\u91cf\u5e94\u8be5\u88ab\u653e\u7f6e\u5728\u547d\u540d\u7a7a\u95f4\u4e2d\uff08\u907f\u514d\u4e0e\u5176\u4ed6\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6807\u8bc6\u7b26\u4ea7\u751f\u547d\u540d\u51b2\u7a81\uff09\uff0c\u6240\u4ee5\u6ca1\u5fc5\u8981\u5bf9\u5176\u4f7f\u7528 g_ \u524d\u7f00\u3002 \u8fd9\u6837\u4e00\u6765\uff0c\u7b26\u53f7\u5e38\u91cf\u53ea\u4f1a\u5728 constants.cpp \u4e2d\u88ab\u521d\u59cb\u5316\u4e00\u6b21\uff0c\u800c\u4e0d\u662f\u5728\u6bcf\u4e2a constants.h \u88ab\u5305\u542b\u7684\u5730\u65b9\u88ab\u521d\u59cb\u5316\u3002\u540c\u65f6\uff0c\u8fd9\u4e9b\u5e38\u6570\u90fd\u4f1a\u88ab\u94fe\u63a5\u5230 constants.cpp \u4e2d\u521d\u59cb\u5316\u7684\u53d8\u91cf\uff0c\u56e0\u6b64\u5f53\u8fd9\u4e9b\u53d8\u91cf\u88ab\u4fee\u6539\u65f6\uff0c\u53ea\u6709 constants.cpp \u9700\u8981\u88ab\u91cd\u65b0\u7f16\u8bd1\u3002 \u4e0d\u8fc7\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u4e0d\u662f\u5b8c\u7f8e\u7684\u3002\u9996\u5148\uff0c\u8fd9\u4e9b\u5e38\u91cf\u73b0\u5728\u53ea\u4f1a\u5728\u5b83\u4eec\u88ab\u5b9a\u4e49\u7684\u6587\u4ef6\u4e2d\u88ab\u770b\u505a \u7f16\u8bd1\u65f6 \u5e38\u91cf\u3002\u800c\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\uff0c\u7f16\u8bd1\u5668\u53ea\u80fd\u770b\u5230\u5b83\u4eec\u7684\u58f0\u660e\uff0c\u800c\u770b\u4e0d\u5230\u5e38\u91cf\u5177\u4f53\u7684\u4e2d\uff08\u5fc5\u987b\u6709\u94fe\u63a5\u5668\u8fdb\u884c\u89e3\u6790\uff09\u3002\u56e0\u6b64\uff0c\u5728 constants.cpp \u4ee5\u5916\u7684\u5730\u65b9\uff0c\u8fd9\u4e9b\u5e38\u91cf\u90fd\u4e0d\u80fd\u88ab\u770b\u505a\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\uff0c\u4e5f\u5c31\u4e0d\u80fd\u5728\u8981\u6c42\u4f7f\u7528\u7f16\u8bd1\u65f6\u5e38\u91cf\u7684\u5730\u65b9\u4f7f\u7528\u5b83\u4eec\u3002\u53e6\u5916\uff0c\u56e0\u4e3a\u7f16\u8bd1\u65f6\u5e38\u91cf\u76f8\u5bf9\u4e8e\u8fd0\u884c\u65f6\u5e38\u91cf\u66f4\u5bb9\u6613\u4f18\u5316\uff0c\u6240\u4ee5\u5bf9\u4e8e\u8fd9\u4e9b\u8fd0\u884c\u65f6\u5e38\u91cf\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u65e0\u6cd5\u5bf9\u5176\u8fdb\u884c\u8db3\u591f\u7684\u4f18\u5316\u3002 \u5173\u952e\u4fe1\u606f \u4e3a\u4e86\u5728\u7f16\u8bd1\u65f6\u4f7f\u7528\u67d0\u4e9b\u53d8\u91cf\uff08\u4f8b\u5982\uff0c\u6570\u7ec4\u5927\u5c0f\uff09\uff0c\u7f16\u8bd1\u5668\u5fc5\u987b\u770b\u5230\u53d8\u91cf\u7684\u5b9a\u4e49\uff08\u800c\u4e0d\u4ec5\u4ec5\u662f\u524d\u9879\u58f0\u660e\uff09\u3002 \u56e0\u4e3a\u7f16\u8bd1\u5668\u662f\u72ec\u7acb\u7f16\u8bd1\u5404\u4e2a\u6e90\u6587\u4ef6\u7684\uff0c\u56e0\u6b64\u5b83\u53ea\u80fd\u5bf9\u6e90\u6587\u4ef6\u4e2d\u5b58\u5728\u5b9a\u4e49\u7684\u53d8\u91cf\u8fdb\u884c\u7f16\u8bd1\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49\u5728 constants.cpp \u7684\u53d8\u91cf\uff0c\u5728\u7f16\u8bd1\u5668\u7f16\u8bd1 main.cpp \u7684\u65f6\u5019\u662f\u4e0d\u53ef\u89c1\u7684\uff0c\u51fa\u4e8e\u8fd9\u4e2a\u539f\u56e0 constexpr \u7684\u5b9a\u4e49\u548c\u58f0\u660e\u4e0d\u80fd\u88ab\u5206\u6563\u5230\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u4e2d\uff0c\u5b83\u5fc5\u987b\u88ab\u5b9a\u4e49\u5728\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\u91cc\u3002 \u51fa\u4e8e\u4e0a\u8ff0\u539f\u56e0\uff0c\u6211\u4eec\u8fd8\u662f\u5efa\u8bae\u6700\u597d\u5c06\u5e38\u91cf\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u3002\u5982\u679c\u56e0\u6b64\u5bfc\u81f4\u4e86\u67d0\u4e9b\u7279\u5b9a\u7684\u95ee\u9898\uff0c\u4e0d\u59a8\u5230\u65f6\u5019\u5728\u628a\u5b83\u4eec\u4e00\u8d77\u79fb\u52a8\u5230.cpp\u6587\u4ef6\u53bb\u3002","title":"\u5c06\u5168\u5c40\u5e38\u91cf\u4f5c\u4e3a\u5916\u90e8\u53d8\u91cf"},{"location":"6-9-Sharing-global-constants-across-multiple-files-using-inline-variables/#c17","text":"C++17 \u5f15\u5165\u4e86\u4e00\u4e2a\u65b0\u7684\u6982\u5ff5\u2014\u2014 \u5185\u8054\u53d8\u91cf(inline variables) \u3002\u5728 C++ \u4e2d\uff0c[[inline|\u5185\u8054]]\u7684\u610f\u601d\u9010\u6e10\u5f15\u7533\u6210\u4e86\u201c\u5141\u8bb8\u591a\u5904\u5b9a\u4e49\u201d\u3002\u56e0\u6b64\uff0c\u5185\u8054\u53d8\u91cf\u53ef\u4ee5\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u88ab\u591a\u6b21\u5b9a\u4e49\uff0c\u4e14\u4e0d\u4f1a\u8fdd\u53cd \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u3002\u5185\u8054\u7684\u5168\u5c40\u53d8\u91cf\u9ed8\u8ba4\u5177\u6709\u5916\u90e8\u94fe\u63a5\u5c5e\u6027\u3002 \u94fe\u63a5\u5668\u53ef\u4ee5\u5c06\u6240\u6709\u7684\u5185\u8054\u5b9a\u4e49\u5408\u5e76\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u5b9a\u4e49\uff08\u8fd9\u6837\u5c31\u6ee1\u8db3\u4e86\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u5f53\u6211\u4eec\u628a\u5185\u8054\u53d8\u91cf\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u7684\u540c\u65f6\uff0c\u8fd8\u80fd\u591f\u5c06\u5b83\u4eec\u5728.cpp\u6587\u4ef6\u4e2d\u7684\u5b9a\u4e49\u770b\u505a\u662f\u4e00\u4e2a\u5b9a\u4e49\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4e00\u4e2a\u666e\u901a\u7684\u5e38\u91cf\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u540c\u65f6\u88ab #include \u5230\u4e8610\u4e2a\u4e0d\u540c\u7684\u6e90\u6587\u4ef6\u3002\u5982\u679c\u5b83\u4e0d\u662f\u5185\u8054\u7684\uff0c\u5219\u6b64\u65f6\u4f1a\u4ea7\u751f10\u4e2a\u5b9a\u4e49\u3002\u5982\u679c\u662f\u5185\u8054\u7684\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u9009\u53d6\u4e00\u4e2a\u5b9a\u4e49\u4f5c\u4e3a\u4e3b\u5b9a\u4e49\uff0c\u8fd9\u6837\u4e00\u6765\u5c31\u53ea\u4f1a\u4ea7\u751f\u4e00\u4e2a\u5b9a\u4e49\u3002\u8fd9\u6837\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8282\u77019\u4e2a\u5e38\u91cf\u7684\u5185\u5b58\u7a7a\u95f4\u3002 \u8fd9\u4e9b\u53d8\u91cf\u540c\u65f6\u8fd8\u4fdd\u7559\u4e86 constexpr \u7684\u7279\u6027\uff0c\u56e0\u6b64\u5728\u4efb\u4f55\u5305\u542b\u5b83\u4eec\u7684\u6587\u4ef6\u4e2d\uff0c\u53ea\u8981\u662f constexpr \u53ef\u7528\u7684\u5730\u65b9\uff0c\u8fd9\u4e9b\u53d8\u91cf\u4e5f\u53ef\u7528\u3002\u540c\u65f6\uff0c\u76f8\u6bd4\u8f83\u4e8e\u8fd0\u884c\u65f6\u5e38\u91cf\uff08\u6216\u975e\u5e38\u91cf\uff09\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u6781\u5927\u5730\u4f18\u5316 constexpr \u7c7b\u578b\u7684\u53d8\u91cf \u5185\u8054\u53d8\u91cf\u6709\u4e24\u4e2a\u4e3b\u8981\u7684\u9650\u5236\uff0c\u9700\u8981\u6211\u4eec\u9075\u5faa\uff1a \u5185\u8054\u53d8\u91cf\u7684\u6240\u6709\u5b9a\u4e49\u5fc5\u987b\u662f\u5b8c\u5168\u4e00\u81f4\u7684\uff08\u5426\u5219\u4f1a\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\uff09\u3002 \u5185\u8054\u53d8\u91cf\u7684\u5b9a\u4e49\uff08\u800c\u975e\u58f0\u660e\uff09\u5fc5\u987b\u51fa\u73b0\u5728\u4efb\u4f55\u4f7f\u7528\u5b83\u4eec\u7684\u6587\u4ef6\u4e2d\u3002 \u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u7ee7\u7eed\u6cbf\u7528\u5c06\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u7684\u65b9\u5f0f\uff0c\u540c\u65f6\u8fd8\u53ef\u4ee5\u907f\u514d\u53d8\u91cf\u7684\u91cd\u590d\u5b9a\u4e49\uff1a constants.h 1 2 3 4 5 6 7 8 9 10 11 12 #ifndef CONSTANTS_H #define CONSTANTS_H // define your own namespace to hold constants namespace constants { inline constexpr double pi { 3.14159 }; // note: now inline constexpr inline constexpr double avogadro { 6.0221413e23 }; inline constexpr double myGravity { 9.2 }; // m/s^2 -- gravity is light on this planet // ... other related constants } #endif main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include \"constants.h\" #include int main () { std :: cout << \"Enter a radius: \" ; int radius {}; std :: cin >> radius ; std :: cout << \"The circumference is: \" << 2.0 * radius * constants :: pi << '\\n' ; return 0 ; } \u5c06 constants.h \u5305\u542b\u5230\u6240\u6709\u9700\u8981\u4f7f\u7528\u5b83\u7684\u4ee3\u7801\u6587\u4ef6\u4e2d\uff0c\u4f46\u662f\u8fd9\u4e9b\u53d8\u91cf\u53ea\u4f1a\u88ab\u521d\u59cb\u5316\u4e00\u6b21\uff0c\u5e76\u4e14\u5728\u6240\u6709\u6587\u4ef6\u4e2d\u5171\u4eab\u4f7f\u7528\u3002 \u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u4ecd\u7136\u4e0d\u80fd\u907f\u514d\u6587\u4ef6\u91cd\u590d\u7f16\u8bd1\u7684\u95ee\u9898\u3002\u5f53\u4efb\u4f55\u5e38\u91cf\u88ab\u4fee\u6539\u65f6\uff0c\u6bcf\u4e2a\u5305\u542b\u5b83\u7684\u6587\u4ef6\u90fd\u9700\u8981\u88ab\u91cd\u65b0\u7f16\u8bd1\u3002\u6240\u4ee5\uff0c\u5982\u679c\u4f60\u7684\u5e38\u91cf\u7ecf\u5e38\u9700\u8981\u4fee\u6539\uff08\u51fa\u4e8e\u8c03\u4f18\u7684\u76ee\u7684\uff09\uff0c\u8fd9\u65e0\u7591\u4f1a\u5bfc\u81f4\u5f88\u957f\u7684\u91cd\u65b0\u7f16\u8bd1\u65f6\u95f4\uff0c\u6b64\u65f6\u4f60\u53ef\u4ee5\u8003\u8651\u5c06\u5e38\u91cf\u5f15\u5165\u5230\u5355\u72ec\u7684\u5934\u6587\u4ef6\u4e2d\uff08\u51cf\u5c11include\u7684\u6570\u91cf\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u652f\u6301 C++17\uff0c\u6700\u597d\u5c06\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u4e3a inline constexpr \u7c7b\u578b\u5e76\u653e\u7f6e\u5728\u5934\u6587\u4ef6\u4e2d\u3002 \u6ce8\u610f \u4f7f\u7528 std::string_view \u5b9a\u4e49 constexpr \u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u5728 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf \u4e2d\u8fdb\u884c\u4e86\u4ecb\u7ecd\u3002","title":"\u5c06\u5168\u5c40\u5e38\u91cf\u4f5c\u4e3a\u5185\u8054\u53d8\u91cf\u00a0(C++17)"},{"location":"6-x-Chapter-6-summary-and-quiz/","tags":["summary","C++17","C++20"],"text":"\u7ae0\u8282\u5c0f\u7ed3 \u672c\u7ae0\u7684\u4ecb\u7ecd\u4e86\u5f88\u591a\u5185\u5bb9\u3002\u4f60\u505a\u7684\u5f88\u597d\uff01 6.1 - \u590d\u5408\u8bed\u53e5\uff08\u8bed\u53e5\u5757\uff09 \u590d\u5408\u8bed\u53e5 \u6216\u8005\u8bed\u53e5\u5757\u662f\u4e00\u7ec40\u4e2a\u6216\u591a\u4e2a\u8bed\u53e5\u7ec4\u6210\u7684\u96c6\u5408\uff0c\u5b83\u4eec\u88ab\u7f16\u8bd1\u5668\u770b\u505a\u4e00\u4e2a\u6574\u4f53\uff0c\u5c31\u597d\u50cf\u4e00\u4e2a\u5355\u4e00\u7684\u8bed\u53e5\u4e00\u6837\u3002\u8bed\u53e5\u5757\u4ece { \u7b26\u53f7\u5f00\u59cb\uff0c\u5230 } \u7b26\u53f7\u7ed3\u675f\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u6240\u6709\u7684\u8bed\u53e5\u3002\u5728\u4efb\u4f55\u5141\u8bb8\u4f7f\u7528\u5355\u4e00\u8bed\u53e5\u7684\u5730\u65b9\uff0c\u4e5f\u90fd\u53ef\u4ee5\u4f7f\u7528\u8bed\u53e5\u5757\u3002\u8bed\u53e5\u5757\u7684\u7ed3\u675f\u5904\u4e0d\u9700\u8981\u4f7f\u7528\u5206\u53f7\u3002\u8bed\u53e5\u5757\u901a\u5e38\u4f1a\u548cif\u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\u3002 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u547d\u540d\u7a7a\u95f4 \u6307\u7684\u662f\u4f60\u4e3a\u4e86\u81ea\u5df1\u7684\u58f0\u660e\u800c\u521b\u5efa\u7684\u547d\u540d\u7a7a\u95f4\u3002\u7531C++\uff08\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff09\u6216\u6807\u51c6\u5e93\uff08 std \u547d\u540d\u7a7a\u95f4\uff09\u63d0\u4f9b\u7684\u547d\u540d\u7a7a\u95f4\u4e0d\u5c5e\u4e8e\u7528\u6237\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\uff1b \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 ( :: )\u6765\u8bbf\u95ee\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u58f0\u660e\u3002\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u544a\u8bc9\u7f16\u8bd1\u5668\u5230\u8fd0\u7b97\u7b26\u5de6\u64cd\u4f5c\u6570\u4e2d\u53bb\u67e5\u627e\u53f3\u64cd\u4f5c\u6570\u3002\u5982\u679c\u4e0d\u63d0\u4f9b\u5de6\u64cd\u4f5c\u6570\uff0c\u5219\u4f1a\u4f7f\u7528\u5168\u5c40\u4f5c\u7528\u57df\u3002 6.3 - \u5c40\u90e8\u53d8\u91cf \u5c40\u90e8\u53d8\u91cf \uff08 6.3 - \u5c40\u90e8\u53d8\u91cf \uff09\u6307\u7684\u662f\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8\u7684\u53d8\u91cf\uff08\u5305\u62ec\u51fd\u6570\u7684 \u5f62\u53c2 \uff09\u3002\u5c40\u90e8\u53d8\u91cf\u5177\u6709\u8bed\u53e5\u5757\u4f5c\u7528\u57df\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u7684\u4f5c\u7528\u57df\u4ece\u53d8\u91cf\u5b9a\u4e49\u5f00\u59cb\uff0c\u5230\u8bed\u53e5\u5757\u7ed3\u675f\u4e3a\u6b62\u3002\u5c40\u90e8\u53d8\u91cf\u5177\u6709 \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u4e5f\u5c31\u662f\u8bf4\u5b83\u5728\u5b9a\u4e49\u65f6\u88ab\u521b\u5efa\uff0c\u5728\u79bb\u5f00\u8bed\u53e5\u5757\u65f6\u88ab\u9500\u6bc1\u3002 6.4 - \u5168\u5c40\u53d8\u91cf \u5168\u5c40\u53d8\u91cf \u6307\u7684\u662f\u5b9a\u4e49\u5728\u51fd\u6570\u5916\u90e8\u7684\u53d8\u91cf\u3002 \u5168\u5c40\u53d8\u91cf\u5177\u6709 \u6587\u4ef6\u4f5c\u7528\u57df \uff0c\u8fd9\u4e5f\u5c31\u610f\u5473\u7740\u5b83\u7684\u53ef\u89c1\u8303\u56f4\u4ece\u53d8\u91cf\u5b9a\u4e49\u5f00\u59cb\u5230\u6587\u4ef6\u7ed3\u675f\u4e3a\u6b62\u3002\u5168\u5c40\u53d8\u91cf\u5177\u6709 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u4e5f\u662f\u8bf4\u8be5\u53d8\u91cf\u4f1a\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u521b\u5efa\uff0c\u7a0b\u5e8f\u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u5c3d\u53ef\u80fd\u907f\u514d\u5bf9\u9759\u6001\u53d8\u91cf\u7684\u52a8\u6001 \u521d\u59cb\u5316\u3002 6.5 - \u53d8\u91cf\u906e\u853d \u58f0\u660e\u5728\u5d4c\u5957\u8bed\u53e5\u5757\u4e2d\u7684\u53d8\u91cf\u4f1a\u5bf9\u5916\u5c42\u8bed\u53e5\u5757\u4e2d\u7684\u540c\u540d\u53d8\u91cf\u5f62\u6210 \u53d8\u91cf\u906e\u853d \uff0c\u53d8\u91cf\u906e\u853d\u5e94\u5f53\u88ab\u907f\u514d\u3002 6.6 - \u5185\u90e8\u94fe\u63a5 & 6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e \u4e00\u4e2a\u6807\u8bc6\u7b26\u7684** \u94fe\u63a5\u5c5e\u6027 \uff0c\u51b3\u5b9a\u4e86\u8be5\u6807\u8bc6\u7b26\u7684\u5176\u4ed6\u58f0\u660e\u503c\u5f97\u662f\u5426\u90fd\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002\u5c40\u90e8\u53d8\u91cf\u6ca1\u6709\u94fe\u63a5\u5c5e\u6027\u3002\u5177\u6709 \u5185\u90e8\u94fe\u63a5 \u5c5e\u6027\u7684\u6807\u8bc6\u7b26\u53ef\u4ee5\u5728\u6587\u4ef6\u4e2d\u88ab\u8bbf\u95ee\uff0c\u4f46\u662f\u4e0d\u80fd\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u88ab\u8bbf\u95ee\u3002\u5177\u6709 \u5916\u90e8\u94fe\u63a5 \u5c5e\u6027\u7684\u6807\u8bc6\u7b26\u5219\u53ef\u4ee5\u5728\u5b9a\u4e49\u5b83\u7684\u6587\u4ef6\u6216\u5176\u4ed6\u6587\u4ef6\u4e2d\u53ef\u89c1\u6216\u88ab\u8bbf\u95ee\uff08\u901a\u8fc7 \u524d\u5411\u58f0\u660e \uff09\u3002 6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c && 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf \u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u975e const \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u3002const \u5168\u5c40\u53d8\u91cf\u901a\u5e38\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\u3002\u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u652f\u6301C++17\uff0c\u8bf7\u4f7f\u7528 inline \u53d8\u91cf\u5b9a\u4e49\u5168\u5c40\u5e38\u91cf\u3002 6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf \u5c40\u90e8\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7 static \u5173\u952e\u5b57\u8d4b\u4e88\u5176 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u3002 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 using \u8bed\u53e5\u5305\u62ec using\u58f0\u660e \u548c using\u6307\u4ee4 \uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u907f\u514d\u4f7f\u7528\u9650\u5b9a\u6807\u8bc6\u7b26\uff0c\u4f46\u662f\u6211\u4eec\u5e94\u8be5\u907f\u514d\u8fd9\u6837\u505a\u3002 6.13 - \u5185\u8054\u51fd\u6570 \u5185\u8054\u51fd\u6570 \u6700\u521d\u7684\u8bbe\u8ba1\u76ee\u7684\u662f\u63d0\u4f9b\u4e00\u79cd\u65b9\u6cd5\u544a\u8bc9\u7f16\u8bd1\u5668\u5c06\u51fd\u6570\u8c03\u7528\u8fdb\u884c\u5185\u8054\u5c55\u5f00\uff0c\u4f46\u6211\u4eec\u5e76\u4e0d\u5e94\u8be5\u51fa\u4e8e\u8be5\u76ee\u7684\u800c\u4f7f\u7528 inline \u5173\u952e\u5b57\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u4e3a\u4f60\u505a\u51b3\u5b9a\u3002\u5728\u73b0\u4ee3 C++ \u4e2d\uff0c inline \u5173\u952e\u5b57\u7528\u4e8e\u5c06\u51fd\u6570\u6392\u9664\u5230 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u7684\u9650\u5b9a\u4e4b\u5916\uff0c\u4ece\u800c\u4f7f\u5176\u5b9a\u4e49\u53ef\u4ee5\u88ab\u5bfc\u5165\u5230\u591a\u4e2a\u6e90\u6587\u4ef6\u4e2d\u80fd\u591f\u3002\u5185\u8054\u51fd\u6570\u901a\u5e38\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u5b83\u4eec\u7684\u5b9a\u4e49\u5c31\u53ef\u4ee5\u5728 #included \u65f6\u88ab\u4e00\u540c\u62f7\u8d1d\u5230\u6e90\u6587\u4ef6\u4e2d\u3002 6.14 - Constexpr \u548c consteval \u51fd\u6570 constexpr \u51fd\u6570\u7684\u8fd4\u56de\u503c \u53ef\u80fd \u4f1a\u5728 \u7f16\u8bd1\u65f6 \u6c42\u503c\u3002\u4f7f\u7528 constexpr \u5173\u952e\u5b57\u53ef\u4ee5\u5c06\u51fd\u6570\u5b9a\u4e49\u4e3a constexpr \u7c7b\u578b\u3002 constexpr \u51fd\u6570 \u6709\u8d44\u683c \u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u800c\u5982\u679c\u5b83\u7684\u8fd4\u56de\u503c\u88ab\u4f7f\u7528\u5728\u9700\u8981\u5e38\u91cf\u7684\u4e0a\u4e0b\u6587\u4e2d\u65f6\uff0c\u5219\u4e00\u5b9a\u4f1a\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u5219\u53ef\u4ee5\u81ea\u7531\u9009\u62e9\u662f\u5728\u7f16\u8bd1\u65f6\u8fd8\u662f\u8fd0\u884c\u65f6\u5bf9\u51fd\u6570\u8fdb\u884c\u6c42\u503c\u3002 C++20 \u5f15\u5165\u4e86\u65b0\u7684\u5173\u952e\u5b57 consteval \uff0c\u5b83\u53ef\u4ee5\u8981\u6c42\u51fd\u6570\u5fc5\u987b\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u5426\u5219\u5c31\u4f1a\u4ea7\u751f\u7f16\u8bd1\u62a5\u9519\u3002\u8fd9\u79cd\u51fd\u6570\u79f0\u4e3a \u5373\u65f6\u51fd\u6570 \u3002 6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4 \u6700\u540e\uff0cC++ \u652f\u6301 \u533f\u540d\u547d\u540d\u7a7a\u95f4 \uff0c\u5b83\u9690\u542b\u5730\u5c06\u5176\u5185\u90e8\u5b9a\u4e49\u7684\u5185\u5bb9\u8d4b\u4e88 \u5185\u90e8\u94fe\u63a5 \u3002 C++ \u540c\u6837\u8fd8\u652f\u6301 \u5185\u8054\u547d\u540d\u7a7a\u95f4 \uff0c\u5b83\u4e3a\u547d\u540d\u7a7a\u95f4\u63d0\u4f9b\u4e86\u539f\u751f\u7684\u7248\u672c\u63a7\u5236\u673a\u5236\u3002 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3","title":"6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5"},{"location":"6-x-Chapter-6-summary-and-quiz/#_1","text":"\u672c\u7ae0\u7684\u4ecb\u7ecd\u4e86\u5f88\u591a\u5185\u5bb9\u3002\u4f60\u505a\u7684\u5f88\u597d\uff01 6.1 - \u590d\u5408\u8bed\u53e5\uff08\u8bed\u53e5\u5757\uff09 \u590d\u5408\u8bed\u53e5 \u6216\u8005\u8bed\u53e5\u5757\u662f\u4e00\u7ec40\u4e2a\u6216\u591a\u4e2a\u8bed\u53e5\u7ec4\u6210\u7684\u96c6\u5408\uff0c\u5b83\u4eec\u88ab\u7f16\u8bd1\u5668\u770b\u505a\u4e00\u4e2a\u6574\u4f53\uff0c\u5c31\u597d\u50cf\u4e00\u4e2a\u5355\u4e00\u7684\u8bed\u53e5\u4e00\u6837\u3002\u8bed\u53e5\u5757\u4ece { \u7b26\u53f7\u5f00\u59cb\uff0c\u5230 } \u7b26\u53f7\u7ed3\u675f\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u6240\u6709\u7684\u8bed\u53e5\u3002\u5728\u4efb\u4f55\u5141\u8bb8\u4f7f\u7528\u5355\u4e00\u8bed\u53e5\u7684\u5730\u65b9\uff0c\u4e5f\u90fd\u53ef\u4ee5\u4f7f\u7528\u8bed\u53e5\u5757\u3002\u8bed\u53e5\u5757\u7684\u7ed3\u675f\u5904\u4e0d\u9700\u8981\u4f7f\u7528\u5206\u53f7\u3002\u8bed\u53e5\u5757\u901a\u5e38\u4f1a\u548cif\u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\u3002 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u547d\u540d\u7a7a\u95f4 \u6307\u7684\u662f\u4f60\u4e3a\u4e86\u81ea\u5df1\u7684\u58f0\u660e\u800c\u521b\u5efa\u7684\u547d\u540d\u7a7a\u95f4\u3002\u7531C++\uff08\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff09\u6216\u6807\u51c6\u5e93\uff08 std \u547d\u540d\u7a7a\u95f4\uff09\u63d0\u4f9b\u7684\u547d\u540d\u7a7a\u95f4\u4e0d\u5c5e\u4e8e\u7528\u6237\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\uff1b \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 ( :: )\u6765\u8bbf\u95ee\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u58f0\u660e\u3002\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26\u544a\u8bc9\u7f16\u8bd1\u5668\u5230\u8fd0\u7b97\u7b26\u5de6\u64cd\u4f5c\u6570\u4e2d\u53bb\u67e5\u627e\u53f3\u64cd\u4f5c\u6570\u3002\u5982\u679c\u4e0d\u63d0\u4f9b\u5de6\u64cd\u4f5c\u6570\uff0c\u5219\u4f1a\u4f7f\u7528\u5168\u5c40\u4f5c\u7528\u57df\u3002 6.3 - \u5c40\u90e8\u53d8\u91cf \u5c40\u90e8\u53d8\u91cf \uff08 6.3 - \u5c40\u90e8\u53d8\u91cf \uff09\u6307\u7684\u662f\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8\u7684\u53d8\u91cf\uff08\u5305\u62ec\u51fd\u6570\u7684 \u5f62\u53c2 \uff09\u3002\u5c40\u90e8\u53d8\u91cf\u5177\u6709\u8bed\u53e5\u5757\u4f5c\u7528\u57df\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u7684\u4f5c\u7528\u57df\u4ece\u53d8\u91cf\u5b9a\u4e49\u5f00\u59cb\uff0c\u5230\u8bed\u53e5\u5757\u7ed3\u675f\u4e3a\u6b62\u3002\u5c40\u90e8\u53d8\u91cf\u5177\u6709 \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u4e5f\u5c31\u662f\u8bf4\u5b83\u5728\u5b9a\u4e49\u65f6\u88ab\u521b\u5efa\uff0c\u5728\u79bb\u5f00\u8bed\u53e5\u5757\u65f6\u88ab\u9500\u6bc1\u3002 6.4 - \u5168\u5c40\u53d8\u91cf \u5168\u5c40\u53d8\u91cf \u6307\u7684\u662f\u5b9a\u4e49\u5728\u51fd\u6570\u5916\u90e8\u7684\u53d8\u91cf\u3002 \u5168\u5c40\u53d8\u91cf\u5177\u6709 \u6587\u4ef6\u4f5c\u7528\u57df \uff0c\u8fd9\u4e5f\u5c31\u610f\u5473\u7740\u5b83\u7684\u53ef\u89c1\u8303\u56f4\u4ece\u53d8\u91cf\u5b9a\u4e49\u5f00\u59cb\u5230\u6587\u4ef6\u7ed3\u675f\u4e3a\u6b62\u3002\u5168\u5c40\u53d8\u91cf\u5177\u6709 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u4e5f\u662f\u8bf4\u8be5\u53d8\u91cf\u4f1a\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u521b\u5efa\uff0c\u7a0b\u5e8f\u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u5c3d\u53ef\u80fd\u907f\u514d\u5bf9\u9759\u6001\u53d8\u91cf\u7684\u52a8\u6001 \u521d\u59cb\u5316\u3002 6.5 - \u53d8\u91cf\u906e\u853d \u58f0\u660e\u5728\u5d4c\u5957\u8bed\u53e5\u5757\u4e2d\u7684\u53d8\u91cf\u4f1a\u5bf9\u5916\u5c42\u8bed\u53e5\u5757\u4e2d\u7684\u540c\u540d\u53d8\u91cf\u5f62\u6210 \u53d8\u91cf\u906e\u853d \uff0c\u53d8\u91cf\u906e\u853d\u5e94\u5f53\u88ab\u907f\u514d\u3002 6.6 - \u5185\u90e8\u94fe\u63a5 & 6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e \u4e00\u4e2a\u6807\u8bc6\u7b26\u7684** \u94fe\u63a5\u5c5e\u6027 \uff0c\u51b3\u5b9a\u4e86\u8be5\u6807\u8bc6\u7b26\u7684\u5176\u4ed6\u58f0\u660e\u503c\u5f97\u662f\u5426\u90fd\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002\u5c40\u90e8\u53d8\u91cf\u6ca1\u6709\u94fe\u63a5\u5c5e\u6027\u3002\u5177\u6709 \u5185\u90e8\u94fe\u63a5 \u5c5e\u6027\u7684\u6807\u8bc6\u7b26\u53ef\u4ee5\u5728\u6587\u4ef6\u4e2d\u88ab\u8bbf\u95ee\uff0c\u4f46\u662f\u4e0d\u80fd\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u88ab\u8bbf\u95ee\u3002\u5177\u6709 \u5916\u90e8\u94fe\u63a5 \u5c5e\u6027\u7684\u6807\u8bc6\u7b26\u5219\u53ef\u4ee5\u5728\u5b9a\u4e49\u5b83\u7684\u6587\u4ef6\u6216\u5176\u4ed6\u6587\u4ef6\u4e2d\u53ef\u89c1\u6216\u88ab\u8bbf\u95ee\uff08\u901a\u8fc7 \u524d\u5411\u58f0\u660e \uff09\u3002 6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c && 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf \u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u975e const \u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u3002const \u5168\u5c40\u53d8\u91cf\u901a\u5e38\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\u3002\u5982\u679c\u4f60\u7684\u7f16\u8bd1\u5668\u652f\u6301C++17\uff0c\u8bf7\u4f7f\u7528 inline \u53d8\u91cf\u5b9a\u4e49\u5168\u5c40\u5e38\u91cf\u3002 6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf \u5c40\u90e8\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7 static \u5173\u952e\u5b57\u8d4b\u4e88\u5176 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u3002 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 using \u8bed\u53e5\u5305\u62ec using\u58f0\u660e \u548c using\u6307\u4ee4 \uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u907f\u514d\u4f7f\u7528\u9650\u5b9a\u6807\u8bc6\u7b26\uff0c\u4f46\u662f\u6211\u4eec\u5e94\u8be5\u907f\u514d\u8fd9\u6837\u505a\u3002 6.13 - \u5185\u8054\u51fd\u6570 \u5185\u8054\u51fd\u6570 \u6700\u521d\u7684\u8bbe\u8ba1\u76ee\u7684\u662f\u63d0\u4f9b\u4e00\u79cd\u65b9\u6cd5\u544a\u8bc9\u7f16\u8bd1\u5668\u5c06\u51fd\u6570\u8c03\u7528\u8fdb\u884c\u5185\u8054\u5c55\u5f00\uff0c\u4f46\u6211\u4eec\u5e76\u4e0d\u5e94\u8be5\u51fa\u4e8e\u8be5\u76ee\u7684\u800c\u4f7f\u7528 inline \u5173\u952e\u5b57\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u4e3a\u4f60\u505a\u51b3\u5b9a\u3002\u5728\u73b0\u4ee3 C++ \u4e2d\uff0c inline \u5173\u952e\u5b57\u7528\u4e8e\u5c06\u51fd\u6570\u6392\u9664\u5230 \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u7684\u9650\u5b9a\u4e4b\u5916\uff0c\u4ece\u800c\u4f7f\u5176\u5b9a\u4e49\u53ef\u4ee5\u88ab\u5bfc\u5165\u5230\u591a\u4e2a\u6e90\u6587\u4ef6\u4e2d\u80fd\u591f\u3002\u5185\u8054\u51fd\u6570\u901a\u5e38\u88ab\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u5b83\u4eec\u7684\u5b9a\u4e49\u5c31\u53ef\u4ee5\u5728 #included \u65f6\u88ab\u4e00\u540c\u62f7\u8d1d\u5230\u6e90\u6587\u4ef6\u4e2d\u3002 6.14 - Constexpr \u548c consteval \u51fd\u6570 constexpr \u51fd\u6570\u7684\u8fd4\u56de\u503c \u53ef\u80fd \u4f1a\u5728 \u7f16\u8bd1\u65f6 \u6c42\u503c\u3002\u4f7f\u7528 constexpr \u5173\u952e\u5b57\u53ef\u4ee5\u5c06\u51fd\u6570\u5b9a\u4e49\u4e3a constexpr \u7c7b\u578b\u3002 constexpr \u51fd\u6570 \u6709\u8d44\u683c \u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u800c\u5982\u679c\u5b83\u7684\u8fd4\u56de\u503c\u88ab\u4f7f\u7528\u5728\u9700\u8981\u5e38\u91cf\u7684\u4e0a\u4e0b\u6587\u4e2d\u65f6\uff0c\u5219\u4e00\u5b9a\u4f1a\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\u3002\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u5219\u53ef\u4ee5\u81ea\u7531\u9009\u62e9\u662f\u5728\u7f16\u8bd1\u65f6\u8fd8\u662f\u8fd0\u884c\u65f6\u5bf9\u51fd\u6570\u8fdb\u884c\u6c42\u503c\u3002 C++20 \u5f15\u5165\u4e86\u65b0\u7684\u5173\u952e\u5b57 consteval \uff0c\u5b83\u53ef\u4ee5\u8981\u6c42\u51fd\u6570\u5fc5\u987b\u8fdb\u884c\u7f16\u8bd1\u65f6\u6c42\u503c\uff0c\u5426\u5219\u5c31\u4f1a\u4ea7\u751f\u7f16\u8bd1\u62a5\u9519\u3002\u8fd9\u79cd\u51fd\u6570\u79f0\u4e3a \u5373\u65f6\u51fd\u6570 \u3002 6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4 \u6700\u540e\uff0cC++ \u652f\u6301 \u533f\u540d\u547d\u540d\u7a7a\u95f4 \uff0c\u5b83\u9690\u542b\u5730\u5c06\u5176\u5185\u90e8\u5b9a\u4e49\u7684\u5185\u5bb9\u8d4b\u4e88 \u5185\u90e8\u94fe\u63a5 \u3002 C++ \u540c\u6837\u8fd8\u652f\u6301 \u5185\u8054\u547d\u540d\u7a7a\u95f4 \uff0c\u5b83\u4e3a\u547d\u540d\u7a7a\u95f4\u63d0\u4f9b\u4e86\u539f\u751f\u7684\u7248\u672c\u63a7\u5236\u673a\u5236\u3002 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3","title":"\u7ae0\u8282\u5c0f\u7ed3"},{"location":"7-1-Control-flow-introduction/","tags":["control flow"],"text":"Key Takeaway \u5f53\u7a0b\u5e8f\u5f00\u59cb\u8fd0\u884c\u65f6\uff0cCPU\u4ece main() \u7684\u9876\u90e8\u5f00\u59cb\u6267\u884c\u6307\u4ee4\uff08\u9ed8\u8ba4\u6309\u7167\u987a\u5e8f\u6267\u884c\uff09\uff0c\u5f53\u5230\u8fbe main() \u51fd\u6570\u672b\u5c3e\u65f6\uff0c\u7a0b\u5e8f\u505c\u6b62\u3002CPU\u6267\u884c\u8bed\u53e5\u7684\u7279\u5b9a\u987a\u5e8f\uff0c\u79f0\u4e3a\u6267\u884c\u8def\u5f84\uff08\u7b80\u79f0\u8def\u5f84\uff09\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"You entered \" << x ; return 0 ; } \u8be5\u7a0b\u5e8f\u7684\u6267\u884c\u8def\u5f84\u4f9d\u6b21\u5305\u62ec\u7b2c5\u30017\u30018\u300110\u548c12\u884c\u3002\u8fd9\u662f\u4e00\u4e2a\u76f4\u7ebf\u7a0b\u5e8f\u7684\u4f8b\u5b50\u3002\u76f4\u7ebf\u7a0b\u5e8f\u6bcf\u6b21\u8fd0\u884c\u65f6\u90fd\u91c7\u7528\u76f8\u540c\u7684\u8def\u5f84(\u4ee5\u76f8\u540c\u7684\u987a\u5e8f\u6267\u884c\u76f8\u540c\u7684\u8bed\u53e5)\u3002 \u7136\u800c\uff0c\u8fd9\u5f80\u5f80\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u8981\u6c42\u7528\u6237\u8f93\u5165\uff0c\u800c\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u65e0\u6548\uff0c\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u8981\u6c42\u7528\u6237\u505a\u51fa\u53e6\u4e00\u4e2a\u9009\u62e9\u3002\u8fd9\u5728\u76f4\u7ebf\u987a\u5e8f\u6267\u884c\u7684\u7a0b\u5e8f\u4e2d\u8fd9\u662f\u4e0d\u53ef\u80fd\u7684\u3002\u4e8b\u5b9e\u4e0a\uff0c\u7528\u6237\u53ef\u80fd\u4f1a\u91cd\u590d\u8f93\u5165\u65e0\u6548\u7684\u8f93\u5165\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u80fd\u9700\u8981\u8981\u6c42\u4ed6\u4eec\u505a\u51fa\u53e6\u4e00\u4e2a\u9009\u62e9\u7684\u6b21\u6570\u76f4\u5230\u8fd0\u884c\u65f6\u624d\u77e5\u9053\u3002 \u5e78\u8fd0\u7684\u662f\uff0cC++\u63d0\u4f9b\u4e86\u8bb8\u591a\u4e0d\u540c\u7684\u63a7\u5236\u6d41\u8bed\u53e5(\u4e5f\u79f0\u4e3a\u6d41\u63a7\u5236\u8bed\u53e5)\uff0c\u8fd9\u4e9b\u8bed\u53e5\u5141\u8bb8\u7a0b\u5e8f\u5458\u901a\u8fc7\u7a0b\u5e8f\u6539\u53d8\u6b63\u5e38\u7684\u6267\u884c\u8def\u5f84\u3002\u4f60\u5df2\u7ecf\u770b\u5230\u4e86\u4e00\u4e2a\u5173\u4e8e\u201cif\u8bed\u53e5\u201d\u7684\u4f8b\u5b50(\u5728\u8bfe\u7a0b 4.10 -if\u8bed\u53e5\u7b80\u4ecb \u4e2d\u4ecb\u7ecd)\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u53ea\u5728\u6761\u4ef6\u8868\u8fbe\u5f0f\u4e3a\u771f\u65f6\u6267\u884c\u8bed\u53e5\u3002 \u5f53\u201c\u63a7\u5236\u6d41\u8bed\u53e5\u201d\u5bfc\u81f4\u6267\u884c\u70b9\u66f4\u6539\u4e3a\u975e\u987a\u5e8f\u8bed\u53e5\u65f6\uff0c\u8fd9\u79f0\u4e3a\u5206\u652f\u3002 \u63a7\u5236\u8bed\u53e5\u7684\u5206\u7c7b\uff1a \u5206\u7c7b \u542b\u4e49 \u5b9e\u73b0 \u6761\u4ef6\u8bed\u53e5 \u6761\u4ef6\u8bed\u53e5\u53ea\u5728\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u65f6\u6267\u884c\u4ee3\u7801\u5e8f\u5217 if , switch \u8df3\u8f6c \u8df3\u8f6c\u544a\u8bc9CPU\u4ece\u5176\u4ed6\u4f4d\u7f6e\u5f00\u59cb\u6267\u884c\u8bed\u53e5. Goto , break , continue \u51fd\u6570\u8c03\u7528 \u51fd\u6570\u8c03\u7528\u4f1a\u8df3\u8f6c\u5230\u5176\u4ed6\u5730\u65b9\u6267\u884c\u518d\u8fd4\u56de Function calls , return \u5faa\u73af \u5faa\u73af\u544a\u8bc9\u7a0b\u5e8f\u91cd\u590d\u6267\u884c\u67d0\u4e9b\u8bed\u53e50\u6b21\u6216\u591a\u6b21\uff0c\u77e5\u9053\u6ee1\u8db3\u67d0\u79cd\u6761\u4ef6 while , do-while , for , ranged-for \u6302\u8d77 \u544a\u8bc9\u7a0b\u5e8f\u505c\u6b62\u8fd0\u884c std::exit() , std::abort() \u5f02\u5e38 \u5f02\u5e38\u662f\u4e3a\u9519\u8bef\u5904\u7406\u800c\u8bbe\u8ba1\u7684\u4e00\u79cd\u7279\u6b8a\u7684\u6d41\u63a7\u5236\u7ed3\u6784 try , throw , catch \u6211\u4eec\u4f1a\u5728\u672c\u7ae0\u4ecb\u7ecd\u9664\u5f02\u5e38\u4ee5\u5916\u7684\u5176\u4ed6\u5168\u90e8\u7c7b\u522b\u7684\u63a7\u5236\u8bed\u53e5\u3002 \u5728\u5b66\u4e60\u672c\u7ae0\u524d\uff0c\u4f60\u53ef\u4ee5\u8ba9\u4e00\u4e2a\u7a0b\u5e8f\u505a\u7684\u4e8b\u60c5\u662f\u76f8\u5f53\u6709\u9650\u7684\u3002\u5728\u5b66\u4f1a\u63a7\u5236\u7a0b\u5e8f\u7684\u6d41\u7a0b(\u7279\u522b\u662f\u4f7f\u7528\u5faa\u73af)\u540e\uff0c\u53ef\u4ee5\u505a\u8bb8\u591a\u6709\u8da3\u7684\u4e8b\u60c5\uff01\u6211\u4eec\u5c06\u4e0d\u518d\u5c40\u9650\u4e8e\u73a9\u5177\u7a0b\u5e8f\u548c\u5c0f\u7ec3\u4e60\u2014\u2014\u4f60\u5c06\u80fd\u591f\u7f16\u5199\u771f\u6b63\u6709\u7528\u7684\u7a0b\u5e8f\u3002 \u8fd9\u624d\u662f\u771f\u6b63\u6709\u8da3\u7684\u5f00\u59cb\u3002\u6240\u4ee5\u8ba9\u6211\u4eec\u7ee7\u7eed\u5427\uff01","title":"7.1 - \u63a7\u5236\u6d41"},{"location":"7-10-Break-and-continue/","text":"title: 7.10 - break \u548c continue alias: 7.10 - break \u548c continue origin: /Break-and-continue/ origin_title: \"7.10 - Break and continue\" time: type: translation tags: - break - continue Break \u8bed\u53e5 \u867d\u7136\u60a8\u5df2\u7ecf\u5728 switch \u8bed\u53e5\u7684\u4e0a\u4e0b\u6587\u4e2d\u89c1\u8fc7 break \u8bed\u53e5( 7.4 - switch \u8bed\u53e5\u57fa\u7840 )\uff0c\u4f46\u5b83\u503c\u5f97\u66f4\u4ed4\u7ec6\u7814\u7a76\uff0c\u56e0\u4e3a\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\u63a7\u5236\u6d41\u8bed\u53e5\u3002break\u8bed\u53e5\u5bfc\u81f4while\u5faa\u73af\u3001do-while\u5faa\u73af\u3001for\u5faa\u73af\u6216switch\u8bed\u53e5\u7ed3\u675f\uff0c\u5e76\u5728\u5faa\u73af\u6216switch\u88ab\u6253\u7834\u540e\u7ee7\u7eed\u6267\u884c\u4e0b\u4e00\u4e2a\u8bed\u53e5\u3002 \u4ece switch \u4e2d\u8df3\u51fa \u5728 switch \u8bed\u53e5\u4e2d\uff0c break \u8bed\u53e5\u901a\u5e38\u653e\u7f6e\u5728\u6bcf\u4e2a\u5206\u652f\u7684\u6700\u540e\uff0c\u7528\u4e8e\u9000\u51faswitch\u8bed\u53e5(\u907f\u514d\u8d2f\u7a7f\u6267\u884c\u5206\u652f)\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include void printMath ( int x , int y , char ch ) { switch ( ch ) { case '+' : std :: cout << x << \" + \" << y << \" = \" << x + y << '\\n' ; break ; // don't fall-through to next case case '-' : std :: cout << x << \" - \" << y << \" = \" << x - y << '\\n' ; break ; // don't fall-through to next case case '*' : std :: cout << x << \" * \" << y << \" = \" << x * y << '\\n' ; break ; // don't fall-through to next case case '/' : std :: cout << x << \" / \" << y << \" = \" << x / y << '\\n' ; break ; } } int main () { printMath ( 2 , 3 , '+' ); return 0 ; } \u5173\u4e8e \u8d2f\u7a7f\u5c5e\u6027 \u7684\u66f4\u591a\u8be6\u7ec6\u4fe1\u606f\uff0c\u53c2\u89c1 7.5 - switch fallthrough\u5c5e\u6027\u548c\u4f5c\u7528\u57df \u3002 \u4ece\u5faa\u73af\u4e2d\u8df3\u51fa \u5728\u5faa\u73af\u4e0a\u4e0b\u6587\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528break\u8bed\u53e5\u63d0\u524d\u7ed3\u675f\u5faa\u73af\u5e76\u4ece\u5faa\u73af\u7ed3\u675f\u540e\u7684\u4e0b\u4e00\u6761\u8bed\u53e5\u7ee7\u7eed\u6267\u884c\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include int main () { int sum { 0 }; // Allow the user to enter up to 10 numbers for ( int count { 0 }; count < 10 ; ++ count ) { std :: cout << \"Enter a number to add, or 0 to exit: \" ; int num {}; std :: cin >> num ; // exit loop if user enters 0 if ( num == 0 ) break ; // exit the loop now // otherwise add number to our sum sum += num ; } // execution will continue here after the break std :: cout << \"The sum of all the numbers you entered is: \" << sum << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u5141\u8bb8\u7528\u6237\u6700\u591a\u8f93\u516510\u4e2a\u6570\u5b57\uff0c\u5e76\u663e\u793a\u6700\u540e\u8f93\u5165\u7684\u6240\u6709\u6570\u5b57\u7684\u603b\u548c\u3002\u5982\u679c\u7528\u6237\u8f93\u51650\uff0c\u4e2d\u65ad\u5c06\u5bfc\u81f4\u5faa\u73af\u63d0\u524d\u7ec8\u6b62(\u5728\u8f93\u516510\u4e2a\u6570\u5b57\u4e4b\u524d)\u3002 \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 Enter a number to add, or 0 to exit: 5 Enter a number to add, or 0 to exit: 2 Enter a number to add, or 0 to exit: 1 Enter a number to add, or 0 to exit: 0 The sum of all the numbers you entered is: 8 break \u4e5f\u662f\u4e00\u79cd\u4ece\u4e00\u4e2a\u6545\u610f\u8bbe\u7f6e\u7684\u65e0\u9650\u5faa\u73af\u4e2d\u8df3\u51fa\u7684\u5e38\u89c1\u65b9\u6cd5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { while ( true ) // infinite loop { std :: cout << \"Enter 0 to exit or any other integer to continue: \" ; int num {}; std :: cin >> num ; // exit loop if user enters 0 if ( num == 0 ) break ; } std :: cout << \"We're out! \\n \" ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 Enter 0 to exit or any other integer to continue: 5 Enter 0 to exit or any other integer to continue: 3 Enter 0 to exit or any other integer to continue: 0 We're out! break vs return \u65b0\u7a0b\u5e8f\u5458\u6709\u65f6\u5f88\u96be\u7406\u89e3\u201cbreak\u201d\u548c\u201creturn\u201d\u4e4b\u95f4\u7684\u533a\u522b\u3002break\u8bed\u53e5\u7ec8\u6b62switch\u6216\u5faa\u73af\uff0c\u5e76\u4eceswitch\u6216\u5faa\u73af\u4ee5\u5916\u7684\u7b2c\u4e00\u4e2a\u8bed\u53e5\u7ee7\u7eed\u6267\u884c\u3002return\u8bed\u53e5\u4f1a\u7ec8\u6b62\u5faa\u73af\u6240\u5728\u7684\u6574\u4e2a\u51fd\u6570\uff0c\u5e76\u5728\u51fd\u6570\u88ab\u8c03\u7528\u7684\u5730\u65b9\u7ee7\u7eed\u6267\u884c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include int breakOrReturn () { while ( true ) // infinite loop { std :: cout << \"Enter 'b' to break or 'r' to return: \" ; char ch ; std :: cin >> ch ; if ( ch == 'b' ) break ; // execution will continue at the first statement beyond the loop if ( ch == 'r' ) return 1 ; // return will cause the function to immediately return to the caller (in this case, main()) } // breaking the loop causes execution to resume here std :: cout << \"We broke out of the loop \\n \" ; return 0 ; } int main () { int returnValue { breakOrReturn () }; std :: cout << \"Function breakOrReturn returned \" << returnValue << '\\n' ; return 0 ; } \u8fd0\u884c\u51fd\u6570\u4e24\u6b21\uff1a 1 2 3 4 5 6 Enter 'b' to break or 'r' to return: r Function breakOrReturn returned 1 Enter 'b' to break or 'r' to return: b We broke out of the loop Function breakOrReturn returned 0 continue \u8bed\u53e5 continue \u8bed\u53e5\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u7684\u65b9\u5f0f\u6765\u7ed3\u675f\u5faa\u73af\u7684 \u5f53\u524d\u8fed\u4ee3 \u800c\u4e0d\u7ec8\u6b62\u6574\u4e2a\u5faa\u73af\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 continue \u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { for ( int count { 0 }; count < 10 ; ++ count ) { // if the number is divisible by 4, skip this iteration if (( count % 4 ) == 0 ) continue ; // go to next iteration // If the number is not divisible by 4, keep going std :: cout << count << '\\n' ; // The continue statement jumps to here } return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u8f93\u51fa\u4ece0\u52309\u6240\u6709\u4e0d\u80fd\u88ab4\u6574\u9664\u7684\u6570\uff1a 1 2 3 4 5 6 7 1 2 3 5 6 7 9 continue \u8bed\u53e5\u4f7f\u5f53\u524d\u6267\u884c\u70b9\u8df3\u8f6c\u5230\u5f53\u524d\u5faa\u73af\u7684\u5e95\u90e8\u3002 \u5728 for \u5faa\u73af\u7684\u4e2d\uff0cfor \u5faa\u73af\u7684\u7ed3\u675f\u8bed\u53e5\u4ecd\u7136\u5728 continue \u8bed\u53e5\u4e4b\u540e\u6267\u884c(\u56e0\u4e3a\u8fd9\u53d1\u751f\u5728\u5faa\u73af\u4f53\u7ed3\u675f\u4e4b\u540e)\u3002 \u5728\u4f7f\u7528\u5e26\u6709 while \u6216 do-while \u5faa\u73af\u7684 continue \u8bed\u53e5\u65f6\u8981\u5c0f\u5fc3\u3002\u8fd9\u4e9b\u5faa\u73af\u901a\u5e38\u4f1a\u66f4\u6539\u5faa\u73af\u4f53\u5185\u90e8\u6761\u4ef6\u4e2d\u4f7f\u7528\u7684\u53d8\u91cf\u7684\u503c\u3002\u5982\u679c\u4f7f\u7528 continue \u8bed\u53e5\u5bfc\u81f4\u8fd9\u4e9b\u884c\u88ab\u8df3\u8fc7\uff0c\u90a3\u4e48\u5faa\u73af\u53ef\u80fd\u4f1a\u53d8\u6210\u65e0\u9650\u5faa\u73af! \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { int count { 0 }; while ( count < 10 ) { if ( count == 5 ) continue ; // \u8df3\u8f6c\u5230\u672b\u5c3e std :: cout << count << '\\n' ; ++ count ; // \u5230\u8fbe 5 \u4e4b\u540e\u518d\u4e5f\u4e0d\u4f1a\u88ab\u6267\u884c // \u4ece\u8fd9\u91cc\u6267\u884c } return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u7684\u76ee\u7684\u662f\u6253\u53700\u52309\u4e4b\u95f4\u96645\u4e4b\u5916\u7684\u6240\u6709\u6570\u5b57\u3002\u4f46\u5b83\u5b9e\u9645\u4e0a\u6253\u5370: 1 2 3 4 5 0 1 2 3 4 \u968f\u540e\u5c31\u8fdb\u5165\u4e86\u65e0\u9650\u5faa\u73af\u3002\u5f53 count \u7b49\u4e8e 5 \u65f6\uff0c if \u8bed\u53e5\u6761\u4ef6\u6c42\u503c\u4e3a true \uff0c continue \u8bed\u53e5\u4f7f\u63a7\u5236\u6d41\u8df3\u8f6c\u5230\u5faa\u73af\u5e95\u90e8\u3002\u6240\u4ee5 count \u53d8\u91cf\u65e0\u6cd5\u88ab\u9012\u589e\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4e0b\u4e00\u6b21\u8fed\u4ee3\u65f6 count \u4ecd\u7136\u662f 5 \uff0c\u6240\u4ee5 if \u8bed\u53e5\u6761\u4ef6\u6c42\u503c\u4ecd\u7136\u4e3a true \uff0c\u7a0b\u5e8f\u53d8\u6210\u4e86\u6b7b\u5faa\u73af\u3002 \u5f53\u7136\uff0c\u5982\u679c\u6211\u4eec\u80fd\u591f\u8bc6\u522b\u51fa\u4e00\u4e2a\u5178\u578b\u7684\u8ba1\u6570\u53d8\u91cf\u7684\u8bdd\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528 for \u5faa\u73af\u6765\u4ee3\u66ff while \u6216 do-while \u5faa\u73af\u3002 \u5173\u4e8e break \u548c continue \u7684\u4e89\u8bba \u8bb8\u591a\u6559\u79d1\u4e66\u544a\u8beb\u8bfb\u8005\u4e0d\u8981\u5728\u5faa\u73af\u4e2d\u4f7f\u7528\u201cbreak\u201d\u548c\u201ccontinue\u201d\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5bfc\u81f4\u6267\u884c\u6d41\u56db\u5904\u8df3\u8dc3\uff0c\u4e5f\u4f1a\u4f7f\u903b\u8f91\u6d41\u66f4\u96be\u9075\u5faa\u3002\u4f8b\u5982\uff0c\u5728\u4e00\u4e2a\u590d\u6742\u7684\u903b\u8f91\u7247\u6bb5\u4e2d\u95f4\u7684\u201c\u4e2d\u65ad\u201d\u53ef\u80fd\u88ab\u9519\u8fc7\uff0c\u6216\u8005\u5728\u4ec0\u4e48\u6761\u4ef6\u4e0b\u5e94\u8be5\u89e6\u53d1\u5b83\u53ef\u80fd\u4e0d\u660e\u663e\u3002 \u7136\u800c\uff0c\u5982\u679c\u4f7f\u7528\u5f97\u5f53\uff0cbreak \u548c continue \u53ef\u4ee5\u51cf\u5c11\u5d4c\u5957\u5757\u7684\u6570\u91cf\uff0c\u51cf\u5c11\u5bf9\u590d\u6742\u5faa\u73af\u903b\u8f91\u7684\u9700\u6c42\uff0c\u4ece\u800c\u4f7f\u5faa\u73af\u66f4\u5177\u53ef\u8bfb\u6027\u3002 \u4f8b\u5982\uff0c\u8003\u8651\u4ee5\u4e0b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include int main () { int count { 0 }; // count how many times the loop iterates bool keepLooping { true }; // controls whether the loop ends or not while ( keepLooping ) { std :: cout << \"Enter 'e' to exit this loop or any other character to continue: \" ; char ch {}; std :: cin >> ch ; if ( ch == 'e' ) keepLooping = false ; else { ++ count ; std :: cout << \"We've iterated \" << count << \" times \\n \" ; } } return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u4f7f\u7528\u4e00\u4e2a\u5e03\u5c14\u53d8\u91cf\u6765\u63a7\u5236\u5faa\u73af\u662f\u5426\u7ee7\u7eed\uff0c\u4ee5\u53ca\u4e00\u4e2a\u53ea\u5728\u7528\u6237\u4e0d\u9000\u51fa\u65f6\u8fd0\u884c\u7684\u5d4c\u5957\u5757\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u66f4\u5bb9\u6613\u7406\u89e3\u7684\u7248\u672c\uff0c\u4f7f\u7528 break \u8bed\u53e5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int main () { int count { 0 }; // count how many times the loop iterates while ( true ) // loop until user terminates { std :: cout << \"Enter 'e' to exit this loop or any other character to continue: \" ; char ch {}; std :: cin >> ch ; if ( ch == 'e' ) break ; ++ count ; std :: cout << \"We've iterated \" << count << \" times \\n \" ; } return 0 ; } \u5728\u8fd9\u4e2a\u7248\u672c\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528\u201cbreak\u8bed\u53e5\u201d\uff0c\u907f\u514d\u4e86\u4f7f\u7528\u5e03\u5c14\u53d8\u91cf(\u5e76\u4e14\u5fc5\u987b\u7406\u89e3\u5b83\u7684\u9884\u671f\u7528\u9014\u4ee5\u53ca\u5b83\u7684\u503c\u5728\u54ea\u91cc\u88ab\u66f4\u6539)\u3001\u201celse\u8bed\u53e5\u201d\u548c\u5d4c\u5957\u5757\u3002 \u5c3d\u91cf\u51cf\u5c11\u4f7f\u7528\u7684\u53d8\u91cf\u6570\u91cf\u548c\u4fdd\u6301\u5d4c\u5957\u5757\u7684\u6570\u91cf\u90fd\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7406\u89e3\u6027\uff0c\u800c\u4e0d\u662f\u201cbreak\u201d\u6216\u201ccontinue\u201d\u5bf9\u4ee3\u7801\u7684\u635f\u5bb3\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u8ba4\u4e3a\u660e\u667a\u5730\u4f7f\u7528\u201cbreak\u201d\u6216\u201ccontinue\u201d\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f7f\u7528break\u548ccontinue\u80fd\u591f\u7b80\u5316\u5faa\u73af\u903b\u8f91\uff0c\u5219\u4f7f\u7528\u5b83\u4eec\u3002 \u5173\u4e8e\u63d0\u524d\u8fd4\u56de\u7684\u4e89\u8bba \u5bf9\u4e8e return \u8bed\u53e5\u4e5f\u6709\u7c7b\u4f3c\u7684\u4e89\u8bba\u3002\u4e0d\u662f\u51fd\u6570\u4e2d\u6700\u540e\u4e00\u6761\u8bed\u53e5\u7684\u8fd4\u56de\u8bed\u53e5\u5219\u79f0\u4e4b\u4e3a \u63d0\u524d\u8fd4\u56de\u8bed\u53e5 \u3002\u8bb8\u591a\u7a0b\u5e8f\u5458\u8ba4\u4e3a\u5e94\u8be5\u907f\u514d\u63d0\u524d\u8fd4\u56de\u3002\u5728\u51fd\u6570\u5e95\u90e8\u53ea\u6709\u4e00\u4e2a return \u8bed\u53e5\u7684\u51fd\u6570\u66f4\u7b80\u5355\u2014\u2014\u4f60\u53ef\u4ee5\u5047\u8bbe\u51fd\u6570\u5c06\u63a5\u53d7\u5b83\u7684\u53c2\u6570\uff0c\u6267\u884c\u5b83\u5b9e\u73b0\u7684\u4efb\u4f55\u903b\u8f91\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u6ca1\u6709\u504f\u5dee\u7684\u7ed3\u679c\u3002\u989d\u5916\u7684 return \u8bed\u53e5\u4f1a\u4f7f\u903b\u8f91\u53d8\u5f97\u590d\u6742\u3002 \u53cd\u65b9\u5219\u8ba4\u4e3a\uff0c\u4f7f\u7528\u63d0\u524d\u8fd4\u56de\u53ef\u4ee5\u8ba9\u51fd\u6570\u5728\u5b8c\u6210\u540e\u7acb\u5373\u9000\u51fa\uff0c\u8fd9\u6837\u5c31\u51cf\u5c11\u4e86\u9605\u8bfb\u4e0d\u5fc5\u8981\u7684\u903b\u8f91\uff0c\u5e76\u51cf\u5c11\u4e86\u5bf9\u6761\u4ef6\u5d4c\u5957\u5757\u7684\u9700\u6c42\uff0c\u4ece\u800c\u4f7f\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\u3002 \u6709\u4e9b\u5f00\u53d1\u4eba\u5458\u91c7\u53d6\u4e2d\u95f4\u7acb\u573a\uff0c \u53ea\u5728\u51fd\u6570\u9876\u90e8\u4f7f\u7528\u63d0\u524d\u8fd4\u56de\u6765\u8fdb\u884c\u53c2\u6570\u9a8c\u8bc1 (\u6355\u83b7\u4f20\u5165\u7684\u9519\u8bef\u53c2\u6570)\uff0c\u7136\u540e\u5728\u51fd\u6570\u4e4b\u540e\u7684\u90e8\u5206\u4fdd\u8bc1\u53ea\u4f7f\u7528\u4e00\u4e2a return \u8bed\u53e5\u3002 \u6211\u4eec\u7684\u7acb\u573a\u662f\uff0c\u63d0\u524d\u8fd4\u56de\u5229\u5927\u4e8e\u5f0a\uff0c\u4f46\u5728\u5b9e\u8df5\u4e2d\u8981\u56e0\u5730\u5236\u5b9c\u5730\u4f7f\u7528\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u63d0\u524d\u8fd4\u56de\u80fd\u591f\u7b80\u5316\u51fd\u6570\u903b\u8f91\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u5b83","title":"7 10 Break and continue"},{"location":"7-10-Break-and-continue/#break","text":"\u867d\u7136\u60a8\u5df2\u7ecf\u5728 switch \u8bed\u53e5\u7684\u4e0a\u4e0b\u6587\u4e2d\u89c1\u8fc7 break \u8bed\u53e5( 7.4 - switch \u8bed\u53e5\u57fa\u7840 )\uff0c\u4f46\u5b83\u503c\u5f97\u66f4\u4ed4\u7ec6\u7814\u7a76\uff0c\u56e0\u4e3a\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\u63a7\u5236\u6d41\u8bed\u53e5\u3002break\u8bed\u53e5\u5bfc\u81f4while\u5faa\u73af\u3001do-while\u5faa\u73af\u3001for\u5faa\u73af\u6216switch\u8bed\u53e5\u7ed3\u675f\uff0c\u5e76\u5728\u5faa\u73af\u6216switch\u88ab\u6253\u7834\u540e\u7ee7\u7eed\u6267\u884c\u4e0b\u4e00\u4e2a\u8bed\u53e5\u3002","title":"Break \u8bed\u53e5"},{"location":"7-10-Break-and-continue/#switch","text":"\u5728 switch \u8bed\u53e5\u4e2d\uff0c break \u8bed\u53e5\u901a\u5e38\u653e\u7f6e\u5728\u6bcf\u4e2a\u5206\u652f\u7684\u6700\u540e\uff0c\u7528\u4e8e\u9000\u51faswitch\u8bed\u53e5(\u907f\u514d\u8d2f\u7a7f\u6267\u884c\u5206\u652f)\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include void printMath ( int x , int y , char ch ) { switch ( ch ) { case '+' : std :: cout << x << \" + \" << y << \" = \" << x + y << '\\n' ; break ; // don't fall-through to next case case '-' : std :: cout << x << \" - \" << y << \" = \" << x - y << '\\n' ; break ; // don't fall-through to next case case '*' : std :: cout << x << \" * \" << y << \" = \" << x * y << '\\n' ; break ; // don't fall-through to next case case '/' : std :: cout << x << \" / \" << y << \" = \" << x / y << '\\n' ; break ; } } int main () { printMath ( 2 , 3 , '+' ); return 0 ; } \u5173\u4e8e \u8d2f\u7a7f\u5c5e\u6027 \u7684\u66f4\u591a\u8be6\u7ec6\u4fe1\u606f\uff0c\u53c2\u89c1 7.5 - switch fallthrough\u5c5e\u6027\u548c\u4f5c\u7528\u57df \u3002","title":"\u4ece switch \u4e2d\u8df3\u51fa"},{"location":"7-10-Break-and-continue/#_1","text":"\u5728\u5faa\u73af\u4e0a\u4e0b\u6587\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528break\u8bed\u53e5\u63d0\u524d\u7ed3\u675f\u5faa\u73af\u5e76\u4ece\u5faa\u73af\u7ed3\u675f\u540e\u7684\u4e0b\u4e00\u6761\u8bed\u53e5\u7ee7\u7eed\u6267\u884c\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include int main () { int sum { 0 }; // Allow the user to enter up to 10 numbers for ( int count { 0 }; count < 10 ; ++ count ) { std :: cout << \"Enter a number to add, or 0 to exit: \" ; int num {}; std :: cin >> num ; // exit loop if user enters 0 if ( num == 0 ) break ; // exit the loop now // otherwise add number to our sum sum += num ; } // execution will continue here after the break std :: cout << \"The sum of all the numbers you entered is: \" << sum << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u5141\u8bb8\u7528\u6237\u6700\u591a\u8f93\u516510\u4e2a\u6570\u5b57\uff0c\u5e76\u663e\u793a\u6700\u540e\u8f93\u5165\u7684\u6240\u6709\u6570\u5b57\u7684\u603b\u548c\u3002\u5982\u679c\u7528\u6237\u8f93\u51650\uff0c\u4e2d\u65ad\u5c06\u5bfc\u81f4\u5faa\u73af\u63d0\u524d\u7ec8\u6b62(\u5728\u8f93\u516510\u4e2a\u6570\u5b57\u4e4b\u524d)\u3002 \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 5 Enter a number to add, or 0 to exit: 5 Enter a number to add, or 0 to exit: 2 Enter a number to add, or 0 to exit: 1 Enter a number to add, or 0 to exit: 0 The sum of all the numbers you entered is: 8 break \u4e5f\u662f\u4e00\u79cd\u4ece\u4e00\u4e2a\u6545\u610f\u8bbe\u7f6e\u7684\u65e0\u9650\u5faa\u73af\u4e2d\u8df3\u51fa\u7684\u5e38\u89c1\u65b9\u6cd5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { while ( true ) // infinite loop { std :: cout << \"Enter 0 to exit or any other integer to continue: \" ; int num {}; std :: cin >> num ; // exit loop if user enters 0 if ( num == 0 ) break ; } std :: cout << \"We're out! \\n \" ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 Enter 0 to exit or any other integer to continue: 5 Enter 0 to exit or any other integer to continue: 3 Enter 0 to exit or any other integer to continue: 0 We're out!","title":"\u4ece\u5faa\u73af\u4e2d\u8df3\u51fa"},{"location":"7-10-Break-and-continue/#break-vs-return","text":"\u65b0\u7a0b\u5e8f\u5458\u6709\u65f6\u5f88\u96be\u7406\u89e3\u201cbreak\u201d\u548c\u201creturn\u201d\u4e4b\u95f4\u7684\u533a\u522b\u3002break\u8bed\u53e5\u7ec8\u6b62switch\u6216\u5faa\u73af\uff0c\u5e76\u4eceswitch\u6216\u5faa\u73af\u4ee5\u5916\u7684\u7b2c\u4e00\u4e2a\u8bed\u53e5\u7ee7\u7eed\u6267\u884c\u3002return\u8bed\u53e5\u4f1a\u7ec8\u6b62\u5faa\u73af\u6240\u5728\u7684\u6574\u4e2a\u51fd\u6570\uff0c\u5e76\u5728\u51fd\u6570\u88ab\u8c03\u7528\u7684\u5730\u65b9\u7ee7\u7eed\u6267\u884c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include int breakOrReturn () { while ( true ) // infinite loop { std :: cout << \"Enter 'b' to break or 'r' to return: \" ; char ch ; std :: cin >> ch ; if ( ch == 'b' ) break ; // execution will continue at the first statement beyond the loop if ( ch == 'r' ) return 1 ; // return will cause the function to immediately return to the caller (in this case, main()) } // breaking the loop causes execution to resume here std :: cout << \"We broke out of the loop \\n \" ; return 0 ; } int main () { int returnValue { breakOrReturn () }; std :: cout << \"Function breakOrReturn returned \" << returnValue << '\\n' ; return 0 ; } \u8fd0\u884c\u51fd\u6570\u4e24\u6b21\uff1a 1 2 3 4 5 6 Enter 'b' to break or 'r' to return: r Function breakOrReturn returned 1 Enter 'b' to break or 'r' to return: b We broke out of the loop Function breakOrReturn returned 0","title":"break vs return"},{"location":"7-10-Break-and-continue/#continue","text":"continue \u8bed\u53e5\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u7684\u65b9\u5f0f\u6765\u7ed3\u675f\u5faa\u73af\u7684 \u5f53\u524d\u8fed\u4ee3 \u800c\u4e0d\u7ec8\u6b62\u6574\u4e2a\u5faa\u73af\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 continue \u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { for ( int count { 0 }; count < 10 ; ++ count ) { // if the number is divisible by 4, skip this iteration if (( count % 4 ) == 0 ) continue ; // go to next iteration // If the number is not divisible by 4, keep going std :: cout << count << '\\n' ; // The continue statement jumps to here } return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u8f93\u51fa\u4ece0\u52309\u6240\u6709\u4e0d\u80fd\u88ab4\u6574\u9664\u7684\u6570\uff1a 1 2 3 4 5 6 7 1 2 3 5 6 7 9 continue \u8bed\u53e5\u4f7f\u5f53\u524d\u6267\u884c\u70b9\u8df3\u8f6c\u5230\u5f53\u524d\u5faa\u73af\u7684\u5e95\u90e8\u3002 \u5728 for \u5faa\u73af\u7684\u4e2d\uff0cfor \u5faa\u73af\u7684\u7ed3\u675f\u8bed\u53e5\u4ecd\u7136\u5728 continue \u8bed\u53e5\u4e4b\u540e\u6267\u884c(\u56e0\u4e3a\u8fd9\u53d1\u751f\u5728\u5faa\u73af\u4f53\u7ed3\u675f\u4e4b\u540e)\u3002 \u5728\u4f7f\u7528\u5e26\u6709 while \u6216 do-while \u5faa\u73af\u7684 continue \u8bed\u53e5\u65f6\u8981\u5c0f\u5fc3\u3002\u8fd9\u4e9b\u5faa\u73af\u901a\u5e38\u4f1a\u66f4\u6539\u5faa\u73af\u4f53\u5185\u90e8\u6761\u4ef6\u4e2d\u4f7f\u7528\u7684\u53d8\u91cf\u7684\u503c\u3002\u5982\u679c\u4f7f\u7528 continue \u8bed\u53e5\u5bfc\u81f4\u8fd9\u4e9b\u884c\u88ab\u8df3\u8fc7\uff0c\u90a3\u4e48\u5faa\u73af\u53ef\u80fd\u4f1a\u53d8\u6210\u65e0\u9650\u5faa\u73af! \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { int count { 0 }; while ( count < 10 ) { if ( count == 5 ) continue ; // \u8df3\u8f6c\u5230\u672b\u5c3e std :: cout << count << '\\n' ; ++ count ; // \u5230\u8fbe 5 \u4e4b\u540e\u518d\u4e5f\u4e0d\u4f1a\u88ab\u6267\u884c // \u4ece\u8fd9\u91cc\u6267\u884c } return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u7684\u76ee\u7684\u662f\u6253\u53700\u52309\u4e4b\u95f4\u96645\u4e4b\u5916\u7684\u6240\u6709\u6570\u5b57\u3002\u4f46\u5b83\u5b9e\u9645\u4e0a\u6253\u5370: 1 2 3 4 5 0 1 2 3 4 \u968f\u540e\u5c31\u8fdb\u5165\u4e86\u65e0\u9650\u5faa\u73af\u3002\u5f53 count \u7b49\u4e8e 5 \u65f6\uff0c if \u8bed\u53e5\u6761\u4ef6\u6c42\u503c\u4e3a true \uff0c continue \u8bed\u53e5\u4f7f\u63a7\u5236\u6d41\u8df3\u8f6c\u5230\u5faa\u73af\u5e95\u90e8\u3002\u6240\u4ee5 count \u53d8\u91cf\u65e0\u6cd5\u88ab\u9012\u589e\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4e0b\u4e00\u6b21\u8fed\u4ee3\u65f6 count \u4ecd\u7136\u662f 5 \uff0c\u6240\u4ee5 if \u8bed\u53e5\u6761\u4ef6\u6c42\u503c\u4ecd\u7136\u4e3a true \uff0c\u7a0b\u5e8f\u53d8\u6210\u4e86\u6b7b\u5faa\u73af\u3002 \u5f53\u7136\uff0c\u5982\u679c\u6211\u4eec\u80fd\u591f\u8bc6\u522b\u51fa\u4e00\u4e2a\u5178\u578b\u7684\u8ba1\u6570\u53d8\u91cf\u7684\u8bdd\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528 for \u5faa\u73af\u6765\u4ee3\u66ff while \u6216 do-while \u5faa\u73af\u3002","title":"continue \u8bed\u53e5"},{"location":"7-10-Break-and-continue/#break-continue","text":"\u8bb8\u591a\u6559\u79d1\u4e66\u544a\u8beb\u8bfb\u8005\u4e0d\u8981\u5728\u5faa\u73af\u4e2d\u4f7f\u7528\u201cbreak\u201d\u548c\u201ccontinue\u201d\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5bfc\u81f4\u6267\u884c\u6d41\u56db\u5904\u8df3\u8dc3\uff0c\u4e5f\u4f1a\u4f7f\u903b\u8f91\u6d41\u66f4\u96be\u9075\u5faa\u3002\u4f8b\u5982\uff0c\u5728\u4e00\u4e2a\u590d\u6742\u7684\u903b\u8f91\u7247\u6bb5\u4e2d\u95f4\u7684\u201c\u4e2d\u65ad\u201d\u53ef\u80fd\u88ab\u9519\u8fc7\uff0c\u6216\u8005\u5728\u4ec0\u4e48\u6761\u4ef6\u4e0b\u5e94\u8be5\u89e6\u53d1\u5b83\u53ef\u80fd\u4e0d\u660e\u663e\u3002 \u7136\u800c\uff0c\u5982\u679c\u4f7f\u7528\u5f97\u5f53\uff0cbreak \u548c continue \u53ef\u4ee5\u51cf\u5c11\u5d4c\u5957\u5757\u7684\u6570\u91cf\uff0c\u51cf\u5c11\u5bf9\u590d\u6742\u5faa\u73af\u903b\u8f91\u7684\u9700\u6c42\uff0c\u4ece\u800c\u4f7f\u5faa\u73af\u66f4\u5177\u53ef\u8bfb\u6027\u3002 \u4f8b\u5982\uff0c\u8003\u8651\u4ee5\u4e0b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include int main () { int count { 0 }; // count how many times the loop iterates bool keepLooping { true }; // controls whether the loop ends or not while ( keepLooping ) { std :: cout << \"Enter 'e' to exit this loop or any other character to continue: \" ; char ch {}; std :: cin >> ch ; if ( ch == 'e' ) keepLooping = false ; else { ++ count ; std :: cout << \"We've iterated \" << count << \" times \\n \" ; } } return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u4f7f\u7528\u4e00\u4e2a\u5e03\u5c14\u53d8\u91cf\u6765\u63a7\u5236\u5faa\u73af\u662f\u5426\u7ee7\u7eed\uff0c\u4ee5\u53ca\u4e00\u4e2a\u53ea\u5728\u7528\u6237\u4e0d\u9000\u51fa\u65f6\u8fd0\u884c\u7684\u5d4c\u5957\u5757\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u66f4\u5bb9\u6613\u7406\u89e3\u7684\u7248\u672c\uff0c\u4f7f\u7528 break \u8bed\u53e5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int main () { int count { 0 }; // count how many times the loop iterates while ( true ) // loop until user terminates { std :: cout << \"Enter 'e' to exit this loop or any other character to continue: \" ; char ch {}; std :: cin >> ch ; if ( ch == 'e' ) break ; ++ count ; std :: cout << \"We've iterated \" << count << \" times \\n \" ; } return 0 ; } \u5728\u8fd9\u4e2a\u7248\u672c\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528\u201cbreak\u8bed\u53e5\u201d\uff0c\u907f\u514d\u4e86\u4f7f\u7528\u5e03\u5c14\u53d8\u91cf(\u5e76\u4e14\u5fc5\u987b\u7406\u89e3\u5b83\u7684\u9884\u671f\u7528\u9014\u4ee5\u53ca\u5b83\u7684\u503c\u5728\u54ea\u91cc\u88ab\u66f4\u6539)\u3001\u201celse\u8bed\u53e5\u201d\u548c\u5d4c\u5957\u5757\u3002 \u5c3d\u91cf\u51cf\u5c11\u4f7f\u7528\u7684\u53d8\u91cf\u6570\u91cf\u548c\u4fdd\u6301\u5d4c\u5957\u5757\u7684\u6570\u91cf\u90fd\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7406\u89e3\u6027\uff0c\u800c\u4e0d\u662f\u201cbreak\u201d\u6216\u201ccontinue\u201d\u5bf9\u4ee3\u7801\u7684\u635f\u5bb3\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u8ba4\u4e3a\u660e\u667a\u5730\u4f7f\u7528\u201cbreak\u201d\u6216\u201ccontinue\u201d\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f7f\u7528break\u548ccontinue\u80fd\u591f\u7b80\u5316\u5faa\u73af\u903b\u8f91\uff0c\u5219\u4f7f\u7528\u5b83\u4eec\u3002","title":"\u5173\u4e8e break \u548c continue \u7684\u4e89\u8bba"},{"location":"7-10-Break-and-continue/#_2","text":"\u5bf9\u4e8e return \u8bed\u53e5\u4e5f\u6709\u7c7b\u4f3c\u7684\u4e89\u8bba\u3002\u4e0d\u662f\u51fd\u6570\u4e2d\u6700\u540e\u4e00\u6761\u8bed\u53e5\u7684\u8fd4\u56de\u8bed\u53e5\u5219\u79f0\u4e4b\u4e3a \u63d0\u524d\u8fd4\u56de\u8bed\u53e5 \u3002\u8bb8\u591a\u7a0b\u5e8f\u5458\u8ba4\u4e3a\u5e94\u8be5\u907f\u514d\u63d0\u524d\u8fd4\u56de\u3002\u5728\u51fd\u6570\u5e95\u90e8\u53ea\u6709\u4e00\u4e2a return \u8bed\u53e5\u7684\u51fd\u6570\u66f4\u7b80\u5355\u2014\u2014\u4f60\u53ef\u4ee5\u5047\u8bbe\u51fd\u6570\u5c06\u63a5\u53d7\u5b83\u7684\u53c2\u6570\uff0c\u6267\u884c\u5b83\u5b9e\u73b0\u7684\u4efb\u4f55\u903b\u8f91\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u6ca1\u6709\u504f\u5dee\u7684\u7ed3\u679c\u3002\u989d\u5916\u7684 return \u8bed\u53e5\u4f1a\u4f7f\u903b\u8f91\u53d8\u5f97\u590d\u6742\u3002 \u53cd\u65b9\u5219\u8ba4\u4e3a\uff0c\u4f7f\u7528\u63d0\u524d\u8fd4\u56de\u53ef\u4ee5\u8ba9\u51fd\u6570\u5728\u5b8c\u6210\u540e\u7acb\u5373\u9000\u51fa\uff0c\u8fd9\u6837\u5c31\u51cf\u5c11\u4e86\u9605\u8bfb\u4e0d\u5fc5\u8981\u7684\u903b\u8f91\uff0c\u5e76\u51cf\u5c11\u4e86\u5bf9\u6761\u4ef6\u5d4c\u5957\u5757\u7684\u9700\u6c42\uff0c\u4ece\u800c\u4f7f\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\u3002 \u6709\u4e9b\u5f00\u53d1\u4eba\u5458\u91c7\u53d6\u4e2d\u95f4\u7acb\u573a\uff0c \u53ea\u5728\u51fd\u6570\u9876\u90e8\u4f7f\u7528\u63d0\u524d\u8fd4\u56de\u6765\u8fdb\u884c\u53c2\u6570\u9a8c\u8bc1 (\u6355\u83b7\u4f20\u5165\u7684\u9519\u8bef\u53c2\u6570)\uff0c\u7136\u540e\u5728\u51fd\u6570\u4e4b\u540e\u7684\u90e8\u5206\u4fdd\u8bc1\u53ea\u4f7f\u7528\u4e00\u4e2a return \u8bed\u53e5\u3002 \u6211\u4eec\u7684\u7acb\u573a\u662f\uff0c\u63d0\u524d\u8fd4\u56de\u5229\u5927\u4e8e\u5f0a\uff0c\u4f46\u5728\u5b9e\u8df5\u4e2d\u8981\u56e0\u5730\u5236\u5b9c\u5730\u4f7f\u7528\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u63d0\u524d\u8fd4\u56de\u80fd\u591f\u7b80\u5316\u51fd\u6570\u903b\u8f91\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u5b83","title":"\u5173\u4e8e\u63d0\u524d\u8fd4\u56de\u7684\u4e89\u8bba"},{"location":"7-11-Halts-exiting-your-program-early/","tags":["halt"],"text":"Key Takeaway \u5728\u672c\u7ae0\u4e2d\u6211\u4eec\u5c06\u4ecb\u7ecd\u7684\u6700\u540e\u4e00\u7c7b\u6d41\u63a7\u5236\u8bed\u53e5\u662f\u6302\u8d77\u8bed\u53e5\u3002\u6302\u8d77\u7ec8\u6b62\u7a0b\u5e8f\u7684\u6d41\u63a7\u5236\u8bed\u53e5\u3002\u5728C++\u4e2d\uff0c\u7a0b\u5e8f\u505c\u6b62\u88ab\u5b9e\u73b0\u4e3a\u51fd\u6570(\u800c\u4e0d\u662f\u5173\u952e\u5b57)\uff0c\u56e0\u6b64\u6211\u4eec\u7684\u505c\u6b62\u8bed\u53e5\u5c06\u662f\u51fd\u6570\u8c03\u7528\u3002 \u8ba9\u6211\u4eec\u7ed5\u4e2a\u5c0f\u5f2f\u5b50\uff0c\u56de\u987e\u4e00\u4e0b\u7a0b\u5e8f\u6b63\u5e38\u9000\u51fa\u65f6\u53d1\u751f\u7684\u60c5\u51b5\u3002\u5f53 main() \u51fd\u6570\u8fd4\u56de\u65f6(\u5230\u8fbe\u51fd\u6570\u7684\u672b\u5c3e\uff0c\u6216\u8005\u901a\u8fc7 return \u8bed\u53e5)\uff0c\u4f1a\u53d1\u751f\u8bb8\u591a\u4e0d\u540c\u7684\u4e8b\u60c5\u3002 \u9996\u5148\uff0c\u56e0\u4e3a\u6211\u4eec\u8981\u79bb\u5f00\u51fd\u6570\uff0c\u6240\u4ee5\u6240\u6709\u5c40\u90e8\u53d8\u91cf\u548c\u51fd\u6570\u5f62\u53c2\u90fd\u88ab\u9500\u6bc1(\u50cf\u5f80\u5e38\u4e00\u6837)\u3002 \u63a5\u4e0b\u6765\uff0c\u4e00\u4e2a\u540d\u4e3a std::exit() \u7684\u7279\u6b8a\u51fd\u6570\u88ab\u8c03\u7528\uff0c\u5e76\u5c06 main() \u7684\u8fd4\u56de\u503c( \u72b6\u6001\u7801 )\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u3002\u90a3\u4e48 std::exit() \u662f\u4ec0\u4e48\u5462? std::exit() \u51fd\u6570 std::exit() \u662f\u4e00\u4e2a\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u6b63\u5e38\u7ec8\u6b62\u7684\u51fd\u6570\u3002\u6b63\u5e38\u7ec8\u6b62\u7684\u610f\u601d\u662f\uff0c\u7a0b\u5e8f\u4ee5\u6211\u4eec\u671f\u671b\u7684\u65b9\u5f0f\u9000\u51fa\u3002\u6ce8\u610f\u672f\u8bed \u6b63\u5e38\u7ec8\u6b62 \u5e76\u4e0d\u9690\u542b\u7a0b\u5e8f\u662f\u5426\u6267\u884c\u6210\u529f\u7684\u542b\u4e49\uff08\u8fd9\u662f\u8fd4\u56de\u72b6\u6001\u7801\u7684\u4f5c\u7528\uff09\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a\u7a0b\u5e8f\u9700\u8981\u7528\u6237\u8f93\u5165\u6587\u4ef6\u540d\u3002\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u662f\u975e\u6cd5\u7684\u6587\u4ef6\u540d\uff0c\u5219\u7a0b\u5e8f\u901a\u5e38\u5e94\u8be5\u8fd4\u56de\u975e\u96f6\u7684\u72b6\u6001\u7801\u6765\u8868\u793a\u5931\u8d25\uff0c\u4f46\u662f\u8be5\u7a0b\u5e8f\u4ecd\u7136\u53ef\u4ee5 \u6b63\u5e38\u7ec8\u6b62 \u3002 std::exit() \u6267\u884c\u8bb8\u591a\u6e05\u7406\u51fd\u6570\u3002\u9996\u5148\uff0c\u9500\u6bc1\u5177\u6709\u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u7684\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u5982\u679c\u4f7f\u7528\u4e86\u4efb\u4f55\u6587\u4ef6\uff0c\u5219\u6267\u884c\u5176\u4ed6\u4e00\u4e9b\u6742\u9879\u6587\u4ef6\u6e05\u7406\u3002\u6700\u540e\uff0c\u63a7\u5236\u88ab\u8fd4\u56de\u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff0c\u4f20\u9012\u7ed9 std::exit() \u7684\u53c2\u6570\u88ab\u7528\u4f5c \u72b6\u6001\u7801 \u3002 \u663e\u5f0f\u8c03\u7528 std::exit() \u867d\u7136 std::exit() \u5728 main() \u7ed3\u675f\u65f6\u4f1a\u88ab\u9690\u5f0f\u8c03\u7528\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u88ab\u663e\u5f0f\u8c03\u7528\uff0c\u4ee5\u5728\u7a0b\u5e8f\u6b63\u5e38\u7ec8\u6b62\u524d\u505c\u6b62\u7a0b\u5e8f\u3002\u5f53\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u8c03\u7528 std::exit() \u65f6\uff0c\u4f60\u9700\u8981\u5305\u542b cstdlib \u5934\u6587\u4ef6\u3002 \u4e0b\u9762\u662f\u663e\u5f0f\u8c03\u7528 std::exit() \u7684\u4e00\u4e2a\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include // for std::exit() #include void cleanup () { // code here to do any kind of cleanup required std :: cout << \"cleanup! \\n \" ; } int main () { std :: cout << 1 << '\\n' ; cleanup (); std :: exit ( 0 ); // terminate and return status code 0 to operating system // The following statements never execute std :: cout << 2 << '\\n' ; return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 2 1 cleanup! \u6ce8\u610f\uff0c std::exit() \u4e4b\u540e\u7684\u8bed\u53e5\u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c\uff0c\u56e0\u4e3a\u7a0b\u5e8f\u5df2\u7ecf\u7ec8\u6b62\u4e86\u3002 \u867d\u7136\u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4ece\u51fd\u6570 main() \u8c03\u7528 std::exit() \uff0c\u4f46\u4e5f\u53ef\u4ee5\u4ece\u4efb\u4f55\u51fd\u6570\u8c03\u7528 std::exit() \u6765\u7ec8\u6b62\u7a0b\u5e8f\u3002 \u5173\u4e8e\u663e\u5f0f\u8c03\u7528 std::exit() \u7684\u4e00\u4e2a\u91cd\u8981\u6ce8\u610f\u4e8b\u9879\u662f\uff1a std::exit() \u4e0d\u4f1a\u6e05\u9664\u4efb\u4f55\u672c\u5730\u53d8\u91cf(\u65e0\u8bba\u662f\u5728\u5f53\u524d\u51fd\u6570\u4e2d\uff0c\u8fd8\u662f\u5728\u8c03\u7528\u6808\u4e2d\u7684\u51fd\u6570)\u3002\u56e0\u6b64\uff0c\u901a\u5e38\u6700\u597d\u907f\u514d\u8c03\u7528 std::exit() \u3002 \u6ce8\u610f std::exit() \u51fd\u6570\u4e0d\u4f1a\u6e05\u7406\u5f53\u524d\u51fd\u6570\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf\u6216\u6e05\u7406\u8c03\u7528\u5806\u6808\u3002 std::atexit \u56e0\u4e3a std::exit() \u4f1a\u7acb\u5373\u7ec8\u6b62\u7a0b\u5e8f\uff0c\u6240\u4ee5\u4f60\u53ef\u80fd\u9700\u8981\u5728\u7ec8\u6b62\u524d\u624b\u52a8\u6267\u884c\u4e00\u4e9b\u6e05\u7406\u64cd\u4f5c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6e05\u7406\u610f\u5473\u7740\u5173\u95ed\u6570\u636e\u5e93\u6216\u7f51\u7edc\u8fde\u63a5\u3001\u91ca\u653e\u6240\u5206\u914d\u7684\u5185\u5b58\u3001\u5c06\u4fe1\u606f\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\u7b49\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8c03\u7528\u51fd\u6570 cleanup() \u6765\u5904\u7406\u6e05\u7406\u4efb\u52a1\u3002\u7136\u800c\uff0c\u8bb0\u4f4f\u5728\u8c03\u7528 exit() \u4e4b\u524d\u624b\u52a8\u8c03\u7528\u4e00\u4e2a\u6e05\u7406\u51fd\u6570\u4f1a\u7ed9\u7a0b\u5e8f\u5458\u589e\u52a0\u8d1f\u62c5\u3002 \u4e3a\u4e86\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u8fd9\u9879\u5de5\u4f5c\uff0cC++\u63d0\u4f9b\u4e86 std::atexit() \u51fd\u6570\uff0c\u5b83\u5141\u8bb8\u6307\u5b9a\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u5728\u7a0b\u5e8f\u7ec8\u6b62\u65f6\u901a\u8fc7 std::exit() \u81ea\u52a8\u8c03\u7528\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 12.1 - \u51fd\u6570\u6307\u9488 \u4e2d\u4ecb\u7ecd\u5982\u4f55\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012 \u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include // for std::exit() #include void cleanup () { // code here to do any kind of cleanup required std :: cout << \"cleanup! \\n \" ; } int main () { // register cleanup() to be called automatically when std::exit() is called std :: atexit ( cleanup ); // note: we use cleanup rather than cleanup() since we're not making a function call to cleanup() right now std :: cout << 1 << '\\n' ; std :: exit ( 0 ); // terminate and return status code 0 to operating system // The following statements never execute std :: cout << 2 << '\\n' ; return 0 ; } \u8be5\u7a0b\u5e8f\u7684\u8f93\u51fa\u4e0e\u524d\u9762\u7684\u793a\u4f8b\u76f8\u540c\uff1a 1 2 1 cleanup! \u4e3a\u4ec0\u4e48\u8981\u8fd9\u4e48\u505a\u5462\uff1f\u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u5728\u4e00\u4e2a\u5730\u65b9\u6307\u5b9a\u4e00\u4e2a\u6e05\u7406\u51fd\u6570(\u53ef\u80fd\u5728 main \u4e2d)\uff0c\u7136\u540e\u4e0d\u5fc5\u62c5\u5fc3\u5728\u8c03\u7528 std::exit() \u4e4b\u524d\u8bb0\u5f97\u663e\u5f0f\u8c03\u7528\u8be5\u51fd\u6570\u3002 \u9996\u5148\uff0c\u7531\u4e8e std::exit() \u5728 main() \u7ec8\u6b62\u65f6\u9690\u5f0f\u8c03\u7528\uff0c\u5982\u679c\u7a0b\u5e8f\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u9000\u51fa\uff0c std::atexit() \u6ce8\u518c\u7684\u4efb\u4f55\u51fd\u6570\u90fd\u4f1a\u88ab\u8c03\u7528\u3002\u7b2c\u4e8c\uff0c\u88ab\u6ce8\u518c\u7684\u51fd\u6570\u5fc5\u987b\u4e0d\u5e26\u53c2\u6570\uff0c\u4e5f\u6ca1\u6709\u8fd4\u56de\u503c\u3002\u6700\u540e\uff0c\u5982\u679c\u4f60\u613f\u610f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 std::atexit() \u6ce8\u518c\u591a\u4e2a\u6e05\u7406\u51fd\u6570\uff0c\u5b83\u4eec\u5c06\u6309\u6ce8\u518c\u7684\u76f8\u53cd\u987a\u5e8f\u8c03\u7528(\u6700\u540e\u6ce8\u518c\u7684\u5c06\u6700\u5148\u8c03\u7528)\u3002 \u6269\u5c55\u9605\u8bfb \u5728\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u4e2d\uff0c\u8c03\u7528 std::exit() \u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83(\u56e0\u4e3a\u8c03\u7528 std::exit() \u7684\u7ebf\u7a0b\u5c06\u6e05\u7406\u4ecd\u7136\u53ef\u80fd\u88ab\u5176\u4ed6\u7ebf\u7a0b\u8bbf\u95ee\u7684\u9759\u6001\u5bf9\u8c61)\u3002\u56e0\u6b64\uff0cC++\u5f15\u5165\u4e86\u53e6\u4e00\u5bf9\u51fd\u6570\uff0c\u5b83\u4eec\u7684\u5de5\u4f5c\u65b9\u5f0f\u7c7b\u4f3c\u4e8e std::exit() \u548c std::atexit() \uff0c\u5b83\u4eec\u88ab\u79f0\u4e3a std::quick_exit() \u548c std::at_quick_exit() \u3002 std::quick_exit() \u4f1a\u6b63\u5e38\u5730\u7ec8\u6b62\u7a0b\u5e8f\uff0c\u4f46\u4e0d\u4f1a\u6e05\u7406\u9759\u6001\u5bf9\u8c61\uff0c\u53ef\u80fd\u4f1a\u4e5f\u53ef\u80fd\u4e0d\u4f1a\u6267\u884c\u5176\u4ed6\u7c7b\u578b\u7684\u6e05\u7406\u3002\u5bf9\u4e8e\u4ee5 std::quick_exit() \u7ed3\u675f\u7684\u7a0b\u5e8f\uff0c std::at_quick_exit() \u6267\u884c\u4e0e std::atexit() \u76f8\u540c\u7684\u4f5c\u7528\u3002 std::abort \u548c std::terminate C++\u5305\u542b\u53e6\u5916\u4e24\u4e2a\u4e0e\u505c\u6b62\u76f8\u5173\u7684\u51fd\u6570\u3002 std::abort() \u51fd\u6570\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u975e\u6b63\u5e38\u7ec8\u6b62\u3002\u5f02\u5e38\u7ec8\u6b62\u610f\u5473\u7740\u7a0b\u5e8f\u6709\u67d0\u79cd\u4e0d\u5bfb\u5e38\u7684\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u7a0b\u5e8f\u4e0d\u80fd\u7ee7\u7eed\u8fd0\u884c\u3002\u4f8b\u5982\uff0c\u8bd5\u56fe\u9664\u4ee50\u5c06\u5bfc\u81f4\u5f02\u5e38\u7ec8\u6b62\u3002 std::abort() \u4e0d\u505a\u4efb\u4f55\u6e05\u7406\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include // for std::abort() #include int main () { std :: cout << 1 << '\\n' ; std :: abort (); // The following statements never execute std :: cout << 2 << '\\n' ; return 0 ; } \u6211\u4eec\u5c06\u5728\u672c\u7ae0\u540e\u9762\u770b\u5230\u9690\u5f0f\u8c03\u7528 std::abort() \u7684\u60c5\u51b5( 7.17 -\u65ad\u8a00\u548cstatic_assert )\u3002 std::terminate() \u51fd\u6570\u901a\u5e38\u4e0e\u5f02\u5e38\u4e00\u8d77\u4f7f\u7528(\u6211\u4eec\u5c06\u5728\u540e\u9762\u7684\u7ae0\u8282\u4e2d\u8ba8\u8bba\u5f02\u5e38)\u3002\u867d\u7136\u53ef\u4ee5\u663e\u5f0f\u5730\u8c03\u7528 std::terminate \uff0c\u4f46\u5728\u6ca1\u6709\u5904\u7406\u5f02\u5e38\u65f6(\u4ee5\u53ca\u5728\u5176\u4ed6\u4e00\u4e9b\u4e0e\u5f02\u5e38\u76f8\u5173\u7684\u60c5\u51b5\u4e0b)\uff0c\u66f4\u591a\u7684\u60c5\u51b5\u662f\u9690\u5f0f\u5730\u8c03\u7528\u5b83\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c std::terminate() \u4f1a\u8c03\u7528 std:: abort() \u3002 \u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u6302\u8d77\u7a0b\u5e8f\uff1f \u7b80\u77ed\u7684\u56de\u7b54\u662f\u201c\u51e0\u4e4e\u6ca1\u6709\u201d\u3002\u9500\u6bc1\u5c40\u90e8\u5bf9\u8c61\u662fC++\u7684\u4e00\u4e2a\u91cd\u8981\u90e8\u5206(\u7279\u522b\u662f\u5f53\u6211\u4eec\u8fdb\u5165\u7c7b\u65f6)\uff0c\u4e0a\u9762\u63d0\u5230\u7684\u51fd\u6570\u90fd\u6ca1\u6709\u6e05\u9664\u5c40\u90e8\u53d8\u91cf\u3002\u5f02\u5e38\u662f\u5904\u7406\u9519\u8bef\u60c5\u51b5\u7684\u66f4\u597d\u3001\u66f4\u5b89\u5168\u7684\u673a\u5236\u3002 \u6700\u4f73\u5b9e\u8df5 \u53ea\u6709\u5728\u6ca1\u6709\u4ece\u4e3b\u51fd\u6570\u6b63\u5e38\u8fd4\u56de\u7684\u5b89\u5168\u65b9\u6cd5\u65f6\u624d\u4f7f\u7528\u6302\u8d77\u3002\u5982\u679c\u6ca1\u6709\u7981\u7528\u5f02\u5e38\uff0c\u6700\u597d\u4f7f\u7528\u5f02\u5e38\u6765\u5b89\u5168\u5730\u5904\u7406\u9519\u8bef\u3002","title":"7.11 - \u6302\u8d77\uff08\u63d0\u524d\u7ed3\u675f\u7a0b\u5e8f\uff09"},{"location":"7-11-Halts-exiting-your-program-early/#stdexit","text":"std::exit() \u662f\u4e00\u4e2a\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u6b63\u5e38\u7ec8\u6b62\u7684\u51fd\u6570\u3002\u6b63\u5e38\u7ec8\u6b62\u7684\u610f\u601d\u662f\uff0c\u7a0b\u5e8f\u4ee5\u6211\u4eec\u671f\u671b\u7684\u65b9\u5f0f\u9000\u51fa\u3002\u6ce8\u610f\u672f\u8bed \u6b63\u5e38\u7ec8\u6b62 \u5e76\u4e0d\u9690\u542b\u7a0b\u5e8f\u662f\u5426\u6267\u884c\u6210\u529f\u7684\u542b\u4e49\uff08\u8fd9\u662f\u8fd4\u56de\u72b6\u6001\u7801\u7684\u4f5c\u7528\uff09\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a\u7a0b\u5e8f\u9700\u8981\u7528\u6237\u8f93\u5165\u6587\u4ef6\u540d\u3002\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u662f\u975e\u6cd5\u7684\u6587\u4ef6\u540d\uff0c\u5219\u7a0b\u5e8f\u901a\u5e38\u5e94\u8be5\u8fd4\u56de\u975e\u96f6\u7684\u72b6\u6001\u7801\u6765\u8868\u793a\u5931\u8d25\uff0c\u4f46\u662f\u8be5\u7a0b\u5e8f\u4ecd\u7136\u53ef\u4ee5 \u6b63\u5e38\u7ec8\u6b62 \u3002 std::exit() \u6267\u884c\u8bb8\u591a\u6e05\u7406\u51fd\u6570\u3002\u9996\u5148\uff0c\u9500\u6bc1\u5177\u6709\u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4\u7684\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u5982\u679c\u4f7f\u7528\u4e86\u4efb\u4f55\u6587\u4ef6\uff0c\u5219\u6267\u884c\u5176\u4ed6\u4e00\u4e9b\u6742\u9879\u6587\u4ef6\u6e05\u7406\u3002\u6700\u540e\uff0c\u63a7\u5236\u88ab\u8fd4\u56de\u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff0c\u4f20\u9012\u7ed9 std::exit() \u7684\u53c2\u6570\u88ab\u7528\u4f5c \u72b6\u6001\u7801 \u3002","title":"std::exit() \u51fd\u6570"},{"location":"7-11-Halts-exiting-your-program-early/#stdexit_1","text":"\u867d\u7136 std::exit() \u5728 main() \u7ed3\u675f\u65f6\u4f1a\u88ab\u9690\u5f0f\u8c03\u7528\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u88ab\u663e\u5f0f\u8c03\u7528\uff0c\u4ee5\u5728\u7a0b\u5e8f\u6b63\u5e38\u7ec8\u6b62\u524d\u505c\u6b62\u7a0b\u5e8f\u3002\u5f53\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u8c03\u7528 std::exit() \u65f6\uff0c\u4f60\u9700\u8981\u5305\u542b cstdlib \u5934\u6587\u4ef6\u3002 \u4e0b\u9762\u662f\u663e\u5f0f\u8c03\u7528 std::exit() \u7684\u4e00\u4e2a\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include // for std::exit() #include void cleanup () { // code here to do any kind of cleanup required std :: cout << \"cleanup! \\n \" ; } int main () { std :: cout << 1 << '\\n' ; cleanup (); std :: exit ( 0 ); // terminate and return status code 0 to operating system // The following statements never execute std :: cout << 2 << '\\n' ; return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 2 1 cleanup! \u6ce8\u610f\uff0c std::exit() \u4e4b\u540e\u7684\u8bed\u53e5\u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c\uff0c\u56e0\u4e3a\u7a0b\u5e8f\u5df2\u7ecf\u7ec8\u6b62\u4e86\u3002 \u867d\u7136\u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4ece\u51fd\u6570 main() \u8c03\u7528 std::exit() \uff0c\u4f46\u4e5f\u53ef\u4ee5\u4ece\u4efb\u4f55\u51fd\u6570\u8c03\u7528 std::exit() \u6765\u7ec8\u6b62\u7a0b\u5e8f\u3002 \u5173\u4e8e\u663e\u5f0f\u8c03\u7528 std::exit() \u7684\u4e00\u4e2a\u91cd\u8981\u6ce8\u610f\u4e8b\u9879\u662f\uff1a std::exit() \u4e0d\u4f1a\u6e05\u9664\u4efb\u4f55\u672c\u5730\u53d8\u91cf(\u65e0\u8bba\u662f\u5728\u5f53\u524d\u51fd\u6570\u4e2d\uff0c\u8fd8\u662f\u5728\u8c03\u7528\u6808\u4e2d\u7684\u51fd\u6570)\u3002\u56e0\u6b64\uff0c\u901a\u5e38\u6700\u597d\u907f\u514d\u8c03\u7528 std::exit() \u3002 \u6ce8\u610f std::exit() \u51fd\u6570\u4e0d\u4f1a\u6e05\u7406\u5f53\u524d\u51fd\u6570\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf\u6216\u6e05\u7406\u8c03\u7528\u5806\u6808\u3002","title":"\u663e\u5f0f\u8c03\u7528 std::exit()"},{"location":"7-11-Halts-exiting-your-program-early/#stdatexit","text":"\u56e0\u4e3a std::exit() \u4f1a\u7acb\u5373\u7ec8\u6b62\u7a0b\u5e8f\uff0c\u6240\u4ee5\u4f60\u53ef\u80fd\u9700\u8981\u5728\u7ec8\u6b62\u524d\u624b\u52a8\u6267\u884c\u4e00\u4e9b\u6e05\u7406\u64cd\u4f5c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6e05\u7406\u610f\u5473\u7740\u5173\u95ed\u6570\u636e\u5e93\u6216\u7f51\u7edc\u8fde\u63a5\u3001\u91ca\u653e\u6240\u5206\u914d\u7684\u5185\u5b58\u3001\u5c06\u4fe1\u606f\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\u7b49\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8c03\u7528\u51fd\u6570 cleanup() \u6765\u5904\u7406\u6e05\u7406\u4efb\u52a1\u3002\u7136\u800c\uff0c\u8bb0\u4f4f\u5728\u8c03\u7528 exit() \u4e4b\u524d\u624b\u52a8\u8c03\u7528\u4e00\u4e2a\u6e05\u7406\u51fd\u6570\u4f1a\u7ed9\u7a0b\u5e8f\u5458\u589e\u52a0\u8d1f\u62c5\u3002 \u4e3a\u4e86\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u8fd9\u9879\u5de5\u4f5c\uff0cC++\u63d0\u4f9b\u4e86 std::atexit() \u51fd\u6570\uff0c\u5b83\u5141\u8bb8\u6307\u5b9a\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u5728\u7a0b\u5e8f\u7ec8\u6b62\u65f6\u901a\u8fc7 std::exit() \u81ea\u52a8\u8c03\u7528\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 12.1 - \u51fd\u6570\u6307\u9488 \u4e2d\u4ecb\u7ecd\u5982\u4f55\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012 \u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include // for std::exit() #include void cleanup () { // code here to do any kind of cleanup required std :: cout << \"cleanup! \\n \" ; } int main () { // register cleanup() to be called automatically when std::exit() is called std :: atexit ( cleanup ); // note: we use cleanup rather than cleanup() since we're not making a function call to cleanup() right now std :: cout << 1 << '\\n' ; std :: exit ( 0 ); // terminate and return status code 0 to operating system // The following statements never execute std :: cout << 2 << '\\n' ; return 0 ; } \u8be5\u7a0b\u5e8f\u7684\u8f93\u51fa\u4e0e\u524d\u9762\u7684\u793a\u4f8b\u76f8\u540c\uff1a 1 2 1 cleanup! \u4e3a\u4ec0\u4e48\u8981\u8fd9\u4e48\u505a\u5462\uff1f\u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u5728\u4e00\u4e2a\u5730\u65b9\u6307\u5b9a\u4e00\u4e2a\u6e05\u7406\u51fd\u6570(\u53ef\u80fd\u5728 main \u4e2d)\uff0c\u7136\u540e\u4e0d\u5fc5\u62c5\u5fc3\u5728\u8c03\u7528 std::exit() \u4e4b\u524d\u8bb0\u5f97\u663e\u5f0f\u8c03\u7528\u8be5\u51fd\u6570\u3002 \u9996\u5148\uff0c\u7531\u4e8e std::exit() \u5728 main() \u7ec8\u6b62\u65f6\u9690\u5f0f\u8c03\u7528\uff0c\u5982\u679c\u7a0b\u5e8f\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u9000\u51fa\uff0c std::atexit() \u6ce8\u518c\u7684\u4efb\u4f55\u51fd\u6570\u90fd\u4f1a\u88ab\u8c03\u7528\u3002\u7b2c\u4e8c\uff0c\u88ab\u6ce8\u518c\u7684\u51fd\u6570\u5fc5\u987b\u4e0d\u5e26\u53c2\u6570\uff0c\u4e5f\u6ca1\u6709\u8fd4\u56de\u503c\u3002\u6700\u540e\uff0c\u5982\u679c\u4f60\u613f\u610f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 std::atexit() \u6ce8\u518c\u591a\u4e2a\u6e05\u7406\u51fd\u6570\uff0c\u5b83\u4eec\u5c06\u6309\u6ce8\u518c\u7684\u76f8\u53cd\u987a\u5e8f\u8c03\u7528(\u6700\u540e\u6ce8\u518c\u7684\u5c06\u6700\u5148\u8c03\u7528)\u3002 \u6269\u5c55\u9605\u8bfb \u5728\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u4e2d\uff0c\u8c03\u7528 std::exit() \u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83(\u56e0\u4e3a\u8c03\u7528 std::exit() \u7684\u7ebf\u7a0b\u5c06\u6e05\u7406\u4ecd\u7136\u53ef\u80fd\u88ab\u5176\u4ed6\u7ebf\u7a0b\u8bbf\u95ee\u7684\u9759\u6001\u5bf9\u8c61)\u3002\u56e0\u6b64\uff0cC++\u5f15\u5165\u4e86\u53e6\u4e00\u5bf9\u51fd\u6570\uff0c\u5b83\u4eec\u7684\u5de5\u4f5c\u65b9\u5f0f\u7c7b\u4f3c\u4e8e std::exit() \u548c std::atexit() \uff0c\u5b83\u4eec\u88ab\u79f0\u4e3a std::quick_exit() \u548c std::at_quick_exit() \u3002 std::quick_exit() \u4f1a\u6b63\u5e38\u5730\u7ec8\u6b62\u7a0b\u5e8f\uff0c\u4f46\u4e0d\u4f1a\u6e05\u7406\u9759\u6001\u5bf9\u8c61\uff0c\u53ef\u80fd\u4f1a\u4e5f\u53ef\u80fd\u4e0d\u4f1a\u6267\u884c\u5176\u4ed6\u7c7b\u578b\u7684\u6e05\u7406\u3002\u5bf9\u4e8e\u4ee5 std::quick_exit() \u7ed3\u675f\u7684\u7a0b\u5e8f\uff0c std::at_quick_exit() \u6267\u884c\u4e0e std::atexit() \u76f8\u540c\u7684\u4f5c\u7528\u3002","title":"std::atexit"},{"location":"7-11-Halts-exiting-your-program-early/#stdabort-stdterminate","text":"C++\u5305\u542b\u53e6\u5916\u4e24\u4e2a\u4e0e\u505c\u6b62\u76f8\u5173\u7684\u51fd\u6570\u3002 std::abort() \u51fd\u6570\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u975e\u6b63\u5e38\u7ec8\u6b62\u3002\u5f02\u5e38\u7ec8\u6b62\u610f\u5473\u7740\u7a0b\u5e8f\u6709\u67d0\u79cd\u4e0d\u5bfb\u5e38\u7684\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u7a0b\u5e8f\u4e0d\u80fd\u7ee7\u7eed\u8fd0\u884c\u3002\u4f8b\u5982\uff0c\u8bd5\u56fe\u9664\u4ee50\u5c06\u5bfc\u81f4\u5f02\u5e38\u7ec8\u6b62\u3002 std::abort() \u4e0d\u505a\u4efb\u4f55\u6e05\u7406\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include // for std::abort() #include int main () { std :: cout << 1 << '\\n' ; std :: abort (); // The following statements never execute std :: cout << 2 << '\\n' ; return 0 ; } \u6211\u4eec\u5c06\u5728\u672c\u7ae0\u540e\u9762\u770b\u5230\u9690\u5f0f\u8c03\u7528 std::abort() \u7684\u60c5\u51b5( 7.17 -\u65ad\u8a00\u548cstatic_assert )\u3002 std::terminate() \u51fd\u6570\u901a\u5e38\u4e0e\u5f02\u5e38\u4e00\u8d77\u4f7f\u7528(\u6211\u4eec\u5c06\u5728\u540e\u9762\u7684\u7ae0\u8282\u4e2d\u8ba8\u8bba\u5f02\u5e38)\u3002\u867d\u7136\u53ef\u4ee5\u663e\u5f0f\u5730\u8c03\u7528 std::terminate \uff0c\u4f46\u5728\u6ca1\u6709\u5904\u7406\u5f02\u5e38\u65f6(\u4ee5\u53ca\u5728\u5176\u4ed6\u4e00\u4e9b\u4e0e\u5f02\u5e38\u76f8\u5173\u7684\u60c5\u51b5\u4e0b)\uff0c\u66f4\u591a\u7684\u60c5\u51b5\u662f\u9690\u5f0f\u5730\u8c03\u7528\u5b83\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c std::terminate() \u4f1a\u8c03\u7528 std:: abort() \u3002","title":"std::abort \u548c std::terminate"},{"location":"7-11-Halts-exiting-your-program-early/#_1","text":"\u7b80\u77ed\u7684\u56de\u7b54\u662f\u201c\u51e0\u4e4e\u6ca1\u6709\u201d\u3002\u9500\u6bc1\u5c40\u90e8\u5bf9\u8c61\u662fC++\u7684\u4e00\u4e2a\u91cd\u8981\u90e8\u5206(\u7279\u522b\u662f\u5f53\u6211\u4eec\u8fdb\u5165\u7c7b\u65f6)\uff0c\u4e0a\u9762\u63d0\u5230\u7684\u51fd\u6570\u90fd\u6ca1\u6709\u6e05\u9664\u5c40\u90e8\u53d8\u91cf\u3002\u5f02\u5e38\u662f\u5904\u7406\u9519\u8bef\u60c5\u51b5\u7684\u66f4\u597d\u3001\u66f4\u5b89\u5168\u7684\u673a\u5236\u3002 \u6700\u4f73\u5b9e\u8df5 \u53ea\u6709\u5728\u6ca1\u6709\u4ece\u4e3b\u51fd\u6570\u6b63\u5e38\u8fd4\u56de\u7684\u5b89\u5168\u65b9\u6cd5\u65f6\u624d\u4f7f\u7528\u6302\u8d77\u3002\u5982\u679c\u6ca1\u6709\u7981\u7528\u5f02\u5e38\uff0c\u6700\u597d\u4f7f\u7528\u5f02\u5e38\u6765\u5b89\u5168\u5730\u5904\u7406\u9519\u8bef\u3002","title":"\u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u6302\u8d77\u7a0b\u5e8f\uff1f"},{"location":"7-12-Introduction-to-testing-your-code/","tags":["testing"],"text":"Key Takeaway \u5f53\u7a0b\u5e8f\u53ef\u4ee5\u7f16\u8bd1\uff0c\u751a\u81f3\u53ef\u4ee5\u8fd0\u884c\u540e\uff0c\u8fd8\u6709\u4ec0\u4e48\u5de5\u4f5c\u9700\u8981\u505a\u5462\uff1f \u8fd9\u8981\u770b\u60c5\u51b5\u3002\u5982\u679c\u8be5\u7a0b\u5e8f\u53ea\u8fd0\u884c\u4e00\u6b21\u5e76\u88ab\u4e22\u5f03\uff0c\u90a3\u4e48\u4e0d\u9700\u8981\u505a\u5176\u4ed6\u4e8b\u60c5\u4e86\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f60\u7684\u7a0b\u5e8f\u662f\u5426\u80fd\u9002\u7528\u4e8e\u6240\u6709\u60c5\u51b5\u53ef\u80fd\u5e76\u4e0d\u91cd\u8981\u2014\u2014\u5982\u679c\u5b83\u80fd\u6ee1\u8db3\u4f60\u5f53\u524d\u4efb\u52a1\u7684\u9700\u8981\uff0c\u5e76\u4e14\u4f60\u4e5f\u53ea\u9700\u8981\u8fd0\u884c\u5b83\u4e00\u6b21\uff0c\u90a3\u4e48\u4efb\u52a1\u5c31\u5df2\u7ecf\u5b8c\u6210\u4e86\u3002 \u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u5b8c\u5168\u662f\u7ebf\u6027\u7684(\u6ca1\u6709\u6761\u4ef6\uff0c\u5982\u201cIf\u8bed\u53e5\u201d\u6216\u201cswitch\u8bed\u53e5\u201d)\uff0c\u4e0d\u63a5\u53d7\u8f93\u5165\uff0c\u5e76\u4ea7\u751f\u6b63\u786e\u7684\u7b54\u6848\uff0c\u90a3\u4e48\u4f60\u5c31\u5b8c\u6210\u4e86\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u4f60\u5df2\u7ecf\u901a\u8fc7\u8fd0\u884c\u548c\u9a8c\u8bc1\u8f93\u51fa\u6d4b\u8bd5\u4e86\u6574\u4e2a\u7a0b\u5e8f\u3002 \u4f46\u66f4\u6709\u53ef\u80fd\u7684\u60c5\u51b5\u662f\uff0c\u4f60\u7684\u7a0b\u5e8f\u9700\u8981\u88ab\u591a\u6b21\u8fd0\u884c\uff0c\u800c\u4e14\u8be5\u7a0b\u5e8f\u4f7f\u7528\u5faa\u73af\u548c\u6761\u4ef6\u903b\u8f91\uff0c\u80fd\u591f\u63a5\u53d7\u67d0\u79cd\u7c7b\u578b\u7684\u7528\u6237\u8f93\u5165\u3002\u4f60\u53ef\u80fd\u5df2\u7ecf\u7f16\u5199\u4e86\u53ef\u4ee5\u5728\u5c06\u6765\u7684\u5176\u4ed6\u7a0b\u5e8f\u4e2d\u91cd\u7528\u7684\u51fd\u6570\uff0c\u4e5f\u53ef\u80fd\u7ecf\u6dfb\u52a0\u4e86\u4e00\u4e9b\u6700\u521d\u6ca1\u6709\u8ba1\u5212\u7684\u65b0\u529f\u80fd\u3002\u4e5f\u8bb8\u8fd8\u6253\u7b97\u5c06\u8fd9\u4e2a\u7a0b\u5e8f\u5206\u53d1\u7ed9\u5176\u4ed6\u4eba(\u7528\u6237\u53ef\u80fd\u4f1a\u505a\u51fa\u4f60\u610f\u6599\u4e4b\u5916\u7684\u64cd\u4f5c)\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5fc5\u987b\u9a8c\u8bc1\u7a0b\u5e8f\u5728\u5404\u79cd\u5404\u6837\u7684\u6761\u4ef6\u4e0b\u90fd\u80fd\u6b63\u786e\u5de5\u4f5c\u2014\u2014\u8fd9\u9700\u8981\u6d4b\u8bd5\u6765\u4fdd\u8bc1\u3002 \u4ec5\u4ec5\u56e0\u4e3a\u4f60\u7684\u7a0b\u5e8f\u9002\u7528\u4e8e\u4e00\u7ec4\u8f93\u5165\u5e76\u4e0d\u610f\u5473\u7740\u5b83\u5728\u6240\u6709\u60c5\u51b5\u4e0b\u90fd\u80fd\u6b63\u786e\u5de5\u4f5c\u3002 \u8f6f\u4ef6\u9a8c\u8bc1(\u4e5f\u79f0\u4e3a\u8f6f\u4ef6\u6d4b\u8bd5)\u662f\u786e\u5b9a\u8f6f\u4ef6\u5728\u6240\u6709\u60c5\u51b5\u4e0b\u662f\u5426\u6309\u9884\u671f\u5de5\u4f5c\u7684\u8fc7\u7a0b\u3002 \u6709\u5173\u6d4b\u8bd5\u7684\u96be\u9898 \u5728\u8ba8\u8bba\u6d4b\u8bd5\u4ee3\u7801\u7684\u5b9e\u7528\u65b9\u6cd5\u4e4b\u524d\uff0c\u8ba9\u6211\u4eec\u5148\u8c08\u8c08\u4e3a\u4ec0\u4e48\u5168\u9762\u6d4b\u8bd5\u7a0b\u5e8f\u5e76\u4e0d\u5bb9\u6613\u3002 \u8003\u8651\u8fd9\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include void compare ( int x , int y ) { if ( x > y ) std :: cout << x << \" is greater than \" << y << '\\n' ; // case 1 else if ( x < y ) std :: cout << x << \" is less than \" << y << '\\n' ; // case 2 else std :: cout << x << \" is equal to \" << y << '\\n' ; // case 3 } int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another number: \" ; int y {}; std :: cin >> y ; compare ( x , y ); return 0 ; } \u5047\u8bbe\u6709\u4e00\u4e2a4\u5b57\u8282\u7684\u6574\u6570\uff0c\u7528\u6bcf\u79cd\u53ef\u80fd\u7684\u8f93\u5165\u7ec4\u5408\u663e\u5f0f\u5730\u6d4b\u8bd5\u8fd9\u4e2a\u7a0b\u5e8f\u5c06\u9700\u8981\u8fd0\u884c\u7a0b\u5e8f18,446,744,073,709,551,616\u6b21(~18\u4ebf\u4ebf\u6b21)\u3002\u663e\u7136\u8fd9\u662f\u4e0d\u53ef\u884c\u7684\u3002 \u5f53\u7a0b\u5e8f\u8981\u6c42\u7528\u6237\u8f93\u5165\u6216\u8005\u5728\u4ee3\u7801\u4e2d\u5b58\u5728\u6761\u4ef6\u5224\u65ad\u65f6\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u6267\u884c\u7684\u65b9\u5f0f\u7684\u5c31\u76f8\u5e94\u5730\u53d8\u591a\u4e86\u3002\u9664\u4e86\u6700\u7b80\u5355\u7684\u7a0b\u5e8f\u4e4b\u5916\uff0c\u51e0\u4e4e\u4e0d\u53ef\u80fd\u663e\u5f0f\u5730\u6d4b\u8bd5\u6bcf\u79cd\u8f93\u5165\u7ec4\u5408\u3002 \u73b0\u5728\uff0c\u76f4\u89c9\u544a\u8bc9\u6211\u4eec\uff0c\u5176\u5b9e\u5e76\u4e0d\u9700\u8981\u771f\u7684\u8fd0\u884c\u7a0b\u5e8f18\u4ebf\u4ebf\u6b21\u624d\u80fd\u786e\u4fdd\u5b83\u53ef\u4ee5\u6b63\u786e\u5de5\u4f5c\u3002\u4f60\u53ef\u4ee5\u5408\u7406\u5730\u5f97\u51fa\u7ed3\u8bba\uff0c\u5982\u679c\u60c5\u51b51\u9002\u7528\u4e8e\u4e00\u5bf9 x \u548c y \u503c\uff0c\u5176\u4e2d x > y \uff0c\u90a3\u4e48\u5b83\u5e94\u8be5\u9002\u7528\u4e8e\u4efb\u4f55\u4e00\u5bf9\u6ee1\u8db3 x>y \u7684 x \u548c y \u7ec4\u5408\u3002\u9274\u4e8e\u6b64\uff0c\u7a0b\u5e8f\u5b9e\u9645\u4e0a\u53ea\u9700\u8981\u8fd0\u884c\u8fd9\u4e2a\u5927\u7ea6\u4e09\u6b21(\u5728\u51fd\u6570 compare() \u4e2d\u5206\u522b\u8fd0\u884c\u4e00\u6b21)\uff0c\u5c31\u53ef\u4ee5\u9ad8\u5ea6\u786e\u4fe1\u5b83\u80fd\u6309\u9884\u671f\u5de5\u4f5c\u3002\u4e3a\u4e86\u4f7f\u6d4b\u8bd5\u6613\u4e8e\u7ba1\u7406\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u7c7b\u4f3c\u7684\u6280\u5de7\u6765\u5927\u5e45\u51cf\u5c11\u6d4b\u8bd5\u7684\u6b21\u6570\u3002 \u5173\u4e8e\u6d4b\u8bd5\u65b9\u6cd5\u6709\u5f88\u591a\u4e1c\u897f\u53ef\u4ee5\u5199\u2014\u2014\u4e8b\u5b9e\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u6b64\u5199\u4e00\u6574\u7ae0\u3002\u4f46\u7531\u4e8e\u8fd9\u4e0d\u662fC++\u6559\u7a0b\u5e94\u8be5\u5173\u6ce8\u7684\u4e8b\u60c5\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u4f1a\u7b80\u5355\u4ecb\u7ecd\u4f5c\u4e3a\u7a0b\u5e8f\u5458\u5e94\u8be5\u5982\u4f55\u6d4b\u8bd5\u81ea\u5df1\u7684\u4ee3\u7801\u3002\u5728\u63a5\u4e0b\u6765\u7684\u51e0\u4e2a\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u4e00\u4e9b\u5728\u6d4b\u8bd5\u4ee3\u7801\u65f6\u5e94\u8be5\u8003\u8651\u7684\u5b9e\u9645\u95ee\u9898\u3002 \u5316\u6574\u4e3a\u96f6\u8fdb\u884c\u6d4b\u8bd5 \u5047\u8bbe\u4f60\u662f\u4e00\u5bb6\u6c7d\u8f66\u5236\u9020\u5546\uff0c\u6b63\u5728\u5236\u9020\u4e00\u8f86\u5ba2\u6237\u6982\u5ff5\u8f66\u3002\u90a3\u4e48\u4f60\u4f1a\u600e\u4e48\u505a\u5462\uff1f a) \u5728\u7ec4\u88c5\u524d\u5236\u9020 (\u6216\u8d2d\u4e70) \u5e76\u6d4b\u8bd5\u5404\u4e2a\u96f6\u90e8\u4ef6\uff0c\u5982\u679c\u96f6\u90e8\u4ef6\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\uff0c\u5219\u5c06\u5176\u7ec4\u88c5\u5230\u6c7d\u8f66\u4e0a\uff0c\u786e\u4fdd\u4ecd\u7136\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u3002\u6700\u540e\uff0c\u5bf9\u6574\u8f66\u8fdb\u884c\u6d4b\u8bd5\uff0c\u786e\u4fdd\u6240\u6709\u90e8\u5206\u90fd\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u3002 b) \u7528\u6240\u6709\u7684\u90e8\u4ef6\u4e00\u6c14\u5475\u6210\u5730\u9020\u51fa\u4e00\u8f86\u8f66\uff0c\u7136\u540e\u5728\u6700\u540e\u5bf9\u6574\u4e2a\u90e8\u4ef6\u8fdb\u884c\u7b2c\u4e00\u6b21\u6d4b\u8bd5\u3002 \u5f88\u660e\u663e\u9009\u9879a)\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u7136\u800c\uff0c\u8bb8\u591a\u65b0\u7a0b\u5e8f\u5458\u4f1a\u6309\u7167 b) \u63cf\u8ff0\u7684\u65b9\u6cd5\u7f16\u5199\u4ee3\u7801\uff01 \u5728b)\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u6c7d\u8f66\u4e2d\u6709\u4efb\u4f55\u90e8\u4ef6\u6ca1\u6709\u6309\u7167\u9884\u671f\u5de5\u4f5c\uff0c\u5de5\u7a0b\u5e08\u5c31\u5fc5\u987b\u5bf9\u6574\u8f86\u8f66\u8fdb\u884c\u8bca\u65ad\uff0c\u4ee5\u786e\u5b9a\u54ea\u91cc\u51fa\u4e86\u95ee\u9898\u2014\u2014\u95ee\u9898\u53ef\u80fd\u5728\u4efb\u4f55\u5730\u65b9\u3002\u4e00\u4e2a\u75c7\u72b6\u53ef\u80fd\u6709\u5f88\u591a\u539f\u56e0\u2014\u2014\u4f8b\u5982\uff0c\u7531\u4e8e\u706b\u82b1\u585e\u3001\u7535\u6c60\u3001\u71c3\u6cb9\u6cf5\u6216\u5176\u4ed6\u539f\u56e0\uff0c\u6c7d\u8f66\u65e0\u6cd5\u542f\u52a8\uff1f\u8fd9\u65e0\u7591\u4f1a\u6d6a\u8d39\u5927\u91cf\u65f6\u95f4\u3002\u5982\u679c\u53d1\u73b0\u4e86\u95ee\u9898\uff0c\u540e\u679c\u53ef\u80fd\u662f\u707e\u96be\u6027\u7684\u2014\u2014\u4e00\u5904\u4fee\u6539\u53ef\u80fd\u4f1a\u5728\u5176\u4ed6\u591a\u4e2a\u5730\u65b9\u5f15\u8d77\u201c\u6d9f\u6f2a\u6548\u5e94\u201d(\u53d8\u5316)\u3002\u4f8b\u5982\uff0c\u71c3\u6cb9\u6cf5\u592a\u5c0f\u53ef\u80fd\u4f1a\u5bfc\u81f4\u53d1\u52a8\u673a\u91cd\u65b0\u8bbe\u8ba1\uff0c\u4ece\u800c\u5bfc\u81f4\u6c7d\u8f66\u6846\u67b6\u7684\u91cd\u65b0\u8bbe\u8ba1\u3002\u5728\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u4f1a\u91cd\u65b0\u8bbe\u8ba1\u6c7d\u8f66\u7684\u4e00\u4e2a\u5f88\u5927\u7684\u90e8\u5206\uff0c\u53ea\u662f\u4e3a\u4e86\u9002\u5e94\u6700\u521d\u7684\u4e00\u4e2a\u5c0f\u95ee\u9898!\u5f88\u660e\u663e\u9009\u9879a)\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u7136\u800c\uff0c\u8bb8\u591a\u65b0\u7a0b\u5e8f\u5458\u7f16\u5199\u7c7b\u4f3c\u9009\u9879b)\u7684\u4ee3\u7801! \u5bf9\u4e8e a) \u7684\u60c5\u51b5\uff0c\u6c7d\u8f66\u516c\u53f8\u4e00\u8fb9\u7ec4\u88c5\u4e00\u8fb9\u6d4b\u8bd5\u3002\u5982\u679c\u4efb\u4f55\u96f6\u4ef6\u662f\u574f\u7684\uff0c\u4ed6\u4eec\u4f1a\u7acb\u5373\u77e5\u9053\uff0c\u5e76\u80fd\u4fee\u590d/\u66f4\u6362\u5b83\u3002\u4efb\u4f55\u4e1c\u897f\u90fd\u4e0d\u4f1a\u88ab\u96c6\u6210\u5230\u6c7d\u8f66\u4e2d\uff0c\u76f4\u5230\u5b83\u88ab\u8bc1\u660e\u53ef\u4ee5\u5de5\u4f5c\uff0c\u7136\u540e\u4e00\u65e6\u5b83\u88ab\u96c6\u6210\u5230\u6c7d\u8f66\u4e2d\uff0c\u5c31\u4f1a\u518d\u6b21\u8fdb\u884c\u6d4b\u8bd5\u3002\u8fd9\u6837\uff0c\u4efb\u4f55\u610f\u5916\u7684\u95ee\u9898\u90fd\u53ef\u4ee5\u5c3d\u65e9\u53d1\u73b0\uff0c\u800c\u5b83\u4eec\u4ecd\u7136\u662f\u53ef\u4ee5\u8f7b\u677e\u89e3\u51b3\u7684\u5c0f\u95ee\u9898\u3002 \u5f00\u59cb\u7ec4\u88c5\u6574\u8f86\u8f66\u7684\u65f6\u5019\uff0c\u5236\u9020\u5546\u5e94\u8be5\u5bf9\u6c7d\u8f66\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u6709\u5408\u7406\u7684\u4fe1\u5fc3\u2014\u2014\u6bd5\u7adf\uff0c\u6240\u6709\u7684\u90e8\u4ef6\u90fd\u5df2\u7ecf\u5355\u72ec\u6d4b\u8bd5\u8fc7\u4e86\uff0c\u5e76\u4e14\u5728\u521d\u59cb\u96c6\u6210\u65f6\u4e5f\u8fdb\u884c\u4e86\u6d4b\u8bd5\u3002\u5728\u8fd9\u4e00\u70b9\u4e0a\u4ecd\u7136\u6709\u53ef\u80fd\u53d1\u73b0\u610f\u5916\u7684\u95ee\u9898\uff0c\u4f46\u662f\u901a\u8fc7\u6240\u6709\u5148\u524d\u7684\u6d4b\u8bd5\u5c06\u98ce\u9669\u964d\u5230\u6700\u4f4e\u3002 \u4e0a\u9762\u7684\u7c7b\u6bd4\u4e5f\u9002\u7528\u4e8e\u7a0b\u5e8f\uff0c\u5c3d\u7ba1\u7531\u4e8e\u67d0\u79cd\u539f\u56e0\uff0c\u65b0\u7a0b\u5e8f\u5458\u7ecf\u5e38\u6ca1\u6709\u610f\u8bc6\u5230\u8fd9\u4e00\u70b9\u3002\u6700\u597d\u662f\u7f16\u5199\u5c0f\u51fd\u6570(\u6216\u7c7b)\uff0c\u7136\u540e\u7acb\u5373\u7f16\u8bd1\u548c\u6d4b\u8bd5\u5b83\u4eec\u3002\u8fd9\u6837\uff0c\u5982\u679c\u4f60\u72af\u4e86\u9519\u8bef\u5c31\u4f1a\u77e5\u9053\u5b83\u4e00\u5b9a\u662f\u5728\u4e0a\u6b21\u7f16\u8bd1/\u6d4b\u8bd5\u540e\u66f4\u6539\u7684\u5c11\u91cf\u4ee3\u7801\u4e2d\u3002\u8fd9\u610f\u5473\u7740\u8981\u67e5\u770b\u7684\u5730\u65b9\u66f4\u5c11\uff0c\u82b1\u5728\u8c03\u8bd5\u4e0a\u7684\u65f6\u95f4\u4e5f\u66f4\u5c11\u3002 \u5355\u72ec\u6d4b\u8bd5\u4e00\u5c0f\u90e8\u5206\u4ee3\u7801\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u201c\u5355\u5143\u201d\u662f\u6b63\u786e\u7684\uff0c\u8fd9\u79f0\u4e3a\u5355\u5143\u6d4b\u8bd5\u3002\u6bcf\u4e2a\u5355\u5143\u6d4b\u8bd5\u90fd\u88ab\u8bbe\u8ba1\u7528\u6765\u786e\u4fdd\u5355\u5143\u7684\u7279\u5b9a\u884c\u4e3a\u662f\u6b63\u786e\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u7528\u5b9a\u4e49\u826f\u597d\u7684\u5c0f\u5355\u5143(\u51fd\u6570\u6216\u7c7b)\u7f16\u5199\u7a0b\u5e8f\uff0c\u9891\u7e41\u7f16\u8bd1\uff0c\u5e76\u5728\u8fd0\u884c\u65f6\u6d4b\u8bd5\u4ee3\u7801\u3002 \u5982\u679c\u7a0b\u5e8f\u5f88\u77ed\u5e76\u4e14\u63a5\u53d7\u7528\u6237\u8f93\u5165\uff0c\u90a3\u4e48\u5c1d\u8bd5\u5404\u79cd\u7528\u6237\u8f93\u5165\u53ef\u80fd\u5c31\u8db3\u591f\u4e86\u3002\u4f46\u662f\u968f\u7740\u7a0b\u5e8f\u53d8\u5f97\u8d8a\u6765\u8d8a\u957f\uff0c\u8fd9\u5c31\u4e0d\u591f\u4e86\uff0c\u5728\u5c06\u5355\u4e2a\u51fd\u6570\u6216\u7c7b\u96c6\u6210\u5230\u7a0b\u5e8f\u7684\u5176\u4f59\u90e8\u5206\u4e4b\u524d\u5c31\u5bf9\u5b83\u4eec\u8fdb\u884c\u6d4b\u8bd5\u6548\u679c\u4f1a\u66f4\u597d\u3002 \u90a3\u4e48\u6211\u4eec\u5982\u4f55\u4ee5\u5355\u4f4d\u6765\u6d4b\u8bd5\u4ee3\u7801\u5462? \u975e\u6b63\u5f0f\u6d4b\u8bd5 \u6d4b\u8bd5\u4ee3\u7801\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u5728\u7f16\u5199\u7a0b\u5e8f\u65f6\u8fdb\u884c\u975e\u6b63\u5f0f\u6d4b\u8bd5\u3002\u5728\u7f16\u5199\u4e86\u4e00\u4e2a\u4ee3\u7801\u5355\u5143(\u4e00\u4e2a\u51fd\u6570\u3001\u4e00\u4e2a\u7c7b\u6216\u5176\u4ed6\u4e00\u4e9b\u79bb\u6563\u7684\u201c\u5305\u201d\u4ee3\u7801)\u4e4b\u540e\uff0c\u53ef\u4ee5\u7f16\u5199\u4e00\u4e9b\u4ee3\u7801\u6765\u6d4b\u8bd5\u521a\u521a\u6dfb\u52a0\u7684\u5355\u5143\uff0c\u7136\u540e\u5728\u6d4b\u8bd5\u901a\u8fc7\u540e\u5220\u9664\u6d4b\u8bd5\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e0b\u9762\u7684isLowerVowel()\u51fd\u6570\uff0c\u5219\u53ef\u4ee5\u7f16\u5199\u4ee5\u4e0b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include // We want to test the following function bool isLowerVowel ( char c ) { switch ( c ) { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : return true ; default : return false ; } } int main () { // So here's our temporary tests to validate it works std :: cout << isLowerVowel ( 'a' ); // temporary test code, should produce 1 std :: cout << isLowerVowel ( 'q' ); // temporary test code, should produce 0 return 0 ; } \u5982\u679c\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u201c 1 \u201d\u548c\u201c 0 \u201d\uff0c\u90a3\u4e48\u7a0b\u5e8f\u5c31\u662f\u6b63\u786e\u7684\u3002\u4f60\u7684\u51fd\u6570\u9002\u7528\u4e8e\u4e00\u4e9b\u57fa\u672c\u60c5\u51b5\uff0c\u968f\u540e\u4f60\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u4ee3\u7801\u5408\u7406\u5730\u63a8\u65ad\u5b83\u4e5f\u9002\u7528\u4e8e\u90a3\u4e9b\u5c1a\u672a\u6d4b\u8bd5\u7684\u60c5\u51b5( e \uff0c i \uff0c o \u548c u )\u3002\u56e0\u6b64\uff0c\u4f60\u53ef\u4ee5\u5220\u9664\u4e34\u65f6\u6d4b\u8bd5\u4ee3\u7801\uff0c\u5e76\u7ee7\u7eed\u7f16\u7a0b\u3002 \u4fdd\u5b58\u6d4b\u8bd5 \u5c3d\u7ba1\u7f16\u5199\u4e34\u65f6\u7684\u6d4b\u8bd5\u4ee3\u7801\u53ef\u4ee5\u7b80\u5355\u5feb\u901f\u5730\u6d4b\u8bd5\u7a0b\u5e8f\uff0c\u4f46\u5982\u679c\u7a0d\u540e\u8fd8\u9700\u8981\u6d4b\u8bd5\u7a0b\u5e8f\u7684\u8bdd\uff0c\u5c31\u4e0d\u80fd\u4f9d\u8d56\u8fd9\u4e9b\u4e34\u65f6\u7684\u6d4b\u8bd5\u4ee3\u7801\u4e86\u3002\u5f53\u6211\u4eec\u4fee\u6539\u4e86\u4e00\u4e2a\u51fd\u6570\u5e76\u6dfb\u52a0\u65b0\u7684\u529f\u80fd\u540e\uff0c\u5f80\u5f80\u5e0c\u671b\u786e\u4fdd\u6ca1\u6709\u7834\u574f\u7a0b\u5e8f\u7684\u5176\u4ed6\u90e8\u5206\u3002\u51fa\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u5c06\u6d4b\u8bd5\u4ee3\u7801\u4fdd\u5b58\u8d77\u6765\u4ee5\u4fbf\u5c06\u6765\u53ef\u4ee5\u518d\u6b21\u8fd0\u884c\u662f\u66f4\u52a0\u6709\u610f\u4e49\u7684\u505a\u6cd5\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5c06\u6d4b\u8bd5\u79fb\u52a8\u5230 testVowel() \u51fd\u6570\u4e2d\uff0c\u800c\u4e0d\u662f\u5220\u9664\u4f60\u7684\u4e34\u65f6\u6d4b\u8bd5\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include bool isLowerVowel ( char c ) { switch ( c ) { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : return true ; default : return false ; } } // Not called from anywhere right now // But here if you want to retest things later void testVowel () { std :: cout << isLowerVowel ( 'a' ); // temporary test code, should produce 1 std :: cout << isLowerVowel ( 'q' ); // temporary test code, should produce 0 } int main () { return 0 ; } \u5728\u521b\u5efa\u66f4\u591a\u6d4b\u8bd5\u65f6\uff0c\u53ef\u4ee5\u5c06\u5b83\u4eec\u6dfb\u52a0\u5230 testVowel() \u51fd\u6570\u4e2d\u3002 \u81ea\u52a8\u5316\u6d4b\u8bd5 \u4e0a\u8ff0\u6d4b\u8bd5\u51fd\u6570\u7684\u4e00\u4e2a\u95ee\u9898\u662f\uff0c\u5b83\u4f9d\u8d56\u4e8e\u4f60\u5728\u8fd0\u884c\u65f6\u624b\u52a8\u9a8c\u8bc1\u7ed3\u679c\u3002\u8fd9\u8981\u6c42\u4f60\u8981\u8bb0\u4f4f\u671f\u671b\u7684\u7ed3\u679c\u662f\u4ec0\u4e48(\u5047\u8bbe\u60a8\u6ca1\u6709\u8bb0\u5f55\u5b83)\uff0c\u5e76\u624b\u52a8\u5c06\u5b9e\u9645\u7ed3\u679c\u4e0e\u9884\u671f\u7ed3\u679c\u8fdb\u884c\u6bd4\u8f83\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u4e00\u4e2a\u5305\u542b\u6d4b\u8bd5\u548c\u9884\u671f\u7b54\u6848\u7684\u6d4b\u8bd5\u51fd\u6570\u6765\u505a\u5f97\u66f4\u597d\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u4e0d\u5fc5\u5bf9\u5b83\u4eec\u8fdb\u884c\u4eba\u5de5\u6bd4\u8f83\u4e86\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include bool isLowerVowel ( char c ) { switch ( c ) { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : return true ; default : return false ; } } // returns the number of the test that failed, or 0 if all tests passed int testVowel () { if ( isLowerVowel ( 'a' ) != true ) return 1 ; if ( isLowerVowel ( 'q' ) != false ) return 2 ; return 0 ; } int main () { return 0 ; } \u8fd9\u6837\u4e00\u6765\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728\u4efb\u4f55\u65f6\u5019\u8c03\u7528 testVowel() \u51fd\u6570\u6765\u6d4b\u8bd5\u7a0b\u5e8f\uff0c\u786e\u4fdd\u5b83\u59cb\u7ec8\u80fd\u591f\u6b63\u786e\u8fd0\u884c\u3002\u6d4b\u8bd5\u4ee3\u7801\u53ef\u4ee5\u81ea\u52a8\u5e2e\u4f60\u6d4b\u8bd5\u6240\u6709\u7528\u4f8b\uff0c\u8fd4\u56de\u201call good\u201d\uff08\u8fd4\u56de\u503c0\uff09\uff0c\u6216\u8005\u8fd4\u56de\u5931\u8d25\u7528\u4f8b\u7684\u5e8f\u53f7\u3002\u8fd9\u6837\u7684\u6d4b\u8bd5\u5728\u6211\u4eec\u4fee\u6539\u8001\u4ee3\u7801\u65f6\u5c24\u4e3a\u91cd\u8981\uff0c\u56e0\u4e3a\u8c01\u4e5f\u4e0d\u5e0c\u671b\u65e0\u610f\u95f4\u7834\u574f\u7a0b\u5e8f\u539f\u672c\u6b63\u5e38\u7684\u529f\u80fd\uff01 \u5355\u5143\u6d4b\u8bd5\u6846\u67b6 \u56e0\u4e3a\u7f16\u5199\u51fd\u6570\u6765\u6267\u884c\u5176\u4ed6\u51fd\u6570\u662f\u975e\u5e38\u5e38\u89c1\u548c\u6709\u7528\u7684\u9700\u6c42\uff0c\u6240\u4ee5\u6709\u4e00\u4e9b\u5b8c\u6574\u7684\u6846\u67b6(\u79f0\u4e3a\u5355\u5143\u6d4b\u8bd5\u6846\u67b6)\u65e8\u5728\u5e2e\u52a9\u7b80\u5316\u7f16\u5199\u3001\u7ef4\u62a4\u548c\u6267\u884c\u5355\u5143\u6d4b\u8bd5\u7684\u8fc7\u7a0b\u3002\u56e0\u4e3a\u8fd9\u4e9b\u6d89\u53ca\u5230\u7b2c\u4e09\u65b9\u8f6f\u4ef6\uff0c\u6240\u4ee5\u6211\u4eec\u5728\u8fd9\u91cc\u4e0d\u505a\u4ecb\u7ecd\uff0c\u4f46\u662f\u4f60\u5e94\u8be5\u77e5\u9053\u5b83\u4eec\u7684\u5b58\u5728\u3002 \u96c6\u6210\u6d4b\u8bd5 \u4e00\u65e6\u7a0b\u5e8f\u7684\u6bcf\u4e2a\u5355\u5143\u90fd\u5355\u72ec\u6d4b\u8bd5\u8fc7\uff0c\u5b83\u4eec\u5c31\u53ef\u4ee5\u88ab\u96c6\u6210\u5230\u4f60\u7684\u7a0b\u5e8f\u4e2d\uff0c\u5e76\u91cd\u65b0\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u88ab\u6b63\u786e\u5730\u96c6\u6210\u4e86\u3002\u8fd9\u5c31\u662f\u6240\u8c13\u7684\u96c6\u6210\u6d4b\u8bd5\u3002\u96c6\u6210\u6d4b\u8bd5\u5f80\u5f80\u66f4\u52a0\u590d\u6742\u2014\u2014\u76ee\u524d\uff0c\u8fd0\u884c\u51e0\u6b21\u7a0b\u5e8f\u5e76\u62bd\u68c0\u96c6\u6210\u5355\u5143\u7684\u884c\u4e3a\u5c31\u8db3\u591f\u4e86\u3002","title":"7.12 - \u4ee3\u7801\u6d4b\u8bd5"},{"location":"7-12-Introduction-to-testing-your-code/#_1","text":"\u5728\u8ba8\u8bba\u6d4b\u8bd5\u4ee3\u7801\u7684\u5b9e\u7528\u65b9\u6cd5\u4e4b\u524d\uff0c\u8ba9\u6211\u4eec\u5148\u8c08\u8c08\u4e3a\u4ec0\u4e48\u5168\u9762\u6d4b\u8bd5\u7a0b\u5e8f\u5e76\u4e0d\u5bb9\u6613\u3002 \u8003\u8651\u8fd9\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include void compare ( int x , int y ) { if ( x > y ) std :: cout << x << \" is greater than \" << y << '\\n' ; // case 1 else if ( x < y ) std :: cout << x << \" is less than \" << y << '\\n' ; // case 2 else std :: cout << x << \" is equal to \" << y << '\\n' ; // case 3 } int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another number: \" ; int y {}; std :: cin >> y ; compare ( x , y ); return 0 ; } \u5047\u8bbe\u6709\u4e00\u4e2a4\u5b57\u8282\u7684\u6574\u6570\uff0c\u7528\u6bcf\u79cd\u53ef\u80fd\u7684\u8f93\u5165\u7ec4\u5408\u663e\u5f0f\u5730\u6d4b\u8bd5\u8fd9\u4e2a\u7a0b\u5e8f\u5c06\u9700\u8981\u8fd0\u884c\u7a0b\u5e8f18,446,744,073,709,551,616\u6b21(~18\u4ebf\u4ebf\u6b21)\u3002\u663e\u7136\u8fd9\u662f\u4e0d\u53ef\u884c\u7684\u3002 \u5f53\u7a0b\u5e8f\u8981\u6c42\u7528\u6237\u8f93\u5165\u6216\u8005\u5728\u4ee3\u7801\u4e2d\u5b58\u5728\u6761\u4ef6\u5224\u65ad\u65f6\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u6267\u884c\u7684\u65b9\u5f0f\u7684\u5c31\u76f8\u5e94\u5730\u53d8\u591a\u4e86\u3002\u9664\u4e86\u6700\u7b80\u5355\u7684\u7a0b\u5e8f\u4e4b\u5916\uff0c\u51e0\u4e4e\u4e0d\u53ef\u80fd\u663e\u5f0f\u5730\u6d4b\u8bd5\u6bcf\u79cd\u8f93\u5165\u7ec4\u5408\u3002 \u73b0\u5728\uff0c\u76f4\u89c9\u544a\u8bc9\u6211\u4eec\uff0c\u5176\u5b9e\u5e76\u4e0d\u9700\u8981\u771f\u7684\u8fd0\u884c\u7a0b\u5e8f18\u4ebf\u4ebf\u6b21\u624d\u80fd\u786e\u4fdd\u5b83\u53ef\u4ee5\u6b63\u786e\u5de5\u4f5c\u3002\u4f60\u53ef\u4ee5\u5408\u7406\u5730\u5f97\u51fa\u7ed3\u8bba\uff0c\u5982\u679c\u60c5\u51b51\u9002\u7528\u4e8e\u4e00\u5bf9 x \u548c y \u503c\uff0c\u5176\u4e2d x > y \uff0c\u90a3\u4e48\u5b83\u5e94\u8be5\u9002\u7528\u4e8e\u4efb\u4f55\u4e00\u5bf9\u6ee1\u8db3 x>y \u7684 x \u548c y \u7ec4\u5408\u3002\u9274\u4e8e\u6b64\uff0c\u7a0b\u5e8f\u5b9e\u9645\u4e0a\u53ea\u9700\u8981\u8fd0\u884c\u8fd9\u4e2a\u5927\u7ea6\u4e09\u6b21(\u5728\u51fd\u6570 compare() \u4e2d\u5206\u522b\u8fd0\u884c\u4e00\u6b21)\uff0c\u5c31\u53ef\u4ee5\u9ad8\u5ea6\u786e\u4fe1\u5b83\u80fd\u6309\u9884\u671f\u5de5\u4f5c\u3002\u4e3a\u4e86\u4f7f\u6d4b\u8bd5\u6613\u4e8e\u7ba1\u7406\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u7c7b\u4f3c\u7684\u6280\u5de7\u6765\u5927\u5e45\u51cf\u5c11\u6d4b\u8bd5\u7684\u6b21\u6570\u3002 \u5173\u4e8e\u6d4b\u8bd5\u65b9\u6cd5\u6709\u5f88\u591a\u4e1c\u897f\u53ef\u4ee5\u5199\u2014\u2014\u4e8b\u5b9e\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u6b64\u5199\u4e00\u6574\u7ae0\u3002\u4f46\u7531\u4e8e\u8fd9\u4e0d\u662fC++\u6559\u7a0b\u5e94\u8be5\u5173\u6ce8\u7684\u4e8b\u60c5\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u4f1a\u7b80\u5355\u4ecb\u7ecd\u4f5c\u4e3a\u7a0b\u5e8f\u5458\u5e94\u8be5\u5982\u4f55\u6d4b\u8bd5\u81ea\u5df1\u7684\u4ee3\u7801\u3002\u5728\u63a5\u4e0b\u6765\u7684\u51e0\u4e2a\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u4e00\u4e9b\u5728\u6d4b\u8bd5\u4ee3\u7801\u65f6\u5e94\u8be5\u8003\u8651\u7684\u5b9e\u9645\u95ee\u9898\u3002","title":"\u6709\u5173\u6d4b\u8bd5\u7684\u96be\u9898"},{"location":"7-12-Introduction-to-testing-your-code/#_2","text":"\u5047\u8bbe\u4f60\u662f\u4e00\u5bb6\u6c7d\u8f66\u5236\u9020\u5546\uff0c\u6b63\u5728\u5236\u9020\u4e00\u8f86\u5ba2\u6237\u6982\u5ff5\u8f66\u3002\u90a3\u4e48\u4f60\u4f1a\u600e\u4e48\u505a\u5462\uff1f a) \u5728\u7ec4\u88c5\u524d\u5236\u9020 (\u6216\u8d2d\u4e70) \u5e76\u6d4b\u8bd5\u5404\u4e2a\u96f6\u90e8\u4ef6\uff0c\u5982\u679c\u96f6\u90e8\u4ef6\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\uff0c\u5219\u5c06\u5176\u7ec4\u88c5\u5230\u6c7d\u8f66\u4e0a\uff0c\u786e\u4fdd\u4ecd\u7136\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u3002\u6700\u540e\uff0c\u5bf9\u6574\u8f66\u8fdb\u884c\u6d4b\u8bd5\uff0c\u786e\u4fdd\u6240\u6709\u90e8\u5206\u90fd\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u3002 b) \u7528\u6240\u6709\u7684\u90e8\u4ef6\u4e00\u6c14\u5475\u6210\u5730\u9020\u51fa\u4e00\u8f86\u8f66\uff0c\u7136\u540e\u5728\u6700\u540e\u5bf9\u6574\u4e2a\u90e8\u4ef6\u8fdb\u884c\u7b2c\u4e00\u6b21\u6d4b\u8bd5\u3002 \u5f88\u660e\u663e\u9009\u9879a)\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u7136\u800c\uff0c\u8bb8\u591a\u65b0\u7a0b\u5e8f\u5458\u4f1a\u6309\u7167 b) \u63cf\u8ff0\u7684\u65b9\u6cd5\u7f16\u5199\u4ee3\u7801\uff01 \u5728b)\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u6c7d\u8f66\u4e2d\u6709\u4efb\u4f55\u90e8\u4ef6\u6ca1\u6709\u6309\u7167\u9884\u671f\u5de5\u4f5c\uff0c\u5de5\u7a0b\u5e08\u5c31\u5fc5\u987b\u5bf9\u6574\u8f86\u8f66\u8fdb\u884c\u8bca\u65ad\uff0c\u4ee5\u786e\u5b9a\u54ea\u91cc\u51fa\u4e86\u95ee\u9898\u2014\u2014\u95ee\u9898\u53ef\u80fd\u5728\u4efb\u4f55\u5730\u65b9\u3002\u4e00\u4e2a\u75c7\u72b6\u53ef\u80fd\u6709\u5f88\u591a\u539f\u56e0\u2014\u2014\u4f8b\u5982\uff0c\u7531\u4e8e\u706b\u82b1\u585e\u3001\u7535\u6c60\u3001\u71c3\u6cb9\u6cf5\u6216\u5176\u4ed6\u539f\u56e0\uff0c\u6c7d\u8f66\u65e0\u6cd5\u542f\u52a8\uff1f\u8fd9\u65e0\u7591\u4f1a\u6d6a\u8d39\u5927\u91cf\u65f6\u95f4\u3002\u5982\u679c\u53d1\u73b0\u4e86\u95ee\u9898\uff0c\u540e\u679c\u53ef\u80fd\u662f\u707e\u96be\u6027\u7684\u2014\u2014\u4e00\u5904\u4fee\u6539\u53ef\u80fd\u4f1a\u5728\u5176\u4ed6\u591a\u4e2a\u5730\u65b9\u5f15\u8d77\u201c\u6d9f\u6f2a\u6548\u5e94\u201d(\u53d8\u5316)\u3002\u4f8b\u5982\uff0c\u71c3\u6cb9\u6cf5\u592a\u5c0f\u53ef\u80fd\u4f1a\u5bfc\u81f4\u53d1\u52a8\u673a\u91cd\u65b0\u8bbe\u8ba1\uff0c\u4ece\u800c\u5bfc\u81f4\u6c7d\u8f66\u6846\u67b6\u7684\u91cd\u65b0\u8bbe\u8ba1\u3002\u5728\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u4f1a\u91cd\u65b0\u8bbe\u8ba1\u6c7d\u8f66\u7684\u4e00\u4e2a\u5f88\u5927\u7684\u90e8\u5206\uff0c\u53ea\u662f\u4e3a\u4e86\u9002\u5e94\u6700\u521d\u7684\u4e00\u4e2a\u5c0f\u95ee\u9898!\u5f88\u660e\u663e\u9009\u9879a)\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u7136\u800c\uff0c\u8bb8\u591a\u65b0\u7a0b\u5e8f\u5458\u7f16\u5199\u7c7b\u4f3c\u9009\u9879b)\u7684\u4ee3\u7801! \u5bf9\u4e8e a) \u7684\u60c5\u51b5\uff0c\u6c7d\u8f66\u516c\u53f8\u4e00\u8fb9\u7ec4\u88c5\u4e00\u8fb9\u6d4b\u8bd5\u3002\u5982\u679c\u4efb\u4f55\u96f6\u4ef6\u662f\u574f\u7684\uff0c\u4ed6\u4eec\u4f1a\u7acb\u5373\u77e5\u9053\uff0c\u5e76\u80fd\u4fee\u590d/\u66f4\u6362\u5b83\u3002\u4efb\u4f55\u4e1c\u897f\u90fd\u4e0d\u4f1a\u88ab\u96c6\u6210\u5230\u6c7d\u8f66\u4e2d\uff0c\u76f4\u5230\u5b83\u88ab\u8bc1\u660e\u53ef\u4ee5\u5de5\u4f5c\uff0c\u7136\u540e\u4e00\u65e6\u5b83\u88ab\u96c6\u6210\u5230\u6c7d\u8f66\u4e2d\uff0c\u5c31\u4f1a\u518d\u6b21\u8fdb\u884c\u6d4b\u8bd5\u3002\u8fd9\u6837\uff0c\u4efb\u4f55\u610f\u5916\u7684\u95ee\u9898\u90fd\u53ef\u4ee5\u5c3d\u65e9\u53d1\u73b0\uff0c\u800c\u5b83\u4eec\u4ecd\u7136\u662f\u53ef\u4ee5\u8f7b\u677e\u89e3\u51b3\u7684\u5c0f\u95ee\u9898\u3002 \u5f00\u59cb\u7ec4\u88c5\u6574\u8f86\u8f66\u7684\u65f6\u5019\uff0c\u5236\u9020\u5546\u5e94\u8be5\u5bf9\u6c7d\u8f66\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u6709\u5408\u7406\u7684\u4fe1\u5fc3\u2014\u2014\u6bd5\u7adf\uff0c\u6240\u6709\u7684\u90e8\u4ef6\u90fd\u5df2\u7ecf\u5355\u72ec\u6d4b\u8bd5\u8fc7\u4e86\uff0c\u5e76\u4e14\u5728\u521d\u59cb\u96c6\u6210\u65f6\u4e5f\u8fdb\u884c\u4e86\u6d4b\u8bd5\u3002\u5728\u8fd9\u4e00\u70b9\u4e0a\u4ecd\u7136\u6709\u53ef\u80fd\u53d1\u73b0\u610f\u5916\u7684\u95ee\u9898\uff0c\u4f46\u662f\u901a\u8fc7\u6240\u6709\u5148\u524d\u7684\u6d4b\u8bd5\u5c06\u98ce\u9669\u964d\u5230\u6700\u4f4e\u3002 \u4e0a\u9762\u7684\u7c7b\u6bd4\u4e5f\u9002\u7528\u4e8e\u7a0b\u5e8f\uff0c\u5c3d\u7ba1\u7531\u4e8e\u67d0\u79cd\u539f\u56e0\uff0c\u65b0\u7a0b\u5e8f\u5458\u7ecf\u5e38\u6ca1\u6709\u610f\u8bc6\u5230\u8fd9\u4e00\u70b9\u3002\u6700\u597d\u662f\u7f16\u5199\u5c0f\u51fd\u6570(\u6216\u7c7b)\uff0c\u7136\u540e\u7acb\u5373\u7f16\u8bd1\u548c\u6d4b\u8bd5\u5b83\u4eec\u3002\u8fd9\u6837\uff0c\u5982\u679c\u4f60\u72af\u4e86\u9519\u8bef\u5c31\u4f1a\u77e5\u9053\u5b83\u4e00\u5b9a\u662f\u5728\u4e0a\u6b21\u7f16\u8bd1/\u6d4b\u8bd5\u540e\u66f4\u6539\u7684\u5c11\u91cf\u4ee3\u7801\u4e2d\u3002\u8fd9\u610f\u5473\u7740\u8981\u67e5\u770b\u7684\u5730\u65b9\u66f4\u5c11\uff0c\u82b1\u5728\u8c03\u8bd5\u4e0a\u7684\u65f6\u95f4\u4e5f\u66f4\u5c11\u3002 \u5355\u72ec\u6d4b\u8bd5\u4e00\u5c0f\u90e8\u5206\u4ee3\u7801\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u201c\u5355\u5143\u201d\u662f\u6b63\u786e\u7684\uff0c\u8fd9\u79f0\u4e3a\u5355\u5143\u6d4b\u8bd5\u3002\u6bcf\u4e2a\u5355\u5143\u6d4b\u8bd5\u90fd\u88ab\u8bbe\u8ba1\u7528\u6765\u786e\u4fdd\u5355\u5143\u7684\u7279\u5b9a\u884c\u4e3a\u662f\u6b63\u786e\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u7528\u5b9a\u4e49\u826f\u597d\u7684\u5c0f\u5355\u5143(\u51fd\u6570\u6216\u7c7b)\u7f16\u5199\u7a0b\u5e8f\uff0c\u9891\u7e41\u7f16\u8bd1\uff0c\u5e76\u5728\u8fd0\u884c\u65f6\u6d4b\u8bd5\u4ee3\u7801\u3002 \u5982\u679c\u7a0b\u5e8f\u5f88\u77ed\u5e76\u4e14\u63a5\u53d7\u7528\u6237\u8f93\u5165\uff0c\u90a3\u4e48\u5c1d\u8bd5\u5404\u79cd\u7528\u6237\u8f93\u5165\u53ef\u80fd\u5c31\u8db3\u591f\u4e86\u3002\u4f46\u662f\u968f\u7740\u7a0b\u5e8f\u53d8\u5f97\u8d8a\u6765\u8d8a\u957f\uff0c\u8fd9\u5c31\u4e0d\u591f\u4e86\uff0c\u5728\u5c06\u5355\u4e2a\u51fd\u6570\u6216\u7c7b\u96c6\u6210\u5230\u7a0b\u5e8f\u7684\u5176\u4f59\u90e8\u5206\u4e4b\u524d\u5c31\u5bf9\u5b83\u4eec\u8fdb\u884c\u6d4b\u8bd5\u6548\u679c\u4f1a\u66f4\u597d\u3002 \u90a3\u4e48\u6211\u4eec\u5982\u4f55\u4ee5\u5355\u4f4d\u6765\u6d4b\u8bd5\u4ee3\u7801\u5462?","title":"\u5316\u6574\u4e3a\u96f6\u8fdb\u884c\u6d4b\u8bd5"},{"location":"7-12-Introduction-to-testing-your-code/#_3","text":"\u6d4b\u8bd5\u4ee3\u7801\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u5728\u7f16\u5199\u7a0b\u5e8f\u65f6\u8fdb\u884c\u975e\u6b63\u5f0f\u6d4b\u8bd5\u3002\u5728\u7f16\u5199\u4e86\u4e00\u4e2a\u4ee3\u7801\u5355\u5143(\u4e00\u4e2a\u51fd\u6570\u3001\u4e00\u4e2a\u7c7b\u6216\u5176\u4ed6\u4e00\u4e9b\u79bb\u6563\u7684\u201c\u5305\u201d\u4ee3\u7801)\u4e4b\u540e\uff0c\u53ef\u4ee5\u7f16\u5199\u4e00\u4e9b\u4ee3\u7801\u6765\u6d4b\u8bd5\u521a\u521a\u6dfb\u52a0\u7684\u5355\u5143\uff0c\u7136\u540e\u5728\u6d4b\u8bd5\u901a\u8fc7\u540e\u5220\u9664\u6d4b\u8bd5\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e0b\u9762\u7684isLowerVowel()\u51fd\u6570\uff0c\u5219\u53ef\u4ee5\u7f16\u5199\u4ee5\u4e0b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include // We want to test the following function bool isLowerVowel ( char c ) { switch ( c ) { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : return true ; default : return false ; } } int main () { // So here's our temporary tests to validate it works std :: cout << isLowerVowel ( 'a' ); // temporary test code, should produce 1 std :: cout << isLowerVowel ( 'q' ); // temporary test code, should produce 0 return 0 ; } \u5982\u679c\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u201c 1 \u201d\u548c\u201c 0 \u201d\uff0c\u90a3\u4e48\u7a0b\u5e8f\u5c31\u662f\u6b63\u786e\u7684\u3002\u4f60\u7684\u51fd\u6570\u9002\u7528\u4e8e\u4e00\u4e9b\u57fa\u672c\u60c5\u51b5\uff0c\u968f\u540e\u4f60\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u4ee3\u7801\u5408\u7406\u5730\u63a8\u65ad\u5b83\u4e5f\u9002\u7528\u4e8e\u90a3\u4e9b\u5c1a\u672a\u6d4b\u8bd5\u7684\u60c5\u51b5( e \uff0c i \uff0c o \u548c u )\u3002\u56e0\u6b64\uff0c\u4f60\u53ef\u4ee5\u5220\u9664\u4e34\u65f6\u6d4b\u8bd5\u4ee3\u7801\uff0c\u5e76\u7ee7\u7eed\u7f16\u7a0b\u3002","title":"\u975e\u6b63\u5f0f\u6d4b\u8bd5"},{"location":"7-12-Introduction-to-testing-your-code/#_4","text":"\u5c3d\u7ba1\u7f16\u5199\u4e34\u65f6\u7684\u6d4b\u8bd5\u4ee3\u7801\u53ef\u4ee5\u7b80\u5355\u5feb\u901f\u5730\u6d4b\u8bd5\u7a0b\u5e8f\uff0c\u4f46\u5982\u679c\u7a0d\u540e\u8fd8\u9700\u8981\u6d4b\u8bd5\u7a0b\u5e8f\u7684\u8bdd\uff0c\u5c31\u4e0d\u80fd\u4f9d\u8d56\u8fd9\u4e9b\u4e34\u65f6\u7684\u6d4b\u8bd5\u4ee3\u7801\u4e86\u3002\u5f53\u6211\u4eec\u4fee\u6539\u4e86\u4e00\u4e2a\u51fd\u6570\u5e76\u6dfb\u52a0\u65b0\u7684\u529f\u80fd\u540e\uff0c\u5f80\u5f80\u5e0c\u671b\u786e\u4fdd\u6ca1\u6709\u7834\u574f\u7a0b\u5e8f\u7684\u5176\u4ed6\u90e8\u5206\u3002\u51fa\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u5c06\u6d4b\u8bd5\u4ee3\u7801\u4fdd\u5b58\u8d77\u6765\u4ee5\u4fbf\u5c06\u6765\u53ef\u4ee5\u518d\u6b21\u8fd0\u884c\u662f\u66f4\u52a0\u6709\u610f\u4e49\u7684\u505a\u6cd5\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5c06\u6d4b\u8bd5\u79fb\u52a8\u5230 testVowel() \u51fd\u6570\u4e2d\uff0c\u800c\u4e0d\u662f\u5220\u9664\u4f60\u7684\u4e34\u65f6\u6d4b\u8bd5\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include bool isLowerVowel ( char c ) { switch ( c ) { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : return true ; default : return false ; } } // Not called from anywhere right now // But here if you want to retest things later void testVowel () { std :: cout << isLowerVowel ( 'a' ); // temporary test code, should produce 1 std :: cout << isLowerVowel ( 'q' ); // temporary test code, should produce 0 } int main () { return 0 ; } \u5728\u521b\u5efa\u66f4\u591a\u6d4b\u8bd5\u65f6\uff0c\u53ef\u4ee5\u5c06\u5b83\u4eec\u6dfb\u52a0\u5230 testVowel() \u51fd\u6570\u4e2d\u3002","title":"\u4fdd\u5b58\u6d4b\u8bd5"},{"location":"7-12-Introduction-to-testing-your-code/#_5","text":"\u4e0a\u8ff0\u6d4b\u8bd5\u51fd\u6570\u7684\u4e00\u4e2a\u95ee\u9898\u662f\uff0c\u5b83\u4f9d\u8d56\u4e8e\u4f60\u5728\u8fd0\u884c\u65f6\u624b\u52a8\u9a8c\u8bc1\u7ed3\u679c\u3002\u8fd9\u8981\u6c42\u4f60\u8981\u8bb0\u4f4f\u671f\u671b\u7684\u7ed3\u679c\u662f\u4ec0\u4e48(\u5047\u8bbe\u60a8\u6ca1\u6709\u8bb0\u5f55\u5b83)\uff0c\u5e76\u624b\u52a8\u5c06\u5b9e\u9645\u7ed3\u679c\u4e0e\u9884\u671f\u7ed3\u679c\u8fdb\u884c\u6bd4\u8f83\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u4e00\u4e2a\u5305\u542b\u6d4b\u8bd5\u548c\u9884\u671f\u7b54\u6848\u7684\u6d4b\u8bd5\u51fd\u6570\u6765\u505a\u5f97\u66f4\u597d\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u4e0d\u5fc5\u5bf9\u5b83\u4eec\u8fdb\u884c\u4eba\u5de5\u6bd4\u8f83\u4e86\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include bool isLowerVowel ( char c ) { switch ( c ) { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : return true ; default : return false ; } } // returns the number of the test that failed, or 0 if all tests passed int testVowel () { if ( isLowerVowel ( 'a' ) != true ) return 1 ; if ( isLowerVowel ( 'q' ) != false ) return 2 ; return 0 ; } int main () { return 0 ; } \u8fd9\u6837\u4e00\u6765\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728\u4efb\u4f55\u65f6\u5019\u8c03\u7528 testVowel() \u51fd\u6570\u6765\u6d4b\u8bd5\u7a0b\u5e8f\uff0c\u786e\u4fdd\u5b83\u59cb\u7ec8\u80fd\u591f\u6b63\u786e\u8fd0\u884c\u3002\u6d4b\u8bd5\u4ee3\u7801\u53ef\u4ee5\u81ea\u52a8\u5e2e\u4f60\u6d4b\u8bd5\u6240\u6709\u7528\u4f8b\uff0c\u8fd4\u56de\u201call good\u201d\uff08\u8fd4\u56de\u503c0\uff09\uff0c\u6216\u8005\u8fd4\u56de\u5931\u8d25\u7528\u4f8b\u7684\u5e8f\u53f7\u3002\u8fd9\u6837\u7684\u6d4b\u8bd5\u5728\u6211\u4eec\u4fee\u6539\u8001\u4ee3\u7801\u65f6\u5c24\u4e3a\u91cd\u8981\uff0c\u56e0\u4e3a\u8c01\u4e5f\u4e0d\u5e0c\u671b\u65e0\u610f\u95f4\u7834\u574f\u7a0b\u5e8f\u539f\u672c\u6b63\u5e38\u7684\u529f\u80fd\uff01","title":"\u81ea\u52a8\u5316\u6d4b\u8bd5"},{"location":"7-12-Introduction-to-testing-your-code/#_6","text":"\u56e0\u4e3a\u7f16\u5199\u51fd\u6570\u6765\u6267\u884c\u5176\u4ed6\u51fd\u6570\u662f\u975e\u5e38\u5e38\u89c1\u548c\u6709\u7528\u7684\u9700\u6c42\uff0c\u6240\u4ee5\u6709\u4e00\u4e9b\u5b8c\u6574\u7684\u6846\u67b6(\u79f0\u4e3a\u5355\u5143\u6d4b\u8bd5\u6846\u67b6)\u65e8\u5728\u5e2e\u52a9\u7b80\u5316\u7f16\u5199\u3001\u7ef4\u62a4\u548c\u6267\u884c\u5355\u5143\u6d4b\u8bd5\u7684\u8fc7\u7a0b\u3002\u56e0\u4e3a\u8fd9\u4e9b\u6d89\u53ca\u5230\u7b2c\u4e09\u65b9\u8f6f\u4ef6\uff0c\u6240\u4ee5\u6211\u4eec\u5728\u8fd9\u91cc\u4e0d\u505a\u4ecb\u7ecd\uff0c\u4f46\u662f\u4f60\u5e94\u8be5\u77e5\u9053\u5b83\u4eec\u7684\u5b58\u5728\u3002","title":"\u5355\u5143\u6d4b\u8bd5\u6846\u67b6"},{"location":"7-12-Introduction-to-testing-your-code/#_7","text":"\u4e00\u65e6\u7a0b\u5e8f\u7684\u6bcf\u4e2a\u5355\u5143\u90fd\u5355\u72ec\u6d4b\u8bd5\u8fc7\uff0c\u5b83\u4eec\u5c31\u53ef\u4ee5\u88ab\u96c6\u6210\u5230\u4f60\u7684\u7a0b\u5e8f\u4e2d\uff0c\u5e76\u91cd\u65b0\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u88ab\u6b63\u786e\u5730\u96c6\u6210\u4e86\u3002\u8fd9\u5c31\u662f\u6240\u8c13\u7684\u96c6\u6210\u6d4b\u8bd5\u3002\u96c6\u6210\u6d4b\u8bd5\u5f80\u5f80\u66f4\u52a0\u590d\u6742\u2014\u2014\u76ee\u524d\uff0c\u8fd0\u884c\u51e0\u6b21\u7a0b\u5e8f\u5e76\u62bd\u68c0\u96c6\u6210\u5355\u5143\u7684\u884c\u4e3a\u5c31\u8db3\u591f\u4e86\u3002","title":"\u96c6\u6210\u6d4b\u8bd5"},{"location":"7-13-Code-coverage/","tags":["code coverage"],"text":"Key Takeaway \u5728 7.12 - \u4ee3\u7801\u6d4b\u8bd5 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u7f16\u5199\u548c\u4fdd\u5b58\u6d4b\u8bd5\u3002\u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u54ea\u4e9b\u6d4b\u8bd5\u5bf9\u4e8e\u4fdd\u8bc1\u7a0b\u5e8f\u8d28\u91cf\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002 \u4ee3\u7801\u8986\u76d6\u7387 \u672f\u8bed\u4ee3\u7801\u8986\u76d6\u7387\u7528\u4e8e\u63cf\u8ff0\u6d4b\u8bd5\u6267\u884c\u65f6\u53ef\u4ee5\u8fd0\u884c\u7684\u7a0b\u5e8f\u6e90\u4ee3\u7801\u6570\u91cf\u3002\u4ee3\u7801\u8986\u76d6\u7387\u6709\u8bb8\u591a\u4e0d\u540c\u7684\u5ea6\u91cf\u6807\u51c6\u3002\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u90e8\u5206\u4e2d\u4ecb\u7ecd\u4e00\u4e9b\u66f4\u6709\u7528\u548c\u66f4\u53d7\u6b22\u8fce\u7684\u65b9\u6cd5\u3002 \u8bed\u53e5\u8986\u76d6\u7387 \u672f\u8bed\u8bed\u53e5\u8986\u76d6\u7387\u6307\u7684\u662f\u6d4b\u8bd5\u4f8b\u7a0b\u6267\u884c\u8fc7\u7684\u4ee3\u7801\u4e2d\u8bed\u53e5\u7684\u767e\u5206\u6bd4\u3002 \u8003\u8651\u4ee5\u4e0b\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 int foo ( int x , int y ) { int z { y }; if ( x > y ) { z = x ; } return z ; } \u8c03\u7528 foo(1, 0) \u53ef\u4ee5\u5b9e\u73b0\u8be5\u51fd\u6570\u8bed\u53e5\u7684\u5b8c\u5168\u8986\u76d6\uff0c\u5373\u51fd\u6570\u4e2d\u7684\u6bcf\u6761\u8bed\u53e5\u90fd\u4f1a\u6267\u884c\u3002 \u5bf9\u4e8e isLowerVowel() \u51fd\u6570\u6765\u8bf4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 bool isLowerVowel ( char c ) { switch ( c ) // \u8bed\u53e5 1 { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : return true ; // \u8bed\u53e5 2 default : return false ; // \u8bed\u53e5 3 } } \u8fd9\u4e2a\u51fd\u6570\u9700\u8981\u4e24\u6b21\u8c03\u7528\u624d\u80fd\u5b9e\u73b0\u8bed\u53e5\u7684\u5b8c\u5168 \u8986\u76d6\uff0c\u56e0\u4e3a\u8bed\u53e52\u548c3\u4e0d\u53ef\u80fd\u5728\u4e00\u6b21\u51fd\u6570\u8c03\u7528\u4e2d\u540c\u65f6\u6267\u884c\u3002 100%\u7684\u8bed\u53e5\u8986\u76d6\u7387\u662f\u5f88\u597d\u7684\u6307\u6807\uff0c\u4f46\u8fd9\u5176\u5b9e\u5e76\u4e0d\u8db3\u4ee5\u786e\u4fdd\u6b63\u786e\u6027\u3002 \u5206\u652f\u8986\u76d6\u7387 \u5206\u652f\u8986\u76d6\u7387\u662f\u6307\u5df2\u7ecf\u6267\u884c\u7684\u5206\u652f\u7684\u767e\u5206\u6bd4\uff0c\u6bcf\u4e2a\u53ef\u80fd\u7684\u5206\u652f\u5206\u522b\u8ba1\u7b97\u3002\u4e00\u4e2a' if\u8bed\u53e5'\u6709\u4e24\u4e2a\u5206\u652f\u2014\u2014\u4e00\u4e2a\u5206\u652f\u5728\u6761\u4ef6\u4e3a'\u771f'\u65f6\u6267\u884c\uff0c\u53e6\u4e00\u4e2a\u5206\u652f\u5728\u6761\u4ef6\u4e3a'\u5047'\u65f6\u6267\u884c(\u5373\u4f7f\u6ca1\u6709\u76f8\u5e94\u7684' else\u8bed\u53e5'\u8981\u6267\u884c)\u3002\u4e00\u4e2aswitch\u8bed\u53e5\u53ef\u4ee5\u6709\u5f88\u591a\u5206\u652f\u3002 1 2 3 4 5 6 7 8 9 int foo ( int x , int y ) { int z { y }; if ( x > y ) { z = x ; } return z ; } \u5728\u4e4b\u524d\u7684\u4f8b\u5b50\u4e2d\uff0c\u8c03\u7528 foo(1, 0) \u53ef\u4ee5\u5b9e\u73b0100%\u7684\u8bed\u53e5\u8986\u76d6\u7387\uff0c\u5e76\u4e14\u6d4b\u8bd5 x > y \u7684\u60c5\u51b5\uff0c\u4f46\u662f\u5206\u652f\u8986\u76d6\u7387\u5374\u53ea\u670950%\u3002\u6211\u4eec\u9700\u8981\u518d\u8c03\u7528\u4e00\u6b21 foo(0, 1) \uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u6d4b\u5230 if \u8bed\u53e5\u4e0d\u6267\u884c\u7684\u60c5\u51b5\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 bool isLowerVowel ( char c ) { switch ( c ) { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : return true ; default : return false ; } } \u5728 isLowerVowel() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4e5f\u9700\u8981\u8c03\u7528\u4e24\u6b21\u624d\u80fd\u5f97\u5230100%\u7684\u5206\u652f\u8986\u76d6\u7387\uff1a\u7b2c\u4e00\u6b21\u8c03\u7528 (\u4f8b\u5982 isLowerVowel('a') ) \u6d4b\u8bd5\u524d\u9762\u7684\u51e0\u4e2acase\u5206\u652f\uff0c\u7b2c\u4e8c\u6b21\u8c03\u7528(\u4f8b\u5982 isLowerVowel('q') ) \u6765\u6d4b\u8bd5 default \u5206\u652f\u3002\u80fd\u591f\u5728\u4e00\u6b21\u6d4b\u8bd5\u4e2d\u6267\u884c\u5230\u7684\u5206\u652f\uff0c\u4e0d\u9700\u8981\u5206\u522b\u6d4b\u8bd5\u3002 \u8003\u8651\u4e0b\u9762\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 void compare ( int x , int y ) { if ( x > y ) std :: cout << x << \" is greater than \" << y << '\\n' ; // case 1 else if ( x < y ) std :: cout << x << \" is less than \" << y << '\\n' ; // case 2 else std :: cout << x << \" is equal to \" << y << '\\n' ; // case 3 } \u8be5\u51fd\u6570\u9700\u8981\u4e09\u6b21\u8c03\u7528\u624d\u80fd\u5b9e\u73b0100%\u5206\u652f\u8986\u76d6\uff1a compare(1, 0) \u6d4b\u8bd5\u7b2c\u4e00\u4e2a if \u8bed\u53e5\u4e3a\u771f\u7684\u5206\u652f\uff0c compare(0, 1) \u6d4b\u8bd5\u7b2c\u4e00\u4e2a if \u8bed\u53e5\u4e3a\u5047\u4e14\u7b2c\u4e8c\u4e2a if \u8bed\u53e5\u4e3a\u771f\u7684\u5206\u652f\u3002 compare(0, 0) \u6d4b\u8bd5\u4e24\u4e2a if \u8bed\u53e5\u90fd\u4e3a\u5047\uff0c\u6267\u884c else \u7684\u5206\u652f\u3002\u6267\u884c\u5b8c\u8fd9\u4e09\u6b21\u6d4b\u8bd5\u540e\uff0c\u6211\u4eec\u5c31\u6709\u4fe1\u5fc3\u8bf4\u8fd9\u4e2a\u51fd\u6570\u88ab\u53ef\u9760\u5730\u6d4b\u8bd5\u4e86\uff08\u4e09\u6b21\u6d4b\u8bd5\u603b\u597d\u8fc71800\u4ebf\u4ebf\u4e2a\u8f93\u5165\u7ec4\u5408\u5427\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 100%\u5206\u652f\u8986\u76d6\u7387\u662f\u6211\u4eec\u7684\u6d4b\u8bd5\u76ee\u6807\uff01 \u5faa\u73af\u8986\u76d6\u7387 \u5faa\u73af\u8986\u76d6\u7387(\u975e\u6b63\u5f0f\u5730\u79f0\u4e3a0,1,2\u6d4b\u8bd5)\u6307\u7684\u662f\uff0c\u5982\u679c\u4f60\u7684\u4ee3\u7801\u4e2d\u6709\u4e00\u4e2a\u5faa\u73af\uff0c\u5219\u5e94\u8be5\u786e\u4fdd\u5b83\u5728\u8fed\u4ee30\u6b21\u30011\u6b21\u548c2\u6b21\u65f6\u6b63\u5e38\u5de5\u4f5c\u3002\u5982\u679c\u5b83\u5bf92\u6b21\u8fed\u4ee3\u7684\u60c5\u51b5\u6b63\u786e\u5de5\u4f5c\uff0c\u90a3\u4e48\u5b83\u5e94\u8be5\u5bf9\u6240\u6709\u5927\u4e8e2\u6b21\u7684\u8fed\u4ee3\u90fd\u6b63\u786e\u5de5\u4f5c\u3002\u56e0\u6b64\uff0c\u8fd9\u4e09\u4e2a\u6d4b\u8bd5\u6db5\u76d6\u4e86\u6240\u6709\u7684\u53ef\u80fd\u6027(\u56e0\u4e3a\u5faa\u73af\u4e0d\u80fd\u6267\u884c\u8d1f\u6570\u6b21)\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 #include void spam ( int timesToPrint ) { for ( int count { 0 }; count < timesToPrint ; ++ count ) std :: cout << \"Spam! \" ; } \u4e3a\u4e86\u66f4\u597d\u5730\u6d4b\u8bd5\u8fd9\u4e2a\u51fd\u6570\u4e2d\u7684\u5faa\u73af\uff0c\u6211\u4eec\u9700\u8981\u8c03\u7528\u4e09\u6b21\u8be5\u51fd\u6570\uff1a spam(0) \u6d4b\u8bd50\u6b21\u8fed\u4ee3\u7684\u60c5\u51b5\uff0c spam(1) \u6d4b\u8bd51\u6b21\u8fed\u4ee3\u7684\u60c5\u51b5\uff0c\u7136\u540e\u8c03\u7528 spam(2) \u6d4b\u8bd5 2 \u6b21\u8fed\u4ee3\u7684\u60c5\u51b5\u3002\u5982\u679c spam(2) \u53ef\u4ee5\u5de5\u4f5c\uff0c\u5219\u5bf9\u4e8e\u4efb\u610f n>2 \u7684\u60c5\u51b5 spam(n) \u90fd\u5e94\u8be5\u53ef\u4ee5\u5de5\u4f5c\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528 \u201c0,1,2\u6d4b\u8bd5\u201d \u6765\u786e\u4fdd\u4f60\u7684\u5faa\u73af\u5728\u4e0d\u540c\u6b21\u6570\u7684\u8fed\u4ee3\u4e0b\u90fd\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002 \u6d4b\u8bd5\u4e0d\u540c\u7c7b\u522b\u7684\u8f93\u5165 \u5728\u7f16\u5199\u63a5\u53d7\u53c2\u6570\u7684\u51fd\u6570\u6216\u63a5\u53d7\u7528\u6237\u8f93\u5165\u65f6\uff0c\u8981\u8003\u8651\u4e0d\u540c\u7c7b\u522b\u7684\u8f93\u5165\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f7f\u7528\u672f\u8bed\u201c\u7c7b\u522b\u201d\u6765\u8868\u793a\u5177\u6709\u76f8\u4f3c\u7279\u5f81\u7684\u4e00\u7ec4\u8f93\u5165\u3002 \u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u4ea7\u751f\u6574\u6570\u5e73\u65b9\u6839\u7684\u51fd\u6570\uff0c\u7528\u4ec0\u4e48\u503c\u6765\u6d4b\u8bd5\u5b83\u662f\u6709\u610f\u4e49\u7684\uff1f\u4f60\u53ef\u80fd\u4f1a\u4ece\u4e00\u4e9b\u6b63\u5e38\u7684\u503c\u5f00\u59cb\uff0c\u6bd4\u5982\u201c4\u201d\u3002\u4f46\u662f\u7528\u201c0\u201d\u548c\u8d1f\u6570\u6765\u6d4b\u8bd5\u4e5f\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\u3002 \u4ee5\u4e0b\u662f\u5206\u7c7b\u6d4b\u8bd5\u7684\u4e00\u4e9b\u57fa\u672c\u51c6\u5219\uff1a \u5bf9\u4e8e\u6574\u6570\uff0c\u9700\u8981\u8003\u8651\u51fd\u6570\u5982\u4f55\u5904\u7406\u8d1f\u503c\u3001\u96f6\u548c\u6b63\u503c\u3002\u5982\u679c\u53ef\u80fd\u7684\u8bdd\uff0c\u8fd8\u5e94\u8be5\u68c0\u67e5\u662f\u5426\u6ea2\u51fa\u3002 \u5bf9\u4e8e\u6d6e\u70b9\u6570\uff0c\u786e\u4fdd\u8003\u8651\u4e86\u51fd\u6570\u5982\u4f55\u5904\u7406\u6709\u7cbe\u5ea6\u95ee\u9898\u7684\u503c(\u7565\u5927\u4e8e\u6216\u7565\u5c0f\u4e8e\u9884\u671f\u503c)\u3002\u9002\u5408\u6d4b\u8bd5\u7684 double \u7c7b\u578b\u503c\u662f 0.1 \u548c -0.1 (\u7528\u4e8e\u6d4b\u8bd5\u7565\u5927\u4e8e\u9884\u671f\u7684\u6570\u5b57)\u4ee5\u53ca 0.6 \u548c -0.6 (\u7528\u4e8e\u6d4b\u8bd5\u7565\u5c0f\u4e8e\u9884\u671f\u7684\u6570\u5b57)\u3002 \u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0c\u786e\u4fdd\u4f60\u8003\u8651\u4e86\u51fd\u6570\u5982\u4f55\u5904\u7406\u7a7a\u5b57\u7b26\u4e32(\u53ea\u662f\u4e00\u4e2a\u7a7a\u7ed3\u675f\u7b26)\u3001\u6b63\u5e38\u6709\u6548\u7684\u5b57\u7b26\u4e32\u3001\u6709\u7a7a\u683c\u7684\u5b57\u7b26\u4e32\u4ee5\u53ca\u5168\u662f\u7a7a\u683c\u7684\u5b57\u7b26\u4e32\u3002 \u5982\u679c\u51fd\u6570\u4f1a\u5904\u7406\u6307\u9488\uff0c\u4e0d\u8981\u5fd8\u8bb0\u6d4b\u8bd5 nullptr (\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u8fd9\u662f\u4ec0\u4e48\u4e5f\u4e0d\u8981\u7d27\uff0c\u6bd5\u7adf\u6211\u4eec\u8fd8\u6ca1\u4ecb\u7ecd\u8fc7\u5b83)\u3002 \u6700\u4f73\u5b9e\u8df5 \u6d4b\u8bd5\u4e0d\u540c\u7c7b\u522b\u7684\u8f93\u5165\u503c\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u5355\u5143\u80fd\u591f\u6b63\u786e\u5730\u5904\u7406\u5b83\u4eec\u3002","title":"7.13 - \u4ee3\u7801\u8986\u76d6\u7387"},{"location":"7-13-Code-coverage/#_1","text":"\u672f\u8bed\u4ee3\u7801\u8986\u76d6\u7387\u7528\u4e8e\u63cf\u8ff0\u6d4b\u8bd5\u6267\u884c\u65f6\u53ef\u4ee5\u8fd0\u884c\u7684\u7a0b\u5e8f\u6e90\u4ee3\u7801\u6570\u91cf\u3002\u4ee3\u7801\u8986\u76d6\u7387\u6709\u8bb8\u591a\u4e0d\u540c\u7684\u5ea6\u91cf\u6807\u51c6\u3002\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u90e8\u5206\u4e2d\u4ecb\u7ecd\u4e00\u4e9b\u66f4\u6709\u7528\u548c\u66f4\u53d7\u6b22\u8fce\u7684\u65b9\u6cd5\u3002","title":"\u4ee3\u7801\u8986\u76d6\u7387"},{"location":"7-13-Code-coverage/#_2","text":"\u672f\u8bed\u8bed\u53e5\u8986\u76d6\u7387\u6307\u7684\u662f\u6d4b\u8bd5\u4f8b\u7a0b\u6267\u884c\u8fc7\u7684\u4ee3\u7801\u4e2d\u8bed\u53e5\u7684\u767e\u5206\u6bd4\u3002 \u8003\u8651\u4ee5\u4e0b\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 int foo ( int x , int y ) { int z { y }; if ( x > y ) { z = x ; } return z ; } \u8c03\u7528 foo(1, 0) \u53ef\u4ee5\u5b9e\u73b0\u8be5\u51fd\u6570\u8bed\u53e5\u7684\u5b8c\u5168\u8986\u76d6\uff0c\u5373\u51fd\u6570\u4e2d\u7684\u6bcf\u6761\u8bed\u53e5\u90fd\u4f1a\u6267\u884c\u3002 \u5bf9\u4e8e isLowerVowel() \u51fd\u6570\u6765\u8bf4\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 bool isLowerVowel ( char c ) { switch ( c ) // \u8bed\u53e5 1 { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : return true ; // \u8bed\u53e5 2 default : return false ; // \u8bed\u53e5 3 } } \u8fd9\u4e2a\u51fd\u6570\u9700\u8981\u4e24\u6b21\u8c03\u7528\u624d\u80fd\u5b9e\u73b0\u8bed\u53e5\u7684\u5b8c\u5168 \u8986\u76d6\uff0c\u56e0\u4e3a\u8bed\u53e52\u548c3\u4e0d\u53ef\u80fd\u5728\u4e00\u6b21\u51fd\u6570\u8c03\u7528\u4e2d\u540c\u65f6\u6267\u884c\u3002 100%\u7684\u8bed\u53e5\u8986\u76d6\u7387\u662f\u5f88\u597d\u7684\u6307\u6807\uff0c\u4f46\u8fd9\u5176\u5b9e\u5e76\u4e0d\u8db3\u4ee5\u786e\u4fdd\u6b63\u786e\u6027\u3002","title":"\u8bed\u53e5\u8986\u76d6\u7387"},{"location":"7-13-Code-coverage/#_3","text":"\u5206\u652f\u8986\u76d6\u7387\u662f\u6307\u5df2\u7ecf\u6267\u884c\u7684\u5206\u652f\u7684\u767e\u5206\u6bd4\uff0c\u6bcf\u4e2a\u53ef\u80fd\u7684\u5206\u652f\u5206\u522b\u8ba1\u7b97\u3002\u4e00\u4e2a' if\u8bed\u53e5'\u6709\u4e24\u4e2a\u5206\u652f\u2014\u2014\u4e00\u4e2a\u5206\u652f\u5728\u6761\u4ef6\u4e3a'\u771f'\u65f6\u6267\u884c\uff0c\u53e6\u4e00\u4e2a\u5206\u652f\u5728\u6761\u4ef6\u4e3a'\u5047'\u65f6\u6267\u884c(\u5373\u4f7f\u6ca1\u6709\u76f8\u5e94\u7684' else\u8bed\u53e5'\u8981\u6267\u884c)\u3002\u4e00\u4e2aswitch\u8bed\u53e5\u53ef\u4ee5\u6709\u5f88\u591a\u5206\u652f\u3002 1 2 3 4 5 6 7 8 9 int foo ( int x , int y ) { int z { y }; if ( x > y ) { z = x ; } return z ; } \u5728\u4e4b\u524d\u7684\u4f8b\u5b50\u4e2d\uff0c\u8c03\u7528 foo(1, 0) \u53ef\u4ee5\u5b9e\u73b0100%\u7684\u8bed\u53e5\u8986\u76d6\u7387\uff0c\u5e76\u4e14\u6d4b\u8bd5 x > y \u7684\u60c5\u51b5\uff0c\u4f46\u662f\u5206\u652f\u8986\u76d6\u7387\u5374\u53ea\u670950%\u3002\u6211\u4eec\u9700\u8981\u518d\u8c03\u7528\u4e00\u6b21 foo(0, 1) \uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u6d4b\u5230 if \u8bed\u53e5\u4e0d\u6267\u884c\u7684\u60c5\u51b5\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 bool isLowerVowel ( char c ) { switch ( c ) { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : return true ; default : return false ; } } \u5728 isLowerVowel() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4e5f\u9700\u8981\u8c03\u7528\u4e24\u6b21\u624d\u80fd\u5f97\u5230100%\u7684\u5206\u652f\u8986\u76d6\u7387\uff1a\u7b2c\u4e00\u6b21\u8c03\u7528 (\u4f8b\u5982 isLowerVowel('a') ) \u6d4b\u8bd5\u524d\u9762\u7684\u51e0\u4e2acase\u5206\u652f\uff0c\u7b2c\u4e8c\u6b21\u8c03\u7528(\u4f8b\u5982 isLowerVowel('q') ) \u6765\u6d4b\u8bd5 default \u5206\u652f\u3002\u80fd\u591f\u5728\u4e00\u6b21\u6d4b\u8bd5\u4e2d\u6267\u884c\u5230\u7684\u5206\u652f\uff0c\u4e0d\u9700\u8981\u5206\u522b\u6d4b\u8bd5\u3002 \u8003\u8651\u4e0b\u9762\u7684\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 void compare ( int x , int y ) { if ( x > y ) std :: cout << x << \" is greater than \" << y << '\\n' ; // case 1 else if ( x < y ) std :: cout << x << \" is less than \" << y << '\\n' ; // case 2 else std :: cout << x << \" is equal to \" << y << '\\n' ; // case 3 } \u8be5\u51fd\u6570\u9700\u8981\u4e09\u6b21\u8c03\u7528\u624d\u80fd\u5b9e\u73b0100%\u5206\u652f\u8986\u76d6\uff1a compare(1, 0) \u6d4b\u8bd5\u7b2c\u4e00\u4e2a if \u8bed\u53e5\u4e3a\u771f\u7684\u5206\u652f\uff0c compare(0, 1) \u6d4b\u8bd5\u7b2c\u4e00\u4e2a if \u8bed\u53e5\u4e3a\u5047\u4e14\u7b2c\u4e8c\u4e2a if \u8bed\u53e5\u4e3a\u771f\u7684\u5206\u652f\u3002 compare(0, 0) \u6d4b\u8bd5\u4e24\u4e2a if \u8bed\u53e5\u90fd\u4e3a\u5047\uff0c\u6267\u884c else \u7684\u5206\u652f\u3002\u6267\u884c\u5b8c\u8fd9\u4e09\u6b21\u6d4b\u8bd5\u540e\uff0c\u6211\u4eec\u5c31\u6709\u4fe1\u5fc3\u8bf4\u8fd9\u4e2a\u51fd\u6570\u88ab\u53ef\u9760\u5730\u6d4b\u8bd5\u4e86\uff08\u4e09\u6b21\u6d4b\u8bd5\u603b\u597d\u8fc71800\u4ebf\u4ebf\u4e2a\u8f93\u5165\u7ec4\u5408\u5427\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 100%\u5206\u652f\u8986\u76d6\u7387\u662f\u6211\u4eec\u7684\u6d4b\u8bd5\u76ee\u6807\uff01","title":"\u5206\u652f\u8986\u76d6\u7387"},{"location":"7-13-Code-coverage/#_4","text":"\u5faa\u73af\u8986\u76d6\u7387(\u975e\u6b63\u5f0f\u5730\u79f0\u4e3a0,1,2\u6d4b\u8bd5)\u6307\u7684\u662f\uff0c\u5982\u679c\u4f60\u7684\u4ee3\u7801\u4e2d\u6709\u4e00\u4e2a\u5faa\u73af\uff0c\u5219\u5e94\u8be5\u786e\u4fdd\u5b83\u5728\u8fed\u4ee30\u6b21\u30011\u6b21\u548c2\u6b21\u65f6\u6b63\u5e38\u5de5\u4f5c\u3002\u5982\u679c\u5b83\u5bf92\u6b21\u8fed\u4ee3\u7684\u60c5\u51b5\u6b63\u786e\u5de5\u4f5c\uff0c\u90a3\u4e48\u5b83\u5e94\u8be5\u5bf9\u6240\u6709\u5927\u4e8e2\u6b21\u7684\u8fed\u4ee3\u90fd\u6b63\u786e\u5de5\u4f5c\u3002\u56e0\u6b64\uff0c\u8fd9\u4e09\u4e2a\u6d4b\u8bd5\u6db5\u76d6\u4e86\u6240\u6709\u7684\u53ef\u80fd\u6027(\u56e0\u4e3a\u5faa\u73af\u4e0d\u80fd\u6267\u884c\u8d1f\u6570\u6b21)\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 #include void spam ( int timesToPrint ) { for ( int count { 0 }; count < timesToPrint ; ++ count ) std :: cout << \"Spam! \" ; } \u4e3a\u4e86\u66f4\u597d\u5730\u6d4b\u8bd5\u8fd9\u4e2a\u51fd\u6570\u4e2d\u7684\u5faa\u73af\uff0c\u6211\u4eec\u9700\u8981\u8c03\u7528\u4e09\u6b21\u8be5\u51fd\u6570\uff1a spam(0) \u6d4b\u8bd50\u6b21\u8fed\u4ee3\u7684\u60c5\u51b5\uff0c spam(1) \u6d4b\u8bd51\u6b21\u8fed\u4ee3\u7684\u60c5\u51b5\uff0c\u7136\u540e\u8c03\u7528 spam(2) \u6d4b\u8bd5 2 \u6b21\u8fed\u4ee3\u7684\u60c5\u51b5\u3002\u5982\u679c spam(2) \u53ef\u4ee5\u5de5\u4f5c\uff0c\u5219\u5bf9\u4e8e\u4efb\u610f n>2 \u7684\u60c5\u51b5 spam(n) \u90fd\u5e94\u8be5\u53ef\u4ee5\u5de5\u4f5c\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528 \u201c0,1,2\u6d4b\u8bd5\u201d \u6765\u786e\u4fdd\u4f60\u7684\u5faa\u73af\u5728\u4e0d\u540c\u6b21\u6570\u7684\u8fed\u4ee3\u4e0b\u90fd\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002","title":"\u5faa\u73af\u8986\u76d6\u7387"},{"location":"7-13-Code-coverage/#_5","text":"\u5728\u7f16\u5199\u63a5\u53d7\u53c2\u6570\u7684\u51fd\u6570\u6216\u63a5\u53d7\u7528\u6237\u8f93\u5165\u65f6\uff0c\u8981\u8003\u8651\u4e0d\u540c\u7c7b\u522b\u7684\u8f93\u5165\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f7f\u7528\u672f\u8bed\u201c\u7c7b\u522b\u201d\u6765\u8868\u793a\u5177\u6709\u76f8\u4f3c\u7279\u5f81\u7684\u4e00\u7ec4\u8f93\u5165\u3002 \u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u4ea7\u751f\u6574\u6570\u5e73\u65b9\u6839\u7684\u51fd\u6570\uff0c\u7528\u4ec0\u4e48\u503c\u6765\u6d4b\u8bd5\u5b83\u662f\u6709\u610f\u4e49\u7684\uff1f\u4f60\u53ef\u80fd\u4f1a\u4ece\u4e00\u4e9b\u6b63\u5e38\u7684\u503c\u5f00\u59cb\uff0c\u6bd4\u5982\u201c4\u201d\u3002\u4f46\u662f\u7528\u201c0\u201d\u548c\u8d1f\u6570\u6765\u6d4b\u8bd5\u4e5f\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\u3002 \u4ee5\u4e0b\u662f\u5206\u7c7b\u6d4b\u8bd5\u7684\u4e00\u4e9b\u57fa\u672c\u51c6\u5219\uff1a \u5bf9\u4e8e\u6574\u6570\uff0c\u9700\u8981\u8003\u8651\u51fd\u6570\u5982\u4f55\u5904\u7406\u8d1f\u503c\u3001\u96f6\u548c\u6b63\u503c\u3002\u5982\u679c\u53ef\u80fd\u7684\u8bdd\uff0c\u8fd8\u5e94\u8be5\u68c0\u67e5\u662f\u5426\u6ea2\u51fa\u3002 \u5bf9\u4e8e\u6d6e\u70b9\u6570\uff0c\u786e\u4fdd\u8003\u8651\u4e86\u51fd\u6570\u5982\u4f55\u5904\u7406\u6709\u7cbe\u5ea6\u95ee\u9898\u7684\u503c(\u7565\u5927\u4e8e\u6216\u7565\u5c0f\u4e8e\u9884\u671f\u503c)\u3002\u9002\u5408\u6d4b\u8bd5\u7684 double \u7c7b\u578b\u503c\u662f 0.1 \u548c -0.1 (\u7528\u4e8e\u6d4b\u8bd5\u7565\u5927\u4e8e\u9884\u671f\u7684\u6570\u5b57)\u4ee5\u53ca 0.6 \u548c -0.6 (\u7528\u4e8e\u6d4b\u8bd5\u7565\u5c0f\u4e8e\u9884\u671f\u7684\u6570\u5b57)\u3002 \u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0c\u786e\u4fdd\u4f60\u8003\u8651\u4e86\u51fd\u6570\u5982\u4f55\u5904\u7406\u7a7a\u5b57\u7b26\u4e32(\u53ea\u662f\u4e00\u4e2a\u7a7a\u7ed3\u675f\u7b26)\u3001\u6b63\u5e38\u6709\u6548\u7684\u5b57\u7b26\u4e32\u3001\u6709\u7a7a\u683c\u7684\u5b57\u7b26\u4e32\u4ee5\u53ca\u5168\u662f\u7a7a\u683c\u7684\u5b57\u7b26\u4e32\u3002 \u5982\u679c\u51fd\u6570\u4f1a\u5904\u7406\u6307\u9488\uff0c\u4e0d\u8981\u5fd8\u8bb0\u6d4b\u8bd5 nullptr (\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u8fd9\u662f\u4ec0\u4e48\u4e5f\u4e0d\u8981\u7d27\uff0c\u6bd5\u7adf\u6211\u4eec\u8fd8\u6ca1\u4ecb\u7ecd\u8fc7\u5b83)\u3002 \u6700\u4f73\u5b9e\u8df5 \u6d4b\u8bd5\u4e0d\u540c\u7c7b\u522b\u7684\u8f93\u5165\u503c\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u5355\u5143\u80fd\u591f\u6b63\u786e\u5730\u5904\u7406\u5b83\u4eec\u3002","title":"\u6d4b\u8bd5\u4e0d\u540c\u7c7b\u522b\u7684\u8f93\u5165"},{"location":"7-14-Common-semantic-errors-in-c/","text":"Key Takeaway \u5728 [[3-1-Syntax-and-semantic-errors|3.1 - \u8bed\u6cd5\u548c\u8bed\u4e49\u9519\u8bef] \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86 \u8bed\u6cd5\u9519\u8bef \u2014\u2014\u5f53\u4ee3\u7801\u4e0d\u7b26\u5408C++\u8bed\u4e49\u7684\u8bed\u6cd5\u89c4\u5219\u65f6\uff0c\u5c31\u4f1a\u4ea7\u751f\u8bed\u6cd5\u9519\u8bef\u3002\u7f16\u8bd1\u5668\u80fd\u591f\u8bc6\u522b\u5e76\u62a5\u544a\u8bed\u6cd5\u9519\u8bef\uff0c\u6240\u4ee5\u5b83\u4eec\u5f88\u5bb9\u6613\u88ab\u8bc6\u522b\u5230\uff0c\u901a\u5e38\u4e5f\u5f88\u5bb9\u6613\u4fee\u590d\u3002 \u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86 \u8bed\u4e49\u9519\u8bef \u2014\u2014\u5f53\u4ee3\u7801\u7684\u884c\u4e3a\u4e0d\u7b26\u5408\u4f60\u7684\u672c\u610f\u65f6\uff0c\u5c31\u88ab\u8ba4\u4e3a\u662f\u8bed\u4e49\u9519\u8bef\u3002\u7f16\u8bd1\u5668\u901a\u5e38\u60c5\u51b5\u4e0b\u5e76\u4e0d\u80fd\u8bc6\u522b\u5230\u8bed\u4e49\u9519\u8bef\uff08\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u667a\u80fd\u7684\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5bf9\u8bed\u4e49\u9519\u8bef\u4ea7\u751f\u544a\u8b66\u4fe1\u606f\uff09\u3002 \u8bed\u4e49\u9519\u8bef\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5f88\u591a \u672a\u5b9a\u4e49\u884c\u4e3a \u7c7b\u4f3c\u7684\u75c7\u72b6\uff0c\u4f8b\u5982\u5bfc\u81f4\u7a0b\u5e8f\u4ea7\u751f\u9519\u8bef\u7684\u7ed3\u679c\u3001\u4ea7\u751f\u53e4\u602a\u7684\u7a0b\u5e8f\u884c\u4e3a\u3001\u7834\u574f\u6570\u636e\u3001\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u2014\u2014\u6216\u8005\u4e0d\u4ea7\u751f\u4efb\u4f55\u5f71\u54cd\u3002 \u5728\u7f16\u5199\u7a0b\u5e8f\u65f6\uff0c\u6211\u4eec\u5f88\u96be\u5b8c\u5168\u907f\u514d\u8bed\u4e49\u9519\u8bef\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5728\u4f7f\u7528\u7a0b\u5e8f\u65f6\u6211\u4eec\u5c31\u4f1a\u9047\u5230\u8fd9\u4e9b\u8bed\u4e49\u9519\u8bef\uff1a\u4f8b\u5982\uff0c\u5728\u4f60\u7f16\u5199\u7684\u8ff7\u5bab\u6e38\u620f\u4e2d\uff0c\u6e38\u620f\u89d2\u8272\u7adf\u7136\u80fd\u591f\u7a7f\u5899\uff01\u901a\u8fc7\u5bf9\u7a0b\u5e8f\u8fdb\u884c\u6d4b\u8bd5 ( 7.12 - \u4ee3\u7801\u6d4b\u8bd5 ) \u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u53d1\u73b0\u8bed\u4e49\u9519\u8bef\u3002 \u4e0d\u8fc7\uff0c\u9664\u4e86\u6d4b\u8bd5\u4e4b\u5916\uff0c\u8fd8\u6709\u4e00\u4ef6\u4e8b\u53ef\u4ee5\u5e2e\u52a9\u4f60\u2014\u2014\u90a3\u5c31\u662f\u77e5\u9053\u54ea\u79cd\u7c7b\u578b\u7684\u8bed\u4e49\u9519\u8bef\u6700\u5e38\u89c1\uff0c\u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u5bf9\u8fd9\u4e9b\u60c5\u51b5\u591a\u52a0\u6ce8\u610f\uff01 \u672c\u8282\u8bfe\u4e2d\u6211\u4eec\u4f1a\u4ecb\u7ecd\u51e0\u79cd\u5e38\u89c1\u7684\u610f\u4e49\u9519\u8bef\uff08\u5f88\u591a\u90fd\u548c\u6761\u4ef6\u63a7\u5236\u76f8\u5173\uff09\u3002 \u6761\u4ef6\u903b\u8f91\u9519\u8bef \u6700\u5e38\u89c1\u7684\u8bed\u4e49\u9519\u8bef\u7c7b\u578b\u4e4b\u4e00\u662f \u6761\u4ef6\u903b\u8f91\u9519\u8bef \u3002\u5f53\u7a0b\u5e8f\u5458\u9519\u8bef\u5730\u7f16\u5199\u4e86\u6761\u4ef6\u8bed\u53e5\u6216\u5faa\u73af\u6761\u4ef6\u7684\u903b\u8f91\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u6761\u4ef6\u903b\u8f91\u9519\u8bef\u3002\u8fd9\u91cc\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; if ( x >= 5 ) // oops, we used operator>= instead of operator> std :: cout << x << \" is greater than 5 \\n \" ; return 0 ; } \u8fd0\u884c\u7a0b\u5e8f\uff0c\u5f88\u5bb9\u6613\u89c2\u5bdf\u5230\u903b\u8f91\u9519\u8bef\uff1a 1 2 Enter an integer: 5 5 is greater than 5 \u5f53\u7528\u6237\u8f93\u51655\u65f6\uff0c\u6761\u4ef6\u8868\u8fbe\u5f0f x >= 5 \u6c42\u503c\u4e3a true \uff0c\u6240\u4ee5\u5bf9\u5e94\u7684\u6253\u5370\u8bed\u53e5\u5c31\u6267\u884c\u4e86\u3002 \u53e6\u5916\u4e00\u4e2a\u4f8b\u5b50\u662f\u5173\u4e8e for \u5faa\u73af\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; // oops, we used operator> instead of operator< for ( int count { 1 }; count > x ; ++ count ) { std :: cout << count << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u5e94\u8be5\u6253\u5370\u4ece1\u5230\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\u4e4b\u95f4\u7684\u6240\u6709\u6570\u5b57\u3002\u4f46\u5b83\u5b9e\u9645\u4e0a\u662f\u8fd9\u6837\u505a\u7684\uff1a 1 Enter an integer: 5 \u7a0b\u5e8f\u6ca1\u6709\u6253\u5370\u4efb\u4f55\u5185\u5bb9\u3002\u8fd9\u662f\u56e0\u4e3a\u8fdb\u5165\u5faa\u73af\u7684\u6761\u4ef6 count > x \u6c42\u503c\u4e3a false \uff0c\u6240\u4ee5\u5faa\u73af\u6839\u672c\u6ca1\u6709\u8fdb\u884c\u3002 \u65e0\u9650\u5faa\u73af \u5728 7.7 - \u5faa\u73af\u548c while \u8bed\u53e5 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u65e0\u9650\u5faa\u73af\uff08\u6b7b\u5faa\u73af\uff09\uff0c\u8bf7\u770b\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int count { 1 }; while ( count <= 10 ) // this condition will never be false { std :: cout << count << ' ' ; // so this line will repeatedly execute } std :: cout << '\\n' ; // this line will never execute return 0 ; // this line will never execute } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5fd8\u8bb0\u4e86\u5bf9 count \u8fdb\u884c\u9012\u589e\uff0c\u6240\u4ee5\u5faa\u73af\u6761\u4ef6\u6c42\u503c\u7ed3\u679c\u6c38\u8fdc\u90fd\u4e0d\u4e3a false \uff0c\u5faa\u73af\u4e5f\u5c31\u6c38\u8fdc\u4e0d\u4f1a\u505c\u6b62\uff0c\u53ea\u80fd\u4e0d\u505c\u6253\u5370\uff1a 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 \u76f4\u5230\u7528\u6237\u7ec8\u6b62\u8be5\u7a0b\u5e8f\u3002 \u8fd9\u662f\u53e6\u4e00\u4e2a\u5bb9\u6613\u88ab\u95ee\u5230\u7684\u5c0f\u6d4b\u9a8c\u95ee\u9898\u2014\u2014\u4e0b\u9762\u7684\u4ee3\u7801\u6709\u4ec0\u4e48\u95ee\u9898? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { for ( unsigned int count { 5 }; count >= 0 ; -- count ) { if ( count == 0 ) std :: cout << \"blastoff! \" ; else std :: cout << count << ' ' ; } std :: cout << '\\n' ; return 0 ; } COPY \u7a0b\u5e8f\u7684\u672c\u610f\u662f\u6253\u5370 5 4 3 2 1 blastoff! \uff0c\u4f46\u662f\u5728\u5b8c\u6210\u64cd\u4f5c\u540e\uff0c\u7a0b\u5e8f\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\u505c\u6b62\uff0c\u800c\u662f\u7ee7\u7eed\u6253\u5370\uff1a 1 5 4 3 2 1 blastoff! 4294967295 4294967294 4294967293 4294967292 4294967291 \u7136\u540e\u7ee7\u7eed\u9012\u51cf\u3002\u7a0b\u5e8f\u6c38\u8fdc\u4e0d\u4f1a\u505c\u6b62\uff0c\u56e0\u4e3a\u5f53 count \u662f\u65e0\u7b26\u53f7\u6574\u578b\u65f6 count >= 0 \u6c38\u8fdc\u4e3a\u771f\u3002 \u5dee\u4e00\u9519\u8bef \u5dee\u4e00\u9519\u8bef \u6307\u7684\u662f\u5faa\u73af\u591a\u6267\u884c\u4e00\u6b21\u6216\u5c11\u6267\u884c\u4e00\u6b21\u800c\u4ea7\u751f\u7684\u9519\u8bef\u3002\u60c5\u51b5\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { for ( unsigned int count { 1 }; count < 5 ; ++ count ) { std :: cout << count << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u7a0b\u5e8f\u5458\u5e0c\u671b\u4ee3\u7801\u6253\u5370 1 2 3 4 5 \u3002\u4f46\u662f\uff0c\u7531\u4e8e\u4f7f\u7528\u4e86\u9519\u8bef\u7684\u8fd0\u7b97\u7b26 ( < \u800c\u4e0d\u662f <= )\uff0c\u6240\u4ee5\u5faa\u73af\u5c11\u6267\u884c\u4e86\u4e00\u6b21\uff0c\u7a0b\u5e8f\u6253\u5370 1 2 3 4 \u3002 \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u9519\u8bef \u5728 5.7 - \u903b\u8f91\u8fd0\u7b97\u7b26 \u4e2d\uff0c\u4e0b\u9762\u7a0b\u5e8f\u5b58\u5728\u4f18\u5148\u7ea7\u9519\u8bef\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 5 }; int y { 7 }; if ( ! x > y ) // oops: operator precedence issue std :: cout << x << \" is not greater than \" << y << '\\n' ; else std :: cout << x << \" is greater than \" << y << '\\n' ; return 0 ; } \u56e0\u4e3a\u903b\u8f91\u8fd0\u7b97\u7b26 NOT \u6bd4 operator> \u7684\u4f18\u5148\u7ea7\u66f4\u9ad8\uff0c\u6240\u4ee5\u6761\u4ef6\u8868\u8fbe\u5f0f\u5b9e\u9645\u4e0a\u662f\u6309\u7167 (!x) > y \u6c42\u503c\uff0c\u8fd9\u5e76\u4e0d\u662f\u7a0b\u5e8f\u5458\u6240\u5e0c\u671b\u7684\u3002 \u7ed3\u679c\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u8f93\u51fa\uff1a 1 5 is greater than 7 \u5728\u540c\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u6df7\u5408\u903b\u8f91\u6216\u548c\u903b\u8f91\u4e0e\u65f6\u4e5f\u4f1a\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5(\u903b\u8f91\u4e0e\u4f18\u5148\u4e8e\u903b\u8f91\u6216)\u3002\u4f7f\u7528\u663e\u5f0f\u62ec\u53f7\u53ef\u4ee5\u907f\u514d\u8fd9\u7c7b\u9519\u8bef\u3002 \u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u95ee\u9898 \u4e0b\u9762\u7684\u6d6e\u70b9\u53d8\u91cf\u6ca1\u6709\u8db3\u591f\u7684\u7cbe\u5ea6\u6765\u5b58\u50a8\u6574\u4e2a\u6570\u5b57\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { float f { 0.123456789f }; std :: cout << f << '\\n' ; return 0 ; } \u7531\u4e8e\u7f3a\u4e4f\u7cbe\u5ea6\uff0c\u8fd9\u4e2a\u6570\u5b57\u88ab\u7a0d\u7a0d\u56db\u820d\u4e94\u5165\uff1a 1 0.123457 \u5728 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 \u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86 operator== \u548c operator != \u5728\u5e94\u5bf9\u6d6e\u70b9\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u4ea7\u751f\u7531\u4e8e\u5fae\u5c0f\u6444\u5165\u800c\u5bfc\u81f4\u7684\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { double d { 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 }; // should sum to 1.0 if ( d == 1.0 ) std :: cout << \"equal \\n \" ; else std :: cout << \"not equal \\n \" ; return 0 ; } \u6253\u5370\uff1a 1 not equal \u7531\u4e8e\u7f3a\u4e4f\u7cbe\u5ea6\uff0c\u8fd9\u4e2a\u6570\u5b57\u4f1a\u7565\u5fae\u56db\u820d\u4e94\u5165\uff1a\u4f60\u5bf9\u6d6e\u70b9\u6570\u505a\u7684\u7b97\u672f\u8d8a\u591a\uff0c\u5b83\u5c31\u4f1a\u79ef\u7d2f\u8d8a\u591a\u7684\u5c0f\u56db\u820d\u4e94\u5165\u9519\u8bef\u3002 \u6574\u578b\u9664\u6cd5 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6253\u7b97\u505a\u6d6e\u70b9\u9664\u6cd5\uff0c\u4f46\u56e0\u4e3a\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u662f\u6574\u6570\uff0c\u6240\u4ee5\u6211\u4eec\u6700\u7ec8\u505a\u7684\u662f\u6574\u6570\u9664\u6cd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { int x { 5 }; int y { 3 }; std :: cout << x << \" divided by \" << y << \" is: \" << x / y << '\\n' ; // integer division return 0 ; } \u6253\u5370\uff1a 1 5 divided by 3 is: 1 \u5728 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528 static_cast \u5c06\u6574\u578b\u64cd\u4f5c\u6570\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u8fdb\u884c\u6d6e\u70b9\u6570\u9664\u6cd5\u3002 \u610f\u5916\u9020\u6210\u7684\u7a7a\u8bed\u53e5 \u5728 7.3 - \u5e38\u89c1\u7684 if \u8bed\u53e5\u9519\u8bef \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u7a7a\u8bed\u53e5\u2014\u2014\u7a7a\u8bed\u53e5\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\u3002 \u5728\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u53ea\u60f3\u5728\u5f97\u5230\u7528\u6237\u8bb8\u53ef\u7684\u60c5\u51b5\u4e0b\"\u70b8\u6389\u8fd9\u4e2a\u4e16\u754c\"\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include void blowUpWorld () { std :: cout << \"Kaboom! \\n \" ; } int main () { std :: cout << \"Should we blow up the world again? (y/n): \" ; char c {}; std :: cin >> c ; if ( c == 'y' ); // \u610f\u5916\u5bfc\u81f4\u4e86\u7a7a\u8bed\u53e5 null statement here blowUpWorld (); // \u8be5\u8bed\u53e5\u603b\u662f\u4f1a\u6267\u884c\uff0c\u56e0\u4e3a\u5b83\u4e0d\u5c5e\u4e8eif\u8bed\u53e5 return 0 ; } \u4f46\u662f\uff0c\u56e0\u4e3a\u5b58\u5728\u4e00\u6761\u610f\u5916\u9020\u6210\u7684\u7a7a\u8bed\u53e5\uff0c\u51fd\u6570 blowUpWorld() \u603b\u662f\u4f1a\u6267\u884c\uff0c\u6240\u4ee5\u201c\u4e16\u754c\u201d\u603b\u662f\u4f1a\u88ab\u6bc1\u706d\uff1a 1 2 Should we blow up the world again? (y/n): n Kaboom! \u5f53\u9700\u8981\u590d\u5408\u8bed\u53e5\u65f6\uff0c\u6ca1\u6709\u4f7f\u7528\u590d\u5408\u8bed\u53e5 \u4e0a\u8ff0\u7a0b\u5e8f\u7684\u53e6\u4e00\u79cd\u53d8\u4f53\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void blowUpWorld () { std :: cout << \"Kaboom! \\n \" ; } int main () { std :: cout << \"Should we blow up the world again? (y/n): \" ; char c {}; std :: cin >> c ; if ( c == 'y' ) std :: cout << \"Okay, here we go... \\n \" ; blowUpWorld (); // oops, will always execute. Should be inside compound statement. return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 2 Should we blow up the world again? (y/n): n Kaboom! \u60ac\u5782 else (\u53c2\u89c1 7.3 - \u5e38\u89c1\u7684 if \u8bed\u53e5\u9519\u8bef ) \u4e5f\u4f1a\u5bfc\u81f4\u6b64\u7c7b\u95ee\u9898\u3002 \u8fd8\u6709\u4ec0\u4e48\uff1f \u4ee5\u4e0a\u662f\u65b0\u624bC++\u7a0b\u5e8f\u5458\u5bb9\u6613\u72af\u7684\u3001\u6700\u5e38\u89c1\u8bed\u4e49\u9519\u8bef\uff0c\u5f53\u7136\uff0c\u9664\u4e86\u8fd9\u4e9b\u8fd8\u6709\u66f4\u591a\u3002\u8bfb\u8005\u4eec\uff0c\u5982\u679c\u4f60\u8fd8\u6709\u5176\u4ed6\u4f60\u8ba4\u4e3a\u5e38\u89c1\u7684\u9677\u9631\uff0c\u8bf7\u5728\u8bc4\u8bba\u533a\u544a\u8bc9\u6211\u4eec\u3002","title":"7.14 - C++ \u4e2d\u5e38\u89c1\u7684\u8bed\u4e49\u9519\u8bef"},{"location":"7-14-Common-semantic-errors-in-c/#_1","text":"\u6700\u5e38\u89c1\u7684\u8bed\u4e49\u9519\u8bef\u7c7b\u578b\u4e4b\u4e00\u662f \u6761\u4ef6\u903b\u8f91\u9519\u8bef \u3002\u5f53\u7a0b\u5e8f\u5458\u9519\u8bef\u5730\u7f16\u5199\u4e86\u6761\u4ef6\u8bed\u53e5\u6216\u5faa\u73af\u6761\u4ef6\u7684\u903b\u8f91\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u6761\u4ef6\u903b\u8f91\u9519\u8bef\u3002\u8fd9\u91cc\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; if ( x >= 5 ) // oops, we used operator>= instead of operator> std :: cout << x << \" is greater than 5 \\n \" ; return 0 ; } \u8fd0\u884c\u7a0b\u5e8f\uff0c\u5f88\u5bb9\u6613\u89c2\u5bdf\u5230\u903b\u8f91\u9519\u8bef\uff1a 1 2 Enter an integer: 5 5 is greater than 5 \u5f53\u7528\u6237\u8f93\u51655\u65f6\uff0c\u6761\u4ef6\u8868\u8fbe\u5f0f x >= 5 \u6c42\u503c\u4e3a true \uff0c\u6240\u4ee5\u5bf9\u5e94\u7684\u6253\u5370\u8bed\u53e5\u5c31\u6267\u884c\u4e86\u3002 \u53e6\u5916\u4e00\u4e2a\u4f8b\u5b50\u662f\u5173\u4e8e for \u5faa\u73af\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; // oops, we used operator> instead of operator< for ( int count { 1 }; count > x ; ++ count ) { std :: cout << count << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u5e94\u8be5\u6253\u5370\u4ece1\u5230\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\u4e4b\u95f4\u7684\u6240\u6709\u6570\u5b57\u3002\u4f46\u5b83\u5b9e\u9645\u4e0a\u662f\u8fd9\u6837\u505a\u7684\uff1a 1 Enter an integer: 5 \u7a0b\u5e8f\u6ca1\u6709\u6253\u5370\u4efb\u4f55\u5185\u5bb9\u3002\u8fd9\u662f\u56e0\u4e3a\u8fdb\u5165\u5faa\u73af\u7684\u6761\u4ef6 count > x \u6c42\u503c\u4e3a false \uff0c\u6240\u4ee5\u5faa\u73af\u6839\u672c\u6ca1\u6709\u8fdb\u884c\u3002","title":"\u6761\u4ef6\u903b\u8f91\u9519\u8bef"},{"location":"7-14-Common-semantic-errors-in-c/#_2","text":"\u5728 7.7 - \u5faa\u73af\u548c while \u8bed\u53e5 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u65e0\u9650\u5faa\u73af\uff08\u6b7b\u5faa\u73af\uff09\uff0c\u8bf7\u770b\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int count { 1 }; while ( count <= 10 ) // this condition will never be false { std :: cout << count << ' ' ; // so this line will repeatedly execute } std :: cout << '\\n' ; // this line will never execute return 0 ; // this line will never execute } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5fd8\u8bb0\u4e86\u5bf9 count \u8fdb\u884c\u9012\u589e\uff0c\u6240\u4ee5\u5faa\u73af\u6761\u4ef6\u6c42\u503c\u7ed3\u679c\u6c38\u8fdc\u90fd\u4e0d\u4e3a false \uff0c\u5faa\u73af\u4e5f\u5c31\u6c38\u8fdc\u4e0d\u4f1a\u505c\u6b62\uff0c\u53ea\u80fd\u4e0d\u505c\u6253\u5370\uff1a 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 \u76f4\u5230\u7528\u6237\u7ec8\u6b62\u8be5\u7a0b\u5e8f\u3002 \u8fd9\u662f\u53e6\u4e00\u4e2a\u5bb9\u6613\u88ab\u95ee\u5230\u7684\u5c0f\u6d4b\u9a8c\u95ee\u9898\u2014\u2014\u4e0b\u9762\u7684\u4ee3\u7801\u6709\u4ec0\u4e48\u95ee\u9898? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { for ( unsigned int count { 5 }; count >= 0 ; -- count ) { if ( count == 0 ) std :: cout << \"blastoff! \" ; else std :: cout << count << ' ' ; } std :: cout << '\\n' ; return 0 ; } COPY \u7a0b\u5e8f\u7684\u672c\u610f\u662f\u6253\u5370 5 4 3 2 1 blastoff! \uff0c\u4f46\u662f\u5728\u5b8c\u6210\u64cd\u4f5c\u540e\uff0c\u7a0b\u5e8f\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\u505c\u6b62\uff0c\u800c\u662f\u7ee7\u7eed\u6253\u5370\uff1a 1 5 4 3 2 1 blastoff! 4294967295 4294967294 4294967293 4294967292 4294967291 \u7136\u540e\u7ee7\u7eed\u9012\u51cf\u3002\u7a0b\u5e8f\u6c38\u8fdc\u4e0d\u4f1a\u505c\u6b62\uff0c\u56e0\u4e3a\u5f53 count \u662f\u65e0\u7b26\u53f7\u6574\u578b\u65f6 count >= 0 \u6c38\u8fdc\u4e3a\u771f\u3002","title":"\u65e0\u9650\u5faa\u73af"},{"location":"7-14-Common-semantic-errors-in-c/#_3","text":"\u5dee\u4e00\u9519\u8bef \u6307\u7684\u662f\u5faa\u73af\u591a\u6267\u884c\u4e00\u6b21\u6216\u5c11\u6267\u884c\u4e00\u6b21\u800c\u4ea7\u751f\u7684\u9519\u8bef\u3002\u60c5\u51b5\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { for ( unsigned int count { 1 }; count < 5 ; ++ count ) { std :: cout << count << ' ' ; } std :: cout << '\\n' ; return 0 ; } \u7a0b\u5e8f\u5458\u5e0c\u671b\u4ee3\u7801\u6253\u5370 1 2 3 4 5 \u3002\u4f46\u662f\uff0c\u7531\u4e8e\u4f7f\u7528\u4e86\u9519\u8bef\u7684\u8fd0\u7b97\u7b26 ( < \u800c\u4e0d\u662f <= )\uff0c\u6240\u4ee5\u5faa\u73af\u5c11\u6267\u884c\u4e86\u4e00\u6b21\uff0c\u7a0b\u5e8f\u6253\u5370 1 2 3 4 \u3002","title":"\u5dee\u4e00\u9519\u8bef"},{"location":"7-14-Common-semantic-errors-in-c/#_4","text":"\u5728 5.7 - \u903b\u8f91\u8fd0\u7b97\u7b26 \u4e2d\uff0c\u4e0b\u9762\u7a0b\u5e8f\u5b58\u5728\u4f18\u5148\u7ea7\u9519\u8bef\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 5 }; int y { 7 }; if ( ! x > y ) // oops: operator precedence issue std :: cout << x << \" is not greater than \" << y << '\\n' ; else std :: cout << x << \" is greater than \" << y << '\\n' ; return 0 ; } \u56e0\u4e3a\u903b\u8f91\u8fd0\u7b97\u7b26 NOT \u6bd4 operator> \u7684\u4f18\u5148\u7ea7\u66f4\u9ad8\uff0c\u6240\u4ee5\u6761\u4ef6\u8868\u8fbe\u5f0f\u5b9e\u9645\u4e0a\u662f\u6309\u7167 (!x) > y \u6c42\u503c\uff0c\u8fd9\u5e76\u4e0d\u662f\u7a0b\u5e8f\u5458\u6240\u5e0c\u671b\u7684\u3002 \u7ed3\u679c\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u8f93\u51fa\uff1a 1 5 is greater than 7 \u5728\u540c\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u6df7\u5408\u903b\u8f91\u6216\u548c\u903b\u8f91\u4e0e\u65f6\u4e5f\u4f1a\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5(\u903b\u8f91\u4e0e\u4f18\u5148\u4e8e\u903b\u8f91\u6216)\u3002\u4f7f\u7528\u663e\u5f0f\u62ec\u53f7\u53ef\u4ee5\u907f\u514d\u8fd9\u7c7b\u9519\u8bef\u3002","title":"\u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u9519\u8bef"},{"location":"7-14-Common-semantic-errors-in-c/#_5","text":"\u4e0b\u9762\u7684\u6d6e\u70b9\u53d8\u91cf\u6ca1\u6709\u8db3\u591f\u7684\u7cbe\u5ea6\u6765\u5b58\u50a8\u6574\u4e2a\u6570\u5b57\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { float f { 0.123456789f }; std :: cout << f << '\\n' ; return 0 ; } \u7531\u4e8e\u7f3a\u4e4f\u7cbe\u5ea6\uff0c\u8fd9\u4e2a\u6570\u5b57\u88ab\u7a0d\u7a0d\u56db\u820d\u4e94\u5165\uff1a 1 0.123457 \u5728 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 \u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86 operator== \u548c operator != \u5728\u5e94\u5bf9\u6d6e\u70b9\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u4ea7\u751f\u7531\u4e8e\u5fae\u5c0f\u6444\u5165\u800c\u5bfc\u81f4\u7684\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { double d { 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 }; // should sum to 1.0 if ( d == 1.0 ) std :: cout << \"equal \\n \" ; else std :: cout << \"not equal \\n \" ; return 0 ; } \u6253\u5370\uff1a 1 not equal \u7531\u4e8e\u7f3a\u4e4f\u7cbe\u5ea6\uff0c\u8fd9\u4e2a\u6570\u5b57\u4f1a\u7565\u5fae\u56db\u820d\u4e94\u5165\uff1a\u4f60\u5bf9\u6d6e\u70b9\u6570\u505a\u7684\u7b97\u672f\u8d8a\u591a\uff0c\u5b83\u5c31\u4f1a\u79ef\u7d2f\u8d8a\u591a\u7684\u5c0f\u56db\u820d\u4e94\u5165\u9519\u8bef\u3002","title":"\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u95ee\u9898"},{"location":"7-14-Common-semantic-errors-in-c/#_6","text":"\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6253\u7b97\u505a\u6d6e\u70b9\u9664\u6cd5\uff0c\u4f46\u56e0\u4e3a\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u662f\u6574\u6570\uff0c\u6240\u4ee5\u6211\u4eec\u6700\u7ec8\u505a\u7684\u662f\u6574\u6570\u9664\u6cd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { int x { 5 }; int y { 3 }; std :: cout << x << \" divided by \" << y << \" is: \" << x / y << '\\n' ; // integer division return 0 ; } \u6253\u5370\uff1a 1 5 divided by 3 is: 1 \u5728 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528 static_cast \u5c06\u6574\u578b\u64cd\u4f5c\u6570\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u8fdb\u884c\u6d6e\u70b9\u6570\u9664\u6cd5\u3002","title":"\u6574\u578b\u9664\u6cd5"},{"location":"7-14-Common-semantic-errors-in-c/#_7","text":"\u5728 7.3 - \u5e38\u89c1\u7684 if \u8bed\u53e5\u9519\u8bef \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u7a7a\u8bed\u53e5\u2014\u2014\u7a7a\u8bed\u53e5\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\u3002 \u5728\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u53ea\u60f3\u5728\u5f97\u5230\u7528\u6237\u8bb8\u53ef\u7684\u60c5\u51b5\u4e0b\"\u70b8\u6389\u8fd9\u4e2a\u4e16\u754c\"\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include void blowUpWorld () { std :: cout << \"Kaboom! \\n \" ; } int main () { std :: cout << \"Should we blow up the world again? (y/n): \" ; char c {}; std :: cin >> c ; if ( c == 'y' ); // \u610f\u5916\u5bfc\u81f4\u4e86\u7a7a\u8bed\u53e5 null statement here blowUpWorld (); // \u8be5\u8bed\u53e5\u603b\u662f\u4f1a\u6267\u884c\uff0c\u56e0\u4e3a\u5b83\u4e0d\u5c5e\u4e8eif\u8bed\u53e5 return 0 ; } \u4f46\u662f\uff0c\u56e0\u4e3a\u5b58\u5728\u4e00\u6761\u610f\u5916\u9020\u6210\u7684\u7a7a\u8bed\u53e5\uff0c\u51fd\u6570 blowUpWorld() \u603b\u662f\u4f1a\u6267\u884c\uff0c\u6240\u4ee5\u201c\u4e16\u754c\u201d\u603b\u662f\u4f1a\u88ab\u6bc1\u706d\uff1a 1 2 Should we blow up the world again? (y/n): n Kaboom! \u5f53\u9700\u8981\u590d\u5408\u8bed\u53e5\u65f6\uff0c\u6ca1\u6709\u4f7f\u7528\u590d\u5408\u8bed\u53e5 \u4e0a\u8ff0\u7a0b\u5e8f\u7684\u53e6\u4e00\u79cd\u53d8\u4f53\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void blowUpWorld () { std :: cout << \"Kaboom! \\n \" ; } int main () { std :: cout << \"Should we blow up the world again? (y/n): \" ; char c {}; std :: cin >> c ; if ( c == 'y' ) std :: cout << \"Okay, here we go... \\n \" ; blowUpWorld (); // oops, will always execute. Should be inside compound statement. return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 2 Should we blow up the world again? (y/n): n Kaboom! \u60ac\u5782 else (\u53c2\u89c1 7.3 - \u5e38\u89c1\u7684 if \u8bed\u53e5\u9519\u8bef ) \u4e5f\u4f1a\u5bfc\u81f4\u6b64\u7c7b\u95ee\u9898\u3002","title":"\u610f\u5916\u9020\u6210\u7684\u7a7a\u8bed\u53e5"},{"location":"7-14-Common-semantic-errors-in-c/#_8","text":"\u4ee5\u4e0a\u662f\u65b0\u624bC++\u7a0b\u5e8f\u5458\u5bb9\u6613\u72af\u7684\u3001\u6700\u5e38\u89c1\u8bed\u4e49\u9519\u8bef\uff0c\u5f53\u7136\uff0c\u9664\u4e86\u8fd9\u4e9b\u8fd8\u6709\u66f4\u591a\u3002\u8bfb\u8005\u4eec\uff0c\u5982\u679c\u4f60\u8fd8\u6709\u5176\u4ed6\u4f60\u8ba4\u4e3a\u5e38\u89c1\u7684\u9677\u9631\uff0c\u8bf7\u5728\u8bc4\u8bba\u533a\u544a\u8bc9\u6211\u4eec\u3002","title":"\u8fd8\u6709\u4ec0\u4e48\uff1f"},{"location":"7-15-Detecting-and-handling-errors/","tags":["error handling"],"text":"Key Takeaway In lesson 7.14 -- Common semantic errors in C++ , we covered many types of common C++ semantic errors that new C++ programmers run into with the language. If an error is the result of a misused language feature or logic error, the error can simply be corrected. But most errors in a program don\u2019t occur as the result of inadvertently misusing language features -- rather, most errors occur due to faulty assumptions made by the programmer and/or a lack of proper error detection/handling. For example, in a function designed to look up a grade for a student, you might have assumed: The student being looked up will exist. All student names will be unique. The class uses letter grading (instead of pass/fail). What if any of these assumptions aren\u2019t true? If the programmer didn\u2019t anticipate these cases, the program will likely malfunction or crash when such cases arise (usually at some point in the future, well after the function has been written). There are three key places where assumption errors typically occur: When a function returns, the programmer may have assumed the called function was successful when it was not. When a program receives input (either from the user, or a file), the programmer may have assumed the input was in the correct format and semantically valid when it was not. When a function has been called, the programmer may have assumed the arguments would be semantically valid when they were not. Many new programmers write code and then only test the happy path: only the cases where there are no errors. But you should also be planning for and testing your sad paths, where things can and will go wrong. In lesson 3.10 -- Finding issues before they become problems , we defined defensive programming as the practice of trying to anticipate all of the ways software can be misused, either by end-users, or by developers (either the programmer themselves, or others). Once you\u2019ve anticipated (or discovered) some misuse, the next thing to do is handle it. In this lesson, we\u2019ll talk about error handling strategies (what to do when things go wrong) inside a function. In the subsequent lessons, we\u2019ll talk about validating user input, and then introduce a useful tool to help document and validate assumptions. Handling errors in functions Functions may fail for any number of reasons -- the caller may have passed in an argument with an invalid value, or something may fail within the body of the function. For example, a function that opens a file for reading might fail if the file cannot be found. When this happens, you have quite a few options at your disposal. There is no best way to handle an error -- it really depends on the nature of the problem and whether the problem can be fixed or not. There are 4 general strategies that can be used: Handle the error within the function Pass the error back to the caller to deal with Halt the program Throw an exception Handling the error within the function If possible, the best strategy is to recover from the error in the same function in which the error occurred, so that the error can be contained and corrected without impacting any code outside the function. There are two options here: retry until successful, or cancel the operation being executed. If the error has occurred due to something outside of the program\u2019s control, the program can retry until success is achieved. For example, if the program requires an internet connection, and the user has lost their connection, the program may be able to display a warning and then use a loop to periodically recheck for internet connectivity. Alternatively, if the user has entered invalid input, the program can ask the user to try again, and loop until the user is successful at entering valid input. We\u2019ll show examples of handling invalid input and using loops to retry in the next lesson ( 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406 )\u3002 An alternate strategy is just to ignore the error and/or cancel the operation. For example: 1 2 3 4 5 void printDivision ( int x , int y ) { if ( y != 0 ) std :: cout << static_cast < double > ( x ) / y ; } COPY In the above example, if the user passed in an invalid value for y , we just ignore the request to print the result of the division operation. The primary challenge with doing this is that the caller or user have no way to identify that something went wrong. In such case, printing an error message can be helpful: 1 2 3 4 5 6 7 void printDivision ( int x , int y ) { if ( y != 0 ) std :: cout << static_cast < double > ( x ) / y ; else std :: cerr << \"Error: Could not divide by zero \\n \" ; } COPY However, if the calling function is expecting the called function to produce a return value or some useful side-effect, then just ignoring the error may not be an option. \u76f8\u5173\u5185\u5bb9 We discuss std::cerr in lesson 3.4 \u2014 \u57fa\u672c\u4ee3\u7801\u8c03\u8bd5\u6280\u672f \u5c06\u9519\u8bef\u4f20\u9012\u7ed9\u8c03\u7528\u8005 In many cases, the error can\u2019t reasonably be handled in the function that detects the error. For example, consider the following function: 1 2 3 4 double doDivision ( int x , int y ) { return static_cast < double > ( x ) / y ; } COPY If y is 0 , what should we do? We can\u2019t just skip the program logic, because the function needs to return some value. We shouldn\u2019t ask the user to enter a new value for y because this is a calculation function, and introducing input routines into it may or may not be appropriate for the program calling this function. In such cases, the best option can be to pass the error back to the caller in hopes that the caller will be able to deal with it. How might we do that? If the function has a void return type, it can be changed to return a Boolean that indicates success or failure. For example, instead of: 1 2 3 4 5 6 7 void printDivision ( int x , int y ) { if ( y != 0 ) std :: cout << static_cast < double > ( x ) / y ; else std :: cerr << \"Error: Could not divide by zero \\n \" ; } COPY We can do this: 1 2 3 4 5 6 7 8 9 10 11 12 bool printDivision ( int x , int y ) { if ( y == 0 ) { std :: cerr << \"Error: could not divide by zero \\n \" ; return false ; } std :: cout << static_cast < double > ( x ) / y ; return true ; } COPY That way, the caller can check the return value to see if the function failed for some reason. If the function returns a normal value, things are a little more complicated. In some cases, the full range of return values isn\u2019t used. In such cases, we can use a return value that wouldn\u2019t otherwise be possible to occur normally to indicate an error. For example, consider the following function: 1 2 3 4 5 // The reciprocal of x is 1/x double reciprocal ( double x ) { return 1.0 / x ; } COPY The reciprocal of some number x is defined as 1/x , and a number multiplied by its reciprocal equals 1. However, what happens if the user calls this function as reciprocal(0.0) ? We get a divide by zero error and a program crash, so clearly we should protect against this case. But this function must return a double value, so what value should we return? It turns out that this function will never produce 0.0 as a legitimate result, so we can return 0.0 to indicate an error case. 1 2 3 4 5 6 7 8 // The reciprocal of x is 1/x, returns 0.0 if x=0 double reciprocal ( double x ) { if ( x == 0.0 ) return 0.0 ; return 1.0 / x ; } COPY However, if the full range of return values are needed, then using the return value to indicate an error will not be possible (because the caller would not be able to tell whether the return value is a valid value or an error value). In such a case, an out parameter (covered in lesson 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 ) might be a viable choice. \u81f4\u547d\u9519\u8bef If the error is so bad that the program can not continue to operate properly, this is called a non-recoverable error (also called a fatal error). In such cases, the best thing to do is terminate the program. If your code is in main() or a function called directly from main() , the best thing to do is let main() return a non-zero status code. However, if you\u2019re deep in some nested subfunction, it may not be convenient or possible to propagate the error all the way back to main() . In such a case, a halt statement (such as std::exit() ) can be used. For example: 1 2 3 4 5 6 7 8 9 double doDivision ( int x , int y ) { if ( y == 0 ) { std :: cerr << \"Error: Could not divide by zero \\n \" ; std :: exit ( 1 ); } return static_cast < double > ( x ) / y ; } \u5f02\u5e38 Because returning an error from a function back to the caller is complicated (and the many different ways to do so leads to inconsistency, and inconsistency leads to mistakes), C++ offers an entirely separate way to pass errors back to the caller: exceptions . The basic idea is that when an error occurs, an exception is \u201cthrown\u201d. If the current function does not \u201ccatch\u201d the error, the caller of the function has a chance to catch the error. If the caller does not catch the error, the caller\u2019s caller has a chance to catch the error. The error progressively moves up the call stack until it is either caught and handled (at which point execution continues normally), or until main() fails to handle the error (at which point the program is terminated with an exception error). We cover exception handling in chapter 20 of this tutorial series.","title":"7.15 - \u53d1\u73b0\u9519\u8bef\u548c\u5904\u7406\u9519\u8bef"},{"location":"7-15-Detecting-and-handling-errors/#handling-errors-in-functions","text":"Functions may fail for any number of reasons -- the caller may have passed in an argument with an invalid value, or something may fail within the body of the function. For example, a function that opens a file for reading might fail if the file cannot be found. When this happens, you have quite a few options at your disposal. There is no best way to handle an error -- it really depends on the nature of the problem and whether the problem can be fixed or not. There are 4 general strategies that can be used: Handle the error within the function Pass the error back to the caller to deal with Halt the program Throw an exception","title":"Handling errors in functions"},{"location":"7-15-Detecting-and-handling-errors/#handling-the-error-within-the-function","text":"If possible, the best strategy is to recover from the error in the same function in which the error occurred, so that the error can be contained and corrected without impacting any code outside the function. There are two options here: retry until successful, or cancel the operation being executed. If the error has occurred due to something outside of the program\u2019s control, the program can retry until success is achieved. For example, if the program requires an internet connection, and the user has lost their connection, the program may be able to display a warning and then use a loop to periodically recheck for internet connectivity. Alternatively, if the user has entered invalid input, the program can ask the user to try again, and loop until the user is successful at entering valid input. We\u2019ll show examples of handling invalid input and using loops to retry in the next lesson ( 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406 )\u3002 An alternate strategy is just to ignore the error and/or cancel the operation. For example: 1 2 3 4 5 void printDivision ( int x , int y ) { if ( y != 0 ) std :: cout << static_cast < double > ( x ) / y ; } COPY In the above example, if the user passed in an invalid value for y , we just ignore the request to print the result of the division operation. The primary challenge with doing this is that the caller or user have no way to identify that something went wrong. In such case, printing an error message can be helpful: 1 2 3 4 5 6 7 void printDivision ( int x , int y ) { if ( y != 0 ) std :: cout << static_cast < double > ( x ) / y ; else std :: cerr << \"Error: Could not divide by zero \\n \" ; } COPY However, if the calling function is expecting the called function to produce a return value or some useful side-effect, then just ignoring the error may not be an option. \u76f8\u5173\u5185\u5bb9 We discuss std::cerr in lesson 3.4 \u2014 \u57fa\u672c\u4ee3\u7801\u8c03\u8bd5\u6280\u672f","title":"Handling the error within the function"},{"location":"7-15-Detecting-and-handling-errors/#_1","text":"In many cases, the error can\u2019t reasonably be handled in the function that detects the error. For example, consider the following function: 1 2 3 4 double doDivision ( int x , int y ) { return static_cast < double > ( x ) / y ; } COPY If y is 0 , what should we do? We can\u2019t just skip the program logic, because the function needs to return some value. We shouldn\u2019t ask the user to enter a new value for y because this is a calculation function, and introducing input routines into it may or may not be appropriate for the program calling this function. In such cases, the best option can be to pass the error back to the caller in hopes that the caller will be able to deal with it. How might we do that? If the function has a void return type, it can be changed to return a Boolean that indicates success or failure. For example, instead of: 1 2 3 4 5 6 7 void printDivision ( int x , int y ) { if ( y != 0 ) std :: cout << static_cast < double > ( x ) / y ; else std :: cerr << \"Error: Could not divide by zero \\n \" ; } COPY We can do this: 1 2 3 4 5 6 7 8 9 10 11 12 bool printDivision ( int x , int y ) { if ( y == 0 ) { std :: cerr << \"Error: could not divide by zero \\n \" ; return false ; } std :: cout << static_cast < double > ( x ) / y ; return true ; } COPY That way, the caller can check the return value to see if the function failed for some reason. If the function returns a normal value, things are a little more complicated. In some cases, the full range of return values isn\u2019t used. In such cases, we can use a return value that wouldn\u2019t otherwise be possible to occur normally to indicate an error. For example, consider the following function: 1 2 3 4 5 // The reciprocal of x is 1/x double reciprocal ( double x ) { return 1.0 / x ; } COPY The reciprocal of some number x is defined as 1/x , and a number multiplied by its reciprocal equals 1. However, what happens if the user calls this function as reciprocal(0.0) ? We get a divide by zero error and a program crash, so clearly we should protect against this case. But this function must return a double value, so what value should we return? It turns out that this function will never produce 0.0 as a legitimate result, so we can return 0.0 to indicate an error case. 1 2 3 4 5 6 7 8 // The reciprocal of x is 1/x, returns 0.0 if x=0 double reciprocal ( double x ) { if ( x == 0.0 ) return 0.0 ; return 1.0 / x ; } COPY However, if the full range of return values are needed, then using the return value to indicate an error will not be possible (because the caller would not be able to tell whether the return value is a valid value or an error value). In such a case, an out parameter (covered in lesson 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 ) might be a viable choice.","title":"\u5c06\u9519\u8bef\u4f20\u9012\u7ed9\u8c03\u7528\u8005"},{"location":"7-15-Detecting-and-handling-errors/#_2","text":"If the error is so bad that the program can not continue to operate properly, this is called a non-recoverable error (also called a fatal error). In such cases, the best thing to do is terminate the program. If your code is in main() or a function called directly from main() , the best thing to do is let main() return a non-zero status code. However, if you\u2019re deep in some nested subfunction, it may not be convenient or possible to propagate the error all the way back to main() . In such a case, a halt statement (such as std::exit() ) can be used. For example: 1 2 3 4 5 6 7 8 9 double doDivision ( int x , int y ) { if ( y == 0 ) { std :: cerr << \"Error: Could not divide by zero \\n \" ; std :: exit ( 1 ); } return static_cast < double > ( x ) / y ; }","title":"\u81f4\u547d\u9519\u8bef"},{"location":"7-15-Detecting-and-handling-errors/#_3","text":"Because returning an error from a function back to the caller is complicated (and the many different ways to do so leads to inconsistency, and inconsistency leads to mistakes), C++ offers an entirely separate way to pass errors back to the caller: exceptions . The basic idea is that when an error occurs, an exception is \u201cthrown\u201d. If the current function does not \u201ccatch\u201d the error, the caller of the function has a chance to catch the error. If the caller does not catch the error, the caller\u2019s caller has a chance to catch the error. The error progressively moves up the call stack until it is either caught and handled (at which point execution continues normally), or until main() fails to handle the error (at which point the program is terminated with an exception error). We cover exception handling in chapter 20 of this tutorial series.","title":"\u5f02\u5e38"},{"location":"7-16-std-cin-and-handling-invalid-input/","tags":["error handling"],"text":"Key Takeaway Most programs that have a user interface of some kind need to handle user input. In the programs that you have been writing, you have been using std::cin to ask the user to enter text input. Because text input is so free-form (the user can enter anything), it\u2019s very easy for the user to enter input that is not expected. As you write programs, you should always consider how users will (unintentionally or otherwise) misuse your programs. A well-written program will anticipate how users will misuse it, and either handle those cases gracefully or prevent them from happening in the first place (if possible). A program that handles error cases well is said to be robust . In this lesson, we\u2019ll take a look specifically at ways the user can enter invalid text input via std::cin, and show you some different ways to handle those cases. std::cin \u3001\u7f13\u51b2\u548c\u63d0\u53d6\u64cd\u4f5c In order to discuss how std::cin and operator>> can fail, it first helps to know a little bit about how they work. When we use operator>> to get user input and put it into a variable, this is called an \u201cextraction\u201d. The >> operator is accordingly called the extraction operator when used in this context. When the user enters input in response to an extraction operation, that data is placed in a buffer inside of std::cin. A buffer (also called a data buffer) is simply a piece of memory set aside for storing data temporarily while it\u2019s moved from one place to another. In this case, the buffer is used to hold user input while it\u2019s waiting to be extracted to variables. When the extraction operator is used, the following procedure happens: If there is data already in the input buffer, that data is used for extraction. If the input buffer contains no data, the user is asked to input data for extraction (this is the case most of the time). When the user hits enter, a \u2018\\n\u2019 character will be placed in the input buffer. operator>> extracts as much data from the input buffer as it can into the variable (ignoring any leading whitespace characters, such as spaces, tabs, or \u2018\\n\u2019). Any data that can not be extracted is left in the input buffer for the next extraction. Extraction succeeds if at least one character is extracted from the input buffer. Any unextracted input is left in the input buffer for future extractions. For example: 1 2 int x {}; std :: cin >> x ; COPY If the user enters \u201c5a\u201d, 5 will be extracted, converted to an integer, and assigned to variable x. \u201ca\\n\u201d will be left in the input buffer for the next extraction. Extraction fails if the input data does not match the type of the variable being extracted to. For example: 1 2 int x {}; std :: cin >> x ; COPY If the user were to enter \u2018b\u2019, extraction would fail because \u2018b\u2019 can not be extracted to an integer variable. \u8f93\u5165\u9a8c\u8bc1 The process of checking whether user input conforms to what the program is expecting is called input validation . There are three basic ways to do input validation: Inline (as the user types) Prevent the user from typing invalid input in the first place. Post-entry (after the user types) Let the user enter whatever they want into a string, then validate whether the string is correct, and if so, convert the string to the final variable format. Let the user enter whatever they want, let std::cin and operator>> try to extract it, and handle the error cases. Some graphical user interfaces and advanced text interfaces will let you validate input as the user enters it (character by character). Generally speaking, the programmer provides a validation function that accepts the input the user has entered so far, and returns true if the input is valid, and false otherwise. This function is called every time the user presses a key. If the validation function returns true, the key the user just pressed is accepted. If the validation function returns false, the character the user just input is discarded (and not shown on the screen). Using this method, you can ensure that any input the user enters is guaranteed to be valid, because any invalid keystrokes are discovered and discarded immediately. Unfortunately, std::cin does not support this style of validation. Since strings do not have any restrictions on what characters can be entered, extraction is guaranteed to succeed (though remember that std::cin stops extracting at the first non-leading whitespace character). Once a string is entered, the program can then parse the string to see if it is valid or not. However, parsing strings and converting string input to other types (e.g. numbers) can be challenging, so this is only done in rare cases. Most often, we let std::cin and the extraction operator do the hard work. Under this method, we let the user enter whatever they want, have std::cin and operator>> try to extract it, and deal with the fallout if it fails. This is the easiest method, and the one we\u2019ll talk more about below. \u4f8b\u7a0b Consider the following calculator program that has no error handling: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include double getDouble () { std :: cout << \"Enter a double value: \" ; double x {}; std :: cin >> x ; return x ; } char getOperator () { std :: cout << \"Enter one of the following: +, -, *, or /: \" ; char op {}; std :: cin >> op ; return op ; } void printResult ( double x , char operation , double y ) { switch ( operation ) { case '+' : std :: cout << x << \" + \" << y << \" is \" << x + y << '\\n' ; break ; case '-' : std :: cout << x << \" - \" << y << \" is \" << x - y << '\\n' ; break ; case '*' : std :: cout << x << \" * \" << y << \" is \" << x * y << '\\n' ; break ; case '/' : std :: cout << x << \" / \" << y << \" is \" << x / y << '\\n' ; break ; } } int main () { double x { getDouble () }; char operation { getOperator () }; double y { getDouble () }; printResult ( x , operation , y ); return 0 ; } COPY This simple program asks the user to enter two numbers and a mathematical operator. 1 2 3 4 Enter a double value: 5 Enter one of the following: +, -, *, or /: * Enter a double value: 7 5 * 7 is 35 Now, consider where invalid user input might break this program. First, we ask the user to enter some numbers. What if they enter something other than a number (e.g. \u2018q\u2019)? In this case, extraction will fail. Second, we ask the user to enter one of four possible symbols. What if they enter a character other than one of the symbols we\u2019re expecting? We\u2019ll be able to extract the input, but we don\u2019t currently handle what happens afterward. Third, what if we ask the user to enter a symbol and they enter a string like \u201c q hello\u201d. Although we can extract the \u2018 \u2019 character we need, there\u2019s additional input left in the buffer that could cause problems down the road. \u975e\u6cd5\u8f93\u5165\u7684\u7c7b\u578b We can generally separate input text errors into four types: Input extraction succeeds but the input is meaningless to the program (e.g. entering \u2018k\u2019 as your mathematical operator). Input extraction succeeds but the user enters additional input (e.g. entering \u2018*q hello\u2019 as your mathematical operator). Input extraction fails (e.g. trying to enter \u2018q\u2019 into a numeric input). Input extraction succeeds but the user overflows a numeric value. Thus, to make our programs robust, whenever we ask the user for input, we ideally should determine whether each of the above can possibly occur, and if so, write code to handle those cases. Let\u2019s dig into each of these cases, and how to handle them using std::cin. Error case 1: Extraction succeeds but input is meaningless This is the simplest case. Consider the following execution of the above program: 1 2 3 Enter a double value: 5 Enter one of the following: +, -, *, or /: k Enter a double value: 7 In this case, we asked the user to enter one of four symbols, but they entered \u2018k\u2019 instead. \u2018k\u2019 is a valid character, so std::cin happily extracts it to variable op, and this gets returned to main. But our program wasn\u2019t expecting this to happen, so it doesn\u2019t properly deal with this case (and thus never outputs anything). The solution here is simple: do input validation. This usually consists of 3 steps: Check whether the user\u2019s input was what you were expecting. If so, return the value to the caller. If not, tell the user something went wrong and have them try again. Here\u2019s an updated getOperator() function that does input validation. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 char getOperator () { while ( true ) // Loop until user enters a valid input { std :: cout << \"Enter one of the following: +, -, *, or /: \" ; char operation {}; std :: cin >> operation ; // Check whether the user entered meaningful input switch ( operation ) { case '+' : case '-' : case '*' : case '/' : return operation ; // return it to the caller default : // otherwise tell the user what went wrong std :: cerr << \"Oops, that input is invalid. Please try again. \\n \" ; } } // and try again } COPY As you can see, we\u2019re using a while loop to continuously loop until the user provides valid input. If they don\u2019t, we ask them to try again until they either give us valid input, shutdown the program, or destroy their computer. Error case 2: Extraction succeeds but with extraneous input Consider the following execution of the above program: Enter a double value: 5*7 What do you think happens next? Enter a double value: 5 7 Enter one of the following: +, -, , or /: Enter a double value: 5 * 7 is 35 The program prints the right answer, but the formatting is all messed up. Let\u2019s take a closer look at why. When the user enters \u201c5 7\u201d as input, that input goes into the buffer. Then operator>> extracts the 5 to variable x, leaving \u201c 7\\n\u201d in the buffer. Next, the program prints \u201cEnter one of the following: +, -, , or /:\u201d. However, when the extraction operator was called, it sees \u201c 7\\n\u201d waiting in the buffer to be extracted, so it uses that instead of asking the user for more input. Consequently, it extracts the \u2018*\u2019 character, leaving \u201c7\\n\u201d in the buffer. After asking the user to enter another double value, the \u201c7\u201d in the buffer gets extracted without asking the user. Since the user never had an opportunity to enter additional data and hit enter (causing a newline), the output prompts all run together on the same line. Although the above program works, the execution is messy. It would be better if any extraneous characters entered were simply ignored. Fortunately, it\u2019s easy to ignore characters: 1 std :: cin . ignore ( 100 , '\\n' ); // clear up to 100 characters out of the buffer, or until a '\\n' character is removed COPY This call would remove up to 100 characters, but if the user entered more than 100 characters we\u2019ll get messy output again. To ignore all characters up to the next \u2018\\n\u2019, we can pass std::numeric_limits::max() to std::cin.ignore() . std::numeric_limits::max() returns the largest value that can be stored in a variable of type std::streamsize . Passing this value to std::cin.ignore() causes it to disable the count check. To ignore everything up to and including the next \u2018\\n\u2019 character, we call 1 std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); COPY Because this line is quite long for what it does, it\u2019s handy to wrap it in a function which can be called in place of std::cin.ignore() . 1 2 3 4 5 6 #include // for std::numeric_limits void ignoreLine () { std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); } COPY Since the last character the user entered must be a \u2018\\n\u2019, we can tell std::cin to ignore buffered characters until it finds a newline character (which is removed as well). Let\u2019s update our getDouble() function to ignore any extraneous input: 1 2 3 4 5 6 7 8 double getDouble () { std :: cout << \"Enter a double value: \" ; double x {}; std :: cin >> x ; ignoreLine (); return x ; } COPY Now our program will work as expected, even if we enter \u201c5*7\u201d for the first input -- the 5 will be extracted, and the rest of the characters will be removed from the input buffer. Since the input buffer is now empty, the user will be properly asked for input the next time an extraction operation is performed! \u4f5c\u8005\u6ce8 Some lessons still pass 32767 to std::cin.ignore() . This is a magic number with no special meaning to std::cin.ignore() and should be avoided. If you see such an occurrence, feel free to point it out. Error case 3: Extraction fails Now consider the following execution of our updated calculator program: Enter a double value: a You shouldn\u2019t be surprised that the program doesn\u2019t perform as expected, but how it fails is interesting: 1 2 3 4 Enter a double value: a Enter one of the following: +, -, *, or /: Oops, that input is invalid. Please try again. Enter one of the following: +, -, *, or /: Oops, that input is invalid. Please try again. Enter one of the following: +, -, *, or /: Oops, that input is invalid. Please try again. and that last line keeps printing until the program is closed. This looks pretty similar to the extraneous input case, but it\u2019s a little different. Let\u2019s take a closer look. When the user enters \u2018a\u2019, that character is placed in the buffer. Then operator>> tries to extract \u2018a\u2019 to variable x, which is of type double. Since \u2018a\u2019 can\u2019t be converted to a double, operator>> can\u2019t do the extraction. Two things happen at this point: \u2018a\u2019 is left in the buffer, and std::cin goes into \u201cfailure mode\u201d. Once in \u201cfailure mode\u201d, future requests for input extraction will silently fail. Thus in our calculator program, the output prompts still print, but any requests for further extraction are ignored. This means that instead waiting for us to enter an operation, the input prompt is skipped, and we get stuck in an infinite loop because there is no way to reach one of the valid cases. Fortunately, we can detect whether an extraction has failed and fix it: 1 2 3 4 5 6 if ( std :: cin . fail ()) // has a previous extraction failed? { // yep, so let's handle the failure std :: cin . clear (); // put us back in 'normal' operation mode ignoreLine (); // and remove the bad input } COPY Because std::cin has a Boolean conversion indicating whether the last input succeeded, it\u2019s more idiomatic to write the above as following: 1 2 3 4 5 6 if ( ! std :: cin ) // has a previous extraction failed? { // yep, so let's handle the failure std :: cin . clear (); // put us back in 'normal' operation mode ignoreLine (); // and remove the bad input } COPY That\u2019s it! Let\u2019s integrate that into our getDouble() function: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 double getDouble () { while ( true ) // Loop until user enters a valid input { std :: cout << \"Enter a double value: \" ; double x {}; std :: cin >> x ; if ( ! std :: cin ) // has a previous extraction failed? { // yep, so let's handle the failure std :: cin . clear (); // put us back in 'normal' operation mode ignoreLine (); // and remove the bad input } else // else our extraction succeeded { ignoreLine (); return x ; // so return the value we extracted } } } COPY A failed extraction due to invalid input will cause the variable to be zero-initialized. Zero initialization means the variable is set to 0, 0.0, \u201c\u201d, or whatever value 0 converts to for that type. Error case 4: Extraction succeeds but the user overflows a numeric value Consider the following simple example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include int main () { std :: int16_t x {}; // x is 16 bits, holds from -32768 to 32767 std :: cout << \"Enter a number between -32768 and 32767: \" ; std :: cin >> x ; std :: int16_t y {}; // y is 16 bits, holds from -32768 to 32767 std :: cout << \"Enter another number between -32768 and 32767: \" ; std :: cin >> y ; std :: cout << \"The sum is: \" << x + y << '\\n' ; return 0 ; } COPY What happens if the user enters a number that is too large (e.g. 40000)? Enter a number between -32768 and 32767: 40000 Enter another number between -32768 and 32767: The sum is: 32767 In the above case, std::cin goes immediately into \u201cfailure mode\u201d, but also assigns the closest in-range value to the variable. Consequently, x is left with the assigned value of 32767. Additional inputs are skipped, leaving y with the initialized value of 0. We can handle this kind of error in the same way as a failed extraction. A failed extraction due to invalid input will cause the variable to be zero-initialized. Zero initialization means the variable is set to 0, 0.0, \u201c\u201d, or whatever value 0 converts to for that type. Putting it all together Here\u2019s our example calculator, updated with a few additional bits of error checking: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 #include #include void ignoreLine () { std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); } double getDouble () { while ( true ) // Loop until user enters a valid input { std :: cout << \"Enter a double value: \" ; double x {}; std :: cin >> x ; // Check for failed extraction if ( ! std :: cin ) // has a previous extraction failed? { // yep, so let's handle the failure std :: cin . clear (); // put us back in 'normal' operation mode ignoreLine (); // and remove the bad input std :: cerr << \"Oops, that input is invalid. Please try again. \\n \" ; } else { ignoreLine (); // remove any extraneous input return x ; } } } char getOperator () { while ( true ) // Loop until user enters a valid input { std :: cout << \"Enter one of the following: +, -, *, or /: \" ; char operation {}; std :: cin >> operation ; ignoreLine (); // // remove any extraneous input // Check whether the user entered meaningful input switch ( operation ) { case '+' : case '-' : case '*' : case '/' : return operation ; // return it to the caller default : // otherwise tell the user what went wrong std :: cerr << \"Oops, that input is invalid. Please try again. \\n \" ; } } // and try again } void printResult ( double x , char operation , double y ) { switch ( operation ) { case '+' : std :: cout << x << \" + \" << y << \" is \" << x + y << '\\n' ; break ; case '-' : std :: cout << x << \" - \" << y << \" is \" << x - y << '\\n' ; break ; case '*' : std :: cout << x << \" * \" << y << \" is \" << x * y << '\\n' ; break ; case '/' : std :: cout << x << \" / \" << y << \" is \" << x / y << '\\n' ; break ; default : // Being robust means handling unexpected parameters as well, even though getOperator() guarantees operation is valid in this particular program std :: cerr << \"Something went wrong: printResult() got an invalid operator. \\n \" ; } } int main () { double x { getDouble () }; char operation { getOperator () }; double y { getDouble () }; printResult ( x , operation , y ); return 0 ; } \u5c0f\u7ed3 As you write your programs, consider how users will misuse your program, especially around text input. For each point of text input, consider: Could extraction fail? Could the user enter more input than expected? Could the user enter meaningless input? Could the user overflow an input? You can use if statements and boolean logic to test whether input is expected and meaningful. The following code will clear any extraneous input: 1 std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); The following code will test for and fix failed extractions or overflow: 1 2 3 4 5 6 if ( ! std :: cin ) // has a previous extraction failed or overflowed? { // yep, so let's handle the failure std :: cin . clear (); // put us back in 'normal' operation mode std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // and remove the bad input } Finally, use loops to ask the user to re-enter input if the original input was invalid. \u4f5c\u8005\u6ce8 Input validation is important and useful, but it also tends to make examples more complicated and harder to follow. Accordingly, in future lessons, we will generally not do any kind of input validation unless it\u2019s relevant to something we\u2019re trying to teach.","title":"7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406"},{"location":"7-16-std-cin-and-handling-invalid-input/#stdcin","text":"In order to discuss how std::cin and operator>> can fail, it first helps to know a little bit about how they work. When we use operator>> to get user input and put it into a variable, this is called an \u201cextraction\u201d. The >> operator is accordingly called the extraction operator when used in this context. When the user enters input in response to an extraction operation, that data is placed in a buffer inside of std::cin. A buffer (also called a data buffer) is simply a piece of memory set aside for storing data temporarily while it\u2019s moved from one place to another. In this case, the buffer is used to hold user input while it\u2019s waiting to be extracted to variables. When the extraction operator is used, the following procedure happens: If there is data already in the input buffer, that data is used for extraction. If the input buffer contains no data, the user is asked to input data for extraction (this is the case most of the time). When the user hits enter, a \u2018\\n\u2019 character will be placed in the input buffer. operator>> extracts as much data from the input buffer as it can into the variable (ignoring any leading whitespace characters, such as spaces, tabs, or \u2018\\n\u2019). Any data that can not be extracted is left in the input buffer for the next extraction. Extraction succeeds if at least one character is extracted from the input buffer. Any unextracted input is left in the input buffer for future extractions. For example: 1 2 int x {}; std :: cin >> x ; COPY If the user enters \u201c5a\u201d, 5 will be extracted, converted to an integer, and assigned to variable x. \u201ca\\n\u201d will be left in the input buffer for the next extraction. Extraction fails if the input data does not match the type of the variable being extracted to. For example: 1 2 int x {}; std :: cin >> x ; COPY If the user were to enter \u2018b\u2019, extraction would fail because \u2018b\u2019 can not be extracted to an integer variable.","title":"std::cin\u3001\u7f13\u51b2\u548c\u63d0\u53d6\u64cd\u4f5c"},{"location":"7-16-std-cin-and-handling-invalid-input/#_1","text":"The process of checking whether user input conforms to what the program is expecting is called input validation . There are three basic ways to do input validation: Inline (as the user types) Prevent the user from typing invalid input in the first place. Post-entry (after the user types) Let the user enter whatever they want into a string, then validate whether the string is correct, and if so, convert the string to the final variable format. Let the user enter whatever they want, let std::cin and operator>> try to extract it, and handle the error cases. Some graphical user interfaces and advanced text interfaces will let you validate input as the user enters it (character by character). Generally speaking, the programmer provides a validation function that accepts the input the user has entered so far, and returns true if the input is valid, and false otherwise. This function is called every time the user presses a key. If the validation function returns true, the key the user just pressed is accepted. If the validation function returns false, the character the user just input is discarded (and not shown on the screen). Using this method, you can ensure that any input the user enters is guaranteed to be valid, because any invalid keystrokes are discovered and discarded immediately. Unfortunately, std::cin does not support this style of validation. Since strings do not have any restrictions on what characters can be entered, extraction is guaranteed to succeed (though remember that std::cin stops extracting at the first non-leading whitespace character). Once a string is entered, the program can then parse the string to see if it is valid or not. However, parsing strings and converting string input to other types (e.g. numbers) can be challenging, so this is only done in rare cases. Most often, we let std::cin and the extraction operator do the hard work. Under this method, we let the user enter whatever they want, have std::cin and operator>> try to extract it, and deal with the fallout if it fails. This is the easiest method, and the one we\u2019ll talk more about below.","title":"\u8f93\u5165\u9a8c\u8bc1"},{"location":"7-16-std-cin-and-handling-invalid-input/#_2","text":"Consider the following calculator program that has no error handling: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include double getDouble () { std :: cout << \"Enter a double value: \" ; double x {}; std :: cin >> x ; return x ; } char getOperator () { std :: cout << \"Enter one of the following: +, -, *, or /: \" ; char op {}; std :: cin >> op ; return op ; } void printResult ( double x , char operation , double y ) { switch ( operation ) { case '+' : std :: cout << x << \" + \" << y << \" is \" << x + y << '\\n' ; break ; case '-' : std :: cout << x << \" - \" << y << \" is \" << x - y << '\\n' ; break ; case '*' : std :: cout << x << \" * \" << y << \" is \" << x * y << '\\n' ; break ; case '/' : std :: cout << x << \" / \" << y << \" is \" << x / y << '\\n' ; break ; } } int main () { double x { getDouble () }; char operation { getOperator () }; double y { getDouble () }; printResult ( x , operation , y ); return 0 ; } COPY This simple program asks the user to enter two numbers and a mathematical operator. 1 2 3 4 Enter a double value: 5 Enter one of the following: +, -, *, or /: * Enter a double value: 7 5 * 7 is 35 Now, consider where invalid user input might break this program. First, we ask the user to enter some numbers. What if they enter something other than a number (e.g. \u2018q\u2019)? In this case, extraction will fail. Second, we ask the user to enter one of four possible symbols. What if they enter a character other than one of the symbols we\u2019re expecting? We\u2019ll be able to extract the input, but we don\u2019t currently handle what happens afterward. Third, what if we ask the user to enter a symbol and they enter a string like \u201c q hello\u201d. Although we can extract the \u2018 \u2019 character we need, there\u2019s additional input left in the buffer that could cause problems down the road.","title":"\u4f8b\u7a0b"},{"location":"7-16-std-cin-and-handling-invalid-input/#_3","text":"We can generally separate input text errors into four types: Input extraction succeeds but the input is meaningless to the program (e.g. entering \u2018k\u2019 as your mathematical operator). Input extraction succeeds but the user enters additional input (e.g. entering \u2018*q hello\u2019 as your mathematical operator). Input extraction fails (e.g. trying to enter \u2018q\u2019 into a numeric input). Input extraction succeeds but the user overflows a numeric value. Thus, to make our programs robust, whenever we ask the user for input, we ideally should determine whether each of the above can possibly occur, and if so, write code to handle those cases. Let\u2019s dig into each of these cases, and how to handle them using std::cin.","title":"\u975e\u6cd5\u8f93\u5165\u7684\u7c7b\u578b"},{"location":"7-16-std-cin-and-handling-invalid-input/#error-case-1-extraction-succeeds-but-input-is-meaningless","text":"This is the simplest case. Consider the following execution of the above program: 1 2 3 Enter a double value: 5 Enter one of the following: +, -, *, or /: k Enter a double value: 7 In this case, we asked the user to enter one of four symbols, but they entered \u2018k\u2019 instead. \u2018k\u2019 is a valid character, so std::cin happily extracts it to variable op, and this gets returned to main. But our program wasn\u2019t expecting this to happen, so it doesn\u2019t properly deal with this case (and thus never outputs anything). The solution here is simple: do input validation. This usually consists of 3 steps: Check whether the user\u2019s input was what you were expecting. If so, return the value to the caller. If not, tell the user something went wrong and have them try again. Here\u2019s an updated getOperator() function that does input validation. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 char getOperator () { while ( true ) // Loop until user enters a valid input { std :: cout << \"Enter one of the following: +, -, *, or /: \" ; char operation {}; std :: cin >> operation ; // Check whether the user entered meaningful input switch ( operation ) { case '+' : case '-' : case '*' : case '/' : return operation ; // return it to the caller default : // otherwise tell the user what went wrong std :: cerr << \"Oops, that input is invalid. Please try again. \\n \" ; } } // and try again } COPY As you can see, we\u2019re using a while loop to continuously loop until the user provides valid input. If they don\u2019t, we ask them to try again until they either give us valid input, shutdown the program, or destroy their computer.","title":"Error case 1: Extraction succeeds but input is meaningless"},{"location":"7-16-std-cin-and-handling-invalid-input/#error-case-2-extraction-succeeds-but-with-extraneous-input","text":"Consider the following execution of the above program: Enter a double value: 5*7 What do you think happens next? Enter a double value: 5 7 Enter one of the following: +, -, , or /: Enter a double value: 5 * 7 is 35 The program prints the right answer, but the formatting is all messed up. Let\u2019s take a closer look at why. When the user enters \u201c5 7\u201d as input, that input goes into the buffer. Then operator>> extracts the 5 to variable x, leaving \u201c 7\\n\u201d in the buffer. Next, the program prints \u201cEnter one of the following: +, -, , or /:\u201d. However, when the extraction operator was called, it sees \u201c 7\\n\u201d waiting in the buffer to be extracted, so it uses that instead of asking the user for more input. Consequently, it extracts the \u2018*\u2019 character, leaving \u201c7\\n\u201d in the buffer. After asking the user to enter another double value, the \u201c7\u201d in the buffer gets extracted without asking the user. Since the user never had an opportunity to enter additional data and hit enter (causing a newline), the output prompts all run together on the same line. Although the above program works, the execution is messy. It would be better if any extraneous characters entered were simply ignored. Fortunately, it\u2019s easy to ignore characters: 1 std :: cin . ignore ( 100 , '\\n' ); // clear up to 100 characters out of the buffer, or until a '\\n' character is removed COPY This call would remove up to 100 characters, but if the user entered more than 100 characters we\u2019ll get messy output again. To ignore all characters up to the next \u2018\\n\u2019, we can pass std::numeric_limits::max() to std::cin.ignore() . std::numeric_limits::max() returns the largest value that can be stored in a variable of type std::streamsize . Passing this value to std::cin.ignore() causes it to disable the count check. To ignore everything up to and including the next \u2018\\n\u2019 character, we call 1 std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); COPY Because this line is quite long for what it does, it\u2019s handy to wrap it in a function which can be called in place of std::cin.ignore() . 1 2 3 4 5 6 #include // for std::numeric_limits void ignoreLine () { std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); } COPY Since the last character the user entered must be a \u2018\\n\u2019, we can tell std::cin to ignore buffered characters until it finds a newline character (which is removed as well). Let\u2019s update our getDouble() function to ignore any extraneous input: 1 2 3 4 5 6 7 8 double getDouble () { std :: cout << \"Enter a double value: \" ; double x {}; std :: cin >> x ; ignoreLine (); return x ; } COPY Now our program will work as expected, even if we enter \u201c5*7\u201d for the first input -- the 5 will be extracted, and the rest of the characters will be removed from the input buffer. Since the input buffer is now empty, the user will be properly asked for input the next time an extraction operation is performed! \u4f5c\u8005\u6ce8 Some lessons still pass 32767 to std::cin.ignore() . This is a magic number with no special meaning to std::cin.ignore() and should be avoided. If you see such an occurrence, feel free to point it out.","title":"Error case 2: Extraction succeeds but with extraneous input"},{"location":"7-16-std-cin-and-handling-invalid-input/#error-case-3-extraction-fails","text":"Now consider the following execution of our updated calculator program: Enter a double value: a You shouldn\u2019t be surprised that the program doesn\u2019t perform as expected, but how it fails is interesting: 1 2 3 4 Enter a double value: a Enter one of the following: +, -, *, or /: Oops, that input is invalid. Please try again. Enter one of the following: +, -, *, or /: Oops, that input is invalid. Please try again. Enter one of the following: +, -, *, or /: Oops, that input is invalid. Please try again. and that last line keeps printing until the program is closed. This looks pretty similar to the extraneous input case, but it\u2019s a little different. Let\u2019s take a closer look. When the user enters \u2018a\u2019, that character is placed in the buffer. Then operator>> tries to extract \u2018a\u2019 to variable x, which is of type double. Since \u2018a\u2019 can\u2019t be converted to a double, operator>> can\u2019t do the extraction. Two things happen at this point: \u2018a\u2019 is left in the buffer, and std::cin goes into \u201cfailure mode\u201d. Once in \u201cfailure mode\u201d, future requests for input extraction will silently fail. Thus in our calculator program, the output prompts still print, but any requests for further extraction are ignored. This means that instead waiting for us to enter an operation, the input prompt is skipped, and we get stuck in an infinite loop because there is no way to reach one of the valid cases. Fortunately, we can detect whether an extraction has failed and fix it: 1 2 3 4 5 6 if ( std :: cin . fail ()) // has a previous extraction failed? { // yep, so let's handle the failure std :: cin . clear (); // put us back in 'normal' operation mode ignoreLine (); // and remove the bad input } COPY Because std::cin has a Boolean conversion indicating whether the last input succeeded, it\u2019s more idiomatic to write the above as following: 1 2 3 4 5 6 if ( ! std :: cin ) // has a previous extraction failed? { // yep, so let's handle the failure std :: cin . clear (); // put us back in 'normal' operation mode ignoreLine (); // and remove the bad input } COPY That\u2019s it! Let\u2019s integrate that into our getDouble() function: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 double getDouble () { while ( true ) // Loop until user enters a valid input { std :: cout << \"Enter a double value: \" ; double x {}; std :: cin >> x ; if ( ! std :: cin ) // has a previous extraction failed? { // yep, so let's handle the failure std :: cin . clear (); // put us back in 'normal' operation mode ignoreLine (); // and remove the bad input } else // else our extraction succeeded { ignoreLine (); return x ; // so return the value we extracted } } } COPY A failed extraction due to invalid input will cause the variable to be zero-initialized. Zero initialization means the variable is set to 0, 0.0, \u201c\u201d, or whatever value 0 converts to for that type.","title":"Error case 3: Extraction fails"},{"location":"7-16-std-cin-and-handling-invalid-input/#error-case-4-extraction-succeeds-but-the-user-overflows-a-numeric-value","text":"Consider the following simple example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include int main () { std :: int16_t x {}; // x is 16 bits, holds from -32768 to 32767 std :: cout << \"Enter a number between -32768 and 32767: \" ; std :: cin >> x ; std :: int16_t y {}; // y is 16 bits, holds from -32768 to 32767 std :: cout << \"Enter another number between -32768 and 32767: \" ; std :: cin >> y ; std :: cout << \"The sum is: \" << x + y << '\\n' ; return 0 ; } COPY What happens if the user enters a number that is too large (e.g. 40000)? Enter a number between -32768 and 32767: 40000 Enter another number between -32768 and 32767: The sum is: 32767 In the above case, std::cin goes immediately into \u201cfailure mode\u201d, but also assigns the closest in-range value to the variable. Consequently, x is left with the assigned value of 32767. Additional inputs are skipped, leaving y with the initialized value of 0. We can handle this kind of error in the same way as a failed extraction. A failed extraction due to invalid input will cause the variable to be zero-initialized. Zero initialization means the variable is set to 0, 0.0, \u201c\u201d, or whatever value 0 converts to for that type.","title":"Error case 4: Extraction succeeds but the user overflows a numeric value"},{"location":"7-16-std-cin-and-handling-invalid-input/#putting-it-all-together","text":"Here\u2019s our example calculator, updated with a few additional bits of error checking: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 #include #include void ignoreLine () { std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); } double getDouble () { while ( true ) // Loop until user enters a valid input { std :: cout << \"Enter a double value: \" ; double x {}; std :: cin >> x ; // Check for failed extraction if ( ! std :: cin ) // has a previous extraction failed? { // yep, so let's handle the failure std :: cin . clear (); // put us back in 'normal' operation mode ignoreLine (); // and remove the bad input std :: cerr << \"Oops, that input is invalid. Please try again. \\n \" ; } else { ignoreLine (); // remove any extraneous input return x ; } } } char getOperator () { while ( true ) // Loop until user enters a valid input { std :: cout << \"Enter one of the following: +, -, *, or /: \" ; char operation {}; std :: cin >> operation ; ignoreLine (); // // remove any extraneous input // Check whether the user entered meaningful input switch ( operation ) { case '+' : case '-' : case '*' : case '/' : return operation ; // return it to the caller default : // otherwise tell the user what went wrong std :: cerr << \"Oops, that input is invalid. Please try again. \\n \" ; } } // and try again } void printResult ( double x , char operation , double y ) { switch ( operation ) { case '+' : std :: cout << x << \" + \" << y << \" is \" << x + y << '\\n' ; break ; case '-' : std :: cout << x << \" - \" << y << \" is \" << x - y << '\\n' ; break ; case '*' : std :: cout << x << \" * \" << y << \" is \" << x * y << '\\n' ; break ; case '/' : std :: cout << x << \" / \" << y << \" is \" << x / y << '\\n' ; break ; default : // Being robust means handling unexpected parameters as well, even though getOperator() guarantees operation is valid in this particular program std :: cerr << \"Something went wrong: printResult() got an invalid operator. \\n \" ; } } int main () { double x { getDouble () }; char operation { getOperator () }; double y { getDouble () }; printResult ( x , operation , y ); return 0 ; }","title":"Putting it all together"},{"location":"7-16-std-cin-and-handling-invalid-input/#_4","text":"As you write your programs, consider how users will misuse your program, especially around text input. For each point of text input, consider: Could extraction fail? Could the user enter more input than expected? Could the user enter meaningless input? Could the user overflow an input? You can use if statements and boolean logic to test whether input is expected and meaningful. The following code will clear any extraneous input: 1 std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); The following code will test for and fix failed extractions or overflow: 1 2 3 4 5 6 if ( ! std :: cin ) // has a previous extraction failed or overflowed? { // yep, so let's handle the failure std :: cin . clear (); // put us back in 'normal' operation mode std :: cin . ignore ( std :: numeric_limits < std :: streamsize >:: max (), '\\n' ); // and remove the bad input } Finally, use loops to ask the user to re-enter input if the original input was invalid. \u4f5c\u8005\u6ce8 Input validation is important and useful, but it also tends to make examples more complicated and harder to follow. Accordingly, in future lessons, we will generally not do any kind of input validation unless it\u2019s relevant to something we\u2019re trying to teach.","title":"\u5c0f\u7ed3"},{"location":"7-17-assert-and-static-assert/","tags":["assert","static_assert","C++17"],"text":"In a function that takes parameters, the caller may be able to pass in arguments that are syntactically valid but semantically meaningless. For example, in the previous lesson ( 7.15 - \u53d1\u73b0\u9519\u8bef\u548c\u5904\u7406\u9519\u8bef ), we showed the following sample function: 1 2 3 4 5 6 7 void printDivision ( int x , int y ) { if ( y != 0 ) std :: cout << static_cast < double > ( x ) / y ; else std :: cerr << \"Error: Could not divide by zero \\n \" ; } This function does an explicit check to see if y is 0 , since dividing by zero is a semantic error and will cause the program to crash if executed. In the prior lesson, we discussed a couple of ways to deal with such problems, including halting the program, or skipping the offending statements. Both of those options are problematic though. If a program skips statements due to an error, then it is essentially failing silently. Especially while we are writing and debugging programs, silent failures are bad, because they obscure real problems. Even if we print an error message, that error message may be lost among the other program output, and it may be non-obvious where the error message is being generated or how the conditions that triggered the error message occurred. Some functions may be called tens or hundreds of times, and if only one of those cases is generating a problem, it can be hard to know which one. If the program terminates (via std::exit ) then we will have lost our call stack and any debugging information that might help us isolate the problem. std::abort is a better option for such cases, as typically the developer will be given the option to start debugging at the point where the program aborted. \u524d\u7f6e\u6761\u4ef6\u3001\u4e0d\u53d8\u91cf\u548c\u540e\u7f6e\u6761\u4ef6 In programming, a precondition is any condition that must always be true prior to the execution of component of code. Our check of y is a precondition that ensures y has a valid value before the function continues. It\u2019s more common for functions with preconditions to be written like this: 1 2 3 4 5 6 7 8 9 10 void printDivision ( int x , int y ) { if ( y == 0 ) { std :: cerr << \"Error: Could not divide by zero \\n \" ; return ; } std :: cout << static_cast < double > ( x ) / y ; } COPY An invariant is a condition that must be true while some component is executing. Similarly, a postcondition is something that must be true after the execution of some component of code. Our function doesn\u2019t have any postconditions. \u65ad\u8a00 Using a conditional statement to detect an invalid parameter (or to validate some other kind of assumption), along with printing an error message and terminating the program, is such a common response to problems that C++ provides a shortcut method for doing this. An assertion is an expression that will be true unless there is a bug in the program. If the expression evaluates to true , the assertion statement does nothing. If the conditional expression evaluates to false , an error message is displayed and the program is terminated (via std::abort ). This error message typically contains the expression that failed as text, along with the name of the code file and the line number of the assertion. This makes it very easy to tell not only what the problem was, but where in the code the problem occurred. This can help with debugging efforts immensely. In C++, runtime assertions are implemented via the assert preprocessor macro, which lives in the header. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include // for assert() #include // for std::sqrt #include double calculateTimeUntilObjectHitsGround ( double initialHeight , double gravity ) { assert ( gravity > 0.0 ); // The object won't reach the ground unless there is positive gravity. if ( initialHeight <= 0.0 ) { // The object is already on the ground. Or buried. return 0.0 ; } return std :: sqrt (( 2.0 * initialHeight ) / gravity ); } int main () { std :: cout << \"Took \" << calculateTimeUntilObjectHitsGround ( 100.0 , -9.8 ) << \" second(s) \\n \" ; return 0 ; } COPY When the program calls calculateTimeUntilObjectHitsGround(100.0, -9.8) , assert(gravity > 0.0) will evaluate to false , which will trigger the assert. That will print a message similar to this: 1 dropsimulator: src/main.cpp:6: double calculateTimeUntilObjectHitsGround(double, double): Assertion 'gravity > 0.0' failed. The actual message varies depending on which compiler you use. Although asserts are most often used to validate function parameters, they can be used anywhere you would like to validate that something is true. Although we told you previously to avoid preprocessor macros, asserts are one of the few preprocessor macros that are considered acceptable to use. We encourage you to use assert statements liberally throughout your code. \u4f7f\u65ad\u8a00\u8bed\u53e5\u66f4\u5177\u63cf\u8ff0\u6027 Sometimes assert expressions aren\u2019t very descriptive. Consider the following statement: 1 assert ( found ); COPY If this assert is triggered, the assert will say: 1 Assertion failed: found, file C:\\\\VCProjects\\\\Test.cpp, line 34 What does this even mean? Clearly found was false (since the assert triggered), but what wasn\u2019t found? You\u2019d have to go look at the code to determine that. Fortunately, there\u2019s a little trick you can use to make your assert statements more descriptive. Simply add a string literal joined by a logical AND: 1 assert ( found && \"Car could not be found in database\" ); COPY Here\u2019s why this works: A string literal always evaluates to Boolean true . So if found is false , false && true is false . If found is true , true && true is true . Thus, logical AND-ing a string literal doesn\u2019t impact the evaluation of the assert. However, when the assert triggers, the string literal will be included in the assert message: Assertion failed: found && \"Car could not be found in database\", file C:\\VCProjects\\Test.cpp, line 34 That gives you some additional context as to what went wrong. \u65ad\u8a00 vs \u9519\u8bef\u5904\u7406 Assertions and error handling are similar enough that their purposes can be confused, so let\u2019s clarify: The goal of an assertion is to catch programming errors by documenting something that should never happen. If that thing does happen, then the programmer made an error somewhere, and that error can be identified and fixed. Assertions do not allow recovery from errors (after all, if something should never happen, there\u2019s no need to recover from it), and the program will not produce a friendly error message. On the other hand, error handling is designed to gracefully handle cases that could happen (however rarely) in release configurations. These may or may not be recoverable, but one should always assume a user of the program may encounter them. \u6700\u4f73\u5b9e\u8df5 Use assertions to document cases that should be logically impossible. Assertions are also sometimes used to document cases that were not implemented because they were not needed at the time the programmer wrote the code: 1 2 // Assert with a message, covered in the next section assert ( moved && \"Need to handle case where student was just moved to another classroom\" ); COPY That way, if a future user of the code does encounter a situation where this case is needed, the code will fail with a useful error message, and the programmer can then determine how to implement that case. NDEBUG The assert macro comes with a small performance cost that is incurred each time the assert condition is checked. Furthermore, asserts should (ideally) never be encountered in production code (because your code should already be thoroughly tested). Consequently, many developers prefer that asserts are only active in debug builds. C++ comes with a way to turn off asserts in production code. If the macro NDEBUG is defined, the assert macro gets disabled. Some IDEs set NDEBUG by default as part of the project settings for release configurations. For example, in Visual Studio, the following preprocessor definitions are set at the project level: WIN32;NDEBUG;_CONSOLE . If you\u2019re using Visual Studio and want your asserts to trigger in release builds, you\u2019ll need to remove NDEBUG from this setting. If you\u2019re using an IDE or build system that doesn\u2019t automatically define NDEBUG in release configuration, you will need to add it in the project or compilation settings manually. \u65ad\u8a00\u7684\u9650\u5236\u548c\u63d0\u9192 There are a few pitfalls and limitations to asserts. First, the assert itself can have a bug. If this happens, the assert will either report an error where none exists, or fail to report a bug where one does exist. Second, your asserts should have no side effects -- that is, the program should run the same with and without the assert. Otherwise, what you are testing in a debug configuration will not be the same as in a release configuration (assuming you ship with NDEBUG). Also note that the abort() function terminates the program immediately, without a chance to do any further cleanup (e.g. close a file or database). Because of this, asserts should be used only in cases where corruption isn\u2019t likely to occur if the program terminates unexpectedly. static_assert C++ also has another type of assert called static_assert . A static_assert is an assertion that is checked at compile-time rather than at runtime, with a failing static_assert causing a compile error. Unlike assert, which is declared in the header, static_assert is a keyword, so no header needs to be included to use it. A static_assert takes the following form: 1 static_assert ( condition , diagnostic_message ) If the condition is not true, the diagnostic message is printed. Here\u2019s an example of using static_assert to ensure types have a certain size: 1 2 3 4 5 6 7 static_assert ( sizeof ( long ) == 8 , \"long must be 8 bytes\" ); static_assert ( sizeof ( int ) == 4 , \"int must be 4 bytes\" ); int main () { return 0 ; } COPY On the author\u2019s machine, when compiled, the compiler errors: 1 1>c:\\consoleapplication1\\main.cpp(19): error C2338: long must be 8 bytes Because static_assert is evaluated by the compiler, the condition must be able to be evaluated at compile time. Also, unlike normal assert (which is evaluated at runtime), static_assert can be placed anywhere in the code file (even in the global namespace). Prior to C++17, the diagnostic message must be supplied as the second parameter. Since C++17, providing a diagnostic message is optional. \u5728\u63a5\u6536\u53c2\u6570\u7684\u51fd\u6570\u4e2d\uff0c\u8c03\u7528\u8005\u53ef\u80fd\u4f1a\u4f20\u9012\u8bed\u6cd5\u4e0a\u6709\u6548\u4f46\u8bed\u4e49\u4e0a\u65e0\u610f\u4e49\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u5728\u4e0a\u4e00\u8bfe\u4e2d\uff08 7.15 - \u53d1\u73b0\u9519\u8bef\u548c\u5904\u7406\u9519\u8bef \uff09\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u4ee5\u4e0b\u793a\u4f8b\u51fd\u6570\uff1a 1 2 3 4 5 6 7 void printDivision ( int x , int y ) { if ( y != 0 ) std :: cout << static_cast ( x ) / y ; else std :: cerr << \"Error: Could not divide by zero \\n \" ; } \u8be5\u51fd\u6570\u663e\u5f0f\u68c0\u67e5 y \u662f\u5426\u4e3a 0 \uff0c\u56e0\u4e3a\u9664\u4ee5\u96f6\u662f\u8bed\u4e49\u9519\u8bef\uff0c\u5982\u679c\u6267\u884c\u5c06\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002 \u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u5904\u7406\u6b64\u7c7b\u95ee\u9898\u7684\u51e0\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u505c\u6b62\u7a0b\u5e8f\u6216\u8df3\u8fc7\u6709\u95ee\u9898\u7684\u8bed\u53e5\u3002 \u4f46\u8fd9\u4e24\u79cd\u9009\u9879\u90fd\u6709\u95ee\u9898\u3002\u5982\u679c\u7a0b\u5e8f\u7531\u4e8e\u9519\u8bef\u800c\u8df3\u8fc7\u8bed\u53e5\uff0c\u5219\u5b9e\u9645\u4e0a\u662f\u5728\u9ed8\u9ed8\u5730\u5931\u8d25\u3002\u5c24\u5176\u662f\u5728\u7f16\u5199\u548c\u8c03\u8bd5\u7a0b\u5e8f\u65f6\uff0c\u9759\u9ed8\u5931\u8d25\u662f\u4e0d\u597d\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u4f1a\u63a9\u76d6\u771f\u6b63\u7684\u95ee\u9898\u3002\u5373\u4f7f\u6211\u4eec\u6253\u5370\u9519\u8bef\u6d88\u606f\uff0c\u8be5\u9519\u8bef\u6d88\u606f\u4e5f\u53ef\u80fd\u4f1a\u5728\u5176\u4ed6\u7a0b\u5e8f\u8f93\u51fa\u4e2d\u4e22\u5931\uff0c\u5e76\u4e14\u53ef\u80fd\u4e0d\u660e\u663e\u5730\u751f\u6210\u9519\u8bef\u6d88\u606f\u6216\u89e6\u53d1\u9519\u8bef\u6d88\u606f\u7684\u6761\u4ef6\u5728\u54ea\u91cc\u3002\u6709\u4e9b\u51fd\u6570\u53ef\u80fd\u4f1a\u88ab\u8c03\u7528\u5341\u6b21\u6216\u51e0\u767e\u6b21\uff0c\u5982\u679c\u53ea\u6709\u5176\u4e2d\u4e00\u4e2a\u60c5\u51b5\u751f\u6210\u95ee\u9898\uff0c\u90a3\u4e48\u5f88\u96be\u77e5\u9053\u662f\u54ea\u4e2a\u60c5\u51b5\u3002 \u5982\u679c\u7a0b\u5e8f\u7ec8\u6b62\uff08\u901a\u8fc7 std::exit \uff09\uff0c\u5219\u6211\u4eec\u5c06\u5931\u53bb\u8c03\u7528\u5806\u6808\u548c\u4efb\u4f55\u53ef\u80fd\u5e2e\u52a9\u6211\u4eec\u9694\u79bb\u95ee\u9898\u7684\u8c03\u8bd5\u4fe1\u606f\u3002 std::abort \u662f\u8fd9\u79cd\u60c5\u51b5\u7684\u66f4\u597d\u9009\u62e9\uff0c\u56e0\u4e3a\u901a\u5e38\u5f00\u53d1\u4eba\u5458\u5c06\u6709\u9009\u62e9\u5728\u7a0b\u5e8f\u4e2d\u6b62\u7684\u70b9\u5f00\u59cb\u8c03\u8bd5\u3002 \u524d\u7f6e\u6761\u4ef6\u3001\u4e0d\u53d8\u91cf\u548c\u540e\u7f6e\u6761\u4ef6 \u5728\u7f16\u7a0b\u4e2d\uff0c\u524d\u7f6e\u6761\u4ef6\u662f\u6307\u5728\u4ee3\u7801\u7ec4\u4ef6\u6267\u884c\u4e4b\u524d\u5fc5\u987b\u59cb\u7ec8\u4e3a\u771f\u7684\u4efb\u4f55\u6761\u4ef6\u3002\u6211\u4eec\u5bf9 y \u7684\u68c0\u67e5\u662f\u786e\u4fdd\u51fd\u6570\u7ee7\u7eed\u4e4b\u524d y \u5177\u6709\u6709\u6548\u503c\u7684\u524d\u7f6e\u6761\u4ef6\u3002 \u66f4\u5e38\u89c1\u7684\u662f\uff0c\u5177\u6709\u524d\u7f6e\u6761\u4ef6\u7684\u51fd\u6570\u7f16\u5199\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 void printDivision ( int x , int y ) { if ( y == 0 ) { std :: cerr << \"Error: Could not divide by zero \\n \" ; return ; } std :: cout << static_cast ( x ) / y ; } \u4e0d\u53d8\u91cf\u662f\u6307\u67d0\u4e2a\u7ec4\u4ef6\u6267\u884c\u65f6\u5fc5\u987b\u4e3a\u771f\u7684\u6761\u4ef6\u3002 \u7c7b\u4f3c\u5730\uff0c\u540e\u7f6e\u6761\u4ef6\u662f\u6307\u67d0\u4e2a\u4ee3\u7801\u7ec4\u4ef6\u6267\u884c\u540e\u5fc5\u987b\u4e3a\u771f\u7684\u6761\u4ef6\u3002\u6211\u4eec\u7684\u51fd\u6570\u6ca1\u6709\u4efb\u4f55\u540e\u7f6e\u6761\u4ef6\u3002 \u65ad\u8a00 \u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u68c0\u6d4b\u65e0\u6548\u53c2\u6570\uff08\u6216\u9a8c\u8bc1\u67d0\u79cd\u5176\u4ed6\u7c7b\u578b\u7684\u5047\u8bbe\uff09\uff0c\u5e76\u6253\u5370\u9519\u8bef\u6d88\u606f\u5e76\u7ec8\u6b62\u7a0b\u5e8f\u662f\u5bf9\u95ee\u9898\u7684\u5e38\u89c1\u54cd\u5e94\uff0c\u56e0\u6b64C++\u63d0\u4f9b\u4e86\u4e00\u79cd\u5feb\u6377\u65b9\u6cd5\u6765\u6267\u884c\u6b64\u64cd\u4f5c\u3002 \u65ad\u8a00\u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u9664\u975e\u7a0b\u5e8f\u4e2d\u5b58\u5728\u9519\u8bef\uff0c\u5426\u5219\u8be5\u8868\u8fbe\u5f0f\u5c06\u4e3a\u771f\u3002\u5982\u679c\u8868\u8fbe\u5f0f\u7684\u503c\u4e3a true \uff0c\u5219\u65ad\u8a00\u8bed\u53e5\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\u3002\u5982\u679c\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u503c\u4e3a false \uff0c\u5219\u663e\u793a\u9519\u8bef\u6d88\u606f\u5e76\u901a\u8fc7 std::abort \u7ec8\u6b62\u7a0b\u5e8f\u3002\u6b64\u9519\u8bef\u6d88\u606f\u901a\u5e38\u5305\u542b\u4ee5\u6587\u672c\u5f62\u5f0f\u5931\u8d25\u7684\u8868\u8fbe\u5f0f\uff0c\u4ee5\u53ca\u65ad\u8a00\u7684\u4ee3\u7801\u6587\u4ef6\u7684\u540d\u79f0\u548c\u884c\u53f7\u3002\u8fd9\u4f7f\u5f97\u4e0d\u4ec5\u53ef\u4ee5\u786e\u5b9a\u95ee\u9898\u6240\u5728\uff0c\u8fd8\u53ef\u4ee5\u786e\u5b9a\u95ee\u9898\u53d1\u751f\u7684\u4ee3\u7801\u4f4d\u7f6e\u3002\u8fd9\u53ef\u4ee5\u6781\u5927\u5730\u5e2e\u52a9\u8c03\u8bd5\u5de5\u4f5c\u3002 \u5728C++\u4e2d\uff0c\u8fd0\u884c\u65f6\u65ad\u8a00\u662f\u901a\u8fc7assert\u9884\u5904\u7406\u5b8f\u5b9e\u73b0\u7684\uff0c\u8be5\u5b8f\u4f4d\u4e8e\u5934\u6587\u4ef6\u4e2d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include // for assert() #include // for std::sqrt #include double calculateTimeUntilObjectHitsGround ( double initialHeight , double gravity ) { assert ( gravity > 0.0 ); // The object won't reach the ground unless there is positive gravity. if ( initialHeight <= 0.0 ) { // The object is already on the ground. Or buried. return 0.0 ; } return std :: sqrt (( 2.0 * initialHeight ) / gravity ); } int main () { std :: cout << \"Took \" << calculateTimeUntilObjectHitsGround ( 100.0 , -9.8 ) << \" second(s) \\n \" ; return 0 ; } COPY \u5f53\u7a0b\u5e8f\u8c03\u7528 calculateTimeUntilObjectHitsGround(100.0, -9.8) \u65f6\uff0c assert(gravity > 0.0) \u5c06\u8ba1\u7b97\u4e3a false \uff0c\u8fd9\u5c06\u89e6\u53d1\u65ad\u8a00\u3002\u8fd9\u5c06\u6253\u5370\u7c7b\u4f3c\u4e8e\u4ee5\u4e0b\u5185\u5bb9\u7684\u6d88\u606f\uff1a 1 dropsimulator: src/main.cpp:6: double calculateTimeUntilObjectHitsGround(double, double): Assertion 'gravity > 0.0' failed. \u5b9e\u9645\u6d88\u606f\u56e0\u4f7f\u7528\u7684\u7f16\u8bd1\u5668\u800c\u5f02\u3002 \u5c3d\u7ba1\u65ad\u8a00\u6700\u5e38\u7528\u4e8e\u9a8c\u8bc1\u51fd\u6570\u53c2\u6570\uff0c\u4f46\u5b83\u4eec\u53ef\u4ee5\u7528\u4e8e\u4efb\u4f55\u60a8\u60f3\u8981\u9a8c\u8bc1\u7684\u5185\u5bb9\u3002 \u5c3d\u7ba1\u6211\u4eec\u4e4b\u524d\u544a\u8bc9\u8fc7\u60a8\u8981\u907f\u514d\u9884\u5904\u7406\u5b8f\uff0c\u4f46\u65ad\u8a00\u662f\u5c11\u6570\u88ab\u8ba4\u4e3a\u53ef\u4ee5\u4f7f\u7528\u7684\u9884\u5904\u7406\u5b8f\u4e4b\u4e00\u3002\u6211\u4eec\u9f13\u52b1\u60a8\u5728\u4ee3\u7801\u4e2d\u5927\u91cf\u4f7f\u7528assert\u8bed\u53e5\u3002 \u4f7f\u65ad\u8a00\u8bed\u53e5\u66f4\u5177\u63cf\u8ff0\u6027 \u6709\u65f6\uff0c\u65ad\u8a00\u8868\u8fbe\u5f0f\u7684\u63cf\u8ff0\u6027\u5e76\u4e0d\u597d\uff0c\u4f8b\u5982\u4e0b\u9762\u7684\u8bed\u53e5\uff1a 1 assert ( found ); \u5982\u679c\u89e6\u53d1\u6b64\u65ad\u8a00\uff0c\u5219\u4f1a\u663e\u793a\uff1a 1 Assertion failed: found, file C:\\\\VCProjects\\\\Test.cpp, line 34 \u8fd9\u662f\u4ec0\u4e48\u610f\u601d\uff1f\u663e\u7136\uff0c found \u662f false \uff08\u56e0\u4e3a\u65ad\u8a00\u89e6\u53d1\u4e86\uff09\uff0c\u81f3\u4e8e\u662f\u4ec0\u4e48\u6ca1\u627e\u5230\uff1f\u4f60\u5fc5\u987b\u67e5\u770b\u4ee3\u7801\u624d\u80fd\u786e\u5b9a\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5c0f\u6280\u5de7\u4f7f assert \u8bed\u53e5\u66f4\u5177\u63cf\u8ff0\u6027\u3002\u53ea\u9700\u6dfb\u52a0\u4e00\u4e2a\u903b\u8f91AND\u8fde\u63a5\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff1a 1 assert ( found && \"Car could not be found in database\" ); \u8fd9\u662f\u4e3a\u4ec0\u4e48\u5b83\u6709\u6548\u7684\u539f\u56e0\uff1a\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u59cb\u7ec8\u8ba1\u7b97\u4e3a\u5e03\u5c14\u503c true \u3002\u56e0\u6b64\uff0c\u5982\u679c found \u4e3a false \uff0c\u5219 false && true \u4e3a false \u3002\u5982\u679c found \u4e3a true \uff0c\u5219 true && true \u4e3a true \u3002\u56e0\u6b64\uff0c\u5728\u903b\u8f91AND\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u4e0d\u4f1a\u5f71\u54cd\u65ad\u8a00\u7684\u8bc4\u4f30\u3002 \u4f46\u662f\uff0c\u5f53\u65ad\u8a00\u89e6\u53d1\u65f6\uff0c\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u5c06\u5305\u542b\u5728\u65ad\u8a00\u6d88\u606f\u4e2d\uff1a Assertion failed: found && \"Car could not be found in database\", file C:\\\\VCProjects\\\\Test.cpp, line 34 \u8fd9\u4e3a\u4f60\u63d0\u4f9b\u4e86\u6709\u5173\u51fa\u4e86\u4ec0\u4e48\u95ee\u9898\u7684\u5176\u4ed6\u4e0a\u4e0b\u6587\u3002 \u65ad\u8a00\u4e0e\u9519\u8bef\u5904\u7406 \u65ad\u8a00\u548c\u9519\u8bef\u5904\u7406\u975e\u5e38\u76f8\u4f3c\uff0c\u4ee5\u81f3\u4e8e\u5b83\u4eec\u7684\u76ee\u7684\u53ef\u80fd\u4f1a\u6df7\u6dc6\uff0c\u56e0\u6b64\u8ba9\u6211\u4eec\u6f84\u6e05\uff1a \u65ad\u8a00\u7684\u76ee\u6807\u662f\u901a\u8fc7\u8bb0\u5f55\u6c38\u8fdc\u4e0d\u5e94\u53d1\u751f\u7684\u4e8b\u60c5\u6765\u6355\u83b7\u7f16\u7a0b\u9519\u8bef\u3002\u5982\u679c\u53d1\u751f\u4e86\u90a3\u4ef6\u4e8b\uff0c\u90a3\u4e48\u7a0b\u5e8f\u5458\u5728\u67d0\u4e2a\u5730\u65b9\u72af\u4e86\u9519\u8bef\uff0c\u5e76\u4e14\u53ef\u4ee5\u786e\u5b9a\u5e76\u4fee\u590d\u8be5\u9519\u8bef\u3002\u65ad\u8a00\u4e0d\u5141\u8bb8\u4ece\u9519\u8bef\u4e2d\u6062\u590d\uff08\u6bd5\u7adf\uff0c\u5982\u679c\u67d0\u4ef6\u4e8b\u60c5\u6c38\u8fdc\u4e0d\u5e94\u8be5\u53d1\u751f\uff0c\u90a3\u4e48\u65e0\u9700\u4ece\u4e2d\u6062\u590d\uff09\uff0c\u5e76\u4e14\u7a0b\u5e8f\u4e0d\u4f1a\u751f\u6210\u53cb\u597d\u7684\u9519\u8bef\u6d88\u606f\u3002 \u53e6\u4e00\u65b9\u9762\uff0c\u9519\u8bef\u5904\u7406\u65e8\u5728\u4f18\u96c5\u5730\u5904\u7406\u5728\u53d1\u5e03\u914d\u7f6e\u4e2d\u53ef\u80fd\u53d1\u751f\uff08\u65e0\u8bba\u591a\u4e48\u7f55\u89c1\uff09\u7684\u60c5\u51b5\u3002\u8fd9\u4e9b\u53ef\u80fd\u662f\u53ef\u6062\u590d\u7684\uff0c\u4e5f\u53ef\u80fd\u662f\u4e0d\u53ef\u6062\u590d\u7684\uff0c\u4f46\u662f\u5e94\u59cb\u7ec8\u5047\u5b9a\u7a0b\u5e8f\u7684\u7528\u6237\u53ef\u80fd\u4f1a\u9047\u5230\u5b83\u4eec\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u65ad\u8a00\u8bb0\u5f55\u5e94\u8be5\u5728\u903b\u8f91\u4e0a\u4e0d\u53ef\u80fd\u7684\u60c5\u51b5\u3002 \u65ad\u8a00\u6709\u65f6\u4e5f\u7528\u4e8e\u8bb0\u5f55\u7531\u4e8e\u7f16\u5199\u4ee3\u7801\u65f6\u4e0d\u9700\u8981\u7684\u60c5\u51b5\uff1a 1 2 // Assert with a message, covered in the next section assert ( moved && \"Need to handle case where student was just moved to another classroom\" ); \u8fd9\u6837\uff0c\u5982\u679c\u4ee3\u7801\u7684\u540e\u7eed\u7ef4\u62a4\u8005\u786e\u5b9e\u9047\u5230\u9700\u8981\u8fd9\u79cd\u60c5\u51b5\u7684\u60c5\u51b5\uff0c\u4ee3\u7801\u5c06\u56e0\u4e3a\u4e00\u4e2a\u6709\u7528\u7684\u9519\u8bef\u4fe1\u606f\u800c\u5931\u8d25\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u786e\u5b9a\u5982\u4f55\u5b9e\u73b0\u8fd9\u79cd\u60c5\u51b5\u3002 NDEBUG assert \u5b8f\u4f1a\u5e26\u6765\u4e00\u4e9b\u5c0f\u7684\u6027\u80fd\u635f\u5931\uff0c\u6bcf\u6b21\u68c0\u67e5 assert \u6761\u4ef6\u65f6\u90fd\u4f1a\u4ea7\u751f\u3002\u6b64\u5916\uff0c(\u7406\u60f3\u60c5\u51b5\u4e0b)asserts \u4e0d\u5e94\u8be5\u51fa\u73b0\u5728\u751f\u4ea7\u4ee3\u7801\u4e2d(\u56e0\u4e3a\u4f60\u7684\u4ee3\u7801\u5e94\u8be5\u5df2\u7ecf\u7ecf\u8fc7\u5f7b\u5e95\u6d4b\u8bd5)\u3002\u56e0\u6b64\uff0c\u8bb8\u591a\u5f00\u53d1\u4eba\u5458\u5e0c\u671b asserts \u4ec5\u5728\u8c03\u8bd5\u7248\u672c\u4e2d\u5904\u4e8e\u6d3b\u52a8\u72b6\u6001\u3002C++ \u63d0\u4f9b\u4e86\u4e00\u79cd\u5728\u751f\u4ea7\u4ee3\u7801\u4e2d\u5173\u95ed asserts \u7684\u65b9\u6cd5\u3002\u5982\u679c\u5b9a\u4e49\u4e86\u5b8f NDEBUG \uff0cassert \u5b8f\u5c06\u88ab\u7981\u7528\u3002 \u4e00\u4e9b IDE \u9ed8\u8ba4\u8bbe\u7f6e NDEBUG \u4f5c\u4e3a\u53d1\u5e03\u914d\u7f6e\u7684\u9879\u76ee\u8bbe\u7f6e\u7684\u4e00\u90e8\u5206\u3002\u4f8b\u5982\uff0c\u5728 Visual Studio \u4e2d\uff0c\u4ee5\u4e0b\u9884\u5904\u7406\u5668\u5b9a\u4e49\u88ab\u8bbe\u7f6e\u4e3a\u9879\u76ee\u7ea7\u522b: WIN32;NDEBUG;_CONSOLE \u3002\u5982\u679c\u4f60\u6b63\u5728\u4f7f\u7528 Visual Studio \u5e76\u5e0c\u671b\u5728\u53d1\u5e03\u7248\u672c\u4e2d\u89e6\u53d1 asserts\uff0c\u5219\u9700\u8981\u4ece\u6b64\u8bbe\u7f6e\u4e2d\u5220\u9664 NDEBUG \u3002 \u5982\u679c\u4f60\u4f7f\u7528\u7684 IDE \u6216\u6784\u5efa\u7cfb\u7edf\u5728\u53d1\u5e03\u914d\u7f6e\u4e2d\u6ca1\u6709\u81ea\u52a8\u5b9a\u4e49 NDEBUG \uff0c\u5219\u9700\u8981\u5728\u9879\u76ee\u6216\u7f16\u8bd1\u8bbe\u7f6e\u4e2d\u624b\u52a8\u6dfb\u52a0\u5b83\u3002 \u65ad\u8a00\u7684\u9650\u5236\u548c\u63d0\u9192 \u5173\u4e8e asserts \u6709\u4e00\u4e9b\u9677\u9631\u548c\u9650\u5236\u3002\u9996\u5148\uff0cassert \u672c\u8eab\u53ef\u80fd\u6709 bug\u3002\u5982\u679c\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\uff0cassert \u5c06\u62a5\u544a\u4e0d\u5b58\u5728\u7684\u9519\u8bef\uff0c\u6216\u8005\u65e0\u6cd5\u62a5\u544a\u5b58\u5728\u7684\u9519\u8bef\u3002 \u5176\u6b21\uff0c\u4f60\u7684 asserts \u4e0d\u5e94\u8be5\u6709\u4efb\u4f55\u526f\u4f5c\u7528\u2014\u2014\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u6709 assert \u548c\u6ca1\u6709 assert \u7684\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u5e94\u8be5\u8fd0\u884c\u76f8\u540c\u3002\u5426\u5219\uff0c\u5728\u8c03\u8bd5\u914d\u7f6e\u4e2d\u8fdb\u884c\u6d4b\u8bd5\u7684\u5185\u5bb9\u5c06\u4e0e\u53d1\u5e03\u914d\u7f6e\u4e2d\u8fdb\u884c\u7684\u6d4b\u8bd5\u5185\u5bb9\u4e0d\u540c(\u5047\u8bbe\u4f60\u4f7f\u7528\u4e86 NDEBUG)\u3002 \u8fd8\u8981\u6ce8\u610f\u7684\u662f\uff0cabort() \u51fd\u6570\u4f1a\u7acb\u5373\u7ec8\u6b62\u7a0b\u5e8f\uff0c\u6ca1\u6709\u673a\u4f1a\u8fdb\u884c\u4efb\u4f55\u8fdb\u4e00\u6b65\u7684\u6e05\u7406(\u4f8b\u5982\u5173\u95ed\u6587\u4ef6\u6216\u6570\u636e\u5e93)\u3002\u56e0\u6b64\uff0casserts \u5e94\u8be5\u4ec5\u5728\u7a0b\u5e8f\u610f\u5916\u7ec8\u6b62\u4e0d\u592a\u53ef\u80fd\u5bfc\u81f4\u635f\u574f\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002 static_assert C++ \u8fd8\u6709\u53e6\u4e00\u79cd\u79f0\u4e3a static_assert \u7684 assert \u7c7b\u578b\u3002 static_assert \u662f\u5728\u7f16\u8bd1\u65f6\u800c\u4e0d\u662f\u8fd0\u884c\u65f6\u68c0\u67e5\u7684\u65ad\u8a00\uff0c\u4e00\u4e2a\u5931\u8d25\u7684 static_assert \u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002\u4e0e assert \u4e0d\u540c\uff0cassert \u662f\u5728\u5934\u6587\u4ef6\u4e2d\u58f0\u660e\u7684\uff0c\u800c static_assert \u662f\u4e00\u4e2a\u5173\u952e\u5b57\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u5305\u542b\u5934\u6587\u4ef6\u624d\u80fd\u4f7f\u7528\u5b83\u3002 static_assert \u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 static_assert ( condition , diagnostic_message ) \u5982\u679c\u6761\u4ef6\u4e0d\u6210\u7acb\uff0c\u5c31\u4f1a\u6253\u5370\u51fa\u8bca\u65ad\u4fe1\u606f\u3002\u4e0b\u9762\u662f\u4f7f\u7528 static_assert \u786e\u4fdd\u7c7b\u578b\u5177\u6709\u7279\u5b9a\u5927\u5c0f\u7684\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 static_assert ( sizeof ( long ) == 8 , \"long must be 8 bytes\" ); static_assert ( sizeof ( int ) == 4 , \"int must be 4 bytes\" ); int main () { return 0 ; } \u5728\u4f5c\u8005\u7684\u673a\u5668\u4e0a\uff0c\u7f16\u8bd1\u65f6\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\uff1a 1 1>c:\\consoleapplication1\\main.cpp(19): error C2338: long must be 8 bytes \u56e0\u4e3a static_assert \u662f\u7531\u7f16\u8bd1\u5668\u8bc4\u4f30\u7684\uff0c\u6240\u4ee5\u6761\u4ef6\u5fc5\u987b\u80fd\u591f\u5728\u7f16\u8bd1\u65f6\u8fdb\u884c\u8bc4\u4f30\u3002\u6b64\u5916\uff0c\u4e0e\u6b63\u5e38\u7684 assert \u4e0d\u540c(\u5728\u8fd0\u884c\u65f6\u8bc4\u4f30)\uff0c static_assert \u53ef\u4ee5\u653e\u7f6e\u5728\u4ee3\u7801\u6587\u4ef6\u4e2d\u7684\u4efb\u4f55\u4f4d\u7f6e(\u751a\u81f3\u662f\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d)\u3002 \u5728 C++17 \u4e4b\u524d\uff0c\u8bca\u65ad\u6d88\u606f\u5fc5\u987b\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u53c2\u6570\u63d0\u4f9b\u3002\u81ea C++17 \u4ee5\u6765\uff0c\u63d0\u4f9b\u8bca\u65ad\u6d88\u606f\u662f\u53ef\u9009\u7684\u3002","title":"7.17 - \u65ad\u8a00\u548c static_assert"},{"location":"7-17-assert-and-static-assert/#_1","text":"In programming, a precondition is any condition that must always be true prior to the execution of component of code. Our check of y is a precondition that ensures y has a valid value before the function continues. It\u2019s more common for functions with preconditions to be written like this: 1 2 3 4 5 6 7 8 9 10 void printDivision ( int x , int y ) { if ( y == 0 ) { std :: cerr << \"Error: Could not divide by zero \\n \" ; return ; } std :: cout << static_cast < double > ( x ) / y ; } COPY An invariant is a condition that must be true while some component is executing. Similarly, a postcondition is something that must be true after the execution of some component of code. Our function doesn\u2019t have any postconditions.","title":"\u524d\u7f6e\u6761\u4ef6\u3001\u4e0d\u53d8\u91cf\u548c\u540e\u7f6e\u6761\u4ef6"},{"location":"7-17-assert-and-static-assert/#_2","text":"Using a conditional statement to detect an invalid parameter (or to validate some other kind of assumption), along with printing an error message and terminating the program, is such a common response to problems that C++ provides a shortcut method for doing this. An assertion is an expression that will be true unless there is a bug in the program. If the expression evaluates to true , the assertion statement does nothing. If the conditional expression evaluates to false , an error message is displayed and the program is terminated (via std::abort ). This error message typically contains the expression that failed as text, along with the name of the code file and the line number of the assertion. This makes it very easy to tell not only what the problem was, but where in the code the problem occurred. This can help with debugging efforts immensely. In C++, runtime assertions are implemented via the assert preprocessor macro, which lives in the header. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include // for assert() #include // for std::sqrt #include double calculateTimeUntilObjectHitsGround ( double initialHeight , double gravity ) { assert ( gravity > 0.0 ); // The object won't reach the ground unless there is positive gravity. if ( initialHeight <= 0.0 ) { // The object is already on the ground. Or buried. return 0.0 ; } return std :: sqrt (( 2.0 * initialHeight ) / gravity ); } int main () { std :: cout << \"Took \" << calculateTimeUntilObjectHitsGround ( 100.0 , -9.8 ) << \" second(s) \\n \" ; return 0 ; } COPY When the program calls calculateTimeUntilObjectHitsGround(100.0, -9.8) , assert(gravity > 0.0) will evaluate to false , which will trigger the assert. That will print a message similar to this: 1 dropsimulator: src/main.cpp:6: double calculateTimeUntilObjectHitsGround(double, double): Assertion 'gravity > 0.0' failed. The actual message varies depending on which compiler you use. Although asserts are most often used to validate function parameters, they can be used anywhere you would like to validate that something is true. Although we told you previously to avoid preprocessor macros, asserts are one of the few preprocessor macros that are considered acceptable to use. We encourage you to use assert statements liberally throughout your code.","title":"\u65ad\u8a00"},{"location":"7-17-assert-and-static-assert/#_3","text":"Sometimes assert expressions aren\u2019t very descriptive. Consider the following statement: 1 assert ( found ); COPY If this assert is triggered, the assert will say: 1 Assertion failed: found, file C:\\\\VCProjects\\\\Test.cpp, line 34 What does this even mean? Clearly found was false (since the assert triggered), but what wasn\u2019t found? You\u2019d have to go look at the code to determine that. Fortunately, there\u2019s a little trick you can use to make your assert statements more descriptive. Simply add a string literal joined by a logical AND: 1 assert ( found && \"Car could not be found in database\" ); COPY Here\u2019s why this works: A string literal always evaluates to Boolean true . So if found is false , false && true is false . If found is true , true && true is true . Thus, logical AND-ing a string literal doesn\u2019t impact the evaluation of the assert. However, when the assert triggers, the string literal will be included in the assert message: Assertion failed: found && \"Car could not be found in database\", file C:\\VCProjects\\Test.cpp, line 34 That gives you some additional context as to what went wrong.","title":"\u4f7f\u65ad\u8a00\u8bed\u53e5\u66f4\u5177\u63cf\u8ff0\u6027"},{"location":"7-17-assert-and-static-assert/#vs","text":"Assertions and error handling are similar enough that their purposes can be confused, so let\u2019s clarify: The goal of an assertion is to catch programming errors by documenting something that should never happen. If that thing does happen, then the programmer made an error somewhere, and that error can be identified and fixed. Assertions do not allow recovery from errors (after all, if something should never happen, there\u2019s no need to recover from it), and the program will not produce a friendly error message. On the other hand, error handling is designed to gracefully handle cases that could happen (however rarely) in release configurations. These may or may not be recoverable, but one should always assume a user of the program may encounter them. \u6700\u4f73\u5b9e\u8df5 Use assertions to document cases that should be logically impossible. Assertions are also sometimes used to document cases that were not implemented because they were not needed at the time the programmer wrote the code: 1 2 // Assert with a message, covered in the next section assert ( moved && \"Need to handle case where student was just moved to another classroom\" ); COPY That way, if a future user of the code does encounter a situation where this case is needed, the code will fail with a useful error message, and the programmer can then determine how to implement that case.","title":"\u65ad\u8a00 vs \u9519\u8bef\u5904\u7406"},{"location":"7-17-assert-and-static-assert/#ndebug","text":"The assert macro comes with a small performance cost that is incurred each time the assert condition is checked. Furthermore, asserts should (ideally) never be encountered in production code (because your code should already be thoroughly tested). Consequently, many developers prefer that asserts are only active in debug builds. C++ comes with a way to turn off asserts in production code. If the macro NDEBUG is defined, the assert macro gets disabled. Some IDEs set NDEBUG by default as part of the project settings for release configurations. For example, in Visual Studio, the following preprocessor definitions are set at the project level: WIN32;NDEBUG;_CONSOLE . If you\u2019re using Visual Studio and want your asserts to trigger in release builds, you\u2019ll need to remove NDEBUG from this setting. If you\u2019re using an IDE or build system that doesn\u2019t automatically define NDEBUG in release configuration, you will need to add it in the project or compilation settings manually.","title":"NDEBUG"},{"location":"7-17-assert-and-static-assert/#_4","text":"There are a few pitfalls and limitations to asserts. First, the assert itself can have a bug. If this happens, the assert will either report an error where none exists, or fail to report a bug where one does exist. Second, your asserts should have no side effects -- that is, the program should run the same with and without the assert. Otherwise, what you are testing in a debug configuration will not be the same as in a release configuration (assuming you ship with NDEBUG). Also note that the abort() function terminates the program immediately, without a chance to do any further cleanup (e.g. close a file or database). Because of this, asserts should be used only in cases where corruption isn\u2019t likely to occur if the program terminates unexpectedly.","title":"\u65ad\u8a00\u7684\u9650\u5236\u548c\u63d0\u9192"},{"location":"7-17-assert-and-static-assert/#static_assert","text":"C++ also has another type of assert called static_assert . A static_assert is an assertion that is checked at compile-time rather than at runtime, with a failing static_assert causing a compile error. Unlike assert, which is declared in the header, static_assert is a keyword, so no header needs to be included to use it. A static_assert takes the following form: 1 static_assert ( condition , diagnostic_message ) If the condition is not true, the diagnostic message is printed. Here\u2019s an example of using static_assert to ensure types have a certain size: 1 2 3 4 5 6 7 static_assert ( sizeof ( long ) == 8 , \"long must be 8 bytes\" ); static_assert ( sizeof ( int ) == 4 , \"int must be 4 bytes\" ); int main () { return 0 ; } COPY On the author\u2019s machine, when compiled, the compiler errors: 1 1>c:\\consoleapplication1\\main.cpp(19): error C2338: long must be 8 bytes Because static_assert is evaluated by the compiler, the condition must be able to be evaluated at compile time. Also, unlike normal assert (which is evaluated at runtime), static_assert can be placed anywhere in the code file (even in the global namespace). Prior to C++17, the diagnostic message must be supplied as the second parameter. Since C++17, providing a diagnostic message is optional. \u5728\u63a5\u6536\u53c2\u6570\u7684\u51fd\u6570\u4e2d\uff0c\u8c03\u7528\u8005\u53ef\u80fd\u4f1a\u4f20\u9012\u8bed\u6cd5\u4e0a\u6709\u6548\u4f46\u8bed\u4e49\u4e0a\u65e0\u610f\u4e49\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u5728\u4e0a\u4e00\u8bfe\u4e2d\uff08 7.15 - \u53d1\u73b0\u9519\u8bef\u548c\u5904\u7406\u9519\u8bef \uff09\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u4ee5\u4e0b\u793a\u4f8b\u51fd\u6570\uff1a 1 2 3 4 5 6 7 void printDivision ( int x , int y ) { if ( y != 0 ) std :: cout << static_cast ( x ) / y ; else std :: cerr << \"Error: Could not divide by zero \\n \" ; } \u8be5\u51fd\u6570\u663e\u5f0f\u68c0\u67e5 y \u662f\u5426\u4e3a 0 \uff0c\u56e0\u4e3a\u9664\u4ee5\u96f6\u662f\u8bed\u4e49\u9519\u8bef\uff0c\u5982\u679c\u6267\u884c\u5c06\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002 \u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u5904\u7406\u6b64\u7c7b\u95ee\u9898\u7684\u51e0\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u505c\u6b62\u7a0b\u5e8f\u6216\u8df3\u8fc7\u6709\u95ee\u9898\u7684\u8bed\u53e5\u3002 \u4f46\u8fd9\u4e24\u79cd\u9009\u9879\u90fd\u6709\u95ee\u9898\u3002\u5982\u679c\u7a0b\u5e8f\u7531\u4e8e\u9519\u8bef\u800c\u8df3\u8fc7\u8bed\u53e5\uff0c\u5219\u5b9e\u9645\u4e0a\u662f\u5728\u9ed8\u9ed8\u5730\u5931\u8d25\u3002\u5c24\u5176\u662f\u5728\u7f16\u5199\u548c\u8c03\u8bd5\u7a0b\u5e8f\u65f6\uff0c\u9759\u9ed8\u5931\u8d25\u662f\u4e0d\u597d\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u4f1a\u63a9\u76d6\u771f\u6b63\u7684\u95ee\u9898\u3002\u5373\u4f7f\u6211\u4eec\u6253\u5370\u9519\u8bef\u6d88\u606f\uff0c\u8be5\u9519\u8bef\u6d88\u606f\u4e5f\u53ef\u80fd\u4f1a\u5728\u5176\u4ed6\u7a0b\u5e8f\u8f93\u51fa\u4e2d\u4e22\u5931\uff0c\u5e76\u4e14\u53ef\u80fd\u4e0d\u660e\u663e\u5730\u751f\u6210\u9519\u8bef\u6d88\u606f\u6216\u89e6\u53d1\u9519\u8bef\u6d88\u606f\u7684\u6761\u4ef6\u5728\u54ea\u91cc\u3002\u6709\u4e9b\u51fd\u6570\u53ef\u80fd\u4f1a\u88ab\u8c03\u7528\u5341\u6b21\u6216\u51e0\u767e\u6b21\uff0c\u5982\u679c\u53ea\u6709\u5176\u4e2d\u4e00\u4e2a\u60c5\u51b5\u751f\u6210\u95ee\u9898\uff0c\u90a3\u4e48\u5f88\u96be\u77e5\u9053\u662f\u54ea\u4e2a\u60c5\u51b5\u3002 \u5982\u679c\u7a0b\u5e8f\u7ec8\u6b62\uff08\u901a\u8fc7 std::exit \uff09\uff0c\u5219\u6211\u4eec\u5c06\u5931\u53bb\u8c03\u7528\u5806\u6808\u548c\u4efb\u4f55\u53ef\u80fd\u5e2e\u52a9\u6211\u4eec\u9694\u79bb\u95ee\u9898\u7684\u8c03\u8bd5\u4fe1\u606f\u3002 std::abort \u662f\u8fd9\u79cd\u60c5\u51b5\u7684\u66f4\u597d\u9009\u62e9\uff0c\u56e0\u4e3a\u901a\u5e38\u5f00\u53d1\u4eba\u5458\u5c06\u6709\u9009\u62e9\u5728\u7a0b\u5e8f\u4e2d\u6b62\u7684\u70b9\u5f00\u59cb\u8c03\u8bd5\u3002","title":"static_assert"},{"location":"7-17-assert-and-static-assert/#_5","text":"\u5728\u7f16\u7a0b\u4e2d\uff0c\u524d\u7f6e\u6761\u4ef6\u662f\u6307\u5728\u4ee3\u7801\u7ec4\u4ef6\u6267\u884c\u4e4b\u524d\u5fc5\u987b\u59cb\u7ec8\u4e3a\u771f\u7684\u4efb\u4f55\u6761\u4ef6\u3002\u6211\u4eec\u5bf9 y \u7684\u68c0\u67e5\u662f\u786e\u4fdd\u51fd\u6570\u7ee7\u7eed\u4e4b\u524d y \u5177\u6709\u6709\u6548\u503c\u7684\u524d\u7f6e\u6761\u4ef6\u3002 \u66f4\u5e38\u89c1\u7684\u662f\uff0c\u5177\u6709\u524d\u7f6e\u6761\u4ef6\u7684\u51fd\u6570\u7f16\u5199\u5982\u4e0b\uff1a 1 2 3 4 5 6 7 8 9 10 void printDivision ( int x , int y ) { if ( y == 0 ) { std :: cerr << \"Error: Could not divide by zero \\n \" ; return ; } std :: cout << static_cast ( x ) / y ; } \u4e0d\u53d8\u91cf\u662f\u6307\u67d0\u4e2a\u7ec4\u4ef6\u6267\u884c\u65f6\u5fc5\u987b\u4e3a\u771f\u7684\u6761\u4ef6\u3002 \u7c7b\u4f3c\u5730\uff0c\u540e\u7f6e\u6761\u4ef6\u662f\u6307\u67d0\u4e2a\u4ee3\u7801\u7ec4\u4ef6\u6267\u884c\u540e\u5fc5\u987b\u4e3a\u771f\u7684\u6761\u4ef6\u3002\u6211\u4eec\u7684\u51fd\u6570\u6ca1\u6709\u4efb\u4f55\u540e\u7f6e\u6761\u4ef6\u3002","title":"\u524d\u7f6e\u6761\u4ef6\u3001\u4e0d\u53d8\u91cf\u548c\u540e\u7f6e\u6761\u4ef6"},{"location":"7-17-assert-and-static-assert/#_6","text":"\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u68c0\u6d4b\u65e0\u6548\u53c2\u6570\uff08\u6216\u9a8c\u8bc1\u67d0\u79cd\u5176\u4ed6\u7c7b\u578b\u7684\u5047\u8bbe\uff09\uff0c\u5e76\u6253\u5370\u9519\u8bef\u6d88\u606f\u5e76\u7ec8\u6b62\u7a0b\u5e8f\u662f\u5bf9\u95ee\u9898\u7684\u5e38\u89c1\u54cd\u5e94\uff0c\u56e0\u6b64C++\u63d0\u4f9b\u4e86\u4e00\u79cd\u5feb\u6377\u65b9\u6cd5\u6765\u6267\u884c\u6b64\u64cd\u4f5c\u3002 \u65ad\u8a00\u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u9664\u975e\u7a0b\u5e8f\u4e2d\u5b58\u5728\u9519\u8bef\uff0c\u5426\u5219\u8be5\u8868\u8fbe\u5f0f\u5c06\u4e3a\u771f\u3002\u5982\u679c\u8868\u8fbe\u5f0f\u7684\u503c\u4e3a true \uff0c\u5219\u65ad\u8a00\u8bed\u53e5\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\u3002\u5982\u679c\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u503c\u4e3a false \uff0c\u5219\u663e\u793a\u9519\u8bef\u6d88\u606f\u5e76\u901a\u8fc7 std::abort \u7ec8\u6b62\u7a0b\u5e8f\u3002\u6b64\u9519\u8bef\u6d88\u606f\u901a\u5e38\u5305\u542b\u4ee5\u6587\u672c\u5f62\u5f0f\u5931\u8d25\u7684\u8868\u8fbe\u5f0f\uff0c\u4ee5\u53ca\u65ad\u8a00\u7684\u4ee3\u7801\u6587\u4ef6\u7684\u540d\u79f0\u548c\u884c\u53f7\u3002\u8fd9\u4f7f\u5f97\u4e0d\u4ec5\u53ef\u4ee5\u786e\u5b9a\u95ee\u9898\u6240\u5728\uff0c\u8fd8\u53ef\u4ee5\u786e\u5b9a\u95ee\u9898\u53d1\u751f\u7684\u4ee3\u7801\u4f4d\u7f6e\u3002\u8fd9\u53ef\u4ee5\u6781\u5927\u5730\u5e2e\u52a9\u8c03\u8bd5\u5de5\u4f5c\u3002 \u5728C++\u4e2d\uff0c\u8fd0\u884c\u65f6\u65ad\u8a00\u662f\u901a\u8fc7assert\u9884\u5904\u7406\u5b8f\u5b9e\u73b0\u7684\uff0c\u8be5\u5b8f\u4f4d\u4e8e\u5934\u6587\u4ef6\u4e2d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include // for assert() #include // for std::sqrt #include double calculateTimeUntilObjectHitsGround ( double initialHeight , double gravity ) { assert ( gravity > 0.0 ); // The object won't reach the ground unless there is positive gravity. if ( initialHeight <= 0.0 ) { // The object is already on the ground. Or buried. return 0.0 ; } return std :: sqrt (( 2.0 * initialHeight ) / gravity ); } int main () { std :: cout << \"Took \" << calculateTimeUntilObjectHitsGround ( 100.0 , -9.8 ) << \" second(s) \\n \" ; return 0 ; } COPY \u5f53\u7a0b\u5e8f\u8c03\u7528 calculateTimeUntilObjectHitsGround(100.0, -9.8) \u65f6\uff0c assert(gravity > 0.0) \u5c06\u8ba1\u7b97\u4e3a false \uff0c\u8fd9\u5c06\u89e6\u53d1\u65ad\u8a00\u3002\u8fd9\u5c06\u6253\u5370\u7c7b\u4f3c\u4e8e\u4ee5\u4e0b\u5185\u5bb9\u7684\u6d88\u606f\uff1a 1 dropsimulator: src/main.cpp:6: double calculateTimeUntilObjectHitsGround(double, double): Assertion 'gravity > 0.0' failed. \u5b9e\u9645\u6d88\u606f\u56e0\u4f7f\u7528\u7684\u7f16\u8bd1\u5668\u800c\u5f02\u3002 \u5c3d\u7ba1\u65ad\u8a00\u6700\u5e38\u7528\u4e8e\u9a8c\u8bc1\u51fd\u6570\u53c2\u6570\uff0c\u4f46\u5b83\u4eec\u53ef\u4ee5\u7528\u4e8e\u4efb\u4f55\u60a8\u60f3\u8981\u9a8c\u8bc1\u7684\u5185\u5bb9\u3002 \u5c3d\u7ba1\u6211\u4eec\u4e4b\u524d\u544a\u8bc9\u8fc7\u60a8\u8981\u907f\u514d\u9884\u5904\u7406\u5b8f\uff0c\u4f46\u65ad\u8a00\u662f\u5c11\u6570\u88ab\u8ba4\u4e3a\u53ef\u4ee5\u4f7f\u7528\u7684\u9884\u5904\u7406\u5b8f\u4e4b\u4e00\u3002\u6211\u4eec\u9f13\u52b1\u60a8\u5728\u4ee3\u7801\u4e2d\u5927\u91cf\u4f7f\u7528assert\u8bed\u53e5\u3002","title":"\u65ad\u8a00"},{"location":"7-17-assert-and-static-assert/#_7","text":"\u6709\u65f6\uff0c\u65ad\u8a00\u8868\u8fbe\u5f0f\u7684\u63cf\u8ff0\u6027\u5e76\u4e0d\u597d\uff0c\u4f8b\u5982\u4e0b\u9762\u7684\u8bed\u53e5\uff1a 1 assert ( found ); \u5982\u679c\u89e6\u53d1\u6b64\u65ad\u8a00\uff0c\u5219\u4f1a\u663e\u793a\uff1a 1 Assertion failed: found, file C:\\\\VCProjects\\\\Test.cpp, line 34 \u8fd9\u662f\u4ec0\u4e48\u610f\u601d\uff1f\u663e\u7136\uff0c found \u662f false \uff08\u56e0\u4e3a\u65ad\u8a00\u89e6\u53d1\u4e86\uff09\uff0c\u81f3\u4e8e\u662f\u4ec0\u4e48\u6ca1\u627e\u5230\uff1f\u4f60\u5fc5\u987b\u67e5\u770b\u4ee3\u7801\u624d\u80fd\u786e\u5b9a\u3002 \u5e78\u8fd0\u7684\u662f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5c0f\u6280\u5de7\u4f7f assert \u8bed\u53e5\u66f4\u5177\u63cf\u8ff0\u6027\u3002\u53ea\u9700\u6dfb\u52a0\u4e00\u4e2a\u903b\u8f91AND\u8fde\u63a5\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff1a 1 assert ( found && \"Car could not be found in database\" ); \u8fd9\u662f\u4e3a\u4ec0\u4e48\u5b83\u6709\u6548\u7684\u539f\u56e0\uff1a\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u59cb\u7ec8\u8ba1\u7b97\u4e3a\u5e03\u5c14\u503c true \u3002\u56e0\u6b64\uff0c\u5982\u679c found \u4e3a false \uff0c\u5219 false && true \u4e3a false \u3002\u5982\u679c found \u4e3a true \uff0c\u5219 true && true \u4e3a true \u3002\u56e0\u6b64\uff0c\u5728\u903b\u8f91AND\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u4e0d\u4f1a\u5f71\u54cd\u65ad\u8a00\u7684\u8bc4\u4f30\u3002 \u4f46\u662f\uff0c\u5f53\u65ad\u8a00\u89e6\u53d1\u65f6\uff0c\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u5c06\u5305\u542b\u5728\u65ad\u8a00\u6d88\u606f\u4e2d\uff1a Assertion failed: found && \"Car could not be found in database\", file C:\\\\VCProjects\\\\Test.cpp, line 34 \u8fd9\u4e3a\u4f60\u63d0\u4f9b\u4e86\u6709\u5173\u51fa\u4e86\u4ec0\u4e48\u95ee\u9898\u7684\u5176\u4ed6\u4e0a\u4e0b\u6587\u3002","title":"\u4f7f\u65ad\u8a00\u8bed\u53e5\u66f4\u5177\u63cf\u8ff0\u6027"},{"location":"7-17-assert-and-static-assert/#_8","text":"\u65ad\u8a00\u548c\u9519\u8bef\u5904\u7406\u975e\u5e38\u76f8\u4f3c\uff0c\u4ee5\u81f3\u4e8e\u5b83\u4eec\u7684\u76ee\u7684\u53ef\u80fd\u4f1a\u6df7\u6dc6\uff0c\u56e0\u6b64\u8ba9\u6211\u4eec\u6f84\u6e05\uff1a \u65ad\u8a00\u7684\u76ee\u6807\u662f\u901a\u8fc7\u8bb0\u5f55\u6c38\u8fdc\u4e0d\u5e94\u53d1\u751f\u7684\u4e8b\u60c5\u6765\u6355\u83b7\u7f16\u7a0b\u9519\u8bef\u3002\u5982\u679c\u53d1\u751f\u4e86\u90a3\u4ef6\u4e8b\uff0c\u90a3\u4e48\u7a0b\u5e8f\u5458\u5728\u67d0\u4e2a\u5730\u65b9\u72af\u4e86\u9519\u8bef\uff0c\u5e76\u4e14\u53ef\u4ee5\u786e\u5b9a\u5e76\u4fee\u590d\u8be5\u9519\u8bef\u3002\u65ad\u8a00\u4e0d\u5141\u8bb8\u4ece\u9519\u8bef\u4e2d\u6062\u590d\uff08\u6bd5\u7adf\uff0c\u5982\u679c\u67d0\u4ef6\u4e8b\u60c5\u6c38\u8fdc\u4e0d\u5e94\u8be5\u53d1\u751f\uff0c\u90a3\u4e48\u65e0\u9700\u4ece\u4e2d\u6062\u590d\uff09\uff0c\u5e76\u4e14\u7a0b\u5e8f\u4e0d\u4f1a\u751f\u6210\u53cb\u597d\u7684\u9519\u8bef\u6d88\u606f\u3002 \u53e6\u4e00\u65b9\u9762\uff0c\u9519\u8bef\u5904\u7406\u65e8\u5728\u4f18\u96c5\u5730\u5904\u7406\u5728\u53d1\u5e03\u914d\u7f6e\u4e2d\u53ef\u80fd\u53d1\u751f\uff08\u65e0\u8bba\u591a\u4e48\u7f55\u89c1\uff09\u7684\u60c5\u51b5\u3002\u8fd9\u4e9b\u53ef\u80fd\u662f\u53ef\u6062\u590d\u7684\uff0c\u4e5f\u53ef\u80fd\u662f\u4e0d\u53ef\u6062\u590d\u7684\uff0c\u4f46\u662f\u5e94\u59cb\u7ec8\u5047\u5b9a\u7a0b\u5e8f\u7684\u7528\u6237\u53ef\u80fd\u4f1a\u9047\u5230\u5b83\u4eec\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u65ad\u8a00\u8bb0\u5f55\u5e94\u8be5\u5728\u903b\u8f91\u4e0a\u4e0d\u53ef\u80fd\u7684\u60c5\u51b5\u3002 \u65ad\u8a00\u6709\u65f6\u4e5f\u7528\u4e8e\u8bb0\u5f55\u7531\u4e8e\u7f16\u5199\u4ee3\u7801\u65f6\u4e0d\u9700\u8981\u7684\u60c5\u51b5\uff1a 1 2 // Assert with a message, covered in the next section assert ( moved && \"Need to handle case where student was just moved to another classroom\" ); \u8fd9\u6837\uff0c\u5982\u679c\u4ee3\u7801\u7684\u540e\u7eed\u7ef4\u62a4\u8005\u786e\u5b9e\u9047\u5230\u9700\u8981\u8fd9\u79cd\u60c5\u51b5\u7684\u60c5\u51b5\uff0c\u4ee3\u7801\u5c06\u56e0\u4e3a\u4e00\u4e2a\u6709\u7528\u7684\u9519\u8bef\u4fe1\u606f\u800c\u5931\u8d25\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u786e\u5b9a\u5982\u4f55\u5b9e\u73b0\u8fd9\u79cd\u60c5\u51b5\u3002","title":"\u65ad\u8a00\u4e0e\u9519\u8bef\u5904\u7406"},{"location":"7-17-assert-and-static-assert/#ndebug_1","text":"assert \u5b8f\u4f1a\u5e26\u6765\u4e00\u4e9b\u5c0f\u7684\u6027\u80fd\u635f\u5931\uff0c\u6bcf\u6b21\u68c0\u67e5 assert \u6761\u4ef6\u65f6\u90fd\u4f1a\u4ea7\u751f\u3002\u6b64\u5916\uff0c(\u7406\u60f3\u60c5\u51b5\u4e0b)asserts \u4e0d\u5e94\u8be5\u51fa\u73b0\u5728\u751f\u4ea7\u4ee3\u7801\u4e2d(\u56e0\u4e3a\u4f60\u7684\u4ee3\u7801\u5e94\u8be5\u5df2\u7ecf\u7ecf\u8fc7\u5f7b\u5e95\u6d4b\u8bd5)\u3002\u56e0\u6b64\uff0c\u8bb8\u591a\u5f00\u53d1\u4eba\u5458\u5e0c\u671b asserts \u4ec5\u5728\u8c03\u8bd5\u7248\u672c\u4e2d\u5904\u4e8e\u6d3b\u52a8\u72b6\u6001\u3002C++ \u63d0\u4f9b\u4e86\u4e00\u79cd\u5728\u751f\u4ea7\u4ee3\u7801\u4e2d\u5173\u95ed asserts \u7684\u65b9\u6cd5\u3002\u5982\u679c\u5b9a\u4e49\u4e86\u5b8f NDEBUG \uff0cassert \u5b8f\u5c06\u88ab\u7981\u7528\u3002 \u4e00\u4e9b IDE \u9ed8\u8ba4\u8bbe\u7f6e NDEBUG \u4f5c\u4e3a\u53d1\u5e03\u914d\u7f6e\u7684\u9879\u76ee\u8bbe\u7f6e\u7684\u4e00\u90e8\u5206\u3002\u4f8b\u5982\uff0c\u5728 Visual Studio \u4e2d\uff0c\u4ee5\u4e0b\u9884\u5904\u7406\u5668\u5b9a\u4e49\u88ab\u8bbe\u7f6e\u4e3a\u9879\u76ee\u7ea7\u522b: WIN32;NDEBUG;_CONSOLE \u3002\u5982\u679c\u4f60\u6b63\u5728\u4f7f\u7528 Visual Studio \u5e76\u5e0c\u671b\u5728\u53d1\u5e03\u7248\u672c\u4e2d\u89e6\u53d1 asserts\uff0c\u5219\u9700\u8981\u4ece\u6b64\u8bbe\u7f6e\u4e2d\u5220\u9664 NDEBUG \u3002 \u5982\u679c\u4f60\u4f7f\u7528\u7684 IDE \u6216\u6784\u5efa\u7cfb\u7edf\u5728\u53d1\u5e03\u914d\u7f6e\u4e2d\u6ca1\u6709\u81ea\u52a8\u5b9a\u4e49 NDEBUG \uff0c\u5219\u9700\u8981\u5728\u9879\u76ee\u6216\u7f16\u8bd1\u8bbe\u7f6e\u4e2d\u624b\u52a8\u6dfb\u52a0\u5b83\u3002","title":"NDEBUG"},{"location":"7-17-assert-and-static-assert/#_9","text":"\u5173\u4e8e asserts \u6709\u4e00\u4e9b\u9677\u9631\u548c\u9650\u5236\u3002\u9996\u5148\uff0cassert \u672c\u8eab\u53ef\u80fd\u6709 bug\u3002\u5982\u679c\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\uff0cassert \u5c06\u62a5\u544a\u4e0d\u5b58\u5728\u7684\u9519\u8bef\uff0c\u6216\u8005\u65e0\u6cd5\u62a5\u544a\u5b58\u5728\u7684\u9519\u8bef\u3002 \u5176\u6b21\uff0c\u4f60\u7684 asserts \u4e0d\u5e94\u8be5\u6709\u4efb\u4f55\u526f\u4f5c\u7528\u2014\u2014\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u6709 assert \u548c\u6ca1\u6709 assert \u7684\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u5e94\u8be5\u8fd0\u884c\u76f8\u540c\u3002\u5426\u5219\uff0c\u5728\u8c03\u8bd5\u914d\u7f6e\u4e2d\u8fdb\u884c\u6d4b\u8bd5\u7684\u5185\u5bb9\u5c06\u4e0e\u53d1\u5e03\u914d\u7f6e\u4e2d\u8fdb\u884c\u7684\u6d4b\u8bd5\u5185\u5bb9\u4e0d\u540c(\u5047\u8bbe\u4f60\u4f7f\u7528\u4e86 NDEBUG)\u3002 \u8fd8\u8981\u6ce8\u610f\u7684\u662f\uff0cabort() \u51fd\u6570\u4f1a\u7acb\u5373\u7ec8\u6b62\u7a0b\u5e8f\uff0c\u6ca1\u6709\u673a\u4f1a\u8fdb\u884c\u4efb\u4f55\u8fdb\u4e00\u6b65\u7684\u6e05\u7406(\u4f8b\u5982\u5173\u95ed\u6587\u4ef6\u6216\u6570\u636e\u5e93)\u3002\u56e0\u6b64\uff0casserts \u5e94\u8be5\u4ec5\u5728\u7a0b\u5e8f\u610f\u5916\u7ec8\u6b62\u4e0d\u592a\u53ef\u80fd\u5bfc\u81f4\u635f\u574f\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002","title":"\u65ad\u8a00\u7684\u9650\u5236\u548c\u63d0\u9192"},{"location":"7-17-assert-and-static-assert/#static_assert_1","text":"C++ \u8fd8\u6709\u53e6\u4e00\u79cd\u79f0\u4e3a static_assert \u7684 assert \u7c7b\u578b\u3002 static_assert \u662f\u5728\u7f16\u8bd1\u65f6\u800c\u4e0d\u662f\u8fd0\u884c\u65f6\u68c0\u67e5\u7684\u65ad\u8a00\uff0c\u4e00\u4e2a\u5931\u8d25\u7684 static_assert \u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002\u4e0e assert \u4e0d\u540c\uff0cassert \u662f\u5728\u5934\u6587\u4ef6\u4e2d\u58f0\u660e\u7684\uff0c\u800c static_assert \u662f\u4e00\u4e2a\u5173\u952e\u5b57\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u5305\u542b\u5934\u6587\u4ef6\u624d\u80fd\u4f7f\u7528\u5b83\u3002 static_assert \u7684\u5f62\u5f0f\u5982\u4e0b\uff1a 1 static_assert ( condition , diagnostic_message ) \u5982\u679c\u6761\u4ef6\u4e0d\u6210\u7acb\uff0c\u5c31\u4f1a\u6253\u5370\u51fa\u8bca\u65ad\u4fe1\u606f\u3002\u4e0b\u9762\u662f\u4f7f\u7528 static_assert \u786e\u4fdd\u7c7b\u578b\u5177\u6709\u7279\u5b9a\u5927\u5c0f\u7684\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 static_assert ( sizeof ( long ) == 8 , \"long must be 8 bytes\" ); static_assert ( sizeof ( int ) == 4 , \"int must be 4 bytes\" ); int main () { return 0 ; } \u5728\u4f5c\u8005\u7684\u673a\u5668\u4e0a\uff0c\u7f16\u8bd1\u65f6\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\uff1a 1 1>c:\\consoleapplication1\\main.cpp(19): error C2338: long must be 8 bytes \u56e0\u4e3a static_assert \u662f\u7531\u7f16\u8bd1\u5668\u8bc4\u4f30\u7684\uff0c\u6240\u4ee5\u6761\u4ef6\u5fc5\u987b\u80fd\u591f\u5728\u7f16\u8bd1\u65f6\u8fdb\u884c\u8bc4\u4f30\u3002\u6b64\u5916\uff0c\u4e0e\u6b63\u5e38\u7684 assert \u4e0d\u540c(\u5728\u8fd0\u884c\u65f6\u8bc4\u4f30)\uff0c static_assert \u53ef\u4ee5\u653e\u7f6e\u5728\u4ee3\u7801\u6587\u4ef6\u4e2d\u7684\u4efb\u4f55\u4f4d\u7f6e(\u751a\u81f3\u662f\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d)\u3002 \u5728 C++17 \u4e4b\u524d\uff0c\u8bca\u65ad\u6d88\u606f\u5fc5\u987b\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u53c2\u6570\u63d0\u4f9b\u3002\u81ea C++17 \u4ee5\u6765\uff0c\u63d0\u4f9b\u8bca\u65ad\u6d88\u606f\u662f\u53ef\u9009\u7684\u3002","title":"static_assert"},{"location":"7-18-introduction-to-random-number-generation/","text":"Key Takeaway The ability to generate random numbers can be useful in certain kinds of programs, particularly in games, statistical modelling programs, and cryptographic applications that need to encrypt and decrypt things. Take games for example -- without random events, monsters would always attack you the same way, you\u2019d always find the same treasure, the dungeon layout would never change, etc\u2026 and that would not make for a very good game. In real life, we often produce randomization by doing things like flipping a coin, rolling a dice, or shuffling a deck of cards. These events aren\u2019t actually random, but involve so many physical variables (e.g. gravity, friction, air resistance, momentum, etc\u2026) that they become almost impossible to predict or control, and (unless you\u2019re a magician) produce results that are for all intents and purposes random. However, computers aren\u2019t designed to take advantage of physical variables -- your computer can\u2019t toss a coin, throw a dice, or shuffle real cards. Modern computers live in a controlled electrical world where everything is binary (0 or 1) and there is no in-between. By their very nature, computers are designed to produce results that are as predictable as possible. When you tell the computer to calculate 2 + 2, you always want the answer to be 4. Not 3 or 5 on occasion. Consequently, computers are generally incapable of generating truly random numbers (at least through software). Instead, modern programs typically _simulate_randomness using an algorithm. In this lesson, we\u2019ll cover a lot of the theory behind how random numbers are generated in programs, and introduce some terminology we\u2019ll use in future lessons. \u7b97\u6cd5\u548c\u72b6\u6001 First, let\u2019s take a detour through the concepts of algorithms and states. An algorithm is a finite sequence of instructions that can be followed to solve some problem or produce some useful result. For example, let\u2019s say your boss gives you a small text file containing a bunch of unsorted names (one per line), and asks you to sort the list. Since the list is small, and you don\u2019t expect to do this often, you decide to sort it by hand. There are multiple ways to sort a list, but you might do something like this: Create a new empty list to hold the sorted results Scan the list of unsorted names to find the name that comes first alphabetically Cut that name out of the unsorted list and paste it at the bottom of the sorted list Repeat the previous two steps until there are no more names on the unsorted list The above set of steps describes a sorting algorithm (using natural language). By nature, algorithms are reusable -- if your boss asks you to sort another list tomorrow, you can just apply the same algorithm to the new list. Because computers can execute instructions and manipulate data much more quickly than we can, algorithms are often written using programming languages, allowing us to automate tasks. In C++, algorithms are typically implemented as reusable functions. Here\u2019s a simple algorithm for generating a sequence of numbers where each successive number is incremented by 1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int plusOne () { static int s_state { 3 }; // only initialized the first time this function is called // Generate the next number ++ s_state ; // first we modify the state return s_state ; // then we use the new state to generate the next number in the sequence } int main () { std :: cout << plusOne () << '\\n' ; std :: cout << plusOne () << '\\n' ; std :: cout << plusOne () << '\\n' ; return 0 ; } COPY This prints: 1 2 3 4 5 6 This algorithm is pretty simple. The first time we call plusOne() , s_state is initialized to value 3 . Then the next number in the sequence is generated and returned. An algorithm is considered to be stateful if it retains some information across calls. Conversely, a stateless algorithm does not store any information (and must be given all the information it needs to work with when it is called). Our plusOne() function is stateful, in that it uses the static variable s_state to store the last number that was generated. When applied to algorithms, the term state refers to the current values held in stateful variables. To generate the next number in the sequence, our algorithm uses a two step process: First, the current state (initialized from the start value, or preserved from the prior call) is modified to produce a new state. Then, the next number in the sequence is generated from the new state. Our algorithm is considered deterministic, meaning that for a given input (the value provided for start ), it will always produce the same output sequence. \u4f2a\u968f\u673a\u6570\u751f\u6210\u5668 (PRNGs) To simulate randomness, programs typically use a pseudo-random number generator. A pseudo-random number generator (PRNG) is an algorithm that generates a sequence of numbers whose properties simulate a sequence of random numbers. It\u2019s easy to write a basic PRNG algorithm. Here\u2019s a short PRNG example that generates 100 16-bit pseudo-random numbers: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include // For illustrative purposes only, don't use this unsigned int LCG16 () // our PRNG { static unsigned int s_state { 5323 }; // Generate the next number // Due to our use of large constants and overflow, it would be // hard for someone to casually predict what the next number is // going to be from the previous one. s_state = 8253729 * s_state + 2396403 ; // first we modify the state return s_state % 32768 ; // then we use the new state to generate the next number in the sequence } int main () { // Print 100 random numbers for ( int count { 1 }; count <= 100 ; ++ count ) { std :: cout << LCG16 () << '\\t' ; // If we've printed 10 numbers, start a new row if ( count % 10 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY The result of this program is: 1 2 3 4 5 6 7 8 9 10 23070 27857 22756 10839 27946 11613 30448 21987 22070 1001 27388 5999 5442 28789 13576 28411 10830 29441 21780 23687 5466 2957 19232 24595 22118 14873 5932 31135 28018 32421 14648 10539 23166 22833 12612 28343 7562 18877 32592 19011 13974 20553 9052 15311 9634 27861 7528 17243 27310 8033 28020 24807 1466 26605 4992 5235 30406 18041 3980 24063 15826 15109 24984 15755 23262 17809 2468 13079 19946 26141 1968 16035 5878 7337 23484 24623 13826 26933 1480 6075 11022 19393 1492 25927 30234 17485 23520 18643 5926 21209 2028 16991 3634 30565 2552 20971 23358 12785 25092 30583 Each number appears to be pretty random with respect to the previous one. Notice how similar LCG16() is to our plusOne() example above! We can pass LCG16() an initial value that is used to initialize the state. Then to produce the next number in the output sequence, the current state is modified (by applying some mathematical operations) to produce a new state, and the next number in the sequence is generated from that new state. As it turns out, this particular algorithm isn\u2019t very good as a random number generator. But most PRNGs work similarly to LCG16() -- they just typically use more state variables and more complex mathematical operations in order to generate better quality results. PRNG \u7684\u79cd\u5b50 The sequence of \u201crandom numbers\u201d generated by a PRNG is not random at all. Just like our plusOne() function, LCG16() is also deterministic. Once the state has been initialized, LCG16() (and all other PRNGs) will generate the same output sequence. When a PRNG is instantiated, an initial value (or set of values) called a random seed (or seed for short) can be provided to initialize the state of the PRNG. When a PRNG has been initialized with a seed, we say it has been seeded. \u5173\u952e\u4fe1\u606f All of the values that a PRNG will produce are deterministically calculated from the seed value(s). Most PRNGs that produce quality results use at least 16 bytes of state, if not significantly more. However, the size of the seed value can be smaller than the size of the state of the PRNG. When this happens, we say the PRNG has been underseeded. Ideally, every bit in the state is initialized from a seed of equal size, and every bit in the seed has been independently determined somehow. However, if a PRNG is underseeded, some number of bits in the state will need to be initialized from the same bits in the seed. If a PRNG is significantly underseeded (meaning the size of the seed is much smaller than the size of the state), the quality of the random results the PRNG produces can be impacted. \u4ec0\u4e48\u6837\u7684PRNG\u662f\u597dPRNG\uff1f(\u9009\u8bfb) In order to be a good PRNG, the PRNG needs to exhibit a number of properties: The PRNG should generate each number with approximately the same probability. This is called distribution uniformity. If some numbers are generated more often than others, the result of the program that uses the PRNG will be biased! To check distribution uniformity, we can use a histogram. A histogram is a graph that tracks how many times each number has been generated. Since our histograms are text-based, we\u2019ll use a * symbol to represent each time a given number was generated. Consider a PRNG that generates numbers between 1 and 6. If we generate 36 numbers, a PRNG with distribution uniformity should generate a histogram that looks something like this: 1 2 3 4 5 6 1|****** 2|****** 3|****** 4|****** 5|****** 6|****** A PRNG that is biased in some way will generate a histogram that is uneven, like this: 1 2 3 4 5 6 1|*** 2|****** 3|****** 4|****** 5|****** 6|********* or this: 1 2 3 4 5 6 1|**** 2|******** 3|****** 4|******** 5|****** 6|**** Let\u2019s say you\u2019re trying to write a random item generator for a game. When a monster is killed, your code generates a random number between 1 and 6, and if the result is a 6, the monster will drop a rare item instead of a common one. You would expect a 1 in 6 chance of this happening. But if the underlying PRNG is not uniform, and generates a lot more 6s than it should (like the second histogram above), your players will end up getting more rare items than you\u2019d intended, possibly trivializing the difficulty of your game, or messing up your in-game economy. Finding PRNG algorithms that produce uniform results is difficult. The method by which the next number in the sequence is generated shouldn\u2019t be predictable. For example, consider the following PRNG algorithm: return ++num . This PRNG is perfectly uniform, but it is also completely predictable -- and not very useful as a sequence of random numbers! Even sequences of numbers that seem random to the eye (such as the output of LCG16() above) may be trivially predictable by someone who is motivated. By examining just a few numbers generated from the LCG16() function above, it is possible to determine which constants are used ( 8253729 and 2396403 ) to modify the state. Once that is known, it becomes trivial to calculate all of the future numbers that will be generated from this PRNG. Now, imagine you\u2019re running a betting website where users can bet $100. Your website then generates a random number between 0 and 32767. If the number is greater than 20000, the customer wins and you pay them double. Otherwise, they lose. Since the customer wins only 12767/32767 (39%) of the time, your website should make tons of money, right? However, if customers are able to determine which numbers will be generated next, then they can strategically place bets so they always (or usually) win. Congrats, now you get to file for bankruptcy! The PRNG should have a good dimensional distribution of numbers. This means the PRNG should return numbers across the entire range of possible results at random. For example, the PRNG should generate low numbers, middle numbers, high numbers, even numbers, and odd numbers seemingly at random. A PRNG that returned all low numbers, then all high numbers may be uniform and non-predictable, but it\u2019s still going to lead to biased results, particularly if the number of random numbers you actually use is small. The PRNG should have a high period for all seeds All PRNGs are periodic, which means that at some point the sequence of numbers generated will begin to repeat itself. The length of the sequence before a PRNG begins to repeat itself is known as the period. For example, here are the first 100 numbers generated from a PRNG with poor periodicity: 1 2 3 4 5 6 7 8 9 10 112 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 You will note that it generated 9 as the 2nd number, again as the 16th number, and then every 14 numbers after that. This PRNG is stuck generating the following sequence repeatedly: 9-130-97-64-31-152-119-86-53-20-141-108-75-42-(repeat) . This happens because PRNGs are deterministic. Once the state of a PRNG is identical to a prior state, the PRNG will start producing the same sequence of outputs it has produced before -- resulting in a loop. A good PRNG should have a long period for all seed numbers. Designing an algorithm that meets this property can be extremely difficult -- many PRNGs have long periods only for some seeds and not others. If the user happens to pick a seed that results in a state with a short period, then the PRNG won\u2019t do a good job if many random numbers are needed. The PRNG should be efficient Most PRNGs have a state size of less than 4096 bytes, so total memory usage typically isn\u2019t a concern. However, the larger the internal state, the more likely the PRNG is to be underseeded, and the slower the initial seeding will be (since there\u2019s more state to initialize). Second, to generate the next number in sequence, a PRNG has to mix up its internal state by applying various mathematical operations. How much time this takes can vary significantly by PRNG and also by architecture (some PRNGs perform better on certain architectures than others). This doesn\u2019t matter if you only generate random numbers periodically, but can have a huge impact if you need lots of randomness. \u591a\u79cd\u4e0d\u540c\u7684 PRNG \u7b97\u6cd5 Over the years, many different kinds of PRNG algorithms have been developed (Wikipedia has a good list here ). Every PRNG algorithm has strengths and weaknesses that might make it more or less suitable for a particular applications, so selecting the right algorithm for your application is important. Many PRNGs are now considered relatively poor by modern standards -- and there\u2019s no reason to use a PRNG that doesn\u2019t perform well when it\u2019s just as easy to use one that does. C++ \u4e2d\u7684\u968f\u673a\u5316 The randomization capabilities in C++ are accessible via the header of the standard library. Within the random library, there are 6 PRNG families available for use (as of C++20): Type name Family Period State size* Performance Quality Should I use this? minstd_rand minstd_rand0 Linear congruential generator 2^31 4 bytes Bad Awful No mt19937 mt19937_64 Mersenne twister 2^19937 2500 bytes Decent Decent Probably (see next section) ranlux24 ranlux48 Subtract and carry 10^171 96 bytes Awful Good No knuth_b Shuffled linear congruential generator 2^31 1028 bytes Awful Bad No default_random_engine Any of above (implementation defined) Varies Varies ? ? No2 rand() Linear congruential generator 2^31 4 bytes Bad Awful Nono There is zero reason to use knuth_b , default_random_engine , or rand() (which is a random number generator provided for compatibility with C). As of C++20, the Mersenne Twister algorithm is the only PRNG that ships with C++ that has both decent performance and quality. \u6269\u5c55\u9605\u8bfb A test called PracRand is often used to assess the performance and quality of PRNGs (to determine whether they have different kinds of biases). You may also see references to SmallCrush, Crush or BigCrush -- these are other tests that are sometimes used for the same purpose. If you want to see what the output of Pracrand looks like, this website has output for all of the PRNGs that C++ supports as of C++20. \u6240\u4ee5\u6211\u4eec\u5e94\u8be5\u4f7f\u7528 Mersenne Twister\uff0c\u5bf9\u5417\uff1f Probably. For most applications, Mersenne Twister is fine, both in terms of performance and quality. However, it\u2019s worth noting that by modern PRNG standards, Mersenne Twister is a bit outdated . The biggest issue with Mersenne Twister is that its results can be predicted after seeing 624 generated numbers, making it non-suitable for any application that requires non-predictability. If you are developing an application that requires the highest quality random results (e.g. a statistical simulation), the fastest results, or one where non-predictability is important (e.g. cryptography), you\u2019ll need to use a 3rd party library. Popular choices as of the time of writing: The Xoshiro family and Wyrand for non-cryptographic PRNGs. The Chacha family for cryptographic (non-predictable) PRNGs. Okay, now that your eyes are probably bleeding, that\u2019s enough theory. Let\u2019s discuss how to actually generate random numbers with Mersenne Twister in C++.","title":"7.18 - \u968f\u673a\u6570\u751f\u6210\u5668\u7b80\u4ecb"},{"location":"7-18-introduction-to-random-number-generation/#_1","text":"First, let\u2019s take a detour through the concepts of algorithms and states. An algorithm is a finite sequence of instructions that can be followed to solve some problem or produce some useful result. For example, let\u2019s say your boss gives you a small text file containing a bunch of unsorted names (one per line), and asks you to sort the list. Since the list is small, and you don\u2019t expect to do this often, you decide to sort it by hand. There are multiple ways to sort a list, but you might do something like this: Create a new empty list to hold the sorted results Scan the list of unsorted names to find the name that comes first alphabetically Cut that name out of the unsorted list and paste it at the bottom of the sorted list Repeat the previous two steps until there are no more names on the unsorted list The above set of steps describes a sorting algorithm (using natural language). By nature, algorithms are reusable -- if your boss asks you to sort another list tomorrow, you can just apply the same algorithm to the new list. Because computers can execute instructions and manipulate data much more quickly than we can, algorithms are often written using programming languages, allowing us to automate tasks. In C++, algorithms are typically implemented as reusable functions. Here\u2019s a simple algorithm for generating a sequence of numbers where each successive number is incremented by 1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int plusOne () { static int s_state { 3 }; // only initialized the first time this function is called // Generate the next number ++ s_state ; // first we modify the state return s_state ; // then we use the new state to generate the next number in the sequence } int main () { std :: cout << plusOne () << '\\n' ; std :: cout << plusOne () << '\\n' ; std :: cout << plusOne () << '\\n' ; return 0 ; } COPY This prints: 1 2 3 4 5 6 This algorithm is pretty simple. The first time we call plusOne() , s_state is initialized to value 3 . Then the next number in the sequence is generated and returned. An algorithm is considered to be stateful if it retains some information across calls. Conversely, a stateless algorithm does not store any information (and must be given all the information it needs to work with when it is called). Our plusOne() function is stateful, in that it uses the static variable s_state to store the last number that was generated. When applied to algorithms, the term state refers to the current values held in stateful variables. To generate the next number in the sequence, our algorithm uses a two step process: First, the current state (initialized from the start value, or preserved from the prior call) is modified to produce a new state. Then, the next number in the sequence is generated from the new state. Our algorithm is considered deterministic, meaning that for a given input (the value provided for start ), it will always produce the same output sequence.","title":"\u7b97\u6cd5\u548c\u72b6\u6001"},{"location":"7-18-introduction-to-random-number-generation/#prngs","text":"To simulate randomness, programs typically use a pseudo-random number generator. A pseudo-random number generator (PRNG) is an algorithm that generates a sequence of numbers whose properties simulate a sequence of random numbers. It\u2019s easy to write a basic PRNG algorithm. Here\u2019s a short PRNG example that generates 100 16-bit pseudo-random numbers: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include // For illustrative purposes only, don't use this unsigned int LCG16 () // our PRNG { static unsigned int s_state { 5323 }; // Generate the next number // Due to our use of large constants and overflow, it would be // hard for someone to casually predict what the next number is // going to be from the previous one. s_state = 8253729 * s_state + 2396403 ; // first we modify the state return s_state % 32768 ; // then we use the new state to generate the next number in the sequence } int main () { // Print 100 random numbers for ( int count { 1 }; count <= 100 ; ++ count ) { std :: cout << LCG16 () << '\\t' ; // If we've printed 10 numbers, start a new row if ( count % 10 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY The result of this program is: 1 2 3 4 5 6 7 8 9 10 23070 27857 22756 10839 27946 11613 30448 21987 22070 1001 27388 5999 5442 28789 13576 28411 10830 29441 21780 23687 5466 2957 19232 24595 22118 14873 5932 31135 28018 32421 14648 10539 23166 22833 12612 28343 7562 18877 32592 19011 13974 20553 9052 15311 9634 27861 7528 17243 27310 8033 28020 24807 1466 26605 4992 5235 30406 18041 3980 24063 15826 15109 24984 15755 23262 17809 2468 13079 19946 26141 1968 16035 5878 7337 23484 24623 13826 26933 1480 6075 11022 19393 1492 25927 30234 17485 23520 18643 5926 21209 2028 16991 3634 30565 2552 20971 23358 12785 25092 30583 Each number appears to be pretty random with respect to the previous one. Notice how similar LCG16() is to our plusOne() example above! We can pass LCG16() an initial value that is used to initialize the state. Then to produce the next number in the output sequence, the current state is modified (by applying some mathematical operations) to produce a new state, and the next number in the sequence is generated from that new state. As it turns out, this particular algorithm isn\u2019t very good as a random number generator. But most PRNGs work similarly to LCG16() -- they just typically use more state variables and more complex mathematical operations in order to generate better quality results.","title":"\u4f2a\u968f\u673a\u6570\u751f\u6210\u5668 (PRNGs)"},{"location":"7-18-introduction-to-random-number-generation/#prng","text":"The sequence of \u201crandom numbers\u201d generated by a PRNG is not random at all. Just like our plusOne() function, LCG16() is also deterministic. Once the state has been initialized, LCG16() (and all other PRNGs) will generate the same output sequence. When a PRNG is instantiated, an initial value (or set of values) called a random seed (or seed for short) can be provided to initialize the state of the PRNG. When a PRNG has been initialized with a seed, we say it has been seeded. \u5173\u952e\u4fe1\u606f All of the values that a PRNG will produce are deterministically calculated from the seed value(s). Most PRNGs that produce quality results use at least 16 bytes of state, if not significantly more. However, the size of the seed value can be smaller than the size of the state of the PRNG. When this happens, we say the PRNG has been underseeded. Ideally, every bit in the state is initialized from a seed of equal size, and every bit in the seed has been independently determined somehow. However, if a PRNG is underseeded, some number of bits in the state will need to be initialized from the same bits in the seed. If a PRNG is significantly underseeded (meaning the size of the seed is much smaller than the size of the state), the quality of the random results the PRNG produces can be impacted.","title":"PRNG \u7684\u79cd\u5b50"},{"location":"7-18-introduction-to-random-number-generation/#prngprng","text":"In order to be a good PRNG, the PRNG needs to exhibit a number of properties: The PRNG should generate each number with approximately the same probability. This is called distribution uniformity. If some numbers are generated more often than others, the result of the program that uses the PRNG will be biased! To check distribution uniformity, we can use a histogram. A histogram is a graph that tracks how many times each number has been generated. Since our histograms are text-based, we\u2019ll use a * symbol to represent each time a given number was generated. Consider a PRNG that generates numbers between 1 and 6. If we generate 36 numbers, a PRNG with distribution uniformity should generate a histogram that looks something like this: 1 2 3 4 5 6 1|****** 2|****** 3|****** 4|****** 5|****** 6|****** A PRNG that is biased in some way will generate a histogram that is uneven, like this: 1 2 3 4 5 6 1|*** 2|****** 3|****** 4|****** 5|****** 6|********* or this: 1 2 3 4 5 6 1|**** 2|******** 3|****** 4|******** 5|****** 6|**** Let\u2019s say you\u2019re trying to write a random item generator for a game. When a monster is killed, your code generates a random number between 1 and 6, and if the result is a 6, the monster will drop a rare item instead of a common one. You would expect a 1 in 6 chance of this happening. But if the underlying PRNG is not uniform, and generates a lot more 6s than it should (like the second histogram above), your players will end up getting more rare items than you\u2019d intended, possibly trivializing the difficulty of your game, or messing up your in-game economy. Finding PRNG algorithms that produce uniform results is difficult. The method by which the next number in the sequence is generated shouldn\u2019t be predictable. For example, consider the following PRNG algorithm: return ++num . This PRNG is perfectly uniform, but it is also completely predictable -- and not very useful as a sequence of random numbers! Even sequences of numbers that seem random to the eye (such as the output of LCG16() above) may be trivially predictable by someone who is motivated. By examining just a few numbers generated from the LCG16() function above, it is possible to determine which constants are used ( 8253729 and 2396403 ) to modify the state. Once that is known, it becomes trivial to calculate all of the future numbers that will be generated from this PRNG. Now, imagine you\u2019re running a betting website where users can bet $100. Your website then generates a random number between 0 and 32767. If the number is greater than 20000, the customer wins and you pay them double. Otherwise, they lose. Since the customer wins only 12767/32767 (39%) of the time, your website should make tons of money, right? However, if customers are able to determine which numbers will be generated next, then they can strategically place bets so they always (or usually) win. Congrats, now you get to file for bankruptcy! The PRNG should have a good dimensional distribution of numbers. This means the PRNG should return numbers across the entire range of possible results at random. For example, the PRNG should generate low numbers, middle numbers, high numbers, even numbers, and odd numbers seemingly at random. A PRNG that returned all low numbers, then all high numbers may be uniform and non-predictable, but it\u2019s still going to lead to biased results, particularly if the number of random numbers you actually use is small. The PRNG should have a high period for all seeds All PRNGs are periodic, which means that at some point the sequence of numbers generated will begin to repeat itself. The length of the sequence before a PRNG begins to repeat itself is known as the period. For example, here are the first 100 numbers generated from a PRNG with poor periodicity: 1 2 3 4 5 6 7 8 9 10 112 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 130 97 64 31 152 119 86 53 20 141 108 75 42 9 You will note that it generated 9 as the 2nd number, again as the 16th number, and then every 14 numbers after that. This PRNG is stuck generating the following sequence repeatedly: 9-130-97-64-31-152-119-86-53-20-141-108-75-42-(repeat) . This happens because PRNGs are deterministic. Once the state of a PRNG is identical to a prior state, the PRNG will start producing the same sequence of outputs it has produced before -- resulting in a loop. A good PRNG should have a long period for all seed numbers. Designing an algorithm that meets this property can be extremely difficult -- many PRNGs have long periods only for some seeds and not others. If the user happens to pick a seed that results in a state with a short period, then the PRNG won\u2019t do a good job if many random numbers are needed. The PRNG should be efficient Most PRNGs have a state size of less than 4096 bytes, so total memory usage typically isn\u2019t a concern. However, the larger the internal state, the more likely the PRNG is to be underseeded, and the slower the initial seeding will be (since there\u2019s more state to initialize). Second, to generate the next number in sequence, a PRNG has to mix up its internal state by applying various mathematical operations. How much time this takes can vary significantly by PRNG and also by architecture (some PRNGs perform better on certain architectures than others). This doesn\u2019t matter if you only generate random numbers periodically, but can have a huge impact if you need lots of randomness.","title":"\u4ec0\u4e48\u6837\u7684PRNG\u662f\u597dPRNG\uff1f(\u9009\u8bfb)"},{"location":"7-18-introduction-to-random-number-generation/#prng_1","text":"Over the years, many different kinds of PRNG algorithms have been developed (Wikipedia has a good list here ). Every PRNG algorithm has strengths and weaknesses that might make it more or less suitable for a particular applications, so selecting the right algorithm for your application is important. Many PRNGs are now considered relatively poor by modern standards -- and there\u2019s no reason to use a PRNG that doesn\u2019t perform well when it\u2019s just as easy to use one that does.","title":"\u591a\u79cd\u4e0d\u540c\u7684 PRNG \u7b97\u6cd5"},{"location":"7-18-introduction-to-random-number-generation/#c","text":"The randomization capabilities in C++ are accessible via the header of the standard library. Within the random library, there are 6 PRNG families available for use (as of C++20): Type name Family Period State size* Performance Quality Should I use this? minstd_rand minstd_rand0 Linear congruential generator 2^31 4 bytes Bad Awful No mt19937 mt19937_64 Mersenne twister 2^19937 2500 bytes Decent Decent Probably (see next section) ranlux24 ranlux48 Subtract and carry 10^171 96 bytes Awful Good No knuth_b Shuffled linear congruential generator 2^31 1028 bytes Awful Bad No default_random_engine Any of above (implementation defined) Varies Varies ? ? No2 rand() Linear congruential generator 2^31 4 bytes Bad Awful Nono There is zero reason to use knuth_b , default_random_engine , or rand() (which is a random number generator provided for compatibility with C). As of C++20, the Mersenne Twister algorithm is the only PRNG that ships with C++ that has both decent performance and quality. \u6269\u5c55\u9605\u8bfb A test called PracRand is often used to assess the performance and quality of PRNGs (to determine whether they have different kinds of biases). You may also see references to SmallCrush, Crush or BigCrush -- these are other tests that are sometimes used for the same purpose. If you want to see what the output of Pracrand looks like, this website has output for all of the PRNGs that C++ supports as of C++20.","title":"C++\u00a0\u4e2d\u7684\u968f\u673a\u5316"},{"location":"7-18-introduction-to-random-number-generation/#mersenne-twister","text":"Probably. For most applications, Mersenne Twister is fine, both in terms of performance and quality. However, it\u2019s worth noting that by modern PRNG standards, Mersenne Twister is a bit outdated . The biggest issue with Mersenne Twister is that its results can be predicted after seeing 624 generated numbers, making it non-suitable for any application that requires non-predictability. If you are developing an application that requires the highest quality random results (e.g. a statistical simulation), the fastest results, or one where non-predictability is important (e.g. cryptography), you\u2019ll need to use a 3rd party library. Popular choices as of the time of writing: The Xoshiro family and Wyrand for non-cryptographic PRNGs. The Chacha family for cryptographic (non-predictable) PRNGs. Okay, now that your eyes are probably bleeding, that\u2019s enough theory. Let\u2019s discuss how to actually generate random numbers with Mersenne Twister in C++.","title":"\u6240\u4ee5\u6211\u4eec\u5e94\u8be5\u4f7f\u7528 Mersenne Twister\uff0c\u5bf9\u5417\uff1f"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/","text":"Key Takeaway In the previous lesson 7.18 -- Introduction to random number generation , we introduced the concept of random number generation, and discussed how PRNG algorithms are typically used to simulate randomness in programs. In this lesson, we\u2019ll take a look at how to generate random numbers in your programs. To access any of the randomization capabilities in C++, we include the header of the standard library. \u5728 C++ \u4e2d\u4f7f\u7528 Mersenne Twister \u751f\u6210\u968f\u673a\u6570 The Mersenne Twister PRNG, besides having a great name, is probably the most popular PRNG across all programming languages. Although it is a bit old by today\u2019s standards, it generally produces quality results and has decent performance. The random library has support for two Mersenne Twister types: mt19937 is a Mersenne Twister that generates 32-bit unsigned integers mt19937_64 is a Mersenne Twister that generates 64-bit unsigned integers Using Mersenne Twister is straightforward: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include // for std::mt19937 int main () { std :: mt19937 mt ; // Instantiate a 32-bit Mersenne Twister // Print a bunch of random numbers for ( int count { 1 }; count <= 40 ; ++ count ) { std :: cout << mt () << '\\t' ; // generate a random number // If we've printed 5 numbers, start a new row if ( count % 5 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY This produces the result: 1 2 3 4 5 6 7 8 3499211612 581869302 3890346734 3586334585 545404204 4161255391 3922919429 949333985 2715962298 1323567403 418932835 2350294565 1196140740 809094426 2348838239 4264392720 4112460519 4279768804 4144164697 4156218106 676943009 3117454609 4168664243 4213834039 4111000746 471852626 2084672536 3427838553 3437178460 1275731771 609397212 20544909 1811450929 483031418 3933054126 2747762695 3402504553 3772830893 4120988587 2163214728 First, we include the header, since that\u2019s where all the random number capabilities live. Next, we instantiate a 32-bit Mersenne Twister engine via the statement std::mt19937 mt . Then, each time we want to generate a random 32-bit unsigned integer, we call mt() . \u5c0f\u8d34\u58eb Since mt is a variable, you may be wondering what mt() means. In lesson 4.17 -- Introduction to std::string , we showed an example where we called the function name.length() , which invoked the length() function on std::string variable name . mt() is a concise syntax for calling the function mt.operator() , which for these PRNG types has been defined to return the next random result in the sequence. The advantage of using operator() instead of a named function is that we don\u2019t need to remember the function\u2019s name, and the concise syntax is less typing. \u4f7f\u7528 Mersenne Twister \u6295\u63b7\u9ab0\u5b50 A 32-bit PRNG will generate random numbers between 0 and 4,294,967,295, but we do not always want numbers in that range. If our program was simulating a board game or a dice game, we\u2019d probably want to simulate the roll of a 6-sided dice by generating random numbers between 1 and 6. If our program was a dungeon adventure, and the player had a sword that did between 7 and 11 damage to monsters, then we\u2019d want to generate random numbers between 7 and 11 whenever the player hit a monster. Unfortunately, PRNGs can\u2019t do this. They can only generate numbers that use the full range. What we need is some way to convert a number that is output from our PRNG into a value in the smaller range we want (with an even probability of each value occurring). While we could write a function to do this ourselves, doing so in a way that produces non-biased results is non-trivial. Fortunately, the random library can help us here, in the form of random number distributions. A random number distribution converts the output of a PRNG into some other distribution of numbers. \u9898\u5916\u8bdd For the stats geeks: a random number distribution is just a probability distribution designed to take PRNG values as input. The random library has many random numbers distributions, most of which you will never use unless you\u2019re doing some kind of statistical analysis. But there\u2019s one random number distribution that\u2019s extremely useful: a uniform distribution is a random number distribution that produces outputs between two numbers X and Y (inclusive) with equal probability. Here\u2019s a similar program to the one above, using a uniform distribution to simulate the roll of a 6-sided dice: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include // for std::mt19937 and std::uniform_int_distribution int main () { std :: mt19937 mt ; // Create a reusable random number generator that generates uniform numbers between 1 and 6 std :: uniform_int_distribution die6 { 1 , 6 }; // for C++14, use std::uniform_int_distribution<> die6{ 1, 6 }; // Print a bunch of random numbers for ( int count { 1 }; count <= 40 ; ++ count ) { std :: cout << die6 ( mt ) << '\\t' ; // generate a roll of the die here // If we've printed 10 numbers, start a new row if ( count % 10 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY This produces the result: 1 2 3 4 3 1 3 6 5 2 6 6 1 2 2 6 1 1 6 1 4 5 2 5 6 2 6 2 1 3 5 4 5 6 1 4 2 3 1 2 2 6 2 1 There are only two noteworthy differences in this example compared to the previous one. First, we\u2019ve created a uniform distribution variable (named die6 ) to generate numbers between 1 and 6. Second, instead of calling mt() to generate 32-bit unsigned integer random numbers, we\u2019re now calling die6(mt) to generate a value between 1 and 6. \u4e0a\u9762\u7684\u7a0b\u5e8f\u5e76\u4e0d\u50cf\u5b83\u770b\u4e0a\u53bb\u90a3\u6837\u968f\u673a Although the results of our dice rolling example above are pretty random, there\u2019s a major flaw with the program. Run the program 3 times and see if you can figure out what it is. Go ahead, we\u2019ll wait. Jeopardy music If you run the program multiple times, you will note that it prints the same numbers every time! While each number in the sequence is random with regards to the previous one, the entire sequence is not random at all! Each run of our program produces the exact same result. Imagine that you\u2019re writing a game of hi-lo, where the user has 10 tries to guess a number that has been picked randomly, and the computer tells the user whether their guess is too high or too low. If the computer picks the same random number every time, the game won\u2019t be interesting past the first time it is played. So let\u2019s take a deeper look at why this is happening, and how we can fix it. In the prior lesson ( 7.18 - \u968f\u673a\u6570\u751f\u6210\u5668\u7b80\u4ecb ), we covered that each number in a PRNG sequence is in a deterministic way. And that the state of the PRNG is initialized from the seed value. Thus, given any starting seed number, PRNGs will always generate the same sequence of numbers from that seed as a result. Because we are default initializing our Mersenne Twister, it is being initialized with the same seed every time the program is run. And because the seed is the same, the random numbers being generated are also the same. In order to make our entire sequence randomized differently each time the program is run, we need to pick a seed that\u2019s not a fixed number. The first answer that probably comes to mind is that we need a random number for our seed! That\u2019s a good thought, but if we need a random number to generate random numbers, then we\u2019re in a catch-22. It turns out, we really don\u2019t need our seed to be a random number -- we just need to pick something that changes each time the program is run. Then we can use our PRNG to generate a unique sequence of pseudo-random numbers from that seed. There are two methods that are commonly used to do this: Use the system clock Use the system\u2019s random device \u4f7f\u7528\u7cfb\u7edf\u65f6\u95f4\u4f5c\u4e3a\u968f\u673a\u6570\u79cd\u5b50 What\u2019s one thing that\u2019s different every time you run your program? Unless you manage to run your program twice at exactly the same moment in time, the answer is that the current time is different. Therefore, if we use the current time as our seed value, then our program will produce a different set of random numbers each time it is run. C and C++ have a long history of PRNGs being seeded using the current time (using the std::time() function), so you will probably see this in a lot of existing code. Fortunately, C++ has a high resolution clock that we can use to generate a seed value. To minimize the chance of two time values being identical if the program is run quickly in succession, we want to use some time measure that changes as quickly as possible. For this, we\u2019ll ask the clock how much time has passed since the earliest time it can measure. This time is measured in \u201cticks\u201d, which is a very small unit of time (usually nanoseconds, but could be milliseconds). 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include // for std::mt19937 #include // for std::chrono int main () { // Seed our Mersenne Twister using the std :: mt19937 mt { static_cast < unsigned int > ( std :: chrono :: steady_clock :: now (). time_since_epoch (). count () ) }; // Create a reusable random number generator that generates uniform numbers between 1 and 6 std :: uniform_int_distribution die6 { 1 , 6 }; // for C++14, use std::uniform_int_distribution<> die6{ 1, 6 }; // Print a bunch of random numbers for ( int count { 1 }; count <= 40 ; ++ count ) { std :: cout << die6 ( mt ) << '\\t' ; // generate a roll of the die here // If we've printed 10 numbers, start a new row if ( count % 10 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY The above program has only two changes from the prior. First, we\u2019re including , which gives us access to the clock. Second, we\u2019re using the current time from the clock as a seed value for our Mersenne Twister. The results generated by this program should now be different each time it is run, which you can verify experimentally by running it several times. The downside of this approach is that if the program is run several times in quick succession, the seeds generated for each run won\u2019t be that different, which can impact the quality of the random results from a statistical standpoint. For normal programs, this doesn\u2019t matter, but for programs that require high quality, independent results, this method of seeding may be insufficient. \u5c0f\u8d34\u58eb std::chrono::high_resolution_clock is a popular choice instead of std::chrono::steady_clock . std::chrono::high_resolution_clock is the clock that uses the most granular unit of time, but it may use the system clock for the current time, which can be changed or rolled back by users. std::chrono::steady_clock may have a less granular tick time, but is the only clock with a guarantee that users can not adjust it. \u4f7f\u7528\u968f\u673a\u6570\u8bbe\u5907\u751f\u6210\u79cd\u5b50 The random library contains a type called std::random_device that is an implementation-defined PRNG. Normally we avoid implementation-defined capabilities because they have no guarantees about quality or portability, but this is one of the exception cases. Typically std::random_device will ask the OS for a random number (how it does this depends on the OS). 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include // for std::mt19937 and std::random_device int main () { std :: mt19937 mt { std :: random_device {}() }; // Create a reusable random number generator that generates uniform numbers between 1 and 6 std :: uniform_int_distribution die6 { 1 , 6 }; // for C++14, use std::uniform_int_distribution<> die6{ 1, 6 }; // Print a bunch of random numbers for ( int count { 1 }; count <= 40 ; ++ count ) { std :: cout << die6 ( mt ) << '\\t' ; // generate a roll of the die here // If we've printed 10 numbers, start a new row if ( count % 10 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY In the above program, we\u2019re seeding our Mersenne Twister with one random number generated from a temporary instance of std::random_device . If you run this program multiple times, it should also produce different results each time. One potential problem with std::random_device : it isn\u2019t required to be non-deterministic, meaning it could , on some systems, produce the same sequence every time the program is run, which is exactly what we\u2019re trying to avoid. There was a bug in MinGW (fixed in GCC 9.2) that would do exactly this, making std::random_device useless. However, the latest versions of the most popular compilers (GCC/MinGW, Clang, Visual Studio) support proper implementations of std::random_device . \u6700\u4f73\u5b9e\u8df5 Use std::random_device to seed your PRNGs (unless it\u2019s not implemented properly for your target compiler/architecture). Q: std::random_device{}() \u662f\u4ec0\u4e48\u610f\u601d? std::random_device{} creates an value-initialized temporary object of type std::random_device . The () then calls operator() on that temporary object, which returns a randomized value (which we use as an initializer for our Mersenne Twister) It\u2019s the equivalent of the calling the following function, which uses a syntax you should be more familiar with: 1 2 3 4 5 unsigned int getRandomDeviceValue () { std :: random_device rd {}; // create a value initialized std::random_device object return rd (); // return the result of operator() to the caller } COPY Using std::random_device{}() allows us to get the same result without creating a named function or named variable, so it\u2019s much more concise. Q: \u5982\u679c std::random_device \u672c\u8eab\u662f\u968f\u673a\u7684\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u76f4\u63a5\u7528\u5b83\u4ee3\u66ff Mersenne Twister \u5462\uff1f Because std::random_device is implementation defined, we can\u2019t assume much about it. It may be expensive to access or it may cause our program to pause while waiting for more random numbers to become available. The pool of numbers that it draws from may also be depleted quickly, which would impact the random results for other applications requesting random numbers via the same method. For this reason, std::random_device is better used to seed other PRNGs rather than as a PRNG itself. Only seed a PRNG once Many PRNGs can be reseeded after the initial seeding. This essentially re-initializes the state of the random number generator, causing it to generate results starting from the new seed state. Reseeding should generally be avoided unless you have a specific reason to do so, as it can cause the results to be less random, or not random at all. \u6700\u4f73\u5b9e\u8df5 Only seed a given pseudo-random number generator once, and do not reseed it. Here\u2019s an example of a common mistake that new programmers make: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include int getCard () { std :: mt19937 mt { std :: random_device {}() }; // this gets created and seeded every time the function is called std :: uniform_int_distribution card { 1 , 52 }; return card ( mt ); } int main () { std :: cout << getCard (); return 0 ; } COPY In the getCard() function, the random number generator is being created and seeded every time the function is called. This is inefficient at best, and will likely cause poor random results. \u8de8\u51fd\u6570\u7684\u968f\u673a\u6570 What happens if we want to use a random number generator in multiple functions? One way is to create (and seed) our PRNG in our main() function, and then pass it everywhere we need it. But that\u2019s a lot of passing for something we may only use sporadically, and in different places. Although you can create a static local std::mt19937 variable in each function that needs it (static so that it only gets seeded once), it\u2019s overkill to have every function that uses a random number generator define and seed its own local generator. A better option in most cases is to create a global random number generator (inside a namespace!). Remember how we told you to avoid non-const global variables? This is an exception. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include // for std::mt19937 and std::random_device namespace Random // capital R to avoid conflicts with functions named random() { std :: mt19937 mt { std :: random_device {}() }; int get ( int min , int max ) { std :: uniform_int_distribution die { min , max }; // we can create a distribution in any function that needs it return die ( mt ); // and then generate a random number from our global generator } } int main () { std :: cout << Random :: get ( 1 , 6 ) << '\\n' ; std :: cout << Random :: get ( 1 , 10 ) << '\\n' ; std :: cout << Random :: get ( 1 , 20 ) << '\\n' ; return 0 ; } COPY In the above example, Random::mt is a global variable that can be accessed from any function. We\u2019ve created Random::get() as an easy way to get a random number between min and max . std::uniform_int_distribution is typically cheap to create, so it\u2019s fine to create when we need it. Mersenne Twister \u548c underseeding issues The internal state of a Mersenne Twister is 624 bytes in size. In the examples above, where we seed from the clock or std::random_device , our seed is only a single 32-bit integer. This means we\u2019re essentially initializing a 624-byte object with a 4-byte value, which is significantly underseeding the Mersenne Twister PRNG. The random library does the best it can to fill in the remaining 620 bytes with \u201crandom\u201d data\u2026 but it can\u2019t work magic. Underseeded PRNG can generate results that are suboptimal for applications that need the highest quality results. For example, seeding std::mt19937 with a single 32-bit value will never generate the number 42 as its first output. So how do we fix this? As of C++20, there\u2019s no easy way. But we do have some suggestions. First, let\u2019s talk about std::seed_seq (which stands for \u201cseed sequence\u201d). In the prior lesson, we mentioned that a seed can be either a single value, or a set of values. std::seed_seq is a type that performs two functions. First, it can hold multiple seed values, so we can use it to seed our PRNG with more than one value. Second, it will generate as many additional unbiased seed values as needed to initialize a PRNG\u2019s state. So if you initialize std::seed_seq with a single 32-bit integer (e.g. from std::random_device) and then initialize a Mersenne Twister with the std::seed_seq object, std::seed_seq will generate 620 bytes of additional seed data. The results won\u2019t be amazingly high quality, but it\u2019s better than nothing. Now on to the ideas. First, the more pieces of random data we can give std::seed_seq to work with, the better. So the easiest idea is to simply use std::random_device to give std::seed_seq more data to work with. If we initialize std::seed_seq with 8 numbers from std::random_device instead of 1, then the remaining numbers generated by std::seed_seq should be that much better: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include int main () { std :: random_device rd ; std :: seed_seq ss { rd (), rd (), rd (), rd (), rd (), rd (), rd (), rd () }; // get 8 integers of random numbers from std::random_device for our seed std :: mt19937 mt { ss }; // initialize our Mersenne Twister with the std::seed_seq // Create a reusable random number generator that generates uniform numbers between 1 and 6 std :: uniform_int_distribution die6 { 1 , 6 }; // for C++14, use std::uniform_int_distribution<> die6{ 1, 6 }; // Print a bunch of random numbers for ( int count { 1 }; count <= 40 ; ++ count ) { std :: cout << die6 ( mt ) << '\\t' ; // generate a roll of the die here // If we've printed 10 numbers, start a new row if ( count % 10 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY This is pretty straightforward so there isn\u2019t much reason not to do this at a minimum. Q: Why not give std::seed_seq 156 integers (624 bytes) from std::random_device ? You can! However, this may be slow, and risks depleting the pool of random numbers that std::random_device uses. Second, you can use other \u201crandom\u201d inputs to std::seed_seq . We\u2019ve already shown you how to get a value from the clock, so you can throw that in easily. Other things that are sometimes used include the current thread id,the address of particular functions, the user\u2019s id, the process id, etc\u2026 Doing that is beyond the scope of this article, but this article has some context and a link to randutils.hpp that implements this. An alternate path is to use a different PRNG with a smaller state. Many good PRNGs use 64 or 128 bits of state, which can easily be initialized using std::seed_seq filled with 8 calls to std::random_device . PRNG \u70ed\u8eab When a PRNG is given a poor quality seed (or underseeded), the initial results of the PRNG may not be high quality. For this reason, some PRNGs benefit from being \u201cwarmed up\u201d, which is a technique where the first N results generated from the PRNG are discarded. This allows the internal state of the PRNG to be mixed up such that the subsequent results should be of higher quality. Typically a few hundred to a few thousand initial results are discarded. The longer the period of the PRNG, the more initial results should be discarded. \u9898\u5916\u8bdd Visual Studio\u2019s implementation of rand() had (or still has?) a bug where the first generated result would not be sufficiently randomized. You may see older programs that use rand() discard a single result as a way to avoid this issue. The seed_seq initialization used by std::mt19937 performs a warm up, so we don\u2019t need to explicitly warm up std::mt19937 objects. \u4f7f\u7528\u968f\u673a\u6570\u5e26\u6765\u7684debug\u96be\u9898 Programs that use random numbers can be difficult to debug because the program may exhibit different behaviors each time it is run. Sometimes it may work, and sometimes it may not. When debugging, it\u2019s helpful to ensure your program executes the same (incorrect) way each time. That way, you can run the program as many times as needed to isolate where the error is. For this reason, when debugging, it\u2019s a useful technique to seed your PRNG with a specific value (e.g. 5 ) that causes the erroneous behavior to occur. This will ensure your program generates the same results each time, making debugging easier. Once you\u2019ve found the error, you can use your normal seeding method to start generating randomized results again. \u968f\u673a\u5316 FAQ Q: Help! My random number generator is generating the same sequence of random numbers. If your random number generator is generating the same sequence of random numbers every time your program is run, you probably didn\u2019t seed it properly (or at all). Make sure you\u2019re seeding it with a value that changes each time the program is run. Q: Help! My random number generator keeps generating the same number over and over. If your random number generator is generating the same number every time you ask it for a random number, then you are probably either reseeding the random number generator before generating a random number, or you\u2019re creating a new random generator for each random number.","title":"7.19 - \u4f7f\u7528 Mersenne Twister \u751f\u6210\u968f\u673a\u6570"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#c-mersenne-twister","text":"The Mersenne Twister PRNG, besides having a great name, is probably the most popular PRNG across all programming languages. Although it is a bit old by today\u2019s standards, it generally produces quality results and has decent performance. The random library has support for two Mersenne Twister types: mt19937 is a Mersenne Twister that generates 32-bit unsigned integers mt19937_64 is a Mersenne Twister that generates 64-bit unsigned integers Using Mersenne Twister is straightforward: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include // for std::mt19937 int main () { std :: mt19937 mt ; // Instantiate a 32-bit Mersenne Twister // Print a bunch of random numbers for ( int count { 1 }; count <= 40 ; ++ count ) { std :: cout << mt () << '\\t' ; // generate a random number // If we've printed 5 numbers, start a new row if ( count % 5 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY This produces the result: 1 2 3 4 5 6 7 8 3499211612 581869302 3890346734 3586334585 545404204 4161255391 3922919429 949333985 2715962298 1323567403 418932835 2350294565 1196140740 809094426 2348838239 4264392720 4112460519 4279768804 4144164697 4156218106 676943009 3117454609 4168664243 4213834039 4111000746 471852626 2084672536 3427838553 3437178460 1275731771 609397212 20544909 1811450929 483031418 3933054126 2747762695 3402504553 3772830893 4120988587 2163214728 First, we include the header, since that\u2019s where all the random number capabilities live. Next, we instantiate a 32-bit Mersenne Twister engine via the statement std::mt19937 mt . Then, each time we want to generate a random 32-bit unsigned integer, we call mt() . \u5c0f\u8d34\u58eb Since mt is a variable, you may be wondering what mt() means. In lesson 4.17 -- Introduction to std::string , we showed an example where we called the function name.length() , which invoked the length() function on std::string variable name . mt() is a concise syntax for calling the function mt.operator() , which for these PRNG types has been defined to return the next random result in the sequence. The advantage of using operator() instead of a named function is that we don\u2019t need to remember the function\u2019s name, and the concise syntax is less typing.","title":"\u5728 C++ \u4e2d\u4f7f\u7528 Mersenne Twister \u751f\u6210\u968f\u673a\u6570"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#mersenne-twister","text":"A 32-bit PRNG will generate random numbers between 0 and 4,294,967,295, but we do not always want numbers in that range. If our program was simulating a board game or a dice game, we\u2019d probably want to simulate the roll of a 6-sided dice by generating random numbers between 1 and 6. If our program was a dungeon adventure, and the player had a sword that did between 7 and 11 damage to monsters, then we\u2019d want to generate random numbers between 7 and 11 whenever the player hit a monster. Unfortunately, PRNGs can\u2019t do this. They can only generate numbers that use the full range. What we need is some way to convert a number that is output from our PRNG into a value in the smaller range we want (with an even probability of each value occurring). While we could write a function to do this ourselves, doing so in a way that produces non-biased results is non-trivial. Fortunately, the random library can help us here, in the form of random number distributions. A random number distribution converts the output of a PRNG into some other distribution of numbers. \u9898\u5916\u8bdd For the stats geeks: a random number distribution is just a probability distribution designed to take PRNG values as input. The random library has many random numbers distributions, most of which you will never use unless you\u2019re doing some kind of statistical analysis. But there\u2019s one random number distribution that\u2019s extremely useful: a uniform distribution is a random number distribution that produces outputs between two numbers X and Y (inclusive) with equal probability. Here\u2019s a similar program to the one above, using a uniform distribution to simulate the roll of a 6-sided dice: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include // for std::mt19937 and std::uniform_int_distribution int main () { std :: mt19937 mt ; // Create a reusable random number generator that generates uniform numbers between 1 and 6 std :: uniform_int_distribution die6 { 1 , 6 }; // for C++14, use std::uniform_int_distribution<> die6{ 1, 6 }; // Print a bunch of random numbers for ( int count { 1 }; count <= 40 ; ++ count ) { std :: cout << die6 ( mt ) << '\\t' ; // generate a roll of the die here // If we've printed 10 numbers, start a new row if ( count % 10 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY This produces the result: 1 2 3 4 3 1 3 6 5 2 6 6 1 2 2 6 1 1 6 1 4 5 2 5 6 2 6 2 1 3 5 4 5 6 1 4 2 3 1 2 2 6 2 1 There are only two noteworthy differences in this example compared to the previous one. First, we\u2019ve created a uniform distribution variable (named die6 ) to generate numbers between 1 and 6. Second, instead of calling mt() to generate 32-bit unsigned integer random numbers, we\u2019re now calling die6(mt) to generate a value between 1 and 6.","title":"\u4f7f\u7528 Mersenne Twister \u6295\u63b7\u9ab0\u5b50"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#_1","text":"Although the results of our dice rolling example above are pretty random, there\u2019s a major flaw with the program. Run the program 3 times and see if you can figure out what it is. Go ahead, we\u2019ll wait. Jeopardy music If you run the program multiple times, you will note that it prints the same numbers every time! While each number in the sequence is random with regards to the previous one, the entire sequence is not random at all! Each run of our program produces the exact same result. Imagine that you\u2019re writing a game of hi-lo, where the user has 10 tries to guess a number that has been picked randomly, and the computer tells the user whether their guess is too high or too low. If the computer picks the same random number every time, the game won\u2019t be interesting past the first time it is played. So let\u2019s take a deeper look at why this is happening, and how we can fix it. In the prior lesson ( 7.18 - \u968f\u673a\u6570\u751f\u6210\u5668\u7b80\u4ecb ), we covered that each number in a PRNG sequence is in a deterministic way. And that the state of the PRNG is initialized from the seed value. Thus, given any starting seed number, PRNGs will always generate the same sequence of numbers from that seed as a result. Because we are default initializing our Mersenne Twister, it is being initialized with the same seed every time the program is run. And because the seed is the same, the random numbers being generated are also the same. In order to make our entire sequence randomized differently each time the program is run, we need to pick a seed that\u2019s not a fixed number. The first answer that probably comes to mind is that we need a random number for our seed! That\u2019s a good thought, but if we need a random number to generate random numbers, then we\u2019re in a catch-22. It turns out, we really don\u2019t need our seed to be a random number -- we just need to pick something that changes each time the program is run. Then we can use our PRNG to generate a unique sequence of pseudo-random numbers from that seed. There are two methods that are commonly used to do this: Use the system clock Use the system\u2019s random device","title":"\u4e0a\u9762\u7684\u7a0b\u5e8f\u5e76\u4e0d\u50cf\u5b83\u770b\u4e0a\u53bb\u90a3\u6837\u968f\u673a"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#_2","text":"What\u2019s one thing that\u2019s different every time you run your program? Unless you manage to run your program twice at exactly the same moment in time, the answer is that the current time is different. Therefore, if we use the current time as our seed value, then our program will produce a different set of random numbers each time it is run. C and C++ have a long history of PRNGs being seeded using the current time (using the std::time() function), so you will probably see this in a lot of existing code. Fortunately, C++ has a high resolution clock that we can use to generate a seed value. To minimize the chance of two time values being identical if the program is run quickly in succession, we want to use some time measure that changes as quickly as possible. For this, we\u2019ll ask the clock how much time has passed since the earliest time it can measure. This time is measured in \u201cticks\u201d, which is a very small unit of time (usually nanoseconds, but could be milliseconds). 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include // for std::mt19937 #include // for std::chrono int main () { // Seed our Mersenne Twister using the std :: mt19937 mt { static_cast < unsigned int > ( std :: chrono :: steady_clock :: now (). time_since_epoch (). count () ) }; // Create a reusable random number generator that generates uniform numbers between 1 and 6 std :: uniform_int_distribution die6 { 1 , 6 }; // for C++14, use std::uniform_int_distribution<> die6{ 1, 6 }; // Print a bunch of random numbers for ( int count { 1 }; count <= 40 ; ++ count ) { std :: cout << die6 ( mt ) << '\\t' ; // generate a roll of the die here // If we've printed 10 numbers, start a new row if ( count % 10 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY The above program has only two changes from the prior. First, we\u2019re including , which gives us access to the clock. Second, we\u2019re using the current time from the clock as a seed value for our Mersenne Twister. The results generated by this program should now be different each time it is run, which you can verify experimentally by running it several times. The downside of this approach is that if the program is run several times in quick succession, the seeds generated for each run won\u2019t be that different, which can impact the quality of the random results from a statistical standpoint. For normal programs, this doesn\u2019t matter, but for programs that require high quality, independent results, this method of seeding may be insufficient. \u5c0f\u8d34\u58eb std::chrono::high_resolution_clock is a popular choice instead of std::chrono::steady_clock . std::chrono::high_resolution_clock is the clock that uses the most granular unit of time, but it may use the system clock for the current time, which can be changed or rolled back by users. std::chrono::steady_clock may have a less granular tick time, but is the only clock with a guarantee that users can not adjust it.","title":"\u4f7f\u7528\u7cfb\u7edf\u65f6\u95f4\u4f5c\u4e3a\u968f\u673a\u6570\u79cd\u5b50"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#_3","text":"The random library contains a type called std::random_device that is an implementation-defined PRNG. Normally we avoid implementation-defined capabilities because they have no guarantees about quality or portability, but this is one of the exception cases. Typically std::random_device will ask the OS for a random number (how it does this depends on the OS). 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include // for std::mt19937 and std::random_device int main () { std :: mt19937 mt { std :: random_device {}() }; // Create a reusable random number generator that generates uniform numbers between 1 and 6 std :: uniform_int_distribution die6 { 1 , 6 }; // for C++14, use std::uniform_int_distribution<> die6{ 1, 6 }; // Print a bunch of random numbers for ( int count { 1 }; count <= 40 ; ++ count ) { std :: cout << die6 ( mt ) << '\\t' ; // generate a roll of the die here // If we've printed 10 numbers, start a new row if ( count % 10 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY In the above program, we\u2019re seeding our Mersenne Twister with one random number generated from a temporary instance of std::random_device . If you run this program multiple times, it should also produce different results each time. One potential problem with std::random_device : it isn\u2019t required to be non-deterministic, meaning it could , on some systems, produce the same sequence every time the program is run, which is exactly what we\u2019re trying to avoid. There was a bug in MinGW (fixed in GCC 9.2) that would do exactly this, making std::random_device useless. However, the latest versions of the most popular compilers (GCC/MinGW, Clang, Visual Studio) support proper implementations of std::random_device . \u6700\u4f73\u5b9e\u8df5 Use std::random_device to seed your PRNGs (unless it\u2019s not implemented properly for your target compiler/architecture).","title":"\u4f7f\u7528\u968f\u673a\u6570\u8bbe\u5907\u751f\u6210\u79cd\u5b50"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#q-stdrandom_device","text":"std::random_device{} creates an value-initialized temporary object of type std::random_device . The () then calls operator() on that temporary object, which returns a randomized value (which we use as an initializer for our Mersenne Twister) It\u2019s the equivalent of the calling the following function, which uses a syntax you should be more familiar with: 1 2 3 4 5 unsigned int getRandomDeviceValue () { std :: random_device rd {}; // create a value initialized std::random_device object return rd (); // return the result of operator() to the caller } COPY Using std::random_device{}() allows us to get the same result without creating a named function or named variable, so it\u2019s much more concise.","title":"Q: \u00a0std::random_device{}()\u00a0\u662f\u4ec0\u4e48\u610f\u601d?"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#q-stdrandom_device-mersenne-twister","text":"Because std::random_device is implementation defined, we can\u2019t assume much about it. It may be expensive to access or it may cause our program to pause while waiting for more random numbers to become available. The pool of numbers that it draws from may also be depleted quickly, which would impact the random results for other applications requesting random numbers via the same method. For this reason, std::random_device is better used to seed other PRNGs rather than as a PRNG itself. Only seed a PRNG once Many PRNGs can be reseeded after the initial seeding. This essentially re-initializes the state of the random number generator, causing it to generate results starting from the new seed state. Reseeding should generally be avoided unless you have a specific reason to do so, as it can cause the results to be less random, or not random at all. \u6700\u4f73\u5b9e\u8df5 Only seed a given pseudo-random number generator once, and do not reseed it. Here\u2019s an example of a common mistake that new programmers make: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include int getCard () { std :: mt19937 mt { std :: random_device {}() }; // this gets created and seeded every time the function is called std :: uniform_int_distribution card { 1 , 52 }; return card ( mt ); } int main () { std :: cout << getCard (); return 0 ; } COPY In the getCard() function, the random number generator is being created and seeded every time the function is called. This is inefficient at best, and will likely cause poor random results.","title":"Q: \u5982\u679c std::random_device \u672c\u8eab\u662f\u968f\u673a\u7684\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u76f4\u63a5\u7528\u5b83\u4ee3\u66ff Mersenne Twister \u5462\uff1f"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#_4","text":"What happens if we want to use a random number generator in multiple functions? One way is to create (and seed) our PRNG in our main() function, and then pass it everywhere we need it. But that\u2019s a lot of passing for something we may only use sporadically, and in different places. Although you can create a static local std::mt19937 variable in each function that needs it (static so that it only gets seeded once), it\u2019s overkill to have every function that uses a random number generator define and seed its own local generator. A better option in most cases is to create a global random number generator (inside a namespace!). Remember how we told you to avoid non-const global variables? This is an exception. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include // for std::mt19937 and std::random_device namespace Random // capital R to avoid conflicts with functions named random() { std :: mt19937 mt { std :: random_device {}() }; int get ( int min , int max ) { std :: uniform_int_distribution die { min , max }; // we can create a distribution in any function that needs it return die ( mt ); // and then generate a random number from our global generator } } int main () { std :: cout << Random :: get ( 1 , 6 ) << '\\n' ; std :: cout << Random :: get ( 1 , 10 ) << '\\n' ; std :: cout << Random :: get ( 1 , 20 ) << '\\n' ; return 0 ; } COPY In the above example, Random::mt is a global variable that can be accessed from any function. We\u2019ve created Random::get() as an easy way to get a random number between min and max . std::uniform_int_distribution is typically cheap to create, so it\u2019s fine to create when we need it.","title":"\u8de8\u51fd\u6570\u7684\u968f\u673a\u6570"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#mersenne-twister-underseeding-issues","text":"The internal state of a Mersenne Twister is 624 bytes in size. In the examples above, where we seed from the clock or std::random_device , our seed is only a single 32-bit integer. This means we\u2019re essentially initializing a 624-byte object with a 4-byte value, which is significantly underseeding the Mersenne Twister PRNG. The random library does the best it can to fill in the remaining 620 bytes with \u201crandom\u201d data\u2026 but it can\u2019t work magic. Underseeded PRNG can generate results that are suboptimal for applications that need the highest quality results. For example, seeding std::mt19937 with a single 32-bit value will never generate the number 42 as its first output. So how do we fix this? As of C++20, there\u2019s no easy way. But we do have some suggestions. First, let\u2019s talk about std::seed_seq (which stands for \u201cseed sequence\u201d). In the prior lesson, we mentioned that a seed can be either a single value, or a set of values. std::seed_seq is a type that performs two functions. First, it can hold multiple seed values, so we can use it to seed our PRNG with more than one value. Second, it will generate as many additional unbiased seed values as needed to initialize a PRNG\u2019s state. So if you initialize std::seed_seq with a single 32-bit integer (e.g. from std::random_device) and then initialize a Mersenne Twister with the std::seed_seq object, std::seed_seq will generate 620 bytes of additional seed data. The results won\u2019t be amazingly high quality, but it\u2019s better than nothing. Now on to the ideas. First, the more pieces of random data we can give std::seed_seq to work with, the better. So the easiest idea is to simply use std::random_device to give std::seed_seq more data to work with. If we initialize std::seed_seq with 8 numbers from std::random_device instead of 1, then the remaining numbers generated by std::seed_seq should be that much better: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include int main () { std :: random_device rd ; std :: seed_seq ss { rd (), rd (), rd (), rd (), rd (), rd (), rd (), rd () }; // get 8 integers of random numbers from std::random_device for our seed std :: mt19937 mt { ss }; // initialize our Mersenne Twister with the std::seed_seq // Create a reusable random number generator that generates uniform numbers between 1 and 6 std :: uniform_int_distribution die6 { 1 , 6 }; // for C++14, use std::uniform_int_distribution<> die6{ 1, 6 }; // Print a bunch of random numbers for ( int count { 1 }; count <= 40 ; ++ count ) { std :: cout << die6 ( mt ) << '\\t' ; // generate a roll of the die here // If we've printed 10 numbers, start a new row if ( count % 10 == 0 ) std :: cout << '\\n' ; } return 0 ; } COPY This is pretty straightforward so there isn\u2019t much reason not to do this at a minimum.","title":"Mersenne Twister \u548c underseeding issues"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#q-why-not-give-stdseed_seq-156-integers-624-bytes-from-stdrandom_device","text":"You can! However, this may be slow, and risks depleting the pool of random numbers that std::random_device uses. Second, you can use other \u201crandom\u201d inputs to std::seed_seq . We\u2019ve already shown you how to get a value from the clock, so you can throw that in easily. Other things that are sometimes used include the current thread id,the address of particular functions, the user\u2019s id, the process id, etc\u2026 Doing that is beyond the scope of this article, but this article has some context and a link to randutils.hpp that implements this. An alternate path is to use a different PRNG with a smaller state. Many good PRNGs use 64 or 128 bits of state, which can easily be initialized using std::seed_seq filled with 8 calls to std::random_device .","title":"Q: Why not give std::seed_seq 156 integers (624 bytes) from\u00a0std::random_device?"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#prng","text":"When a PRNG is given a poor quality seed (or underseeded), the initial results of the PRNG may not be high quality. For this reason, some PRNGs benefit from being \u201cwarmed up\u201d, which is a technique where the first N results generated from the PRNG are discarded. This allows the internal state of the PRNG to be mixed up such that the subsequent results should be of higher quality. Typically a few hundred to a few thousand initial results are discarded. The longer the period of the PRNG, the more initial results should be discarded. \u9898\u5916\u8bdd Visual Studio\u2019s implementation of rand() had (or still has?) a bug where the first generated result would not be sufficiently randomized. You may see older programs that use rand() discard a single result as a way to avoid this issue. The seed_seq initialization used by std::mt19937 performs a warm up, so we don\u2019t need to explicitly warm up std::mt19937 objects.","title":"PRNG \u70ed\u8eab"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#debug","text":"Programs that use random numbers can be difficult to debug because the program may exhibit different behaviors each time it is run. Sometimes it may work, and sometimes it may not. When debugging, it\u2019s helpful to ensure your program executes the same (incorrect) way each time. That way, you can run the program as many times as needed to isolate where the error is. For this reason, when debugging, it\u2019s a useful technique to seed your PRNG with a specific value (e.g. 5 ) that causes the erroneous behavior to occur. This will ensure your program generates the same results each time, making debugging easier. Once you\u2019ve found the error, you can use your normal seeding method to start generating randomized results again.","title":"\u4f7f\u7528\u968f\u673a\u6570\u5e26\u6765\u7684debug\u96be\u9898"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#faq","text":"","title":"\u968f\u673a\u5316 FAQ"},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#q-help-my-random-number-generator-is-generating-the-same-sequence-of-random-numbers","text":"If your random number generator is generating the same sequence of random numbers every time your program is run, you probably didn\u2019t seed it properly (or at all). Make sure you\u2019re seeding it with a value that changes each time the program is run.","title":"Q: Help! My random number generator is generating the same sequence of random numbers."},{"location":"7-19-generating-random-numbers-using-mersenne-twister/#q-help-my-random-number-generator-keeps-generating-the-same-number-over-and-over","text":"If your random number generator is generating the same number every time you ask it for a random number, then you are probably either reseeding the random number generator before generating a random number, or you\u2019re creating a new random generator for each random number.","title":"Q: Help! My random number generator keeps generating the same number over and over."},{"location":"7-2-If-statements-and-blocks/","tags":["if"],"text":"\u6211\u4eec\u8981\u8ba8\u8bba\u7684\u7b2c\u4e00\u7c7b\u63a7\u5236\u6d41\u8bed\u53e5\u662f\u6761\u4ef6\u8bed\u53e5\u3002\u6761\u4ef6\u8bed\u53e5\u662f\u4e00\u79cd\u6307\u5b9a\u662f\u5426\u5e94\u8be5\u6267\u884c\u67d0\u4e9b\u5173\u8054\u8bed\u53e5\u7684\u8bed\u53e5\u3002 C++\u652f\u6301\u4e24\u79cd\u57fa\u672c\u6761\u4ef6\u8bed\u53e5\uff1aif\u8bed\u53e5(\u6211\u4eec\u5728\u8bfe\u7a0b[[4-10-Introduction-to-if-statements|4.10 -if\u8bed\u53e5\u7b80\u4ecb]\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u5e76\u5c06\u5728\u8fd9\u91cc\u8fdb\u4e00\u6b65\u8ba8\u8bba)\u548c switch \u8bed\u53e5(\u6211\u4eec\u5c06\u5728\u51e0\u8282\u8bfe\u4e2d\u8ba8\u8bba)\u3002 \u5feb\u901f\u590d\u4e60\u4e00\u4e0b if \u8bed\u53e5 if \u8bed\u53e5\u662fC++\u4e2d\u6700\u57fa\u672c\u7684\u6761\u4ef6\u8bed\u53e5\uff0c\u5176\u5f62\u5f0f\u4e3a\uff1a 1 2 if ( condition ) true_statement ; \u6216\u8005\u662f\u5e26 else \u7684\u5f62\u5f0f\uff1a 1 2 3 4 if ( condition ) true_statement ; else false_statement ; \u5982\u679c condition \u6c42\u503c\u4e3a true \uff0c\u5219\u6267\u884c true_statement \u3002\u5982\u679c\u6c42\u503c\u4e3a false \u800c\u4e14\u5b58\u5728 else \u8bed\u53e5\u5219\u6267\u884c false_statement \u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86if\u8bed\u53e5\u548celse\u8bed\u53e5\u7684\u7528\u6cd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x > 10 ) std :: cout << x << \" is greater than 10 \\n \" ; else std :: cout << x << \" is not greater than 10 \\n \" ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u5982\u4f60\u6240\u671f\u671b\u7684\u90a3\u6837\uff1a 1 2 3 4 5 Enter a number: 15 15 is greater than 10 Enter a number: 4 4 is not greater than 10 \u591a\u4e2a\u6761\u4ef6\u8bed\u53e5 \u65b0\u624b\u7a0b\u5e8f\u5458\u53ef\u80fd\u4f1a\u7f16\u5199\u8fd9\u6837\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { std :: cout << \"Enter your height (in cm): \" ; int x {}; std :: cin >> x ; if ( x > 140 ) std :: cout << \"You are tall enough to ride. \\n \" ; else std :: cout << \"You are not tall enough to ride. \\n \" ; std :: cout << \"Too bad! \\n \" ; // focus on this line return 0 ; } \u4e0d\u8fc7\uff0c\u7a0b\u5e8f\u7684\u8fd0\u884c\u7ed3\u679c\u53ef\u80fd\u5e76\u4e0d\u5982\u613f\uff1a 1 2 3 Enter your height (in cm): 180 You are tall enough to ride. Too bad! \u8fd9\u4e2a\u7a0b\u5e8f\u6ca1\u6709\u50cf\u9884\u671f\u7684\u90a3\u6837\u5de5\u4f5c\uff0c\u56e0\u4e3a true_statement \u548c false_statement \u53ea\u80fd\u662f\u4e00\u4e2a\u8bed\u53e5\u3002\u8fd9\u91cc\u7684\u7f29\u8fdb\u6b3a\u9a97\u4e86\u6211\u4eec\u2014\u2014\u4e0a\u9762\u7684\u7a0b\u5e8f\u6267\u884c\u8d77\u6765\u5c31\u597d\u50cf\u5b83\u662f\u8fd9\u6837\u5199\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { std :: cout << \"Enter your height (in cm): \" ; int x {}; std :: cin >> x ; if ( x > 140 ) std :: cout << \"You are tall enough to ride. \\n \" ; else std :: cout << \"You are not tall enough to ride. \\n \" ; std :: cout << \"Too bad! \\n \" ; // focus on this line return 0 ; } \u8fd9\u4e5f\u5c31\u770b\u5230\u5f88\u6e05\u695a\u4e86\uff0c\u663e\u7136 \u201cToo bad!\u201d \u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u90fd\u4f1a\u88ab\u6253\u5370\u3002 \u4f46\u662f\uff0c\u901a\u5e38\u5e0c\u671b\u57fa\u4e8e\u67d0\u4e9b\u6761\u4ef6\u6267\u884c\u591a\u4e2a\u8bed\u53e5\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u590d\u5408\u8bed\u53e5(\u5757): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { std :: cout << \"Enter your height (in cm): \" ; int x {}; std :: cin >> x ; if ( x > 140 ) std :: cout << \"You are tall enough to ride. \\n \" ; else { // note addition of block here std :: cout << \"You are not tall enough to ride. \\n \" ; std :: cout << \"Too bad! \\n \" ; } return 0 ; } \u8bb0\u4f4f\uff0cblock\u88ab\u5f53\u4f5c\u4e00\u4e2a\u5355\u72ec\u7684\u8bed\u53e5\uff0c\u6240\u4ee5\u73b0\u5728\u5de5\u4f5c\u6b63\u5e38\uff1a 1 2 Enter your height (in cm): 180 You are tall enough to ride. 1 2 3 Enter your height (in cm): 130 You are not tall enough to ride. Too bad! \u5e94\u8be5\u5c06\u5355\u72ec\u7684\u6761\u4ef6\u8bed\u53e5\u5199\u4f5c\u8bed\u53e5\u5757\u5417\uff1f \u5bf9\u4e8e if \u6216 else \u540e\u9762\u7684\u5355\u4e2a\u8bed\u53e5\u662f\u5426\u5e94\u8be5\u663e\u5f0f\u5730\u5305\u542b\u5728\u8bed\u53e5\u5757\u4e2d\uff0c\u5728\u7a0b\u5e8f\u5458\u793e\u533a\u4e2d\u5f15\u53d1\u4e86\u5e7f\u6cdb\u7684\u4e89\u8bba\u3002 \u8fd9\u6837\u505a\u901a\u5e38\u6709\u4e24\u4e2a\u7406\u7531\u3002\u9996\u5148\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\uff1a\u8bb0\u4f4f\uff0c\u5757\u88ab\u89c6\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u8bed\u53e5\uff0c\u6240\u4ee5\u73b0\u5728\u5de5\u4f5c\u6b63\u5e38: 1 2 if ( age >= 21 ) purchaseBeer (); \u7136\u540e\uff0c\u5982\u679c\u4f60\u7279\u522b\u5306\u5fd9\u5730\u5c06\u67d0\u4e2a\u65b0\u529f\u80fd\u52a0\u5165\u5230\u7a0b\u5e8f\u4e2d\uff1a 1 2 3 if ( age >= 21 ) purchaseBeer (); gamble (); // will always execute \u4e0a\u8ff0\u4ee3\u7801\u5c06\u5141\u8bb8\u672a\u6210\u5e74\u4eba\u8d4c\u535a\u3002\u5728\u76d1\u72f1\u91cc\u73a9\u5f97\u5f00\u5fc3\uff01 \u5176\u6b21\uff0c\u5b83\u4f1a\u4f7f\u7a0b\u5e8f\u66f4\u96be\u8c03\u8bd5\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u4ee3\u7801\u7247\u6bb5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 if ( age >= 21 ) addBeerToCart (); checkout (); ``` \u5982\u679c\u6211\u4eec\u6000\u7591 ` addBeerToCart () ` \u51fd\u6570\u6709\u95ee\u9898 \uff0c \u5f80\u5f80\u4f1a\u5c06\u5176\u4e34\u65f6\u6ce8\u91ca\u6389 \uff1a ``` cpp if ( age >= 21 ) // addBeerToCart(); checkout (); \u8fd9\u6837\u4e00\u6765\u4f1a\u4f7f\u5f97 checkout() \u6309\u6761\u4ef6\u6267\u884c\uff0c\u8fd9\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c\u3002 \u5982\u679c\u6211\u4eec\u603b\u662f\u5c06if\u548celse\u7684\u8bed\u53e5\u653e\u5728\u8bed\u53e5\u5757\u4e2d\u7684\u8bdd\uff0c\u4e0a\u9762\u7684\u8fd9\u4e9b\u95ee\u9898\u90fd\u53ef\u4ee5\u907f\u514d\u3002 \u53cd\u65b9\u5219\u8ba4\u4e3a\uff0c\u4e0d\u8981\u5728\u5355\u4e2a\u8bed\u53e5\u5468\u56f4\u4f7f\u7528\u5757\u7684\u6700\u4f73\u7406\u7531\u662f\u56e0\u4e3a\u6dfb\u52a0\u5757\u4f1a\u5360\u7528\u5782\u76f4\u7a7a\u95f4\uff0c\u4f7f\u4f60\u4e00\u6b21\u770b\u5230\u7684\u4ee3\u7801\u66f4\u5c11\uff0c\u8fd9\u4f1a\u4f7f\u4ee3\u7801\u53ef\u8bfb\u6027\u53d8\u5dee\uff0c\u5e76\u53ef\u80fd\u5bfc\u81f4\u5176\u4ed6\u66f4\u4e25\u91cd\u7684\u9519\u8bef\u3002 \u793e\u533a\u4f3c\u4e4e\u66f4\u503e\u5411\u4e8e\u59cb\u7ec8\u4f7f\u7528\u5757\uff0c\u5c3d\u7ba1\u8fd9\u79cd\u5efa\u8bae\u5f53\u7136\u4e0d\u662f\u666e\u904d\u5b58\u5728\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u8003\u8651\u5c06\u4e0e\u201c if \u201d\u6216\u201c else \u201d\u76f8\u5173\u7684\u5355\u4e2a\u8bed\u53e5\u653e\u5728\u5757\u4e2d(\u7279\u522b\u662f\u5728\u4f60\u5b66\u4e60\u7684\u65f6\u5019)\u3002\u66f4\u6709\u7ecf\u9a8c\u7684C++\u5f00\u53d1\u4eba\u5458\u6709\u65f6\u4f1a\u5ffd\u7565\u8fd9\u79cd\u505a\u6cd5\uff0c\u800c\u503e\u5411\u4e8e\u66f4\u7d27\u51d1\u7684\u5782\u76f4\u95f4\u8ddd\u3002 \u8fd8\u6709\u4e00\u79cd\u6298\u4e2d\u7684\u529e\u6cd5\uff0c\u5373\u5c06\u5355\u4e00\u6761\u4ef6\u8bed\u53e5\u548cif\u6216else\u5199\u5728\u540c\u4e00\u884c\uff1a 1 if ( age >= 21 ) purchaseBeer (); \u8fd9\u907f\u514d\u4e86\u4e0a\u9762\u63d0\u5230\u7684\u4e24\u4e2a\u7f3a\u70b9\uff0c\u4e0d\u8fc7\u53ef\u8bfb\u6027\u7a0d\u5dee\u4e86\u4e00\u70b9\u3002 \u9690\u5f0f\u8bed\u53e5\u5757 \u5982\u679c\u7a0b\u5e8f\u5458\u6ca1\u6709\u5728 if \u8bed\u53e5\u6216 else \u8bed\u53e5\u7684\u90e8\u5206\u58f0\u660e\u4e00\u4e2a\u8bed\u53e5\u5757\uff0c\u5219\u7f16\u8bd1\u5668\u5c06\u9690\u5f0f\u58f0\u660e\u4e00\u4e2a\u3002\u56e0\u6b64\uff1a 1 2 3 4 if (condition) true_statement; else false_statement; \u7b49\u4ef7\u4e8e\uff1a 1 2 3 4 5 6 7 8 if (condition) { true_statement; } else { false_statement; } \u591a\u6570\u60c5\u51b5\u4e0b\u8fd9\u5e76\u4e0d\u4f1a\u5e26\u6765\u95ee\u9898\uff0c\u4f46\u662f\u65b0\u624b\u7a0b\u5e8f\u5458\u53ef\u80fd\u4f1a\u5c1d\u8bd5\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { if ( true ) int x { 5 }; else int x { 6 }; std :: cout << x ; return 0 ; } \u6b64\u65f6\u4ee3\u7801\u662f\u4e0d\u80fd\u7f16\u8bd1\u7684\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u544a x \u672a\u5b9a\u4e49\u3002\u56e0\u4e3a\u4e0a\u8ff0\u4ee3\u7801\u7b49\u4ef7\u4e8e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { if ( true ) { int x { 5 }; } // x \u9500\u6bc1\u4e86 else { int x { 6 }; } // x \u9500\u6bc1\u4e86 std :: cout << x ; // x \u5e76\u4e0d\u5728\u8be5\u4f5c\u7528\u57df\u4e2d return 0 ; } \u8fd9\u91cc\uff0c\u5f88\u663e\u7136\u53d8\u91cf x \u5177\u6709\u8bed\u53e5\u5757\u4f5c\u7528\u57df\uff0c\u5e76\u4e14\u4f1a\u5728\u8bed\u53e5\u5757\u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u5f53\u6211\u4eec\u5728 std::cout \u4e2d\u8bbf\u95ee x \u65f6\uff0c\u5b83\u5df2\u7ecf\u4e0d\u5b58\u5728\u4e86\u3002","title":"7.2 - if \u8bed\u53e5\u548c\u8bed\u53e5\u5757"},{"location":"7-2-If-statements-and-blocks/#if","text":"if \u8bed\u53e5\u662fC++\u4e2d\u6700\u57fa\u672c\u7684\u6761\u4ef6\u8bed\u53e5\uff0c\u5176\u5f62\u5f0f\u4e3a\uff1a 1 2 if ( condition ) true_statement ; \u6216\u8005\u662f\u5e26 else \u7684\u5f62\u5f0f\uff1a 1 2 3 4 if ( condition ) true_statement ; else false_statement ; \u5982\u679c condition \u6c42\u503c\u4e3a true \uff0c\u5219\u6267\u884c true_statement \u3002\u5982\u679c\u6c42\u503c\u4e3a false \u800c\u4e14\u5b58\u5728 else \u8bed\u53e5\u5219\u6267\u884c false_statement \u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86if\u8bed\u53e5\u548celse\u8bed\u53e5\u7684\u7528\u6cd5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x > 10 ) std :: cout << x << \" is greater than 10 \\n \" ; else std :: cout << x << \" is not greater than 10 \\n \" ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u5982\u4f60\u6240\u671f\u671b\u7684\u90a3\u6837\uff1a 1 2 3 4 5 Enter a number: 15 15 is greater than 10 Enter a number: 4 4 is not greater than 10","title":"\u5feb\u901f\u590d\u4e60\u4e00\u4e0b if \u8bed\u53e5"},{"location":"7-2-If-statements-and-blocks/#_1","text":"\u65b0\u624b\u7a0b\u5e8f\u5458\u53ef\u80fd\u4f1a\u7f16\u5199\u8fd9\u6837\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { std :: cout << \"Enter your height (in cm): \" ; int x {}; std :: cin >> x ; if ( x > 140 ) std :: cout << \"You are tall enough to ride. \\n \" ; else std :: cout << \"You are not tall enough to ride. \\n \" ; std :: cout << \"Too bad! \\n \" ; // focus on this line return 0 ; } \u4e0d\u8fc7\uff0c\u7a0b\u5e8f\u7684\u8fd0\u884c\u7ed3\u679c\u53ef\u80fd\u5e76\u4e0d\u5982\u613f\uff1a 1 2 3 Enter your height (in cm): 180 You are tall enough to ride. Too bad! \u8fd9\u4e2a\u7a0b\u5e8f\u6ca1\u6709\u50cf\u9884\u671f\u7684\u90a3\u6837\u5de5\u4f5c\uff0c\u56e0\u4e3a true_statement \u548c false_statement \u53ea\u80fd\u662f\u4e00\u4e2a\u8bed\u53e5\u3002\u8fd9\u91cc\u7684\u7f29\u8fdb\u6b3a\u9a97\u4e86\u6211\u4eec\u2014\u2014\u4e0a\u9762\u7684\u7a0b\u5e8f\u6267\u884c\u8d77\u6765\u5c31\u597d\u50cf\u5b83\u662f\u8fd9\u6837\u5199\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { std :: cout << \"Enter your height (in cm): \" ; int x {}; std :: cin >> x ; if ( x > 140 ) std :: cout << \"You are tall enough to ride. \\n \" ; else std :: cout << \"You are not tall enough to ride. \\n \" ; std :: cout << \"Too bad! \\n \" ; // focus on this line return 0 ; } \u8fd9\u4e5f\u5c31\u770b\u5230\u5f88\u6e05\u695a\u4e86\uff0c\u663e\u7136 \u201cToo bad!\u201d \u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u90fd\u4f1a\u88ab\u6253\u5370\u3002 \u4f46\u662f\uff0c\u901a\u5e38\u5e0c\u671b\u57fa\u4e8e\u67d0\u4e9b\u6761\u4ef6\u6267\u884c\u591a\u4e2a\u8bed\u53e5\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u590d\u5408\u8bed\u53e5(\u5757): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { std :: cout << \"Enter your height (in cm): \" ; int x {}; std :: cin >> x ; if ( x > 140 ) std :: cout << \"You are tall enough to ride. \\n \" ; else { // note addition of block here std :: cout << \"You are not tall enough to ride. \\n \" ; std :: cout << \"Too bad! \\n \" ; } return 0 ; } \u8bb0\u4f4f\uff0cblock\u88ab\u5f53\u4f5c\u4e00\u4e2a\u5355\u72ec\u7684\u8bed\u53e5\uff0c\u6240\u4ee5\u73b0\u5728\u5de5\u4f5c\u6b63\u5e38\uff1a 1 2 Enter your height (in cm): 180 You are tall enough to ride. 1 2 3 Enter your height (in cm): 130 You are not tall enough to ride. Too bad!","title":"\u591a\u4e2a\u6761\u4ef6\u8bed\u53e5"},{"location":"7-2-If-statements-and-blocks/#_2","text":"\u5bf9\u4e8e if \u6216 else \u540e\u9762\u7684\u5355\u4e2a\u8bed\u53e5\u662f\u5426\u5e94\u8be5\u663e\u5f0f\u5730\u5305\u542b\u5728\u8bed\u53e5\u5757\u4e2d\uff0c\u5728\u7a0b\u5e8f\u5458\u793e\u533a\u4e2d\u5f15\u53d1\u4e86\u5e7f\u6cdb\u7684\u4e89\u8bba\u3002 \u8fd9\u6837\u505a\u901a\u5e38\u6709\u4e24\u4e2a\u7406\u7531\u3002\u9996\u5148\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\uff1a\u8bb0\u4f4f\uff0c\u5757\u88ab\u89c6\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u8bed\u53e5\uff0c\u6240\u4ee5\u73b0\u5728\u5de5\u4f5c\u6b63\u5e38: 1 2 if ( age >= 21 ) purchaseBeer (); \u7136\u540e\uff0c\u5982\u679c\u4f60\u7279\u522b\u5306\u5fd9\u5730\u5c06\u67d0\u4e2a\u65b0\u529f\u80fd\u52a0\u5165\u5230\u7a0b\u5e8f\u4e2d\uff1a 1 2 3 if ( age >= 21 ) purchaseBeer (); gamble (); // will always execute \u4e0a\u8ff0\u4ee3\u7801\u5c06\u5141\u8bb8\u672a\u6210\u5e74\u4eba\u8d4c\u535a\u3002\u5728\u76d1\u72f1\u91cc\u73a9\u5f97\u5f00\u5fc3\uff01 \u5176\u6b21\uff0c\u5b83\u4f1a\u4f7f\u7a0b\u5e8f\u66f4\u96be\u8c03\u8bd5\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u4ee3\u7801\u7247\u6bb5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 if ( age >= 21 ) addBeerToCart (); checkout (); ``` \u5982\u679c\u6211\u4eec\u6000\u7591 ` addBeerToCart () ` \u51fd\u6570\u6709\u95ee\u9898 \uff0c \u5f80\u5f80\u4f1a\u5c06\u5176\u4e34\u65f6\u6ce8\u91ca\u6389 \uff1a ``` cpp if ( age >= 21 ) // addBeerToCart(); checkout (); \u8fd9\u6837\u4e00\u6765\u4f1a\u4f7f\u5f97 checkout() \u6309\u6761\u4ef6\u6267\u884c\uff0c\u8fd9\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c\u3002 \u5982\u679c\u6211\u4eec\u603b\u662f\u5c06if\u548celse\u7684\u8bed\u53e5\u653e\u5728\u8bed\u53e5\u5757\u4e2d\u7684\u8bdd\uff0c\u4e0a\u9762\u7684\u8fd9\u4e9b\u95ee\u9898\u90fd\u53ef\u4ee5\u907f\u514d\u3002 \u53cd\u65b9\u5219\u8ba4\u4e3a\uff0c\u4e0d\u8981\u5728\u5355\u4e2a\u8bed\u53e5\u5468\u56f4\u4f7f\u7528\u5757\u7684\u6700\u4f73\u7406\u7531\u662f\u56e0\u4e3a\u6dfb\u52a0\u5757\u4f1a\u5360\u7528\u5782\u76f4\u7a7a\u95f4\uff0c\u4f7f\u4f60\u4e00\u6b21\u770b\u5230\u7684\u4ee3\u7801\u66f4\u5c11\uff0c\u8fd9\u4f1a\u4f7f\u4ee3\u7801\u53ef\u8bfb\u6027\u53d8\u5dee\uff0c\u5e76\u53ef\u80fd\u5bfc\u81f4\u5176\u4ed6\u66f4\u4e25\u91cd\u7684\u9519\u8bef\u3002 \u793e\u533a\u4f3c\u4e4e\u66f4\u503e\u5411\u4e8e\u59cb\u7ec8\u4f7f\u7528\u5757\uff0c\u5c3d\u7ba1\u8fd9\u79cd\u5efa\u8bae\u5f53\u7136\u4e0d\u662f\u666e\u904d\u5b58\u5728\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u8003\u8651\u5c06\u4e0e\u201c if \u201d\u6216\u201c else \u201d\u76f8\u5173\u7684\u5355\u4e2a\u8bed\u53e5\u653e\u5728\u5757\u4e2d(\u7279\u522b\u662f\u5728\u4f60\u5b66\u4e60\u7684\u65f6\u5019)\u3002\u66f4\u6709\u7ecf\u9a8c\u7684C++\u5f00\u53d1\u4eba\u5458\u6709\u65f6\u4f1a\u5ffd\u7565\u8fd9\u79cd\u505a\u6cd5\uff0c\u800c\u503e\u5411\u4e8e\u66f4\u7d27\u51d1\u7684\u5782\u76f4\u95f4\u8ddd\u3002 \u8fd8\u6709\u4e00\u79cd\u6298\u4e2d\u7684\u529e\u6cd5\uff0c\u5373\u5c06\u5355\u4e00\u6761\u4ef6\u8bed\u53e5\u548cif\u6216else\u5199\u5728\u540c\u4e00\u884c\uff1a 1 if ( age >= 21 ) purchaseBeer (); \u8fd9\u907f\u514d\u4e86\u4e0a\u9762\u63d0\u5230\u7684\u4e24\u4e2a\u7f3a\u70b9\uff0c\u4e0d\u8fc7\u53ef\u8bfb\u6027\u7a0d\u5dee\u4e86\u4e00\u70b9\u3002","title":"\u5e94\u8be5\u5c06\u5355\u72ec\u7684\u6761\u4ef6\u8bed\u53e5\u5199\u4f5c\u8bed\u53e5\u5757\u5417\uff1f"},{"location":"7-2-If-statements-and-blocks/#_3","text":"\u5982\u679c\u7a0b\u5e8f\u5458\u6ca1\u6709\u5728 if \u8bed\u53e5\u6216 else \u8bed\u53e5\u7684\u90e8\u5206\u58f0\u660e\u4e00\u4e2a\u8bed\u53e5\u5757\uff0c\u5219\u7f16\u8bd1\u5668\u5c06\u9690\u5f0f\u58f0\u660e\u4e00\u4e2a\u3002\u56e0\u6b64\uff1a 1 2 3 4 if (condition) true_statement; else false_statement; \u7b49\u4ef7\u4e8e\uff1a 1 2 3 4 5 6 7 8 if (condition) { true_statement; } else { false_statement; } \u591a\u6570\u60c5\u51b5\u4e0b\u8fd9\u5e76\u4e0d\u4f1a\u5e26\u6765\u95ee\u9898\uff0c\u4f46\u662f\u65b0\u624b\u7a0b\u5e8f\u5458\u53ef\u80fd\u4f1a\u5c1d\u8bd5\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { if ( true ) int x { 5 }; else int x { 6 }; std :: cout << x ; return 0 ; } \u6b64\u65f6\u4ee3\u7801\u662f\u4e0d\u80fd\u7f16\u8bd1\u7684\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u544a x \u672a\u5b9a\u4e49\u3002\u56e0\u4e3a\u4e0a\u8ff0\u4ee3\u7801\u7b49\u4ef7\u4e8e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { if ( true ) { int x { 5 }; } // x \u9500\u6bc1\u4e86 else { int x { 6 }; } // x \u9500\u6bc1\u4e86 std :: cout << x ; // x \u5e76\u4e0d\u5728\u8be5\u4f5c\u7528\u57df\u4e2d return 0 ; } \u8fd9\u91cc\uff0c\u5f88\u663e\u7136\u53d8\u91cf x \u5177\u6709\u8bed\u53e5\u5757\u4f5c\u7528\u57df\uff0c\u5e76\u4e14\u4f1a\u5728\u8bed\u53e5\u5757\u7ed3\u675f\u65f6\u9500\u6bc1\u3002\u5f53\u6211\u4eec\u5728 std::cout \u4e2d\u8bbf\u95ee x \u65f6\uff0c\u5b83\u5df2\u7ecf\u4e0d\u5b58\u5728\u4e86\u3002","title":"\u9690\u5f0f\u8bed\u53e5\u5757"},{"location":"7-3-Common-if-statement-problems/","tags":["if"],"text":"Key Takeaway \u7ee7\u7eed 7.2 - if \u8bed\u53e5\u548c\u8bed\u53e5\u5757 \u7684\u5185\u5bb9\u3002\u672c\u8282\u8bfe\u6211\u4eec\u4f1a\u4ecb\u7ecd\u4e00\u4e9b\u548cif\u8bed\u53e5\u76f8\u5173\u7684\u5e38\u89c1\u9519\u8bef\u3002 \u5d4c\u5957if\u8bed\u53e5\u548c\u60ac\u5782else\u95ee\u9898 if \u8bed\u53e5\u53ef\u4ee5\u5d4c\u5957\u5728\u5176\u4ed6 if \u8bed\u53e5\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x >= 0 ) // \u5916\u5c42if\u8bed\u53e5 // \u8fd9\u79cd\u5d4c\u5957\u65b9\u5f0f\u662f\u4e0d\u597d\u7684\u7f16\u7a0b\u98ce\u683c if ( x <= 20 ) // \u5185\u5c42if\u8bed\u53e5 std :: cout << x << \" is between 0 and 20 \\n \" ; return 0 ; } \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x >= 0 ) // \u5916\u5c42if\u8bed\u53e5 // \u8fd9\u79cd\u5d4c\u5957\u65b9\u5f0f\u662f\u4e0d\u597d\u7684\u7f16\u7a0b\u98ce\u683c if ( x <= 20 ) // \u5185\u5c42if\u8bed\u53e5 std :: cout << x << \" is between 0 and 20 \\n \" ; // \u8fd9\u4e2a else \u5e94\u8be5\u548c\u54ea\u4e2a if \u8bed\u53e5\u5339\u914d\uff1f else std :: cout << x << \" is negative \\n \" ; return 0 ; } \u4e0a\u9762\u7684\u7a0b\u5e8f\u5f15\u5165\u4e86\u4e00\u4e2a\u6f5c\u5728\u7684\u6b67\u4e49\u6765\u6e90\uff0c\u79f0\u4e3a\u60ac\u5782else\u95ee\u9898\u3002\u4e0a\u9762\u7a0b\u5e8f\u4e2d\u7684 else \u8bed\u53e5\u5e94\u8be5\u4e0e\u5916\u90e8if\u5339\u914d\u8fd8\u662f\u548c\u5185\u90e8if\u5339\u914d\uff1f \u7b54\u6848\u662f else \u8bed\u53e5\u4e0e\u540c\u4e00\u8bed\u53e5\u5757\u4e2d\u6700\u540e\u4e00\u4e2a\u672a\u5339\u914d\u7684 if \u8bed\u53e5\u914d\u5bf9\u3002\u56e0\u6b64\uff0c\u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c else \u4e0e\u5185\u90e8\u7684 if \u8bed\u53e5\u5339\u914d\uff0c\u7b49\u4ef7\u4e8e\u4e0b\u9762\u5f62\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x >= 0 ) // \u5916\u5c42 if \u8bed\u53e5 { if ( x <= 20 ) // \u5185\u5c42 if \u8bed\u53e5 std :: cout << x << \" is between 0 and 20 \\n \" ; else // \u548c\u5185\u5c42 if \u8bed\u53e5\u5339\u914d std :: cout << x << \" is negative \\n \" ; } return 0 ; } \u8fd9\u4f7f\u5f97\u7a0b\u5e8f\u4ea7\u751f\u9519\u8bef\u7684\u7ed3\u679c\uff1a 1 2 Enter a number: 21 21 is negative \u4e3a\u4e86\u907f\u514d\u56e0\u5d4c\u5957 if \u8bed\u53e5\u4ea7\u751f\u7684\u6b67\u4e49\uff0c\u6700\u597d\u7684\u529e\u6cd5\u662f\u5c06\u5185\u90e8\u7684if\u8bed\u53e5\u663e\u5f0f\u5730\u5b9a\u4e49\u5728\u8bed\u53e5\u5757\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x >= 0 ) { if ( x <= 20 ) std :: cout << x << \" is between 0 and 20 \\n \" ; else // attached to inner if statement std :: cout << x << \" is greater than 20 \\n \" ; } else // attached to outer if statement std :: cout << x << \" is negative \\n \" ; return 0 ; } \u6b64\u65f6\uff0c\u8bed\u53e5\u5757\u4e2d\u7684 else \u8bed\u53e5\u4f1a\u548c\u5185\u5c42\u7684 if \u8bed\u53e5\u5339\u914d\uff0c\u800c\u8bed\u53e5\u5757\u5916\u90e8\u7684 else \u5219\u548c\u5916\u5c42\u7684 if \u8bed\u53e5\u5339\u914d\u3002 \u5d4c\u5957\u8bed\u53e5\u5c55\u5f00 \u5d4c\u5957\u7684\u591a\u5c42 if \u8bed\u53e5\u53ef\u4ee5\u901a\u8fc7\u91cd\u65b0\u7ec4\u7ec7\u903b\u8f91\u6216\u4f7f\u7528 \u903b\u8f91\u8fd0\u7b97\u7b26 \u5c55\u5f00\u6210\u4e00\u5c42\u903b\u8f91\u3002\u5d4c\u5957\u8d8a\u5c11\u7684\u4ee3\u7801\u8d8a\u4e0d\u5bb9\u6613\u51fa\u9519\u3002 \u4f8b\u5982\uff0c\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u53ef\u4ee5\u5c55\u5f00\u6210\u5982\u4e0b\u5f62\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x < 0 ) std :: cout << x << \" is negative \\n \" ; else if ( x <= 20 ) // only executes if x >= 0 std :: cout << x << \" is between 0 and 20 \\n \" ; else // only executes if x > 20 std :: cout << x << \" is greater than 20 \\n \" ; return 0 ; } \u4e0b\u9762\u662f\u53e6\u4e00\u4e2a\u4f7f\u7528\u903b\u8f91\u8fd0\u7b97\u7b26\u5728\u4e00\u4e2a if \u8bed\u53e5\u4e2d\u68c0\u67e5\u591a\u4e2a\u6761\u4ef6\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another integer: \" ; int y {}; std :: cin >> y ; if ( x > 0 && y > 0 ) // && \u662f\u903b\u8f91\u4e0e\u2014\u2014\u68c0\u67e5\u662f\u5426\u4e24\u4e2a\u6761\u4ef6\u5747\u4e3a\u771f std :: cout << \"Both numbers are positive \\n \" ; else if ( x > 0 || y > 0 ) // || \u662f\u903b\u8f91\u6216\u2014\u2014\u68c0\u67e5\u662f\u5426\u6709\u4e3a\u771f\u7684\u6761\u4ef6 std :: cout << \"One of the numbers is positive \\n \" ; else std :: cout << \"Neither number is positive \\n \" ; return 0 ; } \u7a7a\u8bed\u53e5 \u7a7a\u8bed\u53e5\u662f\u4e00\u4e2a\u53ea\u5305\u542b\u5206\u53f7\u7684\u8bed\u53e5\uff1a 1 2 if ( x > 10 ) ; // this is a null statement \u7a7a\u8bed\u53e5\u4ec0\u4e48\u90fd\u4e0d\u505a\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u53ea\u6709\u5f53\u8bed\u8a00\u8981\u6c42\u5b58\u5728\u4e00\u6761\u8bed\u53e5\u4f46\u7a0b\u5e8f\u5458\u4e0d\u9700\u8981\u7684\u60c5\u51b5\u4e0b\u624d\u8fd9\u4e48\u505a\u3002\u4e3a\u4e86\u53ef\u8bfb\u6027\uff0c\u201c\u7a7a\u8bed\u53e5\u201d\u901a\u5e38\u88ab\u653e\u5728\u5355\u72ec\u7684\u884c\u4e2d\u3002 \u6211\u4eec\u5c06\u5728\u672c\u7ae0\u540e\u9762\u8bb2\u5230\u5faa\u73af\u7684\u65f6\u5019\u770b\u5230\u6709\u610f\u7684\u7a7a\u8bed\u53e5\u7684\u4f8b\u5b50\u3002\u201c\u7a7a\u8bed\u53e5\u201d\u5f88\u5c11\u4e0e\u201cif\u8bed\u53e5\u201d\u4e00\u8d77\u4f7f\u7528\u3002\u7136\u800c\uff0c\u5b83\u4eec\u53ef\u80fd\u4f1a\u65e0\u610f\u4e2d\u7ed9\u65b0\u624b(\u6216\u7c97\u5fc3\u7684)\u7a0b\u5e8f\u5458\u5e26\u6765\u95ee\u9898\u3002\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 if ( nuclearCodesActivated ()); blowUpTheWorld (); \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u5458\u4e0d\u7ecf\u610f\u95f4\u5728if\u8bed\u53e5\u7684\u672b\u5c3e\u6dfb\u52a0\u4e86\u4e00\u4e2a\u5206\u53f7\u3002\u8fd9\u4e2a\u4e0d\u8d77\u773c\u7684\u9519\u8bef\u53ef\u4ee5\u5f88\u597d\u5730\u7f16\u8bd1\uff0c\u5e76\u4f7f\u4ee3\u7801\u6bb5\u6267\u884c\u8d77\u6765\u5c31\u50cf\u5b83\u662f\u8fd9\u6837\u7f16\u5199\u7684\u4e00\u6837\uff1a 1 2 3 if ( nuclearCodesActivated ()) ; // \u5206\u53f7\u5b9e\u9645\u4e0a\u5236\u9020\u4e86\u4e00\u4e2a\u7a7a\u8bed\u53e5 blowUpTheWorld (); // \u8fd9\u4e2a\u51fd\u6570\u603b\u662f\u4f1a\u6267\u884c \u6ce8\u610f \u6ce8\u610f\u4e0d\u8981\u5728if\u8bed\u53e5\u540e\u9762\u6dfb\u52a0\u5206\u53f7\uff0c\u5426\u5219\u4f60\u7684\u201c\u6761\u4ef6\u8bed\u53e5\u201d\u603b\u662f\u4f1a\u65e0\u6761\u4ef6\u6267\u884c\uff08\u5373\u4f7f\u4f60\u628a\u5b83\u5199\u5728\u8bed\u53e5\u5757\u4e2d\uff09\u3002 \u6761\u4ef6\u8bed\u53e5\u4e2d\u7684\u7b49\u53f7\u548c\u8d4b\u503c\u53f7 \u5728 if \u7684\u6761\u4ef6\u4e2d\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528\u7b49\u53f7 == \u6765\u5224\u65ad\u662f\u5426\u76f8\u7b49\uff0c\u800c\u4e0d\u662f\u8d4b\u503c\u53f7 = \uff0c\u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { std :: cout << \"Enter 0 or 1: \" ; int x {}; std :: cin >> x ; if ( x = 0 ) // \u7cdf\u7cd5, \u6211\u4eec\u4f7f\u7528\u8d4b\u503c\u53f7\u8fdb\u884c\u4e86\u8d4b\u503c\u800c\u4e0d\u662f\u5224\u65ad\u76f8\u7b49 std :: cout << \"You entered 0\" ; else std :: cout << \"You entered 1\" ; return 0 ; } \u7a0b\u5e8f\u53ef\u4ee5\u7f16\u8bd1\u8fd0\u884c\uff0c\u4f46\u662f\u67d0\u4e9b\u60c5\u51b5\u4e0b\u7ed3\u679c\u662f\u9519\u8bef\u7684\uff1a 1 2 Enter 0 or 1: 0 You entered 1 \u4e8b\u5b9e\u4e0a\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u603b\u662f\u4f1a\u4ea7\u751f\u201c\u4f60\u8f93\u5165\u4e861\u201d\u7684\u7ed3\u679c\u3002\u8fd9\u662f\u56e0\u4e3a x = 0 \u9996\u5148\u5c06\u503c 0 \u8d4b\u503c\u7ed9 x \uff0c\u7136\u540e\u8ba1\u7b97 x \u7684\u503c\uff0c\u73b0\u5728\u662f 0 \uff0c\u5373\u5e03\u5c14\u503c false \u3002\u7531\u4e8e\u6761\u4ef6\u603b\u662f false \u6240\u4ee5 else \u8bed\u53e5\u603b\u662f\u6267\u884c\u3002","title":"7.3 - \u5e38\u89c1\u7684 if \u8bed\u53e5\u9519\u8bef"},{"location":"7-3-Common-if-statement-problems/#ifelse","text":"if \u8bed\u53e5\u53ef\u4ee5\u5d4c\u5957\u5728\u5176\u4ed6 if \u8bed\u53e5\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x >= 0 ) // \u5916\u5c42if\u8bed\u53e5 // \u8fd9\u79cd\u5d4c\u5957\u65b9\u5f0f\u662f\u4e0d\u597d\u7684\u7f16\u7a0b\u98ce\u683c if ( x <= 20 ) // \u5185\u5c42if\u8bed\u53e5 std :: cout << x << \" is between 0 and 20 \\n \" ; return 0 ; } \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x >= 0 ) // \u5916\u5c42if\u8bed\u53e5 // \u8fd9\u79cd\u5d4c\u5957\u65b9\u5f0f\u662f\u4e0d\u597d\u7684\u7f16\u7a0b\u98ce\u683c if ( x <= 20 ) // \u5185\u5c42if\u8bed\u53e5 std :: cout << x << \" is between 0 and 20 \\n \" ; // \u8fd9\u4e2a else \u5e94\u8be5\u548c\u54ea\u4e2a if \u8bed\u53e5\u5339\u914d\uff1f else std :: cout << x << \" is negative \\n \" ; return 0 ; } \u4e0a\u9762\u7684\u7a0b\u5e8f\u5f15\u5165\u4e86\u4e00\u4e2a\u6f5c\u5728\u7684\u6b67\u4e49\u6765\u6e90\uff0c\u79f0\u4e3a\u60ac\u5782else\u95ee\u9898\u3002\u4e0a\u9762\u7a0b\u5e8f\u4e2d\u7684 else \u8bed\u53e5\u5e94\u8be5\u4e0e\u5916\u90e8if\u5339\u914d\u8fd8\u662f\u548c\u5185\u90e8if\u5339\u914d\uff1f \u7b54\u6848\u662f else \u8bed\u53e5\u4e0e\u540c\u4e00\u8bed\u53e5\u5757\u4e2d\u6700\u540e\u4e00\u4e2a\u672a\u5339\u914d\u7684 if \u8bed\u53e5\u914d\u5bf9\u3002\u56e0\u6b64\uff0c\u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c else \u4e0e\u5185\u90e8\u7684 if \u8bed\u53e5\u5339\u914d\uff0c\u7b49\u4ef7\u4e8e\u4e0b\u9762\u5f62\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x >= 0 ) // \u5916\u5c42 if \u8bed\u53e5 { if ( x <= 20 ) // \u5185\u5c42 if \u8bed\u53e5 std :: cout << x << \" is between 0 and 20 \\n \" ; else // \u548c\u5185\u5c42 if \u8bed\u53e5\u5339\u914d std :: cout << x << \" is negative \\n \" ; } return 0 ; } \u8fd9\u4f7f\u5f97\u7a0b\u5e8f\u4ea7\u751f\u9519\u8bef\u7684\u7ed3\u679c\uff1a 1 2 Enter a number: 21 21 is negative \u4e3a\u4e86\u907f\u514d\u56e0\u5d4c\u5957 if \u8bed\u53e5\u4ea7\u751f\u7684\u6b67\u4e49\uff0c\u6700\u597d\u7684\u529e\u6cd5\u662f\u5c06\u5185\u90e8\u7684if\u8bed\u53e5\u663e\u5f0f\u5730\u5b9a\u4e49\u5728\u8bed\u53e5\u5757\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x >= 0 ) { if ( x <= 20 ) std :: cout << x << \" is between 0 and 20 \\n \" ; else // attached to inner if statement std :: cout << x << \" is greater than 20 \\n \" ; } else // attached to outer if statement std :: cout << x << \" is negative \\n \" ; return 0 ; } \u6b64\u65f6\uff0c\u8bed\u53e5\u5757\u4e2d\u7684 else \u8bed\u53e5\u4f1a\u548c\u5185\u5c42\u7684 if \u8bed\u53e5\u5339\u914d\uff0c\u800c\u8bed\u53e5\u5757\u5916\u90e8\u7684 else \u5219\u548c\u5916\u5c42\u7684 if \u8bed\u53e5\u5339\u914d\u3002","title":"\u5d4c\u5957if\u8bed\u53e5\u548c\u60ac\u5782else\u95ee\u9898"},{"location":"7-3-Common-if-statement-problems/#_1","text":"\u5d4c\u5957\u7684\u591a\u5c42 if \u8bed\u53e5\u53ef\u4ee5\u901a\u8fc7\u91cd\u65b0\u7ec4\u7ec7\u903b\u8f91\u6216\u4f7f\u7528 \u903b\u8f91\u8fd0\u7b97\u7b26 \u5c55\u5f00\u6210\u4e00\u5c42\u903b\u8f91\u3002\u5d4c\u5957\u8d8a\u5c11\u7684\u4ee3\u7801\u8d8a\u4e0d\u5bb9\u6613\u51fa\u9519\u3002 \u4f8b\u5982\uff0c\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u53ef\u4ee5\u5c55\u5f00\u6210\u5982\u4e0b\u5f62\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { std :: cout << \"Enter a number: \" ; int x {}; std :: cin >> x ; if ( x < 0 ) std :: cout << x << \" is negative \\n \" ; else if ( x <= 20 ) // only executes if x >= 0 std :: cout << x << \" is between 0 and 20 \\n \" ; else // only executes if x > 20 std :: cout << x << \" is greater than 20 \\n \" ; return 0 ; } \u4e0b\u9762\u662f\u53e6\u4e00\u4e2a\u4f7f\u7528\u903b\u8f91\u8fd0\u7b97\u7b26\u5728\u4e00\u4e2a if \u8bed\u53e5\u4e2d\u68c0\u67e5\u591a\u4e2a\u6761\u4ef6\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int main () { std :: cout << \"Enter an integer: \" ; int x {}; std :: cin >> x ; std :: cout << \"Enter another integer: \" ; int y {}; std :: cin >> y ; if ( x > 0 && y > 0 ) // && \u662f\u903b\u8f91\u4e0e\u2014\u2014\u68c0\u67e5\u662f\u5426\u4e24\u4e2a\u6761\u4ef6\u5747\u4e3a\u771f std :: cout << \"Both numbers are positive \\n \" ; else if ( x > 0 || y > 0 ) // || \u662f\u903b\u8f91\u6216\u2014\u2014\u68c0\u67e5\u662f\u5426\u6709\u4e3a\u771f\u7684\u6761\u4ef6 std :: cout << \"One of the numbers is positive \\n \" ; else std :: cout << \"Neither number is positive \\n \" ; return 0 ; }","title":"\u5d4c\u5957\u8bed\u53e5\u5c55\u5f00"},{"location":"7-3-Common-if-statement-problems/#_2","text":"\u7a7a\u8bed\u53e5\u662f\u4e00\u4e2a\u53ea\u5305\u542b\u5206\u53f7\u7684\u8bed\u53e5\uff1a 1 2 if ( x > 10 ) ; // this is a null statement \u7a7a\u8bed\u53e5\u4ec0\u4e48\u90fd\u4e0d\u505a\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u53ea\u6709\u5f53\u8bed\u8a00\u8981\u6c42\u5b58\u5728\u4e00\u6761\u8bed\u53e5\u4f46\u7a0b\u5e8f\u5458\u4e0d\u9700\u8981\u7684\u60c5\u51b5\u4e0b\u624d\u8fd9\u4e48\u505a\u3002\u4e3a\u4e86\u53ef\u8bfb\u6027\uff0c\u201c\u7a7a\u8bed\u53e5\u201d\u901a\u5e38\u88ab\u653e\u5728\u5355\u72ec\u7684\u884c\u4e2d\u3002 \u6211\u4eec\u5c06\u5728\u672c\u7ae0\u540e\u9762\u8bb2\u5230\u5faa\u73af\u7684\u65f6\u5019\u770b\u5230\u6709\u610f\u7684\u7a7a\u8bed\u53e5\u7684\u4f8b\u5b50\u3002\u201c\u7a7a\u8bed\u53e5\u201d\u5f88\u5c11\u4e0e\u201cif\u8bed\u53e5\u201d\u4e00\u8d77\u4f7f\u7528\u3002\u7136\u800c\uff0c\u5b83\u4eec\u53ef\u80fd\u4f1a\u65e0\u610f\u4e2d\u7ed9\u65b0\u624b(\u6216\u7c97\u5fc3\u7684)\u7a0b\u5e8f\u5458\u5e26\u6765\u95ee\u9898\u3002\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 if ( nuclearCodesActivated ()); blowUpTheWorld (); \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u5458\u4e0d\u7ecf\u610f\u95f4\u5728if\u8bed\u53e5\u7684\u672b\u5c3e\u6dfb\u52a0\u4e86\u4e00\u4e2a\u5206\u53f7\u3002\u8fd9\u4e2a\u4e0d\u8d77\u773c\u7684\u9519\u8bef\u53ef\u4ee5\u5f88\u597d\u5730\u7f16\u8bd1\uff0c\u5e76\u4f7f\u4ee3\u7801\u6bb5\u6267\u884c\u8d77\u6765\u5c31\u50cf\u5b83\u662f\u8fd9\u6837\u7f16\u5199\u7684\u4e00\u6837\uff1a 1 2 3 if ( nuclearCodesActivated ()) ; // \u5206\u53f7\u5b9e\u9645\u4e0a\u5236\u9020\u4e86\u4e00\u4e2a\u7a7a\u8bed\u53e5 blowUpTheWorld (); // \u8fd9\u4e2a\u51fd\u6570\u603b\u662f\u4f1a\u6267\u884c \u6ce8\u610f \u6ce8\u610f\u4e0d\u8981\u5728if\u8bed\u53e5\u540e\u9762\u6dfb\u52a0\u5206\u53f7\uff0c\u5426\u5219\u4f60\u7684\u201c\u6761\u4ef6\u8bed\u53e5\u201d\u603b\u662f\u4f1a\u65e0\u6761\u4ef6\u6267\u884c\uff08\u5373\u4f7f\u4f60\u628a\u5b83\u5199\u5728\u8bed\u53e5\u5757\u4e2d\uff09\u3002","title":"\u7a7a\u8bed\u53e5"},{"location":"7-3-Common-if-statement-problems/#_3","text":"\u5728 if \u7684\u6761\u4ef6\u4e2d\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528\u7b49\u53f7 == \u6765\u5224\u65ad\u662f\u5426\u76f8\u7b49\uff0c\u800c\u4e0d\u662f\u8d4b\u503c\u53f7 = \uff0c\u8003\u8651\u4e0b\u9762\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { std :: cout << \"Enter 0 or 1: \" ; int x {}; std :: cin >> x ; if ( x = 0 ) // \u7cdf\u7cd5, \u6211\u4eec\u4f7f\u7528\u8d4b\u503c\u53f7\u8fdb\u884c\u4e86\u8d4b\u503c\u800c\u4e0d\u662f\u5224\u65ad\u76f8\u7b49 std :: cout << \"You entered 0\" ; else std :: cout << \"You entered 1\" ; return 0 ; } \u7a0b\u5e8f\u53ef\u4ee5\u7f16\u8bd1\u8fd0\u884c\uff0c\u4f46\u662f\u67d0\u4e9b\u60c5\u51b5\u4e0b\u7ed3\u679c\u662f\u9519\u8bef\u7684\uff1a 1 2 Enter 0 or 1: 0 You entered 1 \u4e8b\u5b9e\u4e0a\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u603b\u662f\u4f1a\u4ea7\u751f\u201c\u4f60\u8f93\u5165\u4e861\u201d\u7684\u7ed3\u679c\u3002\u8fd9\u662f\u56e0\u4e3a x = 0 \u9996\u5148\u5c06\u503c 0 \u8d4b\u503c\u7ed9 x \uff0c\u7136\u540e\u8ba1\u7b97 x \u7684\u503c\uff0c\u73b0\u5728\u662f 0 \uff0c\u5373\u5e03\u5c14\u503c false \u3002\u7531\u4e8e\u6761\u4ef6\u603b\u662f false \u6240\u4ee5 else \u8bed\u53e5\u603b\u662f\u6267\u884c\u3002","title":"\u6761\u4ef6\u8bed\u53e5\u4e2d\u7684\u7b49\u53f7\u548c\u8d4b\u503c\u53f7"},{"location":"7-4-Switch-statement-basics/","tags":["switch","control flow"],"text":"Key Takeaway \u5c3d\u7ba1\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u5f88\u591a\u94fe\u5f0f if-else\uff0c\u4f46\u662f\u8fd9\u4e48\u505a\u4e0d\u4ec5\u5199\u8d77\u6765\u8d39\u52b2\uff0c\u6548\u7387\u4e5f\u4e0d\u9ad8\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include void printDigitName ( int x ) { if ( x == 1 ) std :: cout << \"One\" ; else if ( x == 2 ) std :: cout << \"Two\" ; else if ( x == 3 ) std :: cout << \"Three\" ; else std :: cout << \"Unknown\" ; } int main () { printDigitName ( 2 ); return 0 ; } \u8fd9\u4e2a\u4f8b\u5b50\u5e76\u4e0d\u590d\u6742\uff0cx \u6700\u591a\u4f1a\u88ab\u6c42\u503c\u4e09\u6b21\uff08\u4e0d\u9ad8\u6548\uff09\uff0c\u8bfb\u8005\u4e5f\u5fc5\u987b\u660e\u767d\u88ab\u6c42\u503c\u591a\u6b21\u7684\u662f x \u800c\u4e0d\u662f\u5176\u4ed6\u53d8\u91cf\u3002 \u7531\u4e8e\u9488\u5bf9\u4e00\u7ec4\u4e0d\u540c\u7684\u503c\u6d4b\u8bd5\u53d8\u91cf\u6216\u8868\u8fbe\u5f0f\u662f\u5426\u76f8\u7b49\u662f\u5f88\u5e38\u89c1\u7684\uff0cC++\u63d0\u4f9b\u4e86\u53e6\u5916\u4e00\u79cd\u53ef\u9009\u7684\u6761\u4ef6\u8bed\u53e5\uff0c\u79f0\u4e3a switch\u8bed\u53e5 \uff0c\u4e13\u95e8\u7528\u4e8e\u6b64\u76ee\u7684\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u7528switch\u8bed\u53e5\u5b9e\u73b0\u4e86\u76f8\u540c\u7684\u529f\u80fd\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include void printDigitName ( int x ) { switch ( x ) { case 1 : std :: cout << \"One\" ; return ; case 2 : std :: cout << \"Two\" ; return ; case 3 : std :: cout << \"Three\" ; return ; default : std :: cout << \"Unknown\" ; return ; } } int main () { printDigitName ( 2 ); return 0 ; } switch \u80cc\u540e\u7684\u601d\u60f3\u5f88\u7b80\u5355\uff1a\u8868\u8fbe\u5f0f(\u6709\u65f6\u79f0\u4e3a\u6761\u4ef6)\u6c42\u503c\u540e\u5f97\u5230\u4e00\u4e2a\u503c\u3002\u5982\u679c\u8868\u8fbe\u5f0f\u7684\u503c\u80fd\u591f\u548c\u540e\u9762\u7684\u67d0\u4e2a\u5206\u652f\u6807\u7b7e\u5339\u914d\uff0c\u5219\u8be5\u6807\u7b7e\u540e\u7684\u8bed\u53e5\u5c31\u4f1a\u88ab\u6267\u884c\u3002\u5982\u679c\u6ca1\u6709\u5339\u914d\u7684\u6807\u7b7e\uff0c\u4f46\u662f\u5b58\u5728default\u6807\u7b7e\uff0c\u5219\u6267\u884cdefault\u540e\u9762\u7684\u8bed\u53e5\u3002 \u548cif\u8bed\u53e5\u76f8\u6bd4\uff0cswitch\u8bed\u53e5\u7684\u4f18\u52bf\u5728\u4e8e\u6761\u4ef6\u6c42\u503c\u53ea\u9700\u8981\u505a\u4e00\u6b21\uff08\u66f4\u9ad8\u6548\uff09\uff0c\u800c\u4e14switch\u8bed\u53e5\u770b\u8d77\u6765\u66f4\u6e05\u695a\uff0c\u53ef\u8bfb\u6027\u66f4\u597d\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u53ef\u4ee5\u7684\u8bdd\uff0c\u5c3d\u91cf\u7528 switch \u8bed\u53e5\u4ee3\u66ff if-else \u8bed\u53e5\u94fe\u3002 \u8ba9\u6211\u4eec\u66f4\u8be6\u7ec6\u5730\u7814\u7a76\u8fd9\u4e9b\u6982\u5ff5\u3002 \u521b\u5efa switch \u4f7f\u7528 switch \u5173\u952e\u5b57\uff0c\u540e\u9762\u52a0\u4e0a\u62ec\u53f7\u4ee5\u53ca\u9700\u8981\u5728\u62ec\u53f7\u4e2d\u6c42\u503c\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\uff0c\u5c31\u53ef\u4ee5\u521b\u5efaswitch\u8bed\u53e5\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\u8868\u8fbe\u5f0f\u4f1a\u662f\u4e00\u4e2a\u5355\u4e00\u53d8\u91cf\uff0c\u4f46\u662f\u4e5f\u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u7684\u8868\u8fbe\u5f0f\u3002 \u5173\u4e8e\u8fd9\u91cc\u7684\u6761\u4ef6\u8bed\u53e5\uff0c\u5b83\u7684\u4e00\u4e2a\u9650\u5236\u6761\u4ef6\u662f\u5176\u6c42\u503c\u7ed3\u679c\u5fc5\u987b\u4e3a \u6574\u578b\u7c7b\u578b (\u5982\u4f55\u4f60\u8fd8\u4e0d\u719f\u6089\u6574\u578b\u7c7b\u578b\uff0c\u8bf7\u53c2\u89c1\uff1a 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb ) \u6216 \u6709\u4f5c\u7528\u57df\u679a\u4e3e \u7c7b\u578b(\u53c2\u89c1\uff1a 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b )\uff0c\u6216\u8005\u662f\u4efb\u4f55\u80fd\u8f6c\u6362\u6210\u4e0a\u8ff0\u7c7b\u578b\u7684\u503c\u3002\u6d6e\u70b9\u7c7b\u578b\u3001\u5b57\u7b26\u4e32\u548c\u5927\u591a\u6570\u5176\u4ed6\u975e\u6574\u578b\u7684\u8868\u8fbe\u5f0f\u90fd\u4e0d\u53ef\u7528\u3002 \u6269\u5c55\u9605\u8bfb \u4e3a\u4ec0\u4e48switch\u53ea\u4f7f\u7528\u6574\u6570(\u6216\u679a\u4e3e)\u7c7b\u578b\uff1f\u7b54\u6848\u662f\u56e0\u4e3aswitch\u8bed\u53e5\u88ab\u9ad8\u5ea6\u4f18\u5316\u4e86\u3002\u5386\u53f2\u4e0a\uff0c\u7f16\u8bd1\u5668\u5b9e\u73b0switch\u8bed\u53e5\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u901a\u8fc7 \u8df3\u8868 \u5b9e\u73b0\u7684\uff0c\u800c\u8df3\u8f6c\u8868\u53ea\u9002\u7528\u4e8e\u6574\u6570\u503c\u3002 \u5bf9\u4e8e\u90a3\u4e9b\u5df2\u7ecf\u719f\u6089\u6570\u7ec4\u7684\u4eba\u6765\u8bf4\uff0c\u8df3\u8868\u7684\u5de5\u4f5c\u539f\u7406\u975e\u5e38\u7c7b\u4f3c\u4e8e\u6570\u7ec4\uff0c\u4f7f\u7528\u6574\u6570\u503c\u4f5c\u4e3a\u6570\u7ec4\u7d22\u5f15\uff0c\u76f4\u63a5\u201c\u8df3\u8f6c\u201d\u5230\u7ed3\u679c\u3002\u8fd9\u6bd4\u6267\u884c\u4e00\u5806\u987a\u5e8f\u6bd4\u8f83\u8981\u9ad8\u6548\u5f97\u591a\u3002 \u5f53\u7136\uff0c\u7f16\u8bd1\u5668\u4e0d\u5fc5\u4f7f\u7528\u8df3\u8868\u5b9e\u73b0switch\uff0c\u6709\u65f6\u4e5f\u4e0d\u9700\u8981\u3002\u4ece\u6280\u672f\u4e0a\u8bb2\uff0cC++\u6ca1\u6709\u7406\u7531\u4e0d\u80fd\u653e\u677e\u6b64\u5904\u7684\u7c7b\u578b\u9650\u5236\uff0c\u4ee5\u4fbf\u5176\u4ed6\u7c7b\u578b\u4e5f\u53ef\u4ee5\u4f7f\u7528\uff0c\u53ea\u662f\u76ee\u524d\u8fd8\u6ca1\u6709\u8fd9\u6837\u505a\u7f62\u4e86(\u76f4\u5230C++20)\u3002 \u5728\u6761\u4ef6\u8868\u8fbe\u5f0f\u540e\u9762\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u8bed\u53e5\u5757\u3002\u5728\u8bed\u53e5\u5757\u5185\u90e8\uff0c\u4f7f\u7528\u6807\u7b7e\u6765\u5b9a\u4e49\u6211\u4eec\u60f3\u8981\u6d4b\u8bd5\u662f\u5426\u76f8\u7b49\u7684\u6240\u6709\u503c\uff0c\u6807\u7b7e\u6709\u4e24\u79cd\uff1a \u5206\u652f\u6807\u7b7e \u7b2c\u4e00\u79cd\u6807\u7b7e\u662f\u5206\u652f\u6807\u7b7e\uff0c\u5b83\u4f7f\u7528 case \u5173\u952e\u5b57\u58f0\u660e\uff0c\u540e\u9762\u8ddf\u7740\u4e00\u4e2a\u5e38\u91cf\u8868\u8fbe\u5f0f\u3002\u5e38\u91cf\u8868\u8fbe\u5f0f\u5fc5\u987b\u5339\u914d\u6761\u4ef6\u7684\u7c7b\u578b\uff0c\u6216\u53ef\u4ee5\u8f6c\u6362\u4e3a\u8be5\u7c7b\u578b\u3002 \u5982\u679c\u6761\u4ef6\u8868\u8fbe\u5f0f\u6c42\u503c\u7ed3\u679c\u7b49\u4e8e\u67d0\u4e2a\u5206\u652f\u6807\u7b7e\u7684\u503c\uff0c\u5219\u4ece\u8be5\u6807\u7b7e\u540e\u9762\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u7ee7\u7eed\u987a\u5e8f\u6267\u884c\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6761\u4ef6\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5339\u914d\u4e00\u4e2a\u5206\u652f\u6807\u7b7e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include void printDigitName ( int x ) { switch ( x ) // x \u6c42\u503c\u4e3a 2 { case 1 : std :: cout << \"One\" ; return ; case 2 : // \u5339\u914d std :: cout << \"Two\" ; // \u4ece\u8fd9\u91cc\u5f00\u59cb\u6267\u884c return ; // \u6267\u884c\u5230\u8fd9\u6761\u8bed\u53e5\u540e\u5c31\u8fd4\u56de case 3 : std :: cout << \"Three\" ; return ; default : std :: cout << \"Unknown\" ; return ; } } int main () { printDigitName ( 2 ); return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 Two \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c x \u6c42\u503c\u4e3a2\u3002\u56e0\u4e3a\u5206\u652f\u6807\u7b7e\u4e2d\u4e5f\u67092\uff0c\u6240\u4ee5\u7a0b\u5e8f\u4f1a\u8df3\u8f6c\u5230\u8be5\u6807\u7b7e\u4e0b\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u5f00\u59cb\u6267\u884c\u3002\u8be5\u7a0b\u5e8f\u4f1a\u6253\u5370 Two \uff0c\u7136\u540e\u4ece return \u8bed\u53e5\u8fd4\u56de\u7ed9\u8c03\u7528\u8005\u3002 \u5206\u652f\u6807\u7b7e\u7684\u4e2a\u6570\u5e76\u6ca1\u6709\u73b0\u5728\uff0c\u4f46\u662f\u6bcf\u4e2a\u6807\u7b7e\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u6240\u4ee5\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u9519\u8bef\u7684\uff1a 1 2 3 4 5 6 switch ( x ) { case 54 : case 54 : // error: already used value 54! case '6' : // error: '6' converts to integer value 54, which is already used } default \u6807\u7b7e \u7b2c\u4e8c\u79cd\u6807\u7b7e\u79f0\u4e3a default \u6807\u7b7e(\u7ecf\u5e38\u88ab\u79f0\u4e3a\u9ed8\u8ba4\u5206\u652f)\uff0c\u4f7f\u7528 default \u5173\u952e\u5b57\u5b9a\u4e49\u3002\u5982\u679c\u6761\u4ef6\u8868\u8fbe\u5f0f\u4e0d\u80fd\u591f\u5339\u914d\u4efb\u4f55\u5176\u4ed6\u6807\u7b7e\u4e14default \u6807\u7b7e\u5b58\u5728\uff0c\u5219\u4ecedefault\u6807\u7b7e\u540e\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u5f00\u59cb\u6267\u884c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include void printDigitName ( int x ) { switch ( x ) // x is evaluated to produce value 5 { case 1 : std :: cout << \"One\" ; return ; case 2 : std :: cout << \"Two\" ; return ; case 3 : std :: cout << \"Three\" ; return ; default : // which does not match to any case labels std :: cout << \"Unknown\" ; // so execution starts here return ; // and then we return to the caller } } int main () { printDigitName ( 5 ); return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 Unknown default \u5206\u652f\u662f\u53ef\u9009\u7684\uff0c\u5982\u679c\u6709\u7684\u8bdd\u5219\u53ea\u80fd\u6709\u4e00\u4e2a\u3002\u4e00\u822c\u6765\u8bb2\uff0cdefault\u5206\u652f\u4f1a\u88ab\u653e\u7f6e\u5728switch\u8bed\u53e5\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5206\u652f\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c06 default \u4f5c\u4e3a\u6700\u540e\u4e00\u4e2a\u5206\u652f\u3002 \u6ca1\u6709\u5339\u914d\u7684\u5206\u652f\u4e5f\u6ca1\u6709\u9ed8\u8ba4\u5206\u652f \u5982\u679c\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u503c\u4e0d\u5339\u914d\u4efb\u4f55\u5206\u652f\uff0c\u5e76\u4e14\u6ca1\u6709\u63d0\u4f9b\u9ed8\u8ba4\u5206\u652f\uff0c\u5219switch\u5185\u90e8\u4e0d\u4f1a\u6267\u884c\u4efb\u4f55case\u3002\u5728switch\u7ed3\u675f\u540e\u7ee7\u7eed\u6267\u884c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include void printDigitName ( int x ) { switch ( x ) // x is evaluated to produce value 5 { case 1 : std :: cout << \"One\" ; return ; case 2 : std :: cout << \"Two\" ; return ; case 3 : std :: cout << \"Three\" ; return ; // no matching case exists and there is no default case } // so execution continues here std :: cout << \"Hello\" ; } int main () { printDigitName ( 5 ); std :: cout << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c x \u6c42\u503c\u4e3a 5 \uff0c\u4f46\u662f\u7531\u4e8e\u6ca1\u6709\u5339\u914d\u7684\u5206\u652f\uff0c\u4e5f\u6ca1\u6709\u9ed8\u8ba4\u5206\u652f\u3002\u6240\u4ee5\u4e0d\u4f1a\u8d70\u5230\u4efb\u4f55case\u4e2d\uff0c\u7a0b\u5e8f\u5728switch\u5757\u7ed3\u675f\u540e\u7ee7\u7eed\u5411\u4e0b\u6267\u884c\u3002\u6253\u5370 Hello \u3002 \u4f7f\u7528 break \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 return \u8bed\u53e5\u6765\u505c\u6b62\u6807\u7b7e\u540e\u9762\u8bed\u53e5\u7684\u6267\u884c\u3002\u4f46\u662f\uff0c\u8fd9\u4e5f\u4f1a\u9000\u51fa\u6574\u4e2a\u51fd\u6570\u3002 break \u8bed\u53e5(\u4f7f\u7528 break \u5173\u952e\u5b57\u58f0\u660e)\u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u6211\u4eec\u5df2\u7ecf\u5b8c\u6210\u4e86switch\u8bed\u53e5\u7684\u6267\u884c\u4f46\u9700\u8981\u7ee7\u7eed\u6267\u884c\u5916\u9762\u7684\u8bed\u53e5\u3002\u8fd9\u5141\u8bb8\u6211\u4eec\u5728\u4e0d\u9000\u51fa\u6574\u4e2a\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\u9000\u51fa\u201cswitch\u8bed\u53e5\u201d\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u7528 break \u800c\u4e0d\u662f return \u91cd\u5199\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include void printDigitName ( int x ) { switch ( x ) // x evaluates to 3 { case 1 : std :: cout << \"One\" ; break ; case 2 : std :: cout << \"Two\" ; break ; case 3 : std :: cout << \"Three\" ; // \u4ece\u8fd9\u91cc\u5f00\u59cb\u6267\u884c break ; // \u8df3\u5230 switch \u7ed3\u675f\u65f6 default : std :: cout << \"Unknown\" ; break ; } // \u4ece\u8fd9\u91cc\u7ee7\u7eed\u6267\u884c std :: cout << \" Ah-Ah-Ah!\" ; } int main () { printDigitName ( 3 ); return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 Three Ah-Ah-Ah! \u6700\u4f73\u5b9e\u8df5 \u6807\u7b7e\u4e0b\u7684\u6bcf\u7ec4\u8bed\u53e5\u90fd\u5e94\u8be5\u4ee5\u201cbreak\u8bed\u53e5\u201d\u6216\u201creturn\u8bed\u53e5\u201d\u7ed3\u675f\u3002\u8fd9\u5305\u62ecswitch\u6700\u540e\u4e00\u4e2a\u6807\u7b7e\u4e0b\u9762\u7684\u8bed\u53e5\u3002 \u90a3\u4e48\uff0c\u5982\u679c\u4e0d\u4ee5 break \u6216 return \u4f5c\u4e3a\u4e00\u7ec4\u8bed\u53e5\u7684\u7ed3\u5c3e\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462?\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u63a2\u8ba8\u8fd9\u4e2a\u8bdd\u9898\u548c\u5176\u4ed6\u8bdd\u9898\u3002","title":"7.4 - switch \u8bed\u53e5\u57fa\u7840"},{"location":"7-4-Switch-statement-basics/#switch","text":"\u4f7f\u7528 switch \u5173\u952e\u5b57\uff0c\u540e\u9762\u52a0\u4e0a\u62ec\u53f7\u4ee5\u53ca\u9700\u8981\u5728\u62ec\u53f7\u4e2d\u6c42\u503c\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\uff0c\u5c31\u53ef\u4ee5\u521b\u5efaswitch\u8bed\u53e5\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\u8868\u8fbe\u5f0f\u4f1a\u662f\u4e00\u4e2a\u5355\u4e00\u53d8\u91cf\uff0c\u4f46\u662f\u4e5f\u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u7684\u8868\u8fbe\u5f0f\u3002 \u5173\u4e8e\u8fd9\u91cc\u7684\u6761\u4ef6\u8bed\u53e5\uff0c\u5b83\u7684\u4e00\u4e2a\u9650\u5236\u6761\u4ef6\u662f\u5176\u6c42\u503c\u7ed3\u679c\u5fc5\u987b\u4e3a \u6574\u578b\u7c7b\u578b (\u5982\u4f55\u4f60\u8fd8\u4e0d\u719f\u6089\u6574\u578b\u7c7b\u578b\uff0c\u8bf7\u53c2\u89c1\uff1a 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb ) \u6216 \u6709\u4f5c\u7528\u57df\u679a\u4e3e \u7c7b\u578b(\u53c2\u89c1\uff1a 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b )\uff0c\u6216\u8005\u662f\u4efb\u4f55\u80fd\u8f6c\u6362\u6210\u4e0a\u8ff0\u7c7b\u578b\u7684\u503c\u3002\u6d6e\u70b9\u7c7b\u578b\u3001\u5b57\u7b26\u4e32\u548c\u5927\u591a\u6570\u5176\u4ed6\u975e\u6574\u578b\u7684\u8868\u8fbe\u5f0f\u90fd\u4e0d\u53ef\u7528\u3002 \u6269\u5c55\u9605\u8bfb \u4e3a\u4ec0\u4e48switch\u53ea\u4f7f\u7528\u6574\u6570(\u6216\u679a\u4e3e)\u7c7b\u578b\uff1f\u7b54\u6848\u662f\u56e0\u4e3aswitch\u8bed\u53e5\u88ab\u9ad8\u5ea6\u4f18\u5316\u4e86\u3002\u5386\u53f2\u4e0a\uff0c\u7f16\u8bd1\u5668\u5b9e\u73b0switch\u8bed\u53e5\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u901a\u8fc7 \u8df3\u8868 \u5b9e\u73b0\u7684\uff0c\u800c\u8df3\u8f6c\u8868\u53ea\u9002\u7528\u4e8e\u6574\u6570\u503c\u3002 \u5bf9\u4e8e\u90a3\u4e9b\u5df2\u7ecf\u719f\u6089\u6570\u7ec4\u7684\u4eba\u6765\u8bf4\uff0c\u8df3\u8868\u7684\u5de5\u4f5c\u539f\u7406\u975e\u5e38\u7c7b\u4f3c\u4e8e\u6570\u7ec4\uff0c\u4f7f\u7528\u6574\u6570\u503c\u4f5c\u4e3a\u6570\u7ec4\u7d22\u5f15\uff0c\u76f4\u63a5\u201c\u8df3\u8f6c\u201d\u5230\u7ed3\u679c\u3002\u8fd9\u6bd4\u6267\u884c\u4e00\u5806\u987a\u5e8f\u6bd4\u8f83\u8981\u9ad8\u6548\u5f97\u591a\u3002 \u5f53\u7136\uff0c\u7f16\u8bd1\u5668\u4e0d\u5fc5\u4f7f\u7528\u8df3\u8868\u5b9e\u73b0switch\uff0c\u6709\u65f6\u4e5f\u4e0d\u9700\u8981\u3002\u4ece\u6280\u672f\u4e0a\u8bb2\uff0cC++\u6ca1\u6709\u7406\u7531\u4e0d\u80fd\u653e\u677e\u6b64\u5904\u7684\u7c7b\u578b\u9650\u5236\uff0c\u4ee5\u4fbf\u5176\u4ed6\u7c7b\u578b\u4e5f\u53ef\u4ee5\u4f7f\u7528\uff0c\u53ea\u662f\u76ee\u524d\u8fd8\u6ca1\u6709\u8fd9\u6837\u505a\u7f62\u4e86(\u76f4\u5230C++20)\u3002 \u5728\u6761\u4ef6\u8868\u8fbe\u5f0f\u540e\u9762\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u8bed\u53e5\u5757\u3002\u5728\u8bed\u53e5\u5757\u5185\u90e8\uff0c\u4f7f\u7528\u6807\u7b7e\u6765\u5b9a\u4e49\u6211\u4eec\u60f3\u8981\u6d4b\u8bd5\u662f\u5426\u76f8\u7b49\u7684\u6240\u6709\u503c\uff0c\u6807\u7b7e\u6709\u4e24\u79cd\uff1a","title":"\u521b\u5efa switch"},{"location":"7-4-Switch-statement-basics/#_1","text":"\u7b2c\u4e00\u79cd\u6807\u7b7e\u662f\u5206\u652f\u6807\u7b7e\uff0c\u5b83\u4f7f\u7528 case \u5173\u952e\u5b57\u58f0\u660e\uff0c\u540e\u9762\u8ddf\u7740\u4e00\u4e2a\u5e38\u91cf\u8868\u8fbe\u5f0f\u3002\u5e38\u91cf\u8868\u8fbe\u5f0f\u5fc5\u987b\u5339\u914d\u6761\u4ef6\u7684\u7c7b\u578b\uff0c\u6216\u53ef\u4ee5\u8f6c\u6362\u4e3a\u8be5\u7c7b\u578b\u3002 \u5982\u679c\u6761\u4ef6\u8868\u8fbe\u5f0f\u6c42\u503c\u7ed3\u679c\u7b49\u4e8e\u67d0\u4e2a\u5206\u652f\u6807\u7b7e\u7684\u503c\uff0c\u5219\u4ece\u8be5\u6807\u7b7e\u540e\u9762\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u7ee7\u7eed\u987a\u5e8f\u6267\u884c\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6761\u4ef6\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5339\u914d\u4e00\u4e2a\u5206\u652f\u6807\u7b7e\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include void printDigitName ( int x ) { switch ( x ) // x \u6c42\u503c\u4e3a 2 { case 1 : std :: cout << \"One\" ; return ; case 2 : // \u5339\u914d std :: cout << \"Two\" ; // \u4ece\u8fd9\u91cc\u5f00\u59cb\u6267\u884c return ; // \u6267\u884c\u5230\u8fd9\u6761\u8bed\u53e5\u540e\u5c31\u8fd4\u56de case 3 : std :: cout << \"Three\" ; return ; default : std :: cout << \"Unknown\" ; return ; } } int main () { printDigitName ( 2 ); return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 Two \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c x \u6c42\u503c\u4e3a2\u3002\u56e0\u4e3a\u5206\u652f\u6807\u7b7e\u4e2d\u4e5f\u67092\uff0c\u6240\u4ee5\u7a0b\u5e8f\u4f1a\u8df3\u8f6c\u5230\u8be5\u6807\u7b7e\u4e0b\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u5f00\u59cb\u6267\u884c\u3002\u8be5\u7a0b\u5e8f\u4f1a\u6253\u5370 Two \uff0c\u7136\u540e\u4ece return \u8bed\u53e5\u8fd4\u56de\u7ed9\u8c03\u7528\u8005\u3002 \u5206\u652f\u6807\u7b7e\u7684\u4e2a\u6570\u5e76\u6ca1\u6709\u73b0\u5728\uff0c\u4f46\u662f\u6bcf\u4e2a\u6807\u7b7e\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u6240\u4ee5\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u9519\u8bef\u7684\uff1a 1 2 3 4 5 6 switch ( x ) { case 54 : case 54 : // error: already used value 54! case '6' : // error: '6' converts to integer value 54, which is already used }","title":"\u5206\u652f\u6807\u7b7e"},{"location":"7-4-Switch-statement-basics/#default","text":"\u7b2c\u4e8c\u79cd\u6807\u7b7e\u79f0\u4e3a default \u6807\u7b7e(\u7ecf\u5e38\u88ab\u79f0\u4e3a\u9ed8\u8ba4\u5206\u652f)\uff0c\u4f7f\u7528 default \u5173\u952e\u5b57\u5b9a\u4e49\u3002\u5982\u679c\u6761\u4ef6\u8868\u8fbe\u5f0f\u4e0d\u80fd\u591f\u5339\u914d\u4efb\u4f55\u5176\u4ed6\u6807\u7b7e\u4e14default \u6807\u7b7e\u5b58\u5728\uff0c\u5219\u4ecedefault\u6807\u7b7e\u540e\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\u5f00\u59cb\u6267\u884c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include void printDigitName ( int x ) { switch ( x ) // x is evaluated to produce value 5 { case 1 : std :: cout << \"One\" ; return ; case 2 : std :: cout << \"Two\" ; return ; case 3 : std :: cout << \"Three\" ; return ; default : // which does not match to any case labels std :: cout << \"Unknown\" ; // so execution starts here return ; // and then we return to the caller } } int main () { printDigitName ( 5 ); return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 Unknown default \u5206\u652f\u662f\u53ef\u9009\u7684\uff0c\u5982\u679c\u6709\u7684\u8bdd\u5219\u53ea\u80fd\u6709\u4e00\u4e2a\u3002\u4e00\u822c\u6765\u8bb2\uff0cdefault\u5206\u652f\u4f1a\u88ab\u653e\u7f6e\u5728switch\u8bed\u53e5\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5206\u652f\u3002 \u6700\u4f73\u5b9e\u8df5 \u5c06 default \u4f5c\u4e3a\u6700\u540e\u4e00\u4e2a\u5206\u652f\u3002","title":"default \u6807\u7b7e"},{"location":"7-4-Switch-statement-basics/#_2","text":"\u5982\u679c\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u503c\u4e0d\u5339\u914d\u4efb\u4f55\u5206\u652f\uff0c\u5e76\u4e14\u6ca1\u6709\u63d0\u4f9b\u9ed8\u8ba4\u5206\u652f\uff0c\u5219switch\u5185\u90e8\u4e0d\u4f1a\u6267\u884c\u4efb\u4f55case\u3002\u5728switch\u7ed3\u675f\u540e\u7ee7\u7eed\u6267\u884c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include void printDigitName ( int x ) { switch ( x ) // x is evaluated to produce value 5 { case 1 : std :: cout << \"One\" ; return ; case 2 : std :: cout << \"Two\" ; return ; case 3 : std :: cout << \"Three\" ; return ; // no matching case exists and there is no default case } // so execution continues here std :: cout << \"Hello\" ; } int main () { printDigitName ( 5 ); std :: cout << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c x \u6c42\u503c\u4e3a 5 \uff0c\u4f46\u662f\u7531\u4e8e\u6ca1\u6709\u5339\u914d\u7684\u5206\u652f\uff0c\u4e5f\u6ca1\u6709\u9ed8\u8ba4\u5206\u652f\u3002\u6240\u4ee5\u4e0d\u4f1a\u8d70\u5230\u4efb\u4f55case\u4e2d\uff0c\u7a0b\u5e8f\u5728switch\u5757\u7ed3\u675f\u540e\u7ee7\u7eed\u5411\u4e0b\u6267\u884c\u3002\u6253\u5370 Hello \u3002","title":"\u6ca1\u6709\u5339\u914d\u7684\u5206\u652f\u4e5f\u6ca1\u6709\u9ed8\u8ba4\u5206\u652f"},{"location":"7-4-Switch-statement-basics/#break","text":"\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 return \u8bed\u53e5\u6765\u505c\u6b62\u6807\u7b7e\u540e\u9762\u8bed\u53e5\u7684\u6267\u884c\u3002\u4f46\u662f\uff0c\u8fd9\u4e5f\u4f1a\u9000\u51fa\u6574\u4e2a\u51fd\u6570\u3002 break \u8bed\u53e5(\u4f7f\u7528 break \u5173\u952e\u5b57\u58f0\u660e)\u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u6211\u4eec\u5df2\u7ecf\u5b8c\u6210\u4e86switch\u8bed\u53e5\u7684\u6267\u884c\u4f46\u9700\u8981\u7ee7\u7eed\u6267\u884c\u5916\u9762\u7684\u8bed\u53e5\u3002\u8fd9\u5141\u8bb8\u6211\u4eec\u5728\u4e0d\u9000\u51fa\u6574\u4e2a\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\u9000\u51fa\u201cswitch\u8bed\u53e5\u201d\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u7528 break \u800c\u4e0d\u662f return \u91cd\u5199\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include void printDigitName ( int x ) { switch ( x ) // x evaluates to 3 { case 1 : std :: cout << \"One\" ; break ; case 2 : std :: cout << \"Two\" ; break ; case 3 : std :: cout << \"Three\" ; // \u4ece\u8fd9\u91cc\u5f00\u59cb\u6267\u884c break ; // \u8df3\u5230 switch \u7ed3\u675f\u65f6 default : std :: cout << \"Unknown\" ; break ; } // \u4ece\u8fd9\u91cc\u7ee7\u7eed\u6267\u884c std :: cout << \" Ah-Ah-Ah!\" ; } int main () { printDigitName ( 3 ); return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 Three Ah-Ah-Ah! \u6700\u4f73\u5b9e\u8df5 \u6807\u7b7e\u4e0b\u7684\u6bcf\u7ec4\u8bed\u53e5\u90fd\u5e94\u8be5\u4ee5\u201cbreak\u8bed\u53e5\u201d\u6216\u201creturn\u8bed\u53e5\u201d\u7ed3\u675f\u3002\u8fd9\u5305\u62ecswitch\u6700\u540e\u4e00\u4e2a\u6807\u7b7e\u4e0b\u9762\u7684\u8bed\u53e5\u3002 \u90a3\u4e48\uff0c\u5982\u679c\u4e0d\u4ee5 break \u6216 return \u4f5c\u4e3a\u4e00\u7ec4\u8bed\u53e5\u7684\u7ed3\u5c3e\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462?\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u4e2d\u63a2\u8ba8\u8fd9\u4e2a\u8bdd\u9898\u548c\u5176\u4ed6\u8bdd\u9898\u3002","title":"\u4f7f\u7528 break"},{"location":"7-5-Switch-fallthrough-and-scoping/","tags":["switch"],"text":"Key Takeaway \u672c\u8282\u8bfe\u7ee7\u7eed\u63a2\u7d22switch\u8bed\u53e5\u3002\u5728\u524d\u9762\u7684\u8bfe\u7a0b\u4e2d\uff08 7.4 - switch \u8bed\u53e5\u57fa\u7840 \uff09\u6211\u4eec\u63d0\u5230\u6bcf\u4e2a\u5206\u652f\u6807\u7b7e\u4e0b\u7684\u8bed\u53e5\u90fd\u5e94\u8be5\u4ee5 break \u6216 return \u7ed3\u5c3e\u3002 \u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u7d22\u5176\u4e2d\u7684\u539f\u56e0\uff0c\u5e76\u8ba8\u8bba\u4e00\u4e9b\u5f00\u5173\u4f5c\u7528\u57df\u7684\u95ee\u9898\uff0c\u8fd9\u4e9b\u95ee\u9898\u6709\u65f6\u4f1a\u7eca\u5012\u65b0\u7a0b\u5e8f\u5458\u3002 \u8d2f\u7a7f \u5f53\u4e00\u4e2aswitch\u8868\u8fbe\u5f0f\u5339\u914d\u4e00\u4e2acase\u6807\u7b7e\u6216\u53ef\u9009\u7684\u9ed8\u8ba4\u6807\u7b7e\u65f6\uff0c\u4ece\u5339\u914d\u6807\u7b7e\u540e\u9762\u7684\u7b2c\u4e00\u4e2a\u8bed\u53e5\u5f00\u59cb\u6267\u884c\u3002\u7136\u540e\u7ee7\u7eed\u6267\u884c\uff0c\u76f4\u5230\u4ee5\u4e0b\u7ec8\u6b62\u6761\u4ef6\u4e4b\u4e00\u53d1\u751f\uff1a \u5230\u8fbeswitch\u672b\u5c3e\uff1b \u9047\u5230\u5176\u4ed6\u63a7\u5236\u6d41\u8bed\u53e5\uff08\u4e00\u822c\u6765\u8bf4\u662f break \u6216\u8005 return \uff09\u5bfc\u81f4switch\u9000\u51fa\uff1b \u5176\u4ed6\u4e2d\u65ad\u5bfc\u81f4\u7a0b\u5e8f\u7684\u6b63\u5e38\u6d41\u7a0b\u88ab\u6253\u65ad(\u4f8b\u5982\uff0c\u64cd\u4f5c\u7cfb\u7edf\u505c\u6b62\u7a0b\u5e8f\u3001\u5b87\u5b99\u5927\u7206\u70b8\u7b49\u7b49)\u3002 \u6ce8\u610f\uff0c\u53e6\u4e00\u4e2acase\u6807\u7b7e\u7684\u51fa\u73b0\u4e0d\u662f\u8fd9\u4e9b\u7ec8\u6b62\u6761\u4ef6\u4e4b\u4e00\u2014\u2014\u56e0\u6b64\uff0c\u5982\u679c\u6ca1\u6709 break \u6216 return \uff0c\u6267\u884c\u5c06\u7ee7\u7eed\u6267\u884c\u540e\u7eed\u7684case\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int main () { switch ( 2 ) { case 1 : // Does not match std :: cout << 1 << '\\n' ; // \u8df3\u8fc7 case 2 : // Match! std :: cout << 2 << '\\n' ; // \u4ece\u8fd9\u91cc\u5f00\u59cb\u6267\u884c case 3 : std :: cout << 3 << '\\n' ; // \u4e5f\u4f1a\u88ab\u6267\u884c case 4 : std :: cout << 4 << '\\n' ; // \u4e5f\u4f1a\u88ab\u6267\u884c default : std :: cout << 5 << '\\n' ; // \u4e5f\u4f1a\u88ab\u6267\u884c } return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 2 3 4 5 \u8fd9\u53ef\u80fd\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684!\u5f53\u6267\u884c\u4ece\u6807\u7b7e\u4e0b\u9762\u7684\u8bed\u53e5\u6d41\u5230\u540e\u7eed\u6807\u7b7e\u4e0b\u9762\u7684\u8bed\u53e5\u65f6\uff0c\u8fd9\u79f0\u4e3a\u8d2f\u7a7f\u3002 \u6ce8\u610f \u4e00\u65e6case\u6216default\u6807\u7b7e\u4e0b\u7684\u8bed\u53e5\u5f00\u59cb\u6267\u884c\uff0c\u5b83\u4eec\u9ed8\u8ba4\u4f1a\u8d2f\u7a7f\u5230\u540e\u7eed\u7684case\u4e2d\u3002 break \u6216 return \u53ef\u4ee5\u9632\u6b62\u8fd9\u79cd\u60c5\u51b5\u3002 \u7531\u4e8e\u4eba\u4eec\u5f88\u5c11\u4f1a\u4e3b\u52a8\u4f7f\u7528\u8d2f\u7a7f\u7279\u6027\uff0c\u56e0\u6b64\u8bb8\u591a\u7f16\u8bd1\u5668\u548c\u4ee3\u7801\u5206\u6790\u5de5\u5177\u4f1a\u5c06\u8d2f\u7a7f\u6807\u8bb0\u4e3a\u8b66\u544a\u3002 \u8d2f\u7a7f [fallthrough](./fallthrough.md) \u5c5e\u6027 \u901a\u8fc7\u6ce8\u91ca\u544a\u8bc9\u5176\u4ed6\u5f00\u53d1\u4eba\u5458\u6545\u610f\u5b9e\u73b0\u7684\u8d2f\u7a7f\u662f\u4e00\u79cd\u5e38\u89c1\u60ef\u4f8b\u3002\u867d\u7136\u5176\u4ed6\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u7406\u89e3\uff0c\u4f46\u7f16\u8bd1\u5668\u548c\u4ee3\u7801\u5206\u6790\u5de5\u5177\u4e0d\u77e5\u9053\u5982\u4f55\u89e3\u91ca\u6ce8\u91ca\uff0c\u56e0\u6b64\u5b83\u65e0\u6cd5\u6d88\u9664\u8b66\u544a\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cC++17\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u5c5e\u6027 [fallthrough](./fallthrough.md) \u3002 \u5c5e\u6027\u662f\u73b0\u4ee3C++\u7684\u4e00\u4e2a\u7279\u6027\uff0c\u5b83\u5141\u8bb8\u7a0b\u5e8f\u5458\u5411\u7f16\u8bd1\u5668\u63d0\u4f9b\u4e00\u4e9b\u5173\u4e8e\u4ee3\u7801\u7684\u9644\u52a0\u6570\u636e\u3002\u8981\u6307\u5b9a\u5c5e\u6027\uff0c\u5c5e\u6027\u540d\u8981\u653e\u5728\u53cc\u5927\u62ec\u53f7\u4e4b\u95f4\u3002\u5c5e\u6027\u4e0d\u662f\u8bed\u53e5\u2014\u2014\u76f8\u53cd\uff0c\u5b83\u4eec\u51e0\u4e4e\u53ef\u4ee5\u5728\u4e0e\u4e0a\u4e0b\u6587\u76f8\u5173\u7684\u4efb\u4f55\u5730\u65b9\u4f7f\u7528\u3002 [fallthrough](./fallthrough.md) \u4f1a\u4fee\u6539\u4e00\u4e2a\u7a7a\u8bed\u53e5\uff0c\u8868\u660e\u6709\u610f\u800c\u4e3a\u4e4b\u7684\u8d2f\u7a7f\u64cd\u4f5c\uff08\u907f\u514d\u89e6\u53d1\u544a\u8b66\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { switch ( 2 ) { case 1 : std :: cout << 1 << '\\n' ; break ; case 2 : std :: cout << 2 << '\\n' ; // \u4ece\u8fd9\u91cc\u5f00\u59cb\u6267\u884c [ fallthrough ](. / fallthrough . md ); // \u6709\u610f\u5730\u8d2f\u7a7f\u64cd\u4f5c\u2014\u2014\u6ce8\u610f\u5206\u53f7\u521b\u5efa\u7684\u7a7a\u8bed\u53e5 case 3 : std :: cout << 3 << '\\n' ; // \u540c\u6837\u4f1a\u6267\u884c break ; } return 0 ; } \u8f93\u51fa\uff1a 1 2 2 3 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528 [fallthrough](./fallthrough.md) \u5c5e\u6027(\u548c\u7a7a\u8bed\u53e5)\u8868\u660e\u6709\u610f\u800c\u4e3a\u4e4b\u7684\u8d2f\u7a7f\u64cd\u4f5c\u3002 \u987a\u5e8f\u5206\u652f\u6807\u7b7e \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u6765\u8fde\u63a5\u591a\u4e2a\u6d4b\u8bd5\u6761\u4ef6\uff1a 1 2 3 4 5 bool isVowel ( char c ) { return ( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' ); } \u5728switch\u8bed\u53e5\u4e2d\u4e5f\u6709\u7c7b\u4f3c\u7684\u573a\u666f\uff1ac \u88ab\u591a\u6b21\u6c42\u503c\u5e76\u6d4b\u8bd5\uff0c\u6b64\u65f6\u8bfb\u4ee3\u7801\u7684\u4eba\u5fc5\u987b\u786e\u4fdd\u6bcf\u6b21\u88ab\u6c42\u503c\u548c\u6bd4\u8f83\u7684\u662fc\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u987a\u5e8f\u6392\u5217\u7684\u591a\u4e2a\u5206\u652f\u6807\u7b7e\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 bool isVowel ( char c ) { switch ( c ) { case 'a' : // if c is 'a' case 'e' : // or if c is 'e' case 'i' : // or if c is 'i' case 'o' : // or if c is 'o' case 'u' : // or if c is 'u' case 'A' : // or if c is 'A' case 'E' : // or if c is 'E' case 'I' : // or if c is 'I' case 'O' : // or if c is 'O' case 'U' : // or if c is 'U' return true ; default : return false ; } } \u8bb0\u4f4f\uff0c\u6267\u884c\u4ece\u5339\u914d\u7684case\u6807\u7b7e\u540e\u7684\u7b2c\u4e00\u4e2a\u8bed\u53e5\u5f00\u59cb\u3002case\u6807\u7b7e\u4e0d\u662f\u8bed\u53e5(\u5b83\u4eec\u662f\u6807\u7b7e)\uff0c\u6240\u4ee5\u5b83\u4eec\u4e0d\u7b97\u6570\u3002 \u5728\u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\uff0c\u80fd\u591f\u5339\u914d\u7684 \u6240\u6709 \u6807\u7b7e\u540e\u9762\u7b2c\u4e00\u4e2a\u8bed\u53e5\u662f return true \uff0c\u56e0\u6b64\uff0c\u5982\u679c\u4efb\u4f55\u6807\u7b7e\u5339\u914d\uff0c\u51fd\u6570\u5c06\u8fd4\u56de true \u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u201c\u5806\u53e0\u201d\u6807\u7b7e\uff0c\u4f7f\u6240\u6709\u8fd9\u4e9b\u6807\u7b7e\u5171\u4eab\u76f8\u540c\u7684\u8bed\u53e5\u3002\u8fd9\u4e0d\u5c5e\u4e8e\u8d2f\u7a7f\uff0c\u6240\u4ee5\u4e0d\u5fc5\u6ce8\u91ca\u6216\u6807\u8bb0\u201c [fallthrough](./fallthrough.md) \u201d\u3002 switch-case \u4f5c\u7528\u57df \u5bf9\u4e8e if \u8bed\u53e5\u6765\u8bf4\uff0c\u6761\u4ef6\u540e\u9762\u53ea\u80fd\u6709\u4e00\u6761\u8bed\u53e5\uff08\u6216\u8bed\u53e5\u5757\uff09\uff0c\u8be5\u8bed\u53e5\u88ab\u8ba4\u4e3a\u9690\u5f0f\u5730\u5c5e\u4e8e\u67d0\u4e2a\u8bed\u53e5\u5757\uff1a 1 2 if ( x > 10 ) std :: cout << x << \" is greater than 10 \\n \" ; // this line implicitly considered to be inside a block \u4f46\u662f\uff0c\u5bf9\u4e8e switch \u8bed\u53e5\u6765\u8bf4\uff0c \u67d0\u4e2a\u6807\u7b7e\u540e\u9762\u7684\u8bed\u53e5\u90fd\u5c5e\u4e8eswitch\u8bed\u53e5\u5757\uff0c\u5e76\u6ca1\u6709\u521b\u5efa\u4efb\u4f55\u7684\u9690\u5f0f\u8bed\u53e5\u5757\u3002 1 2 3 4 5 6 7 8 9 switch ( 1 ) { case 1 : // \u5e76\u4e0d\u4f1a\u521b\u5efa\u9690\u5f0f\u5730\u8bed\u53e5\u5757 foo (); // \u5c5e\u4e8eswitch\u8bed\u53e5\u5757\uff0c\u800c\u4e0d\u662fcase1\uff08\u7684\u9690\u5f0f\u8bed\u53e5\u5757\uff09 break ; // \u5c5e\u4e8eswitch\u8bed\u53e5\u5757\uff0c\u800c\u4e0d\u662fcase1\uff08\u7684\u9690\u5f0f\u8bed\u53e5\u5757\uff09 default : std :: cout << \"default case \\n \" ; break ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0ccase 1 \u540e\u9762\u7684\u4e24\u6761\u8bed\u53e5\u548c default \u540e\u9762\u7684\u8bed\u53e5\u90fd\u5c5e\u4e8e switch \u8bed\u53e5\u5757\u3002 case \u8bed\u53e5\u4e2d\u58f0\u660e\u548c\u521d\u59cb\u5316\u53d8\u91cf \u6211\u4eec\u53ef\u4ee5\u5728 switch \u4e2d\u58f0\u660e\u53d8\u91cf (\u4f46\u4e0d\u80fd\u521d\u59cb\u5316)\uff0c\u5728\u6807\u7b7e\u7684\u524d\u540e\u8fdb\u884c\u90fd\u53ef\u4ee5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 switch ( 1 ) { int a ; // okay: declaration is allowed before the case labels int b { 5 }; // illegal: initialization is not allowed before the case labels case 1 : int y ; // okay but bad practice: declaration is allowed within a case y = 4 ; // okay: assignment is allowed break ; case 2 : int z { 4 }; // \u975e\u6cd5\u64cd\u4f5c: \u5982\u679c\u540e\u7eed\u8fd8\u6709\u5206\u652f\uff0c\u5219\u4e0d\u5141\u8bb8\u521d\u59cb\u5316 y = 5 ; // okay: y was declared above, so we can use it here too break ; case 3 : break ; } \u5c3d\u7ba1\u53d8\u91cf y \u662f\u5728 case 1 \u4e2d\u58f0\u660e\u7684\uff0c\u4f46\u662f\u5728 case 2 \u4e2d\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528\u5b83\u3002\u56e0\u4e3a\u6240\u6709\u7684\u8fd9\u4e9b\u8bed\u53e5\u90fd\u4e0d\u5c5e\u4e8e\u67d0\u4e2a\u9690\u5f0f\u7684\u4f5c\u7528\u57df\u800c\u662f\u5c5e\u4e8eswitch\u8bed\u53e5\u5757\uff0c\u6240\u4ee5\u5b83\u4eec\u90fd\u5728\u540c\u4e00\u4e2a\u4f5c\u7528\u57df\u4e2d\uff0c\u524d\u9762\u5b9a\u4e49\u7684\u53d8\u91cf\u81ea\u7136\u53ef\u4ee5\u5728\u540e\u9762\u4f7f\u7528\uff0c\u5373\u4f7f\u5b9a\u4e49\u5b83\u7684\u5206\u652f\u4ece\u6ca1\u88ab\u6267\u884c\u8fc7\u4e5f\u6ca1\u6709\u95ee\u9898\u3002 \u6362\u53e5\u8bdd\u8bf4\uff0c\u5b9a\u4e49\u4e00\u4e2a\u6ca1\u6709\u521d\u59cb\u5316\u503c\u7684\u53d8\u91cf\u53ea\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\u5728\u8fd9\u4e2a\u4f5c\u7528\u57df\u5185\uff0c\u4ece\u6b64\u65f6\u8d77\uff0c\u8fd9\u4e2a\u53d8\u91cf\u6709\u5b9a\u4e49\u4e86\u3002\u8fd9\u4e00\u5207\u90fd\u53d1\u751f\u5728 \u7f16\u8bd1\u65f6 \uff0c\u5b83\u5e76\u4e0d\u8981\u8be5\u5b9a\u4e49\u5728 \u8fd0\u884c\u65f6 \u88ab\u5b9e\u9645\u6267\u884c\u3002 \u4e0d\u8fc7\uff0c\u53d8\u91cf\u7684\u521d\u59cb\u5316\u662f\u5fc5\u987b\u5728 \u8fd0\u884c\u65f6 \u6267\u884c\u624d\u6709\u6548\u7684\u3002\u53d8\u91cf\u7684\u521d\u59cb\u5316\u53ea\u80fd\u5728\u6700\u540e\u4e00\u4e2a\u5206\u652f\u8fdb\u884c\uff08\u5426\u5219\u8be5\u521d\u59cb\u5316\u53ef\u80fd\u88ab\u8df3\u8fc7\u5bfc\u81f4\u53d8\u91cf\u6ca1\u6709\u88ab\u521d\u59cb\u5316\uff09\u3002\u540c\u65f6\uff0c\u53d8\u91cf\u7684\u521d\u59cb\u5316\u4e5f\u4e0d\u80fd\u5728\u7b2c\u4e00\u4e2a\u5206\u652f\u4e4b\u524d\u8fdb\u884c\uff0c\u56e0\u4e3a\u8fd9\u90e8\u5206\u4ee3\u7801\u6c38\u8fdc\u4e0d\u4f1a\u88ab\u6267\u884c\u3002 \u5982\u679ccase\u9700\u8981\u5b9a\u4e49\u548c/\u6216\u521d\u59cb\u5316\u4e00\u4e2a\u65b0\u53d8\u91cf\uff0c\u6700\u4f73\u505a\u6cd5\u662f\u5728case\u8bed\u53e5\u4e0b\u9762\u7684\u663e\u5f0f\u5757\u4e2d\u5b8c\u6210: 1 2 3 4 5 6 7 8 9 10 11 12 switch ( 1 ) { case 1 : { // note addition of explicit block here int x { 4 }; // okay, variables can be initialized inside a block inside a case std :: cout << x ; break ; } default : std :: cout << \"default case \\n \" ; break ; } COPY \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u9700\u8981\u5b9a\u4e49\u548c/\u6216\u521d\u59cb\u5316\u4e00\u4e2a\u65b0\u53d8\u91cf\uff0c\u6700\u4f73\u505a\u6cd5\u662f\u5728case\u8bed\u53e5\u4e0b\u9762\u7684\u663e\u5f0f\u5757\u4e2d\u5b8c\u6210","title":"7.5 - switch fallthrough\u5c5e\u6027\u548c\u4f5c\u7528\u57df"},{"location":"7-5-Switch-fallthrough-and-scoping/#_1","text":"\u5f53\u4e00\u4e2aswitch\u8868\u8fbe\u5f0f\u5339\u914d\u4e00\u4e2acase\u6807\u7b7e\u6216\u53ef\u9009\u7684\u9ed8\u8ba4\u6807\u7b7e\u65f6\uff0c\u4ece\u5339\u914d\u6807\u7b7e\u540e\u9762\u7684\u7b2c\u4e00\u4e2a\u8bed\u53e5\u5f00\u59cb\u6267\u884c\u3002\u7136\u540e\u7ee7\u7eed\u6267\u884c\uff0c\u76f4\u5230\u4ee5\u4e0b\u7ec8\u6b62\u6761\u4ef6\u4e4b\u4e00\u53d1\u751f\uff1a \u5230\u8fbeswitch\u672b\u5c3e\uff1b \u9047\u5230\u5176\u4ed6\u63a7\u5236\u6d41\u8bed\u53e5\uff08\u4e00\u822c\u6765\u8bf4\u662f break \u6216\u8005 return \uff09\u5bfc\u81f4switch\u9000\u51fa\uff1b \u5176\u4ed6\u4e2d\u65ad\u5bfc\u81f4\u7a0b\u5e8f\u7684\u6b63\u5e38\u6d41\u7a0b\u88ab\u6253\u65ad(\u4f8b\u5982\uff0c\u64cd\u4f5c\u7cfb\u7edf\u505c\u6b62\u7a0b\u5e8f\u3001\u5b87\u5b99\u5927\u7206\u70b8\u7b49\u7b49)\u3002 \u6ce8\u610f\uff0c\u53e6\u4e00\u4e2acase\u6807\u7b7e\u7684\u51fa\u73b0\u4e0d\u662f\u8fd9\u4e9b\u7ec8\u6b62\u6761\u4ef6\u4e4b\u4e00\u2014\u2014\u56e0\u6b64\uff0c\u5982\u679c\u6ca1\u6709 break \u6216 return \uff0c\u6267\u884c\u5c06\u7ee7\u7eed\u6267\u884c\u540e\u7eed\u7684case\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int main () { switch ( 2 ) { case 1 : // Does not match std :: cout << 1 << '\\n' ; // \u8df3\u8fc7 case 2 : // Match! std :: cout << 2 << '\\n' ; // \u4ece\u8fd9\u91cc\u5f00\u59cb\u6267\u884c case 3 : std :: cout << 3 << '\\n' ; // \u4e5f\u4f1a\u88ab\u6267\u884c case 4 : std :: cout << 4 << '\\n' ; // \u4e5f\u4f1a\u88ab\u6267\u884c default : std :: cout << 5 << '\\n' ; // \u4e5f\u4f1a\u88ab\u6267\u884c } return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 2 3 4 5 \u8fd9\u53ef\u80fd\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684!\u5f53\u6267\u884c\u4ece\u6807\u7b7e\u4e0b\u9762\u7684\u8bed\u53e5\u6d41\u5230\u540e\u7eed\u6807\u7b7e\u4e0b\u9762\u7684\u8bed\u53e5\u65f6\uff0c\u8fd9\u79f0\u4e3a\u8d2f\u7a7f\u3002 \u6ce8\u610f \u4e00\u65e6case\u6216default\u6807\u7b7e\u4e0b\u7684\u8bed\u53e5\u5f00\u59cb\u6267\u884c\uff0c\u5b83\u4eec\u9ed8\u8ba4\u4f1a\u8d2f\u7a7f\u5230\u540e\u7eed\u7684case\u4e2d\u3002 break \u6216 return \u53ef\u4ee5\u9632\u6b62\u8fd9\u79cd\u60c5\u51b5\u3002 \u7531\u4e8e\u4eba\u4eec\u5f88\u5c11\u4f1a\u4e3b\u52a8\u4f7f\u7528\u8d2f\u7a7f\u7279\u6027\uff0c\u56e0\u6b64\u8bb8\u591a\u7f16\u8bd1\u5668\u548c\u4ee3\u7801\u5206\u6790\u5de5\u5177\u4f1a\u5c06\u8d2f\u7a7f\u6807\u8bb0\u4e3a\u8b66\u544a\u3002","title":"\u8d2f\u7a7f"},{"location":"7-5-Switch-fallthrough-and-scoping/#fallthroughfallthroughmd","text":"\u901a\u8fc7\u6ce8\u91ca\u544a\u8bc9\u5176\u4ed6\u5f00\u53d1\u4eba\u5458\u6545\u610f\u5b9e\u73b0\u7684\u8d2f\u7a7f\u662f\u4e00\u79cd\u5e38\u89c1\u60ef\u4f8b\u3002\u867d\u7136\u5176\u4ed6\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u7406\u89e3\uff0c\u4f46\u7f16\u8bd1\u5668\u548c\u4ee3\u7801\u5206\u6790\u5de5\u5177\u4e0d\u77e5\u9053\u5982\u4f55\u89e3\u91ca\u6ce8\u91ca\uff0c\u56e0\u6b64\u5b83\u65e0\u6cd5\u6d88\u9664\u8b66\u544a\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cC++17\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u5c5e\u6027 [fallthrough](./fallthrough.md) \u3002 \u5c5e\u6027\u662f\u73b0\u4ee3C++\u7684\u4e00\u4e2a\u7279\u6027\uff0c\u5b83\u5141\u8bb8\u7a0b\u5e8f\u5458\u5411\u7f16\u8bd1\u5668\u63d0\u4f9b\u4e00\u4e9b\u5173\u4e8e\u4ee3\u7801\u7684\u9644\u52a0\u6570\u636e\u3002\u8981\u6307\u5b9a\u5c5e\u6027\uff0c\u5c5e\u6027\u540d\u8981\u653e\u5728\u53cc\u5927\u62ec\u53f7\u4e4b\u95f4\u3002\u5c5e\u6027\u4e0d\u662f\u8bed\u53e5\u2014\u2014\u76f8\u53cd\uff0c\u5b83\u4eec\u51e0\u4e4e\u53ef\u4ee5\u5728\u4e0e\u4e0a\u4e0b\u6587\u76f8\u5173\u7684\u4efb\u4f55\u5730\u65b9\u4f7f\u7528\u3002 [fallthrough](./fallthrough.md) \u4f1a\u4fee\u6539\u4e00\u4e2a\u7a7a\u8bed\u53e5\uff0c\u8868\u660e\u6709\u610f\u800c\u4e3a\u4e4b\u7684\u8d2f\u7a7f\u64cd\u4f5c\uff08\u907f\u514d\u89e6\u53d1\u544a\u8b66\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { switch ( 2 ) { case 1 : std :: cout << 1 << '\\n' ; break ; case 2 : std :: cout << 2 << '\\n' ; // \u4ece\u8fd9\u91cc\u5f00\u59cb\u6267\u884c [ fallthrough ](. / fallthrough . md ); // \u6709\u610f\u5730\u8d2f\u7a7f\u64cd\u4f5c\u2014\u2014\u6ce8\u610f\u5206\u53f7\u521b\u5efa\u7684\u7a7a\u8bed\u53e5 case 3 : std :: cout << 3 << '\\n' ; // \u540c\u6837\u4f1a\u6267\u884c break ; } return 0 ; } \u8f93\u51fa\uff1a 1 2 2 3 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528 [fallthrough](./fallthrough.md) \u5c5e\u6027(\u548c\u7a7a\u8bed\u53e5)\u8868\u660e\u6709\u610f\u800c\u4e3a\u4e4b\u7684\u8d2f\u7a7f\u64cd\u4f5c\u3002","title":"\u8d2f\u7a7f [fallthrough](./fallthrough.md) \u5c5e\u6027"},{"location":"7-5-Switch-fallthrough-and-scoping/#_2","text":"\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u6765\u8fde\u63a5\u591a\u4e2a\u6d4b\u8bd5\u6761\u4ef6\uff1a 1 2 3 4 5 bool isVowel ( char c ) { return ( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' ); } \u5728switch\u8bed\u53e5\u4e2d\u4e5f\u6709\u7c7b\u4f3c\u7684\u573a\u666f\uff1ac \u88ab\u591a\u6b21\u6c42\u503c\u5e76\u6d4b\u8bd5\uff0c\u6b64\u65f6\u8bfb\u4ee3\u7801\u7684\u4eba\u5fc5\u987b\u786e\u4fdd\u6bcf\u6b21\u88ab\u6c42\u503c\u548c\u6bd4\u8f83\u7684\u662fc\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u987a\u5e8f\u6392\u5217\u7684\u591a\u4e2a\u5206\u652f\u6807\u7b7e\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 bool isVowel ( char c ) { switch ( c ) { case 'a' : // if c is 'a' case 'e' : // or if c is 'e' case 'i' : // or if c is 'i' case 'o' : // or if c is 'o' case 'u' : // or if c is 'u' case 'A' : // or if c is 'A' case 'E' : // or if c is 'E' case 'I' : // or if c is 'I' case 'O' : // or if c is 'O' case 'U' : // or if c is 'U' return true ; default : return false ; } } \u8bb0\u4f4f\uff0c\u6267\u884c\u4ece\u5339\u914d\u7684case\u6807\u7b7e\u540e\u7684\u7b2c\u4e00\u4e2a\u8bed\u53e5\u5f00\u59cb\u3002case\u6807\u7b7e\u4e0d\u662f\u8bed\u53e5(\u5b83\u4eec\u662f\u6807\u7b7e)\uff0c\u6240\u4ee5\u5b83\u4eec\u4e0d\u7b97\u6570\u3002 \u5728\u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\uff0c\u80fd\u591f\u5339\u914d\u7684 \u6240\u6709 \u6807\u7b7e\u540e\u9762\u7b2c\u4e00\u4e2a\u8bed\u53e5\u662f return true \uff0c\u56e0\u6b64\uff0c\u5982\u679c\u4efb\u4f55\u6807\u7b7e\u5339\u914d\uff0c\u51fd\u6570\u5c06\u8fd4\u56de true \u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u201c\u5806\u53e0\u201d\u6807\u7b7e\uff0c\u4f7f\u6240\u6709\u8fd9\u4e9b\u6807\u7b7e\u5171\u4eab\u76f8\u540c\u7684\u8bed\u53e5\u3002\u8fd9\u4e0d\u5c5e\u4e8e\u8d2f\u7a7f\uff0c\u6240\u4ee5\u4e0d\u5fc5\u6ce8\u91ca\u6216\u6807\u8bb0\u201c [fallthrough](./fallthrough.md) \u201d\u3002","title":"\u987a\u5e8f\u5206\u652f\u6807\u7b7e"},{"location":"7-5-Switch-fallthrough-and-scoping/#switch-case","text":"\u5bf9\u4e8e if \u8bed\u53e5\u6765\u8bf4\uff0c\u6761\u4ef6\u540e\u9762\u53ea\u80fd\u6709\u4e00\u6761\u8bed\u53e5\uff08\u6216\u8bed\u53e5\u5757\uff09\uff0c\u8be5\u8bed\u53e5\u88ab\u8ba4\u4e3a\u9690\u5f0f\u5730\u5c5e\u4e8e\u67d0\u4e2a\u8bed\u53e5\u5757\uff1a 1 2 if ( x > 10 ) std :: cout << x << \" is greater than 10 \\n \" ; // this line implicitly considered to be inside a block \u4f46\u662f\uff0c\u5bf9\u4e8e switch \u8bed\u53e5\u6765\u8bf4\uff0c \u67d0\u4e2a\u6807\u7b7e\u540e\u9762\u7684\u8bed\u53e5\u90fd\u5c5e\u4e8eswitch\u8bed\u53e5\u5757\uff0c\u5e76\u6ca1\u6709\u521b\u5efa\u4efb\u4f55\u7684\u9690\u5f0f\u8bed\u53e5\u5757\u3002 1 2 3 4 5 6 7 8 9 switch ( 1 ) { case 1 : // \u5e76\u4e0d\u4f1a\u521b\u5efa\u9690\u5f0f\u5730\u8bed\u53e5\u5757 foo (); // \u5c5e\u4e8eswitch\u8bed\u53e5\u5757\uff0c\u800c\u4e0d\u662fcase1\uff08\u7684\u9690\u5f0f\u8bed\u53e5\u5757\uff09 break ; // \u5c5e\u4e8eswitch\u8bed\u53e5\u5757\uff0c\u800c\u4e0d\u662fcase1\uff08\u7684\u9690\u5f0f\u8bed\u53e5\u5757\uff09 default : std :: cout << \"default case \\n \" ; break ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0ccase 1 \u540e\u9762\u7684\u4e24\u6761\u8bed\u53e5\u548c default \u540e\u9762\u7684\u8bed\u53e5\u90fd\u5c5e\u4e8e switch \u8bed\u53e5\u5757\u3002","title":"switch-case \u4f5c\u7528\u57df"},{"location":"7-5-Switch-fallthrough-and-scoping/#case","text":"\u6211\u4eec\u53ef\u4ee5\u5728 switch \u4e2d\u58f0\u660e\u53d8\u91cf (\u4f46\u4e0d\u80fd\u521d\u59cb\u5316)\uff0c\u5728\u6807\u7b7e\u7684\u524d\u540e\u8fdb\u884c\u90fd\u53ef\u4ee5\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 switch ( 1 ) { int a ; // okay: declaration is allowed before the case labels int b { 5 }; // illegal: initialization is not allowed before the case labels case 1 : int y ; // okay but bad practice: declaration is allowed within a case y = 4 ; // okay: assignment is allowed break ; case 2 : int z { 4 }; // \u975e\u6cd5\u64cd\u4f5c: \u5982\u679c\u540e\u7eed\u8fd8\u6709\u5206\u652f\uff0c\u5219\u4e0d\u5141\u8bb8\u521d\u59cb\u5316 y = 5 ; // okay: y was declared above, so we can use it here too break ; case 3 : break ; } \u5c3d\u7ba1\u53d8\u91cf y \u662f\u5728 case 1 \u4e2d\u58f0\u660e\u7684\uff0c\u4f46\u662f\u5728 case 2 \u4e2d\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528\u5b83\u3002\u56e0\u4e3a\u6240\u6709\u7684\u8fd9\u4e9b\u8bed\u53e5\u90fd\u4e0d\u5c5e\u4e8e\u67d0\u4e2a\u9690\u5f0f\u7684\u4f5c\u7528\u57df\u800c\u662f\u5c5e\u4e8eswitch\u8bed\u53e5\u5757\uff0c\u6240\u4ee5\u5b83\u4eec\u90fd\u5728\u540c\u4e00\u4e2a\u4f5c\u7528\u57df\u4e2d\uff0c\u524d\u9762\u5b9a\u4e49\u7684\u53d8\u91cf\u81ea\u7136\u53ef\u4ee5\u5728\u540e\u9762\u4f7f\u7528\uff0c\u5373\u4f7f\u5b9a\u4e49\u5b83\u7684\u5206\u652f\u4ece\u6ca1\u88ab\u6267\u884c\u8fc7\u4e5f\u6ca1\u6709\u95ee\u9898\u3002 \u6362\u53e5\u8bdd\u8bf4\uff0c\u5b9a\u4e49\u4e00\u4e2a\u6ca1\u6709\u521d\u59cb\u5316\u503c\u7684\u53d8\u91cf\u53ea\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\u5728\u8fd9\u4e2a\u4f5c\u7528\u57df\u5185\uff0c\u4ece\u6b64\u65f6\u8d77\uff0c\u8fd9\u4e2a\u53d8\u91cf\u6709\u5b9a\u4e49\u4e86\u3002\u8fd9\u4e00\u5207\u90fd\u53d1\u751f\u5728 \u7f16\u8bd1\u65f6 \uff0c\u5b83\u5e76\u4e0d\u8981\u8be5\u5b9a\u4e49\u5728 \u8fd0\u884c\u65f6 \u88ab\u5b9e\u9645\u6267\u884c\u3002 \u4e0d\u8fc7\uff0c\u53d8\u91cf\u7684\u521d\u59cb\u5316\u662f\u5fc5\u987b\u5728 \u8fd0\u884c\u65f6 \u6267\u884c\u624d\u6709\u6548\u7684\u3002\u53d8\u91cf\u7684\u521d\u59cb\u5316\u53ea\u80fd\u5728\u6700\u540e\u4e00\u4e2a\u5206\u652f\u8fdb\u884c\uff08\u5426\u5219\u8be5\u521d\u59cb\u5316\u53ef\u80fd\u88ab\u8df3\u8fc7\u5bfc\u81f4\u53d8\u91cf\u6ca1\u6709\u88ab\u521d\u59cb\u5316\uff09\u3002\u540c\u65f6\uff0c\u53d8\u91cf\u7684\u521d\u59cb\u5316\u4e5f\u4e0d\u80fd\u5728\u7b2c\u4e00\u4e2a\u5206\u652f\u4e4b\u524d\u8fdb\u884c\uff0c\u56e0\u4e3a\u8fd9\u90e8\u5206\u4ee3\u7801\u6c38\u8fdc\u4e0d\u4f1a\u88ab\u6267\u884c\u3002 \u5982\u679ccase\u9700\u8981\u5b9a\u4e49\u548c/\u6216\u521d\u59cb\u5316\u4e00\u4e2a\u65b0\u53d8\u91cf\uff0c\u6700\u4f73\u505a\u6cd5\u662f\u5728case\u8bed\u53e5\u4e0b\u9762\u7684\u663e\u5f0f\u5757\u4e2d\u5b8c\u6210: 1 2 3 4 5 6 7 8 9 10 11 12 switch ( 1 ) { case 1 : { // note addition of explicit block here int x { 4 }; // okay, variables can be initialized inside a block inside a case std :: cout << x ; break ; } default : std :: cout << \"default case \\n \" ; break ; } COPY \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u9700\u8981\u5b9a\u4e49\u548c/\u6216\u521d\u59cb\u5316\u4e00\u4e2a\u65b0\u53d8\u91cf\uff0c\u6700\u4f73\u505a\u6cd5\u662f\u5728case\u8bed\u53e5\u4e0b\u9762\u7684\u663e\u5f0f\u5757\u4e2d\u5b8c\u6210","title":"case \u8bed\u53e5\u4e2d\u58f0\u660e\u548c\u521d\u59cb\u5316\u53d8\u91cf"},{"location":"7-6-Goto-statements/","tags":["goto"],"text":"Key Takeaway The next kind of control flow statement we\u2019ll cover is the unconditional jump. An unconditional jump causes execution to jump to another spot in the code. The term \u201cunconditional\u201d means the jump always happens (unlike an if statement or switch statement , where the jump only happens conditionally based on the result of an expression). In C++, unconditional jumps are implemented via a goto statement, and the spot to jump to is identified through use of a statement label. The following is an example of a goto statement and statement label : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include // for sqrt() function int main () { double x {}; tryAgain : // this is a statement label std :: cout << \"Enter a non-negative number: \" ; std :: cin >> x ; if ( x < 0.0 ) goto tryAgain ; // this is the goto statement std :: cout << \"The square root of \" << x << \" is \" << std :: sqrt ( x ) << '\\n' ; return 0 ; } COPY In this program, the user is asked to enter a non-negative number. However, if a negative number is entered, the program utilizes a goto statement to jump back to the tryAgain label. The user is then asked again to enter a new number. In this way, we can continually ask the user for input until he or she enters something valid. Here\u2019s a sample run of this program: 1 2 3 Enter a non-negative number: -4 Enter a non-negative number: 4 The square root of 4 is 2 \u8bed\u53e5\u6807\u7b7e\u5177\u6709\u51fd\u6570\u4f5c\u7528\u57df In the chapter on object scope ( chapter 6 ), we covered two kinds of scope: local (block) scope, and file (global) scope. Statement labels utilize a third kind of scope: function scope, which means the label is visible throughout the function even before its point of declaration. The goto statement and its corresponding statement label must appear in the same function. While the above example shows a goto statement that jumps backwards (to a preceding point in the function), goto statements can also jump forward: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void printCats ( bool skip ) { if ( skip ) goto end ; // jump forward; statement label 'end' is visible here due to it having function scope std :: cout << \"cats\" ; end : ; // statement labels must be associated with a statement } int main () { printCats ( true ); // jumps over the print statement and doesn't print anything printCats ( false ); // prints \"cats\" return 0 ; } COPY This prints: 1 cats Beyond the jumping forward, there are a couple of interesting things worth mentioning in the program above. First, note that statement labels must be associated with a statement (hence their name: they label a statement). Because the end of the function had no statement, we had to use a null statement so we had a statement to label. Second, we were able to jump to the statement labeled by end even though we hadn\u2019t declared end yet due to statement labels having function scope. No forward declaration of statement labels is necessary. Third, it\u2019s worth explicitly mentioning that the above program is poor form -- it would have been better to use an if statement to skip the print statement than a goto statement to jump over it. There are two primary limitations to jumping: You can jump only within the bounds of a single function (you can\u2019t jump out of one function and into another), and if you jump forward, you can\u2019t jump forward over the initialization of any variable that is still in scope at the location being jumped to. For example: 1 2 3 4 5 6 7 8 int main () { goto skip ; // error: this jump is illegal because... int x { 5 }; // this initialized variable is still in scope at statement label 'skip' skip : x += 3 ; // what would this even evaluate to if x wasn't initialized? return 0 ; } COPY Note that you can jump backwards over a variable initialization, and the variable will be re-initialized when the initialization is executed. \u907f\u514d\u4f7f\u7528 goto \u8bed\u53e5 Use of goto is shunned in C++ (and other modern high level languages as well). Edsger W. Dijkstra , a noted computer scientist, laid out the case for avoiding goto in a famous but difficult to read paper called Go To Statement Considered Harmful . The primary problem with goto is that it allows a programmer to jump around the code arbitrarily. This creates what is not-so-affectionately known as spaghetti code . Spaghetti code is code that has a path of execution that resembles a bowl of spaghetti (all tangled and twisted), making it extremely difficult to follow the logic of such code. As Dijkstra says somewhat humorously, \u201cthe quality of programmers is a decreasing function of the density of go to statements in the programs they produce\u201d. Almost any code written using a goto statement can be more clearly written using other constructs in C++, such as if statements and loops. One notable exception is when you need to exit a nested loop but not the entire function -- in such a case, a goto to just beyond the loops is probably the cleanest solution. \u6700\u4f73\u5b9e\u8df5 Avoid goto statements (unless the alternatives are significantly worse for code readability).","title":"7.6 - goto \u8bed\u53e5"},{"location":"7-6-Goto-statements/#_1","text":"In the chapter on object scope ( chapter 6 ), we covered two kinds of scope: local (block) scope, and file (global) scope. Statement labels utilize a third kind of scope: function scope, which means the label is visible throughout the function even before its point of declaration. The goto statement and its corresponding statement label must appear in the same function. While the above example shows a goto statement that jumps backwards (to a preceding point in the function), goto statements can also jump forward: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void printCats ( bool skip ) { if ( skip ) goto end ; // jump forward; statement label 'end' is visible here due to it having function scope std :: cout << \"cats\" ; end : ; // statement labels must be associated with a statement } int main () { printCats ( true ); // jumps over the print statement and doesn't print anything printCats ( false ); // prints \"cats\" return 0 ; } COPY This prints: 1 cats Beyond the jumping forward, there are a couple of interesting things worth mentioning in the program above. First, note that statement labels must be associated with a statement (hence their name: they label a statement). Because the end of the function had no statement, we had to use a null statement so we had a statement to label. Second, we were able to jump to the statement labeled by end even though we hadn\u2019t declared end yet due to statement labels having function scope. No forward declaration of statement labels is necessary. Third, it\u2019s worth explicitly mentioning that the above program is poor form -- it would have been better to use an if statement to skip the print statement than a goto statement to jump over it. There are two primary limitations to jumping: You can jump only within the bounds of a single function (you can\u2019t jump out of one function and into another), and if you jump forward, you can\u2019t jump forward over the initialization of any variable that is still in scope at the location being jumped to. For example: 1 2 3 4 5 6 7 8 int main () { goto skip ; // error: this jump is illegal because... int x { 5 }; // this initialized variable is still in scope at statement label 'skip' skip : x += 3 ; // what would this even evaluate to if x wasn't initialized? return 0 ; } COPY Note that you can jump backwards over a variable initialization, and the variable will be re-initialized when the initialization is executed.","title":"\u8bed\u53e5\u6807\u7b7e\u5177\u6709\u51fd\u6570\u4f5c\u7528\u57df"},{"location":"7-6-Goto-statements/#goto","text":"Use of goto is shunned in C++ (and other modern high level languages as well). Edsger W. Dijkstra , a noted computer scientist, laid out the case for avoiding goto in a famous but difficult to read paper called Go To Statement Considered Harmful . The primary problem with goto is that it allows a programmer to jump around the code arbitrarily. This creates what is not-so-affectionately known as spaghetti code . Spaghetti code is code that has a path of execution that resembles a bowl of spaghetti (all tangled and twisted), making it extremely difficult to follow the logic of such code. As Dijkstra says somewhat humorously, \u201cthe quality of programmers is a decreasing function of the density of go to statements in the programs they produce\u201d. Almost any code written using a goto statement can be more clearly written using other constructs in C++, such as if statements and loops. One notable exception is when you need to exit a nested loop but not the entire function -- in such a case, a goto to just beyond the loops is probably the cleanest solution. \u6700\u4f73\u5b9e\u8df5 Avoid goto statements (unless the alternatives are significantly worse for code readability).","title":"\u907f\u514d\u4f7f\u7528 goto \u8bed\u53e5"},{"location":"7-7-Intro-to-loops-and-while-statements/","tags":["while","for"],"text":"Key Takeaway \u5faa\u73af \u73b0\u5728\u771f\u6b63\u7684\u4e50\u8da3\u5f00\u59cb\u4e86\u2014\u2014\u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5faa\u73af\u3002\u5faa\u73af\u662f\u63a7\u5236\u6d41\u7ed3\u6784\uff0c\u5b83\u5141\u8bb8\u4e00\u6bb5\u4ee3\u7801\u88ab\u91cd\u590d\u6267\u884c\uff0c\u76f4\u5230\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u3002\u5faa\u73af\u4e3a\u7f16\u7a0b\u5de5\u5177\u5305\u589e\u52a0\u4e86\u5f88\u5927\u7684\u7075\u6d3b\u6027\uff0c\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u5f88\u591a\u4e0d\u4f7f\u7528\u5faa\u73af\u96be\u4ee5\u5b9e\u73b0\u7684\u529f\u80fd\u3002 \u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u60f3\u8981\u6253\u53701\u523010\uff0c\u5982\u679c\u6ca1\u6709\u5faa\u73af\uff0c\u4f60\u53ef\u80fd\u4f1a\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << \"1 2 3 4 5 6 7 8 9 10\" ; std :: cout << \" done! \\n \" ; return 0 ; } \u867d\u7136\u8fd9\u4e48\u505a\u662f\u53ef\u884c\u7684\uff0c\u4f46\u5f53\u6211\u4eec\u9700\u8981\u6253\u5370\u66f4\u591a\u7684\u6570\u5b57\u65f6\uff0c\u5b83\u4f1a\u53d8\u5f97\u8d8a\u6765\u8d8a\u4e0d\u53ef\u884c\uff1a\u5982\u679c\u9700\u8981\u6253\u53701\u52301000\u4e4b\u95f4\u7684\u6240\u6709\u6570\u5b57\u5462\uff1f\u90a3\u6253\u5b57\u7684\u5de5\u4f5c\u91cf\u5c31\u592a\u5927\u4e86\uff01\u800c\u4e14\u8be5\u7a0b\u5e8f\u8981\u6253\u5370\u7684\u5185\u5bb9\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u5c31\u77e5\u9053\u8981\u6253\u5370\u591a\u5c11\u6570\u5b57\u3002 \u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u7a0d\u5fae\u6539\u53d8\u4e00\u4e0b\u53c2\u6570\u3002\u5982\u679c\u9700\u8981\u8ba9\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff0c\u7136\u540e\u6253\u53701\u548c\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\u4e4b\u95f4\u7684\u6240\u6709\u6570\u5b57\uff0c\u90a3\u4f1a\u600e\u4e48\u6837\u5462?\u7528\u6237\u5c06\u8f93\u5165\u7684\u6570\u5b57\u5728\u7f16\u8bd1\u65f6\u662f\u4e0d\u53ef\u77e5\u7684\uff0c\u8981\u600e\u4e48\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5462? while \u8bed\u53e5 while \u8bed\u53e5\uff08\u4e5f\u88ab\u53eb\u505a while \u5faa\u73af\uff09\u662fC++\u4e2d\u6700\u7b80\u5355\u5faa\u73af\uff0c\u5b83\u7684\u5b9a\u4e49\u548c if \u8bed\u53e5\u7279\u522b\u5730\u50cf\uff1a 1 2 while ( condition ) statement ; while \u8bed\u53e5\u7684\u58f0\u660e\u9700\u8981\u4f7f\u7528 while \u5173\u952e\u5b57\u3002\u5f53 while \u8bed\u53e5\u6267\u884c\u65f6\uff0c condition \u4f1a\u88ab\u6c42\u503c\u3002\u5982\u679c\u6c42\u503c\u7ed3\u679c\u662f\u771f\uff0c\u5219\u76f8\u5173\u7684\u8bed\u53e5\u4f1a\u88ab\u6267\u884c\u3002 \u4f46\u662f\uff0c\u548cif\u8bed\u53e5\u4e0d\u540c\u7684\u662f\uff0c while \u8bed\u53e5\u4e2d\u7684 statement \u6267\u884c\u5b8c\u6210\u540e\uff0c\u4f1a\u8fd4\u56de\u5230 while \u8bed\u53e5\u7684\u5f00\u59cb\uff0c\u7136\u540e\u7ee7\u7eed\u6267\u884c\u3001\u8fd9\u610f\u5473\u7740\u53ea\u8981\u6761\u4ef6\u6c42\u503c\u4e3a\u771f\uff0c while \u5faa\u73af\u5c31\u4f1a\u4e0d\u505c\u5730\u6267\u884c\u3002 \u8fd8\u662f\u6253\u53701\u523010\u7684\u4f8b\u5b50\uff0c\u8fd9\u6b21\u4f7f\u7528 while \u5faa\u73af\u5b8c\u6210\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int count { 1 }; while ( count <= 10 ) { std :: cout << count << ' ' ; ++ count ; } std :: cout << \"done! \\n \" ; return 0 ; } \u8f93\u51fa\uff1a 1 1 2 3 4 5 6 7 8 9 10 done! \u8ba9\u6211\u4eec\u4ed4\u7ec6\u770b\u770b\u8fd9\u4e2a\u7a0b\u5e8f\u6b63\u5728\u505a\u4ec0\u4e48\u3002\u9996\u5148\uff0c count \u88ab\u521d\u59cb\u5316\u4e3a1\uff0c\u8fd9\u662f\u6211\u4eec\u8981\u6253\u5370\u7684\u7b2c\u4e00\u4e2a\u6570\u5b57\u3002\u6761\u4ef6 count <= 10 \u4e3a true \uff0c\u56e0\u6b64\u6267\u884c\u8bed\u53e5\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u7684\u8bed\u53e5\u662f\u4e00\u4e2a\u8bed\u53e5\u5757\uff0c\u56e0\u6b64\u5757\u4e2d\u7684\u6240\u6709\u8bed\u53e5\u90fd\u5c06\u6267\u884c\u3002\u5757\u4e2d\u7684\u7b2c\u4e00\u4e2a\u8bed\u53e5\u8f93\u51fa 1 \u548c\u4e00\u4e2a\u7a7a\u683c\uff0c\u7b2c\u4e8c\u4e2a\u8bed\u53e5\u5c06 count \u9012\u589e\u52302\u3002\u63a7\u5236\u73b0\u5728\u8fd4\u56de\u5230 while \u7684\u9876\u90e8\uff0c\u5e76\u518d\u6b21\u8ba1\u7b97\u6761\u4ef6\u3002 2 <= 10 \u7684\u8ba1\u7b97\u7ed3\u679c\u4e3a\u4ecd\u7136 true \uff0c\u56e0\u6b64\u4ee3\u7801\u5757\u5c06\u518d\u6b21\u6267\u884c\u3002\u5faa\u73af\u5c06\u91cd\u590d\u6267\u884c\uff0c\u76f4\u5230 count \u4e3a11 \uff0c\u6b64\u65f6 11 <= 10 \u5c06\u88ab\u8ba1\u7b97\u4e3a false \uff0c\u4e0e\u5faa\u73af\u76f8\u5173\u7684\u8bed\u53e5\u5c06\u88ab\u8df3\u8fc7\u3002\u81f3\u6b64\uff0c\u5faa\u73af\u5b8c\u6210\u4e86\u3002 \u867d\u7136\u4e0a\u9762\u7684\u4ee3\u7801\u770b\u4e0a\u53bb\u4f3c\u4e4e\u6bd4\u624b\u52a8\u8f93\u51651\u523010\u8fd8\u591a\u4e9b\uff0c\u4f46\u662f\u4f60\u53ef\u4ee5\u60f3\u8c61\uff0c\u5982\u679c\u9700\u8981\u6253\u53701\u52301000\uff0c\u6211\u4eec\u53ea\u9700\u8981\u7b80\u5355\u5730\u5c06 count <= 10 \u6539\u4e3a count <= 1000 \u5373\u53ef\u3002 \u521d\u59cb\u6761\u4ef6\u4e3a false \u7684 while \u8bed\u53e5 \u5982\u679c\u521d\u59cb\u5316\u6761\u4ef6\u4e3a false \uff0c\u90a3\u4e48\u8bed\u53e5\u5c31\u5b8c\u5168\u4e0d\u4f1a\u88ab\u6267\u884c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int count { 15 }; while ( count <= 10 ) { std :: cout << count << ' ' ; ++ count ; } std :: cout << \"done! \\n \" ; return 0 ; } \u7531\u4e8e 15 <= 10 \u6c42\u503c\u4e3a false \uff0c\u6240\u4ee5 while \u4e2d\u7684\u6240\u6709\u8bed\u53e5\u90fd\u4f1a\u88ab\u8df3\u8fc7\uff0c\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c\u5e76\u6253\u5370 done \u3002 \u65e0\u9650\u5faa\u73af \u5982\u679c\u8868\u8fbe\u5f0f\u59cb\u7ec8\u6c42\u503c\u4e3a true \uff0c\u5219 while \u5faa\u73af\u5c31\u4f1a\u4e00\u76f4\u8fd0\u884c\u2014\u2014\u79f0\u4e3a\u65e0\u9650\u5faa\u73af\u6216\u6b7b\u5faa\u73af\u3002\u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int count { 1 }; while ( count <= 10 ) // \u8be5\u6761\u4ef6\u7528\u4e8e\u4e0d\u4f1a\u53d8\u4e3afalse { std :: cout << count << ' ' ; // \u53cd\u590d\u6267\u884c } std :: cout << '\\n' ; // \u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c return 0 ; // \u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c } \u56e0\u4e3a count \u59cb\u7ec8\u6ca1\u6709\u88ab\u9012\u589e\uff0c\u6240\u4ee5 count <= 10 \u603b\u662f\u771f\u3002\u56e0\u6b64\u5faa\u73af\u6c38\u8fdc\u4e0d\u4f1a\u505c\u6b62\uff0c\u7a0b\u5e8f\u4e0d\u505c\u5730\u6253\u5370 \u201c1 1 1 1 1\u2033\u3002 \u6709\u610f\u800c\u4e3a\u4e4b\u7684\u65e0\u9650\u5faa\u73af \u6211\u4eec\u53ef\u4ee5\u6545\u610f\u521b\u5efa\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff1a 1 2 3 4 while ( true ) { // this loop will execute forever } \u9000\u51fa\u65e0\u9650\u5faa\u73af\u7684\u552f\u4e00\u65b9\u6cd5\u662f\u4f7f\u7528 return y \u8bed\u53e5\u3001 break \u8bed\u53e5\u3001 exit \u8bed\u53e5\u3001 goto \u8bed\u53e5\u3001\u629b\u51fa\u5f02\u5e38\u6216\u88ab\u5176\u4ed6\u7a0b\u5e8f\u7ec8\u6b62\u3002 \u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { while ( true ) // infinite loop { std :: cout << \"Loop again (y/n)? \" ; char c {}; std :: cin >> c ; if ( c == 'n' ) return 0 ; } return 0 ; } \u8be5\u5faa\u73af\u4f1a\u4e0d\u65ad\u6267\u884c\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165 n \uff0c\u6b64\u65f6if\u8bed\u53e5\u7684\u6761\u4ef6\u6c42\u503c\u4e3a\u771f\uff0c return 0 \u88ab\u6267\u884c\uff0cmain\u51fd\u6570\u9000\u51fa\uff0c\u7a0b\u5e8f\u7ec8\u6b62\u3002 \u5728\u6301\u7eed\u8fd0\u884c\u5e76\u4e3aweb\u8bf7\u6c42\u63d0\u4f9b\u670d\u52a1\u7684web\u670d\u52a1\u5668\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u7ecf\u5e38\u53ef\u4ee5\u770b\u5230\u8fd9\u79cd\u5faa\u73af\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u4e8e\u6709\u610f\u800c\u4e3a\u4e4b\u7684\u6b7b\u5faa\u73af\uff0c\u8bf7\u4f7f\u7528 while(true) \u3002 \u5faa\u73af\u53d8\u91cf \u901a\u5e38\uff0c\u6211\u4eec\u4f1a\u5e0c\u671b\u4e00\u4e2a\u5faa\u73af\u6267\u884c\u4e00\u5b9a\u6b21\u6570\u3002\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u53d8\u91cf\uff0c\u79f0\u4e3a\u8ba1\u6570\u5668\u3002\u5faa\u73af\u53d8\u91cf\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u7528\u4e8e\u8ba1\u7b97\u5faa\u73af\u6267\u884c\u4e86\u591a\u5c11\u6b21\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf count \u662f\u4e00\u4e2a\u5faa\u73af\u53d8\u91cf\u3002 \u5faa\u73af\u53d8\u91cf\u901a\u5e38\u6709\u7b80\u5355\u7684\u540d\u79f0\uff0c\u5982i \u3001 j \u6216 k \u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u60f3\u77e5\u9053\u5faa\u73af\u53d8\u91cf\u5728\u7a0b\u5e8f\u4e2d\u7684\u54ea\u4e2a\u4f4d\u7f6e\u88ab\u4f7f\u7528\uff0c\u6b64\u65f6\u4f60\u4f1a\u641c\u7d22 i \u3001j \u6216 k \uff0c\u6b64\u65f6\u4f1a\u8fd4\u56de\u5927\u91cf\u7684\u641c\u7d22\u7ed3\u679c\uff01\u56e0\u6b64\uff0c\u4e00\u4e9b\u5f00\u53d1\u4eba\u5458\u559c\u6b22\u7528 iii \u3001 jjj \u6216 kkk \u8fd9\u6837\u7684\u53d8\u91cf\u540d\u3002\u56e0\u4e3a\u8fd9\u4e9b\u540d\u79f0\u66f4\u52a0\u72ec\u7279\uff0c\u8fd9\u4f7f\u5f97\u641c\u7d22\u5faa\u73af\u53d8\u91cf\u66f4\u52a0\u5bb9\u6613\uff0c\u5e76\u5e2e\u52a9\u5b83\u4eec\u4f5c\u4e3a\u5faa\u73af\u53d8\u91cf\u8131\u9896\u800c\u51fa\u3002\u4e00\u4e2a\u66f4\u597d\u7684\u4e3b\u610f\u662f\u4f7f\u7528\u201c\u771f\u5b9e\u7684\u201d\u53d8\u91cf\u540d\uff0c\u6bd4\u5982 count \uff0c\u6216\u8005\u4e00\u4e2a\u5173\u4e8e\u4f60\u6b63\u5728\u8ba1\u6570\u7684\u4e1c\u897f\u7684\u66f4\u8be6\u7ec6\u7684\u540d\u5b57(\u4f8b\u5982 userCount )\u3002 \u5faa\u73af\u53d8\u91cf\u5e94\u8be5\u4e3a\u6709\u7b26\u53f7\u6570 \u5faa\u73af\u53d8\u91cf\u51e0\u4e4e\u90fd\u5e94\u8be5\u662f\u6709\u7b26\u53f7\u7684\uff0c\u56e0\u4e3a\u65e0\u7b26\u53f7\u6574\u6570\u53ef\u80fd\u5bfc\u81f4\u610f\u60f3\u4e0d\u5230\u7684\u95ee\u9898\u3002\u8003\u8651\u4ee5\u4e0b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include int main () { unsigned int count { 10 }; // count from 10 down to 0 while ( count >= 0 ) { if ( count == 0 ) { std :: cout << \"blastoff!\" ; } else { std :: cout << count << ' ' ; } -- count ; } std :: cout << '\\n' ; return 0 ; } \u770b\u4e00\u4e0b\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u770b\u770b\u80fd\u5426\u53d1\u73b0\u9519\u8bef\u3002\u5e76\u4e0d\u662f\u5f88\u660e\u663e\u3002 \u4e8b\u5b9e\u8bc1\u660e\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u662f\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u3002\u5b83\u5f00\u59cb\u6253\u5370 10 9 8 7 6 5 4 3 2 1 blastoff \uff0c\u4f46\u968f\u540e\u5c31\u8131\u8f68\u4e86\uff0c\u4ece 4294967295 \u5f00\u59cb\u5012\u6570\u3002\u4e3a\u4ec0\u4e48\uff1f\u56e0\u4e3a\u5faa\u73af\u6761\u4ef6 count >= 0 \u6c38\u8fdc\u4e0d\u4f1a\u4e3a\u5047\uff01\u5f53 count \u4e3a 0 \u65f6\uff0c 0 >= 0 \u4e3a\u771f\u3002\u7136\u540e\u6267\u884c --count \uff0c count \u53cd\u8f6c\u4e3a 4294967295 (\u5047\u8bbe\u662f32\u4f4d\u6574\u6570)\u3002\u7531\u4e8e 4294967295 >= 0 \u4e3a\u771f\uff0c\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c\u3002\u56e0\u4e3a count \u662f\u65e0\u7b26\u53f7\u7684\uff0c\u5b83\u6c38\u8fdc\u4e0d\u53ef\u80fd\u662f\u8d1f\u6570\uff0c\u6240\u4ee5\u5faa\u73af\u6c38\u8fdc\u4e0d\u4f1a\u505c\u6b62\u3002 \u6700\u4f73\u5b9e\u8df5 \u5faa\u73af\u53d8\u91cf\u5e94\u8be5\u662f\u6709\u7b26\u53f7\u6574\u578b\u3002 \u8fed\u4ee3\u6267\u884cN\u6b21 \u6bcf\u6b21\u5faa\u73af\u6267\u884c\u88ab\u79f0\u4e3a\u4e00\u4e2a\u8fed\u4ee3\u3002 \u901a\u5e38\uff0c\u6211\u4eec\u5e0c\u671b\u6bcf\u9694\u7b2c2\u30013\u62164\u6b21\u8fed\u4ee3\u505a\u4e00\u4e9b\u4e8b\u60c5\uff0c\u4f8b\u5982\u6253\u5370\u6362\u884c\u7b26\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728\u8ba1\u6570\u5668\u4e0a\u4f7f\u7528\u6c42\u6a21\u8fd0\u7b97\u8f7b\u677e\u5b9e\u73b0: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include // Iterate through every number between 1 and 50 int main () { int count { 1 }; while ( count <= 50 ) { // print the number (pad numbers under 10 with a leading 0 for formatting purposes) if ( count < 10 ) { std :: cout << '0' ; } std :: cout << count << ' ' ; // \u5982\u679c\u5faa\u73af\u6b21\u6570\u53ef\u4ee5\u6574\u966410\uff0c\u5219\u6253\u5370\u6362\u884c if ( count % 10 == 0 ) { std :: cout << '\\n' ; } // increment the loop counter ++ count ; } return 0 ; } \u6253\u5370\uff1a 1 2 3 4 5 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 \u5d4c\u5957\u5faa\u73af \u5faa\u73af\u53ef\u4ee5\u5d4c\u5957\u5728\u5176\u4ed6\u5faa\u73af\u4e2d\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u88ab\u5d4c\u5957\u5faa\u73af(\u6211\u4eec\u79f0\u4e4b\u4e3a\u5185\u5c42\u5faa\u73af)\u548c\u5916\u5c42\u5faa\u73af\u90fd\u6709\u5404\u81ea\u7684\u8ba1\u6570\u5668\u3002\u6ce8\u610f\uff0c\u5185\u90e8\u5faa\u73af\u7684\u5faa\u73af\u8868\u8fbe\u5f0f\u4e5f\u4f7f\u7528\u4e86\u5916\u90e8\u5faa\u73af\u7684\u8ba1\u6570\u5668\uff01 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include int main () { // outer loops between 1 and 5 int outer { 1 }; while ( outer <= 5 ) { // For each iteration of the outer loop, the code in the body of the loop executes once // inner loops between 1 and outer int inner { 1 }; while ( inner <= outer ) { std :: cout << inner << ' ' ; ++ inner ; } // print a newline at the end of each row std :: cout << '\\n' ; ++ outer ; } return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 2 3 4 5 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 \u65b0\u624b\u7a0b\u5e8f\u5458\u5f80\u5f80\u96be\u4ee5\u7406\u89e3\u5d4c\u5957\u5faa\u73af\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u4e5f\u770b\u4e0d\u61c2\u7684\u8bdd\uff0c\u5343\u4e07\u4e0d\u8981\u7070\u5fc3\u4e27\u6c14\u3002\u5bf9\u4e8e\u5916\u5c42\u5faa\u73af\u7684\u6bcf\u4e00\u6b21\u8fed\u4ee3\uff0c\u5916\u5c42\u5faa\u73af\u7684\u5faa\u73af\u4f53\u5185\u8bed\u53e5\u4f1a\u6267\u884c\u4e00\u6b21\u3002\u7531\u4e8e\u5916\u5c42\u5faa\u73af\u7684\u5faa\u73af\u4f53\u5185\u5305\u62ec\u5185\u5c42\u5faa\u73af\uff0c\u6240\u4ee5\u5185\u5c42\u5faa\u73af\u5728\u6bcf\u4e00\u4e2a\u5916\u5c42\u5faa\u73af\u7684\u8fed\u4ee3\u4e2d\u90fd\u8981\u5b8c\u6574\u6267\u884c\u4e00\u6b21\u3002 \u8ba9\u6211\u4eec\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\u4e0a\u9762\u7684\u4ee3\u7801\uff1a \u9996\u5148\uff0c\u5bf9\u4e8e\u5916\u5c42\u5faa\u73af(\u5faa\u73af\u53d8\u91cf\u4e3a outer ) \u4f1a\u8fed\u4ee3\u6267\u884c5\u6b21\uff08 outer \u4f9d\u6b21\u53d8\u4e3a 1 , 2 , 3 , 4 , \u548c 5 \uff09\u3002 \u5916\u5c42\u5faa\u73af\u7b2c\u4e00\u6b21\u8fed\u4ee3\u65f6 outer \u503c\u4e3a 1 \uff0c\u7136\u540e\u6267\u884c\u5faa\u73af\u4f53\u5185\u7684\u8bed\u53e5\u3002\u5728\u5faa\u73af\u4f53\u5185\uff0c\u6709\u53e6\u5916\u4e00\u4e2a\u5faa\u73af\uff0c\u5176\u5faa\u73af\u53d8\u91cf\u4e3a inner \u3002\u5185\u5c42\u5faa\u73af\u7684\u5faa\u73af\u53d8\u91cf\u4ece 1 \u5230 outer (\u6b64\u65f6\u662f 1 )\uff0c\u6240\u4ee5\u5185\u5c42\u5faa\u73af\u6267\u884c\u4e00\u6b21\uff0c\u6253\u5370 1 \u3002\u7136\u540e\u6253\u5370\u6362\u884c\uff0c\u5e76\u5c06 outer \u9012\u589e\u4e3a 2 \u3002 \u5728\u5916\u5c42\u5faa\u73af\u6267\u884c\u7b2c\u4e8c\u6b21\u8fed\u4ee3\u65f6\uff0c outer \u7684\u503c\u4e3a 2 \uff0c\u7136\u540e\u6267\u884c\u5faa\u73af\u4f53\u3002\u5728\u5faa\u73af\u4f53\u5185\uff0c inner \u4ecd\u7136\u4ece 1 \u5230 outer (\u6b64\u65f6\u4e3a 2 )\u8fdb\u884c\u8fed\u4ee3\u3002 \u6240\u4ee5\u8fd9\u4e00\u6b21\u5185\u5c42\u5faa\u73af\u4f1a\u6267\u884c\u4e24\u6b21\uff0c\u6253\u5370 1 \u548c 2 \u3002 \u7136\u540e\u6253\u5370\u6362\u884c\uff0c\u5e76\u5c06 outer \u9012\u589e\u4e3a 3 \u3002 \u8fed\u4ee3\u7ee7\u7eed\u8fdb\u884c\u7684\u8bdd\uff0c\u5185\u5b58\u5faa\u73af\u4f1a\u4f9d\u6b21\u6253\u5370 1 2 3 \u3001 1 2 3 4 \u4ee5\u53ca 1 2 3 4 5 \u3002\u6700\u7ec8\uff0c outer \u4f1a\u9012\u589e\u5230 6 \uff0c\u6b64\u65f6\u56e0\u4e3a\u5916\u5c42\u7684\u5faa\u73af\u6761\u4ef6( outer <= 5 ) \u6c42\u503c\u4e3afalse\uff0c\u6240\u4ee5\u5916\u5c42\u5faa\u73af\u4f1a\u7ec8\u6b62\u3002\u7a0b\u5e8f\u9000\u51fa\u3002 \u5982\u679c\u60a8\u4ecd\u7136\u611f\u5230\u56f0\u60d1\uff0c\u53ef\u4ee5\u5728\u8c03\u8bd5\u5668\u4e2d\u9010\u884c\u68c0\u67e5\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u5e76\u67e5\u770b\u201cinner\u201d\u548c\u201couter\u201d\u7684\u503c\uff0c\u901a\u8fc7\u8fd9\u4e2a\u65b9\u6cd5\u4f60\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u4ee3\u7801\u7684\u8fd0\u884c\u903b\u8f91\u3002","title":"7.7 - \u5faa\u73af\u548c while \u8bed\u53e5"},{"location":"7-7-Intro-to-loops-and-while-statements/#_1","text":"\u73b0\u5728\u771f\u6b63\u7684\u4e50\u8da3\u5f00\u59cb\u4e86\u2014\u2014\u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5faa\u73af\u3002\u5faa\u73af\u662f\u63a7\u5236\u6d41\u7ed3\u6784\uff0c\u5b83\u5141\u8bb8\u4e00\u6bb5\u4ee3\u7801\u88ab\u91cd\u590d\u6267\u884c\uff0c\u76f4\u5230\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u3002\u5faa\u73af\u4e3a\u7f16\u7a0b\u5de5\u5177\u5305\u589e\u52a0\u4e86\u5f88\u5927\u7684\u7075\u6d3b\u6027\uff0c\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u5f88\u591a\u4e0d\u4f7f\u7528\u5faa\u73af\u96be\u4ee5\u5b9e\u73b0\u7684\u529f\u80fd\u3002 \u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u60f3\u8981\u6253\u53701\u523010\uff0c\u5982\u679c\u6ca1\u6709\u5faa\u73af\uff0c\u4f60\u53ef\u80fd\u4f1a\u8fd9\u4e48\u505a\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << \"1 2 3 4 5 6 7 8 9 10\" ; std :: cout << \" done! \\n \" ; return 0 ; } \u867d\u7136\u8fd9\u4e48\u505a\u662f\u53ef\u884c\u7684\uff0c\u4f46\u5f53\u6211\u4eec\u9700\u8981\u6253\u5370\u66f4\u591a\u7684\u6570\u5b57\u65f6\uff0c\u5b83\u4f1a\u53d8\u5f97\u8d8a\u6765\u8d8a\u4e0d\u53ef\u884c\uff1a\u5982\u679c\u9700\u8981\u6253\u53701\u52301000\u4e4b\u95f4\u7684\u6240\u6709\u6570\u5b57\u5462\uff1f\u90a3\u6253\u5b57\u7684\u5de5\u4f5c\u91cf\u5c31\u592a\u5927\u4e86\uff01\u800c\u4e14\u8be5\u7a0b\u5e8f\u8981\u6253\u5370\u7684\u5185\u5bb9\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u5c31\u77e5\u9053\u8981\u6253\u5370\u591a\u5c11\u6570\u5b57\u3002 \u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u7a0d\u5fae\u6539\u53d8\u4e00\u4e0b\u53c2\u6570\u3002\u5982\u679c\u9700\u8981\u8ba9\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff0c\u7136\u540e\u6253\u53701\u548c\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\u4e4b\u95f4\u7684\u6240\u6709\u6570\u5b57\uff0c\u90a3\u4f1a\u600e\u4e48\u6837\u5462?\u7528\u6237\u5c06\u8f93\u5165\u7684\u6570\u5b57\u5728\u7f16\u8bd1\u65f6\u662f\u4e0d\u53ef\u77e5\u7684\uff0c\u8981\u600e\u4e48\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5462?","title":"\u5faa\u73af"},{"location":"7-7-Intro-to-loops-and-while-statements/#while","text":"while \u8bed\u53e5\uff08\u4e5f\u88ab\u53eb\u505a while \u5faa\u73af\uff09\u662fC++\u4e2d\u6700\u7b80\u5355\u5faa\u73af\uff0c\u5b83\u7684\u5b9a\u4e49\u548c if \u8bed\u53e5\u7279\u522b\u5730\u50cf\uff1a 1 2 while ( condition ) statement ; while \u8bed\u53e5\u7684\u58f0\u660e\u9700\u8981\u4f7f\u7528 while \u5173\u952e\u5b57\u3002\u5f53 while \u8bed\u53e5\u6267\u884c\u65f6\uff0c condition \u4f1a\u88ab\u6c42\u503c\u3002\u5982\u679c\u6c42\u503c\u7ed3\u679c\u662f\u771f\uff0c\u5219\u76f8\u5173\u7684\u8bed\u53e5\u4f1a\u88ab\u6267\u884c\u3002 \u4f46\u662f\uff0c\u548cif\u8bed\u53e5\u4e0d\u540c\u7684\u662f\uff0c while \u8bed\u53e5\u4e2d\u7684 statement \u6267\u884c\u5b8c\u6210\u540e\uff0c\u4f1a\u8fd4\u56de\u5230 while \u8bed\u53e5\u7684\u5f00\u59cb\uff0c\u7136\u540e\u7ee7\u7eed\u6267\u884c\u3001\u8fd9\u610f\u5473\u7740\u53ea\u8981\u6761\u4ef6\u6c42\u503c\u4e3a\u771f\uff0c while \u5faa\u73af\u5c31\u4f1a\u4e0d\u505c\u5730\u6267\u884c\u3002 \u8fd8\u662f\u6253\u53701\u523010\u7684\u4f8b\u5b50\uff0c\u8fd9\u6b21\u4f7f\u7528 while \u5faa\u73af\u5b8c\u6210\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int count { 1 }; while ( count <= 10 ) { std :: cout << count << ' ' ; ++ count ; } std :: cout << \"done! \\n \" ; return 0 ; } \u8f93\u51fa\uff1a 1 1 2 3 4 5 6 7 8 9 10 done! \u8ba9\u6211\u4eec\u4ed4\u7ec6\u770b\u770b\u8fd9\u4e2a\u7a0b\u5e8f\u6b63\u5728\u505a\u4ec0\u4e48\u3002\u9996\u5148\uff0c count \u88ab\u521d\u59cb\u5316\u4e3a1\uff0c\u8fd9\u662f\u6211\u4eec\u8981\u6253\u5370\u7684\u7b2c\u4e00\u4e2a\u6570\u5b57\u3002\u6761\u4ef6 count <= 10 \u4e3a true \uff0c\u56e0\u6b64\u6267\u884c\u8bed\u53e5\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u7684\u8bed\u53e5\u662f\u4e00\u4e2a\u8bed\u53e5\u5757\uff0c\u56e0\u6b64\u5757\u4e2d\u7684\u6240\u6709\u8bed\u53e5\u90fd\u5c06\u6267\u884c\u3002\u5757\u4e2d\u7684\u7b2c\u4e00\u4e2a\u8bed\u53e5\u8f93\u51fa 1 \u548c\u4e00\u4e2a\u7a7a\u683c\uff0c\u7b2c\u4e8c\u4e2a\u8bed\u53e5\u5c06 count \u9012\u589e\u52302\u3002\u63a7\u5236\u73b0\u5728\u8fd4\u56de\u5230 while \u7684\u9876\u90e8\uff0c\u5e76\u518d\u6b21\u8ba1\u7b97\u6761\u4ef6\u3002 2 <= 10 \u7684\u8ba1\u7b97\u7ed3\u679c\u4e3a\u4ecd\u7136 true \uff0c\u56e0\u6b64\u4ee3\u7801\u5757\u5c06\u518d\u6b21\u6267\u884c\u3002\u5faa\u73af\u5c06\u91cd\u590d\u6267\u884c\uff0c\u76f4\u5230 count \u4e3a11 \uff0c\u6b64\u65f6 11 <= 10 \u5c06\u88ab\u8ba1\u7b97\u4e3a false \uff0c\u4e0e\u5faa\u73af\u76f8\u5173\u7684\u8bed\u53e5\u5c06\u88ab\u8df3\u8fc7\u3002\u81f3\u6b64\uff0c\u5faa\u73af\u5b8c\u6210\u4e86\u3002 \u867d\u7136\u4e0a\u9762\u7684\u4ee3\u7801\u770b\u4e0a\u53bb\u4f3c\u4e4e\u6bd4\u624b\u52a8\u8f93\u51651\u523010\u8fd8\u591a\u4e9b\uff0c\u4f46\u662f\u4f60\u53ef\u4ee5\u60f3\u8c61\uff0c\u5982\u679c\u9700\u8981\u6253\u53701\u52301000\uff0c\u6211\u4eec\u53ea\u9700\u8981\u7b80\u5355\u5730\u5c06 count <= 10 \u6539\u4e3a count <= 1000 \u5373\u53ef\u3002","title":"while \u8bed\u53e5"},{"location":"7-7-Intro-to-loops-and-while-statements/#falsewhile","text":"\u5982\u679c\u521d\u59cb\u5316\u6761\u4ef6\u4e3a false \uff0c\u90a3\u4e48\u8bed\u53e5\u5c31\u5b8c\u5168\u4e0d\u4f1a\u88ab\u6267\u884c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int count { 15 }; while ( count <= 10 ) { std :: cout << count << ' ' ; ++ count ; } std :: cout << \"done! \\n \" ; return 0 ; } \u7531\u4e8e 15 <= 10 \u6c42\u503c\u4e3a false \uff0c\u6240\u4ee5 while \u4e2d\u7684\u6240\u6709\u8bed\u53e5\u90fd\u4f1a\u88ab\u8df3\u8fc7\uff0c\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c\u5e76\u6253\u5370 done \u3002","title":"\u521d\u59cb\u6761\u4ef6\u4e3afalse\u7684while\u8bed\u53e5"},{"location":"7-7-Intro-to-loops-and-while-statements/#_2","text":"\u5982\u679c\u8868\u8fbe\u5f0f\u59cb\u7ec8\u6c42\u503c\u4e3a true \uff0c\u5219 while \u5faa\u73af\u5c31\u4f1a\u4e00\u76f4\u8fd0\u884c\u2014\u2014\u79f0\u4e3a\u65e0\u9650\u5faa\u73af\u6216\u6b7b\u5faa\u73af\u3002\u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int count { 1 }; while ( count <= 10 ) // \u8be5\u6761\u4ef6\u7528\u4e8e\u4e0d\u4f1a\u53d8\u4e3afalse { std :: cout << count << ' ' ; // \u53cd\u590d\u6267\u884c } std :: cout << '\\n' ; // \u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c return 0 ; // \u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c } \u56e0\u4e3a count \u59cb\u7ec8\u6ca1\u6709\u88ab\u9012\u589e\uff0c\u6240\u4ee5 count <= 10 \u603b\u662f\u771f\u3002\u56e0\u6b64\u5faa\u73af\u6c38\u8fdc\u4e0d\u4f1a\u505c\u6b62\uff0c\u7a0b\u5e8f\u4e0d\u505c\u5730\u6253\u5370 \u201c1 1 1 1 1\u2033\u3002","title":"\u65e0\u9650\u5faa\u73af"},{"location":"7-7-Intro-to-loops-and-while-statements/#_3","text":"\u6211\u4eec\u53ef\u4ee5\u6545\u610f\u521b\u5efa\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff1a 1 2 3 4 while ( true ) { // this loop will execute forever } \u9000\u51fa\u65e0\u9650\u5faa\u73af\u7684\u552f\u4e00\u65b9\u6cd5\u662f\u4f7f\u7528 return y \u8bed\u53e5\u3001 break \u8bed\u53e5\u3001 exit \u8bed\u53e5\u3001 goto \u8bed\u53e5\u3001\u629b\u51fa\u5f02\u5e38\u6216\u88ab\u5176\u4ed6\u7a0b\u5e8f\u7ec8\u6b62\u3002 \u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { while ( true ) // infinite loop { std :: cout << \"Loop again (y/n)? \" ; char c {}; std :: cin >> c ; if ( c == 'n' ) return 0 ; } return 0 ; } \u8be5\u5faa\u73af\u4f1a\u4e0d\u65ad\u6267\u884c\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165 n \uff0c\u6b64\u65f6if\u8bed\u53e5\u7684\u6761\u4ef6\u6c42\u503c\u4e3a\u771f\uff0c return 0 \u88ab\u6267\u884c\uff0cmain\u51fd\u6570\u9000\u51fa\uff0c\u7a0b\u5e8f\u7ec8\u6b62\u3002 \u5728\u6301\u7eed\u8fd0\u884c\u5e76\u4e3aweb\u8bf7\u6c42\u63d0\u4f9b\u670d\u52a1\u7684web\u670d\u52a1\u5668\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u7ecf\u5e38\u53ef\u4ee5\u770b\u5230\u8fd9\u79cd\u5faa\u73af\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u4e8e\u6709\u610f\u800c\u4e3a\u4e4b\u7684\u6b7b\u5faa\u73af\uff0c\u8bf7\u4f7f\u7528 while(true) \u3002","title":"\u6709\u610f\u800c\u4e3a\u4e4b\u7684\u65e0\u9650\u5faa\u73af"},{"location":"7-7-Intro-to-loops-and-while-statements/#_4","text":"\u901a\u5e38\uff0c\u6211\u4eec\u4f1a\u5e0c\u671b\u4e00\u4e2a\u5faa\u73af\u6267\u884c\u4e00\u5b9a\u6b21\u6570\u3002\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u53d8\u91cf\uff0c\u79f0\u4e3a\u8ba1\u6570\u5668\u3002\u5faa\u73af\u53d8\u91cf\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u7528\u4e8e\u8ba1\u7b97\u5faa\u73af\u6267\u884c\u4e86\u591a\u5c11\u6b21\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf count \u662f\u4e00\u4e2a\u5faa\u73af\u53d8\u91cf\u3002 \u5faa\u73af\u53d8\u91cf\u901a\u5e38\u6709\u7b80\u5355\u7684\u540d\u79f0\uff0c\u5982i \u3001 j \u6216 k \u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u60f3\u77e5\u9053\u5faa\u73af\u53d8\u91cf\u5728\u7a0b\u5e8f\u4e2d\u7684\u54ea\u4e2a\u4f4d\u7f6e\u88ab\u4f7f\u7528\uff0c\u6b64\u65f6\u4f60\u4f1a\u641c\u7d22 i \u3001j \u6216 k \uff0c\u6b64\u65f6\u4f1a\u8fd4\u56de\u5927\u91cf\u7684\u641c\u7d22\u7ed3\u679c\uff01\u56e0\u6b64\uff0c\u4e00\u4e9b\u5f00\u53d1\u4eba\u5458\u559c\u6b22\u7528 iii \u3001 jjj \u6216 kkk \u8fd9\u6837\u7684\u53d8\u91cf\u540d\u3002\u56e0\u4e3a\u8fd9\u4e9b\u540d\u79f0\u66f4\u52a0\u72ec\u7279\uff0c\u8fd9\u4f7f\u5f97\u641c\u7d22\u5faa\u73af\u53d8\u91cf\u66f4\u52a0\u5bb9\u6613\uff0c\u5e76\u5e2e\u52a9\u5b83\u4eec\u4f5c\u4e3a\u5faa\u73af\u53d8\u91cf\u8131\u9896\u800c\u51fa\u3002\u4e00\u4e2a\u66f4\u597d\u7684\u4e3b\u610f\u662f\u4f7f\u7528\u201c\u771f\u5b9e\u7684\u201d\u53d8\u91cf\u540d\uff0c\u6bd4\u5982 count \uff0c\u6216\u8005\u4e00\u4e2a\u5173\u4e8e\u4f60\u6b63\u5728\u8ba1\u6570\u7684\u4e1c\u897f\u7684\u66f4\u8be6\u7ec6\u7684\u540d\u5b57(\u4f8b\u5982 userCount )\u3002","title":"\u5faa\u73af\u53d8\u91cf"},{"location":"7-7-Intro-to-loops-and-while-statements/#_5","text":"\u5faa\u73af\u53d8\u91cf\u51e0\u4e4e\u90fd\u5e94\u8be5\u662f\u6709\u7b26\u53f7\u7684\uff0c\u56e0\u4e3a\u65e0\u7b26\u53f7\u6574\u6570\u53ef\u80fd\u5bfc\u81f4\u610f\u60f3\u4e0d\u5230\u7684\u95ee\u9898\u3002\u8003\u8651\u4ee5\u4e0b\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include int main () { unsigned int count { 10 }; // count from 10 down to 0 while ( count >= 0 ) { if ( count == 0 ) { std :: cout << \"blastoff!\" ; } else { std :: cout << count << ' ' ; } -- count ; } std :: cout << '\\n' ; return 0 ; } \u770b\u4e00\u4e0b\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u770b\u770b\u80fd\u5426\u53d1\u73b0\u9519\u8bef\u3002\u5e76\u4e0d\u662f\u5f88\u660e\u663e\u3002 \u4e8b\u5b9e\u8bc1\u660e\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u662f\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u3002\u5b83\u5f00\u59cb\u6253\u5370 10 9 8 7 6 5 4 3 2 1 blastoff \uff0c\u4f46\u968f\u540e\u5c31\u8131\u8f68\u4e86\uff0c\u4ece 4294967295 \u5f00\u59cb\u5012\u6570\u3002\u4e3a\u4ec0\u4e48\uff1f\u56e0\u4e3a\u5faa\u73af\u6761\u4ef6 count >= 0 \u6c38\u8fdc\u4e0d\u4f1a\u4e3a\u5047\uff01\u5f53 count \u4e3a 0 \u65f6\uff0c 0 >= 0 \u4e3a\u771f\u3002\u7136\u540e\u6267\u884c --count \uff0c count \u53cd\u8f6c\u4e3a 4294967295 (\u5047\u8bbe\u662f32\u4f4d\u6574\u6570)\u3002\u7531\u4e8e 4294967295 >= 0 \u4e3a\u771f\uff0c\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c\u3002\u56e0\u4e3a count \u662f\u65e0\u7b26\u53f7\u7684\uff0c\u5b83\u6c38\u8fdc\u4e0d\u53ef\u80fd\u662f\u8d1f\u6570\uff0c\u6240\u4ee5\u5faa\u73af\u6c38\u8fdc\u4e0d\u4f1a\u505c\u6b62\u3002 \u6700\u4f73\u5b9e\u8df5 \u5faa\u73af\u53d8\u91cf\u5e94\u8be5\u662f\u6709\u7b26\u53f7\u6574\u578b\u3002","title":"\u5faa\u73af\u53d8\u91cf\u5e94\u8be5\u4e3a\u6709\u7b26\u53f7\u6570"},{"location":"7-7-Intro-to-loops-and-while-statements/#n","text":"\u6bcf\u6b21\u5faa\u73af\u6267\u884c\u88ab\u79f0\u4e3a\u4e00\u4e2a\u8fed\u4ee3\u3002 \u901a\u5e38\uff0c\u6211\u4eec\u5e0c\u671b\u6bcf\u9694\u7b2c2\u30013\u62164\u6b21\u8fed\u4ee3\u505a\u4e00\u4e9b\u4e8b\u60c5\uff0c\u4f8b\u5982\u6253\u5370\u6362\u884c\u7b26\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728\u8ba1\u6570\u5668\u4e0a\u4f7f\u7528\u6c42\u6a21\u8fd0\u7b97\u8f7b\u677e\u5b9e\u73b0: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include // Iterate through every number between 1 and 50 int main () { int count { 1 }; while ( count <= 50 ) { // print the number (pad numbers under 10 with a leading 0 for formatting purposes) if ( count < 10 ) { std :: cout << '0' ; } std :: cout << count << ' ' ; // \u5982\u679c\u5faa\u73af\u6b21\u6570\u53ef\u4ee5\u6574\u966410\uff0c\u5219\u6253\u5370\u6362\u884c if ( count % 10 == 0 ) { std :: cout << '\\n' ; } // increment the loop counter ++ count ; } return 0 ; } \u6253\u5370\uff1a 1 2 3 4 5 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50","title":"\u8fed\u4ee3\u6267\u884cN\u6b21"},{"location":"7-7-Intro-to-loops-and-while-statements/#_6","text":"\u5faa\u73af\u53ef\u4ee5\u5d4c\u5957\u5728\u5176\u4ed6\u5faa\u73af\u4e2d\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u88ab\u5d4c\u5957\u5faa\u73af(\u6211\u4eec\u79f0\u4e4b\u4e3a\u5185\u5c42\u5faa\u73af)\u548c\u5916\u5c42\u5faa\u73af\u90fd\u6709\u5404\u81ea\u7684\u8ba1\u6570\u5668\u3002\u6ce8\u610f\uff0c\u5185\u90e8\u5faa\u73af\u7684\u5faa\u73af\u8868\u8fbe\u5f0f\u4e5f\u4f7f\u7528\u4e86\u5916\u90e8\u5faa\u73af\u7684\u8ba1\u6570\u5668\uff01 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include int main () { // outer loops between 1 and 5 int outer { 1 }; while ( outer <= 5 ) { // For each iteration of the outer loop, the code in the body of the loop executes once // inner loops between 1 and outer int inner { 1 }; while ( inner <= outer ) { std :: cout << inner << ' ' ; ++ inner ; } // print a newline at the end of each row std :: cout << '\\n' ; ++ outer ; } return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 2 3 4 5 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 \u65b0\u624b\u7a0b\u5e8f\u5458\u5f80\u5f80\u96be\u4ee5\u7406\u89e3\u5d4c\u5957\u5faa\u73af\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u4e5f\u770b\u4e0d\u61c2\u7684\u8bdd\uff0c\u5343\u4e07\u4e0d\u8981\u7070\u5fc3\u4e27\u6c14\u3002\u5bf9\u4e8e\u5916\u5c42\u5faa\u73af\u7684\u6bcf\u4e00\u6b21\u8fed\u4ee3\uff0c\u5916\u5c42\u5faa\u73af\u7684\u5faa\u73af\u4f53\u5185\u8bed\u53e5\u4f1a\u6267\u884c\u4e00\u6b21\u3002\u7531\u4e8e\u5916\u5c42\u5faa\u73af\u7684\u5faa\u73af\u4f53\u5185\u5305\u62ec\u5185\u5c42\u5faa\u73af\uff0c\u6240\u4ee5\u5185\u5c42\u5faa\u73af\u5728\u6bcf\u4e00\u4e2a\u5916\u5c42\u5faa\u73af\u7684\u8fed\u4ee3\u4e2d\u90fd\u8981\u5b8c\u6574\u6267\u884c\u4e00\u6b21\u3002 \u8ba9\u6211\u4eec\u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\u4e0a\u9762\u7684\u4ee3\u7801\uff1a \u9996\u5148\uff0c\u5bf9\u4e8e\u5916\u5c42\u5faa\u73af(\u5faa\u73af\u53d8\u91cf\u4e3a outer ) \u4f1a\u8fed\u4ee3\u6267\u884c5\u6b21\uff08 outer \u4f9d\u6b21\u53d8\u4e3a 1 , 2 , 3 , 4 , \u548c 5 \uff09\u3002 \u5916\u5c42\u5faa\u73af\u7b2c\u4e00\u6b21\u8fed\u4ee3\u65f6 outer \u503c\u4e3a 1 \uff0c\u7136\u540e\u6267\u884c\u5faa\u73af\u4f53\u5185\u7684\u8bed\u53e5\u3002\u5728\u5faa\u73af\u4f53\u5185\uff0c\u6709\u53e6\u5916\u4e00\u4e2a\u5faa\u73af\uff0c\u5176\u5faa\u73af\u53d8\u91cf\u4e3a inner \u3002\u5185\u5c42\u5faa\u73af\u7684\u5faa\u73af\u53d8\u91cf\u4ece 1 \u5230 outer (\u6b64\u65f6\u662f 1 )\uff0c\u6240\u4ee5\u5185\u5c42\u5faa\u73af\u6267\u884c\u4e00\u6b21\uff0c\u6253\u5370 1 \u3002\u7136\u540e\u6253\u5370\u6362\u884c\uff0c\u5e76\u5c06 outer \u9012\u589e\u4e3a 2 \u3002 \u5728\u5916\u5c42\u5faa\u73af\u6267\u884c\u7b2c\u4e8c\u6b21\u8fed\u4ee3\u65f6\uff0c outer \u7684\u503c\u4e3a 2 \uff0c\u7136\u540e\u6267\u884c\u5faa\u73af\u4f53\u3002\u5728\u5faa\u73af\u4f53\u5185\uff0c inner \u4ecd\u7136\u4ece 1 \u5230 outer (\u6b64\u65f6\u4e3a 2 )\u8fdb\u884c\u8fed\u4ee3\u3002 \u6240\u4ee5\u8fd9\u4e00\u6b21\u5185\u5c42\u5faa\u73af\u4f1a\u6267\u884c\u4e24\u6b21\uff0c\u6253\u5370 1 \u548c 2 \u3002 \u7136\u540e\u6253\u5370\u6362\u884c\uff0c\u5e76\u5c06 outer \u9012\u589e\u4e3a 3 \u3002 \u8fed\u4ee3\u7ee7\u7eed\u8fdb\u884c\u7684\u8bdd\uff0c\u5185\u5b58\u5faa\u73af\u4f1a\u4f9d\u6b21\u6253\u5370 1 2 3 \u3001 1 2 3 4 \u4ee5\u53ca 1 2 3 4 5 \u3002\u6700\u7ec8\uff0c outer \u4f1a\u9012\u589e\u5230 6 \uff0c\u6b64\u65f6\u56e0\u4e3a\u5916\u5c42\u7684\u5faa\u73af\u6761\u4ef6( outer <= 5 ) \u6c42\u503c\u4e3afalse\uff0c\u6240\u4ee5\u5916\u5c42\u5faa\u73af\u4f1a\u7ec8\u6b62\u3002\u7a0b\u5e8f\u9000\u51fa\u3002 \u5982\u679c\u60a8\u4ecd\u7136\u611f\u5230\u56f0\u60d1\uff0c\u53ef\u4ee5\u5728\u8c03\u8bd5\u5668\u4e2d\u9010\u884c\u68c0\u67e5\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u5e76\u67e5\u770b\u201cinner\u201d\u548c\u201couter\u201d\u7684\u503c\uff0c\u901a\u8fc7\u8fd9\u4e2a\u65b9\u6cd5\u4f60\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u4ee3\u7801\u7684\u8fd0\u884c\u903b\u8f91\u3002","title":"\u5d4c\u5957\u5faa\u73af"},{"location":"7-8-Do-while-statements/","tags":["do-while"],"text":"Key Takeaway \u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u5e0c\u671b\u5411\u7528\u6237\u663e\u793a\u4e00\u4e2a\u83dc\u5355\uff0c\u5e76\u8981\u6c42\u4ed6\u4eec\u8fdb\u884c\u9009\u62e9\u2014\u2014\u5982\u679c\u7528\u6237\u9009\u62e9\u4e86\u65e0\u6548\u7684\u9009\u62e9\uff0c\u5219\u9700\u8981\u518d\u6b21\u8be2\u95ee\u3002\u663e\u7136\uff0c\u83dc\u5355\u548c\u9009\u9879\u5e94\u8be5\u653e\u5728\u67d0\u79cd\u5faa\u73af\u4e2d(\u8fd9\u6837\u624d\u53ef\u4ee5\u53cd\u590d\u8be2\u95ee\u7528\u6237\uff0c\u76f4\u5230\u4ed6\u4eec\u8f93\u5165\u6709\u6548\u7684\u8f93\u5165)\uff0c\u4f46\u662f\u5e94\u8be5\u9009\u62e9\u54ea\u79cd\u7c7b\u578b\u7684\u5faa\u73af\u5462? \u7531\u4e8e while \u5faa\u73af\u4f1a\u5148\u5bf9\u6761\u4ef6\u6c42\u503c\uff0c\u6240\u4ee5\u4f7f\u7528 while \u5faa\u73af\u5f88\u522b\u626d\u3002\u5047\u8bbe\u6211\u4eec\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include int main () { // \u9009\u62e9\u5fc5\u987b\u88ab\u5b9a\u4e49\u5728\u5faa\u73af\u5916 int selection { 0 }; while ( selection != 1 && selection != 2 && selection != 3 && selection != 4 ) { std :: cout << \"Please make a selection: \\n \" ; std :: cout << \"1) Addition \\n \" ; std :: cout << \"2) Subtraction \\n \" ; std :: cout << \"3) Multiplication \\n \" ; std :: cout << \"4) Division \\n \" ; std :: cin >> selection ; } // \u7ee7\u7eed\u9009\u62e9\u8fdb\u884c\u64cd\u4f5c // \u53ef\u4ee5\u4f7f\u7528 switch \u8bed\u53e5\u8fdb\u884c std :: cout << \"You selected option #\" << selection << '\\n' ; return 0 ; } \u4f46\u662f\uff0c\u4e0a\u8ff0\u4ee3\u7801\u80fd\u591f\u6b63\u786e\u5de5\u4f5c\u7684\u539f\u56e0\u662f\u56e0\u4e3a\u6b64\u5904\u7684\u521d\u59cb\u503c\u4e3a0\u2014\u2014\u5b83\u4e0d\u662f\u4efb\u4f55\u5408\u6cd5\u7684 selection \u503c ( 1, 2, 3 or 4 )\u3002\u5982\u679c 0 \u4e5f\u662f\u5408\u6cd5\u7684\u503c\u4f1a\u600e\u6837\uff1f\u6211\u4eec\u5fc5\u987b\u9009\u62e9\u4e00\u4e2a\u4e0d\u540c\u7684\u521d\u59cb\u5316\u503c\u6765\u8868\u793a\u201c\u65e0\u6548\u201d\u2014\u2014\u8fd9\u5bfc\u81f4\u6211\u4eec\u7684\u4ee3\u7801\u4e2d\u5f15\u5165\u9b54\u9b3c\u6570\u5b57( 4.15 -\u5b57\u9762\u91cf )\u3002 \u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\u6765\u8ddf\u8e2a\u6709\u6548\u6027\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include int main () { int selection { 0 }; bool invalid { true }; // new variable just to gate the loop while ( invalid ) { std :: cout << \"Please make a selection: \\n \" ; std :: cout << \"1) Addition \\n \" ; std :: cout << \"2) Subtraction \\n \" ; std :: cout << \"3) Multiplication \\n \" ; std :: cout << \"4) Division \\n \" ; std :: cin >> selection ; invalid = ( selection != 1 && selection != 2 && selection != 3 && selection != 4 ); } // do something with selection here // such as a switch statement std :: cout << \"You selected option #\" << selection << '\\n' ; return 0 ; } \u867d\u7136\u8fd9\u907f\u514d\u4e86\u9b54\u9b3c\u6570\u5b57\uff0c\u4f46\u5f15\u5165\u4e86\u4e00\u4e2a\u65b0\u53d8\u91cf\uff0c\u4ee5\u786e\u4fdd\u5faa\u73af\u80fd\u591f\u81f3\u5c11\u8fd0\u884c\u4e00\u6b21\uff0c\u8fd9\u589e\u52a0\u4e86\u590d\u6742\u6027\u548c\u72af\u9519\u7684\u53ef\u80fd\u6027\u3002 do-while \u8bed\u53e5 \u4e3a\u4e86\u5e2e\u52a9\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\uff0cC++\u63d0\u4f9b\u4e86 do-while \u8bed\u53e5: 1 2 3 do statement ; // \u53ef\u4ee5\u662f\u5355\u4e00\u8bed\u53e5\uff0c\u4e5f\u53ef\u4ee5\u662f\u590d\u5408\u53e5 while ( condition ); do-while \u8bed\u53e5\u662f\u4e00\u4e2a\u5faa\u73af\u6784\u9020\u65b9\u5f0f\uff0c\u5176\u5de5\u4f5c\u539f\u7406\u4e0e while \u5faa\u73af\u7c7b\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u8be5\u8bed\u53e5\u603b\u662f\u81f3\u5c11\u6267\u884c\u4e00\u6b21\u3002\u8bed\u53e5\u6267\u884c\u540e\uff0cdo-while\u5faa\u73af\u68c0\u67e5\u6761\u4ef6\u3002\u5982\u679c\u6761\u4ef6\u7684\u8ba1\u7b97\u7ed3\u679c\u4e3a true \uff0c\u6267\u884c\u8def\u5f84\u5c06\u8df3\u8f6c\u56dedo-while\u5faa\u73af\u7684\u9876\u90e8\u5e76\u518d\u6b21\u6267\u884c\u3002 \u4e0b\u9762\u662f\u6211\u4eec\u4e0a\u9762\u7528do-while\u5faa\u73af\u4ee3\u66ffwhile\u5faa\u73af\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include int main () { // selection must be declared outside of the do-while so we can use it later int selection {}; do { std :: cout << \"Please make a selection: \\n \" ; std :: cout << \"1) Addition \\n \" ; std :: cout << \"2) Subtraction \\n \" ; std :: cout << \"3) Multiplication \\n \" ; std :: cout << \"4) Division \\n \" ; std :: cin >> selection ; } while ( selection != 1 && selection != 2 && selection != 3 && selection != 4 ); // do something with selection here // such as a switch statement std :: cout << \"You selected option #\" << selection << '\\n' ; return 0 ; } \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u65e2\u907f\u514d\u4e86\u9b54\u9b3c\u6570\u5b57\uff0c\u4e5f\u907f\u514d\u4e86\u989d\u5916\u7684\u53d8\u91cf\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u6709\u4e00\u4ef6\u4e8b\u503c\u5f97\u8ba8\u8bba\uff0c\u90a3\u5c31\u662f selection \u53d8\u91cf\u5fc5\u987b\u5728do\u5757\u4e4b\u5916\u58f0\u660e\u3002\u5982\u679c\u5728do\u5757\u4e2d\u58f0\u660e\u4e86 selection \u53d8\u91cf\uff0c\u90a3\u4e48\u5f53do\u5757\u7ec8\u6b62\u65f6\uff0c\u5b83\u5c06\u88ab\u9500\u6bc1\uff0c\u8fd9\u53d1\u751f\u5728\u6761\u4ef6\u503c\u88ab\u6c42\u503c\u4e4b\u524d\u3002\u4f46\u662f\u6211\u4eec\u9700\u8981\u5728while\u6761\u4ef6\u8bed\u53e5\u4e2d\u58f0\u660e\u53d8\u91cf\u2014\u2014\u56e0\u6b64\uff0c selection \u53d8\u91cf\u5fc5\u987b\u5728do\u5757\u4e4b\u5916\u58f0\u660e(\u5373\u4f7f\u5b83\u6ca1\u6709\u5728\u51fd\u6570\u4f53\u540e\u9762\u4f7f\u7528)\u3002 \u5b9e\u9645\u4e0a\uff0cdo-while\u5faa\u73af\u5e76\u4e0d\u5e38\u7528\u3002\u5c06\u6761\u4ef6\u653e\u5728\u5faa\u73af\u7684\u5e95\u90e8\u4f1a\u6a21\u7cca\u5faa\u73af\u6761\u4ef6\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u56e0\u6b64\uff0c\u8bb8\u591a\u5f00\u53d1\u4eba\u5458\u5efa\u8bae\u5b8c\u5168\u907f\u514ddo-while\u5faa\u73af\u3002\u6211\u4eec\u5c06\u91c7\u53d6\u4e00\u79cd\u8f83\u4e3a\u6e29\u548c\u7684\u7acb\u573a\uff0c\u5982\u679c\u4e24\u79cd\u65b9\u6cd5\u90fd\u53ef\u4ee5\u4f7f\u7528\u65f6\uff0c\u4f7f\u7528while\u5faa\u73af\u800c\u4e0d\u662fdo-while\u5faa\u73af\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u7ed9\u51fa\u76f8\u540c\u7684\u9009\u62e9\u65f6\uff0c\u66f4\u503e\u5411\u4e8ewhile\u5faa\u73af\u800c\u4e0d\u662fdo-while\u5faa\u73af\u3002","title":"7.8 - do-while \u8bed\u53e5"},{"location":"7-8-Do-while-statements/#do-while","text":"\u4e3a\u4e86\u5e2e\u52a9\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\uff0cC++\u63d0\u4f9b\u4e86 do-while \u8bed\u53e5: 1 2 3 do statement ; // \u53ef\u4ee5\u662f\u5355\u4e00\u8bed\u53e5\uff0c\u4e5f\u53ef\u4ee5\u662f\u590d\u5408\u53e5 while ( condition ); do-while \u8bed\u53e5\u662f\u4e00\u4e2a\u5faa\u73af\u6784\u9020\u65b9\u5f0f\uff0c\u5176\u5de5\u4f5c\u539f\u7406\u4e0e while \u5faa\u73af\u7c7b\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u8be5\u8bed\u53e5\u603b\u662f\u81f3\u5c11\u6267\u884c\u4e00\u6b21\u3002\u8bed\u53e5\u6267\u884c\u540e\uff0cdo-while\u5faa\u73af\u68c0\u67e5\u6761\u4ef6\u3002\u5982\u679c\u6761\u4ef6\u7684\u8ba1\u7b97\u7ed3\u679c\u4e3a true \uff0c\u6267\u884c\u8def\u5f84\u5c06\u8df3\u8f6c\u56dedo-while\u5faa\u73af\u7684\u9876\u90e8\u5e76\u518d\u6b21\u6267\u884c\u3002 \u4e0b\u9762\u662f\u6211\u4eec\u4e0a\u9762\u7528do-while\u5faa\u73af\u4ee3\u66ffwhile\u5faa\u73af\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include int main () { // selection must be declared outside of the do-while so we can use it later int selection {}; do { std :: cout << \"Please make a selection: \\n \" ; std :: cout << \"1) Addition \\n \" ; std :: cout << \"2) Subtraction \\n \" ; std :: cout << \"3) Multiplication \\n \" ; std :: cout << \"4) Division \\n \" ; std :: cin >> selection ; } while ( selection != 1 && selection != 2 && selection != 3 && selection != 4 ); // do something with selection here // such as a switch statement std :: cout << \"You selected option #\" << selection << '\\n' ; return 0 ; } \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u65e2\u907f\u514d\u4e86\u9b54\u9b3c\u6570\u5b57\uff0c\u4e5f\u907f\u514d\u4e86\u989d\u5916\u7684\u53d8\u91cf\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u6709\u4e00\u4ef6\u4e8b\u503c\u5f97\u8ba8\u8bba\uff0c\u90a3\u5c31\u662f selection \u53d8\u91cf\u5fc5\u987b\u5728do\u5757\u4e4b\u5916\u58f0\u660e\u3002\u5982\u679c\u5728do\u5757\u4e2d\u58f0\u660e\u4e86 selection \u53d8\u91cf\uff0c\u90a3\u4e48\u5f53do\u5757\u7ec8\u6b62\u65f6\uff0c\u5b83\u5c06\u88ab\u9500\u6bc1\uff0c\u8fd9\u53d1\u751f\u5728\u6761\u4ef6\u503c\u88ab\u6c42\u503c\u4e4b\u524d\u3002\u4f46\u662f\u6211\u4eec\u9700\u8981\u5728while\u6761\u4ef6\u8bed\u53e5\u4e2d\u58f0\u660e\u53d8\u91cf\u2014\u2014\u56e0\u6b64\uff0c selection \u53d8\u91cf\u5fc5\u987b\u5728do\u5757\u4e4b\u5916\u58f0\u660e(\u5373\u4f7f\u5b83\u6ca1\u6709\u5728\u51fd\u6570\u4f53\u540e\u9762\u4f7f\u7528)\u3002 \u5b9e\u9645\u4e0a\uff0cdo-while\u5faa\u73af\u5e76\u4e0d\u5e38\u7528\u3002\u5c06\u6761\u4ef6\u653e\u5728\u5faa\u73af\u7684\u5e95\u90e8\u4f1a\u6a21\u7cca\u5faa\u73af\u6761\u4ef6\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u56e0\u6b64\uff0c\u8bb8\u591a\u5f00\u53d1\u4eba\u5458\u5efa\u8bae\u5b8c\u5168\u907f\u514ddo-while\u5faa\u73af\u3002\u6211\u4eec\u5c06\u91c7\u53d6\u4e00\u79cd\u8f83\u4e3a\u6e29\u548c\u7684\u7acb\u573a\uff0c\u5982\u679c\u4e24\u79cd\u65b9\u6cd5\u90fd\u53ef\u4ee5\u4f7f\u7528\u65f6\uff0c\u4f7f\u7528while\u5faa\u73af\u800c\u4e0d\u662fdo-while\u5faa\u73af\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u7ed9\u51fa\u76f8\u540c\u7684\u9009\u62e9\u65f6\uff0c\u66f4\u503e\u5411\u4e8ewhile\u5faa\u73af\u800c\u4e0d\u662fdo-while\u5faa\u73af\u3002","title":"do-while \u8bed\u53e5"},{"location":"7-9-For-statements/","tags":["for"],"text":"C++\u4e2d\u6700\u5e38\u7528\u7684\u8bed\u53e5\u662f for \u8bed\u53e5\u3002\u5f53\u6211\u4eec\u6709\u4e00\u4e2a\u660e\u663e\u7684\u5faa\u73af\u53d8\u91cf\u65f6\uff0c\u9996\u9009\u4f7f\u7528for\u8bed\u53e5(\u4e5f\u79f0\u4e3afor\u5faa\u73af)\uff0c\u56e0\u4e3a\u5b83\u4f7f\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u800c\u7b80\u6d01\u5730\u5b9a\u4e49\u3001\u521d\u59cb\u5316\u3001\u6d4b\u8bd5\u548c\u66f4\u6539\u5faa\u73af\u53d8\u91cf\u7684\u503c\u3002 \u5728 C++11 \u4e2d\uff0c\u6709\u4e24\u79cd for \u5faa\u73af\uff0c\u6211\u4eec\u4f1a\u5728\u672c\u8282\u8bfe\u4ecb\u7ecd\u6700\u5178\u578b\u7684 for \u5faa\u73af\uff0c\u7136\u540e\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\uff08 11.13 - for-each \u5faa\u73af \uff09\u4ecb\u7ecd\u8f83\u65b0\u7684\u7684 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af ( 11.13 -- For-each loops ) \u3002 for \u5faa\u73af\u8bed\u6cd5\u7684\u62bd\u8c61\u5f62\u5f0f\u975e\u5e38\u7b80\u5355\uff1a 1 2 for ( init - statement ; condition ; end - expression ) statement ; \u7406\u89e3 for \u5faa\u73af\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u662f\u5c06\u5176\u8f6c\u6362\u6210\u4e00\u4e2a\u7b49\u4ef7\u7684 while \u5faa\u73af\uff1a 1 2 3 4 5 6 7 8 { // \u6ce8\u610f\u8fd9\u91cc\u7684\u8bed\u53e5\u5757 init - statement ; // \u7528\u4e8e\u5b9a\u4e49\u5faa\u73af\u4e2d\u4f7f\u7528\u7684\u53d8\u91cf while ( condition ) { statement ; end - expression ; // \u7528\u4e8e\u4fee\u6539\u5faa\u73af\u53d8\u91cf } } // \u5728\u5faa\u73af\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u5728\u8fd9\u91cc\u8d85\u51fa\u4e86\u4f5c\u7528\u57df for \u8bed\u53e5\u6c42\u503c for \u8bed\u53e5\u6c42\u503c\u5206\u4e09\u90e8\u5206\uff1a \u9996\u5148\u662f\u6267\u884c init-statement \uff0c\u4f46\u662f\u5b83\u4e4b\u540e\u5728\u5faa\u73af\u521d\u59cb\u5316\u65f6\u6267\u884c\u4e00\u6b21\u3002 init-statement \u901a\u5e38\u7528\u4e8e\u53d8\u91cf\u7684\u5b9a\u4e49\u548c\u521d\u59cb\u5316\u3002\u8fd9\u4e9b\u53d8\u91cf\u5747\u5177\u6709\u201c\u5faa\u73af\u4f5c\u7528\u57df\u201d\u2014\u2014\u8fd9\u53ea\u662f\u5757\u4f5c\u7528\u57df\u7684\u4e00\u79cd\u5f62\u5f0f\uff0c\u8fd9\u4e9b\u53d8\u91cf\u4ece\u5b9a\u4e49\u70b9\u4e00\u76f4\u5b58\u7eed\u5230\u5faa\u73af\u8bed\u53e5\u7ed3\u675f\u3002\u5728\u7b49\u4ef7\u7684while\u5faa\u73af\u4e2d\uff0c\u53ef\u4ee5\u770b\u5230 init-statement \u4f4d\u4e8e\u5305\u542b\u5faa\u73af\u7684\u5757\u4e2d\uff0c\u56e0\u6b64\u5f53\u5305\u542b\u5faa\u73af\u7684\u5757\u7ed3\u675f\u65f6\uff0c init-statement \u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u5c06\u8d85\u51fa\u4f5c\u7528\u57df\u3002 \u5176\u6b21, \u5bf9\u4e8e\u6bcf\u4e2a\u5faa\u73af\u8fed\u4ee3\uff0c\u90fd\u8981\u8ba1\u7b97\u6761\u4ef6\u3002\u5982\u679c\u8be5\u503c\u4e3a true \uff0c\u5219\u6267\u884c\u8bed\u53e5\u3002\u5982\u679c\u8be5\u503c\u4e3a false \uff0c\u5219\u5faa\u73af\u7ec8\u6b62\uff0c\u5e76\u7ee7\u7eed\u6267\u884c\u5faa\u73af\u4ee5\u5916\u7684\u4e0b\u4e00\u4e2a\u8bed\u53e5\u3002 \u6700\u540e\uff0c\u5f53 statement \u6267\u884c\u540e\uff0c\u4f1a\u5bf9 end-expression \u8fdb\u884c\u6c42\u503c\u3002\u901a\u5e38\uff0c\u8be5\u8868\u8fbe\u5f0f\u7528\u4e8e\u589e\u52a0\u6216\u51cf\u5c11 init-statement \u4e2d\u5b9a\u4e49\u7684\u5faa\u73af\u53d8\u91cf\u3002\u5728\u8ba1\u7b97\u7ed3\u675f\u8868\u8fbe\u5f0f\u4e4b\u540e\uff0c\u6267\u884c\u8fd4\u56de\u5230\u7b2c\u4e8c\u6b65(\u518d\u6b21\u8ba1\u7b97\u6761\u4ef6)\u3002 \u63a5\u4e0b\u6765\u770b\u4e00\u4e2a for \u5faa\u73af\u4f8b\u7a0b\uff0c\u4e86\u89e3\u5176\u5de5\u4f5c\u65b9\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { for ( int count { 1 }; count <= 10 ; ++ count ) std :: cout << count << ' ' ; std :: cout << '\\n' ; return 0 ; } \u9996\u5148\uff0c\u58f0\u660e\u4e00\u4e2a\u5faa\u73af\u53d8\u91cf count \u5e76\u5c06\u5176\u521d\u59cb\u5316\u4e3a1\u3002 \u7136\u540e\u5bf9 count <= 10 \u6c42\u503c\uff0c\u56e0\u4e3a count \u7b49\u4e8e1\uff0c\u6240\u4ee5\u6761\u4ef6\u6c42\u503c\u4e3a true \u3002\u56e0\u6b64\uff0c statement \u4f1a\u88ab\u6267\u884c\uff0c\u6253\u53701\u548c\u7a7a\u683c\u3002 \u6700\u540e\uff0c\u5bf9 ++count \u6c42\u503c\uff0c\u5c06 count \u9012\u589e\u4e3a 2 \u3002\u7136\u540e\u56de\u5230\u7b2c\u4e8c\u6b65\u3002 \u73b0\u5728\uff0c\u518d\u6b21\u5bf9 count <= 10 \u6c42\u503c\u3002\u7531\u4e8e count \u5df2\u7ecf\u662f 2 \u4e86\uff0c\u6240\u4ee5\u6c42\u503c\u7ed3\u679c\u4e3a true \uff0c\u518d\u6b21\u6267\u884c\u5faa\u73af\u3002\u8bed\u53e5\u8f93\u51fa 2 \u548c\u4e00\u4e2a\u7a7a\u683c\uff0c count \u88ab\u589e\u52a0\u5230 3 \u3002\u5faa\u73af\u7ee7\u7eed\u8fed\u4ee3\uff0c\u76f4\u5230 count \u88ab\u589e\u52a0\u5230 11 \uff0c\u6b64\u65f6 count <= 10 \u7684\u8ba1\u7b97\u503c\u4e3a false \uff0c\u5faa\u73af\u9000\u51fa\u3002 \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 1 2 3 4 5 6 7 8 9 10 \u5bf9\u4e8e\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u8ba9\u6211\u4eec\u5c06\u5176\u8f6c\u6362\u4e3a\u7b49\u4ef7\u7684 while \u5faa\u73af\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { { // the block here ensures block scope for count int count { 1 }; // our init-statement while ( count <= 10 ) // our condition { std :: cout << count << ' ' ; // our statement ++ count ; // our end-expression } } std :: cout << '\\n' ; } \u770b\u8d77\u6765\u6ca1\u90a3\u4e48\u7cdf\uff0c\u662f\u5427\uff1f\u6ce8\u610f\uff0c\u8fd9\u91cc\u9700\u8981\u4f7f\u7528\u5916\u5927\u62ec\u53f7\uff0c\u56e0\u4e3a\u5f53\u5faa\u73af\u7ed3\u675f\u65f6 count \u8981 \u79bb\u5f00\u4f5c\u7528\u57df \u3002 \u5bf9\u4e8e\u65b0\u7a0b\u5e8f\u5458\u6765\u8bf4\uff0c\u201cfor\u5faa\u73af\u201d\u53ef\u80fd\u5f88\u96be\u8bfb\u61c2\u2014\u2014\u7136\u800c\uff0c\u6709\u7ecf\u9a8c\u7684\u7a0b\u5e8f\u5458\u559c\u6b22\u5b83\u4eec\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u4e00\u79cd\u975e\u5e38\u7d27\u51d1\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u8ba1\u6570\u5668\u6765\u6267\u884c\u5faa\u73af\uff0c\u5173\u4e8e\u5faa\u73af\u53d8\u91cf\u3001\u5faa\u73af\u6761\u4ef6\u548c\u5faa\u73af\u8ba1\u6570\u4fee\u9970\u7b26\u7684\u6240\u6709\u5fc5\u8981\u4fe1\u606f\u90fd\u5728\u524d\u9762\u7ed9\u51fa\u4e86\u3002\u8fd9\u6709\u52a9\u4e8e\u51cf\u5c11\u9519\u8bef\u3002 \u66f4\u591a\u5faa\u73af\u6848\u4f8b \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 for \u5faa\u73af\u8ba1\u7b97\u6574\u6570\u6307\u6570\u7684\u51fd\u6570\u793a\u4f8b: 1 2 3 4 5 6 7 8 9 10 11 12 #include // for fixed-width integers // returns the value base ^ exponent -- watch out for overflow! std :: int64_t pow ( int base , int exponent ) { std :: int64_t total { 1 }; for ( int count { 0 }; count < exponent ; ++ count ) total *= base ; return total ; } This function returns the value base^exponent (base to the exponent power). This is a straightforward incrementing for loop , with count looping from 0 up to (but excluding) exponent . If exponent is 0 , the for loop will execute 0 times, and the function will return 1 . If exponent is 1 , the for loop will execute 1 time, and the function will return 1 * base . If exponent is 2 , the for loop will execute 2 times, and the function will return 1 * base * base . Although most for loops increment the loop variable by 1, we can decrement it as well: 1 2 3 4 5 6 7 8 9 10 11 #include int main () { for ( int count { 9 }; count >= 0 ; -- count ) std :: cout << count << ' ' ; std :: cout << '\\n' ; return 0 ; } COPY This prints the result: 1 9 8 7 6 5 4 3 2 1 0 Alternately, we can change the value of our loop variable by more than 1 with each iteration: 1 2 3 4 5 6 7 8 9 10 11 #include int main () { for ( int count { 0 }; count <= 10 ; count += 2 ) std :: cout << count << ' ' ; std :: cout << '\\n' ; return 0 ; } COPY This prints the result: 1 0 2 4 6 8 10 The perils of operator!= in for-loop conditions When writing a loop condition involving a value, we can often write the condition in many different ways. The following two loops execute identically: 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { for ( int i { 0 }; i < 10 ; ++ i ) // uses < std :: cout << i ; for ( int i { 0 }; i != 10 ; ++ i ) // uses != std :: cout << i ; return 0 ; } COPY So which should we prefer? The former is the better choice, as it will terminate even if i jumps over the value 10 , whereas the latter will not. The following example demonstrates this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { for ( int i { 0 }; i < 10 ; ++ i ) // uses <, still terminates { std :: cout << i ; if ( i == 9 ) ++ i ; // jump over value 10 } for ( int i { 0 }; i != 10 ; ++ i ) // uses !=, infinite loop { std :: cout << i ; if ( i == 9 ) ++ i ; // jump over value 10 } return 0 ; } COPY \u6700\u4f73\u5b9e\u8df5 Avoid operator!= when doing numeric comparisons in the for-loop condition. \u5dee\u4e00\u9519\u8bef One of the biggest problems that new programmers have with for loops (and other loops that utilize counters) are \u5dee\u4e00\u9519\u8bef \u3002 Off-by-one errorsoccur when the loop iterates one too many or one too few times to produce the desired result. Here\u2019s an example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { // oops, we used operator< instead of operator<= for ( int count { 1 }; count < 5 ; ++ count ) { std :: cout << count << ' ' ; } std :: cout << '\\n' ; return 0 ; } COPY This program is supposed to print 1 2 3 4 5 , but it only prints 1 2 3 4 because we used the wrong relational operator. Although the most common cause for these errors is using the wrong relational operator, they can sometimes occur by using pre-increment or pre-decrement instead of post-increment or post-decrement, or vice-versa. \u7701\u7565\u8868\u8fbe\u5f0f It is possible to write for loops that omit any or all of the statements or expressions. For example, in the following example, we\u2019ll omit the init-statement and end-expression, leaving only the condition: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int count { 0 }; for ( ; count < 10 ; ) // no init-statement or end-expression { std :: cout << count << ' ' ; ++ count ; } std :: cout << '\\n' ; return 0 ; } COPY This for loop produces the result: 1 0 1 2 3 4 5 6 7 8 9 Rather than having the for loop do the initialization and incrementing, we\u2019ve done it manually. We have done so purely for academic purposes in this example, but there are cases where not declaring a loop variable (because you already have one) or not incrementing it in the end-expression (because you\u2019re incrementing it some other way) is desired. Although you do not see it very often, it is worth noting that the following example produces an infinite loop: 1 2 for (;;) statement ; COPY The above example is equivalent to: 1 2 while ( true ) statement ; COPY This might be a little unexpected, as you\u2019d probably expect an omitted condition-expression to be treated as false . However, the C++ standard explicitly (and inconsistently) defines that an omitted condition-expression in a for loop should be treated as true . We recommend avoiding this form of the for loop altogether and using while(true) instead. \u6709\u591a\u4e2a\u8ba1\u6570\u7684\u5faa\u73af Although for loops typically iterate over only one variable, sometimes for loops need to work with multiple variables. To assist with this, the programmer can define multiple variables in the init-statement, and can make use of the comma operator to change the value of multiple variables in the end-expression: 1 2 3 4 5 6 7 8 9 #include int main () { for ( int x { 0 }, y { 9 }; x < 10 ; ++ x , -- y ) std :: cout << x << ' ' << y << '\\n' ; return 0 ; } COPY This loop defines and initializes two new variables: x and y . It iterates x over the range 0 to 9 , and after each iteration x is incremented and y is decremented. This program produces the result: 1 2 3 4 5 6 7 8 9 10 0 9 1 8 2 7 3 6 4 5 5 4 6 3 7 2 8 1 9 0 This is about the only place in C++ where defining multiple variables in the same statement, and use of the comma operator is considered an acceptable practice. \u6700\u4f73\u5b9e\u8df5 Defining multiple variables (in the init-statement) and using the comma operator (in the end-expression) is acceptable inside a for statement . \u5d4c\u5957\u5faa\u73af Like other types of loops, for loops can be nested inside other loops. In the following example, we\u2019re nesting a for loop inside another for loop : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { for ( char c { 'a' }; c <= 'e' ; ++ c ) // outer loop on letters { std :: cout << c ; // print our letter first for ( int i { 0 }; i < 3 ; ++ i ) // inner loop on all numbers std :: cout << i ; std :: cout << '\\n' ; } return 0 ; } COPY For each iteration of the outer loop, the inner loop runs in its entirety. Consequently, the output is: 1 2 3 4 5 a012 b012 c012 d012 e012 Here\u2019s some more detail on what\u2019s happening here. The outer loop runs first, and char c is initialized to 'a' . Then c <= 'e' is evaluated, which is true , so the loop body executes. Since c is set to 'a' , this first prints a . Next the inner loop executes entirely (which prints 0 , 1 , and 2 ). Then a newline is printed. Now the outer loop body is finished, so the outer loop returns to the top, c is incremented to 'b' , and the loop condition is re-evaluated. Since the loop condition is still true the next iteration of the outer loop begins. This prints b012\\n . And so on. \u5c0f\u7ed3 For statements are the most commonly used loop in the C++ language. Even though its syntax is typically a bit confusing to new programmers, you will see for loops so often that you will understand them in no time at all! For statements excel when you have a counter variable. If you do not have a counter, a while statement is probably a better choice. \u6700\u4f73\u5b9e\u8df5 Prefer for loops over while loops when there is an obvious loop variable. Prefer while loops over for loops when there is no obvious loop variable.","title":"7.9 - for \u8bed\u53e5"},{"location":"7-9-For-statements/#for","text":"for \u8bed\u53e5\u6c42\u503c\u5206\u4e09\u90e8\u5206\uff1a \u9996\u5148\u662f\u6267\u884c init-statement \uff0c\u4f46\u662f\u5b83\u4e4b\u540e\u5728\u5faa\u73af\u521d\u59cb\u5316\u65f6\u6267\u884c\u4e00\u6b21\u3002 init-statement \u901a\u5e38\u7528\u4e8e\u53d8\u91cf\u7684\u5b9a\u4e49\u548c\u521d\u59cb\u5316\u3002\u8fd9\u4e9b\u53d8\u91cf\u5747\u5177\u6709\u201c\u5faa\u73af\u4f5c\u7528\u57df\u201d\u2014\u2014\u8fd9\u53ea\u662f\u5757\u4f5c\u7528\u57df\u7684\u4e00\u79cd\u5f62\u5f0f\uff0c\u8fd9\u4e9b\u53d8\u91cf\u4ece\u5b9a\u4e49\u70b9\u4e00\u76f4\u5b58\u7eed\u5230\u5faa\u73af\u8bed\u53e5\u7ed3\u675f\u3002\u5728\u7b49\u4ef7\u7684while\u5faa\u73af\u4e2d\uff0c\u53ef\u4ee5\u770b\u5230 init-statement \u4f4d\u4e8e\u5305\u542b\u5faa\u73af\u7684\u5757\u4e2d\uff0c\u56e0\u6b64\u5f53\u5305\u542b\u5faa\u73af\u7684\u5757\u7ed3\u675f\u65f6\uff0c init-statement \u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u5c06\u8d85\u51fa\u4f5c\u7528\u57df\u3002 \u5176\u6b21, \u5bf9\u4e8e\u6bcf\u4e2a\u5faa\u73af\u8fed\u4ee3\uff0c\u90fd\u8981\u8ba1\u7b97\u6761\u4ef6\u3002\u5982\u679c\u8be5\u503c\u4e3a true \uff0c\u5219\u6267\u884c\u8bed\u53e5\u3002\u5982\u679c\u8be5\u503c\u4e3a false \uff0c\u5219\u5faa\u73af\u7ec8\u6b62\uff0c\u5e76\u7ee7\u7eed\u6267\u884c\u5faa\u73af\u4ee5\u5916\u7684\u4e0b\u4e00\u4e2a\u8bed\u53e5\u3002 \u6700\u540e\uff0c\u5f53 statement \u6267\u884c\u540e\uff0c\u4f1a\u5bf9 end-expression \u8fdb\u884c\u6c42\u503c\u3002\u901a\u5e38\uff0c\u8be5\u8868\u8fbe\u5f0f\u7528\u4e8e\u589e\u52a0\u6216\u51cf\u5c11 init-statement \u4e2d\u5b9a\u4e49\u7684\u5faa\u73af\u53d8\u91cf\u3002\u5728\u8ba1\u7b97\u7ed3\u675f\u8868\u8fbe\u5f0f\u4e4b\u540e\uff0c\u6267\u884c\u8fd4\u56de\u5230\u7b2c\u4e8c\u6b65(\u518d\u6b21\u8ba1\u7b97\u6761\u4ef6)\u3002 \u63a5\u4e0b\u6765\u770b\u4e00\u4e2a for \u5faa\u73af\u4f8b\u7a0b\uff0c\u4e86\u89e3\u5176\u5de5\u4f5c\u65b9\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { for ( int count { 1 }; count <= 10 ; ++ count ) std :: cout << count << ' ' ; std :: cout << '\\n' ; return 0 ; } \u9996\u5148\uff0c\u58f0\u660e\u4e00\u4e2a\u5faa\u73af\u53d8\u91cf count \u5e76\u5c06\u5176\u521d\u59cb\u5316\u4e3a1\u3002 \u7136\u540e\u5bf9 count <= 10 \u6c42\u503c\uff0c\u56e0\u4e3a count \u7b49\u4e8e1\uff0c\u6240\u4ee5\u6761\u4ef6\u6c42\u503c\u4e3a true \u3002\u56e0\u6b64\uff0c statement \u4f1a\u88ab\u6267\u884c\uff0c\u6253\u53701\u548c\u7a7a\u683c\u3002 \u6700\u540e\uff0c\u5bf9 ++count \u6c42\u503c\uff0c\u5c06 count \u9012\u589e\u4e3a 2 \u3002\u7136\u540e\u56de\u5230\u7b2c\u4e8c\u6b65\u3002 \u73b0\u5728\uff0c\u518d\u6b21\u5bf9 count <= 10 \u6c42\u503c\u3002\u7531\u4e8e count \u5df2\u7ecf\u662f 2 \u4e86\uff0c\u6240\u4ee5\u6c42\u503c\u7ed3\u679c\u4e3a true \uff0c\u518d\u6b21\u6267\u884c\u5faa\u73af\u3002\u8bed\u53e5\u8f93\u51fa 2 \u548c\u4e00\u4e2a\u7a7a\u683c\uff0c count \u88ab\u589e\u52a0\u5230 3 \u3002\u5faa\u73af\u7ee7\u7eed\u8fed\u4ee3\uff0c\u76f4\u5230 count \u88ab\u589e\u52a0\u5230 11 \uff0c\u6b64\u65f6 count <= 10 \u7684\u8ba1\u7b97\u503c\u4e3a false \uff0c\u5faa\u73af\u9000\u51fa\u3002 \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 1 2 3 4 5 6 7 8 9 10 \u5bf9\u4e8e\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u8ba9\u6211\u4eec\u5c06\u5176\u8f6c\u6362\u4e3a\u7b49\u4ef7\u7684 while \u5faa\u73af\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { { // the block here ensures block scope for count int count { 1 }; // our init-statement while ( count <= 10 ) // our condition { std :: cout << count << ' ' ; // our statement ++ count ; // our end-expression } } std :: cout << '\\n' ; } \u770b\u8d77\u6765\u6ca1\u90a3\u4e48\u7cdf\uff0c\u662f\u5427\uff1f\u6ce8\u610f\uff0c\u8fd9\u91cc\u9700\u8981\u4f7f\u7528\u5916\u5927\u62ec\u53f7\uff0c\u56e0\u4e3a\u5f53\u5faa\u73af\u7ed3\u675f\u65f6 count \u8981 \u79bb\u5f00\u4f5c\u7528\u57df \u3002 \u5bf9\u4e8e\u65b0\u7a0b\u5e8f\u5458\u6765\u8bf4\uff0c\u201cfor\u5faa\u73af\u201d\u53ef\u80fd\u5f88\u96be\u8bfb\u61c2\u2014\u2014\u7136\u800c\uff0c\u6709\u7ecf\u9a8c\u7684\u7a0b\u5e8f\u5458\u559c\u6b22\u5b83\u4eec\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u4e00\u79cd\u975e\u5e38\u7d27\u51d1\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u8ba1\u6570\u5668\u6765\u6267\u884c\u5faa\u73af\uff0c\u5173\u4e8e\u5faa\u73af\u53d8\u91cf\u3001\u5faa\u73af\u6761\u4ef6\u548c\u5faa\u73af\u8ba1\u6570\u4fee\u9970\u7b26\u7684\u6240\u6709\u5fc5\u8981\u4fe1\u606f\u90fd\u5728\u524d\u9762\u7ed9\u51fa\u4e86\u3002\u8fd9\u6709\u52a9\u4e8e\u51cf\u5c11\u9519\u8bef\u3002","title":"for \u8bed\u53e5\u6c42\u503c"},{"location":"7-9-For-statements/#_1","text":"\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 for \u5faa\u73af\u8ba1\u7b97\u6574\u6570\u6307\u6570\u7684\u51fd\u6570\u793a\u4f8b: 1 2 3 4 5 6 7 8 9 10 11 12 #include // for fixed-width integers // returns the value base ^ exponent -- watch out for overflow! std :: int64_t pow ( int base , int exponent ) { std :: int64_t total { 1 }; for ( int count { 0 }; count < exponent ; ++ count ) total *= base ; return total ; } This function returns the value base^exponent (base to the exponent power). This is a straightforward incrementing for loop , with count looping from 0 up to (but excluding) exponent . If exponent is 0 , the for loop will execute 0 times, and the function will return 1 . If exponent is 1 , the for loop will execute 1 time, and the function will return 1 * base . If exponent is 2 , the for loop will execute 2 times, and the function will return 1 * base * base . Although most for loops increment the loop variable by 1, we can decrement it as well: 1 2 3 4 5 6 7 8 9 10 11 #include int main () { for ( int count { 9 }; count >= 0 ; -- count ) std :: cout << count << ' ' ; std :: cout << '\\n' ; return 0 ; } COPY This prints the result: 1 9 8 7 6 5 4 3 2 1 0 Alternately, we can change the value of our loop variable by more than 1 with each iteration: 1 2 3 4 5 6 7 8 9 10 11 #include int main () { for ( int count { 0 }; count <= 10 ; count += 2 ) std :: cout << count << ' ' ; std :: cout << '\\n' ; return 0 ; } COPY This prints the result: 1 0 2 4 6 8 10 The perils of operator!= in for-loop conditions When writing a loop condition involving a value, we can often write the condition in many different ways. The following two loops execute identically: 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { for ( int i { 0 }; i < 10 ; ++ i ) // uses < std :: cout << i ; for ( int i { 0 }; i != 10 ; ++ i ) // uses != std :: cout << i ; return 0 ; } COPY So which should we prefer? The former is the better choice, as it will terminate even if i jumps over the value 10 , whereas the latter will not. The following example demonstrates this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { for ( int i { 0 }; i < 10 ; ++ i ) // uses <, still terminates { std :: cout << i ; if ( i == 9 ) ++ i ; // jump over value 10 } for ( int i { 0 }; i != 10 ; ++ i ) // uses !=, infinite loop { std :: cout << i ; if ( i == 9 ) ++ i ; // jump over value 10 } return 0 ; } COPY \u6700\u4f73\u5b9e\u8df5 Avoid operator!= when doing numeric comparisons in the for-loop condition.","title":"\u66f4\u591a\u5faa\u73af\u6848\u4f8b"},{"location":"7-9-For-statements/#_2","text":"One of the biggest problems that new programmers have with for loops (and other loops that utilize counters) are \u5dee\u4e00\u9519\u8bef \u3002 Off-by-one errorsoccur when the loop iterates one too many or one too few times to produce the desired result. Here\u2019s an example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { // oops, we used operator< instead of operator<= for ( int count { 1 }; count < 5 ; ++ count ) { std :: cout << count << ' ' ; } std :: cout << '\\n' ; return 0 ; } COPY This program is supposed to print 1 2 3 4 5 , but it only prints 1 2 3 4 because we used the wrong relational operator. Although the most common cause for these errors is using the wrong relational operator, they can sometimes occur by using pre-increment or pre-decrement instead of post-increment or post-decrement, or vice-versa.","title":"\u5dee\u4e00\u9519\u8bef"},{"location":"7-9-For-statements/#_3","text":"It is possible to write for loops that omit any or all of the statements or expressions. For example, in the following example, we\u2019ll omit the init-statement and end-expression, leaving only the condition: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int count { 0 }; for ( ; count < 10 ; ) // no init-statement or end-expression { std :: cout << count << ' ' ; ++ count ; } std :: cout << '\\n' ; return 0 ; } COPY This for loop produces the result: 1 0 1 2 3 4 5 6 7 8 9 Rather than having the for loop do the initialization and incrementing, we\u2019ve done it manually. We have done so purely for academic purposes in this example, but there are cases where not declaring a loop variable (because you already have one) or not incrementing it in the end-expression (because you\u2019re incrementing it some other way) is desired. Although you do not see it very often, it is worth noting that the following example produces an infinite loop: 1 2 for (;;) statement ; COPY The above example is equivalent to: 1 2 while ( true ) statement ; COPY This might be a little unexpected, as you\u2019d probably expect an omitted condition-expression to be treated as false . However, the C++ standard explicitly (and inconsistently) defines that an omitted condition-expression in a for loop should be treated as true . We recommend avoiding this form of the for loop altogether and using while(true) instead.","title":"\u7701\u7565\u8868\u8fbe\u5f0f"},{"location":"7-9-For-statements/#_4","text":"Although for loops typically iterate over only one variable, sometimes for loops need to work with multiple variables. To assist with this, the programmer can define multiple variables in the init-statement, and can make use of the comma operator to change the value of multiple variables in the end-expression: 1 2 3 4 5 6 7 8 9 #include int main () { for ( int x { 0 }, y { 9 }; x < 10 ; ++ x , -- y ) std :: cout << x << ' ' << y << '\\n' ; return 0 ; } COPY This loop defines and initializes two new variables: x and y . It iterates x over the range 0 to 9 , and after each iteration x is incremented and y is decremented. This program produces the result: 1 2 3 4 5 6 7 8 9 10 0 9 1 8 2 7 3 6 4 5 5 4 6 3 7 2 8 1 9 0 This is about the only place in C++ where defining multiple variables in the same statement, and use of the comma operator is considered an acceptable practice. \u6700\u4f73\u5b9e\u8df5 Defining multiple variables (in the init-statement) and using the comma operator (in the end-expression) is acceptable inside a for statement .","title":"\u6709\u591a\u4e2a\u8ba1\u6570\u7684\u5faa\u73af"},{"location":"7-9-For-statements/#_5","text":"Like other types of loops, for loops can be nested inside other loops. In the following example, we\u2019re nesting a for loop inside another for loop : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { for ( char c { 'a' }; c <= 'e' ; ++ c ) // outer loop on letters { std :: cout << c ; // print our letter first for ( int i { 0 }; i < 3 ; ++ i ) // inner loop on all numbers std :: cout << i ; std :: cout << '\\n' ; } return 0 ; } COPY For each iteration of the outer loop, the inner loop runs in its entirety. Consequently, the output is: 1 2 3 4 5 a012 b012 c012 d012 e012 Here\u2019s some more detail on what\u2019s happening here. The outer loop runs first, and char c is initialized to 'a' . Then c <= 'e' is evaluated, which is true , so the loop body executes. Since c is set to 'a' , this first prints a . Next the inner loop executes entirely (which prints 0 , 1 , and 2 ). Then a newline is printed. Now the outer loop body is finished, so the outer loop returns to the top, c is incremented to 'b' , and the loop condition is re-evaluated. Since the loop condition is still true the next iteration of the outer loop begins. This prints b012\\n . And so on.","title":"\u5d4c\u5957\u5faa\u73af"},{"location":"7-9-For-statements/#_6","text":"For statements are the most commonly used loop in the C++ language. Even though its syntax is typically a bit confusing to new programmers, you will see for loops so often that you will understand them in no time at all! For statements excel when you have a counter variable. If you do not have a counter, a while statement is probably a better choice. \u6700\u4f73\u5b9e\u8df5 Prefer for loops over while loops when there is an obvious loop variable. Prefer while loops over for loops when there is no obvious loop variable.","title":"\u5c0f\u7ed3"},{"location":"7-x-Chapter-7-summary-and-quiz/","tags":["summary"],"text":"The specific sequence of statements that the CPU executes in a program is called the program\u2019s execution path. A straight-line program takes the same path every time it is run. Control flow statements (also called Flow control statements) allow the programmer to change the normal path of execution. When a control flow statement causes the program to begin executing some non-sequential instruction sequence, this is called a branch. A conditional statement is a statement that specifies whether some associated statement(s) should be executed or not. If statements allow us to execute an associated statement based on whether some condition is true . Else statements execute if the associated condition is false . You can chain together multiple if and else statements. A dangling else occurs when it is ambiguous which if statement an else statement is connected to. Dangling else statements are matched up with the last unmatched if statement in the same block. Thus, we trivially avoid dangling else statements by ensuring the body of an if statement is placed in a block. A null statement is a statement that consists of just a semicolon. It does nothing, and is used when the language requires a statement to exist but the programmer does not need the statement to do anything. Switch statements provide a cleaner and faster method for selecting between a number of matching items. Switch statements only work with integral types. Case labels are used to identify the values for the evaluated condition to match. The statements beneath a default label are executed if no matching case label can be found. When execution flows from a statement underneath a label into statements underneath a subsequent label, this is called fallthrough. A break statement (or return statement ) can be used to prevent fallthrough. The fallthrough attribute can be used to document intentional fallthrough. Goto statements allow the program to jump to somewhere else in the code, either forward or backwards. These should generally be avoided, as they can create spaghetti code, which occurs when a program has a path of execution that resembles a bowl of spaghetti. While loops allow the program to loop as long as a given condition evaluates to true . The condition is evaluated before the loop executes. An infinite loop is a loop that has a condition that always evaluates to true . These loops will loop forever unless another control flow statement is used to stop them. A loop variable (also called a counter) is an integer variable used to count how many times a loop has executed. Each execution of a loop is called an iteration. Do while loops are similar to while loops, but the condition is evaluated after the loop executes instead of before. For loops are the most used loop, and are ideal when you need to loop a specific number of times. An off-by-one error occurs when the loop iterates one too many or one too few times. Break statements allow us to break out of a switch, while, do while, or for loop (also range-based for loops , which we haven\u2019t covered yet). Continue statements allow us to move immediately to the next loop iteration. Halts allow us to terminate our program. Normal termination means the program has exited in an expected way (and the status code will indicate whether it succeeded or not). std::exit() is automatically called at the end of main , or it can be called explicitly to terminate the program. It does some cleanup, but does not cleanup any local variables, or unwind the call stack. Abnormal termination occurs when the program encountered some kind of unexpected error and had to be shut down. std::abort can be called for an abnormal termination. Scope creep occurs when a project\u2019s capabilities grow beyond what was originally intended at the start of the project or project phase. Software verification is the process of testing whether or not the software works as expected in all cases. A unit test is a test designed to test a small portion of the code (typically a function or call) in isolation to ensure a particular behavior occurs as expected. Unit test frameworks can help you organize your unit tests. Integration testing tests the integration of a bunch of units together to ensure they work properly. Code coverage refers to how much of the source code is executed while testing. Statement coverage refers to the percentage of statements in a program that have been exercised by testing routines. Branch coverage refers to the percentage of branches that have been executed by testing routines. Loop coverage (also called the 0, 1, 2 test) means that if you have a loop, you should ensure it works properly when it iterates 0 times, 1 time, and 2 times. The happy path is the path of execution that occurs when there are no errors encountered. A sad path is one where an error or failure state occurs. A non-recoverable error (also called a fatal error) is an error that is severe enough that the program can\u2019t continue running. A program that handles error cases well is robust. A buffer is a piece of memory set aside for storing data temporarily while it is moved from one place to another. The process of checking whether user input conforms to what the program is expecting is called input validation. std::cerr is an output stream (like std::cout ) designed to be used for error messages. A precondition is any condition that must always be true prior to the execution of some segment of code. An invariant is a condition that must be true while some component is executing. A postcondition is any condition that must always be true after the execution of some code. An assertion is an expression that will be true unless there is a bug in the program. In C++, runtime assertions are typically implemented using the assert preprocessor macro. Assertions are usually turned off in non-debug code. A static_assert is an assertion that is evaluated at compile-time. Assertions should be used to document cases that should be logically impossible. Error handling should be used to handle cases that are possible. An algorithm is a finite sequence of instructions that can be followed to solve some problem or produce some useful result. An algorithm is considered to be stateful if it retains some information across calls. Conversely, a stateless algorithm does not store any information (and must be given all the information it needs to work with when it is called). When applied to algorithms, the term state refers to the current values held in stateful variables. An algorithm is considered deterministic if for a given input (the value provided for start ) it will always produce the same output sequence. A pseudo-random number generator (PRNG) is an algorithm that generates a sequence of numbers whose properties simulate a sequence of random numbers. When a PRNG is instantiated, an initial value (or set of values) called a random seed (or seed for short) can be provided to initialize the state of the PRNG. When a PRNG has been initialized with a seed, we say it has been seeded. The size of the seed value can be smaller than the size of the state of the PRNG. When this happens, we say the PRNG has been underseeded. The length of the sequence before a PRNG begins to repeat itself is known as the period. A random number distribution converts the output of a PRNG into some other distribution of numbers. A uniform distribution is a random number distribution that produces outputs between two numbers X and Y (inclusive) with equal probability.","title":"7.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406"},{"location":"8-1-Implicit-type-conversion-coercion/","tags":["type conversion","conversions"],"text":"Key Takeaway \u7c7b\u578b\u8f6c\u6362\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\u89e6\u53d1\uff1a\u9690\u5f0f\u5730\uff08\u7f16\u8bd1\u5668\u9700\u8981\u8fd9\u4e48\u505a\uff09\u6216\u662f\u663e\u5f0f\u5730\uff08\u7531\u7a0b\u5e8f\u5458\u53d1\u8d77\uff09 C++ \u4e2d\u7684\u5927\u90e8\u5206\u7c7b\u578b\u8f6c\u6362\u90fd\u662f\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u3002\u5b83\u4f1a\u53d1\u751f\u5728\u5982\u4e0b\u65f6\u673a\uff1a \u5f53\u4f7f\u7528\u4e0d\u540c\u7c7b\u578b\u7684\u53d8\u91cf\u5bf9\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\u7684\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u6216\u8d4b\u503c\u7684\u65f6\u5019 \u5f53\u51fd\u6570\u7684\u5b9e\u9645\u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u540c\u4e8e\u51fd\u6570\u5b9a\u4e49\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u65f6 \u5f53\u5bf9\u4e0d\u540c\u7c7b\u578b\u7684\u64cd\u4f5c\u6570\u4f7f\u7528\u67d0\u4e9b\u4e8c\u5143\u64cd\u4f5c\u7b26\u65f6 \u5f53\u5728 if \u8bed\u53e5\u4e2d\u4f7f\u7528\u975e\u5e03\u5c14\u503c\u65f6 \u5f53\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u5b9e\u53c2\u4e0e\u51fd\u6570\u5f62\u53c2\u7c7b\u578b\u4e0d\u540c\u65f6 C++ \u6807\u51c6\u5b9a\u4e49\u4e86\u4e0d\u540c\u7684\u57fa\u672c\u7c7b\u578b\u7684\u8f6c\u6362\u65b9\u5f0f\uff0c\u5305\u62ec\uff1a \u6570\u503c\u63d0\u5347(\u53c2\u8003\uff1a 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 )\uff1b \u6570\u503c\u8f6c\u6362(\u53c2\u8003\uff1a 8.3 - \u6570\u503c\u8f6c\u6362 )\uff1b \u7b97\u6570\u8f6c\u6362(\u53c2\u8003\uff1a 8.4 - \u7b97\u6570\u8f6c\u6362 )\uff1b \u5176\u4ed6\u8f6c\u6362 (\u5305\u62ec\u591a\u79cd\u6307\u9488\u548c\u5f15\u7528\u7684\u8f6c\u6362) \u7c7b\u578b\u8f6c\u6362\u7b80\u4ecb \u5bf9\u8c61\u7684\u503c\u662f\u4ee5\u4e00\u7cfb\u5217\u7684 \u6bd4\u7279 \u5b58\u653e\u7684\uff0c\u800c\u6570\u636e\u7c7b\u578b\u5219\u544a\u8bc9\u7f16\u8bd1\u5668\u5e94\u8be5\u5982\u4f55\u5c06\u8fd9\u4e00\u7cfb\u5217\u6bd4\u7279\u89e3\u6790\u6210\u6709\u610f\u4e49\u7684\u503c\u3002\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u4ee5\u4e0d\u540c\u7684\u65b9\u5f0f\u8868\u793a \u76f8\u540c \u7684\u503c\u3002\u4f8b\u5982\uff0c\u6574\u578b\u503c 3 \u53ef\u80fd\u88ab\u5b58\u653e\u4e3a\u4e8c\u8fdb\u5236 0000 0000 0000 0000 0000 0000 0000 0011 \uff0c\u800c\u6d6e\u70b9\u6570 3.0 \u5219\u53ef\u80fd\u88ab\u5b58\u653e\u4e3a\u4e8c\u8fdb\u5236 0100 0000 0100 0000 0000 0000 0000 0000 \u3002 \u90a3\u4e48\u5f53\u6211\u4eec\u8fd9\u6837\u505a\u7684\u65f6\u5019\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462? 1 float f { 3 }; // initialize floating point variable with int 3 \u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u4e0d\u80fd\u4ec5\u4ec5\u628a\u8868\u793a int \u7c7b\u578b\u503c 3 \u7684\u6bd4\u7279\u62f7\u8d1d\u5230\u5185\u5b58\u4e2d\u8868\u793a float \u53d8\u91cf f \u3002\u76f8\u53cd\uff0c\u5b83\u5fc5\u987b\u5c06\u6574\u578b\u503c 3 \u8f6c\u6362\u4e3a\u7b49\u4ef7\u7684\u6d6e\u70b9\u6570\uff0c\u7136\u540e\u624d\u80fd\u5b58\u653e\u5230\u5185\u5b58\u4e2d\u8868\u793a\u53d8\u91cf f \u3002 \u5c06\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u7684\u8fc7\u7a0b\uff0c\u79f0\u4e3a\u7c7b\u578b\u8f6c\u6362\u3002 \u7c7b\u578b\u8f6c\u6362\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\u89e6\u53d1\uff1a\u9690\u5f0f\u5730\uff08\u7f16\u8bd1\u5668\u9700\u8981\u8fd9\u4e48\u505a\uff09\u6216\u662f\u663e\u5f0f\u5730\uff08\u7531\u7a0b\u5e8f\u5458\u53d1\u8d77\uff09\u3002\u6211\u4eec\u4f1a\u5728\u672c\u8282\u8bfe\u4ecb\u7ecd \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362(implicit type conversion) \uff0c\u7136\u540e\u5728\u4e0b\u8282\u8bfe 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u4e2d\u4ecb\u7ecd \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362(Explicit type conversion) \u3002 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\uff08\u4e5f\u53eb\u505a\u81ea\u52a8\u7c7b\u578b\u8f6c\u6362\uff09\u7531\u7f16\u8bd1\u5668\u5728\u9700\u8981\u65f6\uff08\u9700\u8981\u67d0\u4e2a\u53d8\u91cf\u7c7b\u578b\u4f46\u5374\u7ed9\u5b9a\u4e86\u53e6\u5916\u7684\u7c7b\u578b\uff09\u81ea\u52a8\u5b8c\u6210\u3002C++ \u4e2d\u7684\u5927\u90e8\u5206\u7c7b\u578b\u8f6c\u6362\u90fd\u662f\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u3002\u5b83\u4f1a\u53d1\u751f\u5728\u5982\u4e0b\u65f6\u673a\uff1a \u5f53\u4f7f\u7528\u4e0d\u540c\u7c7b\u578b\u7684\u53d8\u91cf\u5bf9\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\u7684\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u6216\u8d4b\u503c\u7684\u65f6\u5019\uff1a 1 2 double d { 3 }; // int 3 \u9690\u5f0f\u8f6c\u6362\u4e3a double \u7c7b\u578b d = 6 ; // int 6 \u9690\u5f0f\u8f6c\u6362\u4e3a double \u7c7b\u578b \u5f53\u51fd\u6570\u7684\u5b9e\u9645\u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u540c\u4e8e\u51fd\u6570\u5b9a\u4e49\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u65f6\uff1a 1 2 3 4 float doSomething () { return 3.0 ; // double 3.0 \u9690\u5f0f\u8f6c\u6362\u4e3a float \u7c7b\u578b } \u5f53\u5bf9\u4e0d\u540c\u7c7b\u578b\u7684\u64cd\u4f5c\u6570\u4f7f\u7528\u67d0\u4e9b\u4e8c\u5143\u64cd\u4f5c\u7b26\u65f6\uff1a 1 double division { 4.0 / 3 }; // int 3 \u9690\u5f0f\u8f6c\u6362\u4e3a double \u7c7b\u578b \u5f53\u5728 if \u8bed\u53e5\u4e2d\u4f7f\u7528\u975e\u5e03\u5c14\u503c\u65f6\uff1a 1 2 3 if ( 5 ) // int 5 \u9690\u5f0f\u8f6c\u6362\u4e3a bool \u7c7b\u578b { } \u5f53\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u5b9e\u53c2\u4e0e\u51fd\u6570\u5f62\u53c2\u7c7b\u578b\u4e0d\u540c\u65f6\uff1a 1 2 3 4 5 void doSomething ( long l ) { } doSomething ( 3 ); // int 3 \u9690\u5f0f\u8f6c\u6362\u4e3a long \u7c7b\u578b \u5f53\u7c7b\u578b\u8f6c\u6362\u89e6\u53d1\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48 \u5f53\u7c7b\u578b\u8f6c\u6362\u89e6\u53d1\u65f6(\u65e0\u8bba\u662f\u9690\u5f0f\u8fd8\u662f\u663e\u5f0f)\uff0c\u7f16\u8bd1\u5668\u9700\u8981\u786e\u5b9a\u662f\u5426\u53ef\u4ee5\u5c06\u503c\u4ece\u5f53\u524d\u7c7b\u578b\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u7c7b\u578b\u3002\u5982\u679c\u53ef\u4ee5\u627e\u5230\u6709\u6548\u7684\u8f6c\u6362\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c06\u751f\u6210\u6240\u9700\u7c7b\u578b\u7684\u65b0\u503c\u3002\u6ce8\u610f\uff0c\u7c7b\u578b\u8f6c\u6362\u4e0d\u4f1a\u6539\u53d8\u88ab\u8f6c\u6362\u7684\u503c\u6216\u5bf9\u8c61\u7684\u503c\u6216\u7c7b\u578b\u3002 \u5982\u679c\u7f16\u8bd1\u5668\u65e0\u6cd5\u627e\u5230\u53ef\u7528\u7684\u8f6c\u6362\uff0c\u5219\u4f1a\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002\u7c7b\u578b\u8f6c\u6362\u5931\u8d25\u7684\u539f\u56e0\u6709\u5f88\u591a\u3002\u4f8b\u5982\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4e0d\u77e5\u9053\u5982\u4f55\u5728\u539f\u59cb\u7c7b\u578b\u548c\u6240\u9700\u7c7b\u578b\u4e4b\u95f4\u8f6c\u6362\u503c\u3002\u6216\u8005\uff0c\u8bed\u53e5\u53ef\u80fd\u4e0d\u5141\u8bb8\u67d0\u4e9b\u7c7b\u578b\u7684\u8f6c\u6362\u3002\u4f8b\u5982: 1 int x { 3.5 }; // \u62ec\u53f7\u7c7b\u578b\u8f6c\u6362\u4e0d\u5141\u8bb8\u7c7b\u578b\u8f6c\u6362\u4ee5\u907f\u514d\u6570\u636e\u4e22\u5931 \u5373\u4f7f\u7f16\u8bd1\u5668\u77e5\u9053\u5982\u4f55\u5c06 double \u7c7b\u578b\u8f6c\u6362\u4e3a int \u7c7b\u578b\uff0c\u4f46\u5728\u4f7f\u7528\u62ec\u53f7\u521d\u59cb\u5316\u65f6\uff0c\u8fd9\u6837\u7684\u8f6c\u6362\u662f\u4e0d\u5141\u8bb8\u7684\u3002 \u8fd8\u6709\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u65e0\u6cd5\u786e\u5b9a\u51e0\u79cd\u53ef\u80fd\u7684\u7c7b\u578b\u8f6c\u6362\u4e2d\u54ea\u4e00\u79cd\u662f\u6700\u9002\u5408\u4f7f\u7528\u7684\u3002\u6211\u4eec\u4f1a\u5728 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u5339\u914d\u6b67\u4e49 \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u90a3\u4e48\u7f16\u8bd1\u5668\u5b9e\u9645\u4e0a\u662f\u5982\u4f55\u786e\u5b9a\u5b83\u662f\u5426\u53ef\u4ee5\u5c06\u4e00\u4e2a\u503c\u4ece\u4e00\u79cd\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u7c7b\u578b\u7684\u5462? \u8f6c\u6362\u6807\u51c6 C++\u8bed\u8a00\u6807\u51c6\u5b9a\u4e49\u4e86\u4e0d\u540c\u7684\u57fa\u672c\u7c7b\u578b(\u4e5f\u5305\u62ec\u4e00\u4e9b\u590d\u5408\u7c7b\u578b)\u5e94\u5f53\u5982\u4f55\u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b\u3002\u8fd9\u4e9b\u8f6c\u6362\u89c4\u5219\u79f0\u4e3a\u8f6c\u6362\u6807\u51c6\u3002 \u8f6c\u6362\u6807\u51c6\u5927\u81f4\u53ef\u5206\u4e3a\u56db\u7c7b\uff0c\u6bcf\u4e00\u7c7b\u6db5\u76d6\u4e0d\u540c\u7684\u8f6c\u6362\u65b9\u5f0f: \u6570\u503c\u63d0\u5347(\u53c2\u8003\uff1a 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 )\uff1b \u6570\u503c\u8f6c\u6362(\u53c2\u8003\uff1a 8.3 - \u6570\u503c\u8f6c\u6362 )\uff1b \u7b97\u6570\u8f6c\u6362(\u53c2\u8003\uff1a 8.4 - \u7b97\u6570\u8f6c\u6362 )\uff1b \u5176\u4ed6\u8f6c\u6362 (\u5305\u62ec\u591a\u79cd\u6307\u9488\u548c\u5f15\u7528\u7684\u8f6c\u6362) \u5f53\u9700\u8981\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u67e5\u770b\u662f\u5426\u6709\u6807\u51c6\u8f6c\u6362\uff0c\u53ef\u4ee5\u7528\u6765\u5c06\u503c\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u7c7b\u578b\u3002\u7f16\u8bd1\u5668\u53ef\u4ee5\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u5e94\u7528 0 \u4e2a\u30011 \u4e2a\u6216\u591a\u4e2a\u6807\u51c6\u8f6c\u6362\u3002 \u9898\u5916\u8bdd \u5982\u4f55\u5728\u4e0d\u8fdb\u884c\u5b9e\u9645\u7c7b\u578b\u8f6c\u6362\u7684\u540c\u65f6\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362? \u4f8b\u5982\uff0c\u5728 int \u548c long \u90fd\u5177\u6709\u76f8\u540c\u5927\u5c0f\u548c\u8303\u56f4\u7684\u67b6\u6784\u4e2d\uff0c\u4f7f\u7528\u76f8\u540c\u7684\u4f4d\u5e8f\u5217\u6765\u8868\u793a\u8fd9\u4e24\u79cd\u7c7b\u578b\u7684\u503c\u3002 \u56e0\u6b64\uff0c\u5728\u8fd9\u4e9b\u7c7b\u578b\u4e4b\u95f4\u8f6c\u6362\u503c\u4e0d\u9700\u8981\u5b9e\u9645\u7684\u8f6c\u6362\u2014\u2014\u503c\u53ef\u4ee5\u7b80\u5355\u5730\u590d\u5236\u3002 \u63cf\u8ff0\u7c7b\u578b\u8f6c\u6362\u5982\u4f55\u5de5\u4f5c\u7684\u4e00\u6574\u5957\u89c4\u5219\u65e2\u5197\u957f\u53c8\u590d\u6742\uff0c\u800c\u4e14\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7c7b\u578b\u8f6c\u6362\u201d\u5c31\u662f\u80fd\u591f\u5de5\u4f5c\u201c\u3002\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u4f60\u9700\u8981\u4e86\u89e3\u7684\u5173\u4e8e\u7c7b\u578b\u8f6c\u6362\u7684\u6700\u91cd\u8981\u7684\u4e8b\u60c5\u3002\u5982\u679c\u4f60\u9700\u8981\u66f4\u8be6\u7ec6\u7684\u8d44\u6599\uff0c\u53ef\u4ee5\u53c2\u8003 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u6280\u672f\u53c2\u8003\u6587\u6863 \u3002","title":"8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362"},{"location":"8-1-Implicit-type-conversion-coercion/#_1","text":"\u5bf9\u8c61\u7684\u503c\u662f\u4ee5\u4e00\u7cfb\u5217\u7684 \u6bd4\u7279 \u5b58\u653e\u7684\uff0c\u800c\u6570\u636e\u7c7b\u578b\u5219\u544a\u8bc9\u7f16\u8bd1\u5668\u5e94\u8be5\u5982\u4f55\u5c06\u8fd9\u4e00\u7cfb\u5217\u6bd4\u7279\u89e3\u6790\u6210\u6709\u610f\u4e49\u7684\u503c\u3002\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u4ee5\u4e0d\u540c\u7684\u65b9\u5f0f\u8868\u793a \u76f8\u540c \u7684\u503c\u3002\u4f8b\u5982\uff0c\u6574\u578b\u503c 3 \u53ef\u80fd\u88ab\u5b58\u653e\u4e3a\u4e8c\u8fdb\u5236 0000 0000 0000 0000 0000 0000 0000 0011 \uff0c\u800c\u6d6e\u70b9\u6570 3.0 \u5219\u53ef\u80fd\u88ab\u5b58\u653e\u4e3a\u4e8c\u8fdb\u5236 0100 0000 0100 0000 0000 0000 0000 0000 \u3002 \u90a3\u4e48\u5f53\u6211\u4eec\u8fd9\u6837\u505a\u7684\u65f6\u5019\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462? 1 float f { 3 }; // initialize floating point variable with int 3 \u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u4e0d\u80fd\u4ec5\u4ec5\u628a\u8868\u793a int \u7c7b\u578b\u503c 3 \u7684\u6bd4\u7279\u62f7\u8d1d\u5230\u5185\u5b58\u4e2d\u8868\u793a float \u53d8\u91cf f \u3002\u76f8\u53cd\uff0c\u5b83\u5fc5\u987b\u5c06\u6574\u578b\u503c 3 \u8f6c\u6362\u4e3a\u7b49\u4ef7\u7684\u6d6e\u70b9\u6570\uff0c\u7136\u540e\u624d\u80fd\u5b58\u653e\u5230\u5185\u5b58\u4e2d\u8868\u793a\u53d8\u91cf f \u3002 \u5c06\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u7684\u8fc7\u7a0b\uff0c\u79f0\u4e3a\u7c7b\u578b\u8f6c\u6362\u3002 \u7c7b\u578b\u8f6c\u6362\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\u89e6\u53d1\uff1a\u9690\u5f0f\u5730\uff08\u7f16\u8bd1\u5668\u9700\u8981\u8fd9\u4e48\u505a\uff09\u6216\u662f\u663e\u5f0f\u5730\uff08\u7531\u7a0b\u5e8f\u5458\u53d1\u8d77\uff09\u3002\u6211\u4eec\u4f1a\u5728\u672c\u8282\u8bfe\u4ecb\u7ecd \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362(implicit type conversion) \uff0c\u7136\u540e\u5728\u4e0b\u8282\u8bfe 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u4e2d\u4ecb\u7ecd \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362(Explicit type conversion) \u3002","title":"\u7c7b\u578b\u8f6c\u6362\u7b80\u4ecb"},{"location":"8-1-Implicit-type-conversion-coercion/#_2","text":"\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\uff08\u4e5f\u53eb\u505a\u81ea\u52a8\u7c7b\u578b\u8f6c\u6362\uff09\u7531\u7f16\u8bd1\u5668\u5728\u9700\u8981\u65f6\uff08\u9700\u8981\u67d0\u4e2a\u53d8\u91cf\u7c7b\u578b\u4f46\u5374\u7ed9\u5b9a\u4e86\u53e6\u5916\u7684\u7c7b\u578b\uff09\u81ea\u52a8\u5b8c\u6210\u3002C++ \u4e2d\u7684\u5927\u90e8\u5206\u7c7b\u578b\u8f6c\u6362\u90fd\u662f\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u3002\u5b83\u4f1a\u53d1\u751f\u5728\u5982\u4e0b\u65f6\u673a\uff1a \u5f53\u4f7f\u7528\u4e0d\u540c\u7c7b\u578b\u7684\u53d8\u91cf\u5bf9\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\u7684\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u6216\u8d4b\u503c\u7684\u65f6\u5019\uff1a 1 2 double d { 3 }; // int 3 \u9690\u5f0f\u8f6c\u6362\u4e3a double \u7c7b\u578b d = 6 ; // int 6 \u9690\u5f0f\u8f6c\u6362\u4e3a double \u7c7b\u578b \u5f53\u51fd\u6570\u7684\u5b9e\u9645\u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u540c\u4e8e\u51fd\u6570\u5b9a\u4e49\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u65f6\uff1a 1 2 3 4 float doSomething () { return 3.0 ; // double 3.0 \u9690\u5f0f\u8f6c\u6362\u4e3a float \u7c7b\u578b } \u5f53\u5bf9\u4e0d\u540c\u7c7b\u578b\u7684\u64cd\u4f5c\u6570\u4f7f\u7528\u67d0\u4e9b\u4e8c\u5143\u64cd\u4f5c\u7b26\u65f6\uff1a 1 double division { 4.0 / 3 }; // int 3 \u9690\u5f0f\u8f6c\u6362\u4e3a double \u7c7b\u578b \u5f53\u5728 if \u8bed\u53e5\u4e2d\u4f7f\u7528\u975e\u5e03\u5c14\u503c\u65f6\uff1a 1 2 3 if ( 5 ) // int 5 \u9690\u5f0f\u8f6c\u6362\u4e3a bool \u7c7b\u578b { } \u5f53\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u5b9e\u53c2\u4e0e\u51fd\u6570\u5f62\u53c2\u7c7b\u578b\u4e0d\u540c\u65f6\uff1a 1 2 3 4 5 void doSomething ( long l ) { } doSomething ( 3 ); // int 3 \u9690\u5f0f\u8f6c\u6362\u4e3a long \u7c7b\u578b","title":"\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362"},{"location":"8-1-Implicit-type-conversion-coercion/#_3","text":"\u5f53\u7c7b\u578b\u8f6c\u6362\u89e6\u53d1\u65f6(\u65e0\u8bba\u662f\u9690\u5f0f\u8fd8\u662f\u663e\u5f0f)\uff0c\u7f16\u8bd1\u5668\u9700\u8981\u786e\u5b9a\u662f\u5426\u53ef\u4ee5\u5c06\u503c\u4ece\u5f53\u524d\u7c7b\u578b\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u7c7b\u578b\u3002\u5982\u679c\u53ef\u4ee5\u627e\u5230\u6709\u6548\u7684\u8f6c\u6362\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c06\u751f\u6210\u6240\u9700\u7c7b\u578b\u7684\u65b0\u503c\u3002\u6ce8\u610f\uff0c\u7c7b\u578b\u8f6c\u6362\u4e0d\u4f1a\u6539\u53d8\u88ab\u8f6c\u6362\u7684\u503c\u6216\u5bf9\u8c61\u7684\u503c\u6216\u7c7b\u578b\u3002 \u5982\u679c\u7f16\u8bd1\u5668\u65e0\u6cd5\u627e\u5230\u53ef\u7528\u7684\u8f6c\u6362\uff0c\u5219\u4f1a\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002\u7c7b\u578b\u8f6c\u6362\u5931\u8d25\u7684\u539f\u56e0\u6709\u5f88\u591a\u3002\u4f8b\u5982\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4e0d\u77e5\u9053\u5982\u4f55\u5728\u539f\u59cb\u7c7b\u578b\u548c\u6240\u9700\u7c7b\u578b\u4e4b\u95f4\u8f6c\u6362\u503c\u3002\u6216\u8005\uff0c\u8bed\u53e5\u53ef\u80fd\u4e0d\u5141\u8bb8\u67d0\u4e9b\u7c7b\u578b\u7684\u8f6c\u6362\u3002\u4f8b\u5982: 1 int x { 3.5 }; // \u62ec\u53f7\u7c7b\u578b\u8f6c\u6362\u4e0d\u5141\u8bb8\u7c7b\u578b\u8f6c\u6362\u4ee5\u907f\u514d\u6570\u636e\u4e22\u5931 \u5373\u4f7f\u7f16\u8bd1\u5668\u77e5\u9053\u5982\u4f55\u5c06 double \u7c7b\u578b\u8f6c\u6362\u4e3a int \u7c7b\u578b\uff0c\u4f46\u5728\u4f7f\u7528\u62ec\u53f7\u521d\u59cb\u5316\u65f6\uff0c\u8fd9\u6837\u7684\u8f6c\u6362\u662f\u4e0d\u5141\u8bb8\u7684\u3002 \u8fd8\u6709\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u65e0\u6cd5\u786e\u5b9a\u51e0\u79cd\u53ef\u80fd\u7684\u7c7b\u578b\u8f6c\u6362\u4e2d\u54ea\u4e00\u79cd\u662f\u6700\u9002\u5408\u4f7f\u7528\u7684\u3002\u6211\u4eec\u4f1a\u5728 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u5339\u914d\u6b67\u4e49 \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u90a3\u4e48\u7f16\u8bd1\u5668\u5b9e\u9645\u4e0a\u662f\u5982\u4f55\u786e\u5b9a\u5b83\u662f\u5426\u53ef\u4ee5\u5c06\u4e00\u4e2a\u503c\u4ece\u4e00\u79cd\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u7c7b\u578b\u7684\u5462?","title":"\u5f53\u7c7b\u578b\u8f6c\u6362\u89e6\u53d1\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48"},{"location":"8-1-Implicit-type-conversion-coercion/#_4","text":"C++\u8bed\u8a00\u6807\u51c6\u5b9a\u4e49\u4e86\u4e0d\u540c\u7684\u57fa\u672c\u7c7b\u578b(\u4e5f\u5305\u62ec\u4e00\u4e9b\u590d\u5408\u7c7b\u578b)\u5e94\u5f53\u5982\u4f55\u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b\u3002\u8fd9\u4e9b\u8f6c\u6362\u89c4\u5219\u79f0\u4e3a\u8f6c\u6362\u6807\u51c6\u3002 \u8f6c\u6362\u6807\u51c6\u5927\u81f4\u53ef\u5206\u4e3a\u56db\u7c7b\uff0c\u6bcf\u4e00\u7c7b\u6db5\u76d6\u4e0d\u540c\u7684\u8f6c\u6362\u65b9\u5f0f: \u6570\u503c\u63d0\u5347(\u53c2\u8003\uff1a 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 )\uff1b \u6570\u503c\u8f6c\u6362(\u53c2\u8003\uff1a 8.3 - \u6570\u503c\u8f6c\u6362 )\uff1b \u7b97\u6570\u8f6c\u6362(\u53c2\u8003\uff1a 8.4 - \u7b97\u6570\u8f6c\u6362 )\uff1b \u5176\u4ed6\u8f6c\u6362 (\u5305\u62ec\u591a\u79cd\u6307\u9488\u548c\u5f15\u7528\u7684\u8f6c\u6362) \u5f53\u9700\u8981\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u67e5\u770b\u662f\u5426\u6709\u6807\u51c6\u8f6c\u6362\uff0c\u53ef\u4ee5\u7528\u6765\u5c06\u503c\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u7c7b\u578b\u3002\u7f16\u8bd1\u5668\u53ef\u4ee5\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u5e94\u7528 0 \u4e2a\u30011 \u4e2a\u6216\u591a\u4e2a\u6807\u51c6\u8f6c\u6362\u3002 \u9898\u5916\u8bdd \u5982\u4f55\u5728\u4e0d\u8fdb\u884c\u5b9e\u9645\u7c7b\u578b\u8f6c\u6362\u7684\u540c\u65f6\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362? \u4f8b\u5982\uff0c\u5728 int \u548c long \u90fd\u5177\u6709\u76f8\u540c\u5927\u5c0f\u548c\u8303\u56f4\u7684\u67b6\u6784\u4e2d\uff0c\u4f7f\u7528\u76f8\u540c\u7684\u4f4d\u5e8f\u5217\u6765\u8868\u793a\u8fd9\u4e24\u79cd\u7c7b\u578b\u7684\u503c\u3002 \u56e0\u6b64\uff0c\u5728\u8fd9\u4e9b\u7c7b\u578b\u4e4b\u95f4\u8f6c\u6362\u503c\u4e0d\u9700\u8981\u5b9e\u9645\u7684\u8f6c\u6362\u2014\u2014\u503c\u53ef\u4ee5\u7b80\u5355\u5730\u590d\u5236\u3002 \u63cf\u8ff0\u7c7b\u578b\u8f6c\u6362\u5982\u4f55\u5de5\u4f5c\u7684\u4e00\u6574\u5957\u89c4\u5219\u65e2\u5197\u957f\u53c8\u590d\u6742\uff0c\u800c\u4e14\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7c7b\u578b\u8f6c\u6362\u201d\u5c31\u662f\u80fd\u591f\u5de5\u4f5c\u201c\u3002\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u4f60\u9700\u8981\u4e86\u89e3\u7684\u5173\u4e8e\u7c7b\u578b\u8f6c\u6362\u7684\u6700\u91cd\u8981\u7684\u4e8b\u60c5\u3002\u5982\u679c\u4f60\u9700\u8981\u66f4\u8be6\u7ec6\u7684\u8d44\u6599\uff0c\u53ef\u4ee5\u53c2\u8003 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u6280\u672f\u53c2\u8003\u6587\u6863 \u3002","title":"\u8f6c\u6362\u6807\u51c6"},{"location":"8-10-Function-overload-differentiation/","tags":["overload"],"text":"Key Takeaway \u51fd\u6570\u4e2d\u7528\u4e8e\u533a\u5206\u91cd\u8f7d\u51fd\u6570\u7684\u90e8\u5206\u6709\uff1a\u5f62\u53c2\u4e2a\u6570\u3001\u5f62\u53c2\u7c7b\u578b\u3001\u7701\u7565\u53f7\u3002 \u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u80fd\u88ab\u7528\u6765\u533a\u5206\u51fd\u6570 \u6210\u5458\u51fd\u6570\u5728\u4e0a\u9762\u7684\u57fa\u7840\u4e0a\u8fd8\u5305\u62ec const \u3001 volatile \u548c\u5f15\u7528\u9650\u5b9a\u7b26 \u5f53\u7f16\u8bd1\u5668\u7f16\u8bd1\u4e00\u4e2a\u51fd\u6570\u65f6\uff0c\u5b83\u4f1a\u6267\u884c \u547d\u540d\u4fee\u9970(Name mangling) \uff0c\u8fd9\u610f\u5473\u7740\u6839\u636e\u5404\u79cd\u6761\u4ef6\u6539\u53d8\u7f16\u8bd1\u540e\u7684\u51fd\u6570\u540d\u79f0(\u201c\u6539\u5199\u201d)\uff0c\u4f8b\u5982\u53c2\u6570\u7684\u6570\u91cf\u548c\u7c7b\u578b\uff0c\u4ee5\u4fbf\u94fe\u63a5\u5668\u6709\u552f\u4e00\u7684\u540d\u79f0\u6765\u5904\u7406\u3002 \u5728\u4e0a\u4e00\u8282\u8bfe\u4e2d ( 8.9 - \u51fd\u6570\u91cd\u8f7d )\uff0c \u6211\u4eec\u5f15\u5165\u4e86\u51fd\u6570\u91cd\u8f7d\u7684\u6982\u5ff5\uff0c\u4f7f\u7528\u51fd\u6570\u91cd\u8f7d\u53ef\u4ee5\u521b\u5efa\u5177\u6709\u76f8\u540c\u540d\u79f0\u7684\u591a\u4e2a\u51fd\u6570\uff0c\u524d\u63d0\u662f\u6bcf\u4e2a\u76f8\u540c\u540d\u79f0\u7684\u51fd\u6570\u5177\u6709\u4e0d\u540c\u7684\u5f62\u53c2\u7c7b\u578b(\u6216\u8005\u53ef\u4ee5\u7528\u5176\u4ed6\u65b9\u5f0f\u533a\u5206\u51fd\u6570)\u3002 \u5728\u8fd9\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u4e86\u89e3\u91cd\u8f7d\u51fd\u6570\u662f\u5982\u4f55\u533a\u5206\u5f00\u6765\u7684\uff0c\u4e0d\u80fd\u88ab\u6b63\u786e\u533a\u5206\u7684\u91cd\u8f7d\u51fd\u6570\u5c06\u5bfc\u81f4\u7f16\u8bd1\u5668\u53d1\u51fa\u7f16\u8bd1\u9519\u8bef\u3002 \u91cd\u8f7d\u51fd\u6570\u662f\u5982\u4f55\u88ab\u533a\u5206\u7684 \u51fd\u6570\u5c5e\u6027 \u88ab\u7528\u4e8e\u533a\u5206\u51fd\u6570 \u5907\u6ce8 \u5f62\u53c2\u4e2a\u6570 Yes \u5f62\u53c2\u7c7b\u578b Yes \u4e0d\u5305\u62ec typedefs \u3001\u7c7b\u578b\u522b\u540d\u3001 const \u9650\u5b9a\u7b26\u3002\u5305\u62ec\u7701\u7565\u53f7(ellipses) \u8fd4\u56de\u503c\u7c7b\u578b No \u6ce8\u610f\uff0c\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u4e0d\u7528\u4e8e\u533a\u5206\u91cd\u8f7d\u51fd\u6570\u3002\u6211\u4eec\u7a0d\u540e\u518d\u8ba8\u8bba\u8fd9\u4e2a\u95ee\u9898\u3002 \u6269\u5c55\u9605\u8bfb \u5bf9\u4e8e \u6210\u5458\u51fd\u6570 \uff0c\u8fd8\u4f1a\u8003\u8651\u9644\u52a0\u7684\u51fd\u6570\u7ea7\u9650\u5b9a\u7b26: \u51fd\u6570\u7ea7\u9650\u5b9a\u7b26 \u662f\u5426\u7528\u4e8e\u91cd\u8f7d const or volatile Yes [[ref-qualifier \u5f15\u7528\u9650\u5b9a\u7b26(Ref-qualifiers)]] \u4f8b\u5982\uff0c const \u6210\u5458\u51fd\u6570\u53ef\u4ee5\u4e0e\u5176\u4ed6\u5b8c\u5168\u76f8\u540c\u7684\u975e const \u6210\u5458\u51fd\u6570\u533a\u522b\u5f00\u6765(\u5373\u4f7f\u5b83\u4eec\u5171\u4eab\u76f8\u540c\u7684\u5f62\u53c2\u96c6)\u3002 \u57fa\u4e8e\u53c2\u6570\u4e2a\u6570\u8fdb\u884c\u91cd\u8f7d \u53ea\u8981\u6bcf\u4e2a\u91cd\u8f7d\u51fd\u6570\u5177\u6709\u4e0d\u540c\u6570\u91cf\u7684\u5f62\u53c2\uff0c\u5c31\u53ef\u4ee5\u533a\u5206\u91cd\u8f7d\u51fd\u6570\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 int add ( int x , int y ) { return x + y ; } int add ( int x , int y , int z ) { return x + y + z ; } \u7f16\u8bd1\u5668\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5c06\u4e24\u4e2a\u6574\u578b\u53c2\u6570\u7684\u51fd\u6570\u8c03\u7528\u5339\u914d\u5230 add(int, int) \uff0c\u800c\u5c06\u4e09\u4e2a\u6574\u578b\u53c2\u6570\u7684\u51fd\u6570\u8c03\u7528\u5339\u914d\u5230 add(int, int, int) \u3002 \u57fa\u4e8e\u53c2\u6570\u7c7b\u578b\u8fdb\u884c\u91cd\u8f7d \u53ea\u8981\u6bcf\u4e2a\u91cd\u8f7d\u51fd\u6570\u7684\u5f62\u53c2\u7c7b\u578b\u5217\u8868\u662f\u4e0d\u540c\u7684\uff0c\u5c31\u53ef\u4ee5\u5bf9\u51fd\u6570\u8fdb\u884c\u533a\u5206\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u6240\u6709\u7684\u91cd\u8f7d\u51fd\u6570\u90fd\u662f\u53ef\u4ee5\u88ab\u533a\u5206\u7684: 1 2 3 4 int add ( int x , int y ); // \u6574\u578b\u7248\u672c double add ( double x , double y ); // \u6d6e\u70b9\u7248\u672c double add ( int x , double y ); // \u6df7\u5408\u7248\u672c double add ( double x , int y ); // \u6df7\u5408\u7248\u672c \u56e0\u4e3a\u7c7b\u578b\u522b\u540d(\u6216 typedefs )\u5e76\u4e0d\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u7c7b\u578b\uff0c\u6240\u4ee5\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u7684\u91cd\u8f7d\u51fd\u6570\u4e0e\u4f7f\u7528\u5bf9\u5e94\u7c7b\u578b\u7684\u91cd\u8f7d\u6ca1\u6709\u533a\u522b\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u6240\u6709\u91cd\u8f7d\u90fd\u662f\u4e0d\u80fd\u88ab\u533a\u5206\u7684(\u5e76\u5c06\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef)\uff1a 1 2 3 4 5 6 typedef int height_t ; // typedef using age_t = int ; // type alias void print ( int value ); void print ( age_t value ); // not differentiated from print(int) void print ( height_t value ); // not differentiated from print(int) \u5bf9\u4e8e\u901a\u8fc7\u503c\u4f20\u9012\u7684\u53c2\u6570\uff0c\u4e5f\u4e0d\u8003\u8651 const \u9650\u5b9a\u7b26\u3002\u56e0\u6b64\uff0c\u4ee5\u4e0b\u51fd\u6570\u662f\u4e0d\u80fd\u88ab\u533a\u5206\u5f00\u7684\uff1a 1 2 void print ( int ); void print ( const int ); // not differentiated from print(int) \u6269\u5c55\u9605\u8bfb \u6211\u4eec\u8fd8\u6ca1\u6709\u6d89\u53ca\u5230\u7701\u7565\u53f7\uff0c\u4f46\u662f\u7701\u7565\u53f7\u53c2\u6570\u88ab\u8ba4\u4e3a\u662f\u4e00\u79cd\u72ec\u7279\u7684\u53c2\u6570\u7c7b\u578b: 1 2 void foo ( int x , int y ); void foo ( int x , ...); // \u548c foo(int, int)\u662f\u4e0d\u540c\u7684 \u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u80fd\u88ab\u7528\u6765\u533a\u5206\u51fd\u6570 \u5728\u533a\u5206\u91cd\u8f7d\u51fd\u6570\u65f6\uff0c\u4e0d\u8003\u8651\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u3002 \u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5:\u60a8\u60f3\u7f16\u5199\u4e00\u4e2a\u8fd4\u56de\u968f\u673a\u6570\u7684\u51fd\u6570\uff0c\u4f46\u662f\u60a8\u9700\u8981\u4e00\u4e2a\u8fd4\u56de\u6574\u6570\u7684\u7248\u672c\uff0c\u4ee5\u53ca\u53e6\u4e00\u4e2a\u8fd4\u56de\u53cc\u7cbe\u5ea6\u6570\u7684\u7248\u672c\u3002\u4f60\u53ef\u80fd\u4f1a\u5fcd\u4e0d\u4f4f\u8fd9\u6837\u505a: 1 2 int getRandomValue (); double getRandomValue (); Visual Studio 2019 \u4e2d\u4f1a\u4ea7\u751f\u5982\u4e0b\u7f16\u8bd1\u9519\u8bef\uff1a 1 error C2556: 'double getRandomValue(void)': overloaded function differs only by return type from 'int getRandomValue(void)' \u8fd9\u662f\u5f88\u663e\u7136\u7684\uff0c\u5047\u8bbe\u4f60\u81ea\u5df1\u662f\u7f16\u8bd1\u5668\uff0c\u5f53\u4f60\u770b\u5230\u4e0b\u9762\u7684\u5b9a\u4e49\u65f6\uff1a 1 getRandomValue (); \u4f60\u5e94\u8be5\u8c03\u7528\u4e24\u4e2a\u51fd\u6570\u4e2d\u7684\u54ea\u4e2a\u5462\uff1f \u9898\u5916\u8bdd \u8fd9\u662f\u4e00\u4e2a\u6709\u610f\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u786e\u4fdd\u4e86\u51fd\u6570\u8c03\u7528\u7684\u884c\u4e3a\u53ef\u4ee5\u72ec\u7acb\u4e8e\u8868\u8fbe\u5f0f\u7684\u5176\u4ed6\u90e8\u5206\u6765\u786e\u5b9a\uff0c\u4ece\u800c\u4f7f\u7406\u89e3\u590d\u6742\u8868\u8fbe\u5f0f\u66f4\u52a0\u7b80\u5355\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u603b\u662f\u53ef\u4ee5\u4ec5\u6839\u636e\u51fd\u6570\u8c03\u7528\u4e2d\u7684\u53c2\u6570\u6765\u51b3\u5b9a\u8c03\u7528\u54ea\u4e2a\u7248\u672c\u7684\u51fd\u6570\u3002\u5982\u679c\u8fd4\u56de\u503c\u7528\u4e8e\u533a\u5206\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u6ca1\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u8bed\u6cd5\u65b9\u6cd5\u6765\u5224\u65ad\u8c03\u7528\u4e86\u54ea\u4e2a\u51fd\u6570\u7684\u91cd\u8f7d\u2014\u2014\u6211\u4eec\u8fd8\u5fc5\u987b\u7406\u89e3\u8fd4\u56de\u503c\u662f\u5982\u4f55\u88ab\u4f7f\u7528\u7684\uff0c\u8fd9\u9700\u8981\u66f4\u591a\u7684\u5206\u6790\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u6700\u597d\u65b9\u6cd5\u662f\u7ed9\u51fd\u6570\u53d6\u4e0d\u540c\u7684\u540d\u5b57\uff1a 1 2 int getRandomInt (); double getRandomDouble (); \u7c7b\u578b\u7b7e\u540d \u51fd\u6570\u7684 \u7c7b\u578b\u7b7e\u540d(type signature) (\u901a\u5e38\u7b80\u79f0\u4e3a\u7b7e\u540d) \u662f\u51fd\u6570\u5934\u7684\u4e00\u90e8\u5206\uff0c\u88ab\u7528\u4e8e\u533a\u5206\u51fd\u6570\u3002\u5728 C++ \u4e2d\uff0c\u7b7e\u540d\u5305\u62ec\u51fd\u6570\u540d\u3001\u53c2\u6570\u6570\u91cf\u3001\u53c2\u6570\u7c7b\u578b\u548c\u51fd\u6570\u7ea7\u7684\u9650\u5b9a\u7b26\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5b83\u4e0d\u5305\u62ec\u8fd4\u56de\u7c7b\u578b\u3002 \u547d\u540d\u4fee\u9970 \u9898\u5916\u8bdd \u5f53\u7f16\u8bd1\u5668\u7f16\u8bd1\u4e00\u4e2a\u51fd\u6570\u65f6\uff0c\u5b83\u4f1a\u6267\u884c \u547d\u540d\u4fee\u9970(Name mangling) \uff0c\u8fd9\u610f\u5473\u7740\u6839\u636e\u5404\u79cd\u6761\u4ef6\u6539\u53d8\u7f16\u8bd1\u540e\u7684\u51fd\u6570\u540d\u79f0(\u201c\u6539\u5199\u201d)\uff0c\u4f8b\u5982\u53c2\u6570\u7684\u6570\u91cf\u548c\u7c7b\u578b\uff0c\u4ee5\u4fbf\u94fe\u63a5\u5668\u6709\u552f\u4e00\u7684\u540d\u79f0\u6765\u5904\u7406\u3002 \u4f8b\u5982\uff0c\u4e00\u4e9b\u539f\u578b\u4e3a int fcn() \u7684\u51fd\u6570\u53ef\u80fd\u4f1a\u88ab\u7f16\u8bd1\u4e3a __fcn_v \uff0c\u800c int fcn(int) \u53ef\u80fd\u4f1a\u88ab\u7f16\u8bd1\u4e3a __fcn_i \u3002\u56e0\u6b64\uff0c\u867d\u7136\u5728\u6e90\u4ee3\u7801\u4e2d\uff0c\u4e24\u4e2a\u91cd\u8f7d\u51fd\u6570\u5171\u4eab\u4e00\u4e2a\u540d\u79f0\uff0c\u4f46\u5728\u7f16\u8bd1\u4ee3\u7801\u4e2d\uff0c\u5b83\u4eec\u7684\u540d\u79f0\u5b9e\u9645\u4e0a\u662f\u552f\u4e00\u7684\u3002 \u6807\u51c6\u6ca1\u6709\u89c4\u5b9a\u540d\u79f0\u5e94\u8be5\u5982\u4f55\u88ab\u4fee\u9970\uff0c\u56e0\u6b64\u4e0d\u540c\u7684\u7f16\u8bd1\u5668\u5c06\u4ea7\u751f\u4e0d\u540c\u7684\u51fd\u6570\u540d\u79f0\u3002","title":"8.10 - \u91cd\u8f7d\u51fd\u6570\u7684\u533a\u5206"},{"location":"8-10-Function-overload-differentiation/#_1","text":"\u51fd\u6570\u5c5e\u6027 \u88ab\u7528\u4e8e\u533a\u5206\u51fd\u6570 \u5907\u6ce8 \u5f62\u53c2\u4e2a\u6570 Yes \u5f62\u53c2\u7c7b\u578b Yes \u4e0d\u5305\u62ec typedefs \u3001\u7c7b\u578b\u522b\u540d\u3001 const \u9650\u5b9a\u7b26\u3002\u5305\u62ec\u7701\u7565\u53f7(ellipses) \u8fd4\u56de\u503c\u7c7b\u578b No \u6ce8\u610f\uff0c\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u4e0d\u7528\u4e8e\u533a\u5206\u91cd\u8f7d\u51fd\u6570\u3002\u6211\u4eec\u7a0d\u540e\u518d\u8ba8\u8bba\u8fd9\u4e2a\u95ee\u9898\u3002 \u6269\u5c55\u9605\u8bfb \u5bf9\u4e8e \u6210\u5458\u51fd\u6570 \uff0c\u8fd8\u4f1a\u8003\u8651\u9644\u52a0\u7684\u51fd\u6570\u7ea7\u9650\u5b9a\u7b26: \u51fd\u6570\u7ea7\u9650\u5b9a\u7b26 \u662f\u5426\u7528\u4e8e\u91cd\u8f7d const or volatile Yes [[ref-qualifier \u5f15\u7528\u9650\u5b9a\u7b26(Ref-qualifiers)]] \u4f8b\u5982\uff0c const \u6210\u5458\u51fd\u6570\u53ef\u4ee5\u4e0e\u5176\u4ed6\u5b8c\u5168\u76f8\u540c\u7684\u975e const \u6210\u5458\u51fd\u6570\u533a\u522b\u5f00\u6765(\u5373\u4f7f\u5b83\u4eec\u5171\u4eab\u76f8\u540c\u7684\u5f62\u53c2\u96c6)\u3002","title":"\u91cd\u8f7d\u51fd\u6570\u662f\u5982\u4f55\u88ab\u533a\u5206\u7684"},{"location":"8-10-Function-overload-differentiation/#_2","text":"\u53ea\u8981\u6bcf\u4e2a\u91cd\u8f7d\u51fd\u6570\u5177\u6709\u4e0d\u540c\u6570\u91cf\u7684\u5f62\u53c2\uff0c\u5c31\u53ef\u4ee5\u533a\u5206\u91cd\u8f7d\u51fd\u6570\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 int add ( int x , int y ) { return x + y ; } int add ( int x , int y , int z ) { return x + y + z ; } \u7f16\u8bd1\u5668\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5c06\u4e24\u4e2a\u6574\u578b\u53c2\u6570\u7684\u51fd\u6570\u8c03\u7528\u5339\u914d\u5230 add(int, int) \uff0c\u800c\u5c06\u4e09\u4e2a\u6574\u578b\u53c2\u6570\u7684\u51fd\u6570\u8c03\u7528\u5339\u914d\u5230 add(int, int, int) \u3002","title":"\u57fa\u4e8e\u53c2\u6570\u4e2a\u6570\u8fdb\u884c\u91cd\u8f7d"},{"location":"8-10-Function-overload-differentiation/#_3","text":"\u53ea\u8981\u6bcf\u4e2a\u91cd\u8f7d\u51fd\u6570\u7684\u5f62\u53c2\u7c7b\u578b\u5217\u8868\u662f\u4e0d\u540c\u7684\uff0c\u5c31\u53ef\u4ee5\u5bf9\u51fd\u6570\u8fdb\u884c\u533a\u5206\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u6240\u6709\u7684\u91cd\u8f7d\u51fd\u6570\u90fd\u662f\u53ef\u4ee5\u88ab\u533a\u5206\u7684: 1 2 3 4 int add ( int x , int y ); // \u6574\u578b\u7248\u672c double add ( double x , double y ); // \u6d6e\u70b9\u7248\u672c double add ( int x , double y ); // \u6df7\u5408\u7248\u672c double add ( double x , int y ); // \u6df7\u5408\u7248\u672c \u56e0\u4e3a\u7c7b\u578b\u522b\u540d(\u6216 typedefs )\u5e76\u4e0d\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u7c7b\u578b\uff0c\u6240\u4ee5\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u7684\u91cd\u8f7d\u51fd\u6570\u4e0e\u4f7f\u7528\u5bf9\u5e94\u7c7b\u578b\u7684\u91cd\u8f7d\u6ca1\u6709\u533a\u522b\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u6240\u6709\u91cd\u8f7d\u90fd\u662f\u4e0d\u80fd\u88ab\u533a\u5206\u7684(\u5e76\u5c06\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef)\uff1a 1 2 3 4 5 6 typedef int height_t ; // typedef using age_t = int ; // type alias void print ( int value ); void print ( age_t value ); // not differentiated from print(int) void print ( height_t value ); // not differentiated from print(int) \u5bf9\u4e8e\u901a\u8fc7\u503c\u4f20\u9012\u7684\u53c2\u6570\uff0c\u4e5f\u4e0d\u8003\u8651 const \u9650\u5b9a\u7b26\u3002\u56e0\u6b64\uff0c\u4ee5\u4e0b\u51fd\u6570\u662f\u4e0d\u80fd\u88ab\u533a\u5206\u5f00\u7684\uff1a 1 2 void print ( int ); void print ( const int ); // not differentiated from print(int) \u6269\u5c55\u9605\u8bfb \u6211\u4eec\u8fd8\u6ca1\u6709\u6d89\u53ca\u5230\u7701\u7565\u53f7\uff0c\u4f46\u662f\u7701\u7565\u53f7\u53c2\u6570\u88ab\u8ba4\u4e3a\u662f\u4e00\u79cd\u72ec\u7279\u7684\u53c2\u6570\u7c7b\u578b: 1 2 void foo ( int x , int y ); void foo ( int x , ...); // \u548c foo(int, int)\u662f\u4e0d\u540c\u7684","title":"\u57fa\u4e8e\u53c2\u6570\u7c7b\u578b\u8fdb\u884c\u91cd\u8f7d"},{"location":"8-10-Function-overload-differentiation/#_4","text":"\u5728\u533a\u5206\u91cd\u8f7d\u51fd\u6570\u65f6\uff0c\u4e0d\u8003\u8651\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u3002 \u8003\u8651\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5:\u60a8\u60f3\u7f16\u5199\u4e00\u4e2a\u8fd4\u56de\u968f\u673a\u6570\u7684\u51fd\u6570\uff0c\u4f46\u662f\u60a8\u9700\u8981\u4e00\u4e2a\u8fd4\u56de\u6574\u6570\u7684\u7248\u672c\uff0c\u4ee5\u53ca\u53e6\u4e00\u4e2a\u8fd4\u56de\u53cc\u7cbe\u5ea6\u6570\u7684\u7248\u672c\u3002\u4f60\u53ef\u80fd\u4f1a\u5fcd\u4e0d\u4f4f\u8fd9\u6837\u505a: 1 2 int getRandomValue (); double getRandomValue (); Visual Studio 2019 \u4e2d\u4f1a\u4ea7\u751f\u5982\u4e0b\u7f16\u8bd1\u9519\u8bef\uff1a 1 error C2556: 'double getRandomValue(void)': overloaded function differs only by return type from 'int getRandomValue(void)' \u8fd9\u662f\u5f88\u663e\u7136\u7684\uff0c\u5047\u8bbe\u4f60\u81ea\u5df1\u662f\u7f16\u8bd1\u5668\uff0c\u5f53\u4f60\u770b\u5230\u4e0b\u9762\u7684\u5b9a\u4e49\u65f6\uff1a 1 getRandomValue (); \u4f60\u5e94\u8be5\u8c03\u7528\u4e24\u4e2a\u51fd\u6570\u4e2d\u7684\u54ea\u4e2a\u5462\uff1f \u9898\u5916\u8bdd \u8fd9\u662f\u4e00\u4e2a\u6709\u610f\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u786e\u4fdd\u4e86\u51fd\u6570\u8c03\u7528\u7684\u884c\u4e3a\u53ef\u4ee5\u72ec\u7acb\u4e8e\u8868\u8fbe\u5f0f\u7684\u5176\u4ed6\u90e8\u5206\u6765\u786e\u5b9a\uff0c\u4ece\u800c\u4f7f\u7406\u89e3\u590d\u6742\u8868\u8fbe\u5f0f\u66f4\u52a0\u7b80\u5355\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u603b\u662f\u53ef\u4ee5\u4ec5\u6839\u636e\u51fd\u6570\u8c03\u7528\u4e2d\u7684\u53c2\u6570\u6765\u51b3\u5b9a\u8c03\u7528\u54ea\u4e2a\u7248\u672c\u7684\u51fd\u6570\u3002\u5982\u679c\u8fd4\u56de\u503c\u7528\u4e8e\u533a\u5206\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u6ca1\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u8bed\u6cd5\u65b9\u6cd5\u6765\u5224\u65ad\u8c03\u7528\u4e86\u54ea\u4e2a\u51fd\u6570\u7684\u91cd\u8f7d\u2014\u2014\u6211\u4eec\u8fd8\u5fc5\u987b\u7406\u89e3\u8fd4\u56de\u503c\u662f\u5982\u4f55\u88ab\u4f7f\u7528\u7684\uff0c\u8fd9\u9700\u8981\u66f4\u591a\u7684\u5206\u6790\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u6700\u597d\u65b9\u6cd5\u662f\u7ed9\u51fd\u6570\u53d6\u4e0d\u540c\u7684\u540d\u5b57\uff1a 1 2 int getRandomInt (); double getRandomDouble ();","title":"\u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u80fd\u88ab\u7528\u6765\u533a\u5206\u51fd\u6570"},{"location":"8-10-Function-overload-differentiation/#_5","text":"\u51fd\u6570\u7684 \u7c7b\u578b\u7b7e\u540d(type signature) (\u901a\u5e38\u7b80\u79f0\u4e3a\u7b7e\u540d) \u662f\u51fd\u6570\u5934\u7684\u4e00\u90e8\u5206\uff0c\u88ab\u7528\u4e8e\u533a\u5206\u51fd\u6570\u3002\u5728 C++ \u4e2d\uff0c\u7b7e\u540d\u5305\u62ec\u51fd\u6570\u540d\u3001\u53c2\u6570\u6570\u91cf\u3001\u53c2\u6570\u7c7b\u578b\u548c\u51fd\u6570\u7ea7\u7684\u9650\u5b9a\u7b26\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5b83\u4e0d\u5305\u62ec\u8fd4\u56de\u7c7b\u578b\u3002","title":"\u7c7b\u578b\u7b7e\u540d"},{"location":"8-10-Function-overload-differentiation/#_6","text":"\u9898\u5916\u8bdd \u5f53\u7f16\u8bd1\u5668\u7f16\u8bd1\u4e00\u4e2a\u51fd\u6570\u65f6\uff0c\u5b83\u4f1a\u6267\u884c \u547d\u540d\u4fee\u9970(Name mangling) \uff0c\u8fd9\u610f\u5473\u7740\u6839\u636e\u5404\u79cd\u6761\u4ef6\u6539\u53d8\u7f16\u8bd1\u540e\u7684\u51fd\u6570\u540d\u79f0(\u201c\u6539\u5199\u201d)\uff0c\u4f8b\u5982\u53c2\u6570\u7684\u6570\u91cf\u548c\u7c7b\u578b\uff0c\u4ee5\u4fbf\u94fe\u63a5\u5668\u6709\u552f\u4e00\u7684\u540d\u79f0\u6765\u5904\u7406\u3002 \u4f8b\u5982\uff0c\u4e00\u4e9b\u539f\u578b\u4e3a int fcn() \u7684\u51fd\u6570\u53ef\u80fd\u4f1a\u88ab\u7f16\u8bd1\u4e3a __fcn_v \uff0c\u800c int fcn(int) \u53ef\u80fd\u4f1a\u88ab\u7f16\u8bd1\u4e3a __fcn_i \u3002\u56e0\u6b64\uff0c\u867d\u7136\u5728\u6e90\u4ee3\u7801\u4e2d\uff0c\u4e24\u4e2a\u91cd\u8f7d\u51fd\u6570\u5171\u4eab\u4e00\u4e2a\u540d\u79f0\uff0c\u4f46\u5728\u7f16\u8bd1\u4ee3\u7801\u4e2d\uff0c\u5b83\u4eec\u7684\u540d\u79f0\u5b9e\u9645\u4e0a\u662f\u552f\u4e00\u7684\u3002 \u6807\u51c6\u6ca1\u6709\u89c4\u5b9a\u540d\u79f0\u5e94\u8be5\u5982\u4f55\u88ab\u4fee\u9970\uff0c\u56e0\u6b64\u4e0d\u540c\u7684\u7f16\u8bd1\u5668\u5c06\u4ea7\u751f\u4e0d\u540c\u7684\u51fd\u6570\u540d\u79f0\u3002","title":"\u547d\u540d\u4fee\u9970"},{"location":"8-11-Function-overload-resolution-and-ambiguous-matches/","tags":["overload"],"text":"Key Takeaway \u51fd\u6570\u8c03\u7528\u65f6\u5339\u914d\u91cd\u8f7d\u51fd\u6570\u7684\u8fc7\u7a0b\u53eb\u505a\u91cd\u8f7d\u89e3\u6790\uff0c\u91cd\u8f7d\u89e3\u6790\u5206\u4e3a 6 \u4e2a\u6b65\u9aa4\uff1a \u5b8c\u5168\u5339\u914d \u6570\u503c\u63d0\u5347\u5339\u914d \u6570\u503c\u8f6c\u6362\u5339\u914d \u81ea\u5b9a\u4e49\u7c7b\u578b\u8f6c\u6362\u5339\u914d \u7701\u7565\u53f7\u5339\u914d \u653e\u5f03\u5e76\u62a5\u9519 \u5728\u6bcf\u4e00\u6b65\u4e2d\uff0c\u5982\u679c\u627e\u5230\u4e14\u53ea\u627e\u5230\u4e00\u4e2a\u5339\u914d\u51fd\u6570\uff0c\u5219\u5b8c\u6210\u5339\u914d\uff0c\u5426\u5219\u8fdb\u5165\u4e0b\u4e00\u6b65\u3002\u5982\u679c\u6700\u7ec8\u5339\u914d\u7684\u7ed3\u679c\u5927\u4e8e 1 \u4e2a\u6216\u8005\u4e3a 0\uff0c\u5219\u62a5\u9519\u3002 \u901a\u8fc7\u6570\u503c\u63d0\u5347\u8fdb\u884c\u5339\u914d\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e\u901a\u8fc7\u6570\u503c\u8f6c\u6362\u5339\u914d\u7684\u4f18\u5148\u7ea7 \u5c06\u975e\u5f15\u7528\u7c7b\u578b\u8f6c\u6362\u4e3a\u5f15\u7528\u7c7b\u578b\uff08\u6216\u8005\u53cd\u8fc7\u6765\uff09\u4e5f\u5c5e\u4e8e \u7b80\u5355\u8f6c\u6362 \u5982\u679c\u7f16\u8bd1\u5668\u5728\u67d0\u4e00\u6b65\u627e\u5230\u4e86\u591a\u4e2a\u53ef\u80fd\u7684\u5339\u914d\uff0c\u5219\u4f1a\u5bfc\u81f4 \u4e0d\u660e\u786e\u5339\u914d \u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u8fd9\u4e00\u6b65\u4e2d\u7f16\u8bd1\u5668\u6ca1\u80fd\u627e\u5230\u4e00\u4e2a\u6bd4\u5176\u4ed6\u5339\u914d\u90fd\u597d\u7684\u5339\u914d\u3002\u6ce8\u610f\uff0c\u5728\u6bcf\u4e00\u6b65\u4e2d\u7f16\u8bd1\u5668\u90fd\u4f1a\u5e94\u7528\u6240\u6709\u53ef\u7528\u7684\u8f6c\u6362\u5e76\u8fdb\u884c\u5339\u914d\uff0c\u4e0d\u4f1a\u627e\u5230\u4e00\u4e2a\u5339\u914d\u540e\u5c31\u505c\u6b62\u3002 \u89e3\u51b3\u4e0d\u660e\u786e\u5339\u914d\u7684\u65b9\u6cd5\u662f\uff1a \u5b9a\u4e49\u4e00\u4e2a\u80fd\u591f\u53c2\u6570\u5b8c\u5168\u5339\u914d\u7684\u65b0\u51fd\u6570 \u5f3a\u5236\u7c7b\u578b\u8f6c\u6362 \u5b57\u9762\u91cf\u6dfb\u52a0\u540e\u7f00 \u5728\u4e0a\u8282\u8bfe( 8.10 - \u51fd\u6570\u91cd\u8f7d\u548c\u533a\u5206 )\u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u51fd\u6570\u4e2d\u53ef\u4ee5\u88ab\u7528\u6765\u5bf9\u91cd\u8f7d\u51fd\u6570\u8fdb\u884c\u533a\u5206\u7684\u5c5e\u6027\u3002\u5982\u679c\u4e00\u4e2a\u91cd\u8f7d\u51fd\u6570\u4e0d\u80fd\u548c\u5176\u4ed6\u91cd\u8f7d\u51fd\u6570\u88ab\u533a\u5206\u5f00\u6765\uff0c\u90a3\u4e48\u5c31\u4f1a\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002 \u4e0d\u8fc7\uff0c\u62e5\u6709\u4e00\u7ec4\u80fd\u591f\u88ab\u533a\u5206\u7684\u91cd\u8f7d\u51fd\u6570\u53ea\u662f\u89e3\u51b3\u4e86\u4e00\u534a\u7684\u95ee\u9898\u3002\u5f53\u8c03\u7528\u4efb\u4f55\u51fd\u6570\u65f6\uff0c\u7f16\u8bd1\u5668\u8fd8\u5fc5\u987b\u786e\u4fdd\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\u58f0\u660e\u3002 \u5bf9\u4e8e\u975e\u91cd\u8f7d\u51fd\u6570(\u5177\u6709\u60df\u4e00\u540d\u79f0\u7684\u51fd\u6570)\uff0c\u53ea\u6709\u4e00\u4e2a\u51fd\u6570\u53ef\u80fd\u4e0e\u51fd\u6570\u8c03\u7528\u5339\u914d\u3002\u8be5\u51fd\u6570\u8981\u4e48\u5339\u914d(\u6216\u8005\u53ef\u4ee5\u5728\u5e94\u7528\u7c7b\u578b\u8f6c\u6362\u540e\u5339\u914d)\uff0c\u8981\u4e48\u4e0d\u5339\u914d(\u7ed3\u679c\u662f\u7f16\u8bd1\u9519\u8bef)\u3002\u5bf9\u4e8e\u91cd\u8f7d\u51fd\u6570\uff0c\u53ef\u4ee5\u6709\u8bb8\u591a\u51fd\u6570\u53ef\u80fd\u4e0e\u51fd\u6570\u8c03\u7528\u5339\u914d\u3002\u7531\u4e8e\u51fd\u6570\u8c03\u7528\u53ea\u80fd\u89e3\u6790\u5230\u5176\u4e2d\u4e00\u4e2a\u51fd\u6570\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u5fc5\u987b\u786e\u5b9a\u54ea\u4e2a\u91cd\u8f7d\u51fd\u6570\u662f\u6700\u4f73\u5339\u914d\u7684\u3002\u5c06\u51fd\u6570\u8c03\u7528\u5339\u914d\u5230\u7279\u5b9a\u91cd\u8f7d\u51fd\u6570\u7684\u8fc7\u7a0b\u79f0\u4e3a \u91cd\u8f7d\u89e3\u6790 \u3002 \u5728\u51fd\u6570 \u5b9e\u53c2\u7c7b\u578b \u548c\u51fd\u6570 \u5f62\u53c2\u7c7b\u578b \u5b8c\u5168\u5339\u914d\u7684\u7b80\u5355\u60c5\u51b5\u4e0b\uff0c\u91cd\u8f7d\u51fd\u6570\u7684\u5339\u914d(\u901a\u5e38)\u662f\u5f88\u7b80\u5355\u7684: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void print ( int x ) { std :: cout << x ; } void print ( double d ) { std :: cout << d ; } int main () { print ( 5 ); // 5 is an int, so this matches print(int) print ( 6.7 ); // 6.7 is a double, so this matches print(double) return 0 ; } \u4f46\u662f\uff0c\u5982\u679c\u51fd\u6570\u8c03\u7528\u4e2d\u7684\u53c2\u6570\u7c7b\u578b\u4e0e\u91cd\u8f7d\u51fd\u6570\u4e2d\u7684\u53c2\u6570\u7c7b\u578b \u4e0d\u5b8c\u5168\u5339\u914d \uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void print ( int x ) { std :: cout << x ; } void print ( double d ) { std :: cout << d ; } int main () { print ( 'a' ); // char does not match int or double print ( 5L ); // long does not match int or double return 0 ; } \u6ca1\u6709\u5b8c\u5168\u5339\u914d\u7684\u51fd\u6570\uff0c\u4e0d\u7b49\u4e8e\u6ca1\u6709\u80fd\u591f\u5339\u914d\u7684\u51fd\u6570\u2014\u2014\u6bd5\u7adf\uff0c char \u6216\u8005 long \u53ef\u4ee5\u901a\u8fc7 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u8f6c\u6362\u4e3a int \u6216\u8005 double \u3002\u4f46\u662f\uff0c\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u5982\u4f55\u9009\u62e9 \u6700\u4f73 \u5339\u914d\u5462\uff1f \u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u63a2\u7d22\u7f16\u8bd1\u5668\u662f\u5982\u4f55\u5728\u51fd\u6570\u8c03\u7528\u65f6\u5339\u914d\u7684\u91cd\u8f7d\u51fd\u6570\u7684\u3002 \u89e3\u6790\u91cd\u8f7d\u51fd\u6570\u8c03\u7528 \u5f53\u5bf9\u91cd\u8f7d\u51fd\u6570\u8fdb\u884c\u8c03\u7528\u65f6\uff0c\u7f16\u8bd1\u5668\u901a\u8fc7\u4e00\u7cfb\u5217\u89c4\u5219\u548c\u6b65\u9aa4\u786e\u5b9a\u91cd\u8f7d\u51fd\u6570\u7684\u6700\u4f73\u5339\u914d(\u5982\u679c\u6709\u7684\u8bdd)\u3002 \u5728\u6bcf\u4e2a\u6b65\u9aa4\u4e2d\uff0c\u7f16\u8bd1\u5668\u5bf9\u51fd\u6570\u8c03\u7528\u4e2d\u7684\u5b9e\u53c2\u5e94\u7528\u4e00\u7cfb\u5217\u4e0d\u540c\u7684\u7c7b\u578b\u8f6c\u6362\u3002\u5bf9\u4e8e\u5e94\u7528\u7684\u6bcf\u4e2a\u8f6c\u6362\uff0c\u7f16\u8bd1\u5668\u68c0\u67e5\u91cd\u8f7d\u7684\u51fd\u6570\u73b0\u5728\u662f\u5426\u5339\u914d\u3002\u5728\u5e94\u7528\u4e86\u6240\u6709\u4e0d\u540c\u7684\u7c7b\u578b\u8f6c\u6362\u5e76\u68c0\u67e5\u4e86\u5339\u914d\u4e4b\u540e\uff0c\u8fd9\u4e00\u6b65\u5c31\u5b8c\u6210\u4e86\u3002\u7ed3\u679c\u5c06\u662f\u4ee5\u4e0b\u4e09\u79cd\u53ef\u80fd\u7684\u7ed3\u679c\u4e4b\u4e00\uff1a \u672a\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\u3002\u7f16\u8bd1\u5668\u8fdb\u5165\u4e0b\u4e00\u4e2a\u6b65\u9aa4\u3002 \u627e\u5230\u5355\u4e2a\u5339\u914d\u51fd\u6570\u3002\u8be5\u51fd\u6570\u88ab\u8ba4\u4e3a\u662f\u6700\u4f73\u5339\u914d\u3002\u5339\u914d\u8fc7\u7a0b\u73b0\u5728\u5df2\u7ecf\u5b8c\u6210\uff0c\u540e\u7eed\u6b65\u9aa4\u4e0d\u518d\u6267\u884c\u3002 \u53d1\u73b0\u591a\u4e2a\u5339\u914d\u51fd\u6570\u3002\u7f16\u8bd1\u5668\u5c06\u53d1\u51fa\u4e00\u4e2a \u4e0d\u660e\u786e\u5339\u914d \u7684\u7f16\u8bd1\u9519\u8bef\u3002\u6211\u4eec\u7a0d\u540e\u5c06\u8fdb\u4e00\u6b65\u8ba8\u8bba\u8fd9\u79cd\u60c5\u51b5\u3002 \u5982\u679c\u7f16\u8bd1\u5668\u6267\u884c\u5b8c\u4e00\u7cfb\u5217\u6b65\u9aa4\u540e\uff0c\u4ecd\u7136\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\uff0c\u5b83\u5c06\u751f\u6210\u4e00\u4e2a\u7f16\u8bd1\u9519\u8bef\uff0c\u5373\u65e0\u6cd5\u4e3a\u51fd\u6570\u8c03\u7528\u627e\u5230\u5339\u914d\u7684\u91cd\u8f7d\u51fd\u6570\u3002 \u53c2\u6570\u5339\u914d\u987a\u5e8f \u7b2c\u4e00\u6b65\uff1a\u7f16\u8bd1\u5668\u9996\u5148\u5c1d\u8bd5\u5bf9\u91cd\u8f7d\u51fd\u6570\u8fdb\u884c \u5b8c\u5168\u5339\u914d \u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5206\u4e3a\u4e24\u4e2a\u9636\u6bb5\u3002\u9996\u5148\uff0c\u7f16\u8bd1\u5668\u4f1a\u67e5\u627e\u662f\u5426\u5b58\u5728\u4e00\u4e2a\u91cd\u8f7d\u51fd\u6570\uff0c\u5176\u8c03\u7528\u65f6\u7684\u5b9e\u53c2\u5b8c\u5168\u5339\u914d\u91cd\u8f7d\u51fd\u6570\u7684\u5f62\u53c2\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 void print ( int ) { } void print ( double ) { } int main () { print ( 0 ); // exact match with print(int) print ( 3.4 ); // exact match with print(double) return 0 ; } \u56e0\u4e3a print(0) \u4e2d\u7684 0 \u662f\u4e00\u4e2a\u6574\u578b\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u67e5\u627e\u662f\u5426\u5b58\u5728 print(int) \u91cd\u8f7d\u51fd\u6570\u3002\u56e0\u4e3a\u7684\u786e\u5b58\u5728\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u53ef\u4ee5\u786e\u5b9a\u8be5\u91cd\u8f7d\u51fd\u6570\u4e3a\u88ab\u8c03\u7528\u51fd\u6570\u7684\u7cbe\u786e\u5339\u914d\u3002 \u5176\u6b21\uff0c\u7f16\u8bd1\u5668\u4f1a\u5bf9\u51fd\u6570\u8c03\u7528\u65f6\u7684\u5b9e\u53c2\u8fdb\u884c\u4e00\u7cfb\u5217\u7684 trivial-conversion \u3002\u8fd9\u4e9b\u7b80\u5355\u7684\u8f6c\u6362\u4f1a\u901a\u8fc7\u4fee\u6539\u7c7b\u578b\uff08\u4e0d\u4fee\u6539\u503c\uff09\u7684\u65b9\u5f0f\u67e5\u627e\u53ef\u80fd\u7684\u5339\u914d\u3002\u4f8b\u5982\uff0c\u975e const \u7c7b\u578b\u53ef\u80fd\u4f1a\u88ab\u8f6c\u6362\u4e3a const \u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 void print ( const int ) { } void print ( double ) { } int main () { int x { 0 }; print ( x ); // x trivially converted to const int return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8c03\u7528 print(x) \u65f6\uff0c x \u662f\u4e00\u4e2a int \u3002\u7f16\u8bd1\u5668\u4f1a\u5c06\u5176\u4ece int \u8f6c\u6362\u4e3a const int \uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5339\u914d\u5230 print(const int) \u3002 \u6269\u5c55\u9605\u8bfb \u5c06\u975e\u5f15\u7528\u7c7b\u578b\u8f6c\u6362\u4e3a\u5f15\u7528\u7c7b\u578b\uff08\u6216\u8005\u53cd\u8fc7\u6765\uff09\u4e5f\u5c5e\u4e8e[[[[trivial conversion|\u7b80\u5355\u8f6c\u6362]]\u3002 \u901a\u8fc7 \u7b80\u5355\u8f6c\u6362 \u5339\u914d\u5230\u7684\u91cd\u8f7d\u51fd\u6570\uff0c\u4e5f\u5c5e\u4e8e \u5b8c\u5168\u5339\u914d \u3002 \u7b2c\u4e8c\u6b65\uff1a\u5982\u679c\u6ca1\u6709\u627e\u5230\u5b8c\u5168\u5339\u914d\u7684\u91cd\u8f7d\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u5bf9\u5b9e\u53c2\u8fdb\u884c \u6570\u503c\u63d0\u5347 \uff0c\u5728 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u4e2d\u6211\u4ecb\u7ecd\u8fc7\uff0c\u5bbd\u5ea6\u8f83\u7a84\u7684\u6574\u578b\u548c\u6d6e\u70b9\u578b\u6570\u503c\u662f\u5982\u4f55\u88ab\u81ea\u52a8\u63d0\u5347\u4e3a\u8f83\u5bbd\u7684\u7c7b\u578b\u7684\uff0c\u4f8b\u5982 int \u6216 double \u3002\u5982\u679c\u6570\u503c\u63d0\u5347\u4e4b\u540e\u80fd\u591f\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\uff0c\u5219\u5b8c\u6210\u51fd\u6570\u8c03\u7528\u89e3\u6790\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 void print ( int ) { } void print ( double ) { } int main () { print ( 'a' ); // promoted to match print(int) print ( true ); // promoted to match print(int) print ( 4.5f ); // promoted to match print(double) return 0 ; } \u5bf9\u4e8e print('a') \u6765\u8bf4\uff0c\u7f16\u8bd1\u5668\u5728\u7b2c\u4e00\u6b65\u4e2d\u4e0d\u80fd\u627e\u5230\u80fd\u591f \u5b8c\u5168\u5339\u914d \u5b83\u7684\u91cd\u8f7d\u51fd\u6570 print(char) \uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u5c06\u5b57\u7b26 'a' \u63d0\u5347\u4e3a\u4e00\u4e2a\u6574\u578b\u5e76\u518d\u6b21\u67e5\u8fdb\u884c\u5339\u914d\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u5339\u914d\u5230 print(int) \uff0c\u6240\u4ee5\u51fd\u6570\u8c03\u7528\u89e3\u6790\u4e3a print(int) \u3002 \u7b2c\u4e09\u6b65\uff1a\u5982\u679c\u901a\u8fc7\u6570\u503c\u63d0\u5347\u4ecd\u4e0d\u80fd\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u5bf9\u51fd\u6570\u5b9e\u53c2\u8fdb\u884c \u6570\u503c\u8f6c\u6362 \uff08\u53c2\u89c1\uff1a 8.3 - \u6570\u503c\u8f6c\u6362 \uff09\u540e\uff0c\u518d\u6b21\u8fdb\u884c\u5339\u914d\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // for std::string void print ( double ) { } void print ( std :: string ) { } int main () { print ( 'a' ); // 'a' converted to match print(double) return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u4e0d\u5b58\u5728 print(char) (\u5b8c\u5168\u5339\u914d)\uff0c\u4e5f\u4e0d\u5b58\u5728 print(int) (\u63d0\u5347\u5339\u914d)\uff0c\u6240\u4ee5 'a' \u4f1a\u88ab\u6570\u503c\u8f6c\u6362\u4e3a double \u7c7b\u578b\u5e76\u5339\u914d print(double) \u3002 \u5173\u952e\u4fe1\u606f \u901a\u8fc7\u6570\u503c\u63d0\u5347\u8fdb\u884c\u5339\u914d\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e\u901a\u8fc7\u6570\u503c\u8f6c\u6362\u5339\u914d\u7684\u4f18\u5148\u7ea7\u3002 \u7b2c\u56db\u6b65\uff1a\u5982\u679c\u901a\u8fc7\u6570\u503c\u8f6c\u6362\u4ecd\u7136\u6ca1\u80fd\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u4f7f\u7528\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u8f6c\u6362\u3002\u867d\u7136\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u4ec0\u4e48\u662f\u7528\u6237\u81ea\u5b9a\u4e49\u8f6c\u6362\uff0c\u5176\u5b9e\u5b83\u5c31\u662f\u4e00\u7ec4\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u578b\u4e4b\u95f4\u7684\u9690\u5f0f\u8f6c\u6362\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // \u6211\u4eec\u8fd8\u6ca1\u8bb2\u5230\u7c7b\uff0c\u6240\u4ee5\u770b\u4e0d\u61c2\u4e5f\u4e0d\u7528\u62c5\u5fc3 class X // \u5b9a\u4e49\u4e86\u4e00\u4e2a\u65b0\u7c7b\u578b X { public : operator int () { return 0 ; } // \u7528\u6237\u81ea\u5b9a\u4e49\u7684\u5c06 X \u8f6c\u6362\u4e3a int \u7684\u89c4\u5219 }; void print ( int ) { } void print ( double ) { } int main () { X x ; // \u521b\u5efa X \u7c7b\u578b\u7684\u53d8\u91cf x print ( x ); // x \u901a\u8fc7\u7528\u6237\u81ea\u5b9a\u4e49\u8f6c\u6362\u4ece X \u8f6c\u6362\u4e3a int return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u68c0\u67e5\u662f\u5426\u80fd\u591f\u5b8c\u5168\u5339\u914d print(X) \u3002\u7531\u4e8e\u6ca1\u6709\u627e\u5230\uff0c\u7f16\u8bd1\u5668\u4f1a\u5bf9 x \u8fdb\u884c\u6570\u503c\u63d0\u5347\uff0c\u4f46\u662f\u8fd9\u4e00\u6b65\u65e0\u6cd5\u5b8c\u6210\uff0c\u7136\u540e\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u5bf9 x \u8fdb\u884c\u6570\u503c\u8f6c\u6362\uff0c\u540c\u6837\u4e0d\u80fd\u5b8c\u6210\u3002\u6700\u540e\uff0c\u7f16\u8bd1\u5668\u4f1a\u67e5\u627e\u662f\u5426\u5b58\u5728\u7528\u6237\u5b9a\u4e49\u7684\u8f6c\u6362\uff0c\u56e0\u4e3a\u6211\u4eec\u5b9a\u4e49\u4e86 X \u7c7b\u578b\u8f6c\u6362\u4e3a int \u7c7b\u578b\u7684\u89c4\u5219\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u4f7f\u7528\u8be5\u89c4\u5219\u5c06 X \u8f6c\u6362\u4e3a int \uff0c\u5e76\u5339\u914d print(int) \u3002 \u5728\u8fdb\u884c\u7528\u6237\u81ea\u5b9a\u4e49\u8f6c\u6362\u540e\uff0c\u7f16\u8bd1\u5668\u8fd8\u53ef\u80fd\u5c1d\u8bd5\u989d\u5916\u7684\u9690\u5f0f\u63d0\u5347\u6216\u8f6c\u6362\u6765\u67e5\u627e\u5339\u914d\uff0c\u6240\u4ee5\u5982\u679c\u6211\u4eec\u5b9a\u4e49\u7684\u8f6c\u6362\u89c4\u5219\u662f\u5c06 X \u8f6c\u6362\u4e3a char \u800c\u4e0d\u662f int \uff0c\u7f16\u8bd1\u5668\u4f1a\u7ee7\u7eed\u5bf9 char \u8fdb\u884c\u6570\u503c\u63d0\u5347\u4f7f\u5176\u63d0\u5347\u4e3a int \u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26 \u4e2d\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26\u6765\u521b\u5efa\u7528\u6237\u81ea\u5b9a\u4e49\u8f6c\u6362\u3002 \u6269\u5c55\u9605\u8bfb \u7c7b\u7684 \u6784\u9020\u51fd\u6570(constructor) \u540c\u6837\u5177\u6709\u81ea\u5b9a\u4e49\u8f6c\u6362\u7684\u529f\u80fd\uff0c\u5728\u4e5f\u53ef\u4ee5\u88ab\u7528\u5728\u7b2c\u56db\u6b65\u3002 \u7b2c\u4e94\u6b65\uff1a\u5982\u679c\u901a\u8fc7\u7528\u6237\u81ea\u5b9a\u4e49\u8f6c\u6362\u540e\u4ecd\u7136\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u91cd\u8f7d\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u5339\u914d\u4f7f\u7528\u4e86\u7701\u7565\u53f7\u7684\u51fd\u6570\u3002 \u76f8\u5173\u5185\u5bb9 \u7701\u7565\u53f7\u7684\u4f7f\u7528\u4f1a\u5728 12.6 - \u7701\u7565\u53f7\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 \u4e2d\u4ecb\u7ecd\u3002 \u7b2c\u516d\u6b65\uff1a \u5982\u679c\u5230\u6b64\u8fd8\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u653e\u5f03\u7ee7\u7eed\u67e5\u627e\u5e76\u4ea7\u751f\u4e00\u4e2a\u6ca1\u6709\u627e\u5230\u5339\u914d\u51fd\u6570\u7684\u7f16\u8bd1\u9519\u8bef\u3002 \u4e0d\u660e\u786e\u5339\u914d \u5728\u6ca1\u6709\u91cd\u8f7d\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u8981\u4e48\u89e3\u6790\u4e3a\u4e00\u4e2a\u51fd\u6570\uff0c\u8981\u4e48\u56e0\u4e3a\u627e\u4e0d\u5230\u800c\u4ea7\u751f\u7f16\u8bd1\u62a5\u9519\uff1a 1 2 3 4 5 6 7 8 9 10 11 void foo () { } int main () { foo (); // okay: match found goo (); // compile error: no match found return 0 ; } \u800c\u5728\u5b58\u5728\u91cd\u8f7d\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd8\u6709\u7b2c\u4e09\u79cd\u53ef\u80fd\uff1a \u4e0d\u660e\u786e\u5339\u914d(ambiguous match) \u3002\u5f53\u7f16\u8bd1\u5668\u5728\u4e00\u4e2a\u6b65\u9aa4\u4e2d\u627e\u5230\u4e24\u4e2a\u6216\u4e24\u4e2a\u4ee5\u4e0a\u5339\u914d\u7684\u91cd\u8f7d\u51fd\u6570\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u505c\u6b62\u5339\u914d\u5e76\u62a5\u544a\u627e\u5230\u4e0d\u660e\u786e\u5339\u914d\u51fd\u6570\u8c03\u7528\u3002 \u4e3a\u4e86\u80fd\u591f\u8fdb\u884c\u7f16\u8bd1\uff0c\u7f16\u8bd1\u5668\u5fc5\u987b\u533a\u5206\u6bcf\u4e2a\u91cd\u8f7d\u51fd\u6570\uff0c\u90a3\u4e48\uff0c\u4ec0\u4e48\u60c5\u51b5\u4e0b\u4f1a\u51fa\u73b0\u591a\u4e2a\u5339\u914d\u7684\u60c5\u51b5\u5462\uff1f\u8bf7\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 void print ( int x ) { } void print ( double d ) { } int main () { print ( 5L ); // 5L is type long return 0 ; } \u5b57\u9762\u91cf 5L \u662f long \u578b\u7684\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u67e5\u627e\u662f\u5426\u5b58\u5728\u5b8c\u5168\u5339\u914d\u7684 print(long) \uff0c\u53ef\u60dc\u6ca1\u80fd\u627e\u5230\u3002\u63a5\u4e0b\u6765\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u6570\u503c\u63d0\u5347\uff0c\u4f46\u662f long \u5df2\u7ecf\u4e0d\u80fd\u63d0\u5347\u4e86\uff0c\u6240\u4ee5\u4ecd\u7136\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\u3002 \u63a5\u4e0b\u6765\uff0c\u7f16\u8bd1\u5668\u4f1a\u5bf9 long \u7c7b\u578b\u7684\u5b9e\u53c2\u8fdb\u884c\u6570\u503c\u8f6c\u6362\u3002\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u5b83\u4f1a\u5c1d\u8bd5\u6240\u6709\u7684\u6570\u503c\u8f6c\u6362\u89c4\u5219\uff0c\u6b64\u65f6\u7f16\u8bd1\u5668\u4f1a\u627e\u5230\u4e24\u4e2a\u53ef\u4ee5\u80fd\u7684\u5339\u914d\u3002\u5982\u679c long \u5b9e\u53c2\u901a\u8fc7\u6570\u503c\u8f6c\u6362\u8f6c\u6362\u6210\u4e86 int \uff0c\u90a3\u4e48\u53ef\u4ee5\u5339\u914d print(int) \u3002\u5982\u679c long \u5b9e\u53c2\u8f6c\u6362\u6210\u4e86 double \uff0c\u5219\u53ef\u4ee5\u5339\u914d print(double) \u3002\u56e0\u4e3a\u627e\u5230\u4e86\u4e24\u79cd\u53ef\u80fd\u7684\u5339\u914d\uff0c\u6240\u4ee5\u8be5\u51fd\u6570\u8c03\u7528\u88ab\u8ba4\u4e3a\u662f\u4e0d\u660e\u786e\u7684\u3002 \u5728 Visual Studio 2019 \u4e0a\u4f1a\u4ea7\u751f\u5982\u4e0b\u62a5\u9519\u4fe1\u606f\uff1a 1 2 3 4 error C2668: 'print': ambiguous call to overloaded function message : could be 'void print(double)' message : or 'void print(int)' message : while trying to match the argument list '(long)' \u5173\u952e\u4fe1\u606f \u5982\u679c\u7f16\u8bd1\u5668\u5728\u67d0\u4e00\u6b65\u627e\u5230\u4e86\u591a\u4e2a\u53ef\u80fd\u7684\u5339\u914d\uff0c\u5219\u4f1a\u5bfc\u81f4\u4e0d\u660e\u786e\u5339\u914d\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u8fd9\u4e00\u6b65\u4e2d\u7f16\u8bd1\u5668\u6ca1\u80fd\u627e\u5230\u4e00\u4e2a\u6bd4\u5176\u4ed6\u5339\u914d\u90fd\u597d\u7684\u5339\u914d\u3002 \u518d\u6765\u770b\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 void print ( unsigned int x ) { } void print ( float y ) { } int main () { print ( 0 ); // int \u53ef\u4ee5\u88ab\u6570\u503c\u8f6c\u6362\u4e3a unsigned int \u6216 float print ( 3.14159 ); // double \u53ef\u4ee5\u88ab\u6570\u503c\u8f6c\u6362\u4e3a unsigned int \u6216 float return 0 ; } \u4e5f\u8bb8\u4f60\u4f1a\u8ba4\u4e3a 0 \u4f1a\u88ab\u89e3\u6790\u4e3a unsigned int \u8fdb\u800c\u5339\u914d print(unsigned int) \u3002\u540c\u65f6 3.14159 \u89e3\u6790\u4e3a float \u8fdb\u800c\u5339\u914d print(float) \u3002\u5176\u5b9e\u5b83\u4eec\u90fd\u4f1a\u4ea7\u751f\u4e0d\u660e\u786e\u5339\u914d\u3002 int \u503c 0 \u53ef\u4ee5\u901a\u8fc7\u6570\u503c\u8f6c\u6362\u88ab\u8f6c\u6362\u4e3a unsigned int \u6216\u8005 float \uff0c\u6240\u4ee5\u4e24\u4e2a\u51fd\u6570\u5b83\u90fd\u80fd\u5339\u914d\uff0c\u8fd9\u52bf\u5fc5\u5c06\u5bfc\u81f4\u4e0d\u660e\u786e\u5339\u914d\u4ea7\u751f\u3002 \u5bf9\u4e8e double \u7c7b\u578b\u4e5f\u4e00\u6837\uff0c\u5b83\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a float \u6216\u8005 unsigned int \u3002\u4e0d\u8bba\u5982\u4f55\u8f6c\u6362\uff0c\u90fd\u5c5e\u4e8e\u6570\u503c\u8f6c\u6362\uff0c\u4e5f\u90fd\u80fd\u5339\u914d\u4e24\u4e2a\u51fd\u6570\uff0c\u6240\u4ee5\u662f\u4e0d\u660e\u786e\u5339\u914d\u3002 \u5904\u7406\u4e0d\u660e\u786e\u5339\u914d \u56e0\u4e3a\u4e0d\u660e\u786e\u5339\u914d\u662f\u7f16\u8bd1\u65f6\u9519\u8bef\uff0c\u6240\u4ee5\u5728\u7a0b\u5e8f\u7f16\u8bd1\u4e4b\u524d\uff0c\u9700\u8981\u6d88\u9664\u6b67\u4e49\u3002\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u89e3\u51b3\u8be5\u95ee\u9898\uff1a \u901a\u5e38\uff0c\u6700\u597d\u7684\u65b9\u6cd5\u662f\u7b80\u5355\u5730\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u91cd\u8f7d\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u7684\u5f62\u53c2\u4e0e\u60a8\u8bd5\u56fe\u8c03\u7528\u8be5\u51fd\u6570\u65f6\u4f7f\u7528\u7684\u5f62\u53c2\u7c7b\u578b\u5b8c\u5168\u76f8\u540c\u3002\u7136\u540eC++\u5c31\u80fd\u627e\u5230\u4e0e\u51fd\u6570\u8c03\u7528\u5b8c\u5168\u5339\u914d\u7684\u5bf9\u8c61\u3002 \u6216\u8005\uff0c \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u8f6c\u6362\u4e0d\u660e\u786e\u7684\u53c2\u6570\uff0c\u4ee5\u5339\u914d\u8981\u8c03\u7528\u7684\u51fd\u6570\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u8981\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f print(0) \u5339\u914d print(unsigned int) \uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a: 1 2 int x { 0 }; print ( static_cast < unsigned int > ( x )); // will call print(unsigned int) \u5982\u679c\u53c2\u6570\u662f\u5b57\u9762\u91cf\uff0c\u4f60\u53ef\u4ee5\u4e3a\u5b83\u6dfb\u52a0\u5b57\u9762\u91cf\u540e\u7f00\uff0c\u4f7f\u5176\u80fd\u591f\u88ab\u89e3\u6790\u4e3a\u6b63\u786e\u7684\u7c7b\u578b\uff1a 1 print ( 0u ); // will call print(unsigned int) since 'u' suffix is unsigned int, so this is now an exact match \u5e38\u7528\u7684\u540e\u7f00\u53c2\u89c1\uff1a 4-15-Literals#\u5b57\u9762\u91cf\u540e\u7f00 \u591a\u53c2\u6570\u91cd\u8f7d\u51fd\u6570\u7684\u5339\u914d \u5982\u679c\u6709\u591a\u4e2a\u53c2\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u4f9d\u6b21\u5bf9\u6bcf\u4e2a\u53c2\u6570\u5e94\u7528\u5339\u914d\u89c4\u5219\u3002\u6700\u7ec8\u5339\u914d\u7684\u51fd\u6570\u7684\u8981\u6c42\u662f\uff1a\u6bcf\u4e2a\u53c2\u6570\u90fd\u5339\u914d\u4e14\u81f3\u5c11\u6709\u4e00\u4e2a\u53c2\u6570\u6bd4\u6240\u6709\u5176\u4ed6\u51fd\u6570\u5339\u914d\u5f97\u66f4\u597d\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6240\u9009\u51fd\u6570\u5fc5\u987b\u5bf9\u81f3\u5c11\u4e00\u4e2a\u53c2\u6570\u63d0\u4f9b\u6bd4\u6240\u6709\u5176\u4ed6\u5019\u9009\u51fd\u6570\u66f4\u597d\u7684\u5339\u914d\uff0c\u800c\u5bf9\u6240\u6709\u5176\u4ed6\u53c2\u6570\u4e0d\u5dee\u3002 \u5728\u627e\u5230\u8fd9\u6837\u4e00\u4e2a\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u5b83\u663e\u7136\u662f\u6700\u597d\u7684\u9009\u62e9\u3002\u5982\u679c\u627e\u4e0d\u5230\u8fd9\u6837\u7684\u51fd\u6570\uff0c\u5219\u8be5\u8c03\u7528\u5c06\u88ab\u89c6\u4e3a\u4e8c\u4e49\u6027(\u6216\u4e0d\u5339\u914d)\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include void print ( char c , int x ) { std :: cout << 'a' ; } void print ( char c , double x ) { std :: cout << 'b' ; } void print ( char c , float x ) { std :: cout << 'c' ; } int main () { print ( 'x' , 'a' ); } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u90fd\u80fd\u591f \u5b8c\u5168\u5339\u914d \u3002\u7b2c\u4e00\u4e2a\u51fd\u6570\u901a\u8fc7 \u6570\u503c\u63d0\u5347 \u53ef\u4ee5\u5339\u914d\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u5176\u4ed6\u51e0\u4e2a\u51fd\u6570\u5219\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u624d\u80fd\u5339\u914d\u3002\u56e0\u6b64 print(char, int) \u6bcb\u5eb8\u7f6e\u7591\u662f\u6700\u4f73\u5339\u914d\u51fd\u6570\u3002","title":"8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u4e0d\u660e\u786e\u5339\u914d"},{"location":"8-11-Function-overload-resolution-and-ambiguous-matches/#_1","text":"\u5f53\u5bf9\u91cd\u8f7d\u51fd\u6570\u8fdb\u884c\u8c03\u7528\u65f6\uff0c\u7f16\u8bd1\u5668\u901a\u8fc7\u4e00\u7cfb\u5217\u89c4\u5219\u548c\u6b65\u9aa4\u786e\u5b9a\u91cd\u8f7d\u51fd\u6570\u7684\u6700\u4f73\u5339\u914d(\u5982\u679c\u6709\u7684\u8bdd)\u3002 \u5728\u6bcf\u4e2a\u6b65\u9aa4\u4e2d\uff0c\u7f16\u8bd1\u5668\u5bf9\u51fd\u6570\u8c03\u7528\u4e2d\u7684\u5b9e\u53c2\u5e94\u7528\u4e00\u7cfb\u5217\u4e0d\u540c\u7684\u7c7b\u578b\u8f6c\u6362\u3002\u5bf9\u4e8e\u5e94\u7528\u7684\u6bcf\u4e2a\u8f6c\u6362\uff0c\u7f16\u8bd1\u5668\u68c0\u67e5\u91cd\u8f7d\u7684\u51fd\u6570\u73b0\u5728\u662f\u5426\u5339\u914d\u3002\u5728\u5e94\u7528\u4e86\u6240\u6709\u4e0d\u540c\u7684\u7c7b\u578b\u8f6c\u6362\u5e76\u68c0\u67e5\u4e86\u5339\u914d\u4e4b\u540e\uff0c\u8fd9\u4e00\u6b65\u5c31\u5b8c\u6210\u4e86\u3002\u7ed3\u679c\u5c06\u662f\u4ee5\u4e0b\u4e09\u79cd\u53ef\u80fd\u7684\u7ed3\u679c\u4e4b\u4e00\uff1a \u672a\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\u3002\u7f16\u8bd1\u5668\u8fdb\u5165\u4e0b\u4e00\u4e2a\u6b65\u9aa4\u3002 \u627e\u5230\u5355\u4e2a\u5339\u914d\u51fd\u6570\u3002\u8be5\u51fd\u6570\u88ab\u8ba4\u4e3a\u662f\u6700\u4f73\u5339\u914d\u3002\u5339\u914d\u8fc7\u7a0b\u73b0\u5728\u5df2\u7ecf\u5b8c\u6210\uff0c\u540e\u7eed\u6b65\u9aa4\u4e0d\u518d\u6267\u884c\u3002 \u53d1\u73b0\u591a\u4e2a\u5339\u914d\u51fd\u6570\u3002\u7f16\u8bd1\u5668\u5c06\u53d1\u51fa\u4e00\u4e2a \u4e0d\u660e\u786e\u5339\u914d \u7684\u7f16\u8bd1\u9519\u8bef\u3002\u6211\u4eec\u7a0d\u540e\u5c06\u8fdb\u4e00\u6b65\u8ba8\u8bba\u8fd9\u79cd\u60c5\u51b5\u3002 \u5982\u679c\u7f16\u8bd1\u5668\u6267\u884c\u5b8c\u4e00\u7cfb\u5217\u6b65\u9aa4\u540e\uff0c\u4ecd\u7136\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\uff0c\u5b83\u5c06\u751f\u6210\u4e00\u4e2a\u7f16\u8bd1\u9519\u8bef\uff0c\u5373\u65e0\u6cd5\u4e3a\u51fd\u6570\u8c03\u7528\u627e\u5230\u5339\u914d\u7684\u91cd\u8f7d\u51fd\u6570\u3002","title":"\u89e3\u6790\u91cd\u8f7d\u51fd\u6570\u8c03\u7528"},{"location":"8-11-Function-overload-resolution-and-ambiguous-matches/#_2","text":"\u7b2c\u4e00\u6b65\uff1a\u7f16\u8bd1\u5668\u9996\u5148\u5c1d\u8bd5\u5bf9\u91cd\u8f7d\u51fd\u6570\u8fdb\u884c \u5b8c\u5168\u5339\u914d \u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5206\u4e3a\u4e24\u4e2a\u9636\u6bb5\u3002\u9996\u5148\uff0c\u7f16\u8bd1\u5668\u4f1a\u67e5\u627e\u662f\u5426\u5b58\u5728\u4e00\u4e2a\u91cd\u8f7d\u51fd\u6570\uff0c\u5176\u8c03\u7528\u65f6\u7684\u5b9e\u53c2\u5b8c\u5168\u5339\u914d\u91cd\u8f7d\u51fd\u6570\u7684\u5f62\u53c2\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 void print ( int ) { } void print ( double ) { } int main () { print ( 0 ); // exact match with print(int) print ( 3.4 ); // exact match with print(double) return 0 ; } \u56e0\u4e3a print(0) \u4e2d\u7684 0 \u662f\u4e00\u4e2a\u6574\u578b\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u67e5\u627e\u662f\u5426\u5b58\u5728 print(int) \u91cd\u8f7d\u51fd\u6570\u3002\u56e0\u4e3a\u7684\u786e\u5b58\u5728\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u53ef\u4ee5\u786e\u5b9a\u8be5\u91cd\u8f7d\u51fd\u6570\u4e3a\u88ab\u8c03\u7528\u51fd\u6570\u7684\u7cbe\u786e\u5339\u914d\u3002 \u5176\u6b21\uff0c\u7f16\u8bd1\u5668\u4f1a\u5bf9\u51fd\u6570\u8c03\u7528\u65f6\u7684\u5b9e\u53c2\u8fdb\u884c\u4e00\u7cfb\u5217\u7684 trivial-conversion \u3002\u8fd9\u4e9b\u7b80\u5355\u7684\u8f6c\u6362\u4f1a\u901a\u8fc7\u4fee\u6539\u7c7b\u578b\uff08\u4e0d\u4fee\u6539\u503c\uff09\u7684\u65b9\u5f0f\u67e5\u627e\u53ef\u80fd\u7684\u5339\u914d\u3002\u4f8b\u5982\uff0c\u975e const \u7c7b\u578b\u53ef\u80fd\u4f1a\u88ab\u8f6c\u6362\u4e3a const \u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 void print ( const int ) { } void print ( double ) { } int main () { int x { 0 }; print ( x ); // x trivially converted to const int return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8c03\u7528 print(x) \u65f6\uff0c x \u662f\u4e00\u4e2a int \u3002\u7f16\u8bd1\u5668\u4f1a\u5c06\u5176\u4ece int \u8f6c\u6362\u4e3a const int \uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5339\u914d\u5230 print(const int) \u3002 \u6269\u5c55\u9605\u8bfb \u5c06\u975e\u5f15\u7528\u7c7b\u578b\u8f6c\u6362\u4e3a\u5f15\u7528\u7c7b\u578b\uff08\u6216\u8005\u53cd\u8fc7\u6765\uff09\u4e5f\u5c5e\u4e8e[[[[trivial conversion|\u7b80\u5355\u8f6c\u6362]]\u3002 \u901a\u8fc7 \u7b80\u5355\u8f6c\u6362 \u5339\u914d\u5230\u7684\u91cd\u8f7d\u51fd\u6570\uff0c\u4e5f\u5c5e\u4e8e \u5b8c\u5168\u5339\u914d \u3002 \u7b2c\u4e8c\u6b65\uff1a\u5982\u679c\u6ca1\u6709\u627e\u5230\u5b8c\u5168\u5339\u914d\u7684\u91cd\u8f7d\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u5bf9\u5b9e\u53c2\u8fdb\u884c \u6570\u503c\u63d0\u5347 \uff0c\u5728 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u4e2d\u6211\u4ecb\u7ecd\u8fc7\uff0c\u5bbd\u5ea6\u8f83\u7a84\u7684\u6574\u578b\u548c\u6d6e\u70b9\u578b\u6570\u503c\u662f\u5982\u4f55\u88ab\u81ea\u52a8\u63d0\u5347\u4e3a\u8f83\u5bbd\u7684\u7c7b\u578b\u7684\uff0c\u4f8b\u5982 int \u6216 double \u3002\u5982\u679c\u6570\u503c\u63d0\u5347\u4e4b\u540e\u80fd\u591f\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\uff0c\u5219\u5b8c\u6210\u51fd\u6570\u8c03\u7528\u89e3\u6790\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 void print ( int ) { } void print ( double ) { } int main () { print ( 'a' ); // promoted to match print(int) print ( true ); // promoted to match print(int) print ( 4.5f ); // promoted to match print(double) return 0 ; } \u5bf9\u4e8e print('a') \u6765\u8bf4\uff0c\u7f16\u8bd1\u5668\u5728\u7b2c\u4e00\u6b65\u4e2d\u4e0d\u80fd\u627e\u5230\u80fd\u591f \u5b8c\u5168\u5339\u914d \u5b83\u7684\u91cd\u8f7d\u51fd\u6570 print(char) \uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u5c06\u5b57\u7b26 'a' \u63d0\u5347\u4e3a\u4e00\u4e2a\u6574\u578b\u5e76\u518d\u6b21\u67e5\u8fdb\u884c\u5339\u914d\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u5339\u914d\u5230 print(int) \uff0c\u6240\u4ee5\u51fd\u6570\u8c03\u7528\u89e3\u6790\u4e3a print(int) \u3002 \u7b2c\u4e09\u6b65\uff1a\u5982\u679c\u901a\u8fc7\u6570\u503c\u63d0\u5347\u4ecd\u4e0d\u80fd\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u5bf9\u51fd\u6570\u5b9e\u53c2\u8fdb\u884c \u6570\u503c\u8f6c\u6362 \uff08\u53c2\u89c1\uff1a 8.3 - \u6570\u503c\u8f6c\u6362 \uff09\u540e\uff0c\u518d\u6b21\u8fdb\u884c\u5339\u914d\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // for std::string void print ( double ) { } void print ( std :: string ) { } int main () { print ( 'a' ); // 'a' converted to match print(double) return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u4e0d\u5b58\u5728 print(char) (\u5b8c\u5168\u5339\u914d)\uff0c\u4e5f\u4e0d\u5b58\u5728 print(int) (\u63d0\u5347\u5339\u914d)\uff0c\u6240\u4ee5 'a' \u4f1a\u88ab\u6570\u503c\u8f6c\u6362\u4e3a double \u7c7b\u578b\u5e76\u5339\u914d print(double) \u3002 \u5173\u952e\u4fe1\u606f \u901a\u8fc7\u6570\u503c\u63d0\u5347\u8fdb\u884c\u5339\u914d\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e\u901a\u8fc7\u6570\u503c\u8f6c\u6362\u5339\u914d\u7684\u4f18\u5148\u7ea7\u3002 \u7b2c\u56db\u6b65\uff1a\u5982\u679c\u901a\u8fc7\u6570\u503c\u8f6c\u6362\u4ecd\u7136\u6ca1\u80fd\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u4f7f\u7528\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u8f6c\u6362\u3002\u867d\u7136\u6211\u4eec\u8fd8\u6ca1\u6709\u4ecb\u7ecd\u4ec0\u4e48\u662f\u7528\u6237\u81ea\u5b9a\u4e49\u8f6c\u6362\uff0c\u5176\u5b9e\u5b83\u5c31\u662f\u4e00\u7ec4\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u578b\u4e4b\u95f4\u7684\u9690\u5f0f\u8f6c\u6362\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // \u6211\u4eec\u8fd8\u6ca1\u8bb2\u5230\u7c7b\uff0c\u6240\u4ee5\u770b\u4e0d\u61c2\u4e5f\u4e0d\u7528\u62c5\u5fc3 class X // \u5b9a\u4e49\u4e86\u4e00\u4e2a\u65b0\u7c7b\u578b X { public : operator int () { return 0 ; } // \u7528\u6237\u81ea\u5b9a\u4e49\u7684\u5c06 X \u8f6c\u6362\u4e3a int \u7684\u89c4\u5219 }; void print ( int ) { } void print ( double ) { } int main () { X x ; // \u521b\u5efa X \u7c7b\u578b\u7684\u53d8\u91cf x print ( x ); // x \u901a\u8fc7\u7528\u6237\u81ea\u5b9a\u4e49\u8f6c\u6362\u4ece X \u8f6c\u6362\u4e3a int return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u68c0\u67e5\u662f\u5426\u80fd\u591f\u5b8c\u5168\u5339\u914d print(X) \u3002\u7531\u4e8e\u6ca1\u6709\u627e\u5230\uff0c\u7f16\u8bd1\u5668\u4f1a\u5bf9 x \u8fdb\u884c\u6570\u503c\u63d0\u5347\uff0c\u4f46\u662f\u8fd9\u4e00\u6b65\u65e0\u6cd5\u5b8c\u6210\uff0c\u7136\u540e\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u5bf9 x \u8fdb\u884c\u6570\u503c\u8f6c\u6362\uff0c\u540c\u6837\u4e0d\u80fd\u5b8c\u6210\u3002\u6700\u540e\uff0c\u7f16\u8bd1\u5668\u4f1a\u67e5\u627e\u662f\u5426\u5b58\u5728\u7528\u6237\u5b9a\u4e49\u7684\u8f6c\u6362\uff0c\u56e0\u4e3a\u6211\u4eec\u5b9a\u4e49\u4e86 X \u7c7b\u578b\u8f6c\u6362\u4e3a int \u7c7b\u578b\u7684\u89c4\u5219\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u4f7f\u7528\u8be5\u89c4\u5219\u5c06 X \u8f6c\u6362\u4e3a int \uff0c\u5e76\u5339\u914d print(int) \u3002 \u5728\u8fdb\u884c\u7528\u6237\u81ea\u5b9a\u4e49\u8f6c\u6362\u540e\uff0c\u7f16\u8bd1\u5668\u8fd8\u53ef\u80fd\u5c1d\u8bd5\u989d\u5916\u7684\u9690\u5f0f\u63d0\u5347\u6216\u8f6c\u6362\u6765\u67e5\u627e\u5339\u914d\uff0c\u6240\u4ee5\u5982\u679c\u6211\u4eec\u5b9a\u4e49\u7684\u8f6c\u6362\u89c4\u5219\u662f\u5c06 X \u8f6c\u6362\u4e3a char \u800c\u4e0d\u662f int \uff0c\u7f16\u8bd1\u5668\u4f1a\u7ee7\u7eed\u5bf9 char \u8fdb\u884c\u6570\u503c\u63d0\u5347\u4f7f\u5176\u63d0\u5347\u4e3a int \u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26 \u4e2d\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26\u6765\u521b\u5efa\u7528\u6237\u81ea\u5b9a\u4e49\u8f6c\u6362\u3002 \u6269\u5c55\u9605\u8bfb \u7c7b\u7684 \u6784\u9020\u51fd\u6570(constructor) \u540c\u6837\u5177\u6709\u81ea\u5b9a\u4e49\u8f6c\u6362\u7684\u529f\u80fd\uff0c\u5728\u4e5f\u53ef\u4ee5\u88ab\u7528\u5728\u7b2c\u56db\u6b65\u3002 \u7b2c\u4e94\u6b65\uff1a\u5982\u679c\u901a\u8fc7\u7528\u6237\u81ea\u5b9a\u4e49\u8f6c\u6362\u540e\u4ecd\u7136\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u91cd\u8f7d\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u5339\u914d\u4f7f\u7528\u4e86\u7701\u7565\u53f7\u7684\u51fd\u6570\u3002 \u76f8\u5173\u5185\u5bb9 \u7701\u7565\u53f7\u7684\u4f7f\u7528\u4f1a\u5728 12.6 - \u7701\u7565\u53f7\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 \u4e2d\u4ecb\u7ecd\u3002 \u7b2c\u516d\u6b65\uff1a \u5982\u679c\u5230\u6b64\u8fd8\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u653e\u5f03\u7ee7\u7eed\u67e5\u627e\u5e76\u4ea7\u751f\u4e00\u4e2a\u6ca1\u6709\u627e\u5230\u5339\u914d\u51fd\u6570\u7684\u7f16\u8bd1\u9519\u8bef\u3002","title":"\u53c2\u6570\u5339\u914d\u987a\u5e8f"},{"location":"8-11-Function-overload-resolution-and-ambiguous-matches/#_3","text":"\u5728\u6ca1\u6709\u91cd\u8f7d\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u8981\u4e48\u89e3\u6790\u4e3a\u4e00\u4e2a\u51fd\u6570\uff0c\u8981\u4e48\u56e0\u4e3a\u627e\u4e0d\u5230\u800c\u4ea7\u751f\u7f16\u8bd1\u62a5\u9519\uff1a 1 2 3 4 5 6 7 8 9 10 11 void foo () { } int main () { foo (); // okay: match found goo (); // compile error: no match found return 0 ; } \u800c\u5728\u5b58\u5728\u91cd\u8f7d\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd8\u6709\u7b2c\u4e09\u79cd\u53ef\u80fd\uff1a \u4e0d\u660e\u786e\u5339\u914d(ambiguous match) \u3002\u5f53\u7f16\u8bd1\u5668\u5728\u4e00\u4e2a\u6b65\u9aa4\u4e2d\u627e\u5230\u4e24\u4e2a\u6216\u4e24\u4e2a\u4ee5\u4e0a\u5339\u914d\u7684\u91cd\u8f7d\u51fd\u6570\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u505c\u6b62\u5339\u914d\u5e76\u62a5\u544a\u627e\u5230\u4e0d\u660e\u786e\u5339\u914d\u51fd\u6570\u8c03\u7528\u3002 \u4e3a\u4e86\u80fd\u591f\u8fdb\u884c\u7f16\u8bd1\uff0c\u7f16\u8bd1\u5668\u5fc5\u987b\u533a\u5206\u6bcf\u4e2a\u91cd\u8f7d\u51fd\u6570\uff0c\u90a3\u4e48\uff0c\u4ec0\u4e48\u60c5\u51b5\u4e0b\u4f1a\u51fa\u73b0\u591a\u4e2a\u5339\u914d\u7684\u60c5\u51b5\u5462\uff1f\u8bf7\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 void print ( int x ) { } void print ( double d ) { } int main () { print ( 5L ); // 5L is type long return 0 ; } \u5b57\u9762\u91cf 5L \u662f long \u578b\u7684\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u9996\u5148\u4f1a\u67e5\u627e\u662f\u5426\u5b58\u5728\u5b8c\u5168\u5339\u914d\u7684 print(long) \uff0c\u53ef\u60dc\u6ca1\u80fd\u627e\u5230\u3002\u63a5\u4e0b\u6765\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u6570\u503c\u63d0\u5347\uff0c\u4f46\u662f long \u5df2\u7ecf\u4e0d\u80fd\u63d0\u5347\u4e86\uff0c\u6240\u4ee5\u4ecd\u7136\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u51fd\u6570\u3002 \u63a5\u4e0b\u6765\uff0c\u7f16\u8bd1\u5668\u4f1a\u5bf9 long \u7c7b\u578b\u7684\u5b9e\u53c2\u8fdb\u884c\u6570\u503c\u8f6c\u6362\u3002\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u5b83\u4f1a\u5c1d\u8bd5\u6240\u6709\u7684\u6570\u503c\u8f6c\u6362\u89c4\u5219\uff0c\u6b64\u65f6\u7f16\u8bd1\u5668\u4f1a\u627e\u5230\u4e24\u4e2a\u53ef\u4ee5\u80fd\u7684\u5339\u914d\u3002\u5982\u679c long \u5b9e\u53c2\u901a\u8fc7\u6570\u503c\u8f6c\u6362\u8f6c\u6362\u6210\u4e86 int \uff0c\u90a3\u4e48\u53ef\u4ee5\u5339\u914d print(int) \u3002\u5982\u679c long \u5b9e\u53c2\u8f6c\u6362\u6210\u4e86 double \uff0c\u5219\u53ef\u4ee5\u5339\u914d print(double) \u3002\u56e0\u4e3a\u627e\u5230\u4e86\u4e24\u79cd\u53ef\u80fd\u7684\u5339\u914d\uff0c\u6240\u4ee5\u8be5\u51fd\u6570\u8c03\u7528\u88ab\u8ba4\u4e3a\u662f\u4e0d\u660e\u786e\u7684\u3002 \u5728 Visual Studio 2019 \u4e0a\u4f1a\u4ea7\u751f\u5982\u4e0b\u62a5\u9519\u4fe1\u606f\uff1a 1 2 3 4 error C2668: 'print': ambiguous call to overloaded function message : could be 'void print(double)' message : or 'void print(int)' message : while trying to match the argument list '(long)' \u5173\u952e\u4fe1\u606f \u5982\u679c\u7f16\u8bd1\u5668\u5728\u67d0\u4e00\u6b65\u627e\u5230\u4e86\u591a\u4e2a\u53ef\u80fd\u7684\u5339\u914d\uff0c\u5219\u4f1a\u5bfc\u81f4\u4e0d\u660e\u786e\u5339\u914d\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u8fd9\u4e00\u6b65\u4e2d\u7f16\u8bd1\u5668\u6ca1\u80fd\u627e\u5230\u4e00\u4e2a\u6bd4\u5176\u4ed6\u5339\u914d\u90fd\u597d\u7684\u5339\u914d\u3002 \u518d\u6765\u770b\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 void print ( unsigned int x ) { } void print ( float y ) { } int main () { print ( 0 ); // int \u53ef\u4ee5\u88ab\u6570\u503c\u8f6c\u6362\u4e3a unsigned int \u6216 float print ( 3.14159 ); // double \u53ef\u4ee5\u88ab\u6570\u503c\u8f6c\u6362\u4e3a unsigned int \u6216 float return 0 ; } \u4e5f\u8bb8\u4f60\u4f1a\u8ba4\u4e3a 0 \u4f1a\u88ab\u89e3\u6790\u4e3a unsigned int \u8fdb\u800c\u5339\u914d print(unsigned int) \u3002\u540c\u65f6 3.14159 \u89e3\u6790\u4e3a float \u8fdb\u800c\u5339\u914d print(float) \u3002\u5176\u5b9e\u5b83\u4eec\u90fd\u4f1a\u4ea7\u751f\u4e0d\u660e\u786e\u5339\u914d\u3002 int \u503c 0 \u53ef\u4ee5\u901a\u8fc7\u6570\u503c\u8f6c\u6362\u88ab\u8f6c\u6362\u4e3a unsigned int \u6216\u8005 float \uff0c\u6240\u4ee5\u4e24\u4e2a\u51fd\u6570\u5b83\u90fd\u80fd\u5339\u914d\uff0c\u8fd9\u52bf\u5fc5\u5c06\u5bfc\u81f4\u4e0d\u660e\u786e\u5339\u914d\u4ea7\u751f\u3002 \u5bf9\u4e8e double \u7c7b\u578b\u4e5f\u4e00\u6837\uff0c\u5b83\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a float \u6216\u8005 unsigned int \u3002\u4e0d\u8bba\u5982\u4f55\u8f6c\u6362\uff0c\u90fd\u5c5e\u4e8e\u6570\u503c\u8f6c\u6362\uff0c\u4e5f\u90fd\u80fd\u5339\u914d\u4e24\u4e2a\u51fd\u6570\uff0c\u6240\u4ee5\u662f\u4e0d\u660e\u786e\u5339\u914d\u3002","title":"\u4e0d\u660e\u786e\u5339\u914d"},{"location":"8-11-Function-overload-resolution-and-ambiguous-matches/#_4","text":"\u56e0\u4e3a\u4e0d\u660e\u786e\u5339\u914d\u662f\u7f16\u8bd1\u65f6\u9519\u8bef\uff0c\u6240\u4ee5\u5728\u7a0b\u5e8f\u7f16\u8bd1\u4e4b\u524d\uff0c\u9700\u8981\u6d88\u9664\u6b67\u4e49\u3002\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u89e3\u51b3\u8be5\u95ee\u9898\uff1a \u901a\u5e38\uff0c\u6700\u597d\u7684\u65b9\u6cd5\u662f\u7b80\u5355\u5730\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u91cd\u8f7d\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u7684\u5f62\u53c2\u4e0e\u60a8\u8bd5\u56fe\u8c03\u7528\u8be5\u51fd\u6570\u65f6\u4f7f\u7528\u7684\u5f62\u53c2\u7c7b\u578b\u5b8c\u5168\u76f8\u540c\u3002\u7136\u540eC++\u5c31\u80fd\u627e\u5230\u4e0e\u51fd\u6570\u8c03\u7528\u5b8c\u5168\u5339\u914d\u7684\u5bf9\u8c61\u3002 \u6216\u8005\uff0c \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u8f6c\u6362\u4e0d\u660e\u786e\u7684\u53c2\u6570\uff0c\u4ee5\u5339\u914d\u8981\u8c03\u7528\u7684\u51fd\u6570\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u8981\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f print(0) \u5339\u914d print(unsigned int) \uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a: 1 2 int x { 0 }; print ( static_cast < unsigned int > ( x )); // will call print(unsigned int) \u5982\u679c\u53c2\u6570\u662f\u5b57\u9762\u91cf\uff0c\u4f60\u53ef\u4ee5\u4e3a\u5b83\u6dfb\u52a0\u5b57\u9762\u91cf\u540e\u7f00\uff0c\u4f7f\u5176\u80fd\u591f\u88ab\u89e3\u6790\u4e3a\u6b63\u786e\u7684\u7c7b\u578b\uff1a 1 print ( 0u ); // will call print(unsigned int) since 'u' suffix is unsigned int, so this is now an exact match \u5e38\u7528\u7684\u540e\u7f00\u53c2\u89c1\uff1a 4-15-Literals#\u5b57\u9762\u91cf\u540e\u7f00","title":"\u5904\u7406\u4e0d\u660e\u786e\u5339\u914d"},{"location":"8-11-Function-overload-resolution-and-ambiguous-matches/#_5","text":"\u5982\u679c\u6709\u591a\u4e2a\u53c2\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u4f9d\u6b21\u5bf9\u6bcf\u4e2a\u53c2\u6570\u5e94\u7528\u5339\u914d\u89c4\u5219\u3002\u6700\u7ec8\u5339\u914d\u7684\u51fd\u6570\u7684\u8981\u6c42\u662f\uff1a\u6bcf\u4e2a\u53c2\u6570\u90fd\u5339\u914d\u4e14\u81f3\u5c11\u6709\u4e00\u4e2a\u53c2\u6570\u6bd4\u6240\u6709\u5176\u4ed6\u51fd\u6570\u5339\u914d\u5f97\u66f4\u597d\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6240\u9009\u51fd\u6570\u5fc5\u987b\u5bf9\u81f3\u5c11\u4e00\u4e2a\u53c2\u6570\u63d0\u4f9b\u6bd4\u6240\u6709\u5176\u4ed6\u5019\u9009\u51fd\u6570\u66f4\u597d\u7684\u5339\u914d\uff0c\u800c\u5bf9\u6240\u6709\u5176\u4ed6\u53c2\u6570\u4e0d\u5dee\u3002 \u5728\u627e\u5230\u8fd9\u6837\u4e00\u4e2a\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u5b83\u663e\u7136\u662f\u6700\u597d\u7684\u9009\u62e9\u3002\u5982\u679c\u627e\u4e0d\u5230\u8fd9\u6837\u7684\u51fd\u6570\uff0c\u5219\u8be5\u8c03\u7528\u5c06\u88ab\u89c6\u4e3a\u4e8c\u4e49\u6027(\u6216\u4e0d\u5339\u914d)\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include void print ( char c , int x ) { std :: cout << 'a' ; } void print ( char c , double x ) { std :: cout << 'b' ; } void print ( char c , float x ) { std :: cout << 'c' ; } int main () { print ( 'x' , 'a' ); } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u90fd\u80fd\u591f \u5b8c\u5168\u5339\u914d \u3002\u7b2c\u4e00\u4e2a\u51fd\u6570\u901a\u8fc7 \u6570\u503c\u63d0\u5347 \u53ef\u4ee5\u5339\u914d\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u5176\u4ed6\u51e0\u4e2a\u51fd\u6570\u5219\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u624d\u80fd\u5339\u914d\u3002\u56e0\u6b64 print(char, int) \u6bcb\u5eb8\u7f6e\u7591\u662f\u6700\u4f73\u5339\u914d\u51fd\u6570\u3002","title":"\u591a\u53c2\u6570\u91cd\u8f7d\u51fd\u6570\u7684\u5339\u914d"},{"location":"8-12-Default-arguments/","tags":["default"],"text":"Key Takeaway \u9ed8\u8ba4\u53c2\u6570\uff1a void print(int x, int y=10) // 10 is the default argument \u9ed8\u8ba4\u53c2\u6570\u53ea\u80fd\u63d0\u4f9b\u7ed9\u6700\u53f3\u8fb9\u7684\u5f62\u53c2 \u5982\u679c\u51fd\u6570\u6709\u524d\u5411\u58f0\u660e(\u7279\u522b\u662f\u5728\u5934\u6587\u4ef6\u4e2d)\uff0c\u5219\u5c06\u9ed8\u8ba4\u53c2\u6570\u653e\u5728\u90a3\u91cc\u3002\u5426\u5219\uff0c\u5c06\u9ed8\u8ba4\u5b9e\u53c2\u653e\u5165\u51fd\u6570\u5b9a\u4e49\u4e2d\u3002 \u5e26\u6709\u9ed8\u8ba4\u53c2\u6570\u7684\u51fd\u6570\u53ef\u4ee5\u91cd\u8f7d\uff0c\u4f46\u662f\u5bb9\u6613\u9020\u6210 \u4e0d\u660e\u786e\u5339\u914d \u9ed8\u8ba4 \u5b9e\u53c2 \u662f\u4e3a\u51fd\u6570 \u5f62\u53c2 \u63d0\u4f9b\u7684\u9ed8\u8ba4\u503c\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 void print ( int x , int y = 10 ) // 10 is the default argument { std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; } \u5728\u8fdb\u884c\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u8c03\u7528\u65b9\u53ef\u4ee5\u6709\u9009\u62e9\u5730\u4e3a\u4efb\u4f55\u5177\u6709\u9ed8\u8ba4\u5b9e\u53c2\u7684\u51fd\u6570\u5f62\u53c2\u63d0\u4f9b\u5b9e\u53c2\u3002\u5982\u679c\u8c03\u7528\u65b9\u63d0\u4f9b\u4e86\u53c2\u6570\uff0c\u5219\u4f7f\u7528\u51fd\u6570\u8c03\u7528\u4e2d\u53c2\u6570\u7684\u503c\u3002\u5982\u679c\u8c03\u7528\u65b9\u6ca1\u6709\u63d0\u4f9b\u5b9e\u53c2\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u5b9e\u53c2\u7684\u503c\u3002 \u8003\u8651\u4ee5\u4e0b\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include void print ( int x , int y = 4 ) // 4 is the default argument { std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; } int main () { print ( 1 , 2 ); // y will use user-supplied argument 2 print ( 3 ); // y will use default argument 4 } \u8fd9\u4e2a\u7a0b\u5e8f\u4ea7\u751f\u4ee5\u4e0b\u8f93\u51fa\uff1a 1 2 3 4 x: 1 y: 2 x: 3 y: 4 \u5728\u7b2c\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u4e2d\uff0c\u8c03\u7528\u8005\u4e3a\u4e24\u4e2a\u5f62\u53c2\u63d0\u4f9b\u4e86\u663e\u5f0f\u53c2\u6570\uff0c\u56e0\u6b64\u4f7f\u7528\u4e86\u8fd9\u4e9b\u53c2\u6570\u503c\u3002\u5728\u7b2c\u4e8c\u4e2a\u51fd\u6570\u8c03\u7528\u4e2d\uff0c\u8c03\u7528\u8005\u7701\u7565\u4e86\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u56e0\u6b64\u4f7f\u7528\u4e86\u9ed8\u8ba4\u503c 4 \u3002 \u6ce8\u610f\uff0c\u5fc5\u987b\u4f7f\u7528\u7b49\u53f7\u6765\u6307\u5b9a\u9ed8\u8ba4\u53c2\u6570\u3002\u4f7f\u7528\u5706\u62ec\u53f7\u6216\u5927\u62ec\u53f7\u521d\u59cb\u5316\u4e0d\u8d77\u4f5c\u7528: 1 2 3 void foo ( int x = 5 ); // ok void goo ( int x ( 5 )); // compile error void boo ( int x { 5 }); // compile error \u4f55\u65f6\u4f7f\u7528\u9ed8\u8ba4\u5b9e\u53c2 \u5f53\u51fd\u6570\u9700\u8981\u4e00\u4e2a\u5408\u7406\u7684\u9ed8\u8ba4\u503c\u7684\u503c\u65f6\uff0c\u9ed8\u8ba4\u53c2\u6570\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002\u540c\u65f6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u8fd8\u5141\u8bb8\u8c03\u7528\u8005\u4fee\u6539\u8be5\u503c(\u5982\u679c\u4ed6\u4eec\u613f\u610f\u7684\u8bdd)\u3002 \u4f8b\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u7528\u9ed8\u8ba4\u5b9e\u53c2\u7684\u51fd\u6570\u539f\u578b: 1 2 int rollDie ( int sides = 6 ); void openLogFile ( std :: string filename = \"default.log\" ); \u4f5c\u8005\u6ce8 \u56e0\u4e3a\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u662f\u63d0\u4f9b\u4e00\u4e2a\u7279\u5b9a\u7684\u5b9e\u53c2\u503c\u8fd8\u662f\u4f7f\u7528\u9ed8\u8ba4\u503c\uff0c\u6240\u4ee5\u63d0\u4f9b\u4e86\u9ed8\u8ba4\u503c\u7684\u5f62\u53c2\u6709\u65f6\u88ab\u79f0\u4e3a \u53ef\u9009\u5f62\u53c2 \u3002\u4f46\u662f\uff0c\u672f\u8bed \u53ef\u9009\u53c2\u6570(optional parameter) \u4e5f\u7528\u4e8e\u6307\u4ee3\u5176\u4ed6\u51e0\u79cd\u7c7b\u578b\u7684\u53c2\u6570(\u5305\u62ec\u901a\u8fc7 \u4f20\u5730\u5740(pass-by-address) \u65b9\u5f0f\u4f20\u9012\u7684\u53c2\u6570\uff0c\u4ee5\u53ca\u4f7f\u7528 std:: optional \u4f20\u9012\u7684\u53c2\u6570)\uff0c\u6240\u4ee5\u5efa\u8bae\u907f\u514d\u4f7f\u7528\u8fd9\u4e2a\u672f\u8bed\u3002 \u591a\u4e2a\u9ed8\u8ba4\u53c2\u6570 \u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u6709\u591a\u4e2a\u5e26\u9ed8\u8ba4\u5b9e\u53c2\u7684\u5f62\u53c2: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include void print ( int x = 10 , int y = 20 , int z = 30 ) { std :: cout << \"Values: \" << x << \" \" << y << \" \" << z << '\\n' ; } int main () { print ( 1 , 2 , 3 ); // all explicit arguments print ( 1 , 2 ); // rightmost argument defaulted print ( 1 ); // two rightmost arguments defaulted print (); // all arguments defaulted return 0 ; } \u4e0a\u8ff0\u4ee3\u7801\u8f93\u51fa\u7684\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 Values: 1 2 3 Values: 1 2 30 Values: 1 20 30 Values: 10 20 30 C++ \u5e76\u4e0d\u652f\u6301\u5f62\u5982 print(,,3) \u8bed\u6cd5\u7684\u51fd\u6570\u8c03\u7528\u2014\u2014\u5373\u4e3a z \u63d0\u4f9b\u4e00\u4e2a\u503c\uff0c\u540c\u65f6\u4f7f\u7528 x \u548c y \u7684\u9ed8\u8ba4\u503c\u3002\u8fd9\u4f1a\u5e26\u6765\u4e24\u4e2a\u95ee\u9898\uff1a \u9ed8\u8ba4\u53c2\u6570\u53ea\u80fd\u63d0\u4f9b\u7ed9\u6700\u53f3\u8fb9\u7684\u5f62\u53c2\u3002\u4e0b\u9762\u7684\u8bed\u6cd5\u662f\u4e0d\u5141\u8bb8\u7684\uff1a 1 void print ( int x = 10 , int y ); // not allowed \u6cd5\u5219 \u9ed8\u8ba4\u53c2\u6570\u53ea\u80fd\u63d0\u4f9b\u7ed9\u6700\u53f3\u8fb9\u7684\u5f62\u53c2\u3002 \u5982\u679c\u9700\u8981\u4e3a\u51fd\u6570\u8bbe\u7f6e\u591a\u4e2a\u9ed8\u8ba4\u53c2\u6570\uff0c\u5219\u6700\u5de6\u8fb9\u7684\u9ed8\u8ba4\u53c2\u6570\u5e94\u8be5\u662f\u7528\u6237\u6700\u6709\u53ef\u80fd\u8fdb\u884c \u4e3b\u52a8\u8986\u76d6 \u7684\u53c2\u6570\u3002 \u9ed8\u8ba4\u5b9e\u53c2\u4e0d\u80fd\u88ab\u91cd\u590d\u58f0\u660e \u4e00\u65e6\u58f0\u660e\u5b8c\u6210\uff0c\u9ed8\u8ba4\u5b9e\u53c2\u5c31\u4e0d\u80fd(\u5728\u540c\u4e00\u4e2a\u6587\u4ef6\u4e2d)\u88ab\u91cd\u65b0\u58f0\u660e\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u4e8e\u5177\u6709 \u524d\u5411\u58f0\u660e \u548c\u51fd\u6570\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u9ed8\u8ba4\u5b9e\u53c2\u53ef\u4ee5\u5728\u524d\u5411\u58f0\u660e\u6216\u51fd\u6570\u5b9a\u4e49\u4e2d\u58f0\u660e\uff0c\u4f46\u4e0d\u80fd\u540c\u65f6\u5728\u4e24\u8005\u4e2d\u58f0\u660e\u3002 1 2 3 4 5 6 7 8 9 #include void print ( int x , int y = 4 ); // forward declaration void print ( int x , int y = 4 ) // error: redefinition of default argument { std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; } \u6700\u4f73\u5b9e\u8df5 \u662f\u5728\u524d\u5411\u58f0\u660e\u800c\u4e0d\u662f\u5728\u5b9a\u4e49\u4e2d\u58f0\u660e\u9ed8\u8ba4\u53c2\u6570\uff0c\u56e0\u4e3a\u524d\u5411\u58f0\u660e\u66f4\u5bb9\u6613\u88ab\u5176\u4ed6\u6587\u4ef6\u770b\u5230(\u7279\u522b\u662f\u5728\u5934\u6587\u4ef6\u4e2d)\u3002 foo.h 1 2 3 4 #ifndef FOO_H #define FOO_H void print ( int x , int y = 4 ); #endif main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include \"foo.h\" #include void print ( int x , int y ) { std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; } int main () { print ( 5 ); return 0 ; } \u6ce8\u610f\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u80fd\u591f\u4f7f\u7528\u51fd\u6570 print() \u7684\u9ed8\u8ba4\u5b9e\u53c2\uff0c\u56e0\u4e3a main.cpp #include \u4e86 foo.h \uff0c foo.h \u7684\u524d\u5411\u58f0\u660e\u5b9a\u4e49\u4e86\u9ed8\u8ba4\u5b9e\u53c2\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u51fd\u6570\u6709\u524d\u5411\u58f0\u660e(\u7279\u522b\u662f\u5728\u5934\u6587\u4ef6\u4e2d)\uff0c\u5219\u5c06\u9ed8\u8ba4\u53c2\u6570\u653e\u5728\u90a3\u91cc\u3002\u5426\u5219\uff0c\u5c06\u9ed8\u8ba4\u5b9e\u53c2\u653e\u5165\u51fd\u6570\u5b9a\u4e49\u4e2d\u3002 \u9ed8\u8ba4\u53c2\u6570\u548c\u51fd\u6570\u91cd\u8f7d \u5e26\u6709\u9ed8\u8ba4\u53c2\u6570\u7684\u51fd\u6570\u53ef\u4ee5\u91cd\u8f7d\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u662f\u5141\u8bb8\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 void print ( std :: string string ) { } void print ( char ch = ' ' ) { } int main () { print ( \"Hello, world\" ); // resolves to print(std::string) print ( 'a' ); // resolves to print(char) print (); // resolves to print(char) return 0 ; } print() \u51fd\u6570\u7684\u6548\u679c\u548c\u7528\u6237\u663e\u5f0f\u8c03\u7528 print(' ') \u662f\u4e00\u6837\u7684\uff0c\u5b83\u4f1a\u5339\u914d print(char) \u3002 \u518d\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 void print ( int x ); void print ( int x , int y = 10 ); void print ( int x , double y = 20.5 ); \u5e26\u6709\u9ed8\u8ba4\u503c\u7684\u53c2\u6570\u53ef\u4ee5\u7528\u4e8e\u533a\u5206\u51fd\u6570\u91cd\u8f7d(\u610f\u5473\u7740\u4e0a\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u7f16\u8bd1)\u3002\u7136\u800c\uff0c\u8fd9\u6837\u7684\u51fd\u6570\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6f5c\u5728\u7684 \u4e0d\u660e\u786e\u5339\u914d \uff0c\u4f8b\u5982\uff1a 1 2 3 print ( 1 , 2 ); // will resolve to print(int, int) print ( 1 , 2.5 ); // will resolve to print(int, double) print ( 1 ); // ambiguous function call \u5728\u6700\u540e\u4e00\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u5224\u65ad print(1) \u662f\u5e94\u8be5\u89e3\u6790\u4e3a print(int) \u8fd8\u662f\u7b2c\u4e8c\u4e2a\u5f62\u53c2\u6709\u9ed8\u8ba4\u503c\u7684\u4e24\u4e2a\u51fd\u6570\uff08\u4e4b\u4e00\uff09\uff0c\u5bfc\u81f4\u4e00\u4e2a\u4e0d\u660e\u786e\u5339\u914d\u9519\u8bef\u3002 \u5c0f\u7ed3 \u9ed8\u8ba4\u53c2\u6570\u63d0\u4f9b\u4e86\u4e00\u79cd\u6709\u7528\u7684\u673a\u5236\uff0c\u53ef\u4ee5\u4e3a\u7528\u6237\u5e0c\u671b\u6216\u4e0d\u5e0c\u671b\u91cd\u5199\u7684\u53c2\u6570\u6307\u5b9a\u9ed8\u8ba4\u503c\uff0c\u8fd9\u5728 C++\u4e2d\u5f88\u5e38\u89c1\uff0c\u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\u4f60\u4f1a\u770b\u5230\u5f88\u591a\u3002","title":"8.12 - \u9ed8\u8ba4\u53c2\u6570"},{"location":"8-12-Default-arguments/#_1","text":"\u5f53\u51fd\u6570\u9700\u8981\u4e00\u4e2a\u5408\u7406\u7684\u9ed8\u8ba4\u503c\u7684\u503c\u65f6\uff0c\u9ed8\u8ba4\u53c2\u6570\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002\u540c\u65f6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u8fd8\u5141\u8bb8\u8c03\u7528\u8005\u4fee\u6539\u8be5\u503c(\u5982\u679c\u4ed6\u4eec\u613f\u610f\u7684\u8bdd)\u3002 \u4f8b\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u7528\u9ed8\u8ba4\u5b9e\u53c2\u7684\u51fd\u6570\u539f\u578b: 1 2 int rollDie ( int sides = 6 ); void openLogFile ( std :: string filename = \"default.log\" ); \u4f5c\u8005\u6ce8 \u56e0\u4e3a\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u662f\u63d0\u4f9b\u4e00\u4e2a\u7279\u5b9a\u7684\u5b9e\u53c2\u503c\u8fd8\u662f\u4f7f\u7528\u9ed8\u8ba4\u503c\uff0c\u6240\u4ee5\u63d0\u4f9b\u4e86\u9ed8\u8ba4\u503c\u7684\u5f62\u53c2\u6709\u65f6\u88ab\u79f0\u4e3a \u53ef\u9009\u5f62\u53c2 \u3002\u4f46\u662f\uff0c\u672f\u8bed \u53ef\u9009\u53c2\u6570(optional parameter) \u4e5f\u7528\u4e8e\u6307\u4ee3\u5176\u4ed6\u51e0\u79cd\u7c7b\u578b\u7684\u53c2\u6570(\u5305\u62ec\u901a\u8fc7 \u4f20\u5730\u5740(pass-by-address) \u65b9\u5f0f\u4f20\u9012\u7684\u53c2\u6570\uff0c\u4ee5\u53ca\u4f7f\u7528 std:: optional \u4f20\u9012\u7684\u53c2\u6570)\uff0c\u6240\u4ee5\u5efa\u8bae\u907f\u514d\u4f7f\u7528\u8fd9\u4e2a\u672f\u8bed\u3002","title":"\u4f55\u65f6\u4f7f\u7528\u9ed8\u8ba4\u5b9e\u53c2"},{"location":"8-12-Default-arguments/#_2","text":"\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u6709\u591a\u4e2a\u5e26\u9ed8\u8ba4\u5b9e\u53c2\u7684\u5f62\u53c2: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include void print ( int x = 10 , int y = 20 , int z = 30 ) { std :: cout << \"Values: \" << x << \" \" << y << \" \" << z << '\\n' ; } int main () { print ( 1 , 2 , 3 ); // all explicit arguments print ( 1 , 2 ); // rightmost argument defaulted print ( 1 ); // two rightmost arguments defaulted print (); // all arguments defaulted return 0 ; } \u4e0a\u8ff0\u4ee3\u7801\u8f93\u51fa\u7684\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4 Values: 1 2 3 Values: 1 2 30 Values: 1 20 30 Values: 10 20 30 C++ \u5e76\u4e0d\u652f\u6301\u5f62\u5982 print(,,3) \u8bed\u6cd5\u7684\u51fd\u6570\u8c03\u7528\u2014\u2014\u5373\u4e3a z \u63d0\u4f9b\u4e00\u4e2a\u503c\uff0c\u540c\u65f6\u4f7f\u7528 x \u548c y \u7684\u9ed8\u8ba4\u503c\u3002\u8fd9\u4f1a\u5e26\u6765\u4e24\u4e2a\u95ee\u9898\uff1a \u9ed8\u8ba4\u53c2\u6570\u53ea\u80fd\u63d0\u4f9b\u7ed9\u6700\u53f3\u8fb9\u7684\u5f62\u53c2\u3002\u4e0b\u9762\u7684\u8bed\u6cd5\u662f\u4e0d\u5141\u8bb8\u7684\uff1a 1 void print ( int x = 10 , int y ); // not allowed \u6cd5\u5219 \u9ed8\u8ba4\u53c2\u6570\u53ea\u80fd\u63d0\u4f9b\u7ed9\u6700\u53f3\u8fb9\u7684\u5f62\u53c2\u3002 \u5982\u679c\u9700\u8981\u4e3a\u51fd\u6570\u8bbe\u7f6e\u591a\u4e2a\u9ed8\u8ba4\u53c2\u6570\uff0c\u5219\u6700\u5de6\u8fb9\u7684\u9ed8\u8ba4\u53c2\u6570\u5e94\u8be5\u662f\u7528\u6237\u6700\u6709\u53ef\u80fd\u8fdb\u884c \u4e3b\u52a8\u8986\u76d6 \u7684\u53c2\u6570\u3002","title":"\u591a\u4e2a\u9ed8\u8ba4\u53c2\u6570"},{"location":"8-12-Default-arguments/#_3","text":"\u4e00\u65e6\u58f0\u660e\u5b8c\u6210\uff0c\u9ed8\u8ba4\u5b9e\u53c2\u5c31\u4e0d\u80fd(\u5728\u540c\u4e00\u4e2a\u6587\u4ef6\u4e2d)\u88ab\u91cd\u65b0\u58f0\u660e\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u4e8e\u5177\u6709 \u524d\u5411\u58f0\u660e \u548c\u51fd\u6570\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u9ed8\u8ba4\u5b9e\u53c2\u53ef\u4ee5\u5728\u524d\u5411\u58f0\u660e\u6216\u51fd\u6570\u5b9a\u4e49\u4e2d\u58f0\u660e\uff0c\u4f46\u4e0d\u80fd\u540c\u65f6\u5728\u4e24\u8005\u4e2d\u58f0\u660e\u3002 1 2 3 4 5 6 7 8 9 #include void print ( int x , int y = 4 ); // forward declaration void print ( int x , int y = 4 ) // error: redefinition of default argument { std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; } \u6700\u4f73\u5b9e\u8df5 \u662f\u5728\u524d\u5411\u58f0\u660e\u800c\u4e0d\u662f\u5728\u5b9a\u4e49\u4e2d\u58f0\u660e\u9ed8\u8ba4\u53c2\u6570\uff0c\u56e0\u4e3a\u524d\u5411\u58f0\u660e\u66f4\u5bb9\u6613\u88ab\u5176\u4ed6\u6587\u4ef6\u770b\u5230(\u7279\u522b\u662f\u5728\u5934\u6587\u4ef6\u4e2d)\u3002 foo.h 1 2 3 4 #ifndef FOO_H #define FOO_H void print ( int x , int y = 4 ); #endif main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include \"foo.h\" #include void print ( int x , int y ) { std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; } int main () { print ( 5 ); return 0 ; } \u6ce8\u610f\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u80fd\u591f\u4f7f\u7528\u51fd\u6570 print() \u7684\u9ed8\u8ba4\u5b9e\u53c2\uff0c\u56e0\u4e3a main.cpp #include \u4e86 foo.h \uff0c foo.h \u7684\u524d\u5411\u58f0\u660e\u5b9a\u4e49\u4e86\u9ed8\u8ba4\u5b9e\u53c2\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u51fd\u6570\u6709\u524d\u5411\u58f0\u660e(\u7279\u522b\u662f\u5728\u5934\u6587\u4ef6\u4e2d)\uff0c\u5219\u5c06\u9ed8\u8ba4\u53c2\u6570\u653e\u5728\u90a3\u91cc\u3002\u5426\u5219\uff0c\u5c06\u9ed8\u8ba4\u5b9e\u53c2\u653e\u5165\u51fd\u6570\u5b9a\u4e49\u4e2d\u3002","title":"\u9ed8\u8ba4\u5b9e\u53c2\u4e0d\u80fd\u88ab\u91cd\u590d\u58f0\u660e"},{"location":"8-12-Default-arguments/#_4","text":"\u5e26\u6709\u9ed8\u8ba4\u53c2\u6570\u7684\u51fd\u6570\u53ef\u4ee5\u91cd\u8f7d\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u662f\u5141\u8bb8\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 void print ( std :: string string ) { } void print ( char ch = ' ' ) { } int main () { print ( \"Hello, world\" ); // resolves to print(std::string) print ( 'a' ); // resolves to print(char) print (); // resolves to print(char) return 0 ; } print() \u51fd\u6570\u7684\u6548\u679c\u548c\u7528\u6237\u663e\u5f0f\u8c03\u7528 print(' ') \u662f\u4e00\u6837\u7684\uff0c\u5b83\u4f1a\u5339\u914d print(char) \u3002 \u518d\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 void print ( int x ); void print ( int x , int y = 10 ); void print ( int x , double y = 20.5 ); \u5e26\u6709\u9ed8\u8ba4\u503c\u7684\u53c2\u6570\u53ef\u4ee5\u7528\u4e8e\u533a\u5206\u51fd\u6570\u91cd\u8f7d(\u610f\u5473\u7740\u4e0a\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u7f16\u8bd1)\u3002\u7136\u800c\uff0c\u8fd9\u6837\u7684\u51fd\u6570\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6f5c\u5728\u7684 \u4e0d\u660e\u786e\u5339\u914d \uff0c\u4f8b\u5982\uff1a 1 2 3 print ( 1 , 2 ); // will resolve to print(int, int) print ( 1 , 2.5 ); // will resolve to print(int, double) print ( 1 ); // ambiguous function call \u5728\u6700\u540e\u4e00\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u5224\u65ad print(1) \u662f\u5e94\u8be5\u89e3\u6790\u4e3a print(int) \u8fd8\u662f\u7b2c\u4e8c\u4e2a\u5f62\u53c2\u6709\u9ed8\u8ba4\u503c\u7684\u4e24\u4e2a\u51fd\u6570\uff08\u4e4b\u4e00\uff09\uff0c\u5bfc\u81f4\u4e00\u4e2a\u4e0d\u660e\u786e\u5339\u914d\u9519\u8bef\u3002","title":"\u9ed8\u8ba4\u53c2\u6570\u548c\u51fd\u6570\u91cd\u8f7d"},{"location":"8-12-Default-arguments/#_5","text":"\u9ed8\u8ba4\u53c2\u6570\u63d0\u4f9b\u4e86\u4e00\u79cd\u6709\u7528\u7684\u673a\u5236\uff0c\u53ef\u4ee5\u4e3a\u7528\u6237\u5e0c\u671b\u6216\u4e0d\u5e0c\u671b\u91cd\u5199\u7684\u53c2\u6570\u6307\u5b9a\u9ed8\u8ba4\u503c\uff0c\u8fd9\u5728 C++\u4e2d\u5f88\u5e38\u89c1\uff0c\u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\u4f60\u4f1a\u770b\u5230\u5f88\u591a\u3002","title":"\u5c0f\u7ed3"},{"location":"8-13-Function-templates/","tags":["Function templates"],"text":"Key Takeaway \u5047\u8bbe\u6211\u4eec\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u6c42\u4e24\u4e2a\u6570\u4e2d\u54ea\u4e2a\u503c\u8f83\u5927\u7684\u51fd\u6570\uff0c\u90a3\u4e48\u6b64\u65f6\u53ef\u80fd\u4f1a\u8fd9\u6837\u505a\uff1a 1 2 3 4 int max ( int x , int y ) { return ( x > y ) ? x : y ; } \u867d\u7136\u8c03\u7528\u8005\u53ef\u4ee5\u5411\u51fd\u6570\u4f20\u9012\u4e0d\u540c\u7684\u503c\uff0c\u4f46\u5f62\u53c2\u7684\u7c7b\u578b\u662f\u56fa\u5b9a\u7684\uff0c\u56e0\u6b64\u8c03\u7528\u8005\u53ea\u80fd\u4f20\u9012 int \u503c\u3002\u8fd9\u610f\u5473\u7740\u8fd9\u4e2a\u51fd\u6570\u5b9e\u9645\u4e0a\u53ea\u9002\u7528\u4e8e\u6574\u6570(\u4ee5\u53ca\u53ef\u4ee5\u63d0\u5347\u4e3a int \u7684\u7c7b\u578b)\u3002 \u90a3\u4e48\uff0c\u5f53\u4f60\u60f3\u627e\u5230\u4e24\u4e2a double \u503c\u7684\u6700\u5927\u503c\u65f6\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f\u56e0\u4e3aC++\u8981\u6c42\u6211\u4eec\u6307\u5b9a\u6240\u6709\u51fd\u6570\u5f62\u53c2\u7684\u7c7b\u578b\uff0c\u89e3\u51b3\u65b9\u6848\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u91cd\u8f7d\u7248\u672c\u7684 max() \uff0c\u5f62\u53c2\u7c7b\u578b\u4e3a double \uff1a 1 2 3 4 double max ( double x , double y ) { return ( x > y ) ? x : y ; } \u6ce8\u610f\uff0c\u5b9e\u73b0 max() \u7684 double \u7248\u672c\u7684\u4ee3\u7801\u4e0e max() \u7684 int \u7248\u672c\u7684\u4ee3\u7801\u5b8c\u5168\u76f8\u540c\uff01\u4e8b\u5b9e\u4e0a\uff0c\u8fd9\u4e2a\u5b9e\u73b0\u9002\u7528\u4e8e\u8bb8\u591a\u4e0d\u540c\u7684\u7c7b\u578b\uff1a\u5305\u62ec int \u3001 double \u3001 long \u3001 long double \u751a\u81f3\u662f\u4f60\u81ea\u5df1\u521b\u5efa\u7684\u65b0\u7c7b\u578b(\u6211\u4eec\u5c06\u5728\u672a\u6765\u7684\u8bfe\u7a0b\u4e2d\u4ecb\u7ecd\u5982\u4f55\u505a)\u3002 \u5fc5\u987b\u4e3a\u6bcf\u4e00\u7ec4\u53c2\u6570\u7c7b\u578b\u4f7f\u7528\u76f8\u540c\u7684\u5b9e\u73b0\u6765\u521b\u5efa\u91cd\u8f7d\u51fd\u6570\uff0c\u8fd9\u662f\u4e00\u4e2a\u4ee4\u4eba\u5934\u75db\u7684\u95ee\u9898\uff0c\u4e5f\u662f\u9519\u8bef\u7684\u6765\u6e90\uff0c\u5e76\u4e14\u660e\u663e\u8fdd\u53cd\u4e86DRY(\u4e0d\u8981\u91cd\u590d\u81ea\u5df1)\u539f\u5219\u3002\u8fd9\u91cc\u8fd8\u6709\u4e00\u4e2a\u4e0d\u592a\u660e\u663e\u7684\u6311\u6218\uff1a\u5e0c\u671b\u4f7f\u7528 max() \u51fd\u6570\u7684\u7a0b\u5e8f\u5458\u53ef\u80fd\u5e0c\u671b\u7528 max() \u7684\u4f5c\u8005\u6ca1\u6709\u9884\u6599\u5230\u7684\u5f62\u53c2\u7c7b\u578b\u8c03\u7528\u5b83(\u56e0\u6b64\u6ca1\u6709\u4e3a\u6b64\u7f16\u5199\u91cd\u8f7d\u51fd\u6570)\u3002 \u6211\u4eec\u9700\u8981\u7684\u662f\u7f16\u5199\u5355\u4e00\u7248\u672c\u7684 max() \uff0c\u5b83\u53ef\u4ee5\u5904\u7406\u4efb\u4f55\u7c7b\u578b\u7684\u53c2\u6570(\u751a\u81f3\u662f\u5728\u7f16\u5199 max() \u4ee3\u7801\u65f6\u53ef\u80fd\u6ca1\u6709\u9884\u6599\u5230\u7684\u7c7b\u578b)\u3002\u6b63\u5e38\u7684\u529f\u80fd\u6839\u672c\u65e0\u6cd5\u80dc\u4efb\u8fd9\u91cc\u7684\u4efb\u52a1\u3002\u5e78\u8fd0\u7684\u662f\uff0cC++\u652f\u6301\u53e6\u4e00\u4e2a\u4e13\u95e8\u4e3a\u89e3\u51b3\u8fd9\u7c7b\u95ee\u9898\u800c\u8bbe\u8ba1\u7684\u7279\u6027\u3002 \u6b22\u8fce\u6765\u5230C++\u6a21\u677f\u7684\u4e16\u754c\u3002 C++\u6a21\u677f\u7b80\u4ecb \u5728C++\u4e2d\uff0c\u6a21\u677f\u7cfb\u7edf\u88ab\u8bbe\u8ba1\u7528\u6765\u7b80\u5316\u521b\u5efa\u80fd\u591f\u5904\u7406\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u51fd\u6570(\u6216\u7c7b)\u7684\u8fc7\u7a0b\u3002 \u6211\u4eec\u4e0d\u518d\u662f\u624b\u52a8\u521b\u5efa\u4e00\u5806\u51e0\u4e4e\u76f8\u540c\u7684\u51fd\u6570\u6216\u7c7b(\u6bcf\u4e00\u7ec4\u4e0d\u540c\u7c7b\u578b\u5bf9\u5e94\u4e00\u4e2a)\uff0c\u800c\u662f\u521b\u5efa\u4e00\u4e2a\u5355\u4e00\u7684\u201c\u6a21\u677f\u201d\u3002\u5c31\u50cf\u666e\u901a\u7684\u5b9a\u4e49\u4e00\u6837\uff0c\u6a21\u677f\u63cf\u8ff0\u51fd\u6570\u6216\u7c7b\u7684\u6837\u5b50\u3002\u4e0e\u5e38\u89c4\u5b9a\u4e49(\u5fc5\u987b\u6307\u5b9a\u6240\u6709\u7c7b\u578b)\u4e0d\u540c\uff0c\u5728\u6a21\u677f\u4e2d\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u6216\u591a\u4e2a\u5360\u4f4d\u7b26\u7c7b\u578b\u3002\u5360\u4f4d\u7b26\u7c7b\u578b\u8868\u793a\u5728\u7f16\u5199\u6a21\u677f\u65f6\u4e0d\u77e5\u9053\u4f46\u5c06\u5728\u540e\u9762\u63d0\u4f9b\u7684\u7c7b\u578b\u3002 \u4e00\u65e6\u5b9a\u4e49\u4e86\u6a21\u677f\uff0c\u7f16\u8bd1\u5668\u5c31\u53ef\u4ee5\u6839\u636e\u9700\u8981\uff0c\u4f7f\u7528\u6a21\u677f\u751f\u6210\u4efb\u610f\u591a\u7684\u91cd\u8f7d\u51fd\u6570(\u6216\u7c7b)\uff0c\u6bcf\u4e2a\u51fd\u6570\u4f7f\u7528\u4e0d\u540c\u7684\u5b9e\u9645\u7c7b\u578b\uff01 \u6700\u7ec8\u7684\u7ed3\u679c\u662f\u4e00\u6837\u7684\u2014\u2014\u6211\u4eec\u5f97\u5230\u4e00\u5806\u51e0\u4e4e\u76f8\u540c\u7684\u51fd\u6570\u6216\u7c7b(\u6bcf\u4e00\u7ec4\u4e0d\u540c\u7684\u7c7b\u578b\u5bf9\u5e94\u4e00\u4e2a)\u3002\u4f46\u662f\u6211\u4eec\u53ea\u9700\u8981\u521b\u5efa\u548c\u7ef4\u62a4\u4e00\u4e2a\u6a21\u677f\uff0c\u7f16\u8bd1\u5668\u4f1a\u5b8c\u6210\u6240\u6709\u8270\u96be\u7684\u5de5\u4f5c\u3002 \u5173\u952e\u4fe1\u606f \u7f16\u8bd1\u5668\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u6a21\u677f\u6765\u751f\u6210\u4e00\u7cfb\u5217\u76f8\u5173\u7684\u51fd\u6570\u6216\u7c7b\uff0c\u6bcf\u4e2a\u51fd\u6570\u6216\u7c7b\u4f7f\u7528\u4e00\u7ec4\u4e0d\u540c\u7684\u7c7b\u578b\u3002 \u9898\u5916\u8bdd Because the concept behind templates can be hard to describe in words, let\u2019s try an analogy. If you were to look up the word \u201ctemplate\u201d in the dictionary, you\u2019d find a definition that was similar to the following: \u201ca template is a model that serves as a pattern for creating similar objects\u201d. One type of template that is very easy to understand is that of a stencil. A stencil is a thin piece of material (such as a piece of cardboard or plastic) with a shape cut out of it (e.g. a happy face). By placing the stencil on top of another object, then spraying paint through the hole, you can very quickly replicate the cut-out shape. The stencil itself only needs to be created once, and then it can be reused as many times as desired, to create the cut out shape in as many different colors as you like. Even better, the color of a shape made with the stencil doesn\u2019t have to be determined until the stencil is actually used. A template is essentially a stencil for creating functions or classes. We create the template (our stencil) once, and then we can use it as many times as needed, to stencil out a function or class for a specific set of actual types. Those actual types don\u2019t need to be determined until the template is actually used. \u56e0\u4e3a\u53ea\u6709\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u6a21\u677f\u65f6(\u800c\u4e0d\u662f\u5728\u7f16\u5199\u6a21\u677f\u65f6)\u624d\u80fd\u786e\u5b9a\u5b9e\u9645\u7c7b\u578b\uff0c\u56e0\u6b64\u6a21\u677f\u7684\u4f5c\u8005\u4e0d\u5fc5\u5c1d\u8bd5\u9884\u6d4b\u53ef\u80fd\u4f7f\u7528\u7684\u6240\u6709\u5b9e\u9645\u7c7b\u578b\u3002\u8fd9\u610f\u5473\u7740\u6a21\u677f\u4ee3\u7801\u53ef\u4ee5\u4e0e\u7f16\u5199\u6a21\u677f\u65f6\u751a\u81f3\u4e0d\u5b58\u5728\u7684\u7c7b\u578b\u4e00\u8d77\u4f7f\u7528\uff01\u7a0d\u540e\uff0c\u5f53\u5f00\u59cb\u63a2\u7d22C++\u6807\u51c6\u5e93\u65f6\uff0c\u6211\u4eec\u5c06\u770b\u5230\u8fd9\u662f\u5982\u4f55\u6d3e\u4e0a\u7528\u573a\u7684\uff0cC++\u6807\u51c6\u5e93\u5145\u6ee1\u4e86\u6a21\u677f\u4ee3\u7801! \u5173\u952e\u4fe1\u606f \u6a21\u677f\u53ef\u4ee5\u4f7f\u7528\u5728\u7f16\u5199\u6a21\u677f\u65f6\u751a\u81f3\u4e0d\u5b58\u5728\u7684\u7c7b\u578b\u3002\u8fd9\u6709\u52a9\u4e8e\u4f7f\u6a21\u677f\u4ee3\u7801\u65e2\u7075\u6d3b\u53c8\u7ecf\u5f97\u8d77\u8003\u9a8c! \u5728\u672c\u8bfe\u7684\u5176\u4f59\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u548c\u63a2\u7d22\u5982\u4f55\u521b\u5efa \u51fd\u6570\u6a21\u677f \uff0c\u5e76\u8be6\u7ec6\u63cf\u8ff0\u5b83\u4eec\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u6211\u4eec\u5c06\u5728\u4ecb\u7ecd\u4e86\u4ec0\u4e48\u662f\u7c7b\u4e4b\u540e\u518d\u8ba8\u8bba \u7c7b\u6a21\u677f \u3002 \u51fd\u6570\u6a21\u677f \u51fd\u6570\u6a21\u677f \u662f\u4e00\u79cd\u7c7b\u4f3c\u51fd\u6570\u7684\u5b9a\u4e49\uff0c\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u6216\u591a\u4e2a\u91cd\u8f7d\u51fd\u6570\uff0c\u6bcf\u4e2a\u91cd\u8f7d\u51fd\u6570\u90fd\u6709\u4e00\u7ec4\u4e0d\u540c\u7684\u5b9e\u9645\u7c7b\u578b\u3002\u8fd9\u5c06\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u53ef\u4ee5\u5904\u7406\u8bb8\u591a\u4e0d\u540c\u7c7b\u578b\u7684\u51fd\u6570\u3002 \u5728\u521b\u5efa\u51fd\u6570\u6a21\u677f\u65f6\uff0c\u5bf9\u4e8e\u6211\u4eec\u5e0c\u671b\u7a0d\u540e\u6307\u5b9a\u7684\u4efb\u4f55\u5f62\u53c2\u7c7b\u578b\u3001\u8fd4\u56de\u7c7b\u578b\u6216\u51fd\u6570\u4f53\u4e2d\u4f7f\u7528\u7684\u7c7b\u578b\uff0c\u6211\u4eec\u90fd\u4f7f\u7528\u5360\u4f4d\u7b26\u7c7b\u578b(\u4e5f\u79f0\u4e3a\u6a21\u677f\u7c7b\u578b)\u3002 \u51fd\u6570\u6a21\u677f\u6700\u597d\u901a\u8fc7\u793a\u4f8b\u6765\u6559\u6388\uff0c\u6240\u4ee5\u8ba9\u6211\u4eec\u5c06\u4e0a\u9762\u793a\u4f8b\u4e2d\u7684 max(int, int) \u51fd\u6570\u8f6c\u6362\u4e3a\u51fd\u6570\u6a21\u677f\u3002\u5b83\u51fa\u5947\u5730\u7b80\u5355\uff0c\u6211\u4eec\u5c06\u89e3\u91ca\u8fd9\u4e00\u8fc7\u7a0b\u4e2d\u53d1\u751f\u4e86\u4ec0\u4e48\u3002 \u521b\u5efa\u6a21\u677f\u5316\u7684 max \u51fd\u6570 \u4e0b\u9762\u4ee3\u7801\u662f int \u7248\u672c\u7684 max() \uff1a 1 2 3 4 int max ( int x , int y ) { return ( x > y ) ? x : y ; } \u6ce8\u610f\uff0c\u6211\u4eec\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\u4f7f\u7528\u4e86\u4e09\u6b21 int \u7c7b\u578b\uff1a\u4e00\u6b21\u7528\u4e8e\u5f62\u53c2 x \uff0c\u4e00\u6b21\u7528\u4e8e\u5f62\u53c2 y \uff0c\u8fd8\u6709\u4e00\u6b21\u7528\u4e8e\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u3002 \u8981\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6a21\u677f\uff0c\u6211\u4eec\u8981\u505a\u4e24\u4ef6\u4e8b\u3002\u9996\u5148\uff0c\u6211\u4eec\u5c06\u7528\u6a21\u677f\u7c7b\u578b\u66ff\u6362\u7279\u5b9a\u7c7b\u578b\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u53ea\u6709\u4e00\u4e2a\u9700\u8981\u66ff\u6362\u7684\u7c7b\u578b( int )\uff0c\u6240\u4ee5\u53ea\u9700\u8981\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\u3002\u901a\u5e38\u7ea6\u5b9a\u4f7f\u7528\u5355\u4e2a\u5927\u5199\u5b57\u6bcd\uff08\u4ece\u5927\u5199\u5b57\u6bcd T \u5f00\u59cb\uff09\u8868\u793a\u6a21\u677f\u7c7b\u578b\u3002 \u4e0b\u9762\u662f\u6211\u4eec\u4f7f\u7528\u5355\u4e00\u6a21\u677f\u7c7b\u578b\u7684\u65b0\u51fd\u6570\uff1a 1 2 3 4 T max ( T x , T y ) // won't compile because we haven't defined T { return ( x > y ) ? x : y ; } \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u5355\u4e2a\u5927\u5199\u5b57\u6bcd(\u4eceT\u5f00\u59cb)\u547d\u540d\u6a21\u677f\u7c7b\u578b(\u4f8b\u5982 T, U, V \u7b49\u2026) \u8fd9\u662f\u4e00\u4e2a\u597d\u7684\u5f00\u59cb\u2014\u2014\u7136\u800c\uff0c\u5b83\u4e0d\u4f1a\u7f16\u8bd1\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053 T \u662f\u4ec0\u4e48\uff01\u8fd9\u4ecd\u7136\u662f\u4e00\u4e2a\u666e\u901a\u7684\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u51fd\u6570\u6a21\u677f\u3002 \u5176\u6b21\uff0c\u6211\u4eec\u4f1a\u544a\u8bc9\u7f16\u8bd1\u5668\u8fd9\u662f\u4e00\u4e2a\u51fd\u6570\u6a21\u677f\uff0c\u800c T \u662f\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\u3002\u8fd9\u662f\u901a\u8fc7\u6240\u8c13\u7684\u6a21\u677f\u53c2\u6570\u58f0\u660e\u6765\u5b8c\u6210\u7684\uff1a 1 2 3 4 5 template < typename T > // this is the template parameter declaration T max ( T x , T y ) // this is the function template definition for max { return ( x > y ) ? x : y ; } \u4ed4\u7ec6\u89c2\u5bdf\u6a21\u677f\u53c2\u6570\u58f0\u660e\u3002\u9996\u5148\u4f7f\u7528\u5173\u952e\u5b57 template \u544a\u8bc9\u7f16\u8bd1\u5668\u521b\u5efa\u4e00\u4e2a\u6a21\u677f\u3002\u63a5\u4e0b\u6765\uff0c\u5728\u5c16\u62ec\u53f7( <> )\u4e2d\u6307\u5b9a\u6a21\u677f\u5c06\u4f7f\u7528\u7684 \u6240\u6709\u6a21\u677f\u7c7b\u578b \u3002\u5bf9\u4e8e\u6bcf\u4e2a\u6a21\u677f\u7c7b\u578b\uff0c\u6211\u4eec\u4f7f\u7528\u5173\u952e\u5b57 typename \u6216 class \uff0c\u540e\u9762\u8ddf\u7740\u6a21\u677f\u7c7b\u578b\u7684\u540d\u79f0(\u4f8b\u5982 \u201cT\u201d)\u3002 \u6bcf\u4e2a\u6a21\u677f\u51fd\u6570(\u6216\u6a21\u677f\u7c7b)\u90fd\u9700\u8981\u58f0\u660e\u81ea\u5df1\u7684\u6a21\u677f\u5f62\u53c2\u3002 \u9898\u5916\u8bdd \u5728\u8fd9\u4e2a\u4e0a\u4e0b\u6587\u4e2d\uff0c typename \u548c class \u5173\u952e\u5b57\u4e4b\u95f4\u6ca1\u6709\u533a\u522b\u3002\u4f60\u4f1a\u7ecf\u5e38\u770b\u5230\u4eba\u4eec\u4f7f\u7528 class \u5173\u952e\u5b57\uff0c\u56e0\u4e3a\u5b83\u66f4\u65e9\u5730\u88ab\u5f15\u5165\u5230\u8bed\u8a00\u4e2d\u3002\u7136\u800c\uff0c\u6211\u4eec\u66f4\u559c\u6b22\u65b0\u5f0f\u7684 typename \u5173\u952e\u5b57\uff0c\u56e0\u4e3a\u5b83\u66f4\u6e05\u695a\u5730\u8868\u660e\u6a21\u677f\u7c7b\u578b\u53ef\u4ee5\u88ab\u4efb\u4f55\u7c7b\u578b(\u4f8b\u5982\u57fa\u672c\u7c7b\u578b)\u66ff\u6362\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u7c7b\u7c7b\u578b\u3002 \u56e0\u4e3a\u8fd9\u4e2a\u51fd\u6570\u6a21\u677f\u53ea\u9700\u8981\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\uff0c\u79f0\u4e3a T \uff0c\u6211\u4eec\u79f0\u5176\u4e3a max \u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728\u8bfe\u7a0b 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f \u4e2d\u8ba8\u8bba\u5982\u4f55\u521b\u5efa\u5177\u6709\u591a\u79cd\u6a21\u677f\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f\u3002 \u641e\u5b9a\uff01\u7b80\u5355\u5230\u4ee4\u4eba\u96be\u4ee5\u7f6e\u4fe1\u3002 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u5982\u4f55\u4f7f\u7528 max \u51fd\u6570\u6a21\u677f\u751f\u6210\u5e26\u6709\u4e0d\u540c\u7c7b\u578b\u53c2\u6570\u7684 max() \u51fd\u6570\u3002","title":"8.13 - \u51fd\u6570\u6a21\u677f"},{"location":"8-13-Function-templates/#c","text":"\u5728C++\u4e2d\uff0c\u6a21\u677f\u7cfb\u7edf\u88ab\u8bbe\u8ba1\u7528\u6765\u7b80\u5316\u521b\u5efa\u80fd\u591f\u5904\u7406\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u51fd\u6570(\u6216\u7c7b)\u7684\u8fc7\u7a0b\u3002 \u6211\u4eec\u4e0d\u518d\u662f\u624b\u52a8\u521b\u5efa\u4e00\u5806\u51e0\u4e4e\u76f8\u540c\u7684\u51fd\u6570\u6216\u7c7b(\u6bcf\u4e00\u7ec4\u4e0d\u540c\u7c7b\u578b\u5bf9\u5e94\u4e00\u4e2a)\uff0c\u800c\u662f\u521b\u5efa\u4e00\u4e2a\u5355\u4e00\u7684\u201c\u6a21\u677f\u201d\u3002\u5c31\u50cf\u666e\u901a\u7684\u5b9a\u4e49\u4e00\u6837\uff0c\u6a21\u677f\u63cf\u8ff0\u51fd\u6570\u6216\u7c7b\u7684\u6837\u5b50\u3002\u4e0e\u5e38\u89c4\u5b9a\u4e49(\u5fc5\u987b\u6307\u5b9a\u6240\u6709\u7c7b\u578b)\u4e0d\u540c\uff0c\u5728\u6a21\u677f\u4e2d\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u6216\u591a\u4e2a\u5360\u4f4d\u7b26\u7c7b\u578b\u3002\u5360\u4f4d\u7b26\u7c7b\u578b\u8868\u793a\u5728\u7f16\u5199\u6a21\u677f\u65f6\u4e0d\u77e5\u9053\u4f46\u5c06\u5728\u540e\u9762\u63d0\u4f9b\u7684\u7c7b\u578b\u3002 \u4e00\u65e6\u5b9a\u4e49\u4e86\u6a21\u677f\uff0c\u7f16\u8bd1\u5668\u5c31\u53ef\u4ee5\u6839\u636e\u9700\u8981\uff0c\u4f7f\u7528\u6a21\u677f\u751f\u6210\u4efb\u610f\u591a\u7684\u91cd\u8f7d\u51fd\u6570(\u6216\u7c7b)\uff0c\u6bcf\u4e2a\u51fd\u6570\u4f7f\u7528\u4e0d\u540c\u7684\u5b9e\u9645\u7c7b\u578b\uff01 \u6700\u7ec8\u7684\u7ed3\u679c\u662f\u4e00\u6837\u7684\u2014\u2014\u6211\u4eec\u5f97\u5230\u4e00\u5806\u51e0\u4e4e\u76f8\u540c\u7684\u51fd\u6570\u6216\u7c7b(\u6bcf\u4e00\u7ec4\u4e0d\u540c\u7684\u7c7b\u578b\u5bf9\u5e94\u4e00\u4e2a)\u3002\u4f46\u662f\u6211\u4eec\u53ea\u9700\u8981\u521b\u5efa\u548c\u7ef4\u62a4\u4e00\u4e2a\u6a21\u677f\uff0c\u7f16\u8bd1\u5668\u4f1a\u5b8c\u6210\u6240\u6709\u8270\u96be\u7684\u5de5\u4f5c\u3002 \u5173\u952e\u4fe1\u606f \u7f16\u8bd1\u5668\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u6a21\u677f\u6765\u751f\u6210\u4e00\u7cfb\u5217\u76f8\u5173\u7684\u51fd\u6570\u6216\u7c7b\uff0c\u6bcf\u4e2a\u51fd\u6570\u6216\u7c7b\u4f7f\u7528\u4e00\u7ec4\u4e0d\u540c\u7684\u7c7b\u578b\u3002 \u9898\u5916\u8bdd Because the concept behind templates can be hard to describe in words, let\u2019s try an analogy. If you were to look up the word \u201ctemplate\u201d in the dictionary, you\u2019d find a definition that was similar to the following: \u201ca template is a model that serves as a pattern for creating similar objects\u201d. One type of template that is very easy to understand is that of a stencil. A stencil is a thin piece of material (such as a piece of cardboard or plastic) with a shape cut out of it (e.g. a happy face). By placing the stencil on top of another object, then spraying paint through the hole, you can very quickly replicate the cut-out shape. The stencil itself only needs to be created once, and then it can be reused as many times as desired, to create the cut out shape in as many different colors as you like. Even better, the color of a shape made with the stencil doesn\u2019t have to be determined until the stencil is actually used. A template is essentially a stencil for creating functions or classes. We create the template (our stencil) once, and then we can use it as many times as needed, to stencil out a function or class for a specific set of actual types. Those actual types don\u2019t need to be determined until the template is actually used. \u56e0\u4e3a\u53ea\u6709\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u6a21\u677f\u65f6(\u800c\u4e0d\u662f\u5728\u7f16\u5199\u6a21\u677f\u65f6)\u624d\u80fd\u786e\u5b9a\u5b9e\u9645\u7c7b\u578b\uff0c\u56e0\u6b64\u6a21\u677f\u7684\u4f5c\u8005\u4e0d\u5fc5\u5c1d\u8bd5\u9884\u6d4b\u53ef\u80fd\u4f7f\u7528\u7684\u6240\u6709\u5b9e\u9645\u7c7b\u578b\u3002\u8fd9\u610f\u5473\u7740\u6a21\u677f\u4ee3\u7801\u53ef\u4ee5\u4e0e\u7f16\u5199\u6a21\u677f\u65f6\u751a\u81f3\u4e0d\u5b58\u5728\u7684\u7c7b\u578b\u4e00\u8d77\u4f7f\u7528\uff01\u7a0d\u540e\uff0c\u5f53\u5f00\u59cb\u63a2\u7d22C++\u6807\u51c6\u5e93\u65f6\uff0c\u6211\u4eec\u5c06\u770b\u5230\u8fd9\u662f\u5982\u4f55\u6d3e\u4e0a\u7528\u573a\u7684\uff0cC++\u6807\u51c6\u5e93\u5145\u6ee1\u4e86\u6a21\u677f\u4ee3\u7801! \u5173\u952e\u4fe1\u606f \u6a21\u677f\u53ef\u4ee5\u4f7f\u7528\u5728\u7f16\u5199\u6a21\u677f\u65f6\u751a\u81f3\u4e0d\u5b58\u5728\u7684\u7c7b\u578b\u3002\u8fd9\u6709\u52a9\u4e8e\u4f7f\u6a21\u677f\u4ee3\u7801\u65e2\u7075\u6d3b\u53c8\u7ecf\u5f97\u8d77\u8003\u9a8c! \u5728\u672c\u8bfe\u7684\u5176\u4f59\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u548c\u63a2\u7d22\u5982\u4f55\u521b\u5efa \u51fd\u6570\u6a21\u677f \uff0c\u5e76\u8be6\u7ec6\u63cf\u8ff0\u5b83\u4eec\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u6211\u4eec\u5c06\u5728\u4ecb\u7ecd\u4e86\u4ec0\u4e48\u662f\u7c7b\u4e4b\u540e\u518d\u8ba8\u8bba \u7c7b\u6a21\u677f \u3002","title":"C++\u6a21\u677f\u7b80\u4ecb"},{"location":"8-13-Function-templates/#_1","text":"\u51fd\u6570\u6a21\u677f \u662f\u4e00\u79cd\u7c7b\u4f3c\u51fd\u6570\u7684\u5b9a\u4e49\uff0c\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u6216\u591a\u4e2a\u91cd\u8f7d\u51fd\u6570\uff0c\u6bcf\u4e2a\u91cd\u8f7d\u51fd\u6570\u90fd\u6709\u4e00\u7ec4\u4e0d\u540c\u7684\u5b9e\u9645\u7c7b\u578b\u3002\u8fd9\u5c06\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u53ef\u4ee5\u5904\u7406\u8bb8\u591a\u4e0d\u540c\u7c7b\u578b\u7684\u51fd\u6570\u3002 \u5728\u521b\u5efa\u51fd\u6570\u6a21\u677f\u65f6\uff0c\u5bf9\u4e8e\u6211\u4eec\u5e0c\u671b\u7a0d\u540e\u6307\u5b9a\u7684\u4efb\u4f55\u5f62\u53c2\u7c7b\u578b\u3001\u8fd4\u56de\u7c7b\u578b\u6216\u51fd\u6570\u4f53\u4e2d\u4f7f\u7528\u7684\u7c7b\u578b\uff0c\u6211\u4eec\u90fd\u4f7f\u7528\u5360\u4f4d\u7b26\u7c7b\u578b(\u4e5f\u79f0\u4e3a\u6a21\u677f\u7c7b\u578b)\u3002 \u51fd\u6570\u6a21\u677f\u6700\u597d\u901a\u8fc7\u793a\u4f8b\u6765\u6559\u6388\uff0c\u6240\u4ee5\u8ba9\u6211\u4eec\u5c06\u4e0a\u9762\u793a\u4f8b\u4e2d\u7684 max(int, int) \u51fd\u6570\u8f6c\u6362\u4e3a\u51fd\u6570\u6a21\u677f\u3002\u5b83\u51fa\u5947\u5730\u7b80\u5355\uff0c\u6211\u4eec\u5c06\u89e3\u91ca\u8fd9\u4e00\u8fc7\u7a0b\u4e2d\u53d1\u751f\u4e86\u4ec0\u4e48\u3002","title":"\u51fd\u6570\u6a21\u677f"},{"location":"8-13-Function-templates/#max","text":"\u4e0b\u9762\u4ee3\u7801\u662f int \u7248\u672c\u7684 max() \uff1a 1 2 3 4 int max ( int x , int y ) { return ( x > y ) ? x : y ; } \u6ce8\u610f\uff0c\u6211\u4eec\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\u4f7f\u7528\u4e86\u4e09\u6b21 int \u7c7b\u578b\uff1a\u4e00\u6b21\u7528\u4e8e\u5f62\u53c2 x \uff0c\u4e00\u6b21\u7528\u4e8e\u5f62\u53c2 y \uff0c\u8fd8\u6709\u4e00\u6b21\u7528\u4e8e\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u3002 \u8981\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6a21\u677f\uff0c\u6211\u4eec\u8981\u505a\u4e24\u4ef6\u4e8b\u3002\u9996\u5148\uff0c\u6211\u4eec\u5c06\u7528\u6a21\u677f\u7c7b\u578b\u66ff\u6362\u7279\u5b9a\u7c7b\u578b\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u53ea\u6709\u4e00\u4e2a\u9700\u8981\u66ff\u6362\u7684\u7c7b\u578b( int )\uff0c\u6240\u4ee5\u53ea\u9700\u8981\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\u3002\u901a\u5e38\u7ea6\u5b9a\u4f7f\u7528\u5355\u4e2a\u5927\u5199\u5b57\u6bcd\uff08\u4ece\u5927\u5199\u5b57\u6bcd T \u5f00\u59cb\uff09\u8868\u793a\u6a21\u677f\u7c7b\u578b\u3002 \u4e0b\u9762\u662f\u6211\u4eec\u4f7f\u7528\u5355\u4e00\u6a21\u677f\u7c7b\u578b\u7684\u65b0\u51fd\u6570\uff1a 1 2 3 4 T max ( T x , T y ) // won't compile because we haven't defined T { return ( x > y ) ? x : y ; } \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u5355\u4e2a\u5927\u5199\u5b57\u6bcd(\u4eceT\u5f00\u59cb)\u547d\u540d\u6a21\u677f\u7c7b\u578b(\u4f8b\u5982 T, U, V \u7b49\u2026) \u8fd9\u662f\u4e00\u4e2a\u597d\u7684\u5f00\u59cb\u2014\u2014\u7136\u800c\uff0c\u5b83\u4e0d\u4f1a\u7f16\u8bd1\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053 T \u662f\u4ec0\u4e48\uff01\u8fd9\u4ecd\u7136\u662f\u4e00\u4e2a\u666e\u901a\u7684\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u51fd\u6570\u6a21\u677f\u3002 \u5176\u6b21\uff0c\u6211\u4eec\u4f1a\u544a\u8bc9\u7f16\u8bd1\u5668\u8fd9\u662f\u4e00\u4e2a\u51fd\u6570\u6a21\u677f\uff0c\u800c T \u662f\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\u3002\u8fd9\u662f\u901a\u8fc7\u6240\u8c13\u7684\u6a21\u677f\u53c2\u6570\u58f0\u660e\u6765\u5b8c\u6210\u7684\uff1a 1 2 3 4 5 template < typename T > // this is the template parameter declaration T max ( T x , T y ) // this is the function template definition for max { return ( x > y ) ? x : y ; } \u4ed4\u7ec6\u89c2\u5bdf\u6a21\u677f\u53c2\u6570\u58f0\u660e\u3002\u9996\u5148\u4f7f\u7528\u5173\u952e\u5b57 template \u544a\u8bc9\u7f16\u8bd1\u5668\u521b\u5efa\u4e00\u4e2a\u6a21\u677f\u3002\u63a5\u4e0b\u6765\uff0c\u5728\u5c16\u62ec\u53f7( <> )\u4e2d\u6307\u5b9a\u6a21\u677f\u5c06\u4f7f\u7528\u7684 \u6240\u6709\u6a21\u677f\u7c7b\u578b \u3002\u5bf9\u4e8e\u6bcf\u4e2a\u6a21\u677f\u7c7b\u578b\uff0c\u6211\u4eec\u4f7f\u7528\u5173\u952e\u5b57 typename \u6216 class \uff0c\u540e\u9762\u8ddf\u7740\u6a21\u677f\u7c7b\u578b\u7684\u540d\u79f0(\u4f8b\u5982 \u201cT\u201d)\u3002 \u6bcf\u4e2a\u6a21\u677f\u51fd\u6570(\u6216\u6a21\u677f\u7c7b)\u90fd\u9700\u8981\u58f0\u660e\u81ea\u5df1\u7684\u6a21\u677f\u5f62\u53c2\u3002 \u9898\u5916\u8bdd \u5728\u8fd9\u4e2a\u4e0a\u4e0b\u6587\u4e2d\uff0c typename \u548c class \u5173\u952e\u5b57\u4e4b\u95f4\u6ca1\u6709\u533a\u522b\u3002\u4f60\u4f1a\u7ecf\u5e38\u770b\u5230\u4eba\u4eec\u4f7f\u7528 class \u5173\u952e\u5b57\uff0c\u56e0\u4e3a\u5b83\u66f4\u65e9\u5730\u88ab\u5f15\u5165\u5230\u8bed\u8a00\u4e2d\u3002\u7136\u800c\uff0c\u6211\u4eec\u66f4\u559c\u6b22\u65b0\u5f0f\u7684 typename \u5173\u952e\u5b57\uff0c\u56e0\u4e3a\u5b83\u66f4\u6e05\u695a\u5730\u8868\u660e\u6a21\u677f\u7c7b\u578b\u53ef\u4ee5\u88ab\u4efb\u4f55\u7c7b\u578b(\u4f8b\u5982\u57fa\u672c\u7c7b\u578b)\u66ff\u6362\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u7c7b\u7c7b\u578b\u3002 \u56e0\u4e3a\u8fd9\u4e2a\u51fd\u6570\u6a21\u677f\u53ea\u9700\u8981\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\uff0c\u79f0\u4e3a T \uff0c\u6211\u4eec\u79f0\u5176\u4e3a max \u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728\u8bfe\u7a0b 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f \u4e2d\u8ba8\u8bba\u5982\u4f55\u521b\u5efa\u5177\u6709\u591a\u79cd\u6a21\u677f\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f\u3002 \u641e\u5b9a\uff01\u7b80\u5355\u5230\u4ee4\u4eba\u96be\u4ee5\u7f6e\u4fe1\u3002 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u5982\u4f55\u4f7f\u7528 max \u51fd\u6570\u6a21\u677f\u751f\u6210\u5e26\u6709\u4e0d\u540c\u7c7b\u578b\u53c2\u6570\u7684 max() \u51fd\u6570\u3002","title":"\u521b\u5efa\u6a21\u677f\u5316\u7684max\u51fd\u6570"},{"location":"8-14-Function-template-instantiation/","tags":["function template"],"text":"Key Takeaway \u5728\u4e0a\u8282\u8bfe\u4e2d ( 8.13 - \u51fd\u6570\u6a21\u677f )\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86 \u51fd\u6570\u6a21\u677f \u4ee5\u53ca\u5982\u4f55\u5c06\u666e\u901a\u51fd\u6570 max() \u8f6c\u6362\u6210 max \u51fd\u6570\u6a21\u677f\uff1a 1 2 3 4 5 template < typename T > T max ( T x , T y ) { return ( x > y ) ? x : y ; } \u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u51fd\u6570\u6a21\u677f\u3002 \u4f7f\u7528\u51fd\u6570\u6a21\u677f \u51fd\u6570\u6a21\u677f\u5e76\u4e0d\u662f\u771f\u6b63\u7684\u51fd\u6570\uff0c\u5176\u4ee3\u7801\u4e0d\u80fd\u88ab\u76f4\u63a5\u7f16\u8bd1\u6216\u6267\u884c\u3002\u51fd\u6570\u6a21\u677f\u53ea\u6709\u4e00\u4e2a\u529f\u80fd\uff1a\u751f\u6210\u51fd\u6570\uff08\u53ef\u4ee5\u88ab\u7f16\u8bd1\u6267\u884c\uff09\u3002 \u4e3a\u4e86\u4f7f\u7528 max \u51fd\u6570\u6a21\u677f\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u5982\u4e0b\u8bed\u6cd5\uff1a 1 max < actual_type > ( arg1 , arg2 ); // actual_type \u662f\u5b9e\u9645\u7c7b\u578b\uff0c\u4f8b\u5982 int\u6216double \u8fd9\u770b\u8d77\u6765\u5f88\u50cf\u666e\u901a\u7684\u51fd\u6570\u8c03\u7528\u2014\u2014\u4e3b\u8981\u7684\u533a\u522b\u662f\u5728\u5c16\u62ec\u53f7\u4e2d\u6dfb\u52a0\u4e86\u7c7b\u578b(\u79f0\u4e3a\u6a21\u677f\u5b9e\u53c2)\uff0c\u5b83\u6307\u5b9a\u4e86\u5c06\u7528\u4e8e\u4ee3\u66ff\u6a21\u677f\u7c7b\u578b\u201cT\u201d\u7684\u5b9e\u9645\u7c7b\u578b\u3002 \u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include template < typename T > T max ( T x , T y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(int, int) return 0 ; } \u5f53\u7f16\u8bd1\u5668\u9047\u5230\u51fd\u6570\u8c03\u7528 max(1,2) \u65f6\uff0c\u5b83\u5c06\u786e\u5b9a max(int, int) \u7684\u51fd\u6570\u5b9a\u4e49\u4e0d\u5b58\u5728\u3002\u56e0\u6b64\uff0c\u7f16\u8bd1\u5668\u5c06\u4f7f\u7528 max \u51fd\u6570\u6a21\u677f\u6765\u521b\u5efa\u4e00\u4e2a\u3002 \u4ece\u51fd\u6570\u6a21\u677f(\u5e26\u6709\u6a21\u677f\u7c7b\u578b)\u521b\u5efa\u51fd\u6570(\u5e26\u6709\u7279\u5b9a\u7c7b\u578b)\u7684\u8fc7\u7a0b\u79f0\u4e3a \u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316 (\u6216\u7b80\u79f0\u5b9e\u4f8b\u5316)\u3002\u5f53\u6b64\u8fc7\u7a0b\u7531\u4e8e\u51fd\u6570\u8c03\u7528\u800c\u53d1\u751f\u65f6\uff0c\u79f0\u4e3a\u9690\u5f0f\u5b9e\u4f8b\u5316\u3002\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u901a\u5e38\u79f0\u4e3a \u51fd\u6570\u5b9e\u4f8b (\u7b80\u79f0\u5b9e\u4f8b)\u6216\u6a21\u677f\u51fd\u6570\u3002\u51fd\u6570\u5b9e\u4f8b\u5728\u6240\u6709\u65b9\u9762\u90fd\u662f\u6b63\u5e38\u7684\u51fd\u6570\u3002 \u5b9e\u4f8b\u5316\u51fd\u6570\u7684\u8fc7\u7a0b\u5f88\u7b80\u5355\uff1a\u7f16\u8bd1\u5668\u57fa\u672c\u4e0a\u662f\u514b\u9686\u51fd\u6570\u6a21\u677f\uff0c\u5e76\u7528\u5b9e\u9645\u7c7b\u578b( int )\u66ff\u6362\u6a21\u677f\u7c7b\u578b( T )\u3002 \u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u8c03\u7528 max(1,2) \u65f6\uff0c\u88ab\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u770b\u8d77\u6765\u50cf\u8fd9\u6837\uff1a 1 2 3 4 5 template <> // ignore this for now int max < int > ( int x , int y ) // the generated function max(int, int) { return ( x > y ) ? x : y ; } \u4e0b\u9762\u662f\u4e0e\u4e0a\u9762\u76f8\u540c\u7684\u793a\u4f8b\uff0c\u663e\u793a\u4e86\u7f16\u8bd1\u5668\u5728\u6240\u6709\u5b9e\u4f8b\u5316\u5b8c\u6210\u540e\u5b9e\u9645\u7f16\u8bd1\u7684\u5185\u5bb9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include // a declaration for our function template (we don't need the definition any more) template < typename T > T max ( T x , T y ); template <> int max < int > ( int x , int y ) // the generated function max(int, int) { return ( x > y ) ? x : y ; } int main () { std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(int, int) return 0 ; } \u4f60\u53ef\u4ee5\u81ea\u5df1\u7f16\u8bd1\u5b83\uff0c\u770b\u770b\u5b83\u662f\u5426\u5de5\u4f5c\u3002\u4e00\u4e2a\u5df2\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u53ea\u5728\u7b2c\u4e00\u6b21\u8c03\u7528\u51fd\u6570\u65f6\u88ab\u5b9e\u4f8b\u5316\u3002\u5bf9\u8be5\u51fd\u6570\u7684\u8fdb\u4e00\u6b65\u8c03\u7528\u88ab\u8def\u7531\u5230\u5df2\u7ecf\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u3002 \u518d\u4e3e\u4e00\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include template < typename T > T max ( T x , T y ) // function template for max(T, T) { return ( x > y ) ? x : y ; } int main () { std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(int, int) std :: cout << max < int > ( 4 , 3 ) << '\\n' ; // calls already instantiated function max(int, int) std :: cout << max < double > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(double, double) return 0 ; } \u8fd9\u4e0e\u524d\u9762\u7684\u4f8b\u5b50\u7c7b\u4f3c\uff0c\u4f46\u662f\u6211\u4eec\u7684\u51fd\u6570\u6a21\u677f\u8fd9\u6b21\u5c06\u7528\u4e8e\u751f\u6210\u4e24\u4e2a\u51fd\u6570:\u4e00\u6b21\u5c06 T \u66ff\u6362\u4e3a int \uff0c\u53e6\u4e00\u6b21\u5c06 T \u66ff\u6362\u4e3a double \u3002\u5728\u6240\u6709\u5b9e\u4f8b\u5316\u4e4b\u540e\uff0c\u7a0b\u5e8f\u770b\u8d77\u6765\u50cf\u8fd9\u6837: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include // a declaration for our function template (we don't need the definition any more) template < typename T > T max ( T x , T y ); template <> int max < int > ( int x , int y ) // the generated function max(int, int) { return ( x > y ) ? x : y ; } template <> double max < double > ( double x , double y ) // the generated function max(double, double) { return ( x > y ) ? x : y ; } int main () { std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(int, int) std :: cout << max < int > ( 4 , 3 ) << '\\n' ; // calls already instantiated function max(int, int) std :: cout << max < double > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(double, double) return 0 ; } \u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a\u5f53\u5b9e\u4f8b\u5316 max \u65f6\uff0c\u5176 \u5b9e\u53c2 \u7c7b\u578b\u4e3a double \u3002\u56e0\u4e3a\u6211\u4eec\u63d0\u4f9b\u7684\u662f \u5b9e\u53c2 \u662f int \uff0c\u6240\u4ee5\u5b83\u4f1a\u88ab \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u4e3a double \u3002 \u6a21\u677f\u53c2\u6570\u63a8\u65ad \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u7528\u4e8e\u5b9e\u4f8b\u5316\u7684\u5b9e\u9645\u7c7b\u578b\u5c06\u4e0e\u51fd\u6570\u5f62\u53c2\u7684\u7c7b\u578b\u5339\u914d\u3002\u4f8b\u5982\uff1a 1 std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // specifying we want to call max \u5728\u8fd9\u4e2a\u51fd\u6570\u8c03\u7528\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u6307\u5b9a\u6211\u4eec\u60f3\u7528 int \u66ff\u6362 T \uff0c\u4f46\u6211\u4eec\u4e5f\u8c03\u7528\u4e86\u5e26\u6709 int \u53c2\u6570\u7684\u51fd\u6570\u3002 \u5728\u5b9e\u53c2\u7684\u7c7b\u578b\u4e0e\u6211\u4eec\u60f3\u8981\u7684\u5b9e\u9645\u7c7b\u578b\u76f8\u5339\u914d\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u6307\u5b9a\u5b9e\u9645\u7684\u7c7b\u578b\u2014\u2014\u76f8\u53cd\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6a21\u677f\u5b9e\u53c2\u63a8\u5bfc\uff0c\u8ba9\u7f16\u8bd1\u5668\u4ece\u51fd\u6570\u8c03\u7528\u4e2d\u7684\u5b9e\u53c2\u7c7b\u578b\u63a8\u5bfc\u51fa\u5e94\u8be5\u4f7f\u7528\u7684\u5b9e\u9645\u7c7b\u578b\u3002 \u4f8b\u5982\uff0c\u4e0d\u7528\u50cf\u8fd9\u6837\u8c03\u7528\u51fd\u6570\uff1a 1 std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // specifying we want to call max \u8fd9\u4e48\u505a\u5373\u53ef\uff1a 1 2 std :: cout << max <> ( 1 , 2 ) << '\\n' ; std :: cout << max ( 1 , 2 ) << '\\n' ; \u5bf9\u4e8e\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u4e24\u4e2a\u8bed\u53e5\uff0c\u7f16\u8bd1\u5668\u4f1a\u53d1\u73b0\u6211\u4eec\u6ca1\u6709\u63d0\u4f9b\u5b9e\u9645\u7c7b\u578b\uff0c\u6240\u4ee5\u5b83\u4f1a\u5c1d\u8bd5\u4ece\u51fd\u6570 \u5b9e\u53c2 \u63a8\u65ad\u5b9e\u9645\u7c7b\u578b\u4ee5\u4f7f\u5176\u80fd\u591f\u751f\u6210\u5408\u9002\u7684 max() \u51fd\u6570\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u7f16\u8bd1\u5668\u80fd\u591f\u901a\u8fc7\u5b9e\u9645\u7c7b\u578b int \u63a8\u65ad\u51fa\u6a21\u677f\u7c7b\u578b\u5e76\u5b9e\u4f8b\u5316\u51fd\u6570 max(int, int) \uff0c\u5176\u6240\u6709\u6a21\u677f\u5f62\u53c2\u90fd\u662f( int )\uff0c\u80fd\u591f\u5339\u914d\u63d0\u4f9b\u7684\u5b9e\u53c2\u7c7b\u578b( int )\u3002 \u8fd9\u4e24\u79cd\u60c5\u51b5\u7684\u533a\u522b\u5728\u4e8e\u7f16\u8bd1\u5668\u5982\u4f55\u89e3\u6790\u6765\u81ea\u4e00\u7ec4\u91cd\u8f7d\u51fd\u6570\u7684\u51fd\u6570\u8c03\u7528\u3002\u5728\u9876\u90e8\u7684\u60c5\u51b5\u4e0b(\u5e26\u6709\u7a7a\u5c16\u62ec\u53f7)\uff0c\u7f16\u8bd1\u5668\u5728\u51b3\u5b9a\u8c03\u7528\u54ea\u4e2a\u91cd\u8f7d\u51fd\u6570\u65f6\uff0c\u53ea\u4f1a\u8003\u8651 max \u6a21\u677f\u51fd\u6570\u91cd\u8f7d\u3002\u5728\u4e0b\u9762\u4e00\u4e2a\u4f8b\u5b50(\u6ca1\u6709\u5c16\u62ec\u53f7)\u4e2d\uff0c\u7f16\u8bd1\u5668\u5c06\u540c\u65f6\u8003\u8651 max \u6a21\u677f\u51fd\u6570\u91cd\u8f7d\u548c max \u975e\u6a21\u677f\u51fd\u6570\u91cd\u8f7d\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include template < typename T > T max ( T x , T y ) { std :: cout << \"called max(int, int) \\n \" ; return ( x > y ) ? x : y ; } int max ( int x , int y ) { std :: cout << \"called max(int, int) \\n \" ; return ( x > y ) ? x : y ; } int main () { std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // selects max(int, int) std :: cout << max <> ( 1 , 2 ) << '\\n' ; // deduces max(int, int) (non-template functions not considered) std :: cout << max ( 1 , 2 ) << '\\n' ; // calls function max(int, int) return 0 ; } \u8bf7\u6ce8\u610f\uff0c\u6700\u4e0b\u9762\u7684\u5199\u6cd5\u770b\u8d77\u6765\u4e0e\u6b63\u5e38\u7684\u51fd\u6570\u8c03\u7528\u662f\u4e00\u6837\u7684\uff01\u8fd9\u901a\u5e38\u662f\u8c03\u7528\u51fd\u6570\u6a21\u677f\u65f6\u4f7f\u7528\u7684\u9996\u9009\u8bed\u6cd5(\u5728\u4ee5\u540e\u7684\u793a\u4f8b\u4e2d\uff0c\u5982\u679c\u53ef\u884c\uff0c\u6211\u4eec\u5c06\u9ed8\u8ba4\u4f7f\u7528\u8fd9\u79cd\u8bed\u6cd5)\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u4f7f\u7528\u51fd\u6570\u6a21\u677f\u65f6\uff0c\u4f7f\u7528\u6b63\u5e38\u7684\u51fd\u6570\u8c03\u7528\u8bed\u6cd5\u3002 \u5e26\u6709\u975e\u6a21\u677f\u53c2\u6570\u7684\u51fd\u6570\u6a21\u677f \u53ef\u4ee5\u521b\u5efa\u540c\u65f6\u5177\u6709\u6a21\u677f\u7c7b\u578b\u548c\u975e\u6a21\u677f\u7c7b\u578b\u53c2\u6570\u7684\u51fd\u6570\u6a21\u677f\u3002\u6a21\u677f\u5f62\u53c2\u53ef\u4ee5\u5339\u914d\u4efb\u4f55\u7c7b\u578b\uff0c\u975e\u6a21\u677f\u5f62\u53c2\u7684\u5de5\u4f5c\u65b9\u5f0f\u4e0e\u666e\u901a\u51fd\u6570\u7684\u5f62\u53c2\u7c7b\u4f3c\u3002 \u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 template < typename T > int someFcn ( T x , double y ) { return 5 ; } int main () { someFcn ( 1 , 3.4 ); // matches someFcn(int, double) someFcn ( 1 , 3.4f ); // matches someFcn(int, double) -- the float is promoted to a double someFcn ( 1.2 , 3.4 ); // matches someFcn(double, double) someFcn ( 1.2f , 3.4 ); // matches someFcn(float, double) someFcn ( 1.2f , 3.4f ); // matches someFcn(float, double) -- the float is promoted to a double return 0 ; } \u8fd9\u4e2a\u51fd\u6570\u6a21\u677f\u6709\u4e00\u4e2a\u6a21\u677f\u5316\u7684\u7b2c\u4e00\u4e2a\u5f62\u53c2\uff0c\u4f46\u662f\u7b2c\u4e8c\u4e2a\u5f62\u53c2\u7684\u7c7b\u578b\u662f\u56fa\u5b9a\u7684 double \u3002\u6ce8\u610f\uff0c\u8fd4\u56de\u7c7b\u578b\u4e5f\u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u7684\u51fd\u6570\u603b\u662f\u8fd4\u56de\u4e00\u4e2a int \u503c\u3002 \u6ce8\u610f\uff0c\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u4e0d\u4fdd\u8bc1\u4e00\u5b9a\u80fd\u591f\u7f16\u8bd1\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include template < typename T > T addOne ( T x ) { return x + 1 ; } int main () { std :: cout << addOne ( 1 ) << '\\n' ; std :: cout << addOne ( 2.3 ) << '\\n' ; return 0 ; } \u7f16\u8bd1\u5668\u4f1a\u7f16\u8bd1\u548c\u6267\u884c\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include template < typename T > T addOne ( T x ); template <> int addOne < int > ( int x ) { return x + 1 ; } template <> double addOne < double > ( double x ) { return x + 1 ; } int main () { std :: cout << addOne ( 1 ) << '\\n' ; // calls addOne(int) std :: cout << addOne ( 2.3 ) << '\\n' ; // calls addOne(double) return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 2 2 3.3 \u4f46\u662f\u5982\u679c\u6211\u4eec\u8fd9\u4e48\u505a\u5462\uff1f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include template < typename T > T addOne ( T x ) { return x + 1 ; } int main () { std :: string hello { \"Hello, world!\" }; std :: cout << addOne ( hello ) << '\\n' ; return 0 ; } \u5f53\u7f16\u8bd1\u5668\u5c1d\u8bd5\u89e3\u6790 addOne(hello) \u65f6\uff0c\u5b83\u65e0\u6cd5\u627e\u5230\u4e00\u4e2a\u975e\u6a21\u677f\u51fd\u6570\u6765\u5339\u914d addOne(std::string) \uff0c\u4f46\u662f\u80fd\u591f\u627e\u5230\u4e00\u4e2a\u51fd\u6570\u6a21\u677f addOne(T) \uff0c\u5e76\u901a\u8fc7\u8be5\u6a21\u677f\u751f\u6210 addOne(std::string) \u51fd\u6570\u3002\u56e0\u6b64\u7f16\u8bd1\u5668\u4f1a\u751f\u6210\u5e76\u7f16\u8bd1\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include template < typename T > T addOne ( T x ); template <> std :: string addOne < std :: string > ( std :: string x ) { return x + 1 ; } int main () { std :: string hello { \"Hello, world!\" }; std :: cout << addOne ( hello ) << '\\n' ; return 0 ; } \u663e\u7136\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\uff0c\u56e0\u4e3a x + 1 \u5e76\u4e0d\u9002\u7528\u4e8e x \u4e3a std::string \u7c7b\u578b\u7684\u60c5\u51b5\u3002\u89e3\u51b3\u529e\u6cd5\u5c31\u662f\u4e0d\u8981\u4f7f\u7528 std::string \u7c7b\u578b\u6765\u8c03\u7528 addOne() \u3002 \u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528\u51fd\u6570\u6a21\u677f \u4e3a\u4e86\u5b9e\u4f8b\u5316\u6a21\u677f\uff0c\u7f16\u8bd1\u5668\u9700\u8981\u770b\u5230\u6a21\u677f\u7684\u5b8c\u6574\u5b9a\u4e49\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u6211\u4eec\u60f3\u5728\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\u4f7f\u7528\u51fd\u6570\u6a21\u677f\uff0c\u6bcf\u4e2a\u4ee3\u7801\u6587\u4ef6\u90fd\u9700\u8981\u51fd\u6570\u6a21\u677f\u5b9a\u4e49\u7684\u526f\u672c\u3002\u56e0\u6b64\uff0c \u6a21\u677f\u901a\u5e38\u7f16\u5199\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u5b83\u4eec\u53ef\u4ee5\u88ab #include \u5230\u4efb\u4f55\u60f3\u8981\u4f7f\u7528\u5b83\u4eec\u7684\u4ee3\u7801\u6587\u4ef6\u4e2d\u3002 \u6a21\u677f\u5b9a\u4e49\u5e76\u4e0d\u53d7\u9650\u4e8e \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \uff0c\u800c\u4e14\u7531\u4e8e\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u901a\u5e38\u662f\u9690\u5f0f \u5185\u8054\u51fd\u6570 \uff0c\u6240\u4ee5\u4e5f\u4e0d\u53d7\u9650\u4e8e\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u3002 Max.h 1 2 3 4 5 6 7 8 9 10 #ifndef MAX_H #define MAX_H template < typename T > T max ( T x , T y ) { return ( x > y ) ? x : y ; } #endif Foo.cp 1 2 3 4 5 6 7 #include \"Max.h\" // import template definition for max() #include void foo () { std :: cout << max ( 3 , 2 ); } main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 #include \"Max.h\" // import template definition for max() #include void foo (); // forward declaration for function foo int main () { std :: cout << max ( 3 , 5 ); foo (); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c main.cpp \u548c foo.cpp \u90fd #include \"Max.h\" \uff0c\u6240\u4ee5\u8fd9\u4e24\u4e2a\u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\u90fd\u53ef\u4ee5\u4f7f\u7528 max \u51fd\u6570\u6a21\u677f\u3002 \u6cdb\u578b\u7f16\u7a0b \u56e0\u4e3a\u6a21\u677f\u7c7b\u578b\u53ef\u4ee5\u7528\u4efb\u4f55\u5b9e\u9645\u7c7b\u578b\u66ff\u6362\uff0c\u6240\u4ee5\u6a21\u677f\u7c7b\u578b\u6709\u65f6\u88ab\u79f0\u4e3a\u6cdb\u578b\u7c7b\u578b\u3002\u7531\u4e8e\u6a21\u677f\u53ef\u4ee5\u4e0d\u53d7\u5177\u4f53\u7c7b\u578b\u7684\u5f71\u54cd\u800c\u7f16\u5199\uff0c\u56e0\u6b64\u4f7f\u7528\u6a21\u677f\u8fdb\u884c\u7f16\u7a0b\u6709\u65f6\u88ab\u79f0\u4e3a\u6cdb\u578b\u7f16\u7a0b\u3002C++\u901a\u5e38\u975e\u5e38\u5173\u6ce8\u7c7b\u578b\u548c\u7c7b\u578b\u68c0\u67e5\uff0c\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u6cdb\u578b\u7f16\u7a0b\u8ba9\u6211\u4eec\u4e13\u6ce8\u4e8e\u7b97\u6cd5\u7684\u903b\u8f91\u548c\u6570\u636e\u7ed3\u6784\u7684\u8bbe\u8ba1\uff0c\u800c\u4e0d\u5fc5\u8fc7\u591a\u5730\u62c5\u5fc3\u7c7b\u578b\u4fe1\u606f\u3002 \u5c0f\u7ed3 \u4e00\u65e6\u4e60\u60ef\u4e86\u7f16\u5199\u51fd\u6570\u6a21\u677f\uff0c\u5c31\u4f1a\u53d1\u73b0\u7f16\u5199\u51fd\u6570\u6a21\u677f\u6240\u82b1\u7684\u65f6\u95f4\u5b9e\u9645\u4e0a\u5e76\u4e0d\u6bd4\u5177\u6709\u5b9e\u9645\u7c7b\u578b\u7684\u51fd\u6570\u957f\u591a\u5c11\u3002\u51fd\u6570\u6a21\u677f\u901a\u8fc7\u6700\u5c0f\u5316\u9700\u8981\u7f16\u5199\u548c\u7ef4\u62a4\u7684\u4ee3\u7801\u6570\u91cf\uff0c\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u4ee3\u7801\u7ef4\u62a4\u548c\u9519\u8bef\u3002 \u51fd\u6570\u6a21\u677f\u4e5f\u786e\u5b9e\u6709\u4e00\u4e9b \u7f3a\u70b9 \uff0c\u5982\u679c\u6211\u4eec\u4e0d\u63d0\u53ca\u5b83\u4eec\uff0c\u90a3\u5c31\u662f\u5931\u804c\u4e86\u3002\u9996\u5148\uff0c\u7f16\u8bd1\u5668\u5c06\u4e3a\u6bcf\u4e2a\u51fd\u6570\u8c03\u7528\u521b\u5efa(\u5e76\u7f16\u8bd1)\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5177\u6709\u4e00\u7ec4\u60df\u4e00\u7684\u53c2\u6570\u7c7b\u578b\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u51fd\u6570\u6a21\u677f\u7f16\u5199\u8d77\u6765\u5f88\u7d27\u51d1\uff0c\u4f46\u5b83\u4eec\u53ef\u80fd\u4f1a\u88ab\u6269\u5c55\u6210\u5927\u91cf\u7684\u4ee3\u7801\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u4ee3\u7801\u81a8\u80c0\u548c\u7f16\u8bd1\u65f6\u95f4\u53d8\u6162\u3002\u51fd\u6570\u6a21\u677f \u66f4\u5927\u7684\u7f3a\u70b9 \u662f\uff0c\u5b83\u4eec\u5f80\u5f80\u4f1a\u4ea7\u751f\u770b\u8d77\u6765\u5f88\u75af\u72c2\u3001\u8fd1\u4e4e\u4e0d\u53ef\u8bfb\u7684\u9519\u8bef\u6d88\u606f\uff0c\u6bd4\u5e38\u89c4\u51fd\u6570\u7684\u9519\u8bef\u6d88\u606f\u66f4\u96be\u89e3\u8bfb\u3002\u8fd9\u4e9b\u9519\u8bef\u6d88\u606f\u53ef\u80fd\u76f8\u5f53\u5413\u4eba\uff0c\u4f46\u4e00\u65e6\u4f60\u80fd\u591f\u7406\u89e3\u4e86\u5b83\u8bd5\u56fe\u4f20\u8fbe\u7684\u4fe1\u606f\uff0c\u5176\u6307\u51fa\u7684\u95ee\u9898\u901a\u5e38\u975e\u5e38\u5bb9\u6613\u89e3\u51b3\u3002 \u6a21\u677f\u4e3a\u7f16\u7a0b\u5de5\u5177\u5305\u5e26\u6765\u4e86\u5f3a\u5927\u529f\u80fd\u548c\u5b89\u5168\u6027\uff0c\u8fd9\u4e9b\u7f3a\u70b9\u53ef\u4ee5\u8bf4\u662f\u7455\u4e0d\u63a9\u745c\u4e86\u3002\u6240\u4ee5\u5728\u4efb\u4f55\u9700\u8981\u7c7b\u578b\u7075\u6d3b\u6027\u7684\u5730\u65b9\u90fd\u53ef\u4ee5\u81ea\u7531\u4f7f\u7528\u6a21\u677f\uff01\u4e00\u4e2a\u597d\u7684\u7ecf\u9a8c\u6cd5\u5219\u662f\uff1a \u9996\u5148\u521b\u5efa\u666e\u901a\u51fd\u6570\uff0c\u5f53\u53d1\u73b0\u9700\u8981\u9488\u5bf9\u4e0d\u540c\u53c2\u6570\u7c7b\u578b \u91cd\u8f7d \u51fd\u6570\u65f6\uff0c\u5219\u5c06\u5b83\u4eec\u8f6c\u6362\u4e3a\u51fd\u6570\u6a21\u677f\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u51fd\u6570\u6a21\u677f\u6765\u7f16\u5199\u6cdb\u578b\u4ee3\u7801\uff0c\u8fd9\u4e9b\u4ee3\u7801\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u5904\u7406\u5404\u79cd\u7c7b\u578b\u3002","title":"8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316"},{"location":"8-14-Function-template-instantiation/#_1","text":"\u51fd\u6570\u6a21\u677f\u5e76\u4e0d\u662f\u771f\u6b63\u7684\u51fd\u6570\uff0c\u5176\u4ee3\u7801\u4e0d\u80fd\u88ab\u76f4\u63a5\u7f16\u8bd1\u6216\u6267\u884c\u3002\u51fd\u6570\u6a21\u677f\u53ea\u6709\u4e00\u4e2a\u529f\u80fd\uff1a\u751f\u6210\u51fd\u6570\uff08\u53ef\u4ee5\u88ab\u7f16\u8bd1\u6267\u884c\uff09\u3002 \u4e3a\u4e86\u4f7f\u7528 max \u51fd\u6570\u6a21\u677f\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u5982\u4e0b\u8bed\u6cd5\uff1a 1 max < actual_type > ( arg1 , arg2 ); // actual_type \u662f\u5b9e\u9645\u7c7b\u578b\uff0c\u4f8b\u5982 int\u6216double \u8fd9\u770b\u8d77\u6765\u5f88\u50cf\u666e\u901a\u7684\u51fd\u6570\u8c03\u7528\u2014\u2014\u4e3b\u8981\u7684\u533a\u522b\u662f\u5728\u5c16\u62ec\u53f7\u4e2d\u6dfb\u52a0\u4e86\u7c7b\u578b(\u79f0\u4e3a\u6a21\u677f\u5b9e\u53c2)\uff0c\u5b83\u6307\u5b9a\u4e86\u5c06\u7528\u4e8e\u4ee3\u66ff\u6a21\u677f\u7c7b\u578b\u201cT\u201d\u7684\u5b9e\u9645\u7c7b\u578b\u3002 \u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include template < typename T > T max ( T x , T y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(int, int) return 0 ; } \u5f53\u7f16\u8bd1\u5668\u9047\u5230\u51fd\u6570\u8c03\u7528 max(1,2) \u65f6\uff0c\u5b83\u5c06\u786e\u5b9a max(int, int) \u7684\u51fd\u6570\u5b9a\u4e49\u4e0d\u5b58\u5728\u3002\u56e0\u6b64\uff0c\u7f16\u8bd1\u5668\u5c06\u4f7f\u7528 max \u51fd\u6570\u6a21\u677f\u6765\u521b\u5efa\u4e00\u4e2a\u3002 \u4ece\u51fd\u6570\u6a21\u677f(\u5e26\u6709\u6a21\u677f\u7c7b\u578b)\u521b\u5efa\u51fd\u6570(\u5e26\u6709\u7279\u5b9a\u7c7b\u578b)\u7684\u8fc7\u7a0b\u79f0\u4e3a \u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316 (\u6216\u7b80\u79f0\u5b9e\u4f8b\u5316)\u3002\u5f53\u6b64\u8fc7\u7a0b\u7531\u4e8e\u51fd\u6570\u8c03\u7528\u800c\u53d1\u751f\u65f6\uff0c\u79f0\u4e3a\u9690\u5f0f\u5b9e\u4f8b\u5316\u3002\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u901a\u5e38\u79f0\u4e3a \u51fd\u6570\u5b9e\u4f8b (\u7b80\u79f0\u5b9e\u4f8b)\u6216\u6a21\u677f\u51fd\u6570\u3002\u51fd\u6570\u5b9e\u4f8b\u5728\u6240\u6709\u65b9\u9762\u90fd\u662f\u6b63\u5e38\u7684\u51fd\u6570\u3002 \u5b9e\u4f8b\u5316\u51fd\u6570\u7684\u8fc7\u7a0b\u5f88\u7b80\u5355\uff1a\u7f16\u8bd1\u5668\u57fa\u672c\u4e0a\u662f\u514b\u9686\u51fd\u6570\u6a21\u677f\uff0c\u5e76\u7528\u5b9e\u9645\u7c7b\u578b( int )\u66ff\u6362\u6a21\u677f\u7c7b\u578b( T )\u3002 \u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u8c03\u7528 max(1,2) \u65f6\uff0c\u88ab\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u770b\u8d77\u6765\u50cf\u8fd9\u6837\uff1a 1 2 3 4 5 template <> // ignore this for now int max < int > ( int x , int y ) // the generated function max(int, int) { return ( x > y ) ? x : y ; } \u4e0b\u9762\u662f\u4e0e\u4e0a\u9762\u76f8\u540c\u7684\u793a\u4f8b\uff0c\u663e\u793a\u4e86\u7f16\u8bd1\u5668\u5728\u6240\u6709\u5b9e\u4f8b\u5316\u5b8c\u6210\u540e\u5b9e\u9645\u7f16\u8bd1\u7684\u5185\u5bb9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include // a declaration for our function template (we don't need the definition any more) template < typename T > T max ( T x , T y ); template <> int max < int > ( int x , int y ) // the generated function max(int, int) { return ( x > y ) ? x : y ; } int main () { std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(int, int) return 0 ; } \u4f60\u53ef\u4ee5\u81ea\u5df1\u7f16\u8bd1\u5b83\uff0c\u770b\u770b\u5b83\u662f\u5426\u5de5\u4f5c\u3002\u4e00\u4e2a\u5df2\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u53ea\u5728\u7b2c\u4e00\u6b21\u8c03\u7528\u51fd\u6570\u65f6\u88ab\u5b9e\u4f8b\u5316\u3002\u5bf9\u8be5\u51fd\u6570\u7684\u8fdb\u4e00\u6b65\u8c03\u7528\u88ab\u8def\u7531\u5230\u5df2\u7ecf\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u3002 \u518d\u4e3e\u4e00\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include template < typename T > T max ( T x , T y ) // function template for max(T, T) { return ( x > y ) ? x : y ; } int main () { std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(int, int) std :: cout << max < int > ( 4 , 3 ) << '\\n' ; // calls already instantiated function max(int, int) std :: cout << max < double > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(double, double) return 0 ; } \u8fd9\u4e0e\u524d\u9762\u7684\u4f8b\u5b50\u7c7b\u4f3c\uff0c\u4f46\u662f\u6211\u4eec\u7684\u51fd\u6570\u6a21\u677f\u8fd9\u6b21\u5c06\u7528\u4e8e\u751f\u6210\u4e24\u4e2a\u51fd\u6570:\u4e00\u6b21\u5c06 T \u66ff\u6362\u4e3a int \uff0c\u53e6\u4e00\u6b21\u5c06 T \u66ff\u6362\u4e3a double \u3002\u5728\u6240\u6709\u5b9e\u4f8b\u5316\u4e4b\u540e\uff0c\u7a0b\u5e8f\u770b\u8d77\u6765\u50cf\u8fd9\u6837: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include // a declaration for our function template (we don't need the definition any more) template < typename T > T max ( T x , T y ); template <> int max < int > ( int x , int y ) // the generated function max(int, int) { return ( x > y ) ? x : y ; } template <> double max < double > ( double x , double y ) // the generated function max(double, double) { return ( x > y ) ? x : y ; } int main () { std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(int, int) std :: cout << max < int > ( 4 , 3 ) << '\\n' ; // calls already instantiated function max(int, int) std :: cout << max < double > ( 1 , 2 ) << '\\n' ; // instantiates and calls function max(double, double) return 0 ; } \u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a\u5f53\u5b9e\u4f8b\u5316 max \u65f6\uff0c\u5176 \u5b9e\u53c2 \u7c7b\u578b\u4e3a double \u3002\u56e0\u4e3a\u6211\u4eec\u63d0\u4f9b\u7684\u662f \u5b9e\u53c2 \u662f int \uff0c\u6240\u4ee5\u5b83\u4f1a\u88ab \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u4e3a double \u3002","title":"\u4f7f\u7528\u51fd\u6570\u6a21\u677f"},{"location":"8-14-Function-template-instantiation/#_2","text":"\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u7528\u4e8e\u5b9e\u4f8b\u5316\u7684\u5b9e\u9645\u7c7b\u578b\u5c06\u4e0e\u51fd\u6570\u5f62\u53c2\u7684\u7c7b\u578b\u5339\u914d\u3002\u4f8b\u5982\uff1a 1 std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // specifying we want to call max \u5728\u8fd9\u4e2a\u51fd\u6570\u8c03\u7528\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u6307\u5b9a\u6211\u4eec\u60f3\u7528 int \u66ff\u6362 T \uff0c\u4f46\u6211\u4eec\u4e5f\u8c03\u7528\u4e86\u5e26\u6709 int \u53c2\u6570\u7684\u51fd\u6570\u3002 \u5728\u5b9e\u53c2\u7684\u7c7b\u578b\u4e0e\u6211\u4eec\u60f3\u8981\u7684\u5b9e\u9645\u7c7b\u578b\u76f8\u5339\u914d\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u6307\u5b9a\u5b9e\u9645\u7684\u7c7b\u578b\u2014\u2014\u76f8\u53cd\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6a21\u677f\u5b9e\u53c2\u63a8\u5bfc\uff0c\u8ba9\u7f16\u8bd1\u5668\u4ece\u51fd\u6570\u8c03\u7528\u4e2d\u7684\u5b9e\u53c2\u7c7b\u578b\u63a8\u5bfc\u51fa\u5e94\u8be5\u4f7f\u7528\u7684\u5b9e\u9645\u7c7b\u578b\u3002 \u4f8b\u5982\uff0c\u4e0d\u7528\u50cf\u8fd9\u6837\u8c03\u7528\u51fd\u6570\uff1a 1 std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // specifying we want to call max \u8fd9\u4e48\u505a\u5373\u53ef\uff1a 1 2 std :: cout << max <> ( 1 , 2 ) << '\\n' ; std :: cout << max ( 1 , 2 ) << '\\n' ; \u5bf9\u4e8e\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u4e24\u4e2a\u8bed\u53e5\uff0c\u7f16\u8bd1\u5668\u4f1a\u53d1\u73b0\u6211\u4eec\u6ca1\u6709\u63d0\u4f9b\u5b9e\u9645\u7c7b\u578b\uff0c\u6240\u4ee5\u5b83\u4f1a\u5c1d\u8bd5\u4ece\u51fd\u6570 \u5b9e\u53c2 \u63a8\u65ad\u5b9e\u9645\u7c7b\u578b\u4ee5\u4f7f\u5176\u80fd\u591f\u751f\u6210\u5408\u9002\u7684 max() \u51fd\u6570\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u7f16\u8bd1\u5668\u80fd\u591f\u901a\u8fc7\u5b9e\u9645\u7c7b\u578b int \u63a8\u65ad\u51fa\u6a21\u677f\u7c7b\u578b\u5e76\u5b9e\u4f8b\u5316\u51fd\u6570 max(int, int) \uff0c\u5176\u6240\u6709\u6a21\u677f\u5f62\u53c2\u90fd\u662f( int )\uff0c\u80fd\u591f\u5339\u914d\u63d0\u4f9b\u7684\u5b9e\u53c2\u7c7b\u578b( int )\u3002 \u8fd9\u4e24\u79cd\u60c5\u51b5\u7684\u533a\u522b\u5728\u4e8e\u7f16\u8bd1\u5668\u5982\u4f55\u89e3\u6790\u6765\u81ea\u4e00\u7ec4\u91cd\u8f7d\u51fd\u6570\u7684\u51fd\u6570\u8c03\u7528\u3002\u5728\u9876\u90e8\u7684\u60c5\u51b5\u4e0b(\u5e26\u6709\u7a7a\u5c16\u62ec\u53f7)\uff0c\u7f16\u8bd1\u5668\u5728\u51b3\u5b9a\u8c03\u7528\u54ea\u4e2a\u91cd\u8f7d\u51fd\u6570\u65f6\uff0c\u53ea\u4f1a\u8003\u8651 max \u6a21\u677f\u51fd\u6570\u91cd\u8f7d\u3002\u5728\u4e0b\u9762\u4e00\u4e2a\u4f8b\u5b50(\u6ca1\u6709\u5c16\u62ec\u53f7)\u4e2d\uff0c\u7f16\u8bd1\u5668\u5c06\u540c\u65f6\u8003\u8651 max \u6a21\u677f\u51fd\u6570\u91cd\u8f7d\u548c max \u975e\u6a21\u677f\u51fd\u6570\u91cd\u8f7d\u3002 \u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include template < typename T > T max ( T x , T y ) { std :: cout << \"called max(int, int) \\n \" ; return ( x > y ) ? x : y ; } int max ( int x , int y ) { std :: cout << \"called max(int, int) \\n \" ; return ( x > y ) ? x : y ; } int main () { std :: cout << max < int > ( 1 , 2 ) << '\\n' ; // selects max(int, int) std :: cout << max <> ( 1 , 2 ) << '\\n' ; // deduces max(int, int) (non-template functions not considered) std :: cout << max ( 1 , 2 ) << '\\n' ; // calls function max(int, int) return 0 ; } \u8bf7\u6ce8\u610f\uff0c\u6700\u4e0b\u9762\u7684\u5199\u6cd5\u770b\u8d77\u6765\u4e0e\u6b63\u5e38\u7684\u51fd\u6570\u8c03\u7528\u662f\u4e00\u6837\u7684\uff01\u8fd9\u901a\u5e38\u662f\u8c03\u7528\u51fd\u6570\u6a21\u677f\u65f6\u4f7f\u7528\u7684\u9996\u9009\u8bed\u6cd5(\u5728\u4ee5\u540e\u7684\u793a\u4f8b\u4e2d\uff0c\u5982\u679c\u53ef\u884c\uff0c\u6211\u4eec\u5c06\u9ed8\u8ba4\u4f7f\u7528\u8fd9\u79cd\u8bed\u6cd5)\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u4f7f\u7528\u51fd\u6570\u6a21\u677f\u65f6\uff0c\u4f7f\u7528\u6b63\u5e38\u7684\u51fd\u6570\u8c03\u7528\u8bed\u6cd5\u3002","title":"\u6a21\u677f\u53c2\u6570\u63a8\u65ad"},{"location":"8-14-Function-template-instantiation/#_3","text":"\u53ef\u4ee5\u521b\u5efa\u540c\u65f6\u5177\u6709\u6a21\u677f\u7c7b\u578b\u548c\u975e\u6a21\u677f\u7c7b\u578b\u53c2\u6570\u7684\u51fd\u6570\u6a21\u677f\u3002\u6a21\u677f\u5f62\u53c2\u53ef\u4ee5\u5339\u914d\u4efb\u4f55\u7c7b\u578b\uff0c\u975e\u6a21\u677f\u5f62\u53c2\u7684\u5de5\u4f5c\u65b9\u5f0f\u4e0e\u666e\u901a\u51fd\u6570\u7684\u5f62\u53c2\u7c7b\u4f3c\u3002 \u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 template < typename T > int someFcn ( T x , double y ) { return 5 ; } int main () { someFcn ( 1 , 3.4 ); // matches someFcn(int, double) someFcn ( 1 , 3.4f ); // matches someFcn(int, double) -- the float is promoted to a double someFcn ( 1.2 , 3.4 ); // matches someFcn(double, double) someFcn ( 1.2f , 3.4 ); // matches someFcn(float, double) someFcn ( 1.2f , 3.4f ); // matches someFcn(float, double) -- the float is promoted to a double return 0 ; } \u8fd9\u4e2a\u51fd\u6570\u6a21\u677f\u6709\u4e00\u4e2a\u6a21\u677f\u5316\u7684\u7b2c\u4e00\u4e2a\u5f62\u53c2\uff0c\u4f46\u662f\u7b2c\u4e8c\u4e2a\u5f62\u53c2\u7684\u7c7b\u578b\u662f\u56fa\u5b9a\u7684 double \u3002\u6ce8\u610f\uff0c\u8fd4\u56de\u7c7b\u578b\u4e5f\u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u7684\u51fd\u6570\u603b\u662f\u8fd4\u56de\u4e00\u4e2a int \u503c\u3002 \u6ce8\u610f\uff0c\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u4e0d\u4fdd\u8bc1\u4e00\u5b9a\u80fd\u591f\u7f16\u8bd1\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include template < typename T > T addOne ( T x ) { return x + 1 ; } int main () { std :: cout << addOne ( 1 ) << '\\n' ; std :: cout << addOne ( 2.3 ) << '\\n' ; return 0 ; } \u7f16\u8bd1\u5668\u4f1a\u7f16\u8bd1\u548c\u6267\u884c\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include template < typename T > T addOne ( T x ); template <> int addOne < int > ( int x ) { return x + 1 ; } template <> double addOne < double > ( double x ) { return x + 1 ; } int main () { std :: cout << addOne ( 1 ) << '\\n' ; // calls addOne(int) std :: cout << addOne ( 2.3 ) << '\\n' ; // calls addOne(double) return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 2 2 3.3 \u4f46\u662f\u5982\u679c\u6211\u4eec\u8fd9\u4e48\u505a\u5462\uff1f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include template < typename T > T addOne ( T x ) { return x + 1 ; } int main () { std :: string hello { \"Hello, world!\" }; std :: cout << addOne ( hello ) << '\\n' ; return 0 ; } \u5f53\u7f16\u8bd1\u5668\u5c1d\u8bd5\u89e3\u6790 addOne(hello) \u65f6\uff0c\u5b83\u65e0\u6cd5\u627e\u5230\u4e00\u4e2a\u975e\u6a21\u677f\u51fd\u6570\u6765\u5339\u914d addOne(std::string) \uff0c\u4f46\u662f\u80fd\u591f\u627e\u5230\u4e00\u4e2a\u51fd\u6570\u6a21\u677f addOne(T) \uff0c\u5e76\u901a\u8fc7\u8be5\u6a21\u677f\u751f\u6210 addOne(std::string) \u51fd\u6570\u3002\u56e0\u6b64\u7f16\u8bd1\u5668\u4f1a\u751f\u6210\u5e76\u7f16\u8bd1\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include template < typename T > T addOne ( T x ); template <> std :: string addOne < std :: string > ( std :: string x ) { return x + 1 ; } int main () { std :: string hello { \"Hello, world!\" }; std :: cout << addOne ( hello ) << '\\n' ; return 0 ; } \u663e\u7136\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\uff0c\u56e0\u4e3a x + 1 \u5e76\u4e0d\u9002\u7528\u4e8e x \u4e3a std::string \u7c7b\u578b\u7684\u60c5\u51b5\u3002\u89e3\u51b3\u529e\u6cd5\u5c31\u662f\u4e0d\u8981\u4f7f\u7528 std::string \u7c7b\u578b\u6765\u8c03\u7528 addOne() \u3002","title":"\u5e26\u6709\u975e\u6a21\u677f\u53c2\u6570\u7684\u51fd\u6570\u6a21\u677f"},{"location":"8-14-Function-template-instantiation/#_4","text":"\u4e3a\u4e86\u5b9e\u4f8b\u5316\u6a21\u677f\uff0c\u7f16\u8bd1\u5668\u9700\u8981\u770b\u5230\u6a21\u677f\u7684\u5b8c\u6574\u5b9a\u4e49\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u6211\u4eec\u60f3\u5728\u591a\u4e2a\u4ee3\u7801\u6587\u4ef6\u4e2d\u4f7f\u7528\u51fd\u6570\u6a21\u677f\uff0c\u6bcf\u4e2a\u4ee3\u7801\u6587\u4ef6\u90fd\u9700\u8981\u51fd\u6570\u6a21\u677f\u5b9a\u4e49\u7684\u526f\u672c\u3002\u56e0\u6b64\uff0c \u6a21\u677f\u901a\u5e38\u7f16\u5199\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u5b83\u4eec\u53ef\u4ee5\u88ab #include \u5230\u4efb\u4f55\u60f3\u8981\u4f7f\u7528\u5b83\u4eec\u7684\u4ee3\u7801\u6587\u4ef6\u4e2d\u3002 \u6a21\u677f\u5b9a\u4e49\u5e76\u4e0d\u53d7\u9650\u4e8e \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \uff0c\u800c\u4e14\u7531\u4e8e\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u901a\u5e38\u662f\u9690\u5f0f \u5185\u8054\u51fd\u6570 \uff0c\u6240\u4ee5\u4e5f\u4e0d\u53d7\u9650\u4e8e\u5355\u4e00\u5b9a\u4e49\u89c4\u5219\u3002 Max.h 1 2 3 4 5 6 7 8 9 10 #ifndef MAX_H #define MAX_H template < typename T > T max ( T x , T y ) { return ( x > y ) ? x : y ; } #endif Foo.cp 1 2 3 4 5 6 7 #include \"Max.h\" // import template definition for max() #include void foo () { std :: cout << max ( 3 , 2 ); } main.cpp 1 2 3 4 5 6 7 8 9 10 11 12 #include \"Max.h\" // import template definition for max() #include void foo (); // forward declaration for function foo int main () { std :: cout << max ( 3 , 5 ); foo (); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c main.cpp \u548c foo.cpp \u90fd #include \"Max.h\" \uff0c\u6240\u4ee5\u8fd9\u4e24\u4e2a\u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\u90fd\u53ef\u4ee5\u4f7f\u7528 max \u51fd\u6570\u6a21\u677f\u3002","title":"\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528\u51fd\u6570\u6a21\u677f"},{"location":"8-14-Function-template-instantiation/#_5","text":"\u56e0\u4e3a\u6a21\u677f\u7c7b\u578b\u53ef\u4ee5\u7528\u4efb\u4f55\u5b9e\u9645\u7c7b\u578b\u66ff\u6362\uff0c\u6240\u4ee5\u6a21\u677f\u7c7b\u578b\u6709\u65f6\u88ab\u79f0\u4e3a\u6cdb\u578b\u7c7b\u578b\u3002\u7531\u4e8e\u6a21\u677f\u53ef\u4ee5\u4e0d\u53d7\u5177\u4f53\u7c7b\u578b\u7684\u5f71\u54cd\u800c\u7f16\u5199\uff0c\u56e0\u6b64\u4f7f\u7528\u6a21\u677f\u8fdb\u884c\u7f16\u7a0b\u6709\u65f6\u88ab\u79f0\u4e3a\u6cdb\u578b\u7f16\u7a0b\u3002C++\u901a\u5e38\u975e\u5e38\u5173\u6ce8\u7c7b\u578b\u548c\u7c7b\u578b\u68c0\u67e5\uff0c\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u6cdb\u578b\u7f16\u7a0b\u8ba9\u6211\u4eec\u4e13\u6ce8\u4e8e\u7b97\u6cd5\u7684\u903b\u8f91\u548c\u6570\u636e\u7ed3\u6784\u7684\u8bbe\u8ba1\uff0c\u800c\u4e0d\u5fc5\u8fc7\u591a\u5730\u62c5\u5fc3\u7c7b\u578b\u4fe1\u606f\u3002","title":"\u6cdb\u578b\u7f16\u7a0b"},{"location":"8-14-Function-template-instantiation/#_6","text":"\u4e00\u65e6\u4e60\u60ef\u4e86\u7f16\u5199\u51fd\u6570\u6a21\u677f\uff0c\u5c31\u4f1a\u53d1\u73b0\u7f16\u5199\u51fd\u6570\u6a21\u677f\u6240\u82b1\u7684\u65f6\u95f4\u5b9e\u9645\u4e0a\u5e76\u4e0d\u6bd4\u5177\u6709\u5b9e\u9645\u7c7b\u578b\u7684\u51fd\u6570\u957f\u591a\u5c11\u3002\u51fd\u6570\u6a21\u677f\u901a\u8fc7\u6700\u5c0f\u5316\u9700\u8981\u7f16\u5199\u548c\u7ef4\u62a4\u7684\u4ee3\u7801\u6570\u91cf\uff0c\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u4ee3\u7801\u7ef4\u62a4\u548c\u9519\u8bef\u3002 \u51fd\u6570\u6a21\u677f\u4e5f\u786e\u5b9e\u6709\u4e00\u4e9b \u7f3a\u70b9 \uff0c\u5982\u679c\u6211\u4eec\u4e0d\u63d0\u53ca\u5b83\u4eec\uff0c\u90a3\u5c31\u662f\u5931\u804c\u4e86\u3002\u9996\u5148\uff0c\u7f16\u8bd1\u5668\u5c06\u4e3a\u6bcf\u4e2a\u51fd\u6570\u8c03\u7528\u521b\u5efa(\u5e76\u7f16\u8bd1)\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5177\u6709\u4e00\u7ec4\u60df\u4e00\u7684\u53c2\u6570\u7c7b\u578b\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u51fd\u6570\u6a21\u677f\u7f16\u5199\u8d77\u6765\u5f88\u7d27\u51d1\uff0c\u4f46\u5b83\u4eec\u53ef\u80fd\u4f1a\u88ab\u6269\u5c55\u6210\u5927\u91cf\u7684\u4ee3\u7801\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u4ee3\u7801\u81a8\u80c0\u548c\u7f16\u8bd1\u65f6\u95f4\u53d8\u6162\u3002\u51fd\u6570\u6a21\u677f \u66f4\u5927\u7684\u7f3a\u70b9 \u662f\uff0c\u5b83\u4eec\u5f80\u5f80\u4f1a\u4ea7\u751f\u770b\u8d77\u6765\u5f88\u75af\u72c2\u3001\u8fd1\u4e4e\u4e0d\u53ef\u8bfb\u7684\u9519\u8bef\u6d88\u606f\uff0c\u6bd4\u5e38\u89c4\u51fd\u6570\u7684\u9519\u8bef\u6d88\u606f\u66f4\u96be\u89e3\u8bfb\u3002\u8fd9\u4e9b\u9519\u8bef\u6d88\u606f\u53ef\u80fd\u76f8\u5f53\u5413\u4eba\uff0c\u4f46\u4e00\u65e6\u4f60\u80fd\u591f\u7406\u89e3\u4e86\u5b83\u8bd5\u56fe\u4f20\u8fbe\u7684\u4fe1\u606f\uff0c\u5176\u6307\u51fa\u7684\u95ee\u9898\u901a\u5e38\u975e\u5e38\u5bb9\u6613\u89e3\u51b3\u3002 \u6a21\u677f\u4e3a\u7f16\u7a0b\u5de5\u5177\u5305\u5e26\u6765\u4e86\u5f3a\u5927\u529f\u80fd\u548c\u5b89\u5168\u6027\uff0c\u8fd9\u4e9b\u7f3a\u70b9\u53ef\u4ee5\u8bf4\u662f\u7455\u4e0d\u63a9\u745c\u4e86\u3002\u6240\u4ee5\u5728\u4efb\u4f55\u9700\u8981\u7c7b\u578b\u7075\u6d3b\u6027\u7684\u5730\u65b9\u90fd\u53ef\u4ee5\u81ea\u7531\u4f7f\u7528\u6a21\u677f\uff01\u4e00\u4e2a\u597d\u7684\u7ecf\u9a8c\u6cd5\u5219\u662f\uff1a \u9996\u5148\u521b\u5efa\u666e\u901a\u51fd\u6570\uff0c\u5f53\u53d1\u73b0\u9700\u8981\u9488\u5bf9\u4e0d\u540c\u53c2\u6570\u7c7b\u578b \u91cd\u8f7d \u51fd\u6570\u65f6\uff0c\u5219\u5c06\u5b83\u4eec\u8f6c\u6362\u4e3a\u51fd\u6570\u6a21\u677f\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u51fd\u6570\u6a21\u677f\u6765\u7f16\u5199\u6cdb\u578b\u4ee3\u7801\uff0c\u8fd9\u4e9b\u4ee3\u7801\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u5904\u7406\u5404\u79cd\u7c7b\u578b\u3002","title":"\u5c0f\u7ed3"},{"location":"8-15-Function-templates-with-multiple-template-types/","tags":["function template","C++20"],"text":"Key Takeaway \u5728 8.13 - \u51fd\u6570\u6a21\u677f \u5728\u6211\u4eec\u7f16\u5199\u4e86\u8ba1\u7b97\u4e24\u4e2a\u503c\u4e2d\u8f83\u5927\u503c\u7684\u51fd \u51fd\u6570\u6a21\u677f \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include template < typename T > T max ( T x , T y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max ( 1 , 2 ) << '\\n' ; // will instantiate max(int, int) std :: cout << max ( 1.5 , 2.5 ) << '\\n' ; // will instantiate max(double, double) return 0 ; } \u73b0\u5728\uff0c\u8003\u8651\u4e0b\u9762\u7c7b\u4f3c\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include template < typename T > T max ( T x , T y ) // <-- \u6ce8\u610f\u8fd9\u91cc { return ( x > y ) ? x : y ; } int main () { std :: cout << max ( 2 , 3.5 ) << '\\n' ; // \u7f16\u8bd1\u9519\u8bef return 0 ; } \u4f60\u53ef\u80fd\u4f1a\u60ca\u8bb6\u5730\u53d1\u73b0\u8fd9\u4e2a\u7a0b\u5e8f\u65e0\u6cd5\u7f16\u8bd1\u3002\u7f16\u8bd1\u5668\u4f1a\u53d1\u51fa\u4e00\u5806(\u53ef\u80fd\u770b\u8d77\u6765\u5f88\u75af\u72c2)\u9519\u8bef\u6d88\u606f\u3002\u5728Visual Studio\u4e0a\uff0c\u7b14\u8005\u5f97\u5230\u4e86\u4ee5\u4e0b\u5185\u5bb9\uff1a 1 2 3 4 5 6 7 Project3.cpp(11,18): error C2672: 'max': no matching overloaded function found Project3.cpp(11,28): error C2782: 'T max(T,T)': template parameter 'T' is ambiguous Project3.cpp(4): message : see declaration of 'max' Project3.cpp(11,28): message : could be 'double' Project3.cpp(11,28): message : or 'int' Project3.cpp(11,28): error C2784: 'T max(T,T)': could not deduce template argument for 'T' from 'double' Project3.cpp(4): message : see declaration of 'max' \u5f53\u8c03\u7528 max(2, 3.5) \u65f6\uff0c\u6211\u4eec\u5b9e\u9645\u4e0a\u4f20\u5165\u4e86\u4e24\u4e2a\u4e0d\u540c\u7c7b\u578b\u7684\u5b9e\u53c2\uff1a int \u548c double \u3002\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u4f7f\u7528\u5c16\u62ec\u53f7\u6765\u6307\u5b9a\u51fd\u6570\u6a21\u677f\u7684\u5b9e\u9645\u7c7b\u578b\uff0c\u7f16\u8bd1\u5668\u4f1a\u9996\u5148\u67e5\u770b\u662f\u5426\u6709\u975e\u6a21\u677f\u51fd\u6570 max(int, double) \u4f9b\u5176\u8c03\u7528\uff0c\u4f46\u5f88\u9057\u61be\u6ca1\u6709\u627e\u5230\u3002 \u63a5\u4e0b\u6765\uff0c\u7f16\u8bd1\u5668\u4f1a\u67e5\u770b\u662f\u5426\u80fd\u627e\u5230\u4e00\u4e2a\u5339\u914d\u7684\u51fd\u6570\u6a21\u677f(\u4f7f\u7528 \u6a21\u677f\u5b9e\u53c2\u63a8\u65ad \uff0c\u53c2\u89c1 8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316 )\u3002\u4f46\u5f88\u9057\u61be\uff0c\u4ecd\u7136\u65e0\u6cd5\u627e\u5230\uff0c\u539f\u56e0\u4e5f\u975e\u5e38\u7b80\u5355\uff1a T \u53ea\u80fd\u8868\u793a\u4e00\u4e2a\u7c7b\u578b\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u6839\u636e max(T, T) \u6a21\u677f\u751f\u6210\u80fd\u591f\u63a5\u53d7\u4e24\u4e2a\u4e0d\u540c \u5f62\u53c2 \u7c7b\u578b\u7684\u51fd\u6570\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c\u51fd\u6570\u6a21\u677f\u4e2d\u7684\u591a\u4e2a\u53c2\u6570\u4e3a\u76f8\u540c\u7c7b\u578b T \uff0c\u5219\u5b83\u4eec\u5bf9\u5e94\u7684\u5b9e\u9645\u7c7b\u578b\u4e5f\u5e94\u8be5\u662f\u76f8\u540c\u7684\u3002 \u56e0\u4e3a\u65e0\u6cd5\u627e\u5230\u975e\u6a21\u677f\u51fd\u6570\uff0c\u4e5f\u65e0\u6cd5\u627e\u5230\u5408\u9002\u7684\u51fd\u6570\u6a21\u677f\uff0c\u6240\u4ee5\u51fd\u6570\u89e3\u6790\u51fa\u9519\uff0c\u5bfc\u81f4\u7f16\u8bd1\u5668\u62a5\u9519\u3002 \u4f60\u53ef\u80fd\u4f1a\u60f3\uff0c\u4e3a\u4ec0\u4e48\u7f16\u8bd1\u5668\u4e0d\u80fd\u751f\u6210\u4e00\u4e2a max(double, double) \u7c7b\u578b\u7684\u51fd\u6570\uff0c\u7136\u540e\u901a\u8fc7 \u6570\u503c\u8f6c\u6362 \u5c06 int \u8f6c\u6362\u4e3a double \u5462\uff1f\u7b54\u6848\u4e5f\u5f88\u7b80\u5355\uff1a\u7c7b\u578b\u8f6c\u6362\u53ea\u6709\u5728\u89e3\u6790\u51fd\u6570 \u91cd\u8f7d \u65f6\u624d\u4f1a\u53d1\u751f\uff0c\u5728\u6267\u884c\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad\u65f6\u5e76\u4e0d\u4f1a\u8fdb\u884c\u3002 \u4e0d\u63d0\u4f9b\u7c7b\u578b\u8f6c\u6362\u662f\u6709\u610f\u800c\u4e3a\u4e4b\u7684\uff0c\u4e14\u81f3\u5c11\u6709\u4e24\u4e2a\u539f\u56e0\u3002\u9996\u5148\uff0c\u5b83\u6709\u52a9\u4e8e\u4f7f\u4e8b\u60c5\u53d8\u5f97\u7b80\u5355\uff1a\u6211\u4eec\u8981\u4e48\u627e\u5230\u51fd\u6570\u8c03\u7528\u5b9e\u53c2\u548c\u6a21\u677f\u7c7b\u578b\u5f62\u53c2\u4e4b\u95f4\u7684\u7cbe\u786e\u5339\u914d\uff0c\u8981\u4e48\u627e\u4e0d\u5230\u3002\u5176\u6b21\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u9700\u8981\u786e\u4fdd\u4e24\u4e2a\u6216\u591a\u4e2a\u5f62\u53c2\u5177\u6709\u76f8\u540c\u7c7b\u578b\u7684\u60c5\u51b5\u4e0b\u521b\u5efa\u51fd\u6570\u6a21\u677f(\u5982\u4e0a\u4f8b\u6240\u793a)\u3002 \u6211\u4eec\u5f97\u53e6\u60f3\u529e\u6cd5\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u7528(\u81f3\u5c11)\u4e09\u79cd\u65b9\u6cd5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002 \u4f7f\u7528 static_cast \u5c06\u5b9e\u53c2\u8f6c\u6362\u4e3a\u5339\u914d\u7684\u7c7b\u578b \u89e3\u51b3\u65b9\u6848 1\uff1a\u5c06\u5b9e\u53c2\u8f6c\u6362\u4e3a\u5339\u914d\u7684\u7c7b\u578b\u7684\u4efb\u52a1\u4ea4\u7ed9\u8c03\u7528\u8005\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include template < typename T > T max ( T x , T y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max ( static_cast < double > ( 2 ), 3.5 ) << '\\n' ; // convert our int to a double so we can call max(double, double) return 0 ; } \u8fd9\u6837\u4e00\u6765\uff0c\u4e24\u4e2a\u5b9e\u53c2\u90fd\u662f double \u7c7b\u578b\u4e86\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u5c06\u4f1a\u5b9e\u4f8b\u5316 max(double, double) \u5e76\u6267\u884c\u51fd\u6570\u8c03\u7528\u3002 \u4e0d\u8fc7\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u592a\u81ea\u7136\uff0c\u53ef\u8bfb\u6027\u4e5f\u4e0d\u4f73\u3002 \u63d0\u4f9b\u5b9e\u9645\u7c7b\u578b \u5982\u679c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u975e\u6a21\u677f\u51fd\u6570 max(double, double) \uff0c\u90a3\u4e48\u662f\u53ef\u4ee5\u8c03\u7528 max(int, double) \u5e76\u901a\u8fc7 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u5c06 int \u8f6c\u6362\u4e3a double \u5e76\u8c03\u7528\u51fd\u6570\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include double max ( double x , double y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max ( 2 , 3.5 ) << '\\n' ; // the int argument will be converted to a double return 0 ; } \u7136\u800c\uff0c \u5f53\u7f16\u8bd1\u5668\u8fdb\u884c\u6a21\u677f\u5b9e\u53c2\u63a8\u5bfc\u65f6\uff0c\u5b83\u4e0d\u4f1a\u8fdb\u884c\u4efb\u4f55\u7c7b\u578b\u8f6c\u6362 \u3002\u5e78\u8fd0\u7684\u662f\uff0c\u5982\u679c\u6307\u5b9a\u8981\u4f7f\u7528\u7684\u5b9e\u9645\u7c7b\u578b\uff0c\u7f16\u8bd1\u5668\u5c31\u4e0d\u5fc5\u4f7f\u7528\u6a21\u677f\u5b9e\u53c2\u63a8\u5bfc\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include template < typename T > T max ( T x , T y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max < double > ( 2 , 3.5 ) << '\\n' ; // we've provided actual type double, so the compiler won't use template argument deduction return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8c03\u7528\u4e86 max(2, 3.5) \u51fd\u6570\u3002\u56e0\u4e3a\u663e\u793a\u63d0\u4f9b\u4e86\u6a21\u677f\u7c7b\u578b double \uff0c\u7f16\u8bd1\u5668\u65e0\u9700\u8fdb\u884c\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad\u3002\u5b83\u4f1a\u76f4\u63a5\u5b9e\u4f8b\u5316 max(double, double) \uff0c\u7136\u540e\u5bf9\u4e0d\u5339\u914d\u7684\u53c2\u6570\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002 int \u5c31\u4f1a\u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a double \u3002 \u5c3d\u7ba1\u8fd9\u4e48\u505a\u7684\u53ef\u8bfb\u6027\u7a0d\u597d\u4e8e static_cast \uff0c\u4f46\u662f\u5982\u679c\u80fd\u591f\u5728\u8c03\u7528\u51fd\u6570 max \u65f6\u4e0d\u8003\u8651\u7c7b\u578b\u5c31\u597d\u4e86\u3002 \u5177\u6709\u591a\u4e2a\u6a21\u677f\u7c7b\u578b\u5f62\u53c2\u7684\u51fd\u6570\u6a21\u677f \u8fd9\u4e2a\u95ee\u9898\u7684\u6839\u56e0\u662f\u56e0\u4e3a\u51fd\u6570\u6a21\u677f\u4e2d\u53ea\u6709\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b ( T ) \uff0c\u6240\u4ee5\u4e24\u4e2a\u53c2\u6570\u90fd\u5fc5\u987b\u662f\u8be5\u7c7b\u578b\u7684\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u6700\u4f73\u7684\u65b9\u6cd5\u662f\u8ba9\u51fd\u6570\u6a21\u677f\u652f\u6301\u591a\u4e2a\u4e0d\u540c\u7684\u7c7b\u578b\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u4f7f\u7528\u4e24\u4e2a\u7c7b\u578b\u5f62\u53c2( T \u548c U )\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include template < typename T , typename U > // We're using two template type parameters named T and U T max ( T x , U y ) // x can resolve to type T, and y can resolve to type U { return ( x > y ) ? x : y ; // uh oh, we have a narrowing conversion problem here } int main () { std :: cout << max ( 2 , 3.5 ) << '\\n' ; return 0 ; } \u56e0\u4e3a\uff0c\u6211\u4eec\u4f7f\u7528\u6a21\u677f\u7c7b\u578b T \u5b9a\u4e49\u4e86 x \uff0c\u4f7f\u7528 U \u5b9a\u4e49\u4e86 y \uff0c\u6240\u4ee5\u73b0\u5728\u4e24\u4e2a\u53c2\u6570\u53ef\u4ee5\u662f\u4e0d\u540c\u7684\u7c7b\u578b\u4e86\u3002 \u5728\u8c03\u7528 max(2, 3.5) \u65f6 T \u88ab\u89e3\u6790\u4e3a int \u800c U \u88ab\u89e3\u6790\u4e3a double \u3002\u7f16\u8bd1\u5668\u5b9e\u4f8b\u5316 max(int, double) \u51fd\u6570\u3002 \u4f46\u662f\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u4ecd\u7136\u6709\u95ee\u9898\uff1a\u5728\u4f7f\u7528\u7b97\u6570\u89c4\u5219\u65f6( 8.4 - \u7b97\u672f\u8f6c\u6362 )\uff0c double \u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e int \u56e0\u6b64\u6761\u4ef6\u8fd0\u7b97\u7b26\u7684\u8fd4\u56de\u503c\u662f double \uff0c\u800c\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a T \u2014\u2014 \u5982\u679c\u6b64\u65f6 T \u88ab\u89e3\u6790\u4e3a\u4e86 int \uff0c\u5219 double \u7c7b\u578b\u7684\u8fd4\u56de\u503c\u5728\u8fd4\u56de\u65f6\u4f1a\u88ab \u7f29\u7a84\u8f6c\u6362 \u4e3a int \u4ece\u800c\u4ea7\u751f\u4e00\u4e2a\u544a\u8b66\uff08\u901a\u5e38\u4e5f\u4f1a\u5bfc\u81f4\u6570\u636e\u4e22\u5931\uff09\u3002 \u5c06\u8fd4\u56de\u503c\u5b9a\u4e49\u4e3a U \u5e76\u4e0d\u80fd\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u56e0\u4e3a T \u548c U \u5728\u4f20\u9012\u65f6\u672c\u8eab\u5c31\u53ef\u4ee5\u4e92\u6362\u3002 \u90a3\u5e94\u8be5\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5462\uff1f\u4e00\u4e2a\u597d\u529e\u6cd5\u662f\u4f7f\u7528 auto \u4f5c\u4e3a\u8fd4\u56de\u7c7b\u578b\u2014\u2014\u8ba9\u7f16\u8bd1\u5668\u4ece\u8fd4\u56de\u8bed\u53e5\u4e2d\u63a8\u65ad\u8fd4\u56de\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include template < typename T , typename U > auto max ( T x , U y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max ( 2 , 3.5 ) << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7248\u672c\u7684 max \u51fd\u6570\u73b0\u5728\u53ef\u4ee5\u5f88\u597d\u5730\u914d\u5408\u5404\u79cd\u53c2\u6570\u7c7b\u578b\u5de5\u4f5c\u4e86\u3002 \u7f29\u5199\u51fd\u6570\u6a21\u677f \uff08C++20\uff09 C++20 \u4e3a auto \u5173\u952e\u5b57\u5f15\u5165\u4e86\u65b0\u7684\u529f\u80fd\uff1a\u5f53\u4f7f\u7528 auto \u5173\u952e\u5b57\u4fee\u9970\u666e\u901a\u51fd\u6570\u7684 \u5f62\u53c2 \u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u5c06\u8be5\u51fd\u6570\u8f6c\u6362\u4e3a\u51fd\u6570\u6a21\u677f\uff0c\u4e14\u6bcf\u4e2a\u88ab auto \u4fee\u9970\u7684\u5f62\u53c2\u90fd\u662f\u72ec\u7acb\u7684\u6a21\u677f\u7c7b\u578b\u5f62\u53c2\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\u521b\u5efa\u7684\u51fd\u6570\u6a21\u677f\u79f0\u4e3a \u7f29\u5199\u51fd\u6570\u6a21\u677f \u3002 \u4f8b\u5982\uff1a 1 2 3 4 auto max ( auto x , auto y ) { return ( x > y ) ? x : y ; } \u4e0a\u9762\u4ee3\u7801\u662fC++20\u4e2d\u5bf9\u4e0b\u9762\u6a21\u677f\u5b9a\u4e49\u7684\u7f29\u5199\u5f62\u5f0f\uff1a 1 2 3 4 5 template < typename T , typename U > auto max ( T x , U y ) { return ( x > y ) ? x : y ; } \u8fd9\u548c\u6211\u4eec\u4e0a\u9762\u5199\u7684 max \u51fd\u6570\u6a21\u677f\u662f\u4e00\u6837\u7684\u3002 \u5728\u5e0c\u671b\u6bcf\u4e2a\u6a21\u677f\u7c7b\u578b\u5f62\u53c2\u90fd\u662f\u72ec\u7acb\u7c7b\u578b\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd9\u79cd\u5f62\u5f0f\u662f\u9996\u9009\u7684\uff0c\u56e0\u4e3a\u5220\u9664\u6a21\u677f\u5f62\u53c2\u58f0\u660e\u884c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u53ef\u8bfb\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u6bcf\u4e2a\u81ea\u52a8\u53c2\u6570\u90fd\u5e94\u8be5\u662f\u72ec\u7acb\u7684\u6a21\u677f\u7c7b\u578b(\u5e76\u4e14\u4f60\u7684\u8bed\u8a00\u6807\u51c6\u8bbe\u7f6e\u4e3aC++20\u6216\u66f4\u65b0\u7248\u672c)\uff0c\u90a3\u4e48\u53ef\u4ee5\u968f\u610f\u4f7f\u7528\u7f29\u5199\u51fd\u6570\u6a21\u677f\u3002","title":"8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f"},{"location":"8-15-Function-templates-with-multiple-template-types/#static_cast","text":"\u89e3\u51b3\u65b9\u6848 1\uff1a\u5c06\u5b9e\u53c2\u8f6c\u6362\u4e3a\u5339\u914d\u7684\u7c7b\u578b\u7684\u4efb\u52a1\u4ea4\u7ed9\u8c03\u7528\u8005\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include template < typename T > T max ( T x , T y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max ( static_cast < double > ( 2 ), 3.5 ) << '\\n' ; // convert our int to a double so we can call max(double, double) return 0 ; } \u8fd9\u6837\u4e00\u6765\uff0c\u4e24\u4e2a\u5b9e\u53c2\u90fd\u662f double \u7c7b\u578b\u4e86\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u5c06\u4f1a\u5b9e\u4f8b\u5316 max(double, double) \u5e76\u6267\u884c\u51fd\u6570\u8c03\u7528\u3002 \u4e0d\u8fc7\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u592a\u81ea\u7136\uff0c\u53ef\u8bfb\u6027\u4e5f\u4e0d\u4f73\u3002","title":"\u4f7f\u7528 static_cast \u5c06\u5b9e\u53c2\u8f6c\u6362\u4e3a\u5339\u914d\u7684\u7c7b\u578b"},{"location":"8-15-Function-templates-with-multiple-template-types/#_1","text":"\u5982\u679c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u975e\u6a21\u677f\u51fd\u6570 max(double, double) \uff0c\u90a3\u4e48\u662f\u53ef\u4ee5\u8c03\u7528 max(int, double) \u5e76\u901a\u8fc7 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u5c06 int \u8f6c\u6362\u4e3a double \u5e76\u8c03\u7528\u51fd\u6570\u7684\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include double max ( double x , double y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max ( 2 , 3.5 ) << '\\n' ; // the int argument will be converted to a double return 0 ; } \u7136\u800c\uff0c \u5f53\u7f16\u8bd1\u5668\u8fdb\u884c\u6a21\u677f\u5b9e\u53c2\u63a8\u5bfc\u65f6\uff0c\u5b83\u4e0d\u4f1a\u8fdb\u884c\u4efb\u4f55\u7c7b\u578b\u8f6c\u6362 \u3002\u5e78\u8fd0\u7684\u662f\uff0c\u5982\u679c\u6307\u5b9a\u8981\u4f7f\u7528\u7684\u5b9e\u9645\u7c7b\u578b\uff0c\u7f16\u8bd1\u5668\u5c31\u4e0d\u5fc5\u4f7f\u7528\u6a21\u677f\u5b9e\u53c2\u63a8\u5bfc\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include template < typename T > T max ( T x , T y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max < double > ( 2 , 3.5 ) << '\\n' ; // we've provided actual type double, so the compiler won't use template argument deduction return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8c03\u7528\u4e86 max(2, 3.5) \u51fd\u6570\u3002\u56e0\u4e3a\u663e\u793a\u63d0\u4f9b\u4e86\u6a21\u677f\u7c7b\u578b double \uff0c\u7f16\u8bd1\u5668\u65e0\u9700\u8fdb\u884c\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad\u3002\u5b83\u4f1a\u76f4\u63a5\u5b9e\u4f8b\u5316 max(double, double) \uff0c\u7136\u540e\u5bf9\u4e0d\u5339\u914d\u7684\u53c2\u6570\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002 int \u5c31\u4f1a\u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a double \u3002 \u5c3d\u7ba1\u8fd9\u4e48\u505a\u7684\u53ef\u8bfb\u6027\u7a0d\u597d\u4e8e static_cast \uff0c\u4f46\u662f\u5982\u679c\u80fd\u591f\u5728\u8c03\u7528\u51fd\u6570 max \u65f6\u4e0d\u8003\u8651\u7c7b\u578b\u5c31\u597d\u4e86\u3002","title":"\u63d0\u4f9b\u5b9e\u9645\u7c7b\u578b"},{"location":"8-15-Function-templates-with-multiple-template-types/#_2","text":"\u8fd9\u4e2a\u95ee\u9898\u7684\u6839\u56e0\u662f\u56e0\u4e3a\u51fd\u6570\u6a21\u677f\u4e2d\u53ea\u6709\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b ( T ) \uff0c\u6240\u4ee5\u4e24\u4e2a\u53c2\u6570\u90fd\u5fc5\u987b\u662f\u8be5\u7c7b\u578b\u7684\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u6700\u4f73\u7684\u65b9\u6cd5\u662f\u8ba9\u51fd\u6570\u6a21\u677f\u652f\u6301\u591a\u4e2a\u4e0d\u540c\u7684\u7c7b\u578b\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u4f7f\u7528\u4e24\u4e2a\u7c7b\u578b\u5f62\u53c2( T \u548c U )\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include template < typename T , typename U > // We're using two template type parameters named T and U T max ( T x , U y ) // x can resolve to type T, and y can resolve to type U { return ( x > y ) ? x : y ; // uh oh, we have a narrowing conversion problem here } int main () { std :: cout << max ( 2 , 3.5 ) << '\\n' ; return 0 ; } \u56e0\u4e3a\uff0c\u6211\u4eec\u4f7f\u7528\u6a21\u677f\u7c7b\u578b T \u5b9a\u4e49\u4e86 x \uff0c\u4f7f\u7528 U \u5b9a\u4e49\u4e86 y \uff0c\u6240\u4ee5\u73b0\u5728\u4e24\u4e2a\u53c2\u6570\u53ef\u4ee5\u662f\u4e0d\u540c\u7684\u7c7b\u578b\u4e86\u3002 \u5728\u8c03\u7528 max(2, 3.5) \u65f6 T \u88ab\u89e3\u6790\u4e3a int \u800c U \u88ab\u89e3\u6790\u4e3a double \u3002\u7f16\u8bd1\u5668\u5b9e\u4f8b\u5316 max(int, double) \u51fd\u6570\u3002 \u4f46\u662f\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u4ecd\u7136\u6709\u95ee\u9898\uff1a\u5728\u4f7f\u7528\u7b97\u6570\u89c4\u5219\u65f6( 8.4 - \u7b97\u672f\u8f6c\u6362 )\uff0c double \u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e int \u56e0\u6b64\u6761\u4ef6\u8fd0\u7b97\u7b26\u7684\u8fd4\u56de\u503c\u662f double \uff0c\u800c\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a T \u2014\u2014 \u5982\u679c\u6b64\u65f6 T \u88ab\u89e3\u6790\u4e3a\u4e86 int \uff0c\u5219 double \u7c7b\u578b\u7684\u8fd4\u56de\u503c\u5728\u8fd4\u56de\u65f6\u4f1a\u88ab \u7f29\u7a84\u8f6c\u6362 \u4e3a int \u4ece\u800c\u4ea7\u751f\u4e00\u4e2a\u544a\u8b66\uff08\u901a\u5e38\u4e5f\u4f1a\u5bfc\u81f4\u6570\u636e\u4e22\u5931\uff09\u3002 \u5c06\u8fd4\u56de\u503c\u5b9a\u4e49\u4e3a U \u5e76\u4e0d\u80fd\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u56e0\u4e3a T \u548c U \u5728\u4f20\u9012\u65f6\u672c\u8eab\u5c31\u53ef\u4ee5\u4e92\u6362\u3002 \u90a3\u5e94\u8be5\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5462\uff1f\u4e00\u4e2a\u597d\u529e\u6cd5\u662f\u4f7f\u7528 auto \u4f5c\u4e3a\u8fd4\u56de\u7c7b\u578b\u2014\u2014\u8ba9\u7f16\u8bd1\u5668\u4ece\u8fd4\u56de\u8bed\u53e5\u4e2d\u63a8\u65ad\u8fd4\u56de\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include template < typename T , typename U > auto max ( T x , U y ) { return ( x > y ) ? x : y ; } int main () { std :: cout << max ( 2 , 3.5 ) << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7248\u672c\u7684 max \u51fd\u6570\u73b0\u5728\u53ef\u4ee5\u5f88\u597d\u5730\u914d\u5408\u5404\u79cd\u53c2\u6570\u7c7b\u578b\u5de5\u4f5c\u4e86\u3002","title":"\u5177\u6709\u591a\u4e2a\u6a21\u677f\u7c7b\u578b\u5f62\u53c2\u7684\u51fd\u6570\u6a21\u677f"},{"location":"8-15-Function-templates-with-multiple-template-types/#c20","text":"C++20 \u4e3a auto \u5173\u952e\u5b57\u5f15\u5165\u4e86\u65b0\u7684\u529f\u80fd\uff1a\u5f53\u4f7f\u7528 auto \u5173\u952e\u5b57\u4fee\u9970\u666e\u901a\u51fd\u6570\u7684 \u5f62\u53c2 \u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u5c06\u8be5\u51fd\u6570\u8f6c\u6362\u4e3a\u51fd\u6570\u6a21\u677f\uff0c\u4e14\u6bcf\u4e2a\u88ab auto \u4fee\u9970\u7684\u5f62\u53c2\u90fd\u662f\u72ec\u7acb\u7684\u6a21\u677f\u7c7b\u578b\u5f62\u53c2\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\u521b\u5efa\u7684\u51fd\u6570\u6a21\u677f\u79f0\u4e3a \u7f29\u5199\u51fd\u6570\u6a21\u677f \u3002 \u4f8b\u5982\uff1a 1 2 3 4 auto max ( auto x , auto y ) { return ( x > y ) ? x : y ; } \u4e0a\u9762\u4ee3\u7801\u662fC++20\u4e2d\u5bf9\u4e0b\u9762\u6a21\u677f\u5b9a\u4e49\u7684\u7f29\u5199\u5f62\u5f0f\uff1a 1 2 3 4 5 template < typename T , typename U > auto max ( T x , U y ) { return ( x > y ) ? x : y ; } \u8fd9\u548c\u6211\u4eec\u4e0a\u9762\u5199\u7684 max \u51fd\u6570\u6a21\u677f\u662f\u4e00\u6837\u7684\u3002 \u5728\u5e0c\u671b\u6bcf\u4e2a\u6a21\u677f\u7c7b\u578b\u5f62\u53c2\u90fd\u662f\u72ec\u7acb\u7c7b\u578b\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd9\u79cd\u5f62\u5f0f\u662f\u9996\u9009\u7684\uff0c\u56e0\u4e3a\u5220\u9664\u6a21\u677f\u5f62\u53c2\u58f0\u660e\u884c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u53ef\u8bfb\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u6bcf\u4e2a\u81ea\u52a8\u53c2\u6570\u90fd\u5e94\u8be5\u662f\u72ec\u7acb\u7684\u6a21\u677f\u7c7b\u578b(\u5e76\u4e14\u4f60\u7684\u8bed\u8a00\u6807\u51c6\u8bbe\u7f6e\u4e3aC++20\u6216\u66f4\u65b0\u7248\u672c)\uff0c\u90a3\u4e48\u53ef\u4ee5\u968f\u610f\u4f7f\u7528\u7f29\u5199\u51fd\u6570\u6a21\u677f\u3002","title":"\u7f29\u5199\u51fd\u6570\u6a21\u677f\uff08C++20\uff09"},{"location":"8-2-Floating-point-and-integral-promotion/","tags":["numeric promotion","conversions"],"text":"Key Takeaway \u6570\u503c\u63d0\u5347\u5c5e\u4e8e\u4e00\u7c7b\u7c7b\u578b\u8f6c\u6362\uff0c\u5b83\u5c06\u66f4\u7a84\u7684\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982 char \uff09\u8f6c\u6362\u4e3a\u66f4\u5bbd\u7684\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982 int \u6216 double \uff09 \u4f7f\u5176\u80fd\u591f\u66f4\u9ad8\u6548\u5730\u88ab\u5904\u7406 \uff0c\u540c\u65f6\u4e5f\u66f4\u4e0d\u5bb9\u6613\u6ea2\u51fa\u3002\uff08\u505a\u4e0d\u5230\u8fd9\u4e00\u70b9\u7684\u4e0d\u53eb\u6570\u503c \u63d0\u5347 \uff09 \u6570\u503c\u63d0\u5347\u662f\u5b89\u5168\u7684\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u7531\u5730\u4f7f\u7528\u6570\u5b57\u63d0\u5347\uff0c\u5e76\u4e14\u5728\u8fd9\u6837\u505a\u65f6\u4e0d\u4f1a\u53d1\u51fa\u8b66\u544a\u3002 \u5229\u7528\u6570\u503c\u63d0\u5347\uff0c\u53ef\u4ee5\u7f16\u5199\u51fd\u6570\u4f7f\u7528\u67d0\u79cd\u53ef\u4ee5\u88ab\u63d0\u5347\u7684\u7c7b\u578b\u4f5c\u4e3a\u5f62\u53c2\uff0c\u7136\u540e\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u7c7b\u578b\u7684\u5f62\u53c2\u8fdb\u884c\u8c03\u7528\u3002\u5728\u8c03\u7528\u65f6\u8fdb\u81ea\u52a8\u8fdb\u884c\u7c7b\u578b\u63d0\u5347 float -> double \u3002 unsigned char \u6216\u8005 signed char -> int \uff1b unsigned char \uff0c char8_t \u53ca unsigned short -> int \uff0c\u53ea\u8981 int \u7684\u8303\u56f4\u8db3\u591f\u8868\u793a\u8be5\u7c7b\u578b\u7684\u8303\u56f4\uff0c\u5426\u5219\u4f1a\u8f6c\u6362\u4e3a unsigned int \uff1b bool -> int \uff0c false \u8f6c\u6362\u4e3a 0 \uff0c true \u8f6c\u6362\u4e3a 1 \u3002 \u6ce8\u610f\uff1a\u5728\u67d0\u4e9b\u7cfb\u7edf\u4e0a\uff0c\u4e00\u4e9b\u6574\u578b\u53ef\u80fd\u4f1a\u88ab\u8f6c\u6362\u4e3a unsigned int \u800c\u4e0d\u662f int \u3002\u5176\u6b21\uff0c\u4e00\u4e9b\u8f83\u7a84\u7684\u65e0\u7b26\u53f7\u7c7b\u578b(\u5982 unsigned char )\u5c06\u88ab\u8f6c\u6362\u4e3a\u8f83\u5927\u7684\u6709\u7b26\u53f7\u7c7b\u578b(\u5982 int )\u3002\u56e0\u6b64\uff0c\u867d\u7136\u6574\u578b\u63d0\u5347\u662f\u4fdd\u503c\u7684\uff0c\u4f46\u4e0d\u4e00\u5b9a\u80fd\u4fdd\u53f7\u3002 \u5728 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0cC++ \u53ea\u80fd\u4fdd\u8bc1\u6bcf\u79cd\u57fa\u7840\u7c7b\u578b\u6700\u5c0f\u7684\u5c3a\u5bf8\u3002\u800c\u8fd9\u4e9b\u7c7b\u578b\u7684\u5177\u4f53\u5c3a\u5bf8\u8981\u770b\u5177\u4f53\u7684\u7f16\u8bd1\u5668\u548c\u4f53\u7cfb\u7ed3\u6784\u3002 \u6b63\u662f\u56e0\u4e3a\u5141\u8bb8\u8fd9\u79cd\u53d8\u5316\uff0c\u6240\u4ee5 int \u548c double \u6570\u636e\u7c7b\u578b\u624d\u80fd\u5728\u7279\u5b9a\u4f53\u7cfb\u7ed3\u6784\u7684\u8ba1\u7b97\u673a\u4e0a\u88ab\u8bbe\u7f6e\u4e3a\u80fd\u591f\u63d0\u4f9b\u6700\u4f18\u6027\u80fd\u7684\u5927\u5c0f\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a 32 \u4f4d\u8ba1\u7b97\u673a\u6765\u8bf4\uff0c\u5b83\u6bcf\u6b21\u80fd\u591f\u5904\u7406 32 \u4f4d\u7684\u6570\u636e\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c int \u88ab\u8bbe\u7f6e\u4e3a 32 \u4f4d\u5bbd\u5ea6\uff0c\u56e0\u4e3a\u8fd9\u662fCPU\u5904\u7406\u6570\u636e\u65f6\u6700\u201d\u81ea\u7136\u201c\uff08\u4e5f\u5c31\u53ef\u80fd\u662f\u6700\u9ad8\u6548\u7684\uff09\u7684\u5bbd\u5ea6\u3002 \u63d0\u9192 \u6570\u636e\u7c7b\u578b\u5360\u7528\u7684\u4f4d\u6570\u79f0\u4e3a\u5bbd\u5ea6\u3002\u5360\u7528\u7684\u4f4d\u6570\u8d8a\u591a\uff0c\u5bbd\u5ea6\u4e5f\u5c31\u8d8a\u5bbd\uff0c\u800c\u8d8a\u7a84\u7684\u6570\u636e\u5219\u5360\u7528\u7684\u4f4d\u6570\u4e5f\u8d8a\u5c11\u3002 \u4f46\u662f\uff0c\u5982\u679c 32 \u4f4d CPU \u9700\u8981\u4fee\u6539\u4e00\u4e2a 8 \u4f4d\u7684\u503c\uff08\u4f8b\u5982 char \u7c7b\u578b\uff09\u65f6\u4f1a\u600e\u6837\uff1f\u6709\u4e9b 32 \u4f4d\u7684\u5904\u7406\u5668\uff08\u4f8b\u5982 x86 \u7cfb\u5217\uff09\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539 8 \u4f4d\u6216\u8005 16 \u4f4d\u7684\u503c\u3002\u4f46\u662f\u8fd9\u79cd\u60c5\u51b5\u6bd4\u4fee\u6539 32 \u4f4d\u7684\u6570\u636e\u8981\u6162\u5f97\u591a\uff01\u5176\u4ed6\u7684 32 \u4f4d CPU\uff08\u4f8b\u5982 PowerPC\uff09\uff0c\u53ea\u80fd\u64cd\u4f5c 32 \u4f4d\u7684\u503c\uff0c\u800c\u64cd\u4f5c\u66f4\u7a84\u7684\u6570\u636e\u9700\u8981\u4e00\u4e9b\u989d\u5916\u7684\u6280\u5de7\u3002 \u6570\u503c\u63d0\u5347 \u56e0\u4e3a C++ \u88ab\u8bbe\u8ba1\u4e3a\u53ef\u4ee5\u5728\u5e7f\u6cdb\u7684\u5e73\u53f0\u4e0a\u8fdb\u884c\u79fb\u690d\uff0c\u56e0\u6b64\u8bed\u8a00\u7684\u8bbe\u8ba1\u8005\u4eec\u5e76\u4e0d\u60f3\u60f3\u5047\u5b9a\u7ed9\u5b9a\u7684 CPU \u80fd\u591f\u9ad8\u6548\u5730\u64cd\u4f5c\u6bd4\u81ea\u7136\u5bbd\u5ea6\u66f4\u7a84\u7684\u6570\u636e\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u6311\u6218\uff0cC++ \u5b9a\u4e49\u4e86\u4e00\u79cd\u7c7b\u578b\u7684\u6570\u636e\u8f6c\u6362\uff0c\u5176\u975e\u6b63\u5f0f\u540d\u79f0\u53eb\u505a \u6570\u503c\u63d0\u5347(numeric promotions) \u3002\u6570\u503c\u63d0\u5347\u5c5e\u4e8e\u4e00\u7c7b\u7c7b\u578b\u8f6c\u6362\uff0c\u5b83\u5c06\u66f4\u7a84\u7684\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982 char \uff09\u8f6c\u6362\u4e3a\u66f4\u5bbd\u7684\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982 int \u6216 double \uff09\u4f7f\u5176\u80fd\u591f\u66f4\u9ad8\u6548\u5730\u88ab\u5904\u7406\uff0c\u540c\u65f6\u4e5f\u66f4\u4e0d\u5bb9\u6613\u6ea2\u51fa\u3002 \u6240\u6709\u6570\u503c\u63d0\u5347\u90fd\u662f \u503c\u4fdd\u7559(value-preserving) \u7684\uff0c\u8fd9\u610f\u5473\u7740\u539f\u59cb\u7c7b\u578b\u4e2d\u7684\u6240\u6709\u503c\u90fd\u53ef\u4ee5\u5728\u4e0d\u635f\u5931\u6570\u636e\u6216\u7cbe\u5ea6\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u8868\u793a\u3002\u56e0\u4e3a\u6570\u503c\u63d0\u5347\u662f\u5b89\u5168\u7684\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u7531\u5730\u4f7f\u7528\u6570\u5b57\u63d0\u5347\uff0c\u5e76\u4e14\u5728\u8fd9\u6837\u505a\u65f6\u4e0d\u4f1a\u53d1\u51fa\u8b66\u544a\u3002 \u6570\u503c\u63d0\u5347\u80fd\u591f\u907f\u514d\u5197\u4f59\u4ee3\u7801 \u6570\u503c\u63d0\u5347\u8fd8\u53ef\u4ee5\u89e3\u51b3\u5176\u4ed6\u95ee\u9898\u3002\u8003\u8651\u8fd9\u6837\u4e00\u4e2a\u573a\u666f\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u6253\u5370 int \u7c7b\u578b\u7684\u503c\uff1a 1 2 3 4 5 6 #include void printInt ( int x ) { std :: cout << x ; } \u7a0b\u5e8f\u975e\u5e38\u7b80\u5355\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u8fd8\u5e0c\u671b\u6253\u5370 short \u6216\u8005 char \u7c7b\u578b\u7684\u503c\u5462\uff1f\u5982\u679c\u6ca1\u6709\u7c7b\u578b\u8f6c\u6362\u7684\u652f\u6301\uff0c\u6211\u4eec\u5c31\u5fc5\u987b\u7f16\u5199\u53e6\u5916\u4e24\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\u5206\u522b\u7528\u4e8e\u6253\u5370 short \u6216\u8005 char \u7c7b\u578b\u7684\u503c\u3002\u66f4\u4e0d\u7528\u8bf4\u8fd8\u6709\u5176\u4ed6\u7c7b\u578b\u4e86\uff08 unsigned char , signed char , unsigned short , wchar_t , char8_t , char16_t \u548c char32_t \uff09\u3002\u5f88\u663e\u7136\uff0c\u8fd9\u6837\u7684\u4ee3\u7801\u662f\u4e0d\u53ef\u7ef4\u62a4\u7684\u3002 \u6570\u503c\u63d0\u5347\u5728\u8fd9\u91cc\u8d77\u5230\u4e86\u5173\u952e\u7684\u4f5c\u7528\uff1a\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u5177\u6709 int \u548c/\u6216 double \u5f62\u53c2\u7684\u51fd\u6570(\u6bd4\u5982\u4e0a\u9762\u7684 printInt() \u51fd\u6570)\u3002\u7136\u540e\u4f7f\u7528\u7c7b\u578b\u80fd\u591f\u5728\u8c03\u7528\u51fd\u6570\u662f\u901a\u8fc7\u6570\u503c\u63d0\u5347\u7c7b\u578b\u8f6c\u6362\u8fdb\u884c\u5339\u914d\u7684\u53c2\u6570\u5373\u53ef\u3002 \u6570\u503c\u63d0\u5347\u5206\u7c7b \u6570\u503c\u63d0\u5347\u8fd8\u80fd\u591f\u8fdb\u4e00\u6b65\u5206\u4e3a\u4e24\u79cd\u5b50\u7c7b\u578b\uff1a\u6574\u578b\u63d0\u5347\u548c\u6d6e\u70b9\u6570\u63d0\u5347\u3002 \u6d6e\u70b9\u6570\u63d0\u5347 \u5148\u4ece\u7b80\u5355\u7684\u5f00\u59cb\u3002 \u57fa\u4e8e\u6d6e\u70b9\u6570\u63d0\u5347\u7684\u89c4\u5219\uff0c float \u7c7b\u578b\u7684\u503c\u53ef\u4ee5\u88ab\u8f6c\u6362 double \u3002 \u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u63a5\u53d7 double \u53c2\u6570\u7684\u51fd\u6570\uff0c\u7136\u540e\u7528 double \u6216 float \u503c\u8c03\u7528\u5b83\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include void printDouble ( double d ) { std :: cout << d ; } int main () { printDouble ( 5.0 ); // no conversion necessary printDouble ( 4.0f ); // numeric promotion of float to double return 0 ; } \u5728\u7b2c\u4e8c\u6b21\u8c03\u7528 printDouble() \u65f6 float \u5b57\u9762\u91cf 4.0f \u88ab\u63d0\u5347\u4e3a double \uff0c\u56e0\u6b64\u5b9e\u53c2\u7684\u7c7b\u578b\u4e0e\u51fd\u6570\u5f62\u53c2\u7684\u7c7b\u578b\u5339\u914d\u3002 \u6574\u578b\u63d0\u5347 \u6574\u578b\u6570\u503c\u63d0\u5347\u5c31\u66f4\u52a0\u590d\u6742\u4e86\u3002 \u57fa\u4e8e\u6574\u578b\u6570\u503c\u63d0\u5347\u7684\u89c4\u5219\uff0c\u53ef\u4ee5\u8fdb\u884c\u5982\u4e0b\u7684\u7c7b\u578b\u8f6c\u6362\uff1a - \u65e0\u7b26\u53f7 char \u6216\u8005\u6709\u7b26\u53f7 char \u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a int \uff1b - \u65e0\u7b26\u53f7 char \uff0c char8_t \u4ee5\u53ca\u65e0\u7b26\u53f7 short \u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a int \uff0c\u53ea\u8981 int \u7684\u8303\u56f4\u8db3\u591f\u8868\u793a\u8be5\u7c7b\u578b\u7684\u8303\u56f4\uff0c\u5426\u5219\u4f1a\u8f6c\u6362\u4e3a\u65e0\u7b26\u53f7 int \uff1b - \u5982\u679c char \u9ed8\u8ba4\u6709\u7b26\u53f7\u7684\uff0c \u5219\u4f1a\u9075\u5faa\u4e0a\u8ff0\u6709\u7b26\u53f7 char \u7684\u8f6c\u6362\u89c4\u5219\u3002\u5982\u679c\u9ed8\u8ba4\u662f\u65e0\u7b26\u53f7\u7684\uff0c\u5219\u9075\u5faa\u4e0a\u8ff0\u65e0\u7b26\u53f7 char \u7684\u8f6c\u6362\u89c4\u5219\uff1b - bool \u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a int \uff0c false \u8f6c\u6362\u4e3a 0 \uff0c true \u8f6c\u6362\u4e3a 1 \u3002 \u8fd8\u6709\u4e00\u4e9b\u4e0d\u5e38\u7528\u7684\u6574\u578b\u8f6c\u6362\u89c4\u5219\u53ef\u4ee5\u5728 \u8fd9\u91cc \u627e\u5230\u3002 \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u5141\u8bb8\u6211\u4eec\u7f16\u5199\u4e00\u4e2a\u63a5\u53d7\u5f62\u53c2\u7c7b\u578b\u4e3a int \u7684\u51fd\u6570\uff0c\u7136\u540e\u4e0e\u5176\u4ed6\u5404\u79cd\u6574\u578b\u7c7b\u578b\u914d\u5408\u4f7f\u7528\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void printInt ( int x ) { std :: cout << x ; } int main () { printInt ( 2 ); short s { 3 }; // there is no short literal suffix, so we'll use a variable for this one printInt ( s ); // numeric promotion of short to int printInt ( 'a' ); // numeric promotion of char to int printInt ( true ); // numeric promotion of bool to int return 0 ; } \u8fd9\u91cc\u6709\u4e24\u70b9\u503c\u5f97\u6ce8\u610f\u3002\u9996\u5148\uff0c\u5728\u67d0\u4e9b\u7cfb\u7edf\u4e0a\uff0c\u4e00\u4e9b\u6574\u578b\u53ef\u80fd\u4f1a\u88ab\u8f6c\u6362\u4e3a unsigned int \u800c\u4e0d\u662f int \u3002\u5176\u6b21\uff0c\u4e00\u4e9b\u8f83\u7a84\u7684\u65e0\u7b26\u53f7\u7c7b\u578b(\u5982 unsigned char )\u5c06\u88ab\u8f6c\u6362\u4e3a\u8f83\u5927\u7684\u6709\u7b26\u53f7\u7c7b\u578b(\u5982 int )\u3002\u56e0\u6b64\uff0c\u867d\u7136\u6574\u578b\u63d0\u5347\u662f\u4fdd\u503c\u7684\uff0c\u4f46\u4e0d\u4e00\u5b9a\u80fd\u4fdd\u53f7\u3002 \u4e0d\u662f\u6240\u6709\u7684\u503c\u4fdd\u7559\u7c7b\u578b\u8f6c\u6362\u90fd\u662f\u6570\u503c\u63d0\u5347 \u4e00\u4e9b \u503c\u4fdd\u7559(value-preserving) \u7684\u7c7b\u578b\u8f6c\u6362(\u4f8b\u5982 char \u5230 short \u3001 int \u5230 long \u6216\u8005 int \u5230 double ) \u5728 C++ \u4e2d\u5e76\u4e0d\u88ab\u770b\u505a\u6570\u503c\u63d0\u5347\uff08\u5b83\u4eec\u5c5e\u4e8e \u6570\u503c\u8f6c\u6362 \uff09\uff0c\u6211\u4eec\u4f1a\u5728 8.3 - \u6570\u503c\u8f6c\u6362 \u4e2d\u8fdb\u884c\u4ecb\u7ecd)\u3002\u8fd9\u662f\u56e0\u4e3a\u8fd9\u6837\u7684\u8f6c\u6362\u4e0d\u80fd\u5e2e\u52a9\u5b9e\u73b0\u5c06\u8f83\u5c0f\u7c7b\u578b\u8f6c\u6362\u4e3a\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u5904\u7406\u7684\u8f83\u5927\u7c7b\u578b\u7684\u76ee\u6807\u3002 \u8fd9\u79cd\u533a\u522b\u4e3b\u8981\u662f\u5b66\u672f\u4e0a\u7684\u3002\u4f46\u662f\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u4f1a\u503e\u5411\u4e8e\u6570\u503c\u63d0\u5347\u800c\u4e0d\u662f\u6570\u503c\u8f6c\u6362\u3002\u5f53\u6211\u4eec\u8ba8\u8bba\u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u65f6\uff0c\u6211\u4eec\u5c06\u770b\u5230\u8fd9\u6837\u505a\u7684\u4e0d\u540c\u4e4b\u5904(\u53c2\u89c1\uff1a 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u5339\u914d\u6b67\u4e49 )\u3002","title":"8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347"},{"location":"8-2-Floating-point-and-integral-promotion/#_1","text":"\u56e0\u4e3a C++ \u88ab\u8bbe\u8ba1\u4e3a\u53ef\u4ee5\u5728\u5e7f\u6cdb\u7684\u5e73\u53f0\u4e0a\u8fdb\u884c\u79fb\u690d\uff0c\u56e0\u6b64\u8bed\u8a00\u7684\u8bbe\u8ba1\u8005\u4eec\u5e76\u4e0d\u60f3\u60f3\u5047\u5b9a\u7ed9\u5b9a\u7684 CPU \u80fd\u591f\u9ad8\u6548\u5730\u64cd\u4f5c\u6bd4\u81ea\u7136\u5bbd\u5ea6\u66f4\u7a84\u7684\u6570\u636e\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u6311\u6218\uff0cC++ \u5b9a\u4e49\u4e86\u4e00\u79cd\u7c7b\u578b\u7684\u6570\u636e\u8f6c\u6362\uff0c\u5176\u975e\u6b63\u5f0f\u540d\u79f0\u53eb\u505a \u6570\u503c\u63d0\u5347(numeric promotions) \u3002\u6570\u503c\u63d0\u5347\u5c5e\u4e8e\u4e00\u7c7b\u7c7b\u578b\u8f6c\u6362\uff0c\u5b83\u5c06\u66f4\u7a84\u7684\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982 char \uff09\u8f6c\u6362\u4e3a\u66f4\u5bbd\u7684\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982 int \u6216 double \uff09\u4f7f\u5176\u80fd\u591f\u66f4\u9ad8\u6548\u5730\u88ab\u5904\u7406\uff0c\u540c\u65f6\u4e5f\u66f4\u4e0d\u5bb9\u6613\u6ea2\u51fa\u3002 \u6240\u6709\u6570\u503c\u63d0\u5347\u90fd\u662f \u503c\u4fdd\u7559(value-preserving) \u7684\uff0c\u8fd9\u610f\u5473\u7740\u539f\u59cb\u7c7b\u578b\u4e2d\u7684\u6240\u6709\u503c\u90fd\u53ef\u4ee5\u5728\u4e0d\u635f\u5931\u6570\u636e\u6216\u7cbe\u5ea6\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u8868\u793a\u3002\u56e0\u4e3a\u6570\u503c\u63d0\u5347\u662f\u5b89\u5168\u7684\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u7531\u5730\u4f7f\u7528\u6570\u5b57\u63d0\u5347\uff0c\u5e76\u4e14\u5728\u8fd9\u6837\u505a\u65f6\u4e0d\u4f1a\u53d1\u51fa\u8b66\u544a\u3002","title":"\u6570\u503c\u63d0\u5347"},{"location":"8-2-Floating-point-and-integral-promotion/#_2","text":"\u6570\u503c\u63d0\u5347\u8fd8\u53ef\u4ee5\u89e3\u51b3\u5176\u4ed6\u95ee\u9898\u3002\u8003\u8651\u8fd9\u6837\u4e00\u4e2a\u573a\u666f\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u6253\u5370 int \u7c7b\u578b\u7684\u503c\uff1a 1 2 3 4 5 6 #include void printInt ( int x ) { std :: cout << x ; } \u7a0b\u5e8f\u975e\u5e38\u7b80\u5355\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u8fd8\u5e0c\u671b\u6253\u5370 short \u6216\u8005 char \u7c7b\u578b\u7684\u503c\u5462\uff1f\u5982\u679c\u6ca1\u6709\u7c7b\u578b\u8f6c\u6362\u7684\u652f\u6301\uff0c\u6211\u4eec\u5c31\u5fc5\u987b\u7f16\u5199\u53e6\u5916\u4e24\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\u5206\u522b\u7528\u4e8e\u6253\u5370 short \u6216\u8005 char \u7c7b\u578b\u7684\u503c\u3002\u66f4\u4e0d\u7528\u8bf4\u8fd8\u6709\u5176\u4ed6\u7c7b\u578b\u4e86\uff08 unsigned char , signed char , unsigned short , wchar_t , char8_t , char16_t \u548c char32_t \uff09\u3002\u5f88\u663e\u7136\uff0c\u8fd9\u6837\u7684\u4ee3\u7801\u662f\u4e0d\u53ef\u7ef4\u62a4\u7684\u3002 \u6570\u503c\u63d0\u5347\u5728\u8fd9\u91cc\u8d77\u5230\u4e86\u5173\u952e\u7684\u4f5c\u7528\uff1a\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u5177\u6709 int \u548c/\u6216 double \u5f62\u53c2\u7684\u51fd\u6570(\u6bd4\u5982\u4e0a\u9762\u7684 printInt() \u51fd\u6570)\u3002\u7136\u540e\u4f7f\u7528\u7c7b\u578b\u80fd\u591f\u5728\u8c03\u7528\u51fd\u6570\u662f\u901a\u8fc7\u6570\u503c\u63d0\u5347\u7c7b\u578b\u8f6c\u6362\u8fdb\u884c\u5339\u914d\u7684\u53c2\u6570\u5373\u53ef\u3002","title":"\u6570\u503c\u63d0\u5347\u80fd\u591f\u907f\u514d\u5197\u4f59\u4ee3\u7801"},{"location":"8-2-Floating-point-and-integral-promotion/#_3","text":"\u6570\u503c\u63d0\u5347\u8fd8\u80fd\u591f\u8fdb\u4e00\u6b65\u5206\u4e3a\u4e24\u79cd\u5b50\u7c7b\u578b\uff1a\u6574\u578b\u63d0\u5347\u548c\u6d6e\u70b9\u6570\u63d0\u5347\u3002","title":"\u6570\u503c\u63d0\u5347\u5206\u7c7b"},{"location":"8-2-Floating-point-and-integral-promotion/#_4","text":"\u5148\u4ece\u7b80\u5355\u7684\u5f00\u59cb\u3002 \u57fa\u4e8e\u6d6e\u70b9\u6570\u63d0\u5347\u7684\u89c4\u5219\uff0c float \u7c7b\u578b\u7684\u503c\u53ef\u4ee5\u88ab\u8f6c\u6362 double \u3002 \u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u63a5\u53d7 double \u53c2\u6570\u7684\u51fd\u6570\uff0c\u7136\u540e\u7528 double \u6216 float \u503c\u8c03\u7528\u5b83\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include void printDouble ( double d ) { std :: cout << d ; } int main () { printDouble ( 5.0 ); // no conversion necessary printDouble ( 4.0f ); // numeric promotion of float to double return 0 ; } \u5728\u7b2c\u4e8c\u6b21\u8c03\u7528 printDouble() \u65f6 float \u5b57\u9762\u91cf 4.0f \u88ab\u63d0\u5347\u4e3a double \uff0c\u56e0\u6b64\u5b9e\u53c2\u7684\u7c7b\u578b\u4e0e\u51fd\u6570\u5f62\u53c2\u7684\u7c7b\u578b\u5339\u914d\u3002","title":"\u6d6e\u70b9\u6570\u63d0\u5347"},{"location":"8-2-Floating-point-and-integral-promotion/#_5","text":"\u6574\u578b\u6570\u503c\u63d0\u5347\u5c31\u66f4\u52a0\u590d\u6742\u4e86\u3002 \u57fa\u4e8e\u6574\u578b\u6570\u503c\u63d0\u5347\u7684\u89c4\u5219\uff0c\u53ef\u4ee5\u8fdb\u884c\u5982\u4e0b\u7684\u7c7b\u578b\u8f6c\u6362\uff1a - \u65e0\u7b26\u53f7 char \u6216\u8005\u6709\u7b26\u53f7 char \u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a int \uff1b - \u65e0\u7b26\u53f7 char \uff0c char8_t \u4ee5\u53ca\u65e0\u7b26\u53f7 short \u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a int \uff0c\u53ea\u8981 int \u7684\u8303\u56f4\u8db3\u591f\u8868\u793a\u8be5\u7c7b\u578b\u7684\u8303\u56f4\uff0c\u5426\u5219\u4f1a\u8f6c\u6362\u4e3a\u65e0\u7b26\u53f7 int \uff1b - \u5982\u679c char \u9ed8\u8ba4\u6709\u7b26\u53f7\u7684\uff0c \u5219\u4f1a\u9075\u5faa\u4e0a\u8ff0\u6709\u7b26\u53f7 char \u7684\u8f6c\u6362\u89c4\u5219\u3002\u5982\u679c\u9ed8\u8ba4\u662f\u65e0\u7b26\u53f7\u7684\uff0c\u5219\u9075\u5faa\u4e0a\u8ff0\u65e0\u7b26\u53f7 char \u7684\u8f6c\u6362\u89c4\u5219\uff1b - bool \u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a int \uff0c false \u8f6c\u6362\u4e3a 0 \uff0c true \u8f6c\u6362\u4e3a 1 \u3002 \u8fd8\u6709\u4e00\u4e9b\u4e0d\u5e38\u7528\u7684\u6574\u578b\u8f6c\u6362\u89c4\u5219\u53ef\u4ee5\u5728 \u8fd9\u91cc \u627e\u5230\u3002 \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u5141\u8bb8\u6211\u4eec\u7f16\u5199\u4e00\u4e2a\u63a5\u53d7\u5f62\u53c2\u7c7b\u578b\u4e3a int \u7684\u51fd\u6570\uff0c\u7136\u540e\u4e0e\u5176\u4ed6\u5404\u79cd\u6574\u578b\u7c7b\u578b\u914d\u5408\u4f7f\u7528\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void printInt ( int x ) { std :: cout << x ; } int main () { printInt ( 2 ); short s { 3 }; // there is no short literal suffix, so we'll use a variable for this one printInt ( s ); // numeric promotion of short to int printInt ( 'a' ); // numeric promotion of char to int printInt ( true ); // numeric promotion of bool to int return 0 ; } \u8fd9\u91cc\u6709\u4e24\u70b9\u503c\u5f97\u6ce8\u610f\u3002\u9996\u5148\uff0c\u5728\u67d0\u4e9b\u7cfb\u7edf\u4e0a\uff0c\u4e00\u4e9b\u6574\u578b\u53ef\u80fd\u4f1a\u88ab\u8f6c\u6362\u4e3a unsigned int \u800c\u4e0d\u662f int \u3002\u5176\u6b21\uff0c\u4e00\u4e9b\u8f83\u7a84\u7684\u65e0\u7b26\u53f7\u7c7b\u578b(\u5982 unsigned char )\u5c06\u88ab\u8f6c\u6362\u4e3a\u8f83\u5927\u7684\u6709\u7b26\u53f7\u7c7b\u578b(\u5982 int )\u3002\u56e0\u6b64\uff0c\u867d\u7136\u6574\u578b\u63d0\u5347\u662f\u4fdd\u503c\u7684\uff0c\u4f46\u4e0d\u4e00\u5b9a\u80fd\u4fdd\u53f7\u3002","title":"\u6574\u578b\u63d0\u5347"},{"location":"8-2-Floating-point-and-integral-promotion/#_6","text":"\u4e00\u4e9b \u503c\u4fdd\u7559(value-preserving) \u7684\u7c7b\u578b\u8f6c\u6362(\u4f8b\u5982 char \u5230 short \u3001 int \u5230 long \u6216\u8005 int \u5230 double ) \u5728 C++ \u4e2d\u5e76\u4e0d\u88ab\u770b\u505a\u6570\u503c\u63d0\u5347\uff08\u5b83\u4eec\u5c5e\u4e8e \u6570\u503c\u8f6c\u6362 \uff09\uff0c\u6211\u4eec\u4f1a\u5728 8.3 - \u6570\u503c\u8f6c\u6362 \u4e2d\u8fdb\u884c\u4ecb\u7ecd)\u3002\u8fd9\u662f\u56e0\u4e3a\u8fd9\u6837\u7684\u8f6c\u6362\u4e0d\u80fd\u5e2e\u52a9\u5b9e\u73b0\u5c06\u8f83\u5c0f\u7c7b\u578b\u8f6c\u6362\u4e3a\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u5904\u7406\u7684\u8f83\u5927\u7c7b\u578b\u7684\u76ee\u6807\u3002 \u8fd9\u79cd\u533a\u522b\u4e3b\u8981\u662f\u5b66\u672f\u4e0a\u7684\u3002\u4f46\u662f\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u4f1a\u503e\u5411\u4e8e\u6570\u503c\u63d0\u5347\u800c\u4e0d\u662f\u6570\u503c\u8f6c\u6362\u3002\u5f53\u6211\u4eec\u8ba8\u8bba\u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u65f6\uff0c\u6211\u4eec\u5c06\u770b\u5230\u8fd9\u6837\u505a\u7684\u4e0d\u540c\u4e4b\u5904(\u53c2\u89c1\uff1a 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u5339\u914d\u6b67\u4e49 )\u3002","title":"\u4e0d\u662f\u6240\u6709\u7684\u503c\u4fdd\u7559\u7c7b\u578b\u8f6c\u6362\u90fd\u662f\u6570\u503c\u63d0\u5347"},{"location":"8-3-Numeric-conversions/","tags":["numeric conversions","conversions","static_cast"],"text":"Key Takeaway \u7b26\u5408\u6570\u503c\u63d0\u5347\u89c4\u5219\u7684\u7c7b\u578b\u8f6c\u6362\u662f\u9996\u5148\u662f\u6570\u503c\u63d0\u5347\uff0c\u4e0d\u662f\u6570\u503c\u8f6c\u6362 \u6570\u503c\u8f6c\u6362\u6db5\u76d6\u4e86\u6570\u503c\u63d0\u5347\u89c4\u5219\u672a\u6d89\u53ca\u7684\u5176\u4ed6\u7c7b\u578b\u8f6c\u6362 5 \u79cd\u57fa\u672c\u6570\u503c\u8f6c\u6362\uff1a \u6574\u578b\u8f6c\u6574\u578b \u6d6e\u70b9\u8f6c\u6d6e\u70b9 \u6d6e\u70b9\u8f6c\u6574\u578b \u6574\u578b\u8f6c\u6d6e\u70b9 \u6574\u578b\u6216\u6d6e\u70b9\u8f6c\u5e03\u5c14 \u6570\u503c\u63d0\u5347\u603b\u662f\u5b89\u5168\u7684\uff0c\u4f46\u662f\u6570\u503c\u8f6c\u6362\u4e0d\u4e00\u5b9a\uff0c\u53ef\u80fd\u9020\u6210\u6570\u636e\u6216\u7cbe\u5ea6\u4e22\u5931 \u7f29\u7a84\u8f6c\u6362\u4f1a\u9020\u6210\u7cbe\u5ea6\u4e22\u5931\u3002\u7f16\u8bd1\u5668\u4f1a\u5728\u53d1\u751f\u9690\u5f0f\u7f29\u7a84\u8f6c\u6362\u65f6\u62a5\u544a\u9519\u8bef \u5c3d\u53ef\u80fd\u907f\u514d\u7f29\u7a84\u8f6c\u6362\u3002\u5982\u679c\u4e00\u5b9a\u8981\u8fdb\u884c\u8f6c\u6362\uff0c\u4f7f\u7528 static_cast \u663e\u5f0f\u5730\u8fdb\u884c\u7f29\u7a84\u8f6c\u6362\u3002 \u62ec\u53f7\u521d\u59cb\u5316\u4e0d\u5141\u8bb8\u9690\u5f0f\u5730\u7f29\u7a84\u8f6c\u6362 \u4e00\u4e9b\u9700\u8981\u6ce8\u610f\u7684\u91cd\u8981\u89c4\u5219\uff1a \u5c06\u503c\u8f6c\u6362\u4e3a\u5176\u8303\u56f4\u4e0d\u652f\u6301\u8be5\u503c\u7684\u7c7b\u578b\u5c06\u53ef\u80fd\u5bfc\u81f4\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c \u53ea\u8981\u503c\u9002\u5408\u8f83\u5c0f\u7c7b\u578b\u7684\u8303\u56f4\uff0c\u4ece\u8f83\u5927\u7684\u6574\u578b\u6216\u6d6e\u70b9\u7c7b\u578b\u8f6c\u6362\u4e3a\u540c\u4e00\u65cf\u7684\u8f83\u5c0f\u7c7b\u578b\u901a\u5e38\u662f\u6709\u6548\u7684 \u6574\u6570\u8f6c\u6d6e\u70b9\u6570\uff0c\u53ea\u8981\u8303\u56f4ok\u5c31\u53ef\u4ee5 \u6d6e\u70b9\u6570\u8f6c\u6574\u6570\uff0c\u53ea\u8981\u8303\u56f4ok\u5c31\u53ef\u4ee5\uff0c\u4f46\u662f\u5c0f\u6570\u90e8\u5206\u4f1a\u4e22\u5931 \u5728\u4e0a\u4e00\u8282\u8bfe ( 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 ) \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86 \u6570\u503c\u63d0\u5347 \uff0c\u5b83\u5c5e\u4e8e\u4e00\u79cd\u7c7b\u578b\u8f6c\u6362\uff0c\u53ef\u4ee5\u5c06\u66f4\u7a84\u7684\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u66f4\u5bbd\u7684\u6570\u636e\u7c7b\u578b(\u901a\u5e38\u662f int \u6216 double ) \u4f7f\u5176\u53ef\u4ee5\u88ab\u66f4\u9ad8\u6548\u5730\u5904\u7406\u3002 C++ \u652f\u6301\u53e6\u4e00\u79cd\u7c7b\u6570\u503c\u7c7b\u578b\u8f6c\u6362\uff0c\u79f0\u4e3a \u6570\u503c\u8f6c\u6362(numeric conversions) \uff0c\u5b83\u6db5\u76d6\u4e86\u6570\u503c\u63d0\u5347\u89c4\u5219\u6ca1\u6709\u6db5\u76d6\u7684\u5176\u4ed6\u7c7b\u578b\u8f6c\u6362\u3002 \u5173\u952e\u4fe1\u606f \u4efb\u4f55\u7b26\u5408 \u6570\u503c\u63d0\u5347 \u89c4\u5219\uff08 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 \uff09\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u90fd\u5c5e\u4e8e \u6570\u503c\u63d0\u5347 \u800c\u4e0d\u662f\u6570\u503c\u8f6c\u6362\u3002 \u4e94\u79cd\u57fa\u672c\u7684\u6570\u503c\u8f6c\u6362\uff1a \u628a\u4e00\u79cd\u6574\u578b\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u6574\u578b\u7c7b\u578b\uff08\u6574\u578b\u63d0\u5347\u9664\u5916\uff09 1 2 3 short s = 3 ; // convert int to short long l = 3 ; // convert int to long char ch = s ; // convert short to char \u628a\u4e00\u79cd\u6d6e\u70b9\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u6d6e\u70b9\u7c7b\u578b\uff08\u6d6e\u70b9\u63d0\u5347\u9664\u5916\uff09: 1 2 float f = 3.0 ; // convert double to float long double ld = 3.0 ; // convert double to long double \u5c06\u6d6e\u70b9\u6570\u7c7b\u578b\u8f6c\u6362\u4e3a\u4efb\u4f55\u6574\u578b\u7c7b\u578b\uff1a 1 int i = 3.5 ; // convert double to int \u5c06\u6574\u578b\u7c7b\u578b\u8f6c\u6362\u4e3a\u4efb\u4f55\u6d6e\u70b9\u7c7b\u578b\uff1a 1 double d = 3 ; // convert int to double \u5c06\u6574\u578b\u6216\u6d6e\u70b9\u578b\u8f6c\u6362\u4e3abool\u7c7b\u578b\uff1a 1 2 bool b1 = 3 ; // convert int to bool bool b2 = 3.0 ; // convert double to bool \u9898\u5916\u8bdd \u7531\u4e8e\u5927\u62ec\u53f7\u521d\u59cb\u5316\u4e0d\u5141\u8bb8\u8fdb\u884c\u4e00\u4e9b\u6570\u503c\u8f6c\u6362(\u7a0d\u540e\u4f1a\u8be6\u7ec6\u4ecb\u7ecd)\uff0c\u4e3a\u4e86\u4f7f\u793a\u4f8b\u7b80\u5355\uff0c\u6211\u4eec\u5728\u672c\u8bfe\u4e2d\u4f7f\u7528\u4e86\u590d\u5236\u521d\u59cb\u5316(\u8fd9\u79cd\u521d\u59cb\u5316\u65b9\u6cd5\u6ca1\u6709\u8fd9\u6837\u7684\u9650\u5236)\u3002 \u7f29\u7a84\u8f6c\u6362 \u4e0e\u6570\u503c\u63d0\u5347(\u59cb\u7ec8\u662f\u5b89\u5168\u7684)\u4e0d\u540c\uff0c\u6570\u503c\u8f6c\u6362\u53ef\u80fd(\u4e5f\u53ef\u80fd\u4e0d\u4f1a)\u5bfc\u81f4\u6570\u636e\u6216\u7cbe\u5ea6\u7684\u4e22\u5931\u3002 \u6709\u4e9b\u6570\u503c\u8f6c\u6362\u603b\u662f\u5b89\u5168\u7684(\u4f8b\u5982 int \u5230 long \uff0c\u6216\u8005 int \u5230 double )\u3002\u5176\u4ed6\u6570\u503c\u8f6c\u6362\uff0c\u4f8b\u5982 double \u5230 int \uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u4e22\u5931(\u53d6\u51b3\u4e8e\u8f6c\u6362\u7684\u7279\u5b9a\u503c\u548c/\u6216\u57fa\u7840\u7c7b\u578b\u7684\u8303\u56f4)\uff1a 1 2 int i1 = 3.5 ; // the 0.5 is dropped, resulting in lost data int i2 = 3.0 ; // okay, will be converted to value 3, so no data is lost \u5728 C++ \u4e2d \u7f29\u7a84\u8f6c\u6362(narrowing conversion) \u662f\u4e00\u79cd\u53ef\u80fd\u4f1a\u9020\u6210\u6570\u636e\u4e22\u5931\u7684\u6570\u503c\u8f6c\u6362\u3002\u8fd9\u79cd\u7f29\u7a84\u8f6c\u6362\u5305\u62ec\u4e3a\uff1a \u6d6e\u70b9\u6570\u7c7b\u578b\u8f6c\u6362\u4e3a\u6574\u578b\u7c7b\u578b\uff1b \u8f83\u5bbd\u7684\u6d6e\u70b9\u7c7b\u578b\u8f6c\u6362\u4e3a\u8f83\u7a84\u7684\u6d6e\u70b9\u7c7b\u578b\uff0c\u9664\u975e\u88ab\u8f6c\u6362\u7684\u503c\u662f constexpr \u7c7b\u578b\u4e14\u5b83\u7684\u8303\u56f4\u5728\u76ee\u6807\u7c7b\u578b\u8303\u56f4\u5185(\u5373\u4f7f\u8f83\u7a84\u7684\u7c7b\u578b\u4e0d\u5177\u5907\u5b58\u653e\u5b8c\u6574\u6570\u636e\u7684\u7cbe\u5ea6\uff09\u3002 \u4ece\u6574\u6570\u7c7b\u578b\u8f6c\u6362\u4e3a\u6d6e\u70b9\u7c7b\u578b\uff0c\u9664\u975e\u8f6c\u6362\u7684\u503c\u662f constexpr \u4e14\u5728\u76ee\u6807\u7c7b\u578b\u8303\u56f4\u5185\uff0c\u5e76\u4e14\u53ef\u4ee5\u8f6c\u6362\u56de\u539f\u59cb\u7c7b\u578b\u800c\u4e0d\u4e22\u5931\u6570\u636e\u3002 \u4ece\u8f83\u5bbd\u7684\u6574\u578b\u8f6c\u6362\u4e3a\u8f83\u7a84\u7684\u6574\u578b\uff0c\u9664\u975e\u8f6c\u6362\u7684\u503c\u662f constexpr \u4e14\u6574\u578b\u63d0\u5347\u540e\u9002\u5408\u76ee\u6807\u7c7b\u578b\u3002 \u597d\u6d88\u606f\u662f\u4f60\u4e0d\u9700\u8981\u8bb0\u4f4f\u8fd9\u4e9b\u3002\u5f53\u7f16\u8bd1\u5668\u786e\u5b9a\u9700\u8981\u9690\u5f0f\u7f29\u7a84\u8f6c\u6362\u65f6\uff0c\u5b83\u901a\u5e38\u4f1a\u53d1\u51fa\u8b66\u544a(\u6216\u9519\u8bef)\u3002 \u6ce8\u610f \u5f53\u5c06 signed int \u8f6c\u6362\u4e3a unsigned int \u65f6\uff0c\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a \u4e0d\u4f1a \u53d1\u51fa\u8b66\u544a\uff0c\u53cd\u4e4b\u4ea6\u7136\uff0c\u5373\u4f7f\u8fd9\u4e9b\u662f\u7f29\u7a84\u8f6c\u6362\u3002\u8981\u7279\u522b\u5c0f\u5fc3\u8fd9\u4e9b\u7c7b\u578b\u4e4b\u95f4\u65e0\u610f\u7684\u8f6c\u6362(\u7279\u522b\u662f\u5f53\u5411\u5e26\u76f8\u53cd\u7b26\u53f7\u5f62\u53c2\u7684\u51fd\u6570\u4f20\u9012\u5b9e\u53c2\u65f6)\u3002 \u4f8b\u5982\uff0c\u5f53\u7f16\u8bd1\u4e0b\u9762\u7684\u7a0b\u5e8f\u65f6\uff1a 1 2 3 4 int main () { int i = 3.5 ; } Visual Studio \u4f1a\u4ea7\u751f\u5982\u4e0b\u544a\u8b66\u4fe1\u606f\uff1a 1 warning C4244: 'initializing': conversion from 'double' to 'int', possible loss of data \u4e00\u822c\u6765\u8bf4\uff0c\u5e94\u8be5\u907f\u514d\u7f29\u7a84\u8f6c\u6362\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u9700\u8981\u8fd9\u6837\u505a\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u60a8\u5e94\u8be5\u4f7f\u7528 static_cast \u663e\u5f0f\u5730\u8fdb\u884c\u7f29\u7a84\u8f6c\u6362\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 void someFcn ( int i ) { } int main () { double d { 5.0 }; someFcn ( d ); // \u4e0d\u597d: \u7f16\u8bd1\u5668\u4f1a\u4ea7\u751f\u7f29\u7a84\u8f6c\u6362\u544a\u8b66 someFcn ( static_cast < int > ( d )); // \u597d\uff1a\u663e\u5f0f\u5730\u544a\u77e5\u7f16\u8bd1\u5668\u4ec5\u7f29\u7a84\u8f6c\u6362\uff0c\u4e0d\u4f1a\u4ea7\u751f\u544a\u8b66 return 0 ; } \u6700\u4f73\u5b9e\u8df5 \u5c3d\u53ef\u80fd\u907f\u514d\u7f29\u7a84\u8f6c\u6362\u3002\u5982\u679c\u4e00\u5b9a\u8981\u8fdb\u884c\u8f6c\u6362\uff0c\u4f7f\u7528 static_cast \u663e\u5f0f\u5730\u8fdb\u884c\u7f29\u7a84\u8f6c\u6362\u3002 \u62ec\u53f7\u521d\u59cb\u5316\u4e0d\u5141\u8bb8\u9690\u5f0f\u7f29\u7a84\u8f6c\u6362 \u5728\u4f7f\u7528\u5927\u62ec\u53f7\u521d\u59cb\u5316\u65f6\uff0c\u4e25\u683c\u7981\u6b62\u7f29\u7a84\u8f6c\u6362(\u8fd9\u4e5f\u662f\u9996\u9009\u6b64\u521d\u59cb\u5316\u5f62\u5f0f\u7684\u4e3b\u8981\u539f\u56e0\u4e4b\u4e00)\uff1a 1 2 3 4 int main () { int i { 3.5 }; // won't compile } Visual Studio \u4f1a\u4ea7\u751f\u5982\u4e0b\u9519\u8bef\u4fe1\u606f\uff1a 1 error C2397: conversion from 'double' to 'int' requires a narrowing conversion \u5173\u4e8e\u6570\u503c\u8f6c\u6362\u7684\u66f4\u591a\u4fe1\u606f \u6570\u503c\u8f6c\u6362\u7684\u5177\u4f53\u89c4\u5219\u53c8\u591a\u53c8\u590d\u6742\uff0c\u6240\u4ee5\u8fd9\u91cc\u6211\u4eec\u53ea\u8bb2\u6700\u91cd\u8981\u3001\u6700\u9700\u8981\u8bb0\u5fc6\u7684\u3002 \u5728 \u6240\u6709 \u60c5\u51b5\u4e0b\uff0c\u5c06\u503c\u8f6c\u6362\u4e3a\u5176\u8303\u56f4\u4e0d\u652f\u6301\u8be5\u503c\u7684\u7c7b\u578b\u5c06\u53ef\u80fd\u5bfc\u81f4\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 int main () { int i { 30000 }; char c = i ; // chars have range -128 to 127 std :: cout << static_cast < int > ( c ); return 0 ; } \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4e00\u4e2a\u5927\u6574\u6570\u8d4b\u7ed9\u4e00\u4e2a char \u578b\u53d8\u91cf(\u8303\u56f4\u4e3a-128\u5230127)\u3002\u8fd9\u4f1a\u5bfc\u81f4 char \u6ea2\u51fa\uff0c\u5e76\u4ea7\u751f\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c: 1 48 \u53ea\u8981\u503c\u9002\u5408\u8f83\u5c0f\u7c7b\u578b\u7684\u8303\u56f4\uff0c\u4ece\u8f83\u5927\u7684\u6574\u578b\u6216\u6d6e\u70b9\u7c7b\u578b\u8f6c\u6362\u4e3a\u540c\u4e00\u65cf\u7684\u8f83\u5c0f\u7c7b\u578b\u901a\u5e38\u662f\u6709\u6548\u7684\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 int i { 2 }; short s = i ; // convert from int to short std :: cout << s << '\\n' ; double d { 0.1234 }; float f = d ; std :: cout << f << '\\n' ; \u80fd\u591f\u8f93\u51fa\u9884\u671f\u7684\u7ed3\u679c\uff1a 1 2 2 0.1234 \u5bf9\u4e8e\u6d6e\u70b9\u6570\u7684\u4f8b\u5b50\uff0c\u5728\u66f4\u5c0f\u7684\u7c7b\u578b\u4e2d\u53ef\u80fd\u51fa\u73b0\u820d\u5165\u8bef\u5dee\uff0c\u4f8b\u5982\uff1a 1 2 float f = 0.123456789 ; // double \u503c 0.123456789 \u6709 9 \u4f4d\u6709\u6548\u6570\u7ec4\uff0c\u4f46\u662f float \u53ea\u80fd\u652f\u6301 7 \u4f4d\u6709\u6548\u6570\u5b57 std :: cout << std :: setprecision ( 9 ) << f << '\\n' ; // std::setprecision defined in iomanip header \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u7531\u4e8e float \u4e0d\u80fd\u652f\u6301 double \u7c7b\u578b\u7684\u7cbe\u5ea6\uff0c\u51fa\u73b0\u4e86\u7cbe\u5ea6\u4e22\u5931\u7684\u95ee\u9898\uff1a 1 0.123456791 \u4ece\u6574\u6570\u5230\u6d6e\u70b9\u6570\u7684\u8f6c\u6362\u901a\u5e38\u53ea\u8981\u503c\u5728\u6d6e\u70b9\u7c7b\u578b\u7684\u8303\u56f4\u5185\u5c31\u53ef\u4ee5\u5de5\u4f5c\u3002\u4f8b\u5982: 1 2 3 int i { 10 }; float f = i ; std :: cout << f ; \u53ef\u4ee5\u8f93\u51fa\u9884\u671f\u7684\u7ed3\u679c\uff1a 1 10 \u4ece\u6d6e\u70b9\u6570\u5230\u6574\u6570\u7684\u8f6c\u6362\u53ea\u8981\u503c\u5728\u6574\u6570\u7684\u8303\u56f4\u5185\u5c31\u53ef\u4ee5\u5de5\u4f5c\uff0c\u4f46\u662f\u4efb\u4f55\u5c0f\u6570\u503c\u90fd\u4f1a\u4e22\u5931\u3002\u4f8b\u5982: 1 2 int i = 3.5 ; std :: cout << i << '\\n' ; \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4e22\u5931\u4e86\u5c0f\u6570\u503c(.5)\uff0c\u7559\u4e0b\u4ee5\u4e0b\u7ed3\u679c\uff1a 1 3 \u867d\u7136\u6570\u503c\u8f6c\u6362\u89c4\u5219\u770b\u8d77\u6765\u5f88\u53ef\u6015\uff0c\u4f46\u5b9e\u9645\u4e0a\uff0c\u5982\u679c\u4f60\u8bd5\u56fe\u505a\u4e00\u4e9b\u5371\u9669\u7684\u4e8b\u60c5(\u4e0d\u5305\u62ec\u4e00\u4e9b\u6709\u7b26\u53f7/\u65e0\u7b26\u53f7\u7684\u8f6c\u6362)\uff0c\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u53d1\u51fa\u8b66\u544a\u3002","title":"8.3 - \u6570\u503c\u8f6c\u6362"},{"location":"8-3-Numeric-conversions/#_1","text":"\u628a\u4e00\u79cd\u6574\u578b\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u6574\u578b\u7c7b\u578b\uff08\u6574\u578b\u63d0\u5347\u9664\u5916\uff09 1 2 3 short s = 3 ; // convert int to short long l = 3 ; // convert int to long char ch = s ; // convert short to char \u628a\u4e00\u79cd\u6d6e\u70b9\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u6d6e\u70b9\u7c7b\u578b\uff08\u6d6e\u70b9\u63d0\u5347\u9664\u5916\uff09: 1 2 float f = 3.0 ; // convert double to float long double ld = 3.0 ; // convert double to long double \u5c06\u6d6e\u70b9\u6570\u7c7b\u578b\u8f6c\u6362\u4e3a\u4efb\u4f55\u6574\u578b\u7c7b\u578b\uff1a 1 int i = 3.5 ; // convert double to int \u5c06\u6574\u578b\u7c7b\u578b\u8f6c\u6362\u4e3a\u4efb\u4f55\u6d6e\u70b9\u7c7b\u578b\uff1a 1 double d = 3 ; // convert int to double \u5c06\u6574\u578b\u6216\u6d6e\u70b9\u578b\u8f6c\u6362\u4e3abool\u7c7b\u578b\uff1a 1 2 bool b1 = 3 ; // convert int to bool bool b2 = 3.0 ; // convert double to bool \u9898\u5916\u8bdd \u7531\u4e8e\u5927\u62ec\u53f7\u521d\u59cb\u5316\u4e0d\u5141\u8bb8\u8fdb\u884c\u4e00\u4e9b\u6570\u503c\u8f6c\u6362(\u7a0d\u540e\u4f1a\u8be6\u7ec6\u4ecb\u7ecd)\uff0c\u4e3a\u4e86\u4f7f\u793a\u4f8b\u7b80\u5355\uff0c\u6211\u4eec\u5728\u672c\u8bfe\u4e2d\u4f7f\u7528\u4e86\u590d\u5236\u521d\u59cb\u5316(\u8fd9\u79cd\u521d\u59cb\u5316\u65b9\u6cd5\u6ca1\u6709\u8fd9\u6837\u7684\u9650\u5236)\u3002","title":"\u4e94\u79cd\u57fa\u672c\u7684\u6570\u503c\u8f6c\u6362\uff1a"},{"location":"8-3-Numeric-conversions/#_2","text":"\u4e0e\u6570\u503c\u63d0\u5347(\u59cb\u7ec8\u662f\u5b89\u5168\u7684)\u4e0d\u540c\uff0c\u6570\u503c\u8f6c\u6362\u53ef\u80fd(\u4e5f\u53ef\u80fd\u4e0d\u4f1a)\u5bfc\u81f4\u6570\u636e\u6216\u7cbe\u5ea6\u7684\u4e22\u5931\u3002 \u6709\u4e9b\u6570\u503c\u8f6c\u6362\u603b\u662f\u5b89\u5168\u7684(\u4f8b\u5982 int \u5230 long \uff0c\u6216\u8005 int \u5230 double )\u3002\u5176\u4ed6\u6570\u503c\u8f6c\u6362\uff0c\u4f8b\u5982 double \u5230 int \uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u4e22\u5931(\u53d6\u51b3\u4e8e\u8f6c\u6362\u7684\u7279\u5b9a\u503c\u548c/\u6216\u57fa\u7840\u7c7b\u578b\u7684\u8303\u56f4)\uff1a 1 2 int i1 = 3.5 ; // the 0.5 is dropped, resulting in lost data int i2 = 3.0 ; // okay, will be converted to value 3, so no data is lost \u5728 C++ \u4e2d \u7f29\u7a84\u8f6c\u6362(narrowing conversion) \u662f\u4e00\u79cd\u53ef\u80fd\u4f1a\u9020\u6210\u6570\u636e\u4e22\u5931\u7684\u6570\u503c\u8f6c\u6362\u3002\u8fd9\u79cd\u7f29\u7a84\u8f6c\u6362\u5305\u62ec\u4e3a\uff1a \u6d6e\u70b9\u6570\u7c7b\u578b\u8f6c\u6362\u4e3a\u6574\u578b\u7c7b\u578b\uff1b \u8f83\u5bbd\u7684\u6d6e\u70b9\u7c7b\u578b\u8f6c\u6362\u4e3a\u8f83\u7a84\u7684\u6d6e\u70b9\u7c7b\u578b\uff0c\u9664\u975e\u88ab\u8f6c\u6362\u7684\u503c\u662f constexpr \u7c7b\u578b\u4e14\u5b83\u7684\u8303\u56f4\u5728\u76ee\u6807\u7c7b\u578b\u8303\u56f4\u5185(\u5373\u4f7f\u8f83\u7a84\u7684\u7c7b\u578b\u4e0d\u5177\u5907\u5b58\u653e\u5b8c\u6574\u6570\u636e\u7684\u7cbe\u5ea6\uff09\u3002 \u4ece\u6574\u6570\u7c7b\u578b\u8f6c\u6362\u4e3a\u6d6e\u70b9\u7c7b\u578b\uff0c\u9664\u975e\u8f6c\u6362\u7684\u503c\u662f constexpr \u4e14\u5728\u76ee\u6807\u7c7b\u578b\u8303\u56f4\u5185\uff0c\u5e76\u4e14\u53ef\u4ee5\u8f6c\u6362\u56de\u539f\u59cb\u7c7b\u578b\u800c\u4e0d\u4e22\u5931\u6570\u636e\u3002 \u4ece\u8f83\u5bbd\u7684\u6574\u578b\u8f6c\u6362\u4e3a\u8f83\u7a84\u7684\u6574\u578b\uff0c\u9664\u975e\u8f6c\u6362\u7684\u503c\u662f constexpr \u4e14\u6574\u578b\u63d0\u5347\u540e\u9002\u5408\u76ee\u6807\u7c7b\u578b\u3002 \u597d\u6d88\u606f\u662f\u4f60\u4e0d\u9700\u8981\u8bb0\u4f4f\u8fd9\u4e9b\u3002\u5f53\u7f16\u8bd1\u5668\u786e\u5b9a\u9700\u8981\u9690\u5f0f\u7f29\u7a84\u8f6c\u6362\u65f6\uff0c\u5b83\u901a\u5e38\u4f1a\u53d1\u51fa\u8b66\u544a(\u6216\u9519\u8bef)\u3002 \u6ce8\u610f \u5f53\u5c06 signed int \u8f6c\u6362\u4e3a unsigned int \u65f6\uff0c\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a \u4e0d\u4f1a \u53d1\u51fa\u8b66\u544a\uff0c\u53cd\u4e4b\u4ea6\u7136\uff0c\u5373\u4f7f\u8fd9\u4e9b\u662f\u7f29\u7a84\u8f6c\u6362\u3002\u8981\u7279\u522b\u5c0f\u5fc3\u8fd9\u4e9b\u7c7b\u578b\u4e4b\u95f4\u65e0\u610f\u7684\u8f6c\u6362(\u7279\u522b\u662f\u5f53\u5411\u5e26\u76f8\u53cd\u7b26\u53f7\u5f62\u53c2\u7684\u51fd\u6570\u4f20\u9012\u5b9e\u53c2\u65f6)\u3002 \u4f8b\u5982\uff0c\u5f53\u7f16\u8bd1\u4e0b\u9762\u7684\u7a0b\u5e8f\u65f6\uff1a 1 2 3 4 int main () { int i = 3.5 ; } Visual Studio \u4f1a\u4ea7\u751f\u5982\u4e0b\u544a\u8b66\u4fe1\u606f\uff1a 1 warning C4244: 'initializing': conversion from 'double' to 'int', possible loss of data \u4e00\u822c\u6765\u8bf4\uff0c\u5e94\u8be5\u907f\u514d\u7f29\u7a84\u8f6c\u6362\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u9700\u8981\u8fd9\u6837\u505a\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u60a8\u5e94\u8be5\u4f7f\u7528 static_cast \u663e\u5f0f\u5730\u8fdb\u884c\u7f29\u7a84\u8f6c\u6362\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 void someFcn ( int i ) { } int main () { double d { 5.0 }; someFcn ( d ); // \u4e0d\u597d: \u7f16\u8bd1\u5668\u4f1a\u4ea7\u751f\u7f29\u7a84\u8f6c\u6362\u544a\u8b66 someFcn ( static_cast < int > ( d )); // \u597d\uff1a\u663e\u5f0f\u5730\u544a\u77e5\u7f16\u8bd1\u5668\u4ec5\u7f29\u7a84\u8f6c\u6362\uff0c\u4e0d\u4f1a\u4ea7\u751f\u544a\u8b66 return 0 ; } \u6700\u4f73\u5b9e\u8df5 \u5c3d\u53ef\u80fd\u907f\u514d\u7f29\u7a84\u8f6c\u6362\u3002\u5982\u679c\u4e00\u5b9a\u8981\u8fdb\u884c\u8f6c\u6362\uff0c\u4f7f\u7528 static_cast \u663e\u5f0f\u5730\u8fdb\u884c\u7f29\u7a84\u8f6c\u6362\u3002","title":"\u7f29\u7a84\u8f6c\u6362"},{"location":"8-3-Numeric-conversions/#_3","text":"\u5728\u4f7f\u7528\u5927\u62ec\u53f7\u521d\u59cb\u5316\u65f6\uff0c\u4e25\u683c\u7981\u6b62\u7f29\u7a84\u8f6c\u6362(\u8fd9\u4e5f\u662f\u9996\u9009\u6b64\u521d\u59cb\u5316\u5f62\u5f0f\u7684\u4e3b\u8981\u539f\u56e0\u4e4b\u4e00)\uff1a 1 2 3 4 int main () { int i { 3.5 }; // won't compile } Visual Studio \u4f1a\u4ea7\u751f\u5982\u4e0b\u9519\u8bef\u4fe1\u606f\uff1a 1 error C2397: conversion from 'double' to 'int' requires a narrowing conversion","title":"\u62ec\u53f7\u521d\u59cb\u5316\u4e0d\u5141\u8bb8\u9690\u5f0f\u7f29\u7a84\u8f6c\u6362"},{"location":"8-3-Numeric-conversions/#_4","text":"\u6570\u503c\u8f6c\u6362\u7684\u5177\u4f53\u89c4\u5219\u53c8\u591a\u53c8\u590d\u6742\uff0c\u6240\u4ee5\u8fd9\u91cc\u6211\u4eec\u53ea\u8bb2\u6700\u91cd\u8981\u3001\u6700\u9700\u8981\u8bb0\u5fc6\u7684\u3002 \u5728 \u6240\u6709 \u60c5\u51b5\u4e0b\uff0c\u5c06\u503c\u8f6c\u6362\u4e3a\u5176\u8303\u56f4\u4e0d\u652f\u6301\u8be5\u503c\u7684\u7c7b\u578b\u5c06\u53ef\u80fd\u5bfc\u81f4\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 int main () { int i { 30000 }; char c = i ; // chars have range -128 to 127 std :: cout << static_cast < int > ( c ); return 0 ; } \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4e00\u4e2a\u5927\u6574\u6570\u8d4b\u7ed9\u4e00\u4e2a char \u578b\u53d8\u91cf(\u8303\u56f4\u4e3a-128\u5230127)\u3002\u8fd9\u4f1a\u5bfc\u81f4 char \u6ea2\u51fa\uff0c\u5e76\u4ea7\u751f\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c: 1 48 \u53ea\u8981\u503c\u9002\u5408\u8f83\u5c0f\u7c7b\u578b\u7684\u8303\u56f4\uff0c\u4ece\u8f83\u5927\u7684\u6574\u578b\u6216\u6d6e\u70b9\u7c7b\u578b\u8f6c\u6362\u4e3a\u540c\u4e00\u65cf\u7684\u8f83\u5c0f\u7c7b\u578b\u901a\u5e38\u662f\u6709\u6548\u7684\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 int i { 2 }; short s = i ; // convert from int to short std :: cout << s << '\\n' ; double d { 0.1234 }; float f = d ; std :: cout << f << '\\n' ; \u80fd\u591f\u8f93\u51fa\u9884\u671f\u7684\u7ed3\u679c\uff1a 1 2 2 0.1234 \u5bf9\u4e8e\u6d6e\u70b9\u6570\u7684\u4f8b\u5b50\uff0c\u5728\u66f4\u5c0f\u7684\u7c7b\u578b\u4e2d\u53ef\u80fd\u51fa\u73b0\u820d\u5165\u8bef\u5dee\uff0c\u4f8b\u5982\uff1a 1 2 float f = 0.123456789 ; // double \u503c 0.123456789 \u6709 9 \u4f4d\u6709\u6548\u6570\u7ec4\uff0c\u4f46\u662f float \u53ea\u80fd\u652f\u6301 7 \u4f4d\u6709\u6548\u6570\u5b57 std :: cout << std :: setprecision ( 9 ) << f << '\\n' ; // std::setprecision defined in iomanip header \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u7531\u4e8e float \u4e0d\u80fd\u652f\u6301 double \u7c7b\u578b\u7684\u7cbe\u5ea6\uff0c\u51fa\u73b0\u4e86\u7cbe\u5ea6\u4e22\u5931\u7684\u95ee\u9898\uff1a 1 0.123456791 \u4ece\u6574\u6570\u5230\u6d6e\u70b9\u6570\u7684\u8f6c\u6362\u901a\u5e38\u53ea\u8981\u503c\u5728\u6d6e\u70b9\u7c7b\u578b\u7684\u8303\u56f4\u5185\u5c31\u53ef\u4ee5\u5de5\u4f5c\u3002\u4f8b\u5982: 1 2 3 int i { 10 }; float f = i ; std :: cout << f ; \u53ef\u4ee5\u8f93\u51fa\u9884\u671f\u7684\u7ed3\u679c\uff1a 1 10 \u4ece\u6d6e\u70b9\u6570\u5230\u6574\u6570\u7684\u8f6c\u6362\u53ea\u8981\u503c\u5728\u6574\u6570\u7684\u8303\u56f4\u5185\u5c31\u53ef\u4ee5\u5de5\u4f5c\uff0c\u4f46\u662f\u4efb\u4f55\u5c0f\u6570\u503c\u90fd\u4f1a\u4e22\u5931\u3002\u4f8b\u5982: 1 2 int i = 3.5 ; std :: cout << i << '\\n' ; \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4e22\u5931\u4e86\u5c0f\u6570\u503c(.5)\uff0c\u7559\u4e0b\u4ee5\u4e0b\u7ed3\u679c\uff1a 1 3 \u867d\u7136\u6570\u503c\u8f6c\u6362\u89c4\u5219\u770b\u8d77\u6765\u5f88\u53ef\u6015\uff0c\u4f46\u5b9e\u9645\u4e0a\uff0c\u5982\u679c\u4f60\u8bd5\u56fe\u505a\u4e00\u4e9b\u5371\u9669\u7684\u4e8b\u60c5(\u4e0d\u5305\u62ec\u4e00\u4e9b\u6709\u7b26\u53f7/\u65e0\u7b26\u53f7\u7684\u8f6c\u6362)\uff0c\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u53d1\u51fa\u8b66\u544a\u3002","title":"\u5173\u4e8e\u6570\u503c\u8f6c\u6362\u7684\u66f4\u591a\u4fe1\u606f"},{"location":"8-4-Arithmetic-conversions/","tags":["arithmetic conversions","conversions","typeinfo"],"text":"Key Takeaway C++ \u4e2d\u7684\u4e00\u4e9b\u4e8c\u5143\u8fd0\u7b97\u7b26\u8981\u6c42\u4e24\u4e2a\u64cd\u4f5c\u6570\u5177\u6709\u76f8\u540c\u7684\u4f18\u5148\u7ea7\uff0c\u5982\u679c\u4e0d\u6ee1\u8db3\u5219\u9700\u8981\u8fdb\u884c\u7b97\u672f\u8f6c\u6362 \u7b97\u672f\u8f6c\u6362\u89c4\u5219 \u5982\u679c\u81f3\u5c11\u6709\u4e00\u4e2a\u64cd\u4f5c\u6570\u5728\u4f18\u5148\u7ea7\u5217\u8868\u4e2d\uff0c\u5219\u5177\u6709\u8f83\u4f4e\u4f18\u5148\u7ea7\u7684\u64cd\u4f5c\u6570\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5177\u6709\u8f83\u9ad8\u4f18\u5148\u7ea7\u7684\u64cd\u4f5c\u6570\uff1b \u5426\u5219 (\u4e24\u4e2a\u64cd\u4f5c\u6570\u7684\u7c7b\u578b\u5747\u4e0d\u5728\u8868\u4e2d)\uff0c\u5219\u4e24\u4e2a\u64cd\u4f5c\u6570\u4f1a\u8fdb\u884c \u6570\u503c\u63d0\u5347 (\u53c2\u89c1\uff1a 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 )\u3002 \u4f7f\u7528 typeid \u8fd0\u7b97\u7b26 (\u5728 \u5934\u6587\u4ef6\u4e2d)\uff0c\u6765\u663e\u793a\u8868\u8fbe\u5f0f\u7ed3\u679c\u6240\u5c5e\u7684\u7c7b\u578b\u3002 \u65e0\u7b26\u53f7\u6574\u578b \u88ab\u7528\u5728\u7b97\u672f\u8868\u8fbe\u5f0f\u4e2d\u7684\u65f6\u5019\uff0c\u7531\u4e8e\u4f18\u5148\u7ea7\u6bd4 \u6574\u578b \u9ad8\uff0c\u4f1a\u5bfc\u81f4\u6574\u578b\u88ab\u8f6c\u6362\u4e3a\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u5982\u679c\u662f\u7b26\u53f7\u53ef\u80fd\u51fa\u73b0\u53cd\u8f6c\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u4e0d\u8981\u4f7f\u7528\u65e0\u7b26\u53f7\u6574\u578b\u7684\u539f\u56e0 \u5728 5.1 - \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b \u4e2d\u6211\u4eec\u8ba8\u8bba\u8fc7\uff0c\u8868\u8fbe\u5f0f\u662f\u5982\u4f55\u57fa\u4e8e\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b\u8fdb\u884c\u8fd0\u7b97\u7684\u3002 \u8003\u8651\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a 1 int x { 2 + 3 }; \u5f53\u4e8c\u5143\u8fd0\u7b97\u7b26 operator+ \u6267\u884c\u65f6\uff0c\u5b83\u63a5\u53d7\u4e86\u4e24\u4e2a\u64cd\u4f5c\u6570\uff0c\u4e14\u5747\u4e3a int \u7c7b\u578b\u3002\u56e0\u4e3a\u4e24\u4e2a\u64cd\u4f5c\u6570\u7684\u7c7b\u578b\u662f\u76f8\u540c\u7684\uff0c\u6240\u4ee5\u5b83\u4eec\u4f1a\u88ab\u7528\u6765\u6267\u884c\u8ba1\u7b97\u7136\u540e\u8fd4\u56de\u7ed3\u679c\u3002\u56e0\u6b64 2 + 3 \u4f1a\u5f97\u5230 int \u503c 5 \u3002 \u4f46\u662f\uff0c\u5982\u679c\u4e24\u4e2a\u64cd\u4f5c\u6570\u7c7b\u578b\u4e0d\u540c\u5462\uff1f 1 ??? y { 2 + 3.5 }; \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c operator+ \u7684\u4e24\u4e2a\u64cd\u4f5c\u6570\uff0c\u4e00\u4e2a\u662f int \uff0c\u53e6\u4e00\u4e2a\u662f double \u3002\u90a3\u4e48\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u5e94\u8be5\u662f\u4ec0\u4e48\u7c7b\u578b\u7684\u5462\uff1f\u662f int \u8fd8\u662f double \uff1f\u8fd8\u662f\u5176\u4ed6\u7684\u5462\uff1f\u5982\u679c\u662f\u5728\u5b9a\u4e49\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u5728\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u4f8b\u5982\u5728\u4f7f\u7528 std::cout << \u7684\u65f6\u5019\uff0c\u8ba1\u7b97\u5f97\u5230\u7684\u7c7b\u578b\u4f1a\u5f71\u54cd\u8f93\u51fa\u7684\u7ed3\u679c\u3002 \u5728C++\u4e2d\uff0c\u67d0\u4e9b\u64cd\u4f5c\u7b26\u8981\u6c42\u5b83\u4eec\u7684\u64cd\u4f5c\u6570\u5177\u6709\u76f8\u540c\u7684\u7c7b\u578b\u3002\u5982\u679c\u5728\u8c03\u7528\u8fd0\u7b97\u7b26\u65f6\uff0c\u4e24\u4e2a\u64cd\u4f5c\u6570\u7c7b\u578b\u4e0d\u540c\uff0c\u5219\u4f7f\u7528\u4e00\u7ec4\u79f0\u4e3a\u666e\u901a\u7b97\u672f\u8f6c\u6362\u7684\u89c4\u5219\u5c06\u5176\u4e2d\u4e00\u4e2a\u6216\u4e24\u4e2a\u64cd\u4f5c\u6570\u9690\u5f0f\u8f6c\u6362\u4e3a\u5339\u914d\u7c7b\u578b\u3002 \u9700\u8981\u64cd\u4f5c\u6570\u5177\u6709\u76f8\u540c\u7c7b\u578b\u7684\u8fd0\u7b97\u7b26 \u4e0b\u5217\u8fd0\u7b97\u7b26\u9700\u8981\u64cd\u4f5c\u6570\u5177\u6709\u76f8\u540c\u7c7b\u578b\uff1a \u4e8c\u5143\u7b97\u672f\u8fd0\u7b97\u7b26\uff1a + , - , * , / , % \u4e8c\u5143\u5173\u7cfb\u8fd0\u7b97\u7b26\uff1a < , > , <= , >= , == , != \u4e8c\u5143\u6309\u4f4d\u8fd0\u7b97\u7b26\uff1a & , ^ , | \u6761\u4ef6\u8fd0\u7b97\u7b26 ?: (\u5305\u62ec\u6761\u4ef6\uff0c\u8981\u6c42\u4e3a\u5e03\u5c14\u7c7b\u578b) \u4e00\u822c\u7b97\u672f\u8f6c\u6362\u89c4\u5219 \u4e00\u822c\u7b97\u672f\u8f6c\u6362\u89c4\u5219\u975e\u5e38\u7b80\u5355\u3002\u7f16\u8bd1\u5668\u5177\u6709\u4e00\u4e2a\u6309\u7167\u4f18\u5148\u7ea7\u6392\u5e8f\u7684\u7c7b\u578b\u5217\u8868\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a long double (\u6700\u9ad8\u4f18\u5148\u7ea7) double float unsigned long long long long unsigned long long unsigned int int (\u6700\u4f4e\u4f18\u5148\u7ea7) \u4ee5\u53ca\u4e24\u6761\u89c4\u5219\uff1a \u5982\u679c\u81f3\u5c11\u6709\u4e00\u4e2a\u64cd\u4f5c\u6570\u5728\u4f18\u5148\u7ea7\u5217\u8868\u4e2d\uff0c\u5219\u5177\u6709\u8f83\u4f4e\u4f18\u5148\u7ea7\u7684\u64cd\u4f5c\u6570\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5177\u6709\u8f83\u9ad8\u4f18\u5148\u7ea7\u7684\u64cd\u4f5c\u6570\uff1b \u5426\u5219 (\u4e24\u4e2a\u64cd\u4f5c\u6570\u7684\u7c7b\u578b\u5747\u4e0d\u5728\u8868\u4e2d)\uff0c\u5219\u4e24\u4e2a\u64cd\u4f5c\u6570\u4f1a\u8fdb\u884c \u6570\u503c\u63d0\u5347 (\u53c2\u89c1\uff1a 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 )\u3002 \u4e00\u4e9b\u4f8b\u5b50 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528 typeid \u8fd0\u7b97\u7b26 (\u5728 \u5934\u6587\u4ef6\u4e2d)\uff0c\u6765\u663e\u793a\u8868\u8fbe\u5f0f\u7ed3\u679c\u6240\u5c5e\u7684\u7c7b\u578b\u3002 \u9996\u5148\uff0c\u5c06 int \u548c double \u76f8\u52a0\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include #include // for typeid() int main () { int i { 2 }; double d { 3.5 }; std :: cout << typeid ( i + d ). name () << ' ' << i + d << '\\n' ; // \u663e\u793a i + d \u7684\u7c7b\u578b return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c double \u7c7b\u578b\u64cd\u4f5c\u6570\u7684\u4f18\u5148\u7ea7\u6700\u9ad8\uff0c\u6240\u4ee5\u4f18\u5148\u7ea7\u8f83\u4f4e\u7684\u64cd\u4f5c\u6570 (\u4f8b\u5982 int ) \u7684\u7c7b\u578b\u4f1a\u88ab\u8f6c\u6362\u4e3a double \u503c 2.0 . Then double values 2.0 and 3.5 are added to produce double result 5.5 . \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u4fe1\u606f\uff1a 1 double 5.5 \u6ce8\u610f\uff0c\u4f60\u7684\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u4ea7\u751f\u7a0d\u5fae\u4e0d\u540c\u7684\u7ed3\u679c\uff0c\u56e0\u4e3a typeid.name() \u8f93\u51fa\u7684\u7ed3\u679c\u662f\u7531\u7f16\u8bd1\u5668\u51b3\u5b9a\u7684\u3002 \u8bd1\u8005\u6ce8 \u5728g++\u4e0a\u4f7f\u7528 typeid \u8fd0\u7b97\u7b26 \uff0c\u8f93\u51fa\u7684\u7ed3\u679c\u4e3a \u4fee\u9970\u540d \uff0c\u53ef\u4ee5\u4f7f\u7528 1 a.out | c++filt --types \u5f97\u5230\u53ef\u8bfb\u7684\u7ed3\u679c\u3002 \u53c2\u8003 \u73b0\u5728\uff0c\u5c06\u4e24\u4e2a short \u7c7b\u578b\u7684\u503c\u76f8\u52a0\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include #include // for typeid() int main () { short a { 4 }; short b { 5 }; std :: cout << typeid ( a + b ). name () << ' ' << a + b << '\\n' ; // \u663e\u793a a + b \u7684\u7c7b\u578b return 0 ; } \u56e0\u4e3a\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u6ca1\u6709\u51fa\u73b0\u5728\u4f18\u5148\u7ea7\u5217\u8868\u4e2d\uff0c\u6240\u4ee5\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u88ab\u6574\u578b\u63d0\u5347\u4e3a int \u7c7b\u578b\u3002\u4e24\u4e2a int \u76f8\u52a0\u7684\u7ed3\u679c\u662f\u4e00\u4e2a int \uff0c\u6b63\u5982\u4f60\u6240\u671f\u671b\u7684\u90a3\u6837: 1 int 9 \u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u95ee\u9898 \u5f53\u6df7\u5408\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u503c\u65f6\uff0c\u8fd9\u79cd\u4f18\u5148\u7ea7\u5c42\u6b21\u7ed3\u6784\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u770b\u770b\u4e0b\u9762\u7684\u4ee3\u7801: 1 2 3 4 5 6 7 8 9 #include #include // for typeid() int main () { std :: cout << typeid ( 5u-10 ). name () << ' ' << 5u - 10 ; // 5u \u5373 5 \u88ab\u5f53\u505a\u65e0\u7b26\u53f7\u6574\u578b\u5904\u7406 return 0 ; } \u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u8868\u8fbe\u5f0f 5u - 10 \u6c42\u503c\u4f1a\u7b49\u4e8e -5 \uff0c\u56e0\u4e3a 5 - 10 = -5 \u3002\u4f46\u5b9e\u9645\u7ed3\u679c\u5374\u4e0d\u662f\u8fd9\u6837\u7684\uff1a 1 unsigned int 4294967291 \u56e0\u4e3a unsigned int \u64cd\u4f5c\u6570\u5177\u6709\u66f4\u9ad8\u7684\u4f18\u5148\u7ea7\uff0c\u6240\u4ee5 int \u64cd\u4f5c\u6570\u88ab\u8f6c\u6362\u6210\u4e86 unsigned int \u3002\u800c\u4e14 -5 \u8d85\u8fc7\u4e86 unsigned int \u80fd\u591f\u8868\u793a\u7684\u8303\u56f4\uff0c\u6240\u4ee5\u7ed3\u679c\u624d\u4f1a\u51fa\u4e4e\u6211\u4eec\u7684\u610f\u6599\u3002 \u4e0b\u9762\u662f\u53e6\u4e00\u4e2a\u53cd\u76f4\u89c9\u7ed3\u679c\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << std :: boolalpha << ( -3 < 5u ); return 0 ; } \u5c3d\u7ba1\uff0c\u5f88\u663e\u7136 5 \u662f\u6bd4 -3 \u5927\u7684\u3002\u5f53\u8868\u8fbe\u5f0f\u6c42\u503c\u65f6\uff0c -3 \u4f1a\u88ab\u8f6c\u6362\u4e3a\u4e00\u4e2a unsigned int \uff08\u53cd\u8f6c\u6210\u4e00\u4e2a\u5f88\u5927\u7684\u6570\uff09\uff0c\u8fd9\u4e2a\u6570\u5927\u4e8e 5 \u3002\u56e0\u6b64\uff0c\u4e0a\u9762\u7684\u8868\u8fbe\u5f0f\u4f1a\u8f93\u51fa false \u800c\u4e0d\u662f true \u3002 \u8fd9\u4e5f\u662f\u907f\u514d\u65e0\u7b26\u53f7\u6574\u6570\u7684\u4e3b\u8981\u539f\u56e0\u4e4b\u4e00\u2014\u2014\u5f53\u5728\u7b97\u672f\u8868\u8fbe\u5f0f\u4e2d\u5c06\u5b83\u4eec\u4e0e\u6709\u7b26\u53f7\u6574\u6570\u6df7\u5408\u4f7f\u7528\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u610f\u5916\u7ed3\u679c\u3002\u7f16\u8bd1\u5668\u751a\u81f3\u53ef\u80fd\u4e0d\u4f1a\u53d1\u51fa\u8b66\u544a\u3002","title":"8.4 - \u7b97\u672f\u8f6c\u6362"},{"location":"8-4-Arithmetic-conversions/#_1","text":"\u4e0b\u5217\u8fd0\u7b97\u7b26\u9700\u8981\u64cd\u4f5c\u6570\u5177\u6709\u76f8\u540c\u7c7b\u578b\uff1a \u4e8c\u5143\u7b97\u672f\u8fd0\u7b97\u7b26\uff1a + , - , * , / , % \u4e8c\u5143\u5173\u7cfb\u8fd0\u7b97\u7b26\uff1a < , > , <= , >= , == , != \u4e8c\u5143\u6309\u4f4d\u8fd0\u7b97\u7b26\uff1a & , ^ , | \u6761\u4ef6\u8fd0\u7b97\u7b26 ?: (\u5305\u62ec\u6761\u4ef6\uff0c\u8981\u6c42\u4e3a\u5e03\u5c14\u7c7b\u578b)","title":"\u9700\u8981\u64cd\u4f5c\u6570\u5177\u6709\u76f8\u540c\u7c7b\u578b\u7684\u8fd0\u7b97\u7b26"},{"location":"8-4-Arithmetic-conversions/#_2","text":"\u4e00\u822c\u7b97\u672f\u8f6c\u6362\u89c4\u5219\u975e\u5e38\u7b80\u5355\u3002\u7f16\u8bd1\u5668\u5177\u6709\u4e00\u4e2a\u6309\u7167\u4f18\u5148\u7ea7\u6392\u5e8f\u7684\u7c7b\u578b\u5217\u8868\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a long double (\u6700\u9ad8\u4f18\u5148\u7ea7) double float unsigned long long long long unsigned long long unsigned int int (\u6700\u4f4e\u4f18\u5148\u7ea7) \u4ee5\u53ca\u4e24\u6761\u89c4\u5219\uff1a \u5982\u679c\u81f3\u5c11\u6709\u4e00\u4e2a\u64cd\u4f5c\u6570\u5728\u4f18\u5148\u7ea7\u5217\u8868\u4e2d\uff0c\u5219\u5177\u6709\u8f83\u4f4e\u4f18\u5148\u7ea7\u7684\u64cd\u4f5c\u6570\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5177\u6709\u8f83\u9ad8\u4f18\u5148\u7ea7\u7684\u64cd\u4f5c\u6570\uff1b \u5426\u5219 (\u4e24\u4e2a\u64cd\u4f5c\u6570\u7684\u7c7b\u578b\u5747\u4e0d\u5728\u8868\u4e2d)\uff0c\u5219\u4e24\u4e2a\u64cd\u4f5c\u6570\u4f1a\u8fdb\u884c \u6570\u503c\u63d0\u5347 (\u53c2\u89c1\uff1a 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 )\u3002","title":"\u4e00\u822c\u7b97\u672f\u8f6c\u6362\u89c4\u5219"},{"location":"8-4-Arithmetic-conversions/#_3","text":"\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528 typeid \u8fd0\u7b97\u7b26 (\u5728 \u5934\u6587\u4ef6\u4e2d)\uff0c\u6765\u663e\u793a\u8868\u8fbe\u5f0f\u7ed3\u679c\u6240\u5c5e\u7684\u7c7b\u578b\u3002 \u9996\u5148\uff0c\u5c06 int \u548c double \u76f8\u52a0\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include #include // for typeid() int main () { int i { 2 }; double d { 3.5 }; std :: cout << typeid ( i + d ). name () << ' ' << i + d << '\\n' ; // \u663e\u793a i + d \u7684\u7c7b\u578b return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c double \u7c7b\u578b\u64cd\u4f5c\u6570\u7684\u4f18\u5148\u7ea7\u6700\u9ad8\uff0c\u6240\u4ee5\u4f18\u5148\u7ea7\u8f83\u4f4e\u7684\u64cd\u4f5c\u6570 (\u4f8b\u5982 int ) \u7684\u7c7b\u578b\u4f1a\u88ab\u8f6c\u6362\u4e3a double \u503c 2.0 . Then double values 2.0 and 3.5 are added to produce double result 5.5 . \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u6253\u5370\u5982\u4e0b\u4fe1\u606f\uff1a 1 double 5.5 \u6ce8\u610f\uff0c\u4f60\u7684\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u4ea7\u751f\u7a0d\u5fae\u4e0d\u540c\u7684\u7ed3\u679c\uff0c\u56e0\u4e3a typeid.name() \u8f93\u51fa\u7684\u7ed3\u679c\u662f\u7531\u7f16\u8bd1\u5668\u51b3\u5b9a\u7684\u3002 \u8bd1\u8005\u6ce8 \u5728g++\u4e0a\u4f7f\u7528 typeid \u8fd0\u7b97\u7b26 \uff0c\u8f93\u51fa\u7684\u7ed3\u679c\u4e3a \u4fee\u9970\u540d \uff0c\u53ef\u4ee5\u4f7f\u7528 1 a.out | c++filt --types \u5f97\u5230\u53ef\u8bfb\u7684\u7ed3\u679c\u3002 \u53c2\u8003 \u73b0\u5728\uff0c\u5c06\u4e24\u4e2a short \u7c7b\u578b\u7684\u503c\u76f8\u52a0\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include #include // for typeid() int main () { short a { 4 }; short b { 5 }; std :: cout << typeid ( a + b ). name () << ' ' << a + b << '\\n' ; // \u663e\u793a a + b \u7684\u7c7b\u578b return 0 ; } \u56e0\u4e3a\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u6ca1\u6709\u51fa\u73b0\u5728\u4f18\u5148\u7ea7\u5217\u8868\u4e2d\uff0c\u6240\u4ee5\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u88ab\u6574\u578b\u63d0\u5347\u4e3a int \u7c7b\u578b\u3002\u4e24\u4e2a int \u76f8\u52a0\u7684\u7ed3\u679c\u662f\u4e00\u4e2a int \uff0c\u6b63\u5982\u4f60\u6240\u671f\u671b\u7684\u90a3\u6837: 1 int 9","title":"\u4e00\u4e9b\u4f8b\u5b50"},{"location":"8-4-Arithmetic-conversions/#_4","text":"\u5f53\u6df7\u5408\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u503c\u65f6\uff0c\u8fd9\u79cd\u4f18\u5148\u7ea7\u5c42\u6b21\u7ed3\u6784\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u770b\u770b\u4e0b\u9762\u7684\u4ee3\u7801: 1 2 3 4 5 6 7 8 9 #include #include // for typeid() int main () { std :: cout << typeid ( 5u-10 ). name () << ' ' << 5u - 10 ; // 5u \u5373 5 \u88ab\u5f53\u505a\u65e0\u7b26\u53f7\u6574\u578b\u5904\u7406 return 0 ; } \u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u8868\u8fbe\u5f0f 5u - 10 \u6c42\u503c\u4f1a\u7b49\u4e8e -5 \uff0c\u56e0\u4e3a 5 - 10 = -5 \u3002\u4f46\u5b9e\u9645\u7ed3\u679c\u5374\u4e0d\u662f\u8fd9\u6837\u7684\uff1a 1 unsigned int 4294967291 \u56e0\u4e3a unsigned int \u64cd\u4f5c\u6570\u5177\u6709\u66f4\u9ad8\u7684\u4f18\u5148\u7ea7\uff0c\u6240\u4ee5 int \u64cd\u4f5c\u6570\u88ab\u8f6c\u6362\u6210\u4e86 unsigned int \u3002\u800c\u4e14 -5 \u8d85\u8fc7\u4e86 unsigned int \u80fd\u591f\u8868\u793a\u7684\u8303\u56f4\uff0c\u6240\u4ee5\u7ed3\u679c\u624d\u4f1a\u51fa\u4e4e\u6211\u4eec\u7684\u610f\u6599\u3002 \u4e0b\u9762\u662f\u53e6\u4e00\u4e2a\u53cd\u76f4\u89c9\u7ed3\u679c\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << std :: boolalpha << ( -3 < 5u ); return 0 ; } \u5c3d\u7ba1\uff0c\u5f88\u663e\u7136 5 \u662f\u6bd4 -3 \u5927\u7684\u3002\u5f53\u8868\u8fbe\u5f0f\u6c42\u503c\u65f6\uff0c -3 \u4f1a\u88ab\u8f6c\u6362\u4e3a\u4e00\u4e2a unsigned int \uff08\u53cd\u8f6c\u6210\u4e00\u4e2a\u5f88\u5927\u7684\u6570\uff09\uff0c\u8fd9\u4e2a\u6570\u5927\u4e8e 5 \u3002\u56e0\u6b64\uff0c\u4e0a\u9762\u7684\u8868\u8fbe\u5f0f\u4f1a\u8f93\u51fa false \u800c\u4e0d\u662f true \u3002 \u8fd9\u4e5f\u662f\u907f\u514d\u65e0\u7b26\u53f7\u6574\u6570\u7684\u4e3b\u8981\u539f\u56e0\u4e4b\u4e00\u2014\u2014\u5f53\u5728\u7b97\u672f\u8868\u8fbe\u5f0f\u4e2d\u5c06\u5b83\u4eec\u4e0e\u6709\u7b26\u53f7\u6574\u6570\u6df7\u5408\u4f7f\u7528\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u610f\u5916\u7ed3\u679c\u3002\u7f16\u8bd1\u5668\u751a\u81f3\u53ef\u80fd\u4e0d\u4f1a\u53d1\u51fa\u8b66\u544a\u3002","title":"\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u95ee\u9898"},{"location":"8-5-Explicit-type-conversion-casting-and-static-cast/","tags":["explicit-type-conversion","conversions","static-cast"],"text":"Key Takeaway C++\u652f\u6301 5 \u79cd\u7c7b\u578b\u7684\u663e\u793a\u7c7b\u578b\u8f6c\u6362: C\u98ce\u683c\u7c7b\u578b\u8f6c\u6362 \u3001 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u3001 const \u7c7b\u578b\u8f6c\u6362 \u3001 \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u548c \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 \u3002\u540e\u56db\u79cd\u7c7b\u578b\u6709\u65f6\u79f0\u4e3a \u5177\u540d\u540d\u7c7b\u578b\u8f6c\u6362(named cast) \u3002 C \u8bed\u8a00\u7c7b\u578b\u7684\u7c7b\u578b\u8f6c\u6362\u5728\u4e0d\u540c\u7684\u8bed\u5883\u4e0b\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u6548\u679c\uff0c\u5c3d\u91cf\u4e0d\u8981\u4f7f\u7528 \u4f18\u5148\u4f7f\u7528 static_cast \uff0c\u5b83\u63d0\u4f9b\u4e86 \u8fd0\u884c\u65f6 \u7684\u7c7b\u578b\u68c0\u67e5\u673a\u5236\uff0c\u4e0d\u5bb9\u6613\u72af\u9519\u3002 \u4f7f\u7528 static_cast \u8fdb\u884c\u663e\u5f0f\u5730\u7f29\u7a84\u8f6c\u6362 \u5728 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u9690\u5f0f\u5730\u5c06\u4e00\u79cd\u7c7b\u578b\u7684\u503c\u8f6c\u6362\u6210\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\uff0c\u5373 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u3002\u5f53\u4f60\u60f3\u8981\u5c06\u4e00\u4e2a\u6570\u503c\u7c7b\u578b\u901a\u8fc7 \u6570\u503c\u63d0\u5347 \u7684\u65b9\u5f0f\u8f6c\u6362\u4e3a\u66f4\u5bbd\u7684\u7c7b\u578b\u65f6\uff0c\u4f7f\u7528\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u662f\u53ef\u4ee5\u7684\u3002 \u5f88\u591a\u65b0\u624b C++ \u7a0b\u5e8f\u5458\u4f1a\u8fd9\u6837\u505a\uff1a 1 double d = 10 / 4 ; // \u901a\u8fc7\u6574\u578b\u4e58\u6cd5\uff0c\u5c06\u503c\u521d\u59cb\u5316\u4e3a 2.0 \u56e0\u4e3a 10 \u548c 4 \u90fd\u662f int \u7c7b\u578b\uff0c\u6240\u4ee5\u6267\u884c\u7684\u662f\u6574\u578b\u9664\u6cd5\uff0c\u8868\u8fbe\u5f0f\u7684\u6c42\u503c\u7ed3\u679c\u4e3a int \u503c 2 \u3002\u7136\u540e\uff0c\u8be5\u503c\u5728\u88ab\u7528\u4e8e\u521d\u59cb\u5316\u53d8\u91cf d \u4e4b\u524d\u88ab\u8f6c\u6362\u6210\u4e86 double \u7c7b\u578b\u7684 2.0 \u3002 \u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u5458\u5e76\u4e0d\u4f1a\u6545\u610f\u8fd9\u4e48\u505a\u3002 \u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u5b57\u9762\u91cf\uff0c\u5c06\u4e0a\u9762\u7684 int \u578b\u64cd\u4f5c\u6570\u66ff\u6362\u6210\u4e86 double \u64cd\u4f5c\u6570\uff0c\u8fd9\u6837\u4e00\u6765\u5c31\u4f1a\u8fdb\u884c\u6d6e\u70b9\u6570\u9664\u6cd5\uff1a 1 double d = 10.0 / 4.0 ; // does floating point division, initializes d with value 2.5 \u4f46\u662f\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u53d8\u91cf\u800c\u975e\u5b57\u9762\u91cf\u5462\uff1f\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 int x { 10 }; int y { 4 }; double d = x / y ; // does integer division, initializes d with value 2.0 \u56e0\u4e3a\u6267\u884c\u4e86\u6574\u578b\u9664\u6cd5\uff0c\u6240\u4ee5\u53d8\u91cf d \u7684\u503c\u6700\u7ec8\u4e3a 2.0 \u3002\u90a3\u4e48\u6211\u4eec\u5e94\u8be5\u5982\u4f55\u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u8fd9\u91cc\u9700\u8981\u4f7f\u7528\u6d6e\u70b9\u6570\u9664\u6cd5\u5462\uff1f\u5b57\u9762\u91cf\u540e\u7f00\u5e76\u4e0d\u80fd\u88ab\u7528\u5728\u53d8\u91cf\u4e0a\u3002\u56e0\u6b64\uff0c\u9700\u8981\u4e00\u79cd\u80fd\u591f\u5c06\u53d8\u91cf\u8f6c\u6362\u4e3a\u6d6e\u70b9\u7c7b\u578b\u7684\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u4f7f\u7528\u6d6e\u70b9\u6570\u9664\u6cd5\u3002 \u5e78\u8fd0\u7684\u662f\uff0cC++\u63d0\u4f9b\u4e86\u8bb8\u591a\u4e0d\u540c\u7684\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26(\u901a\u5e38\u79f0\u4e3a\u7c7b\u578b\u8f6c\u6362)\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u8bf7\u6c42\u7f16\u8bd1\u5668\u6267\u884c\u7c7b\u578b\u8f6c\u6362\u3002\u56e0\u4e3a\u7c7b\u578b\u8f6c\u6362\u662f\u7a0b\u5e8f\u5458\u7684\u663e\u5f0f\u8bf7\u6c42\uff0c\u6240\u4ee5\u8fd9\u79cd\u5f62\u5f0f\u7684\u7c7b\u578b\u8f6c\u6362\u901a\u5e38\u79f0\u4e3a \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 (\u4e0e\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u76f8\u53cd\uff0c\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u662f\u7f16\u8bd1\u5668\u81ea\u52a8\u6267\u884c\u7684\u7c7b\u578b\u8f6c\u6362)\u3002 \u7c7b\u578b\u8f6c\u6362 C++\u652f\u6301 5 \u79cd\u7c7b\u578b\u7684\u663e\u793a\u7c7b\u578b\u8f6c\u6362: C\u98ce\u683c\u7c7b\u578b\u8f6c\u6362 \u3001 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u3001 const \u7c7b\u578b\u8f6c\u6362 \u3001 \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u548c \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 \u3002\u540e\u56db\u79cd\u7c7b\u578b\u6709\u65f6\u79f0\u4e3a \u5177\u540d\u540d\u7c7b\u578b\u8f6c\u6362(named cast) \u3002 \u5728\u672c\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd C\u98ce\u683c\u7c7b\u578b\u8f6c\u6362 \u548c \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 18.10 -- Dynamic casting \u4e2d\u4ecb\u7ecd\u52a8\u6001\u7c7b\u578b\u8f6c\u6362\uff0c\u4f46\u662f\u6211\u4eec\u9996\u5148\u8981\u4ecb\u7ecd\u4e00\u4e9b\u524d\u7f6e\u5185\u5bb9\u3002 \u901a\u5e38\u5e94\u8be5\u907f\u514d\u4f7f\u7528 const \u7c7b\u578b\u8f6c\u6362 \u548c \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 \uff0c\u56e0\u4e3a\u53ea\u6709\u5f88\u5c11\u7684\u60c5\u51b5\u4e0b\u4f1a\u9700\u8981\u4f7f\u7528\u5b83\u4eec\uff0c\u800c\u4e14\u4f7f\u7528\u4e0d\u5f53\u662f\u975e\u5e38\u6709\u5bb3\u7684\u3002 \u6ce8\u610f \u9664\u975e\u4f60\u6709\u5145\u5206\u7684\u7406\u7531\uff0c\u5426\u5219\u8bf7\u907f\u514d\u4f7f\u7528 const \u7c7b\u578b\u8f6c\u6362 \u548c \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 C\u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362 \u5728\u6807\u51c6\u7684C\u8bed\u8a00\u4e2d\uff0c\u5f3a\u5236\u8f6c\u6362\u662f\u901a\u8fc7 () \u8fd0\u7b97\u7b26\u5b8c\u6210\u7684\uff0c\u62ec\u53f7\u5185\u4e3a\u9700\u8981\u8f6c\u6362\u7684\u7c7b\u578b\u540d\u3002\u5728C++\u4e2d\uff0c\u4f60\u4ecd\u7136\u53ef\u4ee5\u5728\u7531C\u8bed\u8a00\u8f6c\u6362\u800c\u6765\u7684\u4ee3\u7801\u4e2d\u770b\u5230\u5b83\u4eec\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { int x { 10 }; int y { 4 }; double d { ( double ) x / y }; // convert x to a double so we get floating point division std :: cout << d ; // prints 2.5 return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u8981\u6c42\u7f16\u8bd1\u5668\u5c06 x \u8f6c\u6362\u4e3a double \u3002\u56e0\u4e3a\uff0c / \u5de6\u4fa7\u7684\u64cd\u4f5c\u6570\u88ab\u8f6c\u6362\u6210\u4e86\u6d6e\u70b9\u6570\uff0c\u6240\u4ee5\u64cd\u4f5c\u7b26\u53f3\u4fa7\u7684\u6570\u540c\u6837\u4e5f\u4f1a\u88ab\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff08 \u6570\u503c\u8f6c\u6362 \uff09\uff0c\u7136\u540e\u8868\u8fbe\u5f0f\u5c31\u4f1a\u6309\u7167\u6d6e\u70b9\u6570\u9664\u6cd5\u800c\u4e0d\u662f\u6574\u578b\u9664\u6cd5\u6c42\u503c\uff01 C++ \u5141\u8bb8\u4f60\u4f7f\u7528\u4e00\u79cd\u66f4\u52a0\u7c7b\u4f3c\u51fd\u6570\u8c03\u7528\u7684\u8bed\u6cd5\u6765\u4f7f\u7528C\u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362\uff1a 1 double d { double ( x ) / y }; // convert x to a double so we get floating point division \u8fd9\u79cd\u65b9\u5f0f\u5b9e\u73b0\u7684\u7c7b\u578b\u8f6c\u6362\u548c\u4e4b\u524d\u4e00\u79cd\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u4f46\u662f\u5c06\u9700\u8981\u8f6c\u6362\u7684\u53d8\u91cf\u653e\u5728\u62ec\u53f7\u4e86\uff0c\u66f4\u5bb9\u6613\u770b\u6e05\u695a\u88ab\u8f6c\u6362\u7684\u5bf9\u8c61\u662f\u4ec0\u4e48\u3002 \u867d\u7136\u201cc\u98ce\u683c\u7c7b\u578b\u8f6c\u6362\u201d\u770b\u8d77\u6765\u662f\u5355\u4e00\u7c7b\u578b\u8f6c\u6362\uff0c\u4f46\u5b9e\u9645\u4e0a\u5b83\u53ef\u4ee5\u6839\u636e\u4e0a\u4e0b\u6587\u6267\u884c\u5404\u79cd\u4e0d\u540c\u7684\u8f6c\u6362\u3002\u8fd9\u53ef\u4ee5\u5305\u62ec\u201c\u9759\u6001\u7c7b\u578b\u8f6c\u6362\u201d\u3001\u201cconst\u7c7b\u578b\u8f6c\u6362\u201d\u6216\u201c\u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362\u201d(\u6211\u4eec\u5728\u4e0a\u9762\u63d0\u5230\u7684\u540e\u4e24\u79cd\u7c7b\u578b\u5e94\u8be5\u907f\u514d)\u3002\u56e0\u6b64\uff0c\u201cC\u98ce\u683c\u5f3a\u5236\u8f6c\u6362\u201d\u6709\u53ef\u80fd\u88ab\u65e0\u610f\u4e2d\u8bef\u7528\uff0c\u800c\u4e0d\u4f1a\u4ea7\u751f\u9884\u671f\u7684\u884c\u4e3a\uff0c\u800c\u4f7f\u7528c++\u5f3a\u5236\u8f6c\u6362\u5219\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002 \u76f8\u5173\u5185\u5bb9 \u5982\u679c\u4f60\u5f88\u597d\u5947\uff0cC\u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u53ef\u4ee5\u53c2\u8003 \u8fd9\u7bc7\u6587\u7ae0 \u3002 \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u4f7f\u7528C\u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362\u3002 static_cast C++ \u5f15\u5165\u4e86\u4e00\u4e2a\u65b0\u7684\u5f3a\u5236\u8f6c\u6362\u8fd0\u7b97\u7b26 static_cast \uff0c\u7528\u4e8e\u5c06\u4e00\u79cd\u7c7b\u578b\u7684\u503c\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\u3002 \u5728\u4e4b\u524d\u7684\u8bfe\u4e2d\uff0c\u4f60\u53ef\u80fd\u5df2\u7ecf\u89c1\u8bc6\u8fc7\u5982\u4f55\u4f7f\u7528 static_cast \u5c06 char \u8f6c\u6362\u4e3a int \u4f7f\u5f97 std::cout \u53ef\u4ee5\u6253\u5370\u6574\u578b\u800c\u4e0d\u662f char \uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char c { 'a' }; std :: cout << c << ' ' << static_cast < int > ( c ) << '\\n' ; // prints a 97 return 0 ; } static_cast \u8fd0\u7b97\u7b26\u5c06\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u8f93\u5165\uff0c\u7136\u540e\u5c06\u8868\u8fbe\u5f0f\u6c42\u503c\u7684\u7ed3\u679c\u8f6c\u6362\u4e3a \u5c16\u62ec\u53f7 \u4e2d\u6307\u5b9a\u7684\u7c7b\u578b\u3002 static_cast \u662f\u5c06\u4e00\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\u7684\u6700\u4f73\u9014\u5f84\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { int x { 10 }; int y { 4 }; // static cast x to a double so we get floating point division double d { static_cast < double > ( x ) / y }; std :: cout << d ; // prints 2.5 return 0 ; } static_cast \u6700\u5927\u7684\u4f18\u52bf\u662f\u5b83\u63d0\u4f9b\u4e86 \u8fd0\u884c\u65f6 \u7684\u7c7b\u578b\u68c0\u67e5\u673a\u5236\uff0c\u8fd9\u6837\u5c31\u4e0d\u5bb9\u6613\u72af\u4e0b\u7531\u7c97\u5fc3\u5bfc\u81f4\u7684\u95ee\u9898\u3002 static_cast \u7684\u529f\u80fd\u4e0d\u5982 C \u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362\uff08\u6545\u610f\u7684\uff09\u5f3a\u5927\uff0c\u6240\u4ee5\u4f60\u4e0d\u4f1a\u65e0\u610f\u95f4\u79fb\u9664 const \u6216\u5176\u4ed6\u4f60\u672c\u4e0d\u5e0c\u671b\u53d1\u751f\u7684\u4e8b\u60c5\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u4f18\u5148\u4f7f\u7528 static_cast \u3002 \u4f7f\u7528 static_cast \u8fdb\u884c\u663e\u5f0f\u5730\u7f29\u7a84\u8f6c\u6362 \u5f53\u6211\u4eec\u8fdb\u884c\u5177\u6709\u6f5c\u5728\u5371\u9669\u7684\uff08\u7f29\u7a84\uff09\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u53d1\u51fa\u544a\u8b66\u3002\u4f8b\u5982\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a 1 2 int i { 48 }; char ch = i ; // \u9690\u5f0f\u5730\u7f29\u7a84\u8f6c\u6362 \u5c06 int (2 \u5b57\u8282\u6216\u8005 4 \u5b57\u8282) \u8f6c\u6362\u4e3a char (1 \u5b57\u8282)\u901a\u5e38\u662f\u4e0d\u5b89\u5168\u7684\uff08\u56e0\u4e3a\u7f16\u8bd1\u5668\u65e0\u6cd5\u5224\u65ad\u6574\u578b\u503c\u662f\u5426\u4f1a\u8d85\u51fa char \u80fd\u591f\u8868\u793a\u7684\u8303\u56f4\uff09\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u4ea7\u751f\u8b66\u544a\u3002\u800c\u5982\u679c\u6211\u4eec\u4f7f\u7528\u7684\u662f \u5217\u8868\u521d\u59cb\u5316 \uff08 \u62ec\u53f7\u521d\u59cb\u5316 \uff09\uff0c\u7f16\u8bd1\u5668\u5219\u901a\u5e38\u4f1a\u4ea7\u751f\u4e00\u4e2a\u7f16\u8bd1\u9519\u8bef\u3002(\u53c2\u89c1\uff1a \u62ec\u53f7\u521d\u59cb\u5316\u4e0d\u5141\u8bb8\u9690\u5f0f\u7f29\u7a84\u8f6c\u6362 ) \u4e3a\u4e86\u907f\u514d\u8fd9\u4e9b\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 static_cast \u663e\u5f0f\u5730\u5c06\u6574\u578b\u8f6c\u6362\u4e3a char \uff1a 1 2 3 4 int i { 48 }; // explicit conversion from int to char, so that a char is assigned to variable ch char ch { static_cast < char > ( i ) }; \u8fd9\u6837\u505a\u65f6\uff0c\u6211\u4eec\u663e\u5f0f\u5730\u544a\u8bc9\u7f16\u8bd1\u5668\u8fd9\u4e2a\u8f6c\u6362\u662f\u6709\u610f\u7684\uff0c\u540e\u679c\u81ea\u8d1f(\u4f8b\u5982\uff0c\u6ea2\u51fa char \u7684\u8303\u56f4)\u3002\u7531\u4e8e\u8fd9\u4e2a static_cast \u7684\u8f93\u51fa\u7c7b\u578b\u4e3a char \uff0c\u53d8\u91cf ch \u7684\u521d\u59cb\u5316\u7684\u7c7b\u578b\u662f\u5339\u914d\u7684\uff0c\u56e0\u6b64\u4e0d\u4f1a\u4ea7\u751f\u8b66\u544a\u6216\u9519\u8bef\u3002 \u4e0b\u9762\u662f\u53e6\u4e00\u4e2a\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u62b1\u6028\u5c06 double \u8f6c\u6362\u4e3a int \u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u4e22\u5931\u7684\u4f8b\u5b50: 1 2 int i { 100 }; i = i / 2.5 ; \u544a\u8bc9\u7f16\u8bd1\u5668\u4e0a\u8ff0\u8f6c\u6362\u662f\u6709\u610f\u800c\u4e3a\u4e4b\u7684\uff1a 1 2 int i { 100 }; i = static_cast < int > ( i / 2.5 );","title":"8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast"},{"location":"8-5-Explicit-type-conversion-casting-and-static-cast/#_1","text":"C++\u652f\u6301 5 \u79cd\u7c7b\u578b\u7684\u663e\u793a\u7c7b\u578b\u8f6c\u6362: C\u98ce\u683c\u7c7b\u578b\u8f6c\u6362 \u3001 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u3001 const \u7c7b\u578b\u8f6c\u6362 \u3001 \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u548c \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 \u3002\u540e\u56db\u79cd\u7c7b\u578b\u6709\u65f6\u79f0\u4e3a \u5177\u540d\u540d\u7c7b\u578b\u8f6c\u6362(named cast) \u3002 \u5728\u672c\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd C\u98ce\u683c\u7c7b\u578b\u8f6c\u6362 \u548c \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 18.10 -- Dynamic casting \u4e2d\u4ecb\u7ecd\u52a8\u6001\u7c7b\u578b\u8f6c\u6362\uff0c\u4f46\u662f\u6211\u4eec\u9996\u5148\u8981\u4ecb\u7ecd\u4e00\u4e9b\u524d\u7f6e\u5185\u5bb9\u3002 \u901a\u5e38\u5e94\u8be5\u907f\u514d\u4f7f\u7528 const \u7c7b\u578b\u8f6c\u6362 \u548c \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 \uff0c\u56e0\u4e3a\u53ea\u6709\u5f88\u5c11\u7684\u60c5\u51b5\u4e0b\u4f1a\u9700\u8981\u4f7f\u7528\u5b83\u4eec\uff0c\u800c\u4e14\u4f7f\u7528\u4e0d\u5f53\u662f\u975e\u5e38\u6709\u5bb3\u7684\u3002 \u6ce8\u610f \u9664\u975e\u4f60\u6709\u5145\u5206\u7684\u7406\u7531\uff0c\u5426\u5219\u8bf7\u907f\u514d\u4f7f\u7528 const \u7c7b\u578b\u8f6c\u6362 \u548c \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362","title":"\u7c7b\u578b\u8f6c\u6362"},{"location":"8-5-Explicit-type-conversion-casting-and-static-cast/#c","text":"\u5728\u6807\u51c6\u7684C\u8bed\u8a00\u4e2d\uff0c\u5f3a\u5236\u8f6c\u6362\u662f\u901a\u8fc7 () \u8fd0\u7b97\u7b26\u5b8c\u6210\u7684\uff0c\u62ec\u53f7\u5185\u4e3a\u9700\u8981\u8f6c\u6362\u7684\u7c7b\u578b\u540d\u3002\u5728C++\u4e2d\uff0c\u4f60\u4ecd\u7136\u53ef\u4ee5\u5728\u7531C\u8bed\u8a00\u8f6c\u6362\u800c\u6765\u7684\u4ee3\u7801\u4e2d\u770b\u5230\u5b83\u4eec\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { int x { 10 }; int y { 4 }; double d { ( double ) x / y }; // convert x to a double so we get floating point division std :: cout << d ; // prints 2.5 return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2aC\u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u8981\u6c42\u7f16\u8bd1\u5668\u5c06 x \u8f6c\u6362\u4e3a double \u3002\u56e0\u4e3a\uff0c / \u5de6\u4fa7\u7684\u64cd\u4f5c\u6570\u88ab\u8f6c\u6362\u6210\u4e86\u6d6e\u70b9\u6570\uff0c\u6240\u4ee5\u64cd\u4f5c\u7b26\u53f3\u4fa7\u7684\u6570\u540c\u6837\u4e5f\u4f1a\u88ab\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff08 \u6570\u503c\u8f6c\u6362 \uff09\uff0c\u7136\u540e\u8868\u8fbe\u5f0f\u5c31\u4f1a\u6309\u7167\u6d6e\u70b9\u6570\u9664\u6cd5\u800c\u4e0d\u662f\u6574\u578b\u9664\u6cd5\u6c42\u503c\uff01 C++ \u5141\u8bb8\u4f60\u4f7f\u7528\u4e00\u79cd\u66f4\u52a0\u7c7b\u4f3c\u51fd\u6570\u8c03\u7528\u7684\u8bed\u6cd5\u6765\u4f7f\u7528C\u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362\uff1a 1 double d { double ( x ) / y }; // convert x to a double so we get floating point division \u8fd9\u79cd\u65b9\u5f0f\u5b9e\u73b0\u7684\u7c7b\u578b\u8f6c\u6362\u548c\u4e4b\u524d\u4e00\u79cd\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u4f46\u662f\u5c06\u9700\u8981\u8f6c\u6362\u7684\u53d8\u91cf\u653e\u5728\u62ec\u53f7\u4e86\uff0c\u66f4\u5bb9\u6613\u770b\u6e05\u695a\u88ab\u8f6c\u6362\u7684\u5bf9\u8c61\u662f\u4ec0\u4e48\u3002 \u867d\u7136\u201cc\u98ce\u683c\u7c7b\u578b\u8f6c\u6362\u201d\u770b\u8d77\u6765\u662f\u5355\u4e00\u7c7b\u578b\u8f6c\u6362\uff0c\u4f46\u5b9e\u9645\u4e0a\u5b83\u53ef\u4ee5\u6839\u636e\u4e0a\u4e0b\u6587\u6267\u884c\u5404\u79cd\u4e0d\u540c\u7684\u8f6c\u6362\u3002\u8fd9\u53ef\u4ee5\u5305\u62ec\u201c\u9759\u6001\u7c7b\u578b\u8f6c\u6362\u201d\u3001\u201cconst\u7c7b\u578b\u8f6c\u6362\u201d\u6216\u201c\u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362\u201d(\u6211\u4eec\u5728\u4e0a\u9762\u63d0\u5230\u7684\u540e\u4e24\u79cd\u7c7b\u578b\u5e94\u8be5\u907f\u514d)\u3002\u56e0\u6b64\uff0c\u201cC\u98ce\u683c\u5f3a\u5236\u8f6c\u6362\u201d\u6709\u53ef\u80fd\u88ab\u65e0\u610f\u4e2d\u8bef\u7528\uff0c\u800c\u4e0d\u4f1a\u4ea7\u751f\u9884\u671f\u7684\u884c\u4e3a\uff0c\u800c\u4f7f\u7528c++\u5f3a\u5236\u8f6c\u6362\u5219\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002 \u76f8\u5173\u5185\u5bb9 \u5982\u679c\u4f60\u5f88\u597d\u5947\uff0cC\u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u53ef\u4ee5\u53c2\u8003 \u8fd9\u7bc7\u6587\u7ae0 \u3002 \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u4f7f\u7528C\u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362\u3002","title":"C\u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362"},{"location":"8-5-Explicit-type-conversion-casting-and-static-cast/#static_cast","text":"C++ \u5f15\u5165\u4e86\u4e00\u4e2a\u65b0\u7684\u5f3a\u5236\u8f6c\u6362\u8fd0\u7b97\u7b26 static_cast \uff0c\u7528\u4e8e\u5c06\u4e00\u79cd\u7c7b\u578b\u7684\u503c\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\u3002 \u5728\u4e4b\u524d\u7684\u8bfe\u4e2d\uff0c\u4f60\u53ef\u80fd\u5df2\u7ecf\u89c1\u8bc6\u8fc7\u5982\u4f55\u4f7f\u7528 static_cast \u5c06 char \u8f6c\u6362\u4e3a int \u4f7f\u5f97 std::cout \u53ef\u4ee5\u6253\u5370\u6574\u578b\u800c\u4e0d\u662f char \uff1a 1 2 3 4 5 6 7 8 9 #include int main () { char c { 'a' }; std :: cout << c << ' ' << static_cast < int > ( c ) << '\\n' ; // prints a 97 return 0 ; } static_cast \u8fd0\u7b97\u7b26\u5c06\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u8f93\u5165\uff0c\u7136\u540e\u5c06\u8868\u8fbe\u5f0f\u6c42\u503c\u7684\u7ed3\u679c\u8f6c\u6362\u4e3a \u5c16\u62ec\u53f7 \u4e2d\u6307\u5b9a\u7684\u7c7b\u578b\u3002 static_cast \u662f\u5c06\u4e00\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\u7684\u6700\u4f73\u9014\u5f84\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { int x { 10 }; int y { 4 }; // static cast x to a double so we get floating point division double d { static_cast < double > ( x ) / y }; std :: cout << d ; // prints 2.5 return 0 ; } static_cast \u6700\u5927\u7684\u4f18\u52bf\u662f\u5b83\u63d0\u4f9b\u4e86 \u8fd0\u884c\u65f6 \u7684\u7c7b\u578b\u68c0\u67e5\u673a\u5236\uff0c\u8fd9\u6837\u5c31\u4e0d\u5bb9\u6613\u72af\u4e0b\u7531\u7c97\u5fc3\u5bfc\u81f4\u7684\u95ee\u9898\u3002 static_cast \u7684\u529f\u80fd\u4e0d\u5982 C \u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362\uff08\u6545\u610f\u7684\uff09\u5f3a\u5927\uff0c\u6240\u4ee5\u4f60\u4e0d\u4f1a\u65e0\u610f\u95f4\u79fb\u9664 const \u6216\u5176\u4ed6\u4f60\u672c\u4e0d\u5e0c\u671b\u53d1\u751f\u7684\u4e8b\u60c5\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u4f18\u5148\u4f7f\u7528 static_cast \u3002","title":"static_cast"},{"location":"8-5-Explicit-type-conversion-casting-and-static-cast/#static_cast_1","text":"\u5f53\u6211\u4eec\u8fdb\u884c\u5177\u6709\u6f5c\u5728\u5371\u9669\u7684\uff08\u7f29\u7a84\uff09\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u53d1\u51fa\u544a\u8b66\u3002\u4f8b\u5982\uff0c\u8003\u8651\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a 1 2 int i { 48 }; char ch = i ; // \u9690\u5f0f\u5730\u7f29\u7a84\u8f6c\u6362 \u5c06 int (2 \u5b57\u8282\u6216\u8005 4 \u5b57\u8282) \u8f6c\u6362\u4e3a char (1 \u5b57\u8282)\u901a\u5e38\u662f\u4e0d\u5b89\u5168\u7684\uff08\u56e0\u4e3a\u7f16\u8bd1\u5668\u65e0\u6cd5\u5224\u65ad\u6574\u578b\u503c\u662f\u5426\u4f1a\u8d85\u51fa char \u80fd\u591f\u8868\u793a\u7684\u8303\u56f4\uff09\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u4ea7\u751f\u8b66\u544a\u3002\u800c\u5982\u679c\u6211\u4eec\u4f7f\u7528\u7684\u662f \u5217\u8868\u521d\u59cb\u5316 \uff08 \u62ec\u53f7\u521d\u59cb\u5316 \uff09\uff0c\u7f16\u8bd1\u5668\u5219\u901a\u5e38\u4f1a\u4ea7\u751f\u4e00\u4e2a\u7f16\u8bd1\u9519\u8bef\u3002(\u53c2\u89c1\uff1a \u62ec\u53f7\u521d\u59cb\u5316\u4e0d\u5141\u8bb8\u9690\u5f0f\u7f29\u7a84\u8f6c\u6362 ) \u4e3a\u4e86\u907f\u514d\u8fd9\u4e9b\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 static_cast \u663e\u5f0f\u5730\u5c06\u6574\u578b\u8f6c\u6362\u4e3a char \uff1a 1 2 3 4 int i { 48 }; // explicit conversion from int to char, so that a char is assigned to variable ch char ch { static_cast < char > ( i ) }; \u8fd9\u6837\u505a\u65f6\uff0c\u6211\u4eec\u663e\u5f0f\u5730\u544a\u8bc9\u7f16\u8bd1\u5668\u8fd9\u4e2a\u8f6c\u6362\u662f\u6709\u610f\u7684\uff0c\u540e\u679c\u81ea\u8d1f(\u4f8b\u5982\uff0c\u6ea2\u51fa char \u7684\u8303\u56f4)\u3002\u7531\u4e8e\u8fd9\u4e2a static_cast \u7684\u8f93\u51fa\u7c7b\u578b\u4e3a char \uff0c\u53d8\u91cf ch \u7684\u521d\u59cb\u5316\u7684\u7c7b\u578b\u662f\u5339\u914d\u7684\uff0c\u56e0\u6b64\u4e0d\u4f1a\u4ea7\u751f\u8b66\u544a\u6216\u9519\u8bef\u3002 \u4e0b\u9762\u662f\u53e6\u4e00\u4e2a\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u62b1\u6028\u5c06 double \u8f6c\u6362\u4e3a int \u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u4e22\u5931\u7684\u4f8b\u5b50: 1 2 int i { 100 }; i = i / 2.5 ; \u544a\u8bc9\u7f16\u8bd1\u5668\u4e0a\u8ff0\u8f6c\u6362\u662f\u6709\u610f\u800c\u4e3a\u4e4b\u7684\uff1a 1 2 int i { 100 }; i = static_cast < int > ( i / 2.5 );","title":"\u4f7f\u7528 static_cast \u8fdb\u884c\u663e\u5f0f\u5730\u7f29\u7a84\u8f6c\u6362"},{"location":"8-6-Typedefs-and-type-aliases/","tags":["typedef","alias"],"text":"Key Takeaway using distance_t = double; // distance_t \u4f5c\u4e3a double \u7684\u522b\u540d \u7c7b\u578b\u522b\u540d\u5e76\u975e\u65b0\u7c7b\u578b \u8bf4\u7c7b\u578b\u522b\u540d\u5e76\u4e0d\u662f \u7c7b\u578b\u5b89\u5168 \u7684\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668 \u8bed\u4e49\u9519\u8bef \uff08\u5c3d\u7ba1\u8bed\u6cd5\u662f\u6b63\u786e\u7684\uff09 \u7c7b\u578b\u522b\u540d\u7684\u4f5c\u7528\u57df\u89c4\u5219\u548c\u53d8\u91cf\u6807\u8bc6\u7b26\u7684\u89c4\u5219\u662f\u4e00\u81f4\u7684 \u4f7f\u7528\u7c7b\u578b\u522b\u540d\u53ef\u4ee5\u5b9a\u4e49\u4e0e\u5e73\u53f0\u65e0\u5173\u7684\u7c7b\u578b \u7c7b\u578b\u522b\u540d\u53ef\u4ee5\u7b80\u5316\u590d\u6742\u7c7b\u578b\u540d\u7684\u8f93\u5165\u548c\u7406\u89e3 \u5f53\u7c7b\u578b\u522b\u540d\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\u3001\u4ee3\u7801\u7ef4\u62a4\u6027\u65f6\uff0c\u660e\u667a\u800c\u5ba1\u614e\u5730\u4f7f\u7528\u5b83\u4eec\u3002 \u7c7b\u578b\u522b\u540d \u5728 C++ \u4e2d\uff0c using \u5173\u952e\u5b57\u8fd8\u53ef\u4ee5\u88ab\u7528\u6765\u4e3a\u5df2\u6709\u7684\u7c7b\u578b\u521b\u5efa\u4e00\u4e2a \u7c7b\u578b\u522b\u540d(type aliases) \u3002\u521b\u5efa\u522b\u540d\u65f6\uff0c\u5728 using \u5173\u952e\u5b57\u540e\u9762\u7d27\u63a5\u7740\u7684\u662f\u522b\u540d\uff0c\u7136\u540e\u662f\u4e00\u4e2a\u7b49\u53f7\u4ee5\u53ca\u4e00\u4e2a\u5df2\u6709\u7684\u7c7b\u578b\u540d\uff0c\u4f8b\u5982\uff1a 1 using distance_t = double ; // distance_t \u4f5c\u4e3a double \u7684\u522b\u540d \u5f88\u591a\u7c7b\u578b\u522b\u540d\u4f1a\u4f7f\u7528 \u201c _t \u201d \u6216 \u201c _type \u201d \u540e\u7f00\u6765\u51cf\u5c11\u547d\u540d\u51b2\u7a81\u7684\u51e0\u7387\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u4e60\u60ef\u7684\u4e00\u81f4\u6027\u5e76\u4e0d\u597d\uff0c\u5f88\u591a\u7c7b\u578b\u522b\u540d\u662f\u6ca1\u6709\u540e\u7f00\u7684\u3002 1 2 using distance_type = double ; // \u6ca1\u95ee\u9898, \u540e\u9762\u7684\u7ae0\u8282\u591a\u4f1a\u4f7f\u7528\u8fd9\u79cd\u5f62\u5f0f using distance = double ; // \u8fd9\u6837\u4e5f\u53ef\u4ee5\uff0c, \u4f46\u662f\u53ef\u80fd\u4f1a\u548c\u5176\u4ed6\u53d8\u91cf\u540d\u9020\u6210\u6df7\u6dc6\u6216\u51b2\u7a81 \u4e00\u65e6\u5b8c\u6210\u5b9a\u4e49\u540e\uff0c \u7c7b\u578b\u522b\u540d \u53ef\u4ee5\u5728\u4efb\u4f55\u4f7f\u7528 \u7c7b\u578b \u7684\u5730\u65b9\u4f7f\u7528\u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf\uff1a 1 distance_t milesToDestination { 3.4 }; // \u5b9a\u4e49 double \u7c7b\u578b\u7684\u53d8\u91cf \u5f53\u7f16\u8bd1\u5668\u9047\u5230\u4e00\u4e2a\u7c7b\u578b\u522b\u540d\u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u628a\u522b\u540d\u66ff\u6362\u4e3a\u771f\u6b63\u7684\u7c7b\u578b\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { using distance_t = double ; // \u5b9a\u4e49 distance_t \u4f5c\u4e3a double \u7684\u522b\u540d distance_t milesToDestination { 3.4 }; // \u5b9a\u4e49 double \u7c7b\u578b\u53d8\u91cf std :: cout << milesToDestination << '\\n' ; // \u6253\u5370 double \u503c return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 3.4 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u5c06 distance_t \u5b9a\u4e49\u4e3a double \u7684\u522b\u540d\u3002 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a distance_t \u7c7b\u578b\u7684\u53d8\u91cf milesToDestination \u3002\u56e0\u4e3a\u7f16\u8bd1\u5668\u6307\u5b9a distance_t \u662f\u4e00\u4e2a\u522b\u540d\uff0c\u6240\u4ee5\u5b83\u4f1a\u4f7f\u7528\u771f\u5b9e\u7684\u53d8\u91cf\u7c7b\u578b\uff0c\u5373 double \u3002\u56e0\u6b64\uff0c\u53d8\u91cf milesToDestination \u5728\u7f16\u8bd1\u65f6\u5b9e\u9645\u4e0a\u662f double \u7c7b\u578b\u7684\uff0c\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u5b83\u7684\u884c\u4e3a\u4e5f\u662f\u548c double \u7c7b\u578b\u5b8c\u5168\u4e00\u81f4\u7684\u3002 \u6700\u540e\uff0c\u5c06 milesToDestination \u7684\u503c\u4f5c\u4e3a double \u7c7b\u578b\u6253\u5370\u3002 \u7c7b\u578b\u522b\u540d\u5e76\u4e0d\u662f\u4e00\u79cd\u65b0\u7684\u7c7b\u578b \u7c7b\u578b\u522b\u540d\u5728\u521b\u5efa\u65f6\u5e76\u6ca1\u6709\u5b9e\u9645\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7c7b\u578b\u2014\u2014\u5b83\u53ea\u662f\u4e3a\u5df2\u6709\u7684\u7c7b\u578b\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u6807\u8bc6\u7b26\u3002\u7c7b\u578b\u522b\u540d\u548c\u5b83\u5bf9\u5e94\u7684\u7c7b\u578b\u662f\u53ef\u4ee5\u5b8c\u5168\u4e92\u6362\u7684\u3002 \u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u505a\u4e00\u4e9b\u8bed\u6cd5\u4e0a\u6b63\u786e\uff0c\u4f46\u662f\u8bed\u4e49\u4e0a\u6ca1\u4ec0\u4e48\u5b9e\u9645\u610f\u4e49\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 int main () { using miles_t = long ; // define miles_t as an alias for type long using speed_t = long ; // define speed_t as an alias for type long miles_t distance { 5 }; // distance is actually just a long speed_t mhz { 3200 }; // mhz is actually just a long // The following is syntactically valid (but semantically meaningless) distance = mhz ; return 0 ; } \u5c3d\u7ba1\uff0c\u4ece\u6982\u5ff5\u4e0a\u6765\u8bb2\u6211\u4eec\u5e0c\u671b miles_t \u548c speed_t \u5177\u6709\u4e0d\u540c\u7684\u542b\u4e49\uff0c\u4f46\u662f\u5b83\u4eec\u5b9e\u9645\u4e0a\u90fd\u662f long \u7c7b\u578b\u7684\u3002\u8fd9\u4e5f\u610f\u5473\u7740 miles_t \u3001 speed_t \u548c long \u53ef\u4ee5\u4e92\u6362\u4f7f\u7528\u3002\u7684\u786e\uff0c\u5f53\u6211\u4eec\u628a speed_t \u7c7b\u578b\u7684\u503c\u8d4b\u503c\u7ed9 miles_t \u7c7b\u578b\u7684\u503c\u662f\uff0c\u7f16\u8bd1\u5668\u4f1a\u8ba4\u4e3a\u8fd9\u53ea\u662f\u5c06\u4e00\u4e2a long \u8d4b\u503c\u7ed9\u4e86\u53e6\u5916\u4e00\u4e2a long \uff0c\u6240\u4ee5\u4e0d\u4f1a\u4ea7\u751f\u4efb\u4f55\u8b66\u544a\u6216\u8005\u62a5\u9519\u4fe1\u606f\u3002 \u56e0\u4e3a\u7f16\u8bd1\u5668\u4e0d\u4f1a\u8bc6\u522b\u8fd9\u4e9b \u8bed\u4e49\u9519\u8bef \uff0c\u6240\u4ee5\u6211\u4eec\u8bf4\u7c7b\u578b\u522b\u540d\u5e76\u4e0d\u662f \u7c7b\u578b\u5b89\u5168 \u7684\u3002\u4e0d\u8fc7\uff0c\u5b83\u4eec\u4ecd\u7136\u975e\u5e38\u6709\u7528\u3002 \u6ce8\u610f \u6ce8\u610f\u4e0d\u8981\u6df7\u6dc6\u4f7f\u7528\u4e24\u4e2a\u8bed\u4e49\u4e0d\u540c\u7684\u7c7b\u578b\u522b\u540d \u9898\u5916\u8bdd \u6709\u4e9b\u8bed\u8a00\u652f\u6301\u5f3a\u7c7b\u578btypedef(\u6216\u5f3a\u7c7b\u578b\u522b\u540d)\u7684\u6982\u5ff5\u3002\u5f3a\u7c7b\u578btypedef\u5b9a\u4e49\u5b9e\u9645\u4e0a\u521b\u5efa\u4e86\u4e00\u4e2a\u5177\u6709\u539f\u7c7b\u578b\u6240\u6709\u539f\u59cb\u5c5e\u6027\u7684\u65b0\u7c7b\u578b\uff0c\u4f46\u5982\u679c\u8bd5\u56fe\u5c06\u522b\u540d\u7c7b\u578b\u7684\u503c\u548c\u5f3a\u7c7b\u578b\u5b9a\u4e49\u7684\u503c\u6df7\u5408\u4f7f\u7528\uff0c\u7f16\u8bd1\u5668\u5c06\u629b\u51fa\u9519\u8bef\u3002\u5bf9\u4e8e C++20 \u6765\u8bf4\uff0cC++ \u5e76\u4e0d\u76f4\u63a5\u652f\u6301\u5f3a\u7c7b\u578btypedef(\u5c3d\u7ba1\u679a\u4e3e\u7c7b\u6709\u7c7b\u4f3c\u4e4b\u5904\uff0c\u53c2\u89c1\uff1a 10.4 -- Scoped enumerations (enum classes) )\uff0c\u4f46\u662f\u6709\u5f88\u591a\u7b2c\u4e09\u65b9\u7684 C++ \u5e93\u90fd\u5b9e\u73b0\u4e86\u5f3atypedef\u7c7b\u4f3c\u7684\u884c\u4e3a\u3002 \u7c7b\u578b\u522b\u540d\u7684\u4f5c\u7528\u57df \u7531\u4e8e\u4f5c\u7528\u57df\u662f\u6807\u8bc6\u7b26\u7684\u5c5e\u6027\uff0c\u7c7b\u578b\u522b\u540d\u6807\u8bc6\u7b26\u9075\u5faa\u4e0e\u53d8\u91cf\u6807\u8bc6\u7b26\u76f8\u540c\u7684\u4f5c\u7528\u57df\u89c4\u5219\uff1a\u5728\u5757\u5185\u5b9a\u4e49\u7684\u7c7b\u578b\u522b\u540d\u5177\u6709\u5757\u4f5c\u7528\u57df\uff0c\u5e76\u4e14\u4ec5\u5728\u8be5\u5757\u5185\u53ef\u7528\uff0c\u800c\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u7c7b\u578b\u522b\u540d\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\uff0c\u76f4\u5230\u6587\u4ef6\u672b\u5c3e\u90fd\u53ef\u7528\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c miles_t \u548c speed_t \u53ea\u5728 main() \u51fd\u6570\u4e2d\u53ef\u7528\u3002 \u5982\u679c\u4f60\u9700\u8981\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528\u4e00\u4e2a\u6216\u591a\u4e2a\u7c7b\u578b\u522b\u540d\uff0c\u5b83\u4eec\u53ef\u4ee5\u5728\u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\uff0c\u5e76\u5728\u4efb\u4f55\u9700\u8981\u4f7f\u7528\u8be5\u5b9a\u4e49\u7684\u4ee3\u7801\u6587\u4ef6\u4e2d #include \uff1a mytypes.h 1 2 3 4 5 6 7 #ifndef MYTYPES #define MYTYPES using miles_t = long ; using speed_t = long ; #endif \u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5 include \u7684\u7c7b\u578b\u522b\u540d\u5177\u6709\u5168\u5c40\u4f5c\u7528\u57df\u3002 Typedef typedef (\u662f\u201ctype definition\u201d\u7684\u7f29\u5199) \u5173\u952e\u5b57\u7684\u8bed\u4e49\u548c using \u662f\u7c7b\u4f3c\u7684\uff0c\u4f46\u662f\u8bed\u6cd5\u987a\u5e8f\u662f\u76f8\u53cd\u7684\u3002 1 2 3 // \u4e0b\u9762\u4e24\u4e2a\u522b\u540d\u662f\u4e00\u81f4\u7684 typedef long miles_t ; using miles_t = long ; typedef \u5b58\u5728\u4e8eC++\u4e2d\u4ecd\u7136\u662f\u51fa\u4e8e\u5386\u53f2\u539f\u56e0\uff0c\u5e76\u4e0d\u63a8\u8350\u4f7f\u7528\u5b83\u3002 typedef \u8fd8\u6709\u4e00\u4e9b\u8bed\u6cd5\u95ee\u9898\u3002\u9996\u5148\uff0c\u5f88\u5bb9\u6613\u5fd8\u8bb0\u5e94\u8be5\u628a typedef \u540d\u653e\u5728\u524d\u9762\u8fd8\u662f\u5e94\u8be5\u628a\u522b\u540d\u653e\u5728\u524d\u9762\u3002\u4e0b\u9762\u54ea\u4e2a\u662f\u5bf9\u7684\uff1f 1 2 typedef distance_t double ; // \u9519\u8bef (typedef \u540d\u5e94\u8be5\u653e\u5728\u524d\u9762) typedef double distance_t ; // \u6b63\u786e (\u9700\u8981\u521b\u5efa\u522b\u540d\u7684\u7c7b\u578b\u5e94\u8be5\u653e\u5728\u524d\u9762) \u8fd9\u4e2a\u662f\u5f88\u5bb9\u6613\u5199\u53cd\u7684\uff0c\u4e0d\u8fc7\u597d\u5728\u5199\u53cd\u7684\u65f6\u5019\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002 \u5176\u6b21\uff0c\u5728\u5904\u7406\u590d\u6742\u7c7b\u578b\u65f6\uff0c typedef \u7684\u8bed\u6cd5\u7279\u522b\u4e11\u964b\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u8fd9\u4e2a typedef \u7684\u4f8b\u5b50\u5c31\u975e\u5e38\u96be\u4ee5\u9605\u8bfb\uff0c\u800c\u4f7f\u7528 using \u521b\u5efa\u7684\u7b49\u4ef7\u7684\u7c7b\u578b\u522b\u540d\u5219\u66f4\u5bb9\u6613\u9605\u8bfb\uff1a 1 2 typedef int ( * fcn_t )( double , char ); // fcn_t hard to find using fcn_t = int ( * )( double , char ); // fcn_t easier to find \u5728\u4e0a\u9762\u7684 typedef \u5b9a\u4e49\u4e2d\uff0c\u65b0\u7c7b\u578b( fcn_t )\u88ab\u85cf\u5728\u4e86\u5b9a\u4e49\u7684\u4e2d\u95f4\u90e8\u5206\uff0c\u8fd9\u4f7f\u5f97\u8be5\u5b9a\u4e49\u53d8\u5f97\u5f88\u96be\u9605\u8bfb\u3002 \u7b2c\u4e09\uff0c\u201ctypedef\u201d \u8fd9\u4e2a\u770b\u8d77\u6765\u50cf\u662f\u8981\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u7c7b\u578b\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\uff0c\u4ece\u4e4b\u524d\u7684\u4f8b\u5b50\u53ef\u4ee5\u770b\u51fa\uff0c\u7c7b\u578b\u548c\u7c7b\u578b\u522b\u540d\u662f\u53ef\u4ee5\u4e92\u6362\u4f7f\u7528\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u521b\u5efa\u7c7b\u578b\u522b\u540d\u65f6\uff0c\u5c3d\u91cf\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u8bed\u6cd5\u800c\u4e0d\u662f typedef \u8bed\u6cd5\u3002 \u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u4f7f\u7528\u7c7b\u578b\u522b\u540d\uff1f \u5728\u4e86\u89e3\u4e86\u4ec0\u4e48\u662f\u7c7b\u578b\u522b\u540d\u4e4b\u540e\uff0c\u662f\u65f6\u5019\u8be5\u804a\u804a\uff0c\u5408\u9002\u5e94\u8be5\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u5462\uff1f \u5728\u5e73\u53f0\u65e0\u5173\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528\u7c7b\u578b\u522b\u540d \u7c7b\u578b\u522b\u540d\u7684\u4e00\u4e2a\u7528\u9014\u662f\uff0c\u5b83\u4eec\u53ef\u4ee5\u7528\u6765\u9690\u85cf\u5e73\u53f0\u7279\u5b9a\u7684\u7ec6\u8282\u3002\u5728\u67d0\u4e9b\u5e73\u53f0\u4e0a\uff0c int \u4e3a2\u5b57\u8282\uff0c\u800c\u5728\u5176\u4ed6\u5e73\u53f0\u4e0a\u4e3a 4 \u5b57\u8282\u3002\u56e0\u6b64\uff0c\u5728\u7f16\u5199\u4e0e\u5e73\u53f0\u65e0\u5173\u7684\u4ee3\u7801\u65f6\uff0c\u4f7f\u7528 int \u5b58\u50a8\u8d85\u8fc7 2 \u5b57\u8282\u7684\u4fe1\u606f\u53ef\u80fd\u4f1a\u6709\u6f5c\u5728\u7684\u5371\u9669\u3002 \u56e0\u4e3a\u4ece\u5b57\u9762\u4e0a\u5e76\u4e0d\u80fd\u770b\u51fa char \u3001 short \u3001 int \u548c long \u7684\u5927\u5c0f\uff0c\u6240\u4ee5\u8de8\u5e73\u53f0\u7a0b\u5e8f\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u6765\u5b9a\u4e49\u4ee5\u4f4d\u4e3a\u5355\u4f4d\u5305\u542b\u7c7b\u578b\u5927\u5c0f\u7684\u522b\u540d\u662f\u76f8\u5f53\u5e38\u89c1\u7684\u3002\u4f8b\u5982\uff0c int8_t \u662f\u4e00\u4e2a 8 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\uff0c int16_t \u662f\u4e00\u4e2a 16 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\uff0c int32_t \u662f\u4e00\u4e2a32\u4f4d\u6709\u7b26\u53f7\u6574\u6570\u3002\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u6709\u52a9\u4e8e\u9632\u6b62\u9519\u8bef\uff0c\u5e76\u66f4\u6e05\u695a\u5730\u8bf4\u660e\u5bf9\u53d8\u91cf\u5927\u5c0f\u7684\u671f\u671b\u503c\u662f\u591a\u5c11\u3002 \u4e3a\u4e86\u786e\u4fdd\u6bcf\u4e2a\u522b\u540d\u7c7b\u578b\u89e3\u6790\u4e3a\u6b63\u786e\u5927\u5c0f\u7684\u7c7b\u578b\uff0c\u8fd9\u7c7b\u7c7b\u578b\u522b\u540d\u901a\u5e38\u4e0e\u9884\u5904\u7406\u5668\u6307\u4ee4\u4e00\u8d77\u4f7f\u7528: 1 2 3 4 5 6 7 8 9 #ifdef INT_2_BYTES using int8_t = char ; using int16_t = int ; using int32_t = long ; #else using int8_t = char ; using int16_t = short ; using int32_t = int ; #endif \u5728\u67d0\u4e9b\u673a\u5668\u4e0a\uff0c\u6574\u578b\u53ea\u5360 2 \u4e2a\u5b57\u8282\uff0c\u8fd9\u65f6\u5b8f INT_2_BYTES \u662f\u88ab #defined \u8fc7\u7684\uff0c\u6240\u4ee5\u7a0b\u5e8f\u5728\u7f16\u8bd1\u65f6\u4f1a\u5c06\u8fd9\u6bb5\u4ee3\u7801\u7684\u4e0a\u534a\u90e8\u5206\u5b9a\u4e49\u7684\u7c7b\u578b\u522b\u540d\u8fdb\u884c\u7f16\u8bd1\u3002\u5728\u6574\u578b\u5360 4 \u4e2a\u5b57\u8282\u7684\u673a\u5668\u4e0a\uff0c \u5b8f INT_2_BYTES \u4e0d\u5b58\u5728\uff0c\u56e0\u6b64\u4e0b\u534a\u90e8\u5206\u7684\u7c7b\u578b\u522b\u540d\u4f1a\u88ab\u7f16\u8bd1\u3002 \u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528 char \u3001 short \u3001 int \u548c long \u7684\u7ec4\u5408\u5728\u7279\u5b9a\u7684\u673a\u5668\u4e0a\u5c06 int8_t \u89e3\u6790\u4e3a 1 \u4e2a\u5b57\u8282\u7684\u6574\u6570\uff0c int16_t \u89e3\u6790\u4e3a 2 \u4e2a\u5b57\u8282\u7684\u6574\u6570\uff0c int32_t \u89e3\u6790\u4e3a 4 \u4e2a\u5b57\u8282\u7684\u6574\u6570\u3002 \u5bf9\u4e8e\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b (\u4f8b\u5982 std::int_fast16_t \u548c std::int_least32_t ) \u548c size_t \u7c7b\u578b (\u53c2\u89c1\uff1a 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t ) \u5176\u5b9e\u4e5f\u6b63\u662f\u8fd9\u4e9b\u57fa\u7840\u7c7b\u578b\u7684\u522b\u540d\u7f62\u4e86\u3002 \u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728\u4f7f\u7528 std::cout \u6253\u5370 8 \u4f4d\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u7684\u65f6\u5019\uff0c\u4f60\u591a\u534a\u4f1a\u6253\u5370\u51fa\u4e00\u4e2a\u5b57\u7b26\u7684\u539f\u56e0\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 #include // for fixed-width integers #include int main () { std :: int_least8_t x { 97 }; // int_least8_t \u5b9e\u9645\u4e0a\u662f char \u7684\u7c7b\u578b\u522b\u540d std :: cout << x ; return 0 ; } \u7a0b\u5e8f\u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 a \u56e0\u4e3a std::int_least8_t \u901a\u5e38\u88ab\u5b9a\u4e49\u4e3a char \u7684\u7c7b\u578b\u522b\u540d\uff0c\u53d8\u91cf x \u5c06\u88ab\u5b9a\u4e49\u4e3a char \u3002 \u800c char \u7c7b\u578b\u5c06\u5176\u503c\u6253\u5370\u4e3aASCII\u5b57\u7b26\uff0c\u800c\u4e0d\u662f\u6574\u6570\u503c\u3002 \u4f7f\u7528\u7c7b\u578b\u522b\u540d\u7b80\u5316\u7c7b\u578b \u867d\u7136\u6211\u4eec\u76ee\u524d\u53ea\u4f7f\u7528\u8fc7\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u662f\u5bf9\u4e8e\u9ad8\u9636C++\u7f16\u7a0b\u6765\u8bf4\uff0c\u7c7b\u578b\u901a\u5e38\u662f\u590d\u6742\u4e14\u5197\u957f\u7684\uff0c\u5982\u679c\u4f60\u624b\u52a8\u8f93\u5165\u5b83\u4eec\u4f1a\u975e\u5e38\u8d39\u52b2\u3002\u4f8b\u5982\uff0c\u6709\u4e9b\u51fd\u6570\u548c\u53d8\u91cf\u7684\u5b9a\u4e49\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // for std::string #include // for std::vector #include // for std::pair bool hasDuplicates ( std :: vector < std :: pair < std :: string , int >> pairlist ) { // some code here return false ; } int main () { std :: vector < std :: pair < std :: string , int >> pairlist ; return 0 ; } \u5728\u6bcf\u4e2a\u9700\u8981\u7684\u5730\u65b9\u8f93\u5165 std::vector> \u4e0d\u4ec5\u7e41\u7410\uff0c\u800c\u4e14\u975e\u5e38\u5bb9\u6613\u51fa\u9519\u3002\u5982\u679c\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u5219\u7b80\u5355\u7684\u591a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include // for std::string #include // for std::vector #include // for std::pair using pairlist_t = std :: vector < std :: pair < std :: string , int >> ; // make pairlist_t an alias for this crazy type bool hasDuplicates ( pairlist_t pairlist ) // use pairlist_t in a function parameter { // some code here return false ; } int main () { pairlist_t pairlist ; // instantiate a pairlist_t variable return 0 ; } \u770b\u8d77\u6765\u597d\u591a\u4e86\u5bf9\u5427\uff01\u73b0\u5728\uff0c\u4f60\u9700\u8981\u8f93\u5165\u7684\u662f pairlist_t \u800c\u4e0d\u662f std::vector> \u3002 \u5982\u679c\u4f60\u8fd8\u4e0d\u77e5\u9053 std::vector \u3001 std::pair \u6216\u8005\u90a3\u4e9b\u770b\u8d77\u6765\u5f88\u5413\u4eba\u7684\u5c16\u62ec\u53f7\u662f\u4ec0\u4e48\uff0c\u4e5f\u4e0d\u7528\u62c5\u5fc3\u3002\u73b0\u5728\u4f60\u53ea\u9700\u8981\u77e5\u9053\u7c7b\u578b\u522b\u540d\u53ef\u4ee5\u7b80\u5316\u7c7b\u578b\u3001\u4f7f\u4f60\u7684\u4ee3\u7801\u66f4\u6613\u8bfb\uff0c\u51cf\u5c11\u7e41\u7410\u7684\u8f93\u5165\u5c31\u53ef\u4ee5\u4e86\u3002 \u8fd9\u4e5f\u662f\u7c7b\u578b\u522b\u540d\u6700\u6709\u7528\u7684\u5730\u65b9\u3002 \u4f7f\u7528\u7c7b\u578b\u522b\u540d\u8ba9\u7a0b\u5e8f\u66f4\u6e05\u6670 \u7c7b\u578b\u522b\u540d\u8fd8\u6709\u52a9\u4e8e\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8df5\u884c\u4ee3\u7801\u5373\u6587\u6863\u7684\u601d\u60f3\u3002 With variables, we have the variable\u2019s identifier to help document the purpose of the variable. But consider the case of a function\u2019s return value. Data types such as char , int , long , double , and bool are good for describing what type a function returns, but more often we want to know what purpose a return value serves. \u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7\u6807\u8bc6\u7b26\uff08\u53d8\u91cf\u540d\uff09\u6765\u5e2e\u52a9\u8868\u793a\u5b83\u7684\u7528\u9014\u3002\u4f46\u662f\u5bf9\u4e8e\u51fd\u6570\u8fd4\u56de\u503c\u6765\u8bf4\uff0c\u5c3d\u7ba1 char \u3001 int \u3001 long \u3001 double \u548c bool \u53ef\u4ee5\u8868\u660e\u51fd\u6570\u503c\u7684\u7c7b\u578b\uff0c\u4f46\u5b83\u4eec\u5e76\u6ca1\u6709\u529e\u6cd5\u5f88\u597d\u5730\u8868\u660e\u51fd\u6570\u8fd4\u56de\u503c\u7684\u7528\u9014\u7a76\u7adf\u662f\u4ec0\u4e48\u3002 \u5bf9\u4e8e\u4e0b\u9762\u7684\u51fd\u6570\u6765\u8bf4\uff1a 1 int gradeTest (); \u51fd\u6570\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u662f\u6574\u578b\uff0c\u4f46\u662f\u8fd9\u4e2a\u6574\u578b\u8fd4\u56de\u503c\u7684\u542b\u4e49\u5230\u5e95\u662f\u4ec0\u4e48\u5462\uff1f\u662f\u5b66\u751f\u7684\u5206\u6570\uff1f\u662f\u6ca1\u6709\u4f5c\u7b54\u7684\u95ee\u9898\u6570\uff1f\u662f\u5b66\u53f7\uff1f\u8fd8\u662f\u9519\u8bef\u7801\uff1f\u6ca1\u6709\u4eba\u77e5\u9053\uff0c\u56e0\u4e3a int \u5e76\u4e0d\u80fd\u8868\u8fbe\u8fd9\u5c42\u542b\u4e49\u3002\u5982\u679c\u5e78\u8fd0\u7684\u8bdd\uff0c\u4e5f\u8bb8\u51fd\u6570\u6709\u6587\u6863\u53ef\u4f9b\u6211\u4eec\u53c2\u8003\u3002\u5982\u679c\u4e0d\u5e78\u7684\u8bdd\uff0c\u4f60\u5c31\u5fc5\u987b\u9605\u8bfb\u6e90\u7801\u7136\u540e\u53bb\u731c\u6d4b\u5b83\u7684\u7528\u9014\u4e86\u3002 \u800c\u5bf9\u4e8e\u4e0b\u9762\u8fd9\u4e2a\u4f7f\u7528\u4e86\u7c7b\u578b\u522b\u540d\u7684\u7b49\u4ef7\u7684\u4f8b\u5b50\u6765\u8bf4\uff1a 1 2 using testScore_t = int ; testScore_t gradeTest (); \u8fd4\u56de\u503c\u7c7b\u578b\u4e3a testScore_t \uff0c\u5b83\u80fd\u591f\u66f4\u597d\u5730\u8868\u660e\u51fd\u6570\u7684\u8fd4\u56de\u503c\u8868\u793a\u4e00\u6b21\u6d4b\u9a8c\u7684\u5206\u6570\u3002 \u6839\u636e\u6211\u4eec\u7684\u7ecf\u9a8c\uff0c\u521b\u5efa\u7c7b\u578b\u522b\u540d\u53ea\u662f\u4e3a\u4e86\u8bb0\u5f55\u5355\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u662f\u4e0d\u503c\u5f97\u7684(\u4f7f\u7528\u6ce8\u91ca\u4ee3\u66ff)\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u5df2\u7ecf\u56e0\u4e3a\u5176\u4ed6\u539f\u56e0\u521b\u5efa\u4e86\u7c7b\u578b\u522b\u540d\uff0c\u8fd9\u53ef\u80fd\u662f\u4e00\u4e2a\u989d\u5916\u597d\u5904\u3002 \u4f7f\u7528\u7c7b\u578b\u522b\u540d\u63d0\u9ad8\u4ee3\u7801\u53ef\u7ef4\u62a4\u6027 \u4f7f\u7528\u7c7b\u578b\u522b\u540d\u8fd8\u53ef\u4ee5\u5728\u4e0d\u8fdb\u884c\u5927\u91cf\u6539\u52a8\u7684\u60c5\u51b5\u4e0b\u4fee\u6539\u5bf9\u8c61\u7684\u57fa\u672c\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5b66\u751f\u7684ID\u539f\u672c\u4f7f\u7528 short \u6765\u4fdd\u5b58\uff0c\u4f46\u540e\u6765\u51b3\u5b9a\u7528 long \u6765\u4ee3\u66ff\uff0c\u6b64\u65f6\u4f60\u5c31\u5fc5\u987b\u5c06\u5927\u91cf\u7684 short \u66ff\u6362\u4e3a long \u3002\u8fd9\u4e2a\u65f6\u5019\u5f80\u5f80\u5f88\u96be\u786e\u5b9a\u7a76\u7adf\u54ea\u4e9b short \u662f\u7528\u6765\u4fdd\u5b58ID\u7684\uff0c\u800c\u54ea\u4e9b\u5219\u7528\u4e8e\u5176\u4ed6\u76ee\u7684\u3002 \u7136\u800c\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u4e86\u7c7b\u578b\u522b\u540d\uff0c\u90a3\u4e48\u66f4\u6539\u7c7b\u578b\u5c31\u50cf\u66f4\u65b0\u7c7b\u578b\u522b\u540d\u4e00\u6837\u7b80\u5355(\u4f8b\u5982\uff0c\u5c06 using studentID_t = short; \u4fee\u6539\u4e3a using studentID_t = long; )\u3002 \u867d\u7136\u8fd9\u770b\u8d77\u6765\u662f\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u7279\u6027\uff0c\u4f46\u6bcf\u5f53\u7c7b\u578b\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u6211\u4eec\u4ecd\u7136\u9700\u8981\u4fdd\u6301\u8c28\u614e\uff0c\u56e0\u4e3a\u7a0b\u5e8f\u7684\u884c\u4e3a\u4e5f\u53ef\u80fd\u968f\u4e4b\u53d1\u751f\u53d8\u5316\u3002\u5f53\u5c06\u7c7b\u578b\u522b\u540d\u7684\u7c7b\u578b\u66f4\u6539\u4e3a\u4e0d\u540c\u7c7b\u578b\u65cf\u4e2d\u7684\u7c7b\u578b\u65f6\u5c24\u5176\u5982\u6b64(\u4f8b\u5982\uff0c\u5c06\u6574\u6570\u66f4\u6539\u4e3a\u6d6e\u70b9\u503c\uff0c\u6216\u53cd\u4e4b\u4ea6\u7136)\uff01\u65b0\u7c7b\u578b\u53ef\u80fd\u6709\u6d6e\u70b9\u6570\u6bd4\u8f83\u6216\u6574\u6570/\u6d6e\u70b9\u9664\u6cd5\u95ee\u9898\uff0c\u6216\u5176\u4ed6\u65e7\u7c7b\u578b\u6240\u6ca1\u6709\u7684\u95ee\u9898\u3002\u5982\u679c\u4f60\u5c06\u73b0\u6709\u7684\u7c7b\u578b\u66f4\u6539\u4e3a\u5176\u4ed6\u7c7b\u578b\uff0c\u4ee3\u7801\u5e94\u8be5\u91cd\u65b0\u3001\u5f7b\u5e95\u5730\u6d4b\u8bd5\u3002 \u7f3a\u70b9\u548c\u7ed3\u8bba \u7136\u7c7b\u578b\u522b\u540d\u63d0\u4f9b\u4e86\u4e00\u4e9b\u597d\u5904\uff0c\u4f46\u5b83\u4eec\u4e5f\u5728\u4ee3\u7801\u4e2d\u5f15\u5165\u4e86\u53e6\u4e00\u4e2a\u9700\u8981\u7406\u89e3\u7684\u6807\u8bc6\u7b26\u3002\u5982\u679c\u8fd9\u4e00\u70b9\u6ca1\u6709\u88ab\u53ef\u8bfb\u6027\u6216\u7406\u89e3\u7684\u597d\u5904\u6240\u62b5\u6d88\uff0c\u90a3\u4e48\u7c7b\u578b\u522b\u540d\u5f0a\u5927\u4e8e\u5229\u3002 \u7c7b\u578b\u522b\u540d\u4f7f\u7528\u4e0d\u5f53\u7684\u8bdd\uff0c\u4f1a\u5c06\u719f\u6089\u7684\u7c7b\u578b(\u4f8b\u5982 std::string )\u9690\u85cf\u5728\u81ea\u5b9a\u4e49\u540d\u79f0\u540e\u9762\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b(\u4f8b\u5982\u667a\u80fd\u6307\u9488\uff0c\u6211\u4eec\u5c06\u5728\u4ee5\u540e\u7684\u7ae0\u8282\u4e2d\u8ba8\u8bba)\uff0c\u6a21\u7cca\u7684\u7c7b\u578b\u4fe1\u606f\u4e5f\u53ef\u80fd\u4e0d\u5229\u4e8e\u7406\u89e3\u7c7b\u578b\u5e94\u8be5\u5982\u4f55\u5de5\u4f5c\u3002 \u56e0\u6b64\uff0c\u7c7b\u578b\u522b\u540d\u5e94\u8be5\u4e3b\u8981\u7528\u4e8e\u5bf9\u4ee3\u7801\u53ef\u8bfb\u6027\u6216\u4ee3\u7801\u7ef4\u62a4\u6709\u660e\u663e\u597d\u5904\u7684\u60c5\u51b5\u3002\u8fd9\u65e2\u662f\u4e00\u95e8\u79d1\u5b66\uff0c\u4e5f\u662f\u4e00\u95e8\u827a\u672f\u3002\u5f53\u7c7b\u578b\u522b\u540d\u53ef\u4ee5\u5728\u4ee3\u7801\u7684\u8bb8\u591a\u5730\u65b9\u4f7f\u7528\u800c\u4e0d\u662f\u5728\u5be5\u5be5\u51e0\u5904\u4f7f\u7528\u65f6\uff0c\u624d\u662f\u5b83\u4eec\u5927\u653e\u5f02\u5f69\u7684\u673a\u4f1a\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u7c7b\u578b\u522b\u540d\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\u3001\u4ee3\u7801\u7ef4\u62a4\u6027\u65f6\uff0c\u660e\u667a\u800c\u5ba1\u614e\u5730\u4f7f\u7528\u5b83\u4eec\u3002","title":"8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d"},{"location":"8-6-Typedefs-and-type-aliases/#_1","text":"\u5728 C++ \u4e2d\uff0c using \u5173\u952e\u5b57\u8fd8\u53ef\u4ee5\u88ab\u7528\u6765\u4e3a\u5df2\u6709\u7684\u7c7b\u578b\u521b\u5efa\u4e00\u4e2a \u7c7b\u578b\u522b\u540d(type aliases) \u3002\u521b\u5efa\u522b\u540d\u65f6\uff0c\u5728 using \u5173\u952e\u5b57\u540e\u9762\u7d27\u63a5\u7740\u7684\u662f\u522b\u540d\uff0c\u7136\u540e\u662f\u4e00\u4e2a\u7b49\u53f7\u4ee5\u53ca\u4e00\u4e2a\u5df2\u6709\u7684\u7c7b\u578b\u540d\uff0c\u4f8b\u5982\uff1a 1 using distance_t = double ; // distance_t \u4f5c\u4e3a double \u7684\u522b\u540d \u5f88\u591a\u7c7b\u578b\u522b\u540d\u4f1a\u4f7f\u7528 \u201c _t \u201d \u6216 \u201c _type \u201d \u540e\u7f00\u6765\u51cf\u5c11\u547d\u540d\u51b2\u7a81\u7684\u51e0\u7387\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u4e60\u60ef\u7684\u4e00\u81f4\u6027\u5e76\u4e0d\u597d\uff0c\u5f88\u591a\u7c7b\u578b\u522b\u540d\u662f\u6ca1\u6709\u540e\u7f00\u7684\u3002 1 2 using distance_type = double ; // \u6ca1\u95ee\u9898, \u540e\u9762\u7684\u7ae0\u8282\u591a\u4f1a\u4f7f\u7528\u8fd9\u79cd\u5f62\u5f0f using distance = double ; // \u8fd9\u6837\u4e5f\u53ef\u4ee5\uff0c, \u4f46\u662f\u53ef\u80fd\u4f1a\u548c\u5176\u4ed6\u53d8\u91cf\u540d\u9020\u6210\u6df7\u6dc6\u6216\u51b2\u7a81 \u4e00\u65e6\u5b8c\u6210\u5b9a\u4e49\u540e\uff0c \u7c7b\u578b\u522b\u540d \u53ef\u4ee5\u5728\u4efb\u4f55\u4f7f\u7528 \u7c7b\u578b \u7684\u5730\u65b9\u4f7f\u7528\u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf\uff1a 1 distance_t milesToDestination { 3.4 }; // \u5b9a\u4e49 double \u7c7b\u578b\u7684\u53d8\u91cf \u5f53\u7f16\u8bd1\u5668\u9047\u5230\u4e00\u4e2a\u7c7b\u578b\u522b\u540d\u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u628a\u522b\u540d\u66ff\u6362\u4e3a\u771f\u6b63\u7684\u7c7b\u578b\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { using distance_t = double ; // \u5b9a\u4e49 distance_t \u4f5c\u4e3a double \u7684\u522b\u540d distance_t milesToDestination { 3.4 }; // \u5b9a\u4e49 double \u7c7b\u578b\u53d8\u91cf std :: cout << milesToDestination << '\\n' ; // \u6253\u5370 double \u503c return 0 ; } \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a 1 3.4 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u5c06 distance_t \u5b9a\u4e49\u4e3a double \u7684\u522b\u540d\u3002 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a distance_t \u7c7b\u578b\u7684\u53d8\u91cf milesToDestination \u3002\u56e0\u4e3a\u7f16\u8bd1\u5668\u6307\u5b9a distance_t \u662f\u4e00\u4e2a\u522b\u540d\uff0c\u6240\u4ee5\u5b83\u4f1a\u4f7f\u7528\u771f\u5b9e\u7684\u53d8\u91cf\u7c7b\u578b\uff0c\u5373 double \u3002\u56e0\u6b64\uff0c\u53d8\u91cf milesToDestination \u5728\u7f16\u8bd1\u65f6\u5b9e\u9645\u4e0a\u662f double \u7c7b\u578b\u7684\uff0c\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u5b83\u7684\u884c\u4e3a\u4e5f\u662f\u548c double \u7c7b\u578b\u5b8c\u5168\u4e00\u81f4\u7684\u3002 \u6700\u540e\uff0c\u5c06 milesToDestination \u7684\u503c\u4f5c\u4e3a double \u7c7b\u578b\u6253\u5370\u3002","title":"\u7c7b\u578b\u522b\u540d"},{"location":"8-6-Typedefs-and-type-aliases/#_2","text":"\u7c7b\u578b\u522b\u540d\u5728\u521b\u5efa\u65f6\u5e76\u6ca1\u6709\u5b9e\u9645\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7c7b\u578b\u2014\u2014\u5b83\u53ea\u662f\u4e3a\u5df2\u6709\u7684\u7c7b\u578b\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u6807\u8bc6\u7b26\u3002\u7c7b\u578b\u522b\u540d\u548c\u5b83\u5bf9\u5e94\u7684\u7c7b\u578b\u662f\u53ef\u4ee5\u5b8c\u5168\u4e92\u6362\u7684\u3002 \u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u505a\u4e00\u4e9b\u8bed\u6cd5\u4e0a\u6b63\u786e\uff0c\u4f46\u662f\u8bed\u4e49\u4e0a\u6ca1\u4ec0\u4e48\u5b9e\u9645\u610f\u4e49\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 int main () { using miles_t = long ; // define miles_t as an alias for type long using speed_t = long ; // define speed_t as an alias for type long miles_t distance { 5 }; // distance is actually just a long speed_t mhz { 3200 }; // mhz is actually just a long // The following is syntactically valid (but semantically meaningless) distance = mhz ; return 0 ; } \u5c3d\u7ba1\uff0c\u4ece\u6982\u5ff5\u4e0a\u6765\u8bb2\u6211\u4eec\u5e0c\u671b miles_t \u548c speed_t \u5177\u6709\u4e0d\u540c\u7684\u542b\u4e49\uff0c\u4f46\u662f\u5b83\u4eec\u5b9e\u9645\u4e0a\u90fd\u662f long \u7c7b\u578b\u7684\u3002\u8fd9\u4e5f\u610f\u5473\u7740 miles_t \u3001 speed_t \u548c long \u53ef\u4ee5\u4e92\u6362\u4f7f\u7528\u3002\u7684\u786e\uff0c\u5f53\u6211\u4eec\u628a speed_t \u7c7b\u578b\u7684\u503c\u8d4b\u503c\u7ed9 miles_t \u7c7b\u578b\u7684\u503c\u662f\uff0c\u7f16\u8bd1\u5668\u4f1a\u8ba4\u4e3a\u8fd9\u53ea\u662f\u5c06\u4e00\u4e2a long \u8d4b\u503c\u7ed9\u4e86\u53e6\u5916\u4e00\u4e2a long \uff0c\u6240\u4ee5\u4e0d\u4f1a\u4ea7\u751f\u4efb\u4f55\u8b66\u544a\u6216\u8005\u62a5\u9519\u4fe1\u606f\u3002 \u56e0\u4e3a\u7f16\u8bd1\u5668\u4e0d\u4f1a\u8bc6\u522b\u8fd9\u4e9b \u8bed\u4e49\u9519\u8bef \uff0c\u6240\u4ee5\u6211\u4eec\u8bf4\u7c7b\u578b\u522b\u540d\u5e76\u4e0d\u662f \u7c7b\u578b\u5b89\u5168 \u7684\u3002\u4e0d\u8fc7\uff0c\u5b83\u4eec\u4ecd\u7136\u975e\u5e38\u6709\u7528\u3002 \u6ce8\u610f \u6ce8\u610f\u4e0d\u8981\u6df7\u6dc6\u4f7f\u7528\u4e24\u4e2a\u8bed\u4e49\u4e0d\u540c\u7684\u7c7b\u578b\u522b\u540d \u9898\u5916\u8bdd \u6709\u4e9b\u8bed\u8a00\u652f\u6301\u5f3a\u7c7b\u578btypedef(\u6216\u5f3a\u7c7b\u578b\u522b\u540d)\u7684\u6982\u5ff5\u3002\u5f3a\u7c7b\u578btypedef\u5b9a\u4e49\u5b9e\u9645\u4e0a\u521b\u5efa\u4e86\u4e00\u4e2a\u5177\u6709\u539f\u7c7b\u578b\u6240\u6709\u539f\u59cb\u5c5e\u6027\u7684\u65b0\u7c7b\u578b\uff0c\u4f46\u5982\u679c\u8bd5\u56fe\u5c06\u522b\u540d\u7c7b\u578b\u7684\u503c\u548c\u5f3a\u7c7b\u578b\u5b9a\u4e49\u7684\u503c\u6df7\u5408\u4f7f\u7528\uff0c\u7f16\u8bd1\u5668\u5c06\u629b\u51fa\u9519\u8bef\u3002\u5bf9\u4e8e C++20 \u6765\u8bf4\uff0cC++ \u5e76\u4e0d\u76f4\u63a5\u652f\u6301\u5f3a\u7c7b\u578btypedef(\u5c3d\u7ba1\u679a\u4e3e\u7c7b\u6709\u7c7b\u4f3c\u4e4b\u5904\uff0c\u53c2\u89c1\uff1a 10.4 -- Scoped enumerations (enum classes) )\uff0c\u4f46\u662f\u6709\u5f88\u591a\u7b2c\u4e09\u65b9\u7684 C++ \u5e93\u90fd\u5b9e\u73b0\u4e86\u5f3atypedef\u7c7b\u4f3c\u7684\u884c\u4e3a\u3002","title":"\u7c7b\u578b\u522b\u540d\u5e76\u4e0d\u662f\u4e00\u79cd\u65b0\u7684\u7c7b\u578b"},{"location":"8-6-Typedefs-and-type-aliases/#_3","text":"\u7531\u4e8e\u4f5c\u7528\u57df\u662f\u6807\u8bc6\u7b26\u7684\u5c5e\u6027\uff0c\u7c7b\u578b\u522b\u540d\u6807\u8bc6\u7b26\u9075\u5faa\u4e0e\u53d8\u91cf\u6807\u8bc6\u7b26\u76f8\u540c\u7684\u4f5c\u7528\u57df\u89c4\u5219\uff1a\u5728\u5757\u5185\u5b9a\u4e49\u7684\u7c7b\u578b\u522b\u540d\u5177\u6709\u5757\u4f5c\u7528\u57df\uff0c\u5e76\u4e14\u4ec5\u5728\u8be5\u5757\u5185\u53ef\u7528\uff0c\u800c\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u7c7b\u578b\u522b\u540d\u5177\u6709\u6587\u4ef6\u4f5c\u7528\u57df\uff0c\u76f4\u5230\u6587\u4ef6\u672b\u5c3e\u90fd\u53ef\u7528\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c miles_t \u548c speed_t \u53ea\u5728 main() \u51fd\u6570\u4e2d\u53ef\u7528\u3002 \u5982\u679c\u4f60\u9700\u8981\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528\u4e00\u4e2a\u6216\u591a\u4e2a\u7c7b\u578b\u522b\u540d\uff0c\u5b83\u4eec\u53ef\u4ee5\u5728\u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\uff0c\u5e76\u5728\u4efb\u4f55\u9700\u8981\u4f7f\u7528\u8be5\u5b9a\u4e49\u7684\u4ee3\u7801\u6587\u4ef6\u4e2d #include \uff1a mytypes.h 1 2 3 4 5 6 7 #ifndef MYTYPES #define MYTYPES using miles_t = long ; using speed_t = long ; #endif \u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5 include \u7684\u7c7b\u578b\u522b\u540d\u5177\u6709\u5168\u5c40\u4f5c\u7528\u57df\u3002","title":"\u7c7b\u578b\u522b\u540d\u7684\u4f5c\u7528\u57df"},{"location":"8-6-Typedefs-and-type-aliases/#typedef","text":"typedef (\u662f\u201ctype definition\u201d\u7684\u7f29\u5199) \u5173\u952e\u5b57\u7684\u8bed\u4e49\u548c using \u662f\u7c7b\u4f3c\u7684\uff0c\u4f46\u662f\u8bed\u6cd5\u987a\u5e8f\u662f\u76f8\u53cd\u7684\u3002 1 2 3 // \u4e0b\u9762\u4e24\u4e2a\u522b\u540d\u662f\u4e00\u81f4\u7684 typedef long miles_t ; using miles_t = long ; typedef \u5b58\u5728\u4e8eC++\u4e2d\u4ecd\u7136\u662f\u51fa\u4e8e\u5386\u53f2\u539f\u56e0\uff0c\u5e76\u4e0d\u63a8\u8350\u4f7f\u7528\u5b83\u3002 typedef \u8fd8\u6709\u4e00\u4e9b\u8bed\u6cd5\u95ee\u9898\u3002\u9996\u5148\uff0c\u5f88\u5bb9\u6613\u5fd8\u8bb0\u5e94\u8be5\u628a typedef \u540d\u653e\u5728\u524d\u9762\u8fd8\u662f\u5e94\u8be5\u628a\u522b\u540d\u653e\u5728\u524d\u9762\u3002\u4e0b\u9762\u54ea\u4e2a\u662f\u5bf9\u7684\uff1f 1 2 typedef distance_t double ; // \u9519\u8bef (typedef \u540d\u5e94\u8be5\u653e\u5728\u524d\u9762) typedef double distance_t ; // \u6b63\u786e (\u9700\u8981\u521b\u5efa\u522b\u540d\u7684\u7c7b\u578b\u5e94\u8be5\u653e\u5728\u524d\u9762) \u8fd9\u4e2a\u662f\u5f88\u5bb9\u6613\u5199\u53cd\u7684\uff0c\u4e0d\u8fc7\u597d\u5728\u5199\u53cd\u7684\u65f6\u5019\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002 \u5176\u6b21\uff0c\u5728\u5904\u7406\u590d\u6742\u7c7b\u578b\u65f6\uff0c typedef \u7684\u8bed\u6cd5\u7279\u522b\u4e11\u964b\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u8fd9\u4e2a typedef \u7684\u4f8b\u5b50\u5c31\u975e\u5e38\u96be\u4ee5\u9605\u8bfb\uff0c\u800c\u4f7f\u7528 using \u521b\u5efa\u7684\u7b49\u4ef7\u7684\u7c7b\u578b\u522b\u540d\u5219\u66f4\u5bb9\u6613\u9605\u8bfb\uff1a 1 2 typedef int ( * fcn_t )( double , char ); // fcn_t hard to find using fcn_t = int ( * )( double , char ); // fcn_t easier to find \u5728\u4e0a\u9762\u7684 typedef \u5b9a\u4e49\u4e2d\uff0c\u65b0\u7c7b\u578b( fcn_t )\u88ab\u85cf\u5728\u4e86\u5b9a\u4e49\u7684\u4e2d\u95f4\u90e8\u5206\uff0c\u8fd9\u4f7f\u5f97\u8be5\u5b9a\u4e49\u53d8\u5f97\u5f88\u96be\u9605\u8bfb\u3002 \u7b2c\u4e09\uff0c\u201ctypedef\u201d \u8fd9\u4e2a\u770b\u8d77\u6765\u50cf\u662f\u8981\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u7c7b\u578b\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\uff0c\u4ece\u4e4b\u524d\u7684\u4f8b\u5b50\u53ef\u4ee5\u770b\u51fa\uff0c\u7c7b\u578b\u548c\u7c7b\u578b\u522b\u540d\u662f\u53ef\u4ee5\u4e92\u6362\u4f7f\u7528\u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u521b\u5efa\u7c7b\u578b\u522b\u540d\u65f6\uff0c\u5c3d\u91cf\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u8bed\u6cd5\u800c\u4e0d\u662f typedef \u8bed\u6cd5\u3002","title":"Typedef"},{"location":"8-6-Typedefs-and-type-aliases/#_4","text":"\u5728\u4e86\u89e3\u4e86\u4ec0\u4e48\u662f\u7c7b\u578b\u522b\u540d\u4e4b\u540e\uff0c\u662f\u65f6\u5019\u8be5\u804a\u804a\uff0c\u5408\u9002\u5e94\u8be5\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u5462\uff1f","title":"\u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u4f7f\u7528\u7c7b\u578b\u522b\u540d\uff1f"},{"location":"8-6-Typedefs-and-type-aliases/#_5","text":"\u7c7b\u578b\u522b\u540d\u7684\u4e00\u4e2a\u7528\u9014\u662f\uff0c\u5b83\u4eec\u53ef\u4ee5\u7528\u6765\u9690\u85cf\u5e73\u53f0\u7279\u5b9a\u7684\u7ec6\u8282\u3002\u5728\u67d0\u4e9b\u5e73\u53f0\u4e0a\uff0c int \u4e3a2\u5b57\u8282\uff0c\u800c\u5728\u5176\u4ed6\u5e73\u53f0\u4e0a\u4e3a 4 \u5b57\u8282\u3002\u56e0\u6b64\uff0c\u5728\u7f16\u5199\u4e0e\u5e73\u53f0\u65e0\u5173\u7684\u4ee3\u7801\u65f6\uff0c\u4f7f\u7528 int \u5b58\u50a8\u8d85\u8fc7 2 \u5b57\u8282\u7684\u4fe1\u606f\u53ef\u80fd\u4f1a\u6709\u6f5c\u5728\u7684\u5371\u9669\u3002 \u56e0\u4e3a\u4ece\u5b57\u9762\u4e0a\u5e76\u4e0d\u80fd\u770b\u51fa char \u3001 short \u3001 int \u548c long \u7684\u5927\u5c0f\uff0c\u6240\u4ee5\u8de8\u5e73\u53f0\u7a0b\u5e8f\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u6765\u5b9a\u4e49\u4ee5\u4f4d\u4e3a\u5355\u4f4d\u5305\u542b\u7c7b\u578b\u5927\u5c0f\u7684\u522b\u540d\u662f\u76f8\u5f53\u5e38\u89c1\u7684\u3002\u4f8b\u5982\uff0c int8_t \u662f\u4e00\u4e2a 8 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\uff0c int16_t \u662f\u4e00\u4e2a 16 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\uff0c int32_t \u662f\u4e00\u4e2a32\u4f4d\u6709\u7b26\u53f7\u6574\u6570\u3002\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u6709\u52a9\u4e8e\u9632\u6b62\u9519\u8bef\uff0c\u5e76\u66f4\u6e05\u695a\u5730\u8bf4\u660e\u5bf9\u53d8\u91cf\u5927\u5c0f\u7684\u671f\u671b\u503c\u662f\u591a\u5c11\u3002 \u4e3a\u4e86\u786e\u4fdd\u6bcf\u4e2a\u522b\u540d\u7c7b\u578b\u89e3\u6790\u4e3a\u6b63\u786e\u5927\u5c0f\u7684\u7c7b\u578b\uff0c\u8fd9\u7c7b\u7c7b\u578b\u522b\u540d\u901a\u5e38\u4e0e\u9884\u5904\u7406\u5668\u6307\u4ee4\u4e00\u8d77\u4f7f\u7528: 1 2 3 4 5 6 7 8 9 #ifdef INT_2_BYTES using int8_t = char ; using int16_t = int ; using int32_t = long ; #else using int8_t = char ; using int16_t = short ; using int32_t = int ; #endif \u5728\u67d0\u4e9b\u673a\u5668\u4e0a\uff0c\u6574\u578b\u53ea\u5360 2 \u4e2a\u5b57\u8282\uff0c\u8fd9\u65f6\u5b8f INT_2_BYTES \u662f\u88ab #defined \u8fc7\u7684\uff0c\u6240\u4ee5\u7a0b\u5e8f\u5728\u7f16\u8bd1\u65f6\u4f1a\u5c06\u8fd9\u6bb5\u4ee3\u7801\u7684\u4e0a\u534a\u90e8\u5206\u5b9a\u4e49\u7684\u7c7b\u578b\u522b\u540d\u8fdb\u884c\u7f16\u8bd1\u3002\u5728\u6574\u578b\u5360 4 \u4e2a\u5b57\u8282\u7684\u673a\u5668\u4e0a\uff0c \u5b8f INT_2_BYTES \u4e0d\u5b58\u5728\uff0c\u56e0\u6b64\u4e0b\u534a\u90e8\u5206\u7684\u7c7b\u578b\u522b\u540d\u4f1a\u88ab\u7f16\u8bd1\u3002 \u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528 char \u3001 short \u3001 int \u548c long \u7684\u7ec4\u5408\u5728\u7279\u5b9a\u7684\u673a\u5668\u4e0a\u5c06 int8_t \u89e3\u6790\u4e3a 1 \u4e2a\u5b57\u8282\u7684\u6574\u6570\uff0c int16_t \u89e3\u6790\u4e3a 2 \u4e2a\u5b57\u8282\u7684\u6574\u6570\uff0c int32_t \u89e3\u6790\u4e3a 4 \u4e2a\u5b57\u8282\u7684\u6574\u6570\u3002 \u5bf9\u4e8e\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b (\u4f8b\u5982 std::int_fast16_t \u548c std::int_least32_t ) \u548c size_t \u7c7b\u578b (\u53c2\u89c1\uff1a 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t ) \u5176\u5b9e\u4e5f\u6b63\u662f\u8fd9\u4e9b\u57fa\u7840\u7c7b\u578b\u7684\u522b\u540d\u7f62\u4e86\u3002 \u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728\u4f7f\u7528 std::cout \u6253\u5370 8 \u4f4d\u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u7684\u65f6\u5019\uff0c\u4f60\u591a\u534a\u4f1a\u6253\u5370\u51fa\u4e00\u4e2a\u5b57\u7b26\u7684\u539f\u56e0\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 #include // for fixed-width integers #include int main () { std :: int_least8_t x { 97 }; // int_least8_t \u5b9e\u9645\u4e0a\u662f char \u7684\u7c7b\u578b\u522b\u540d std :: cout << x ; return 0 ; } \u7a0b\u5e8f\u6253\u5370\u7ed3\u679c\u4e3a\uff1a 1 a \u56e0\u4e3a std::int_least8_t \u901a\u5e38\u88ab\u5b9a\u4e49\u4e3a char \u7684\u7c7b\u578b\u522b\u540d\uff0c\u53d8\u91cf x \u5c06\u88ab\u5b9a\u4e49\u4e3a char \u3002 \u800c char \u7c7b\u578b\u5c06\u5176\u503c\u6253\u5370\u4e3aASCII\u5b57\u7b26\uff0c\u800c\u4e0d\u662f\u6574\u6570\u503c\u3002","title":"\u5728\u5e73\u53f0\u65e0\u5173\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528\u7c7b\u578b\u522b\u540d"},{"location":"8-6-Typedefs-and-type-aliases/#_6","text":"\u867d\u7136\u6211\u4eec\u76ee\u524d\u53ea\u4f7f\u7528\u8fc7\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u662f\u5bf9\u4e8e\u9ad8\u9636C++\u7f16\u7a0b\u6765\u8bf4\uff0c\u7c7b\u578b\u901a\u5e38\u662f\u590d\u6742\u4e14\u5197\u957f\u7684\uff0c\u5982\u679c\u4f60\u624b\u52a8\u8f93\u5165\u5b83\u4eec\u4f1a\u975e\u5e38\u8d39\u52b2\u3002\u4f8b\u5982\uff0c\u6709\u4e9b\u51fd\u6570\u548c\u53d8\u91cf\u7684\u5b9a\u4e49\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include // for std::string #include // for std::vector #include // for std::pair bool hasDuplicates ( std :: vector < std :: pair < std :: string , int >> pairlist ) { // some code here return false ; } int main () { std :: vector < std :: pair < std :: string , int >> pairlist ; return 0 ; } \u5728\u6bcf\u4e2a\u9700\u8981\u7684\u5730\u65b9\u8f93\u5165 std::vector> \u4e0d\u4ec5\u7e41\u7410\uff0c\u800c\u4e14\u975e\u5e38\u5bb9\u6613\u51fa\u9519\u3002\u5982\u679c\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u5219\u7b80\u5355\u7684\u591a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include // for std::string #include // for std::vector #include // for std::pair using pairlist_t = std :: vector < std :: pair < std :: string , int >> ; // make pairlist_t an alias for this crazy type bool hasDuplicates ( pairlist_t pairlist ) // use pairlist_t in a function parameter { // some code here return false ; } int main () { pairlist_t pairlist ; // instantiate a pairlist_t variable return 0 ; } \u770b\u8d77\u6765\u597d\u591a\u4e86\u5bf9\u5427\uff01\u73b0\u5728\uff0c\u4f60\u9700\u8981\u8f93\u5165\u7684\u662f pairlist_t \u800c\u4e0d\u662f std::vector> \u3002 \u5982\u679c\u4f60\u8fd8\u4e0d\u77e5\u9053 std::vector \u3001 std::pair \u6216\u8005\u90a3\u4e9b\u770b\u8d77\u6765\u5f88\u5413\u4eba\u7684\u5c16\u62ec\u53f7\u662f\u4ec0\u4e48\uff0c\u4e5f\u4e0d\u7528\u62c5\u5fc3\u3002\u73b0\u5728\u4f60\u53ea\u9700\u8981\u77e5\u9053\u7c7b\u578b\u522b\u540d\u53ef\u4ee5\u7b80\u5316\u7c7b\u578b\u3001\u4f7f\u4f60\u7684\u4ee3\u7801\u66f4\u6613\u8bfb\uff0c\u51cf\u5c11\u7e41\u7410\u7684\u8f93\u5165\u5c31\u53ef\u4ee5\u4e86\u3002 \u8fd9\u4e5f\u662f\u7c7b\u578b\u522b\u540d\u6700\u6709\u7528\u7684\u5730\u65b9\u3002","title":"\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u7b80\u5316\u7c7b\u578b"},{"location":"8-6-Typedefs-and-type-aliases/#_7","text":"\u7c7b\u578b\u522b\u540d\u8fd8\u6709\u52a9\u4e8e\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8df5\u884c\u4ee3\u7801\u5373\u6587\u6863\u7684\u601d\u60f3\u3002 With variables, we have the variable\u2019s identifier to help document the purpose of the variable. But consider the case of a function\u2019s return value. Data types such as char , int , long , double , and bool are good for describing what type a function returns, but more often we want to know what purpose a return value serves. \u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7\u6807\u8bc6\u7b26\uff08\u53d8\u91cf\u540d\uff09\u6765\u5e2e\u52a9\u8868\u793a\u5b83\u7684\u7528\u9014\u3002\u4f46\u662f\u5bf9\u4e8e\u51fd\u6570\u8fd4\u56de\u503c\u6765\u8bf4\uff0c\u5c3d\u7ba1 char \u3001 int \u3001 long \u3001 double \u548c bool \u53ef\u4ee5\u8868\u660e\u51fd\u6570\u503c\u7684\u7c7b\u578b\uff0c\u4f46\u5b83\u4eec\u5e76\u6ca1\u6709\u529e\u6cd5\u5f88\u597d\u5730\u8868\u660e\u51fd\u6570\u8fd4\u56de\u503c\u7684\u7528\u9014\u7a76\u7adf\u662f\u4ec0\u4e48\u3002 \u5bf9\u4e8e\u4e0b\u9762\u7684\u51fd\u6570\u6765\u8bf4\uff1a 1 int gradeTest (); \u51fd\u6570\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u662f\u6574\u578b\uff0c\u4f46\u662f\u8fd9\u4e2a\u6574\u578b\u8fd4\u56de\u503c\u7684\u542b\u4e49\u5230\u5e95\u662f\u4ec0\u4e48\u5462\uff1f\u662f\u5b66\u751f\u7684\u5206\u6570\uff1f\u662f\u6ca1\u6709\u4f5c\u7b54\u7684\u95ee\u9898\u6570\uff1f\u662f\u5b66\u53f7\uff1f\u8fd8\u662f\u9519\u8bef\u7801\uff1f\u6ca1\u6709\u4eba\u77e5\u9053\uff0c\u56e0\u4e3a int \u5e76\u4e0d\u80fd\u8868\u8fbe\u8fd9\u5c42\u542b\u4e49\u3002\u5982\u679c\u5e78\u8fd0\u7684\u8bdd\uff0c\u4e5f\u8bb8\u51fd\u6570\u6709\u6587\u6863\u53ef\u4f9b\u6211\u4eec\u53c2\u8003\u3002\u5982\u679c\u4e0d\u5e78\u7684\u8bdd\uff0c\u4f60\u5c31\u5fc5\u987b\u9605\u8bfb\u6e90\u7801\u7136\u540e\u53bb\u731c\u6d4b\u5b83\u7684\u7528\u9014\u4e86\u3002 \u800c\u5bf9\u4e8e\u4e0b\u9762\u8fd9\u4e2a\u4f7f\u7528\u4e86\u7c7b\u578b\u522b\u540d\u7684\u7b49\u4ef7\u7684\u4f8b\u5b50\u6765\u8bf4\uff1a 1 2 using testScore_t = int ; testScore_t gradeTest (); \u8fd4\u56de\u503c\u7c7b\u578b\u4e3a testScore_t \uff0c\u5b83\u80fd\u591f\u66f4\u597d\u5730\u8868\u660e\u51fd\u6570\u7684\u8fd4\u56de\u503c\u8868\u793a\u4e00\u6b21\u6d4b\u9a8c\u7684\u5206\u6570\u3002 \u6839\u636e\u6211\u4eec\u7684\u7ecf\u9a8c\uff0c\u521b\u5efa\u7c7b\u578b\u522b\u540d\u53ea\u662f\u4e3a\u4e86\u8bb0\u5f55\u5355\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u662f\u4e0d\u503c\u5f97\u7684(\u4f7f\u7528\u6ce8\u91ca\u4ee3\u66ff)\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u5df2\u7ecf\u56e0\u4e3a\u5176\u4ed6\u539f\u56e0\u521b\u5efa\u4e86\u7c7b\u578b\u522b\u540d\uff0c\u8fd9\u53ef\u80fd\u662f\u4e00\u4e2a\u989d\u5916\u597d\u5904\u3002","title":"\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u8ba9\u7a0b\u5e8f\u66f4\u6e05\u6670"},{"location":"8-6-Typedefs-and-type-aliases/#_8","text":"\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u8fd8\u53ef\u4ee5\u5728\u4e0d\u8fdb\u884c\u5927\u91cf\u6539\u52a8\u7684\u60c5\u51b5\u4e0b\u4fee\u6539\u5bf9\u8c61\u7684\u57fa\u672c\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5b66\u751f\u7684ID\u539f\u672c\u4f7f\u7528 short \u6765\u4fdd\u5b58\uff0c\u4f46\u540e\u6765\u51b3\u5b9a\u7528 long \u6765\u4ee3\u66ff\uff0c\u6b64\u65f6\u4f60\u5c31\u5fc5\u987b\u5c06\u5927\u91cf\u7684 short \u66ff\u6362\u4e3a long \u3002\u8fd9\u4e2a\u65f6\u5019\u5f80\u5f80\u5f88\u96be\u786e\u5b9a\u7a76\u7adf\u54ea\u4e9b short \u662f\u7528\u6765\u4fdd\u5b58ID\u7684\uff0c\u800c\u54ea\u4e9b\u5219\u7528\u4e8e\u5176\u4ed6\u76ee\u7684\u3002 \u7136\u800c\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u4e86\u7c7b\u578b\u522b\u540d\uff0c\u90a3\u4e48\u66f4\u6539\u7c7b\u578b\u5c31\u50cf\u66f4\u65b0\u7c7b\u578b\u522b\u540d\u4e00\u6837\u7b80\u5355(\u4f8b\u5982\uff0c\u5c06 using studentID_t = short; \u4fee\u6539\u4e3a using studentID_t = long; )\u3002 \u867d\u7136\u8fd9\u770b\u8d77\u6765\u662f\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u7279\u6027\uff0c\u4f46\u6bcf\u5f53\u7c7b\u578b\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u6211\u4eec\u4ecd\u7136\u9700\u8981\u4fdd\u6301\u8c28\u614e\uff0c\u56e0\u4e3a\u7a0b\u5e8f\u7684\u884c\u4e3a\u4e5f\u53ef\u80fd\u968f\u4e4b\u53d1\u751f\u53d8\u5316\u3002\u5f53\u5c06\u7c7b\u578b\u522b\u540d\u7684\u7c7b\u578b\u66f4\u6539\u4e3a\u4e0d\u540c\u7c7b\u578b\u65cf\u4e2d\u7684\u7c7b\u578b\u65f6\u5c24\u5176\u5982\u6b64(\u4f8b\u5982\uff0c\u5c06\u6574\u6570\u66f4\u6539\u4e3a\u6d6e\u70b9\u503c\uff0c\u6216\u53cd\u4e4b\u4ea6\u7136)\uff01\u65b0\u7c7b\u578b\u53ef\u80fd\u6709\u6d6e\u70b9\u6570\u6bd4\u8f83\u6216\u6574\u6570/\u6d6e\u70b9\u9664\u6cd5\u95ee\u9898\uff0c\u6216\u5176\u4ed6\u65e7\u7c7b\u578b\u6240\u6ca1\u6709\u7684\u95ee\u9898\u3002\u5982\u679c\u4f60\u5c06\u73b0\u6709\u7684\u7c7b\u578b\u66f4\u6539\u4e3a\u5176\u4ed6\u7c7b\u578b\uff0c\u4ee3\u7801\u5e94\u8be5\u91cd\u65b0\u3001\u5f7b\u5e95\u5730\u6d4b\u8bd5\u3002","title":"\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u63d0\u9ad8\u4ee3\u7801\u53ef\u7ef4\u62a4\u6027"},{"location":"8-6-Typedefs-and-type-aliases/#_9","text":"\u7136\u7c7b\u578b\u522b\u540d\u63d0\u4f9b\u4e86\u4e00\u4e9b\u597d\u5904\uff0c\u4f46\u5b83\u4eec\u4e5f\u5728\u4ee3\u7801\u4e2d\u5f15\u5165\u4e86\u53e6\u4e00\u4e2a\u9700\u8981\u7406\u89e3\u7684\u6807\u8bc6\u7b26\u3002\u5982\u679c\u8fd9\u4e00\u70b9\u6ca1\u6709\u88ab\u53ef\u8bfb\u6027\u6216\u7406\u89e3\u7684\u597d\u5904\u6240\u62b5\u6d88\uff0c\u90a3\u4e48\u7c7b\u578b\u522b\u540d\u5f0a\u5927\u4e8e\u5229\u3002 \u7c7b\u578b\u522b\u540d\u4f7f\u7528\u4e0d\u5f53\u7684\u8bdd\uff0c\u4f1a\u5c06\u719f\u6089\u7684\u7c7b\u578b(\u4f8b\u5982 std::string )\u9690\u85cf\u5728\u81ea\u5b9a\u4e49\u540d\u79f0\u540e\u9762\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b(\u4f8b\u5982\u667a\u80fd\u6307\u9488\uff0c\u6211\u4eec\u5c06\u5728\u4ee5\u540e\u7684\u7ae0\u8282\u4e2d\u8ba8\u8bba)\uff0c\u6a21\u7cca\u7684\u7c7b\u578b\u4fe1\u606f\u4e5f\u53ef\u80fd\u4e0d\u5229\u4e8e\u7406\u89e3\u7c7b\u578b\u5e94\u8be5\u5982\u4f55\u5de5\u4f5c\u3002 \u56e0\u6b64\uff0c\u7c7b\u578b\u522b\u540d\u5e94\u8be5\u4e3b\u8981\u7528\u4e8e\u5bf9\u4ee3\u7801\u53ef\u8bfb\u6027\u6216\u4ee3\u7801\u7ef4\u62a4\u6709\u660e\u663e\u597d\u5904\u7684\u60c5\u51b5\u3002\u8fd9\u65e2\u662f\u4e00\u95e8\u79d1\u5b66\uff0c\u4e5f\u662f\u4e00\u95e8\u827a\u672f\u3002\u5f53\u7c7b\u578b\u522b\u540d\u53ef\u4ee5\u5728\u4ee3\u7801\u7684\u8bb8\u591a\u5730\u65b9\u4f7f\u7528\u800c\u4e0d\u662f\u5728\u5be5\u5be5\u51e0\u5904\u4f7f\u7528\u65f6\uff0c\u624d\u662f\u5b83\u4eec\u5927\u653e\u5f02\u5f69\u7684\u673a\u4f1a\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u7c7b\u578b\u522b\u540d\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\u3001\u4ee3\u7801\u7ef4\u62a4\u6027\u65f6\uff0c\u660e\u667a\u800c\u5ba1\u614e\u5730\u4f7f\u7528\u5b83\u4eec\u3002","title":"\u7f3a\u70b9\u548c\u7ed3\u8bba"},{"location":"8-7-Type-deduction-for-objects-using-the%20auto-keyword/","tags":["type deduction","data type","auto"],"text":"Key Takeaway \u7c7b\u578b\u63a8\u65ad\u5fc5\u987b\u914d\u5408\u53d8\u91cf\u521d\u59cb\u5316\u503c\u4f7f\u7528\uff0c\u8fd9\u6837\u53ef\u4ee5\u5fd8\u8bb0\u521d\u59cb\u5316\u7684\u53d8\u91cf \u7c7b\u578b\u63a8\u65ad\u4f1a\u4e22\u5f03 const \uff0c\u5fc5\u987b\u989d\u5916\u6dfb\u52a0 const auto \u5982\u679c\u5e0c\u671b\u5b57\u7b26\u4e32\u63a8\u65ad\u51fa\u7684\u7c7b\u578b\u4e3a std:: string \u6216 std:: string_view \uff0c\u4f60\u9700\u8981\u4f7f\u7528 s \u6216\u8005 sv \u5b57\u9762\u91cf\u540e\u7f00 \u5728\u4e0b\u9762\u8fd9\u4e2a\u7b80\u5355\u7684\u53d8\u91cf\u5b9a\u4e49\u4e2d\uff0c\u5b58\u5728\u4e00\u4e2a\u96be\u4ee5\u5bdf\u89c9\u7684\u5197\u4f59\u70b9\uff1a 1 double d { 5.0 }; \u56e0\u4e3a C++ \u5c5e\u4e8e\u5f3a\u7c7b\u578b\u8bed\u8a00\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u4e3a\u6bcf\u4e00\u4e2a\u5bf9\u8c61\u6307\u660e\u5176\u7c7b\u578b\u3002\u56e0\u6b64\uff0c\u8fd9\u91cc\u6211\u4eec\u663e\u5f0f\u5730\u6307\u660e\u4e86\u53d8\u91cf d \u7684\u7c7b\u578b\u4e3a double \u3002 \u4f46\u662f\uff0c\u65e2\u7136\u88ab\u7528\u6765\u521d\u59cb\u5316 d \u7684 \u5b57\u9762\u91cf 5.0 \u5df2\u7ecf\u662f double \u7c7b\u578b\u7684\u4e86\uff0c\u96be\u9053\u518d\u4e3a d \u6307\u5b9a double \u4e0d\u591a\u4e8e\u5417\uff1f \u76f8\u5173\u5185\u5bb9 \u5173\u4e8e\u5b57\u9762\u91cf\u7684\u7c7b\u578b\uff0c\u53c2\u89c1 4.15 - \u5b57\u9762\u91cf \u3002 \u5982\u679c\u6211\u4eec\u5e0c\u671b\u4e00\u4e2a\u53d8\u91cf\u53ca\u5176\u521d\u59cb\u5316\u5f0f\u5177\u6709\u76f8\u540c\u7684\u7c7b\u578b\uff0c\u90a3\u4e48\u4e24\u6b21\u63d0\u4f9b\u4e86\u76f8\u540c\u7684\u7c7b\u578b\u4fe1\u606f\u5176\u5b9e\u662f\u591a\u4f59\u7684\u3002 \u53d8\u91cf\u521d\u59cb\u5316\u7684\u7c7b\u578b\u8f6c\u6362 \u7c7b\u578b\u63a8\u65ad\u662f\u4e00\u79cd\u5141\u8bb8\u7f16\u8bd1\u5668\u901a\u8fc7\u5bf9\u8c61\u7684\u521d\u59cb\u5316\u503c\u63a8\u65ad\u5bf9\u8c61\u7c7b\u578b\u7684\u7279\u6027\u3002\u4f7f\u7528 auto \u5173\u952e\u5b57\u4ee3\u66ff\u53d8\u91cf\u7684\u7c7b\u578b\u5c31\u53ef\u4ee5\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u3002 1 2 3 4 5 6 7 8 int main () { auto d { 5.0 }; // 5.0 is a double literal, so d will be type double auto i { 1 + 2 }; // 1 + 2 evaluates to an int, so i will be type int auto x { i }; // i is an int, so x will be type int too return 0 ; } \u5728\u7b2c\u4e00\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a 5.0 \u662f double \u7c7b\u578b\u7684\u5b57\u9762\u91cf\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u53ef\u4ee5\u63a8\u65ad\u51fa\u53d8\u91cf d \u4e5f\u5e94\u8be5\u662f double \u7c7b\u578b\u7684\u3002\u5728\u7b2c\u4e8c\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8868\u8fbe\u5f0f 1 + 2 \u4f1a\u6c42\u503c\u5f97\u5230\u4e00\u4e2a int \u7c7b\u7684\u89e3\uff0c\u56e0\u6b64\u53d8\u91cf i \u662f int \u7c7b\u578b\u7684\u3002\u5728\u7b2c\u4e09\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a i \u7684\u7c7b\u578b\u5df2\u7ecf\u88ab\u63a8\u65ad\u4e3a int \u7c7b\u578b\uff0c\u56e0\u6b64\u8fd9\u91cc\u7684 x \u81ea\u7136\u4e5f\u4f1a\u88ab\u63a8\u65ad\u4e3a int \u7c7b\u578b\u3002 \u56e0\u4e3a\u51fd\u6570\u8c03\u7528\u4e5f\u662f\u4e00\u79cd\u5408\u6cd5\u7684\u8868\u8fbe\u5f0f\uff0c\u6240\u4ee5\u51fd\u6570\u4f5c\u4e3a\u521d\u59cb\u5316\u503c\u65f6\uff0c\u4e5f\u53ef\u4ee5\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\uff1a 1 2 3 4 5 6 7 8 9 10 int add ( int x , int y ) { return x + y ; } int main () { auto sum { add ( 5 , 6 ) }; // add() returns an int, so sum's type will be deduced to int return 0 ; } add() \u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e3a int \uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u5c06\u53d8\u91cf sum \u7684\u7c7b\u578b\u63a8\u65ad\u4e3a int \u3002 \u5982\u679c\u5bf9\u8c61\u6ca1\u6709\u521d\u59cb\u5316\u503c\u6216\u4f7f\u7528\u4e86\u7a7a\u7684\u521d\u59cb\u5316\u503c\uff0c\u5219\u65e0\u6cd5\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\u3002\u4e0b\u9762\u7684\u4e24\u4e2a\u53d8\u91cf\u5b9a\u4e49\u662f\u975e\u6cd5\u7684\uff1a 1 2 3 4 5 6 7 int main () { auto x ; // The compiler is unable to deduce the type of x auto y { }; // The compiler is unable to deduce the type of y return 0 ; } \u867d\u7136\u5bf9\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u53ea\u80fd\u5c11\u6253\u51e0\u4e2a\u5b57\uff0c\u4f46\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4f1a\u770b\u5230\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7c7b\u578b\u53ef\u80fd\u4f1a\u53d8\u5f97\u590d\u6742\u548c\u5197\u957f\uff08\u6216\u8005\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u7c7b\u578b\u4f1a\u53d8\u5f97\u96be\u4ee5\u786e\u5b9a\uff09\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528 auto \u53ef\u4ee5\u8282\u7701\u5927\u91cf\u7684\u8f93\u5165(\u548c\u9519\u522b\u5b57)\u3002 \u76f8\u5173\u5185\u5bb9 \u6307\u9488\u548c\u5f15\u7528\u7684\u7c7b\u578b\u63a8\u65ad\u6709\u4e9b\u590d\u6742\uff0c\u6211\u4eec\u4f1a\u5728 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548c const \u7684\u7c7b\u578b\u63a8\u65ad \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u7c7b\u578b\u63a8\u65ad\u4f1a\u4e22\u5f03 const \u4fee\u9970\u7b26 \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7c7b\u578b\u63a8\u65ad\u4f1a\u4e22\u5f03 const \u4fee\u9970\u7b26\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 int main () { const int x { 5 }; // x has type const int auto y { x }; // y will be type int (const is dropped) } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c x \u7684\u7c7b\u578b\u672c\u6765\u662f const int \uff0c\u4f46\u662f\u5f53\u4f7f\u7528 x \u4f5c\u4e3a y \u7684\u521d\u59cb\u5316\u503c\u662f\uff0c\u7c7b\u578b\u63a8\u65ad\u5f97\u5230\u7684\u7ed3\u679c\u662f int \u800c\u4e0d\u662f int const \u3002 \u5982\u679c\u4f60\u60f3\u8981\u4e00\u4e2a\u63a8\u65ad\u7684\u7c7b\u578b\u662f const \uff0c\u4f60\u5fc5\u987b\u81ea\u5df1\u63d0\u4f9b const \u3002\u4e3a\u6b64\uff0c\u53ea\u9700\u5c06\u5173\u952e\u5b57 const \u4e0e auto \u7ed3\u5408\u4f7f\u7528\u5373\u53ef\uff1a 1 2 3 4 5 6 7 int main () { const int x { 5 }; // x has type const int auto y { x }; // y will be type int (const is dropped) const auto z { x }; // z will be type const int (const is reapplied) } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528 x \u4f5c\u4e3a\u521d\u59cb\u5316\u503c\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\u7684\u7ed3\u679c\u662f int ( const \u88ab\u4e22\u5f03\u4e86)\uff0c\u4f46\u662f\u56e0\u4e3a\u6211\u4eec\u5728\u5b9a\u4e49\u53d8\u91cf z \u65f6\u91cd\u65b0\u6dfb\u52a0\u4e86 const \u4fee\u9970\u7b26\uff0c\u6240\u4ee5 z \u7684\u7c7b\u578b\u4e3a const int \u3002 string \u5b57\u9762\u91cf\u7684\u7c7b\u578b\u63a8\u65ad \u51fa\u4e8e\u67d0\u4e9b\u5386\u53f2\u539f\u56e0\uff0c\u5b57\u7b26\u4e32\u5728 C++ \u4e2d\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u7c7b\u578b\u3002\u56e0\u6b64\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u80fd\u5e76\u4e0d\u80fd\u5982\u4f60\u6240\u613f\u5730\u5de5\u4f5c\uff1a 1 auto s { \"Hello, world\" }; // s will be type const char*, not std::string \u5982\u679c\u4f60\u5e0c\u671b\u63a8\u65ad\u51fa\u7684\u7c7b\u578b\u4e3a std:: string \u6216 std:: string_view \uff0c\u4f60\u9700\u8981\u4f7f\u7528 s \u6216\u8005 sv \u5b57\u9762\u91cf\u540e\u7f00 (\u53c2\u8003\uff1a 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf ): 1 2 3 4 5 6 7 8 9 10 11 12 #include #include int main () { using namespace std :: literals ; // easiest way to access the s and sv suffixes auto s1 { \"goo\" s }; // \"goo\"s is a std::string literal, so s1 will be deduced as a std::string auto s2 { \"moo\" sv }; // \"moo\"sv is a std::string_view literal, so s2 will be deduced as a std::string_view return 0 ; } \u7c7b\u578b\u63a8\u65ad\u7684\u4f18\u7f3a\u70b9 \u7c7b\u578b\u63a8\u65ad\u4e0d\u4ec5\u65b9\u4fbf\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u6709\u7528\u7684\u5730\u65b9\u3002 \u9996\u5148\uff0c\u5728\u8fde\u7eed\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\u7684\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u53ef\u4ee5\u8ba9\u53d8\u91cf\u540d\u5bf9\u9f50\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff1a 1 2 3 4 5 6 7 // harder to read int a { 5 }; double b { 6.7 }; // easier to read auto c { 5 }; auto d { 6.7 }; \u5176\u6b21\uff0c\u7c7b\u578b\u63a8\u65ad\u53ea\u80fd\u591f\u5728\u5177\u6709\u53d8\u91cf\u521d\u59cb\u5316\u503c\u662f\u624d\u80fd\u4f7f\u7528\uff0c\u5982\u679c\u6211\u4eec\u517b\u6210\u4e86\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u7684\u4e60\u60ef\u7684\u8bdd\uff0c\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u907f\u514d\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\uff1a 1 2 int x ; // oops, we forgot to initialize x, but the compiler may not complain auto y ; // the compiler will error out because it can't deduce a type for y \u7b2c\u4e09\uff0c\u901a\u8fc7\u7c7b\u578b\u63a8\u65ad\uff0c\u53ef\u4ee5\u786e\u4fdd\u4e0d\u53d1\u751f\u5bf9\u6027\u80fd\u4ea7\u751f\u5f71\u54cd\u7684\u7c7b\u578b\u8f6c\u6362\uff1a 1 2 double x { 5 }; // \u4e0d\u597d: int \u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a double auto y { 5 }; // \u597d\uff1ay \u662f int \u5e76\u4e0d\u4f1a\u53d1\u751f\u7c7b\u578b\u8f6c\u6362 \u5f53\u7136\uff0c\u7c7b\u578b\u63a8\u65ad\u4e5f\u6709\u4e00\u4e9b\u7f3a\u70b9\u3002 \u9996\u5148\uff0c\u7c7b\u578b\u63a8\u65ad\u6a21\u7cca\u5316\u4e86\u4ee3\u7801\u4e2d\u7684\u7c7b\u578b\u4fe1\u606f\u3002\u5c3d\u7ba1\u4f18\u79c0\u7684 IDE \u53ef\u4ee5\u663e\u793a\u63a8\u65ad\u540e\u7684\u7c7b\u578b\uff08\u4f8b\u5982\uff0c\u5f53\u9f20\u6807\u60ac\u6d6e\u5728\u53d8\u91cf\u4e0a\u65f6\uff09\uff0c\u4f46\u662f\u5728\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u65f6\u66f4\u5bb9\u6613\u53d1\u751f\u4e0e\u7c7b\u578b\u76f8\u5173\u7684\u9519\u8bef\u3002 \u4f8b\u5982\uff1a 1 auto y { 5 }; // oops, we wanted a double here but we accidentally provided an int literal \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u5982\u679c\u6211\u4eec\u663e\u5f0f\u5730\u5c06 y \u6307\u5b9a\u4e3a double \u7c7b\u578b\uff0c\u90a3\u4e48\u5373\u4f7f\u6211\u4eec\u610f\u5916\u5730\u4e3a y \u63d0\u4f9b\u4e86\u4e00\u4e2a int \u5b57\u9762\u91cf\u7684\u521d\u59cb\u5316\u503c\uff0c y \u4ecd\u7136\u8fd8\u662f double \u7c7b\u578b\uff0c\u800c\u5982\u679c\u4f7f\u7528\u7c7b\u578b\u63a8\u6f14\uff0c y \u5c06\u88ab\u63a8\u6f14\u4e3a int \u7c7b\u578b\u3002 \u518d\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { auto x { 3 }; auto y { 2 }; std :: cout << x / y ; // oops, we wanted floating point division here return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u5c31\u4e0d\u5bb9\u6613\u770b\u51fa\u4e0b\u9762\u8fdb\u884c\u7684\u662f\u6574\u6570\u9664\u6cd5\u800c\u4e0d\u662f\u6d6e\u70b9\u9664\u6cd5\u3002 \u5176\u6b21\uff0c\u5982\u679c\u521d\u59cb\u5316\u5f0f\u7684\u7c7b\u578b\u6539\u53d8\uff0c\u4f7f\u7528\u7c7b\u578b\u6f14\u7ece\u7684\u53d8\u91cf\u7684\u7c7b\u578b\u4e5f\u4f1a\u6539\u53d8\uff0c\u6b64\u65f6\u53ef\u80fd\u4f1a\u53d1\u751f\u610f\u5916\u7684\u95ee\u9898\u3002\u4f8b\u5982\uff1a 1 auto sum { add ( 5 , 6 ) + gravity }; \u5982\u679c add \u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4ece int \u88ab\u4fee\u6539\u4e3a double \u4e86\uff0c\u6216\u8005 gravity \u7684\u7c7b\u578b\u4ece int \u88ab\u4fee\u6539\u4e3a double \u4e86\uff0c\u90a3\u4e48 sum \u7684\u7c7b\u578b\u540c\u6837\u4f1a\u88ab\u91cd\u65b0\u63a8\u65ad\u4e3a double \u3002 \u603b\u7684\u6765\u8bf4\uff0c\u73b0\u4ee3 C++ \u7684\u5171\u8bc6\u662f\uff0c\u7c7b\u578b\u6f14\u7ece\u7528\u4e8e\u5bf9\u8c61\u901a\u5e38\u662f\u5b89\u5168\u7684\uff0c\u8fd9\u6837\u505a\u53ef\u4ee5\u51cf\u5c11\u5bf9\u7c7b\u578b\u4fe1\u606f\u7684\u5f3a\u8c03\uff0c\u4ece\u800c\u66f4\u597d\u5730\u51f8\u663e\u4ee3\u7801\u7684\u903b\u8f91\uff0c\u4f9d\u6b21\u6765\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u53d8\u91cf\u4f7f\u7528\u7c7b\u578b\u6f14\u7ece\uff0c\u9664\u975e\u9700\u8981\u786e\u4fdd\u4f7f\u7528\u7279\u5b9a\u7684\u7c7b\u578b\u3002 \u4f5c\u8005\u6ce8 \u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\uff0c\u5f53\u6211\u4eec\u8ba4\u4e3a\u663e\u793a\u7c7b\u578b\u4fe1\u606f\u6709\u52a9\u4e8e\u7406\u89e3\u6982\u5ff5\u6216\u793a\u4f8b\u65f6\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u4f7f\u7528\u663e\u5f0f\u7c7b\u578b\uff0c\u800c\u4e0d\u662f\u7c7b\u578b\u63a8\u65ad\u3002","title":"8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad"},{"location":"8-7-Type-deduction-for-objects-using-the%20auto-keyword/#_1","text":"\u7c7b\u578b\u63a8\u65ad\u662f\u4e00\u79cd\u5141\u8bb8\u7f16\u8bd1\u5668\u901a\u8fc7\u5bf9\u8c61\u7684\u521d\u59cb\u5316\u503c\u63a8\u65ad\u5bf9\u8c61\u7c7b\u578b\u7684\u7279\u6027\u3002\u4f7f\u7528 auto \u5173\u952e\u5b57\u4ee3\u66ff\u53d8\u91cf\u7684\u7c7b\u578b\u5c31\u53ef\u4ee5\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u3002 1 2 3 4 5 6 7 8 int main () { auto d { 5.0 }; // 5.0 is a double literal, so d will be type double auto i { 1 + 2 }; // 1 + 2 evaluates to an int, so i will be type int auto x { i }; // i is an int, so x will be type int too return 0 ; } \u5728\u7b2c\u4e00\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a 5.0 \u662f double \u7c7b\u578b\u7684\u5b57\u9762\u91cf\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u53ef\u4ee5\u63a8\u65ad\u51fa\u53d8\u91cf d \u4e5f\u5e94\u8be5\u662f double \u7c7b\u578b\u7684\u3002\u5728\u7b2c\u4e8c\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8868\u8fbe\u5f0f 1 + 2 \u4f1a\u6c42\u503c\u5f97\u5230\u4e00\u4e2a int \u7c7b\u7684\u89e3\uff0c\u56e0\u6b64\u53d8\u91cf i \u662f int \u7c7b\u578b\u7684\u3002\u5728\u7b2c\u4e09\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a i \u7684\u7c7b\u578b\u5df2\u7ecf\u88ab\u63a8\u65ad\u4e3a int \u7c7b\u578b\uff0c\u56e0\u6b64\u8fd9\u91cc\u7684 x \u81ea\u7136\u4e5f\u4f1a\u88ab\u63a8\u65ad\u4e3a int \u7c7b\u578b\u3002 \u56e0\u4e3a\u51fd\u6570\u8c03\u7528\u4e5f\u662f\u4e00\u79cd\u5408\u6cd5\u7684\u8868\u8fbe\u5f0f\uff0c\u6240\u4ee5\u51fd\u6570\u4f5c\u4e3a\u521d\u59cb\u5316\u503c\u65f6\uff0c\u4e5f\u53ef\u4ee5\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\uff1a 1 2 3 4 5 6 7 8 9 10 int add ( int x , int y ) { return x + y ; } int main () { auto sum { add ( 5 , 6 ) }; // add() returns an int, so sum's type will be deduced to int return 0 ; } add() \u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e3a int \uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u5c06\u53d8\u91cf sum \u7684\u7c7b\u578b\u63a8\u65ad\u4e3a int \u3002 \u5982\u679c\u5bf9\u8c61\u6ca1\u6709\u521d\u59cb\u5316\u503c\u6216\u4f7f\u7528\u4e86\u7a7a\u7684\u521d\u59cb\u5316\u503c\uff0c\u5219\u65e0\u6cd5\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\u3002\u4e0b\u9762\u7684\u4e24\u4e2a\u53d8\u91cf\u5b9a\u4e49\u662f\u975e\u6cd5\u7684\uff1a 1 2 3 4 5 6 7 int main () { auto x ; // The compiler is unable to deduce the type of x auto y { }; // The compiler is unable to deduce the type of y return 0 ; } \u867d\u7136\u5bf9\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u53ea\u80fd\u5c11\u6253\u51e0\u4e2a\u5b57\uff0c\u4f46\u5728\u540e\u7eed\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4f1a\u770b\u5230\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7c7b\u578b\u53ef\u80fd\u4f1a\u53d8\u5f97\u590d\u6742\u548c\u5197\u957f\uff08\u6216\u8005\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u7c7b\u578b\u4f1a\u53d8\u5f97\u96be\u4ee5\u786e\u5b9a\uff09\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528 auto \u53ef\u4ee5\u8282\u7701\u5927\u91cf\u7684\u8f93\u5165(\u548c\u9519\u522b\u5b57)\u3002 \u76f8\u5173\u5185\u5bb9 \u6307\u9488\u548c\u5f15\u7528\u7684\u7c7b\u578b\u63a8\u65ad\u6709\u4e9b\u590d\u6742\uff0c\u6211\u4eec\u4f1a\u5728 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548c const \u7684\u7c7b\u578b\u63a8\u65ad \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"\u53d8\u91cf\u521d\u59cb\u5316\u7684\u7c7b\u578b\u8f6c\u6362"},{"location":"8-7-Type-deduction-for-objects-using-the%20auto-keyword/#const","text":"\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7c7b\u578b\u63a8\u65ad\u4f1a\u4e22\u5f03 const \u4fee\u9970\u7b26\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 int main () { const int x { 5 }; // x has type const int auto y { x }; // y will be type int (const is dropped) } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c x \u7684\u7c7b\u578b\u672c\u6765\u662f const int \uff0c\u4f46\u662f\u5f53\u4f7f\u7528 x \u4f5c\u4e3a y \u7684\u521d\u59cb\u5316\u503c\u662f\uff0c\u7c7b\u578b\u63a8\u65ad\u5f97\u5230\u7684\u7ed3\u679c\u662f int \u800c\u4e0d\u662f int const \u3002 \u5982\u679c\u4f60\u60f3\u8981\u4e00\u4e2a\u63a8\u65ad\u7684\u7c7b\u578b\u662f const \uff0c\u4f60\u5fc5\u987b\u81ea\u5df1\u63d0\u4f9b const \u3002\u4e3a\u6b64\uff0c\u53ea\u9700\u5c06\u5173\u952e\u5b57 const \u4e0e auto \u7ed3\u5408\u4f7f\u7528\u5373\u53ef\uff1a 1 2 3 4 5 6 7 int main () { const int x { 5 }; // x has type const int auto y { x }; // y will be type int (const is dropped) const auto z { x }; // z will be type const int (const is reapplied) } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528 x \u4f5c\u4e3a\u521d\u59cb\u5316\u503c\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\u7684\u7ed3\u679c\u662f int ( const \u88ab\u4e22\u5f03\u4e86)\uff0c\u4f46\u662f\u56e0\u4e3a\u6211\u4eec\u5728\u5b9a\u4e49\u53d8\u91cf z \u65f6\u91cd\u65b0\u6dfb\u52a0\u4e86 const \u4fee\u9970\u7b26\uff0c\u6240\u4ee5 z \u7684\u7c7b\u578b\u4e3a const int \u3002","title":"\u7c7b\u578b\u63a8\u65ad\u4f1a\u4e22\u5f03 const \u4fee\u9970\u7b26"},{"location":"8-7-Type-deduction-for-objects-using-the%20auto-keyword/#string","text":"\u51fa\u4e8e\u67d0\u4e9b\u5386\u53f2\u539f\u56e0\uff0c\u5b57\u7b26\u4e32\u5728 C++ \u4e2d\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u7c7b\u578b\u3002\u56e0\u6b64\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u80fd\u5e76\u4e0d\u80fd\u5982\u4f60\u6240\u613f\u5730\u5de5\u4f5c\uff1a 1 auto s { \"Hello, world\" }; // s will be type const char*, not std::string \u5982\u679c\u4f60\u5e0c\u671b\u63a8\u65ad\u51fa\u7684\u7c7b\u578b\u4e3a std:: string \u6216 std:: string_view \uff0c\u4f60\u9700\u8981\u4f7f\u7528 s \u6216\u8005 sv \u5b57\u9762\u91cf\u540e\u7f00 (\u53c2\u8003\uff1a 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf ): 1 2 3 4 5 6 7 8 9 10 11 12 #include #include int main () { using namespace std :: literals ; // easiest way to access the s and sv suffixes auto s1 { \"goo\" s }; // \"goo\"s is a std::string literal, so s1 will be deduced as a std::string auto s2 { \"moo\" sv }; // \"moo\"sv is a std::string_view literal, so s2 will be deduced as a std::string_view return 0 ; }","title":"string\u5b57\u9762\u91cf\u7684\u7c7b\u578b\u63a8\u65ad"},{"location":"8-7-Type-deduction-for-objects-using-the%20auto-keyword/#_2","text":"\u7c7b\u578b\u63a8\u65ad\u4e0d\u4ec5\u65b9\u4fbf\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u6709\u7528\u7684\u5730\u65b9\u3002 \u9996\u5148\uff0c\u5728\u8fde\u7eed\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\u7684\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u53ef\u4ee5\u8ba9\u53d8\u91cf\u540d\u5bf9\u9f50\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff1a 1 2 3 4 5 6 7 // harder to read int a { 5 }; double b { 6.7 }; // easier to read auto c { 5 }; auto d { 6.7 }; \u5176\u6b21\uff0c\u7c7b\u578b\u63a8\u65ad\u53ea\u80fd\u591f\u5728\u5177\u6709\u53d8\u91cf\u521d\u59cb\u5316\u503c\u662f\u624d\u80fd\u4f7f\u7528\uff0c\u5982\u679c\u6211\u4eec\u517b\u6210\u4e86\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u7684\u4e60\u60ef\u7684\u8bdd\uff0c\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u907f\u514d\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\uff1a 1 2 int x ; // oops, we forgot to initialize x, but the compiler may not complain auto y ; // the compiler will error out because it can't deduce a type for y \u7b2c\u4e09\uff0c\u901a\u8fc7\u7c7b\u578b\u63a8\u65ad\uff0c\u53ef\u4ee5\u786e\u4fdd\u4e0d\u53d1\u751f\u5bf9\u6027\u80fd\u4ea7\u751f\u5f71\u54cd\u7684\u7c7b\u578b\u8f6c\u6362\uff1a 1 2 double x { 5 }; // \u4e0d\u597d: int \u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a double auto y { 5 }; // \u597d\uff1ay \u662f int \u5e76\u4e0d\u4f1a\u53d1\u751f\u7c7b\u578b\u8f6c\u6362 \u5f53\u7136\uff0c\u7c7b\u578b\u63a8\u65ad\u4e5f\u6709\u4e00\u4e9b\u7f3a\u70b9\u3002 \u9996\u5148\uff0c\u7c7b\u578b\u63a8\u65ad\u6a21\u7cca\u5316\u4e86\u4ee3\u7801\u4e2d\u7684\u7c7b\u578b\u4fe1\u606f\u3002\u5c3d\u7ba1\u4f18\u79c0\u7684 IDE \u53ef\u4ee5\u663e\u793a\u63a8\u65ad\u540e\u7684\u7c7b\u578b\uff08\u4f8b\u5982\uff0c\u5f53\u9f20\u6807\u60ac\u6d6e\u5728\u53d8\u91cf\u4e0a\u65f6\uff09\uff0c\u4f46\u662f\u5728\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u65f6\u66f4\u5bb9\u6613\u53d1\u751f\u4e0e\u7c7b\u578b\u76f8\u5173\u7684\u9519\u8bef\u3002 \u4f8b\u5982\uff1a 1 auto y { 5 }; // oops, we wanted a double here but we accidentally provided an int literal \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u5982\u679c\u6211\u4eec\u663e\u5f0f\u5730\u5c06 y \u6307\u5b9a\u4e3a double \u7c7b\u578b\uff0c\u90a3\u4e48\u5373\u4f7f\u6211\u4eec\u610f\u5916\u5730\u4e3a y \u63d0\u4f9b\u4e86\u4e00\u4e2a int \u5b57\u9762\u91cf\u7684\u521d\u59cb\u5316\u503c\uff0c y \u4ecd\u7136\u8fd8\u662f double \u7c7b\u578b\uff0c\u800c\u5982\u679c\u4f7f\u7528\u7c7b\u578b\u63a8\u6f14\uff0c y \u5c06\u88ab\u63a8\u6f14\u4e3a int \u7c7b\u578b\u3002 \u518d\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include int main () { auto x { 3 }; auto y { 2 }; std :: cout << x / y ; // oops, we wanted floating point division here return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u5c31\u4e0d\u5bb9\u6613\u770b\u51fa\u4e0b\u9762\u8fdb\u884c\u7684\u662f\u6574\u6570\u9664\u6cd5\u800c\u4e0d\u662f\u6d6e\u70b9\u9664\u6cd5\u3002 \u5176\u6b21\uff0c\u5982\u679c\u521d\u59cb\u5316\u5f0f\u7684\u7c7b\u578b\u6539\u53d8\uff0c\u4f7f\u7528\u7c7b\u578b\u6f14\u7ece\u7684\u53d8\u91cf\u7684\u7c7b\u578b\u4e5f\u4f1a\u6539\u53d8\uff0c\u6b64\u65f6\u53ef\u80fd\u4f1a\u53d1\u751f\u610f\u5916\u7684\u95ee\u9898\u3002\u4f8b\u5982\uff1a 1 auto sum { add ( 5 , 6 ) + gravity }; \u5982\u679c add \u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4ece int \u88ab\u4fee\u6539\u4e3a double \u4e86\uff0c\u6216\u8005 gravity \u7684\u7c7b\u578b\u4ece int \u88ab\u4fee\u6539\u4e3a double \u4e86\uff0c\u90a3\u4e48 sum \u7684\u7c7b\u578b\u540c\u6837\u4f1a\u88ab\u91cd\u65b0\u63a8\u65ad\u4e3a double \u3002 \u603b\u7684\u6765\u8bf4\uff0c\u73b0\u4ee3 C++ \u7684\u5171\u8bc6\u662f\uff0c\u7c7b\u578b\u6f14\u7ece\u7528\u4e8e\u5bf9\u8c61\u901a\u5e38\u662f\u5b89\u5168\u7684\uff0c\u8fd9\u6837\u505a\u53ef\u4ee5\u51cf\u5c11\u5bf9\u7c7b\u578b\u4fe1\u606f\u7684\u5f3a\u8c03\uff0c\u4ece\u800c\u66f4\u597d\u5730\u51f8\u663e\u4ee3\u7801\u7684\u903b\u8f91\uff0c\u4f9d\u6b21\u6765\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u53d8\u91cf\u4f7f\u7528\u7c7b\u578b\u6f14\u7ece\uff0c\u9664\u975e\u9700\u8981\u786e\u4fdd\u4f7f\u7528\u7279\u5b9a\u7684\u7c7b\u578b\u3002 \u4f5c\u8005\u6ce8 \u5728\u4ee5\u540e\u7684\u8bfe\u7a0b\u4e2d\uff0c\u5f53\u6211\u4eec\u8ba4\u4e3a\u663e\u793a\u7c7b\u578b\u4fe1\u606f\u6709\u52a9\u4e8e\u7406\u89e3\u6982\u5ff5\u6216\u793a\u4f8b\u65f6\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u4f7f\u7528\u663e\u5f0f\u7c7b\u578b\uff0c\u800c\u4e0d\u662f\u7c7b\u578b\u63a8\u65ad\u3002","title":"\u7c7b\u578b\u63a8\u65ad\u7684\u4f18\u7f3a\u70b9"},{"location":"8-8-Type-deduction-for-functions/","tags":["type deduction","C++14","C++20"],"text":"Key Takeaway \u5728 C++14 \u4e2d\uff0c auto \u53ef\u4ee5\u88ab\u7528\u4e8e\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u6b64\u65f6\u9700\u8981\u4fdd\u8bc1 \u6240\u6709\u8fd4\u56de\u503c\u7c7b\u578b\u662f\u4e00\u81f4\u7684 \uff0c\u5426\u5219\u9700\u8981\u8fdb\u884c\u8f6c\u6362 \u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u4f7f\u7528 auto \u7684\u6700\u5927\u7f3a\u70b9\u8fd9\u4e9b\u51fd\u6570\u5728\u4f7f\u7528\u524d\u5fc5\u987b\u5df2\u7ecf\u5b8c\u6574\u5b9a\u4e49\uff08\u53ea\u6709 \u524d\u5411\u58f0\u660e \uff09\u662f\u4e0d\u591f\u7684\u3002\u8fd9\u4e5f\u610f\u5473\u7740 auto \u7c7b\u578b\u8fd4\u56de\u503c\u7684\u51fd\u6570\u901a\u5e38\u53ea\u80fd\u591f\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u6587\u4ef6\u4e2d\u4f7f\u7528\u3002 auto add(int x, int y) -> int \u540e\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\u3002\u8fd9\u79cd\u5f62\u5f0f\u53ef\u4ee5\u5bf9\u9f50\u51fd\u6570\u540d\u6216\u662f\u914d\u5408 lambda \u4f7f\u7528 \u51fd\u6570\u5f62\u53c2\u7684\u7c7b\u578b\u4e0d\u80fd\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 int add ( int x , int y ) { return x + y ; } \u4f46\u7f16\u8bd1\u8fd9\u4e2a\u51fd\u6570\u7684\u65f6\u5019\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u786e\u5b9a x + y \u7684\u6c42\u503c\u7ed3\u679c\u4e3a int \u7c7b\u578b\uff0c\u7136\u540e\u5b83\u4f1a\u786e\u4fdd\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u548c\u8be5\u7c7b\u578b\u5339\u914d\uff08\u6216\u8005\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a\u51fd\u6570\u58f0\u660e\u7684\u8fd4\u56de\u7c7b\u578b\uff09\u3002 \u56e0\u4e3a\u7f16\u8bd1\u5668\u5df2\u7ecf\u80fd\u591f\u4ece return \u8bed\u53e5\u63a8\u65ad\u8fd4\u56de\u503c\u7684\u7c7b\u578b\uff0c\u6240\u4ee5\u5728 C++14 \u4e2d\uff0c auto \u53ef\u4ee5\u88ab\u7528\u4e8e\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u5c06 auto \u5173\u952e\u5b57\u653e\u7f6e\u5728\u539f\u6765\u7528\u4e8e\u58f0\u660e\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u7684\u5730\u65b9\u5c31\u53ef\u4ee5\u3002 \u4f8b\u5982\uff1a 1 2 3 4 auto add ( int x , int y ) { return x + y ; } \u7531\u4e8e return \u8bed\u53e5\u8fd4\u56de\u7684\u662f\u4e00\u4e2a int \u7c7b\u578b\u7684\u503c\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u5c06\u51fd\u6570\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u63a8\u65ad\u4e3a int \u3002 \u5f53\u4f7f\u7528 auto \u8fd4\u56de\u503c\u7c7b\u578b\u662f\uff0c\u6240\u6709\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u5fc5\u987b\u662f\u4e00\u81f4\u7684\uff0c\u5426\u5219\u4f1a\u9020\u6210\u9519\u8bef\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 auto someFcn ( bool b ) { if ( b ) return 5 ; // return type int else return 6.7 ; // return type double } \u5728\u4e0a\u9762\u7684\u51fd\u6570\u4e2d\uff0c\u7531\u4e8e\u4e24\u4e2a return \u8bed\u53e5\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u4e0d\u540c\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u4f1a\u63d0\u793a\u9519\u8bef\u3002 \u5982\u679c\u6709\u4e9b\u60c5\u51b5\u4e0b\u9700\u8981\u4f7f\u7528\u4e0d\u540c\u7684\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u90a3\u4e48\u8981\u4e48\u4f60\u4e3a\u51fd\u6570\u663e\u5f0f\u5730\u6307\u660e\u8fd4\u56de\u503c\u7c7b\u578b\uff08\u6b64\u65f6\u7f16\u8bd1\u5668\u4f1a\u5c06\u4e0d\u5339\u914d\u7684\u8fd4\u56de\u503c\u8fdb\u884c \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \uff09\uff0c\u6216\u8005\u4f60\u53ef\u4ee5\u663e\u5f0f\u5730\u5c06 return \u8bed\u53e5\u7684\u8fd4\u56de\u503c\u8f6c\u6362\u6210\u76f8\u540c\u7684\u7c7b\u578b\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06 5 \u4fee\u6539\u4e3a 5.0 \uff0c\u6216\u8005\u5bf9\u4e8e\u975e\u5b57\u9762\u91cf\u7684\u60c5\u51b5\u8fd8\u53ef\u4ee5\u4f7f\u7528 static_cast \u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u4f7f\u7528 auto \u7684\u6700\u5927\u7f3a\u70b9\u8fd9\u4e9b\u51fd\u6570\u5728\u4f7f\u7528\u524d\u5fc5\u987b\u5df2\u7ecf\u5b8c\u6574\u5b9a\u4e49\uff08\u53ea\u6709 \u524d\u5411\u58f0\u660e \uff09\u662f\u4e0d\u591f\u7684\u3002\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include auto foo (); int main () { std :: cout << foo (); // the compiler has only seen a forward declaration at this point return 0 ; } auto foo () { return 5 ; } \u5728\u4f5c\u8005\u7684\u7535\u8111\u4e0a\u4f1a\u4ea7\u751f\u5982\u4e0b\u7f16\u8bd1\u9519\u8bef\uff1a 1 error C3779: 'foo': a function that returns 'auto' cannot be used before it is defined. \u8fd9\u662f\u7406\u6240\u5f53\u7136\u7684\uff0c\u56e0\u4e3a\u4ec5\u51ed\u524d\u5411\u58f0\u660e\u7f16\u8bd1\u5668\u65e0\u6cd5\u5bf9\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u8fdb\u884c\u63a8\u65ad\u3002\u8fd9\u4e5f\u610f\u5473\u7740 auto \u7c7b\u578b\u8fd4\u56de\u503c\u7684\u51fd\u6570\u901a\u5e38\u53ea\u80fd\u591f\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u6587\u4ef6\u4e2d\u4f7f\u7528\u3002 \u4e0e\u5bf9\u8c61\u7684\u7c7b\u578b\u63a8\u65ad\u4e0d\u540c\uff0c\u5bf9\u4e8e\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u63a8\u65ad\u7684\u6700\u4f73\u5b9e\u8df5\u5e76\u6ca1\u6709\u592a\u591a\u5171\u8bc6\u3002\u5f53\u5bf9\u5bf9\u8c61\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u65f6\uff0c\u521d\u59cb\u5316\u5f0f\u603b\u662f\u4f5c\u4e3a\u540c\u4e00\u8bed\u53e5\u7684\u4e00\u90e8\u5206\u51fa\u73b0\uff0c\u56e0\u6b64\u786e\u5b9a\u8981\u63a8\u65ad\u7684\u7c7b\u578b\u901a\u5e38\u4e0d\u4f1a\u592a\u9ebb\u70e6\u3002\u5bf9\u4e8e\u51fd\u6570\uff0c\u60c5\u51b5\u5c31\u4e0d\u662f\u8fd9\u6837\u4e86\u2014\u2014\u5f53\u67e5\u770b\u51fd\u6570\u7684\u539f\u578b\u65f6\uff0c\u6ca1\u6709\u4e0a\u4e0b\u6587\u6765\u5e2e\u52a9\u6307\u793a\u51fd\u6570\u8fd4\u56de\u7684\u7c7b\u578b\u3002\u867d\u7136\u4e00\u4e2a\u4f18\u79c0\u7684 IDE \u5e94\u8be5\u6e05\u695a\u63a8\u65ad\u51fa\u7684\u51fd\u6570\u7c7b\u578b\u662f\u4ec0\u4e48\uff0c\u4f46\u5728\u6ca1\u6709\u8fd9\u79cd\u529f\u80fd\u7684\u65f6\u5019\uff0c\u7528\u6237\u5b9e\u9645\u4e0a\u5fc5\u987b\u6df1\u5165\u5230\u51fd\u6570\u4f53\u672c\u8eab\u6765\u786e\u5b9a\u51fd\u6570\u8fd4\u56de\u7684\u7c7b\u578b\u3002\u72af\u9519\u8bef\u7684\u51e0\u7387\u66f4\u9ad8\u3002\u6b64\u5916\uff0c\u51fd\u6570\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u63a8\u65ad\u4e0d\u80fd\u901a\u8fc7 \u524d\u5411\u58f0\u660e \u4f7f\u7528\uff0c\u8fd9\u4e5f\u9650\u5236\u4e86\u5b83\u4eec\u5728\u591a\u6587\u4ef6\u7a0b\u5e8f\u4e2d\u7684\u53ef\u7528\u6027\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u4e8e\u4e00\u822c\u51fd\u6570\u6765\u8bf4\uff0c\u6700\u597d\u4f7f\u7528\u663e\u5f0f\u7684\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u800c\u4e0d\u662f\u7c7b\u578b\u63a8\u65ad\u3002 \u540e\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\u8bed\u6cd5 auto \u4e5f\u53ef\u4ee5\u7528\u4e8e\u540e\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\u8bed\u6cd5\uff0c\u5373\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u88ab\u653e\u7f6e\u5728\u51fd\u6570\u539f\u578b\u7684\u540e\u9762\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 int add ( int x , int y ) { return ( x + y ); } \u5b83\u7b49\u4ef7\u4e8e\u4e0b\u9762\u540e\u7f6e\u8fd4\u56de\u503c\u8bed\u6cd5\u5f62\u5f0f\uff1a 1 2 3 4 auto add ( int x , int y ) -> int { return ( x + y ); } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c auto \u4e0d\u518d\u88ab\u7528\u6765\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\u2014\u2014\u5b83\u53ea\u662f\u540e\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\u8bed\u6cd5\u7684\u4e00\u90e8\u5206\u3002 \u8fd9\u79cd\u8bed\u6cd5\u6709\u4ec0\u4e48\u7528\u5462\uff1f \u8fd9\u79cd\u8bed\u6cd5\u53ef\u4ee5\u5e2e\u6211\u4eec\u5bf9\u9f50\u51fd\u6570\u540d\uff1a 1 2 3 4 auto add ( int x , int y ) -> int ; auto divide ( double x , double y ) -> double ; auto printSomething () -> void ; auto generateSubstring ( const std :: string & s , int start , int len ) -> std :: string ; \u6b64\u5916\uff0c\u4e00\u4e9b C++ \u9ad8\u7ea7\u7279\u6027\u4e5f\u9700\u8981\u540e\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\u8bed\u6cd5\u7684\u652f\u6301\uff0c\u4f8b\u5982 lambda \u8868\u8fbe\u5f0f\uff08\u53c2\u89c1\uff1a 12.7 -- Introduction to lambdas (anonymous functions) \uff09 \u5c31\u76ee\u524d\u800c\u8a00\uff0c\u6211\u4eec\u8fd8\u662f\u5efa\u8bae\u5927\u5bb6\u4f7f\u7528\u4f20\u7edf\u7684\u51fd\u6570\u8fd4\u56de\u503c\u8bed\u6cd5\uff0c\u9664\u975e\u662f\u9700\u8981\u914d\u5408\u4e00\u4e9b\u7279\u6b8a\u7684\u7279\u6027\u6765\u4f7f\u7528\u65f6\u3002 ## \u51fd\u6570\u5f62\u53c2\u7684\u7c7b\u578b\u4e0d\u80fd\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad \u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u5728\u5b66\u4e60\u8fc7\u7c7b\u578b\u63a8\u65ad\u540e\uff0c\u4f1a\u5c1d\u8bd5\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include void addAndPrint ( auto x , auto y ) { std :: cout << x + y ; } int main () { addAndPrint ( 2 , 3 ); // case 1: call addAndPrint with int parameters addAndPrint ( 4.5 , 6.7 ); // case 2: call addAndPrint with double parameters } \u4e0d\u5e78\u7684\u662f\uff0c\u7c7b\u578b\u63a8\u6f14\u4e0d\u9002\u7528\u4e8e\u51fd\u6570\u5f62\u53c2\uff0c\u5728++ 20 \u4e4b\u524d\uff0c\u4e0a\u9762\u7684\u7a0b\u5e8f\u65e0\u6cd5\u7f16\u8bd1(\u4f1a\u5c06\u5f97\u5230\u5173\u4e8e\u51fd\u6570\u5f62\u53c2\u4e0d\u80fd\u5177\u6709\u81ea\u52a8\u7c7b\u578b\u7684\u9519\u8bef)\u3002 \u5728 C++20 \u4e2d\uff0c auto \u5173\u952e\u5b57\u518d\u6b21\u5f97\u5230\u4e86\u6269\u5c55\uff0c\u6240\u4ee5\u4e0a\u8ff0\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u7f16\u8bd1\u4e86\u3002\u4e0d\u8fc7\uff0c\u6b64\u65f6 auto \u5e76\u4e0d\u662f\u88ab\u7528\u505a\u7c7b\u578b\u63a8\u65ad\uff0c\u800c\u662f\u7528\u4e8e\u89e6\u53d1 \u51fd\u6570\u6a21\u677f(function template) \u7279\u6027\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 8.13 - \u51fd\u6570\u6a21\u677f \u4e2d\u4ecb\u7ecd\u51fd\u6570\u6a21\u677f\u5e76\u5728 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f \u4e2d\u8ba8\u8bba auto \u5728\u6b64\u8bed\u5883\u4e2d\u7684\u7528\u9014\u3002","title":"8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad"},{"location":"8-8-Type-deduction-for-functions/#_1","text":"auto \u4e5f\u53ef\u4ee5\u7528\u4e8e\u540e\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\u8bed\u6cd5\uff0c\u5373\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u88ab\u653e\u7f6e\u5728\u51fd\u6570\u539f\u578b\u7684\u540e\u9762\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 int add ( int x , int y ) { return ( x + y ); } \u5b83\u7b49\u4ef7\u4e8e\u4e0b\u9762\u540e\u7f6e\u8fd4\u56de\u503c\u8bed\u6cd5\u5f62\u5f0f\uff1a 1 2 3 4 auto add ( int x , int y ) -> int { return ( x + y ); } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c auto \u4e0d\u518d\u88ab\u7528\u6765\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\u2014\u2014\u5b83\u53ea\u662f\u540e\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\u8bed\u6cd5\u7684\u4e00\u90e8\u5206\u3002 \u8fd9\u79cd\u8bed\u6cd5\u6709\u4ec0\u4e48\u7528\u5462\uff1f \u8fd9\u79cd\u8bed\u6cd5\u53ef\u4ee5\u5e2e\u6211\u4eec\u5bf9\u9f50\u51fd\u6570\u540d\uff1a 1 2 3 4 auto add ( int x , int y ) -> int ; auto divide ( double x , double y ) -> double ; auto printSomething () -> void ; auto generateSubstring ( const std :: string & s , int start , int len ) -> std :: string ; \u6b64\u5916\uff0c\u4e00\u4e9b C++ \u9ad8\u7ea7\u7279\u6027\u4e5f\u9700\u8981\u540e\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\u8bed\u6cd5\u7684\u652f\u6301\uff0c\u4f8b\u5982 lambda \u8868\u8fbe\u5f0f\uff08\u53c2\u89c1\uff1a 12.7 -- Introduction to lambdas (anonymous functions) \uff09 \u5c31\u76ee\u524d\u800c\u8a00\uff0c\u6211\u4eec\u8fd8\u662f\u5efa\u8bae\u5927\u5bb6\u4f7f\u7528\u4f20\u7edf\u7684\u51fd\u6570\u8fd4\u56de\u503c\u8bed\u6cd5\uff0c\u9664\u975e\u662f\u9700\u8981\u914d\u5408\u4e00\u4e9b\u7279\u6b8a\u7684\u7279\u6027\u6765\u4f7f\u7528\u65f6\u3002","title":"\u540e\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\u8bed\u6cd5"},{"location":"8-8-Type-deduction-for-functions/#_2","text":"\u5f88\u591a\u65b0\u624b\u7a0b\u5e8f\u5458\u5728\u5b66\u4e60\u8fc7\u7c7b\u578b\u63a8\u65ad\u540e\uff0c\u4f1a\u5c1d\u8bd5\u8fd9\u6837\u505a\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include void addAndPrint ( auto x , auto y ) { std :: cout << x + y ; } int main () { addAndPrint ( 2 , 3 ); // case 1: call addAndPrint with int parameters addAndPrint ( 4.5 , 6.7 ); // case 2: call addAndPrint with double parameters } \u4e0d\u5e78\u7684\u662f\uff0c\u7c7b\u578b\u63a8\u6f14\u4e0d\u9002\u7528\u4e8e\u51fd\u6570\u5f62\u53c2\uff0c\u5728++ 20 \u4e4b\u524d\uff0c\u4e0a\u9762\u7684\u7a0b\u5e8f\u65e0\u6cd5\u7f16\u8bd1(\u4f1a\u5c06\u5f97\u5230\u5173\u4e8e\u51fd\u6570\u5f62\u53c2\u4e0d\u80fd\u5177\u6709\u81ea\u52a8\u7c7b\u578b\u7684\u9519\u8bef)\u3002 \u5728 C++20 \u4e2d\uff0c auto \u5173\u952e\u5b57\u518d\u6b21\u5f97\u5230\u4e86\u6269\u5c55\uff0c\u6240\u4ee5\u4e0a\u8ff0\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u7f16\u8bd1\u4e86\u3002\u4e0d\u8fc7\uff0c\u6b64\u65f6 auto \u5e76\u4e0d\u662f\u88ab\u7528\u505a\u7c7b\u578b\u63a8\u65ad\uff0c\u800c\u662f\u7528\u4e8e\u89e6\u53d1 \u51fd\u6570\u6a21\u677f(function template) \u7279\u6027\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u4f1a\u5728 8.13 - \u51fd\u6570\u6a21\u677f \u4e2d\u4ecb\u7ecd\u51fd\u6570\u6a21\u677f\u5e76\u5728 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f \u4e2d\u8ba8\u8bba auto \u5728\u6b64\u8bed\u5883\u4e2d\u7684\u7528\u9014\u3002","title":"## \u51fd\u6570\u5f62\u53c2\u7684\u7c7b\u578b\u4e0d\u80fd\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad"},{"location":"8-9-Introduction-to-function-overloading/","tags":["overloading"],"text":"Key Takeaway \u4f7f\u7528\u76f8\u540c\u51fd\u6570\u540d\u4f46\u5f62\u53c2\u4e0d\u540c\u7684\u51fd\u6570\uff0c\u79f0\u4e3a\u51fd\u6570\u91cd\u8f7d\u3002 \u53ea\u8981\u7f16\u8bd1\u5668\u80fd\u591f\u533a\u5206\u6bcf\u4e2a\u91cd\u8f7d\u51fd\u6570\uff0c\u51fd\u6570\u5c31\u53ef\u4ee5\u88ab\u91cd\u8f7d\u3002\u5982\u679c\u4e0d\u80fd\u533a\u5206\u91cd\u8f7d\u51fd\u6570\uff0c\u5c06\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 C++ \u4e2d\u7684\u64cd\u4f5c\u7b26\u5176\u5b9e\u4e5f\u662f\u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u4eec\u4e5f\u53ef\u4ee5\u88ab\u91cd\u8f7d \u91cd\u8f7d\u51fd\u6570\u80fd\u591f\u8fdb\u884c\u7f16\u8bd1\uff0c\u5fc5\u987b\u6ee1\u8db3\u4e24\u4e2a\u6761\u4ef6\uff1a \u6bcf\u4e2a\u91cd\u8f7d\u7684\u51fd\u6570\u90fd\u5fc5\u987b\u80fd\u591f\u533a\u5206\u3002\u6211\u4eec\u4f1a\u5728 8.10 - \u51fd\u6570\u91cd\u8f7d\u548c\u533a\u5206 \u4e2d\u4ecb\u7ecd\u5b83\u4eec\u662f\u5982\u4f55\u88ab\u533a\u5206\u7684\u3002 \u6bcf\u4e2a\u88ab\u8c03\u7528\u7684\u91cd\u8f7d\u51fd\u6570\u5728\u91cd\u8f7d\u89e3\u6790\u65f6\u5fc5\u987b\u80fd\u591f\u5b9a\u4f4d\u5230\u4e00\u4e2a\u51fd\u6570\u3002\u6211\u4eec\u4f1a\u5728 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u5339\u914d\u6b67\u4e49 \u4e2d\u4ecb\u7ecd\u5982\u4f55\u8fdb\u884c\u91cd\u8f7d\u51fd \u8003\u8651\u4e0b\u9762\u7684\u51fd\u6570\uff1a 1 2 3 4 int add ( int x , int y ) { return x + y ; } \u8fd9\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u5c06\u4e24\u4e2a\u6574\u6570\u76f8\u52a0\u5e76\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u7ed3\u679c\u3002\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u8fd8\u9700\u8981\u4e00\u4e2a\u53ef\u4ee5\u5c06\u4e24\u4e2a\u6d6e\u70b9\u6570\u76f8\u52a0\u7684\u51fd\u6570\u5462\uff1f\u8fd9\u4e2a add() \u51fd\u6570\u5e76\u4e0d\u80fd\u5b9e\u73b0\u6d6e\u70b9\u6570\u76f8\u52a0\uff0c\u56e0\u4e3a\u4efb\u4f55\u6d6e\u70b9 \u5f62\u53c2 \u90fd\u4f1a\u88ab\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5bfc\u81f4\u6d6e\u70b9\u5f62\u53c2\u4e22\u5931\u5b83\u4eec\u7684\u5c0f\u6570\u503c\u3002 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u5b9a\u4e49\u591a\u4e2a\u540d\u79f0\u7565\u6709\u4e0d\u540c\u7684\u51fd\u6570: 1 2 3 4 5 6 7 8 9 int addInteger ( int x , int y ) { return x + y ; } double addDouble ( double x , double y ) { return x + y ; } \u4f46\u662f\uff0c\u4e3a\u4e86\u8fbe\u5230\u6700\u4f73\u6548\u679c\uff0c\u4f60\u5fc5\u987b\u4e3a\u5177\u6709\u4e0d\u540c\u7c7b\u578b\u53c2\u6570\u7684\u7c7b\u4f3c\u51fd\u6570\uff0c\u5b9a\u4e49\u4e00\u81f4\u7684\u547d\u540d\u6807\u51c6\uff0c\u540c\u65f6\u8bb0\u4f4f\u8fd9\u4e9b\u51fd\u6570\u7684\u540d\u79f0\uff0c\u5e76\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u51fd\u6570\u3002 \u5c06\u6765\uff0c\u5982\u679c\u6211\u4eec\u53c8\u9700\u8981\u4e00\u4e2a\u7c7b\u4f3c\u7684\u51fd\u6570\uff0c\u5c06\u4e09\u4e2a\u6574\u6570\u76f8\u52a0\u800c\u4e0d\u662f\u4e24\u4e2a\u6574\u6570\u76f8\u52a0\u7684\u65f6\u5019\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u4e3a\u6bcf\u4e00\u4e2a\u51fd\u6570\u521b\u5efa\u4e0d\u540c\u7684\u540d\u79f0\u4f1a\u4f7f\u4ee3\u7801\u53d8\u5f97\u96be\u4ee5\u7ef4\u62a4\u3002 \u51fd\u6570\u91cd\u8f7d\u7b80\u4ecb \u5e78\u8fd0\u7684\u662f\uff0cC++ \u63d0\u4f9b\u4e86\u4e00\u4e2a\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\u3002\u51fd\u6570\u91cd\u8f7d\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u76f8\u540c\u7684\u540d\u79f0\u521b\u5efa\u591a\u4e2a\u51fd\u6570\uff0c\u53ea\u8981\u6bcf\u4e2a\u76f8\u540c\u540d\u79f0\u7684\u51fd\u6570\u5177\u6709\u4e0d\u540c\u7684\u5f62\u53c2\u7c7b\u578b(\u6216\u8005\u53ef\u4ee5\u7528\u5176\u4ed6\u65b9\u5f0f\u533a\u5206\u51fd\u6570)\u3002\u6bcf\u4e2a\u51fd\u6570\u90fd\u5171\u4eab\u4e00\u4e2a\u540d\u79f0(\u5728\u540c\u4e00\u4e2a\u4f5c\u7528\u57df\u4e2d)\uff0c\u79f0\u4e3a \u91cd\u8f7d(overload) \u51fd\u6570(\u6709\u65f6\u7b80\u79f0\u91cd\u8f7d)\u3002 \u5bf9 add() \u51fd\u6570\u8fdb\u884c\u91cd\u8f7d\uff0c\u6211\u4eec\u53ef\u4ee5\u58f0\u660e\u53e6\u5916\u4e00\u4e2a add() \u51fd\u6570\uff0c\u4f46\u662f\u53c2\u6570\u7c7b\u578b\u4e3a double \uff1a 1 2 3 4 double add ( double x , double y ) { return x + y ; } \u6b64\u65f6\uff0c\u5728\u540c\u4e00\u4e2a\u4f5c\u7528\u57df\u4e2d\uff0c\u51fa\u73b0\u4e86\u4e24\u4e2a\u540c\u540d\u7684 add() \u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int add ( int x , int y ) // integer version { return x + y ; } double add ( double x , double y ) // floating point version { return x + y ; } int main () { return 0 ; } \u4ee5\u4e0a\u7a0b\u5e8f\u662f\u53ef\u4ee5\u7f16\u8bd1\u7684\u3002\u5c3d\u7ba1\u4f60\u53ef\u80fd\u62c5\u5fc3\u8fd9\u4e9b\u51fd\u6570\u4f1a\u51fa\u73b0\u547d\u540d\u51b2\u7a81\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u4f1a\u3002\u56e0\u4e3a\u8fd9\u4e9b\u51fd\u6570\u7684\u5f62\u53c2\u7c7b\u578b\u4e0d\u540c\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u80fd\u591f\u533a\u5206\u8fd9\u4e9b\u51fd\u6570\uff0c\u5e76\u5c06\u5b83\u4eec\u89c6\u4e3a\u5171\u4eab\u4e00\u4e2a\u540d\u79f0\u7684\u5355\u72ec\u51fd\u6570\u3002 \u5173\u952e\u4fe1\u606f \u53ea\u8981\u7f16\u8bd1\u5668\u80fd\u591f\u533a\u5206\u6bcf\u4e2a\u91cd\u8f7d\u51fd\u6570\uff0c\u51fd\u6570\u5c31\u53ef\u4ee5\u88ab\u91cd\u8f7d\u3002\u5982\u679c\u4e0d\u80fd\u533a\u5206\u91cd\u8f7d\u51fd\u6570\uff0c\u5c06\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 \u76f8\u5173\u5185\u5bb9 \u56e0\u4e3a C++ \u4e2d\u7684\u64cd\u4f5c\u7b26\u5176\u5b9e\u4e5f\u662f\u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u4eec\u4e5f\u53ef\u4ee5\u88ab\u91cd\u8f7d\uff0c\u6211\u4eec\u4f1a\u5728 14.1 -- Introduction to operator overloading \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002 \u51fd\u6570\u91cd\u8f7d\u89e3\u6790 \u6b64\u5916\uff0c\u5f53\u8c03\u7528\u88ab\u91cd\u8f7d\u7684\u51fd\u6570\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u57fa\u4e8e\u5f62\u53c2\u6765\u5339\u914d\u6700\u5408\u9002\u7684\u51fd\u6570\u8c03\u7528\u2014\u2014\u79f0\u4e3a \u91cd\u8f7d\u89e3\u6790(overload resolution) \u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int add ( int x , int y ) { return x + y ; } double add ( double x , double y ) { return x + y ; } int main () { std :: cout << add ( 1 , 2 ); // calls add(int, int) std :: cout << '\\n' ; std :: cout << add ( 1.2 , 3.4 ); // calls add(double, double) return 0 ; } \u4e0a\u8ff0\u51fd\u6570\u7f16\u8bd1\u6267\u884c\u7684\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4.6 \u5f53\u63d0\u4f9b\u6574\u578b\u5f62\u53c2\u65f6\uff08 add(1, 2) \uff09\u7f16\u8bd1\u5668\u53ef\u4ee5\u5224\u65ad\u51fa\u6211\u4eec\u9700\u8981\u8c03\u7528\u7684\u662f add(int, int) \u3002\u800c\u5f53\u6211\u4eec\u63d0\u4f9b\u6d6e\u70b9\u7c7b\u578b\u7684\u5f62\u53c2\u65f6\uff08 add(1.2, 3.4) \uff09\uff0c\u7f16\u8bd1\u5668\u80fd\u591f\u786e\u5b9a\u6211\u4eec\u8c03\u7528\u7684\u662f add(double, double) \u3002 \u4f7f\u91cd\u8f7d\u7684\u51fd\u6570\u80fd\u591f\u7f16\u8bd1 \u4e3a\u4e86\u4f7f\u4f7f\u7528\u4e86\u91cd\u8f7d\u51fd\u6570\u7684\u7a0b\u5e8f\u80fd\u591f\u8fdb\u884c\u7f16\u8bd1\uff0c\u5fc5\u987b\u6ee1\u8db3\u4e24\u4e2a\u6761\u4ef6\uff1a \u6bcf\u4e2a\u91cd\u8f7d\u7684\u51fd\u6570\u90fd\u5fc5\u987b\u80fd\u591f\u533a\u5206\u3002\u6211\u4eec\u4f1a\u5728 8.10 - \u51fd\u6570\u91cd\u8f7d\u548c\u533a\u5206 \u4e2d\u4ecb\u7ecd\u5b83\u4eec\u662f\u5982\u4f55\u88ab\u533a\u5206\u7684\u3002 \u6bcf\u4e2a\u88ab\u8c03\u7528\u7684\u91cd\u8f7d\u51fd\u6570\u5728\u91cd\u8f7d\u89e3\u6790\u65f6\u5fc5\u987b\u80fd\u591f\u5b9a\u4f4d\u5230\u4e00\u4e2a\u51fd\u6570\u3002\u6211\u4eec\u4f1a\u5728 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u5339\u914d\u6b67\u4e49 \u4e2d\u4ecb\u7ecd\u5982\u4f55\u8fdb\u884c\u91cd\u8f7d\u51fd\u6570\u7684\u5339\u914d\u3002 \u5982\u679c\u91cd\u8f7d\u51fd\u6570\u4e0d\u80fd\u88ab\u533a\u5206\uff0c\u6216\u8005\u91cd\u8f7d\u51fd\u6570\u7684\u51fd\u6570\u8c03\u7528\u65e0\u6cd5\u89e3\u6790\u4e3a\u91cd\u8f7d\u51fd\u6570\uff0c\u5219\u5c06\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u533a\u5206\u91cd\u8f7d\u51fd\u6570\u3002\u7136\u540e\uff0c\u5728\u518d\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u7d22\u7f16\u8bd1\u5668\u5982\u4f55\u5c06\u51fd\u6570\u8c03\u7528\u89e3\u6790\u4e3a\u91cd\u8f7d\u51fd\u6570\u3002 \u5c0f\u7ed3 \u51fd\u6570\u91cd\u8f7d\u901a\u8fc7\u51cf\u5c11\u9700\u8981\u8bb0\u4f4f\u7684\u51fd\u6570\u540d\u7684\u6570\u91cf\u6765\u964d\u4f4e\u7a0b\u5e8f\u7684\u590d\u6742\u6027\u3002\u5b83\u53ef\u4ee5\u800c\u4e14\u5e94\u8be5\u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u51fd\u6570\u91cd\u8f7d\u53ef\u4ee5\u7b80\u5316\u7a0b\u5e8f\u3002","title":"8.9 - \u51fd\u6570\u91cd\u8f7d"},{"location":"8-9-Introduction-to-function-overloading/#_1","text":"\u5e78\u8fd0\u7684\u662f\uff0cC++ \u63d0\u4f9b\u4e86\u4e00\u4e2a\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\u3002\u51fd\u6570\u91cd\u8f7d\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u76f8\u540c\u7684\u540d\u79f0\u521b\u5efa\u591a\u4e2a\u51fd\u6570\uff0c\u53ea\u8981\u6bcf\u4e2a\u76f8\u540c\u540d\u79f0\u7684\u51fd\u6570\u5177\u6709\u4e0d\u540c\u7684\u5f62\u53c2\u7c7b\u578b(\u6216\u8005\u53ef\u4ee5\u7528\u5176\u4ed6\u65b9\u5f0f\u533a\u5206\u51fd\u6570)\u3002\u6bcf\u4e2a\u51fd\u6570\u90fd\u5171\u4eab\u4e00\u4e2a\u540d\u79f0(\u5728\u540c\u4e00\u4e2a\u4f5c\u7528\u57df\u4e2d)\uff0c\u79f0\u4e3a \u91cd\u8f7d(overload) \u51fd\u6570(\u6709\u65f6\u7b80\u79f0\u91cd\u8f7d)\u3002 \u5bf9 add() \u51fd\u6570\u8fdb\u884c\u91cd\u8f7d\uff0c\u6211\u4eec\u53ef\u4ee5\u58f0\u660e\u53e6\u5916\u4e00\u4e2a add() \u51fd\u6570\uff0c\u4f46\u662f\u53c2\u6570\u7c7b\u578b\u4e3a double \uff1a 1 2 3 4 double add ( double x , double y ) { return x + y ; } \u6b64\u65f6\uff0c\u5728\u540c\u4e00\u4e2a\u4f5c\u7528\u57df\u4e2d\uff0c\u51fa\u73b0\u4e86\u4e24\u4e2a\u540c\u540d\u7684 add() \u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int add ( int x , int y ) // integer version { return x + y ; } double add ( double x , double y ) // floating point version { return x + y ; } int main () { return 0 ; } \u4ee5\u4e0a\u7a0b\u5e8f\u662f\u53ef\u4ee5\u7f16\u8bd1\u7684\u3002\u5c3d\u7ba1\u4f60\u53ef\u80fd\u62c5\u5fc3\u8fd9\u4e9b\u51fd\u6570\u4f1a\u51fa\u73b0\u547d\u540d\u51b2\u7a81\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u4f1a\u3002\u56e0\u4e3a\u8fd9\u4e9b\u51fd\u6570\u7684\u5f62\u53c2\u7c7b\u578b\u4e0d\u540c\uff0c\u6240\u4ee5\u7f16\u8bd1\u5668\u80fd\u591f\u533a\u5206\u8fd9\u4e9b\u51fd\u6570\uff0c\u5e76\u5c06\u5b83\u4eec\u89c6\u4e3a\u5171\u4eab\u4e00\u4e2a\u540d\u79f0\u7684\u5355\u72ec\u51fd\u6570\u3002 \u5173\u952e\u4fe1\u606f \u53ea\u8981\u7f16\u8bd1\u5668\u80fd\u591f\u533a\u5206\u6bcf\u4e2a\u91cd\u8f7d\u51fd\u6570\uff0c\u51fd\u6570\u5c31\u53ef\u4ee5\u88ab\u91cd\u8f7d\u3002\u5982\u679c\u4e0d\u80fd\u533a\u5206\u91cd\u8f7d\u51fd\u6570\uff0c\u5c06\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 \u76f8\u5173\u5185\u5bb9 \u56e0\u4e3a C++ \u4e2d\u7684\u64cd\u4f5c\u7b26\u5176\u5b9e\u4e5f\u662f\u51fd\u6570\uff0c\u6240\u4ee5\u5b83\u4eec\u4e5f\u53ef\u4ee5\u88ab\u91cd\u8f7d\uff0c\u6211\u4eec\u4f1a\u5728 14.1 -- Introduction to operator overloading \u4e2d\u8fdb\u884c\u4ecb\u7ecd\u3002","title":"\u51fd\u6570\u91cd\u8f7d\u7b80\u4ecb"},{"location":"8-9-Introduction-to-function-overloading/#_2","text":"\u6b64\u5916\uff0c\u5f53\u8c03\u7528\u88ab\u91cd\u8f7d\u7684\u51fd\u6570\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u57fa\u4e8e\u5f62\u53c2\u6765\u5339\u914d\u6700\u5408\u9002\u7684\u51fd\u6570\u8c03\u7528\u2014\u2014\u79f0\u4e3a \u91cd\u8f7d\u89e3\u6790(overload resolution) \u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int add ( int x , int y ) { return x + y ; } double add ( double x , double y ) { return x + y ; } int main () { std :: cout << add ( 1 , 2 ); // calls add(int, int) std :: cout << '\\n' ; std :: cout << add ( 1.2 , 3.4 ); // calls add(double, double) return 0 ; } \u4e0a\u8ff0\u51fd\u6570\u7f16\u8bd1\u6267\u884c\u7684\u7ed3\u679c\u5982\u4e0b\uff1a 1 2 3 4.6 \u5f53\u63d0\u4f9b\u6574\u578b\u5f62\u53c2\u65f6\uff08 add(1, 2) \uff09\u7f16\u8bd1\u5668\u53ef\u4ee5\u5224\u65ad\u51fa\u6211\u4eec\u9700\u8981\u8c03\u7528\u7684\u662f add(int, int) \u3002\u800c\u5f53\u6211\u4eec\u63d0\u4f9b\u6d6e\u70b9\u7c7b\u578b\u7684\u5f62\u53c2\u65f6\uff08 add(1.2, 3.4) \uff09\uff0c\u7f16\u8bd1\u5668\u80fd\u591f\u786e\u5b9a\u6211\u4eec\u8c03\u7528\u7684\u662f add(double, double) \u3002","title":"\u51fd\u6570\u91cd\u8f7d\u89e3\u6790"},{"location":"8-9-Introduction-to-function-overloading/#_3","text":"\u4e3a\u4e86\u4f7f\u4f7f\u7528\u4e86\u91cd\u8f7d\u51fd\u6570\u7684\u7a0b\u5e8f\u80fd\u591f\u8fdb\u884c\u7f16\u8bd1\uff0c\u5fc5\u987b\u6ee1\u8db3\u4e24\u4e2a\u6761\u4ef6\uff1a \u6bcf\u4e2a\u91cd\u8f7d\u7684\u51fd\u6570\u90fd\u5fc5\u987b\u80fd\u591f\u533a\u5206\u3002\u6211\u4eec\u4f1a\u5728 8.10 - \u51fd\u6570\u91cd\u8f7d\u548c\u533a\u5206 \u4e2d\u4ecb\u7ecd\u5b83\u4eec\u662f\u5982\u4f55\u88ab\u533a\u5206\u7684\u3002 \u6bcf\u4e2a\u88ab\u8c03\u7528\u7684\u91cd\u8f7d\u51fd\u6570\u5728\u91cd\u8f7d\u89e3\u6790\u65f6\u5fc5\u987b\u80fd\u591f\u5b9a\u4f4d\u5230\u4e00\u4e2a\u51fd\u6570\u3002\u6211\u4eec\u4f1a\u5728 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u5339\u914d\u6b67\u4e49 \u4e2d\u4ecb\u7ecd\u5982\u4f55\u8fdb\u884c\u91cd\u8f7d\u51fd\u6570\u7684\u5339\u914d\u3002 \u5982\u679c\u91cd\u8f7d\u51fd\u6570\u4e0d\u80fd\u88ab\u533a\u5206\uff0c\u6216\u8005\u91cd\u8f7d\u51fd\u6570\u7684\u51fd\u6570\u8c03\u7528\u65e0\u6cd5\u89e3\u6790\u4e3a\u91cd\u8f7d\u51fd\u6570\uff0c\u5219\u5c06\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002 \u5728\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u533a\u5206\u91cd\u8f7d\u51fd\u6570\u3002\u7136\u540e\uff0c\u5728\u518d\u4e0b\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u7d22\u7f16\u8bd1\u5668\u5982\u4f55\u5c06\u51fd\u6570\u8c03\u7528\u89e3\u6790\u4e3a\u91cd\u8f7d\u51fd\u6570\u3002","title":"\u4f7f\u91cd\u8f7d\u7684\u51fd\u6570\u80fd\u591f\u7f16\u8bd1"},{"location":"8-9-Introduction-to-function-overloading/#_4","text":"\u51fd\u6570\u91cd\u8f7d\u901a\u8fc7\u51cf\u5c11\u9700\u8981\u8bb0\u4f4f\u7684\u51fd\u6570\u540d\u7684\u6570\u91cf\u6765\u964d\u4f4e\u7a0b\u5e8f\u7684\u590d\u6742\u6027\u3002\u5b83\u53ef\u4ee5\u800c\u4e14\u5e94\u8be5\u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u51fd\u6570\u91cd\u8f7d\u53ef\u4ee5\u7b80\u5316\u7a0b\u5e8f\u3002","title":"\u5c0f\u7ed3"},{"location":"8-x-Chapter-8-summary-and-quiz/","tags":["summary"],"text":"\u5b66\u5b8c\u54af\uff01\u672c\u7ae0\u7684\u4e3b\u9898(\u7279\u522b\u662f\u7c7b\u578b\u522b\u540d\u3001\u91cd\u8f7d\u51fd\u6570\u548c\u51fd\u6570\u6a21\u677f)\u5728 C++ \u6807\u51c6\u5e93\u4e2d\u968f\u5904\u53ef\u89c1\u3002\u6211\u4eec\u8fd8\u6709\u4e00\u7ae0\u8981\u8bb2(\u4ecb\u7ecd\u590d\u5408\u7c7b\u578b)\uff0c\u7136\u540e\u6211\u4eec\u5c06\u51c6\u5907\u597d\u6df1\u5165\u7814\u7a76\u6807\u51c6\u5e93\u4e2d\u6700\u6709\u7528\u7684\u90e8\u5206\uff01 \u590d\u4e60 \u5c06\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u7684\u8fc7\u7a0b\u53eb\u505a \u7c7b\u578b\u8f6c\u6362 \u3002 \u5f53\u9700\u8981\u4e00\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u63d0\u4f9b\u4e86\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u65f6\uff0c\u5219\u4f1a\u6267\u884c \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 (\u4e5f\u79f0\u4e3a\u81ea\u52a8\u7c7b\u578b\u8f6c\u6362\u6216\u5f3a\u5236\u8f6c\u6362)\u3002\u5982\u679c\u7f16\u8bd1\u5668\u80fd\u591f\u627e\u5230\u4e24\u79cd\u7c7b\u578b\u4e4b\u95f4\u8fdb\u8fdb\u884c\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u7684\u89c4\u5219\uff0c\u5219\u5b83\u4f1a\u6267\u884c\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\uff0c\u5426\u5219\u5c31\u4f1a\u4ea7\u751f\u7f16\u8bd1\u62a5\u9519\u3002\uff08 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \uff09 C++ \u8bed\u8a00\u5b9a\u4e49\u4e86\u8bb8\u591a\u57fa\u672c\u7c7b\u578b\u4e4b\u95f4\u7684\u5185\u7f6e\u8f6c\u6362\u65b9\u5f0f(\u4ee5\u53ca\u4e00\u4e9b\u66f4\u9ad8\u7ea7\u7684\u7c7b\u578b\u7684\u8f6c\u6362)\uff0c\u79f0\u4e3a \u6807\u51c6\u8f6c\u6362 \u3002\u5176\u4e2d\u5305\u62ec \u6570\u503c\u63d0\u5347 \u3001 \u6570\u503c\u8f6c\u6362 \u548c \u7b97\u6570\u8f6c\u6362 \u6570\u503c\u63d0\u5347 \uff08 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 \uff09\u662f\u5c06\u8f83\u5c0f\u7684\u6570\u503c\u7c7b\u578b\u8f6c\u6362\u4e3a\u8f83\u5927\u7684\u6570\u503c\u7c7b\u578b(\u901a\u5e38\u4e3a int \u6216 double )\uff0c\u8fd9\u6837CPU\u5c31\u53ef\u4ee5\u5bf9\u4e0e\u5904\u7406\u5668\u81ea\u7136\u6570\u636e\u5927\u5c0f\u76f8\u5339\u914d\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002\u6570\u503c\u63d0\u5347\u5305\u62ec\u6574\u6570\u63d0\u5347\u548c\u6d6e\u70b9\u63d0\u5347\u3002\u6570\u5b57\u63d0\u5347\u662f\u4fdd\u503c\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4e0d\u4f1a\u4e22\u5931\u503c\u6216\u503c\u7684\u7cbe\u5ea6\u3002 \u6570\u503c\u8f6c\u6362 \uff08 8.3 - \u6570\u503c\u8f6c\u6362 \uff09\u6570\u503c\u8f6c\u6362\u662f\u57fa\u672c\u7c7b\u578b\u4e4b\u95f4\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u800c\u4e0d\u662f\u6570\u503c\u63d0\u5347\u3002\u6536\u7f29\u8f6c\u6362\u53ef\u80fd\u4f1a\u5bfc\u81f4\u503c\u6216\u7cbe\u5ea6\u7684\u635f\u5931\u3002 \u5728C++\u4e2d\uff0c\u67d0\u4e9b\u4e8c\u5143\u64cd\u4f5c\u7b26\u8981\u6c42\u5b83\u4eec\u7684\u64cd\u4f5c\u6570\u5177\u6709\u76f8\u540c\u7684\u7c7b\u578b\u3002\u5982\u679c\u63d0\u4f9b\u4e86\u4e0d\u540c\u7c7b\u578b\u7684\u64cd\u4f5c\u6570\uff0c\u5219\u4f7f\u7528\u4e00\u7ec4\u79f0\u4e3a \u666e\u901a\u7b97\u672f\u8f6c\u6362 \uff08 8.4 - \u7b97\u672f\u8f6c\u6362 \uff09\u89c4\u5219\u5c06\u5176\u4e2d\u4e00\u4e2a\u6216\u4e24\u4e2a\u64cd\u4f5c\u6570\u9690\u5f0f\u8f6c\u6362\u4e3a\u5339\u914d\u7c7b\u578b\u3002 \u5f53\u7a0b\u5e8f\u5458\u663e\u5f0f\u5730\u901a\u8fc7 cast \u6765\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u79f0\u4e3a \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \uff08 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast \uff09\u3002C++\u652f\u63015\u79cd\u7c7b\u578b\u5f3a\u5236\u8f6c\u6362\uff1a C\u98ce\u683c\u7c7b\u578b\u8f6c\u6362 \u3001 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u3001 const \u7c7b\u578b\u8f6c\u6362 \u3001 \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u548c \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 \u3002\u4e00\u822c\u6765\u8bf4\uff0c\u4f60\u5e94\u8be5\u907f\u514d\u4f7f\u7528\"C\u98ce\u683c\u7c7b\u578b\u8f6c\u6362\u201d\u3001\u201cconst\u7c7b\u578b\u8f6c\u6362\u201d\u548c\u201c\u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362\u201d\u3002 static_cast \u7528\u4e8e\u5c06\u4e00\u4e2a\u503c\u4ece\u4e00\u79cd\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u7c7b\u578b\uff0c\u5b83\u662f\u76ee\u524dC++\u4e2d\u88ab\u6700\u591a\u4f7f\u7528\u7684\u5f3a\u5236\u8f6c\u6362\u3002 typedef \u548c\u7c7b\u578b\u522b\u540d \u5141\u8bb8\u7a0b\u5e8f\u5458\u4e3a\u6570\u636e\u7c7b\u578b\u521b\u5efa\u522b\u540d\u3002\u8fd9\u4e9b\u522b\u540d\u4e0d\u662f\u65b0\u7c7b\u578b\uff0c\u5e76\u4e14\u4e0e\u539f\u7c7b\u578b\u7684\u64cd\u4f5c\u76f8\u540c\u3002Typedefs \u548c\u7c7b\u578b\u522b\u540d\u4e0d\u63d0\u4f9b\u4efb\u4f55\u7c7b\u578b\u5b89\u5168\u673a\u5236\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u8981\u5047\u5b9a\u7c7b\u578b\u522b\u540d\u548c\u5176\u539f\u7c7b\u578b\u662f\u6709\u533a\u522b\u7684\u3002 auto \u5173\u952e\u5b57\u6709\u5f88\u591a\u7528\u9014\u3002\u9996\u5148\uff0c auto \u53ef\u7528\u4e8e\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad( 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad )\uff0c\u5b83\u5c06\u4ece\u53d8\u91cf\u7684\u521d\u59cb\u5316\u503c\u63a8\u65ad\u51fa\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u4f46\u662f\uff0c\u7c7b\u578b\u63a8\u65ad\u4f1a\u4e22\u5f03 const \u548c\u5f15\u7528\uff0c\u6240\u4ee5\u5982\u679c\u9700\u8981\uff0c\u8bf7\u786e\u4fdd\u5c06\u5b83\u4eec\u6dfb\u52a0\u56de\u6765\u3002 auto \u4e5f\u53ef\u4ee5\u7528\u4f5c\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u7684\u63a8\u65ad\uff08 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad \uff09\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u4ece\u51fd\u6570\u7684 return \u8bed\u53e5\u4e2d\u63a8\u65ad\u51fa\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\uff0c\u4e0d\u8fc7\u5bf9\u4e8e\u666e\u901a\u51fd\u6570\u5e94\u8be5\u907f\u514d\u8fd9\u6837\u505a\u3002 auto \u8fd8\u88ab\u7528\u4f5c\u540e\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\u8bed\u6cd5\u3002 \u51fd\u6570\u91cd\u8f7d \uff08 8.9 - \u51fd\u6570\u91cd\u8f7d \uff09\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u76f8\u540c\u7684\u540d\u79f0\u521b\u5efa\u591a\u4e2a\u51fd\u6570\uff0c\u53ea\u8981\u6bcf\u4e2a\u76f8\u540c\u540d\u79f0\u7684\u51fd\u6570\u5177\u6709\u4e0d\u540c\u7684\u5f62\u53c2\u7c7b\u578b\u96c6\u5408\u5373\u53ef(\u6216\u8005\u53ef\u4ee5\u7528\u5176\u4ed6\u65b9\u5f0f\u533a\u5206\u51fd\u6570)\u3002\u8fd9\u6837\u7684\u51fd\u6570\u79f0\u4e3a\u91cd\u8f7d\u51fd\u6570(\u6216\u7b80\u79f0 \u91cd\u8f7d )\u3002\u8fd4\u56de\u7c7b\u578b\u4e0d\u80fd\u88ab\u7528\u6765\u8fdb\u884c\u91cd\u8f7d\u51fd\u6570\u7684\u533a\u5206\u3002 \u5728\u89e3\u6790\u91cd\u8f7d\u51fd\u6570\u65f6\uff08 8.10 - \u51fd\u6570\u91cd\u8f7d\u548c\u533a\u5206 \uff09\uff0c\u5982\u679c\u6ca1\u6709\u627e\u5230\u7cbe\u786e\u5339\u914d\uff0c\u7f16\u8bd1\u5668\u5c06\u4f18\u5148\u9009\u62e9\u53ef\u4ee5\u901a\u8fc7\u6570\u503c\u63d0\u5347\u5339\u914d\u7684\u91cd\u8f7d\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u90a3\u4e9b\u9700\u8981\u6570\u503c\u8f6c\u6362\u7684\u91cd\u8f7d\u51fd\u6570\u3002\u5f53\u5bf9\u5df2\u91cd\u8f7d\u7684\u51fd\u6570\u8fdb\u884c\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u5c1d\u8bd5\u6839\u636e\u51fd\u6570\u8c03\u7528\u4e2d\u4f7f\u7528\u7684\u53c2\u6570\u5c06\u51fd\u6570\u8c03\u7528\u4e0e\u9002\u5f53\u7684\u91cd\u8f7d\u5339\u914d\u3002\u8fd9\u88ab\u79f0\u4e3a \u91cd\u8f7d\u89e3\u6790 \u3002 \u5f53\u7f16\u8bd1\u5668\u53d1\u73b0\u4e24\u4e2a\u6216\u591a\u4e2a\u51fd\u6570\u53ef\u4ee5\u5c06\u51fd\u6570\u8c03\u7528\u4e0e\u91cd\u8f7d\u51fd\u6570\u5339\u914d\uff0c\u4f46\u65e0\u6cd5\u786e\u5b9a\u54ea\u4e00\u4e2a\u662f\u6700\u597d\u7684\u65f6\uff0c\u5c31\u4f1a\u51fa\u73b0 \u4e0d\u660e\u786e\u5339\u914d \uff08 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u4e0d\u660e\u786e\u5339\u914d \uff09\u3002 \u9ed8\u8ba4\u5b9e\u53c2 \u662f\u4e3a\u51fd\u6570\u5f62\u53c2\u63d0\u4f9b\u7684\u9ed8\u8ba4\u503c\uff08 8.12 - \u9ed8\u8ba4\u53c2\u6570 \uff09\u3002\u5e26\u9ed8\u8ba4\u5b9e\u53c2\u7684\u5f62\u53c2\u5fc5\u987b\u59cb\u7ec8\u662f\u6700\u53f3\u8fb9\u7684\u5f62\u53c2\uff0c\u5e76\u4e14\u5728\u89e3\u6790\u91cd\u8f7d\u51fd\u6570\u65f6\u4e0d\u4f7f\u7528\u5b83\u4eec\u6765\u533a\u5206\u51fd\u6570\u3002 \u51fd\u6570\u6a21\u677f\uff08 8.13 - \u51fd\u6570\u6a21\u677f \uff09\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7c7b\u4f3c\u51fd\u6570\u7684\u5b9a\u4e49\uff0c\u4f5c\u4e3a\u521b\u5efa\u76f8\u5173\u51fd\u6570\u7684\u6a21\u5f0f\u3002\u5728\u51fd\u6570\u6a21\u677f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 \u6a21\u677f\u7c7b\u578b \u4f5c\u4e3a\u4ee5\u540e\u8981\u6307\u5b9a\u7684\u4efb\u4f55\u7c7b\u578b\u7684\u5360\u4f4d\u7b26\u3002\u544a\u8bc9\u7f16\u8bd1\u5668\u6211\u4eec\u6b63\u5728\u5b9a\u4e49\u4e00\u4e2a\u6a21\u677f\u5e76\u58f0\u660e\u6a21\u677f\u7c7b\u578b\u7684\u8bed\u6cd5\u79f0\u4e3a \u6a21\u677f\u5f62\u53c2\u58f0\u660e \u3002 \u4ece\u51fd\u6570\u6a21\u677f(\u4f7f\u7528\u6a21\u677f\u7c7b\u578b)\u521b\u5efa\u51fd\u6570(\u4f7f\u7528\u7279\u5b9a\u7c7b\u578b)\u7684\u8fc7\u7a0b\u7b80\u79f0\u4e3a \u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316 (\u6216\u5b9e\u4f8b\u5316)\u3002\u5f53\u8fd9\u4e2a\u8fc7\u7a0b\u7531\u4e8e\u51fd\u6570\u8c03\u7528\u800c\u53d1\u751f\u65f6\uff0c\u5b83\u88ab\u79f0\u4e3a\u201c\u9690\u5f0f\u5b9e\u4f8b\u5316\u201d\u3002\u4e00\u4e2a\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u88ab\u79f0\u4e3a \u51fd\u6570\u5b9e\u4f8b (\u6216\u7b80\u79f0 \u5b9e\u4f8b \uff0c\u6709\u65f6\u4e5f\u79f0\u4e3a \u6a21\u677f\u51fd\u6570 )\u3002 \u6a21\u677f\u5b9e\u53c2\u63a8\u5bfc\u5141\u8bb8\u7f16\u8bd1\u5668\u4ece\u51fd\u6570\u8c03\u7528\u7684\u5b9e\u53c2\u63a8\u5bfc\u51fa\u5e94\u8be5\u7528\u4e8e\u5b9e\u4f8b\u5316\u51fd\u6570\u7684\u5b9e\u9645\u7c7b\u578b\u3002\u6a21\u677f\u53c2\u6570\u63a8\u5bfc\u4e0d\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002 \u6a21\u677f\u7c7b\u578b\u6709\u65f6\u88ab\u79f0\u4e3a \u6cdb\u578b\u7c7b\u578b \uff0c\u4f7f\u7528\u6a21\u677f\u7f16\u7a0b\u6709\u65f6\u88ab\u79f0\u4e3a \u6cdb\u578b\u7f16\u7a0b \u3002 \u5728C++ 20\u4e2d\uff0c\u5f53 auto \u5173\u952e\u5b57\u5728\u666e\u901a\u51fd\u6570\u4e2d\u7528\u4f5c\u5f62\u53c2\u7c7b\u578b\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u5c06\u51fd\u6570\u8f6c\u6362\u4e3a\u51fd\u6570\u6a21\u677f\uff0c\u6bcf\u4e2a auto \u5f62\u53c2\u90fd\u6210\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684\u6a21\u677f\u7c7b\u578b\u5f62\u53c2\u3002\u8fd9\u79cd\u521b\u5efa\u51fd\u6570\u6a21\u677f\u7684\u65b9\u6cd5\u79f0\u4e3a \u7f29\u5199\u51fd\u6570\u6a21\u677f \u3002","title":"8.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d"},{"location":"8-x-Chapter-8-summary-and-quiz/#_1","text":"\u5c06\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u7684\u8fc7\u7a0b\u53eb\u505a \u7c7b\u578b\u8f6c\u6362 \u3002 \u5f53\u9700\u8981\u4e00\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u63d0\u4f9b\u4e86\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u65f6\uff0c\u5219\u4f1a\u6267\u884c \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 (\u4e5f\u79f0\u4e3a\u81ea\u52a8\u7c7b\u578b\u8f6c\u6362\u6216\u5f3a\u5236\u8f6c\u6362)\u3002\u5982\u679c\u7f16\u8bd1\u5668\u80fd\u591f\u627e\u5230\u4e24\u79cd\u7c7b\u578b\u4e4b\u95f4\u8fdb\u8fdb\u884c\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u7684\u89c4\u5219\uff0c\u5219\u5b83\u4f1a\u6267\u884c\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\uff0c\u5426\u5219\u5c31\u4f1a\u4ea7\u751f\u7f16\u8bd1\u62a5\u9519\u3002\uff08 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \uff09 C++ \u8bed\u8a00\u5b9a\u4e49\u4e86\u8bb8\u591a\u57fa\u672c\u7c7b\u578b\u4e4b\u95f4\u7684\u5185\u7f6e\u8f6c\u6362\u65b9\u5f0f(\u4ee5\u53ca\u4e00\u4e9b\u66f4\u9ad8\u7ea7\u7684\u7c7b\u578b\u7684\u8f6c\u6362)\uff0c\u79f0\u4e3a \u6807\u51c6\u8f6c\u6362 \u3002\u5176\u4e2d\u5305\u62ec \u6570\u503c\u63d0\u5347 \u3001 \u6570\u503c\u8f6c\u6362 \u548c \u7b97\u6570\u8f6c\u6362 \u6570\u503c\u63d0\u5347 \uff08 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 \uff09\u662f\u5c06\u8f83\u5c0f\u7684\u6570\u503c\u7c7b\u578b\u8f6c\u6362\u4e3a\u8f83\u5927\u7684\u6570\u503c\u7c7b\u578b(\u901a\u5e38\u4e3a int \u6216 double )\uff0c\u8fd9\u6837CPU\u5c31\u53ef\u4ee5\u5bf9\u4e0e\u5904\u7406\u5668\u81ea\u7136\u6570\u636e\u5927\u5c0f\u76f8\u5339\u914d\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002\u6570\u503c\u63d0\u5347\u5305\u62ec\u6574\u6570\u63d0\u5347\u548c\u6d6e\u70b9\u63d0\u5347\u3002\u6570\u5b57\u63d0\u5347\u662f\u4fdd\u503c\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4e0d\u4f1a\u4e22\u5931\u503c\u6216\u503c\u7684\u7cbe\u5ea6\u3002 \u6570\u503c\u8f6c\u6362 \uff08 8.3 - \u6570\u503c\u8f6c\u6362 \uff09\u6570\u503c\u8f6c\u6362\u662f\u57fa\u672c\u7c7b\u578b\u4e4b\u95f4\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u800c\u4e0d\u662f\u6570\u503c\u63d0\u5347\u3002\u6536\u7f29\u8f6c\u6362\u53ef\u80fd\u4f1a\u5bfc\u81f4\u503c\u6216\u7cbe\u5ea6\u7684\u635f\u5931\u3002 \u5728C++\u4e2d\uff0c\u67d0\u4e9b\u4e8c\u5143\u64cd\u4f5c\u7b26\u8981\u6c42\u5b83\u4eec\u7684\u64cd\u4f5c\u6570\u5177\u6709\u76f8\u540c\u7684\u7c7b\u578b\u3002\u5982\u679c\u63d0\u4f9b\u4e86\u4e0d\u540c\u7c7b\u578b\u7684\u64cd\u4f5c\u6570\uff0c\u5219\u4f7f\u7528\u4e00\u7ec4\u79f0\u4e3a \u666e\u901a\u7b97\u672f\u8f6c\u6362 \uff08 8.4 - \u7b97\u672f\u8f6c\u6362 \uff09\u89c4\u5219\u5c06\u5176\u4e2d\u4e00\u4e2a\u6216\u4e24\u4e2a\u64cd\u4f5c\u6570\u9690\u5f0f\u8f6c\u6362\u4e3a\u5339\u914d\u7c7b\u578b\u3002 \u5f53\u7a0b\u5e8f\u5458\u663e\u5f0f\u5730\u901a\u8fc7 cast \u6765\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u79f0\u4e3a \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \uff08 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast \uff09\u3002C++\u652f\u63015\u79cd\u7c7b\u578b\u5f3a\u5236\u8f6c\u6362\uff1a C\u98ce\u683c\u7c7b\u578b\u8f6c\u6362 \u3001 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u3001 const \u7c7b\u578b\u8f6c\u6362 \u3001 \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u548c \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 \u3002\u4e00\u822c\u6765\u8bf4\uff0c\u4f60\u5e94\u8be5\u907f\u514d\u4f7f\u7528\"C\u98ce\u683c\u7c7b\u578b\u8f6c\u6362\u201d\u3001\u201cconst\u7c7b\u578b\u8f6c\u6362\u201d\u548c\u201c\u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362\u201d\u3002 static_cast \u7528\u4e8e\u5c06\u4e00\u4e2a\u503c\u4ece\u4e00\u79cd\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u7c7b\u578b\uff0c\u5b83\u662f\u76ee\u524dC++\u4e2d\u88ab\u6700\u591a\u4f7f\u7528\u7684\u5f3a\u5236\u8f6c\u6362\u3002 typedef \u548c\u7c7b\u578b\u522b\u540d \u5141\u8bb8\u7a0b\u5e8f\u5458\u4e3a\u6570\u636e\u7c7b\u578b\u521b\u5efa\u522b\u540d\u3002\u8fd9\u4e9b\u522b\u540d\u4e0d\u662f\u65b0\u7c7b\u578b\uff0c\u5e76\u4e14\u4e0e\u539f\u7c7b\u578b\u7684\u64cd\u4f5c\u76f8\u540c\u3002Typedefs \u548c\u7c7b\u578b\u522b\u540d\u4e0d\u63d0\u4f9b\u4efb\u4f55\u7c7b\u578b\u5b89\u5168\u673a\u5236\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u8981\u5047\u5b9a\u7c7b\u578b\u522b\u540d\u548c\u5176\u539f\u7c7b\u578b\u662f\u6709\u533a\u522b\u7684\u3002 auto \u5173\u952e\u5b57\u6709\u5f88\u591a\u7528\u9014\u3002\u9996\u5148\uff0c auto \u53ef\u7528\u4e8e\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad( 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad )\uff0c\u5b83\u5c06\u4ece\u53d8\u91cf\u7684\u521d\u59cb\u5316\u503c\u63a8\u65ad\u51fa\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u4f46\u662f\uff0c\u7c7b\u578b\u63a8\u65ad\u4f1a\u4e22\u5f03 const \u548c\u5f15\u7528\uff0c\u6240\u4ee5\u5982\u679c\u9700\u8981\uff0c\u8bf7\u786e\u4fdd\u5c06\u5b83\u4eec\u6dfb\u52a0\u56de\u6765\u3002 auto \u4e5f\u53ef\u4ee5\u7528\u4f5c\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u7684\u63a8\u65ad\uff08 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad \uff09\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u4ece\u51fd\u6570\u7684 return \u8bed\u53e5\u4e2d\u63a8\u65ad\u51fa\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\uff0c\u4e0d\u8fc7\u5bf9\u4e8e\u666e\u901a\u51fd\u6570\u5e94\u8be5\u907f\u514d\u8fd9\u6837\u505a\u3002 auto \u8fd8\u88ab\u7528\u4f5c\u540e\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\u8bed\u6cd5\u3002 \u51fd\u6570\u91cd\u8f7d \uff08 8.9 - \u51fd\u6570\u91cd\u8f7d \uff09\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u76f8\u540c\u7684\u540d\u79f0\u521b\u5efa\u591a\u4e2a\u51fd\u6570\uff0c\u53ea\u8981\u6bcf\u4e2a\u76f8\u540c\u540d\u79f0\u7684\u51fd\u6570\u5177\u6709\u4e0d\u540c\u7684\u5f62\u53c2\u7c7b\u578b\u96c6\u5408\u5373\u53ef(\u6216\u8005\u53ef\u4ee5\u7528\u5176\u4ed6\u65b9\u5f0f\u533a\u5206\u51fd\u6570)\u3002\u8fd9\u6837\u7684\u51fd\u6570\u79f0\u4e3a\u91cd\u8f7d\u51fd\u6570(\u6216\u7b80\u79f0 \u91cd\u8f7d )\u3002\u8fd4\u56de\u7c7b\u578b\u4e0d\u80fd\u88ab\u7528\u6765\u8fdb\u884c\u91cd\u8f7d\u51fd\u6570\u7684\u533a\u5206\u3002 \u5728\u89e3\u6790\u91cd\u8f7d\u51fd\u6570\u65f6\uff08 8.10 - \u51fd\u6570\u91cd\u8f7d\u548c\u533a\u5206 \uff09\uff0c\u5982\u679c\u6ca1\u6709\u627e\u5230\u7cbe\u786e\u5339\u914d\uff0c\u7f16\u8bd1\u5668\u5c06\u4f18\u5148\u9009\u62e9\u53ef\u4ee5\u901a\u8fc7\u6570\u503c\u63d0\u5347\u5339\u914d\u7684\u91cd\u8f7d\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u90a3\u4e9b\u9700\u8981\u6570\u503c\u8f6c\u6362\u7684\u91cd\u8f7d\u51fd\u6570\u3002\u5f53\u5bf9\u5df2\u91cd\u8f7d\u7684\u51fd\u6570\u8fdb\u884c\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u5c1d\u8bd5\u6839\u636e\u51fd\u6570\u8c03\u7528\u4e2d\u4f7f\u7528\u7684\u53c2\u6570\u5c06\u51fd\u6570\u8c03\u7528\u4e0e\u9002\u5f53\u7684\u91cd\u8f7d\u5339\u914d\u3002\u8fd9\u88ab\u79f0\u4e3a \u91cd\u8f7d\u89e3\u6790 \u3002 \u5f53\u7f16\u8bd1\u5668\u53d1\u73b0\u4e24\u4e2a\u6216\u591a\u4e2a\u51fd\u6570\u53ef\u4ee5\u5c06\u51fd\u6570\u8c03\u7528\u4e0e\u91cd\u8f7d\u51fd\u6570\u5339\u914d\uff0c\u4f46\u65e0\u6cd5\u786e\u5b9a\u54ea\u4e00\u4e2a\u662f\u6700\u597d\u7684\u65f6\uff0c\u5c31\u4f1a\u51fa\u73b0 \u4e0d\u660e\u786e\u5339\u914d \uff08 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u4e0d\u660e\u786e\u5339\u914d \uff09\u3002 \u9ed8\u8ba4\u5b9e\u53c2 \u662f\u4e3a\u51fd\u6570\u5f62\u53c2\u63d0\u4f9b\u7684\u9ed8\u8ba4\u503c\uff08 8.12 - \u9ed8\u8ba4\u53c2\u6570 \uff09\u3002\u5e26\u9ed8\u8ba4\u5b9e\u53c2\u7684\u5f62\u53c2\u5fc5\u987b\u59cb\u7ec8\u662f\u6700\u53f3\u8fb9\u7684\u5f62\u53c2\uff0c\u5e76\u4e14\u5728\u89e3\u6790\u91cd\u8f7d\u51fd\u6570\u65f6\u4e0d\u4f7f\u7528\u5b83\u4eec\u6765\u533a\u5206\u51fd\u6570\u3002 \u51fd\u6570\u6a21\u677f\uff08 8.13 - \u51fd\u6570\u6a21\u677f \uff09\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7c7b\u4f3c\u51fd\u6570\u7684\u5b9a\u4e49\uff0c\u4f5c\u4e3a\u521b\u5efa\u76f8\u5173\u51fd\u6570\u7684\u6a21\u5f0f\u3002\u5728\u51fd\u6570\u6a21\u677f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 \u6a21\u677f\u7c7b\u578b \u4f5c\u4e3a\u4ee5\u540e\u8981\u6307\u5b9a\u7684\u4efb\u4f55\u7c7b\u578b\u7684\u5360\u4f4d\u7b26\u3002\u544a\u8bc9\u7f16\u8bd1\u5668\u6211\u4eec\u6b63\u5728\u5b9a\u4e49\u4e00\u4e2a\u6a21\u677f\u5e76\u58f0\u660e\u6a21\u677f\u7c7b\u578b\u7684\u8bed\u6cd5\u79f0\u4e3a \u6a21\u677f\u5f62\u53c2\u58f0\u660e \u3002 \u4ece\u51fd\u6570\u6a21\u677f(\u4f7f\u7528\u6a21\u677f\u7c7b\u578b)\u521b\u5efa\u51fd\u6570(\u4f7f\u7528\u7279\u5b9a\u7c7b\u578b)\u7684\u8fc7\u7a0b\u7b80\u79f0\u4e3a \u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316 (\u6216\u5b9e\u4f8b\u5316)\u3002\u5f53\u8fd9\u4e2a\u8fc7\u7a0b\u7531\u4e8e\u51fd\u6570\u8c03\u7528\u800c\u53d1\u751f\u65f6\uff0c\u5b83\u88ab\u79f0\u4e3a\u201c\u9690\u5f0f\u5b9e\u4f8b\u5316\u201d\u3002\u4e00\u4e2a\u5b9e\u4f8b\u5316\u7684\u51fd\u6570\u88ab\u79f0\u4e3a \u51fd\u6570\u5b9e\u4f8b (\u6216\u7b80\u79f0 \u5b9e\u4f8b \uff0c\u6709\u65f6\u4e5f\u79f0\u4e3a \u6a21\u677f\u51fd\u6570 )\u3002 \u6a21\u677f\u5b9e\u53c2\u63a8\u5bfc\u5141\u8bb8\u7f16\u8bd1\u5668\u4ece\u51fd\u6570\u8c03\u7528\u7684\u5b9e\u53c2\u63a8\u5bfc\u51fa\u5e94\u8be5\u7528\u4e8e\u5b9e\u4f8b\u5316\u51fd\u6570\u7684\u5b9e\u9645\u7c7b\u578b\u3002\u6a21\u677f\u53c2\u6570\u63a8\u5bfc\u4e0d\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002 \u6a21\u677f\u7c7b\u578b\u6709\u65f6\u88ab\u79f0\u4e3a \u6cdb\u578b\u7c7b\u578b \uff0c\u4f7f\u7528\u6a21\u677f\u7f16\u7a0b\u6709\u65f6\u88ab\u79f0\u4e3a \u6cdb\u578b\u7f16\u7a0b \u3002 \u5728C++ 20\u4e2d\uff0c\u5f53 auto \u5173\u952e\u5b57\u5728\u666e\u901a\u51fd\u6570\u4e2d\u7528\u4f5c\u5f62\u53c2\u7c7b\u578b\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u5c06\u51fd\u6570\u8f6c\u6362\u4e3a\u51fd\u6570\u6a21\u677f\uff0c\u6bcf\u4e2a auto \u5f62\u53c2\u90fd\u6210\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684\u6a21\u677f\u7c7b\u578b\u5f62\u53c2\u3002\u8fd9\u79cd\u521b\u5efa\u51fd\u6570\u6a21\u677f\u7684\u65b9\u6cd5\u79f0\u4e3a \u7f29\u5199\u51fd\u6570\u6a21\u677f \u3002","title":"\u590d\u4e60"},{"location":"9-1-Introduction-to-compound-data-types/","tags":["compound data types"],"text":"Key Takeaway C++\u652f\u6301\u4e0b\u5217\u590d\u5408\u6570\u636e\u7ed3\u6784 \u51fd\u6570 Functions \u6570\u7ec4 Arrays \u6307\u9488 Pointer types: \u6307\u5411\u5bf9\u8c61\u7684\u6307\u9488 Pointer to object \u6307\u5411\u51fd\u6570\u7684\u6307\u9488 Pointer to function \u6307\u5411\u6210\u5458\u7684\u6307\u9488 Pointer to member types: \u6307\u5411\u6570\u636e\u6210\u5458\u7684\u6307\u9488 Pointer to data member \u6307\u5411\u6210\u5458\u51fd\u6570\u7684\u6307\u9488 Pointer to member function \u5f15\u7528 Reference types: \u5de6\u503c\u5f15\u7528 L-value references \u53f3\u503c\u5f15\u7528 R-value references \u679a\u4e3e Enumerated types: \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b Unscoped enumerations \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e Scoped enumerations \u7c7b Class types: \u7ed3\u6784\u4f53 Structs \u7c7b Classes \u8054\u5408\u4f53 Unions \u5728 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u57fa\u7840\u6570\u636e\u7c7b\u578b\uff0c\u5b83\u4eec\u662fC++\u8bed\u8a00\u6838\u5fc3\u7684\u4e00\u90e8\u5206\u3002 \u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u4f7f\u7528\u8fd9\u4e9b\u57fa\u7840\u6570\u636e\u7c7b\u578b\u7f16\u5199\u4e86\u4e00\u4e9b\u7a0b\u5e8f\uff0c\u5c24\u5176\u591a\u5730\u7528\u5230\u4e86 int \u3002\u5c3d\u7ba1\u8fd9\u4e9b\u57fa\u672c\u6570\u636e\u7c7b\u578b\u975e\u5e38\u6709\u7528\u4e5f\u975e\u5e38\u6613\u7528\uff0c\u5b83\u4eec\u5e76\u4e0d\u80fd\u6db5\u76d6\u6211\u4eec\u6240\u6709\u7684\u9700\u6c42\uff0c\u5c24\u5176\u662f\u5728\u5f53\u6211\u4eec\u7f16\u5199\u590d\u6742\u7a0b\u5e8f\u7684\u65f6\u5019\u3002 \u4f8b\u5982\uff0c\u5f53\u7f16\u5199\u4e00\u4e2a\u8ba1\u7b97\u7a0b\u5e8f\u5bf9\u4e24\u4e2a\u5206\u6570\u8fdb\u884c\u76f8\u4e58\u7684\u65f6\u5019\uff0c\u4f60\u8be5\u5982\u4f55\u8868\u793a\u5206\u6570\u5462\uff1f\u4f60\u53ef\u80fd\u4f1a\u60f3\u5230\u4f7f\u7528\u4e00\u7ec4\u6574\u578b\uff08\u4e00\u4e2a\u4f5c\u4e3a\u5206\u5b50\uff0c\u4e00\u4e2a\u4f5c\u4e3a\u5206\u6bcd\uff09\uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include int main () { // Our first fraction int num1 {}; int den1 {}; // Our second fraction int num2 {}; int den2 {}; char ignore {}; std :: cout << \"Enter a fraction: \" ; std :: cin >> num1 >> ignore >> den1 ; std :: cout << \"Enter a fraction: \" ; std :: cin >> num2 >> ignore >> den2 ; std :: cout << \"The two fractions multiplied: \" << num1 * num2 << '/' << den1 * den2 ; return 0 ; } \u8fd0\u884c\u7a0b\u5e8f\uff1a 1 2 3 Enter a fraction: 1/2 Enter a fraction: 3/4 The two fractions multiplied: 3/8 \u5c3d\u7ba1\u7a0b\u5e8f\u53ef\u4ee5\u8fd0\u884c\uff0c\u4f46\u662f\u8fd8\u6709\u5f88\u591a\u53ef\u4ee5\u6539\u8fdb\u7684\u5730\u65b9\u3002\u9996\u5148\uff0c\u4e00\u5bf9\u6574\u578b\u6570\u7684\u4e24\u4e2a\u6574\u578b\u7684\u8054\u7cfb\u662f\u5f88\u677e\u6563\u7684\u2014\u2014\u79bb\u5f00\u6ce8\u91ca\u548c\u4e0a\u4e0b\u6587\u4e4b\u540e\uff0c\u4f60\u5f88\u96be\u5c06\u5b83\u4eec\u8054\u7cfb\u8d77\u6765\u3002\u7b2c\u4e8c\uff0c\u4e3a\u4e86\u9075\u5faaDRY\u539f\u5219 \uff0c\u6211\u4eec\u5e94\u8be5\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u4e13\u95e8\u5904\u7406\u5206\u6570\u7684\u8f93\u5165\uff0c\u4f46\u53ef\u60dc\u7684\u51fd\u6570\u53ea\u80fd\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u6240\u4ee5\u6211\u4eec\u6ca1\u529e\u6cd5\u5c06\u5206\u5b50\u5206\u6bcd\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002 \u73b0\u5728\uff0c\u8003\u8651\u8fd9\u6837\u4e00\u79cd\u6e05\u6670\uff0c\u4f60\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u5305\u542b\u5458\u5de5ID\u7684\u5217\u8868\u3002\u4f60\u4f1a\u600e\u4e48\u505a\u5462\uff1f\u4e5f\u8bb8\u662f\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 int main () { int id1 { 42 }; int id2 { 57 }; int id3 { 162 }; // and so on } \u4f46\u662f\u5982\u679c\u4f60\u6709100\u591a\u540d\u5458\u5de5\u600e\u4e48\u529e\uff1f\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b9a\u4e49100\u4e2a\u53d8\u91cf\u540d\u3002\u5176\u6b21\u5982\u679c\u4f60\u9700\u8981\u5c06\u5b83\u4eec\u5168\u90e8\u6253\u5370\u51fa\u6765\u600e\u4e48\u529e\uff1f\u4f60\u53c8\u5982\u4f55\u5c06\u5b83\u4eec\u4f20\u9012\u7ed9\u51fd\u6570\u5462\uff1f\u4f60\u9700\u8981\u6572\u5f88\u591a\u5f88\u591a\u7684\u4ee3\u7801\uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u53ef\u6301\u7eed\u7684\u3002 \u663e\u7136\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u53ea\u80fd\u966a\u6211\u4eec\u8d70\u8fd9\u4e48\u8fdc\u4e86\u3002 \u590d\u5408\u6570\u636e\u7c7b\u578b \u5e78\u8fd0\u7684\u662f\uff0cC++\u652f\u6301\u7b2c\u4e8c\u7c7b\u6570\u636e\u7c7b\u578b\uff0c\u79f0\u4e3a \u590d\u5408\u6570\u636e\u7c7b\u578b \u3002\u590d\u5408\u6570\u636e\u7c7b\u578b\uff08\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a\u7ec4\u5408\u6570\u636e\u7c7b\u578b\uff09\u662f\u5efa\u7acb\u5728\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6216\u5176\u4ed6\u590d\u5408\u6570\u636e\u7c7b\u578b\u4e0a\u7684\u7c7b\u578b\uff0c\u6bcf\u4e2a\u590d\u6742\u6570\u636e\u7c7b\u578b\u90fd\u6709\u5176\u72ec\u7279\u7684\u5c5e\u6027\u3002 \u5728\u672c\u7ae0\u8282\u53ca\u540e\u7eed\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4f1a\u4f7f\u7528\u590d\u6742\u6570\u636e\u7c7b\u578b\u4f18\u96c5\u5730\u89e3\u51b3\u4e0a\u9762\u63d0\u5230\u7684\u90a3\u4e9b\u4ee4\u6211\u4eec\u5934\u75db\u7684\u95ee\u9898\u3002 C++ \u652f\u6301\u4e0b\u5217\u590d\u5408\u6570\u636e\u7c7b\u578b\uff1a \u51fd\u6570 Functions \u6570\u7ec4 Arrays \u6307\u9488 Pointer types: \u6307\u5411\u5bf9\u8c61\u7684\u6307\u9488 Pointer to object \u6307\u5411\u51fd\u6570\u7684\u6307\u9488 Pointer to function \u6307\u5411\u6210\u5458\u7684\u6307\u9488 Pointer to member types: \u6307\u5411\u6570\u636e\u6210\u5458\u7684\u6307\u9488 Pointer to data member \u6307\u5411\u6210\u5458\u51fd\u6570\u7684\u6307\u9488 Pointer to member function \u5f15\u7528 Reference types: \u5de6\u503c\u5f15\u7528 L-value references \u53f3\u503c\u5f15\u7528 R-value references \u679a\u4e3e Enumerated types: \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b Unscoped enumerations \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e Scoped enumerations \u7c7b Class types: \u7ed3\u6784\u4f53 Structs \u7c7b Classes \u8054\u5408\u4f53 Unions \u6211\u4eec\u5176\u5b9e\u5df2\u7ecf\u5728\u9891\u7e41\u4f7f\u7528\u4e00\u79cd\u590d\u5408\u7c7b\u578b\u2014\u2014\u51fd\u6570\u3002\u4f8b\u5982\uff1a 1 2 3 void doSomething ( int x , double y ) { } \u8fd9\u4e2a\u51fd\u6570\u7684\u7c7b\u578b\u662f void(int, double) \u6ce8\u610f\uff0c\u8fd9\u4e2a\u7c7b\u578b\u5305\u542b\u4e86\u591a\u4e2a\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u4ece\u800c\u5f62\u6210\u4e86\u590d\u5408\u7c7b\u578b\u3002\u5f53\u7136\uff0c\u51fd\u6570\u4e5f\u6709\u5176\u7279\u6b8a\u7684\u884c\u4e3a\uff08\u4f8b\u5982\uff1a\u53ef\u4ee5\u88ab\u8c03\u7528\uff09\u3002 \u5173\u4e8e\u8fd9\u4e00\u8bdd\u9898\u6709\u5f88\u591a\u5185\u5bb9\u9700\u8981\u4ecb\u7ecd\uff0c\u56e0\u6b64\u6211\u4eec\u4f1a\u4f7f\u7528\u4e09\u4e2a\u7ae0\u8282\u6765\u4ecb\u7ecd\u3002\u5728\u672c\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ecb\u7ecd\u4e00\u4e9b\u7b80\u5355\u76f4\u89c2\u7684\u590d\u5408\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec \u5de6\u503c\u5f15\u7528 \u548c\u6307\u9488\u3002\u5728\u4e0b\u4e00\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5219\u4f1a\u4ecb\u7ecd \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u3001 \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e \u548c\u57fa\u672c\u7684\u7ed3\u6784\u4f53\u3002\u7136\u540e\uff0c\u518d\u4e0b\u4e00\u7ae0\u8282\u6211\u4eec\u4f1a\u66f4\u52a0\u6df1\u5165\u5730\u4ecb\u7ecd\u975e\u5e38\u6709\u7528\u7684\u6570\u7ec4\u7c7b\u578b\uff0c\u5305\u62ec std::string ( 4.17 - std::string \u7b80\u4ecb )\uff0c\u5b83\u5176\u5b9e\u662f\u4e00\u79cdclass\u7c7b\u578b\u3002 \u51c6\u5907\u597d\u4e86\u5417\uff01Let\u2019s go!","title":"9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b"},{"location":"9-1-Introduction-to-compound-data-types/#_1","text":"\u5e78\u8fd0\u7684\u662f\uff0cC++\u652f\u6301\u7b2c\u4e8c\u7c7b\u6570\u636e\u7c7b\u578b\uff0c\u79f0\u4e3a \u590d\u5408\u6570\u636e\u7c7b\u578b \u3002\u590d\u5408\u6570\u636e\u7c7b\u578b\uff08\u6709\u65f6\u5019\u4e5f\u79f0\u4e3a\u7ec4\u5408\u6570\u636e\u7c7b\u578b\uff09\u662f\u5efa\u7acb\u5728\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6216\u5176\u4ed6\u590d\u5408\u6570\u636e\u7c7b\u578b\u4e0a\u7684\u7c7b\u578b\uff0c\u6bcf\u4e2a\u590d\u6742\u6570\u636e\u7c7b\u578b\u90fd\u6709\u5176\u72ec\u7279\u7684\u5c5e\u6027\u3002 \u5728\u672c\u7ae0\u8282\u53ca\u540e\u7eed\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4f1a\u4f7f\u7528\u590d\u6742\u6570\u636e\u7c7b\u578b\u4f18\u96c5\u5730\u89e3\u51b3\u4e0a\u9762\u63d0\u5230\u7684\u90a3\u4e9b\u4ee4\u6211\u4eec\u5934\u75db\u7684\u95ee\u9898\u3002","title":"\u590d\u5408\u6570\u636e\u7c7b\u578b"},{"location":"9-1-Introduction-to-compound-data-types/#c","text":"\u51fd\u6570 Functions \u6570\u7ec4 Arrays \u6307\u9488 Pointer types: \u6307\u5411\u5bf9\u8c61\u7684\u6307\u9488 Pointer to object \u6307\u5411\u51fd\u6570\u7684\u6307\u9488 Pointer to function \u6307\u5411\u6210\u5458\u7684\u6307\u9488 Pointer to member types: \u6307\u5411\u6570\u636e\u6210\u5458\u7684\u6307\u9488 Pointer to data member \u6307\u5411\u6210\u5458\u51fd\u6570\u7684\u6307\u9488 Pointer to member function \u5f15\u7528 Reference types: \u5de6\u503c\u5f15\u7528 L-value references \u53f3\u503c\u5f15\u7528 R-value references \u679a\u4e3e Enumerated types: \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b Unscoped enumerations \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e Scoped enumerations \u7c7b Class types: \u7ed3\u6784\u4f53 Structs \u7c7b Classes \u8054\u5408\u4f53 Unions \u6211\u4eec\u5176\u5b9e\u5df2\u7ecf\u5728\u9891\u7e41\u4f7f\u7528\u4e00\u79cd\u590d\u5408\u7c7b\u578b\u2014\u2014\u51fd\u6570\u3002\u4f8b\u5982\uff1a 1 2 3 void doSomething ( int x , double y ) { } \u8fd9\u4e2a\u51fd\u6570\u7684\u7c7b\u578b\u662f void(int, double) \u6ce8\u610f\uff0c\u8fd9\u4e2a\u7c7b\u578b\u5305\u542b\u4e86\u591a\u4e2a\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u4ece\u800c\u5f62\u6210\u4e86\u590d\u5408\u7c7b\u578b\u3002\u5f53\u7136\uff0c\u51fd\u6570\u4e5f\u6709\u5176\u7279\u6b8a\u7684\u884c\u4e3a\uff08\u4f8b\u5982\uff1a\u53ef\u4ee5\u88ab\u8c03\u7528\uff09\u3002 \u5173\u4e8e\u8fd9\u4e00\u8bdd\u9898\u6709\u5f88\u591a\u5185\u5bb9\u9700\u8981\u4ecb\u7ecd\uff0c\u56e0\u6b64\u6211\u4eec\u4f1a\u4f7f\u7528\u4e09\u4e2a\u7ae0\u8282\u6765\u4ecb\u7ecd\u3002\u5728\u672c\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ecb\u7ecd\u4e00\u4e9b\u7b80\u5355\u76f4\u89c2\u7684\u590d\u5408\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec \u5de6\u503c\u5f15\u7528 \u548c\u6307\u9488\u3002\u5728\u4e0b\u4e00\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5219\u4f1a\u4ecb\u7ecd \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u3001 \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e \u548c\u57fa\u672c\u7684\u7ed3\u6784\u4f53\u3002\u7136\u540e\uff0c\u518d\u4e0b\u4e00\u7ae0\u8282\u6211\u4eec\u4f1a\u66f4\u52a0\u6df1\u5165\u5730\u4ecb\u7ecd\u975e\u5e38\u6709\u7528\u7684\u6570\u7ec4\u7c7b\u578b\uff0c\u5305\u62ec std::string ( 4.17 - std::string \u7b80\u4ecb )\uff0c\u5b83\u5176\u5b9e\u662f\u4e00\u79cdclass\u7c7b\u578b\u3002 \u51c6\u5907\u597d\u4e86\u5417\uff01Let\u2019s go!","title":"C++ \u652f\u6301\u4e0b\u5217\u590d\u5408\u6570\u636e\u7c7b\u578b\uff1a"},{"location":"9-10-Pass-by-address-part-2/","tags":["pointer","address"],"text":"\u672c\u8282\u8bfe\u7ee7\u7eed 9.9 - \u6309\u5730\u5740\u4f20\u9012 \u7684\u5185\u5bb9\u3002 \u201c\u53ef\u9009\u201d\u53c2\u6570\u6309\u5730\u5740\u4f20\u9012 \u4f20\u9012\u5730\u5740\u7684\u4e00\u4e2a\u66f4\u5e38\u89c1\u7684\u7528\u9014\u662f\u5141\u8bb8\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u201c\u53ef\u9009\u201d\u53c2\u6570\u3002\u4e3e\u4f8b\u8bf4\u660e\u6bd4\u63cf\u8ff0\u66f4\u5bb9\u6613\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include void greet ( std :: string * name = nullptr ) { std :: cout << \"Hello \" ; std :: cout << ( name ? * name : \"guest\" ) << '\\n' ; } int main () { greet (); // we don't know who the user is yet std :: string joe { \"Joe\" }; greet ( & joe ); // we know the user is joe return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 Hello guest Hello Joe \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c greet() \u51fd\u6570\u7684\u53c2\u6570\u662f\u6309\u5730\u5740\u4f20\u9012\u7684\uff0c\u4e14\u5177\u6709\u9ed8\u8ba4\u503c nullptr \u3002\u5728 main() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4e24\u6b21\u8c03\u7528\u8be5\u51fd\u6570\u3002\u7b2c\u4e00\u6b21\u8c03\u7528\u65f6\uff0c\u6211\u4eec\u4e0d\u77e5\u9053\u7528\u6237\u662f\u8c01\uff0c\u6240\u4ee5\u8c03\u7528 greet() \u65f6\u6ca1\u6709\u4f20\u9012\u4efb\u4f55 \u5b9e\u53c2 \uff0c\u6b64\u65f6\u53c2\u6570 name \u4f7f\u7528\u9ed8\u8ba4\u503c nullptr \uff0c\u51fd\u6570\u6253\u5370 \u201cguest\u201d\u3002 \u7b2c\u4e8c\u6b21\u8c03\u7528\u65f6\uff0c\u6211\u4eec\u4f20\u9012\u4e86\u4e00\u4e2a\u6709\u6548\u7684\u7528\u6237\u2014\u2014 greet(&joe) \u3002\u6b64\u65f6 name \u53c2\u6570\u901a\u8fc7\u5730\u5740\u5f97\u5230\u4e86 joe \uff0c\u51fd\u6570\u4f7f\u7528\u5b83\u6253\u5370\u4e86\u7528\u6237\u540d \u201cJoe\u201d\u3002 \u7136\u800c\uff0c\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u51fd\u6570\u91cd\u8f7d\u662f\u5b9e\u73b0\u76f8\u540c\u7ed3\u679c\u7684\u66f4\u597d\u9009\u62e9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include #include void greet ( std :: string_view name ) { std :: cout << \"Hello \" << name << '\\n' ; } void greet () { greet ( \"guest\" ); } int main () { greet (); // we don't know who the user is yet std :: string joe { \"Joe\" }; greet ( joe ); // we know the user is joe return 0 ; } \u8fd9\u6837\u505a\u6709\u5f88\u591a\u597d\u5904\uff1a\u6211\u4eec\u4e0d\u518d\u9700\u8981\u62c5\u5fc3 \u89e3\u5f15\u7528 \u7a7a\u6307\u9488\uff0c\u800c\u4e14\u5982\u679c\u9700\u8981\u7684\u8bdd\uff0c\u6211\u4eec\u53ef\u4ee5\u4f20\u5165\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u3002 \u6539\u53d8\u6307\u9488\u53c2\u6570\u6307\u5411\u7684\u503c \u5f53\u5c06\u5730\u5740\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\uff0c\u8be5\u5730\u5740\u5c06\u4ece\u5b9e\u53c2\u590d\u5236\u5230\u6307\u9488\u5f62\u53c2\u4e2d(\u8fd9\u5f88\u597d\uff0c\u56e0\u4e3a\u590d\u5236\u5730\u5740\u975e\u5e38\u5feb)\u3002\u73b0\u5728\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include // [[maybe_unused]] gets rid of compiler warnings about ptr2 being set but not used void nullify ([[ maybe_unused ]] int * ptr2 ) { ptr2 = nullptr ; // Make the function parameter a null pointer } int main () { int x { 5 }; int * ptr { & x }; // ptr points to x std :: cout << \"ptr is \" << ( ptr ? \"non-null \\n \" : \"null \\n \" ); nullify ( ptr ); std :: cout << \"ptr is \" << ( ptr ? \"non-null \\n \" : \"null \\n \" ); return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 ptr is non-null ptr is non-null \u5982\u4f60\u6240\u89c1\uff0c\u4fee\u6539 \u5f62\u53c2 \u6307\u9488\u7684\u5730\u5740\u5bf9 \u5b9e\u53c2 \u6ca1\u6709\u5f71\u54cd( ptr \u4ecd\u7136\u6307\u5411 x )\u3002\u5f53\u8c03\u7528\u51fd\u6570 nullify() \u88ab\u8c03\u7528\u65f6 ptr2 \u62f7\u8d1d\u4e86\u4e00\u4efd ptr \u6240\u6301\u6709\u7684\u5730\u5740\uff08\u6307\u5411 x \uff09\u3002\u5f53\u51fd\u6570\u4fee\u6539 ptr2 \u7684\u503c\u65f6\uff0c\u5b83\u53ea\u662f\u4fee\u6539\u4e86\u8fd9\u4efd\u62f7\u8d1d\u7684\u526f\u672c\u7f62\u4e86\u3002 \u5982\u679c\u6211\u4eec\u60f3\u8ba9\u51fd\u6570\u6539\u53d8\u6307\u9488\u53c2\u6570\u7684\u6307\u5411\uff0c\u8be5\u600e\u4e48\u529e\u5462\uff1f \u901a\u8fc7\u5f15\u7528\u4f20\u5730\u5740\uff1fPass by address\u2026 by reference? \u662f\u7684\uff0c\u53ef\u4ee5\u8fd9\u4e48\u505a\u3002\u5c31\u50cf\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u666e\u901a\u53d8\u91cf\u4e00\u6837\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7 \u6309\u5f15\u7528\u4f20\u9012 \u6307\u9488\u3002 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5f15\u7528\u4f20\u9012 ptr2 \u6307\u5411\u7684\u5730\u5740\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void nullify ( int *& refptr ) // refptr is now a reference to a pointer { refptr = nullptr ; // Make the function parameter a null pointer } int main () { int x { 5 }; int * ptr { & x }; // ptr points to x std :: cout << \"ptr is \" << ( ptr ? \"non-null \\n \" : \"null \\n \" ); nullify ( ptr ); std :: cout << \"ptr is \" << ( ptr ? \"non-null \\n \" : \"null \\n \" ); return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 2 ptr is non-null ptr is null \u56e0\u4e3a refptr \u662f\u4e00\u4e2a\u6307\u9488\u7684\u5f15\u7528\uff0c\u6240\u4ee5\u5f53 ptr \u4f20\u5165\u65f6\uff0c refptr \u88ab\u7ed1\u5b9a ptr \u3002\u8fd9\u610f\u5473\u7740\u4efb\u4f55\u5bf9 refptr \u7684\u4fee\u6539\u90fd\u4f1a\u4f5c\u7528\u4e8e ptr \u3002 \u9898\u5916\u8bdd \u56e0\u4e3a\u5bf9\u6307\u9488\u7684\u5f15\u7528\u76f8\u5f53\u7f55\u89c1\uff0c\u5176\u8bed\u6cd5\u5f88\u5bb9\u6613\u6df7\u6dc6(\u5b83\u662f int*& \u8fd8\u662f int&* ?)\u597d\u6d88\u606f\u662f\uff0c\u5982\u679c\u53cd\u5411\u64cd\u4f5c\uff0c\u7f16\u8bd1\u5668\u5c06\u51fa\u9519\uff0c\u56e0\u4e3a\u4e0d\u80fd\u6709\u6307\u5411\u5f15\u7528\u7684\u6307\u9488(\u56e0\u4e3a\u6307\u9488\u5fc5\u987b\u4fdd\u5b58\u5bf9\u8c61\u7684\u5730\u5740\uff0c\u800c\u5f15\u7528\u4e0d\u662f\u5bf9\u8c61)\u3002\u7136\u540e\u4f60\u53ef\u4ee5\u628a\u5b83\u6362\u8fc7\u6765\u3002 \u4e3a\u4ec0\u4e48\u73b0\u5728\u4e0d\u63a8\u8350\u4f7f\u7528 0 \u6216 NULL \u8868\u793a\u7a7a\u6307\u9488\u4e86\uff1f\uff08\u9009\u8bfb\uff09 \u5728\u672c\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u89e3\u91ca\u4e3a\u4ec0\u4e48\u4e0d\u518d\u4f7f\u7528 0 \u6216 NULL \u3002 \u5b57\u9762\u91cf\u201c 0 \u201d\u65e2\u53ef\u4ee5\u88ab\u89e3\u91ca\u4e3a\u6574\u6570 \u5b57\u9762\u91cf \uff0c\u4e5f\u53ef\u4ee5\u88ab\u89e3\u91ca\u4e3a\u7a7a\u6307\u9488\u5b57\u9762\u91cf\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60f3\u8981\u533a\u5206\u5176\u51c6\u786e\u542b\u4e49\u5e76\u4e0d\u5bb9\u6613\u2014\u2014\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u8ba4\u4e3a\u6211\u4eec\u6307\u7684\u662f\u5176\u4e2d\u4e00\u4e2a\u542b\u4e49\uff0c\u800c\u6211\u4eec\u6307\u7684\u662f\u53e6\u4e00\u4e2a\u2014\u2014\u8fd9\u4f1a\u7ed9\u7a0b\u5e8f\u7684\u884c\u4e3a\u5e26\u6765\u610f\u60f3\u4e0d\u5230\u7684\u540e\u679c\u3002 \u9884\u5904\u7406\u5668 \u5b8f NULL \u5e76\u6ca1\u6709\u5728\u6807\u51c6\u4e2d\u660e\u786e\u5b9a\u4e49\uff0c\u5b83\u7684\u503c\u53ef\u80fd\u662f 0 , 0L , ((void*)0) , \u6216\u5176\u4ed6\u503c\u3002 \u5728\u8bfe\u7a0b 8.9 - \u51fd\u6570\u91cd\u8f7d \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u51fd\u6570 \u91cd\u8f7d (\u591a\u4e2a\u51fd\u6570\u53ef\u4ee5\u5177\u6709\u76f8\u540c\u7684\u540d\u79f0\uff0c\u53ea\u8981\u5b83\u4eec\u53ef\u4ee5\u901a\u8fc7\u5f62\u53c2\u7684\u6570\u91cf\u6216\u7c7b\u578b\u6765\u533a\u5206)\u3002\u7f16\u8bd1\u5668\u53ef\u4ee5\u901a\u8fc7\u4f5c\u4e3a\u51fd\u6570\u8c03\u7528\u4e00\u90e8\u5206\u4f20\u5165\u7684\u53c2\u6570\u6765\u786e\u5b9a\u60a8\u60f3\u8981\u54ea\u4e2a\u91cd\u8f7d\u51fd\u6570\u3002 \u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f7f\u7528 0 \u6216 NULL \u65f6\u53ef\u80fd\u5e26\u6765\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include #include // \u5b9a\u4e49\u4e86 NULL void print ( int x ) // \u51fd\u6570\u63a5\u53d7\u6574\u6570 { std :: cout << \"print(int): \" << x << '\\n' ; } void print ( int * ptr ) // \u63a5\u53d7\u4e00\u4e2a\u6574\u578b\u6307\u9488 { std :: cout << \"print(int*): \" << ( ptr ? \"non-null \\n \" : \"null \\n \" ); } int main () { int x { 5 }; int * ptr { & x }; print ( ptr ); // \u603b\u662f\u8c03\u7528 print(int*) \u56e0\u4e3aptr\u7c7b\u578b\u4e3a int* (good) print ( 0 ); // \u603b\u662f\u8c03\u7528 print(int) \u56e0\u4e3a 0 \u662f\u4e00\u4e2a\u6574\u578b\u5b57\u9762\u91cf (\u4f46\u613f\u8fd9\u662f\u6211\u4eec\u6240\u5e0c\u671b\u7684) print ( NULL ); // \u8fd9\u4e2a\u8bed\u53e5\u53ef\u80fd\u505a\u4ee5\u4e0b\u4efb\u4f55\u4e8b\uff1a // \u8c03\u7528 print(int) (Visual Studio \u4f1a\u8fd9\u4e48\u505a) // \u8c03\u7528 print(int*) // \u5bfc\u81f4\u6b67\u4e49\uff0c\u5f15\u53d1\u7f16\u8bd1\u9519\u8bef(gcc \u548c Clang \u4f1a\u8fd9\u4e48\u505a) print ( nullptr ); // \u603b\u662f\u8c03\u7528 print(int*) return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a(\u4f7f\u7528 Visual Studio)\uff0c\u6253\u5370\uff1a 1 2 3 4 print(int*): non-null print(int): 0 print(int): 0 print(int*): null \u5f53\u4f20\u9012\u53c2\u65700\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u4f18\u5148\u8c03\u7528 print(int) \u800c\u4e0d\u662f print(int*) \uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u95ee\u9898\uff0c\u5982\u679c\u6211\u4eec\u7684\u672c\u610f\u662f\u5e0c\u671b\u4f7f\u7528\u7a7a\u6307\u9488\u5e76\u8c03\u7528 print(int*) \u7684\u8bdd\u3002 \u5728 NULL \u88ab\u5b9a\u4e49\u4e3a\u503c 0 \u7684\u60c5\u51b5\u4e0b\uff0c print(NULL) \u4e5f\u4f1a\u8c03\u7528 print(int) \uff0c\u800c\u4e0d\u662f print(int*) \uff0c\u5c31\u50cf\u4f60\u671f\u671b\u7684NULL\u6307\u9488\u5b57\u9762\u91cf\u4e00\u6837\u3002\u5728 NULL \u6ca1\u6709\u5b9a\u4e49\u4e3a 0 \u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5176\u4ed6\u884c\u4e3a\uff0c\u5982\u8c03\u7528 print(int*) \u6216\u7f16\u8bd1\u9519\u8bef\u3002 \u4f7f\u7528 nullptr \u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u4e8c\u4e49\u6027(\u5b83\u603b\u662f\u4f1a\u5bfc\u81f4\u51fd\u6570 print(int*) \u88ab\u8c03\u7528)\uff0c\u56e0\u4e3a nullptr \u53ea\u80fd\u5339\u914d\u6307\u9488\u7c7b\u578b\u3002 std::nullptr_t (\u9009\u8bfb) \u56e0\u4e3a nullptr \u53ef\u4ee5\u5728\u91cd\u8f7d\u51fd\u6570\u4e2d\u533a\u5206\u4e0e\u6574\u578b\u503c\uff0c\u6240\u4ee5\u5b83\u5fc5\u5b9a\u5c5e\u4e8e\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\u3002\u90a3\u4e48\uff0c nullptr \u662f\u4ec0\u4e48\u7c7b\u578b\u5462\uff1f\u5b9e\u9645\u4e0a\uff0c nullptr \u7684\u7c7b\u578b\u4e3a std::nullptr_t (\u5b9a\u4e49\u5728 )\u3002 std::nullptr_t \u53ea\u80fd\u4fdd\u5b58\u4e00\u4e2a\u503c\uff1a nullptr \uff01\u5c3d\u7ba1\u6709\u4eba\u89c9\u5f97\u8fd9\u5f88\u8822\uff0c\u4f46\u662f\u6709\u4e00\u79cd\u60c5\u51b5\u4e0b\u662f\u6709\u7528\u7684\u3002\u5982\u679c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u5e76\u4e14\u53ea\u63a5\u6536 nullptr \u5b57\u9762\u91cf \u5b9e\u53c2\uff0c\u5219\u53ef\u4ee5\u8ba9\u5f62\u53c2\u4e3a std::nullptr_t \u7c7b\u578b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include // for std::nullptr_t void print ( std :: nullptr_t ) { std :: cout << \"in print(std::nullptr_t) \\n \" ; } void print ( int * ) { std :: cout << \"in print(int*) \\n \" ; } int main () { print ( nullptr ); // calls print(std::nullptr_t) int x { 5 }; int * ptr { & x }; print ( ptr ); // calls print(int*) ptr = nullptr ; print ( ptr ); // calls print(int*) (since ptr has type int*) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570\u8c03\u7528 print(nullptr) \u4f1a\u88ab\u89e3\u6790\u4e3a print(std::nullptr_t) \u800c\u4e0d\u662f print(int*) \uff0c\u56e0\u4e3a\u524d\u8005\u65e0\u9700\u7c7b\u578b\u8f6c\u6362\u5373\u53ef\u5339\u914d\u3002 \u6709\u4e00\u79cd\u60c5\u51b5\u53ef\u80fd\u4f1a\u8ba9\u4eba\u6709\u70b9\u56f0\u60d1\uff0c\u90a3\u5c31\u662f\u5f53 ptr \u503c\u4e3a nullptr \u65f6\u8c03\u7528 print(ptr) \u3002\u8bb0\u4f4f\uff0c\u51fd\u6570\u91cd\u8f7d\u5339\u914d\u7684\u662f\u7c7b\u578b\uff0c\u800c\u4e0d\u662f\u503c\uff0c ptr \u7684\u7c7b\u578b\u662f int* \u3002\u56e0\u6b64\uff0c print(int*) \u5c06\u88ab\u5339\u914d\u3002 print(std::nullptr_t) \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u751a\u81f3\u4e0d\u9700\u8981\u8003\u8651\uff0c\u56e0\u4e3a\u6307\u9488\u7c7b\u578b\u4e0d\u4f1a\u9690\u5f0f\u8f6c\u6362\u4e3a std::nullptr_t \u3002 \u4f60\u53ef\u80fd\u6c38\u8fdc\u90fd\u4e0d\u9700\u8981\u4f7f\u7528\u5b83\uff0c\u4f46\u4e3a\u4e86\u4ee5\u9632\u4e07\u4e00\uff0c\u77e5\u9053\u5b83\u662f\u5f88\u597d\u7684\u3002 \u5176\u5b9e\u90fd\u662f\u6309\u503c\u4f20\u9012 \u73b0\u5728\u60a8\u5df2\u7ecf\u7406\u89e3\u4e86\u901a\u8fc7\u5f15\u7528\u3001\u5730\u5740\u548c\u503c\u4f20\u9012\u4e4b\u95f4\u7684\u57fa\u672c\u533a\u522b\uff0c\u8ba9\u6211\u4eec\u6682\u65f6\u7b80\u5316\u4e00\u4e0b\u3002\uff1a\uff09 \u867d\u7136\u7f16\u8bd1\u5668\u901a\u5e38\u53ef\u4ee5\u5c06\u5f15\u7528\u4f18\u5316\u6389\uff0c\u4f46\u5728\u6709\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8fd9\u662f\u4e0d\u53ef\u80fd\u7684\uff0c\u800c\u5f15\u7528\u5b9e\u9645\u4e0a\u662f\u9700\u8981\u7684\u3002\u5f15\u7528\u901a\u5e38\u7531\u7f16\u8bd1\u5668\u4f7f\u7528\u6307\u9488\u5b9e\u73b0\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5728\u5e55\u540e\uff0c\u5f15\u7528\u4f20\u9012\u672c\u8d28\u4e0a\u53ea\u662f\u5730\u5740\u4f20\u9012(\u5bf9\u5f15\u7528\u7684\u8bbf\u95ee\u6267\u884c\u9690\u5f0f\u89e3\u5f15\u7528)\u3002 \u5728\u4e0a\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u63d0\u5230\u8fc7\uff0c\u901a\u8fc7\u5730\u5740\u4f20\u9012\u53ea\u662f\u5c06\u4e00\u4e2a\u5730\u5740\u4ece\u8c03\u7528\u8005\u590d\u5236\u5230\u88ab\u8c03\u7528\u51fd\u6570\u2014\u2014\u5b83\u53ea\u662f\u6309\u503c\u4f20\u9012\u4e00\u4e2a\u5730\u5740\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u8fd9\u6837\u7684\u7ed3\u8bba\uff1aC++\u786e\u5b9e\u662f\u6309\u503c\u4f20\u9012\u6240\u6709\u5185\u5bb9\u7684\uff01 \u6309\u5730\u5740\u4f20\u9012 \u6216 \u6309\u5f15\u7528\u4f20\u9012 \u7684\u53c2\u6570\uff0c\u5176\u7279\u522b\u4e4b\u5904\u53ea\u5728\u4e8e\u4f20\u9012\u8fdb\u6765\u7684\u5730\u5740\u53ef\u4ee5\u88ab\u89e3\u5f15\u7528\uff0c\u6240\u4ee5\u80fd\u901a\u8fc7\u5b83\u4fee\u6539\u5b9e\u53c2\uff0c\u800c\u666e\u901a\u5f62\u53c2\u4e0d\u80fd\u505a\u5230\u8fd9\u4e00\u70b9\u3002","title":"9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2"},{"location":"9-10-Pass-by-address-part-2/#_1","text":"\u4f20\u9012\u5730\u5740\u7684\u4e00\u4e2a\u66f4\u5e38\u89c1\u7684\u7528\u9014\u662f\u5141\u8bb8\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u201c\u53ef\u9009\u201d\u53c2\u6570\u3002\u4e3e\u4f8b\u8bf4\u660e\u6bd4\u63cf\u8ff0\u66f4\u5bb9\u6613\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include void greet ( std :: string * name = nullptr ) { std :: cout << \"Hello \" ; std :: cout << ( name ? * name : \"guest\" ) << '\\n' ; } int main () { greet (); // we don't know who the user is yet std :: string joe { \"Joe\" }; greet ( & joe ); // we know the user is joe return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 Hello guest Hello Joe \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c greet() \u51fd\u6570\u7684\u53c2\u6570\u662f\u6309\u5730\u5740\u4f20\u9012\u7684\uff0c\u4e14\u5177\u6709\u9ed8\u8ba4\u503c nullptr \u3002\u5728 main() \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4e24\u6b21\u8c03\u7528\u8be5\u51fd\u6570\u3002\u7b2c\u4e00\u6b21\u8c03\u7528\u65f6\uff0c\u6211\u4eec\u4e0d\u77e5\u9053\u7528\u6237\u662f\u8c01\uff0c\u6240\u4ee5\u8c03\u7528 greet() \u65f6\u6ca1\u6709\u4f20\u9012\u4efb\u4f55 \u5b9e\u53c2 \uff0c\u6b64\u65f6\u53c2\u6570 name \u4f7f\u7528\u9ed8\u8ba4\u503c nullptr \uff0c\u51fd\u6570\u6253\u5370 \u201cguest\u201d\u3002 \u7b2c\u4e8c\u6b21\u8c03\u7528\u65f6\uff0c\u6211\u4eec\u4f20\u9012\u4e86\u4e00\u4e2a\u6709\u6548\u7684\u7528\u6237\u2014\u2014 greet(&joe) \u3002\u6b64\u65f6 name \u53c2\u6570\u901a\u8fc7\u5730\u5740\u5f97\u5230\u4e86 joe \uff0c\u51fd\u6570\u4f7f\u7528\u5b83\u6253\u5370\u4e86\u7528\u6237\u540d \u201cJoe\u201d\u3002 \u7136\u800c\uff0c\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u51fd\u6570\u91cd\u8f7d\u662f\u5b9e\u73b0\u76f8\u540c\u7ed3\u679c\u7684\u66f4\u597d\u9009\u62e9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include #include void greet ( std :: string_view name ) { std :: cout << \"Hello \" << name << '\\n' ; } void greet () { greet ( \"guest\" ); } int main () { greet (); // we don't know who the user is yet std :: string joe { \"Joe\" }; greet ( joe ); // we know the user is joe return 0 ; } \u8fd9\u6837\u505a\u6709\u5f88\u591a\u597d\u5904\uff1a\u6211\u4eec\u4e0d\u518d\u9700\u8981\u62c5\u5fc3 \u89e3\u5f15\u7528 \u7a7a\u6307\u9488\uff0c\u800c\u4e14\u5982\u679c\u9700\u8981\u7684\u8bdd\uff0c\u6211\u4eec\u53ef\u4ee5\u4f20\u5165\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u3002","title":"\u201c\u53ef\u9009\u201d\u53c2\u6570\u6309\u5730\u5740\u4f20\u9012"},{"location":"9-10-Pass-by-address-part-2/#_2","text":"\u5f53\u5c06\u5730\u5740\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\uff0c\u8be5\u5730\u5740\u5c06\u4ece\u5b9e\u53c2\u590d\u5236\u5230\u6307\u9488\u5f62\u53c2\u4e2d(\u8fd9\u5f88\u597d\uff0c\u56e0\u4e3a\u590d\u5236\u5730\u5740\u975e\u5e38\u5feb)\u3002\u73b0\u5728\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include // [[maybe_unused]] gets rid of compiler warnings about ptr2 being set but not used void nullify ([[ maybe_unused ]] int * ptr2 ) { ptr2 = nullptr ; // Make the function parameter a null pointer } int main () { int x { 5 }; int * ptr { & x }; // ptr points to x std :: cout << \"ptr is \" << ( ptr ? \"non-null \\n \" : \"null \\n \" ); nullify ( ptr ); std :: cout << \"ptr is \" << ( ptr ? \"non-null \\n \" : \"null \\n \" ); return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 ptr is non-null ptr is non-null \u5982\u4f60\u6240\u89c1\uff0c\u4fee\u6539 \u5f62\u53c2 \u6307\u9488\u7684\u5730\u5740\u5bf9 \u5b9e\u53c2 \u6ca1\u6709\u5f71\u54cd( ptr \u4ecd\u7136\u6307\u5411 x )\u3002\u5f53\u8c03\u7528\u51fd\u6570 nullify() \u88ab\u8c03\u7528\u65f6 ptr2 \u62f7\u8d1d\u4e86\u4e00\u4efd ptr \u6240\u6301\u6709\u7684\u5730\u5740\uff08\u6307\u5411 x \uff09\u3002\u5f53\u51fd\u6570\u4fee\u6539 ptr2 \u7684\u503c\u65f6\uff0c\u5b83\u53ea\u662f\u4fee\u6539\u4e86\u8fd9\u4efd\u62f7\u8d1d\u7684\u526f\u672c\u7f62\u4e86\u3002 \u5982\u679c\u6211\u4eec\u60f3\u8ba9\u51fd\u6570\u6539\u53d8\u6307\u9488\u53c2\u6570\u7684\u6307\u5411\uff0c\u8be5\u600e\u4e48\u529e\u5462\uff1f","title":"\u6539\u53d8\u6307\u9488\u53c2\u6570\u6307\u5411\u7684\u503c"},{"location":"9-10-Pass-by-address-part-2/#pass-by-address-by-reference","text":"\u662f\u7684\uff0c\u53ef\u4ee5\u8fd9\u4e48\u505a\u3002\u5c31\u50cf\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u666e\u901a\u53d8\u91cf\u4e00\u6837\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7 \u6309\u5f15\u7528\u4f20\u9012 \u6307\u9488\u3002 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5f15\u7528\u4f20\u9012 ptr2 \u6307\u5411\u7684\u5730\u5740\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void nullify ( int *& refptr ) // refptr is now a reference to a pointer { refptr = nullptr ; // Make the function parameter a null pointer } int main () { int x { 5 }; int * ptr { & x }; // ptr points to x std :: cout << \"ptr is \" << ( ptr ? \"non-null \\n \" : \"null \\n \" ); nullify ( ptr ); std :: cout << \"ptr is \" << ( ptr ? \"non-null \\n \" : \"null \\n \" ); return 0 ; } \u8fd0\u884c\u7ed3\u679c\uff1a 1 2 ptr is non-null ptr is null \u56e0\u4e3a refptr \u662f\u4e00\u4e2a\u6307\u9488\u7684\u5f15\u7528\uff0c\u6240\u4ee5\u5f53 ptr \u4f20\u5165\u65f6\uff0c refptr \u88ab\u7ed1\u5b9a ptr \u3002\u8fd9\u610f\u5473\u7740\u4efb\u4f55\u5bf9 refptr \u7684\u4fee\u6539\u90fd\u4f1a\u4f5c\u7528\u4e8e ptr \u3002 \u9898\u5916\u8bdd \u56e0\u4e3a\u5bf9\u6307\u9488\u7684\u5f15\u7528\u76f8\u5f53\u7f55\u89c1\uff0c\u5176\u8bed\u6cd5\u5f88\u5bb9\u6613\u6df7\u6dc6(\u5b83\u662f int*& \u8fd8\u662f int&* ?)\u597d\u6d88\u606f\u662f\uff0c\u5982\u679c\u53cd\u5411\u64cd\u4f5c\uff0c\u7f16\u8bd1\u5668\u5c06\u51fa\u9519\uff0c\u56e0\u4e3a\u4e0d\u80fd\u6709\u6307\u5411\u5f15\u7528\u7684\u6307\u9488(\u56e0\u4e3a\u6307\u9488\u5fc5\u987b\u4fdd\u5b58\u5bf9\u8c61\u7684\u5730\u5740\uff0c\u800c\u5f15\u7528\u4e0d\u662f\u5bf9\u8c61)\u3002\u7136\u540e\u4f60\u53ef\u4ee5\u628a\u5b83\u6362\u8fc7\u6765\u3002","title":"\u901a\u8fc7\u5f15\u7528\u4f20\u5730\u5740\uff1fPass by address\u2026 by reference?"},{"location":"9-10-Pass-by-address-part-2/#0-null","text":"\u5728\u672c\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u89e3\u91ca\u4e3a\u4ec0\u4e48\u4e0d\u518d\u4f7f\u7528 0 \u6216 NULL \u3002 \u5b57\u9762\u91cf\u201c 0 \u201d\u65e2\u53ef\u4ee5\u88ab\u89e3\u91ca\u4e3a\u6574\u6570 \u5b57\u9762\u91cf \uff0c\u4e5f\u53ef\u4ee5\u88ab\u89e3\u91ca\u4e3a\u7a7a\u6307\u9488\u5b57\u9762\u91cf\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60f3\u8981\u533a\u5206\u5176\u51c6\u786e\u542b\u4e49\u5e76\u4e0d\u5bb9\u6613\u2014\u2014\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u8ba4\u4e3a\u6211\u4eec\u6307\u7684\u662f\u5176\u4e2d\u4e00\u4e2a\u542b\u4e49\uff0c\u800c\u6211\u4eec\u6307\u7684\u662f\u53e6\u4e00\u4e2a\u2014\u2014\u8fd9\u4f1a\u7ed9\u7a0b\u5e8f\u7684\u884c\u4e3a\u5e26\u6765\u610f\u60f3\u4e0d\u5230\u7684\u540e\u679c\u3002 \u9884\u5904\u7406\u5668 \u5b8f NULL \u5e76\u6ca1\u6709\u5728\u6807\u51c6\u4e2d\u660e\u786e\u5b9a\u4e49\uff0c\u5b83\u7684\u503c\u53ef\u80fd\u662f 0 , 0L , ((void*)0) , \u6216\u5176\u4ed6\u503c\u3002 \u5728\u8bfe\u7a0b 8.9 - \u51fd\u6570\u91cd\u8f7d \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u51fd\u6570 \u91cd\u8f7d (\u591a\u4e2a\u51fd\u6570\u53ef\u4ee5\u5177\u6709\u76f8\u540c\u7684\u540d\u79f0\uff0c\u53ea\u8981\u5b83\u4eec\u53ef\u4ee5\u901a\u8fc7\u5f62\u53c2\u7684\u6570\u91cf\u6216\u7c7b\u578b\u6765\u533a\u5206)\u3002\u7f16\u8bd1\u5668\u53ef\u4ee5\u901a\u8fc7\u4f5c\u4e3a\u51fd\u6570\u8c03\u7528\u4e00\u90e8\u5206\u4f20\u5165\u7684\u53c2\u6570\u6765\u786e\u5b9a\u60a8\u60f3\u8981\u54ea\u4e2a\u91cd\u8f7d\u51fd\u6570\u3002 \u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f7f\u7528 0 \u6216 NULL \u65f6\u53ef\u80fd\u5e26\u6765\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include #include // \u5b9a\u4e49\u4e86 NULL void print ( int x ) // \u51fd\u6570\u63a5\u53d7\u6574\u6570 { std :: cout << \"print(int): \" << x << '\\n' ; } void print ( int * ptr ) // \u63a5\u53d7\u4e00\u4e2a\u6574\u578b\u6307\u9488 { std :: cout << \"print(int*): \" << ( ptr ? \"non-null \\n \" : \"null \\n \" ); } int main () { int x { 5 }; int * ptr { & x }; print ( ptr ); // \u603b\u662f\u8c03\u7528 print(int*) \u56e0\u4e3aptr\u7c7b\u578b\u4e3a int* (good) print ( 0 ); // \u603b\u662f\u8c03\u7528 print(int) \u56e0\u4e3a 0 \u662f\u4e00\u4e2a\u6574\u578b\u5b57\u9762\u91cf (\u4f46\u613f\u8fd9\u662f\u6211\u4eec\u6240\u5e0c\u671b\u7684) print ( NULL ); // \u8fd9\u4e2a\u8bed\u53e5\u53ef\u80fd\u505a\u4ee5\u4e0b\u4efb\u4f55\u4e8b\uff1a // \u8c03\u7528 print(int) (Visual Studio \u4f1a\u8fd9\u4e48\u505a) // \u8c03\u7528 print(int*) // \u5bfc\u81f4\u6b67\u4e49\uff0c\u5f15\u53d1\u7f16\u8bd1\u9519\u8bef(gcc \u548c Clang \u4f1a\u8fd9\u4e48\u505a) print ( nullptr ); // \u603b\u662f\u8c03\u7528 print(int*) return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a(\u4f7f\u7528 Visual Studio)\uff0c\u6253\u5370\uff1a 1 2 3 4 print(int*): non-null print(int): 0 print(int): 0 print(int*): null \u5f53\u4f20\u9012\u53c2\u65700\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u4f18\u5148\u8c03\u7528 print(int) \u800c\u4e0d\u662f print(int*) \uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u95ee\u9898\uff0c\u5982\u679c\u6211\u4eec\u7684\u672c\u610f\u662f\u5e0c\u671b\u4f7f\u7528\u7a7a\u6307\u9488\u5e76\u8c03\u7528 print(int*) \u7684\u8bdd\u3002 \u5728 NULL \u88ab\u5b9a\u4e49\u4e3a\u503c 0 \u7684\u60c5\u51b5\u4e0b\uff0c print(NULL) \u4e5f\u4f1a\u8c03\u7528 print(int) \uff0c\u800c\u4e0d\u662f print(int*) \uff0c\u5c31\u50cf\u4f60\u671f\u671b\u7684NULL\u6307\u9488\u5b57\u9762\u91cf\u4e00\u6837\u3002\u5728 NULL \u6ca1\u6709\u5b9a\u4e49\u4e3a 0 \u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5176\u4ed6\u884c\u4e3a\uff0c\u5982\u8c03\u7528 print(int*) \u6216\u7f16\u8bd1\u9519\u8bef\u3002 \u4f7f\u7528 nullptr \u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u4e8c\u4e49\u6027(\u5b83\u603b\u662f\u4f1a\u5bfc\u81f4\u51fd\u6570 print(int*) \u88ab\u8c03\u7528)\uff0c\u56e0\u4e3a nullptr \u53ea\u80fd\u5339\u914d\u6307\u9488\u7c7b\u578b\u3002","title":"\u4e3a\u4ec0\u4e48\u73b0\u5728\u4e0d\u63a8\u8350\u4f7f\u7528\u00a00\u00a0\u6216\u00a0NULL\u00a0\u8868\u793a\u7a7a\u6307\u9488\u4e86\uff1f\uff08\u9009\u8bfb\uff09"},{"location":"9-10-Pass-by-address-part-2/#stdnullptr_t","text":"\u56e0\u4e3a nullptr \u53ef\u4ee5\u5728\u91cd\u8f7d\u51fd\u6570\u4e2d\u533a\u5206\u4e0e\u6574\u578b\u503c\uff0c\u6240\u4ee5\u5b83\u5fc5\u5b9a\u5c5e\u4e8e\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\u3002\u90a3\u4e48\uff0c nullptr \u662f\u4ec0\u4e48\u7c7b\u578b\u5462\uff1f\u5b9e\u9645\u4e0a\uff0c nullptr \u7684\u7c7b\u578b\u4e3a std::nullptr_t (\u5b9a\u4e49\u5728 )\u3002 std::nullptr_t \u53ea\u80fd\u4fdd\u5b58\u4e00\u4e2a\u503c\uff1a nullptr \uff01\u5c3d\u7ba1\u6709\u4eba\u89c9\u5f97\u8fd9\u5f88\u8822\uff0c\u4f46\u662f\u6709\u4e00\u79cd\u60c5\u51b5\u4e0b\u662f\u6709\u7528\u7684\u3002\u5982\u679c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u5e76\u4e14\u53ea\u63a5\u6536 nullptr \u5b57\u9762\u91cf \u5b9e\u53c2\uff0c\u5219\u53ef\u4ee5\u8ba9\u5f62\u53c2\u4e3a std::nullptr_t \u7c7b\u578b\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include // for std::nullptr_t void print ( std :: nullptr_t ) { std :: cout << \"in print(std::nullptr_t) \\n \" ; } void print ( int * ) { std :: cout << \"in print(int*) \\n \" ; } int main () { print ( nullptr ); // calls print(std::nullptr_t) int x { 5 }; int * ptr { & x }; print ( ptr ); // calls print(int*) ptr = nullptr ; print ( ptr ); // calls print(int*) (since ptr has type int*) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570\u8c03\u7528 print(nullptr) \u4f1a\u88ab\u89e3\u6790\u4e3a print(std::nullptr_t) \u800c\u4e0d\u662f print(int*) \uff0c\u56e0\u4e3a\u524d\u8005\u65e0\u9700\u7c7b\u578b\u8f6c\u6362\u5373\u53ef\u5339\u914d\u3002 \u6709\u4e00\u79cd\u60c5\u51b5\u53ef\u80fd\u4f1a\u8ba9\u4eba\u6709\u70b9\u56f0\u60d1\uff0c\u90a3\u5c31\u662f\u5f53 ptr \u503c\u4e3a nullptr \u65f6\u8c03\u7528 print(ptr) \u3002\u8bb0\u4f4f\uff0c\u51fd\u6570\u91cd\u8f7d\u5339\u914d\u7684\u662f\u7c7b\u578b\uff0c\u800c\u4e0d\u662f\u503c\uff0c ptr \u7684\u7c7b\u578b\u662f int* \u3002\u56e0\u6b64\uff0c print(int*) \u5c06\u88ab\u5339\u914d\u3002 print(std::nullptr_t) \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u751a\u81f3\u4e0d\u9700\u8981\u8003\u8651\uff0c\u56e0\u4e3a\u6307\u9488\u7c7b\u578b\u4e0d\u4f1a\u9690\u5f0f\u8f6c\u6362\u4e3a std::nullptr_t \u3002 \u4f60\u53ef\u80fd\u6c38\u8fdc\u90fd\u4e0d\u9700\u8981\u4f7f\u7528\u5b83\uff0c\u4f46\u4e3a\u4e86\u4ee5\u9632\u4e07\u4e00\uff0c\u77e5\u9053\u5b83\u662f\u5f88\u597d\u7684\u3002","title":"std::nullptr_t (\u9009\u8bfb)"},{"location":"9-10-Pass-by-address-part-2/#_3","text":"\u73b0\u5728\u60a8\u5df2\u7ecf\u7406\u89e3\u4e86\u901a\u8fc7\u5f15\u7528\u3001\u5730\u5740\u548c\u503c\u4f20\u9012\u4e4b\u95f4\u7684\u57fa\u672c\u533a\u522b\uff0c\u8ba9\u6211\u4eec\u6682\u65f6\u7b80\u5316\u4e00\u4e0b\u3002\uff1a\uff09 \u867d\u7136\u7f16\u8bd1\u5668\u901a\u5e38\u53ef\u4ee5\u5c06\u5f15\u7528\u4f18\u5316\u6389\uff0c\u4f46\u5728\u6709\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8fd9\u662f\u4e0d\u53ef\u80fd\u7684\uff0c\u800c\u5f15\u7528\u5b9e\u9645\u4e0a\u662f\u9700\u8981\u7684\u3002\u5f15\u7528\u901a\u5e38\u7531\u7f16\u8bd1\u5668\u4f7f\u7528\u6307\u9488\u5b9e\u73b0\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5728\u5e55\u540e\uff0c\u5f15\u7528\u4f20\u9012\u672c\u8d28\u4e0a\u53ea\u662f\u5730\u5740\u4f20\u9012(\u5bf9\u5f15\u7528\u7684\u8bbf\u95ee\u6267\u884c\u9690\u5f0f\u89e3\u5f15\u7528)\u3002 \u5728\u4e0a\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u63d0\u5230\u8fc7\uff0c\u901a\u8fc7\u5730\u5740\u4f20\u9012\u53ea\u662f\u5c06\u4e00\u4e2a\u5730\u5740\u4ece\u8c03\u7528\u8005\u590d\u5236\u5230\u88ab\u8c03\u7528\u51fd\u6570\u2014\u2014\u5b83\u53ea\u662f\u6309\u503c\u4f20\u9012\u4e00\u4e2a\u5730\u5740\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u8fd9\u6837\u7684\u7ed3\u8bba\uff1aC++\u786e\u5b9e\u662f\u6309\u503c\u4f20\u9012\u6240\u6709\u5185\u5bb9\u7684\uff01 \u6309\u5730\u5740\u4f20\u9012 \u6216 \u6309\u5f15\u7528\u4f20\u9012 \u7684\u53c2\u6570\uff0c\u5176\u7279\u522b\u4e4b\u5904\u53ea\u5728\u4e8e\u4f20\u9012\u8fdb\u6765\u7684\u5730\u5740\u53ef\u4ee5\u88ab\u89e3\u5f15\u7528\uff0c\u6240\u4ee5\u80fd\u901a\u8fc7\u5b83\u4fee\u6539\u5b9e\u53c2\uff0c\u800c\u666e\u901a\u5f62\u53c2\u4e0d\u80fd\u505a\u5230\u8fd9\u4e00\u70b9\u3002","title":"\u5176\u5b9e\u90fd\u662f\u6309\u503c\u4f20\u9012"},{"location":"9-11-Return-by-reference-and-return-by-address/","tags":["reference","address","return"],"text":"Key Takeaway \u5f15\u7528\u8fd4\u56de\u7684\u5bf9\u8c61\u5fc5\u987b\u5b58\u5728\u4e8e\u8fd4\u56de\u5f15\u7528\u7684\u51fd\u6570\u7684\u4f5c\u7528\u57df\u4e4b\u5916\uff0c\u5426\u5219\u5c06\u5bfc\u81f4\u60ac\u7a7a\u5f15\u7528\u3002\u6c38\u8fdc\u4e0d\u8981\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u5c40\u90e8\u53d8\u91cf \u4e0d\u8981\u6309\u5f15\u7528\u8fd4\u56de\u975econst\u7684\u5c40\u90e8\u9759\u6001\u53d8\u91cf \u5982\u679c\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5f15\u7528\uff0c\u5e76\u4e14\u8be5\u5f15\u7528\u7528\u4e8e\u521d\u59cb\u5316\u6216\u8d4b\u503c\u7ed9\u4e00\u4e2a\u975e\u5f15\u7528\u53d8\u91cf\uff0c\u5219\u8fd4\u56de\u503c\u5c06\u88ab\u590d\u5236(\u5c31\u50cf\u5b83\u662f\u901a\u8fc7value\u8fd4\u56de\u7684\u4e00\u6837)\u3002 \u5982\u679c\u53c2\u6570\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u5219\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u8be5\u53c2\u6570\u662f\u5b89\u5168\u7684 \u5728\u4e0a\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u8fc7\uff0c\u5f53\u5b9e\u53c2 \u6309\u503c\u4f20\u9012 \u65f6\uff0c\u5b9e\u53c2\u7684\u503c\u4f1a\u88ab \u62f7\u8d1d \u4e00\u4efd\u5230 \u5f62\u53c2 \u3002 \u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\u6765\u8bf4\uff08\u62f7\u8d1d\u5f00\u9500\u5c0f\uff09\uff0c\u8fd9\u6ca1\u6709\u4ec0\u4e48\u95ee\u9898\u3002\u4f46\u662f\u5bf9\u4e8e \u7c7b\u7c7b\u578b \u6765\u8bf4\uff0c\u62f7\u8d1d\u5f00\u9500\u901a\u5e38\u4f1a\u5f88\u5927\uff08\u4f8b\u5982 std::string )\u3002\u901a\u8fc7 \u6309\u5f15\u7528\u4f20\u9012 \uff08\u901a\u5e38\u4e3a const \uff09\u6216\u6309 \u6309\u5730\u5740\u4f20\u9012 \u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u5f00\u9500\u3002 \u5728\u6309\u503c\u8fd4\u56de\u65f6\uff0c\u6211\u4eec\u4f1a\u9047\u5230\u7c7b\u4f3c\u7684\u60c5\u51b5\uff1a\u8fd4\u56de\u503c\u7684\u526f\u672c\u88ab\u4f20\u9012\u56de\u8c03\u7528\u8005\u3002\u5982\u679c\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u662f\u7c7b\u7c7b\u578b\uff0c\u5219\u4ee3\u4ef7\u53ef\u80fd\u5f88\u9ad8\u3002 1 std :: string returnByValue (); // \u8fd4\u56de std::string \u7684\u62f7\u8d1d(expensive) \u6309\u5f15\u7528\u8fd4\u56de \u5728\u5c06\u7c7b\u7c7b\u578b\u4f20\u9012\u56de\u8c03\u7528\u8005\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd(\u4e5f\u53ef\u80fd\u4e0d)\u5e0c\u671b \u6309\u5f15\u7528\u8fd4\u56de \u3002\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u8fd4\u56de\u4e00\u4e2a\u7ed1\u5b9a\u5230\u88ab\u8fd4\u56de\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u8fd9\u6837\u5c31\u907f\u514d\u4e86\u5bf9\u8fd4\u56de\u503c\u8fdb\u884c\u590d\u5236\u3002\u8981\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\uff0c\u53ea\u9700\u5c06\u51fd\u6570\u7684\u8fd4\u56de\u503c\u5b9a\u4e49\u4e3a\u5f15\u7528\u7c7b\u578b: 1 2 std :: string & returnByReference (); // returns a reference to an existing std::string (cheap) const std :: string & returnByReferenceToConst (); // returns a const reference to an existing std::string (cheap) \u4e0b\u9762\u4f8b\u7a0b\u5c55\u793a\u4e86\u5176\u539f\u7406\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include const std :: string & getProgramName () // \u8fd4\u56deconst\u5f15\u7528 { static const std :: string s_programName { \"Calculator\" }; // \u9759\u6001\u6301\u7eed\u65f6\u95f4\uff0c\u6301\u7eed\u5230\u7a0b\u5e8f\u7ed3\u675f return s_programName ; } int main () { std :: cout << \"This program is named \" << getProgramName (); return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 This program is named Calculator \u56e0\u4e3a getProgramName() \u8fd4\u56de const \u5f15\u7528\uff0c\u6240\u4ee5 return s_programName \u6267\u884c\u65f6\uff0c getProgramName() \u4f1a\u8fd4\u56de const \u5f15\u7528\u5230 s_programName (\u907f\u514d\u4e86\u62f7\u8d1d\u5bf9\u8c61)\u3002\u8c03\u7528\u8005\u53ef\u4ee5\u4f7f\u7528\u8be5\u5f15\u7528\u6765\u8bbf\u95ee s_programName \u7684\u503c\uff0c\u4ece\u800c\u5c06\u5176\u6253\u5370\u51fa\u6765\u3002 \u6309\u5f15\u7528\u8fd4\u56de\u7684\u5bf9\u8c61\u5176\u6301\u7eed\u65f6\u95f4\u5fc5\u987b\u8d85\u8fc7\u51fd\u6570\u8c03\u7528\u672c\u8eab \u4f7f\u7528\u6309\u5f15\u7528\u8fd4\u56de\u6700\u9700\u8981\u6ce8\u610f\u7684\u4e8b\u662f\uff1a\u7a0b\u5e8f\u5458\u5fc5\u987b\u786e\u4fdd\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u6bd4\u8fd4\u56de\u5f15\u7528\u7684\u51fd\u6570\u5bff\u547d\u957f\u3002\u5426\u5219\uff0c\u8fd4\u56de\u7684\u5f15\u7528\u5c06\u79f0\u4e3a \u60ac\u5782 \u5f15\u7528(\u5f15\u7528\u4e00\u4e2a\u5df2\u88ab\u9500\u6bc1\u7684\u5bf9\u8c61)\uff0c\u4f7f\u7528\u8be5\u5f15\u7528\u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u5728\u4e0a\u9762\u7684 \u7a0b\u5e8f\u4e2d\uff0c\u56e0\u4e3a s_programName \u5177\u6709 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u6240\u4ee5\u5b83\u7684\u77e5\u9053\u7a0b\u5e8f\u7ed3\u675f\u624d\u4f1a\u88ab\u9500\u6bc1\u3002\u5f53 main \u51fd\u6570\u8bbf\u95ee\u8be5\u8fd4\u56de\u7684\u5f15\u7528\u65f6\uff0c\u5b83\u5b9e\u9645\u8bbf\u95ee\u7684\u662f s_programName \uff0c\u8fd9\u65f6\u662f\u6ca1\u6709\u95ee\u9898\u7684\uff0c\u5728\u8fd9\u4e2a\u65f6\u95f4\u70b9\u4e0a\u5b83\u8fd8\u6ca1\u6709\u88ab\u9500\u6bc1\u3002 \u63a5\u4e0b\u6765\uff0c\u4fee\u6539\u4e0a\u9762\u7684\u7a0b\u5e8f\uff0c\u770b\u770b\u51fd\u6570\u8fd4\u56de\u60ac\u5782\u5f15\u7528\u7684\u60c5\u51b5\u4e0b\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 #include #include const std :: string & getProgramName () { const std :: string programName { \"Calculator\" }; // now a local variable, destroyed when function ends return programName ; } int main () { std :: cout << \"This program is named \" << getProgramName (); return 0 ; } ``` \u8be5\u7a0b\u5e8f\u7684\u8fd0\u884c\u7ed3\u679c\u662f\u672a\u5b9a\u4e49\u7684 \u3002 \u5f53 ` getProgramName () ` \u8fd4\u56de\u65f6 \uff0c \u4e00\u4e2a\u5f15\u7528\u7ed1\u5b9a\u5230\u4e86\u5c40\u90e8\u53d8\u91cf ` programName ` \u3002 \u4e5f\u6b63\u662f\u56e0\u4e3a\u8be5\u53d8\u91cf\u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf \uff0c \u5b83\u5177\u6709 [ \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 ](. / automatic - storage - duration . md ) \uff0c \u5b83\u4f1a\u5728\u51fd\u6570\u672b\u5c3e\u88ab\u9500\u6bc1 \uff0c \u6240\u4ee5\u8fd4\u56de\u7684\u5f15\u7528\u5c31\u662f\u4f1a\u662f\u4e00\u4e2a\u60ac\u5782\u5f15\u7528 \uff0c \u5728 ` main ` \u51fd\u6570\u4e2d\u4f7f\u7528\u8be5\u60ac\u5782\u5f15\u7528\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u5982\u679c\u8bd5\u56fe\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u5c40\u90e8\u53d8\u91cf \uff0c \u73b0\u4ee3\u7f16\u8bd1\u5668\u5c06\u4ea7\u751f\u8b66\u544a\u6216\u9519\u8bef ( \u56e0\u6b64 \uff0c \u4e0a\u9762\u7684\u7a0b\u5e8f\u751a\u81f3\u53ef\u80fd\u65e0\u6cd5\u7f16\u8bd1 ) \uff0c \u4f46\u7f16\u8bd1\u5668\u6709\u65f6\u5728\u68c0\u6d4b\u66f4\u590d\u6742\u7684\u60c5\u51b5\u65f6\u4f1a\u9047\u5230\u9ebb\u70e6 \u3002 !!! warning \"\u6ce8\u610f\" == \u5f15\u7528\u8fd4\u56de\u7684\u5bf9\u8c61\u5fc5\u987b\u5b58\u5728\u4e8e\u8fd4\u56de\u5f15\u7528\u7684\u51fd\u6570\u7684\u4f5c\u7528\u57df\u4e4b\u5916 \uff0c \u5426\u5219\u5c06\u5bfc\u81f4\u60ac\u7a7a\u5f15\u7528 \u3002 \u6c38\u8fdc\u4e0d\u8981\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u5c40\u90e8\u53d8\u91cf \u3002 == ## \u4e0d\u8981\u6309\u5f15\u7528\u8fd4\u56de\u975econst\u7684\u5c40\u90e8\u9759\u6001\u53d8\u91cf \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d \uff0c \u6211\u4eec\u6309\u5f15\u7528\u8fd4\u56de\u7684\u662f\u4e00\u4e2aconst\u7684\u5c40\u90e8\u9759\u6001\u53d8\u91cf \uff0c \u5e76\u4ee5\u6b64\u6765\u6f14\u793a\u6309\u5f15\u7528\u4f20\u9012\u7684\u65b9\u5f0f \u3002 \u4f46\u662f \uff0c \u6309\u5f15\u7528\u8fd4\u56de\u975econst\u7684\u9759\u6001\u53d8\u91cf\u5e76\u4e0d\u7b26\u5408\u4e60\u60ef \uff0c \u901a\u5e38\u5e94\u8be5\u907f\u514d\u8fd9\u4e48\u505a \u3002 \u8fd9\u4e48\u505a\u4f1a\u6709\u4ec0\u4e48\u95ee\u9898 \uff1f \u8bf7\u770b\u4e0b\u9762\u7684\u7a0b\u5e8f \uff1a ``` cpp #include #include const int & getNextId () { static int s_x { 0 }; // \u6ce8\u610f: \u53d8\u91cf\u662f\u975econst\u7684 ++ s_x ; // \u751f\u6210\u4e0b\u4e00\u4e2aid return s_x ; // \u6309\u5f15\u7528\u8fd4\u56de } int main () { const int & id1 { getNextId () }; // id1 \u662f\u4e00\u4e2a\u5f15\u7528 const int & id2 { getNextId () }; // id2 \u662f\u4e00\u4e2a\u5f15\u7528 std :: cout << id1 << id2 << '\\n' ; return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 22 \u4e4b\u6240\u4ee5\u8fd9\u6837\u662f\u56e0\u4e3a id1 \u548c id2 \u5f15\u7528\u7684\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61(\u5373\u9759\u6001\u53d8\u91cf s_x )\uff0c\u6240\u4ee5\u4efb\u4f55\u5bf9\u8be5\u53d8\u91cf\u7684\u4fee\u6539(\u4f8b\u5982 getNextId() )\uff0c\u90fd\u4f1a\u5f71\u54cd\u5230\u6240\u6709\u5f15\u7528\u3002\u901a\u8fc7const\u5f15\u7528\u8fd4\u56de\u9759\u6001\u5c40\u90e8\u503c\u7684\u7a0b\u5e8f\u7ecf\u5e38\u51fa\u73b0\u7684\u53e6\u4e00\u4e2a\u95ee\u9898\u662f\uff0c\u6ca1\u6709\u6807\u51c6\u5316\u7684\u65b9\u6cd5\u5c06 s_x \u91cd\u7f6e\u56de\u9ed8\u8ba4\u72b6\u6001\u3002\u8fd9\u6837\u7684\u7a0b\u5e8f\u5fc5\u987b\u4f7f\u7528\u975e\u60ef\u7528\u7684\u89e3\u51b3\u65b9\u6848(\u4f8b\u5982\u91cd\u7f6e\u53c2\u6570)\uff0c\u6216\u8005\u53ea\u80fd\u901a\u8fc7\u9000\u51fa\u548c\u91cd\u65b0\u542f\u52a8\u7a0b\u5e8f\u6765\u91cd\u7f6e\u3002 \u867d\u7136\u4e0a\u9762\u7684\u4f8b\u5b50\u6709\u70b9\u50bb\uff0c\u4f46\u7a0b\u5e8f\u5458\u6709\u65f6\u4f1a\u4e3a\u4e86\u4f18\u5316\u76ee\u7684\u800c\u5c1d\u8bd5\u4e0a\u9762\u7684\u505a\u6cd5\uff0c\u7136\u540e\u7a0b\u5e8f\u5c31\u4e0d\u80fd\u6309\u9884\u671f\u5de5\u4f5c\u4e86\u3002 \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u8fd4\u56de\u5bf9\u975econst\u5c40\u90e8\u9759\u6001\u53d8\u91cf\u7684\u5f15\u7528\u3002 \u5982\u679c\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u7684\u5c40\u90e8\u53d8\u91cf\u7684\u521b\u5efa\u6210\u672c\u5f88\u9ad8(\u56e0\u6b64\u4e0d\u5fc5\u6bcf\u6b21\u51fd\u6570\u8c03\u7528\u90fd\u91cd\u65b0\u521b\u5efa\u8be5\u53d8\u91cf)\uff0c\u5219\u6709\u65f6\u4f1a\u8fd4\u56de\u5bf9 const \u5c40\u90e8\u9759\u6001\u53d8\u91cf\u7684const\u5f15\u7528\u3002\u4f46\u8fd9\u662f\u7f55\u89c1\u7684\u3002 \u8fd4\u56de\u4e00\u4e2a\u6307\u5411const\u5168\u5c40\u53d8\u91cf\u7684const\u5f15\u7528\u6709\u65f6\u4e5f\u662f\u4e00\u79cd\u7528\u4e8e\u5c01\u88c5\u5bf9\u5168\u5c40\u53d8\u91cf\u8bbf\u95ee\u7684\u65b9\u5f0f \u3002\u6211\u4eec\u5728\u8bfe\u7a0b 6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c \u4e2d\u8ba8\u8bba\u8fd9\u4e2a\u95ee\u9898\u3002\u5982\u679c\u6709\u610f\u4e14\u8c28\u614e\u5730\u4f7f\u7528\uff0c\u8fd9\u4e5f\u662f\u53ef\u4ee5\u7684\u3002 \u4f7f\u7528\u8fd4\u56de\u7684\u5f15\u7528\u6765\u8bbf\u95ee/\u521d\u59cb\u5316\u666e\u901a\u53d8\u91cf\u65f6\u4f1a\u521b\u5efa\u62f7\u8d1d \u5982\u679c\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5f15\u7528\uff0c\u5e76\u4e14\u8be5\u5f15\u7528\u7528\u4e8e\u521d\u59cb\u5316\u6216\u8d4b\u503c\u7ed9\u4e00\u4e2a\u975e\u5f15\u7528\u53d8\u91cf\uff0c\u5219\u8fd4\u56de\u503c\u5c06\u88ab\u590d\u5236(\u5c31\u50cf\u5b83\u662f\u901a\u8fc7value\u8fd4\u56de\u7684\u4e00\u6837)\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include const int & getNextId () { static int s_x { 0 }; ++ s_x ; return s_x ; } int main () { const int id1 { getNextId () }; // id1 is a normal variable now and receives a copy of the value returned by reference from getNextId() const int id2 { getNextId () }; // id2 is a normal variable now and receives a copy of the value returned by reference from getNextId() std :: cout << id1 << id2 << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c getNextId() \u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5f15\u7528\uff0c\u4f46\u662f id1 \u548c id2 \u90fd\u662f\u975e\u5f15\u7528\u7684\u666e\u901a\u53d8\u91cf\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8fd4\u56de\u7684\u5f15\u7528\u7ed1\u5b9a\u7684\u503c\u4f1a\u88ab\u62f7\u8d1d\u5230\u8fd9\u4e2a\u666e\u901a\u53d8\u91cf\uff0c\u56e0\u6b64\u7a0b\u5e8f\u6253\u5370\uff1a 1 12 \u5f53\u7136\uff0c\u8fd9\u4e5f\u8fdd\u80cc\u4e86\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u503c\u7684\u76ee\u7684\u3002 \u8fd8\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u7a0b\u5e8f\u8fd4\u56de\u4e00\u4e2a\u60ac\u5782\u5f15\u7528\uff0c\u5219\u8be5\u5f15\u7528\u5728\u590d\u5236\u4e4b\u524d\u4e00\u76f4\u60ac\u7a7a\uff0c\u8fd9\u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include const std :: string & getProgramName () // will return a const reference { const std :: string programName { \"Calculator\" }; return programName ; } int main () { std :: string name { getProgramName () }; // makes a copy of a dangling reference std :: cout << \"This program is named \" << name << '\\n' ; // undefined behavior return 0 ; } \u6309\u5f15\u7528\u8fd4\u56de\u6309\u5f15\u7528\u4f20\u9012\u7684\u53c2\u6570\u6ca1\u6709\u95ee\u9898 \u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u5bf9\u8c61\u662f\u6709\u610f\u4e49\u7684\uff0c\u6211\u4eec\u5c06\u5728\u672a\u6765\u7684\u8bfe\u7a0b\u4e2d\u9047\u5230\u8bb8\u591a\u8fd9\u6837\u7684\u60c5\u51b5\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u73b0\u5728\u53ef\u4ee5\u4e3e\u4e00\u4e2a\u6709\u7528\u7684\u4f8b\u5b50\u3002 \u5982\u679c\u53c2\u6570\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u5219\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u8be5\u53c2\u6570\u662f\u5b89\u5168\u7684 \u3002\u8fd9\u662f\u6709\u610f\u4e49\u7684\uff1a\u4e3a\u4e86\u5c06\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u53c2\u6570\u5fc5\u987b\u5b58\u5728\u4e8e\u8c03\u7528\u8005\u7684\u4f5c\u7528\u57df\u4e2d\u3002\u5f53\u88ab\u8c03\u7528\u7684\u51fd\u6570\u8fd4\u56de\u65f6\uff0c\u8be5\u5bf9\u8c61\u5fc5\u987b\u4ecd\u7136\u5b58\u5728\u4e8e\u8c03\u7528\u8005\u7684\u4f5c\u7528\u57df\u4e2d\u3002 \u4e0b\u9762\u662f\u8fd9\u6837\u4e00\u4e2a\u51fd\u6570\u7684\u7b80\u5355\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include // Takes two std::string objects, returns the one that comes first alphabetically const std :: string & firstAlphabetical ( const std :: string & a , const std :: string & b ) { return ( a < b ) ? a : b ; // We can use operator< on std::string to determine which comes first alphabetically } int main () { std :: string hello { \"Hello\" }; std :: string world { \"World\" }; std :: cout << firstAlphabetical ( hello , world ) << '\\n' ; return 0 ; } \u8f93\u51fa\uff1a 1 Hello \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8c03\u7528\u8005\u6309\u5f15\u7528\u4f20\u9012\u4e86\u4e24\u4e2a std::string \u5bf9\u8c61\uff0c\u7136\u540e\u7ecf\u8fc7\u6bd4\u8f83\uff0c\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e2d\u6309\u7167\u5b57\u6bcd\u8868\u6bd4\u8f83\u6392\u5728\u524d\u9762\u7684\u5bf9\u8c61\u88ab\u6309\u5f15\u7528(const)\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u5982\u679c\u6211\u4eec\u662f\u6309\u7167\u503c\u4f20\u9012\uff0c\u5219\u4f1a\u5bfc\u81f4\u521b\u5efa std::string \u7684\u4e09\u4e2a\u62f7\u8d1d\uff08\u6bcf\u4e2a\u5f62\u53c2\u62f7\u8d1d\u4e00\u6b21\u3001\u8fd4\u56de\u503c\u62f7\u8d1d\u4e00\u6b21\uff09\u3002\u800c\u4f7f\u7528\u5f15\u7528\u4f20\u9012\u5219\u53ef\u4ee5\u907f\u514d\u8fd9\u4e9b\u62f7\u8d1d\u3002 \u8c03\u7528\u8005\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u4fee\u6539\u503c caller can modify values through the reference \u5f53\u5b9e\u53c2\u6309\u975econst\u5f15\u7528\u4f20\u9012\u65f6\uff0c\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u4fee\u6539\u5b9e\u53c2\u7684\u503c\u3002 \u7c7b\u4f3c\u7684\uff0c\u5f53\u6309\u975econst\u5f15\u7528\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u65f6\uff0c\u8c03\u7528\u8005\u53ef\u4ee5\u4f7f\u7528\u8be5\u5f15\u7528\u4fee\u6539\u8fd4\u56de\u503c\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include // \u63a5\u53d7\u4e24\u4e2a\u6574\u578b\u7684\u975econst\u5f15\u7528\uff0c\u8fd4\u56de\u5176\u4e2d\u8f83\u5927\u7684\u4e00\u4e2a\uff08\u6309\u5f15\u7528\u8fd4\u56de\uff09 int & max ( int & x , int & y ) { return ( x > y ) ? x : y ; } int main () { int a { 5 }; int b { 6 }; max ( a , b ) = 7 ; // \u5c06a\u548cb\u4e2d\u8f83\u5927\u7684\u4e00\u4e2a\u7684\u503c\u8bbe\u7f6e\u4e3a 7 std :: cout << a << b << '\\n' ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c max(a, b) \u5728\u8c03\u7528 max() \u51fd\u6570\u65f6\u4f20\u5165 a \u548c b \u4f5c\u4e3a\u5b9e\u53c2\u3002\u5f15\u7528\u5f62\u53c2 x \u7ed1\u5b9a\u5230\u5b9e\u53c2 a \uff0c\u800c\u5f15\u7528\u5f62\u53c2 y \u7ed1\u5b9a\u5230\u5b9e\u53c2 b \u3002\u968f\u540e\uff0c\u51fd\u6570\u4f1a\u5224\u65ad x ( 5 ) \u548c y ( 6 ) \u54ea\u4e2a\u6bd4\u8f83\u5927\u3002\u672c\u4f8b\u4e2d\u663e\u7136 y \u66f4\u5927\uff0c\u56e0\u6b64 y (\u4ecd\u7136\u7ed1\u5b9a\u5230 b ) \u88ab\u6309\u5f15\u7528\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u8c03\u7528\u8005\u968f\u540e\u901a\u8fc7\u8fd4\u56de\u7684\u5f15\u7528\u5c06 b \u8d4b\u503c\u4e3a 7\u3002 \u56e0\u6b64\uff0c\u8868\u8fbe\u5f0f max(a, b) = 7 \u6700\u7ec8\u89e3\u6790\u4e3a b = 7 \u3002 \u7a0b\u5e8f\u6253\u5370\uff1a 1 57 \u6309\u5730\u5740\u8fd4\u56de \u6309\u5730\u5740\u8fd4\u56de \u4e0e \u6309\u5f15\u7528\u8fd4\u56de \u7684\u5de5\u4f5c\u539f\u7406\u51e0\u4e4e\u76f8\u540c\uff0c\u53ea\u4e0d\u8fc7\u8fd4\u56de\u7684\u662f\u6307\u5411\u5bf9\u8c61\u7684\u6307\u9488\u800c\u4e0d\u662f\u5bf9\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u6309\u5730\u5740\u8fd4\u56de\u6709\u4e0e\u6309\u5f15\u7528\u8fd4\u56de\u76f8\u540c\u7684\u6ce8\u610f\u4e8b\u9879\u2014\u2014\u6309\u5730\u5740\u8fd4\u56de\u7684\u5bf9\u8c61\u5fc5\u987b\u6bd4\u8fd4\u56de\u5730\u5740\u7684\u51fd\u6570\u7684\u4f5c\u7528\u57df\u66f4\u957f\u4e45\uff0c\u5426\u5219\u8c03\u7528\u8005\u5c06\u6536\u5230\u4e00\u4e2a\u60ac\u5782\u6307\u9488\u3002 \u6309\u5730\u5740\u8fd4\u56de\u6bd4\u6309\u5f15\u7528\u8fd4\u56de\u7684\u4e3b\u8981\u4f18\u70b9\u662f\uff0c\u5982\u679c\u6ca1\u6709\u8981\u8fd4\u56de\u7684\u6709\u6548\u5bf9\u8c61\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u8fd4\u56de nullptr \u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u60f3\u8981\u641c\u7d22\u7684\u5b66\u751f\u5217\u8868\u3002\u5982\u679c\u5728\u5217\u8868\u4e2d\u627e\u5230\u4e86\u8981\u67e5\u627e\u7684\u5b66\u751f\uff0c\u5219\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u8868\u793a\u5339\u914d\u5b66\u751f\u7684\u5bf9\u8c61\u7684\u6307\u9488\u3002\u5982\u679c\u6211\u4eec\u6ca1\u6709\u627e\u5230\u4efb\u4f55\u5339\u914d\u7684\u5b66\u751f\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd4\u56de nullptr \u6765\u8868\u793a\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u5b66\u751f\u5bf9\u8c61\u3002 \u5176\u4e3b\u8981\u7f3a\u70b9\u5219\u662f\u8c03\u7528\u8005\u5fc5\u987b\u8bb0\u5f97\u5728\u89e3\u5f15\u7528\u8fd4\u56de\u503c\u4e4b\u524d\u8fdb\u884c\u6307\u9488\u5224\u7a7a\u64cd\u4f5c\uff0c\u5219\u53ef\u80fd\u4f1a\u53d1\u751f\u7a7a\u6307\u9488\u89e3\u5f15\u7528\u5e76\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u7531\u4e8e\u8fd9\u79cd\u5371\u9669\uff0c\u9664\u975e\u9700\u8981\u8fd4\u56de\u201c\u65e0\u5bf9\u8c61\u201d\u7684\u80fd\u529b\uff0c\u5426\u5219\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u5e94\u4f18\u5148\u4e8e\u901a\u8fc7\u5730\u5740\u8fd4\u56de\u3002 \u6700\u4f73\u5b9e\u8df5 \u9996\u9009\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u800c\u4e0d\u662f\u901a\u8fc7\u5730\u5740\u8fd4\u56de\uff0c\u9664\u975e\u8fd4\u56de\u201c\u65e0\u5bf9\u8c61\u201d(\u4f7f\u7528 nullptr )\u7684\u80fd\u529b\u5f88\u91cd\u8981\u3002","title":"9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de"},{"location":"9-11-Return-by-reference-and-return-by-address/#_1","text":"\u5728\u5c06\u7c7b\u7c7b\u578b\u4f20\u9012\u56de\u8c03\u7528\u8005\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd(\u4e5f\u53ef\u80fd\u4e0d)\u5e0c\u671b \u6309\u5f15\u7528\u8fd4\u56de \u3002\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u8fd4\u56de\u4e00\u4e2a\u7ed1\u5b9a\u5230\u88ab\u8fd4\u56de\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u8fd9\u6837\u5c31\u907f\u514d\u4e86\u5bf9\u8fd4\u56de\u503c\u8fdb\u884c\u590d\u5236\u3002\u8981\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\uff0c\u53ea\u9700\u5c06\u51fd\u6570\u7684\u8fd4\u56de\u503c\u5b9a\u4e49\u4e3a\u5f15\u7528\u7c7b\u578b: 1 2 std :: string & returnByReference (); // returns a reference to an existing std::string (cheap) const std :: string & returnByReferenceToConst (); // returns a const reference to an existing std::string (cheap) \u4e0b\u9762\u4f8b\u7a0b\u5c55\u793a\u4e86\u5176\u539f\u7406\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include const std :: string & getProgramName () // \u8fd4\u56deconst\u5f15\u7528 { static const std :: string s_programName { \"Calculator\" }; // \u9759\u6001\u6301\u7eed\u65f6\u95f4\uff0c\u6301\u7eed\u5230\u7a0b\u5e8f\u7ed3\u675f return s_programName ; } int main () { std :: cout << \"This program is named \" << getProgramName (); return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 This program is named Calculator \u56e0\u4e3a getProgramName() \u8fd4\u56de const \u5f15\u7528\uff0c\u6240\u4ee5 return s_programName \u6267\u884c\u65f6\uff0c getProgramName() \u4f1a\u8fd4\u56de const \u5f15\u7528\u5230 s_programName (\u907f\u514d\u4e86\u62f7\u8d1d\u5bf9\u8c61)\u3002\u8c03\u7528\u8005\u53ef\u4ee5\u4f7f\u7528\u8be5\u5f15\u7528\u6765\u8bbf\u95ee s_programName \u7684\u503c\uff0c\u4ece\u800c\u5c06\u5176\u6253\u5370\u51fa\u6765\u3002","title":"\u6309\u5f15\u7528\u8fd4\u56de"},{"location":"9-11-Return-by-reference-and-return-by-address/#_2","text":"\u4f7f\u7528\u6309\u5f15\u7528\u8fd4\u56de\u6700\u9700\u8981\u6ce8\u610f\u7684\u4e8b\u662f\uff1a\u7a0b\u5e8f\u5458\u5fc5\u987b\u786e\u4fdd\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u6bd4\u8fd4\u56de\u5f15\u7528\u7684\u51fd\u6570\u5bff\u547d\u957f\u3002\u5426\u5219\uff0c\u8fd4\u56de\u7684\u5f15\u7528\u5c06\u79f0\u4e3a \u60ac\u5782 \u5f15\u7528(\u5f15\u7528\u4e00\u4e2a\u5df2\u88ab\u9500\u6bc1\u7684\u5bf9\u8c61)\uff0c\u4f7f\u7528\u8be5\u5f15\u7528\u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u5728\u4e0a\u9762\u7684 \u7a0b\u5e8f\u4e2d\uff0c\u56e0\u4e3a s_programName \u5177\u6709 \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \uff0c\u6240\u4ee5\u5b83\u7684\u77e5\u9053\u7a0b\u5e8f\u7ed3\u675f\u624d\u4f1a\u88ab\u9500\u6bc1\u3002\u5f53 main \u51fd\u6570\u8bbf\u95ee\u8be5\u8fd4\u56de\u7684\u5f15\u7528\u65f6\uff0c\u5b83\u5b9e\u9645\u8bbf\u95ee\u7684\u662f s_programName \uff0c\u8fd9\u65f6\u662f\u6ca1\u6709\u95ee\u9898\u7684\uff0c\u5728\u8fd9\u4e2a\u65f6\u95f4\u70b9\u4e0a\u5b83\u8fd8\u6ca1\u6709\u88ab\u9500\u6bc1\u3002 \u63a5\u4e0b\u6765\uff0c\u4fee\u6539\u4e0a\u9762\u7684\u7a0b\u5e8f\uff0c\u770b\u770b\u51fd\u6570\u8fd4\u56de\u60ac\u5782\u5f15\u7528\u7684\u60c5\u51b5\u4e0b\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 #include #include const std :: string & getProgramName () { const std :: string programName { \"Calculator\" }; // now a local variable, destroyed when function ends return programName ; } int main () { std :: cout << \"This program is named \" << getProgramName (); return 0 ; } ``` \u8be5\u7a0b\u5e8f\u7684\u8fd0\u884c\u7ed3\u679c\u662f\u672a\u5b9a\u4e49\u7684 \u3002 \u5f53 ` getProgramName () ` \u8fd4\u56de\u65f6 \uff0c \u4e00\u4e2a\u5f15\u7528\u7ed1\u5b9a\u5230\u4e86\u5c40\u90e8\u53d8\u91cf ` programName ` \u3002 \u4e5f\u6b63\u662f\u56e0\u4e3a\u8be5\u53d8\u91cf\u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf \uff0c \u5b83\u5177\u6709 [ \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 ](. / automatic - storage - duration . md ) \uff0c \u5b83\u4f1a\u5728\u51fd\u6570\u672b\u5c3e\u88ab\u9500\u6bc1 \uff0c \u6240\u4ee5\u8fd4\u56de\u7684\u5f15\u7528\u5c31\u662f\u4f1a\u662f\u4e00\u4e2a\u60ac\u5782\u5f15\u7528 \uff0c \u5728 ` main ` \u51fd\u6570\u4e2d\u4f7f\u7528\u8be5\u60ac\u5782\u5f15\u7528\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u5982\u679c\u8bd5\u56fe\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u5c40\u90e8\u53d8\u91cf \uff0c \u73b0\u4ee3\u7f16\u8bd1\u5668\u5c06\u4ea7\u751f\u8b66\u544a\u6216\u9519\u8bef ( \u56e0\u6b64 \uff0c \u4e0a\u9762\u7684\u7a0b\u5e8f\u751a\u81f3\u53ef\u80fd\u65e0\u6cd5\u7f16\u8bd1 ) \uff0c \u4f46\u7f16\u8bd1\u5668\u6709\u65f6\u5728\u68c0\u6d4b\u66f4\u590d\u6742\u7684\u60c5\u51b5\u65f6\u4f1a\u9047\u5230\u9ebb\u70e6 \u3002 !!! warning \"\u6ce8\u610f\" == \u5f15\u7528\u8fd4\u56de\u7684\u5bf9\u8c61\u5fc5\u987b\u5b58\u5728\u4e8e\u8fd4\u56de\u5f15\u7528\u7684\u51fd\u6570\u7684\u4f5c\u7528\u57df\u4e4b\u5916 \uff0c \u5426\u5219\u5c06\u5bfc\u81f4\u60ac\u7a7a\u5f15\u7528 \u3002 \u6c38\u8fdc\u4e0d\u8981\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u5c40\u90e8\u53d8\u91cf \u3002 == ## \u4e0d\u8981\u6309\u5f15\u7528\u8fd4\u56de\u975econst\u7684\u5c40\u90e8\u9759\u6001\u53d8\u91cf \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d \uff0c \u6211\u4eec\u6309\u5f15\u7528\u8fd4\u56de\u7684\u662f\u4e00\u4e2aconst\u7684\u5c40\u90e8\u9759\u6001\u53d8\u91cf \uff0c \u5e76\u4ee5\u6b64\u6765\u6f14\u793a\u6309\u5f15\u7528\u4f20\u9012\u7684\u65b9\u5f0f \u3002 \u4f46\u662f \uff0c \u6309\u5f15\u7528\u8fd4\u56de\u975econst\u7684\u9759\u6001\u53d8\u91cf\u5e76\u4e0d\u7b26\u5408\u4e60\u60ef \uff0c \u901a\u5e38\u5e94\u8be5\u907f\u514d\u8fd9\u4e48\u505a \u3002 \u8fd9\u4e48\u505a\u4f1a\u6709\u4ec0\u4e48\u95ee\u9898 \uff1f \u8bf7\u770b\u4e0b\u9762\u7684\u7a0b\u5e8f \uff1a ``` cpp #include #include const int & getNextId () { static int s_x { 0 }; // \u6ce8\u610f: \u53d8\u91cf\u662f\u975econst\u7684 ++ s_x ; // \u751f\u6210\u4e0b\u4e00\u4e2aid return s_x ; // \u6309\u5f15\u7528\u8fd4\u56de } int main () { const int & id1 { getNextId () }; // id1 \u662f\u4e00\u4e2a\u5f15\u7528 const int & id2 { getNextId () }; // id2 \u662f\u4e00\u4e2a\u5f15\u7528 std :: cout << id1 << id2 << '\\n' ; return 0 ; } \u7a0b\u5e8f\u6253\u5370\uff1a 1 22 \u4e4b\u6240\u4ee5\u8fd9\u6837\u662f\u56e0\u4e3a id1 \u548c id2 \u5f15\u7528\u7684\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61(\u5373\u9759\u6001\u53d8\u91cf s_x )\uff0c\u6240\u4ee5\u4efb\u4f55\u5bf9\u8be5\u53d8\u91cf\u7684\u4fee\u6539(\u4f8b\u5982 getNextId() )\uff0c\u90fd\u4f1a\u5f71\u54cd\u5230\u6240\u6709\u5f15\u7528\u3002\u901a\u8fc7const\u5f15\u7528\u8fd4\u56de\u9759\u6001\u5c40\u90e8\u503c\u7684\u7a0b\u5e8f\u7ecf\u5e38\u51fa\u73b0\u7684\u53e6\u4e00\u4e2a\u95ee\u9898\u662f\uff0c\u6ca1\u6709\u6807\u51c6\u5316\u7684\u65b9\u6cd5\u5c06 s_x \u91cd\u7f6e\u56de\u9ed8\u8ba4\u72b6\u6001\u3002\u8fd9\u6837\u7684\u7a0b\u5e8f\u5fc5\u987b\u4f7f\u7528\u975e\u60ef\u7528\u7684\u89e3\u51b3\u65b9\u6848(\u4f8b\u5982\u91cd\u7f6e\u53c2\u6570)\uff0c\u6216\u8005\u53ea\u80fd\u901a\u8fc7\u9000\u51fa\u548c\u91cd\u65b0\u542f\u52a8\u7a0b\u5e8f\u6765\u91cd\u7f6e\u3002 \u867d\u7136\u4e0a\u9762\u7684\u4f8b\u5b50\u6709\u70b9\u50bb\uff0c\u4f46\u7a0b\u5e8f\u5458\u6709\u65f6\u4f1a\u4e3a\u4e86\u4f18\u5316\u76ee\u7684\u800c\u5c1d\u8bd5\u4e0a\u9762\u7684\u505a\u6cd5\uff0c\u7136\u540e\u7a0b\u5e8f\u5c31\u4e0d\u80fd\u6309\u9884\u671f\u5de5\u4f5c\u4e86\u3002 \u6700\u4f73\u5b9e\u8df5 \u907f\u514d\u8fd4\u56de\u5bf9\u975econst\u5c40\u90e8\u9759\u6001\u53d8\u91cf\u7684\u5f15\u7528\u3002 \u5982\u679c\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u7684\u5c40\u90e8\u53d8\u91cf\u7684\u521b\u5efa\u6210\u672c\u5f88\u9ad8(\u56e0\u6b64\u4e0d\u5fc5\u6bcf\u6b21\u51fd\u6570\u8c03\u7528\u90fd\u91cd\u65b0\u521b\u5efa\u8be5\u53d8\u91cf)\uff0c\u5219\u6709\u65f6\u4f1a\u8fd4\u56de\u5bf9 const \u5c40\u90e8\u9759\u6001\u53d8\u91cf\u7684const\u5f15\u7528\u3002\u4f46\u8fd9\u662f\u7f55\u89c1\u7684\u3002 \u8fd4\u56de\u4e00\u4e2a\u6307\u5411const\u5168\u5c40\u53d8\u91cf\u7684const\u5f15\u7528\u6709\u65f6\u4e5f\u662f\u4e00\u79cd\u7528\u4e8e\u5c01\u88c5\u5bf9\u5168\u5c40\u53d8\u91cf\u8bbf\u95ee\u7684\u65b9\u5f0f \u3002\u6211\u4eec\u5728\u8bfe\u7a0b 6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c \u4e2d\u8ba8\u8bba\u8fd9\u4e2a\u95ee\u9898\u3002\u5982\u679c\u6709\u610f\u4e14\u8c28\u614e\u5730\u4f7f\u7528\uff0c\u8fd9\u4e5f\u662f\u53ef\u4ee5\u7684\u3002","title":"\u6309\u5f15\u7528\u8fd4\u56de\u7684\u5bf9\u8c61\u5176\u6301\u7eed\u65f6\u95f4\u5fc5\u987b\u8d85\u8fc7\u51fd\u6570\u8c03\u7528\u672c\u8eab"},{"location":"9-11-Return-by-reference-and-return-by-address/#_3","text":"\u5982\u679c\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5f15\u7528\uff0c\u5e76\u4e14\u8be5\u5f15\u7528\u7528\u4e8e\u521d\u59cb\u5316\u6216\u8d4b\u503c\u7ed9\u4e00\u4e2a\u975e\u5f15\u7528\u53d8\u91cf\uff0c\u5219\u8fd4\u56de\u503c\u5c06\u88ab\u590d\u5236(\u5c31\u50cf\u5b83\u662f\u901a\u8fc7value\u8fd4\u56de\u7684\u4e00\u6837)\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include const int & getNextId () { static int s_x { 0 }; ++ s_x ; return s_x ; } int main () { const int id1 { getNextId () }; // id1 is a normal variable now and receives a copy of the value returned by reference from getNextId() const int id2 { getNextId () }; // id2 is a normal variable now and receives a copy of the value returned by reference from getNextId() std :: cout << id1 << id2 << '\\n' ; return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c getNextId() \u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5f15\u7528\uff0c\u4f46\u662f id1 \u548c id2 \u90fd\u662f\u975e\u5f15\u7528\u7684\u666e\u901a\u53d8\u91cf\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8fd4\u56de\u7684\u5f15\u7528\u7ed1\u5b9a\u7684\u503c\u4f1a\u88ab\u62f7\u8d1d\u5230\u8fd9\u4e2a\u666e\u901a\u53d8\u91cf\uff0c\u56e0\u6b64\u7a0b\u5e8f\u6253\u5370\uff1a 1 12 \u5f53\u7136\uff0c\u8fd9\u4e5f\u8fdd\u80cc\u4e86\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u503c\u7684\u76ee\u7684\u3002 \u8fd8\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u7a0b\u5e8f\u8fd4\u56de\u4e00\u4e2a\u60ac\u5782\u5f15\u7528\uff0c\u5219\u8be5\u5f15\u7528\u5728\u590d\u5236\u4e4b\u524d\u4e00\u76f4\u60ac\u7a7a\uff0c\u8fd9\u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include const std :: string & getProgramName () // will return a const reference { const std :: string programName { \"Calculator\" }; return programName ; } int main () { std :: string name { getProgramName () }; // makes a copy of a dangling reference std :: cout << \"This program is named \" << name << '\\n' ; // undefined behavior return 0 ; }","title":"\u4f7f\u7528\u8fd4\u56de\u7684\u5f15\u7528\u6765\u8bbf\u95ee/\u521d\u59cb\u5316\u666e\u901a\u53d8\u91cf\u65f6\u4f1a\u521b\u5efa\u62f7\u8d1d"},{"location":"9-11-Return-by-reference-and-return-by-address/#_4","text":"\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u5bf9\u8c61\u662f\u6709\u610f\u4e49\u7684\uff0c\u6211\u4eec\u5c06\u5728\u672a\u6765\u7684\u8bfe\u7a0b\u4e2d\u9047\u5230\u8bb8\u591a\u8fd9\u6837\u7684\u60c5\u51b5\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u73b0\u5728\u53ef\u4ee5\u4e3e\u4e00\u4e2a\u6709\u7528\u7684\u4f8b\u5b50\u3002 \u5982\u679c\u53c2\u6570\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u5219\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u8be5\u53c2\u6570\u662f\u5b89\u5168\u7684 \u3002\u8fd9\u662f\u6709\u610f\u4e49\u7684\uff1a\u4e3a\u4e86\u5c06\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u53c2\u6570\u5fc5\u987b\u5b58\u5728\u4e8e\u8c03\u7528\u8005\u7684\u4f5c\u7528\u57df\u4e2d\u3002\u5f53\u88ab\u8c03\u7528\u7684\u51fd\u6570\u8fd4\u56de\u65f6\uff0c\u8be5\u5bf9\u8c61\u5fc5\u987b\u4ecd\u7136\u5b58\u5728\u4e8e\u8c03\u7528\u8005\u7684\u4f5c\u7528\u57df\u4e2d\u3002 \u4e0b\u9762\u662f\u8fd9\u6837\u4e00\u4e2a\u51fd\u6570\u7684\u7b80\u5355\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include // Takes two std::string objects, returns the one that comes first alphabetically const std :: string & firstAlphabetical ( const std :: string & a , const std :: string & b ) { return ( a < b ) ? a : b ; // We can use operator< on std::string to determine which comes first alphabetically } int main () { std :: string hello { \"Hello\" }; std :: string world { \"World\" }; std :: cout << firstAlphabetical ( hello , world ) << '\\n' ; return 0 ; } \u8f93\u51fa\uff1a 1 Hello \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8c03\u7528\u8005\u6309\u5f15\u7528\u4f20\u9012\u4e86\u4e24\u4e2a std::string \u5bf9\u8c61\uff0c\u7136\u540e\u7ecf\u8fc7\u6bd4\u8f83\uff0c\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e2d\u6309\u7167\u5b57\u6bcd\u8868\u6bd4\u8f83\u6392\u5728\u524d\u9762\u7684\u5bf9\u8c61\u88ab\u6309\u5f15\u7528(const)\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u5982\u679c\u6211\u4eec\u662f\u6309\u7167\u503c\u4f20\u9012\uff0c\u5219\u4f1a\u5bfc\u81f4\u521b\u5efa std::string \u7684\u4e09\u4e2a\u62f7\u8d1d\uff08\u6bcf\u4e2a\u5f62\u53c2\u62f7\u8d1d\u4e00\u6b21\u3001\u8fd4\u56de\u503c\u62f7\u8d1d\u4e00\u6b21\uff09\u3002\u800c\u4f7f\u7528\u5f15\u7528\u4f20\u9012\u5219\u53ef\u4ee5\u907f\u514d\u8fd9\u4e9b\u62f7\u8d1d\u3002","title":"\u6309\u5f15\u7528\u8fd4\u56de\u6309\u5f15\u7528\u4f20\u9012\u7684\u53c2\u6570\u6ca1\u6709\u95ee\u9898"},{"location":"9-11-Return-by-reference-and-return-by-address/#caller-can-modify-values-through-the-reference","text":"\u5f53\u5b9e\u53c2\u6309\u975econst\u5f15\u7528\u4f20\u9012\u65f6\uff0c\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u4fee\u6539\u5b9e\u53c2\u7684\u503c\u3002 \u7c7b\u4f3c\u7684\uff0c\u5f53\u6309\u975econst\u5f15\u7528\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u65f6\uff0c\u8c03\u7528\u8005\u53ef\u4ee5\u4f7f\u7528\u8be5\u5f15\u7528\u4fee\u6539\u8fd4\u56de\u503c\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include // \u63a5\u53d7\u4e24\u4e2a\u6574\u578b\u7684\u975econst\u5f15\u7528\uff0c\u8fd4\u56de\u5176\u4e2d\u8f83\u5927\u7684\u4e00\u4e2a\uff08\u6309\u5f15\u7528\u8fd4\u56de\uff09 int & max ( int & x , int & y ) { return ( x > y ) ? x : y ; } int main () { int a { 5 }; int b { 6 }; max ( a , b ) = 7 ; // \u5c06a\u548cb\u4e2d\u8f83\u5927\u7684\u4e00\u4e2a\u7684\u503c\u8bbe\u7f6e\u4e3a 7 std :: cout << a << b << '\\n' ; return 0 ; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c max(a, b) \u5728\u8c03\u7528 max() \u51fd\u6570\u65f6\u4f20\u5165 a \u548c b \u4f5c\u4e3a\u5b9e\u53c2\u3002\u5f15\u7528\u5f62\u53c2 x \u7ed1\u5b9a\u5230\u5b9e\u53c2 a \uff0c\u800c\u5f15\u7528\u5f62\u53c2 y \u7ed1\u5b9a\u5230\u5b9e\u53c2 b \u3002\u968f\u540e\uff0c\u51fd\u6570\u4f1a\u5224\u65ad x ( 5 ) \u548c y ( 6 ) \u54ea\u4e2a\u6bd4\u8f83\u5927\u3002\u672c\u4f8b\u4e2d\u663e\u7136 y \u66f4\u5927\uff0c\u56e0\u6b64 y (\u4ecd\u7136\u7ed1\u5b9a\u5230 b ) \u88ab\u6309\u5f15\u7528\u8fd4\u56de\u7ed9\u4e3b\u8c03\u51fd\u6570\u3002\u8c03\u7528\u8005\u968f\u540e\u901a\u8fc7\u8fd4\u56de\u7684\u5f15\u7528\u5c06 b \u8d4b\u503c\u4e3a 7\u3002 \u56e0\u6b64\uff0c\u8868\u8fbe\u5f0f max(a, b) = 7 \u6700\u7ec8\u89e3\u6790\u4e3a b = 7 \u3002 \u7a0b\u5e8f\u6253\u5370\uff1a 1 57","title":"\u8c03\u7528\u8005\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u4fee\u6539\u503c caller can modify values through the reference"},{"location":"9-11-Return-by-reference-and-return-by-address/#_5","text":"\u6309\u5730\u5740\u8fd4\u56de \u4e0e \u6309\u5f15\u7528\u8fd4\u56de \u7684\u5de5\u4f5c\u539f\u7406\u51e0\u4e4e\u76f8\u540c\uff0c\u53ea\u4e0d\u8fc7\u8fd4\u56de\u7684\u662f\u6307\u5411\u5bf9\u8c61\u7684\u6307\u9488\u800c\u4e0d\u662f\u5bf9\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u6309\u5730\u5740\u8fd4\u56de\u6709\u4e0e\u6309\u5f15\u7528\u8fd4\u56de\u76f8\u540c\u7684\u6ce8\u610f\u4e8b\u9879\u2014\u2014\u6309\u5730\u5740\u8fd4\u56de\u7684\u5bf9\u8c61\u5fc5\u987b\u6bd4\u8fd4\u56de\u5730\u5740\u7684\u51fd\u6570\u7684\u4f5c\u7528\u57df\u66f4\u957f\u4e45\uff0c\u5426\u5219\u8c03\u7528\u8005\u5c06\u6536\u5230\u4e00\u4e2a\u60ac\u5782\u6307\u9488\u3002 \u6309\u5730\u5740\u8fd4\u56de\u6bd4\u6309\u5f15\u7528\u8fd4\u56de\u7684\u4e3b\u8981\u4f18\u70b9\u662f\uff0c\u5982\u679c\u6ca1\u6709\u8981\u8fd4\u56de\u7684\u6709\u6548\u5bf9\u8c61\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u8fd4\u56de nullptr \u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u60f3\u8981\u641c\u7d22\u7684\u5b66\u751f\u5217\u8868\u3002\u5982\u679c\u5728\u5217\u8868\u4e2d\u627e\u5230\u4e86\u8981\u67e5\u627e\u7684\u5b66\u751f\uff0c\u5219\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u8868\u793a\u5339\u914d\u5b66\u751f\u7684\u5bf9\u8c61\u7684\u6307\u9488\u3002\u5982\u679c\u6211\u4eec\u6ca1\u6709\u627e\u5230\u4efb\u4f55\u5339\u914d\u7684\u5b66\u751f\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd4\u56de nullptr \u6765\u8868\u793a\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u5b66\u751f\u5bf9\u8c61\u3002 \u5176\u4e3b\u8981\u7f3a\u70b9\u5219\u662f\u8c03\u7528\u8005\u5fc5\u987b\u8bb0\u5f97\u5728\u89e3\u5f15\u7528\u8fd4\u56de\u503c\u4e4b\u524d\u8fdb\u884c\u6307\u9488\u5224\u7a7a\u64cd\u4f5c\uff0c\u5219\u53ef\u80fd\u4f1a\u53d1\u751f\u7a7a\u6307\u9488\u89e3\u5f15\u7528\u5e76\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u7531\u4e8e\u8fd9\u79cd\u5371\u9669\uff0c\u9664\u975e\u9700\u8981\u8fd4\u56de\u201c\u65e0\u5bf9\u8c61\u201d\u7684\u80fd\u529b\uff0c\u5426\u5219\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u5e94\u4f18\u5148\u4e8e\u901a\u8fc7\u5730\u5740\u8fd4\u56de\u3002 \u6700\u4f73\u5b9e\u8df5 \u9996\u9009\u901a\u8fc7\u5f15\u7528\u8fd4\u56de\u800c\u4e0d\u662f\u901a\u8fc7\u5730\u5740\u8fd4\u56de\uff0c\u9664\u975e\u8fd4\u56de\u201c\u65e0\u5bf9\u8c61\u201d(\u4f7f\u7528 nullptr )\u7684\u80fd\u529b\u5f88\u91cd\u8981\u3002","title":"\u6309\u5730\u5740\u8fd4\u56de"},{"location":"9-12-Type-deduction-with-pointers-references-and-const/","tags":["const","type deduction"],"text":"Key Takeaway \u5728 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86 auto \u5173\u952e\u5b57\uff0c\u5b83\u544a\u8bc9\u7f16\u8bd1\u5668\u6839\u636e\u521d\u59cb\u5316\u503c\u5bf9\u53d8\u91cf\u7684\u7c7b\u578b\u8fdb\u884c\u63a8\u65ad\uff1a 1 2 3 4 5 6 7 8 int getVal (); // some function that returns an int by value int main () { auto val { getVal () }; // val deduced as type int return 0 ; } \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u7c7b\u578b\u63a8\u65ad\u4f1a\u4e22\u5f03 const \u9650\u5b9a\u7b26\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 const double foo () { return 5.6 ; } int main () { const double cd { 7.8 }; auto x { cd }; // double (const dropped) auto y { foo () }; // double (const dropped) return 0 ; } \u5982\u679c\u9700\u8981\uff0c\u6211\u4eec\u9700\u8981\u624b\u52a8\u5c06 const \u9650\u5b9a\u7b26\u91cd\u65b0\u6dfb\u52a0\u5230\u5b9a\u4e49\u4e2d\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 const double foo () { return 5.6 ; } int main () { const double cd { 7.8 }; const auto x { cd }; // const double (const reapplied) const auto y { foo () }; // const double (const reapplied) return 0 ; } \u7c7b\u578b\u63a8\u65ad\u4f1a\u4e22\u5f03\u5f15\u7528\u4fee\u9970\u7b26 \u9664\u4e86\u5220\u9664const\u9650\u5b9a\u7b26\u5916\uff0c\u7c7b\u578b\u63a8\u65ad\u4e5f\u5c06\u5220\u9664\u5f15\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 #include std :: string & getRef (); // some function that returns a reference int main () { auto ref { getRef () }; // type deduced as std::string (not std::string&) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf ref \u4f7f\u7528\u4e86\u7c7b\u578b\u63a8\u65ad\u3002\u5c3d\u7ba1\u51fd\u6570 getRef() \u8fd4\u56de\u7684\u662f std::string& \uff0c\u4f46\u5f15\u7528\u9650\u5b9a\u7b26\u88ab\u4e22\u5f03\u4e86\uff0c\u6240\u4ee5 ref \u7684\u7c7b\u578b\u4e3a std::string \u3002 \u7c7b\u4f3c\u4e8e const \u9650\u5b9a\u7b26\uff0c\u5982\u679c\u5f15\u7528\u9650\u5b9a\u7b26\u88ab\u4e22\u5f03\u4e86\uff0c\u4e5f\u53ef\u4ee5\u663e\u5f0f\u5730\u88ab\u518d\u6b21\u52a0\u5230\u5b9a\u4e49\u4e0a\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include std :: string & getRef (); // some function that returns a reference to const int main () { auto ref1 { getRef () }; // std::string (reference dropped) auto & ref2 { getRef () }; // std::string& (reference reapplied) return 0 ; } \u9876\u5c42 const \u548c\u5e95\u5c42 const \u9876\u5c42 const \u662f\u5e94\u7528\u4e8e\u5bf9\u8c61\u672c\u8eab\u7684 const \u9650\u5b9a\u7b26 \u3002\u4f8b\u5982\uff1a 1 2 const int x ; // this const applies to x, so it is top-level int * const ptr ; // this const applies to ptr, so it is top-level \u76f8\u6bd4\u4e4b\u4e0b\uff0c \u5e95\u5c42 const \u662f\u4e00\u4e2a\u5e94\u7528\u4e8e\u88ab\u5f15\u7528\u6216\u6307\u5411\u7684\u5bf9\u8c61\u7684 const \u9650\u5b9a\u7b26 \uff1a 1 2 const int & ref ; // this const applies to the object being referenced, so it is low-level const int * ptr ; // this const applies to the object being pointed to, so it is low-level const \u503c\u7684\u5f15\u7528\u603b\u662f\u4e00\u4e2a\u5e95\u5c42const\u3002\u6307\u9488\u53ef\u4ee5\u662f\u9876\u5c42 const\u3001\u5e95\u5c42const\u6216\u4e24\u8005\u90fd\u662f\uff1a 1 const int * const ptr ; // the left const is low-level, the right const is top-level \u7c7b\u578b\u63a8\u65ad\u5728\u4e22\u5f03 const \u9650\u5b9a\u7b26\u65f6\uff0c\u53ea\u4f1a\u4e22\u5f03\u9876\u5c42 const\u3002\u5e95\u5c42 const \u4e0d\u53d7\u5f71\u54cd\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u4f8b\u5b50\u4e2d\u8bf4\u660e\u8fd9\u4e00\u70b9\u3002 \u7c7b\u578b\u63a8\u65ad\u548cconst\u5f15\u7528 \u5982\u679c\u521d\u59cb\u5316\u503c\u662f\u5bf9const\u7684\u5f15\u7528\uff0c\u5219\u9996\u5148\u5220\u9664\u5f15\u7528(\u5982\u679c\u9002\u7528\uff0c\u7136\u540e\u91cd\u65b0\u5e94\u7528)\uff0c\u7136\u540e\u4ece\u7ed3\u679c\u4e2d\u5220\u9664\u4efb\u4f55\u9876\u7ea7const\u3002 1 2 3 4 5 6 7 8 9 10 #include const std :: string & getRef (); // some function that returns a reference to const int main () { auto ref1 { getRef () }; // std::string (reference dropped, then top-level const dropped from result) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c getRef() \u8fd4\u56de const std::string& \uff0c\u8be5\u5f15\u7528\u4f1a\u88ab\u9996\u5148\u4e22\u5f03\uff0c\u8fd4\u56de\u503c\u7c7b\u578b\u53d8\u4e3a const std::string \u3002\u6b64\u65f6\uff0c\u8be5 const \u662f\u4e00\u4e2a\u9876\u5c42 const\uff0c\u6240\u4ee5\u4e5f\u4f1a\u88ab\u4e22\u5f03\uff0c\u8fd4\u56de\u503c\u7c7b\u578b\u8fdb\u4e00\u6b65\u53d8\u4e3a std::string \u3002 \u4e24\u79cd\u6807\u8bc6\u7b26\u90fd\u53ef\u4ee5\u88ab\u624b\u52a8\u586b\u56de\u53bb\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include const std :: string & getRef (); // some function that returns a const reference int main () { auto ref1 { getRef () }; // std::string (top-level const and reference dropped) const auto ref2 { getRef () }; // const std::string (const reapplied, reference dropped) auto & ref3 { getRef () }; // const std::string& (reference reapplied, low-level const not dropped) const auto & ref4 { getRef () }; // const std::string& (reference and const reapplied) return 0 ; } ref1 \u5df2\u7ecf\u5728\u4e0a\u4e00\u4e2a\u4f8b\u5b50\u4e2d\u4ecb\u7ecd\u8fc7\u4e86\uff0c\u5bf9\u4e8e ref2 \uff0c\u5b83\u548c ref1 \u7c7b\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u91cd\u65b0\u6dfb\u52a0\u4e86 const \u9650\u5b9a\u7b26\uff0c\u6240\u4ee5\u6700\u7ec8\u7c7b\u578b\u4e3a const std::string \u3002 ref3 \u5219\u9700\u8981\u7279\u522b\u6ce8\u610f\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5f15\u7528\u4f1a\u88ab\u5220\u9664\uff0c\u4f46\u7531\u4e8e\u6211\u4eec\u5df2\u7ecf\u91cd\u65b0\u5e94\u7528\u4e86\u5f15\u7528\uff0c\u6240\u4ee5\u5b83\u4e0d\u4f1a\u88ab\u5220\u9664\u3002\u8fd9\u610f\u5473\u7740\u7c7b\u578b\u4ecd\u7136\u662f const std::string& \u3002\u56e0\u4e3a\u8fd9\u4e2aconst\u662f\u4e00\u4e2a\u5e95\u5c42\u7684const\uff0c\u6240\u4ee5\u5b83\u4e0d\u4f1a\u88ab\u4e22\u5f03\u3002\u56e0\u6b64\uff0c\u63a8\u5bfc\u51fa\u7684\u7c7b\u578b\u662f const std::string& \u3002 ref4 \u7684\u60c5\u51b5\u7c7b\u4f3c\u4e8e ref3 \uff0c\u53ea\u4e0d\u8fc7\u91cd\u65b0\u6dfb\u52a0\u5927\u91cf const \u9650\u5b9a\u7b26\u3002\u7531\u4e8e\u7c7b\u578b\u5df2\u7ecf\u63a8\u65ad\u4e3a\u6307\u5411\u5e38\u91cf\u7684\u5f15\u7528\uff0c\u6240\u4ee5\u6dfb\u52a0 const \u5176\u5b9e\u662f\u591a\u4f59\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8fd9\u91cc\u6211\u4eec\u660e\u786e\u5730\u6307\u51fa\u7ed3\u679c\u5e94\u8be5\u662f const \u7684\uff0c\u542b\u4e49\u975e\u5e38\u6e05\u695a\uff08 ref3 \u7684\u4f8b\u5b50\u4e2d\uff0c\u5e38\u91cf\u6027\u662f\u9690\u5f0f\u7684\uff0c\u5e76\u6ca1\u6709\u90a3\u4e48\u660e\u663e\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f60\u60f3\u8981\u4e00\u4e2aconst\u5f15\u7528\uff0c\u5373\u4f7f\u4e0d\u662f\u4e25\u683c\u5fc5\u8981\u7684\uff0c\u4e5f\u8981\u91cd\u65b0\u5e94\u7528 const \u9650\u5b9a\u7b26\uff0c\u56e0\u4e3a\u5b83\u4f7f\u4f60\u7684\u610f\u56fe\u66f4\u660e\u786e\uff0c\u5e76\u6709\u52a9\u4e8e\u9632\u6b62\u9519\u8bef\u3002 \u7c7b\u578b\u63a8\u65ad\u548c\u6307\u9488 \u4e0e\u5f15\u7528\u4e0d\u540c\uff0c\u7c7b\u578b\u6f14\u7ece\u4e0d\u4f1a\u4e22\u5f03\u6307\u9488\uff1a 1 2 3 4 5 6 7 8 9 10 #include std :: string * getPtr (); // some function that returns a pointer int main () { auto ptr1 { getPtr () }; // std::string* return 0 ; } \u6211\u4eec\u4e5f\u53ef\u4ee5\u5728\u6307\u9488\u7c7b\u578b\u6f14\u7ece\u7684\u540c\u65f6\u4f7f\u7528\u661f\u53f7\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include std :: string * getPtr (); // some function that returns a pointer int main () { auto ptr1 { getPtr () }; // std::string* auto * ptr2 { getPtr () }; // std::string* return 0 ; } auto \u548c auto* \u7684\u533a\u522b(\u9009\u8bfb) \u5728\u4f7f\u7528 auto \u548c\u6307\u9488\u7c7b\u578b\u7684\u521d\u59cb\u5316\u503c\u65f6\uff0c\u7c7b\u578b\u63a8\u65ad\u4f1a\u5305\u62ec\u6307\u9488\uff0c\u6240\u4ee5\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684 ptr1 \u7c7b\u578b\u4f1a\u88ab\u66ff\u6362\uff1a std::string* \u3002 \u5982\u679c\u5c06 auto* \u548c\u6307\u9488\u7c7b\u578b\u521d\u59cb\u5316\u503c\u4e00\u8d77\u4f7f\u7528\uff0c\u5219 auto \u7684\u7c7b\u578b\u63a8\u65ad\u4e0d\u4f1a\u5305\u542b\u6307\u9488\u2014\u2014\u6307\u9488\u662f\u5728\u7c7b\u578b\u63a8\u65ad\u5b8c\u6210\u540e\u88ab\u52a0\u4e0a\u7684\u3002\u6240\u4ee5\u5bf9\u4e8e\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684 ptr2 \uff0c auto \u66ff\u6362\u4e3a std::string \uff0c\u800c\u6307\u9488\u7684\u661f\u53f7\u662f\u540e\u52a0\u4e0a\u53bb\u7684\u3002 \u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4e0a\u9762\u4e24\u4e2a\u4f8b\u5b50\u7684\u5b9e\u9645\u6548\u679c\u662f\u4e00\u6837\u7684( ptr1 \u548c ptr2 \u90fd\u6700\u7ec8\u63a8\u65ad\u4e3a std::string* )\u3002 \u4f46\u662f\uff0c\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d auto \u548c auto* \u8fd8\u662f\u5b58\u5728\u4e0d\u5c11\u533a\u522b\u7684\u3002\u9996\u5148 auto* \u5fc5\u987b\u89e3\u6790\u4e3a\u4e00\u4e2a\u6307\u9488\u521d\u59cb\u5316\u503c\uff0c\u5426\u5219\u7f16\u8bd1\u4f1a\u62a5\u9519\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include std :: string * getPtr (); // some function that returns a pointer int main () { auto ptr3 { * getPtr () }; // std::string (\u5bf9 getPtr() \u89e3\u5f15\u7528\u4e86) auto * ptr4 { * getPtr () }; // \u4e0d\u80fd\u7f16\u8bd1 (\u521d\u59cb\u5316\u503c\u4e0d\u662f\u6307\u9488) return 0 ; } \u8fd9\u4e48\u505a\u662f\u6709\u610f\u4e49\u7684\uff0c\u5728 ptr4 \u7684\u4f8b\u5b50\u4e2d\uff0c auto \u5f97\u5230\u4e86 std::string \uff0c\u7136\u540e\u6307\u9488\u88ab\u91cd\u65b0\u52a0\u4e0a\u3002\u56e0\u6b64 ptr4 \u7684\u7c7b\u578b\u662f std::string* \uff0c\u6211\u4eec\u4e0d\u80fd\u4f7f\u7528\u4e00\u4e2a\u975e\u6307\u9488\u521d\u59cb\u5316\u503c\u6765\u521d\u59cb\u5316 std::string* \u7c7b\u578b\u7684\u53d8\u91cf\u3002 \u5176\u6b21\uff0c\u5f53\u8868\u8fbe\u5f0f\u4e2d\u51fa\u73b0 const \u65f6\uff0c auto \u548c auto* \u7684\u884c\u4e3a\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e00\u70b9\u3002 \u7c7b\u578b\u63a8\u65ad\u548cconst\u6307\u9488(\u9009\u8bfb) \u56e0\u4e3a\u6307\u9488\u4e0d\u4f1a\u88ab\u4e22\u5f03\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u5fc5\u62c5\u5fc3\u8fd9\u4e2a\u95ee\u9898\u3002\u4f46\u662f\u5bf9\u4e8e\u6307\u9488\uff0c\u6211\u4eec\u65e2\u8981\u8003\u8651\u6307\u9488\u5e38\u91cf\u4e5f\u8981\u8003\u8651\u5e38\u91cf\u6307\u9488\uff0c\u4ee5\u53ca auto \u548c auto* \u3002\u5c31\u50cf\u5f15\u7528\u4e00\u6837\uff0c\u5728\u6307\u9488\u7c7b\u578b\u63a8\u65ad\u671f\u95f4\uff0c\u53ea\u6709\u9876\u5c42const\u4f1a\u88ab\u4e22\u5f03\u3002 \u5148\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include std :: string * getPtr (); // some function that returns a pointer int main () { const auto ptr1 { getPtr () }; // std::string* const auto const ptr2 { getPtr () }; // std::string* const const auto * ptr3 { getPtr () }; // const std::string* auto * const ptr4 { getPtr () }; // std::string* const return 0 ; } \u4e0d\u8bba\u662f auto const \u8fd8\u662f const auto \uff0c\u90fd\u8868\u793a \u201c\u5c06\u7c7b\u578b\u63a8\u65ad\u7ed3\u679c\u53d8\u4e3aconst\u201d\u3002\u6240\u4ee5 ptr1 \u548c ptr2 \u7684\u4f8b\u5b50\u4e2d\uff0c\u63a8\u65ad\u7684\u7ed3\u679c\u4e3a std::string* \uff0c\u968f\u5373\u53c8\u88ab\u6dfb\u52a0\u4e86 const \uff0c\u6700\u7ec8\u5f97\u5230 std::string* const \u3002\u8fd9\u5c31\u548c const int \u4e0e int const \u662f\u4e00\u56de\u4e8b\u4e00\u6837\u3002 \u4f46\u662f\u5bf9\u4e8e auto* \uff0c const \u9650\u5b9a\u7b26\u7684\u4f4d\u7f6e\u4f1a\u5e26\u6765\u5f71\u54cd\u3002 const \u5728\u5de6\u9762\u8868\u793a\u201c\u63a8\u65ad\u5f97\u5230\u7684\u6307\u9488\u7c7b\u578b\u6307\u5411\u5e38\u91cf\u201d\uff0c\u800c const \u5728\u53f3\u65f6\u8868\u793a \u201c\u63a8\u65ad\u5f97\u5230\u7684\u6307\u9488\u4f5c\u4e3aconst\u6307\u9488\u201d\u3002\u56e0\u6b64 ptr3 \u6700\u7ec8\u662f\u4ee5\u4e00\u4e2a\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488\uff0c\u800c ptr4 \u5219\u662f\u4e00\u4e2a\u5e38\u91cf\u6307\u9488\u3002 \u518d\u770b\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5176\u4e2d\u521d\u59cb\u5316\u503c\u662f\u6307\u5411\u5e38\u91cf\u7684\u5e38\u91cf\u6307\u9488\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include const std :: string * const getConstPtr (); // some function that returns a const pointer to a const value int main () { auto ptr1 { getConstPtr () }; // const std::string* auto * ptr2 { getConstPtr () }; // const std::string* auto const ptr3 { getConstPtr () }; // const std::string* const const auto ptr4 { getConstPtr () }; // const std::string* const auto * const ptr5 { getConstPtr () }; // const std::string* const const auto * ptr6 { getConstPtr () }; // const std::string* const auto const ptr7 { getConstPtr () }; // error: const qualifer can not be applied twice const auto * const ptr8 { getConstPtr () }; // const std::string* const return 0 ; } ptr1 \u548c ptr2 \u5f88\u7b80\u5355\u3002\u9876\u5c42 const (\u4fee\u9970\u6307\u9488\u7684 const ) \u88ab\u4e22\u5f03\u3002\u4fee\u9970\u88ab\u6307\u5bf9\u8c61\u7684\u5e95\u5c42 const \u5219\u88ab\u4fdd\u6301\u4e0b\u6765\u3002\u6240\u4ee5\u5728\u8fd9\u4e24\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6700\u7ec8\u7684\u7c7b\u578b\u90fd\u662f const std::string* \u3002 ptr3 \u548c ptr4 \u7684\u4f8b\u5b50\u4e5f\u5f88\u7b80\u5355\u3002\u9876\u5c42 const \u88ab\u4e22\u5f03\uff0c\u4f46\u662f\u53c8\u88ab\u91cd\u65b0\u6dfb\u52a0\u4e86\u3002\u4fee\u9970\u88ab\u6307\u5bf9\u8c61\u7684\u5e95\u5c42 const \u88ab\u4fdd\u7559\u3002\u6240\u4ee5\u6700\u7ec8\u7c7b\u578b\u4e3a const std::string* const \u3002 ptr5 \u548c ptr6 \u4e0e\u4e4b\u524d\u4f8b\u5b50\u4e2d\u7684\u60c5\u51b5\u7c7b\u4f3c\u3002\u5b83\u4eec\u7684\u9876\u5c42 const \u90fd\u88ab\u4e22\u5f03\u4e86\u3002\u5bf9\u4e8e ptr5 \uff0c auto* const \u88ab\u91cd\u65b0\u6dfb\u52a0\uff0c\u6700\u7ec8\u7c7b\u578b\u4e3a const std::string* const \u3002\u5bf9\u4e8e ptr6 \uff0c const auto* \u5e94\u7528\u4e8e\u88ab\u6307\u5bf9\u8c61\uff08\u6b64\u65f6\u5df2\u7ecf\u662f const \uff09\uff0c\u56e0\u6b64\u6700\u7ec8\u7c7b\u578b\u4e3a const std::string* \u3002 \u5bf9\u4e8e ptr7 \uff0c\u6211\u4eec\u5e94\u7528\u4e86\u4e24\u6b21 const \u9650\u5b9a\u7b26\uff0c\u8fd9\u662f\u4e0d\u5141\u8bb8\u7684\uff0c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u62a5\u9519\u3002 \u5bf9\u4e8e ptr8 \uff0c\u6211\u4eec\u5728\u6307\u9488\u7684\u4e24\u4fa7\u6dfb\u52a0\u4e86 const \uff08\u53ef\u4ee5\u8fd9\u4e48\u505a\u56e0\u4e3a auto* \u4e00\u5b9a\u662f\u6307\u9488\u7c7b\u578b\uff09\uff0c\u6240\u4ee5\u6700\u7ec8\u7ed3\u679c\u4e3a const std::string* const \u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a const \u6307\u9488\uff0c\u5373\u4f7f\u4e0d\u662f\u4e25\u683c\u5fc5\u8981\u7684\uff0c\u4e5f\u8981\u91cd\u65b0\u5e94\u7528 const \u9650\u5b9a\u7b26\uff0c\u56e0\u4e3a\u5b83\u4f7f\u4f60\u7684\u610f\u56fe\u66f4\u660e\u786e\uff0c\u5e76\u6709\u52a9\u4e8e\u9632\u6b62\u9519\u8bef\u3002","title":"9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad"},{"location":"9-12-Type-deduction-with-pointers-references-and-const/#_1","text":"\u9664\u4e86\u5220\u9664const\u9650\u5b9a\u7b26\u5916\uff0c\u7c7b\u578b\u63a8\u65ad\u4e5f\u5c06\u5220\u9664\u5f15\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 #include std :: string & getRef (); // some function that returns a reference int main () { auto ref { getRef () }; // type deduced as std::string (not std::string&) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf ref \u4f7f\u7528\u4e86\u7c7b\u578b\u63a8\u65ad\u3002\u5c3d\u7ba1\u51fd\u6570 getRef() \u8fd4\u56de\u7684\u662f std::string& \uff0c\u4f46\u5f15\u7528\u9650\u5b9a\u7b26\u88ab\u4e22\u5f03\u4e86\uff0c\u6240\u4ee5 ref \u7684\u7c7b\u578b\u4e3a std::string \u3002 \u7c7b\u4f3c\u4e8e const \u9650\u5b9a\u7b26\uff0c\u5982\u679c\u5f15\u7528\u9650\u5b9a\u7b26\u88ab\u4e22\u5f03\u4e86\uff0c\u4e5f\u53ef\u4ee5\u663e\u5f0f\u5730\u88ab\u518d\u6b21\u52a0\u5230\u5b9a\u4e49\u4e0a\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include std :: string & getRef (); // some function that returns a reference to const int main () { auto ref1 { getRef () }; // std::string (reference dropped) auto & ref2 { getRef () }; // std::string& (reference reapplied) return 0 ; }","title":"\u7c7b\u578b\u63a8\u65ad\u4f1a\u4e22\u5f03\u5f15\u7528\u4fee\u9970\u7b26"},{"location":"9-12-Type-deduction-with-pointers-references-and-const/#const-const","text":"\u9876\u5c42 const \u662f\u5e94\u7528\u4e8e\u5bf9\u8c61\u672c\u8eab\u7684 const \u9650\u5b9a\u7b26 \u3002\u4f8b\u5982\uff1a 1 2 const int x ; // this const applies to x, so it is top-level int * const ptr ; // this const applies to ptr, so it is top-level \u76f8\u6bd4\u4e4b\u4e0b\uff0c \u5e95\u5c42 const \u662f\u4e00\u4e2a\u5e94\u7528\u4e8e\u88ab\u5f15\u7528\u6216\u6307\u5411\u7684\u5bf9\u8c61\u7684 const \u9650\u5b9a\u7b26 \uff1a 1 2 const int & ref ; // this const applies to the object being referenced, so it is low-level const int * ptr ; // this const applies to the object being pointed to, so it is low-level const \u503c\u7684\u5f15\u7528\u603b\u662f\u4e00\u4e2a\u5e95\u5c42const\u3002\u6307\u9488\u53ef\u4ee5\u662f\u9876\u5c42 const\u3001\u5e95\u5c42const\u6216\u4e24\u8005\u90fd\u662f\uff1a 1 const int * const ptr ; // the left const is low-level, the right const is top-level \u7c7b\u578b\u63a8\u65ad\u5728\u4e22\u5f03 const \u9650\u5b9a\u7b26\u65f6\uff0c\u53ea\u4f1a\u4e22\u5f03\u9876\u5c42 const\u3002\u5e95\u5c42 const \u4e0d\u53d7\u5f71\u54cd\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u4f8b\u5b50\u4e2d\u8bf4\u660e\u8fd9\u4e00\u70b9\u3002","title":"\u9876\u5c42 const \u548c\u5e95\u5c42 const"},{"location":"9-12-Type-deduction-with-pointers-references-and-const/#const","text":"\u5982\u679c\u521d\u59cb\u5316\u503c\u662f\u5bf9const\u7684\u5f15\u7528\uff0c\u5219\u9996\u5148\u5220\u9664\u5f15\u7528(\u5982\u679c\u9002\u7528\uff0c\u7136\u540e\u91cd\u65b0\u5e94\u7528)\uff0c\u7136\u540e\u4ece\u7ed3\u679c\u4e2d\u5220\u9664\u4efb\u4f55\u9876\u7ea7const\u3002 1 2 3 4 5 6 7 8 9 10 #include const std :: string & getRef (); // some function that returns a reference to const int main () { auto ref1 { getRef () }; // std::string (reference dropped, then top-level const dropped from result) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c getRef() \u8fd4\u56de const std::string& \uff0c\u8be5\u5f15\u7528\u4f1a\u88ab\u9996\u5148\u4e22\u5f03\uff0c\u8fd4\u56de\u503c\u7c7b\u578b\u53d8\u4e3a const std::string \u3002\u6b64\u65f6\uff0c\u8be5 const \u662f\u4e00\u4e2a\u9876\u5c42 const\uff0c\u6240\u4ee5\u4e5f\u4f1a\u88ab\u4e22\u5f03\uff0c\u8fd4\u56de\u503c\u7c7b\u578b\u8fdb\u4e00\u6b65\u53d8\u4e3a std::string \u3002 \u4e24\u79cd\u6807\u8bc6\u7b26\u90fd\u53ef\u4ee5\u88ab\u624b\u52a8\u586b\u56de\u53bb\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include const std :: string & getRef (); // some function that returns a const reference int main () { auto ref1 { getRef () }; // std::string (top-level const and reference dropped) const auto ref2 { getRef () }; // const std::string (const reapplied, reference dropped) auto & ref3 { getRef () }; // const std::string& (reference reapplied, low-level const not dropped) const auto & ref4 { getRef () }; // const std::string& (reference and const reapplied) return 0 ; } ref1 \u5df2\u7ecf\u5728\u4e0a\u4e00\u4e2a\u4f8b\u5b50\u4e2d\u4ecb\u7ecd\u8fc7\u4e86\uff0c\u5bf9\u4e8e ref2 \uff0c\u5b83\u548c ref1 \u7c7b\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u91cd\u65b0\u6dfb\u52a0\u4e86 const \u9650\u5b9a\u7b26\uff0c\u6240\u4ee5\u6700\u7ec8\u7c7b\u578b\u4e3a const std::string \u3002 ref3 \u5219\u9700\u8981\u7279\u522b\u6ce8\u610f\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5f15\u7528\u4f1a\u88ab\u5220\u9664\uff0c\u4f46\u7531\u4e8e\u6211\u4eec\u5df2\u7ecf\u91cd\u65b0\u5e94\u7528\u4e86\u5f15\u7528\uff0c\u6240\u4ee5\u5b83\u4e0d\u4f1a\u88ab\u5220\u9664\u3002\u8fd9\u610f\u5473\u7740\u7c7b\u578b\u4ecd\u7136\u662f const std::string& \u3002\u56e0\u4e3a\u8fd9\u4e2aconst\u662f\u4e00\u4e2a\u5e95\u5c42\u7684const\uff0c\u6240\u4ee5\u5b83\u4e0d\u4f1a\u88ab\u4e22\u5f03\u3002\u56e0\u6b64\uff0c\u63a8\u5bfc\u51fa\u7684\u7c7b\u578b\u662f const std::string& \u3002 ref4 \u7684\u60c5\u51b5\u7c7b\u4f3c\u4e8e ref3 \uff0c\u53ea\u4e0d\u8fc7\u91cd\u65b0\u6dfb\u52a0\u5927\u91cf const \u9650\u5b9a\u7b26\u3002\u7531\u4e8e\u7c7b\u578b\u5df2\u7ecf\u63a8\u65ad\u4e3a\u6307\u5411\u5e38\u91cf\u7684\u5f15\u7528\uff0c\u6240\u4ee5\u6dfb\u52a0 const \u5176\u5b9e\u662f\u591a\u4f59\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8fd9\u91cc\u6211\u4eec\u660e\u786e\u5730\u6307\u51fa\u7ed3\u679c\u5e94\u8be5\u662f const \u7684\uff0c\u542b\u4e49\u975e\u5e38\u6e05\u695a\uff08 ref3 \u7684\u4f8b\u5b50\u4e2d\uff0c\u5e38\u91cf\u6027\u662f\u9690\u5f0f\u7684\uff0c\u5e76\u6ca1\u6709\u90a3\u4e48\u660e\u663e\uff09\u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f60\u60f3\u8981\u4e00\u4e2aconst\u5f15\u7528\uff0c\u5373\u4f7f\u4e0d\u662f\u4e25\u683c\u5fc5\u8981\u7684\uff0c\u4e5f\u8981\u91cd\u65b0\u5e94\u7528 const \u9650\u5b9a\u7b26\uff0c\u56e0\u4e3a\u5b83\u4f7f\u4f60\u7684\u610f\u56fe\u66f4\u660e\u786e\uff0c\u5e76\u6709\u52a9\u4e8e\u9632\u6b62\u9519\u8bef\u3002","title":"\u7c7b\u578b\u63a8\u65ad\u548cconst\u5f15\u7528"},{"location":"9-12-Type-deduction-with-pointers-references-and-const/#_2","text":"\u4e0e\u5f15\u7528\u4e0d\u540c\uff0c\u7c7b\u578b\u6f14\u7ece\u4e0d\u4f1a\u4e22\u5f03\u6307\u9488\uff1a 1 2 3 4 5 6 7 8 9 10 #include std :: string * getPtr (); // some function that returns a pointer int main () { auto ptr1 { getPtr () }; // std::string* return 0 ; } \u6211\u4eec\u4e5f\u53ef\u4ee5\u5728\u6307\u9488\u7c7b\u578b\u6f14\u7ece\u7684\u540c\u65f6\u4f7f\u7528\u661f\u53f7\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include std :: string * getPtr (); // some function that returns a pointer int main () { auto ptr1 { getPtr () }; // std::string* auto * ptr2 { getPtr () }; // std::string* return 0 ; }","title":"\u7c7b\u578b\u63a8\u65ad\u548c\u6307\u9488"},{"location":"9-12-Type-deduction-with-pointers-references-and-const/#auto-auto","text":"\u5728\u4f7f\u7528 auto \u548c\u6307\u9488\u7c7b\u578b\u7684\u521d\u59cb\u5316\u503c\u65f6\uff0c\u7c7b\u578b\u63a8\u65ad\u4f1a\u5305\u62ec\u6307\u9488\uff0c\u6240\u4ee5\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684 ptr1 \u7c7b\u578b\u4f1a\u88ab\u66ff\u6362\uff1a std::string* \u3002 \u5982\u679c\u5c06 auto* \u548c\u6307\u9488\u7c7b\u578b\u521d\u59cb\u5316\u503c\u4e00\u8d77\u4f7f\u7528\uff0c\u5219 auto \u7684\u7c7b\u578b\u63a8\u65ad\u4e0d\u4f1a\u5305\u542b\u6307\u9488\u2014\u2014\u6307\u9488\u662f\u5728\u7c7b\u578b\u63a8\u65ad\u5b8c\u6210\u540e\u88ab\u52a0\u4e0a\u7684\u3002\u6240\u4ee5\u5bf9\u4e8e\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684 ptr2 \uff0c auto \u66ff\u6362\u4e3a std::string \uff0c\u800c\u6307\u9488\u7684\u661f\u53f7\u662f\u540e\u52a0\u4e0a\u53bb\u7684\u3002 \u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4e0a\u9762\u4e24\u4e2a\u4f8b\u5b50\u7684\u5b9e\u9645\u6548\u679c\u662f\u4e00\u6837\u7684( ptr1 \u548c ptr2 \u90fd\u6700\u7ec8\u63a8\u65ad\u4e3a std::string* )\u3002 \u4f46\u662f\uff0c\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d auto \u548c auto* \u8fd8\u662f\u5b58\u5728\u4e0d\u5c11\u533a\u522b\u7684\u3002\u9996\u5148 auto* \u5fc5\u987b\u89e3\u6790\u4e3a\u4e00\u4e2a\u6307\u9488\u521d\u59cb\u5316\u503c\uff0c\u5426\u5219\u7f16\u8bd1\u4f1a\u62a5\u9519\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include std :: string * getPtr (); // some function that returns a pointer int main () { auto ptr3 { * getPtr () }; // std::string (\u5bf9 getPtr() \u89e3\u5f15\u7528\u4e86) auto * ptr4 { * getPtr () }; // \u4e0d\u80fd\u7f16\u8bd1 (\u521d\u59cb\u5316\u503c\u4e0d\u662f\u6307\u9488) return 0 ; } \u8fd9\u4e48\u505a\u662f\u6709\u610f\u4e49\u7684\uff0c\u5728 ptr4 \u7684\u4f8b\u5b50\u4e2d\uff0c auto \u5f97\u5230\u4e86 std::string \uff0c\u7136\u540e\u6307\u9488\u88ab\u91cd\u65b0\u52a0\u4e0a\u3002\u56e0\u6b64 ptr4 \u7684\u7c7b\u578b\u662f std::string* \uff0c\u6211\u4eec\u4e0d\u80fd\u4f7f\u7528\u4e00\u4e2a\u975e\u6307\u9488\u521d\u59cb\u5316\u503c\u6765\u521d\u59cb\u5316 std::string* \u7c7b\u578b\u7684\u53d8\u91cf\u3002 \u5176\u6b21\uff0c\u5f53\u8868\u8fbe\u5f0f\u4e2d\u51fa\u73b0 const \u65f6\uff0c auto \u548c auto* \u7684\u884c\u4e3a\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e00\u70b9\u3002","title":"auto \u548c auto* \u7684\u533a\u522b(\u9009\u8bfb)"},{"location":"9-12-Type-deduction-with-pointers-references-and-const/#const_1","text":"\u56e0\u4e3a\u6307\u9488\u4e0d\u4f1a\u88ab\u4e22\u5f03\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u5fc5\u62c5\u5fc3\u8fd9\u4e2a\u95ee\u9898\u3002\u4f46\u662f\u5bf9\u4e8e\u6307\u9488\uff0c\u6211\u4eec\u65e2\u8981\u8003\u8651\u6307\u9488\u5e38\u91cf\u4e5f\u8981\u8003\u8651\u5e38\u91cf\u6307\u9488\uff0c\u4ee5\u53ca auto \u548c auto* \u3002\u5c31\u50cf\u5f15\u7528\u4e00\u6837\uff0c\u5728\u6307\u9488\u7c7b\u578b\u63a8\u65ad\u671f\u95f4\uff0c\u53ea\u6709\u9876\u5c42const\u4f1a\u88ab\u4e22\u5f03\u3002 \u5148\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include std :: string * getPtr (); // some function that returns a pointer int main () { const auto ptr1 { getPtr () }; // std::string* const auto const ptr2 { getPtr () }; // std::string* const const auto * ptr3 { getPtr () }; // const std::string* auto * const ptr4 { getPtr () }; // std::string* const return 0 ; } \u4e0d\u8bba\u662f auto const \u8fd8\u662f const auto \uff0c\u90fd\u8868\u793a \u201c\u5c06\u7c7b\u578b\u63a8\u65ad\u7ed3\u679c\u53d8\u4e3aconst\u201d\u3002\u6240\u4ee5 ptr1 \u548c ptr2 \u7684\u4f8b\u5b50\u4e2d\uff0c\u63a8\u65ad\u7684\u7ed3\u679c\u4e3a std::string* \uff0c\u968f\u5373\u53c8\u88ab\u6dfb\u52a0\u4e86 const \uff0c\u6700\u7ec8\u5f97\u5230 std::string* const \u3002\u8fd9\u5c31\u548c const int \u4e0e int const \u662f\u4e00\u56de\u4e8b\u4e00\u6837\u3002 \u4f46\u662f\u5bf9\u4e8e auto* \uff0c const \u9650\u5b9a\u7b26\u7684\u4f4d\u7f6e\u4f1a\u5e26\u6765\u5f71\u54cd\u3002 const \u5728\u5de6\u9762\u8868\u793a\u201c\u63a8\u65ad\u5f97\u5230\u7684\u6307\u9488\u7c7b\u578b\u6307\u5411\u5e38\u91cf\u201d\uff0c\u800c const \u5728\u53f3\u65f6\u8868\u793a \u201c\u63a8\u65ad\u5f97\u5230\u7684\u6307\u9488\u4f5c\u4e3aconst\u6307\u9488\u201d\u3002\u56e0\u6b64 ptr3 \u6700\u7ec8\u662f\u4ee5\u4e00\u4e2a\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488\uff0c\u800c ptr4 \u5219\u662f\u4e00\u4e2a\u5e38\u91cf\u6307\u9488\u3002 \u518d\u770b\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5176\u4e2d\u521d\u59cb\u5316\u503c\u662f\u6307\u5411\u5e38\u91cf\u7684\u5e38\u91cf\u6307\u9488\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include const std :: string * const getConstPtr (); // some function that returns a const pointer to a const value int main () { auto ptr1 { getConstPtr () }; // const std::string* auto * ptr2 { getConstPtr () }; // const std::string* auto const ptr3 { getConstPtr () }; // const std::string* const const auto ptr4 { getConstPtr () }; // const std::string* const auto * const ptr5 { getConstPtr () }; // const std::string* const const auto * ptr6 { getConstPtr () }; // const std::string* const auto const ptr7 { getConstPtr () }; // error: const qualifer can not be applied twice const auto * const ptr8 { getConstPtr () }; // const std::string* const return 0 ; } ptr1 \u548c ptr2 \u5f88\u7b80\u5355\u3002\u9876\u5c42 const (\u4fee\u9970\u6307\u9488\u7684 const ) \u88ab\u4e22\u5f03\u3002\u4fee\u9970\u88ab\u6307\u5bf9\u8c61\u7684\u5e95\u5c42 const \u5219\u88ab\u4fdd\u6301\u4e0b\u6765\u3002\u6240\u4ee5\u5728\u8fd9\u4e24\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6700\u7ec8\u7684\u7c7b\u578b\u90fd\u662f const std::string* \u3002 ptr3 \u548c ptr4 \u7684\u4f8b\u5b50\u4e5f\u5f88\u7b80\u5355\u3002\u9876\u5c42 const \u88ab\u4e22\u5f03\uff0c\u4f46\u662f\u53c8\u88ab\u91cd\u65b0\u6dfb\u52a0\u4e86\u3002\u4fee\u9970\u88ab\u6307\u5bf9\u8c61\u7684\u5e95\u5c42 const \u88ab\u4fdd\u7559\u3002\u6240\u4ee5\u6700\u7ec8\u7c7b\u578b\u4e3a const std::string* const \u3002 ptr5 \u548c ptr6 \u4e0e\u4e4b\u524d\u4f8b\u5b50\u4e2d\u7684\u60c5\u51b5\u7c7b\u4f3c\u3002\u5b83\u4eec\u7684\u9876\u5c42 const \u90fd\u88ab\u4e22\u5f03\u4e86\u3002\u5bf9\u4e8e ptr5 \uff0c auto* const \u88ab\u91cd\u65b0\u6dfb\u52a0\uff0c\u6700\u7ec8\u7c7b\u578b\u4e3a const std::string* const \u3002\u5bf9\u4e8e ptr6 \uff0c const auto* \u5e94\u7528\u4e8e\u88ab\u6307\u5bf9\u8c61\uff08\u6b64\u65f6\u5df2\u7ecf\u662f const \uff09\uff0c\u56e0\u6b64\u6700\u7ec8\u7c7b\u578b\u4e3a const std::string* \u3002 \u5bf9\u4e8e ptr7 \uff0c\u6211\u4eec\u5e94\u7528\u4e86\u4e24\u6b21 const \u9650\u5b9a\u7b26\uff0c\u8fd9\u662f\u4e0d\u5141\u8bb8\u7684\uff0c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u62a5\u9519\u3002 \u5bf9\u4e8e ptr8 \uff0c\u6211\u4eec\u5728\u6307\u9488\u7684\u4e24\u4fa7\u6dfb\u52a0\u4e86 const \uff08\u53ef\u4ee5\u8fd9\u4e48\u505a\u56e0\u4e3a auto* \u4e00\u5b9a\u662f\u6307\u9488\u7c7b\u578b\uff09\uff0c\u6240\u4ee5\u6700\u7ec8\u7ed3\u679c\u4e3a const std::string* const \u3002 \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a const \u6307\u9488\uff0c\u5373\u4f7f\u4e0d\u662f\u4e25\u683c\u5fc5\u8981\u7684\uff0c\u4e5f\u8981\u91cd\u65b0\u5e94\u7528 const \u9650\u5b9a\u7b26\uff0c\u56e0\u4e3a\u5b83\u4f7f\u4f60\u7684\u610f\u56fe\u66f4\u660e\u786e\uff0c\u5e76\u6709\u52a9\u4e8e\u9632\u6b62\u9519\u8bef\u3002","title":"\u7c7b\u578b\u63a8\u65ad\u548cconst\u6307\u9488(\u9009\u8bfb)"},{"location":"9-2-Value-categories-lvalues-and-rvalues/","tags":["lvalues","rvalues"],"text":"Key Takeaway \u5de6\u503c\u5c31\u662f\u89e3\u6790\u5230\u6709\u6807\u8bc6\u7b26\u7684\u8868\u8fbe\u5f0f \u8bb0\u5fc6\u7a8d\u95e8\uff1a\u5de6\u503c\u51fa\u73b0\u5728\u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u5de6\u8fb9\uff0c\u53f3\u503c\u51fa\u73b0\u5728\u8d4b\u503c\u8868\u8fbe\u5f0f\u7684\u53f3\u8fb9\u3002\u5de6\u4fa7\u7684\u662f\u88ab\u8d4b\u503c\u7684\u4e00\u65b9\uff0c\u56e0\u6b64\u5fc5\u987b\u662f\u53ef\u53d8\u7684\u4e14\u5177\u6709\u6807\u8bc6\u7b26\u3002\u53f3\u4fa7\u663e\u7136\u53ef\u4ee5\u662f\u5b57\u9762\u91cf\u3001\u51fd\u6570\u548c\u5bf9\u8c61\uff0c\u5b83\u4eec\u90fd\u5c5e\u4e8e\u53f3\u503c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u56e0\u4e3a\u5de6\u503c\u53ef\u4ee5\u81ea\u52a8\u8f6c\u6362\u4e3a\u53f3\u503c\uff0c\u6240\u4ee5\u5de6\u503c\u53ef\u4ee5\u51fa\u73b0\u5728\u53f3\u503c\u7684\u4f4d\u7f6e\uff08\u5373\u53ef\u4ee5\u51fa\u73b0\u5728\u53f3\u4fa7\uff09 \u53f3\u503c\u4f1a\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u540e\u88ab\u9500\u6bc1\uff0c\u800c\u5de6\u503c\u5219\u4f1a\u7ee7\u7eed\u5b58\u5728 \u5728\u5f00\u59cb\u4ecb\u7ecd\u7b2c\u4e00\u4e2a\u590d\u5408\u7c7b\u578b \u5de6\u503c\u5f15\u7528 \u524d\uff0c\u8ba9\u6211\u4eec\u5148\u6765\u4e86\u89e3\u4e00\u4e0b\u4ec0\u4e48\u662f \u5de6\u503c \u3002 \u5728 1.10 - \u8868\u8fbe\u5f0f\u7b80\u4ecb \u4e2d\uff0c\u6211\u4eec\u5c06\u8868\u8fbe\u5f0f\u5b9a\u4e49\u4e3a\uff1a\u201c\u5b57\u9762\u91cf\u3001\u53d8\u91cf\u3001\u64cd\u4f5c\u7b26\u548c\u51fd\u6570\u8c03\u7528\u7684\u7ec4\u5408\u5e76\u53ef\u4ee5\u6267\u884c\u4ea7\u751f\u4e00\u4e2a\u5355\u4e00\u7684\u503c\u201d\u3002 \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 #include int main () { std :: cout << 2 + 3 ; // The expression 2 + 3 produces the value 5 return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u8868\u8fbe\u5f0f 2 + 3 \u6c42\u503c\u4f1a\u5f97\u5230 5\uff0c\u7136\u540e 5 \u88ab\u6253\u5370\u5230\u63a7\u5236\u53f0\u4e0a\u3002 \u5728 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 \u4e2d\uff0c\u6211\u4eec\u8fd8\u4ecb\u7ecd\u8fc7\uff0c\u8868\u8fbe\u5f0f\u662f\u53ef\u4ee5\u6709 \u526f\u4f5c\u7528 \u7684\uff0c\u5373\u5176\u7ed3\u679c\u7684\u751f\u547d\u5468\u671f\u4f1a\u8d85\u8fc7\u8868\u8fbe\u5f0f\u672c\u8eab\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { int x { 5 }; ++ x ; // This expression statement has the side-effect of incrementing x std :: cout << x ; // prints 6 return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u8868\u8fbe\u5f0f ++x \u4f1a\u5c06 x \u7684\u503c\u9012\u589e\uff0c\u5176\u503c\u7684\u6539\u53d8\u5373\u4f7f\u5728\u8868\u8fbe\u5f0f\u6c42\u503c\u5b8c\u6210\u540e\u4e5f\u662f\u5b58\u5728\u7684\u3002 \u9664\u4e86\u4ea7\u751f\u503c\u548c\u526f\u4f5c\u7528\u4e4b\u5916\uff0c\u8868\u8fbe\u5f0f\u8fd8\u53ef\u4ee5\u505a\u53e6\u5916\u4e00\u4ef6\u4e8b\uff0c\u5b83\u4eec\u53ef\u4ee5\u6c42\u503c\u5f97\u5230\u5bf9\u8c61\u548c\u51fd\u6570\u2014\u2014\u6211\u4eec\u7a0d\u540e\u4f1a\u4ecb\u7ecd\u76f8\u5173\u5185\u5bb9\u3002 \u8868\u8fbe\u5f0f\u7684\u5c5e\u6027 \u4e3a\u4e86\u786e\u5b9a\u8868\u8fbe\u5f0f\u5982\u4f55\u6c42\u503c\u4ee5\u53ca\u53ef\u4ee5\u5728\u54ea\u91cc\u88ab\u4f7f\u7528\uff0cC++\u4e2d\u7684\u6240\u6709\u8868\u8fbe\u5f0f\u90fd\u6709\u4e24\u4e2a\u5c5e\u6027\uff1a \u8868\u8fbe\u5f0f\u7c7b\u578b \u548c \u8868\u8fbe\u5f0f\u503c\u7c7b\u522b \u3002 \u8868\u8fbe\u5f0f\u7684\u7c7b\u578b \u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u7b49\u4e8e\u8868\u8fbe\u5f0f\u6c42\u503c\u5f97\u5230\u7684\u503c\u3001\u5bf9\u8c61\u6216\u51fd\u6570\u7684\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { auto v1 { 12 / 4 }; // int / int => int auto v2 { 12.0 / 4 }; // double / int => double return 0 ; } \u5bf9\u4e8e v1 \uff0c\u7f16\u8bd1\u5668\u4f1a\u5728 \u7f16\u8bd1\u65f6 \u786e\u5b9a\u4e24\u4e2a int \u7684\u6c42\u503c\u7ed3\u679c\u662f int \uff0c\u6240\u4ee5 int \u5c31\u662f\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u3002\u901a\u8fc7 \u7c7b\u578b\u63a8\u65ad \uff0c int \u6700\u7ec8\u4f1a\u4f5c\u4e3a v1 \u7684\u7c7b\u578b\u3002 \u5bf9\u4e8e v2 \uff0c\u7f16\u8bd1\u5668\u4f1a\u5728 \u7f16\u8bd1\u65f6 \u786e\u5b9a\uff0c\u4e00\u4e2a double \u64cd\u4f5c\u6570\u548c\u4e00\u4e2a int \u64cd\u4f5c\u6570\u7684\u6c42\u503c\u7ed3\u679c\u662f double \u7c7b\u578b\u7684\u3002\u8fd8\u8bb0\u5f97\u5417\uff0c\u7b97\u6570\u8fd0\u7b97\u7b26\u8981\u6c42\u4e24\u4e2a\u64cd\u4f5c\u6570\u5177\u6709\u5339\u914d\u7684\u7c7b\u578b\uff0c\u56e0\u6b64\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c int \u64cd\u4f5c\u6570\u4f1a\u88ab\u8f6c\u6362\u4e3a double \uff0c\u968f\u540e\u5c31\u4f1a\u6267\u884c\u6d6e\u70b9\u6570\u9664\u6cd5\u3002\u6240\u4ee5 double \u4f1a\u6210\u4e3a\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u3002 \u7f16\u8bd1\u5668\u4f1a\u4f7f\u7528\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u6765\u5224\u65ad\u5728\u7279\u5b9a\u4e0a\u4e0b\u6587\u4e2d\u662f\u4e0d\u662f\u5408\u6cd5\u7684\u8868\u8fbe\u5f0f \uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void print ( int x ) { std :: cout << x ; } int main () { print ( \"foo\" ); // error: print() was expecting an int argument, we tried to pass in a string literal return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c print(int) \u51fd\u6570\u671f\u671b\u4e00\u4e2a int \u7c7b\u578b\u7684 \u5f62\u53c2 \u3002\u4e0d\u8fc7\uff0c\u6b64\u5904\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\uff08\u5b57\u7b26\u4e32\"foo\"\uff09\u5e76\u4e0d\u5339\u914d\uff0c\u800c\u4e14\u6ca1\u6709\u53ef\u7528\u7684\u7c7b\u578b\u8f6c\u6362\u53ef\u7528\u3002\u6240\u4ee5\u4f1a\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002 \u6ce8\u610f\uff0c\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u786e\u5b9a\uff08\u5426\u5219\u7c7b\u578b\u68c0\u67e5\u548c\u7c7b\u578b\u63a8\u65ad\u5c06\u65e0\u6cd5\u8fdb\u884c\uff09\u2014\u2014\u4e0d\u8fc7\uff0c\u8868\u8fbe\u5f0f\u7684\u503c\u5219\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u786e\u5b9a\uff08\u5982\u679c \u8868\u8fbe\u5f0f\u662fconstexpr\uff09\u4e5f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u786e\u5b9a\uff08\u5bf9\u4e8e\u4e0d\u662fconstexpr\u7c7b\u578b\u7684\u8868\u8fbe\u5f0f\uff09\u3002 \u8868\u8fbe\u5f0f\u7684\u503c\u7c7b\u522b \u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 int main () { int x {}; x = 5 ; // valid: we can assign 5 to x 5 = x ; // error: can not assign value of x to literal value 5 return 0 ; } \u5176\u4e2d\u7684\u4e00\u4e2a\u8d4b\u503c\u8bed\u53e5\u662f\u5408\u6cd5\u7684\uff08\u5c06 5 \u8d4b\u503c\u7ed9 x\uff09\uff0c\u53e6\u5916\u4e00\u4e2a\u5219\u662f\u5408\u6cd5\u7684\uff08\u5c06x\u7684\u503c\u8d4b\u503c\u7ed9\u5b57\u9762\u91cf5\u662f\u4ec0\u4e48\u610f\u601d\uff1f\uff09\u3002\u53ef\u662f\uff0c\u7f16\u8bd1\u5668\u662f\u5982\u4f55\u5224\u65ad\u8d4b\u503c\u8bed\u53e5\u4e2d\u7684\u8868\u8fbe\u5f0f\u662f\u5426\u5408\u6cd5\u5462\uff1f \u8fd9\u4e2a\u95ee\u9898\u7684\u7b54\u6848\u5c31\u5728\u4e8e\u7b2c\u4e8c\u4e2a\u8868\u8fbe\u5f0f\u5c5e\u6027\uff1a\u8868\u8fbe\u5f0f \u503c\u7c7b\u578b \u3002\u503c\u7c7b\u578b\u8868\u660e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4f1a\u6c42\u503c\u5f97\u5230\u4e00\u4e2a\u503c\u3001\u4e00\u4e2a\u51fd\u6570\u8fd8\u662f\u4e00\u4e2a\u5bf9\u8c61\u3002 \u5728 C++11 \u4e4b\u524d\uff0cC++ \u4e2d\u53ea\u6709\u5577\u4e2a\u53ef\u80fd\u7684\u503c\u7c7b\u578b\uff1a \u5de6\u503c \u548c \u53f3\u503c \u3002 \u5728 C++11 \u4e2d\uff0c\u65b0\u589e\u4e86\u4e09\u4e2a\u503c\u7c7b\u578b ( glvalue \u3001 prvalue \u548c xvalue )\uff0c\u7528\u4e8e\u652f\u6301 \u79fb\u52a8\u8bed\u4e49 \u3002 \u4f5c\u8005\u6ce8 \u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u53ea\u5173\u6ce8 C++11 \u4e4b\u524d\u7684\u4e24\u79cd\u503c\u7c7b\u578b\uff08\u4e5f\u662f\u6211\u4eec\u76ee\u524d\u6240\u9700\u8981\u7684\uff09\u4f5c\u4e3a\u7b80\u4ecb\u3002\u6211\u4eec\u4f1a\u5728 M.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u79fb\u52a8\u548c\u667a\u80fd\u6307\u9488 \u4e00\u7ae0\u4e2d\u4ecb\u7ecd\u79fb\u52a8\u8bed\u4e49\u548c\u5176\u4ed6\u7684\u51e0\u79cd\u503c\u7c7b\u578b\u3002 Lvalue \u548c rvalue \u8868\u8fbe\u5f0f \u5de6\u503c lvalue (\u662f \u201cleft value\u201d \u6216 \u201clocator value\u201d\u7684\u7f29\u5199\uff0c\u6709\u65f6\u4e5f\u5199\u4f5c \u201cl-value\u201d) \u662f\u4e00\u79cd\u8868\u8fbe\u5f0f\uff0c\u5b83\u6700\u7ec8\u4f1a\u6c42\u503c\u5f97\u5230\u4e00\u4e2a\u5177\u6709 \u8eab\u4efd\u7279\u5f81 \u7684\u51fd\u6570\u6216\u5bf9\u8c61\u3002\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u5177\u6709 \u6807\u8bc6\u7b26(identifier) \uff08\u4f8b\u5982\u53d8\u91cf\u540d\u6216\u51fd\u6570\u540d\uff09\u6216\u4e00\u4e2a\u53ef\u88ab\u6807\u8bc6\u7684\u5185\u5b58\u5730\u5740\uff08\u53ef\u4ee5\u901a\u8fc7 & \u8fd0\u7b97\u7b26\u53d6\u5730\u5740\u2014\u2014\u4f1a\u5728 9.6 - \u6307\u9488\u7b80\u4ecb \u4e2d\u8fdb\u884c\u4ecb\u7ecd\uff09\u5219\u79f0\u5176\u5177\u6709\u8eab\u4efd\u7279\u5f81\u3002\u5177\u6709\u6807\u8bc6\u7684\u5bf9\u8c61\u751f\u547d\u5468\u671f\u4f1a\u8d85\u8fc7\u8868\u8fbe\u5f0f\u672c\u8eab\u3002 1 2 3 4 5 6 7 8 9 10 #include int main () { int x {}; std :: cout << x << '\\n' ; // x is an lvalue expression return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u8868\u8fbe\u5f0f x \u662f\u4e00\u4e2a\u5de6\u503c\u8868\u8fbe\u5f0f\uff0c\u56e0\u4e3a\u5b83\u6c42\u503c\u5f97\u5230\u53d8\u91cf x \uff08\u5b83\u5177\u6709\u6807\u8bc6\u7b26\uff09\u3002 \u56e0\u4e3a\u8bed\u8a00\u5f15\u5165\u4e86\u5e38\u91cf\uff0c\u5de6\u503c\u73b0\u5728\u6709\u4e24\u4e2a\u5b50\u7c7b\u578b\uff1a\u53ef\u53d8\u5de6\u503c\u2014\u2014\u5b83\u7684\u503c\u662f\u53ef\u4ee5\u6539\u53d8\u7684\u548c\u4e0d\u53ef\u53d8\u5de6\u503c\u2014\u2014\u5b83\u7684\u503c\u4e0d\u53ef\u4ee5\u88ab\u4fee\u6539\uff08\u56e0\u4e3a\u8be5\u5de6\u503c\u662f const \u6216 constexpr\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int x {}; const double d {}; std :: cout << x << '\\n' ; // x is a modifiable lvalue expression std :: cout << d << '\\n' ; // d is a non-modifiable lvalue expression return 0 ; } rvalue (\u662f\u201cright value\u201d\u7684\u7b80\u79f0\uff0c\u6709\u65f6\u5019\u4e5f\u5199\u4f5c r-value ) \u662f\u4e00\u79cd\u8868\u8fbe\u5f0f\uff0c\u9664\u4e86\u5de6\u503c\u4e4b\u5916\u7684\u90fd\u662f\u53f3\u503c\u3002\u5e38\u89c1\u7684\u53f3\u503c\u5305\u62ec \u5b57\u9762\u91cf \uff08\u9664\u4e86\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff0c\u5b83\u662f\u5de6\u503c\uff09\u548c\u51fd\u6570\u6216\u64cd\u4f5c\u7b26\u7684\u8fd4\u56de\u503c\u3002 \u53f3\u503c \u5176\u4f5c\u7528\u57df\u53ea\u5b58\u5728\u4e8e\u4f7f\u7528\u5b83\u7684\u8868\u8fbe\u5f0f\u4e2d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int return5 () { return 5 ; } int main () { int x { 5 }; // 5 is an rvalue expression const double d { 1.2 }; // 1.2 is an rvalue expression std :: cout << x << '\\n' ; // x is a modifiable lvalue expression std :: cout << d << '\\n' ; // d is a non-modifiable lvalue expression std :: cout << return5 (); // return5() is an rvalue expression (since the result is returned by value) std :: cout << x + 1 << '\\n' ; // x + 1 is a rvalue std :: cout << static_cast < int > ( d ) << '\\n' ; // the result of static casting d to an int is an rvalue return 0 ; } \u4f60\u53ef\u80fd\u4f1a\u597d\u5947\u4e3a\u4ec0\u4e48 return5() \u548c x + 1 \u5c5e\u4e8e\u53f3\u503c\uff1a\u95ee\u9898\u7684\u7b54\u6848\u5728\u4e8e\u8fd9\u4e9b\u8868\u8fbe\u5f0f\u7684\u503c\u5728\u4ea7\u751f\u540e\u5fc5\u987b\u9a6c\u4e0a\u4f7f\u7528\uff08\u5728\u8868\u8fbe\u5f0f\u7684\u4f5c\u7528\u57df\u5185\uff09\u6216\u88ab\u4e22\u5f03\u3002 \u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u56de\u7b54\u524d\u9762\u7684\u95ee\u9898\u4e86\uff0c\u4e3a\u4ec0\u4e48 x = 5 \u662f\u5408\u6cd5\u7684\u4f46 5 = x \u5219\u662f\u4e0d\u5408\u6cd5\uff1a\u8d4b\u503c\u8fd0\u7b97\u7b26\u8981\u6c42\u5176\u5de6\u64cd\u4f5c\u6570\u662f\u4e00\u4e2a\u53ef\u4fee\u6539\u7684\u5de6\u503c\u8868\u8fbe\u5f0f\uff0c\u4e14\u53f3\u64cd\u4f5c\u6570\u4e3a\u4e00\u4e2a\u53f3\u503c\u8868\u8fbe\u5f0f\u3002\u8d4b\u503c\u64cd\u4f5c 5 = x \u5931\u8d25\u662f\u56e0\u4e3a 5 \u5e76\u4e0d\u662f\u4e00\u4e2a\u5de6\u503c\u3002 1 2 3 4 5 6 7 8 9 10 int main () { int x {}; // Assignment requires the left operand to be a modifiable lvalue expression and the right operand to be an rvalue expression x = 5 ; // valid: x is a modifiable lvalue expression and 5 is an rvalue expression 5 = x ; // error: 5 is an rvalue expression and x is a modifiable lvalue expression return 0 ; } \u76f8\u5173\u5185\u5bb9 \u5de6\u503c\u548c\u53f3\u503c\u8868\u8fbe\u5f0f\u7684\u5b8c\u6574\u5217\u8868\u53c2\u8003 \u8fd9\u91cc \u3002\u5728C++11\u4e2d\uff0c\u53f3\u503c\u88ab\u5206\u6210\u4e86\u4e24\u4e2a\u5b50\u7c7b\u522b\uff1aprvalues \u548c xvalues\u3002\u6b64\u5904\u6211\u4eec\u8c08\u8bba\u7684 rvalues \u7efc\u5408\u4e86\u8fd9\u4e24\u79cd\u7c7b\u578b\u7684\u7279\u70b9\u3002 L-value \u548c r-value \u7684\u8f6c\u6362 \u5728\u4e0a\u9762\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u4e2d\u6211\u4eec\u63d0\u5230\uff0c\u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u53f3\u4fa7\u8fd0\u7b97\u6570\u5fc5\u987b\u662f\u4e00\u4e2a\u53f3\u503c\u8868\u8fbe\u5f0f\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u6b63\u786e\u8fd0\u884c\u5462\uff1f 1 2 3 4 5 6 7 8 9 int main () { int x { 1 }; int y { 2 }; x = y ; // y \u662f\u4e00\u4e2a\u53ef\u4ee5\u4fee\u6539\u7684\u5de6\u503c\u800c\u4e0d\u662f\u53f3\u503c\uff0c\u4e3a\u4ec0\u4e48\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u662f\u5408\u6cd5\u7684\u5462\uff1f return 0 ; } \u8fd9\u662f\u56e0\u4e3a\u5de6\u503c\u53ef\u4ee5\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u53f3\u503c\uff0c\u6240\u4ee5\u4efb\u4f55\u53ef\u4ee5\u4f7f\u7528\u53f3\u503c\u7684\u5730\u65b9\u90fd\u53ef\u4ee5\u4f7f\u7528\u5de6\u503c\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 int main () { int x { 2 }; x = x + 1 ; return 0 ; } \u5728\u8fd9\u4e2a\u8bed\u53e5\u4e2d\uff0c\u53d8\u91cfx\u7684\u4f7f\u7528\u5b58\u5728\u4e24\u4e2a\u4e0d\u540c\u7684\u8bed\u5883\u3002\u8d4b\u503c\u8fd0\u7b97\u7b26\u5de6\u4fa7\u7684x\u662f\u4e00\u4e2a\u5de6\u503c\u8868\u8fbe\u5f0f\uff0c\u5b83\u6c42\u503c\u5f97\u5230\u53d8\u91cf x\u3002\u8d4b\u503c\u8fd0\u7b97\u7b26\u53f3\u4fa7\u7684 x+1 \u5219\u662f\u4e00\u4e2a\u53f3\u503c\u8868\u8fbe\u5f0f\uff0c\u5b83\u6c42\u503c\u5f97\u52303\u3002 \u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u8be6\u7ec6\u5730\u4ecb\u7ecd\u4e86\u5de6\u503c\uff0c\u63a5\u4e0b\u6765\u5c31\u662f\u6211\u4eec\u8981\u63a5\u89e6\u5230\u7684\u7b2c\u4e00\u4e2a\u590d\u6742\u8868\u8fbe\u5f0f\u7c7b\u578b\uff1a \u5de6\u503c\u5f15\u7528 \u3002 \u5173\u952e\u4fe1\u606f \u8fa8\u522b\u5de6\u503c\u8868\u8fbe\u5f0f\u548c\u53f3\u503c\u8868\u8fbe\u5f0f\u7684\u5173\u952e\u6cd5\u5219\uff1a - \u5de6\u503c\u8868\u8fbe\u5f0f\u662f\u90a3\u4e9b\u6c42\u503c\u4e3a\u53d8\u91cf\u6216\u5176\u4ed6\u5177\u6709\u8eab\u4efd\u6807\u8bc6\u7684\u53d8\u91cf\uff0c\u4e14\u5b83\u4eec\u53ef\u4ee5\u6301\u7eed\u5230\u8868\u8fbe\u5f0f\u7ed3\u675f\u540e\u3002 - \u53f3\u503c\u8868\u8fbe\u5f0f\u5219\u662f\u90a3\u4e9b\u53ef\u4ee5\u6c42\u503c\u4e3a\u5b57\u9762\u91cf\u7684\u8868\u8fbe\u5f0f\uff0c\u6216\u8005\u662f\u51fd\u6570\u548c\u64cd\u4f5c\u7b26\u7684\u8fd4\u56de\u503c\uff0c\u5b83\u4eec\u4f1a\u5728\u8868\u8fbe\u5f0f\u89e3\u91ca\u540e\u88ab\u4e22\u5f03\u3002","title":"9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09"},{"location":"9-2-Value-categories-lvalues-and-rvalues/#_1","text":"\u4e3a\u4e86\u786e\u5b9a\u8868\u8fbe\u5f0f\u5982\u4f55\u6c42\u503c\u4ee5\u53ca\u53ef\u4ee5\u5728\u54ea\u91cc\u88ab\u4f7f\u7528\uff0cC++\u4e2d\u7684\u6240\u6709\u8868\u8fbe\u5f0f\u90fd\u6709\u4e24\u4e2a\u5c5e\u6027\uff1a \u8868\u8fbe\u5f0f\u7c7b\u578b \u548c \u8868\u8fbe\u5f0f\u503c\u7c7b\u522b \u3002","title":"\u8868\u8fbe\u5f0f\u7684\u5c5e\u6027"},{"location":"9-2-Value-categories-lvalues-and-rvalues/#_2","text":"\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u7b49\u4e8e\u8868\u8fbe\u5f0f\u6c42\u503c\u5f97\u5230\u7684\u503c\u3001\u5bf9\u8c61\u6216\u51fd\u6570\u7684\u7c7b\u578b\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { auto v1 { 12 / 4 }; // int / int => int auto v2 { 12.0 / 4 }; // double / int => double return 0 ; } \u5bf9\u4e8e v1 \uff0c\u7f16\u8bd1\u5668\u4f1a\u5728 \u7f16\u8bd1\u65f6 \u786e\u5b9a\u4e24\u4e2a int \u7684\u6c42\u503c\u7ed3\u679c\u662f int \uff0c\u6240\u4ee5 int \u5c31\u662f\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u3002\u901a\u8fc7 \u7c7b\u578b\u63a8\u65ad \uff0c int \u6700\u7ec8\u4f1a\u4f5c\u4e3a v1 \u7684\u7c7b\u578b\u3002 \u5bf9\u4e8e v2 \uff0c\u7f16\u8bd1\u5668\u4f1a\u5728 \u7f16\u8bd1\u65f6 \u786e\u5b9a\uff0c\u4e00\u4e2a double \u64cd\u4f5c\u6570\u548c\u4e00\u4e2a int \u64cd\u4f5c\u6570\u7684\u6c42\u503c\u7ed3\u679c\u662f double \u7c7b\u578b\u7684\u3002\u8fd8\u8bb0\u5f97\u5417\uff0c\u7b97\u6570\u8fd0\u7b97\u7b26\u8981\u6c42\u4e24\u4e2a\u64cd\u4f5c\u6570\u5177\u6709\u5339\u914d\u7684\u7c7b\u578b\uff0c\u56e0\u6b64\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c int \u64cd\u4f5c\u6570\u4f1a\u88ab\u8f6c\u6362\u4e3a double \uff0c\u968f\u540e\u5c31\u4f1a\u6267\u884c\u6d6e\u70b9\u6570\u9664\u6cd5\u3002\u6240\u4ee5 double \u4f1a\u6210\u4e3a\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u3002 \u7f16\u8bd1\u5668\u4f1a\u4f7f\u7528\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u6765\u5224\u65ad\u5728\u7279\u5b9a\u4e0a\u4e0b\u6587\u4e2d\u662f\u4e0d\u662f\u5408\u6cd5\u7684\u8868\u8fbe\u5f0f \uff0c\u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 #include void print ( int x ) { std :: cout << x ; } int main () { print ( \"foo\" ); // error: print() was expecting an int argument, we tried to pass in a string literal return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c print(int) \u51fd\u6570\u671f\u671b\u4e00\u4e2a int \u7c7b\u578b\u7684 \u5f62\u53c2 \u3002\u4e0d\u8fc7\uff0c\u6b64\u5904\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\uff08\u5b57\u7b26\u4e32\"foo\"\uff09\u5e76\u4e0d\u5339\u914d\uff0c\u800c\u4e14\u6ca1\u6709\u53ef\u7528\u7684\u7c7b\u578b\u8f6c\u6362\u53ef\u7528\u3002\u6240\u4ee5\u4f1a\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002 \u6ce8\u610f\uff0c\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u786e\u5b9a\uff08\u5426\u5219\u7c7b\u578b\u68c0\u67e5\u548c\u7c7b\u578b\u63a8\u65ad\u5c06\u65e0\u6cd5\u8fdb\u884c\uff09\u2014\u2014\u4e0d\u8fc7\uff0c\u8868\u8fbe\u5f0f\u7684\u503c\u5219\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u786e\u5b9a\uff08\u5982\u679c \u8868\u8fbe\u5f0f\u662fconstexpr\uff09\u4e5f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u786e\u5b9a\uff08\u5bf9\u4e8e\u4e0d\u662fconstexpr\u7c7b\u578b\u7684\u8868\u8fbe\u5f0f\uff09\u3002","title":"\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b"},{"location":"9-2-Value-categories-lvalues-and-rvalues/#_3","text":"\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 int main () { int x {}; x = 5 ; // valid: we can assign 5 to x 5 = x ; // error: can not assign value of x to literal value 5 return 0 ; } \u5176\u4e2d\u7684\u4e00\u4e2a\u8d4b\u503c\u8bed\u53e5\u662f\u5408\u6cd5\u7684\uff08\u5c06 5 \u8d4b\u503c\u7ed9 x\uff09\uff0c\u53e6\u5916\u4e00\u4e2a\u5219\u662f\u5408\u6cd5\u7684\uff08\u5c06x\u7684\u503c\u8d4b\u503c\u7ed9\u5b57\u9762\u91cf5\u662f\u4ec0\u4e48\u610f\u601d\uff1f\uff09\u3002\u53ef\u662f\uff0c\u7f16\u8bd1\u5668\u662f\u5982\u4f55\u5224\u65ad\u8d4b\u503c\u8bed\u53e5\u4e2d\u7684\u8868\u8fbe\u5f0f\u662f\u5426\u5408\u6cd5\u5462\uff1f \u8fd9\u4e2a\u95ee\u9898\u7684\u7b54\u6848\u5c31\u5728\u4e8e\u7b2c\u4e8c\u4e2a\u8868\u8fbe\u5f0f\u5c5e\u6027\uff1a\u8868\u8fbe\u5f0f \u503c\u7c7b\u578b \u3002\u503c\u7c7b\u578b\u8868\u660e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4f1a\u6c42\u503c\u5f97\u5230\u4e00\u4e2a\u503c\u3001\u4e00\u4e2a\u51fd\u6570\u8fd8\u662f\u4e00\u4e2a\u5bf9\u8c61\u3002 \u5728 C++11 \u4e4b\u524d\uff0cC++ \u4e2d\u53ea\u6709\u5577\u4e2a\u53ef\u80fd\u7684\u503c\u7c7b\u578b\uff1a \u5de6\u503c \u548c \u53f3\u503c \u3002 \u5728 C++11 \u4e2d\uff0c\u65b0\u589e\u4e86\u4e09\u4e2a\u503c\u7c7b\u578b ( glvalue \u3001 prvalue \u548c xvalue )\uff0c\u7528\u4e8e\u652f\u6301 \u79fb\u52a8\u8bed\u4e49 \u3002 \u4f5c\u8005\u6ce8 \u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u53ea\u5173\u6ce8 C++11 \u4e4b\u524d\u7684\u4e24\u79cd\u503c\u7c7b\u578b\uff08\u4e5f\u662f\u6211\u4eec\u76ee\u524d\u6240\u9700\u8981\u7684\uff09\u4f5c\u4e3a\u7b80\u4ecb\u3002\u6211\u4eec\u4f1a\u5728 M.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u79fb\u52a8\u548c\u667a\u80fd\u6307\u9488 \u4e00\u7ae0\u4e2d\u4ecb\u7ecd\u79fb\u52a8\u8bed\u4e49\u548c\u5176\u4ed6\u7684\u51e0\u79cd\u503c\u7c7b\u578b\u3002","title":"\u8868\u8fbe\u5f0f\u7684\u503c\u7c7b\u522b"},{"location":"9-2-Value-categories-lvalues-and-rvalues/#lvalue-rvalue","text":"\u5de6\u503c lvalue (\u662f \u201cleft value\u201d \u6216 \u201clocator value\u201d\u7684\u7f29\u5199\uff0c\u6709\u65f6\u4e5f\u5199\u4f5c \u201cl-value\u201d) \u662f\u4e00\u79cd\u8868\u8fbe\u5f0f\uff0c\u5b83\u6700\u7ec8\u4f1a\u6c42\u503c\u5f97\u5230\u4e00\u4e2a\u5177\u6709 \u8eab\u4efd\u7279\u5f81 \u7684\u51fd\u6570\u6216\u5bf9\u8c61\u3002\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u5177\u6709 \u6807\u8bc6\u7b26(identifier) \uff08\u4f8b\u5982\u53d8\u91cf\u540d\u6216\u51fd\u6570\u540d\uff09\u6216\u4e00\u4e2a\u53ef\u88ab\u6807\u8bc6\u7684\u5185\u5b58\u5730\u5740\uff08\u53ef\u4ee5\u901a\u8fc7 & \u8fd0\u7b97\u7b26\u53d6\u5730\u5740\u2014\u2014\u4f1a\u5728 9.6 - \u6307\u9488\u7b80\u4ecb \u4e2d\u8fdb\u884c\u4ecb\u7ecd\uff09\u5219\u79f0\u5176\u5177\u6709\u8eab\u4efd\u7279\u5f81\u3002\u5177\u6709\u6807\u8bc6\u7684\u5bf9\u8c61\u751f\u547d\u5468\u671f\u4f1a\u8d85\u8fc7\u8868\u8fbe\u5f0f\u672c\u8eab\u3002 1 2 3 4 5 6 7 8 9 10 #include int main () { int x {}; std :: cout << x << '\\n' ; // x is an lvalue expression return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u8868\u8fbe\u5f0f x \u662f\u4e00\u4e2a\u5de6\u503c\u8868\u8fbe\u5f0f\uff0c\u56e0\u4e3a\u5b83\u6c42\u503c\u5f97\u5230\u53d8\u91cf x \uff08\u5b83\u5177\u6709\u6807\u8bc6\u7b26\uff09\u3002 \u56e0\u4e3a\u8bed\u8a00\u5f15\u5165\u4e86\u5e38\u91cf\uff0c\u5de6\u503c\u73b0\u5728\u6709\u4e24\u4e2a\u5b50\u7c7b\u578b\uff1a\u53ef\u53d8\u5de6\u503c\u2014\u2014\u5b83\u7684\u503c\u662f\u53ef\u4ee5\u6539\u53d8\u7684\u548c\u4e0d\u53ef\u53d8\u5de6\u503c\u2014\u2014\u5b83\u7684\u503c\u4e0d\u53ef\u4ee5\u88ab\u4fee\u6539\uff08\u56e0\u4e3a\u8be5\u5de6\u503c\u662f const \u6216 constexpr\uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int x {}; const double d {}; std :: cout << x << '\\n' ; // x is a modifiable lvalue expression std :: cout << d << '\\n' ; // d is a non-modifiable lvalue expression return 0 ; } rvalue (\u662f\u201cright value\u201d\u7684\u7b80\u79f0\uff0c\u6709\u65f6\u5019\u4e5f\u5199\u4f5c r-value ) \u662f\u4e00\u79cd\u8868\u8fbe\u5f0f\uff0c\u9664\u4e86\u5de6\u503c\u4e4b\u5916\u7684\u90fd\u662f\u53f3\u503c\u3002\u5e38\u89c1\u7684\u53f3\u503c\u5305\u62ec \u5b57\u9762\u91cf \uff08\u9664\u4e86\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff0c\u5b83\u662f\u5de6\u503c\uff09\u548c\u51fd\u6570\u6216\u64cd\u4f5c\u7b26\u7684\u8fd4\u56de\u503c\u3002 \u53f3\u503c \u5176\u4f5c\u7528\u57df\u53ea\u5b58\u5728\u4e8e\u4f7f\u7528\u5b83\u7684\u8868\u8fbe\u5f0f\u4e2d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int return5 () { return 5 ; } int main () { int x { 5 }; // 5 is an rvalue expression const double d { 1.2 }; // 1.2 is an rvalue expression std :: cout << x << '\\n' ; // x is a modifiable lvalue expression std :: cout << d << '\\n' ; // d is a non-modifiable lvalue expression std :: cout << return5 (); // return5() is an rvalue expression (since the result is returned by value) std :: cout << x + 1 << '\\n' ; // x + 1 is a rvalue std :: cout << static_cast < int > ( d ) << '\\n' ; // the result of static casting d to an int is an rvalue return 0 ; } \u4f60\u53ef\u80fd\u4f1a\u597d\u5947\u4e3a\u4ec0\u4e48 return5() \u548c x + 1 \u5c5e\u4e8e\u53f3\u503c\uff1a\u95ee\u9898\u7684\u7b54\u6848\u5728\u4e8e\u8fd9\u4e9b\u8868\u8fbe\u5f0f\u7684\u503c\u5728\u4ea7\u751f\u540e\u5fc5\u987b\u9a6c\u4e0a\u4f7f\u7528\uff08\u5728\u8868\u8fbe\u5f0f\u7684\u4f5c\u7528\u57df\u5185\uff09\u6216\u88ab\u4e22\u5f03\u3002 \u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u56de\u7b54\u524d\u9762\u7684\u95ee\u9898\u4e86\uff0c\u4e3a\u4ec0\u4e48 x = 5 \u662f\u5408\u6cd5\u7684\u4f46 5 = x \u5219\u662f\u4e0d\u5408\u6cd5\uff1a\u8d4b\u503c\u8fd0\u7b97\u7b26\u8981\u6c42\u5176\u5de6\u64cd\u4f5c\u6570\u662f\u4e00\u4e2a\u53ef\u4fee\u6539\u7684\u5de6\u503c\u8868\u8fbe\u5f0f\uff0c\u4e14\u53f3\u64cd\u4f5c\u6570\u4e3a\u4e00\u4e2a\u53f3\u503c\u8868\u8fbe\u5f0f\u3002\u8d4b\u503c\u64cd\u4f5c 5 = x \u5931\u8d25\u662f\u56e0\u4e3a 5 \u5e76\u4e0d\u662f\u4e00\u4e2a\u5de6\u503c\u3002 1 2 3 4 5 6 7 8 9 10 int main () { int x {}; // Assignment requires the left operand to be a modifiable lvalue expression and the right operand to be an rvalue expression x = 5 ; // valid: x is a modifiable lvalue expression and 5 is an rvalue expression 5 = x ; // error: 5 is an rvalue expression and x is a modifiable lvalue expression return 0 ; } \u76f8\u5173\u5185\u5bb9 \u5de6\u503c\u548c\u53f3\u503c\u8868\u8fbe\u5f0f\u7684\u5b8c\u6574\u5217\u8868\u53c2\u8003 \u8fd9\u91cc \u3002\u5728C++11\u4e2d\uff0c\u53f3\u503c\u88ab\u5206\u6210\u4e86\u4e24\u4e2a\u5b50\u7c7b\u522b\uff1aprvalues \u548c xvalues\u3002\u6b64\u5904\u6211\u4eec\u8c08\u8bba\u7684 rvalues \u7efc\u5408\u4e86\u8fd9\u4e24\u79cd\u7c7b\u578b\u7684\u7279\u70b9\u3002","title":"Lvalue \u548c rvalue \u8868\u8fbe\u5f0f"},{"location":"9-2-Value-categories-lvalues-and-rvalues/#l-value-r-value","text":"\u5728\u4e0a\u9762\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u4e2d\u6211\u4eec\u63d0\u5230\uff0c\u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u53f3\u4fa7\u8fd0\u7b97\u6570\u5fc5\u987b\u662f\u4e00\u4e2a\u53f3\u503c\u8868\u8fbe\u5f0f\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u6b63\u786e\u8fd0\u884c\u5462\uff1f 1 2 3 4 5 6 7 8 9 int main () { int x { 1 }; int y { 2 }; x = y ; // y \u662f\u4e00\u4e2a\u53ef\u4ee5\u4fee\u6539\u7684\u5de6\u503c\u800c\u4e0d\u662f\u53f3\u503c\uff0c\u4e3a\u4ec0\u4e48\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u662f\u5408\u6cd5\u7684\u5462\uff1f return 0 ; } \u8fd9\u662f\u56e0\u4e3a\u5de6\u503c\u53ef\u4ee5\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u53f3\u503c\uff0c\u6240\u4ee5\u4efb\u4f55\u53ef\u4ee5\u4f7f\u7528\u53f3\u503c\u7684\u5730\u65b9\u90fd\u53ef\u4ee5\u4f7f\u7528\u5de6\u503c\u3002 \u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 int main () { int x { 2 }; x = x + 1 ; return 0 ; } \u5728\u8fd9\u4e2a\u8bed\u53e5\u4e2d\uff0c\u53d8\u91cfx\u7684\u4f7f\u7528\u5b58\u5728\u4e24\u4e2a\u4e0d\u540c\u7684\u8bed\u5883\u3002\u8d4b\u503c\u8fd0\u7b97\u7b26\u5de6\u4fa7\u7684x\u662f\u4e00\u4e2a\u5de6\u503c\u8868\u8fbe\u5f0f\uff0c\u5b83\u6c42\u503c\u5f97\u5230\u53d8\u91cf x\u3002\u8d4b\u503c\u8fd0\u7b97\u7b26\u53f3\u4fa7\u7684 x+1 \u5219\u662f\u4e00\u4e2a\u53f3\u503c\u8868\u8fbe\u5f0f\uff0c\u5b83\u6c42\u503c\u5f97\u52303\u3002 \u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u8be6\u7ec6\u5730\u4ecb\u7ecd\u4e86\u5de6\u503c\uff0c\u63a5\u4e0b\u6765\u5c31\u662f\u6211\u4eec\u8981\u63a5\u89e6\u5230\u7684\u7b2c\u4e00\u4e2a\u590d\u6742\u8868\u8fbe\u5f0f\u7c7b\u578b\uff1a \u5de6\u503c\u5f15\u7528 \u3002 \u5173\u952e\u4fe1\u606f \u8fa8\u522b\u5de6\u503c\u8868\u8fbe\u5f0f\u548c\u53f3\u503c\u8868\u8fbe\u5f0f\u7684\u5173\u952e\u6cd5\u5219\uff1a - \u5de6\u503c\u8868\u8fbe\u5f0f\u662f\u90a3\u4e9b\u6c42\u503c\u4e3a\u53d8\u91cf\u6216\u5176\u4ed6\u5177\u6709\u8eab\u4efd\u6807\u8bc6\u7684\u53d8\u91cf\uff0c\u4e14\u5b83\u4eec\u53ef\u4ee5\u6301\u7eed\u5230\u8868\u8fbe\u5f0f\u7ed3\u675f\u540e\u3002 - \u53f3\u503c\u8868\u8fbe\u5f0f\u5219\u662f\u90a3\u4e9b\u53ef\u4ee5\u6c42\u503c\u4e3a\u5b57\u9762\u91cf\u7684\u8868\u8fbe\u5f0f\uff0c\u6216\u8005\u662f\u51fd\u6570\u548c\u64cd\u4f5c\u7b26\u7684\u8fd4\u56de\u503c\uff0c\u5b83\u4eec\u4f1a\u5728\u8868\u8fbe\u5f0f\u89e3\u91ca\u540e\u88ab\u4e22\u5f03\u3002","title":"L-value \u548c r-value \u7684\u8f6c\u6362"},{"location":"9-3-Lvalue-references/","tags":["Lvalue"],"text":"Key Takeaway \u5f15\u7528\u662f\u5bf9\u8c61\u7684\u522b\u540d\uff0c\u5728\u521b\u5efa\u65f6\u8fdb\u884c\u7ed1\u5b9a\uff0c\u7136\u540e\u5c31\u4e0d\u53ef\u4ee5\u88ab\u4fee\u6539 \u5f15\u7528\u6709\u5de6\u503c\u5f15\u7528\u548c\u53f3\u503c\u5f15\u7528\u4e24\u79cd\u3002\u5de6\u503c\u5f15\u7528\u53c8\u5206\u4e3a\u53ef\u53d8\u5de6\u503c\u5f15\u7528\u548c\u4e0d\u53ef\u53d8\u5de6\u503c\u5f15\u7528 \u5f15\u7528\u548c\u88ab\u5f15\u7528\u5bf9\u8c61\u5177\u6709\u72ec\u7acb\u7684\u751f\u547d\u5468\u671f \u5f15\u7528\u4e0d\u662f\u5bf9\u8c61\uff0c\u6240\u4ee5\u4e0d\u5360\u7528\u5b58\u50a8\u7a7a\u95f4\uff0c\u5f53\u7136\u4e5f\u4e0d\u80fd\u88ab\u7528\u5728\u6240\u6709\u5141\u8bb8\u4f7f\u7528\u5bf9\u8c61\u7684\u5730\u65b9\u3002 std::reference_wrapper \u53ef\u63d0\u4f9b\u5141\u8bb8\u518d\u6b21\u7ed1\u5b9a\u7684\u5f15\u7528 \u5728 C++ \u4e2d\uff0c\u5f15\u7528\u5176\u5b9e\u5c31\u662f\u67d0\u4e2a\u5df2\u5b58\u5728\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u4e00\u65e6\u5f15\u7528\u88ab\u5b9a\u4e49\uff0c\u4efb\u4f55\u5bf9\u5f15\u7528\u7684\u64cd\u4f5c\u90fd\u4f1a\u76f4\u63a5\u5e94\u7528\u4e8e\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u3002 \u5173\u952e\u4fe1\u606f \u5f15\u7528\u5728\u672c\u8d28\u4e0a\u548c\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u662f\u4e00\u56de\u4e8b\u3002 \u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u6765\u8bfb\u53d6\u6216\u4fee\u6539\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u3002\u5c3d\u7ba1\u73b0\u5728\u5f15\u7528\u770b\u4e0a\u53bb\u6709\u70b9\u50bb\uff0c\u6ca1\u5565\u7528\uff0c\u751a\u81f3\u8fd8\u5f88\u591a\u4f59\uff0c\u4f46\u5b9e\u9645\u4e0a\u5728C++\u4e2d\u6211\u4eec\u4f1a\u5927\u91cf\u5730\u4f7f\u7528\u5f15\u7528\u3002 \u4f60\u4e5f\u53ef\u4ee5\u521b\u5efa\u5bf9\u51fd\u6570\u7684\u5f15\u7528\uff0c\u5c3d\u7ba1\u8fd9\u4e2a\u505a\u6cd5\u5e76\u4e0d\u5e38\u89c1\u3002 \u73b0\u4ee3C++\u5b58\u5728\u4e24\u79cd\u7c7b\u578b\u7684\u5f15\u7528\uff1a \u5de6\u503c\u5f15\u7528 \u548c \u53f3\u503c\u5f15\u7528 \u3002\u5728\u672c\u7ae0\u4e2d\u6211\u4eec\u4f1a\u9996\u5148\u8ba8\u8bba\u5de6\u503c\u5f15\u7528\u3002 \u76f8\u5173\u5185\u5bb9 \u56e0\u4e3a\u6211\u4eec\u5728\u672c\u7ae0\u4f1a\u8ba8\u8bba\u5de6\u503c\u548c\u53f3\u503c\uff0c\u5982\u679c\u4f60\u9700\u8981\u590d\u4e60\u4e00\u4e0b\u7684\u8bdd\uff0c\u8bf7\u53c2\u8003 9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09 \u3002 \u53f3\u503c\u5f15\u7528\u4f1a\u5728 \u79fb\u52a8\u8bed\u4e49 \u7ae0\u8282\u8fdb\u884c\u4ecb\u7ecd ( chapter M ). \u5de6\u503c\u5f15\u7528\u7c7b\u578b \u5de6\u503c\u5f15\u7528\uff08\u901a\u5e38\u7b80\u79f0\u4e3a\u5f15\u7528\u56e0\u4e3aC++11\u4e4b\u524d\u53ea\u6709\u8fd9\u4e00\u79cd\u7c7b\u578b\u7684\u5f15\u7528\uff09\u662f\u4e00\u4e2a\u5df2\u5b58\u5728\u5de6\u503c\uff08\u4f8b\u5982\u67d0\u4e2a\u53d8\u91cf\uff09\u7684\u522b\u540d\u3002 \u58f0\u660e\u5de6\u503c\u7c7b\u578b\u53ef\u4ee5\u5728\u7c7b\u578b\u58f0\u660e\u65f6\u4f7f\u7528 & \u7b26\u53f7\uff1a 1 2 3 int // a normal int type int & // an lvalue reference to an int object double & // an lvalue reference to a double object \u5de6\u503c\u5f15\u7528\u53d8\u91cf \u5de6\u503c\u5f15\u7528\u7c7b\u578b\u53ef\u4ee5\u7528\u6765\u58f0\u660e\u4e00\u4e2a\u5de6\u503c\u5f15\u7528\u53d8\u91cf\u3002\u5de6\u503c\u5f15\u7528\u53d8\u91cf\u4f5c\u4e3a\u5bf9\u4e00\u4e2a\u5de6\u503c\u7684\u5f15\u7528\u6765\u4f7f\u7528\uff08\u901a\u5e38\u662f\u53e6\u5916\u4e00\u4e2a\u53d8\u91cf\uff09\u3002 \u521b\u5efa\u5de6\u503c\u5f15\u7528\u53d8\u91cf\uff0c\u53ea\u9700\u8981\u4f7f\u7528\u5de6\u503c\u5f15\u7528\u7c7b\u578b\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u5373\u53ef\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { int x { 5 }; // x is a normal integer variable int & ref { x }; // ref is an lvalue reference variable that can now be used as an alias for variable x std :: cout << x << '\\n' ; // print the value of x (5) std :: cout << ref << '\\n' ; // print the value of x via ref (5) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528 int& \u5b9a\u4e49 ref \u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5bf9 int \u7c7b\u578b\u5de6\u503c\u7684\u5f15\u7528\uff0c\u7136\u540e\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u5de6\u503c\u8868\u8fbe\u5f0f x \u5bf9\u5176\u8fdb\u884c\u4e86\u521d\u59cb\u5316\u3002\u4ece\u6b64\u4ee5\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06 ref \u4f5c\u4e3a x \u7684\u540c\u4e49\u8bcd\u6765\u4f7f\u7528\u3002\u7a0b\u5e8f\u4f1a\u6253\u5370\u5982\u4e0b\u5185\u5bb9\uff1a 1 2 5 5 \u4ece\u7f16\u8bd1\u5668\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u5c06 & \u653e\u5728\u7c7b\u578b\u65c1\u8fb9( int& ref )\u8fd8\u662f\u53d8\u91cf\u65c1\u8fb9( int &ref )\u5e76\u6ca1\u6709\u4ec0\u4e48\u533a\u522b\uff0c\u9009\u62e9\u90a3\u79cd\u5199\u6cd5\u53ea\u5173\u4e4e\u4e8e\u98ce\u683c\u3002\u73b0\u4ee3C++\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u9009\u62e9\u5c06 & \u7d27\u6328\u7740\u7c7b\u578b\u5173\u952e\u5b57\u653e\u7f6e\uff0c\u56e0\u4e3a\u8fd9\u6837\u505a\u53ef\u4ee5\u66f4\u6e05\u695a\u5730\u8868\u660e\u5f15\u7528\u662f\u7c7b\u578b\u4fe1\u606f\u7684\u4e00\u90e8\u5206\uff0c\u800c\u4e0d\u662f\u6807\u8bc6\u7b26\u7684\u4e00\u90e8\u5206\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u5b9a\u4e49\u5f15\u7528\u7684\u65f6\u5019\uff0c\u5c06 & \u7d27\u6328\u7740\u7c7b\u578b\u5173\u952e\u8bcd\u653e\u7f6e\uff08\u800c\u4e0d\u662f\u53d8\u91cf\u540d\uff09\u3002 \u6269\u5c55\u9605\u8bfb \u5bf9\u4e8e\u90a3\u4e9b\u5df2\u7ecf\u719f\u6089\u6307\u9488\u7684\u5f00\u53d1\u8005\u6765\u8bf4\uff0c & \u5728\u8fd9\u91cc\u542b\u4e49\u5e76\u4e0d\u662f\u53d6\u5730\u5740\u800c\u662f\u5de6\u503c\u5f15\u7528\u3002 \u901a\u8fc7\u5de6\u503c\u5f15\u7528\u6765\u4fee\u6539\u503c \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u901a\u8fc7\u5f15\u7528\u6765\u83b7\u53d6\u88ab\u5f15\u7528\u5bf9\u8c61\u7684\u503c\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u6765\u4fee\u6539\u88ab\u5f15\u7528\u5bf9\u8c61\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { int x { 5 }; // normal integer variable int & ref { x }; // ref is now an alias for variable x std :: cout << x << ref ; // print 55 x = 6 ; // x now has value 6 std :: cout << x << ref ; // prints 66 ref = 7 ; // the object being referenced (x) now has value 7 std :: cout << x << ref ; // prints 77 return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 556677 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c ref \u662f x \u7684\u522b\u540d\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 x \u6216\u8005 ref \u6765\u4fee\u6539 x \u7684\u503c\u3002 \u5de6\u503c\u5f15\u7528\u7684\u521d\u59cb\u5316 \u548c\u5e38\u91cf\u975e\u5e38\u7c7b\u4f3c\uff0c\u6240\u6709\u7684\u5f15\u7528\u90fd\u5fc5\u987b\u88ab\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 int main () { int & invalidRef ; // error: references must be initialized int x { 5 }; int & ref { x }; // okay: reference to int is bound to int variable return 0 ; } \u5f53\u4f7f\u7528\u4e00\u4e2a\u5bf9\u8c61(\u6216\u51fd\u6570)\u521d\u59cb\u5316\u5f15\u7528\u65f6\uff0c\u6211\u4eec\u8bf4\u5b83\u88ab\u7ed1\u5b9a\u5230\u4e86\u90a3\u4e2a\u5bf9\u8c61(\u6216\u51fd\u6570)\u3002\u7ed1\u5b9a\u6b64\u7c7b\u5f15\u7528\u7684\u8fc7\u7a0b\u79f0\u4e3a\u5f15\u7528\u7ed1\u5b9a\u3002\u88ab\u5f15\u7528\u7684\u5bf9\u8c61(\u6216\u51fd\u6570)\u6709\u65f6\u79f0\u4e3a referent\u3002 \u4f5c\u6307\u5f15\u4e0e\u5fc5\u987b\u88ab\u7ed1\u5b9a\u5230\u4e00\u4e2a\u53ef\u4fee\u6539\u5de6\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 int main () { int x { 5 }; int & ref { x }; // valid: lvalue reference bound to a modifiable lvalue const int y { 5 }; int & invalidRef { y }; // invalid: can't bind to a non-modifiable lvalue int & invalidRef2 { 0 }; // invalid: can't bind to an r-value return 0 ; } \u5de6\u503c\u5f15\u7528\u4e0d\u80fd\u88ab\u7ed1\u5b9a\u5230\u4e00\u4e2a\u4e0d\u53ef\u4fee\u6539\u7684\u5de6\u503c\uff08\u5426\u5219\u4f60\u5c82\u4e0d\u662f\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u6765\u4fee\u6539\u8fd9\u4e2a\u503c\u54af\uff0c\u8fd9\u663e\u7136\u8fdd\u80cc\u4e86\u5e38\u91cf\u7684\u5e38\u91cf\u6027\uff09\u3002\u56e0\u6b64\uff0c\u5de6\u503c\u5f15\u7528\u6709\u65f6\u4e5f\u79f0\u4e3a\u975econst\u53d8\u91cf\u7684\u5de6\u503c\u5f15\u7528\uff08\u7b80\u79f0\u4e3a\u975econst\u53d8\u91cf\u7684\u5f15\u7528\uff09\u3002 \u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5de6\u503c\u5f15\u7528\u7684\u7c7b\u578b\u5fc5\u987b\u548c\u88ab\u5f15\u7528\u5bf9\u8c61\u7684\u7c7b\u578b\u76f8\u5339\u914d\uff08\u4f8b\u5916\u7684\u60c5\u51b5\u4f1a\u5728\u6211\u4eec\u8ba8\u8bba\u7ee7\u627f\u65f6\u9047\u5230\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 int main () { int x { 5 }; int & ref { x }; // okay: reference to int is bound to int variable double y { 6.0 }; int & invalidRef { y }; // invalid; reference to int cannot bind to double variable double & invalidRef2 { x }; // invalid: reference to double cannot bind to int variable return 0 ; } \u5bf9 void \u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528\u662f\u4e0d\u53ef\u4ee5\u7684\uff08\u8fd9\u4e48\u505a\u7684\u76ee\u7684\u53c8\u662f\u4ec0\u4e48\u5462\uff1f\uff09 \u5f15\u7528\u4e0d\u80fd\u88ab\u91cd\u65b0\u8bbe\u7f6e(\u4f7f\u5176\u5f15\u7528\u5176\u4ed6\u5bf9\u8c61) \u5f15\u7528\u4e00\u65e6\u88ab\u521d\u59cb\u5316\uff0c\u5b83\u4fbf\u4e0d\u80fd\u591f\u88ab\u91cd\u65b0\u7ed1\u5b9a\u5230\u5176\u4ed6\u5bf9\u8c61\u3002 \u65b0\u624b C++ \u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u5c1d\u8bd5\u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\u91cd\u65b0\u8bbe\u7f6e\u5f15\u7528\u3002\u8fd9\u4e48\u505a\u662f\u53ef\u4ee5\u7f16\u8bd1\u5e76\u8fd0\u884c\u7684\uff0c\u4f46\u662f\u5b83\u7684\u6548\u679c\u5e76\u4e0d\u662f\u6211\u4eec\u671f\u671b\u7684\u90a3\u6837\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { int x { 5 }; int y { 6 }; int & ref { x }; // ref is now an alias for x ref = y ; // assigns 6 (the value of y) to x (the object being referenced by ref) // The above line does NOT change ref into a reference to variable y! std :: cout << x ; // user is expecting this to print 5 return 0 ; } \u7ed3\u679c\u53ef\u80fd\u51fa\u4eba\u610f\u6599\uff1a 1 6 \u5f53\u8868\u8fbe\u5f0f\u4e2d\u7684\u5f15\u7528\u8fdb\u884c\u6c42\u503c\u65f6\uff0c\u5b83\u4f1a\u89e3\u6790\u6210\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u3002\u6240\u4ee5 ref = y \u5e76\u4e0d\u4f1a\u4e3a ref \u91cd\u65b0\u8bbe\u7f6e\u4e3a y \u7684\u5f15\u7528\u3002\u5b9e\u9645\u60c5\u51b5\u662f ref \u662f x \u7684\u5f15\u7528\uff0c\u56e0\u6b64\u4e0a\u9762\u7684\u8868\u8fbe\u5f0f\u7b49\u4ef7\u4e8e x = y \uff0c\u56e0\u4e3a y \u7684\u503c\u4e3a 6 \uff0c\u6240\u4ee5 x \u88ab\u8d4b\u503c\u4e3a 6 \u3002 \u5de6\u503c\u5f15\u7528\u7684\u4f5c\u7528\u57df\u548c\u6301\u7eed\u65f6\u95f4 \u5f15\u7528\u53d8\u91cf\u7684 \u4f5c\u7528\u57df \u548c \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u6240\u9075\u5faa\u7684\u89c4\u5219\u548c\u4e00\u822c\u53d8\u91cf\u662f\u4e00\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { int x { 5 }; // normal integer int & ref { x }; // reference to variable value return 0 ; } // x and ref die here \u5f15\u7528\u548c\u88ab\u5f15\u7528\u5bf9\u8c61\u5177\u6709\u72ec\u7acb\u7684\u751f\u547d\u5468\u671f \u9664\u4e86\u67d0\u79cd\u4f8b\u5916\u60c5\u51b5\uff08\u4e0b\u4e00\u8282\u4f1a\u4ecb\u7ecd\uff09\u4e4b\u5916\uff0c \u5f15\u7528\u548c\u88ab\u5f15\u7528\u5bf9\u8c61\u5177\u6709\u72ec\u7acb\u7684\u751f\u547d\u5468\u671f \u3002\u6362\u8a00\u4e4b\uff0c\u4e0b\u9762\u4e24\u6761\u662f\u6b63\u786e\u7684\uff1a \u88ab\u5f15\u7528\u5bf9\u8c61\u53ef\u4ee5\u5148\u4e8e\u5f15\u7528\u9500\u6bc1\uff1b \u5f15\u7528\u53ef\u4ee5\u5148\u4e8e\u88ab\u5f15\u7528\u5bf9\u8c61\u9500\u6bc1\u3002 \u5982\u679c\u5f15\u7528\u5148\u4e8e\u88ab\u5f15\u7528\u5bf9\u8c61\u9500\u6bc1\uff0c\u5b8c\u5168\u4e0d\u4f1a\u5f71\u54cd\u5230\u88ab\u5f15\u7528\u5bf9\u8c61\uff0c\u4e0b\u9762\u7a0b\u5e8f\u53ef\u4ee5\u8868\u660e\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int x { 5 }; { int & ref { x }; // ref is a reference to x std :: cout << ref ; // prints value of ref (5) } // ref is destroyed here -- x is unaware of this std :: cout << x ; // prints value of x (5) return 0 ; } // x destroyed here \u6253\u5370\u7ed3\u679c\uff1a 1 55 \u5f53 ref \u88ab\u9500\u6bc1\u540e\uff0c\u53d8\u91cf x \u4e00\u5207\u5982\u6545\u3002 \u5168\u7136\u4e0d\u77e5\u6253\u5b83\u7684\u5f15\u7528\u5df2\u7ecf\u88ab\u9500\u6bc1\u4e86\u3002 \u60ac\u5782\u5f15\u7528 \u5f53\u4e00\u4e2a\u5bf9\u8c61\u5148\u4e8e\u5b83\u7684\u5f15\u7528\u88ab\u9500\u6bc1\uff0c\u5219\u5176\u5f15\u7528\u6b64\u65f6\u4f1a\u7ed1\u5b9a\u5230\u4e00\u4e2a\u5df2\u7ecf\u4e0d\u5b58\u5728\u7684\u5bf9\u8c61\u3002\u6b64\u65f6\u8be5\u5f15\u7528\u4f1a\u6210\u4e3a\u4e00\u4e2a \u60ac\u5782 \u5f15\u7528\u3002\u8bbf\u95ee\u60ac\u5782\u5f15\u7528\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u60ac\u5782\u6307\u9488\u5176\u5b9e\u5f88\u5bb9\u6613\u907f\u514d\uff0c\u4f46\u662f\u6211\u4eec\u4f1a\u5728 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 \u4e2d\u50cf\u4f60\u5c55\u793a\u4e00\u4e2a\u7531\u60ac\u5782\u6307\u9488\u5f15\u53d1\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u6848\u4f8b\u3002 \u5f15\u7528\u4e0d\u662f\u5bf9\u8c61 \u4e5f\u8bb8\u51fa\u4e4e\u4f60\u7684\u610f\u6599\uff0c\u5728C++\u4e2d\u5f15\u7528\u5e76\u4e0d\u662f\u4e00\u4e2a\u5bf9\u8c61\u3002 \u5f15\u7528\u5e76\u4e0d\u9700\u8981\u5b58\u5728\u6216\u5360\u6709\u5b58\u50a8\u7a7a\u95f4 \u3002\u5982\u679c\u53ef\u80fd\u7684\u8bdd\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u4f18\u5316\u5f15\u7528\uff0c\u4f7f\u7528\u88ab\u5f15\u7528\u5bf9\u8c61\u5bf9\u5176\u8fdb\u884c\u66ff\u6362\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u5e76\u603b\u662f\u53ef\u884c\u7684\uff0c\u6b64\u65f6\u5f15\u7528\u4fbf\u9700\u8981\u5b58\u50a8\u7a7a\u95f4\u3002 \u8fd9\u4e5f\u610f\u5473\u7740\u201c\u5f15\u7528\u53d8\u91cf\u201d\u8fd9\u4e2a\u672f\u8bed\u6709\u4e9b\u7528\u8bcd\u4e0d\u5f53\uff0c\u56e0\u4e3a\u53d8\u91cf\u662f\u5bf9\u8c61\u7684\u540d\u5b57\uff0c\u800c\u5f15\u7528\u5e76\u4e0d\u662f\u5bf9\u8c61\u3002 \u56e0\u4e3a==\u5f15\u7528\u5e76\u4e0d\u662f\u5bf9\u8c61\uff0c\u6240\u4ee5\u4f60\u5e76\u4e0d\u80fd\u5728\u4efb\u4f55\u80fd\u4f7f\u7528\u5bf9\u8c61\u7684\u5730\u65b9\u6765\u4f7f\u7528\u5b83\uff08\u4f8b\u5982\uff0c\u4f60\u4e0d\u80fd\u521b\u5efa\u5f15\u7528\u7684\u5f15\u7528\uff0c\u56e0\u4e3a\u4f5c\u6307\u5f15\u4e0e\u5fc5\u987b\u5f15\u7528\u4e00\u4e2a\u6709\u8eab\u4efd\u6807\u8bc6\u7684\u5bf9\u8c61\uff09==\u3002\u4e07\u4e00\u4f60\u9700\u8981\u4e00\u4e2a\u652f\u6301\u91cd\u65b0\u7ed1\u5b9a\u7684\u5bf9\u8c61\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 std::reference_wrapper (\u5728 16.3 -- Aggregation \u4e2d\u4ecb\u7ecd)\u3002 \u9898\u5916\u8bdd \u8003\u8651\u4e0b\u5217\u53d8\u91cf\uff1a 1 2 3 int var {}; int & ref1 { var }; // an lvalue reference bound to var int & ref2 { ref1 }; // an lvalue reference bound to var \u56e0\u4e3a ref2 (\u662f\u4e00\u4e2a\u5f15\u7528) \u88ab\u521d\u59cb\u5316\u4e3a ref1 (\u4e5f\u662f\u4e00\u4e2a\u5f15\u7528)\uff0c\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a ref2 \u662f\u4e00\u4e2a\u5f15\u7528\u7684\u5f15\u7528\u3002\u4f46\u5176\u5b9e\u5b83\u4e0d\u662f\uff0c\u56e0\u4e3a ref1 \u662f var \u7684\u5f15\u7528\uff0c\u5f53\u6211\u4eec\u5728\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u5b83\u7684\u7684\u65f6\u5019\uff08\u4f5c\u4e3a\u521d\u59cb\u5316\u503c\uff09\uff0c ref1 \u4f1a\u6c42\u503c\u5f97\u5230 var \u3002\u6240\u4ee5 ref2 \u5176\u5b9e\u53ea\u662f\u4e00\u4e2a\u666e\u901a\u7684 \u5de6\u503c\u5f15\u7528 \uff08\u6b63\u5982\u5b83\u7684\u7c7b\u578b\u6240\u8868\u660e\u7684\u90a3\u6837\uff09\uff0c\u5b83\u4e5f\u4e0e var \u7ed1\u5b9a\u3002 \u5f15\u7528\u7684\u5f15\u7528\u672c\u6765\u53ef\u4ee5\u4f7f\u7528\u8bed\u6cd5 int&& \u7684\uff0c\u53ef\u60dcC++\u5e76\u4e0d\u652f\u6301\u5f15\u7528\u7684\u5f15\u7528\uff0c\u56e0\u6b64\u8fd9\u4e2a\u8bed\u6cd5\u5f62\u5f0f\u5728 C++11\u4e2d\u88ab\u7528\u6765\u8868\u793a \u53f3\u503c\u5f15\u7528 \uff09\u3002","title":"9.3 - \u5de6\u503c\u5f15\u7528"},{"location":"9-3-Lvalue-references/#_1","text":"\u5de6\u503c\u5f15\u7528\uff08\u901a\u5e38\u7b80\u79f0\u4e3a\u5f15\u7528\u56e0\u4e3aC++11\u4e4b\u524d\u53ea\u6709\u8fd9\u4e00\u79cd\u7c7b\u578b\u7684\u5f15\u7528\uff09\u662f\u4e00\u4e2a\u5df2\u5b58\u5728\u5de6\u503c\uff08\u4f8b\u5982\u67d0\u4e2a\u53d8\u91cf\uff09\u7684\u522b\u540d\u3002 \u58f0\u660e\u5de6\u503c\u7c7b\u578b\u53ef\u4ee5\u5728\u7c7b\u578b\u58f0\u660e\u65f6\u4f7f\u7528 & \u7b26\u53f7\uff1a 1 2 3 int // a normal int type int & // an lvalue reference to an int object double & // an lvalue reference to a double object","title":"\u5de6\u503c\u5f15\u7528\u7c7b\u578b"},{"location":"9-3-Lvalue-references/#_2","text":"\u5de6\u503c\u5f15\u7528\u7c7b\u578b\u53ef\u4ee5\u7528\u6765\u58f0\u660e\u4e00\u4e2a\u5de6\u503c\u5f15\u7528\u53d8\u91cf\u3002\u5de6\u503c\u5f15\u7528\u53d8\u91cf\u4f5c\u4e3a\u5bf9\u4e00\u4e2a\u5de6\u503c\u7684\u5f15\u7528\u6765\u4f7f\u7528\uff08\u901a\u5e38\u662f\u53e6\u5916\u4e00\u4e2a\u53d8\u91cf\uff09\u3002 \u521b\u5efa\u5de6\u503c\u5f15\u7528\u53d8\u91cf\uff0c\u53ea\u9700\u8981\u4f7f\u7528\u5de6\u503c\u5f15\u7528\u7c7b\u578b\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u5373\u53ef\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { int x { 5 }; // x is a normal integer variable int & ref { x }; // ref is an lvalue reference variable that can now be used as an alias for variable x std :: cout << x << '\\n' ; // print the value of x (5) std :: cout << ref << '\\n' ; // print the value of x via ref (5) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528 int& \u5b9a\u4e49 ref \u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5bf9 int \u7c7b\u578b\u5de6\u503c\u7684\u5f15\u7528\uff0c\u7136\u540e\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u5de6\u503c\u8868\u8fbe\u5f0f x \u5bf9\u5176\u8fdb\u884c\u4e86\u521d\u59cb\u5316\u3002\u4ece\u6b64\u4ee5\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06 ref \u4f5c\u4e3a x \u7684\u540c\u4e49\u8bcd\u6765\u4f7f\u7528\u3002\u7a0b\u5e8f\u4f1a\u6253\u5370\u5982\u4e0b\u5185\u5bb9\uff1a 1 2 5 5 \u4ece\u7f16\u8bd1\u5668\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u5c06 & \u653e\u5728\u7c7b\u578b\u65c1\u8fb9( int& ref )\u8fd8\u662f\u53d8\u91cf\u65c1\u8fb9( int &ref )\u5e76\u6ca1\u6709\u4ec0\u4e48\u533a\u522b\uff0c\u9009\u62e9\u90a3\u79cd\u5199\u6cd5\u53ea\u5173\u4e4e\u4e8e\u98ce\u683c\u3002\u73b0\u4ee3C++\u7a0b\u5e8f\u5458\u901a\u5e38\u4f1a\u9009\u62e9\u5c06 & \u7d27\u6328\u7740\u7c7b\u578b\u5173\u952e\u5b57\u653e\u7f6e\uff0c\u56e0\u4e3a\u8fd9\u6837\u505a\u53ef\u4ee5\u66f4\u6e05\u695a\u5730\u8868\u660e\u5f15\u7528\u662f\u7c7b\u578b\u4fe1\u606f\u7684\u4e00\u90e8\u5206\uff0c\u800c\u4e0d\u662f\u6807\u8bc6\u7b26\u7684\u4e00\u90e8\u5206\u3002 \u6700\u4f73\u5b9e\u8df5 \u5728\u5b9a\u4e49\u5f15\u7528\u7684\u65f6\u5019\uff0c\u5c06 & \u7d27\u6328\u7740\u7c7b\u578b\u5173\u952e\u8bcd\u653e\u7f6e\uff08\u800c\u4e0d\u662f\u53d8\u91cf\u540d\uff09\u3002 \u6269\u5c55\u9605\u8bfb \u5bf9\u4e8e\u90a3\u4e9b\u5df2\u7ecf\u719f\u6089\u6307\u9488\u7684\u5f00\u53d1\u8005\u6765\u8bf4\uff0c & \u5728\u8fd9\u91cc\u542b\u4e49\u5e76\u4e0d\u662f\u53d6\u5730\u5740\u800c\u662f\u5de6\u503c\u5f15\u7528\u3002","title":"\u5de6\u503c\u5f15\u7528\u53d8\u91cf"},{"location":"9-3-Lvalue-references/#_3","text":"\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u901a\u8fc7\u5f15\u7528\u6765\u83b7\u53d6\u88ab\u5f15\u7528\u5bf9\u8c61\u7684\u503c\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u6765\u4fee\u6539\u88ab\u5f15\u7528\u5bf9\u8c61\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include int main () { int x { 5 }; // normal integer variable int & ref { x }; // ref is now an alias for variable x std :: cout << x << ref ; // print 55 x = 6 ; // x now has value 6 std :: cout << x << ref ; // prints 66 ref = 7 ; // the object being referenced (x) now has value 7 std :: cout << x << ref ; // prints 77 return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 556677 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c ref \u662f x \u7684\u522b\u540d\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 x \u6216\u8005 ref \u6765\u4fee\u6539 x \u7684\u503c\u3002","title":"\u901a\u8fc7\u5de6\u503c\u5f15\u7528\u6765\u4fee\u6539\u503c"},{"location":"9-3-Lvalue-references/#_4","text":"\u548c\u5e38\u91cf\u975e\u5e38\u7c7b\u4f3c\uff0c\u6240\u6709\u7684\u5f15\u7528\u90fd\u5fc5\u987b\u88ab\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 int main () { int & invalidRef ; // error: references must be initialized int x { 5 }; int & ref { x }; // okay: reference to int is bound to int variable return 0 ; } \u5f53\u4f7f\u7528\u4e00\u4e2a\u5bf9\u8c61(\u6216\u51fd\u6570)\u521d\u59cb\u5316\u5f15\u7528\u65f6\uff0c\u6211\u4eec\u8bf4\u5b83\u88ab\u7ed1\u5b9a\u5230\u4e86\u90a3\u4e2a\u5bf9\u8c61(\u6216\u51fd\u6570)\u3002\u7ed1\u5b9a\u6b64\u7c7b\u5f15\u7528\u7684\u8fc7\u7a0b\u79f0\u4e3a\u5f15\u7528\u7ed1\u5b9a\u3002\u88ab\u5f15\u7528\u7684\u5bf9\u8c61(\u6216\u51fd\u6570)\u6709\u65f6\u79f0\u4e3a referent\u3002 \u4f5c\u6307\u5f15\u4e0e\u5fc5\u987b\u88ab\u7ed1\u5b9a\u5230\u4e00\u4e2a\u53ef\u4fee\u6539\u5de6\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 int main () { int x { 5 }; int & ref { x }; // valid: lvalue reference bound to a modifiable lvalue const int y { 5 }; int & invalidRef { y }; // invalid: can't bind to a non-modifiable lvalue int & invalidRef2 { 0 }; // invalid: can't bind to an r-value return 0 ; } \u5de6\u503c\u5f15\u7528\u4e0d\u80fd\u88ab\u7ed1\u5b9a\u5230\u4e00\u4e2a\u4e0d\u53ef\u4fee\u6539\u7684\u5de6\u503c\uff08\u5426\u5219\u4f60\u5c82\u4e0d\u662f\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u6765\u4fee\u6539\u8fd9\u4e2a\u503c\u54af\uff0c\u8fd9\u663e\u7136\u8fdd\u80cc\u4e86\u5e38\u91cf\u7684\u5e38\u91cf\u6027\uff09\u3002\u56e0\u6b64\uff0c\u5de6\u503c\u5f15\u7528\u6709\u65f6\u4e5f\u79f0\u4e3a\u975econst\u53d8\u91cf\u7684\u5de6\u503c\u5f15\u7528\uff08\u7b80\u79f0\u4e3a\u975econst\u53d8\u91cf\u7684\u5f15\u7528\uff09\u3002 \u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5de6\u503c\u5f15\u7528\u7684\u7c7b\u578b\u5fc5\u987b\u548c\u88ab\u5f15\u7528\u5bf9\u8c61\u7684\u7c7b\u578b\u76f8\u5339\u914d\uff08\u4f8b\u5916\u7684\u60c5\u51b5\u4f1a\u5728\u6211\u4eec\u8ba8\u8bba\u7ee7\u627f\u65f6\u9047\u5230\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 int main () { int x { 5 }; int & ref { x }; // okay: reference to int is bound to int variable double y { 6.0 }; int & invalidRef { y }; // invalid; reference to int cannot bind to double variable double & invalidRef2 { x }; // invalid: reference to double cannot bind to int variable return 0 ; } \u5bf9 void \u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528\u662f\u4e0d\u53ef\u4ee5\u7684\uff08\u8fd9\u4e48\u505a\u7684\u76ee\u7684\u53c8\u662f\u4ec0\u4e48\u5462\uff1f\uff09","title":"\u5de6\u503c\u5f15\u7528\u7684\u521d\u59cb\u5316"},{"location":"9-3-Lvalue-references/#_5","text":"\u5f15\u7528\u4e00\u65e6\u88ab\u521d\u59cb\u5316\uff0c\u5b83\u4fbf\u4e0d\u80fd\u591f\u88ab\u91cd\u65b0\u7ed1\u5b9a\u5230\u5176\u4ed6\u5bf9\u8c61\u3002 \u65b0\u624b C++ \u7a0b\u5e8f\u5458\u7ecf\u5e38\u4f1a\u5c1d\u8bd5\u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\u91cd\u65b0\u8bbe\u7f6e\u5f15\u7528\u3002\u8fd9\u4e48\u505a\u662f\u53ef\u4ee5\u7f16\u8bd1\u5e76\u8fd0\u884c\u7684\uff0c\u4f46\u662f\u5b83\u7684\u6548\u679c\u5e76\u4e0d\u662f\u6211\u4eec\u671f\u671b\u7684\u90a3\u6837\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { int x { 5 }; int y { 6 }; int & ref { x }; // ref is now an alias for x ref = y ; // assigns 6 (the value of y) to x (the object being referenced by ref) // The above line does NOT change ref into a reference to variable y! std :: cout << x ; // user is expecting this to print 5 return 0 ; } \u7ed3\u679c\u53ef\u80fd\u51fa\u4eba\u610f\u6599\uff1a 1 6 \u5f53\u8868\u8fbe\u5f0f\u4e2d\u7684\u5f15\u7528\u8fdb\u884c\u6c42\u503c\u65f6\uff0c\u5b83\u4f1a\u89e3\u6790\u6210\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u3002\u6240\u4ee5 ref = y \u5e76\u4e0d\u4f1a\u4e3a ref \u91cd\u65b0\u8bbe\u7f6e\u4e3a y \u7684\u5f15\u7528\u3002\u5b9e\u9645\u60c5\u51b5\u662f ref \u662f x \u7684\u5f15\u7528\uff0c\u56e0\u6b64\u4e0a\u9762\u7684\u8868\u8fbe\u5f0f\u7b49\u4ef7\u4e8e x = y \uff0c\u56e0\u4e3a y \u7684\u503c\u4e3a 6 \uff0c\u6240\u4ee5 x \u88ab\u8d4b\u503c\u4e3a 6 \u3002","title":"\u5f15\u7528\u4e0d\u80fd\u88ab\u91cd\u65b0\u8bbe\u7f6e(\u4f7f\u5176\u5f15\u7528\u5176\u4ed6\u5bf9\u8c61)"},{"location":"9-3-Lvalue-references/#_6","text":"\u5f15\u7528\u53d8\u91cf\u7684 \u4f5c\u7528\u57df \u548c \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u6240\u9075\u5faa\u7684\u89c4\u5219\u548c\u4e00\u822c\u53d8\u91cf\u662f\u4e00\u6837\u7684\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { int x { 5 }; // normal integer int & ref { x }; // reference to variable value return 0 ; } // x and ref die here","title":"\u5de6\u503c\u5f15\u7528\u7684\u4f5c\u7528\u57df\u548c\u6301\u7eed\u65f6\u95f4"},{"location":"9-3-Lvalue-references/#_7","text":"\u9664\u4e86\u67d0\u79cd\u4f8b\u5916\u60c5\u51b5\uff08\u4e0b\u4e00\u8282\u4f1a\u4ecb\u7ecd\uff09\u4e4b\u5916\uff0c \u5f15\u7528\u548c\u88ab\u5f15\u7528\u5bf9\u8c61\u5177\u6709\u72ec\u7acb\u7684\u751f\u547d\u5468\u671f \u3002\u6362\u8a00\u4e4b\uff0c\u4e0b\u9762\u4e24\u6761\u662f\u6b63\u786e\u7684\uff1a \u88ab\u5f15\u7528\u5bf9\u8c61\u53ef\u4ee5\u5148\u4e8e\u5f15\u7528\u9500\u6bc1\uff1b \u5f15\u7528\u53ef\u4ee5\u5148\u4e8e\u88ab\u5f15\u7528\u5bf9\u8c61\u9500\u6bc1\u3002 \u5982\u679c\u5f15\u7528\u5148\u4e8e\u88ab\u5f15\u7528\u5bf9\u8c61\u9500\u6bc1\uff0c\u5b8c\u5168\u4e0d\u4f1a\u5f71\u54cd\u5230\u88ab\u5f15\u7528\u5bf9\u8c61\uff0c\u4e0b\u9762\u7a0b\u5e8f\u53ef\u4ee5\u8868\u660e\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include int main () { int x { 5 }; { int & ref { x }; // ref is a reference to x std :: cout << ref ; // prints value of ref (5) } // ref is destroyed here -- x is unaware of this std :: cout << x ; // prints value of x (5) return 0 ; } // x destroyed here \u6253\u5370\u7ed3\u679c\uff1a 1 55 \u5f53 ref \u88ab\u9500\u6bc1\u540e\uff0c\u53d8\u91cf x \u4e00\u5207\u5982\u6545\u3002 \u5168\u7136\u4e0d\u77e5\u6253\u5b83\u7684\u5f15\u7528\u5df2\u7ecf\u88ab\u9500\u6bc1\u4e86\u3002","title":"\u5f15\u7528\u548c\u88ab\u5f15\u7528\u5bf9\u8c61\u5177\u6709\u72ec\u7acb\u7684\u751f\u547d\u5468\u671f"},{"location":"9-3-Lvalue-references/#_8","text":"\u5f53\u4e00\u4e2a\u5bf9\u8c61\u5148\u4e8e\u5b83\u7684\u5f15\u7528\u88ab\u9500\u6bc1\uff0c\u5219\u5176\u5f15\u7528\u6b64\u65f6\u4f1a\u7ed1\u5b9a\u5230\u4e00\u4e2a\u5df2\u7ecf\u4e0d\u5b58\u5728\u7684\u5bf9\u8c61\u3002\u6b64\u65f6\u8be5\u5f15\u7528\u4f1a\u6210\u4e3a\u4e00\u4e2a \u60ac\u5782 \u5f15\u7528\u3002\u8bbf\u95ee\u60ac\u5782\u5f15\u7528\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u60ac\u5782\u6307\u9488\u5176\u5b9e\u5f88\u5bb9\u6613\u907f\u514d\uff0c\u4f46\u662f\u6211\u4eec\u4f1a\u5728 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 \u4e2d\u50cf\u4f60\u5c55\u793a\u4e00\u4e2a\u7531\u60ac\u5782\u6307\u9488\u5f15\u53d1\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u6848\u4f8b\u3002","title":"\u60ac\u5782\u5f15\u7528"},{"location":"9-3-Lvalue-references/#_9","text":"\u4e5f\u8bb8\u51fa\u4e4e\u4f60\u7684\u610f\u6599\uff0c\u5728C++\u4e2d\u5f15\u7528\u5e76\u4e0d\u662f\u4e00\u4e2a\u5bf9\u8c61\u3002 \u5f15\u7528\u5e76\u4e0d\u9700\u8981\u5b58\u5728\u6216\u5360\u6709\u5b58\u50a8\u7a7a\u95f4 \u3002\u5982\u679c\u53ef\u80fd\u7684\u8bdd\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c1d\u8bd5\u4f18\u5316\u5f15\u7528\uff0c\u4f7f\u7528\u88ab\u5f15\u7528\u5bf9\u8c61\u5bf9\u5176\u8fdb\u884c\u66ff\u6362\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e48\u505a\u5e76\u603b\u662f\u53ef\u884c\u7684\uff0c\u6b64\u65f6\u5f15\u7528\u4fbf\u9700\u8981\u5b58\u50a8\u7a7a\u95f4\u3002 \u8fd9\u4e5f\u610f\u5473\u7740\u201c\u5f15\u7528\u53d8\u91cf\u201d\u8fd9\u4e2a\u672f\u8bed\u6709\u4e9b\u7528\u8bcd\u4e0d\u5f53\uff0c\u56e0\u4e3a\u53d8\u91cf\u662f\u5bf9\u8c61\u7684\u540d\u5b57\uff0c\u800c\u5f15\u7528\u5e76\u4e0d\u662f\u5bf9\u8c61\u3002 \u56e0\u4e3a==\u5f15\u7528\u5e76\u4e0d\u662f\u5bf9\u8c61\uff0c\u6240\u4ee5\u4f60\u5e76\u4e0d\u80fd\u5728\u4efb\u4f55\u80fd\u4f7f\u7528\u5bf9\u8c61\u7684\u5730\u65b9\u6765\u4f7f\u7528\u5b83\uff08\u4f8b\u5982\uff0c\u4f60\u4e0d\u80fd\u521b\u5efa\u5f15\u7528\u7684\u5f15\u7528\uff0c\u56e0\u4e3a\u4f5c\u6307\u5f15\u4e0e\u5fc5\u987b\u5f15\u7528\u4e00\u4e2a\u6709\u8eab\u4efd\u6807\u8bc6\u7684\u5bf9\u8c61\uff09==\u3002\u4e07\u4e00\u4f60\u9700\u8981\u4e00\u4e2a\u652f\u6301\u91cd\u65b0\u7ed1\u5b9a\u7684\u5bf9\u8c61\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 std::reference_wrapper (\u5728 16.3 -- Aggregation \u4e2d\u4ecb\u7ecd)\u3002 \u9898\u5916\u8bdd \u8003\u8651\u4e0b\u5217\u53d8\u91cf\uff1a 1 2 3 int var {}; int & ref1 { var }; // an lvalue reference bound to var int & ref2 { ref1 }; // an lvalue reference bound to var \u56e0\u4e3a ref2 (\u662f\u4e00\u4e2a\u5f15\u7528) \u88ab\u521d\u59cb\u5316\u4e3a ref1 (\u4e5f\u662f\u4e00\u4e2a\u5f15\u7528)\uff0c\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a ref2 \u662f\u4e00\u4e2a\u5f15\u7528\u7684\u5f15\u7528\u3002\u4f46\u5176\u5b9e\u5b83\u4e0d\u662f\uff0c\u56e0\u4e3a ref1 \u662f var \u7684\u5f15\u7528\uff0c\u5f53\u6211\u4eec\u5728\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u5b83\u7684\u7684\u65f6\u5019\uff08\u4f5c\u4e3a\u521d\u59cb\u5316\u503c\uff09\uff0c ref1 \u4f1a\u6c42\u503c\u5f97\u5230 var \u3002\u6240\u4ee5 ref2 \u5176\u5b9e\u53ea\u662f\u4e00\u4e2a\u666e\u901a\u7684 \u5de6\u503c\u5f15\u7528 \uff08\u6b63\u5982\u5b83\u7684\u7c7b\u578b\u6240\u8868\u660e\u7684\u90a3\u6837\uff09\uff0c\u5b83\u4e5f\u4e0e var \u7ed1\u5b9a\u3002 \u5f15\u7528\u7684\u5f15\u7528\u672c\u6765\u53ef\u4ee5\u4f7f\u7528\u8bed\u6cd5 int&& \u7684\uff0c\u53ef\u60dcC++\u5e76\u4e0d\u652f\u6301\u5f15\u7528\u7684\u5f15\u7528\uff0c\u56e0\u6b64\u8fd9\u4e2a\u8bed\u6cd5\u5f62\u5f0f\u5728 C++11\u4e2d\u88ab\u7528\u6765\u8868\u793a \u53f3\u503c\u5f15\u7528 \uff09\u3002","title":"\u5f15\u7528\u4e0d\u662f\u5bf9\u8c61"},{"location":"9-4-Lvalue-references-to-const/","tags":["lvalue references","const"],"text":"Key Takeaway \u5de6\u503c\u5f15\u7528\u53ea\u80fd\u7ed1\u5b9a\u5230\u53ef\u4fee\u6539\u7684\u5de6\u503c\u3002 \u5bf9const\u7684\u5de6\u503c\u5f15\u7528\u53ef\u4ee5\u7ed1\u5b9a\u5230\u53ef\u4fee\u6539\u7684\u5de6\u503c\u3001\u4e0d\u53ef\u4fee\u6539\u7684\u5de6\u503c\u548c\u53f3\u503c\u3002\u8fd9\u4f7f\u5f97\u5b83\u4eec\u6210\u4e3a\u4e00\u79cd\u66f4\u52a0\u7075\u6d3b\u7684\u5f15\u7528\u7c7b\u578b\u3002 \u5728\u4e0a\u8282\u8bfe ( 9.3 - \u5de6\u503c\u5f15\u7528 ) \u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u4e3a\u58f0\u660e \u5de6\u503c\u5f15\u7528 \u53ea\u80fd\u7ed1\u5b9a\u5230\u4e00\u4e2a\u53ef\u4fee\u6539\u7684 \u5de6\u503c \u4e0a\u3002\u8fd9\u610f\u5473\u7740\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u975e\u6cd5\u7684\uff1a 1 2 3 4 5 6 7 int main () { const int x { 5 }; // x is a non-modifiable (const) lvalue int & ref { x }; // error: ref can not bind to non-modifiable lvalue return 0 ; } \u8fd9\u662f\u4e0d\u5141\u8bb8\u7684\uff0c\u56e0\u4e3a\u8fd9\u6837\u6211\u4eec\u5c31\u80fd\u591f\u901a\u8fc7\u975e const \u5f15\u7528( ref )\u4fee\u6539 const \u53d8\u91cf( x )\u3002 \u4f46\u5982\u679c\u6211\u4eec\u60f3\u8981\u4e00\u4e2a const \u53d8\u91cf\u6765\u521b\u5efa\u5f15\u7528\uff0c\u8be5\u600e\u4e48\u529e\u5462\uff1f\u666e\u901a\u7684\u5de6\u503c\u5f15\u7528(\u6307\u5411\u975econst\u503c)\u662f\u4e0d\u884c\u7684\u3002 \u6307\u5411 const \u7684\u5de6\u503c\u5f15\u7528 \u4f7f\u7528 const \u5173\u952e\u5b57\u58f0\u660e\u5de6\u503c\u5f15\u7528\uff0c\u5373\u8981\u6c42\u5de6\u503c\u5f15\u7528\u5c06\u5176\u5f15\u7528\u7684\u5bf9\u8c61\u5f53\u505aconst\u770b\u5230\u3002\u6b64\u65f6\u79f0\u8be5\u5f15\u7528\u4e3a\u6307\u5411const\u5bf9\u8c61\u7684\u5de6\u503c\u5f15\u7528\uff08\u6709\u65f6\u79f0\u4e3a\u6307\u5411const\u7684\u5f15\u7528\u6216 const\u5de6\u503c\u5f15\u7528 \uff09\u3002 \u5bf9const\u5f15\u7528\u53ef\u4ee5\u7ed1\u5b9a\u5230\u4e0d\u53ef\u4fee\u6539\u7684\u5de6\u503c: 1 2 3 4 5 6 7 int main () { const int x { 5 }; // x is a non-modifiable lvalue const int & ref { x }; // okay: ref is a an lvalue reference to a const value return 0 ; } \u56e0\u4e3aconst\u7684\u5de6\u503c\u5f15\u7528\u5c06\u5b83\u4eec\u5f15\u7528\u7684\u5bf9\u8c61\u89c6\u4e3aconst\uff0c\u6240\u4ee5\u5b83\u4eec\u53ef\u4ee5\u7528\u4e8e\u8bbf\u95ee\u4f46\u4e0d\u80fd\u4fee\u6539\u88ab\u5f15\u7528\u7684\u503c: 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { const int x { 5 }; // x is a non-modifiable lvalue const int & ref { x }; // okay: ref is a an lvalue reference to a const value std :: cout << ref ; // okay: we can access the const object ref = 6 ; // error: we can not modify a const object return 0 ; } \u4f7f\u7528\u53ef\u4fee\u6539\u5de6\u503c\u521d\u59cb\u5316const\u5de6\u503c\u5f15\u7528 const\u5de6\u503c\u5f15\u7528\u4e5f\u53ef\u4ee5\u7ed1\u5b9a\u5230\u53ef\u4fee\u6539\u7684\u5de6\u503c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u5f15\u7528\u8bbf\u95ee\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u65f6\u5c06\u88ab\u89c6\u4e3aconst(\u5373\u4f7f\u5e95\u5c42\u5bf9\u8c61\u4e0d\u662fconst): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 5 }; // x is a modifiable lvalue const int & ref { x }; // okay: we can bind a const reference to a modifiable lvalue std :: cout << ref ; // okay: we can access the object through our const reference ref = 7 ; // error: we can not modify an object through a const reference x = 6 ; // okay: x is a modifiable lvalue, we can still modify it through the original identifier return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06const\u5f15\u7528 ref \u7ed1\u5b9a\u5230\u53ef\u4fee\u6539\u5de6\u503c x \u3002\u968f\u540e\u4fbf\u53ef\u4ee5\u4f7f\u7528 ref \u8bbf\u95ee x \uff0c\u4f46\u662f\u56e0\u4e3a ref \u662f const \u7684\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u80fd\u901a\u8fc7 ref \u4fee\u6539 x \u3002\u7136\u800c\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539 x \u3002 \u6700\u4f73\u5b9e\u8df5 \u9664\u975e\u9700\u8981\u4fee\u6539\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\uff0c\u6700\u597d\u4f7f\u7528\u201cconst\u5de6\u503c\u5f15\u7528\u201d\uff0c\u800c\u4e0d\u662f\u201c\u975econst\u7684\u5de6\u503c\u5f15\u7528\u201d\u3002 \u4f7f\u7528\u53f3\u503c\u521d\u59cb\u5316const\u5de6\u503c\u5f15\u7528 \u4e5f\u8bb8\u4ee4\u4eba\u60ca\u8bb6\u7684\u662f\uff0cconst\u5de6\u503c\u5f15\u7528\u4e5f\u53ef\u4ee5\u7ed1\u5b9a\u5230\u53f3\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { const int & ref { 5 }; // okay: 5 is an rvalue std :: cout << ref ; // prints 5 return 0 ; } \u5f53\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\u65f6\uff0c\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\u5e76\u7528\u53f3\u503c\u521d\u59cb\u5316\uff0c\u5bf9const\u7684\u5f15\u7528\u88ab\u7ed1\u5b9a\u5230\u8be5\u4e34\u65f6\u5bf9\u8c61\u3002 \u4e34\u65f6\u5bf9\u8c61(\u6709\u65f6\u4e5f\u79f0\u4e3a\u533f\u540d\u5bf9\u8c61)\u662f\u5728\u5355\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u4e3a\u4e34\u65f6\u4f7f\u7528(\u7136\u540e\u9500\u6bc1)\u800c\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u4e34\u65f6\u5bf9\u8c61\u5b8c\u5168\u6ca1\u6709\u4f5c\u7528\u57df(\u8fd9\u662f\u6709\u610f\u4e49\u7684\uff0c\u56e0\u4e3a==\u4f5c\u7528\u57df\u662f\u6807\u8bc6\u7b26\u7684\u5c5e\u6027\uff0c\u800c\u4e34\u65f6\u5bf9\u8c61\u6ca1\u6709\u6807\u8bc6\u7b26==)\u3002\u8fd9\u610f\u5473\u7740\u4e34\u65f6\u5bf9\u8c61\u53ea\u80fd\u5728\u521b\u5efa\u5b83\u7684\u4f4d\u7f6e\u76f4\u63a5\u4f7f\u7528\uff0c\u56e0\u4e3a\u6ca1\u6709\u529e\u6cd5\u5728\u8be5\u4f4d\u7f6e\u4e4b\u540e\u518d\u5f15\u7528\u5b83\u3002 const \u5f15\u7528\u7ed1\u5b9a\u5230\u4e34\u65f6\u5bf9\u8c61\u65f6\u4f1a\u5ef6\u957f\u4e34\u65f6\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f \u4e34\u65f6\u5bf9\u8c61\u901a\u5e38\u5728\u521b\u5efa\u5b83\u4eec\u7684\u8868\u8fbe\u5f0f\u7684\u672b\u5c3e\u88ab\u9500\u6bc1\u3002 \u4f46\u662f\uff0c\u5982\u679c\u4e3a\u4fdd\u5b58\u53f3\u503c 5 \u800c\u521b\u5efa\u7684\u4e34\u65f6\u5bf9\u8c61\u5728\u521d\u59cb\u5316 ref \u7684\u8868\u8fbe\u5f0f\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\uff0c\u90a3\u4e48\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u4f1a\u53d1\u751f\u4ec0\u4e48\u60c5\u51b5\u5462\uff1f\u5f15\u7528 ref \u5c06\u53d8\u6210 \u60ac\u5782 \u5f15\u7528(\u5f15\u7528\u4e00\u4e2a\u5df2\u7ecf\u88ab\u9500\u6bc1\u7684\u5bf9\u8c61)\uff0c\u5f53\u6211\u4eec\u8bd5\u56fe\u8bbf\u95ee ref \u65f6\uff0c\u5c06\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u4e0b\u7684 \u60ac\u5782 \u5f15\u7528\uff0cC++\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u89c4\u5219\uff1a \u5f53const\u5de6\u503c\u5f15\u7528\u88ab\u7ed1\u5b9a\u5230\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\u65f6\uff0c\u4e34\u65f6\u5bf9\u8c61\u7684\u751f\u5b58\u671f\u5c06\u88ab\u6269\u5c55\u5230\u4e0e\u5f15\u7528\u7684\u751f\u5b58\u671f\u76f8\u5339\u914d\u3002 1 2 3 4 5 6 7 8 9 10 #include int main () { const int & ref { 5 }; // The temporary object holding value 5 has its lifetime extended to match ref std :: cout << ref ; // Therefore, we can safely use it here return 0 ; } // Both ref and the temporary object die here \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53\u4f7f\u7528\u53f3\u503c 5 \u521d\u59cb\u5316 ref \u65f6\uff0c\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\u88ab\u521b\u5efa\u5e76\u7ed1\u5b9a\u5230 ref \u3002\u8be5\u4e34\u65f6\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u5c31\u4f1a\u88ab\u6269\u5c55\u5230 ref \u7684\u751f\u547d\u5468\u671f\u3002Thus, we can safely print the value of ref in the next statement. Then both ref and the temporary object go out of scope and are destroyed at the end of the block. \u5173\u952e\u4fe1\u606f \u5de6\u503c\u5f15\u7528\u53ea\u80fd\u7ed1\u5b9a\u5230\u53ef\u4fee\u6539\u7684\u5de6\u503c\u3002 \u5bf9const\u7684\u5de6\u503c\u5f15\u7528\u53ef\u4ee5\u7ed1\u5b9a\u5230\u53ef\u4fee\u6539\u7684\u5de6\u503c\u3001\u4e0d\u53ef\u4fee\u6539\u7684\u5de6\u503c\u548c\u53f3\u503c\u3002\u8fd9\u4f7f\u5f97\u5b83\u4eec\u6210\u4e3a\u4e00\u79cd\u66f4\u52a0\u7075\u6d3b\u7684\u5f15\u7528\u7c7b\u578b\u3002 \u90a3\u4e48\uff0c\u4e3a\u4ec0\u4e48C++\u5141\u8bb8const\u5f15\u7528\u7ed1\u5b9a\u5230\u53f3\u503c\u5462\uff1f\u6211\u4eec\u5c06\u5728 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 \u4e2d\u56de\u7b54\u8fd9\u4e2a\u95ee\u9898!","title":"9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528"},{"location":"9-4-Lvalue-references-to-const/#const","text":"\u4f7f\u7528 const \u5173\u952e\u5b57\u58f0\u660e\u5de6\u503c\u5f15\u7528\uff0c\u5373\u8981\u6c42\u5de6\u503c\u5f15\u7528\u5c06\u5176\u5f15\u7528\u7684\u5bf9\u8c61\u5f53\u505aconst\u770b\u5230\u3002\u6b64\u65f6\u79f0\u8be5\u5f15\u7528\u4e3a\u6307\u5411const\u5bf9\u8c61\u7684\u5de6\u503c\u5f15\u7528\uff08\u6709\u65f6\u79f0\u4e3a\u6307\u5411const\u7684\u5f15\u7528\u6216 const\u5de6\u503c\u5f15\u7528 \uff09\u3002 \u5bf9const\u5f15\u7528\u53ef\u4ee5\u7ed1\u5b9a\u5230\u4e0d\u53ef\u4fee\u6539\u7684\u5de6\u503c: 1 2 3 4 5 6 7 int main () { const int x { 5 }; // x is a non-modifiable lvalue const int & ref { x }; // okay: ref is a an lvalue reference to a const value return 0 ; } \u56e0\u4e3aconst\u7684\u5de6\u503c\u5f15\u7528\u5c06\u5b83\u4eec\u5f15\u7528\u7684\u5bf9\u8c61\u89c6\u4e3aconst\uff0c\u6240\u4ee5\u5b83\u4eec\u53ef\u4ee5\u7528\u4e8e\u8bbf\u95ee\u4f46\u4e0d\u80fd\u4fee\u6539\u88ab\u5f15\u7528\u7684\u503c: 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { const int x { 5 }; // x is a non-modifiable lvalue const int & ref { x }; // okay: ref is a an lvalue reference to a const value std :: cout << ref ; // okay: we can access the const object ref = 6 ; // error: we can not modify a const object return 0 ; }","title":"\u6307\u5411 const \u7684\u5de6\u503c\u5f15\u7528"},{"location":"9-4-Lvalue-references-to-const/#const_1","text":"const\u5de6\u503c\u5f15\u7528\u4e5f\u53ef\u4ee5\u7ed1\u5b9a\u5230\u53ef\u4fee\u6539\u7684\u5de6\u503c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u5f15\u7528\u8bbf\u95ee\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u65f6\u5c06\u88ab\u89c6\u4e3aconst(\u5373\u4f7f\u5e95\u5c42\u5bf9\u8c61\u4e0d\u662fconst): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () { int x { 5 }; // x is a modifiable lvalue const int & ref { x }; // okay: we can bind a const reference to a modifiable lvalue std :: cout << ref ; // okay: we can access the object through our const reference ref = 7 ; // error: we can not modify an object through a const reference x = 6 ; // okay: x is a modifiable lvalue, we can still modify it through the original identifier return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06const\u5f15\u7528 ref \u7ed1\u5b9a\u5230\u53ef\u4fee\u6539\u5de6\u503c x \u3002\u968f\u540e\u4fbf\u53ef\u4ee5\u4f7f\u7528 ref \u8bbf\u95ee x \uff0c\u4f46\u662f\u56e0\u4e3a ref \u662f const \u7684\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u80fd\u901a\u8fc7 ref \u4fee\u6539 x \u3002\u7136\u800c\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539 x \u3002 \u6700\u4f73\u5b9e\u8df5 \u9664\u975e\u9700\u8981\u4fee\u6539\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\uff0c\u6700\u597d\u4f7f\u7528\u201cconst\u5de6\u503c\u5f15\u7528\u201d\uff0c\u800c\u4e0d\u662f\u201c\u975econst\u7684\u5de6\u503c\u5f15\u7528\u201d\u3002","title":"\u4f7f\u7528\u53ef\u4fee\u6539\u5de6\u503c\u521d\u59cb\u5316const\u5de6\u503c\u5f15\u7528"},{"location":"9-4-Lvalue-references-to-const/#const_2","text":"\u4e5f\u8bb8\u4ee4\u4eba\u60ca\u8bb6\u7684\u662f\uff0cconst\u5de6\u503c\u5f15\u7528\u4e5f\u53ef\u4ee5\u7ed1\u5b9a\u5230\u53f3\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { const int & ref { 5 }; // okay: 5 is an rvalue std :: cout << ref ; // prints 5 return 0 ; } \u5f53\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\u65f6\uff0c\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\u5e76\u7528\u53f3\u503c\u521d\u59cb\u5316\uff0c\u5bf9const\u7684\u5f15\u7528\u88ab\u7ed1\u5b9a\u5230\u8be5\u4e34\u65f6\u5bf9\u8c61\u3002 \u4e34\u65f6\u5bf9\u8c61(\u6709\u65f6\u4e5f\u79f0\u4e3a\u533f\u540d\u5bf9\u8c61)\u662f\u5728\u5355\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u4e3a\u4e34\u65f6\u4f7f\u7528(\u7136\u540e\u9500\u6bc1)\u800c\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u4e34\u65f6\u5bf9\u8c61\u5b8c\u5168\u6ca1\u6709\u4f5c\u7528\u57df(\u8fd9\u662f\u6709\u610f\u4e49\u7684\uff0c\u56e0\u4e3a==\u4f5c\u7528\u57df\u662f\u6807\u8bc6\u7b26\u7684\u5c5e\u6027\uff0c\u800c\u4e34\u65f6\u5bf9\u8c61\u6ca1\u6709\u6807\u8bc6\u7b26==)\u3002\u8fd9\u610f\u5473\u7740\u4e34\u65f6\u5bf9\u8c61\u53ea\u80fd\u5728\u521b\u5efa\u5b83\u7684\u4f4d\u7f6e\u76f4\u63a5\u4f7f\u7528\uff0c\u56e0\u4e3a\u6ca1\u6709\u529e\u6cd5\u5728\u8be5\u4f4d\u7f6e\u4e4b\u540e\u518d\u5f15\u7528\u5b83\u3002","title":"\u4f7f\u7528\u53f3\u503c\u521d\u59cb\u5316const\u5de6\u503c\u5f15\u7528"},{"location":"9-4-Lvalue-references-to-const/#const_3","text":"\u4e34\u65f6\u5bf9\u8c61\u901a\u5e38\u5728\u521b\u5efa\u5b83\u4eec\u7684\u8868\u8fbe\u5f0f\u7684\u672b\u5c3e\u88ab\u9500\u6bc1\u3002 \u4f46\u662f\uff0c\u5982\u679c\u4e3a\u4fdd\u5b58\u53f3\u503c 5 \u800c\u521b\u5efa\u7684\u4e34\u65f6\u5bf9\u8c61\u5728\u521d\u59cb\u5316 ref \u7684\u8868\u8fbe\u5f0f\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\uff0c\u90a3\u4e48\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u4f1a\u53d1\u751f\u4ec0\u4e48\u60c5\u51b5\u5462\uff1f\u5f15\u7528 ref \u5c06\u53d8\u6210 \u60ac\u5782 \u5f15\u7528(\u5f15\u7528\u4e00\u4e2a\u5df2\u7ecf\u88ab\u9500\u6bc1\u7684\u5bf9\u8c61)\uff0c\u5f53\u6211\u4eec\u8bd5\u56fe\u8bbf\u95ee ref \u65f6\uff0c\u5c06\u4ea7\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u4e0b\u7684 \u60ac\u5782 \u5f15\u7528\uff0cC++\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u89c4\u5219\uff1a \u5f53const\u5de6\u503c\u5f15\u7528\u88ab\u7ed1\u5b9a\u5230\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\u65f6\uff0c\u4e34\u65f6\u5bf9\u8c61\u7684\u751f\u5b58\u671f\u5c06\u88ab\u6269\u5c55\u5230\u4e0e\u5f15\u7528\u7684\u751f\u5b58\u671f\u76f8\u5339\u914d\u3002 1 2 3 4 5 6 7 8 9 10 #include int main () { const int & ref { 5 }; // The temporary object holding value 5 has its lifetime extended to match ref std :: cout << ref ; // Therefore, we can safely use it here return 0 ; } // Both ref and the temporary object die here \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53\u4f7f\u7528\u53f3\u503c 5 \u521d\u59cb\u5316 ref \u65f6\uff0c\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\u88ab\u521b\u5efa\u5e76\u7ed1\u5b9a\u5230 ref \u3002\u8be5\u4e34\u65f6\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u5c31\u4f1a\u88ab\u6269\u5c55\u5230 ref \u7684\u751f\u547d\u5468\u671f\u3002Thus, we can safely print the value of ref in the next statement. Then both ref and the temporary object go out of scope and are destroyed at the end of the block. \u5173\u952e\u4fe1\u606f \u5de6\u503c\u5f15\u7528\u53ea\u80fd\u7ed1\u5b9a\u5230\u53ef\u4fee\u6539\u7684\u5de6\u503c\u3002 \u5bf9const\u7684\u5de6\u503c\u5f15\u7528\u53ef\u4ee5\u7ed1\u5b9a\u5230\u53ef\u4fee\u6539\u7684\u5de6\u503c\u3001\u4e0d\u53ef\u4fee\u6539\u7684\u5de6\u503c\u548c\u53f3\u503c\u3002\u8fd9\u4f7f\u5f97\u5b83\u4eec\u6210\u4e3a\u4e00\u79cd\u66f4\u52a0\u7075\u6d3b\u7684\u5f15\u7528\u7c7b\u578b\u3002 \u90a3\u4e48\uff0c\u4e3a\u4ec0\u4e48C++\u5141\u8bb8const\u5f15\u7528\u7ed1\u5b9a\u5230\u53f3\u503c\u5462\uff1f\u6211\u4eec\u5c06\u5728 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 \u4e2d\u56de\u7b54\u8fd9\u4e2a\u95ee\u9898!","title":"const \u5f15\u7528\u7ed1\u5b9a\u5230\u4e34\u65f6\u5bf9\u8c61\u65f6\u4f1a\u5ef6\u957f\u4e34\u65f6\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f"},{"location":"9-5-Pass-by-lvalue-reference/","tags":["Lvalue"],"text":"Key Takeaway \u5f62\u53c2\u5b9a\u4e49\u4e3a\u6307\u5411\u975econst\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u5b83\u53ea\u80fd\u63a5\u6536\u975econst\u7c7b\u578b\u7684\u5b9e\u53c2\uff0c\u65e0\u6cd5\u914d\u5408\u975econst\u7c7b\u578b\u548c\u5b57\u9762\u91cf\u4f7f\u7528\uff0c\u6781\u5927\u5730\u9650\u5236\u4e86\u5b83\u7684\u4f5c\u7528\u3002\u56e0\u6b64\u4e00\u822c\u53ef\u4ee5\u5b9a\u4e49\u4e3aconst\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u6b64\u65f6\u5c31\u53ef\u4ee5\u914d\u5408const\u7c7b\u578b\u3001\u975econst\u7c7b\u578b\u548c\u5b57\u9762\u91cf\u6765\u4f7f\u7528\u3002\u4f46\u6b64\u65f6\u4f60\u5c31\u4e0d\u80fd\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5b9e\u53c2\u7684\u5185\u5bb9\u3002 \u5bf9\u4e8e\u62f7\u8d1d\u5f00\u9500\u5c0f\u7684\u5bf9\u8c61\uff0c\u4f7f\u7528\u6309\u503c\u4f20\u9012\uff0c\u5bf9\u4e8e\u62f7\u8d1d\u5f00\u9500\u5927\u7684\u5bf9\u8c61\uff0c\u4f7f\u7528\u6309const\u5f15\u7528\u4f20\u9012\u3002\u5982\u679c\u4e0d\u786e\u5b9a\u5f00\u9500\u5927\u8fd8\u662f\u5c0f\uff0c\u5219\u4f7f\u7528\u6309const\u5f15\u7528\u4f20\u9012\u3002 \u5982\u679c\u5bf9\u8c61\u4f7f\u7528\u5185\u5b58\u4e0d\u8d85\u8fc7\u4e24\u4e2a \u5b57\u957f \u4e14\u6ca1\u6709\u989d\u5916\u7684\u8bbe\u7f6e\u5de5\u4f5c\uff08\u6253\u5f00\u6587\u4ef6\u6216\u5206\u914d\u5185\u5b58\uff09\uff0c\u5219\u53ef\u4ee5\u8ba4\u4e3a\u62f7\u8d1d\u5f00\u9500\u5c0f \u62f7\u8d1d\u5f00\u9500\u5c0f\u7684\u7c7b\u578b\u901a\u5e38\u6709\uff1a\u6240\u6709\u57fa\u672c\u7c7b\u578b\u3001\u679a\u4e3e\u7c7b\u578b\u548c std::string_view . \u62f7\u8d1d\u5f00\u9500\u5927\u7684\u7c7b\u578b\u901a\u5e38\u6709\uff1a std::array , std::string , std::vector , \u548c std::ostream . \u5728\u524d\u9762\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5de6\u503c\u5f15\u7528 ( 9.3 - \u5de6\u503c\u5f15\u7528 ) \u548c\u6307\u5411const\u7684\u5de6\u503c\u5f15\u7528 ( 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 )\u3002\u5355\u72ec\u6765\u770b\uff0c\u8fd9\u4e24\u79cd\u5f15\u7528\u770b\u4e0a\u53bb\u90fd\u6ca1\u5565\u7528\u2014\u2014\u4e3a\u4ec0\u4e48\u6211\u4eec\u80fd\u591f\u76f4\u63a5\u8bbf\u95ee\u53d8\u91cf\u5374\u8fd8\u8981\u4e3a\u5176\u521b\u5efa\u4e00\u4e2a\u522b\u540d\u5462\uff1f \u672c\u8282\u8bfe\u6211\u4eec\u4f1a\u5411\u4f60\u5c55\u793a\uff0c\u4e3a\u4ec0\u4e48\u5f15\u7528\u975e\u5e38\u6709\u7528\u3002\u800c\u4e14\u4ece\u672c\u7ae0\u5f00\u59cb\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u6211\u4eec\u4f1a\u7ecf\u5e38\u4f7f\u7528\u5b83\u4eec\u3002 \u9996\u5148\uff0c\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\uff08\u56de\u770b 2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2 \uff09\uff0c\u6211\u4eec\u8ba8\u8bba\u8fc7 \u6309\u503c\u4f20\u9012 \uff0c\u5373 \u5b9e\u53c2 \u5728\u4f20\u5165\u51fd\u6570\u4f5c\u4e3a \u5f62\u53c2 \u65f6\uff0c\u901a\u8fc7\u521b\u5efa\u62f7\u8d1d\u7684\u65b9\u5f0f\u4f20\u5165\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include void printValue ( int y ) { std :: cout << y << '\\n' ; } // y is destroyed here int main () { int x { 2 }; printValue ( x ); // x is passed by value (copied) into parameter y (inexpensive) return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53 printValue(x) \u88ab\u8c03\u7528\u7684\u65f6\u5019\uff0c x \u7684\u503c\u4f1a\u88ab\u62f7\u8d1d\u5230\u5f62\u53c2 y \u3002\u5f53\u5230\u8fbe\u51fd\u6570\u7ed3\u5c3e\u7684\u65f6\u5019\uff0c y \u5c31\u4f1a\u88ab\u9500\u6bc1\u3002 \u8fd9\u610f\u5473\u7740\u5f53\u6211\u4eec\u8c03\u7528\u51fd\u6570\u7684\u65f6\u5019\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u5b9e\u53c2\u503c\u7684\u62f7\u8d1d\uff0c\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u540e\u4fbf\u5c06\u5176\u9500\u6bc1\u4e86\uff01\u5e78\u8fd0\u5730\u662f\uff0c\u56e0\u4e3a\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u62f7\u8d1d\u5f00\u9500\u5e76\u4e0d\u5927\uff0c\u6240\u4ee5\u8fd9\u5e76\u4e0d\u4f1a\u5e26\u6765\u4ec0\u4e48\u95ee\u9898\u3002 \u6709\u4e9b\u5bf9\u8c61\u7684\u62f7\u8d1d\u5f00\u9500\u5f88\u5927 \u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u591a\u6570\u6570\u636e\u7c7b\u578b\u90fd\u5c5e\u4e8e\u7c7b(\u4f8b\u5982 std::string )\u3002\u7c7b\u7c7b\u578b\u7684\u62f7\u8d1d\u5f00\u9500\u901a\u5e38\u662f\u5f88\u5927\u7684\u3002\u5982\u679c\u53ef\u80fd\uff0c\u5e94\u8be5\u5c3d\u53ef\u80fd\u5730\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\uff0c\u5c24\u5176\u662f\u5728\u8fd9\u4e9b\u53d8\u91cf\u7528\u5b8c\u5373\u5f03\u7684\u60c5\u51b5\u4e0b\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u6b63\u662f\u8868\u660e\u4e86\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include void printValue ( std :: string y ) { std :: cout << y << '\\n' ; } // y is destroyed here int main () { std :: string x { \"Hello, world!\" }; // x is a std::string printValue ( x ); // x is passed by value (copied) into parameter y (expensive) return 0 ; } \u7a0b\u5e8f\u4f1a\u6253\u5370\uff1a 1 Hello, world! \u5c3d\u7ba1\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u548c\u6211\u4eec\u60f3\u7684\u4e00\u6837\uff0c\u4f46\u5176\u6548\u7387\u662f\u5f88\u4f4e\u7684\u3002\u548c\u524d\u9762\u7684\u7a0b\u5e8f\u4e00\u6837\u5f53\u6211\u4eec\u8c03\u7528 printValue() \u7684\u65f6\u5019\uff0c\u5b9e\u53c2 x \u4f1a\u88ab\u62f7\u8d1d\u5230\u5f62\u53c2 y \u3002\u4e0d\u8fc7\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b9e\u53c2 std::string \u5e76\u4e0d\u50cf int \u90a3\u6837\uff0c std::string \u662f\u4e00\u79cd\u7c7b\u7c7b\u578b\uff0c\u5bf9\u5b83\u7684\u62f7\u8d1d\u5f00\u9500\u662f\u975e\u5e38\u5927\u7684\uff0c\u66f4\u4e0d\u7528\u8bf4\u6bcf\u6b21\u8c03\u7528 printValue() \u90fd\u4f1a\u4ea7\u751f\u8fd9\u6837\u7684\u5f00\u9500\u3002 \u6211\u4eec\u53ef\u4ee5\u505a\u7684\u66f4\u597d\u3002 \u6309\u5f15\u7528\u4f20\u9012 \u907f\u514d\u51fd\u6570\u8c03\u7528\u65f6\u4ea7\u751f\u7684\u62f7\u8d1d\u5f00\u9500\uff0c\u53ef\u901a\u8fc7 \u6309\u5f15\u7528\u4f20\u9012 \u6765\u4ee3\u66ff \u6309\u503c\u4f20\u9012 \u3002\u5f53\u4f7f\u7528\u6309\u5f15\u7528\u4f20\u9012\u65f6\uff0c\u6211\u4eec\u5c06\u51fd\u6570\u7684\u5f62\u53c2\u58f0\u660e\u4e3a\u5f15\u7528\u7c7b\u578b\uff08\u6216\u6307\u5411const\u7684\u5f15\u7528\uff09\u800c\u4e0d\u662f\u666e\u901a\u7c7b\u578b\u3002\u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u6bcf\u4e2a\u5f15\u7528\u7c7b\u578b\u7684\u5f62\u53c2\u4f1a\u88ab\u7ed1\u5b9a\u5230\u4f20\u5165\u7684\u5b9e\u53c2\u3002\u56e0\u4e3a\u5f15\u7528\u5176\u5b9e\u662f\u5b9e\u53c2\u7684\u522b\u540d\uff0c\u6240\u4ee5\u5e76\u4e0d\u4f1a\u521b\u5efa\u62f7\u8d1d\u3002 \u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u548c\u4e0a\u9762\u7684\u4f8b\u5b50\u662f\u4e00\u81f4\u7684\uff0c\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\u5176\u4f7f\u7528\u4e86\u6309\u5f15\u7528\u4f20\u9012\u800c\u4e0d\u662f\u6309\u503c\u51fa\u4f20\u9012\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include void printValue ( std :: string & y ) // type changed to std::string& { std :: cout << y << '\\n' ; } // y is destroyed here int main () { std :: string x { \"Hello, world!\" }; printValue ( x ); // x is now passed by reference into reference parameter y (inexpensive) return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u548c\u4e0a\u9762\u7684\u7a0b\u5e8f\u6bcf\u592a\u5927\u533a\u522b\uff0c\u9664\u4e86\u5f62\u53c2 y \u7684\u7c7b\u578b\u4ece std::string \u88ab\u6539\u4e3a std::string& (\u5de6\u503c\u5f15\u7528)\u3002\u73b0\u5728\uff0c\u5f53 printValue(x) \u88ab\u8c03\u7528\u7684\u65f6\u5019\uff0c\u5de6\u503c\u5f15\u7528\u7c7b\u578b\u7684\u5f62\u53c2 y \u88ab\u7ed1\u5b9a\u5230\u5b9e\u53c2 x \u3002\u5f15\u7528\u7684\u7ed1\u5b9a\u5f00\u9500\u603b\u662f\u5f88\u5c0f\u7684\uff0c\u5b83\u4e0d\u9700\u8981\u521b\u5efa x \u7684\u62f7\u8d1d\u3002\u56e0\u4e3a\u5f15\u7528\u662f\u88ab\u5f15\u7528\u5bf9\u8c61\u7684\u522b\u540d\uff0c\u6240\u4ee5\u5f53 printValue() \u4f7f\u7528 y \u65f6\uff0c\u5b83\u8bbf\u95ee\u7684\u662f\u5b9e\u53c2 x \u672c\u8eab\uff08\u800c\u4e0d\u662f x \u7684\u62f7\u8d1d\uff09\u3002 \u5173\u952e\u4fe1\u606f \u6309\u5f15\u7528\u4f20\u9012\u5141\u8bb8\u6211\u4eec\u5411\u51fd\u6570\u4f20\u9012\u53c2\u6570\u800c\u4e0d\u9700\u521b\u5efa\u62f7\u8d1d\u3002 \u6309\u5f15\u7528\u4f20\u9012\u5141\u8bb8\u51fd\u6570\u4fee\u6539\u5b9e\u53c2\u7684\u503c \u5f53\u4e00\u4e2a\u5bf9\u8c61\u901a\u8fc7\u6309\u503c\u4f20\u9012\u7684\u65b9\u5f0f\u4f20\u5165\u51fd\u6570\u65f6\uff0c\u51fd\u6570\u63a5\u6536\u5230\u7684\u662f\u5176\u62f7\u8d1d\u3002\u8fd9\u4e5f\u5c31\u610f\u5473\u7740\u4efb\u4f55\u5bf9\u5f62\u53c2\u7684\u4fee\u6539\u90fd\u662f\u5bf9\u8fd9\u4efd\u62f7\u8d1d\u7684\u4fee\u6539\uff0c\u800c\u4e0d\u662f\u5b9e\u53c2\u672c\u8eab\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void addOne ( int y ) // y is a copy of x { ++ y ; // this modifies the copy of x, not the actual object x } int main () { int x { 5 }; std :: cout << \"value = \" << x << '\\n' ; addOne ( x ); std :: cout << \"value = \" << x << '\\n' ; // x has not been modified return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u5f62\u53c2 y \u662f\u5b9e\u53c2 x \u7684\u62f7\u8d1d\uff0c\u5f53\u6211\u4eec\u5bf9 y \u8fdb\u884c\u9012\u589e\u65f6\uff0c\u5b83\u53ea\u4f1a\u5f71\u54cd\u5230 y \uff0c\u56e0\u6b64\u7a0b\u5e8f\u7684\u8f93\u51fa\u4e3a\uff1a 1 2 value = 5 value = 5 \u7136\u800c\uff0c\u7531\u4e8e\u5f15\u7528\u6765\u8bf4\uff0c\u56e0\u4e3a\u5b83\u5c31\u662f\u88ab\u5f15\u7528\u5bf9\u8c61\u672c\u8eab\uff0c\u5f53\u4f7f\u7528\u6309\u5f15\u7528\u4f20\u9012\u65f6\uff0c\u5bf9\u5f15\u7528\u5f62\u53c2\u7684\u4efb\u4f55\u66f4\u6539\u90fd\u5c06\u5f71\u54cd\u5b9e\u53c2\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void addOne ( int & y ) // y is bound to the actual object x { ++ y ; // this modifies the actual object x } int main () { int x { 5 }; std :: cout << \"value = \" << x << '\\n' ; addOne ( x ); std :: cout << \"value = \" << x << '\\n' ; // x has been modified return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u4e3a\uff1a 1 2 value = 5 value = 6 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c x \u9996\u5148\u88ab\u521d\u59cb\u5316\u4e3a 5\u3002\u5f53 addOne(x) \u88ab\u8c03\u7528\u65f6\uff0c\u5f15\u7528\u7c7b\u578b\u5f62\u53c2 y \u4f1a\u88ab\u7ed1\u5b9a\u5230\u5b9e\u53c2 x \u3002\u5f53\u6211\u4eec\u5728 addOne() \u51fd\u6570\u4e2d\u5bf9 y \u8fdb\u884c\u9012\u589e\u65f6\uff0c\u5b83\u5b9e\u9645\u4fee\u6539\u4e86\u5b9e\u53c2 x \u7684\u503c\uff08\u800c\u4e0d\u662f\u5b83\u7684\u62f7\u8d1d\uff09\u3002\u56e0\u6b64\u5f53 addOne() \u8c03\u7528\u7ed3\u675f\u540e\u53d8\u91cf\u7684\u503c\u4ecd\u7136\u662f\u88ab\u4fee\u6539\u4e86\u3002 \u5173\u952e\u4fe1\u606f \u901a\u8fc7\u6309\u503c\u4f20\u9012\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u4fee\u6539\u4f20\u5165\u7684\u5b9e\u53c2\u3002 \u51fd\u6570\u4fee\u6539\u4f20\u5165\u53c2\u6570\u503c\u7684\u80fd\u529b\u662f\u5f88\u6709\u7528\u7684\u3002\u5047\u8bbe\u4f60\u7f16\u5199\u4e86\u4e00\u4e2a\u51fd\u6570\u6765\u5224\u65ad\u602a\u7269\u662f\u5426\u6210\u529f\u653b\u51fb\u4e86\u73a9\u5bb6\u3002\u5982\u679c\u662f\u8fd9\u6837\uff0c\u602a\u7269\u5e94\u8be5\u5bf9\u73a9\u5bb6\u7684\u751f\u547d\u503c\u9020\u6210\u4e00\u5b9a\u7684\u4f24\u5bb3\u3002\u5982\u679c\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u73a9\u5bb6\u5bf9\u8c61\uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539\u4f20\u5165\u7684\u5b9e\u9645\u73a9\u5bb6\u5bf9\u8c61\u7684\u5065\u5eb7\u72b6\u51b5\u3002\u5982\u679c\u4f60\u901a\u8fc7\u503c\u4f20\u9012\u73a9\u5bb6\u5bf9\u8c61\uff0c\u4f60\u53ea\u80fd\u4fee\u6539\u73a9\u5bb6\u5bf9\u8c61\u526f\u672c\u7684\u5065\u5eb7\u503c\uff0c\u8fd9\u5c31\u6ca1\u4ec0\u4e48\u7528\u4e86\u3002 \u4f20\u9012\u6307\u5411\u975econst\u7c7b\u578b\u7684\u5f15\u7528\u65f6\u53ea\u80fd\u63a5\u6536\u53ef\u4ee5\u4fee\u6539\u7684\u5de6\u503c\u5b9e\u53c2 \u56e0\u4e3a\u6267\u884c\u975econst\u503c\u7684\u5f15\u7528\u53ea\u80fd\u7ed1\u5b9a\u5230\u4e00\u4e2a\u53ef\u4fee\u6539\u7684 \u5de6\u503c \uff08\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u975econst\u7c7b\u578b\u53d8\u91cf\uff09\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u6309\u5f15\u7528\u4f20\u9012\u53ea\u80fd\u914d\u5408\u53ef\u4fee\u6539\u5de6\u503c\u6765\u4f7f\u7528\u3002\u4ece\u5b9e\u7528\u6027\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u8fd9\u65e0\u7591\u6781\u5927\u5730\u9650\u5236\u4e86\u6309\u503c\u4f20\u9012\u7684\u5b9e\u7528\u6027\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u80fd\u591f\u4e3a\u5176\u4f20\u9012const\u53d8\u91cf\u548c \u5b57\u9762\u91cf \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include void printValue ( int & y ) // y only accepts modifiable lvalues { std :: cout << y << '\\n' ; } int main () { int x { 5 }; printValue ( x ); // ok: x is a modifiable lvalue const int z { 5 }; printValue ( z ); // error: z is a non-modifiable lvalue printValue ( 5 ); // error: 5 is an rvalue return 0 ; } \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u6709\u66f4\u597d\u7684\u529e\u6cd5\u3002 \u4f20\u9012\u6307\u5411 const \u7684\u5f15\u7528 \u4e0d\u540c\u4e8e\u6307\u5411\u975econst\u7684\u5f15\u7528\uff08\u53ea\u80fd\u7ed1\u5b9a\u53ef\u4fee\u6539\u5de6\u503c\uff09\uff0cconst\u7c7b\u578b\u5f15\u7528\u53ef\u4ee5\u7ed1\u5b9a\u53ef\u4fee\u6539\u5de6\u503c\u3001\u4e0d\u53ef\u4fee\u6539\u5de6\u503c\u548c\u53f3\u503c\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u628a\u5f62\u53c2\u5b9a\u4e49\u4e3aconst\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u5219\u53ef\u4ee5\u5c06\u5176\u7ed1\u5b9a\u5230\u4efb\u4f55\u7c7b\u578b\u7684\u5b9e\u53c2\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include void printValue ( const int & y ) // y is now a const reference { std :: cout << y << '\\n' ; } int main () { int x { 5 }; printValue ( x ); // ok: x is a modifiable lvalue const int z { 5 }; printValue ( z ); // ok: z is a non-modifiable lvalue printValue ( 5 ); // ok: 5 is a literal rvalue return 0 ; } \u6309const\u5f15\u7528\u4f20\u9012\u63d0\u4f9b\u4e86\u548c\u6309\u5f15\u7528\u4f20\u9012\u4e00\u6837\u7684\u4f18\u70b9\uff08\u907f\u514d\u5b9e\u53c2\u62f7\u8d1d\uff09\uff0c\u540c\u65f6\u8fd8\u53ef\u4ee5\u907f\u514d\u51fd\u6570\u5bf9\u5b9e\u53c2\u8fdb\u884c\u4fee\u6539\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u662f\u65e0\u6cd5\u8fdb\u884c\u7684\uff0c\u56e0\u4e3a ref \u662f const \u7684\uff1a 1 2 3 4 void addOne ( const int & ref ) { ++ ref ; // not allowed: ref is const } \u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5176\u5b9e\u5e76\u4e0d\u5e0c\u671b\u51fd\u6570\u7684\u5b9e\u53c2\u88ab\u4fee\u6539\u3002 \u6700\u4f73\u5b9e\u8df5 \u6309\u5f15\u7528\u4f20\u9012\u6700\u597d\u5b9a\u4e49\u4e3aconst\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u9664\u975e\u4f60\u6709\u5f88\u597d\u7684\u7406\u7531\uff08\u4f8b\u5982\u51fd\u6570\u5fc5\u987b\u4fee\u6539\u5b9e\u53c2\u7684\u503c\uff09\u3002 \u73b0\u5728\uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u53ef\u4ee5\u4e86\u89e3\u652f\u6301const\u7c7b\u578b\u5de6\u503c\u5f15\u7528\u7684\u52a8\u673a\u4e86\uff0c\u5982\u679c\u6ca1\u6709\u5b83\uff0c\u6211\u4eec\u5c31\u65e0\u6cd5\u4f7f\u7528\u6309\u5f15\u7528\u4f20\u9012\u7684\u51fd\u6570\u4f20\u9012\u5b57\u9762\u91cf\uff08\u6216\u5176\u4ed6\u53f3\u503c\uff09\u3002 \u540c\u65f6\u4f7f\u7528\u6309\u503c\u4f20\u9012\u548c\u6309\u5f15\u7528\u4f20\u9012 \u5177\u6709\u591a\u4e2a\u5f62\u53c2\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u5206\u522b\u6307\u5b9a\u6bcf\u4e2a\u53c2\u6570\u662f\u6309\u503c\u4f20\u9012\u8fd8\u662f\u6309\u5f15\u7528\u4f20\u9012\uff1a \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include void foo ( int a , int & b , const std :: string & c ) { } int main () { int x { 5 }; const std :: string s { \"Hello, world!\" }; foo ( 5 , x , s ); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7b2c\u4e00\u4e2a\u5b9e\u53c2\u662f\u6309\u503c\u4f20\u9012\u7684\uff0c\u800c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5219\u662f\u6309\u5f15\u7528\u4f20\u9012\u7684\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570\u5219\u662f\u6309const\u5f15\u7528\u4f20\u9012\u7684\u3002 \u4f55\u65f6\u4f7f\u7528\u6309\u5f15\u7528\u4f20\u9012 \u56e0\u4e3a\u7c7b\u7c7b\u578b\u7684\u62f7\u8d1d\u5f00\u9500\u5f88\u5927\uff0c\u7c7b\u7c7b\u578b\u901a\u5e38\u4f1a\u6309\u5f15\u7528\u4f20\u9012\u800c\u4e0d\u662f\u6309\u503c\u4f20\u9012\u4ee5\u907f\u514d\u6602\u8d35\u7684\u62f7\u8d1d\u5f00\u9500\u3002\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u62f7\u8d1d\u5f00\u9500\u662f\u5f88\u5c0f\u7684\uff0c\u6240\u4ee5\u5b83\u4eec\u901a\u5e38\u4f1a\u6309\u503c\u4f20\u9012\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4f7f\u7528 \u6309\u503c\u4f20\u9012 \uff0c\u800c\u5bf9\u4e8e\u7c7b\u6216\u8005\u7ed3\u6784\u4f53\u4f7f\u7528const\u7684\u6309\u503c\u4f20\u9012\u3002 \u6309\u503c\u4f20\u9012\u548c\u6309\u5f15\u7528\u4f20\u9012\u7684\u5f00\u9500\u6bd4\u8f83\uff08\u8fdb\u9636\u8bdd\u9898\uff09 \u5176\u5b9e\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u7684\u7c7b\u7c7b\u578b\u90fd\u5fc5\u987b \u6309\u503c\u4f20\u9012 \u3002\u5f53\u7136\u4f60\u53ef\u80fd\u4f1a\u95ee\uff0c\u90fd\u6309\u503c\u4f20\u9012\u6709\u4f55\u4e0d\u53ef\u5462\uff1f\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u4f1a\u8be6\u7ec6\u5730\u5206\u6790\u6309\u503c\u4f20\u9012\u548c\u6309\u5f15\u7528\u4f20\u9012\u7684\u5f00\u9500\uff0c\u5e76\u63d0\u51fa\u5982\u4f55\u9009\u62e9\u4e24\u79cd\u65b9\u5f0f\u7684\u6700\u4f73\u5b9e\u8df5\u3002 \u6709\u4e24\u4e2a\u5173\u952e\u70b9\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u6309\u503c\u4f20\u9012\uff0c\u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u6309\u5f15\u7528\u4f20\u9012\uff1a \u9996\u5148\uff0c\u5bf9\u8c61\u62f7\u8d1d\u7684\u5f00\u9500\u5f88\u5927\u7a0b\u5ea6\u4e0a\u53d6\u51b3\u4e8e\u4e24\u65b9\u9762\uff1a \u5bf9\u8c61\u7684\u5927\u5c0f\u3002\u5bf9\u8c61\u5360\u7528\u8d8a\u591a\u7684\u5185\u5b58\uff0c\u62f7\u8d1d\u4e5f\u5c31\u8d8a\u8d39\u65f6\u95f4\uff1b \u5176\u4ed6\u989d\u5916\u8bbe\u7f6e\u6240\u9700\u7684\u65f6\u95f4\u3002\u6709\u4e9b\u7c7b\u4f1a\u5728\u5b9e\u4f8b\u5316\u65f6\u8fdb\u884c\u989d\u5916\u7684\u8bbe\u7f6e\u64cd\u4f5c\uff08\u4f8b\u5982\u6253\u5f00\u6587\u4ef6\u6216\u6570\u636e\u5e93\u3001\u6216\u8005\u5206\u914d\u52a8\u6001\u5185\u5b58\uff09\u3002\u8fd9\u4e9b\u64cd\u4f5c\u65e0\u7591\u4e5f\u4f1a\u5728\u5bf9\u8c61\u62f7\u8d1d\u65f6\u5360\u636e\u4e00\u5b9a\u7684\u65f6\u95f4\u3002 \u53e6\u4e00\u65b9\u9762\uff0c\u5f15\u7528\u7684\u7ed1\u5b9a\u603b\u662f\u975e\u5e38\u5feb\u7684\uff08\u51e0\u4e4e\u548c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u62f7\u8d1d\u4e00\u6837\u5feb\uff09\u3002 \u5176\u6b21\uff0c\u901a\u8fc7\u5f15\u7528\u8bbf\u95ee\u5bf9\u8c61\u6bd4\u76f4\u63a5\u8bbf\u95ee\u5bf9\u8c61\u7684\u901f\u5ea6\u7565\u6162\u3002\u4f7f\u7528\u53d8\u91cf\u6807\u8bc6\u7b26\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u8be5\u53d8\u91cf\u5bf9\u5e94\u7684\u5185\u5b58\u5730\u5740\u4ee5\u83b7\u53d6\u503c\u3002\u5bf9\u4e8e\u5f15\u7528\uff0c\u901a\u5e38\u6709\u4e00\u4e2a\u989d\u5916\u7684\u6b65\u9aa4\uff1a\u7f16\u8bd1\u5668\u5fc5\u987b\u9996\u5148\u786e\u5b9a\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\uff0c\u53ea\u6709\u8fd9\u6837\u5b83\u624d\u80fd\u5230\u8be5\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u5e76\u8bbf\u95ee\u8be5\u503c\u3002\u6b64\u5916\uff0c\u7f16\u8bd1\u5668\u5728\u4f18\u5316\u4ee3\u7801\u65f6\uff0c\u6709\u65f6\u5019\u9488\u5bf9\u6309\u503c\u4f20\u9012\u7684\u8fdb\u884c\u7684\u4f18\u5316\u4f1a\u76f8\u5bf9\u4e8e\u6309\u5f15\u7528\u4f20\u9012\u7684\u4ee3\u7801\u66f4\u9ad8\u6548\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u7f16\u8bd1\u5668\u751f\u6210\u7684\u5bf9\u8c61\u6309\u5f15\u7528\u4f20\u9012\u4ee3\u7801\u901a\u5e38\u6bd4\u5176\u751f\u6210\u7684\u5bf9\u8c61\u6309\u503c\u4f20\u9012\u4ee3\u7801\u8981\u6162\u4e00\u4e9b\u3002 \u6211\u4eec\u73b0\u5728\u53ef\u4ee5\u56de\u7b54\u4e3a\u4ec0\u4e48\u4e0d\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u6240\u6709\u5185\u5bb9\u7684\u95ee\u9898\u4e86: \u5bf9\u4e8e\u62f7\u8d1d\u5f00\u9500\u4e0d\u5927\u7684\u5bf9\u8c61\uff0c\u62f7\u8d1d\u4f20\u9012\u7684\u5f00\u9500\u548c\u5f15\u7528\u7ed1\u5b9a\u7684\u5f00\u9500\u53ef\u80fd\u5dee\u4e0d\u591a\uff0c\u6240\u4ee5\u5e94\u8be5\u4f7f\u7528\u6309\u503c\u4f20\u9012\uff0c\u4ee5\u4fbf\u7f16\u8bd1\u5668\u751f\u6210\u66f4\u9ad8\u6548\u7684\u4ee3\u7801\uff1b \u5bf9\u4e8e\u62f7\u8d1d\u5f00\u9500\u5f88\u5927\u7684\u5bf9\u8c61\uff0c\u62f7\u8d1d\u5f00\u9500\u5360\u636e\u4e86\u5927\u5934\uff0c\u6240\u4ee5\u5e94\u8be5\u4f7f\u7528\u6309\u503c\u4f20\u9012\uff08const\uff09\u6765\u907f\u514d\u62f7\u8d1d\u3002 \u8bd1\u8005\u6ce8 \u6309\u5f15\u7528\u4f20\u9012\u5141\u8bb8\u4fee\u6539\u503c\u5e76\u4e0d\u662f\u539f\u56e0\uff0c\u56e0\u4e3a\u5982\u679c\u4e0d\u5e0c\u671b\u4fee\u6539\u503c\u53ef\u4ee5\u4f20\u9012const\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u66f4\u4e0d\u7528\u8bf4const\u7c7b\u578b\u7684\u5f15\u7528\u6709\u66f4\u597d\u7684\u6cdb\u7528\u6027\u2014\u2014\u80fd\u914d\u5408\u53f3\u503c\u4f7f\u7528 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u4e8e\u62f7\u8d1d\u5f00\u9500\u5c0f\u7684\u5bf9\u8c61\uff0c\u4f7f\u7528\u6309\u503c\u4f20\u9012\uff0c\u5bf9\u4e8e\u62f7\u8d1d\u5f00\u9500\u5927\u7684\u5bf9\u8c61\uff0c\u4f7f\u7528\u6309const\u5f15\u7528\u4f20\u9012\u3002\u5982\u679c\u4e0d\u786e\u5b9a\u5f00\u9500\u5927\u8fd8\u662f\u5c0f\uff0c\u5219\u4f7f\u7528\u6309const\u5f15\u7528\u4f20\u9012\u3002 \u6700\u540e\u4e00\u4e2a\u95ee\u9898\uff0c\u201c\u62f7\u8d1d\u5f00\u9500\u5c0f\u201d\u662f\u5982\u4f55\u8861\u91cf\u7684\uff1f\u8fd9\u4e2a\u95ee\u9898\u6ca1\u6709\u7edd\u5bf9\u7684\u7b54\u6848\uff0c\u8fd9\u53d6\u51b3\u4e8e\u7f16\u8bd1\u5668\u3001\u4f7f\u7528\u573a\u666f\u548c\u67b6\u6784\u3002\u4f46\u662f\uff0c\u7ecf\u9a8c\u6cd5\u5219\u662f\u8fd9\u6837\u7684\uff1a\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u4f7f\u7528\u7684\u5185\u5b58\u4e0d\u8d85\u8fc7\u4e24\u4e2a \u5b57\u957f \uff08\u201d\u5b57\u957f\u201c\u5927\u7ea6\u662f\u5185\u5b58\u5730\u5740\u7684\u957f\u5ea6\uff09\uff0c\u800c\u4e14\u6ca1\u6709\u989d\u5916\u7684\u8bbe\u7f6e\u8fc7\u7a0b\uff0c\u5219\u53ef\u4ee5\u8ba4\u4e3a\u662f\u201d\u62f7\u8d1d\u5f00\u9500\u5c0f\u201c\u3002 \u4e0b\u9762\u8fd9\u6bb5\u7a0b\u5e8f\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53ef\u4ee5\u7528\u4e8e\u5224\u65ad\u5bf9\u8c61\u4f7f\u7528\u5185\u5b58\u662f\u5426\u5927\u4e8e\u4e24\u4e2a\u5b57\u957f\uff08 isSmall \uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include // \u5982\u679c\u7c7b\u578b\u6216\u5bf9\u8c61\u4f7f\u7528\u7684\u5806\u5185\u5b58\u4e0d\u8d85\u8fc7\u4e24\u4e2a\u5b57\u957f\u5219\u8fd4\u56detrue #define isSmall(T) (sizeof(T) <= 2 * sizeof(void*)) struct S { double a , b , c ; }; int main () { std :: cout << std :: boolalpha ; // \u5c06bool\u7c7b\u578b\u6253\u5370\u4e3atrue\u6216false\uff0c\u800c\u4e0d\u662f01 std :: cout << isSmall ( int ) << '\\n' ; // true std :: cout << isSmall ( double ) << '\\n' ; // true std :: cout << isSmall ( S ) << '\\n' ; // false return 0 ; } \u9898\u5916\u8bdd \u6211\u4eec\u5728\u8fd9\u91cc\u4f7f\u7528\u4e86\u4e00\u4e2a\u9884\u5904\u7406\u5668\u5b8f\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u7c7b\u578b\u4e2d\u8fdb\u884c\u66ff\u6362(\u666e\u901a\u51fd\u6570\u4e0d\u5141\u8bb8\u8fd9\u6837\u505a)\u3002 \u7136\u800c\uff0c\u5f88\u96be\u77e5\u9053\u7c7b\u7c7b\u578b\u5bf9\u8c61\u662f\u5426\u5177\u6709\u8bbe\u7f6e\u6210\u672c\u3002\u6700\u597d\u5047\u8bbe\u5927\u591a\u6570\u6807\u51c6\u5e93\u7c7b\u90fd\u6709\u8bbe\u7f6e\u6210\u672c\uff0c\u9664\u975e\u4f60\u77e5\u9053\u5b83\u4eec\u6ca1\u6709\u3002 \u5c0f\u8d34\u58eb \u5982\u679c sizeof(T) <= 2 * sizeof(void*) \u800c\u4e14\u7c7b\u578bT\u6ca1\u6709\u989d\u5916\u7684\u8bbe\u7f6e\u64cd\u4f5c\uff0c\u5219\u53ef\u4ee5\u8ba4\u4e3aT\u7684\u62f7\u8d1d\u5f00\u9500\u5c0f\u3002 \u62f7\u8d1d\u5f00\u9500\u5c0f\u7684\u7c7b\u578b\u901a\u5e38\u6709\uff1a\u6240\u6709\u57fa\u672c\u7c7b\u578b\u3001\u679a\u4e3e\u7c7b\u578b\u548c std::string_view . \u62f7\u8d1d\u5f00\u9500\u5927\u7684\u7c7b\u578b\u901a\u5e38\u6709\uff1a std::array , std::string , std::vector , \u548c std::ostream .","title":"9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528"},{"location":"9-5-Pass-by-lvalue-reference/#_1","text":"\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u591a\u6570\u6570\u636e\u7c7b\u578b\u90fd\u5c5e\u4e8e\u7c7b(\u4f8b\u5982 std::string )\u3002\u7c7b\u7c7b\u578b\u7684\u62f7\u8d1d\u5f00\u9500\u901a\u5e38\u662f\u5f88\u5927\u7684\u3002\u5982\u679c\u53ef\u80fd\uff0c\u5e94\u8be5\u5c3d\u53ef\u80fd\u5730\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\uff0c\u5c24\u5176\u662f\u5728\u8fd9\u4e9b\u53d8\u91cf\u7528\u5b8c\u5373\u5f03\u7684\u60c5\u51b5\u4e0b\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u6b63\u662f\u8868\u660e\u4e86\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include void printValue ( std :: string y ) { std :: cout << y << '\\n' ; } // y is destroyed here int main () { std :: string x { \"Hello, world!\" }; // x is a std::string printValue ( x ); // x is passed by value (copied) into parameter y (expensive) return 0 ; } \u7a0b\u5e8f\u4f1a\u6253\u5370\uff1a 1 Hello, world! \u5c3d\u7ba1\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u548c\u6211\u4eec\u60f3\u7684\u4e00\u6837\uff0c\u4f46\u5176\u6548\u7387\u662f\u5f88\u4f4e\u7684\u3002\u548c\u524d\u9762\u7684\u7a0b\u5e8f\u4e00\u6837\u5f53\u6211\u4eec\u8c03\u7528 printValue() \u7684\u65f6\u5019\uff0c\u5b9e\u53c2 x \u4f1a\u88ab\u62f7\u8d1d\u5230\u5f62\u53c2 y \u3002\u4e0d\u8fc7\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b9e\u53c2 std::string \u5e76\u4e0d\u50cf int \u90a3\u6837\uff0c std::string \u662f\u4e00\u79cd\u7c7b\u7c7b\u578b\uff0c\u5bf9\u5b83\u7684\u62f7\u8d1d\u5f00\u9500\u662f\u975e\u5e38\u5927\u7684\uff0c\u66f4\u4e0d\u7528\u8bf4\u6bcf\u6b21\u8c03\u7528 printValue() \u90fd\u4f1a\u4ea7\u751f\u8fd9\u6837\u7684\u5f00\u9500\u3002 \u6211\u4eec\u53ef\u4ee5\u505a\u7684\u66f4\u597d\u3002","title":"\u6709\u4e9b\u5bf9\u8c61\u7684\u62f7\u8d1d\u5f00\u9500\u5f88\u5927"},{"location":"9-5-Pass-by-lvalue-reference/#_2","text":"\u907f\u514d\u51fd\u6570\u8c03\u7528\u65f6\u4ea7\u751f\u7684\u62f7\u8d1d\u5f00\u9500\uff0c\u53ef\u901a\u8fc7 \u6309\u5f15\u7528\u4f20\u9012 \u6765\u4ee3\u66ff \u6309\u503c\u4f20\u9012 \u3002\u5f53\u4f7f\u7528\u6309\u5f15\u7528\u4f20\u9012\u65f6\uff0c\u6211\u4eec\u5c06\u51fd\u6570\u7684\u5f62\u53c2\u58f0\u660e\u4e3a\u5f15\u7528\u7c7b\u578b\uff08\u6216\u6307\u5411const\u7684\u5f15\u7528\uff09\u800c\u4e0d\u662f\u666e\u901a\u7c7b\u578b\u3002\u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u6bcf\u4e2a\u5f15\u7528\u7c7b\u578b\u7684\u5f62\u53c2\u4f1a\u88ab\u7ed1\u5b9a\u5230\u4f20\u5165\u7684\u5b9e\u53c2\u3002\u56e0\u4e3a\u5f15\u7528\u5176\u5b9e\u662f\u5b9e\u53c2\u7684\u522b\u540d\uff0c\u6240\u4ee5\u5e76\u4e0d\u4f1a\u521b\u5efa\u62f7\u8d1d\u3002 \u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u548c\u4e0a\u9762\u7684\u4f8b\u5b50\u662f\u4e00\u81f4\u7684\uff0c\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\u5176\u4f7f\u7528\u4e86\u6309\u5f15\u7528\u4f20\u9012\u800c\u4e0d\u662f\u6309\u503c\u51fa\u4f20\u9012\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include void printValue ( std :: string & y ) // type changed to std::string& { std :: cout << y << '\\n' ; } // y is destroyed here int main () { std :: string x { \"Hello, world!\" }; printValue ( x ); // x is now passed by reference into reference parameter y (inexpensive) return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u548c\u4e0a\u9762\u7684\u7a0b\u5e8f\u6bcf\u592a\u5927\u533a\u522b\uff0c\u9664\u4e86\u5f62\u53c2 y \u7684\u7c7b\u578b\u4ece std::string \u88ab\u6539\u4e3a std::string& (\u5de6\u503c\u5f15\u7528)\u3002\u73b0\u5728\uff0c\u5f53 printValue(x) \u88ab\u8c03\u7528\u7684\u65f6\u5019\uff0c\u5de6\u503c\u5f15\u7528\u7c7b\u578b\u7684\u5f62\u53c2 y \u88ab\u7ed1\u5b9a\u5230\u5b9e\u53c2 x \u3002\u5f15\u7528\u7684\u7ed1\u5b9a\u5f00\u9500\u603b\u662f\u5f88\u5c0f\u7684\uff0c\u5b83\u4e0d\u9700\u8981\u521b\u5efa x \u7684\u62f7\u8d1d\u3002\u56e0\u4e3a\u5f15\u7528\u662f\u88ab\u5f15\u7528\u5bf9\u8c61\u7684\u522b\u540d\uff0c\u6240\u4ee5\u5f53 printValue() \u4f7f\u7528 y \u65f6\uff0c\u5b83\u8bbf\u95ee\u7684\u662f\u5b9e\u53c2 x \u672c\u8eab\uff08\u800c\u4e0d\u662f x \u7684\u62f7\u8d1d\uff09\u3002 \u5173\u952e\u4fe1\u606f \u6309\u5f15\u7528\u4f20\u9012\u5141\u8bb8\u6211\u4eec\u5411\u51fd\u6570\u4f20\u9012\u53c2\u6570\u800c\u4e0d\u9700\u521b\u5efa\u62f7\u8d1d\u3002","title":"\u6309\u5f15\u7528\u4f20\u9012"},{"location":"9-5-Pass-by-lvalue-reference/#_3","text":"\u5f53\u4e00\u4e2a\u5bf9\u8c61\u901a\u8fc7\u6309\u503c\u4f20\u9012\u7684\u65b9\u5f0f\u4f20\u5165\u51fd\u6570\u65f6\uff0c\u51fd\u6570\u63a5\u6536\u5230\u7684\u662f\u5176\u62f7\u8d1d\u3002\u8fd9\u4e5f\u5c31\u610f\u5473\u7740\u4efb\u4f55\u5bf9\u5f62\u53c2\u7684\u4fee\u6539\u90fd\u662f\u5bf9\u8fd9\u4efd\u62f7\u8d1d\u7684\u4fee\u6539\uff0c\u800c\u4e0d\u662f\u5b9e\u53c2\u672c\u8eab\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void addOne ( int y ) // y is a copy of x { ++ y ; // this modifies the copy of x, not the actual object x } int main () { int x { 5 }; std :: cout << \"value = \" << x << '\\n' ; addOne ( x ); std :: cout << \"value = \" << x << '\\n' ; // x has not been modified return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u5f62\u53c2 y \u662f\u5b9e\u53c2 x \u7684\u62f7\u8d1d\uff0c\u5f53\u6211\u4eec\u5bf9 y \u8fdb\u884c\u9012\u589e\u65f6\uff0c\u5b83\u53ea\u4f1a\u5f71\u54cd\u5230 y \uff0c\u56e0\u6b64\u7a0b\u5e8f\u7684\u8f93\u51fa\u4e3a\uff1a 1 2 value = 5 value = 5 \u7136\u800c\uff0c\u7531\u4e8e\u5f15\u7528\u6765\u8bf4\uff0c\u56e0\u4e3a\u5b83\u5c31\u662f\u88ab\u5f15\u7528\u5bf9\u8c61\u672c\u8eab\uff0c\u5f53\u4f7f\u7528\u6309\u5f15\u7528\u4f20\u9012\u65f6\uff0c\u5bf9\u5f15\u7528\u5f62\u53c2\u7684\u4efb\u4f55\u66f4\u6539\u90fd\u5c06\u5f71\u54cd\u5b9e\u53c2\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void addOne ( int & y ) // y is bound to the actual object x { ++ y ; // this modifies the actual object x } int main () { int x { 5 }; std :: cout << \"value = \" << x << '\\n' ; addOne ( x ); std :: cout << \"value = \" << x << '\\n' ; // x has been modified return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u4e3a\uff1a 1 2 value = 5 value = 6 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c x \u9996\u5148\u88ab\u521d\u59cb\u5316\u4e3a 5\u3002\u5f53 addOne(x) \u88ab\u8c03\u7528\u65f6\uff0c\u5f15\u7528\u7c7b\u578b\u5f62\u53c2 y \u4f1a\u88ab\u7ed1\u5b9a\u5230\u5b9e\u53c2 x \u3002\u5f53\u6211\u4eec\u5728 addOne() \u51fd\u6570\u4e2d\u5bf9 y \u8fdb\u884c\u9012\u589e\u65f6\uff0c\u5b83\u5b9e\u9645\u4fee\u6539\u4e86\u5b9e\u53c2 x \u7684\u503c\uff08\u800c\u4e0d\u662f\u5b83\u7684\u62f7\u8d1d\uff09\u3002\u56e0\u6b64\u5f53 addOne() \u8c03\u7528\u7ed3\u675f\u540e\u53d8\u91cf\u7684\u503c\u4ecd\u7136\u662f\u88ab\u4fee\u6539\u4e86\u3002 \u5173\u952e\u4fe1\u606f \u901a\u8fc7\u6309\u503c\u4f20\u9012\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u4fee\u6539\u4f20\u5165\u7684\u5b9e\u53c2\u3002 \u51fd\u6570\u4fee\u6539\u4f20\u5165\u53c2\u6570\u503c\u7684\u80fd\u529b\u662f\u5f88\u6709\u7528\u7684\u3002\u5047\u8bbe\u4f60\u7f16\u5199\u4e86\u4e00\u4e2a\u51fd\u6570\u6765\u5224\u65ad\u602a\u7269\u662f\u5426\u6210\u529f\u653b\u51fb\u4e86\u73a9\u5bb6\u3002\u5982\u679c\u662f\u8fd9\u6837\uff0c\u602a\u7269\u5e94\u8be5\u5bf9\u73a9\u5bb6\u7684\u751f\u547d\u503c\u9020\u6210\u4e00\u5b9a\u7684\u4f24\u5bb3\u3002\u5982\u679c\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u73a9\u5bb6\u5bf9\u8c61\uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539\u4f20\u5165\u7684\u5b9e\u9645\u73a9\u5bb6\u5bf9\u8c61\u7684\u5065\u5eb7\u72b6\u51b5\u3002\u5982\u679c\u4f60\u901a\u8fc7\u503c\u4f20\u9012\u73a9\u5bb6\u5bf9\u8c61\uff0c\u4f60\u53ea\u80fd\u4fee\u6539\u73a9\u5bb6\u5bf9\u8c61\u526f\u672c\u7684\u5065\u5eb7\u503c\uff0c\u8fd9\u5c31\u6ca1\u4ec0\u4e48\u7528\u4e86\u3002","title":"\u6309\u5f15\u7528\u4f20\u9012\u5141\u8bb8\u51fd\u6570\u4fee\u6539\u5b9e\u53c2\u7684\u503c"},{"location":"9-5-Pass-by-lvalue-reference/#const","text":"\u56e0\u4e3a\u6267\u884c\u975econst\u503c\u7684\u5f15\u7528\u53ea\u80fd\u7ed1\u5b9a\u5230\u4e00\u4e2a\u53ef\u4fee\u6539\u7684 \u5de6\u503c \uff08\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u975econst\u7c7b\u578b\u53d8\u91cf\uff09\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u6309\u5f15\u7528\u4f20\u9012\u53ea\u80fd\u914d\u5408\u53ef\u4fee\u6539\u5de6\u503c\u6765\u4f7f\u7528\u3002\u4ece\u5b9e\u7528\u6027\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u8fd9\u65e0\u7591\u6781\u5927\u5730\u9650\u5236\u4e86\u6309\u503c\u4f20\u9012\u7684\u5b9e\u7528\u6027\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u80fd\u591f\u4e3a\u5176\u4f20\u9012const\u53d8\u91cf\u548c \u5b57\u9762\u91cf \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include void printValue ( int & y ) // y only accepts modifiable lvalues { std :: cout << y << '\\n' ; } int main () { int x { 5 }; printValue ( x ); // ok: x is a modifiable lvalue const int z { 5 }; printValue ( z ); // error: z is a non-modifiable lvalue printValue ( 5 ); // error: 5 is an rvalue return 0 ; } \u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u6709\u66f4\u597d\u7684\u529e\u6cd5\u3002","title":"\u4f20\u9012\u6307\u5411\u975econst\u7c7b\u578b\u7684\u5f15\u7528\u65f6\u53ea\u80fd\u63a5\u6536\u53ef\u4ee5\u4fee\u6539\u7684\u5de6\u503c\u5b9e\u53c2"},{"location":"9-5-Pass-by-lvalue-reference/#const_1","text":"\u4e0d\u540c\u4e8e\u6307\u5411\u975econst\u7684\u5f15\u7528\uff08\u53ea\u80fd\u7ed1\u5b9a\u53ef\u4fee\u6539\u5de6\u503c\uff09\uff0cconst\u7c7b\u578b\u5f15\u7528\u53ef\u4ee5\u7ed1\u5b9a\u53ef\u4fee\u6539\u5de6\u503c\u3001\u4e0d\u53ef\u4fee\u6539\u5de6\u503c\u548c\u53f3\u503c\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u628a\u5f62\u53c2\u5b9a\u4e49\u4e3aconst\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u5219\u53ef\u4ee5\u5c06\u5176\u7ed1\u5b9a\u5230\u4efb\u4f55\u7c7b\u578b\u7684\u5b9e\u53c2\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include void printValue ( const int & y ) // y is now a const reference { std :: cout << y << '\\n' ; } int main () { int x { 5 }; printValue ( x ); // ok: x is a modifiable lvalue const int z { 5 }; printValue ( z ); // ok: z is a non-modifiable lvalue printValue ( 5 ); // ok: 5 is a literal rvalue return 0 ; } \u6309const\u5f15\u7528\u4f20\u9012\u63d0\u4f9b\u4e86\u548c\u6309\u5f15\u7528\u4f20\u9012\u4e00\u6837\u7684\u4f18\u70b9\uff08\u907f\u514d\u5b9e\u53c2\u62f7\u8d1d\uff09\uff0c\u540c\u65f6\u8fd8\u53ef\u4ee5\u907f\u514d\u51fd\u6570\u5bf9\u5b9e\u53c2\u8fdb\u884c\u4fee\u6539\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u662f\u65e0\u6cd5\u8fdb\u884c\u7684\uff0c\u56e0\u4e3a ref \u662f const \u7684\uff1a 1 2 3 4 void addOne ( const int & ref ) { ++ ref ; // not allowed: ref is const } \u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5176\u5b9e\u5e76\u4e0d\u5e0c\u671b\u51fd\u6570\u7684\u5b9e\u53c2\u88ab\u4fee\u6539\u3002 \u6700\u4f73\u5b9e\u8df5 \u6309\u5f15\u7528\u4f20\u9012\u6700\u597d\u5b9a\u4e49\u4e3aconst\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u9664\u975e\u4f60\u6709\u5f88\u597d\u7684\u7406\u7531\uff08\u4f8b\u5982\u51fd\u6570\u5fc5\u987b\u4fee\u6539\u5b9e\u53c2\u7684\u503c\uff09\u3002 \u73b0\u5728\uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u53ef\u4ee5\u4e86\u89e3\u652f\u6301const\u7c7b\u578b\u5de6\u503c\u5f15\u7528\u7684\u52a8\u673a\u4e86\uff0c\u5982\u679c\u6ca1\u6709\u5b83\uff0c\u6211\u4eec\u5c31\u65e0\u6cd5\u4f7f\u7528\u6309\u5f15\u7528\u4f20\u9012\u7684\u51fd\u6570\u4f20\u9012\u5b57\u9762\u91cf\uff08\u6216\u5176\u4ed6\u53f3\u503c\uff09\u3002","title":"\u4f20\u9012\u6307\u5411 const \u7684\u5f15\u7528"},{"location":"9-5-Pass-by-lvalue-reference/#_4","text":"\u5177\u6709\u591a\u4e2a\u5f62\u53c2\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u5206\u522b\u6307\u5b9a\u6bcf\u4e2a\u53c2\u6570\u662f\u6309\u503c\u4f20\u9012\u8fd8\u662f\u6309\u5f15\u7528\u4f20\u9012\uff1a \u4f8b\u5982\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include void foo ( int a , int & b , const std :: string & c ) { } int main () { int x { 5 }; const std :: string s { \"Hello, world!\" }; foo ( 5 , x , s ); return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7b2c\u4e00\u4e2a\u5b9e\u53c2\u662f\u6309\u503c\u4f20\u9012\u7684\uff0c\u800c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5219\u662f\u6309\u5f15\u7528\u4f20\u9012\u7684\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570\u5219\u662f\u6309const\u5f15\u7528\u4f20\u9012\u7684\u3002","title":"\u540c\u65f6\u4f7f\u7528\u6309\u503c\u4f20\u9012\u548c\u6309\u5f15\u7528\u4f20\u9012"},{"location":"9-5-Pass-by-lvalue-reference/#_5","text":"\u56e0\u4e3a\u7c7b\u7c7b\u578b\u7684\u62f7\u8d1d\u5f00\u9500\u5f88\u5927\uff0c\u7c7b\u7c7b\u578b\u901a\u5e38\u4f1a\u6309\u5f15\u7528\u4f20\u9012\u800c\u4e0d\u662f\u6309\u503c\u4f20\u9012\u4ee5\u907f\u514d\u6602\u8d35\u7684\u62f7\u8d1d\u5f00\u9500\u3002\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u62f7\u8d1d\u5f00\u9500\u662f\u5f88\u5c0f\u7684\uff0c\u6240\u4ee5\u5b83\u4eec\u901a\u5e38\u4f1a\u6309\u503c\u4f20\u9012\u3002 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4f7f\u7528 \u6309\u503c\u4f20\u9012 \uff0c\u800c\u5bf9\u4e8e\u7c7b\u6216\u8005\u7ed3\u6784\u4f53\u4f7f\u7528const\u7684\u6309\u503c\u4f20\u9012\u3002","title":"\u4f55\u65f6\u4f7f\u7528\u6309\u5f15\u7528\u4f20\u9012"},{"location":"9-5-Pass-by-lvalue-reference/#_6","text":"\u5176\u5b9e\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u7684\u7c7b\u7c7b\u578b\u90fd\u5fc5\u987b \u6309\u503c\u4f20\u9012 \u3002\u5f53\u7136\u4f60\u53ef\u80fd\u4f1a\u95ee\uff0c\u90fd\u6309\u503c\u4f20\u9012\u6709\u4f55\u4e0d\u53ef\u5462\uff1f\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u4f1a\u8be6\u7ec6\u5730\u5206\u6790\u6309\u503c\u4f20\u9012\u548c\u6309\u5f15\u7528\u4f20\u9012\u7684\u5f00\u9500\uff0c\u5e76\u63d0\u51fa\u5982\u4f55\u9009\u62e9\u4e24\u79cd\u65b9\u5f0f\u7684\u6700\u4f73\u5b9e\u8df5\u3002 \u6709\u4e24\u4e2a\u5173\u952e\u70b9\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u6309\u503c\u4f20\u9012\uff0c\u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u6309\u5f15\u7528\u4f20\u9012\uff1a \u9996\u5148\uff0c\u5bf9\u8c61\u62f7\u8d1d\u7684\u5f00\u9500\u5f88\u5927\u7a0b\u5ea6\u4e0a\u53d6\u51b3\u4e8e\u4e24\u65b9\u9762\uff1a \u5bf9\u8c61\u7684\u5927\u5c0f\u3002\u5bf9\u8c61\u5360\u7528\u8d8a\u591a\u7684\u5185\u5b58\uff0c\u62f7\u8d1d\u4e5f\u5c31\u8d8a\u8d39\u65f6\u95f4\uff1b \u5176\u4ed6\u989d\u5916\u8bbe\u7f6e\u6240\u9700\u7684\u65f6\u95f4\u3002\u6709\u4e9b\u7c7b\u4f1a\u5728\u5b9e\u4f8b\u5316\u65f6\u8fdb\u884c\u989d\u5916\u7684\u8bbe\u7f6e\u64cd\u4f5c\uff08\u4f8b\u5982\u6253\u5f00\u6587\u4ef6\u6216\u6570\u636e\u5e93\u3001\u6216\u8005\u5206\u914d\u52a8\u6001\u5185\u5b58\uff09\u3002\u8fd9\u4e9b\u64cd\u4f5c\u65e0\u7591\u4e5f\u4f1a\u5728\u5bf9\u8c61\u62f7\u8d1d\u65f6\u5360\u636e\u4e00\u5b9a\u7684\u65f6\u95f4\u3002 \u53e6\u4e00\u65b9\u9762\uff0c\u5f15\u7528\u7684\u7ed1\u5b9a\u603b\u662f\u975e\u5e38\u5feb\u7684\uff08\u51e0\u4e4e\u548c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u62f7\u8d1d\u4e00\u6837\u5feb\uff09\u3002 \u5176\u6b21\uff0c\u901a\u8fc7\u5f15\u7528\u8bbf\u95ee\u5bf9\u8c61\u6bd4\u76f4\u63a5\u8bbf\u95ee\u5bf9\u8c61\u7684\u901f\u5ea6\u7565\u6162\u3002\u4f7f\u7528\u53d8\u91cf\u6807\u8bc6\u7b26\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u8be5\u53d8\u91cf\u5bf9\u5e94\u7684\u5185\u5b58\u5730\u5740\u4ee5\u83b7\u53d6\u503c\u3002\u5bf9\u4e8e\u5f15\u7528\uff0c\u901a\u5e38\u6709\u4e00\u4e2a\u989d\u5916\u7684\u6b65\u9aa4\uff1a\u7f16\u8bd1\u5668\u5fc5\u987b\u9996\u5148\u786e\u5b9a\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\uff0c\u53ea\u6709\u8fd9\u6837\u5b83\u624d\u80fd\u5230\u8be5\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u5e76\u8bbf\u95ee\u8be5\u503c\u3002\u6b64\u5916\uff0c\u7f16\u8bd1\u5668\u5728\u4f18\u5316\u4ee3\u7801\u65f6\uff0c\u6709\u65f6\u5019\u9488\u5bf9\u6309\u503c\u4f20\u9012\u7684\u8fdb\u884c\u7684\u4f18\u5316\u4f1a\u76f8\u5bf9\u4e8e\u6309\u5f15\u7528\u4f20\u9012\u7684\u4ee3\u7801\u66f4\u9ad8\u6548\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u7f16\u8bd1\u5668\u751f\u6210\u7684\u5bf9\u8c61\u6309\u5f15\u7528\u4f20\u9012\u4ee3\u7801\u901a\u5e38\u6bd4\u5176\u751f\u6210\u7684\u5bf9\u8c61\u6309\u503c\u4f20\u9012\u4ee3\u7801\u8981\u6162\u4e00\u4e9b\u3002 \u6211\u4eec\u73b0\u5728\u53ef\u4ee5\u56de\u7b54\u4e3a\u4ec0\u4e48\u4e0d\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u6240\u6709\u5185\u5bb9\u7684\u95ee\u9898\u4e86: \u5bf9\u4e8e\u62f7\u8d1d\u5f00\u9500\u4e0d\u5927\u7684\u5bf9\u8c61\uff0c\u62f7\u8d1d\u4f20\u9012\u7684\u5f00\u9500\u548c\u5f15\u7528\u7ed1\u5b9a\u7684\u5f00\u9500\u53ef\u80fd\u5dee\u4e0d\u591a\uff0c\u6240\u4ee5\u5e94\u8be5\u4f7f\u7528\u6309\u503c\u4f20\u9012\uff0c\u4ee5\u4fbf\u7f16\u8bd1\u5668\u751f\u6210\u66f4\u9ad8\u6548\u7684\u4ee3\u7801\uff1b \u5bf9\u4e8e\u62f7\u8d1d\u5f00\u9500\u5f88\u5927\u7684\u5bf9\u8c61\uff0c\u62f7\u8d1d\u5f00\u9500\u5360\u636e\u4e86\u5927\u5934\uff0c\u6240\u4ee5\u5e94\u8be5\u4f7f\u7528\u6309\u503c\u4f20\u9012\uff08const\uff09\u6765\u907f\u514d\u62f7\u8d1d\u3002 \u8bd1\u8005\u6ce8 \u6309\u5f15\u7528\u4f20\u9012\u5141\u8bb8\u4fee\u6539\u503c\u5e76\u4e0d\u662f\u539f\u56e0\uff0c\u56e0\u4e3a\u5982\u679c\u4e0d\u5e0c\u671b\u4fee\u6539\u503c\u53ef\u4ee5\u4f20\u9012const\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u66f4\u4e0d\u7528\u8bf4const\u7c7b\u578b\u7684\u5f15\u7528\u6709\u66f4\u597d\u7684\u6cdb\u7528\u6027\u2014\u2014\u80fd\u914d\u5408\u53f3\u503c\u4f7f\u7528 \u6700\u4f73\u5b9e\u8df5 \u5bf9\u4e8e\u62f7\u8d1d\u5f00\u9500\u5c0f\u7684\u5bf9\u8c61\uff0c\u4f7f\u7528\u6309\u503c\u4f20\u9012\uff0c\u5bf9\u4e8e\u62f7\u8d1d\u5f00\u9500\u5927\u7684\u5bf9\u8c61\uff0c\u4f7f\u7528\u6309const\u5f15\u7528\u4f20\u9012\u3002\u5982\u679c\u4e0d\u786e\u5b9a\u5f00\u9500\u5927\u8fd8\u662f\u5c0f\uff0c\u5219\u4f7f\u7528\u6309const\u5f15\u7528\u4f20\u9012\u3002 \u6700\u540e\u4e00\u4e2a\u95ee\u9898\uff0c\u201c\u62f7\u8d1d\u5f00\u9500\u5c0f\u201d\u662f\u5982\u4f55\u8861\u91cf\u7684\uff1f\u8fd9\u4e2a\u95ee\u9898\u6ca1\u6709\u7edd\u5bf9\u7684\u7b54\u6848\uff0c\u8fd9\u53d6\u51b3\u4e8e\u7f16\u8bd1\u5668\u3001\u4f7f\u7528\u573a\u666f\u548c\u67b6\u6784\u3002\u4f46\u662f\uff0c\u7ecf\u9a8c\u6cd5\u5219\u662f\u8fd9\u6837\u7684\uff1a\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u4f7f\u7528\u7684\u5185\u5b58\u4e0d\u8d85\u8fc7\u4e24\u4e2a \u5b57\u957f \uff08\u201d\u5b57\u957f\u201c\u5927\u7ea6\u662f\u5185\u5b58\u5730\u5740\u7684\u957f\u5ea6\uff09\uff0c\u800c\u4e14\u6ca1\u6709\u989d\u5916\u7684\u8bbe\u7f6e\u8fc7\u7a0b\uff0c\u5219\u53ef\u4ee5\u8ba4\u4e3a\u662f\u201d\u62f7\u8d1d\u5f00\u9500\u5c0f\u201c\u3002 \u4e0b\u9762\u8fd9\u6bb5\u7a0b\u5e8f\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53ef\u4ee5\u7528\u4e8e\u5224\u65ad\u5bf9\u8c61\u4f7f\u7528\u5185\u5b58\u662f\u5426\u5927\u4e8e\u4e24\u4e2a\u5b57\u957f\uff08 isSmall \uff09\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include // \u5982\u679c\u7c7b\u578b\u6216\u5bf9\u8c61\u4f7f\u7528\u7684\u5806\u5185\u5b58\u4e0d\u8d85\u8fc7\u4e24\u4e2a\u5b57\u957f\u5219\u8fd4\u56detrue #define isSmall(T) (sizeof(T) <= 2 * sizeof(void*)) struct S { double a , b , c ; }; int main () { std :: cout << std :: boolalpha ; // \u5c06bool\u7c7b\u578b\u6253\u5370\u4e3atrue\u6216false\uff0c\u800c\u4e0d\u662f01 std :: cout << isSmall ( int ) << '\\n' ; // true std :: cout << isSmall ( double ) << '\\n' ; // true std :: cout << isSmall ( S ) << '\\n' ; // false return 0 ; } \u9898\u5916\u8bdd \u6211\u4eec\u5728\u8fd9\u91cc\u4f7f\u7528\u4e86\u4e00\u4e2a\u9884\u5904\u7406\u5668\u5b8f\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u7c7b\u578b\u4e2d\u8fdb\u884c\u66ff\u6362(\u666e\u901a\u51fd\u6570\u4e0d\u5141\u8bb8\u8fd9\u6837\u505a)\u3002 \u7136\u800c\uff0c\u5f88\u96be\u77e5\u9053\u7c7b\u7c7b\u578b\u5bf9\u8c61\u662f\u5426\u5177\u6709\u8bbe\u7f6e\u6210\u672c\u3002\u6700\u597d\u5047\u8bbe\u5927\u591a\u6570\u6807\u51c6\u5e93\u7c7b\u90fd\u6709\u8bbe\u7f6e\u6210\u672c\uff0c\u9664\u975e\u4f60\u77e5\u9053\u5b83\u4eec\u6ca1\u6709\u3002 \u5c0f\u8d34\u58eb \u5982\u679c sizeof(T) <= 2 * sizeof(void*) \u800c\u4e14\u7c7b\u578bT\u6ca1\u6709\u989d\u5916\u7684\u8bbe\u7f6e\u64cd\u4f5c\uff0c\u5219\u53ef\u4ee5\u8ba4\u4e3aT\u7684\u62f7\u8d1d\u5f00\u9500\u5c0f\u3002 \u62f7\u8d1d\u5f00\u9500\u5c0f\u7684\u7c7b\u578b\u901a\u5e38\u6709\uff1a\u6240\u6709\u57fa\u672c\u7c7b\u578b\u3001\u679a\u4e3e\u7c7b\u578b\u548c std::string_view . \u62f7\u8d1d\u5f00\u9500\u5927\u7684\u7c7b\u578b\u901a\u5e38\u6709\uff1a std::array , std::string , std::vector , \u548c std::ostream .","title":"\u6309\u503c\u4f20\u9012\u548c\u6309\u5f15\u7528\u4f20\u9012\u7684\u5f00\u9500\u6bd4\u8f83\uff08\u8fdb\u9636\u8bdd\u9898\uff09"},{"location":"9-6-Introduction-to-pointers/","tags":["pointer"],"text":"Key Takeaway \u6307\u9488\u548c\u5f15\u7528\u4e4b\u95f4\u8fd8\u6709\u4e00\u4e9b\u503c\u5f97\u4e00\u63d0\u7684\u533a\u522b\uff1a \u5f15\u7528\u5fc5\u987b\u88ab\u521d\u59cb\u5316\uff0c\u6307\u9488\u5e76\u4e0d\u662f\u5fc5\u987b\u521d\u59cb\u5316\uff08\u4f46\u662f\u4f60\u5e94\u8be5\u521d\u59cb\u5316\uff09\uff1b \u5f15\u7528\u4e0d\u662f\u5bf9\u8c61\uff0c\u6307\u9488\u662f\u5bf9\u8c61\uff1b \u5f15\u7528\u4e0d\u80fd\u88ab\u91cd\u65b0\u8bbe\u7f6e\uff08\u4fee\u6539\u5f15\u7528\u4f7f\u5176\u7ed1\u5b9a\u5230\u5176\u4ed6\u5bf9\u8c61\uff09\uff0c\u6307\u9488\u5219\u53ef\u4ee5\u88ab\u4fee\u6539\u6307\u5411\u5176\u4ed6\u5bf9\u8c61\uff1b \u5f15\u7528\u603b\u662f\u7ed1\u5b9a\u5230\u67d0\u4e2a\u5bf9\u8c61\uff0c\u6307\u9488\u53ef\u4ee5\u4e0d\u6267\u884c\u4efb\u4f55\u5bf9\u8c61\uff1b \u5f15\u7528\u662f\u201d\u5b89\u5168\u201c\u7684\uff08\u9664\u4e86 \u60ac\u5782 \u5f15\u7528\uff09\uff0c\u6307\u9488\u5219\u662f\u5371\u9669\u7684\u3002 \u6307\u9488\u662f C++ \u4e2d\u7684\u5386\u53f2\u9057\u7559\u4ea7\u7269\u4e4b\u4e00\uff0c\u4e5f\u662f\u5f88\u591a\u4eba\u5b66\u4e60C++\u65f6\u5bb9\u6613\u5361\u58f3\u7684\u5730\u65b9\u3002\u4f46\u662f\uff0c\u7a0d\u540e\u4f60\u4f1a\u770b\u5230\uff0c\u5176\u5b9e\u6307\u9488\u5e76\u4e0d\u53ef\u6015\u3002 \u5b9e\u9645\u4e0a\uff0c\u6307\u9488\u7684\u884c\u4e3a\u548c \u5de6\u503c\u5f15\u7528 \u662f\u5f88\u50cf\u7684\u3002\u4f46\u662f\u5728\u6211\u4eec\u7ee7\u7eed\u4ecb\u7ecd\u4e4b\u524d\uff0c\u5148\u6765\u5de6\u4e00\u4e9b\u51c6\u5907\u5de5\u4f5c\u3002 \u76f8\u5173\u5185\u5bb9 \u5982\u679c\u4f60\u8fd8\u4e0d\u719f\u6089\u5de6\u503c\u5f15\u7528\uff0c\u73b0\u5728\u662f\u590d\u4e60\u7684\u597d\u65f6\u673a\u3002 9.3 - \u5de6\u503c\u5f15\u7528 \u3001 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 \u548c 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 \u90fd\u662f\u975e\u5e38\u6709\u5e2e\u52a9\u7684\u6750\u6599\u3002 \u8003\u8651\u4e0b\u4e0b\u9762\u4ee3\u7801\uff1a 1 char x {}; // chars use 1 byte of memory \u8fd9\u884c\u5b9a\u4e49\u5728\u751f\u6210\u4ee3\u7801\u65f6\uff0cRAM\u4e2d\u7684\u4e00\u6bb5\u5185\u5b58\u5c31\u4f1a\u88ab\u6307\u5b9a\u7ed9\u8be5\u5bf9\u8c61\u3002\u5bf9\u4e8e\u8fd9\u4e2a\u4f8b\u5b50\u6765\u8bf4\uff0c\u5047\u8bbe\u53d8\u91cfx\u6240\u7528\u7684\u5185\u5b58\u5730\u5740\u4e3a140\u3002\u90a3\u4e48\u6bcf\u5f53\u6211\u4eec\u5728\u8868\u8fbe\u5f0f\u6216\u8005\u8bed\u53e5\u4e2d\u4f7f\u7528x\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u81ea\u52a8\u5730\u5230\u5185\u5b58\u5730\u5740140\u7684\u5730\u65b9\u53bb\u53d6\u503c\u3002 \u8fd9\u4e48\u505a\u6700\u5927\u7684\u597d\u5904\u662f\u6211\u4eec\u65e0\u9700\u5173\u5fc3\u5185\u5b58\u5730\u5740\u7684\u6307\u5b9a\u548c\u4f7f\u7528\uff0c\u4e5f\u4e0d\u9700\u8981\u5173\u5fc3\u5b58\u653e\u5bf9\u8c61\u503c\u4f7f\u7528\u4e86\u591a\u5c11\u4f60\u6751\u3002\u6211\u4eec\u53ea\u9700\u8981\u901a\u8fc7\u53d8\u91cf\u7684 \u6807\u8bc6\u7b26(identifier) \u4e5f\u5c31\u662f\u53d8\u91cf\u540d\u5373\u53ef\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u5c06\u5176\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u5185\u5b58\u5730\u5740\u3002\u7f16\u8bd1\u5668\u4e3a\u6211\u4eec\u4ee3\u52b3\u4e86\u5168\u90e8\u7684\u5bfb\u5740\u5de5\u4f5c\u3002 \u5bf9\u4e8e\u5f15\u7528\u6765\u8bf4\u4e5f\u662f\u8fd9\u6837\uff1a 1 2 3 4 5 6 7 int main () { char x {}; // assume this is assigned memory address 140 char & ref { x }; // ref is an lvalue reference to x (when used with a type, & means lvalue reference) return 0 ; } \u56e0\u4e3a ref \u662f x \u7684\u522b\u540d\uff0c\u6240\u4ee5\u4efb\u4f55\u4f7f\u7528 ref \u7684\u5730\u65b9\uff0c\u7a0b\u5e8f\u4ecd\u7136\u4f1a\u5230\u5185\u5b58\u5730\u5740140\u7684\u5730\u65b9\u53bb\u53d6\u503c\u3002\u7f16\u8bd1\u5668\u540c\u6837\u4e3a\u6211\u4eec\u4ee3\u52b3\u4e86\u5bfb\u5740\u7684\u8fc7\u7a0b\uff0c\u6211\u4eec\u65e0\u9700\u64cd\u5fc3\u3002 \u53d6\u5730\u5740\u8fd0\u7b97\u7b26 (&) \u5c3d\u7ba1\u5728\u9ed8\u8ba4\u7684\u60c5\u51b5\u4e0b\uff0c\u53d8\u91cf\u7684\u5730\u5740\u5e76\u4e0d\u4f1a\u66b4\u9732\u7ed9\u7528\u6237\uff0c\u4f46\u8be5\u5730\u5740\u5b9e\u9645\u4e0a\u662f\u53ef\u4ee5\u83b7\u53d6\u7684\u3002 \u53d6\u5730\u5740\u64cd\u4f5c\u7b26 & \u5c31\u53ef\u4ee5\u8fd4\u56de\u5176\u64cd\u4f5c\u6570\u7684\u5730\u5740\uff0c\u975e\u5e38\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { int x { 5 }; std :: cout << x << '\\n' ; // print the value of variable x std :: cout << & x << '\\n' ; // print the memory address of variable x return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u8fd4\u56de\u5982\u4e0b\u7ed3\u679c\uff1a 1 2 5 0027FEA0 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u53d6\u5730\u5740\u8fd0\u7b97\u7b26\u6765\u83b7\u53d6\u53d8\u91cfx\u7684\u5730\u5740\u5e76\u5c06\u5176\u6253\u5370\u5904\u7406\u3002\u5185\u5b58\u5730\u5740\u901a\u5e38\u662f\u4ee5\u5341\u516d\u8fdb\u5236\u503c\u7684\u5f62\u5f0f\u6253\u5370\u7684\uff08\u53c2\u8003 4.16 - \u6570\u503c\u7cfb\u7edf\uff08\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236\uff09 \u4e86\u89e3\u5341\u516d\u8fdb\u5236\u7684\u5185\u5bb9\uff09\uff0c\u4f46\u901a\u5e38\u4e0d\u5305\u62ec\u524d\u7f00 0x \u3002 \u5bf9\u4e8e\u4f7f\u7528\u5185\u5b58\u6570\u8d85\u8fc7\u4e00\u4e2a\u5b57\u8282\u7684\u5bf9\u8c61\uff0c\u53d6\u5730\u5740\u4f1a\u8fd4\u56de\u8be5\u6bb5\u5185\u5b58\u7684\u9996\u5730\u5740\uff08\u7b2c\u4e00\u4e2a\u5b57\u8282\uff09\u3002 \u5c0f\u8d34\u58eb & \u7b26\u53f7\u5728\u4e0d\u540c\u7684\u8bed\u5883\u4e0b\u6709\u4e0d\u540c\u7684\u542b\u4e49\uff0c\u6bd4\u8f83\u5bb9\u6613\u6df7\u6dc6\uff1a \u5f53\u540e\u9762\u63a5\u7740\u7c7b\u578b\u540d\u65f6\uff0c&\u8868\u793a\u4e00\u4e2a\u5de6\u503c\u5f15\u7528: int& ref \uff1b \u5f53\u7528\u4e8e\u4e00\u5143\u8868\u8fbe\u5f0f\u65f6\uff0c&\u8868\u793a\u53d6\u5730\u5740\u8fd0\u7b97\u7b26: std::cout << &x . \u5f53\u7528\u4e8e\u4e8c\u5143\u8868\u8fbe\u5f0f\u65f6\uff0c&\u662f \u6309\u4f4d\u4e0e\u64cd\u4f5c : std::cout << x & y . \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 * \u4ec5\u4ec5\u83b7\u5f97\u53d8\u91cf\u7684\u5730\u5740\uff0c\u5f80\u5f80\u4e0d\u662f\u5f88\u6709\u7528\u3002 \u5bf9\u4e8e\u4e00\u4e2a\u5730\u5740\u6765\u8bf4\uff0c\u6700\u6709\u7528\u7684\u64cd\u4f5c\u662f\u83b7\u53d6\u5b83\u5b58\u653e\u7684\u503c\u3002 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \uff08\u4e5f\u53eb\u95f4\u63a5\u8bbf\u95ee\u8fd0\u7b97\u7b26\uff09\uff0c\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u5730\u5740\u5b58\u653e\u7684\u503c\uff08\u4f5c\u4e3a\u5de6\u503c\u8fd4\u56de\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int x { 5 }; std :: cout << x << '\\n' ; // print the value of variable x std :: cout << & x << '\\n' ; // print the memory address of variable x std :: cout << * ( & x ) << '\\n' ; // print the value at the memory address of variable x (parentheses not required, but make it easier to read) return 0 ; } \u5728\u7b14\u8005\u673a\u5668\u4e0a\u8fd4\u56de\u4e0b\u9762\u5185\u5bb9\uff1a 1 2 3 5 0027FEA0 5 \u8fd9\u4e2a\u7a0b\u5e8f\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a\u53d8\u91cfx\u5e76\u4e14\u6253\u5370\u51fa\u5b83\u7684\u503c\u3002\u7136\u540e\u6211\u4eec\u6253\u5370\u51fa\u4e86\u53d8\u91cfx\u7684\u5730\u5740\u3002\u6700\u540e\uff0c\u4f7f\u7528\u89e3\u5f15\u7528\u8fd0\u7b97\u7b26\u4ece\u53d8\u91cfx\u7684\u5730\u5740\u83b7\u53d6\u503c\uff08\u5c31\u662fx\u7684\u503c\uff09\uff0c\u7136\u540e\u6253\u5370\u51fa\u6765\u3002 \u5173\u952e\u4fe1\u606f \u5bf9\u4e8e\u4e00\u4e2a\u7ed9\u5b9a\u5730\u5740\uff0c\u53ef\u4ee5\u4f7f\u7528 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u83b7\u53d6\u8be5\u5730\u5740\u4fdd\u5b58\u7684\u503c\uff08\u5de6\u503c\uff09 \u89e3\u5f15\u7528\u548c\u53d6\u5730\u5740\u662f\u4e00\u5bf9\u76f8\u53cd\u7684\u64cd\u4f5c\uff0c\u53d6\u5730\u5740\u83b7\u53d6\u5bf9\u8c61\u7684\u5730\u5740\uff0c\u89e3\u5f15\u7528\u4ece\u5730\u5740\u83b7\u53d6\u5bf9\u8c61\u3002 \u5c0f\u8d34\u58eb \u5c3d\u7ba1\u89e3\u5f15\u7528\u770b\u8d77\u6765\u5c31\u50cf\u4e58\u6cd5\uff0c\u4f46\u662f\u4f60\u53ef\u4ee5\u533a\u5206\u5b83\u4eec\uff0c\u56e0\u4e3a\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26\u662f\u4e00\u5143\u7684\uff0c\u800c\u4e58\u6cd5\u64cd\u4f5c\u7b26\u662f\u4e8c\u5143\u7684\u3002 \u83b7\u53d6\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\uff0c\u7136\u540e\u7acb\u5373\u89e3\u5f15\u7528\u8be5\u5730\u5740\u4ee5\u83b7\u5f97\u503c\uff0c\u8fd9\u5176\u5b9e\u4e5f\u4e0d\u7b97\u662f\u4ec0\u4e48\u6709\u7528\u7684\u64cd\u4f5c(\u6bd5\u7adf\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u53d8\u91cf\u7684\u503c)\u3002 \u4e0d\u8fc7\uff0c\u5728\u4e86\u89e3\u53d6\u5730\u5740\u548c\u89e3\u5f15\u7528\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb\u4ecb\u7ecd\u6307\u9488\u4e86\u3002 \u6307\u9488 \u6307\u9488\u53ef\u4ee5\u770b\u505a\u662f\u4e00\u4e2a\u6301\u6709\u5185\u5b58\u5730\u5740\u7684\u5bf9\u8c61\uff08\u901a\u5e38\u662f\u5176\u4ed6\u5bf9\u8c61\u7684\u5730\u5740\uff09\u3002\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u5728\u540e\u9762\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528\u8be5\u5730\u5740\u3002 \u9898\u5916\u8bdd \u5728\u73b0\u4ee3 C++ \u4e2d\uff0c\u6211\u4eec\u6b64\u5904\u8c08\u8bba\u7684\u6307\u9488\u88ab\u79f0\u4e3a \u539f\u59cb\u6307\u9488 \u6216 \u7b28\u6307\u9488 \uff0c\u4ee5\u4fbf\u5c06\u5176\u4e0e \u667a\u80fd\u6307\u9488 \u533a\u522b\u5f00\u6765\u3002\u667a\u80fd\u6307\u9488\u4f1a\u5728 chapter M \u4ecb\u7ecd\u3002 \u548c\u4f7f\u7528&\u5b9a\u4e49\u5f15\u7528\u7684\u505a\u6cd5\u7c7b\u4f3c\uff0c\u6307\u9488\u7c7b\u578b\u4f7f\u7528 * \u6765\u5b9a\u4e49\uff1a 1 2 3 4 int ; // a normal int int & ; // an lvalue reference to an int value int * ; // a pointer to an int value (holds the address of an integer value) \u8981\u521b\u5efa\u6307\u9488\u53d8\u91cf\uff0c\u53ea\u9700\u5b9a\u4e49\u4e00\u4e2a\u6307\u9488\u7c7b\u578b\u7684\u53d8\u91cf: 1 2 3 4 5 6 7 8 9 int main () { int x { 5 }; // normal variable int & ref { x }; // a reference to an integer (bound to x) int * ptr ; // a pointer to an integer return 0 ; } \u6ce8\u610f\uff0c\u8fd9\u4e2a\u661f\u53f7\u662f\u6307\u9488\u58f0\u660e\u8bed\u6cd5\u7684\u4e00\u90e8\u5206\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26\u3002 \u6700\u4f73\u5b9e\u8df5 \u58f0\u660e\u6307\u9488\u7c7b\u578b\u65f6\uff0c\u5728\u7c7b\u578b\u540d\u79f0\u65c1\u8fb9\u52a0\u4e0a\u661f\u53f7\u3002 \u6ce8\u610f \u5c3d\u7ba1\u901a\u5e38\u60c5\u51b5\u4e0b\u6211\u4eec\u4e0d\u5e94\u8be5\u5728\u4e00\u884c\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\uff0c\u4f46\u662f\u5982\u679c\u9700\u8981\u8fd9\u4e48\u505a\uff0c\u5219\u6bcf\u4e2a\u6307\u9488\u53d8\u91cf\u524d\u9762\u90fd\u9700\u8981\u661f\u53f7 1 2 int * ptr1 , ptr2 ; // incorrect: ptr1 is a pointer to an int, but ptr2 is just a plain int! int * ptr3 , * ptr4 ; // correct: ptr3 and p4 are both pointers to an int \u867d\u7136\u8fd9\u4e2a\u4f8b\u5b50\u65f6\u5e38\u88ab\u4f5c\u4e3a\u201d\u661f\u53f7\u4e0d\u5e94\u8be5\u4e0e\u7c7b\u578b\u540d\u653e\u5728\u4e00\u8d77\u201c\u7684\u8bba\u636e\uff0c\u4f46\u6211\u4eec\u5012\u4e0d\u5982\u8bf4\u8fd9\u662f\u4e00\u4e2a\u201d\u4e0d\u5e94\u5728\u4e00\u884c\u4e2d\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\u7684\u201c\u4f8b\u5b50\u3002 \u6307\u9488\u7684\u521d\u59cb\u5316 \u548c\u666e\u901a\u7684\u53d8\u91cf\u4e0d\u540c\uff0c \u6307\u9488\u9ed8\u8ba4\u662f\u4e0d\u521d\u59cb\u5316\u7684 \u3002\u4e00\u4e2a\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u7684\u6307\u9488\uff0c\u6709\u65f6\u79f0\u4e3a \u91ce\u6307\u9488 \u3002\u91ce\u6307\u9488\u4e2d\u5b58\u653e\u7684\u662f\u4e00\u4e2a\u65e0\u7528\u7684\u5730\u5740\uff0c\u5bf9\u91ce\u6307\u9488\u89e3\u5f15\u7528\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4e00\u5b9a\u8981\u5c06\u6307\u9488\u521d\u59cb\u5316\u4e3a\u4e00\u4e2a\u5df2\u77e5\u7684\u503c\u3002 \u6700\u4f73\u5b9e\u8df5 \u6307\u9488\u5fc5\u987b\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 10 int main () { int x { 5 }; int * ptr ; // \u4e00\u4e2a\u672a\u521d\u59cb\u5316\u7684\u6307\u9488 (\u5b58\u653e\u8fd9\u65e0\u6548\u5730\u5740) int * ptr2 {}; // \u4e00\u4e2a\u7a7a\u6307\u9488 (\u7a0d\u540e\u8ba8\u8bba\u8fd9\u79cd\u60c5\u51b5) int * ptr3 { & x }; // \u6307\u9488\uff0c\u521d\u59cb\u5316\u4e3a\u53d8\u91cfx\u7684\u5730\u5740 return 0 ; } \u7531\u4e8e\u6307\u9488\u4fdd\u5b58\u7684\u662f\u5730\u5740\uff0c\u6240\u4ee5\u5176\u521d\u59cb\u5316\u503c\u5fc5\u987b\u662f\u4e00\u4e2a\u5730\u5740\u3002\u901a\u5e38\uff0c\u6307\u9488\u7528\u4e8e\u4fdd\u5b58\u53e6\u4e00\u4e2a\u53d8\u91cf\u7684\u5730\u5740(\u53ef\u4ee5\u4f7f\u7528\u53d6\u5730\u5740\u64cd\u4f5c\u7b26(&)\u83b7\u53d6)\u3002 \u4e00\u65e6\u6211\u4eec\u6709\u4e86\u4e00\u4e2a\u6301\u6709\u53e6\u4e00\u4e2a\u5bf9\u8c61\u5730\u5740\u7684\u6307\u9488\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26 * \u6765\u8bbf\u95ee\u8be5\u5730\u5740\u7684\u503c\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int x { 5 }; std :: cout << x << '\\n' ; // \u6253\u5370\u53d8\u91cf x \u7684\u503c int * ptr { & x }; // ptr \u4fdd\u5b58\u7740 x \u7684\u5730\u5740 std :: cout << * ptr << '\\n' ; // \u4f7f\u7528\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26\u6253\u5370\u8be5\u5730\u5740\u7684\u5185\u5bb9 return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 5 5 \u5f62\u8c61\u4e00\u70b9\u6765\u770b\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u662f\u8fd9\u6837\u7684\uff1a \u8fd9\u5c31\u662f\u6307\u9488\u540d\u79f0\u7684\u7531\u6765\u2014\u2014 ptr \u4fdd\u5b58\u7740 x \u7684\u5730\u5740\uff0c\u6240\u4ee5\u6211\u4eec\u8bf4 ptr \u662f\u201c\u6307\u5411 x \u3002 \u4f5c\u8005\u6ce8 \u5173\u4e8e\u6307\u9488\u547d\u540d\u7684\u6ce8\u610f\u4e8b\u9879:\u201cX\u6307\u9488\u201d(\u5176\u4e2dX\u662f\u67d0\u79cd\u7c7b\u578b)\u662f\u201c\u6307\u5411X\u7684\u6307\u9488\u201d\u7684\u5e38\u7528\u7f29\u5199\u3002\u6240\u4ee5\u5f53\u6211\u4eec\u8bf4\u201c\u4e00\u4e2a\u6574\u6570\u6307\u9488\u201d\u65f6\uff0c\u6211\u4eec\u5b9e\u9645\u4e0a\u662f\u6307\u201c\u4e00\u4e2a\u6307\u5411\u6574\u578b\u53d8\u91cf\u7684\u6307\u9488\u201d\u3002\u5f53\u6211\u4eec\u8ba8\u8bbaconst\u6307\u9488\u65f6\uff0c\u8fd9\u79cd\u533a\u522b\u5c31\u5f88\u91cd\u8981\u4e86\u3002 \u5c31\u50cf\u5f15\u7528\u7684\u7c7b\u578b\u5fc5\u987b\u5339\u914d\u88ab\u5f15\u7528\u5bf9\u8c61\u7684\u7c7b\u578b\u4e00\u6837\uff0c\u6307\u9488\u7684\u7c7b\u578b\u5fc5\u987b\u5339\u914d\u88ab\u6307\u5411\u5bf9\u8c61\u7684\u7c7b\u578b: 1 2 3 4 5 6 7 8 9 10 int main () { int i { 5 }; double d { 7.0 }; int * iPtr { & i }; // ok: a pointer to an int can point to an int object int * iPtr2 { & d }; // not okay: a pointer to an int can't point to a double double * dPtr { & d }; // ok: a pointer to a double can point to a double object double * dPtr2 { & i }; // not okay: a pointer to a double can't point to an int } \u9664\u4e86\u4e00\u4e2a\u4f8b\u5916\uff0c\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u8ba8\u8bba\uff0c\u7528\u5b57\u9762\u91cf\u521d\u59cb\u5316\u6307\u9488\u662f\u4e0d\u5141\u8bb8\u7684\uff1a 1 2 int * ptr { 5 }; // \u4e0d\u53ef\u4ee5 int * ptr { 0x0012FF7C }; // \u4e0d\u53ef\u4ee5, 0x0012FF7C \u88ab\u770b\u505a\u6574\u578b\u5b57\u9762\u91cf \u6307\u9488\u548c\u8d4b\u503c \u5728\u8c08\u8bba\u6307\u9488\u7684\u8d4b\u503c\u65f6\uff0c\u53ef\u80fd\u6709\u4e24\u79cd\u542b\u4e49\uff1a \u6539\u53d8\u6307\u9488\u7684\u503c\u4f7f\u5176\u6307\u5411\u5176\u4ed6\u5730\u5740\uff08\u7ed9\u6307\u9488\u8d4b\u503c\u4e00\u4e2a\u65b0\u5730\u5740\uff09\uff1b \u6539\u53d8\u6307\u9488\u6240\u6307\u5185\u5bb9\u7684\u503c\uff08\u7ed9\u6307\u9488\u89e3\u5f15\u7528\u7684\u7ed3\u679c\u8d4b\u65b0\u503c\uff09\u3002 \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u6307\u9488\u88ab\u66f4\u6539\u4e3a\u6307\u5411\u53e6\u4e00\u4e2a\u5bf9\u8c61\u7684\u60c5\u51b5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { int x { 5 }; int * ptr { & x }; // ptr \u521d\u59cb\u5316\u4e3a\u6307\u5411 x std :: cout << * ptr << '\\n' ; // \u6253\u5370\u5730\u5740\u503c(x\u7684\u5730\u5740) int y { 6 }; ptr = & y ; // // ptr \u66f4\u6539\u4e3a\u6307\u5411 y std :: cout << * ptr << '\\n' ; // \u6253\u5370\u5730\u5740\u503c (y\u7684\u5730\u5740) return 0 ; } \u6253\u5370\uff1a 1 2 5 6 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u6307\u9488 ptr \u5e76\u7528x\u7684\u5730\u5740\u521d\u59cb\u5316\u5b83\u3002\u5bf9\u6307\u9488\u89e3\u5f15\u7528\u4ee5\u6253\u5370\u88ab\u6307\u5411\u7684\u503c( 5 )\u3002\u7136\u540e\u4f7f\u7528\u8d4b\u503c\u64cd\u4f5c\u7b26\u5c06 ptr \u4fdd\u5b58\u7684\u5730\u5740\u66f4\u6539\u4e3ay\u7684\u5730\u5740\u3002\u7136\u540e\u518d\u6b21\u89e3\u5f15\u7528\u8be5\u6307\u9488\u4ee5\u6253\u5370\u88ab\u6307\u5411\u7684\u503c(\u73b0\u5728\u662f 6 )\u3002 \u73b0\u5728\u8ba9\u6211\u4eec\u770b\u770b\u5982\u4f55\u4f7f\u7528\u6307\u9488\u6765\u6539\u53d8\u88ab\u6307\u5411\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { int x { 5 }; int * ptr { & x }; // initialize ptr with address of variable x std :: cout << x << '\\n' ; // print x's value std :: cout << * ptr << '\\n' ; // print the value at the address that ptr is holding (x's address) * ptr = 6 ; // The object at the address held by ptr (x) assigned value 6 (note that ptr is dereferenced here) std :: cout << x << '\\n' ; std :: cout << * ptr << '\\n' ; // print the value at the address that ptr is holding (x's address) return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 5 5 6 6 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u6307\u9488 ptr \uff0c\u5b83\u7684\u521d\u59cb\u5316\u4e3a\u53d8\u91cf x \u7684\u5730\u5740\uff0c\u7136\u540e\u6253\u5370\u4e86 x \u548c *ptr \u7684\u503c( 5 )\u3002\u56e0\u4e3a *ptr \u8fd4\u56de\u7684\u662f\u5de6\u503c\uff0c\u6240\u6211\u4eec\u53ef\u4ee5\u628a\u5b83\u7528\u5728\u8d4b\u503c\u53f7\u7684\u5de6\u4fa7\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06 ptr \u6240\u6307\u7684\u53d8\u91cf\u7684\u503c\u4fee\u6539\u4e3a 6 \u3002\u7136\u540e\uff0c\u518d\u6b21\u6253\u5370 x \u548c *ptr \u4ee5\u4fbf\u786e\u5b9a\u6211\u4eec\u7684\u4fee\u6539\u662f\u5426\u751f\u6548\u3002 \u5173\u952e\u4fe1\u606f \u5728\u4f7f\u7528\u6307\u9488\u65f6\uff0c\u5982\u679c\u4e0d\u540c\u65f6\u4f7f\u7528 \u89e3\u5f15\u7528 \u7684\u65f6\u5019\uff0c\u6211\u4eec\u4f7f\u7528\u7684\u662f\u6307\u9488\u5b58\u653e\u7684\u5730\u5740\u3002\u4fee\u6539\u8be5\u5730\u5740\u5c31\u53ef\u4ee5\u6539\u53d8\u6307\u9488\u7684\u6307\u5411 ( ptr = &y )\u3002 \u5f53\u5bf9\u6307\u9488\u5e94\u7528\u89e3\u5f15\u7528\u65f6( *ptr )\uff0c\u6211\u4eec\u8bbf\u95ee\u7684\u5219\u662f\u6307\u9488\u6240\u6307\u7684\u5bf9\u8c61\u3002\u4fee\u6539( *ptr = 6; ) \u5b83\u53ef\u4ee5\u6539\u53d8\u8be5\u53d8\u91cf\u7684\u503c\u3002 \u6307\u9488\u548c\u5de6\u503c\u5f15\u7528\u5f88\u50cf \u6307\u9488\u548c\u5de6\u503c\u5f15\u7528\u7684\u884c\u4e3a\u7c7b\u4f3c\u3002\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include int main () { int x { 5 }; int & ref { x }; // get a reference to x int * ptr { & x }; // get a pointer to x std :: cout << x ; std :: cout << ref ; // use the reference to print x's value (5) std :: cout << * ptr << '\\n' ; // use the pointer to print x's value (5) ref = 6 ; // use the reference to change the value of x std :: cout << x ; std :: cout << ref ; // use the reference to print x's value (6) std :: cout << * ptr << '\\n' ; // use the pointer to print x's value (6) * ptr = 7 ; // use the pointer to change the value of x std :: cout << x ; std :: cout << ref ; // use the reference to print x's value (7) std :: cout << * ptr << '\\n' ; // use the pointer to print x's value (7) return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 2 3 555 666 777 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u503c\u4e3a5\u7684\u666e\u901a\u53d8\u91cf x \uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u5de6\u503c\u5f15\u7528\u548c\u4e00\u4e2a\u6307\u5411 x \u7684\u6307\u9488\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528\u5de6\u503c\u5f15\u7528\u5c06\u503c\u4ece5\u66f4\u6539\u4e3a6\uff0c\u5e76\u8bf4\u660e\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6240\u6709\u4e09\u4e2a\u65b9\u6cd5\u8bbf\u95ee\u66f4\u65b0\u540e\u7684\u503c\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u89e3\u5f15\u7528\u7684\u6307\u9488\u5c06\u503c\u4ece6\u6539\u4e3a7\uff0c\u5e76\u518d\u6b21\u8868\u660e\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6240\u6709\u4e09\u79cd\u65b9\u6cd5\u8bbf\u95ee\u66f4\u65b0\u540e\u7684\u503c\u3002 \u56e0\u6b64\uff0c\u6307\u9488\u548c\u5f15\u7528\u90fd\u63d0\u4f9b\u4e86\u95f4\u63a5\u8bbf\u95ee\u53e6\u4e00\u4e2a\u5bf9\u8c61\u7684\u65b9\u6cd5\u3002\u4e3b\u8981\u7684\u533a\u522b\u5728\u4e8e\uff0c\u5bf9\u4e8e\u6307\u9488\uff0c\u6211\u4eec\u9700\u8981\u663e\u5f0f\u5730\u83b7\u53d6\u8981\u6307\u5411\u7684\u5730\u5740\uff0c\u5e76\u4e14\u663e\u5f0f\u5730\u89e3\u5f15\u7528\u6307\u9488\u6765\u83b7\u53d6\u503c\u3002\u5bf9\u4e8e\u5f15\u7528\uff0c\u5730\u5740\u548c\u89e3\u5f15\u7528\u90fd\u662f\u9690\u5f0f\u8fdb\u884c\u7684\u3002 \u6307\u9488\u548c\u5f15\u7528\u4e4b\u95f4\u8fd8\u6709\u4e00\u4e9b\u503c\u5f97\u4e00\u63d0\u7684\u533a\u522b\uff1a \u5f15\u7528\u5fc5\u987b\u88ab\u521d\u59cb\u5316\uff0c\u6307\u9488\u5e76\u4e0d\u662f\u5fc5\u987b\u521d\u59cb\u5316\uff08\u4f46\u662f\u4f60\u5e94\u8be5\u521d\u59cb\u5316\uff09\uff1b \u5f15\u7528\u4e0d\u662f\u5bf9\u8c61\uff0c\u6307\u9488\u662f\u5bf9\u8c61\uff1b \u5f15\u7528\u4e0d\u80fd\u88ab\u91cd\u65b0\u8bbe\u7f6e\uff08\u4fee\u6539\u5f15\u7528\u4f7f\u5176\u7ed1\u5b9a\u5230\u5176\u4ed6\u5bf9\u8c61\uff09\uff0c\u6307\u9488\u5219\u53ef\u4ee5\u88ab\u4fee\u6539\u6307\u5411\u5176\u4ed6\u5bf9\u8c61\uff1b \u5f15\u7528\u603b\u662f\u7ed1\u5b9a\u5230\u67d0\u4e2a\u5bf9\u8c61\uff0c\u6307\u9488\u53ef\u4ee5\u4e0d\u6267\u884c\u4efb\u4f55\u5bf9\u8c61\uff1b \u5f15\u7528\u662f\u201d\u5b89\u5168\u201c\u7684\uff08\u9664\u4e86 \u60ac\u5782 \u5f15\u7528\uff09\uff0c\u6307\u9488\u5219\u662f\u5371\u9669\u7684\u3002 \u53d6\u5730\u5740\u8fd0\u7b97\u7b26\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u6307\u9488 \u9700\u8981\u6ce8\u610f\uff0c \u53d6\u5730\u5740\u64cd\u4f5c\u7b26 & \u8fd4\u56de\u7684\u5e76\u4e0d\u662f\u5730\u5740\u7684\u5b57\u9762\u91cf\uff0c\u5b83\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u6307\u5411\u64cd\u4f5c\u6570\u5730\u5740\u7684\u6307\u9488\uff0c\u8be5\u6307\u9488\u7684\u7c7b\u578b\u53d6\u51b3\u4e8e\u53c2\u6570\u7684\u7c7b\u578b\uff08\u4f8b\u5982\uff0c\u83b7\u53d6\u6574\u578b\u53d8\u91cf\u7684\u5730\u5740\u8fd4\u56de\u7684\u662f\u6574\u578b\u6307\u9488\uff09\u3002 \u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { int x { 4 }; std :: cout << typeid ( & x ). name () << '\\n' ; // print the type of &x return 0 ; } \u5728 Visual Studio \u4e2d\u4f1a\u6253\u5370\u5982\u4e0b\u5185\u5bb9\uff1a 1 int * \u5982\u679c\u662fgcc\uff0c\u5219\u4f1a\u6253\u5370\u201cpi\u201d (\u6307\u5411int\u7684\u6307\u9488\u2014\u2014pointer to int)\u3002\u56e0\u4e3a typeid().name() \u7684\u7ed3\u679c\u53d6\u51b3\u4e8e\u7f16\u8bd1\u5668\uff08 typeid \u8fd0\u7b97\u7b26 \uff09\uff0c\u6240\u4ee5\u5b83\u7684\u6253\u5370\u7ed3\u679c\u53ef\u80fd\u662f\u4e0d\u540c\u7684\uff0c\u4e0d\u8fc7\u610f\u601d\u662f\u4e00\u6837\u7684\u3002 \u6307\u9488\u7684\u5927\u5c0f \u6307\u9488\u7684\u5927\u5c0f\u53d6\u51b3\u4e8e\u8be5\u53ef\u6267\u884c\u7a0b\u5e8f\u7f16\u8bd1\u7684\u4f53\u7cfb\u7ed3\u6784\u2014\u201432\u4f4d\u53ef\u6267\u884c\u7a0b\u5e8f\u4f7f\u752832\u4f4d\u5730\u5740\u2014\u2014\u5219\u4e00\u4e2a\u6307\u9488\u572832\u4f4d\u673a\u5668\u4e0a\u7684\u5927\u5c0f\u662f4\u4e2a\u5b57\u8282\u3002\u5bf9\u4e8e64\u4f4d\u53ef\u6267\u884c\u7a0b\u5e8f\uff0c\u6307\u9488\u5927\u5c0f\u4e3a64\u4f4d\uff088\u5b57\u8282\uff09\u3002\u6ce8\u610f\uff0c\u4e0d\u8bba\u6307\u9488\u6240\u6307\u5bf9\u8c61\u7684\u5927\u5c0f\u662f\u591a\u5c11\uff0c\u6307\u9488\u672c\u8eab\u7684\u5927\u5c0f\u603b\u662f\u7b26\u5408\u4e0a\u9762\u7684\u89c4\u5219\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () // \u5047\u8bbe\u4e3a32\u4f4d\u7a0b\u5e8f { char * chPtr {}; // char \u4e3a1\u4e2a\u5b57\u8282 int * iPtr {}; // int\u901a\u5e38\u662f4\u5b57\u8282 long double * ldPtr {}; // long doubles \u901a\u5e38\u662f8\u5b57\u8282\u621612\u5b57\u8282 std :: cout << sizeof ( chPtr ) << '\\n' ; // \u6253\u5370 4 std :: cout << sizeof ( iPtr ) << '\\n' ; // \u6253\u5370 4 std :: cout << sizeof ( ldPtr ) << '\\n' ; // \u6253\u5370 4 return 0 ; } \u6307\u9488\u7684\u5927\u5c0f\u603b\u662f\u76f8\u540c\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u6307\u9488\u53ea\u662f\u4e00\u4e2a\u5185\u5b58\u5730\u5740\uff0c\u800c\u8bbf\u95ee\u5185\u5b58\u5730\u5740\u6240\u9700\u7684\u6bd4\u7279\u6570\u662f\u6052\u5b9a\u7684\u3002 \u60ac\u5782\u6307\u9488 \u5c31\u50cf\u60ac\u5782\u5f15\u7528\u4e00\u6837\uff0c\u60ac\u5782\u6307\u9488\u662f\u4fdd\u5b58\u4e0d\u518d\u6709\u6548\u7684\u5bf9\u8c61\u5730\u5740\u7684\u6307\u9488(\u4f8b\u5982\uff0c\u5bf9\u8c61\u5df2\u88ab\u9500\u6bc1)\u3002\u5bf9\u60ac\u6d6e\u6307\u9488\u7684\u89e3\u5f15\u7528\u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u521b\u5efa\u60ac\u6d6e\u6307\u9488\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int main () { int x { 5 }; int * ptr { & x }; std :: cout << * ptr << '\\n' ; // valid { int y { 6 }; ptr = & y ; std :: cout << * ptr << '\\n' ; // valid } // y goes out of scope, and ptr is now dangling std :: cout << * ptr << '\\n' ; // undefined behavior from dereferencing a dangling pointer return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 2 3 5 6 6 \u4f46\u662f\uff0c\u7ed3\u679c\u4e5f\u53ef\u80fd\u4e0d\u662f\u4e0a\u9762\u8fd9\u6837\uff0c\u56e0\u4e3a ptr \u6240\u6307\u7684\u5bf9\u8c61\u5728 \u79bb\u5f00\u4f5c\u7528\u57df \u65f6(\u5185\u5c42\u8bed\u53e5\u5757)\u4ee5\u53ca\u88ab\u9500\u6bc1\u4e86\uff0c\u6b64\u65f6\u7684 ptr \u662f\u4e00\u4e2a\u60ac\u5782\u6307\u9488\u3002 \u5c0f\u7ed3 \u6307\u9488\u662f\u4fdd\u5b58\u5185\u5b58\u5730\u5740\u7684\u53d8\u91cf\u3002\u53ef\u4ee5\u4f7f\u7528 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u5bf9\u5b83\u4eec\u8fdb\u884c\u89e3\u5f15\u7528\uff0c\u4ee5\u83b7\u53d6\u6307\u9488\u6240\u6301\u6709\u7684\u5730\u5740\u7684\u503c\u3002\u5bf9 \u91ce\u6307\u9488 \u6216 \u60ac\u5782 \u6307\u9488(\u6216\u7a7a\u6307\u9488)\u7684\u89e3\u5f15\u7528\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u5e76\u53ef\u80fd\u5bfc\u81f4\u5e94\u7528\u7a0b\u5e8f\u5d29\u6e83\u3002 \u6307\u9488\u6bd4\u5f15\u7528\u66f4\u7075\u6d3b\uff0c\u4e5f\u66f4\u5371\u9669\u3002\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\u7ee7\u7eed\u63a2\u8ba8\u8fd9\u4e2a\u95ee\u9898\u3002","title":"9.6 - \u6307\u9488\u7b80\u4ecb"},{"location":"9-6-Introduction-to-pointers/#_1","text":"\u5c3d\u7ba1\u5728\u9ed8\u8ba4\u7684\u60c5\u51b5\u4e0b\uff0c\u53d8\u91cf\u7684\u5730\u5740\u5e76\u4e0d\u4f1a\u66b4\u9732\u7ed9\u7528\u6237\uff0c\u4f46\u8be5\u5730\u5740\u5b9e\u9645\u4e0a\u662f\u53ef\u4ee5\u83b7\u53d6\u7684\u3002 \u53d6\u5730\u5740\u64cd\u4f5c\u7b26 & \u5c31\u53ef\u4ee5\u8fd4\u56de\u5176\u64cd\u4f5c\u6570\u7684\u5730\u5740\uff0c\u975e\u5e38\u7b80\u5355\uff1a 1 2 3 4 5 6 7 8 9 10 #include int main () { int x { 5 }; std :: cout << x << '\\n' ; // print the value of variable x std :: cout << & x << '\\n' ; // print the memory address of variable x return 0 ; } \u5728\u7b14\u8005\u7684\u7535\u8111\u4e0a\u8fd4\u56de\u5982\u4e0b\u7ed3\u679c\uff1a 1 2 5 0027FEA0 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u53d6\u5730\u5740\u8fd0\u7b97\u7b26\u6765\u83b7\u53d6\u53d8\u91cfx\u7684\u5730\u5740\u5e76\u5c06\u5176\u6253\u5370\u5904\u7406\u3002\u5185\u5b58\u5730\u5740\u901a\u5e38\u662f\u4ee5\u5341\u516d\u8fdb\u5236\u503c\u7684\u5f62\u5f0f\u6253\u5370\u7684\uff08\u53c2\u8003 4.16 - \u6570\u503c\u7cfb\u7edf\uff08\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236\uff09 \u4e86\u89e3\u5341\u516d\u8fdb\u5236\u7684\u5185\u5bb9\uff09\uff0c\u4f46\u901a\u5e38\u4e0d\u5305\u62ec\u524d\u7f00 0x \u3002 \u5bf9\u4e8e\u4f7f\u7528\u5185\u5b58\u6570\u8d85\u8fc7\u4e00\u4e2a\u5b57\u8282\u7684\u5bf9\u8c61\uff0c\u53d6\u5730\u5740\u4f1a\u8fd4\u56de\u8be5\u6bb5\u5185\u5b58\u7684\u9996\u5730\u5740\uff08\u7b2c\u4e00\u4e2a\u5b57\u8282\uff09\u3002 \u5c0f\u8d34\u58eb & \u7b26\u53f7\u5728\u4e0d\u540c\u7684\u8bed\u5883\u4e0b\u6709\u4e0d\u540c\u7684\u542b\u4e49\uff0c\u6bd4\u8f83\u5bb9\u6613\u6df7\u6dc6\uff1a \u5f53\u540e\u9762\u63a5\u7740\u7c7b\u578b\u540d\u65f6\uff0c&\u8868\u793a\u4e00\u4e2a\u5de6\u503c\u5f15\u7528: int& ref \uff1b \u5f53\u7528\u4e8e\u4e00\u5143\u8868\u8fbe\u5f0f\u65f6\uff0c&\u8868\u793a\u53d6\u5730\u5740\u8fd0\u7b97\u7b26: std::cout << &x . \u5f53\u7528\u4e8e\u4e8c\u5143\u8868\u8fbe\u5f0f\u65f6\uff0c&\u662f \u6309\u4f4d\u4e0e\u64cd\u4f5c : std::cout << x & y .","title":"\u53d6\u5730\u5740\u8fd0\u7b97\u7b26 (&)"},{"location":"9-6-Introduction-to-pointers/#_2","text":"\u4ec5\u4ec5\u83b7\u5f97\u53d8\u91cf\u7684\u5730\u5740\uff0c\u5f80\u5f80\u4e0d\u662f\u5f88\u6709\u7528\u3002 \u5bf9\u4e8e\u4e00\u4e2a\u5730\u5740\u6765\u8bf4\uff0c\u6700\u6709\u7528\u7684\u64cd\u4f5c\u662f\u83b7\u53d6\u5b83\u5b58\u653e\u7684\u503c\u3002 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \uff08\u4e5f\u53eb\u95f4\u63a5\u8bbf\u95ee\u8fd0\u7b97\u7b26\uff09\uff0c\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u5730\u5740\u5b58\u653e\u7684\u503c\uff08\u4f5c\u4e3a\u5de6\u503c\u8fd4\u56de\uff09\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int x { 5 }; std :: cout << x << '\\n' ; // print the value of variable x std :: cout << & x << '\\n' ; // print the memory address of variable x std :: cout << * ( & x ) << '\\n' ; // print the value at the memory address of variable x (parentheses not required, but make it easier to read) return 0 ; } \u5728\u7b14\u8005\u673a\u5668\u4e0a\u8fd4\u56de\u4e0b\u9762\u5185\u5bb9\uff1a 1 2 3 5 0027FEA0 5 \u8fd9\u4e2a\u7a0b\u5e8f\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a\u53d8\u91cfx\u5e76\u4e14\u6253\u5370\u51fa\u5b83\u7684\u503c\u3002\u7136\u540e\u6211\u4eec\u6253\u5370\u51fa\u4e86\u53d8\u91cfx\u7684\u5730\u5740\u3002\u6700\u540e\uff0c\u4f7f\u7528\u89e3\u5f15\u7528\u8fd0\u7b97\u7b26\u4ece\u53d8\u91cfx\u7684\u5730\u5740\u83b7\u53d6\u503c\uff08\u5c31\u662fx\u7684\u503c\uff09\uff0c\u7136\u540e\u6253\u5370\u51fa\u6765\u3002 \u5173\u952e\u4fe1\u606f \u5bf9\u4e8e\u4e00\u4e2a\u7ed9\u5b9a\u5730\u5740\uff0c\u53ef\u4ee5\u4f7f\u7528 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u83b7\u53d6\u8be5\u5730\u5740\u4fdd\u5b58\u7684\u503c\uff08\u5de6\u503c\uff09 \u89e3\u5f15\u7528\u548c\u53d6\u5730\u5740\u662f\u4e00\u5bf9\u76f8\u53cd\u7684\u64cd\u4f5c\uff0c\u53d6\u5730\u5740\u83b7\u53d6\u5bf9\u8c61\u7684\u5730\u5740\uff0c\u89e3\u5f15\u7528\u4ece\u5730\u5740\u83b7\u53d6\u5bf9\u8c61\u3002 \u5c0f\u8d34\u58eb \u5c3d\u7ba1\u89e3\u5f15\u7528\u770b\u8d77\u6765\u5c31\u50cf\u4e58\u6cd5\uff0c\u4f46\u662f\u4f60\u53ef\u4ee5\u533a\u5206\u5b83\u4eec\uff0c\u56e0\u4e3a\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26\u662f\u4e00\u5143\u7684\uff0c\u800c\u4e58\u6cd5\u64cd\u4f5c\u7b26\u662f\u4e8c\u5143\u7684\u3002 \u83b7\u53d6\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\uff0c\u7136\u540e\u7acb\u5373\u89e3\u5f15\u7528\u8be5\u5730\u5740\u4ee5\u83b7\u5f97\u503c\uff0c\u8fd9\u5176\u5b9e\u4e5f\u4e0d\u7b97\u662f\u4ec0\u4e48\u6709\u7528\u7684\u64cd\u4f5c(\u6bd5\u7adf\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u53d8\u91cf\u7684\u503c)\u3002 \u4e0d\u8fc7\uff0c\u5728\u4e86\u89e3\u53d6\u5730\u5740\u548c\u89e3\u5f15\u7528\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb\u4ecb\u7ecd\u6307\u9488\u4e86\u3002","title":"\u89e3\u5f15\u7528\u8fd0\u7b97\u7b26*"},{"location":"9-6-Introduction-to-pointers/#_3","text":"\u6307\u9488\u53ef\u4ee5\u770b\u505a\u662f\u4e00\u4e2a\u6301\u6709\u5185\u5b58\u5730\u5740\u7684\u5bf9\u8c61\uff08\u901a\u5e38\u662f\u5176\u4ed6\u5bf9\u8c61\u7684\u5730\u5740\uff09\u3002\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u5728\u540e\u9762\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528\u8be5\u5730\u5740\u3002 \u9898\u5916\u8bdd \u5728\u73b0\u4ee3 C++ \u4e2d\uff0c\u6211\u4eec\u6b64\u5904\u8c08\u8bba\u7684\u6307\u9488\u88ab\u79f0\u4e3a \u539f\u59cb\u6307\u9488 \u6216 \u7b28\u6307\u9488 \uff0c\u4ee5\u4fbf\u5c06\u5176\u4e0e \u667a\u80fd\u6307\u9488 \u533a\u522b\u5f00\u6765\u3002\u667a\u80fd\u6307\u9488\u4f1a\u5728 chapter M \u4ecb\u7ecd\u3002 \u548c\u4f7f\u7528&\u5b9a\u4e49\u5f15\u7528\u7684\u505a\u6cd5\u7c7b\u4f3c\uff0c\u6307\u9488\u7c7b\u578b\u4f7f\u7528 * \u6765\u5b9a\u4e49\uff1a 1 2 3 4 int ; // a normal int int & ; // an lvalue reference to an int value int * ; // a pointer to an int value (holds the address of an integer value) \u8981\u521b\u5efa\u6307\u9488\u53d8\u91cf\uff0c\u53ea\u9700\u5b9a\u4e49\u4e00\u4e2a\u6307\u9488\u7c7b\u578b\u7684\u53d8\u91cf: 1 2 3 4 5 6 7 8 9 int main () { int x { 5 }; // normal variable int & ref { x }; // a reference to an integer (bound to x) int * ptr ; // a pointer to an integer return 0 ; } \u6ce8\u610f\uff0c\u8fd9\u4e2a\u661f\u53f7\u662f\u6307\u9488\u58f0\u660e\u8bed\u6cd5\u7684\u4e00\u90e8\u5206\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26\u3002 \u6700\u4f73\u5b9e\u8df5 \u58f0\u660e\u6307\u9488\u7c7b\u578b\u65f6\uff0c\u5728\u7c7b\u578b\u540d\u79f0\u65c1\u8fb9\u52a0\u4e0a\u661f\u53f7\u3002 \u6ce8\u610f \u5c3d\u7ba1\u901a\u5e38\u60c5\u51b5\u4e0b\u6211\u4eec\u4e0d\u5e94\u8be5\u5728\u4e00\u884c\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\uff0c\u4f46\u662f\u5982\u679c\u9700\u8981\u8fd9\u4e48\u505a\uff0c\u5219\u6bcf\u4e2a\u6307\u9488\u53d8\u91cf\u524d\u9762\u90fd\u9700\u8981\u661f\u53f7 1 2 int * ptr1 , ptr2 ; // incorrect: ptr1 is a pointer to an int, but ptr2 is just a plain int! int * ptr3 , * ptr4 ; // correct: ptr3 and p4 are both pointers to an int \u867d\u7136\u8fd9\u4e2a\u4f8b\u5b50\u65f6\u5e38\u88ab\u4f5c\u4e3a\u201d\u661f\u53f7\u4e0d\u5e94\u8be5\u4e0e\u7c7b\u578b\u540d\u653e\u5728\u4e00\u8d77\u201c\u7684\u8bba\u636e\uff0c\u4f46\u6211\u4eec\u5012\u4e0d\u5982\u8bf4\u8fd9\u662f\u4e00\u4e2a\u201d\u4e0d\u5e94\u5728\u4e00\u884c\u4e2d\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\u7684\u201c\u4f8b\u5b50\u3002","title":"\u6307\u9488"},{"location":"9-6-Introduction-to-pointers/#_4","text":"\u548c\u666e\u901a\u7684\u53d8\u91cf\u4e0d\u540c\uff0c \u6307\u9488\u9ed8\u8ba4\u662f\u4e0d\u521d\u59cb\u5316\u7684 \u3002\u4e00\u4e2a\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u7684\u6307\u9488\uff0c\u6709\u65f6\u79f0\u4e3a \u91ce\u6307\u9488 \u3002\u91ce\u6307\u9488\u4e2d\u5b58\u653e\u7684\u662f\u4e00\u4e2a\u65e0\u7528\u7684\u5730\u5740\uff0c\u5bf9\u91ce\u6307\u9488\u89e3\u5f15\u7528\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4e00\u5b9a\u8981\u5c06\u6307\u9488\u521d\u59cb\u5316\u4e3a\u4e00\u4e2a\u5df2\u77e5\u7684\u503c\u3002 \u6700\u4f73\u5b9e\u8df5 \u6307\u9488\u5fc5\u987b\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 10 int main () { int x { 5 }; int * ptr ; // \u4e00\u4e2a\u672a\u521d\u59cb\u5316\u7684\u6307\u9488 (\u5b58\u653e\u8fd9\u65e0\u6548\u5730\u5740) int * ptr2 {}; // \u4e00\u4e2a\u7a7a\u6307\u9488 (\u7a0d\u540e\u8ba8\u8bba\u8fd9\u79cd\u60c5\u51b5) int * ptr3 { & x }; // \u6307\u9488\uff0c\u521d\u59cb\u5316\u4e3a\u53d8\u91cfx\u7684\u5730\u5740 return 0 ; } \u7531\u4e8e\u6307\u9488\u4fdd\u5b58\u7684\u662f\u5730\u5740\uff0c\u6240\u4ee5\u5176\u521d\u59cb\u5316\u503c\u5fc5\u987b\u662f\u4e00\u4e2a\u5730\u5740\u3002\u901a\u5e38\uff0c\u6307\u9488\u7528\u4e8e\u4fdd\u5b58\u53e6\u4e00\u4e2a\u53d8\u91cf\u7684\u5730\u5740(\u53ef\u4ee5\u4f7f\u7528\u53d6\u5730\u5740\u64cd\u4f5c\u7b26(&)\u83b7\u53d6)\u3002 \u4e00\u65e6\u6211\u4eec\u6709\u4e86\u4e00\u4e2a\u6301\u6709\u53e6\u4e00\u4e2a\u5bf9\u8c61\u5730\u5740\u7684\u6307\u9488\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26 * \u6765\u8bbf\u95ee\u8be5\u5730\u5740\u7684\u503c\u3002\u4f8b\u5982: 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int x { 5 }; std :: cout << x << '\\n' ; // \u6253\u5370\u53d8\u91cf x \u7684\u503c int * ptr { & x }; // ptr \u4fdd\u5b58\u7740 x \u7684\u5730\u5740 std :: cout << * ptr << '\\n' ; // \u4f7f\u7528\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26\u6253\u5370\u8be5\u5730\u5740\u7684\u5185\u5bb9 return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 5 5 \u5f62\u8c61\u4e00\u70b9\u6765\u770b\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u662f\u8fd9\u6837\u7684\uff1a \u8fd9\u5c31\u662f\u6307\u9488\u540d\u79f0\u7684\u7531\u6765\u2014\u2014 ptr \u4fdd\u5b58\u7740 x \u7684\u5730\u5740\uff0c\u6240\u4ee5\u6211\u4eec\u8bf4 ptr \u662f\u201c\u6307\u5411 x \u3002 \u4f5c\u8005\u6ce8 \u5173\u4e8e\u6307\u9488\u547d\u540d\u7684\u6ce8\u610f\u4e8b\u9879:\u201cX\u6307\u9488\u201d(\u5176\u4e2dX\u662f\u67d0\u79cd\u7c7b\u578b)\u662f\u201c\u6307\u5411X\u7684\u6307\u9488\u201d\u7684\u5e38\u7528\u7f29\u5199\u3002\u6240\u4ee5\u5f53\u6211\u4eec\u8bf4\u201c\u4e00\u4e2a\u6574\u6570\u6307\u9488\u201d\u65f6\uff0c\u6211\u4eec\u5b9e\u9645\u4e0a\u662f\u6307\u201c\u4e00\u4e2a\u6307\u5411\u6574\u578b\u53d8\u91cf\u7684\u6307\u9488\u201d\u3002\u5f53\u6211\u4eec\u8ba8\u8bbaconst\u6307\u9488\u65f6\uff0c\u8fd9\u79cd\u533a\u522b\u5c31\u5f88\u91cd\u8981\u4e86\u3002 \u5c31\u50cf\u5f15\u7528\u7684\u7c7b\u578b\u5fc5\u987b\u5339\u914d\u88ab\u5f15\u7528\u5bf9\u8c61\u7684\u7c7b\u578b\u4e00\u6837\uff0c\u6307\u9488\u7684\u7c7b\u578b\u5fc5\u987b\u5339\u914d\u88ab\u6307\u5411\u5bf9\u8c61\u7684\u7c7b\u578b: 1 2 3 4 5 6 7 8 9 10 int main () { int i { 5 }; double d { 7.0 }; int * iPtr { & i }; // ok: a pointer to an int can point to an int object int * iPtr2 { & d }; // not okay: a pointer to an int can't point to a double double * dPtr { & d }; // ok: a pointer to a double can point to a double object double * dPtr2 { & i }; // not okay: a pointer to a double can't point to an int } \u9664\u4e86\u4e00\u4e2a\u4f8b\u5916\uff0c\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8bfe\u8ba8\u8bba\uff0c\u7528\u5b57\u9762\u91cf\u521d\u59cb\u5316\u6307\u9488\u662f\u4e0d\u5141\u8bb8\u7684\uff1a 1 2 int * ptr { 5 }; // \u4e0d\u53ef\u4ee5 int * ptr { 0x0012FF7C }; // \u4e0d\u53ef\u4ee5, 0x0012FF7C \u88ab\u770b\u505a\u6574\u578b\u5b57\u9762\u91cf","title":"\u6307\u9488\u7684\u521d\u59cb\u5316"},{"location":"9-6-Introduction-to-pointers/#_5","text":"\u5728\u8c08\u8bba\u6307\u9488\u7684\u8d4b\u503c\u65f6\uff0c\u53ef\u80fd\u6709\u4e24\u79cd\u542b\u4e49\uff1a \u6539\u53d8\u6307\u9488\u7684\u503c\u4f7f\u5176\u6307\u5411\u5176\u4ed6\u5730\u5740\uff08\u7ed9\u6307\u9488\u8d4b\u503c\u4e00\u4e2a\u65b0\u5730\u5740\uff09\uff1b \u6539\u53d8\u6307\u9488\u6240\u6307\u5185\u5bb9\u7684\u503c\uff08\u7ed9\u6307\u9488\u89e3\u5f15\u7528\u7684\u7ed3\u679c\u8d4b\u65b0\u503c\uff09\u3002 \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u6307\u9488\u88ab\u66f4\u6539\u4e3a\u6307\u5411\u53e6\u4e00\u4e2a\u5bf9\u8c61\u7684\u60c5\u51b5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include int main () { int x { 5 }; int * ptr { & x }; // ptr \u521d\u59cb\u5316\u4e3a\u6307\u5411 x std :: cout << * ptr << '\\n' ; // \u6253\u5370\u5730\u5740\u503c(x\u7684\u5730\u5740) int y { 6 }; ptr = & y ; // // ptr \u66f4\u6539\u4e3a\u6307\u5411 y std :: cout << * ptr << '\\n' ; // \u6253\u5370\u5730\u5740\u503c (y\u7684\u5730\u5740) return 0 ; } \u6253\u5370\uff1a 1 2 5 6 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u6307\u9488 ptr \u5e76\u7528x\u7684\u5730\u5740\u521d\u59cb\u5316\u5b83\u3002\u5bf9\u6307\u9488\u89e3\u5f15\u7528\u4ee5\u6253\u5370\u88ab\u6307\u5411\u7684\u503c( 5 )\u3002\u7136\u540e\u4f7f\u7528\u8d4b\u503c\u64cd\u4f5c\u7b26\u5c06 ptr \u4fdd\u5b58\u7684\u5730\u5740\u66f4\u6539\u4e3ay\u7684\u5730\u5740\u3002\u7136\u540e\u518d\u6b21\u89e3\u5f15\u7528\u8be5\u6307\u9488\u4ee5\u6253\u5370\u88ab\u6307\u5411\u7684\u503c(\u73b0\u5728\u662f 6 )\u3002 \u73b0\u5728\u8ba9\u6211\u4eec\u770b\u770b\u5982\u4f55\u4f7f\u7528\u6307\u9488\u6765\u6539\u53d8\u88ab\u6307\u5411\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include int main () { int x { 5 }; int * ptr { & x }; // initialize ptr with address of variable x std :: cout << x << '\\n' ; // print x's value std :: cout << * ptr << '\\n' ; // print the value at the address that ptr is holding (x's address) * ptr = 6 ; // The object at the address held by ptr (x) assigned value 6 (note that ptr is dereferenced here) std :: cout << x << '\\n' ; std :: cout << * ptr << '\\n' ; // print the value at the address that ptr is holding (x's address) return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 4 5 5 6 6 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u6307\u9488 ptr \uff0c\u5b83\u7684\u521d\u59cb\u5316\u4e3a\u53d8\u91cf x \u7684\u5730\u5740\uff0c\u7136\u540e\u6253\u5370\u4e86 x \u548c *ptr \u7684\u503c( 5 )\u3002\u56e0\u4e3a *ptr \u8fd4\u56de\u7684\u662f\u5de6\u503c\uff0c\u6240\u6211\u4eec\u53ef\u4ee5\u628a\u5b83\u7528\u5728\u8d4b\u503c\u53f7\u7684\u5de6\u4fa7\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06 ptr \u6240\u6307\u7684\u53d8\u91cf\u7684\u503c\u4fee\u6539\u4e3a 6 \u3002\u7136\u540e\uff0c\u518d\u6b21\u6253\u5370 x \u548c *ptr \u4ee5\u4fbf\u786e\u5b9a\u6211\u4eec\u7684\u4fee\u6539\u662f\u5426\u751f\u6548\u3002 \u5173\u952e\u4fe1\u606f \u5728\u4f7f\u7528\u6307\u9488\u65f6\uff0c\u5982\u679c\u4e0d\u540c\u65f6\u4f7f\u7528 \u89e3\u5f15\u7528 \u7684\u65f6\u5019\uff0c\u6211\u4eec\u4f7f\u7528\u7684\u662f\u6307\u9488\u5b58\u653e\u7684\u5730\u5740\u3002\u4fee\u6539\u8be5\u5730\u5740\u5c31\u53ef\u4ee5\u6539\u53d8\u6307\u9488\u7684\u6307\u5411 ( ptr = &y )\u3002 \u5f53\u5bf9\u6307\u9488\u5e94\u7528\u89e3\u5f15\u7528\u65f6( *ptr )\uff0c\u6211\u4eec\u8bbf\u95ee\u7684\u5219\u662f\u6307\u9488\u6240\u6307\u7684\u5bf9\u8c61\u3002\u4fee\u6539( *ptr = 6; ) \u5b83\u53ef\u4ee5\u6539\u53d8\u8be5\u53d8\u91cf\u7684\u503c\u3002","title":"\u6307\u9488\u548c\u8d4b\u503c"},{"location":"9-6-Introduction-to-pointers/#_6","text":"\u6307\u9488\u548c\u5de6\u503c\u5f15\u7528\u7684\u884c\u4e3a\u7c7b\u4f3c\u3002\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include int main () { int x { 5 }; int & ref { x }; // get a reference to x int * ptr { & x }; // get a pointer to x std :: cout << x ; std :: cout << ref ; // use the reference to print x's value (5) std :: cout << * ptr << '\\n' ; // use the pointer to print x's value (5) ref = 6 ; // use the reference to change the value of x std :: cout << x ; std :: cout << ref ; // use the reference to print x's value (6) std :: cout << * ptr << '\\n' ; // use the pointer to print x's value (6) * ptr = 7 ; // use the pointer to change the value of x std :: cout << x ; std :: cout << ref ; // use the reference to print x's value (7) std :: cout << * ptr << '\\n' ; // use the pointer to print x's value (7) return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 2 3 555 666 777 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u503c\u4e3a5\u7684\u666e\u901a\u53d8\u91cf x \uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u5de6\u503c\u5f15\u7528\u548c\u4e00\u4e2a\u6307\u5411 x \u7684\u6307\u9488\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528\u5de6\u503c\u5f15\u7528\u5c06\u503c\u4ece5\u66f4\u6539\u4e3a6\uff0c\u5e76\u8bf4\u660e\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6240\u6709\u4e09\u4e2a\u65b9\u6cd5\u8bbf\u95ee\u66f4\u65b0\u540e\u7684\u503c\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u89e3\u5f15\u7528\u7684\u6307\u9488\u5c06\u503c\u4ece6\u6539\u4e3a7\uff0c\u5e76\u518d\u6b21\u8868\u660e\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6240\u6709\u4e09\u79cd\u65b9\u6cd5\u8bbf\u95ee\u66f4\u65b0\u540e\u7684\u503c\u3002 \u56e0\u6b64\uff0c\u6307\u9488\u548c\u5f15\u7528\u90fd\u63d0\u4f9b\u4e86\u95f4\u63a5\u8bbf\u95ee\u53e6\u4e00\u4e2a\u5bf9\u8c61\u7684\u65b9\u6cd5\u3002\u4e3b\u8981\u7684\u533a\u522b\u5728\u4e8e\uff0c\u5bf9\u4e8e\u6307\u9488\uff0c\u6211\u4eec\u9700\u8981\u663e\u5f0f\u5730\u83b7\u53d6\u8981\u6307\u5411\u7684\u5730\u5740\uff0c\u5e76\u4e14\u663e\u5f0f\u5730\u89e3\u5f15\u7528\u6307\u9488\u6765\u83b7\u53d6\u503c\u3002\u5bf9\u4e8e\u5f15\u7528\uff0c\u5730\u5740\u548c\u89e3\u5f15\u7528\u90fd\u662f\u9690\u5f0f\u8fdb\u884c\u7684\u3002 \u6307\u9488\u548c\u5f15\u7528\u4e4b\u95f4\u8fd8\u6709\u4e00\u4e9b\u503c\u5f97\u4e00\u63d0\u7684\u533a\u522b\uff1a \u5f15\u7528\u5fc5\u987b\u88ab\u521d\u59cb\u5316\uff0c\u6307\u9488\u5e76\u4e0d\u662f\u5fc5\u987b\u521d\u59cb\u5316\uff08\u4f46\u662f\u4f60\u5e94\u8be5\u521d\u59cb\u5316\uff09\uff1b \u5f15\u7528\u4e0d\u662f\u5bf9\u8c61\uff0c\u6307\u9488\u662f\u5bf9\u8c61\uff1b \u5f15\u7528\u4e0d\u80fd\u88ab\u91cd\u65b0\u8bbe\u7f6e\uff08\u4fee\u6539\u5f15\u7528\u4f7f\u5176\u7ed1\u5b9a\u5230\u5176\u4ed6\u5bf9\u8c61\uff09\uff0c\u6307\u9488\u5219\u53ef\u4ee5\u88ab\u4fee\u6539\u6307\u5411\u5176\u4ed6\u5bf9\u8c61\uff1b \u5f15\u7528\u603b\u662f\u7ed1\u5b9a\u5230\u67d0\u4e2a\u5bf9\u8c61\uff0c\u6307\u9488\u53ef\u4ee5\u4e0d\u6267\u884c\u4efb\u4f55\u5bf9\u8c61\uff1b \u5f15\u7528\u662f\u201d\u5b89\u5168\u201c\u7684\uff08\u9664\u4e86 \u60ac\u5782 \u5f15\u7528\uff09\uff0c\u6307\u9488\u5219\u662f\u5371\u9669\u7684\u3002","title":"\u6307\u9488\u548c\u5de6\u503c\u5f15\u7528\u5f88\u50cf"},{"location":"9-6-Introduction-to-pointers/#_7","text":"\u9700\u8981\u6ce8\u610f\uff0c \u53d6\u5730\u5740\u64cd\u4f5c\u7b26 & \u8fd4\u56de\u7684\u5e76\u4e0d\u662f\u5730\u5740\u7684\u5b57\u9762\u91cf\uff0c\u5b83\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u6307\u5411\u64cd\u4f5c\u6570\u5730\u5740\u7684\u6307\u9488\uff0c\u8be5\u6307\u9488\u7684\u7c7b\u578b\u53d6\u51b3\u4e8e\u53c2\u6570\u7684\u7c7b\u578b\uff08\u4f8b\u5982\uff0c\u83b7\u53d6\u6574\u578b\u53d8\u91cf\u7684\u5730\u5740\u8fd4\u56de\u7684\u662f\u6574\u578b\u6307\u9488\uff09\u3002 \u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 #include #include int main () { int x { 4 }; std :: cout << typeid ( & x ). name () << '\\n' ; // print the type of &x return 0 ; } \u5728 Visual Studio \u4e2d\u4f1a\u6253\u5370\u5982\u4e0b\u5185\u5bb9\uff1a 1 int * \u5982\u679c\u662fgcc\uff0c\u5219\u4f1a\u6253\u5370\u201cpi\u201d (\u6307\u5411int\u7684\u6307\u9488\u2014\u2014pointer to int)\u3002\u56e0\u4e3a typeid().name() \u7684\u7ed3\u679c\u53d6\u51b3\u4e8e\u7f16\u8bd1\u5668\uff08 typeid \u8fd0\u7b97\u7b26 \uff09\uff0c\u6240\u4ee5\u5b83\u7684\u6253\u5370\u7ed3\u679c\u53ef\u80fd\u662f\u4e0d\u540c\u7684\uff0c\u4e0d\u8fc7\u610f\u601d\u662f\u4e00\u6837\u7684\u3002","title":"\u53d6\u5730\u5740\u8fd0\u7b97\u7b26\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u6307\u9488"},{"location":"9-6-Introduction-to-pointers/#_8","text":"\u6307\u9488\u7684\u5927\u5c0f\u53d6\u51b3\u4e8e\u8be5\u53ef\u6267\u884c\u7a0b\u5e8f\u7f16\u8bd1\u7684\u4f53\u7cfb\u7ed3\u6784\u2014\u201432\u4f4d\u53ef\u6267\u884c\u7a0b\u5e8f\u4f7f\u752832\u4f4d\u5730\u5740\u2014\u2014\u5219\u4e00\u4e2a\u6307\u9488\u572832\u4f4d\u673a\u5668\u4e0a\u7684\u5927\u5c0f\u662f4\u4e2a\u5b57\u8282\u3002\u5bf9\u4e8e64\u4f4d\u53ef\u6267\u884c\u7a0b\u5e8f\uff0c\u6307\u9488\u5927\u5c0f\u4e3a64\u4f4d\uff088\u5b57\u8282\uff09\u3002\u6ce8\u610f\uff0c\u4e0d\u8bba\u6307\u9488\u6240\u6307\u5bf9\u8c61\u7684\u5927\u5c0f\u662f\u591a\u5c11\uff0c\u6307\u9488\u672c\u8eab\u7684\u5927\u5c0f\u603b\u662f\u7b26\u5408\u4e0a\u9762\u7684\u89c4\u5219\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include int main () // \u5047\u8bbe\u4e3a32\u4f4d\u7a0b\u5e8f { char * chPtr {}; // char \u4e3a1\u4e2a\u5b57\u8282 int * iPtr {}; // int\u901a\u5e38\u662f4\u5b57\u8282 long double * ldPtr {}; // long doubles \u901a\u5e38\u662f8\u5b57\u8282\u621612\u5b57\u8282 std :: cout << sizeof ( chPtr ) << '\\n' ; // \u6253\u5370 4 std :: cout << sizeof ( iPtr ) << '\\n' ; // \u6253\u5370 4 std :: cout << sizeof ( ldPtr ) << '\\n' ; // \u6253\u5370 4 return 0 ; } \u6307\u9488\u7684\u5927\u5c0f\u603b\u662f\u76f8\u540c\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u6307\u9488\u53ea\u662f\u4e00\u4e2a\u5185\u5b58\u5730\u5740\uff0c\u800c\u8bbf\u95ee\u5185\u5b58\u5730\u5740\u6240\u9700\u7684\u6bd4\u7279\u6570\u662f\u6052\u5b9a\u7684\u3002","title":"\u6307\u9488\u7684\u5927\u5c0f"},{"location":"9-6-Introduction-to-pointers/#_9","text":"\u5c31\u50cf\u60ac\u5782\u5f15\u7528\u4e00\u6837\uff0c\u60ac\u5782\u6307\u9488\u662f\u4fdd\u5b58\u4e0d\u518d\u6709\u6548\u7684\u5bf9\u8c61\u5730\u5740\u7684\u6307\u9488(\u4f8b\u5982\uff0c\u5bf9\u8c61\u5df2\u88ab\u9500\u6bc1)\u3002\u5bf9\u60ac\u6d6e\u6307\u9488\u7684\u89e3\u5f15\u7528\u5c06\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u521b\u5efa\u60ac\u6d6e\u6307\u9488\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include int main () { int x { 5 }; int * ptr { & x }; std :: cout << * ptr << '\\n' ; // valid { int y { 6 }; ptr = & y ; std :: cout << * ptr << '\\n' ; // valid } // y goes out of scope, and ptr is now dangling std :: cout << * ptr << '\\n' ; // undefined behavior from dereferencing a dangling pointer return 0 ; } \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 2 3 5 6 6 \u4f46\u662f\uff0c\u7ed3\u679c\u4e5f\u53ef\u80fd\u4e0d\u662f\u4e0a\u9762\u8fd9\u6837\uff0c\u56e0\u4e3a ptr \u6240\u6307\u7684\u5bf9\u8c61\u5728 \u79bb\u5f00\u4f5c\u7528\u57df \u65f6(\u5185\u5c42\u8bed\u53e5\u5757)\u4ee5\u53ca\u88ab\u9500\u6bc1\u4e86\uff0c\u6b64\u65f6\u7684 ptr \u662f\u4e00\u4e2a\u60ac\u5782\u6307\u9488\u3002","title":"\u60ac\u5782\u6307\u9488"},{"location":"9-6-Introduction-to-pointers/#_10","text":"\u6307\u9488\u662f\u4fdd\u5b58\u5185\u5b58\u5730\u5740\u7684\u53d8\u91cf\u3002\u53ef\u4ee5\u4f7f\u7528 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u5bf9\u5b83\u4eec\u8fdb\u884c\u89e3\u5f15\u7528\uff0c\u4ee5\u83b7\u53d6\u6307\u9488\u6240\u6301\u6709\u7684\u5730\u5740\u7684\u503c\u3002\u5bf9 \u91ce\u6307\u9488 \u6216 \u60ac\u5782 \u6307\u9488(\u6216\u7a7a\u6307\u9488)\u7684\u89e3\u5f15\u7528\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u5e76\u53ef\u80fd\u5bfc\u81f4\u5e94\u7528\u7a0b\u5e8f\u5d29\u6e83\u3002 \u6307\u9488\u6bd4\u5f15\u7528\u66f4\u7075\u6d3b\uff0c\u4e5f\u66f4\u5371\u9669\u3002\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\u7ee7\u7eed\u63a2\u8ba8\u8fd9\u4e2a\u95ee\u9898\u3002","title":"\u5c0f\u7ed3"},{"location":"9-7-Null-pointers/","tags":["pointer"],"text":"Key Takeaway \u6307\u9488\u53ef\u4ee5\u901a\u8fc7\u503c\u521d\u59cb\u5316\u8bbe\u4e3a\u7a7a\u503c\u3002 int* ptr{}; \u5f53\u9700\u8981\u7a7a\u6307\u9488\u5b57\u9762\u91cf\u7528\u4e8e\u6307\u9488\u7684\u521d\u59cb\u5316\u3001\u8d4b\u503c\u6216\u4f5c\u4e3a\u53c2\u6570\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 nullptr \u5bf9\u7a7a\u6307\u9488\u89e3\u5f15\u7528\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a \u6574\u578b\u503c\u4f1a\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff1a\u6574\u578b\u503c0\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c false \uff0c\u800c\u5176\u4ed6\u4efb\u4f55\u6574\u578b\u503c\u90fd\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c true \u5982\u679c\u6307\u9488\u662f\u7a7a\u6307\u9488\u5219\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u7c7b\u578bfalse\uff0c\u5982\u679c\u4e0d\u662f\u975e\u7a7a\u6307\u9488\u5219\u8f6c\u6362\u4e3atrue \u6761\u4ef6\u5224\u65ad\u53ea\u80fd\u7528\u4e8e\u533a\u5206\u7a7a\u6307\u9488\u4e0e\u975e\u7a7a\u6307\u9488\u3002\u6ca1\u6709\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u786e\u5b9a\u975e\u7a7a\u6307\u9488\u662f\u6307\u5411\u6709\u6548\u5bf9\u8c61\u8fd8\u662f\u60ac\u7a7a\u6307\u9488(\u6307\u5411\u65e0\u6548\u5bf9\u8c61)\u3002\u6307\u9488\u5e94\u8be5\u4fdd\u5b58\u6709\u6548\u5bf9\u8c61\u7684\u5730\u5740\uff0c\u6216\u8005\u8bbe\u7f6e\u4e3anullptr\u3002\u8fd9\u6837\u6211\u4eec\u53ea\u9700\u8981\u6d4b\u8bd5\u6307\u9488\u662f\u5426\u4e3a\u7a7a\uff0c\u5e76\u4e14\u53ef\u4ee5\u5047\u8bbe\u4efb\u4f55\u975e\u7a7a\u6307\u9488\u90fd\u662f\u6709\u6548\u7684\u3002 \u5f53\u4e00\u4e2a\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\uff0c\u6307\u5411\u88ab\u9500\u6bc1\u5bf9\u8c61\u7684\u4efb\u4f55\u6307\u9488\u90fd\u5c06\u6210\u4e3a\u60ac\u5782\u6307\u9488(\u5b83\u4eec\u4e0d\u4f1a\u81ea\u52a8\u8bbe\u7f6e\u4e3a nullptr )\u3002\u68c0\u6d4b\u8fd9\u4e9b\u60c5\u51b5\u5e76\u786e\u4fdd\u8fd9\u4e9b\u6307\u9488\u968f\u540e\u88ab\u8bbe\u7f6e\u4e3a nullptr \u662f\u4f60\u7684\u8d23\u4efb\u3002 \u73b0\u4ee3C++\u4e2d\u5e94\u8be5\u907f\u514d\u4f7f\u75280\u548cNULL\u8fd9\u4e24\u4e2a\u5386\u53f2\u9057\u7559\u7684\u6307\u9488\u5b57\u9762\u91cf\uff08\u5e94\u8be5\u4f7f\u7528 nullpt \u6765\u4ee3\u66ff\uff09 \u5728\u4e0a\u8282\u8bfe\u4e2d ( 9.6 - \u6307\u9488\u7b80\u4ecb )\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u6307\u9488\u7684\u57fa\u672c\u77e5\u8bc6\u2014\u2014\u6307\u9488\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5b83\u6301\u6709\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61\u7684\u5730\u5740\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 \u89e3\u5f15\u7528\u64cd\u4f5c\u7b26 \u5bf9\u5730\u5740\u8fdb\u884c\u89e3\u5f15\u7528\u4ee5\u83b7\u5f97\u8be5\u5730\u5740\u5b58\u653e\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include int main () { int x { 5 }; std :: cout << x << '\\n' ; // print the value of variable x int * ptr { & x }; // ptr holds the address of x std :: cout << * ptr << '\\n' ; // use dereference operator to print the value at the address that ptr is holding (which is x's address) return 0 ; } \u8f93\u51fa\u7ed3\u679c\uff1a 1 2 5 5 \u5728\u4e0a\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u8fd8\u6ce8\u610f\u5230\u6307\u9488\u53ef\u4ee5\u4e0d\u6307\u5411\u4efb\u4f55\u4e1c\u897f\u3002\u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u63a2\u7d22\u8fd9\u79cd\u6307\u9488(\u4ee5\u53ca\u6307\u5411\u865a\u65e0\u7684\u5404\u79cd\u542b\u4e49)\u3002 \u7a7a\u6307\u9488\uff08Null\uff09 \u9664\u4e86\u5185\u5b58\u5730\u5740\uff0c\u6307\u9488\u8fd8\u53ef\u4ee5\u4fdd\u5b58\u4e00\u4e2a\u989d\u5916\u7684\u503c\uff1a\u7a7a\u503c\u3002\u7a7a\u503c(\u901a\u5e38\u7f29\u5199\u4e3anull)\u662f\u4e00\u79cd\u7279\u6b8a\u503c\uff0c\u8868\u793a\u67d0\u7269\u6ca1\u6709\u503c\u3002\u5f53\u6307\u9488\u6301\u6709\u7a7a\u503c\u65f6\uff0c\u610f\u5473\u7740\u8be5\u6307\u9488\u4e0d\u6307\u5411\u4efb\u4f55\u4e1c\u897f\u3002\u8fd9\u6837\u7684\u6307\u9488\u79f0\u4e3a\u7a7a\u6307\u9488\u3002 \u521b\u5efa\u7a7a\u6307\u9488\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u503c\u521d\u59cb\u5316: 1 2 3 4 5 6 int main () { int * ptr {}; // ptr is now a null pointer, and is not holding an address return 0 ; } \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f60\u4e0d\u80fd\u5c06\u6307\u9488\u521d\u59cb\u5316\u4e3a\u4e00\u4e2a\u6709\u6548\u7684\u5730\u5740\uff0c\u8bf7\u4f7f\u7528\u503c\u521d\u59cb\u5316\u5c06\u5176\u521d\u59cb\u5316\u4e3a\u7a7a\u6307\u9488 \u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8d4b\u503c\u6765\u6539\u53d8\u6307\u9488\u6240\u6307\u5411\u7684\u5bf9\u8c61\uff0c\u6240\u4ee5\u6700\u521d\u88ab\u8bbe\u7f6e\u4e3a\u7a7a\u7684\u6307\u9488\u4ee5\u540e\u53ef\u4ee5\u88ab\u66f4\u6539\u4e3a\u6307\u5411\u4e00\u4e2a\u6709\u6548\u7684\u5bf9\u8c61: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { int * ptr {}; // ptr is a null pointer, and is not holding an address int x { 5 }; ptr = & x ; // ptr now pointing at object x (no longer a null pointer) std :: cout << * ptr << '\\n' ; // print value of x through dereferenced ptr return 0 ; } nullptr \u5173\u952e\u5b57 \u7c7b\u4f3c\u4e8e true \u548c false \u7528\u4e8e\u8868\u793a\u5e03\u5c14\u5b57\u9762\u91cf\u503c\uff0c nullptr \u5173\u952e\u5b57\u5219\u7528\u4e8e\u8868\u793a\u7a7a\u6307\u9488\u5b57\u9762\u91cf\u3002\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 nullptr \u663e\u5f0f\u5730\u5c06\u6307\u9488\u521d\u59cb\u5316\u6216\u8d4b\u503c\u4e3a\u7a7a\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 int main () { int * ptr { nullptr }; // can use nullptr to initialize a pointer to be a null pointer int value { 5 }; int * ptr2 { & value }; // ptr2 is a valid pointer ptr2 = nullptr ; // Can assign nullptr to make the pointer a null pointer someFunction ( nullptr ); // we can also pass nullptr to a function that has a pointer parameter return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u6307\u9488 ptr2 \u7684\u503c\u8bbe\u7f6e\u4e3a nullptr \uff0c\u8fd9\u4f7f\u5f97 ptr2 \u79f0\u4e3a\u4e00\u4e2a\u7a7a\u6307\u9488\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u9700\u8981\u7a7a\u6307\u9488\u5b57\u9762\u91cf\u7528\u4e8e\u6307\u9488\u7684\u521d\u59cb\u5316\u3001\u8d4b\u503c\u6216\u4f5c\u4e3a\u53c2\u6570\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 nullptr \u3002 \u5bf9\u7a7a\u6307\u9488\u89e3\u5f15\u7528\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a \u7c7b\u4f3c\u4e8e\u5bf9\u60ac\u5782\u6307\u9488\u6216\u91ce\u6307\u9488\u89e3\u5f15\u7528\uff0c\u5bf9\u7a7a\u6307\u9488\u89e3\u5f15\u7528\u4e5f\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4f1a\u4f7f\u4f60\u7684\u7a0b\u5e8f\u5d29\u6e83\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u8bf4\u660e\u4e86\u8fd9\u4e00\u70b9\uff0c\u5f53\u4f60\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u5b83\u53ef\u80fd\u4f1a\u5d29\u6e83\u6216\u5f02\u5e38\u7ec8\u6b62(\u6765\u5427\uff0c\u5c1d\u8bd5\u4e00\u4e0b\uff0c\u4e0d\u4f1a\u641e\u574f\u4f60\u7684\u673a\u5668\u7684)\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { int * ptr {}; // Create a null pointer std :: cout << * ptr << '\\n' ; // Dereference the null pointer return 0 ; } \u4ece\u6982\u5ff5\u4e0a\u8bb2\uff0c\u8fd9\u662f\u6709\u9053\u7406\u7684\u3002\u5bf9\u6307\u9488\u7684\u89e3\u5f15\u7528\u610f\u5473\u7740\u201c\u5230\u6307\u9488\u6240\u6307\u5411\u7684\u5730\u5740\u5e76\u8bbf\u95ee\u90a3\u91cc\u7684\u503c\u201d\u3002\u7a7a\u6307\u9488\u6ca1\u6709\u5730\u5740\u3002\u56e0\u6b64\uff0c\u5f53\u4f60\u8bd5\u56fe\u8bbf\u95ee\u8be5\u5730\u5740\u7684\u503c\u65f6\uff0c\u5b83\u5e94\u8be5\u505a\u4ec0\u4e48? \u610f\u5916\u5730\u89e3\u5f15\u7528\u7a7a\u6307\u9488\u548c\u60ac\u7a7a\u6307\u9488\u662fC++\u7a0b\u5e8f\u5458\u6700\u5e38\u72af\u7684\u9519\u8bef\u4e4b\u4e00\uff0c\u5e76\u4e14\u53ef\u80fd\u662fC++\u7a0b\u5e8f\u5728\u5b9e\u8df5\u4e2d\u5d29\u6e83\u7684\u6700\u5e38\u89c1\u539f\u56e0\u3002 \u6ce8\u610f \u65e0\u8bba\u4f55\u65f6\u4f7f\u7528\u6307\u9488\uff0c\u90fd\u9700\u8981\u683c\u5916\u5c0f\u5fc3\uff0c\u786e\u4fdd\u4ee3\u7801\u6ca1\u6709\u89e3\u5f15\u7528\u7a7a\u6307\u9488\u6216\u60ac\u6d6e\u6307\u9488\uff0c\u56e0\u4e3a\u8fd9\u5c06\u5bfc\u81f4\u672a\u5b9a\u4e49\u7684\u884c\u4e3a(\u53ef\u80fd\u5bfc\u81f4\u5e94\u7528\u7a0b\u5e8f\u5d29\u6e83)\u3002 \u68c0\u67e5\u7a7a\u6307\u9488 \u6b63\u5982\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u6d4b\u8bd5\u5224\u65ad\u5e03\u5c14\u503c\u662f true \u8fd8\u662f false \u90a3\u6837\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5224\u65ad\u4e00\u4e2a\u6307\u9488\u7684\u503c\u662f\u5426\u662f nullptr \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { int x { 5 }; int * ptr { & x }; // pointers convert to Boolean false if they are null, and Boolean true if they are non-null if ( ptr == nullptr ) // explicit test for equivalence std :: cout << \"ptr is null \\n \" ; else std :: cout << \"ptr is non-null \\n \" ; int * nullPtr {}; std :: cout << \"nullPtr is \" << ( nullPtr == nullptr ? \"null \\n \" : \"non-null \\n \" ); // explicit test for equivalence return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u63a5\u8fc7\u4e3a\uff1a 1 2 ptr is non-null nullPtr is null \u5728 4.9 - \u5e03\u5c14\u503c \u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u77e5\u9053==\u6574\u578b\u503c\u4f1a\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff1a\u6574\u578b\u503c0\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c false \uff0c\u800c\u5176\u4ed6\u4efb\u4f55\u6574\u578b\u503c\u90fd\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c true \u3002== \u7c7b\u4f3c\u5730\uff0c\u6307\u9488\u4e5f\u4f1a\u9690\u5f0f\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff1a\u7a7a\u6307\u9488\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c false \uff0c\u975e\u7a7a\u6307\u9488\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c true \u3002\u8fd9\u6837\u4e00\u6765\u6211\u4eec\u53ef\u4ee5\u4e0d\u5fc5\u663e\u5f0f\u5730\u6d4b\u8bd5 nullptr \uff0c\u53ea\u4f7f\u7528\u9690\u5f0f\u8f6c\u6362\u5230\u5e03\u5c14\u503c\u6765\u6d4b\u8bd5\u6307\u9488\u662f\u5426\u4e3a\u7a7a\u6307\u9488\u5373\u53ef\u3002\u4e0b\u9762\u7684\u7a0b\u5e8f\u548c\u4e4b\u524d\u7684\u7a0b\u5e8f\u6548\u679c\u4e00\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { int x { 5 }; int * ptr { & x }; // \u5982\u679c\u6307\u9488\u662f\u7a7a\u6307\u9488\u5219\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u7c7b\u578bfalse\uff0c\u5982\u679c\u4e0d\u662f\u975e\u7a7a\u6307\u9488\u5219\u8f6c\u6362\u4e3atrue if ( ptr ) // implicit conversion to Boolean std :: cout << \"ptr is non-null \\n \" ; else std :: cout << \"ptr is null \\n \" ; int * nullPtr {}; std :: cout << \"nullPtr is \" << ( nullPtr ? \"non-null \\n \" : \"null \\n \" ); // implicit conversion to Boolean return 0 ; } \u6ce8\u610f \u6761\u4ef6\u5224\u65ad\u53ea\u80fd\u7528\u4e8e\u533a\u5206\u7a7a\u6307\u9488\u4e0e\u975e\u7a7a\u6307\u9488\u3002\u6ca1\u6709\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u786e\u5b9a\u975e\u7a7a\u6307\u9488\u662f\u6307\u5411\u6709\u6548\u5bf9\u8c61\u8fd8\u662f\u60ac\u7a7a\u6307\u9488(\u6307\u5411\u65e0\u6548\u5bf9\u8c61)\u3002 \u4f7f\u7528 nullptr \u907f\u514d\u60ac\u5782\u6307\u9488 \u5bf9\u7a7a\u6307\u9488\u6216\u60ac\u7a7a\u6307\u9488\u7684\u89e3\u5f15\u7528\u5c06\u5bfc\u81f4\u672a\u5b9a\u4e49\u7684\u884c\u4e3a\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u4e0d\u5728\u4ee3\u7801\u4e2d\u505a\u8fd9\u4e24\u4ef6\u4e8b\u3002 \u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u907f\u514d\u5bf9\u7a7a\u6307\u9488\u7684\u89e3\u5f15\u7528\uff0c\u5728\u5c1d\u8bd5\u89e3\u5f15\u7528\u4e4b\u524d\u5148\u5bf9\u6307\u9488\u8fdb\u884c\u5224\u7a7a\u5373\u53ef\uff1a 1 2 3 4 5 // Assume ptr is some pointer that may or may not be a null pointer if ( ptr ) // if ptr is not a null pointer std :: cout << * ptr << '\\n' ; // okay to dereference else // do something else that doesn't involve dereferencing ptr (print an error message, do nothing at all, etc...) \u4f46\u662f\uff0c\u60ac\u5782\u6307\u9488\u5982\u4f55\u5224\u65ad\u5462\uff1f\u56e0\u4e3a\u6ca1\u6709\u529e\u6cd5\u68c0\u6d4b\u6307\u9488\u662f\u5426\u662f \u60ac\u5782 \u72b6\u6001\uff0c\u6240\u4ee5\u5fc5\u987b\u4ece\u6e90\u5934\u4e0a\u907f\u514d\u5728\u7a0b\u5e8f\u4e2d\u51fa\u73b0\u4efb\u4f55\u60ac\u6d6e\u6307\u9488\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u4efb\u4f55\u4e0d\u6307\u5411\u6709\u6548\u5bf9\u8c61\u7684\u6307\u9488\u90fd\u88ab\u8bbe\u7f6e\u4e3a nullptr \u3002 \u8fd9\u6837\uff0c\u5728\u5bf9\u6307\u9488\u8fdb\u884c\u89e3\u5f15\u7528\u4e4b\u524d\uff0c\u6211\u4eec\u53ea\u9700\u8981\u6d4b\u8bd5\u5b83\u662f\u5426\u4e3a\u7a7a\u2014\u2014\u5982\u679c\u5b83\u975e\u7a7a\uff0c\u6211\u4eec\u5c31\u5047\u5b9a\u8be5\u6307\u9488\u4e0d\u662f \u60ac\u5782 \u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u6307\u9488\u5e94\u8be5\u4fdd\u5b58\u6709\u6548\u5bf9\u8c61\u7684\u5730\u5740\uff0c\u6216\u8005\u8bbe\u7f6e\u4e3anullptr\u3002\u8fd9\u6837\u6211\u4eec\u53ea\u9700\u8981\u6d4b\u8bd5\u6307\u9488\u662f\u5426\u4e3a\u7a7a\uff0c\u5e76\u4e14\u53ef\u4ee5\u5047\u8bbe\u4efb\u4f55\u975e\u7a7a\u6307\u9488\u90fd\u662f\u6709\u6548\u7684\u3002 \u4e0d\u5e78\u7684\u662f\uff0c\u907f\u514d\u60ac\u5782\u6307\u9488\u5e76\u4e0d\u5bb9\u6613\uff1a\u5f53\u4e00\u4e2a\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\uff0c\u4efb\u4f55\u6307\u5411\u8be5\u5bf9\u8c61\u7684\u6307\u9488\u90fd\u4f1a\u53d8\u6210\u60ac\u5782\u6307\u9488\uff0c\u800c\u4e14\u8fd9\u4e9b\u6307\u9488\u5e76\u4e0d\u4f1a\u4f1a\u81ea\u52a8\u5730\u8bbe\u7f6e\u4e3a\u7a7a\u6307\u9488\uff01\u56e0\u6b64\uff0c\u7a0b\u5e8f\u5458\u6709\u8d23\u4efb\u786e\u4fdd\u6240\u6709\u6307\u5411\u521a\u521a\u88ab\u9500\u6bc1\u7684\u5bf9\u8c61\u7684\u6307\u9488\u90fd\u88ab\u6b63\u786e\u5730\u8bbe\u7f6e\u4e3a nullptr \u3002 \u6ce8\u610f \u5f53\u4e00\u4e2a\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\uff0c\u6307\u5411\u88ab\u9500\u6bc1\u5bf9\u8c61\u7684\u4efb\u4f55\u6307\u9488\u90fd\u5c06\u6210\u4e3a\u60ac\u5782\u6307\u9488(\u5b83\u4eec\u4e0d\u4f1a\u81ea\u52a8\u8bbe\u7f6e\u4e3a nullptr )\u3002\u68c0\u6d4b\u8fd9\u4e9b\u60c5\u51b5\u5e76\u786e\u4fdd\u8fd9\u4e9b\u6307\u9488\u968f\u540e\u88ab\u8bbe\u7f6e\u4e3a nullptr \u662f\u4f60\u7684\u8d23\u4efb\u3002 \u9057\u7559\u7684\u6307\u9488\u5b57\u9762\u91cf 0 \u548c NULL \u5728\u5386\u53f2\u9057\u7559\u4ee3\u7801\u4e2d\uff0c\u4f60\u53ef\u80fd\u4f1a\u770b\u5230\u9664 nullptr \u4ee5\u5916\u7684\u6307\u9488\u5b57\u9762\u91cf\u3002 \u7b2c\u4e00\u4e2a\u5b57\u9762\u91cf\u662f 0 \u3002\u5728\u6307\u9488\u7684\u8bed\u5883\u4e2d\uff0c 0 \u5c31\u88ab\u8ba4\u4e3a\u662f\u7a7a\u503c\uff0c\u800c\u4e14\u4e5f\u662f\u552f\u4e00\u53ef\u4ee5\u88ab\u8d4b\u503c\u7ed9\u6307\u9488\u7684\u6574\u578b\u5b57\u9762\u91cf\u3002 1 2 3 4 5 6 7 8 9 int main () { float * ptr { 0 }; // ptr is now a null pointer (for example only, don't do this) float * ptr2 ; // ptr2 is uninitialized ptr2 = 0 ; // ptr2 is now a null pointer (for example only, don't do this) return 0 ; } \u9898\u5916\u8bdd \u5728\u73b0\u4ee3\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u4e2d\uff0c\u5730\u5740\u201c0\u201d\u901a\u5e38\u7528\u4e8e\u8868\u793a\u7a7a\u6307\u9488\u3002\u4f46\u662f\uff0cC++\u6807\u51c6\u5e76\u6ca1\u6709\u5bf9\u6b64\u505a\u51fa\u4fdd\u8bc1\uff0c\u800c\u4e14\u4e00\u4e9b\u4f53\u7cfb\u7ed3\u6784\u4e5f\u7684\u786e\u4f1a\u4f7f\u7528\u5176\u4ed6\u503c\u3002\u5f53\u5728\u7a7a\u6307\u9488\u7684\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\u201c0\u201d\u5b57\u9762\u503c\u65f6\uff0c\u5b83\u5c06\u88ab\u8f6c\u6362\u4e3a\u5f53\u524d\u4f53\u7cfb\u7ed3\u6784\u7528\u6765\u8868\u793a\u7a7a\u6307\u9488\u7684\u4efb\u4f55\u5730\u5740\u3002 \u6b64\u5916\uff0c\u8fd8\u6709\u4e00\u4e2a\u540d\u4e3a NULL \u7684\u9884\u5904\u7406\u5668\u5b8f(\u5728 \u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49)\u3002\u8fd9\u4e2a\u5b8f\u662f\u4eceC\u8bed\u8a00\u4e2d\u7ee7\u627f\u7684\uff0c\u5728C\u8bed\u8a00\u4e2d\u5b83\u901a\u5e38\u7528\u4e8e\u6307\u793a\u7a7a\u6307\u9488\u3002 1 2 3 4 5 6 7 8 9 #include // for NULL int main () { double * ptr { NULL }; // ptr is a null pointer double * ptr2 ; // ptr2 is uninitialized ptr2 = NULL ; // ptr2 is now a null pointer } \u73b0\u4ee3C++\u4e2d\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u4e0a\u8ff0\u4e24\u79cd\u5b57\u9762\u91cf\uff08\u5e94\u8be5\u4f7f\u7528 nullpt \u6765\u4ee3\u66ff\uff09\uff0c\u6211\u4eec\u4f1a\u5728 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 \u4e2d\u4ecb\u7ecd\u4e3a\u4ec0\u4e48\u8981\u8fd9\u4e48\u505a\u3002 \u5c3d\u53ef\u80fd\u4f7f\u7528\u5f15\u7528\u800c\u4e0d\u662f\u6307\u9488 \u6307\u9488\u548c\u5f15\u7528\u90fd\u4f7f\u6211\u4eec\u80fd\u591f\u95f4\u63a5\u8bbf\u95ee\u5176\u4ed6\u5bf9\u8c61\u3002 \u6307\u9488\u8fd8\u6709\u5176\u4ed6\u529f\u80fd\uff0c\u53ef\u4ee5\u6539\u53d8\u5b83\u4eec\u6240\u6307\u5411\u7684\u5bf9\u8c61\uff0c\u4e5f\u53ef\u4ee5\u6307\u5411null\u3002\u7136\u800c\uff0c\u8fd9\u4e9b\u6307\u9488\u529f\u80fd\u672c\u8eab\u4e5f\u5f88\u5371\u9669\uff1a\u7a7a\u6307\u9488\u6709\u88ab\u89e3\u5f15\u7528\u7684\u98ce\u9669\uff0c\u800c\u6539\u53d8\u6307\u9488\u6240\u6307\u5411\u7684\u5bf9\u8c61\u7684\u80fd\u529b\u4e5f\u4f7f\u5176\u66f4\u5bb9\u6613\u5bfc\u81f4\u60ac\u5782\u6307\u9488\u7684\u51fa\u73b0\uff1a 1 2 3 4 5 6 7 8 9 10 11 int main () { int * ptr { }; { int x { 5 }; ptr = & x ; // \u5c06\u6307\u9488\u6307\u5411\u4e00\u4e2a\u5373\u5c06\u88ab\u9500\u6bc1\u7684\u5bf9\u8c61\uff08\u5982\u679c\u662f\u5f15\u7528\u5219\u4e0d\u53ef\u80fd\u8fd9\u4e48\u505a\uff09 } // ptr \u73b0\u5728\u662f\u60ac\u5782\u6307\u9488 return 0 ; } \u56e0\u4e3a\u5f15\u7528\u4e0d\u80fd\u88ab\u7ed1\u5b9a\u5230null\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u5fc5\u62c5\u5fc3\u7a7a\u5f15\u7528\u3002\u7531\u4e8e\u5f15\u7528\u5728\u521b\u5efa\u65f6\u5fc5\u987b\u7ed1\u5b9a\u5230\u6709\u6548\u7684\u5bf9\u8c61\uff0c\u7136\u540e\u4e0d\u80fd\u91cd\u65b0\u5b9a\u4f4d\uff0c\u56e0\u6b64\u4e5f\u66f4\u4e0d\u5bb9\u6613\u51fa\u73b0\u60ac\u5782\u6307\u9488\u3002 \u56e0\u4e3a\u5f15\u7528\u66f4\u5b89\u5168\uff0c\u6240\u4ee5\u5e94\u8be5\u4f18\u5148\u4f7f\u7528\u5f15\u7528\u800c\u4e0d\u662f\u6307\u9488\uff0c\u9664\u975e\u9700\u8981\u6307\u9488\u63d0\u4f9b\u7684\u989d\u5916\u529f\u80fd\u3002 \u6700\u4f73\u5b9e\u8df5 \u9664\u975e\u9700\u8981\u6307\u9488\u63d0\u4f9b\u7684\u9644\u52a0\u529f\u80fd\uff0c\u5426\u5219\u6700\u597d\u4f7f\u7528\u5f15\u7528\u800c\u4e0d\u662f\u6307\u9488\u3002","title":"9.7 - \u7a7a\u6307\u9488"},{"location":"9-7-Null-pointers/#null","text":"\u9664\u4e86\u5185\u5b58\u5730\u5740\uff0c\u6307\u9488\u8fd8\u53ef\u4ee5\u4fdd\u5b58\u4e00\u4e2a\u989d\u5916\u7684\u503c\uff1a\u7a7a\u503c\u3002\u7a7a\u503c(\u901a\u5e38\u7f29\u5199\u4e3anull)\u662f\u4e00\u79cd\u7279\u6b8a\u503c\uff0c\u8868\u793a\u67d0\u7269\u6ca1\u6709\u503c\u3002\u5f53\u6307\u9488\u6301\u6709\u7a7a\u503c\u65f6\uff0c\u610f\u5473\u7740\u8be5\u6307\u9488\u4e0d\u6307\u5411\u4efb\u4f55\u4e1c\u897f\u3002\u8fd9\u6837\u7684\u6307\u9488\u79f0\u4e3a\u7a7a\u6307\u9488\u3002 \u521b\u5efa\u7a7a\u6307\u9488\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u503c\u521d\u59cb\u5316: 1 2 3 4 5 6 int main () { int * ptr {}; // ptr is now a null pointer, and is not holding an address return 0 ; } \u6700\u4f73\u5b9e\u8df5 \u5982\u679c\u4f60\u4e0d\u80fd\u5c06\u6307\u9488\u521d\u59cb\u5316\u4e3a\u4e00\u4e2a\u6709\u6548\u7684\u5730\u5740\uff0c\u8bf7\u4f7f\u7528\u503c\u521d\u59cb\u5316\u5c06\u5176\u521d\u59cb\u5316\u4e3a\u7a7a\u6307\u9488 \u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8d4b\u503c\u6765\u6539\u53d8\u6307\u9488\u6240\u6307\u5411\u7684\u5bf9\u8c61\uff0c\u6240\u4ee5\u6700\u521d\u88ab\u8bbe\u7f6e\u4e3a\u7a7a\u7684\u6307\u9488\u4ee5\u540e\u53ef\u4ee5\u88ab\u66f4\u6539\u4e3a\u6307\u5411\u4e00\u4e2a\u6709\u6548\u7684\u5bf9\u8c61: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int main () { int * ptr {}; // ptr is a null pointer, and is not holding an address int x { 5 }; ptr = & x ; // ptr now pointing at object x (no longer a null pointer) std :: cout << * ptr << '\\n' ; // print value of x through dereferenced ptr return 0 ; }","title":"\u7a7a\u6307\u9488\uff08Null\uff09"},{"location":"9-7-Null-pointers/#nullptr","text":"\u7c7b\u4f3c\u4e8e true \u548c false \u7528\u4e8e\u8868\u793a\u5e03\u5c14\u5b57\u9762\u91cf\u503c\uff0c nullptr \u5173\u952e\u5b57\u5219\u7528\u4e8e\u8868\u793a\u7a7a\u6307\u9488\u5b57\u9762\u91cf\u3002\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 nullptr \u663e\u5f0f\u5730\u5c06\u6307\u9488\u521d\u59cb\u5316\u6216\u8d4b\u503c\u4e3a\u7a7a\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 int main () { int * ptr { nullptr }; // can use nullptr to initialize a pointer to be a null pointer int value { 5 }; int * ptr2 { & value }; // ptr2 is a valid pointer ptr2 = nullptr ; // Can assign nullptr to make the pointer a null pointer someFunction ( nullptr ); // we can also pass nullptr to a function that has a pointer parameter return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u6307\u9488 ptr2 \u7684\u503c\u8bbe\u7f6e\u4e3a nullptr \uff0c\u8fd9\u4f7f\u5f97 ptr2 \u79f0\u4e3a\u4e00\u4e2a\u7a7a\u6307\u9488\u3002 \u6700\u4f73\u5b9e\u8df5 \u5f53\u9700\u8981\u7a7a\u6307\u9488\u5b57\u9762\u91cf\u7528\u4e8e\u6307\u9488\u7684\u521d\u59cb\u5316\u3001\u8d4b\u503c\u6216\u4f5c\u4e3a\u53c2\u6570\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 nullptr \u3002","title":"nullptr \u5173\u952e\u5b57"},{"location":"9-7-Null-pointers/#_1","text":"\u7c7b\u4f3c\u4e8e\u5bf9\u60ac\u5782\u6307\u9488\u6216\u91ce\u6307\u9488\u89e3\u5f15\u7528\uff0c\u5bf9\u7a7a\u6307\u9488\u89e3\u5f15\u7528\u4e5f\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4f1a\u4f7f\u4f60\u7684\u7a0b\u5e8f\u5d29\u6e83\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u8bf4\u660e\u4e86\u8fd9\u4e00\u70b9\uff0c\u5f53\u4f60\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u5b83\u53ef\u80fd\u4f1a\u5d29\u6e83\u6216\u5f02\u5e38\u7ec8\u6b62(\u6765\u5427\uff0c\u5c1d\u8bd5\u4e00\u4e0b\uff0c\u4e0d\u4f1a\u641e\u574f\u4f60\u7684\u673a\u5668\u7684)\uff1a 1 2 3 4 5 6 7 8 9 #include int main () { int * ptr {}; // Create a null pointer std :: cout << * ptr << '\\n' ; // Dereference the null pointer return 0 ; } \u4ece\u6982\u5ff5\u4e0a\u8bb2\uff0c\u8fd9\u662f\u6709\u9053\u7406\u7684\u3002\u5bf9\u6307\u9488\u7684\u89e3\u5f15\u7528\u610f\u5473\u7740\u201c\u5230\u6307\u9488\u6240\u6307\u5411\u7684\u5730\u5740\u5e76\u8bbf\u95ee\u90a3\u91cc\u7684\u503c\u201d\u3002\u7a7a\u6307\u9488\u6ca1\u6709\u5730\u5740\u3002\u56e0\u6b64\uff0c\u5f53\u4f60\u8bd5\u56fe\u8bbf\u95ee\u8be5\u5730\u5740\u7684\u503c\u65f6\uff0c\u5b83\u5e94\u8be5\u505a\u4ec0\u4e48? \u610f\u5916\u5730\u89e3\u5f15\u7528\u7a7a\u6307\u9488\u548c\u60ac\u7a7a\u6307\u9488\u662fC++\u7a0b\u5e8f\u5458\u6700\u5e38\u72af\u7684\u9519\u8bef\u4e4b\u4e00\uff0c\u5e76\u4e14\u53ef\u80fd\u662fC++\u7a0b\u5e8f\u5728\u5b9e\u8df5\u4e2d\u5d29\u6e83\u7684\u6700\u5e38\u89c1\u539f\u56e0\u3002 \u6ce8\u610f \u65e0\u8bba\u4f55\u65f6\u4f7f\u7528\u6307\u9488\uff0c\u90fd\u9700\u8981\u683c\u5916\u5c0f\u5fc3\uff0c\u786e\u4fdd\u4ee3\u7801\u6ca1\u6709\u89e3\u5f15\u7528\u7a7a\u6307\u9488\u6216\u60ac\u6d6e\u6307\u9488\uff0c\u56e0\u4e3a\u8fd9\u5c06\u5bfc\u81f4\u672a\u5b9a\u4e49\u7684\u884c\u4e3a(\u53ef\u80fd\u5bfc\u81f4\u5e94\u7528\u7a0b\u5e8f\u5d29\u6e83)\u3002","title":"\u5bf9\u7a7a\u6307\u9488\u89e3\u5f15\u7528\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a"},{"location":"9-7-Null-pointers/#_2","text":"\u6b63\u5982\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u6d4b\u8bd5\u5224\u65ad\u5e03\u5c14\u503c\u662f true \u8fd8\u662f false \u90a3\u6837\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5224\u65ad\u4e00\u4e2a\u6307\u9488\u7684\u503c\u662f\u5426\u662f nullptr \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { int x { 5 }; int * ptr { & x }; // pointers convert to Boolean false if they are null, and Boolean true if they are non-null if ( ptr == nullptr ) // explicit test for equivalence std :: cout << \"ptr is null \\n \" ; else std :: cout << \"ptr is non-null \\n \" ; int * nullPtr {}; std :: cout << \"nullPtr is \" << ( nullPtr == nullptr ? \"null \\n \" : \"non-null \\n \" ); // explicit test for equivalence return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u63a5\u8fc7\u4e3a\uff1a 1 2 ptr is non-null nullPtr is null \u5728 4.9 - \u5e03\u5c14\u503c \u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u77e5\u9053==\u6574\u578b\u503c\u4f1a\u88ab\u9690\u5f0f\u5730\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff1a\u6574\u578b\u503c0\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c false \uff0c\u800c\u5176\u4ed6\u4efb\u4f55\u6574\u578b\u503c\u90fd\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c true \u3002== \u7c7b\u4f3c\u5730\uff0c\u6307\u9488\u4e5f\u4f1a\u9690\u5f0f\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff1a\u7a7a\u6307\u9488\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c false \uff0c\u975e\u7a7a\u6307\u9488\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c true \u3002\u8fd9\u6837\u4e00\u6765\u6211\u4eec\u53ef\u4ee5\u4e0d\u5fc5\u663e\u5f0f\u5730\u6d4b\u8bd5 nullptr \uff0c\u53ea\u4f7f\u7528\u9690\u5f0f\u8f6c\u6362\u5230\u5e03\u5c14\u503c\u6765\u6d4b\u8bd5\u6307\u9488\u662f\u5426\u4e3a\u7a7a\u6307\u9488\u5373\u53ef\u3002\u4e0b\u9762\u7684\u7a0b\u5e8f\u548c\u4e4b\u524d\u7684\u7a0b\u5e8f\u6548\u679c\u4e00\u6837\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include int main () { int x { 5 }; int * ptr { & x }; // \u5982\u679c\u6307\u9488\u662f\u7a7a\u6307\u9488\u5219\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u7c7b\u578bfalse\uff0c\u5982\u679c\u4e0d\u662f\u975e\u7a7a\u6307\u9488\u5219\u8f6c\u6362\u4e3atrue if ( ptr ) // implicit conversion to Boolean std :: cout << \"ptr is non-null \\n \" ; else std :: cout << \"ptr is null \\n \" ; int * nullPtr {}; std :: cout << \"nullPtr is \" << ( nullPtr ? \"non-null \\n \" : \"null \\n \" ); // implicit conversion to Boolean return 0 ; } \u6ce8\u610f \u6761\u4ef6\u5224\u65ad\u53ea\u80fd\u7528\u4e8e\u533a\u5206\u7a7a\u6307\u9488\u4e0e\u975e\u7a7a\u6307\u9488\u3002\u6ca1\u6709\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u786e\u5b9a\u975e\u7a7a\u6307\u9488\u662f\u6307\u5411\u6709\u6548\u5bf9\u8c61\u8fd8\u662f\u60ac\u7a7a\u6307\u9488(\u6307\u5411\u65e0\u6548\u5bf9\u8c61)\u3002","title":"\u68c0\u67e5\u7a7a\u6307\u9488"},{"location":"9-7-Null-pointers/#nullptr_1","text":"\u5bf9\u7a7a\u6307\u9488\u6216\u60ac\u7a7a\u6307\u9488\u7684\u89e3\u5f15\u7528\u5c06\u5bfc\u81f4\u672a\u5b9a\u4e49\u7684\u884c\u4e3a\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u4e0d\u5728\u4ee3\u7801\u4e2d\u505a\u8fd9\u4e24\u4ef6\u4e8b\u3002 \u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u907f\u514d\u5bf9\u7a7a\u6307\u9488\u7684\u89e3\u5f15\u7528\uff0c\u5728\u5c1d\u8bd5\u89e3\u5f15\u7528\u4e4b\u524d\u5148\u5bf9\u6307\u9488\u8fdb\u884c\u5224\u7a7a\u5373\u53ef\uff1a 1 2 3 4 5 // Assume ptr is some pointer that may or may not be a null pointer if ( ptr ) // if ptr is not a null pointer std :: cout << * ptr << '\\n' ; // okay to dereference else // do something else that doesn't involve dereferencing ptr (print an error message, do nothing at all, etc...) \u4f46\u662f\uff0c\u60ac\u5782\u6307\u9488\u5982\u4f55\u5224\u65ad\u5462\uff1f\u56e0\u4e3a\u6ca1\u6709\u529e\u6cd5\u68c0\u6d4b\u6307\u9488\u662f\u5426\u662f \u60ac\u5782 \u72b6\u6001\uff0c\u6240\u4ee5\u5fc5\u987b\u4ece\u6e90\u5934\u4e0a\u907f\u514d\u5728\u7a0b\u5e8f\u4e2d\u51fa\u73b0\u4efb\u4f55\u60ac\u6d6e\u6307\u9488\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u4efb\u4f55\u4e0d\u6307\u5411\u6709\u6548\u5bf9\u8c61\u7684\u6307\u9488\u90fd\u88ab\u8bbe\u7f6e\u4e3a nullptr \u3002 \u8fd9\u6837\uff0c\u5728\u5bf9\u6307\u9488\u8fdb\u884c\u89e3\u5f15\u7528\u4e4b\u524d\uff0c\u6211\u4eec\u53ea\u9700\u8981\u6d4b\u8bd5\u5b83\u662f\u5426\u4e3a\u7a7a\u2014\u2014\u5982\u679c\u5b83\u975e\u7a7a\uff0c\u6211\u4eec\u5c31\u5047\u5b9a\u8be5\u6307\u9488\u4e0d\u662f \u60ac\u5782 \u7684\u3002 \u6700\u4f73\u5b9e\u8df5 \u6307\u9488\u5e94\u8be5\u4fdd\u5b58\u6709\u6548\u5bf9\u8c61\u7684\u5730\u5740\uff0c\u6216\u8005\u8bbe\u7f6e\u4e3anullptr\u3002\u8fd9\u6837\u6211\u4eec\u53ea\u9700\u8981\u6d4b\u8bd5\u6307\u9488\u662f\u5426\u4e3a\u7a7a\uff0c\u5e76\u4e14\u53ef\u4ee5\u5047\u8bbe\u4efb\u4f55\u975e\u7a7a\u6307\u9488\u90fd\u662f\u6709\u6548\u7684\u3002 \u4e0d\u5e78\u7684\u662f\uff0c\u907f\u514d\u60ac\u5782\u6307\u9488\u5e76\u4e0d\u5bb9\u6613\uff1a\u5f53\u4e00\u4e2a\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\uff0c\u4efb\u4f55\u6307\u5411\u8be5\u5bf9\u8c61\u7684\u6307\u9488\u90fd\u4f1a\u53d8\u6210\u60ac\u5782\u6307\u9488\uff0c\u800c\u4e14\u8fd9\u4e9b\u6307\u9488\u5e76\u4e0d\u4f1a\u4f1a\u81ea\u52a8\u5730\u8bbe\u7f6e\u4e3a\u7a7a\u6307\u9488\uff01\u56e0\u6b64\uff0c\u7a0b\u5e8f\u5458\u6709\u8d23\u4efb\u786e\u4fdd\u6240\u6709\u6307\u5411\u521a\u521a\u88ab\u9500\u6bc1\u7684\u5bf9\u8c61\u7684\u6307\u9488\u90fd\u88ab\u6b63\u786e\u5730\u8bbe\u7f6e\u4e3a nullptr \u3002 \u6ce8\u610f \u5f53\u4e00\u4e2a\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\uff0c\u6307\u5411\u88ab\u9500\u6bc1\u5bf9\u8c61\u7684\u4efb\u4f55\u6307\u9488\u90fd\u5c06\u6210\u4e3a\u60ac\u5782\u6307\u9488(\u5b83\u4eec\u4e0d\u4f1a\u81ea\u52a8\u8bbe\u7f6e\u4e3a nullptr )\u3002\u68c0\u6d4b\u8fd9\u4e9b\u60c5\u51b5\u5e76\u786e\u4fdd\u8fd9\u4e9b\u6307\u9488\u968f\u540e\u88ab\u8bbe\u7f6e\u4e3a nullptr \u662f\u4f60\u7684\u8d23\u4efb\u3002","title":"\u4f7f\u7528 nullptr \u907f\u514d\u60ac\u5782\u6307\u9488"},{"location":"9-7-Null-pointers/#0-null","text":"\u5728\u5386\u53f2\u9057\u7559\u4ee3\u7801\u4e2d\uff0c\u4f60\u53ef\u80fd\u4f1a\u770b\u5230\u9664 nullptr \u4ee5\u5916\u7684\u6307\u9488\u5b57\u9762\u91cf\u3002 \u7b2c\u4e00\u4e2a\u5b57\u9762\u91cf\u662f 0 \u3002\u5728\u6307\u9488\u7684\u8bed\u5883\u4e2d\uff0c 0 \u5c31\u88ab\u8ba4\u4e3a\u662f\u7a7a\u503c\uff0c\u800c\u4e14\u4e5f\u662f\u552f\u4e00\u53ef\u4ee5\u88ab\u8d4b\u503c\u7ed9\u6307\u9488\u7684\u6574\u578b\u5b57\u9762\u91cf\u3002 1 2 3 4 5 6 7 8 9 int main () { float * ptr { 0 }; // ptr is now a null pointer (for example only, don't do this) float * ptr2 ; // ptr2 is uninitialized ptr2 = 0 ; // ptr2 is now a null pointer (for example only, don't do this) return 0 ; } \u9898\u5916\u8bdd \u5728\u73b0\u4ee3\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u4e2d\uff0c\u5730\u5740\u201c0\u201d\u901a\u5e38\u7528\u4e8e\u8868\u793a\u7a7a\u6307\u9488\u3002\u4f46\u662f\uff0cC++\u6807\u51c6\u5e76\u6ca1\u6709\u5bf9\u6b64\u505a\u51fa\u4fdd\u8bc1\uff0c\u800c\u4e14\u4e00\u4e9b\u4f53\u7cfb\u7ed3\u6784\u4e5f\u7684\u786e\u4f1a\u4f7f\u7528\u5176\u4ed6\u503c\u3002\u5f53\u5728\u7a7a\u6307\u9488\u7684\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\u201c0\u201d\u5b57\u9762\u503c\u65f6\uff0c\u5b83\u5c06\u88ab\u8f6c\u6362\u4e3a\u5f53\u524d\u4f53\u7cfb\u7ed3\u6784\u7528\u6765\u8868\u793a\u7a7a\u6307\u9488\u7684\u4efb\u4f55\u5730\u5740\u3002 \u6b64\u5916\uff0c\u8fd8\u6709\u4e00\u4e2a\u540d\u4e3a NULL \u7684\u9884\u5904\u7406\u5668\u5b8f(\u5728 \u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49)\u3002\u8fd9\u4e2a\u5b8f\u662f\u4eceC\u8bed\u8a00\u4e2d\u7ee7\u627f\u7684\uff0c\u5728C\u8bed\u8a00\u4e2d\u5b83\u901a\u5e38\u7528\u4e8e\u6307\u793a\u7a7a\u6307\u9488\u3002 1 2 3 4 5 6 7 8 9 #include // for NULL int main () { double * ptr { NULL }; // ptr is a null pointer double * ptr2 ; // ptr2 is uninitialized ptr2 = NULL ; // ptr2 is now a null pointer } \u73b0\u4ee3C++\u4e2d\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u4e0a\u8ff0\u4e24\u79cd\u5b57\u9762\u91cf\uff08\u5e94\u8be5\u4f7f\u7528 nullpt \u6765\u4ee3\u66ff\uff09\uff0c\u6211\u4eec\u4f1a\u5728 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 \u4e2d\u4ecb\u7ecd\u4e3a\u4ec0\u4e48\u8981\u8fd9\u4e48\u505a\u3002","title":"\u9057\u7559\u7684\u6307\u9488\u5b57\u9762\u91cf 0 \u548c NULL"},{"location":"9-7-Null-pointers/#_3","text":"\u6307\u9488\u548c\u5f15\u7528\u90fd\u4f7f\u6211\u4eec\u80fd\u591f\u95f4\u63a5\u8bbf\u95ee\u5176\u4ed6\u5bf9\u8c61\u3002 \u6307\u9488\u8fd8\u6709\u5176\u4ed6\u529f\u80fd\uff0c\u53ef\u4ee5\u6539\u53d8\u5b83\u4eec\u6240\u6307\u5411\u7684\u5bf9\u8c61\uff0c\u4e5f\u53ef\u4ee5\u6307\u5411null\u3002\u7136\u800c\uff0c\u8fd9\u4e9b\u6307\u9488\u529f\u80fd\u672c\u8eab\u4e5f\u5f88\u5371\u9669\uff1a\u7a7a\u6307\u9488\u6709\u88ab\u89e3\u5f15\u7528\u7684\u98ce\u9669\uff0c\u800c\u6539\u53d8\u6307\u9488\u6240\u6307\u5411\u7684\u5bf9\u8c61\u7684\u80fd\u529b\u4e5f\u4f7f\u5176\u66f4\u5bb9\u6613\u5bfc\u81f4\u60ac\u5782\u6307\u9488\u7684\u51fa\u73b0\uff1a 1 2 3 4 5 6 7 8 9 10 11 int main () { int * ptr { }; { int x { 5 }; ptr = & x ; // \u5c06\u6307\u9488\u6307\u5411\u4e00\u4e2a\u5373\u5c06\u88ab\u9500\u6bc1\u7684\u5bf9\u8c61\uff08\u5982\u679c\u662f\u5f15\u7528\u5219\u4e0d\u53ef\u80fd\u8fd9\u4e48\u505a\uff09 } // ptr \u73b0\u5728\u662f\u60ac\u5782\u6307\u9488 return 0 ; } \u56e0\u4e3a\u5f15\u7528\u4e0d\u80fd\u88ab\u7ed1\u5b9a\u5230null\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u5fc5\u62c5\u5fc3\u7a7a\u5f15\u7528\u3002\u7531\u4e8e\u5f15\u7528\u5728\u521b\u5efa\u65f6\u5fc5\u987b\u7ed1\u5b9a\u5230\u6709\u6548\u7684\u5bf9\u8c61\uff0c\u7136\u540e\u4e0d\u80fd\u91cd\u65b0\u5b9a\u4f4d\uff0c\u56e0\u6b64\u4e5f\u66f4\u4e0d\u5bb9\u6613\u51fa\u73b0\u60ac\u5782\u6307\u9488\u3002 \u56e0\u4e3a\u5f15\u7528\u66f4\u5b89\u5168\uff0c\u6240\u4ee5\u5e94\u8be5\u4f18\u5148\u4f7f\u7528\u5f15\u7528\u800c\u4e0d\u662f\u6307\u9488\uff0c\u9664\u975e\u9700\u8981\u6307\u9488\u63d0\u4f9b\u7684\u989d\u5916\u529f\u80fd\u3002 \u6700\u4f73\u5b9e\u8df5 \u9664\u975e\u9700\u8981\u6307\u9488\u63d0\u4f9b\u7684\u9644\u52a0\u529f\u80fd\uff0c\u5426\u5219\u6700\u597d\u4f7f\u7528\u5f15\u7528\u800c\u4e0d\u662f\u6307\u9488\u3002","title":"\u5c3d\u53ef\u80fd\u4f7f\u7528\u5f15\u7528\u800c\u4e0d\u662f\u6307\u9488"},{"location":"9-8-Pointers-and-const/","tags":["pointer","const"],"text":"Key Takeaway \u7c7b\u578b\u524dconst\u8868\u793a\u7c7b\u578b\u662fconst\uff0c\u6307\u9488\u524d\uff08\u661f\u53f7\u540e\uff09const\u8868\u793a\u6307\u9488\u662fconst \u5c31\u50cf\u5bf9const\u53d8\u91cf\u7684\u5f15\u7528\u4e00\u6837\uff0c\u6307\u5411const\u7684\u6307\u9488\u4e5f\u53ef\u4ee5\u6307\u5411\u975econst\u53d8\u91cf\u3002\u6307\u5411const\u7684\u6307\u9488\u5c06\u88ab\u6307\u5411\u7684\u503c\u89c6\u4e3a\u5e38\u91cf\uff0c\u800c\u4e0d\u7ba1\u8be5\u5730\u5740\u7684\u5bf9\u8c61\u6700\u521d\u662f\u5426\u5b9a\u4e49\u4e3aconst \u603b\u800c\u8a00\u4e4b\uff0c\u4f60\u53ea\u9700\u8981\u8bb0\u4f4f4\u6761\u89c4\u5219\uff0c\u800c\u4e14\u5b83\u4eec\u975e\u5e38\u7b26\u5408\u903b\u8f91: \u975econst\u6307\u9488\u53ef\u4ee5\u88ab\u8d4b\u4e88\u53e6\u4e00\u4e2a\u5730\u5740\u6765\u6539\u53d8\u5b83\u6240\u6307\u5411\u7684\u5bf9\u8c61\uff1b const\u6307\u9488\u603b\u662f\u6307\u5411\u76f8\u540c\u7684\u5730\u5740\uff0c\u4e14\u8be5\u5730\u5740\u4e0d\u80fd\u66f4\u6539\uff1b \u6307\u5411\u975econst\u503c\u7684\u6307\u9488\u53ef\u4ee5\u6539\u53d8\u5b83\u6240\u6307\u5411\u7684\u503c\uff0c\u4f46\u8be5\u6307\u9488\u4e0d\u80fd\u6307\u5411const\u7c7b\u578b\u53d8\u91cf\uff1b \u6307\u5411const\u503c\u7684\u6307\u9488\u5728\u901a\u8fc7\u8be5\u6307\u9488\u8bbf\u95ee\u65f6\u5c06\u8be5\u503c\u89c6\u4e3aconst\uff0c\u56e0\u6b64\u4e0d\u80fd\u66f4\u6539\u5b83\u6240\u6307\u5411\u7684\u503c\u3002\u5b83\u4eec\u53ef\u4ee5\u6307\u5411const\u6216\u975econst\u7684 \u5de6\u503c (\u4f46\u4e0d\u80fd\u6307\u5411\u6ca1\u6709\u5730\u5740\u7684 \u53f3\u503c ) \u8bf7\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 int main () { int x { 5 }; int * ptr { & x }; // ptr is a normal (non-const) pointer int y { 6 }; ptr = & y ; // we can point at another value * ptr = 7 ; // we can change the value at the address being held return 0 ; } \u4f7f\u7528\u666e\u901a(\u975e const )\u6307\u9488\uff0c\u65e2\u53ef\u4ee5\u66f4\u6539\u6307\u9488\u6240\u6307\u5411\u7684\u5bf9\u8c61(\u901a\u8fc7\u4e3a\u6307\u9488\u5206\u914d\u4e00\u4e2a\u65b0\u5730\u5740\u6765\u4fdd\u5b58)\uff0c\u4e5f\u53ef\u4ee5\u66f4\u6539\u88ab\u4fdd\u5b58\u5730\u5740\u5904\u7684\u503c(\u901a\u8fc7\u4e3a\u89e3\u5f15\u7528\u6307\u9488\u5206\u914d\u4e00\u4e2a\u65b0\u503c)\u3002 \u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u8981\u6307\u5411\u7684\u503c\u662f const \uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462? 1 2 3 4 5 6 7 int main () { const int x { 5 }; // x \u662f const int * ptr { & x }; // \u7f16\u8bd1\u9519\u8bef\uff1a\u4e0d\u80fd\u628aconst int* \u8f6c\u6362\u4e3a int* return 0 ; } \u4e0a\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u65e0\u6cd5\u7f16\u8bd1\u2014\u2014 \u666e\u901a\u6307\u9488\u4e0d\u80fd\u6307\u5411const\u53d8\u91cf \u3002\u8fd9\u6837\u7684\u9650\u5236\u662f\u6709\u5fc5\u8981\u7684\uff0c\u56e0\u4e3aconst\u53d8\u91cf\u7684\u503c\u662f\u4e0d\u80fd\u66f4\u6539\u7684\u3002\u5141\u8bb8\u7a0b\u5e8f\u5458\u5c06\u975econst\u6307\u9488\u8bbe\u7f6e\u4e3aconst\u503c\u5c06\u5141\u8bb8\u7a0b\u5e8f\u5458\u89e3\u5f15\u7528\u8be5\u6307\u9488\u5e76\u66f4\u6539\u8be5\u503c\u3002\u8fd9\u5c06\u8fdd\u53cd\u53d8\u91cf\u7684\u5e38\u91cf\u6027\u3002 \u6307\u5411\u5e38\u91cf\u7684\u6307\u9488 \u6307\u9488\u6307\u5411\u4e00\u4e2aconst\u7684\u503c\uff08\u4e00\u822c\u7b80\u79f0\u4e3a \u6307\u5411\u5e38\u91cf\u7684\u6307\u9488 \uff09\uff0c\u4e5f\u5c31\u662f\u8bf4\u4e00\u4e2a\u975e\u5e38\u91cf\u7684\u6307\u9488\u6307\u5411\u4e86\u4e00\u4e2a\u5e38\u91cf\u503c\u3002 \u58f0\u660e\u4e00\u4e2a\u53ef\u4ee5\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488\uff0c\u9700\u8981\u5728\u6307\u9488\u7684\u6570\u636e\u7c7b\u578b\u524d\u6dfb\u52a0\u5173\u952e\u5b57 const \uff1a 1 2 3 4 5 6 7 8 9 int main () { const int x { 5 }; const int * ptr { & x }; // okay: ptr is pointing to a \"const int\" * ptr = 6 ; // not allowed: we can't change a const value return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d ptr \u6307\u5411\u4e86\u4e00\u4e2a const int \u3002\u56e0\u4e3a\u6307\u9488\u6240\u6307\u7684\u6570\u636e\u7c7b\u578b\u662fconst\uff0c\u6240\u4ee5\u4e0d\u80fd\u901a\u8fc7\u6307\u9488\u4fee\u6539\u5b83\u6307\u7684\u8fd9\u4e2a\u503c\u3002 \u4f46\u662f\uff0c\u56e0\u4e3a\u6307\u9488\u672c\u8eab\u4e0d\u662fconst\u7684\uff08\u53ea\u662f\u6307\u5411\u4e00\u4e2aconst\u7c7b\u578b\u7684\u503c\u53d8\u91cf\uff09\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4fee\u6539\u6307\u9488\u672c\u8eab\u6240\u6301\u6709\u7684\u5730\u5740\u4f7f\u5176\u6307\u5411\u5176\u4ed6\u53d8\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { const int x { 5 }; const int * ptr { & x }; // ptr \u6307\u5411 const int x const int y { 6 }; ptr = & y ; // okay: \u91cd\u65b0\u6307\u5411 const int y return 0 ; } \u5c31\u50cf\u5bf9const\u53d8\u91cf\u7684\u5f15\u7528\u4e00\u6837\uff0c\u6307\u5411const\u7684\u6307\u9488\u4e5f\u53ef\u4ee5\u6307\u5411\u975econst\u53d8\u91cf\u3002\u6307\u5411const\u7684\u6307\u9488\u5c06\u88ab\u6307\u5411\u7684\u503c\u89c6\u4e3a\u5e38\u91cf\uff0c\u800c\u4e0d\u7ba1\u8be5\u5730\u5740\u7684\u5bf9\u8c61\u6700\u521d\u662f\u5426\u5b9a\u4e49\u4e3aconst: 1 2 3 4 5 6 7 8 9 10 int main () { int x { 5 }; // non-const const int * ptr { & x }; // ptr points to a \"const int\" * ptr = 6 ; // not allowed: ptr points to a \"const int\" so we can't change the value through ptr x = 6 ; // allowed: the value is still non-const when accessed through non-const identifier x return 0 ; } \u5e38\u91cf\u6307\u9488 \u6307\u9488\u81ea\u8eab\u4e5f\u53ef\u4ee5\u662f\u5e38\u91cf\u3002\u4e00\u4e2a\u5e38\u91cf\u6307\u9488\u7684\u5730\u5740\uff0c\u5728\u521d\u59cb\u5316\u4e4b\u540e\u662f\u4e0d\u80fd\u591f\u88ab\u4fee\u6539\u7684\u3002 \u58f0\u660e\u4e00\u4e2a\u5e38\u91cf\u6307\u9488\uff0c\u53ea\u9700\u8981\u5728\u6307\u9488\u58f0\u660e\u7684\u661f\u53f7\u540e\u6dfb\u52a0 const \u5173\u952e\u5b57\uff1a 1 2 3 4 5 6 7 int main () { int x { 5 }; int * const ptr { & x }; // const after the asterisk means this is a const pointer return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c ptr \u662f\u4e00\u4e2a\u6307\u5411(\u975e const ) int \u503c\u7684 const \u6307\u9488\u3002 \u5c31\u50cf\u666e\u901a\u7684 const \u53d8\u91cf\u4e00\u6837\uff0c const \u6307\u9488\u5fc5\u987b\u5728\u5b9a\u4e49\u65f6\u521d\u59cb\u5316\uff0c\u5e76\u4e14\u8fd9\u4e2a\u503c\u4e0d\u80fd\u901a\u8fc7\u8d4b\u503c\u6765\u6539\u53d8: 1 2 3 4 5 6 7 8 9 10 int main () { int x { 5 }; int y { 6 }; int * const ptr { & x }; // okay: the const pointer is initialized to the address of x ptr = & y ; // error: once initialized, a const pointer can not be changed. return 0 ; } \u4e0d\u8fc7\uff0c\u8be5\u6307\u9488\u6240\u6307\u7684\u5bf9\u8c61\u5e76\u4e0d\u662f\u4e00\u4e2a\u5e38\u91cf\uff0c\u6240\u4ee5\u4ecd\u65e7\u53ef\u4ee5\u901a\u8fc7\u5bf9\u6307\u9488\u89e3\u5f15\u7528\u7136\u540e\u4fee\u6539\u8be5\u53d8\u91cf\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 9 int main () { int x { 5 }; int * const ptr { & x }; // ptr will always point to x * ptr = 6 ; // okay: the value being pointed to is non-const return 0 ; } \u6307\u5411\u5e38\u91cf\u7684\u5e38\u91cf\u6307\u9488 \u6700\u540e\u4e00\u79cd\u7ec4\u5408\uff0c\u5373\u6307\u9488\u53ef\u4ee5\u88ab\u58f0\u660e\u4e3a\u5e38\u91cf\u6307\u9488\u540c\u65f6\u5b83\u6240\u6307\u7684\u503c\u4e5f\u662f\u5e38\u91cf\uff0c\u5728\u7c7b\u578b\u524d\u548c\u661f\u53f7\u540e\u5206\u522b\u6dfb\u52a0const\u5173\u952e\u5b57\u5373\u53ef\uff1a 1 2 3 4 5 6 7 int main () { int value { 5 }; const int * const ptr { & value }; // a const pointer to a const value return 0 ; } \u6307\u5411const\u503c\u7684 \u5e38\u91cf\u6307\u9488 \u4e0d\u80fd\u6539\u53d8\u5176\u5730\u5740\uff0c\u4e5f\u4e0d\u80fd\u901a\u8fc7\u8be5\u6307\u9488\u6539\u53d8\u5b83\u6240\u6307\u5411\u7684\u503c\u3002\u5b83\u53ea\u80fd\u901a\u8fc7\u89e3\u5f15\u7528\u4ee5\u83b7\u5f97\u5b83\u6240\u6307\u5411\u7684\u503c\u3002 \u590d\u4e60 \u603b\u800c\u8a00\u4e4b\uff0c\u4f60\u53ea\u9700\u8981\u8bb0\u4f4f4\u6761\u89c4\u5219\uff0c\u800c\u4e14\u5b83\u4eec\u975e\u5e38\u7b26\u5408\u903b\u8f91: \u975econst\u6307\u9488\u53ef\u4ee5\u88ab\u8d4b\u4e88\u53e6\u4e00\u4e2a\u5730\u5740\u6765\u6539\u53d8\u5b83\u6240\u6307\u5411\u7684\u5bf9\u8c61\uff1b const\u6307\u9488\u603b\u662f\u6307\u5411\u76f8\u540c\u7684\u5730\u5740\uff0c\u4e14\u8be5\u5730\u5740\u4e0d\u80fd\u66f4\u6539\uff1b \u6307\u5411\u975econst\u503c\u7684\u6307\u9488\u53ef\u4ee5\u6539\u53d8\u5b83\u6240\u6307\u5411\u7684\u503c\uff0c\u4f46\u8be5\u6307\u9488\u4e0d\u80fd\u6307\u5411const\u7c7b\u578b\u53d8\u91cf\uff1b \u6307\u5411const\u503c\u7684\u6307\u9488\u5728\u901a\u8fc7\u8be5\u6307\u9488\u8bbf\u95ee\u65f6\u5c06\u8be5\u503c\u89c6\u4e3aconst\uff0c\u56e0\u6b64\u4e0d\u80fd\u66f4\u6539\u5b83\u6240\u6307\u5411\u7684\u503c\u3002\u5b83\u4eec\u53ef\u4ee5\u6307\u5411const\u6216\u975econst\u7684 \u5de6\u503c (\u4f46\u4e0d\u80fd\u6307\u5411\u6ca1\u6709\u5730\u5740\u7684 \u53f3\u503c ) \u5728\u914d\u5408const\u5173\u952e\u5b57\u4f7f\u7528\u6307\u9488\u65f6\uff0c\u58f0\u660e\u7684\u8bed\u6cd5\u8fd8\u662f\u6709\u70b9\u590d\u6742\u7684\uff1a \u6307\u9488\u7684\u7c7b\u578b\u51b3\u5b9a\u4e86\u5b83\u80fd\u6307\u5411\u5bf9\u8c61\u7684\u7c7b\u578b\u3002\u6240\u4ee5const\u5bf9\u6307\u9488\u7c7b\u578b\u7684\u4fee\u9970\u51b3\u5b9a\u4e86\u6307\u9488\u6240\u6307\u53d8\u91cf\u662f\u5426\u4e3aconst\uff1b \u661f\u53f7\u540e\u9762\u7684 const \u5173\u952e\u5b57\u8868\u793a\u6307\u9488\u672c\u8eab\u662f\u5e38\u91cf\uff0c\u4e0d\u80fd\u88ab\u8d4b\u4e88\u65b0\u7684\u5730\u5740\u3002 1 2 3 4 5 6 7 8 9 10 11 int main () { int value { 5 }; int * ptr0 { & value }; // ptr0 \u6307\u5411 \"int\" \uff0c\u6307\u9488\u672c\u8eab\u4e0d\u662f\u5e38\u91cf\uff0c\u662f\u4e00\u4e2a\u666e\u901a\u6307\u9488 const int * ptr1 { & value }; // ptr1 \u6307\u5411 \"const int\", \u6307\u9488\u672c\u8eab\u4e0d\u662f\u5e38\u91cf, \u662f\u4e00\u4e2a\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488 int * const ptr2 { & value }; // ptr2 \u6307\u5411 \"int\",\u6307\u9488\u672c\u8eab\u662f\u5e38\u91cf, \u662f\u4e00\u4e2a\u6307\u5411\u975e\u5e38\u91cf\u7684\u5e38\u91cf\u6307\u9488 const int * const ptr3 { & value }; // ptr3 \u6307\u5411 \"const int\", \u6307\u9488\u672c\u8eab\u662f\u5e38\u91cf, \u662f\u4e00\u4e2a\u6307\u5411\u5e38\u91cf\u7684\u5e38\u91cf\u6307\u9488 return 0 ; }","title":"9.8 - \u6307\u9488\u548cconst"},{"location":"9-8-Pointers-and-const/#_1","text":"\u6307\u9488\u6307\u5411\u4e00\u4e2aconst\u7684\u503c\uff08\u4e00\u822c\u7b80\u79f0\u4e3a \u6307\u5411\u5e38\u91cf\u7684\u6307\u9488 \uff09\uff0c\u4e5f\u5c31\u662f\u8bf4\u4e00\u4e2a\u975e\u5e38\u91cf\u7684\u6307\u9488\u6307\u5411\u4e86\u4e00\u4e2a\u5e38\u91cf\u503c\u3002 \u58f0\u660e\u4e00\u4e2a\u53ef\u4ee5\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488\uff0c\u9700\u8981\u5728\u6307\u9488\u7684\u6570\u636e\u7c7b\u578b\u524d\u6dfb\u52a0\u5173\u952e\u5b57 const \uff1a 1 2 3 4 5 6 7 8 9 int main () { const int x { 5 }; const int * ptr { & x }; // okay: ptr is pointing to a \"const int\" * ptr = 6 ; // not allowed: we can't change a const value return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d ptr \u6307\u5411\u4e86\u4e00\u4e2a const int \u3002\u56e0\u4e3a\u6307\u9488\u6240\u6307\u7684\u6570\u636e\u7c7b\u578b\u662fconst\uff0c\u6240\u4ee5\u4e0d\u80fd\u901a\u8fc7\u6307\u9488\u4fee\u6539\u5b83\u6307\u7684\u8fd9\u4e2a\u503c\u3002 \u4f46\u662f\uff0c\u56e0\u4e3a\u6307\u9488\u672c\u8eab\u4e0d\u662fconst\u7684\uff08\u53ea\u662f\u6307\u5411\u4e00\u4e2aconst\u7c7b\u578b\u7684\u503c\u53d8\u91cf\uff09\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4fee\u6539\u6307\u9488\u672c\u8eab\u6240\u6301\u6709\u7684\u5730\u5740\u4f7f\u5176\u6307\u5411\u5176\u4ed6\u53d8\u91cf\uff1a 1 2 3 4 5 6 7 8 9 10 int main () { const int x { 5 }; const int * ptr { & x }; // ptr \u6307\u5411 const int x const int y { 6 }; ptr = & y ; // okay: \u91cd\u65b0\u6307\u5411 const int y return 0 ; } \u5c31\u50cf\u5bf9const\u53d8\u91cf\u7684\u5f15\u7528\u4e00\u6837\uff0c\u6307\u5411const\u7684\u6307\u9488\u4e5f\u53ef\u4ee5\u6307\u5411\u975econst\u53d8\u91cf\u3002\u6307\u5411const\u7684\u6307\u9488\u5c06\u88ab\u6307\u5411\u7684\u503c\u89c6\u4e3a\u5e38\u91cf\uff0c\u800c\u4e0d\u7ba1\u8be5\u5730\u5740\u7684\u5bf9\u8c61\u6700\u521d\u662f\u5426\u5b9a\u4e49\u4e3aconst: 1 2 3 4 5 6 7 8 9 10 int main () { int x { 5 }; // non-const const int * ptr { & x }; // ptr points to a \"const int\" * ptr = 6 ; // not allowed: ptr points to a \"const int\" so we can't change the value through ptr x = 6 ; // allowed: the value is still non-const when accessed through non-const identifier x return 0 ; }","title":"\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488"},{"location":"9-8-Pointers-and-const/#_2","text":"\u6307\u9488\u81ea\u8eab\u4e5f\u53ef\u4ee5\u662f\u5e38\u91cf\u3002\u4e00\u4e2a\u5e38\u91cf\u6307\u9488\u7684\u5730\u5740\uff0c\u5728\u521d\u59cb\u5316\u4e4b\u540e\u662f\u4e0d\u80fd\u591f\u88ab\u4fee\u6539\u7684\u3002 \u58f0\u660e\u4e00\u4e2a\u5e38\u91cf\u6307\u9488\uff0c\u53ea\u9700\u8981\u5728\u6307\u9488\u58f0\u660e\u7684\u661f\u53f7\u540e\u6dfb\u52a0 const \u5173\u952e\u5b57\uff1a 1 2 3 4 5 6 7 int main () { int x { 5 }; int * const ptr { & x }; // const after the asterisk means this is a const pointer return 0 ; } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c ptr \u662f\u4e00\u4e2a\u6307\u5411(\u975e const ) int \u503c\u7684 const \u6307\u9488\u3002 \u5c31\u50cf\u666e\u901a\u7684 const \u53d8\u91cf\u4e00\u6837\uff0c const \u6307\u9488\u5fc5\u987b\u5728\u5b9a\u4e49\u65f6\u521d\u59cb\u5316\uff0c\u5e76\u4e14\u8fd9\u4e2a\u503c\u4e0d\u80fd\u901a\u8fc7\u8d4b\u503c\u6765\u6539\u53d8: 1 2 3 4 5 6 7 8 9 10 int main () { int x { 5 }; int y { 6 }; int * const ptr { & x }; // okay: the const pointer is initialized to the address of x ptr = & y ; // error: once initialized, a const pointer can not be changed. return 0 ; } \u4e0d\u8fc7\uff0c\u8be5\u6307\u9488\u6240\u6307\u7684\u5bf9\u8c61\u5e76\u4e0d\u662f\u4e00\u4e2a\u5e38\u91cf\uff0c\u6240\u4ee5\u4ecd\u65e7\u53ef\u4ee5\u901a\u8fc7\u5bf9\u6307\u9488\u89e3\u5f15\u7528\u7136\u540e\u4fee\u6539\u8be5\u53d8\u91cf\u7684\u503c\uff1a 1 2 3 4 5 6 7 8 9 int main () { int x { 5 }; int * const ptr { & x }; // ptr will always point to x * ptr = 6 ; // okay: the value being pointed to is non-const return 0 ; }","title":"\u5e38\u91cf\u6307\u9488"},{"location":"9-8-Pointers-and-const/#_3","text":"\u6700\u540e\u4e00\u79cd\u7ec4\u5408\uff0c\u5373\u6307\u9488\u53ef\u4ee5\u88ab\u58f0\u660e\u4e3a\u5e38\u91cf\u6307\u9488\u540c\u65f6\u5b83\u6240\u6307\u7684\u503c\u4e5f\u662f\u5e38\u91cf\uff0c\u5728\u7c7b\u578b\u524d\u548c\u661f\u53f7\u540e\u5206\u522b\u6dfb\u52a0const\u5173\u952e\u5b57\u5373\u53ef\uff1a 1 2 3 4 5 6 7 int main () { int value { 5 }; const int * const ptr { & value }; // a const pointer to a const value return 0 ; } \u6307\u5411const\u503c\u7684 \u5e38\u91cf\u6307\u9488 \u4e0d\u80fd\u6539\u53d8\u5176\u5730\u5740\uff0c\u4e5f\u4e0d\u80fd\u901a\u8fc7\u8be5\u6307\u9488\u6539\u53d8\u5b83\u6240\u6307\u5411\u7684\u503c\u3002\u5b83\u53ea\u80fd\u901a\u8fc7\u89e3\u5f15\u7528\u4ee5\u83b7\u5f97\u5b83\u6240\u6307\u5411\u7684\u503c\u3002","title":"\u6307\u5411\u5e38\u91cf\u7684\u5e38\u91cf\u6307\u9488"},{"location":"9-8-Pointers-and-const/#_4","text":"\u603b\u800c\u8a00\u4e4b\uff0c\u4f60\u53ea\u9700\u8981\u8bb0\u4f4f4\u6761\u89c4\u5219\uff0c\u800c\u4e14\u5b83\u4eec\u975e\u5e38\u7b26\u5408\u903b\u8f91: \u975econst\u6307\u9488\u53ef\u4ee5\u88ab\u8d4b\u4e88\u53e6\u4e00\u4e2a\u5730\u5740\u6765\u6539\u53d8\u5b83\u6240\u6307\u5411\u7684\u5bf9\u8c61\uff1b const\u6307\u9488\u603b\u662f\u6307\u5411\u76f8\u540c\u7684\u5730\u5740\uff0c\u4e14\u8be5\u5730\u5740\u4e0d\u80fd\u66f4\u6539\uff1b \u6307\u5411\u975econst\u503c\u7684\u6307\u9488\u53ef\u4ee5\u6539\u53d8\u5b83\u6240\u6307\u5411\u7684\u503c\uff0c\u4f46\u8be5\u6307\u9488\u4e0d\u80fd\u6307\u5411const\u7c7b\u578b\u53d8\u91cf\uff1b \u6307\u5411const\u503c\u7684\u6307\u9488\u5728\u901a\u8fc7\u8be5\u6307\u9488\u8bbf\u95ee\u65f6\u5c06\u8be5\u503c\u89c6\u4e3aconst\uff0c\u56e0\u6b64\u4e0d\u80fd\u66f4\u6539\u5b83\u6240\u6307\u5411\u7684\u503c\u3002\u5b83\u4eec\u53ef\u4ee5\u6307\u5411const\u6216\u975econst\u7684 \u5de6\u503c (\u4f46\u4e0d\u80fd\u6307\u5411\u6ca1\u6709\u5730\u5740\u7684 \u53f3\u503c ) \u5728\u914d\u5408const\u5173\u952e\u5b57\u4f7f\u7528\u6307\u9488\u65f6\uff0c\u58f0\u660e\u7684\u8bed\u6cd5\u8fd8\u662f\u6709\u70b9\u590d\u6742\u7684\uff1a \u6307\u9488\u7684\u7c7b\u578b\u51b3\u5b9a\u4e86\u5b83\u80fd\u6307\u5411\u5bf9\u8c61\u7684\u7c7b\u578b\u3002\u6240\u4ee5const\u5bf9\u6307\u9488\u7c7b\u578b\u7684\u4fee\u9970\u51b3\u5b9a\u4e86\u6307\u9488\u6240\u6307\u53d8\u91cf\u662f\u5426\u4e3aconst\uff1b \u661f\u53f7\u540e\u9762\u7684 const \u5173\u952e\u5b57\u8868\u793a\u6307\u9488\u672c\u8eab\u662f\u5e38\u91cf\uff0c\u4e0d\u80fd\u88ab\u8d4b\u4e88\u65b0\u7684\u5730\u5740\u3002 1 2 3 4 5 6 7 8 9 10 11 int main () { int value { 5 }; int * ptr0 { & value }; // ptr0 \u6307\u5411 \"int\" \uff0c\u6307\u9488\u672c\u8eab\u4e0d\u662f\u5e38\u91cf\uff0c\u662f\u4e00\u4e2a\u666e\u901a\u6307\u9488 const int * ptr1 { & value }; // ptr1 \u6307\u5411 \"const int\", \u6307\u9488\u672c\u8eab\u4e0d\u662f\u5e38\u91cf, \u662f\u4e00\u4e2a\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488 int * const ptr2 { & value }; // ptr2 \u6307\u5411 \"int\",\u6307\u9488\u672c\u8eab\u662f\u5e38\u91cf, \u662f\u4e00\u4e2a\u6307\u5411\u975e\u5e38\u91cf\u7684\u5e38\u91cf\u6307\u9488 const int * const ptr3 { & value }; // ptr3 \u6307\u5411 \"const int\", \u6307\u9488\u672c\u8eab\u662f\u5e38\u91cf, \u662f\u4e00\u4e2a\u6307\u5411\u5e38\u91cf\u7684\u5e38\u91cf\u6307\u9488 return 0 ; }","title":"\u590d\u4e60"},{"location":"9-9-Pass-by-address/","tags":["pointer","address"],"text":"\u5728\u524d\u9762\u7684\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u51fd\u6570\u7684\u4e24\u79cd\u4f20\u53c2\u65b9\u5f0f\uff1a \u6309\u503c\u4f20\u9012 ( 2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2 ) \u548c \u6309\u5f15\u7528\u4f20\u9012 ( 9.5 -- Pass by lvalue reference )\u3002 \u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u57fa\u4e8e std::string \u5bf9\u8c61\u5206\u522b\u6f14\u793a\u4e86\u4e0a\u9762\u4e24\u79cd\u4f20\u53c2\u65b9\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include void printByValue ( std :: string val ) // \u5f62\u53c2\u662f\u5b9e\u53c2\u7684\u62f7\u8d1d { std :: cout << val << '\\n' ; // \u6253\u5370\u62f7\u8d1d\u7684\u503c } void printByReference ( const std :: string & ref ) // \u5f62\u53c2\u7ed1\u5b9a\u5230str { std :: cout << ref << '\\n' ; // \u901a\u8fc7\u5f15\u7528\u76f4\u63a5\u6253\u5370str } int main () { std :: string str { \"Hello, world!\" }; printByValue ( str ); // \u4f20\u9012str\u7684\u503c\uff0c\u4f1a\u521b\u5efa\u62f7\u8d1d printByReference ( str ); // \u6309\u5f15\u7528\u4f20\u9012 str \uff0c\u4e0d\u4f1a\u521b\u5efa\u62f7\u8d1d return 0 ; } When we pass argument str by value, the function parameter val receives a copy of the argument. Because the parameter is a copy of the argument, any changes to the val are made to the copy, not the original argument. When we pass argument str by reference, the reference parameter ref is bound to the actual argument. This avoids making a copy of the argument. Because our reference parameter is const, we are not allowed to change ref . But if ref were non-const, any changes we made to ref would change str . In both cases, the caller is providing the actual object ( str ) to be passed as an argument to the function call. Pass by address C++ provides a third way to pass values to a function, called pass by address. With pass by address, instead of providing an object as an argument, the caller provides an object\u2019s address (via a pointer). This pointer (holding the address of the object) is copied into a pointer parameter of the called function (which now also holds the address of the object). The function can then dereference that pointer to access the object whose address was passed. Here\u2019s a version of the above program that adds a pass by address variant: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include #include void printByValue ( std :: string val ) // The function parameter is a copy of str { std :: cout << val << '\\n' ; // print the value via the copy } void printByReference ( const std :: string & ref ) // The function parameter is a reference that binds to str { std :: cout << ref << '\\n' ; // print the value via the reference } void printByAddress ( const std :: string * ptr ) // The function parameter is a pointer that holds the address of str { std :: cout << * ptr << '\\n' ; // print the value via the dereferenced pointer } int main () { std :: string str { \"Hello, world!\" }; printByValue ( str ); // pass str by value, makes a copy of str printByReference ( str ); // pass str by reference, does not make a copy of str printByAddress ( & str ); // pass str by address, does not make a copy of str return 0 ; } COPY Note how similar all three of these versions are. Let\u2019s explore the pass by address version in more detail. First, because we want our printByAddress() function to use pass by address, we\u2019ve made our function parameter a pointer named ptr . Since printByAddress() will use ptr in a read-only manner, ptr is a pointer to a const value. 1 2 3 4 void printByAddress ( const std :: string * ptr ) { std :: cout << * ptr << '\\n' ; // print the value via the dereferenced pointer } COPY Inside the printByAddress() function, we dereference ptr parameter to access the value of the object being pointed to. Second, when the function is called, we can\u2019t just pass in the str object -- we need to pass in the address of str . The easiest way to do that is to use the address-of operator (&) to get a pointer holding the address of str : 1 printByAddress ( & str ); // use address-of operator (&) to get pointer holding address of str COPY When this call is executed, &str will create a pointer holding the address of str . This address is then copied into function parameter ptr as part of the function call. Because ptr now holds the address of str , when the function dereferences ptr , it will get the value of str , which the function prints to the console. That\u2019s it. Although we use the address-of operator in the above example to get the address of str , if we already had a pointer variable holding the address of str , we could use that instead: 1 2 3 4 5 6 7 8 9 10 11 12 13 int main () { std :: string str { \"Hello, world!\" }; printByValue ( str ); // pass str by value, makes a copy of str printByReference ( str ); // pass str by reference, does not make a copy of str printByAddress ( & str ); // pass str by address, does not make a copy of str std :: string * ptr { & str }; // define a pointer variable holding the address of str printByAddress ( ptr ); // pass str by address, does not make a copy of str return 0 ; } \u6309\u5730\u5740\u4f20\u9012\u4e0d\u4f1a\u521b\u5efa\u88ab\u6307\u7684\u5bf9\u8c61\u7684\u62f7\u8d1d Consider the following statements: 1 2 std :: string str { \"Hello, world!\" }; printByAddress ( & str ); // use address-of operator (&) to get pointer holding address of str As we noted in 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 , 9.5 -- Pass by lvalue reference , copying a std::string is expensive, so that\u2019s something we want to avoid. When we pass a std::string by address, we\u2019re not copying the actual std::string object -- we\u2019re just copying the pointer (holding the address of the object) from the caller to the called function. Since an address is typically only 4 or 8 bytes, a pointer is only 4 or 8 bytes, so copying a pointer is always fast. Thus, just like pass by reference, pass by address is fast, and avoids making a copy of the argument object. Pass by address allows the function to modify the argument\u2019s value When we pass an object by address, the function receives the address of the passed object, which it can access via dereferencing. Because this is the address of the actual argument object being passed (not a copy of the object), if the function parameter is a pointer to non-const, the function can modify the argument via the pointer parameter: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void changeValue ( int * ptr ) // note: ptr is a pointer to non-const in this example { * ptr = 6 ; // change the value to 6 } int main () { int x { 5 }; std :: cout << \"x = \" << x << '\\n' ; changeValue ( & x ); // we're passing the address of x to the function std :: cout << \"x = \" << x << '\\n' ; return 0 ; } COPY This prints: 1 2 x = 5 x = 6 As you can see, the argument is modified and this modification persists even after changeValue() has finished running. If a function is not supposed to modify the object being passed in, the function parameter can be made a pointer to const: 1 2 3 4 void changeValue ( const int * ptr ) // note: ptr is now a pointer to a const { * ptr = 6 ; // error: can not change const value } COPY \u6307\u9488\u5224\u7a7a Now consider this fairly innocent looking program: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void print ( int * ptr ) { std :: cout << * ptr ; } int main () { int x { 5 }; print ( & x ); int * myptr {}; print ( myptr ); return 0 ; } COPY When this program is run, it will print the value 5 and then most likely crash. In the call to print(myptr) , myptr is a null pointer, so function parameter ptr will also be a null pointer. When this null pointer is dereferenced in the body of the function, undefined behavior results. When passing a parameter by address, care should be taken to ensure the pointer is not a null pointer before you dereference the value. One way to do that is to use a conditional statement: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void print ( int * ptr ) { if ( ptr ) // if ptr is not a null pointer { std :: cout << * ptr ; } } int main () { int x { 5 }; print ( & x ); print ( nullptr ); return 0 ; } COPY In the above program, we\u2019re testing ptr to ensure it is not null before we dereference it. While this is fine for such a simple function, in more complicated functions this can result in redundant logic (testing if ptr is not null multiple times) or nesting of the primary logic of the function (if contained in a block). In most cases, it is more effective to do the opposite: test whether the function parameter is null as a precondition ( 7.17 -- Assert and static_assert ) and handle the negative case immediately: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include void print ( int * ptr ) { if ( ! ptr ) // if ptr is a null pointer, early return back to the caller return ; // if we reached this point, we can assume ptr is valid // so no more testing or nesting required std :: cout << * ptr ; } int main () { int x { 5 }; print ( & x ); print ( nullptr ); return 0 ; } COPY If a null pointer should never be passed to the function, an assert (which we covered in lesson 7.17 -- Assert and static_assert ) can be used instead (or also) (as asserts are intended to document things that should never happen): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include void print ( const int * ptr ) // now a pointer to a const int { assert ( ptr ); // fail the program in debug mode if a null pointer is passed (since this should never happen) // (optionally) handle this as an error case in production mode so we don't crash if it does happen if ( ! ptr ) return ; std :: cout << * ptr ; } int main () { int x { 5 }; print ( & x ); print ( nullptr ); return 0 ; } COPY \u63a8\u8350\u6309\uff08const\uff09\u5f15\u7528\u4f20\u9012 Note that function print() in the example above doesn\u2019t handle null values very well -- it effectively just aborts the function. Given this, why allow a user to pass in a null value at all? Pass by reference has the same benefits as pass by address without the risk of inadvertently dereferencing a null pointer. Pass by const reference has a few other advantages over pass by address. First, because an object being passed by address must have an address, only lvalues can be passed by address (as rvalues don\u2019t have addresses). Pass by const reference is more flexible, as it can accept lvalues and rvalues: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include void printByValue ( int val ) // The function parameter is a copy of the argument { std :: cout << val << '\\n' ; // print the value via the copy } void printByReference ( const int & ref ) // The function parameter is a reference that binds to the argument { std :: cout << ref << '\\n' ; // print the value via the reference } void printByAddress ( const int * ptr ) // The function parameter is a pointer that holds the address of the argument { std :: cout << * ptr << '\\n' ; // print the value via the dereferenced pointer } int main () { printByValue ( 5 ); // valid (but makes a copy) printByReference ( 5 ); // valid (because the parameter is a const reference) printByAddress ( & 5 ); // error: can't take address of r-value return 0 ; } COPY Second, the syntax for pass by reference is natural, as we can just pass in literals or objects. With pass by address, our code ends up littered with ampersands (&) and asterisks (*). In modern C++, most things that can be done with pass by address are better accomplished through other methods. Follow this common maxim: \u201cPass by reference when you can, pass by address when you must\u201d. \u6700\u4f73\u5b9e\u8df5 Prefer pass by reference to pass by address unless you have a specific reason to use pass by address.","title":"9.9 - \u6309\u5730\u5740\u4f20\u9012"},{"location":"9-9-Pass-by-address/#pass-by-address","text":"C++ provides a third way to pass values to a function, called pass by address. With pass by address, instead of providing an object as an argument, the caller provides an object\u2019s address (via a pointer). This pointer (holding the address of the object) is copied into a pointer parameter of the called function (which now also holds the address of the object). The function can then dereference that pointer to access the object whose address was passed. Here\u2019s a version of the above program that adds a pass by address variant: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include #include void printByValue ( std :: string val ) // The function parameter is a copy of str { std :: cout << val << '\\n' ; // print the value via the copy } void printByReference ( const std :: string & ref ) // The function parameter is a reference that binds to str { std :: cout << ref << '\\n' ; // print the value via the reference } void printByAddress ( const std :: string * ptr ) // The function parameter is a pointer that holds the address of str { std :: cout << * ptr << '\\n' ; // print the value via the dereferenced pointer } int main () { std :: string str { \"Hello, world!\" }; printByValue ( str ); // pass str by value, makes a copy of str printByReference ( str ); // pass str by reference, does not make a copy of str printByAddress ( & str ); // pass str by address, does not make a copy of str return 0 ; } COPY Note how similar all three of these versions are. Let\u2019s explore the pass by address version in more detail. First, because we want our printByAddress() function to use pass by address, we\u2019ve made our function parameter a pointer named ptr . Since printByAddress() will use ptr in a read-only manner, ptr is a pointer to a const value. 1 2 3 4 void printByAddress ( const std :: string * ptr ) { std :: cout << * ptr << '\\n' ; // print the value via the dereferenced pointer } COPY Inside the printByAddress() function, we dereference ptr parameter to access the value of the object being pointed to. Second, when the function is called, we can\u2019t just pass in the str object -- we need to pass in the address of str . The easiest way to do that is to use the address-of operator (&) to get a pointer holding the address of str : 1 printByAddress ( & str ); // use address-of operator (&) to get pointer holding address of str COPY When this call is executed, &str will create a pointer holding the address of str . This address is then copied into function parameter ptr as part of the function call. Because ptr now holds the address of str , when the function dereferences ptr , it will get the value of str , which the function prints to the console. That\u2019s it. Although we use the address-of operator in the above example to get the address of str , if we already had a pointer variable holding the address of str , we could use that instead: 1 2 3 4 5 6 7 8 9 10 11 12 13 int main () { std :: string str { \"Hello, world!\" }; printByValue ( str ); // pass str by value, makes a copy of str printByReference ( str ); // pass str by reference, does not make a copy of str printByAddress ( & str ); // pass str by address, does not make a copy of str std :: string * ptr { & str }; // define a pointer variable holding the address of str printByAddress ( ptr ); // pass str by address, does not make a copy of str return 0 ; }","title":"Pass by address"},{"location":"9-9-Pass-by-address/#_1","text":"Consider the following statements: 1 2 std :: string str { \"Hello, world!\" }; printByAddress ( & str ); // use address-of operator (&) to get pointer holding address of str As we noted in 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 , 9.5 -- Pass by lvalue reference , copying a std::string is expensive, so that\u2019s something we want to avoid. When we pass a std::string by address, we\u2019re not copying the actual std::string object -- we\u2019re just copying the pointer (holding the address of the object) from the caller to the called function. Since an address is typically only 4 or 8 bytes, a pointer is only 4 or 8 bytes, so copying a pointer is always fast. Thus, just like pass by reference, pass by address is fast, and avoids making a copy of the argument object.","title":"\u6309\u5730\u5740\u4f20\u9012\u4e0d\u4f1a\u521b\u5efa\u88ab\u6307\u7684\u5bf9\u8c61\u7684\u62f7\u8d1d"},{"location":"9-9-Pass-by-address/#pass-by-address-allows-the-function-to-modify-the-arguments-value","text":"When we pass an object by address, the function receives the address of the passed object, which it can access via dereferencing. Because this is the address of the actual argument object being passed (not a copy of the object), if the function parameter is a pointer to non-const, the function can modify the argument via the pointer parameter: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void changeValue ( int * ptr ) // note: ptr is a pointer to non-const in this example { * ptr = 6 ; // change the value to 6 } int main () { int x { 5 }; std :: cout << \"x = \" << x << '\\n' ; changeValue ( & x ); // we're passing the address of x to the function std :: cout << \"x = \" << x << '\\n' ; return 0 ; } COPY This prints: 1 2 x = 5 x = 6 As you can see, the argument is modified and this modification persists even after changeValue() has finished running. If a function is not supposed to modify the object being passed in, the function parameter can be made a pointer to const: 1 2 3 4 void changeValue ( const int * ptr ) // note: ptr is now a pointer to a const { * ptr = 6 ; // error: can not change const value } COPY","title":"Pass by address allows the function to modify the argument\u2019s value"},{"location":"9-9-Pass-by-address/#_2","text":"Now consider this fairly innocent looking program: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void print ( int * ptr ) { std :: cout << * ptr ; } int main () { int x { 5 }; print ( & x ); int * myptr {}; print ( myptr ); return 0 ; } COPY When this program is run, it will print the value 5 and then most likely crash. In the call to print(myptr) , myptr is a null pointer, so function parameter ptr will also be a null pointer. When this null pointer is dereferenced in the body of the function, undefined behavior results. When passing a parameter by address, care should be taken to ensure the pointer is not a null pointer before you dereference the value. One way to do that is to use a conditional statement: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include void print ( int * ptr ) { if ( ptr ) // if ptr is not a null pointer { std :: cout << * ptr ; } } int main () { int x { 5 }; print ( & x ); print ( nullptr ); return 0 ; } COPY In the above program, we\u2019re testing ptr to ensure it is not null before we dereference it. While this is fine for such a simple function, in more complicated functions this can result in redundant logic (testing if ptr is not null multiple times) or nesting of the primary logic of the function (if contained in a block). In most cases, it is more effective to do the opposite: test whether the function parameter is null as a precondition ( 7.17 -- Assert and static_assert ) and handle the negative case immediately: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include void print ( int * ptr ) { if ( ! ptr ) // if ptr is a null pointer, early return back to the caller return ; // if we reached this point, we can assume ptr is valid // so no more testing or nesting required std :: cout << * ptr ; } int main () { int x { 5 }; print ( & x ); print ( nullptr ); return 0 ; } COPY If a null pointer should never be passed to the function, an assert (which we covered in lesson 7.17 -- Assert and static_assert ) can be used instead (or also) (as asserts are intended to document things that should never happen): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include #include void print ( const int * ptr ) // now a pointer to a const int { assert ( ptr ); // fail the program in debug mode if a null pointer is passed (since this should never happen) // (optionally) handle this as an error case in production mode so we don't crash if it does happen if ( ! ptr ) return ; std :: cout << * ptr ; } int main () { int x { 5 }; print ( & x ); print ( nullptr ); return 0 ; } COPY","title":"\u6307\u9488\u5224\u7a7a"},{"location":"9-9-Pass-by-address/#const","text":"Note that function print() in the example above doesn\u2019t handle null values very well -- it effectively just aborts the function. Given this, why allow a user to pass in a null value at all? Pass by reference has the same benefits as pass by address without the risk of inadvertently dereferencing a null pointer. Pass by const reference has a few other advantages over pass by address. First, because an object being passed by address must have an address, only lvalues can be passed by address (as rvalues don\u2019t have addresses). Pass by const reference is more flexible, as it can accept lvalues and rvalues: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include void printByValue ( int val ) // The function parameter is a copy of the argument { std :: cout << val << '\\n' ; // print the value via the copy } void printByReference ( const int & ref ) // The function parameter is a reference that binds to the argument { std :: cout << ref << '\\n' ; // print the value via the reference } void printByAddress ( const int * ptr ) // The function parameter is a pointer that holds the address of the argument { std :: cout << * ptr << '\\n' ; // print the value via the dereferenced pointer } int main () { printByValue ( 5 ); // valid (but makes a copy) printByReference ( 5 ); // valid (because the parameter is a const reference) printByAddress ( & 5 ); // error: can't take address of r-value return 0 ; } COPY Second, the syntax for pass by reference is natural, as we can just pass in literals or objects. With pass by address, our code ends up littered with ampersands (&) and asterisks (*). In modern C++, most things that can be done with pass by address are better accomplished through other methods. Follow this common maxim: \u201cPass by reference when you can, pass by address when you must\u201d. \u6700\u4f73\u5b9e\u8df5 Prefer pass by reference to pass by address unless you have a specific reason to use pass by address.","title":"\u63a8\u8350\u6309\uff08const\uff09\u5f15\u7528\u4f20\u9012"},{"location":"9-x-Chapter-9-comprehensive-quiz/","tags":["summary"],"text":"\u590d\u4e60 9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b \u590d\u5408\u6570\u636e\u7c7b\u578b (\u4e5f\u79f0\u4f5c \u7ec4\u5408\u6570\u636e\u7c7b\u578b )\uff0c\u6307\u7684\u662f\u54ea\u4e9b\u7531\u57fa\u7840\u6570\u636e\u7c7b\u578b\u7ec4\u6210\u7684\u6570\u636e\u7c7b\u578b\uff08\u6216\u7531\u5176\u4ed6\u590d\u5408\u7c7b\u578b\u7ec4\u6210\uff09\u3002 9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09 \u4e00\u4e2a\u8868\u8fbe\u5f0f\u7684\u503c\u7c7b\u522b\uff08value category\uff09\u8868\u660e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u6700\u7ec8\u4f1a\u88ab\u89e3\u6790\u4e3a\u4e00\u4e2a\u503c\u3001\u4e00\u4e2a\u51fd\u6570\u8fd8\u662f\u67d0\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\u3002 \u5de6\u503c \u8868\u8fbe\u5f0f\u6c42\u503c\u4e3a\u4e00\u4e2a\u5177\u6709\u6807\u8bc6\u7684\u51fd\u6570\u6216\u8005\u4e00\u4e2a\u5bf9\u8c61\u3002\u4e00\u4e2a\u5bf9\u8c61\u5177\u6709\u6807\u8bc6\uff0c\u610f\u5473\u7740\u5b83\u5177\u6709\u6807\u8bc6\u7b26\u6216\u8005\u4e00\u4e2a\u5177\u6709\u6807\u8bc6\u7684\u5185\u5b58\u5730\u5740\u3002\u5de6\u503c\u6709\u4e24\u4e2a\u5b50\u7c7b\uff1a\u53ef\u53d8\u5de6\u503c\u662f\u53ef\u4ee5\u88ab\u4fee\u6539\u7684\u5de6\u503c\uff0c\u4e0d\u53ef\u53d8\u5de6\u503c\u662f\u4e0d\u53ef\u4ee5\u88ab\u4fee\u6539\u7684\u5de6\u503c\uff08\u901a\u5e38\u56e0\u4e3a\u5b83\u4eec\u662f const \u6216 constexpr \u7c7b\u578b\u7684\uff09\u3002 \u5f53\u8868\u8fbe\u5f0f\u4e0d\u662f\u5de6\u503c\u65f6\uff0c\u5b83\u4fbf\u662f \u53f3\u503c \u3002\u5305\u62ec\u5b57\u9762\u91cf\uff08\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u9664\u5916\uff09\u548c\u51fd\u6570\u6216\u64cd\u4f5c\uff08\u5f53 \u6309\u503c\u8fd4\u56de \u65f6\uff09\u7684\u8fd4\u56de\u503c\u3002 9.3 - \u5de6\u503c\u5f15\u7528 \u5f15\u7528 \u662f\u67d0\u4e2a\u5df2\u7ecf \u5b58\u5728 \u7684\u5bf9\u8c61\u7684\u522b\u540d\u3002\u5f15\u7528\u4e00\u65e6\u88ab\u5b9a\u4e49\uff0c\u4efb\u4f55\u5bf9\u5f15\u7528\u7684\u64cd\u4f5c\u90fd\u76f8\u5f53\u4e8e\u64cd\u4f5c\u8be5\u5f15\u7528\u6240\u8868\u793a\u7684\u5bf9\u8c61\u3002C++ \u6709\u4e24\u79cd\u5f15\u7528\uff0c\u5de6\u503c\u5f15\u7528\u548c\u53f3\u503c\u5f15\u7528\u3002\u5de6\u503c\u5f15\u7528\uff08\u7b80\u79f0\u5f15\u7528\uff09\u5c31\u662f\u67d0\u4e2a\u5de6\u503c\u7684\u522b\u540d\u3002\u5de6\u503c\u5f15\u7528\u53d8\u91cf\u5c31\u662f\u4e00\u4e2a\u7528\u4f5c\u5de6\u503c\u5f15\u7528\u7684\u53d8\u91cf\uff0c\u5176\u5f15\u7528\u7684\u5de6\u503c\u901a\u5e38\u662f\u53e6\u5916\u4e00\u4e2a\u53d8\u91cf\u3002 \u5f53\u4f7f\u7528\u5bf9\u8c61\u6216\u51fd\u6570\u5bf9\u5f15\u7528\u8fdb\u884c\u521d\u59cb\u5316\u4e4b\u540e\uff0c\u6211\u4eec\u8bf4\u8be5\u5f15\u7528\u548c\u5bf9\u8c61\u6216\u51fd\u6570\u7ed1\u5b9a\u4e86\u3002\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u6216\u8005\u51fd\u6570\u79f0\u4e3areferent\u3002 \u5de6\u503c\u5f15\u7528\u4e0d\u53ef\u4ee5\u88ab\u7ed1\u5b9a\u5230\u4e0d\u53ef\u4fee\u6539\u7684\u5de6\u503c\u6216\u53f3\u503c\uff08\u4e0d\u7136\u7684\u8bdd\u4f60\u5c31\u53ef\u4ee5\u901a\u8fc7\u8be5\u5f15\u7528\u6765\u4fee\u6539\u5b83\u4eec\u7684\u503c\u4e86\uff0c\u8fd9\u8fdd\u53cd\u4e86\u5b83\u4eec\u4f5c\u4e3a\u5e38\u91cf\u7684\u5c5e\u6027\uff09\u3002\u56e0\u6b64\uff0c\u5de6\u503c\u5f15\u7528\u6709\u65f6\u4e5f\u88ab\u79f0\u4e3a\u5de6\u503c\u975e\u5e38\u91cf\u5f15\u7528\uff08\u7b80\u79f0\u5e38\u91cf\u5f15\u7528\uff09\u3002 \u5f15\u7528\u4e00\u65e6\u521d\u59cb\u5316\uff0c\u5b83\u4fbf\u4e0d\u80fd\u591f\u88ab\u91cd\u7f6e\uff0c\u4e5f\u5c31\u662f\u8bf4\u4e0d\u80fd\u4fee\u6539\u5b83\u53bb\u5f15\u7528\u522b\u7684\u5bf9\u8c61\u3002 \u5f53\u5982\u679c\u5bf9\u8c61\u5148\u4e8e\u5b83\u7684\u5f15\u7528\u88ab\u9500\u6bc1\uff0c\u5219\u8be5\u5f15\u7528\u4f1a\u6307\u5411\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u5bf9\u8c61\uff0c\u6b64\u65f6\u8be5\u5f15\u7528\u88ab\u79f0\u4e3a \u60ac\u5782 \u5f15\u7528\u3002\u8bbf\u95ee\u60ac\u5782\u5f15\u7528\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 \u5f53\u4f7f\u7528 const \u5173\u952e\u5b57\u6765\u4fee\u9970\u4e00\u4e2a\u5de6\u503c\u5f15\u7528\u65f6\uff0c\u5373\u8981\u6c42\u8be5\u5f15\u7528\u5c06\u5b83\u6240\u5f15\u7528\u7684\u5bf9\u8c61\u770b\u505a\u5e38\u91cf\u3002\u6b64\u65f6\u6210\u8be5\u5f15\u7528\u4e3a \u6307\u5411\u5e38\u91cf\u503c\u7684\u5de6\u503c\u5f15\u7528 \uff08\u7b80\u79f0\u6307\u5411\u5e38\u91cf\u7684\u5f15\u7528\u6216\u5e38\u91cf\u5f15\u7528\uff09\u3002 \u5e38\u91cf\u5f15\u7528 \u53ef\u4ee5\u88ab\u7ed1\u5b9a\u5230\u53ef\u4ee5\u4fee\u6539\u5de6\u503c\uff0c\u4e0d\u53ef\u4fee\u6539\u5de6\u503c\u548c\u53f3\u503c\u3002 \u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff08\u6709\u65f6\u79f0\u4e3a\u672a\u547d\u540d\u5bf9\u8c61\u6216\u533f\u540d\u5bf9\u8c61\uff09\u662f\u6307\u5728\u4e00\u4e2a\u5355\u4e00\u8868\u8fbe\u5f0f\u4e2d\u4e3a\u4e86\u4e34\u65f6\u4f7f\u7528\u800c\u521b\u5efa\u7684\u5bf9\u8c61\uff08\u4f7f\u7528\u540e\u9500\u6bc1\uff09\u3002 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 \u5f53\u8fdb\u884c \u6309\u5f15\u7528\u4f20\u9012 \u65f6\uff0c\u6211\u4eec\u5c06\u51fd\u6570\u7684 \u5f62\u53c2 \u58f0\u660e\u4e3a\u5f15\u7528\u6216\u5e38\u91cf\u5f15\u7528\u800c\u4e0d\u662f\u666e\u901a\u7684\u53d8\u91cf\u3002\u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u5f62\u53c2\u4e2d\u7684\u5f15\u7528\u4f1a\u7ed1\u5b9a\u5230 \u5b9e\u53c2 \u3002\u56e0\u4e3a\u5f15\u7528\u53ea\u662f\u5bf9\u8c61\u7684\u522b\u540d\uff0c\u6240\u4ee5\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5e76\u4e0d\u4f1a\u521b\u5efa\u5b9e\u53c2\u7684\u62f7\u8d1d\u3002 \u53d6\u5730\u5740\u8fd0\u7b97\u7b26 (&) \u4f1a\u8fd4\u56de\u5176\u64cd\u4f5c\u6570\u7684\u5730\u5740\u3002\u800c \u89e3\u5f15\u7528(*) \u8fd0\u7b97\u7b26\u5219\u4f1a\u8fd4\u56de\u7ed9\u5b9a\u5730\u5740\u5b58\u653e\u7684\u5de6\u503c\u3002 9.6 - \u6307\u9488\u7b80\u4ecb \u6307\u9488\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5b83 \u6301\u6709 \u4e00\u4e2a\u5185\u5b58\u5730\u5740\uff08\u901a\u5e38\u662f\u53e6\u5916\u4e00\u4e2a\u53d8\u91cf\u7684\u5730\u5740\uff09\u4f5c\u4e3a\u5176\u503c\u3002\u4f7f\u7528\u6307\u9488\u6211\u4eec\u53ef\u4ee5\u5c06\u67d0\u4e2a\u5bf9\u8c61\u7684\u5730\u5740\u4fdd\u5b58\u8d77\u6765\u4ee5\u4fbf\u7a0d\u540e\u4f7f\u7528\u3002\u548c\u4e0d\u540c\u7684\u53d8\u91cf\u4e00\u6837\uff0c\u6307\u9488\u4e0d\u4f1a\u88ab\u9ed8\u8ba4\u521d\u59cb\u5316\u3002\u800c\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u7684\u6307\u9488\u901a\u5e38\u88ab\u79f0\u4e3a \u91ce\u6307\u9488 \u3002 \u60ac\u5782 \u6307\u9488\u5219\u662f\u90a3\u4e9b\u4fdd\u5b58\u7740\u65e0\u6548\u5730\u5740\u7684\u6307\u9488\uff08\u8be5\u5730\u5740\u4e2d\u7684\u5bf9\u8c61\u5df2\u7ecf\u88ab\u9500\u6bc1\uff09\u3002 9.7 - \u7a7a\u6307\u9488 \u9664\u4e86\u5185\u5b58\u5730\u5740\uff0c\u6307\u9488\u8fd8\u53ef\u4ee5\u4fdd\u5b58\u4e00\u4e2a\u7279\u6b8a\u503c\uff0c\u5373\u7a7a\u503c\uff08null\uff09\u3002null\u662f\u4e00\u4e2a\u7279\u6b8a\u503c\u5b83\u8868\u793a\u7a7a\u503c\u3002\u5f53\u6307\u9488\u7684\u503c\u4e3anull\u65f6\uff0c\u8868\u793a\u5b83\u6ca1\u6709\u6307\u5411\u4efb\u4f55\u5730\u5740\u3002\u6b64\u65f6\u8be5\u6307\u9488\u88ab\u79f0\u4e3a\u7a7a\u6307\u9488\u3002\u5173\u952e\u5b57 nullptr \u662f\u7a7a\u6307\u9488\u7684\u5b57\u9762\u91cf\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 nullptr \u6765\u663e\u5f0f\u5730\u4e3a\u6307\u9488\u521d\u59cb\u5316\u6216\u8d4b\u503c\u4e3a\u7a7a\u503c\u3002 \u6307\u9488\u5e94\u5f53\u6301\u6709\u4e00\u4e2a\u5305\u542b\u6709\u6548\u5bf9\u8c61\u7684\u5730\u5740\uff0c\u6216\u8005\u8bbe\u7f6e\u4e3a nullptr \u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6211\u4eec\u53ea\u9700\u8981\u5224\u65ad\u6307\u9488\u662f\u4e0d\u662f\u7a7a\u5373\u53ef\uff0c\u5426\u5219\u6211\u4eec\u5c31\u8ba4\u4e3a\u5b83\u662f\u6709\u6548\u7684\u3002 9.8 - \u6307\u9488\u548cconst \u6307\u9488\u5982\u679c\u6307\u5411\u4e00\u4e2aconst\u5e38\u91cf\uff08\u6709\u65f6\u7b80\u79f0\u4e3a\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488\uff09\uff0c\u5219\u8868\u793a\u4e00\u4e2a\uff08\u975e\u5e38\u91cf\uff09\u6307\u9488\u6307\u5411\u4e00\u4e2a\u5e38\u91cf\u3002 \u800c\u4e00\u4e2a\u5e38\u91cf\u6307\u9488\uff0c\u5219\u662f\u5728\u5176\u6301\u6709\u5730\u5740\u88ab\u521d\u59cb\u540e\uff0c\u4e0d\u80fd\u518d\u6539\u53d8\u7684\u6307\u9488\u3002 \u6307\u5411\u5e38\u91cf\u7684\u5e38\u91cf\u6307\u9488\uff0c\u5176\u6307\u9488\u672c\u8eab\u6240\u6301\u6709\u7684\u5730\u5740\u4e0d\u80fd\u6539\u53d8\uff0c\u4e5f\u4e0d\u80fd\u4fee\u6539\u8be5\u5730\u5740\u6240\u6307\u5411\u7684\u5bf9\u8c61\u3002 9.9 - \u6309\u5730\u5740\u4f20\u9012 \u5bf9\u4e8e \u4f20\u5730\u5740 \u6765\u8bf4\uff0c\u8c03\u7528\u8005\u4f1a\u4f7f\u7528\u5bf9\u8c61\u7684\u5730\u5740\uff08\u901a\u8fc7\u6307\u9488\uff09\u800c\u4e0d\u662f\u5bf9\u8c61\u672c\u8eab\u4f5c\u4e3a \u5b9e\u53c2 \u3002\u8be5\u6307\u9488\uff08\u6301\u6709\u8be5\u5bf9\u8c61\u7684\u5730\u5740\uff09\u4f1a\u88ab\u62f7\u8d1d\u5230\u88ab\u8c03\u51fd\u6570\u7684\u6307\u9488\u53c2\u6570\uff08 \u5f62\u53c2 \uff09\uff0c\u8be5\u53c2\u6570\u6b64\u65f6\u4fdd\u5b58\u7740\u8be5\u5bf9\u8c61\u6240\u5728\u5730\u5740\uff08\u7684\u4e00\u4efd\u62f7\u8d1d\uff09\u3002\u968f\u540e\uff0c\u51fd\u6570\u53ef\u4ee5\u5bf9\u8be5\u6307\u9488\u8fdb\u884c \u89e3\u5f15\u7528 \u4ee5\u8bbf\u95ee\u8be5\u5bf9\u8c61\u3002 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 \u6309\u5f15\u7528\u8fd4\u56de \u65f6\uff0c\u51fd\u6570\u8fd4\u56de\u7ed1\u5b9a\u5230\u8fd4\u56de\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4e0d\u4f1a\u5bf9\u8fd4\u56de\u503c\u8fdb\u884c\u62f7\u8d1d\u3002\u5728\u8fd4\u56de\u5f15\u7528\u65f6\uff0c\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\uff1a\u8be5\u5f15\u7528\u6240\u7ed1\u5b9a\u7684\u5bf9\u8c61\u5176\u751f\u547d\u5468\u671f\u8981\u6bd4\u51fd\u6570\u957f\u3002\u5426\u5219\u8fd4\u56de\u7684\u5f15\u7528\u5c06\u53d8\u6210 \u60ac\u5782 \u5f15\u7528\uff08\u5f15\u7528\u7684\u5bf9\u8c61\u88ab\u9500\u6bc1\u4e86\uff09\uff0c\u6b64\u65f6\u7ee7\u7eed\u4f7f\u7528\u8be5\u5f15\u7528\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 \u5982\u679c\u5b9e\u53c2\u901a\u8fc7\u5f15\u7528\u7684\u65b9\u5f0f\u88ab\u4f20\u5165\uff0c\u90a3\u4e48\u4f7f\u7528\u6309\u5f15\u7528\u8fd4\u56de\u8be5\u53c2\u6570\u4e5f\u662f\u66f4\u5b89\u5168\u7684 \u3002 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de \u5982\u679c\u4e00\u4e2a\u51fd\u6570\u8fd4\u56de\u5f15\u7528\uff0c\u4e14\u8be5\u5f15\u7528\u88ab\u7528\u6765\u521d\u59cb\u5316\u6216\u8d4b\u503c\u7ed9\u4e00\u4e2a\u975e\u5f15\u7528\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u90a3\u4e48\u8be5\u8fd4\u56de\u503c\u4f1a\u88ab\u62f7\u8d1d\uff08\u548c \u6309\u503c\u8fd4\u56de \u7684\u6548\u679c\u662f\u4e00\u6837\u7684\uff09\u3002 \u6309\u5730\u5740\u8fd4\u56de \u548c \u6309\u5f15\u7528\u8fd4\u56de \u7684\u5de5\u4f5c\u539f\u7406\u51e0\u4e4e\u662f\u4e00\u6837\u7684\uff0c\u533a\u522b\u53ea\u5728\u4e8e\u524d\u8005\u8fd4\u56de\u7684\u662f\u6307\u5411\u5bf9\u8c61\u7684\u6307\u9488\u800c\u540e\u8005\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5bf9\u8c61\u7684\u5f15\u7528\u3002 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad \u4f7f\u7528 auto \u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\u65f6\uff0c\u5b83\u4f1a\u79fb\u9664\u5f15\u7528\u6807\u8bb0\u6216\u5176\u4ed6\u9876\u5c42\u5e38\u91cf\u4fee\u9970\u7b26\u3002\u5982\u679c\uff0c\u4f60\u4ecd\u9700\u8981\u8fd9\u4e9b\u6807\u8bc6\u7b26\uff0c\u5219\u9700\u8981\u5c06\u5176\u4f5c\u4e3a\u53d8\u91cf\u58f0\u660e\u7684\u4e00\u90e8\u5206\u8fdb\u884c\u6dfb\u52a0\u3002","title":"9.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f15\u7528\u548c\u6307\u9488"},{"location":"9-x-Chapter-9-comprehensive-quiz/#_1","text":"9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b \u590d\u5408\u6570\u636e\u7c7b\u578b (\u4e5f\u79f0\u4f5c \u7ec4\u5408\u6570\u636e\u7c7b\u578b )\uff0c\u6307\u7684\u662f\u54ea\u4e9b\u7531\u57fa\u7840\u6570\u636e\u7c7b\u578b\u7ec4\u6210\u7684\u6570\u636e\u7c7b\u578b\uff08\u6216\u7531\u5176\u4ed6\u590d\u5408\u7c7b\u578b\u7ec4\u6210\uff09\u3002 9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09 \u4e00\u4e2a\u8868\u8fbe\u5f0f\u7684\u503c\u7c7b\u522b\uff08value category\uff09\u8868\u660e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u6700\u7ec8\u4f1a\u88ab\u89e3\u6790\u4e3a\u4e00\u4e2a\u503c\u3001\u4e00\u4e2a\u51fd\u6570\u8fd8\u662f\u67d0\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\u3002 \u5de6\u503c \u8868\u8fbe\u5f0f\u6c42\u503c\u4e3a\u4e00\u4e2a\u5177\u6709\u6807\u8bc6\u7684\u51fd\u6570\u6216\u8005\u4e00\u4e2a\u5bf9\u8c61\u3002\u4e00\u4e2a\u5bf9\u8c61\u5177\u6709\u6807\u8bc6\uff0c\u610f\u5473\u7740\u5b83\u5177\u6709\u6807\u8bc6\u7b26\u6216\u8005\u4e00\u4e2a\u5177\u6709\u6807\u8bc6\u7684\u5185\u5b58\u5730\u5740\u3002\u5de6\u503c\u6709\u4e24\u4e2a\u5b50\u7c7b\uff1a\u53ef\u53d8\u5de6\u503c\u662f\u53ef\u4ee5\u88ab\u4fee\u6539\u7684\u5de6\u503c\uff0c\u4e0d\u53ef\u53d8\u5de6\u503c\u662f\u4e0d\u53ef\u4ee5\u88ab\u4fee\u6539\u7684\u5de6\u503c\uff08\u901a\u5e38\u56e0\u4e3a\u5b83\u4eec\u662f const \u6216 constexpr \u7c7b\u578b\u7684\uff09\u3002 \u5f53\u8868\u8fbe\u5f0f\u4e0d\u662f\u5de6\u503c\u65f6\uff0c\u5b83\u4fbf\u662f \u53f3\u503c \u3002\u5305\u62ec\u5b57\u9762\u91cf\uff08\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u9664\u5916\uff09\u548c\u51fd\u6570\u6216\u64cd\u4f5c\uff08\u5f53 \u6309\u503c\u8fd4\u56de \u65f6\uff09\u7684\u8fd4\u56de\u503c\u3002 9.3 - \u5de6\u503c\u5f15\u7528 \u5f15\u7528 \u662f\u67d0\u4e2a\u5df2\u7ecf \u5b58\u5728 \u7684\u5bf9\u8c61\u7684\u522b\u540d\u3002\u5f15\u7528\u4e00\u65e6\u88ab\u5b9a\u4e49\uff0c\u4efb\u4f55\u5bf9\u5f15\u7528\u7684\u64cd\u4f5c\u90fd\u76f8\u5f53\u4e8e\u64cd\u4f5c\u8be5\u5f15\u7528\u6240\u8868\u793a\u7684\u5bf9\u8c61\u3002C++ \u6709\u4e24\u79cd\u5f15\u7528\uff0c\u5de6\u503c\u5f15\u7528\u548c\u53f3\u503c\u5f15\u7528\u3002\u5de6\u503c\u5f15\u7528\uff08\u7b80\u79f0\u5f15\u7528\uff09\u5c31\u662f\u67d0\u4e2a\u5de6\u503c\u7684\u522b\u540d\u3002\u5de6\u503c\u5f15\u7528\u53d8\u91cf\u5c31\u662f\u4e00\u4e2a\u7528\u4f5c\u5de6\u503c\u5f15\u7528\u7684\u53d8\u91cf\uff0c\u5176\u5f15\u7528\u7684\u5de6\u503c\u901a\u5e38\u662f\u53e6\u5916\u4e00\u4e2a\u53d8\u91cf\u3002 \u5f53\u4f7f\u7528\u5bf9\u8c61\u6216\u51fd\u6570\u5bf9\u5f15\u7528\u8fdb\u884c\u521d\u59cb\u5316\u4e4b\u540e\uff0c\u6211\u4eec\u8bf4\u8be5\u5f15\u7528\u548c\u5bf9\u8c61\u6216\u51fd\u6570\u7ed1\u5b9a\u4e86\u3002\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u6216\u8005\u51fd\u6570\u79f0\u4e3areferent\u3002 \u5de6\u503c\u5f15\u7528\u4e0d\u53ef\u4ee5\u88ab\u7ed1\u5b9a\u5230\u4e0d\u53ef\u4fee\u6539\u7684\u5de6\u503c\u6216\u53f3\u503c\uff08\u4e0d\u7136\u7684\u8bdd\u4f60\u5c31\u53ef\u4ee5\u901a\u8fc7\u8be5\u5f15\u7528\u6765\u4fee\u6539\u5b83\u4eec\u7684\u503c\u4e86\uff0c\u8fd9\u8fdd\u53cd\u4e86\u5b83\u4eec\u4f5c\u4e3a\u5e38\u91cf\u7684\u5c5e\u6027\uff09\u3002\u56e0\u6b64\uff0c\u5de6\u503c\u5f15\u7528\u6709\u65f6\u4e5f\u88ab\u79f0\u4e3a\u5de6\u503c\u975e\u5e38\u91cf\u5f15\u7528\uff08\u7b80\u79f0\u5e38\u91cf\u5f15\u7528\uff09\u3002 \u5f15\u7528\u4e00\u65e6\u521d\u59cb\u5316\uff0c\u5b83\u4fbf\u4e0d\u80fd\u591f\u88ab\u91cd\u7f6e\uff0c\u4e5f\u5c31\u662f\u8bf4\u4e0d\u80fd\u4fee\u6539\u5b83\u53bb\u5f15\u7528\u522b\u7684\u5bf9\u8c61\u3002 \u5f53\u5982\u679c\u5bf9\u8c61\u5148\u4e8e\u5b83\u7684\u5f15\u7528\u88ab\u9500\u6bc1\uff0c\u5219\u8be5\u5f15\u7528\u4f1a\u6307\u5411\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u5bf9\u8c61\uff0c\u6b64\u65f6\u8be5\u5f15\u7528\u88ab\u79f0\u4e3a \u60ac\u5782 \u5f15\u7528\u3002\u8bbf\u95ee\u60ac\u5782\u5f15\u7528\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 \u5f53\u4f7f\u7528 const \u5173\u952e\u5b57\u6765\u4fee\u9970\u4e00\u4e2a\u5de6\u503c\u5f15\u7528\u65f6\uff0c\u5373\u8981\u6c42\u8be5\u5f15\u7528\u5c06\u5b83\u6240\u5f15\u7528\u7684\u5bf9\u8c61\u770b\u505a\u5e38\u91cf\u3002\u6b64\u65f6\u6210\u8be5\u5f15\u7528\u4e3a \u6307\u5411\u5e38\u91cf\u503c\u7684\u5de6\u503c\u5f15\u7528 \uff08\u7b80\u79f0\u6307\u5411\u5e38\u91cf\u7684\u5f15\u7528\u6216\u5e38\u91cf\u5f15\u7528\uff09\u3002 \u5e38\u91cf\u5f15\u7528 \u53ef\u4ee5\u88ab\u7ed1\u5b9a\u5230\u53ef\u4ee5\u4fee\u6539\u5de6\u503c\uff0c\u4e0d\u53ef\u4fee\u6539\u5de6\u503c\u548c\u53f3\u503c\u3002 \u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff08\u6709\u65f6\u79f0\u4e3a\u672a\u547d\u540d\u5bf9\u8c61\u6216\u533f\u540d\u5bf9\u8c61\uff09\u662f\u6307\u5728\u4e00\u4e2a\u5355\u4e00\u8868\u8fbe\u5f0f\u4e2d\u4e3a\u4e86\u4e34\u65f6\u4f7f\u7528\u800c\u521b\u5efa\u7684\u5bf9\u8c61\uff08\u4f7f\u7528\u540e\u9500\u6bc1\uff09\u3002 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 \u5f53\u8fdb\u884c \u6309\u5f15\u7528\u4f20\u9012 \u65f6\uff0c\u6211\u4eec\u5c06\u51fd\u6570\u7684 \u5f62\u53c2 \u58f0\u660e\u4e3a\u5f15\u7528\u6216\u5e38\u91cf\u5f15\u7528\u800c\u4e0d\u662f\u666e\u901a\u7684\u53d8\u91cf\u3002\u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u5f62\u53c2\u4e2d\u7684\u5f15\u7528\u4f1a\u7ed1\u5b9a\u5230 \u5b9e\u53c2 \u3002\u56e0\u4e3a\u5f15\u7528\u53ea\u662f\u5bf9\u8c61\u7684\u522b\u540d\uff0c\u6240\u4ee5\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5e76\u4e0d\u4f1a\u521b\u5efa\u5b9e\u53c2\u7684\u62f7\u8d1d\u3002 \u53d6\u5730\u5740\u8fd0\u7b97\u7b26 (&) \u4f1a\u8fd4\u56de\u5176\u64cd\u4f5c\u6570\u7684\u5730\u5740\u3002\u800c \u89e3\u5f15\u7528(*) \u8fd0\u7b97\u7b26\u5219\u4f1a\u8fd4\u56de\u7ed9\u5b9a\u5730\u5740\u5b58\u653e\u7684\u5de6\u503c\u3002 9.6 - \u6307\u9488\u7b80\u4ecb \u6307\u9488\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5b83 \u6301\u6709 \u4e00\u4e2a\u5185\u5b58\u5730\u5740\uff08\u901a\u5e38\u662f\u53e6\u5916\u4e00\u4e2a\u53d8\u91cf\u7684\u5730\u5740\uff09\u4f5c\u4e3a\u5176\u503c\u3002\u4f7f\u7528\u6307\u9488\u6211\u4eec\u53ef\u4ee5\u5c06\u67d0\u4e2a\u5bf9\u8c61\u7684\u5730\u5740\u4fdd\u5b58\u8d77\u6765\u4ee5\u4fbf\u7a0d\u540e\u4f7f\u7528\u3002\u548c\u4e0d\u540c\u7684\u53d8\u91cf\u4e00\u6837\uff0c\u6307\u9488\u4e0d\u4f1a\u88ab\u9ed8\u8ba4\u521d\u59cb\u5316\u3002\u800c\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u7684\u6307\u9488\u901a\u5e38\u88ab\u79f0\u4e3a \u91ce\u6307\u9488 \u3002 \u60ac\u5782 \u6307\u9488\u5219\u662f\u90a3\u4e9b\u4fdd\u5b58\u7740\u65e0\u6548\u5730\u5740\u7684\u6307\u9488\uff08\u8be5\u5730\u5740\u4e2d\u7684\u5bf9\u8c61\u5df2\u7ecf\u88ab\u9500\u6bc1\uff09\u3002 9.7 - \u7a7a\u6307\u9488 \u9664\u4e86\u5185\u5b58\u5730\u5740\uff0c\u6307\u9488\u8fd8\u53ef\u4ee5\u4fdd\u5b58\u4e00\u4e2a\u7279\u6b8a\u503c\uff0c\u5373\u7a7a\u503c\uff08null\uff09\u3002null\u662f\u4e00\u4e2a\u7279\u6b8a\u503c\u5b83\u8868\u793a\u7a7a\u503c\u3002\u5f53\u6307\u9488\u7684\u503c\u4e3anull\u65f6\uff0c\u8868\u793a\u5b83\u6ca1\u6709\u6307\u5411\u4efb\u4f55\u5730\u5740\u3002\u6b64\u65f6\u8be5\u6307\u9488\u88ab\u79f0\u4e3a\u7a7a\u6307\u9488\u3002\u5173\u952e\u5b57 nullptr \u662f\u7a7a\u6307\u9488\u7684\u5b57\u9762\u91cf\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 nullptr \u6765\u663e\u5f0f\u5730\u4e3a\u6307\u9488\u521d\u59cb\u5316\u6216\u8d4b\u503c\u4e3a\u7a7a\u503c\u3002 \u6307\u9488\u5e94\u5f53\u6301\u6709\u4e00\u4e2a\u5305\u542b\u6709\u6548\u5bf9\u8c61\u7684\u5730\u5740\uff0c\u6216\u8005\u8bbe\u7f6e\u4e3a nullptr \u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6211\u4eec\u53ea\u9700\u8981\u5224\u65ad\u6307\u9488\u662f\u4e0d\u662f\u7a7a\u5373\u53ef\uff0c\u5426\u5219\u6211\u4eec\u5c31\u8ba4\u4e3a\u5b83\u662f\u6709\u6548\u7684\u3002 9.8 - \u6307\u9488\u548cconst \u6307\u9488\u5982\u679c\u6307\u5411\u4e00\u4e2aconst\u5e38\u91cf\uff08\u6709\u65f6\u7b80\u79f0\u4e3a\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488\uff09\uff0c\u5219\u8868\u793a\u4e00\u4e2a\uff08\u975e\u5e38\u91cf\uff09\u6307\u9488\u6307\u5411\u4e00\u4e2a\u5e38\u91cf\u3002 \u800c\u4e00\u4e2a\u5e38\u91cf\u6307\u9488\uff0c\u5219\u662f\u5728\u5176\u6301\u6709\u5730\u5740\u88ab\u521d\u59cb\u540e\uff0c\u4e0d\u80fd\u518d\u6539\u53d8\u7684\u6307\u9488\u3002 \u6307\u5411\u5e38\u91cf\u7684\u5e38\u91cf\u6307\u9488\uff0c\u5176\u6307\u9488\u672c\u8eab\u6240\u6301\u6709\u7684\u5730\u5740\u4e0d\u80fd\u6539\u53d8\uff0c\u4e5f\u4e0d\u80fd\u4fee\u6539\u8be5\u5730\u5740\u6240\u6307\u5411\u7684\u5bf9\u8c61\u3002 9.9 - \u6309\u5730\u5740\u4f20\u9012 \u5bf9\u4e8e \u4f20\u5730\u5740 \u6765\u8bf4\uff0c\u8c03\u7528\u8005\u4f1a\u4f7f\u7528\u5bf9\u8c61\u7684\u5730\u5740\uff08\u901a\u8fc7\u6307\u9488\uff09\u800c\u4e0d\u662f\u5bf9\u8c61\u672c\u8eab\u4f5c\u4e3a \u5b9e\u53c2 \u3002\u8be5\u6307\u9488\uff08\u6301\u6709\u8be5\u5bf9\u8c61\u7684\u5730\u5740\uff09\u4f1a\u88ab\u62f7\u8d1d\u5230\u88ab\u8c03\u51fd\u6570\u7684\u6307\u9488\u53c2\u6570\uff08 \u5f62\u53c2 \uff09\uff0c\u8be5\u53c2\u6570\u6b64\u65f6\u4fdd\u5b58\u7740\u8be5\u5bf9\u8c61\u6240\u5728\u5730\u5740\uff08\u7684\u4e00\u4efd\u62f7\u8d1d\uff09\u3002\u968f\u540e\uff0c\u51fd\u6570\u53ef\u4ee5\u5bf9\u8be5\u6307\u9488\u8fdb\u884c \u89e3\u5f15\u7528 \u4ee5\u8bbf\u95ee\u8be5\u5bf9\u8c61\u3002 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 \u6309\u5f15\u7528\u8fd4\u56de \u65f6\uff0c\u51fd\u6570\u8fd4\u56de\u7ed1\u5b9a\u5230\u8fd4\u56de\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4e0d\u4f1a\u5bf9\u8fd4\u56de\u503c\u8fdb\u884c\u62f7\u8d1d\u3002\u5728\u8fd4\u56de\u5f15\u7528\u65f6\uff0c\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\uff1a\u8be5\u5f15\u7528\u6240\u7ed1\u5b9a\u7684\u5bf9\u8c61\u5176\u751f\u547d\u5468\u671f\u8981\u6bd4\u51fd\u6570\u957f\u3002\u5426\u5219\u8fd4\u56de\u7684\u5f15\u7528\u5c06\u53d8\u6210 \u60ac\u5782 \u5f15\u7528\uff08\u5f15\u7528\u7684\u5bf9\u8c61\u88ab\u9500\u6bc1\u4e86\uff09\uff0c\u6b64\u65f6\u7ee7\u7eed\u4f7f\u7528\u8be5\u5f15\u7528\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 \u5982\u679c\u5b9e\u53c2\u901a\u8fc7\u5f15\u7528\u7684\u65b9\u5f0f\u88ab\u4f20\u5165\uff0c\u90a3\u4e48\u4f7f\u7528\u6309\u5f15\u7528\u8fd4\u56de\u8be5\u53c2\u6570\u4e5f\u662f\u66f4\u5b89\u5168\u7684 \u3002 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de \u5982\u679c\u4e00\u4e2a\u51fd\u6570\u8fd4\u56de\u5f15\u7528\uff0c\u4e14\u8be5\u5f15\u7528\u88ab\u7528\u6765\u521d\u59cb\u5316\u6216\u8d4b\u503c\u7ed9\u4e00\u4e2a\u975e\u5f15\u7528\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u90a3\u4e48\u8be5\u8fd4\u56de\u503c\u4f1a\u88ab\u62f7\u8d1d\uff08\u548c \u6309\u503c\u8fd4\u56de \u7684\u6548\u679c\u662f\u4e00\u6837\u7684\uff09\u3002 \u6309\u5730\u5740\u8fd4\u56de \u548c \u6309\u5f15\u7528\u8fd4\u56de \u7684\u5de5\u4f5c\u539f\u7406\u51e0\u4e4e\u662f\u4e00\u6837\u7684\uff0c\u533a\u522b\u53ea\u5728\u4e8e\u524d\u8005\u8fd4\u56de\u7684\u662f\u6307\u5411\u5bf9\u8c61\u7684\u6307\u9488\u800c\u540e\u8005\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5bf9\u8c61\u7684\u5f15\u7528\u3002 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad \u4f7f\u7528 auto \u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\u65f6\uff0c\u5b83\u4f1a\u79fb\u9664\u5f15\u7528\u6807\u8bb0\u6216\u5176\u4ed6\u9876\u5c42\u5e38\u91cf\u4fee\u9970\u7b26\u3002\u5982\u679c\uff0c\u4f60\u4ecd\u9700\u8981\u8fd9\u4e9b\u6807\u8bc6\u7b26\uff0c\u5219\u9700\u8981\u5c06\u5176\u4f5c\u4e3a\u53d8\u91cf\u58f0\u660e\u7684\u4e00\u90e8\u5206\u8fdb\u884c\u6dfb\u52a0\u3002","title":"\u590d\u4e60"},{"location":"A-1-static-and-dynamic-libraries/","text":"title: A.1 - \u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93 alias: A.1 - \u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93 origin: /a1-static-and-dynamic-libraries/ origin_title: \"A.1 \u2014 Static and dynamic libraries\" time: 2022-9-13 type: translation Key Takeaway \u5e93 \u662f\u4e00\u4e2a\u4ee3\u7801\u5305\uff0c\u5b83\u53ef\u4ee5\u88ab\u8bb8\u591a\u7a0b\u5e8f\u91cd\u7528\u3002\u901a\u5e38\uff0cC++\u5e93\u5206\u4e3a\u4e24\u90e8\u5206\uff1a \u5b9a\u4e49\u516c\u5f00\u63a5\u53e3(\u63d0\u4f9b\u529f\u80fd)\u7684\u5934\u6587\u4ef6\u3002 \u4e00\u79cd\u9884\u7f16\u8bd1\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u9884\u7f16\u8bd1\u6210\u673a\u5668\u8bed\u8a00\u7684\u529f\u80fd\u7684\u5b9e\u73b0\u3002 \u6709\u4e9b\u5e93\u53ef\u80fd\u88ab\u62c6\u5206\u4e3a\u591a\u4e2a\u6587\u4ef6\u548c/\u6216\u5177\u6709\u591a\u4e2a\u5934\u6587\u4ef6\u3002 \u5e93\u88ab\u9884\u7f16\u8bd1\u6709\u51e0\u4e2a\u539f\u56e0\u3002\u9996\u5148\uff0c\u7531\u4e8e\u5e93\u5f88\u5c11\u66f4\u6539\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u7ecf\u5e38\u91cd\u65b0\u7f16\u8bd1\u3002\u6bcf\u6b21\u7f16\u5199\u4f7f\u7528\u5e93\u7684\u7a0b\u5e8f\u65f6\u91cd\u65b0\u7f16\u8bd1\u5e93\u662f\u6d6a\u8d39\u65f6\u95f4\u7684\u3002\u5176\u6b21\uff0c\u56e0\u4e3a\u9884\u7f16\u8bd1\u5bf9\u8c61\u662f\u7528\u673a\u5668\u8bed\u8a00\u7f16\u5199\u7684\uff0c\u6240\u4ee5\u5b83\u963b\u6b62\u4e86\u4eba\u4eec\u8bbf\u95ee\u6216\u66f4\u6539\u6e90\u4ee3\u7801\uff0c\u8fd9\u5bf9\u4e8e\u4f01\u4e1a\u6216\u56e0\u4e3a\u77e5\u8bc6\u4ea7\u6743\u539f\u56e0\u4e0d\u60f3\u8ba9\u6e90\u4ee3\u7801\u53ef\u7528\u7684\u4eba\u6765\u8bf4\u5f88\u91cd\u8981\u3002 \u5e93\u53ef\u4ee5\u5206\u4e3a\u4e24\u7c7b\uff1a\u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93 \u9759\u6001\u5e93 (\u4e5f\u79f0\u4e3a \u5b58\u6863 archive )\u7531\u7f16\u8bd1\u540e\u76f4\u63a5\u94fe\u63a5\u5230\u7a0b\u5e8f\u4e2d\u7a0b\u5e8f\u7ec4\u6210\u3002\u5f53\u7f16\u8bd1\u4f7f\u7528\u9759\u6001\u5e93\u7684\u7a0b\u5e8f\u65f6\uff0c\u7a0b\u5e8f\u4f7f\u7528\u7684\u9759\u6001\u5e93\u7684\u6240\u6709\u529f\u80fd\u90fd\u6210\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u7684\u4e00\u90e8\u5206\u3002\u5728Windows\u4e0a\uff0c\u9759\u6001\u5e93\u901a\u5e38\u6709\u4e00\u4e2a .lib \u6269\u5c55\u540d\uff0c\u800c\u5728Linux\u4e0a\uff0c\u9759\u6001\u5e93\u901a\u5e38\u6709\u4e00\u4e2a .a (\u5b58\u6863)\u6269\u5c55\u540d\u3002\u9759\u6001\u5e93\u7684\u4e00\u4e2a\u4f18\u70b9\u662f\uff0c\u8ba9\u7528\u6237\u8fd0\u884c\u4f60\u7684\u7a0b\u5e8f\uff0c\u53ea\u9700\u5206\u53d1\u53ef\u6267\u884c\u6587\u4ef6\u3002\u56e0\u4e3a\u5e93\u6210\u4e3a\u4e86\u7a0b\u5e8f\u7684\u4e00\u90e8\u5206\uff0c\u8fd9\u5c31\u786e\u4fdd\u4e86\u7a0b\u5e8f\u603b\u662f\u4f7f\u7528\u6b63\u786e\u7248\u672c\u7684\u5e93\u3002\u53e6\u5916\uff0c\u7531\u4e8e\u9759\u6001\u5e93\u6210\u4e3a\u7a0b\u5e8f\u7684\u4e00\u90e8\u5206\uff0c\u4f60\u53ef\u4ee5\u50cf\u4e3a\u81ea\u5df1\u7684\u7a0b\u5e8f\u7f16\u5199\u7684\u529f\u80fd\u4e00\u6837\u4f7f\u7528\u5b83\u4eec\u3002\u7f3a\u70b9\u662f\uff0c\u7531\u4e8e\u5e93\u7684\u526f\u672c\u6210\u4e3a\u6bcf\u4e2a\u4f7f\u7528\u5b83\u7684\u53ef\u6267\u884c\u6587\u4ef6\u7684\u4e00\u90e8\u5206\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u5927\u91cf\u7684\u7a7a\u95f4\u6d6a\u8d39\u3002\u9759\u6001\u5e93\u4e5f\u4e0d\u5bb9\u6613\u5347\u7ea7\u2014\u2014\u8981\u66f4\u65b0\u5e93\uff0c\u9700\u8981\u66ff\u6362\u6574\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u3002 \u52a8\u6001\u5e93(\u4e5f\u79f0\u4e3a \u5171\u4eab\u5e93 shared library )\u7531\u5728\u8fd0\u884c\u65f6\u52a0\u8f7d\u5230\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u7a0b\u5e8f\u7ec4\u6210\u3002\u5f53\u7f16\u8bd1\u4e00\u4e2a\u4f7f\u7528\u52a8\u6001\u5e93\u7684\u7a0b\u5e8f\u65f6\uff0c\u5e93\u4e0d\u4f1a\u6210\u4e3a\u53ef\u6267\u884c\u7a0b\u5e8f\u7684\u4e00\u90e8\u5206\u2014\u2014\u5b83\u4ecd\u7136\u662f\u4e00\u4e2a\u72ec\u7acb\u7684\u5355\u5143\u3002\u5728Windows\u4e0a\uff0c\u52a8\u6001\u5e93\u901a\u5e38\u5177\u6709 .dll (\u52a8\u6001\u94fe\u63a5\u5e93)\u6269\u5c55\u540d\uff0c\u800c\u5728Linux\u4e0a\uff0c\u52a8\u6001\u5e93\u901a\u5e38\u5177\u6709 .so (\u5171\u4eab\u5bf9\u8c61)\u6269\u5c55\u540d\u3002\u52a8\u6001\u5e93\u7684\u4e00\u4e2a\u4f18\u70b9\u662f\u8bb8\u591a\u7a0b\u5e8f\u53ef\u4ee5\u5171\u4eab\u4e00\u4e2a\u526f\u672c\uff0c\u8fd9\u8282\u7701\u4e86\u7a7a\u95f4\u3002\u4e5f\u8bb8\u66f4\u5927\u7684\u4f18\u70b9\u662f\u52a8\u6001\u5e93\u53ef\u4ee5\u5347\u7ea7\u5230\u65b0\u7248\u672c\uff0c\u800c\u4e0d\u9700\u8981\u66ff\u6362\u4f7f\u7528\u5b83\u7684\u6240\u6709\u53ef\u6267\u884c\u7a0b\u5e8f\u3002 \u56e0\u4e3a\u52a8\u6001\u5e93\u6ca1\u6709\u94fe\u63a5\u5230\u7a0b\u5e8f\u4e2d\uff0c\u6240\u4ee5\u4f7f\u7528\u52a8\u6001\u5e93\u7684\u7a0b\u5e8f\u5fc5\u987b\u663e\u5f0f\u52a0\u8f7d\u52a8\u6001\u5e93\u5e76\u4e0e\u4e4b\u8fde\u63a5\u3002\u8fd9\u79cd\u673a\u5236\u53ef\u80fd\u4ee4\u4eba\u56f0\u60d1\uff0c\u5e76\u4f7f\u4e0e\u52a8\u6001\u5e93\u7684\u63a5\u53e3\u53d8\u5f97\u5c34\u5c2c\u3002\u4e3a\u4e86\u4f7f\u52a8\u6001\u5e93\u66f4\u5bb9\u6613\u4f7f\u7528\uff0c\u53ef\u4ee5\u4f7f\u7528\u5bfc\u5165\u5e93\u3002 \u5bfc\u5165\u5e93 \u662f\u4e00\u4e2a\u81ea\u52a8\u52a0\u8f7d\u548c\u4f7f\u7528 \u52a8\u6001\u5e93 \u7684\u5e93\u3002\u5728Windows\u4e0a\uff0c\u8fd9\u901a\u5e38\u662f\u901a\u8fc7\u4e0e\u52a8\u6001\u5e93( .dll )\u540c\u540d\u7684\u5c0f\u578b\u9759\u6001\u5e93( .lib )\u6765\u5b8c\u6210\u7684\u3002\u5728\u7f16\u8bd1\u65f6\u5c06\u9759\u6001\u5e93\u94fe\u63a5\u5230\u7a0b\u5e8f\u4e2d\uff0c\u7136\u540e\u5c31\u53ef\u4ee5\u50cf\u4f7f\u7528\u9759\u6001\u5e93\u4e00\u6837\u6709\u6548\u5730\u4f7f\u7528\u52a8\u6001\u5e93\u7684\u529f\u80fd\u3002\u5728Linux\u4e0a\uff0c\u5171\u4eab\u5bf9\u8c61( .so )\u6587\u4ef6\u65e2\u662f\u52a8\u6001\u5e93\u53c8\u662f\u5bfc\u5165\u5e93\u3002 \u5927\u591a\u6570\u94fe\u63a5\u5668\u53ef\u4ee5\u5728\u521b\u5efa\u52a8\u6001\u5e93\u65f6\u4e3a\u52a8\u6001\u5e93\u6784\u5efa\u5bfc\u5165\u5e93 \u3002 \u5b89\u88c5\u548c\u4f7f\u7528\u5e93 \u73b0\u5728\u4f60\u5df2\u7ecf\u4e86\u89e3\u4e86\u4e0d\u540c\u7c7b\u578b\u7684\u5e93\uff0c\u8ba9\u6211\u4eec\u8ba8\u8bba\u4e00\u4e0b\u5982\u4f55\u5728\u7a0b\u5e8f\u4e2d\u5b9e\u9645\u4f7f\u7528\u5e93\u3002\u5728C++\u4e2d\u5b89\u88c5\u5e93\u901a\u5e38\u5305\u62ec4\u4e2a\u6b65\u9aa4\uff1a \u83b7\u5f97\u5e93\u6700\u597d\u7684\u65b9\u5f0f\u662f\u4e3a\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u8f7d\u9884\u7f16\u8bd1\u7684\u5305(\u5982\u679c\u5b83\u5b58\u5728\u7684\u8bdd)\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u4e0d\u5fc5\u81ea\u5df1\u7f16\u8bd1\u5e93\u3002\u5982\u679c\u6ca1\u6709\uff0c\u90a3\u4e48\u6211\u4eec\u5c06\u4e0d\u5f97\u4e0d\u4e0b\u8f7d\u4e00\u4e2a\u6e90\u4ee3\u7801\u5305\u5e76\u81ea\u5df1\u7f16\u8bd1\u5b83(\u8fd9\u8d85\u51fa\u4e86\u672c\u8bfe\u7684\u8303\u56f4)\u3002\u5728Windows\u4e0a\uff0c\u5e93\u901a\u5e38\u4ee5 .zip \u6587\u4ef6\u7684\u5f62\u5f0f\u5206\u53d1\u3002\u5728Linux\u4e0a\uff0c\u5e93\u901a\u5e38\u4ee5\u5305\u7684\u5f62\u5f0f\u5206\u53d1(\u4f8b\u5982 . rpm )\u3002\u5305\u7ba1\u7406\u5668\u53ef\u80fd\u5df2\u7ecf\u5217\u51fa\u4e86\u4e00\u4e9b\u66f4\u6d41\u884c\u7684\u5e93(\u4f8b\u5982SDL)\uff0c\u4ee5\u4fbf\u4e8e\u5b89\u88c5\uff0c\u6240\u4ee5\u8bf7\u5148\u68c0\u67e5\u8fd9\u4e9b\u5e93\u3002 \u5b89\u88c5\u5e93\u3002\u5728Linux\u4e0a\uff0c\u901a\u5e38\u4f7f\u7528\u5305\u7ba1\u7406\u5668\u5b8c\u6210\u6240\u6709\u5de5\u4f5c\u3002\u5728Windows\u4e0a\uff0c\u8fd9\u901a\u5e38\u6d89\u53ca\u5c06\u5e93\u89e3\u538b\u7f29\u5230\u4f60\u6240\u9009\u62e9\u7684\u76ee\u5f55\u3002\u6211\u4eec\u5efa\u8bae\u5c06\u6240\u6709\u7684\u5e93\u653e\u5728\u4e00\u4e2a\u4f4d\u7f6e\u4ee5\u4fbf\u4e8e\u8bbf\u95ee\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u4e00\u4e2a\u540d\u4e3a C:\\Libs \u7684\u76ee\u5f55\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5e93\u653e\u5728\u5b83\u81ea\u5df1\u7684\u5b50\u76ee\u5f55\u4e2d\u3002 \u786e\u4fdd\u7f16\u8bd1\u5668\u77e5\u9053\u5728\u54ea\u91cc\u67e5\u627e\u5e93\u7684\u5934\u6587\u4ef6\u3002\u5728Windows\u4e0a\uff0c\u901a\u5e38\u8fd9\u662f\u4f60\u5b89\u88c5\u5e93\u6587\u4ef6\u7684\u76ee\u5f55\u7684include\u5b50\u76ee\u5f55(\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u628a\u5e93\u5b89\u88c5\u5230 C:\\libs\\SDL-1.2.11 \uff0c\u5934\u6587\u4ef6\u53ef\u80fd\u5728 C:\\libs\\SDL-1.2.11\\include )\u3002\u5728Linux\u4e0a\uff0c\u5934\u6587\u4ef6\u901a\u5e38\u5b89\u88c5\u5230 /usr/include \uff0c\u5b83\u5e94\u8be5\u5df2\u7ecf\u662f\u5305\u542b\u6587\u4ef6\u641c\u7d22\u8def\u5f84\u7684\u4e00\u90e8\u5206\u3002\u4f46\u662f\uff0c\u5982\u679c\u6587\u4ef6\u5b89\u88c5\u5728\u5176\u4ed6\u5730\u65b9\uff0c\u5219\u5fc5\u987b\u544a\u8bc9\u7f16\u8bd1\u5668\u5728\u54ea\u91cc\u627e\u5230\u5b83\u4eec\u3002 \u544a\u8bc9\u94fe\u63a5\u5668\u5728\u54ea\u91cc\u67e5\u627e\u5e93\u6587\u4ef6\u3002\u4e0e\u6b65\u9aa43\u4e00\u6837\uff0c\u8fd9\u901a\u5e38\u6d89\u53ca\u5230\u5411\u94fe\u63a5\u5668\u67e5\u627e\u5e93\u7684\u4f4d\u7f6e\u5217\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u76ee\u5f55\u3002\u5728Windows\u4e0a\uff0c\u8fd9\u901a\u5e38\u662f\u60a8\u5b89\u88c5\u5e93\u6587\u4ef6\u7684\u76ee\u5f55\u7684 /lib \u5b50\u76ee\u5f55\u3002\u5728Linux\u4e0a\uff0c\u5e93\u901a\u5e38\u5b89\u88c5\u5230 /usr/lib \uff0c\u5b83\u5e94\u8be5\u5df2\u7ecf\u662f\u5e93\u641c\u7d22\u8def\u5f84\u7684\u4e00\u90e8\u5206\u3002 \u4e00\u65e6\u5b89\u88c5\u4e86\u5e93\uff0c\u5e76\u4e14IDE\u77e5\u9053\u5728\u54ea\u91cc\u67e5\u627e\u5b83\u4eec\u540e\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u60f3\u8981\u4f7f\u7528\u5e93\u7684\u9879\u76ee\uff0c\u901a\u5e38\u8fd8\u9700\u8981\u6267\u884c\u4ee5\u4e0b3\u4e2a\u6b65\u9aa4: \u5982\u679c\u4f7f\u7528\u9759\u6001\u5e93\u6216\u5bfc\u5165\u5e93\uff0c\u8bf7\u544a\u8bc9\u94fe\u63a5\u5668\u8981\u94fe\u63a5\u54ea\u4e9b\u5e93\u6587\u4ef6\u3002 \u4f7f\u7528 #include \u5728\u4f60\u7684\u7a0b\u5e8f\u4e2d\u5305\u542b\u5e93\u7684\u5934\u6587\u4ef6\u3002\u8fd9\u53ef\u4ee5\u544a\u8bc9\u7f16\u8bd1\u5668\u8be5\u5e93\u63d0\u4f9b\u7684\u529f\u80fd\u6709\u54ea\u4e9b\u3002 \u5982\u679c\u4f7f\u7528\u52a8\u6001\u5e93\uff0c\u8bf7\u786e\u4fdd\u7a0b\u5e8f\u77e5\u9053\u5728\u54ea\u91cc\u53ef\u4ee5\u627e\u5230\u5b83\u4eec\u3002\u5728Linux\u4e0b\uff0c\u5e93\u901a\u5e38\u5b89\u88c5\u5230 /usr/lib \uff0c\u5b83\u4f4d\u4e8e LD_LIBRARY_PATH \u73af\u5883\u53d8\u91cf\u7684\u9ed8\u8ba4\u641c\u7d22\u8def\u5f84\u4e2d\u3002\u5728Windows\u4e0a\uff0c\u9ed8\u8ba4\u641c\u7d22\u8def\u5f84\u5305\u62ec\u8fd0\u884c\u7a0b\u5e8f\u7684\u76ee\u5f55\u3001\u8c03\u7528 SetDllDirectory() \u8bbe\u7f6e\u7684\u76ee\u5f55\u3001Windows\u3001System\u548cSystem32\u76ee\u5f55\u4ee5\u53capath\u73af\u5883\u53d8\u91cf\u4e2d\u7684\u76ee\u5f55\u3002\u4f7f\u7528 .dll \u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u5c06 .dll \u590d\u5236\u5230\u53ef\u6267\u884c\u6587\u4ef6\u7684\u4f4d\u7f6e\u3002\u7531\u4e8e\u4f60\u901a\u5e38\u4f1a\u5c06 .dll \u4e0e\u53ef\u6267\u884c\u6587\u4ef6\u4e00\u8d77\u5206\u53d1\uff0c\u56e0\u6b64\u5c06\u5b83\u4eec\u653e\u5728\u4e00\u8d77\u662f\u6709\u610f\u4e49\u7684\u3002 \u6b65\u9aa43-5\u6d89\u53ca\u5230\u914d\u7f6eIDE\u2014\u2014\u5e78\u8fd0\u7684\u662f\uff0c\u51e0\u4e4e\u6240\u6709IDE\u5728\u505a\u8fd9\u4e9b\u4e8b\u60c5\u65f6\u90fd\u4ee5\u76f8\u540c\u7684\u65b9\u5f0f\u5de5\u4f5c\u3002\u4e0d\u5e78\u7684\u662f\uff0c\u56e0\u4e3a\u6bcf\u4e2aIDE\u90fd\u6709\u4e0d\u540c\u7684\u63a5\u53e3\uff0c\u6240\u4ee5\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u6700\u56f0\u96be\u7684\u90e8\u5206\u4ec5\u4ec5\u662f\u627e\u5230\u6267\u884c\u8fd9\u4e9b\u6b65\u9aa4\u7684\u5408\u9002\u4f4d\u7f6e\u3002\u56e0\u6b64\uff0c\u5728\u672c\u8282\u63a5\u4e0b\u6765\u7684\u51e0\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4e3a Visual Studio Express 2005 \u548c Code::Blocks \u6267\u884c\u6240\u6709\u8fd9\u4e9b\u6b65\u9aa4\u3002\u5982\u679c\u4f60\u6b63\u5728\u4f7f\u7528\u53e6\u4e00\u79cdIDE\uff0c\u8bf7\u540c\u65f6\u9605\u8bfb\u8fd9\u4e24\u79cdIDE\u7684\u4f7f\u7528\u65b9\u5f0f\u2014\u2014\u8bfb\u5b8c\u4e4b\u540e\u4f60\u5e94\u8be5\u6709\u8db3\u591f\u7684\u4fe1\u606f\u6765\u4f7f\u7528\u81ea\u5df1\u7684IDE\u8fdb\u884c\u76f8\u540c\u7684\u64cd\u4f5c\uff0c\u53ea\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u641c\u7d22\u3002","title":"A 1 static and dynamic libraries"},{"location":"A-1-static-and-dynamic-libraries/#_1","text":"\u73b0\u5728\u4f60\u5df2\u7ecf\u4e86\u89e3\u4e86\u4e0d\u540c\u7c7b\u578b\u7684\u5e93\uff0c\u8ba9\u6211\u4eec\u8ba8\u8bba\u4e00\u4e0b\u5982\u4f55\u5728\u7a0b\u5e8f\u4e2d\u5b9e\u9645\u4f7f\u7528\u5e93\u3002\u5728C++\u4e2d\u5b89\u88c5\u5e93\u901a\u5e38\u5305\u62ec4\u4e2a\u6b65\u9aa4\uff1a \u83b7\u5f97\u5e93\u6700\u597d\u7684\u65b9\u5f0f\u662f\u4e3a\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u8f7d\u9884\u7f16\u8bd1\u7684\u5305(\u5982\u679c\u5b83\u5b58\u5728\u7684\u8bdd)\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u4e0d\u5fc5\u81ea\u5df1\u7f16\u8bd1\u5e93\u3002\u5982\u679c\u6ca1\u6709\uff0c\u90a3\u4e48\u6211\u4eec\u5c06\u4e0d\u5f97\u4e0d\u4e0b\u8f7d\u4e00\u4e2a\u6e90\u4ee3\u7801\u5305\u5e76\u81ea\u5df1\u7f16\u8bd1\u5b83(\u8fd9\u8d85\u51fa\u4e86\u672c\u8bfe\u7684\u8303\u56f4)\u3002\u5728Windows\u4e0a\uff0c\u5e93\u901a\u5e38\u4ee5 .zip \u6587\u4ef6\u7684\u5f62\u5f0f\u5206\u53d1\u3002\u5728Linux\u4e0a\uff0c\u5e93\u901a\u5e38\u4ee5\u5305\u7684\u5f62\u5f0f\u5206\u53d1(\u4f8b\u5982 . rpm )\u3002\u5305\u7ba1\u7406\u5668\u53ef\u80fd\u5df2\u7ecf\u5217\u51fa\u4e86\u4e00\u4e9b\u66f4\u6d41\u884c\u7684\u5e93(\u4f8b\u5982SDL)\uff0c\u4ee5\u4fbf\u4e8e\u5b89\u88c5\uff0c\u6240\u4ee5\u8bf7\u5148\u68c0\u67e5\u8fd9\u4e9b\u5e93\u3002 \u5b89\u88c5\u5e93\u3002\u5728Linux\u4e0a\uff0c\u901a\u5e38\u4f7f\u7528\u5305\u7ba1\u7406\u5668\u5b8c\u6210\u6240\u6709\u5de5\u4f5c\u3002\u5728Windows\u4e0a\uff0c\u8fd9\u901a\u5e38\u6d89\u53ca\u5c06\u5e93\u89e3\u538b\u7f29\u5230\u4f60\u6240\u9009\u62e9\u7684\u76ee\u5f55\u3002\u6211\u4eec\u5efa\u8bae\u5c06\u6240\u6709\u7684\u5e93\u653e\u5728\u4e00\u4e2a\u4f4d\u7f6e\u4ee5\u4fbf\u4e8e\u8bbf\u95ee\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u4e00\u4e2a\u540d\u4e3a C:\\Libs \u7684\u76ee\u5f55\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5e93\u653e\u5728\u5b83\u81ea\u5df1\u7684\u5b50\u76ee\u5f55\u4e2d\u3002 \u786e\u4fdd\u7f16\u8bd1\u5668\u77e5\u9053\u5728\u54ea\u91cc\u67e5\u627e\u5e93\u7684\u5934\u6587\u4ef6\u3002\u5728Windows\u4e0a\uff0c\u901a\u5e38\u8fd9\u662f\u4f60\u5b89\u88c5\u5e93\u6587\u4ef6\u7684\u76ee\u5f55\u7684include\u5b50\u76ee\u5f55(\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u628a\u5e93\u5b89\u88c5\u5230 C:\\libs\\SDL-1.2.11 \uff0c\u5934\u6587\u4ef6\u53ef\u80fd\u5728 C:\\libs\\SDL-1.2.11\\include )\u3002\u5728Linux\u4e0a\uff0c\u5934\u6587\u4ef6\u901a\u5e38\u5b89\u88c5\u5230 /usr/include \uff0c\u5b83\u5e94\u8be5\u5df2\u7ecf\u662f\u5305\u542b\u6587\u4ef6\u641c\u7d22\u8def\u5f84\u7684\u4e00\u90e8\u5206\u3002\u4f46\u662f\uff0c\u5982\u679c\u6587\u4ef6\u5b89\u88c5\u5728\u5176\u4ed6\u5730\u65b9\uff0c\u5219\u5fc5\u987b\u544a\u8bc9\u7f16\u8bd1\u5668\u5728\u54ea\u91cc\u627e\u5230\u5b83\u4eec\u3002 \u544a\u8bc9\u94fe\u63a5\u5668\u5728\u54ea\u91cc\u67e5\u627e\u5e93\u6587\u4ef6\u3002\u4e0e\u6b65\u9aa43\u4e00\u6837\uff0c\u8fd9\u901a\u5e38\u6d89\u53ca\u5230\u5411\u94fe\u63a5\u5668\u67e5\u627e\u5e93\u7684\u4f4d\u7f6e\u5217\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u76ee\u5f55\u3002\u5728Windows\u4e0a\uff0c\u8fd9\u901a\u5e38\u662f\u60a8\u5b89\u88c5\u5e93\u6587\u4ef6\u7684\u76ee\u5f55\u7684 /lib \u5b50\u76ee\u5f55\u3002\u5728Linux\u4e0a\uff0c\u5e93\u901a\u5e38\u5b89\u88c5\u5230 /usr/lib \uff0c\u5b83\u5e94\u8be5\u5df2\u7ecf\u662f\u5e93\u641c\u7d22\u8def\u5f84\u7684\u4e00\u90e8\u5206\u3002 \u4e00\u65e6\u5b89\u88c5\u4e86\u5e93\uff0c\u5e76\u4e14IDE\u77e5\u9053\u5728\u54ea\u91cc\u67e5\u627e\u5b83\u4eec\u540e\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u60f3\u8981\u4f7f\u7528\u5e93\u7684\u9879\u76ee\uff0c\u901a\u5e38\u8fd8\u9700\u8981\u6267\u884c\u4ee5\u4e0b3\u4e2a\u6b65\u9aa4: \u5982\u679c\u4f7f\u7528\u9759\u6001\u5e93\u6216\u5bfc\u5165\u5e93\uff0c\u8bf7\u544a\u8bc9\u94fe\u63a5\u5668\u8981\u94fe\u63a5\u54ea\u4e9b\u5e93\u6587\u4ef6\u3002 \u4f7f\u7528 #include \u5728\u4f60\u7684\u7a0b\u5e8f\u4e2d\u5305\u542b\u5e93\u7684\u5934\u6587\u4ef6\u3002\u8fd9\u53ef\u4ee5\u544a\u8bc9\u7f16\u8bd1\u5668\u8be5\u5e93\u63d0\u4f9b\u7684\u529f\u80fd\u6709\u54ea\u4e9b\u3002 \u5982\u679c\u4f7f\u7528\u52a8\u6001\u5e93\uff0c\u8bf7\u786e\u4fdd\u7a0b\u5e8f\u77e5\u9053\u5728\u54ea\u91cc\u53ef\u4ee5\u627e\u5230\u5b83\u4eec\u3002\u5728Linux\u4e0b\uff0c\u5e93\u901a\u5e38\u5b89\u88c5\u5230 /usr/lib \uff0c\u5b83\u4f4d\u4e8e LD_LIBRARY_PATH \u73af\u5883\u53d8\u91cf\u7684\u9ed8\u8ba4\u641c\u7d22\u8def\u5f84\u4e2d\u3002\u5728Windows\u4e0a\uff0c\u9ed8\u8ba4\u641c\u7d22\u8def\u5f84\u5305\u62ec\u8fd0\u884c\u7a0b\u5e8f\u7684\u76ee\u5f55\u3001\u8c03\u7528 SetDllDirectory() \u8bbe\u7f6e\u7684\u76ee\u5f55\u3001Windows\u3001System\u548cSystem32\u76ee\u5f55\u4ee5\u53capath\u73af\u5883\u53d8\u91cf\u4e2d\u7684\u76ee\u5f55\u3002\u4f7f\u7528 .dll \u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u5c06 .dll \u590d\u5236\u5230\u53ef\u6267\u884c\u6587\u4ef6\u7684\u4f4d\u7f6e\u3002\u7531\u4e8e\u4f60\u901a\u5e38\u4f1a\u5c06 .dll \u4e0e\u53ef\u6267\u884c\u6587\u4ef6\u4e00\u8d77\u5206\u53d1\uff0c\u56e0\u6b64\u5c06\u5b83\u4eec\u653e\u5728\u4e00\u8d77\u662f\u6709\u610f\u4e49\u7684\u3002 \u6b65\u9aa43-5\u6d89\u53ca\u5230\u914d\u7f6eIDE\u2014\u2014\u5e78\u8fd0\u7684\u662f\uff0c\u51e0\u4e4e\u6240\u6709IDE\u5728\u505a\u8fd9\u4e9b\u4e8b\u60c5\u65f6\u90fd\u4ee5\u76f8\u540c\u7684\u65b9\u5f0f\u5de5\u4f5c\u3002\u4e0d\u5e78\u7684\u662f\uff0c\u56e0\u4e3a\u6bcf\u4e2aIDE\u90fd\u6709\u4e0d\u540c\u7684\u63a5\u53e3\uff0c\u6240\u4ee5\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u6700\u56f0\u96be\u7684\u90e8\u5206\u4ec5\u4ec5\u662f\u627e\u5230\u6267\u884c\u8fd9\u4e9b\u6b65\u9aa4\u7684\u5408\u9002\u4f4d\u7f6e\u3002\u56e0\u6b64\uff0c\u5728\u672c\u8282\u63a5\u4e0b\u6765\u7684\u51e0\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4e3a Visual Studio Express 2005 \u548c Code::Blocks \u6267\u884c\u6240\u6709\u8fd9\u4e9b\u6b65\u9aa4\u3002\u5982\u679c\u4f60\u6b63\u5728\u4f7f\u7528\u53e6\u4e00\u79cdIDE\uff0c\u8bf7\u540c\u65f6\u9605\u8bfb\u8fd9\u4e24\u79cdIDE\u7684\u4f7f\u7528\u65b9\u5f0f\u2014\u2014\u8bfb\u5b8c\u4e4b\u540e\u4f60\u5e94\u8be5\u6709\u8db3\u591f\u7684\u4fe1\u606f\u6765\u4f7f\u7528\u81ea\u5df1\u7684IDE\u8fdb\u884c\u76f8\u540c\u7684\u64cd\u4f5c\uff0c\u53ea\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u641c\u7d22\u3002","title":"\u5b89\u88c5\u548c\u4f7f\u7528\u5e93"},{"location":"A-2-using-libraries-with-visual-studio/","text":"Key Takeaway \u56de\u987e\u4e00\u4e0b\u4f7f\u7528\u5e93\u9700\u8981\u5b8c\u6210\u7684\u51e0\u4e2a\u6b65\u9aa4\uff1a \u5bf9\u4e8e\u6bcf\u4e2a\u5e93\uff1a \u83b7\u53d6\u5e93\u3002\u4ece\u5b98\u7f51\u6216\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u4e0b\u8f7d\u5e93\uff1b \u5b89\u88c5\u5e93\u3002\u5c06\u5e93\u89e3\u538b\u6216\u5b89\u88c5\u5230\u7279\u5b9a\u7684\u76ee\u5f55\u3002 \u5bf9\u4e8e\u6bcf\u4e2a\u9879\u76ee\uff1a \u544a\u77e5\u7f16\u8bd1\u5668\u5230\u54ea\u91cc\u5bfb\u627e\u5934\u6587\u4ef6\uff1b \u544a\u77e5\u94fe\u63a5\u5668\u5230\u54ea\u91cc\u5bfb\u627e\u5e93\u6587\u4ef6\uff1b \u544a\u77e5\u94fe\u63a5\u5668\u94fe\u63a5\u54ea\u4e2a\u9759\u6001\u5e93\u6216\u5bfc\u5165\u5e93\u6587\u4ef6\uff1b \u5728\u7a0b\u5e8f\u4e2d #include \u5934\u6587\u4ef6\uff1b \u786e\u4fdd\u7a0b\u5e8f\u77e5\u9053\u5230\u54ea\u91cc\u5bfb\u627e\u52a8\u6001\u5e93\u3002 \u6ce8\u610f\uff1a\u672c\u8bfe\u4e2d\u7684\u4f8b\u5b50\u57fa\u4e8e Visual Studio 2005 express\uff0c\u4f46\u662f\u4ece\u90a3\u65f6\u8d77\u8fd9\u4e2a\u8fc7\u7a0b\u5e76\u6ca1\u6709\u53d1\u751f\u592a\u5927\u7684\u53d8\u5316\u3002 **\u6b65\u9aa41\u548c2\u2014\u2014\u83b7\u53d6\u548c\u5b89\u88c5\u5e93 \u4e0b\u8f7d\u5e93\u5230\u672c\u5730\u7535\u8111\uff0c\u53c2\u89c1 A.1 - \u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93 \u3002 \u6b65\u9aa43\u548c4\u2014\u2014\u544a\u8bc9\u7f16\u8bd1\u5668\u5230\u54ea\u91cc\u67e5\u627e\u5934\u6587\u4ef6\u548c\u5e93\u6587\u4ef6 \u8fdb\u5165\u9879\u76ee\u83dc\u5355\uff0c\u9009\u62e9\u9879\u76ee->\u5c5e\u6027(\u5b83\u5e94\u8be5\u5728\u5e95\u90e8) \u5728\u201c\u914d\u7f6e\u201d\u4e0b\u62c9\u83dc\u5355\u4e2d\uff0c\u786e\u4fdd\u9009\u62e9\u4e86\u201c\u6240\u6709\u914d\u7f6e\u201d\u3002 \u5728\u5de6\u4fa7\u7a97\u53e3\u7a97\u683c\u4e2d\uff0c\u9009\u62e9\u201c\u914d\u7f6e\u5c5e\u6027\u201d->\u201cVC++\u76ee\u5f55\u201d\u3002 \u5728\u201cInclude Directories\u201d\u884c\uff0c\u4e3a\u5e93\u6dfb\u52a0.h\u6587\u4ef6\u7684\u8def\u5f84(\u786e\u4fdd\u8fd9\u4e0e\u524d\u9762\u7684\u6761\u76ee\u7528\u5206\u53f7\u5206\u9694)\u3002 \u5728\u201c\u5e93\u76ee\u5f55\u201d\u4e2d\uff0c\u6dfb\u52a0\u5e93\u7684 .lib \u6587\u4ef6\u7684\u8def\u5f84\u3002 \u70b9\u51fb\u201c\u786e\u5b9a\u201d\u3002 \u6b65\u9aa4 5 \u2014\u2014 \u544a\u8bc9\u94fe\u63a5\u5668\u7a0b\u5e8f\u4f7f\u7528\u54ea\u4e9b\u5e93 \u5bf9\u4e8e\u7b2c5\u6b65\uff0c\u9700\u8981\u5c06\u5e93\u4e2d\u7684 .lib \u6587\u4ef6\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u3002\u6211\u4eec\u5728\u6bcf\u4e2a\u9879\u76ee\u7684\u57fa\u7840\u4e0a\u8fd9\u6837\u505a\u3002 \u8fdb\u5165\u9879\u76ee\u83dc\u5355\uff0c\u9009\u62e9\u9879\u76ee->\u5c5e\u6027(\u5b83\u5e94\u8be5\u5728\u5e95\u90e8) \u5728\u201c\u914d\u7f6e\u201d\u4e0b\u62c9\u83dc\u5355\u4e2d\uff0c\u786e\u4fdd\u9009\u62e9\u4e86\u201c\u6240\u6709\u914d\u7f6e\u201d\u3002 \u5728\u5de6\u4fa7\u7a97\u53e3\u4e2d\uff0c\u9009\u62e9\u201cConfiguration Properties\u201d->\u201cLinker\u201d->\u201cInput\u201d\u3002 \u5c06.lib\u6587\u4ef6\u7684\u540d\u79f0\u6dfb\u52a0\u5230\u201c\u9644\u52a0\u4f9d\u8d56\u9879\u201d\u5217\u8868\u4e2d(\u7528\u5206\u53f7\u4e0e\u524d\u9762\u7684\u6761\u76ee\u5206\u9694) \u70b9\u51fb\u201c\u786e\u5b9a\u201d\u3002 **\u6b65\u9aa4 6\u548c 7 \u2014\u2014 #include \u5934\u6587\u4ef6 \u53ea\u9700\u50cf\u5f80\u5e38\u4e00\u6837\u5c06\u5e93\u4e2d\u7684\u5934\u6587\u4ef6 #include \u5230\u9879\u76ee\u4e2d\u3002","title":"A.2 - \u5728visual studio\u4e2d\u4f7f\u7528\u5e93"},{"location":"A-3-using-libraries-with-Code-Blocks/","text":"Key Takeaway \u56de\u987e\u4e00\u4e0b\u4f7f\u7528\u5e93\u9700\u8981\u5b8c\u6210\u7684\u51e0\u4e2a\u6b65\u9aa4\uff1a \u5bf9\u4e8e\u6bcf\u4e2a\u5e93\uff1a \u83b7\u53d6\u5e93\u3002\u4ece\u5b98\u7f51\u6216\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u4e0b\u8f7d\u5e93\uff1b \u5b89\u88c5\u5e93\u3002\u5c06\u5e93\u89e3\u538b\u6216\u5b89\u88c5\u5230\u7279\u5b9a\u7684\u76ee\u5f55\u3002 \u544a\u77e5\u7f16\u8bd1\u5668\u5230\u54ea\u91cc\u5bfb\u627e\u5934\u6587\u4ef6\uff1b \u544a\u77e5\u94fe\u63a5\u5668\u5230\u54ea\u91cc\u5bfb\u627e\u5e93\u6587\u4ef6\uff1b \u5bf9\u4e8e\u6bcf\u4e2a\u9879\u76ee\uff1a \u544a\u77e5\u94fe\u63a5\u5668\u94fe\u63a5\u54ea\u4e2a\u9759\u6001\u5e93\u6216\u5bfc\u5165\u5e93\u6587\u4ef6\uff1b \u5728\u7a0b\u5e8f\u4e2d #include \u5934\u6587\u4ef6\uff1b \u786e\u4fdd\u7a0b\u5e8f\u77e5\u9053\u5230\u54ea\u91cc\u5bfb\u627e\u52a8\u6001\u5e93\u3002 **\u6b65\u9aa41\u548c2\u2014\u2014\u83b7\u53d6\u548c\u5b89\u88c5\u5e93 \u4e0b\u8f7d\u5e93\u5230\u672c\u5730\u7535\u8111\uff0c\u53c2\u89c1 A.1 - \u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93 \u3002 \u6b65\u9aa43\u548c4\u2014\u2014\u544a\u8bc9\u7f16\u8bd1\u5668\u5230\u54ea\u91cc\u67e5\u627e\u5934\u6587\u4ef6\u548c\u5e93\u6587\u4ef6 \u4e0b\u9762\u7684\u64cd\u4f5c\u662f\u9488\u5bf9\u5168\u5c40\u7684\u8bbe\u7f6e\uff0c\u8fd9\u6837\u6240\u6709\u7684\u9879\u76ee\u90fd\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u5e93\u3002\u56e0\u6b64\uff0c\u4e0b\u9762\u7684\u6b65\u9aa4\u6bcf\u4e2a\u5e93\u53ea\u9700\u8981\u6267\u884c\u4e00\u6b21\u3002 \u627e\u5230 \u201cSettings menu\u201d \u7136\u540e\u9009\u62e9 \u201cCompiler\u201d\u3002 \u70b9\u51fb \u201cDirectories\u201d \u9009\u9879\u5361\u627e\u5230\u9ed8\u8ba4\u7684 \u201cCompiler\u201d \u5b50\u9009\u9879\u5361\u3002 \u6309\u4e0b\u201cAdd\u201d\u6309\u94ae\uff0c\u4e3a\u5e93\u7684\u5934\u6587\u4ef6\u6dfb\u52a0\u8def\u5f84\u3002\u5982\u679c\u4f60\u4f7f\u7528Linux\u5e76\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u5b89\u88c5\u5e93\uff0c\u8fd9\u91cc\u5217\u51fa\u4e86 /usr/include \uff1b \u70b9\u51fb \u201cLinker\u201d \u6807\u7b7e\u3002\u70b9\u51fb \u201cAdd\u201d \u6309\u94ae\u5e76\u6dfb\u52a0 .lib \u6587\u4ef6\u7684\u5730\u5740\u3002\u5982\u679c\u4f60\u8fd0\u884c\u7684\u662f LInux \u4e14\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u5b89\u88c5\u7684\u5e93\uff0c\u8bf7\u786e\u4fdd\u5728\u6b64\u5904\u5217\u51fa /usr/lib \uff1b \u70b9\u51fbOK\u6309\u94ae\u3002 \u6b65\u9aa4 5 \u2014\u2014 \u544a\u8bc9\u94fe\u63a5\u5668\u7a0b\u5e8f\u4f7f\u7528\u54ea\u4e9b\u5e93 \u5bf9\u4e8e\u6b65\u9aa4 5\uff0c\u6211\u4eec\u9700\u8981\u5c06\u5e93\u6587\u4ef6\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u3002\u8fd9\u4e2a\u64cd\u4f5c\u6bcf\u4e2a\u9879\u76ee\u90fd\u9700\u8981\u505a\u4e00\u6b21\u3002 \u53f3\u952edefault workspace\u4e0b\u9762\u7684\u9879\u76ee\u540d\uff0c\u9009\u62e9\u201cBuild options\u201d\uff1b \u70b9\u51fb linker \u9009\u9879\u5361\u3002\u5728 \u201cLink libraries\u201d \u7a97\u53e3\u4e0b\u70b9\u51fb \u201cAdd\u201d \u6309\u94ae\u5e76\u6dfb\u52a0\u5e93\uff1b \u70b9\u51fbOK\u6309\u94ae\u3002 \u6b65\u9aa4 6\u548c 7 \u2014\u2014 #include \u5934\u6587\u4ef6 \u53ea\u9700\u50cf\u5f80\u5e38\u4e00\u6837\u5c06\u5e93\u4e2d\u7684\u5934\u6587\u4ef6 #include \u5230\u9879\u76ee\u4e2d\u3002 \u53c2\u89c1\uff1a A.1 - \u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93 \u83b7\u53d6\u66f4\u591a\u5173\u4e8e\u6b65\u9aa4 7 \u7684\u4fe1\u606f","title":"A.3 - \u5728Code Blocks\u4e2d\u4f7f\u7528\u5e93"},{"location":"A-4-C%2B%2BFAQ/","text":"Key Takeaway \u6709\u4e00\u4e9b\u95ee\u9898\u5f80\u5f80\u88ab\u95ee\u4e86\u4e00\u904d\u53c8\u4e00\u904d\u3002\u672cFAQ\u5c06\u5c1d\u8bd5\u56de\u7b54\u6700\u5e38\u89c1\u7684\u95ee\u9898\u3002 \u95ee: \u4e3a\u4ec0\u4e48\u4e0d\u5e94\u8be5\u4f7f\u7528 \u201c using namespace std \u201d? \u8bed\u53e5 using namespace std; \u4f7f\u7528\u4e86 using \u6307\u4ee4 \u2014\u2014\u5b83\u5c06\u67d0\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6240\u6709\u6807\u8bc6\u7b26\u90fd\u5bfc\u5165\u5230\u5f53\u524d\u547d\u540d\u7a7a\u95f4\u3002 \u4f60\u53ef\u80fd\u770b\u8fc7\u8fd9\u6837\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 #include using namespace std ; int main () { cout << \"Hello world!\" ; return 0 ; } \u8fd9\u4e48\u505a\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4e0d\u5fc5\u663e\u5f0f\u6307\u660e std \u7684\u547d\u540d\u7a7a\u95f4\uff0c\u518d\u4e5f\u4e0d\u7528\u5199 std:: \u4e86\u3002\u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u53ea\u9700\u8981\u4f7f\u7528 cout \u800c\u4e0d\u9700\u8981\u4f7f\u7528 std::cout \u3002\u542c\u8d77\u6765\u5f88\u4e0d\u9519\u5bf9\u5427\uff1f \u7136\u800c\uff0c\u5f53\u7f16\u8bd1\u5668\u9047\u5230 using namespace std \u65f6\uff0c\u5b83\u4f1a\u5c06\u5728 namespace std \u4e2d\u627e\u5230\u7684\u6bcf\u4e00\u4e2a\u6807\u8bc6\u7b26\u90fd\u5bfc\u5165\u5168\u5c40\u4f5c\u7528\u57df(\u56e0\u4e3ausing\u6307\u4ee4\u5c31\u653e\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d)\u3002\u8fd9\u5e26\u6765\u4e863\u4e2a\u4e3b\u8981\u6311\u6218\uff1a \u4f60\u6240\u9009\u62e9\u7684\u6807\u8bc6\u7b26\u4e0e std \u540d\u79f0\u7a7a\u95f4\u4e2d\u5df2\u7ecf\u5b58\u5728\u7684\u6807\u8bc6\u7b26\u4e4b\u95f4\u53d1\u751f\u547d\u540d\u51b2\u7a81\u7684\u51e0\u7387\u5927\u5927\u589e\u52a0\uff1b \u6807\u51c6\u5e93\u7684\u65b0\u7248\u672c\u53ef\u80fd\u4f1a\u7834\u574f\u4f60\u5f53\u524d\u53ef\u4ee5\u5de5\u4f5c\u7684\u7a0b\u5e8f\u3002\u8fd9\u4e9b\u672a\u6765\u7684\u7248\u672c\u53ef\u80fd\u4f1a\u5f15\u5165\u5bfc\u81f4\u65b0\u7684\u547d\u540d\u51b2\u7a81\u7684\u540d\u79f0\uff0c\u6216\u8005\u5728\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u7684\u884c\u4e3a\u53ef\u80fd\u4f1a\u6084\u65e0\u58f0\u606f\u5730\u51fa\u4e4e\u610f\u6599\u5730\u6539\u53d8! \u7f3a\u5c11 std:: \u524d\u7f00\u4f1a\u4f7f\u8bfb\u8005\u96be\u4ee5\u533a\u5206\u54ea\u4e9b\u540d\u5b57\u5c5e\u4e8e std \u5e93\u54ea\u4e9b\u662f\u7528\u6237\u5b9a\u4e49\u7684\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u63a8\u8350\u907f\u514d\u4f7f\u7528 using namespace std (\u4ee5\u53ca\u5176\u4ed6 using \u8bed\u53e5)\u3002\u901a\u8fc7\u5b83\u8282\u7701\u7684\u6253\u5b57\u65f6\u95f4\u548c\u53ef\u80fd\u5e26\u6765\u7684\u98ce\u9669\u76f8\u6bd4\u662f\u4e0d\u5212\u7b97\u7684\u3002 \u76f8\u5173\u5185\u5bb9 \u53c2\u89c1 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 \u83b7\u53d6\u66f4\u591a\u4fe1\u606f\u3002 \u95ee\uff1a\u4e3a\u4ec0\u4e48\u4f7f\u7528\u67d0\u4e9b\u529f\u80fd\u65f6\u4e0d\u9700\u8981\u5305\u542b\u5934\u6587\u4ef6\uff1f \u5934\u6587\u4ef6\u53ef\u4ee5\u201c #include \u201d\u5176\u4ed6\u5934\u6587\u4ef6\u3002\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u5305\u542b\u4e00\u4e2a\u5934\u6587\u4ef6\u65f6\uff0c\u540c\u65f6\u5c31\u53ef\u4ee5\u83b7\u5f97\u5b83\u5305\u542b\u7684\u6240\u6709\u9644\u52a0\u5934\u6587\u4ef6(\u4ee5\u53ca\u90a3\u4e9b\u5934\u6587\u4ef6\u5305\u542b\u7684\u6240\u6709\u5934\u6587\u4ef6)\u3002\u6240\u6709\u6ca1\u6709\u663e\u5f0f\u5305\u542b\u7684\u9644\u52a0\u5934\u6587\u4ef6\u79f0\u4e3a\u201c\u4f20\u9012\u5305\u542b\u201d\u3002 \u6bd4\u65b9\u8bf4\uff0c main.cpp \u4e2d #included \uff0c\u540c\u65f6\u4f60\u7684\u7f16\u8bd1\u5668\u5728 \u4e2d #included (\u6216\u8005\u5176\u4ed6\u5934\u6587\u4ef6 #included )\u3002 \u5373\u4f7f\u5b83\u53ef\u4ee5\u5728\u7f16\u8bd1\u5668\u4e0a\u7f16\u8bd1\uff0c\u4e5f\u4e0d\u5e94\u8be5\u4f9d\u8d56\u5b83\u3002\u56e0\u4e3a\u4f60\u80fd\u591f\u7f16\u8bd1\u7684\u4e1c\u897f\u53ef\u80fd\u4e0d\u80fd\u5728\u5176\u4ed6\u7f16\u8bd1\u5668\u4e0a\u7f16\u8bd1\uff0c\u751a\u81f3\u4e0d\u80fd\u5728\u4f60\u7684\u7f16\u8bd1\u5668\u7684\u672a\u6765\u7248\u672c\u4e0a\u7f16\u8bd1\u3002 \u6ca1\u6709\u529e\u6cd5\u5728\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\u65f6\u53d1\u51fa\u8b66\u544a\uff0c\u6216\u9632\u6b62\u5b83\u53d1\u751f\u3002\u6211\u4eec\u80fd\u505a\u7684\u5c31\u662f\u4e3a\u6240\u4f7f\u7528\u7684\u6240\u6709\u4e1c\u897f\u663e\u5f0f\u5730\u5305\u62ec\u9002\u5f53\u7684\u5934\u6587\u4ef6\u3002\u5728\u51e0\u4e2a\u4e0d\u540c\u7684\u7f16\u8bd1\u5668\u4e0a\u7f16\u8bd1\u4f60\u7684\u7a0b\u5e8f\u53ef\u80fd\u6709\u52a9\u4e8e\u8bc6\u522b\u88ab\u5176\u4ed6\u7f16\u8bd1\u5668\u4f20\u9012\u5305\u542b\u7684\u5934\u6587\u4ef6\u3002 \u76f8\u5173\u5185\u5bb9 \u53c2\u89c1 2.11 - \u5934\u6587\u4ef6 \u95ee\uff1a\u4e3a\u4ec0\u4e48\uff08\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u4ee3\u7801\uff09\u6709\u8fd9\u6837\u7684\u7ed3\u679c\uff1f \u5f53\u7a0b\u5e8f\u6267\u884c\u975eC++\u6807\u51c6\u5b9a\u4e49\u7684\u64cd\u4f5c\u65f6\uff0c\u4f1a\u53d1\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u5b9e\u73b0\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u4ee3\u7801\u53ef\u80fd\u4f1a\u51fa\u73b0\u4ee5\u4e0b\u4efb\u4f55\u75c7\u72b6\uff1a \u7a0b\u5e8f\u6bcf\u6b21\u8fd0\u884c\u90fd\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u7ed3\u679c\uff1b \u7a0b\u5e8f\u59cb\u7ec8\u4ea7\u751f\u76f8\u540c\u7684\u9519\u8bef\u7ed3\u679c\uff1b \u7a0b\u5e8f\u884c\u4e3a\u4e0d\u4e00\u81f4(\u6709\u65f6\u4ea7\u751f\u6b63\u786e\u7684\u7ed3\u679c\uff0c\u6709\u65f6\u4e0d)\uff1b \u7a0b\u5e8f\u4f3c\u4e4e\u5728\u5de5\u4f5c\uff0c\u4f46\u5728\u7a0b\u5e8f\u540e\u9762\u4ea7\u751f\u4e86\u4e0d\u6b63\u786e\u7684\u7ed3\u679c\uff1b \u7a0b\u5e8f\u7acb\u5373\u5d29\u6e83\u6216\u4e00\u6bb5\u65f6\u95f4\u540e\u5d29\u6e83\uff1b \u7a0b\u5e8f\u53ef\u4ee5\u5728\u4e00\u4e9b\u7f16\u8bd1\u5668\u4e0a\u5de5\u4f5c\uff0c\u4f46\u4e0d\u80fd\u5728\u5176\u4ed6\u7f16\u8bd1\u5668\u4e0a\u5de5\u4f5c\uff1b \u7a0b\u5e8f\u53ef\u4ee5\u5de5\u4f5c\uff0c\u4f46\u662f\u5728\u6539\u53d8\u4e00\u4e9b\u5176\u4ed6\u770b\u8d77\u6765\u4e0d\u76f8\u5173\u7684\u4ee3\u7801\u540e\u5c31\u4e0d\u80fd\u5de5\u4f5c\u4e86 \u5f53\u7136\uff0c\u4f60\u7684\u4ee3\u7801\u4e5f\u53ef\u80fd\u4f1a\u4ea7\u751f\u6b63\u786e\u7684\u7ed3\u679c\u3002 \u8bfb\u8005\u7ecf\u5e38\u4f1a\u5728\u63d0\u95ee\u4e2d\u95ee\u5230\u4e3a\u4ec0\u4e48\u5728\u4ed6\u4eec\u7684\u7cfb\u7edf\u4e0a\u4f1a\u4ea7\u751f\u7279\u5b9a\u7684\u73b0\u8c61\uff0c\u662f\u4ec0\u4e48\u539f\u56e0\u5bfc\u81f4\u7684\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u5f88\u96be\u8bf4\uff0c\u56e0\u4e3a\u751f\u6210\u7684\u7ed3\u679c\u53ef\u80fd\u53d6\u51b3\u4e8e\u5f53\u524d\u7a0b\u5e8f\u72b6\u6001\u3001\u7f16\u8bd1\u5668\u8bbe\u7f6e\u3001\u7f16\u8bd1\u5668\u5982\u4f55\u5b9e\u73b0\u67d0\u4e2a\u7279\u6027\u3001\u8ba1\u7b97\u673a\u7684\u67b6\u6784\u548c/\u6216\u64cd\u4f5c\u7cfb\u7edf\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6253\u5370\u4e00\u4e2a\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\u7684\u503c\uff0c\u53ef\u80fd\u4f1a\u5f97\u5230\u5783\u573e\uff0c\u6216\u8005\u603b\u662f\u5f97\u5230\u4e00\u4e2a\u7279\u5b9a\u7684\u503c\u3002\u8fd9\u53d6\u51b3\u4e8e\u5b83\u662f\u4ec0\u4e48\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u7f16\u8bd1\u5668\u5982\u4f55\u5728\u5185\u5b58\u4e2d\u5e03\u5c40\u53d8\u91cf\uff0c\u4ee5\u53ca\u5728\u6b64\u4e4b\u524d\u5185\u5b58\u4e2d\u6709\u4ec0\u4e48(\u53ef\u80fd\u53d7\u5230\u64cd\u4f5c\u7cfb\u7edf\u6216\u5728\u6b64\u70b9\u4e4b\u524d\u7684\u7a0b\u5e8f\u72b6\u6001\u7684\u5f71\u54cd)\u3002 \u867d\u7136\u56de\u7b54\u4e0a\u8ff0\u95ee\u9898\u53ef\u80fd\u5f88\u6709\u8da3\uff0c\u4f46\u603b\u4f53\u4e0a\u8fd9\u6837\u7684\u7b54\u6848\u5e76\u6ca1\u6709\u4ec0\u4e48\u7528\u7528(\u800c\u4e14\u53ef\u80fd\u4f1a\u5728\u5176\u4ed6\u4e1c\u897f\u53d1\u751f\u53d8\u5316\u65f6\u53d1\u751f\u53d8\u5316)\u3002\u8fd9\u5c31\u50cf\u5728\u95ee:\u201c\u5f53\u6211\u628a\u5b89\u5168\u5e26\u7a7f\u8fc7\u65b9\u5411\u76d8\u5e76\u8fde\u63a5\u5230\u6cb9\u95e8\u4e0a\u65f6\uff0c\u4e3a\u4ec0\u4e48\u6211\u5728\u96e8\u5929\u8f6c\u5934\u65f6\uff0c\u6c7d\u8f66\u4f1a\u5411\u8f6c\uff1f\u201d\u6700\u597d\u7684\u7b54\u6848\u4e0d\u662f\u5bf9\u6b63\u5728\u53d1\u751f\u7684\u4e8b\u60c5\u505a\u51fa\u89e3\u91ca\uff0c\u800c\u662f\u201c\u4e0d\u8981\u90a3\u6837\u505a\u201d\u3002 \u76f8\u5173\u5185\u5bb9 \u672a\u5b9a\u4e49\u884c\u4e3a \u8bf7\u53c2\u8003 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u95ee\uff1a\u6211\u5c1d\u8bd5\u7f16\u8bd1\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5b83\u672c\u5e94\u8be5\u5de5\u4f5c\u5374\u7f16\u8bd1\u62a5\u9519\u4e86\uff0c\u4e3a\u4ec0\u4e48\uff1f \u6700\u5e38\u89c1\u7684\u539f\u56e0\u662f\uff0c\u4f60\u7684\u9879\u76ee\u6b63\u5728\u4f7f\u7528\u8bed\u8a00\u6807\u51c6\u662f\u4e0d\u5bf9\u7684\u3002 C++\u5728\u6bcf\u4e2a\u65b0\u7684\u8bed\u8a00\u6807\u51c6\u4e2d\u90fd\u5f15\u5165\u4e86\u8bb8\u591a\u65b0\u7279\u6027\u3002\u5982\u679c\u6211\u4eec\u7684\u4e00\u4e2a\u793a\u4f8b\u4f7f\u7528C++ 17\u4e2d\u5f15\u5165\u7684\u7279\u6027\uff0c\u4f46\u4f60\u7684\u7a0b\u5e8f\u6b63\u5728\u4f7f\u7528C++ 14\u8bed\u8a00\u6807\u51c6\u7f16\u8bd1\uff0c\u90a3\u4e48\u5b83\u5c06\u65e0\u6cd5\u7f16\u8bd1\uff0c\u56e0\u4e3a\u6211\u4eec\u6b63\u5728\u4f7f\u7528\u7684\u7279\u6027\u4e0d\u5b58\u5728\u3002\u5c1d\u8bd5\u6807\u51c6\u8bbe\u7f6e\u4e3a\u7f16\u8bd1\u5668\u652f\u6301\u7684\u6700\u65b0\u7248\u672c\uff0c\u770b\u770b\u8fd9\u662f\u5426\u89e3\u51b3\u4e86\u95ee\u9898\u3002 \u76f8\u5173\u5185\u5bb9 \u53c2\u8003 0.12 -- Configuring your compiler: Choosing a language standard . \u4e5f\u6709\u53ef\u80fd\u4f60\u7684\u7f16\u8bd1\u5668\u8fd8\u4e0d\u652f\u6301\u7279\u5b9a\u7684\u7279\u6027\uff0c\u6216\u8005\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6709\u4e00\u4e2abug\u5bfc\u81f4\u5176\u65e0\u6cd5\u4f7f\u7528\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5c1d\u8bd5\u5c06\u7f16\u8bd1\u5668\u66f4\u65b0\u5230\u53ef\u7528\u7684\u6700\u65b0\u7248\u672c\u3002","title":"A.4 \u2014 C++ FAQ"},{"location":"A-4-C%2B%2BFAQ/#using-namespace-std","text":"\u8bed\u53e5 using namespace std; \u4f7f\u7528\u4e86 using \u6307\u4ee4 \u2014\u2014\u5b83\u5c06\u67d0\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6240\u6709\u6807\u8bc6\u7b26\u90fd\u5bfc\u5165\u5230\u5f53\u524d\u547d\u540d\u7a7a\u95f4\u3002 \u4f60\u53ef\u80fd\u770b\u8fc7\u8fd9\u6837\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 #include using namespace std ; int main () { cout << \"Hello world!\" ; return 0 ; } \u8fd9\u4e48\u505a\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4e0d\u5fc5\u663e\u5f0f\u6307\u660e std \u7684\u547d\u540d\u7a7a\u95f4\uff0c\u518d\u4e5f\u4e0d\u7528\u5199 std:: \u4e86\u3002\u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u53ea\u9700\u8981\u4f7f\u7528 cout \u800c\u4e0d\u9700\u8981\u4f7f\u7528 std::cout \u3002\u542c\u8d77\u6765\u5f88\u4e0d\u9519\u5bf9\u5427\uff1f \u7136\u800c\uff0c\u5f53\u7f16\u8bd1\u5668\u9047\u5230 using namespace std \u65f6\uff0c\u5b83\u4f1a\u5c06\u5728 namespace std \u4e2d\u627e\u5230\u7684\u6bcf\u4e00\u4e2a\u6807\u8bc6\u7b26\u90fd\u5bfc\u5165\u5168\u5c40\u4f5c\u7528\u57df(\u56e0\u4e3ausing\u6307\u4ee4\u5c31\u653e\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d)\u3002\u8fd9\u5e26\u6765\u4e863\u4e2a\u4e3b\u8981\u6311\u6218\uff1a \u4f60\u6240\u9009\u62e9\u7684\u6807\u8bc6\u7b26\u4e0e std \u540d\u79f0\u7a7a\u95f4\u4e2d\u5df2\u7ecf\u5b58\u5728\u7684\u6807\u8bc6\u7b26\u4e4b\u95f4\u53d1\u751f\u547d\u540d\u51b2\u7a81\u7684\u51e0\u7387\u5927\u5927\u589e\u52a0\uff1b \u6807\u51c6\u5e93\u7684\u65b0\u7248\u672c\u53ef\u80fd\u4f1a\u7834\u574f\u4f60\u5f53\u524d\u53ef\u4ee5\u5de5\u4f5c\u7684\u7a0b\u5e8f\u3002\u8fd9\u4e9b\u672a\u6765\u7684\u7248\u672c\u53ef\u80fd\u4f1a\u5f15\u5165\u5bfc\u81f4\u65b0\u7684\u547d\u540d\u51b2\u7a81\u7684\u540d\u79f0\uff0c\u6216\u8005\u5728\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u7684\u884c\u4e3a\u53ef\u80fd\u4f1a\u6084\u65e0\u58f0\u606f\u5730\u51fa\u4e4e\u610f\u6599\u5730\u6539\u53d8! \u7f3a\u5c11 std:: \u524d\u7f00\u4f1a\u4f7f\u8bfb\u8005\u96be\u4ee5\u533a\u5206\u54ea\u4e9b\u540d\u5b57\u5c5e\u4e8e std \u5e93\u54ea\u4e9b\u662f\u7528\u6237\u5b9a\u4e49\u7684\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u63a8\u8350\u907f\u514d\u4f7f\u7528 using namespace std (\u4ee5\u53ca\u5176\u4ed6 using \u8bed\u53e5)\u3002\u901a\u8fc7\u5b83\u8282\u7701\u7684\u6253\u5b57\u65f6\u95f4\u548c\u53ef\u80fd\u5e26\u6765\u7684\u98ce\u9669\u76f8\u6bd4\u662f\u4e0d\u5212\u7b97\u7684\u3002 \u76f8\u5173\u5185\u5bb9 \u53c2\u89c1 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 \u83b7\u53d6\u66f4\u591a\u4fe1\u606f\u3002","title":"\u95ee: \u4e3a\u4ec0\u4e48\u4e0d\u5e94\u8be5\u4f7f\u7528 \u201cusing namespace std\u201d?"},{"location":"A-4-C%2B%2BFAQ/#_1","text":"\u5934\u6587\u4ef6\u53ef\u4ee5\u201c #include \u201d\u5176\u4ed6\u5934\u6587\u4ef6\u3002\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u5305\u542b\u4e00\u4e2a\u5934\u6587\u4ef6\u65f6\uff0c\u540c\u65f6\u5c31\u53ef\u4ee5\u83b7\u5f97\u5b83\u5305\u542b\u7684\u6240\u6709\u9644\u52a0\u5934\u6587\u4ef6(\u4ee5\u53ca\u90a3\u4e9b\u5934\u6587\u4ef6\u5305\u542b\u7684\u6240\u6709\u5934\u6587\u4ef6)\u3002\u6240\u6709\u6ca1\u6709\u663e\u5f0f\u5305\u542b\u7684\u9644\u52a0\u5934\u6587\u4ef6\u79f0\u4e3a\u201c\u4f20\u9012\u5305\u542b\u201d\u3002 \u6bd4\u65b9\u8bf4\uff0c main.cpp \u4e2d #included \uff0c\u540c\u65f6\u4f60\u7684\u7f16\u8bd1\u5668\u5728 \u4e2d #included (\u6216\u8005\u5176\u4ed6\u5934\u6587\u4ef6 #included )\u3002 \u5373\u4f7f\u5b83\u53ef\u4ee5\u5728\u7f16\u8bd1\u5668\u4e0a\u7f16\u8bd1\uff0c\u4e5f\u4e0d\u5e94\u8be5\u4f9d\u8d56\u5b83\u3002\u56e0\u4e3a\u4f60\u80fd\u591f\u7f16\u8bd1\u7684\u4e1c\u897f\u53ef\u80fd\u4e0d\u80fd\u5728\u5176\u4ed6\u7f16\u8bd1\u5668\u4e0a\u7f16\u8bd1\uff0c\u751a\u81f3\u4e0d\u80fd\u5728\u4f60\u7684\u7f16\u8bd1\u5668\u7684\u672a\u6765\u7248\u672c\u4e0a\u7f16\u8bd1\u3002 \u6ca1\u6709\u529e\u6cd5\u5728\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\u65f6\u53d1\u51fa\u8b66\u544a\uff0c\u6216\u9632\u6b62\u5b83\u53d1\u751f\u3002\u6211\u4eec\u80fd\u505a\u7684\u5c31\u662f\u4e3a\u6240\u4f7f\u7528\u7684\u6240\u6709\u4e1c\u897f\u663e\u5f0f\u5730\u5305\u62ec\u9002\u5f53\u7684\u5934\u6587\u4ef6\u3002\u5728\u51e0\u4e2a\u4e0d\u540c\u7684\u7f16\u8bd1\u5668\u4e0a\u7f16\u8bd1\u4f60\u7684\u7a0b\u5e8f\u53ef\u80fd\u6709\u52a9\u4e8e\u8bc6\u522b\u88ab\u5176\u4ed6\u7f16\u8bd1\u5668\u4f20\u9012\u5305\u542b\u7684\u5934\u6587\u4ef6\u3002 \u76f8\u5173\u5185\u5bb9 \u53c2\u89c1 2.11 - \u5934\u6587\u4ef6","title":"\u95ee\uff1a\u4e3a\u4ec0\u4e48\u4f7f\u7528\u67d0\u4e9b\u529f\u80fd\u65f6\u4e0d\u9700\u8981\u5305\u542b\u5934\u6587\u4ef6\uff1f"},{"location":"A-4-C%2B%2BFAQ/#_2","text":"\u5f53\u7a0b\u5e8f\u6267\u884c\u975eC++\u6807\u51c6\u5b9a\u4e49\u7684\u64cd\u4f5c\u65f6\uff0c\u4f1a\u53d1\u751f \u672a\u5b9a\u4e49\u884c\u4e3a \u3002\u5b9e\u73b0\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u4ee3\u7801\u53ef\u80fd\u4f1a\u51fa\u73b0\u4ee5\u4e0b\u4efb\u4f55\u75c7\u72b6\uff1a \u7a0b\u5e8f\u6bcf\u6b21\u8fd0\u884c\u90fd\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u7ed3\u679c\uff1b \u7a0b\u5e8f\u59cb\u7ec8\u4ea7\u751f\u76f8\u540c\u7684\u9519\u8bef\u7ed3\u679c\uff1b \u7a0b\u5e8f\u884c\u4e3a\u4e0d\u4e00\u81f4(\u6709\u65f6\u4ea7\u751f\u6b63\u786e\u7684\u7ed3\u679c\uff0c\u6709\u65f6\u4e0d)\uff1b \u7a0b\u5e8f\u4f3c\u4e4e\u5728\u5de5\u4f5c\uff0c\u4f46\u5728\u7a0b\u5e8f\u540e\u9762\u4ea7\u751f\u4e86\u4e0d\u6b63\u786e\u7684\u7ed3\u679c\uff1b \u7a0b\u5e8f\u7acb\u5373\u5d29\u6e83\u6216\u4e00\u6bb5\u65f6\u95f4\u540e\u5d29\u6e83\uff1b \u7a0b\u5e8f\u53ef\u4ee5\u5728\u4e00\u4e9b\u7f16\u8bd1\u5668\u4e0a\u5de5\u4f5c\uff0c\u4f46\u4e0d\u80fd\u5728\u5176\u4ed6\u7f16\u8bd1\u5668\u4e0a\u5de5\u4f5c\uff1b \u7a0b\u5e8f\u53ef\u4ee5\u5de5\u4f5c\uff0c\u4f46\u662f\u5728\u6539\u53d8\u4e00\u4e9b\u5176\u4ed6\u770b\u8d77\u6765\u4e0d\u76f8\u5173\u7684\u4ee3\u7801\u540e\u5c31\u4e0d\u80fd\u5de5\u4f5c\u4e86 \u5f53\u7136\uff0c\u4f60\u7684\u4ee3\u7801\u4e5f\u53ef\u80fd\u4f1a\u4ea7\u751f\u6b63\u786e\u7684\u7ed3\u679c\u3002 \u8bfb\u8005\u7ecf\u5e38\u4f1a\u5728\u63d0\u95ee\u4e2d\u95ee\u5230\u4e3a\u4ec0\u4e48\u5728\u4ed6\u4eec\u7684\u7cfb\u7edf\u4e0a\u4f1a\u4ea7\u751f\u7279\u5b9a\u7684\u73b0\u8c61\uff0c\u662f\u4ec0\u4e48\u539f\u56e0\u5bfc\u81f4\u7684\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u5f88\u96be\u8bf4\uff0c\u56e0\u4e3a\u751f\u6210\u7684\u7ed3\u679c\u53ef\u80fd\u53d6\u51b3\u4e8e\u5f53\u524d\u7a0b\u5e8f\u72b6\u6001\u3001\u7f16\u8bd1\u5668\u8bbe\u7f6e\u3001\u7f16\u8bd1\u5668\u5982\u4f55\u5b9e\u73b0\u67d0\u4e2a\u7279\u6027\u3001\u8ba1\u7b97\u673a\u7684\u67b6\u6784\u548c/\u6216\u64cd\u4f5c\u7cfb\u7edf\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6253\u5370\u4e00\u4e2a\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\u7684\u503c\uff0c\u53ef\u80fd\u4f1a\u5f97\u5230\u5783\u573e\uff0c\u6216\u8005\u603b\u662f\u5f97\u5230\u4e00\u4e2a\u7279\u5b9a\u7684\u503c\u3002\u8fd9\u53d6\u51b3\u4e8e\u5b83\u662f\u4ec0\u4e48\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u7f16\u8bd1\u5668\u5982\u4f55\u5728\u5185\u5b58\u4e2d\u5e03\u5c40\u53d8\u91cf\uff0c\u4ee5\u53ca\u5728\u6b64\u4e4b\u524d\u5185\u5b58\u4e2d\u6709\u4ec0\u4e48(\u53ef\u80fd\u53d7\u5230\u64cd\u4f5c\u7cfb\u7edf\u6216\u5728\u6b64\u70b9\u4e4b\u524d\u7684\u7a0b\u5e8f\u72b6\u6001\u7684\u5f71\u54cd)\u3002 \u867d\u7136\u56de\u7b54\u4e0a\u8ff0\u95ee\u9898\u53ef\u80fd\u5f88\u6709\u8da3\uff0c\u4f46\u603b\u4f53\u4e0a\u8fd9\u6837\u7684\u7b54\u6848\u5e76\u6ca1\u6709\u4ec0\u4e48\u7528\u7528(\u800c\u4e14\u53ef\u80fd\u4f1a\u5728\u5176\u4ed6\u4e1c\u897f\u53d1\u751f\u53d8\u5316\u65f6\u53d1\u751f\u53d8\u5316)\u3002\u8fd9\u5c31\u50cf\u5728\u95ee:\u201c\u5f53\u6211\u628a\u5b89\u5168\u5e26\u7a7f\u8fc7\u65b9\u5411\u76d8\u5e76\u8fde\u63a5\u5230\u6cb9\u95e8\u4e0a\u65f6\uff0c\u4e3a\u4ec0\u4e48\u6211\u5728\u96e8\u5929\u8f6c\u5934\u65f6\uff0c\u6c7d\u8f66\u4f1a\u5411\u8f6c\uff1f\u201d\u6700\u597d\u7684\u7b54\u6848\u4e0d\u662f\u5bf9\u6b63\u5728\u53d1\u751f\u7684\u4e8b\u60c5\u505a\u51fa\u89e3\u91ca\uff0c\u800c\u662f\u201c\u4e0d\u8981\u90a3\u6837\u505a\u201d\u3002 \u76f8\u5173\u5185\u5bb9 \u672a\u5b9a\u4e49\u884c\u4e3a \u8bf7\u53c2\u8003 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a \u3002","title":"\u95ee\uff1a\u4e3a\u4ec0\u4e48\uff08\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\u7684\u4ee3\u7801\uff09\u6709\u8fd9\u6837\u7684\u7ed3\u679c\uff1f"},{"location":"A-4-C%2B%2BFAQ/#_3","text":"\u6700\u5e38\u89c1\u7684\u539f\u56e0\u662f\uff0c\u4f60\u7684\u9879\u76ee\u6b63\u5728\u4f7f\u7528\u8bed\u8a00\u6807\u51c6\u662f\u4e0d\u5bf9\u7684\u3002 C++\u5728\u6bcf\u4e2a\u65b0\u7684\u8bed\u8a00\u6807\u51c6\u4e2d\u90fd\u5f15\u5165\u4e86\u8bb8\u591a\u65b0\u7279\u6027\u3002\u5982\u679c\u6211\u4eec\u7684\u4e00\u4e2a\u793a\u4f8b\u4f7f\u7528C++ 17\u4e2d\u5f15\u5165\u7684\u7279\u6027\uff0c\u4f46\u4f60\u7684\u7a0b\u5e8f\u6b63\u5728\u4f7f\u7528C++ 14\u8bed\u8a00\u6807\u51c6\u7f16\u8bd1\uff0c\u90a3\u4e48\u5b83\u5c06\u65e0\u6cd5\u7f16\u8bd1\uff0c\u56e0\u4e3a\u6211\u4eec\u6b63\u5728\u4f7f\u7528\u7684\u7279\u6027\u4e0d\u5b58\u5728\u3002\u5c1d\u8bd5\u6807\u51c6\u8bbe\u7f6e\u4e3a\u7f16\u8bd1\u5668\u652f\u6301\u7684\u6700\u65b0\u7248\u672c\uff0c\u770b\u770b\u8fd9\u662f\u5426\u89e3\u51b3\u4e86\u95ee\u9898\u3002 \u76f8\u5173\u5185\u5bb9 \u53c2\u8003 0.12 -- Configuring your compiler: Choosing a language standard . \u4e5f\u6709\u53ef\u80fd\u4f60\u7684\u7f16\u8bd1\u5668\u8fd8\u4e0d\u652f\u6301\u7279\u5b9a\u7684\u7279\u6027\uff0c\u6216\u8005\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6709\u4e00\u4e2abug\u5bfc\u81f4\u5176\u65e0\u6cd5\u4f7f\u7528\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5c1d\u8bd5\u5c06\u7f16\u8bd1\u5668\u66f4\u65b0\u5230\u53ef\u7528\u7684\u6700\u65b0\u7248\u672c\u3002","title":"\u95ee\uff1a\u6211\u5c1d\u8bd5\u7f16\u8bd1\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5b83\u672c\u5e94\u8be5\u5de5\u4f5c\u5374\u7f16\u8bd1\u62a5\u9519\u4e86\uff0c\u4e3a\u4ec0\u4e48\uff1f"},{"location":"Associations/","tags":["\u8bcd\u6c47\u8868","Associations"],"text":"","title":"\u5173\u8054"},{"location":"Associative-containers/","tags":["\u8bcd\u6c47\u8868","Associative-containers"],"text":"","title":"\u5173\u8054\u5bb9\u5668"},{"location":"B-1-introduction-to-C%2B%2B11/","text":"C++11 \u662f\u4ec0\u4e48\uff1f 2011\u5e748\u670811\u65e5\uff0c ISO (\u56fd\u9645\u6807\u51c6\u5316\u7ec4\u7ec7) \u901a\u8fc7\u4e86C++\u7684\u65b0\u7248\u672c\u2014\u2014C++11\u3002C++11 \u4e3aC++\u8bed\u8a00\u6dfb\u52a0\u4e86\u4e00\u7cfb\u5217\u5168\u65b0\u7684\u7279\u6027\uff01\u662f\u5426\u4f7f\u7528\u8fd9\u4e9b\u65b0\u529f\u80fd\u662f\u53ef\u9009\u7684\u2014\u2014\u4f46\u6beb\u65e0\u7591\u95ee\uff0c\u4f60\u4f1a\u53d1\u73b0\u5176\u4e2d\u4e00\u4e9b\u529f\u80fd\u662f\u975e\u5e38\u6709\u7528\u7684\u3002\u6559\u7a0b\u90fd\u5df2\u66f4\u65b0\u4e3aC++ 11\u517c\u5bb9\u3002 C++ 11\u7684\u8bbe\u8ba1\u76ee\u6807 Bjarne Stroustrup \u8fd9\u6837\u63cf\u8ff0C++11\u7684\u76ee\u6807\uff1a \u626c\u957f\u907f\u77ed\u2014\u2014\u6269\u5927C++\u7684\u4f18\u52bf\u800c\u4e0d\u662f\u8bd5\u56fe\u5c06C++\u6269\u5c55\u5230\u5b83\u53ef\u80fd\u8f83\u5f31\u7684\u65b0\u9886\u57df\uff08\u4f8b\u5982\u6709\u5927\u91cfGUI\u7684Windows\u5e94\u7528\u7a0b\u5e8f)\uff0c\u4e13\u6ce8\u4e8e\u8ba9\u5b83\u66f4\u597d\u5730\u505a\u5b83\u64c5\u957f\u7684\u4e8b\u60c5\u3002 \u4f7fC++\u66f4\u5bb9\u6613\u5b66\u4e60\u3001\u4f7f\u7528\u548c\u6559\u6388\u2014\u2014\u63d0\u4f9b\u4f7f\u8bed\u8a00\u66f4\u4e00\u81f4\u548c\u66f4\u5bb9\u6613\u4f7f\u7528\u7684\u529f\u80fd\u3002 \u4e3a\u6b64\uff0c\u7f16\u5199\u8be5\u8bed\u8a00\u7684\u59d4\u5458\u4f1a\u8bd5\u56fe\u9075\u5b88\u4ee5\u4e0b\u57fa\u672c\u539f\u5219\uff1a \u5c3d\u53ef\u80fd\u4fdd\u6301\u7a33\u5b9a\u6027\u4ee5\u53ca\u4e0e\u65e7\u7248\u672cC++\u548cC\u7684\u517c\u5bb9\u6027\u3002\u5728C++03\u80fd\u591f\u5de5\u4f5c\u7684\u7a0b\u5e8f\u4ecd\u7136\u5e94\u8be5\u53ef\u4ee5\u5728C++11\u4e0b\u5de5\u4f5c\u3002 \u5c06\u6838\u5fc3\u8bed\u8a00\u7684\u6269\u5c55\u6570\u91cf\u4fdd\u6301\u5728\u6700\u4f4e\u9650\u5ea6\uff0c\u5e76\u5c06\u5927\u90e8\u5206\u7684\u66f4\u6539\u653e\u5728\u6807\u51c6\u5e93\u4e2d(\u8fd9\u4e2a\u76ee\u6807\u5728\u8fd9\u4e2a\u7248\u672c\u4e2d\u6ca1\u6709\u5f88\u597d\u5730\u5b9e\u73b0) \u4e13\u6ce8\u4e8e\u6539\u8fdb\u62bd\u8c61\u673a\u5236(\u7c7b\u3001\u6a21\u677f)\uff0c\u800c\u4e0d\u662f\u6dfb\u52a0\u673a\u5236\u6765\u5904\u7406\u7279\u5b9a\u7684\u3001\u7f55\u89c1\u7684\u60c5\u51b5\u3002 \u4e3a\u65b0\u624b\u548c\u4e13\u5bb6\u6dfb\u52a0\u65b0\u7684\u529f\u80fd\u3002\u7ed9\u6bcf\u4e2a\u4eba\u4e00\u70b9\u597d\u5904\uff01 \u63d0\u9ad8\u7c7b\u578b\u5b89\u5168\uff0c\u9632\u6b62\u65e0\u5fc3\u7684\u9519\u8bef\u3002 \u63d0\u9ad8\u6027\u80fd\uff0c\u5e76\u5141\u8bb8C++\u76f4\u63a5\u4e0e\u786c\u4ef6\u534f\u540c\u5de5\u4f5c\u3002 \u8003\u8651\u53ef\u7528\u6027\u548c\u751f\u6001\u7cfb\u7edf\u95ee\u9898\u3002C++\u9700\u8981\u4e0e\u5176\u4ed6\u5de5\u5177\u5f88\u597d\u914d\u5408\uff0c\u6613\u4e8e\u4f7f\u7528\u548c\u6559\u6388\uff0c\u7b49\u7b49\u3002 C++ 11\u5728\u4e3b\u65e8\u601d\u60f3\u4e0a\u4e0eC++03\u76f8\u5dee\u4e0d\u5927\uff0c\u4f46\u5b83\u786e\u5b9e\u589e\u52a0\u4e86\u5927\u91cf\u7684\u65b0\u529f\u80fd\u3002 C++11\u4e2d\u7684\u91cd\u8981\u65b0\u7279\u6027 \u8fd9\u91cc\u662fC++ 11\u6dfb\u52a0\u7684\u4e3b\u8981\u7279\u6027\u7684\u5217\u8868\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e2a\u5217\u8868\u4e0d\u662f\u5168\u9762\u7684\uff0c\u800c\u662f\u65e8\u5728\u7a81\u51fa\u4e00\u4e9b\u611f\u5174\u8da3\u7684\u5173\u952e\u7279\u6027\u3002 auto ( 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad ) char16_t \u548c char32_t \u4ee5\u53ca\u5176\u4ed6\u5b57\u9762\u91cf\u7684\u652f\u6301( \u6682\u65e0\u6559\u7a0b ) constexpr ( 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf ) decltype ( \u6682\u65e0\u6559\u7a0b ) default \u4fee\u9970\u7b26 ( \u6682\u65e0\u6559\u7a0b ) \u59d4\u6258\u6784\u9020\u51fd\u6570 ( 13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570 ) delete \u4fee\u9970\u7b26 ( 14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57 ) \u679a\u4e3e\u7c7b ( 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 ) Extern templates ( \u6682\u65e0\u6559\u7a0b ) \u533f\u540d\u51fd\u6570 ( 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 ) \u548c \u95ed\u5305 ( 12.8 - lambda \u95ed\u5305 ) long long int ( 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 ) \u79fb\u52a8\u6784\u9020\u51fd\u6570 \u548c \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 ( M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c ) Noexcept \u8bf4\u660e\u7b26 (quick mention in 20.4 ) nullptr ( 9.7 - \u7a7a\u6307\u9488 ) override \u548c final \u8bf4\u660e\u7b26( 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b ) \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af ( 11.13 - for-each \u5faa\u73af ) \u53f3\u503c\u5f15\u7528 ( M.2 - \u53f3\u503c\u5f15\u7528 ) static_assert ( 7.17 - \u65ad\u8a00\u548c static_assert ) std::initializer_list ( 16.7 - std::initializer_list ) \u672b\u5c3e\u8fd4\u56de\u7c7b\u578b\u8bed\u6cd5( 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad ) \u7c7b\u578b\u522b\u540d ( 8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d ) typedef \u53ef\u4ee5\u7528\u4e8e\u6a21\u677f\u7c7b \u7edf\u4e00\u521d\u59cb\u5316 ( 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb ) \u81ea\u5b9a\u4e49\u5b57\u9762\u91cf ( \u6682\u65e0\u6559\u7a0b ) \u53ef\u53d8\u6a21\u677f(Variadic templates )( \u6682\u65e0\u6559\u7a0b ) Two >> symbols without a space between them will now properly be interpreted as closing a template object C++\u6807\u51c6\u5e93\u4e2d\u8fd8\u6709\u8bb8\u591a\u65b0\u7c7b\u53ef\u4f9b\u4f7f\u7528\u3002 \u66f4\u597d\u5730\u652f\u6301\u591a\u7ebf\u7a0b\u548c\u7ebf\u7a0b\u672c\u5730\u5b58\u50a8 ( \u6682\u65e0\u6559\u7a0b ) \u54c8\u5e0c\u8868 ( \u6682\u65e0\u6559\u7a0b ) \u6539\u8fdb\u968f\u673a\u6570\u751f\u6210(\u5728 7.19 - \u4f7f\u7528 Mersenne Twister \u751f\u6210\u968f\u673a\u6570 \u4e2d\u8fdb\u884c\u4e86\u8ba8\u8bba) \u5f15\u7528\u5305\u88c5 ( 18.9 - \u5bf9\u8c61\u5207\u7247 ) \u6b63\u5219\u8868\u8fbe\u5f0f ( \u6682\u65e0\u6559\u7a0b ) std::auto_ptr \u88ab\u5f03\u7528 ( M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb ) std::tuple ( \u6682\u65e0\u6559\u7a0b ) std::unique_ptr ( M.6 \u2014 std::unique_ptr )","title":"B.1 - C++11"},{"location":"B-1-introduction-to-C%2B%2B11/#c11","text":"2011\u5e748\u670811\u65e5\uff0c ISO (\u56fd\u9645\u6807\u51c6\u5316\u7ec4\u7ec7) \u901a\u8fc7\u4e86C++\u7684\u65b0\u7248\u672c\u2014\u2014C++11\u3002C++11 \u4e3aC++\u8bed\u8a00\u6dfb\u52a0\u4e86\u4e00\u7cfb\u5217\u5168\u65b0\u7684\u7279\u6027\uff01\u662f\u5426\u4f7f\u7528\u8fd9\u4e9b\u65b0\u529f\u80fd\u662f\u53ef\u9009\u7684\u2014\u2014\u4f46\u6beb\u65e0\u7591\u95ee\uff0c\u4f60\u4f1a\u53d1\u73b0\u5176\u4e2d\u4e00\u4e9b\u529f\u80fd\u662f\u975e\u5e38\u6709\u7528\u7684\u3002\u6559\u7a0b\u90fd\u5df2\u66f4\u65b0\u4e3aC++ 11\u517c\u5bb9\u3002","title":"C++11 \u662f\u4ec0\u4e48\uff1f"},{"location":"B-1-introduction-to-C%2B%2B11/#c-11","text":"Bjarne Stroustrup \u8fd9\u6837\u63cf\u8ff0C++11\u7684\u76ee\u6807\uff1a \u626c\u957f\u907f\u77ed\u2014\u2014\u6269\u5927C++\u7684\u4f18\u52bf\u800c\u4e0d\u662f\u8bd5\u56fe\u5c06C++\u6269\u5c55\u5230\u5b83\u53ef\u80fd\u8f83\u5f31\u7684\u65b0\u9886\u57df\uff08\u4f8b\u5982\u6709\u5927\u91cfGUI\u7684Windows\u5e94\u7528\u7a0b\u5e8f)\uff0c\u4e13\u6ce8\u4e8e\u8ba9\u5b83\u66f4\u597d\u5730\u505a\u5b83\u64c5\u957f\u7684\u4e8b\u60c5\u3002 \u4f7fC++\u66f4\u5bb9\u6613\u5b66\u4e60\u3001\u4f7f\u7528\u548c\u6559\u6388\u2014\u2014\u63d0\u4f9b\u4f7f\u8bed\u8a00\u66f4\u4e00\u81f4\u548c\u66f4\u5bb9\u6613\u4f7f\u7528\u7684\u529f\u80fd\u3002 \u4e3a\u6b64\uff0c\u7f16\u5199\u8be5\u8bed\u8a00\u7684\u59d4\u5458\u4f1a\u8bd5\u56fe\u9075\u5b88\u4ee5\u4e0b\u57fa\u672c\u539f\u5219\uff1a \u5c3d\u53ef\u80fd\u4fdd\u6301\u7a33\u5b9a\u6027\u4ee5\u53ca\u4e0e\u65e7\u7248\u672cC++\u548cC\u7684\u517c\u5bb9\u6027\u3002\u5728C++03\u80fd\u591f\u5de5\u4f5c\u7684\u7a0b\u5e8f\u4ecd\u7136\u5e94\u8be5\u53ef\u4ee5\u5728C++11\u4e0b\u5de5\u4f5c\u3002 \u5c06\u6838\u5fc3\u8bed\u8a00\u7684\u6269\u5c55\u6570\u91cf\u4fdd\u6301\u5728\u6700\u4f4e\u9650\u5ea6\uff0c\u5e76\u5c06\u5927\u90e8\u5206\u7684\u66f4\u6539\u653e\u5728\u6807\u51c6\u5e93\u4e2d(\u8fd9\u4e2a\u76ee\u6807\u5728\u8fd9\u4e2a\u7248\u672c\u4e2d\u6ca1\u6709\u5f88\u597d\u5730\u5b9e\u73b0) \u4e13\u6ce8\u4e8e\u6539\u8fdb\u62bd\u8c61\u673a\u5236(\u7c7b\u3001\u6a21\u677f)\uff0c\u800c\u4e0d\u662f\u6dfb\u52a0\u673a\u5236\u6765\u5904\u7406\u7279\u5b9a\u7684\u3001\u7f55\u89c1\u7684\u60c5\u51b5\u3002 \u4e3a\u65b0\u624b\u548c\u4e13\u5bb6\u6dfb\u52a0\u65b0\u7684\u529f\u80fd\u3002\u7ed9\u6bcf\u4e2a\u4eba\u4e00\u70b9\u597d\u5904\uff01 \u63d0\u9ad8\u7c7b\u578b\u5b89\u5168\uff0c\u9632\u6b62\u65e0\u5fc3\u7684\u9519\u8bef\u3002 \u63d0\u9ad8\u6027\u80fd\uff0c\u5e76\u5141\u8bb8C++\u76f4\u63a5\u4e0e\u786c\u4ef6\u534f\u540c\u5de5\u4f5c\u3002 \u8003\u8651\u53ef\u7528\u6027\u548c\u751f\u6001\u7cfb\u7edf\u95ee\u9898\u3002C++\u9700\u8981\u4e0e\u5176\u4ed6\u5de5\u5177\u5f88\u597d\u914d\u5408\uff0c\u6613\u4e8e\u4f7f\u7528\u548c\u6559\u6388\uff0c\u7b49\u7b49\u3002 C++ 11\u5728\u4e3b\u65e8\u601d\u60f3\u4e0a\u4e0eC++03\u76f8\u5dee\u4e0d\u5927\uff0c\u4f46\u5b83\u786e\u5b9e\u589e\u52a0\u4e86\u5927\u91cf\u7684\u65b0\u529f\u80fd\u3002","title":"C++ 11\u7684\u8bbe\u8ba1\u76ee\u6807"},{"location":"B-1-introduction-to-C%2B%2B11/#c11_1","text":"\u8fd9\u91cc\u662fC++ 11\u6dfb\u52a0\u7684\u4e3b\u8981\u7279\u6027\u7684\u5217\u8868\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e2a\u5217\u8868\u4e0d\u662f\u5168\u9762\u7684\uff0c\u800c\u662f\u65e8\u5728\u7a81\u51fa\u4e00\u4e9b\u611f\u5174\u8da3\u7684\u5173\u952e\u7279\u6027\u3002 auto ( 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad ) char16_t \u548c char32_t \u4ee5\u53ca\u5176\u4ed6\u5b57\u9762\u91cf\u7684\u652f\u6301( \u6682\u65e0\u6559\u7a0b ) constexpr ( 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf ) decltype ( \u6682\u65e0\u6559\u7a0b ) default \u4fee\u9970\u7b26 ( \u6682\u65e0\u6559\u7a0b ) \u59d4\u6258\u6784\u9020\u51fd\u6570 ( 13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570 ) delete \u4fee\u9970\u7b26 ( 14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57 ) \u679a\u4e3e\u7c7b ( 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 ) Extern templates ( \u6682\u65e0\u6559\u7a0b ) \u533f\u540d\u51fd\u6570 ( 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 ) \u548c \u95ed\u5305 ( 12.8 - lambda \u95ed\u5305 ) long long int ( 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 ) \u79fb\u52a8\u6784\u9020\u51fd\u6570 \u548c \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 ( M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c ) Noexcept \u8bf4\u660e\u7b26 (quick mention in 20.4 ) nullptr ( 9.7 - \u7a7a\u6307\u9488 ) override \u548c final \u8bf4\u660e\u7b26( 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b ) \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af ( 11.13 - for-each \u5faa\u73af ) \u53f3\u503c\u5f15\u7528 ( M.2 - \u53f3\u503c\u5f15\u7528 ) static_assert ( 7.17 - \u65ad\u8a00\u548c static_assert ) std::initializer_list ( 16.7 - std::initializer_list ) \u672b\u5c3e\u8fd4\u56de\u7c7b\u578b\u8bed\u6cd5( 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad ) \u7c7b\u578b\u522b\u540d ( 8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d ) typedef \u53ef\u4ee5\u7528\u4e8e\u6a21\u677f\u7c7b \u7edf\u4e00\u521d\u59cb\u5316 ( 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb ) \u81ea\u5b9a\u4e49\u5b57\u9762\u91cf ( \u6682\u65e0\u6559\u7a0b ) \u53ef\u53d8\u6a21\u677f(Variadic templates )( \u6682\u65e0\u6559\u7a0b ) Two >> symbols without a space between them will now properly be interpreted as closing a template object C++\u6807\u51c6\u5e93\u4e2d\u8fd8\u6709\u8bb8\u591a\u65b0\u7c7b\u53ef\u4f9b\u4f7f\u7528\u3002 \u66f4\u597d\u5730\u652f\u6301\u591a\u7ebf\u7a0b\u548c\u7ebf\u7a0b\u672c\u5730\u5b58\u50a8 ( \u6682\u65e0\u6559\u7a0b ) \u54c8\u5e0c\u8868 ( \u6682\u65e0\u6559\u7a0b ) \u6539\u8fdb\u968f\u673a\u6570\u751f\u6210(\u5728 7.19 - \u4f7f\u7528 Mersenne Twister \u751f\u6210\u968f\u673a\u6570 \u4e2d\u8fdb\u884c\u4e86\u8ba8\u8bba) \u5f15\u7528\u5305\u88c5 ( 18.9 - \u5bf9\u8c61\u5207\u7247 ) \u6b63\u5219\u8868\u8fbe\u5f0f ( \u6682\u65e0\u6559\u7a0b ) std::auto_ptr \u88ab\u5f03\u7528 ( M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb ) std::tuple ( \u6682\u65e0\u6559\u7a0b ) std::unique_ptr ( M.6 \u2014 std::unique_ptr )","title":"C++11\u4e2d\u7684\u91cd\u8981\u65b0\u7279\u6027"},{"location":"B-2-introduction-to-C%2B%2B14/","text":"Key Takeaway C++14 \u662f\u4ec0\u4e48\uff1f 2014\u5e748\u670818\u65e5\uff0c ISO (\u56fd\u9645\u6807\u51c6\u5316\u7ec4\u7ec7) \u901a\u8fc7\u4e86C++\u7684\u65b0\u7248\u672c\u2014\u2014C++14\u3002C++14\u6ca1\u6709\u50cfC++11\u90a3\u6837\u6dfb\u52a0\u5927\u91cf\u65b0\u7279\u6027\uff0c\u76f8\u5bf9\u6765\u8bf4\u5b83\u53ea\u8fdb\u884c\u4e86\u8f83\u5c11\u5730\u66f4\u65b0\uff0c\u4e3b\u8981\u4e13\u6ce8\u4e8e\u4fee\u6539bug\u548c\u6539\u8fdb\u8bed\u8a00\u3002 C++14 \u4e2d\u7684\u6539\u8fdb \u8fd9\u91cc\u662fC++14\u6dfb\u52a0\u7684\u4e3b\u8981\u6539\u8fdb\u7684\u5217\u8868\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e2a\u5217\u8868\u4e0d\u662f\u5168\u9762\u7684\uff0c\u800c\u662f\u65e8\u5728\u7a81\u51fa\u4e00\u4e9b\u611f\u5174\u8da3\u7684\u5173\u952e\u6539\u8fdb\u3002 \u805a\u5408\u6210\u5458\u521d\u59cb\u5316( 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 ) \u4e8c\u8fdb\u5236\u5b57\u9762\u91cf ( 4.15 - \u5b57\u9762\u91cf ) [[deprecated]] \u5c5e\u6027 ( \u6682\u65e0\u6559\u7a0b ) \u6570\u5b57\u5206\u9694\u7b26 ( 4.15 - \u5b57\u9762\u91cf ) \u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad( 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad ) \u6cdb\u578b lambdas ( 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 ) Relaxed constexpr functions ( 6.14 - Constexpr \u548c consteval \u51fd\u6570 ) \u53d8\u91cf\u6a21\u677f ( \u6682\u65e0\u6559\u7a0b ) std::make_unique ( M.6 \u2014 std::unique_ptr )","title":"B.2 - C++14"},{"location":"B-2-introduction-to-C%2B%2B14/#c14","text":"2014\u5e748\u670818\u65e5\uff0c ISO (\u56fd\u9645\u6807\u51c6\u5316\u7ec4\u7ec7) \u901a\u8fc7\u4e86C++\u7684\u65b0\u7248\u672c\u2014\u2014C++14\u3002C++14\u6ca1\u6709\u50cfC++11\u90a3\u6837\u6dfb\u52a0\u5927\u91cf\u65b0\u7279\u6027\uff0c\u76f8\u5bf9\u6765\u8bf4\u5b83\u53ea\u8fdb\u884c\u4e86\u8f83\u5c11\u5730\u66f4\u65b0\uff0c\u4e3b\u8981\u4e13\u6ce8\u4e8e\u4fee\u6539bug\u548c\u6539\u8fdb\u8bed\u8a00\u3002","title":"C++14 \u662f\u4ec0\u4e48\uff1f"},{"location":"B-2-introduction-to-C%2B%2B14/#c14_1","text":"\u8fd9\u91cc\u662fC++14\u6dfb\u52a0\u7684\u4e3b\u8981\u6539\u8fdb\u7684\u5217\u8868\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e2a\u5217\u8868\u4e0d\u662f\u5168\u9762\u7684\uff0c\u800c\u662f\u65e8\u5728\u7a81\u51fa\u4e00\u4e9b\u611f\u5174\u8da3\u7684\u5173\u952e\u6539\u8fdb\u3002 \u805a\u5408\u6210\u5458\u521d\u59cb\u5316( 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 ) \u4e8c\u8fdb\u5236\u5b57\u9762\u91cf ( 4.15 - \u5b57\u9762\u91cf ) [[deprecated]] \u5c5e\u6027 ( \u6682\u65e0\u6559\u7a0b ) \u6570\u5b57\u5206\u9694\u7b26 ( 4.15 - \u5b57\u9762\u91cf ) \u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad( 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad ) \u6cdb\u578b lambdas ( 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 ) Relaxed constexpr functions ( 6.14 - Constexpr \u548c consteval \u51fd\u6570 ) \u53d8\u91cf\u6a21\u677f ( \u6682\u65e0\u6559\u7a0b ) std::make_unique ( M.6 \u2014 std::unique_ptr )","title":"C++14 \u4e2d\u7684\u6539\u8fdb"},{"location":"B-3-introduction-to-C%2B%2B17/","text":"Key Takeaway C++17 \u662f\u4ec0\u4e48\uff1f 2017\u5e749\u6708\uff0c ISO (\u56fd\u9645\u6807\u51c6\u5316\u7ec4\u7ec7) \u901a\u8fc7\u4e86C++\u7684\u65b0\u7248\u672c\u2014\u2014C++17\u3002C++17\u5305\u542b\u4e86\u5927\u91cf\u7684\u66f4\u65b0\u3002 C++17 \u4e2d\u7684\u6539\u8fdb \u8fd9\u91cc\u662fC++17\u6dfb\u52a0\u7684\u4e3b\u8981\u6539\u8fdb\u7684\u5217\u8868\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e2a\u5217\u8868\u4e0d\u662f\u5168\u9762\u7684\uff0c\u800c\u662f\u65e8\u5728\u7a81\u51fa\u4e00\u4e9b\u611f\u5174\u8da3\u7684\u5173\u952e\u6539\u8fdb\u3002 __has_include \u9884\u5904\u7406\u5668\u6807\u8bc6\u7b26\uff0c\u68c0\u67e5\u5934\u6587\u4ef6\u662f\u5426\u5305\u542b( \u6682\u65e0\u6559\u7a0b ) \u7f16\u8bd1\u65f6\u6c42\u503c\u7684 if \u8bed\u53e5( \u6682\u65e0\u6559\u7a0b ) if \u548c switch \u8bed\u53e5\u4e2d\u7684\u521d\u59cb\u5316\u503c( \u6682\u65e0\u6559\u7a0b ) \u5185\u8054\u53d8\u91cf ( 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf ) \u9ad8\u9636\u51fd\u6570\u8868\u8fbe\u5f0f(Fold expressions) ( \u6682\u65e0\u6559\u7a0b ) \u5d4c\u5957\u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u901a\u8fc7 namespace X::Y \u6765\u5b9a\u4e49( 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 ) \u79fb\u9664 std::auto_ptr \u548c\u5176\u4ed6\u7684\u4e00\u4e9b\u5e9f\u5f03\u7c7b\u578b static_assert \u4e0d\u518d\u8981\u6c42\u8bca\u65ad\u4fe1\u606f\u53c2\u6570( 7.17 - \u65ad\u8a00\u548c static_assert ) std::any ( \u6682\u65e0\u6559\u7a0b ) std::byte ( \u6682\u65e0\u6559\u7a0b ) std::filesystem ( \u6682\u65e0\u6559\u7a0b ) std::optional ( \u6682\u65e0\u6559\u7a0b ) std::shared_ptr \u53ef\u7528\u4e8e\u7ba1\u7406C\u8bed\u8a00\u6570\u7ec4 (\u4f46\u8fd8\u4e0d\u80fd\u901a\u8fc7 std::make_shared \u521b\u5efa) ( M.7 \u2014 std::shared_ptr ) std::size ( 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 ) std::string_view ( 4.18 - std::string_view \u7b80\u4ecb ) \u7ed3\u6784\u5316\u7ed1\u5b9a\u58f0\u660e \u6784\u9020\u51fd\u6570\u7684\u6a21\u677f\u63a8\u65ad( \u6682\u65e0\u6559\u7a0b ) \u4e09\u5b57\u7b26\u7ec4\uff08Trigraphs\uff09\u88ab\u79fb\u9664 \u53ef\u4ee5\u5728\u6a21\u677f\u7684\u6a21\u677f\u53c2\u6570\u4e2d\u4f7f\u7528 typename \uff08\u800c\u4e0d\u662f class \uff09 UTF-8 (u8) \u5b57\u7b26\u5b57\u9762\u91cf( \u6682\u65e0\u6559\u7a0b )","title":"B.3 - C++17"},{"location":"B-3-introduction-to-C%2B%2B17/#c17","text":"2017\u5e749\u6708\uff0c ISO (\u56fd\u9645\u6807\u51c6\u5316\u7ec4\u7ec7) \u901a\u8fc7\u4e86C++\u7684\u65b0\u7248\u672c\u2014\u2014C++17\u3002C++17\u5305\u542b\u4e86\u5927\u91cf\u7684\u66f4\u65b0\u3002","title":"C++17 \u662f\u4ec0\u4e48\uff1f"},{"location":"B-3-introduction-to-C%2B%2B17/#c17_1","text":"\u8fd9\u91cc\u662fC++17\u6dfb\u52a0\u7684\u4e3b\u8981\u6539\u8fdb\u7684\u5217\u8868\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e2a\u5217\u8868\u4e0d\u662f\u5168\u9762\u7684\uff0c\u800c\u662f\u65e8\u5728\u7a81\u51fa\u4e00\u4e9b\u611f\u5174\u8da3\u7684\u5173\u952e\u6539\u8fdb\u3002 __has_include \u9884\u5904\u7406\u5668\u6807\u8bc6\u7b26\uff0c\u68c0\u67e5\u5934\u6587\u4ef6\u662f\u5426\u5305\u542b( \u6682\u65e0\u6559\u7a0b ) \u7f16\u8bd1\u65f6\u6c42\u503c\u7684 if \u8bed\u53e5( \u6682\u65e0\u6559\u7a0b ) if \u548c switch \u8bed\u53e5\u4e2d\u7684\u521d\u59cb\u5316\u503c( \u6682\u65e0\u6559\u7a0b ) \u5185\u8054\u53d8\u91cf ( 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf ) \u9ad8\u9636\u51fd\u6570\u8868\u8fbe\u5f0f(Fold expressions) ( \u6682\u65e0\u6559\u7a0b ) \u5d4c\u5957\u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u901a\u8fc7 namespace X::Y \u6765\u5b9a\u4e49( 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 ) \u79fb\u9664 std::auto_ptr \u548c\u5176\u4ed6\u7684\u4e00\u4e9b\u5e9f\u5f03\u7c7b\u578b static_assert \u4e0d\u518d\u8981\u6c42\u8bca\u65ad\u4fe1\u606f\u53c2\u6570( 7.17 - \u65ad\u8a00\u548c static_assert ) std::any ( \u6682\u65e0\u6559\u7a0b ) std::byte ( \u6682\u65e0\u6559\u7a0b ) std::filesystem ( \u6682\u65e0\u6559\u7a0b ) std::optional ( \u6682\u65e0\u6559\u7a0b ) std::shared_ptr \u53ef\u7528\u4e8e\u7ba1\u7406C\u8bed\u8a00\u6570\u7ec4 (\u4f46\u8fd8\u4e0d\u80fd\u901a\u8fc7 std::make_shared \u521b\u5efa) ( M.7 \u2014 std::shared_ptr ) std::size ( 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 ) std::string_view ( 4.18 - std::string_view \u7b80\u4ecb ) \u7ed3\u6784\u5316\u7ed1\u5b9a\u58f0\u660e \u6784\u9020\u51fd\u6570\u7684\u6a21\u677f\u63a8\u65ad( \u6682\u65e0\u6559\u7a0b ) \u4e09\u5b57\u7b26\u7ec4\uff08Trigraphs\uff09\u88ab\u79fb\u9664 \u53ef\u4ee5\u5728\u6a21\u677f\u7684\u6a21\u677f\u53c2\u6570\u4e2d\u4f7f\u7528 typename \uff08\u800c\u4e0d\u662f class \uff09 UTF-8 (u8) \u5b57\u7b26\u5b57\u9762\u91cf( \u6682\u65e0\u6559\u7a0b )","title":"C++17 \u4e2d\u7684\u6539\u8fdb"},{"location":"B-4-introduction-to-C%2B%2B20/","text":"Key Takeaway C++20 \u662f\u4ec0\u4e48\uff1f 2020\u5e742\u6708\uff0c ISO (\u56fd\u9645\u6807\u51c6\u5316\u7ec4\u7ec7) \u901a\u8fc7\u4e86C++\u7684\u65b0\u7248\u672c\u2014\u2014C++20\u3002C++20\u662f\u81eaC++11\u4ee5\u6765\uff0c\u5305\u542b\u6700\u591a\u53d8\u5316\u7684\u7248\u672c\u3002 C++20 \u7684\u6539\u8fdb \u8fd9\u91cc\u5217\u51fa\u4e86C++ 20\u589e\u52a0\u7684\u4e3b\u8981\u6539\u8fdb\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e2a\u5217\u8868\u4e0d\u662f\u5168\u9762\u7684\uff0c\u800c\u662f\u65e8\u5728\u7a81\u51fa\u4e00\u4e9b\u611f\u5174\u8da3\u7684\u5173\u952e\u6539\u8fdb\u70b9\u3002 \u901a\u8fc7 auto \u53c2\u6570\u652f\u6301 \u7f29\u5199\u51fd\u6570\u6a21\u677f ( 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f ) Chrono \u6269\u5c55\u7528\u4e8e\u652f\u6301\u65e5\u5386\u548c\u65f6\u533a( \u6682\u65e0\u6559\u7a0b ) Concepts\uff0c\u7528\u4e8e\u5bf9\u6a21\u677f\u53c2\u6570\u8fdb\u884c\u9650\u5236 ( \u6682\u65e0\u6559\u7a0b ) Constexpr virtual functions, unions , try , catch , dynamic_cast , and typeid ( \u6682\u65e0\u6559\u7a0b ) Constinit \u5173\u952e\u5b57\uff0c\u7528\u4e8e\u65ad\u8a00\u67d0\u4e2a\u53d8\u91cf\u88ab\u9759\u6001\u521d\u59cb\u5316( \u6682\u65e0\u6559\u7a0b ) \u534f\u7a0b ( \u6682\u65e0\u6559\u7a0b ) \u6307\u5b9a\u521d\u59cb\u5316 ( 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 ) \u4f7f\u7528 consteval \u521b\u5efa\u5373\u65f6\u51fd\u6570 ( 6.14 - Constexpr \u548c consteval \u51fd\u6570 ) \u6a21\u5757\uff0c\u4f5c\u4e3a #include \u7684\u66ff\u4ee3\u54c1( \u6682\u65e0\u6559\u7a0b ) Ranges ( \u6682\u65e0\u6559\u7a0b ) std::erase ( \u6682\u65e0\u6559\u7a0b ) std::make_shared for arrays ( \u6682\u65e0\u6559\u7a0b ) std::map::contains() ( \u6682\u65e0\u6559\u7a0b ) std::span ( \u6682\u65e0\u6559\u7a0b ) \u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u5e93( \u6682\u65e0\u6559\u7a0b \uff0c\u53c2\u89c1 https://en.cppreference.com/w/cpp/utility/format ) \u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u4f5c\u4e3a\u6a21\u677f\u53c2\u6570 ( \u6682\u65e0\u6559\u7a0b ) \u4f7f\u7528\u98de\u8239\u8fd0\u7b97\u7b26 <=> \u8fdb\u884c\u4e09\u8def\u6bd4\u8f83( \u6682\u65e0\u6559\u7a0b ) Using scoped enums ( \u6682\u65e0\u6559\u7a0b ) Views ( \u6682\u65e0\u6559\u7a0b )","title":"B.4 - C++20"},{"location":"B-4-introduction-to-C%2B%2B20/#c20","text":"2020\u5e742\u6708\uff0c ISO (\u56fd\u9645\u6807\u51c6\u5316\u7ec4\u7ec7) \u901a\u8fc7\u4e86C++\u7684\u65b0\u7248\u672c\u2014\u2014C++20\u3002C++20\u662f\u81eaC++11\u4ee5\u6765\uff0c\u5305\u542b\u6700\u591a\u53d8\u5316\u7684\u7248\u672c\u3002","title":"C++20 \u662f\u4ec0\u4e48\uff1f"},{"location":"B-4-introduction-to-C%2B%2B20/#c20_1","text":"\u8fd9\u91cc\u5217\u51fa\u4e86C++ 20\u589e\u52a0\u7684\u4e3b\u8981\u6539\u8fdb\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e2a\u5217\u8868\u4e0d\u662f\u5168\u9762\u7684\uff0c\u800c\u662f\u65e8\u5728\u7a81\u51fa\u4e00\u4e9b\u611f\u5174\u8da3\u7684\u5173\u952e\u6539\u8fdb\u70b9\u3002 \u901a\u8fc7 auto \u53c2\u6570\u652f\u6301 \u7f29\u5199\u51fd\u6570\u6a21\u677f ( 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f ) Chrono \u6269\u5c55\u7528\u4e8e\u652f\u6301\u65e5\u5386\u548c\u65f6\u533a( \u6682\u65e0\u6559\u7a0b ) Concepts\uff0c\u7528\u4e8e\u5bf9\u6a21\u677f\u53c2\u6570\u8fdb\u884c\u9650\u5236 ( \u6682\u65e0\u6559\u7a0b ) Constexpr virtual functions, unions , try , catch , dynamic_cast , and typeid ( \u6682\u65e0\u6559\u7a0b ) Constinit \u5173\u952e\u5b57\uff0c\u7528\u4e8e\u65ad\u8a00\u67d0\u4e2a\u53d8\u91cf\u88ab\u9759\u6001\u521d\u59cb\u5316( \u6682\u65e0\u6559\u7a0b ) \u534f\u7a0b ( \u6682\u65e0\u6559\u7a0b ) \u6307\u5b9a\u521d\u59cb\u5316 ( 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 ) \u4f7f\u7528 consteval \u521b\u5efa\u5373\u65f6\u51fd\u6570 ( 6.14 - Constexpr \u548c consteval \u51fd\u6570 ) \u6a21\u5757\uff0c\u4f5c\u4e3a #include \u7684\u66ff\u4ee3\u54c1( \u6682\u65e0\u6559\u7a0b ) Ranges ( \u6682\u65e0\u6559\u7a0b ) std::erase ( \u6682\u65e0\u6559\u7a0b ) std::make_shared for arrays ( \u6682\u65e0\u6559\u7a0b ) std::map::contains() ( \u6682\u65e0\u6559\u7a0b ) std::span ( \u6682\u65e0\u6559\u7a0b ) \u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u5e93( \u6682\u65e0\u6559\u7a0b \uff0c\u53c2\u89c1 https://en.cppreference.com/w/cpp/utility/format ) \u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u4f5c\u4e3a\u6a21\u677f\u53c2\u6570 ( \u6682\u65e0\u6559\u7a0b ) \u4f7f\u7528\u98de\u8239\u8fd0\u7b97\u7b26 <=> \u8fdb\u884c\u4e09\u8def\u6bd4\u8f83( \u6682\u65e0\u6559\u7a0b ) Using scoped enums ( \u6682\u65e0\u6559\u7a0b ) Views ( \u6682\u65e0\u6559\u7a0b )","title":"C++20 \u7684\u6539\u8fdb"},{"location":"Binding/","tags":["\u8bcd\u6c47\u8868","Binding"],"text":"","title":"\u7ed1\u5b9a"},{"location":"C-style-casts/","tags":["\u8bcd\u6c47\u8868","C-style-casts"],"text":"","title":"C \u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362"},{"location":"Composition/","text":"","title":"Composition"},{"location":"Early-binding/","tags":["\u8bcd\u6c47\u8868","Early binding"],"text":"\u7f16\u8bd1\u5668(\u6216\u94fe\u63a5\u5668)\u80fd\u591f\u76f4\u63a5\u5c06\u6807\u8bc6\u7b26\u540d(\u5982\u51fd\u6570\u540d\u6216\u53d8\u91cf\u540d)\u4e0e\u673a\u5668\u5730\u5740\u5173\u8054\u8d77\u6765 \u533a\u522b \u540e\u671f\u7ed1\u5b9a","title":"\u65e9\u671f\u7ed1\u5b9a"},{"location":"Encapsulation/","tags":["\u8bcd\u6c47\u8868","Encapsulation"],"text":"","title":"\u5c01\u88c5"},{"location":"Expression-parameters/","tags":["\u8bcd\u6c47\u8868","Expression-parameters"],"text":"","title":"\u8868\u8fbe\u5f0f\u53c2\u6570"},{"location":"Frankenobject/","tags":["\u8bcd\u6c47\u8868","Frankenobject"],"text":"","title":"\u7f1d\u5408\u602a\u5bf9\u8c61"},{"location":"Late-binding/","tags":["\u8bcd\u6c47\u8868","Late binding"],"text":"","title":"\u540e\u671f\u7ed1\u5b9a"},{"location":"M-1-introduction-to-smart-pointers-and-move-semantics/","tags":["smart-pointers","move-semantics"],"text":"Key Takeaway \u79fb\u52a8\u8bed\u4e49\u548c\u62f7\u8d1d\u8bed\u4e49\u662f\u4e8c\u9009\u4e00\u7684\uff0c\u53ea\u6709\u5728\u53ef\u80fd\u53d1\u751f\u62f7\u8d1d\u8bed\u4e49\u7684\u5730\u65b9\uff0c\u624d\u5e94\u8be5\u8003\u8651\u5b83\u6267\u884c\u7684\u662f\u79fb\u52a8\u8bed\u4e49\u8fd8\u662f\u62f7\u8d1d\u8bed\u4e49\uff08\u53d6\u51b3\u4e8e\u53f3\u503c\u8fd8\u662f\u5de6\u503c\uff09 \u8003\u8651\u4e0b\u9762\u51fd\u6570\uff0c\u51fd\u6570\u4e2d\u52a8\u6001\u5206\u914d\u4e86\u4e00\u4e2a\u8d44\u6e90\uff1a 1 2 3 4 5 6 7 8 void someFunction () { Resource * ptr = new Resource (); // Resource is a struct or class // do stuff with ptr here delete ptr ; } \u5c3d\u7ba1\u4e0a\u9762\u7684\u4ee3\u7801\u770b\u8d77\u6765\u76f8\u5f53\u7b80\u5355\uff0c\u4f46\u662f\u5728\u5b9e\u8df5\u4e2d\u5f88\u5bb9\u6613\u5fd8\u8bb0\u91ca\u653e ptr \u3002\u5373\u4f7f\u4f60\u8bb0\u5f97\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u5220\u9664 ptr \uff0c\u5982\u679c\u51fd\u6570\u63d0\u524d\u9000\u51fa\uff0c\u4e5f\u6709\u8bb8\u591a\u56e0\u7d20\u5bfc\u81f4 ptr \u4e0d\u80fd\u88ab\u6b63\u786e\u5220\u9664\uff0c\u4f8b\u5982\u51fd\u6570\u7684\u63d0\u524d\u8fd4\u56de\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void someFunction () { Resource * ptr = new Resource (); int x ; std :: cout << \"Enter an integer: \" ; std :: cin >> x ; if ( x == 0 ) return ; // \u51fd\u6570\u63d0\u524d\u8fd4\u56de\uff0cptr \u65e0\u6cd5\u88ab\u5220\u9664\uff01 // do stuff with ptr here delete ptr ; } \u629b\u51fa\u5f02\u5e38\u4e5f\u53ef\u80fd\u4f1a\u5bfc\u81f4\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void someFunction () { Resource * ptr = new Resource (); int x ; std :: cout << \"Enter an integer: \" ; std :: cin >> x ; if ( x == 0 ) throw 0 ; // the function returns early, and ptr won\u2019t be deleted! // do stuff with ptr here delete ptr ; } \u5728\u4e0a\u8ff0\u4e24\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u63d0\u524d\u8fd4\u56de\u6216throw\u8bed\u53e5\uff0c\u90fd\u53ef\u80fd\u4f1a\u5bfc\u81f4\u51fd\u6570\u5728\u6ca1\u6709\u5220\u9664 ptr \u7684\u60c5\u51b5\u4e0b\u7ec8\u6b62\u3002\u56e0\u6b64\uff0c\u5206\u914d\u7ed9\u53d8\u91cf ptr \u7684\u5185\u5b58\u53ef\u80fd\u51fa\u73b0\u5185\u5b58\u6cc4\u6f0f(\u800c\u4e14\u662f\u6bcf\u6b21\u8c03\u7528\u6b64\u51fd\u6570\u5e76\u63d0\u524d\u8fd4\u56de\u65f6\u90fd\u4f1a\u6cc4\u6f0f)\u3002 \u672c\u8d28\u4e0a\uff0c\u53d1\u751f\u8fd9\u7c7b\u95ee\u9898\u662f\u56e0\u4e3a\u6307\u9488\u53d8\u91cf\u4e0d\u5177\u5907\u81ea\u6211\u6e05\u7406\u7684\u80fd\u529b\u3002 \u667a\u80fd\u6307\u9488\u7c7b\u53ef\u4ee5\u62ef\u6551\u6211\u4eec\u5417\uff1f \u5bf9\u4e8e\u7c7b\u5bf9\u8c61\u6765\u8bf4\uff0c\u5b83\u4eec\u6700\u5927\u7684\u4f18\u52bf\u662f\u53ef\u4ee5\u5728 \u79bb\u5f00\u4f5c\u7528\u57df \u662f\u81ea\u52a8\u6267\u884c \u6790\u6784\u51fd\u6570 \u3002\u8fd9\u6837\u4e00\u6765\uff0c\u5982\u679c\u6211\u4eec\u5728\u6784\u9020\u51fd\u6570\u4e2d\u5206\u914d\u6216\u83b7\u53d6\u5185\u5b58\uff0c\u5219\u53ef\u4ee5\u5728\u6790\u6784\u51fd\u6570\u4e2d\u8fdb\u884c\u91ca\u653e\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u786e\u4fdd\u5185\u5b58\u5728\u5bf9\u8c61\u88ab\u9500\u6bc1\u524d\uff08\u4e0d\u8bba\u662f\u79bb\u5f00\u4f5c\u7528\u57df\u8fd8\u662f\u663e\u5f0f\u5220\u9664\u7b49\uff09\u91ca\u653e\u3002\u8fd9\u4e5f\u6b63\u662f \u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09 \u7f16\u7a0b\u8303\u5f0f\u7684\u6838\u5fc3\u601d\u60f3\uff08\u89c1 13.9 - \u6790\u6784\u51fd\u6570 \uff09\u3002 \u90a3\u4e48\uff0c\u662f\u5426\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7c7b\uff0c\u5e2e\u52a9\u6211\u4eec\u7ba1\u7406\u548c\u6e05\u7406\u6307\u9488\u5462\uff1f\u5f53\u7136\u53ef\u4ee5\uff01 \u8003\u8651\u8bbe\u8ba1\u8fd9\u6837\u4e00\u4e2a\u7c7b\uff0c\u5b83\u7684\u552f\u4e00\u5de5\u4f5c\uff0c\u5c31\u662f\u7ba1\u7406\u4e00\u4e2a\u4f20\u5165\u7684\u6307\u9488\u3002\u5f53\u8be5\u7c7b\u5bf9\u8c61\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c\u91ca\u653e\u6307\u9488\u6240\u6307\u5411\u7684\u5185\u5b58\u3002\u53ea\u8981\u8be5\u5bf9\u8c61\u88ab\u58f0\u660e\u4e3a\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4fdd\u8bc1\uff0c\u5f53\u5b83\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff08\u4e0d\u8bba\u4f55\u65f6\u3001\u4e5f\u4e0d\u8bba\u51fd\u6570\u5982\u4f55\u7ec8\u6b62\uff09\uff0c\u5b83\u7ba1\u7406\u7684\u6307\u9488\u5c31\u4f1a\u88ab\u9500\u6bc1\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #include template < typename T > class Auto_ptr1 { T * m_ptr ; public : // \u901a\u8fc7\u6784\u9020\u51fd\u6570\u4f20\u5165\u4e00\u4e2a\u6307\u9488\uff0c\u4f7f\u8be5\u7c7b\u62e5\u6709\u5b83 Auto_ptr1 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } // \u6790\u6784\u51fd\u6570\u4f1a\u786e\u4fdd\u6307\u9488\u88ab\u5220\u9664 ~ Auto_ptr1 () { delete m_ptr ; } // \u91cd\u8f7d\u89e3\u5f15\u7528\u548c\u6210\u5458\u8fd0\u7b97\u7b26 T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } }; // A sample class to prove the above works class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { Auto_ptr1 < Resource > res ( new Resource ()); // \u6ce8\u610f\u6b64\u5904\u5206\u914d\u7684\u8d44\u6e90 // ... \u6ca1\u6709\u663e\u5f0f\u5220\u9664\u6307\u9488 // \u6ce8\u610f\uff0c\u5c16\u62ec\u53f7\u4e2d\u7684Resource\u4e0d\u9700\u8981\u661f\u53f7\uff0c\u661f\u53f7\u662f\u6a21\u677f\u63d0\u4f9b\u7684 return 0 ; } // res \u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u5e76\u4e14\u5220\u9664\u4e86\u8d44\u6e90 \u7a0b\u5e8f\u6253\u5370\uff1a 1 2 Resource acquired Resource destroyed \u8003\u8651\u4e00\u4e0b\u8fd9\u4e2a\u7a0b\u5e8f\u548c\u7c7b\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u9996\u5148\uff0c\u52a8\u6001\u521b\u5efa Resource \uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u6a21\u677f\u5316\u7684 Auto_ptr1 \u7c7b\u3002\u4ece\u8fd9\u4e00\u523b\u5f00\u59cb\uff0c Auto_ptr1 \u53d8\u91cf res \u62e5\u6709\u8be5\u8d44\u6e90\u5bf9\u8c61( Auto_ptr1 \u662f m_ptr \u662f \u7ec4\u5408\u5173\u7cfb )\u3002\u56e0\u4e3a res \u88ab\u58f0\u660e\u4e3a\u5c40\u90e8\u53d8\u91cf\u5e76\u5177\u6709\u8bed\u53e5\u5757\u4f5c\u7528\u57df\uff0c\u6240\u4ee5\u5f53\u8bed\u53e5\u5757\u7ed3\u675f\u65f6\uff0c\u5b83\u5c06\u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1(\u4e0d\u7528\u62c5\u5fc3\u5fd8\u8bb0\u91ca\u653e\u5b83)\u3002\u7531\u4e8e\u5b83\u662f\u4e00\u4e2a\u7c7b\uff0c\u56e0\u6b64\u5728\u9500\u6bc1\u5b83\u65f6\uff0c\u5c06\u8c03\u7528 Auto_ptr1 \u6790\u6784\u51fd\u6570\u3002\u8be5\u6790\u6784\u51fd\u6570\u5c06\u786e\u4fdd\u5b83\u6240\u6301\u6709\u7684 Resource \u6307\u9488\u88ab\u5220\u9664! \u53ea\u8981 Auto_ptr1 \u88ab\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf(\u5177\u6709\u81ea\u52a8\u6301\u7eed\u65f6\u95f4\uff0c\u56e0\u6b64\u7c7b\u540d\u4e2d\u6709\u201cAuto\u201d\u90e8\u5206)\uff0c\u65e0\u8bba\u51fd\u6570\u5982\u4f55\u7ec8\u6b62(\u5373\u4f7f\u5b83\u63d0\u524d\u7ec8\u6b62)\uff0c Resource \u603b\u662f\u53ef\u4ee5\u5728\u58f0\u660e\u5b83\u7684\u5757\u7684\u672b\u5c3e\u88ab\u9500\u6bc1\u3002 \u8fd9\u79cd\u7c7b\u88ab\u79f0\u4e3a\u667a\u80fd\u6307\u9488\u3002 \u667a\u80fd\u6307\u9488\u7c7b \u662f\u4e00\u79cd\u7ec4\u5408\u7c7b\uff0c\u5b83\u7528\u4e8e\u7ba1\u7406\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\uff0c\u5e76\u4e14\u80fd\u591f\u786e\u4fdd\u6307\u9488\u5bf9\u8c61\u5728\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\u88ab\u5220\u9664\uff08\u5185\u7f6e\u7684\u6307\u9488\u5bf9\u8c61\u88ab\u79f0\u4e3a\u201c\u7b28\u6307\u9488\u201d\uff0c\u6b63\u662f\u56e0\u4e3a\u5b83\u4eec\u4e0d\u80fd\u81ea\u5df1\u6e05\u7406\u81ea\u5df1\u6240\u7ba1\u7406\u7684\u5185\u5b58\uff09\u3002 \u73b0\u5728\u8ba9\u6211\u4eec\u56de\u5230\u4e0a\u9762\u7684 someFunction() \u4f8b\u5b50\uff0c\u770b\u770b\u5982\u4f55\u4f7f\u7528\u667a\u80fd\u6307\u9488\u89e3\u51b3\u6211\u4eec\u9762\u4e34\u7684\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 #include template < typename T > class Auto_ptr1 { T * m_ptr ; public : // Pass in a pointer to \"own\" via the constructor Auto_ptr1 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } // The destructor will make sure it gets deallocated ~ Auto_ptr1 () { delete m_ptr ; } // Overload dereference and operator-> so we can use Auto_ptr1 like m_ptr. T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } }; // A sample class to prove the above works class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } void sayHi () { std :: cout << \"Hi! \\n \" ; } }; void someFunction () { Auto_ptr1 < Resource > ptr ( new Resource ()); // ptr \u73b0\u5728\u62e5\u6709 Resource int x ; std :: cout << \"Enter an integer: \" ; std :: cin >> x ; if ( x == 0 ) return ; // \u51fd\u6570\u63d0\u524d\u8fd4\u56de // do stuff with ptr here ptr -> sayHi (); } int main () { someFunction (); return 0 ; } \u5982\u679c\u7528\u6237\u8f93\u5165\u975e0\uff0c\u5219\u7a0b\u5e8f\u6253\u5370\uff1a 1 2 3 Resource acquired Hi! Resource destroyed \u5982\u679c\u7528\u6237\u8f93\u51650\uff0c\u5219\u7a0b\u5e8f\u4f1a\u63d0\u524d\u9000\u51fa\uff0c\u6253\u5370\uff1a 1 2 Resource acquired Resource destroyed \u6ce8\u610f\uff0c\u5373\u4f7f\u5728\u7528\u6237\u8f93\u5165\u96f6\u4e14\u51fd\u6570\u63d0\u524d\u7ec8\u6b62\u7684\u60c5\u51b5\u4e0b\uff0c\u8d44\u6e90\u4ecd\u7136\u88ab\u6b63\u786e\u5730\u91ca\u653e\u3002 \u56e0\u4e3a ptr \u53d8\u91cf\u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u6240\u4ee5 ptr \u5c06\u5728\u51fd\u6570\u7ec8\u6b62\u65f6\u88ab\u9500\u6bc1(\u4e0d\u7ba1\u5b83\u4ee5\u4f55\u79cd\u65b9\u5f0f\u7ec8\u6b62)\u3002\u56e0\u4e3a Auto_ptr1 \u6790\u6784\u51fd\u6570\u5c06\u6e05\u7406 Resource \uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4fdd\u8bc1 Resource \u5c06\u88ab\u6b63\u786e\u5730\u6e05\u7406\u3002 \u4e25\u91cd\u7f3a\u9677 Auto_ptr1 \u7c7b\u6709\u4e00\u4e2a\u4e25\u91cd\u7684\u7f3a\u9677\uff0c\u662f\u7531\u81ea\u52a8\u751f\u6210\u7684\u4ee3\u7801\u5f15\u8d77\u7684\u3002\u4f60\u80fd\u770b\u51fa\u6765\u5417\uff1f (\u63d0\u793a\uff1a\u5982\u679c\u7f16\u8bd1\u5668\u4f1a\u4e3a\u7c7b\u63d0\u4f9b\u54ea\u4e9b\u9ed8\u8ba4\u7684\u51fd\u6570\uff1f) \u65f6\u95f4\u5230\uff01 \u731c\u5230\u4e86\u5417\uff1f\u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include // Same as above template < typename T > class Auto_ptr1 { T * m_ptr ; public : Auto_ptr1 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } ~ Auto_ptr1 () { delete m_ptr ; } T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } }; class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { Auto_ptr1 < Resource > res1 ( new Resource ()); Auto_ptr1 < Resource > res2 ( res1 ); // \u6ca1\u6709\u521d\u59cb\u5316res2\uff0c\u800c\u662f\u8fdb\u884c\u8d4b\u503c return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 Resource acquired Resource destroyed Resource destroyed \u7a0b\u5e8f\u5f88\u53ef\u80fd(\u4f46\u4e0d\u4e00\u5b9a)\u5728\u6b64\u65f6\u5d29\u6e83\u3002\u53d1\u73b0\u95ee\u9898\u7684\u6240\u5728\u4e86\u5417\uff1f\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u63d0\u4f9b\u590d\u5236\u6784\u9020\u51fd\u6570\u6216\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u6240\u4ee5C++\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u4e2a\u3002\u5b83\u63d0\u4f9b\u7684\u51fd\u6570\u53ea\u505a\u6d45\u62f7\u8d1d\u3002\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u7528 res1 \u521d\u59cb\u5316 res2 \u65f6\uff0c\u4e24\u4e2a Auto_ptr1 \u53d8\u91cf\u90fd\u6307\u5411\u540c\u4e00\u4e2a Resource \u3002\u5f53 res2 \u8d85\u51fa\u4f5c\u7528\u57df\u65f6\uff0c\u5b83\u5220\u9664\u8d44\u6e90\uff0c\u7ed9 res1 \u7559\u4e0b\u4e00\u4e2a \u60ac\u5782 \u6307\u9488\u3002\u5f53 res1 \u53bb\u5220\u9664\u5b83\u7684(\u5df2\u7ecf\u5220\u9664\u7684)\u8d44\u6e90\u65f6\uff0c\u7a0b\u5e8f\u5d29\u6e83! \u4e0b\u9762\u7684\u4ee3\u7801\u4e5f\u4f1a\u51fa\u73b0\u7c7b\u4f3c\u7684\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 void passByValue ( Auto_ptr1 < Resource > res ) { } int main () { Auto_ptr1 < Resource > res1 ( new Resource ()); passByValue ( res1 ); return 0 ; } \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c res1 \u5c06\u6309\u503c\u590d\u5236\u5230 passByValue \u7684\u53c2\u6570 res \u4e2d\uff0c\u5bfc\u81f4 Resource \u6307\u9488\u88ab\u590d\u5236\uff0c\u8fdb\u800c\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002 \u663e\u7136\u8fd9\u4e0d\u662f\u4ec0\u4e48\u597d\u4e8b\u3002\u6211\u4eec\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898? \u4e00\u79cd\u65b9\u6cd5\u662f\u663e\u5f0f\u5730\u5b9a\u4e49\u548c\u5220\u9664\u590d\u5236\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u4ece\u800c\u4ece\u6839\u672c\u4e0a\u963b\u6b62\u62f7\u8d1d\u3002\u8fd9\u4f1a\u963b\u6b62\u6309\u503c\u4f20\u9012\u7684\u60c5\u51b5(\u8fd9\u5f88\u597d\uff0c\u56e0\u4e3a\u6211\u4eec\u672c\u6765\u5c31\u4e0d\u5e94\u8be5\u6309\u503c\u4f20\u9012\u5b83\u4eec)\u3002 \u4f46\u662f\uff0c\u6211\u4eec\u5982\u4f55\u4ece\u51fd\u6570\u8fd4\u56de Auto_ptr1 \u7ed9\u8c03\u7528\u8005\u5462? 1 2 3 4 5 ??? generateResource () { Resource * r { new Resource () }; return Auto_ptr1 ( r ); } \u6211\u4eec\u65e0\u6cd5\u5c06 Auto_ptr1 \u6309\u5f15\u7528\u8fd4\u56de \uff0c\u56e0\u4e3a\u5c40\u90e8\u53d8\u91cf Auto_ptr1 \u4f1a\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u5220\u9664\uff0c\u6240\u4ee5\u4e3b\u8c03\u51fd\u6570\u5f97\u5230\u7684\u662f\u4e00\u4e2a\u60ac\u5782\u5f15\u7528\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u6307\u9488r\u8fd4\u56de\u4e3a Resource* \uff0c\u4f46\u662f\u6211\u4eec\u53ef\u80fd\u4f1a\u5728\u540e\u9762\u5fd8\u8bb0\u5220\u9664 r \uff0c\u8fd9\u5c31\u8fdd\u80cc\u4e86\u4f7f\u7528\u667a\u80fd\u6307\u9488\u7684\u521d\u8877\u3002\u6240\u4ee5\uff0c\u6309\u503c\u8fd4\u56de Auto_ptr1 \u662f\u552f\u4e00\u6709\u610f\u4e49\u7684\u9009\u9879\u2014\u2014\u4f46\u6211\u4eec\u6700\u7ec8\u4f1a\u5f97\u5230\u6d45\u62f7\u8d1d\u3001\u91cd\u590d\u7684\u6307\u9488\u5e76\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002 \u53e6\u4e00\u79cd\u9009\u62e9\u662f\u91cd\u5199 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\u6765\u8fdb\u884c \u6df1\u62f7\u8d1d \u3002\u8fd9\u6837\uff0c\u6211\u4eec\u81f3\u5c11\u53ef\u4ee5\u907f\u514d\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684\u91cd\u590d\u6307\u9488\u3002\u4f46\u662f\u62f7\u8d1d\u7684\u5f00\u9500\u53ef\u80fd\u4f1a\u5f88\u5927(\u800c\u4e14\u53ef\u80fd\u4e0d\u9700\u8981\u751a\u81f3\u4e0d\u53ef\u80fd)\uff0c\u800c\u4e14\u6211\u4eec\u4e0d\u5e0c\u671b\u4ec5\u4ec5\u4e3a\u4e86\u4ece\u51fd\u6570\u8fd4\u56de Auto_ptr1 \u800c\u5bf9\u5bf9\u8c61\u8fdb\u884c\u4e0d\u5fc5\u8981\u7684\u590d\u5236\u3002\u53e6\u5916\uff0c\u5bf9\u666e\u901a\u6307\u9488\u8d4b\u503c\u6216\u521d\u59cb\u5316\u65f6\u5e76\u4e0d\u4f1a\u62f7\u8d1d\u5bf9\u8c61\uff0c\u4e3a\u4ec0\u4e48\u667a\u80fd\u6307\u9488\u8981\u5177\u6709\u4e0d\u540c\u7684\u903b\u8f91\u5462\uff1f \u8be5\u600e\u4e48\u505a\u5462\uff1f \u79fb\u52a8\u8bed\u4e49 \u5982\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba9\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u8fd0\u7b97\u7b26\u4e0d\u53bb\u62f7\u8d1d\u6307\u9488\uff08\u62f7\u8d1d\u8bed\u4e49\uff09\uff0c\u800c\u662f\u5c06\u5b83\u6240\u7ba1\u7406\u7684\u8d44\u6e90\u4f20\u9012\u6216\u79fb\u52a8\u7ed9\u76ee\u6807\u6307\u9488\u5462\uff1f\u8fd9\u6b63\u662f \u79fb\u52a8\u8bed\u4e49 \u80cc\u540e\u7684\u6838\u5fc3\u601d\u60f3\u3002\u79fb\u52a8\u8bed\u4e49\u8bf4\u7684\u5c31\u662f\u5bf9\u8c61\u6240\u6709\u6743\u53d1\u751f\u4e86\u8f6c\u79fb\u800c\u4e0d\u662f\u62f7\u8d1d\u3002 \u66f4\u65b0\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 #include template < typename T > class Auto_ptr2 { T * m_ptr ; public : Auto_ptr2 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } ~ Auto_ptr2 () { delete m_ptr ; } // \u4f7f\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49 Auto_ptr2 ( Auto_ptr2 & a ) // \u6ce8\u610f: \u4e0d\u80fd\u662f const { m_ptr = a . m_ptr ; // \u76ee\u7684\u5bf9\u8c61\u83b7\u5f97\u6e90\u5bf9\u8c61\u7684\u6307\u9488 a . m_ptr = nullptr ; // \u786e\u4fdd\u6e90\u5bf9\u8c61\u4e0d\u518d\u62e5\u6709\u8be5\u6307\u9488 } // \u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49 Auto_ptr2 & operator = ( Auto_ptr2 & a ) // \u6ce8\u610f: \u4e0d\u80fd\u662f const { if ( & a == this ) return * this ; delete m_ptr ; // \u786e\u4fdd\u76ee\u7684\u5bf9\u8c61\u7684\u6307\u9488\u6240\u6307\u7684\u8d44\u6e90\u88ab\u91ca\u653e m_ptr = a . m_ptr ; // \u76ee\u7684\u5bf9\u8c61\u83b7\u5f97\u6e90\u5bf9\u8c61\u7684\u6307\u9488 a . m_ptr = nullptr ; // \u786e\u4fdd\u6e90\u5bf9\u8c61\u4e0d\u518d\u62e5\u6709\u8be5\u6307\u9488 return * this ; } T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } bool isNull () const { return m_ptr == nullptr ; } }; class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { Auto_ptr2 < Resource > res1 ( new Resource ()); Auto_ptr2 < Resource > res2 ; // Start as nullptr std :: cout << \"res1 is \" << ( res1 . isNull () ? \"null \\n \" : \"not null \\n \" ); std :: cout << \"res2 is \" << ( res2 . isNull () ? \"null \\n \" : \"not null \\n \" ); res2 = res1 ; // res2 assumes ownership, res1 is set to null std :: cout << \"Ownership transferred \\n \" ; std :: cout << \"res1 is \" << ( res1 . isNull () ? \"null \\n \" : \"not null \\n \" ); std :: cout << \"res2 is \" << ( res2 . isNull () ? \"null \\n \" : \"not null \\n \" ); return 0 ; } \u6253\u5370\uff1a 1 2 3 4 5 6 7 Resource acquired res1 is not null res2 is null Ownership transferred res1 is null res2 is not null Resource destroyed \u6ce8\u610f\uff0c\u6211\u4eec\u91cd\u8f7d\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u73b0\u5728\u53ef\u4ee5\u628a m_ptr \u7684\u6240\u6709\u6743\u4ece res1 \u4f20\u9012\u7ed9 res2 \uff01\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c06\u4e0d\u4f1a\u5f97\u5230\u91cd\u590d\u7684\u6307\u9488\uff0c\u8d44\u6e90\u6e05\u7406\u53ef\u4ee5\u987a\u5229\u8fdb\u884c\u3002 std::auto_ptr \u4e3a\u4ec0\u4e48\u662f\u7cdf\u7cd5\u7684\u5b9e\u73b0 \u73b0\u5728\u662f\u8ba8\u8bba std::auto_ptr \u7684\u5408\u9002\u65f6\u673a\u4e86\u3002 std::auto_ptr \u5728C++ 98\u4e2d\u5f15\u5165\uff0c\u5728C++ 17\u4e2d\u88ab\u5220\u9664\uff0c\u5b83\u662fC++\u7b2c\u4e00\u6b21\u5c1d\u8bd5\u6807\u51c6\u5316\u667a\u80fd\u6307\u9488\u3002 auto_ptr \u9009\u62e9\u4e86 Auto_ptr2 \u7c7b\u4e00\u6837\u7684\u79fb\u52a8\u8bed\u4e49\u5b9e\u73b0\u65b9\u5f0f\u3002 \u7136\u800c\uff0c std::auto_ptr (\u4ee5\u53ca\u6211\u4eec\u7684 Auto_ptr2 \u7c7b)\u6709\u8bb8\u591a\u95ee\u9898\uff0c\u4f7f\u5f97\u4f7f\u7528\u5b83\u5f88\u5371\u9669\u3002 \u9996\u5148\uff0c\u7531\u4e8e std::auto_ptr \u901a\u8fc7\u590d\u5236\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\u5b9e\u73b0\u4e86\u79fb\u52a8\u8bed\u4e49\uff0c\u5c06 std::auto_ptr \u6309\u503c\u4f20\u9012\u7ed9\u51fd\u6570\u5c06\u5bfc\u81f4\u8d44\u6e90\u88ab\u79fb\u52a8\u5230\u51fd\u6570\u5f62\u53c2\u4e2d(\u5e76\u5728\u51fd\u6570\u5f62\u53c2\u8d85\u51fa\u4f5c\u7528\u57df\u65f6\u9500\u6bc1)\u3002\u7136\u540e\uff0c\u5f53\u4f60\u4ece\u8c03\u7528\u8005\u8bbf\u95ee auto_ptr \u5b9e\u53c2\u65f6(\u6ca1\u6709\u610f\u8bc6\u5230\u5b83\u5df2\u7ecf\u88ab\u79fb\u52a8\u548c\u5220\u9664\u4e86)\u65f6\uff0c\u8fdb\u884c \u89e3\u5f15\u7528 \u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002 \u5176\u6b21\uff0c std::auto_ptr \u603b\u662f\u4f7f\u7528\u975e\u6570\u7ec4\u5220\u9664\u6765\u5220\u9664\u5176\u5185\u5bb9\u3002\u8fd9\u610f\u5473\u7740 auto_ptr \u4e0d\u80fd\u6b63\u786e\u5730\u5904\u7406\u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4\uff0c\u56e0\u4e3a\u5b83\u4f7f\u7528\u4e86\u9519\u8bef\u7684\u5185\u5b58\u91ca\u653e\u65b9\u5f0f\u3002\u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u5b83\u4e0d\u80fd\u963b\u6b62\u60a8\u5411\u5b83\u4f20\u9012\u52a8\u6001\u6570\u7ec4\uff0c\u8fd9\u6837\u5b83\u5c31\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002 \u6700\u540e\uff0c auto_ptr \u4e0d\u80fd\u5f88\u597d\u5730\u5904\u7406\u6807\u51c6\u5e93\u4e2d\u7684\u8bb8\u591a\u5176\u4ed6\u7c7b\uff0c\u5305\u62ec\u5927\u591a\u6570\u5bb9\u5668\u548c\u7b97\u6cd5\u3002\u8fd9\u662f\u56e0\u4e3a\u90a3\u4e9b\u6807\u51c6\u5e93\u7c7b\u5047\u5b9a\u5f53\u5b83\u4eec\u590d\u5236\u4e00\u4e2a\u8d44\u6e90\u65f6\uff0c\u5b83\u8fdb\u884c\u4e86\u5b9e\u9645\u7684\u590d\u5236\uff0c\u800c\u4e0d\u662f\u79fb\u52a8\u3002 \u7531\u4e8e\u4e0a\u8ff0\u7f3a\u70b9\uff0c std::auto_ptr \u5728C++ 11\u4e2d\u5df2\u5f03\u7528\uff0c\u5728C++ 17\u4e2d\u5df2\u5220\u9664\u3002 \u7ee7\u7eed std::auto_ptr \u7684\u6838\u5fc3\u95ee\u9898\u662f\uff1aC++ 11\u4e4b\u524d\uff0cC++\u8bed\u8a00\u6839\u672c\u6ca1\u6709\u533a\u5206\u201c\u62f7\u8d1d\u8bed\u4e49\u201d\u548c\u201c\u79fb\u52a8\u8bed\u4e49\u201d\u7684\u673a\u5236\u3002\u5bf9\u62f7\u8d1d\u8bed\u4e49\u91cd\u5199\u6765\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49\uff0c\u4f1a\u5bfc\u81f4\u5947\u602a\u7684\u8fb9\u754c\u60c5\u51b5\u548c\u4e0d\u6613\u5bdf\u89c9\u7684\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5199 res1 = res2 \uff0c\u4f46\u65e0\u6cd5\u77e5\u9053 res2 \u662f\u5426\u4f1a\u88ab\u6539\u53d8\uff01 \u6b63\u56e0\u4e3a\u5982\u6b64\uff0c\u5728C++ 11\u4e2d\uff0c\u6b63\u5f0f\u5b9a\u4e49\u4e86\u201c\u79fb\u52a8\u201d\u7684\u6982\u5ff5\uff0c\u5e76\u5728\u8bed\u8a00\u4e2d\u6dfb\u52a0\u4e86\u201c\u79fb\u52a8\u8bed\u4e49\u201d\uff0c\u4ee5\u6b63\u786e\u533a\u5206\u590d\u5236\u548c\u79fb\u52a8\u3002\u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u79fb\u52a8\u8bed\u4e49\u662f\u5f88\u6709\u7528\u7684\uff0c\u5728\u672c\u7ae0\u7684\u5176\u4f59\u90e8\u5206\u6211\u4eec\u4f1a\u7ee7\u7eed\u63a2\u7d22\u79fb\u52a8\u8bed\u4e49\u7684\u5176\u4ed6\u8bdd\u9898\u3002\u6211\u4eec\u8fd8\u5c06\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\u4fee\u590d Auto_ptr2 \u7c7b\u3002 \u5728C++ 11\u4e2d\uff0c std::auto_ptr \u5df2\u7ecf\u88ab\u4e00\u5806\u5176\u4ed6\u7c7b\u578b\u7684\u201c\u79fb\u52a8\u8bed\u4e49\u517c\u5bb9\u7684\u201d\u667a\u80fd\u6307\u9488\u6240\u53d6\u4ee3\uff1a std::unique_ptr , std::weak_ptr \u548c std::shared_ptr \u3002\u6211\u4eec\u8fd8\u5c06\u63a2\u8ba8\u5176\u4e2d\u6700\u6d41\u884c\u7684\u4e24\u4e2a\uff1a unique_ptr (\u5b83\u76f4\u63a5\u66ff\u6362\u4e86 auto_ptr )\u548c shared_ptr \u3002","title":"M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb"},{"location":"M-1-introduction-to-smart-pointers-and-move-semantics/#_1","text":"\u5bf9\u4e8e\u7c7b\u5bf9\u8c61\u6765\u8bf4\uff0c\u5b83\u4eec\u6700\u5927\u7684\u4f18\u52bf\u662f\u53ef\u4ee5\u5728 \u79bb\u5f00\u4f5c\u7528\u57df \u662f\u81ea\u52a8\u6267\u884c \u6790\u6784\u51fd\u6570 \u3002\u8fd9\u6837\u4e00\u6765\uff0c\u5982\u679c\u6211\u4eec\u5728\u6784\u9020\u51fd\u6570\u4e2d\u5206\u914d\u6216\u83b7\u53d6\u5185\u5b58\uff0c\u5219\u53ef\u4ee5\u5728\u6790\u6784\u51fd\u6570\u4e2d\u8fdb\u884c\u91ca\u653e\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u786e\u4fdd\u5185\u5b58\u5728\u5bf9\u8c61\u88ab\u9500\u6bc1\u524d\uff08\u4e0d\u8bba\u662f\u79bb\u5f00\u4f5c\u7528\u57df\u8fd8\u662f\u663e\u5f0f\u5220\u9664\u7b49\uff09\u91ca\u653e\u3002\u8fd9\u4e5f\u6b63\u662f \u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09 \u7f16\u7a0b\u8303\u5f0f\u7684\u6838\u5fc3\u601d\u60f3\uff08\u89c1 13.9 - \u6790\u6784\u51fd\u6570 \uff09\u3002 \u90a3\u4e48\uff0c\u662f\u5426\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7c7b\uff0c\u5e2e\u52a9\u6211\u4eec\u7ba1\u7406\u548c\u6e05\u7406\u6307\u9488\u5462\uff1f\u5f53\u7136\u53ef\u4ee5\uff01 \u8003\u8651\u8bbe\u8ba1\u8fd9\u6837\u4e00\u4e2a\u7c7b\uff0c\u5b83\u7684\u552f\u4e00\u5de5\u4f5c\uff0c\u5c31\u662f\u7ba1\u7406\u4e00\u4e2a\u4f20\u5165\u7684\u6307\u9488\u3002\u5f53\u8be5\u7c7b\u5bf9\u8c61\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c\u91ca\u653e\u6307\u9488\u6240\u6307\u5411\u7684\u5185\u5b58\u3002\u53ea\u8981\u8be5\u5bf9\u8c61\u88ab\u58f0\u660e\u4e3a\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4fdd\u8bc1\uff0c\u5f53\u5b83\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff08\u4e0d\u8bba\u4f55\u65f6\u3001\u4e5f\u4e0d\u8bba\u51fd\u6570\u5982\u4f55\u7ec8\u6b62\uff09\uff0c\u5b83\u7ba1\u7406\u7684\u6307\u9488\u5c31\u4f1a\u88ab\u9500\u6bc1\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #include template < typename T > class Auto_ptr1 { T * m_ptr ; public : // \u901a\u8fc7\u6784\u9020\u51fd\u6570\u4f20\u5165\u4e00\u4e2a\u6307\u9488\uff0c\u4f7f\u8be5\u7c7b\u62e5\u6709\u5b83 Auto_ptr1 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } // \u6790\u6784\u51fd\u6570\u4f1a\u786e\u4fdd\u6307\u9488\u88ab\u5220\u9664 ~ Auto_ptr1 () { delete m_ptr ; } // \u91cd\u8f7d\u89e3\u5f15\u7528\u548c\u6210\u5458\u8fd0\u7b97\u7b26 T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } }; // A sample class to prove the above works class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { Auto_ptr1 < Resource > res ( new Resource ()); // \u6ce8\u610f\u6b64\u5904\u5206\u914d\u7684\u8d44\u6e90 // ... \u6ca1\u6709\u663e\u5f0f\u5220\u9664\u6307\u9488 // \u6ce8\u610f\uff0c\u5c16\u62ec\u53f7\u4e2d\u7684Resource\u4e0d\u9700\u8981\u661f\u53f7\uff0c\u661f\u53f7\u662f\u6a21\u677f\u63d0\u4f9b\u7684 return 0 ; } // res \u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u5e76\u4e14\u5220\u9664\u4e86\u8d44\u6e90 \u7a0b\u5e8f\u6253\u5370\uff1a 1 2 Resource acquired Resource destroyed \u8003\u8651\u4e00\u4e0b\u8fd9\u4e2a\u7a0b\u5e8f\u548c\u7c7b\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u9996\u5148\uff0c\u52a8\u6001\u521b\u5efa Resource \uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u6a21\u677f\u5316\u7684 Auto_ptr1 \u7c7b\u3002\u4ece\u8fd9\u4e00\u523b\u5f00\u59cb\uff0c Auto_ptr1 \u53d8\u91cf res \u62e5\u6709\u8be5\u8d44\u6e90\u5bf9\u8c61( Auto_ptr1 \u662f m_ptr \u662f \u7ec4\u5408\u5173\u7cfb )\u3002\u56e0\u4e3a res \u88ab\u58f0\u660e\u4e3a\u5c40\u90e8\u53d8\u91cf\u5e76\u5177\u6709\u8bed\u53e5\u5757\u4f5c\u7528\u57df\uff0c\u6240\u4ee5\u5f53\u8bed\u53e5\u5757\u7ed3\u675f\u65f6\uff0c\u5b83\u5c06\u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1(\u4e0d\u7528\u62c5\u5fc3\u5fd8\u8bb0\u91ca\u653e\u5b83)\u3002\u7531\u4e8e\u5b83\u662f\u4e00\u4e2a\u7c7b\uff0c\u56e0\u6b64\u5728\u9500\u6bc1\u5b83\u65f6\uff0c\u5c06\u8c03\u7528 Auto_ptr1 \u6790\u6784\u51fd\u6570\u3002\u8be5\u6790\u6784\u51fd\u6570\u5c06\u786e\u4fdd\u5b83\u6240\u6301\u6709\u7684 Resource \u6307\u9488\u88ab\u5220\u9664! \u53ea\u8981 Auto_ptr1 \u88ab\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf(\u5177\u6709\u81ea\u52a8\u6301\u7eed\u65f6\u95f4\uff0c\u56e0\u6b64\u7c7b\u540d\u4e2d\u6709\u201cAuto\u201d\u90e8\u5206)\uff0c\u65e0\u8bba\u51fd\u6570\u5982\u4f55\u7ec8\u6b62(\u5373\u4f7f\u5b83\u63d0\u524d\u7ec8\u6b62)\uff0c Resource \u603b\u662f\u53ef\u4ee5\u5728\u58f0\u660e\u5b83\u7684\u5757\u7684\u672b\u5c3e\u88ab\u9500\u6bc1\u3002 \u8fd9\u79cd\u7c7b\u88ab\u79f0\u4e3a\u667a\u80fd\u6307\u9488\u3002 \u667a\u80fd\u6307\u9488\u7c7b \u662f\u4e00\u79cd\u7ec4\u5408\u7c7b\uff0c\u5b83\u7528\u4e8e\u7ba1\u7406\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\uff0c\u5e76\u4e14\u80fd\u591f\u786e\u4fdd\u6307\u9488\u5bf9\u8c61\u5728\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\u88ab\u5220\u9664\uff08\u5185\u7f6e\u7684\u6307\u9488\u5bf9\u8c61\u88ab\u79f0\u4e3a\u201c\u7b28\u6307\u9488\u201d\uff0c\u6b63\u662f\u56e0\u4e3a\u5b83\u4eec\u4e0d\u80fd\u81ea\u5df1\u6e05\u7406\u81ea\u5df1\u6240\u7ba1\u7406\u7684\u5185\u5b58\uff09\u3002 \u73b0\u5728\u8ba9\u6211\u4eec\u56de\u5230\u4e0a\u9762\u7684 someFunction() \u4f8b\u5b50\uff0c\u770b\u770b\u5982\u4f55\u4f7f\u7528\u667a\u80fd\u6307\u9488\u89e3\u51b3\u6211\u4eec\u9762\u4e34\u7684\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 #include template < typename T > class Auto_ptr1 { T * m_ptr ; public : // Pass in a pointer to \"own\" via the constructor Auto_ptr1 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } // The destructor will make sure it gets deallocated ~ Auto_ptr1 () { delete m_ptr ; } // Overload dereference and operator-> so we can use Auto_ptr1 like m_ptr. T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } }; // A sample class to prove the above works class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } void sayHi () { std :: cout << \"Hi! \\n \" ; } }; void someFunction () { Auto_ptr1 < Resource > ptr ( new Resource ()); // ptr \u73b0\u5728\u62e5\u6709 Resource int x ; std :: cout << \"Enter an integer: \" ; std :: cin >> x ; if ( x == 0 ) return ; // \u51fd\u6570\u63d0\u524d\u8fd4\u56de // do stuff with ptr here ptr -> sayHi (); } int main () { someFunction (); return 0 ; } \u5982\u679c\u7528\u6237\u8f93\u5165\u975e0\uff0c\u5219\u7a0b\u5e8f\u6253\u5370\uff1a 1 2 3 Resource acquired Hi! Resource destroyed \u5982\u679c\u7528\u6237\u8f93\u51650\uff0c\u5219\u7a0b\u5e8f\u4f1a\u63d0\u524d\u9000\u51fa\uff0c\u6253\u5370\uff1a 1 2 Resource acquired Resource destroyed \u6ce8\u610f\uff0c\u5373\u4f7f\u5728\u7528\u6237\u8f93\u5165\u96f6\u4e14\u51fd\u6570\u63d0\u524d\u7ec8\u6b62\u7684\u60c5\u51b5\u4e0b\uff0c\u8d44\u6e90\u4ecd\u7136\u88ab\u6b63\u786e\u5730\u91ca\u653e\u3002 \u56e0\u4e3a ptr \u53d8\u91cf\u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u6240\u4ee5 ptr \u5c06\u5728\u51fd\u6570\u7ec8\u6b62\u65f6\u88ab\u9500\u6bc1(\u4e0d\u7ba1\u5b83\u4ee5\u4f55\u79cd\u65b9\u5f0f\u7ec8\u6b62)\u3002\u56e0\u4e3a Auto_ptr1 \u6790\u6784\u51fd\u6570\u5c06\u6e05\u7406 Resource \uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4fdd\u8bc1 Resource \u5c06\u88ab\u6b63\u786e\u5730\u6e05\u7406\u3002","title":"\u667a\u80fd\u6307\u9488\u7c7b\u53ef\u4ee5\u62ef\u6551\u6211\u4eec\u5417\uff1f"},{"location":"M-1-introduction-to-smart-pointers-and-move-semantics/#_2","text":"Auto_ptr1 \u7c7b\u6709\u4e00\u4e2a\u4e25\u91cd\u7684\u7f3a\u9677\uff0c\u662f\u7531\u81ea\u52a8\u751f\u6210\u7684\u4ee3\u7801\u5f15\u8d77\u7684\u3002\u4f60\u80fd\u770b\u51fa\u6765\u5417\uff1f (\u63d0\u793a\uff1a\u5982\u679c\u7f16\u8bd1\u5668\u4f1a\u4e3a\u7c7b\u63d0\u4f9b\u54ea\u4e9b\u9ed8\u8ba4\u7684\u51fd\u6570\uff1f) \u65f6\u95f4\u5230\uff01 \u731c\u5230\u4e86\u5417\uff1f\u8003\u8651\u4e0b\u9762\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include // Same as above template < typename T > class Auto_ptr1 { T * m_ptr ; public : Auto_ptr1 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } ~ Auto_ptr1 () { delete m_ptr ; } T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } }; class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { Auto_ptr1 < Resource > res1 ( new Resource ()); Auto_ptr1 < Resource > res2 ( res1 ); // \u6ca1\u6709\u521d\u59cb\u5316res2\uff0c\u800c\u662f\u8fdb\u884c\u8d4b\u503c return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\uff1a 1 2 3 Resource acquired Resource destroyed Resource destroyed \u7a0b\u5e8f\u5f88\u53ef\u80fd(\u4f46\u4e0d\u4e00\u5b9a)\u5728\u6b64\u65f6\u5d29\u6e83\u3002\u53d1\u73b0\u95ee\u9898\u7684\u6240\u5728\u4e86\u5417\uff1f\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u63d0\u4f9b\u590d\u5236\u6784\u9020\u51fd\u6570\u6216\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u6240\u4ee5C++\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u4e2a\u3002\u5b83\u63d0\u4f9b\u7684\u51fd\u6570\u53ea\u505a\u6d45\u62f7\u8d1d\u3002\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u7528 res1 \u521d\u59cb\u5316 res2 \u65f6\uff0c\u4e24\u4e2a Auto_ptr1 \u53d8\u91cf\u90fd\u6307\u5411\u540c\u4e00\u4e2a Resource \u3002\u5f53 res2 \u8d85\u51fa\u4f5c\u7528\u57df\u65f6\uff0c\u5b83\u5220\u9664\u8d44\u6e90\uff0c\u7ed9 res1 \u7559\u4e0b\u4e00\u4e2a \u60ac\u5782 \u6307\u9488\u3002\u5f53 res1 \u53bb\u5220\u9664\u5b83\u7684(\u5df2\u7ecf\u5220\u9664\u7684)\u8d44\u6e90\u65f6\uff0c\u7a0b\u5e8f\u5d29\u6e83! \u4e0b\u9762\u7684\u4ee3\u7801\u4e5f\u4f1a\u51fa\u73b0\u7c7b\u4f3c\u7684\u95ee\u9898\uff1a 1 2 3 4 5 6 7 8 9 10 11 void passByValue ( Auto_ptr1 < Resource > res ) { } int main () { Auto_ptr1 < Resource > res1 ( new Resource ()); passByValue ( res1 ); return 0 ; } \u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c res1 \u5c06\u6309\u503c\u590d\u5236\u5230 passByValue \u7684\u53c2\u6570 res \u4e2d\uff0c\u5bfc\u81f4 Resource \u6307\u9488\u88ab\u590d\u5236\uff0c\u8fdb\u800c\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002 \u663e\u7136\u8fd9\u4e0d\u662f\u4ec0\u4e48\u597d\u4e8b\u3002\u6211\u4eec\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898? \u4e00\u79cd\u65b9\u6cd5\u662f\u663e\u5f0f\u5730\u5b9a\u4e49\u548c\u5220\u9664\u590d\u5236\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u4ece\u800c\u4ece\u6839\u672c\u4e0a\u963b\u6b62\u62f7\u8d1d\u3002\u8fd9\u4f1a\u963b\u6b62\u6309\u503c\u4f20\u9012\u7684\u60c5\u51b5(\u8fd9\u5f88\u597d\uff0c\u56e0\u4e3a\u6211\u4eec\u672c\u6765\u5c31\u4e0d\u5e94\u8be5\u6309\u503c\u4f20\u9012\u5b83\u4eec)\u3002 \u4f46\u662f\uff0c\u6211\u4eec\u5982\u4f55\u4ece\u51fd\u6570\u8fd4\u56de Auto_ptr1 \u7ed9\u8c03\u7528\u8005\u5462? 1 2 3 4 5 ??? generateResource () { Resource * r { new Resource () }; return Auto_ptr1 ( r ); } \u6211\u4eec\u65e0\u6cd5\u5c06 Auto_ptr1 \u6309\u5f15\u7528\u8fd4\u56de \uff0c\u56e0\u4e3a\u5c40\u90e8\u53d8\u91cf Auto_ptr1 \u4f1a\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u5220\u9664\uff0c\u6240\u4ee5\u4e3b\u8c03\u51fd\u6570\u5f97\u5230\u7684\u662f\u4e00\u4e2a\u60ac\u5782\u5f15\u7528\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u6307\u9488r\u8fd4\u56de\u4e3a Resource* \uff0c\u4f46\u662f\u6211\u4eec\u53ef\u80fd\u4f1a\u5728\u540e\u9762\u5fd8\u8bb0\u5220\u9664 r \uff0c\u8fd9\u5c31\u8fdd\u80cc\u4e86\u4f7f\u7528\u667a\u80fd\u6307\u9488\u7684\u521d\u8877\u3002\u6240\u4ee5\uff0c\u6309\u503c\u8fd4\u56de Auto_ptr1 \u662f\u552f\u4e00\u6709\u610f\u4e49\u7684\u9009\u9879\u2014\u2014\u4f46\u6211\u4eec\u6700\u7ec8\u4f1a\u5f97\u5230\u6d45\u62f7\u8d1d\u3001\u91cd\u590d\u7684\u6307\u9488\u5e76\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002 \u53e6\u4e00\u79cd\u9009\u62e9\u662f\u91cd\u5199 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\u6765\u8fdb\u884c \u6df1\u62f7\u8d1d \u3002\u8fd9\u6837\uff0c\u6211\u4eec\u81f3\u5c11\u53ef\u4ee5\u907f\u514d\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684\u91cd\u590d\u6307\u9488\u3002\u4f46\u662f\u62f7\u8d1d\u7684\u5f00\u9500\u53ef\u80fd\u4f1a\u5f88\u5927(\u800c\u4e14\u53ef\u80fd\u4e0d\u9700\u8981\u751a\u81f3\u4e0d\u53ef\u80fd)\uff0c\u800c\u4e14\u6211\u4eec\u4e0d\u5e0c\u671b\u4ec5\u4ec5\u4e3a\u4e86\u4ece\u51fd\u6570\u8fd4\u56de Auto_ptr1 \u800c\u5bf9\u5bf9\u8c61\u8fdb\u884c\u4e0d\u5fc5\u8981\u7684\u590d\u5236\u3002\u53e6\u5916\uff0c\u5bf9\u666e\u901a\u6307\u9488\u8d4b\u503c\u6216\u521d\u59cb\u5316\u65f6\u5e76\u4e0d\u4f1a\u62f7\u8d1d\u5bf9\u8c61\uff0c\u4e3a\u4ec0\u4e48\u667a\u80fd\u6307\u9488\u8981\u5177\u6709\u4e0d\u540c\u7684\u903b\u8f91\u5462\uff1f \u8be5\u600e\u4e48\u505a\u5462\uff1f","title":"\u4e25\u91cd\u7f3a\u9677"},{"location":"M-1-introduction-to-smart-pointers-and-move-semantics/#_3","text":"\u5982\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba9\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u8fd0\u7b97\u7b26\u4e0d\u53bb\u62f7\u8d1d\u6307\u9488\uff08\u62f7\u8d1d\u8bed\u4e49\uff09\uff0c\u800c\u662f\u5c06\u5b83\u6240\u7ba1\u7406\u7684\u8d44\u6e90\u4f20\u9012\u6216\u79fb\u52a8\u7ed9\u76ee\u6807\u6307\u9488\u5462\uff1f\u8fd9\u6b63\u662f \u79fb\u52a8\u8bed\u4e49 \u80cc\u540e\u7684\u6838\u5fc3\u601d\u60f3\u3002\u79fb\u52a8\u8bed\u4e49\u8bf4\u7684\u5c31\u662f\u5bf9\u8c61\u6240\u6709\u6743\u53d1\u751f\u4e86\u8f6c\u79fb\u800c\u4e0d\u662f\u62f7\u8d1d\u3002 \u66f4\u65b0\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 #include template < typename T > class Auto_ptr2 { T * m_ptr ; public : Auto_ptr2 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } ~ Auto_ptr2 () { delete m_ptr ; } // \u4f7f\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49 Auto_ptr2 ( Auto_ptr2 & a ) // \u6ce8\u610f: \u4e0d\u80fd\u662f const { m_ptr = a . m_ptr ; // \u76ee\u7684\u5bf9\u8c61\u83b7\u5f97\u6e90\u5bf9\u8c61\u7684\u6307\u9488 a . m_ptr = nullptr ; // \u786e\u4fdd\u6e90\u5bf9\u8c61\u4e0d\u518d\u62e5\u6709\u8be5\u6307\u9488 } // \u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49 Auto_ptr2 & operator = ( Auto_ptr2 & a ) // \u6ce8\u610f: \u4e0d\u80fd\u662f const { if ( & a == this ) return * this ; delete m_ptr ; // \u786e\u4fdd\u76ee\u7684\u5bf9\u8c61\u7684\u6307\u9488\u6240\u6307\u7684\u8d44\u6e90\u88ab\u91ca\u653e m_ptr = a . m_ptr ; // \u76ee\u7684\u5bf9\u8c61\u83b7\u5f97\u6e90\u5bf9\u8c61\u7684\u6307\u9488 a . m_ptr = nullptr ; // \u786e\u4fdd\u6e90\u5bf9\u8c61\u4e0d\u518d\u62e5\u6709\u8be5\u6307\u9488 return * this ; } T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } bool isNull () const { return m_ptr == nullptr ; } }; class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { Auto_ptr2 < Resource > res1 ( new Resource ()); Auto_ptr2 < Resource > res2 ; // Start as nullptr std :: cout << \"res1 is \" << ( res1 . isNull () ? \"null \\n \" : \"not null \\n \" ); std :: cout << \"res2 is \" << ( res2 . isNull () ? \"null \\n \" : \"not null \\n \" ); res2 = res1 ; // res2 assumes ownership, res1 is set to null std :: cout << \"Ownership transferred \\n \" ; std :: cout << \"res1 is \" << ( res1 . isNull () ? \"null \\n \" : \"not null \\n \" ); std :: cout << \"res2 is \" << ( res2 . isNull () ? \"null \\n \" : \"not null \\n \" ); return 0 ; } \u6253\u5370\uff1a 1 2 3 4 5 6 7 Resource acquired res1 is not null res2 is null Ownership transferred res1 is null res2 is not null Resource destroyed \u6ce8\u610f\uff0c\u6211\u4eec\u91cd\u8f7d\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u73b0\u5728\u53ef\u4ee5\u628a m_ptr \u7684\u6240\u6709\u6743\u4ece res1 \u4f20\u9012\u7ed9 res2 \uff01\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c06\u4e0d\u4f1a\u5f97\u5230\u91cd\u590d\u7684\u6307\u9488\uff0c\u8d44\u6e90\u6e05\u7406\u53ef\u4ee5\u987a\u5229\u8fdb\u884c\u3002","title":"\u79fb\u52a8\u8bed\u4e49"},{"location":"M-1-introduction-to-smart-pointers-and-move-semantics/#stdauto_ptr","text":"\u73b0\u5728\u662f\u8ba8\u8bba std::auto_ptr \u7684\u5408\u9002\u65f6\u673a\u4e86\u3002 std::auto_ptr \u5728C++ 98\u4e2d\u5f15\u5165\uff0c\u5728C++ 17\u4e2d\u88ab\u5220\u9664\uff0c\u5b83\u662fC++\u7b2c\u4e00\u6b21\u5c1d\u8bd5\u6807\u51c6\u5316\u667a\u80fd\u6307\u9488\u3002 auto_ptr \u9009\u62e9\u4e86 Auto_ptr2 \u7c7b\u4e00\u6837\u7684\u79fb\u52a8\u8bed\u4e49\u5b9e\u73b0\u65b9\u5f0f\u3002 \u7136\u800c\uff0c std::auto_ptr (\u4ee5\u53ca\u6211\u4eec\u7684 Auto_ptr2 \u7c7b)\u6709\u8bb8\u591a\u95ee\u9898\uff0c\u4f7f\u5f97\u4f7f\u7528\u5b83\u5f88\u5371\u9669\u3002 \u9996\u5148\uff0c\u7531\u4e8e std::auto_ptr \u901a\u8fc7\u590d\u5236\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u64cd\u4f5c\u7b26\u5b9e\u73b0\u4e86\u79fb\u52a8\u8bed\u4e49\uff0c\u5c06 std::auto_ptr \u6309\u503c\u4f20\u9012\u7ed9\u51fd\u6570\u5c06\u5bfc\u81f4\u8d44\u6e90\u88ab\u79fb\u52a8\u5230\u51fd\u6570\u5f62\u53c2\u4e2d(\u5e76\u5728\u51fd\u6570\u5f62\u53c2\u8d85\u51fa\u4f5c\u7528\u57df\u65f6\u9500\u6bc1)\u3002\u7136\u540e\uff0c\u5f53\u4f60\u4ece\u8c03\u7528\u8005\u8bbf\u95ee auto_ptr \u5b9e\u53c2\u65f6(\u6ca1\u6709\u610f\u8bc6\u5230\u5b83\u5df2\u7ecf\u88ab\u79fb\u52a8\u548c\u5220\u9664\u4e86)\u65f6\uff0c\u8fdb\u884c \u89e3\u5f15\u7528 \u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002 \u5176\u6b21\uff0c std::auto_ptr \u603b\u662f\u4f7f\u7528\u975e\u6570\u7ec4\u5220\u9664\u6765\u5220\u9664\u5176\u5185\u5bb9\u3002\u8fd9\u610f\u5473\u7740 auto_ptr \u4e0d\u80fd\u6b63\u786e\u5730\u5904\u7406\u52a8\u6001\u5206\u914d\u7684\u6570\u7ec4\uff0c\u56e0\u4e3a\u5b83\u4f7f\u7528\u4e86\u9519\u8bef\u7684\u5185\u5b58\u91ca\u653e\u65b9\u5f0f\u3002\u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u5b83\u4e0d\u80fd\u963b\u6b62\u60a8\u5411\u5b83\u4f20\u9012\u52a8\u6001\u6570\u7ec4\uff0c\u8fd9\u6837\u5b83\u5c31\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002 \u6700\u540e\uff0c auto_ptr \u4e0d\u80fd\u5f88\u597d\u5730\u5904\u7406\u6807\u51c6\u5e93\u4e2d\u7684\u8bb8\u591a\u5176\u4ed6\u7c7b\uff0c\u5305\u62ec\u5927\u591a\u6570\u5bb9\u5668\u548c\u7b97\u6cd5\u3002\u8fd9\u662f\u56e0\u4e3a\u90a3\u4e9b\u6807\u51c6\u5e93\u7c7b\u5047\u5b9a\u5f53\u5b83\u4eec\u590d\u5236\u4e00\u4e2a\u8d44\u6e90\u65f6\uff0c\u5b83\u8fdb\u884c\u4e86\u5b9e\u9645\u7684\u590d\u5236\uff0c\u800c\u4e0d\u662f\u79fb\u52a8\u3002 \u7531\u4e8e\u4e0a\u8ff0\u7f3a\u70b9\uff0c std::auto_ptr \u5728C++ 11\u4e2d\u5df2\u5f03\u7528\uff0c\u5728C++ 17\u4e2d\u5df2\u5220\u9664\u3002","title":"std::auto_ptr \u4e3a\u4ec0\u4e48\u662f\u7cdf\u7cd5\u7684\u5b9e\u73b0"},{"location":"M-1-introduction-to-smart-pointers-and-move-semantics/#_4","text":"std::auto_ptr \u7684\u6838\u5fc3\u95ee\u9898\u662f\uff1aC++ 11\u4e4b\u524d\uff0cC++\u8bed\u8a00\u6839\u672c\u6ca1\u6709\u533a\u5206\u201c\u62f7\u8d1d\u8bed\u4e49\u201d\u548c\u201c\u79fb\u52a8\u8bed\u4e49\u201d\u7684\u673a\u5236\u3002\u5bf9\u62f7\u8d1d\u8bed\u4e49\u91cd\u5199\u6765\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49\uff0c\u4f1a\u5bfc\u81f4\u5947\u602a\u7684\u8fb9\u754c\u60c5\u51b5\u548c\u4e0d\u6613\u5bdf\u89c9\u7684\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5199 res1 = res2 \uff0c\u4f46\u65e0\u6cd5\u77e5\u9053 res2 \u662f\u5426\u4f1a\u88ab\u6539\u53d8\uff01 \u6b63\u56e0\u4e3a\u5982\u6b64\uff0c\u5728C++ 11\u4e2d\uff0c\u6b63\u5f0f\u5b9a\u4e49\u4e86\u201c\u79fb\u52a8\u201d\u7684\u6982\u5ff5\uff0c\u5e76\u5728\u8bed\u8a00\u4e2d\u6dfb\u52a0\u4e86\u201c\u79fb\u52a8\u8bed\u4e49\u201d\uff0c\u4ee5\u6b63\u786e\u533a\u5206\u590d\u5236\u548c\u79fb\u52a8\u3002\u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u79fb\u52a8\u8bed\u4e49\u662f\u5f88\u6709\u7528\u7684\uff0c\u5728\u672c\u7ae0\u7684\u5176\u4f59\u90e8\u5206\u6211\u4eec\u4f1a\u7ee7\u7eed\u63a2\u7d22\u79fb\u52a8\u8bed\u4e49\u7684\u5176\u4ed6\u8bdd\u9898\u3002\u6211\u4eec\u8fd8\u5c06\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\u4fee\u590d Auto_ptr2 \u7c7b\u3002 \u5728C++ 11\u4e2d\uff0c std::auto_ptr \u5df2\u7ecf\u88ab\u4e00\u5806\u5176\u4ed6\u7c7b\u578b\u7684\u201c\u79fb\u52a8\u8bed\u4e49\u517c\u5bb9\u7684\u201d\u667a\u80fd\u6307\u9488\u6240\u53d6\u4ee3\uff1a std::unique_ptr , std::weak_ptr \u548c std::shared_ptr \u3002\u6211\u4eec\u8fd8\u5c06\u63a2\u8ba8\u5176\u4e2d\u6700\u6d41\u884c\u7684\u4e24\u4e2a\uff1a unique_ptr (\u5b83\u76f4\u63a5\u66ff\u6362\u4e86 auto_ptr )\u548c shared_ptr \u3002","title":"\u7ee7\u7eed"},{"location":"M-2-R-value-references/","tags":["R-value-references","references"],"text":"Key Takeaway \u5728\u7b2c\u4e5d\u7ae0\u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86 \u503c\u7c7b\u578b ( 9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09 )\u2014\u2014\u503c\u7c7b\u578b\u662f\u8868\u8fbe\u5f0f\u7684\u4e00\u4e2a\u5c5e\u6027\uff0c\u7528\u4e8e\u786e\u5b9a\u8868\u8fbe\u5f0f\u6c42\u503c\u5f97\u5230\u503c\u3001\u51fd\u6570\u8fd8\u662f\u5bf9\u8c61\u3002\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86 \u5de6\u503c \u548c \u53f3\u503c \uff0c\u4ee5\u4fbf\u5c55\u5f00\u5bf9 \u5de6\u503c\u5f15\u7528 \u7684\u8ba8\u8bba\u3002 \u5982\u679c\u4f60\u5bf9\u5de6\u503c\u548c\u53f3\u503c\u611f\u5230\u751f\u758f\u4e86\uff0c\u73b0\u5728\u6b63\u662f\u590d\u4e60\u5b83\u4eec\u7684\u597d\u65f6\u673a\u3002 \u590d\u4e60\uff1a\u5de6\u503c \u5728C++11\u4e4b\u524d\uff0cC++\u4e2d\u53ea\u6709\u5de6\u503c\u5f15\u7528\u8fd9\u4e00\u79cd\u5f15\u7528\uff0c\u6240\u4ee5\u5b83\u88ab\u79f0\u4e3a\u201c\u5f15\u7528\u201d\u3002\u4f46\u662f\u5230\u4e86C++11\uff0c\u5b83\u88ab\u91cd\u65b0\u547d\u540d\u4e3a\u5de6\u503c\u5f15\u7528\u3002 \u5de6\u503c\u5f15\u7528\u5fc5\u987b\u901a\u8fc7\u53ef\u4fee\u6539\u5de6\u503c\u521d\u59cb\u5316\u3002 \u5de6\u503c\u5f15\u7528 \u53ef\u7528\u4e8e\u521d\u59cb\u5316 \u53ef\u4fee\u6539 Modifiable l-values Yes Yes Non-modifiable l-values No No R-values No No const \u5bf9\u8c61\u7684\u5de6\u503c\u5f15\u7528\u53ef\u4ee5\u901a\u8fc7\u53ef\u4fee\u6539\u3001\u4e0d\u53ef\u4fee\u6539\u7684\u5de6\u503c\u6216\u53f3\u503c\u521d\u59cb\u5316\u3002\u4f46\u662f\uff0c\u8fd9\u4e9b\u503c\u90fd\u4e0d\u80fd\u88ab\u4fee\u6539\u3002 const\u5de6\u503c\u7684\u5f15\u7528 \u53ef\u7528\u4e8e\u521d\u59cb\u5316 \u53ef\u4fee\u6539 Modifiable l-values Yes No Non-modifiable l-values Yes No R-values Yes No const \u5bf9\u8c61\u7684\u5de6\u503c\u5f15\u7528\u5c24\u5176\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u914d\u5408\u4efb\u4f55\u7c7b\u578b\u7684\u5b9e\u53c2\uff08\u5de6\u503c\u6216\u53f3\u503c\uff09\u5de5\u4f5c\uff0c\u4e14\u4e0d\u4f1a\u521b\u5efa\u62f7\u8d1d\u3002 R-value references C++11 \u6dfb\u52a0\u4e86\u4e00\u79cd\u65b0\u7684\u5f15\u7528\u7c7b\u578b\uff0c\u79f0\u4e3a \u53f3\u503c\u5f15\u7528 \u3002\u53f3\u503c\u5f15\u7528\u88ab\u8bbe\u8ba1\u51fa\u6765\u7684\u76ee\u7684\uff0c\u662f\u4e3a\u4e86\u914d\u5408\u53f3\u503c\u4f7f\u7528\uff08\u4e5f\u53ea\u80fd\u914d\u5408\u53f3\u503c\u4f7f\u7528\uff09\u3002\u521b\u5efa\u5de6\u503c\u5f15\u7528\u4f7f\u7528\u4e00\u4e2a & \u7b26\u53f7\uff0c\u521b\u5efa\u53f3\u503c\u5f15\u7528\u5219\u4f7f\u7528\u4e24\u4e2a\uff1a 1 2 3 int x { 5 }; int & lref { x }; // \u901a\u8fc7\u5de6\u503c\u6765\u521d\u59cb\u5316\u4e00\u4e2a\u5de6\u503c\u5f15\u7528 int && rref { 5 }; // \u4f7f\u7528\u53f3\u503c 5\uff08\u5b57\u9762\u91cf\uff09\u6765\u521d\u59cb\u5316\u53f3\u503c\u5f15\u7528 \u53f3\u503c\u5f15\u7528\u4e0d\u80fd\u591f\u901a\u8fc7\u5de6\u503c\u6765\u521d\u59cb\u5316\u3002 \u53f3\u503c\u5f15\u7528 \u53ef\u7528\u4e8e\u521d\u59cb\u5316 \u53ef\u4fee\u6539 Modifiable l-values No No Non-modifiable l-values No No R-values Yes Yes const\u53f3\u503c\u5f15\u7528 \u53ef\u7528\u4e8e\u521d\u59cb\u5316 \u53ef\u4fee\u6539 Modifiable l-values No No Non-modifiable l-values No No R-values Yes No \u53f3\u503c\u5f15\u7528\u6709\u4e24\u4e2a\u6709\u7528\u7684\u5c5e\u6027\u3002\u9996\u5148\uff0c \u53f3\u503c\u5f15\u7528\u53ef\u4ee5\u5c06\u521d\u59cb\u5316\u5b83\u4eec\u7684\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u5ef6\u957f\u5230\u81ea\u5df1\u7684\u751f\u547d\u5468\u671f (\u5bf9const\u5bf9\u8c61\u7684\u5de6\u503c\u5f15\u7528\u4e5f\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9)\u3002\u5176\u6b21\uff0c\u4f60==\u53ef\u4ee5\u901a\u8fc7\u6307\u5411\u975econst\u53f3\u503c\u5f15\u7528\u6765\u4fee\u6539\u53f3\u503c\u3002== \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include class Fraction { private : int m_numerator ; int m_denominator ; public : Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } }; int main () { auto && rref { Fraction { 3 , 5 } }; // \u5bf9\u4e34\u65f6 Fraction \u7684\u53f3\u503c\u5f15\u7528 // operator<< \u7684\u53c2\u6570f1\u4f1a\u7ed1\u5b9a\u5230\u4e34\u65f6\u5bf9\u8c61\uff0c\u800c\u4e0d\u4f1a\u521b\u5efa\u62f7\u8d1d std :: cout << rref << '\\n' ; return 0 ; } // rref (\u548c\u4e34\u65f6\u7684Fraction) \u79bb\u5f00\u4f5c\u7528\u57df \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 3/5 \u4f5c\u4e3a\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff0c Fraction(3, 5) \u901a\u5e38\u4f1a\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u540e\u5c31\u79bb\u5f00\u4f5c\u7528\u57df\u3002\u4f46\u662f\uff0c\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528\u5b83\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\uff0c\u6240\u4ee5\u5b83\u7684\u6301\u7eed\u65f6\u95f4\u88ab\u5ef6\u957f\u5230\u4e86\u548c\u8be5\u53f3\u503c\u5f15\u7528\u4e00\u6837\u2014\u2014\u8bed\u53e5\u5757\u7ed3\u5c3e\u65f6\u79bb\u5f00\u4f5c\u7528\u57df\u3002\u7136\u540e\uff0c\u6211\u4eec\u53d8\u53ef\u4ee5\u901a\u8fc7\u8be5\u53f3\u503c\u5f15\u7528\u6253\u5370 Fraction \u7684\u503c\u3002 \u73b0\u5728\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u4e0d\u90a3\u4e48\u76f4\u89c2\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 #include int main () { int && rref { 5 }; // \u901a\u8fc7\u5b57\u9762\u91cf\u521d\u59cb\u5316\u53f3\u503c\u5f15\u7528\uff0c\u6b64\u65f6\u521b\u5efa\u4e86\u4e34\u65f6\u5bf9\u8c615 rref = 10 ; std :: cout << rref << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 10 \u867d\u7136\u7528\u5b57\u9762\u503c\u521d\u59cb\u5316\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\uff0c\u7136\u540e\u518d\u901a\u8fc7\u53f3\u503c\u5f15\u7528\u4fee\u6539\u8be5\u53f3\u503c\u770b\u8d77\u6765\u6709\u4e9b\u5947\u602a\u3002\u4f46\u662f\u5f53\u6211\u4eec\u8fd9\u4e48\u505a\u65f6\uff0c\u4f1a\u4ece\u5b57\u9762\u503c\u6784\u9020\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff0c\u6b64\u65f6\u53f3\u503c\u5f15\u7528\u5f15\u7528\u7684\u5bf9\u8c61\u662f\u8be5\u4e34\u65f6\u5bf9\u8c61\u800c\u4e0d\u662f\u5b57\u9762\u91cf\u672c\u8eab\u3002 \u4e0d\u8fc7\uff0c\u4e0a\u9762\u4e24\u4e2a\u4f8b\u5b50\u5e76\u4e0d\u662f\u5e38\u89c1\u7684\u53f3\u503c\u4f7f\u7528\u573a\u5408\u3002 \u4f7f\u7528\u53f3\u503c\u5f15\u7528\u4f5c\u4e3a\u51fd\u6570\u5f62\u53c2 \u53f3\u503c\u5f15\u7528\u901a\u5e38\u7528\u4f5c\u51fd\u6570\u5f62\u53c2 \u3002\u5728\u91cd\u8f7d\u51fd\u6570\u65f6\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u51fd\u6570\u7684\u884c\u4e3a\u5728\u5904\u7406\u5de6\u503c\u53f3\u503c\u65f6\u4e0d\u540c\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u53f3\u503c\u4f5c\u4e3a\u51fd\u6570\u5f62\u53c2\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include void fun ( const int & lref ) // \u5de6\u503c\u5f62\u53c2\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570 { std :: cout << \"l-value reference to const \\n \" ; } void fun ( int && rref ) // \u53f3\u503c\u5f62\u53c2\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570 { std :: cout << \"r-value reference \\n \" ; } int main () { int x { 5 }; fun ( x ); // l-value argument calls l-value version of function fun ( 5 ); // r-value argument calls r-value version of function return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 l-value reference to const r-value reference \u5982\u4f60\u6240\u89c1\uff0c\u5f53\u4f20\u9012\u4e00\u4e2a\u5de6\u503c\u65f6\uff0c\u91cd\u8f7d\u51fd\u6570\u89e3\u6790\u4e3a\u5177\u6709\u5de6\u503c\u5f15\u7528\u7684\u7248\u672c\u3002\u5f53\u4f20\u9012\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\uff0c\u91cd\u8f7d\u51fd\u6570\u89e3\u6790\u5230\u5177\u6709\u53f3\u503c\u5f15\u7528\u7684\u7248\u672c(\u8fd9\u88ab\u8ba4\u4e3a\u662f\u6bd4\u5de6\u503c\u5f15\u7528const\u66f4\u597d\u7684\u5339\u914d)\u3002 \u4e3a\u4ec0\u4e48\u8981\u8fd9\u4e48\u505a\u5462\uff1f\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8bfe\u4e2d\u66f4\u8be6\u7ec6\u5730\u8ba8\u8bba\u8fd9\u4e2a\u95ee\u9898\u3002\u6beb\u65e0\u7591\u95ee\uff0c\u8fd9\u662f \u79fb\u52a8\u8bed\u4e49 \u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002 \u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a 1 2 int && ref { 5 }; fun ( ref ); \u4e0a\u9762\u4ee3\u7801\u5b9e\u9645\u4e0a\u4f1a\u8c03\u7528\u5de6\u503c\u5f15\u7528\u7248\u672c\u7684\u51fd\u6570\uff01\u5c3d\u7ba1 ref \u662f\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\uff0c\u4f46\u662f\u5b83\u672c\u8eab\u662f\u4e00\u4e2a\u5de6\u503c\uff08\u6240\u6709\u6709\u540d\u5b57\u7684\u53d8\u91cf\u90fd\u662f\u5de6\u503c\uff09\u3002\u9020\u6210\u6df7\u6dc6\u7684\u6839\u672c\u539f\u56e0\u5728\u4e8e\uff0c\u6211\u4eec\u5728\u4e0d\u540c\u7684\u8bed\u5883\u4e2d\u4f7f\u7528\u4e86\u672f\u8bed\u201d\u53f3\u503c\u201c\u3002\u5e94\u8be5\u8fd9\u6837\u7406\u89e3\uff1a \u547d\u540d\u5bf9\u8c61\u662f\u5de6\u503c\uff0c\u533f\u540d\u5bf9\u8c61\u662f\u53f3\u503c\u3002\u547d\u540d\u5bf9\u8c61\u6216\u533f\u540d\u5bf9\u8c61\u7684\u7c7b\u578b\u4e0e\u5b83\u662f\u5de6\u503c\u8fd8\u662f\u53f3\u503c\u65e0\u5173 \u3002\u6216\u8005\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c\u53f3\u503c\u5f15\u7528\u4e0d\u53eb\u53f3\u503c\u5f15\u7528\u7684\u8bdd\uff0c\u5c31\u4e0d\u4f1a\u51fa\u73b0\u8fd9\u79cd\u65e0\u89e3\u4e86\u3002 \u8fd4\u56de\u53f3\u503c\u5f15\u7528 \u60a8\u51e0\u4e4e\u4e0d\u5e94\u8be5\u8fd4\u56de\u53f3\u503c\u7684\u5f15\u7528\uff0c\u539f\u56e0\u4e0e\u4f60\u51e0\u4e4e\u4e0d\u5e94\u8be5\u8fd4\u56de\u5de6\u503c\u7684\u5f15\u7528\u76f8\u540c \u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5f53\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u6700\u7ec8\u4f1a\u6210\u4e3a\u4e00\u4e2a \u60ac\u5782 \u5f15\u7528\u3002","title":"M.2 - \u53f3\u503c\u5f15\u7528"},{"location":"M-2-R-value-references/#_1","text":"\u5728C++11\u4e4b\u524d\uff0cC++\u4e2d\u53ea\u6709\u5de6\u503c\u5f15\u7528\u8fd9\u4e00\u79cd\u5f15\u7528\uff0c\u6240\u4ee5\u5b83\u88ab\u79f0\u4e3a\u201c\u5f15\u7528\u201d\u3002\u4f46\u662f\u5230\u4e86C++11\uff0c\u5b83\u88ab\u91cd\u65b0\u547d\u540d\u4e3a\u5de6\u503c\u5f15\u7528\u3002 \u5de6\u503c\u5f15\u7528\u5fc5\u987b\u901a\u8fc7\u53ef\u4fee\u6539\u5de6\u503c\u521d\u59cb\u5316\u3002 \u5de6\u503c\u5f15\u7528 \u53ef\u7528\u4e8e\u521d\u59cb\u5316 \u53ef\u4fee\u6539 Modifiable l-values Yes Yes Non-modifiable l-values No No R-values No No const \u5bf9\u8c61\u7684\u5de6\u503c\u5f15\u7528\u53ef\u4ee5\u901a\u8fc7\u53ef\u4fee\u6539\u3001\u4e0d\u53ef\u4fee\u6539\u7684\u5de6\u503c\u6216\u53f3\u503c\u521d\u59cb\u5316\u3002\u4f46\u662f\uff0c\u8fd9\u4e9b\u503c\u90fd\u4e0d\u80fd\u88ab\u4fee\u6539\u3002 const\u5de6\u503c\u7684\u5f15\u7528 \u53ef\u7528\u4e8e\u521d\u59cb\u5316 \u53ef\u4fee\u6539 Modifiable l-values Yes No Non-modifiable l-values Yes No R-values Yes No const \u5bf9\u8c61\u7684\u5de6\u503c\u5f15\u7528\u5c24\u5176\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u914d\u5408\u4efb\u4f55\u7c7b\u578b\u7684\u5b9e\u53c2\uff08\u5de6\u503c\u6216\u53f3\u503c\uff09\u5de5\u4f5c\uff0c\u4e14\u4e0d\u4f1a\u521b\u5efa\u62f7\u8d1d\u3002","title":"\u590d\u4e60\uff1a\u5de6\u503c"},{"location":"M-2-R-value-references/#r-value-references","text":"C++11 \u6dfb\u52a0\u4e86\u4e00\u79cd\u65b0\u7684\u5f15\u7528\u7c7b\u578b\uff0c\u79f0\u4e3a \u53f3\u503c\u5f15\u7528 \u3002\u53f3\u503c\u5f15\u7528\u88ab\u8bbe\u8ba1\u51fa\u6765\u7684\u76ee\u7684\uff0c\u662f\u4e3a\u4e86\u914d\u5408\u53f3\u503c\u4f7f\u7528\uff08\u4e5f\u53ea\u80fd\u914d\u5408\u53f3\u503c\u4f7f\u7528\uff09\u3002\u521b\u5efa\u5de6\u503c\u5f15\u7528\u4f7f\u7528\u4e00\u4e2a & \u7b26\u53f7\uff0c\u521b\u5efa\u53f3\u503c\u5f15\u7528\u5219\u4f7f\u7528\u4e24\u4e2a\uff1a 1 2 3 int x { 5 }; int & lref { x }; // \u901a\u8fc7\u5de6\u503c\u6765\u521d\u59cb\u5316\u4e00\u4e2a\u5de6\u503c\u5f15\u7528 int && rref { 5 }; // \u4f7f\u7528\u53f3\u503c 5\uff08\u5b57\u9762\u91cf\uff09\u6765\u521d\u59cb\u5316\u53f3\u503c\u5f15\u7528 \u53f3\u503c\u5f15\u7528\u4e0d\u80fd\u591f\u901a\u8fc7\u5de6\u503c\u6765\u521d\u59cb\u5316\u3002 \u53f3\u503c\u5f15\u7528 \u53ef\u7528\u4e8e\u521d\u59cb\u5316 \u53ef\u4fee\u6539 Modifiable l-values No No Non-modifiable l-values No No R-values Yes Yes const\u53f3\u503c\u5f15\u7528 \u53ef\u7528\u4e8e\u521d\u59cb\u5316 \u53ef\u4fee\u6539 Modifiable l-values No No Non-modifiable l-values No No R-values Yes No \u53f3\u503c\u5f15\u7528\u6709\u4e24\u4e2a\u6709\u7528\u7684\u5c5e\u6027\u3002\u9996\u5148\uff0c \u53f3\u503c\u5f15\u7528\u53ef\u4ee5\u5c06\u521d\u59cb\u5316\u5b83\u4eec\u7684\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u5ef6\u957f\u5230\u81ea\u5df1\u7684\u751f\u547d\u5468\u671f (\u5bf9const\u5bf9\u8c61\u7684\u5de6\u503c\u5f15\u7528\u4e5f\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9)\u3002\u5176\u6b21\uff0c\u4f60==\u53ef\u4ee5\u901a\u8fc7\u6307\u5411\u975econst\u53f3\u503c\u5f15\u7528\u6765\u4fee\u6539\u53f3\u503c\u3002== \u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include class Fraction { private : int m_numerator ; int m_denominator ; public : Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } }; int main () { auto && rref { Fraction { 3 , 5 } }; // \u5bf9\u4e34\u65f6 Fraction \u7684\u53f3\u503c\u5f15\u7528 // operator<< \u7684\u53c2\u6570f1\u4f1a\u7ed1\u5b9a\u5230\u4e34\u65f6\u5bf9\u8c61\uff0c\u800c\u4e0d\u4f1a\u521b\u5efa\u62f7\u8d1d std :: cout << rref << '\\n' ; return 0 ; } // rref (\u548c\u4e34\u65f6\u7684Fraction) \u79bb\u5f00\u4f5c\u7528\u57df \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 3/5 \u4f5c\u4e3a\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff0c Fraction(3, 5) \u901a\u5e38\u4f1a\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u540e\u5c31\u79bb\u5f00\u4f5c\u7528\u57df\u3002\u4f46\u662f\uff0c\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528\u5b83\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\uff0c\u6240\u4ee5\u5b83\u7684\u6301\u7eed\u65f6\u95f4\u88ab\u5ef6\u957f\u5230\u4e86\u548c\u8be5\u53f3\u503c\u5f15\u7528\u4e00\u6837\u2014\u2014\u8bed\u53e5\u5757\u7ed3\u5c3e\u65f6\u79bb\u5f00\u4f5c\u7528\u57df\u3002\u7136\u540e\uff0c\u6211\u4eec\u53d8\u53ef\u4ee5\u901a\u8fc7\u8be5\u53f3\u503c\u5f15\u7528\u6253\u5370 Fraction \u7684\u503c\u3002 \u73b0\u5728\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u4e0d\u90a3\u4e48\u76f4\u89c2\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 #include int main () { int && rref { 5 }; // \u901a\u8fc7\u5b57\u9762\u91cf\u521d\u59cb\u5316\u53f3\u503c\u5f15\u7528\uff0c\u6b64\u65f6\u521b\u5efa\u4e86\u4e34\u65f6\u5bf9\u8c615 rref = 10 ; std :: cout << rref << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 10 \u867d\u7136\u7528\u5b57\u9762\u503c\u521d\u59cb\u5316\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\uff0c\u7136\u540e\u518d\u901a\u8fc7\u53f3\u503c\u5f15\u7528\u4fee\u6539\u8be5\u53f3\u503c\u770b\u8d77\u6765\u6709\u4e9b\u5947\u602a\u3002\u4f46\u662f\u5f53\u6211\u4eec\u8fd9\u4e48\u505a\u65f6\uff0c\u4f1a\u4ece\u5b57\u9762\u503c\u6784\u9020\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff0c\u6b64\u65f6\u53f3\u503c\u5f15\u7528\u5f15\u7528\u7684\u5bf9\u8c61\u662f\u8be5\u4e34\u65f6\u5bf9\u8c61\u800c\u4e0d\u662f\u5b57\u9762\u91cf\u672c\u8eab\u3002 \u4e0d\u8fc7\uff0c\u4e0a\u9762\u4e24\u4e2a\u4f8b\u5b50\u5e76\u4e0d\u662f\u5e38\u89c1\u7684\u53f3\u503c\u4f7f\u7528\u573a\u5408\u3002","title":"R-value references"},{"location":"M-2-R-value-references/#_2","text":"\u53f3\u503c\u5f15\u7528\u901a\u5e38\u7528\u4f5c\u51fd\u6570\u5f62\u53c2 \u3002\u5728\u91cd\u8f7d\u51fd\u6570\u65f6\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u51fd\u6570\u7684\u884c\u4e3a\u5728\u5904\u7406\u5de6\u503c\u53f3\u503c\u65f6\u4e0d\u540c\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u53f3\u503c\u4f5c\u4e3a\u51fd\u6570\u5f62\u53c2\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include void fun ( const int & lref ) // \u5de6\u503c\u5f62\u53c2\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570 { std :: cout << \"l-value reference to const \\n \" ; } void fun ( int && rref ) // \u53f3\u503c\u5f62\u53c2\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570 { std :: cout << \"r-value reference \\n \" ; } int main () { int x { 5 }; fun ( x ); // l-value argument calls l-value version of function fun ( 5 ); // r-value argument calls r-value version of function return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 l-value reference to const r-value reference \u5982\u4f60\u6240\u89c1\uff0c\u5f53\u4f20\u9012\u4e00\u4e2a\u5de6\u503c\u65f6\uff0c\u91cd\u8f7d\u51fd\u6570\u89e3\u6790\u4e3a\u5177\u6709\u5de6\u503c\u5f15\u7528\u7684\u7248\u672c\u3002\u5f53\u4f20\u9012\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\uff0c\u91cd\u8f7d\u51fd\u6570\u89e3\u6790\u5230\u5177\u6709\u53f3\u503c\u5f15\u7528\u7684\u7248\u672c(\u8fd9\u88ab\u8ba4\u4e3a\u662f\u6bd4\u5de6\u503c\u5f15\u7528const\u66f4\u597d\u7684\u5339\u914d)\u3002 \u4e3a\u4ec0\u4e48\u8981\u8fd9\u4e48\u505a\u5462\uff1f\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8bfe\u4e2d\u66f4\u8be6\u7ec6\u5730\u8ba8\u8bba\u8fd9\u4e2a\u95ee\u9898\u3002\u6beb\u65e0\u7591\u95ee\uff0c\u8fd9\u662f \u79fb\u52a8\u8bed\u4e49 \u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002 \u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a 1 2 int && ref { 5 }; fun ( ref ); \u4e0a\u9762\u4ee3\u7801\u5b9e\u9645\u4e0a\u4f1a\u8c03\u7528\u5de6\u503c\u5f15\u7528\u7248\u672c\u7684\u51fd\u6570\uff01\u5c3d\u7ba1 ref \u662f\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\uff0c\u4f46\u662f\u5b83\u672c\u8eab\u662f\u4e00\u4e2a\u5de6\u503c\uff08\u6240\u6709\u6709\u540d\u5b57\u7684\u53d8\u91cf\u90fd\u662f\u5de6\u503c\uff09\u3002\u9020\u6210\u6df7\u6dc6\u7684\u6839\u672c\u539f\u56e0\u5728\u4e8e\uff0c\u6211\u4eec\u5728\u4e0d\u540c\u7684\u8bed\u5883\u4e2d\u4f7f\u7528\u4e86\u672f\u8bed\u201d\u53f3\u503c\u201c\u3002\u5e94\u8be5\u8fd9\u6837\u7406\u89e3\uff1a \u547d\u540d\u5bf9\u8c61\u662f\u5de6\u503c\uff0c\u533f\u540d\u5bf9\u8c61\u662f\u53f3\u503c\u3002\u547d\u540d\u5bf9\u8c61\u6216\u533f\u540d\u5bf9\u8c61\u7684\u7c7b\u578b\u4e0e\u5b83\u662f\u5de6\u503c\u8fd8\u662f\u53f3\u503c\u65e0\u5173 \u3002\u6216\u8005\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c\u53f3\u503c\u5f15\u7528\u4e0d\u53eb\u53f3\u503c\u5f15\u7528\u7684\u8bdd\uff0c\u5c31\u4e0d\u4f1a\u51fa\u73b0\u8fd9\u79cd\u65e0\u89e3\u4e86\u3002","title":"\u4f7f\u7528\u53f3\u503c\u5f15\u7528\u4f5c\u4e3a\u51fd\u6570\u5f62\u53c2"},{"location":"M-2-R-value-references/#_3","text":"\u60a8\u51e0\u4e4e\u4e0d\u5e94\u8be5\u8fd4\u56de\u53f3\u503c\u7684\u5f15\u7528\uff0c\u539f\u56e0\u4e0e\u4f60\u51e0\u4e4e\u4e0d\u5e94\u8be5\u8fd4\u56de\u5de6\u503c\u7684\u5f15\u7528\u76f8\u540c \u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5f53\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u6700\u7ec8\u4f1a\u6210\u4e3a\u4e00\u4e2a \u60ac\u5782 \u5f15\u7528\u3002","title":"\u8fd4\u56de\u53f3\u503c\u5f15\u7528"},{"location":"M-3-move-constructors-and-move-assignment/","tags":["R-value-references","references"],"text":"Key Takeaway \u5b9e\u73b0\u62f7\u8d1d\u8bed\u4e49\u65f6\u9700\u8981\u4f7f\u7528const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528\u4f5c\u4e3a\u5f62\u53c2\uff0c\u800c\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49\u65f6\uff0c\u9700\u8981\u4f7f\u7528\u975econst\u7684\u53f3\u503c\u5f62\u53c2\uff1b \u7f16\u8bd1\u5668\u4e0d\u4f1a\u63d0\u4f9b\u9ed8\u8ba4\u7684\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u9664\u975e\u8be5\u7c7b\u6ca1\u6709\u5b9a\u4e49\u4efb\u4f55\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3001\u62f7\u8d1d\u8d4b\u503c\u3001\u79fb\u52a8\u8d4b\u503c\u6216\u6790\u6784\u51fd\u6570 \u5de6\u503c\u7a0d\u540e\u53ef\u80fd\u4f1a\u88ab\u4f7f\u7528\uff0c\u6240\u4ee5\u53ea\u6709\u62f7\u8d1d\u5b83\u662f\u5b89\u5168\u7684\u3002\u53f3\u503c\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u540e\u603b\u662f\u4f1a\u88ab\u9500\u6bc1\uff0c\u6240\u4ee5\u79fb\u52a8\u5b83\u662f\u5b89\u5168\u7684 \u6709\u4e86\u53f3\u503c\u5f15\u7528\uff0c\u51fd\u6570\u5c31\u80fd\u591f\u57fa\u4e8e\u53c2\u6570\u662f\u5de6\u503c\u8fd8\u662f\u53f3\u503c\u8868\u73b0\u51fa\u4e0d\u540c\u7684\u884c\u6587\uff0c\u4e5f\u5c31\u53ef\u4ee5\u9488\u5bf9\u53f3\u503c\u4f7f\u7528\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c \u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49\u65f6\uff0c\u8981\u4fdd\u6301\u53cc\u65b9\u90fd\u5904\u4e8e\u6709\u6548\u72b6\u6001 \u5c40\u90e8\u53d8\u91cf\u5de6\u503c\u4ece\u51fd\u6570\u4e2d\u6309\u503c\u8fd4\u56de\u65f6\uff0cC++\u4f1a\u79fb\u52a8\u5b83\u800c\u4e0d\u662f\u62f7\u8d1d\u5b83\uff0c\u5f53\u7136\u6709\u7684\u7f16\u8bd1\u5668\u4e5f\u4f1a\u76f4\u63a5\u5ffd\u7565\u3002 \u5728\u652f\u6301\u79fb\u52a8\u8bed\u4e49\u7684\u7c7b\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u5220\u9664\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u51fd\u6570\uff0c\u4ee5\u786e\u4fdd\u4e0d\u4f1a\u6267\u884c\u62f7\u8d1d \u5728 M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86 std::auto_ptr \uff0c\u8fdb\u800c\u5f15\u7533\u51fa\u4e86 \u79fb\u52a8\u8bed\u4e49 \u7684\u91cd\u8981\u6027\u3002\u7136\u540e\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u5f53\u5c06\u672c\u6765\u88ab\u8bbe\u8ba1\u4e3a\u62f7\u8d1d\u8bed\u4e49\u7684\u51fd\u6570\uff08 \u62f7\u8d1d\u521d\u59cb\u5316 \u548c \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \uff09\u88ab\u7528\u6765\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49\u65f6\u4f1a\u5e26\u6765\u7684\u95ee\u9898\u3002 \u5728\u672c\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u6df1\u5165\u5206\u6790C++11\u662f\u5982\u4f55\u901a\u8fc7 \u79fb\u52a8\u6784\u9020\u51fd\u6570 \u548c \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 \u6765\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\u7684\u3002 \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c \u9996\u5148\uff0c\u590d\u4e60\u4e00\u4e0b \u62f7\u8d1d\u8bed\u4e49 \u3002 \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u7528\u4e8e\u901a\u8fc7\u62f7\u8d1d\u4e00\u4e2a\u540c\u7c7b\u5bf9\u8c61\u6765\u521d\u59cb\u5316\u53e6\u4e00\u4e2a\u5bf9\u8c61\u3002\u62f7\u8d1d\u8d4b\u503c\u5219\u7528\u4e8e\u5c06\u4e00\u4e2a\u7c7b\u5bf9\u8c61\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u73b0\u6709\u7c7b\u5bf9\u8c61\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u6ca1\u6709\u663e\u5f0f\u63d0\u4f9b\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0cC++\u4f1a\u63d0\u4f9b\u4e00\u4e2a\u9ed8\u8ba4\u7248\u672c\u3002\u8fd9\u4e9b\u7f16\u8bd1\u5668\u63d0\u4f9b\u7684\u51fd\u6570\u4f1a\u6267\u884c \u6d45\u62f7\u8d1d \uff0c\u8fd9\u53ef\u80fd\u4f1a\u7ed9\u5206\u914d\u52a8\u6001\u5185\u5b58\u7684\u7c7b\u5e26\u6765\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u5904\u7406\u52a8\u6001\u5185\u5b58\u7684\u7c7b\u5e94\u8be5\u91cd\u5199\u8fd9\u4e9b\u51fd\u6570\u6765\u8fdb\u884c \u6df1\u62f7\u8d1d \u3002 \u56de\u5230\u672c\u7ae0\u7b2c\u4e00\u8bfe\u4e2d\u7684 Auto_ptr \u667a\u80fd\u6307\u9488\u7c7b\u793a\u4f8b\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u5b9e\u73b0\u4e86\u6df1\u62f7\u8d1d\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u64cd\u4f5c\u7b26\u7684\u7248\u672c\u80fd\u5426\u6b63\u5e38\u5de5\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 #include template < typename T > class Auto_ptr3 { T * m_ptr ; public : Auto_ptr3 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } ~ Auto_ptr3 () { delete m_ptr ; } // \u62f7\u8d1d\u6784\u9020\u51fd\u6570 // \u5c06 a.m_ptr \u6df1\u62f7\u8d1d\u5230 m_ptr Auto_ptr3 ( const Auto_ptr3 & a ) { m_ptr = new T ; * m_ptr = * a . m_ptr ; //\u89e3\u5f15\u7528\u7684\u7ed3\u679c\u662f\u4e00\u4e2aResource\u5bf9\u8c61\uff0c\u5b83\u80fd\u591f\u901a\u8fc7\u9ed8\u8ba4\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u62f7\u8d1d\u8d4b\u503c\u81ea\u5df1 } // \u62f7\u8d1d\u8d4b\u503c // \u5c06 a.m_ptr \u6df1\u62f7\u8d1d\u5230 m_ptr Auto_ptr3 & operator = ( const Auto_ptr3 & a ) { // Self-assignment detection if ( & a == this ) return * this ; // Release any resource we're holding delete m_ptr ; // Copy the resource m_ptr = new T ; * m_ptr = * a . m_ptr ; return * this ; } T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } bool isNull () const { return m_ptr == nullptr ; } }; class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; Auto_ptr3 < Resource > generateResource () { Auto_ptr3 < Resource > res { new Resource }; return res ; // \u8fd4\u56de\u503c\u4f1a\u89e6\u53d1\u62f7\u8d1d\u6784\u9020\u51fd\u6570 } int main () { Auto_ptr3 < Resource > mainres ; mainres = generateResource (); // \u8d4b\u503c\u4f1a\u89e6\u53d1\u62f7\u8d1d\u8d4b\u503c return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c generateResource() \u51fd\u6570\u4f1a\u521b\u5efa\u4e00\u4e2a\u88ab\u667a\u80fd\u6307\u9488\u5c01\u88c5\u7684\u8d44\u6e90\uff0c\u7136\u540e\u8be5\u8d44\u6e90\u4f1a\u88ab\u8fd4\u56de\u7ed9\u4e3b\u51fd\u6570\u3002\u4e3b\u51fd\u6570\u968f\u540e\u5c06\u5176\u8d4b\u503c\u7ed9 Auto_ptr3 \u5bf9\u8c61\u3002 \u7a0b\u5e8f\u8fd0\u884c\u65f6\u6253\u5370\uff1a 1 2 3 4 5 6 Resource acquired Resource acquired Resource destroyed Resource acquired Resource destroyed Resource destroyed (\u6ce8\u610f:\u5982\u679c\u7f16\u8bd1\u5668\u7701\u7565\u4e86generateResource()\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u4f60\u53ef\u80fd\u53ea\u4f1a\u5f97\u52304\u884c\u8f93\u51fa) \u5982\u6b64\u7b80\u5355\u7684\u7a0b\u5e8f\uff0c\u7adf\u7136\u8981\u521b\u5efa\u5e76\u9500\u6bc1\u8fd9\u4e48\u591a\u6b21\uff1f\u4ec0\u4e48\u60c5\u51b5\uff01 \u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\u67096\u4e2a\u5173\u952e\u6b65\u9aa4(\u6bcf\u4e2a\u6253\u5370\u4fe1\u606f\u4e00\u4e2a\u6b65\u9aa4)\uff1a \u5728 generateResource() \u4e2d\uff0c\u5c40\u90e8\u53d8\u91cf res \u88ab\u521b\u5efa\uff0c\u540c\u65f6\u88ab\u4e00\u4e2a\u52a8\u6001\u5206\u914d\u7684 Resource \u5bf9\u8c61\u521d\u59cb\u5316\uff0c\u56e0\u6b64\u7b2c\u4e00\u6b21\u6253\u5370\u4e86 \u201cResource acquired\u201d\uff1b Res \u6309\u503c\u8fd4\u56de main() \u3002\u8fd9\u91cc\u4f7f\u7528\u6309\u503c\u8fd4\u56de\u662f\u56e0\u4e3a res \u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u5982\u679c\u6309\u5730\u5740\u8fd4\u56de\u6216\u6309\u5f15\u7528\u8fd4\u56de\u5219\u4f1a\u5e26\u6765\u95ee\u9898\u56e0\u4e3a res \u5728 generateResource() \u51fd\u6570\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u4e86\u3002\u56e0\u6b64\uff0c res \u4f1a\u88ab==\u62f7\u8d1d\u6784\u5efa\u5230\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\u4e2d==\u3002\u56e0\u4e3a\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4f7f\u7528\u4e86\u6df1\u62f7\u8d1d\uff0c\u6240\u4ee5\u5b83\u5206\u914d\u4e86\u4e00\u4e2a\u65b0\u7684 Resource \uff0c\u6240\u4ee5\u7b2c\u4e8c\u6b21\u6253\u5370\u4e86 \u201cResource acquired\u201d\uff1b Res \u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u6700\u521d\u88ab\u521b\u5efa\u7684 Resource \u5bf9\u8c61\u5c31\u9500\u6bc1\u4e86\uff0c\u56e0\u6b64\u7b2c\u4e00\u6253\u5370\u4e86 \u201cResource destroyed\u201d\uff1b \u4e34\u65f6\u5bf9\u4e8e\u901a\u8fc7\u62f7\u8d1d\u8d4b\u503c\u88ab\u8d4b\u503c\u7ed9\u4e86 mainres \u3002\u56e0\u4e3a\u6b64\u5904\u7684\u62f7\u8d1d\u8d4b\u503c\u4f7f\u7528\u4e86\u6df1\u62f7\u8d1d\uff0c\u6240\u4ee5\u5b83\u5206\u914d\u4e86\u4e00\u4e2a\u65b0\u7684 Resource \uff0c\u6240\u4ee5\u7b2c\u4e09\u6b21\u6253\u5370\u4e86 \u201cResource acquired\u201d\uff1b \u5f53\u8d4b\u503c\u8868\u8fbe\u5f0f\u7ed3\u675f\u540e\uff0c\u4e34\u65f6\u5bf9\u8c61\u5c31\u79bb\u5f00\u4e86\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1\uff0c\u56e0\u6b64\u7b2c\u4e8c\u6b21\u6253\u5370\u4e86\u201cResource destroyed\u201d\uff1b \u5728 main() \u7684\u672b\u5c3e\uff0c mainres \u79bb\u5f00\u4e86\u4f5c\u7528\u57df\uff0c\u4e8e\u662f\u6253\u5370\u4e86\u6700\u540e\u4e00\u4e2a \u201cResource destroyed\u201d \u3002 \u7b80\u800c\u8a00\u4e4b\uff0c\u6211\u4eec\u8c03\u7528\u4e86\u4e00\u6b21\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u62f7\u8d1d\u6784\u9020\u4e86 res \u5230\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff0c\u7136\u540e\u8c03\u7528\u4e86\u4e00\u6b21\u62f7\u8d1d\u8d4b\u503c\u5c06\u4e34\u65f6\u5bf9\u8c61\u590d\u5236\u5230 mainres \u5bf9\u8c61\uff0c\u6240\u4ee5\u6700\u7ec8\u603b\u5171\u5206\u914d\u548c\u9500\u6bc1\u4e863\u4e2a\u5355\u72ec\u7684\u5bf9\u8c61\u3002 \u6548\u7387\u5f88\u4f4e\uff0c\u4f46\u81f3\u5c11\u4e0d\u4f1a\u5d29\u6e83\uff01 \u4f46\u662f\uff0c\u5982\u679c\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\u7684\u8bdd\uff0c\u6548\u679c\u4f1a\u66f4\u597d\uff01 \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c C++ 11\u4e3a\u4e86\u652f\u6301\u79fb\u52a8\u8bed\u4e49\uff0c\u65b0\u5b9a\u4e49\u4e86\u4e24\u4e2a\u51fd\u6570\uff1a\u4e00\u4e2a\u662f \u79fb\u52a8\u6784\u9020\u51fd\u6570 \uff0c\u4e00\u4e2a\u662f \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 \u3002\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u7684\u76ee\u7684\u662f\u5c06\u4e00\u4e2a\u5bf9\u8c61\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u5bf9\u8c61\uff0c\u800c\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u7684\u76ee\u7684\u662f\u5c06\u8d44\u6e90\u7684\u6240\u6709\u6743\u4ece\u4e00\u4e2a\u5bf9\u8c61\u8f6c\u79fb\u5230\u53e6\u4e00\u4e2a\u5bf9\u8c61(\u8fd9\u901a\u5e38\u6bd4\u590d\u5236\u6210\u672c\u4f4e\u5f97\u591a)\u3002 \u5b9a\u4e49\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\uff0c\u548c\u5b9a\u4e49\u5b83\u4eec\u7684\u201c\u62f7\u8d1d\u8bed\u4e49\u201d\u540c\u50da\u7c7b\u4f3c\u3002\u4e0d\u8fc7\uff0c \u5b9e\u73b0\u62f7\u8d1d\u8bed\u4e49\u65f6\u9700\u8981\u4f7f\u7528const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528\u4f5c\u4e3a\u5f62\u53c2\uff0c\u800c\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49\u65f6\uff0c\u9700\u8981\u4f7f\u7528\u975econst\u7684\u53f3\u503c\u5f62\u53c2\u3002 \u8fd9\u91cc\u662f\u4e0e\u4e0a\u9762\u76f8\u540c\u7684 Auto_ptr3 \u7c7b\uff0c\u6dfb\u52a0\u4e86\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002\u4e3a\u4e86\u8fdb\u884c\u6bd4\u8f83\uff0c\u6211\u4eec\u4fdd\u7559\u4e86\u6df1\u62f7\u8d1d\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 #include template < typename T > class Auto_ptr4 { T * m_ptr ; public : Auto_ptr4 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } ~ Auto_ptr4 () { delete m_ptr ; } // \u62f7\u8d1d\u6784\u9020\u51fd\u6570 // \u5c06 a.m_ptr \u6df1\u62f7\u8d1d\u5230 m_ptr Auto_ptr4 ( const Auto_ptr4 & a ) { m_ptr = new T ; * m_ptr = * a . m_ptr ; } // \u79fb\u52a8\u6784\u9020\u51fd\u6570 // \u8f6c\u79fb a.m_ptr \u7684\u8d44\u6e90\u7ed9 m_ptr Auto_ptr4 ( Auto_ptr4 && a ) noexcept //\u6ce8\u610f\u4f7f\u7528\u7684\u662f\u53f3\u503c\u5f15\u7528 : m_ptr ( a . m_ptr ) { a . m_ptr = nullptr ; // we'll talk more about this line below } // \u62f7\u8d1d\u8d4b\u503c // \u5c06 a.m_ptr \u6df1\u62f7\u8d1d\u5230 m_ptr Auto_ptr4 & operator = ( const Auto_ptr4 & a ) { // Self-assignment detection if ( & a == this ) return * this ; // Release any resource we're holding delete m_ptr ; // Copy the resource m_ptr = new T ; * m_ptr = * a . m_ptr ; return * this ; } // \u79fb\u52a8\u8d4b\u503c // \u8f6c\u79fb a.m_ptr \u7684\u8d44\u6e90\u7ed9 m_ptr Auto_ptr4 & operator = ( Auto_ptr4 && a ) noexcept { // Self-assignment detection if ( & a == this ) return * this ; // \u91ca\u653e\u5f53\u524d\u6301\u6709\u7684\u8d44\u6e90 delete m_ptr ; // \u8f6c\u79fb\u6240\u6709\u6743 m_ptr = a . m_ptr ; a . m_ptr = nullptr ; // we'll talk more about this line below return * this ; } T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } bool isNull () const { return m_ptr == nullptr ; } }; class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; Auto_ptr4 < Resource > generateResource () { Auto_ptr4 < Resource > res { new Resource }; return res ; // \u6b64\u5904\u4f1a\u8c03\u7528\u79fb\u52a8\u6784\u9020\u51fd\u6570 } int main () { Auto_ptr4 < Resource > mainres ; mainres = generateResource (); // \u6b64\u5904\u8d4b\u503c\u4f1a\u8c03\u7528\u79fb\u52a8\u8d4b\u503c return 0 ; } \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26\u5e76\u4e0d\u590d\u6742\u3002\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4e0d\u518d\u5c06\u6e90\u5bf9\u8c61( a )\u6df1\u62f7\u8d1d\u5230\u9690\u5f0f\u5bf9\u8c61\u4e2d\uff0c\u800c\u662f\u8f6c\u79fb\u4e86\u6e90\u5bf9\u8c61\u7684\u8d44\u6e90\u3002\u8fd9\u6d89\u53ca\u5230\u5c06\u6e90\u6307\u9488\u6d45\u62f7\u8d1d\u5230\u9690\u5f0f\u5bf9\u8c61\u4e2d\uff0c\u7136\u540e\u5c06\u6e90\u6307\u9488\u8bbe\u7f6e\u4e3a\u7a7a\u3002 \u7a0b\u5e8f\u8fd0\u884c\u8f93\u51fa\uff1a 1 2 Resource acquired Resource destroyed \u6548\u679c\u597d\u6781\u4e86\uff01 \u7a0b\u5e8f\u7684\u6d41\u7a0b\u548c\u4e4b\u524d\u7684\u5b8c\u5168\u4e00\u6837\u3002\u4f46\u662f\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\u8c03\u7528\u7684\u4e0d\u662f\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u800c\u662f\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002\u518d\u6df1\u5165\u4e00\u70b9\u770b\uff1a \u5728 generateResource() \uff0c\u5c40\u90e8\u53d8\u91cf res \u88ab\u521b\u5efa\uff0c\u540c\u65f6\u88ab\u4e00\u4e2a\u52a8\u6001\u5206\u914d\u7684 Resource \u5bf9\u8c61\u521d\u59cb\u5316\uff0c\u56e0\u6b64\u7b2c\u4e00\u6b21\u6253\u5370\u4e86 \u201cResource acquired\u201d\uff1b Res \u6309\u503c\u8fd4\u56de main() \u3002 res \u4f1a\u88ab==\u79fb\u52a8\u6784\u9020\u5230\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\u4e2d==\uff0c\u7136\u540e\u5c06\u5b58\u653e\u5728 res \u52a8\u6001\u521b\u5efa\u7684\u5bf9\u8c61\u7684\u6240\u6709\u6743\u8f6c\u79fb\u7ed9\u4e86\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff1b Res \u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u4f46\u662f\u7531\u4e8e res \u5df2\u7ecf\u4e0d\u7ba1\u7406\u6307\u9488\u4e86\uff08\u5df2\u7ecf\u8f6c\u79fb\u7ed9\u4e86\u4e34\u65f6\u5bf9\u8c61\uff09\uff0c\u6240\u4ee5\u4ec0\u4e48\u4e8b\u60c5\u90fd\u4e0d\u4f1a\u53d1\u751f\uff1b \u4e34\u65f6\u5bf9\u8c61\u88ab\u79fb\u52a8\u8d4b\u503c\u7ed9 mainres \uff0c\u6b64\u65f6\u5b58\u653e\u5728\u4e34\u65f6\u5bf9\u8c61\u4e2d\u7684\u52a8\u6001\u521b\u5efa\u7684\u8d44\u6e90\u5c31\u88ab\u8f6c\u79fb\u7ed9\u4e86 mainres \uff1b \u5f53\u8d4b\u503c\u8868\u8fbe\u5f0f\u7ed3\u675f\u65f6\uff0c\u4e34\u65f6\u5bf9\u8c61\u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1\uff0c\u4f46\u662f\u7531\u4e8e\u5b83\u5df2\u7ecf\u4e0d\u518d\u7ba1\u7406\u4efb\u4f55\u6307\u9488\uff08\u5df2\u7ecf\u8f6c\u79fb\u7ed9\u4e86 mainres \uff09\uff0c\u6240\u4ee5\u4ecd\u7136\u4ec0\u4e48\u90fd\u4e0d\u4f1a\u53d1\u751f\u3002 \u5728 main() \u7ed3\u675f\u7684\u5730\u65b9\uff0c mainres \u4f1a\u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u6240\u4ee5\u6253\u5370\u4e86 \u201cResource destroyed\u201d \u3002 \u56e0\u6b64\uff0c\u8fd9\u6b21\uff0c Resource \u5e76\u6ca1\u6709\u88ab\u590d\u5236\u4e24\u6b21(\u4e00\u6b21\u662f\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u4e00\u6b21\u662f\u62f7\u8d1d\u8d4b\u503c)\uff0c\u800c\u662f\u5c06\u5176\u8f6c\u79fb\u4e86\u4e24\u6b21\u3002\u8fd9\u4e48\u505a\u6548\u7387\u660e\u663e\u66f4\u9ad8\uff0c\u56e0\u4e3a\u8d44\u6e90\u53ea\u6784\u5efa\u548c\u6467\u6bc1\u4e00\u6b21\uff0c\u800c\u4e0d\u662f\u4e09\u6b21\u3002 \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u4f1a\u5728\u4f55\u65f6\u88ab\u8c03\u7528\uff1f \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u5728\u5b9a\u4e49\u540e\u624d\u4f1a\u88ab\u8c03\u7528\uff0c\u800c\u4e14\u7528\u4e8e\u6784\u9020\u6216\u8d4b\u503c\u7684\u53c2\u6570\u9700\u8981\u662f\u4e00\u4e2a\u53f3\u503c\u3002\u6700\u5178\u578b\u7684\u662f\uff0c\u8fd9\u4e2a\u53f3\u503c\u662f\u4e00\u4e2a\u5b57\u9762\u91cf\u6216\u4e34\u65f6\u503c\u3002 \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c \u7f16\u8bd1\u5668\u4e0d\u4f1a\u63d0\u4f9b\u9ed8\u8ba4\u7684\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u9664\u975e\u8be5\u7c7b\u6ca1\u6709\u5b9a\u4e49\u4efb\u4f55\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3001\u62f7\u8d1d\u8d4b\u503c\u3001\u79fb\u52a8\u8d4b\u503c\u6216\u6790\u6784\u51fd\u6570 \u3002 \u79fb\u52a8\u8bed\u4e49\u80cc\u540e\u7684\u5173\u952e\u4fe1\u606f \u73b0\u5728\u4f60\u5df2\u7ecf\u5177\u6709\u7406\u89e3\u79fb\u52a8\u8bed\u4e49\u80cc\u540e\u5173\u952e\u4fe1\u606f\u7684\u80cc\u666f\u77e5\u8bc6\u4e86\u3002 \u5f53\u4f7f\u7528\u4e00\u4e2a \u5de6\u503c \u5b9e\u53c2\u6784\u9020\u4e00\u4e2a\u5bf9\u8c61\u6216\u8fdb\u884c\u8d4b\u503c\u65f6\uff0c\u6211\u4eec\u552f\u4e00\u80fd\u505a\u7684\u5408\u7406\u7684\u4e8b\u60c5\u5c31\u662f\u590d\u5236\u8fd9\u4e2a\u5de6\u503c\u3002\u6211\u4eec\u4e0d\u80fd\u5047\u8bbe\u6539\u53d8\u53ef\u4ee5\u5b89\u5168\u5730\u6539\u53d8\u8fd9\u4e2a\u5de6\u503c\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u5728\u7a0d\u540e\u7684\u7a0b\u5e8f\u4e2d\u518d\u6b21\u88ab\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u8868\u8fbe\u5f0f a = b \uff0c\u6211\u4eec\u4e0d\u80fd\u671f\u671b\u6709\u4efb\u4f55\u5408\u7406\u7684\u65b9\u5f0f\u53ef\u4ee5\u7528\u6765\u4fee\u6539b\u7684\u503c\u3002 \u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a \u53f3\u503c \u6765\u6784\u9020\u4e00\u4e2a\u5bf9\u8c61\u6216\u8d4b\u503c\uff0c\u7531\u4e8e\u53f3\u503c\u53ea\u662f\u67d0\u79cd\u7c7b\u578b\u7684\u4e34\u65f6\u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4e0d\u5fc5\u590d\u5236\u5b83(\u590d\u5236\u5f00\u9500\u5f88\u5927)\uff0c\u800c\u662f\u53ef\u4ee5\u7b80\u5355\u5730\u5c06\u5b83\u7ba1\u7406\u7684\u8d44\u6e90\u8f6c\u79fb\u5230\u6b63\u5728\u6784\u9020\u6216\u5206\u914d\u7684\u5bf9\u8c61\u5373\u53ef\u3002\u8fd9\u6837\u505a\u662f\u5b89\u5168\u7684\uff0c\u56e0\u4e3a\u4e34\u65f6\u53d8\u91cf\u65e0\u8bba\u5982\u4f55\u90fd\u4f1a\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u3002\u663e\u7136\uff0c\u5b83\u6c38\u8fdc\u4e0d\u4f1a\u518d\u88ab\u4f7f\u7528! c++ 11\u901a\u8fc7 \u53f3\u503c\u5f15\u7528 \uff0c\u4f7f\u6211\u4eec\u80fd\u591f\u5728\u5b9e\u53c2\u662f\u53f3\u503c\u548c\u5de6\u503c\u65f6\u9009\u62e9\u4e0d\u540c\u7684\u884c\u4e3a\uff0c\u4ee5\u4fbf\u8bbe\u8ba1\u51fa\u66f4\u806a\u660e\u3001\u66f4\u6709\u6548\u7684\u5bf9\u8c61\u884c\u4e3a\u3002 \u79fb\u52a8\u51fd\u6570\u5e94\u8be5\u4fdd\u6301\u4e24\u4e2a\u5bf9\u8c61\u90fd\u5904\u4e8e\u6709\u6548\u72b6\u6001 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4e0d\u8bba\u662f\u79fb\u52a8\u6784\u9020\u51fd\u6570\u8fd8\u79fb\u52a8\u8d4b\u503c\u51fd\u6570\uff0c\u90fd\u4f1a\u5c06 a.m_ptr \u8bbe\u7f6e\u4e3a nullptr \uff08\u7a7a\u6307\u9488\u5b57\u9762\u91cf\uff09\u3002\u8fd9\u4e48\u505a\u770b\u4e0a\u53bb\u6709\u70b9\u591a\u4f59\u2014\u2014\u6bd5\u7adf\u8fd9\u53ea\u662f\u4e00\u4e2a\u4e34\u65f6\u7684\u53f3\u503c\u3002\u65e2\u7136\u5b83\u7a0d\u540e\u5c31\u4f1a\u88ab\u9500\u6bc1\uff0c\u4e3a\u5565\u8fd8\u8981\u624b\u5de5\u201c\u6e05\u7406\u201d\u5b83\u5462\uff1f \u7b54\u6848\u5f88\u7b80\u5355\uff1a\u5f53 a \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c\u6790\u6784\u51fd\u6570\u4f1a\u88ab\u8c03\u7528\uff0c\u6240\u4ee5 a.m_ptr \u4f1a\u88ab\u5220\u9664\u3002\u5728\u64cd\u4f5c\u6267\u884c\u65f6\uff0c\u5982\u679c a.m_ptr \u4ecd\u7136\u6307\u5411\u4e0e m_ptr \u6307\u5411\u76f8\u540c\u7684\u5bf9\u8c61\uff0c\u5219 m_ptr \u663e\u7136\u4f1a\u53d8\u6210\u4e00\u4e2a \u60ac\u5782 \u6307\u9488\u3002\u5f53\u6211\u4eec\u540e\u9762\u4f7f\u7528\uff08\u6216\u9500\u6bc1\uff09\u5305\u542b\u4e86 m_ptr \u7684\u5bf9\u8c61\u65f6\uff0c\u5c31\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u5728\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49\u65f6\uff0c\u786e\u4fdd\u88ab\u79fb\u52a8\u7684\u5bf9\u8c61\u4e5f\u5904\u4e8e\u6709\u6548\u72b6\u6001\u662f\u5f88\u91cd\u8981\u7684\uff0c\u8fd9\u6837\u5b83\u5c31\u53ef\u4ee5\u6b63\u786e\u5730\u9500\u6bc1(\u800c\u4e0d\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u7684\u884c\u4e3a)\u3002 \u81ea\u52a8\u53d8\u91cf\u5de6\u503c\u6309\u503c\u8fd4\u56de\u65f6\u53ef\u80fd\u4f1a\u8fdb\u884c\u79fb\u52a8\u800c\u4e0d\u662f\u62f7\u8d1d \u5728\u4e0a\u9762 Auto_ptr4 \u793a\u4f8b\u7684 generateResource() \u51fd\u6570\u4e2d\uff0c\u5f53\u53d8\u91cf res \u6309\u503c\u8fd4\u56de\u65f6\uff0c\u5b83\u5c06\u88ab\u79fb\u52a8\u800c\u4e0d\u662f\u590d\u5236\uff0c\u5373\u4f7f res \u662f\u4e00\u4e2a\u5de6\u503c\u3002==C++\u89c4\u8303\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u89c4\u5219\uff0c\u5373\u51fd\u6570\u6309\u503c\u8fd4\u56de\u7684\u81ea\u52a8\u5bf9\u8c61\u53ef\u4ee5\u79fb\u52a8\uff0c\u5373\u4f7f\u5b83\u4eec\u662f\u5de6\u503c\u3002==\u8fd9\u662f\u6709\u9053\u7406\u7684\uff0c\u56e0\u4e3a res \u65e0\u8bba\u5982\u4f55\u90fd\u4f1a\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\uff01\u90a3\u4e48\u6211\u4eec\u8fd8\u4e0d\u5982\u7a83\u53d6\u5b83\u7684\u8d44\u6e90\uff0c\u800c\u4e0d\u662f\u6267\u884c\u6602\u8d35\u4e14\u65e0\u7528\u7684\u62f7\u8d1d\u64cd\u4f5c\u3002 \u867d\u7136\u7f16\u8bd1\u5668\u53ef\u4ee5\u79fb\u52a8\u5de6\u503c\u8fd4\u56de\u503c\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u80fd\u591f\u5b8c\u5168\u907f\u514d\u62f7\u8d1d(\u8fd9\u5b8c\u5168\u907f\u514d\u4e86\u590d\u5236\u6216\u79fb\u52a8\u7684\u9700\u8981)\u5c31\u66f4\u597d\u4e86\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u6784\u9020\u51fd\u6570\u751a\u81f3\u90fd\u4e0d\u4f1a\u88ab\u8c03\u7528\u3002 \u963b\u6b62\u62f7\u8d1d \u5728\u4e0a\u9762\u7684 Auto_ptr4 \u7c7b\u4e2d\uff0c\u4e3a\u4e86\u8fdb\u884c\u5bf9\u7167\uff0c\u6211\u4eec\u4fdd\u7559\u4e86\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u64cd\u4f5c\u7b26\u3002 \u4f46\u5728\u652f\u6301\u79fb\u52a8\u8bed\u4e49\u7684\u7c7b\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u5220\u9664\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u51fd\u6570\uff0c\u4ee5\u786e\u4fdd\u4e0d\u4f1a\u6267\u884c\u62f7\u8d1d \u3002\u5728 Auto_ptr \u7c7b\u4e2d\uff0c\u6211\u4eec\u4e0d\u60f3\u62f7\u8d1d\u6a21\u677f\u5316\u5bf9\u8c61T\u2014\u2014\u56e0\u4e3a\u5b83\u7684\u5f00\u9500\u5f88\u5927\uff0c\u800c\u4e14\u7c7bT\u751a\u81f3\u53ef\u80fd\u4e0d\u652f\u6301\u590d\u5236\uff01 \u4e0b\u9762\u662f Auto_ptr \u7684\u4e00\u4e2a\u7248\u672c\uff0c\u5b83\u652f\u6301\u79fb\u52a8\u8bed\u4e49\u4f46\u4e0d\u652f\u6301\u590d\u5236\u8bed\u4e49: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 #include template < typename T > class Auto_ptr5 { T * m_ptr ; public : Auto_ptr5 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } ~ Auto_ptr5 () { delete m_ptr ; } // \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u2014\u2014 \u4e0d\u5141\u8bb8\u62f7\u8d1d! Auto_ptr5 ( const Auto_ptr5 & a ) = delete ; // Move constructor // Transfer ownership of a.m_ptr to m_ptr Auto_ptr5 ( Auto_ptr5 && a ) noexcept : m_ptr ( a . m_ptr ) { a . m_ptr = nullptr ; } // \u62f7\u8d1d\u8d4b\u503c \u2014\u2014 \u4e0d\u5141\u8bb8\u62f7\u8d1d Auto_ptr5 & operator = ( const Auto_ptr5 & a ) = delete ; // Move assignment // Transfer ownership of a.m_ptr to m_ptr Auto_ptr5 & operator = ( Auto_ptr5 && a ) noexcept { // Self-assignment detection if ( & a == this ) return * this ; // Release any resource we're holding delete m_ptr ; // Transfer ownership of a.m_ptr to m_ptr m_ptr = a . m_ptr ; a . m_ptr = nullptr ; return * this ; } T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } bool isNull () const { return m_ptr == nullptr ; } }; \u5982\u679c\u4f60\u5c1d\u8bd5\u5c06\u4e00\u4e2a Auto_ptr5 \u7c7b\u578b\u7684\u5de6\u503c\u6309\u503c\u4f20\u9012\u7ed9\u4e00\u4e2a\u51fd\u6570\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u62b1\u6028\u7528\u4e8e\u521d\u59cb\u5316\u51fd\u6570\u53c2\u6570\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u88ab\u5220\u9664\u4e86\u3002\u8fd9\u662f\u597d\u4e8b\uff0c\u56e0\u4e3a\u6211\u4eec\u5e94\u8be5\u901a\u8fc7\u6309\u5f15\u7528\u4f20\u9012\u7684\u65b9\u5f0f\u4f20\u9012 Auto_ptr5 \uff01 Auto_ptr5 (\u7ec8\u4e8e)\u4e00\u4e2a\u5f88\u597d\u7684\u667a\u80fd\u6307\u9488\u7c7b\u3002\u800c\u4e14\uff0c\u5b9e\u9645\u4e0a\u6807\u51c6\u5e93\u5305\u542b\u4e00\u4e2a\u975e\u5e38\u7c7b\u4f3c\u4e8e\u5b83\u7684\u7c7b\uff0c\u540d\u4e3a std::unique_ptr \u3002\u6211\u4eec\u5c06\u5728\u672c\u7ae0\u540e\u9762\u8ba8\u8bba\u66f4\u591a\u5173\u4e8e std::unique_ptr \u7684\u5185\u5bb9\u3002 \u53e6\u4e00\u4e2a\u4f8b\u5b50 \u8ba9\u6211\u4eec\u6765\u770b\u770b\u53e6\u4e00\u4e2a\u4f7f\u7528\u52a8\u6001\u5185\u5b58\u7684\u7c7b\uff1a\u4e00\u4e2a\u7b80\u5355\u7684\u52a8\u6001\u7684\u6a21\u677f\u5316\u6570\u7ec4\u3002\u8be5\u7c7b\u5305\u542b\u4e00\u4e2a \u6df1\u62f7\u8d1d \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 #include template < typename T > class DynamicArray { private : T * m_array ; int m_length ; public : DynamicArray ( int length ) : m_array ( new T [ length ]), m_length ( length ) { } ~ DynamicArray () { delete [] m_array ; } // Copy constructor DynamicArray ( const DynamicArray & arr ) : m_length ( arr . m_length ) { m_array = new T [ m_length ]; for ( int i = 0 ; i < m_length ; ++ i ) m_array [ i ] = arr . m_array [ i ]; } // Copy assignment DynamicArray & operator = ( const DynamicArray & arr ) { if ( & arr == this ) return * this ; delete [] m_array ; m_length = arr . m_length ; m_array = new T [ m_length ]; for ( int i = 0 ; i < m_length ; ++ i ) m_array [ i ] = arr . m_array [ i ]; return * this ; } int getLength () const { return m_length ; } T & operator []( int index ) { return m_array [ index ]; } const T & operator []( int index ) const { return m_array [ index ]; } }; \u73b0\u5728\u8ba9\u6211\u4eec\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u7c7b\u3002\u4e3a\u4e86\u6f14\u793a\u5728\u5806\u4e0a\u5206\u914d\u4e00\u767e\u4e07\u4e2a\u6574\u6570\u65f6\u8be5\u7c7b\u7684\u6267\u884c\u60c5\u51b5\uff0c\u6211\u4eec\u5c06\u5229\u7528\u5728\u7b2c 13.18 -\u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 \u4e2d\u5f00\u53d1\u7684Timer\u7c7b\u3002\u6211\u4eec\u5c06\u4f7f\u7528Timer\u7c7b\u6765\u5bf9\u4ee3\u7801\u8fd0\u884c\u65f6\u95f4\u8fdb\u884c\u7edf\u8ba1\uff0c\u4ee5\u5c55\u793a\u62f7\u8d1d\u548c\u79fb\u52a8\u4e4b\u95f4\u7684\u6027\u80fd\u5dee\u5f02\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include #include // for std::chrono functions // Uses the above DynamicArray class class Timer { private : // Type aliases to make accessing nested type easier using Clock = std :: chrono :: high_resolution_clock ; using Second = std :: chrono :: duration < double , std :: ratio < 1 > > ; std :: chrono :: time_point < Clock > m_beg { Clock :: now () }; public : void reset () { m_beg = Clock :: now (); } double elapsed () const { return std :: chrono :: duration_cast < Second > ( Clock :: now () - m_beg ). count (); } }; // Return a copy of arr with all of the values doubled DynamicArray < int > cloneArrayAndDouble ( const DynamicArray < int > & arr ) { DynamicArray < int > dbl ( arr . getLength ()); for ( int i = 0 ; i < arr . getLength (); ++ i ) dbl [ i ] = arr [ i ] * 2 ; return dbl ; } int main () { Timer t ; DynamicArray < int > arr ( 1000000 ); for ( int i = 0 ; i < arr . getLength (); i ++ ) arr [ i ] = i ; arr = cloneArrayAndDouble ( arr ); std :: cout << t . elapsed (); } \u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\uff0c\u4ee5release\u6a21\u5f0f\u7f16\u8bd1\uff0c\u7a0b\u5e8f\u6267\u884c\u8017\u65f6 0.00825559 \u79d2\u3002 \u518d\u6b21\u8fd0\u884c\u76f8\u540c\u7684\u7a0b\u5e8f\uff0c\u540c\u65f6\u5c06\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u66ff\u66ff\u6362\u4e3a\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 template < typename T > class DynamicArray { private : T * m_array ; int m_length ; public : DynamicArray ( int length ) : m_array ( new T [ length ]), m_length ( length ) { } ~ DynamicArray () { delete [] m_array ; } // Copy constructor DynamicArray ( const DynamicArray & arr ) = delete ; // Copy assignment DynamicArray & operator = ( const DynamicArray & arr ) = delete ; // Move constructor DynamicArray ( DynamicArray && arr ) noexcept : m_array ( arr . m_array ), m_length ( arr . m_length ) { arr . m_length = 0 ; arr . m_array = nullptr ; } // Move assignment DynamicArray & operator = ( DynamicArray && arr ) noexcept { if ( & arr == this ) return * this ; delete [] m_array ; m_length = arr . m_length ; m_array = arr . m_array ; arr . m_length = 0 ; arr . m_array = nullptr ; return * this ; } int getLength () const { return m_length ; } T & operator []( int index ) { return m_array [ index ]; } const T & operator []( int index ) const { return m_array [ index ]; } }; #include #include // for std::chrono functions class Timer { private : // Type aliases to make accessing nested type easier using Clock = std :: chrono :: high_resolution_clock ; using Second = std :: chrono :: duration < double , std :: ratio < 1 > > ; std :: chrono :: time_point < Clock > m_beg { Clock :: now () }; public : void reset () { m_beg = Clock :: now (); } double elapsed () const { return std :: chrono :: duration_cast < Second > ( Clock :: now () - m_beg ). count (); } }; // Return a copy of arr with all of the values doubled DynamicArray < int > cloneArrayAndDouble ( const DynamicArray < int > & arr ) { DynamicArray < int > dbl ( arr . getLength ()); for ( int i = 0 ; i < arr . getLength (); ++ i ) dbl [ i ] = arr [ i ] * 2 ; return dbl ; } int main () { Timer t ; DynamicArray < int > arr ( 1000000 ); for ( int i = 0 ; i < arr . getLength (); i ++ ) arr [ i ] = i ; arr = cloneArrayAndDouble ( arr ); std :: cout << t . elapsed (); } \u540c\u6837\u7684\u673a\u5668\u4e0a\uff0c\u7a0b\u5e8f\u6267\u884c\u8017\u65f6 0.0056 \u79d2\u3002 \u6bd4\u8f83\u4e24\u4e2a\u7248\u672c\u7684\u8fd0\u884c\u65f6\u95f4\uff0c0.0056 / 0.00825559 = 67.8%\u3002\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\u7684\u7248\u672c\u901f\u5ea6\u5feb\u4e50 47.4% \uff01 \u4e0d\u8981\u4f7f\u7528 std::swap \u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49 \u7531\u4e8e\u79fb\u52a8\u8bed\u4e49\u7684\u76ee\u6807\u662f\u5c06\u8d44\u6e90\u4ece\u6e90\u5bf9\u8c61\u79fb\u52a8\u5230\u76ee\u6807\u5bf9\u8c61\uff0c\u6240\u4ee5\u4f60\u53ef\u80fd\u4f1a\u8003\u8651\u4f7f\u7528 std::swap() \u5b9e\u73b0\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002\u7136\u800c\uff0c\u8fd9\u5e76\u4e0d\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\uff0c\u56e0\u4e3a std::swap() \u4f1a\u5728\u53ef\u79fb\u52a8\u5bf9\u8c61\u4e0a\u8c03\u7528\u79fb\u52a8\u6784\u9020\u51fd\u6570 \u548c \u79fb\u52a8\u8d4b\u503c\uff0c\u8fd9\u5c06\u5bfc\u81f4\u65e0\u9650\u9012\u5f52\u3002\u4f60\u53ef\u4ee5\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u770b\u5230\u8fd9\u79cd\u60c5\u51b5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include #include class Name { private : std :: string m_name ; // std::string is move capable public : Name ( std :: string name ) : m_name { name } { } Name ( Name & name ) = delete ; Name & operator = ( Name & name ) = delete ; Name ( Name && name ) { std :: cout << \"Move ctor \\n \" ; std :: swap ( * this , name ); // bad! } Name & operator = ( Name && name ) { std :: cout << \"Move assign \\n \" ; std :: swap ( * this , name ); // bad! return * this ; } const std :: string & get () { return m_name ; } }; int main () { Name n1 { \"Alex\" }; n1 = Name { \"Joe\" }; // invokes move assignment std :: cout << n1 . get () << '\\n' ; return 0 ; } \u6253\u5370\uff1a 1 2 3 4 5 Move assign Move ctor Move ctor Move ctor Move ctor \u4e00\u76f4\u6253\u5370\u3002\u3002\u3002\u76f4\u5230\u51fd\u6570\u6808\u6ea2\u51fa\u3002 \u4f60\u53ef\u4ee5\u4f7f\u7528\u81ea\u5df1\u7684 swap \u51fd\u6570\u5b9e\u73b0\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\uff0c\u53ea\u8981\u8be5 swap \u51fd\u6570\u4e0d\u8981\u8c03\u7528\u79fb\u52a8\u6784\u9020\u51fd\u6570\u6216\u79fb\u52a8\u8d4b\u503c\u5373\u53ef\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 #include #include class Name { private : std :: string m_name ; public : Name ( std :: string name ) : m_name { name } { } Name ( Name & name ) = delete ; Name & operator = ( Name & name ) = delete ; // Create our own swap friend function to swap the members of Name friend void swap ( Name & a , Name & b ) noexcept { // \u5bf9std::string \u7c7b\u578b\u7684\u6210\u5458\u76f4\u63a5\u4f7f\u7528std:swap // not on Name std :: swap ( a . m_name , b . m_name ); } Name ( Name && name ) { std :: cout << \"Move ctor \\n \" ; swap ( * this , name ); // Now calling our swap, not std::swap } Name & operator = ( Name && name ) { std :: cout << \"Move assign \\n \" ; swap ( * this , name ); // Now calling our swap, not std::swap return * this ; } }; int main () { Name n1 { \"Alex\" }; n1 = Name { \"Joe\" }; // invokes move assignment std :: cout << n1 . get () << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u7b26\u5408\u9884\u671f\uff1a 1 2 Move assign Joe \u5220\u9664\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c \u4f7f\u7528 = delete \u8bed\u6cd5\u5220\u9664\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u7684\u65b9\u6cd5\u4e0e\u5220\u9664\u590d\u5236\u6784\u9020\u51fd\u6570\u548c\u590d\u5236\u8d4b\u503c\u7684\u65b9\u6cd5\u5b8c\u5168\u76f8\u540c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include #include #include class Name { private : std :: string m_name {}; public : Name ( std :: string_view name ) : m_name { name } { } Name ( const Name & name ) = delete ; Name & operator = ( const Name & name ) = delete ; Name ( Name && name ) = delete ; Name & operator = ( Name && name ) = delete ; const std :: string & get () const { return m_name ; } }; int main () { Name n1 { \"Alex\" }; n1 = Name { \"Joe\" }; // error: move assignment deleted std :: cout << n1 . get () << '\\n' ; return 0 ; } \u5982\u679c\u5220\u9664\u590d\u5236\u6784\u9020\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u5c06\u4e0d\u4f1a\u751f\u6210\u9690\u5f0f\u79fb\u52a8\u6784\u9020\u51fd\u6570\uff08\u4f7f\u5bf9\u8c61\u65e2\u4e0d\u53ef\u590d\u5236\u4e5f\u4e0d\u53ef\u79fb\u52a8\uff09\u3002\u56e0\u6b64\uff0c\u5728\u5220\u9664\u590d\u5236\u6784\u9020\u51fd\u6570\u65f6\uff0c\u6700\u597d\u660e\u786e\u8bf4\u660e\u4f60\u5e0c\u671b\u4ece\u79fb\u52a8\u6784\u9020\u51fd\u6570\u4e2d\u5f97\u5230\u4ec0\u4e48\u884c\u4e3a\u3002\u8981\u4e48\u663e\u5f0f\u5220\u9664\u6784\u9020\u51fd\u6570\uff08\u660e\u786e\u8fd9\u662f\u6211\u4eec\u60f3\u8981\u7684\u884c\u4e3a\uff09\uff0c\u8981\u4e48\u9ed8\u8ba4\u5220\u9664\u6784\u9020\u51fd\u6570\uff08\u4f7f\u7c7b\u53ea\u53ef\u79fb\u52a8\uff09\u3002 Key insight \u4e94\u5219\u8fd0\u7b97\u6cd5\u5219 \u6307\u51fa\uff0c\u5982\u679c\u5b9a\u4e49\u6216\u5220\u9664\u4e86\u590d\u5236\u6784\u9020\u51fd\u6570\u3001\u590d\u5236\u8d4b\u503c\u51fd\u6570\u3001\u79fb\u52a8\u6784\u9020\u51fd\u6570\u3001\u79fb\u52a8\u8d4b\u503c\u51fd\u6570\u6216\u6790\u6784\u51fd\u6570\uff0c\u90a3\u4e48\u5c31\u5e94\u8be5\u5b9a\u4e49\u6216\u5220\u9664\u8fd9\u4e9b\u51fd\u6570\u4e2d\u7684> \u6bcf\u4e00\u4e2a\u3002 \u5982\u679c\u4f60\u60f3\u8981\u4e00\u4e2a\u53ef\u590d\u5236\u4f46\u4e0d\u53ef\u79fb\u52a8\u7684\u5bf9\u8c61\uff0c\u90a3\u4e48\u53ea\u5220\u9664 move \u6784\u9020\u51fd\u6570\u548c move \u8d4b\u503c\u4f3c\u4e4e\u662f\u4e2a\u597d\u4e3b\u610f\uff0c\u4f46\u8fd9\u6837\u505a\u7684\u4e0d\u5e78\u540e\u679c\u662f\uff0c\u5728\u5f3a\u5236\u590d\u5236\u6d88\u9664\u4e0d\u9002\u7528\u7684\u60c5\u51b5\u4e0b\uff0c\u8be5\u7c7b\u4e0d\u80fd\u6309\u503c\u8fd4\u56de\u3002\u51fa\u73b0\u8fd9\u79cd\u60c5\u51b5\u7684\u539f\u56e0\u662f\uff0c\u5df2\u5220\u9664\u7684\u79fb\u52a8\u6784\u9020\u51fd\u6570\u4ecd\u88ab\u58f0\u660e\uff0c\u56e0\u6b64\u7b26\u5408\u91cd\u8f7d\u89e3\u6790\u7684\u6761\u4ef6\u3002\u800c\u6309\u503c\u8fd4\u56de\u5c06\u4f7f\u5df2\u5220\u9664\u7684\u79fb\u52a8\u6784\u9020\u51fd\u6570\u4f18\u5148\u4e8e\u672a\u5220\u9664\u7684\u590d\u5236\u6784\u9020\u51fd\u6570\u3002\u4e0b\u9762\u7684\u7a0b\u5e8f\u53ef\u4ee5\u8bf4\u660e\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include #include #include class Name { private : std :: string m_name {}; public : Name ( std :: string_view name ) : m_name { name } { } Name ( const Name & name ) = default ; Name & operator = ( const Name & name ) = default ; Name ( Name && name ) = delete ; Name & operator = ( Name && name ) = delete ; const std :: string & get () const { return m_name ; } }; Name getJoe () { Name joe { \"Joe\" }; return joe ; // error: Move constructor was deleted } int main () { Name n { getJoe () }; std :: cout << n . get () << '\\n' ; return 0 ; }","title":"M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c"},{"location":"M-3-move-constructors-and-move-assignment/#_1","text":"\u9996\u5148\uff0c\u590d\u4e60\u4e00\u4e0b \u62f7\u8d1d\u8bed\u4e49 \u3002 \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u7528\u4e8e\u901a\u8fc7\u62f7\u8d1d\u4e00\u4e2a\u540c\u7c7b\u5bf9\u8c61\u6765\u521d\u59cb\u5316\u53e6\u4e00\u4e2a\u5bf9\u8c61\u3002\u62f7\u8d1d\u8d4b\u503c\u5219\u7528\u4e8e\u5c06\u4e00\u4e2a\u7c7b\u5bf9\u8c61\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u73b0\u6709\u7c7b\u5bf9\u8c61\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u6ca1\u6709\u663e\u5f0f\u63d0\u4f9b\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0cC++\u4f1a\u63d0\u4f9b\u4e00\u4e2a\u9ed8\u8ba4\u7248\u672c\u3002\u8fd9\u4e9b\u7f16\u8bd1\u5668\u63d0\u4f9b\u7684\u51fd\u6570\u4f1a\u6267\u884c \u6d45\u62f7\u8d1d \uff0c\u8fd9\u53ef\u80fd\u4f1a\u7ed9\u5206\u914d\u52a8\u6001\u5185\u5b58\u7684\u7c7b\u5e26\u6765\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u5904\u7406\u52a8\u6001\u5185\u5b58\u7684\u7c7b\u5e94\u8be5\u91cd\u5199\u8fd9\u4e9b\u51fd\u6570\u6765\u8fdb\u884c \u6df1\u62f7\u8d1d \u3002 \u56de\u5230\u672c\u7ae0\u7b2c\u4e00\u8bfe\u4e2d\u7684 Auto_ptr \u667a\u80fd\u6307\u9488\u7c7b\u793a\u4f8b\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u5b9e\u73b0\u4e86\u6df1\u62f7\u8d1d\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u64cd\u4f5c\u7b26\u7684\u7248\u672c\u80fd\u5426\u6b63\u5e38\u5de5\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 #include template < typename T > class Auto_ptr3 { T * m_ptr ; public : Auto_ptr3 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } ~ Auto_ptr3 () { delete m_ptr ; } // \u62f7\u8d1d\u6784\u9020\u51fd\u6570 // \u5c06 a.m_ptr \u6df1\u62f7\u8d1d\u5230 m_ptr Auto_ptr3 ( const Auto_ptr3 & a ) { m_ptr = new T ; * m_ptr = * a . m_ptr ; //\u89e3\u5f15\u7528\u7684\u7ed3\u679c\u662f\u4e00\u4e2aResource\u5bf9\u8c61\uff0c\u5b83\u80fd\u591f\u901a\u8fc7\u9ed8\u8ba4\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u62f7\u8d1d\u8d4b\u503c\u81ea\u5df1 } // \u62f7\u8d1d\u8d4b\u503c // \u5c06 a.m_ptr \u6df1\u62f7\u8d1d\u5230 m_ptr Auto_ptr3 & operator = ( const Auto_ptr3 & a ) { // Self-assignment detection if ( & a == this ) return * this ; // Release any resource we're holding delete m_ptr ; // Copy the resource m_ptr = new T ; * m_ptr = * a . m_ptr ; return * this ; } T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } bool isNull () const { return m_ptr == nullptr ; } }; class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; Auto_ptr3 < Resource > generateResource () { Auto_ptr3 < Resource > res { new Resource }; return res ; // \u8fd4\u56de\u503c\u4f1a\u89e6\u53d1\u62f7\u8d1d\u6784\u9020\u51fd\u6570 } int main () { Auto_ptr3 < Resource > mainres ; mainres = generateResource (); // \u8d4b\u503c\u4f1a\u89e6\u53d1\u62f7\u8d1d\u8d4b\u503c return 0 ; } \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c generateResource() \u51fd\u6570\u4f1a\u521b\u5efa\u4e00\u4e2a\u88ab\u667a\u80fd\u6307\u9488\u5c01\u88c5\u7684\u8d44\u6e90\uff0c\u7136\u540e\u8be5\u8d44\u6e90\u4f1a\u88ab\u8fd4\u56de\u7ed9\u4e3b\u51fd\u6570\u3002\u4e3b\u51fd\u6570\u968f\u540e\u5c06\u5176\u8d4b\u503c\u7ed9 Auto_ptr3 \u5bf9\u8c61\u3002 \u7a0b\u5e8f\u8fd0\u884c\u65f6\u6253\u5370\uff1a 1 2 3 4 5 6 Resource acquired Resource acquired Resource destroyed Resource acquired Resource destroyed Resource destroyed (\u6ce8\u610f:\u5982\u679c\u7f16\u8bd1\u5668\u7701\u7565\u4e86generateResource()\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u4f60\u53ef\u80fd\u53ea\u4f1a\u5f97\u52304\u884c\u8f93\u51fa) \u5982\u6b64\u7b80\u5355\u7684\u7a0b\u5e8f\uff0c\u7adf\u7136\u8981\u521b\u5efa\u5e76\u9500\u6bc1\u8fd9\u4e48\u591a\u6b21\uff1f\u4ec0\u4e48\u60c5\u51b5\uff01 \u4ed4\u7ec6\u7814\u7a76\u4e00\u4e0b\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\u67096\u4e2a\u5173\u952e\u6b65\u9aa4(\u6bcf\u4e2a\u6253\u5370\u4fe1\u606f\u4e00\u4e2a\u6b65\u9aa4)\uff1a \u5728 generateResource() \u4e2d\uff0c\u5c40\u90e8\u53d8\u91cf res \u88ab\u521b\u5efa\uff0c\u540c\u65f6\u88ab\u4e00\u4e2a\u52a8\u6001\u5206\u914d\u7684 Resource \u5bf9\u8c61\u521d\u59cb\u5316\uff0c\u56e0\u6b64\u7b2c\u4e00\u6b21\u6253\u5370\u4e86 \u201cResource acquired\u201d\uff1b Res \u6309\u503c\u8fd4\u56de main() \u3002\u8fd9\u91cc\u4f7f\u7528\u6309\u503c\u8fd4\u56de\u662f\u56e0\u4e3a res \u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u5982\u679c\u6309\u5730\u5740\u8fd4\u56de\u6216\u6309\u5f15\u7528\u8fd4\u56de\u5219\u4f1a\u5e26\u6765\u95ee\u9898\u56e0\u4e3a res \u5728 generateResource() \u51fd\u6570\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u4e86\u3002\u56e0\u6b64\uff0c res \u4f1a\u88ab==\u62f7\u8d1d\u6784\u5efa\u5230\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\u4e2d==\u3002\u56e0\u4e3a\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4f7f\u7528\u4e86\u6df1\u62f7\u8d1d\uff0c\u6240\u4ee5\u5b83\u5206\u914d\u4e86\u4e00\u4e2a\u65b0\u7684 Resource \uff0c\u6240\u4ee5\u7b2c\u4e8c\u6b21\u6253\u5370\u4e86 \u201cResource acquired\u201d\uff1b Res \u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u6700\u521d\u88ab\u521b\u5efa\u7684 Resource \u5bf9\u8c61\u5c31\u9500\u6bc1\u4e86\uff0c\u56e0\u6b64\u7b2c\u4e00\u6253\u5370\u4e86 \u201cResource destroyed\u201d\uff1b \u4e34\u65f6\u5bf9\u4e8e\u901a\u8fc7\u62f7\u8d1d\u8d4b\u503c\u88ab\u8d4b\u503c\u7ed9\u4e86 mainres \u3002\u56e0\u4e3a\u6b64\u5904\u7684\u62f7\u8d1d\u8d4b\u503c\u4f7f\u7528\u4e86\u6df1\u62f7\u8d1d\uff0c\u6240\u4ee5\u5b83\u5206\u914d\u4e86\u4e00\u4e2a\u65b0\u7684 Resource \uff0c\u6240\u4ee5\u7b2c\u4e09\u6b21\u6253\u5370\u4e86 \u201cResource acquired\u201d\uff1b \u5f53\u8d4b\u503c\u8868\u8fbe\u5f0f\u7ed3\u675f\u540e\uff0c\u4e34\u65f6\u5bf9\u8c61\u5c31\u79bb\u5f00\u4e86\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1\uff0c\u56e0\u6b64\u7b2c\u4e8c\u6b21\u6253\u5370\u4e86\u201cResource destroyed\u201d\uff1b \u5728 main() \u7684\u672b\u5c3e\uff0c mainres \u79bb\u5f00\u4e86\u4f5c\u7528\u57df\uff0c\u4e8e\u662f\u6253\u5370\u4e86\u6700\u540e\u4e00\u4e2a \u201cResource destroyed\u201d \u3002 \u7b80\u800c\u8a00\u4e4b\uff0c\u6211\u4eec\u8c03\u7528\u4e86\u4e00\u6b21\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u62f7\u8d1d\u6784\u9020\u4e86 res \u5230\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff0c\u7136\u540e\u8c03\u7528\u4e86\u4e00\u6b21\u62f7\u8d1d\u8d4b\u503c\u5c06\u4e34\u65f6\u5bf9\u8c61\u590d\u5236\u5230 mainres \u5bf9\u8c61\uff0c\u6240\u4ee5\u6700\u7ec8\u603b\u5171\u5206\u914d\u548c\u9500\u6bc1\u4e863\u4e2a\u5355\u72ec\u7684\u5bf9\u8c61\u3002 \u6548\u7387\u5f88\u4f4e\uff0c\u4f46\u81f3\u5c11\u4e0d\u4f1a\u5d29\u6e83\uff01 \u4f46\u662f\uff0c\u5982\u679c\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\u7684\u8bdd\uff0c\u6548\u679c\u4f1a\u66f4\u597d\uff01","title":"\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c"},{"location":"M-3-move-constructors-and-move-assignment/#_2","text":"C++ 11\u4e3a\u4e86\u652f\u6301\u79fb\u52a8\u8bed\u4e49\uff0c\u65b0\u5b9a\u4e49\u4e86\u4e24\u4e2a\u51fd\u6570\uff1a\u4e00\u4e2a\u662f \u79fb\u52a8\u6784\u9020\u51fd\u6570 \uff0c\u4e00\u4e2a\u662f \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 \u3002\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u7684\u76ee\u7684\u662f\u5c06\u4e00\u4e2a\u5bf9\u8c61\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u5bf9\u8c61\uff0c\u800c\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u7684\u76ee\u7684\u662f\u5c06\u8d44\u6e90\u7684\u6240\u6709\u6743\u4ece\u4e00\u4e2a\u5bf9\u8c61\u8f6c\u79fb\u5230\u53e6\u4e00\u4e2a\u5bf9\u8c61(\u8fd9\u901a\u5e38\u6bd4\u590d\u5236\u6210\u672c\u4f4e\u5f97\u591a)\u3002 \u5b9a\u4e49\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\uff0c\u548c\u5b9a\u4e49\u5b83\u4eec\u7684\u201c\u62f7\u8d1d\u8bed\u4e49\u201d\u540c\u50da\u7c7b\u4f3c\u3002\u4e0d\u8fc7\uff0c \u5b9e\u73b0\u62f7\u8d1d\u8bed\u4e49\u65f6\u9700\u8981\u4f7f\u7528const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528\u4f5c\u4e3a\u5f62\u53c2\uff0c\u800c\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49\u65f6\uff0c\u9700\u8981\u4f7f\u7528\u975econst\u7684\u53f3\u503c\u5f62\u53c2\u3002 \u8fd9\u91cc\u662f\u4e0e\u4e0a\u9762\u76f8\u540c\u7684 Auto_ptr3 \u7c7b\uff0c\u6dfb\u52a0\u4e86\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002\u4e3a\u4e86\u8fdb\u884c\u6bd4\u8f83\uff0c\u6211\u4eec\u4fdd\u7559\u4e86\u6df1\u62f7\u8d1d\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 #include template < typename T > class Auto_ptr4 { T * m_ptr ; public : Auto_ptr4 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } ~ Auto_ptr4 () { delete m_ptr ; } // \u62f7\u8d1d\u6784\u9020\u51fd\u6570 // \u5c06 a.m_ptr \u6df1\u62f7\u8d1d\u5230 m_ptr Auto_ptr4 ( const Auto_ptr4 & a ) { m_ptr = new T ; * m_ptr = * a . m_ptr ; } // \u79fb\u52a8\u6784\u9020\u51fd\u6570 // \u8f6c\u79fb a.m_ptr \u7684\u8d44\u6e90\u7ed9 m_ptr Auto_ptr4 ( Auto_ptr4 && a ) noexcept //\u6ce8\u610f\u4f7f\u7528\u7684\u662f\u53f3\u503c\u5f15\u7528 : m_ptr ( a . m_ptr ) { a . m_ptr = nullptr ; // we'll talk more about this line below } // \u62f7\u8d1d\u8d4b\u503c // \u5c06 a.m_ptr \u6df1\u62f7\u8d1d\u5230 m_ptr Auto_ptr4 & operator = ( const Auto_ptr4 & a ) { // Self-assignment detection if ( & a == this ) return * this ; // Release any resource we're holding delete m_ptr ; // Copy the resource m_ptr = new T ; * m_ptr = * a . m_ptr ; return * this ; } // \u79fb\u52a8\u8d4b\u503c // \u8f6c\u79fb a.m_ptr \u7684\u8d44\u6e90\u7ed9 m_ptr Auto_ptr4 & operator = ( Auto_ptr4 && a ) noexcept { // Self-assignment detection if ( & a == this ) return * this ; // \u91ca\u653e\u5f53\u524d\u6301\u6709\u7684\u8d44\u6e90 delete m_ptr ; // \u8f6c\u79fb\u6240\u6709\u6743 m_ptr = a . m_ptr ; a . m_ptr = nullptr ; // we'll talk more about this line below return * this ; } T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } bool isNull () const { return m_ptr == nullptr ; } }; class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; Auto_ptr4 < Resource > generateResource () { Auto_ptr4 < Resource > res { new Resource }; return res ; // \u6b64\u5904\u4f1a\u8c03\u7528\u79fb\u52a8\u6784\u9020\u51fd\u6570 } int main () { Auto_ptr4 < Resource > mainres ; mainres = generateResource (); // \u6b64\u5904\u8d4b\u503c\u4f1a\u8c03\u7528\u79fb\u52a8\u8d4b\u503c return 0 ; } \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26\u5e76\u4e0d\u590d\u6742\u3002\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4e0d\u518d\u5c06\u6e90\u5bf9\u8c61( a )\u6df1\u62f7\u8d1d\u5230\u9690\u5f0f\u5bf9\u8c61\u4e2d\uff0c\u800c\u662f\u8f6c\u79fb\u4e86\u6e90\u5bf9\u8c61\u7684\u8d44\u6e90\u3002\u8fd9\u6d89\u53ca\u5230\u5c06\u6e90\u6307\u9488\u6d45\u62f7\u8d1d\u5230\u9690\u5f0f\u5bf9\u8c61\u4e2d\uff0c\u7136\u540e\u5c06\u6e90\u6307\u9488\u8bbe\u7f6e\u4e3a\u7a7a\u3002 \u7a0b\u5e8f\u8fd0\u884c\u8f93\u51fa\uff1a 1 2 Resource acquired Resource destroyed \u6548\u679c\u597d\u6781\u4e86\uff01 \u7a0b\u5e8f\u7684\u6d41\u7a0b\u548c\u4e4b\u524d\u7684\u5b8c\u5168\u4e00\u6837\u3002\u4f46\u662f\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\u8c03\u7528\u7684\u4e0d\u662f\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u800c\u662f\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002\u518d\u6df1\u5165\u4e00\u70b9\u770b\uff1a \u5728 generateResource() \uff0c\u5c40\u90e8\u53d8\u91cf res \u88ab\u521b\u5efa\uff0c\u540c\u65f6\u88ab\u4e00\u4e2a\u52a8\u6001\u5206\u914d\u7684 Resource \u5bf9\u8c61\u521d\u59cb\u5316\uff0c\u56e0\u6b64\u7b2c\u4e00\u6b21\u6253\u5370\u4e86 \u201cResource acquired\u201d\uff1b Res \u6309\u503c\u8fd4\u56de main() \u3002 res \u4f1a\u88ab==\u79fb\u52a8\u6784\u9020\u5230\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\u4e2d==\uff0c\u7136\u540e\u5c06\u5b58\u653e\u5728 res \u52a8\u6001\u521b\u5efa\u7684\u5bf9\u8c61\u7684\u6240\u6709\u6743\u8f6c\u79fb\u7ed9\u4e86\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff1b Res \u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u4f46\u662f\u7531\u4e8e res \u5df2\u7ecf\u4e0d\u7ba1\u7406\u6307\u9488\u4e86\uff08\u5df2\u7ecf\u8f6c\u79fb\u7ed9\u4e86\u4e34\u65f6\u5bf9\u8c61\uff09\uff0c\u6240\u4ee5\u4ec0\u4e48\u4e8b\u60c5\u90fd\u4e0d\u4f1a\u53d1\u751f\uff1b \u4e34\u65f6\u5bf9\u8c61\u88ab\u79fb\u52a8\u8d4b\u503c\u7ed9 mainres \uff0c\u6b64\u65f6\u5b58\u653e\u5728\u4e34\u65f6\u5bf9\u8c61\u4e2d\u7684\u52a8\u6001\u521b\u5efa\u7684\u8d44\u6e90\u5c31\u88ab\u8f6c\u79fb\u7ed9\u4e86 mainres \uff1b \u5f53\u8d4b\u503c\u8868\u8fbe\u5f0f\u7ed3\u675f\u65f6\uff0c\u4e34\u65f6\u5bf9\u8c61\u79bb\u5f00\u4f5c\u7528\u57df\u5e76\u88ab\u9500\u6bc1\uff0c\u4f46\u662f\u7531\u4e8e\u5b83\u5df2\u7ecf\u4e0d\u518d\u7ba1\u7406\u4efb\u4f55\u6307\u9488\uff08\u5df2\u7ecf\u8f6c\u79fb\u7ed9\u4e86 mainres \uff09\uff0c\u6240\u4ee5\u4ecd\u7136\u4ec0\u4e48\u90fd\u4e0d\u4f1a\u53d1\u751f\u3002 \u5728 main() \u7ed3\u675f\u7684\u5730\u65b9\uff0c mainres \u4f1a\u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u6240\u4ee5\u6253\u5370\u4e86 \u201cResource destroyed\u201d \u3002 \u56e0\u6b64\uff0c\u8fd9\u6b21\uff0c Resource \u5e76\u6ca1\u6709\u88ab\u590d\u5236\u4e24\u6b21(\u4e00\u6b21\u662f\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u4e00\u6b21\u662f\u62f7\u8d1d\u8d4b\u503c)\uff0c\u800c\u662f\u5c06\u5176\u8f6c\u79fb\u4e86\u4e24\u6b21\u3002\u8fd9\u4e48\u505a\u6548\u7387\u660e\u663e\u66f4\u9ad8\uff0c\u56e0\u4e3a\u8d44\u6e90\u53ea\u6784\u5efa\u548c\u6467\u6bc1\u4e00\u6b21\uff0c\u800c\u4e0d\u662f\u4e09\u6b21\u3002","title":"\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c"},{"location":"M-3-move-constructors-and-move-assignment/#_3","text":"\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u5728\u5b9a\u4e49\u540e\u624d\u4f1a\u88ab\u8c03\u7528\uff0c\u800c\u4e14\u7528\u4e8e\u6784\u9020\u6216\u8d4b\u503c\u7684\u53c2\u6570\u9700\u8981\u662f\u4e00\u4e2a\u53f3\u503c\u3002\u6700\u5178\u578b\u7684\u662f\uff0c\u8fd9\u4e2a\u53f3\u503c\u662f\u4e00\u4e2a\u5b57\u9762\u91cf\u6216\u4e34\u65f6\u503c\u3002 \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c \u7f16\u8bd1\u5668\u4e0d\u4f1a\u63d0\u4f9b\u9ed8\u8ba4\u7684\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u9664\u975e\u8be5\u7c7b\u6ca1\u6709\u5b9a\u4e49\u4efb\u4f55\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3001\u62f7\u8d1d\u8d4b\u503c\u3001\u79fb\u52a8\u8d4b\u503c\u6216\u6790\u6784\u51fd\u6570 \u3002","title":"\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u4f1a\u5728\u4f55\u65f6\u88ab\u8c03\u7528\uff1f"},{"location":"M-3-move-constructors-and-move-assignment/#_4","text":"\u73b0\u5728\u4f60\u5df2\u7ecf\u5177\u6709\u7406\u89e3\u79fb\u52a8\u8bed\u4e49\u80cc\u540e\u5173\u952e\u4fe1\u606f\u7684\u80cc\u666f\u77e5\u8bc6\u4e86\u3002 \u5f53\u4f7f\u7528\u4e00\u4e2a \u5de6\u503c \u5b9e\u53c2\u6784\u9020\u4e00\u4e2a\u5bf9\u8c61\u6216\u8fdb\u884c\u8d4b\u503c\u65f6\uff0c\u6211\u4eec\u552f\u4e00\u80fd\u505a\u7684\u5408\u7406\u7684\u4e8b\u60c5\u5c31\u662f\u590d\u5236\u8fd9\u4e2a\u5de6\u503c\u3002\u6211\u4eec\u4e0d\u80fd\u5047\u8bbe\u6539\u53d8\u53ef\u4ee5\u5b89\u5168\u5730\u6539\u53d8\u8fd9\u4e2a\u5de6\u503c\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u5728\u7a0d\u540e\u7684\u7a0b\u5e8f\u4e2d\u518d\u6b21\u88ab\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u8868\u8fbe\u5f0f a = b \uff0c\u6211\u4eec\u4e0d\u80fd\u671f\u671b\u6709\u4efb\u4f55\u5408\u7406\u7684\u65b9\u5f0f\u53ef\u4ee5\u7528\u6765\u4fee\u6539b\u7684\u503c\u3002 \u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a \u53f3\u503c \u6765\u6784\u9020\u4e00\u4e2a\u5bf9\u8c61\u6216\u8d4b\u503c\uff0c\u7531\u4e8e\u53f3\u503c\u53ea\u662f\u67d0\u79cd\u7c7b\u578b\u7684\u4e34\u65f6\u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4e0d\u5fc5\u590d\u5236\u5b83(\u590d\u5236\u5f00\u9500\u5f88\u5927)\uff0c\u800c\u662f\u53ef\u4ee5\u7b80\u5355\u5730\u5c06\u5b83\u7ba1\u7406\u7684\u8d44\u6e90\u8f6c\u79fb\u5230\u6b63\u5728\u6784\u9020\u6216\u5206\u914d\u7684\u5bf9\u8c61\u5373\u53ef\u3002\u8fd9\u6837\u505a\u662f\u5b89\u5168\u7684\uff0c\u56e0\u4e3a\u4e34\u65f6\u53d8\u91cf\u65e0\u8bba\u5982\u4f55\u90fd\u4f1a\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u3002\u663e\u7136\uff0c\u5b83\u6c38\u8fdc\u4e0d\u4f1a\u518d\u88ab\u4f7f\u7528! c++ 11\u901a\u8fc7 \u53f3\u503c\u5f15\u7528 \uff0c\u4f7f\u6211\u4eec\u80fd\u591f\u5728\u5b9e\u53c2\u662f\u53f3\u503c\u548c\u5de6\u503c\u65f6\u9009\u62e9\u4e0d\u540c\u7684\u884c\u4e3a\uff0c\u4ee5\u4fbf\u8bbe\u8ba1\u51fa\u66f4\u806a\u660e\u3001\u66f4\u6709\u6548\u7684\u5bf9\u8c61\u884c\u4e3a\u3002","title":"\u79fb\u52a8\u8bed\u4e49\u80cc\u540e\u7684\u5173\u952e\u4fe1\u606f"},{"location":"M-3-move-constructors-and-move-assignment/#_5","text":"\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4e0d\u8bba\u662f\u79fb\u52a8\u6784\u9020\u51fd\u6570\u8fd8\u79fb\u52a8\u8d4b\u503c\u51fd\u6570\uff0c\u90fd\u4f1a\u5c06 a.m_ptr \u8bbe\u7f6e\u4e3a nullptr \uff08\u7a7a\u6307\u9488\u5b57\u9762\u91cf\uff09\u3002\u8fd9\u4e48\u505a\u770b\u4e0a\u53bb\u6709\u70b9\u591a\u4f59\u2014\u2014\u6bd5\u7adf\u8fd9\u53ea\u662f\u4e00\u4e2a\u4e34\u65f6\u7684\u53f3\u503c\u3002\u65e2\u7136\u5b83\u7a0d\u540e\u5c31\u4f1a\u88ab\u9500\u6bc1\uff0c\u4e3a\u5565\u8fd8\u8981\u624b\u5de5\u201c\u6e05\u7406\u201d\u5b83\u5462\uff1f \u7b54\u6848\u5f88\u7b80\u5355\uff1a\u5f53 a \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c\u6790\u6784\u51fd\u6570\u4f1a\u88ab\u8c03\u7528\uff0c\u6240\u4ee5 a.m_ptr \u4f1a\u88ab\u5220\u9664\u3002\u5728\u64cd\u4f5c\u6267\u884c\u65f6\uff0c\u5982\u679c a.m_ptr \u4ecd\u7136\u6307\u5411\u4e0e m_ptr \u6307\u5411\u76f8\u540c\u7684\u5bf9\u8c61\uff0c\u5219 m_ptr \u663e\u7136\u4f1a\u53d8\u6210\u4e00\u4e2a \u60ac\u5782 \u6307\u9488\u3002\u5f53\u6211\u4eec\u540e\u9762\u4f7f\u7528\uff08\u6216\u9500\u6bc1\uff09\u5305\u542b\u4e86 m_ptr \u7684\u5bf9\u8c61\u65f6\uff0c\u5c31\u4f1a\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u5728\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49\u65f6\uff0c\u786e\u4fdd\u88ab\u79fb\u52a8\u7684\u5bf9\u8c61\u4e5f\u5904\u4e8e\u6709\u6548\u72b6\u6001\u662f\u5f88\u91cd\u8981\u7684\uff0c\u8fd9\u6837\u5b83\u5c31\u53ef\u4ee5\u6b63\u786e\u5730\u9500\u6bc1(\u800c\u4e0d\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u7684\u884c\u4e3a)\u3002","title":"\u79fb\u52a8\u51fd\u6570\u5e94\u8be5\u4fdd\u6301\u4e24\u4e2a\u5bf9\u8c61\u90fd\u5904\u4e8e\u6709\u6548\u72b6\u6001"},{"location":"M-3-move-constructors-and-move-assignment/#_6","text":"\u5728\u4e0a\u9762 Auto_ptr4 \u793a\u4f8b\u7684 generateResource() \u51fd\u6570\u4e2d\uff0c\u5f53\u53d8\u91cf res \u6309\u503c\u8fd4\u56de\u65f6\uff0c\u5b83\u5c06\u88ab\u79fb\u52a8\u800c\u4e0d\u662f\u590d\u5236\uff0c\u5373\u4f7f res \u662f\u4e00\u4e2a\u5de6\u503c\u3002==C++\u89c4\u8303\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u89c4\u5219\uff0c\u5373\u51fd\u6570\u6309\u503c\u8fd4\u56de\u7684\u81ea\u52a8\u5bf9\u8c61\u53ef\u4ee5\u79fb\u52a8\uff0c\u5373\u4f7f\u5b83\u4eec\u662f\u5de6\u503c\u3002==\u8fd9\u662f\u6709\u9053\u7406\u7684\uff0c\u56e0\u4e3a res \u65e0\u8bba\u5982\u4f55\u90fd\u4f1a\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\uff01\u90a3\u4e48\u6211\u4eec\u8fd8\u4e0d\u5982\u7a83\u53d6\u5b83\u7684\u8d44\u6e90\uff0c\u800c\u4e0d\u662f\u6267\u884c\u6602\u8d35\u4e14\u65e0\u7528\u7684\u62f7\u8d1d\u64cd\u4f5c\u3002 \u867d\u7136\u7f16\u8bd1\u5668\u53ef\u4ee5\u79fb\u52a8\u5de6\u503c\u8fd4\u56de\u503c\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u80fd\u591f\u5b8c\u5168\u907f\u514d\u62f7\u8d1d(\u8fd9\u5b8c\u5168\u907f\u514d\u4e86\u590d\u5236\u6216\u79fb\u52a8\u7684\u9700\u8981)\u5c31\u66f4\u597d\u4e86\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u6784\u9020\u51fd\u6570\u751a\u81f3\u90fd\u4e0d\u4f1a\u88ab\u8c03\u7528\u3002","title":"\u81ea\u52a8\u53d8\u91cf\u5de6\u503c\u6309\u503c\u8fd4\u56de\u65f6\u53ef\u80fd\u4f1a\u8fdb\u884c\u79fb\u52a8\u800c\u4e0d\u662f\u62f7\u8d1d"},{"location":"M-3-move-constructors-and-move-assignment/#_7","text":"\u5728\u4e0a\u9762\u7684 Auto_ptr4 \u7c7b\u4e2d\uff0c\u4e3a\u4e86\u8fdb\u884c\u5bf9\u7167\uff0c\u6211\u4eec\u4fdd\u7559\u4e86\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u64cd\u4f5c\u7b26\u3002 \u4f46\u5728\u652f\u6301\u79fb\u52a8\u8bed\u4e49\u7684\u7c7b\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u5220\u9664\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u51fd\u6570\uff0c\u4ee5\u786e\u4fdd\u4e0d\u4f1a\u6267\u884c\u62f7\u8d1d \u3002\u5728 Auto_ptr \u7c7b\u4e2d\uff0c\u6211\u4eec\u4e0d\u60f3\u62f7\u8d1d\u6a21\u677f\u5316\u5bf9\u8c61T\u2014\u2014\u56e0\u4e3a\u5b83\u7684\u5f00\u9500\u5f88\u5927\uff0c\u800c\u4e14\u7c7bT\u751a\u81f3\u53ef\u80fd\u4e0d\u652f\u6301\u590d\u5236\uff01 \u4e0b\u9762\u662f Auto_ptr \u7684\u4e00\u4e2a\u7248\u672c\uff0c\u5b83\u652f\u6301\u79fb\u52a8\u8bed\u4e49\u4f46\u4e0d\u652f\u6301\u590d\u5236\u8bed\u4e49: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 #include template < typename T > class Auto_ptr5 { T * m_ptr ; public : Auto_ptr5 ( T * ptr = nullptr ) : m_ptr ( ptr ) { } ~ Auto_ptr5 () { delete m_ptr ; } // \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u2014\u2014 \u4e0d\u5141\u8bb8\u62f7\u8d1d! Auto_ptr5 ( const Auto_ptr5 & a ) = delete ; // Move constructor // Transfer ownership of a.m_ptr to m_ptr Auto_ptr5 ( Auto_ptr5 && a ) noexcept : m_ptr ( a . m_ptr ) { a . m_ptr = nullptr ; } // \u62f7\u8d1d\u8d4b\u503c \u2014\u2014 \u4e0d\u5141\u8bb8\u62f7\u8d1d Auto_ptr5 & operator = ( const Auto_ptr5 & a ) = delete ; // Move assignment // Transfer ownership of a.m_ptr to m_ptr Auto_ptr5 & operator = ( Auto_ptr5 && a ) noexcept { // Self-assignment detection if ( & a == this ) return * this ; // Release any resource we're holding delete m_ptr ; // Transfer ownership of a.m_ptr to m_ptr m_ptr = a . m_ptr ; a . m_ptr = nullptr ; return * this ; } T & operator * () const { return * m_ptr ; } T * operator -> () const { return m_ptr ; } bool isNull () const { return m_ptr == nullptr ; } }; \u5982\u679c\u4f60\u5c1d\u8bd5\u5c06\u4e00\u4e2a Auto_ptr5 \u7c7b\u578b\u7684\u5de6\u503c\u6309\u503c\u4f20\u9012\u7ed9\u4e00\u4e2a\u51fd\u6570\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u62b1\u6028\u7528\u4e8e\u521d\u59cb\u5316\u51fd\u6570\u53c2\u6570\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u88ab\u5220\u9664\u4e86\u3002\u8fd9\u662f\u597d\u4e8b\uff0c\u56e0\u4e3a\u6211\u4eec\u5e94\u8be5\u901a\u8fc7\u6309\u5f15\u7528\u4f20\u9012\u7684\u65b9\u5f0f\u4f20\u9012 Auto_ptr5 \uff01 Auto_ptr5 (\u7ec8\u4e8e)\u4e00\u4e2a\u5f88\u597d\u7684\u667a\u80fd\u6307\u9488\u7c7b\u3002\u800c\u4e14\uff0c\u5b9e\u9645\u4e0a\u6807\u51c6\u5e93\u5305\u542b\u4e00\u4e2a\u975e\u5e38\u7c7b\u4f3c\u4e8e\u5b83\u7684\u7c7b\uff0c\u540d\u4e3a std::unique_ptr \u3002\u6211\u4eec\u5c06\u5728\u672c\u7ae0\u540e\u9762\u8ba8\u8bba\u66f4\u591a\u5173\u4e8e std::unique_ptr \u7684\u5185\u5bb9\u3002","title":"\u963b\u6b62\u62f7\u8d1d"},{"location":"M-3-move-constructors-and-move-assignment/#_8","text":"\u8ba9\u6211\u4eec\u6765\u770b\u770b\u53e6\u4e00\u4e2a\u4f7f\u7528\u52a8\u6001\u5185\u5b58\u7684\u7c7b\uff1a\u4e00\u4e2a\u7b80\u5355\u7684\u52a8\u6001\u7684\u6a21\u677f\u5316\u6570\u7ec4\u3002\u8be5\u7c7b\u5305\u542b\u4e00\u4e2a \u6df1\u62f7\u8d1d \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 #include template < typename T > class DynamicArray { private : T * m_array ; int m_length ; public : DynamicArray ( int length ) : m_array ( new T [ length ]), m_length ( length ) { } ~ DynamicArray () { delete [] m_array ; } // Copy constructor DynamicArray ( const DynamicArray & arr ) : m_length ( arr . m_length ) { m_array = new T [ m_length ]; for ( int i = 0 ; i < m_length ; ++ i ) m_array [ i ] = arr . m_array [ i ]; } // Copy assignment DynamicArray & operator = ( const DynamicArray & arr ) { if ( & arr == this ) return * this ; delete [] m_array ; m_length = arr . m_length ; m_array = new T [ m_length ]; for ( int i = 0 ; i < m_length ; ++ i ) m_array [ i ] = arr . m_array [ i ]; return * this ; } int getLength () const { return m_length ; } T & operator []( int index ) { return m_array [ index ]; } const T & operator []( int index ) const { return m_array [ index ]; } }; \u73b0\u5728\u8ba9\u6211\u4eec\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u7c7b\u3002\u4e3a\u4e86\u6f14\u793a\u5728\u5806\u4e0a\u5206\u914d\u4e00\u767e\u4e07\u4e2a\u6574\u6570\u65f6\u8be5\u7c7b\u7684\u6267\u884c\u60c5\u51b5\uff0c\u6211\u4eec\u5c06\u5229\u7528\u5728\u7b2c 13.18 -\u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 \u4e2d\u5f00\u53d1\u7684Timer\u7c7b\u3002\u6211\u4eec\u5c06\u4f7f\u7528Timer\u7c7b\u6765\u5bf9\u4ee3\u7801\u8fd0\u884c\u65f6\u95f4\u8fdb\u884c\u7edf\u8ba1\uff0c\u4ee5\u5c55\u793a\u62f7\u8d1d\u548c\u79fb\u52a8\u4e4b\u95f4\u7684\u6027\u80fd\u5dee\u5f02\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include #include // for std::chrono functions // Uses the above DynamicArray class class Timer { private : // Type aliases to make accessing nested type easier using Clock = std :: chrono :: high_resolution_clock ; using Second = std :: chrono :: duration < double , std :: ratio < 1 > > ; std :: chrono :: time_point < Clock > m_beg { Clock :: now () }; public : void reset () { m_beg = Clock :: now (); } double elapsed () const { return std :: chrono :: duration_cast < Second > ( Clock :: now () - m_beg ). count (); } }; // Return a copy of arr with all of the values doubled DynamicArray < int > cloneArrayAndDouble ( const DynamicArray < int > & arr ) { DynamicArray < int > dbl ( arr . getLength ()); for ( int i = 0 ; i < arr . getLength (); ++ i ) dbl [ i ] = arr [ i ] * 2 ; return dbl ; } int main () { Timer t ; DynamicArray < int > arr ( 1000000 ); for ( int i = 0 ; i < arr . getLength (); i ++ ) arr [ i ] = i ; arr = cloneArrayAndDouble ( arr ); std :: cout << t . elapsed (); } \u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\uff0c\u4ee5release\u6a21\u5f0f\u7f16\u8bd1\uff0c\u7a0b\u5e8f\u6267\u884c\u8017\u65f6 0.00825559 \u79d2\u3002 \u518d\u6b21\u8fd0\u884c\u76f8\u540c\u7684\u7a0b\u5e8f\uff0c\u540c\u65f6\u5c06\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u66ff\u66ff\u6362\u4e3a\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 template < typename T > class DynamicArray { private : T * m_array ; int m_length ; public : DynamicArray ( int length ) : m_array ( new T [ length ]), m_length ( length ) { } ~ DynamicArray () { delete [] m_array ; } // Copy constructor DynamicArray ( const DynamicArray & arr ) = delete ; // Copy assignment DynamicArray & operator = ( const DynamicArray & arr ) = delete ; // Move constructor DynamicArray ( DynamicArray && arr ) noexcept : m_array ( arr . m_array ), m_length ( arr . m_length ) { arr . m_length = 0 ; arr . m_array = nullptr ; } // Move assignment DynamicArray & operator = ( DynamicArray && arr ) noexcept { if ( & arr == this ) return * this ; delete [] m_array ; m_length = arr . m_length ; m_array = arr . m_array ; arr . m_length = 0 ; arr . m_array = nullptr ; return * this ; } int getLength () const { return m_length ; } T & operator []( int index ) { return m_array [ index ]; } const T & operator []( int index ) const { return m_array [ index ]; } }; #include #include // for std::chrono functions class Timer { private : // Type aliases to make accessing nested type easier using Clock = std :: chrono :: high_resolution_clock ; using Second = std :: chrono :: duration < double , std :: ratio < 1 > > ; std :: chrono :: time_point < Clock > m_beg { Clock :: now () }; public : void reset () { m_beg = Clock :: now (); } double elapsed () const { return std :: chrono :: duration_cast < Second > ( Clock :: now () - m_beg ). count (); } }; // Return a copy of arr with all of the values doubled DynamicArray < int > cloneArrayAndDouble ( const DynamicArray < int > & arr ) { DynamicArray < int > dbl ( arr . getLength ()); for ( int i = 0 ; i < arr . getLength (); ++ i ) dbl [ i ] = arr [ i ] * 2 ; return dbl ; } int main () { Timer t ; DynamicArray < int > arr ( 1000000 ); for ( int i = 0 ; i < arr . getLength (); i ++ ) arr [ i ] = i ; arr = cloneArrayAndDouble ( arr ); std :: cout << t . elapsed (); } \u540c\u6837\u7684\u673a\u5668\u4e0a\uff0c\u7a0b\u5e8f\u6267\u884c\u8017\u65f6 0.0056 \u79d2\u3002 \u6bd4\u8f83\u4e24\u4e2a\u7248\u672c\u7684\u8fd0\u884c\u65f6\u95f4\uff0c0.0056 / 0.00825559 = 67.8%\u3002\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\u7684\u7248\u672c\u901f\u5ea6\u5feb\u4e50 47.4% \uff01","title":"\u53e6\u4e00\u4e2a\u4f8b\u5b50"},{"location":"M-3-move-constructors-and-move-assignment/#stdswap","text":"\u7531\u4e8e\u79fb\u52a8\u8bed\u4e49\u7684\u76ee\u6807\u662f\u5c06\u8d44\u6e90\u4ece\u6e90\u5bf9\u8c61\u79fb\u52a8\u5230\u76ee\u6807\u5bf9\u8c61\uff0c\u6240\u4ee5\u4f60\u53ef\u80fd\u4f1a\u8003\u8651\u4f7f\u7528 std::swap() \u5b9e\u73b0\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002\u7136\u800c\uff0c\u8fd9\u5e76\u4e0d\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\uff0c\u56e0\u4e3a std::swap() \u4f1a\u5728\u53ef\u79fb\u52a8\u5bf9\u8c61\u4e0a\u8c03\u7528\u79fb\u52a8\u6784\u9020\u51fd\u6570 \u548c \u79fb\u52a8\u8d4b\u503c\uff0c\u8fd9\u5c06\u5bfc\u81f4\u65e0\u9650\u9012\u5f52\u3002\u4f60\u53ef\u4ee5\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u770b\u5230\u8fd9\u79cd\u60c5\u51b5: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include #include class Name { private : std :: string m_name ; // std::string is move capable public : Name ( std :: string name ) : m_name { name } { } Name ( Name & name ) = delete ; Name & operator = ( Name & name ) = delete ; Name ( Name && name ) { std :: cout << \"Move ctor \\n \" ; std :: swap ( * this , name ); // bad! } Name & operator = ( Name && name ) { std :: cout << \"Move assign \\n \" ; std :: swap ( * this , name ); // bad! return * this ; } const std :: string & get () { return m_name ; } }; int main () { Name n1 { \"Alex\" }; n1 = Name { \"Joe\" }; // invokes move assignment std :: cout << n1 . get () << '\\n' ; return 0 ; } \u6253\u5370\uff1a 1 2 3 4 5 Move assign Move ctor Move ctor Move ctor Move ctor \u4e00\u76f4\u6253\u5370\u3002\u3002\u3002\u76f4\u5230\u51fd\u6570\u6808\u6ea2\u51fa\u3002 \u4f60\u53ef\u4ee5\u4f7f\u7528\u81ea\u5df1\u7684 swap \u51fd\u6570\u5b9e\u73b0\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\uff0c\u53ea\u8981\u8be5 swap \u51fd\u6570\u4e0d\u8981\u8c03\u7528\u79fb\u52a8\u6784\u9020\u51fd\u6570\u6216\u79fb\u52a8\u8d4b\u503c\u5373\u53ef\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9\u7684\u4f8b\u5b50: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 #include #include class Name { private : std :: string m_name ; public : Name ( std :: string name ) : m_name { name } { } Name ( Name & name ) = delete ; Name & operator = ( Name & name ) = delete ; // Create our own swap friend function to swap the members of Name friend void swap ( Name & a , Name & b ) noexcept { // \u5bf9std::string \u7c7b\u578b\u7684\u6210\u5458\u76f4\u63a5\u4f7f\u7528std:swap // not on Name std :: swap ( a . m_name , b . m_name ); } Name ( Name && name ) { std :: cout << \"Move ctor \\n \" ; swap ( * this , name ); // Now calling our swap, not std::swap } Name & operator = ( Name && name ) { std :: cout << \"Move assign \\n \" ; swap ( * this , name ); // Now calling our swap, not std::swap return * this ; } }; int main () { Name n1 { \"Alex\" }; n1 = Name { \"Joe\" }; // invokes move assignment std :: cout << n1 . get () << '\\n' ; return 0 ; } \u8fd0\u884c\u7ed3\u679c\u7b26\u5408\u9884\u671f\uff1a 1 2 Move assign Joe","title":"\u4e0d\u8981\u4f7f\u7528 std::swap \u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49"},{"location":"M-3-move-constructors-and-move-assignment/#_9","text":"\u4f7f\u7528 = delete \u8bed\u6cd5\u5220\u9664\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u7684\u65b9\u6cd5\u4e0e\u5220\u9664\u590d\u5236\u6784\u9020\u51fd\u6570\u548c\u590d\u5236\u8d4b\u503c\u7684\u65b9\u6cd5\u5b8c\u5168\u76f8\u540c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include #include #include class Name { private : std :: string m_name {}; public : Name ( std :: string_view name ) : m_name { name } { } Name ( const Name & name ) = delete ; Name & operator = ( const Name & name ) = delete ; Name ( Name && name ) = delete ; Name & operator = ( Name && name ) = delete ; const std :: string & get () const { return m_name ; } }; int main () { Name n1 { \"Alex\" }; n1 = Name { \"Joe\" }; // error: move assignment deleted std :: cout << n1 . get () << '\\n' ; return 0 ; } \u5982\u679c\u5220\u9664\u590d\u5236\u6784\u9020\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u5c06\u4e0d\u4f1a\u751f\u6210\u9690\u5f0f\u79fb\u52a8\u6784\u9020\u51fd\u6570\uff08\u4f7f\u5bf9\u8c61\u65e2\u4e0d\u53ef\u590d\u5236\u4e5f\u4e0d\u53ef\u79fb\u52a8\uff09\u3002\u56e0\u6b64\uff0c\u5728\u5220\u9664\u590d\u5236\u6784\u9020\u51fd\u6570\u65f6\uff0c\u6700\u597d\u660e\u786e\u8bf4\u660e\u4f60\u5e0c\u671b\u4ece\u79fb\u52a8\u6784\u9020\u51fd\u6570\u4e2d\u5f97\u5230\u4ec0\u4e48\u884c\u4e3a\u3002\u8981\u4e48\u663e\u5f0f\u5220\u9664\u6784\u9020\u51fd\u6570\uff08\u660e\u786e\u8fd9\u662f\u6211\u4eec\u60f3\u8981\u7684\u884c\u4e3a\uff09\uff0c\u8981\u4e48\u9ed8\u8ba4\u5220\u9664\u6784\u9020\u51fd\u6570\uff08\u4f7f\u7c7b\u53ea\u53ef\u79fb\u52a8\uff09\u3002 Key insight \u4e94\u5219\u8fd0\u7b97\u6cd5\u5219 \u6307\u51fa\uff0c\u5982\u679c\u5b9a\u4e49\u6216\u5220\u9664\u4e86\u590d\u5236\u6784\u9020\u51fd\u6570\u3001\u590d\u5236\u8d4b\u503c\u51fd\u6570\u3001\u79fb\u52a8\u6784\u9020\u51fd\u6570\u3001\u79fb\u52a8\u8d4b\u503c\u51fd\u6570\u6216\u6790\u6784\u51fd\u6570\uff0c\u90a3\u4e48\u5c31\u5e94\u8be5\u5b9a\u4e49\u6216\u5220\u9664\u8fd9\u4e9b\u51fd\u6570\u4e2d\u7684> \u6bcf\u4e00\u4e2a\u3002 \u5982\u679c\u4f60\u60f3\u8981\u4e00\u4e2a\u53ef\u590d\u5236\u4f46\u4e0d\u53ef\u79fb\u52a8\u7684\u5bf9\u8c61\uff0c\u90a3\u4e48\u53ea\u5220\u9664 move \u6784\u9020\u51fd\u6570\u548c move \u8d4b\u503c\u4f3c\u4e4e\u662f\u4e2a\u597d\u4e3b\u610f\uff0c\u4f46\u8fd9\u6837\u505a\u7684\u4e0d\u5e78\u540e\u679c\u662f\uff0c\u5728\u5f3a\u5236\u590d\u5236\u6d88\u9664\u4e0d\u9002\u7528\u7684\u60c5\u51b5\u4e0b\uff0c\u8be5\u7c7b\u4e0d\u80fd\u6309\u503c\u8fd4\u56de\u3002\u51fa\u73b0\u8fd9\u79cd\u60c5\u51b5\u7684\u539f\u56e0\u662f\uff0c\u5df2\u5220\u9664\u7684\u79fb\u52a8\u6784\u9020\u51fd\u6570\u4ecd\u88ab\u58f0\u660e\uff0c\u56e0\u6b64\u7b26\u5408\u91cd\u8f7d\u89e3\u6790\u7684\u6761\u4ef6\u3002\u800c\u6309\u503c\u8fd4\u56de\u5c06\u4f7f\u5df2\u5220\u9664\u7684\u79fb\u52a8\u6784\u9020\u51fd\u6570\u4f18\u5148\u4e8e\u672a\u5220\u9664\u7684\u590d\u5236\u6784\u9020\u51fd\u6570\u3002\u4e0b\u9762\u7684\u7a0b\u5e8f\u53ef\u4ee5\u8bf4\u660e\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include #include #include class Name { private : std :: string m_name {}; public : Name ( std :: string_view name ) : m_name { name } { } Name ( const Name & name ) = default ; Name & operator = ( const Name & name ) = default ; Name ( Name && name ) = delete ; Name & operator = ( Name && name ) = delete ; const std :: string & get () const { return m_name ; } }; Name getJoe () { Name joe { \"Joe\" }; return joe ; // error: Move constructor was deleted } int main () { Name n { getJoe () }; std :: cout << n . get () << '\\n' ; return 0 ; }","title":"\u5220\u9664\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c"},{"location":"M-4-std-move/","tags":["move"],"text":"Key Takeaway \u53ea\u6709\u53f3\u503c\u624d\u80fd\u89e6\u53d1 \u79fb\u52a8\u8bed\u4e49 \uff0c\u56e0\u6b64\u5fc5\u987b\u662f\u53f3\u503c\uff08\u5b57\u9762\u91cf\u3001\u533f\u540d\u5bf9\u8c61\uff09\u6216\u8005\u88ab std::move() \u4f5c\u7528\u7684\u5de6\u503c\u624d\u80fd\u591f\u89e6\u53d1 std::move() \u4f1a\u63d0\u793a\u7f16\u8bd1\u5668\uff0c\u7a0b\u5e8f\u5458\u5df2\u7ecf\u4e0d\u518d\u9700\u8981\u8be5\u503c\u4e86\u3002\u6240\u4ee5\uff0c std::move() \u5e94\u8be5\u88ab\u5e94\u7528\u4e8e\u90a3\u4e9b\u5e0c\u671b\u8d44\u6e90\u88ab\u79fb\u51fa\u7684\u5bf9\u8c61\uff0c\u4e14\u6b64\u540e\u4e0d\u5e94\u5f53\u5047\u5b9a\u8be5\u5bf9\u8c61\u8fd8\u6301\u6709\u8be5\u8d44\u6e90\u3002\u7ed9\u8d44\u6e90\u88ab\u79fb\u51fa\u7684\u5bf9\u8c61\u8d4b\u65b0\u503c\u662f\u53ef\u4ee5\u7684\u3002 \u968f\u7740\u4f60\u5f00\u59cb\u9891\u7e41\u5730\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\uff0c\u4f60\u4f1a\u53d1\u73b0\u6709\u65f6\u5019\u5728\u4f60\u9700\u8981\u4f7f\u7528\u5b83\u7684\u65f6\u5019\uff0c\u5bf9\u8c61\u662f\u5de6\u503c\u800c\u4e0d\u662f\u53f3\u503c\u5bfc\u81f4\u4f60\u65e0\u6cd5\u4f7f\u7528\u3002\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a swap \u51fd\u6570\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include template < class T > void myswapCopy ( T & a , T & b ) { T tmp { a }; // invokes copy constructor a = b ; // invokes copy assignment b = tmp ; // invokes copy assignment } int main () { std :: string x { \"abc\" }; std :: string y { \"de\" }; std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; myswapCopy ( x , y ); std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; return 0 ; } \u4f20\u5165\u4e24\u4e2aT\u7c7b\u578b\u7684\u5bf9\u8c61\uff08\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u662f std::string \uff09\u7ed9\u51fd\u6570\uff0c\u7136\u540e\u5c06\u5b83\u4eec\u7684\u503c\u4ea4\u6362\uff08\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u521b\u5efa\u4e86\u4e09\u4e2a\u526f\u672c\uff09\u3002\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 3 4 x: abc y: de x: de y: abc \u8fd9\u4e2a\u7248\u672c\u7684 swap \u51fd\u6570\u521b\u5efa\u4e86\u4e09\u4e2a\u526f\u672c\uff0c \u800c\u521b\u5efa\u526f\u672c\u662f\u4f4e\u6548\u7684\u64cd\u4f5c\uff0c\u8fd9\u5176\u4e2d\u6d89\u53ca\u5230\u4e86\u591a\u6b21\u5b57\u7b26\u4e32\u7684\u521b\u5efa\u548c\u9500\u6bc1\uff0c\u663e\u7136\u6548\u7387\u662f\u5f88\u4f4e\u7684\u3002 \u5b9e\u9645\u4e0a\uff0c\u8fd9\u91cc\u5e76\u4e0d\u9700\u8981\u521b\u5efa\u62f7\u8d1d\uff0c\u56e0\u4e3a\u6211\u4eec\u6267\u884c\u5e0c\u671b\u5c06a\u548cb\u7684\u503c\u4ea4\u6362\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u4f7f\u7528\u4e09\u6b21\u79fb\u52a8\u4e5f\u53ef\u5b8c\u6210\u554a\uff01\u6240\u4ee5\u5982\u679c\u80fd\u5c06\u62f7\u8d1d\u8bed\u4e49\u66ff\u6362\u4e3a\u79fb\u52a8\u8bed\u4e49\uff0c\u4ee3\u7801\u65e0\u7591\u4f1a\u66f4\u9ad8\u6548\u3002 \u4f46\u662f\u5e94\u8be5\u600e\u4e48\u505a\u5462\uff1f\u8fd9\u91cc\u7684\u95ee\u9898\u5728\u4e8e\uff0ca\u548cb\u90fd\u662f \u5de6\u503c\u5f15\u7528 \uff0c\u800c\u4e0d\u662f \u53f3\u503c\u5f15\u7528 \uff0c\u6240\u4ee5\u6ca1\u6709\u529e\u6cd5\u8c03\u7528 \u79fb\u52a8\u6784\u9020\u51fd\u6570 \u548c \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 \u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u57fa\u4e8e\u5de6\u503c\u5f15\u7528\u6267\u884c\u7684\u662f\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u64cd\u4f5c\u7b26\u3002\u90a3\u7a76\u7adf\u5e94\u8be5\u5982\u4f55\u5b9e\u73b0\u5462\uff1f std::move \u5728 C++11 \u4e2d \u6807\u51c6\u5e93\u63d0\u4f9b\u4e86 std::move \u51fd\u6570\u7528\u4e8e\u5c06\u5b9e\u53c2\u8f6c\u6362\uff08\u4f7f\u7528 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \uff09\u4e3a\u53f3\u503c\u5f15\u7528\uff0c\u4ee5\u4fbf\u6fc0\u6d3b\u79fb\u52a8\u64cd\u4f5c\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 std::move \u5bf9\u4e00\u4e2a\u5de6\u503c\u7c7b\u578b\u8fdb\u884c\u8f6c\u6362\uff0c\u4f7f\u5176\u80fd\u591f\u88ab\u79fb\u52a8\u3002 std::move \u5b9a\u4e49\u5728utility\u5934\u6587\u4ef6\u4e2d\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u548c\u4e4b\u524d\u7684\u7c7b\u4f3c\uff0c\u4f46\u662f myswapMove() \u51fd\u6570\u4f7f\u7528 std::move \u51fd\u6570\u5c06\u5de6\u503c\u8f6c\u6362\u6210\u4e86\u53f3\u503c\uff0c\u6240\u4ee5\u8c03\u7528\u4e86\u79fb\u52a8\u8bed\u4e49\u76f8\u5173\u64cd\u4f5c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include #include // for std::move template < class T > void myswapMove ( T & a , T & b ) { T tmp { std :: move ( a ) }; // invokes move constructor a = std :: move ( b ); // invokes move assignment b = std :: move ( tmp ); // invokes move assignment } int main () { std :: string x { \"abc\" }; std :: string y { \"de\" }; std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; myswapMove ( x , y ); std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 3 4 x: abc y: de x: de y: abc \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4e00\u6837\uff0c\u4f46\u662f\u6548\u7387\u8981\u9ad8\u5f97\u591a\u3002\u5f53 tmp \u521d\u59cb\u5316\u65f6\uff0c\u5b83\u6ca1\u6709\u521b\u5efax\u526f\u672c\uff0c\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528 std::move \u5c06\u5de6\u503cx\u8f6c\u6362\u6210\u4e86\u4e00\u4e2a\u53f3\u503c\u3002\u56e0\u4e3a\u53c2\u6570\u662f\u53f3\u503c\uff0c\u6240\u4ee5\u8c03\u7528\u4e86\u79fb\u52a8\u6784\u9020\u51fd\u6570\uff0cx\u88ab\u79fb\u52a8\u5230\u4e86 tmp \u3002 \u7ecf\u8fc7\u51e0\u6b21\u8f6c\u6362\uff0c\u53d8\u91cfx\u7684\u503c\u88ab\u79fb\u52a8\u5230\u4e86y\uff0c\u800cy\u7684\u503c\u4e5f\u88ab\u79fb\u52a8\u5230\u4e86x\u3002 \u53e6\u5916\u4e00\u4e2a\u4f8b\u5b50 \u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528 std::move \u6765\u586b\u5145\u5bb9\u5668\u7684\u5143\u7d20\uff0c\u4f8b\u5982\u4f7f\u7528\u5de6\u503c\u586b\u5145 std::vector \u3002 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528\u62f7\u8d1d\u8bed\u4e49\u5411 vector \u6dfb\u52a0\u5143\u7d20\u3002\u7136\u540e\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\u505a\u540c\u6837\u7684\u64cd\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include #include // for std::move #include int main () { std :: vector < std :: string > v ; // \u8fd9\u91cc\u4f7f\u7528 std::string \u662f\u56e0\u4e3a\u5b83\u662f\u53ef\u79fb\u52a8\u7684 (std::string_view \u4e0d\u53ef\u79fb\u52a8) std :: string str { \"Knock\" }; std :: cout << \"Copying str \\n \" ; v . push_back ( str ); // \u8c03\u7528\u5de6\u503c\u7248\u672c\u7684 push_back\uff0c\u5b83\u4f1a\u5c06str\u62f7\u8d1d\u5230\u6570\u7ec4\u5143\u7d20 std :: cout << \"str: \" << str << '\\n' ; std :: cout << \"vector: \" << v [ 0 ] << '\\n' ; std :: cout << \" \\n Moving str \\n \" ; v . push_back ( std :: move ( str )); // \u8c03\u7528\u53f3\u503c\u7248\u672c\u7684 push_back\uff0c\u5b83\u4f1a\u5c06str\u79fb\u52a8\u5230\u6570\u7ec4\u5143\u7d20 std :: cout << \"str: \" << str << '\\n' ; // \u6253\u5370\u4e2d\u5956\u7ed3\u679c std :: cout << \"vector:\" << v [ 0 ] << ' ' << v [ 1 ] << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\u4f1a\u8f93\u51fa\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 5 6 7 Copying str str: Knock vector: Knock Moving str str: vector: Knock Knock \u5728\u7b2c\u4e00\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f20\u5165 push_back() \u7684\u662f\u4e00\u4e2a\u5de6\u503c\uff0c\u6240\u4ee5\u5b83\u4f1a\u4f7f\u7528\u62f7\u8d1d\u8bed\u4e49\u5411 vector \u6dfb\u52a0\u5143\u7d20\u3002\u56e0\u6b64\uff0c\u539f\u5b57\u7b26\u4e32\u8fd8\u88ab\u4fdd\u5b58\u5728 str \u4e2d\u3002 \u5bf9\u4e8e\u7b2c\u4e8c\u79cd\u60c5\u51b5\uff0c\u4f20\u5165 push_back() \u7684\u662f\u4e00\u4e2a\u53f3\u503c\uff08\u51c6\u786e\u6765\u8bb2\u662f\u901a\u8fc7 std::move \u5c06\u5de6\u503c\u8f6c\u6362\u6210\u7684\u53f3\u503c\uff09\uff0c\u6240\u4ee5\u5b83\u4f1a\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\u5411 vector \u6dfb\u52a0\u5143\u7d20\u3002\u8fd9\u4e2a\u64cd\u4f5c\u65e0\u7591\u662f\u66f4\u52a0\u9ad8\u6548\u7684\uff0c\u56e0\u4e3a vector \u7684\u5143\u7d20\u201c\u7a83\u53d6\u4e86\u201d\u5b57\u7b26\u4e32\u7684\u503c\uff0c\u800c\u6ca1\u6709\u521b\u5efa\u5b83\u7684\u62f7\u8d1d\u3002 \u8d44\u6e90\u88ab\u79fb\u52a8\u7684\u5bf9\u8c61\u5904\u4e8e\u4e00\u79cd\u6709\u6548\uff0c\u4f46\u53ef\u80fd\u4e0d\u786e\u5b9a\u7684\u72b6\u6001 \u5f53\u6211\u4eec\u4ece\u4e00\u4e2a \u4e34\u65f6\u5bf9\u8c61 \u4e2d\u628a\u8d44\u6e90\u79fb\u52a8\u51fa\u6765\u65f6\uff0c\u8fd9\u4e2a\u4e34\u65f6\u5bf9\u8c61\u4e2d\u8fd8\u5269\u4e0b\u4ec0\u4e48\u5176\u5b9e\u5e76\u4e0d\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u9a6c\u4e0a\u5c31\u8981\u88ab\u9500\u6bc1\u4e86\u3002\u4f46\u662f\u5bf9\u4e8e\u4e00\u4e2a \u5de6\u503c\u5bf9\u8c61 \u6765\u8bf4\uff0c\u5f53\u6211\u4eec\u5bf9\u5b83\u4f7f\u7528 std::move() \u540e\u4f1a\u600e\u4e48\u6837\u5462\uff1f\u56e0\u4e3a\u8be5\u5de6\u503c\u5728\u5176\u8d44\u6e90\u88ab\u79fb\u52a8\u540e\uff0c\u4ecd\u7136\u53ef\u80fd\u4f1a\u88ab\u8bbf\u95ee\uff08\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f53\u6211\u4eec\u6253\u5370 str \u7684\u503c\u65f6\uff0c\u5185\u5bb9\u5df2\u7ecf\u6ca1\u6709\u4e86\uff09\uff0c\u6240\u4ee5\u6709\u5fc5\u8981\u77e5\u9053\u8be5\u5bf9\u8c61\u8d44\u6e90\u88ab\u79fb\u52a8\u540e\uff0c\u8fd8\u6709\u4ec0\u4e48\u7559\u5728\u539f\u5bf9\u8c61\u4e2d\u3002 \u5173\u4e8e\u8fd9\u4e2a\u95ee\u9898\u7684\u601d\u8003\u6709\u4e24\u4e2a\u6d3e\u7cfb\u3002\u4e00\u6d3e\u4eba\u4efb\u52a1\uff0c\u8d44\u6e90\u5df2\u7ecf\u88ab\u79fb\u52a8\u8d70\u7684\u5bf9\u8c61\uff0c\u5e94\u8be5\u88ab\u91cd\u7f6e\u4e3a\u67d0\u79cd\u9ed8\u8ba4\u72b6\u6001\u62160\u5e62\uff0c\u56e0\u4e3a\u5b83\u4e0d\u518d\u62e5\u6709\u8d44\u6e90\u3002\u4e0a\u9762\u7684\u4f8b\u5b50\u5f88\u597d\u5730\u5c55\u793a\u4e86\u8fd9\u79cd\u60c5\u51b5\u3002 \u53e6\u5916\u4e00\u6d3e\u4eba\u5219\u8ba4\u4e3a\u5e94\u8be5\u201c\u600e\u4e48\u65b9\u4fbf\u600e\u4e48\u6765\u201d\uff0c\u5982\u679c\u6e05\u7406\u8be5\u5bf9\u8c61\u5f88\u9ebb\u70e6\uff0c\u90a3\u5c31\u4e0d\u5fc5\u6e05\u7406\u3002 \u90a3\u4e48\uff0c\u6807\u51c6\u5e93\u662f\u5982\u4f55\u505a\u7684\u5462\uff1f\u5173\u4e8e\u8fd9\u4e2a\u95ee\u9898\uff0cC++\u6807\u51c6\u8ba4\u4e3a\uff1a\u201c\u9664\u975e\u6709\u660e\u786e\u7684\u89c4\u5b9a\uff0c\u5426\u5219C++\u6807\u51c6\u5e93\u4e2d\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u5728\u8d44\u6e90\u88ab\u79fb\u52a8\u540e\u5e94\u8be5\u88ab\u7f6e\u4e8e \u4efb\u610f\u4e00\u79cd\u6709\u6548 \u7684\u72b6\u6001\u201d\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53\u5de6\u503c\u6253\u5370 str \u503c\u65f6\uff0c\u7a0b\u5e8f\u8f93\u51fa\u7684\u7a7a\u5b57\u7b26\u4e32\u3002\u4f46\u662f\uff0c\u8fd9\u5e76\u4e0d\u662f\u4e00\u5b9a\u7684\uff0c\u6253\u5370\u4efb\u4f55\u5176\u4ed6\u5408\u6cd5\u7684\u5b57\u7b26\u4e32\u4e5f\u53ef\u4ee5\uff0c\u5305\u62ec\u7a7a\u5b57\u7b26\u4e32\u3001\u539f\u672c\u7684\u5b57\u7b26\u4e32\u6216\u4efb\u4f55\u5408\u6cd5\u7684\u5b57\u7b26\u4e32\u3002\u56e0\u6b64\uff0c \u6211\u4eec\u5e94\u8be5\u907f\u514d\u7ee7\u7eed\u4f7f\u7528\u5df2\u7ecf\u88ab\u79fb\u52a8\u7684\u5de6\u503c\u5bf9\u8c61\uff0c\u56e0\u4e3a\u5176\u7ed3\u679c\u53d6\u51b3\u4e8e\u7f16\u8bd1\u5668\u7684\u5177\u4f53\u5b9e\u73b0\u3002 \u5728\u6709\u4e9b\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u7ee7\u7eed\u4f7f\u7528\u5df2\u7ecf\u88ab\u79fb\u52a8\u7684\u5bf9\u8c61\uff08\u800c\u4e0d\u5e0c\u671b\u91cd\u65b0\u5206\u914d\u4e00\u4e2a\u65b0\u5bf9\u8c61\uff09\u3002\u4f8b\u5982\uff0c\u5728\u4e0a\u9762 myswapMove() \u7684\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06a\u7684\u8d44\u6e90\u79fb\u51fa\uff0c\u7136\u540e\u5c06\u5176\u4ed6\u8d44\u6e90\u79fb\u52a8\u5230a\u3002\u8fd9\u4e48\u505a\u53ef\u884c\u662f\u56e0\u4e3a\uff0ca\u7684\u503c\u88ab\u79fb\u9664\u5230a\u518d\u6b21\u83b7\u5f97\u4e00\u4e2a\u786e\u5b9a\u7684\u503c\u7684\u8fd9\u6bb5\u65f6\u95f4\u5185\uff0c\u6211\u4eec\u4e0d\u4f1a\u4f7f\u7528a\u7684\u503c\u3002 \u5bf9\u4e8e\u4e00\u4e2a\u88ab\u79fb\u52a8\u4e86\u8d44\u6e90\u7684\u5bf9\u8c61\u6765\u8bf4\uff0c\u8c03\u7528\u4efb\u4f55\u4e0d\u4f9d\u8d56\u5176\u503c\u7684\u51fd\u6570\u662f\u5b89\u5168\u7684 \u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u3001\u91cd\u7f6e\u8be5\u5bf9\u8c61\u7684\u503c\uff08\u4f7f\u7528 = \u6216 clear() \u6216 reset() \u6210\u5458\u51fd\u6570\uff09\u3002\u6211\u4eec\u53ef\u4ee5\u68c0\u67e5\u5b83\u7684\u72b6\u6001(\u4f8b\u5982\u4f7f\u7528 empty() \u5224\u65ad\u8be5\u5bf9\u8c61\u662f\u5426\u6709\u503c)\u3002\u4f46\u662f\uff0c\u6211\u4eec\u5fc5\u987b\u907f\u514d operator[] \u6216 front() \u8fd9\u6837\u7684\u51fd\u6570\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u51fd\u6570\u4f9d\u8d56\u4e8e\u5bf9\u8c61\u4e2d\u5b58\u653e\u7684\u503c\uff0c\u800c\u88ab\u79fb\u52a8\u540e\u7684\u5bf9\u8c61\u53ef\u80fd\u53ef\u4ee5\uff0c\u4e5f\u53ef\u80fd\u4e0d\u80fd\u591f\u63d0\u4f9b\u8fd9\u4e9b\u503c\u3002 \u5173\u952e\u4fe1\u606f std::move() \u4f1a\u63d0\u793a\u7f16\u8bd1\u5668\uff0c\u7a0b\u5e8f\u5458\u5df2\u7ecf\u4e0d\u518d\u9700\u8981\u8be5\u503c\u4e86\u3002\u6240\u4ee5\uff0c std::move() \u5e94\u8be5\u88ab\u5e94\u7528\u4e8e\u90a3\u4e9b\u5e0c\u671b\u8d44\u6e90\u88ab\u79fb\u51fa\u7684\u5bf9\u8c61\uff0c\u4e14\u6b64\u540e\u4e0d\u5e94\u5f53\u5047\u5b9a\u8be5\u5bf9\u8c61\u8fd8\u6301\u6709\u8be5\u8d44\u6e90\u3002\u7ed9\u8d44\u6e90\u88ab\u79fb\u51fa\u7684\u5bf9\u8c61\u8d4b\u65b0\u503c\u662f\u53ef\u4ee5\u7684\u3002 std::move \u8fd8\u6709\u4ec0\u4e48\u7528\u9014\uff1f std::move \u5728\u8fdb\u884c\u6570\u7ec4\u6392\u5e8f\u65f6\u4e5f\u5f88\u6709\u7528\u3002\u5f88\u591a\u6392\u5e8f\u7b97\u6cd5 (\u4f8b\u5982\u9009\u62e9\u6392\u5e8f\u548c\u5192\u6ce1\u6392\u5e8f)\u662f\u57fa\u4e8e\u4ea4\u6362\u4e24\u4e2a\u5143\u7d20\u800c\u5b9e\u73b0\u7684\u3002\u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u57fa\u4e8e\u62f7\u8d1d\u8bed\u4e49\u7684\u4ea4\u6362\uff0c\u73b0\u5728\u53ef\u4ee5\u5c06\u5b83\u66ff\u6362\u6210\u57fa\u4e8e\u79fb\u52a8\u8bed\u4e49\u7684\u4ea4\u6362\u4e86\uff0c\u8fd9\u4e48\u505a\u6548\u7387\u4f1a\u9ad8\u5f88\u591a\u3002 \u6b64\u5916\uff0c\u5f53\u6211\u4eec\u9700\u8981\u5c06\u67d0\u4e2a\u667a\u80fd\u6307\u9488\u7ba1\u7406\u7684\u5185\u5bb9\u8f6c\u79fb\u5230\u53e6\u4e00\u4e2a\u667a\u80fd\u6307\u9488\u65f6\uff0c\u8be5\u51fd\u6570\u4e5f\u5f88\u6709\u7528\u3002 \u5c0f\u7ed3 \u5f53\u4e00\u4e2a\u5de6\u503c\u9700\u8981\u88ab\u5f53\u505a\u53f3\u503c\u4f7f\u7528\uff0c\u4ee5\u4fbf\u89e6\u53d1\u79fb\u52a8\u8bed\u4e49\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 std::move \u3002","title":"M.4 - std::move"},{"location":"M-4-std-move/#stdmove","text":"\u5728 C++11 \u4e2d \u6807\u51c6\u5e93\u63d0\u4f9b\u4e86 std::move \u51fd\u6570\u7528\u4e8e\u5c06\u5b9e\u53c2\u8f6c\u6362\uff08\u4f7f\u7528 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \uff09\u4e3a\u53f3\u503c\u5f15\u7528\uff0c\u4ee5\u4fbf\u6fc0\u6d3b\u79fb\u52a8\u64cd\u4f5c\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 std::move \u5bf9\u4e00\u4e2a\u5de6\u503c\u7c7b\u578b\u8fdb\u884c\u8f6c\u6362\uff0c\u4f7f\u5176\u80fd\u591f\u88ab\u79fb\u52a8\u3002 std::move \u5b9a\u4e49\u5728utility\u5934\u6587\u4ef6\u4e2d\u3002 \u4e0b\u9762\u7684\u7a0b\u5e8f\u548c\u4e4b\u524d\u7684\u7c7b\u4f3c\uff0c\u4f46\u662f myswapMove() \u51fd\u6570\u4f7f\u7528 std::move \u51fd\u6570\u5c06\u5de6\u503c\u8f6c\u6362\u6210\u4e86\u53f3\u503c\uff0c\u6240\u4ee5\u8c03\u7528\u4e86\u79fb\u52a8\u8bed\u4e49\u76f8\u5173\u64cd\u4f5c\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include #include // for std::move template < class T > void myswapMove ( T & a , T & b ) { T tmp { std :: move ( a ) }; // invokes move constructor a = std :: move ( b ); // invokes move assignment b = std :: move ( tmp ); // invokes move assignment } int main () { std :: string x { \"abc\" }; std :: string y { \"de\" }; std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; myswapMove ( x , y ); std :: cout << \"x: \" << x << '\\n' ; std :: cout << \"y: \" << y << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 3 4 x: abc y: de x: de y: abc \u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4e00\u6837\uff0c\u4f46\u662f\u6548\u7387\u8981\u9ad8\u5f97\u591a\u3002\u5f53 tmp \u521d\u59cb\u5316\u65f6\uff0c\u5b83\u6ca1\u6709\u521b\u5efax\u526f\u672c\uff0c\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528 std::move \u5c06\u5de6\u503cx\u8f6c\u6362\u6210\u4e86\u4e00\u4e2a\u53f3\u503c\u3002\u56e0\u4e3a\u53c2\u6570\u662f\u53f3\u503c\uff0c\u6240\u4ee5\u8c03\u7528\u4e86\u79fb\u52a8\u6784\u9020\u51fd\u6570\uff0cx\u88ab\u79fb\u52a8\u5230\u4e86 tmp \u3002 \u7ecf\u8fc7\u51e0\u6b21\u8f6c\u6362\uff0c\u53d8\u91cfx\u7684\u503c\u88ab\u79fb\u52a8\u5230\u4e86y\uff0c\u800cy\u7684\u503c\u4e5f\u88ab\u79fb\u52a8\u5230\u4e86x\u3002","title":"std::move"},{"location":"M-4-std-move/#_1","text":"\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528 std::move \u6765\u586b\u5145\u5bb9\u5668\u7684\u5143\u7d20\uff0c\u4f8b\u5982\u4f7f\u7528\u5de6\u503c\u586b\u5145 std::vector \u3002 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528\u62f7\u8d1d\u8bed\u4e49\u5411 vector \u6dfb\u52a0\u5143\u7d20\u3002\u7136\u540e\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\u505a\u540c\u6837\u7684\u64cd\u4f5c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include #include // for std::move #include int main () { std :: vector < std :: string > v ; // \u8fd9\u91cc\u4f7f\u7528 std::string \u662f\u56e0\u4e3a\u5b83\u662f\u53ef\u79fb\u52a8\u7684 (std::string_view \u4e0d\u53ef\u79fb\u52a8) std :: string str { \"Knock\" }; std :: cout << \"Copying str \\n \" ; v . push_back ( str ); // \u8c03\u7528\u5de6\u503c\u7248\u672c\u7684 push_back\uff0c\u5b83\u4f1a\u5c06str\u62f7\u8d1d\u5230\u6570\u7ec4\u5143\u7d20 std :: cout << \"str: \" << str << '\\n' ; std :: cout << \"vector: \" << v [ 0 ] << '\\n' ; std :: cout << \" \\n Moving str \\n \" ; v . push_back ( std :: move ( str )); // \u8c03\u7528\u53f3\u503c\u7248\u672c\u7684 push_back\uff0c\u5b83\u4f1a\u5c06str\u79fb\u52a8\u5230\u6570\u7ec4\u5143\u7d20 std :: cout << \"str: \" << str << '\\n' ; // \u6253\u5370\u4e2d\u5956\u7ed3\u679c std :: cout << \"vector:\" << v [ 0 ] << ' ' << v [ 1 ] << '\\n' ; return 0 ; } \u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\u4f1a\u8f93\u51fa\u5982\u4e0b\u4fe1\u606f\uff1a 1 2 3 4 5 6 7 Copying str str: Knock vector: Knock Moving str str: vector: Knock Knock \u5728\u7b2c\u4e00\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f20\u5165 push_back() \u7684\u662f\u4e00\u4e2a\u5de6\u503c\uff0c\u6240\u4ee5\u5b83\u4f1a\u4f7f\u7528\u62f7\u8d1d\u8bed\u4e49\u5411 vector \u6dfb\u52a0\u5143\u7d20\u3002\u56e0\u6b64\uff0c\u539f\u5b57\u7b26\u4e32\u8fd8\u88ab\u4fdd\u5b58\u5728 str \u4e2d\u3002 \u5bf9\u4e8e\u7b2c\u4e8c\u79cd\u60c5\u51b5\uff0c\u4f20\u5165 push_back() \u7684\u662f\u4e00\u4e2a\u53f3\u503c\uff08\u51c6\u786e\u6765\u8bb2\u662f\u901a\u8fc7 std::move \u5c06\u5de6\u503c\u8f6c\u6362\u6210\u7684\u53f3\u503c\uff09\uff0c\u6240\u4ee5\u5b83\u4f1a\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\u5411 vector \u6dfb\u52a0\u5143\u7d20\u3002\u8fd9\u4e2a\u64cd\u4f5c\u65e0\u7591\u662f\u66f4\u52a0\u9ad8\u6548\u7684\uff0c\u56e0\u4e3a vector \u7684\u5143\u7d20\u201c\u7a83\u53d6\u4e86\u201d\u5b57\u7b26\u4e32\u7684\u503c\uff0c\u800c\u6ca1\u6709\u521b\u5efa\u5b83\u7684\u62f7\u8d1d\u3002","title":"\u53e6\u5916\u4e00\u4e2a\u4f8b\u5b50"},{"location":"M-4-std-move/#_2","text":"\u5f53\u6211\u4eec\u4ece\u4e00\u4e2a \u4e34\u65f6\u5bf9\u8c61 \u4e2d\u628a\u8d44\u6e90\u79fb\u52a8\u51fa\u6765\u65f6\uff0c\u8fd9\u4e2a\u4e34\u65f6\u5bf9\u8c61\u4e2d\u8fd8\u5269\u4e0b\u4ec0\u4e48\u5176\u5b9e\u5e76\u4e0d\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u9a6c\u4e0a\u5c31\u8981\u88ab\u9500\u6bc1\u4e86\u3002\u4f46\u662f\u5bf9\u4e8e\u4e00\u4e2a \u5de6\u503c\u5bf9\u8c61 \u6765\u8bf4\uff0c\u5f53\u6211\u4eec\u5bf9\u5b83\u4f7f\u7528 std::move() \u540e\u4f1a\u600e\u4e48\u6837\u5462\uff1f\u56e0\u4e3a\u8be5\u5de6\u503c\u5728\u5176\u8d44\u6e90\u88ab\u79fb\u52a8\u540e\uff0c\u4ecd\u7136\u53ef\u80fd\u4f1a\u88ab\u8bbf\u95ee\uff08\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f53\u6211\u4eec\u6253\u5370 str \u7684\u503c\u65f6\uff0c\u5185\u5bb9\u5df2\u7ecf\u6ca1\u6709\u4e86\uff09\uff0c\u6240\u4ee5\u6709\u5fc5\u8981\u77e5\u9053\u8be5\u5bf9\u8c61\u8d44\u6e90\u88ab\u79fb\u52a8\u540e\uff0c\u8fd8\u6709\u4ec0\u4e48\u7559\u5728\u539f\u5bf9\u8c61\u4e2d\u3002 \u5173\u4e8e\u8fd9\u4e2a\u95ee\u9898\u7684\u601d\u8003\u6709\u4e24\u4e2a\u6d3e\u7cfb\u3002\u4e00\u6d3e\u4eba\u4efb\u52a1\uff0c\u8d44\u6e90\u5df2\u7ecf\u88ab\u79fb\u52a8\u8d70\u7684\u5bf9\u8c61\uff0c\u5e94\u8be5\u88ab\u91cd\u7f6e\u4e3a\u67d0\u79cd\u9ed8\u8ba4\u72b6\u6001\u62160\u5e62\uff0c\u56e0\u4e3a\u5b83\u4e0d\u518d\u62e5\u6709\u8d44\u6e90\u3002\u4e0a\u9762\u7684\u4f8b\u5b50\u5f88\u597d\u5730\u5c55\u793a\u4e86\u8fd9\u79cd\u60c5\u51b5\u3002 \u53e6\u5916\u4e00\u6d3e\u4eba\u5219\u8ba4\u4e3a\u5e94\u8be5\u201c\u600e\u4e48\u65b9\u4fbf\u600e\u4e48\u6765\u201d\uff0c\u5982\u679c\u6e05\u7406\u8be5\u5bf9\u8c61\u5f88\u9ebb\u70e6\uff0c\u90a3\u5c31\u4e0d\u5fc5\u6e05\u7406\u3002 \u90a3\u4e48\uff0c\u6807\u51c6\u5e93\u662f\u5982\u4f55\u505a\u7684\u5462\uff1f\u5173\u4e8e\u8fd9\u4e2a\u95ee\u9898\uff0cC++\u6807\u51c6\u8ba4\u4e3a\uff1a\u201c\u9664\u975e\u6709\u660e\u786e\u7684\u89c4\u5b9a\uff0c\u5426\u5219C++\u6807\u51c6\u5e93\u4e2d\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u5728\u8d44\u6e90\u88ab\u79fb\u52a8\u540e\u5e94\u8be5\u88ab\u7f6e\u4e8e \u4efb\u610f\u4e00\u79cd\u6709\u6548 \u7684\u72b6\u6001\u201d\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53\u5de6\u503c\u6253\u5370 str \u503c\u65f6\uff0c\u7a0b\u5e8f\u8f93\u51fa\u7684\u7a7a\u5b57\u7b26\u4e32\u3002\u4f46\u662f\uff0c\u8fd9\u5e76\u4e0d\u662f\u4e00\u5b9a\u7684\uff0c\u6253\u5370\u4efb\u4f55\u5176\u4ed6\u5408\u6cd5\u7684\u5b57\u7b26\u4e32\u4e5f\u53ef\u4ee5\uff0c\u5305\u62ec\u7a7a\u5b57\u7b26\u4e32\u3001\u539f\u672c\u7684\u5b57\u7b26\u4e32\u6216\u4efb\u4f55\u5408\u6cd5\u7684\u5b57\u7b26\u4e32\u3002\u56e0\u6b64\uff0c \u6211\u4eec\u5e94\u8be5\u907f\u514d\u7ee7\u7eed\u4f7f\u7528\u5df2\u7ecf\u88ab\u79fb\u52a8\u7684\u5de6\u503c\u5bf9\u8c61\uff0c\u56e0\u4e3a\u5176\u7ed3\u679c\u53d6\u51b3\u4e8e\u7f16\u8bd1\u5668\u7684\u5177\u4f53\u5b9e\u73b0\u3002 \u5728\u6709\u4e9b\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u7ee7\u7eed\u4f7f\u7528\u5df2\u7ecf\u88ab\u79fb\u52a8\u7684\u5bf9\u8c61\uff08\u800c\u4e0d\u5e0c\u671b\u91cd\u65b0\u5206\u914d\u4e00\u4e2a\u65b0\u5bf9\u8c61\uff09\u3002\u4f8b\u5982\uff0c\u5728\u4e0a\u9762 myswapMove() \u7684\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06a\u7684\u8d44\u6e90\u79fb\u51fa\uff0c\u7136\u540e\u5c06\u5176\u4ed6\u8d44\u6e90\u79fb\u52a8\u5230a\u3002\u8fd9\u4e48\u505a\u53ef\u884c\u662f\u56e0\u4e3a\uff0ca\u7684\u503c\u88ab\u79fb\u9664\u5230a\u518d\u6b21\u83b7\u5f97\u4e00\u4e2a\u786e\u5b9a\u7684\u503c\u7684\u8fd9\u6bb5\u65f6\u95f4\u5185\uff0c\u6211\u4eec\u4e0d\u4f1a\u4f7f\u7528a\u7684\u503c\u3002 \u5bf9\u4e8e\u4e00\u4e2a\u88ab\u79fb\u52a8\u4e86\u8d44\u6e90\u7684\u5bf9\u8c61\u6765\u8bf4\uff0c\u8c03\u7528\u4efb\u4f55\u4e0d\u4f9d\u8d56\u5176\u503c\u7684\u51fd\u6570\u662f\u5b89\u5168\u7684 \u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u3001\u91cd\u7f6e\u8be5\u5bf9\u8c61\u7684\u503c\uff08\u4f7f\u7528 = \u6216 clear() \u6216 reset() \u6210\u5458\u51fd\u6570\uff09\u3002\u6211\u4eec\u53ef\u4ee5\u68c0\u67e5\u5b83\u7684\u72b6\u6001(\u4f8b\u5982\u4f7f\u7528 empty() \u5224\u65ad\u8be5\u5bf9\u8c61\u662f\u5426\u6709\u503c)\u3002\u4f46\u662f\uff0c\u6211\u4eec\u5fc5\u987b\u907f\u514d operator[] \u6216 front() \u8fd9\u6837\u7684\u51fd\u6570\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u51fd\u6570\u4f9d\u8d56\u4e8e\u5bf9\u8c61\u4e2d\u5b58\u653e\u7684\u503c\uff0c\u800c\u88ab\u79fb\u52a8\u540e\u7684\u5bf9\u8c61\u53ef\u80fd\u53ef\u4ee5\uff0c\u4e5f\u53ef\u80fd\u4e0d\u80fd\u591f\u63d0\u4f9b\u8fd9\u4e9b\u503c\u3002 \u5173\u952e\u4fe1\u606f std::move() \u4f1a\u63d0\u793a\u7f16\u8bd1\u5668\uff0c\u7a0b\u5e8f\u5458\u5df2\u7ecf\u4e0d\u518d\u9700\u8981\u8be5\u503c\u4e86\u3002\u6240\u4ee5\uff0c std::move() \u5e94\u8be5\u88ab\u5e94\u7528\u4e8e\u90a3\u4e9b\u5e0c\u671b\u8d44\u6e90\u88ab\u79fb\u51fa\u7684\u5bf9\u8c61\uff0c\u4e14\u6b64\u540e\u4e0d\u5e94\u5f53\u5047\u5b9a\u8be5\u5bf9\u8c61\u8fd8\u6301\u6709\u8be5\u8d44\u6e90\u3002\u7ed9\u8d44\u6e90\u88ab\u79fb\u51fa\u7684\u5bf9\u8c61\u8d4b\u65b0\u503c\u662f\u53ef\u4ee5\u7684\u3002","title":"\u8d44\u6e90\u88ab\u79fb\u52a8\u7684\u5bf9\u8c61\u5904\u4e8e\u4e00\u79cd\u6709\u6548\uff0c\u4f46\u53ef\u80fd\u4e0d\u786e\u5b9a\u7684\u72b6\u6001"},{"location":"M-4-std-move/#stdmove_1","text":"std::move \u5728\u8fdb\u884c\u6570\u7ec4\u6392\u5e8f\u65f6\u4e5f\u5f88\u6709\u7528\u3002\u5f88\u591a\u6392\u5e8f\u7b97\u6cd5 (\u4f8b\u5982\u9009\u62e9\u6392\u5e8f\u548c\u5192\u6ce1\u6392\u5e8f)\u662f\u57fa\u4e8e\u4ea4\u6362\u4e24\u4e2a\u5143\u7d20\u800c\u5b9e\u73b0\u7684\u3002\u5728\u4e4b\u524d\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u57fa\u4e8e\u62f7\u8d1d\u8bed\u4e49\u7684\u4ea4\u6362\uff0c\u73b0\u5728\u53ef\u4ee5\u5c06\u5b83\u66ff\u6362\u6210\u57fa\u4e8e\u79fb\u52a8\u8bed\u4e49\u7684\u4ea4\u6362\u4e86\uff0c\u8fd9\u4e48\u505a\u6548\u7387\u4f1a\u9ad8\u5f88\u591a\u3002 \u6b64\u5916\uff0c\u5f53\u6211\u4eec\u9700\u8981\u5c06\u67d0\u4e2a\u667a\u80fd\u6307\u9488\u7ba1\u7406\u7684\u5185\u5bb9\u8f6c\u79fb\u5230\u53e6\u4e00\u4e2a\u667a\u80fd\u6307\u9488\u65f6\uff0c\u8be5\u51fd\u6570\u4e5f\u5f88\u6709\u7528\u3002","title":"std::move \u8fd8\u6709\u4ec0\u4e48\u7528\u9014\uff1f"},{"location":"M-4-std-move/#_3","text":"\u5f53\u4e00\u4e2a\u5de6\u503c\u9700\u8981\u88ab\u5f53\u505a\u53f3\u503c\u4f7f\u7528\uff0c\u4ee5\u4fbf\u89e6\u53d1\u79fb\u52a8\u8bed\u4e49\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 std::move \u3002","title":"\u5c0f\u7ed3"},{"location":"M-5-std-move-if-noexcept/","tags":["move"],"text":"Key Takeaway (\u611f\u8c22\u8bfb\u8005 Koe \u8d21\u732e\u4e86\u672c\u8282\u8bfe\u7684\u8349\u7a3f\uff01) \u5728 20.9 -- Exception specifications and noexcept \u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86 noexcept exception specifier and operator, which this lesson builds on. We also covered the strong exception guarantee , which guarantees that if a function is interrupted by an exception, no memory will be leaked and the program state will not be changed. In particular, all constructors should uphold the strong exception guarantee, so that the rest of the program won\u2019t be left in an altered state if construction of an object fails. The move constructors exception problem Consider the case where we are copying some object, and the copy fails for some reason (e.g. the machine is out of memory). In such a case, the object being copied is not harmed in any way, because the source object doesn\u2019t need to be modified to create a copy. We can discard the failed copy, and move on. The strong exception guarantee is upheld. Now consider the case where we are instead moving an object. A move operation transfers ownership of a given resource from the source to the destination object. If the move operation is interrupted by an exception after the transfer of ownership occurs, then our source object will be left in a modified state. This isn\u2019t a problem if the source object is a temporary object and going to be discarded after the move anyway -- but for non-temporary objects, we\u2019ve now damaged the source object. To comply with the strong exception guarantee , we\u2019d need to move the resource back to the source object, but if the move failed the first time, there\u2019s no guarantee the move back will succeed either. How can we give move constructors the strong exception guarantee ? It is simple enough to avoid throwing exceptions in the body of a move constructor, but a move constructor may invoke other constructors that are potentially throwing . Take for example the move constructor for std::pair , which must try to move each subobject in the source pair into the new pair object. 1 2 3 4 5 6 7 // Example move constructor definition for std::pair // Take in an 'old' pair, and then move construct the new pair's 'first' and 'second' subobjects from the 'old' ones template < typename T1 , typename T2 > pair < T1 , T2 >:: pair ( pair && old ) : first ( std :: move ( old . first )), second ( std :: move ( old . second )) {} COPY Now lets use two classes, MoveClass and CopyClass , which we will pair together to demonstrate the strong exception guarantee problem with move constructors: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 #include #include // For std::pair, std::make_pair, std::move, std::move_if_noexcept #include // std::runtime_error class MoveClass { private : int * m_resource {}; public : MoveClass () = default ; MoveClass ( int resource ) : m_resource { new int { resource } } {} // Copy constructor MoveClass ( const MoveClass & that ) { // deep copy if ( that . m_resource != nullptr ) { m_resource = new int { * that . m_resource }; } } // Move constructor MoveClass ( MoveClass && that ) noexcept : m_resource { that . m_resource } { that . m_resource = nullptr ; } ~ MoveClass () { std :: cout << \"destroying \" << * this << '\\n' ; delete m_resource ; } friend std :: ostream & operator << ( std :: ostream & out , const MoveClass & moveClass ) { out << \"MoveClass(\" ; if ( moveClass . m_resource == nullptr ) { out << \"empty\" ; } else { out << * moveClass . m_resource ; } out << ')' ; return out ; } }; class CopyClass { public : bool m_throw {}; CopyClass () = default ; // Copy constructor throws an exception when copying from a CopyClass object where its m_throw is 'true' CopyClass ( const CopyClass & that ) : m_throw { that . m_throw } { if ( m_throw ) { throw std :: runtime_error { \"abort!\" }; } } }; int main () { // We can make a std::pair without any problems: std :: pair my_pair { MoveClass { 13 }, CopyClass {} }; std :: cout << \"my_pair.first: \" << my_pair . first << '\\n' ; // But the problem arises when we try to move that pair into another pair. try { my_pair . second . m_throw = true ; // To trigger copy constructor exception // The following line will throw an exception std :: pair moved_pair { std :: move ( my_pair ) }; // We'll comment out this line later // std::pair moved_pair{ std::move_if_noexcept(my_pair) }; // We'll uncomment this line later std :: cout << \"moved pair exists \\n \" ; // Never prints } catch ( const std :: exception & ex ) { std :: cerr << \"Error found: \" << ex . what () << '\\n' ; } std :: cout << \"my_pair.first: \" << my_pair . first << '\\n' ; return 0 ; } COPY The above program prints: 1 2 3 4 5 6 destroying MoveClass(empty) my_pair.first: MoveClass(13) destroying MoveClass(13) Error found: abort! my_pair.first: MoveClass(empty) destroying MoveClass(empty) Let\u2019s explore what happened. The first printed line shows the temporary MoveClass object used to initialize my_pair gets destroyed as soon as the my_pair instantiation statement has been executed. It is empty since the MoveClass subobject in my_pair was move constructed from it, demonstrated by the next line which shows my_pair.first contains the MoveClass object with value 13 . It gets interesting in the third line. We created moved_pair by copy constructing its CopyClass subobject (it doesn\u2019t have a move constructor), but that copy construction threw an exception since we changed the Boolean flag. Construction of moved_pair was aborted by the exception, and its already-constructed members were destroyed. In this case, the MoveClass member was destroyed, printing destroying MoveClass(13) variable . Next we see the Error found: abort! message printed by main() . When we try to print my_pair.first again, it shows the MoveClass member is empty. Since moved_pair was initialized with std::move , the MoveClass member (which has a move constructor) got move constructed and my_pair.first was nulled. Finally, my_pair was destroyed at the end of main(). To summarize the above results: the move constructor of std::pair used the throwing copy constructor of CopyClass . This copy constructor threw an exception, causing the creation of moved_pair to abort, and my_pair.first to be permanently damaged. The strong exception guarantee was not preserved. std::move_if_noexcept to the rescue Note that the above problem could have been avoided if std::pair had tried to do a copy instead of a move. In that case, moved_pair would have failed to construct, but my_pair would not have been altered. But copying instead of moving has a performance cost that we don\u2019t want to pay for all objects -- ideally we want to do a move if we can do so safely, and a copy otherwise. Fortunately, C++ has a two mechanisms that, when used in combination, let us do exactly that. First, because noexcept functions are no-throw/no-fail, they implicitly meet the criteria for the strong exception guarantee . Thus, a noexcept move constructor is guaranteed to succeed. Second, we can use the standard library function std::move_if_noexcept() to determine whether a move or a copy should be performed. std::move_if_noexcept is a counterpart to std::move , and is used in the same way. If the compiler can tell that an object passed as an argument to std::move_if_noexcept won\u2019t throw an exception when it is move constructed (or if the object is move-only and has no copy constructor), then std::move_if_noexcept will perform identically to std::move() (and return the object converted to an r-value). Otherwise, std::move_if_noexcept will return a normal l-value reference to the object. \u5173\u952e\u4fe1\u606f std::move_if_noexcept will return a movable r-value if the object has a noexcept move constructor, otherwise it will return a copyable l-value. We can use the noexcept specifier in conjunction with std::move_if_noexcept to use move semantics only when a strong exception guarantee exists (and use copy semantics otherwise). Let\u2019s update the code in the previous example as follows: 1 2 //std::pair moved_pair{std::move(my_pair)}; // comment out this line now std :: pair moved_pair { std :: move_if_noexcept ( my_pair )}; // and uncomment this line COPY Running the program again prints: 1 2 3 4 5 6 destroying MoveClass(empty) my_pair.first: MoveClass(13) destroying MoveClass(13) Error found: abort! my_pair.first: MoveClass(13) destroying MoveClass(13) As you can see, after the exception was thrown, the subobject my_pair.first still points to the value 13 . The move constructor of std::pair isn\u2019t noexcept (as of C++20), so std::move_if_noexcept returns my_pair as an l-value reference. This causes moved_pair to be created via the copy constructor (rather than the move constructor). The copy constructor can throw safely, because it doesn\u2019t modify the source object. The standard library uses std::move_if_noexcept often to optimize for functions that are noexcept . For example, std::vector::resize will use move semantics if the element type has a noexcept move constructor, and copy semantics otherwise. This means std::vector will generally operate faster with objects that have a noexcept move constructor. \u6ce8\u610f If a type has both potentially throwing move semantics and deleted copy semantics (the copy constructor and copy assignment operator are unavailable), then std::move_if_noexcept will waive the strong guarantee and invoke move semantics. This conditional waiving of the strong guarantee is ubiquitous in the standard library container classes, since they use std::move_if_noexcept often.","title":"M.5 - std::move_if_noexcept"},{"location":"M-5-std-move-if-noexcept/#the-move-constructors-exception-problem","text":"Consider the case where we are copying some object, and the copy fails for some reason (e.g. the machine is out of memory). In such a case, the object being copied is not harmed in any way, because the source object doesn\u2019t need to be modified to create a copy. We can discard the failed copy, and move on. The strong exception guarantee is upheld. Now consider the case where we are instead moving an object. A move operation transfers ownership of a given resource from the source to the destination object. If the move operation is interrupted by an exception after the transfer of ownership occurs, then our source object will be left in a modified state. This isn\u2019t a problem if the source object is a temporary object and going to be discarded after the move anyway -- but for non-temporary objects, we\u2019ve now damaged the source object. To comply with the strong exception guarantee , we\u2019d need to move the resource back to the source object, but if the move failed the first time, there\u2019s no guarantee the move back will succeed either. How can we give move constructors the strong exception guarantee ? It is simple enough to avoid throwing exceptions in the body of a move constructor, but a move constructor may invoke other constructors that are potentially throwing . Take for example the move constructor for std::pair , which must try to move each subobject in the source pair into the new pair object. 1 2 3 4 5 6 7 // Example move constructor definition for std::pair // Take in an 'old' pair, and then move construct the new pair's 'first' and 'second' subobjects from the 'old' ones template < typename T1 , typename T2 > pair < T1 , T2 >:: pair ( pair && old ) : first ( std :: move ( old . first )), second ( std :: move ( old . second )) {} COPY Now lets use two classes, MoveClass and CopyClass , which we will pair together to demonstrate the strong exception guarantee problem with move constructors: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 #include #include // For std::pair, std::make_pair, std::move, std::move_if_noexcept #include // std::runtime_error class MoveClass { private : int * m_resource {}; public : MoveClass () = default ; MoveClass ( int resource ) : m_resource { new int { resource } } {} // Copy constructor MoveClass ( const MoveClass & that ) { // deep copy if ( that . m_resource != nullptr ) { m_resource = new int { * that . m_resource }; } } // Move constructor MoveClass ( MoveClass && that ) noexcept : m_resource { that . m_resource } { that . m_resource = nullptr ; } ~ MoveClass () { std :: cout << \"destroying \" << * this << '\\n' ; delete m_resource ; } friend std :: ostream & operator << ( std :: ostream & out , const MoveClass & moveClass ) { out << \"MoveClass(\" ; if ( moveClass . m_resource == nullptr ) { out << \"empty\" ; } else { out << * moveClass . m_resource ; } out << ')' ; return out ; } }; class CopyClass { public : bool m_throw {}; CopyClass () = default ; // Copy constructor throws an exception when copying from a CopyClass object where its m_throw is 'true' CopyClass ( const CopyClass & that ) : m_throw { that . m_throw } { if ( m_throw ) { throw std :: runtime_error { \"abort!\" }; } } }; int main () { // We can make a std::pair without any problems: std :: pair my_pair { MoveClass { 13 }, CopyClass {} }; std :: cout << \"my_pair.first: \" << my_pair . first << '\\n' ; // But the problem arises when we try to move that pair into another pair. try { my_pair . second . m_throw = true ; // To trigger copy constructor exception // The following line will throw an exception std :: pair moved_pair { std :: move ( my_pair ) }; // We'll comment out this line later // std::pair moved_pair{ std::move_if_noexcept(my_pair) }; // We'll uncomment this line later std :: cout << \"moved pair exists \\n \" ; // Never prints } catch ( const std :: exception & ex ) { std :: cerr << \"Error found: \" << ex . what () << '\\n' ; } std :: cout << \"my_pair.first: \" << my_pair . first << '\\n' ; return 0 ; } COPY The above program prints: 1 2 3 4 5 6 destroying MoveClass(empty) my_pair.first: MoveClass(13) destroying MoveClass(13) Error found: abort! my_pair.first: MoveClass(empty) destroying MoveClass(empty) Let\u2019s explore what happened. The first printed line shows the temporary MoveClass object used to initialize my_pair gets destroyed as soon as the my_pair instantiation statement has been executed. It is empty since the MoveClass subobject in my_pair was move constructed from it, demonstrated by the next line which shows my_pair.first contains the MoveClass object with value 13 . It gets interesting in the third line. We created moved_pair by copy constructing its CopyClass subobject (it doesn\u2019t have a move constructor), but that copy construction threw an exception since we changed the Boolean flag. Construction of moved_pair was aborted by the exception, and its already-constructed members were destroyed. In this case, the MoveClass member was destroyed, printing destroying MoveClass(13) variable . Next we see the Error found: abort! message printed by main() . When we try to print my_pair.first again, it shows the MoveClass member is empty. Since moved_pair was initialized with std::move , the MoveClass member (which has a move constructor) got move constructed and my_pair.first was nulled. Finally, my_pair was destroyed at the end of main(). To summarize the above results: the move constructor of std::pair used the throwing copy constructor of CopyClass . This copy constructor threw an exception, causing the creation of moved_pair to abort, and my_pair.first to be permanently damaged. The strong exception guarantee was not preserved.","title":"The move constructors exception problem"},{"location":"M-5-std-move-if-noexcept/#stdmove_if_noexcept-to-the-rescue","text":"Note that the above problem could have been avoided if std::pair had tried to do a copy instead of a move. In that case, moved_pair would have failed to construct, but my_pair would not have been altered. But copying instead of moving has a performance cost that we don\u2019t want to pay for all objects -- ideally we want to do a move if we can do so safely, and a copy otherwise. Fortunately, C++ has a two mechanisms that, when used in combination, let us do exactly that. First, because noexcept functions are no-throw/no-fail, they implicitly meet the criteria for the strong exception guarantee . Thus, a noexcept move constructor is guaranteed to succeed. Second, we can use the standard library function std::move_if_noexcept() to determine whether a move or a copy should be performed. std::move_if_noexcept is a counterpart to std::move , and is used in the same way. If the compiler can tell that an object passed as an argument to std::move_if_noexcept won\u2019t throw an exception when it is move constructed (or if the object is move-only and has no copy constructor), then std::move_if_noexcept will perform identically to std::move() (and return the object converted to an r-value). Otherwise, std::move_if_noexcept will return a normal l-value reference to the object. \u5173\u952e\u4fe1\u606f std::move_if_noexcept will return a movable r-value if the object has a noexcept move constructor, otherwise it will return a copyable l-value. We can use the noexcept specifier in conjunction with std::move_if_noexcept to use move semantics only when a strong exception guarantee exists (and use copy semantics otherwise). Let\u2019s update the code in the previous example as follows: 1 2 //std::pair moved_pair{std::move(my_pair)}; // comment out this line now std :: pair moved_pair { std :: move_if_noexcept ( my_pair )}; // and uncomment this line COPY Running the program again prints: 1 2 3 4 5 6 destroying MoveClass(empty) my_pair.first: MoveClass(13) destroying MoveClass(13) Error found: abort! my_pair.first: MoveClass(13) destroying MoveClass(13) As you can see, after the exception was thrown, the subobject my_pair.first still points to the value 13 . The move constructor of std::pair isn\u2019t noexcept (as of C++20), so std::move_if_noexcept returns my_pair as an l-value reference. This causes moved_pair to be created via the copy constructor (rather than the move constructor). The copy constructor can throw safely, because it doesn\u2019t modify the source object. The standard library uses std::move_if_noexcept often to optimize for functions that are noexcept . For example, std::vector::resize will use move semantics if the element type has a noexcept move constructor, and copy semantics otherwise. This means std::vector will generally operate faster with objects that have a noexcept move constructor. \u6ce8\u610f If a type has both potentially throwing move semantics and deleted copy semantics (the copy constructor and copy assignment operator are unavailable), then std::move_if_noexcept will waive the strong guarantee and invoke move semantics. This conditional waiving of the strong guarantee is ubiquitous in the standard library container classes, since they use std::move_if_noexcept often.","title":"std::move_if_noexcept to the rescue"},{"location":"M-6-std-unique-ptr/","tags":["move","C++14","C++17"],"text":"Key Takeaway \u667a\u80fd\u6307\u9488\u6c38\u8fdc\u90fd\u4e0d\u5e94\u8be5\u88ab\u52a8\u6001\u521b\u5efa\uff0c\u603b\u662f\u5e94\u8be5\u5728\u6808\u4e0a std::unique_ptr \u667a\u80fd\u6307\u9488\u662f\u5176\u7ba1\u7406\u8d44\u6e90\u7684\u552f\u4e00\u62e5\u6709\u8005\uff0c\u4e14\u53ea\u6709\u79fb\u52a8\u8bed\u4e49 std::unique_ptr \u5e76\u4e0d\u4e00\u5b9a\u603b\u662f\u7ba1\u7406\u7740\u67d0\u4e2a\u5bf9\u8c61\u2014\u2014\u6709\u53ef\u80fd\u662f\u5b83\u88ab\u521d\u59cb\u5316\u4e3a\u7a7a\uff08\u4f7f\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u6216 nullptr \u5b57\u9762\u91cf\u4f5c\u4e3a\u53c2\u6570\uff09\uff0c\u4e5f\u6709\u53ef\u80fd\u662f\u5b83\u7ba1\u7406\u7684\u8d44\u6e90\u88ab\u8f6c\u79fb\u4e86\uff0c\u901a\u8fc7\u76f4\u63a5\u5bf9\u5b83\u5224\u65adtrue\u8fd8\u662ffalse\u53ef\u4ee5\u77e5\u9053\u5b83\u662f\u5426\u6b63\u5728\u7ba1\u7406\u8d44\u6e90 \u6a21\u677f\u5316\u51fd\u6570 std::make_unique \u6bd4\u76f4\u63a5\u4f7f\u7528 std::unique_ptr \u66f4\u597d\u7528\u4e5f\u66f4\u5b89\u5168\uff0c\u5b83\u8fd4\u56de\u7684\u662f\u7ba1\u7406\u8d44\u6e90\u7684 std::unique_ptr \u51fd\u6570\u4f20\u53c2\u65f6\uff0c\u4e00\u822c\u4e0d\u5e0c\u671b\u8f6c\u79fb\u6240\u6709\u6743\uff0c\u5426\u5219\u8d44\u6e90\u4f1a\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\u3002\u6b64\u65f6\u6700\u597d\u4f20\u9012\u667a\u80fd\u6307\u9488\u7ba1\u7406\u7684\u8d44\u6e90\u672c\u8eab\uff08\u6309\u503c\u4f20\u9012\u6216\u6309\u5730\u5740\u4f20\u9012\u90fd\u53ef\u4ee5\uff09\uff0c\u4f46\u662f\u4e0d\u8981\u628a\u667a\u80fd\u6307\u9488\u7684\u5f15\u7528\u76f4\u63a5\u4f20\u8fdb\u53bb \u5728\u672c\u7ae0\u5f00\u59cb\u7684\u65f6\u5019\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u4f7f\u7528\u6307\u9488\u53ef\u80fd\u4f1a\u5f15\u53d1\u7684bug\u548c\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u51fd\u6570\u7684\u63d0\u524d\u8fd4\u56de\u3001\u5f02\u5e38\u7684\u629b\u51fa\u548c\u6307\u9488\u5220\u9664\u4e0d\u5f53\u90fd\u53ef\u80fd\u5bfc\u81f4\u4e0a\u8ff0\u95ee\u9898\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include void someFunction () { auto * ptr { new Resource () }; int x {}; std :: cout << \"Enter an integer: \" ; std :: cin >> x ; if ( x == 0 ) throw 0 ; // the function returns early, and ptr won\u2019t be deleted! // do stuff with ptr here delete ptr ; } \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u5b66\u4e60\u4e86 \u79fb\u52a8\u8bed\u4e49 \u7684\u57fa\u672c\u5185\u5bb9\uff0c\u63a5\u4e0b\u6765\u53ef\u4ee5\u7ee7\u7eed\u56de\u5230\u667a\u80fd\u6307\u9488\u7c7b\u7684\u8ba8\u8bba\u4e86\u3002\u56de\u5fc6\u4e00\u4e0b\uff0c\u667a\u80fd\u6307\u9488\u662f\u4e00\u4e2a\u7528\u4e8e\u7ba1\u7406\u52a8\u6001\u5206\u914d\u5185\u5b58\u7684\u7c7b\u3002\u5c3d\u7ba1\u667a\u80fd\u6307\u9488\u4e5f\u63d0\u4f9b\u5176\u4ed6\u529f\u80fd\uff0c\u4f46\u662f\u5b83\u6700\u672c\u8d28\u7684\u529f\u80fd\u8fd8\u662f\u7ba1\u7406\u52a8\u6001\u5206\u914d\u7684\u8d44\u6e90\uff0c\u5e76\u4e14\u786e\u4fdd\u5728\u6070\u5f53\u7684\u65f6\u5019\uff08\u901a\u5e38\u662f\u667a\u80fd\u6307\u9488 \u79bb\u5f00\u4f5c\u7528\u57df \u65f6\uff09\uff0c\u8be5\u52a8\u6001\u8d44\u6e90\u53ef\u4ee5\u88ab\u6b63\u786e\u5730\u6e05\u7406\u3002 \u56e0\u6b64\uff0c \u667a\u80fd\u6307\u9488\u672c\u8eab\u6c38\u8fdc\u90fd\u4e0d\u5e94\u8be5\u88ab\u52a8\u6001\u521b\u5efa \uff08\u5426\u5219\u667a\u80fd\u6307\u9488\u5bf9\u8c61\u672c\u8eab\u53ef\u80fd\u4f1a\u88ab\u5fd8\u8bb0\u91ca\u653e\uff0c\u4ece\u800c\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\uff09\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u667a\u80fd\u6307\u9488\u5e94\u8be5\u88ab\u521b\u5efa\u5728\u6808\u4e0a\uff08\u4f5c\u4e3a \u5c40\u90e8\u53d8\u91cf \u6216\u4f5c\u4e3a\u5176\u4ed6\u7c7b\u7684\u6210\u5458\uff09\uff0c\u6211\u4eec\u4fdd\u8bc1\u667a\u80fd\u6307\u9488\u5728\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff08\u5305\u542b\u8be5\u6307\u9488\u7684\u51fd\u6570\u6216\u5bf9\u8c61\u7ed3\u675f\u65f6\uff09\uff0c\u5b83\u6240\u62e5\u6709\u7684\u8d44\u6e90\u4f1a\u88ab\u6070\u5f53\u5730\u91ca\u653e\u3002 C++11 \u6807\u51c6\u5e93\u63d0\u4f9b\u4e864\u79cd\u667a\u80fd\u6307\u9488\u7c7b\uff1a std::auto_ptr (\u5728 C++17 \u4e2d\u5df2\u7ecf\u5220\u9664\u4e86)\uff0c std::unique_ptr \uff0c std::shared_ptr \u548c std::weak_ptr \u3002\u5176\u4e2d std::unique_ptr \u662f\u76ee\u524d\u6700\u4e3a\u5e38\u7528\u7684\u4e00\u4e2a\u6307\u9488\u7c7b\uff0c\u6240\u4ee5\u6211\u4eec\u7a0d\u540e\u4f1a\u9996\u5148\u4ecb\u7ecd\u5b83\uff0c\u5728\u63a5\u4e0b\u6765\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f1a\u4ecb\u7ecd std::shared_ptr \u548c std::weak_ptr \u3002 std::unique_ptr std::unique_ptr \u5728C++11\u4e2d\u88ab\u7528\u6765\u66ff\u4ee3 std::auto_ptr \u3002\u8be5\u667a\u80fd\u6307\u9488\u88ab\u7528\u6765\u7ba1\u7406\u52a8\u6001\u5206\u914d\u7684\u5bf9\u8c61\uff0c\u4e14\u8be5\u5bf9\u8c61\u5e76\u4e0d\u88ab\u591a\u4e2a\u5176\u4ed6\u5bf9\u8c61\u6240\u5171\u4eab\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c std::unique_ptr \u5355\u72ec\u62e5\u6709\u5b83\u6240\u7ba1\u7406\u7684\u8d44\u6e90\uff0c\u800c\u4e0d\u4f1a\u548c\u5176\u4ed6\u7c7b\u5171\u4eab\u8d44\u6e90\u7684\u6240\u6709\u6743\u3002 std::unique_ptr \u88ab\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d\u3002 \u518d\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include // for std::unique_ptr class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { // allocate a Resource object and have it owned by std::unique_ptr std :: unique_ptr < Resource > res { new Resource () }; return 0 ; } // res goes out of scope here, and the allocated Resource is destroyed \u56e0\u4e3a std::unique_ptr \u88ab\u5206\u914d\u5728\u6808\u4e0a\uff0c\u6240\u4ee5\u5b83\u6700\u7ec8\u4e00\u5b9a\u4f1a\u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u5f53\u5b83\u79bb\u5f00\u65f6\uff0c\u5b83\u4f1a\u786e\u4fdd Resource \u88ab\u5220\u9664\u3002 \u548c std::auto_ptr \u4e0d\u540c\u7684\u662f std::unique_ptr \u6b63\u786e\u5730\u5b9e\u73b0\u4e86\u79fb\u52a8\u8bed\u4e49\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include #include // for std::unique_ptr #include // for std::move class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { std :: unique_ptr < Resource > res1 { new Resource {} }; // \u521b\u5efa Resource std :: unique_ptr < Resource > res2 {}; // \u521d\u59cb\u5316\u4e3a\u7a7a\u6307\u9488 std :: cout << \"res1 is \" << ( res1 ? \"not null \\n \" : \"null \\n \" ); std :: cout << \"res2 is \" << ( res2 ? \"not null \\n \" : \"null \\n \" ); // res2 = res1; // \u4e0d\u80fd\u7f16\u8bd1: \u56e0\u4e3a\u62f7\u8d1d\u8d4b\u503c\u88ab\u7981\u7528\u4e86 res2 = std :: move ( res1 ); // res2 \u83b7\u53d6\u8d44\u6e90\u6240\u6709\u6743\uff0cres1 \u8bbe\u4e3a\u7a7a std :: cout << \"Ownership transferred \\n \" ; std :: cout << \"res1 is \" << ( res1 ? \"not null \\n \" : \"null \\n \" ); std :: cout << \"res2 is \" << ( res2 ? \"not null \\n \" : \"null \\n \" ); return 0 ; } // Resource destroyed here when res2 goes out of scope \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 2 3 4 5 6 7 Resource acquired res1 is not null res2 is null Ownership transferred res1 is null res2 is not null Resource destroyed \u56e0\u4e3a std::unique_ptr \u5728\u8bbe\u8ba1\u65f6\u8003\u8651\u4e86\u79fb\u52a8\u8bed\u4e49\uff0c\u6240\u4ee5\u5176 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u548c \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u90fd\u88ab\u7981\u7528\u4e86\u3002\u5982\u679c\u4f60\u60f3\u8981\u8f6c\u79fb std::unique_ptr \u7ba1\u7406\u7684\u8d44\u6e90\u7684\u8bdd\uff0c\u5c31\u5fc5\u987b\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49 \u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u79fb\u52a8\u8bed\u4e49\u662f\u901a\u8fc7 std::move \u5b9e\u73b0\u7684\uff08\u5b83\u628a res1 \u8f6c\u6362\u6210\u4e86\u4e00\u4e2a \u53f3\u503c \uff0c\u56e0\u6b64\u89e6\u53d1\u4e86\u79fb\u52a8\u8d4b\u503c\u800c\u4e0d\u662f\u62f7\u8d1d\u8d4b\u503c\uff09\u3002 \u8bbf\u95ee\u88ab\u667a\u80fd\u6307\u9488\u7ba1\u7406\u7684\u5bf9\u8c61 std::unique_ptr \u7c7b\u91cd\u8f7d\u4e86 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u548c \u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26-> \uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e24\u4e2a\u8fd0\u7b97\u7b26\u6765\u8fd4\u56de\u88ab\u7ba1\u7406\u7684\u5bf9\u8c61\u3002\u89e3\u5f15\u7528\u8fd0\u7b97\u7b26\u4f1a\u8fd4\u56de\u88ab\u7ba1\u7406\u8d44\u6e90\u7684\u5f15\u7528\uff0c\u800c operator-> \u8fd4\u56de\u4e00\u4e2a\u6307\u9488\u3002 \u8bb0\u4f4f\uff0c std::unique_ptr \u5e76\u4e0d\u4e00\u5b9a\u603b\u662f\u7ba1\u7406\u7740\u67d0\u4e2a\u5bf9\u8c61\u2014\u2014\u6709\u53ef\u80fd\u662f\u5b83\u88ab\u521d\u59cb\u5316\u4e3a\u7a7a\uff08\u4f7f\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u6216 nullptr \u5b57\u9762\u91cf\u4f5c\u4e3a\u53c2\u6570\uff09\uff0c\u4e5f\u6709\u53ef\u80fd\u662f\u5b83\u7ba1\u7406\u7684\u8d44\u6e90\u88ab\u8f6c\u79fb\u4e86 \u3002 \u6240\u4ee5\u5728\u4f7f\u7528\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u4e4b\u524d\uff0c\u5fc5\u987b\u9996\u5148\u68c0\u67e5 std::unique_ptr \u662f\u5426\u6709\u8d44\u6e90\u3002 \u56e0\u4e3a std::unique_ptr \u4f1a\u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a\u5e03\u5c14\u7c7b\u578b\uff0c\u6240\u4ee5\u53ea\u9700\u8981\u5bf9\u8be5\u6307\u9488\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\u5c31\u53ef\u4ee5\uff0c\u5f53\u8fd4\u56de true \u65f6\u8bf4\u660e\u5b83\u5305\u542b\u8d44\u6e90\u3002 Here\u2019s an example of this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include // for std::unique_ptr class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } friend std :: ostream & operator << ( std :: ostream & out , const Resource & res ) { out << \"I am a resource\" ; return out ; } }; int main () { std :: unique_ptr < Resource > res { new Resource {} }; if ( res ) // use implicit cast to bool to ensure res contains a Resource std :: cout << * res << '\\n' ; // print the Resource that res is owning return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 Resource acquired I am a resource Resource destroyed \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u91cd\u8f7d\u7684 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u6765\u83b7\u53d6 std::unique_ptr res \u7ba1\u7406\u7684 Resource \u5bf9\u8c61\uff0c\u7136\u540e\u5c06\u5176\u9001\u53bb std::cout \u6253\u5370\u3002 std::unique_ptr \u548c\u6570\u7ec4 \u548c std::auto_ptr \u4e0d\u4e00\u6837\u7684\u662f\uff0c std::unique_ptr \u8db3\u591f\u667a\u80fd\uff0c\u5b83\u61c2\u5f97\u4f7f\u7528\u6070\u5f53\u7684 delete \u53bb\u5220\u9664\u5185\u5b58\uff08\u666e\u901a delete \u6216\u6570\u7ec4 delete \uff09\uff0c\u6240\u4ee5\uff0c std::unique_ptr \u65e2\u53ef\u4ee5\u7528\u4e8e\u4e00\u822c\u5bf9\u8c61\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6570\u7ec4\u3002 \u4f46\u662f\uff0c \u76f8\u5bf9\u4e8e\u4f7f\u7528 std::unique_ptr \u7ba1\u7406\u4e00\u4e2a\u56fa\u5b9a\u6570\u7ec4\u6216C\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528 std::array \u6216\u8005 std::vector (\u6216\u8005 std::string ) \u603b\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002 \u6700\u4f73\u5b9e\u8df5 \u76f8\u5bf9\u4e8e\u4f7f\u7528 std::unique_ptr \u7ba1\u7406\u4e00\u4e2a\u56fa\u5b9a\u6570\u7ec4\u3001\u52a8\u6001\u6570\u7ec4\u6216C\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528 std::array \u6216\u8005 std::vector (\u6216\u8005 std::string ) \u603b\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002 std::make_unique C++14 \u65b0\u589e\u4e86\u4e00\u4e2a\u540d\u4e3a std::make_unique() \u7684\u51fd\u6570\u3002\u8fd9\u4e2a\u6a21\u677f\u5316\u51fd\u6570\u57fa\u4e8e\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\u6784\u5efa\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528\u4f20\u5165\u7684\u53c2\u6570\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include // for std::unique_ptr \u548c std::make_unique #include class Fraction { private : int m_numerator { 0 }; int m_denominator { 1 }; public : Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } }; int main () { // \u521b\u5efa\u4e00\u4e2a\u52a8\u6001\u5206\u914d\u7684 Fraction // \u8fd9\u91cc\u53ef\u4ee5\u914d\u5408\u4f7f\u7528\u81ea\u52a8\u7c7b\u578b\u63a8\u65ad\u83b7\u5f97\u66f4\u597d\u7684\u6548\u679c auto f1 { std :: make_unique < Fraction > ( 3 , 5 ) }; std :: cout << * f1 << '\\n' ; // \u521b\u5efa\u4e00\u4e2a\u52a8\u6001\u5206\u914d\u7684 Fraction \u6570\u7ec4\uff0c\u957f\u5ea6\u4e3a4 auto f2 { std :: make_unique < Fraction [] > ( 4 ) }; std :: cout << f2 [ 0 ] << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 3/5 0/1 \u4f7f\u7528 std::make_unique() \u662f\u4e00\u79cd\u53ef\u9009\u7684\u529e\u6cd5\uff0c\u4f46\u662f\u76f8\u5bf9\u4e8e\u76f4\u63a5\u521b\u5efa std::unique_ptr \uff0c\u6211\u4eec\u66f4\u63a8\u8350\u8fd9\u79cd\u505a\u6cd5\u3002\u4e0d\u5173\u56e0\u4e3a\u4f7f\u7528 std::make_unique \u7684\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff08\u5c24\u5176\u662f\u4f7f\u7528\u4e86 \u7c7b\u578b\u63a8\u65ad \u540e\uff09\uff0c\u5b83\u8fd8\u53ef\u4ee5\u89e3\u51b3C++\u53c2\u6570\u6c42\u503c\u987a\u5e8f\u6ca1\u6709\u89c4\u8303\u800c\u5f15\u53d1\u7684\u5f02\u5e38\u5b89\u5168\u95ee\u9898\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528 std::make_unique() \u6765\u4ee3\u66ff std::unique_ptr \u548c new \u3002 \u5f02\u5e38\u5b89\u5168\u95ee\u9898 \u5982\u679c\u4f60\u5bf9\u4e0a\u6587\u63d0\u5230\u7684 \u5f02\u5e38\u5b89\u5168\u95ee\u9898 \u611f\u5230\u597d\u5947\u7684\u8bdd\uff0c\u8fd9\u4e00\u5c0f\u7ed3\u6211\u4eec\u4f1a\u8be6\u7ec6\u8fdb\u884c\u4ecb\u7ecd\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 some_function ( std :: unique_ptr < T > ( new T ), function_that_can_throw_exception ()); \u5bf9\u4e8e\u5982\u4f55\u6267\u884c\u4e0a\u8ff0\u51fd\u6570\u8c03\u7528\uff0c\u7f16\u8bd1\u5668\u6709\u5f88\u5927\u7684\u7075\u6d3b\u6027\u3002\u5b83\u53ef\u4ee5\u5148\u521b\u5efa\u65b0\u7684\u7c7b\u578bT\uff0c\u7136\u540e\u8c03\u7528 function_that_can_throw_exception() \u5417\uff0c\u7136\u540e\u521b\u5efa std::unique_ptr \u53bb\u7ba1\u7406\u52a8\u6001\u5206\u914d\u7684T\u3002\u5982\u679c function_that_can_throw_exception() \u629b\u51fa\u4e86\u4f9d\u6b21\uff0c\u90a3\u4e48T\u5728\u88ab\u5206\u914d\u5185\u5b58\u540e\uff0c\u6ca1\u6709\u88ab\u91ca\u653e\uff0c\u56e0\u4e3a\u7528\u4e8e\u7ba1\u7406\u5b83\u7684\u667a\u80fd\u6307\u9488\u8fd8\u6ca1\u6709\u88ab\u521b\u5efa\u3002\u663e\u7136\u8fd9\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002 std::make_unique() \u514b\u670d\u4e86\u6574\u4e2a\u95ee\u9898\uff0c\u56e0\u4e3a\u5bf9\u8c61T\u548c std::unique_ptr \u7684\u521b\u5efa\u90fd\u662f\u5728 std::make_unique() \u4e2d\u5b8c\u6210\u7684\uff0c\u6267\u884c\u987a\u5e8f\u6ca1\u6709\u6b67\u4e49\u3002 \u51fd\u6570\u8fd4\u56de std::unique_ptr std::unique_ptr \u53ef\u4ee5\u5b89\u5168\u5730\u4ece\u51fd\u6570\u4e2d \u6309\u503c\u8fd4\u56de \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // for std::unique_ptr std :: unique_ptr < Resource > createResource () { return std :: make_unique < Resource > (); //\u533f\u540d\u5bf9\u8c61\u662f\u53f3\u503c\uff0c\u6240\u4ee5\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49 } int main () { auto ptr { createResource () }; // do whatever return 0 ; } \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c createResource() \u6309\u503c\u8fd4\u56de\u4e86 std::unique_ptr \u3002\u5982\u679c\u8fd9\u4e2a\u503c\u6ca1\u6709\u88ab\u8d4b\u503c\u7ed9\u4efb\u4f55\u5176\u4ed6\u7684\u5bf9\u8c61\uff0c\u90a3\u4e48\u4e34\u65f6\u5bf9\u8c61\u5c31\u4f1a\u79bb\u5f00\u4f5c\u7528\u57df\uff0c Resource \u4e5f\u4f1a\u88ab\u6e05\u7406\u3002\u5982\u679c\u5b83\u88ab\u8d4b\u503c\u7ed9\u5176\u4ed6\u5bf9\u8c61 (\u50cf main() \u4e2d\u90a3\u6837)\uff0c\u5728C++14\u6216\u66f4\u65e9\u7684\u4ee3\u7801\u4e2d\uff0c \u79fb\u52a8\u8bed\u4e49 \u4f1a\u88ab\u7528\u6765\u5c06\u8fd4\u56de\u503c Resource \u8f6c\u79fb\u7ed9\u88ab\u8d4b\u503c\u7684\u5bf9\u8c61\uff08\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684 ptr \uff09\u3002\u5728C++17\u6216\u66f4\u65b0\u7684\u4ee3\u7801\u4e2d\uff0c\u8fd9\u4e2a\u8fd4\u56de\u503c\u4f1a\u88ab\u7701\u7565\uff0c\u56e0\u6b64\u901a\u8fc7 std::unique_ptr \u8fd4\u56de\u8d44\u6e90\u8fd4\u56de\u539f\u59cb\u6307\u9488\u8981\u5b89\u5168\u7684\u591a\uff01 \u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u4f60\u4e0d\u5e94\u8be5\u5c06 std::unique_ptr \u6309\u6307\u9488\u8fd4\u56de\uff08\u6c38\u8fdc\u4e0d\u8981\uff09\u6216\u8005\u6309\u5f15\u7528\u8fd4\u56de\uff08\u9664\u975e\u4f60\u6709\u8db3\u591f\u7684\u7406\u7531\uff09\u3002 \u4f20\u9012 std::unique_ptr \u7ed9\u51fd\u6570 \u5982\u679c\u4f60\u9700\u8981\u51fd\u6570\u83b7\u53d6 std::unique_ptr \u7ba1\u7406\u7684\u8d44\u6e90\uff0c\u4f7f\u7528 \u6309\u503c\u4f20\u9012 \u3002\u6ce8\u610f\uff0c\u7531\u4e8e \u62f7\u8d1d\u8bed\u4e49 \u88ab\u7981\u7528\u4e86\uff0c\u6240\u4ee5\u5fc5\u987b\u5bf9\u5b9e\u9645\u4f20\u5165\u7684\u53c2\u6570\u8c03\u7528 std::move \u3002 \u8bd1\u8005\u6ce8 \u9700\u4e0d\u9700\u8981\u4f7f\u7528 std::move \u8981\u770b\u4f20\u5165\u7684\u662f\u5de6\u503c\u8fd8\u662f\u53f3\u503c\uff0c\u5de6\u503c\u624d\u9700\u8981\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include #include // for std::unique_ptr #include // for std::move class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } friend std :: ostream & operator << ( std :: ostream & out , const Resource & res ) { out << \"I am a resource\" ; return out ; } }; void takeOwnership ( std :: unique_ptr < Resource > res ) { if ( res ) std :: cout << * res << '\\n' ; } // the Resource is destroyed here int main () { auto ptr { std :: make_unique < Resource > () }; // takeOwnership(ptr); // This doesn't work, need to use move semantics takeOwnership ( std :: move ( ptr )); // ok: \u4f7f\u7528\u79fb\u52a8\u8bed\u4e49 std :: cout << \"Ending program \\n \" ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 3 4 Resource acquired I am a resource Resource destroyed Ending program \u6ce8\u610f\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c Resource \u7684\u6240\u6709\u6743\u88ab\u8f6c\u79fb\u7ed9\u4e86 takeOwnership() \uff0c\u6240\u4ee5 Resource \u4f1a\u5728 takeOwnership() \u51fd\u6570\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\uff0c\u800c\u4e0d\u662f main() \u3002 \u4f46\u662f\uff0c\u5927\u591a\u6570\u65f6\u5019\uff0c\u6211\u4eec\u4e0d\u4f1a\u5e0c\u671b\u67d0\u4e2a\u51fd\u6570\u62e5\u6709\u8d44\u6e90\u7684\u6240\u6709\u6743\u3002\u5c3d\u7ba1\u4f60\u53ef\u4ee5 \u6309\u5f15\u7528\u4f20\u9012 std::unique_ptr (\u8fd9\u6837\u4e00\u6765\u51fd\u6570\u5c31\u65e0\u9700\u8003\u8651\u6240\u6709\u6743\u76f4\u63a5\u4f7f\u7528\u5bf9\u8c61\u672c\u8eab)\uff0c\u4f46\u662f\u8fd9\u79cd\u505a\u6cd5\u53ea\u6709\u5728\u51fd\u6570\u53ef\u80fd\u9700\u8981\u6539\u53d8\u88ab\u7ba1\u7406\u7684\u5bf9\u8c61\u65f6\u624d\u4f7f\u7528\u3002 \u76f8\u53cd\uff0c\u6700\u597d\u662f \u76f4\u63a5\u4f20\u9012\u8d44\u6e90\u672c\u8eab (\u901a\u8fc7\u6307\u9488\u6216\u5f15\u7528\uff0c\u53d6\u51b3\u4e8enull\u662f\u5426\u662f\u6709\u6548\u53c2\u6570)\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u51fd\u6570\u5c31\u65e0\u9700\u5173\u5fc3\u8d44\u6e90\u662f\u5982\u4f55\u88ab\u7ba1\u7406\u7684\u4e86\u3002\u8981\u4ece std::unique_ptr \u4e2d\u83b7\u53d6\u539f\u59cb\u8d44\u6e90\u6307\u9488\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 get() \u6210\u5458\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include // for std::unique_ptr #include class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } friend std :: ostream & operator << ( std :: ostream & out , const Resource & res ) { out << \"I am a resource\" ; return out ; } }; // \u51fd\u6570\u503c\u4f1a\u4f7f\u7528\u8d44\u6e90\uff0c\u6240\u4ee5\u5b83\u53ea\u9700\u8981\u83b7\u53d6\u6307\u5411\u8d44\u6e90\u672c\u8eab\u7684\u6307\u9488\uff0c\u800c\u4e0d\u9700\u8981\u83b7\u53d6\u5b8c\u6574\u7684 std::unique_ptr \u7684\u5f15\u7528 void useResource ( Resource * res ) { if ( res ) std :: cout << * res << '\\n' ; else std :: cout << \"No resource \\n \" ; } int main () { auto ptr { std :: make_unique < Resource > () }; useResource ( ptr . get ()); // note: \u901a\u8fc7 get() \u83b7\u53d6\u88ab\u7ba1\u7406\u7684\u8d44\u6e90 std :: cout << \"Ending program \\n \" ; return 0 ; } // The Resource is destroyed here \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 2 3 4 Resource acquired I am a resource Ending program Resource destroyed std::unique_ptr \u548c\u7c7b \u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5c06 std::unique_ptr \u4f5c\u4e3a\u4e00\u4e2a\u7c7b\u6210\u5458\uff08\u5f62\u6210 \u7ec4\u5408\u5173\u7cfb \u5173\u7cfb\uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4f60\u5c31\u4e0d\u9700\u8981\u5728\u7c7b\u7684\u6790\u6784\u51fd\u6570\u4e2d\u5220\u9664\u8be5\u52a8\u5185\u5b58\uff0c\u56e0\u4e3a std::unique_ptr \u4f1a\u5728\u7c7b\u5bf9\u8c61\u9500\u6bc1\u65f6\u81ea\u52a8\u9500\u6bc1\u3002 \u4f46\u662f\uff0c\u5982\u679c\u7c7b\u6ca1\u6709\u88ab\u6b63\u786e\u5730\u9500\u6bc1\u7684\u8bdd\uff08\u4f8b\u5982\uff0c\u52a8\u6001\u5206\u914d\u4e86\u5185\u5b58\u4f46\u6ca1\u6709\u88ab\u91ca\u653e\uff09\uff0c\u4e8e\u662f std::unique_ptr \u6210\u5458\u4e5f\u4e0d\u4f1a\u88ab\u9500\u6bc1\uff0c\u6240\u4ee5\u5b83\u7ba1\u7406\u7684\u8d44\u6e90\u4e5f\u4e0d\u4f1a\u88ab\u91ca\u653e\u3002 std::unique_ptr \u7684\u8bef\u7528 std::unique_ptrs \u6709\u4e24\u79cd\u5178\u578b\u7684\u8bef\u7528\uff0c\u4f46\u662f\u5b83\u4eec\u5176\u5b9e\u5f88\u5bb9\u6613\u907f\u514d\u3002\u9996\u5148\uff0c\u4e0d\u8981\u8ba9\u591a\u4e2a\u7c7b\u7ba1\u7406\u540c\u4e00\u4e2a\u8d44\u6e90\uff0c\u4f8b\u5982\uff1a 1 2 3 Resource * res { new Resource () }; std :: unique_ptr < Resource > res1 { res }; std :: unique_ptr < Resource > res2 { res }; \u5c3d\u7ba1\u8bed\u6cd5\u4e0a\u5b8c\u5168\u6b63\u786e\uff0c\u4f46\u662f\u6700\u7ec8\u7684\u7ed3\u679c\u662f res1 \u548c res2 \u90fd\u4f1a\u53bb\u5220\u9664\u8d44\u6e90 Resource \uff0c\u8fdb\u800c\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u7b2c\u4e8c\uff0c\u4e0d\u8981\u624b\u52a8\u91ca\u653e\u5df2\u7ecf\u5728 std::unique_ptr \u7ba1\u7406\u4e0b\u7684\u8d44\u6e90\uff1a 1 2 3 Resource * res { new Resource () }; std :: unique_ptr < Resource > res1 { res }; delete res ; \u8fd9\u4e48\u505a\u7684\u8bdd\uff0c std::unique_ptr \u5c31\u4f1a\u518d\u6b21\u5220\u9664\u8fd9\u4e2a\u5df2\u7ecf\u88ab\u91ca\u653e\u7684\u8d44\u6e90\uff0c\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4f7f\u7528 std::make_unique() \u53ef\u4ee5\u907f\u514d\u4e0a\u8ff0\u4e24\u4e2a\u95ee\u9898\u3002","title":"M.6 \u2014 std::unique_ptr"},{"location":"M-6-std-unique-ptr/#stdunique_ptr","text":"std::unique_ptr \u5728C++11\u4e2d\u88ab\u7528\u6765\u66ff\u4ee3 std::auto_ptr \u3002\u8be5\u667a\u80fd\u6307\u9488\u88ab\u7528\u6765\u7ba1\u7406\u52a8\u6001\u5206\u914d\u7684\u5bf9\u8c61\uff0c\u4e14\u8be5\u5bf9\u8c61\u5e76\u4e0d\u88ab\u591a\u4e2a\u5176\u4ed6\u5bf9\u8c61\u6240\u5171\u4eab\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c std::unique_ptr \u5355\u72ec\u62e5\u6709\u5b83\u6240\u7ba1\u7406\u7684\u8d44\u6e90\uff0c\u800c\u4e0d\u4f1a\u548c\u5176\u4ed6\u7c7b\u5171\u4eab\u8d44\u6e90\u7684\u6240\u6709\u6743\u3002 std::unique_ptr \u88ab\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d\u3002 \u518d\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include // for std::unique_ptr class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { // allocate a Resource object and have it owned by std::unique_ptr std :: unique_ptr < Resource > res { new Resource () }; return 0 ; } // res goes out of scope here, and the allocated Resource is destroyed \u56e0\u4e3a std::unique_ptr \u88ab\u5206\u914d\u5728\u6808\u4e0a\uff0c\u6240\u4ee5\u5b83\u6700\u7ec8\u4e00\u5b9a\u4f1a\u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u5f53\u5b83\u79bb\u5f00\u65f6\uff0c\u5b83\u4f1a\u786e\u4fdd Resource \u88ab\u5220\u9664\u3002 \u548c std::auto_ptr \u4e0d\u540c\u7684\u662f std::unique_ptr \u6b63\u786e\u5730\u5b9e\u73b0\u4e86\u79fb\u52a8\u8bed\u4e49\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include #include // for std::unique_ptr #include // for std::move class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { std :: unique_ptr < Resource > res1 { new Resource {} }; // \u521b\u5efa Resource std :: unique_ptr < Resource > res2 {}; // \u521d\u59cb\u5316\u4e3a\u7a7a\u6307\u9488 std :: cout << \"res1 is \" << ( res1 ? \"not null \\n \" : \"null \\n \" ); std :: cout << \"res2 is \" << ( res2 ? \"not null \\n \" : \"null \\n \" ); // res2 = res1; // \u4e0d\u80fd\u7f16\u8bd1: \u56e0\u4e3a\u62f7\u8d1d\u8d4b\u503c\u88ab\u7981\u7528\u4e86 res2 = std :: move ( res1 ); // res2 \u83b7\u53d6\u8d44\u6e90\u6240\u6709\u6743\uff0cres1 \u8bbe\u4e3a\u7a7a std :: cout << \"Ownership transferred \\n \" ; std :: cout << \"res1 is \" << ( res1 ? \"not null \\n \" : \"null \\n \" ); std :: cout << \"res2 is \" << ( res2 ? \"not null \\n \" : \"null \\n \" ); return 0 ; } // Resource destroyed here when res2 goes out of scope \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a 1 2 3 4 5 6 7 Resource acquired res1 is not null res2 is null Ownership transferred res1 is null res2 is not null Resource destroyed \u56e0\u4e3a std::unique_ptr \u5728\u8bbe\u8ba1\u65f6\u8003\u8651\u4e86\u79fb\u52a8\u8bed\u4e49\uff0c\u6240\u4ee5\u5176 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u548c \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u90fd\u88ab\u7981\u7528\u4e86\u3002\u5982\u679c\u4f60\u60f3\u8981\u8f6c\u79fb std::unique_ptr \u7ba1\u7406\u7684\u8d44\u6e90\u7684\u8bdd\uff0c\u5c31\u5fc5\u987b\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49 \u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u79fb\u52a8\u8bed\u4e49\u662f\u901a\u8fc7 std::move \u5b9e\u73b0\u7684\uff08\u5b83\u628a res1 \u8f6c\u6362\u6210\u4e86\u4e00\u4e2a \u53f3\u503c \uff0c\u56e0\u6b64\u89e6\u53d1\u4e86\u79fb\u52a8\u8d4b\u503c\u800c\u4e0d\u662f\u62f7\u8d1d\u8d4b\u503c\uff09\u3002","title":"std::unique_ptr"},{"location":"M-6-std-unique-ptr/#_1","text":"std::unique_ptr \u7c7b\u91cd\u8f7d\u4e86 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u548c \u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26-> \uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e24\u4e2a\u8fd0\u7b97\u7b26\u6765\u8fd4\u56de\u88ab\u7ba1\u7406\u7684\u5bf9\u8c61\u3002\u89e3\u5f15\u7528\u8fd0\u7b97\u7b26\u4f1a\u8fd4\u56de\u88ab\u7ba1\u7406\u8d44\u6e90\u7684\u5f15\u7528\uff0c\u800c operator-> \u8fd4\u56de\u4e00\u4e2a\u6307\u9488\u3002 \u8bb0\u4f4f\uff0c std::unique_ptr \u5e76\u4e0d\u4e00\u5b9a\u603b\u662f\u7ba1\u7406\u7740\u67d0\u4e2a\u5bf9\u8c61\u2014\u2014\u6709\u53ef\u80fd\u662f\u5b83\u88ab\u521d\u59cb\u5316\u4e3a\u7a7a\uff08\u4f7f\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u6216 nullptr \u5b57\u9762\u91cf\u4f5c\u4e3a\u53c2\u6570\uff09\uff0c\u4e5f\u6709\u53ef\u80fd\u662f\u5b83\u7ba1\u7406\u7684\u8d44\u6e90\u88ab\u8f6c\u79fb\u4e86 \u3002 \u6240\u4ee5\u5728\u4f7f\u7528\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u4e4b\u524d\uff0c\u5fc5\u987b\u9996\u5148\u68c0\u67e5 std::unique_ptr \u662f\u5426\u6709\u8d44\u6e90\u3002 \u56e0\u4e3a std::unique_ptr \u4f1a\u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a\u5e03\u5c14\u7c7b\u578b\uff0c\u6240\u4ee5\u53ea\u9700\u8981\u5bf9\u8be5\u6307\u9488\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\u5c31\u53ef\u4ee5\uff0c\u5f53\u8fd4\u56de true \u65f6\u8bf4\u660e\u5b83\u5305\u542b\u8d44\u6e90\u3002 Here\u2019s an example of this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include // for std::unique_ptr class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } friend std :: ostream & operator << ( std :: ostream & out , const Resource & res ) { out << \"I am a resource\" ; return out ; } }; int main () { std :: unique_ptr < Resource > res { new Resource {} }; if ( res ) // use implicit cast to bool to ensure res contains a Resource std :: cout << * res << '\\n' ; // print the Resource that res is owning return 0 ; } \u6253\u5370\u7ed3\u679c\uff1a 1 2 3 Resource acquired I am a resource Resource destroyed \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u91cd\u8f7d\u7684 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u6765\u83b7\u53d6 std::unique_ptr res \u7ba1\u7406\u7684 Resource \u5bf9\u8c61\uff0c\u7136\u540e\u5c06\u5176\u9001\u53bb std::cout \u6253\u5370\u3002","title":"\u8bbf\u95ee\u88ab\u667a\u80fd\u6307\u9488\u7ba1\u7406\u7684\u5bf9\u8c61"},{"location":"M-6-std-unique-ptr/#stdunique_ptr_1","text":"\u548c std::auto_ptr \u4e0d\u4e00\u6837\u7684\u662f\uff0c std::unique_ptr \u8db3\u591f\u667a\u80fd\uff0c\u5b83\u61c2\u5f97\u4f7f\u7528\u6070\u5f53\u7684 delete \u53bb\u5220\u9664\u5185\u5b58\uff08\u666e\u901a delete \u6216\u6570\u7ec4 delete \uff09\uff0c\u6240\u4ee5\uff0c std::unique_ptr \u65e2\u53ef\u4ee5\u7528\u4e8e\u4e00\u822c\u5bf9\u8c61\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6570\u7ec4\u3002 \u4f46\u662f\uff0c \u76f8\u5bf9\u4e8e\u4f7f\u7528 std::unique_ptr \u7ba1\u7406\u4e00\u4e2a\u56fa\u5b9a\u6570\u7ec4\u6216C\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528 std::array \u6216\u8005 std::vector (\u6216\u8005 std::string ) \u603b\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002 \u6700\u4f73\u5b9e\u8df5 \u76f8\u5bf9\u4e8e\u4f7f\u7528 std::unique_ptr \u7ba1\u7406\u4e00\u4e2a\u56fa\u5b9a\u6570\u7ec4\u3001\u52a8\u6001\u6570\u7ec4\u6216C\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528 std::array \u6216\u8005 std::vector (\u6216\u8005 std::string ) \u603b\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002","title":"std::unique_ptr \u548c\u6570\u7ec4"},{"location":"M-6-std-unique-ptr/#stdmake_unique","text":"C++14 \u65b0\u589e\u4e86\u4e00\u4e2a\u540d\u4e3a std::make_unique() \u7684\u51fd\u6570\u3002\u8fd9\u4e2a\u6a21\u677f\u5316\u51fd\u6570\u57fa\u4e8e\u4e00\u4e2a\u6a21\u677f\u7c7b\u578b\u6784\u5efa\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528\u4f20\u5165\u7684\u53c2\u6570\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include // for std::unique_ptr \u548c std::make_unique #include class Fraction { private : int m_numerator { 0 }; int m_denominator { 1 }; public : Fraction ( int numerator = 0 , int denominator = 1 ) : m_numerator { numerator }, m_denominator { denominator } { } friend std :: ostream & operator << ( std :: ostream & out , const Fraction & f1 ) { out << f1 . m_numerator << '/' << f1 . m_denominator ; return out ; } }; int main () { // \u521b\u5efa\u4e00\u4e2a\u52a8\u6001\u5206\u914d\u7684 Fraction // \u8fd9\u91cc\u53ef\u4ee5\u914d\u5408\u4f7f\u7528\u81ea\u52a8\u7c7b\u578b\u63a8\u65ad\u83b7\u5f97\u66f4\u597d\u7684\u6548\u679c auto f1 { std :: make_unique < Fraction > ( 3 , 5 ) }; std :: cout << * f1 << '\\n' ; // \u521b\u5efa\u4e00\u4e2a\u52a8\u6001\u5206\u914d\u7684 Fraction \u6570\u7ec4\uff0c\u957f\u5ea6\u4e3a4 auto f2 { std :: make_unique < Fraction [] > ( 4 ) }; std :: cout << f2 [ 0 ] << '\\n' ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 3/5 0/1 \u4f7f\u7528 std::make_unique() \u662f\u4e00\u79cd\u53ef\u9009\u7684\u529e\u6cd5\uff0c\u4f46\u662f\u76f8\u5bf9\u4e8e\u76f4\u63a5\u521b\u5efa std::unique_ptr \uff0c\u6211\u4eec\u66f4\u63a8\u8350\u8fd9\u79cd\u505a\u6cd5\u3002\u4e0d\u5173\u56e0\u4e3a\u4f7f\u7528 std::make_unique \u7684\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff08\u5c24\u5176\u662f\u4f7f\u7528\u4e86 \u7c7b\u578b\u63a8\u65ad \u540e\uff09\uff0c\u5b83\u8fd8\u53ef\u4ee5\u89e3\u51b3C++\u53c2\u6570\u6c42\u503c\u987a\u5e8f\u6ca1\u6709\u89c4\u8303\u800c\u5f15\u53d1\u7684\u5f02\u5e38\u5b89\u5168\u95ee\u9898\u3002 \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528 std::make_unique() \u6765\u4ee3\u66ff std::unique_ptr \u548c new \u3002","title":"std::make_unique"},{"location":"M-6-std-unique-ptr/#_2","text":"\u5982\u679c\u4f60\u5bf9\u4e0a\u6587\u63d0\u5230\u7684 \u5f02\u5e38\u5b89\u5168\u95ee\u9898 \u611f\u5230\u597d\u5947\u7684\u8bdd\uff0c\u8fd9\u4e00\u5c0f\u7ed3\u6211\u4eec\u4f1a\u8be6\u7ec6\u8fdb\u884c\u4ecb\u7ecd\u3002 \u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 some_function ( std :: unique_ptr < T > ( new T ), function_that_can_throw_exception ()); \u5bf9\u4e8e\u5982\u4f55\u6267\u884c\u4e0a\u8ff0\u51fd\u6570\u8c03\u7528\uff0c\u7f16\u8bd1\u5668\u6709\u5f88\u5927\u7684\u7075\u6d3b\u6027\u3002\u5b83\u53ef\u4ee5\u5148\u521b\u5efa\u65b0\u7684\u7c7b\u578bT\uff0c\u7136\u540e\u8c03\u7528 function_that_can_throw_exception() \u5417\uff0c\u7136\u540e\u521b\u5efa std::unique_ptr \u53bb\u7ba1\u7406\u52a8\u6001\u5206\u914d\u7684T\u3002\u5982\u679c function_that_can_throw_exception() \u629b\u51fa\u4e86\u4f9d\u6b21\uff0c\u90a3\u4e48T\u5728\u88ab\u5206\u914d\u5185\u5b58\u540e\uff0c\u6ca1\u6709\u88ab\u91ca\u653e\uff0c\u56e0\u4e3a\u7528\u4e8e\u7ba1\u7406\u5b83\u7684\u667a\u80fd\u6307\u9488\u8fd8\u6ca1\u6709\u88ab\u521b\u5efa\u3002\u663e\u7136\u8fd9\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002 std::make_unique() \u514b\u670d\u4e86\u6574\u4e2a\u95ee\u9898\uff0c\u56e0\u4e3a\u5bf9\u8c61T\u548c std::unique_ptr \u7684\u521b\u5efa\u90fd\u662f\u5728 std::make_unique() \u4e2d\u5b8c\u6210\u7684\uff0c\u6267\u884c\u987a\u5e8f\u6ca1\u6709\u6b67\u4e49\u3002","title":"\u5f02\u5e38\u5b89\u5168\u95ee\u9898"},{"location":"M-6-std-unique-ptr/#stdunique_ptr_2","text":"std::unique_ptr \u53ef\u4ee5\u5b89\u5168\u5730\u4ece\u51fd\u6570\u4e2d \u6309\u503c\u8fd4\u56de \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include // for std::unique_ptr std :: unique_ptr < Resource > createResource () { return std :: make_unique < Resource > (); //\u533f\u540d\u5bf9\u8c61\u662f\u53f3\u503c\uff0c\u6240\u4ee5\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49 } int main () { auto ptr { createResource () }; // do whatever return 0 ; } \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c createResource() \u6309\u503c\u8fd4\u56de\u4e86 std::unique_ptr \u3002\u5982\u679c\u8fd9\u4e2a\u503c\u6ca1\u6709\u88ab\u8d4b\u503c\u7ed9\u4efb\u4f55\u5176\u4ed6\u7684\u5bf9\u8c61\uff0c\u90a3\u4e48\u4e34\u65f6\u5bf9\u8c61\u5c31\u4f1a\u79bb\u5f00\u4f5c\u7528\u57df\uff0c Resource \u4e5f\u4f1a\u88ab\u6e05\u7406\u3002\u5982\u679c\u5b83\u88ab\u8d4b\u503c\u7ed9\u5176\u4ed6\u5bf9\u8c61 (\u50cf main() \u4e2d\u90a3\u6837)\uff0c\u5728C++14\u6216\u66f4\u65e9\u7684\u4ee3\u7801\u4e2d\uff0c \u79fb\u52a8\u8bed\u4e49 \u4f1a\u88ab\u7528\u6765\u5c06\u8fd4\u56de\u503c Resource \u8f6c\u79fb\u7ed9\u88ab\u8d4b\u503c\u7684\u5bf9\u8c61\uff08\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684 ptr \uff09\u3002\u5728C++17\u6216\u66f4\u65b0\u7684\u4ee3\u7801\u4e2d\uff0c\u8fd9\u4e2a\u8fd4\u56de\u503c\u4f1a\u88ab\u7701\u7565\uff0c\u56e0\u6b64\u901a\u8fc7 std::unique_ptr \u8fd4\u56de\u8d44\u6e90\u8fd4\u56de\u539f\u59cb\u6307\u9488\u8981\u5b89\u5168\u7684\u591a\uff01 \u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u4f60\u4e0d\u5e94\u8be5\u5c06 std::unique_ptr \u6309\u6307\u9488\u8fd4\u56de\uff08\u6c38\u8fdc\u4e0d\u8981\uff09\u6216\u8005\u6309\u5f15\u7528\u8fd4\u56de\uff08\u9664\u975e\u4f60\u6709\u8db3\u591f\u7684\u7406\u7531\uff09\u3002","title":"\u51fd\u6570\u8fd4\u56de std::unique_ptr"},{"location":"M-6-std-unique-ptr/#stdunique_ptr_3","text":"\u5982\u679c\u4f60\u9700\u8981\u51fd\u6570\u83b7\u53d6 std::unique_ptr \u7ba1\u7406\u7684\u8d44\u6e90\uff0c\u4f7f\u7528 \u6309\u503c\u4f20\u9012 \u3002\u6ce8\u610f\uff0c\u7531\u4e8e \u62f7\u8d1d\u8bed\u4e49 \u88ab\u7981\u7528\u4e86\uff0c\u6240\u4ee5\u5fc5\u987b\u5bf9\u5b9e\u9645\u4f20\u5165\u7684\u53c2\u6570\u8c03\u7528 std::move \u3002 \u8bd1\u8005\u6ce8 \u9700\u4e0d\u9700\u8981\u4f7f\u7528 std::move \u8981\u770b\u4f20\u5165\u7684\u662f\u5de6\u503c\u8fd8\u662f\u53f3\u503c\uff0c\u5de6\u503c\u624d\u9700\u8981\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include #include // for std::unique_ptr #include // for std::move class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } friend std :: ostream & operator << ( std :: ostream & out , const Resource & res ) { out << \"I am a resource\" ; return out ; } }; void takeOwnership ( std :: unique_ptr < Resource > res ) { if ( res ) std :: cout << * res << '\\n' ; } // the Resource is destroyed here int main () { auto ptr { std :: make_unique < Resource > () }; // takeOwnership(ptr); // This doesn't work, need to use move semantics takeOwnership ( std :: move ( ptr )); // ok: \u4f7f\u7528\u79fb\u52a8\u8bed\u4e49 std :: cout << \"Ending program \\n \" ; return 0 ; } \u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a 1 2 3 4 Resource acquired I am a resource Resource destroyed Ending program \u6ce8\u610f\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c Resource \u7684\u6240\u6709\u6743\u88ab\u8f6c\u79fb\u7ed9\u4e86 takeOwnership() \uff0c\u6240\u4ee5 Resource \u4f1a\u5728 takeOwnership() \u51fd\u6570\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1\uff0c\u800c\u4e0d\u662f main() \u3002 \u4f46\u662f\uff0c\u5927\u591a\u6570\u65f6\u5019\uff0c\u6211\u4eec\u4e0d\u4f1a\u5e0c\u671b\u67d0\u4e2a\u51fd\u6570\u62e5\u6709\u8d44\u6e90\u7684\u6240\u6709\u6743\u3002\u5c3d\u7ba1\u4f60\u53ef\u4ee5 \u6309\u5f15\u7528\u4f20\u9012 std::unique_ptr (\u8fd9\u6837\u4e00\u6765\u51fd\u6570\u5c31\u65e0\u9700\u8003\u8651\u6240\u6709\u6743\u76f4\u63a5\u4f7f\u7528\u5bf9\u8c61\u672c\u8eab)\uff0c\u4f46\u662f\u8fd9\u79cd\u505a\u6cd5\u53ea\u6709\u5728\u51fd\u6570\u53ef\u80fd\u9700\u8981\u6539\u53d8\u88ab\u7ba1\u7406\u7684\u5bf9\u8c61\u65f6\u624d\u4f7f\u7528\u3002 \u76f8\u53cd\uff0c\u6700\u597d\u662f \u76f4\u63a5\u4f20\u9012\u8d44\u6e90\u672c\u8eab (\u901a\u8fc7\u6307\u9488\u6216\u5f15\u7528\uff0c\u53d6\u51b3\u4e8enull\u662f\u5426\u662f\u6709\u6548\u53c2\u6570)\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u51fd\u6570\u5c31\u65e0\u9700\u5173\u5fc3\u8d44\u6e90\u662f\u5982\u4f55\u88ab\u7ba1\u7406\u7684\u4e86\u3002\u8981\u4ece std::unique_ptr \u4e2d\u83b7\u53d6\u539f\u59cb\u8d44\u6e90\u6307\u9488\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 get() \u6210\u5458\u51fd\u6570\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include // for std::unique_ptr #include class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } friend std :: ostream & operator << ( std :: ostream & out , const Resource & res ) { out << \"I am a resource\" ; return out ; } }; // \u51fd\u6570\u503c\u4f1a\u4f7f\u7528\u8d44\u6e90\uff0c\u6240\u4ee5\u5b83\u53ea\u9700\u8981\u83b7\u53d6\u6307\u5411\u8d44\u6e90\u672c\u8eab\u7684\u6307\u9488\uff0c\u800c\u4e0d\u9700\u8981\u83b7\u53d6\u5b8c\u6574\u7684 std::unique_ptr \u7684\u5f15\u7528 void useResource ( Resource * res ) { if ( res ) std :: cout << * res << '\\n' ; else std :: cout << \"No resource \\n \" ; } int main () { auto ptr { std :: make_unique < Resource > () }; useResource ( ptr . get ()); // note: \u901a\u8fc7 get() \u83b7\u53d6\u88ab\u7ba1\u7406\u7684\u8d44\u6e90 std :: cout << \"Ending program \\n \" ; return 0 ; } // The Resource is destroyed here \u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a 1 2 3 4 Resource acquired I am a resource Ending program Resource destroyed","title":"\u4f20\u9012 std::unique_ptr \u7ed9\u51fd\u6570"},{"location":"M-6-std-unique-ptr/#stdunique_ptr_4","text":"\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5c06 std::unique_ptr \u4f5c\u4e3a\u4e00\u4e2a\u7c7b\u6210\u5458\uff08\u5f62\u6210 \u7ec4\u5408\u5173\u7cfb \u5173\u7cfb\uff09\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4f60\u5c31\u4e0d\u9700\u8981\u5728\u7c7b\u7684\u6790\u6784\u51fd\u6570\u4e2d\u5220\u9664\u8be5\u52a8\u5185\u5b58\uff0c\u56e0\u4e3a std::unique_ptr \u4f1a\u5728\u7c7b\u5bf9\u8c61\u9500\u6bc1\u65f6\u81ea\u52a8\u9500\u6bc1\u3002 \u4f46\u662f\uff0c\u5982\u679c\u7c7b\u6ca1\u6709\u88ab\u6b63\u786e\u5730\u9500\u6bc1\u7684\u8bdd\uff08\u4f8b\u5982\uff0c\u52a8\u6001\u5206\u914d\u4e86\u5185\u5b58\u4f46\u6ca1\u6709\u88ab\u91ca\u653e\uff09\uff0c\u4e8e\u662f std::unique_ptr \u6210\u5458\u4e5f\u4e0d\u4f1a\u88ab\u9500\u6bc1\uff0c\u6240\u4ee5\u5b83\u7ba1\u7406\u7684\u8d44\u6e90\u4e5f\u4e0d\u4f1a\u88ab\u91ca\u653e\u3002","title":"std::unique_ptr \u548c\u7c7b"},{"location":"M-6-std-unique-ptr/#stdunique_ptr_5","text":"std::unique_ptrs \u6709\u4e24\u79cd\u5178\u578b\u7684\u8bef\u7528\uff0c\u4f46\u662f\u5b83\u4eec\u5176\u5b9e\u5f88\u5bb9\u6613\u907f\u514d\u3002\u9996\u5148\uff0c\u4e0d\u8981\u8ba9\u591a\u4e2a\u7c7b\u7ba1\u7406\u540c\u4e00\u4e2a\u8d44\u6e90\uff0c\u4f8b\u5982\uff1a 1 2 3 Resource * res { new Resource () }; std :: unique_ptr < Resource > res1 { res }; std :: unique_ptr < Resource > res2 { res }; \u5c3d\u7ba1\u8bed\u6cd5\u4e0a\u5b8c\u5168\u6b63\u786e\uff0c\u4f46\u662f\u6700\u7ec8\u7684\u7ed3\u679c\u662f res1 \u548c res2 \u90fd\u4f1a\u53bb\u5220\u9664\u8d44\u6e90 Resource \uff0c\u8fdb\u800c\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u7b2c\u4e8c\uff0c\u4e0d\u8981\u624b\u52a8\u91ca\u653e\u5df2\u7ecf\u5728 std::unique_ptr \u7ba1\u7406\u4e0b\u7684\u8d44\u6e90\uff1a 1 2 3 Resource * res { new Resource () }; std :: unique_ptr < Resource > res1 { res }; delete res ; \u8fd9\u4e48\u505a\u7684\u8bdd\uff0c std::unique_ptr \u5c31\u4f1a\u518d\u6b21\u5220\u9664\u8fd9\u4e2a\u5df2\u7ecf\u88ab\u91ca\u653e\u7684\u8d44\u6e90\uff0c\u5bfc\u81f4 \u672a\u5b9a\u4e49\u884c\u4e3a \u3002 \u4f7f\u7528 std::make_unique() \u53ef\u4ee5\u907f\u514d\u4e0a\u8ff0\u4e24\u4e2a\u95ee\u9898\u3002","title":"std::unique_ptr \u7684\u8bef\u7528"},{"location":"M-7-std-shared-ptr/","tags":["shared_ptr","C++11","C++14","C++17","C++20"],"text":"Key Takeaway \u5982\u679c\u4f60\u9700\u8981\u4e3a\u76f8\u540c\u7684\u8d44\u6e90\u521b\u5efa\u989d\u5916\u7684 std::shared_ptr \uff0c\u8bf7\u4ece\u5df2\u6709\u7684 std::shared_ptr \u590d\u5236\u4e00\u4efd\u3002 std::unique_ptr \u88ab\u8bbe\u8ba1\u51fa\u6765\u72ec\u5360\u5b83\u6240\u7ba1\u7406\u7684\u8d44\u6e90\uff0c\u4e0e\u6b64\u4e0d\u540c\u7684\u662f std::shared_ptr \u5219\u662f\u4e3a\u4e86\u89e3\u51b3\u591a\u4e2a\u6307\u9488\u5171\u540c\u7ba1\u7406\u540c\u4e00\u4e2a\u8d44\u6e90\u7684\u60c5\u51b5\u3002 \u8fd9\u4e5f\u610f\u5473\u7740\uff0c\u591a\u4e2a std::shared_ptr \u6307\u5411\u540c\u4e00\u4e2a\u8d44\u6e90\u662f\u53ef\u4ee5\u7684\u3002 std::shared_ptr \u5185\u90e8\u4f1a\u81ea\u52a8\u8ffd\u8e2a\u5f53\u524d\u5171\u4eab\u8be5\u8d44\u6e90\u7684 std::shared_ptr \u7684\u4e2a\u6570\u3002\u53ea\u6709\u8fd8\u6709\u4e00\u4e2a std::shared_ptr \u8fd8\u6307\u5411\u8d44\u6e90\uff0c\u8be5\u8d44\u6e90\u5c31\u4e0d\u4f1a\u88ab\u91ca\u653e\uff0c\u5373\u4f7f\u4efb\u610f\u4e00\u4e2a std::shared_ptr \u88ab\u9500\u6bc1\u3002\u5f53\u6700\u540e\u4e00\u4e2a\u6307\u5411\u8be5\u8d44\u6e90\u7684 std::shared_ptr \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff08\u6216\u4e0d\u518d\u6307\u5411\u8be5\u8d44\u6e90\uff09\uff0c\u8d44\u6e90\u624d\u4f1a\u88ab\u91ca\u653e\u3002 \u548c std::unique_ptr \u4e00\u6837 std::shared_ptr \u4e5f\u88ab\u5b9a\u4e49\u5728 \u5934\u6587\u4ef6\u4e2d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include // for std::shared_ptr class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { // \u5206\u914d\u4e00\u4e2a Resource \u5bf9\u8c61\u5e76\u8ba9 std::shared_ptr \u62e5\u6709\u5b83 Resource * res { new Resource }; std :: shared_ptr < Resource > ptr1 { res }; { std :: shared_ptr < Resource > ptr2 { ptr1 }; // \u521b\u5efa\u53e6\u5916\u4e00\u4e2a std::shared_ptr \u4e5f\u6307\u5411\u8be5\u8d44\u6e90 std :: cout << \"Killing one shared pointer \\n \" ; } // ptr2 \u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u4ec0\u4e48\u90fd\u6ca1\u6709\u53d1\u751f std :: cout << \"Killing another shared pointer \\n \" ; return 0 ; } // ptr1 \u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u8d44\u6e90\u88ab\u91ca\u653e \u6253\u5370\uff1a 1 2 3 4 Resource acquired Killing one shared pointer Killing another shared pointer Resource destroyed \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u52a8\u6001 Resource \u5bf9\u8c61\uff0c\u7136\u540e\u5c06 std::shared_ptr \u7c7b\u578b\u7684 ptr1 \u6307\u5411\u5b83\uff0c\u5bf9\u8d44\u6e90\u8fdb\u884c\u7ba1\u7406\u3002\u5728\u5d4c\u5957\u7684\u8bed\u53e5\u5757\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u521b\u5efa\u4e86\u7b2c\u4e8c\u4e2a std::shared_ptr ( ptr2 )\u4e5f\u6307\u5411\u540c\u4e00\u4e2a Resource \u3002\u5f53 ptr2 \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c Resource \u5e76\u6ca1\u6709\u88ab\u91ca\u653e\uff0c\u56e0\u4e3a ptr1 \u4ecd\u7136\u6307\u5411\u8be5\u8d44\u6e90\u3002\u5f53 ptr1 \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c ptr1 \u4f1a\u610f\u8bc6\u5230\u6b64\u65f6\u6ca1\u6709 std::shared_ptr \u7ba1\u7406\u8be5\u8d44\u6e90\u4e86\uff0c\u6240\u4ee5\u5b83\u4f1a\u91ca\u653e Resource \u3002 \u6ce8\u610f\uff0c\u6211\u4eec\u662f\u4ece\u7b2c\u4e00\u4e2a\u6307\u9488\u521b\u5efa\u7684\u7b2c\u4e8c\u4e2a\u6307\u9488\uff0c\u8fd9\u4e00\u70b9\u975e\u5e38\u91cd\u8981\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include // for std::shared_ptr class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { Resource * res { new Resource }; std :: shared_ptr < Resource > ptr1 { res }; { std :: shared_ptr < Resource > ptr2 { res }; // \u76f4\u63a5\u901a\u8fc7res\u521b\u5efa ptr2 std :: cout << \"Killing one shared pointer \\n \" ; } // ptr2 \u79bb\u5f00\u4f5c\u7528\u57df Resource \u88ab\u9500\u6bc1 std :: cout << \"Killing another shared pointer \\n \" ; return 0 ; } // ptr1 \u79bb\u5f00\u4f5c\u7528\u57df\uff0cResource \u518d\u6b21\u88ab\u9500\u6bc1 \u7a0b\u5e8f\u8f93\u51fa\uff1a 1 2 3 4 5 Resource acquired Killing one shared pointer Resource destroyed Killing another shared pointer Resource destroyed \u7d27\u63a5\u7740\u5c31\u5d29\u6e83\u4e86\uff08\u81f3\u5c11\u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\u4f1a\u5d29\u6e83\uff09\u3002 \u548c\u4e0a\u4e00\u4e2a\u7a0b\u5e8f\u4e0d\u540c\u7684\u662f\uff0c\u8fd9\u91cc\u521b\u5efa\u7684\u4e24\u4e2a std::shared_ptr \u662f\u76f8\u4e92\u72ec\u7acb\u7684\u3002\u5176\u7ed3\u679c\u5c31\u662f\u5b83\u4eec\u5e76\u4e0d\u77e5\u9053\u5bf9\u65b9\u548c\u81ea\u5df1\u6307\u5411\u4e86\u540c\u4e00\u4e2a Resource \uff0c\u5b83\u4eec\u4e5f\u4e92\u76f8\u4e0d\u77e5\u9053\u5bf9\u65b9\u7684\u5b58\u5728\u3002\u5f53 ptr2 \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c\u5b83\u8ba4\u4e3a\u53ea\u6709\u81ea\u5df1\u5728\u7ba1\u7406\u8be5\u8d44\u6e90\uff0c\u6240\u4ee5\u5c31\u4f1a\u91ca\u653e\u5b83\u3002\u800c\u5f53 ptr1 \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c\u5b83\u4e5f\u8ba4\u4e3a\u53ea\u6709\u81ea\u5df1\u5728\u7ba1\u7406\u8be5\u8d44\u6e90\uff0c\u6240\u4ee5\u4f1a\u518d\u6b21\u91ca\u653e\u3002\u95ee\u9898\u5c31\u6b64\u53d1\u751f\u4e86\uff01 \u5e78\u597d\uff0c\u8fd9\u4e2a\u95ee\u9898\u5f88\u5bb9\u6613\u907f\u514d\uff1a\u5982\u679c\u4f60\u9700\u8981\u4e3a\u4e00\u4e2a\u8d44\u6e90\u518d\u521b\u5efa\u4e00\u4e2a std::shared_ptr \uff0c\u8bf7\u4ece\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684 std::shared_ptr \u514b\u9686\u3002 \u6700\u4f73\u5b9e\u8df5 =\u5982\u679c\u4f60\u9700\u8981\u4e3a\u76f8\u540c\u7684\u8d44\u6e90\u521b\u5efa\u989d\u5916\u7684 std::shared_ptr \uff0c\u8bf7\u4ece\u5df2\u6709\u7684 std::shared_ptr \u590d\u5236\u4e00\u4efd\u3002 std::make_shared \u7c7b\u4f3cC++14\u4e2d\u7684 std::make_unique() \u53ef\u4ee5\u7528\u6765\u521b\u5efa std::unique_ptr \uff0c std::make_shared() \u4e5f\u53ef\u4ee5\uff08\u5e94\u8be5\uff09\u88ab\u7528\u6765\u521b\u5efa std::shared_ptr \u3002 std::make_shared() \u53ef\u4ee5\u5728 C++11 \u4e2d\u4f7f\u7528\u3002 \u8fd9\u4e2a\u4f8b\u5b50\u4f7f\u7528 std::make_shared() \u5bf9\u539f\u6765\u7684\u7a0b\u5e8f\u8fdb\u884c\u4e86\u4fee\u6539\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include // for std::shared_ptr class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { // \u5206\u914d\u4e00\u4e2a Resource \u5bf9\u8c61\u5e76\u5c06\u5176\u4ea4\u7ed9 std::shared_ptr \u7ba1\u7406 auto ptr1 { std :: make_shared < Resource > () }; { auto ptr2 { ptr1 }; // \u590d\u5236 ptr1 \u521b\u5efa ptr2 std :: cout << \"Killing one shared pointer \\n \" ; } // ptr2 \u79bb\u5f00\u4f5c\u7528\u57df\u4f46\u662f\u4ec0\u4e48\u90fd\u6ca1\u6709\u53d1\u751f std :: cout << \"Killing another shared pointer \\n \" ; return 0 ; } // ptr1 \u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u8d44\u6e90\u88ab\u9500\u6bc1 \u4f7f\u7528 std::make_shared() \u7684\u7406\u7531\u548c\u4f7f\u7528 std::make_unique() \u7684\u7406\u7531\u7c7b\u4f3c\u2014\u2014 std::make_shared() \u66f4\u7b80\u6d01\u4e5f\u66f4\u5b89\u5168(\u4f7f\u7528\u8be5\u65b9\u6cd5\u7684\u60c5\u51b5\u4e0b\uff0c\u4e0d\u53ef\u80fd\u521b\u5efa\u4e24\u4e2a\u6307\u5411\u5934\u4e00\u4e2a\u8d44\u6e90\u7684 std::shared_ptr )\u3002 std::make_shared() \u6548\u7387\u4e5f\u66f4\u9ad8\uff0c\u5b83\u4f1a\u8ddf\u8e2a\u6307\u5411\u7279\u5b9a\u8d44\u6e90\u7684\u6307\u9488\u7684\u4e2a\u6570\u3002 std::shared_ptr \u5185\u5e55 \u548c std::unique_ptr \u5185\u90e8\u4f7f\u7528\u4e86\u4e00\u4e2a\u6307\u9488\u4e0d\u540c\uff0c std::shared_ptr \u5185\u90e8\u4f7f\u7528\u4e86\u4e24\u4e2a\u6307\u9488\u3002\u5176\u4e2d\u4e00\u4e2a\u6307\u9488\u6307\u5411\u88ab\u7ba1\u7406\u7684\u8d44\u6e90\u3002\u53e6\u5916\u4e00\u4e2a\u6307\u9488\u5219\u6307\u5411\u4e00\u4e2a\u201c\u63a7\u5236\u5757\u201d\uff0c\u63a7\u5236\u5757\u662f\u4e00\u4e2a\u52a8\u6001\u5206\u914d\u7684\u5bf9\u8c61\uff0c\u7528\u4e8e\u8ffd\u8e2a\u4e00\u7cfb\u5217\u7684\u4fe1\u606f\uff0c\u5176\u4e2d\u5c31\u5305\u62ec\u6709\u591a\u5c11 std::shared_ptr \u6307\u5411\u4e86\u8be5\u5bf9\u8c61 \u3002\u5f53 std::shared_ptr \u901a\u8fc7 std::shared_ptr \u6784\u9020\u51fd\u6570\u521b\u5efa\u65f6\uff0c\u88ab\u7ba1\u7406\u5bf9\u8c61\u7684\u5185\u5b58\uff08\u901a\u5e38\u662f\u4f20\u5165\u7684\uff09\u548c\u63a7\u5236\u5757\uff08\u901a\u5e38\u7531\u6784\u9020\u51fd\u6570\u521b\u5efa\uff09\u7684\u5185\u5b58\u4f1a\u88ab\u5206\u522b\u5206\u914d\u3002 \u4f46\u662f\uff0c\u5f53\u4f7f\u7528 std::make_shared() \u521b\u5efa\u65f6\uff0c\u4e24\u4e2a\u5185\u5b58\u7684\u5206\u914d\u53ef\u4ee5\u88ab\u4f18\u5316\u4e3a\u4e00\u6b21\u5185\u5b58\u5206\u914d\uff0c\u4ece\u800c\u63d0\u5347\u6027\u80fd\u3002 \u8fd9\u4e5f\u89e3\u91ca\u4e86\u4e3a\u4ec0\u4e48\u5206\u914d\u521b\u5efa\u4e24\u4e2a\u6307\u5411\u76f8\u540c\u8d44\u6e90\u7684 std::shared_ptr \u65f6\u4f1a\u6709\u95ee\u9898\u3002\u6bcf\u4e2a std::shared_ptr \u90fd\u4f1a\u6709\u4e00\u4e2a\u6307\u5411\u8be5\u8d44\u6e90\u7684\u6307\u9488\uff0c\u4f46\u540c\u65f6\uff0c\u6bcf\u4e2a std::shared_ptr \u4e5f\u90fd\u4f1a\u72ec\u7acb\u5206\u914d\u81ea\u5df1\u7684\u63a7\u5236\u5757\uff0c\u56e0\u6b64\u63a7\u5236\u5757\u4e2d\u90fd\u53ea\u4f1a\u663e\u793a\u6709\u4e00\u4e2a\u6307\u9488\u6307\u5411\u88ab\u7ba1\u7406\u7684\u8d44\u6e90\u3002\u5f53 std::shared_ptr \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c\u5b83\u5c31\u4f1a\u91ca\u653e\u8d44\u6e90\uff0c\u800c\u4e0d\u4f1a\u610f\u8bc6\u5230\u8fd8\u6709\u5176\u4ed6\u7684 std::shared_ptr \u4e5f\u5728\u7ba1\u7406\u7740\u8be5\u8d44\u6e90\u3002 \u4f46\u662f\uff0c\u5f53 std::shared_ptr \u901a\u8fc7 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u88ab\u521b\u5efa\u65f6\uff0c\u63a7\u5236\u5757\u4f1a\u88ab\u6b63\u786e\u5730\u66f4\u65b0\uff0c\u4f7f\u5176\u80fd\u591f\u663e\u793a\u53c8\u6709\u53e6\u5916\u4e00\u4e2a std::shared_ptr \u5f00\u59cb\u7ba1\u7406\u8fd9\u4e2a\u8d44\u6e90\u4e86\u3002 std::shared_ptr \u53ef\u4ee5\u901a\u8fc7 std::unique_ptr \u521b\u5efa std::unique_ptr \u53ef\u4ee5\u901a\u8fc7\u7279\u6b8a\u7684\u6784\u9020\u51fd\u6570\uff08\u63a5\u53d7\u4e00\u4e2a std::unique_ptr \u53f3\u503c\uff09\u88ab\u8f6c\u6362\u4e3a std::shared_ptr \u3002 std::unique_ptr \u7684\u5185\u5bb9\u4f1a\u88ab\u79fb\u52a8\u5230 std::shared_ptr \u3002 \u4f46\u662f\uff0c std::shared_ptr \u5e76\u4e0d\u80fd\u88ab\u5b89\u5168\u5730\u8f6c\u6362\u4e3a std::unique_ptr \u3002\u8fd9\u610f\u5473\u7740\u5f53\u4f60\u521b\u5efa\u7684\u51fd\u6570\u9700\u8981\u8fd4\u56de\u4e00\u4e2a\u667a\u80fd\u6307\u9488\u65f6\uff0c\u6240\u4ee5\u4f60\u6700\u597d\u8fd4\u56de std::unique_ptr \u5e76\u5c06\u5176\u8d4b\u503c\u7ed9 std::shared_ptr \u3002 std::shared_ptr \u7684\u9677\u9631 std::shared_ptr \u4e5f\u5b58\u5728 std::unique_ptr \u6240\u5177\u6709\u7684\u4e00\u4e9b\u95ee\u9898\u2014\u2014\u5982\u679c std::shared_ptr \u6ca1\u6709\u88ab\u6b63\u5e38\u5730\u9500\u6bc1\uff08\u53ef\u80fd\u662f\u56e0\u4e3a\u52a8\u6001\u5185\u5b58\u6ca1\u6709\u88ab\u91ca\u653e\u6216\u90e8\u5206\u5bf9\u8c61\u6ca1\u6709\u88ab\u91ca\u653e\uff09\u5219\u5176\u7ba1\u7406\u7684\u8d44\u6e90\u4e5f\u4e0d\u4f1a\u88ab\u91ca\u653e\u3002\u4f7f\u7528 std::unique_ptr \u65f6\uff0c\u4f60\u53ea\u9700\u8981\u5173\u5fc3\u4e00\u4e2a\u667a\u80fd\u6307\u9488\u662f\u5426\u88ab\u6b63\u5e38\u7684\u9500\u6bc1\u3002\u4f46\u662f\u5728\u4f7f\u7528 std::shared_ptr \u65f6\uff0c\u4f60\u9700\u8981\u5173\u6ce8\u7ba1\u7406\u8be5\u5bf9\u8c61\u7684\u6240\u6709\u6307\u9488\u3002\u5982\u679c\u5b83\u4eec\u4e2d\u4efb\u4f55\u4e00\u4e2a\u6ca1\u6709\u88ab\u9500\u6bc1\uff0c\u5219\u8d44\u6e90\u4e5f\u4e0d\u4f1a\u88ab\u6b63\u5e38\u9500\u6bc1\u3002 std::shared_ptr \u548c\u6570\u7ec4 \u5728 C++17 \u53ca\u4e4b\u524d\u7684\u7248\u672c\u4e2d\uff0c std::shared_ptr \u5e76\u4e0d\u652f\u6301\u7ba1\u7406\u6570\u7ec4\uff0c\u800c\u4e14\u4e5f\u4e0d\u5e94\u8be5\u88ab\u7528\u6765\u7ba1\u7406C\u8bed\u8a00\u98ce\u683c\u7684\u6570\u7ec4\u3002\u5728 C++20 \u4e2d std::shared_ptr \u5df2\u7ecf\u53ef\u4ee5\u652f\u6301\u6570\u7ec4\u4e86\u3002 \u5c0f\u7ed3 \u8bbe\u8ba1 std::shared_ptr \u7684\u521d\u8877\uff0c\u662f\u4e3a\u4e86\u652f\u6301\u591a\u4e2a\u6307\u9488\u80fd\u591f\u5171\u540c\u7ba1\u7406\u4e00\u4e2a\u8d44\u6e90\u3002\u5f53\u6700\u540e\u4e00\u4e2a std::shared_ptr \u88ab\u9500\u6bc1\u65f6\uff0c\u88ab\u7ba1\u7406\u7684\u8d44\u6e90\u4e5f\u5c31\u4f1a\u88ab\u9500\u6bc1\u4e86\u3002","title":"M.7 \u2014 std::shared_ptr"},{"location":"M-7-std-shared-ptr/#stdmake_shared","text":"\u7c7b\u4f3cC++14\u4e2d\u7684 std::make_unique() \u53ef\u4ee5\u7528\u6765\u521b\u5efa std::unique_ptr \uff0c std::make_shared() \u4e5f\u53ef\u4ee5\uff08\u5e94\u8be5\uff09\u88ab\u7528\u6765\u521b\u5efa std::shared_ptr \u3002 std::make_shared() \u53ef\u4ee5\u5728 C++11 \u4e2d\u4f7f\u7528\u3002 \u8fd9\u4e2a\u4f8b\u5b50\u4f7f\u7528 std::make_shared() \u5bf9\u539f\u6765\u7684\u7a0b\u5e8f\u8fdb\u884c\u4e86\u4fee\u6539\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include // for std::shared_ptr class Resource { public : Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { // \u5206\u914d\u4e00\u4e2a Resource \u5bf9\u8c61\u5e76\u5c06\u5176\u4ea4\u7ed9 std::shared_ptr \u7ba1\u7406 auto ptr1 { std :: make_shared < Resource > () }; { auto ptr2 { ptr1 }; // \u590d\u5236 ptr1 \u521b\u5efa ptr2 std :: cout << \"Killing one shared pointer \\n \" ; } // ptr2 \u79bb\u5f00\u4f5c\u7528\u57df\u4f46\u662f\u4ec0\u4e48\u90fd\u6ca1\u6709\u53d1\u751f std :: cout << \"Killing another shared pointer \\n \" ; return 0 ; } // ptr1 \u79bb\u5f00\u4f5c\u7528\u57df\uff0c\u8d44\u6e90\u88ab\u9500\u6bc1 \u4f7f\u7528 std::make_shared() \u7684\u7406\u7531\u548c\u4f7f\u7528 std::make_unique() \u7684\u7406\u7531\u7c7b\u4f3c\u2014\u2014 std::make_shared() \u66f4\u7b80\u6d01\u4e5f\u66f4\u5b89\u5168(\u4f7f\u7528\u8be5\u65b9\u6cd5\u7684\u60c5\u51b5\u4e0b\uff0c\u4e0d\u53ef\u80fd\u521b\u5efa\u4e24\u4e2a\u6307\u5411\u5934\u4e00\u4e2a\u8d44\u6e90\u7684 std::shared_ptr )\u3002 std::make_shared() \u6548\u7387\u4e5f\u66f4\u9ad8\uff0c\u5b83\u4f1a\u8ddf\u8e2a\u6307\u5411\u7279\u5b9a\u8d44\u6e90\u7684\u6307\u9488\u7684\u4e2a\u6570\u3002","title":"std::make_shared"},{"location":"M-7-std-shared-ptr/#stdshared_ptr","text":"\u548c std::unique_ptr \u5185\u90e8\u4f7f\u7528\u4e86\u4e00\u4e2a\u6307\u9488\u4e0d\u540c\uff0c std::shared_ptr \u5185\u90e8\u4f7f\u7528\u4e86\u4e24\u4e2a\u6307\u9488\u3002\u5176\u4e2d\u4e00\u4e2a\u6307\u9488\u6307\u5411\u88ab\u7ba1\u7406\u7684\u8d44\u6e90\u3002\u53e6\u5916\u4e00\u4e2a\u6307\u9488\u5219\u6307\u5411\u4e00\u4e2a\u201c\u63a7\u5236\u5757\u201d\uff0c\u63a7\u5236\u5757\u662f\u4e00\u4e2a\u52a8\u6001\u5206\u914d\u7684\u5bf9\u8c61\uff0c\u7528\u4e8e\u8ffd\u8e2a\u4e00\u7cfb\u5217\u7684\u4fe1\u606f\uff0c\u5176\u4e2d\u5c31\u5305\u62ec\u6709\u591a\u5c11 std::shared_ptr \u6307\u5411\u4e86\u8be5\u5bf9\u8c61 \u3002\u5f53 std::shared_ptr \u901a\u8fc7 std::shared_ptr \u6784\u9020\u51fd\u6570\u521b\u5efa\u65f6\uff0c\u88ab\u7ba1\u7406\u5bf9\u8c61\u7684\u5185\u5b58\uff08\u901a\u5e38\u662f\u4f20\u5165\u7684\uff09\u548c\u63a7\u5236\u5757\uff08\u901a\u5e38\u7531\u6784\u9020\u51fd\u6570\u521b\u5efa\uff09\u7684\u5185\u5b58\u4f1a\u88ab\u5206\u522b\u5206\u914d\u3002 \u4f46\u662f\uff0c\u5f53\u4f7f\u7528 std::make_shared() \u521b\u5efa\u65f6\uff0c\u4e24\u4e2a\u5185\u5b58\u7684\u5206\u914d\u53ef\u4ee5\u88ab\u4f18\u5316\u4e3a\u4e00\u6b21\u5185\u5b58\u5206\u914d\uff0c\u4ece\u800c\u63d0\u5347\u6027\u80fd\u3002 \u8fd9\u4e5f\u89e3\u91ca\u4e86\u4e3a\u4ec0\u4e48\u5206\u914d\u521b\u5efa\u4e24\u4e2a\u6307\u5411\u76f8\u540c\u8d44\u6e90\u7684 std::shared_ptr \u65f6\u4f1a\u6709\u95ee\u9898\u3002\u6bcf\u4e2a std::shared_ptr \u90fd\u4f1a\u6709\u4e00\u4e2a\u6307\u5411\u8be5\u8d44\u6e90\u7684\u6307\u9488\uff0c\u4f46\u540c\u65f6\uff0c\u6bcf\u4e2a std::shared_ptr \u4e5f\u90fd\u4f1a\u72ec\u7acb\u5206\u914d\u81ea\u5df1\u7684\u63a7\u5236\u5757\uff0c\u56e0\u6b64\u63a7\u5236\u5757\u4e2d\u90fd\u53ea\u4f1a\u663e\u793a\u6709\u4e00\u4e2a\u6307\u9488\u6307\u5411\u88ab\u7ba1\u7406\u7684\u8d44\u6e90\u3002\u5f53 std::shared_ptr \u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c\u5b83\u5c31\u4f1a\u91ca\u653e\u8d44\u6e90\uff0c\u800c\u4e0d\u4f1a\u610f\u8bc6\u5230\u8fd8\u6709\u5176\u4ed6\u7684 std::shared_ptr \u4e5f\u5728\u7ba1\u7406\u7740\u8be5\u8d44\u6e90\u3002 \u4f46\u662f\uff0c\u5f53 std::shared_ptr \u901a\u8fc7 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u88ab\u521b\u5efa\u65f6\uff0c\u63a7\u5236\u5757\u4f1a\u88ab\u6b63\u786e\u5730\u66f4\u65b0\uff0c\u4f7f\u5176\u80fd\u591f\u663e\u793a\u53c8\u6709\u53e6\u5916\u4e00\u4e2a std::shared_ptr \u5f00\u59cb\u7ba1\u7406\u8fd9\u4e2a\u8d44\u6e90\u4e86\u3002","title":"std::shared_ptr \u5185\u5e55"},{"location":"M-7-std-shared-ptr/#stdshared_ptr-stdunique_ptr","text":"std::unique_ptr \u53ef\u4ee5\u901a\u8fc7\u7279\u6b8a\u7684\u6784\u9020\u51fd\u6570\uff08\u63a5\u53d7\u4e00\u4e2a std::unique_ptr \u53f3\u503c\uff09\u88ab\u8f6c\u6362\u4e3a std::shared_ptr \u3002 std::unique_ptr \u7684\u5185\u5bb9\u4f1a\u88ab\u79fb\u52a8\u5230 std::shared_ptr \u3002 \u4f46\u662f\uff0c std::shared_ptr \u5e76\u4e0d\u80fd\u88ab\u5b89\u5168\u5730\u8f6c\u6362\u4e3a std::unique_ptr \u3002\u8fd9\u610f\u5473\u7740\u5f53\u4f60\u521b\u5efa\u7684\u51fd\u6570\u9700\u8981\u8fd4\u56de\u4e00\u4e2a\u667a\u80fd\u6307\u9488\u65f6\uff0c\u6240\u4ee5\u4f60\u6700\u597d\u8fd4\u56de std::unique_ptr \u5e76\u5c06\u5176\u8d4b\u503c\u7ed9 std::shared_ptr \u3002","title":"std::shared_ptr \u53ef\u4ee5\u901a\u8fc7 std::unique_ptr \u521b\u5efa"},{"location":"M-7-std-shared-ptr/#stdshared_ptr_1","text":"std::shared_ptr \u4e5f\u5b58\u5728 std::unique_ptr \u6240\u5177\u6709\u7684\u4e00\u4e9b\u95ee\u9898\u2014\u2014\u5982\u679c std::shared_ptr \u6ca1\u6709\u88ab\u6b63\u5e38\u5730\u9500\u6bc1\uff08\u53ef\u80fd\u662f\u56e0\u4e3a\u52a8\u6001\u5185\u5b58\u6ca1\u6709\u88ab\u91ca\u653e\u6216\u90e8\u5206\u5bf9\u8c61\u6ca1\u6709\u88ab\u91ca\u653e\uff09\u5219\u5176\u7ba1\u7406\u7684\u8d44\u6e90\u4e5f\u4e0d\u4f1a\u88ab\u91ca\u653e\u3002\u4f7f\u7528 std::unique_ptr \u65f6\uff0c\u4f60\u53ea\u9700\u8981\u5173\u5fc3\u4e00\u4e2a\u667a\u80fd\u6307\u9488\u662f\u5426\u88ab\u6b63\u5e38\u7684\u9500\u6bc1\u3002\u4f46\u662f\u5728\u4f7f\u7528 std::shared_ptr \u65f6\uff0c\u4f60\u9700\u8981\u5173\u6ce8\u7ba1\u7406\u8be5\u5bf9\u8c61\u7684\u6240\u6709\u6307\u9488\u3002\u5982\u679c\u5b83\u4eec\u4e2d\u4efb\u4f55\u4e00\u4e2a\u6ca1\u6709\u88ab\u9500\u6bc1\uff0c\u5219\u8d44\u6e90\u4e5f\u4e0d\u4f1a\u88ab\u6b63\u5e38\u9500\u6bc1\u3002","title":"std::shared_ptr \u7684\u9677\u9631"},{"location":"M-7-std-shared-ptr/#stdshared_ptr_2","text":"\u5728 C++17 \u53ca\u4e4b\u524d\u7684\u7248\u672c\u4e2d\uff0c std::shared_ptr \u5e76\u4e0d\u652f\u6301\u7ba1\u7406\u6570\u7ec4\uff0c\u800c\u4e14\u4e5f\u4e0d\u5e94\u8be5\u88ab\u7528\u6765\u7ba1\u7406C\u8bed\u8a00\u98ce\u683c\u7684\u6570\u7ec4\u3002\u5728 C++20 \u4e2d std::shared_ptr \u5df2\u7ecf\u53ef\u4ee5\u652f\u6301\u6570\u7ec4\u4e86\u3002","title":"std::shared_ptr \u548c\u6570\u7ec4"},{"location":"M-7-std-shared-ptr/#_1","text":"\u8bbe\u8ba1 std::shared_ptr \u7684\u521d\u8877\uff0c\u662f\u4e3a\u4e86\u652f\u6301\u591a\u4e2a\u6307\u9488\u80fd\u591f\u5171\u540c\u7ba1\u7406\u4e00\u4e2a\u8d44\u6e90\u3002\u5f53\u6700\u540e\u4e00\u4e2a std::shared_ptr \u88ab\u9500\u6bc1\u65f6\uff0c\u88ab\u7ba1\u7406\u7684\u8d44\u6e90\u4e5f\u5c31\u4f1a\u88ab\u9500\u6bc1\u4e86\u3002","title":"\u5c0f\u7ed3"},{"location":"M-8-circular-dependency-issues-with-std-shared-ptr-and-std-weak-ptr/","tags":["shared_ptr","circular dependency","weak_ptr"],"text":"Key Takeaway In the previous lesson, we saw how std::shared_ptr allowed us to have multiple smart pointers co-owning the same resource. However, in certain cases, this can become problematic. Consider the following case, where the shared pointers in two separate objects each point at the other object: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include #include // for std::shared_ptr #include class Person { std :: string m_name ; std :: shared_ptr < Person > m_partner ; // initially created empty public : Person ( const std :: string & name ) : m_name ( name ) { std :: cout << m_name << \" created \\n \" ; } ~ Person () { std :: cout << m_name << \" destroyed \\n \" ; } friend bool partnerUp ( std :: shared_ptr < Person > & p1 , std :: shared_ptr < Person > & p2 ) { if ( ! p1 || ! p2 ) return false ; p1 -> m_partner = p2 ; p2 -> m_partner = p1 ; std :: cout << p1 -> m_name << \" is now partnered with \" << p2 -> m_name << '\\n' ; return true ; } }; int main () { auto lucy { std :: make_shared < Person > ( \"Lucy\" ) }; // create a Person named \"Lucy\" auto ricky { std :: make_shared < Person > ( \"Ricky\" ) }; // create a Person named \"Ricky\" partnerUp ( lucy , ricky ); // Make \"Lucy\" point to \"Ricky\" and vice-versa return 0 ; } COPY In the above example, we dynamically allocate two Persons, \u201cLucy\u201d and \u201cRicky\u201d using make_shared() (to ensure lucy and ricky are destroyed at the end of main()). Then we partner them up. This sets the std::shared_ptr inside \u201cLucy\u201d to point at \u201cRicky\u201d, and the std::shared_ptr inside \u201cRicky\u201d to point at \u201cLucy\u201d. Shared pointers are meant to be shared, so it\u2019s fine that both the lucy shared pointer and Rick\u2019s m_partner shared pointer both point at \u201cLucy\u201d (and vice-versa). However, this program doesn\u2019t execute as expected: 1 2 3 Lucy created Ricky created Lucy is now partnered with Ricky And that\u2019s it. No deallocations took place. Uh oh. What happened? After partnerUp() is called, there are two shared pointers pointing to \u201cRicky\u201d (ricky, and Lucy\u2019s m_partner) and two shared pointers pointing to \u201cLucy\u201d (lucy, and Ricky\u2019s m_partner). At the end of main(), the ricky shared pointer goes out of scope first. When that happens, ricky checks if there are any other shared pointers that co-own the Person \u201cRicky\u201d. There are (Lucy\u2019s m_partner). Because of this, it doesn\u2019t deallocate \u201cRicky\u201d (if it did, then Lucy\u2019s m_partner would end up as a dangling pointer). At this point, we now have one shared pointer to \u201cRicky\u201d (Lucy\u2019s m_partner) and two shared pointers to \u201cLucy\u201d (lucy, and Ricky\u2019s m_partner). Next the lucy shared pointer goes out of scope, and the same thing happens. The shared pointer lucy checks if there are any other shared pointers co-owning the Person \u201cLucy\u201d. There are (Ricky\u2019s m_partner), so \u201cLucy\u201d isn\u2019t deallocated. At this point, there is one shared pointer to \u201cLucy\u201d (Ricky\u2019s m_partner) and one shared pointer to \u201cRicky\u201d (Lucy\u2019s m_partner). Then the program ends -- and neither Person \u201cLucy\u201d or \u201cRicky\u201d have been deallocated! Essentially, \u201cLucy\u201d ends up keeping \u201cRicky\u201d from being destroyed, and \u201cRicky\u201d ends up keeping \u201cLucy\u201d from being destroyed. It turns out that this can happen any time shared pointers form a circular reference. \u5faa\u73af\u5f15\u7528 A Circular reference (also called a cyclical reference or a cycle ) is a series of references where each object references the next, and the last object references back to the first, causing a referential loop. The references do not need to be actual C++ references -- they can be pointers, unique IDs, or any other means of identifying specific objects. In the context of shared pointers, the references will be pointers. This is exactly what we see in the case above: \u201cLucy\u201d points at \u201cRicky\u201d, and \u201cRicky\u201d points at \u201cLucy\u201d. With three pointers, you\u2019d get the same thing when A points at B, B points at C, and C points at A. The practical effect of having shared pointers form a cycle is that each object ends up keeping the next object alive -- with the last object keeping the first object alive. Thus, no objects in the series can be deallocated because they all think some other object still needs it! A reductive case It turns out, this cyclical reference issue can even happen with a single std::shared_ptr -- a std::shared_ptr referencing the object that contains it is still a cycle (just a reductive one). Although it\u2019s fairly unlikely that this would ever happen in practice, we\u2019ll show you for additional comprehension: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include // for std::shared_ptr class Resource { public : std :: shared_ptr < Resource > m_ptr {}; // initially created empty Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { auto ptr1 { std :: make_shared < Resource > () }; ptr1 -> m_ptr = ptr1 ; // m_ptr is now sharing the Resource that contains it return 0 ; } COPY In the above example, when ptr1 goes out of scope, the Resource is not deallocated because the Resource\u2019s m_ptr is sharing the Resource. At that point, the only way for the Resource to be released would be to set m_ptr to something else (so nothing is sharing the Resource any longer). But we can\u2019t access m_ptr because ptr1 is out of scope, so we no longer have a way to do this. The Resource has become a memory leak. Thus, the program prints: Resource acquired and that\u2019s it. std::weak_ptr \u5230\u5e95\u662f\u4ec0\u4e48\uff1f std::weak_ptr was designed to solve the \u201ccyclical ownership\u201d problem described above. A std::weak_ptr is an observer -- it can observe and access the same object as a std::shared_ptr (or other std::weak_ptrs) but it is not considered an owner. Remember, when a std::shared pointer goes out of scope, it only considers whether other std::shared_ptr are co-owning the object. std::weak_ptr does not count! Let\u2019s solve our Person-al issue using a std::weak_ptr: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include #include // for std::shared_ptr and std::weak_ptr #include class Person { std :: string m_name ; std :: weak_ptr < Person > m_partner ; // note: This is now a std::weak_ptr public : Person ( const std :: string & name ) : m_name ( name ) { std :: cout << m_name << \" created \\n \" ; } ~ Person () { std :: cout << m_name << \" destroyed \\n \" ; } friend bool partnerUp ( std :: shared_ptr < Person > & p1 , std :: shared_ptr < Person > & p2 ) { if ( ! p1 || ! p2 ) return false ; p1 -> m_partner = p2 ; p2 -> m_partner = p1 ; std :: cout << p1 -> m_name << \" is now partnered with \" << p2 -> m_name << '\\n' ; return true ; } }; int main () { auto lucy { std :: make_shared < Person > ( \"Lucy\" ) }; auto ricky { std :: make_shared < Person > ( \"Ricky\" ) }; partnerUp ( lucy , ricky ); return 0 ; } COPY This code behaves properly: 1 2 3 4 5 Lucy created Ricky created Lucy is now partnered with Ricky Ricky destroyed Lucy destroyed Functionally, it works almost identically to the problematic example. However, now when ricky goes out of scope, it sees that there are no other std::shared_ptr pointing at \u201cRicky\u201d (the std::weak_ptr from \u201cLucy\u201d doesn\u2019t count). Therefore, it will deallocate \u201cRicky\u201d. The same occurs for lucy. \u4f7f\u7528 std::weak_ptr The downside of std::weak_ptr is that std::weak_ptr are not directly usable (they have no operator->). To use a std::weak_ptr, you must first convert it into a std::shared_ptr. Then you can use the std::shared_ptr. To convert a std::weak_ptr into a std::shared_ptr, you can use the lock() member function. Here\u2019s the above example, updated to show this off: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include #include // for std::shared_ptr and std::weak_ptr #include class Person { std :: string m_name ; std :: weak_ptr < Person > m_partner ; // note: This is now a std::weak_ptr public : Person ( const std :: string & name ) : m_name ( name ) { std :: cout << m_name << \" created \\n \" ; } ~ Person () { std :: cout << m_name << \" destroyed \\n \" ; } friend bool partnerUp ( std :: shared_ptr < Person > & p1 , std :: shared_ptr < Person > & p2 ) { if ( ! p1 || ! p2 ) return false ; p1 -> m_partner = p2 ; p2 -> m_partner = p1 ; std :: cout << p1 -> m_name << \" is now partnered with \" << p2 -> m_name << \" \\'\\n '; return true ; } const std :: shared_ptr < Person > getPartner () const { return m_partner . lock (); } // use lock() to convert weak_ptr to shared_ptr const std :: string & getName () const { return m_name ; } }; int main () { auto lucy { std :: make_shared < Person > ( \"Lucy\" ) }; auto ricky { std :: make_shared < Person > ( \"Ricky\" ) }; partnerUp ( lucy , ricky ); auto partner = ricky -> getPartner (); // get shared_ptr to Ricky's partner std :: cout << ricky -> getName () << \"'s partner is: \" << partner -> getName () << '\\n' ; return 0 ; } COPY This prints: 1 2 3 4 5 6 Lucy created Ricky created Lucy is now partnered with Ricky Ricky's partner is: Lucy Ricky destroyed Lucy destroyed We don\u2019t have to worry about circular dependencies with std::shared_ptr variable \u201cpartner\u201d since it\u2019s just a local variable inside the function. It will eventually go out of scope at the end of the function and the reference count will be decremented by 1. Dangling pointers with std::weak_ptr Because std::weak_ptr won\u2019t keep an owned resource alive, it\u2019s possible for a std::weak_ptr to be left pointing to a resource that has been deallocated by a std::shared_ptr. Such a std::weak_ptr is dangling, and using it will cause undefined behavior. Here\u2019s a simple example showing how this can happen: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 // h/t to reader Waldo for this example #include #include class Resource { public : Resource () { std :: cerr << \"Resource acquired \\n \" ; } ~ Resource () { std :: cerr << \"Resource destroyed \\n \" ; } }; auto getWeakPtr () { auto ptr { std :: make_shared < Resource > () }; // Resource acquired return std :: weak_ptr { ptr }; } // ptr goes out of scope, Resource destroyed int main () { std :: cerr << \"Getting weak_ptr... \\n \" ; auto ptr { getWeakPtr () }; // dangling std :: cerr << \"Done. \\n \" ; } COPY In the above example, inside getWeakPtr() we use std::make_shared() to create a std::shared_ptr variable named ptr that owns a Resource object. The function returns a std::weak_ptr back to the caller, which does not increment the reference count. Then because ptr is a local variable, it goes out of scope at the end of the function, which decrements the reference count to 0 and deallocates the Resource object. The returned std::weak_ptr is left dangling, pointing to a Resource that was deallocated. \u5c0f\u7ed3 std::shared_ptr can be used when you need multiple smart pointers that can co-own a resource. The resource will be deallocated when the last std::shared_ptr goes out of scope. std::weak_ptr can be used when you want a smart pointer that can see and use a shared resource, but does not participate in the ownership of that resource.","title":"M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898"},{"location":"M-8-circular-dependency-issues-with-std-shared-ptr-and-std-weak-ptr/#_1","text":"A Circular reference (also called a cyclical reference or a cycle ) is a series of references where each object references the next, and the last object references back to the first, causing a referential loop. The references do not need to be actual C++ references -- they can be pointers, unique IDs, or any other means of identifying specific objects. In the context of shared pointers, the references will be pointers. This is exactly what we see in the case above: \u201cLucy\u201d points at \u201cRicky\u201d, and \u201cRicky\u201d points at \u201cLucy\u201d. With three pointers, you\u2019d get the same thing when A points at B, B points at C, and C points at A. The practical effect of having shared pointers form a cycle is that each object ends up keeping the next object alive -- with the last object keeping the first object alive. Thus, no objects in the series can be deallocated because they all think some other object still needs it!","title":"\u5faa\u73af\u5f15\u7528"},{"location":"M-8-circular-dependency-issues-with-std-shared-ptr-and-std-weak-ptr/#a-reductive-case","text":"It turns out, this cyclical reference issue can even happen with a single std::shared_ptr -- a std::shared_ptr referencing the object that contains it is still a cycle (just a reductive one). Although it\u2019s fairly unlikely that this would ever happen in practice, we\u2019ll show you for additional comprehension: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include // for std::shared_ptr class Resource { public : std :: shared_ptr < Resource > m_ptr {}; // initially created empty Resource () { std :: cout << \"Resource acquired \\n \" ; } ~ Resource () { std :: cout << \"Resource destroyed \\n \" ; } }; int main () { auto ptr1 { std :: make_shared < Resource > () }; ptr1 -> m_ptr = ptr1 ; // m_ptr is now sharing the Resource that contains it return 0 ; } COPY In the above example, when ptr1 goes out of scope, the Resource is not deallocated because the Resource\u2019s m_ptr is sharing the Resource. At that point, the only way for the Resource to be released would be to set m_ptr to something else (so nothing is sharing the Resource any longer). But we can\u2019t access m_ptr because ptr1 is out of scope, so we no longer have a way to do this. The Resource has become a memory leak. Thus, the program prints: Resource acquired and that\u2019s it.","title":"A reductive case"},{"location":"M-8-circular-dependency-issues-with-std-shared-ptr-and-std-weak-ptr/#stdweak_ptr","text":"std::weak_ptr was designed to solve the \u201ccyclical ownership\u201d problem described above. A std::weak_ptr is an observer -- it can observe and access the same object as a std::shared_ptr (or other std::weak_ptrs) but it is not considered an owner. Remember, when a std::shared pointer goes out of scope, it only considers whether other std::shared_ptr are co-owning the object. std::weak_ptr does not count! Let\u2019s solve our Person-al issue using a std::weak_ptr: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include #include // for std::shared_ptr and std::weak_ptr #include class Person { std :: string m_name ; std :: weak_ptr < Person > m_partner ; // note: This is now a std::weak_ptr public : Person ( const std :: string & name ) : m_name ( name ) { std :: cout << m_name << \" created \\n \" ; } ~ Person () { std :: cout << m_name << \" destroyed \\n \" ; } friend bool partnerUp ( std :: shared_ptr < Person > & p1 , std :: shared_ptr < Person > & p2 ) { if ( ! p1 || ! p2 ) return false ; p1 -> m_partner = p2 ; p2 -> m_partner = p1 ; std :: cout << p1 -> m_name << \" is now partnered with \" << p2 -> m_name << '\\n' ; return true ; } }; int main () { auto lucy { std :: make_shared < Person > ( \"Lucy\" ) }; auto ricky { std :: make_shared < Person > ( \"Ricky\" ) }; partnerUp ( lucy , ricky ); return 0 ; } COPY This code behaves properly: 1 2 3 4 5 Lucy created Ricky created Lucy is now partnered with Ricky Ricky destroyed Lucy destroyed Functionally, it works almost identically to the problematic example. However, now when ricky goes out of scope, it sees that there are no other std::shared_ptr pointing at \u201cRicky\u201d (the std::weak_ptr from \u201cLucy\u201d doesn\u2019t count). Therefore, it will deallocate \u201cRicky\u201d. The same occurs for lucy.","title":"std::weak_ptr \u5230\u5e95\u662f\u4ec0\u4e48\uff1f"},{"location":"M-8-circular-dependency-issues-with-std-shared-ptr-and-std-weak-ptr/#stdweak_ptr_1","text":"The downside of std::weak_ptr is that std::weak_ptr are not directly usable (they have no operator->). To use a std::weak_ptr, you must first convert it into a std::shared_ptr. Then you can use the std::shared_ptr. To convert a std::weak_ptr into a std::shared_ptr, you can use the lock() member function. Here\u2019s the above example, updated to show this off: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include #include // for std::shared_ptr and std::weak_ptr #include class Person { std :: string m_name ; std :: weak_ptr < Person > m_partner ; // note: This is now a std::weak_ptr public : Person ( const std :: string & name ) : m_name ( name ) { std :: cout << m_name << \" created \\n \" ; } ~ Person () { std :: cout << m_name << \" destroyed \\n \" ; } friend bool partnerUp ( std :: shared_ptr < Person > & p1 , std :: shared_ptr < Person > & p2 ) { if ( ! p1 || ! p2 ) return false ; p1 -> m_partner = p2 ; p2 -> m_partner = p1 ; std :: cout << p1 -> m_name << \" is now partnered with \" << p2 -> m_name << \" \\'\\n '; return true ; } const std :: shared_ptr < Person > getPartner () const { return m_partner . lock (); } // use lock() to convert weak_ptr to shared_ptr const std :: string & getName () const { return m_name ; } }; int main () { auto lucy { std :: make_shared < Person > ( \"Lucy\" ) }; auto ricky { std :: make_shared < Person > ( \"Ricky\" ) }; partnerUp ( lucy , ricky ); auto partner = ricky -> getPartner (); // get shared_ptr to Ricky's partner std :: cout << ricky -> getName () << \"'s partner is: \" << partner -> getName () << '\\n' ; return 0 ; } COPY This prints: 1 2 3 4 5 6 Lucy created Ricky created Lucy is now partnered with Ricky Ricky's partner is: Lucy Ricky destroyed Lucy destroyed We don\u2019t have to worry about circular dependencies with std::shared_ptr variable \u201cpartner\u201d since it\u2019s just a local variable inside the function. It will eventually go out of scope at the end of the function and the reference count will be decremented by 1.","title":"\u4f7f\u7528 std::weak_ptr"},{"location":"M-8-circular-dependency-issues-with-std-shared-ptr-and-std-weak-ptr/#dangling-pointers-with-stdweak_ptr","text":"Because std::weak_ptr won\u2019t keep an owned resource alive, it\u2019s possible for a std::weak_ptr to be left pointing to a resource that has been deallocated by a std::shared_ptr. Such a std::weak_ptr is dangling, and using it will cause undefined behavior. Here\u2019s a simple example showing how this can happen: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 // h/t to reader Waldo for this example #include #include class Resource { public : Resource () { std :: cerr << \"Resource acquired \\n \" ; } ~ Resource () { std :: cerr << \"Resource destroyed \\n \" ; } }; auto getWeakPtr () { auto ptr { std :: make_shared < Resource > () }; // Resource acquired return std :: weak_ptr { ptr }; } // ptr goes out of scope, Resource destroyed int main () { std :: cerr << \"Getting weak_ptr... \\n \" ; auto ptr { getWeakPtr () }; // dangling std :: cerr << \"Done. \\n \" ; } COPY In the above example, inside getWeakPtr() we use std::make_shared() to create a std::shared_ptr variable named ptr that owns a Resource object. The function returns a std::weak_ptr back to the caller, which does not increment the reference count. Then because ptr is a local variable, it goes out of scope at the end of the function, which decrements the reference count to 0 and deallocates the Resource object. The returned std::weak_ptr is left dangling, pointing to a Resource that was deallocated.","title":"Dangling pointers with std::weak_ptr"},{"location":"M-8-circular-dependency-issues-with-std-shared-ptr-and-std-weak-ptr/#_2","text":"std::shared_ptr can be used when you need multiple smart pointers that can co-own a resource. The resource will be deallocated when the last std::shared_ptr goes out of scope. std::weak_ptr can be used when you want a smart pointer that can see and use a shared resource, but does not participate in the ownership of that resource.","title":"\u5c0f\u7ed3"},{"location":"M-x-chapter-M-comprehensive-review/","tags":["summary"],"text":"\u590d\u4e60 M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb \u667a\u80fd\u6307\u9488\u7c7b \u662f\u4e00\u79cd\u7ec4\u5408\u7c7b\uff0c\u7528\u4e8e\u5bf9\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u8fdb\u884c\u7ba1\u7406\u5e76\u786e\u4fdd\u8be5\u5185\u5b58\u5728\u667a\u80fd\u6307\u9488\u6307\u5411\u7684\u5bf9\u8c61 \u79bb\u5f00\u4f5c\u7528\u57df \u540e\u88ab\u91ca\u653e\u3002 \u62f7\u8d1d\u8bed\u4e49 \u5141\u8bb8\u7c7b\u88ab\u590d\u5236\uff0c\u8be5\u8fc7\u7a0b\u4e3b\u8981\u901a\u8fc7 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u548c \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u6765\u5b8c\u6210\u3002 \u79fb\u52a8\u8bed\u4e49 \u6307\u7684\u662f\u7c7b\u5c06\u5bf9\u8c61\u7684\u6240\u6709\u6743\u8f6c\u8bd1\u7ed9\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u521b\u5efa\u4e00\u4e2a\u62f7\u8d1d\uff0c\u8be5\u8fc7\u7a0b\u4e3b\u8981\u901a\u8fc7 \u79fb\u52a8\u6784\u9020\u51fd\u6570 \u548c \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 \u6765\u5b8c\u6210\u3002 std::auto_ptr \u5df2\u7ecf\u88ab\u5f03\u7528\uff0c\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u3002 M.2 - \u53f3\u503c\u5f15\u7528 \u4e00\u4e2a \u53f3\u503c\u5f15\u7528 \u5f15\u7528\u5e94\u8be5\u4f7f\u7528\u4e00\u4e2a\u53f3\u503c\u6765\u521d\u59cb\u5316\u3002 \u53f3\u503c\u5f15\u7528\u4f7f\u7528\u53cc && \u53f7\u521b\u5efa\u3002\u7f16\u5199\u5c06\u53f3\u503c\u5f15\u7528\u4f5c\u4e3a \u5f62\u53c2 \u7684\u51fd\u6570\u662f\u53ef\u4ee5\u7684\uff0c\u4f46\u662f\u7edd\u5bf9\u4e0d\u8981\u5c06\u53f3\u503c\u5f15\u7528\u4f5c\u4e3a\u8fd4\u56de\u503c\u3002 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c \u5f53\u6211\u4eec\u6784\u5efa\u5bf9\u8c61\u6216\u5bf9\u5bf9\u8c61\u8d4b\u503c\u65f6\uff0c\u5982\u679c\u53c2\u6570\u662f \u5de6\u503c \uff0c\u5219\u552f\u4e00\u80fd\u505a\u7684\u5408\u7406\u64cd\u4f5c\u662f\u62f7\u8d1d\u8be5\u5de6\u503c\u3002\u6211\u4eec\u4e0d\u80fd\u5047\u5b9a\u6539\u53d8\u8be5\u5de6\u503c\u662f\u5b89\u5168\u7684\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u4f1a\u5728\u540e\u9762\u7684\u7a0b\u5e8f\u4e2d\u518d\u6b21\u88ab\u4f7f\u7528\u3002\u5bf9\u4e8e\u8868\u8fbe\u5f0f \u201ca = b\u201d\uff0c\u6211\u4eec\u6ca1\u6709\u7406\u7531\u671f\u671bb\u4ee5\u4efb\u4f55\u65b9\u5f0f\u88ab\u6539\u53d8\u3002 \u4f46\u662f\uff0c\u5f53\u6784\u5efa\u5bf9\u8c61\u6216\u8005\u8d4b\u503c\u5bf9\u8c61\u65f6\uff0c\u5982\u679c\u53c2\u6570\u662f \u53f3\u503c \uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u786e\u5b9a\u8be5\u53f3\u503c\u80af\u5b9a\u662f\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\u3002\u4e0e\u5176\u62f7\u8d1d\uff08\u5f00\u9500\u5927\uff09\u5b83\uff0c\u4e0d\u5982\u76f4\u63a5\u5c06\u5b83\u7684\u8d44\u6e90\u8f6c\u79fb\uff08\u5f00\u9500\u5c0f\uff09\u7ed9\u88ab\u6784\u5efa\u6216\u88ab\u8d4b\u503c\u7684\u5bf9\u8c61\u3002\u8fd9\u4e48\u505a\u662f\u5b89\u5168\u7684\uff0c\u56e0\u4e3a\u4e34\u65f6\u5bf9\u8c61\u603b\u662f\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u65f6\u5c31\u88ab\u9500\u6bc1\uff0c\u6240\u4ee5\u80af\u5b9a\u4e0d\u4f1a\u518d\u6b21\u88ab\u4f7f\u7528\u4e86\u3002 \u4f60\u53ef\u4ee5\u4f7f\u7528 delete \u5173\u952e\u5b57\u7981\u7528\u62f7\u8d1d\u8bed\u4e49\uff08\u901a\u8fc7\u5220\u9664\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26\uff09 M.4 - std::move std::move \u5141\u8bb8\u6211\u4eec\u5c06\u5de6\u503c\u5f53\u505a\u53f3\u503c\u5904\u7406\u3002\u5f53\u9700\u8981\u57fa\u4e8e\u7ec4\u7ec7\u8c03\u7528\u6216\u6fc0\u6d3b\u79fb\u52a8\u8bed\u4e49\u800c\u4e0d\u662f\u62f7\u8d1d\u8bed\u4e49\u65f6\u8fd9\u5f88\u6709\u7528\u3002 M.5 - std::move_if_noexcept \u5f53\u5bf9\u8c61\u6709\u4e00\u4e2a noexcept \u79fb\u52a8\u6784\u9020\u51fd\u6570\u65f6\uff0c std::move_if_noexcept \u4f1a\u8fd4\u56de\u4e00\u4e2a\u53ef\u79fb\u52a8\u7684\u53f3\u503c\u3002\u5426\u5219\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u53ef\u4ee5\u62f7\u8d1d\u7684\u5de6\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 noexcept \u4fee\u9970\u7b26\u548c std::move_if_noexcept \u786e\u4fdd\u53ea\u5728\u5177\u6709 \u5f3a\u5f02\u5e38\u4fdd\u8bc1 \u65f6\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\uff0c\u5176\u4ed6\u60c5\u51b5\u4e0b\u4f7f\u7528\u62f7\u8d1d\u8bed\u4e49\u3002 M.6 \u2014 std::unique_ptr \u667a\u80fd\u6307\u9488 std::unique_ptr \u5f88\u5e38\u7528\u3002\u5b83\u7528\u4e8e\u7ba1\u7406\u4e00\u4e2a\u4e0d\u53ef\u5171\u4eab\u7684\u8d44\u6e90\u3002\u5e94\u8be5\u4f7f\u7528 std::make_unique() (C++14) \u6765\u521b\u5efa std::unique_ptr \u3002 std::unique_ptr \u7981\u7528\u4e86\u62f7\u8d1d\u8bed\u4e49 M.7 \u2014 std::shared_ptr \u5f53\u591a\u4e2a\u5bf9\u8c61\u9700\u8981\u8bbf\u95ee\u540c\u4e00\u4e2a\u8d44\u6e90\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 std::shared_ptr \u3002\u5728\u6700\u540e\u4e00\u4e2a std::shared_ptr \u88ab\u9500\u6bc1\u524d\uff0c\u8d44\u6e90\u4e0d\u4f1a\u88ab\u9500\u6bc1\u3002\u5e94\u8be5\u4f7f\u7528 std::make_shared() \u6765\u521b\u5efa std::shared_ptr \u3002 \u5982\u679c\u9700\u8981\u521b\u5efa std::shared_ptr \u6307\u5411\u4e00\u4e2a\u76f8\u540c\u7684\u8d44\u6e90\uff0c\u5219\u9700\u8981\u4f7f\u7528\u62f7\u8d1d\u8bed\u4e49\u4ece std::shared_ptr \u521b\u5efa\u3002 M.8 \u2014 \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898 \u5f53\u4f60\u9700\u8981\u4e00\u4e2a\u6216\u591a\u4e2a\u5bf9\u8c61\u6765\u89c2\u5bdf\u6216\u83b7\u53d6\u7531 std::shared_pr \u7ba1\u7406\u7684\u8d44\u6e90\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u667a\u80fd\u6307\u9488 std::weak_ptr \u3002\u4f46\u662f\uff0c\u548c std::shared_ptr \u4e0d\u540c\u7684\u662f\uff0c\u5728\u5224\u65ad\u8d44\u6e90\u662f\u5426\u9700\u8981\u88ab\u9500\u6bc1\u65f6\uff0c\u6307\u5411\u5b83\u7684 std::weak_ptr \u7684\u4e2a\u6570\u4e0d\u4f1a\u88ab\u8003\u8651\u5728\u5185\u3002","title":"M.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u79fb\u52a8\u548c\u667a\u80fd\u6307\u9488"},{"location":"M-x-chapter-M-comprehensive-review/#_1","text":"M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb \u667a\u80fd\u6307\u9488\u7c7b \u662f\u4e00\u79cd\u7ec4\u5408\u7c7b\uff0c\u7528\u4e8e\u5bf9\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u8fdb\u884c\u7ba1\u7406\u5e76\u786e\u4fdd\u8be5\u5185\u5b58\u5728\u667a\u80fd\u6307\u9488\u6307\u5411\u7684\u5bf9\u8c61 \u79bb\u5f00\u4f5c\u7528\u57df \u540e\u88ab\u91ca\u653e\u3002 \u62f7\u8d1d\u8bed\u4e49 \u5141\u8bb8\u7c7b\u88ab\u590d\u5236\uff0c\u8be5\u8fc7\u7a0b\u4e3b\u8981\u901a\u8fc7 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u548c \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u6765\u5b8c\u6210\u3002 \u79fb\u52a8\u8bed\u4e49 \u6307\u7684\u662f\u7c7b\u5c06\u5bf9\u8c61\u7684\u6240\u6709\u6743\u8f6c\u8bd1\u7ed9\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u521b\u5efa\u4e00\u4e2a\u62f7\u8d1d\uff0c\u8be5\u8fc7\u7a0b\u4e3b\u8981\u901a\u8fc7 \u79fb\u52a8\u6784\u9020\u51fd\u6570 \u548c \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 \u6765\u5b8c\u6210\u3002 std::auto_ptr \u5df2\u7ecf\u88ab\u5f03\u7528\uff0c\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u3002 M.2 - \u53f3\u503c\u5f15\u7528 \u4e00\u4e2a \u53f3\u503c\u5f15\u7528 \u5f15\u7528\u5e94\u8be5\u4f7f\u7528\u4e00\u4e2a\u53f3\u503c\u6765\u521d\u59cb\u5316\u3002 \u53f3\u503c\u5f15\u7528\u4f7f\u7528\u53cc && \u53f7\u521b\u5efa\u3002\u7f16\u5199\u5c06\u53f3\u503c\u5f15\u7528\u4f5c\u4e3a \u5f62\u53c2 \u7684\u51fd\u6570\u662f\u53ef\u4ee5\u7684\uff0c\u4f46\u662f\u7edd\u5bf9\u4e0d\u8981\u5c06\u53f3\u503c\u5f15\u7528\u4f5c\u4e3a\u8fd4\u56de\u503c\u3002 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c \u5f53\u6211\u4eec\u6784\u5efa\u5bf9\u8c61\u6216\u5bf9\u5bf9\u8c61\u8d4b\u503c\u65f6\uff0c\u5982\u679c\u53c2\u6570\u662f \u5de6\u503c \uff0c\u5219\u552f\u4e00\u80fd\u505a\u7684\u5408\u7406\u64cd\u4f5c\u662f\u62f7\u8d1d\u8be5\u5de6\u503c\u3002\u6211\u4eec\u4e0d\u80fd\u5047\u5b9a\u6539\u53d8\u8be5\u5de6\u503c\u662f\u5b89\u5168\u7684\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u4f1a\u5728\u540e\u9762\u7684\u7a0b\u5e8f\u4e2d\u518d\u6b21\u88ab\u4f7f\u7528\u3002\u5bf9\u4e8e\u8868\u8fbe\u5f0f \u201ca = b\u201d\uff0c\u6211\u4eec\u6ca1\u6709\u7406\u7531\u671f\u671bb\u4ee5\u4efb\u4f55\u65b9\u5f0f\u88ab\u6539\u53d8\u3002 \u4f46\u662f\uff0c\u5f53\u6784\u5efa\u5bf9\u8c61\u6216\u8005\u8d4b\u503c\u5bf9\u8c61\u65f6\uff0c\u5982\u679c\u53c2\u6570\u662f \u53f3\u503c \uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u786e\u5b9a\u8be5\u53f3\u503c\u80af\u5b9a\u662f\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\u3002\u4e0e\u5176\u62f7\u8d1d\uff08\u5f00\u9500\u5927\uff09\u5b83\uff0c\u4e0d\u5982\u76f4\u63a5\u5c06\u5b83\u7684\u8d44\u6e90\u8f6c\u79fb\uff08\u5f00\u9500\u5c0f\uff09\u7ed9\u88ab\u6784\u5efa\u6216\u88ab\u8d4b\u503c\u7684\u5bf9\u8c61\u3002\u8fd9\u4e48\u505a\u662f\u5b89\u5168\u7684\uff0c\u56e0\u4e3a\u4e34\u65f6\u5bf9\u8c61\u603b\u662f\u5728\u8868\u8fbe\u5f0f\u7ed3\u675f\u65f6\u5c31\u88ab\u9500\u6bc1\uff0c\u6240\u4ee5\u80af\u5b9a\u4e0d\u4f1a\u518d\u6b21\u88ab\u4f7f\u7528\u4e86\u3002 \u4f60\u53ef\u4ee5\u4f7f\u7528 delete \u5173\u952e\u5b57\u7981\u7528\u62f7\u8d1d\u8bed\u4e49\uff08\u901a\u8fc7\u5220\u9664\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26\uff09 M.4 - std::move std::move \u5141\u8bb8\u6211\u4eec\u5c06\u5de6\u503c\u5f53\u505a\u53f3\u503c\u5904\u7406\u3002\u5f53\u9700\u8981\u57fa\u4e8e\u7ec4\u7ec7\u8c03\u7528\u6216\u6fc0\u6d3b\u79fb\u52a8\u8bed\u4e49\u800c\u4e0d\u662f\u62f7\u8d1d\u8bed\u4e49\u65f6\u8fd9\u5f88\u6709\u7528\u3002 M.5 - std::move_if_noexcept \u5f53\u5bf9\u8c61\u6709\u4e00\u4e2a noexcept \u79fb\u52a8\u6784\u9020\u51fd\u6570\u65f6\uff0c std::move_if_noexcept \u4f1a\u8fd4\u56de\u4e00\u4e2a\u53ef\u79fb\u52a8\u7684\u53f3\u503c\u3002\u5426\u5219\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u53ef\u4ee5\u62f7\u8d1d\u7684\u5de6\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 noexcept \u4fee\u9970\u7b26\u548c std::move_if_noexcept \u786e\u4fdd\u53ea\u5728\u5177\u6709 \u5f3a\u5f02\u5e38\u4fdd\u8bc1 \u65f6\u4f7f\u7528\u79fb\u52a8\u8bed\u4e49\uff0c\u5176\u4ed6\u60c5\u51b5\u4e0b\u4f7f\u7528\u62f7\u8d1d\u8bed\u4e49\u3002 M.6 \u2014 std::unique_ptr \u667a\u80fd\u6307\u9488 std::unique_ptr \u5f88\u5e38\u7528\u3002\u5b83\u7528\u4e8e\u7ba1\u7406\u4e00\u4e2a\u4e0d\u53ef\u5171\u4eab\u7684\u8d44\u6e90\u3002\u5e94\u8be5\u4f7f\u7528 std::make_unique() (C++14) \u6765\u521b\u5efa std::unique_ptr \u3002 std::unique_ptr \u7981\u7528\u4e86\u62f7\u8d1d\u8bed\u4e49 M.7 \u2014 std::shared_ptr \u5f53\u591a\u4e2a\u5bf9\u8c61\u9700\u8981\u8bbf\u95ee\u540c\u4e00\u4e2a\u8d44\u6e90\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 std::shared_ptr \u3002\u5728\u6700\u540e\u4e00\u4e2a std::shared_ptr \u88ab\u9500\u6bc1\u524d\uff0c\u8d44\u6e90\u4e0d\u4f1a\u88ab\u9500\u6bc1\u3002\u5e94\u8be5\u4f7f\u7528 std::make_shared() \u6765\u521b\u5efa std::shared_ptr \u3002 \u5982\u679c\u9700\u8981\u521b\u5efa std::shared_ptr \u6307\u5411\u4e00\u4e2a\u76f8\u540c\u7684\u8d44\u6e90\uff0c\u5219\u9700\u8981\u4f7f\u7528\u62f7\u8d1d\u8bed\u4e49\u4ece std::shared_ptr \u521b\u5efa\u3002 M.8 \u2014 \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898 \u5f53\u4f60\u9700\u8981\u4e00\u4e2a\u6216\u591a\u4e2a\u5bf9\u8c61\u6765\u89c2\u5bdf\u6216\u83b7\u53d6\u7531 std::shared_pr \u7ba1\u7406\u7684\u8d44\u6e90\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u667a\u80fd\u6307\u9488 std::weak_ptr \u3002\u4f46\u662f\uff0c\u548c std::shared_ptr \u4e0d\u540c\u7684\u662f\uff0c\u5728\u5224\u65ad\u8d44\u6e90\u662f\u5426\u9700\u8981\u88ab\u9500\u6bc1\u65f6\uff0c\u6307\u5411\u5b83\u7684 std::weak_ptr \u7684\u4e2a\u6570\u4e0d\u4f1a\u88ab\u8003\u8651\u5728\u5185\u3002","title":"\u590d\u4e60"},{"location":"MOC/","text":"Mind Map 1. C++ \u57fa\u7840 1.1 - \u7a0b\u5e8f\u7ed3\u6784\u548c\u8bed\u53e5 1.2 - \u6ce8\u91ca 1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 1.5 - iostream \u7b80\u4ecb\uff1acout\uff0ccin \u548c endl 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a 1.7 - \u5173\u952e\u5b57\u548c\u6807\u8bc6\u7b26 1.8 - \u7a7a\u767d\u7b26\u548c\u57fa\u672c\u6392\u7248 1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 1.10 - \u8868\u8fbe\u5f0f\u7b80\u4ecb 1.11 - \u5f00\u53d1\u4f60\u7684\u7b2c\u4e00\u4e2a\u7a0b\u5e8f 1.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - C++\u57fa\u7840 2. C++ \u57fa\u7840\u2014\u2014\u51fd\u6570\u548c\u6587\u4ef6 2.1 - \u51fd\u6570\u7b80\u4ecb 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 2.3 - void \u51fd\u6570 2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2 2.5 - \u5c40\u90e8\u4f5c\u7528\u57df 2.6 - \u4e3a\u4ec0\u4e48\u51fd\u6570\u6709\u7528\uff0c\u5982\u4f55\u9ad8\u6548\u4f7f\u7528\u51fd\u6570 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f 2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb 2.11 - \u5934\u6587\u4ef6 2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e 2.13 - \u5982\u4f55\u8bbe\u8ba1\u7b2c\u4e00\u4e2a\u7a0b\u5e8f 2.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570\u4e0e\u6587\u4ef6 3. \u8c03\u8bd5 C++ \u7a0b\u5e8f \ud83d\udea7 3.1 - \u8bed\u6cd5\u548c\u8bed\u4e49\u9519\u8bef 3.2 - \u4ee3\u7801\u8c03\u8bd5\u6b65\u9aa4 3.3 - \u4ee3\u7801\u8c03\u8bd5\u7b56\u7565 3.4 \u2014 \u57fa\u672c\u4ee3\u7801\u8c03\u8bd5\u6280\u672f 3.5 - \u66f4\u591a\u4ee3\u7801\u8c03\u8bd5\u6280\u672f 3.6 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u5355\u6b65\u8c03\u8bd5 3.7 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8fd0\u884c\u548c\u65ad\u70b9 3.8 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u76d1\u63a7\u53d8\u91cf 3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808 3.10 - \u5728\u95ee\u9898\u53d1\u751f\u524d\u53d1\u73b0\u95ee\u9898 3.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4ee3\u7801\u8c03\u8bd5 4. \u57fa\u7840\u6570\u636e\u7c7b\u578b 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb 4.2 - Void 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 4.4 - \u6709\u7b26\u53f7\u6574\u578b 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 4.8 - \u6d6e\u70b9\u6570 4.9 - \u5e03\u5c14\u503c 4.10 - if \u8bed\u53e5\u7b80\u4ecb 4.11 - \u5b57\u7b26 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr 4.15 - \u5b57\u9762\u91cf 4.16 - \u6570\u503c\u7cfb\u7edf\uff08\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236\uff09 4.17 - std::string \u7b80\u4ecb 4.18 - std::string_view \u7b80\u4ecb 4.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u57fa\u672c\u6570\u636e\u7c7b\u578b 5. \u8fd0\u7b97\u7b26 5.1 - \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 5.3 - \u6c42\u6a21\u548c\u6307\u6570\u8fd0\u7b97 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 5.5 - \u9017\u53f7\u548c\u6761\u4ef6\u8fd0\u7b97\u7b26 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 5.7 - \u903b\u8f91\u8fd0\u7b97\u7b26 5.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26 6. \u4f5c\u7528\u57df\u3001\u751f\u547d\u5468\u671f\u548c\u94fe\u63a5 6.1 - \u590d\u5408\u8bed\u53e5\uff08\u8bed\u53e5\u5757\uff09 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 6.3 - \u5c40\u90e8\u53d8\u91cf 6.4 - \u5168\u5c40\u53d8\u91cf 6.5 - \u53d8\u91cf\u906e\u853d 6.6 - \u5185\u90e8\u94fe\u63a5 6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e 6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf 6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 6.13 - \u5185\u8054\u51fd\u6570 6.14 - Constexpr \u548c consteval \u51fd\u6570 6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 7. \u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406 \ud83d\udea7 7.1 - \u63a7\u5236\u6d41 7.2 - if \u8bed\u53e5\u548c\u8bed\u53e5\u5757 7.3 - \u5e38\u89c1\u7684 if \u8bed\u53e5\u9519\u8bef 7.4 - switch \u8bed\u53e5\u57fa\u7840 7.5 - switch fallthrough\u5c5e\u6027\u548c\u4f5c\u7528\u57df 7.6 - goto \u8bed\u53e5 7.7 - \u5faa\u73af\u548c while \u8bed\u53e5 7.8 - do-while \u8bed\u53e5 7.9 - for \u8bed\u53e5 7.10 - break \u548c continue 7.11 - \u6302\u8d77\uff08\u63d0\u524d\u7ed3\u675f\u7a0b\u5e8f\uff09 7.12 - \u4ee3\u7801\u6d4b\u8bd5 7.13 - \u4ee3\u7801\u8986\u76d6\u7387 7.14 - C++ \u4e2d\u5e38\u89c1\u7684\u8bed\u4e49\u9519\u8bef 7.15 - \u53d1\u73b0\u9519\u8bef\u548c\u5904\u7406\u9519\u8bef 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406 7.17 - \u65ad\u8a00\u548c static_assert 7.18 - \u968f\u673a\u6570\u751f\u6210\u5668\u7b80\u4ecb 7.19 - \u4f7f\u7528 Mersenne Twister \u751f\u6210\u968f\u673a\u6570 7.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406 8. \u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 8.3 - \u6570\u503c\u8f6c\u6362 8.4 - \u7b97\u672f\u8f6c\u6362 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast 8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad 8.9 - \u51fd\u6570\u91cd\u8f7d 8.10 - \u91cd\u8f7d\u51fd\u6570\u7684\u533a\u5206 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u4e0d\u660e\u786e\u5339\u914d 8.12 - \u9ed8\u8ba4\u53c2\u6570 8.13 - \u51fd\u6570\u6a21\u677f 8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f 8.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 9. \u590d\u5408\u7c7b\u578b-\u5f15\u7528\u548c\u6307\u9488 9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b 9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09 9.3 - \u5de6\u503c\u5f15\u7528 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 9.6 - \u6307\u9488\u7b80\u4ecb 9.7 - \u7a7a\u6307\u9488 9.8 - \u6307\u9488\u548cconst 9.9 - \u6309\u5730\u5740\u4f20\u9012 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad 9.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f15\u7528\u548c\u6307\u9488 10. \u590d\u5408\u7c7b\u578b-\u679a\u4e3e\u548c\u7ed3\u6784\u4f53 10.1 - \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7b80\u4ecb 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b 10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 10.8 - \u7ed3\u6784\u4f53\u4f20\u9012\u53ca\u5176\u4ed6 10.9 - \u57fa\u4e8e\u6307\u9488\u548c\u5f15\u7528\u7684\u6210\u5458\u9009\u62e9 10.10 - \u7c7b\u6a21\u677f 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD 10.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u679a\u4e3e\u548c\u7ed3\u6784\u4f53 10.y - \u5b66\u4f1a\u4f7f\u7528C++\u53c2\u8003\u624b\u518c 11. \u6570\u7ec4\uff0c\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d 11.1 - \u6570\u7ec4\uff08\u7b2c\u4e00\u90e8\u5206\uff09 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.3 - \u6570\u7ec4\u548c\u5faa\u73af 11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f 11.5 - \u591a\u7ef4\u6570\u7ec4 11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.8 - \u6307\u9488\u548c\u6570\u7ec4 11.9 - \u6307\u9488\u8fd0\u7b97\u548c\u6570\u7ec4\u7d22\u5f15 11.10 - C \u98ce\u683c\u5b57\u7b26\u4e32\u7b26\u53f7\u5e38\u91cf 11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d 11.12 - \u52a8\u6001\u5206\u914d\u6570\u7ec4 11.13 - for-each \u5faa\u73af 11.14 - void \u6307\u9488 11.15 - \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u548c\u591a\u7ef4\u6570\u7ec4 11.16 - \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb 11.17 - \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb 11.18 - \u8fed\u4ee3\u5668\u7b80\u4ecb 11.19 - \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb 11.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5206\u914d 12. \u51fd\u6570 12.1 - \u51fd\u6570\u6307\u9488 12.2 - \u6808\u548c\u5806 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a 12.4 - \u9012\u5f52 12.5 - \u547d\u4ee4\u884c\u53c2\u6570 12.6 - \u7701\u7565\u53f7\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb 12.8 - lambda \u95ed\u5305 12.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570 13. \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u57fa\u7840 13.1 - \u6b22\u8fce\u6765\u5230\u9762\u5411\u5bf9\u8c61\u7684\u4e16\u754c 13.2 - \u7c7b\u548c\u7c7b\u6210\u5458 13.3 - \u8bbf\u95ee\u4fee\u9970\u7b26 public \u548c private 13.4 - \u6210\u5458\u8bbf\u95ee\u51fd\u6570\u4e0e\u5c01\u88c5 13.5 - \u6784\u9020\u51fd\u6570 13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 13.8 - \u91cd\u53e0\u548c\u59d4\u6d3e\u6784\u9020\u51fd\u6570 13.9 - \u6790\u6784\u51fd\u6570 13.10 - \u9690\u85cf\u7684this\u6307\u9488 13.11 - \u7c7b\u4ee3\u7801\u548c\u5934\u6587\u4ef6 13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570 13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf 13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570 13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b 13.16 - \u533f\u540d\u5bf9\u8c61 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b 13.18 - \u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 13.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u9762\u5411\u5bf9\u8c61\u57fa\u7840 14. \u64cd\u4f5c\u7b26\u91cd\u8f7d \ud83d\udea7 14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26 14.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26\u91cd\u8f7d 16. \u5bf9\u8c61\u5173\u7cfb\u7b80\u4ecb 16.1 - \u5bf9\u8c61\u5173\u7cfb 16.2 - \u7ec4\u5408\u5173\u7cfb 16.3 - \u805a\u5408\u5173\u7cfb 16.4 - \u5173\u8054\u5173\u7cfb 16.5 - \u4f9d\u8d56\u5173\u7cfb 16.6 - \u5bb9\u5668\u7c7b 16.7 - std::initializer_list 16.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5bf9\u8c61\u5173\u7cfb 17. \u7ee7\u627f 17.1 - \u7ee7\u627f\u7b80\u4ecb 17.2 - C++\u7ee7\u627f\u57fa\u7840 17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f 17.4 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u548c\u521d\u59cb\u5316 17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 17.6 - \u4e3a\u6d3e\u751f\u7c7b\u6dfb\u52a0\u65b0\u529f\u80fd 17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570 17.9 - \u591a\u91cd\u7ee7\u627f 17.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7ee7\u627f 18. \u865a\u51fd\u6570 18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b 18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316 18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a 18.6 - \u865a\u51fd\u6570\u8868 18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u7c7b 18.8 - \u865a\u57fa\u7c7b 18.9 - \u5bf9\u8c61\u5207\u7247 18.10 - \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 18.11 - \u4f7f\u7528<<\u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b 18.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u865a\u51fd\u6570 19. \u6a21\u677f\u548c\u7c7b \ud83d\udea7 19.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6a21\u677f\u548c\u7c7b 20. \u5f02\u5e38 \ud83d\udea7 20.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f02\u5e38 20.4 M. move \u548c\u667a\u80fd\u6307\u9488 M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb M.2 - \u53f3\u503c\u5f15\u7528 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c M.4 - std::move M.5 - std::move_if_noexcept M.6 \u2014 std::unique_ptr M.7 \u2014 std::shared_ptr M.8 \u2014 \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898 M.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u79fb\u52a8\u548c\u667a\u80fd\u6307\u9488 21. STL 21.1 - \u6807\u51c6\u5e93 21.2 - STL\u5bb9\u5668 21.3 - STL\u8fed\u4ee3\u5668 21.4 - STL\u7b97\u6cd5 22. std \u5b57\u7b26\u4e32 22.1 - std::string \u548c std::wstring 22.2 - std-string \u7684\u6784\u9020\u548c\u6790\u6784 22.3 - std-string \u7684\u957f\u5ea6\u548c\u5bb9\u91cf 22.4 - std-string \u5b57\u7b26\u8bbf\u95ee\u548cC\u5b57\u7b26\u6570\u7ec4\u8f6c\u6362 22.5 - std-string \u8d4b\u503c\u548c\u4ea4\u6362 22.6 - std-string \u8ffd\u52a0 22.7 - std-string \u63d2\u5165 23. \u8f93\u5165\u8f93\u51fa 23.1 - \u8f93\u5165\u8f93\u51fa\u6d41 23.2 - \u4f7f\u7528istream\u5904\u7406\u8f93\u5165 23.3 - \u4f7f\u7528ostream\u548cios\u5904\u7406\u8f93\u51fa 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b 23.5 - \u6d41\u72b6\u6001\u548c\u8f93\u5165\u9a8c\u8bc1 23.6 - \u57fa\u672c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa 23.7 - \u968f\u673a\u6587\u4ef6\u8f93\u5165\u8f93\u51fa A. \u5176\u4ed6\u8bdd\u9898 A.1 - \u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93 A.2 - \u5728visual studio\u4e2d\u4f7f\u7528\u5e93 A.3 - \u5728Code Blocks\u4e2d\u4f7f\u7528\u5e93 A.4 \u2014 C++ FAQ B. C++\u6807\u51c6\u66f4\u65b0 B.1 - C++11 B.2 - C++14 B.3 - C++17 B.4 - C++20 O1. \u4f4d\u8fd0\u7b97 \ud83d\udea7 O.1 - bit\u6807\u8bb0\u548c\u901a\u8fc7std::bitset\u64cd\u4f5cbit O.2 - \u4f4d\u8fd0\u7b97\u7b26 O.3 - \u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u548c\u63a9\u7801\u64cd\u4f5c\u4f4d O.4 - \u4e8c\u8fdb\u5236\u548c\u5341\u8fdb\u5236\u8f6c\u6362","title":"\u603b\u76ee\u5f55"},{"location":"MOC/#1-c","text":"1.1 - \u7a0b\u5e8f\u7ed3\u6784\u548c\u8bed\u53e5 1.2 - \u6ce8\u91ca 1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 1.5 - iostream \u7b80\u4ecb\uff1acout\uff0ccin \u548c endl 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a 1.7 - \u5173\u952e\u5b57\u548c\u6807\u8bc6\u7b26 1.8 - \u7a7a\u767d\u7b26\u548c\u57fa\u672c\u6392\u7248 1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 1.10 - \u8868\u8fbe\u5f0f\u7b80\u4ecb 1.11 - \u5f00\u53d1\u4f60\u7684\u7b2c\u4e00\u4e2a\u7a0b\u5e8f 1.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - C++\u57fa\u7840","title":"1. C++ \u57fa\u7840"},{"location":"MOC/#2-c","text":"2.1 - \u51fd\u6570\u7b80\u4ecb 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 2.3 - void \u51fd\u6570 2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2 2.5 - \u5c40\u90e8\u4f5c\u7528\u57df 2.6 - \u4e3a\u4ec0\u4e48\u51fd\u6570\u6709\u7528\uff0c\u5982\u4f55\u9ad8\u6548\u4f7f\u7528\u51fd\u6570 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f 2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb 2.11 - \u5934\u6587\u4ef6 2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e 2.13 - \u5982\u4f55\u8bbe\u8ba1\u7b2c\u4e00\u4e2a\u7a0b\u5e8f 2.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570\u4e0e\u6587\u4ef6","title":"2. C++ \u57fa\u7840\u2014\u2014\u51fd\u6570\u548c\u6587\u4ef6"},{"location":"MOC/#3-c","text":"3.1 - \u8bed\u6cd5\u548c\u8bed\u4e49\u9519\u8bef 3.2 - \u4ee3\u7801\u8c03\u8bd5\u6b65\u9aa4 3.3 - \u4ee3\u7801\u8c03\u8bd5\u7b56\u7565 3.4 \u2014 \u57fa\u672c\u4ee3\u7801\u8c03\u8bd5\u6280\u672f 3.5 - \u66f4\u591a\u4ee3\u7801\u8c03\u8bd5\u6280\u672f 3.6 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u5355\u6b65\u8c03\u8bd5 3.7 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8fd0\u884c\u548c\u65ad\u70b9 3.8 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u76d1\u63a7\u53d8\u91cf 3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808 3.10 - \u5728\u95ee\u9898\u53d1\u751f\u524d\u53d1\u73b0\u95ee\u9898 3.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4ee3\u7801\u8c03\u8bd5","title":"3. \u8c03\u8bd5 C++ \u7a0b\u5e8f \ud83d\udea7"},{"location":"MOC/#4","text":"4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb 4.2 - Void 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 4.4 - \u6709\u7b26\u53f7\u6574\u578b 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 4.8 - \u6d6e\u70b9\u6570 4.9 - \u5e03\u5c14\u503c 4.10 - if \u8bed\u53e5\u7b80\u4ecb 4.11 - \u5b57\u7b26 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr 4.15 - \u5b57\u9762\u91cf 4.16 - \u6570\u503c\u7cfb\u7edf\uff08\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236\uff09 4.17 - std::string \u7b80\u4ecb 4.18 - std::string_view \u7b80\u4ecb 4.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u57fa\u672c\u6570\u636e\u7c7b\u578b","title":"4. \u57fa\u7840\u6570\u636e\u7c7b\u578b"},{"location":"MOC/#5","text":"5.1 - \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 5.3 - \u6c42\u6a21\u548c\u6307\u6570\u8fd0\u7b97 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 5.5 - \u9017\u53f7\u548c\u6761\u4ef6\u8fd0\u7b97\u7b26 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 5.7 - \u903b\u8f91\u8fd0\u7b97\u7b26 5.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26","title":"5. \u8fd0\u7b97\u7b26"},{"location":"MOC/#6","text":"6.1 - \u590d\u5408\u8bed\u53e5\uff08\u8bed\u53e5\u5757\uff09 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 6.3 - \u5c40\u90e8\u53d8\u91cf 6.4 - \u5168\u5c40\u53d8\u91cf 6.5 - \u53d8\u91cf\u906e\u853d 6.6 - \u5185\u90e8\u94fe\u63a5 6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e 6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf 6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 6.13 - \u5185\u8054\u51fd\u6570 6.14 - Constexpr \u548c consteval \u51fd\u6570 6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5","title":"6. \u4f5c\u7528\u57df\u3001\u751f\u547d\u5468\u671f\u548c\u94fe\u63a5"},{"location":"MOC/#7","text":"7.1 - \u63a7\u5236\u6d41 7.2 - if \u8bed\u53e5\u548c\u8bed\u53e5\u5757 7.3 - \u5e38\u89c1\u7684 if \u8bed\u53e5\u9519\u8bef 7.4 - switch \u8bed\u53e5\u57fa\u7840 7.5 - switch fallthrough\u5c5e\u6027\u548c\u4f5c\u7528\u57df 7.6 - goto \u8bed\u53e5 7.7 - \u5faa\u73af\u548c while \u8bed\u53e5 7.8 - do-while \u8bed\u53e5 7.9 - for \u8bed\u53e5 7.10 - break \u548c continue 7.11 - \u6302\u8d77\uff08\u63d0\u524d\u7ed3\u675f\u7a0b\u5e8f\uff09 7.12 - \u4ee3\u7801\u6d4b\u8bd5 7.13 - \u4ee3\u7801\u8986\u76d6\u7387 7.14 - C++ \u4e2d\u5e38\u89c1\u7684\u8bed\u4e49\u9519\u8bef 7.15 - \u53d1\u73b0\u9519\u8bef\u548c\u5904\u7406\u9519\u8bef 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406 7.17 - \u65ad\u8a00\u548c static_assert 7.18 - \u968f\u673a\u6570\u751f\u6210\u5668\u7b80\u4ecb 7.19 - \u4f7f\u7528 Mersenne Twister \u751f\u6210\u968f\u673a\u6570 7.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406","title":"7. \u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406 \ud83d\udea7"},{"location":"MOC/#8","text":"8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 8.3 - \u6570\u503c\u8f6c\u6362 8.4 - \u7b97\u672f\u8f6c\u6362 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast 8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad 8.9 - \u51fd\u6570\u91cd\u8f7d 8.10 - \u91cd\u8f7d\u51fd\u6570\u7684\u533a\u5206 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u4e0d\u660e\u786e\u5339\u914d 8.12 - \u9ed8\u8ba4\u53c2\u6570 8.13 - \u51fd\u6570\u6a21\u677f 8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f 8.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5","title":"8. \u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d"},{"location":"MOC/#9-","text":"9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b 9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09 9.3 - \u5de6\u503c\u5f15\u7528 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 9.6 - \u6307\u9488\u7b80\u4ecb 9.7 - \u7a7a\u6307\u9488 9.8 - \u6307\u9488\u548cconst 9.9 - \u6309\u5730\u5740\u4f20\u9012 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad 9.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f15\u7528\u548c\u6307\u9488","title":"9. \u590d\u5408\u7c7b\u578b-\u5f15\u7528\u548c\u6307\u9488"},{"location":"MOC/#10-","text":"10.1 - \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7b80\u4ecb 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b 10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 10.8 - \u7ed3\u6784\u4f53\u4f20\u9012\u53ca\u5176\u4ed6 10.9 - \u57fa\u4e8e\u6307\u9488\u548c\u5f15\u7528\u7684\u6210\u5458\u9009\u62e9 10.10 - \u7c7b\u6a21\u677f 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD 10.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u679a\u4e3e\u548c\u7ed3\u6784\u4f53 10.y - \u5b66\u4f1a\u4f7f\u7528C++\u53c2\u8003\u624b\u518c","title":"10. \u590d\u5408\u7c7b\u578b-\u679a\u4e3e\u548c\u7ed3\u6784\u4f53"},{"location":"MOC/#11","text":"11.1 - \u6570\u7ec4\uff08\u7b2c\u4e00\u90e8\u5206\uff09 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.3 - \u6570\u7ec4\u548c\u5faa\u73af 11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f 11.5 - \u591a\u7ef4\u6570\u7ec4 11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.8 - \u6307\u9488\u548c\u6570\u7ec4 11.9 - \u6307\u9488\u8fd0\u7b97\u548c\u6570\u7ec4\u7d22\u5f15 11.10 - C \u98ce\u683c\u5b57\u7b26\u4e32\u7b26\u53f7\u5e38\u91cf 11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d 11.12 - \u52a8\u6001\u5206\u914d\u6570\u7ec4 11.13 - for-each \u5faa\u73af 11.14 - void \u6307\u9488 11.15 - \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u548c\u591a\u7ef4\u6570\u7ec4 11.16 - \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb 11.17 - \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb 11.18 - \u8fed\u4ee3\u5668\u7b80\u4ecb 11.19 - \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb 11.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5206\u914d","title":"11. \u6570\u7ec4\uff0c\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d"},{"location":"MOC/#12","text":"12.1 - \u51fd\u6570\u6307\u9488 12.2 - \u6808\u548c\u5806 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a 12.4 - \u9012\u5f52 12.5 - \u547d\u4ee4\u884c\u53c2\u6570 12.6 - \u7701\u7565\u53f7\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb 12.8 - lambda \u95ed\u5305 12.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570","title":"12. \u51fd\u6570"},{"location":"MOC/#13","text":"13.1 - \u6b22\u8fce\u6765\u5230\u9762\u5411\u5bf9\u8c61\u7684\u4e16\u754c 13.2 - \u7c7b\u548c\u7c7b\u6210\u5458 13.3 - \u8bbf\u95ee\u4fee\u9970\u7b26 public \u548c private 13.4 - \u6210\u5458\u8bbf\u95ee\u51fd\u6570\u4e0e\u5c01\u88c5 13.5 - \u6784\u9020\u51fd\u6570 13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 13.8 - \u91cd\u53e0\u548c\u59d4\u6d3e\u6784\u9020\u51fd\u6570 13.9 - \u6790\u6784\u51fd\u6570 13.10 - \u9690\u85cf\u7684this\u6307\u9488 13.11 - \u7c7b\u4ee3\u7801\u548c\u5934\u6587\u4ef6 13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570 13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf 13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570 13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b 13.16 - \u533f\u540d\u5bf9\u8c61 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b 13.18 - \u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 13.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u9762\u5411\u5bf9\u8c61\u57fa\u7840","title":"13. \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u57fa\u7840"},{"location":"MOC/#14","text":"14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26 14.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26\u91cd\u8f7d","title":"14. \u64cd\u4f5c\u7b26\u91cd\u8f7d \ud83d\udea7"},{"location":"MOC/#16","text":"16.1 - \u5bf9\u8c61\u5173\u7cfb 16.2 - \u7ec4\u5408\u5173\u7cfb 16.3 - \u805a\u5408\u5173\u7cfb 16.4 - \u5173\u8054\u5173\u7cfb 16.5 - \u4f9d\u8d56\u5173\u7cfb 16.6 - \u5bb9\u5668\u7c7b 16.7 - std::initializer_list 16.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5bf9\u8c61\u5173\u7cfb","title":"16. \u5bf9\u8c61\u5173\u7cfb\u7b80\u4ecb"},{"location":"MOC/#17","text":"17.1 - \u7ee7\u627f\u7b80\u4ecb 17.2 - C++\u7ee7\u627f\u57fa\u7840 17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f 17.4 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u548c\u521d\u59cb\u5316 17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 17.6 - \u4e3a\u6d3e\u751f\u7c7b\u6dfb\u52a0\u65b0\u529f\u80fd 17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570 17.9 - \u591a\u91cd\u7ee7\u627f 17.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7ee7\u627f","title":"17. \u7ee7\u627f"},{"location":"MOC/#18","text":"18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b 18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316 18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a 18.6 - \u865a\u51fd\u6570\u8868 18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u7c7b 18.8 - \u865a\u57fa\u7c7b 18.9 - \u5bf9\u8c61\u5207\u7247 18.10 - \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 18.11 - \u4f7f\u7528<<\u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b 18.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u865a\u51fd\u6570","title":"18. \u865a\u51fd\u6570"},{"location":"MOC/#19","text":"19.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6a21\u677f\u548c\u7c7b","title":"19. \u6a21\u677f\u548c\u7c7b \ud83d\udea7"},{"location":"MOC/#20","text":"20.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f02\u5e38 20.4","title":"20. \u5f02\u5e38 \ud83d\udea7"},{"location":"MOC/#m-move","text":"M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb M.2 - \u53f3\u503c\u5f15\u7528 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c M.4 - std::move M.5 - std::move_if_noexcept M.6 \u2014 std::unique_ptr M.7 \u2014 std::shared_ptr M.8 \u2014 \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898 M.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u79fb\u52a8\u548c\u667a\u80fd\u6307\u9488","title":"M. move \u548c\u667a\u80fd\u6307\u9488"},{"location":"MOC/#21-stl","text":"21.1 - \u6807\u51c6\u5e93 21.2 - STL\u5bb9\u5668 21.3 - STL\u8fed\u4ee3\u5668 21.4 - STL\u7b97\u6cd5","title":"21. STL"},{"location":"MOC/#22-std","text":"22.1 - std::string \u548c std::wstring 22.2 - std-string \u7684\u6784\u9020\u548c\u6790\u6784 22.3 - std-string \u7684\u957f\u5ea6\u548c\u5bb9\u91cf 22.4 - std-string \u5b57\u7b26\u8bbf\u95ee\u548cC\u5b57\u7b26\u6570\u7ec4\u8f6c\u6362 22.5 - std-string \u8d4b\u503c\u548c\u4ea4\u6362 22.6 - std-string \u8ffd\u52a0 22.7 - std-string \u63d2\u5165","title":"22. std \u5b57\u7b26\u4e32"},{"location":"MOC/#23","text":"23.1 - \u8f93\u5165\u8f93\u51fa\u6d41 23.2 - \u4f7f\u7528istream\u5904\u7406\u8f93\u5165 23.3 - \u4f7f\u7528ostream\u548cios\u5904\u7406\u8f93\u51fa 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b 23.5 - \u6d41\u72b6\u6001\u548c\u8f93\u5165\u9a8c\u8bc1 23.6 - \u57fa\u672c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa 23.7 - \u968f\u673a\u6587\u4ef6\u8f93\u5165\u8f93\u51fa","title":"23. \u8f93\u5165\u8f93\u51fa"},{"location":"MOC/#a","text":"A.1 - \u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93 A.2 - \u5728visual studio\u4e2d\u4f7f\u7528\u5e93 A.3 - \u5728Code Blocks\u4e2d\u4f7f\u7528\u5e93 A.4 \u2014 C++ FAQ","title":"A. \u5176\u4ed6\u8bdd\u9898"},{"location":"MOC/#b-c","text":"B.1 - C++11 B.2 - C++14 B.3 - C++17 B.4 - C++20","title":"B. C++\u6807\u51c6\u66f4\u65b0"},{"location":"MOC/#o1","text":"O.1 - bit\u6807\u8bb0\u548c\u901a\u8fc7std::bitset\u64cd\u4f5cbit O.2 - \u4f4d\u8fd0\u7b97\u7b26 O.3 - \u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u548c\u63a9\u7801\u64cd\u4f5c\u4f4d O.4 - \u4e8c\u8fdb\u5236\u548c\u5341\u8fdb\u5236\u8f6c\u6362","title":"O1. \u4f4d\u8fd0\u7b97 \ud83d\udea7"},{"location":"Multiple%20inheritance/","tags":["\u8bcd\u6c47\u8868","Multiple inheritance"],"text":"","title":"\u591a\u91cd\u7ee7\u627f"},{"location":"Name-mangling/","tags":["\u8bcd\u6c47\u8868","Name-mangling"],"text":"","title":"\u540d\u79f0\u4fee\u9970"},{"location":"O-1-bit-flags-and-bit-manipulation-via-std-bitset/","tags":["bit"],"text":"Key Takeaway \u5728\u73b0\u4ee3\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u4e0a\uff0c\u6700\u5c0f\u7684\u53ef\u5bfb\u5740\u5185\u5b58\u5355\u5143\u662f\u5b57\u8282\u3002\u7531\u4e8e\u6240\u6709\u5bf9\u8c61\u90fd\u9700\u8981\u5177\u6709\u552f\u4e00\u7684\u5185\u5b58\u5730\u5740\uff0c\u8fd9\u610f\u5473\u7740\u5bf9\u8c61\u7684\u5927\u5c0f\u5fc5\u987b\u81f3\u5c11\u4e3a\u4e00\u4e2a\u5b57\u8282\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u53d8\u91cf\u7c7b\u578b\uff0c\u8fd9\u5f88\u597d\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u5e03\u5c14\u503c\uff0c\u8fd9\u6709\u70b9\u6d6a\u8d39(\u53cc\u5173\u8bed\uff09 \u201c\u539f\u6587:this is a bit wasteful\u201d \u3002\u5e03\u5c14\u7c7b\u578b\u53ea\u6709\u4e24\u79cd\u72b6\u6001\uff1a\u771f \uff081\uff09 \u6216\u5047 \uff080\uff09\u3002\u8fd9\u7ec4\u72b6\u6001\u53ea\u9700\u8981\u4e00\u4e2a\u4f4d\u6765\u5b58\u50a8\u3002\u4f46\u662f\uff0c\u5982\u679c\u53d8\u91cf\u5fc5\u987b\u81f3\u5c11\u662f\u4e00\u4e2a\u5b57\u8282\uff0c\u5e76\u4e14\u4e00\u4e2a\u5b57\u8282\u662f 8 \u4f4d\uff0c\u8fd9\u610f\u5473\u7740\u5e03\u5c14\u503c\u4f7f\u7528 1 \u4f4d\uff0c\u800c\u5176\u4ed6 7 \u4f4d\u672a\u4f7f\u7528\u3002 \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4e48\u505a\u5f88\u597d \u2014\u2014 \u6211\u4eec\u901a\u5e38\u4e0d\u4f1a\u5bf9\u5185\u5b58\u7ba1\u7406\u5982\u6b64\u82db\u523b\uff0c\u4ee5\u81f3\u4e8e\u6211\u4eec\u9700\u8981\u5173\u5fc37\u4e2a\u6d6a\u8d39\u7684\u4f4d\uff08\u6211\u4eec\u66f4\u5e94\u8be5\u4f7f\u4ee3\u7801\u6613\u4e8e\u7406\u89e3\uff0c\u6613\u4e8e\u7ef4\u62a4\uff09\u3002 \u4f46\u662f\uff0c\u5728\u67d0\u4e9b\u5b58\u50a8\u5bc6\u96c6\u578b\u60c5\u51b5\u4e0b\uff0c\u51fa\u4e8e\u5b58\u50a8\u6548\u7387\u76ee\u7684\uff0c\u5c06 8 \u4e2a\u5355\u72ec\u7684\u5e03\u5c14\u503c\u201c\u6253\u5305\u201d\u5230\u5355\u4e2a\u5b57\u8282\u4e2d\u53ef\u80fd\u5f88\u6709\u7528\u3002 \u505a\u8fd9\u4e9b\u4e8b\u60c5\u9700\u8981\u6211\u4eec\u53ef\u4ee5\u5728\u4f4d\u7ea7\u522b\u64cd\u4f5c\u5bf9\u8c61\u3002\u5e78\u8fd0\u7684\u662f\uff0cC++\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u505a\u5230\u8fd9\u4e00\u70b9\u7684\u5de5\u5177\u3002\u4fee\u6539\u5bf9\u8c61\u4e2d\u7684\u5355\u4e2a\u4f4d\u79f0\u4e3a \u4f4d\u64cd\u4f5c \u3002 \u4f4d\u64cd\u4f5c\u5728\u52a0\u5bc6\u548c\u538b\u7f29\u7b97\u6cd5\u4e2d\u4e5f\u5f88\u6709\u7528\u3002 \u4f4d\u6807\u5fd7 \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u4f7f\u7528\u53d8\u91cf\u6765\u4fdd\u5b58\u5355\u4e2a\u503c\uff1a 1 2 int foo { 5 }; // assign foo the value 5 (probably uses 32 bits of storage) std :: cout << foo ; // print the value 5 \u4f46\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u4eec\u89c6\u4e3a\u5355\u4e2a\u4f4d\u7684\u96c6\u5408\uff0c\u800c\u4e0d\u662f\u5c06\u5bf9\u8c61\u89c6\u4e3a\u5305\u542b\u5355\u4e2a\u503c\u7684\u5bf9\u8c61\u3002\u5f53\u5bf9\u8c61\u7684\u5355\u4e2a\u4f4d\u7528\u4f5c\u5e03\u5c14\u503c\u65f6\uff0c\u8fd9\u4e9b\u4f4d\u79f0\u4e3a \u4f4d\u6807\u5fd7 \u3002 \u8981\u5b9a\u4e49\u4e00\u7ec4\u4f4d\u6807\u5fd7\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528 \u9002\u5f53\u5927\u5c0f\u7684\u65e0\u7b26\u53f7\u6574\u6570 \uff088 \u4f4d\u300116 \u4f4d\u300132 \u4f4d\u7b49......\u53d6\u51b3\u4e8e\u6211\u4eec\u6709\u591a\u5c11\u6807\u5fd7\uff09\uff0c\u6216 std::bitset \u3002 1 2 3 #include // for std::bitset std :: bitset < 8 > mybitset {}; // 8 bits in size means room for 8 flags \u6700\u4f73\u5b9e\u8df5 \u4f4d\u64cd\u4f5c\u662f\u5c11\u6570\u5e94\u8be5\u660e\u786e\u4f7f\u7528\u65e0\u7b26\u53f7\u6574\u6570\uff08\u6216 std\uff1a\uff1abitset\uff09\u7684\u60c5\u51b5\u4e4b\u4e00\u3002 \u5728\u672c\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u5c55\u793a\u5982\u4f55\u901a\u8fc7 std::bitset \u4ee5\u7b80\u5355\u7684\u65b9\u5f0f\u8fdb\u884c\u4f4d\u64cd\u4f5c\u3002\u5728\u4e0b\u4e00\u7ec4\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u4ee5\u66f4\u56f0\u96be\u4f46\u7528\u9014\u5e7f\u6cdb\u7684\u65b9\u5f0f\u505a\u5230\u8fd9\u4e00\u70b9\u3002 \u4f4d\u7f16\u53f7\u548c\u4f4d\u4f4d\u7f6e \u7ed9\u5b9a\u4e00\u4e2a\u4f4d\u5e8f\u5217\uff0c\u6211\u4eec\u901a\u5e38\u4ece\u53f3\u5230\u5de6\u5bf9\u4f4d\u8fdb\u884c\u7f16\u53f7\uff0c \u4ece 0\uff08\u800c\u4e0d\u662f 1\uff09\u5f00\u59cb \u3002\u6bcf\u4e2a\u6570\u5b57\u8868\u793a\u4e00\u4e2a \u4f4d\u4f4d\u7f6e \u3002 1 2 76543210 Bit position 00000101 Bit sequence \u7ed9\u5b9a\u4f4d\u5e8f\u5217 0000 0101\uff0c\u4f4d\u4e8e\u4f4d\u7f6e 0 \u548c 2 \u7684\u4f4d\u7684\u503c\u4e3a 1\uff0c\u5176\u4ed6\u4f4d\u7684\u503c\u4e3a 0\u3002 \u901a\u8fc7 std::bitset \u64cd\u4f5c\u4f4d \u5728 \u7b2c 4.16 \u8bfe\u2014\u2014\u6570\u5b57\u7cfb\u7edf\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236 \u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 std::bitset \u6253\u5370\u4e8c\u8fdb\u5236\u503c\u3002\u7136\u800c\uff0c\u8fd9\u5e76\u4e0d\u662f std::bitset \u80fd\u505a\u7684\u552f\u4e00\u6709\u7528\u7684\u4e8b\u60c5\u3002 std::bitset \u63d0\u4f9b\u4e86 4 \u4e2a\u53ef\u7528\u4e8e\u4f4d\u64cd\u4f5c\u7684\u5173\u952e\u51fd\u6570\uff1a test() \u5141\u8bb8\u6211\u4eec\u67e5\u8be2\u67d0\u4e2a\u4f4d\u662f 0 \u8fd8\u662f 1 set() \u5141\u8bb8\u6211\u4eec\u6253\u5f00\u4e00\u4e2a\u4f4d\uff08\u5982\u679c\u4f4d\u5df2\u7ecf\u6253\u5f00\uff0c\u8fd9\u5c06\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\uff09 reset() \u5141\u8bb8\u6211\u4eec\u5173\u95ed\u4e00\u4e2a\u4f4d\uff08\u5982\u679c\u8be5\u4f4d\u5df2\u7ecf\u5173\u95ed\uff0c\u8fd9\u5c06\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\uff09 flip() \u5141\u8bb8\u6211\u4eec\u5c06\u4f4d\u503c\u4ece 0 \u7ffb\u8f6c\u4e3a 1\uff0c\u53cd\u4e4b\u4ea6\u7136 \u8fd9\u4e9b\u51fd\u6570\u4e2d\u7684\u6bcf\u4e00\u4e2a\u90fd\u5c06\u6211\u4eec\u8981\u64cd\u4f5c\u7684\u4f4d\u7684\u4f4d\u7f6e\u4f5c\u4e3a\u5b83\u4eec\u552f\u4e00\u7684\u53c2\u6570\u3002 \u8fd9\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include int main () { std :: bitset < 8 > bits { 0b0000'0101 }; // we need 8 bits, start with bit pattern 0000 0101 bits . set ( 3 ); // set bit position 3 to 1 (now we have 0000 1101) bits . flip ( 4 ); // flip bit 4 (now we have 0001 1101) bits . reset ( 4 ); // set bit 4 back to 0 (now we have 0000 1101) std :: cout << \"All the bits: \" << bits << '\\n' ; std :: cout << \"Bit 3 has value: \" << bits . test ( 3 ) << '\\n' ; std :: cout << \"Bit 4 has value: \" << bits . test ( 4 ) << '\\n' ; return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 3 All the bits: 00001101 Bit 3 has value: 1 Bit 4 has value: 0 \u5c0f\u8d34\u58eb \u5982\u679c\u60a8\u9700\u8981\u590d\u4e60\u4ec0\u4e48\u662f 0b \u524d\u7f00\u6216 ' \u5206\u9694\u7b26\uff0c\u8bf7\u67e5\u770b 4.16 - \u6570\u5b57\u7cfb\u7edf\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236 \u5982\u679c\u6211\u4eec\u60f3\u4e00\u6b21\u83b7\u53d6\u6216\u8bbe\u7f6e\u591a\u4e2a\u4f4d\u600e\u4e48\u529e std::bitset \u5e76\u4e0d\u5bb9\u6613\u5b9e\u73b0\u8fd9\u4e2a\u9700\u6c42\u3002\u4e3a\u4e86\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u6216\u8005\u5982\u679c\u6211\u4eec\u60f3\u4f7f\u7528\u65e0\u7b26\u53f7\u6574\u6570\u4f4d\u6807\u5fd7\u800c\u4e0d\u662f std::bitset\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u66f4\u4f20\u7edf\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u51e0\u8282\u8bfe\u4e2d\u4ecb\u7ecd\u8fd9\u4e9b\u5185\u5bb9\u3002 std::bitset \u7684\u5927\u5c0f \u4e00\u4e2a\u6f5c\u5728\u7684\u60ca\u559c\u662f std::bitset \u9488\u5bf9\u901f\u5ea6\u8fdb\u884c\u4e86\u4f18\u5316 \uff0c\u800c\u4e0d\u662f\u8282\u7701\u5185\u5b58\u3002std::bitset \u7684\u5927\u5c0f\u901a\u5e38\u662f\u4fdd\u5b58\u4f4d\u6240\u9700\u7684\u5b57\u8282\u6570\uff0c \u56db\u820d\u4e94\u5165\u5230(rounded up to) \u6700\u63a5\u8fd1\u7684 sizeof(size_t) \uff0c\u5728 32 \u4f4d\u673a\u5668\u4e0a\u4e3a 4 \u5b57\u8282\uff0c\u5728 64 \u4f4d\u673a\u5668\u4e0a\u4e3a 8 \u5b57\u8282\u3002 \u56e0\u6b64\uff0c std::bitset<8> \u901a\u5e38\u4f7f\u7528 4 \u6216 8 \u5b57\u8282\u7684\u5185\u5b58\uff0c\u5373\u4f7f\u5b83\u5728\u6280\u672f\u4e0a\u53ea\u9700\u8981 1 \u4e2a\u5b57\u8282\u6765\u5b58\u50a8 8 \u4f4d\u3002\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u60f3\u8981\u65b9\u4fbf\u800c\u4e0d\u662f\u8282\u7701\u5185\u5b58\u65f6\uff0cstd::bitset \u662f\u6700\u6709\u7528\u7684\u3002","title":"O.1 - bit\u6807\u8bb0\u548c\u901a\u8fc7std::bitset\u64cd\u4f5cbit"},{"location":"O-1-bit-flags-and-bit-manipulation-via-std-bitset/#_1","text":"\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u4f7f\u7528\u53d8\u91cf\u6765\u4fdd\u5b58\u5355\u4e2a\u503c\uff1a 1 2 int foo { 5 }; // assign foo the value 5 (probably uses 32 bits of storage) std :: cout << foo ; // print the value 5 \u4f46\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u4eec\u89c6\u4e3a\u5355\u4e2a\u4f4d\u7684\u96c6\u5408\uff0c\u800c\u4e0d\u662f\u5c06\u5bf9\u8c61\u89c6\u4e3a\u5305\u542b\u5355\u4e2a\u503c\u7684\u5bf9\u8c61\u3002\u5f53\u5bf9\u8c61\u7684\u5355\u4e2a\u4f4d\u7528\u4f5c\u5e03\u5c14\u503c\u65f6\uff0c\u8fd9\u4e9b\u4f4d\u79f0\u4e3a \u4f4d\u6807\u5fd7 \u3002 \u8981\u5b9a\u4e49\u4e00\u7ec4\u4f4d\u6807\u5fd7\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528 \u9002\u5f53\u5927\u5c0f\u7684\u65e0\u7b26\u53f7\u6574\u6570 \uff088 \u4f4d\u300116 \u4f4d\u300132 \u4f4d\u7b49......\u53d6\u51b3\u4e8e\u6211\u4eec\u6709\u591a\u5c11\u6807\u5fd7\uff09\uff0c\u6216 std::bitset \u3002 1 2 3 #include // for std::bitset std :: bitset < 8 > mybitset {}; // 8 bits in size means room for 8 flags \u6700\u4f73\u5b9e\u8df5 \u4f4d\u64cd\u4f5c\u662f\u5c11\u6570\u5e94\u8be5\u660e\u786e\u4f7f\u7528\u65e0\u7b26\u53f7\u6574\u6570\uff08\u6216 std\uff1a\uff1abitset\uff09\u7684\u60c5\u51b5\u4e4b\u4e00\u3002 \u5728\u672c\u8bfe\u4e2d\uff0c\u6211\u4eec\u5c06\u5c55\u793a\u5982\u4f55\u901a\u8fc7 std::bitset \u4ee5\u7b80\u5355\u7684\u65b9\u5f0f\u8fdb\u884c\u4f4d\u64cd\u4f5c\u3002\u5728\u4e0b\u4e00\u7ec4\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u4ee5\u66f4\u56f0\u96be\u4f46\u7528\u9014\u5e7f\u6cdb\u7684\u65b9\u5f0f\u505a\u5230\u8fd9\u4e00\u70b9\u3002","title":"\u4f4d\u6807\u5fd7"},{"location":"O-1-bit-flags-and-bit-manipulation-via-std-bitset/#_2","text":"\u7ed9\u5b9a\u4e00\u4e2a\u4f4d\u5e8f\u5217\uff0c\u6211\u4eec\u901a\u5e38\u4ece\u53f3\u5230\u5de6\u5bf9\u4f4d\u8fdb\u884c\u7f16\u53f7\uff0c \u4ece 0\uff08\u800c\u4e0d\u662f 1\uff09\u5f00\u59cb \u3002\u6bcf\u4e2a\u6570\u5b57\u8868\u793a\u4e00\u4e2a \u4f4d\u4f4d\u7f6e \u3002 1 2 76543210 Bit position 00000101 Bit sequence \u7ed9\u5b9a\u4f4d\u5e8f\u5217 0000 0101\uff0c\u4f4d\u4e8e\u4f4d\u7f6e 0 \u548c 2 \u7684\u4f4d\u7684\u503c\u4e3a 1\uff0c\u5176\u4ed6\u4f4d\u7684\u503c\u4e3a 0\u3002","title":"\u4f4d\u7f16\u53f7\u548c\u4f4d\u4f4d\u7f6e"},{"location":"O-1-bit-flags-and-bit-manipulation-via-std-bitset/#stdbitset","text":"\u5728 \u7b2c 4.16 \u8bfe\u2014\u2014\u6570\u5b57\u7cfb\u7edf\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236 \u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 std::bitset \u6253\u5370\u4e8c\u8fdb\u5236\u503c\u3002\u7136\u800c\uff0c\u8fd9\u5e76\u4e0d\u662f std::bitset \u80fd\u505a\u7684\u552f\u4e00\u6709\u7528\u7684\u4e8b\u60c5\u3002 std::bitset \u63d0\u4f9b\u4e86 4 \u4e2a\u53ef\u7528\u4e8e\u4f4d\u64cd\u4f5c\u7684\u5173\u952e\u51fd\u6570\uff1a test() \u5141\u8bb8\u6211\u4eec\u67e5\u8be2\u67d0\u4e2a\u4f4d\u662f 0 \u8fd8\u662f 1 set() \u5141\u8bb8\u6211\u4eec\u6253\u5f00\u4e00\u4e2a\u4f4d\uff08\u5982\u679c\u4f4d\u5df2\u7ecf\u6253\u5f00\uff0c\u8fd9\u5c06\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\uff09 reset() \u5141\u8bb8\u6211\u4eec\u5173\u95ed\u4e00\u4e2a\u4f4d\uff08\u5982\u679c\u8be5\u4f4d\u5df2\u7ecf\u5173\u95ed\uff0c\u8fd9\u5c06\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\uff09 flip() \u5141\u8bb8\u6211\u4eec\u5c06\u4f4d\u503c\u4ece 0 \u7ffb\u8f6c\u4e3a 1\uff0c\u53cd\u4e4b\u4ea6\u7136 \u8fd9\u4e9b\u51fd\u6570\u4e2d\u7684\u6bcf\u4e00\u4e2a\u90fd\u5c06\u6211\u4eec\u8981\u64cd\u4f5c\u7684\u4f4d\u7684\u4f4d\u7f6e\u4f5c\u4e3a\u5b83\u4eec\u552f\u4e00\u7684\u53c2\u6570\u3002 \u8fd9\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include int main () { std :: bitset < 8 > bits { 0b0000'0101 }; // we need 8 bits, start with bit pattern 0000 0101 bits . set ( 3 ); // set bit position 3 to 1 (now we have 0000 1101) bits . flip ( 4 ); // flip bit 4 (now we have 0001 1101) bits . reset ( 4 ); // set bit 4 back to 0 (now we have 0000 1101) std :: cout << \"All the bits: \" << bits << '\\n' ; std :: cout << \"Bit 3 has value: \" << bits . test ( 3 ) << '\\n' ; std :: cout << \"Bit 4 has value: \" << bits . test ( 4 ) << '\\n' ; return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 3 All the bits: 00001101 Bit 3 has value: 1 Bit 4 has value: 0 \u5c0f\u8d34\u58eb \u5982\u679c\u60a8\u9700\u8981\u590d\u4e60\u4ec0\u4e48\u662f 0b \u524d\u7f00\u6216 ' \u5206\u9694\u7b26\uff0c\u8bf7\u67e5\u770b 4.16 - \u6570\u5b57\u7cfb\u7edf\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236","title":"\u901a\u8fc7 std::bitset \u64cd\u4f5c\u4f4d"},{"location":"O-1-bit-flags-and-bit-manipulation-via-std-bitset/#_3","text":"std::bitset \u5e76\u4e0d\u5bb9\u6613\u5b9e\u73b0\u8fd9\u4e2a\u9700\u6c42\u3002\u4e3a\u4e86\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u6216\u8005\u5982\u679c\u6211\u4eec\u60f3\u4f7f\u7528\u65e0\u7b26\u53f7\u6574\u6570\u4f4d\u6807\u5fd7\u800c\u4e0d\u662f std::bitset\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u66f4\u4f20\u7edf\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u51e0\u8282\u8bfe\u4e2d\u4ecb\u7ecd\u8fd9\u4e9b\u5185\u5bb9\u3002","title":"\u5982\u679c\u6211\u4eec\u60f3\u4e00\u6b21\u83b7\u53d6\u6216\u8bbe\u7f6e\u591a\u4e2a\u4f4d\u600e\u4e48\u529e"},{"location":"O-1-bit-flags-and-bit-manipulation-via-std-bitset/#stdbitset_1","text":"\u4e00\u4e2a\u6f5c\u5728\u7684\u60ca\u559c\u662f std::bitset \u9488\u5bf9\u901f\u5ea6\u8fdb\u884c\u4e86\u4f18\u5316 \uff0c\u800c\u4e0d\u662f\u8282\u7701\u5185\u5b58\u3002std::bitset \u7684\u5927\u5c0f\u901a\u5e38\u662f\u4fdd\u5b58\u4f4d\u6240\u9700\u7684\u5b57\u8282\u6570\uff0c \u56db\u820d\u4e94\u5165\u5230(rounded up to) \u6700\u63a5\u8fd1\u7684 sizeof(size_t) \uff0c\u5728 32 \u4f4d\u673a\u5668\u4e0a\u4e3a 4 \u5b57\u8282\uff0c\u5728 64 \u4f4d\u673a\u5668\u4e0a\u4e3a 8 \u5b57\u8282\u3002 \u56e0\u6b64\uff0c std::bitset<8> \u901a\u5e38\u4f7f\u7528 4 \u6216 8 \u5b57\u8282\u7684\u5185\u5b58\uff0c\u5373\u4f7f\u5b83\u5728\u6280\u672f\u4e0a\u53ea\u9700\u8981 1 \u4e2a\u5b57\u8282\u6765\u5b58\u50a8 8 \u4f4d\u3002\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u60f3\u8981\u65b9\u4fbf\u800c\u4e0d\u662f\u8282\u7701\u5185\u5b58\u65f6\uff0cstd::bitset \u662f\u6700\u6709\u7528\u7684\u3002","title":"std::bitset \u7684\u5927\u5c0f"},{"location":"O-2-bitwise-operators/","tags":["bit"],"text":"Key Takeaway \u6309\u4f4d\u8fd0\u7b97\u7b26 C++ \u63d0\u4f9b 6 \u4f4d\u64cd\u4f5c\u8fd0\u7b97\u7b26\uff0c\u901a\u5e38\u79f0\u4e3a \u6309\u4f4d\u8fd0\u7b97 \u7b26\uff1a Operator Symbol Form Operation \u5de6\u79fb << x << y x \u4e2d\u7684\u6240\u6709\u4f4d\u5de6\u79fb y \u4f4d \u53f3\u79fb >> x >> y x \u4e2d\u7684\u6240\u6709\u4f4d\u53f3\u79fb y \u4f4d \u6309\u4f4d\u975e ~ ~x x \u4e2d\u7684\u6240\u6709\u4f4d\u7ffb\u8f6c \u6309\u4f4d\u4e0e & x & y x \u4e2d\u7684\u6bcf\u4e00\u4f4d AND y \u4e2d\u7684\u6bcf\u4e00\u4f4d \u6309\u4f4d\u6216 | \u00d7 | y x \u4e2d\u7684\u6bcf\u4e00\u4f4d OR y \u4e2d\u7684\u6bcf\u4e00\u4f4d \u6309\u4f4d\u5f02\u6216 ^ x^y x \u4e2d\u7684\u6bcf\u4e00\u4f4d XOR y \u4e2d\u7684\u6bcf\u4e00\u4f4d \u4f5c\u8005\u6ce8 \u5728\u4ee5\u4e0b\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4e3b\u8981\u4f7f\u7528 4 \u4f4d\u4e8c\u8fdb\u5236\u503c\u3002\u8fd9\u662f\u4e3a\u4e86\u65b9\u4fbf\u548c\u4fdd\u6301\u793a\u4f8b\u7b80\u5355\u3002\u5728\u5b9e\u9645\u7a0b\u5e8f\u4e2d\uff0c\u4f7f\u7528\u7684\u4f4d\u6570\u53d6\u51b3\u4e8e\u5bf9\u8c61\u7684\u5927\u5c0f\uff08\u4f8b\u5982\uff0c\u4e00\u4e2a 2 \u5b57\u8282\u7684\u5bf9\u8c61\u5c06\u5b58\u50a8 16 \u4f4d\uff09\u3002 \u4e3a\u4e86\u53ef\u8bfb\u6027\uff0c\u6211\u4eec\u8fd8\u5c06\u7701\u7565\u4ee3\u7801\u793a\u4f8b\u4e4b\u5916\u7684 0b \u524d\u7f00\uff08\u4f8b\u5982\uff0c\u6211\u4eec\u5c06\u53ea\u4f7f\u7528 0101 \u800c\u4e0d\u662f 0b0101\uff09\u3002 \u6309\u4f4d\u8fd0\u7b97\u7b26\u662f\u4e3a\u6574\u6570\u7c7b\u578b\u548c std::bitset \u5b9a\u4e49\u7684\u3002\u6211\u4eec\u5c06\u5728\u793a\u4f8b\u4e2d\u4f7f\u7528 std::bitset\uff0c\u56e0\u4e3a\u5b83\u66f4\u5bb9\u6613\u4ee5\u4e8c\u8fdb\u5236\u5f62\u5f0f\u6253\u5370\u8f93\u51fa\u3002 \u907f\u514d\u4f7f\u7528\u5e26\u7b26\u53f7\u64cd\u4f5c\u6570\u7684\u6309\u4f4d\u8fd0\u7b97\u7b26 \uff0c\u56e0\u4e3a\u5728 C++20 \u4e4b\u524d\uff0c\u8bb8\u591a\u8fd0\u7b97\u7b26\u5c06\u8fd4\u56de\u5b9e\u73b0\u5b9a\u4e49\u7684\u7ed3\u679c\uff0c\u6216\u6709\u7740\u4f7f\u7528\u65e0\u7b26\u53f7\u64cd\u4f5c\u6570\uff08\u6216 std::bitset\uff09\u53ef\u4ee5\u8f7b\u677e\u907f\u514d\u7684\u5176\u4ed6\u6f5c\u5728\u95ee\u9898\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e3a\u907f\u514d\u610f\u5916\uff0c\u8bf7\u4f7f\u7528\u5e26\u65e0\u7b26\u53f7\u64cd\u4f5c\u6570\u7684\u6309\u4f4d\u8fd0\u7b97\u7b26\u6216 std::bitset\u3002 \u6309\u4f4d\u5de6\u79fb (<<) \u548c\u6309\u4f4d\u53f3\u79fb (>>) \u8fd0\u7b97\u7b26 \u6309\u4f4d\u5de6\u79fb (<<) \u8fd0\u7b97\u7b26\u5c06\u4f4d\u5411\u5de6\u79fb\u52a8\u3002\u5de6\u64cd\u4f5c\u6570\u662f\u8981\u79fb\u4f4d\u7684\u4f4d\u7684\u8868\u8fbe\u5f0f\uff0c\u53f3\u64cd\u4f5c\u6570\u662f\u8981\u5de6\u79fb\u7684\u6574\u6570\u4f4d\u6570\u3002 \u6240\u4ee5\u5f53\u6211\u4eec\u8bf4 \u65f6 x << 1 \uff0c\u6211\u4eec\u662f\u5728\u8bf4\u201c\u5c06\u53d8\u91cf x \u4e2d\u7684\u4f4d\u5de6\u79fb\u4e00\u4f4d\u201d\u3002\u4ece\u53f3\u4fa7\u79fb\u5165\u7684\u65b0\u4f4d\u63a5\u6536\u503c 0\u3002 0011 << 1 \u662f 0110 0011 << 2 \u662f 1100 0011 << 3 \u662f 1000 \u8bf7\u6ce8\u610f\uff0c\u5728\u7b2c\u4e09\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5c06\u4e00\u4e2a1\u4ece\u5de6\u7aef\u504f\u79fb\u51fa\u4e86\uff01\u4ece\u4e8c\u8fdb\u5236\u6570\u672b\u5c3e\u79fb\u51fa\u7684\u4f4d\u5c06\u6c38\u8fdc\u4e22\u5931\u3002 \u6309\u4f4d\u53f3\u79fb (>>) \u8fd0\u7b97\u7b26\u5c06\u4f4d\u53f3\u79fb\u3002 1100 >> 1 \u662f 0110 1100 >> 2 \u662f 0011 1100 >> 3 \u662f 0001 \u8bf7\u6ce8\u610f\uff0c\u5728\u7b2c\u4e09\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5c06\u4e00\u4e2a1\u4ece\u53f3\u7aef\u504f\u79fb\u51fa\u4e86\uff0c\u56e0\u6b64\u5b83\u4e22\u5931\u4e86\u3002 \u8fd9\u662f\u8fdb\u884c\u4e00\u4e9b\u4f4d\u79fb\u7684\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include #include int main () { std :: bitset < 4 > x { 0b1100 }; std :: cout << x << '\\n' ; std :: cout << ( x >> 1 ) << '\\n' ; // shift right by 1, yielding 0110 std :: cout << ( x << 1 ) << '\\n' ; // shift left by 1, yielding 1000 return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 3 1100 0110 1000 \u4ec0\u4e48\uff01\uff1foperator<< \u548c operator>> \u4e0d\u662f\u7528\u4e8e\u8f93\u5165\u548c\u8f93\u51fa\u5417\uff1f \u4ed6\u4eec\u80af\u5b9a\u662f\u3002 \u4eca\u5929\u7684\u7a0b\u5e8f\u901a\u5e38\u4e0d\u4f1a\u5927\u91cf\u4f7f\u7528\u6309\u4f4d\u5de6\u79fb\u548c\u53f3\u79fb\u8fd0\u7b97\u7b26\u6765\u79fb\u4f4d\u3002\u76f8\u53cd\uff0c\u60a8\u503e\u5411\u4e8e\u770b\u5230\u4e0e std::cout\uff08\u6216\u5176\u4ed6\u6d41\u5bf9\u8c61\uff09\u4e00\u8d77\u4f7f\u7528\u7684\u6309\u4f4d\u5de6\u79fb\u8fd0\u7b97\u7b26\u6765\u8f93\u51fa\u6587\u672c\u3002\u8003\u8651\u4ee5\u4e0b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include #include int main () { unsigned int x { 0b0100 }; x = x << 1 ; // use operator<< for left shift std :: cout << std :: bitset < 4 > { x } << '\\n' ; // use operator<< for output return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u6253\u5370\uff1a 1 1000 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0coperator<< \u662f\u5982\u4f55\u77e5\u9053\u5728\u4e00\u79cd\u60c5\u51b5\u4e0b\u79fb\u4f4d\u800c\u5728\u53e6\u4e00\u79cd\u60c5\u51b5\u4e0b\u8f93\u51fa x \u7684\u5462\uff1f\u7b54\u6848\u662f std::cout \u5df2\u7ecf \u91cd\u8f7d \uff08\u63d0\u4f9b\u4e86\u66ff\u4ee3\u5b9a\u4e49\uff09 operator<< \u6267\u884c\u63a7\u5236\u53f0\u8f93\u51fa\u800c\u4e0d\u662f\u4f4d\u79fb\u3002 \u5f53\u7f16\u8bd1\u5668\u770b\u5230 operator<< \u7684\u5de6\u64cd\u4f5c\u6570\u662f std::cout \u65f6\uff0c\u5b83\u77e5\u9053\u5b83\u5e94\u8be5\u8c03\u7528 std::cout \u91cd\u8f7d\u7684 operator<< \u7248\u672c\u6765\u8fdb\u884c\u8f93\u51fa\u3002\u5982\u679c\u5de6\u64cd\u4f5c\u6570\u662f\u6574\u6570\u7c7b\u578b\uff0c\u5219 operator<< \u77e5\u9053\u5b83\u5e94\u8be5\u6267\u884c\u5176\u901a\u5e38\u7684\u4f4d\u79fb\u884c\u4e3a\u3002 \u8fd9\u540c\u6837\u9002\u7528\u4e8e\u8fd0\u7b97\u7b26>>\u3002 \u8bf7\u6ce8\u610f\uff0c\u5982\u679c\u60a8\u5bf9\u8f93\u51fa\u548c\u5de6\u79fb\u90fd\u4f7f\u7528\u8fd0\u7b97\u7b26 <<\uff0c\u5219\u9700\u8981\u62ec\u53f7\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include #include int main () { std :: bitset < 4 > x { 0b0110 }; std :: cout << x << 1 << '\\n' ; // print value of x (0110), then 1 std :: cout << ( x << 1 ) << '\\n' ; // print x left shifted by 1 (1100) return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 01101 1100 \u7b2c\u4e00\u884c\u6253\u5370 x \u7684\u503c (0110)\uff0c\u7136\u540e\u662f\u6587\u5b57 1\u3002\u7b2c\u4e8c\u884c\u6253\u5370 x \u5de6\u79fb 1 (1100) \u7684\u503c\u3002 \u6211\u4eec\u5c06\u5728\u4ee5\u540e\u7684\u90e8\u5206\u4e2d\u66f4\u591a\u5730\u8ba8\u8bba\u8fd0\u7b97\u7b26\u91cd\u8f7d\uff0c\u5305\u62ec\u8ba8\u8bba\u5982\u4f55\u4e3a\u60a8\u81ea\u5df1\u7684\u76ee\u7684\u91cd\u8f7d\u8fd0\u7b97\u7b26\u3002 \u6309\u4f4d\u975e \u6309\u4f4d\u975e \u8fd0\u7b97\u7b26 (~) \u53ef\u80fd\u662f\u6240\u6709\u6309\u4f4d\u8fd0\u7b97\u7b26\u4e2d\u6700\u5bb9\u6613\u7406\u89e3\u7684\u3002\u5b83\u53ea\u662f\u5c06\u6bcf\u4e2a\u4f4d\u4ece 0 \u7ffb\u8f6c\u4e3a 1\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u8bf7\u6ce8\u610f\uff0c \u6309\u4f4d NOT \u7684\u7ed3\u679c\u53d6\u51b3\u4e8e\u60a8\u7684\u6570\u636e\u7c7b\u578b\u7684\u5927\u5c0f\u3002 \u7ffb\u8f6c 4 \u4f4d\uff1a ~0100 \u662f 1011 \u7ffb\u8f6c 8 \u4f4d\uff1a ~0000 0100 \u662f 1111 1011 \u5728 4 \u4f4d\u548c 8 \u4f4d\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4ece\u76f8\u540c\u7684\u6570\u5b57\u5f00\u59cb\uff08\u4e8c\u8fdb\u5236 0100 \u4e0e 0000 0100 \u76f8\u540c\uff0c\u5341\u8fdb\u5236 7 \u4e0e 07 \u76f8\u540c\uff09\uff0c\u4f46\u6700\u7ec8\u5f97\u5230\u4e0d\u540c\u7684\u7ed3\u679c\u3002 \u6211\u4eec\u53ef\u4ee5\u5728\u4ee5\u4e0b\u7a0b\u5e8f\u4e2d\u770b\u5230\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 7 8 9 #include #include int main () { std :: cout << ~ std :: bitset < 4 > { 0b0100 } << ' ' << ~ std :: bitset < 8 > { 0b0100 } << '\\n' ; return 0 ; } ```\u8fd9\u6253\u5370\uff1a \u8fd9\u6253\u5370\uff1a 1011 11111011 1 2 3 4 5 ## \u6309\u4f4d\u6216 **\u6309\u4f4d\u6216**(|) \u7684\u5de5\u4f5c\u65b9\u5f0f\u4e0e\u5176\u5bf9\u5e94\u7684*\u903b\u8f91\u6216*\u975e\u5e38\u76f8\u4f3c\u3002\u4f46\u662f\uff0c\u4e0d\u662f\u5c06*OR*\u5e94\u7528\u4e8e\u64cd\u4f5c\u6570\u4ee5\u4ea7\u751f\u5355\u4e2a\u7ed3\u679c\uff0c\u800c\u662f*\u5c06\u6309\u4f4d OR*\u5e94\u7528\u4e8e\u6bcf\u4e00\u4f4d\uff01\u4f8b\u5982\uff0c\u8003\u8651\u8868\u8fbe\u5f0f`0b0101 | 0b0110`\u3002 \u8981\u8fdb\u884c\uff08\u4efb\u4f55\uff09\u6309\u4f4d\u8fd0\u7b97\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u50cf\u8fd9\u6837\u5c06\u4e24\u4e2a\u64cd\u4f5c\u6570\u6392\u5217\u8d77\u6765\uff1a 0 1 0 1 OR 0 1 1 0 1 2 3 \u7136\u540e\u5c06\u64cd\u4f5c\u5e94\u7528\u4e8e\u6bcf\u4e00*\u5217\u4f4d*\u3002 \u5982\u679c\u60a8\u8fd8\u8bb0\u5f97\u7684\u8bdd\uff0c\u5982\u679c\u5de6\u64cd\u4f5c\u6570\u3001\u53f3\u64cd\u4f5c\u6570\u6216\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u4e3a*\u771f (1) \uff0c\u5219**\u903b\u8f91 OR*\u7684\u8ba1\u7b97\u7ed3\u679c\u4e3a*\u771f (1* ) \uff0c\u5426\u5219*\u4e3a 0*\u3002\u5982\u679c\u5de6\u4fa7\u4f4d\u3001\u53f3\u4fa7\u4f4d\u6216\u4e24\u4e2a\u4f4d\u90fd\u4e3a*1*\uff0c*\u5219\u6309\u4f4d\u6216*\u8ba1\u7b97\u7ed3\u679c\u4e3a*1 \uff0c\u5426\u5219\u4e3a**0*\u3002\u56e0\u6b64\uff0c\u8868\u8fbe\u5f0f\u7684\u8ba1\u7b97\u7ed3\u679c\u5982\u4e0b\uff1a 0 1 0 1 OR 0 1 1 0 0 1 1 1 1 2 3 4 5 6 7 8 9 10 11 12 \u6211\u4eec\u7684\u7ed3\u679c\u662f 0111 \u4e8c\u8fdb\u5236\u3002 ```cpp #include #include int main() { std::cout << (std::bitset<4>{ 0b0101 } | std::bitset<4>{ 0b0110 }) << '\\n'; return 0; } \u8fd9\u6253\u5370\uff1a 1 0111 \u6211\u4eec\u53ef\u4ee5\u5bf9\u590d\u5408 OR \u8868\u8fbe\u5f0f\u505a\u540c\u6837\u7684\u4e8b\u60c5\uff0c\u4f8b\u5982 0b0111 | 0b0011 | 0b0001 . \u5982\u679c\u4e00\u5217\u4e2d\u7684\u4efb\u4f55\u4f4d\u4e3a 1 \uff0c\u5219\u8be5\u5217\u7684\u7ed3\u679c\u4e3a 1 \u3002 1 2 3 4 5 0 1 1 1 OR 0 0 1 1 OR 0 0 0 1 -------- 0 1 1 1 \u8fd9\u662f\u4e0a\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 #include #include int main () { std :: cout << ( std :: bitset < 4 > { 0b0111 } | std :: bitset < 4 > { 0b0011 } | std :: bitset < 4 > { 0b0001 }) << '\\n' ; return 0 ; } \u8fd9\u6253\u5370\uff1a 1 0111 \u6309\u4f4d\u4e0e \u6309\u4f4d\u4e0e (&) \u7684\u5de5\u4f5c\u65b9\u5f0f\u4e0e\u4e0a\u8ff0\u7c7b\u4f3c\u3002\u5982\u679c\u5de6\u53f3\u64cd\u4f5c\u6570\u7684\u8ba1\u7b97\u7ed3\u679c\u90fd\u4e3a true \uff0c\u5219 \u903b\u8f91 AND\u7684\u8ba1\u7b97\u7ed3\u679c\u4e3a true \u3002\u5982\u679c\u5217\u4e2d\u7684\u4e24\u4f4d\u90fd\u4e3a 1 \uff0c\u5219\u6309\u4f4d\u4e0e \u8ba1\u7b97\u7ed3\u679c\u4e3a \u771f (1) \u3002\u8003\u8651\u8868\u8fbe\u5f0f\u3002\u6392\u5217\u6bcf\u4e2a\u4f4d\u5e76\u5bf9\u6bcf\u4e00\u5217\u4f4d\u5e94\u7528 AND \u8fd0\u7b97\uff1a 0b0101 & 0b0110 1 2 3 4 0 1 0 1 AND 0 1 1 0 -------- 0 1 0 0 1 2 3 4 5 6 7 8 #include #include int main () { std :: cout << ( std :: bitset < 4 > { 0b0101 } & std :: bitset < 4 > { 0b0110 }) << '\\n' ; return 0 ; } \u8fd9\u6253\u5370\uff1a 1 0100 \u540c\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u590d\u5408 AND \u8868\u8fbe\u5f0f\u505a\u540c\u6837\u7684\u4e8b\u60c5\uff0c\u4f8b\u5982 0b0001 & 0b0011 & 0b0111 . \u5982\u679c\u4e00\u5217\u4e2d\u7684\u6240\u6709\u4f4d\u5747\u4e3a 1\uff0c\u5219\u8be5\u5217\u7684\u7ed3\u679c\u4e3a 1\u3002 1 2 3 4 5 0 0 0 1 AND 0 0 1 1 AND 0 1 1 1 -------- 0 0 0 1 1 2 3 4 5 6 7 8 9 #include #include int main () { std :: cout << ( std :: bitset < 4 > { 0b0001 } & std :: bitset < 4 > { 0b0011 } & std :: bitset < 4 > { 0b0111 }) << '\\n' ; return 0 ; } \u8fd9\u6253\u5370\uff1a 1 0001 \u6309\u4f4d\u5f02\u6216 \u6700\u540e\u4e00\u4e2a\u8fd0\u7b97\u7b26\u662f \u6309\u4f4d\u5f02\u6216 (^)\uff0c\u4e5f\u79f0\u4e3a \u5f02\u6216 \u3002 \u8bc4\u4f30\u4e24\u4e2a\u64cd\u4f5c\u6570\u65f6\uff0c\u5982\u679c XOR \u7684\u4e00\u4e2a \u4e14\u53ea\u6709\u4e00\u4e2a \u64cd\u4f5c\u6570\u4e3atrue (1) \uff0c\u5219 XOR \u7684\u8ba1\u7b97\u7ed3\u679c\u4e3a true (1) \u3002\u5982\u679c\u4e24\u8005\u90fd\u4e0d\u4e3a\u771f\uff0c\u5219\u8ba1\u7b97\u7ed3\u679c\u4e3a 0 \u3002\u8003\u8651\u8868\u8fbe\u5f0f\uff1a 0b0110 ^ 0b0011 1 2 3 4 0 1 1 0 XOR 0 0 1 1 ------- 0 1 0 1 \u4e5f\u53ef\u4ee5\u8bc4\u4f30\u590d\u5408 XOR \u8868\u8fbe\u5f0f\u5217\u6837\u5f0f\uff0c\u4f8b\u5982 0b0001 ^ 0b0011 ^ 0b0111 . \u5982\u679c\u4e00\u5217\u4e2d\u6709\u5076\u6570\u4e2a 1 \u4f4d\uff0c\u5219\u7ed3\u679c\u4e3a 0 \u3002\u5982\u679c\u4e00\u5217\u4e2d\u6709\u5947\u6570\u4e2a 1 \u4f4d\uff0c\u5219\u7ed3\u679c\u4e3a 1 \u3002 1 2 3 4 5 0 0 0 1 XOR 0 0 1 1 XOR 0 1 1 1 -------- 0 1 0 1 \u6309\u4f4d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u4e0e\u7b97\u672f\u8d4b\u503c\u8fd0\u7b97\u7b26\u7c7b\u4f3c\uff0cC++ \u63d0\u4f9b\u4e86\u6309\u4f4d\u8d4b\u503c\u8fd0\u7b97\u7b26\uff0c\u4ee5\u65b9\u4fbf\u53d8\u91cf\u7684\u4fee\u6539\u3002 Operator Symbol Form Operation \u5de6\u79fb\u5206\u914d <<= x <<= y x \u5de6\u79fb y \u4f4d \u53f3\u79fb\u5206\u914d >>= x >>= y x \u53f3\u79fb y \u4f4d \u6309\u4f4d\u6216\u8d4b\u503c |= x |= y \u5c06 x | y \u8d4b\u503c\u7ed9x \u6309\u4f4d\u4e0e\u8d4b\u503c &= x &= y \u5c06 x & y \u8d4b\u503c\u7ed9 x \u6309\u4f4d\u5f02\u6216\u8d4b\u503c ^= x^=y \u5c06 x ^ y \u8d4b\u503c\u7ed9 x \u4f8b\u5982 x = x >> 1; \uff0c\u60a8\u53ef\u4ee5\u4e0d\u5199 \uff0c\u800c\u5199 x >>= 1; . 1 2 3 4 5 6 7 8 9 10 11 #include #include int main () { std :: bitset < 4 > bits { 0b0100 }; bits >>= 1 ; std :: cout << bits << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u6253\u5370\uff1a 1 0010 \u6982\u62ec \u603b\u7ed3\u5982\u4f55\u4f7f\u7528\u5217\u65b9\u6cd5\u6309\u4f4d\u8fd0\u7b97\uff1a \u5728\u8bc4\u4f30 \u6309\u4f4d\u6216 \u65f6\uff0c\u5982\u679c\u5217\u4e2d\u7684\u4efb\u4f55\u4f4d\u4e3a 1\uff0c\u5219\u8be5\u5217\u7684\u7ed3\u679c\u4e3a 1\u3002 \u5728\u8bc4\u4f30 \u6309\u4f4d\u4e0e \u65f6\uff0c\u5982\u679c\u5217\u4e2d\u7684\u6240\u6709\u4f4d\u5747\u4e3a 1\uff0c\u5219\u8be5\u5217\u7684\u7ed3\u679c\u4e3a 1\u3002 \u5728\u8bc4\u4f30 \u6309\u4f4d\u5f02\u6216 \u65f6\uff0c\u5982\u679c\u4e00\u5217\u4e2d\u6709\u5947\u6570\u4e2a 1 \u4f4d\uff0c\u8be5\u5217\u7684\u7ed3\u679c\u4e3a 1\u3002","title":"O.2 - \u4f4d\u8fd0\u7b97\u7b26"},{"location":"O-2-bitwise-operators/#_1","text":"C++ \u63d0\u4f9b 6 \u4f4d\u64cd\u4f5c\u8fd0\u7b97\u7b26\uff0c\u901a\u5e38\u79f0\u4e3a \u6309\u4f4d\u8fd0\u7b97 \u7b26\uff1a Operator Symbol Form Operation \u5de6\u79fb << x << y x \u4e2d\u7684\u6240\u6709\u4f4d\u5de6\u79fb y \u4f4d \u53f3\u79fb >> x >> y x \u4e2d\u7684\u6240\u6709\u4f4d\u53f3\u79fb y \u4f4d \u6309\u4f4d\u975e ~ ~x x \u4e2d\u7684\u6240\u6709\u4f4d\u7ffb\u8f6c \u6309\u4f4d\u4e0e & x & y x \u4e2d\u7684\u6bcf\u4e00\u4f4d AND y \u4e2d\u7684\u6bcf\u4e00\u4f4d \u6309\u4f4d\u6216 | \u00d7 | y x \u4e2d\u7684\u6bcf\u4e00\u4f4d OR y \u4e2d\u7684\u6bcf\u4e00\u4f4d \u6309\u4f4d\u5f02\u6216 ^ x^y x \u4e2d\u7684\u6bcf\u4e00\u4f4d XOR y \u4e2d\u7684\u6bcf\u4e00\u4f4d \u4f5c\u8005\u6ce8 \u5728\u4ee5\u4e0b\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4e3b\u8981\u4f7f\u7528 4 \u4f4d\u4e8c\u8fdb\u5236\u503c\u3002\u8fd9\u662f\u4e3a\u4e86\u65b9\u4fbf\u548c\u4fdd\u6301\u793a\u4f8b\u7b80\u5355\u3002\u5728\u5b9e\u9645\u7a0b\u5e8f\u4e2d\uff0c\u4f7f\u7528\u7684\u4f4d\u6570\u53d6\u51b3\u4e8e\u5bf9\u8c61\u7684\u5927\u5c0f\uff08\u4f8b\u5982\uff0c\u4e00\u4e2a 2 \u5b57\u8282\u7684\u5bf9\u8c61\u5c06\u5b58\u50a8 16 \u4f4d\uff09\u3002 \u4e3a\u4e86\u53ef\u8bfb\u6027\uff0c\u6211\u4eec\u8fd8\u5c06\u7701\u7565\u4ee3\u7801\u793a\u4f8b\u4e4b\u5916\u7684 0b \u524d\u7f00\uff08\u4f8b\u5982\uff0c\u6211\u4eec\u5c06\u53ea\u4f7f\u7528 0101 \u800c\u4e0d\u662f 0b0101\uff09\u3002 \u6309\u4f4d\u8fd0\u7b97\u7b26\u662f\u4e3a\u6574\u6570\u7c7b\u578b\u548c std::bitset \u5b9a\u4e49\u7684\u3002\u6211\u4eec\u5c06\u5728\u793a\u4f8b\u4e2d\u4f7f\u7528 std::bitset\uff0c\u56e0\u4e3a\u5b83\u66f4\u5bb9\u6613\u4ee5\u4e8c\u8fdb\u5236\u5f62\u5f0f\u6253\u5370\u8f93\u51fa\u3002 \u907f\u514d\u4f7f\u7528\u5e26\u7b26\u53f7\u64cd\u4f5c\u6570\u7684\u6309\u4f4d\u8fd0\u7b97\u7b26 \uff0c\u56e0\u4e3a\u5728 C++20 \u4e4b\u524d\uff0c\u8bb8\u591a\u8fd0\u7b97\u7b26\u5c06\u8fd4\u56de\u5b9e\u73b0\u5b9a\u4e49\u7684\u7ed3\u679c\uff0c\u6216\u6709\u7740\u4f7f\u7528\u65e0\u7b26\u53f7\u64cd\u4f5c\u6570\uff08\u6216 std::bitset\uff09\u53ef\u4ee5\u8f7b\u677e\u907f\u514d\u7684\u5176\u4ed6\u6f5c\u5728\u95ee\u9898\u3002 \u6700\u4f73\u5b9e\u8df5 \u4e3a\u907f\u514d\u610f\u5916\uff0c\u8bf7\u4f7f\u7528\u5e26\u65e0\u7b26\u53f7\u64cd\u4f5c\u6570\u7684\u6309\u4f4d\u8fd0\u7b97\u7b26\u6216 std::bitset\u3002","title":"\u6309\u4f4d\u8fd0\u7b97\u7b26"},{"location":"O-2-bitwise-operators/#_2","text":"\u6309\u4f4d\u5de6\u79fb (<<) \u8fd0\u7b97\u7b26\u5c06\u4f4d\u5411\u5de6\u79fb\u52a8\u3002\u5de6\u64cd\u4f5c\u6570\u662f\u8981\u79fb\u4f4d\u7684\u4f4d\u7684\u8868\u8fbe\u5f0f\uff0c\u53f3\u64cd\u4f5c\u6570\u662f\u8981\u5de6\u79fb\u7684\u6574\u6570\u4f4d\u6570\u3002 \u6240\u4ee5\u5f53\u6211\u4eec\u8bf4 \u65f6 x << 1 \uff0c\u6211\u4eec\u662f\u5728\u8bf4\u201c\u5c06\u53d8\u91cf x \u4e2d\u7684\u4f4d\u5de6\u79fb\u4e00\u4f4d\u201d\u3002\u4ece\u53f3\u4fa7\u79fb\u5165\u7684\u65b0\u4f4d\u63a5\u6536\u503c 0\u3002 0011 << 1 \u662f 0110 0011 << 2 \u662f 1100 0011 << 3 \u662f 1000 \u8bf7\u6ce8\u610f\uff0c\u5728\u7b2c\u4e09\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5c06\u4e00\u4e2a1\u4ece\u5de6\u7aef\u504f\u79fb\u51fa\u4e86\uff01\u4ece\u4e8c\u8fdb\u5236\u6570\u672b\u5c3e\u79fb\u51fa\u7684\u4f4d\u5c06\u6c38\u8fdc\u4e22\u5931\u3002 \u6309\u4f4d\u53f3\u79fb (>>) \u8fd0\u7b97\u7b26\u5c06\u4f4d\u53f3\u79fb\u3002 1100 >> 1 \u662f 0110 1100 >> 2 \u662f 0011 1100 >> 3 \u662f 0001 \u8bf7\u6ce8\u610f\uff0c\u5728\u7b2c\u4e09\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5c06\u4e00\u4e2a1\u4ece\u53f3\u7aef\u504f\u79fb\u51fa\u4e86\uff0c\u56e0\u6b64\u5b83\u4e22\u5931\u4e86\u3002 \u8fd9\u662f\u8fdb\u884c\u4e00\u4e9b\u4f4d\u79fb\u7684\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include #include int main () { std :: bitset < 4 > x { 0b1100 }; std :: cout << x << '\\n' ; std :: cout << ( x >> 1 ) << '\\n' ; // shift right by 1, yielding 0110 std :: cout << ( x << 1 ) << '\\n' ; // shift left by 1, yielding 1000 return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 3 1100 0110 1000","title":"\u6309\u4f4d\u5de6\u79fb (<<) \u548c\u6309\u4f4d\u53f3\u79fb (>>) \u8fd0\u7b97\u7b26"},{"location":"O-2-bitwise-operators/#operator-operator","text":"\u4ed6\u4eec\u80af\u5b9a\u662f\u3002 \u4eca\u5929\u7684\u7a0b\u5e8f\u901a\u5e38\u4e0d\u4f1a\u5927\u91cf\u4f7f\u7528\u6309\u4f4d\u5de6\u79fb\u548c\u53f3\u79fb\u8fd0\u7b97\u7b26\u6765\u79fb\u4f4d\u3002\u76f8\u53cd\uff0c\u60a8\u503e\u5411\u4e8e\u770b\u5230\u4e0e std::cout\uff08\u6216\u5176\u4ed6\u6d41\u5bf9\u8c61\uff09\u4e00\u8d77\u4f7f\u7528\u7684\u6309\u4f4d\u5de6\u79fb\u8fd0\u7b97\u7b26\u6765\u8f93\u51fa\u6587\u672c\u3002\u8003\u8651\u4ee5\u4e0b\u7a0b\u5e8f\uff1a 1 2 3 4 5 6 7 8 9 10 11 #include #include int main () { unsigned int x { 0b0100 }; x = x << 1 ; // use operator<< for left shift std :: cout << std :: bitset < 4 > { x } << '\\n' ; // use operator<< for output return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u6253\u5370\uff1a 1 1000 \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0coperator<< \u662f\u5982\u4f55\u77e5\u9053\u5728\u4e00\u79cd\u60c5\u51b5\u4e0b\u79fb\u4f4d\u800c\u5728\u53e6\u4e00\u79cd\u60c5\u51b5\u4e0b\u8f93\u51fa x \u7684\u5462\uff1f\u7b54\u6848\u662f std::cout \u5df2\u7ecf \u91cd\u8f7d \uff08\u63d0\u4f9b\u4e86\u66ff\u4ee3\u5b9a\u4e49\uff09 operator<< \u6267\u884c\u63a7\u5236\u53f0\u8f93\u51fa\u800c\u4e0d\u662f\u4f4d\u79fb\u3002 \u5f53\u7f16\u8bd1\u5668\u770b\u5230 operator<< \u7684\u5de6\u64cd\u4f5c\u6570\u662f std::cout \u65f6\uff0c\u5b83\u77e5\u9053\u5b83\u5e94\u8be5\u8c03\u7528 std::cout \u91cd\u8f7d\u7684 operator<< \u7248\u672c\u6765\u8fdb\u884c\u8f93\u51fa\u3002\u5982\u679c\u5de6\u64cd\u4f5c\u6570\u662f\u6574\u6570\u7c7b\u578b\uff0c\u5219 operator<< \u77e5\u9053\u5b83\u5e94\u8be5\u6267\u884c\u5176\u901a\u5e38\u7684\u4f4d\u79fb\u884c\u4e3a\u3002 \u8fd9\u540c\u6837\u9002\u7528\u4e8e\u8fd0\u7b97\u7b26>>\u3002 \u8bf7\u6ce8\u610f\uff0c\u5982\u679c\u60a8\u5bf9\u8f93\u51fa\u548c\u5de6\u79fb\u90fd\u4f7f\u7528\u8fd0\u7b97\u7b26 <<\uff0c\u5219\u9700\u8981\u62ec\u53f7\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 #include #include int main () { std :: bitset < 4 > x { 0b0110 }; std :: cout << x << 1 << '\\n' ; // print value of x (0110), then 1 std :: cout << ( x << 1 ) << '\\n' ; // print x left shifted by 1 (1100) return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 01101 1100 \u7b2c\u4e00\u884c\u6253\u5370 x \u7684\u503c (0110)\uff0c\u7136\u540e\u662f\u6587\u5b57 1\u3002\u7b2c\u4e8c\u884c\u6253\u5370 x \u5de6\u79fb 1 (1100) \u7684\u503c\u3002 \u6211\u4eec\u5c06\u5728\u4ee5\u540e\u7684\u90e8\u5206\u4e2d\u66f4\u591a\u5730\u8ba8\u8bba\u8fd0\u7b97\u7b26\u91cd\u8f7d\uff0c\u5305\u62ec\u8ba8\u8bba\u5982\u4f55\u4e3a\u60a8\u81ea\u5df1\u7684\u76ee\u7684\u91cd\u8f7d\u8fd0\u7b97\u7b26\u3002","title":"\u4ec0\u4e48\uff01\uff1foperator<< \u548c operator>> \u4e0d\u662f\u7528\u4e8e\u8f93\u5165\u548c\u8f93\u51fa\u5417\uff1f"},{"location":"O-2-bitwise-operators/#_3","text":"\u6309\u4f4d\u975e \u8fd0\u7b97\u7b26 (~) \u53ef\u80fd\u662f\u6240\u6709\u6309\u4f4d\u8fd0\u7b97\u7b26\u4e2d\u6700\u5bb9\u6613\u7406\u89e3\u7684\u3002\u5b83\u53ea\u662f\u5c06\u6bcf\u4e2a\u4f4d\u4ece 0 \u7ffb\u8f6c\u4e3a 1\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u8bf7\u6ce8\u610f\uff0c \u6309\u4f4d NOT \u7684\u7ed3\u679c\u53d6\u51b3\u4e8e\u60a8\u7684\u6570\u636e\u7c7b\u578b\u7684\u5927\u5c0f\u3002 \u7ffb\u8f6c 4 \u4f4d\uff1a ~0100 \u662f 1011 \u7ffb\u8f6c 8 \u4f4d\uff1a ~0000 0100 \u662f 1111 1011 \u5728 4 \u4f4d\u548c 8 \u4f4d\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4ece\u76f8\u540c\u7684\u6570\u5b57\u5f00\u59cb\uff08\u4e8c\u8fdb\u5236 0100 \u4e0e 0000 0100 \u76f8\u540c\uff0c\u5341\u8fdb\u5236 7 \u4e0e 07 \u76f8\u540c\uff09\uff0c\u4f46\u6700\u7ec8\u5f97\u5230\u4e0d\u540c\u7684\u7ed3\u679c\u3002 \u6211\u4eec\u53ef\u4ee5\u5728\u4ee5\u4e0b\u7a0b\u5e8f\u4e2d\u770b\u5230\u8fd9\u4e00\u70b9\uff1a 1 2 3 4 5 6 7 8 9 #include #include int main () { std :: cout << ~ std :: bitset < 4 > { 0b0100 } << ' ' << ~ std :: bitset < 8 > { 0b0100 } << '\\n' ; return 0 ; } ```\u8fd9\u6253\u5370\uff1a \u8fd9\u6253\u5370\uff1a 1011 11111011 1 2 3 4 5 ## \u6309\u4f4d\u6216 **\u6309\u4f4d\u6216**(|) \u7684\u5de5\u4f5c\u65b9\u5f0f\u4e0e\u5176\u5bf9\u5e94\u7684*\u903b\u8f91\u6216*\u975e\u5e38\u76f8\u4f3c\u3002\u4f46\u662f\uff0c\u4e0d\u662f\u5c06*OR*\u5e94\u7528\u4e8e\u64cd\u4f5c\u6570\u4ee5\u4ea7\u751f\u5355\u4e2a\u7ed3\u679c\uff0c\u800c\u662f*\u5c06\u6309\u4f4d OR*\u5e94\u7528\u4e8e\u6bcf\u4e00\u4f4d\uff01\u4f8b\u5982\uff0c\u8003\u8651\u8868\u8fbe\u5f0f`0b0101 | 0b0110`\u3002 \u8981\u8fdb\u884c\uff08\u4efb\u4f55\uff09\u6309\u4f4d\u8fd0\u7b97\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u50cf\u8fd9\u6837\u5c06\u4e24\u4e2a\u64cd\u4f5c\u6570\u6392\u5217\u8d77\u6765\uff1a 0 1 0 1 OR 0 1 1 0 1 2 3 \u7136\u540e\u5c06\u64cd\u4f5c\u5e94\u7528\u4e8e\u6bcf\u4e00*\u5217\u4f4d*\u3002 \u5982\u679c\u60a8\u8fd8\u8bb0\u5f97\u7684\u8bdd\uff0c\u5982\u679c\u5de6\u64cd\u4f5c\u6570\u3001\u53f3\u64cd\u4f5c\u6570\u6216\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u4e3a*\u771f (1) \uff0c\u5219**\u903b\u8f91 OR*\u7684\u8ba1\u7b97\u7ed3\u679c\u4e3a*\u771f (1* ) \uff0c\u5426\u5219*\u4e3a 0*\u3002\u5982\u679c\u5de6\u4fa7\u4f4d\u3001\u53f3\u4fa7\u4f4d\u6216\u4e24\u4e2a\u4f4d\u90fd\u4e3a*1*\uff0c*\u5219\u6309\u4f4d\u6216*\u8ba1\u7b97\u7ed3\u679c\u4e3a*1 \uff0c\u5426\u5219\u4e3a**0*\u3002\u56e0\u6b64\uff0c\u8868\u8fbe\u5f0f\u7684\u8ba1\u7b97\u7ed3\u679c\u5982\u4e0b\uff1a 0 1 0 1 OR 0 1 1 0 0 1 1 1 1 2 3 4 5 6 7 8 9 10 11 12 \u6211\u4eec\u7684\u7ed3\u679c\u662f 0111 \u4e8c\u8fdb\u5236\u3002 ```cpp #include #include int main() { std::cout << (std::bitset<4>{ 0b0101 } | std::bitset<4>{ 0b0110 }) << '\\n'; return 0; } \u8fd9\u6253\u5370\uff1a 1 0111 \u6211\u4eec\u53ef\u4ee5\u5bf9\u590d\u5408 OR \u8868\u8fbe\u5f0f\u505a\u540c\u6837\u7684\u4e8b\u60c5\uff0c\u4f8b\u5982 0b0111 | 0b0011 | 0b0001 . \u5982\u679c\u4e00\u5217\u4e2d\u7684\u4efb\u4f55\u4f4d\u4e3a 1 \uff0c\u5219\u8be5\u5217\u7684\u7ed3\u679c\u4e3a 1 \u3002 1 2 3 4 5 0 1 1 1 OR 0 0 1 1 OR 0 0 0 1 -------- 0 1 1 1 \u8fd9\u662f\u4e0a\u9762\u7684\u4ee3\u7801\uff1a 1 2 3 4 5 6 7 8 9 #include #include int main () { std :: cout << ( std :: bitset < 4 > { 0b0111 } | std :: bitset < 4 > { 0b0011 } | std :: bitset < 4 > { 0b0001 }) << '\\n' ; return 0 ; } \u8fd9\u6253\u5370\uff1a 1 0111","title":"\u6309\u4f4d\u975e"},{"location":"O-2-bitwise-operators/#_4","text":"\u6309\u4f4d\u4e0e (&) \u7684\u5de5\u4f5c\u65b9\u5f0f\u4e0e\u4e0a\u8ff0\u7c7b\u4f3c\u3002\u5982\u679c\u5de6\u53f3\u64cd\u4f5c\u6570\u7684\u8ba1\u7b97\u7ed3\u679c\u90fd\u4e3a true \uff0c\u5219 \u903b\u8f91 AND\u7684\u8ba1\u7b97\u7ed3\u679c\u4e3a true \u3002\u5982\u679c\u5217\u4e2d\u7684\u4e24\u4f4d\u90fd\u4e3a 1 \uff0c\u5219\u6309\u4f4d\u4e0e \u8ba1\u7b97\u7ed3\u679c\u4e3a \u771f (1) \u3002\u8003\u8651\u8868\u8fbe\u5f0f\u3002\u6392\u5217\u6bcf\u4e2a\u4f4d\u5e76\u5bf9\u6bcf\u4e00\u5217\u4f4d\u5e94\u7528 AND \u8fd0\u7b97\uff1a 0b0101 & 0b0110 1 2 3 4 0 1 0 1 AND 0 1 1 0 -------- 0 1 0 0 1 2 3 4 5 6 7 8 #include #include int main () { std :: cout << ( std :: bitset < 4 > { 0b0101 } & std :: bitset < 4 > { 0b0110 }) << '\\n' ; return 0 ; } \u8fd9\u6253\u5370\uff1a 1 0100 \u540c\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u590d\u5408 AND \u8868\u8fbe\u5f0f\u505a\u540c\u6837\u7684\u4e8b\u60c5\uff0c\u4f8b\u5982 0b0001 & 0b0011 & 0b0111 . \u5982\u679c\u4e00\u5217\u4e2d\u7684\u6240\u6709\u4f4d\u5747\u4e3a 1\uff0c\u5219\u8be5\u5217\u7684\u7ed3\u679c\u4e3a 1\u3002 1 2 3 4 5 0 0 0 1 AND 0 0 1 1 AND 0 1 1 1 -------- 0 0 0 1 1 2 3 4 5 6 7 8 9 #include #include int main () { std :: cout << ( std :: bitset < 4 > { 0b0001 } & std :: bitset < 4 > { 0b0011 } & std :: bitset < 4 > { 0b0111 }) << '\\n' ; return 0 ; } \u8fd9\u6253\u5370\uff1a 1 0001","title":"\u6309\u4f4d\u4e0e"},{"location":"O-2-bitwise-operators/#_5","text":"\u6700\u540e\u4e00\u4e2a\u8fd0\u7b97\u7b26\u662f \u6309\u4f4d\u5f02\u6216 (^)\uff0c\u4e5f\u79f0\u4e3a \u5f02\u6216 \u3002 \u8bc4\u4f30\u4e24\u4e2a\u64cd\u4f5c\u6570\u65f6\uff0c\u5982\u679c XOR \u7684\u4e00\u4e2a \u4e14\u53ea\u6709\u4e00\u4e2a \u64cd\u4f5c\u6570\u4e3atrue (1) \uff0c\u5219 XOR \u7684\u8ba1\u7b97\u7ed3\u679c\u4e3a true (1) \u3002\u5982\u679c\u4e24\u8005\u90fd\u4e0d\u4e3a\u771f\uff0c\u5219\u8ba1\u7b97\u7ed3\u679c\u4e3a 0 \u3002\u8003\u8651\u8868\u8fbe\u5f0f\uff1a 0b0110 ^ 0b0011 1 2 3 4 0 1 1 0 XOR 0 0 1 1 ------- 0 1 0 1 \u4e5f\u53ef\u4ee5\u8bc4\u4f30\u590d\u5408 XOR \u8868\u8fbe\u5f0f\u5217\u6837\u5f0f\uff0c\u4f8b\u5982 0b0001 ^ 0b0011 ^ 0b0111 . \u5982\u679c\u4e00\u5217\u4e2d\u6709\u5076\u6570\u4e2a 1 \u4f4d\uff0c\u5219\u7ed3\u679c\u4e3a 0 \u3002\u5982\u679c\u4e00\u5217\u4e2d\u6709\u5947\u6570\u4e2a 1 \u4f4d\uff0c\u5219\u7ed3\u679c\u4e3a 1 \u3002 1 2 3 4 5 0 0 0 1 XOR 0 0 1 1 XOR 0 1 1 1 -------- 0 1 0 1","title":"\u6309\u4f4d\u5f02\u6216"},{"location":"O-2-bitwise-operators/#_6","text":"\u4e0e\u7b97\u672f\u8d4b\u503c\u8fd0\u7b97\u7b26\u7c7b\u4f3c\uff0cC++ \u63d0\u4f9b\u4e86\u6309\u4f4d\u8d4b\u503c\u8fd0\u7b97\u7b26\uff0c\u4ee5\u65b9\u4fbf\u53d8\u91cf\u7684\u4fee\u6539\u3002 Operator Symbol Form Operation \u5de6\u79fb\u5206\u914d <<= x <<= y x \u5de6\u79fb y \u4f4d \u53f3\u79fb\u5206\u914d >>= x >>= y x \u53f3\u79fb y \u4f4d \u6309\u4f4d\u6216\u8d4b\u503c |= x |= y \u5c06 x | y \u8d4b\u503c\u7ed9x \u6309\u4f4d\u4e0e\u8d4b\u503c &= x &= y \u5c06 x & y \u8d4b\u503c\u7ed9 x \u6309\u4f4d\u5f02\u6216\u8d4b\u503c ^= x^=y \u5c06 x ^ y \u8d4b\u503c\u7ed9 x \u4f8b\u5982 x = x >> 1; \uff0c\u60a8\u53ef\u4ee5\u4e0d\u5199 \uff0c\u800c\u5199 x >>= 1; . 1 2 3 4 5 6 7 8 9 10 11 #include #include int main () { std :: bitset < 4 > bits { 0b0100 }; bits >>= 1 ; std :: cout << bits << '\\n' ; return 0 ; } \u8fd9\u4e2a\u7a0b\u5e8f\u6253\u5370\uff1a 1 0010","title":"\u6309\u4f4d\u8d4b\u503c\u8fd0\u7b97\u7b26"},{"location":"O-2-bitwise-operators/#_7","text":"\u603b\u7ed3\u5982\u4f55\u4f7f\u7528\u5217\u65b9\u6cd5\u6309\u4f4d\u8fd0\u7b97\uff1a \u5728\u8bc4\u4f30 \u6309\u4f4d\u6216 \u65f6\uff0c\u5982\u679c\u5217\u4e2d\u7684\u4efb\u4f55\u4f4d\u4e3a 1\uff0c\u5219\u8be5\u5217\u7684\u7ed3\u679c\u4e3a 1\u3002 \u5728\u8bc4\u4f30 \u6309\u4f4d\u4e0e \u65f6\uff0c\u5982\u679c\u5217\u4e2d\u7684\u6240\u6709\u4f4d\u5747\u4e3a 1\uff0c\u5219\u8be5\u5217\u7684\u7ed3\u679c\u4e3a 1\u3002 \u5728\u8bc4\u4f30 \u6309\u4f4d\u5f02\u6216 \u65f6\uff0c\u5982\u679c\u4e00\u5217\u4e2d\u6709\u5947\u6570\u4e2a 1 \u4f4d\uff0c\u8be5\u5217\u7684\u7ed3\u679c\u4e3a 1\u3002","title":"\u6982\u62ec"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/","tags":["bit"],"text":"Key Takeaway \u4f4d\u63a9\u7801 \u4e3a\u4e86\u64cd\u4f5c\u5355\u4e2a\u4f4d\uff08\u4f8b\u5982\u6253\u5f00\u6216\u5173\u95ed\u5b83\u4eec\uff09\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e9b\u65b9\u6cd5\u6765\u8bc6\u522b\u6211\u4eec\u60f3\u8981\u64cd\u4f5c\u7684\u7279\u5b9a\u4f4d\u3002\u4e0d\u5e78\u7684\u662f\uff0c\u6309\u4f4d\u8fd0\u7b97\u7b26\u4e0d\u77e5\u9053\u5982\u4f55\u5904\u7406\u4f4d\u4f4d\u7f6e\u3002\u76f8\u53cd\uff0c\u4ed6\u4eec\u4f7f\u7528\u4f4d\u63a9\u7801\u3002 \u4f4d\u63a9\u7801 \u662f\u4e00\u7ec4\u9884\u5b9a\u4e49\u7684\u4f4d\uff0c\u7528\u4e8e\u9009\u62e9\u54ea\u4e9b\u7279\u5b9a\u4f4d\u5c06\u88ab\u540e\u7eed\u64cd\u4f5c\u4fee\u6539\u3002 \u8003\u8651\u4e00\u4e2a\u771f\u5b9e\u7684\u6848\u4f8b\uff0c\u60a8\u60f3\u8981\u7ed8\u5236\u4e00\u4e2a\u7a97\u6846\u3002\u5982\u679c\u60a8\u4e0d\u5c0f\u5fc3\uff0c\u60a8\u4e0d\u4ec5\u4f1a\u5192\u7740\u7c89\u5237\u7a97\u6846\u7684\u98ce\u9669\uff0c\u8fd8\u4f1a\u5192\u7740\u7c89\u5237\u73bb\u7483\u672c\u8eab\u7684\u98ce\u9669\u3002\u60a8\u53ef\u4ee5\u8d2d\u4e70\u4e00\u4e9b\u906e\u853d\u80f6\u5e26\u5e76\u5c06\u5176\u8d34\u5728\u73bb\u7483\u548c\u60a8\u4e0d\u60f3\u6d82\u6f06\u7684\u4efb\u4f55\u5176\u4ed6\u90e8\u4ef6\u4e0a\u3002\u7136\u540e\uff0c\u5f53\u60a8\u7ed8\u753b\u65f6\uff0c\u906e\u853d\u80f6\u5e26\u4f1a\u963b\u6b62\u6cb9\u6f06\u63a5\u89e6\u5230\u60a8\u4e0d\u60f3\u6d82\u6f06\u7684\u4efb\u4f55\u4e1c\u897f\u3002\u6700\u540e\uff0c\u53ea\u6709\u672a\u906e\u7f69\u7684\u90e8\u5206\uff08\u60a8\u8981\u7ed8\u5236\u7684\u90e8\u5206\uff09\u88ab\u7ed8\u5236\u3002 \u4f4d\u63a9\u7801\u672c\u8d28\u4e0a\u5bf9\u4f4d\u6267\u884c\u76f8\u540c\u7684\u529f\u80fd\u2014\u2014 \u4f4d\u63a9\u7801\u963b\u6b62\u6309\u4f4d\u8fd0\u7b97\u7b26\u63a5\u89e6\u6211\u4eec\u4e0d\u60f3\u4fee\u6539\u7684\u4f4d\uff0c\u5e76\u5141\u8bb8\u8bbf\u95ee\u6211\u4eec\u786e\u5b9e\u60f3\u8981\u4fee\u6539\u7684\u4f4d\u3002 \u8ba9\u6211\u4eec\u9996\u5148\u63a2\u8ba8\u5982\u4f55\u5b9a\u4e49\u4e00\u4e9b\u7b80\u5355\u7684\u4f4d\u63a9\u7801\uff0c\u7136\u540e\u6211\u4eec\u5c06\u5411\u60a8\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u5b83\u4eec\u3002 \u5728 C++14 \u4e2d\u5b9a\u4e49\u4f4d\u63a9\u7801 \u6700\u7b80\u5355\u7684\u4e00\u7ec4\u4f4d\u63a9\u7801\u662f\u4e3a\u6bcf\u4e2a\u4f4d\u4f4d\u7f6e\u5b9a\u4e49\u4e00\u4e2a\u4f4d\u63a9\u7801\u3002\u6211\u4eec\u7528 0 \u6765\u5c4f\u853d\u6211\u4eec\u4e0d\u5173\u5fc3\u7684\u4f4d\uff0c\u7528 1 \u6765\u8868\u793a\u6211\u4eec\u60f3\u8981\u4fee\u6539\u7684\u4f4d\u3002 \u5c3d\u7ba1\u4f4d\u63a9\u7801\u53ef\u4ee5\u662f\u6587\u5b57\uff0c\u4f46\u5b83\u4eec\u901a\u5e38\u88ab\u5b9a\u4e49\u4e3a\u7b26\u53f7\u5e38\u91cf\uff0c\u56e0\u6b64\u53ef\u4ee5\u4e3a\u5b83\u4eec\u6307\u5b9a\u4e00\u4e2a\u6709\u610f\u4e49\u7684\u540d\u79f0\u5e76\u6613\u4e8e\u91cd\u7528\u3002 \u56e0\u4e3a C++14 \u652f\u6301\u4e8c\u8fdb\u5236\u6587\u5b57\uff0c\u6240\u4ee5\u5b9a\u4e49\u8fd9\u4e9b\u4f4d\u63a9\u7801\u5f88\u5bb9\u6613\uff1a 1 2 3 4 5 6 7 8 9 10 #include constexpr std :: uint8_t mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: uint8_t mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: uint8_t mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: uint8_t mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: uint8_t mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: uint8_t mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: uint8_t mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: uint8_t mask7 { 0b1000'0000 }; // represents bit 7 \u73b0\u5728\u6211\u4eec\u6709\u4e00\u7ec4\u4ee3\u8868\u6bcf\u4e2a\u4f4d\u4f4d\u7f6e\u7684\u7b26\u53f7\u5e38\u91cf\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u6765\u64cd\u4f5c\u4f4d\uff08\u7a0d\u540e\u6211\u4eec\u5c06\u5c55\u793a\u5982\u4f55\u64cd\u4f5c\uff09\u3002 \u5728 C++11 \u6216\u66f4\u65e9\u7248\u672c\u4e2d\u5b9a\u4e49\u4f4d\u63a9\u7801 \u56e0\u4e3a C++11 \u4e0d\u652f\u6301\u4e8c\u8fdb\u5236\u6587\u5b57 \uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u6765\u8bbe\u7f6e\u7b26\u53f7\u5e38\u91cf\u3002\u6709\u4e24\u79cd\u5f88\u597d\u7684\u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\u3002 \u7b2c\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u5341\u516d\u8fdb\u5236\u6587\u5b57\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u5728 4.15-\u5b57\u9762\u91cf \u4e2d\u8ba8\u8bba\u4e86\u5341\u516d\u8fdb\u5236\u3002 \u5341\u516d\u8fdb\u5236\u8f6c\u4e8c\u8fdb\u5236\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a \u5341\u516d\u8fdb\u5236 0 1 2 3 4 5 6 7 8 9 A B C D E F \u4e8c\u8fdb\u5236 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u50cf\u8fd9\u6837\u4f7f\u7528\u5341\u516d\u8fdb\u5236\u5b9a\u4e49\u4f4d\u63a9\u7801\uff1a 1 2 3 4 5 6 7 8 constexpr std :: uint8_t mask0 { 0x01 }; // hex for 0000 0001 constexpr std :: uint8_t mask1 { 0x02 }; // hex for 0000 0010 constexpr std :: uint8_t mask2 { 0x04 }; // hex for 0000 0100 constexpr std :: uint8_t mask3 { 0x08 }; // hex for 0000 1000 constexpr std :: uint8_t mask4 { 0x10 }; // hex for 0001 0000 constexpr std :: uint8_t mask5 { 0x20 }; // hex for 0010 0000 constexpr std :: uint8_t mask6 { 0x40 }; // hex for 0100 0000 constexpr std :: uint8_t mask7 { 0x80 }; // hex for 1000 0000 \u6709\u65f6\u4f1a\u7701\u7565\u524d\u5bfc\u7684\u5341\u516d\u8fdb\u5236 0\uff08\u4f8b\u5982\uff0c 0x01 \u60a8\u53ea\u4f1a\u770b\u5230 0x1 \uff09\u3002\u65e0\u8bba\u54ea\u79cd\u65b9\u5f0f\uff0c\u5982\u679c\u60a8\u4e0d\u719f\u6089\u5341\u516d\u8fdb\u5236\u5230\u4e8c\u8fdb\u5236\u7684\u8f6c\u6362\uff0c\u8fd9\u53ef\u80fd\u6709\u70b9\u96be\u4ee5\u9605\u8bfb\u3002 \u4e00\u79cd\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5de6\u79fb\u8fd0\u7b97\u7b26\u5c06\u4e00\u4f4d\u79fb\u52a8\u5230\u6b63\u786e\u7684\u4f4d\u7f6e\uff1a 1 2 3 4 5 6 7 8 constexpr std :: uint8_t mask0 { 1 << 0 }; // 0000 0001 constexpr std :: uint8_t mask1 { 1 << 1 }; // 0000 0010 constexpr std :: uint8_t mask2 { 1 << 2 }; // 0000 0100 constexpr std :: uint8_t mask3 { 1 << 3 }; // 0000 1000 constexpr std :: uint8_t mask4 { 1 << 4 }; // 0001 0000 constexpr std :: uint8_t mask5 { 1 << 5 }; // 0010 0000 constexpr std :: uint8_t mask6 { 1 << 6 }; // 0100 0000 constexpr std :: uint8_t mask7 { 1 << 7 }; // 1000 0000 \u6d4b\u8bd5\u4e00\u4e0b\uff08\u770b\u5b83\u662f1\u8fd8\u662f0\uff09 \u73b0\u5728\u6211\u4eec\u6709\u4e86\u4e00\u7ec4\u4f4d\u63a9\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u4eec\u4e0e\u4f4d\u6807\u5fd7\u53d8\u91cf\u7ed3\u5408\u4f7f\u7528\u6765\u64cd\u4f5c\u6211\u4eec\u7684\u4f4d\u6807\u5fd7\u3002 \u8981\u786e\u5b9a\u67d0\u4e2a\u4f4d\u662f\u5f00\u8fd8\u662f\u5173\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u4e0e \u7ed3\u5408\u76f8\u5e94\u4f4d\u7684\u4f4d\u63a9\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include int main () { constexpr std :: uint8_t mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: uint8_t mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: uint8_t mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: uint8_t mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: uint8_t mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: uint8_t mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: uint8_t mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: uint8_t mask7 { 0b1000'0000 }; // represents bit 7 std :: uint8_t flags { 0b0000'0101 }; // 8 bits in size means room for 8 flags std :: cout << \"bit 0 is \" << (( flags & mask0 ) ? \"on \\n \" : \"off \\n \" ); std :: cout << \"bit 1 is \" << (( flags & mask1 ) ? \"on \\n \" : \"off \\n \" ); return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 bit 1 is off bit 1 is on \u7f6e1\u4e00\u4f4d \u8981\u8bbe\u7f6e\uff08\u7f6e1\uff09\u4f4d\uff0c\u6211\u4eec\u5c06\u6309\u4f4d\u6216\u7b49\u4e8e\uff08\u8fd0\u7b97\u7b26 |=\uff09\u4e0e\u76f8\u5e94\u4f4d\u7684\u4f4d\u63a9\u7801\u7ed3\u5408\u4f7f\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include int main () { constexpr std :: uint8_t mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: uint8_t mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: uint8_t mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: uint8_t mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: uint8_t mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: uint8_t mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: uint8_t mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: uint8_t mask7 { 0b1000'0000 }; // represents bit 7 std :: uint8_t flags { 0b0000'0101 }; // 8 bits in size means room for 8 flags std :: cout << \"bit 1 is \" << (( flags & mask1 ) ? \"on \\n \" : \"off \\n \" ); flags |= mask1 ; // turn on bit 1 std :: cout << \"bit 1 is \" << (( flags & mask1 ) ? \"on \\n \" : \"off \\n \" ); return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 bit 1 is off bit 1 is on \u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6309\u4f4d\u6216 \u540c\u65f6\u7f6e1\u591a\u4e2a\u4f4d\uff1a 1 flags |= ( mask4 | mask5 ); // turn bits 4 and 5 on at the same time \u7f6e0\u4e00\u4f4d \u8981\u6e05\u9664\u4f4d\uff08\u7f6e0\uff09\uff0c\u6211\u4eec\u540c\u65f6\u4f7f\u7528 \u6309\u4f4d\u4e0e \u548c \u6309\u4f4d\u975e \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include int main () { constexpr std :: uint8_t mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: uint8_t mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: uint8_t mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: uint8_t mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: uint8_t mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: uint8_t mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: uint8_t mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: uint8_t mask7 { 0b1000'0000 }; // represents bit 7 std :: uint8_t flags { 0b0000'0101 }; // 8 bits in size means room for 8 flags std :: cout << \"bit 2 is \" << (( flags & mask2 ) ? \"on \\n \" : \"off \\n \" ); flags &= ~ mask2 ; // turn off bit 2 std :: cout << \"bit 2 is \" << (( flags & mask2 ) ? \"on \\n \" : \"off \\n \" ); return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 bit 2 is on bit 2 is off \u6211\u4eec\u53ef\u4ee5\u540c\u65f6\u5173\u95ed\u591a\u4e2a\u4f4d\uff1a 1 flags &= ~ ( mask4 | mask5 ); // turn bits 4 and 5 off at the same time \u7ffb\u8f6c\u4e00\u4f4d \u8981\u5207\u6362\u4f4d\u72b6\u6001\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u5f02\u6216 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include int main () { constexpr std :: uint8_t mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: uint8_t mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: uint8_t mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: uint8_t mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: uint8_t mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: uint8_t mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: uint8_t mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: uint8_t mask7 { 0b1000'0000 }; // represents bit 7 std :: uint8_t flags { 0b0000'0101 }; // 8 bits in size means room for 8 flags std :: cout << \"bit 2 is \" << (( flags & mask2 ) ? \"on \\n \" : \"off \\n \" ); flags ^= mask2 ; // flip bit 2 std :: cout << \"bit 2 is \" << (( flags & mask2 ) ? \"on \\n \" : \"off \\n \" ); flags ^= mask2 ; // flip bit 2 std :: cout << \"bit 2 is \" << (( flags & mask2 ) ? \"on \\n \" : \"off \\n \" ); return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 3 bit 2 is on bit 2 is off bit 2 is on \u6211\u4eec\u53ef\u4ee5\u540c\u65f6\u7ffb\u8f6c\u591a\u4e2a\u4f4d\uff1a 1 flags ^= ( mask4 | mask5 ); // flip bits 4 and 5 at the same time \u4f4d\u63a9\u7801\u548c std::bitset std::bitset \u652f\u6301\u5168\u5957\u4f4d\u8fd0\u7b97\u7b26\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u4f7f\u7528\u51fd\u6570\uff08\u6d4b\u8bd5\u3001\u8bbe\u7f6e\u3001\u91cd\u7f6e\u548c\u7ffb\u8f6c\uff09\u4fee\u6539\u5355\u4e2a\u4f4d\u66f4\u5bb9\u6613\uff0c\u4f46\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4f7f\u7528\u6309\u4f4d\u8fd0\u7b97\u7b26\u548c\u4f4d\u63a9\u7801\u3002 \u4f60\u4e3a\u4ec0\u4e48\u8981\u8fd9\u6837\u505a\uff1f \u8fd9\u4e9b\u51fd\u6570test(),set(),reset(),flip()\u53ea\u5141\u8bb8\u60a8\u4fee\u6539\u5355\u4e2a\u4f4d\u3002\u6309\u4f4d\u8fd0\u7b97\u7b26\u5141\u8bb8\u60a8\u4e00\u6b21\u4fee\u6539\u591a\u4e2a\u4f4d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include #include #include int main () { constexpr std :: bitset < 8 > mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: bitset < 8 > mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: bitset < 8 > mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: bitset < 8 > mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: bitset < 8 > mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: bitset < 8 > mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: bitset < 8 > mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: bitset < 8 > mask7 { 0b1000'0000 }; // represents bit 7 std :: bitset < 8 > flags { 0b0000'0101 }; // 8 bits in size means room for 8 flags std :: cout << \"bit 1 is \" << ( flags . test ( 1 ) ? \"on \\n \" : \"off \\n \" ); std :: cout << \"bit 2 is \" << ( flags . test ( 2 ) ? \"on \\n \" : \"off \\n \" ); flags ^= ( mask1 | mask2 ); // flip bits 1 and 2 std :: cout << \"bit 1 is \" << ( flags . test ( 1 ) ? \"on \\n \" : \"off \\n \" ); std :: cout << \"bit 2 is \" << ( flags . test ( 2 ) ? \"on \\n \" : \"off \\n \" ); flags |= ( mask1 | mask2 ); // turn bits 1 and 2 on std :: cout << \"bit 1 is \" << ( flags . test ( 1 ) ? \"on \\n \" : \"off \\n \" ); std :: cout << \"bit 2 is \" << ( flags . test ( 2 ) ? \"on \\n \" : \"off \\n \" ); flags &= ~ ( mask1 | mask2 ); // turn bits 1 and 2 off std :: cout << \"bit 1 is \" << ( flags . test ( 1 ) ? \"on \\n \" : \"off \\n \" ); std :: cout << \"bit 2 is \" << ( flags . test ( 2 ) ? \"on \\n \" : \"off \\n \" ); return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 3 4 5 6 7 8 bit 1 is off bit 2 is on bit 1 is on bit 2 is off bit 1 is on bit 2 is on bit 1 is off bit 2 is off \u4f7f\u4f4d\u63a9\u7801\u6709\u610f\u4e49 \u5c06\u6211\u4eec\u7684\u4f4d\u63a9\u7801\u547d\u540d\u4e3a\u201cmask1\u201d\u6216\u201cmask2\u201d\u53ef\u4ee5\u544a\u8bc9\u6211\u4eec\u6b63\u5728\u64cd\u4f5c\u7684\u662f\u54ea\u4e2a\u4f4d\uff0c\u4f46\u4e0d\u4f1a\u544a\u8bc9\u6211\u4eec\u8be5\u4f4d\u6807\u5fd7\u5b9e\u9645\u7528\u4e8e\u4ec0\u4e48\u3002 \u6700\u4f73\u505a\u6cd5\u662f\u4e3a\u60a8\u7684\u4f4d\u63a9\u7801\u63d0\u4f9b\u6709\u7528\u7684\u540d\u79f0\uff0c\u4f5c\u4e3a\u8bb0\u5f55\u4f4d\u6807\u5fd7\u542b\u4e49\u7684\u4e00\u79cd\u65b9\u5f0f\u3002\u8fd9\u662f\u6211\u4eec\u53ef\u80fd\u7f16\u5199\u7684\u6e38\u620f\u7684\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include int main () { // Define a bunch of physical/emotional states constexpr std :: uint8_t isHungry { 1 << 0 }; // 0000 0001 constexpr std :: uint8_t isSad { 1 << 1 }; // 0000 0010 constexpr std :: uint8_t isMad { 1 << 2 }; // 0000 0100 constexpr std :: uint8_t isHappy { 1 << 3 }; // 0000 1000 constexpr std :: uint8_t isLaughing { 1 << 4 }; // 0001 0000 constexpr std :: uint8_t isAsleep { 1 << 5 }; // 0010 0000 constexpr std :: uint8_t isDead { 1 << 6 }; // 0100 0000 constexpr std :: uint8_t isCrying { 1 << 7 }; // 1000 0000 std :: uint8_t me {}; // all flags/options turned off to start me |= ( isHappy | isLaughing ); // I am happy and laughing me &= ~ isLaughing ; // I am no longer laughing // Query a few states // (we'll use static_cast to interpret the results as a boolean value) std :: cout << \"I am happy? \" << static_cast < bool > ( me & isHappy ) << '\\n' ; std :: cout << \"I am laughing? \" << static_cast < bool > ( me & isLaughing ) << '\\n' ; return 0 ; } \u4e0b\u9762\u662f\u4f7f\u7528 std::bitset \u5b9e\u73b0\u7684\u76f8\u540c\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include int main () { // Define a bunch of physical/emotional states constexpr std :: bitset < 8 > isHungry { 0b0000'0001 }; constexpr std :: bitset < 8 > isSad { 0b0000'0010 }; constexpr std :: bitset < 8 > isMad { 0b0000'0100 }; constexpr std :: bitset < 8 > isHappy { 0b0000'1000 }; constexpr std :: bitset < 8 > isLaughing { 0b0001'0000 }; constexpr std :: bitset < 8 > isAsleep { 0b0010'0000 }; constexpr std :: bitset < 8 > isDead { 0b0100'0000 }; constexpr std :: bitset < 8 > isCrying { 0b1000'0000 }; std :: bitset < 8 > me {}; // all flags/options turned off to start me |= ( isHappy | isLaughing ); // I am happy and laughing me &= ~ isLaughing ; // I am no longer laughing // Query a few states (we use the any() function to see if any bits remain set) std :: cout << \"I am happy? \" << ( me & isHappy ). any () << '\\n' ; std :: cout << \"I am laughing? \" << ( me & isLaughing ). any () << '\\n' ; return 0 ; } \u8fd9\u91cc\u6709\u4e24\u4e2a\u6ce8\u610f\u4e8b\u9879\uff1a\u9996\u5148\uff0cstd::bitset \u6ca1\u6709\u5141\u8bb8\u60a8\u4f7f\u7528\u4f4d\u63a9\u7801\u67e5\u8be2\u4f4d\u7684\u597d\u51fd\u6570\u3002\u56e0\u6b64\uff0c\u5982\u679c\u60a8\u60f3\u4f7f\u7528\u4f4d\u63a9\u7801\u800c\u4e0d\u662f\u4f4d\u7f6e\u7d22\u5f15\uff0c\u5219\u5fc5\u987b\u4f7f\u7528 \u6309\u4f4d\u4e0e \u67e5\u8be2\u4f4d\u3002\u5176\u6b21\uff0c\u6211\u4eec\u4f7f\u7528 any() \u51fd\u6570\uff0c\u5982\u679c\u8bbe\u7f6e\u4e86\u4efb\u4f55\u4f4d\uff0c\u8be5\u51fd\u6570\u8fd4\u56de true\uff0c\u5426\u5219\u8fd4\u56de false\uff0c\u4ee5\u67e5\u770b\u6211\u4eec\u67e5\u8be2\u7684\u4f4d\u662f\u5426\u4fdd\u6301\u6253\u5f00\u6216\u5173\u95ed\u3002 \u4ec0\u4e48\u65f6\u5019\u4f4d\u6807\u5fd7\u6700\u6709\u7528\uff1f \u7ec6\u5fc3\u7684\u8bfb\u8005\u53ef\u80fd\u4f1a\u6ce8\u610f\u5230\uff0c\u4e0a\u9762\u7684\u793a\u4f8b\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\u8282\u7701\u4efb\u4f55\u5185\u5b58\u30028 \u4e2a\u5e03\u5c14\u503c\u901a\u5e38\u9700\u8981 8 \u4e2a\u5b57\u8282\u3002\u4f46\u662f\u4e0a\u9762\u7684\u4f8b\u5b50\u4f7f\u7528\u4e86 9 \u4e2a\u5b57\u8282\uff088 \u4e2a\u5b57\u8282\u7528\u4e8e\u5b9a\u4e49\u4f4d\u63a9\u7801\uff0c1 \u4e2a\u5b57\u8282\u7528\u4e8e\u6807\u5fd7\u53d8\u91cf\uff09\uff01 \u5f53\u60a8\u6709\u8bb8\u591a\u76f8\u540c\u7684\u6807\u5fd7\u53d8\u91cf\u65f6\uff0c\u4f4d\u6807\u5fd7\u6700\u6709\u610f\u4e49\u3002\u4f8b\u5982\uff0c\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5047\u8bbe\u60a8\u6709 100 \u4e2a\u800c\u4e0d\u662f\u4e00\u4e2a\u4eba\uff08\u6211\uff09\u3002\u5982\u679c\u60a8\u6bcf\u4e2a\u4eba\u4f7f\u7528 8 \u4e2a\u5e03\u5c14\u503c\uff08\u6bcf\u4e2a\u53ef\u80fd\u7684\u72b6\u6001\u4e00\u4e2a\uff09\uff0c\u60a8\u5c06\u4f7f\u7528 800 \u4e2a\u5b57\u8282\u7684\u5185\u5b58\u3002\u5bf9\u4e8e\u4f4d\u6807\u5fd7\uff0c\u60a8\u5c06\u4f7f\u7528 8 \u4e2a\u5b57\u8282\u4f5c\u4e3a\u4f4d\u63a9\u7801\uff0c\u4f7f\u7528 100 \u4e2a\u5b57\u8282\u4f5c\u4e3a\u4f4d\u6807\u5fd7\u53d8\u91cf\uff0c\u603b\u5171\u9700\u8981 108 \u4e2a\u5b57\u8282\u7684\u5185\u5b58\u2014\u2014\u5927\u7ea6\u51cf\u5c11 8 \u500d\u7684\u5185\u5b58\u3002 \u5bf9\u4e8e\u5927\u591a\u6570\u7a0b\u5e8f\uff0c\u4f7f\u7528\u4f4d\u6807\u5fd7\u8282\u7701\u7684\u5185\u5b58\u91cf\u4e0d\u503c\u5f97\u589e\u52a0\u590d\u6742\u6027\u3002\u4f46\u662f\u5728\u6709\u6570\u4e07\u751a\u81f3\u6570\u767e\u4e07\u4e2a\u76f8\u4f3c\u5bf9\u8c61\u7684\u7a0b\u5e8f\u4e2d\uff0c\u4f7f\u7528\u4f4d\u6807\u5fd7\u53ef\u4ee5\u5927\u5927\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u3002\u5982\u679c\u9700\u8981\uff0c\u5c06\u5176\u5305\u542b\u5728\u60a8\u7684\u5de5\u5177\u5305\u4e2d\u662f\u4e00\u9879\u6709\u7528\u7684\u4f18\u5316\u3002 \u5728\u53e6\u4e00\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f4d\u6807\u5fd7\u548c\u4f4d\u63a9\u7801\u662f\u6709\u610f\u4e49\u7684\u3002\u60f3\u8c61\u4e00\u4e0b\uff0c\u60a8\u6709\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u91c7\u7528 32 \u79cd\u4e0d\u540c\u9009\u9879\u7684\u4efb\u610f\u7ec4\u5408\u3002\u7f16\u5199\u8be5\u51fd\u6570\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 32 \u4e2a\u5355\u72ec\u7684\u5e03\u5c14\u53c2\u6570\uff1a 1 void someFunction ( bool option1 , bool option2 , bool option3 , bool option4 , bool option5 , bool option6 , bool option7 , bool option8 , bool option9 , bool option10 , bool option11 , bool option12 , bool option13 , bool option14 , bool option15 , bool option16 , bool option17 , bool option18 , bool option19 , bool option20 , bool option21 , bool option22 , bool option23 , bool option24 , bool option25 , bool option26 , bool option27 , bool option28 , bool option29 , bool option30 , bool option31 , bool option32 ); \u5e0c\u671b\u4f60\u80fd\u7ed9\u4f60\u7684\u53c2\u6570\u8d77\u66f4\u5177\u63cf\u8ff0\u6027\u7684\u540d\u5b57\uff0c\u4f46\u8fd9\u91cc\u7684\u91cd\u70b9\u662f\u5411\u4f60\u5c55\u793a\u53c2\u6570\u5217\u8868\u662f\u591a\u4e48\u4ee4\u4eba\u8ba8\u538c\u3002 \u7136\u540e\u5f53\u4f60\u60f3\u8c03\u7528\u9009\u9879 10 \u548c 32 \u8bbe\u7f6e\u4e3a true \u7684\u51fd\u6570\u65f6\uff0c\u4f60\u5fc5\u987b\u8fd9\u6837\u505a\uff1a 1 someFunction ( false , false , false , false , false , false , false , false , false , true , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , true ); \u8fd9\u975e\u5e38\u96be\u4ee5\u9605\u8bfb\uff08\u9009\u9879 9\u300110 \u6216 11 \u662f\u5426\u8bbe\u7f6e\u4e3a true\uff1f\uff09\uff0c\u5e76\u4e14\u8fd8\u610f\u5473\u7740\u60a8\u5fc5\u987b\u8bb0\u4f4f\u54ea\u4e2a\u53c2\u6570\u5bf9\u5e94\u4e8e\u54ea\u4e2a\u9009\u9879\uff08\u5c06\u201c\u7f16\u8f91\u6807\u5fd7\u201d\u8bbe\u7f6e\u4e3a\u7b2c 9\u300110 \u6216\u7b2c 11 \u4e2a\u53c2\u6570\uff1f\uff09\u5b83\u4e5f\u53ef\u80fd\u4e0d\u662f\u5f88\u9ad8\u6548\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u51fd\u6570\u8c03\u7528\u90fd\u5fc5\u987b\u5c06 32 \u4e2a\u5e03\u5c14\u503c\u4ece\u8c03\u7528\u8005\u590d\u5236\u5230\u51fd\u6570\u3002 \u76f8\u53cd\uff0c\u5982\u679c\u60a8\u4f7f\u7528\u8fd9\u6837\u7684\u4f4d\u6807\u5fd7\u5b9a\u4e49\u51fd\u6570\uff1a 1 void someFunction ( std :: bitset < 32 > options ); \u7136\u540e\u4f60\u53ef\u4ee5\u4f7f\u7528\u4f4d\u6807\u5fd7\u6765\u53ea\u4f20\u9012\u4f60\u60f3\u8981\u7684\u9009\u9879\uff1a 1 someFunction ( option10 | option32 ); \u8fd9\u4e0d\u4ec5\u66f4\u5177\u53ef\u8bfb\u6027\uff0c\u800c\u4e14\u6027\u80fd\u4e5f\u53ef\u80fd\u66f4\u9ad8\uff0c\u56e0\u4e3a\u5b83\u53ea\u6d89\u53ca 2 \u4e2a\u64cd\u4f5c\uff08\u4e00\u4e2a \u6309\u4f4d\u6216 \u548c\u4e00\u4e2a\u53c2\u6570\u590d\u5236\uff09\u3002 \u8fd9\u5c31\u662f\u5e7f\u53d7\u597d\u8bc4\u7684 3d \u56fe\u5f62\u5e93 OpenGL \u9009\u62e9\u4f7f\u7528\u4f4d\u6807\u5fd7\u53c2\u6570\u800c\u4e0d\u662f\u8bb8\u591a\u8fde\u7eed\u7684\u5e03\u5c14\u53c2\u6570\u7684\u539f\u56e0\u4e4b\u4e00\u3002 \u4e0b\u9762\u662f\u6765\u81ea OpenGL \u7684\u793a\u4f8b\u51fd\u6570\u8c03\u7528\uff1a 1 glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // clear the color and the depth buffer GL_COLOR_BUFFER_BIT \u548c GL_DEPTH_BUFFER_BIT \u662f\u4f4d\u63a9\u7801\uff0c\u5b9a\u4e49\u5982\u4e0b\uff08\u5728 gl2.h \u4e2d\uff09\uff1a 1 2 3 #define GL_DEPTH_BUFFER_BIT 0x00000100 #define GL_STENCIL_BUFFER_BIT 0x00000400 #define GL_COLOR_BUFFER_BIT 0x00004000 \u6d89\u53ca\u591a\u4e2a\u4f4d\u7684\u4f4d\u63a9\u7801 \u5c3d\u7ba1\u4f4d\u63a9\u7801\u901a\u5e38\u7528\u4e8e\u9009\u62e9\u5355\u4e2a\u4f4d\uff0c\u4f46\u5b83\u4eec\u4e5f\u53ef\u7528\u4e8e\u9009\u62e9\u591a\u4e2a\u4f4d\u3002\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u7a0d\u5fae\u590d\u6742\u4e00\u70b9\u7684\u4f8b\u5b50\u3002 \u7535\u89c6\u673a\u3001\u663e\u793a\u5668\u7b49\u5f69\u8272\u663e\u793a\u8bbe\u5907\u7531\u6570\u767e\u4e07\u4e2a\u50cf\u7d20\u7ec4\u6210\uff0c\u6bcf\u4e2a\u50cf\u7d20\u90fd\u53ef\u4ee5\u663e\u793a\u4e00\u4e2a\u989c\u8272\u7684\u70b9\u3002\u989c\u8272\u70b9\u7531\u4e09\u675f\u5149\u7ec4\u6210\uff1a\u4e00\u675f\u7ea2\u8272\u3001\u4e00\u675f\u7eff\u8272\u548c\u4e00\u675f\u84dd\u8272 (RGB)\u3002\u901a\u8fc7\u6539\u53d8\u989c\u8272\u7684\u5f3a\u5ea6\uff0c\u53ef\u4ee5\u5236\u6210\u8272\u8c31\u4e0a\u7684\u4efb\u4f55\u989c\u8272\u3002\u901a\u5e38\uff0c\u7ed9\u5b9a\u50cf\u7d20\u7684 R\u3001G \u548c B \u7684\u6570\u91cf\u7531 8 \u4f4d\u65e0\u7b26\u53f7\u6574\u6570\u8868\u793a\u3002\u4f8b\u5982\uff0c\u7ea2\u8272\u50cf\u7d20\u7684 R=255\u3001G=0\u3001B=0\u3002\u7d2b\u8272\u50cf\u7d20\u7684 R=255\u3001G=0\u3001B=255\u3002\u4e2d\u7070\u8272\u50cf\u7d20\u5c06\u5177\u6709 R=127\u3001G=127\u3001B=127\u3002 \u5728\u4e3a\u50cf\u7d20\u5206\u914d\u989c\u8272\u503c\u65f6\uff0c\u9664\u4e86 R\u3001G \u548c B \u4e4b\u5916\uff0c\u901a\u5e38\u8fd8\u4f1a\u4f7f\u7528\u79f0\u4e3a A \u7684\u7b2c 4 \u4e2a\u503c\u3002\u201cA\u201d\u4ee3\u8868\u201calpha\u201d\uff0c\u5b83\u63a7\u5236\u989c\u8272\u7684\u900f\u660e\u5ea6\u3002\u5982\u679c A=0\uff0c\u989c\u8272\u662f\u5b8c\u5168\u900f\u660e\u7684\u3002\u5982\u679c A=255\uff0c\u5219\u989c\u8272\u4e0d\u900f\u660e\u3002 R\u3001G\u3001B \u548c A \u901a\u5e38\u5b58\u50a8\u4e3a\u5355\u4e2a 32 \u4f4d\u6574\u6570\uff0c\u6bcf\u4e2a\u5206\u91cf\u4f7f\u7528 8 \u4f4d\uff1a 32 \u4f4d RGBA \u503c 31-24 \u4f4d 23-16 \u4f4d 15-8 \u4f4d 7-0 \u4f4d RRRRRRRR GGGGGGGG BBBBBBBB AAAAAAAA red green blue alpha \u4e0b\u9762\u7684\u7a0b\u5e8f\u8981\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a 32 \u4f4d\u7684\u5341\u516d\u8fdb\u5236\u503c\uff0c\u7136\u540e\u63d0\u53d6 R\u3001G\u3001B \u548c A \u7684 8 \u4f4d\u989c\u8272\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include #include int main () { constexpr std :: uint32_t redBits { 0xFF000000 }; constexpr std :: uint32_t greenBits { 0x00FF0000 }; constexpr std :: uint32_t blueBits { 0x0000FF00 }; constexpr std :: uint32_t alphaBits { 0x000000FF }; std :: cout << \"Enter a 32-bit RGBA color value in hexadecimal (e.g. FF7F3300): \" ; std :: uint32_t pixel {}; std :: cin >> std :: hex >> pixel ; // std::hex allows us to read in a hex value // use Bitwise AND to isolate red pixels, // then right shift the value into the lower 8 bits std :: uint8_t red { static_cast < std :: uint8_t > (( pixel & redBits ) >> 24 ) }; std :: uint8_t green { static_cast < std :: uint8_t > (( pixel & greenBits ) >> 16 ) }; std :: uint8_t blue { static_cast < std :: uint8_t > (( pixel & blueBits ) >> 8 ) }; std :: uint8_t alpha { static_cast < std :: uint8_t > ( pixel & alphaBits ) }; std :: cout << \"Your color contains: \\n \" ; std :: cout << std :: hex ; // print the following values in hex std :: cout << static_cast < int > ( red ) << \" red \\n \" ; std :: cout << static_cast < int > ( green ) << \" green \\n \" ; std :: cout << static_cast < int > ( blue ) << \" blue \\n \" ; std :: cout << static_cast < int > ( alpha ) << \" alpha \\n \" ; return 0 ; } \u8fd9\u4f1a\u4ea7\u751f\u8f93\u51fa\uff1a 1 2 3 4 5 6 Enter a 32-bit RGBA color value in hexadecimal (e.g. FF7F3300): FF7F3300 Your color contains: ff red 7f green 33 blue 0 alpha \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u4e0e \u6765\u67e5\u8be2\u6211\u4eec\u611f\u5174\u8da3\u7684 8 \u4f4d\u96c6\u5408\uff0c\u7136\u540e\u5c06\u5b83\u4eec \u53f3 \u79fb\u5230\u4e00\u4e2a 8 \u4f4d\u503c\u4e2d\uff0c\u4ee5\u4fbf\u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u4eec\u4f5c\u4e3a\u5341\u516d\u8fdb\u5236\u503c\u6253\u5370\u56de\u6765\u3002 \u6982\u62ec \u603b\u7ed3\u5982\u4f55\u8bbe\u7f6e\u3001\u6e05\u9664\u3001\u5207\u6362\u548c\u67e5\u8be2\u4f4d\u6807\u5fd7\uff1a \u8981\u67e5\u8be2\u4f4d\u72b6\u6001\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u4e0e \uff1a 1 if ( flags & option4 ) ... // if option4 is set, do something \u8981\u8bbe\u7f6e\u4f4d\uff08\u6253\u5f00\uff09\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u6216 \uff1a 1 2 flags |= option4 ; // turn option 4 on. flags |= ( option4 | option5 ); // turn options 4 and 5 on. \u8981\u6e05\u9664\u4f4d\uff08\u5173\u95ed\uff09\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d AND \u548c \u6309\u4f4d NOT \uff1a 1 2 flags &= ~ option4 ; // turn option 4 off flags &= ~ ( option4 | option5 ); // turn options 4 and 5 off \u8981\u7ffb\u8f6c\u4f4d\u72b6\u6001\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u5f02\u6216 \uff1a 1 2 flags ^= option4 ; // flip option4 from on to off, or vice versa flags ^= ( option4 | option5 ); // flip options 4 and 5","title":"O.3 - \u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u548c\u63a9\u7801\u64cd\u4f5c\u4f4d"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#_1","text":"\u4e3a\u4e86\u64cd\u4f5c\u5355\u4e2a\u4f4d\uff08\u4f8b\u5982\u6253\u5f00\u6216\u5173\u95ed\u5b83\u4eec\uff09\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e9b\u65b9\u6cd5\u6765\u8bc6\u522b\u6211\u4eec\u60f3\u8981\u64cd\u4f5c\u7684\u7279\u5b9a\u4f4d\u3002\u4e0d\u5e78\u7684\u662f\uff0c\u6309\u4f4d\u8fd0\u7b97\u7b26\u4e0d\u77e5\u9053\u5982\u4f55\u5904\u7406\u4f4d\u4f4d\u7f6e\u3002\u76f8\u53cd\uff0c\u4ed6\u4eec\u4f7f\u7528\u4f4d\u63a9\u7801\u3002 \u4f4d\u63a9\u7801 \u662f\u4e00\u7ec4\u9884\u5b9a\u4e49\u7684\u4f4d\uff0c\u7528\u4e8e\u9009\u62e9\u54ea\u4e9b\u7279\u5b9a\u4f4d\u5c06\u88ab\u540e\u7eed\u64cd\u4f5c\u4fee\u6539\u3002 \u8003\u8651\u4e00\u4e2a\u771f\u5b9e\u7684\u6848\u4f8b\uff0c\u60a8\u60f3\u8981\u7ed8\u5236\u4e00\u4e2a\u7a97\u6846\u3002\u5982\u679c\u60a8\u4e0d\u5c0f\u5fc3\uff0c\u60a8\u4e0d\u4ec5\u4f1a\u5192\u7740\u7c89\u5237\u7a97\u6846\u7684\u98ce\u9669\uff0c\u8fd8\u4f1a\u5192\u7740\u7c89\u5237\u73bb\u7483\u672c\u8eab\u7684\u98ce\u9669\u3002\u60a8\u53ef\u4ee5\u8d2d\u4e70\u4e00\u4e9b\u906e\u853d\u80f6\u5e26\u5e76\u5c06\u5176\u8d34\u5728\u73bb\u7483\u548c\u60a8\u4e0d\u60f3\u6d82\u6f06\u7684\u4efb\u4f55\u5176\u4ed6\u90e8\u4ef6\u4e0a\u3002\u7136\u540e\uff0c\u5f53\u60a8\u7ed8\u753b\u65f6\uff0c\u906e\u853d\u80f6\u5e26\u4f1a\u963b\u6b62\u6cb9\u6f06\u63a5\u89e6\u5230\u60a8\u4e0d\u60f3\u6d82\u6f06\u7684\u4efb\u4f55\u4e1c\u897f\u3002\u6700\u540e\uff0c\u53ea\u6709\u672a\u906e\u7f69\u7684\u90e8\u5206\uff08\u60a8\u8981\u7ed8\u5236\u7684\u90e8\u5206\uff09\u88ab\u7ed8\u5236\u3002 \u4f4d\u63a9\u7801\u672c\u8d28\u4e0a\u5bf9\u4f4d\u6267\u884c\u76f8\u540c\u7684\u529f\u80fd\u2014\u2014 \u4f4d\u63a9\u7801\u963b\u6b62\u6309\u4f4d\u8fd0\u7b97\u7b26\u63a5\u89e6\u6211\u4eec\u4e0d\u60f3\u4fee\u6539\u7684\u4f4d\uff0c\u5e76\u5141\u8bb8\u8bbf\u95ee\u6211\u4eec\u786e\u5b9e\u60f3\u8981\u4fee\u6539\u7684\u4f4d\u3002 \u8ba9\u6211\u4eec\u9996\u5148\u63a2\u8ba8\u5982\u4f55\u5b9a\u4e49\u4e00\u4e9b\u7b80\u5355\u7684\u4f4d\u63a9\u7801\uff0c\u7136\u540e\u6211\u4eec\u5c06\u5411\u60a8\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u5b83\u4eec\u3002","title":"\u4f4d\u63a9\u7801"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#c14","text":"\u6700\u7b80\u5355\u7684\u4e00\u7ec4\u4f4d\u63a9\u7801\u662f\u4e3a\u6bcf\u4e2a\u4f4d\u4f4d\u7f6e\u5b9a\u4e49\u4e00\u4e2a\u4f4d\u63a9\u7801\u3002\u6211\u4eec\u7528 0 \u6765\u5c4f\u853d\u6211\u4eec\u4e0d\u5173\u5fc3\u7684\u4f4d\uff0c\u7528 1 \u6765\u8868\u793a\u6211\u4eec\u60f3\u8981\u4fee\u6539\u7684\u4f4d\u3002 \u5c3d\u7ba1\u4f4d\u63a9\u7801\u53ef\u4ee5\u662f\u6587\u5b57\uff0c\u4f46\u5b83\u4eec\u901a\u5e38\u88ab\u5b9a\u4e49\u4e3a\u7b26\u53f7\u5e38\u91cf\uff0c\u56e0\u6b64\u53ef\u4ee5\u4e3a\u5b83\u4eec\u6307\u5b9a\u4e00\u4e2a\u6709\u610f\u4e49\u7684\u540d\u79f0\u5e76\u6613\u4e8e\u91cd\u7528\u3002 \u56e0\u4e3a C++14 \u652f\u6301\u4e8c\u8fdb\u5236\u6587\u5b57\uff0c\u6240\u4ee5\u5b9a\u4e49\u8fd9\u4e9b\u4f4d\u63a9\u7801\u5f88\u5bb9\u6613\uff1a 1 2 3 4 5 6 7 8 9 10 #include constexpr std :: uint8_t mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: uint8_t mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: uint8_t mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: uint8_t mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: uint8_t mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: uint8_t mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: uint8_t mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: uint8_t mask7 { 0b1000'0000 }; // represents bit 7 \u73b0\u5728\u6211\u4eec\u6709\u4e00\u7ec4\u4ee3\u8868\u6bcf\u4e2a\u4f4d\u4f4d\u7f6e\u7684\u7b26\u53f7\u5e38\u91cf\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u6765\u64cd\u4f5c\u4f4d\uff08\u7a0d\u540e\u6211\u4eec\u5c06\u5c55\u793a\u5982\u4f55\u64cd\u4f5c\uff09\u3002","title":"\u5728 C++14 \u4e2d\u5b9a\u4e49\u4f4d\u63a9\u7801"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#c11","text":"\u56e0\u4e3a C++11 \u4e0d\u652f\u6301\u4e8c\u8fdb\u5236\u6587\u5b57 \uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u6765\u8bbe\u7f6e\u7b26\u53f7\u5e38\u91cf\u3002\u6709\u4e24\u79cd\u5f88\u597d\u7684\u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\u3002 \u7b2c\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u5341\u516d\u8fdb\u5236\u6587\u5b57\u3002 \u76f8\u5173\u5185\u5bb9 \u6211\u4eec\u5728 4.15-\u5b57\u9762\u91cf \u4e2d\u8ba8\u8bba\u4e86\u5341\u516d\u8fdb\u5236\u3002 \u5341\u516d\u8fdb\u5236\u8f6c\u4e8c\u8fdb\u5236\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a \u5341\u516d\u8fdb\u5236 0 1 2 3 4 5 6 7 8 9 A B C D E F \u4e8c\u8fdb\u5236 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u50cf\u8fd9\u6837\u4f7f\u7528\u5341\u516d\u8fdb\u5236\u5b9a\u4e49\u4f4d\u63a9\u7801\uff1a 1 2 3 4 5 6 7 8 constexpr std :: uint8_t mask0 { 0x01 }; // hex for 0000 0001 constexpr std :: uint8_t mask1 { 0x02 }; // hex for 0000 0010 constexpr std :: uint8_t mask2 { 0x04 }; // hex for 0000 0100 constexpr std :: uint8_t mask3 { 0x08 }; // hex for 0000 1000 constexpr std :: uint8_t mask4 { 0x10 }; // hex for 0001 0000 constexpr std :: uint8_t mask5 { 0x20 }; // hex for 0010 0000 constexpr std :: uint8_t mask6 { 0x40 }; // hex for 0100 0000 constexpr std :: uint8_t mask7 { 0x80 }; // hex for 1000 0000 \u6709\u65f6\u4f1a\u7701\u7565\u524d\u5bfc\u7684\u5341\u516d\u8fdb\u5236 0\uff08\u4f8b\u5982\uff0c 0x01 \u60a8\u53ea\u4f1a\u770b\u5230 0x1 \uff09\u3002\u65e0\u8bba\u54ea\u79cd\u65b9\u5f0f\uff0c\u5982\u679c\u60a8\u4e0d\u719f\u6089\u5341\u516d\u8fdb\u5236\u5230\u4e8c\u8fdb\u5236\u7684\u8f6c\u6362\uff0c\u8fd9\u53ef\u80fd\u6709\u70b9\u96be\u4ee5\u9605\u8bfb\u3002 \u4e00\u79cd\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5de6\u79fb\u8fd0\u7b97\u7b26\u5c06\u4e00\u4f4d\u79fb\u52a8\u5230\u6b63\u786e\u7684\u4f4d\u7f6e\uff1a 1 2 3 4 5 6 7 8 constexpr std :: uint8_t mask0 { 1 << 0 }; // 0000 0001 constexpr std :: uint8_t mask1 { 1 << 1 }; // 0000 0010 constexpr std :: uint8_t mask2 { 1 << 2 }; // 0000 0100 constexpr std :: uint8_t mask3 { 1 << 3 }; // 0000 1000 constexpr std :: uint8_t mask4 { 1 << 4 }; // 0001 0000 constexpr std :: uint8_t mask5 { 1 << 5 }; // 0010 0000 constexpr std :: uint8_t mask6 { 1 << 6 }; // 0100 0000 constexpr std :: uint8_t mask7 { 1 << 7 }; // 1000 0000","title":"\u5728 C++11 \u6216\u66f4\u65e9\u7248\u672c\u4e2d\u5b9a\u4e49\u4f4d\u63a9\u7801"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#10","text":"\u73b0\u5728\u6211\u4eec\u6709\u4e86\u4e00\u7ec4\u4f4d\u63a9\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u4eec\u4e0e\u4f4d\u6807\u5fd7\u53d8\u91cf\u7ed3\u5408\u4f7f\u7528\u6765\u64cd\u4f5c\u6211\u4eec\u7684\u4f4d\u6807\u5fd7\u3002 \u8981\u786e\u5b9a\u67d0\u4e2a\u4f4d\u662f\u5f00\u8fd8\u662f\u5173\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u4e0e \u7ed3\u5408\u76f8\u5e94\u4f4d\u7684\u4f4d\u63a9\u7801\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include int main () { constexpr std :: uint8_t mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: uint8_t mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: uint8_t mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: uint8_t mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: uint8_t mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: uint8_t mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: uint8_t mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: uint8_t mask7 { 0b1000'0000 }; // represents bit 7 std :: uint8_t flags { 0b0000'0101 }; // 8 bits in size means room for 8 flags std :: cout << \"bit 0 is \" << (( flags & mask0 ) ? \"on \\n \" : \"off \\n \" ); std :: cout << \"bit 1 is \" << (( flags & mask1 ) ? \"on \\n \" : \"off \\n \" ); return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 bit 1 is off bit 1 is on","title":"\u6d4b\u8bd5\u4e00\u4e0b\uff08\u770b\u5b83\u662f1\u8fd8\u662f0\uff09"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#1","text":"\u8981\u8bbe\u7f6e\uff08\u7f6e1\uff09\u4f4d\uff0c\u6211\u4eec\u5c06\u6309\u4f4d\u6216\u7b49\u4e8e\uff08\u8fd0\u7b97\u7b26 |=\uff09\u4e0e\u76f8\u5e94\u4f4d\u7684\u4f4d\u63a9\u7801\u7ed3\u5408\u4f7f\u7528\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include int main () { constexpr std :: uint8_t mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: uint8_t mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: uint8_t mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: uint8_t mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: uint8_t mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: uint8_t mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: uint8_t mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: uint8_t mask7 { 0b1000'0000 }; // represents bit 7 std :: uint8_t flags { 0b0000'0101 }; // 8 bits in size means room for 8 flags std :: cout << \"bit 1 is \" << (( flags & mask1 ) ? \"on \\n \" : \"off \\n \" ); flags |= mask1 ; // turn on bit 1 std :: cout << \"bit 1 is \" << (( flags & mask1 ) ? \"on \\n \" : \"off \\n \" ); return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 bit 1 is off bit 1 is on \u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6309\u4f4d\u6216 \u540c\u65f6\u7f6e1\u591a\u4e2a\u4f4d\uff1a 1 flags |= ( mask4 | mask5 ); // turn bits 4 and 5 on at the same time","title":"\u7f6e1\u4e00\u4f4d"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#0","text":"\u8981\u6e05\u9664\u4f4d\uff08\u7f6e0\uff09\uff0c\u6211\u4eec\u540c\u65f6\u4f7f\u7528 \u6309\u4f4d\u4e0e \u548c \u6309\u4f4d\u975e \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include int main () { constexpr std :: uint8_t mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: uint8_t mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: uint8_t mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: uint8_t mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: uint8_t mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: uint8_t mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: uint8_t mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: uint8_t mask7 { 0b1000'0000 }; // represents bit 7 std :: uint8_t flags { 0b0000'0101 }; // 8 bits in size means room for 8 flags std :: cout << \"bit 2 is \" << (( flags & mask2 ) ? \"on \\n \" : \"off \\n \" ); flags &= ~ mask2 ; // turn off bit 2 std :: cout << \"bit 2 is \" << (( flags & mask2 ) ? \"on \\n \" : \"off \\n \" ); return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 bit 2 is on bit 2 is off \u6211\u4eec\u53ef\u4ee5\u540c\u65f6\u5173\u95ed\u591a\u4e2a\u4f4d\uff1a 1 flags &= ~ ( mask4 | mask5 ); // turn bits 4 and 5 off at the same time","title":"\u7f6e0\u4e00\u4f4d"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#_2","text":"\u8981\u5207\u6362\u4f4d\u72b6\u6001\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u5f02\u6216 \uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include int main () { constexpr std :: uint8_t mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: uint8_t mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: uint8_t mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: uint8_t mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: uint8_t mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: uint8_t mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: uint8_t mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: uint8_t mask7 { 0b1000'0000 }; // represents bit 7 std :: uint8_t flags { 0b0000'0101 }; // 8 bits in size means room for 8 flags std :: cout << \"bit 2 is \" << (( flags & mask2 ) ? \"on \\n \" : \"off \\n \" ); flags ^= mask2 ; // flip bit 2 std :: cout << \"bit 2 is \" << (( flags & mask2 ) ? \"on \\n \" : \"off \\n \" ); flags ^= mask2 ; // flip bit 2 std :: cout << \"bit 2 is \" << (( flags & mask2 ) ? \"on \\n \" : \"off \\n \" ); return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 3 bit 2 is on bit 2 is off bit 2 is on \u6211\u4eec\u53ef\u4ee5\u540c\u65f6\u7ffb\u8f6c\u591a\u4e2a\u4f4d\uff1a 1 flags ^= ( mask4 | mask5 ); // flip bits 4 and 5 at the same time","title":"\u7ffb\u8f6c\u4e00\u4f4d"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#stdbitset","text":"std::bitset \u652f\u6301\u5168\u5957\u4f4d\u8fd0\u7b97\u7b26\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u4f7f\u7528\u51fd\u6570\uff08\u6d4b\u8bd5\u3001\u8bbe\u7f6e\u3001\u91cd\u7f6e\u548c\u7ffb\u8f6c\uff09\u4fee\u6539\u5355\u4e2a\u4f4d\u66f4\u5bb9\u6613\uff0c\u4f46\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4f7f\u7528\u6309\u4f4d\u8fd0\u7b97\u7b26\u548c\u4f4d\u63a9\u7801\u3002 \u4f60\u4e3a\u4ec0\u4e48\u8981\u8fd9\u6837\u505a\uff1f \u8fd9\u4e9b\u51fd\u6570test(),set(),reset(),flip()\u53ea\u5141\u8bb8\u60a8\u4fee\u6539\u5355\u4e2a\u4f4d\u3002\u6309\u4f4d\u8fd0\u7b97\u7b26\u5141\u8bb8\u60a8\u4e00\u6b21\u4fee\u6539\u591a\u4e2a\u4f4d\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include #include #include int main () { constexpr std :: bitset < 8 > mask0 { 0b0000'0001 }; // represents bit 0 constexpr std :: bitset < 8 > mask1 { 0b0000'0010 }; // represents bit 1 constexpr std :: bitset < 8 > mask2 { 0b0000'0100 }; // represents bit 2 constexpr std :: bitset < 8 > mask3 { 0b0000'1000 }; // represents bit 3 constexpr std :: bitset < 8 > mask4 { 0b0001'0000 }; // represents bit 4 constexpr std :: bitset < 8 > mask5 { 0b0010'0000 }; // represents bit 5 constexpr std :: bitset < 8 > mask6 { 0b0100'0000 }; // represents bit 6 constexpr std :: bitset < 8 > mask7 { 0b1000'0000 }; // represents bit 7 std :: bitset < 8 > flags { 0b0000'0101 }; // 8 bits in size means room for 8 flags std :: cout << \"bit 1 is \" << ( flags . test ( 1 ) ? \"on \\n \" : \"off \\n \" ); std :: cout << \"bit 2 is \" << ( flags . test ( 2 ) ? \"on \\n \" : \"off \\n \" ); flags ^= ( mask1 | mask2 ); // flip bits 1 and 2 std :: cout << \"bit 1 is \" << ( flags . test ( 1 ) ? \"on \\n \" : \"off \\n \" ); std :: cout << \"bit 2 is \" << ( flags . test ( 2 ) ? \"on \\n \" : \"off \\n \" ); flags |= ( mask1 | mask2 ); // turn bits 1 and 2 on std :: cout << \"bit 1 is \" << ( flags . test ( 1 ) ? \"on \\n \" : \"off \\n \" ); std :: cout << \"bit 2 is \" << ( flags . test ( 2 ) ? \"on \\n \" : \"off \\n \" ); flags &= ~ ( mask1 | mask2 ); // turn bits 1 and 2 off std :: cout << \"bit 1 is \" << ( flags . test ( 1 ) ? \"on \\n \" : \"off \\n \" ); std :: cout << \"bit 2 is \" << ( flags . test ( 2 ) ? \"on \\n \" : \"off \\n \" ); return 0 ; } \u8fd9\u6253\u5370\uff1a 1 2 3 4 5 6 7 8 bit 1 is off bit 2 is on bit 1 is on bit 2 is off bit 1 is on bit 2 is on bit 1 is off bit 2 is off","title":"\u4f4d\u63a9\u7801\u548c std::bitset"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#_3","text":"\u5c06\u6211\u4eec\u7684\u4f4d\u63a9\u7801\u547d\u540d\u4e3a\u201cmask1\u201d\u6216\u201cmask2\u201d\u53ef\u4ee5\u544a\u8bc9\u6211\u4eec\u6b63\u5728\u64cd\u4f5c\u7684\u662f\u54ea\u4e2a\u4f4d\uff0c\u4f46\u4e0d\u4f1a\u544a\u8bc9\u6211\u4eec\u8be5\u4f4d\u6807\u5fd7\u5b9e\u9645\u7528\u4e8e\u4ec0\u4e48\u3002 \u6700\u4f73\u505a\u6cd5\u662f\u4e3a\u60a8\u7684\u4f4d\u63a9\u7801\u63d0\u4f9b\u6709\u7528\u7684\u540d\u79f0\uff0c\u4f5c\u4e3a\u8bb0\u5f55\u4f4d\u6807\u5fd7\u542b\u4e49\u7684\u4e00\u79cd\u65b9\u5f0f\u3002\u8fd9\u662f\u6211\u4eec\u53ef\u80fd\u7f16\u5199\u7684\u6e38\u620f\u7684\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include int main () { // Define a bunch of physical/emotional states constexpr std :: uint8_t isHungry { 1 << 0 }; // 0000 0001 constexpr std :: uint8_t isSad { 1 << 1 }; // 0000 0010 constexpr std :: uint8_t isMad { 1 << 2 }; // 0000 0100 constexpr std :: uint8_t isHappy { 1 << 3 }; // 0000 1000 constexpr std :: uint8_t isLaughing { 1 << 4 }; // 0001 0000 constexpr std :: uint8_t isAsleep { 1 << 5 }; // 0010 0000 constexpr std :: uint8_t isDead { 1 << 6 }; // 0100 0000 constexpr std :: uint8_t isCrying { 1 << 7 }; // 1000 0000 std :: uint8_t me {}; // all flags/options turned off to start me |= ( isHappy | isLaughing ); // I am happy and laughing me &= ~ isLaughing ; // I am no longer laughing // Query a few states // (we'll use static_cast to interpret the results as a boolean value) std :: cout << \"I am happy? \" << static_cast < bool > ( me & isHappy ) << '\\n' ; std :: cout << \"I am laughing? \" << static_cast < bool > ( me & isLaughing ) << '\\n' ; return 0 ; } \u4e0b\u9762\u662f\u4f7f\u7528 std::bitset \u5b9e\u73b0\u7684\u76f8\u540c\u793a\u4f8b\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include int main () { // Define a bunch of physical/emotional states constexpr std :: bitset < 8 > isHungry { 0b0000'0001 }; constexpr std :: bitset < 8 > isSad { 0b0000'0010 }; constexpr std :: bitset < 8 > isMad { 0b0000'0100 }; constexpr std :: bitset < 8 > isHappy { 0b0000'1000 }; constexpr std :: bitset < 8 > isLaughing { 0b0001'0000 }; constexpr std :: bitset < 8 > isAsleep { 0b0010'0000 }; constexpr std :: bitset < 8 > isDead { 0b0100'0000 }; constexpr std :: bitset < 8 > isCrying { 0b1000'0000 }; std :: bitset < 8 > me {}; // all flags/options turned off to start me |= ( isHappy | isLaughing ); // I am happy and laughing me &= ~ isLaughing ; // I am no longer laughing // Query a few states (we use the any() function to see if any bits remain set) std :: cout << \"I am happy? \" << ( me & isHappy ). any () << '\\n' ; std :: cout << \"I am laughing? \" << ( me & isLaughing ). any () << '\\n' ; return 0 ; } \u8fd9\u91cc\u6709\u4e24\u4e2a\u6ce8\u610f\u4e8b\u9879\uff1a\u9996\u5148\uff0cstd::bitset \u6ca1\u6709\u5141\u8bb8\u60a8\u4f7f\u7528\u4f4d\u63a9\u7801\u67e5\u8be2\u4f4d\u7684\u597d\u51fd\u6570\u3002\u56e0\u6b64\uff0c\u5982\u679c\u60a8\u60f3\u4f7f\u7528\u4f4d\u63a9\u7801\u800c\u4e0d\u662f\u4f4d\u7f6e\u7d22\u5f15\uff0c\u5219\u5fc5\u987b\u4f7f\u7528 \u6309\u4f4d\u4e0e \u67e5\u8be2\u4f4d\u3002\u5176\u6b21\uff0c\u6211\u4eec\u4f7f\u7528 any() \u51fd\u6570\uff0c\u5982\u679c\u8bbe\u7f6e\u4e86\u4efb\u4f55\u4f4d\uff0c\u8be5\u51fd\u6570\u8fd4\u56de true\uff0c\u5426\u5219\u8fd4\u56de false\uff0c\u4ee5\u67e5\u770b\u6211\u4eec\u67e5\u8be2\u7684\u4f4d\u662f\u5426\u4fdd\u6301\u6253\u5f00\u6216\u5173\u95ed\u3002","title":"\u4f7f\u4f4d\u63a9\u7801\u6709\u610f\u4e49"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#_4","text":"\u7ec6\u5fc3\u7684\u8bfb\u8005\u53ef\u80fd\u4f1a\u6ce8\u610f\u5230\uff0c\u4e0a\u9762\u7684\u793a\u4f8b\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\u8282\u7701\u4efb\u4f55\u5185\u5b58\u30028 \u4e2a\u5e03\u5c14\u503c\u901a\u5e38\u9700\u8981 8 \u4e2a\u5b57\u8282\u3002\u4f46\u662f\u4e0a\u9762\u7684\u4f8b\u5b50\u4f7f\u7528\u4e86 9 \u4e2a\u5b57\u8282\uff088 \u4e2a\u5b57\u8282\u7528\u4e8e\u5b9a\u4e49\u4f4d\u63a9\u7801\uff0c1 \u4e2a\u5b57\u8282\u7528\u4e8e\u6807\u5fd7\u53d8\u91cf\uff09\uff01 \u5f53\u60a8\u6709\u8bb8\u591a\u76f8\u540c\u7684\u6807\u5fd7\u53d8\u91cf\u65f6\uff0c\u4f4d\u6807\u5fd7\u6700\u6709\u610f\u4e49\u3002\u4f8b\u5982\uff0c\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5047\u8bbe\u60a8\u6709 100 \u4e2a\u800c\u4e0d\u662f\u4e00\u4e2a\u4eba\uff08\u6211\uff09\u3002\u5982\u679c\u60a8\u6bcf\u4e2a\u4eba\u4f7f\u7528 8 \u4e2a\u5e03\u5c14\u503c\uff08\u6bcf\u4e2a\u53ef\u80fd\u7684\u72b6\u6001\u4e00\u4e2a\uff09\uff0c\u60a8\u5c06\u4f7f\u7528 800 \u4e2a\u5b57\u8282\u7684\u5185\u5b58\u3002\u5bf9\u4e8e\u4f4d\u6807\u5fd7\uff0c\u60a8\u5c06\u4f7f\u7528 8 \u4e2a\u5b57\u8282\u4f5c\u4e3a\u4f4d\u63a9\u7801\uff0c\u4f7f\u7528 100 \u4e2a\u5b57\u8282\u4f5c\u4e3a\u4f4d\u6807\u5fd7\u53d8\u91cf\uff0c\u603b\u5171\u9700\u8981 108 \u4e2a\u5b57\u8282\u7684\u5185\u5b58\u2014\u2014\u5927\u7ea6\u51cf\u5c11 8 \u500d\u7684\u5185\u5b58\u3002 \u5bf9\u4e8e\u5927\u591a\u6570\u7a0b\u5e8f\uff0c\u4f7f\u7528\u4f4d\u6807\u5fd7\u8282\u7701\u7684\u5185\u5b58\u91cf\u4e0d\u503c\u5f97\u589e\u52a0\u590d\u6742\u6027\u3002\u4f46\u662f\u5728\u6709\u6570\u4e07\u751a\u81f3\u6570\u767e\u4e07\u4e2a\u76f8\u4f3c\u5bf9\u8c61\u7684\u7a0b\u5e8f\u4e2d\uff0c\u4f7f\u7528\u4f4d\u6807\u5fd7\u53ef\u4ee5\u5927\u5927\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u3002\u5982\u679c\u9700\u8981\uff0c\u5c06\u5176\u5305\u542b\u5728\u60a8\u7684\u5de5\u5177\u5305\u4e2d\u662f\u4e00\u9879\u6709\u7528\u7684\u4f18\u5316\u3002 \u5728\u53e6\u4e00\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f4d\u6807\u5fd7\u548c\u4f4d\u63a9\u7801\u662f\u6709\u610f\u4e49\u7684\u3002\u60f3\u8c61\u4e00\u4e0b\uff0c\u60a8\u6709\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u91c7\u7528 32 \u79cd\u4e0d\u540c\u9009\u9879\u7684\u4efb\u610f\u7ec4\u5408\u3002\u7f16\u5199\u8be5\u51fd\u6570\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 32 \u4e2a\u5355\u72ec\u7684\u5e03\u5c14\u53c2\u6570\uff1a 1 void someFunction ( bool option1 , bool option2 , bool option3 , bool option4 , bool option5 , bool option6 , bool option7 , bool option8 , bool option9 , bool option10 , bool option11 , bool option12 , bool option13 , bool option14 , bool option15 , bool option16 , bool option17 , bool option18 , bool option19 , bool option20 , bool option21 , bool option22 , bool option23 , bool option24 , bool option25 , bool option26 , bool option27 , bool option28 , bool option29 , bool option30 , bool option31 , bool option32 ); \u5e0c\u671b\u4f60\u80fd\u7ed9\u4f60\u7684\u53c2\u6570\u8d77\u66f4\u5177\u63cf\u8ff0\u6027\u7684\u540d\u5b57\uff0c\u4f46\u8fd9\u91cc\u7684\u91cd\u70b9\u662f\u5411\u4f60\u5c55\u793a\u53c2\u6570\u5217\u8868\u662f\u591a\u4e48\u4ee4\u4eba\u8ba8\u538c\u3002 \u7136\u540e\u5f53\u4f60\u60f3\u8c03\u7528\u9009\u9879 10 \u548c 32 \u8bbe\u7f6e\u4e3a true \u7684\u51fd\u6570\u65f6\uff0c\u4f60\u5fc5\u987b\u8fd9\u6837\u505a\uff1a 1 someFunction ( false , false , false , false , false , false , false , false , false , true , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , true ); \u8fd9\u975e\u5e38\u96be\u4ee5\u9605\u8bfb\uff08\u9009\u9879 9\u300110 \u6216 11 \u662f\u5426\u8bbe\u7f6e\u4e3a true\uff1f\uff09\uff0c\u5e76\u4e14\u8fd8\u610f\u5473\u7740\u60a8\u5fc5\u987b\u8bb0\u4f4f\u54ea\u4e2a\u53c2\u6570\u5bf9\u5e94\u4e8e\u54ea\u4e2a\u9009\u9879\uff08\u5c06\u201c\u7f16\u8f91\u6807\u5fd7\u201d\u8bbe\u7f6e\u4e3a\u7b2c 9\u300110 \u6216\u7b2c 11 \u4e2a\u53c2\u6570\uff1f\uff09\u5b83\u4e5f\u53ef\u80fd\u4e0d\u662f\u5f88\u9ad8\u6548\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u51fd\u6570\u8c03\u7528\u90fd\u5fc5\u987b\u5c06 32 \u4e2a\u5e03\u5c14\u503c\u4ece\u8c03\u7528\u8005\u590d\u5236\u5230\u51fd\u6570\u3002 \u76f8\u53cd\uff0c\u5982\u679c\u60a8\u4f7f\u7528\u8fd9\u6837\u7684\u4f4d\u6807\u5fd7\u5b9a\u4e49\u51fd\u6570\uff1a 1 void someFunction ( std :: bitset < 32 > options ); \u7136\u540e\u4f60\u53ef\u4ee5\u4f7f\u7528\u4f4d\u6807\u5fd7\u6765\u53ea\u4f20\u9012\u4f60\u60f3\u8981\u7684\u9009\u9879\uff1a 1 someFunction ( option10 | option32 ); \u8fd9\u4e0d\u4ec5\u66f4\u5177\u53ef\u8bfb\u6027\uff0c\u800c\u4e14\u6027\u80fd\u4e5f\u53ef\u80fd\u66f4\u9ad8\uff0c\u56e0\u4e3a\u5b83\u53ea\u6d89\u53ca 2 \u4e2a\u64cd\u4f5c\uff08\u4e00\u4e2a \u6309\u4f4d\u6216 \u548c\u4e00\u4e2a\u53c2\u6570\u590d\u5236\uff09\u3002 \u8fd9\u5c31\u662f\u5e7f\u53d7\u597d\u8bc4\u7684 3d \u56fe\u5f62\u5e93 OpenGL \u9009\u62e9\u4f7f\u7528\u4f4d\u6807\u5fd7\u53c2\u6570\u800c\u4e0d\u662f\u8bb8\u591a\u8fde\u7eed\u7684\u5e03\u5c14\u53c2\u6570\u7684\u539f\u56e0\u4e4b\u4e00\u3002 \u4e0b\u9762\u662f\u6765\u81ea OpenGL \u7684\u793a\u4f8b\u51fd\u6570\u8c03\u7528\uff1a 1 glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // clear the color and the depth buffer GL_COLOR_BUFFER_BIT \u548c GL_DEPTH_BUFFER_BIT \u662f\u4f4d\u63a9\u7801\uff0c\u5b9a\u4e49\u5982\u4e0b\uff08\u5728 gl2.h \u4e2d\uff09\uff1a 1 2 3 #define GL_DEPTH_BUFFER_BIT 0x00000100 #define GL_STENCIL_BUFFER_BIT 0x00000400 #define GL_COLOR_BUFFER_BIT 0x00004000","title":"\u4ec0\u4e48\u65f6\u5019\u4f4d\u6807\u5fd7\u6700\u6709\u7528\uff1f"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#_5","text":"\u5c3d\u7ba1\u4f4d\u63a9\u7801\u901a\u5e38\u7528\u4e8e\u9009\u62e9\u5355\u4e2a\u4f4d\uff0c\u4f46\u5b83\u4eec\u4e5f\u53ef\u7528\u4e8e\u9009\u62e9\u591a\u4e2a\u4f4d\u3002\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u7a0d\u5fae\u590d\u6742\u4e00\u70b9\u7684\u4f8b\u5b50\u3002 \u7535\u89c6\u673a\u3001\u663e\u793a\u5668\u7b49\u5f69\u8272\u663e\u793a\u8bbe\u5907\u7531\u6570\u767e\u4e07\u4e2a\u50cf\u7d20\u7ec4\u6210\uff0c\u6bcf\u4e2a\u50cf\u7d20\u90fd\u53ef\u4ee5\u663e\u793a\u4e00\u4e2a\u989c\u8272\u7684\u70b9\u3002\u989c\u8272\u70b9\u7531\u4e09\u675f\u5149\u7ec4\u6210\uff1a\u4e00\u675f\u7ea2\u8272\u3001\u4e00\u675f\u7eff\u8272\u548c\u4e00\u675f\u84dd\u8272 (RGB)\u3002\u901a\u8fc7\u6539\u53d8\u989c\u8272\u7684\u5f3a\u5ea6\uff0c\u53ef\u4ee5\u5236\u6210\u8272\u8c31\u4e0a\u7684\u4efb\u4f55\u989c\u8272\u3002\u901a\u5e38\uff0c\u7ed9\u5b9a\u50cf\u7d20\u7684 R\u3001G \u548c B \u7684\u6570\u91cf\u7531 8 \u4f4d\u65e0\u7b26\u53f7\u6574\u6570\u8868\u793a\u3002\u4f8b\u5982\uff0c\u7ea2\u8272\u50cf\u7d20\u7684 R=255\u3001G=0\u3001B=0\u3002\u7d2b\u8272\u50cf\u7d20\u7684 R=255\u3001G=0\u3001B=255\u3002\u4e2d\u7070\u8272\u50cf\u7d20\u5c06\u5177\u6709 R=127\u3001G=127\u3001B=127\u3002 \u5728\u4e3a\u50cf\u7d20\u5206\u914d\u989c\u8272\u503c\u65f6\uff0c\u9664\u4e86 R\u3001G \u548c B \u4e4b\u5916\uff0c\u901a\u5e38\u8fd8\u4f1a\u4f7f\u7528\u79f0\u4e3a A \u7684\u7b2c 4 \u4e2a\u503c\u3002\u201cA\u201d\u4ee3\u8868\u201calpha\u201d\uff0c\u5b83\u63a7\u5236\u989c\u8272\u7684\u900f\u660e\u5ea6\u3002\u5982\u679c A=0\uff0c\u989c\u8272\u662f\u5b8c\u5168\u900f\u660e\u7684\u3002\u5982\u679c A=255\uff0c\u5219\u989c\u8272\u4e0d\u900f\u660e\u3002 R\u3001G\u3001B \u548c A \u901a\u5e38\u5b58\u50a8\u4e3a\u5355\u4e2a 32 \u4f4d\u6574\u6570\uff0c\u6bcf\u4e2a\u5206\u91cf\u4f7f\u7528 8 \u4f4d\uff1a 32 \u4f4d RGBA \u503c 31-24 \u4f4d 23-16 \u4f4d 15-8 \u4f4d 7-0 \u4f4d RRRRRRRR GGGGGGGG BBBBBBBB AAAAAAAA red green blue alpha \u4e0b\u9762\u7684\u7a0b\u5e8f\u8981\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a 32 \u4f4d\u7684\u5341\u516d\u8fdb\u5236\u503c\uff0c\u7136\u540e\u63d0\u53d6 R\u3001G\u3001B \u548c A \u7684 8 \u4f4d\u989c\u8272\u503c\u3002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include #include int main () { constexpr std :: uint32_t redBits { 0xFF000000 }; constexpr std :: uint32_t greenBits { 0x00FF0000 }; constexpr std :: uint32_t blueBits { 0x0000FF00 }; constexpr std :: uint32_t alphaBits { 0x000000FF }; std :: cout << \"Enter a 32-bit RGBA color value in hexadecimal (e.g. FF7F3300): \" ; std :: uint32_t pixel {}; std :: cin >> std :: hex >> pixel ; // std::hex allows us to read in a hex value // use Bitwise AND to isolate red pixels, // then right shift the value into the lower 8 bits std :: uint8_t red { static_cast < std :: uint8_t > (( pixel & redBits ) >> 24 ) }; std :: uint8_t green { static_cast < std :: uint8_t > (( pixel & greenBits ) >> 16 ) }; std :: uint8_t blue { static_cast < std :: uint8_t > (( pixel & blueBits ) >> 8 ) }; std :: uint8_t alpha { static_cast < std :: uint8_t > ( pixel & alphaBits ) }; std :: cout << \"Your color contains: \\n \" ; std :: cout << std :: hex ; // print the following values in hex std :: cout << static_cast < int > ( red ) << \" red \\n \" ; std :: cout << static_cast < int > ( green ) << \" green \\n \" ; std :: cout << static_cast < int > ( blue ) << \" blue \\n \" ; std :: cout << static_cast < int > ( alpha ) << \" alpha \\n \" ; return 0 ; } \u8fd9\u4f1a\u4ea7\u751f\u8f93\u51fa\uff1a 1 2 3 4 5 6 Enter a 32-bit RGBA color value in hexadecimal (e.g. FF7F3300): FF7F3300 Your color contains: ff red 7f green 33 blue 0 alpha \u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u4e0e \u6765\u67e5\u8be2\u6211\u4eec\u611f\u5174\u8da3\u7684 8 \u4f4d\u96c6\u5408\uff0c\u7136\u540e\u5c06\u5b83\u4eec \u53f3 \u79fb\u5230\u4e00\u4e2a 8 \u4f4d\u503c\u4e2d\uff0c\u4ee5\u4fbf\u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u4eec\u4f5c\u4e3a\u5341\u516d\u8fdb\u5236\u503c\u6253\u5370\u56de\u6765\u3002","title":"\u6d89\u53ca\u591a\u4e2a\u4f4d\u7684\u4f4d\u63a9\u7801"},{"location":"O-3-bit-manipulation-with-bitwise-operators-and-bit-masks/#_6","text":"\u603b\u7ed3\u5982\u4f55\u8bbe\u7f6e\u3001\u6e05\u9664\u3001\u5207\u6362\u548c\u67e5\u8be2\u4f4d\u6807\u5fd7\uff1a \u8981\u67e5\u8be2\u4f4d\u72b6\u6001\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u4e0e \uff1a 1 if ( flags & option4 ) ... // if option4 is set, do something \u8981\u8bbe\u7f6e\u4f4d\uff08\u6253\u5f00\uff09\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u6216 \uff1a 1 2 flags |= option4 ; // turn option 4 on. flags |= ( option4 | option5 ); // turn options 4 and 5 on. \u8981\u6e05\u9664\u4f4d\uff08\u5173\u95ed\uff09\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d AND \u548c \u6309\u4f4d NOT \uff1a 1 2 flags &= ~ option4 ; // turn option 4 off flags &= ~ ( option4 | option5 ); // turn options 4 and 5 off \u8981\u7ffb\u8f6c\u4f4d\u72b6\u6001\uff0c\u6211\u4eec\u4f7f\u7528 \u6309\u4f4d\u5f02\u6216 \uff1a 1 2 flags ^= option4 ; // flip option4 from on to off, or vice versa flags ^= ( option4 | option5 ); // flip options 4 and 5","title":"\u6982\u62ec"},{"location":"O-4-converting-between-binary-and-decimal/","tags":["bit"],"text":"Key Takeaway \u8003\u8651\u4e00\u4e2a\u666e\u901a\u7684\u5341\u8fdb\u5236\u6570\uff0c\u6bd4\u5982 5623\u3002\u6211\u4eec\u76f4\u89c2\u5730\u7406\u89e3\u8fd9\u4e9b\u6570\u5b57\u7684\u610f\u601d\u662f (5 * 1000) + (6 * 100) + (2 * 10) + (3 * 1)\u3002\u56e0\u4e3a\u6709 10 \u4e2a\u5341\u8fdb\u5236\u6570\uff0c\u6240\u4ee5\u5de6\u8fb9\u6bcf\u4e2a\u540e\u7eed\u6570\u5b57\u7684\u503c\u90fd\u4f1a\u589e\u52a0 10 \u500d\u3002 \u4e8c\u8fdb\u5236\u6570\u7684\u5de5\u4f5c\u65b9\u5f0f\u76f8\u540c\uff0c\u53ea\u662f\u56e0\u4e3a\u53ea\u6709 2 \u4e2a\u4e8c\u8fdb\u5236\u6570\u5b57\uff080 \u548c 1\uff09\uff0c\u6bcf\u4e2a\u6570\u5b57\u7684\u503c\u589e\u52a0 2 \u500d\u3002\u5c31\u50cf\u9017\u53f7\u901a\u5e38\u7528\u4e8e\u4f7f\u5927\u5341\u8fdb\u5236\u6570\u6613\u4e8e\u9605\u8bfb\uff08\u4f8b\u59821,427,435)\uff0c\u6211\u4eec\u7ecf\u5e38\u4ee5 4 \u4f4d\u4e3a\u4e00\u7ec4\u7f16\u5199\u4e8c\u8fdb\u5236\u6570\uff0c\u4ee5\u4fbf\u4e8e\u9605\u8bfb\uff08\u4f8b\u5982 1101 0101\uff09\u3002 \u4e0b\u8868\u4ee5\u5341\u8fdb\u5236\u548c\u4e8c\u8fdb\u5236\u8ba1\u6570\u5230 15\uff1a \u5341\u8fdb\u5236\u503c \u4e8c\u8fdb\u5236\u503c 0 0 1 1 2 10 3 11 4 100 5 101 6 110 7 111 8 1000 9 1001 10 1010 11 1011 12 1100 13 1101 14 1110 15 1111 \u5c06\u4e8c\u8fdb\u5236\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236 \u5728\u4ee5\u4e0b\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5047\u8bbe\u6211\u4eec\u6b63\u5728\u5904\u7406\u65e0\u7b26\u53f7\u6574\u6570\u3002 \u8003\u8651 8 \u4f4d\uff081 \u5b57\u8282\uff09\u4e8c\u8fdb\u5236\u6570 0101 1110\u3002 \u4e8c\u8fdb\u5236 0101 1110 \u8868\u793a (0 * 128) + (1 * 64) + (0 * 32) + (1 * 16) + (1 * 8) + (1 * 4) + (1 * 2) + (0 * 1)\u3002\u5982\u679c\u6211\u4eec\u5c06\u6240\u6709\u8fd9\u4e9b\u90e8\u5206\u76f8\u52a0\uff0c\u6211\u4eec\u5f97\u5230\u5341\u8fdb\u5236\u6570 64 + 16 + 8 + 4 + 2 = 94\u3002 \u8fd9\u662f\u8868\u683c\u683c\u5f0f\u7684\u76f8\u540c\u8fc7\u7a0b\u3002\u6211\u4eec\u5c06\u6bcf\u4e2a\u4e8c\u8fdb\u5236\u6570\u5b57\u4e58\u4ee5\u5176\u6570\u5b57\u503c\uff08\u7531\u5176\u4f4d\u7f6e\u786e\u5b9a\uff09\u3002\u5c06\u6240\u6709\u8fd9\u4e9b\u503c\u52a0\u8d77\u6765\u5c31\u662f\u603b\u6570\u3002 \u5c06 0101 1110 \u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\uff1a \u4e8c\u8fdb\u5236\u6570\u5b57 0 1 0 1 1 1 1 0 * \u6570\u5b57\u503c 128 64 32 16 8 4 2 1 = \u603b\u8ba1 (94) 0 64 0 16 8 4 2 0 \u8ba9\u6211\u4eec\u5c06 1001 0111 \u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\uff1a \u4e8c\u8fdb\u5236\u6570\u5b57 1 0 0 1 0 1 1 1 * \u6570\u5b57\u503c 128 64 32 16 8 4 2 1 = \u603b\u8ba1 (151) 128 0 0 16 0 4 2 1 1001 0111 \u4e8c\u8fdb\u5236 = \u5341\u8fdb\u5236\u7684 151\u3002 \u8fd9\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u6269\u5c55\u5230 16 \u4f4d\u6216 32 \u4f4d\u4e8c\u8fdb\u5236\u6570\uff0c\u53ea\u9700\u6dfb\u52a0\u66f4\u591a\u5217\u5373\u53ef\u3002\u8bf7\u6ce8\u610f\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4ece\u53f3\u7aef\u5f00\u59cb\uff0c\u7136\u540e\u5411\u5de6\u79fb\u52a8\uff0c\u8fb9\u8d70\u8fb9\u5c06\u6570\u5b57\u503c\u4e58\u4ee5 2\u3002 \u5341\u8fdb\u5236\u8f6c\u4e8c\u8fdb\u5236\u7684\u65b9\u6cd5\u4e00 \u4ece\u5341\u8fdb\u5236\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u6709\u70b9\u68d8\u624b\uff0c\u4f46\u4ecd\u7136\u975e\u5e38\u7b80\u5355\u3002\u6709\u4e24\u79cd\u597d\u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\u3002 \u7b2c\u4e00\u79cd\u65b9\u6cd5\u662f\u8fde\u7eed\u9664\u4ee5 2\uff0c\u7136\u540e\u8bb0\u4e0b\u4f59\u6570\u3002\u4e8c\u8fdb\u5236\u6570\u662f\u5728\u4f59\u6570\u7684\u672b\u5c3e\u6784\u9020\u7684\uff0c\u4ece\u4e0b\u5230\u4e0a\u3002 \u5c06 148 \u4ece\u5341\u8fdb\u5236\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\uff08\u4f7f\u7528 r \u8868\u793a\u4f59\u6570\uff09\uff1a 148 / 2 = 74 r0 74 / 2 = 37 r0 37 / 2 = 18 r1 18 / 2 = 9 r0 9 / 2 = 4 r1 4 / 2 = 2 r0 2 / 2 = 1 r0 1 / 2 = 0 r1 \u4ece\u4e0b\u5f80\u4e0a \u5199\u51fa\u6240\u6709\u4f59\u6570\uff1a1001 0100 148 \u5341\u8fdb\u5236 = 1001 0100 \u4e8c\u8fdb\u5236\u3002 \u60a8\u53ef\u4ee5\u901a\u8fc7\u5c06\u4e8c\u8fdb\u5236\u8f6c\u6362\u56de\u5341\u8fdb\u5236\u6765\u9a8c\u8bc1\u6b64\u7b54\u6848\uff1a (1 * 128) + (0 * 64) + (0 * 32) + (1 * 16) + (0 * 8) + (1 * 4) + (0 * 2) + (0 * 1) = 148 \u5341\u8fdb\u5236\u8f6c\u4e8c\u8fdb\u5236\u7684\u65b9\u6cd5\u4e8c \u7b2c\u4e8c\u79cd\u65b9\u6cd5\u6d89\u53ca\u9006\u5411\u8ba1\u7b97\u6bcf\u4e2a\u4f4d\u5fc5\u987b\u662f\u4ec0\u4e48\u3002\u5bf9\u4e8e\u8f83\u5c0f\u7684\u4e8c\u8fdb\u5236\u6570\uff0c\u6b64\u65b9\u6cd5\u53ef\u80fd\u66f4\u5bb9\u6613\u3002 \u518d\u6b21\u8003\u8651\u5341\u8fdb\u5236\u6570 148\u3002\u5c0f\u4e8e148\u76842\u7684\u6700\u5927\u6b21\u65b9\u662f\u591a\u5c11\uff1f128\uff0c\u6240\u4ee5\u6211\u4eec\u5c06\u4ece\u90a3\u91cc\u5f00\u59cb\u3002 \u662f 148 >= 128 \u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 128 \u4f4d\u5fc5\u987b\u662f 1. 148 - 128 = 20\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u9700\u8981\u518d\u627e\u5230\u503c 20 \u7684\u4f4d\u3002 \u662f 20 >= 64 \u5417\uff1f\u4e0d\uff0c\u6240\u4ee564\u4f4d\u5fc5\u987b\u662f0\u300220 >=32\u5417\uff1f\u4e0d\u662f\uff0c\u6240\u4ee532\u4f4d\u4e00\u5b9a\u662f0\u300220 >= 16\u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 16 \u4f4d\u5fc5\u987b\u662f 1. 20 - 16 = 4\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u9700\u8981\u627e\u5230\u66f4\u591a\u7684 4 \u4f4d\u3002 \u662f 4 >= 8 \u5417\uff1f\u4e0d\u662f\uff0c\u6240\u4ee58\u4f4d\u4e00\u5b9a\u662f0\u3002 \u662f4 >= 4\u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5\u7b2c 4 \u4f4d\u5fc5\u987b\u662f 1\u30024 - 4 = 0\uff0c\u8fd9\u610f\u5473\u7740\u6240\u6709\u5176\u4f59\u4f4d\u5fc5\u987b\u662f 0\u3002 148 = (1 * 128) + (0 * 64) + (0 * 32) + (1 * 16) + (0 * 8) + (1 * 4) + (0 * 2) + (0 * 1) = 1001 0100 \u8868\u683c\u5f62\u5f0f\uff1a \u4e8c\u8fdb\u5236\u6570 1 0 0 1 0 1 0 0 * \u6570\u5b57\u503c 128 64 32 16 8 4 2 1 = \u603b\u8ba1 (148) 128 0 0 16 0 4 0 0 \u53e6\u4e00\u4e2a\u4f8b\u5b50 \u8ba9\u6211\u4eec\u4f7f\u7528\u65b9\u6cd5 1 \u5c06 117 \u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\uff1a 117 / 2 = 58 r1 58 / 2 = 29 r0 29 / 2 = 14 r1 14 / 2 = 7 r0 7 / 2 = 3 r1 3 / 2 = 1 r1 1 / 2 = 0 r1 \u4ece\u4e0b\u5f80\u4e0a\u7684\u4f59\u6570\u6784\u9020\u6570\uff0c117 = 111 0101 \u4e8c\u8fdb\u5236 \u5e76\u4f7f\u7528\u65b9\u6cd52\uff1a 2 \u5c0f\u4e8e 117 \u7684\u6700\u5927\u6b21\u65b9\u662f 64\u3002 \u662f 117 >= 64 \u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 64 \u4f4d\u5fc5\u987b\u662f 1\u3002117 - 64 = 53\u300253 >= 32 \u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 32 \u4f4d\u5fc5\u987b\u662f 1\u300253 - 32 = 21\u300221 >= 16 \u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 16 \u4f4d\u5fc5\u987b\u662f 1\u3002 21 - 16 = 5\u3002 5 >= 8 \u5417\uff1f\u4e0d\u662f\uff0c\u6240\u4ee58\u4f4d\u4e00\u5b9a\u662f0\u3002 \u662f5 >= 4\u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5\u7b2c 4 \u4f4d\u5fc5\u987b\u662f 1\u30025 - 4 = 1\u30021 >= 2\uff1f\u4e0d\u662f\uff0c\u6240\u4ee52\u4f4d\u4e00\u5b9a\u662f0\u3002 \u662f1 >= 1\u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 1 \u4f4d\u5fc5\u987b\u662f 1\u3002 117 \u5341\u8fdb\u5236 = 111 0101 \u4e8c\u8fdb\u5236\u3002 \u76f8\u52a0\u4e8c\u8fdb\u5236\u6570 \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff08\u7a0d\u540e\u6211\u4eec\u4f1a\u770b\u5230\uff09\uff0c\u80fd\u591f\u5c06\u4e24\u4e2a\u4e8c\u8fdb\u5236\u6570\u76f8\u52a0\u662f\u5f88\u6709\u7528\u7684\u3002\u76f8\u52a0\u4e8c\u8fdb\u5236\u6570\u51fa\u5947\u5730\u5bb9\u6613\uff08\u751a\u81f3\u53ef\u80fd\u6bd4\u6dfb\u52a0\u5341\u8fdb\u5236\u6570\u66f4\u5bb9\u6613\uff09\uff0c\u5c3d\u7ba1\u4e00\u5f00\u59cb\u5b83\u53ef\u80fd\u770b\u8d77\u6765\u5f88\u5947\u602a\uff0c\u56e0\u4e3a\u60a8\u4e0d\u4e60\u60ef\u5b83\u3002 \u8003\u8651\u4e24\u4e2a\u5c0f\u7684\u4e8c\u8fdb\u5236\u6570\uff1a 0110\uff08\u5341\u8fdb\u5236\u4e3a 6\uff09+ 0111\uff08\u5341\u8fdb\u5236\u4e3a 7\uff09 \u8ba9\u6211\u4eec\u6dfb\u52a0\u8fd9\u4e9b\u3002\u9996\u5148\uff0c\u50cf\u4e0a\u9762\u4e00\u6837\u5c06\u5b83\u4eec\u6392\u6210\u4e00\u884c\u3002\u7136\u540e\uff0c\u4ece\u53f3\u5411\u5de6\u5f00\u59cb\uff0c\u6211\u4eec\u5c06\u6bcf\u4e00\u5217\u6570\u5b57\u76f8\u52a0\uff0c\u5c31\u50cf\u6211\u4eec\u5728\u5341\u8fdb\u5236\u6570\u4e2d\u6240\u505a\u7684\u90a3\u6837\u3002\u4f46\u662f\uff0c\u7531\u4e8e\u4e8c\u8fdb\u5236\u6570\u5b57\u53ea\u80fd\u662f 0 \u6216 1\uff0c\u6240\u4ee5\u53ea\u6709 4 \u79cd\u53ef\u80fd\uff1a 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 0\uff0c\u5c06 1 \u79fb\u81f3\u4e0b\u4e00\u5217 \u8ba9\u6211\u4eec\u505a\u7b2c\u4e00\u5217\uff1a 1 2 3 4 0110\uff08\u5341\u8fdb\u5236\u4e3a 6\uff09+ 0111\uff08\u5341\u8fdb\u5236\u4e3a 7\uff09 ---- 1 0 + 1 = 1\u3002\u7b80\u5355\u3002 \u7b2c\u4e8c\u680f\uff1a 1 2 3 4 5 1 0110\uff08\u5341\u8fdb\u5236\u4e3a 6\uff09+ 0111\uff08\u5341\u8fdb\u5236\u4e3a 7\uff09 ---- 01 1 + 1 = 0\uff0c\u5e26\u4e00\u4e2a\u8fdb\u4f4d\u5230\u4e0b\u4e00\u5217 \u7b2c\u4e09\u680f\uff1a 1 2 3 4 5 11 0110\uff08\u5341\u8fdb\u5236\u4e3a 6\uff09+ 0111\uff08\u5341\u8fdb\u5236\u4e3a 7\uff09 ---- 101 \u8fd9\u4e2a\u6709\u70b9\u68d8\u624b\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c1 + 1 = 0\uff0c\u5e26\u4e00\u4e2a\u8fdb\u4f4d\u5230\u4e0b\u4e00\u5217\u3002\u4f46\u662f\uff0c\u6211\u4eec\u5df2\u7ecf\u4ece\u4e0a\u4e00\u5217\u4e2d\u643a\u5e26\u4e86\u4e00\u4e2a 1\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u52a0 1\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5728\u8fd9\u4e00\u5217\u4e2d\u5f97\u5230\u4e00\u4e2a 1\uff0c\u5e76\u5c06 1 \u5e26\u5165\u4e0b\u4e00\u5217 \u6700\u540e\u4e00\u680f\uff1a 1 2 3 4 5 11 0110\uff08\u5341\u8fdb\u5236\u4e3a 6\uff09+ 0111\uff08\u5341\u8fdb\u5236\u4e3a 7\uff09 ---- 1101 0 + 0 = 0\uff0c\u4f46\u6709\u4e00\u4e2a\u8fdb\u4f4d\u7684 1\uff0c\u6240\u4ee5\u6211\u4eec\u52a0 1\u30021101 = \u5341\u8fdb\u5236\u7684 13\u3002 \u73b0\u5728\uff0c\u6211\u4eec\u5982\u4f55\u7ed9\u4efb\u4f55\u7ed9\u5b9a\u7684\u4e8c\u8fdb\u5236\u6570\uff08\u4f8b\u5982 1011 0011\uff09\u52a0 1\uff1f\u540c\u4e0a\uff0c\u53ea\u662f\u6700\u4e0b\u9762\u7684\u6570\u662f\u4e8c\u8fdb\u52361\u3002 1 2 3 4 5 1\u4e2a\uff08\u8fdb\u4f4d\u67f1\uff09 1011 0011\uff08\u539f\u59cb\u4e8c\u8fdb\u5236\u6570\uff09 0000 0001\uff08\u4e8c\u8fdb\u5236\u4e3a 1\uff09 --------- 1011 0100 \u6709\u7b26\u53f7\u6570\u548c\u8865\u7801 \u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u53ea\u5904\u7406\u4e86\u65e0\u7b26\u53f7\u6574\u6570\u3002\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4e86\u89e3\u5982\u4f55\u5904\u7406\u6709\u7b26\u53f7\u6570\uff08\u53ef\u4ee5\u4e3a\u8d1f\u6570\uff09\u3002 \u6709\u7b26\u53f7\u6574\u6570\u901a\u5e38\u4f7f\u7528\u79f0\u4e3a \u4e8c\u8fdb\u5236\u8865\u7801 \u7684\u65b9\u6cd5\u5b58\u50a8\u3002\u5728\u4e8c\u8fdb\u5236\u8865\u7801\u4e2d\uff0c\u6700\u5de6\u8fb9\uff08\u6700\u9ad8\u6709\u6548\u4f4d\uff09\u7684\u4f4d\u7528\u4f5c\u7b26\u53f7\u4f4d\u3002\u7b26\u53f7\u4f4d 0 \u8868\u793a\u6570\u5b57\u4e3a\u6b63\u6570\uff0c\u7b26\u53f7\u4f4d 1 \u8868\u793a\u6570\u5b57\u4e3a\u8d1f\u6570\u3002 \u6b63\u7b26\u53f7\u6570\u4e0e\u65e0\u7b26\u53f7\u6b63\u6570\u4e00\u6837\u4ee5\u4e8c\u8fdb\u5236\u8868\u793a\uff08\u7b26\u53f7\u4f4d\u8bbe\u7f6e\u4e3a 0\uff09\u3002 \u8d1f\u7b26\u53f7\u6570\u4ee5\u4e8c\u8fdb\u5236\u8868\u793a\u4e3a\u6b63\u6570\u7684\u6309\u4f4d\u53d6\u53cd\u52a0 1\u3002 \u5341\u8fdb\u5236\u8f6c\u4e8c\u8fdb\u5236\uff08\u4e8c\u8fdb\u5236\u8865\u7801\uff09 \u4f8b\u5982\uff0c\u8fd9\u662f\u6211\u4eec\u5982\u4f55\u7528\u4e8c\u8fdb\u5236\u4e8c\u8fdb\u5236\u8865\u7801\u8868\u793a -5\uff1a \u9996\u5148\u6211\u4eec\u627e\u51fa 5 \u7684\u4e8c\u8fdb\u5236\u8868\u793a\uff1a0000 0101 \u7136\u540e\u6211\u4eec\u53cd\u8f6c\u6240\u6709\u4f4d\uff1a1111 1010 \u7136\u540e\u6211\u4eec\u6dfb\u52a0 1\uff1a1111 1011 \u5c06 -76 \u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\uff1a \u4e8c\u8fdb\u5236\u6b63 76\uff1a0100 1100 \u53cd\u8f6c\u6240\u6709\u4f4d\uff1a1011 0011 \u52a0 1\uff1a1011 0100 \u4e3a\u4ec0\u4e48\u8981\u52a01\uff1f\u8003\u8651\u6570\u5b57 0\u3002\u5982\u679c\u5c06\u8d1f\u503c\u7b80\u5355\u5730\u8868\u793a\u4e3a\u6b63\u6570\u7684\u5012\u6570\uff0c\u5219 0 \u5c06\u6709\u4e24\u79cd\u8868\u793a\u5f62\u5f0f\uff1a0000 0000\uff08\u6b63\u96f6\uff09\u548c 1111 1111\uff08\u8d1f\u96f6\uff09\u3002\u901a\u8fc7\u52a0 1\uff0c1111 1111 \u6709\u610f\u6ea2\u51fa\u5e76\u53d8\u6210 0000 0000\u3002\u8fd9\u53ef\u4ee5\u9632\u6b62 0 \u6709\u4e24\u79cd\u8868\u793a\u5f62\u5f0f\uff0c\u5e76\u7b80\u5316\u4e86\u4e00\u4e9b\u7528\u8d1f\u6570\u8fdb\u884c\u7b97\u672f\u8fd0\u7b97\u6240\u9700\u7684\u5185\u90e8\u903b\u8f91\u3002 \u5c06\u4e8c\u8fdb\u5236\uff08\u4e8c\u8fdb\u5236\u8865\u7801\uff09\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236 \u8981\u5c06\u4e8c\u8fdb\u5236\u8865\u7801\u8f6c\u6362\u56de\u5341\u8fdb\u5236\uff0c\u9996\u5148\u8981\u67e5\u770b\u7b26\u53f7\u4f4d\u3002 \u5982\u679c\u7b26\u53f7\u4f4d\u4e3a 0\uff0c\u53ea\u9700\u6309\u7167\u4e0a\u9762\u65e0\u7b26\u53f7\u6570\u6240\u793a\u7684\u65b9\u5f0f\u8f6c\u6362\u6570\u5b57\u3002 \u5982\u679c\u7b26\u53f7\u4f4d\u662f1\uff0c\u90a3\u4e48\u6211\u4eec\u628a\u4f4d\u53d6\u53cd\uff0c\u52a01\uff0c\u7136\u540e\u8f6c\u6210\u5341\u8fdb\u5236\uff0c\u7136\u540e\u8ba9\u90a3\u4e2a\u5341\u8fdb\u5236\u6570\u53d8\u6210\u8d1f\u6570\uff08\u56e0\u4e3a\u7b26\u53f7\u4f4d\u672c\u6765\u5c31\u662f\u8d1f\u6570\uff09\u3002 \u4f8b\u5982\uff0c\u8981\u5c06 1001 1110 \u4ece\u4e8c\u8fdb\u5236\u8865\u7801\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u6570\uff1a \u7ed9\u5b9a\uff1a1001 1110 \u53cd\u8f6c\u4f4d\uff1a0110 0001 \u52a0 1\uff1a0110 0010 \u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\uff1a(0 * 128) + (1 * 64) + (1 * 32 ) + (0 * 16) + (0 * 8) + (0 * 4) + (1 * 2) + (0 * 1) = 64 + 32 + 2 = 98 \u7531\u4e8e\u539f\u59cb\u7b26\u53f7\u4f4d\u4e3a\u8d1f\uff0c\u6240\u4ee5\u6700\u540e\u7684\u503c\u662f-98\u3002 \u4e3a\u4ec0\u4e48\u7c7b\u578b\u5f88\u91cd\u8981 \u8003\u8651\u4e8c\u8fdb\u5236\u503c 1011 0100\u3002\u8fd9\u4ee3\u8868\u4ec0\u4e48\u503c\uff1f\u4f60\u53ef\u80fd\u4f1a\u8bf4 180\uff0c\u5982\u679c\u8fd9\u662f\u4e00\u4e2a\u6807\u51c6\u7684\u65e0\u7b26\u53f7\u4e8c\u8fdb\u5236\u6570\uff0c\u4f60\u5c31\u662f\u5bf9\u7684\u3002 \u4f46\u662f\uff0c\u5982\u679c\u4f7f\u7528\u4e8c\u8fdb\u5236\u8865\u7801\u5b58\u50a8\u6b64\u503c\uff0c\u5219\u5b83\u5c06\u662f -76\u3002 \u5982\u679c\u8be5\u503c\u4ee5\u5176\u4ed6\u65b9\u5f0f\u7f16\u7801\uff0c\u5219\u5b83\u53ef\u80fd\u5b8c\u5168\u4e0d\u540c\u3002 \u90a3\u4e48 C++ \u662f\u5982\u4f55\u77e5\u9053\u5c06\u5305\u542b\u4e8c\u8fdb\u5236 1011 0100 \u7684\u53d8\u91cf\u6253\u5370\u4e3a 180 \u8fd8\u662f -76 \u5462\uff1f \u5982\u679c\u7ae0\u8282\u6807\u9898\u6ca1\u6709\u900f\u9732\uff0c\u8fd9\u5c31\u662f\u7c7b\u578b\u53d1\u6325\u4f5c\u7528\u7684\u5730\u65b9\u3002\u53d8\u91cf\u7684\u7c7b\u578b\u51b3\u5b9a\u4e86\u53d8\u91cf\u7684\u503c\u5982\u4f55\u7f16\u7801\u4e3a\u4e8c\u8fdb\u5236\uff0c\u4ee5\u53ca\u5982\u4f55\u89e3\u7801\u56de\u4e00\u4e2a\u503c\u3002\u56e0\u6b64\uff0c \u5982\u679c\u53d8\u91cf\u7c7b\u578b\u662f\u65e0\u7b26\u53f7\u6574\u6570\uff0c\u5b83\u4f1a\u77e5\u9053 1011 0100 \u662f\u6807\u51c6\u4e8c\u8fdb\u5236\uff0c\u5e94\u8be5\u6253\u5370\u4e3a 180\u3002\u5982\u679c\u53d8\u91cf\u662f\u6709\u7b26\u53f7\u6574\u6570\uff0c\u5b83\u4f1a\u77e5\u9053 1011 0100 \u662f\u4f7f\u7528\u4e8c\u8fdb\u5236\u8865\u7801\u7f16\u7801\u7684\uff08\u73b0\u5728\u4fdd\u8bc1\u4e3aC++20 \u7684\uff09\uff0c\u5e76\u4e14\u5e94\u8be5\u6253\u5370\u4e3a -76 \u3002 \u5982\u4f55\u5c06\u6d6e\u70b9\u6570\u4ece\u4e8c\u8fdb\u5236\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\uff1f \u6d6e\u70b9\u6570\u5982\u4f55\u4ece\u4e8c\u8fdb\u5236\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u8981\u590d\u6742\u5f97\u591a\uff0c\u800c\u4e14\u60a8\u53ef\u80fd\u6c38\u8fdc\u90fd\u4e0d\u9700\u8981\u77e5\u9053\u3002\u4f46\u662f\uff0c\u5982\u679c\u60a8\u5f88\u597d\u5947\uff0c\u8bf7\u8bbf\u95ee \u6b64\u7ad9\u70b9 \uff0c\u8be5\u7ad9\u70b9\u5f88\u597d\u5730\u8be6\u7ec6\u89e3\u91ca\u4e86\u8be5\u4e3b\u9898\u3002","title":"O.4 - \u4e8c\u8fdb\u5236\u548c\u5341\u8fdb\u5236\u8f6c\u6362"},{"location":"O-4-converting-between-binary-and-decimal/#_1","text":"\u5728\u4ee5\u4e0b\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5047\u8bbe\u6211\u4eec\u6b63\u5728\u5904\u7406\u65e0\u7b26\u53f7\u6574\u6570\u3002 \u8003\u8651 8 \u4f4d\uff081 \u5b57\u8282\uff09\u4e8c\u8fdb\u5236\u6570 0101 1110\u3002 \u4e8c\u8fdb\u5236 0101 1110 \u8868\u793a (0 * 128) + (1 * 64) + (0 * 32) + (1 * 16) + (1 * 8) + (1 * 4) + (1 * 2) + (0 * 1)\u3002\u5982\u679c\u6211\u4eec\u5c06\u6240\u6709\u8fd9\u4e9b\u90e8\u5206\u76f8\u52a0\uff0c\u6211\u4eec\u5f97\u5230\u5341\u8fdb\u5236\u6570 64 + 16 + 8 + 4 + 2 = 94\u3002 \u8fd9\u662f\u8868\u683c\u683c\u5f0f\u7684\u76f8\u540c\u8fc7\u7a0b\u3002\u6211\u4eec\u5c06\u6bcf\u4e2a\u4e8c\u8fdb\u5236\u6570\u5b57\u4e58\u4ee5\u5176\u6570\u5b57\u503c\uff08\u7531\u5176\u4f4d\u7f6e\u786e\u5b9a\uff09\u3002\u5c06\u6240\u6709\u8fd9\u4e9b\u503c\u52a0\u8d77\u6765\u5c31\u662f\u603b\u6570\u3002 \u5c06 0101 1110 \u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\uff1a \u4e8c\u8fdb\u5236\u6570\u5b57 0 1 0 1 1 1 1 0 * \u6570\u5b57\u503c 128 64 32 16 8 4 2 1 = \u603b\u8ba1 (94) 0 64 0 16 8 4 2 0 \u8ba9\u6211\u4eec\u5c06 1001 0111 \u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\uff1a \u4e8c\u8fdb\u5236\u6570\u5b57 1 0 0 1 0 1 1 1 * \u6570\u5b57\u503c 128 64 32 16 8 4 2 1 = \u603b\u8ba1 (151) 128 0 0 16 0 4 2 1 1001 0111 \u4e8c\u8fdb\u5236 = \u5341\u8fdb\u5236\u7684 151\u3002 \u8fd9\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u6269\u5c55\u5230 16 \u4f4d\u6216 32 \u4f4d\u4e8c\u8fdb\u5236\u6570\uff0c\u53ea\u9700\u6dfb\u52a0\u66f4\u591a\u5217\u5373\u53ef\u3002\u8bf7\u6ce8\u610f\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4ece\u53f3\u7aef\u5f00\u59cb\uff0c\u7136\u540e\u5411\u5de6\u79fb\u52a8\uff0c\u8fb9\u8d70\u8fb9\u5c06\u6570\u5b57\u503c\u4e58\u4ee5 2\u3002","title":"\u5c06\u4e8c\u8fdb\u5236\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236"},{"location":"O-4-converting-between-binary-and-decimal/#_2","text":"\u4ece\u5341\u8fdb\u5236\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u6709\u70b9\u68d8\u624b\uff0c\u4f46\u4ecd\u7136\u975e\u5e38\u7b80\u5355\u3002\u6709\u4e24\u79cd\u597d\u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\u3002 \u7b2c\u4e00\u79cd\u65b9\u6cd5\u662f\u8fde\u7eed\u9664\u4ee5 2\uff0c\u7136\u540e\u8bb0\u4e0b\u4f59\u6570\u3002\u4e8c\u8fdb\u5236\u6570\u662f\u5728\u4f59\u6570\u7684\u672b\u5c3e\u6784\u9020\u7684\uff0c\u4ece\u4e0b\u5230\u4e0a\u3002 \u5c06 148 \u4ece\u5341\u8fdb\u5236\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\uff08\u4f7f\u7528 r \u8868\u793a\u4f59\u6570\uff09\uff1a 148 / 2 = 74 r0 74 / 2 = 37 r0 37 / 2 = 18 r1 18 / 2 = 9 r0 9 / 2 = 4 r1 4 / 2 = 2 r0 2 / 2 = 1 r0 1 / 2 = 0 r1 \u4ece\u4e0b\u5f80\u4e0a \u5199\u51fa\u6240\u6709\u4f59\u6570\uff1a1001 0100 148 \u5341\u8fdb\u5236 = 1001 0100 \u4e8c\u8fdb\u5236\u3002 \u60a8\u53ef\u4ee5\u901a\u8fc7\u5c06\u4e8c\u8fdb\u5236\u8f6c\u6362\u56de\u5341\u8fdb\u5236\u6765\u9a8c\u8bc1\u6b64\u7b54\u6848\uff1a (1 * 128) + (0 * 64) + (0 * 32) + (1 * 16) + (0 * 8) + (1 * 4) + (0 * 2) + (0 * 1) = 148","title":"\u5341\u8fdb\u5236\u8f6c\u4e8c\u8fdb\u5236\u7684\u65b9\u6cd5\u4e00"},{"location":"O-4-converting-between-binary-and-decimal/#_3","text":"\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u6d89\u53ca\u9006\u5411\u8ba1\u7b97\u6bcf\u4e2a\u4f4d\u5fc5\u987b\u662f\u4ec0\u4e48\u3002\u5bf9\u4e8e\u8f83\u5c0f\u7684\u4e8c\u8fdb\u5236\u6570\uff0c\u6b64\u65b9\u6cd5\u53ef\u80fd\u66f4\u5bb9\u6613\u3002 \u518d\u6b21\u8003\u8651\u5341\u8fdb\u5236\u6570 148\u3002\u5c0f\u4e8e148\u76842\u7684\u6700\u5927\u6b21\u65b9\u662f\u591a\u5c11\uff1f128\uff0c\u6240\u4ee5\u6211\u4eec\u5c06\u4ece\u90a3\u91cc\u5f00\u59cb\u3002 \u662f 148 >= 128 \u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 128 \u4f4d\u5fc5\u987b\u662f 1. 148 - 128 = 20\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u9700\u8981\u518d\u627e\u5230\u503c 20 \u7684\u4f4d\u3002 \u662f 20 >= 64 \u5417\uff1f\u4e0d\uff0c\u6240\u4ee564\u4f4d\u5fc5\u987b\u662f0\u300220 >=32\u5417\uff1f\u4e0d\u662f\uff0c\u6240\u4ee532\u4f4d\u4e00\u5b9a\u662f0\u300220 >= 16\u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 16 \u4f4d\u5fc5\u987b\u662f 1. 20 - 16 = 4\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u9700\u8981\u627e\u5230\u66f4\u591a\u7684 4 \u4f4d\u3002 \u662f 4 >= 8 \u5417\uff1f\u4e0d\u662f\uff0c\u6240\u4ee58\u4f4d\u4e00\u5b9a\u662f0\u3002 \u662f4 >= 4\u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5\u7b2c 4 \u4f4d\u5fc5\u987b\u662f 1\u30024 - 4 = 0\uff0c\u8fd9\u610f\u5473\u7740\u6240\u6709\u5176\u4f59\u4f4d\u5fc5\u987b\u662f 0\u3002 148 = (1 * 128) + (0 * 64) + (0 * 32) + (1 * 16) + (0 * 8) + (1 * 4) + (0 * 2) + (0 * 1) = 1001 0100 \u8868\u683c\u5f62\u5f0f\uff1a \u4e8c\u8fdb\u5236\u6570 1 0 0 1 0 1 0 0 * \u6570\u5b57\u503c 128 64 32 16 8 4 2 1 = \u603b\u8ba1 (148) 128 0 0 16 0 4 0 0 \u53e6\u4e00\u4e2a\u4f8b\u5b50 \u8ba9\u6211\u4eec\u4f7f\u7528\u65b9\u6cd5 1 \u5c06 117 \u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\uff1a 117 / 2 = 58 r1 58 / 2 = 29 r0 29 / 2 = 14 r1 14 / 2 = 7 r0 7 / 2 = 3 r1 3 / 2 = 1 r1 1 / 2 = 0 r1 \u4ece\u4e0b\u5f80\u4e0a\u7684\u4f59\u6570\u6784\u9020\u6570\uff0c117 = 111 0101 \u4e8c\u8fdb\u5236 \u5e76\u4f7f\u7528\u65b9\u6cd52\uff1a 2 \u5c0f\u4e8e 117 \u7684\u6700\u5927\u6b21\u65b9\u662f 64\u3002 \u662f 117 >= 64 \u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 64 \u4f4d\u5fc5\u987b\u662f 1\u3002117 - 64 = 53\u300253 >= 32 \u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 32 \u4f4d\u5fc5\u987b\u662f 1\u300253 - 32 = 21\u300221 >= 16 \u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 16 \u4f4d\u5fc5\u987b\u662f 1\u3002 21 - 16 = 5\u3002 5 >= 8 \u5417\uff1f\u4e0d\u662f\uff0c\u6240\u4ee58\u4f4d\u4e00\u5b9a\u662f0\u3002 \u662f5 >= 4\u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5\u7b2c 4 \u4f4d\u5fc5\u987b\u662f 1\u30025 - 4 = 1\u30021 >= 2\uff1f\u4e0d\u662f\uff0c\u6240\u4ee52\u4f4d\u4e00\u5b9a\u662f0\u3002 \u662f1 >= 1\u5417\uff1f\u662f\u7684\uff0c\u6240\u4ee5 1 \u4f4d\u5fc5\u987b\u662f 1\u3002 117 \u5341\u8fdb\u5236 = 111 0101 \u4e8c\u8fdb\u5236\u3002","title":"\u5341\u8fdb\u5236\u8f6c\u4e8c\u8fdb\u5236\u7684\u65b9\u6cd5\u4e8c"},{"location":"O-4-converting-between-binary-and-decimal/#_4","text":"\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff08\u7a0d\u540e\u6211\u4eec\u4f1a\u770b\u5230\uff09\uff0c\u80fd\u591f\u5c06\u4e24\u4e2a\u4e8c\u8fdb\u5236\u6570\u76f8\u52a0\u662f\u5f88\u6709\u7528\u7684\u3002\u76f8\u52a0\u4e8c\u8fdb\u5236\u6570\u51fa\u5947\u5730\u5bb9\u6613\uff08\u751a\u81f3\u53ef\u80fd\u6bd4\u6dfb\u52a0\u5341\u8fdb\u5236\u6570\u66f4\u5bb9\u6613\uff09\uff0c\u5c3d\u7ba1\u4e00\u5f00\u59cb\u5b83\u53ef\u80fd\u770b\u8d77\u6765\u5f88\u5947\u602a\uff0c\u56e0\u4e3a\u60a8\u4e0d\u4e60\u60ef\u5b83\u3002 \u8003\u8651\u4e24\u4e2a\u5c0f\u7684\u4e8c\u8fdb\u5236\u6570\uff1a 0110\uff08\u5341\u8fdb\u5236\u4e3a 6\uff09+ 0111\uff08\u5341\u8fdb\u5236\u4e3a 7\uff09 \u8ba9\u6211\u4eec\u6dfb\u52a0\u8fd9\u4e9b\u3002\u9996\u5148\uff0c\u50cf\u4e0a\u9762\u4e00\u6837\u5c06\u5b83\u4eec\u6392\u6210\u4e00\u884c\u3002\u7136\u540e\uff0c\u4ece\u53f3\u5411\u5de6\u5f00\u59cb\uff0c\u6211\u4eec\u5c06\u6bcf\u4e00\u5217\u6570\u5b57\u76f8\u52a0\uff0c\u5c31\u50cf\u6211\u4eec\u5728\u5341\u8fdb\u5236\u6570\u4e2d\u6240\u505a\u7684\u90a3\u6837\u3002\u4f46\u662f\uff0c\u7531\u4e8e\u4e8c\u8fdb\u5236\u6570\u5b57\u53ea\u80fd\u662f 0 \u6216 1\uff0c\u6240\u4ee5\u53ea\u6709 4 \u79cd\u53ef\u80fd\uff1a 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 0\uff0c\u5c06 1 \u79fb\u81f3\u4e0b\u4e00\u5217 \u8ba9\u6211\u4eec\u505a\u7b2c\u4e00\u5217\uff1a 1 2 3 4 0110\uff08\u5341\u8fdb\u5236\u4e3a 6\uff09+ 0111\uff08\u5341\u8fdb\u5236\u4e3a 7\uff09 ---- 1 0 + 1 = 1\u3002\u7b80\u5355\u3002 \u7b2c\u4e8c\u680f\uff1a 1 2 3 4 5 1 0110\uff08\u5341\u8fdb\u5236\u4e3a 6\uff09+ 0111\uff08\u5341\u8fdb\u5236\u4e3a 7\uff09 ---- 01 1 + 1 = 0\uff0c\u5e26\u4e00\u4e2a\u8fdb\u4f4d\u5230\u4e0b\u4e00\u5217 \u7b2c\u4e09\u680f\uff1a 1 2 3 4 5 11 0110\uff08\u5341\u8fdb\u5236\u4e3a 6\uff09+ 0111\uff08\u5341\u8fdb\u5236\u4e3a 7\uff09 ---- 101 \u8fd9\u4e2a\u6709\u70b9\u68d8\u624b\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c1 + 1 = 0\uff0c\u5e26\u4e00\u4e2a\u8fdb\u4f4d\u5230\u4e0b\u4e00\u5217\u3002\u4f46\u662f\uff0c\u6211\u4eec\u5df2\u7ecf\u4ece\u4e0a\u4e00\u5217\u4e2d\u643a\u5e26\u4e86\u4e00\u4e2a 1\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u52a0 1\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5728\u8fd9\u4e00\u5217\u4e2d\u5f97\u5230\u4e00\u4e2a 1\uff0c\u5e76\u5c06 1 \u5e26\u5165\u4e0b\u4e00\u5217 \u6700\u540e\u4e00\u680f\uff1a 1 2 3 4 5 11 0110\uff08\u5341\u8fdb\u5236\u4e3a 6\uff09+ 0111\uff08\u5341\u8fdb\u5236\u4e3a 7\uff09 ---- 1101 0 + 0 = 0\uff0c\u4f46\u6709\u4e00\u4e2a\u8fdb\u4f4d\u7684 1\uff0c\u6240\u4ee5\u6211\u4eec\u52a0 1\u30021101 = \u5341\u8fdb\u5236\u7684 13\u3002 \u73b0\u5728\uff0c\u6211\u4eec\u5982\u4f55\u7ed9\u4efb\u4f55\u7ed9\u5b9a\u7684\u4e8c\u8fdb\u5236\u6570\uff08\u4f8b\u5982 1011 0011\uff09\u52a0 1\uff1f\u540c\u4e0a\uff0c\u53ea\u662f\u6700\u4e0b\u9762\u7684\u6570\u662f\u4e8c\u8fdb\u52361\u3002 1 2 3 4 5 1\u4e2a\uff08\u8fdb\u4f4d\u67f1\uff09 1011 0011\uff08\u539f\u59cb\u4e8c\u8fdb\u5236\u6570\uff09 0000 0001\uff08\u4e8c\u8fdb\u5236\u4e3a 1\uff09 --------- 1011 0100","title":"\u76f8\u52a0\u4e8c\u8fdb\u5236\u6570"},{"location":"O-4-converting-between-binary-and-decimal/#_5","text":"\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u53ea\u5904\u7406\u4e86\u65e0\u7b26\u53f7\u6574\u6570\u3002\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4e86\u89e3\u5982\u4f55\u5904\u7406\u6709\u7b26\u53f7\u6570\uff08\u53ef\u4ee5\u4e3a\u8d1f\u6570\uff09\u3002 \u6709\u7b26\u53f7\u6574\u6570\u901a\u5e38\u4f7f\u7528\u79f0\u4e3a \u4e8c\u8fdb\u5236\u8865\u7801 \u7684\u65b9\u6cd5\u5b58\u50a8\u3002\u5728\u4e8c\u8fdb\u5236\u8865\u7801\u4e2d\uff0c\u6700\u5de6\u8fb9\uff08\u6700\u9ad8\u6709\u6548\u4f4d\uff09\u7684\u4f4d\u7528\u4f5c\u7b26\u53f7\u4f4d\u3002\u7b26\u53f7\u4f4d 0 \u8868\u793a\u6570\u5b57\u4e3a\u6b63\u6570\uff0c\u7b26\u53f7\u4f4d 1 \u8868\u793a\u6570\u5b57\u4e3a\u8d1f\u6570\u3002 \u6b63\u7b26\u53f7\u6570\u4e0e\u65e0\u7b26\u53f7\u6b63\u6570\u4e00\u6837\u4ee5\u4e8c\u8fdb\u5236\u8868\u793a\uff08\u7b26\u53f7\u4f4d\u8bbe\u7f6e\u4e3a 0\uff09\u3002 \u8d1f\u7b26\u53f7\u6570\u4ee5\u4e8c\u8fdb\u5236\u8868\u793a\u4e3a\u6b63\u6570\u7684\u6309\u4f4d\u53d6\u53cd\u52a0 1\u3002 \u5341\u8fdb\u5236\u8f6c\u4e8c\u8fdb\u5236\uff08\u4e8c\u8fdb\u5236\u8865\u7801\uff09 \u4f8b\u5982\uff0c\u8fd9\u662f\u6211\u4eec\u5982\u4f55\u7528\u4e8c\u8fdb\u5236\u4e8c\u8fdb\u5236\u8865\u7801\u8868\u793a -5\uff1a \u9996\u5148\u6211\u4eec\u627e\u51fa 5 \u7684\u4e8c\u8fdb\u5236\u8868\u793a\uff1a0000 0101 \u7136\u540e\u6211\u4eec\u53cd\u8f6c\u6240\u6709\u4f4d\uff1a1111 1010 \u7136\u540e\u6211\u4eec\u6dfb\u52a0 1\uff1a1111 1011 \u5c06 -76 \u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\uff1a \u4e8c\u8fdb\u5236\u6b63 76\uff1a0100 1100 \u53cd\u8f6c\u6240\u6709\u4f4d\uff1a1011 0011 \u52a0 1\uff1a1011 0100 \u4e3a\u4ec0\u4e48\u8981\u52a01\uff1f\u8003\u8651\u6570\u5b57 0\u3002\u5982\u679c\u5c06\u8d1f\u503c\u7b80\u5355\u5730\u8868\u793a\u4e3a\u6b63\u6570\u7684\u5012\u6570\uff0c\u5219 0 \u5c06\u6709\u4e24\u79cd\u8868\u793a\u5f62\u5f0f\uff1a0000 0000\uff08\u6b63\u96f6\uff09\u548c 1111 1111\uff08\u8d1f\u96f6\uff09\u3002\u901a\u8fc7\u52a0 1\uff0c1111 1111 \u6709\u610f\u6ea2\u51fa\u5e76\u53d8\u6210 0000 0000\u3002\u8fd9\u53ef\u4ee5\u9632\u6b62 0 \u6709\u4e24\u79cd\u8868\u793a\u5f62\u5f0f\uff0c\u5e76\u7b80\u5316\u4e86\u4e00\u4e9b\u7528\u8d1f\u6570\u8fdb\u884c\u7b97\u672f\u8fd0\u7b97\u6240\u9700\u7684\u5185\u90e8\u903b\u8f91\u3002","title":"\u6709\u7b26\u53f7\u6570\u548c\u8865\u7801"},{"location":"O-4-converting-between-binary-and-decimal/#_6","text":"\u8981\u5c06\u4e8c\u8fdb\u5236\u8865\u7801\u8f6c\u6362\u56de\u5341\u8fdb\u5236\uff0c\u9996\u5148\u8981\u67e5\u770b\u7b26\u53f7\u4f4d\u3002 \u5982\u679c\u7b26\u53f7\u4f4d\u4e3a 0\uff0c\u53ea\u9700\u6309\u7167\u4e0a\u9762\u65e0\u7b26\u53f7\u6570\u6240\u793a\u7684\u65b9\u5f0f\u8f6c\u6362\u6570\u5b57\u3002 \u5982\u679c\u7b26\u53f7\u4f4d\u662f1\uff0c\u90a3\u4e48\u6211\u4eec\u628a\u4f4d\u53d6\u53cd\uff0c\u52a01\uff0c\u7136\u540e\u8f6c\u6210\u5341\u8fdb\u5236\uff0c\u7136\u540e\u8ba9\u90a3\u4e2a\u5341\u8fdb\u5236\u6570\u53d8\u6210\u8d1f\u6570\uff08\u56e0\u4e3a\u7b26\u53f7\u4f4d\u672c\u6765\u5c31\u662f\u8d1f\u6570\uff09\u3002 \u4f8b\u5982\uff0c\u8981\u5c06 1001 1110 \u4ece\u4e8c\u8fdb\u5236\u8865\u7801\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u6570\uff1a \u7ed9\u5b9a\uff1a1001 1110 \u53cd\u8f6c\u4f4d\uff1a0110 0001 \u52a0 1\uff1a0110 0010 \u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\uff1a(0 * 128) + (1 * 64) + (1 * 32 ) + (0 * 16) + (0 * 8) + (0 * 4) + (1 * 2) + (0 * 1) = 64 + 32 + 2 = 98 \u7531\u4e8e\u539f\u59cb\u7b26\u53f7\u4f4d\u4e3a\u8d1f\uff0c\u6240\u4ee5\u6700\u540e\u7684\u503c\u662f-98\u3002","title":"\u5c06\u4e8c\u8fdb\u5236\uff08\u4e8c\u8fdb\u5236\u8865\u7801\uff09\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236"},{"location":"O-4-converting-between-binary-and-decimal/#_7","text":"\u8003\u8651\u4e8c\u8fdb\u5236\u503c 1011 0100\u3002\u8fd9\u4ee3\u8868\u4ec0\u4e48\u503c\uff1f\u4f60\u53ef\u80fd\u4f1a\u8bf4 180\uff0c\u5982\u679c\u8fd9\u662f\u4e00\u4e2a\u6807\u51c6\u7684\u65e0\u7b26\u53f7\u4e8c\u8fdb\u5236\u6570\uff0c\u4f60\u5c31\u662f\u5bf9\u7684\u3002 \u4f46\u662f\uff0c\u5982\u679c\u4f7f\u7528\u4e8c\u8fdb\u5236\u8865\u7801\u5b58\u50a8\u6b64\u503c\uff0c\u5219\u5b83\u5c06\u662f -76\u3002 \u5982\u679c\u8be5\u503c\u4ee5\u5176\u4ed6\u65b9\u5f0f\u7f16\u7801\uff0c\u5219\u5b83\u53ef\u80fd\u5b8c\u5168\u4e0d\u540c\u3002 \u90a3\u4e48 C++ \u662f\u5982\u4f55\u77e5\u9053\u5c06\u5305\u542b\u4e8c\u8fdb\u5236 1011 0100 \u7684\u53d8\u91cf\u6253\u5370\u4e3a 180 \u8fd8\u662f -76 \u5462\uff1f \u5982\u679c\u7ae0\u8282\u6807\u9898\u6ca1\u6709\u900f\u9732\uff0c\u8fd9\u5c31\u662f\u7c7b\u578b\u53d1\u6325\u4f5c\u7528\u7684\u5730\u65b9\u3002\u53d8\u91cf\u7684\u7c7b\u578b\u51b3\u5b9a\u4e86\u53d8\u91cf\u7684\u503c\u5982\u4f55\u7f16\u7801\u4e3a\u4e8c\u8fdb\u5236\uff0c\u4ee5\u53ca\u5982\u4f55\u89e3\u7801\u56de\u4e00\u4e2a\u503c\u3002\u56e0\u6b64\uff0c \u5982\u679c\u53d8\u91cf\u7c7b\u578b\u662f\u65e0\u7b26\u53f7\u6574\u6570\uff0c\u5b83\u4f1a\u77e5\u9053 1011 0100 \u662f\u6807\u51c6\u4e8c\u8fdb\u5236\uff0c\u5e94\u8be5\u6253\u5370\u4e3a 180\u3002\u5982\u679c\u53d8\u91cf\u662f\u6709\u7b26\u53f7\u6574\u6570\uff0c\u5b83\u4f1a\u77e5\u9053 1011 0100 \u662f\u4f7f\u7528\u4e8c\u8fdb\u5236\u8865\u7801\u7f16\u7801\u7684\uff08\u73b0\u5728\u4fdd\u8bc1\u4e3aC++20 \u7684\uff09\uff0c\u5e76\u4e14\u5e94\u8be5\u6253\u5370\u4e3a -76 \u3002","title":"\u4e3a\u4ec0\u4e48\u7c7b\u578b\u5f88\u91cd\u8981"},{"location":"O-4-converting-between-binary-and-decimal/#_8","text":"\u6d6e\u70b9\u6570\u5982\u4f55\u4ece\u4e8c\u8fdb\u5236\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u8981\u590d\u6742\u5f97\u591a\uff0c\u800c\u4e14\u60a8\u53ef\u80fd\u6c38\u8fdc\u90fd\u4e0d\u9700\u8981\u77e5\u9053\u3002\u4f46\u662f\uff0c\u5982\u679c\u60a8\u5f88\u597d\u5947\uff0c\u8bf7\u8bbf\u95ee \u6b64\u7ad9\u70b9 \uff0c\u8be5\u7ad9\u70b9\u5f88\u597d\u5730\u8be6\u7ec6\u89e3\u91ca\u4e86\u8be5\u4e3b\u9898\u3002","title":"\u5982\u4f55\u5c06\u6d6e\u70b9\u6570\u4ece\u4e8c\u8fdb\u5236\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\uff1f"},{"location":"Off-by-one/","tags":["\u8bcd\u6c47\u8868","Off-by-one"],"text":"","title":"\u5dee\u4e00\u9519\u8bef"},{"location":"Output-manipulators/","tags":["\u8bcd\u6c47\u8868","Output-manipulators"],"text":"","title":"\u8f93\u51fa\u64cd\u7eb5\u5668"},{"location":"RAII%20%28Resource%20Acquisition%20Is%20Initialization%29/","tags":["\u8bcd\u6c47\u8868","RAII (Resource Acquisition Is Initialization)"],"text":"","title":"\u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09"},{"location":"Run-time-type-information-RTTI/","tags":["\u8bcd\u6c47\u8868","Run-time-type-information-RTTI"],"text":"","title":"\u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f(RTTI)"},{"location":"Sequence-containers/","tags":["\u8bcd\u6c47\u8868","Sequence-containers"],"text":"","title":"\u987a\u5e8f\u5bb9\u5668"},{"location":"Template-specialization/","tags":["\u8bcd\u6c47\u8868","Template-specialization"],"text":"","title":"\u6a21\u677f\u7279\u5316"},{"location":"abbreviated%20function%20templates/","tags":["\u8bcd\u6c47\u8868","abbreviated function templates"],"text":"","title":"\u7f29\u5199\u51fd\u6570\u6a21\u677f"},{"location":"abstract-class/","tags":["\u8bcd\u6c47\u8868","\u62bd\u8c61\u7c7b"],"text":"","title":"\u62bd\u8c61\u7c7b"},{"location":"abstraction/","tags":["\u8bcd\u6c47\u8868","abstraction"],"text":"","title":"\u62bd\u8c61"},{"location":"access-function/","tags":["\u8bcd\u6c47\u8868","access-function"],"text":"","title":"\u6210\u5458\u8bbf\u95ee\u51fd\u6570"},{"location":"access-specifiers/","tags":["\u8bcd\u6c47\u8868","access-specifiers"],"text":"","title":"\u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26"},{"location":"address-of-operator/","tags":["\u8bcd\u6c47\u8868","address-of-operator"],"text":"","title":"\u53d6\u5730\u5740\u64cd\u4f5c\u7b26 &"},{"location":"aggregate-data-type/","tags":["\u8bcd\u6c47\u8868","aggregate-data-type"],"text":"\u79f0\u4e3a aggregates\uff08\u805a\u5408\u7269\uff09 \u805a\u5408\u7c7b\u578b\u5305\u62ec\uff1a - array type - class type (typically, struct or union), that has - no private or protected direct (since C++17)non-static data members - no user-declared constructors(until C++11) - no user-provided , inherited , or explicit constructors (since C++11) (until C++20) - no user-declared or inherited constructors(since C++20) - no virtual, private, or protected (since C++17) base classes - no virtual member functions - no default member initializers (since C++11) (until C++14) \u805a\u5408\u7c7b\u578b\u4e2d\u5143\u7d20\u7684\u7279\u70b9 - for an array, the array elements in increasing subscript order, or - for a class, the non-static data members that are not anonymous bit-fields , in declaration order.(until C++17) - for a class, the direct base classes in declaration order, followed by the direct non-static data members that are neither anonymous bit-fields nor members of an anonymous union , in declaration order.(since C++17) C++ \u4e2d\u7684\u805a\u5408\u6570\u636e\u7c7b\u578b\u5fc5\u987b\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\uff1a \u662f\u4e00\u4e2a \u7c7b\u7c7b\u578b (\u5305\u62ec struct, class \u6216 union) \u6216\u6570\u7ec4 (\u5305\u62ec\u5185\u7f6e\u6570\u7ec4\u6216 std::array )\uff1b \u6ca1\u6709\u79c1\u6709\u6216\u53d7\u4fdd\u62a4\u7684\u975e\u9759\u6001\u6570\u636e\u6210\u5458\uff1b \u6ca1\u6709\u7528\u6237\u58f0\u660e\u7684\u6216\u7ee7\u627f\u7684 \u6784\u9020\u51fd\u6570 \uff1b \u6ca1\u6709 \u57fa\u7c7b \uff1b \u6ca1\u6709 \u865a\u51fd\u6570 \uff1b","title":"\u805a\u5408\u6570\u636e\u7c7b\u578b"},{"location":"aggregate-initialization/","tags":["\u8bcd\u6c47\u8868","aggregate-initialization"],"text":"\u805a\u5408\u521d\u59cb\u5316\u4f7f\u7528\u5927\u62ec\u53f7\uff0c\u4e3b\u8981\u7528\u4e8e\u521d\u59cb\u5316\u7ed3\u6784\u4f53\u7684\uff0c\u5982\u679c\u662f\u7c7b\u7684\u8bdd\u5219\u5fc5\u987b\u90fd\u662fpublic\u6210\u5458\uff08\u4e0d\u592a\u5e38\u7528\uff09\uff1a 1 2 3 4 5 struct Data { std :: string name ; double value ; }; Data x { \"test1\" , 6.778 }; \u4eceC++17\u5f00\u59cb\uff0c\u805a\u5408\u8fd8\u53ef\u4ee5\u6709\u57fa\u7c7b\uff0c\u56e0\u6b64\u53ef\u4ee5\u521d\u59cb\u5316\u4ece\u5176\u4ed6\u7c7b/\u7ed3\u6784\u4f53\u6d3e\u751f\u800c\u6765\u7684\u7ed3\u6784\u4f53\uff1a 1 2 3 4 struct MoreData : Data { bool done ; }; MoreData y {{ \"test1\" , 6.778 }, false }; https://toutiao.io/posts/tt34ysp/preview https://en.cppreference.com/w/cpp/language/aggregate_initialization \u805a\u5408\u521d\u59cb\u5316\u6709\u4e09\u79cd\u5f62\u5f0f\uff1a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct Employee { int id {}; int age {}; double wage {}; }; int main () { Employee frank = { 1 , 32 , 60000.0 }; // \u62f7\u8d1d\u5217\u8868\u521d\u59cb\u5316\uff0c\u4f7f\u7528\u5927\u62ec\u53f7 Employee robert ( 3 , 45 , 62500.0 ); // \u4f7f\u7528\u5c0f\u62ec\u53f7\u7684\u76f4\u63a5\u521d\u59cb\u5316(C++20) Employee joe { 2 , 28 , 45000.0 }; // \u4f7f\u7528\u5927\u62ec\u53f7\u5217\u8868\u7684\u5217\u8868\u521d\u59cb\u5316\uff08\u63a8\u8350\uff09 return 0 ; }","title":"\u805a\u5408\u521d\u59cb\u5316"},{"location":"aggregation/","tags":["\u8bcd\u6c47\u8868","aggregation"],"text":"","title":"\u805a\u5408"},{"location":"ambiguous-match/","tags":["\u8bcd\u6c47\u8868","ambiguous-match"],"text":"","title":"\u4e0d\u660e\u786e\u5339\u914d"},{"location":"anonymous-object/","tags":["\u8bcd\u6c47\u8868","anonymous-object"],"text":"","title":"\u533f\u540d\u5bf9\u8c61"},{"location":"arguments/","tags":["\u8bcd\u6c47\u8868","arguments"],"text":"\u8c03\u7528\u51fd\u6570\u65f6\u4f7f\u7528\u7684\u53c2\u6570\u3002 \u5b9e\u53c2\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u5f0f\u5411 \u5f62\u53c2 \u4f20\u9012\uff1a 1. \u6309\u503c\u4f20\u9012 \u65f6\uff0c\u5f62\u53c2\u5f97\u5230\u4e00\u4efd\u5b9e\u53c2\u7684\u62f7\u8d1d 2. \u6309\u5f15\u7528\u4f20\u9012 \u65f6\uff0c\u5f62\u53c2\uff08\u58f0\u660e\u4e3a\u5f15\u7528\u7c7b\u578b\uff09\u88ab\u7ed1\u5b9a\u5230\u5b9e\u53c2\uff0c\u4ece\u800c\u53ef\u4ee5\u4fee\u6539\u5b9e\u53c2 3. \u6309\u5730\u5740\u4f20\u9012 \u662f\uff0c\u5f62\u53c2\uff08\u58f0\u660e\u4e3a\u6307\u9488\u7c7b\u578b\uff09\u83b7\u5f97\u5f62\u53c2\u6301\u6709\u5730\u5740\u7684\u62f7\u8d1d\uff0c\u548c\u5b9e\u53c2\u6307\u5411\u540c\u4e00\u4e2a\u5730\u5740\uff0c\u53ef\u4ee5\u4fee\u6539\u8be5\u5730\u5740\u5b58\u653e\u7684\u503c\u3002\u4f46\u662f\u5730\u5740\u672c\u8eab\u662f\u5b9e\u53c2\u7684\u62f7\u8d1d\uff0c\u6240\u4ee5\u4fee\u6539\u5f62\u53c2\u7684\u503c\uff08\u5730\u5740\uff09\u4e0d\u4f1a\u5f71\u54cd\u5230\u5b9e\u53c2\uff0c\u5b9e\u53c2\u4ecd\u7136\u6301\u6709\u539f\u5730\u5740\u3002","title":"\u5b9e\u53c2"},{"location":"arity/","tags":["\u8bcd\u6c47\u8868","arity"],"text":"","title":"\u5143\u6570"},{"location":"automatic-memory-allocation/","tags":["\u8bcd\u6c47\u8868","automatic-memory-allocation"],"text":"","title":"\u81ea\u52a8\u5185\u5b58\u5206\u914d"},{"location":"automatic-storage-duration/","tags":["\u8bcd\u6c47\u8868","automatic-storage-duration"],"text":"","title":"\u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4"},{"location":"base-class/","tags":["\u8bcd\u6c47\u8868","base-class"],"text":"","title":"\u57fa\u7c7b"},{"location":"bit/","tags":["\u8bcd\u6c47\u8868","bit"],"text":"","title":"\u4f4d"},{"location":"bitwise-and/","tags":["\u8bcd\u6c47\u8868","bitwise-and"],"text":"","title":null},{"location":"bitwise-or/","tags":["\u8bcd\u6c47\u8868","bitwise-or"],"text":"","title":"\u6309\u4f4d\u6216"},{"location":"bss%20segment/","tags":["\u8bcd\u6c47\u8868","bss segment"],"text":"","title":"bss\u6bb5"},{"location":"byte/","tags":["\u8bcd\u6c47\u8868","byte"],"text":"","title":"\u5b57\u8282"},{"location":"call-stack/","tags":["\u8bcd\u6c47\u8868","call-stack"],"text":"","title":"\u8c03\u7528\u6808"},{"location":"callable-object/","tags":["\u8bcd\u6c47\u8868","callable-object"],"text":"","title":"\u53ef\u8c03\u7528\u5bf9\u8c61"},{"location":"callback%20functions/","tags":["\u8bcd\u6c47\u8868","callback functions"],"text":"","title":"\u56de\u8c03\u51fd\u6570"},{"location":"child-class/","tags":["\u8bcd\u6c47\u8868","child-class"],"text":"","title":"\u5b50\u7c7b"},{"location":"class-instance/","tags":["\u8bcd\u6c47\u8868","\u7c7b\u5b9e\u4f8b"],"text":"","title":"\u7c7b\u5b9e\u4f8b"},{"location":"class-template-argument-deduction/","tags":["\u8bcd\u6c47\u8868","class-template-argument-deduction"],"text":"","title":"\u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad"},{"location":"class-template/","tags":["\u8bcd\u6c47\u8868","class-template"],"text":"","title":"\u7c7b\u6a21\u677f"},{"location":"class-type/","tags":["\u8bcd\u6c47\u8868","class-type"],"text":"\u5305\u62ec\u7ed3\u6784\u4f53\u3001\u7c7b\u548c\u8054\u5408\u4f53","title":"\u7c7b\u7c7b\u578b"},{"location":"closure/","tags":["\u8bcd\u6c47\u8868","closure"],"text":"","title":"\u95ed\u5305"},{"location":"code%20segment/","tags":["\u8bcd\u6c47\u8868","code segment"],"text":"","title":"\u4ee3\u7801\u6bb5"},{"location":"compile-time/","tags":["glossary","compile-time"],"text":"","title":"\u7f16\u8bd1\u65f6"},{"location":"compiler/","tags":["\u8bcd\u6c47\u8868","compiler"],"text":"","title":"\u7f16\u8bd1\u5668"},{"location":"composite-types/","tags":["\u8bcd\u6c47\u8868","composite-types"],"text":"","title":"\u7ec4\u5408\u7c7b\u578b"},{"location":"const-cast/","tags":["\u8bcd\u6c47\u8868","const-cast"],"text":"","title":"const \u7c7b\u578b\u8f6c\u6362"},{"location":"const-member-function/","tags":["\u8bcd\u6c47\u8868","const-member-function"],"text":"","title":"const \u6210\u5458\u51fd\u6570"},{"location":"const-pointer/","tags":["\u8bcd\u6c47\u8868","const-pointer"],"text":"","title":"\u5e38\u91cf\u6307\u9488"},{"location":"constant%20expression/","tags":["\u8bcd\u6c47\u8868","constant expression"],"text":"","title":"\u5e38\u91cf\u8868\u8fbe\u5f0f"},{"location":"constructor/","tags":["\u8bcd\u6c47\u8868","\u6784\u9020\u51fd\u6570"],"text":"","title":"\u6784\u9020\u51fd\u6570"},{"location":"container-adapter/","tags":["\u8bcd\u6c47\u8868","container-adapter"],"text":"","title":"\u5bb9\u5668\u9002\u914d\u5668"},{"location":"container-class/","tags":["\u8bcd\u6c47\u8868","container-class"],"text":"","title":"\u5bb9\u5668\u7c7b"},{"location":"converting-constructors/","tags":["\u8bcd\u6c47\u8868","converting-constructors"],"text":"","title":"\u8f6c\u6362\u6784\u9020\u51fd\u6570"},{"location":"copy-assignment-operator/","tags":["\u8bcd\u6c47\u8868","\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26"],"text":"","title":"\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26"},{"location":"copy-constructor/","tags":["\u8bcd\u6c47\u8868","copy-constructor"],"text":"","title":"\u62f7\u8d1d\u6784\u9020\u51fd\u6570"},{"location":"copy-constructors/","tags":["\u8bcd\u6c47\u8868","copy-constructors"],"text":"","title":"\u62f7\u8d1d\u6784\u9020\u51fd\u6570"},{"location":"copy-initialization/","tags":["\u8bcd\u6c47\u8868","copy-initialization","initialization"],"text":"","title":"\u62f7\u8d1d\u521d\u59cb\u5316"},{"location":"copy-semantics/","tags":["\u8bcd\u6c47\u8868","copy-semantics"],"text":"","title":"\u62f7\u8d1d\u8bed\u4e49"},{"location":"copy-swap%E4%B9%A0%E8%AF%AD/","text":"","title":"Copy swap\u4e60\u8bed"},{"location":"covariant/","tags":["\u8bcd\u6c47\u8868","covariant"],"text":"\u5982\u679c\u865a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f\u6307\u5411\u76f8\u540c\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u5176\u91cd\u5199\u51fd\u6570\u4e5f\u53ef\u4ee5\u8fd4\u56de\u6307\u5411\u6d3e\u751f\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528\u3002\u8fd9\u4e2a\u673a\u5236\u79f0\u4e3a\u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b","title":"\u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b"},{"location":"dangling/","tags":["\u8bcd\u6c47\u8868","dangling"],"text":"\u6307\u5411\u4e0d\u5b58\u5728\u5bf9\u8c61\u7684\u6307\u9488\u6216\u5f15\u7528","title":"\u60ac\u5782"},{"location":"data%20segment/","tags":["\u8bcd\u6c47\u8868","data segment"],"text":"","title":"\u6570\u636e\u6bb5"},{"location":"declaration/","tags":["glossary","declaration"],"text":"","title":"\u58f0\u660e"},{"location":"deep-copy/","tags":["\u8bcd\u6c47\u8868","deep-copy"],"text":"","title":"\u6df1\u62f7\u8d1d"},{"location":"default-constructor/","tags":["\u8bcd\u6c47\u8868","default-constructor"],"text":"","title":"\u9ed8\u8ba4\u6784\u9020\u51fd\u6570"},{"location":"default-initialization/","tags":["\u8bcd\u6c47\u8868","default-initialization"],"text":"","title":"\u9ed8\u8ba4\u521d\u59cb\u5316"},{"location":"default-member-initializer/","tags":["\u8bcd\u6c47\u8868","default-member-initializer"],"text":"","title":"\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c"},{"location":"definition/","tags":["glossary","definition"],"text":"","title":"\u5b9a\u4e49"},{"location":"delegating-constructors/","tags":["\u8bcd\u6c47\u8868","delegating-constructors"],"text":"","title":"\u59d4\u6258\u6784\u9020\u51fd\u6570"},{"location":"dependency/","tags":["\u8bcd\u6c47\u8868","dependency"],"text":"","title":"\u4f9d\u8d56"},{"location":"dereference-operator/","tags":["\u8bcd\u6c47\u8868","dereference-operator"],"text":"","title":"\u89e3\u5f15\u7528\u8fd0\u7b97\u7b26"},{"location":"derived-class/","tags":["\u8bcd\u6c47\u8868","derived-class"],"text":"","title":"\u6d3e\u751f\u7c7b"},{"location":"designated-initializers/","tags":["\u8bcd\u6c47\u8868","designated-initializers"],"text":"","title":"\u6307\u5b9a\u521d\u59cb\u5316"},{"location":"destructor/","tags":["\u8bcd\u6c47\u8868","destructor"],"text":"","title":"\u6790\u6784\u51fd\u6570"},{"location":"direct-initialization/","tags":["\u8bcd\u6c47\u8868","direct-initialization","initialization"],"text":"\u521d\u59cb\u5316 \u4f7f\u7528\u5706\u62ec\u53f7\u7684\u521d\u59cb\u5316","title":"\u76f4\u63a5\u521d\u59cb\u5316"},{"location":"downcasting/","tags":["\u8bcd\u6c47\u8868","downcasting"],"text":"\u5c06\u57fa\u7c7b\u6307\u9488\u8f6c\u6362\u4e3a\u6d3e\u751f\u7c7b\u6307\u9488","title":"\u5411\u4e0b\u8f6c\u6362"},{"location":"dumb-pointer/","tags":["\u8bcd\u6c47\u8868","dumb-pointer"],"text":"","title":"\u7b28\u6307\u9488"},{"location":"dynamic-array/","tags":["\u8bcd\u6c47\u8868","\u52a8\u6001\u6570\u7ec4"],"text":"","title":"\u52a8\u6001\u6570\u7ec4"},{"location":"dynamic-casts/","tags":["\u8bcd\u6c47\u8868","dynamic-casts"],"text":"","title":"\u52a8\u6001\u7c7b\u578b\u8f6c\u6362"},{"location":"dynamic-duration/","tags":["\u8bcd\u6c47\u8868","dynamic duration"],"text":"","title":"\u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4"},{"location":"dynamic-initialization/","tags":["\u8bcd\u6c47\u8868","dynamic-initialization"],"text":"","title":"\u52a8\u6001\u521d\u59cb\u5316"},{"location":"dynamic-memory-allocation/","tags":["\u8bcd\u6c47\u8868","dynamic-memory-allocation"],"text":"","title":"\u52a8\u6001\u5185\u5b58\u5206\u914d"},{"location":"enum-class/","tags":["\u8bcd\u6c47\u8868","enum-class"],"text":"\u679a\u4e3e\u7c7b\u4e0d\u662f\u7c7b\uff0c\u5b83\u662fenum class\u521b\u5efa\u7684 \u6709\u4f5c\u7528\u57df\u679a\u4e3e","title":"\u679a\u4e3e\u7c7b"},{"location":"evaluation/","tags":["\u8bcd\u6c47\u8868","evaluation"],"text":"","title":"\u6c42\u503c"},{"location":"exception-safety-issue/","tags":["\u8bcd\u6c47\u8868","exception-safety-issue"],"text":"","title":"\u5f02\u5e38\u5b89\u5168\u95ee\u9898"},{"location":"explicit-type-conversion/","tags":["\u8bcd\u6c47\u8868","explicit-type-conversion"],"text":"","title":"\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362"},{"location":"expression-type/","tags":["\u8bcd\u6c47\u8868","expression type"],"text":"","title":"\u8868\u8fbe\u5f0f\u7c7b\u578b"},{"location":"external-linkage/","tags":["\u8bcd\u6c47\u8868","external-linkage"],"text":"","title":"\u5916\u90e8\u94fe\u63a5"},{"location":"external-variable/","tags":["\u8bcd\u6c47\u8868","external-variable"],"text":"","title":"\u5916\u90e8\u53d8\u91cf"},{"location":"extraction-operator/","tags":["\u8bcd\u6c47\u8868","extraction-operator"],"text":"","title":"\u63d0\u53d6\u8fd0\u7b97\u7b26"},{"location":"fallthrough/","tags":["\u8bcd\u6c47\u8868","fallthrough"],"text":"","title":"\u8d2f\u7a7f\u5c5e\u6027"},{"location":"file-scope/","tags":["\u8bcd\u6c47\u8868","file-scope"],"text":"","title":"\u6587\u4ef6\u4f5c\u7528\u57df"},{"location":"fixed-array/","tags":["\u8bcd\u6c47\u8868","fixed-array"],"text":"","title":"\u56fa\u5b9a\u6570\u7ec4"},{"location":"format-group/","tags":["\u8bcd\u6c47\u8868","format-group"],"text":"","title":"\u683c\u5f0f\u7ec4"},{"location":"forward-declaration/","tags":["glossary","forward declaration"],"text":"","title":"\u524d\u5411\u58f0\u660e"},{"location":"friend-class/","tags":["\u8bcd\u6c47\u8868","friend-class"],"text":"","title":"\u53cb\u5143\u7c7b"},{"location":"friend-function/","tags":["\u8bcd\u6c47\u8868","friend-function"],"text":"","title":"\u53cb\u5143\u51fd\u6570"},{"location":"function-instance/","tags":["\u8bcd\u6c47\u8868","function-instance"],"text":"","title":"\u51fd\u6570\u5b9e\u4f8b"},{"location":"function-literal/","tags":["\u8bcd\u6c47\u8868","function-literal"],"text":"","title":"\u51fd\u6570\u5b57\u9762\u91cf"},{"location":"function-pointer/","tags":["\u8bcd\u6c47\u8868","function-pointer"],"text":"","title":"\u51fd\u6570\u6307\u9488"},{"location":"function-prototype/","tags":["\u8bcd\u6c47\u8868","\u51fd\u6570\u539f\u578b(function prototype)"],"text":"","title":"\u51fd\u6570\u539f\u578b"},{"location":"function-template-instantiation/","tags":["\u8bcd\u6c47\u8868","function-template-instantiation"],"text":"","title":"\u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316"},{"location":"function-template/","tags":["\u8bcd\u6c47\u8868","function-template"],"text":"\u7f16\u8bd1\u5668\u4f1a\u6839\u636e\u6240\u9700\u7684\u7c7b\u578b\u81ea\u52a8\u521b\u5efa\u5bf9\u5e94\u7684\u51fd\u6570","title":"\u51fd\u6570\u6a21\u677f"},{"location":"generic-algorithm/","tags":["\u8bcd\u6c47\u8868","generic-algorithm"],"text":"","title":"\u6cdb\u578b\u7b97\u6cd5"},{"location":"global-variable/","tags":["glossary","global variable"],"text":"","title":"\u5168\u5c40\u53d8\u91cf"},{"location":"going-out-of-scope/","tags":["\u8bcd\u6c47\u8868","\u79bb\u5f00\u4f5c\u7528\u57df"],"text":"","title":"\u79bb\u5f00\u4f5c\u7528\u57df"},{"location":"header-guard/","text":"title: \u5934\u6587\u4ef6\u9632\u8303 alias: \u5934\u6587\u4ef6\u9632\u8303 english:header-guard type: glossary tags: - glossary - header-guard \u4e5f\u79f0\u4e3a include \u9632\u8303 \u6216 \u5934\u6587\u4ef6\u9632\u5fa1\u5f0f\u58f0\u660e","title":"Header guard"},{"location":"heap/","tags":["\u8bcd\u6c47\u8868","heap"],"text":"","title":"\u5806"},{"location":"identifier/","tags":["glossary","identifier"],"text":"","title":"\u6807\u8bc6\u7b26(identifier)"},{"location":"identity/","tags":["\u8bcd\u6c47\u8868","identity"],"text":"","title":"\u8eab\u4efd\u7279\u5f81"},{"location":"immediate-functions/","tags":["\u8bcd\u6c47\u8868","immediate-functions"],"text":"","title":"\u5373\u65f6\u51fd\u6570"},{"location":"implicit-constructor/","tags":["\u8bcd\u6c47\u8868","implicit-constructor"],"text":"","title":"\u9690\u5f0f\u6784\u9020\u51fd\u6570"},{"location":"implicit-object/","tags":["\u8bcd\u6c47\u8868","implicit-object"],"text":"","title":"\u9690\u5f0f\u5bf9\u8c61"},{"location":"implicit-type-conversion/","tags":["\u8bcd\u6c47\u8868","implicit-type-conversion"],"text":"","title":"\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362"},{"location":"inheritance/","tags":["\u8bcd\u6c47\u8868","inheritance"],"text":"","title":"\u7ee7\u627f"},{"location":"initialization/","tags":["\u8bcd\u6c47\u8868","\u521d\u59cb\u5316"],"text":"C++ \u652f\u6301\u4e09\u79cd\u7c7b\u578b\u7684\u521d\u59cb\u5316\uff1a \u62f7\u8d1d\u521d\u59cb\u5316 \u3001 \u76f4\u63a5\u521d\u59cb\u5316 \u548c \u7edf\u4e00\u521d\u59cb\u5316 \uff08\u4e5f\u79f0\u4e3a \u5217\u8868\u521d\u59cb\u5316 \u6216 \u62ec\u53f7\u521d\u59cb\u5316 \uff09\u3002 \u805a\u5408\u521d\u59cb\u5316 \uff0c\u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u521d\u59cb\u5316\u4e00\u4e2a \u805a\u5408\u6570\u636e\u7c7b\u578b \uff0c\u662f \u5217\u8868\u521d\u59cb\u5316 \u7684\u4e00\u79cd\u5f62\u5f0f \u521d\u59cb\u5316\u503c\u5217\u8868 16.7 - std::initializer_list 0\u521d\u59cb\u5316 \u503c\u521d\u59cb\u5316 initializer \u6307\u5b9a\u521d\u59cb\u5316 \u9ed8\u8ba4\u521d\u59cb\u5316 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09","title":"\u521d\u59cb\u5316"},{"location":"initializer-list/","tags":["\u8bcd\u6c47\u8868","initializer-list"],"text":"\u7528\u4e8e \u805a\u5408\u521d\u59cb\u5316 \u666e\u901a\u7c7b\u4e0d\u9700\u8981\u521d\u59cb\u503c\u5217\u8868\uff0c\u5bb9\u5668\u7c7b\u9700\u8981\u3002\u5b9e\u73b0\u65f6\u9700\u8981\u7279\u6b8a\u7684\u6784\u9020\u51fd\u6570\u652f\u6301\u3002 \u6240\u4ee5\u521d\u59cb\u5316\u503c\u5217\u8868\u662f\u5bf9\u5bb9\u5668\u7c7b\u6765\u8bf4\u7684\u3002\u552f\u4e00\u7279\u4f8b\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e2d\u7684\u6570\u7ec4\uff0c\u56e0\u4e3a\u5b83\u4e5f\u662f\u5bb9\u5668 \u4e00\u822c\u63d0\u4f9b\u521d\u59cb\u5316\u503c\u5217\u8868\u6784\u9020\u51fd\u6570\u65f6\u4e5f\u8981\u63d0\u4f9b\u5bf9\u5e94\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u91cd\u8f7d \u5bf9\u4e8e\u4e00\u822c\u7c7b\u6765\u8bf4\u53ea\u6709 \u5217\u8868\u521d\u59cb\u5316 \u3002\u4f46\u662f\u6709 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u7684\u6982\u5ff5\uff0c\u662f\u6784\u9020\u51fd\u6570\u540e\u9762\u7684\u5217\u8868\u3002 1 2 3 4 5 6 7 //\u90fd\u662f\u5229\u7528\u805a\u5408\u521d\u59cb\u5316 std :: array < int , 5 > myArray = { 9 , 7 , 5 , 3 , 1 }; // \u521d\u59cb\u5316\u503c\u5217\u8868 std :: array < int , 5 > myArray2 { 9 , 7 , 5 , 3 , 1 }; // \u662f\u5217\u8868\u521d\u59cb\u5316\u5417\uff1f\u5e94\u8be5\u4e5f\u662f\u521d\u59cb\u5316\u503c\u5217\u8868\u8bed\u6cd5\uff0c\u8c03\u7528\u5bf9\u5e94\u7684\u6784\u9020\u51fd\u6570 std :: array < int , 3 > a1 { { 1 , 2 , 3 } }; // \u5728CPP14\u4e4b\u524d\u9700\u8981\u4f7f\u7528\u8fd9\u79cd\u5f62\u5f0f int prime [ 5 ]{ 2 , 3 , 5 , 7 , 11 }; int prime [ 5 ] = { 2 , 3 , 5 , 7 , 11 }; \u5bf9\u4e8e\u5bb9\u5668\u7c7b\uff0c\u4e0d\u5e94\u8be5\u4f7f\u7528\u4e00\u822c\u7684\u5217\u8868\u521d\u59cb\u5316\uff1f\u662f\u5426\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u521d\u59cb\u5316\u76f4\u63a5\u521d\u59cb\u5316\u5bb9\u5668\u7c7b\u4e2d\u7684\u6570\u636e\u6210\u5458\uff1f\u90a3\u6837\u4e5f\u5e94\u8be5\u4f7f\u7528\u53cc\u5c42\u62ec\u53f7\u5427\uff1f","title":"\u521d\u59cb\u5316\u503c\u5217\u8868"},{"location":"initializer/","tags":["\u8bcd\u6c47\u8868","\u521d\u59cb\u5316\u503c(initializer)"],"text":"","title":"\u521d\u59cb\u5316\u503c"},{"location":"inline-function/","tags":["\u8bcd\u6c47\u8868","inline-function"],"text":"","title":"\u5185\u8054\u51fd\u6570"},{"location":"inline-namespace/","tags":["\u8bcd\u6c47\u8868","inline-namespace"],"text":"","title":"\u5185\u8054\u547d\u540d\u7a7a\u95f4"},{"location":"inline-variables/","tags":["\u8bcd\u6c47\u8868","inline-variables"],"text":"","title":"\u5185\u8054\u53d8\u91cf"},{"location":"input-manipulators/","tags":["\u8bcd\u6c47\u8868","input-manipulators"],"text":"","title":"\u8f93\u5165\u64cd\u7eb5\u5668"},{"location":"insertion-operator/","tags":["\u8bcd\u6c47\u8868","insertion-operator"],"text":"","title":"\u63d2\u5165\u8fd0\u7b97\u7b26"},{"location":"instance/","tags":["\u8bcd\u6c47\u8868","instance"],"text":"","title":"\u5b9e\u4f8b"},{"location":"instantiated/","tags":["glossary","instantiated"],"text":"","title":"\u5b9e\u4f8b\u5316"},{"location":"instuction/","tags":["glossary","instuction"],"text":"","title":"\u6307\u4ee4"},{"location":"integral-type/","tags":["\u8bcd\u6c47\u8868","integral-type"],"text":"","title":"\u6574\u578b\u7c7b\u578b"},{"location":"interface-class/","tags":["\u8bcd\u6c47\u8868","interface-class"],"text":"","title":"\u63a5\u53e3\u7c7b"},{"location":"internal-linkage/","tags":["\u8bcd\u6c47\u8868","internal-linkage"],"text":"","title":"\u5185\u90e8\u94fe\u63a5"},{"location":"internal-variable/","tags":["\u8bcd\u6c47\u8868","internal-variable"],"text":"","title":"\u5185\u90e8\u53d8\u91cf"},{"location":"iterating/","tags":["\u8bcd\u6c47\u8868","iterating"],"text":"","title":"\u904d\u5386"},{"location":"iterator/","tags":["\u8bcd\u6c47\u8868","iterator"],"text":"","title":"\u8fed\u4ee3\u5668"},{"location":"lambda/","tags":["\u8bcd\u6c47\u8868","lambda"],"text":"","title":"\u533f\u540d\u51fd\u6570"},{"location":"link/","tags":["\u8bcd\u6c47\u8868","link"],"text":"","title":"\u94fe\u63a5"},{"location":"linkage/","tags":["\u8bcd\u6c47\u8868","linkage"],"text":"","title":"\u94fe\u63a5\u5c5e\u6027"},{"location":"linker/","tags":["glossary","linker"],"text":"","title":"\u94fe\u63a5\u5668(linker)"},{"location":"list%20assignment%20operator/","tags":["\u8bcd\u6c47\u8868","list assignment operator"],"text":"","title":"\u7c7b\u522b\u8d4b\u503c\u8fd0\u7b97\u7b26"},{"location":"list-constructor/","tags":["\u8bcd\u6c47\u8868","list-constructor"],"text":"","title":"\u5217\u8868\u6784\u9020\u51fd\u6570"},{"location":"list-initialization/","tags":["\u8bcd\u6c47\u8868","list-initialization","initialization"],"text":"\u548c \u7edf\u4e00\u521d\u59cb\u5316 \u3001 \u62ec\u53f7\u521d\u59cb\u5316 \u662f\u4e00\u56de\u4e8b\uff0c\u548c \u521d\u59cb\u5316\u503c\u5217\u8868 \u6709\u4ec0\u4e48\u533a\u522b 1 2 std :: array < int , 5 > myArray = { 9 , 7 , 5 , 3 , 1 }; // \u521d\u59cb\u5316\u503c\u5217\u8868 std :: array < int , 5 > myArray2 { 9 , 7 , 5 , 3 , 1 }; // \u5217\u8868\u521d\u59cb\u5316 https://en.cppreference.com/w/cpp/language/list_initialization","title":"\u5217\u8868\u521d\u59cb\u5316"},{"location":"literals/","tags":["\u8bcd\u6c47\u8868","literals"],"text":"","title":"\u5b57\u9762\u91cf"},{"location":"local-variable/","text":"title: \u5c40\u90e8\u53d8\u91cf alias: \u5c40\u90e8\u53d8\u91cf english:local-variable type: glossary tags: - glossary - local-variable","title":"Local variable"},{"location":"lvalue-reference/","tags":["\u8bcd\u6c47\u8868","lvalue-reference"],"text":"","title":"\u5de6\u503c\u5f15\u7528"},{"location":"lvalue/","tags":["\u8bcd\u6c47\u8868","lvalue"],"text":"","title":"\u5de6\u503c"},{"location":"macro/","tags":["glossary","macro"],"text":"","title":"\u5b8f"},{"location":"member-access-operator/","tags":["\u8bcd\u6c47\u8868","member access operator"],"text":"\u5bf9\u4e8e\u5bf9\u8c61\u662f . \uff0c\u5bf9\u4e8e\u6307\u9488\u662f ->","title":"\u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26"},{"location":"member-function/","tags":["\u8bcd\u6c47\u8868","member-function"],"text":"","title":"\u6210\u5458\u51fd\u6570"},{"location":"member-initializer-list/","tags":["\u8bcd\u6c47\u8868","member-initializer-list"],"text":"1 2 3 4 Something () : m_value1 { 1 }, m_value2 { 2.2 }, m_value3 { 'c' } // Initialize our member variables { // No need for assignment here }","title":"\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868"},{"location":"member-selection-operator/","tags":["\u8bcd\u6c47\u8868","member-selection-operator"],"text":"","title":"\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26"},{"location":"member-types/","tags":["\u8bcd\u6c47\u8868","member-types"],"text":"\u5d4c\u5957\u5728\u7c7b\u4e2d\u7684\u7c7b\u578b","title":"\u6210\u5458\u7c7b\u578b"},{"location":"member-variable/","tags":["\u8bcd\u6c47\u8868","member-variable"],"text":"","title":"\u6210\u5458\u53d8\u91cf"},{"location":"memberwise%20initialization/","tags":["\u8bcd\u6c47\u8868","memberwise initialization"],"text":"","title":"\u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316"},{"location":"memberwise-assignment/","tags":["\u8bcd\u6c47\u8868","memberwise-assignment"],"text":"","title":"\u6210\u5458\u4f9d\u6b21\u8d4b\u503c"},{"location":"memberwise-copy/","tags":["\u8bcd\u6c47\u8868","memberwise-copy"],"text":"","title":"\u6210\u5458\u4f9d\u6b21\u62f7\u8d1d"},{"location":"memory-leak/","tags":["\u8bcd\u6c47\u8868","memory-leak"],"text":"","title":"\u5185\u5b58\u6cc4\u6f0f"},{"location":"mixin/","tags":["\u8bcd\u6c47\u8868","mixin"],"text":"","title":null},{"location":"move-assignment-operator/","tags":["\u8bcd\u6c47\u8868","move assignment operator"],"text":"","title":"\u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26"},{"location":"move-constructor/","tags":["\u8bcd\u6c47\u8868","move-constructor"],"text":"","title":"\u79fb\u52a8\u6784\u9020\u51fd\u6570"},{"location":"move-semantics/","tags":["\u8bcd\u6c47\u8868","move-semantics"],"text":"","title":"\u79fb\u52a8\u8bed\u4e49"},{"location":"named-cast/","tags":["\u8bcd\u6c47\u8868","named-cast"],"text":"","title":"\u5177\u540d\u7c7b\u578b\u8f6c\u6362"},{"location":"namespace/","tags":["\u8bcd\u6c47\u8868","namespace"],"text":"","title":"\u547d\u540d\u7a7a\u95f4"},{"location":"narrowing-convertions/","tags":["\u8bcd\u6c47\u8868","narrowing-convertions"],"text":"","title":"\u7f29\u7a84\u8f6c\u6362"},{"location":"non-static-member-initialization/","tags":["\u8bcd\u6c47\u8868","\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316"],"text":"\u5728\u5b9a\u4e49\u7ed3\u6784\u4f53\uff08\u6216\u7c7b\uff09\u7c7b\u578b\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u6210\u5458\u90fd\u63d0\u4f9b\u9ed8\u8ba4\u521d\u59cb\u5316\u503c\u4f5c\u4e3a\u5176\u5b9a\u4e49\u7684\u4e00\u90e8\u5206\u3002\u8be5\u503c\u79f0\u4e3a \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c \u3002","title":"\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316"},{"location":"numeric%20promotions/","tags":["\u8bcd\u6c47\u8868","numeric promotions"],"text":"","title":"\u6570\u503c\u63d0\u5347"},{"location":"numeric-conversions/","tags":["\u8bcd\u6c47\u8868","numeric-conversions"],"text":"","title":"\u6570\u503c\u8f6c\u6362"},{"location":"object-composition/","tags":["\u8bcd\u6c47\u8868","object-composition"],"text":"","title":"\u5bf9\u8c61\u7ec4\u5408"},{"location":"object-like-macros/","tags":["glossary","object-like-macros"],"text":"","title":"\u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f"},{"location":"object-slicing/","tags":["\u8bcd\u6c47\u8868","object-slicing"],"text":"\u5c06\u6d3e\u751f\u7c7b\u5bf9\u8c61\u8d4b\u503c\u7ed9\u57fa\u7c7b\u5bf9\u8c61\u79f0\u4e3a\uff0c\u56e0\u4e3a\u88ab\u8d4b\u503c\u7684\u57fa\u7c7b\u5bf9\u8c61\u53ea\u80fd\u63a5\u6536\u6d3e\u751f\u7c7b\u4e2d\u7684\u57fa\u7c7b\u90e8\u5206\uff0c\u6d3e\u751f\u7c7b\u90e8\u5206\u88ab\u201d\u5207\u6389\u4e86\u201c","title":"\u5bf9\u8c61\u5207\u7247"},{"location":"object/","tags":["\u8bcd\u6c47\u8868","\u5bf9\u8c61(objec)"],"text":"","title":"\u5bf9\u8c61"},{"location":"one-definition-rule/","tags":["glossary","one-definition-rule"],"text":"wiki link \u4e0d\u4f1a\u9650\u5236\u7c7b\u6a21\u677f\u5b9a\u4e49","title":"\u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule)"},{"location":"operands/","tags":["\u8bcd\u6c47\u8868","operands"],"text":"","title":"\u64cd\u4f5c\u6570"},{"location":"operator/","tags":["\u8bcd\u6c47\u8868","operator"],"text":"","title":"\u8fd0\u7b97\u7b26"},{"location":"optional-parameter/","tags":["optional-parameter"],"text":"Key Takeaway","title":"\u53ef\u9009\u53c2\u6570"},{"location":"out-of-scope/","tags":["\u8bcd\u6c47\u8868","\u8d85\u51fa\u4f5c\u7528\u57df(out of scope)"],"text":"","title":"\u8d85\u51fa\u4f5c\u7528\u57df"},{"location":"overload-resolution/","tags":["\u8bcd\u6c47\u8868","overload-resolution"],"text":"","title":"\u91cd\u8f7d\u89e3\u6790"},{"location":"overload/","tags":["\u8bcd\u6c47\u8868","overload"],"text":"","title":"\u91cd\u8f7d"},{"location":"override/","tags":["\u8bcd\u6c47\u8868","override"],"text":"\u533a\u5206\u4e8e \u91cd\u8f7d \uff0c\u91cd\u5199\u662f\u7b7e\u540d\u8981\u76f8\u540c\uff08\u6d3e\u751f\u7c7b\u4e2d\uff09\uff0c\u91cd\u8f7d\u662f\u7b7e\u540d\u4e0d\u540c\uff08\u540c\u7c7b\u4e2d\uff09","title":"\u91cd\u5199"},{"location":"parameters/","tags":["glossary","parameters"],"text":"\u5b9a\u4e49\u51fd\u6570\u65f6\u4f7f\u7528\u7684\u53c2\u6570\u3002 \u5b9e\u53c2 \u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u5f0f\u5411\u5f62\u53c2\u4f20\u9012\uff1a 1. \u6309\u503c\u4f20\u9012 \u65f6\uff0c\u5f62\u53c2\u5f97\u5230\u4e00\u4efd\u5b9e\u53c2\u7684\u62f7\u8d1d 2. \u6309\u5f15\u7528\u4f20\u9012 \u65f6\uff0c\u5f62\u53c2\uff08\u58f0\u660e\u4e3a\u5f15\u7528\u7c7b\u578b\uff09\u88ab\u7ed1\u5b9a\u5230\u5b9e\u53c2\uff0c\u4ece\u800c\u53ef\u4ee5\u4fee\u6539\u5b9e\u53c2 3. \u6309\u5730\u5740\u4f20\u9012 \u662f\uff0c\u5f62\u53c2\uff08\u58f0\u660e\u4e3a\u6307\u9488\u7c7b\u578b\uff09\u83b7\u5f97\u5f62\u53c2\u6301\u6709\u5730\u5740\u7684\u62f7\u8d1d\uff0c\u548c\u5b9e\u53c2\u6307\u5411\u540c\u4e00\u4e2a\u5730\u5740\uff0c\u53ef\u4ee5\u4fee\u6539\u8be5\u5730\u5740\u5b58\u653e\u7684\u503c\u3002\u4f46\u662f\u5730\u5740\u672c\u8eab\u662f\u5b9e\u53c2\u7684\u62f7\u8d1d\uff0c\u6240\u4ee5\u4fee\u6539\u5f62\u53c2\u7684\u503c\uff08\u5730\u5740\uff09\u4e0d\u4f1a\u5f71\u54cd\u5230\u5b9e\u53c2\uff0c\u5b9e\u53c2\u4ecd\u7136\u6301\u6709\u539f\u5730\u5740\u3002","title":"\u5f62\u53c2"},{"location":"parent-class/","tags":["\u8bcd\u6c47\u8868","parent-class"],"text":"","title":"\u7236\u7c7b"},{"location":"pass-by-address/","tags":["\u8bcd\u6c47\u8868","pass-by-address"],"text":"","title":"\u6309\u5730\u5740\u4f20\u9012"},{"location":"pass-by-reference/","tags":["\u8bcd\u6c47\u8868","pass-by-reference"],"text":"","title":"\u6309\u5f15\u7528\u4f20\u9012"},{"location":"pass-by-value/","tags":["glossary","pass-by-value"],"text":"","title":"\u6309\u503c\u4f20\u9012"},{"location":"placeholder-types/","tags":["\u8bcd\u6c47\u8868","placeholder-types"],"text":"","title":"\u5360\u4f4d\u7b26\u7c7b\u578b"},{"location":"pointer-to-const/","tags":["\u8bcd\u6c47\u8868","pointer-to-const"],"text":"","title":"\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488"},{"location":"polymorphism/","tags":["\u8bcd\u6c47\u8868","polymorphism"],"text":"","title":"\u591a\u6001"},{"location":"preprocessor-directive/","tags":["glossary","preprocessor"],"text":"","title":"\u9884\u5904\u7406\u5668\u6307\u4ee4"},{"location":"preprocessor/","tags":["glossary","preprocessor"],"text":"","title":"\u9884\u5904\u7406\u5668"},{"location":"private-inheritance/","tags":["\u8bcd\u6c47\u8868","private-inheritance"],"text":"","title":"\u79c1\u6709\u7ee7\u627f"},{"location":"private-member/","tags":["\u8bcd\u6c47\u8868","private-member"],"text":"","title":"\u79c1\u6709\u6210\u5458"},{"location":"protected-inheritance/","tags":["\u8bcd\u6c47\u8868","protected-inheritance"],"text":"","title":"\u53d7\u4fdd\u62a4\u7ee7\u627f"},{"location":"protected-members/","tags":["\u8bcd\u6c47\u8868","protected-members"],"text":"","title":"\u53d7\u4fdd\u62a4\u6210\u5458"},{"location":"public-inheritance/","tags":["\u8bcd\u6c47\u8868","public-inheritance"],"text":"","title":"\u516c\u5f00\u7ee7\u627f"},{"location":"public-member/","tags":["\u8bcd\u6c47\u8868","public-member"],"text":"\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u7684\u7ed3\u6784\u6216\u7c7b\u7684\u6210\u5458","title":"\u516c\u6709\u6210\u5458"},{"location":"pure-declaration/","tags":["glossary","declaration","pure-declaration"],"text":"\u76f8\u5173\u6982\u5ff5\uff1a \u58f0\u660e(declaration)","title":"\u7eaf\u58f0\u660e(pure-declaration)"},{"location":"pure-virtual/","tags":["\u8bcd\u6c47\u8868","pure-virtual"],"text":"","title":"\u7eaf\u865a\u51fd\u6570"},{"location":"qualifier/","tags":["glossary","qualifier"],"text":"\u4e5f\u79f0\u4e3a\u4fee\u9970\u7b26\u3002\u672c\u9879\u76ee\u7ffb\u8bd1\u65f6\u53c2\u8003\u4e86\u4faf\u6377\u8001\u5e08\u5728Essential C++ \u4e2d\u6587\u7248\u4e2d\u7684\u8bd1\u6cd5\u3002","title":"\u9650\u5b9a\u7b26"},{"location":"quiz/","text":"\u9762\u8bd5\u516b\u80a1\u6587","title":"Quiz"},{"location":"random-access-memory/","tags":["\u8bcd\u6c47\u8868","random-access-memory"],"text":"","title":"\u968f\u673a\u8bbf\u95ee\u5185\u5b58"},{"location":"range-based-for-loops/","tags":["\u8bcd\u6c47\u8868","range-based-for-loops"],"text":"\u6240\u6709\u5177\u6709 begin() \u548c end() \u6210\u5458\u51fd\u6570\u7684\u7c7b\u578b\uff0c\u6216\u8005\u53ef\u4ee5\u914d\u5408 std::begin() \u548c std::end() \u4f7f\u7528\u7684\u7c7b\u578b\uff0c\u90fd\u53ef\u4ee5\u7528\u4e8e\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af\u3002","title":"\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af"},{"location":"raw-pointer/","tags":["\u8bcd\u6c47\u8868","raw-pointer"],"text":"","title":"\u539f\u59cb\u6307\u9488"},{"location":"refactoring/","tags":["glossary","refactoring"],"text":"","title":"\u91cd\u6784"},{"location":"reference-container/","tags":["\u8bcd\u6c47\u8868","reference-container"],"text":"","title":"\u5f15\u7528\u5bb9\u5668"},{"location":"reflexive%20association/","tags":["\u8bcd\u6c47\u8868","reflexive association"],"text":"","title":"\u53cd\u8eab\u5173\u8054"},{"location":"reinterpret-casts/","tags":["\u8bcd\u6c47\u8868","reinterpret-casts"],"text":"","title":"\u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362"},{"location":"return-by-address/","tags":["\u8bcd\u6c47\u8868","return-by-address"],"text":"","title":"\u6309\u5730\u5740\u8fd4\u56de"},{"location":"return-by-reference/","tags":["\u8bcd\u6c47\u8868","return-by-reference"],"text":"","title":"\u6309\u5f15\u7528\u8fd4\u56de"},{"location":"return-by-value/","tags":["glossary","return-by-value"],"text":"","title":"\u6309\u503c\u8fd4\u56de"},{"location":"return-type-deduction/","tags":["\u8bcd\u6c47\u8868","return-type-deduction"],"text":"\u7c7b\u578b\u63a8\u65ad \u7684\u4e00\u79cd\uff0c\u4e00\u822c\u4e0d\u63a8\u8350\u4f7f\u7528\u3002\u4f7f\u7528\u65f6\u9700\u8981\u786e\u4fdd\u6240\u6709\u7684\u8fd4\u56de\u8bed\u53e5\u8fd4\u56de\u76f8\u540c\u7684\u7c7b\u578b\u3002 \u5728 \u533f\u540d\u51fd\u6570 \u4e2d\u7ecf\u5e38\u4f1a\u4f7f\u7528","title":"\u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad"},{"location":"rounding-error/","tags":["\u8bcd\u6c47\u8868","rounding-error"],"text":"","title":"\u820d\u5165\u8bef\u5dee"},{"location":"runtime/","tags":["glossary","runtime"],"text":"","title":"\u8fd0\u884c\u65f6"},{"location":"rvalue-reference/","tags":["\u8bcd\u6c47\u8868","rvalue-reference"],"text":"","title":"\u53f3\u503c\u5f15\u7528"},{"location":"rvalue/","tags":["\u8bcd\u6c47\u8868","rvalue"],"text":"\u6ce8\u610f\uff0c\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u662f\u5de6\u503c\uff0c\u56e0\u4e3a\u5b83\u5360\u7528\u5185\u5b58\u3002\u80cc\u666f\u662f\u4e3a\u4e86\u517c\u5bb9C\u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32","title":"\u53f3\u503c"},{"location":"scaling/","tags":["\u8bcd\u6c47\u8868","scaling"],"text":"","title":"\u6307\u9488\u7f29\u653e"},{"location":"scope-resolution-operator/","tags":["glossary","scope-resolution-operator"],"text":"::","title":"\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26"},{"location":"scope/","tags":["glossary","scope"],"text":"","title":"\u4f5c\u7528\u57df"},{"location":"scoped-enumerations/","tags":["\u8bcd\u6c47\u8868","scoped-enumerations"],"text":"","title":"\u6709\u4f5c\u7528\u57df\u679a\u4e3e"},{"location":"segment/","tags":["\u8bcd\u6c47\u8868","segment"],"text":"","title":"\u5185\u5b58\u6bb5"},{"location":"sentinel/","tags":["\u8bcd\u6c47\u8868","sentinel"],"text":"","title":"\u54e8\u5175"},{"location":"shadow/","tags":["\u8bcd\u6c47\u8868","shadow"],"text":"","title":"\u53d8\u91cf\u906e\u853d"},{"location":"shallow-copy/","tags":["\u8bcd\u6c47\u8868","shallow-copy"],"text":"","title":"\u6d45\u62f7\u8d1d"},{"location":"side-effects/","tags":["\u8bcd\u6c47\u8868","side-effects"],"text":"","title":"\u526f\u4f5c\u7528"},{"location":"signature/","tags":["\u8bcd\u6c47\u8868","signature"],"text":"","title":"\u7b7e\u540d"},{"location":"smart%20pointer%20class/","tags":["\u8bcd\u6c47\u8868","smart pointer class"],"text":"","title":"\u667a\u80fd\u6307\u9488\u7c7b"},{"location":"smart-pointer/","tags":["\u8bcd\u6c47\u8868","smart-pointer"],"text":"","title":"\u667a\u80fd\u6307\u9488"},{"location":"snake-case/","tags":["\u8bcd\u6c47\u8868","\u86c7\u5f62\u547d\u540d\u6cd5(snake_case)"],"text":"","title":"\u86c7\u5f62\u547d\u540d\u6cd5"},{"location":"stack-frame/","tags":["\u8bcd\u6c47\u8868","stack-frame"],"text":"","title":"\u6808\u5e27"},{"location":"stack-overflow/","tags":["\u8bcd\u6c47\u8868","stack-overflow"],"text":"","title":"\u5806\u6808\u6ea2\u51fa"},{"location":"stack/","tags":["\u8bcd\u6c47\u8868","stack"],"text":"","title":"\u6808"},{"location":"standard-stream/","tags":["\u8bcd\u6c47\u8868","standard-stream"],"text":"","title":"\u6807\u51c6\u6d41"},{"location":"statement/","tags":["glossary","statement"],"text":"","title":"\u8bed\u53e5"},{"location":"static-casts/","tags":["\u8bcd\u6c47\u8868","static-casts"],"text":"","title":"\u9759\u6001\u7c7b\u578b\u8f6c\u6362"},{"location":"static-initialization/","tags":["\u8bcd\u6c47\u8868","static-initialization"],"text":"","title":"\u9759\u6001\u521d\u59cb\u5316"},{"location":"static-member-functions/","tags":["\u8bcd\u6c47\u8868","static-member-functions"],"text":"","title":"\u9759\u6001\u6210\u5458\u51fd\u6570"},{"location":"static-member-variables/","tags":["\u8bcd\u6c47\u8868","static-member-variables"],"text":"","title":"\u9759\u6001\u6210\u5458\u53d8\u91cf"},{"location":"static-memory-allocation/","tags":["\u8bcd\u6c47\u8868","static-memory-allocation"],"text":"","title":"\u9759\u6001\u5185\u5b58\u5206\u914d"},{"location":"static-storage-duration/","tags":["\u8bcd\u6c47\u8868","static-storage-duration"],"text":"","title":"\u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4"},{"location":"static-variables/","tags":["\u8bcd\u6c47\u8868","static-variables"],"text":"","title":"\u9759\u6001\u53d8\u91cf"},{"location":"storage-duration/","tags":["\u8bcd\u6c47\u8868","storage-duration"],"text":"","title":"\u5b58\u50a8\u6301\u7eed\u65f6\u95f4"},{"location":"strong-except/","tags":["\u8bcd\u6c47\u8868","strong-except"],"text":"","title":"\u5f3a\u5f02\u5e38\u4fdd\u8bc1"},{"location":"struct-padding/","tags":["\u8bcd\u6c47\u8868","struct-padding"],"text":"","title":"\u7ed3\u6784\u4f53\u586b\u5145"},{"location":"stub/","text":"title:\u6253\u6869 alias: \u6253\u6869 english:stub type: glossary tags: - glossary - stub","title":"Stub"},{"location":"subscripts/","tags":["\u8bcd\u6c47\u8868","subscripts"],"text":"","title":"\u4e0b\u6807"},{"location":"super-class/","tags":["\u8bcd\u6c47\u8868","super-class"],"text":"","title":"\u8d85\u7c7b"},{"location":"symbolic-constants/","tags":["glossary","symbolic-constants"],"text":"","title":"\u7b26\u53f7\u5e38\u91cf"},{"location":"syntax/","tags":["\u8bcd\u6c47\u8868","\u8bed\u6cd5(syntax)"],"text":"","title":"\u8bed\u6cd5"},{"location":"tabledata/","text":"Category Meaning Implementated in C++ by Conditional statements Conditional statements cause a sequence of code to execute only if some condition is met. If, switch Jumps Jumps tell the CPU to start executing the statements at some other location. Goto, break, continue Function calls Function calls are jumps to some other location and back. Function calls, return Loops Loops tell the program to repeatedly execute some sequence of code zero or more times, until some condition is met. While, do-while, for, ranged-for Halts Halts tell the program to quit running. std::exit(), std::abort() Exceptions Exceptions are a special kind of flow control structure designed for error handling. Try, throw, catch","title":"Tabledata"},{"location":"tags/","text":"Tags Associations \u5173\u8054 Associative-containers \u5173\u8054\u5bb9\u5668 Binding \u7ed1\u5b9a C++11 11.13 - for-each \u5faa\u73af 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb 4.11 - \u5b57\u7b26 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 M.7 \u2014 std::shared_ptr C++14 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 4.15 - \u5b57\u9762\u91cf 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad M.6 \u2014 std::unique_ptr M.7 \u2014 std::shared_ptr C++17 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 11.16 \u2014 \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb 11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 4.18 - std::string_view \u7b80\u4ecb 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 7.17 - \u65ad\u8a00\u548c static_assert M.6 \u2014 std::unique_ptr M.7 \u2014 std::shared_ptr C++20 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 11.13 - for-each \u5faa\u73af 11.19 \u2014 \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 6.14 - Constexpr \u548c consteval \u51fd\u6570 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad M.7 \u2014 std::shared_ptr C-style-casts C \u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362 CTAD 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD Early binding \u65e9\u671f\u7ed1\u5b9a Encapsulation \u5c01\u88c5 Expression-parameters \u8868\u8fbe\u5f0f\u53c2\u6570 Frankenobject \u7f1d\u5408\u602a\u5bf9\u8c61 Function templates 8.13 - \u51fd\u6570\u6a21\u677f Late binding \u540e\u671f\u7ed1\u5b9a Lvalue 9.3 - \u5de6\u503c\u5f15\u7528 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 Multiple inheritance \u591a\u91cd\u7ee7\u627f Name-mangling \u540d\u79f0\u4fee\u9970 Numeral systems 4.16 - \u6570\u503c\u7cfb\u7edf\uff08\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236\uff09 Off-by-one \u5dee\u4e00\u9519\u8bef Output-manipulators \u8f93\u51fa\u64cd\u7eb5\u5668 Pointer arithmetic 11.9 - \u6307\u9488\u8fd0\u7b97\u548c\u6570\u7ec4\u7d22\u5f15 R-value-references M.2 - \u53f3\u503c\u5f15\u7528 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c RAII (Resource Acquisition Is Initialization) \u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09 Run-time-type-information-RTTI \u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f(RTTI) Sequence-containers \u987a\u5e8f\u5bb9\u5668 Syntax 3.1 - \u8bed\u6cd5\u548c\u8bed\u4e49\u9519\u8bef Template-specialization \u6a21\u677f\u7279\u5316 abbreviated function templates \u7f29\u5199\u51fd\u6570\u6a21\u677f abstract-base-classes 18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b abstraction \u62bd\u8c61 access specifier 13.3 - \u8bbf\u95ee\u4fee\u9970\u7b26 public \u548c private access-function \u6210\u5458\u8bbf\u95ee\u51fd\u6570 access-specifiers 17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 address 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de 9.9 - \u6309\u5730\u5740\u4f20\u9012 address-of-operator \u53d6\u5730\u5740\u64cd\u4f5c\u7b26 & aggregate initialization 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 aggregate-data-type \u805a\u5408\u6570\u636e\u7c7b\u578b aggregate-initialization \u805a\u5408\u521d\u59cb\u5316 aggregation 16.3 - \u805a\u5408\u5173\u7cfb \u805a\u5408 algorithms 11.19 \u2014 \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb alias 8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d ambiguous-match \u4e0d\u660e\u786e\u5339\u914d anonymous-object \u533f\u540d\u5bf9\u8c61 argument 12.5 - \u547d\u4ee4\u884c\u53c2\u6570 2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2 arguments \u5b9e\u53c2 arithmetic conversions 8.4 - \u7b97\u672f\u8f6c\u6362 arity \u5143\u6570 array 11.1 - \u6570\u7ec4\uff08\u7b2c\u4e00\u90e8\u5206\uff09 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.3 - \u6570\u7ec4\u548c\u5faa\u73af 11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f 11.5 - \u591a\u7ef4\u6570\u7ec4 11.8 - \u6307\u9488\u548c\u6570\u7ec4 arrays 11.12 - \u52a8\u6001\u5206\u914d\u6570\u7ec4 assert 7.17 - \u65ad\u8a00\u548c static_assert assignment 14.15 - \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26 association 16.4 - \u5173\u8054\u5173\u7cfb auto 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad automatic-memory-allocation \u81ea\u52a8\u5185\u5b58\u5206\u914d automatic-storage-duration \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 base-class \u57fa\u7c7b bit O.1 - bit\u6807\u8bb0\u548c\u901a\u8fc7std::bitset\u64cd\u4f5cbit O.2 - \u4f4d\u8fd0\u7b97\u7b26 O.3 - \u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u548c\u63a9\u7801\u64cd\u4f5c\u4f4d O.4 - \u4e8c\u8fdb\u5236\u548c\u5341\u8fdb\u5236\u8f6c\u6362 \u4f4d bitwise-and None bitwise-or \u6309\u4f4d\u6216 bool 4.9 - \u5e03\u5c14\u503c boolean 4.10 - if \u8bed\u53e5\u7b80\u4ecb bss segment bss\u6bb5 byte \u5b57\u8282 c-style 11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 call stack 3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808 call-stack \u8c03\u7528\u6808 callable-object \u53ef\u8c03\u7528\u5bf9\u8c61 callback functions \u56de\u8c03\u51fd\u6570 char 4.11 - \u5b57\u7b26 child-class \u5b50\u7c7b circular dependency M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898 class 13.11 - \u7c7b\u4ee3\u7801\u548c\u5934\u6587\u4ef6 13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570 13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf 13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570 13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b 13.16 - \u533f\u540d\u5bf9\u8c61 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b 13.18 - \u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 13.2 - \u7c7b\u548c\u7c7b\u6210\u5458 class template 10.10 - \u7c7b\u6a21\u677f 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD class-template \u7c7b\u6a21\u677f class-template-argument-deduction \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad class-type \u7c7b\u7c7b\u578b closure \u95ed\u5305 code coverage 7.13 - \u4ee3\u7801\u8986\u76d6\u7387 code segment \u4ee3\u7801\u6bb5 comments 1.2 - \u6ce8\u91ca compile-time \u7f16\u8bd1\u65f6 compiler \u7f16\u8bd1\u5668 composite-types \u7ec4\u5408\u7c7b\u578b composition 16.2 - \u7ec4\u5408\u5173\u7cfb compound data types 9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b const 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 9.8 - \u6307\u9488\u548cconst const class 13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570 const-cast const \u7c7b\u578b\u8f6c\u6362 const-member-function const \u6210\u5458\u51fd\u6570 const-pointer \u5e38\u91cf\u6307\u9488 constant expression \u5e38\u91cf\u8868\u8fbe\u5f0f constants 4.15 - \u5b57\u9762\u91cf consteval 6.14 - Constexpr \u548c consteval \u51fd\u6570 constexpr 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr 6.14 - Constexpr \u548c consteval \u51fd\u6570 constructor 13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570 14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570 constructors 13.5 - \u6784\u9020\u51fd\u6570 13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 container classes 16.6 - \u5bb9\u5668\u7c7b container-adapter \u5bb9\u5668\u9002\u914d\u5668 container-class \u5bb9\u5668\u7c7b containers 21.2 - STL\u5bb9\u5668 21.3 - STL\u8fed\u4ee3\u5668 21.4 - STL\u7b97\u6cd5 control flow 7.1 - \u63a7\u5236\u6d41 7.4 - switch \u8bed\u53e5\u57fa\u7840 conversions 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 8.3 - \u6570\u503c\u8f6c\u6362 8.4 - \u7b97\u672f\u8f6c\u6362 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast converting constructors 14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57 converting-constructors \u8f6c\u6362\u6784\u9020\u51fd\u6570 copy constructor 14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570 copy initialization 14.13 - \u62f7\u8d1d\u521d\u59cb\u5316 copy-constructor \u62f7\u8d1d\u6784\u9020\u51fd\u6570 copy-constructors \u62f7\u8d1d\u6784\u9020\u51fd\u6570 copy-initialization \u62f7\u8d1d\u521d\u59cb\u5316 copy-semantics \u62f7\u8d1d\u8bed\u4e49 covariant \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b covariant-return-types 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b dangling \u60ac\u5782 data segment \u6570\u636e\u6bb5 data type 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb 4.4 - \u6709\u7b26\u53f7\u6574\u578b 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 4.8 - \u6d6e\u70b9\u6570 4.9 - \u5e03\u5c14\u503c 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad debugger 3.6 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u5355\u6b65\u8c03\u8bd5 3.7 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8fd0\u884c\u548c\u65ad\u70b9 3.8 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u76d1\u63a7\u53d8\u91cf 3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808 debugging 3.2 - \u4ee3\u7801\u8c03\u8bd5\u6b65\u9aa4 3.3 - \u4ee3\u7801\u8c03\u8bd5\u7b56\u7565 3.4 \u2014 \u57fa\u672c\u4ee3\u7801\u8c03\u8bd5\u6280\u672f 3.5 - \u66f4\u591a\u4ee3\u7801\u8c03\u8bd5\u6280\u672f declaration 6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e \u58f0\u660e \u7eaf\u58f0\u660e(pure-declaration) deep copy 14.16 - \u6d45\u62f7\u8d1d\u548c\u6df1\u62f7\u8d1d 14.17 - \u91cd\u8f7d\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u6a21\u677f deep-copy \u6df1\u62f7\u8d1d default 8.12 - \u9ed8\u8ba4\u53c2\u6570 default-constructor \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 default-initialization \u9ed8\u8ba4\u521d\u59cb\u5316 default-member-initializer \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c definition \u5b9a\u4e49 delegating 13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570 delegating-constructors \u59d4\u6258\u6784\u9020\u51fd\u6570 delete 11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d 14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57 dependencies 16.5 - \u4f9d\u8d56\u5173\u7cfb dependency \u4f9d\u8d56 dereference-operator \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 derived-class \u6d3e\u751f\u7c7b desgin 2.13 - \u5982\u4f55\u8bbe\u8ba1\u7b2c\u4e00\u4e2a\u7a0b\u5e8f designated-initializers \u6307\u5b9a\u521d\u59cb\u5316 destructor \u6790\u6784\u51fd\u6570 destructors 13.9 - \u6790\u6784\u51fd\u6570 direct-initialization \u76f4\u63a5\u521d\u59cb\u5316 do-while 7.8 - do-while \u8bed\u53e5 downcasting \u5411\u4e0b\u8f6c\u6362 dumb-pointer \u7b28\u6307\u9488 dynamic casting 18.10 - \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 dynamic duration \u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 dynamic-casts \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 dynamic-initialization \u52a8\u6001\u521d\u59cb\u5316 dynamic-memory 11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d dynamic-memory-allocation \u52a8\u6001\u5185\u5b58\u5206\u914d early-binding 18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a ellipsis 12.6 - \u7701\u7565\u53f7\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 encapsulation 13.4 - \u6210\u5458\u8bbf\u95ee\u51fd\u6570\u4e0e\u5c01\u88c5 enum 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b 10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 enum-class \u679a\u4e3e\u7c7b error handling 7.15 - \u53d1\u73b0\u9519\u8bef\u548c\u5904\u7406\u9519\u8bef 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406 evaluation \u6c42\u503c excalidraw Drawing 2023 02 11 13.33.50.excalidraw Template.excalidraw C++ basic onepage Moc onepage exception-safety-issue \u5f02\u5e38\u5b89\u5168\u95ee\u9898 explicit 14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57 explicit-type-conversion 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 expression 1.10 - \u8868\u8fbe\u5f0f\u7b80\u4ecb expression type \u8868\u8fbe\u5f0f\u7c7b\u578b external-linkage \u5916\u90e8\u94fe\u63a5 external-variable \u5916\u90e8\u53d8\u91cf extraction-operator \u63d0\u53d6\u8fd0\u7b97\u7b26 fallthrough \u8d2f\u7a7f\u5c5e\u6027 file 23.6 - \u57fa\u672c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa file-scope \u6587\u4ef6\u4f5c\u7528\u57df final 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b fixed-array \u56fa\u5b9a\u6570\u7ec4 fixed-width 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t float 4.8 - \u6d6e\u70b9\u6570 float point 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 for 7.7 - \u5faa\u73af\u548c while \u8bed\u53e5 7.9 - for \u8bed\u53e5 for-each 11.13 - for-each \u5faa\u73af fordward declaration 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 format-group \u683c\u5f0f\u7ec4 formatting 1.8 - \u7a7a\u767d\u7b26\u548c\u57fa\u672c\u6392\u7248 forward declaration \u524d\u5411\u58f0\u660e friend 13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b 13.16 - \u533f\u540d\u5bf9\u8c61 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b 13.18 - \u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 friend-class \u53cb\u5143\u7c7b friend-function \u53cb\u5143\u51fd\u6570 function 12.1 - \u51fd\u6570\u6307\u9488 2.1 - \u51fd\u6570\u7b80\u4ecb 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 2.6 - \u4e3a\u4ec0\u4e48\u51fd\u6570\u6709\u7528\uff0c\u5982\u4f55\u9ad8\u6548\u4f7f\u7528\u51fd\u6570 function template 8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f function-instance \u51fd\u6570\u5b9e\u4f8b function-literal \u51fd\u6570\u5b57\u9762\u91cf function-pointer \u51fd\u6570\u6307\u9488 function-template \u51fd\u6570\u6a21\u677f function-template-instantiation \u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316 generic-algorithm \u6cdb\u578b\u7b97\u6cd5 global 6.4 - \u5168\u5c40\u53d8\u91cf 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf global variable 6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c \u5168\u5c40\u53d8\u91cf glossary \u7f16\u8bd1\u65f6 \u58f0\u660e \u5b9a\u4e49 \u524d\u5411\u58f0\u660e \u5168\u5c40\u53d8\u91cf \u6807\u8bc6\u7b26(identifier) \u5b9e\u4f8b\u5316 \u6307\u4ee4 \u94fe\u63a5\u5668(linker) \u5b8f \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u5f62\u53c2 \u6309\u503c\u4f20\u9012 \u9884\u5904\u7406\u5668\u6307\u4ee4 \u9884\u5904\u7406\u5668 \u7eaf\u58f0\u660e(pure-declaration) \u9650\u5b9a\u7b26 \u91cd\u6784 \u6309\u503c\u8fd4\u56de \u8fd0\u884c\u65f6 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u4f5c\u7528\u57df \u8bed\u53e5 \u7b26\u53f7\u5e38\u91cf goto 7.6 - goto \u8bed\u53e5 halt 7.11 - \u6302\u8d77\uff08\u63d0\u524d\u7ed3\u675f\u7a0b\u5e8f\uff09 header 2.11 - \u5934\u6587\u4ef6 header guards 2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e heap 12.2 - \u6808\u548c\u5806 \u5806 identifier 1.7 - \u5173\u952e\u5b57\u548c\u6807\u8bc6\u7b26 \u6807\u8bc6\u7b26(identifier) identity \u8eab\u4efd\u7279\u5f81 if 4.10 - if \u8bed\u53e5\u7b80\u4ecb 7.2 - if \u8bed\u53e5\u548c\u8bed\u53e5\u5757 7.3 - \u5e38\u89c1\u7684 if \u8bed\u53e5\u9519\u8bef immediate-functions \u5373\u65f6\u51fd\u6570 implicit-constructor \u9690\u5f0f\u6784\u9020\u51fd\u6570 implicit-object \u9690\u5f0f\u5bf9\u8c61 implicit-type-conversion \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 inheritance 17.1 - \u7ee7\u627f\u7b80\u4ecb 17.2 - C++\u7ee7\u627f\u57fa\u7840 17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f 17.4 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u548c\u521d\u59cb\u5316 17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 17.6 - \u4e3a\u6d3e\u751f\u7c7b\u6dfb\u52a0\u65b0\u529f\u80fd 17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570 17.9 - \u591a\u91cd\u7ee7\u627f 18.11 - \u4f7f\u7528 << \u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b \u7ee7\u627f initialization 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u62f7\u8d1d\u521d\u59cb\u5316 \u76f4\u63a5\u521d\u59cb\u5316 \u5217\u8868\u521d\u59cb\u5316 initializer-list \u521d\u59cb\u5316\u503c\u5217\u8868 initializer_list 16.7 - std::initializer_list inline 6.13 - \u5185\u8054\u51fd\u6570 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf inline namespace 6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4 inline-function \u5185\u8054\u51fd\u6570 inline-namespace \u5185\u8054\u547d\u540d\u7a7a\u95f4 inline-variables \u5185\u8054\u53d8\u91cf input-manipulator 4.17 - std::string \u7b80\u4ecb input-manipulators \u8f93\u5165\u64cd\u7eb5\u5668 insertion-operator \u63d2\u5165\u8fd0\u7b97\u7b26 instance \u5b9e\u4f8b instantiated \u5b9e\u4f8b\u5316 instuction \u6307\u4ee4 integer 4.4 - \u6709\u7b26\u53f7\u6574\u578b 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t integral-type \u6574\u578b\u7c7b\u578b interface-class \u63a5\u53e3\u7c7b interface-classes 18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b internal-linkage \u5185\u90e8\u94fe\u63a5 internal-variable \u5185\u90e8\u53d8\u91cf io 23.1 - \u8f93\u5165\u8f93\u51fa\u6d41 23.2 - \u4f7f\u7528istream\u5904\u7406\u8f93\u5165 23.3 - \u4f7f\u7528ostream\u548cios\u5904\u7406\u8f93\u51fa 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b 23.5 - \u6d41\u72b6\u6001\u548c\u8f93\u5165\u9a8c\u8bc1 23.6 - \u57fa\u672c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa 23.7 - \u968f\u673a\u6587\u4ef6\u8f93\u5165\u8f93\u51fa iostream 1.5 - iostream \u7b80\u4ecb\uff1acout\uff0ccin \u548c endl iterating \u904d\u5386 iterator 11.18 \u2014 \u8fed\u4ee3\u5668\u7b80\u4ecb \u8fed\u4ee3\u5668 lambda 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 \u533f\u540d\u51fd\u6570 late-binding 18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a link \u94fe\u63a5 linkage 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 6.3 - \u5c40\u90e8\u53d8\u91cf 6.6 - \u5185\u90e8\u94fe\u63a5 6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e \u94fe\u63a5\u5c5e\u6027 linker \u94fe\u63a5\u5668(linker) list assignment operator \u7c7b\u522b\u8d4b\u503c\u8fd0\u7b97\u7b26 list-constructor \u5217\u8868\u6784\u9020\u51fd\u6570 list-initialization \u5217\u8868\u521d\u59cb\u5316 literal 4.15 - \u5b57\u9762\u91cf literals 1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 \u5b57\u9762\u91cf local variable 6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf local variables 6.3 - \u5c40\u90e8\u53d8\u91cf loop 11.3 - \u6570\u7ec4\u548c\u5faa\u73af lvalue \u5de6\u503c lvalue references 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 lvalue-reference \u5de6\u503c\u5f15\u7528 lvalues 9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09 macro \u5b8f member access operator \u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26 member selection 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 member-function \u6210\u5458\u51fd\u6570 member-initializer-list \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 member-selection-operator \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 member-types \u6210\u5458\u7c7b\u578b member-variable \u6210\u5458\u53d8\u91cf members 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 memberwise initialization \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 memberwise-assignment \u6210\u5458\u4f9d\u6b21\u8d4b\u503c memberwise-copy \u6210\u5458\u4f9d\u6b21\u62f7\u8d1d memory-leak \u5185\u5b58\u6cc4\u6f0f mixin None move M.4 - std::move M.5 - std::move_if_noexcept M.6 \u2014 std::unique_ptr move assignment operator \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 move-constructor \u79fb\u52a8\u6784\u9020\u51fd\u6570 move-semantics M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb \u79fb\u52a8\u8bed\u4e49 multiple-inheritance 17.9 - \u591a\u91cd\u7ee7\u627f named-cast \u5177\u540d\u7c7b\u578b\u8f6c\u6362 namespace 2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u547d\u540d\u7a7a\u95f4 namespace aliases 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 narrowing-convertions \u7f29\u7a84\u8f6c\u6362 new 11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d numeric conversions 8.3 - \u6570\u503c\u8f6c\u6362 numeric promotion 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 numeric promotions \u6570\u503c\u63d0\u5347 numeric-conversions \u6570\u503c\u8f6c\u6362 object 1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf 13.1 - \u6b22\u8fce\u6765\u5230\u9762\u5411\u5bf9\u8c61\u7684\u4e16\u754c 16.1 - \u5bf9\u8c61\u5173\u7cfb 16.2 - \u7ec4\u5408\u5173\u7cfb 16.3 - \u805a\u5408\u5173\u7cfb 16.4 - \u5173\u8054\u5173\u7cfb 16.5 - \u4f9d\u8d56\u5173\u7cfb 16.6 - \u5bb9\u5668\u7c7b 16.7 - std::initializer_list object-composition \u5bf9\u8c61\u7ec4\u5408 object-like-macros \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f object-slicing \u5bf9\u8c61\u5207\u7247 one-definition-rule \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) operands \u64cd\u4f5c\u6570 operator 1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 5.1 - \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 5.3 - \u6c42\u6a21\u548c\u6307\u6570\u8fd0\u7b97 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 5.5 - \u9017\u53f7\u548c\u6761\u4ef6\u8fd0\u7b97\u7b26 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 5.7 - \u903b\u8f91\u8fd0\u7b97\u7b26 \u8fd0\u7b97\u7b26 operator<< 18.11 - \u4f7f\u7528 << \u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b optional-parameter \u53ef\u9009\u53c2\u6570 overflow 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 overlapping 13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570 overload 14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26 14.15 - \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 8.10 - \u91cd\u8f7d\u51fd\u6570\u7684\u533a\u5206 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u4e0d\u660e\u786e\u5339\u914d \u91cd\u8f7d overload-resolution \u91cd\u8f7d\u89e3\u6790 overloading 8.9 - \u51fd\u6570\u91cd\u8f7d override 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b \u91cd\u5199 overriding 17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570 overriding-virtualization 18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316 parameter 2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2 parameters \u5f62\u53c2 parent-class \u7236\u7c7b parenthesis 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 pass-by-address \u6309\u5730\u5740\u4f20\u9012 pass-by-reference \u6309\u5f15\u7528\u4f20\u9012 pass-by-value \u6309\u503c\u4f20\u9012 placeholder-types \u5360\u4f4d\u7b26\u7c7b\u578b pointer 11.15 \u2014 \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u548c\u591a\u7ef4\u6570\u7ec4 11.8 - \u6307\u9488\u548c\u6570\u7ec4 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 9.6 - \u6307\u9488\u7b80\u4ecb 9.7 - \u7a7a\u6307\u9488 9.8 - \u6307\u9488\u548cconst 9.9 - \u6309\u5730\u5740\u4f20\u9012 pointer-to-const \u6307\u5411\u5e38\u91cf\u7684\u6307\u9488 pointers 18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 polymorphism 18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001 \u591a\u6001 pragma once 2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e preprocessor 2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb \u9884\u5904\u7406\u5668\u6307\u4ee4 \u9884\u5904\u7406\u5668 private-inheritance \u79c1\u6709\u7ee7\u627f private-member \u79c1\u6709\u6210\u5458 program organization 2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f protected-inheritance \u53d7\u4fdd\u62a4\u7ee7\u627f protected-members \u53d7\u4fdd\u62a4\u6210\u5458 public-inheritance \u516c\u5f00\u7ee7\u627f public-member \u516c\u6709\u6210\u5458 pure-declaration \u7eaf\u58f0\u660e(pure-declaration) pure-virtual \u7eaf\u865a\u51fd\u6570 pure-virtual-functions 18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b qualifier \u9650\u5b9a\u7b26 random-access-memory \u968f\u673a\u8bbf\u95ee\u5185\u5b58 range-based-for-loops \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af raw-pointer \u539f\u59cb\u6307\u9488 recursion 12.4 - \u9012\u5f52 refactoring \u91cd\u6784 reference 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de reference-container \u5f15\u7528\u5bb9\u5668 references 18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 M.2 - \u53f3\u503c\u5f15\u7528 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c reflexive association \u53cd\u8eab\u5173\u8054 reinterpret-casts \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 return 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de return-by-address \u6309\u5730\u5740\u8fd4\u56de return-by-reference \u6309\u5f15\u7528\u8fd4\u56de return-by-value \u6309\u503c\u8fd4\u56de return-type-deduction \u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad rounding-error \u820d\u5165\u8bef\u5dee runtime \u8fd0\u884c\u65f6 rvalue \u53f3\u503c rvalue-reference \u53f3\u503c\u5f15\u7528 rvalues 9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09 scaling \u6307\u9488\u7f29\u653e scientific notation 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 scope 2.5 - \u5c40\u90e8\u4f5c\u7528\u57df 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 \u4f5c\u7528\u57df scope resolution operator 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 scope-resolution-operator \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 scoped enumerations 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 scoped-enumerations \u6709\u4f5c\u7528\u57df\u679a\u4e3e segment \u5185\u5b58\u6bb5 sentinel \u54e8\u5175 setup 0.8 - \u4e00\u4e9b\u5178\u578b\u7684 C++ \u95ee\u9898 shadow \u53d8\u91cf\u906e\u853d shallow copy 14.16 - \u6d45\u62f7\u8d1d\u548c\u6df1\u62f7\u8d1d 14.17 - \u91cd\u8f7d\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u6a21\u677f shallow-copy \u6d45\u62f7\u8d1d shared_ptr M.7 \u2014 std::shared_ptr M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898 side-effects \u526f\u4f5c\u7528 signature \u7b7e\u540d size_t 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t sizeof 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 smart pointer class \u667a\u80fd\u6307\u9488\u7c7b smart-pointer \u667a\u80fd\u6307\u9488 smart-pointers M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb sort 11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f stack 12.2 - \u6808\u548c\u5806 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a \u6808 stack-frame \u6808\u5e27 stack-overflow \u5806\u6808\u6ea2\u51fa standard-stream \u6807\u51c6\u6d41 statement 6.1 - \u590d\u5408\u8bed\u53e5\uff08\u8bed\u53e5\u5757\uff09 \u8bed\u53e5 statements 1.1 - \u7a0b\u5e8f\u7ed3\u6784\u548c\u8bed\u53e5 static 6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf static membe function 13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570 static member 13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 static member variable 13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf static-cast 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast static-casts \u9759\u6001\u7c7b\u578b\u8f6c\u6362 static-initialization \u9759\u6001\u521d\u59cb\u5316 static-member-functions \u9759\u6001\u6210\u5458\u51fd\u6570 static-member-variables \u9759\u6001\u6210\u5458\u53d8\u91cf static-memory-allocation \u9759\u6001\u5185\u5b58\u5206\u914d static-storage-duration \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 static-variables \u9759\u6001\u53d8\u91cf static_assert 7.17 - \u65ad\u8a00\u548c static_assert static_cast 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast 8.3 - \u6570\u503c\u8f6c\u6362 std::array 11.16 \u2014 \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb std::string_view 11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 stl 21.1 - \u6807\u51c6\u5e93 21.2 - STL\u5bb9\u5668 21.3 - STL\u8fed\u4ee3\u5668 21.4 - STL\u7b97\u6cd5 storage duration 6.3 - \u5c40\u90e8\u53d8\u91cf 6.4 - \u5168\u5c40\u53d8\u91cf storage-duration \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 string 11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 22.1 - std::string \u548c std::wstring 22.2 - std-string \u7684\u6784\u9020\u548c\u6790\u6784 22.3 - std-string \u7684\u957f\u5ea6\u548c\u5bb9\u91cf 22.4 - std-string \u5b57\u7b26\u8bbf\u95ee\u548cC\u5b57\u7b26\u6570\u7ec4\u8f6c\u6362 22.5 - std-string \u8d4b\u503c\u548c\u4ea4\u6362 22.6 - std-string \u8ffd\u52a0 22.7 - std-string \u63d2\u5165 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b 4.17 - std::string \u7b80\u4ecb string_view 4.18 - std::string_view \u7b80\u4ecb strong-except \u5f3a\u5f02\u5e38\u4fdd\u8bc1 struct 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 10.8 - \u7ed3\u6784\u4f53\u4f20\u9012\u53ca\u5176\u4ed6 10.9 - \u57fa\u4e8e\u6307\u9488\u548c\u5f15\u7528\u7684\u6210\u5458\u9009\u62e9 struct-padding \u7ed3\u6784\u4f53\u586b\u5145 subscript 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 subscripts \u4e0b\u6807 summary 1.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - C++\u57fa\u7840 10.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u679a\u4e3e\u548c\u7ed3\u6784\u4f53 10.y - \u5b66\u4f1a\u4f7f\u7528C++\u53c2\u8003\u624b\u518c 11.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5206\u914d 12.8 - lambda \u95ed\u5305 12.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570 13.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u9762\u5411\u5bf9\u8c61\u57fa\u7840 14.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26\u91cd\u8f7d 16.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5bf9\u8c61\u5173\u7cfb 17.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7ee7\u627f 18.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u865a\u51fd\u6570 19.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6a21\u677f\u548c\u7c7b 2.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570\u4e0e\u6587\u4ef6 20.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f02\u5e38 3.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4ee3\u7801\u8c03\u8bd5 4.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u57fa\u672c\u6570\u636e\u7c7b\u578b 5.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 7.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406 8.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d 9.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f15\u7528\u548c\u6307\u9488 M.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u79fb\u52a8\u548c\u667a\u80fd\u6307\u9488 super-class \u8d85\u7c7b switch 7.4 - switch \u8bed\u53e5\u57fa\u7840 7.5 - switch fallthrough\u5c5e\u6027\u548c\u4f5c\u7528\u57df symbolic constants 11.10 - C \u98ce\u683c\u5b57\u7b26\u4e32\u7b26\u53f7\u5e38\u91cf symbolic-constants \u7b26\u53f7\u5e38\u91cf template class specialization \u7c7b\u6a21\u677f\u7279\u5316 testing 7.12 - \u4ee3\u7801\u6d4b\u8bd5 the-diamond-problem \u83f1\u5f62\u7ee7\u627f\u95ee\u9898 this 13.10 - \u9690\u85cf\u7684this\u6307\u9488 translation \u7ffb\u8bd1 type 10.1 - \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7b80\u4ecb type conversion 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 type deduction 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u63a8\u65ad type-aliases \u7c7b\u578b\u522b\u540d type-conversion \u7c7b\u578b\u8f6c\u6362 type-definition \u7c7b\u578b\u5b9a\u4e49 type-inference \u7c7b\u578b\u63a8\u65ad type-safe \u7c7b\u578b\u5b89\u5168 type-signature \u7c7b\u578b\u7b7e\u540d typecast 14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26 typedef 8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d typeid typeid \u8fd0\u7b97\u7b26 typeinfo 8.4 - \u7b97\u672f\u8f6c\u6362 unary 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 undefined behavior 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 4.4 - \u6709\u7b26\u53f7\u6574\u578b undefined-behavior \u672a\u5b9a\u4e49\u884c\u4e3a uniform initialization \u7edf\u4e00\u521d\u59cb\u5316 unmanned namespace 6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4 unnamed-namespace \u533f\u540d\u547d\u540d\u7a7a\u95f4 unscoped enumerations 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b 10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa unscoped-enumerations \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b upcasting \u5411\u4e0a\u8f6c\u6362 using 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 using-declaration using \u58f0\u660e using-directive using \u6307\u4ee4 value-category \u503c\u7c7b\u578b value-container \u503c\u5bb9\u5668 value-initialization \u503c\u521d\u59cb\u5316 value-preserving \u503c\u4fdd\u7559 variable 1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 variable shadowing 6.5 - \u53d8\u91cf\u906e\u853d vector 11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a virtual functions 18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001 virtual-assignment 18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316 virtual-base-class \u865a\u57fa\u7c7b virtual-base-classes 18.8 - \u865a\u57fa\u7c7b 18.9 - \u5bf9\u8c61\u5207\u7247 virtual-destructor \u865a\u6790\u6784\u51fd\u6570 virtual-destructors 18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316 virtual-function \u865a\u51fd\u6570 virtual-table 18.6 - \u865a\u51fd\u6570\u8868 \u865a\u8868 void 11.14 \u2014 void \u6307\u9488 2.3 - void \u51fd\u6570 4.2 - Void void pointer void \u6307\u9488 weak_ptr M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898 while 7.7 - \u5faa\u73af\u548c while \u8bed\u53e5 wild-pointer \u91ce\u6307\u9488 word \u5b57\u957f wstring 22.1 - std::string \u548c std::wstring zero-initialization 0\u521d\u59cb\u5316 \u51fd\u6570\u539f\u578b(function prototype) \u51fd\u6570\u539f\u578b \u521d\u59cb\u5316 \u521d\u59cb\u5316 \u521d\u59cb\u5316\u503c(initializer) \u521d\u59cb\u5316\u503c \u52a8\u6001\u6570\u7ec4 \u52a8\u6001\u6570\u7ec4 \u5bf9\u8c61(objec) \u5bf9\u8c61 \u62bd\u8c61\u7c7b \u62bd\u8c61\u7c7b \u62ec\u53f7\u521d\u59cb\u5316 None \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u6784\u9020\u51fd\u6570 \u6784\u9020\u51fd\u6570 \u6a21\u677f\u7c7b\u578b\u53c2\u6570 \u6a21\u677f\u7c7b\u578b\u53c2\u6570 \u79bb\u5f00\u4f5c\u7528\u57df \u79bb\u5f00\u4f5c\u7528\u57df \u7b80\u5355\u8f6c\u6362(trivial conversion) \u7b80\u5355\u8f6c\u6362 \u7c7b\u5b9e\u4f8b \u7c7b\u5b9e\u4f8b \u86c7\u5f62\u547d\u540d\u6cd5(snake_case) \u86c7\u5f62\u547d\u540d\u6cd5 \u8bcd\u6c47\u8868 \u5173\u8054 \u5173\u8054\u5bb9\u5668 \u7ed1\u5b9a C \u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362 \u65e9\u671f\u7ed1\u5b9a \u5c01\u88c5 \u8868\u8fbe\u5f0f\u53c2\u6570 \u7f1d\u5408\u602a\u5bf9\u8c61 \u540e\u671f\u7ed1\u5b9a \u591a\u91cd\u7ee7\u627f \u540d\u79f0\u4fee\u9970 \u5dee\u4e00\u9519\u8bef \u8f93\u51fa\u64cd\u7eb5\u5668 \u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09 \u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f(RTTI) \u987a\u5e8f\u5bb9\u5668 \u6a21\u677f\u7279\u5316 \u7f29\u5199\u51fd\u6570\u6a21\u677f \u62bd\u8c61\u7c7b \u62bd\u8c61 \u6210\u5458\u8bbf\u95ee\u51fd\u6570 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u53d6\u5730\u5740\u64cd\u4f5c\u7b26 & \u805a\u5408\u6570\u636e\u7c7b\u578b \u805a\u5408\u521d\u59cb\u5316 \u805a\u5408 \u4e0d\u660e\u786e\u5339\u914d \u533f\u540d\u5bf9\u8c61 \u5b9e\u53c2 \u5143\u6570 \u81ea\u52a8\u5185\u5b58\u5206\u914d \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u57fa\u7c7b \u4f4d None \u6309\u4f4d\u6216 bss\u6bb5 \u5b57\u8282 \u8c03\u7528\u6808 \u53ef\u8c03\u7528\u5bf9\u8c61 \u56de\u8c03\u51fd\u6570 \u5b50\u7c7b \u7c7b\u5b9e\u4f8b \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad \u7c7b\u6a21\u677f \u7c7b\u7c7b\u578b \u95ed\u5305 \u4ee3\u7801\u6bb5 \u7f16\u8bd1\u5668 \u7ec4\u5408\u7c7b\u578b const \u7c7b\u578b\u8f6c\u6362 const \u6210\u5458\u51fd\u6570 \u5e38\u91cf\u6307\u9488 \u5e38\u91cf\u8868\u8fbe\u5f0f \u6784\u9020\u51fd\u6570 \u5bb9\u5668\u9002\u914d\u5668 \u5bb9\u5668\u7c7b \u8f6c\u6362\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u521d\u59cb\u5316 \u62f7\u8d1d\u8bed\u4e49 \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b \u60ac\u5782 \u6570\u636e\u6bb5 \u6df1\u62f7\u8d1d \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 \u9ed8\u8ba4\u521d\u59cb\u5316 \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c \u59d4\u6258\u6784\u9020\u51fd\u6570 \u4f9d\u8d56 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u6d3e\u751f\u7c7b \u6307\u5b9a\u521d\u59cb\u5316 \u6790\u6784\u51fd\u6570 \u76f4\u63a5\u521d\u59cb\u5316 \u5411\u4e0b\u8f6c\u6362 \u7b28\u6307\u9488 \u52a8\u6001\u6570\u7ec4 \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u52a8\u6001\u521d\u59cb\u5316 \u52a8\u6001\u5185\u5b58\u5206\u914d \u679a\u4e3e\u7c7b \u6c42\u503c \u5f02\u5e38\u5b89\u5168\u95ee\u9898 \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u8868\u8fbe\u5f0f\u7c7b\u578b \u5916\u90e8\u94fe\u63a5 \u5916\u90e8\u53d8\u91cf \u63d0\u53d6\u8fd0\u7b97\u7b26 \u8d2f\u7a7f\u5c5e\u6027 \u6587\u4ef6\u4f5c\u7528\u57df \u56fa\u5b9a\u6570\u7ec4 \u683c\u5f0f\u7ec4 \u53cb\u5143\u7c7b \u53cb\u5143\u51fd\u6570 \u51fd\u6570\u5b9e\u4f8b \u51fd\u6570\u5b57\u9762\u91cf \u51fd\u6570\u6307\u9488 \u51fd\u6570\u539f\u578b \u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316 \u51fd\u6570\u6a21\u677f \u6cdb\u578b\u7b97\u6cd5 \u79bb\u5f00\u4f5c\u7528\u57df \u5806 \u8eab\u4efd\u7279\u5f81 \u5373\u65f6\u51fd\u6570 \u9690\u5f0f\u6784\u9020\u51fd\u6570 \u9690\u5f0f\u5bf9\u8c61 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u7ee7\u627f \u521d\u59cb\u5316 \u521d\u59cb\u5316\u503c\u5217\u8868 \u521d\u59cb\u5316\u503c \u5185\u8054\u51fd\u6570 \u5185\u8054\u547d\u540d\u7a7a\u95f4 \u5185\u8054\u53d8\u91cf \u8f93\u5165\u64cd\u7eb5\u5668 \u63d2\u5165\u8fd0\u7b97\u7b26 \u5b9e\u4f8b \u6574\u578b\u7c7b\u578b \u63a5\u53e3\u7c7b \u5185\u90e8\u94fe\u63a5 \u5185\u90e8\u53d8\u91cf \u904d\u5386 \u8fed\u4ee3\u5668 \u533f\u540d\u51fd\u6570 \u94fe\u63a5 \u94fe\u63a5\u5c5e\u6027 \u7c7b\u522b\u8d4b\u503c\u8fd0\u7b97\u7b26 \u5217\u8868\u6784\u9020\u51fd\u6570 \u5217\u8868\u521d\u59cb\u5316 \u5b57\u9762\u91cf \u5de6\u503c\u5f15\u7528 \u5de6\u503c \u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26 \u6210\u5458\u51fd\u6570 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 \u6210\u5458\u7c7b\u578b \u6210\u5458\u53d8\u91cf \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \u6210\u5458\u4f9d\u6b21\u8d4b\u503c \u6210\u5458\u4f9d\u6b21\u62f7\u8d1d \u5185\u5b58\u6cc4\u6f0f None \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 \u79fb\u52a8\u6784\u9020\u51fd\u6570 \u79fb\u52a8\u8bed\u4e49 \u5177\u540d\u7c7b\u578b\u8f6c\u6362 \u547d\u540d\u7a7a\u95f4 \u7f29\u7a84\u8f6c\u6362 \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u6570\u503c\u63d0\u5347 \u6570\u503c\u8f6c\u6362 \u5bf9\u8c61\u7ec4\u5408 \u5bf9\u8c61\u5207\u7247 \u5bf9\u8c61 \u64cd\u4f5c\u6570 \u8fd0\u7b97\u7b26 \u8d85\u51fa\u4f5c\u7528\u57df \u91cd\u8f7d\u89e3\u6790 \u91cd\u8f7d \u91cd\u5199 \u7236\u7c7b \u6309\u5730\u5740\u4f20\u9012 \u6309\u5f15\u7528\u4f20\u9012 \u5360\u4f4d\u7b26\u7c7b\u578b \u6307\u5411\u5e38\u91cf\u7684\u6307\u9488 \u591a\u6001 \u79c1\u6709\u7ee7\u627f \u79c1\u6709\u6210\u5458 \u53d7\u4fdd\u62a4\u7ee7\u627f \u53d7\u4fdd\u62a4\u6210\u5458 \u516c\u5f00\u7ee7\u627f \u516c\u6709\u6210\u5458 \u7eaf\u865a\u51fd\u6570 \u968f\u673a\u8bbf\u95ee\u5185\u5b58 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u539f\u59cb\u6307\u9488 \u5f15\u7528\u5bb9\u5668 \u53cd\u8eab\u5173\u8054 \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 \u6309\u5730\u5740\u8fd4\u56de \u6309\u5f15\u7528\u8fd4\u56de \u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad \u820d\u5165\u8bef\u5dee \u53f3\u503c\u5f15\u7528 \u53f3\u503c \u6307\u9488\u7f29\u653e \u6709\u4f5c\u7528\u57df\u679a\u4e3e \u5185\u5b58\u6bb5 \u54e8\u5175 \u53d8\u91cf\u906e\u853d \u6d45\u62f7\u8d1d \u526f\u4f5c\u7528 \u7b7e\u540d \u667a\u80fd\u6307\u9488\u7c7b \u667a\u80fd\u6307\u9488 \u86c7\u5f62\u547d\u540d\u6cd5 \u6808\u5e27 \u5806\u6808\u6ea2\u51fa \u6808 \u6807\u51c6\u6d41 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u9759\u6001\u521d\u59cb\u5316 \u9759\u6001\u6210\u5458\u51fd\u6570 \u9759\u6001\u6210\u5458\u53d8\u91cf \u9759\u6001\u5185\u5b58\u5206\u914d \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u9759\u6001\u53d8\u91cf \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u5f3a\u5f02\u5e38\u4fdd\u8bc1 \u7ed3\u6784\u4f53\u586b\u5145 \u4e0b\u6807 \u8d85\u7c7b \u8bed\u6cd5 \u7c7b\u6a21\u677f\u7279\u5316 \u6a21\u677f\u7c7b\u578b\u53c2\u6570 \u83f1\u5f62\u7ee7\u627f\u95ee\u9898 \u7ffb\u8bd1 \u7b80\u5355\u8f6c\u6362 \u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u522b\u540d \u7c7b\u578b\u8f6c\u6362 \u7c7b\u578b\u5b9a\u4e49 \u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u5b89\u5168 \u7c7b\u578b\u7b7e\u540d typeid \u8fd0\u7b97\u7b26 \u672a\u5b9a\u4e49\u884c\u4e3a \u7edf\u4e00\u521d\u59cb\u5316 \u533f\u540d\u547d\u540d\u7a7a\u95f4 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u5411\u4e0a\u8f6c\u6362 using \u58f0\u660e using \u6307\u4ee4 \u503c\u7c7b\u578b \u503c\u5bb9\u5668 \u503c\u521d\u59cb\u5316 \u503c\u4fdd\u7559 \u865a\u57fa\u7c7b \u865a\u6790\u6784\u51fd\u6570 \u865a\u51fd\u6570 \u865a\u8868 void \u6307\u9488 \u91ce\u6307\u9488 \u5b57\u957f 0\u521d\u59cb\u5316 None \u8bed\u6cd5(syntax) \u8bed\u6cd5 \u8d85\u51fa\u4f5c\u7528\u57df(out of scope) \u8d85\u51fa\u4f5c\u7528\u57df \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316","title":"\u7d22\u5f15"},{"location":"tags/#tags","text":"","title":"Tags"},{"location":"tags/#associations","text":"\u5173\u8054","title":"Associations"},{"location":"tags/#associative-containers","text":"\u5173\u8054\u5bb9\u5668","title":"Associative-containers"},{"location":"tags/#binding","text":"\u7ed1\u5b9a","title":"Binding"},{"location":"tags/#c11","text":"11.13 - for-each \u5faa\u73af 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb 4.11 - \u5b57\u7b26 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 M.7 \u2014 std::shared_ptr","title":"C++11"},{"location":"tags/#c14","text":"12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 4.15 - \u5b57\u9762\u91cf 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad M.6 \u2014 std::unique_ptr M.7 \u2014 std::shared_ptr","title":"C++14"},{"location":"tags/#c17","text":"10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 11.16 \u2014 \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb 11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 4.18 - std::string_view \u7b80\u4ecb 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 7.17 - \u65ad\u8a00\u548c static_assert M.6 \u2014 std::unique_ptr M.7 \u2014 std::shared_ptr","title":"C++17"},{"location":"tags/#c20","text":"10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 11.13 - for-each \u5faa\u73af 11.19 \u2014 \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 6.14 - Constexpr \u548c consteval \u51fd\u6570 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad M.7 \u2014 std::shared_ptr","title":"C++20"},{"location":"tags/#c-style-casts","text":"C \u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362","title":"C-style-casts"},{"location":"tags/#ctad","text":"10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD","title":"CTAD"},{"location":"tags/#early-binding","text":"\u65e9\u671f\u7ed1\u5b9a","title":"Early binding"},{"location":"tags/#encapsulation","text":"\u5c01\u88c5","title":"Encapsulation"},{"location":"tags/#expression-parameters","text":"\u8868\u8fbe\u5f0f\u53c2\u6570","title":"Expression-parameters"},{"location":"tags/#frankenobject","text":"\u7f1d\u5408\u602a\u5bf9\u8c61","title":"Frankenobject"},{"location":"tags/#function-templates","text":"8.13 - \u51fd\u6570\u6a21\u677f","title":"Function templates"},{"location":"tags/#late-binding","text":"\u540e\u671f\u7ed1\u5b9a","title":"Late binding"},{"location":"tags/#lvalue","text":"9.3 - \u5de6\u503c\u5f15\u7528 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528","title":"Lvalue"},{"location":"tags/#multiple-inheritance","text":"\u591a\u91cd\u7ee7\u627f","title":"Multiple inheritance"},{"location":"tags/#name-mangling","text":"\u540d\u79f0\u4fee\u9970","title":"Name-mangling"},{"location":"tags/#numeral-systems","text":"4.16 - \u6570\u503c\u7cfb\u7edf\uff08\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236\uff09","title":"Numeral systems"},{"location":"tags/#off-by-one","text":"\u5dee\u4e00\u9519\u8bef","title":"Off-by-one"},{"location":"tags/#output-manipulators","text":"\u8f93\u51fa\u64cd\u7eb5\u5668","title":"Output-manipulators"},{"location":"tags/#pointer-arithmetic","text":"11.9 - \u6307\u9488\u8fd0\u7b97\u548c\u6570\u7ec4\u7d22\u5f15","title":"Pointer arithmetic"},{"location":"tags/#r-value-references","text":"M.2 - \u53f3\u503c\u5f15\u7528 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c","title":"R-value-references"},{"location":"tags/#raii-resource-acquisition-is-initialization","text":"\u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09","title":"RAII (Resource Acquisition Is Initialization)"},{"location":"tags/#run-time-type-information-rtti","text":"\u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f(RTTI)","title":"Run-time-type-information-RTTI"},{"location":"tags/#sequence-containers","text":"\u987a\u5e8f\u5bb9\u5668","title":"Sequence-containers"},{"location":"tags/#syntax","text":"3.1 - \u8bed\u6cd5\u548c\u8bed\u4e49\u9519\u8bef","title":"Syntax"},{"location":"tags/#template-specialization","text":"\u6a21\u677f\u7279\u5316","title":"Template-specialization"},{"location":"tags/#abbreviated-function-templates","text":"\u7f29\u5199\u51fd\u6570\u6a21\u677f","title":"abbreviated function templates"},{"location":"tags/#abstract-base-classes","text":"18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b","title":"abstract-base-classes"},{"location":"tags/#abstraction","text":"\u62bd\u8c61","title":"abstraction"},{"location":"tags/#access-specifier","text":"13.3 - \u8bbf\u95ee\u4fee\u9970\u7b26 public \u548c private","title":"access specifier"},{"location":"tags/#access-function","text":"\u6210\u5458\u8bbf\u95ee\u51fd\u6570","title":"access-function"},{"location":"tags/#access-specifiers","text":"17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26","title":"access-specifiers"},{"location":"tags/#address","text":"9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de 9.9 - \u6309\u5730\u5740\u4f20\u9012","title":"address"},{"location":"tags/#address-of-operator","text":"\u53d6\u5730\u5740\u64cd\u4f5c\u7b26 &","title":"address-of-operator"},{"location":"tags/#aggregate-initialization","text":"10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316","title":"aggregate initialization"},{"location":"tags/#aggregate-data-type","text":"\u805a\u5408\u6570\u636e\u7c7b\u578b","title":"aggregate-data-type"},{"location":"tags/#aggregate-initialization_1","text":"\u805a\u5408\u521d\u59cb\u5316","title":"aggregate-initialization"},{"location":"tags/#aggregation","text":"16.3 - \u805a\u5408\u5173\u7cfb \u805a\u5408","title":"aggregation"},{"location":"tags/#algorithms","text":"11.19 \u2014 \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb","title":"algorithms"},{"location":"tags/#alias","text":"8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d","title":"alias"},{"location":"tags/#ambiguous-match","text":"\u4e0d\u660e\u786e\u5339\u914d","title":"ambiguous-match"},{"location":"tags/#anonymous-object","text":"\u533f\u540d\u5bf9\u8c61","title":"anonymous-object"},{"location":"tags/#argument","text":"12.5 - \u547d\u4ee4\u884c\u53c2\u6570 2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2","title":"argument"},{"location":"tags/#arguments","text":"\u5b9e\u53c2","title":"arguments"},{"location":"tags/#arithmetic-conversions","text":"8.4 - \u7b97\u672f\u8f6c\u6362","title":"arithmetic conversions"},{"location":"tags/#arity","text":"\u5143\u6570","title":"arity"},{"location":"tags/#array","text":"11.1 - \u6570\u7ec4\uff08\u7b2c\u4e00\u90e8\u5206\uff09 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.3 - \u6570\u7ec4\u548c\u5faa\u73af 11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f 11.5 - \u591a\u7ef4\u6570\u7ec4 11.8 - \u6307\u9488\u548c\u6570\u7ec4","title":"array"},{"location":"tags/#arrays","text":"11.12 - \u52a8\u6001\u5206\u914d\u6570\u7ec4","title":"arrays"},{"location":"tags/#assert","text":"7.17 - \u65ad\u8a00\u548c static_assert","title":"assert"},{"location":"tags/#assignment","text":"14.15 - \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26","title":"assignment"},{"location":"tags/#association","text":"16.4 - \u5173\u8054\u5173\u7cfb","title":"association"},{"location":"tags/#auto","text":"8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad","title":"auto"},{"location":"tags/#automatic-memory-allocation","text":"\u81ea\u52a8\u5185\u5b58\u5206\u914d","title":"automatic-memory-allocation"},{"location":"tags/#automatic-storage-duration","text":"\u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4","title":"automatic-storage-duration"},{"location":"tags/#base-class","text":"\u57fa\u7c7b","title":"base-class"},{"location":"tags/#bit","text":"O.1 - bit\u6807\u8bb0\u548c\u901a\u8fc7std::bitset\u64cd\u4f5cbit O.2 - \u4f4d\u8fd0\u7b97\u7b26 O.3 - \u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u548c\u63a9\u7801\u64cd\u4f5c\u4f4d O.4 - \u4e8c\u8fdb\u5236\u548c\u5341\u8fdb\u5236\u8f6c\u6362 \u4f4d","title":"bit"},{"location":"tags/#bitwise-and","text":"None","title":"bitwise-and"},{"location":"tags/#bitwise-or","text":"\u6309\u4f4d\u6216","title":"bitwise-or"},{"location":"tags/#bool","text":"4.9 - \u5e03\u5c14\u503c","title":"bool"},{"location":"tags/#boolean","text":"4.10 - if \u8bed\u53e5\u7b80\u4ecb","title":"boolean"},{"location":"tags/#bss-segment","text":"bss\u6bb5","title":"bss segment"},{"location":"tags/#byte","text":"\u5b57\u8282","title":"byte"},{"location":"tags/#c-style","text":"11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32","title":"c-style"},{"location":"tags/#call-stack","text":"3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808","title":"call stack"},{"location":"tags/#call-stack_1","text":"\u8c03\u7528\u6808","title":"call-stack"},{"location":"tags/#callable-object","text":"\u53ef\u8c03\u7528\u5bf9\u8c61","title":"callable-object"},{"location":"tags/#callback-functions","text":"\u56de\u8c03\u51fd\u6570","title":"callback functions"},{"location":"tags/#char","text":"4.11 - \u5b57\u7b26","title":"char"},{"location":"tags/#child-class","text":"\u5b50\u7c7b","title":"child-class"},{"location":"tags/#circular-dependency","text":"M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898","title":"circular dependency"},{"location":"tags/#class","text":"13.11 - \u7c7b\u4ee3\u7801\u548c\u5934\u6587\u4ef6 13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570 13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf 13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570 13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b 13.16 - \u533f\u540d\u5bf9\u8c61 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b 13.18 - \u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 13.2 - \u7c7b\u548c\u7c7b\u6210\u5458","title":"class"},{"location":"tags/#class-template","text":"10.10 - \u7c7b\u6a21\u677f 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD","title":"class template"},{"location":"tags/#class-template_1","text":"\u7c7b\u6a21\u677f","title":"class-template"},{"location":"tags/#class-template-argument-deduction","text":"\u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad","title":"class-template-argument-deduction"},{"location":"tags/#class-type","text":"\u7c7b\u7c7b\u578b","title":"class-type"},{"location":"tags/#closure","text":"\u95ed\u5305","title":"closure"},{"location":"tags/#code-coverage","text":"7.13 - \u4ee3\u7801\u8986\u76d6\u7387","title":"code coverage"},{"location":"tags/#code-segment","text":"\u4ee3\u7801\u6bb5","title":"code segment"},{"location":"tags/#comments","text":"1.2 - \u6ce8\u91ca","title":"comments"},{"location":"tags/#compile-time","text":"\u7f16\u8bd1\u65f6","title":"compile-time"},{"location":"tags/#compiler","text":"\u7f16\u8bd1\u5668","title":"compiler"},{"location":"tags/#composite-types","text":"\u7ec4\u5408\u7c7b\u578b","title":"composite-types"},{"location":"tags/#composition","text":"16.2 - \u7ec4\u5408\u5173\u7cfb","title":"composition"},{"location":"tags/#compound-data-types","text":"9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b","title":"compound data types"},{"location":"tags/#const","text":"4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 9.8 - \u6307\u9488\u548cconst","title":"const"},{"location":"tags/#const-class","text":"13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570","title":"const class"},{"location":"tags/#const-cast","text":"const \u7c7b\u578b\u8f6c\u6362","title":"const-cast"},{"location":"tags/#const-member-function","text":"const \u6210\u5458\u51fd\u6570","title":"const-member-function"},{"location":"tags/#const-pointer","text":"\u5e38\u91cf\u6307\u9488","title":"const-pointer"},{"location":"tags/#constant-expression","text":"\u5e38\u91cf\u8868\u8fbe\u5f0f","title":"constant expression"},{"location":"tags/#constants","text":"4.15 - \u5b57\u9762\u91cf","title":"constants"},{"location":"tags/#consteval","text":"6.14 - Constexpr \u548c consteval \u51fd\u6570","title":"consteval"},{"location":"tags/#constexpr","text":"4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr 6.14 - Constexpr \u548c consteval \u51fd\u6570","title":"constexpr"},{"location":"tags/#constructor","text":"13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570 14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570","title":"constructor"},{"location":"tags/#constructors","text":"13.5 - \u6784\u9020\u51fd\u6570 13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868","title":"constructors"},{"location":"tags/#container-classes","text":"16.6 - \u5bb9\u5668\u7c7b","title":"container classes"},{"location":"tags/#container-adapter","text":"\u5bb9\u5668\u9002\u914d\u5668","title":"container-adapter"},{"location":"tags/#container-class","text":"\u5bb9\u5668\u7c7b","title":"container-class"},{"location":"tags/#containers","text":"21.2 - STL\u5bb9\u5668 21.3 - STL\u8fed\u4ee3\u5668 21.4 - STL\u7b97\u6cd5","title":"containers"},{"location":"tags/#control-flow","text":"7.1 - \u63a7\u5236\u6d41 7.4 - switch \u8bed\u53e5\u57fa\u7840","title":"control flow"},{"location":"tags/#conversions","text":"8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 8.3 - \u6570\u503c\u8f6c\u6362 8.4 - \u7b97\u672f\u8f6c\u6362 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast","title":"conversions"},{"location":"tags/#converting-constructors","text":"14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57","title":"converting constructors"},{"location":"tags/#converting-constructors_1","text":"\u8f6c\u6362\u6784\u9020\u51fd\u6570","title":"converting-constructors"},{"location":"tags/#copy-constructor","text":"14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570","title":"copy constructor"},{"location":"tags/#copy-initialization","text":"14.13 - \u62f7\u8d1d\u521d\u59cb\u5316","title":"copy initialization"},{"location":"tags/#copy-constructor_1","text":"\u62f7\u8d1d\u6784\u9020\u51fd\u6570","title":"copy-constructor"},{"location":"tags/#copy-constructors","text":"\u62f7\u8d1d\u6784\u9020\u51fd\u6570","title":"copy-constructors"},{"location":"tags/#copy-initialization_1","text":"\u62f7\u8d1d\u521d\u59cb\u5316","title":"copy-initialization"},{"location":"tags/#copy-semantics","text":"\u62f7\u8d1d\u8bed\u4e49","title":"copy-semantics"},{"location":"tags/#covariant","text":"\u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b","title":"covariant"},{"location":"tags/#covariant-return-types","text":"18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b","title":"covariant-return-types"},{"location":"tags/#dangling","text":"\u60ac\u5782","title":"dangling"},{"location":"tags/#data-segment","text":"\u6570\u636e\u6bb5","title":"data segment"},{"location":"tags/#data-type","text":"4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb 4.4 - \u6709\u7b26\u53f7\u6574\u578b 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 4.8 - \u6d6e\u70b9\u6570 4.9 - \u5e03\u5c14\u503c 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad","title":"data type"},{"location":"tags/#debugger","text":"3.6 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u5355\u6b65\u8c03\u8bd5 3.7 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8fd0\u884c\u548c\u65ad\u70b9 3.8 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u76d1\u63a7\u53d8\u91cf 3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808","title":"debugger"},{"location":"tags/#debugging","text":"3.2 - \u4ee3\u7801\u8c03\u8bd5\u6b65\u9aa4 3.3 - \u4ee3\u7801\u8c03\u8bd5\u7b56\u7565 3.4 \u2014 \u57fa\u672c\u4ee3\u7801\u8c03\u8bd5\u6280\u672f 3.5 - \u66f4\u591a\u4ee3\u7801\u8c03\u8bd5\u6280\u672f","title":"debugging"},{"location":"tags/#declaration","text":"6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e \u58f0\u660e \u7eaf\u58f0\u660e(pure-declaration)","title":"declaration"},{"location":"tags/#deep-copy","text":"14.16 - \u6d45\u62f7\u8d1d\u548c\u6df1\u62f7\u8d1d 14.17 - \u91cd\u8f7d\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u6a21\u677f","title":"deep copy"},{"location":"tags/#deep-copy_1","text":"\u6df1\u62f7\u8d1d","title":"deep-copy"},{"location":"tags/#default","text":"8.12 - \u9ed8\u8ba4\u53c2\u6570","title":"default"},{"location":"tags/#default-constructor","text":"\u9ed8\u8ba4\u6784\u9020\u51fd\u6570","title":"default-constructor"},{"location":"tags/#default-initialization","text":"\u9ed8\u8ba4\u521d\u59cb\u5316","title":"default-initialization"},{"location":"tags/#default-member-initializer","text":"\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c","title":"default-member-initializer"},{"location":"tags/#definition","text":"\u5b9a\u4e49","title":"definition"},{"location":"tags/#delegating","text":"13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570","title":"delegating"},{"location":"tags/#delegating-constructors","text":"\u59d4\u6258\u6784\u9020\u51fd\u6570","title":"delegating-constructors"},{"location":"tags/#delete","text":"11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d 14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57","title":"delete"},{"location":"tags/#dependencies","text":"16.5 - \u4f9d\u8d56\u5173\u7cfb","title":"dependencies"},{"location":"tags/#dependency","text":"\u4f9d\u8d56","title":"dependency"},{"location":"tags/#dereference-operator","text":"\u89e3\u5f15\u7528\u8fd0\u7b97\u7b26","title":"dereference-operator"},{"location":"tags/#derived-class","text":"\u6d3e\u751f\u7c7b","title":"derived-class"},{"location":"tags/#desgin","text":"2.13 - \u5982\u4f55\u8bbe\u8ba1\u7b2c\u4e00\u4e2a\u7a0b\u5e8f","title":"desgin"},{"location":"tags/#designated-initializers","text":"\u6307\u5b9a\u521d\u59cb\u5316","title":"designated-initializers"},{"location":"tags/#destructor","text":"\u6790\u6784\u51fd\u6570","title":"destructor"},{"location":"tags/#destructors","text":"13.9 - \u6790\u6784\u51fd\u6570","title":"destructors"},{"location":"tags/#direct-initialization","text":"\u76f4\u63a5\u521d\u59cb\u5316","title":"direct-initialization"},{"location":"tags/#do-while","text":"7.8 - do-while \u8bed\u53e5","title":"do-while"},{"location":"tags/#downcasting","text":"\u5411\u4e0b\u8f6c\u6362","title":"downcasting"},{"location":"tags/#dumb-pointer","text":"\u7b28\u6307\u9488","title":"dumb-pointer"},{"location":"tags/#dynamic-casting","text":"18.10 - \u52a8\u6001\u7c7b\u578b\u8f6c\u6362","title":"dynamic casting"},{"location":"tags/#dynamic-duration","text":"\u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4","title":"dynamic duration"},{"location":"tags/#dynamic-casts","text":"\u52a8\u6001\u7c7b\u578b\u8f6c\u6362","title":"dynamic-casts"},{"location":"tags/#dynamic-initialization","text":"\u52a8\u6001\u521d\u59cb\u5316","title":"dynamic-initialization"},{"location":"tags/#dynamic-memory","text":"11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d","title":"dynamic-memory"},{"location":"tags/#dynamic-memory-allocation","text":"\u52a8\u6001\u5185\u5b58\u5206\u914d","title":"dynamic-memory-allocation"},{"location":"tags/#early-binding_1","text":"18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a","title":"early-binding"},{"location":"tags/#ellipsis","text":"12.6 - \u7701\u7565\u53f7\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83","title":"ellipsis"},{"location":"tags/#encapsulation_1","text":"13.4 - \u6210\u5458\u8bbf\u95ee\u51fd\u6570\u4e0e\u5c01\u88c5","title":"encapsulation"},{"location":"tags/#enum","text":"10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b 10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09","title":"enum"},{"location":"tags/#enum-class","text":"\u679a\u4e3e\u7c7b","title":"enum-class"},{"location":"tags/#error-handling","text":"7.15 - \u53d1\u73b0\u9519\u8bef\u548c\u5904\u7406\u9519\u8bef 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406","title":"error handling"},{"location":"tags/#evaluation","text":"\u6c42\u503c","title":"evaluation"},{"location":"tags/#excalidraw","text":"Drawing 2023 02 11 13.33.50.excalidraw Template.excalidraw C++ basic onepage Moc onepage","title":"excalidraw"},{"location":"tags/#exception-safety-issue","text":"\u5f02\u5e38\u5b89\u5168\u95ee\u9898","title":"exception-safety-issue"},{"location":"tags/#explicit","text":"14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57","title":"explicit"},{"location":"tags/#explicit-type-conversion","text":"8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362","title":"explicit-type-conversion"},{"location":"tags/#expression","text":"1.10 - \u8868\u8fbe\u5f0f\u7b80\u4ecb","title":"expression"},{"location":"tags/#expression-type","text":"\u8868\u8fbe\u5f0f\u7c7b\u578b","title":"expression type"},{"location":"tags/#external-linkage","text":"\u5916\u90e8\u94fe\u63a5","title":"external-linkage"},{"location":"tags/#external-variable","text":"\u5916\u90e8\u53d8\u91cf","title":"external-variable"},{"location":"tags/#extraction-operator","text":"\u63d0\u53d6\u8fd0\u7b97\u7b26","title":"extraction-operator"},{"location":"tags/#fallthrough","text":"\u8d2f\u7a7f\u5c5e\u6027","title":"fallthrough"},{"location":"tags/#file","text":"23.6 - \u57fa\u672c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa","title":"file"},{"location":"tags/#file-scope","text":"\u6587\u4ef6\u4f5c\u7528\u57df","title":"file-scope"},{"location":"tags/#final","text":"18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b","title":"final"},{"location":"tags/#fixed-array","text":"\u56fa\u5b9a\u6570\u7ec4","title":"fixed-array"},{"location":"tags/#fixed-width","text":"4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t","title":"fixed-width"},{"location":"tags/#float","text":"4.8 - \u6d6e\u70b9\u6570","title":"float"},{"location":"tags/#float-point","text":"5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83","title":"float point"},{"location":"tags/#for","text":"7.7 - \u5faa\u73af\u548c while \u8bed\u53e5 7.9 - for \u8bed\u53e5","title":"for"},{"location":"tags/#for-each","text":"11.13 - for-each \u5faa\u73af","title":"for-each"},{"location":"tags/#fordward-declaration","text":"2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49","title":"fordward declaration"},{"location":"tags/#format-group","text":"\u683c\u5f0f\u7ec4","title":"format-group"},{"location":"tags/#formatting","text":"1.8 - \u7a7a\u767d\u7b26\u548c\u57fa\u672c\u6392\u7248","title":"formatting"},{"location":"tags/#forward-declaration","text":"\u524d\u5411\u58f0\u660e","title":"forward declaration"},{"location":"tags/#friend","text":"13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b 13.16 - \u533f\u540d\u5bf9\u8c61 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b 13.18 - \u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26","title":"friend"},{"location":"tags/#friend-class","text":"\u53cb\u5143\u7c7b","title":"friend-class"},{"location":"tags/#friend-function","text":"\u53cb\u5143\u51fd\u6570","title":"friend-function"},{"location":"tags/#function","text":"12.1 - \u51fd\u6570\u6307\u9488 2.1 - \u51fd\u6570\u7b80\u4ecb 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 2.6 - \u4e3a\u4ec0\u4e48\u51fd\u6570\u6709\u7528\uff0c\u5982\u4f55\u9ad8\u6548\u4f7f\u7528\u51fd\u6570","title":"function"},{"location":"tags/#function-template","text":"8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f","title":"function template"},{"location":"tags/#function-instance","text":"\u51fd\u6570\u5b9e\u4f8b","title":"function-instance"},{"location":"tags/#function-literal","text":"\u51fd\u6570\u5b57\u9762\u91cf","title":"function-literal"},{"location":"tags/#function-pointer","text":"\u51fd\u6570\u6307\u9488","title":"function-pointer"},{"location":"tags/#function-template_1","text":"\u51fd\u6570\u6a21\u677f","title":"function-template"},{"location":"tags/#function-template-instantiation","text":"\u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316","title":"function-template-instantiation"},{"location":"tags/#generic-algorithm","text":"\u6cdb\u578b\u7b97\u6cd5","title":"generic-algorithm"},{"location":"tags/#global","text":"6.4 - \u5168\u5c40\u53d8\u91cf 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf","title":"global"},{"location":"tags/#global-variable","text":"6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c \u5168\u5c40\u53d8\u91cf","title":"global variable"},{"location":"tags/#glossary","text":"\u7f16\u8bd1\u65f6 \u58f0\u660e \u5b9a\u4e49 \u524d\u5411\u58f0\u660e \u5168\u5c40\u53d8\u91cf \u6807\u8bc6\u7b26(identifier) \u5b9e\u4f8b\u5316 \u6307\u4ee4 \u94fe\u63a5\u5668(linker) \u5b8f \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u5f62\u53c2 \u6309\u503c\u4f20\u9012 \u9884\u5904\u7406\u5668\u6307\u4ee4 \u9884\u5904\u7406\u5668 \u7eaf\u58f0\u660e(pure-declaration) \u9650\u5b9a\u7b26 \u91cd\u6784 \u6309\u503c\u8fd4\u56de \u8fd0\u884c\u65f6 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u4f5c\u7528\u57df \u8bed\u53e5 \u7b26\u53f7\u5e38\u91cf","title":"glossary"},{"location":"tags/#goto","text":"7.6 - goto \u8bed\u53e5","title":"goto"},{"location":"tags/#halt","text":"7.11 - \u6302\u8d77\uff08\u63d0\u524d\u7ed3\u675f\u7a0b\u5e8f\uff09","title":"halt"},{"location":"tags/#header","text":"2.11 - \u5934\u6587\u4ef6","title":"header"},{"location":"tags/#header-guards","text":"2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e","title":"header guards"},{"location":"tags/#heap","text":"12.2 - \u6808\u548c\u5806 \u5806","title":"heap"},{"location":"tags/#identifier","text":"1.7 - \u5173\u952e\u5b57\u548c\u6807\u8bc6\u7b26 \u6807\u8bc6\u7b26(identifier)","title":"identifier"},{"location":"tags/#identity","text":"\u8eab\u4efd\u7279\u5f81","title":"identity"},{"location":"tags/#if","text":"4.10 - if \u8bed\u53e5\u7b80\u4ecb 7.2 - if \u8bed\u53e5\u548c\u8bed\u53e5\u5757 7.3 - \u5e38\u89c1\u7684 if \u8bed\u53e5\u9519\u8bef","title":"if"},{"location":"tags/#immediate-functions","text":"\u5373\u65f6\u51fd\u6570","title":"immediate-functions"},{"location":"tags/#implicit-constructor","text":"\u9690\u5f0f\u6784\u9020\u51fd\u6570","title":"implicit-constructor"},{"location":"tags/#implicit-object","text":"\u9690\u5f0f\u5bf9\u8c61","title":"implicit-object"},{"location":"tags/#implicit-type-conversion","text":"\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362","title":"implicit-type-conversion"},{"location":"tags/#inheritance","text":"17.1 - \u7ee7\u627f\u7b80\u4ecb 17.2 - C++\u7ee7\u627f\u57fa\u7840 17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f 17.4 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u548c\u521d\u59cb\u5316 17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 17.6 - \u4e3a\u6d3e\u751f\u7c7b\u6dfb\u52a0\u65b0\u529f\u80fd 17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570 17.9 - \u591a\u91cd\u7ee7\u627f 18.11 - \u4f7f\u7528 << \u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b \u7ee7\u627f","title":"inheritance"},{"location":"tags/#initialization","text":"1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u62f7\u8d1d\u521d\u59cb\u5316 \u76f4\u63a5\u521d\u59cb\u5316 \u5217\u8868\u521d\u59cb\u5316","title":"initialization"},{"location":"tags/#initializer-list","text":"\u521d\u59cb\u5316\u503c\u5217\u8868","title":"initializer-list"},{"location":"tags/#initializer_list","text":"16.7 - std::initializer_list","title":"initializer_list"},{"location":"tags/#inline","text":"6.13 - \u5185\u8054\u51fd\u6570 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf","title":"inline"},{"location":"tags/#inline-namespace","text":"6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4","title":"inline namespace"},{"location":"tags/#inline-function","text":"\u5185\u8054\u51fd\u6570","title":"inline-function"},{"location":"tags/#inline-namespace_1","text":"\u5185\u8054\u547d\u540d\u7a7a\u95f4","title":"inline-namespace"},{"location":"tags/#inline-variables","text":"\u5185\u8054\u53d8\u91cf","title":"inline-variables"},{"location":"tags/#input-manipulator","text":"4.17 - std::string \u7b80\u4ecb","title":"input-manipulator"},{"location":"tags/#input-manipulators","text":"\u8f93\u5165\u64cd\u7eb5\u5668","title":"input-manipulators"},{"location":"tags/#insertion-operator","text":"\u63d2\u5165\u8fd0\u7b97\u7b26","title":"insertion-operator"},{"location":"tags/#instance","text":"\u5b9e\u4f8b","title":"instance"},{"location":"tags/#instantiated","text":"\u5b9e\u4f8b\u5316","title":"instantiated"},{"location":"tags/#instuction","text":"\u6307\u4ee4","title":"instuction"},{"location":"tags/#integer","text":"4.4 - \u6709\u7b26\u53f7\u6574\u578b 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t","title":"integer"},{"location":"tags/#integral-type","text":"\u6574\u578b\u7c7b\u578b","title":"integral-type"},{"location":"tags/#interface-class","text":"\u63a5\u53e3\u7c7b","title":"interface-class"},{"location":"tags/#interface-classes","text":"18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b","title":"interface-classes"},{"location":"tags/#internal-linkage","text":"\u5185\u90e8\u94fe\u63a5","title":"internal-linkage"},{"location":"tags/#internal-variable","text":"\u5185\u90e8\u53d8\u91cf","title":"internal-variable"},{"location":"tags/#io","text":"23.1 - \u8f93\u5165\u8f93\u51fa\u6d41 23.2 - \u4f7f\u7528istream\u5904\u7406\u8f93\u5165 23.3 - \u4f7f\u7528ostream\u548cios\u5904\u7406\u8f93\u51fa 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b 23.5 - \u6d41\u72b6\u6001\u548c\u8f93\u5165\u9a8c\u8bc1 23.6 - \u57fa\u672c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa 23.7 - \u968f\u673a\u6587\u4ef6\u8f93\u5165\u8f93\u51fa","title":"io"},{"location":"tags/#iostream","text":"1.5 - iostream \u7b80\u4ecb\uff1acout\uff0ccin \u548c endl","title":"iostream"},{"location":"tags/#iterating","text":"\u904d\u5386","title":"iterating"},{"location":"tags/#iterator","text":"11.18 \u2014 \u8fed\u4ee3\u5668\u7b80\u4ecb \u8fed\u4ee3\u5668","title":"iterator"},{"location":"tags/#lambda","text":"12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 \u533f\u540d\u51fd\u6570","title":"lambda"},{"location":"tags/#late-binding_1","text":"18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a","title":"late-binding"},{"location":"tags/#link","text":"\u94fe\u63a5","title":"link"},{"location":"tags/#linkage","text":"6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 6.3 - \u5c40\u90e8\u53d8\u91cf 6.6 - \u5185\u90e8\u94fe\u63a5 6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e \u94fe\u63a5\u5c5e\u6027","title":"linkage"},{"location":"tags/#linker","text":"\u94fe\u63a5\u5668(linker)","title":"linker"},{"location":"tags/#list-assignment-operator","text":"\u7c7b\u522b\u8d4b\u503c\u8fd0\u7b97\u7b26","title":"list assignment operator"},{"location":"tags/#list-constructor","text":"\u5217\u8868\u6784\u9020\u51fd\u6570","title":"list-constructor"},{"location":"tags/#list-initialization","text":"\u5217\u8868\u521d\u59cb\u5316","title":"list-initialization"},{"location":"tags/#literal","text":"4.15 - \u5b57\u9762\u91cf","title":"literal"},{"location":"tags/#literals","text":"1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 \u5b57\u9762\u91cf","title":"literals"},{"location":"tags/#local-variable","text":"6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf","title":"local variable"},{"location":"tags/#local-variables","text":"6.3 - \u5c40\u90e8\u53d8\u91cf","title":"local variables"},{"location":"tags/#loop","text":"11.3 - \u6570\u7ec4\u548c\u5faa\u73af","title":"loop"},{"location":"tags/#lvalue_1","text":"\u5de6\u503c","title":"lvalue"},{"location":"tags/#lvalue-references","text":"9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528","title":"lvalue references"},{"location":"tags/#lvalue-reference","text":"\u5de6\u503c\u5f15\u7528","title":"lvalue-reference"},{"location":"tags/#lvalues","text":"9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09","title":"lvalues"},{"location":"tags/#macro","text":"\u5b8f","title":"macro"},{"location":"tags/#member-access-operator","text":"\u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26","title":"member access operator"},{"location":"tags/#member-selection","text":"10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9","title":"member selection"},{"location":"tags/#member-function","text":"\u6210\u5458\u51fd\u6570","title":"member-function"},{"location":"tags/#member-initializer-list","text":"\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868","title":"member-initializer-list"},{"location":"tags/#member-selection-operator","text":"\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26","title":"member-selection-operator"},{"location":"tags/#member-types","text":"\u6210\u5458\u7c7b\u578b","title":"member-types"},{"location":"tags/#member-variable","text":"\u6210\u5458\u53d8\u91cf","title":"member-variable"},{"location":"tags/#members","text":"10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9","title":"members"},{"location":"tags/#memberwise-initialization","text":"\u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316","title":"memberwise initialization"},{"location":"tags/#memberwise-assignment","text":"\u6210\u5458\u4f9d\u6b21\u8d4b\u503c","title":"memberwise-assignment"},{"location":"tags/#memberwise-copy","text":"\u6210\u5458\u4f9d\u6b21\u62f7\u8d1d","title":"memberwise-copy"},{"location":"tags/#memory-leak","text":"\u5185\u5b58\u6cc4\u6f0f","title":"memory-leak"},{"location":"tags/#mixin","text":"None","title":"mixin"},{"location":"tags/#move","text":"M.4 - std::move M.5 - std::move_if_noexcept M.6 \u2014 std::unique_ptr","title":"move"},{"location":"tags/#move-assignment-operator","text":"\u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26","title":"move assignment operator"},{"location":"tags/#move-constructor","text":"\u79fb\u52a8\u6784\u9020\u51fd\u6570","title":"move-constructor"},{"location":"tags/#move-semantics","text":"M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb \u79fb\u52a8\u8bed\u4e49","title":"move-semantics"},{"location":"tags/#multiple-inheritance_1","text":"17.9 - \u591a\u91cd\u7ee7\u627f","title":"multiple-inheritance"},{"location":"tags/#named-cast","text":"\u5177\u540d\u7c7b\u578b\u8f6c\u6362","title":"named-cast"},{"location":"tags/#namespace","text":"2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u547d\u540d\u7a7a\u95f4","title":"namespace"},{"location":"tags/#namespace-aliases","text":"6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26","title":"namespace aliases"},{"location":"tags/#narrowing-convertions","text":"\u7f29\u7a84\u8f6c\u6362","title":"narrowing-convertions"},{"location":"tags/#new","text":"11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d","title":"new"},{"location":"tags/#numeric-conversions","text":"8.3 - \u6570\u503c\u8f6c\u6362","title":"numeric conversions"},{"location":"tags/#numeric-promotion","text":"8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347","title":"numeric promotion"},{"location":"tags/#numeric-promotions","text":"\u6570\u503c\u63d0\u5347","title":"numeric promotions"},{"location":"tags/#numeric-conversions_1","text":"\u6570\u503c\u8f6c\u6362","title":"numeric-conversions"},{"location":"tags/#object","text":"1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf 13.1 - \u6b22\u8fce\u6765\u5230\u9762\u5411\u5bf9\u8c61\u7684\u4e16\u754c 16.1 - \u5bf9\u8c61\u5173\u7cfb 16.2 - \u7ec4\u5408\u5173\u7cfb 16.3 - \u805a\u5408\u5173\u7cfb 16.4 - \u5173\u8054\u5173\u7cfb 16.5 - \u4f9d\u8d56\u5173\u7cfb 16.6 - \u5bb9\u5668\u7c7b 16.7 - std::initializer_list","title":"object"},{"location":"tags/#object-composition","text":"\u5bf9\u8c61\u7ec4\u5408","title":"object-composition"},{"location":"tags/#object-like-macros","text":"\u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f","title":"object-like-macros"},{"location":"tags/#object-slicing","text":"\u5bf9\u8c61\u5207\u7247","title":"object-slicing"},{"location":"tags/#one-definition-rule","text":"\u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule)","title":"one-definition-rule"},{"location":"tags/#operands","text":"\u64cd\u4f5c\u6570","title":"operands"},{"location":"tags/#operator","text":"1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 5.1 - \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 5.3 - \u6c42\u6a21\u548c\u6307\u6570\u8fd0\u7b97 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 5.5 - \u9017\u53f7\u548c\u6761\u4ef6\u8fd0\u7b97\u7b26 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 5.7 - \u903b\u8f91\u8fd0\u7b97\u7b26 \u8fd0\u7b97\u7b26","title":"operator"},{"location":"tags/#operator_1","text":"18.11 - \u4f7f\u7528 << \u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b","title":"operator<<"},{"location":"tags/#optional-parameter","text":"\u53ef\u9009\u53c2\u6570","title":"optional-parameter"},{"location":"tags/#overflow","text":"4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83","title":"overflow"},{"location":"tags/#overlapping","text":"13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570","title":"overlapping"},{"location":"tags/#overload","text":"14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26 14.15 - \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 8.10 - \u91cd\u8f7d\u51fd\u6570\u7684\u533a\u5206 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u4e0d\u660e\u786e\u5339\u914d \u91cd\u8f7d","title":"overload"},{"location":"tags/#overload-resolution","text":"\u91cd\u8f7d\u89e3\u6790","title":"overload-resolution"},{"location":"tags/#overloading","text":"8.9 - \u51fd\u6570\u91cd\u8f7d","title":"overloading"},{"location":"tags/#override","text":"18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b \u91cd\u5199","title":"override"},{"location":"tags/#overriding","text":"17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570","title":"overriding"},{"location":"tags/#overriding-virtualization","text":"18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316","title":"overriding-virtualization"},{"location":"tags/#parameter","text":"2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2","title":"parameter"},{"location":"tags/#parameters","text":"\u5f62\u53c2","title":"parameters"},{"location":"tags/#parent-class","text":"\u7236\u7c7b","title":"parent-class"},{"location":"tags/#parenthesis","text":"14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26","title":"parenthesis"},{"location":"tags/#pass-by-address","text":"\u6309\u5730\u5740\u4f20\u9012","title":"pass-by-address"},{"location":"tags/#pass-by-reference","text":"\u6309\u5f15\u7528\u4f20\u9012","title":"pass-by-reference"},{"location":"tags/#pass-by-value","text":"\u6309\u503c\u4f20\u9012","title":"pass-by-value"},{"location":"tags/#placeholder-types","text":"\u5360\u4f4d\u7b26\u7c7b\u578b","title":"placeholder-types"},{"location":"tags/#pointer","text":"11.15 \u2014 \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u548c\u591a\u7ef4\u6570\u7ec4 11.8 - \u6307\u9488\u548c\u6570\u7ec4 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 9.6 - \u6307\u9488\u7b80\u4ecb 9.7 - \u7a7a\u6307\u9488 9.8 - \u6307\u9488\u548cconst 9.9 - \u6309\u5730\u5740\u4f20\u9012","title":"pointer"},{"location":"tags/#pointer-to-const","text":"\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488","title":"pointer-to-const"},{"location":"tags/#pointers","text":"18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528","title":"pointers"},{"location":"tags/#polymorphism","text":"18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001 \u591a\u6001","title":"polymorphism"},{"location":"tags/#pragma-once","text":"2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e","title":"pragma once"},{"location":"tags/#preprocessor","text":"2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb \u9884\u5904\u7406\u5668\u6307\u4ee4 \u9884\u5904\u7406\u5668","title":"preprocessor"},{"location":"tags/#private-inheritance","text":"\u79c1\u6709\u7ee7\u627f","title":"private-inheritance"},{"location":"tags/#private-member","text":"\u79c1\u6709\u6210\u5458","title":"private-member"},{"location":"tags/#program-organization","text":"2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f","title":"program organization"},{"location":"tags/#protected-inheritance","text":"\u53d7\u4fdd\u62a4\u7ee7\u627f","title":"protected-inheritance"},{"location":"tags/#protected-members","text":"\u53d7\u4fdd\u62a4\u6210\u5458","title":"protected-members"},{"location":"tags/#public-inheritance","text":"\u516c\u5f00\u7ee7\u627f","title":"public-inheritance"},{"location":"tags/#public-member","text":"\u516c\u6709\u6210\u5458","title":"public-member"},{"location":"tags/#pure-declaration","text":"\u7eaf\u58f0\u660e(pure-declaration)","title":"pure-declaration"},{"location":"tags/#pure-virtual","text":"\u7eaf\u865a\u51fd\u6570","title":"pure-virtual"},{"location":"tags/#pure-virtual-functions","text":"18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b","title":"pure-virtual-functions"},{"location":"tags/#qualifier","text":"\u9650\u5b9a\u7b26","title":"qualifier"},{"location":"tags/#random-access-memory","text":"\u968f\u673a\u8bbf\u95ee\u5185\u5b58","title":"random-access-memory"},{"location":"tags/#range-based-for-loops","text":"\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af","title":"range-based-for-loops"},{"location":"tags/#raw-pointer","text":"\u539f\u59cb\u6307\u9488","title":"raw-pointer"},{"location":"tags/#recursion","text":"12.4 - \u9012\u5f52","title":"recursion"},{"location":"tags/#refactoring","text":"\u91cd\u6784","title":"refactoring"},{"location":"tags/#reference","text":"9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de","title":"reference"},{"location":"tags/#reference-container","text":"\u5f15\u7528\u5bb9\u5668","title":"reference-container"},{"location":"tags/#references","text":"18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 M.2 - \u53f3\u503c\u5f15\u7528 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c","title":"references"},{"location":"tags/#reflexive-association","text":"\u53cd\u8eab\u5173\u8054","title":"reflexive association"},{"location":"tags/#reinterpret-casts","text":"\u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362","title":"reinterpret-casts"},{"location":"tags/#return","text":"2.2 - \u51fd\u6570\u8fd4\u56de\u503c 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de","title":"return"},{"location":"tags/#return-by-address","text":"\u6309\u5730\u5740\u8fd4\u56de","title":"return-by-address"},{"location":"tags/#return-by-reference","text":"\u6309\u5f15\u7528\u8fd4\u56de","title":"return-by-reference"},{"location":"tags/#return-by-value","text":"\u6309\u503c\u8fd4\u56de","title":"return-by-value"},{"location":"tags/#return-type-deduction","text":"\u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad","title":"return-type-deduction"},{"location":"tags/#rounding-error","text":"\u820d\u5165\u8bef\u5dee","title":"rounding-error"},{"location":"tags/#runtime","text":"\u8fd0\u884c\u65f6","title":"runtime"},{"location":"tags/#rvalue","text":"\u53f3\u503c","title":"rvalue"},{"location":"tags/#rvalue-reference","text":"\u53f3\u503c\u5f15\u7528","title":"rvalue-reference"},{"location":"tags/#rvalues","text":"9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09","title":"rvalues"},{"location":"tags/#scaling","text":"\u6307\u9488\u7f29\u653e","title":"scaling"},{"location":"tags/#scientific-notation","text":"4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5","title":"scientific notation"},{"location":"tags/#scope","text":"2.5 - \u5c40\u90e8\u4f5c\u7528\u57df 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 \u4f5c\u7528\u57df","title":"scope"},{"location":"tags/#scope-resolution-operator","text":"6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26","title":"scope resolution operator"},{"location":"tags/#scope-resolution-operator_1","text":"\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26","title":"scope-resolution-operator"},{"location":"tags/#scoped-enumerations","text":"10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09","title":"scoped enumerations"},{"location":"tags/#scoped-enumerations_1","text":"\u6709\u4f5c\u7528\u57df\u679a\u4e3e","title":"scoped-enumerations"},{"location":"tags/#segment","text":"\u5185\u5b58\u6bb5","title":"segment"},{"location":"tags/#sentinel","text":"\u54e8\u5175","title":"sentinel"},{"location":"tags/#setup","text":"0.8 - \u4e00\u4e9b\u5178\u578b\u7684 C++ \u95ee\u9898","title":"setup"},{"location":"tags/#shadow","text":"\u53d8\u91cf\u906e\u853d","title":"shadow"},{"location":"tags/#shallow-copy","text":"14.16 - \u6d45\u62f7\u8d1d\u548c\u6df1\u62f7\u8d1d 14.17 - \u91cd\u8f7d\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u6a21\u677f","title":"shallow copy"},{"location":"tags/#shallow-copy_1","text":"\u6d45\u62f7\u8d1d","title":"shallow-copy"},{"location":"tags/#shared_ptr","text":"M.7 \u2014 std::shared_ptr M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898","title":"shared_ptr"},{"location":"tags/#side-effects","text":"\u526f\u4f5c\u7528","title":"side-effects"},{"location":"tags/#signature","text":"\u7b7e\u540d","title":"signature"},{"location":"tags/#size_t","text":"4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t","title":"size_t"},{"location":"tags/#sizeof","text":"4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26","title":"sizeof"},{"location":"tags/#smart-pointer-class","text":"\u667a\u80fd\u6307\u9488\u7c7b","title":"smart pointer class"},{"location":"tags/#smart-pointer","text":"\u667a\u80fd\u6307\u9488","title":"smart-pointer"},{"location":"tags/#smart-pointers","text":"M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb","title":"smart-pointers"},{"location":"tags/#sort","text":"11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f","title":"sort"},{"location":"tags/#stack","text":"12.2 - \u6808\u548c\u5806 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a \u6808","title":"stack"},{"location":"tags/#stack-frame","text":"\u6808\u5e27","title":"stack-frame"},{"location":"tags/#stack-overflow","text":"\u5806\u6808\u6ea2\u51fa","title":"stack-overflow"},{"location":"tags/#standard-stream","text":"\u6807\u51c6\u6d41","title":"standard-stream"},{"location":"tags/#statement","text":"6.1 - \u590d\u5408\u8bed\u53e5\uff08\u8bed\u53e5\u5757\uff09 \u8bed\u53e5","title":"statement"},{"location":"tags/#statements","text":"1.1 - \u7a0b\u5e8f\u7ed3\u6784\u548c\u8bed\u53e5","title":"statements"},{"location":"tags/#static","text":"6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf","title":"static"},{"location":"tags/#static-membe-function","text":"13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570","title":"static membe function"},{"location":"tags/#static-member","text":"13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316","title":"static member"},{"location":"tags/#static-member-variable","text":"13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf","title":"static member variable"},{"location":"tags/#static-cast","text":"8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast","title":"static-cast"},{"location":"tags/#static-casts","text":"\u9759\u6001\u7c7b\u578b\u8f6c\u6362","title":"static-casts"},{"location":"tags/#static-initialization","text":"\u9759\u6001\u521d\u59cb\u5316","title":"static-initialization"},{"location":"tags/#static-member-functions","text":"\u9759\u6001\u6210\u5458\u51fd\u6570","title":"static-member-functions"},{"location":"tags/#static-member-variables","text":"\u9759\u6001\u6210\u5458\u53d8\u91cf","title":"static-member-variables"},{"location":"tags/#static-memory-allocation","text":"\u9759\u6001\u5185\u5b58\u5206\u914d","title":"static-memory-allocation"},{"location":"tags/#static-storage-duration","text":"\u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4","title":"static-storage-duration"},{"location":"tags/#static-variables","text":"\u9759\u6001\u53d8\u91cf","title":"static-variables"},{"location":"tags/#static_assert","text":"7.17 - \u65ad\u8a00\u548c static_assert","title":"static_assert"},{"location":"tags/#static_cast","text":"4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast 8.3 - \u6570\u503c\u8f6c\u6362","title":"static_cast"},{"location":"tags/#stdarray","text":"11.16 \u2014 \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb","title":"std::array"},{"location":"tags/#stdstring_view","text":"11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09","title":"std::string_view"},{"location":"tags/#stl","text":"21.1 - \u6807\u51c6\u5e93 21.2 - STL\u5bb9\u5668 21.3 - STL\u8fed\u4ee3\u5668 21.4 - STL\u7b97\u6cd5","title":"stl"},{"location":"tags/#storage-duration","text":"6.3 - \u5c40\u90e8\u53d8\u91cf 6.4 - \u5168\u5c40\u53d8\u91cf","title":"storage duration"},{"location":"tags/#storage-duration_1","text":"\u5b58\u50a8\u6301\u7eed\u65f6\u95f4","title":"storage-duration"},{"location":"tags/#string","text":"11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 22.1 - std::string \u548c std::wstring 22.2 - std-string \u7684\u6784\u9020\u548c\u6790\u6784 22.3 - std-string \u7684\u957f\u5ea6\u548c\u5bb9\u91cf 22.4 - std-string \u5b57\u7b26\u8bbf\u95ee\u548cC\u5b57\u7b26\u6570\u7ec4\u8f6c\u6362 22.5 - std-string \u8d4b\u503c\u548c\u4ea4\u6362 22.6 - std-string \u8ffd\u52a0 22.7 - std-string \u63d2\u5165 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b 4.17 - std::string \u7b80\u4ecb","title":"string"},{"location":"tags/#string_view","text":"4.18 - std::string_view \u7b80\u4ecb","title":"string_view"},{"location":"tags/#strong-except","text":"\u5f3a\u5f02\u5e38\u4fdd\u8bc1","title":"strong-except"},{"location":"tags/#struct","text":"10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 10.8 - \u7ed3\u6784\u4f53\u4f20\u9012\u53ca\u5176\u4ed6 10.9 - \u57fa\u4e8e\u6307\u9488\u548c\u5f15\u7528\u7684\u6210\u5458\u9009\u62e9","title":"struct"},{"location":"tags/#struct-padding","text":"\u7ed3\u6784\u4f53\u586b\u5145","title":"struct-padding"},{"location":"tags/#subscript","text":"14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26","title":"subscript"},{"location":"tags/#subscripts","text":"\u4e0b\u6807","title":"subscripts"},{"location":"tags/#summary","text":"1.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - C++\u57fa\u7840 10.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u679a\u4e3e\u548c\u7ed3\u6784\u4f53 10.y - \u5b66\u4f1a\u4f7f\u7528C++\u53c2\u8003\u624b\u518c 11.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5206\u914d 12.8 - lambda \u95ed\u5305 12.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570 13.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u9762\u5411\u5bf9\u8c61\u57fa\u7840 14.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26\u91cd\u8f7d 16.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5bf9\u8c61\u5173\u7cfb 17.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7ee7\u627f 18.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u865a\u51fd\u6570 19.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6a21\u677f\u548c\u7c7b 2.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570\u4e0e\u6587\u4ef6 20.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f02\u5e38 3.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4ee3\u7801\u8c03\u8bd5 4.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u57fa\u672c\u6570\u636e\u7c7b\u578b 5.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 7.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406 8.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d 9.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f15\u7528\u548c\u6307\u9488 M.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u79fb\u52a8\u548c\u667a\u80fd\u6307\u9488","title":"summary"},{"location":"tags/#super-class","text":"\u8d85\u7c7b","title":"super-class"},{"location":"tags/#switch","text":"7.4 - switch \u8bed\u53e5\u57fa\u7840 7.5 - switch fallthrough\u5c5e\u6027\u548c\u4f5c\u7528\u57df","title":"switch"},{"location":"tags/#symbolic-constants","text":"11.10 - C \u98ce\u683c\u5b57\u7b26\u4e32\u7b26\u53f7\u5e38\u91cf","title":"symbolic constants"},{"location":"tags/#symbolic-constants_1","text":"\u7b26\u53f7\u5e38\u91cf","title":"symbolic-constants"},{"location":"tags/#template-class-specialization","text":"\u7c7b\u6a21\u677f\u7279\u5316","title":"template class specialization"},{"location":"tags/#testing","text":"7.12 - \u4ee3\u7801\u6d4b\u8bd5","title":"testing"},{"location":"tags/#the-diamond-problem","text":"\u83f1\u5f62\u7ee7\u627f\u95ee\u9898","title":"the-diamond-problem"},{"location":"tags/#this","text":"13.10 - \u9690\u85cf\u7684this\u6307\u9488","title":"this"},{"location":"tags/#translation","text":"\u7ffb\u8bd1","title":"translation"},{"location":"tags/#type","text":"10.1 - \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7b80\u4ecb","title":"type"},{"location":"tags/#type-conversion","text":"4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362","title":"type conversion"},{"location":"tags/#type-deduction","text":"8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u63a8\u65ad","title":"type deduction"},{"location":"tags/#type-aliases","text":"\u7c7b\u578b\u522b\u540d","title":"type-aliases"},{"location":"tags/#type-conversion_1","text":"\u7c7b\u578b\u8f6c\u6362","title":"type-conversion"},{"location":"tags/#type-definition","text":"\u7c7b\u578b\u5b9a\u4e49","title":"type-definition"},{"location":"tags/#type-inference","text":"\u7c7b\u578b\u63a8\u65ad","title":"type-inference"},{"location":"tags/#type-safe","text":"\u7c7b\u578b\u5b89\u5168","title":"type-safe"},{"location":"tags/#type-signature","text":"\u7c7b\u578b\u7b7e\u540d","title":"type-signature"},{"location":"tags/#typecast","text":"14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26","title":"typecast"},{"location":"tags/#typedef","text":"8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d","title":"typedef"},{"location":"tags/#typeid","text":"typeid \u8fd0\u7b97\u7b26","title":"typeid"},{"location":"tags/#typeinfo","text":"8.4 - \u7b97\u672f\u8f6c\u6362","title":"typeinfo"},{"location":"tags/#unary","text":"14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26","title":"unary"},{"location":"tags/#undefined-behavior","text":"1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 4.4 - \u6709\u7b26\u53f7\u6574\u578b","title":"undefined behavior"},{"location":"tags/#undefined-behavior_1","text":"\u672a\u5b9a\u4e49\u884c\u4e3a","title":"undefined-behavior"},{"location":"tags/#uniform-initialization","text":"\u7edf\u4e00\u521d\u59cb\u5316","title":"uniform initialization"},{"location":"tags/#unmanned-namespace","text":"6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4","title":"unmanned namespace"},{"location":"tags/#unnamed-namespace","text":"\u533f\u540d\u547d\u540d\u7a7a\u95f4","title":"unnamed-namespace"},{"location":"tags/#unscoped-enumerations","text":"10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b 10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa","title":"unscoped enumerations"},{"location":"tags/#unscoped-enumerations_1","text":"\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b","title":"unscoped-enumerations"},{"location":"tags/#upcasting","text":"\u5411\u4e0a\u8f6c\u6362","title":"upcasting"},{"location":"tags/#using","text":"6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4","title":"using"},{"location":"tags/#using-declaration","text":"using \u58f0\u660e","title":"using-declaration"},{"location":"tags/#using-directive","text":"using \u6307\u4ee4","title":"using-directive"},{"location":"tags/#value-category","text":"\u503c\u7c7b\u578b","title":"value-category"},{"location":"tags/#value-container","text":"\u503c\u5bb9\u5668","title":"value-container"},{"location":"tags/#value-initialization","text":"\u503c\u521d\u59cb\u5316","title":"value-initialization"},{"location":"tags/#value-preserving","text":"\u503c\u4fdd\u7559","title":"value-preserving"},{"location":"tags/#variable","text":"1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316","title":"variable"},{"location":"tags/#variable-shadowing","text":"6.5 - \u53d8\u91cf\u906e\u853d","title":"variable shadowing"},{"location":"tags/#vector","text":"11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a","title":"vector"},{"location":"tags/#virtual-functions","text":"18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001","title":"virtual functions"},{"location":"tags/#virtual-assignment","text":"18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316","title":"virtual-assignment"},{"location":"tags/#virtual-base-class","text":"\u865a\u57fa\u7c7b","title":"virtual-base-class"},{"location":"tags/#virtual-base-classes","text":"18.8 - \u865a\u57fa\u7c7b 18.9 - \u5bf9\u8c61\u5207\u7247","title":"virtual-base-classes"},{"location":"tags/#virtual-destructor","text":"\u865a\u6790\u6784\u51fd\u6570","title":"virtual-destructor"},{"location":"tags/#virtual-destructors","text":"18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316","title":"virtual-destructors"},{"location":"tags/#virtual-function","text":"\u865a\u51fd\u6570","title":"virtual-function"},{"location":"tags/#virtual-table","text":"18.6 - \u865a\u51fd\u6570\u8868 \u865a\u8868","title":"virtual-table"},{"location":"tags/#void","text":"11.14 \u2014 void \u6307\u9488 2.3 - void \u51fd\u6570 4.2 - Void","title":"void"},{"location":"tags/#void-pointer","text":"void \u6307\u9488","title":"void pointer"},{"location":"tags/#weak_ptr","text":"M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898","title":"weak_ptr"},{"location":"tags/#while","text":"7.7 - \u5faa\u73af\u548c while \u8bed\u53e5","title":"while"},{"location":"tags/#wild-pointer","text":"\u91ce\u6307\u9488","title":"wild-pointer"},{"location":"tags/#word","text":"\u5b57\u957f","title":"word"},{"location":"tags/#wstring","text":"22.1 - std::string \u548c std::wstring","title":"wstring"},{"location":"tags/#zero-initialization","text":"0\u521d\u59cb\u5316","title":"zero-initialization"},{"location":"tags/#function-prototype","text":"\u51fd\u6570\u539f\u578b","title":"\u51fd\u6570\u539f\u578b(function prototype)"},{"location":"tags/#_1","text":"\u521d\u59cb\u5316","title":"\u521d\u59cb\u5316"},{"location":"tags/#initializer","text":"\u521d\u59cb\u5316\u503c","title":"\u521d\u59cb\u5316\u503c(initializer)"},{"location":"tags/#_2","text":"\u52a8\u6001\u6570\u7ec4","title":"\u52a8\u6001\u6570\u7ec4"},{"location":"tags/#objec","text":"\u5bf9\u8c61","title":"\u5bf9\u8c61(objec)"},{"location":"tags/#_3","text":"\u62bd\u8c61\u7c7b","title":"\u62bd\u8c61\u7c7b"},{"location":"tags/#_4","text":"None","title":"\u62ec\u53f7\u521d\u59cb\u5316"},{"location":"tags/#_5","text":"\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26","title":"\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26"},{"location":"tags/#_6","text":"\u6784\u9020\u51fd\u6570","title":"\u6784\u9020\u51fd\u6570"},{"location":"tags/#_7","text":"\u6a21\u677f\u7c7b\u578b\u53c2\u6570","title":"\u6a21\u677f\u7c7b\u578b\u53c2\u6570"},{"location":"tags/#_8","text":"\u79bb\u5f00\u4f5c\u7528\u57df","title":"\u79bb\u5f00\u4f5c\u7528\u57df"},{"location":"tags/#trivial-conversion","text":"\u7b80\u5355\u8f6c\u6362","title":"\u7b80\u5355\u8f6c\u6362(trivial conversion)"},{"location":"tags/#_9","text":"\u7c7b\u5b9e\u4f8b","title":"\u7c7b\u5b9e\u4f8b"},{"location":"tags/#snake_case","text":"\u86c7\u5f62\u547d\u540d\u6cd5","title":"\u86c7\u5f62\u547d\u540d\u6cd5(snake_case)"},{"location":"tags/#_10","text":"\u5173\u8054 \u5173\u8054\u5bb9\u5668 \u7ed1\u5b9a C \u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362 \u65e9\u671f\u7ed1\u5b9a \u5c01\u88c5 \u8868\u8fbe\u5f0f\u53c2\u6570 \u7f1d\u5408\u602a\u5bf9\u8c61 \u540e\u671f\u7ed1\u5b9a \u591a\u91cd\u7ee7\u627f \u540d\u79f0\u4fee\u9970 \u5dee\u4e00\u9519\u8bef \u8f93\u51fa\u64cd\u7eb5\u5668 \u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09 \u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f(RTTI) \u987a\u5e8f\u5bb9\u5668 \u6a21\u677f\u7279\u5316 \u7f29\u5199\u51fd\u6570\u6a21\u677f \u62bd\u8c61\u7c7b \u62bd\u8c61 \u6210\u5458\u8bbf\u95ee\u51fd\u6570 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u53d6\u5730\u5740\u64cd\u4f5c\u7b26 & \u805a\u5408\u6570\u636e\u7c7b\u578b \u805a\u5408\u521d\u59cb\u5316 \u805a\u5408 \u4e0d\u660e\u786e\u5339\u914d \u533f\u540d\u5bf9\u8c61 \u5b9e\u53c2 \u5143\u6570 \u81ea\u52a8\u5185\u5b58\u5206\u914d \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u57fa\u7c7b \u4f4d None \u6309\u4f4d\u6216 bss\u6bb5 \u5b57\u8282 \u8c03\u7528\u6808 \u53ef\u8c03\u7528\u5bf9\u8c61 \u56de\u8c03\u51fd\u6570 \u5b50\u7c7b \u7c7b\u5b9e\u4f8b \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad \u7c7b\u6a21\u677f \u7c7b\u7c7b\u578b \u95ed\u5305 \u4ee3\u7801\u6bb5 \u7f16\u8bd1\u5668 \u7ec4\u5408\u7c7b\u578b const \u7c7b\u578b\u8f6c\u6362 const \u6210\u5458\u51fd\u6570 \u5e38\u91cf\u6307\u9488 \u5e38\u91cf\u8868\u8fbe\u5f0f \u6784\u9020\u51fd\u6570 \u5bb9\u5668\u9002\u914d\u5668 \u5bb9\u5668\u7c7b \u8f6c\u6362\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u521d\u59cb\u5316 \u62f7\u8d1d\u8bed\u4e49 \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b \u60ac\u5782 \u6570\u636e\u6bb5 \u6df1\u62f7\u8d1d \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 \u9ed8\u8ba4\u521d\u59cb\u5316 \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c \u59d4\u6258\u6784\u9020\u51fd\u6570 \u4f9d\u8d56 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u6d3e\u751f\u7c7b \u6307\u5b9a\u521d\u59cb\u5316 \u6790\u6784\u51fd\u6570 \u76f4\u63a5\u521d\u59cb\u5316 \u5411\u4e0b\u8f6c\u6362 \u7b28\u6307\u9488 \u52a8\u6001\u6570\u7ec4 \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u52a8\u6001\u521d\u59cb\u5316 \u52a8\u6001\u5185\u5b58\u5206\u914d \u679a\u4e3e\u7c7b \u6c42\u503c \u5f02\u5e38\u5b89\u5168\u95ee\u9898 \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u8868\u8fbe\u5f0f\u7c7b\u578b \u5916\u90e8\u94fe\u63a5 \u5916\u90e8\u53d8\u91cf \u63d0\u53d6\u8fd0\u7b97\u7b26 \u8d2f\u7a7f\u5c5e\u6027 \u6587\u4ef6\u4f5c\u7528\u57df \u56fa\u5b9a\u6570\u7ec4 \u683c\u5f0f\u7ec4 \u53cb\u5143\u7c7b \u53cb\u5143\u51fd\u6570 \u51fd\u6570\u5b9e\u4f8b \u51fd\u6570\u5b57\u9762\u91cf \u51fd\u6570\u6307\u9488 \u51fd\u6570\u539f\u578b \u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316 \u51fd\u6570\u6a21\u677f \u6cdb\u578b\u7b97\u6cd5 \u79bb\u5f00\u4f5c\u7528\u57df \u5806 \u8eab\u4efd\u7279\u5f81 \u5373\u65f6\u51fd\u6570 \u9690\u5f0f\u6784\u9020\u51fd\u6570 \u9690\u5f0f\u5bf9\u8c61 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u7ee7\u627f \u521d\u59cb\u5316 \u521d\u59cb\u5316\u503c\u5217\u8868 \u521d\u59cb\u5316\u503c \u5185\u8054\u51fd\u6570 \u5185\u8054\u547d\u540d\u7a7a\u95f4 \u5185\u8054\u53d8\u91cf \u8f93\u5165\u64cd\u7eb5\u5668 \u63d2\u5165\u8fd0\u7b97\u7b26 \u5b9e\u4f8b \u6574\u578b\u7c7b\u578b \u63a5\u53e3\u7c7b \u5185\u90e8\u94fe\u63a5 \u5185\u90e8\u53d8\u91cf \u904d\u5386 \u8fed\u4ee3\u5668 \u533f\u540d\u51fd\u6570 \u94fe\u63a5 \u94fe\u63a5\u5c5e\u6027 \u7c7b\u522b\u8d4b\u503c\u8fd0\u7b97\u7b26 \u5217\u8868\u6784\u9020\u51fd\u6570 \u5217\u8868\u521d\u59cb\u5316 \u5b57\u9762\u91cf \u5de6\u503c\u5f15\u7528 \u5de6\u503c \u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26 \u6210\u5458\u51fd\u6570 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 \u6210\u5458\u7c7b\u578b \u6210\u5458\u53d8\u91cf \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \u6210\u5458\u4f9d\u6b21\u8d4b\u503c \u6210\u5458\u4f9d\u6b21\u62f7\u8d1d \u5185\u5b58\u6cc4\u6f0f None \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 \u79fb\u52a8\u6784\u9020\u51fd\u6570 \u79fb\u52a8\u8bed\u4e49 \u5177\u540d\u7c7b\u578b\u8f6c\u6362 \u547d\u540d\u7a7a\u95f4 \u7f29\u7a84\u8f6c\u6362 \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u6570\u503c\u63d0\u5347 \u6570\u503c\u8f6c\u6362 \u5bf9\u8c61\u7ec4\u5408 \u5bf9\u8c61\u5207\u7247 \u5bf9\u8c61 \u64cd\u4f5c\u6570 \u8fd0\u7b97\u7b26 \u8d85\u51fa\u4f5c\u7528\u57df \u91cd\u8f7d\u89e3\u6790 \u91cd\u8f7d \u91cd\u5199 \u7236\u7c7b \u6309\u5730\u5740\u4f20\u9012 \u6309\u5f15\u7528\u4f20\u9012 \u5360\u4f4d\u7b26\u7c7b\u578b \u6307\u5411\u5e38\u91cf\u7684\u6307\u9488 \u591a\u6001 \u79c1\u6709\u7ee7\u627f \u79c1\u6709\u6210\u5458 \u53d7\u4fdd\u62a4\u7ee7\u627f \u53d7\u4fdd\u62a4\u6210\u5458 \u516c\u5f00\u7ee7\u627f \u516c\u6709\u6210\u5458 \u7eaf\u865a\u51fd\u6570 \u968f\u673a\u8bbf\u95ee\u5185\u5b58 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u539f\u59cb\u6307\u9488 \u5f15\u7528\u5bb9\u5668 \u53cd\u8eab\u5173\u8054 \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 \u6309\u5730\u5740\u8fd4\u56de \u6309\u5f15\u7528\u8fd4\u56de \u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad \u820d\u5165\u8bef\u5dee \u53f3\u503c\u5f15\u7528 \u53f3\u503c \u6307\u9488\u7f29\u653e \u6709\u4f5c\u7528\u57df\u679a\u4e3e \u5185\u5b58\u6bb5 \u54e8\u5175 \u53d8\u91cf\u906e\u853d \u6d45\u62f7\u8d1d \u526f\u4f5c\u7528 \u7b7e\u540d \u667a\u80fd\u6307\u9488\u7c7b \u667a\u80fd\u6307\u9488 \u86c7\u5f62\u547d\u540d\u6cd5 \u6808\u5e27 \u5806\u6808\u6ea2\u51fa \u6808 \u6807\u51c6\u6d41 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u9759\u6001\u521d\u59cb\u5316 \u9759\u6001\u6210\u5458\u51fd\u6570 \u9759\u6001\u6210\u5458\u53d8\u91cf \u9759\u6001\u5185\u5b58\u5206\u914d \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u9759\u6001\u53d8\u91cf \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u5f3a\u5f02\u5e38\u4fdd\u8bc1 \u7ed3\u6784\u4f53\u586b\u5145 \u4e0b\u6807 \u8d85\u7c7b \u8bed\u6cd5 \u7c7b\u6a21\u677f\u7279\u5316 \u6a21\u677f\u7c7b\u578b\u53c2\u6570 \u83f1\u5f62\u7ee7\u627f\u95ee\u9898 \u7ffb\u8bd1 \u7b80\u5355\u8f6c\u6362 \u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u522b\u540d \u7c7b\u578b\u8f6c\u6362 \u7c7b\u578b\u5b9a\u4e49 \u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u5b89\u5168 \u7c7b\u578b\u7b7e\u540d typeid \u8fd0\u7b97\u7b26 \u672a\u5b9a\u4e49\u884c\u4e3a \u7edf\u4e00\u521d\u59cb\u5316 \u533f\u540d\u547d\u540d\u7a7a\u95f4 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u5411\u4e0a\u8f6c\u6362 using \u58f0\u660e using \u6307\u4ee4 \u503c\u7c7b\u578b \u503c\u5bb9\u5668 \u503c\u521d\u59cb\u5316 \u503c\u4fdd\u7559 \u865a\u57fa\u7c7b \u865a\u6790\u6784\u51fd\u6570 \u865a\u51fd\u6570 \u865a\u8868 void \u6307\u9488 \u91ce\u6307\u9488 \u5b57\u957f 0\u521d\u59cb\u5316 None","title":"\u8bcd\u6c47\u8868"},{"location":"tags/#syntax_1","text":"\u8bed\u6cd5","title":"\u8bed\u6cd5(syntax)"},{"location":"tags/#out-of-scope","text":"\u8d85\u51fa\u4f5c\u7528\u57df","title":"\u8d85\u51fa\u4f5c\u7528\u57df(out of scope)"},{"location":"tags/#_11","text":"\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316","title":"\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316"},{"location":"template-class-specialization/","tags":["\u8bcd\u6c47\u8868","template class specialization"],"text":"","title":"\u7c7b\u6a21\u677f\u7279\u5316"},{"location":"template-type-parameters/","tags":["\u8bcd\u6c47\u8868","\u6a21\u677f\u7c7b\u578b\u53c2\u6570"],"text":"","title":"\u6a21\u677f\u7c7b\u578b\u53c2\u6570"},{"location":"the-diamond-problem/","tags":["\u8bcd\u6c47\u8868","the-diamond-problem"],"text":"","title":"\u83f1\u5f62\u7ee7\u627f\u95ee\u9898"},{"location":"transitive%20includes/","text":"title: \u95f4\u63a5\u5305\u542b alias: \u95f4\u63a5\u5305\u542b english:transitive includes type: glossary tags: - glossary - transitive includes","title":"Transitive includes"},{"location":"translation/","tags":["\u8bcd\u6c47\u8868","translation"],"text":"\u7ffb\u8bd1","title":"\u7ffb\u8bd1"},{"location":"trivial-conversion/","tags":["\u8bcd\u6c47\u8868","\u7b80\u5355\u8f6c\u6362(trivial conversion)"],"text":"","title":"\u7b80\u5355\u8f6c\u6362"},{"location":"type%20deduction/","tags":["\u8bcd\u6c47\u8868","type deduction"],"text":"","title":"\u7c7b\u578b\u63a8\u65ad"},{"location":"type-aliases/","tags":["\u8bcd\u6c47\u8868","type-aliases"],"text":"using distance_t = double; // distance_t \u4f5c\u4e3a double \u7684\u522b\u540d \u53ef\u7528\u4e8e\u7b80\u5316 \u51fd\u6570\u6307\u9488 \u8bed\u6cd5","title":"\u7c7b\u578b\u522b\u540d"},{"location":"type-conversion/","tags":["\u8bcd\u6c47\u8868","type-conversion"],"text":"","title":"\u7c7b\u578b\u8f6c\u6362"},{"location":"type-definition/","tags":["\u8bcd\u6c47\u8868","type-definition"],"text":"","title":"\u7c7b\u578b\u5b9a\u4e49"},{"location":"type-inference/","tags":["\u8bcd\u6c47\u8868","type-inference"],"text":"","title":"\u7c7b\u578b\u63a8\u65ad"},{"location":"type-safe/","tags":["\u8bcd\u6c47\u8868","type-safe"],"text":"","title":"\u7c7b\u578b\u5b89\u5168"},{"location":"type-signature/","tags":["\u8bcd\u6c47\u8868","type-signature"],"text":"","title":"\u7c7b\u578b\u7b7e\u540d"},{"location":"typeid/","tags":["\u8bcd\u6c47\u8868","typeid"],"text":"\u5728g++\u4e0a\uff0c\u8f93\u51fa\u7684\u7ed3\u679c\u4e3a \u4fee\u9970\u540d \uff0c\u53ef\u4ee5\u4f7f\u7528 1 a.out | c++filt --types \u5f97\u5230\u53ef\u8bfb\u7684\u7ed3\u679c\u3002 \u53c2\u8003","title":"typeid \u8fd0\u7b97\u7b26"},{"location":"undefined-behavior/","tags":["\u8bcd\u6c47\u8868","undefined-behavior"],"text":"","title":"\u672a\u5b9a\u4e49\u884c\u4e3a"},{"location":"uniform-initialization/","tags":["\u8bcd\u6c47\u8868","uniform initialization"],"text":"","title":"\u7edf\u4e00\u521d\u59cb\u5316"},{"location":"unnamed-namespace/","tags":["\u8bcd\u6c47\u8868","unnamed-namespace"],"text":"","title":"\u533f\u540d\u547d\u540d\u7a7a\u95f4"},{"location":"unscoped-enumerations/","tags":["\u8bcd\u6c47\u8868","unscoped-enumerations"],"text":"","title":"\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b"},{"location":"upcasting/","tags":["\u8bcd\u6c47\u8868","upcasting"],"text":"","title":"\u5411\u4e0a\u8f6c\u6362"},{"location":"using-declaration/","tags":["\u8bcd\u6c47\u8868","using-declaration"],"text":"","title":"using \u58f0\u660e"},{"location":"using-directive/","tags":["\u8bcd\u6c47\u8868","using-directive"],"text":"","title":"using \u6307\u4ee4"},{"location":"value-category/","tags":["\u8bcd\u6c47\u8868","value-category"],"text":"","title":"\u503c\u7c7b\u578b"},{"location":"value-container/","tags":["\u8bcd\u6c47\u8868","value-container"],"text":"","title":"\u503c\u5bb9\u5668"},{"location":"value-initialization/","tags":["\u8bcd\u6c47\u8868","value-initialization"],"text":"\u503c\u521d\u59cb\u5316 \uff1a\u901a\u8fc7\u7a7a\u521d\u59cb\u5316\u503c\u6784\u9020\u5bf9\u8c61","title":"\u503c\u521d\u59cb\u5316"},{"location":"value-preserving/","tags":["\u8bcd\u6c47\u8868","value-preserving"],"text":"","title":"\u503c\u4fdd\u7559"},{"location":"virtual-base-class/","tags":["\u8bcd\u6c47\u8868","virtual-base-class"],"text":"","title":"\u865a\u57fa\u7c7b"},{"location":"virtual-destructor/","tags":["\u8bcd\u6c47\u8868","virtual-destructor"],"text":"","title":"\u865a\u6790\u6784\u51fd\u6570"},{"location":"virtual-function/","tags":["\u8bcd\u6c47\u8868","virtual-function"],"text":"","title":"\u865a\u51fd\u6570"},{"location":"virtual-table/","tags":["\u8bcd\u6c47\u8868","virtual-table"],"text":"","title":"\u865a\u8868"},{"location":"void%20pointer/","tags":["\u8bcd\u6c47\u8868","void pointer"],"text":"","title":"void \u6307\u9488"},{"location":"wild-pointer/","tags":["\u8bcd\u6c47\u8868","wild-pointer"],"text":"\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u7684\u6307\u9488","title":"\u91ce\u6307\u9488"},{"location":"word/","tags":["\u8bcd\u6c47\u8868","word"],"text":"","title":"\u5b57\u957f"},{"location":"zero-initialization/","tags":["\u8bcd\u6c47\u8868","zero-initialization"],"text":"","title":"0\u521d\u59cb\u5316"},{"location":"%E6%8B%AC%E5%8F%B7%E5%88%9D%E5%A7%8B%E5%8C%96/","tags":["\u8bcd\u6c47\u8868","\u62ec\u53f7\u521d\u59cb\u5316"],"text":"","title":null},{"location":"%E7%BF%BB%E8%AF%91%E7%BA%A6%E5%AE%9A/","text":"\u82f1\u6587\u539f\u6587\u4f7f\u7528 variable \u6307\u4ee3\u4e86\u53d8\u91cf\u3001\u51fd\u6570\u3001\u5bf9\u8c61\u7b49\u591a\u79cd\u7c7b\u578b\uff0c\u5e76\u4e0d\u4ec5\u9650\u4e8e\u201d\u53d8\u91cf\u201c\uff0c\u5728\u7ffb\u8bd1\u4e2d\u6211\u4eec\u9075\u5faa\u539f\u6587\u8fd9\u4e00\u7ea6\u5b9a {} \u7ffb\u8bd1\u4e3a\u82b1\u62ec\u53f7 \u6bcf\u8282\u8bfe\u540e\u7684\u5c0f\u6d4b\u8bd5\u6682\u65f6\u4e0d\u7ffb\u8bd1\uff0c\u5168\u90e8\u5b8c\u7ed3\u540e\u7edf\u4e00\u7ffb\u8bd1\u5e76\u89e3\u6790 \u5c3d\u7ba1\u81ea\u7136\u8bed\u8a00\u4e2d\u7684\u53e5\u6cd5\u548c\u8bed\u6cd5\u662f\u4e0d\u540c\u7684\uff0c\u5728\u7ffb\u8bd1\u8ba1\u7b97\u673a\u6587\u6863\u65f6\uff0c\u53e5\u6cd5(syntax)\u901a\u5e38\u4e5f\u88ab\u8bd1\u4e3a\u8bed\u6cd5\uff0c\u672c\u9879\u76ee\u4e5f\u91c7\u53d6\u8be5\u505a\u6cd5\u3002 operator \u4e00\u822c\u7ffb\u8bd1\u4e3a\u64cd\u4f5c\u7b26\u6216\u8fd0\u7b97\u7b26\uff0c\u672c\u6587\u7edf\u4e00\u7ffb\u8bd1\u4e3a\u8fd0\u7b97\u7b26\u3002 specifiers \u7ffb\u8bd1\u4e3a\u4fee\u9970\u7b26 Callout 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 !!! success \"\u6700\u4f73\u5b9e\u8df5\" <> !!! tldr \"\u5173\u952e\u4fe1\u606f\" <> !!! info \"\u4f5c\u8005\u6ce8\" <> !!! cite \"\u9898\u5916\u8bdd\" <\u5185\u5bb9> !!! info \"\u6269\u5c55\u9605\u8bfb\" <\u5185\u5bb9> !!! note \"\u6cd5\u5219\" <> !!! question \"Q & A\" !!! tip \"\u5c0f\u8d34\u58eb\" <> !!! warning \"\u6ce8\u610f\" <>","title":"\u7ffb\u8bd1\u7ea6\u5b9a"},{"location":"%E7%BF%BB%E8%AF%91%E7%BA%A6%E5%AE%9A/#callout","text":"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 !!! success \"\u6700\u4f73\u5b9e\u8df5\" <> !!! tldr \"\u5173\u952e\u4fe1\u606f\" <> !!! info \"\u4f5c\u8005\u6ce8\" <> !!! cite \"\u9898\u5916\u8bdd\" <\u5185\u5bb9> !!! info \"\u6269\u5c55\u9605\u8bfb\" <\u5185\u5bb9> !!! note \"\u6cd5\u5219\" <> !!! question \"Q & A\" !!! tip \"\u5c0f\u8d34\u58eb\" <> !!! warning \"\u6ce8\u610f\" <>","title":"Callout"},{"location":"images/onepage/Drawing%202023-02-11%2013.33.50.excalidraw/","tags":["excalidraw"],"text":"\u26a0 Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. \u26a0 Text Elements \u4e00\u7ea7\u6807\u9898 ^2Qkzc43x Header2 L ^NyuMC5Oo \u4e8c\u7ea7\u6807\u9898 ^1THwLYGb Header3 M ^RqaR2vTw \u4e09\u7ea7\u6807\u9898 ^3NfRm2iw Text S ^oNO98VDR \u6b63\u6587 ^lg2Qrkc8 Chrome\u7f29\u653e45%\u6253\u5370 ^w9XRlZgg %% Drawing 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 { \"type\" : \"excalidraw\" , \"version\" : 2 , \"source\" : \"https://excalidraw.com\" , \"elements\" : [ { \"type\" : \"text\" , \"version\" : 555 , \"versionNonce\" : 662979008 , \"isDeleted\" : false , \"id\" : \"2Qkzc43x\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1530.2689639876626 , \"y\" : 1040.4671415627722 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 155 , \"height\" : 50 , \"seed\" : 2122546293 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636025 , \"link\" : null , \"locked\" : false , \"fontSize\" : 36 , \"fontFamily\" : 2 , \"text\" : \"\u4e00\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e00\u7ea7\u6807\u9898 \" , \"baseline\" : 38 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e00\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 532 , \"versionNonce\" : 858372672 , \"isDeleted\" : false , \"id\" : \"NyuMC5Oo\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1535.3314377403399 , \"y\" : 1112.760604189277 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 139 , \"height\" : 32 , \"seed\" : 987263323 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636025 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 2 , \"text\" : \"Header2 L \" , \"rawText\" : \"Header2 L \" , \"baseline\" : 26 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Header2 L \" }, { \"type\" : \"text\" , \"version\" : 532 , \"versionNonce\" : 957745600 , \"isDeleted\" : false , \"id\" : \"1THwLYGb\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1535.3314377403399 , \"y\" : 1148.760604189278 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 121 , \"height\" : 39 , \"seed\" : 1378231765 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 2 , \"text\" : \"\u4e8c\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e8c\u7ea7\u6807\u9898 \" , \"baseline\" : 30 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e8c\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 550 , \"versionNonce\" : 309883456 , \"isDeleted\" : false , \"id\" : \"RqaR2vTw\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1540.1305209719483 , \"y\" : 1231.8259114810737 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 100 , \"height\" : 23 , \"seed\" : 1729596923 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 20 , \"fontFamily\" : 2 , \"text\" : \"Header3 M\" , \"rawText\" : \"Header3 M\" , \"baseline\" : 19 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Header3 M\" }, { \"type\" : \"text\" , \"version\" : 550 , \"versionNonce\" : 1160696256 , \"isDeleted\" : false , \"id\" : \"3NfRm2iw\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1540.1305209719483 , \"y\" : 1257.3259114810737 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 87 , \"height\" : 28 , \"seed\" : 1651286837 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 20 , \"fontFamily\" : 2 , \"text\" : \"\u4e09\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e09\u7ea7\u6807\u9898 \" , \"baseline\" : 21 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e09\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 554 , \"versionNonce\" : 120597056 , \"isDeleted\" : false , \"id\" : \"oNO98VDR\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1543.4508169287008 , \"y\" : 1333.351421558052 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 46 , \"height\" : 19 , \"seed\" : 1116346011 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"text\" : \"Text S\" , \"rawText\" : \"Text S\" , \"baseline\" : 15 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Text S\" }, { \"type\" : \"text\" , \"version\" : 582 , \"versionNonce\" : 1659804096 , \"isDeleted\" : false , \"id\" : \"lg2Qrkc8\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1543.4508169287008 , \"y\" : 1356.9284304866233 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 33 , \"height\" : 23 , \"seed\" : 331117717 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"text\" : \"\u6b63\u6587\" , \"rawText\" : \"\u6b63\u6587\" , \"baseline\" : 17 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u6b63\u6587\" }, { \"type\" : \"rectangle\" , \"version\" : 143 , \"versionNonce\" : 2083578432 , \"isDeleted\" : false , \"id\" : \"vefkyDhSzZRGpVKbqbz-A\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1501.1026384102925 , \"y\" : 1018.4363377836585 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 1754 , \"height\" : 1240 , \"seed\" : 457283003 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false }, { \"type\" : \"text\" , \"version\" : 172 , \"versionNonce\" : 1165320640 , \"isDeleted\" : false , \"id\" : \"w9XRlZgg\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2969.3187842436255 , \"y\" : 2200.7865982003254 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"#fab005\" , \"width\" : 254 , \"height\" : 36 , \"seed\" : 1635294037 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"rawText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"baseline\" : 29 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" }, { \"id\" : \"qiMkuWD7\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1204.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 2107232832 , \"version\" : 2 , \"versionNonce\" : 5937728 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" }, { \"id\" : \"D2qyHaD2\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1233.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 525562304 , \"version\" : 2 , \"versionNonce\" : 1004743104 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"RzU4paPb\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1262.6650309654767 , \"width\" : 107 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1193642560 , \"version\" : 2 , \"versionNonce\" : 1183666752 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"text\" : \"| Process 1 |\" , \"rawText\" : \"| Process 1 |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| Process 1 |\" }, { \"id\" : \"ned75VoD\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1291.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1333737920 , \"version\" : 2 , \"versionNonce\" : 827847104 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"bSOpMKrI\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1320.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1450852928 , \"version\" : 2 , \"versionNonce\" : 631835200 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" }, { \"id\" : \"1GngddyL\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1349.6650309654767 , \"width\" : 58 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 384958912 , \"version\" : 2 , \"versionNonce\" : 754170304 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"Fgrq3YbS\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1378.6650309654767 , \"width\" : 58 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 175635008 , \"version\" : 2 , \"versionNonce\" : 411943488 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"DyanYWrG\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1407.6650309654767 , \"width\" : 66 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1959512512 , \"version\" : 2 , \"versionNonce\" : 1373012416 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"v v\" , \"rawText\" : \"v v\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"v v\" }, { \"id\" : \"BKIPuNmW\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1436.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1711190592 , \"version\" : 2 , \"versionNonce\" : 1041988160 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" }, { \"id\" : \"5QSZqMpi\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1465.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 70029760 , \"version\" : 2 , \"versionNonce\" : 1595951552 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"vhjm4Sfg\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1494.6650309654767 , \"width\" : 107 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 267283008 , \"version\" : 2 , \"versionNonce\" : 1496461888 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| Process 2 |\" , \"rawText\" : \"| Process 2 |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| Process 2 |\" }, { \"id\" : \"jPGVeFjH\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1523.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 2083805632 , \"version\" : 2 , \"versionNonce\" : 804327872 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"lPqCpAQM\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1552.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1395073600 , \"version\" : 2 , \"versionNonce\" : 1353837120 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" }, { \"id\" : \"KXMgPLu1\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1581.6650309654767 , \"width\" : 58 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 805511616 , \"version\" : 2 , \"versionNonce\" : 996727232 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"0gm4X5Po\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1610.6650309654767 , \"width\" : 58 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 512285248 , \"version\" : 2 , \"versionNonce\" : 796566080 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"dZiNiEbb\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1639.6650309654767 , \"width\" : 66 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 246999488 , \"version\" : 2 , \"versionNonce\" : 346530240 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"v v\" , \"rawText\" : \"v v\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"v v\" }, { \"id\" : \"alDnoWo9\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1668.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 83071552 , \"version\" : 2 , \"versionNonce\" : 611080768 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" }, { \"id\" : \"RfAk4D6J\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1697.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 594915776 , \"version\" : 2 , \"versionNonce\" : 1791846848 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"rttDgSOC\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1726.6650309654767 , \"width\" : 107 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1028082240 , \"version\" : 2 , \"versionNonce\" : 40309312 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| Process n |\" , \"rawText\" : \"| Process n |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| Process n |\" }, { \"id\" : \"StAWfpEW\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1755.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 358185408 , \"version\" : 2 , \"versionNonce\" : 150614464 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"N7lr98Ho\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1784.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 576979520 , \"version\" : 2 , \"versionNonce\" : 1078643264 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" } ], \"appState\" : { \"theme\" : \"light\" , \"viewBackgroundColor\" : \"white\" , \"currentItemStrokeColor\" : \"#000000\" , \"currentItemBackgroundColor\" : \"transparent\" , \"currentItemFillStyle\" : \"solid\" , \"currentItemStrokeWidth\" : 4 , \"currentItemStrokeStyle\" : \"solid\" , \"currentItemRoughness\" : 1 , \"currentItemOpacity\" : 100 , \"currentItemFontFamily\" : 2 , \"currentItemFontSize\" : 16 , \"currentItemTextAlign\" : \"left\" , \"currentItemStartArrowhead\" : null , \"currentItemEndArrowhead\" : \"arrow\" , \"scrollX\" : -1190.8299111375652 , \"scrollY\" : -943.1167355109312 , \"zoom\" : { \"value\" : 0.55 }, \"currentItemRoundness\" : \"round\" , \"gridSize\" : null , \"colorPalette\" : { \"elementStroke\" : [ \"#000000\" , \"#343a40\" , \"#495057\" , \"#c92a2a\" , \"#a61e4d\" , \"#862e9c\" , \"#5f3dc4\" , \"#364fc7\" , \"#1864ab\" , \"#0b7285\" , \"#087f5b\" , \"#2b8a3e\" , \"#5c940d\" , \"#e67700\" , \"#d9480f\" ], \"elementBackground\" : [ \"transparent\" , \"#ffffff\" , \"#F8F9FA\" , \"#EDEDE9\" , \"#ADB5BD\" , \"#8cb369\" , \"#f4e285\" , \"#f4a259\" , \"#5b8e7d\" , \"#bc4b51\" , \"#05668d\" , \"#028090\" , \"#00a896\" , \"#02c39a\" , \"#f0f3bd\" , \"#c9e4ca\" , \"#87bba2\" , \"#55828b\" , \"#3b6064\" , \"#FEE440\" , \"#ced4da\" , \"#868e96\" , \"#fa5252\" , \"#e64980\" , \"#be4bdb\" , \"#7950f2\" , \"#4c6ef5\" , \"#228be6\" , \"#15aabf\" , \"#12b886\" , \"#40c057\" , \"#82c91e\" , \"#fab005\" , \"#fd7e14\" , \"#A30000\" ], \"canvasBackground\" : [ \"#ffffff\" , \"#f8f9fa\" , \"#f1f3f5\" , \"#fff5f5\" , \"#fff0f6\" , \"#f8f0fc\" , \"#f3f0ff\" , \"#edf2ff\" , \"#e7f5ff\" , \"#e3fafc\" , \"#e6fcf5\" , \"#ebfbee\" , \"#f4fce3\" , \"#fff9db\" , \"#fff4e6\" ] }, \"currentStrokeOptions\" : null , \"previousGridSize\" : null }, \"files\" : {} } %%","title":"Drawing 2023 02 11 13.33.50.excalidraw"},{"location":"images/onepage/Drawing%202023-02-11%2013.33.50.excalidraw/#text-elements","text":"\u4e00\u7ea7\u6807\u9898 ^2Qkzc43x Header2 L ^NyuMC5Oo \u4e8c\u7ea7\u6807\u9898 ^1THwLYGb Header3 M ^RqaR2vTw \u4e09\u7ea7\u6807\u9898 ^3NfRm2iw Text S ^oNO98VDR \u6b63\u6587 ^lg2Qrkc8 Chrome\u7f29\u653e45%\u6253\u5370 ^w9XRlZgg %%","title":"Text Elements"},{"location":"images/onepage/Drawing%202023-02-11%2013.33.50.excalidraw/#drawing","text":"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 { \"type\" : \"excalidraw\" , \"version\" : 2 , \"source\" : \"https://excalidraw.com\" , \"elements\" : [ { \"type\" : \"text\" , \"version\" : 555 , \"versionNonce\" : 662979008 , \"isDeleted\" : false , \"id\" : \"2Qkzc43x\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1530.2689639876626 , \"y\" : 1040.4671415627722 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 155 , \"height\" : 50 , \"seed\" : 2122546293 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636025 , \"link\" : null , \"locked\" : false , \"fontSize\" : 36 , \"fontFamily\" : 2 , \"text\" : \"\u4e00\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e00\u7ea7\u6807\u9898 \" , \"baseline\" : 38 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e00\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 532 , \"versionNonce\" : 858372672 , \"isDeleted\" : false , \"id\" : \"NyuMC5Oo\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1535.3314377403399 , \"y\" : 1112.760604189277 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 139 , \"height\" : 32 , \"seed\" : 987263323 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636025 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 2 , \"text\" : \"Header2 L \" , \"rawText\" : \"Header2 L \" , \"baseline\" : 26 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Header2 L \" }, { \"type\" : \"text\" , \"version\" : 532 , \"versionNonce\" : 957745600 , \"isDeleted\" : false , \"id\" : \"1THwLYGb\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1535.3314377403399 , \"y\" : 1148.760604189278 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 121 , \"height\" : 39 , \"seed\" : 1378231765 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 2 , \"text\" : \"\u4e8c\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e8c\u7ea7\u6807\u9898 \" , \"baseline\" : 30 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e8c\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 550 , \"versionNonce\" : 309883456 , \"isDeleted\" : false , \"id\" : \"RqaR2vTw\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1540.1305209719483 , \"y\" : 1231.8259114810737 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 100 , \"height\" : 23 , \"seed\" : 1729596923 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 20 , \"fontFamily\" : 2 , \"text\" : \"Header3 M\" , \"rawText\" : \"Header3 M\" , \"baseline\" : 19 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Header3 M\" }, { \"type\" : \"text\" , \"version\" : 550 , \"versionNonce\" : 1160696256 , \"isDeleted\" : false , \"id\" : \"3NfRm2iw\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1540.1305209719483 , \"y\" : 1257.3259114810737 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 87 , \"height\" : 28 , \"seed\" : 1651286837 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 20 , \"fontFamily\" : 2 , \"text\" : \"\u4e09\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e09\u7ea7\u6807\u9898 \" , \"baseline\" : 21 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e09\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 554 , \"versionNonce\" : 120597056 , \"isDeleted\" : false , \"id\" : \"oNO98VDR\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1543.4508169287008 , \"y\" : 1333.351421558052 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 46 , \"height\" : 19 , \"seed\" : 1116346011 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"text\" : \"Text S\" , \"rawText\" : \"Text S\" , \"baseline\" : 15 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Text S\" }, { \"type\" : \"text\" , \"version\" : 582 , \"versionNonce\" : 1659804096 , \"isDeleted\" : false , \"id\" : \"lg2Qrkc8\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1543.4508169287008 , \"y\" : 1356.9284304866233 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 33 , \"height\" : 23 , \"seed\" : 331117717 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"text\" : \"\u6b63\u6587\" , \"rawText\" : \"\u6b63\u6587\" , \"baseline\" : 17 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u6b63\u6587\" }, { \"type\" : \"rectangle\" , \"version\" : 143 , \"versionNonce\" : 2083578432 , \"isDeleted\" : false , \"id\" : \"vefkyDhSzZRGpVKbqbz-A\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1501.1026384102925 , \"y\" : 1018.4363377836585 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 1754 , \"height\" : 1240 , \"seed\" : 457283003 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false }, { \"type\" : \"text\" , \"version\" : 172 , \"versionNonce\" : 1165320640 , \"isDeleted\" : false , \"id\" : \"w9XRlZgg\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2969.3187842436255 , \"y\" : 2200.7865982003254 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"#fab005\" , \"width\" : 254 , \"height\" : 36 , \"seed\" : 1635294037 , \"groupIds\" : [], \"roundness\" : null , \"boundElements\" : [], \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"rawText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"baseline\" : 29 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" }, { \"id\" : \"qiMkuWD7\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1204.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 2107232832 , \"version\" : 2 , \"versionNonce\" : 5937728 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" }, { \"id\" : \"D2qyHaD2\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1233.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 525562304 , \"version\" : 2 , \"versionNonce\" : 1004743104 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"RzU4paPb\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1262.6650309654767 , \"width\" : 107 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1193642560 , \"version\" : 2 , \"versionNonce\" : 1183666752 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"text\" : \"| Process 1 |\" , \"rawText\" : \"| Process 1 |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| Process 1 |\" }, { \"id\" : \"ned75VoD\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1291.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1333737920 , \"version\" : 2 , \"versionNonce\" : 827847104 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"bSOpMKrI\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1320.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1450852928 , \"version\" : 2 , \"versionNonce\" : 631835200 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636026 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" }, { \"id\" : \"1GngddyL\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1349.6650309654767 , \"width\" : 58 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 384958912 , \"version\" : 2 , \"versionNonce\" : 754170304 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"Fgrq3YbS\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1378.6650309654767 , \"width\" : 58 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 175635008 , \"version\" : 2 , \"versionNonce\" : 411943488 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"DyanYWrG\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1407.6650309654767 , \"width\" : 66 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1959512512 , \"version\" : 2 , \"versionNonce\" : 1373012416 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"v v\" , \"rawText\" : \"v v\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"v v\" }, { \"id\" : \"BKIPuNmW\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1436.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1711190592 , \"version\" : 2 , \"versionNonce\" : 1041988160 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" }, { \"id\" : \"5QSZqMpi\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1465.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 70029760 , \"version\" : 2 , \"versionNonce\" : 1595951552 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"vhjm4Sfg\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1494.6650309654767 , \"width\" : 107 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 267283008 , \"version\" : 2 , \"versionNonce\" : 1496461888 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| Process 2 |\" , \"rawText\" : \"| Process 2 |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| Process 2 |\" }, { \"id\" : \"jPGVeFjH\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1523.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 2083805632 , \"version\" : 2 , \"versionNonce\" : 804327872 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"lPqCpAQM\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1552.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1395073600 , \"version\" : 2 , \"versionNonce\" : 1353837120 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" }, { \"id\" : \"KXMgPLu1\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1581.6650309654767 , \"width\" : 58 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 805511616 , \"version\" : 2 , \"versionNonce\" : 996727232 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"0gm4X5Po\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1610.6650309654767 , \"width\" : 58 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 512285248 , \"version\" : 2 , \"versionNonce\" : 796566080 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"dZiNiEbb\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1639.6650309654767 , \"width\" : 66 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 246999488 , \"version\" : 2 , \"versionNonce\" : 346530240 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"v v\" , \"rawText\" : \"v v\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"v v\" }, { \"id\" : \"alDnoWo9\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1668.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 83071552 , \"version\" : 2 , \"versionNonce\" : 611080768 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" }, { \"id\" : \"RfAk4D6J\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1697.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 594915776 , \"version\" : 2 , \"versionNonce\" : 1791846848 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"rttDgSOC\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1726.6650309654767 , \"width\" : 107 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 1028082240 , \"version\" : 2 , \"versionNonce\" : 40309312 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| Process n |\" , \"rawText\" : \"| Process n |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| Process n |\" }, { \"id\" : \"StAWfpEW\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1755.6650309654767 , \"width\" : 76 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 358185408 , \"version\" : 2 , \"versionNonce\" : 150614464 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"| |\" , \"rawText\" : \"| |\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"| |\" }, { \"id\" : \"N7lr98Ho\" , \"type\" : \"text\" , \"x\" : 2083.557183864838 , \"y\" : 1784.6650309654767 , \"width\" : 100 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"roundness\" : null , \"seed\" : 576979520 , \"version\" : 2 , \"versionNonce\" : 1078643264 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1676093636027 , \"link\" : null , \"locked\" : false , \"text\" : \"+---------------+\" , \"rawText\" : \"+---------------+\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"+---------------+\" } ], \"appState\" : { \"theme\" : \"light\" , \"viewBackgroundColor\" : \"white\" , \"currentItemStrokeColor\" : \"#000000\" , \"currentItemBackgroundColor\" : \"transparent\" , \"currentItemFillStyle\" : \"solid\" , \"currentItemStrokeWidth\" : 4 , \"currentItemStrokeStyle\" : \"solid\" , \"currentItemRoughness\" : 1 , \"currentItemOpacity\" : 100 , \"currentItemFontFamily\" : 2 , \"currentItemFontSize\" : 16 , \"currentItemTextAlign\" : \"left\" , \"currentItemStartArrowhead\" : null , \"currentItemEndArrowhead\" : \"arrow\" , \"scrollX\" : -1190.8299111375652 , \"scrollY\" : -943.1167355109312 , \"zoom\" : { \"value\" : 0.55 }, \"currentItemRoundness\" : \"round\" , \"gridSize\" : null , \"colorPalette\" : { \"elementStroke\" : [ \"#000000\" , \"#343a40\" , \"#495057\" , \"#c92a2a\" , \"#a61e4d\" , \"#862e9c\" , \"#5f3dc4\" , \"#364fc7\" , \"#1864ab\" , \"#0b7285\" , \"#087f5b\" , \"#2b8a3e\" , \"#5c940d\" , \"#e67700\" , \"#d9480f\" ], \"elementBackground\" : [ \"transparent\" , \"#ffffff\" , \"#F8F9FA\" , \"#EDEDE9\" , \"#ADB5BD\" , \"#8cb369\" , \"#f4e285\" , \"#f4a259\" , \"#5b8e7d\" , \"#bc4b51\" , \"#05668d\" , \"#028090\" , \"#00a896\" , \"#02c39a\" , \"#f0f3bd\" , \"#c9e4ca\" , \"#87bba2\" , \"#55828b\" , \"#3b6064\" , \"#FEE440\" , \"#ced4da\" , \"#868e96\" , \"#fa5252\" , \"#e64980\" , \"#be4bdb\" , \"#7950f2\" , \"#4c6ef5\" , \"#228be6\" , \"#15aabf\" , \"#12b886\" , \"#40c057\" , \"#82c91e\" , \"#fab005\" , \"#fd7e14\" , \"#A30000\" ], \"canvasBackground\" : [ \"#ffffff\" , \"#f8f9fa\" , \"#f1f3f5\" , \"#fff5f5\" , \"#fff0f6\" , \"#f8f0fc\" , \"#f3f0ff\" , \"#edf2ff\" , \"#e7f5ff\" , \"#e3fafc\" , \"#e6fcf5\" , \"#ebfbee\" , \"#f4fce3\" , \"#fff9db\" , \"#fff4e6\" ] }, \"currentStrokeOptions\" : null , \"previousGridSize\" : null }, \"files\" : {} } %%","title":"Drawing"},{"location":"images/onepage/Template.excalidraw/","tags":["excalidraw"],"text":"\u26a0 Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. \u26a0 Text Elements \u4e00\u7ea7\u6807\u9898 ^2Qkzc43x Header2 L ^NyuMC5Oo \u4e8c\u7ea7\u6807\u9898 ^1THwLYGb Header3 M ^RqaR2vTw \u4e09\u7ea7\u6807\u9898 ^3NfRm2iw Text S ^oNO98VDR \u6b63\u6587 ^lg2Qrkc8 Chrome\u7f29\u653e45%\u6253\u5370 ^w9XRlZgg %% Drawing 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 { \"type\" : \"excalidraw\" , \"version\" : 2 , \"source\" : \"https://excalidraw.com\" , \"elements\" : [ { \"type\" : \"text\" , \"version\" : 554 , \"versionNonce\" : 1059646139 , \"isDeleted\" : false , \"id\" : \"2Qkzc43x\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1530.2689639876626 , \"y\" : 1040.4671415627722 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 156 , \"height\" : 50 , \"seed\" : 2122546293 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103436870 , \"link\" : null , \"locked\" : false , \"fontSize\" : 36 , \"fontFamily\" : 2 , \"text\" : \"\u4e00\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e00\u7ea7\u6807\u9898 \" , \"baseline\" : 38 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e00\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 531 , \"versionNonce\" : 482119867 , \"isDeleted\" : false , \"id\" : \"NyuMC5Oo\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1535.3314377403399 , \"y\" : 1112.760604189277 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 140 , \"height\" : 32 , \"seed\" : 987263323 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103446136 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 2 , \"text\" : \"Header2 L \" , \"rawText\" : \"Header2 L \" , \"baseline\" : 26 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Header2 L \" }, { \"type\" : \"text\" , \"version\" : 531 , \"versionNonce\" : 127806581 , \"isDeleted\" : false , \"id\" : \"1THwLYGb\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1535.3314377403399 , \"y\" : 1148.760604189278 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 122 , \"height\" : 39 , \"seed\" : 1378231765 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103446136 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 2 , \"text\" : \"\u4e8c\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e8c\u7ea7\u6807\u9898 \" , \"baseline\" : 30 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e8c\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 549 , \"versionNonce\" : 1778434395 , \"isDeleted\" : false , \"id\" : \"RqaR2vTw\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1540.1305209719483 , \"y\" : 1231.8259114810737 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 101 , \"height\" : 23 , \"seed\" : 1729596923 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103446136 , \"link\" : null , \"locked\" : false , \"fontSize\" : 20 , \"fontFamily\" : 2 , \"text\" : \"Header3 M\" , \"rawText\" : \"Header3 M\" , \"baseline\" : 19 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Header3 M\" }, { \"type\" : \"text\" , \"version\" : 549 , \"versionNonce\" : 1147852245 , \"isDeleted\" : false , \"id\" : \"3NfRm2iw\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1540.1305209719483 , \"y\" : 1257.3259114810737 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 88 , \"height\" : 28 , \"seed\" : 1651286837 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103446136 , \"link\" : null , \"locked\" : false , \"fontSize\" : 20 , \"fontFamily\" : 2 , \"text\" : \"\u4e09\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e09\u7ea7\u6807\u9898 \" , \"baseline\" : 21 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e09\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 553 , \"versionNonce\" : 750116347 , \"isDeleted\" : false , \"id\" : \"oNO98VDR\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1543.4508169287008 , \"y\" : 1333.351421558052 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 47 , \"height\" : 19 , \"seed\" : 1116346011 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103446136 , \"link\" : null , \"locked\" : false , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"text\" : \"Text S\" , \"rawText\" : \"Text S\" , \"baseline\" : 15 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Text S\" }, { \"type\" : \"text\" , \"version\" : 581 , \"versionNonce\" : 1409519547 , \"isDeleted\" : false , \"id\" : \"lg2Qrkc8\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1543.4508169287008 , \"y\" : 1356.9284304866233 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 34 , \"height\" : 23 , \"seed\" : 331117717 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103455324 , \"link\" : null , \"locked\" : false , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"text\" : \"\u6b63\u6587\" , \"rawText\" : \"\u6b63\u6587\" , \"baseline\" : 17 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u6b63\u6587\" }, { \"type\" : \"rectangle\" , \"version\" : 142 , \"versionNonce\" : 227795253 , \"isDeleted\" : false , \"id\" : \"vefkyDhSzZRGpVKbqbz-A\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1501.1026384102925 , \"y\" : 1018.4363377836585 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 1754 , \"height\" : 1240 , \"seed\" : 457283003 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103205233 , \"link\" : null , \"locked\" : false }, { \"type\" : \"text\" , \"version\" : 171 , \"versionNonce\" : 29873979 , \"isDeleted\" : false , \"id\" : \"w9XRlZgg\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2969.3187842436255 , \"y\" : 2200.7865982003254 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"#fab005\" , \"width\" : 268 , \"height\" : 40 , \"seed\" : 1635294037 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103356545 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"rawText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"baseline\" : 30 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" }, { \"id\" : \"9QJQIml8\" , \"type\" : \"text\" , \"x\" : 2645.3753656830195 , \"y\" : 1408.0741218745675 , \"width\" : 7 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"seed\" : 443708244 , \"version\" : 3 , \"versionNonce\" : 1005643988 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1665035496509 , \"link\" : null , \"locked\" : false , \"text\" : \"\" , \"rawText\" : \"\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"\" } ], \"appState\" : { \"theme\" : \"light\" , \"viewBackgroundColor\" : \"white\" , \"currentItemStrokeColor\" : \"#000000\" , \"currentItemBackgroundColor\" : \"transparent\" , \"currentItemFillStyle\" : \"solid\" , \"currentItemStrokeWidth\" : 4 , \"currentItemStrokeStyle\" : \"solid\" , \"currentItemRoughness\" : 1 , \"currentItemOpacity\" : 100 , \"currentItemFontFamily\" : 2 , \"currentItemFontSize\" : 16 , \"currentItemTextAlign\" : \"left\" , \"currentItemStrokeSharpness\" : \"sharp\" , \"currentItemStartArrowhead\" : null , \"currentItemEndArrowhead\" : \"arrow\" , \"currentItemLinearStrokeSharpness\" : \"round\" , \"gridSize\" : null , \"colorPalette\" : { \"elementStroke\" : [ \"#000000\" , \"#343a40\" , \"#495057\" , \"#c92a2a\" , \"#a61e4d\" , \"#862e9c\" , \"#5f3dc4\" , \"#364fc7\" , \"#1864ab\" , \"#0b7285\" , \"#087f5b\" , \"#2b8a3e\" , \"#5c940d\" , \"#e67700\" , \"#d9480f\" ], \"elementBackground\" : [ \"transparent\" , \"#ffffff\" , \"#F8F9FA\" , \"#EDEDE9\" , \"#ADB5BD\" , \"#8cb369\" , \"#f4e285\" , \"#f4a259\" , \"#5b8e7d\" , \"#bc4b51\" , \"#05668d\" , \"#028090\" , \"#00a896\" , \"#02c39a\" , \"#f0f3bd\" , \"#c9e4ca\" , \"#87bba2\" , \"#55828b\" , \"#3b6064\" , \"#FEE440\" , \"#ced4da\" , \"#868e96\" , \"#fa5252\" , \"#e64980\" , \"#be4bdb\" , \"#7950f2\" , \"#4c6ef5\" , \"#228be6\" , \"#15aabf\" , \"#12b886\" , \"#40c057\" , \"#82c91e\" , \"#fab005\" , \"#fd7e14\" , \"#A30000\" ], \"canvasBackground\" : [ \"#ffffff\" , \"#f8f9fa\" , \"#f1f3f5\" , \"#fff5f5\" , \"#fff0f6\" , \"#f8f0fc\" , \"#f3f0ff\" , \"#edf2ff\" , \"#e7f5ff\" , \"#e3fafc\" , \"#e6fcf5\" , \"#ebfbee\" , \"#f4fce3\" , \"#fff9db\" , \"#fff4e6\" ] } }, \"files\" : {} } %%","title":"Template.excalidraw"},{"location":"images/onepage/Template.excalidraw/#text-elements","text":"\u4e00\u7ea7\u6807\u9898 ^2Qkzc43x Header2 L ^NyuMC5Oo \u4e8c\u7ea7\u6807\u9898 ^1THwLYGb Header3 M ^RqaR2vTw \u4e09\u7ea7\u6807\u9898 ^3NfRm2iw Text S ^oNO98VDR \u6b63\u6587 ^lg2Qrkc8 Chrome\u7f29\u653e45%\u6253\u5370 ^w9XRlZgg %%","title":"Text Elements"},{"location":"images/onepage/Template.excalidraw/#drawing","text":"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 { \"type\" : \"excalidraw\" , \"version\" : 2 , \"source\" : \"https://excalidraw.com\" , \"elements\" : [ { \"type\" : \"text\" , \"version\" : 554 , \"versionNonce\" : 1059646139 , \"isDeleted\" : false , \"id\" : \"2Qkzc43x\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1530.2689639876626 , \"y\" : 1040.4671415627722 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 156 , \"height\" : 50 , \"seed\" : 2122546293 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103436870 , \"link\" : null , \"locked\" : false , \"fontSize\" : 36 , \"fontFamily\" : 2 , \"text\" : \"\u4e00\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e00\u7ea7\u6807\u9898 \" , \"baseline\" : 38 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e00\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 531 , \"versionNonce\" : 482119867 , \"isDeleted\" : false , \"id\" : \"NyuMC5Oo\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1535.3314377403399 , \"y\" : 1112.760604189277 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 140 , \"height\" : 32 , \"seed\" : 987263323 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103446136 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 2 , \"text\" : \"Header2 L \" , \"rawText\" : \"Header2 L \" , \"baseline\" : 26 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Header2 L \" }, { \"type\" : \"text\" , \"version\" : 531 , \"versionNonce\" : 127806581 , \"isDeleted\" : false , \"id\" : \"1THwLYGb\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1535.3314377403399 , \"y\" : 1148.760604189278 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 122 , \"height\" : 39 , \"seed\" : 1378231765 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103446136 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 2 , \"text\" : \"\u4e8c\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e8c\u7ea7\u6807\u9898 \" , \"baseline\" : 30 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e8c\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 549 , \"versionNonce\" : 1778434395 , \"isDeleted\" : false , \"id\" : \"RqaR2vTw\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1540.1305209719483 , \"y\" : 1231.8259114810737 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 101 , \"height\" : 23 , \"seed\" : 1729596923 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103446136 , \"link\" : null , \"locked\" : false , \"fontSize\" : 20 , \"fontFamily\" : 2 , \"text\" : \"Header3 M\" , \"rawText\" : \"Header3 M\" , \"baseline\" : 19 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Header3 M\" }, { \"type\" : \"text\" , \"version\" : 549 , \"versionNonce\" : 1147852245 , \"isDeleted\" : false , \"id\" : \"3NfRm2iw\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1540.1305209719483 , \"y\" : 1257.3259114810737 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 88 , \"height\" : 28 , \"seed\" : 1651286837 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103446136 , \"link\" : null , \"locked\" : false , \"fontSize\" : 20 , \"fontFamily\" : 2 , \"text\" : \"\u4e09\u7ea7\u6807\u9898 \" , \"rawText\" : \"\u4e09\u7ea7\u6807\u9898 \" , \"baseline\" : 21 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4e09\u7ea7\u6807\u9898 \" }, { \"type\" : \"text\" , \"version\" : 553 , \"versionNonce\" : 750116347 , \"isDeleted\" : false , \"id\" : \"oNO98VDR\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1543.4508169287008 , \"y\" : 1333.351421558052 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 47 , \"height\" : 19 , \"seed\" : 1116346011 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103446136 , \"link\" : null , \"locked\" : false , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"text\" : \"Text S\" , \"rawText\" : \"Text S\" , \"baseline\" : 15 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Text S\" }, { \"type\" : \"text\" , \"version\" : 581 , \"versionNonce\" : 1409519547 , \"isDeleted\" : false , \"id\" : \"lg2Qrkc8\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1543.4508169287008 , \"y\" : 1356.9284304866233 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 34 , \"height\" : 23 , \"seed\" : 331117717 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103455324 , \"link\" : null , \"locked\" : false , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"text\" : \"\u6b63\u6587\" , \"rawText\" : \"\u6b63\u6587\" , \"baseline\" : 17 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u6b63\u6587\" }, { \"type\" : \"rectangle\" , \"version\" : 142 , \"versionNonce\" : 227795253 , \"isDeleted\" : false , \"id\" : \"vefkyDhSzZRGpVKbqbz-A\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1501.1026384102925 , \"y\" : 1018.4363377836585 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 1754 , \"height\" : 1240 , \"seed\" : 457283003 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103205233 , \"link\" : null , \"locked\" : false }, { \"type\" : \"text\" , \"version\" : 171 , \"versionNonce\" : 29873979 , \"isDeleted\" : false , \"id\" : \"w9XRlZgg\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2969.3187842436255 , \"y\" : 2200.7865982003254 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"#fab005\" , \"width\" : 268 , \"height\" : 40 , \"seed\" : 1635294037 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103356545 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"rawText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"baseline\" : 30 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" }, { \"id\" : \"9QJQIml8\" , \"type\" : \"text\" , \"x\" : 2645.3753656830195 , \"y\" : 1408.0741218745675 , \"width\" : 7 , \"height\" : 19 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"seed\" : 443708244 , \"version\" : 3 , \"versionNonce\" : 1005643988 , \"isDeleted\" : true , \"boundElements\" : null , \"updated\" : 1665035496509 , \"link\" : null , \"locked\" : false , \"text\" : \"\" , \"rawText\" : \"\" , \"fontSize\" : 16 , \"fontFamily\" : 2 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"baseline\" : 15 , \"containerId\" : null , \"originalText\" : \"\" } ], \"appState\" : { \"theme\" : \"light\" , \"viewBackgroundColor\" : \"white\" , \"currentItemStrokeColor\" : \"#000000\" , \"currentItemBackgroundColor\" : \"transparent\" , \"currentItemFillStyle\" : \"solid\" , \"currentItemStrokeWidth\" : 4 , \"currentItemStrokeStyle\" : \"solid\" , \"currentItemRoughness\" : 1 , \"currentItemOpacity\" : 100 , \"currentItemFontFamily\" : 2 , \"currentItemFontSize\" : 16 , \"currentItemTextAlign\" : \"left\" , \"currentItemStrokeSharpness\" : \"sharp\" , \"currentItemStartArrowhead\" : null , \"currentItemEndArrowhead\" : \"arrow\" , \"currentItemLinearStrokeSharpness\" : \"round\" , \"gridSize\" : null , \"colorPalette\" : { \"elementStroke\" : [ \"#000000\" , \"#343a40\" , \"#495057\" , \"#c92a2a\" , \"#a61e4d\" , \"#862e9c\" , \"#5f3dc4\" , \"#364fc7\" , \"#1864ab\" , \"#0b7285\" , \"#087f5b\" , \"#2b8a3e\" , \"#5c940d\" , \"#e67700\" , \"#d9480f\" ], \"elementBackground\" : [ \"transparent\" , \"#ffffff\" , \"#F8F9FA\" , \"#EDEDE9\" , \"#ADB5BD\" , \"#8cb369\" , \"#f4e285\" , \"#f4a259\" , \"#5b8e7d\" , \"#bc4b51\" , \"#05668d\" , \"#028090\" , \"#00a896\" , \"#02c39a\" , \"#f0f3bd\" , \"#c9e4ca\" , \"#87bba2\" , \"#55828b\" , \"#3b6064\" , \"#FEE440\" , \"#ced4da\" , \"#868e96\" , \"#fa5252\" , \"#e64980\" , \"#be4bdb\" , \"#7950f2\" , \"#4c6ef5\" , \"#228be6\" , \"#15aabf\" , \"#12b886\" , \"#40c057\" , \"#82c91e\" , \"#fab005\" , \"#fd7e14\" , \"#A30000\" ], \"canvasBackground\" : [ \"#ffffff\" , \"#f8f9fa\" , \"#f1f3f5\" , \"#fff5f5\" , \"#fff0f6\" , \"#f8f0fc\" , \"#f3f0ff\" , \"#edf2ff\" , \"#e7f5ff\" , \"#e3fafc\" , \"#e6fcf5\" , \"#ebfbee\" , \"#f4fce3\" , \"#fff9db\" , \"#fff4e6\" ] } }, \"files\" : {} } %%","title":"Drawing"},{"location":"images/onepage/c%2B%2B-basic-onepage/","tags":["excalidraw"],"text":"\u26a0 Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. \u26a0 Text Elements C++ \u57fa\u7840 ^lwSrVksx %% Drawing 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 { \"type\" : \"excalidraw\" , \"version\" : 2 , \"source\" : \"https://excalidraw.com\" , \"elements\" : [ { \"id\" : \"ZXOOsHpZx0cLKavt_neEO\" , \"type\" : \"rectangle\" , \"x\" : 1526.7604451938828 , \"y\" : 1087.1960299121963 , \"width\" : 148.51637279132316 , \"height\" : 10.141263341084612 , \"angle\" : 0 , \"strokeColor\" : \"#ffff\" , \"backgroundColor\" : \"#02c39a\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 50 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"seed\" : 850846956 , \"version\" : 462 , \"versionNonce\" : 916899820 , \"isDeleted\" : false , \"boundElements\" : null , \"updated\" : 1665060708977 , \"link\" : null , \"locked\" : false }, { \"type\" : \"rectangle\" , \"version\" : 142 , \"versionNonce\" : 227795253 , \"isDeleted\" : false , \"id\" : \"vefkyDhSzZRGpVKbqbz-A\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1501.1026384102925 , \"y\" : 1018.4363377836585 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 1754 , \"height\" : 1240 , \"seed\" : 457283003 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103205233 , \"link\" : null , \"locked\" : false }, { \"type\" : \"text\" , \"version\" : 869 , \"versionNonce\" : 1653346924 , \"isDeleted\" : false , \"id\" : \"lwSrVksx\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dashed\" , \"roughness\" : 0 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1531.385308864838 , \"y\" : 1056.5940082382049 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 145 , \"height\" : 46 , \"seed\" : 416331988 , \"groupIds\" : [ \"cKrkJIDYpZUA6XddFe5PY\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665060550725 , \"link\" : null , \"locked\" : false , \"fontSize\" : 36 , \"fontFamily\" : 4 , \"text\" : \"C++ \u57fa\u7840\" , \"rawText\" : \"C++ \u57fa\u7840\" , \"baseline\" : 37 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"C++ \u57fa\u7840\" }, { \"type\" : \"text\" , \"version\" : 172 , \"versionNonce\" : 274743148 , \"isDeleted\" : true , \"id\" : \"w9XRlZgg\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2969.3187842436255 , \"y\" : 2200.7865982003254 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"#fab005\" , \"width\" : 255 , \"height\" : 36 , \"seed\" : 1635294037 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665060725588 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"rawText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"baseline\" : 29 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" } ], \"appState\" : { \"theme\" : \"light\" , \"viewBackgroundColor\" : \"#ffffff\" , \"currentItemStrokeColor\" : \"#ffff\" , \"currentItemBackgroundColor\" : \"#02c39a\" , \"currentItemFillStyle\" : \"solid\" , \"currentItemStrokeWidth\" : 1 , \"currentItemStrokeStyle\" : \"solid\" , \"currentItemRoughness\" : 1 , \"currentItemOpacity\" : 50 , \"currentItemFontFamily\" : 2 , \"currentItemFontSize\" : 16 , \"currentItemTextAlign\" : \"left\" , \"currentItemStrokeSharpness\" : \"sharp\" , \"currentItemStartArrowhead\" : null , \"currentItemEndArrowhead\" : \"arrow\" , \"currentItemLinearStrokeSharpness\" : \"round\" , \"gridSize\" : null , \"colorPalette\" : { \"elementStroke\" : [ \"#000000\" , \"#343a40\" , \"#495057\" , \"#c92a2a\" , \"#a61e4d\" , \"#862e9c\" , \"#5f3dc4\" , \"#364fc7\" , \"#1864ab\" , \"#0b7285\" , \"#087f5b\" , \"#2b8a3e\" , \"#5c940d\" , \"#e67700\" , \"#d9480f\" ], \"elementBackground\" : [ \"transparent\" , \"#ffffff\" , \"#F8F9FA\" , \"#EDEDE9\" , \"#ADB5BD\" , \"#8cb369\" , \"#f4e285\" , \"#f4a259\" , \"#5b8e7d\" , \"#bc4b51\" , \"#05668d\" , \"#028090\" , \"#00a896\" , \"#02c39a\" , \"#f0f3bd\" , \"#c9e4ca\" , \"#87bba2\" , \"#55828b\" , \"#3b6064\" , \"#FEE440\" , \"#ced4da\" , \"#868e96\" , \"#fa5252\" , \"#e64980\" , \"#be4bdb\" , \"#7950f2\" , \"#4c6ef5\" , \"#228be6\" , \"#15aabf\" , \"#12b886\" , \"#40c057\" , \"#82c91e\" , \"#fab005\" , \"#fd7e14\" , \"#A30000\" ], \"canvasBackground\" : [ \"#ffffff\" , \"#f8f9fa\" , \"#f1f3f5\" , \"#fff5f5\" , \"#fff0f6\" , \"#f8f0fc\" , \"#f3f0ff\" , \"#edf2ff\" , \"#e7f5ff\" , \"#e3fafc\" , \"#e6fcf5\" , \"#ebfbee\" , \"#f4fce3\" , \"#fff9db\" , \"#fff4e6\" ] } }, \"files\" : {} } %%","title":"C++ basic onepage"},{"location":"images/onepage/c%2B%2B-basic-onepage/#text-elements","text":"C++ \u57fa\u7840 ^lwSrVksx %%","title":"Text Elements"},{"location":"images/onepage/c%2B%2B-basic-onepage/#drawing","text":"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 { \"type\" : \"excalidraw\" , \"version\" : 2 , \"source\" : \"https://excalidraw.com\" , \"elements\" : [ { \"id\" : \"ZXOOsHpZx0cLKavt_neEO\" , \"type\" : \"rectangle\" , \"x\" : 1526.7604451938828 , \"y\" : 1087.1960299121963 , \"width\" : 148.51637279132316 , \"height\" : 10.141263341084612 , \"angle\" : 0 , \"strokeColor\" : \"#ffff\" , \"backgroundColor\" : \"#02c39a\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 50 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"seed\" : 850846956 , \"version\" : 462 , \"versionNonce\" : 916899820 , \"isDeleted\" : false , \"boundElements\" : null , \"updated\" : 1665060708977 , \"link\" : null , \"locked\" : false }, { \"type\" : \"rectangle\" , \"version\" : 142 , \"versionNonce\" : 227795253 , \"isDeleted\" : false , \"id\" : \"vefkyDhSzZRGpVKbqbz-A\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1501.1026384102925 , \"y\" : 1018.4363377836585 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 1754 , \"height\" : 1240 , \"seed\" : 457283003 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1662103205233 , \"link\" : null , \"locked\" : false }, { \"type\" : \"text\" , \"version\" : 869 , \"versionNonce\" : 1653346924 , \"isDeleted\" : false , \"id\" : \"lwSrVksx\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"dashed\" , \"roughness\" : 0 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1531.385308864838 , \"y\" : 1056.5940082382049 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 145 , \"height\" : 46 , \"seed\" : 416331988 , \"groupIds\" : [ \"cKrkJIDYpZUA6XddFe5PY\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665060550725 , \"link\" : null , \"locked\" : false , \"fontSize\" : 36 , \"fontFamily\" : 4 , \"text\" : \"C++ \u57fa\u7840\" , \"rawText\" : \"C++ \u57fa\u7840\" , \"baseline\" : 37 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"C++ \u57fa\u7840\" }, { \"type\" : \"text\" , \"version\" : 172 , \"versionNonce\" : 274743148 , \"isDeleted\" : true , \"id\" : \"w9XRlZgg\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"dotted\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2969.3187842436255 , \"y\" : 2200.7865982003254 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"#fab005\" , \"width\" : 255 , \"height\" : 36 , \"seed\" : 1635294037 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665060725588 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"rawText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" , \"baseline\" : 29 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"Chrome\u7f29\u653e45%\u6253\u5370\" } ], \"appState\" : { \"theme\" : \"light\" , \"viewBackgroundColor\" : \"#ffffff\" , \"currentItemStrokeColor\" : \"#ffff\" , \"currentItemBackgroundColor\" : \"#02c39a\" , \"currentItemFillStyle\" : \"solid\" , \"currentItemStrokeWidth\" : 1 , \"currentItemStrokeStyle\" : \"solid\" , \"currentItemRoughness\" : 1 , \"currentItemOpacity\" : 50 , \"currentItemFontFamily\" : 2 , \"currentItemFontSize\" : 16 , \"currentItemTextAlign\" : \"left\" , \"currentItemStrokeSharpness\" : \"sharp\" , \"currentItemStartArrowhead\" : null , \"currentItemEndArrowhead\" : \"arrow\" , \"currentItemLinearStrokeSharpness\" : \"round\" , \"gridSize\" : null , \"colorPalette\" : { \"elementStroke\" : [ \"#000000\" , \"#343a40\" , \"#495057\" , \"#c92a2a\" , \"#a61e4d\" , \"#862e9c\" , \"#5f3dc4\" , \"#364fc7\" , \"#1864ab\" , \"#0b7285\" , \"#087f5b\" , \"#2b8a3e\" , \"#5c940d\" , \"#e67700\" , \"#d9480f\" ], \"elementBackground\" : [ \"transparent\" , \"#ffffff\" , \"#F8F9FA\" , \"#EDEDE9\" , \"#ADB5BD\" , \"#8cb369\" , \"#f4e285\" , \"#f4a259\" , \"#5b8e7d\" , \"#bc4b51\" , \"#05668d\" , \"#028090\" , \"#00a896\" , \"#02c39a\" , \"#f0f3bd\" , \"#c9e4ca\" , \"#87bba2\" , \"#55828b\" , \"#3b6064\" , \"#FEE440\" , \"#ced4da\" , \"#868e96\" , \"#fa5252\" , \"#e64980\" , \"#be4bdb\" , \"#7950f2\" , \"#4c6ef5\" , \"#228be6\" , \"#15aabf\" , \"#12b886\" , \"#40c057\" , \"#82c91e\" , \"#fab005\" , \"#fd7e14\" , \"#A30000\" ], \"canvasBackground\" : [ \"#ffffff\" , \"#f8f9fa\" , \"#f1f3f5\" , \"#fff5f5\" , \"#fff0f6\" , \"#f8f0fc\" , \"#f3f0ff\" , \"#edf2ff\" , \"#e7f5ff\" , \"#e3fafc\" , \"#e6fcf5\" , \"#ebfbee\" , \"#f4fce3\" , \"#fff9db\" , \"#fff4e6\" ] } }, \"files\" : {} } %%","title":"Drawing"},{"location":"images/onepage/moc-onepage/","tags":["excalidraw"],"text":"\u26a0 Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. \u26a0 Text Elements C++\u57fa\u7840 ^1mr2Eo26 C++ \u8c03\u8bd5\u6280\u672f ^bb31ZJyB \u57fa\u7840\u6570\u636e\u7c7b\u578b ^olTbwHhy \u8fd0\u7b97\u7b26 ^kA66qMSC \u4f5c\u7528\u57df\u3001\u751f\u547d\u5468\u671f\u548c\u94fe\u63a5\u72b6\u6001 ^BNKiTPV9 \u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406 ^AfT65zJz \u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d ^YxPeVAuU \u590d\u5408\u6570\u636e\u7c7b\u578b ^uMlZJGo6 \u679a\u4e3e\u3001\u7ed3\u6784\u4f53\u3001\u8054\u5408\u4f53\u3001\u7c7b ^HYmrPSvn \u6307\u9488\u548c\u5f15\u7528 ^QnFX9MK3 \u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d ^XOSz3Pfr \u51fd\u6570 ^lYY3Wnxo \u9762\u5411\u5bf9\u8c61 ^gjbfsqQD \u64cd\u4f5c\u7b26\u91cd\u8f7d ^tz6YZ7RQ \u5bf9\u8c61\u5173\u7cfb ^bjVsOrRK \u7ee7\u627f ^3GV1Izm9 \u865a\u51fd\u6570 ^sx6WmlZy \u6a21\u677f\u548c\u7c7b ^GIXAA467 \u5f02\u5e38 ^YeDrvOh6 move\u548c\u667a\u80fd\u6307\u9488 ^FMB76L5Z std::string ^7SrQWl95 \u8f93\u5165\u8f93\u51fa ^tTXxyfZG STL ^CQVGISOo Embedded files 5c9c5c8928e9096c0f746787692758a8ef5b1684: icon/icon-cpp.png %% Drawing 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 { \"type\" : \"excalidraw\" , \"version\" : 2 , \"source\" : \"https://excalidraw.com\" , \"elements\" : [ { \"type\" : \"rectangle\" , \"version\" : 164 , \"versionNonce\" : 673785684 , \"isDeleted\" : false , \"id\" : \"vefkyDhSzZRGpVKbqbz-A\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1501.1026384102925 , \"y\" : 1018.4363377836585 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 1754 , \"height\" : 1240 , \"seed\" : 457283003 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false }, { \"type\" : \"text\" , \"version\" : 752 , \"versionNonce\" : 668903404 , \"isDeleted\" : false , \"id\" : \"1mr2Eo26\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2003.183699440322 , \"y\" : 1183.007709557343 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 106 , \"height\" : 35 , \"seed\" : 13740908 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"C++\u57fa\u7840\" , \"rawText\" : \"C++\u57fa\u7840\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"C++\u57fa\u7840\" }, { \"type\" : \"image\" , \"version\" : 782 , \"versionNonce\" : 1219566804 , \"isDeleted\" : false , \"id\" : \"3nYL79EK\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2252.5261386504494 , \"y\" : 1418.0528792824043 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 264.0676833547609 , \"height\" : 264.0676833547609 , \"seed\" : 3786 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"status\" : \"pending\" , \"fileId\" : \"5c9c5c8928e9096c0f746787692758a8ef5b1684\" , \"scale\" : [ 1 , 1 ] }, { \"type\" : \"text\" , \"version\" : 643 , \"versionNonce\" : 739953260 , \"isDeleted\" : false , \"id\" : \"bb31ZJyB\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1940.183699440322 , \"y\" : 1258.617154313309 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 169 , \"height\" : 35 , \"seed\" : 1304755948 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"C++ \u8c03\u8bd5\u6280\u672f\" , \"rawText\" : \"C++ \u8c03\u8bd5\u6280\u672f\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"C++ \u8c03\u8bd5\u6280\u672f\" }, { \"type\" : \"text\" , \"version\" : 602 , \"versionNonce\" : 1335779924 , \"isDeleted\" : false , \"id\" : \"olTbwHhy\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1939.183699440322 , \"y\" : 1322.7847071940316 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 170 , \"height\" : 35 , \"seed\" : 642744916 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u57fa\u7840\u6570\u636e\u7c7b\u578b\" , \"rawText\" : \"\u57fa\u7840\u6570\u636e\u7c7b\u578b\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u57fa\u7840\u6570\u636e\u7c7b\u578b\" }, { \"type\" : \"text\" , \"version\" : 545 , \"versionNonce\" : 1236180204 , \"isDeleted\" : false , \"id\" : \"kA66qMSC\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2023.183699440322 , \"y\" : 1390.3187826424135 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 86 , \"height\" : 35 , \"seed\" : 1262260308 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u8fd0\u7b97\u7b26\" , \"rawText\" : \"\u8fd0\u7b97\u7b26\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u8fd0\u7b97\u7b26\" }, { \"type\" : \"text\" , \"version\" : 513 , \"versionNonce\" : 1673983956 , \"isDeleted\" : false , \"id\" : \"BNKiTPV9\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1743.183699440322 , \"y\" : 1460.1662352073538 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 366 , \"height\" : 35 , \"seed\" : 103667180 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u4f5c\u7528\u57df\u3001\u751f\u547d\u5468\u671f\u548c\u94fe\u63a5\u72b6\u6001\" , \"rawText\" : \"\u4f5c\u7528\u57df\u3001\u751f\u547d\u5468\u671f\u548c\u94fe\u63a5\u72b6\u6001\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4f5c\u7528\u57df\u3001\u751f\u547d\u5468\u671f\u548c\u94fe\u63a5\u72b6\u6001\" }, { \"type\" : \"text\" , \"version\" : 410 , \"versionNonce\" : 1512316780 , \"isDeleted\" : false , \"id\" : \"AfT65zJz\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1883.183699440322 , \"y\" : 1532.43628691515 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 226 , \"height\" : 35 , \"seed\" : 934101612 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406\" , \"rawText\" : \"\u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406\" }, { \"type\" : \"text\" , \"version\" : 414 , \"versionNonce\" : 1631640916 , \"isDeleted\" : false , \"id\" : \"YxPeVAuU\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1855.1836994403218 , \"y\" : 1604.8325837511927 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 254 , \"height\" : 35 , \"seed\" : 1503980140 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d\" , \"rawText\" : \"\u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d\" }, { \"type\" : \"text\" , \"version\" : 389 , \"versionNonce\" : 768447316 , \"isDeleted\" : false , \"id\" : \"gjbfsqQD\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2643.1852495106145 , \"y\" : 1192.485649027449 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 114 , \"height\" : 35 , \"seed\" : 1106618220 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u9762\u5411\u5bf9\u8c61\" , \"rawText\" : \"\u9762\u5411\u5bf9\u8c61\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u9762\u5411\u5bf9\u8c61\" }, { \"type\" : \"text\" , \"version\" : 489 , \"versionNonce\" : 235338732 , \"isDeleted\" : false , \"id\" : \"tz6YZ7RQ\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1277.5153128755035 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 142 , \"height\" : 35 , \"seed\" : 845482580 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u64cd\u4f5c\u7b26\u91cd\u8f7d\" , \"rawText\" : \"\u64cd\u4f5c\u7b26\u91cd\u8f7d\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u64cd\u4f5c\u7b26\u91cd\u8f7d\" }, { \"type\" : \"text\" , \"version\" : 309 , \"versionNonce\" : 1994918100 , \"isDeleted\" : false , \"id\" : \"bjVsOrRK\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1348.5229993106886 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 114 , \"height\" : 35 , \"seed\" : 939697876 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u5bf9\u8c61\u5173\u7cfb\" , \"rawText\" : \"\u5bf9\u8c61\u5173\u7cfb\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u5bf9\u8c61\u5173\u7cfb\" }, { \"type\" : \"text\" , \"version\" : 309 , \"versionNonce\" : 1377345132 , \"isDeleted\" : false , \"id\" : \"3GV1Izm9\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1418.7836946882596 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 58 , \"height\" : 35 , \"seed\" : 1275507820 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u7ee7\u627f\" , \"rawText\" : \"\u7ee7\u627f\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u7ee7\u627f\" }, { \"type\" : \"text\" , \"version\" : 318 , \"versionNonce\" : 1880608340 , \"isDeleted\" : false , \"id\" : \"sx6WmlZy\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1495.3576598567095 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 86 , \"height\" : 35 , \"seed\" : 960700012 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u865a\u51fd\u6570\" , \"rawText\" : \"\u865a\u51fd\u6570\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u865a\u51fd\u6570\" }, { \"type\" : \"text\" , \"version\" : 394 , \"versionNonce\" : 416411884 , \"isDeleted\" : false , \"id\" : \"GIXAA467\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1584.2367228285116 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 114 , \"height\" : 35 , \"seed\" : 1105428308 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u6a21\u677f\u548c\u7c7b\" , \"rawText\" : \"\u6a21\u677f\u548c\u7c7b\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u6a21\u677f\u548c\u7c7b\" }, { \"type\" : \"text\" , \"version\" : 258 , \"versionNonce\" : 171107284 , \"isDeleted\" : false , \"id\" : \"YeDrvOh6\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1657.7953289861653 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 58 , \"height\" : 35 , \"seed\" : 1041638380 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u5f02\u5e38\" , \"rawText\" : \"\u5f02\u5e38\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u5f02\u5e38\" }, { \"type\" : \"text\" , \"version\" : 283 , \"versionNonce\" : 297595756 , \"isDeleted\" : false , \"id\" : \"FMB76L5Z\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1737.4953083229743 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 202 , \"height\" : 35 , \"seed\" : 237917908 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"move\u548c\u667a\u80fd\u6307\u9488\" , \"rawText\" : \"move\u548c\u667a\u80fd\u6307\u9488\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"move\u548c\u667a\u80fd\u6307\u9488\" }, { \"type\" : \"text\" , \"version\" : 238 , \"versionNonce\" : 726616404 , \"isDeleted\" : false , \"id\" : \"7SrQWl95\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1885.8813504963032 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 117 , \"height\" : 35 , \"seed\" : 1096469484 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"std::string\" , \"rawText\" : \"std::string\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"std::string\" }, { \"type\" : \"text\" , \"version\" : 302 , \"versionNonce\" : 1797498348 , \"isDeleted\" : false , \"id\" : \"tTXxyfZG\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1963.5937900037088 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 114 , \"height\" : 35 , \"seed\" : 228038508 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u8f93\u5165\u8f93\u51fa\" , \"rawText\" : \"\u8f93\u5165\u8f93\u51fa\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u8f93\u5165\u8f93\u51fa\" }, { \"type\" : \"text\" , \"version\" : 246 , \"versionNonce\" : 113442516 , \"isDeleted\" : false , \"id\" : \"CQVGISOo\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1803.2140152263964 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 47 , \"height\" : 35 , \"seed\" : 839597780 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"STL\" , \"rawText\" : \"STL\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"STL\" }, { \"id\" : \"Z47K0mod_t1fau6ZQNkND\" , \"type\" : \"freedraw\" , \"x\" : 2067.560768035615 , \"y\" : 1841.9648764467327 , \"width\" : 104.40199822995942 , \"height\" : 138.54831266364909 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"round\" , \"seed\" : 239673044 , \"version\" : 88 , \"versionNonce\" : 1884203372 , \"isDeleted\" : false , \"boundElements\" : null , \"updated\" : 1665111053227 , \"link\" : null , \"locked\" : false , \"points\" : [ [ 0 , 0 ], [ 0 , -0.2439299024065349 ], [ -0.2439299024065349 , -0.2439299024065349 ], [ -4.464304404359382 , -0.2439299024065349 ], [ -8.59949703563052 , -0.2439299024065349 ], [ -12.734689666901886 , 0.782124448985769 ], [ -16.424613428700923 , 1.7694597682500444 ], [ -21.89948457160108 , 5.350970240090646 ], [ -25.67459020408205 , 9.745580386619395 ], [ -28.67531519400245 , 15.01136875602856 ], [ -32.32651992367346 , 21.89948457160108 ], [ -34.35539720718111 , 27.773161745419884 ], [ -37.25932461678167 , 39.32304902920441 ], [ -38.072424291469815 , 46.617714682121004 ], [ -38.39766416134512 , 57.89656874100956 ], [ -39.27271428743825 , 67.05361983928333 ], [ -40.89891363681454 , 74.34828549219992 ], [ -42.97038185566271 , 85.89817277598445 ], [ -45.0031310423833 , 93.19283842890104 ], [ -46.30409052188429 , 96.47234045014352 ], [ -48.08903790298541 , 101.08764907980185 ], [ -50.241816089302574 , 105.38158974279781 ], [ -57.098956679172716 , 116.88888609124183 ], [ -59.73572276709001 , 119.17330898679415 ], [ -63.75475830197729 , 122.82064181325245 ], [ -68.98957144568385 , 127.62180179712527 ], [ -74.8709924259283 , 130.40957211034197 ], [ -77.31029144999275 , 131.0135890115389 ], [ -81.00021521179178 , 133.0153629725569 ], [ -85.13540784306292 , 134.38601670988828 ], [ -92.31391639959566 , 135.79926138256064 ], [ -97.42482864049248 , 137.25509699057352 ], [ -101.60261220710458 , 137.94042385923944 ], [ -104.40199822995942 , 138.30438276124255 ], [ -104.40199822995942 , 138.30438276124255 ] ], \"pressures\" : [ 1 , 0.9714285714285714 , 0.9428571428571428 , 0.9142857142857143 , 0.8857142857142857 , 0.8571428571428571 , 0.8285714285714286 , 0.8 , 0.7714285714285715 , 0.7428571428571429 , 0.7142857142857143 , 0.6857142857142857 , 0.6571428571428571 , 0.6285714285714286 , 0.6 , 0.5714285714285714 , 0.5428571428571428 , 0.5142857142857142 , 0.4857142857142857 , 0.45714285714285713 , 0.42857142857142855 , 0.4 , 0.37142857142857144 , 0.34285714285714286 , 0.3142857142857143 , 0.2857142857142857 , 0.2571428571428571 , 0.22857142857142856 , 0.2 , 0.17142857142857143 , 0.14285714285714285 , 0.11428571428571428 , 0.08571428571428572 , 0.05714285714285714 , 0.02857142857142857 ], \"simulatePressure\" : false , \"lastCommittedPoint\" : [ -104.40199822995942 , 138.30438276124255 ] }, { \"id\" : \"KyxvbIi7weJQGwq2RPa_j\" , \"type\" : \"freedraw\" , \"x\" : 2062.453727697931 , \"y\" : 1843.2851954422977 , \"width\" : 84.45008097439722 , \"height\" : 47.33014487327637 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"round\" , \"seed\" : 208092372 , \"version\" : 76 , \"versionNonce\" : 6026068 , \"isDeleted\" : false , \"boundElements\" : null , \"updated\" : 1665111053227 , \"link\" : null , \"locked\" : false , \"points\" : [ [ 0 , 0 ], [ -0.4878598048128424 , 0 ], [ -0.7317897072193773 , 0 ], [ -2.361860959808382 , 0 ], [ -3.422762440116003 , 0.2632894184705492 ], [ -6.346049365780345 , 0.8866658357314918 ], [ -11.983540443618267 , 2.768410797152683 ], [ -19.893838707370378 , 5.258044562983514 ], [ -28.4120257755319 , 9.548113322766767 ], [ -32.55109031001575 , 12.951516246818528 ], [ -38.796470192263314 , 18.360565035101217 ], [ -41.96368702033442 , 21.233517218999395 ], [ -47.76766993632282 , 26.336685653470795 ], [ -54.01304981857038 , 31.745734441753484 ], [ -60.095809765880404 , 35.39306726821178 ], [ -67.17364883888013 , 39.543747512333994 ], [ -71.55664327577051 , 42.05274079422907 ], [ -78.31311438210787 , 45.79687120100721 ], [ -82.08822001458861 , 46.82679745561222 ], [ -83.47436136477131 , 47.09008687408277 ], [ -83.96222116958415 , 47.09008687408277 ], [ -84.45008097439722 , 47.33014487327637 ], [ -84.45008097439722 , 47.33014487327637 ] ], \"pressures\" : [ 1 , 0.9565217391304348 , 0.9130434782608695 , 0.8695652173913043 , 0.8260869565217391 , 0.782608695652174 , 0.7391304347826086 , 0.6956521739130435 , 0.6521739130434783 , 0.6086956521739131 , 0.5652173913043478 , 0.5217391304347826 , 0.4782608695652174 , 0.43478260869565216 , 0.391304347826087 , 0.34782608695652173 , 0.30434782608695654 , 0.2608695652173913 , 0.21739130434782608 , 0.17391304347826086 , 0.13043478260869565 , 0.08695652173913043 , 0.043478260869565216 ], \"simulatePressure\" : false , \"lastCommittedPoint\" : [ -84.45008097439722 , 47.33014487327637 ] }, { \"id\" : \"16LsNqHn0Q7NpMmUHfFRJ\" , \"type\" : \"freedraw\" , \"x\" : 2068.2151196785785 , \"y\" : 1840.2922142588027 , \"width\" : 111.45660588368241 , \"height\" : 16.571745750787386 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"round\" , \"seed\" : 1556751700 , \"version\" : 78 , \"versionNonce\" : 974035948 , \"isDeleted\" : false , \"boundElements\" : null , \"updated\" : 1665111053227 , \"link\" : null , \"locked\" : false , \"points\" : [ [ 0 , 0 ], [ -3.2872458276679026 , 0 ], [ -6.373152688270238 , 0 ], [ -9.617807580597173 , 0.32136796666259215 ], [ -12.459784538792974 , 0.32136796666259215 ], [ -17.082836974876955 , -1.4635794144387546 ], [ -23.773485726596846 , -3.2098077634118454 ], [ -28.28038106629674 , -5.1186560473224745 ], [ -32.21810663371525 , -7.228843298298898 ], [ -35.90803039551429 , -8.223922423988824 ], [ -41.386773441627156 , -9.931431740833887 ], [ -48.727901933097655 , -11.979668540405328 ], [ -55.054591782813986 , -13.176086633160821 ], [ -61.582620599595884 , -13.950467275721167 ], [ -72.78016469101567 , -15.352096238754939 ], [ -84.21389487841634 , -15.801237011439753 ], [ -90.74192369519847 , -16.250377784124794 ], [ -96.6233446754427 , -16.250377784124794 ], [ -101.49419891714615 , -15.139141562050781 ], [ -106.72901206085271 , -14.109215307445993 ], [ -108.11515341103541 , -14.109215307445993 ], [ -109.5012947612181 , -14.109215307445993 ], [ -111.21267598127588 , -13.82656637291143 ], [ -111.45660588368241 , -13.82656637291143 ], [ -111.45660588368241 , -13.82656637291143 ] ], \"pressures\" : [ 1 , 0.96 , 0.92 , 0.88 , 0.84 , 0.8 , 0.76 , 0.72 , 0.68 , 0.64 , 0.6 , 0.56 , 0.52 , 0.48 , 0.44 , 0.4 , 0.36 , 0.32 , 0.28 , 0.24 , 0.2 , 0.16 , 0.12 , 0.08 , 0.04 ], \"simulatePressure\" : false , \"lastCommittedPoint\" : [ -111.45660588368241 , -13.82656637291143 ] }, { \"id\" : \"Xbq_iHMIlWBVyWWgQsPSB\" , \"type\" : \"freedraw\" , \"x\" : 2063.3520092433005 , \"y\" : 1837.4192620749045 , \"width\" : 92.45717681846872 , \"height\" : 109.34254672949305 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"round\" , \"seed\" : 500221036 , \"version\" : 81 , \"versionNonce\" : 682256596 , \"isDeleted\" : false , \"boundElements\" : null , \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"points\" : [ [ 0 , 0 ], [ -1.060901480307166 , -0.26716132168303375 ], [ -4.3481473079750685 , -1.2622404473729603 ], [ -7.635393135642971 , -2.601918959001978 ], [ -11.549887283784301 , -5.440024013984839 ], [ -14.77905456326016 , -9.439700032808105 ], [ -20.602396995312574 , -23.982568500087837 ], [ -24.64466394947658 , -37.495510712762325 ], [ -26.65418171692022 , -44.310060367291726 ], [ -28.683059000427875 , -51.283358053545726 ], [ -31.343056507621895 , -60.002884088773044 ], [ -33.00797488912622 , -63.69280785057208 ], [ -38.11888713002327 , -73.79460333276938 ], [ -40.69370276653581 , -78.56091618772712 ], [ -43.39241930585786 , -83.24979097842879 ], [ -52.321028114576166 , -94.81129397185168 ], [ -55.06233558923941 , -97.91656034851803 ], [ -57.66425454824139 , -99.86799956776963 ], [ -61.00957892410133 , -102.22211672115236 ], [ -65.07120539432935 , -104.11547739221214 ], [ -69.24898896094123 , -105.4977468391819 ], [ -73.42677252755311 , -106.88001628615166 ], [ -75.86607155161755 , -107.18589663996295 ], [ -83.8189607507104 , -108.28551715239837 ], [ -88.52332315426338 , -108.67270747367843 ], [ -92.21324691606242 , -109.34254672949305 ], [ -92.45717681846872 , -109.34254672949305 ], [ -92.45717681846872 , -109.34254672949305 ] ], \"pressures\" : [ 1 , 0.9642857142857143 , 0.9285714285714286 , 0.8928571428571429 , 0.8571428571428571 , 0.8214285714285714 , 0.7857142857142857 , 0.75 , 0.7142857142857143 , 0.6785714285714286 , 0.6428571428571429 , 0.6071428571428571 , 0.5714285714285714 , 0.5357142857142857 , 0.5 , 0.4642857142857143 , 0.42857142857142855 , 0.39285714285714285 , 0.35714285714285715 , 0.32142857142857145 , 0.2857142857142857 , 0.25 , 0.21428571428571427 , 0.17857142857142858 , 0.14285714285714285 , 0.10714285714285714 , 0.07142857142857142 , 0.03571428571428571 ], \"simulatePressure\" : false , \"lastCommittedPoint\" : [ -92.45717681846872 , -109.34254672949305 ] }, { \"type\" : \"text\" , \"version\" : 641 , \"versionNonce\" : 1546403436 , \"isDeleted\" : false , \"id\" : \"uMlZJGo6\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2080.4926584636933 , \"y\" : 1827.2122128352817 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 170 , \"height\" : 35 , \"seed\" : 1992861652 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u590d\u5408\u6570\u636e\u7c7b\u578b\" , \"rawText\" : \"\u590d\u5408\u6570\u636e\u7c7b\u578b\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u590d\u5408\u6570\u636e\u7c7b\u578b\" }, { \"type\" : \"text\" , \"version\" : 358 , \"versionNonce\" : 2056138324 , \"isDeleted\" : false , \"id\" : \"QnFX9MK3\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1805.0035911794105 , \"y\" : 1714.6381277495311 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 142 , \"height\" : 35 , \"seed\" : 176679148 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u6307\u9488\u548c\u5f15\u7528\" , \"rawText\" : \"\u6307\u9488\u548c\u5f15\u7528\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u6307\u9488\u548c\u5f15\u7528\" }, { \"type\" : \"text\" , \"version\" : 581 , \"versionNonce\" : 1601278188 , \"isDeleted\" : false , \"id\" : \"HYmrPSvn\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1608.013708893797 , \"y\" : 1807.1527917457315 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 338 , \"height\" : 35 , \"seed\" : 1357615060 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u679a\u4e3e\u3001\u7ed3\u6784\u4f53\u3001\u8054\u5408\u4f53\u3001\u7c7b\" , \"rawText\" : \"\u679a\u4e3e\u3001\u7ed3\u6784\u4f53\u3001\u8054\u5408\u4f53\u3001\u7c7b\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u679a\u4e3e\u3001\u7ed3\u6784\u4f53\u3001\u8054\u5408\u4f53\u3001\u7c7b\" }, { \"type\" : \"text\" , \"version\" : 360 , \"versionNonce\" : 920470484 , \"isDeleted\" : false , \"id\" : \"XOSz3Pfr\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1596.8748581279638 , \"y\" : 1869.4389768060942 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 366 , \"height\" : 35 , \"seed\" : 226773716 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d\" , \"rawText\" : \"\u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d\" }, { \"type\" : \"text\" , \"version\" : 433 , \"versionNonce\" : 594056044 , \"isDeleted\" : false , \"id\" : \"lYY3Wnxo\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1876.9415330172199 , \"y\" : 1962.435173187996 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 58 , \"height\" : 35 , \"seed\" : 1218028 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u51fd\u6570\" , \"rawText\" : \"\u51fd\u6570\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u51fd\u6570\" } ], \"appState\" : { \"theme\" : \"light\" , \"viewBackgroundColor\" : \"white\" , \"currentItemStrokeColor\" : \"#000000\" , \"currentItemBackgroundColor\" : \"transparent\" , \"currentItemFillStyle\" : \"solid\" , \"currentItemStrokeWidth\" : 2 , \"currentItemStrokeStyle\" : \"solid\" , \"currentItemRoughness\" : 1 , \"currentItemOpacity\" : 100 , \"currentItemFontFamily\" : 4 , \"currentItemFontSize\" : 28 , \"currentItemTextAlign\" : \"left\" , \"currentItemStrokeSharpness\" : \"sharp\" , \"currentItemStartArrowhead\" : null , \"currentItemEndArrowhead\" : \"arrow\" , \"currentItemLinearStrokeSharpness\" : \"round\" , \"gridSize\" : null , \"colorPalette\" : { \"elementStroke\" : [ \"#000000\" , \"#343a40\" , \"#495057\" , \"#c92a2a\" , \"#a61e4d\" , \"#862e9c\" , \"#5f3dc4\" , \"#364fc7\" , \"#1864ab\" , \"#0b7285\" , \"#087f5b\" , \"#2b8a3e\" , \"#5c940d\" , \"#e67700\" , \"#d9480f\" ], \"elementBackground\" : [ \"transparent\" , \"#ffffff\" , \"#F8F9FA\" , \"#EDEDE9\" , \"#ADB5BD\" , \"#8cb369\" , \"#f4e285\" , \"#f4a259\" , \"#5b8e7d\" , \"#bc4b51\" , \"#05668d\" , \"#028090\" , \"#00a896\" , \"#02c39a\" , \"#f0f3bd\" , \"#c9e4ca\" , \"#87bba2\" , \"#55828b\" , \"#3b6064\" , \"#FEE440\" , \"#ced4da\" , \"#868e96\" , \"#fa5252\" , \"#e64980\" , \"#be4bdb\" , \"#7950f2\" , \"#4c6ef5\" , \"#228be6\" , \"#15aabf\" , \"#12b886\" , \"#40c057\" , \"#82c91e\" , \"#fab005\" , \"#fd7e14\" , \"#A30000\" ], \"canvasBackground\" : [ \"#ffffff\" , \"#f8f9fa\" , \"#f1f3f5\" , \"#fff5f5\" , \"#fff0f6\" , \"#f8f0fc\" , \"#f3f0ff\" , \"#edf2ff\" , \"#e7f5ff\" , \"#e3fafc\" , \"#e6fcf5\" , \"#ebfbee\" , \"#f4fce3\" , \"#fff9db\" , \"#fff4e6\" ] } }, \"files\" : {} } %%","title":"Moc onepage"},{"location":"images/onepage/moc-onepage/#text-elements","text":"C++\u57fa\u7840 ^1mr2Eo26 C++ \u8c03\u8bd5\u6280\u672f ^bb31ZJyB \u57fa\u7840\u6570\u636e\u7c7b\u578b ^olTbwHhy \u8fd0\u7b97\u7b26 ^kA66qMSC \u4f5c\u7528\u57df\u3001\u751f\u547d\u5468\u671f\u548c\u94fe\u63a5\u72b6\u6001 ^BNKiTPV9 \u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406 ^AfT65zJz \u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d ^YxPeVAuU \u590d\u5408\u6570\u636e\u7c7b\u578b ^uMlZJGo6 \u679a\u4e3e\u3001\u7ed3\u6784\u4f53\u3001\u8054\u5408\u4f53\u3001\u7c7b ^HYmrPSvn \u6307\u9488\u548c\u5f15\u7528 ^QnFX9MK3 \u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d ^XOSz3Pfr \u51fd\u6570 ^lYY3Wnxo \u9762\u5411\u5bf9\u8c61 ^gjbfsqQD \u64cd\u4f5c\u7b26\u91cd\u8f7d ^tz6YZ7RQ \u5bf9\u8c61\u5173\u7cfb ^bjVsOrRK \u7ee7\u627f ^3GV1Izm9 \u865a\u51fd\u6570 ^sx6WmlZy \u6a21\u677f\u548c\u7c7b ^GIXAA467 \u5f02\u5e38 ^YeDrvOh6 move\u548c\u667a\u80fd\u6307\u9488 ^FMB76L5Z std::string ^7SrQWl95 \u8f93\u5165\u8f93\u51fa ^tTXxyfZG STL ^CQVGISOo","title":"Text Elements"},{"location":"images/onepage/moc-onepage/#embedded-files","text":"5c9c5c8928e9096c0f746787692758a8ef5b1684: icon/icon-cpp.png %%","title":"Embedded files"},{"location":"images/onepage/moc-onepage/#drawing","text":"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 { \"type\" : \"excalidraw\" , \"version\" : 2 , \"source\" : \"https://excalidraw.com\" , \"elements\" : [ { \"type\" : \"rectangle\" , \"version\" : 164 , \"versionNonce\" : 673785684 , \"isDeleted\" : false , \"id\" : \"vefkyDhSzZRGpVKbqbz-A\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1501.1026384102925 , \"y\" : 1018.4363377836585 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 1754 , \"height\" : 1240 , \"seed\" : 457283003 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false }, { \"type\" : \"text\" , \"version\" : 752 , \"versionNonce\" : 668903404 , \"isDeleted\" : false , \"id\" : \"1mr2Eo26\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2003.183699440322 , \"y\" : 1183.007709557343 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 106 , \"height\" : 35 , \"seed\" : 13740908 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"C++\u57fa\u7840\" , \"rawText\" : \"C++\u57fa\u7840\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"C++\u57fa\u7840\" }, { \"type\" : \"image\" , \"version\" : 782 , \"versionNonce\" : 1219566804 , \"isDeleted\" : false , \"id\" : \"3nYL79EK\" , \"fillStyle\" : \"hachure\" , \"strokeWidth\" : 1 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2252.5261386504494 , \"y\" : 1418.0528792824043 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 264.0676833547609 , \"height\" : 264.0676833547609 , \"seed\" : 3786 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"status\" : \"pending\" , \"fileId\" : \"5c9c5c8928e9096c0f746787692758a8ef5b1684\" , \"scale\" : [ 1 , 1 ] }, { \"type\" : \"text\" , \"version\" : 643 , \"versionNonce\" : 739953260 , \"isDeleted\" : false , \"id\" : \"bb31ZJyB\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1940.183699440322 , \"y\" : 1258.617154313309 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 169 , \"height\" : 35 , \"seed\" : 1304755948 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"C++ \u8c03\u8bd5\u6280\u672f\" , \"rawText\" : \"C++ \u8c03\u8bd5\u6280\u672f\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"C++ \u8c03\u8bd5\u6280\u672f\" }, { \"type\" : \"text\" , \"version\" : 602 , \"versionNonce\" : 1335779924 , \"isDeleted\" : false , \"id\" : \"olTbwHhy\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1939.183699440322 , \"y\" : 1322.7847071940316 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 170 , \"height\" : 35 , \"seed\" : 642744916 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u57fa\u7840\u6570\u636e\u7c7b\u578b\" , \"rawText\" : \"\u57fa\u7840\u6570\u636e\u7c7b\u578b\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u57fa\u7840\u6570\u636e\u7c7b\u578b\" }, { \"type\" : \"text\" , \"version\" : 545 , \"versionNonce\" : 1236180204 , \"isDeleted\" : false , \"id\" : \"kA66qMSC\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2023.183699440322 , \"y\" : 1390.3187826424135 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 86 , \"height\" : 35 , \"seed\" : 1262260308 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u8fd0\u7b97\u7b26\" , \"rawText\" : \"\u8fd0\u7b97\u7b26\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u8fd0\u7b97\u7b26\" }, { \"type\" : \"text\" , \"version\" : 513 , \"versionNonce\" : 1673983956 , \"isDeleted\" : false , \"id\" : \"BNKiTPV9\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1743.183699440322 , \"y\" : 1460.1662352073538 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 366 , \"height\" : 35 , \"seed\" : 103667180 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u4f5c\u7528\u57df\u3001\u751f\u547d\u5468\u671f\u548c\u94fe\u63a5\u72b6\u6001\" , \"rawText\" : \"\u4f5c\u7528\u57df\u3001\u751f\u547d\u5468\u671f\u548c\u94fe\u63a5\u72b6\u6001\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u4f5c\u7528\u57df\u3001\u751f\u547d\u5468\u671f\u548c\u94fe\u63a5\u72b6\u6001\" }, { \"type\" : \"text\" , \"version\" : 410 , \"versionNonce\" : 1512316780 , \"isDeleted\" : false , \"id\" : \"AfT65zJz\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1883.183699440322 , \"y\" : 1532.43628691515 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 226 , \"height\" : 35 , \"seed\" : 934101612 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406\" , \"rawText\" : \"\u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406\" }, { \"type\" : \"text\" , \"version\" : 414 , \"versionNonce\" : 1631640916 , \"isDeleted\" : false , \"id\" : \"YxPeVAuU\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1855.1836994403218 , \"y\" : 1604.8325837511927 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 254 , \"height\" : 35 , \"seed\" : 1503980140 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d\" , \"rawText\" : \"\u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d\" }, { \"type\" : \"text\" , \"version\" : 389 , \"versionNonce\" : 768447316 , \"isDeleted\" : false , \"id\" : \"gjbfsqQD\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2643.1852495106145 , \"y\" : 1192.485649027449 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 114 , \"height\" : 35 , \"seed\" : 1106618220 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u9762\u5411\u5bf9\u8c61\" , \"rawText\" : \"\u9762\u5411\u5bf9\u8c61\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u9762\u5411\u5bf9\u8c61\" }, { \"type\" : \"text\" , \"version\" : 489 , \"versionNonce\" : 235338732 , \"isDeleted\" : false , \"id\" : \"tz6YZ7RQ\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1277.5153128755035 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 142 , \"height\" : 35 , \"seed\" : 845482580 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u64cd\u4f5c\u7b26\u91cd\u8f7d\" , \"rawText\" : \"\u64cd\u4f5c\u7b26\u91cd\u8f7d\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u64cd\u4f5c\u7b26\u91cd\u8f7d\" }, { \"type\" : \"text\" , \"version\" : 309 , \"versionNonce\" : 1994918100 , \"isDeleted\" : false , \"id\" : \"bjVsOrRK\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1348.5229993106886 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 114 , \"height\" : 35 , \"seed\" : 939697876 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u5bf9\u8c61\u5173\u7cfb\" , \"rawText\" : \"\u5bf9\u8c61\u5173\u7cfb\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u5bf9\u8c61\u5173\u7cfb\" }, { \"type\" : \"text\" , \"version\" : 309 , \"versionNonce\" : 1377345132 , \"isDeleted\" : false , \"id\" : \"3GV1Izm9\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1418.7836946882596 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 58 , \"height\" : 35 , \"seed\" : 1275507820 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u7ee7\u627f\" , \"rawText\" : \"\u7ee7\u627f\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u7ee7\u627f\" }, { \"type\" : \"text\" , \"version\" : 318 , \"versionNonce\" : 1880608340 , \"isDeleted\" : false , \"id\" : \"sx6WmlZy\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1495.3576598567095 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 86 , \"height\" : 35 , \"seed\" : 960700012 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u865a\u51fd\u6570\" , \"rawText\" : \"\u865a\u51fd\u6570\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u865a\u51fd\u6570\" }, { \"type\" : \"text\" , \"version\" : 394 , \"versionNonce\" : 416411884 , \"isDeleted\" : false , \"id\" : \"GIXAA467\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1584.2367228285116 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 114 , \"height\" : 35 , \"seed\" : 1105428308 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u6a21\u677f\u548c\u7c7b\" , \"rawText\" : \"\u6a21\u677f\u548c\u7c7b\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u6a21\u677f\u548c\u7c7b\" }, { \"type\" : \"text\" , \"version\" : 258 , \"versionNonce\" : 171107284 , \"isDeleted\" : false , \"id\" : \"YeDrvOh6\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1657.7953289861653 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 58 , \"height\" : 35 , \"seed\" : 1041638380 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u5f02\u5e38\" , \"rawText\" : \"\u5f02\u5e38\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u5f02\u5e38\" }, { \"type\" : \"text\" , \"version\" : 283 , \"versionNonce\" : 297595756 , \"isDeleted\" : false , \"id\" : \"FMB76L5Z\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1737.4953083229743 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 202 , \"height\" : 35 , \"seed\" : 237917908 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"move\u548c\u667a\u80fd\u6307\u9488\" , \"rawText\" : \"move\u548c\u667a\u80fd\u6307\u9488\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"move\u548c\u667a\u80fd\u6307\u9488\" }, { \"type\" : \"text\" , \"version\" : 238 , \"versionNonce\" : 726616404 , \"isDeleted\" : false , \"id\" : \"7SrQWl95\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1885.8813504963032 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 117 , \"height\" : 35 , \"seed\" : 1096469484 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"std::string\" , \"rawText\" : \"std::string\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"std::string\" }, { \"type\" : \"text\" , \"version\" : 302 , \"versionNonce\" : 1797498348 , \"isDeleted\" : false , \"id\" : \"tTXxyfZG\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1963.5937900037088 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 114 , \"height\" : 35 , \"seed\" : 228038508 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u8f93\u5165\u8f93\u51fa\" , \"rawText\" : \"\u8f93\u5165\u8f93\u51fa\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u8f93\u5165\u8f93\u51fa\" }, { \"type\" : \"text\" , \"version\" : 246 , \"versionNonce\" : 113442516 , \"isDeleted\" : false , \"id\" : \"CQVGISOo\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2634.491669932109 , \"y\" : 1803.2140152263964 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 47 , \"height\" : 35 , \"seed\" : 839597780 , \"groupIds\" : [], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665110824834 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"STL\" , \"rawText\" : \"STL\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"STL\" }, { \"id\" : \"Z47K0mod_t1fau6ZQNkND\" , \"type\" : \"freedraw\" , \"x\" : 2067.560768035615 , \"y\" : 1841.9648764467327 , \"width\" : 104.40199822995942 , \"height\" : 138.54831266364909 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"round\" , \"seed\" : 239673044 , \"version\" : 88 , \"versionNonce\" : 1884203372 , \"isDeleted\" : false , \"boundElements\" : null , \"updated\" : 1665111053227 , \"link\" : null , \"locked\" : false , \"points\" : [ [ 0 , 0 ], [ 0 , -0.2439299024065349 ], [ -0.2439299024065349 , -0.2439299024065349 ], [ -4.464304404359382 , -0.2439299024065349 ], [ -8.59949703563052 , -0.2439299024065349 ], [ -12.734689666901886 , 0.782124448985769 ], [ -16.424613428700923 , 1.7694597682500444 ], [ -21.89948457160108 , 5.350970240090646 ], [ -25.67459020408205 , 9.745580386619395 ], [ -28.67531519400245 , 15.01136875602856 ], [ -32.32651992367346 , 21.89948457160108 ], [ -34.35539720718111 , 27.773161745419884 ], [ -37.25932461678167 , 39.32304902920441 ], [ -38.072424291469815 , 46.617714682121004 ], [ -38.39766416134512 , 57.89656874100956 ], [ -39.27271428743825 , 67.05361983928333 ], [ -40.89891363681454 , 74.34828549219992 ], [ -42.97038185566271 , 85.89817277598445 ], [ -45.0031310423833 , 93.19283842890104 ], [ -46.30409052188429 , 96.47234045014352 ], [ -48.08903790298541 , 101.08764907980185 ], [ -50.241816089302574 , 105.38158974279781 ], [ -57.098956679172716 , 116.88888609124183 ], [ -59.73572276709001 , 119.17330898679415 ], [ -63.75475830197729 , 122.82064181325245 ], [ -68.98957144568385 , 127.62180179712527 ], [ -74.8709924259283 , 130.40957211034197 ], [ -77.31029144999275 , 131.0135890115389 ], [ -81.00021521179178 , 133.0153629725569 ], [ -85.13540784306292 , 134.38601670988828 ], [ -92.31391639959566 , 135.79926138256064 ], [ -97.42482864049248 , 137.25509699057352 ], [ -101.60261220710458 , 137.94042385923944 ], [ -104.40199822995942 , 138.30438276124255 ], [ -104.40199822995942 , 138.30438276124255 ] ], \"pressures\" : [ 1 , 0.9714285714285714 , 0.9428571428571428 , 0.9142857142857143 , 0.8857142857142857 , 0.8571428571428571 , 0.8285714285714286 , 0.8 , 0.7714285714285715 , 0.7428571428571429 , 0.7142857142857143 , 0.6857142857142857 , 0.6571428571428571 , 0.6285714285714286 , 0.6 , 0.5714285714285714 , 0.5428571428571428 , 0.5142857142857142 , 0.4857142857142857 , 0.45714285714285713 , 0.42857142857142855 , 0.4 , 0.37142857142857144 , 0.34285714285714286 , 0.3142857142857143 , 0.2857142857142857 , 0.2571428571428571 , 0.22857142857142856 , 0.2 , 0.17142857142857143 , 0.14285714285714285 , 0.11428571428571428 , 0.08571428571428572 , 0.05714285714285714 , 0.02857142857142857 ], \"simulatePressure\" : false , \"lastCommittedPoint\" : [ -104.40199822995942 , 138.30438276124255 ] }, { \"id\" : \"KyxvbIi7weJQGwq2RPa_j\" , \"type\" : \"freedraw\" , \"x\" : 2062.453727697931 , \"y\" : 1843.2851954422977 , \"width\" : 84.45008097439722 , \"height\" : 47.33014487327637 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"round\" , \"seed\" : 208092372 , \"version\" : 76 , \"versionNonce\" : 6026068 , \"isDeleted\" : false , \"boundElements\" : null , \"updated\" : 1665111053227 , \"link\" : null , \"locked\" : false , \"points\" : [ [ 0 , 0 ], [ -0.4878598048128424 , 0 ], [ -0.7317897072193773 , 0 ], [ -2.361860959808382 , 0 ], [ -3.422762440116003 , 0.2632894184705492 ], [ -6.346049365780345 , 0.8866658357314918 ], [ -11.983540443618267 , 2.768410797152683 ], [ -19.893838707370378 , 5.258044562983514 ], [ -28.4120257755319 , 9.548113322766767 ], [ -32.55109031001575 , 12.951516246818528 ], [ -38.796470192263314 , 18.360565035101217 ], [ -41.96368702033442 , 21.233517218999395 ], [ -47.76766993632282 , 26.336685653470795 ], [ -54.01304981857038 , 31.745734441753484 ], [ -60.095809765880404 , 35.39306726821178 ], [ -67.17364883888013 , 39.543747512333994 ], [ -71.55664327577051 , 42.05274079422907 ], [ -78.31311438210787 , 45.79687120100721 ], [ -82.08822001458861 , 46.82679745561222 ], [ -83.47436136477131 , 47.09008687408277 ], [ -83.96222116958415 , 47.09008687408277 ], [ -84.45008097439722 , 47.33014487327637 ], [ -84.45008097439722 , 47.33014487327637 ] ], \"pressures\" : [ 1 , 0.9565217391304348 , 0.9130434782608695 , 0.8695652173913043 , 0.8260869565217391 , 0.782608695652174 , 0.7391304347826086 , 0.6956521739130435 , 0.6521739130434783 , 0.6086956521739131 , 0.5652173913043478 , 0.5217391304347826 , 0.4782608695652174 , 0.43478260869565216 , 0.391304347826087 , 0.34782608695652173 , 0.30434782608695654 , 0.2608695652173913 , 0.21739130434782608 , 0.17391304347826086 , 0.13043478260869565 , 0.08695652173913043 , 0.043478260869565216 ], \"simulatePressure\" : false , \"lastCommittedPoint\" : [ -84.45008097439722 , 47.33014487327637 ] }, { \"id\" : \"16LsNqHn0Q7NpMmUHfFRJ\" , \"type\" : \"freedraw\" , \"x\" : 2068.2151196785785 , \"y\" : 1840.2922142588027 , \"width\" : 111.45660588368241 , \"height\" : 16.571745750787386 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"round\" , \"seed\" : 1556751700 , \"version\" : 78 , \"versionNonce\" : 974035948 , \"isDeleted\" : false , \"boundElements\" : null , \"updated\" : 1665111053227 , \"link\" : null , \"locked\" : false , \"points\" : [ [ 0 , 0 ], [ -3.2872458276679026 , 0 ], [ -6.373152688270238 , 0 ], [ -9.617807580597173 , 0.32136796666259215 ], [ -12.459784538792974 , 0.32136796666259215 ], [ -17.082836974876955 , -1.4635794144387546 ], [ -23.773485726596846 , -3.2098077634118454 ], [ -28.28038106629674 , -5.1186560473224745 ], [ -32.21810663371525 , -7.228843298298898 ], [ -35.90803039551429 , -8.223922423988824 ], [ -41.386773441627156 , -9.931431740833887 ], [ -48.727901933097655 , -11.979668540405328 ], [ -55.054591782813986 , -13.176086633160821 ], [ -61.582620599595884 , -13.950467275721167 ], [ -72.78016469101567 , -15.352096238754939 ], [ -84.21389487841634 , -15.801237011439753 ], [ -90.74192369519847 , -16.250377784124794 ], [ -96.6233446754427 , -16.250377784124794 ], [ -101.49419891714615 , -15.139141562050781 ], [ -106.72901206085271 , -14.109215307445993 ], [ -108.11515341103541 , -14.109215307445993 ], [ -109.5012947612181 , -14.109215307445993 ], [ -111.21267598127588 , -13.82656637291143 ], [ -111.45660588368241 , -13.82656637291143 ], [ -111.45660588368241 , -13.82656637291143 ] ], \"pressures\" : [ 1 , 0.96 , 0.92 , 0.88 , 0.84 , 0.8 , 0.76 , 0.72 , 0.68 , 0.64 , 0.6 , 0.56 , 0.52 , 0.48 , 0.44 , 0.4 , 0.36 , 0.32 , 0.28 , 0.24 , 0.2 , 0.16 , 0.12 , 0.08 , 0.04 ], \"simulatePressure\" : false , \"lastCommittedPoint\" : [ -111.45660588368241 , -13.82656637291143 ] }, { \"id\" : \"Xbq_iHMIlWBVyWWgQsPSB\" , \"type\" : \"freedraw\" , \"x\" : 2063.3520092433005 , \"y\" : 1837.4192620749045 , \"width\" : 92.45717681846872 , \"height\" : 109.34254672949305 , \"angle\" : 0 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"round\" , \"seed\" : 500221036 , \"version\" : 81 , \"versionNonce\" : 682256596 , \"isDeleted\" : false , \"boundElements\" : null , \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"points\" : [ [ 0 , 0 ], [ -1.060901480307166 , -0.26716132168303375 ], [ -4.3481473079750685 , -1.2622404473729603 ], [ -7.635393135642971 , -2.601918959001978 ], [ -11.549887283784301 , -5.440024013984839 ], [ -14.77905456326016 , -9.439700032808105 ], [ -20.602396995312574 , -23.982568500087837 ], [ -24.64466394947658 , -37.495510712762325 ], [ -26.65418171692022 , -44.310060367291726 ], [ -28.683059000427875 , -51.283358053545726 ], [ -31.343056507621895 , -60.002884088773044 ], [ -33.00797488912622 , -63.69280785057208 ], [ -38.11888713002327 , -73.79460333276938 ], [ -40.69370276653581 , -78.56091618772712 ], [ -43.39241930585786 , -83.24979097842879 ], [ -52.321028114576166 , -94.81129397185168 ], [ -55.06233558923941 , -97.91656034851803 ], [ -57.66425454824139 , -99.86799956776963 ], [ -61.00957892410133 , -102.22211672115236 ], [ -65.07120539432935 , -104.11547739221214 ], [ -69.24898896094123 , -105.4977468391819 ], [ -73.42677252755311 , -106.88001628615166 ], [ -75.86607155161755 , -107.18589663996295 ], [ -83.8189607507104 , -108.28551715239837 ], [ -88.52332315426338 , -108.67270747367843 ], [ -92.21324691606242 , -109.34254672949305 ], [ -92.45717681846872 , -109.34254672949305 ], [ -92.45717681846872 , -109.34254672949305 ] ], \"pressures\" : [ 1 , 0.9642857142857143 , 0.9285714285714286 , 0.8928571428571429 , 0.8571428571428571 , 0.8214285714285714 , 0.7857142857142857 , 0.75 , 0.7142857142857143 , 0.6785714285714286 , 0.6428571428571429 , 0.6071428571428571 , 0.5714285714285714 , 0.5357142857142857 , 0.5 , 0.4642857142857143 , 0.42857142857142855 , 0.39285714285714285 , 0.35714285714285715 , 0.32142857142857145 , 0.2857142857142857 , 0.25 , 0.21428571428571427 , 0.17857142857142858 , 0.14285714285714285 , 0.10714285714285714 , 0.07142857142857142 , 0.03571428571428571 ], \"simulatePressure\" : false , \"lastCommittedPoint\" : [ -92.45717681846872 , -109.34254672949305 ] }, { \"type\" : \"text\" , \"version\" : 641 , \"versionNonce\" : 1546403436 , \"isDeleted\" : false , \"id\" : \"uMlZJGo6\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 2080.4926584636933 , \"y\" : 1827.2122128352817 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 170 , \"height\" : 35 , \"seed\" : 1992861652 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u590d\u5408\u6570\u636e\u7c7b\u578b\" , \"rawText\" : \"\u590d\u5408\u6570\u636e\u7c7b\u578b\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u590d\u5408\u6570\u636e\u7c7b\u578b\" }, { \"type\" : \"text\" , \"version\" : 358 , \"versionNonce\" : 2056138324 , \"isDeleted\" : false , \"id\" : \"QnFX9MK3\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1805.0035911794105 , \"y\" : 1714.6381277495311 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 142 , \"height\" : 35 , \"seed\" : 176679148 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u6307\u9488\u548c\u5f15\u7528\" , \"rawText\" : \"\u6307\u9488\u548c\u5f15\u7528\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u6307\u9488\u548c\u5f15\u7528\" }, { \"type\" : \"text\" , \"version\" : 581 , \"versionNonce\" : 1601278188 , \"isDeleted\" : false , \"id\" : \"HYmrPSvn\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 4 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1608.013708893797 , \"y\" : 1807.1527917457315 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 338 , \"height\" : 35 , \"seed\" : 1357615060 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u679a\u4e3e\u3001\u7ed3\u6784\u4f53\u3001\u8054\u5408\u4f53\u3001\u7c7b\" , \"rawText\" : \"\u679a\u4e3e\u3001\u7ed3\u6784\u4f53\u3001\u8054\u5408\u4f53\u3001\u7c7b\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u679a\u4e3e\u3001\u7ed3\u6784\u4f53\u3001\u8054\u5408\u4f53\u3001\u7c7b\" }, { \"type\" : \"text\" , \"version\" : 360 , \"versionNonce\" : 920470484 , \"isDeleted\" : false , \"id\" : \"XOSz3Pfr\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1596.8748581279638 , \"y\" : 1869.4389768060942 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 366 , \"height\" : 35 , \"seed\" : 226773716 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d\" , \"rawText\" : \"\u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5185\u5b58\u5206\u914d\" }, { \"type\" : \"text\" , \"version\" : 433 , \"versionNonce\" : 594056044 , \"isDeleted\" : false , \"id\" : \"lYY3Wnxo\" , \"fillStyle\" : \"solid\" , \"strokeWidth\" : 2 , \"strokeStyle\" : \"solid\" , \"roughness\" : 1 , \"opacity\" : 100 , \"angle\" : 0 , \"x\" : 1876.9415330172199 , \"y\" : 1962.435173187996 , \"strokeColor\" : \"#000000\" , \"backgroundColor\" : \"transparent\" , \"width\" : 58 , \"height\" : 35 , \"seed\" : 1218028 , \"groupIds\" : [ \"0P9QqL56Rx28ETXfLmqVq\" ], \"strokeSharpness\" : \"sharp\" , \"boundElements\" : [], \"updated\" : 1665111053228 , \"link\" : null , \"locked\" : false , \"fontSize\" : 28 , \"fontFamily\" : 4 , \"text\" : \"\u51fd\u6570\" , \"rawText\" : \"\u51fd\u6570\" , \"baseline\" : 28 , \"textAlign\" : \"left\" , \"verticalAlign\" : \"top\" , \"containerId\" : null , \"originalText\" : \"\u51fd\u6570\" } ], \"appState\" : { \"theme\" : \"light\" , \"viewBackgroundColor\" : \"white\" , \"currentItemStrokeColor\" : \"#000000\" , \"currentItemBackgroundColor\" : \"transparent\" , \"currentItemFillStyle\" : \"solid\" , \"currentItemStrokeWidth\" : 2 , \"currentItemStrokeStyle\" : \"solid\" , \"currentItemRoughness\" : 1 , \"currentItemOpacity\" : 100 , \"currentItemFontFamily\" : 4 , \"currentItemFontSize\" : 28 , \"currentItemTextAlign\" : \"left\" , \"currentItemStrokeSharpness\" : \"sharp\" , \"currentItemStartArrowhead\" : null , \"currentItemEndArrowhead\" : \"arrow\" , \"currentItemLinearStrokeSharpness\" : \"round\" , \"gridSize\" : null , \"colorPalette\" : { \"elementStroke\" : [ \"#000000\" , \"#343a40\" , \"#495057\" , \"#c92a2a\" , \"#a61e4d\" , \"#862e9c\" , \"#5f3dc4\" , \"#364fc7\" , \"#1864ab\" , \"#0b7285\" , \"#087f5b\" , \"#2b8a3e\" , \"#5c940d\" , \"#e67700\" , \"#d9480f\" ], \"elementBackground\" : [ \"transparent\" , \"#ffffff\" , \"#F8F9FA\" , \"#EDEDE9\" , \"#ADB5BD\" , \"#8cb369\" , \"#f4e285\" , \"#f4a259\" , \"#5b8e7d\" , \"#bc4b51\" , \"#05668d\" , \"#028090\" , \"#00a896\" , \"#02c39a\" , \"#f0f3bd\" , \"#c9e4ca\" , \"#87bba2\" , \"#55828b\" , \"#3b6064\" , \"#FEE440\" , \"#ced4da\" , \"#868e96\" , \"#fa5252\" , \"#e64980\" , \"#be4bdb\" , \"#7950f2\" , \"#4c6ef5\" , \"#228be6\" , \"#15aabf\" , \"#12b886\" , \"#40c057\" , \"#82c91e\" , \"#fab005\" , \"#fd7e14\" , \"#A30000\" ], \"canvasBackground\" : [ \"#ffffff\" , \"#f8f9fa\" , \"#f1f3f5\" , \"#fff5f5\" , \"#fff0f6\" , \"#f8f0fc\" , \"#f3f0ff\" , \"#edf2ff\" , \"#e7f5ff\" , \"#e3fafc\" , \"#e6fcf5\" , \"#ebfbee\" , \"#f4fce3\" , \"#fff9db\" , \"#fff4e6\" ] } }, \"files\" : {} } %%","title":"Drawing"},{"location":"images/onepage/Scripts/Downloaded/Add%20Link%20and%20Open%20Page/","text":"/* Prompt for a file in the vault. Add a link above or below (based on settings) the selected element, to the selected file. If no file is selected then the script creates a new file following the default filename defined for excalidraw embeds. Creates empty markdown file by default, this can be changed to creating a drawing by default via settings. ```javascript */ if(!ea.verifyMinimumPluginVersion || !ea.verifyMinimumPluginVersion(\"1.5.21\")) { new Notice(\"This script requires a newer version of Excalidraw. Please install the latest version.\"); return; } const BLANK_DRAWING = [\"---\",\"\",\"excalidraw-plugin: parsed\",\"\",\"---\",\" \u26a0 Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. \u26a0 \",\"\",\"\",\"%%\",\"# Drawing\",\"\\x60\\x60\\x60json\",'{\"type\":\"excalidraw\",\"version\":2,\"source\":\"https://excalidraw.com\",\"elements\":[],\"appState\":{\"gridSize\":null,\"viewBackgroundColor\":\"#ffffff\"}}',\"\\x60\\x60\\x60\",\"%%\"].join(\"\\n\"); settings = ea.getScriptSettings(); //set default values on first run if(!settings[\"Link position\"]) { settings = { \"Link position\" : { value: \"below\", valueset: [\"above\",\"below\"], description: \"Add link below or above the selected object?\" }, \"Link font size\" : { value: 12 } }; ea.setScriptSettings(settings); } if(!settings[\"New document should be an Excalidraw drawing\"]) { settings = { \"New document should be an Excalidraw drawing\": { value: false, description: \"When adding a new document, should the new document be a blank markdown document (toggle == off) or a blank Excalidraw drawing (toggle=on)?\" }, ...settings }; ea.setScriptSettings(settings); } const below = settings[\"Link position\"].value === \"below\"; const newDocExcalidraw = settings[\"New document should be an Excalidraw drawing\"].value; const fontSize = Math.floor(settings[\"Link font size\"].value); elements = ea.getViewSelectedElements(); if(elements.length === 0) { new Notice(\"No selected elements\"); return; } const files = app.vault.getFiles() const filePaths = files.map((f)=>f.path); file = await utils.suggester(filePaths,files,\"Select file or press ESC to create a new document\"); alias = null; if(file) { alias = file.basename; } else { const prefix = ea.targetView.file.path.substring(0,ea.targetView.file.path.length-3); const timestamp = moment(Date.now()).format(ea.plugin.settings.drawingFilenameDateTime); file = await app.vault.create( ${prefix} ${timestamp}.md ,newDocExcalidraw?BLANK_DRAWING:\"\"); if(newDocExcalidraw) await new Promise(r => setTimeout(r, 100)); //wait for metadata cache to update, so file opens as excalidraw } const filepath = app.metadataCache.fileToLinktext(file,ea.targetView.file.path,true); ea.style.textAlign = \"center\"; ea.style.fontSize = fontSize; const textElementsIfAny = elements.filter(el=>el.type===\"text\"); if(textElementsIfAny.length>0) ea.style.fontFamily = textElementsIfAny[0].fontFamily; ea.style.strokeColor = elements[0].strokeColor; const box = ea.getBoundingBox(elements); const linkText = [[${filepath}${alias?\"|\"+alias:\"\"}]] ; const size = ea.measureText(alias?ea.plugin.settings.linkPrefix+alias:linkText); const id = ea.addText( box.topX+(box.width-size.width)/2, below ? box.topY + box.height + size.height : box.topY - size.height - 3, linkText ); ea.copyViewElementsToEAforEditing(elements); ea.addToGroup(elements.map((e)=>e.id).concat([id])); ea.addElementsToView(false,true,true); ea.openFileInNewOrAdjacentLeaf(file);","title":"Add Link and Open Page"},{"location":"images/onepage/Scripts/Downloaded/Box%20Selected%20Elements/","text":"/* This script will add an encapsulating box around the currently selected elements in Excalidraw. See documentation for more details: https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html ```javascript */ if(!ea.verifyMinimumPluginVersion || !ea.verifyMinimumPluginVersion(\"1.5.21\")) { new Notice(\"This script requires a newer version of Excalidraw. Please install the latest version.\"); return; } settings = ea.getScriptSettings(); //set default values on first run if(!settings[\"Default padding\"]) { settings = { \"Prompt for padding?\": true, \"Default padding\" : { value: 10, description: \"Padding between the bounding box of the selected elements, and the box the script creates\" } }; ea.setScriptSettings(settings); } let padding = settings[\"Default padding\"].value; if(settings[\"Prompt for padding?\"]) { padding = parseInt (await utils.inputPrompt(\"padding?\",\"number\",padding.toString())); } if(isNaN(padding)) { new Notice(\"The padding value provided is not a number\"); return; } elements = ea.getViewSelectedElements(); const box = ea.getBoundingBox(elements); color = ea .getExcalidrawAPI() .getAppState() .currentItemStrokeColor; //uncomment for random color: //color = '#'+(Math.random() 0xFFFFFF<<0).toString(16).padStart(6,\"0\"); ea.style.strokeColor = color; id = ea.addRect( box.topX - padding, box.topY - padding, box.width + 2 padding, box.height + 2*padding ); ea.copyViewElementsToEAforEditing(elements); ea.addToGroup([id].concat(elements.map((el)=>el.id))); ea.addElementsToView(false,false);","title":"Box Selected Elements"},{"location":"images/onepage/Scripts/Downloaded/Bullet%20Point/","text":"/* Download this file and save to your Obsidian Vault including the first line, or open it in \"Raw\" and copy the entire contents to Obsidian. This script will add a small circle to the top left of each text element in the selection and add the text and the \"bullet point\" into a group. See documentation for more details: https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html ```javascript */ elements = ea.getViewSelectedElements().filter((el)=>el.type===\"text\"); ea.copyViewElementsToEAforEditing(elements); const padding = 10; elements.forEach((el)=>{ ea.style.strokeColor = el.strokeColor; const size = el.fontSize/2; const ellipseId = ea.addEllipse( el.x-padding-size, el.y+size/2, size, size ); ea.addToGroup([el.id,ellipseId]); }); ea.addElementsToView();","title":"Bullet Point"},{"location":"images/onepage/Scripts/Downloaded/Convert%20freedraw%20to%20line/","text":"/* Convert selected freedraw objects into editable lines. This will allow you to adjust your drawings by dragging line points and will also allow you to select shape fill in case of enclosed lines. You can adjust conversion point density in settings. ```javascript */ if(!ea.verifyMinimumPluginVersion || !ea.verifyMinimumPluginVersion(\"1.5.21\")) { new Notice(\"This script requires a newer version of Excalidraw. Please install the latest version.\"); return; } settings = ea.getScriptSettings(); //set default values on first run if(!settings[\"Point density\"]) { settings = { \"Point density\" : { value: \"7:1\", valueset: [\"1:1\",\"2:1\",\"3:1\",\"4:1\",\"5:1\",\"6:1\",\"7:1\",\"8:1\",\"9:1\",\"10:1\",\"11:1\"], description: \"A freedraw object has many points. Converting freedraw to a line with too many points will result in an impractical object that is hard to edit. This setting sepcifies how many points from freedraw should be averaged to form a point on the line\" }, }; ea.setScriptSettings(settings); } const scale = settings[\"Point density\"].value; const setSize = parseInt(scale.substring(0,scale.indexOf(\":\"))); const elements = ea.getViewSelectedElements().filter(el=>el.type===\"freedraw\"); if(elements.length === 0) { new Notice(\"No freedraw object is selected\"); } ea.style.roughness=0; ea.style.strokeSharpness=\"round\"; elements.forEach((el)=>{ points = []; points.push(el.points[0]); for(i=1;i [original text](../../../../selected folder/original text.md) See documentation for more details: https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html ```javascript */ folders = new Set(); app.vault.getFiles().forEach((f)=> folders.add(f.path.substring(0,f.path.lastIndexOf(\"/\"))) ); f = Array.from(folders); folder = await utils.suggester(f,f); folder = folder??\"\"; //if exiting suggester with ESC folder = folder === \"\" ? folder : folder + \"/\"; elements = ea.getViewSelectedElements().filter((el)=>el.type===\"text\"); elements.forEach((el)=>{ el.rawText = \"[[\"+folder+el.rawText+\"|\"+el.rawText+\"]]\"; el.text = \"[[\"+folder+el.text+\"|\"+el.text+\"]]\"; el.originalText = \"[[\"+folder+el.originalText+\"|\"+el.originalText+\"]]\"; }) ea.copyViewElementsToEAforEditing(elements); ea.addElementsToView();","title":"Convert text to link with folder and alias"},{"location":"images/onepage/Scripts/Downloaded/Expand%20rectangles%20horizontally%20keep%20text%20centered/","text":"/* Download this file and save to your Obsidian Vault including the first line, or open it in \"Raw\" and copy the entire contents to Obsidian. This script expands the width of the selected rectangles until they are all the same width and keep the text centered. See documentation for more details: https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html ```javascript */ const elements = ea.getViewSelectedElements(); const topGroups = ea.getMaximumGroups(elements); const allIndividualArrows = ea.getMaximumGroups(ea.getViewElements()) .reduce((result, group) => (group.length === 1 && (group[0].type === 'arrow')) ? [...result, group[0]] : result, []); const groupWidths = topGroups .map((g) => { if(g.length === 1 && (g[0].type === 'arrow' || g[0].type === 'line')) { // ignore individual lines return { minLeft: 0, maxRight: 0 }; } return g.reduce( (pre, cur, i) => { if (i === 0) { return { minLeft: cur.x, maxRight: cur.x + cur.width, index: i, }; } else { return { minLeft: cur.x < pre.minLeft ? cur.x : pre.minLeft, maxRight: cur.x + cur.width > pre.maxRight ? cur.x + cur.width : pre.maxRight, index: i, }; } }, { minLeft: 0, maxRight: 0 } ); }) .map((r) => { r.width = r.maxRight - r.minLeft; return r; }); const maxGroupWidth = Math.max(...groupWidths.map((g) => g.width)); for (var i = 0; i < topGroups.length; i++) { const rects = topGroups[i] .filter((el) => el.type === \"rectangle\") .sort((lha, rha) => lha.x - rha.x); const texts = topGroups[i] .filter((el) => el.type === \"text\") .sort((lha, rha) => lha.x - rha.x); const groupWith = groupWidths[i].width; if (groupWith < maxGroupWidth) { const distance = maxGroupWidth - groupWith; const perRectDistance = distance / rects.length; const textsWithRectIndex = []; for (var j = 0; j < rects.length; j++) { const rect = rects[j]; const rectLeft = rect.x; const rectTop = rect.y; const rectRight = rect.x + rect.width; const rectBottom = rect.y + rect.height; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 const textsWithRect = texts.filter(text => text.x >= rectLeft && text.x <= rectRight && text.y >= rectTop && text.y <= rectBottom); textsWithRectIndex[j] = textsWithRect; } for (var j = 0; j < rects.length; j++) { const rect = rects[j]; rect.x = rect.x + perRectDistance * j - perRectDistance / 2; rect.width += perRectDistance; const textsWithRect = textsWithRectIndex[j]; if(textsWithRect) { for(const text of textsWithRect) { text.x = text.x + perRectDistance * j; } } // recalculate the position of the points const startBindingLines = allIndividualArrows.filter(el => (el.startBinding||{}).elementId === rect.id); for(startBindingLine of startBindingLines) { recalculateStartPointOfLine(startBindingLine, rect); } const endBindingLines = allIndividualArrows.filter(el => (el.endBinding||{}).elementId === rect.id); for(endBindingLine of endBindingLines) { recalculateEndPointOfLine(endBindingLine, rect); } } } } ea.copyViewElementsToEAforEditing(elements); await ea.addElementsToView(false, false); function recalculateStartPointOfLine(line, el) { const aX = el.x + el.width/2; const bX = line.x + line.points[1][0]; const aY = el.y + el.height/2; const bY = line.y + line.points[1][1]; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 line.startBinding.gap = 8; line.startBinding.focus = 0; const intersectA = ea.intersectElementWithLine( el, [bX, bY], [aX, aY], line.startBinding.gap ); if(intersectA.length > 0) { line.points[0] = [0, 0]; for(var i = 1; i 0) { line.points[line.points.length - 1] = [intersectA[0][0] - line.x, intersectA[0][1] - line.y]; } }","title":"Expand rectangles horizontally keep text centered"},{"location":"images/onepage/Scripts/Downloaded/Expand%20rectangles%20horizontally/","text":"/* Download this file and save to your Obsidian Vault including the first line, or open it in \"Raw\" and copy the entire contents to Obsidian. This script expands the width of the selected rectangles until they are all the same width. See documentation for more details: https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html ```javascript */ const elements = ea.getViewSelectedElements(); const topGroups = ea.getMaximumGroups(elements); const allIndividualArrows = ea.getMaximumGroups(ea.getViewElements()) .reduce((result, group) => (group.length === 1 && (group[0].type === 'arrow' || group[0].type === 'line')) ? [...result, group[0]] : result, []); const groupWidths = topGroups .map((g) => { if(g.length === 1 && (g[0].type === 'arrow' || g[0].type === 'line')) { // ignore individual lines return { minLeft: 0, maxRight: 0 }; } return g.reduce( (pre, cur, i) => { if (i === 0) { return { minLeft: cur.x, maxRight: cur.x + cur.width, index: i, }; } else { return { minLeft: cur.x < pre.minLeft ? cur.x : pre.minLeft, maxRight: cur.x + cur.width > pre.maxRight ? cur.x + cur.width : pre.maxRight, index: i, }; } }, { minLeft: 0, maxRight: 0 } ); }) .map((r) => { r.width = r.maxRight - r.minLeft; return r; }); const maxGroupWidth = Math.max(...groupWidths.map((g) => g.width)); for (var i = 0; i < topGroups.length; i++) { const rects = topGroups[i] .filter((el) => el.type === \"rectangle\") .sort((lha, rha) => lha.x - rha.x); const groupWith = groupWidths[i].width; if (groupWith < maxGroupWidth) { const distance = maxGroupWidth - groupWith; const perRectDistance = distance / rects.length; for (var j = 0; j < rects.length; j++) { const rect = rects[j]; rect.x = rect.x + perRectDistance * j; rect.width += perRectDistance; 1 2 3 4 5 6 7 8 9 10 11 // recalculate the position of the points const startBindingLines = allIndividualArrows.filter(el => (el.startBinding||{}).elementId === rect.id); for(startBindingLine of startBindingLines) { recalculateStartPointOfLine(startBindingLine, rect); } const endBindingLines = allIndividualArrows.filter(el => (el.endBinding||{}).elementId === rect.id); for(endBindingLine of endBindingLines) { recalculateEndPointOfLine(endBindingLine, rect); } } } } ea.copyViewElementsToEAforEditing(elements); await ea.addElementsToView(false, false); function recalculateStartPointOfLine(line, el) { const aX = el.x + el.width/2; const bX = line.x + line.points[1][0]; const aY = el.y + el.height/2; const bY = line.y + line.points[1][1]; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 line.startBinding.gap = 8; line.startBinding.focus = 0; const intersectA = ea.intersectElementWithLine( el, [bX, bY], [aX, aY], line.startBinding.gap ); if(intersectA.length > 0) { line.points[0] = [0, 0]; for(var i = 1; i 0) { line.points[line.points.length - 1] = [intersectA[0][0] - line.x, intersectA[0][1] - line.y]; } }","title":"Expand rectangles horizontally"},{"location":"images/onepage/Scripts/Downloaded/Expand%20rectangles%20vertically%20keep%20text%20centered/","text":"/* Download this file and save to your Obsidian Vault including the first line, or open it in \"Raw\" and copy the entire contents to Obsidian. This script expands the height of the selected rectangles until they are all the same height and keep the text centered. See documentation for more details: https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html ```javascript */ const elements = ea.getViewSelectedElements(); const topGroups = ea.getMaximumGroups(elements); const allIndividualArrows = ea.getMaximumGroups(ea.getViewElements()) .reduce((result, group) => (group.length === 1 && (group[0].type === 'arrow' || group[0].type === 'line')) ? [...result, group[0]] : result, []); const groupHeights = topGroups .map((g) => { if(g.length === 1 && (g[0].type === 'arrow' || g[0].type === 'line')) { // ignore individual lines return { minTop: 0, maxBottom: 0 }; } return g.reduce( (pre, cur, i) => { if (i === 0) { return { minTop: cur.y, maxBottom: cur.y + cur.height, index: i, }; } else { return { minTop: cur.y < pre.minTop ? cur.y : pre.minTop, maxBottom: cur.y + cur.height > pre.maxBottom ? cur.y + cur.height : pre.maxBottom, index: i, }; } }, { minTop: 0, maxBottom: 0 } ); }) .map((r) => { r.height = r.maxBottom - r.minTop; return r; }); const maxGroupHeight = Math.max(...groupHeights.map((g) => g.height)); for (var i = 0; i < topGroups.length; i++) { const rects = topGroups[i] .filter((el) => el.type === \"rectangle\") .sort((lha, rha) => lha.y - rha.y); const texts = topGroups[i] .filter((el) => el.type === \"text\") .sort((lha, rha) => lha.y - rha.y); const groupWith = groupHeights[i].height; if (groupWith < maxGroupHeight) { const distance = maxGroupHeight - groupWith; const perRectDistance = distance / rects.length; const textsWithRectIndex = []; for (var j = 0; j < rects.length; j++) { const rect = rects[j]; const rectLeft = rect.x; const rectTop = rect.y; const rectRight = rect.x + rect.width; const rectBottom = rect.y + rect.height; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 const textsWithRect = texts.filter(text => text.x >= rectLeft && text.x <= rectRight && text.y >= rectTop && text.y <= rectBottom); textsWithRectIndex[j] = textsWithRect; } for (var j = 0; j < rects.length; j++) { const rect = rects[j]; rect.y = rect.y + perRectDistance * j - perRectDistance / 2; rect.height += perRectDistance; const textsWithRect = textsWithRectIndex[j]; if(textsWithRect) { for(const text of textsWithRect) { text.y = text.y + perRectDistance * j; } } // recalculate the position of the points const startBindingLines = allIndividualArrows.filter(el => (el.startBinding||{}).elementId === rect.id); for(startBindingLine of startBindingLines) { recalculateStartPointOfLine(startBindingLine, rect); } const endBindingLines = allIndividualArrows.filter(el => (el.endBinding||{}).elementId === rect.id); for(endBindingLine of endBindingLines) { recalculateEndPointOfLine(endBindingLine, rect); } } } } ea.copyViewElementsToEAforEditing(elements); await ea.addElementsToView(false, false); function recalculateStartPointOfLine(line, el) { const aX = el.x + el.width/2; const bX = line.x + line.points[1][0]; const aY = el.y + el.height/2; const bY = line.y + line.points[1][1]; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 line.startBinding.gap = 8; line.startBinding.focus = 0; const intersectA = ea.intersectElementWithLine( el, [bX, bY], [aX, aY], line.startBinding.gap ); if(intersectA.length > 0) { line.points[0] = [0, 0]; for(var i = 1; i 0) { line.points[line.points.length - 1] = [intersectA[0][0] - line.x, intersectA[0][1] - line.y]; } }","title":"Expand rectangles vertically keep text centered"},{"location":"images/onepage/Scripts/Downloaded/Expand%20rectangles%20vertically/","text":"/* Download this file and save to your Obsidian Vault including the first line, or open it in \"Raw\" and copy the entire contents to Obsidian. This script expands the height of the selected rectangles until they are all the same height. ```javascript */ const elements = ea.getViewSelectedElements(); const topGroups = ea.getMaximumGroups(elements); const allLines = ea.getViewElements().filter(el => el.type === 'arrow' || el.type === 'line'); const allIndividualArrows = ea.getMaximumGroups(ea.getViewElements()) .reduce((result, group) => (group.length === 1 && (group[0].type === 'arrow' || group[0].type === 'line')) ? [...result, group[0]] : result, []); const groupHeights = topGroups .map((g) => { if(g.length === 1 && (g[0].type === 'arrow' || g[0].type === 'line')) { // ignore individual lines return { minTop: 0, maxBottom: 0 }; } return g.reduce( (pre, cur, i) => { if (i === 0) { return { minTop: cur.y, maxBottom: cur.y + cur.height, index: i, }; } else { return { minTop: cur.y < pre.minTop ? cur.y : pre.minTop, maxBottom: cur.y + cur.height > pre.maxBottom ? cur.y + cur.height : pre.maxBottom, index: i, }; } }, { minTop: 0, maxBottom: 0 } ); }) .map((r) => { r.height = r.maxBottom - r.minTop; return r; }); const maxGroupHeight = Math.max(...groupHeights.map((g) => g.height)); for (var i = 0; i < topGroups.length; i++) { const rects = topGroups[i] .filter((el) => el.type === \"rectangle\") .sort((lha, rha) => lha.y - rha.y); const groupWidth = groupHeights[i].height; if (groupWidth < maxGroupHeight) { const distance = maxGroupHeight - groupWidth; const perRectDistance = distance / rects.length; for (var j = 0; j < rects.length; j++) { const rect = rects[j]; rect.y = rect.y + perRectDistance * j; rect.height += perRectDistance; 1 2 3 4 5 6 7 8 9 10 11 // recalculate the position of the points const startBindingLines = allIndividualArrows.filter(el => (el.startBinding||{}).elementId === rect.id); for(startBindingLine of startBindingLines) { recalculateStartPointOfLine(startBindingLine, rect); } const endBindingLines = allIndividualArrows.filter(el => (el.endBinding||{}).elementId === rect.id); for(endBindingLine of endBindingLines) { recalculateEndPointOfLine(endBindingLine, rect); } } } } ea.copyViewElementsToEAforEditing(elements); await ea.addElementsToView(false, false); function recalculateStartPointOfLine(line, el) { const aX = el.x + el.width/2; const bX = line.x + line.points[1][0]; const aY = el.y + el.height/2; const bY = line.y + line.points[1][1]; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 line.startBinding.gap = 8; line.startBinding.focus = 0; const intersectA = ea.intersectElementWithLine( el, [bX, bY], [aX, aY], line.startBinding.gap ); if(intersectA.length > 0) { line.points[0] = [0, 0]; for(var i = 1; i 0) { line.points[line.points.length - 1] = [intersectA[0][0] - line.x, intersectA[0][1] - line.y]; } }","title":"Expand rectangles vertically"},{"location":"images/onepage/Scripts/Downloaded/Fixed%20spacing/","text":"/* Download this file and save to your Obsidian Vault including the first line, or open it in \"Raw\" and copy the entire contents to Obsidian. The script arranges the selected elements horizontally with a fixed spacing. When we create an architecture diagram or mind map, we often need to arrange a large number of elements in a fixed spacing. Fixed spacing and Fixed vertical Distance scripts can save us a lot of time. ```javascript */ if(!ea.verifyMinimumPluginVersion || !ea.verifyMinimumPluginVersion(\"1.5.21\")) { new Notice(\"This script requires a newer version of Excalidraw. Please install the latest version.\"); return; } settings = ea.getScriptSettings(); //set default values on first run if(!settings[\"Default spacing\"]) { settings = { \"Prompt for spacing?\": true, \"Default spacing\" : { value: 10, description: \"Fixed horizontal spacing between elements\" }, \"Remember last spacing?\": false }; ea.setScriptSettings(settings); } let spacingStr = settings[\"Default spacing\"].value.toString(); const rememberLastSpacing = settings[\"Remember last spacing?\"]; if(settings[\"Prompt for spacing?\"]) { spacingStr = await utils.inputPrompt(\"spacing?\",\"number\",spacingStr); } const spacing = parseInt(spacingStr); if(isNaN(spacing)) { return; } if(rememberLastSpacing) { settings[\"Default spacing\"].value = spacing; ea.setScriptSettings(settings); } const elements=ea.getViewSelectedElements(); const topGroups = ea.getMaximumGroups(elements) .filter(els => !(els.length === 1 && els[0].type ===\"arrow\")) // ignore individual arrows .filter(els => !(els.length === 1 && (els[0].containerId))); // ignore text in stickynote const groups = topGroups.sort((lha,rha) => lha[0].x - rha[0].x); for(var i=0; i 0) { const preGroup = groups[i-1]; const curGroup = groups[i]; 1 2 3 4 5 6 7 8 const preRight = Math.max(...preGroup.map(el => el.x + el.width)); const curLeft = Math.min(...curGroup.map(el => el.x)); const distance = curLeft - preRight - spacing; for(const curEl of curGroup) { curEl.x = curEl.x - distance; } } } ea.copyViewElementsToEAforEditing(elements); await ea.addElementsToView(false, false);","title":"Fixed spacing"},{"location":"images/onepage/Scripts/Downloaded/Fixed%20vertical%20distance/","text":"/* Download this file and save to your Obsidian Vault including the first line, or open it in \"Raw\" and copy the entire contents to Obsidian. The script arranges the selected elements vertically with a fixed spacing. When we create an architecture diagram or mind map, we often need to arrange a large number of elements in a fixed spacing. Fixed spacing and Fixed vertical Distance scripts can save us a lot of time. ```javascript */ if(!ea.verifyMinimumPluginVersion || !ea.verifyMinimumPluginVersion(\"1.5.21\")) { new Notice(\"This script requires a newer version of Excalidraw. Please install the latest version.\"); return; } settings = ea.getScriptSettings(); //set default values on first run if(!settings[\"Default spacing\"]) { settings = { \"Prompt for spacing?\": true, \"Default spacing\" : { value: 10, description: \"Fixed vertical spacing between elements\" }, \"Remember last spacing?\": false }; ea.setScriptSettings(settings); } let spacingStr = settings[\"Default spacing\"].value.toString(); const rememberLastSpacing = settings[\"Remember last spacing?\"]; if(settings[\"Prompt for spacing?\"]) { spacingStr = await utils.inputPrompt(\"spacing?\",\"number\",spacingStr); } const spacing = parseInt(spacingStr); if(isNaN(spacing)) { return; } if(rememberLastSpacing) { settings[\"Default spacing\"].value = spacing; ea.setScriptSettings(settings); } const elements=ea.getViewSelectedElements(); const topGroups = ea.getMaximumGroups(elements) .filter(els => !(els.length === 1 && els[0].type ===\"arrow\")) // ignore individual arrows .filter(els => !(els.length === 1 && (els[0].containerId))); // ignore text in stickynote const groups = topGroups.sort((lha,rha) => lha[0].y - rha[0].y); for(var i=0; i 0) { const preGroup = groups[i-1]; const curGroup = groups[i]; 1 2 3 4 5 6 7 8 const preBottom = Math.max(...preGroup.map(el => el.y + el.height)); const curTop = Math.min(...curGroup.map(el => el.y)); const distance = curTop - preBottom - spacing; for(const curEl of curGroup) { curEl.y = curEl.y - distance; } } } ea.copyViewElementsToEAforEditing(elements); await ea.addElementsToView(false, false);","title":"Fixed vertical distance"},{"location":"images/onepage/Scripts/Downloaded/Modify%20stroke%20width%20of%20selected%20elements/","text":"/* Download this file and save to your Obsidian Vault including the first line, or open it in \"Raw\" and copy the entire contents to Obsidian. This script will set the stroke width of selected elements. This is helpful, for example, when you scale freedraw sketches and want to reduce or increase their line width. See documentation for more details: https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html ```javascript */ let width = (ea.getViewSelectedElement().strokeWidth??1).toString(); width = await utils.inputPrompt(\"Width?\",\"number\",width); const elements=ea.getViewSelectedElements(); ea.copyViewElementsToEAforEditing(elements); ea.getElements().forEach((el)=>el.strokeWidth=width); ea.addElementsToView();","title":"Modify stroke width of selected elements"},{"location":"images/onepage/Scripts/Downloaded/Normalize%20Selected%20Arrows/","text":"/* This script will reset the start and end positions of the selected arrows. The arrow will point to the center of the connected box and will have a gap of 8px from the box. Tips: If you are drawing a flowchart, you can use Normalize Selected Arrows script to correct the position of the start and end points of the arrows, then use Elbow connectors script, and you will get the perfect connecting line! ```javascript */ if(!ea.verifyMinimumPluginVersion || !ea.verifyMinimumPluginVersion(\"1.5.21\")) { new Notice(\"This script requires a newer version of Excalidraw. Please install the latest version.\"); return; } settings = ea.getScriptSettings(); //set default values on first run if(!settings[\"Gap\"]) { settings = { \"Gap\" : { value: 8, description: \"The value of the gap between the connection line and the element, which must be greater than 0. If you want the connector to be next to the element, set it to 1.\" } }; ea.setScriptSettings(settings); } let gapValue = settings[\"Gap\"].value; const selectedIndividualArrows = ea.getMaximumGroups(ea.getViewSelectedElements()) .reduce((result, group) => (group.length === 1 && (group[0].type === 'arrow' || group[0].type === 'line')) ? [...result, group[0]] : result, []); const allElements = ea.getViewElements(); for(const arrow of selectedIndividualArrows) { const startBindingEl = allElements.filter(el => el.id === (arrow.startBinding||{}).elementId)[0]; const endBindingEl = allElements.filter(el => el.id === (arrow.endBinding||{}).elementId)[0]; 1 2 3 4 5 6 if(startBindingEl) { recalculateStartPointOfLine(arrow, startBindingEl, endBindingEl, gapValue); } if(endBindingEl) { recalculateEndPointOfLine(arrow, endBindingEl, startBindingEl, gapValue); } } ea.copyViewElementsToEAforEditing(selectedIndividualArrows); await ea.addElementsToView(false,false); function recalculateStartPointOfLine(line, el, elB, gapValue) { const aX = el.x + el.width/2; const bX = (line.points.length <=2 && elB) ? elB.x + elB.width/2 : line.x + line.points[1][0]; const aY = el.y + el.height/2; const bY = (line.points.length <=2 && elB) ? elB.y + elB.height/2 : line.y + line.points[1][1]; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 line.startBinding.gap = gapValue; line.startBinding.focus = 0; const intersectA = ea.intersectElementWithLine( el, [bX, bY], [aX, aY], line.startBinding.gap ); if(intersectA.length > 0) { line.points[0] = [0, 0]; for(var i = 1; i 0) { line.points[line.points.length - 1] = [intersectA[0][0] - line.x, intersectA[0][1] - line.y]; } }","title":"Normalize Selected Arrows"},{"location":"images/onepage/Scripts/Downloaded/Organic%20Line/","text":"/* Converts selected freedraw lines such that pencil pressure will decrease from maximum to minimum from the beginning of the line to its end. The resulting line is placed at the back of the layers, under all other items. Helpful when drawing organic mindmaps. ```javascript */ let elements = ea.getViewSelectedElements().filter((el)=>[\"freedraw\",\"line\",\"arrow\"].includes(el.type)); if(elements.length === 0) { elements = ea.getViewSelectedElements(); const len = elements.length; if(len === 0 || [\"freedraw\",\"line\",\"arrow\"].includes(elements[len].type)) { return; } elements = [elements[len]]; } elements.forEach((el)=>{ el.simulatePressure = false; el.type = \"freedraw\"; el.pressures = []; const len = el.points.length; for(i=0;i ea.moveViewElementToZIndex(el.id,0));","title":"Organic Line"},{"location":"images/onepage/Scripts/Downloaded/Repeat%20Elements/","text":"/* Download this file and save to your Obsidian Vault including the first line, or open it in \"Raw\" and copy the entire contents to Obsidian. This script will detect the difference between 2 selected elements, including position, size, angle, stroke and background color, and create several elements that repeat these differences based on the number of repetitions entered by the user. See documentation for more details: https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html ```javascript */ let repeatNum = parseInt(await utils.inputPrompt(\"repeat times?\",\"number\",\"5\")); if(!repeatNum) { new Notice(\"Please enter a number.\"); return; } const selectedElements = ea.getViewSelectedElements().sort((lha,rha) => lha.x === rha.x? (lha.y === rha.y? (lha.width === rha.width? (lha.height - rha.height) : lha.width - rha.width) : lha.y - rha.y) : lha.x - rha.x); if(selectedElements.length !== 2) { new Notice(\"Please select 2 elements.\"); return; } if(selectedElements[0].type !== selectedElements[1].type) { new Notice(\"The selected elements must be of the same type.\"); return; } const xDistance = selectedElements[1].x - selectedElements[0].x; const yDistance = selectedElements[1].y - selectedElements[0].y; const widthDistance = selectedElements[1].width - selectedElements[0].width; const heightDistance = selectedElements[1].height - selectedElements[0].height; const angleDistance = selectedElements[1].angle - selectedElements[0].angle; const bgColor1 = ea.colorNameToHex(selectedElements[0].backgroundColor); const rgbBgColor1 = parseColorString(bgColor1); const bgColor2 = ea.colorNameToHex(selectedElements[1].backgroundColor); const rgbBgColor2 = parseColorString(bgColor2); let bgHDistance = 0; let bgSDistance = 0; let bgLDistance = 0; if(rgbBgColor1 && rgbBgColor2) { const bgHsl1 = ea.rgbToHsl([rgbBgColor1.value[0], rgbBgColor1.value[1], rgbBgColor1.value[2]]); const bgHsl2 = ea.rgbToHsl([rgbBgColor2.value[0], rgbBgColor2.value[1], rgbBgColor2.value[2]]); 1 2 3 bgHDistance = bgHsl2[0] - bgHsl1[0]; bgSDistance = bgHsl2[1] - bgHsl1[1]; bgLDistance = bgHsl2[2] - bgHsl1[2]; } const strokeColor1 = ea.colorNameToHex(selectedElements[0].strokeColor); const rgbStrokeColor1 = parseColorString(strokeColor1); const strokeColor2 = ea.colorNameToHex(selectedElements[1].strokeColor); const rgbStrokeColor2 = parseColorString(strokeColor2); let strokeHDistance = 0; let strokeSDistance = 0; let strokeLDistance = 0; if(rgbStrokeColor1 && rgbStrokeColor2) { const strokeHsl1 = ea.rgbToHsl([rgbStrokeColor1.value[0], rgbStrokeColor1.value[1], rgbStrokeColor1.value[2]]); const strokeHsl2 = ea.rgbToHsl([rgbStrokeColor2.value[0], rgbStrokeColor2.value[1], rgbStrokeColor2.value[2]]); 1 2 3 strokeHDistance = strokeHsl2[0] - strokeHsl1[0]; strokeSDistance = strokeHsl2[1] - strokeHsl1[1]; strokeLDistance = strokeHsl2[2] - strokeHsl1[2]; } ea.copyViewElementsToEAforEditing(selectedElements); for(let i=0; i = 0 && newHeight >= 0) { if(newEl.type === 'arrow' || newEl.type === 'line' || newEl.type === 'freedraw') { const minX = Math.min(...newEl.points.map(pt => pt[0])); const minY = Math.min(...newEl.points.map(pt => pt[1])); for(let j = 0; j < newEl.points.length; j++) { if(newEl.points[j][0] > minX) { newEl.points[j][0] = newEl.points[j][0] + ((newEl.points[j][0] - minX) / originWidth) * (newWidth - originWidth); } if(newEl.points[j][1] > minY) { newEl.points[j][1] = newEl.points[j][1] + ((newEl.points[j][1] - minY) / originHeight) * (newHeight - originHeight); } } } else { newEl.width = newWidth; newEl.height = newHeight; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 if(rgbBgColor1 && rgbBgColor2) { const bgHsl2 = ea.rgbToHsl([rgbBgColor2.value[0], rgbBgColor2.value[1], rgbBgColor2.value[2]]); const newBgH = bgHsl2[0] + bgHDistance * (i + 1); const newBgS = bgHsl2[1] + bgSDistance * (i + 1); const newBgL = bgHsl2[2] + bgLDistance * (i + 1); if(newBgH >= 0 && newBgH <= 360 && newBgS >= 0 && newBgS <= 100 && newBgL >= 0 && newBgL <= 100) { const newBgRgb = ea.hslToRgb([newBgH, newBgS, newBgL]); newEl.backgroundColor = rgbColorToString(newBgRgb, rgbBgColor1.model); } } if(rgbStrokeColor1 && rgbStrokeColor2) { const strokeHsl2 = ea.rgbToHsl([rgbStrokeColor2.value[0], rgbStrokeColor2.value[1], rgbStrokeColor2.value[2]]); const newStrokeH = strokeHsl2[0] + strokeHDistance * (i + 1); const newStrokeS = strokeHsl2[1] + strokeSDistance * (i + 1); const newStrokeL = strokeHsl2[2] + strokeLDistance * (i + 1); if(newStrokeH >= 0 && newStrokeH <= 360 && newStrokeS >= 0 && newStrokeS <= 100 && newStrokeL >= 0 && newStrokeL <= 100) { const newStrokeRgb = ea.hslToRgb([newStrokeH, newStrokeS, newStrokeL]); newEl.strokeColor = rgbColorToString(newStrokeRgb, rgbStrokeColor1.model); } } } await ea.addElementsToView(false, false, true); function parseColorString(string) { var prefix = string.substring(0, 3).toLowerCase(); var val; var model; switch (prefix) { case 'hsl': val = ea.hslToRgb(parseHslColorString(string)); model = 'hsl'; break; case 'hwb': val = hwbToRgb(parseHwbColorString(string)); model = 'hwb'; break; default: val = parseRgbColorString(string); model = 'rgb'; break; } 1 2 3 4 5 if (!val) { return null; } return {model: model, value: val}; }; function parseRgbColorString(string) { if (!string) { return null; } var colorNames={}; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 var abbr = /^#([a-f0-9]{3,4})$/i; var hex = /^#([a-f0-9]{6})([a-f0-9]{2})?$/i; var rgba = /^rgba?\\(\\s*([+-]?\\d+)(?=[\\s,])\\s*(?:,\\s*)?([+-]?\\d+)(?=[\\s,])\\s*(?:,\\s*)?([+-]?\\d+)\\s*(?:[,|\\/]\\s*([+-]?[\\d\\.]+)(%?)\\s*)?\\)$/; var per = /^rgba?\\(\\s*([+-]?[\\d\\.]+)\\%\\s*,?\\s*([+-]?[\\d\\.]+)\\%\\s*,?\\s*([+-]?[\\d\\.]+)\\%\\s*(?:[,|\\/]\\s*([+-]?[\\d\\.]+)(%?)\\s*)?\\)$/; var keyword = /^(\\w+)$/; var rgb = [0, 0, 0, 1]; var match; var i; var hexAlpha; if (match = string.match(hex)) { hexAlpha = match[2]; match = match[1]; for (i = 0; i < 3; i++) { var i2 = i * 2; rgb[i] = parseInt(match.slice(i2, i2 + 2), 16); } if (hexAlpha) { rgb[3] = parseInt(hexAlpha, 16) / 255; } } else if (match = string.match(abbr)) { match = match[1]; hexAlpha = match[3]; for (i = 0; i < 3; i++) { rgb[i] = parseInt(match[i] + match[i], 16); } if (hexAlpha) { rgb[3] = parseInt(hexAlpha + hexAlpha, 16) / 255; } } else if (match = string.match(rgba)) { for (i = 0; i < 3; i++) { rgb[i] = parseInt(match[i + 1], 0); } if (match[4]) { if (match[5]) { rgb[3] = parseFloat(match[4]) * 0.01; } else { rgb[3] = parseFloat(match[4]); } } } else if (match = string.match(per)) { for (i = 0; i < 3; i++) { rgb[i] = Math.round(parseFloat(match[i + 1]) * 2.55); } if (match[4]) { if (match[5]) { rgb[3] = parseFloat(match[4]) * 0.01; } else { rgb[3] = parseFloat(match[4]); } } } else if (match = string.match(keyword)) { if (match[1] === 'transparent') { return [0, 0, 0, 0]; } if (!hasOwnProperty.call(colorNames, match[1])) { return null; } rgb = colorNames[match[1]]; rgb[3] = 1; return rgb; } else { return null; } for (i = 0; i < 3; i++) { rgb[i] = clamp(rgb[i], 0, 255); } rgb[3] = clamp(rgb[3], 0, 1); return rgb; } function parseHslColorString(string) { if (!string) { return null; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 var hsl = /^hsla?\\(\\s*([+-]?(?:\\d{0,3}\\.)?\\d+)(?:deg)?\\s*,?\\s*([+-]?[\\d\\.]+)%\\s*,?\\s*([+-]?[\\d\\.]+)%\\s*(?:[,|\\/]\\s*([+-]?(?=\\.\\d|\\d)(?:0|[1-9]\\d*)?(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)\\s*)?\\)$/; var match = string.match(hsl); if (match) { var alpha = parseFloat(match[4]); var h = ((parseFloat(match[1]) % 360) + 360) % 360; var s = clamp(parseFloat(match[2]), 0, 100); var l = clamp(parseFloat(match[3]), 0, 100); var a = clamp(isNaN(alpha) ? 1 : alpha, 0, 1); return [h, s, l, a]; } return null; } function parseHwbColorString(string) { if (!string) { return null; } 1 2 3 4 5 6 7 8 9 10 11 12 13 var hwb = /^hwb\\(\\s*([+-]?\\d{0,3}(?:\\.\\d+)?)(?:deg)?\\s*,\\s*([+-]?[\\d\\.]+)%\\s*,\\s*([+-]?[\\d\\.]+)%\\s*(?:,\\s*([+-]?(?=\\.\\d|\\d)(?:0|[1-9]\\d*)?(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)\\s*)?\\)$/; var match = string.match(hwb); if (match) { var alpha = parseFloat(match[4]); var h = ((parseFloat(match[1]) % 360) + 360) % 360; var w = clamp(parseFloat(match[2]), 0, 100); var b = clamp(parseFloat(match[3]), 0, 100); var a = clamp(isNaN(alpha) ? 1 : alpha, 0, 1); return [h, w, b, a]; } return null; } function rgbColorToString(color, model) { switch (model) { case 'hsl': return rgbColorToHslString(color); case 'hwb': return rgbColorToHwbString(color); default: return ea.rgbToHexString(color); } } function rgbColorToHslString(rgb) { var hsl = ea.rgbToHsl(rgb); return 'hsl(' + hsl[0] + ', ' + hsl[1] + '%, ' + hsl[2] + '%)' }; function rgbColorToHwbString(rgb) { var hwb = rgbToHwb(rgb); 1 return 'hwb(' + hwb[0] + ', ' + hwb[1] + '%, ' + hwb[2] + '%)'; }; function rgbToHwb(rgb) { const r = rgb[0]; const g = rgb[1]; let b = rgb[2]; const h = convert.rgb.hsl(rgb)[0]; const w = 1 / 255 * Math.min(r, Math.min(g, b)); 1 2 3 b = 1 - 1 / 255 * Math.max(r, Math.max(g, b)); return [h, w * 100, b * 100]; }; function hwbToRgb(hwb) { const h = hwb[0] / 360; let wh = hwb[1] / 100; let bl = hwb[2] / 100; const ratio = wh + bl; let f; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 // Wh + bl cant be > 1 if (ratio > 1) { wh /= ratio; bl /= ratio; } const i = Math.floor(6 * h); const v = 1 - bl; f = 6 * h - i; if ((i & 0x01) !== 0) { f = 1 - f; } const n = wh + f * (v - wh); let r; let g; let b; switch (i) { default: case 6: case 0: r = v; g = n; b = wh; break; case 1: r = n; g = v; b = wh; break; case 2: r = wh; g = v; b = n; break; case 3: r = wh; g = n; b = v; break; case 4: r = n; g = wh; b = v; break; case 5: r = v; g = wh; b = n; break; } return [r * 255, g * 255, b * 255]; } function clamp(num, min, max) { return Math.min(Math.max(min, num), max); }","title":"Repeat Elements"},{"location":"images/onepage/Scripts/Downloaded/Set%20Dimensions/","text":"/* Download this file and save to your Obsidian Vault including the first line, or open it in \"Raw\" and copy the entire contents to Obsidian. Currently there is no way to specify the exact location and size of objects in Excalidraw. You can bridge this gap with the following simple script. See documentation for more details: https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html ```javascript */ const elements = ea.getViewSelectedElements(); if(elements.length === 0) return; const el = ea.getLargestElement(elements); const sizeIn = [ Math.round(el.x), Math.round(el.y), Math.round(el.width), Math.round(el.height) ].join(\",\"); let res = await utils.inputPrompt(\"x,y,width,height?\",null,sizeIn); res = res.split(\",\"); if(res.length !== 4) return; let size = []; for (v of res) { const i = parseInt(v); if(isNaN(i)) return; size.push(i); } el.x = size[0]; el.y = size[1]; el.width = size[2]; el.height = size[3]; ea.copyViewElementsToEAforEditing([el]); ea.addElementsToView(false,false);","title":"Set Dimensions"},{"location":"images/onepage/Scripts/Downloaded/Set%20Text%20Alignment/","text":"/* Sets text alignment of text block (cetner, right, left). Useful if you want to set a keyboard shortcut for selecting text alignment. See documentation for more details: https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html ```javascript */ elements = ea.getViewSelectedElements().filter((el)=>el.type===\"text\"); if(elements.length===0) return; let align = [\"left\",\"right\",\"center\"]; align = await utils.suggester(align,align); elements.forEach((el)=>el.textAlign = align); ea.copyViewElementsToEAforEditing(elements); ea.addElementsToView(false,false);","title":"Set Text Alignment"},{"location":"images/onepage/Scripts/Downloaded/Split%20text%20by%20lines/","text":"/* requires Excalidraw 1.5.1 or higher Download this file and save to your Obsidian Vault including the first line, or open it in \"Raw\" and copy the entire contents to Obsidian. Split lines of text into separate text elements for easier reorganization See documentation for more details: https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html ```javascript / elements = ea.getViewSelectedElements().filter((el)=>el.type===\"text\"); elements.forEach((el)=>{ ea.style.strokeColor = el.strokeColor; ea.style.fontFamily = el.fontFamily; ea.style.fontSize = el.fontSize; const text = el.text.split(\"\\n\"); for(i=0;iel.type===\"text\"); elements.forEach((el)=>{ ea.style.strokeColor = el.strokeColor; ea.style.fontFamily = el.fontFamily; ea.style.fontSize = el.fontSize; const text = el.text.split(\"\\n\"); for(i=0;i { app.workspace.off(EVENT,window.brainGraphEventHandler); if(isBoolean(window.excalidrawView?.linksAlwaysOpenInANewPane)) { window.excalidrawView.linksAlwaysOpenInANewPane = false; } const ea = ExcalidrawAutomate; ea.setView(window.excalidrawView); if(ea.targetView?.excalidrawAPI) { try { ea.targetView.semaphores.saving = false; ea.getExcalidrawAPI().updateScene({appState:{viewModeEnabled:false}}); } catch {} } delete window.excalidrawView; delete window.excalidrawFile; delete window.lastfilePath; new Notice(\"Brain Graph Off\") setTimeout(()=>delete window.brainGraphEventHandler); } //Turn off event handler if it is already running if(window.brainGraphEventHandler) { removeEventHandler(); return; } //------------------------------------------------------- // Load Settings //------------------------------------------------------- let saveSettings = false; settings = ea.getScriptSettings(); //set default values on first run if(!settings[\"Max number of nodes/domain\"]) { settings = { \"Confirmation prompt at startup\": { value: true, description: \"Prompt me to confirm starting of the script because \" + \"it will overwrite the current active drawing. \" + \"You can disable this warning by turning off this switch\" }, \"Max number of nodes/domain\": { value: 30, description: \"Maximum number of items to show in each domain: parents, children, siblings, jumps.\" }, \"Infer non-Breadcrumbs links\": { value: true, description: \"Links on the page are children, backlinks to the page are \" + \"parents. Breadcrumbs take priority. Inferred nodes have a dashed border.\" }, \"Hide attachments\": { value: true, description: \"Hide attachments. Will only have an effect if Infer non-Breadcrumbs links is turned on.\" }, \"Font family\": { value: \"Code\", valueset: [\"Hand-drawn\",\"Normal\",\"Code\",\"Fourth (custom) Font\"] }, \"Stroke roughness\": { value: \"Architect\", valueset: [\"Architect\", \"Artist\", \"Cartoonist\"] }, \"Rectangle stroke sharpness\": { value: \"round\", valueset: [\"sharp\", \"round\"] }, \"Central font size\": { value: 30, description: \"Font size of the central node\" }, \"Font size\": { value: 20, description: \"Font size of jumps, children and parents\" }, \"Siblings font size\": { value: 15, description: \"Font size of siblings\" }, \"Max label length\": { value: 30, description: \"Maximum number of characters to display from node title. Longer nodes will end with '...'\" }, \"Padding\": { value: 10, description: \"Padding of the node rectangle\" }, \"Gate offset\": { value: 15, description: \"The offset to the left and right of the parent and child gates.\" }, \"Gate radius\": { value: 5, description: \"The radius of the 3 small circles (alias: gates) serving as connection points for nodes\" }, \"Canvas color\": { value: \"hsl(208, 80%, 23%)\", description: \"Any legal HTML color (#000000, rgb, color-name, etc.).\" }, \"Gate color\": { value: \"white\", description: \"Any legal HTML color (#000000, rgb, color-name, etc.).\" }, \"Link color\": { value: \"hsl(0, 0%, 41%)\", description: \"Any legal HTML color (#000000, rgb, color-name, etc.).\" }, \"Central-node background color\": { value: \"#C49A13\", description: \"Any legal HTML color (#000000, rgb, color-name, etc.).\" }, \"Central-node color\": { value: \"black\", description: \"Any legal HTML color (#000000, rgb, color-name, etc.).\" }, \"Breadcrumbs-node background color\": { value: \"rgba(0,0,0,0.4)\", description: \"Any legal HTML color (#000000, rgb, color-name, etc.).\" }, \"Breadcrumbs-node color\": { value: \"white\", description: \"Any legal HTML color (#000000, rgb, color-name, etc.).\" }, \"Non-breadcrumbs-node background color\": { value: \"rgba(0,0,5,0.7)\", description: \"Any legal HTML color (#000000, rgb, color-name, etc.).\" }, \"Non-breadcrumbs-node color\": { value: \"hsl(208, 80%, 77%)\", description: \"Any legal HTML color (#000000, rgb, color-name, etc.).\" }, \"Virtual-node background color\": { value: \"rgba(255,0,0,0.4)\", description: \"Any legal HTML color (#000000, rgb, color-name, etc.).\" }, \"Virtual-node color\": { value: \"white\", description: \"Any legal HTML color (#000000, rgb, color-name, etc.).\" } }; saveSettings = true; } if(!settings[\"Display alias if available\"]) { settings[\"Display alias if available\"] = { value: true, description: \"Displays the page alias instead of the \" + \"filename if it is specified in the page's front matter. \" }; saveSettings = true; } if(!settings[\"Graph settings JSON\"]) { settings[\"Graph settings JSON\"] = { height: \"450px\", value: {\\n \"breadcrumbs\": {\\n \"down\": [\"children\", \"child\"],\\n \"up\": [\"parents\", \"parent\"],\\n \"jump\": [\"jump\", \"jumps\"]\\n },\\n \"tags\": {\\n \"#excalidraw\": {\\n \"nodeColor\": \"hsl(59, 80%, 77%)\",\\n \"gateColor\": \"#fd7e14\",\\n \"borderColor\": \"black\",\\n \"backgroundColor\": \"rgba(50,50,50,0.5)\",\\n \"prefix\": \"\ud83c\udfa8 \"\\n },\\n \"#dnp\": {\\n \"prefix\": \"\ud83d\uddd3 \"\\n }\\n }\\n} , description: This may contain two elements:
      1. A specification of your breadcrumbs hierarchy. Note, that if you have the Breadcrumbs plugin installed and enabled then TheBrain-navigation script will take your hierarchy settings from Breadcrumbs. If Breadcrumbs is disabled, this specification will be used.
      2. Formatting of nodes based on page tags. You can specify special formatting rules for tags. If multiple tags are present on the page the first matching a specification will be used. You may provide partial specifications as well. e.g. if you only specify prefix, the other attributes will follow your default settings.
      { "breadcrumbs": { "down": ["children", "child"], "up": ["parents", "parent"], "jump": ["jump", "jumps"] }, "tags": { "#excalidraw": { "nodeColor": "hsl(59, 80%, 77%)", "gateColor": "#fd7e14", "borderColor": "black", "backgroundColor": "rgba(50,50,50,0.5)", "prefix": "\ud83c\udfa8 " }, "#dnp": { "prefix": "\ud83d\uddd3 " } } } 
      }; saveSettings = true; } if(saveSettings) { ea.setScriptSettings(settings); } const SHOW_CONFIRMATION_PROMPT = settings[\"Confirmation prompt at startup\"].value; const MAX_ITEMS = Math.floor(settings[\"Max number of nodes/domain\"].value)??40; const INCLUDE_OBSIDIAN_LINKS = settings[\"Infer non-Breadcrumbs links\"].value; const HIDE_ATTACHMENTS = settings[\"Hide attachments\"].value; const FONT_FAMILY = settings[\"Font family\"].value === \"Hand-drawn\" ? 1 : settings[\"Font family\"].value === \"Normal\" ? 2 : settings[\"Font family\"].value === \"Code\" ? 3 : 4; const STROKE_ROUGHNESS = settings[\"Stroke roughness\"].value === \"Architect\" ? 0 : settings[\"Stroke roughness\"].value === \"Artist\" ? 1 : 2; const STROKE_SHARPNESS = settings[\"Rectangle stroke sharpness\"].value; const CENTRAL_FONT_SIZE = Math.floor(settings[\"Central font size\"].value)??30; const FONT_SIZE = Math.floor(settings[\"Font size\"].value)??20; const DISTANT_FONT_SIZE = Math.floor(settings[\"Siblings font size\"].value)??15; const MAX_LABEL_LENGTH = Math.floor(settings[\"Max label length\"].value)??30; const PADDING = Math.floor(settings[\"Padding\"].value)??10; const GATE_OFFSET = Math.floor(settings[\"Gate offset\"].value)??15; const GATE_RADIUS = Math.floor(settings[\"Gate radius\"].value)??5; const BG_COLOR = settings[\"Canvas color\"].value; const GATE_COLOR = settings[\"Gate color\"].value; const LINK_COLOR = settings[\"Link color\"].value; const CENTRAL_NODE_BG_COLOR = settings[\"Central-node background color\"].value; const CENTRAL_NODE_COLOR = settings[\"Central-node color\"].value; const NODE_BG_COLOR = settings[\"Breadcrumbs-node background color\"].value; const NODE_COLOR = settings[\"Breadcrumbs-node color\"].value; const OBSIDIAN_NODE_BG_COLOR = settings[\"Non-breadcrumbs-node background color\"].value; const OBSIDIAN_NODE_COLOR = settings[\"Non-breadcrumbs-node color\"].value; const VIRTUAL_NODE_BG_COLOR = settings[\"Virtual-node background color\"].value; const VIRTUAL_NODE_COLOR = settings[\"Virtual-node color\"].value; const USE_ALIAS = settings[\"Display alias if available\"].value; let formattingJSON = {}; try { formattingJSON = JSON.parse(settings[\"Graph settings JSON\"].value); } catch (e) { new Notice(\"Error reading graph settings JSON, see developer console for more information\",4000); console.log(e); }; const NODE_FORMATTING = formattingJSON?.tags??{}; const FORMATTED_TAGS = Object.keys(NODE_FORMATTING); //------------------------------------------------------- // Load breadcrumbs hierarchies const HIERARCHIES = new Map(); if(window.BCAPI) { //read breadcrumbs if available const getHierarchyFields = (direction) => { const values = new Set(direction); direction.forEach( d => BCAPI.plugin.settings.userHiers.forEach( h => h[d].forEach( x => values.add(x) ) ) ); return Array.from(values); } HIERARCHIES.set(\"down\",getHierarchyFields([\"down\"])); HIERARCHIES.set(\"up\",getHierarchyFields([\"up\"])); HIERARCHIES.set(\"jump\",getHierarchyFields([\"prev\",\"next\"])); } else { HIERARCHIES.set(\"down\",formattingJSON?.breadcrumbs?.down??[\"down\",\"child\",\"children\"]); HIERARCHIES.set(\"up\",formattingJSON?.breadcrumbs?.up??[\"up\", \"parent\",\"parents\"]); HIERARCHIES.set(\"jump\",formattingJSON?.breadcrumbs?.jump??[\"jump\", \"jumps\", \"next\", \"previous\"]); } //------------------------------------------------------- // Initialization //------------------------------------------------------- if(SHOW_CONFIRMATION_PROMPT) { const result = await utils.inputPrompt( \"This will overwrite the current active drawing\", \"type: 'ok' to Continue\" ); if(result !== \"ok\") return; } const measureText = (text,fontSize) => { ea.style.fontSize = fontSize; return ea.measureText(text); } ea.style.fontFamily = FONT_FAMILY; const TEXT_SIZE = measureText(\"m\".repeat(MAX_LABEL_LENGTH+3),FONT_SIZE); const NODE_WIDTH = TEXT_SIZE.width + 3 * PADDING; const NODE_HEIGHT = 2 * (TEXT_SIZE.height + 2 * PADDING); ea.getExcalidrawAPI().updateScene({ appState: { viewModeEnabled:true, theme: \"light\", viewBackgroundColor: BG_COLOR }, elements:[] }); ea.style.strokeColor = NODE_COLOR; ea.addText(0,0,\"Open a document in another pane and click it to get started.\\n\\n\" + \"For the best experience enable 'Open in adjacent pane'\\nin Excalidraw settings \" + \"under 'Links and Transclusion'.\", {textAlign:\"center\"}); await ea.addElementsToView(); ea.getExcalidrawAPI().zoomToFit(); window.excalidrawView = ea.targetView; window.excalidrawFile = ea.targetView.file; ea.targetView.linksAlwaysOpenInANewPane = true; new Notice(\"Brain Graph On\"); //------------------------------------------------------- // Supporting functions and classes //------------------------------------------------------- const getFilenameFromPath = (path) => { const mdFile = path.endsWith(\".md\"); const filename = path.substring(path.lastIndexOf(\"/\")+1); return mdFile ? filename.slice(0,-3) : filename; } const getExtension = (path) => { const lastDot = path.lastIndexOf(\".\"); if(lastDot === -1) return \"md\"; return path.slice(lastDot+1); } const distinct = (data) => Array.from(new Set(data)); class Layout { constructor(spec) { this.spec = spec; this.nodes = []; this.renderedNodes = []; } layout(columns = this.spec.columns) { const generateLayoutVector = (pattern) => { const res = []; let cur = 1; let state = true; pattern .map(p => Math.floor(p)) .forEach(cnt => { for(let i=0;i items%2 ? generateLayoutVector([(columns-items)/2,items,(columns-items)/2]) : generateLayoutVector([(columns-items)/2,items/2,1,items/2,(columns-items)/2]); const sortedNodes = this.nodes.sort((a,b) => a.label.toLowerCase() < b.label.toLowerCase() ? -1 : 1) const itemCount = sortedNodes.length; if(itemCount === 0) return; const rowCount = Math.ceil(itemCount / columns); this.renderedNodes = Array(rowCount).fill().map(( ,i) => (i+1 < rowCount) || (itemCount % columns === 0) ? Array(columns).fill().map(( ,j) => sortedNodes[i columns+j]) //full row : getRowLayout(itemCount % columns).map(idx => idx ? sortedNodes[i columns+idx-1]:null)); } render() { this.layout(); const rows = this.renderedNodes.length; const height = rows * this.spec.rowHeight; const top = (this.spec.top === null && this.spec.bottom === null) //unconstrained ? this.spec.origoY - height/2 : this.spec.top !== null ? (this.spec.origoY - height/2) < this.spec.top //top constrained ? this.spec.top : this.spec.origoY - height/2 : (this.spec.origoY + height/2) > this.spec.bottom //bottom constrained ? this.spec.bottom - height : this.spec.origoY - height/2; const center00 = { x: this.spec.origoX - (this.spec.columns === 1 ? 0 : (this.spec.columns-1)/2 this.spec.columnWidth), y: top }; this.renderedNodes.forEach((nodes,row) => nodes.forEach((node,idx) => { if(!node) return; node.setCenter({ x: center00.x + idx this.spec.columnWidth, y: center00.y + row*this.spec.rowHeight }); node.render(); }) ); } } class Node { constructor(spec) { const dvPage = spec.file ? DataviewAPI?.page(spec.file.path) : null; const tag = (dvPage?.file?.tags?.values??[]).filter(t=>FORMATTED_TAGS.some(x=>t.startsWith(x)))[0]; if(tag) { const format = NODE_FORMATTING[FORMATTED_TAGS.filter(x=>tag.startsWith(x))[0]]; spec.gateColor = format.gateColor ?? spec.gateColor; spec.backgroundColor = format.backgroundColor ?? spec.backgroundColor; spec.nodeColor = format.nodeColor ?? spec.nodeColor; spec.borderColor = format.borderColor ?? spec.borderColor; spec.prefix = format.prefix; } this.spec = spec; 1 2 3 4 5 6 7 8 9 10 11 12 const aliases = (spec.file && USE_ALIAS) ? (dvPage?.file?.aliases?.values??[]) : []; const label = (spec.prefix??\"\") + (aliases.length > 0 ? aliases[0] : (spec.file ? (spec.file.extension === \"md\" ? spec.file.basename : spec.file.name) : spec.nodeTitle)); this.label = label.length > spec.maxLabelLength ? label.substring(0,spec.maxLabelLength-1) + \"...\" : label; this.labelSize = measureText(this.label, spec.fontSize); } setCenter(center) { this.center = center; } render() { ea.style.fontSize = this.spec.fontSize; ea.style.strokeColor = this.spec.file ? this.spec.nodeColor : this.spec.virtualNodeColor; ea.style.backgroundColor = \"transparent\"; this.id = ea.addText( this.center.x - this.labelSize.width / 2, this.center.y - this.labelSize.height / 2, this.label, { wrapAt: this.spec.maxLabelLength+5, textAlign: \"center\", box: true, boxPadding: this.spec.padding } ); const box = ea.getElement(this.id); box.link = [[${this.spec.file?.path??this.spec.nodeTitle}]] ; box.backgroundColor = this.spec.file ? this.spec.backgroundColor : this.spec.virtualNodeBGColor; box.strokeColor = this.spec.borderColor; box.strokeStyle = this.spec.strokeStyle??\"solid\"; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ea.style.strokeColor = this.spec.gateColor; ea.style.backgroundColor = this.spec.hasJumps ? this.spec.gateColor : \"transparent\"; this.jumpGateId = ea.addEllipse( this.spec.jumpOnLeft ? this.center.x - this.spec.gateRadius * 2 - this.spec.padding - this.labelSize.width / 2 : this.center.x + this.spec.padding + this.labelSize.width / 2, this.center.y - this.spec.gateRadius, this.spec.gateRadius * 2, this.spec.gateRadius * 2 ); ea.style.backgroundColor = this.spec.hasParents ? this.spec.gateColor : \"transparent\"; this.parentGateId = ea.addEllipse( this.center.x - this.spec.gateRadius - this.spec.gateOffset, this.center.y - 2 * this.spec.gateRadius - this.spec.padding - this.labelSize.height / 2, this.spec.gateRadius * 2, this.spec.gateRadius * 2 ); ea.style.backgroundColor = this.spec.hasChildren ? this.spec.gateColor : \"transparent\"; this.childGateId = ea.addEllipse( this.center.x - this.spec.gateRadius + this.spec.gateOffset, this.center.y + this.spec.padding + this.labelSize.height / 2, this.spec.gateRadius * 2, this.spec.gateRadius * 2 ); ea.addToGroup([this.jumpGateId,this.parentGateId,this.childGateId,this.id, box.boundElements[0].id]); } } const addNodes = (nodesMap, root, nodes, layout, options) => { nodes.forEach(filePath => { const node = new Node({ nodeTitle: getFilenameFromPath(filePath), file: app.vault.getAbstractFileByPath(filePath), hasChildren: false, hasParents: false, hasJumps: false, ...options }); nodesMap.set(filePath,node); layout.nodes.push(node); }); } //------------------------------------------------------- // Breadcrumbs workaround to handle full filepath //------------------------------------------------------- const getPathOrSelf = (link,host) => { return (app.metadataCache.getFirstLinkpathDest(link,host)?.path)??link; } const readDVField = (field,file) => { const res = new Set(); if(field.values) { field.values.forEach(l=>{ if(l.type === \"file\") res.add(getPathOrSelf(l.path,file.path)); }); return Array.from(res); } if(field.path) { return [getPathOrSelf(field.path,file.path)]; } const m = field.matchAll(/[ []*[[([ #]|] )[^]] ]]/g); while(!(r=m.next()).done) { if(r.value[1]) { res.add(getPathOrSelf(r.value[1],file.path)); } } return Array.from(res); } const getDVFieldLinks = (page,dir,retSet=false) => { const fields = HIERARCHIES.get(dir); const links = new Set(); const processed = new Set(); fields.forEach(f => { if(page[f] && !processed.has(f)) { processed.add(f); readDVField(page[f],page.file).forEach(l=>links.add(l)) }; }); return retSet ? links : Array.from(links); } //------------------------------------------------------- // Event handler //------------------------------------------------------- window.brainGraphEventHandler = async (leaf) => { //sleep for 100ms to give time for leaf.view.file to be set await new Promise((resolve) => setTimeout(resolve, 100)); //------------------------------------------------------- //terminate event handler if view no longer exists or file has changed if(!window.excalidrawView?.file || window.excalidrawView.file.path !== window.excalidrawFile?.path) { removeEventHandler(); return; } if(!leaf?.view?.file) return; const rootFile = leaf.view.file; if (rootFile.path === window.excalidrawFile.path) return; //brainview drawing is active if(window.lastfilePath && window.lastfilePath === rootFile.path) return; //don't reload the file if it has not changed window.lastfilePath = rootFile.path; //------------------------------------------------------- //I must reinitialize ea because in the event handler the script engine ea will no longer be available ea = ExcalidrawAutomate; ea.reset(); ea.setView(window.excalidrawView); ea.style.fontFamily = FONT_FAMILY; ea.style.roughness = STROKE_ROUGHNESS; ea.style.strokeSharpness = STROKE_SHARPNESS; ea.getExcalidrawAPI().updateScene({elements:[]}); ea.style.verticalAlign = \"middle\"; ea.style.strokeSharpness = \"round\"; ea.style.fillStyle = \"solid\"; //------------------------------------------------------- //build list of nodes const dvPage = DataviewAPI.page(rootFile.path); //workaround because const parentsSet = dvPage ? getDVFieldLinks(dvPage,\"up\",true) : new Set(); parentsSet.delete(rootFile.path); const childrenSet = dvPage ? getDVFieldLinks(dvPage,\"down\",true) : new Set(); childrenSet.delete(rootFile.path); const jumpsSet = dvPage ? getDVFieldLinks(dvPage,\"jump\",true) : new Set(); jumpsSet.delete(rootFile.path); let backlinksSet = new Set( Object.keys((app.metadataCache.getBacklinksForFile(rootFile)?.data)??{}) .map(l=>app.metadataCache.getFirstLinkpathDest(l,rootFile.path)?.path??l) ); backlinksSet.forEach(l=>{ const page = DataviewAPI.page(l); if(page) { if(getDVFieldLinks(page,\"down\",true).has(rootFile.path)) { parentsSet.add(l); backlinksSet.delete(l); return; } if(getDVFieldLinks(page,\"up\",true).has(rootFile.path)) { childrenSet.add(l); backlinksSet.delete(l); return; } if(getDVFieldLinks(page,\"jump\", true).has(rootFile.path)) { jumpsSet.add(l); backlinksSet.delete(l); return; } } }) const parents = Array.from(parentsSet).slice(0,MAX_ITEMS); const children = Array.from(childrenSet).slice(0,MAX_ITEMS); const jumps = Array.from(jumpsSet).slice(0,MAX_ITEMS); //adding links from the document, not explicitly declared as a breadcrumb const forwardLinks = INCLUDE_OBSIDIAN_LINKS ? distinct(app.metadataCache .getLinks()[rootFile.path]?.map( l=>app.metadataCache.getFirstLinkpathDest(l.link.match(/[ #]*/)[0],rootFile.path)??l.link.match(/[ #]*/)[0] ).filter(f=>f && (!HIDE_ATTACHMENTS || (f.extension??getExtension(f)) === \"md\")) .map(f=>f.path??f) .filter(l=>!parentsSet.has(l) && !childrenSet.has(l) && !jumpsSet.has(l) && l!==rootFile.path && !backlinksSet.has(l)) .slice(0,Math.max(MAX_ITEMS-children.length)) ) : []; const forwardlinksSet = new Set(forwardLinks); const backLinks = INCLUDE_OBSIDIAN_LINKS ? Array.from(backlinksSet) .filter(l=>!parentsSet.has(l) && !childrenSet.has(l) && !jumpsSet.has(l) && l!==rootFile.path && !forwardlinksSet.has(l)) .slice(0,Math.max(MAX_ITEMS-parents.length)) : []; backlinksSet = new Set(backLinks); const sharedParents = INCLUDE_OBSIDIAN_LINKS ? backLinks.concat(parents) : parents; const siblings = distinct( sharedParents.map(p=>{ const page = DataviewAPI.page(p); return page ? getDVFieldLinks(page,\"down\") .filter(l=>!parentsSet.has(l) && !childrenSet.has(l) && !jumpsSet.has(l) && l!==rootFile.path && !forwardlinksSet.has(l) && !backlinksSet.has(l)) : []; }).flat() ).slice(0,MAX_ITEMS); const siblingsSet = new Set(siblings); const inverseInferredSiblingsMap = new Map(); const inferredSiblings = INCLUDE_OBSIDIAN_LINKS ? distinct( sharedParents.map(p=>{ f = app.vault.getAbstractFileByPath(p); //app.metadataCache.getFirstLinkpathDest(p,rootFile.path); if(!f) { return []; } const res = Object.keys((app.metadataCache.getBacklinksForFile(f)?.data)??{}) .map(l=>app.metadataCache.getFirstLinkpathDest(l,rootFile.path)?.path??l) .filter(l=>!parentsSet.has(l) && !childrenSet.has(l) && !jumpsSet.has(l) && l!==rootFile.path && !forwardlinksSet.has(l) && !backlinksSet.has(l) && !siblingsSet.has(l)); res.forEach(r=>inverseInferredSiblingsMap.set(r,p)); return res; }).flat() ).slice(0,Math.max(MAX_ITEMS-siblings.length)) : []; const inferredSiblingsSet = new Set(inferredSiblings); //------------------------------------------------------- // Generate layout and nodes const nodesMap = new Map(); const linksMap = new Map(); const lCenter = new Layout({ origoX: 0, origoY: 0, top: null, bottom: null, columns: 1, columnWidth: NODE_WIDTH, rowHeight: NODE_HEIGHT }); const manyChildren = (children.length + forwardLinks.length) >10; const manySiblings = (siblings.length + inferredSiblings.length) > 10; const singleParent = (parents.length + backLinks.length) <= 1 const lChildren = new Layout({ origoX: 0, origoY: 2.5 * NODE_HEIGHT, top: 2.5 * NODE_HEIGHT - NODE_HEIGHT/2, bottom: null, columns: manyChildren ? 5 : 3, columnWidth: NODE_WIDTH, rowHeight: NODE_HEIGHT }); const lJumps = new Layout({ origoX: (manyChildren ? -3 : -2) * NODE_WIDTH, origoY: 0, top: null, bottom: null, columns: 1, columnWidth: NODE_WIDTH, rowHeight: NODE_HEIGHT }); const lParents = new Layout({ origoX: 0, origoY: -2.5 * NODE_HEIGHT, top: null, bottom: -2.5 * NODE_HEIGHT + NODE_HEIGHT/2, columns: 3, columnWidth: NODE_WIDTH, rowHeight: NODE_HEIGHT }); const lSiblings = new Layout({ origoX: NODE_WIDTH * ((singleParent ? 0 : 1) + (manySiblings ? 2 : 1)), origoY: -2.5 * NODE_HEIGHT, top: null, bottom: NODE_HEIGHT, columns: (manySiblings ? 3 : 1), columnWidth: NODE_WIDTH, rowHeight: NODE_HEIGHT }) const rootNode = new Node({ file: rootFile, hasChildren: false, hasParents: false, hasJumps: false, fontSize: CENTRAL_FONT_SIZE, jumpOnLeft: true, maxLabelLength: 2*MAX_LABEL_LENGTH, gateRadius: GATE_RADIUS, gateOffset: GATE_OFFSET, padding: PADDING, nodeColor: CENTRAL_NODE_COLOR, gateColor: GATE_COLOR, borderColor: CENTRAL_NODE_COLOR, backgroundColor: CENTRAL_NODE_BG_COLOR, virtualNodeBGColor: VIRTUAL_NODE_BG_COLOR }); nodesMap.set(rootFile.path,rootNode); lCenter.nodes.push(rootNode); addNodes( nodesMap, rootFile, parents, lParents, { fontSize: FONT_SIZE, jumpOnLeft: true, maxLabelLength: MAX_LABEL_LENGTH, gateRadius: GATE_RADIUS, gateOffset: GATE_OFFSET, padding: PADDING, nodeColor: NODE_COLOR, gateColor: GATE_COLOR, borderColor: \"transparent\", backgroundColor: NODE_BG_COLOR, virtualNodeColor: VIRTUAL_NODE_COLOR, virtualNodeBGColor: VIRTUAL_NODE_BG_COLOR } ); addNodes( nodesMap, rootFile, backLinks, lParents, { fontSize: FONT_SIZE, jumpOnLeft: true, maxLabelLength: MAX_LABEL_LENGTH, gateRadius: GATE_RADIUS, gateOffset: GATE_OFFSET, padding: PADDING, nodeColor: OBSIDIAN_NODE_COLOR, gateColor: GATE_COLOR, borderColor: OBSIDIAN_NODE_BG_COLOR, strokeStyle: \"dotted\", strokeWidth: 3, backgroundColor: OBSIDIAN_NODE_BG_COLOR, virtualNodeColor: VIRTUAL_NODE_COLOR, virtualNodeBGColor: VIRTUAL_NODE_BG_COLOR } ); addNodes( nodesMap, rootFile, children, lChildren, { fontSize: FONT_SIZE, jumpOnLeft: true, maxLabelLength: MAX_LABEL_LENGTH, gateRadius: GATE_RADIUS, gateOffset: GATE_OFFSET, padding: PADDING, nodeColor: NODE_COLOR, gateColor: GATE_COLOR, borderColor: \"transparent\", backgroundColor: NODE_BG_COLOR, virtualNodeColor: VIRTUAL_NODE_COLOR, virtualNodeBGColor: VIRTUAL_NODE_BG_COLOR } ); addNodes( nodesMap, rootFile, forwardLinks, lChildren, { fontSize: FONT_SIZE, jumpOnLeft: true, maxLabelLength: MAX_LABEL_LENGTH, gateRadius: GATE_RADIUS, gateOffset: GATE_OFFSET, padding: PADDING, nodeColor: OBSIDIAN_NODE_COLOR, gateColor: GATE_COLOR, borderColor: OBSIDIAN_NODE_BG_COLOR, strokeStyle: \"dotted\", strokeWidth: 3, backgroundColor: OBSIDIAN_NODE_BG_COLOR, virtualNodeColor: VIRTUAL_NODE_COLOR, virtualNodeBGColor: VIRTUAL_NODE_BG_COLOR } ); addNodes( nodesMap, rootFile, jumps, lJumps, { fontSize: FONT_SIZE, jumpOnLeft: false, maxLabelLength: MAX_LABEL_LENGTH, gateRadius: GATE_RADIUS, gateOffset: GATE_OFFSET, padding: PADDING, nodeColor: NODE_COLOR, gateColor: GATE_COLOR, borderColor: \"transparent\", backgroundColor: NODE_BG_COLOR, virtualNodeColor: VIRTUAL_NODE_COLOR, virtualNodeBGColor: VIRTUAL_NODE_BG_COLOR } ); addNodes( nodesMap, rootFile, siblings, lSiblings, { fontSize: DISTANT_FONT_SIZE, jumpOnLeft: true, maxLabelLength: MAX_LABEL_LENGTH, gateRadius: GATE_RADIUS, gateOffset: GATE_OFFSET, padding: PADDING, nodeColor: NODE_COLOR, gateColor: GATE_COLOR, borderColor: \"transparent\", backgroundColor: NODE_BG_COLOR, virtualNodeColor: VIRTUAL_NODE_COLOR, virtualNodeBGColor: VIRTUAL_NODE_BG_COLOR } ); addNodes( nodesMap, rootFile, inferredSiblings, lSiblings, { fontSize: DISTANT_FONT_SIZE, jumpOnLeft: true, maxLabelLength: MAX_LABEL_LENGTH, gateRadius: GATE_RADIUS, gateOffset: GATE_OFFSET, padding: PADDING, nodeColor: OBSIDIAN_NODE_COLOR, gateColor: GATE_COLOR, borderColor: OBSIDIAN_NODE_BG_COLOR, strokeStyle: \"dotted\", strokeWidth: 3, backgroundColor: OBSIDIAN_NODE_BG_COLOR, virtualNodeColor: VIRTUAL_NODE_COLOR, virtualNodeBGColor: VIRTUAL_NODE_BG_COLOR } ); //------------------------------------------------------- // Generate links for all displayed nodes const separator = \"?\"; //any character disallowed in filenames Array.from(nodesMap.keys()).forEach(filePath => { const node = nodesMap.get(filePath); const dvPage = DataviewAPI.page(filePath); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 //------------------------------------------------------- // Add links originating from node const parent = forwardlinksSet.has(filePath) ? rootFile.path : inverseInferredSiblingsMap.get(filePath); const parents = dvPage ? (parent ? getDVFieldLinks(dvPage,\"up\",true).add(parent) : getDVFieldLinks(dvPage,\"up\",true)) : (parent ? new Set([parent]) : new Set()); const child = backlinksSet.has(filePath) ? rootFile.path : null const children = dvPage ? (child ? getDVFieldLinks(dvPage,\"down\", true).add(child) : getDVFieldLinks(dvPage,\"down\", true)) : (child ? new Set([child]) : new Set()); const jumps = dvPage ? getDVFieldLinks(dvPage,\"jump\", true) : new Set(); //siblings are left out in this case on purpose const addLinks = (nodes,relation) => nodes.forEach(n => { if(!nodesMap.has(n)) return; if(linksMap.has(filePath + separator + n)) return; linksMap.set(filePath + separator + n,relation); linksMap.set(n + separator + filePath,null); }); addLinks(parents,[\"parent\",\"child\"]); addLinks(children, [\"child\",\"parent\"]); addLinks(jumps, [\"jump\",\"jump\"]); //------------------------------------------------------- //Set gate fill //- if node has outgoing links node.spec.hasChildren = node.spec.hasChildren || children.size>0; node.spec.hasParents = node.spec.hasParents || parents.size>0; node.spec.hasJumps = node.spec.hasJumps || jumps.size>0; //- of the nodes on the other end of outgoing links // this is required on top of backlinks, to handle // nodes for referenced but not yet created pages parents.forEach(p=>{ const n = nodesMap.get(p); if(n) n.spec.hasChildren = true; }); children.forEach(p=>{ const n = nodesMap.get(p); if(n) n.spec.hasParents = true; }); jumps.forEach(p=>{ const n = nodesMap.get(p); if(n) n.spec.hasJumps = true; }); //- if node has an incoming link from a node outside the visible graph if(node.spec.file && !(node.spec.hasParents && node.spec.hasChildren && node.hasJumps)) { const nodeBacklinks = new Set( Object.keys((app.metadataCache.getBacklinksForFile(node.spec.file)?.data)??{}) .map(l=>app.metadataCache.getFirstLinkpathDest(l,filePath)?.path??l) ); nodeBacklinks.forEach(l=>{ if(node.spec.hasParents && node.spec.hasChildren && node.hasJumps) return; const page = DataviewAPI.page(l); if(page) { if(getDVFieldLinks(page,\"down\",true).has(filePath)) { node.spec.hasParents = true; return; } if(getDVFieldLinks(page,\"up\",true).has(filePath)) { node.spec.hasChildren = true; return; } if(getDVFieldLinks(page,\"jump\",true).has(filePath)) { node.spec.hasJumps = true; return; } if(INCLUDE_OBSIDIAN_LINKS) { node.spec.hasParents = true; return; } } }); } if(!node.spec.hasChildren && INCLUDE_OBSIDIAN_LINKS && node.spec.file) { const forwardLinks = app.metadataCache .getLinks()[filePath]?.map( l=>app.metadataCache.getFirstLinkpathDest(l.link.match(/[^#]*/)[0],rootFile.path)??l.link.match(/[^#]*/)[0] ).filter(f=>f && (!HIDE_ATTACHMENTS || (f.extension??getExtension(f)) === \"md\")) .map(f=>f.path??f) .filter(l=>!parents.has(l) && !children.has(l) && !jumps.has(l) && l!==rootFile.path); if(forwardLinks.length > 0) { node.spec.hasChildren = true; } } }); //------------------------------------------------------- // Render lCenter.render(); lParents.render(); lChildren.render(); lJumps.render(); lSiblings.render(); const getGate = (key,value) => (value === \"parent\") ? nodesMap.get(key).parentGateId : (value === \"child\") ? nodesMap.get(key).childGateId : nodesMap.get(key).jumpGateId; ea.style.strokeColor = LINK_COLOR; for([key,value] of linksMap) { if(value) { const k=key.split(separator); const gate1 = getGate(k[0],value[0]); const gate2 = getGate(k[1],value[1]); ea.connectObjects(gate1, null, gate2, null, { startArrowHead: null, endArrowHead: null }); } } elements = ea.getElements(); ea.getExcalidrawAPI().updateScene({ elements: elements.filter( el=>el.type===\"arrow\" ).concat(elements.filter(el=>el.type!==\"arrow\")) }) ea.getExcalidrawAPI().zoomToFit(); } //------------------------------------------------------- // Initiate event listner and trigger plex if file is open //------------------------------------------------------- app.workspace.on(EVENT, window.brainGraphEventHandler); ea.targetView.semaphores.saving = true; //disable saving by setting this Excalidraw flag (not published API) const mdLeaf = app.workspace.getLeavesOfType(\"markdown\"); if(mdLeaf.length>0) { window.brainGraphEventHandler(mdLeaf[0]); return; } const mdExcalidrawLeaf = app.workspace.getLeavesOfType(\"excalidraw\").filter(l=>l!==ea.targetView.leaf); if(mdExcalidrawLeaf.length>0) { window.brainGraphEventHandler(mdExcalidrawLeaf[0]); return; } const mdImageLeaf = app.workspace.getLeavesOfType(\"image\"); if(mdImageLeaf.length>0) { window.brainGraphEventHandler(mdImageLeaf[0]); return; }","title":"TheBrain navigation"},{"location":"tags/","text":"Tags Associations \u5173\u8054 Associative-containers \u5173\u8054\u5bb9\u5668 Binding \u7ed1\u5b9a C++11 11.13 - for-each \u5faa\u73af 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb 4.11 - \u5b57\u7b26 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 M.7 \u2014 std::shared_ptr C++14 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 4.15 - \u5b57\u9762\u91cf 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad M.6 \u2014 std::unique_ptr M.7 \u2014 std::shared_ptr C++17 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 11.16 \u2014 \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb 11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 4.18 - std::string_view \u7b80\u4ecb 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 7.17 - \u65ad\u8a00\u548c static_assert M.6 \u2014 std::unique_ptr M.7 \u2014 std::shared_ptr C++20 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 11.13 - for-each \u5faa\u73af 11.19 \u2014 \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 6.14 - Constexpr \u548c consteval \u51fd\u6570 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad M.7 \u2014 std::shared_ptr C-style-casts C \u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362 CTAD 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD Early binding \u65e9\u671f\u7ed1\u5b9a Encapsulation \u5c01\u88c5 Expression-parameters \u8868\u8fbe\u5f0f\u53c2\u6570 Frankenobject \u7f1d\u5408\u602a\u5bf9\u8c61 Function templates 8.13 - \u51fd\u6570\u6a21\u677f Late binding \u540e\u671f\u7ed1\u5b9a Lvalue 9.3 - \u5de6\u503c\u5f15\u7528 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528 Multiple inheritance \u591a\u91cd\u7ee7\u627f Name-mangling \u540d\u79f0\u4fee\u9970 Numeral systems 4.16 - \u6570\u503c\u7cfb\u7edf\uff08\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236\uff09 Off-by-one \u5dee\u4e00\u9519\u8bef Output-manipulators \u8f93\u51fa\u64cd\u7eb5\u5668 Pointer arithmetic 11.9 - \u6307\u9488\u8fd0\u7b97\u548c\u6570\u7ec4\u7d22\u5f15 R-value-references M.2 - \u53f3\u503c\u5f15\u7528 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c RAII (Resource Acquisition Is Initialization) \u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09 Run-time-type-information-RTTI \u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f(RTTI) Sequence-containers \u987a\u5e8f\u5bb9\u5668 Syntax 3.1 - \u8bed\u6cd5\u548c\u8bed\u4e49\u9519\u8bef Template-specialization \u6a21\u677f\u7279\u5316 abbreviated function templates \u7f29\u5199\u51fd\u6570\u6a21\u677f abstract-base-classes 18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b abstraction \u62bd\u8c61 access specifier 13.3 - \u8bbf\u95ee\u4fee\u9970\u7b26 public \u548c private access-function \u6210\u5458\u8bbf\u95ee\u51fd\u6570 access-specifiers 17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 address 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de 9.9 - \u6309\u5730\u5740\u4f20\u9012 address-of-operator \u53d6\u5730\u5740\u64cd\u4f5c\u7b26 & aggregate initialization 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 aggregate-data-type \u805a\u5408\u6570\u636e\u7c7b\u578b aggregate-initialization \u805a\u5408\u521d\u59cb\u5316 aggregation 16.3 - \u805a\u5408\u5173\u7cfb \u805a\u5408 algorithms 11.19 \u2014 \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb alias 8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d ambiguous-match \u4e0d\u660e\u786e\u5339\u914d anonymous-object \u533f\u540d\u5bf9\u8c61 argument 12.5 - \u547d\u4ee4\u884c\u53c2\u6570 2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2 arguments \u5b9e\u53c2 arithmetic conversions 8.4 - \u7b97\u672f\u8f6c\u6362 arity \u5143\u6570 array 11.1 - \u6570\u7ec4\uff08\u7b2c\u4e00\u90e8\u5206\uff09 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.3 - \u6570\u7ec4\u548c\u5faa\u73af 11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f 11.5 - \u591a\u7ef4\u6570\u7ec4 11.8 - \u6307\u9488\u548c\u6570\u7ec4 arrays 11.12 - \u52a8\u6001\u5206\u914d\u6570\u7ec4 assert 7.17 - \u65ad\u8a00\u548c static_assert assignment 14.15 - \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26 association 16.4 - \u5173\u8054\u5173\u7cfb auto 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad automatic-memory-allocation \u81ea\u52a8\u5185\u5b58\u5206\u914d automatic-storage-duration \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 base-class \u57fa\u7c7b bit O.1 - bit\u6807\u8bb0\u548c\u901a\u8fc7std::bitset\u64cd\u4f5cbit O.2 - \u4f4d\u8fd0\u7b97\u7b26 O.3 - \u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u548c\u63a9\u7801\u64cd\u4f5c\u4f4d O.4 - \u4e8c\u8fdb\u5236\u548c\u5341\u8fdb\u5236\u8f6c\u6362 \u4f4d bitwise-and None bitwise-or \u6309\u4f4d\u6216 bool 4.9 - \u5e03\u5c14\u503c boolean 4.10 - if \u8bed\u53e5\u7b80\u4ecb bss segment bss\u6bb5 byte \u5b57\u8282 c-style 11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 call stack 3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808 call-stack \u8c03\u7528\u6808 callable-object \u53ef\u8c03\u7528\u5bf9\u8c61 callback functions \u56de\u8c03\u51fd\u6570 char 4.11 - \u5b57\u7b26 child-class \u5b50\u7c7b circular dependency M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898 class 13.11 - \u7c7b\u4ee3\u7801\u548c\u5934\u6587\u4ef6 13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570 13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf 13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570 13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b 13.16 - \u533f\u540d\u5bf9\u8c61 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b 13.18 - \u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 13.2 - \u7c7b\u548c\u7c7b\u6210\u5458 class template 10.10 - \u7c7b\u6a21\u677f 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD class-template \u7c7b\u6a21\u677f class-template-argument-deduction \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad class-type \u7c7b\u7c7b\u578b closure \u95ed\u5305 code coverage 7.13 - \u4ee3\u7801\u8986\u76d6\u7387 code segment \u4ee3\u7801\u6bb5 comments 1.2 - \u6ce8\u91ca compile-time \u7f16\u8bd1\u65f6 compiler \u7f16\u8bd1\u5668 composite-types \u7ec4\u5408\u7c7b\u578b composition 16.2 - \u7ec4\u5408\u5173\u7cfb compound data types 9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b const 4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 9.8 - \u6307\u9488\u548cconst const class 13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570 const-cast const \u7c7b\u578b\u8f6c\u6362 const-member-function const \u6210\u5458\u51fd\u6570 const-pointer \u5e38\u91cf\u6307\u9488 constant expression \u5e38\u91cf\u8868\u8fbe\u5f0f constants 4.15 - \u5b57\u9762\u91cf consteval 6.14 - Constexpr \u548c consteval \u51fd\u6570 constexpr 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr 6.14 - Constexpr \u548c consteval \u51fd\u6570 constructor 13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570 14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570 constructors 13.5 - \u6784\u9020\u51fd\u6570 13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 container classes 16.6 - \u5bb9\u5668\u7c7b container-adapter \u5bb9\u5668\u9002\u914d\u5668 container-class \u5bb9\u5668\u7c7b containers 21.2 - STL\u5bb9\u5668 21.3 - STL\u8fed\u4ee3\u5668 21.4 - STL\u7b97\u6cd5 control flow 7.1 - \u63a7\u5236\u6d41 7.4 - switch \u8bed\u53e5\u57fa\u7840 conversions 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 8.3 - \u6570\u503c\u8f6c\u6362 8.4 - \u7b97\u672f\u8f6c\u6362 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast converting constructors 14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57 converting-constructors \u8f6c\u6362\u6784\u9020\u51fd\u6570 copy constructor 14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570 copy initialization 14.13 - \u62f7\u8d1d\u521d\u59cb\u5316 copy-constructor \u62f7\u8d1d\u6784\u9020\u51fd\u6570 copy-constructors \u62f7\u8d1d\u6784\u9020\u51fd\u6570 copy-initialization \u62f7\u8d1d\u521d\u59cb\u5316 copy-semantics \u62f7\u8d1d\u8bed\u4e49 covariant \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b covariant-return-types 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b dangling \u60ac\u5782 data segment \u6570\u636e\u6bb5 data type 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb 4.4 - \u6709\u7b26\u53f7\u6574\u578b 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 4.8 - \u6d6e\u70b9\u6570 4.9 - \u5e03\u5c14\u503c 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad debugger 3.6 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u5355\u6b65\u8c03\u8bd5 3.7 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8fd0\u884c\u548c\u65ad\u70b9 3.8 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u76d1\u63a7\u53d8\u91cf 3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808 debugging 3.2 - \u4ee3\u7801\u8c03\u8bd5\u6b65\u9aa4 3.3 - \u4ee3\u7801\u8c03\u8bd5\u7b56\u7565 3.4 \u2014 \u57fa\u672c\u4ee3\u7801\u8c03\u8bd5\u6280\u672f 3.5 - \u66f4\u591a\u4ee3\u7801\u8c03\u8bd5\u6280\u672f declaration 6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e \u58f0\u660e \u7eaf\u58f0\u660e(pure-declaration) deep copy 14.16 - \u6d45\u62f7\u8d1d\u548c\u6df1\u62f7\u8d1d 14.17 - \u91cd\u8f7d\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u6a21\u677f deep-copy \u6df1\u62f7\u8d1d default 8.12 - \u9ed8\u8ba4\u53c2\u6570 default-constructor \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 default-initialization \u9ed8\u8ba4\u521d\u59cb\u5316 default-member-initializer \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c definition \u5b9a\u4e49 delegating 13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570 delegating-constructors \u59d4\u6258\u6784\u9020\u51fd\u6570 delete 11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d 14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57 dependencies 16.5 - \u4f9d\u8d56\u5173\u7cfb dependency \u4f9d\u8d56 dereference-operator \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 derived-class \u6d3e\u751f\u7c7b desgin 2.13 - \u5982\u4f55\u8bbe\u8ba1\u7b2c\u4e00\u4e2a\u7a0b\u5e8f designated-initializers \u6307\u5b9a\u521d\u59cb\u5316 destructor \u6790\u6784\u51fd\u6570 destructors 13.9 - \u6790\u6784\u51fd\u6570 direct-initialization \u76f4\u63a5\u521d\u59cb\u5316 do-while 7.8 - do-while \u8bed\u53e5 downcasting \u5411\u4e0b\u8f6c\u6362 dumb-pointer \u7b28\u6307\u9488 dynamic casting 18.10 - \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 dynamic duration \u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 dynamic-casts \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 dynamic-initialization \u52a8\u6001\u521d\u59cb\u5316 dynamic-memory 11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d dynamic-memory-allocation \u52a8\u6001\u5185\u5b58\u5206\u914d early-binding 18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a ellipsis 12.6 - \u7701\u7565\u53f7\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 encapsulation 13.4 - \u6210\u5458\u8bbf\u95ee\u51fd\u6570\u4e0e\u5c01\u88c5 enum 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b 10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 enum-class \u679a\u4e3e\u7c7b error handling 7.15 - \u53d1\u73b0\u9519\u8bef\u548c\u5904\u7406\u9519\u8bef 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406 evaluation \u6c42\u503c excalidraw Drawing 2023 02 11 13.33.50.excalidraw Template.excalidraw C++ basic onepage Moc onepage exception-safety-issue \u5f02\u5e38\u5b89\u5168\u95ee\u9898 explicit 14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57 explicit-type-conversion 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 expression 1.10 - \u8868\u8fbe\u5f0f\u7b80\u4ecb expression type \u8868\u8fbe\u5f0f\u7c7b\u578b external-linkage \u5916\u90e8\u94fe\u63a5 external-variable \u5916\u90e8\u53d8\u91cf extraction-operator \u63d0\u53d6\u8fd0\u7b97\u7b26 fallthrough \u8d2f\u7a7f\u5c5e\u6027 file 23.6 - \u57fa\u672c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa file-scope \u6587\u4ef6\u4f5c\u7528\u57df final 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b fixed-array \u56fa\u5b9a\u6570\u7ec4 fixed-width 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t float 4.8 - \u6d6e\u70b9\u6570 float point 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 for 7.7 - \u5faa\u73af\u548c while \u8bed\u53e5 7.9 - for \u8bed\u53e5 for-each 11.13 - for-each \u5faa\u73af fordward declaration 2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49 format-group \u683c\u5f0f\u7ec4 formatting 1.8 - \u7a7a\u767d\u7b26\u548c\u57fa\u672c\u6392\u7248 forward declaration \u524d\u5411\u58f0\u660e friend 13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b 13.16 - \u533f\u540d\u5bf9\u8c61 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b 13.18 - \u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 friend-class \u53cb\u5143\u7c7b friend-function \u53cb\u5143\u51fd\u6570 function 12.1 - \u51fd\u6570\u6307\u9488 2.1 - \u51fd\u6570\u7b80\u4ecb 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 2.6 - \u4e3a\u4ec0\u4e48\u51fd\u6570\u6709\u7528\uff0c\u5982\u4f55\u9ad8\u6548\u4f7f\u7528\u51fd\u6570 function template 8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f function-instance \u51fd\u6570\u5b9e\u4f8b function-literal \u51fd\u6570\u5b57\u9762\u91cf function-pointer \u51fd\u6570\u6307\u9488 function-template \u51fd\u6570\u6a21\u677f function-template-instantiation \u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316 generic-algorithm \u6cdb\u578b\u7b97\u6cd5 global 6.4 - \u5168\u5c40\u53d8\u91cf 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf global variable 6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c \u5168\u5c40\u53d8\u91cf glossary \u7f16\u8bd1\u65f6 \u58f0\u660e \u5b9a\u4e49 \u524d\u5411\u58f0\u660e \u5168\u5c40\u53d8\u91cf \u6807\u8bc6\u7b26(identifier) \u5b9e\u4f8b\u5316 \u6307\u4ee4 \u94fe\u63a5\u5668(linker) \u5b8f \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u5f62\u53c2 \u6309\u503c\u4f20\u9012 \u9884\u5904\u7406\u5668\u6307\u4ee4 \u9884\u5904\u7406\u5668 \u7eaf\u58f0\u660e(pure-declaration) \u9650\u5b9a\u7b26 \u91cd\u6784 \u6309\u503c\u8fd4\u56de \u8fd0\u884c\u65f6 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u4f5c\u7528\u57df \u8bed\u53e5 \u7b26\u53f7\u5e38\u91cf goto 7.6 - goto \u8bed\u53e5 halt 7.11 - \u6302\u8d77\uff08\u63d0\u524d\u7ed3\u675f\u7a0b\u5e8f\uff09 header 2.11 - \u5934\u6587\u4ef6 header guards 2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e heap 12.2 - \u6808\u548c\u5806 \u5806 identifier 1.7 - \u5173\u952e\u5b57\u548c\u6807\u8bc6\u7b26 \u6807\u8bc6\u7b26(identifier) identity \u8eab\u4efd\u7279\u5f81 if 4.10 - if \u8bed\u53e5\u7b80\u4ecb 7.2 - if \u8bed\u53e5\u548c\u8bed\u53e5\u5757 7.3 - \u5e38\u89c1\u7684 if \u8bed\u53e5\u9519\u8bef immediate-functions \u5373\u65f6\u51fd\u6570 implicit-constructor \u9690\u5f0f\u6784\u9020\u51fd\u6570 implicit-object \u9690\u5f0f\u5bf9\u8c61 implicit-type-conversion \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 inheritance 17.1 - \u7ee7\u627f\u7b80\u4ecb 17.2 - C++\u7ee7\u627f\u57fa\u7840 17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f 17.4 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u548c\u521d\u59cb\u5316 17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 17.6 - \u4e3a\u6d3e\u751f\u7c7b\u6dfb\u52a0\u65b0\u529f\u80fd 17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570 17.9 - \u591a\u91cd\u7ee7\u627f 18.11 - \u4f7f\u7528 << \u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b \u7ee7\u627f initialization 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u62f7\u8d1d\u521d\u59cb\u5316 \u76f4\u63a5\u521d\u59cb\u5316 \u5217\u8868\u521d\u59cb\u5316 initializer-list \u521d\u59cb\u5316\u503c\u5217\u8868 initializer_list 16.7 - std::initializer_list inline 6.13 - \u5185\u8054\u51fd\u6570 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf inline namespace 6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4 inline-function \u5185\u8054\u51fd\u6570 inline-namespace \u5185\u8054\u547d\u540d\u7a7a\u95f4 inline-variables \u5185\u8054\u53d8\u91cf input-manipulator 4.17 - std::string \u7b80\u4ecb input-manipulators \u8f93\u5165\u64cd\u7eb5\u5668 insertion-operator \u63d2\u5165\u8fd0\u7b97\u7b26 instance \u5b9e\u4f8b instantiated \u5b9e\u4f8b\u5316 instuction \u6307\u4ee4 integer 4.4 - \u6709\u7b26\u53f7\u6574\u578b 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t integral-type \u6574\u578b\u7c7b\u578b interface-class \u63a5\u53e3\u7c7b interface-classes 18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b internal-linkage \u5185\u90e8\u94fe\u63a5 internal-variable \u5185\u90e8\u53d8\u91cf io 23.1 - \u8f93\u5165\u8f93\u51fa\u6d41 23.2 - \u4f7f\u7528istream\u5904\u7406\u8f93\u5165 23.3 - \u4f7f\u7528ostream\u548cios\u5904\u7406\u8f93\u51fa 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b 23.5 - \u6d41\u72b6\u6001\u548c\u8f93\u5165\u9a8c\u8bc1 23.6 - \u57fa\u672c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa 23.7 - \u968f\u673a\u6587\u4ef6\u8f93\u5165\u8f93\u51fa iostream 1.5 - iostream \u7b80\u4ecb\uff1acout\uff0ccin \u548c endl iterating \u904d\u5386 iterator 11.18 \u2014 \u8fed\u4ee3\u5668\u7b80\u4ecb \u8fed\u4ee3\u5668 lambda 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 \u533f\u540d\u51fd\u6570 late-binding 18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a link \u94fe\u63a5 linkage 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 6.3 - \u5c40\u90e8\u53d8\u91cf 6.6 - \u5185\u90e8\u94fe\u63a5 6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e \u94fe\u63a5\u5c5e\u6027 linker \u94fe\u63a5\u5668(linker) list assignment operator \u7c7b\u522b\u8d4b\u503c\u8fd0\u7b97\u7b26 list-constructor \u5217\u8868\u6784\u9020\u51fd\u6570 list-initialization \u5217\u8868\u521d\u59cb\u5316 literal 4.15 - \u5b57\u9762\u91cf literals 1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 \u5b57\u9762\u91cf local variable 6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf local variables 6.3 - \u5c40\u90e8\u53d8\u91cf loop 11.3 - \u6570\u7ec4\u548c\u5faa\u73af lvalue \u5de6\u503c lvalue references 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 lvalue-reference \u5de6\u503c\u5f15\u7528 lvalues 9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09 macro \u5b8f member access operator \u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26 member selection 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 member-function \u6210\u5458\u51fd\u6570 member-initializer-list \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 member-selection-operator \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 member-types \u6210\u5458\u7c7b\u578b member-variable \u6210\u5458\u53d8\u91cf members 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 memberwise initialization \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 memberwise-assignment \u6210\u5458\u4f9d\u6b21\u8d4b\u503c memberwise-copy \u6210\u5458\u4f9d\u6b21\u62f7\u8d1d memory-leak \u5185\u5b58\u6cc4\u6f0f mixin None move M.4 - std::move M.5 - std::move_if_noexcept M.6 \u2014 std::unique_ptr move assignment operator \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 move-constructor \u79fb\u52a8\u6784\u9020\u51fd\u6570 move-semantics M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb \u79fb\u52a8\u8bed\u4e49 multiple-inheritance 17.9 - \u591a\u91cd\u7ee7\u627f named-cast \u5177\u540d\u7c7b\u578b\u8f6c\u6362 namespace 2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u547d\u540d\u7a7a\u95f4 namespace aliases 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 narrowing-convertions \u7f29\u7a84\u8f6c\u6362 new 11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d numeric conversions 8.3 - \u6570\u503c\u8f6c\u6362 numeric promotion 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 numeric promotions \u6570\u503c\u63d0\u5347 numeric-conversions \u6570\u503c\u8f6c\u6362 object 1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf 13.1 - \u6b22\u8fce\u6765\u5230\u9762\u5411\u5bf9\u8c61\u7684\u4e16\u754c 16.1 - \u5bf9\u8c61\u5173\u7cfb 16.2 - \u7ec4\u5408\u5173\u7cfb 16.3 - \u805a\u5408\u5173\u7cfb 16.4 - \u5173\u8054\u5173\u7cfb 16.5 - \u4f9d\u8d56\u5173\u7cfb 16.6 - \u5bb9\u5668\u7c7b 16.7 - std::initializer_list object-composition \u5bf9\u8c61\u7ec4\u5408 object-like-macros \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f object-slicing \u5bf9\u8c61\u5207\u7247 one-definition-rule \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) operands \u64cd\u4f5c\u6570 operator 1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 5.1 - \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 5.3 - \u6c42\u6a21\u548c\u6307\u6570\u8fd0\u7b97 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 5.5 - \u9017\u53f7\u548c\u6761\u4ef6\u8fd0\u7b97\u7b26 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 5.7 - \u903b\u8f91\u8fd0\u7b97\u7b26 \u8fd0\u7b97\u7b26 operator<< 18.11 - \u4f7f\u7528 << \u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b optional-parameter \u53ef\u9009\u53c2\u6570 overflow 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 overlapping 13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570 overload 14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26 14.15 - \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 8.10 - \u91cd\u8f7d\u51fd\u6570\u7684\u533a\u5206 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u4e0d\u660e\u786e\u5339\u914d \u91cd\u8f7d overload-resolution \u91cd\u8f7d\u89e3\u6790 overloading 8.9 - \u51fd\u6570\u91cd\u8f7d override 18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b \u91cd\u5199 overriding 17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570 overriding-virtualization 18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316 parameter 2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2 parameters \u5f62\u53c2 parent-class \u7236\u7c7b parenthesis 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 pass-by-address \u6309\u5730\u5740\u4f20\u9012 pass-by-reference \u6309\u5f15\u7528\u4f20\u9012 pass-by-value \u6309\u503c\u4f20\u9012 placeholder-types \u5360\u4f4d\u7b26\u7c7b\u578b pointer 11.15 \u2014 \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u548c\u591a\u7ef4\u6570\u7ec4 11.8 - \u6307\u9488\u548c\u6570\u7ec4 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 9.6 - \u6307\u9488\u7b80\u4ecb 9.7 - \u7a7a\u6307\u9488 9.8 - \u6307\u9488\u548cconst 9.9 - \u6309\u5730\u5740\u4f20\u9012 pointer-to-const \u6307\u5411\u5e38\u91cf\u7684\u6307\u9488 pointers 18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 polymorphism 18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001 \u591a\u6001 pragma once 2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e preprocessor 2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb \u9884\u5904\u7406\u5668\u6307\u4ee4 \u9884\u5904\u7406\u5668 private-inheritance \u79c1\u6709\u7ee7\u627f private-member \u79c1\u6709\u6210\u5458 program organization 2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f protected-inheritance \u53d7\u4fdd\u62a4\u7ee7\u627f protected-members \u53d7\u4fdd\u62a4\u6210\u5458 public-inheritance \u516c\u5f00\u7ee7\u627f public-member \u516c\u6709\u6210\u5458 pure-declaration \u7eaf\u58f0\u660e(pure-declaration) pure-virtual \u7eaf\u865a\u51fd\u6570 pure-virtual-functions 18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b qualifier \u9650\u5b9a\u7b26 random-access-memory \u968f\u673a\u8bbf\u95ee\u5185\u5b58 range-based-for-loops \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af raw-pointer \u539f\u59cb\u6307\u9488 recursion 12.4 - \u9012\u5f52 refactoring \u91cd\u6784 reference 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de reference-container \u5f15\u7528\u5bb9\u5668 references 18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 M.2 - \u53f3\u503c\u5f15\u7528 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c reflexive association \u53cd\u8eab\u5173\u8054 reinterpret-casts \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 return 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de return-by-address \u6309\u5730\u5740\u8fd4\u56de return-by-reference \u6309\u5f15\u7528\u8fd4\u56de return-by-value \u6309\u503c\u8fd4\u56de return-type-deduction \u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad rounding-error \u820d\u5165\u8bef\u5dee runtime \u8fd0\u884c\u65f6 rvalue \u53f3\u503c rvalue-reference \u53f3\u503c\u5f15\u7528 rvalues 9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09 scaling \u6307\u9488\u7f29\u653e scientific notation 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 scope 2.5 - \u5c40\u90e8\u4f5c\u7528\u57df 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 \u4f5c\u7528\u57df scope resolution operator 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 scope-resolution-operator \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 scoped enumerations 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 scoped-enumerations \u6709\u4f5c\u7528\u57df\u679a\u4e3e segment \u5185\u5b58\u6bb5 sentinel \u54e8\u5175 setup 0.8 - \u4e00\u4e9b\u5178\u578b\u7684 C++ \u95ee\u9898 shadow \u53d8\u91cf\u906e\u853d shallow copy 14.16 - \u6d45\u62f7\u8d1d\u548c\u6df1\u62f7\u8d1d 14.17 - \u91cd\u8f7d\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u6a21\u677f shallow-copy \u6d45\u62f7\u8d1d shared_ptr M.7 \u2014 std::shared_ptr M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898 side-effects \u526f\u4f5c\u7528 signature \u7b7e\u540d size_t 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t sizeof 4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26 smart pointer class \u667a\u80fd\u6307\u9488\u7c7b smart-pointer \u667a\u80fd\u6307\u9488 smart-pointers M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb sort 11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f stack 12.2 - \u6808\u548c\u5806 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a \u6808 stack-frame \u6808\u5e27 stack-overflow \u5806\u6808\u6ea2\u51fa standard-stream \u6807\u51c6\u6d41 statement 6.1 - \u590d\u5408\u8bed\u53e5\uff08\u8bed\u53e5\u5757\uff09 \u8bed\u53e5 statements 1.1 - \u7a0b\u5e8f\u7ed3\u6784\u548c\u8bed\u53e5 static 6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf static membe function 13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570 static member 13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 static member variable 13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf static-cast 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast static-casts \u9759\u6001\u7c7b\u578b\u8f6c\u6362 static-initialization \u9759\u6001\u521d\u59cb\u5316 static-member-functions \u9759\u6001\u6210\u5458\u51fd\u6570 static-member-variables \u9759\u6001\u6210\u5458\u53d8\u91cf static-memory-allocation \u9759\u6001\u5185\u5b58\u5206\u914d static-storage-duration \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 static-variables \u9759\u6001\u53d8\u91cf static_assert 7.17 - \u65ad\u8a00\u548c static_assert static_cast 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast 8.3 - \u6570\u503c\u8f6c\u6362 std::array 11.16 \u2014 \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb std::string_view 11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 stl 21.1 - \u6807\u51c6\u5e93 21.2 - STL\u5bb9\u5668 21.3 - STL\u8fed\u4ee3\u5668 21.4 - STL\u7b97\u6cd5 storage duration 6.3 - \u5c40\u90e8\u53d8\u91cf 6.4 - \u5168\u5c40\u53d8\u91cf storage-duration \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 string 11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 22.1 - std::string \u548c std::wstring 22.2 - std-string \u7684\u6784\u9020\u548c\u6790\u6784 22.3 - std-string \u7684\u957f\u5ea6\u548c\u5bb9\u91cf 22.4 - std-string \u5b57\u7b26\u8bbf\u95ee\u548cC\u5b57\u7b26\u6570\u7ec4\u8f6c\u6362 22.5 - std-string \u8d4b\u503c\u548c\u4ea4\u6362 22.6 - std-string \u8ffd\u52a0 22.7 - std-string \u63d2\u5165 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b 4.17 - std::string \u7b80\u4ecb string_view 4.18 - std::string_view \u7b80\u4ecb strong-except \u5f3a\u5f02\u5e38\u4fdd\u8bc1 struct 10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 10.8 - \u7ed3\u6784\u4f53\u4f20\u9012\u53ca\u5176\u4ed6 10.9 - \u57fa\u4e8e\u6307\u9488\u548c\u5f15\u7528\u7684\u6210\u5458\u9009\u62e9 struct-padding \u7ed3\u6784\u4f53\u586b\u5145 subscript 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 subscripts \u4e0b\u6807 summary 1.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - C++\u57fa\u7840 10.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u679a\u4e3e\u548c\u7ed3\u6784\u4f53 10.y - \u5b66\u4f1a\u4f7f\u7528C++\u53c2\u8003\u624b\u518c 11.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5206\u914d 12.8 - lambda \u95ed\u5305 12.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570 13.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u9762\u5411\u5bf9\u8c61\u57fa\u7840 14.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26\u91cd\u8f7d 16.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5bf9\u8c61\u5173\u7cfb 17.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7ee7\u627f 18.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u865a\u51fd\u6570 19.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6a21\u677f\u548c\u7c7b 2.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570\u4e0e\u6587\u4ef6 20.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f02\u5e38 3.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4ee3\u7801\u8c03\u8bd5 4.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u57fa\u672c\u6570\u636e\u7c7b\u578b 5.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 7.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406 8.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d 9.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f15\u7528\u548c\u6307\u9488 M.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u79fb\u52a8\u548c\u667a\u80fd\u6307\u9488 super-class \u8d85\u7c7b switch 7.4 - switch \u8bed\u53e5\u57fa\u7840 7.5 - switch fallthrough\u5c5e\u6027\u548c\u4f5c\u7528\u57df symbolic constants 11.10 - C \u98ce\u683c\u5b57\u7b26\u4e32\u7b26\u53f7\u5e38\u91cf symbolic-constants \u7b26\u53f7\u5e38\u91cf template class specialization \u7c7b\u6a21\u677f\u7279\u5316 testing 7.12 - \u4ee3\u7801\u6d4b\u8bd5 the-diamond-problem \u83f1\u5f62\u7ee7\u627f\u95ee\u9898 this 13.10 - \u9690\u85cf\u7684this\u6307\u9488 translation \u7ffb\u8bd1 type 10.1 - \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7b80\u4ecb type conversion 4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 type deduction 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u63a8\u65ad type-aliases \u7c7b\u578b\u522b\u540d type-conversion \u7c7b\u578b\u8f6c\u6362 type-definition \u7c7b\u578b\u5b9a\u4e49 type-inference \u7c7b\u578b\u63a8\u65ad type-safe \u7c7b\u578b\u5b89\u5168 type-signature \u7c7b\u578b\u7b7e\u540d typecast 14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26 typedef 8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d typeid typeid \u8fd0\u7b97\u7b26 typeinfo 8.4 - \u7b97\u672f\u8f6c\u6362 unary 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 undefined behavior 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 4.4 - \u6709\u7b26\u53f7\u6574\u578b undefined-behavior \u672a\u5b9a\u4e49\u884c\u4e3a uniform initialization \u7edf\u4e00\u521d\u59cb\u5316 unmanned namespace 6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4 unnamed-namespace \u533f\u540d\u547d\u540d\u7a7a\u95f4 unscoped enumerations 10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b 10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa unscoped-enumerations \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b upcasting \u5411\u4e0a\u8f6c\u6362 using 6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4 using-declaration using \u58f0\u660e using-directive using \u6307\u4ee4 value-category \u503c\u7c7b\u578b value-container \u503c\u5bb9\u5668 value-initialization \u503c\u521d\u59cb\u5316 value-preserving \u503c\u4fdd\u7559 variable 1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 variable shadowing 6.5 - \u53d8\u91cf\u906e\u853d vector 11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a virtual functions 18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001 virtual-assignment 18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316 virtual-base-class \u865a\u57fa\u7c7b virtual-base-classes 18.8 - \u865a\u57fa\u7c7b 18.9 - \u5bf9\u8c61\u5207\u7247 virtual-destructor \u865a\u6790\u6784\u51fd\u6570 virtual-destructors 18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316 virtual-function \u865a\u51fd\u6570 virtual-table 18.6 - \u865a\u51fd\u6570\u8868 \u865a\u8868 void 11.14 \u2014 void \u6307\u9488 2.3 - void \u51fd\u6570 4.2 - Void void pointer void \u6307\u9488 weak_ptr M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898 while 7.7 - \u5faa\u73af\u548c while \u8bed\u53e5 wild-pointer \u91ce\u6307\u9488 word \u5b57\u957f wstring 22.1 - std::string \u548c std::wstring zero-initialization 0\u521d\u59cb\u5316 \u51fd\u6570\u539f\u578b(function prototype) \u51fd\u6570\u539f\u578b \u521d\u59cb\u5316 \u521d\u59cb\u5316 \u521d\u59cb\u5316\u503c(initializer) \u521d\u59cb\u5316\u503c \u52a8\u6001\u6570\u7ec4 \u52a8\u6001\u6570\u7ec4 \u5bf9\u8c61(objec) \u5bf9\u8c61 \u62bd\u8c61\u7c7b \u62bd\u8c61\u7c7b \u62ec\u53f7\u521d\u59cb\u5316 None \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u6784\u9020\u51fd\u6570 \u6784\u9020\u51fd\u6570 \u6a21\u677f\u7c7b\u578b\u53c2\u6570 \u6a21\u677f\u7c7b\u578b\u53c2\u6570 \u79bb\u5f00\u4f5c\u7528\u57df \u79bb\u5f00\u4f5c\u7528\u57df \u7b80\u5355\u8f6c\u6362(trivial conversion) \u7b80\u5355\u8f6c\u6362 \u7c7b\u5b9e\u4f8b \u7c7b\u5b9e\u4f8b \u86c7\u5f62\u547d\u540d\u6cd5(snake_case) \u86c7\u5f62\u547d\u540d\u6cd5 \u8bcd\u6c47\u8868 \u5173\u8054 \u5173\u8054\u5bb9\u5668 \u7ed1\u5b9a C \u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362 \u65e9\u671f\u7ed1\u5b9a \u5c01\u88c5 \u8868\u8fbe\u5f0f\u53c2\u6570 \u7f1d\u5408\u602a\u5bf9\u8c61 \u540e\u671f\u7ed1\u5b9a \u591a\u91cd\u7ee7\u627f \u540d\u79f0\u4fee\u9970 \u5dee\u4e00\u9519\u8bef \u8f93\u51fa\u64cd\u7eb5\u5668 \u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09 \u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f(RTTI) \u987a\u5e8f\u5bb9\u5668 \u6a21\u677f\u7279\u5316 \u7f29\u5199\u51fd\u6570\u6a21\u677f \u62bd\u8c61\u7c7b \u62bd\u8c61 \u6210\u5458\u8bbf\u95ee\u51fd\u6570 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u53d6\u5730\u5740\u64cd\u4f5c\u7b26 & \u805a\u5408\u6570\u636e\u7c7b\u578b \u805a\u5408\u521d\u59cb\u5316 \u805a\u5408 \u4e0d\u660e\u786e\u5339\u914d \u533f\u540d\u5bf9\u8c61 \u5b9e\u53c2 \u5143\u6570 \u81ea\u52a8\u5185\u5b58\u5206\u914d \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u57fa\u7c7b \u4f4d None \u6309\u4f4d\u6216 bss\u6bb5 \u5b57\u8282 \u8c03\u7528\u6808 \u53ef\u8c03\u7528\u5bf9\u8c61 \u56de\u8c03\u51fd\u6570 \u5b50\u7c7b \u7c7b\u5b9e\u4f8b \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad \u7c7b\u6a21\u677f \u7c7b\u7c7b\u578b \u95ed\u5305 \u4ee3\u7801\u6bb5 \u7f16\u8bd1\u5668 \u7ec4\u5408\u7c7b\u578b const \u7c7b\u578b\u8f6c\u6362 const \u6210\u5458\u51fd\u6570 \u5e38\u91cf\u6307\u9488 \u5e38\u91cf\u8868\u8fbe\u5f0f \u6784\u9020\u51fd\u6570 \u5bb9\u5668\u9002\u914d\u5668 \u5bb9\u5668\u7c7b \u8f6c\u6362\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u521d\u59cb\u5316 \u62f7\u8d1d\u8bed\u4e49 \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b \u60ac\u5782 \u6570\u636e\u6bb5 \u6df1\u62f7\u8d1d \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 \u9ed8\u8ba4\u521d\u59cb\u5316 \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c \u59d4\u6258\u6784\u9020\u51fd\u6570 \u4f9d\u8d56 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u6d3e\u751f\u7c7b \u6307\u5b9a\u521d\u59cb\u5316 \u6790\u6784\u51fd\u6570 \u76f4\u63a5\u521d\u59cb\u5316 \u5411\u4e0b\u8f6c\u6362 \u7b28\u6307\u9488 \u52a8\u6001\u6570\u7ec4 \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u52a8\u6001\u521d\u59cb\u5316 \u52a8\u6001\u5185\u5b58\u5206\u914d \u679a\u4e3e\u7c7b \u6c42\u503c \u5f02\u5e38\u5b89\u5168\u95ee\u9898 \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u8868\u8fbe\u5f0f\u7c7b\u578b \u5916\u90e8\u94fe\u63a5 \u5916\u90e8\u53d8\u91cf \u63d0\u53d6\u8fd0\u7b97\u7b26 \u8d2f\u7a7f\u5c5e\u6027 \u6587\u4ef6\u4f5c\u7528\u57df \u56fa\u5b9a\u6570\u7ec4 \u683c\u5f0f\u7ec4 \u53cb\u5143\u7c7b \u53cb\u5143\u51fd\u6570 \u51fd\u6570\u5b9e\u4f8b \u51fd\u6570\u5b57\u9762\u91cf \u51fd\u6570\u6307\u9488 \u51fd\u6570\u539f\u578b \u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316 \u51fd\u6570\u6a21\u677f \u6cdb\u578b\u7b97\u6cd5 \u79bb\u5f00\u4f5c\u7528\u57df \u5806 \u8eab\u4efd\u7279\u5f81 \u5373\u65f6\u51fd\u6570 \u9690\u5f0f\u6784\u9020\u51fd\u6570 \u9690\u5f0f\u5bf9\u8c61 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u7ee7\u627f \u521d\u59cb\u5316 \u521d\u59cb\u5316\u503c\u5217\u8868 \u521d\u59cb\u5316\u503c \u5185\u8054\u51fd\u6570 \u5185\u8054\u547d\u540d\u7a7a\u95f4 \u5185\u8054\u53d8\u91cf \u8f93\u5165\u64cd\u7eb5\u5668 \u63d2\u5165\u8fd0\u7b97\u7b26 \u5b9e\u4f8b \u6574\u578b\u7c7b\u578b \u63a5\u53e3\u7c7b \u5185\u90e8\u94fe\u63a5 \u5185\u90e8\u53d8\u91cf \u904d\u5386 \u8fed\u4ee3\u5668 \u533f\u540d\u51fd\u6570 \u94fe\u63a5 \u94fe\u63a5\u5c5e\u6027 \u7c7b\u522b\u8d4b\u503c\u8fd0\u7b97\u7b26 \u5217\u8868\u6784\u9020\u51fd\u6570 \u5217\u8868\u521d\u59cb\u5316 \u5b57\u9762\u91cf \u5de6\u503c\u5f15\u7528 \u5de6\u503c \u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26 \u6210\u5458\u51fd\u6570 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 \u6210\u5458\u7c7b\u578b \u6210\u5458\u53d8\u91cf \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \u6210\u5458\u4f9d\u6b21\u8d4b\u503c \u6210\u5458\u4f9d\u6b21\u62f7\u8d1d \u5185\u5b58\u6cc4\u6f0f None \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 \u79fb\u52a8\u6784\u9020\u51fd\u6570 \u79fb\u52a8\u8bed\u4e49 \u5177\u540d\u7c7b\u578b\u8f6c\u6362 \u547d\u540d\u7a7a\u95f4 \u7f29\u7a84\u8f6c\u6362 \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u6570\u503c\u63d0\u5347 \u6570\u503c\u8f6c\u6362 \u5bf9\u8c61\u7ec4\u5408 \u5bf9\u8c61\u5207\u7247 \u5bf9\u8c61 \u64cd\u4f5c\u6570 \u8fd0\u7b97\u7b26 \u8d85\u51fa\u4f5c\u7528\u57df \u91cd\u8f7d\u89e3\u6790 \u91cd\u8f7d \u91cd\u5199 \u7236\u7c7b \u6309\u5730\u5740\u4f20\u9012 \u6309\u5f15\u7528\u4f20\u9012 \u5360\u4f4d\u7b26\u7c7b\u578b \u6307\u5411\u5e38\u91cf\u7684\u6307\u9488 \u591a\u6001 \u79c1\u6709\u7ee7\u627f \u79c1\u6709\u6210\u5458 \u53d7\u4fdd\u62a4\u7ee7\u627f \u53d7\u4fdd\u62a4\u6210\u5458 \u516c\u5f00\u7ee7\u627f \u516c\u6709\u6210\u5458 \u7eaf\u865a\u51fd\u6570 \u968f\u673a\u8bbf\u95ee\u5185\u5b58 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u539f\u59cb\u6307\u9488 \u5f15\u7528\u5bb9\u5668 \u53cd\u8eab\u5173\u8054 \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 \u6309\u5730\u5740\u8fd4\u56de \u6309\u5f15\u7528\u8fd4\u56de \u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad \u820d\u5165\u8bef\u5dee \u53f3\u503c\u5f15\u7528 \u53f3\u503c \u6307\u9488\u7f29\u653e \u6709\u4f5c\u7528\u57df\u679a\u4e3e \u5185\u5b58\u6bb5 \u54e8\u5175 \u53d8\u91cf\u906e\u853d \u6d45\u62f7\u8d1d \u526f\u4f5c\u7528 \u7b7e\u540d \u667a\u80fd\u6307\u9488\u7c7b \u667a\u80fd\u6307\u9488 \u86c7\u5f62\u547d\u540d\u6cd5 \u6808\u5e27 \u5806\u6808\u6ea2\u51fa \u6808 \u6807\u51c6\u6d41 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u9759\u6001\u521d\u59cb\u5316 \u9759\u6001\u6210\u5458\u51fd\u6570 \u9759\u6001\u6210\u5458\u53d8\u91cf \u9759\u6001\u5185\u5b58\u5206\u914d \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u9759\u6001\u53d8\u91cf \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u5f3a\u5f02\u5e38\u4fdd\u8bc1 \u7ed3\u6784\u4f53\u586b\u5145 \u4e0b\u6807 \u8d85\u7c7b \u8bed\u6cd5 \u7c7b\u6a21\u677f\u7279\u5316 \u6a21\u677f\u7c7b\u578b\u53c2\u6570 \u83f1\u5f62\u7ee7\u627f\u95ee\u9898 \u7ffb\u8bd1 \u7b80\u5355\u8f6c\u6362 \u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u522b\u540d \u7c7b\u578b\u8f6c\u6362 \u7c7b\u578b\u5b9a\u4e49 \u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u5b89\u5168 \u7c7b\u578b\u7b7e\u540d typeid \u8fd0\u7b97\u7b26 \u672a\u5b9a\u4e49\u884c\u4e3a \u7edf\u4e00\u521d\u59cb\u5316 \u533f\u540d\u547d\u540d\u7a7a\u95f4 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u5411\u4e0a\u8f6c\u6362 using \u58f0\u660e using \u6307\u4ee4 \u503c\u7c7b\u578b \u503c\u5bb9\u5668 \u503c\u521d\u59cb\u5316 \u503c\u4fdd\u7559 \u865a\u57fa\u7c7b \u865a\u6790\u6784\u51fd\u6570 \u865a\u51fd\u6570 \u865a\u8868 void \u6307\u9488 \u91ce\u6307\u9488 \u5b57\u957f 0\u521d\u59cb\u5316 None \u8bed\u6cd5(syntax) \u8bed\u6cd5 \u8d85\u51fa\u4f5c\u7528\u57df(out of scope) \u8d85\u51fa\u4f5c\u7528\u57df \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316","title":"\u7d22\u5f15"},{"location":"tags/#tags","text":"","title":"Tags"},{"location":"tags/#associations","text":"\u5173\u8054","title":"Associations"},{"location":"tags/#associative-containers","text":"\u5173\u8054\u5bb9\u5668","title":"Associative-containers"},{"location":"tags/#binding","text":"\u7ed1\u5b9a","title":"Binding"},{"location":"tags/#c11","text":"11.13 - for-each \u5faa\u73af 4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb 4.11 - \u5b57\u7b26 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 M.7 \u2014 std::shared_ptr","title":"C++11"},{"location":"tags/#c14","text":"12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 4.15 - \u5b57\u9762\u91cf 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad M.6 \u2014 std::unique_ptr M.7 \u2014 std::shared_ptr","title":"C++14"},{"location":"tags/#c17","text":"10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 11.16 \u2014 \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb 11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 4.18 - std::string_view \u7b80\u4ecb 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 7.17 - \u65ad\u8a00\u548c static_assert M.6 \u2014 std::unique_ptr M.7 \u2014 std::shared_ptr","title":"C++17"},{"location":"tags/#c20","text":"10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 11.13 - for-each \u5faa\u73af 11.19 \u2014 \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 6.14 - Constexpr \u548c consteval \u51fd\u6570 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad M.7 \u2014 std::shared_ptr","title":"C++20"},{"location":"tags/#c-style-casts","text":"C \u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362","title":"C-style-casts"},{"location":"tags/#ctad","text":"10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD","title":"CTAD"},{"location":"tags/#early-binding","text":"\u65e9\u671f\u7ed1\u5b9a","title":"Early binding"},{"location":"tags/#encapsulation","text":"\u5c01\u88c5","title":"Encapsulation"},{"location":"tags/#expression-parameters","text":"\u8868\u8fbe\u5f0f\u53c2\u6570","title":"Expression-parameters"},{"location":"tags/#frankenobject","text":"\u7f1d\u5408\u602a\u5bf9\u8c61","title":"Frankenobject"},{"location":"tags/#function-templates","text":"8.13 - \u51fd\u6570\u6a21\u677f","title":"Function templates"},{"location":"tags/#late-binding","text":"\u540e\u671f\u7ed1\u5b9a","title":"Late binding"},{"location":"tags/#lvalue","text":"9.3 - \u5de6\u503c\u5f15\u7528 9.5 - \u4f20\u9012\u5de6\u503c\u5f15\u7528","title":"Lvalue"},{"location":"tags/#multiple-inheritance","text":"\u591a\u91cd\u7ee7\u627f","title":"Multiple inheritance"},{"location":"tags/#name-mangling","text":"\u540d\u79f0\u4fee\u9970","title":"Name-mangling"},{"location":"tags/#numeral-systems","text":"4.16 - \u6570\u503c\u7cfb\u7edf\uff08\u5341\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u548c\u516b\u8fdb\u5236\uff09","title":"Numeral systems"},{"location":"tags/#off-by-one","text":"\u5dee\u4e00\u9519\u8bef","title":"Off-by-one"},{"location":"tags/#output-manipulators","text":"\u8f93\u51fa\u64cd\u7eb5\u5668","title":"Output-manipulators"},{"location":"tags/#pointer-arithmetic","text":"11.9 - \u6307\u9488\u8fd0\u7b97\u548c\u6570\u7ec4\u7d22\u5f15","title":"Pointer arithmetic"},{"location":"tags/#r-value-references","text":"M.2 - \u53f3\u503c\u5f15\u7528 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c","title":"R-value-references"},{"location":"tags/#raii-resource-acquisition-is-initialization","text":"\u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09","title":"RAII (Resource Acquisition Is Initialization)"},{"location":"tags/#run-time-type-information-rtti","text":"\u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f(RTTI)","title":"Run-time-type-information-RTTI"},{"location":"tags/#sequence-containers","text":"\u987a\u5e8f\u5bb9\u5668","title":"Sequence-containers"},{"location":"tags/#syntax","text":"3.1 - \u8bed\u6cd5\u548c\u8bed\u4e49\u9519\u8bef","title":"Syntax"},{"location":"tags/#template-specialization","text":"\u6a21\u677f\u7279\u5316","title":"Template-specialization"},{"location":"tags/#abbreviated-function-templates","text":"\u7f29\u5199\u51fd\u6570\u6a21\u677f","title":"abbreviated function templates"},{"location":"tags/#abstract-base-classes","text":"18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b","title":"abstract-base-classes"},{"location":"tags/#abstraction","text":"\u62bd\u8c61","title":"abstraction"},{"location":"tags/#access-specifier","text":"13.3 - \u8bbf\u95ee\u4fee\u9970\u7b26 public \u548c private","title":"access specifier"},{"location":"tags/#access-function","text":"\u6210\u5458\u8bbf\u95ee\u51fd\u6570","title":"access-function"},{"location":"tags/#access-specifiers","text":"17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26","title":"access-specifiers"},{"location":"tags/#address","text":"9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de 9.9 - \u6309\u5730\u5740\u4f20\u9012","title":"address"},{"location":"tags/#address-of-operator","text":"\u53d6\u5730\u5740\u64cd\u4f5c\u7b26 &","title":"address-of-operator"},{"location":"tags/#aggregate-initialization","text":"10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316","title":"aggregate initialization"},{"location":"tags/#aggregate-data-type","text":"\u805a\u5408\u6570\u636e\u7c7b\u578b","title":"aggregate-data-type"},{"location":"tags/#aggregate-initialization_1","text":"\u805a\u5408\u521d\u59cb\u5316","title":"aggregate-initialization"},{"location":"tags/#aggregation","text":"16.3 - \u805a\u5408\u5173\u7cfb \u805a\u5408","title":"aggregation"},{"location":"tags/#algorithms","text":"11.19 \u2014 \u6807\u51c6\u5e93\u7b97\u6cd5\u7b80\u4ecb","title":"algorithms"},{"location":"tags/#alias","text":"8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d","title":"alias"},{"location":"tags/#ambiguous-match","text":"\u4e0d\u660e\u786e\u5339\u914d","title":"ambiguous-match"},{"location":"tags/#anonymous-object","text":"\u533f\u540d\u5bf9\u8c61","title":"anonymous-object"},{"location":"tags/#argument","text":"12.5 - \u547d\u4ee4\u884c\u53c2\u6570 2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2","title":"argument"},{"location":"tags/#arguments","text":"\u5b9e\u53c2","title":"arguments"},{"location":"tags/#arithmetic-conversions","text":"8.4 - \u7b97\u672f\u8f6c\u6362","title":"arithmetic conversions"},{"location":"tags/#arity","text":"\u5143\u6570","title":"arity"},{"location":"tags/#array","text":"11.1 - \u6570\u7ec4\uff08\u7b2c\u4e00\u90e8\u5206\uff09 11.2 - \u6570\u7ec4\uff08\u7b2c\u4e8c\u90e8\u5206\uff09 11.3 - \u6570\u7ec4\u548c\u5faa\u73af 11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f 11.5 - \u591a\u7ef4\u6570\u7ec4 11.8 - \u6307\u9488\u548c\u6570\u7ec4","title":"array"},{"location":"tags/#arrays","text":"11.12 - \u52a8\u6001\u5206\u914d\u6570\u7ec4","title":"arrays"},{"location":"tags/#assert","text":"7.17 - \u65ad\u8a00\u548c static_assert","title":"assert"},{"location":"tags/#assignment","text":"14.15 - \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26","title":"assignment"},{"location":"tags/#association","text":"16.4 - \u5173\u8054\u5173\u7cfb","title":"association"},{"location":"tags/#auto","text":"8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad","title":"auto"},{"location":"tags/#automatic-memory-allocation","text":"\u81ea\u52a8\u5185\u5b58\u5206\u914d","title":"automatic-memory-allocation"},{"location":"tags/#automatic-storage-duration","text":"\u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4","title":"automatic-storage-duration"},{"location":"tags/#base-class","text":"\u57fa\u7c7b","title":"base-class"},{"location":"tags/#bit","text":"O.1 - bit\u6807\u8bb0\u548c\u901a\u8fc7std::bitset\u64cd\u4f5cbit O.2 - \u4f4d\u8fd0\u7b97\u7b26 O.3 - \u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u548c\u63a9\u7801\u64cd\u4f5c\u4f4d O.4 - \u4e8c\u8fdb\u5236\u548c\u5341\u8fdb\u5236\u8f6c\u6362 \u4f4d","title":"bit"},{"location":"tags/#bitwise-and","text":"None","title":"bitwise-and"},{"location":"tags/#bitwise-or","text":"\u6309\u4f4d\u6216","title":"bitwise-or"},{"location":"tags/#bool","text":"4.9 - \u5e03\u5c14\u503c","title":"bool"},{"location":"tags/#boolean","text":"4.10 - if \u8bed\u53e5\u7b80\u4ecb","title":"boolean"},{"location":"tags/#bss-segment","text":"bss\u6bb5","title":"bss segment"},{"location":"tags/#byte","text":"\u5b57\u8282","title":"byte"},{"location":"tags/#c-style","text":"11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32","title":"c-style"},{"location":"tags/#call-stack","text":"3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808","title":"call stack"},{"location":"tags/#call-stack_1","text":"\u8c03\u7528\u6808","title":"call-stack"},{"location":"tags/#callable-object","text":"\u53ef\u8c03\u7528\u5bf9\u8c61","title":"callable-object"},{"location":"tags/#callback-functions","text":"\u56de\u8c03\u51fd\u6570","title":"callback functions"},{"location":"tags/#char","text":"4.11 - \u5b57\u7b26","title":"char"},{"location":"tags/#child-class","text":"\u5b50\u7c7b","title":"child-class"},{"location":"tags/#circular-dependency","text":"M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898","title":"circular dependency"},{"location":"tags/#class","text":"13.11 - \u7c7b\u4ee3\u7801\u548c\u5934\u6587\u4ef6 13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570 13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf 13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570 13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b 13.16 - \u533f\u540d\u5bf9\u8c61 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b 13.18 - \u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 13.2 - \u7c7b\u548c\u7c7b\u6210\u5458","title":"class"},{"location":"tags/#class-template","text":"10.10 - \u7c7b\u6a21\u677f 10.11 - \u7c7b\u6a21\u677f\u53c2\u6570\u63a8\u65adCTAD","title":"class template"},{"location":"tags/#class-template_1","text":"\u7c7b\u6a21\u677f","title":"class-template"},{"location":"tags/#class-template-argument-deduction","text":"\u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad","title":"class-template-argument-deduction"},{"location":"tags/#class-type","text":"\u7c7b\u7c7b\u578b","title":"class-type"},{"location":"tags/#closure","text":"\u95ed\u5305","title":"closure"},{"location":"tags/#code-coverage","text":"7.13 - \u4ee3\u7801\u8986\u76d6\u7387","title":"code coverage"},{"location":"tags/#code-segment","text":"\u4ee3\u7801\u6bb5","title":"code segment"},{"location":"tags/#comments","text":"1.2 - \u6ce8\u91ca","title":"comments"},{"location":"tags/#compile-time","text":"\u7f16\u8bd1\u65f6","title":"compile-time"},{"location":"tags/#compiler","text":"\u7f16\u8bd1\u5668","title":"compiler"},{"location":"tags/#composite-types","text":"\u7ec4\u5408\u7c7b\u578b","title":"composite-types"},{"location":"tags/#composition","text":"16.2 - \u7ec4\u5408\u5173\u7cfb","title":"composition"},{"location":"tags/#compound-data-types","text":"9.1 - \u590d\u5408\u6570\u636e\u7c7b\u578b","title":"compound data types"},{"location":"tags/#const","text":"4.13 - const \u53d8\u91cf\u548c\u7b26\u53f7\u5e38\u91cf 4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad 9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528 9.8 - \u6307\u9488\u548cconst","title":"const"},{"location":"tags/#const-class","text":"13.12 - const \u5bf9\u8c61\u548c\u6210\u5458\u51fd\u6570","title":"const class"},{"location":"tags/#const-cast","text":"const \u7c7b\u578b\u8f6c\u6362","title":"const-cast"},{"location":"tags/#const-member-function","text":"const \u6210\u5458\u51fd\u6570","title":"const-member-function"},{"location":"tags/#const-pointer","text":"\u5e38\u91cf\u6307\u9488","title":"const-pointer"},{"location":"tags/#constant-expression","text":"\u5e38\u91cf\u8868\u8fbe\u5f0f","title":"constant expression"},{"location":"tags/#constants","text":"4.15 - \u5b57\u9762\u91cf","title":"constants"},{"location":"tags/#consteval","text":"6.14 - Constexpr \u548c consteval \u51fd\u6570","title":"consteval"},{"location":"tags/#constexpr","text":"4.14 - \u7f16\u8bd1\u65f6\u5e38\u91cf\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr 6.14 - Constexpr \u548c consteval \u51fd\u6570","title":"constexpr"},{"location":"tags/#constructor","text":"13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570 14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570","title":"constructor"},{"location":"tags/#constructors","text":"13.5 - \u6784\u9020\u51fd\u6570 13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868","title":"constructors"},{"location":"tags/#container-classes","text":"16.6 - \u5bb9\u5668\u7c7b","title":"container classes"},{"location":"tags/#container-adapter","text":"\u5bb9\u5668\u9002\u914d\u5668","title":"container-adapter"},{"location":"tags/#container-class","text":"\u5bb9\u5668\u7c7b","title":"container-class"},{"location":"tags/#containers","text":"21.2 - STL\u5bb9\u5668 21.3 - STL\u8fed\u4ee3\u5668 21.4 - STL\u7b97\u6cd5","title":"containers"},{"location":"tags/#control-flow","text":"7.1 - \u63a7\u5236\u6d41 7.4 - switch \u8bed\u53e5\u57fa\u7840","title":"control flow"},{"location":"tags/#conversions","text":"8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347 8.3 - \u6570\u503c\u8f6c\u6362 8.4 - \u7b97\u672f\u8f6c\u6362 8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast","title":"conversions"},{"location":"tags/#converting-constructors","text":"14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57","title":"converting constructors"},{"location":"tags/#converting-constructors_1","text":"\u8f6c\u6362\u6784\u9020\u51fd\u6570","title":"converting-constructors"},{"location":"tags/#copy-constructor","text":"14.12 - \u62f7\u8d1d\u6784\u9020\u51fd\u6570","title":"copy constructor"},{"location":"tags/#copy-initialization","text":"14.13 - \u62f7\u8d1d\u521d\u59cb\u5316","title":"copy initialization"},{"location":"tags/#copy-constructor_1","text":"\u62f7\u8d1d\u6784\u9020\u51fd\u6570","title":"copy-constructor"},{"location":"tags/#copy-constructors","text":"\u62f7\u8d1d\u6784\u9020\u51fd\u6570","title":"copy-constructors"},{"location":"tags/#copy-initialization_1","text":"\u62f7\u8d1d\u521d\u59cb\u5316","title":"copy-initialization"},{"location":"tags/#copy-semantics","text":"\u62f7\u8d1d\u8bed\u4e49","title":"copy-semantics"},{"location":"tags/#covariant","text":"\u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b","title":"covariant"},{"location":"tags/#covariant-return-types","text":"18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b","title":"covariant-return-types"},{"location":"tags/#dangling","text":"\u60ac\u5782","title":"dangling"},{"location":"tags/#data-segment","text":"\u6570\u636e\u6bb5","title":"data segment"},{"location":"tags/#data-type","text":"4.1 - \u57fa\u7840\u6570\u636e\u7c7b\u578b\u7b80\u4ecb 4.4 - \u6709\u7b26\u53f7\u6574\u578b 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t 4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5 4.8 - \u6d6e\u70b9\u6570 4.9 - \u5e03\u5c14\u503c 8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad","title":"data type"},{"location":"tags/#debugger","text":"3.6 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u5355\u6b65\u8c03\u8bd5 3.7 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8fd0\u884c\u548c\u65ad\u70b9 3.8 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u76d1\u63a7\u53d8\u91cf 3.9 - \u4f7f\u7528\u96c6\u6210\u8c03\u8bd5\u5668\u4e4b\u8c03\u7528\u6808","title":"debugger"},{"location":"tags/#debugging","text":"3.2 - \u4ee3\u7801\u8c03\u8bd5\u6b65\u9aa4 3.3 - \u4ee3\u7801\u8c03\u8bd5\u7b56\u7565 3.4 \u2014 \u57fa\u672c\u4ee3\u7801\u8c03\u8bd5\u6280\u672f 3.5 - \u66f4\u591a\u4ee3\u7801\u8c03\u8bd5\u6280\u672f","title":"debugging"},{"location":"tags/#declaration","text":"6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e \u58f0\u660e \u7eaf\u58f0\u660e(pure-declaration)","title":"declaration"},{"location":"tags/#deep-copy","text":"14.16 - \u6d45\u62f7\u8d1d\u548c\u6df1\u62f7\u8d1d 14.17 - \u91cd\u8f7d\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u6a21\u677f","title":"deep copy"},{"location":"tags/#deep-copy_1","text":"\u6df1\u62f7\u8d1d","title":"deep-copy"},{"location":"tags/#default","text":"8.12 - \u9ed8\u8ba4\u53c2\u6570","title":"default"},{"location":"tags/#default-constructor","text":"\u9ed8\u8ba4\u6784\u9020\u51fd\u6570","title":"default-constructor"},{"location":"tags/#default-initialization","text":"\u9ed8\u8ba4\u521d\u59cb\u5316","title":"default-initialization"},{"location":"tags/#default-member-initializer","text":"\u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c","title":"default-member-initializer"},{"location":"tags/#definition","text":"\u5b9a\u4e49","title":"definition"},{"location":"tags/#delegating","text":"13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570","title":"delegating"},{"location":"tags/#delegating-constructors","text":"\u59d4\u6258\u6784\u9020\u51fd\u6570","title":"delegating-constructors"},{"location":"tags/#delete","text":"11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d 14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57","title":"delete"},{"location":"tags/#dependencies","text":"16.5 - \u4f9d\u8d56\u5173\u7cfb","title":"dependencies"},{"location":"tags/#dependency","text":"\u4f9d\u8d56","title":"dependency"},{"location":"tags/#dereference-operator","text":"\u89e3\u5f15\u7528\u8fd0\u7b97\u7b26","title":"dereference-operator"},{"location":"tags/#derived-class","text":"\u6d3e\u751f\u7c7b","title":"derived-class"},{"location":"tags/#desgin","text":"2.13 - \u5982\u4f55\u8bbe\u8ba1\u7b2c\u4e00\u4e2a\u7a0b\u5e8f","title":"desgin"},{"location":"tags/#designated-initializers","text":"\u6307\u5b9a\u521d\u59cb\u5316","title":"designated-initializers"},{"location":"tags/#destructor","text":"\u6790\u6784\u51fd\u6570","title":"destructor"},{"location":"tags/#destructors","text":"13.9 - \u6790\u6784\u51fd\u6570","title":"destructors"},{"location":"tags/#direct-initialization","text":"\u76f4\u63a5\u521d\u59cb\u5316","title":"direct-initialization"},{"location":"tags/#do-while","text":"7.8 - do-while \u8bed\u53e5","title":"do-while"},{"location":"tags/#downcasting","text":"\u5411\u4e0b\u8f6c\u6362","title":"downcasting"},{"location":"tags/#dumb-pointer","text":"\u7b28\u6307\u9488","title":"dumb-pointer"},{"location":"tags/#dynamic-casting","text":"18.10 - \u52a8\u6001\u7c7b\u578b\u8f6c\u6362","title":"dynamic casting"},{"location":"tags/#dynamic-duration","text":"\u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4","title":"dynamic duration"},{"location":"tags/#dynamic-casts","text":"\u52a8\u6001\u7c7b\u578b\u8f6c\u6362","title":"dynamic-casts"},{"location":"tags/#dynamic-initialization","text":"\u52a8\u6001\u521d\u59cb\u5316","title":"dynamic-initialization"},{"location":"tags/#dynamic-memory","text":"11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d","title":"dynamic-memory"},{"location":"tags/#dynamic-memory-allocation","text":"\u52a8\u6001\u5185\u5b58\u5206\u914d","title":"dynamic-memory-allocation"},{"location":"tags/#early-binding_1","text":"18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a","title":"early-binding"},{"location":"tags/#ellipsis","text":"12.6 - \u7701\u7565\u53f7\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83","title":"ellipsis"},{"location":"tags/#encapsulation_1","text":"13.4 - \u6210\u5458\u8bbf\u95ee\u51fd\u6570\u4e0e\u5c01\u88c5","title":"encapsulation"},{"location":"tags/#enum","text":"10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b 10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa 10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09","title":"enum"},{"location":"tags/#enum-class","text":"\u679a\u4e3e\u7c7b","title":"enum-class"},{"location":"tags/#error-handling","text":"7.15 - \u53d1\u73b0\u9519\u8bef\u548c\u5904\u7406\u9519\u8bef 7.16 - std::in \u548c\u8f93\u5165\u9519\u8bef\u5904\u7406","title":"error handling"},{"location":"tags/#evaluation","text":"\u6c42\u503c","title":"evaluation"},{"location":"tags/#excalidraw","text":"Drawing 2023 02 11 13.33.50.excalidraw Template.excalidraw C++ basic onepage Moc onepage","title":"excalidraw"},{"location":"tags/#exception-safety-issue","text":"\u5f02\u5e38\u5b89\u5168\u95ee\u9898","title":"exception-safety-issue"},{"location":"tags/#explicit","text":"14.14 - \u8f6c\u6362\u6784\u9020\u51fd\u6570\u4e0eexplicit\u548cdelete\u5173\u952e\u5b57","title":"explicit"},{"location":"tags/#explicit-type-conversion","text":"8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362","title":"explicit-type-conversion"},{"location":"tags/#expression","text":"1.10 - \u8868\u8fbe\u5f0f\u7b80\u4ecb","title":"expression"},{"location":"tags/#expression-type","text":"\u8868\u8fbe\u5f0f\u7c7b\u578b","title":"expression type"},{"location":"tags/#external-linkage","text":"\u5916\u90e8\u94fe\u63a5","title":"external-linkage"},{"location":"tags/#external-variable","text":"\u5916\u90e8\u53d8\u91cf","title":"external-variable"},{"location":"tags/#extraction-operator","text":"\u63d0\u53d6\u8fd0\u7b97\u7b26","title":"extraction-operator"},{"location":"tags/#fallthrough","text":"\u8d2f\u7a7f\u5c5e\u6027","title":"fallthrough"},{"location":"tags/#file","text":"23.6 - \u57fa\u672c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa","title":"file"},{"location":"tags/#file-scope","text":"\u6587\u4ef6\u4f5c\u7528\u57df","title":"file-scope"},{"location":"tags/#final","text":"18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b","title":"final"},{"location":"tags/#fixed-array","text":"\u56fa\u5b9a\u6570\u7ec4","title":"fixed-array"},{"location":"tags/#fixed-width","text":"4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t","title":"fixed-width"},{"location":"tags/#float","text":"4.8 - \u6d6e\u70b9\u6570","title":"float"},{"location":"tags/#float-point","text":"5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83","title":"float point"},{"location":"tags/#for","text":"7.7 - \u5faa\u73af\u548c while \u8bed\u53e5 7.9 - for \u8bed\u53e5","title":"for"},{"location":"tags/#for-each","text":"11.13 - for-each \u5faa\u73af","title":"for-each"},{"location":"tags/#fordward-declaration","text":"2.7 - \u524d\u5411\u58f0\u660e\u548c\u5b9a\u4e49","title":"fordward declaration"},{"location":"tags/#format-group","text":"\u683c\u5f0f\u7ec4","title":"format-group"},{"location":"tags/#formatting","text":"1.8 - \u7a7a\u767d\u7b26\u548c\u57fa\u672c\u6392\u7248","title":"formatting"},{"location":"tags/#forward-declaration","text":"\u524d\u5411\u58f0\u660e","title":"forward declaration"},{"location":"tags/#friend","text":"13.15 - \u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b 13.16 - \u533f\u540d\u5bf9\u8c61 13.17 - \u7c7b\u4e2d\u7684\u5d4c\u5957\u7c7b\u578b 13.18 - \u5bf9\u7a0b\u5e8f\u8fdb\u884c\u8ba1\u65f6 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26","title":"friend"},{"location":"tags/#friend-class","text":"\u53cb\u5143\u7c7b","title":"friend-class"},{"location":"tags/#friend-function","text":"\u53cb\u5143\u51fd\u6570","title":"friend-function"},{"location":"tags/#function","text":"12.1 - \u51fd\u6570\u6307\u9488 2.1 - \u51fd\u6570\u7b80\u4ecb 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 2.6 - \u4e3a\u4ec0\u4e48\u51fd\u6570\u6709\u7528\uff0c\u5982\u4f55\u9ad8\u6548\u4f7f\u7528\u51fd\u6570","title":"function"},{"location":"tags/#function-template","text":"8.14 - \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316 8.15 - \u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u6a21\u677f","title":"function template"},{"location":"tags/#function-instance","text":"\u51fd\u6570\u5b9e\u4f8b","title":"function-instance"},{"location":"tags/#function-literal","text":"\u51fd\u6570\u5b57\u9762\u91cf","title":"function-literal"},{"location":"tags/#function-pointer","text":"\u51fd\u6570\u6307\u9488","title":"function-pointer"},{"location":"tags/#function-template_1","text":"\u51fd\u6570\u6a21\u677f","title":"function-template"},{"location":"tags/#function-template-instantiation","text":"\u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316","title":"function-template-instantiation"},{"location":"tags/#generic-algorithm","text":"\u6cdb\u578b\u7b97\u6cd5","title":"generic-algorithm"},{"location":"tags/#global","text":"6.4 - \u5168\u5c40\u53d8\u91cf 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf","title":"global"},{"location":"tags/#global-variable","text":"6.8 - \u4e3a\u4ec0\u4e48\u975e const \u5168\u5c40\u53d8\u91cf\u662f\u9b54\u9b3c \u5168\u5c40\u53d8\u91cf","title":"global variable"},{"location":"tags/#glossary","text":"\u7f16\u8bd1\u65f6 \u58f0\u660e \u5b9a\u4e49 \u524d\u5411\u58f0\u660e \u5168\u5c40\u53d8\u91cf \u6807\u8bc6\u7b26(identifier) \u5b9e\u4f8b\u5316 \u6307\u4ee4 \u94fe\u63a5\u5668(linker) \u5b8f \u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f \u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule) \u5f62\u53c2 \u6309\u503c\u4f20\u9012 \u9884\u5904\u7406\u5668\u6307\u4ee4 \u9884\u5904\u7406\u5668 \u7eaf\u58f0\u660e(pure-declaration) \u9650\u5b9a\u7b26 \u91cd\u6784 \u6309\u503c\u8fd4\u56de \u8fd0\u884c\u65f6 \u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u4f5c\u7528\u57df \u8bed\u53e5 \u7b26\u53f7\u5e38\u91cf","title":"glossary"},{"location":"tags/#goto","text":"7.6 - goto \u8bed\u53e5","title":"goto"},{"location":"tags/#halt","text":"7.11 - \u6302\u8d77\uff08\u63d0\u524d\u7ed3\u675f\u7a0b\u5e8f\uff09","title":"halt"},{"location":"tags/#header","text":"2.11 - \u5934\u6587\u4ef6","title":"header"},{"location":"tags/#header-guards","text":"2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e","title":"header guards"},{"location":"tags/#heap","text":"12.2 - \u6808\u548c\u5806 \u5806","title":"heap"},{"location":"tags/#identifier","text":"1.7 - \u5173\u952e\u5b57\u548c\u6807\u8bc6\u7b26 \u6807\u8bc6\u7b26(identifier)","title":"identifier"},{"location":"tags/#identity","text":"\u8eab\u4efd\u7279\u5f81","title":"identity"},{"location":"tags/#if","text":"4.10 - if \u8bed\u53e5\u7b80\u4ecb 7.2 - if \u8bed\u53e5\u548c\u8bed\u53e5\u5757 7.3 - \u5e38\u89c1\u7684 if \u8bed\u53e5\u9519\u8bef","title":"if"},{"location":"tags/#immediate-functions","text":"\u5373\u65f6\u51fd\u6570","title":"immediate-functions"},{"location":"tags/#implicit-constructor","text":"\u9690\u5f0f\u6784\u9020\u51fd\u6570","title":"implicit-constructor"},{"location":"tags/#implicit-object","text":"\u9690\u5f0f\u5bf9\u8c61","title":"implicit-object"},{"location":"tags/#implicit-type-conversion","text":"\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362","title":"implicit-type-conversion"},{"location":"tags/#inheritance","text":"17.1 - \u7ee7\u627f\u7b80\u4ecb 17.2 - C++\u7ee7\u627f\u57fa\u7840 17.3 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u987a\u5e8f 17.4 - \u6d3e\u751f\u7c7b\u7684\u6784\u9020\u548c\u521d\u59cb\u5316 17.5 - \u7ee7\u627f\u548c\u8bbf\u95ee\u8bf4\u660e\u7b26 17.6 - \u4e3a\u6d3e\u751f\u7c7b\u6dfb\u52a0\u65b0\u529f\u80fd 17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570 17.9 - \u591a\u91cd\u7ee7\u627f 18.11 - \u4f7f\u7528 << \u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b \u7ee7\u627f","title":"inheritance"},{"location":"tags/#initialization","text":"1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316 1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 13.6 - \u6784\u9020\u51fd\u6570\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u62f7\u8d1d\u521d\u59cb\u5316 \u76f4\u63a5\u521d\u59cb\u5316 \u5217\u8868\u521d\u59cb\u5316","title":"initialization"},{"location":"tags/#initializer-list","text":"\u521d\u59cb\u5316\u503c\u5217\u8868","title":"initializer-list"},{"location":"tags/#initializer_list","text":"16.7 - std::initializer_list","title":"initializer_list"},{"location":"tags/#inline","text":"6.13 - \u5185\u8054\u51fd\u6570 6.9 - \u4f7f\u7528 inline \u53d8\u91cf\u5171\u4eab\u5168\u5c40\u5e38\u91cf","title":"inline"},{"location":"tags/#inline-namespace","text":"6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4","title":"inline namespace"},{"location":"tags/#inline-function","text":"\u5185\u8054\u51fd\u6570","title":"inline-function"},{"location":"tags/#inline-namespace_1","text":"\u5185\u8054\u547d\u540d\u7a7a\u95f4","title":"inline-namespace"},{"location":"tags/#inline-variables","text":"\u5185\u8054\u53d8\u91cf","title":"inline-variables"},{"location":"tags/#input-manipulator","text":"4.17 - std::string \u7b80\u4ecb","title":"input-manipulator"},{"location":"tags/#input-manipulators","text":"\u8f93\u5165\u64cd\u7eb5\u5668","title":"input-manipulators"},{"location":"tags/#insertion-operator","text":"\u63d2\u5165\u8fd0\u7b97\u7b26","title":"insertion-operator"},{"location":"tags/#instance","text":"\u5b9e\u4f8b","title":"instance"},{"location":"tags/#instantiated","text":"\u5b9e\u4f8b\u5316","title":"instantiated"},{"location":"tags/#instuction","text":"\u6307\u4ee4","title":"instuction"},{"location":"tags/#integer","text":"4.4 - \u6709\u7b26\u53f7\u6574\u578b 4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83 4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t","title":"integer"},{"location":"tags/#integral-type","text":"\u6574\u578b\u7c7b\u578b","title":"integral-type"},{"location":"tags/#interface-class","text":"\u63a5\u53e3\u7c7b","title":"interface-class"},{"location":"tags/#interface-classes","text":"18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b","title":"interface-classes"},{"location":"tags/#internal-linkage","text":"\u5185\u90e8\u94fe\u63a5","title":"internal-linkage"},{"location":"tags/#internal-variable","text":"\u5185\u90e8\u53d8\u91cf","title":"internal-variable"},{"location":"tags/#io","text":"23.1 - \u8f93\u5165\u8f93\u51fa\u6d41 23.2 - \u4f7f\u7528istream\u5904\u7406\u8f93\u5165 23.3 - \u4f7f\u7528ostream\u548cios\u5904\u7406\u8f93\u51fa 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b 23.5 - \u6d41\u72b6\u6001\u548c\u8f93\u5165\u9a8c\u8bc1 23.6 - \u57fa\u672c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa 23.7 - \u968f\u673a\u6587\u4ef6\u8f93\u5165\u8f93\u51fa","title":"io"},{"location":"tags/#iostream","text":"1.5 - iostream \u7b80\u4ecb\uff1acout\uff0ccin \u548c endl","title":"iostream"},{"location":"tags/#iterating","text":"\u904d\u5386","title":"iterating"},{"location":"tags/#iterator","text":"11.18 \u2014 \u8fed\u4ee3\u5668\u7b80\u4ecb \u8fed\u4ee3\u5668","title":"iterator"},{"location":"tags/#lambda","text":"12.7 - lambda\u8868\u8fbe\u5f0f\u7b80\u4ecb\uff08\u533f\u540d\u51fd\u6570\uff09 \u533f\u540d\u51fd\u6570","title":"lambda"},{"location":"tags/#late-binding_1","text":"18.5 - \u65e9\u671f\u7ed1\u5b9a\u548c\u540e\u671f\u7ed1\u5b9a","title":"late-binding"},{"location":"tags/#link","text":"\u94fe\u63a5","title":"link"},{"location":"tags/#linkage","text":"6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 6.3 - \u5c40\u90e8\u53d8\u91cf 6.6 - \u5185\u90e8\u94fe\u63a5 6.7 - \u5916\u90e8\u94fe\u63a5\u548c\u53d8\u91cf\u524d\u5411\u58f0\u660e \u94fe\u63a5\u5c5e\u6027","title":"linkage"},{"location":"tags/#linker","text":"\u94fe\u63a5\u5668(linker)","title":"linker"},{"location":"tags/#list-assignment-operator","text":"\u7c7b\u522b\u8d4b\u503c\u8fd0\u7b97\u7b26","title":"list assignment operator"},{"location":"tags/#list-constructor","text":"\u5217\u8868\u6784\u9020\u51fd\u6570","title":"list-constructor"},{"location":"tags/#list-initialization","text":"\u5217\u8868\u521d\u59cb\u5316","title":"list-initialization"},{"location":"tags/#literal","text":"4.15 - \u5b57\u9762\u91cf","title":"literal"},{"location":"tags/#literals","text":"1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 \u5b57\u9762\u91cf","title":"literals"},{"location":"tags/#local-variable","text":"6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf","title":"local variable"},{"location":"tags/#local-variables","text":"6.3 - \u5c40\u90e8\u53d8\u91cf","title":"local variables"},{"location":"tags/#loop","text":"11.3 - \u6570\u7ec4\u548c\u5faa\u73af","title":"loop"},{"location":"tags/#lvalue_1","text":"\u5de6\u503c","title":"lvalue"},{"location":"tags/#lvalue-references","text":"9.4 - const\u7c7b\u578b\u7684\u5de6\u503c\u5f15\u7528","title":"lvalue references"},{"location":"tags/#lvalue-reference","text":"\u5de6\u503c\u5f15\u7528","title":"lvalue-reference"},{"location":"tags/#lvalues","text":"9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09","title":"lvalues"},{"location":"tags/#macro","text":"\u5b8f","title":"macro"},{"location":"tags/#member-access-operator","text":"\u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26","title":"member access operator"},{"location":"tags/#member-selection","text":"10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9","title":"member selection"},{"location":"tags/#member-function","text":"\u6210\u5458\u51fd\u6570","title":"member-function"},{"location":"tags/#member-initializer-list","text":"\u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868","title":"member-initializer-list"},{"location":"tags/#member-selection-operator","text":"\u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26","title":"member-selection-operator"},{"location":"tags/#member-types","text":"\u6210\u5458\u7c7b\u578b","title":"member-types"},{"location":"tags/#member-variable","text":"\u6210\u5458\u53d8\u91cf","title":"member-variable"},{"location":"tags/#members","text":"10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9","title":"members"},{"location":"tags/#memberwise-initialization","text":"\u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316","title":"memberwise initialization"},{"location":"tags/#memberwise-assignment","text":"\u6210\u5458\u4f9d\u6b21\u8d4b\u503c","title":"memberwise-assignment"},{"location":"tags/#memberwise-copy","text":"\u6210\u5458\u4f9d\u6b21\u62f7\u8d1d","title":"memberwise-copy"},{"location":"tags/#memory-leak","text":"\u5185\u5b58\u6cc4\u6f0f","title":"memory-leak"},{"location":"tags/#mixin","text":"None","title":"mixin"},{"location":"tags/#move","text":"M.4 - std::move M.5 - std::move_if_noexcept M.6 \u2014 std::unique_ptr","title":"move"},{"location":"tags/#move-assignment-operator","text":"\u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26","title":"move assignment operator"},{"location":"tags/#move-constructor","text":"\u79fb\u52a8\u6784\u9020\u51fd\u6570","title":"move-constructor"},{"location":"tags/#move-semantics","text":"M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb \u79fb\u52a8\u8bed\u4e49","title":"move-semantics"},{"location":"tags/#multiple-inheritance_1","text":"17.9 - \u591a\u91cd\u7ee7\u627f","title":"multiple-inheritance"},{"location":"tags/#named-cast","text":"\u5177\u540d\u7c7b\u578b\u8f6c\u6362","title":"named-cast"},{"location":"tags/#namespace","text":"2.9 - \u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u7a7a\u95f4 6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26 \u547d\u540d\u7a7a\u95f4","title":"namespace"},{"location":"tags/#namespace-aliases","text":"6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26","title":"namespace aliases"},{"location":"tags/#narrowing-convertions","text":"\u7f29\u7a84\u8f6c\u6362","title":"narrowing-convertions"},{"location":"tags/#new","text":"11.11 - \u4f7f\u7528 new \u548c delete \u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d","title":"new"},{"location":"tags/#numeric-conversions","text":"8.3 - \u6570\u503c\u8f6c\u6362","title":"numeric conversions"},{"location":"tags/#numeric-promotion","text":"8.2 - \u6d6e\u70b9\u6570\u548c\u6574\u578b\u63d0\u5347","title":"numeric promotion"},{"location":"tags/#numeric-promotions","text":"\u6570\u503c\u63d0\u5347","title":"numeric promotions"},{"location":"tags/#numeric-conversions_1","text":"\u6570\u503c\u8f6c\u6362","title":"numeric-conversions"},{"location":"tags/#object","text":"1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf 13.1 - \u6b22\u8fce\u6765\u5230\u9762\u5411\u5bf9\u8c61\u7684\u4e16\u754c 16.1 - \u5bf9\u8c61\u5173\u7cfb 16.2 - \u7ec4\u5408\u5173\u7cfb 16.3 - \u805a\u5408\u5173\u7cfb 16.4 - \u5173\u8054\u5173\u7cfb 16.5 - \u4f9d\u8d56\u5173\u7cfb 16.6 - \u5bb9\u5668\u7c7b 16.7 - std::initializer_list","title":"object"},{"location":"tags/#object-composition","text":"\u5bf9\u8c61\u7ec4\u5408","title":"object-composition"},{"location":"tags/#object-like-macros","text":"\u5bf9\u8c61\u7c7b\u578b\u7684\u5b8f","title":"object-like-macros"},{"location":"tags/#object-slicing","text":"\u5bf9\u8c61\u5207\u7247","title":"object-slicing"},{"location":"tags/#one-definition-rule","text":"\u5355\u4e00\u5b9a\u4e49\u89c4\u5219(one-definition-rule)","title":"one-definition-rule"},{"location":"tags/#operands","text":"\u64cd\u4f5c\u6570","title":"operands"},{"location":"tags/#operator","text":"1.9 - \u5b57\u9762\u91cf\u548c\u64cd\u4f5c\u7b26 14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 5.1 - \u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b 5.2 - \u6570\u5b66\u8fd0\u7b97\u7b26 5.3 - \u6c42\u6a21\u548c\u6307\u6570\u8fd0\u7b97 5.4 - \u81ea\u589e\u81ea\u51cf\u8fd0\u7b97\u7b26\u53ca\u5176\u526f\u4f5c\u7528 5.5 - \u9017\u53f7\u548c\u6761\u4ef6\u8fd0\u7b97\u7b26 5.6 - \u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u6d6e\u70b9\u6570\u6bd4\u8f83 5.7 - \u903b\u8f91\u8fd0\u7b97\u7b26 \u8fd0\u7b97\u7b26","title":"operator"},{"location":"tags/#operator_1","text":"18.11 - \u4f7f\u7528 << \u8fd0\u7b97\u7b26\u6253\u5370\u7ee7\u627f\u7c7b","title":"operator<<"},{"location":"tags/#optional-parameter","text":"\u53ef\u9009\u53c2\u6570","title":"optional-parameter"},{"location":"tags/#overflow","text":"4.5 - \u65e0\u7b26\u53f7\u6574\u578b\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u907f\u514d\u4f7f\u7528\u5b83","title":"overflow"},{"location":"tags/#overlapping","text":"13.8 - \u91cd\u53e0\u548c\u59d4\u6258\u6784\u9020\u51fd\u6570","title":"overlapping"},{"location":"tags/#overload","text":"14.1 - \u8fd0\u7b97\u7b26\u91cd\u8f7d\u7b80\u4ecb 14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26 14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26 14.15 - \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26 14.2 - \u4f7f\u7528\u53cb\u5143\u91cd\u8f7d\u7b97\u6570\u8fd0\u7b97\u7b26 14.3 - \u4f7f\u7528\u666e\u901a\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.4 - \u91cd\u8f7d\u8f93\u5165\u8f93\u51fa\u8fd0\u7b97\u7b26 14.5 - \u4f7f\u7528\u6210\u5458\u51fd\u6570\u91cd\u8f7d\u8fd0\u7b97\u7b26 14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26 14.8 - \u91cd\u8f7d\u9012\u589e\u9012\u51cf\u8fd0\u7b97\u7b26 14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26 8.10 - \u91cd\u8f7d\u51fd\u6570\u7684\u533a\u5206 8.11 - \u51fd\u6570\u91cd\u8f7d\u89e3\u6790\u548c\u4e0d\u660e\u786e\u5339\u914d \u91cd\u8f7d","title":"overload"},{"location":"tags/#overload-resolution","text":"\u91cd\u8f7d\u89e3\u6790","title":"overload-resolution"},{"location":"tags/#overloading","text":"8.9 - \u51fd\u6570\u91cd\u8f7d","title":"overloading"},{"location":"tags/#override","text":"18.3 - override\u3001final\u6807\u8bc6\u7b26\u4ee5\u53ca\u534f\u53d8\u8fd4\u56de\u7c7b\u578b \u91cd\u5199","title":"override"},{"location":"tags/#overriding","text":"17.7 - \u8c03\u7528\u7ee7\u627f\u51fd\u6570\u548c\u91cd\u5199 17.8 - \u9690\u85cf\u7ee7\u627f\u7684\u51fd\u6570","title":"overriding"},{"location":"tags/#overriding-virtualization","text":"18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316","title":"overriding-virtualization"},{"location":"tags/#parameter","text":"2.4 - \u51fd\u6570\u5f62\u53c2\u548c\u5b9e\u53c2","title":"parameter"},{"location":"tags/#parameters","text":"\u5f62\u53c2","title":"parameters"},{"location":"tags/#parent-class","text":"\u7236\u7c7b","title":"parent-class"},{"location":"tags/#parenthesis","text":"14.10 - \u91cd\u8f7d\u62ec\u53f7\u8fd0\u7b97\u7b26","title":"parenthesis"},{"location":"tags/#pass-by-address","text":"\u6309\u5730\u5740\u4f20\u9012","title":"pass-by-address"},{"location":"tags/#pass-by-reference","text":"\u6309\u5f15\u7528\u4f20\u9012","title":"pass-by-reference"},{"location":"tags/#pass-by-value","text":"\u6309\u503c\u4f20\u9012","title":"pass-by-value"},{"location":"tags/#placeholder-types","text":"\u5360\u4f4d\u7b26\u7c7b\u578b","title":"placeholder-types"},{"location":"tags/#pointer","text":"11.15 \u2014 \u6307\u5411\u6307\u9488\u7684\u6307\u9488\u548c\u591a\u7ef4\u6570\u7ec4 11.8 - \u6307\u9488\u548c\u6570\u7ec4 9.10 - \u6309\u5730\u5740\u4f20\u9012 Part2 9.6 - \u6307\u9488\u7b80\u4ecb 9.7 - \u7a7a\u6307\u9488 9.8 - \u6307\u9488\u548cconst 9.9 - \u6309\u5730\u5740\u4f20\u9012","title":"pointer"},{"location":"tags/#pointer-to-const","text":"\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488","title":"pointer-to-const"},{"location":"tags/#pointers","text":"18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528","title":"pointers"},{"location":"tags/#polymorphism","text":"18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001 \u591a\u6001","title":"polymorphism"},{"location":"tags/#pragma-once","text":"2.12 - \u5934\u6587\u4ef6\u9632\u536b\u5f0f\u58f0\u660e","title":"pragma once"},{"location":"tags/#preprocessor","text":"2.10 - \u9884\u5904\u7406\u5668\u7b80\u4ecb \u9884\u5904\u7406\u5668\u6307\u4ee4 \u9884\u5904\u7406\u5668","title":"preprocessor"},{"location":"tags/#private-inheritance","text":"\u79c1\u6709\u7ee7\u627f","title":"private-inheritance"},{"location":"tags/#private-member","text":"\u79c1\u6709\u6210\u5458","title":"private-member"},{"location":"tags/#program-organization","text":"2.8 - \u591a\u6587\u4ef6\u7a0b\u5e8f","title":"program organization"},{"location":"tags/#protected-inheritance","text":"\u53d7\u4fdd\u62a4\u7ee7\u627f","title":"protected-inheritance"},{"location":"tags/#protected-members","text":"\u53d7\u4fdd\u62a4\u6210\u5458","title":"protected-members"},{"location":"tags/#public-inheritance","text":"\u516c\u5f00\u7ee7\u627f","title":"public-inheritance"},{"location":"tags/#public-member","text":"\u516c\u6709\u6210\u5458","title":"public-member"},{"location":"tags/#pure-declaration","text":"\u7eaf\u58f0\u660e(pure-declaration)","title":"pure-declaration"},{"location":"tags/#pure-virtual","text":"\u7eaf\u865a\u51fd\u6570","title":"pure-virtual"},{"location":"tags/#pure-virtual-functions","text":"18.7 - \u7eaf\u865a\u51fd\u6570\uff0c\u62bd\u8c61\u57fa\u7c7b\u548c\u63a5\u53e3\u7c7b","title":"pure-virtual-functions"},{"location":"tags/#qualifier","text":"\u9650\u5b9a\u7b26","title":"qualifier"},{"location":"tags/#random-access-memory","text":"\u968f\u673a\u8bbf\u95ee\u5185\u5b58","title":"random-access-memory"},{"location":"tags/#range-based-for-loops","text":"\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af","title":"range-based-for-loops"},{"location":"tags/#raw-pointer","text":"\u539f\u59cb\u6307\u9488","title":"raw-pointer"},{"location":"tags/#recursion","text":"12.4 - \u9012\u5f52","title":"recursion"},{"location":"tags/#refactoring","text":"\u91cd\u6784","title":"refactoring"},{"location":"tags/#reference","text":"9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de","title":"reference"},{"location":"tags/#reference-container","text":"\u5f15\u7528\u5bb9\u5668","title":"reference-container"},{"location":"tags/#references","text":"18.1 - \u57fa\u7c7b\u7684\u6307\u9488\u548c\u5f15\u7528 M.2 - \u53f3\u503c\u5f15\u7528 M.3 - \u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c","title":"references"},{"location":"tags/#reflexive-association","text":"\u53cd\u8eab\u5173\u8054","title":"reflexive association"},{"location":"tags/#reinterpret-casts","text":"\u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362","title":"reinterpret-casts"},{"location":"tags/#return","text":"2.2 - \u51fd\u6570\u8fd4\u56de\u503c 9.11 - \u6309\u5f15\u7528\u8fd4\u56de\u548c\u6309\u5730\u5740\u8fd4\u56de","title":"return"},{"location":"tags/#return-by-address","text":"\u6309\u5730\u5740\u8fd4\u56de","title":"return-by-address"},{"location":"tags/#return-by-reference","text":"\u6309\u5f15\u7528\u8fd4\u56de","title":"return-by-reference"},{"location":"tags/#return-by-value","text":"\u6309\u503c\u8fd4\u56de","title":"return-by-value"},{"location":"tags/#return-type-deduction","text":"\u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad","title":"return-type-deduction"},{"location":"tags/#rounding-error","text":"\u820d\u5165\u8bef\u5dee","title":"rounding-error"},{"location":"tags/#runtime","text":"\u8fd0\u884c\u65f6","title":"runtime"},{"location":"tags/#rvalue","text":"\u53f3\u503c","title":"rvalue"},{"location":"tags/#rvalue-reference","text":"\u53f3\u503c\u5f15\u7528","title":"rvalue-reference"},{"location":"tags/#rvalues","text":"9.2 - \u503c\u7684\u5206\u7c7b\uff08\u5de6\u503c\u548c\u53f3\u503c\uff09","title":"rvalues"},{"location":"tags/#scaling","text":"\u6307\u9488\u7f29\u653e","title":"scaling"},{"location":"tags/#scientific-notation","text":"4.7 - \u79d1\u5b66\u8ba1\u6570\u6cd5","title":"scientific notation"},{"location":"tags/#scope","text":"2.5 - \u5c40\u90e8\u4f5c\u7528\u57df 6.11 - \u4f5c\u7528\u57df\u548c\u94fe\u63a5\u5c0f\u7ed3 \u4f5c\u7528\u57df","title":"scope"},{"location":"tags/#scope-resolution-operator","text":"6.2 - \u7528\u6237\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26","title":"scope resolution operator"},{"location":"tags/#scope-resolution-operator_1","text":"\u4f5c\u7528\u57df\u89e3\u6790\u8fd0\u7b97\u7b26","title":"scope-resolution-operator"},{"location":"tags/#scoped-enumerations","text":"10.4 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\uff08\u679a\u4e3e\u7c7b\uff09","title":"scoped enumerations"},{"location":"tags/#scoped-enumerations_1","text":"\u6709\u4f5c\u7528\u57df\u679a\u4e3e","title":"scoped-enumerations"},{"location":"tags/#segment","text":"\u5185\u5b58\u6bb5","title":"segment"},{"location":"tags/#sentinel","text":"\u54e8\u5175","title":"sentinel"},{"location":"tags/#setup","text":"0.8 - \u4e00\u4e9b\u5178\u578b\u7684 C++ \u95ee\u9898","title":"setup"},{"location":"tags/#shadow","text":"\u53d8\u91cf\u906e\u853d","title":"shadow"},{"location":"tags/#shallow-copy","text":"14.16 - \u6d45\u62f7\u8d1d\u548c\u6df1\u62f7\u8d1d 14.17 - \u91cd\u8f7d\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u6a21\u677f","title":"shallow copy"},{"location":"tags/#shallow-copy_1","text":"\u6d45\u62f7\u8d1d","title":"shallow-copy"},{"location":"tags/#shared_ptr","text":"M.7 \u2014 std::shared_ptr M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898","title":"shared_ptr"},{"location":"tags/#side-effects","text":"\u526f\u4f5c\u7528","title":"side-effects"},{"location":"tags/#signature","text":"\u7b7e\u540d","title":"signature"},{"location":"tags/#size_t","text":"4.6 - \u56fa\u5b9a\u5bbd\u5ea6\u6574\u578b\u548c size_t","title":"size_t"},{"location":"tags/#sizeof","text":"4.3 - \u5bf9\u8c61\u7684\u5927\u5c0f\u548c sizeof \u64cd\u4f5c\u7b26","title":"sizeof"},{"location":"tags/#smart-pointer-class","text":"\u667a\u80fd\u6307\u9488\u7c7b","title":"smart pointer class"},{"location":"tags/#smart-pointer","text":"\u667a\u80fd\u6307\u9488","title":"smart-pointer"},{"location":"tags/#smart-pointers","text":"M.1 - \u667a\u80fd\u6307\u9488\u548c\u79fb\u52a8\u8bed\u4e49\u7b80\u4ecb","title":"smart-pointers"},{"location":"tags/#sort","text":"11.4 - \u6570\u7ec4\u6392\u5e8f\u4e4b\u9009\u62e9\u6392\u5e8f","title":"sort"},{"location":"tags/#stack","text":"12.2 - \u6808\u548c\u5806 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a \u6808","title":"stack"},{"location":"tags/#stack-frame","text":"\u6808\u5e27","title":"stack-frame"},{"location":"tags/#stack-overflow","text":"\u5806\u6808\u6ea2\u51fa","title":"stack-overflow"},{"location":"tags/#standard-stream","text":"\u6807\u51c6\u6d41","title":"standard-stream"},{"location":"tags/#statement","text":"6.1 - \u590d\u5408\u8bed\u53e5\uff08\u8bed\u53e5\u5757\uff09 \u8bed\u53e5","title":"statement"},{"location":"tags/#statements","text":"1.1 - \u7a0b\u5e8f\u7ed3\u6784\u548c\u8bed\u53e5","title":"statements"},{"location":"tags/#static","text":"6.10 - \u9759\u6001\u5c40\u90e8\u53d8\u91cf","title":"static"},{"location":"tags/#static-membe-function","text":"13.14 - \u9759\u6001\u6210\u5458\u51fd\u6570","title":"static membe function"},{"location":"tags/#static-member","text":"13.7 - \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316","title":"static member"},{"location":"tags/#static-member-variable","text":"13.13 - \u9759\u6001\u6210\u5458\u53d8\u91cf","title":"static member variable"},{"location":"tags/#static-cast","text":"8.5 - \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u548cstatic_cast","title":"static-cast"},{"location":"tags/#static-casts","text":"\u9759\u6001\u7c7b\u578b\u8f6c\u6362","title":"static-casts"},{"location":"tags/#static-initialization","text":"\u9759\u6001\u521d\u59cb\u5316","title":"static-initialization"},{"location":"tags/#static-member-functions","text":"\u9759\u6001\u6210\u5458\u51fd\u6570","title":"static-member-functions"},{"location":"tags/#static-member-variables","text":"\u9759\u6001\u6210\u5458\u53d8\u91cf","title":"static-member-variables"},{"location":"tags/#static-memory-allocation","text":"\u9759\u6001\u5185\u5b58\u5206\u914d","title":"static-memory-allocation"},{"location":"tags/#static-storage-duration","text":"\u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4","title":"static-storage-duration"},{"location":"tags/#static-variables","text":"\u9759\u6001\u53d8\u91cf","title":"static-variables"},{"location":"tags/#static_assert","text":"7.17 - \u65ad\u8a00\u548c static_assert","title":"static_assert"},{"location":"tags/#static_cast","text":"4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast 8.3 - \u6570\u503c\u8f6c\u6362","title":"static_cast"},{"location":"tags/#stdarray","text":"11.16 \u2014 \u56fa\u5b9a\u6570\u7ec4 std::array \u7b80\u4ecb","title":"std::array"},{"location":"tags/#stdstring_view","text":"11.7 - std::string_view\uff08\u7b2c\u4e8c\u90e8\u5206\uff09","title":"std::string_view"},{"location":"tags/#stl","text":"21.1 - \u6807\u51c6\u5e93 21.2 - STL\u5bb9\u5668 21.3 - STL\u8fed\u4ee3\u5668 21.4 - STL\u7b97\u6cd5","title":"stl"},{"location":"tags/#storage-duration","text":"6.3 - \u5c40\u90e8\u53d8\u91cf 6.4 - \u5168\u5c40\u53d8\u91cf","title":"storage duration"},{"location":"tags/#storage-duration_1","text":"\u5b58\u50a8\u6301\u7eed\u65f6\u95f4","title":"storage-duration"},{"location":"tags/#string","text":"11.6 - C \u8bed\u8a00\u98ce\u683c\u5b57\u7b26\u4e32 22.1 - std::string \u548c std::wstring 22.2 - std-string \u7684\u6784\u9020\u548c\u6790\u6784 22.3 - std-string \u7684\u957f\u5ea6\u548c\u5bb9\u91cf 22.4 - std-string \u5b57\u7b26\u8bbf\u95ee\u548cC\u5b57\u7b26\u6570\u7ec4\u8f6c\u6362 22.5 - std-string \u8d4b\u503c\u548c\u4ea4\u6362 22.6 - std-string \u8ffd\u52a0 22.7 - std-string \u63d2\u5165 23.4 - \u7528\u4e8e\u5b57\u7b26\u4e32\u7684 stream \u7c7b 4.17 - std::string \u7b80\u4ecb","title":"string"},{"location":"tags/#string_view","text":"4.18 - std::string_view \u7b80\u4ecb","title":"string_view"},{"location":"tags/#strong-except","text":"\u5f3a\u5f02\u5e38\u4fdd\u8bc1","title":"strong-except"},{"location":"tags/#struct","text":"10.5 - \u7ed3\u6784\u4f53\u3001\u6210\u5458\u548c\u6210\u5458\u9009\u62e9 10.6 - \u7ed3\u6784\u4f53\u7684\u805a\u5408\u521d\u59cb\u5316 10.7 - \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316 10.8 - \u7ed3\u6784\u4f53\u4f20\u9012\u53ca\u5176\u4ed6 10.9 - \u57fa\u4e8e\u6307\u9488\u548c\u5f15\u7528\u7684\u6210\u5458\u9009\u62e9","title":"struct"},{"location":"tags/#struct-padding","text":"\u7ed3\u6784\u4f53\u586b\u5145","title":"struct-padding"},{"location":"tags/#subscript","text":"14.9 - \u91cd\u8f7d\u4e0b\u6807\u8fd0\u7b97\u7b26","title":"subscript"},{"location":"tags/#subscripts","text":"\u4e0b\u6807","title":"subscripts"},{"location":"tags/#summary","text":"1.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - C++\u57fa\u7840 10.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u679a\u4e3e\u548c\u7ed3\u6784\u4f53 10.y - \u5b66\u4f1a\u4f7f\u7528C++\u53c2\u8003\u624b\u518c 11.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u52a8\u6001\u5206\u914d 12.8 - lambda \u95ed\u5305 12.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570 13.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u9762\u5411\u5bf9\u8c61\u57fa\u7840 14.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26\u91cd\u8f7d 16.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5bf9\u8c61\u5173\u7cfb 17.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7ee7\u627f 18.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u865a\u51fd\u6570 19.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u6a21\u677f\u548c\u7c7b 2.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u51fd\u6570\u4e0e\u6587\u4ef6 20.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f02\u5e38 3.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4ee3\u7801\u8c03\u8bd5 4.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u57fa\u672c\u6570\u636e\u7c7b\u578b 5.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u8fd0\u7b97\u7b26 6.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u4f5c\u7528\u57df\u3001\u6301\u7eed\u65f6\u95f4\u548c\u94fe\u63a5 7.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u63a7\u5236\u6d41\u548c\u9519\u8bef\u5904\u7406 8.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u7c7b\u578b\u8f6c\u6362\u548c\u51fd\u6570\u91cd\u8f7d 9.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u5f15\u7528\u548c\u6307\u9488 M.x - \u5c0f\u7ed3\u4e0e\u6d4b\u8bd5 - \u79fb\u52a8\u548c\u667a\u80fd\u6307\u9488","title":"summary"},{"location":"tags/#super-class","text":"\u8d85\u7c7b","title":"super-class"},{"location":"tags/#switch","text":"7.4 - switch \u8bed\u53e5\u57fa\u7840 7.5 - switch fallthrough\u5c5e\u6027\u548c\u4f5c\u7528\u57df","title":"switch"},{"location":"tags/#symbolic-constants","text":"11.10 - C \u98ce\u683c\u5b57\u7b26\u4e32\u7b26\u53f7\u5e38\u91cf","title":"symbolic constants"},{"location":"tags/#symbolic-constants_1","text":"\u7b26\u53f7\u5e38\u91cf","title":"symbolic-constants"},{"location":"tags/#template-class-specialization","text":"\u7c7b\u6a21\u677f\u7279\u5316","title":"template class specialization"},{"location":"tags/#testing","text":"7.12 - \u4ee3\u7801\u6d4b\u8bd5","title":"testing"},{"location":"tags/#the-diamond-problem","text":"\u83f1\u5f62\u7ee7\u627f\u95ee\u9898","title":"the-diamond-problem"},{"location":"tags/#this","text":"13.10 - \u9690\u85cf\u7684this\u6307\u9488","title":"this"},{"location":"tags/#translation","text":"\u7ffb\u8bd1","title":"translation"},{"location":"tags/#type","text":"10.1 - \u7a0b\u5e8f\u5b9a\u4e49\u7c7b\u578b\u7b80\u4ecb","title":"type"},{"location":"tags/#type-conversion","text":"4.12 - \u7c7b\u578b\u8f6c\u6362\u548c static_cast 8.1 - \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362","title":"type conversion"},{"location":"tags/#type-deduction","text":"8.7 - \u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad 8.8 - \u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad 9.12 - \u6307\u9488\u3001\u5f15\u7528\u548cconst\u7684\u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u63a8\u65ad","title":"type deduction"},{"location":"tags/#type-aliases","text":"\u7c7b\u578b\u522b\u540d","title":"type-aliases"},{"location":"tags/#type-conversion_1","text":"\u7c7b\u578b\u8f6c\u6362","title":"type-conversion"},{"location":"tags/#type-definition","text":"\u7c7b\u578b\u5b9a\u4e49","title":"type-definition"},{"location":"tags/#type-inference","text":"\u7c7b\u578b\u63a8\u65ad","title":"type-inference"},{"location":"tags/#type-safe","text":"\u7c7b\u578b\u5b89\u5168","title":"type-safe"},{"location":"tags/#type-signature","text":"\u7c7b\u578b\u7b7e\u540d","title":"type-signature"},{"location":"tags/#typecast","text":"14.11 - \u91cd\u8f7d\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26","title":"typecast"},{"location":"tags/#typedef","text":"8.6 - typedef \u548c\u7c7b\u578b\u522b\u540d","title":"typedef"},{"location":"tags/#typeid","text":"typeid \u8fd0\u7b97\u7b26","title":"typeid"},{"location":"tags/#typeinfo","text":"8.4 - \u7b97\u672f\u8f6c\u6362","title":"typeinfo"},{"location":"tags/#unary","text":"14.6 - \u91cd\u8f7d\u4e00\u5143\u8fd0\u7b97\u7b26+\uff0c- \u548c ! 14.7 - \u91cd\u8f7d\u6bd4\u8f83\u8fd0\u7b97\u7b26","title":"unary"},{"location":"tags/#undefined-behavior","text":"1.6 - \u672a\u521d\u59cb\u5316\u53d8\u91cf\u548c\u672a\u5b9a\u4e49\u884c\u4e3a 2.2 - \u51fd\u6570\u8fd4\u56de\u503c 4.4 - \u6709\u7b26\u53f7\u6574\u578b","title":"undefined behavior"},{"location":"tags/#undefined-behavior_1","text":"\u672a\u5b9a\u4e49\u884c\u4e3a","title":"undefined-behavior"},{"location":"tags/#uniform-initialization","text":"\u7edf\u4e00\u521d\u59cb\u5316","title":"uniform initialization"},{"location":"tags/#unmanned-namespace","text":"6.15 - \u672a\u547d\u540d\u548c\u5185\u8054\u547d\u540d\u7a7a\u95f4","title":"unmanned namespace"},{"location":"tags/#unnamed-namespace","text":"\u533f\u540d\u547d\u540d\u7a7a\u95f4","title":"unnamed-namespace"},{"location":"tags/#unscoped-enumerations","text":"10.2 - \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b 10.3 - \u9650\u5b9a\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b\u7684\u8f93\u5165\u8f93\u51fa","title":"unscoped enumerations"},{"location":"tags/#unscoped-enumerations_1","text":"\u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b","title":"unscoped-enumerations"},{"location":"tags/#upcasting","text":"\u5411\u4e0a\u8f6c\u6362","title":"upcasting"},{"location":"tags/#using","text":"6.12 - using \u58f0\u660e\u548c using \u6307\u4ee4","title":"using"},{"location":"tags/#using-declaration","text":"using \u58f0\u660e","title":"using-declaration"},{"location":"tags/#using-directive","text":"using \u6307\u4ee4","title":"using-directive"},{"location":"tags/#value-category","text":"\u503c\u7c7b\u578b","title":"value-category"},{"location":"tags/#value-container","text":"\u503c\u5bb9\u5668","title":"value-container"},{"location":"tags/#value-initialization","text":"\u503c\u521d\u59cb\u5316","title":"value-initialization"},{"location":"tags/#value-preserving","text":"\u503c\u4fdd\u7559","title":"value-preserving"},{"location":"tags/#variable","text":"1.3 - \u5bf9\u8c61\u548c\u53d8\u91cf 1.4 - \u53d8\u91cf\u8d4b\u503c\u548c\u521d\u59cb\u5316","title":"variable"},{"location":"tags/#variable-shadowing","text":"6.5 - \u53d8\u91cf\u906e\u853d","title":"variable shadowing"},{"location":"tags/#vector","text":"11.17 \u2014 \u52a8\u6001\u6570\u7ec4 std::vector \u7b80\u4ecb 12.3 - std::vector\u7684\u5bb9\u91cf\u548c\u7c7b\u6808\u884c\u4e3a","title":"vector"},{"location":"tags/#virtual-functions","text":"18.2 - \u865a\u51fd\u6570\u548c\u591a\u6001","title":"virtual functions"},{"location":"tags/#virtual-assignment","text":"18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316","title":"virtual-assignment"},{"location":"tags/#virtual-base-class","text":"\u865a\u57fa\u7c7b","title":"virtual-base-class"},{"location":"tags/#virtual-base-classes","text":"18.8 - \u865a\u57fa\u7c7b 18.9 - \u5bf9\u8c61\u5207\u7247","title":"virtual-base-classes"},{"location":"tags/#virtual-destructor","text":"\u865a\u6790\u6784\u51fd\u6570","title":"virtual-destructor"},{"location":"tags/#virtual-destructors","text":"18.4 - \u865a\u6784\u9020\u51fd\u6570\u3001\u865a\u8d4b\u503c\u548c\u91cd\u8f7d\u865a\u62df\u5316","title":"virtual-destructors"},{"location":"tags/#virtual-function","text":"\u865a\u51fd\u6570","title":"virtual-function"},{"location":"tags/#virtual-table","text":"18.6 - \u865a\u51fd\u6570\u8868 \u865a\u8868","title":"virtual-table"},{"location":"tags/#void","text":"11.14 \u2014 void \u6307\u9488 2.3 - void \u51fd\u6570 4.2 - Void","title":"void"},{"location":"tags/#void-pointer","text":"void \u6307\u9488","title":"void pointer"},{"location":"tags/#weak_ptr","text":"M.8 - \u667a\u80fd\u6307\u9488\u5e26\u6765\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898","title":"weak_ptr"},{"location":"tags/#while","text":"7.7 - \u5faa\u73af\u548c while \u8bed\u53e5","title":"while"},{"location":"tags/#wild-pointer","text":"\u91ce\u6307\u9488","title":"wild-pointer"},{"location":"tags/#word","text":"\u5b57\u957f","title":"word"},{"location":"tags/#wstring","text":"22.1 - std::string \u548c std::wstring","title":"wstring"},{"location":"tags/#zero-initialization","text":"0\u521d\u59cb\u5316","title":"zero-initialization"},{"location":"tags/#function-prototype","text":"\u51fd\u6570\u539f\u578b","title":"\u51fd\u6570\u539f\u578b(function prototype)"},{"location":"tags/#_1","text":"\u521d\u59cb\u5316","title":"\u521d\u59cb\u5316"},{"location":"tags/#initializer","text":"\u521d\u59cb\u5316\u503c","title":"\u521d\u59cb\u5316\u503c(initializer)"},{"location":"tags/#_2","text":"\u52a8\u6001\u6570\u7ec4","title":"\u52a8\u6001\u6570\u7ec4"},{"location":"tags/#objec","text":"\u5bf9\u8c61","title":"\u5bf9\u8c61(objec)"},{"location":"tags/#_3","text":"\u62bd\u8c61\u7c7b","title":"\u62bd\u8c61\u7c7b"},{"location":"tags/#_4","text":"None","title":"\u62ec\u53f7\u521d\u59cb\u5316"},{"location":"tags/#_5","text":"\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26","title":"\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26"},{"location":"tags/#_6","text":"\u6784\u9020\u51fd\u6570","title":"\u6784\u9020\u51fd\u6570"},{"location":"tags/#_7","text":"\u6a21\u677f\u7c7b\u578b\u53c2\u6570","title":"\u6a21\u677f\u7c7b\u578b\u53c2\u6570"},{"location":"tags/#_8","text":"\u79bb\u5f00\u4f5c\u7528\u57df","title":"\u79bb\u5f00\u4f5c\u7528\u57df"},{"location":"tags/#trivial-conversion","text":"\u7b80\u5355\u8f6c\u6362","title":"\u7b80\u5355\u8f6c\u6362(trivial conversion)"},{"location":"tags/#_9","text":"\u7c7b\u5b9e\u4f8b","title":"\u7c7b\u5b9e\u4f8b"},{"location":"tags/#snake_case","text":"\u86c7\u5f62\u547d\u540d\u6cd5","title":"\u86c7\u5f62\u547d\u540d\u6cd5(snake_case)"},{"location":"tags/#_10","text":"\u5173\u8054 \u5173\u8054\u5bb9\u5668 \u7ed1\u5b9a C \u8bed\u8a00\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362 \u65e9\u671f\u7ed1\u5b9a \u5c01\u88c5 \u8868\u8fbe\u5f0f\u53c2\u6570 \u7f1d\u5408\u602a\u5bf9\u8c61 \u540e\u671f\u7ed1\u5b9a \u591a\u91cd\u7ee7\u627f \u540d\u79f0\u4fee\u9970 \u5dee\u4e00\u9519\u8bef \u8f93\u51fa\u64cd\u7eb5\u5668 \u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316\uff08RAII\uff09 \u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f(RTTI) \u987a\u5e8f\u5bb9\u5668 \u6a21\u677f\u7279\u5316 \u7f29\u5199\u51fd\u6570\u6a21\u677f \u62bd\u8c61\u7c7b \u62bd\u8c61 \u6210\u5458\u8bbf\u95ee\u51fd\u6570 \u6210\u5458\u8bbf\u95ee\u4fee\u9970\u7b26 \u53d6\u5730\u5740\u64cd\u4f5c\u7b26 & \u805a\u5408\u6570\u636e\u7c7b\u578b \u805a\u5408\u521d\u59cb\u5316 \u805a\u5408 \u4e0d\u660e\u786e\u5339\u914d \u533f\u540d\u5bf9\u8c61 \u5b9e\u53c2 \u5143\u6570 \u81ea\u52a8\u5185\u5b58\u5206\u914d \u81ea\u52a8\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u57fa\u7c7b \u4f4d None \u6309\u4f4d\u6216 bss\u6bb5 \u5b57\u8282 \u8c03\u7528\u6808 \u53ef\u8c03\u7528\u5bf9\u8c61 \u56de\u8c03\u51fd\u6570 \u5b50\u7c7b \u7c7b\u5b9e\u4f8b \u7c7b\u6a21\u677f\u5b9e\u53c2\u63a8\u65ad \u7c7b\u6a21\u677f \u7c7b\u7c7b\u578b \u95ed\u5305 \u4ee3\u7801\u6bb5 \u7f16\u8bd1\u5668 \u7ec4\u5408\u7c7b\u578b const \u7c7b\u578b\u8f6c\u6362 const \u6210\u5458\u51fd\u6570 \u5e38\u91cf\u6307\u9488 \u5e38\u91cf\u8868\u8fbe\u5f0f \u6784\u9020\u51fd\u6570 \u5bb9\u5668\u9002\u914d\u5668 \u5bb9\u5668\u7c7b \u8f6c\u6362\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u6784\u9020\u51fd\u6570 \u62f7\u8d1d\u521d\u59cb\u5316 \u62f7\u8d1d\u8bed\u4e49 \u534f\u53d8\u8fd4\u56de\u503c\u7c7b\u578b \u60ac\u5782 \u6570\u636e\u6bb5 \u6df1\u62f7\u8d1d \u9ed8\u8ba4\u6784\u9020\u51fd\u6570 \u9ed8\u8ba4\u521d\u59cb\u5316 \u9ed8\u8ba4\u6210\u5458\u521d\u59cb\u5316\u503c \u59d4\u6258\u6784\u9020\u51fd\u6570 \u4f9d\u8d56 \u89e3\u5f15\u7528\u8fd0\u7b97\u7b26 \u6d3e\u751f\u7c7b \u6307\u5b9a\u521d\u59cb\u5316 \u6790\u6784\u51fd\u6570 \u76f4\u63a5\u521d\u59cb\u5316 \u5411\u4e0b\u8f6c\u6362 \u7b28\u6307\u9488 \u52a8\u6001\u6570\u7ec4 \u52a8\u6001\u7c7b\u578b\u8f6c\u6362 \u52a8\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u52a8\u6001\u521d\u59cb\u5316 \u52a8\u6001\u5185\u5b58\u5206\u914d \u679a\u4e3e\u7c7b \u6c42\u503c \u5f02\u5e38\u5b89\u5168\u95ee\u9898 \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 \u8868\u8fbe\u5f0f\u7c7b\u578b \u5916\u90e8\u94fe\u63a5 \u5916\u90e8\u53d8\u91cf \u63d0\u53d6\u8fd0\u7b97\u7b26 \u8d2f\u7a7f\u5c5e\u6027 \u6587\u4ef6\u4f5c\u7528\u57df \u56fa\u5b9a\u6570\u7ec4 \u683c\u5f0f\u7ec4 \u53cb\u5143\u7c7b \u53cb\u5143\u51fd\u6570 \u51fd\u6570\u5b9e\u4f8b \u51fd\u6570\u5b57\u9762\u91cf \u51fd\u6570\u6307\u9488 \u51fd\u6570\u539f\u578b \u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316 \u51fd\u6570\u6a21\u677f \u6cdb\u578b\u7b97\u6cd5 \u79bb\u5f00\u4f5c\u7528\u57df \u5806 \u8eab\u4efd\u7279\u5f81 \u5373\u65f6\u51fd\u6570 \u9690\u5f0f\u6784\u9020\u51fd\u6570 \u9690\u5f0f\u5bf9\u8c61 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 \u7ee7\u627f \u521d\u59cb\u5316 \u521d\u59cb\u5316\u503c\u5217\u8868 \u521d\u59cb\u5316\u503c \u5185\u8054\u51fd\u6570 \u5185\u8054\u547d\u540d\u7a7a\u95f4 \u5185\u8054\u53d8\u91cf \u8f93\u5165\u64cd\u7eb5\u5668 \u63d2\u5165\u8fd0\u7b97\u7b26 \u5b9e\u4f8b \u6574\u578b\u7c7b\u578b \u63a5\u53e3\u7c7b \u5185\u90e8\u94fe\u63a5 \u5185\u90e8\u53d8\u91cf \u904d\u5386 \u8fed\u4ee3\u5668 \u533f\u540d\u51fd\u6570 \u94fe\u63a5 \u94fe\u63a5\u5c5e\u6027 \u7c7b\u522b\u8d4b\u503c\u8fd0\u7b97\u7b26 \u5217\u8868\u6784\u9020\u51fd\u6570 \u5217\u8868\u521d\u59cb\u5316 \u5b57\u9762\u91cf \u5de6\u503c\u5f15\u7528 \u5de6\u503c \u6210\u5458\u8bbf\u95ee\u8fd0\u7b97\u7b26 \u6210\u5458\u51fd\u6570 \u6210\u5458\u521d\u59cb\u5316\u503c\u5217\u8868 \u6210\u5458\u9009\u62e9\u8fd0\u7b97\u7b26 \u6210\u5458\u7c7b\u578b \u6210\u5458\u53d8\u91cf \u6210\u5458\u4f9d\u6b21\u521d\u59cb\u5316 \u6210\u5458\u4f9d\u6b21\u8d4b\u503c \u6210\u5458\u4f9d\u6b21\u62f7\u8d1d \u5185\u5b58\u6cc4\u6f0f None \u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26 \u79fb\u52a8\u6784\u9020\u51fd\u6570 \u79fb\u52a8\u8bed\u4e49 \u5177\u540d\u7c7b\u578b\u8f6c\u6362 \u547d\u540d\u7a7a\u95f4 \u7f29\u7a84\u8f6c\u6362 \u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316 \u6570\u503c\u63d0\u5347 \u6570\u503c\u8f6c\u6362 \u5bf9\u8c61\u7ec4\u5408 \u5bf9\u8c61\u5207\u7247 \u5bf9\u8c61 \u64cd\u4f5c\u6570 \u8fd0\u7b97\u7b26 \u8d85\u51fa\u4f5c\u7528\u57df \u91cd\u8f7d\u89e3\u6790 \u91cd\u8f7d \u91cd\u5199 \u7236\u7c7b \u6309\u5730\u5740\u4f20\u9012 \u6309\u5f15\u7528\u4f20\u9012 \u5360\u4f4d\u7b26\u7c7b\u578b \u6307\u5411\u5e38\u91cf\u7684\u6307\u9488 \u591a\u6001 \u79c1\u6709\u7ee7\u627f \u79c1\u6709\u6210\u5458 \u53d7\u4fdd\u62a4\u7ee7\u627f \u53d7\u4fdd\u62a4\u6210\u5458 \u516c\u5f00\u7ee7\u627f \u516c\u6709\u6210\u5458 \u7eaf\u865a\u51fd\u6570 \u968f\u673a\u8bbf\u95ee\u5185\u5b58 \u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af \u539f\u59cb\u6307\u9488 \u5f15\u7528\u5bb9\u5668 \u53cd\u8eab\u5173\u8054 \u91cd\u65b0\u89e3\u91ca\u7c7b\u578b\u8f6c\u6362 \u6309\u5730\u5740\u8fd4\u56de \u6309\u5f15\u7528\u8fd4\u56de \u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u65ad \u820d\u5165\u8bef\u5dee \u53f3\u503c\u5f15\u7528 \u53f3\u503c \u6307\u9488\u7f29\u653e \u6709\u4f5c\u7528\u57df\u679a\u4e3e \u5185\u5b58\u6bb5 \u54e8\u5175 \u53d8\u91cf\u906e\u853d \u6d45\u62f7\u8d1d \u526f\u4f5c\u7528 \u7b7e\u540d \u667a\u80fd\u6307\u9488\u7c7b \u667a\u80fd\u6307\u9488 \u86c7\u5f62\u547d\u540d\u6cd5 \u6808\u5e27 \u5806\u6808\u6ea2\u51fa \u6808 \u6807\u51c6\u6d41 \u9759\u6001\u7c7b\u578b\u8f6c\u6362 \u9759\u6001\u521d\u59cb\u5316 \u9759\u6001\u6210\u5458\u51fd\u6570 \u9759\u6001\u6210\u5458\u53d8\u91cf \u9759\u6001\u5185\u5b58\u5206\u914d \u9759\u6001\u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u9759\u6001\u53d8\u91cf \u5b58\u50a8\u6301\u7eed\u65f6\u95f4 \u5f3a\u5f02\u5e38\u4fdd\u8bc1 \u7ed3\u6784\u4f53\u586b\u5145 \u4e0b\u6807 \u8d85\u7c7b \u8bed\u6cd5 \u7c7b\u6a21\u677f\u7279\u5316 \u6a21\u677f\u7c7b\u578b\u53c2\u6570 \u83f1\u5f62\u7ee7\u627f\u95ee\u9898 \u7ffb\u8bd1 \u7b80\u5355\u8f6c\u6362 \u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u522b\u540d \u7c7b\u578b\u8f6c\u6362 \u7c7b\u578b\u5b9a\u4e49 \u7c7b\u578b\u63a8\u65ad \u7c7b\u578b\u5b89\u5168 \u7c7b\u578b\u7b7e\u540d typeid \u8fd0\u7b97\u7b26 \u672a\u5b9a\u4e49\u884c\u4e3a \u7edf\u4e00\u521d\u59cb\u5316 \u533f\u540d\u547d\u540d\u7a7a\u95f4 \u65e0\u4f5c\u7528\u57df\u679a\u4e3e\u7c7b\u578b \u5411\u4e0a\u8f6c\u6362 using \u58f0\u660e using \u6307\u4ee4 \u503c\u7c7b\u578b \u503c\u5bb9\u5668 \u503c\u521d\u59cb\u5316 \u503c\u4fdd\u7559 \u865a\u57fa\u7c7b \u865a\u6790\u6784\u51fd\u6570 \u865a\u51fd\u6570 \u865a\u8868 void \u6307\u9488 \u91ce\u6307\u9488 \u5b57\u957f 0\u521d\u59cb\u5316 None","title":"\u8bcd\u6c47\u8868"},{"location":"tags/#syntax_1","text":"\u8bed\u6cd5","title":"\u8bed\u6cd5(syntax)"},{"location":"tags/#out-of-scope","text":"\u8d85\u51fa\u4f5c\u7528\u57df","title":"\u8d85\u51fa\u4f5c\u7528\u57df(out of scope)"},{"location":"tags/#_11","text":"\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316","title":"\u975e\u9759\u6001\u6210\u5458\u521d\u59cb\u5316"}]} \ No newline at end of file diff --git a/segment/index.html b/segment/index.html index 7b42c0a3f..f07bc5a2a 100644 --- a/segment/index.html +++ b/segment/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/sentinel/index.html b/sentinel/index.html index a61833a89..b5320f903 100644 --- a/sentinel/index.html +++ b/sentinel/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/shadow/index.html b/shadow/index.html index 9a8440f10..8591b001c 100644 --- a/shadow/index.html +++ b/shadow/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/shallow-copy/index.html b/shallow-copy/index.html index e29535303..1d3fe74b4 100644 --- a/shallow-copy/index.html +++ b/shallow-copy/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/side-effects/index.html b/side-effects/index.html index 1649ccce0..a2fab9904 100644 --- a/side-effects/index.html +++ b/side-effects/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/signature/index.html b/signature/index.html index 872b5b639..d0096e4d6 100644 --- a/signature/index.html +++ b/signature/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/sitemap.xml b/sitemap.xml index 0018fec7a..90f497f12 100644 --- a/sitemap.xml +++ b/sitemap.xml @@ -2,2977 +2,2977 @@ None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily None - 2023-09-02 + 2024-02-22 daily \ No newline at end of file diff --git a/sitemap.xml.gz b/sitemap.xml.gz index d9c9b885ccf0189e76e1e16f010b02b328b2fe43..0f2cb85e85c989158fbf1d8f07ab958c9fb3c097 100644 GIT binary patch delta 232 zcmV`BhC?p&A^`wQ`F2(S delta 233 zcmVaTl&^7(K?KaEw z?e%%~n69e3@^je5FeX#Ei_3I7j)$i_KOT=+Y{#>9XXhfN-v?R1IxHOz}_kydxf zG^Y>r^+?EsWscW+l2Qad{pbY-TjpL jO>z - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None diff --git a/smart-pointer/index.html b/smart-pointer/index.html index 3e540107f..c490e466b 100644 --- a/smart-pointer/index.html +++ b/smart-pointer/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/snake-case/index.html b/snake-case/index.html index 1a2b3055d..b1c5e5602 100644 --- a/snake-case/index.html +++ b/snake-case/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/stack-frame/index.html b/stack-frame/index.html index 85148d998..7d50c4f3a 100644 --- a/stack-frame/index.html +++ b/stack-frame/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/stack-overflow/index.html b/stack-overflow/index.html index ef1f3088d..2b21f5348 100644 --- a/stack-overflow/index.html +++ b/stack-overflow/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/stack/index.html b/stack/index.html index 8e9efb4e2..2515a4652 100644 --- a/stack/index.html +++ b/stack/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/standard-stream/index.html b/standard-stream/index.html index 12c91d1e7..95b762c69 100644 --- a/standard-stream/index.html +++ b/standard-stream/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/statement/index.html b/statement/index.html index 98323e772..f90ca167f 100644 --- a/statement/index.html +++ b/statement/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/static-casts/index.html b/static-casts/index.html index 091cefa6a..14982c284 100644 --- a/static-casts/index.html +++ b/static-casts/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/static-initialization/index.html b/static-initialization/index.html index 0a9251ca4..cfde5939f 100644 --- a/static-initialization/index.html +++ b/static-initialization/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/static-member-functions/index.html b/static-member-functions/index.html index 3ef1eaa06..a7fee27af 100644 --- a/static-member-functions/index.html +++ b/static-member-functions/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/static-member-variables/index.html b/static-member-variables/index.html index be7f4795c..07a534b37 100644 --- a/static-member-variables/index.html +++ b/static-member-variables/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/static-memory-allocation/index.html b/static-memory-allocation/index.html index e8ede5efe..1779fe2e5 100644 --- a/static-memory-allocation/index.html +++ b/static-memory-allocation/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/static-storage-duration/index.html b/static-storage-duration/index.html index e3dcdfff7..e3fc8ae7a 100644 --- a/static-storage-duration/index.html +++ b/static-storage-duration/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/static-variables/index.html b/static-variables/index.html index 54ce65f47..7fceb8612 100644 --- a/static-variables/index.html +++ b/static-variables/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/storage-duration/index.html b/storage-duration/index.html index c6df89691..7112bec36 100644 --- a/storage-duration/index.html +++ b/storage-duration/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/strong-except/index.html b/strong-except/index.html index 99ffd8db5..ec1f490b5 100644 --- a/strong-except/index.html +++ b/strong-except/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/struct-padding/index.html b/struct-padding/index.html index b1c0e7aed..4759487c2 100644 --- a/struct-padding/index.html +++ b/struct-padding/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/stub/index.html b/stub/index.html index 99785b9d2..5ed5324c6 100644 --- a/stub/index.html +++ b/stub/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/subscripts/index.html b/subscripts/index.html index e6df02adc..5e9eb2fe2 100644 --- a/subscripts/index.html +++ b/subscripts/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/super-class/index.html b/super-class/index.html index 07f1caaff..506ec0a34 100644 --- a/super-class/index.html +++ b/super-class/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/symbolic-constants/index.html b/symbolic-constants/index.html index cdb8f08a0..6630a2584 100644 --- a/symbolic-constants/index.html +++ b/symbolic-constants/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/syntax/index.html b/syntax/index.html index 94ffc8ff9..00cef16a7 100644 --- a/syntax/index.html +++ b/syntax/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/tabledata/index.html b/tabledata/index.html index 9bf3ded78..4646fa138 100644 --- a/tabledata/index.html +++ b/tabledata/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/tags/index.html b/tags/index.html index 3de12c347..4316e250b 100644 --- a/tags/index.html +++ b/tags/index.html @@ -3258,8 +3258,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • @@ -14122,7 +14122,7 @@

      summary

    • 1.x - 小结与测试 - C++基础
    • 10.x - 小结与测试 - 枚举和结构体
    • 10.y - 学会使用C++参考手册
    • -
    • 11.x - 小结与测试 - 数组、字符串和动态分配
    • +
    • 11.x - 小结与测试 - 数组、字符串和动态分配
    • 12.8 - lambda 闭包
    • 12.x - 小结与测试 - 函数
    • 13.x - 小结与测试 - 面向对象基础
    • diff --git a/template-class-specialization/index.html b/template-class-specialization/index.html index f6eeff565..fcb445436 100644 --- a/template-class-specialization/index.html +++ b/template-class-specialization/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/template-type-parameters/index.html b/template-type-parameters/index.html index 413b81860..332cfd723 100644 --- a/template-type-parameters/index.html +++ b/template-type-parameters/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/the-diamond-problem/index.html b/the-diamond-problem/index.html index e92b6a1d8..2719ef541 100644 --- a/the-diamond-problem/index.html +++ b/the-diamond-problem/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/transitive includes/index.html b/transitive includes/index.html index 69169e14e..1710e3c16 100644 --- a/transitive includes/index.html +++ b/transitive includes/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/translation/index.html b/translation/index.html index fc97df423..30134eb5e 100644 --- a/translation/index.html +++ b/translation/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/trivial-conversion/index.html b/trivial-conversion/index.html index f28acff3b..7c4796552 100644 --- a/trivial-conversion/index.html +++ b/trivial-conversion/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/type deduction/index.html b/type deduction/index.html index 20dbb467a..bb0296177 100644 --- a/type deduction/index.html +++ b/type deduction/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/type-aliases/index.html b/type-aliases/index.html index e955e2606..95bd26122 100644 --- a/type-aliases/index.html +++ b/type-aliases/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/type-conversion/index.html b/type-conversion/index.html index bea5f19e2..7c20d340f 100644 --- a/type-conversion/index.html +++ b/type-conversion/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/type-definition/index.html b/type-definition/index.html index 9a62d3b28..2f6058b01 100644 --- a/type-definition/index.html +++ b/type-definition/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/type-inference/index.html b/type-inference/index.html index a3e5a5bee..d8f6aaa4b 100644 --- a/type-inference/index.html +++ b/type-inference/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/type-safe/index.html b/type-safe/index.html index 8607ffe6d..bb37c2010 100644 --- a/type-safe/index.html +++ b/type-safe/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/type-signature/index.html b/type-signature/index.html index 99bb9a2ef..2f6772264 100644 --- a/type-signature/index.html +++ b/type-signature/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/typeid/index.html b/typeid/index.html index 1b63be79c..da6f431b3 100644 --- a/typeid/index.html +++ b/typeid/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/undefined-behavior/index.html b/undefined-behavior/index.html index 934bedb7f..7cff68192 100644 --- a/undefined-behavior/index.html +++ b/undefined-behavior/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/uniform-initialization/index.html b/uniform-initialization/index.html index 67857d331..c54b7fe33 100644 --- a/uniform-initialization/index.html +++ b/uniform-initialization/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/unnamed-namespace/index.html b/unnamed-namespace/index.html index 9b892ff67..d132c685b 100644 --- a/unnamed-namespace/index.html +++ b/unnamed-namespace/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/unscoped-enumerations/index.html b/unscoped-enumerations/index.html index e9647f002..28f310061 100644 --- a/unscoped-enumerations/index.html +++ b/unscoped-enumerations/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/upcasting/index.html b/upcasting/index.html index a0a1e3b57..728749d99 100644 --- a/upcasting/index.html +++ b/upcasting/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/using-declaration/index.html b/using-declaration/index.html index bd225480f..c81a8dbf4 100644 --- a/using-declaration/index.html +++ b/using-declaration/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/using-directive/index.html b/using-directive/index.html index 5f7dda299..2b7014114 100644 --- a/using-directive/index.html +++ b/using-directive/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/value-category/index.html b/value-category/index.html index 7dec98283..c049abd8e 100644 --- a/value-category/index.html +++ b/value-category/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/value-container/index.html b/value-container/index.html index c2a003e8b..d9b475173 100644 --- a/value-container/index.html +++ b/value-container/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/value-initialization/index.html b/value-initialization/index.html index fe86a88e0..120ccc6b8 100644 --- a/value-initialization/index.html +++ b/value-initialization/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/value-preserving/index.html b/value-preserving/index.html index f69355e7a..3b2ece2c6 100644 --- a/value-preserving/index.html +++ b/value-preserving/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/virtual-base-class/index.html b/virtual-base-class/index.html index 306d31ade..77b25b525 100644 --- a/virtual-base-class/index.html +++ b/virtual-base-class/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/virtual-destructor/index.html b/virtual-destructor/index.html index 7e2c388d5..a60676ead 100644 --- a/virtual-destructor/index.html +++ b/virtual-destructor/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/virtual-function/index.html b/virtual-function/index.html index c96abee33..2a14b30ab 100644 --- a/virtual-function/index.html +++ b/virtual-function/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/virtual-table/index.html b/virtual-table/index.html index 785e22b16..eb5ef17a2 100644 --- a/virtual-table/index.html +++ b/virtual-table/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/void pointer/index.html b/void pointer/index.html index 0630760ed..71efe77fa 100644 --- a/void pointer/index.html +++ b/void pointer/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/wild-pointer/index.html b/wild-pointer/index.html index e15703a01..db06b335b 100644 --- a/wild-pointer/index.html +++ b/wild-pointer/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/word/index.html b/word/index.html index 624c2d80a..a6ded2960 100644 --- a/word/index.html +++ b/word/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git a/zero-initialization/index.html b/zero-initialization/index.html index 482ce600a..e70d437af 100644 --- a/zero-initialization/index.html +++ b/zero-initialization/index.html @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git "a/\346\213\254\345\217\267\345\210\235\345\247\213\345\214\226/index.html" "b/\346\213\254\345\217\267\345\210\235\345\247\213\345\214\226/index.html" index 66d53e782..fd46bcbc1 100644 --- "a/\346\213\254\345\217\267\345\210\235\345\247\213\345\214\226/index.html" +++ "b/\346\213\254\345\217\267\345\210\235\345\247\213\345\214\226/index.html" @@ -3251,8 +3251,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None
    • diff --git "a/\347\277\273\350\257\221\347\272\246\345\256\232/index.html" "b/\347\277\273\350\257\221\347\272\246\345\256\232/index.html" index e105344e4..2e419871a 100644 --- "a/\347\277\273\350\257\221\347\272\246\345\256\232/index.html" +++ "b/\347\277\273\350\257\221\347\272\246\345\256\232/index.html" @@ -3258,8 +3258,8 @@
    • - - 11.x - 小结与测试 - 数组、字符串和动态分配 + + None